Bug Summary

File:3rdparty/sqlite3/sqlite3.c
Warning:line 140059, column 15
Access to field 'pSrc' results in a dereference of a null pointer (loaded from variable 'pSub')

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/18 -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/18/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-07-22-155139-54044-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.39.2. By combining all the individual C code files into this
4** single large file, the entire code can be compiled as a single translation
5** unit. This allows many compilers to do optimizations that would not be
6** possible if the files were compiled separately. Performance improvements
7** of 5% or more are commonly seen when SQLite is compiled as a single
8** translation unit.
9**
10** This file is all you need to compile SQLite. To use SQLite in other
11** programs, you need this file and the "sqlite3.h" header file that defines
12** the programming interface to the SQLite library. (If you do not have
13** the "sqlite3.h" header file at hand, you will find a copy embedded within
14** the text of this file. Search for "Begin file sqlite3.h" to find the start
15** of the embedded sqlite3.h header file.) Additional code files may be needed
16** if you want a wrapper to interface SQLite with your choice of programming
17** language. The code for the "sqlite3" command-line shell is also in a
18** separate file. This file contains only code for the core SQLite library.
19*/
20#define SQLITE_CORE1 1
21#define SQLITE_AMALGAMATION1 1
22#ifndef SQLITE_PRIVATEstatic
23# define SQLITE_PRIVATEstatic static
24#endif
25/************** Begin file sqliteInt.h ***************************************/
26/*
27** 2001 September 15
28**
29** The author disclaims copyright to this source code. In place of
30** a legal notice, here is a blessing:
31**
32** May you do good and not evil.
33** May you find forgiveness for yourself and forgive others.
34** May you share freely, never taking more than you give.
35**
36*************************************************************************
37** Internal interface definitions for SQLite.
38**
39*/
40#ifndef SQLITEINT_H
41#define SQLITEINT_H
42
43/* Special Comments:
44**
45** Some comments have special meaning to the tools that measure test
46** coverage:
47**
48** NO_TEST - The branches on this line are not
49** measured by branch coverage. This is
50** used on lines of code that actually
51** implement parts of coverage testing.
52**
53** OPTIMIZATION-IF-TRUE - This branch is allowed to alway be false
54** and the correct answer is still obtained,
55** though perhaps more slowly.
56**
57** OPTIMIZATION-IF-FALSE - This branch is allowed to alway be true
58** and the correct answer is still obtained,
59** though perhaps more slowly.
60**
61** PREVENTS-HARMLESS-OVERREAD - This branch prevents a buffer overread
62** that would be harmless and undetectable
63** if it did occur.
64**
65** In all cases, the special comment must be enclosed in the usual
66** slash-asterisk...asterisk-slash comment marks, with no spaces between the
67** asterisks and the comment text.
68*/
69
70/*
71** Make sure the Tcl calling convention macro is defined. This macro is
72** only used by test code and Tcl integration code.
73*/
74#ifndef SQLITE_TCLAPI
75# define SQLITE_TCLAPI
76#endif
77
78/*
79** Include the header file used to customize the compiler options for MSVC.
80** This should be done first so that it can successfully prevent spurious
81** compiler warnings due to subsequent content in this file and other files
82** that are included by this file.
83*/
84/************** Include msvc.h in the middle of sqliteInt.h ******************/
85/************** Begin file msvc.h ********************************************/
86/*
87** 2015 January 12
88**
89** The author disclaims copyright to this source code. In place of
90** a legal notice, here is a blessing:
91**
92** May you do good and not evil.
93** May you find forgiveness for yourself and forgive others.
94** May you share freely, never taking more than you give.
95**
96******************************************************************************
97**
98** This file contains code that is specific to MSVC.
99*/
100#ifndef SQLITE_MSVC_H
101#define SQLITE_MSVC_H
102
103#if defined(_MSC_VER)
104#pragma warning(disable : 4054)
105#pragma warning(disable : 4055)
106#pragma warning(disable : 4100)
107#pragma warning(disable : 4127)
108#pragma warning(disable : 4130)
109#pragma warning(disable : 4152)
110#pragma warning(disable : 4189)
111#pragma warning(disable : 4206)
112#pragma warning(disable : 4210)
113#pragma warning(disable : 4232)
114#pragma warning(disable : 4244)
115#pragma warning(disable : 4305)
116#pragma warning(disable : 4306)
117#pragma warning(disable : 4702)
118#pragma warning(disable : 4706)
119#endif /* defined(_MSC_VER) */
120
121#if defined(_MSC_VER) && !defined(_WIN64)
122#undef SQLITE_4_BYTE_ALIGNED_MALLOC
123#define SQLITE_4_BYTE_ALIGNED_MALLOC
124#endif /* defined(_MSC_VER) && !defined(_WIN64) */
125
126#endif /* SQLITE_MSVC_H */
127
128/************** End of msvc.h ************************************************/
129/************** Continuing where we left off in sqliteInt.h ******************/
130
131/*
132** Special setup for VxWorks
133*/
134/************** Include vxworks.h in the middle of sqliteInt.h ***************/
135/************** Begin file vxworks.h *****************************************/
136/*
137** 2015-03-02
138**
139** The author disclaims copyright to this source code. In place of
140** a legal notice, here is a blessing:
141**
142** May you do good and not evil.
143** May you find forgiveness for yourself and forgive others.
144** May you share freely, never taking more than you give.
145**
146******************************************************************************
147**
148** This file contains code that is specific to Wind River's VxWorks
149*/
150#if defined(__RTP__) || defined(_WRS_KERNEL)
151/* This is VxWorks. Set up things specially for that OS
152*/
153#include <vxWorks.h>
154#include <pthread.h> /* amalgamator: dontcache */
155#define OS_VXWORKS0 1
156#define SQLITE_OS_OTHER0 0
157#define SQLITE_HOMEGROWN_RECURSIVE_MUTEX 1
158#define SQLITE_OMIT_LOAD_EXTENSION1 1
159#define SQLITE_ENABLE_LOCKING_STYLE0 0
160#define HAVE_UTIME 1
161#else
162/* This is not VxWorks. */
163#define OS_VXWORKS0 0
164#define HAVE_FCHOWN1 1
165#define HAVE_READLINK1 1
166#define HAVE_LSTAT1 1
167#endif /* defined(_WRS_KERNEL) */
168
169/************** End of vxworks.h *********************************************/
170/************** Continuing where we left off in sqliteInt.h ******************/
171
172/*
173** These #defines should enable >2GB file support on POSIX if the
174** underlying operating system supports it. If the OS lacks
175** large file support, or if the OS is windows, these should be no-ops.
176**
177** Ticket #2739: The _LARGEFILE_SOURCE macro must appear before any
178** system #includes. Hence, this block of code must be the very first
179** code in all source files.
180**
181** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
182** on the compiler command line. This is necessary if you are compiling
183** on a recent machine (ex: Red Hat 7.2) but you want your code to work
184** on an older machine (ex: Red Hat 6.0). If you compile on Red Hat 7.2
185** without this option, LFS is enable. But LFS does not exist in the kernel
186** in Red Hat 6.0, so the code won't work. Hence, for maximum binary
187** portability you should omit LFS.
188**
189** The previous paragraph was written in 2005. (This paragraph is written
190** on 2008-11-28.) These days, all Linux kernels support large files, so
191** you should probably leave LFS enabled. But some embedded platforms might
192** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
193**
194** Similar is true for Mac OS X. LFS is only supported on Mac OS X 9 and later.
195*/
196#ifndef SQLITE_DISABLE_LFS
197# define _LARGE_FILE1 1
198# ifndef _FILE_OFFSET_BITS64
199# define _FILE_OFFSET_BITS64 64
200# endif
201# define _LARGEFILE_SOURCE1 1
202#endif
203
204/* The GCC_VERSION and MSVC_VERSION macros are used to
205** conditionally include optimizations for each of these compilers. A
206** value of 0 means that compiler is not being used. The
207** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific
208** optimizations, and hence set all compiler macros to 0
209**
210** There was once also a CLANG_VERSION macro. However, we learn that the
211** version numbers in clang are for "marketing" only and are inconsistent
212** and unreliable. Fortunately, all versions of clang also recognize the
213** gcc version numbers and have reasonable settings for gcc version numbers,
214** so the GCC_VERSION macro will be set to a correct non-zero value even
215** when compiling with clang.
216*/
217#if defined(__GNUC__4) && !defined(SQLITE_DISABLE_INTRINSIC)
218# define GCC_VERSION(4*1000000+2*1000+1) (__GNUC__4*1000000+__GNUC_MINOR__2*1000+__GNUC_PATCHLEVEL__1)
219#else
220# define GCC_VERSION(4*1000000+2*1000+1) 0
221#endif
222#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
223# define MSVC_VERSION0 _MSC_VER
224#else
225# define MSVC_VERSION0 0
226#endif
227
228/*
229** Some C99 functions in "math.h" are only present for MSVC when its version
230** is associated with Visual Studio 2013 or higher.
231*/
232#ifndef SQLITE_HAVE_C99_MATH_FUNCS(1)
233# if MSVC_VERSION0==0 || MSVC_VERSION0>=1800
234# define SQLITE_HAVE_C99_MATH_FUNCS(1) (1)
235# else
236# define SQLITE_HAVE_C99_MATH_FUNCS(1) (0)
237# endif
238#endif
239
240/* Needed for various definitions... */
241#if defined(__GNUC__4) && !defined(_GNU_SOURCE)
242# define _GNU_SOURCE
243#endif
244
245#if defined(__OpenBSD__) && !defined(_BSD_SOURCE)
246# define _BSD_SOURCE
247#endif
248
249/*
250** Macro to disable warnings about missing "break" at the end of a "case".
251*/
252#if GCC_VERSION(4*1000000+2*1000+1)>=7000000
253# define deliberate_fall_through __attribute__((fallthrough));
254#else
255# define deliberate_fall_through
256#endif
257
258/*
259** For MinGW, check to see if we can include the header file containing its
260** version information, among other things. Normally, this internal MinGW
261** header file would [only] be included automatically by other MinGW header
262** files; however, the contained version information is now required by this
263** header file to work around binary compatibility issues (see below) and
264** this is the only known way to reliably obtain it. This entire #if block
265** would be completely unnecessary if there was any other way of detecting
266** MinGW via their preprocessor (e.g. if they customized their GCC to define
267** some MinGW-specific macros). When compiling for MinGW, either the
268** _HAVE_MINGW_H or _HAVE__MINGW_H (note the extra underscore) macro must be
269** defined; otherwise, detection of conditions specific to MinGW will be
270** disabled.
271*/
272#if defined(_HAVE_MINGW_H)
273# include "mingw.h"
274#elif defined(_HAVE__MINGW_H)
275# include "_mingw.h"
276#endif
277
278/*
279** For MinGW version 4.x (and higher), check to see if the _USE_32BIT_TIME_T
280** define is required to maintain binary compatibility with the MSVC runtime
281** library in use (e.g. for Windows XP).
282*/
283#if !defined(_USE_32BIT_TIME_T) && !defined(_USE_64BIT_TIME_T) && \
284 defined(_WIN32) && !defined(_WIN64) && \
285 defined(__MINGW_MAJOR_VERSION) && __MINGW_MAJOR_VERSION >= 4 && \
286 defined(__MSVCRT__)
287# define _USE_32BIT_TIME_T
288#endif
289
290/* Optionally #include a user-defined header, whereby compilation options
291** may be set prior to where they take effect, but after platform setup.
292** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include
293** file.
294*/
295#ifdef SQLITE_CUSTOM_INCLUDE
296# define INC_STRINGIFY_(f) #f
297# define INC_STRINGIFY(f) INC_STRINGIFY_(f)
298# include INC_STRINGIFY(SQLITE_CUSTOM_INCLUDE)
299#endif
300
301/* The public SQLite interface. The _FILE_OFFSET_BITS macro must appear
302** first in QNX. Also, the _USE_32BIT_TIME_T macro must appear first for
303** MinGW.
304*/
305/************** Include sqlite3.h in the middle of sqliteInt.h ***************/
306/************** Begin file sqlite3.h *****************************************/
307/*
308** 2001-09-15
309**
310** The author disclaims copyright to this source code. In place of
311** a legal notice, here is a blessing:
312**
313** May you do good and not evil.
314** May you find forgiveness for yourself and forgive others.
315** May you share freely, never taking more than you give.
316**
317*************************************************************************
318** This header file defines the interface that the SQLite library
319** presents to client programs. If a C-function, structure, datatype,
320** or constant definition does not appear in this file, then it is
321** not a published API of SQLite, is subject to change without
322** notice, and should not be referenced by programs that use SQLite.
323**
324** Some of the definitions that are in this file are marked as
325** "experimental". Experimental interfaces are normally new
326** features recently added to SQLite. We do not anticipate changes
327** to experimental interfaces but reserve the right to make minor changes
328** if experience from use "in the wild" suggest such changes are prudent.
329**
330** The official C-language API documentation for SQLite is derived
331** from comments in this file. This file is the authoritative source
332** on how SQLite interfaces are supposed to operate.
333**
334** The name of this file under configuration management is "sqlite.h.in".
335** The makefile makes some minor changes to this file (such as inserting
336** the version number) and changes its name to "sqlite3.h" as
337** part of the build process.
338*/
339#ifndef SQLITE3_H
340#define SQLITE3_H
341#include <stdarg.h> /* Needed for the definition of va_list */
342
343/*
344** Make sure we can call this stuff from C++.
345*/
346#if 0
347extern "C" {
348#endif
349
350
351/*
352** Facilitate override of interface linkage and calling conventions.
353** Be aware that these macros may not be used within this particular
354** translation of the amalgamation and its associated header file.
355**
356** The SQLITE_EXTERN and SQLITE_API macros are used to instruct the
357** compiler that the target identifier should have external linkage.
358**
359** The SQLITE_CDECL macro is used to set the calling convention for
360** public functions that accept a variable number of arguments.
361**
362** The SQLITE_APICALL macro is used to set the calling convention for
363** public functions that accept a fixed number of arguments.
364**
365** The SQLITE_STDCALL macro is no longer used and is now deprecated.
366**
367** The SQLITE_CALLBACK macro is used to set the calling convention for
368** function pointers.
369**
370** The SQLITE_SYSAPI macro is used to set the calling convention for
371** functions provided by the operating system.
372**
373** Currently, the SQLITE_CDECL, SQLITE_APICALL, SQLITE_CALLBACK, and
374** SQLITE_SYSAPI macros are used only when building for environments
375** that require non-default calling conventions.
376*/
377#ifndef SQLITE_EXTERNextern
378# define SQLITE_EXTERNextern extern
379#endif
380#ifndef SQLITE_API
381# define SQLITE_API
382#endif
383#ifndef SQLITE_CDECL
384# define SQLITE_CDECL
385#endif
386#ifndef SQLITE_APICALL
387# define SQLITE_APICALL
388#endif
389#ifndef SQLITE_STDCALL
390# define SQLITE_STDCALL SQLITE_APICALL
391#endif
392#ifndef SQLITE_CALLBACK
393# define SQLITE_CALLBACK
394#endif
395#ifndef SQLITE_SYSAPI
396# define SQLITE_SYSAPI
397#endif
398
399/*
400** These no-op macros are used in front of interfaces to mark those
401** interfaces as either deprecated or experimental. New applications
402** should not use deprecated interfaces - they are supported for backwards
403** compatibility only. Application writers should be aware that
404** experimental interfaces are subject to change in point releases.
405**
406** These macros used to resolve to various kinds of compiler magic that
407** would generate warning messages when they were used. But that
408** compiler magic ended up generating such a flurry of bug reports
409** that we have taken it all out and gone back to using simple
410** noop macros.
411*/
412#define SQLITE_DEPRECATED
413#define SQLITE_EXPERIMENTAL
414
415/*
416** Ensure these symbols were not defined by some previous header file.
417*/
418#ifdef SQLITE_VERSION"3.39.2"
419# undef SQLITE_VERSION"3.39.2"
420#endif
421#ifdef SQLITE_VERSION_NUMBER3039002
422# undef SQLITE_VERSION_NUMBER3039002
423#endif
424
425/*
426** CAPI3REF: Compile-Time Library Version Numbers
427**
428** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header
429** evaluates to a string literal that is the SQLite version in the
430** format "X.Y.Z" where X is the major version number (always 3 for
431** SQLite3) and Y is the minor version number and Z is the release number.)^
432** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer
433** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
434** numbers used in [SQLITE_VERSION].)^
435** The SQLITE_VERSION_NUMBER for any given release of SQLite will also
436** be larger than the release from which it is derived. Either Y will
437** be held constant and Z will be incremented or else Y will be incremented
438** and Z will be reset to zero.
439**
440** Since [version 3.6.18] ([dateof:3.6.18]),
441** SQLite source code has been stored in the
442** <a href="http://www.fossil-scm.org/">Fossil configuration management
443** system</a>. ^The SQLITE_SOURCE_ID macro evaluates to
444** a string which identifies a particular check-in of SQLite
445** within its configuration management system. ^The SQLITE_SOURCE_ID
446** string contains the date and time of the check-in (UTC) and a SHA1
447** or SHA3-256 hash of the entire source tree. If the source code has
448** been edited in any way since it was last checked in, then the last
449** four hexadecimal digits of the hash may be modified.
450**
451** See also: [sqlite3_libversion()],
452** [sqlite3_libversion_number()], [sqlite3_sourceid()],
453** [sqlite_version()] and [sqlite_source_id()].
454*/
455#define SQLITE_VERSION"3.39.2" "3.39.2"
456#define SQLITE_VERSION_NUMBER3039002 3039002
457#define SQLITE_SOURCE_ID"2022-07-21 15:24:47 698edb77537b67c41adc68f9b892db56bcf9a55e00371a61420f3ddd668e6603" "2022-07-21 15:24:47 698edb77537b67c41adc68f9b892db56bcf9a55e00371a61420f3ddd668e6603"
458
459/*
460** CAPI3REF: Run-Time Library Version Numbers
461** KEYWORDS: sqlite3_version sqlite3_sourceid
462**
463** These interfaces provide the same information as the [SQLITE_VERSION],
464** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
465** but are associated with the library instead of the header file. ^(Cautious
466** programmers might include assert() statements in their application to
467** verify that values returned by these interfaces match the macros in
468** the header, and thus ensure that the application is
469** compiled with matching library and header files.
470**
471** <blockquote><pre>
472** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
473** assert( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,80)==0 );
474** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
475** </pre></blockquote>)^
476**
477** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
478** macro. ^The sqlite3_libversion() function returns a pointer to the
479** to the sqlite3_version[] string constant. The sqlite3_libversion()
480** function is provided for use in DLLs since DLL users usually do not have
481** direct access to string constants within the DLL. ^The
482** sqlite3_libversion_number() function returns an integer equal to
483** [SQLITE_VERSION_NUMBER]. ^(The sqlite3_sourceid() function returns
484** a pointer to a string constant whose value is the same as the
485** [SQLITE_SOURCE_ID] C preprocessor macro. Except if SQLite is built
486** using an edited copy of [the amalgamation], then the last four characters
487** of the hash might be different from [SQLITE_SOURCE_ID].)^
488**
489** See also: [sqlite_version()] and [sqlite_source_id()].
490*/
491SQLITE_API const char sqlite3_version[] = SQLITE_VERSION"3.39.2";
492SQLITE_API const char *sqlite3_libversion(void);
493SQLITE_API const char *sqlite3_sourceid(void);
494SQLITE_API int sqlite3_libversion_number(void);
495
496/*
497** CAPI3REF: Run-Time Library Compilation Options Diagnostics
498**
499** ^The sqlite3_compileoption_used() function returns 0 or 1
500** indicating whether the specified option was defined at
501** compile time. ^The SQLITE_ prefix may be omitted from the
502** option name passed to sqlite3_compileoption_used().
503**
504** ^The sqlite3_compileoption_get() function allows iterating
505** over the list of options that were defined at compile time by
506** returning the N-th compile time option string. ^If N is out of range,
507** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_
508** prefix is omitted from any strings returned by
509** sqlite3_compileoption_get().
510**
511** ^Support for the diagnostic functions sqlite3_compileoption_used()
512** and sqlite3_compileoption_get() may be omitted by specifying the
513** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
514**
515** See also: SQL functions [sqlite_compileoption_used()] and
516** [sqlite_compileoption_get()] and the [compile_options pragma].
517*/
518#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
519SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
520SQLITE_API const char *sqlite3_compileoption_get(int N);
521#else
522# define sqlite3_compileoption_used(X) 0
523# define sqlite3_compileoption_get(X) ((void*)0)
524#endif
525
526/*
527** CAPI3REF: Test To See If The Library Is Threadsafe
528**
529** ^The sqlite3_threadsafe() function returns zero if and only if
530** SQLite was compiled with mutexing code omitted due to the
531** [SQLITE_THREADSAFE] compile-time option being set to 0.
532**
533** SQLite can be compiled with or without mutexes. When
534** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
535** are enabled and SQLite is threadsafe. When the
536** [SQLITE_THREADSAFE] macro is 0,
537** the mutexes are omitted. Without the mutexes, it is not safe
538** to use SQLite concurrently from more than one thread.
539**
540** Enabling mutexes incurs a measurable performance penalty.
541** So if speed is of utmost importance, it makes sense to disable
542** the mutexes. But for maximum safety, mutexes should be enabled.
543** ^The default behavior is for mutexes to be enabled.
544**
545** This interface can be used by an application to make sure that the
546** version of SQLite that it is linking against was compiled with
547** the desired setting of the [SQLITE_THREADSAFE] macro.
548**
549** This interface only reports on the compile-time mutex setting
550** of the [SQLITE_THREADSAFE] flag. If SQLite is compiled with
551** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but
552** can be fully or partially disabled using a call to [sqlite3_config()]
553** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
554** or [SQLITE_CONFIG_SERIALIZED]. ^(The return value of the
555** sqlite3_threadsafe() function shows only the compile-time setting of
556** thread safety, not any run-time changes to that setting made by
557** sqlite3_config(). In other words, the return value from sqlite3_threadsafe()
558** is unchanged by calls to sqlite3_config().)^
559**
560** See the [threading mode] documentation for additional information.
561*/
562SQLITE_API int sqlite3_threadsafe(void);
563
564/*
565** CAPI3REF: Database Connection Handle
566** KEYWORDS: {database connection} {database connections}
567**
568** Each open SQLite database is represented by a pointer to an instance of
569** the opaque structure named "sqlite3". It is useful to think of an sqlite3
570** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and
571** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
572** and [sqlite3_close_v2()] are its destructors. There are many other
573** interfaces (such as
574** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
575** [sqlite3_busy_timeout()] to name but three) that are methods on an
576** sqlite3 object.
577*/
578typedef struct sqlite3 sqlite3;
579
580/*
581** CAPI3REF: 64-Bit Integer Types
582** KEYWORDS: sqlite_int64 sqlite_uint64
583**
584** Because there is no cross-platform way to specify 64-bit integer types
585** SQLite includes typedefs for 64-bit signed and unsigned integers.
586**
587** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
588** The sqlite_int64 and sqlite_uint64 types are supported for backwards
589** compatibility only.
590**
591** ^The sqlite3_int64 and sqlite_int64 types can store integer values
592** between -9223372036854775808 and +9223372036854775807 inclusive. ^The
593** sqlite3_uint64 and sqlite_uint64 types can store integer values
594** between 0 and +18446744073709551615 inclusive.
595*/
596#ifdef SQLITE_INT64_TYPE
597 typedef SQLITE_INT64_TYPE sqlite_int64;
598# ifdef SQLITE_UINT64_TYPE
599 typedef SQLITE_UINT64_TYPE sqlite_uint64;
600# else
601 typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
602# endif
603#elif defined(_MSC_VER) || defined(__BORLANDC__)
604 typedef __int64 sqlite_int64;
605 typedef unsigned __int64 sqlite_uint64;
606#else
607 typedef long long int sqlite_int64;
608 typedef unsigned long long int sqlite_uint64;
609#endif
610typedef sqlite_int64 sqlite3_int64;
611typedef sqlite_uint64 sqlite3_uint64;
612
613/*
614** If compiling for a processor that lacks floating point support,
615** substitute integer for floating-point.
616*/
617#ifdef SQLITE_OMIT_FLOATING_POINT
618# define double sqlite3_int64
619#endif
620
621/*
622** CAPI3REF: Closing A Database Connection
623** DESTRUCTOR: sqlite3
624**
625** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
626** for the [sqlite3] object.
627** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if
628** the [sqlite3] object is successfully destroyed and all associated
629** resources are deallocated.
630**
631** Ideally, applications should [sqlite3_finalize | finalize] all
632** [prepared statements], [sqlite3_blob_close | close] all [BLOB handles], and
633** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
634** with the [sqlite3] object prior to attempting to close the object.
635** ^If the database connection is associated with unfinalized prepared
636** statements, BLOB handlers, and/or unfinished sqlite3_backup objects then
637** sqlite3_close() will leave the database connection open and return
638** [SQLITE_BUSY]. ^If sqlite3_close_v2() is called with unfinalized prepared
639** statements, unclosed BLOB handlers, and/or unfinished sqlite3_backups,
640** it returns [SQLITE_OK] regardless, but instead of deallocating the database
641** connection immediately, it marks the database connection as an unusable
642** "zombie" and makes arrangements to automatically deallocate the database
643** connection after all prepared statements are finalized, all BLOB handles
644** are closed, and all backups have finished. The sqlite3_close_v2() interface
645** is intended for use with host languages that are garbage collected, and
646** where the order in which destructors are called is arbitrary.
647**
648** ^If an [sqlite3] object is destroyed while a transaction is open,
649** the transaction is automatically rolled back.
650**
651** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)]
652** must be either a NULL
653** pointer or an [sqlite3] object pointer obtained
654** from [sqlite3_open()], [sqlite3_open16()], or
655** [sqlite3_open_v2()], and not previously closed.
656** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
657** argument is a harmless no-op.
658*/
659SQLITE_API int sqlite3_close(sqlite3*);
660SQLITE_API int sqlite3_close_v2(sqlite3*);
661
662/*
663** The type for a callback function.
664** This is legacy and deprecated. It is included for historical
665** compatibility and is not documented.
666*/
667typedef int (*sqlite3_callback)(void*,int,char**, char**);
668
669/*
670** CAPI3REF: One-Step Query Execution Interface
671** METHOD: sqlite3
672**
673** The sqlite3_exec() interface is a convenience wrapper around
674** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
675** that allows an application to run multiple statements of SQL
676** without having to use a lot of C code.
677**
678** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
679** semicolon-separate SQL statements passed into its 2nd argument,
680** in the context of the [database connection] passed in as its 1st
681** argument. ^If the callback function of the 3rd argument to
682** sqlite3_exec() is not NULL, then it is invoked for each result row
683** coming out of the evaluated SQL statements. ^The 4th argument to
684** sqlite3_exec() is relayed through to the 1st argument of each
685** callback invocation. ^If the callback pointer to sqlite3_exec()
686** is NULL, then no callback is ever invoked and result rows are
687** ignored.
688**
689** ^If an error occurs while evaluating the SQL statements passed into
690** sqlite3_exec(), then execution of the current statement stops and
691** subsequent statements are skipped. ^If the 5th parameter to sqlite3_exec()
692** is not NULL then any error message is written into memory obtained
693** from [sqlite3_malloc()] and passed back through the 5th parameter.
694** To avoid memory leaks, the application should invoke [sqlite3_free()]
695** on error message strings returned through the 5th parameter of
696** sqlite3_exec() after the error message string is no longer needed.
697** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
698** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
699** NULL before returning.
700**
701** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
702** routine returns SQLITE_ABORT without invoking the callback again and
703** without running any subsequent SQL statements.
704**
705** ^The 2nd argument to the sqlite3_exec() callback function is the
706** number of columns in the result. ^The 3rd argument to the sqlite3_exec()
707** callback is an array of pointers to strings obtained as if from
708** [sqlite3_column_text()], one for each column. ^If an element of a
709** result row is NULL then the corresponding string pointer for the
710** sqlite3_exec() callback is a NULL pointer. ^The 4th argument to the
711** sqlite3_exec() callback is an array of pointers to strings where each
712** entry represents the name of corresponding result column as obtained
713** from [sqlite3_column_name()].
714**
715** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
716** to an empty string, or a pointer that contains only whitespace and/or
717** SQL comments, then no SQL statements are evaluated and the database
718** is not changed.
719**
720** Restrictions:
721**
722** <ul>
723** <li> The application must ensure that the 1st parameter to sqlite3_exec()
724** is a valid and open [database connection].
725** <li> The application must not close the [database connection] specified by
726** the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
727** <li> The application must not modify the SQL statement text passed into
728** the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
729** </ul>
730*/
731SQLITE_API int sqlite3_exec(
732 sqlite3*, /* An open database */
733 const char *sql, /* SQL to be evaluated */
734 int (*callback)(void*,int,char**,char**), /* Callback function */
735 void *, /* 1st argument to callback */
736 char **errmsg /* Error msg written here */
737);
738
739/*
740** CAPI3REF: Result Codes
741** KEYWORDS: {result code definitions}
742**
743** Many SQLite functions return an integer result code from the set shown
744** here in order to indicate success or failure.
745**
746** New error codes may be added in future versions of SQLite.
747**
748** See also: [extended result code definitions]
749*/
750#define SQLITE_OK0 0 /* Successful result */
751/* beginning-of-error-codes */
752#define SQLITE_ERROR1 1 /* Generic error */
753#define SQLITE_INTERNAL2 2 /* Internal logic error in SQLite */
754#define SQLITE_PERM3 3 /* Access permission denied */
755#define SQLITE_ABORT4 4 /* Callback routine requested an abort */
756#define SQLITE_BUSY5 5 /* The database file is locked */
757#define SQLITE_LOCKED6 6 /* A table in the database is locked */
758#define SQLITE_NOMEM7 7 /* A malloc() failed */
759#define SQLITE_READONLY8 8 /* Attempt to write a readonly database */
760#define SQLITE_INTERRUPT9 9 /* Operation terminated by sqlite3_interrupt()*/
761#define SQLITE_IOERR10 10 /* Some kind of disk I/O error occurred */
762#define SQLITE_CORRUPT11 11 /* The database disk image is malformed */
763#define SQLITE_NOTFOUND12 12 /* Unknown opcode in sqlite3_file_control() */
764#define SQLITE_FULL13 13 /* Insertion failed because database is full */
765#define SQLITE_CANTOPEN14 14 /* Unable to open the database file */
766#define SQLITE_PROTOCOL15 15 /* Database lock protocol error */
767#define SQLITE_EMPTY16 16 /* Internal use only */
768#define SQLITE_SCHEMA17 17 /* The database schema changed */
769#define SQLITE_TOOBIG18 18 /* String or BLOB exceeds size limit */
770#define SQLITE_CONSTRAINT19 19 /* Abort due to constraint violation */
771#define SQLITE_MISMATCH20 20 /* Data type mismatch */
772#define SQLITE_MISUSE21 21 /* Library used incorrectly */
773#define SQLITE_NOLFS22 22 /* Uses OS features not supported on host */
774#define SQLITE_AUTH23 23 /* Authorization denied */
775#define SQLITE_FORMAT24 24 /* Not used */
776#define SQLITE_RANGE25 25 /* 2nd parameter to sqlite3_bind out of range */
777#define SQLITE_NOTADB26 26 /* File opened that is not a database file */
778#define SQLITE_NOTICE27 27 /* Notifications from sqlite3_log() */
779#define SQLITE_WARNING28 28 /* Warnings from sqlite3_log() */
780#define SQLITE_ROW100 100 /* sqlite3_step() has another row ready */
781#define SQLITE_DONE101 101 /* sqlite3_step() has finished executing */
782/* end-of-error-codes */
783
784/*
785** CAPI3REF: Extended Result Codes
786** KEYWORDS: {extended result code definitions}
787**
788** In its default configuration, SQLite API routines return one of 30 integer
789** [result codes]. However, experience has shown that many of
790** these result codes are too coarse-grained. They do not provide as
791** much information about problems as programmers might like. In an effort to
792** address this, newer versions of SQLite (version 3.3.8 [dateof:3.3.8]
793** and later) include
794** support for additional result codes that provide more detailed information
795** about errors. These [extended result codes] are enabled or disabled
796** on a per database connection basis using the
797** [sqlite3_extended_result_codes()] API. Or, the extended code for
798** the most recent error can be obtained using
799** [sqlite3_extended_errcode()].
800*/
801#define SQLITE_ERROR_MISSING_COLLSEQ(1 | (1<<8)) (SQLITE_ERROR1 | (1<<8))
802#define SQLITE_ERROR_RETRY(1 | (2<<8)) (SQLITE_ERROR1 | (2<<8))
803#define SQLITE_ERROR_SNAPSHOT(1 | (3<<8)) (SQLITE_ERROR1 | (3<<8))
804#define SQLITE_IOERR_READ(10 | (1<<8)) (SQLITE_IOERR10 | (1<<8))
805#define SQLITE_IOERR_SHORT_READ(10 | (2<<8)) (SQLITE_IOERR10 | (2<<8))
806#define SQLITE_IOERR_WRITE(10 | (3<<8)) (SQLITE_IOERR10 | (3<<8))
807#define SQLITE_IOERR_FSYNC(10 | (4<<8)) (SQLITE_IOERR10 | (4<<8))
808#define SQLITE_IOERR_DIR_FSYNC(10 | (5<<8)) (SQLITE_IOERR10 | (5<<8))
809#define SQLITE_IOERR_TRUNCATE(10 | (6<<8)) (SQLITE_IOERR10 | (6<<8))
810#define SQLITE_IOERR_FSTAT(10 | (7<<8)) (SQLITE_IOERR10 | (7<<8))
811#define SQLITE_IOERR_UNLOCK(10 | (8<<8)) (SQLITE_IOERR10 | (8<<8))
812#define SQLITE_IOERR_RDLOCK(10 | (9<<8)) (SQLITE_IOERR10 | (9<<8))
813#define SQLITE_IOERR_DELETE(10 | (10<<8)) (SQLITE_IOERR10 | (10<<8))
814#define SQLITE_IOERR_BLOCKED(10 | (11<<8)) (SQLITE_IOERR10 | (11<<8))
815#define SQLITE_IOERR_NOMEM(10 | (12<<8)) (SQLITE_IOERR10 | (12<<8))
816#define SQLITE_IOERR_ACCESS(10 | (13<<8)) (SQLITE_IOERR10 | (13<<8))
817#define SQLITE_IOERR_CHECKRESERVEDLOCK(10 | (14<<8)) (SQLITE_IOERR10 | (14<<8))
818#define SQLITE_IOERR_LOCK(10 | (15<<8)) (SQLITE_IOERR10 | (15<<8))
819#define SQLITE_IOERR_CLOSE(10 | (16<<8)) (SQLITE_IOERR10 | (16<<8))
820#define SQLITE_IOERR_DIR_CLOSE(10 | (17<<8)) (SQLITE_IOERR10 | (17<<8))
821#define SQLITE_IOERR_SHMOPEN(10 | (18<<8)) (SQLITE_IOERR10 | (18<<8))
822#define SQLITE_IOERR_SHMSIZE(10 | (19<<8)) (SQLITE_IOERR10 | (19<<8))
823#define SQLITE_IOERR_SHMLOCK(10 | (20<<8)) (SQLITE_IOERR10 | (20<<8))
824#define SQLITE_IOERR_SHMMAP(10 | (21<<8)) (SQLITE_IOERR10 | (21<<8))
825#define SQLITE_IOERR_SEEK(10 | (22<<8)) (SQLITE_IOERR10 | (22<<8))
826#define SQLITE_IOERR_DELETE_NOENT(10 | (23<<8)) (SQLITE_IOERR10 | (23<<8))
827#define SQLITE_IOERR_MMAP(10 | (24<<8)) (SQLITE_IOERR10 | (24<<8))
828#define SQLITE_IOERR_GETTEMPPATH(10 | (25<<8)) (SQLITE_IOERR10 | (25<<8))
829#define SQLITE_IOERR_CONVPATH(10 | (26<<8)) (SQLITE_IOERR10 | (26<<8))
830#define SQLITE_IOERR_VNODE(10 | (27<<8)) (SQLITE_IOERR10 | (27<<8))
831#define SQLITE_IOERR_AUTH(10 | (28<<8)) (SQLITE_IOERR10 | (28<<8))
832#define SQLITE_IOERR_BEGIN_ATOMIC(10 | (29<<8)) (SQLITE_IOERR10 | (29<<8))
833#define SQLITE_IOERR_COMMIT_ATOMIC(10 | (30<<8)) (SQLITE_IOERR10 | (30<<8))
834#define SQLITE_IOERR_ROLLBACK_ATOMIC(10 | (31<<8)) (SQLITE_IOERR10 | (31<<8))
835#define SQLITE_IOERR_DATA(10 | (32<<8)) (SQLITE_IOERR10 | (32<<8))
836#define SQLITE_IOERR_CORRUPTFS(10 | (33<<8)) (SQLITE_IOERR10 | (33<<8))
837#define SQLITE_LOCKED_SHAREDCACHE(6 | (1<<8)) (SQLITE_LOCKED6 | (1<<8))
838#define SQLITE_LOCKED_VTAB(6 | (2<<8)) (SQLITE_LOCKED6 | (2<<8))
839#define SQLITE_BUSY_RECOVERY(5 | (1<<8)) (SQLITE_BUSY5 | (1<<8))
840#define SQLITE_BUSY_SNAPSHOT(5 | (2<<8)) (SQLITE_BUSY5 | (2<<8))
841#define SQLITE_BUSY_TIMEOUT(5 | (3<<8)) (SQLITE_BUSY5 | (3<<8))
842#define SQLITE_CANTOPEN_NOTEMPDIR(14 | (1<<8)) (SQLITE_CANTOPEN14 | (1<<8))
843#define SQLITE_CANTOPEN_ISDIR(14 | (2<<8)) (SQLITE_CANTOPEN14 | (2<<8))
844#define SQLITE_CANTOPEN_FULLPATH(14 | (3<<8)) (SQLITE_CANTOPEN14 | (3<<8))
845#define SQLITE_CANTOPEN_CONVPATH(14 | (4<<8)) (SQLITE_CANTOPEN14 | (4<<8))
846#define SQLITE_CANTOPEN_DIRTYWAL(14 | (5<<8)) (SQLITE_CANTOPEN14 | (5<<8)) /* Not Used */
847#define SQLITE_CANTOPEN_SYMLINK(14 | (6<<8)) (SQLITE_CANTOPEN14 | (6<<8))
848#define SQLITE_CORRUPT_VTAB(11 | (1<<8)) (SQLITE_CORRUPT11 | (1<<8))
849#define SQLITE_CORRUPT_SEQUENCE(11 | (2<<8)) (SQLITE_CORRUPT11 | (2<<8))
850#define SQLITE_CORRUPT_INDEX(11 | (3<<8)) (SQLITE_CORRUPT11 | (3<<8))
851#define SQLITE_READONLY_RECOVERY(8 | (1<<8)) (SQLITE_READONLY8 | (1<<8))
852#define SQLITE_READONLY_CANTLOCK(8 | (2<<8)) (SQLITE_READONLY8 | (2<<8))
853#define SQLITE_READONLY_ROLLBACK(8 | (3<<8)) (SQLITE_READONLY8 | (3<<8))
854#define SQLITE_READONLY_DBMOVED(8 | (4<<8)) (SQLITE_READONLY8 | (4<<8))
855#define SQLITE_READONLY_CANTINIT(8 | (5<<8)) (SQLITE_READONLY8 | (5<<8))
856#define SQLITE_READONLY_DIRECTORY(8 | (6<<8)) (SQLITE_READONLY8 | (6<<8))
857#define SQLITE_ABORT_ROLLBACK(4 | (2<<8)) (SQLITE_ABORT4 | (2<<8))
858#define SQLITE_CONSTRAINT_CHECK(19 | (1<<8)) (SQLITE_CONSTRAINT19 | (1<<8))
859#define SQLITE_CONSTRAINT_COMMITHOOK(19 | (2<<8)) (SQLITE_CONSTRAINT19 | (2<<8))
860#define SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8)) (SQLITE_CONSTRAINT19 | (3<<8))
861#define SQLITE_CONSTRAINT_FUNCTION(19 | (4<<8)) (SQLITE_CONSTRAINT19 | (4<<8))
862#define SQLITE_CONSTRAINT_NOTNULL(19 | (5<<8)) (SQLITE_CONSTRAINT19 | (5<<8))
863#define SQLITE_CONSTRAINT_PRIMARYKEY(19 | (6<<8)) (SQLITE_CONSTRAINT19 | (6<<8))
864#define SQLITE_CONSTRAINT_TRIGGER(19 | (7<<8)) (SQLITE_CONSTRAINT19 | (7<<8))
865#define SQLITE_CONSTRAINT_UNIQUE(19 | (8<<8)) (SQLITE_CONSTRAINT19 | (8<<8))
866#define SQLITE_CONSTRAINT_VTAB(19 | (9<<8)) (SQLITE_CONSTRAINT19 | (9<<8))
867#define SQLITE_CONSTRAINT_ROWID(19 |(10<<8)) (SQLITE_CONSTRAINT19 |(10<<8))
868#define SQLITE_CONSTRAINT_PINNED(19 |(11<<8)) (SQLITE_CONSTRAINT19 |(11<<8))
869#define SQLITE_CONSTRAINT_DATATYPE(19 |(12<<8)) (SQLITE_CONSTRAINT19 |(12<<8))
870#define SQLITE_NOTICE_RECOVER_WAL(27 | (1<<8)) (SQLITE_NOTICE27 | (1<<8))
871#define SQLITE_NOTICE_RECOVER_ROLLBACK(27 | (2<<8)) (SQLITE_NOTICE27 | (2<<8))
872#define SQLITE_WARNING_AUTOINDEX(28 | (1<<8)) (SQLITE_WARNING28 | (1<<8))
873#define SQLITE_AUTH_USER(23 | (1<<8)) (SQLITE_AUTH23 | (1<<8))
874#define SQLITE_OK_LOAD_PERMANENTLY(0 | (1<<8)) (SQLITE_OK0 | (1<<8))
875#define SQLITE_OK_SYMLINK(0 | (2<<8)) (SQLITE_OK0 | (2<<8)) /* internal use only */
876
877/*
878** CAPI3REF: Flags For File Open Operations
879**
880** These bit values are intended for use in the
881** 3rd parameter to the [sqlite3_open_v2()] interface and
882** in the 4th parameter to the [sqlite3_vfs.xOpen] method.
883**
884** Only those flags marked as "Ok for sqlite3_open_v2()" may be
885** used as the third argument to the [sqlite3_open_v2()] interface.
886** The other flags have historically been ignored by sqlite3_open_v2(),
887** though future versions of SQLite might change so that an error is
888** raised if any of the disallowed bits are passed into sqlite3_open_v2().
889** Applications should not depend on the historical behavior.
890**
891** Note in particular that passing the SQLITE_OPEN_EXCLUSIVE flag into
892** [sqlite3_open_v2()] does *not* cause the underlying database file
893** to be opened using O_EXCL. Passing SQLITE_OPEN_EXCLUSIVE into
894** [sqlite3_open_v2()] has historically be a no-op and might become an
895** error in future versions of SQLite.
896*/
897#define SQLITE_OPEN_READONLY0x00000001 0x00000001 /* Ok for sqlite3_open_v2() */
898#define SQLITE_OPEN_READWRITE0x00000002 0x00000002 /* Ok for sqlite3_open_v2() */
899#define SQLITE_OPEN_CREATE0x00000004 0x00000004 /* Ok for sqlite3_open_v2() */
900#define SQLITE_OPEN_DELETEONCLOSE0x00000008 0x00000008 /* VFS only */
901#define SQLITE_OPEN_EXCLUSIVE0x00000010 0x00000010 /* VFS only */
902#define SQLITE_OPEN_AUTOPROXY0x00000020 0x00000020 /* VFS only */
903#define SQLITE_OPEN_URI0x00000040 0x00000040 /* Ok for sqlite3_open_v2() */
904#define SQLITE_OPEN_MEMORY0x00000080 0x00000080 /* Ok for sqlite3_open_v2() */
905#define SQLITE_OPEN_MAIN_DB0x00000100 0x00000100 /* VFS only */
906#define SQLITE_OPEN_TEMP_DB0x00000200 0x00000200 /* VFS only */
907#define SQLITE_OPEN_TRANSIENT_DB0x00000400 0x00000400 /* VFS only */
908#define SQLITE_OPEN_MAIN_JOURNAL0x00000800 0x00000800 /* VFS only */
909#define SQLITE_OPEN_TEMP_JOURNAL0x00001000 0x00001000 /* VFS only */
910#define SQLITE_OPEN_SUBJOURNAL0x00002000 0x00002000 /* VFS only */
911#define SQLITE_OPEN_SUPER_JOURNAL0x00004000 0x00004000 /* VFS only */
912#define SQLITE_OPEN_NOMUTEX0x00008000 0x00008000 /* Ok for sqlite3_open_v2() */
913#define SQLITE_OPEN_FULLMUTEX0x00010000 0x00010000 /* Ok for sqlite3_open_v2() */
914#define SQLITE_OPEN_SHAREDCACHE0x00020000 0x00020000 /* Ok for sqlite3_open_v2() */
915#define SQLITE_OPEN_PRIVATECACHE0x00040000 0x00040000 /* Ok for sqlite3_open_v2() */
916#define SQLITE_OPEN_WAL0x00080000 0x00080000 /* VFS only */
917#define SQLITE_OPEN_NOFOLLOW0x01000000 0x01000000 /* Ok for sqlite3_open_v2() */
918#define SQLITE_OPEN_EXRESCODE0x02000000 0x02000000 /* Extended result codes */
919
920/* Reserved: 0x00F00000 */
921/* Legacy compatibility: */
922#define SQLITE_OPEN_MASTER_JOURNAL0x00004000 0x00004000 /* VFS only */
923
924
925/*
926** CAPI3REF: Device Characteristics
927**
928** The xDeviceCharacteristics method of the [sqlite3_io_methods]
929** object returns an integer which is a vector of these
930** bit values expressing I/O characteristics of the mass storage
931** device that holds the file that the [sqlite3_io_methods]
932** refers to.
933**
934** The SQLITE_IOCAP_ATOMIC property means that all writes of
935** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
936** mean that writes of blocks that are nnn bytes in size and
937** are aligned to an address which is an integer multiple of
938** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
939** that when data is appended to a file, the data is appended
940** first then the size of the file is extended, never the other
941** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
942** information is written to disk in the same order as calls
943** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
944** after reboot following a crash or power loss, the only bytes in a
945** file that were written at the application level might have changed
946** and that adjacent bytes, even bytes within the same sector are
947** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
948** flag indicates that a file cannot be deleted when open. The
949** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
950** read-only media and cannot be changed even by processes with
951** elevated privileges.
952**
953** The SQLITE_IOCAP_BATCH_ATOMIC property means that the underlying
954** filesystem supports doing multiple write operations atomically when those
955** write operations are bracketed by [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] and
956** [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE].
957*/
958#define SQLITE_IOCAP_ATOMIC0x00000001 0x00000001
959#define SQLITE_IOCAP_ATOMIC5120x00000002 0x00000002
960#define SQLITE_IOCAP_ATOMIC1K0x00000004 0x00000004
961#define SQLITE_IOCAP_ATOMIC2K0x00000008 0x00000008
962#define SQLITE_IOCAP_ATOMIC4K0x00000010 0x00000010
963#define SQLITE_IOCAP_ATOMIC8K0x00000020 0x00000020
964#define SQLITE_IOCAP_ATOMIC16K0x00000040 0x00000040
965#define SQLITE_IOCAP_ATOMIC32K0x00000080 0x00000080
966#define SQLITE_IOCAP_ATOMIC64K0x00000100 0x00000100
967#define SQLITE_IOCAP_SAFE_APPEND0x00000200 0x00000200
968#define SQLITE_IOCAP_SEQUENTIAL0x00000400 0x00000400
969#define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN0x00000800 0x00000800
970#define SQLITE_IOCAP_POWERSAFE_OVERWRITE0x00001000 0x00001000
971#define SQLITE_IOCAP_IMMUTABLE0x00002000 0x00002000
972#define SQLITE_IOCAP_BATCH_ATOMIC0x00004000 0x00004000
973
974/*
975** CAPI3REF: File Locking Levels
976**
977** SQLite uses one of these integer values as the second
978** argument to calls it makes to the xLock() and xUnlock() methods
979** of an [sqlite3_io_methods] object.
980*/
981#define SQLITE_LOCK_NONE0 0
982#define SQLITE_LOCK_SHARED1 1
983#define SQLITE_LOCK_RESERVED2 2
984#define SQLITE_LOCK_PENDING3 3
985#define SQLITE_LOCK_EXCLUSIVE4 4
986
987/*
988** CAPI3REF: Synchronization Type Flags
989**
990** When SQLite invokes the xSync() method of an
991** [sqlite3_io_methods] object it uses a combination of
992** these integer values as the second argument.
993**
994** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
995** sync operation only needs to flush data to mass storage. Inode
996** information need not be flushed. If the lower four bits of the flag
997** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics.
998** If the lower four bits equal SQLITE_SYNC_FULL, that means
999** to use Mac OS X style fullsync instead of fsync().
1000**
1001** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags
1002** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL
1003** settings. The [synchronous pragma] determines when calls to the
1004** xSync VFS method occur and applies uniformly across all platforms.
1005** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how
1006** energetic or rigorous or forceful the sync operations are and
1007** only make a difference on Mac OSX for the default SQLite code.
1008** (Third-party VFS implementations might also make the distinction
1009** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the
1010** operating systems natively supported by SQLite, only Mac OSX
1011** cares about the difference.)
1012*/
1013#define SQLITE_SYNC_NORMAL0x00002 0x00002
1014#define SQLITE_SYNC_FULL0x00003 0x00003
1015#define SQLITE_SYNC_DATAONLY0x00010 0x00010
1016
1017/*
1018** CAPI3REF: OS Interface Open File Handle
1019**
1020** An [sqlite3_file] object represents an open file in the
1021** [sqlite3_vfs | OS interface layer]. Individual OS interface
1022** implementations will
1023** want to subclass this object by appending additional fields
1024** for their own use. The pMethods entry is a pointer to an
1025** [sqlite3_io_methods] object that defines methods for performing
1026** I/O operations on the open file.
1027*/
1028typedef struct sqlite3_file sqlite3_file;
1029struct sqlite3_file {
1030 const struct sqlite3_io_methods *pMethods; /* Methods for an open file */
1031};
1032
1033/*
1034** CAPI3REF: OS Interface File Virtual Methods Object
1035**
1036** Every file opened by the [sqlite3_vfs.xOpen] method populates an
1037** [sqlite3_file] object (or, more commonly, a subclass of the
1038** [sqlite3_file] object) with a pointer to an instance of this object.
1039** This object defines the methods used to perform various operations
1040** against the open file represented by the [sqlite3_file] object.
1041**
1042** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element
1043** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
1044** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed. The
1045** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen]
1046** is for the [sqlite3_vfs.xOpen] to set the sqlite3_file.pMethods element
1047** to NULL.
1048**
1049** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
1050** [SQLITE_SYNC_FULL]. The first choice is the normal fsync().
1051** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY]
1052** flag may be ORed in to indicate that only the data of the file
1053** and not its inode needs to be synced.
1054**
1055** The integer values to xLock() and xUnlock() are one of
1056** <ul>
1057** <li> [SQLITE_LOCK_NONE],
1058** <li> [SQLITE_LOCK_SHARED],
1059** <li> [SQLITE_LOCK_RESERVED],
1060** <li> [SQLITE_LOCK_PENDING], or
1061** <li> [SQLITE_LOCK_EXCLUSIVE].
1062** </ul>
1063** xLock() increases the lock. xUnlock() decreases the lock.
1064** The xCheckReservedLock() method checks whether any database connection,
1065** either in this process or in some other process, is holding a RESERVED,
1066** PENDING, or EXCLUSIVE lock on the file. It returns true
1067** if such a lock exists and false otherwise.
1068**
1069** The xFileControl() method is a generic interface that allows custom
1070** VFS implementations to directly control an open file using the
1071** [sqlite3_file_control()] interface. The second "op" argument is an
1072** integer opcode. The third argument is a generic pointer intended to
1073** point to a structure that may contain arguments or space in which to
1074** write return values. Potential uses for xFileControl() might be
1075** functions to enable blocking locks with timeouts, to change the
1076** locking strategy (for example to use dot-file locks), to inquire
1077** about the status of a lock, or to break stale locks. The SQLite
1078** core reserves all opcodes less than 100 for its own use.
1079** A [file control opcodes | list of opcodes] less than 100 is available.
1080** Applications that define a custom xFileControl method should use opcodes
1081** greater than 100 to avoid conflicts. VFS implementations should
1082** return [SQLITE_NOTFOUND] for file control opcodes that they do not
1083** recognize.
1084**
1085** The xSectorSize() method returns the sector size of the
1086** device that underlies the file. The sector size is the
1087** minimum write that can be performed without disturbing
1088** other bytes in the file. The xDeviceCharacteristics()
1089** method returns a bit vector describing behaviors of the
1090** underlying device:
1091**
1092** <ul>
1093** <li> [SQLITE_IOCAP_ATOMIC]
1094** <li> [SQLITE_IOCAP_ATOMIC512]
1095** <li> [SQLITE_IOCAP_ATOMIC1K]
1096** <li> [SQLITE_IOCAP_ATOMIC2K]
1097** <li> [SQLITE_IOCAP_ATOMIC4K]
1098** <li> [SQLITE_IOCAP_ATOMIC8K]
1099** <li> [SQLITE_IOCAP_ATOMIC16K]
1100** <li> [SQLITE_IOCAP_ATOMIC32K]
1101** <li> [SQLITE_IOCAP_ATOMIC64K]
1102** <li> [SQLITE_IOCAP_SAFE_APPEND]
1103** <li> [SQLITE_IOCAP_SEQUENTIAL]
1104** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
1105** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
1106** <li> [SQLITE_IOCAP_IMMUTABLE]
1107** <li> [SQLITE_IOCAP_BATCH_ATOMIC]
1108** </ul>
1109**
1110** The SQLITE_IOCAP_ATOMIC property means that all writes of
1111** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
1112** mean that writes of blocks that are nnn bytes in size and
1113** are aligned to an address which is an integer multiple of
1114** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
1115** that when data is appended to a file, the data is appended
1116** first then the size of the file is extended, never the other
1117** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
1118** information is written to disk in the same order as calls
1119** to xWrite().
1120**
1121** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
1122** in the unread portions of the buffer with zeros. A VFS that
1123** fails to zero-fill short reads might seem to work. However,
1124** failure to zero-fill short reads will eventually lead to
1125** database corruption.
1126*/
1127typedef struct sqlite3_io_methods sqlite3_io_methods;
1128struct sqlite3_io_methods {
1129 int iVersion;
1130 int (*xClose)(sqlite3_file*);
1131 int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
1132 int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
1133 int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
1134 int (*xSync)(sqlite3_file*, int flags);
1135 int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
1136 int (*xLock)(sqlite3_file*, int);
1137 int (*xUnlock)(sqlite3_file*, int);
1138 int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
1139 int (*xFileControl)(sqlite3_file*, int op, void *pArg);
1140 int (*xSectorSize)(sqlite3_file*);
1141 int (*xDeviceCharacteristics)(sqlite3_file*);
1142 /* Methods above are valid for version 1 */
1143 int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
1144 int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
1145 void (*xShmBarrier)(sqlite3_file*);
1146 int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
1147 /* Methods above are valid for version 2 */
1148 int (*xFetch)(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
1149 int (*xUnfetch)(sqlite3_file*, sqlite3_int64 iOfst, void *p);
1150 /* Methods above are valid for version 3 */
1151 /* Additional methods may be added in future releases */
1152};
1153
1154/*
1155** CAPI3REF: Standard File Control Opcodes
1156** KEYWORDS: {file control opcodes} {file control opcode}
1157**
1158** These integer constants are opcodes for the xFileControl method
1159** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
1160** interface.
1161**
1162** <ul>
1163** <li>[[SQLITE_FCNTL_LOCKSTATE]]
1164** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This
1165** opcode causes the xFileControl method to write the current state of
1166** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
1167** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
1168** into an integer that the pArg argument points to. This capability
1169** is used during testing and is only available when the SQLITE_TEST
1170** compile-time option is used.
1171**
1172** <li>[[SQLITE_FCNTL_SIZE_HINT]]
1173** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
1174** layer a hint of how large the database file will grow to be during the
1175** current transaction. This hint is not guaranteed to be accurate but it
1176** is often close. The underlying VFS might choose to preallocate database
1177** file space based on this hint in order to help writes to the database
1178** file run faster.
1179**
1180** <li>[[SQLITE_FCNTL_SIZE_LIMIT]]
1181** The [SQLITE_FCNTL_SIZE_LIMIT] opcode is used by in-memory VFS that
1182** implements [sqlite3_deserialize()] to set an upper bound on the size
1183** of the in-memory database. The argument is a pointer to a [sqlite3_int64].
1184** If the integer pointed to is negative, then it is filled in with the
1185** current limit. Otherwise the limit is set to the larger of the value
1186** of the integer pointed to and the current database size. The integer
1187** pointed to is set to the new limit.
1188**
1189** <li>[[SQLITE_FCNTL_CHUNK_SIZE]]
1190** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
1191** extends and truncates the database file in chunks of a size specified
1192** by the user. The fourth argument to [sqlite3_file_control()] should
1193** point to an integer (type int) containing the new chunk-size to use
1194** for the nominated database. Allocating database file space in large
1195** chunks (say 1MB at a time), may reduce file-system fragmentation and
1196** improve performance on some systems.
1197**
1198** <li>[[SQLITE_FCNTL_FILE_POINTER]]
1199** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
1200** to the [sqlite3_file] object associated with a particular database
1201** connection. See also [SQLITE_FCNTL_JOURNAL_POINTER].
1202**
1203** <li>[[SQLITE_FCNTL_JOURNAL_POINTER]]
1204** The [SQLITE_FCNTL_JOURNAL_POINTER] opcode is used to obtain a pointer
1205** to the [sqlite3_file] object associated with the journal file (either
1206** the [rollback journal] or the [write-ahead log]) for a particular database
1207** connection. See also [SQLITE_FCNTL_FILE_POINTER].
1208**
1209** <li>[[SQLITE_FCNTL_SYNC_OMITTED]]
1210** No longer in use.
1211**
1212** <li>[[SQLITE_FCNTL_SYNC]]
1213** The [SQLITE_FCNTL_SYNC] opcode is generated internally by SQLite and
1214** sent to the VFS immediately before the xSync method is invoked on a
1215** database file descriptor. Or, if the xSync method is not invoked
1216** because the user has configured SQLite with
1217** [PRAGMA synchronous | PRAGMA synchronous=OFF] it is invoked in place
1218** of the xSync method. In most cases, the pointer argument passed with
1219** this file-control is NULL. However, if the database file is being synced
1220** as part of a multi-database commit, the argument points to a nul-terminated
1221** string containing the transactions super-journal file name. VFSes that
1222** do not need this signal should silently ignore this opcode. Applications
1223** should not call [sqlite3_file_control()] with this opcode as doing so may
1224** disrupt the operation of the specialized VFSes that do require it.
1225**
1226** <li>[[SQLITE_FCNTL_COMMIT_PHASETWO]]
1227** The [SQLITE_FCNTL_COMMIT_PHASETWO] opcode is generated internally by SQLite
1228** and sent to the VFS after a transaction has been committed immediately
1229** but before the database is unlocked. VFSes that do not need this signal
1230** should silently ignore this opcode. Applications should not call
1231** [sqlite3_file_control()] with this opcode as doing so may disrupt the
1232** operation of the specialized VFSes that do require it.
1233**
1234** <li>[[SQLITE_FCNTL_WIN32_AV_RETRY]]
1235** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
1236** retry counts and intervals for certain disk I/O operations for the
1237** windows [VFS] in order to provide robustness in the presence of
1238** anti-virus programs. By default, the windows VFS will retry file read,
1239** file write, and file delete operations up to 10 times, with a delay
1240** of 25 milliseconds before the first retry and with the delay increasing
1241** by an additional 25 milliseconds with each subsequent retry. This
1242** opcode allows these two values (10 retries and 25 milliseconds of delay)
1243** to be adjusted. The values are changed for all database connections
1244** within the same process. The argument is a pointer to an array of two
1245** integers where the first integer is the new retry count and the second
1246** integer is the delay. If either integer is negative, then the setting
1247** is not changed but instead the prior value of that setting is written
1248** into the array entry, allowing the current retry settings to be
1249** interrogated. The zDbName parameter is ignored.
1250**
1251** <li>[[SQLITE_FCNTL_PERSIST_WAL]]
1252** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
1253** persistent [WAL | Write Ahead Log] setting. By default, the auxiliary
1254** write ahead log ([WAL file]) and shared memory
1255** files used for transaction control
1256** are automatically deleted when the latest connection to the database
1257** closes. Setting persistent WAL mode causes those files to persist after
1258** close. Persisting the files is useful when other processes that do not
1259** have write permission on the directory containing the database file want
1260** to read the database file, as the WAL and shared memory files must exist
1261** in order for the database to be readable. The fourth parameter to
1262** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1263** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
1264** WAL mode. If the integer is -1, then it is overwritten with the current
1265** WAL persistence setting.
1266**
1267** <li>[[SQLITE_FCNTL_POWERSAFE_OVERWRITE]]
1268** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the
1269** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting
1270** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the
1271** xDeviceCharacteristics methods. The fourth parameter to
1272** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1273** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
1274** mode. If the integer is -1, then it is overwritten with the current
1275** zero-damage mode setting.
1276**
1277** <li>[[SQLITE_FCNTL_OVERWRITE]]
1278** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
1279** a write transaction to indicate that, unless it is rolled back for some
1280** reason, the entire database file will be overwritten by the current
1281** transaction. This is used by VACUUM operations.
1282**
1283** <li>[[SQLITE_FCNTL_VFSNAME]]
1284** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of
1285** all [VFSes] in the VFS stack. The names are of all VFS shims and the
1286** final bottom-level VFS are written into memory obtained from
1287** [sqlite3_malloc()] and the result is stored in the char* variable
1288** that the fourth parameter of [sqlite3_file_control()] points to.
1289** The caller is responsible for freeing the memory when done. As with
1290** all file-control actions, there is no guarantee that this will actually
1291** do anything. Callers should initialize the char* variable to a NULL
1292** pointer in case this file-control is not implemented. This file-control
1293** is intended for diagnostic use only.
1294**
1295** <li>[[SQLITE_FCNTL_VFS_POINTER]]
1296** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level
1297** [VFSes] currently in use. ^(The argument X in
1298** sqlite3_file_control(db,SQLITE_FCNTL_VFS_POINTER,X) must be
1299** of type "[sqlite3_vfs] **". This opcodes will set *X
1300** to a pointer to the top-level VFS.)^
1301** ^When there are multiple VFS shims in the stack, this opcode finds the
1302** upper-most shim only.
1303**
1304** <li>[[SQLITE_FCNTL_PRAGMA]]
1305** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA]
1306** file control is sent to the open [sqlite3_file] object corresponding
1307** to the database file to which the pragma statement refers. ^The argument
1308** to the [SQLITE_FCNTL_PRAGMA] file control is an array of
1309** pointers to strings (char**) in which the second element of the array
1310** is the name of the pragma and the third element is the argument to the
1311** pragma or NULL if the pragma has no argument. ^The handler for an
1312** [SQLITE_FCNTL_PRAGMA] file control can optionally make the first element
1313** of the char** argument point to a string obtained from [sqlite3_mprintf()]
1314** or the equivalent and that string will become the result of the pragma or
1315** the error message if the pragma fails. ^If the
1316** [SQLITE_FCNTL_PRAGMA] file control returns [SQLITE_NOTFOUND], then normal
1317** [PRAGMA] processing continues. ^If the [SQLITE_FCNTL_PRAGMA]
1318** file control returns [SQLITE_OK], then the parser assumes that the
1319** VFS has handled the PRAGMA itself and the parser generates a no-op
1320** prepared statement if result string is NULL, or that returns a copy
1321** of the result string if the string is non-NULL.
1322** ^If the [SQLITE_FCNTL_PRAGMA] file control returns
1323** any result code other than [SQLITE_OK] or [SQLITE_NOTFOUND], that means
1324** that the VFS encountered an error while handling the [PRAGMA] and the
1325** compilation of the PRAGMA fails with an error. ^The [SQLITE_FCNTL_PRAGMA]
1326** file control occurs at the beginning of pragma statement analysis and so
1327** it is able to override built-in [PRAGMA] statements.
1328**
1329** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
1330** ^The [SQLITE_FCNTL_BUSYHANDLER]
1331** file-control may be invoked by SQLite on the database file handle
1332** shortly after it is opened in order to provide a custom VFS with access
1333** to the connection's busy-handler callback. The argument is of type (void**)
1334** - an array of two (void *) values. The first (void *) actually points
1335** to a function of type (int (*)(void *)). In order to invoke the connection's
1336** busy-handler, this function should be invoked with the second (void *) in
1337** the array as the only argument. If it returns non-zero, then the operation
1338** should be retried. If it returns zero, the custom VFS should abandon the
1339** current operation.
1340**
1341** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
1342** ^Applications can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
1343** to have SQLite generate a
1344** temporary filename using the same algorithm that is followed to generate
1345** temporary filenames for TEMP tables and other internal uses. The
1346** argument should be a char** which will be filled with the filename
1347** written into memory obtained from [sqlite3_malloc()]. The caller should
1348** invoke [sqlite3_free()] on the result to avoid a memory leak.
1349**
1350** <li>[[SQLITE_FCNTL_MMAP_SIZE]]
1351** The [SQLITE_FCNTL_MMAP_SIZE] file control is used to query or set the
1352** maximum number of bytes that will be used for memory-mapped I/O.
1353** The argument is a pointer to a value of type sqlite3_int64 that
1354** is an advisory maximum number of bytes in the file to memory map. The
1355** pointer is overwritten with the old value. The limit is not changed if
1356** the value originally pointed to is negative, and so the current limit
1357** can be queried by passing in a pointer to a negative number. This
1358** file-control is used internally to implement [PRAGMA mmap_size].
1359**
1360** <li>[[SQLITE_FCNTL_TRACE]]
1361** The [SQLITE_FCNTL_TRACE] file control provides advisory information
1362** to the VFS about what the higher layers of the SQLite stack are doing.
1363** This file control is used by some VFS activity tracing [shims].
1364** The argument is a zero-terminated string. Higher layers in the
1365** SQLite stack may generate instances of this file control if
1366** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
1367**
1368** <li>[[SQLITE_FCNTL_HAS_MOVED]]
1369** The [SQLITE_FCNTL_HAS_MOVED] file control interprets its argument as a
1370** pointer to an integer and it writes a boolean into that integer depending
1371** on whether or not the file has been renamed, moved, or deleted since it
1372** was first opened.
1373**
1374** <li>[[SQLITE_FCNTL_WIN32_GET_HANDLE]]
1375** The [SQLITE_FCNTL_WIN32_GET_HANDLE] opcode can be used to obtain the
1376** underlying native file handle associated with a file handle. This file
1377** control interprets its argument as a pointer to a native file handle and
1378** writes the resulting value there.
1379**
1380** <li>[[SQLITE_FCNTL_WIN32_SET_HANDLE]]
1381** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
1382** opcode causes the xFileControl method to swap the file handle with the one
1383** pointed to by the pArg argument. This capability is used during testing
1384** and only needs to be supported when SQLITE_TEST is defined.
1385**
1386** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
1387** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
1388** be advantageous to block on the next WAL lock if the lock is not immediately
1389** available. The WAL subsystem issues this signal during rare
1390** circumstances in order to fix a problem with priority inversion.
1391** Applications should <em>not</em> use this file-control.
1392**
1393** <li>[[SQLITE_FCNTL_ZIPVFS]]
1394** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
1395** VFS should return SQLITE_NOTFOUND for this opcode.
1396**
1397** <li>[[SQLITE_FCNTL_RBU]]
1398** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by
1399** the RBU extension only. All other VFS should return SQLITE_NOTFOUND for
1400** this opcode.
1401**
1402** <li>[[SQLITE_FCNTL_BEGIN_ATOMIC_WRITE]]
1403** If the [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] opcode returns SQLITE_OK, then
1404** the file descriptor is placed in "batch write mode", which
1405** means all subsequent write operations will be deferred and done
1406** atomically at the next [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]. Systems
1407** that do not support batch atomic writes will return SQLITE_NOTFOUND.
1408** ^Following a successful SQLITE_FCNTL_BEGIN_ATOMIC_WRITE and prior to
1409** the closing [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] or
1410** [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE], SQLite will make
1411** no VFS interface calls on the same [sqlite3_file] file descriptor
1412** except for calls to the xWrite method and the xFileControl method
1413** with [SQLITE_FCNTL_SIZE_HINT].
1414**
1415** <li>[[SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]]
1416** The [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] opcode causes all write
1417** operations since the previous successful call to
1418** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be performed atomically.
1419** This file control returns [SQLITE_OK] if and only if the writes were
1420** all performed successfully and have been committed to persistent storage.
1421** ^Regardless of whether or not it is successful, this file control takes
1422** the file descriptor out of batch write mode so that all subsequent
1423** write operations are independent.
1424** ^SQLite will never invoke SQLITE_FCNTL_COMMIT_ATOMIC_WRITE without
1425** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
1426**
1427** <li>[[SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE]]
1428** The [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE] opcode causes all write
1429** operations since the previous successful call to
1430** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
1431** ^This file control takes the file descriptor out of batch write mode
1432** so that all subsequent write operations are independent.
1433** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
1434** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
1435**
1436** <li>[[SQLITE_FCNTL_LOCK_TIMEOUT]]
1437** The [SQLITE_FCNTL_LOCK_TIMEOUT] opcode is used to configure a VFS
1438** to block for up to M milliseconds before failing when attempting to
1439** obtain a file lock using the xLock or xShmLock methods of the VFS.
1440** The parameter is a pointer to a 32-bit signed integer that contains
1441** the value that M is to be set to. Before returning, the 32-bit signed
1442** integer is overwritten with the previous value of M.
1443**
1444** <li>[[SQLITE_FCNTL_DATA_VERSION]]
1445** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to
1446** a database file. The argument is a pointer to a 32-bit unsigned integer.
1447** The "data version" for the pager is written into the pointer. The
1448** "data version" changes whenever any change occurs to the corresponding
1449** database file, either through SQL statements on the same database
1450** connection or through transactions committed by separate database
1451** connections possibly in other processes. The [sqlite3_total_changes()]
1452** interface can be used to find if any database on the connection has changed,
1453** but that interface responds to changes on TEMP as well as MAIN and does
1454** not provide a mechanism to detect changes to MAIN only. Also, the
1455** [sqlite3_total_changes()] interface responds to internal changes only and
1456** omits changes made by other database connections. The
1457** [PRAGMA data_version] command provides a mechanism to detect changes to
1458** a single attached database that occur due to other database connections,
1459** but omits changes implemented by the database connection on which it is
1460** called. This file control is the only mechanism to detect changes that
1461** happen either internally or externally and that are associated with
1462** a particular attached database.
1463**
1464** <li>[[SQLITE_FCNTL_CKPT_START]]
1465** The [SQLITE_FCNTL_CKPT_START] opcode is invoked from within a checkpoint
1466** in wal mode before the client starts to copy pages from the wal
1467** file to the database file.
1468**
1469** <li>[[SQLITE_FCNTL_CKPT_DONE]]
1470** The [SQLITE_FCNTL_CKPT_DONE] opcode is invoked from within a checkpoint
1471** in wal mode after the client has finished copying pages from the wal
1472** file to the database file, but before the *-shm file is updated to
1473** record the fact that the pages have been checkpointed.
1474** </ul>
1475**
1476** <li>[[SQLITE_FCNTL_EXTERNAL_READER]]
1477** The EXPERIMENTAL [SQLITE_FCNTL_EXTERNAL_READER] opcode is used to detect
1478** whether or not there is a database client in another process with a wal-mode
1479** transaction open on the database or not. It is only available on unix.The
1480** (void*) argument passed with this file-control should be a pointer to a
1481** value of type (int). The integer value is set to 1 if the database is a wal
1482** mode database and there exists at least one client in another process that
1483** currently has an SQL transaction open on the database. It is set to 0 if
1484** the database is not a wal-mode db, or if there is no such connection in any
1485** other process. This opcode cannot be used to detect transactions opened
1486** by clients within the current process, only within other processes.
1487** </ul>
1488**
1489** <li>[[SQLITE_FCNTL_CKSM_FILE]]
1490** Used by the cksmvfs VFS module only.
1491** </ul>
1492*/
1493#define SQLITE_FCNTL_LOCKSTATE1 1
1494#define SQLITE_FCNTL_GET_LOCKPROXYFILE2 2
1495#define SQLITE_FCNTL_SET_LOCKPROXYFILE3 3
1496#define SQLITE_FCNTL_LAST_ERRNO4 4
1497#define SQLITE_FCNTL_SIZE_HINT5 5
1498#define SQLITE_FCNTL_CHUNK_SIZE6 6
1499#define SQLITE_FCNTL_FILE_POINTER7 7
1500#define SQLITE_FCNTL_SYNC_OMITTED8 8
1501#define SQLITE_FCNTL_WIN32_AV_RETRY9 9
1502#define SQLITE_FCNTL_PERSIST_WAL10 10
1503#define SQLITE_FCNTL_OVERWRITE11 11
1504#define SQLITE_FCNTL_VFSNAME12 12
1505#define SQLITE_FCNTL_POWERSAFE_OVERWRITE13 13
1506#define SQLITE_FCNTL_PRAGMA14 14
1507#define SQLITE_FCNTL_BUSYHANDLER15 15
1508#define SQLITE_FCNTL_TEMPFILENAME16 16
1509#define SQLITE_FCNTL_MMAP_SIZE18 18
1510#define SQLITE_FCNTL_TRACE19 19
1511#define SQLITE_FCNTL_HAS_MOVED20 20
1512#define SQLITE_FCNTL_SYNC21 21
1513#define SQLITE_FCNTL_COMMIT_PHASETWO22 22
1514#define SQLITE_FCNTL_WIN32_SET_HANDLE23 23
1515#define SQLITE_FCNTL_WAL_BLOCK24 24
1516#define SQLITE_FCNTL_ZIPVFS25 25
1517#define SQLITE_FCNTL_RBU26 26
1518#define SQLITE_FCNTL_VFS_POINTER27 27
1519#define SQLITE_FCNTL_JOURNAL_POINTER28 28
1520#define SQLITE_FCNTL_WIN32_GET_HANDLE29 29
1521#define SQLITE_FCNTL_PDB30 30
1522#define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE31 31
1523#define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE32 32
1524#define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE33 33
1525#define SQLITE_FCNTL_LOCK_TIMEOUT34 34
1526#define SQLITE_FCNTL_DATA_VERSION35 35
1527#define SQLITE_FCNTL_SIZE_LIMIT36 36
1528#define SQLITE_FCNTL_CKPT_DONE37 37
1529#define SQLITE_FCNTL_RESERVE_BYTES38 38
1530#define SQLITE_FCNTL_CKPT_START39 39
1531#define SQLITE_FCNTL_EXTERNAL_READER40 40
1532#define SQLITE_FCNTL_CKSM_FILE41 41
1533
1534/* deprecated names */
1535#define SQLITE_GET_LOCKPROXYFILE2 SQLITE_FCNTL_GET_LOCKPROXYFILE2
1536#define SQLITE_SET_LOCKPROXYFILE3 SQLITE_FCNTL_SET_LOCKPROXYFILE3
1537#define SQLITE_LAST_ERRNO4 SQLITE_FCNTL_LAST_ERRNO4
1538
1539
1540/*
1541** CAPI3REF: Mutex Handle
1542**
1543** The mutex module within SQLite defines [sqlite3_mutex] to be an
1544** abstract type for a mutex object. The SQLite core never looks
1545** at the internal representation of an [sqlite3_mutex]. It only
1546** deals with pointers to the [sqlite3_mutex] object.
1547**
1548** Mutexes are created using [sqlite3_mutex_alloc()].
1549*/
1550typedef struct sqlite3_mutex sqlite3_mutex;
1551
1552/*
1553** CAPI3REF: Loadable Extension Thunk
1554**
1555** A pointer to the opaque sqlite3_api_routines structure is passed as
1556** the third parameter to entry points of [loadable extensions]. This
1557** structure must be typedefed in order to work around compiler warnings
1558** on some platforms.
1559*/
1560typedef struct sqlite3_api_routines sqlite3_api_routines;
1561
1562/*
1563** CAPI3REF: OS Interface Object
1564**
1565** An instance of the sqlite3_vfs object defines the interface between
1566** the SQLite core and the underlying operating system. The "vfs"
1567** in the name of the object stands for "virtual file system". See
1568** the [VFS | VFS documentation] for further information.
1569**
1570** The VFS interface is sometimes extended by adding new methods onto
1571** the end. Each time such an extension occurs, the iVersion field
1572** is incremented. The iVersion value started out as 1 in
1573** SQLite [version 3.5.0] on [dateof:3.5.0], then increased to 2
1574** with SQLite [version 3.7.0] on [dateof:3.7.0], and then increased
1575** to 3 with SQLite [version 3.7.6] on [dateof:3.7.6]. Additional fields
1576** may be appended to the sqlite3_vfs object and the iVersion value
1577** may increase again in future versions of SQLite.
1578** Note that due to an oversight, the structure
1579** of the sqlite3_vfs object changed in the transition from
1580** SQLite [version 3.5.9] to [version 3.6.0] on [dateof:3.6.0]
1581** and yet the iVersion field was not increased.
1582**
1583** The szOsFile field is the size of the subclassed [sqlite3_file]
1584** structure used by this VFS. mxPathname is the maximum length of
1585** a pathname in this VFS.
1586**
1587** Registered sqlite3_vfs objects are kept on a linked list formed by
1588** the pNext pointer. The [sqlite3_vfs_register()]
1589** and [sqlite3_vfs_unregister()] interfaces manage this list
1590** in a thread-safe way. The [sqlite3_vfs_find()] interface
1591** searches the list. Neither the application code nor the VFS
1592** implementation should use the pNext pointer.
1593**
1594** The pNext field is the only field in the sqlite3_vfs
1595** structure that SQLite will ever modify. SQLite will only access
1596** or modify this field while holding a particular static mutex.
1597** The application should never modify anything within the sqlite3_vfs
1598** object once the object has been registered.
1599**
1600** The zName field holds the name of the VFS module. The name must
1601** be unique across all VFS modules.
1602**
1603** [[sqlite3_vfs.xOpen]]
1604** ^SQLite guarantees that the zFilename parameter to xOpen
1605** is either a NULL pointer or string obtained
1606** from xFullPathname() with an optional suffix added.
1607** ^If a suffix is added to the zFilename parameter, it will
1608** consist of a single "-" character followed by no more than
1609** 11 alphanumeric and/or "-" characters.
1610** ^SQLite further guarantees that
1611** the string will be valid and unchanged until xClose() is
1612** called. Because of the previous sentence,
1613** the [sqlite3_file] can safely store a pointer to the
1614** filename if it needs to remember the filename for some reason.
1615** If the zFilename parameter to xOpen is a NULL pointer then xOpen
1616** must invent its own temporary name for the file. ^Whenever the
1617** xFilename parameter is NULL it will also be the case that the
1618** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
1619**
1620** The flags argument to xOpen() includes all bits set in
1621** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()]
1622** or [sqlite3_open16()] is used, then flags includes at least
1623** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE].
1624** If xOpen() opens a file read-only then it sets *pOutFlags to
1625** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be set.
1626**
1627** ^(SQLite will also add one of the following flags to the xOpen()
1628** call, depending on the object being opened:
1629**
1630** <ul>
1631** <li> [SQLITE_OPEN_MAIN_DB]
1632** <li> [SQLITE_OPEN_MAIN_JOURNAL]
1633** <li> [SQLITE_OPEN_TEMP_DB]
1634** <li> [SQLITE_OPEN_TEMP_JOURNAL]
1635** <li> [SQLITE_OPEN_TRANSIENT_DB]
1636** <li> [SQLITE_OPEN_SUBJOURNAL]
1637** <li> [SQLITE_OPEN_SUPER_JOURNAL]
1638** <li> [SQLITE_OPEN_WAL]
1639** </ul>)^
1640**
1641** The file I/O implementation can use the object type flags to
1642** change the way it deals with files. For example, an application
1643** that does not care about crash recovery or rollback might make
1644** the open of a journal file a no-op. Writes to this journal would
1645** also be no-ops, and any attempt to read the journal would return
1646** SQLITE_IOERR. Or the implementation might recognize that a database
1647** file will be doing page-aligned sector reads and writes in a random
1648** order and set up its I/O subsystem accordingly.
1649**
1650** SQLite might also add one of the following flags to the xOpen method:
1651**
1652** <ul>
1653** <li> [SQLITE_OPEN_DELETEONCLOSE]
1654** <li> [SQLITE_OPEN_EXCLUSIVE]
1655** </ul>
1656**
1657** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
1658** deleted when it is closed. ^The [SQLITE_OPEN_DELETEONCLOSE]
1659** will be set for TEMP databases and their journals, transient
1660** databases, and subjournals.
1661**
1662** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
1663** with the [SQLITE_OPEN_CREATE] flag, which are both directly
1664** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
1665** API. The SQLITE_OPEN_EXCLUSIVE flag, when paired with the
1666** SQLITE_OPEN_CREATE, is used to indicate that file should always
1667** be created, and that it is an error if it already exists.
1668** It is <i>not</i> used to indicate the file should be opened
1669** for exclusive access.
1670**
1671** ^At least szOsFile bytes of memory are allocated by SQLite
1672** to hold the [sqlite3_file] structure passed as the third
1673** argument to xOpen. The xOpen method does not have to
1674** allocate the structure; it should just fill it in. Note that
1675** the xOpen method must set the sqlite3_file.pMethods to either
1676** a valid [sqlite3_io_methods] object or to NULL. xOpen must do
1677** this even if the open fails. SQLite expects that the sqlite3_file.pMethods
1678** element will be valid after xOpen returns regardless of the success
1679** or failure of the xOpen call.
1680**
1681** [[sqlite3_vfs.xAccess]]
1682** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
1683** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
1684** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
1685** to test whether a file is at least readable. The SQLITE_ACCESS_READ
1686** flag is never actually used and is not implemented in the built-in
1687** VFSes of SQLite. The file is named by the second argument and can be a
1688** directory. The xAccess method returns [SQLITE_OK] on success or some
1689** non-zero error code if there is an I/O error or if the name of
1690** the file given in the second argument is illegal. If SQLITE_OK
1691** is returned, then non-zero or zero is written into *pResOut to indicate
1692** whether or not the file is accessible.
1693**
1694** ^SQLite will always allocate at least mxPathname+1 bytes for the
1695** output buffer xFullPathname. The exact size of the output buffer
1696** is also passed as a parameter to both methods. If the output buffer
1697** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is
1698** handled as a fatal error by SQLite, vfs implementations should endeavor
1699** to prevent this by setting mxPathname to a sufficiently large value.
1700**
1701** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64()
1702** interfaces are not strictly a part of the filesystem, but they are
1703** included in the VFS structure for completeness.
1704** The xRandomness() function attempts to return nBytes bytes
1705** of good-quality randomness into zOut. The return value is
1706** the actual number of bytes of randomness obtained.
1707** The xSleep() method causes the calling thread to sleep for at
1708** least the number of microseconds given. ^The xCurrentTime()
1709** method returns a Julian Day Number for the current date and time as
1710** a floating point value.
1711** ^The xCurrentTimeInt64() method returns, as an integer, the Julian
1712** Day Number multiplied by 86400000 (the number of milliseconds in
1713** a 24-hour day).
1714** ^SQLite will use the xCurrentTimeInt64() method to get the current
1715** date and time if that method is available (if iVersion is 2 or
1716** greater and the function pointer is not NULL) and will fall back
1717** to xCurrentTime() if xCurrentTimeInt64() is unavailable.
1718**
1719** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces
1720** are not used by the SQLite core. These optional interfaces are provided
1721** by some VFSes to facilitate testing of the VFS code. By overriding
1722** system calls with functions under its control, a test program can
1723** simulate faults and error conditions that would otherwise be difficult
1724** or impossible to induce. The set of system calls that can be overridden
1725** varies from one VFS to another, and from one version of the same VFS to the
1726** next. Applications that use these interfaces must be prepared for any
1727** or all of these interfaces to be NULL or for their behavior to change
1728** from one release to the next. Applications must not attempt to access
1729** any of these methods if the iVersion of the VFS is less than 3.
1730*/
1731typedef struct sqlite3_vfs sqlite3_vfs;
1732typedef void (*sqlite3_syscall_ptr)(void);
1733struct sqlite3_vfs {
1734 int iVersion; /* Structure version number (currently 3) */
1735 int szOsFile; /* Size of subclassed sqlite3_file */
1736 int mxPathname; /* Maximum file pathname length */
1737 sqlite3_vfs *pNext; /* Next registered VFS */
1738 const char *zName; /* Name of this virtual file system */
1739 void *pAppData; /* Pointer to application-specific data */
1740 int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
1741 int flags, int *pOutFlags);
1742 int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
1743 int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
1744 int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
1745 void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
1746 void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
1747 void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
1748 void (*xDlClose)(sqlite3_vfs*, void*);
1749 int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
1750 int (*xSleep)(sqlite3_vfs*, int microseconds);
1751 int (*xCurrentTime)(sqlite3_vfs*, double*);
1752 int (*xGetLastError)(sqlite3_vfs*, int, char *);
1753 /*
1754 ** The methods above are in version 1 of the sqlite_vfs object
1755 ** definition. Those that follow are added in version 2 or later
1756 */
1757 int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
1758 /*
1759 ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
1760 ** Those below are for version 3 and greater.
1761 */
1762 int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
1763 sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
1764 const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
1765 /*
1766 ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
1767 ** New fields may be appended in future versions. The iVersion
1768 ** value will increment whenever this happens.
1769 */
1770};
1771
1772/*
1773** CAPI3REF: Flags for the xAccess VFS method
1774**
1775** These integer constants can be used as the third parameter to
1776** the xAccess method of an [sqlite3_vfs] object. They determine
1777** what kind of permissions the xAccess method is looking for.
1778** With SQLITE_ACCESS_EXISTS, the xAccess method
1779** simply checks whether the file exists.
1780** With SQLITE_ACCESS_READWRITE, the xAccess method
1781** checks whether the named directory is both readable and writable
1782** (in other words, if files can be added, removed, and renamed within
1783** the directory).
1784** The SQLITE_ACCESS_READWRITE constant is currently used only by the
1785** [temp_store_directory pragma], though this could change in a future
1786** release of SQLite.
1787** With SQLITE_ACCESS_READ, the xAccess method
1788** checks whether the file is readable. The SQLITE_ACCESS_READ constant is
1789** currently unused, though it might be used in a future release of
1790** SQLite.
1791*/
1792#define SQLITE_ACCESS_EXISTS0 0
1793#define SQLITE_ACCESS_READWRITE1 1 /* Used by PRAGMA temp_store_directory */
1794#define SQLITE_ACCESS_READ2 2 /* Unused */
1795
1796/*
1797** CAPI3REF: Flags for the xShmLock VFS method
1798**
1799** These integer constants define the various locking operations
1800** allowed by the xShmLock method of [sqlite3_io_methods]. The
1801** following are the only legal combinations of flags to the
1802** xShmLock method:
1803**
1804** <ul>
1805** <li> SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
1806** <li> SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
1807** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
1808** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
1809** </ul>
1810**
1811** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
1812** was given on the corresponding lock.
1813**
1814** The xShmLock method can transition between unlocked and SHARED or
1815** between unlocked and EXCLUSIVE. It cannot transition between SHARED
1816** and EXCLUSIVE.
1817*/
1818#define SQLITE_SHM_UNLOCK1 1
1819#define SQLITE_SHM_LOCK2 2
1820#define SQLITE_SHM_SHARED4 4
1821#define SQLITE_SHM_EXCLUSIVE8 8
1822
1823/*
1824** CAPI3REF: Maximum xShmLock index
1825**
1826** The xShmLock method on [sqlite3_io_methods] may use values
1827** between 0 and this upper bound as its "offset" argument.
1828** The SQLite core will never attempt to acquire or release a
1829** lock outside of this range
1830*/
1831#define SQLITE_SHM_NLOCK8 8
1832
1833
1834/*
1835** CAPI3REF: Initialize The SQLite Library
1836**
1837** ^The sqlite3_initialize() routine initializes the
1838** SQLite library. ^The sqlite3_shutdown() routine
1839** deallocates any resources that were allocated by sqlite3_initialize().
1840** These routines are designed to aid in process initialization and
1841** shutdown on embedded systems. Workstation applications using
1842** SQLite normally do not need to invoke either of these routines.
1843**
1844** A call to sqlite3_initialize() is an "effective" call if it is
1845** the first time sqlite3_initialize() is invoked during the lifetime of
1846** the process, or if it is the first time sqlite3_initialize() is invoked
1847** following a call to sqlite3_shutdown(). ^(Only an effective call
1848** of sqlite3_initialize() does any initialization. All other calls
1849** are harmless no-ops.)^
1850**
1851** A call to sqlite3_shutdown() is an "effective" call if it is the first
1852** call to sqlite3_shutdown() since the last sqlite3_initialize(). ^(Only
1853** an effective call to sqlite3_shutdown() does any deinitialization.
1854** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
1855**
1856** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown()
1857** is not. The sqlite3_shutdown() interface must only be called from a
1858** single thread. All open [database connections] must be closed and all
1859** other SQLite resources must be deallocated prior to invoking
1860** sqlite3_shutdown().
1861**
1862** Among other things, ^sqlite3_initialize() will invoke
1863** sqlite3_os_init(). Similarly, ^sqlite3_shutdown()
1864** will invoke sqlite3_os_end().
1865**
1866** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success.
1867** ^If for some reason, sqlite3_initialize() is unable to initialize
1868** the library (perhaps it is unable to allocate a needed resource such
1869** as a mutex) it returns an [error code] other than [SQLITE_OK].
1870**
1871** ^The sqlite3_initialize() routine is called internally by many other
1872** SQLite interfaces so that an application usually does not need to
1873** invoke sqlite3_initialize() directly. For example, [sqlite3_open()]
1874** calls sqlite3_initialize() so the SQLite library will be automatically
1875** initialized when [sqlite3_open()] is called if it has not be initialized
1876** already. ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT]
1877** compile-time option, then the automatic calls to sqlite3_initialize()
1878** are omitted and the application must call sqlite3_initialize() directly
1879** prior to using any other SQLite interface. For maximum portability,
1880** it is recommended that applications always invoke sqlite3_initialize()
1881** directly prior to using any other SQLite interface. Future releases
1882** of SQLite may require this. In other words, the behavior exhibited
1883** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the
1884** default behavior in some future release of SQLite.
1885**
1886** The sqlite3_os_init() routine does operating-system specific
1887** initialization of the SQLite library. The sqlite3_os_end()
1888** routine undoes the effect of sqlite3_os_init(). Typical tasks
1889** performed by these routines include allocation or deallocation
1890** of static resources, initialization of global variables,
1891** setting up a default [sqlite3_vfs] module, or setting up
1892** a default configuration using [sqlite3_config()].
1893**
1894** The application should never invoke either sqlite3_os_init()
1895** or sqlite3_os_end() directly. The application should only invoke
1896** sqlite3_initialize() and sqlite3_shutdown(). The sqlite3_os_init()
1897** interface is called automatically by sqlite3_initialize() and
1898** sqlite3_os_end() is called by sqlite3_shutdown(). Appropriate
1899** implementations for sqlite3_os_init() and sqlite3_os_end()
1900** are built into SQLite when it is compiled for Unix, Windows, or OS/2.
1901** When [custom builds | built for other platforms]
1902** (using the [SQLITE_OS_OTHER=1] compile-time
1903** option) the application must supply a suitable implementation for
1904** sqlite3_os_init() and sqlite3_os_end(). An application-supplied
1905** implementation of sqlite3_os_init() or sqlite3_os_end()
1906** must return [SQLITE_OK] on success and some other [error code] upon
1907** failure.
1908*/
1909SQLITE_API int sqlite3_initialize(void);
1910SQLITE_API int sqlite3_shutdown(void);
1911SQLITE_API int sqlite3_os_init(void);
1912SQLITE_API int sqlite3_os_end(void);
1913
1914/*
1915** CAPI3REF: Configuring The SQLite Library
1916**
1917** The sqlite3_config() interface is used to make global configuration
1918** changes to SQLite in order to tune SQLite to the specific needs of
1919** the application. The default configuration is recommended for most
1920** applications and so this routine is usually not necessary. It is
1921** provided to support rare applications with unusual needs.
1922**
1923** <b>The sqlite3_config() interface is not threadsafe. The application
1924** must ensure that no other SQLite interfaces are invoked by other
1925** threads while sqlite3_config() is running.</b>
1926**
1927** The sqlite3_config() interface
1928** may only be invoked prior to library initialization using
1929** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
1930** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
1931** [sqlite3_shutdown()] then it will return SQLITE_MISUSE.
1932** Note, however, that ^sqlite3_config() can be called as part of the
1933** implementation of an application-defined [sqlite3_os_init()].
1934**
1935** The first argument to sqlite3_config() is an integer
1936** [configuration option] that determines
1937** what property of SQLite is to be configured. Subsequent arguments
1938** vary depending on the [configuration option]
1939** in the first argument.
1940**
1941** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
1942** ^If the option is unknown or SQLite is unable to set the option
1943** then this routine returns a non-zero [error code].
1944*/
1945SQLITE_API int sqlite3_config(int, ...);
1946
1947/*
1948** CAPI3REF: Configure database connections
1949** METHOD: sqlite3
1950**
1951** The sqlite3_db_config() interface is used to make configuration
1952** changes to a [database connection]. The interface is similar to
1953** [sqlite3_config()] except that the changes apply to a single
1954** [database connection] (specified in the first argument).
1955**
1956** The second argument to sqlite3_db_config(D,V,...) is the
1957** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
1958** that indicates what aspect of the [database connection] is being configured.
1959** Subsequent arguments vary depending on the configuration verb.
1960**
1961** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
1962** the call is considered successful.
1963*/
1964SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
1965
1966/*
1967** CAPI3REF: Memory Allocation Routines
1968**
1969** An instance of this object defines the interface between SQLite
1970** and low-level memory allocation routines.
1971**
1972** This object is used in only one place in the SQLite interface.
1973** A pointer to an instance of this object is the argument to
1974** [sqlite3_config()] when the configuration option is
1975** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC].
1976** By creating an instance of this object
1977** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC])
1978** during configuration, an application can specify an alternative
1979** memory allocation subsystem for SQLite to use for all of its
1980** dynamic memory needs.
1981**
1982** Note that SQLite comes with several [built-in memory allocators]
1983** that are perfectly adequate for the overwhelming majority of applications
1984** and that this object is only useful to a tiny minority of applications
1985** with specialized memory allocation requirements. This object is
1986** also used during testing of SQLite in order to specify an alternative
1987** memory allocator that simulates memory out-of-memory conditions in
1988** order to verify that SQLite recovers gracefully from such
1989** conditions.
1990**
1991** The xMalloc, xRealloc, and xFree methods must work like the
1992** malloc(), realloc() and free() functions from the standard C library.
1993** ^SQLite guarantees that the second argument to
1994** xRealloc is always a value returned by a prior call to xRoundup.
1995**
1996** xSize should return the allocated size of a memory allocation
1997** previously obtained from xMalloc or xRealloc. The allocated size
1998** is always at least as big as the requested size but may be larger.
1999**
2000** The xRoundup method returns what would be the allocated size of
2001** a memory allocation given a particular requested size. Most memory
2002** allocators round up memory allocations at least to the next multiple
2003** of 8. Some allocators round up to a larger multiple or to a power of 2.
2004** Every memory allocation request coming in through [sqlite3_malloc()]
2005** or [sqlite3_realloc()] first calls xRoundup. If xRoundup returns 0,
2006** that causes the corresponding memory allocation to fail.
2007**
2008** The xInit method initializes the memory allocator. For example,
2009** it might allocate any required mutexes or initialize internal data
2010** structures. The xShutdown method is invoked (indirectly) by
2011** [sqlite3_shutdown()] and should deallocate any resources acquired
2012** by xInit. The pAppData pointer is used as the only parameter to
2013** xInit and xShutdown.
2014**
2015** SQLite holds the [SQLITE_MUTEX_STATIC_MAIN] mutex when it invokes
2016** the xInit method, so the xInit method need not be threadsafe. The
2017** xShutdown method is only called from [sqlite3_shutdown()] so it does
2018** not need to be threadsafe either. For all other methods, SQLite
2019** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the
2020** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which
2021** it is by default) and so the methods are automatically serialized.
2022** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other
2023** methods must be threadsafe or else make their own arrangements for
2024** serialization.
2025**
2026** SQLite will never invoke xInit() more than once without an intervening
2027** call to xShutdown().
2028*/
2029typedef struct sqlite3_mem_methods sqlite3_mem_methods;
2030struct sqlite3_mem_methods {
2031 void *(*xMalloc)(int); /* Memory allocation function */
2032 void (*xFree)(void*); /* Free a prior allocation */
2033 void *(*xRealloc)(void*,int); /* Resize an allocation */
2034 int (*xSize)(void*); /* Return the size of an allocation */
2035 int (*xRoundup)(int); /* Round up request size to allocation size */
2036 int (*xInit)(void*); /* Initialize the memory allocator */
2037 void (*xShutdown)(void*); /* Deinitialize the memory allocator */
2038 void *pAppData; /* Argument to xInit() and xShutdown() */
2039};
2040
2041/*
2042** CAPI3REF: Configuration Options
2043** KEYWORDS: {configuration option}
2044**
2045** These constants are the available integer configuration options that
2046** can be passed as the first argument to the [sqlite3_config()] interface.
2047**
2048** New configuration options may be added in future releases of SQLite.
2049** Existing configuration options might be discontinued. Applications
2050** should check the return code from [sqlite3_config()] to make sure that
2051** the call worked. The [sqlite3_config()] interface will return a
2052** non-zero [error code] if a discontinued or unsupported configuration option
2053** is invoked.
2054**
2055** <dl>
2056** [[SQLITE_CONFIG_SINGLETHREAD]] <dt>SQLITE_CONFIG_SINGLETHREAD</dt>
2057** <dd>There are no arguments to this option. ^This option sets the
2058** [threading mode] to Single-thread. In other words, it disables
2059** all mutexing and puts SQLite into a mode where it can only be used
2060** by a single thread. ^If SQLite is compiled with
2061** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2062** it is not possible to change the [threading mode] from its default
2063** value of Single-thread and so [sqlite3_config()] will return
2064** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD
2065** configuration option.</dd>
2066**
2067** [[SQLITE_CONFIG_MULTITHREAD]] <dt>SQLITE_CONFIG_MULTITHREAD</dt>
2068** <dd>There are no arguments to this option. ^This option sets the
2069** [threading mode] to Multi-thread. In other words, it disables
2070** mutexing on [database connection] and [prepared statement] objects.
2071** The application is responsible for serializing access to
2072** [database connections] and [prepared statements]. But other mutexes
2073** are enabled so that SQLite will be safe to use in a multi-threaded
2074** environment as long as no two threads attempt to use the same
2075** [database connection] at the same time. ^If SQLite is compiled with
2076** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2077** it is not possible to set the Multi-thread [threading mode] and
2078** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
2079** SQLITE_CONFIG_MULTITHREAD configuration option.</dd>
2080**
2081** [[SQLITE_CONFIG_SERIALIZED]] <dt>SQLITE_CONFIG_SERIALIZED</dt>
2082** <dd>There are no arguments to this option. ^This option sets the
2083** [threading mode] to Serialized. In other words, this option enables
2084** all mutexes including the recursive
2085** mutexes on [database connection] and [prepared statement] objects.
2086** In this mode (which is the default when SQLite is compiled with
2087** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access
2088** to [database connections] and [prepared statements] so that the
2089** application is free to use the same [database connection] or the
2090** same [prepared statement] in different threads at the same time.
2091** ^If SQLite is compiled with
2092** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2093** it is not possible to set the Serialized [threading mode] and
2094** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
2095** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
2096**
2097** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
2098** <dd> ^(The SQLITE_CONFIG_MALLOC option takes a single argument which is
2099** a pointer to an instance of the [sqlite3_mem_methods] structure.
2100** The argument specifies
2101** alternative low-level memory allocation routines to be used in place of
2102** the memory allocation routines built into SQLite.)^ ^SQLite makes
2103** its own private copy of the content of the [sqlite3_mem_methods] structure
2104** before the [sqlite3_config()] call returns.</dd>
2105**
2106** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
2107** <dd> ^(The SQLITE_CONFIG_GETMALLOC option takes a single argument which
2108** is a pointer to an instance of the [sqlite3_mem_methods] structure.
2109** The [sqlite3_mem_methods]
2110** structure is filled with the currently defined memory allocation routines.)^
2111** This option can be used to overload the default memory allocation
2112** routines with a wrapper that simulations memory allocation failure or
2113** tracks memory usage, for example. </dd>
2114**
2115** [[SQLITE_CONFIG_SMALL_MALLOC]] <dt>SQLITE_CONFIG_SMALL_MALLOC</dt>
2116** <dd> ^The SQLITE_CONFIG_SMALL_MALLOC option takes single argument of
2117** type int, interpreted as a boolean, which if true provides a hint to
2118** SQLite that it should avoid large memory allocations if possible.
2119** SQLite will run faster if it is free to make large memory allocations,
2120** but some application might prefer to run slower in exchange for
2121** guarantees about memory fragmentation that are possible if large
2122** allocations are avoided. This hint is normally off.
2123** </dd>
2124**
2125** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
2126** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int,
2127** interpreted as a boolean, which enables or disables the collection of
2128** memory allocation statistics. ^(When memory allocation statistics are
2129** disabled, the following SQLite interfaces become non-operational:
2130** <ul>
2131** <li> [sqlite3_hard_heap_limit64()]
2132** <li> [sqlite3_memory_used()]
2133** <li> [sqlite3_memory_highwater()]
2134** <li> [sqlite3_soft_heap_limit64()]
2135** <li> [sqlite3_status64()]
2136** </ul>)^
2137** ^Memory allocation statistics are enabled by default unless SQLite is
2138** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
2139** allocation statistics are disabled by default.
2140** </dd>
2141**
2142** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
2143** <dd> The SQLITE_CONFIG_SCRATCH option is no longer used.
2144** </dd>
2145**
2146** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
2147** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a memory pool
2148** that SQLite can use for the database page cache with the default page
2149** cache implementation.
2150** This configuration option is a no-op if an application-defined page
2151** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2].
2152** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to
2153** 8-byte aligned memory (pMem), the size of each page cache line (sz),
2154** and the number of cache lines (N).
2155** The sz argument should be the size of the largest database page
2156** (a power of two between 512 and 65536) plus some extra bytes for each
2157** page header. ^The number of extra bytes needed by the page header
2158** can be determined using [SQLITE_CONFIG_PCACHE_HDRSZ].
2159** ^It is harmless, apart from the wasted memory,
2160** for the sz parameter to be larger than necessary. The pMem
2161** argument must be either a NULL pointer or a pointer to an 8-byte
2162** aligned block of memory of at least sz*N bytes, otherwise
2163** subsequent behavior is undefined.
2164** ^When pMem is not NULL, SQLite will strive to use the memory provided
2165** to satisfy page cache needs, falling back to [sqlite3_malloc()] if
2166** a page cache line is larger than sz bytes or if all of the pMem buffer
2167** is exhausted.
2168** ^If pMem is NULL and N is non-zero, then each database connection
2169** does an initial bulk allocation for page cache memory
2170** from [sqlite3_malloc()] sufficient for N cache lines if N is positive or
2171** of -1024*N bytes if N is negative, . ^If additional
2172** page cache memory is needed beyond what is provided by the initial
2173** allocation, then SQLite goes to [sqlite3_malloc()] separately for each
2174** additional cache line. </dd>
2175**
2176** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
2177** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer
2178** that SQLite will use for all of its dynamic memory allocation needs
2179** beyond those provided for by [SQLITE_CONFIG_PAGECACHE].
2180** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled
2181** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns
2182** [SQLITE_ERROR] if invoked otherwise.
2183** ^There are three arguments to SQLITE_CONFIG_HEAP:
2184** An 8-byte aligned pointer to the memory,
2185** the number of bytes in the memory buffer, and the minimum allocation size.
2186** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
2187** to using its default memory allocator (the system malloc() implementation),
2188** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the
2189** memory pointer is not NULL then the alternative memory
2190** allocator is engaged to handle all of SQLites memory allocation needs.
2191** The first pointer (the memory pointer) must be aligned to an 8-byte
2192** boundary or subsequent behavior of SQLite will be undefined.
2193** The minimum allocation size is capped at 2**12. Reasonable values
2194** for the minimum allocation size are 2**5 through 2**8.</dd>
2195**
2196** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
2197** <dd> ^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a
2198** pointer to an instance of the [sqlite3_mutex_methods] structure.
2199** The argument specifies alternative low-level mutex routines to be used
2200** in place the mutex routines built into SQLite.)^ ^SQLite makes a copy of
2201** the content of the [sqlite3_mutex_methods] structure before the call to
2202** [sqlite3_config()] returns. ^If SQLite is compiled with
2203** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2204** the entire mutexing subsystem is omitted from the build and hence calls to
2205** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
2206** return [SQLITE_ERROR].</dd>
2207**
2208** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
2209** <dd> ^(The SQLITE_CONFIG_GETMUTEX option takes a single argument which
2210** is a pointer to an instance of the [sqlite3_mutex_methods] structure. The
2211** [sqlite3_mutex_methods]
2212** structure is filled with the currently defined mutex routines.)^
2213** This option can be used to overload the default mutex allocation
2214** routines with a wrapper used to track mutex usage for performance
2215** profiling or testing, for example. ^If SQLite is compiled with
2216** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2217** the entire mutexing subsystem is omitted from the build and hence calls to
2218** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
2219** return [SQLITE_ERROR].</dd>
2220**
2221** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
2222** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine
2223** the default size of lookaside memory on each [database connection].
2224** The first argument is the
2225** size of each lookaside buffer slot and the second is the number of
2226** slots allocated to each database connection.)^ ^(SQLITE_CONFIG_LOOKASIDE
2227** sets the <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
2228** option to [sqlite3_db_config()] can be used to change the lookaside
2229** configuration on individual connections.)^ </dd>
2230**
2231** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
2232** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is
2233** a pointer to an [sqlite3_pcache_methods2] object. This object specifies
2234** the interface to a custom page cache implementation.)^
2235** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.</dd>
2236**
2237** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
2238** <dd> ^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which
2239** is a pointer to an [sqlite3_pcache_methods2] object. SQLite copies of
2240** the current page cache implementation into that object.)^ </dd>
2241**
2242** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
2243** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite
2244** global [error log].
2245** (^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
2246** function with a call signature of void(*)(void*,int,const char*),
2247** and a pointer to void. ^If the function pointer is not NULL, it is
2248** invoked by [sqlite3_log()] to process each logging event. ^If the
2249** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
2250** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is
2251** passed through as the first parameter to the application-defined logger
2252** function whenever that function is invoked. ^The second parameter to
2253** the logger function is a copy of the first parameter to the corresponding
2254** [sqlite3_log()] call and is intended to be a [result code] or an
2255** [extended result code]. ^The third parameter passed to the logger is
2256** log message after formatting via [sqlite3_snprintf()].
2257** The SQLite logging interface is not reentrant; the logger function
2258** supplied by the application must not invoke any SQLite interface.
2259** In a multi-threaded application, the application-defined logger
2260** function must be threadsafe. </dd>
2261**
2262** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
2263** <dd>^(The SQLITE_CONFIG_URI option takes a single argument of type int.
2264** If non-zero, then URI handling is globally enabled. If the parameter is zero,
2265** then URI handling is globally disabled.)^ ^If URI handling is globally
2266** enabled, all filenames passed to [sqlite3_open()], [sqlite3_open_v2()],
2267** [sqlite3_open16()] or
2268** specified as part of [ATTACH] commands are interpreted as URIs, regardless
2269** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
2270** connection is opened. ^If it is globally disabled, filenames are
2271** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
2272** database connection is opened. ^(By default, URI handling is globally
2273** disabled. The default value may be changed by compiling with the
2274** [SQLITE_USE_URI] symbol defined.)^
2275**
2276** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
2277** <dd>^The SQLITE_CONFIG_COVERING_INDEX_SCAN option takes a single integer
2278** argument which is interpreted as a boolean in order to enable or disable
2279** the use of covering indices for full table scans in the query optimizer.
2280** ^The default setting is determined
2281** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
2282** if that compile-time option is omitted.
2283** The ability to disable the use of covering indices for full table scans
2284** is because some incorrectly coded legacy applications might malfunction
2285** when the optimization is enabled. Providing the ability to
2286** disable the optimization allows the older, buggy application code to work
2287** without change even with newer versions of SQLite.
2288**
2289** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
2290** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
2291** <dd> These options are obsolete and should not be used by new code.
2292** They are retained for backwards compatibility but are now no-ops.
2293** </dd>
2294**
2295** [[SQLITE_CONFIG_SQLLOG]]
2296** <dt>SQLITE_CONFIG_SQLLOG
2297** <dd>This option is only available if sqlite is compiled with the
2298** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
2299** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
2300** The second should be of type (void*). The callback is invoked by the library
2301** in three separate circumstances, identified by the value passed as the
2302** fourth parameter. If the fourth parameter is 0, then the database connection
2303** passed as the second argument has just been opened. The third argument
2304** points to a buffer containing the name of the main database file. If the
2305** fourth parameter is 1, then the SQL statement that the third parameter
2306** points to has just been executed. Or, if the fourth parameter is 2, then
2307** the connection being passed as the second parameter is being closed. The
2308** third parameter is passed NULL In this case. An example of using this
2309** configuration option can be seen in the "test_sqllog.c" source file in
2310** the canonical SQLite source tree.</dd>
2311**
2312** [[SQLITE_CONFIG_MMAP_SIZE]]
2313** <dt>SQLITE_CONFIG_MMAP_SIZE
2314** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
2315** that are the default mmap size limit (the default setting for
2316** [PRAGMA mmap_size]) and the maximum allowed mmap size limit.
2317** ^The default setting can be overridden by each database connection using
2318** either the [PRAGMA mmap_size] command, or by using the
2319** [SQLITE_FCNTL_MMAP_SIZE] file control. ^(The maximum allowed mmap size
2320** will be silently truncated if necessary so that it does not exceed the
2321** compile-time maximum mmap size set by the
2322** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
2323** ^If either argument to this option is negative, then that argument is
2324** changed to its compile-time default.
2325**
2326** [[SQLITE_CONFIG_WIN32_HEAPSIZE]]
2327** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE
2328** <dd>^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is
2329** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro
2330** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
2331** that specifies the maximum size of the created heap.
2332**
2333** [[SQLITE_CONFIG_PCACHE_HDRSZ]]
2334** <dt>SQLITE_CONFIG_PCACHE_HDRSZ
2335** <dd>^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which
2336** is a pointer to an integer and writes into that integer the number of extra
2337** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE].
2338** The amount of extra space required can change depending on the compiler,
2339** target platform, and SQLite version.
2340**
2341** [[SQLITE_CONFIG_PMASZ]]
2342** <dt>SQLITE_CONFIG_PMASZ
2343** <dd>^The SQLITE_CONFIG_PMASZ option takes a single parameter which
2344** is an unsigned integer and sets the "Minimum PMA Size" for the multithreaded
2345** sorter to that integer. The default minimum PMA Size is set by the
2346** [SQLITE_SORTER_PMASZ] compile-time option. New threads are launched
2347** to help with sort operations when multithreaded sorting
2348** is enabled (using the [PRAGMA threads] command) and the amount of content
2349** to be sorted exceeds the page size times the minimum of the
2350** [PRAGMA cache_size] setting and this value.
2351**
2352** [[SQLITE_CONFIG_STMTJRNL_SPILL]]
2353** <dt>SQLITE_CONFIG_STMTJRNL_SPILL
2354** <dd>^The SQLITE_CONFIG_STMTJRNL_SPILL option takes a single parameter which
2355** becomes the [statement journal] spill-to-disk threshold.
2356** [Statement journals] are held in memory until their size (in bytes)
2357** exceeds this threshold, at which point they are written to disk.
2358** Or if the threshold is -1, statement journals are always held
2359** exclusively in memory.
2360** Since many statement journals never become large, setting the spill
2361** threshold to a value such as 64KiB can greatly reduce the amount of
2362** I/O required to support statement rollback.
2363** The default value for this setting is controlled by the
2364** [SQLITE_STMTJRNL_SPILL] compile-time option.
2365**
2366** [[SQLITE_CONFIG_SORTERREF_SIZE]]
2367** <dt>SQLITE_CONFIG_SORTERREF_SIZE
2368** <dd>The SQLITE_CONFIG_SORTERREF_SIZE option accepts a single parameter
2369** of type (int) - the new value of the sorter-reference size threshold.
2370** Usually, when SQLite uses an external sort to order records according
2371** to an ORDER BY clause, all fields required by the caller are present in the
2372** sorted records. However, if SQLite determines based on the declared type
2373** of a table column that its values are likely to be very large - larger
2374** than the configured sorter-reference size threshold - then a reference
2375** is stored in each sorted record and the required column values loaded
2376** from the database as records are returned in sorted order. The default
2377** value for this option is to never use this optimization. Specifying a
2378** negative value for this option restores the default behaviour.
2379** This option is only available if SQLite is compiled with the
2380** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option.
2381**
2382** [[SQLITE_CONFIG_MEMDB_MAXSIZE]]
2383** <dt>SQLITE_CONFIG_MEMDB_MAXSIZE
2384** <dd>The SQLITE_CONFIG_MEMDB_MAXSIZE option accepts a single parameter
2385** [sqlite3_int64] parameter which is the default maximum size for an in-memory
2386** database created using [sqlite3_deserialize()]. This default maximum
2387** size can be adjusted up or down for individual databases using the
2388** [SQLITE_FCNTL_SIZE_LIMIT] [sqlite3_file_control|file-control]. If this
2389** configuration setting is never used, then the default maximum is determined
2390** by the [SQLITE_MEMDB_DEFAULT_MAXSIZE] compile-time option. If that
2391** compile-time option is not set, then the default maximum is 1073741824.
2392** </dl>
2393*/
2394#define SQLITE_CONFIG_SINGLETHREAD1 1 /* nil */
2395#define SQLITE_CONFIG_MULTITHREAD2 2 /* nil */
2396#define SQLITE_CONFIG_SERIALIZED3 3 /* nil */
2397#define SQLITE_CONFIG_MALLOC4 4 /* sqlite3_mem_methods* */
2398#define SQLITE_CONFIG_GETMALLOC5 5 /* sqlite3_mem_methods* */
2399#define SQLITE_CONFIG_SCRATCH6 6 /* No longer used */
2400#define SQLITE_CONFIG_PAGECACHE7 7 /* void*, int sz, int N */
2401#define SQLITE_CONFIG_HEAP8 8 /* void*, int nByte, int min */
2402#define SQLITE_CONFIG_MEMSTATUS9 9 /* boolean */
2403#define SQLITE_CONFIG_MUTEX10 10 /* sqlite3_mutex_methods* */
2404#define SQLITE_CONFIG_GETMUTEX11 11 /* sqlite3_mutex_methods* */
2405/* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
2406#define SQLITE_CONFIG_LOOKASIDE13 13 /* int int */
2407#define SQLITE_CONFIG_PCACHE14 14 /* no-op */
2408#define SQLITE_CONFIG_GETPCACHE15 15 /* no-op */
2409#define SQLITE_CONFIG_LOG16 16 /* xFunc, void* */
2410#define SQLITE_CONFIG_URI17 17 /* int */
2411#define SQLITE_CONFIG_PCACHE218 18 /* sqlite3_pcache_methods2* */
2412#define SQLITE_CONFIG_GETPCACHE219 19 /* sqlite3_pcache_methods2* */
2413#define SQLITE_CONFIG_COVERING_INDEX_SCAN20 20 /* int */
2414#define SQLITE_CONFIG_SQLLOG21 21 /* xSqllog, void* */
2415#define SQLITE_CONFIG_MMAP_SIZE22 22 /* sqlite3_int64, sqlite3_int64 */
2416#define SQLITE_CONFIG_WIN32_HEAPSIZE23 23 /* int nByte */
2417#define SQLITE_CONFIG_PCACHE_HDRSZ24 24 /* int *psz */
2418#define SQLITE_CONFIG_PMASZ25 25 /* unsigned int szPma */
2419#define SQLITE_CONFIG_STMTJRNL_SPILL26 26 /* int nByte */
2420#define SQLITE_CONFIG_SMALL_MALLOC27 27 /* boolean */
2421#define SQLITE_CONFIG_SORTERREF_SIZE28 28 /* int nByte */
2422#define SQLITE_CONFIG_MEMDB_MAXSIZE29 29 /* sqlite3_int64 */
2423
2424/*
2425** CAPI3REF: Database Connection Configuration Options
2426**
2427** These constants are the available integer configuration options that
2428** can be passed as the second argument to the [sqlite3_db_config()] interface.
2429**
2430** New configuration options may be added in future releases of SQLite.
2431** Existing configuration options might be discontinued. Applications
2432** should check the return code from [sqlite3_db_config()] to make sure that
2433** the call worked. ^The [sqlite3_db_config()] interface will return a
2434** non-zero [error code] if a discontinued or unsupported configuration option
2435** is invoked.
2436**
2437** <dl>
2438** [[SQLITE_DBCONFIG_LOOKASIDE]]
2439** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
2440** <dd> ^This option takes three additional arguments that determine the
2441** [lookaside memory allocator] configuration for the [database connection].
2442** ^The first argument (the third parameter to [sqlite3_db_config()] is a
2443** pointer to a memory buffer to use for lookaside memory.
2444** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
2445** may be NULL in which case SQLite will allocate the
2446** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
2447** size of each lookaside buffer slot. ^The third argument is the number of
2448** slots. The size of the buffer in the first argument must be greater than
2449** or equal to the product of the second and third arguments. The buffer
2450** must be aligned to an 8-byte boundary. ^If the second argument to
2451** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
2452** rounded down to the next smaller multiple of 8. ^(The lookaside memory
2453** configuration for a database connection can only be changed when that
2454** connection is not currently using lookaside memory, or in other words
2455** when the "current value" returned by
2456** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
2457** Any attempt to change the lookaside memory configuration when lookaside
2458** memory is in use leaves the configuration unchanged and returns
2459** [SQLITE_BUSY].)^</dd>
2460**
2461** [[SQLITE_DBCONFIG_ENABLE_FKEY]]
2462** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
2463** <dd> ^This option is used to enable or disable the enforcement of
2464** [foreign key constraints]. There should be two additional arguments.
2465** The first argument is an integer which is 0 to disable FK enforcement,
2466** positive to enable FK enforcement or negative to leave FK enforcement
2467** unchanged. The second parameter is a pointer to an integer into which
2468** is written 0 or 1 to indicate whether FK enforcement is off or on
2469** following this call. The second parameter may be a NULL pointer, in
2470** which case the FK enforcement setting is not reported back. </dd>
2471**
2472** [[SQLITE_DBCONFIG_ENABLE_TRIGGER]]
2473** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
2474** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
2475** There should be two additional arguments.
2476** The first argument is an integer which is 0 to disable triggers,
2477** positive to enable triggers or negative to leave the setting unchanged.
2478** The second parameter is a pointer to an integer into which
2479** is written 0 or 1 to indicate whether triggers are disabled or enabled
2480** following this call. The second parameter may be a NULL pointer, in
2481** which case the trigger setting is not reported back.
2482**
2483** <p>Originally this option disabled all triggers. ^(However, since
2484** SQLite version 3.35.0, TEMP triggers are still allowed even if
2485** this option is off. So, in other words, this option now only disables
2486** triggers in the main database schema or in the schemas of ATTACH-ed
2487** databases.)^ </dd>
2488**
2489** [[SQLITE_DBCONFIG_ENABLE_VIEW]]
2490** <dt>SQLITE_DBCONFIG_ENABLE_VIEW</dt>
2491** <dd> ^This option is used to enable or disable [CREATE VIEW | views].
2492** There should be two additional arguments.
2493** The first argument is an integer which is 0 to disable views,
2494** positive to enable views or negative to leave the setting unchanged.
2495** The second parameter is a pointer to an integer into which
2496** is written 0 or 1 to indicate whether views are disabled or enabled
2497** following this call. The second parameter may be a NULL pointer, in
2498** which case the view setting is not reported back.
2499**
2500** <p>Originally this option disabled all views. ^(However, since
2501** SQLite version 3.35.0, TEMP views are still allowed even if
2502** this option is off. So, in other words, this option now only disables
2503** views in the main database schema or in the schemas of ATTACH-ed
2504** databases.)^ </dd>
2505**
2506** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]]
2507** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
2508** <dd> ^This option is used to enable or disable the
2509** [fts3_tokenizer()] function which is part of the
2510** [FTS3] full-text search engine extension.
2511** There should be two additional arguments.
2512** The first argument is an integer which is 0 to disable fts3_tokenizer() or
2513** positive to enable fts3_tokenizer() or negative to leave the setting
2514** unchanged.
2515** The second parameter is a pointer to an integer into which
2516** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled
2517** following this call. The second parameter may be a NULL pointer, in
2518** which case the new setting is not reported back. </dd>
2519**
2520** [[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION]]
2521** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt>
2522** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()]
2523** interface independently of the [load_extension()] SQL function.
2524** The [sqlite3_enable_load_extension()] API enables or disables both the
2525** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
2526** There should be two additional arguments.
2527** When the first argument to this interface is 1, then only the C-API is
2528** enabled and the SQL function remains disabled. If the first argument to
2529** this interface is 0, then both the C-API and the SQL function are disabled.
2530** If the first argument is -1, then no changes are made to state of either the
2531** C-API or the SQL function.
2532** The second parameter is a pointer to an integer into which
2533** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface
2534** is disabled or enabled following this call. The second parameter may
2535** be a NULL pointer, in which case the new setting is not reported back.
2536** </dd>
2537**
2538** [[SQLITE_DBCONFIG_MAINDBNAME]] <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
2539** <dd> ^This option is used to change the name of the "main" database
2540** schema. ^The sole argument is a pointer to a constant UTF8 string
2541** which will become the new schema name in place of "main". ^SQLite
2542** does not make a copy of the new main schema name string, so the application
2543** must ensure that the argument passed into this DBCONFIG option is unchanged
2544** until after the database connection closes.
2545** </dd>
2546**
2547** [[SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE]]
2548** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt>
2549** <dd> Usually, when a database in wal mode is closed or detached from a
2550** database handle, SQLite checks if this will mean that there are now no
2551** connections at all to the database. If so, it performs a checkpoint
2552** operation before closing the connection. This option may be used to
2553** override this behaviour. The first parameter passed to this operation
2554** is an integer - positive to disable checkpoints-on-close, or zero (the
2555** default) to enable them, and negative to leave the setting unchanged.
2556** The second parameter is a pointer to an integer
2557** into which is written 0 or 1 to indicate whether checkpoints-on-close
2558** have been disabled - 0 if they are not disabled, 1 if they are.
2559** </dd>
2560**
2561** [[SQLITE_DBCONFIG_ENABLE_QPSG]] <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
2562** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
2563** the [query planner stability guarantee] (QPSG). When the QPSG is active,
2564** a single SQL query statement will always use the same algorithm regardless
2565** of values of [bound parameters].)^ The QPSG disables some query optimizations
2566** that look at the values of bound parameters, which can make some queries
2567** slower. But the QPSG has the advantage of more predictable behavior. With
2568** the QPSG active, SQLite will always use the same query plan in the field as
2569** was used during testing in the lab.
2570** The first argument to this setting is an integer which is 0 to disable
2571** the QPSG, positive to enable QPSG, or negative to leave the setting
2572** unchanged. The second parameter is a pointer to an integer into which
2573** is written 0 or 1 to indicate whether the QPSG is disabled or enabled
2574** following this call.
2575** </dd>
2576**
2577** [[SQLITE_DBCONFIG_TRIGGER_EQP]] <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
2578** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not
2579** include output for any operations performed by trigger programs. This
2580** option is used to set or clear (the default) a flag that governs this
2581** behavior. The first parameter passed to this operation is an integer -
2582** positive to enable output for trigger programs, or zero to disable it,
2583** or negative to leave the setting unchanged.
2584** The second parameter is a pointer to an integer into which is written
2585** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
2586** it is not disabled, 1 if it is.
2587** </dd>
2588**
2589** [[SQLITE_DBCONFIG_RESET_DATABASE]] <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
2590** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run
2591** [VACUUM] in order to reset a database back to an empty database
2592** with no schema and no content. The following process works even for
2593** a badly corrupted database file:
2594** <ol>
2595** <li> If the database connection is newly opened, make sure it has read the
2596** database schema by preparing then discarding some query against the
2597** database, or calling sqlite3_table_column_metadata(), ignoring any
2598** errors. This step is only necessary if the application desires to keep
2599** the database in WAL mode after the reset if it was in WAL mode before
2600** the reset.
2601** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
2602** <li> [sqlite3_exec](db, "[VACUUM]", 0, 0, 0);
2603** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
2604** </ol>
2605** Because resetting a database is destructive and irreversible, the
2606** process requires the use of this obscure API and multiple steps to help
2607** ensure that it does not happen by accident.
2608**
2609** [[SQLITE_DBCONFIG_DEFENSIVE]] <dt>SQLITE_DBCONFIG_DEFENSIVE</dt>
2610** <dd>The SQLITE_DBCONFIG_DEFENSIVE option activates or deactivates the
2611** "defensive" flag for a database connection. When the defensive
2612** flag is enabled, language features that allow ordinary SQL to
2613** deliberately corrupt the database file are disabled. The disabled
2614** features include but are not limited to the following:
2615** <ul>
2616** <li> The [PRAGMA writable_schema=ON] statement.
2617** <li> The [PRAGMA journal_mode=OFF] statement.
2618** <li> Writes to the [sqlite_dbpage] virtual table.
2619** <li> Direct writes to [shadow tables].
2620** </ul>
2621** </dd>
2622**
2623** [[SQLITE_DBCONFIG_WRITABLE_SCHEMA]] <dt>SQLITE_DBCONFIG_WRITABLE_SCHEMA</dt>
2624** <dd>The SQLITE_DBCONFIG_WRITABLE_SCHEMA option activates or deactivates the
2625** "writable_schema" flag. This has the same effect and is logically equivalent
2626** to setting [PRAGMA writable_schema=ON] or [PRAGMA writable_schema=OFF].
2627** The first argument to this setting is an integer which is 0 to disable
2628** the writable_schema, positive to enable writable_schema, or negative to
2629** leave the setting unchanged. The second parameter is a pointer to an
2630** integer into which is written 0 or 1 to indicate whether the writable_schema
2631** is enabled or disabled following this call.
2632** </dd>
2633**
2634** [[SQLITE_DBCONFIG_LEGACY_ALTER_TABLE]]
2635** <dt>SQLITE_DBCONFIG_LEGACY_ALTER_TABLE</dt>
2636** <dd>The SQLITE_DBCONFIG_LEGACY_ALTER_TABLE option activates or deactivates
2637** the legacy behavior of the [ALTER TABLE RENAME] command such it
2638** behaves as it did prior to [version 3.24.0] (2018-06-04). See the
2639** "Compatibility Notice" on the [ALTER TABLE RENAME documentation] for
2640** additional information. This feature can also be turned on and off
2641** using the [PRAGMA legacy_alter_table] statement.
2642** </dd>
2643**
2644** [[SQLITE_DBCONFIG_DQS_DML]]
2645** <dt>SQLITE_DBCONFIG_DQS_DML</td>
2646** <dd>The SQLITE_DBCONFIG_DQS_DML option activates or deactivates
2647** the legacy [double-quoted string literal] misfeature for DML statements
2648** only, that is DELETE, INSERT, SELECT, and UPDATE statements. The
2649** default value of this setting is determined by the [-DSQLITE_DQS]
2650** compile-time option.
2651** </dd>
2652**
2653** [[SQLITE_DBCONFIG_DQS_DDL]]
2654** <dt>SQLITE_DBCONFIG_DQS_DDL</td>
2655** <dd>The SQLITE_DBCONFIG_DQS option activates or deactivates
2656** the legacy [double-quoted string literal] misfeature for DDL statements,
2657** such as CREATE TABLE and CREATE INDEX. The
2658** default value of this setting is determined by the [-DSQLITE_DQS]
2659** compile-time option.
2660** </dd>
2661**
2662** [[SQLITE_DBCONFIG_TRUSTED_SCHEMA]]
2663** <dt>SQLITE_DBCONFIG_TRUSTED_SCHEMA</td>
2664** <dd>The SQLITE_DBCONFIG_TRUSTED_SCHEMA option tells SQLite to
2665** assume that database schemas are untainted by malicious content.
2666** When the SQLITE_DBCONFIG_TRUSTED_SCHEMA option is disabled, SQLite
2667** takes additional defensive steps to protect the application from harm
2668** including:
2669** <ul>
2670** <li> Prohibit the use of SQL functions inside triggers, views,
2671** CHECK constraints, DEFAULT clauses, expression indexes,
2672** partial indexes, or generated columns
2673** unless those functions are tagged with [SQLITE_INNOCUOUS].
2674** <li> Prohibit the use of virtual tables inside of triggers or views
2675** unless those virtual tables are tagged with [SQLITE_VTAB_INNOCUOUS].
2676** </ul>
2677** This setting defaults to "on" for legacy compatibility, however
2678** all applications are advised to turn it off if possible. This setting
2679** can also be controlled using the [PRAGMA trusted_schema] statement.
2680** </dd>
2681**
2682** [[SQLITE_DBCONFIG_LEGACY_FILE_FORMAT]]
2683** <dt>SQLITE_DBCONFIG_LEGACY_FILE_FORMAT</td>
2684** <dd>The SQLITE_DBCONFIG_LEGACY_FILE_FORMAT option activates or deactivates
2685** the legacy file format flag. When activated, this flag causes all newly
2686** created database file to have a schema format version number (the 4-byte
2687** integer found at offset 44 into the database header) of 1. This in turn
2688** means that the resulting database file will be readable and writable by
2689** any SQLite version back to 3.0.0 ([dateof:3.0.0]). Without this setting,
2690** newly created databases are generally not understandable by SQLite versions
2691** prior to 3.3.0 ([dateof:3.3.0]). As these words are written, there
2692** is now scarcely any need to generated database files that are compatible
2693** all the way back to version 3.0.0, and so this setting is of little
2694** practical use, but is provided so that SQLite can continue to claim the
2695** ability to generate new database files that are compatible with version
2696** 3.0.0.
2697** <p>Note that when the SQLITE_DBCONFIG_LEGACY_FILE_FORMAT setting is on,
2698** the [VACUUM] command will fail with an obscure error when attempting to
2699** process a table with generated columns and a descending index. This is
2700** not considered a bug since SQLite versions 3.3.0 and earlier do not support
2701** either generated columns or decending indexes.
2702** </dd>
2703** </dl>
2704*/
2705#define SQLITE_DBCONFIG_MAINDBNAME1000 1000 /* const char* */
2706#define SQLITE_DBCONFIG_LOOKASIDE1001 1001 /* void* int int */
2707#define SQLITE_DBCONFIG_ENABLE_FKEY1002 1002 /* int int* */
2708#define SQLITE_DBCONFIG_ENABLE_TRIGGER1003 1003 /* int int* */
2709#define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER1004 1004 /* int int* */
2710#define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION1005 1005 /* int int* */
2711#define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE1006 1006 /* int int* */
2712#define SQLITE_DBCONFIG_ENABLE_QPSG1007 1007 /* int int* */
2713#define SQLITE_DBCONFIG_TRIGGER_EQP1008 1008 /* int int* */
2714#define SQLITE_DBCONFIG_RESET_DATABASE1009 1009 /* int int* */
2715#define SQLITE_DBCONFIG_DEFENSIVE1010 1010 /* int int* */
2716#define SQLITE_DBCONFIG_WRITABLE_SCHEMA1011 1011 /* int int* */
2717#define SQLITE_DBCONFIG_LEGACY_ALTER_TABLE1012 1012 /* int int* */
2718#define SQLITE_DBCONFIG_DQS_DML1013 1013 /* int int* */
2719#define SQLITE_DBCONFIG_DQS_DDL1014 1014 /* int int* */
2720#define SQLITE_DBCONFIG_ENABLE_VIEW1015 1015 /* int int* */
2721#define SQLITE_DBCONFIG_LEGACY_FILE_FORMAT1016 1016 /* int int* */
2722#define SQLITE_DBCONFIG_TRUSTED_SCHEMA1017 1017 /* int int* */
2723#define SQLITE_DBCONFIG_MAX1017 1017 /* Largest DBCONFIG */
2724
2725/*
2726** CAPI3REF: Enable Or Disable Extended Result Codes
2727** METHOD: sqlite3
2728**
2729** ^The sqlite3_extended_result_codes() routine enables or disables the
2730** [extended result codes] feature of SQLite. ^The extended result
2731** codes are disabled by default for historical compatibility.
2732*/
2733SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
2734
2735/*
2736** CAPI3REF: Last Insert Rowid
2737** METHOD: sqlite3
2738**
2739** ^Each entry in most SQLite tables (except for [WITHOUT ROWID] tables)
2740** has a unique 64-bit signed
2741** integer key called the [ROWID | "rowid"]. ^The rowid is always available
2742** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
2743** names are not also used by explicitly declared columns. ^If
2744** the table has a column of type [INTEGER PRIMARY KEY] then that column
2745** is another alias for the rowid.
2746**
2747** ^The sqlite3_last_insert_rowid(D) interface usually returns the [rowid] of
2748** the most recent successful [INSERT] into a rowid table or [virtual table]
2749** on database connection D. ^Inserts into [WITHOUT ROWID] tables are not
2750** recorded. ^If no successful [INSERT]s into rowid tables have ever occurred
2751** on the database connection D, then sqlite3_last_insert_rowid(D) returns
2752** zero.
2753**
2754** As well as being set automatically as rows are inserted into database
2755** tables, the value returned by this function may be set explicitly by
2756** [sqlite3_set_last_insert_rowid()]
2757**
2758** Some virtual table implementations may INSERT rows into rowid tables as
2759** part of committing a transaction (e.g. to flush data accumulated in memory
2760** to disk). In this case subsequent calls to this function return the rowid
2761** associated with these internal INSERT operations, which leads to
2762** unintuitive results. Virtual table implementations that do write to rowid
2763** tables in this way can avoid this problem by restoring the original
2764** rowid value using [sqlite3_set_last_insert_rowid()] before returning
2765** control to the user.
2766**
2767** ^(If an [INSERT] occurs within a trigger then this routine will
2768** return the [rowid] of the inserted row as long as the trigger is
2769** running. Once the trigger program ends, the value returned
2770** by this routine reverts to what it was before the trigger was fired.)^
2771**
2772** ^An [INSERT] that fails due to a constraint violation is not a
2773** successful [INSERT] and does not change the value returned by this
2774** routine. ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
2775** and INSERT OR ABORT make no changes to the return value of this
2776** routine when their insertion fails. ^(When INSERT OR REPLACE
2777** encounters a constraint violation, it does not fail. The
2778** INSERT continues to completion after deleting rows that caused
2779** the constraint problem so INSERT OR REPLACE will always change
2780** the return value of this interface.)^
2781**
2782** ^For the purposes of this routine, an [INSERT] is considered to
2783** be successful even if it is subsequently rolled back.
2784**
2785** This function is accessible to SQL statements via the
2786** [last_insert_rowid() SQL function].
2787**
2788** If a separate thread performs a new [INSERT] on the same
2789** database connection while the [sqlite3_last_insert_rowid()]
2790** function is running and thus changes the last insert [rowid],
2791** then the value returned by [sqlite3_last_insert_rowid()] is
2792** unpredictable and might not equal either the old or the new
2793** last insert [rowid].
2794*/
2795SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
2796
2797/*
2798** CAPI3REF: Set the Last Insert Rowid value.
2799** METHOD: sqlite3
2800**
2801** The sqlite3_set_last_insert_rowid(D, R) method allows the application to
2802** set the value returned by calling sqlite3_last_insert_rowid(D) to R
2803** without inserting a row into the database.
2804*/
2805SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3*,sqlite3_int64);
2806
2807/*
2808** CAPI3REF: Count The Number Of Rows Modified
2809** METHOD: sqlite3
2810**
2811** ^These functions return the number of rows modified, inserted or
2812** deleted by the most recently completed INSERT, UPDATE or DELETE
2813** statement on the database connection specified by the only parameter.
2814** The two functions are identical except for the type of the return value
2815** and that if the number of rows modified by the most recent INSERT, UPDATE
2816** or DELETE is greater than the maximum value supported by type "int", then
2817** the return value of sqlite3_changes() is undefined. ^Executing any other
2818** type of SQL statement does not modify the value returned by these functions.
2819**
2820** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are
2821** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
2822** [foreign key actions] or [REPLACE] constraint resolution are not counted.
2823**
2824** Changes to a view that are intercepted by
2825** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value
2826** returned by sqlite3_changes() immediately after an INSERT, UPDATE or
2827** DELETE statement run on a view is always zero. Only changes made to real
2828** tables are counted.
2829**
2830** Things are more complicated if the sqlite3_changes() function is
2831** executed while a trigger program is running. This may happen if the
2832** program uses the [changes() SQL function], or if some other callback
2833** function invokes sqlite3_changes() directly. Essentially:
2834**
2835** <ul>
2836** <li> ^(Before entering a trigger program the value returned by
2837** sqlite3_changes() function is saved. After the trigger program
2838** has finished, the original value is restored.)^
2839**
2840** <li> ^(Within a trigger program each INSERT, UPDATE and DELETE
2841** statement sets the value returned by sqlite3_changes()
2842** upon completion as normal. Of course, this value will not include
2843** any changes performed by sub-triggers, as the sqlite3_changes()
2844** value will be saved and restored after each sub-trigger has run.)^
2845** </ul>
2846**
2847** ^This means that if the changes() SQL function (or similar) is used
2848** by the first INSERT, UPDATE or DELETE statement within a trigger, it
2849** returns the value as set when the calling statement began executing.
2850** ^If it is used by the second or subsequent such statement within a trigger
2851** program, the value returned reflects the number of rows modified by the
2852** previous INSERT, UPDATE or DELETE statement within the same trigger.
2853**
2854** If a separate thread makes changes on the same database connection
2855** while [sqlite3_changes()] is running then the value returned
2856** is unpredictable and not meaningful.
2857**
2858** See also:
2859** <ul>
2860** <li> the [sqlite3_total_changes()] interface
2861** <li> the [count_changes pragma]
2862** <li> the [changes() SQL function]
2863** <li> the [data_version pragma]
2864** </ul>
2865*/
2866SQLITE_API int sqlite3_changes(sqlite3*);
2867SQLITE_API sqlite3_int64 sqlite3_changes64(sqlite3*);
2868
2869/*
2870** CAPI3REF: Total Number Of Rows Modified
2871** METHOD: sqlite3
2872**
2873** ^These functions return the total number of rows inserted, modified or
2874** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed
2875** since the database connection was opened, including those executed as
2876** part of trigger programs. The two functions are identical except for the
2877** type of the return value and that if the number of rows modified by the
2878** connection exceeds the maximum value supported by type "int", then
2879** the return value of sqlite3_total_changes() is undefined. ^Executing
2880** any other type of SQL statement does not affect the value returned by
2881** sqlite3_total_changes().
2882**
2883** ^Changes made as part of [foreign key actions] are included in the
2884** count, but those made as part of REPLACE constraint resolution are
2885** not. ^Changes to a view that are intercepted by INSTEAD OF triggers
2886** are not counted.
2887**
2888** The [sqlite3_total_changes(D)] interface only reports the number
2889** of rows that changed due to SQL statement run against database
2890** connection D. Any changes by other database connections are ignored.
2891** To detect changes against a database file from other database
2892** connections use the [PRAGMA data_version] command or the
2893** [SQLITE_FCNTL_DATA_VERSION] [file control].
2894**
2895** If a separate thread makes changes on the same database connection
2896** while [sqlite3_total_changes()] is running then the value
2897** returned is unpredictable and not meaningful.
2898**
2899** See also:
2900** <ul>
2901** <li> the [sqlite3_changes()] interface
2902** <li> the [count_changes pragma]
2903** <li> the [changes() SQL function]
2904** <li> the [data_version pragma]
2905** <li> the [SQLITE_FCNTL_DATA_VERSION] [file control]
2906** </ul>
2907*/
2908SQLITE_API int sqlite3_total_changes(sqlite3*);
2909SQLITE_API sqlite3_int64 sqlite3_total_changes64(sqlite3*);
2910
2911/*
2912** CAPI3REF: Interrupt A Long-Running Query
2913** METHOD: sqlite3
2914**
2915** ^This function causes any pending database operation to abort and
2916** return at its earliest opportunity. This routine is typically
2917** called in response to a user action such as pressing "Cancel"
2918** or Ctrl-C where the user wants a long query operation to halt
2919** immediately.
2920**
2921** ^It is safe to call this routine from a thread different from the
2922** thread that is currently running the database operation. But it
2923** is not safe to call this routine with a [database connection] that
2924** is closed or might close before sqlite3_interrupt() returns.
2925**
2926** ^If an SQL operation is very nearly finished at the time when
2927** sqlite3_interrupt() is called, then it might not have an opportunity
2928** to be interrupted and might continue to completion.
2929**
2930** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT].
2931** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE
2932** that is inside an explicit transaction, then the entire transaction
2933** will be rolled back automatically.
2934**
2935** ^The sqlite3_interrupt(D) call is in effect until all currently running
2936** SQL statements on [database connection] D complete. ^Any new SQL statements
2937** that are started after the sqlite3_interrupt() call and before the
2938** running statement count reaches zero are interrupted as if they had been
2939** running prior to the sqlite3_interrupt() call. ^New SQL statements
2940** that are started after the running statement count reaches zero are
2941** not effected by the sqlite3_interrupt().
2942** ^A call to sqlite3_interrupt(D) that occurs when there are no running
2943** SQL statements is a no-op and has no effect on SQL statements
2944** that are started after the sqlite3_interrupt() call returns.
2945*/
2946SQLITE_API void sqlite3_interrupt(sqlite3*);
2947
2948/*
2949** CAPI3REF: Determine If An SQL Statement Is Complete
2950**
2951** These routines are useful during command-line input to determine if the
2952** currently entered text seems to form a complete SQL statement or
2953** if additional input is needed before sending the text into
2954** SQLite for parsing. ^These routines return 1 if the input string
2955** appears to be a complete SQL statement. ^A statement is judged to be
2956** complete if it ends with a semicolon token and is not a prefix of a
2957** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within
2958** string literals or quoted identifier names or comments are not
2959** independent tokens (they are part of the token in which they are
2960** embedded) and thus do not count as a statement terminator. ^Whitespace
2961** and comments that follow the final semicolon are ignored.
2962**
2963** ^These routines return 0 if the statement is incomplete. ^If a
2964** memory allocation fails, then SQLITE_NOMEM is returned.
2965**
2966** ^These routines do not parse the SQL statements thus
2967** will not detect syntactically incorrect SQL.
2968**
2969** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior
2970** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked
2971** automatically by sqlite3_complete16(). If that initialization fails,
2972** then the return value from sqlite3_complete16() will be non-zero
2973** regardless of whether or not the input SQL is complete.)^
2974**
2975** The input to [sqlite3_complete()] must be a zero-terminated
2976** UTF-8 string.
2977**
2978** The input to [sqlite3_complete16()] must be a zero-terminated
2979** UTF-16 string in native byte order.
2980*/
2981SQLITE_API int sqlite3_complete(const char *sql);
2982SQLITE_API int sqlite3_complete16(const void *sql);
2983
2984/*
2985** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
2986** KEYWORDS: {busy-handler callback} {busy handler}
2987** METHOD: sqlite3
2988**
2989** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X
2990** that might be invoked with argument P whenever
2991** an attempt is made to access a database table associated with
2992** [database connection] D when another thread
2993** or process has the table locked.
2994** The sqlite3_busy_handler() interface is used to implement
2995** [sqlite3_busy_timeout()] and [PRAGMA busy_timeout].
2996**
2997** ^If the busy callback is NULL, then [SQLITE_BUSY]
2998** is returned immediately upon encountering the lock. ^If the busy callback
2999** is not NULL, then the callback might be invoked with two arguments.
3000**
3001** ^The first argument to the busy handler is a copy of the void* pointer which
3002** is the third argument to sqlite3_busy_handler(). ^The second argument to
3003** the busy handler callback is the number of times that the busy handler has
3004** been invoked previously for the same locking event. ^If the
3005** busy callback returns 0, then no additional attempts are made to
3006** access the database and [SQLITE_BUSY] is returned
3007** to the application.
3008** ^If the callback returns non-zero, then another attempt
3009** is made to access the database and the cycle repeats.
3010**
3011** The presence of a busy handler does not guarantee that it will be invoked
3012** when there is lock contention. ^If SQLite determines that invoking the busy
3013** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
3014** to the application instead of invoking the
3015** busy handler.
3016** Consider a scenario where one process is holding a read lock that
3017** it is trying to promote to a reserved lock and
3018** a second process is holding a reserved lock that it is trying
3019** to promote to an exclusive lock. The first process cannot proceed
3020** because it is blocked by the second and the second process cannot
3021** proceed because it is blocked by the first. If both processes
3022** invoke the busy handlers, neither will make any progress. Therefore,
3023** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
3024** will induce the first process to release its read lock and allow
3025** the second process to proceed.
3026**
3027** ^The default busy callback is NULL.
3028**
3029** ^(There can only be a single busy handler defined for each
3030** [database connection]. Setting a new busy handler clears any
3031** previously set handler.)^ ^Note that calling [sqlite3_busy_timeout()]
3032** or evaluating [PRAGMA busy_timeout=N] will change the
3033** busy handler and thus clear any previously set busy handler.
3034**
3035** The busy callback should not take any actions which modify the
3036** database connection that invoked the busy handler. In other words,
3037** the busy handler is not reentrant. Any such actions
3038** result in undefined behavior.
3039**
3040** A busy handler must not close the database connection
3041** or [prepared statement] that invoked the busy handler.
3042*/
3043SQLITE_API int sqlite3_busy_handler(sqlite3*,int(*)(void*,int),void*);
3044
3045/*
3046** CAPI3REF: Set A Busy Timeout
3047** METHOD: sqlite3
3048**
3049** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
3050** for a specified amount of time when a table is locked. ^The handler
3051** will sleep multiple times until at least "ms" milliseconds of sleeping
3052** have accumulated. ^After at least "ms" milliseconds of sleeping,
3053** the handler returns 0 which causes [sqlite3_step()] to return
3054** [SQLITE_BUSY].
3055**
3056** ^Calling this routine with an argument less than or equal to zero
3057** turns off all busy handlers.
3058**
3059** ^(There can only be a single busy handler for a particular
3060** [database connection] at any given moment. If another busy handler
3061** was defined (using [sqlite3_busy_handler()]) prior to calling
3062** this routine, that other busy handler is cleared.)^
3063**
3064** See also: [PRAGMA busy_timeout]
3065*/
3066SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
3067
3068/*
3069** CAPI3REF: Convenience Routines For Running Queries
3070** METHOD: sqlite3
3071**
3072** This is a legacy interface that is preserved for backwards compatibility.
3073** Use of this interface is not recommended.
3074**
3075** Definition: A <b>result table</b> is memory data structure created by the
3076** [sqlite3_get_table()] interface. A result table records the
3077** complete query results from one or more queries.
3078**
3079** The table conceptually has a number of rows and columns. But
3080** these numbers are not part of the result table itself. These
3081** numbers are obtained separately. Let N be the number of rows
3082** and M be the number of columns.
3083**
3084** A result table is an array of pointers to zero-terminated UTF-8 strings.
3085** There are (N+1)*M elements in the array. The first M pointers point
3086** to zero-terminated strings that contain the names of the columns.
3087** The remaining entries all point to query results. NULL values result
3088** in NULL pointers. All other values are in their UTF-8 zero-terminated
3089** string representation as returned by [sqlite3_column_text()].
3090**
3091** A result table might consist of one or more memory allocations.
3092** It is not safe to pass a result table directly to [sqlite3_free()].
3093** A result table should be deallocated using [sqlite3_free_table()].
3094**
3095** ^(As an example of the result table format, suppose a query result
3096** is as follows:
3097**
3098** <blockquote><pre>
3099** Name | Age
3100** -----------------------
3101** Alice | 43
3102** Bob | 28
3103** Cindy | 21
3104** </pre></blockquote>
3105**
3106** There are two columns (M==2) and three rows (N==3). Thus the
3107** result table has 8 entries. Suppose the result table is stored
3108** in an array named azResult. Then azResult holds this content:
3109**
3110** <blockquote><pre>
3111** azResult&#91;0] = "Name";
3112** azResult&#91;1] = "Age";
3113** azResult&#91;2] = "Alice";
3114** azResult&#91;3] = "43";
3115** azResult&#91;4] = "Bob";
3116** azResult&#91;5] = "28";
3117** azResult&#91;6] = "Cindy";
3118** azResult&#91;7] = "21";
3119** </pre></blockquote>)^
3120**
3121** ^The sqlite3_get_table() function evaluates one or more
3122** semicolon-separated SQL statements in the zero-terminated UTF-8
3123** string of its 2nd parameter and returns a result table to the
3124** pointer given in its 3rd parameter.
3125**
3126** After the application has finished with the result from sqlite3_get_table(),
3127** it must pass the result table pointer to sqlite3_free_table() in order to
3128** release the memory that was malloced. Because of the way the
3129** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling
3130** function must not try to call [sqlite3_free()] directly. Only
3131** [sqlite3_free_table()] is able to release the memory properly and safely.
3132**
3133** The sqlite3_get_table() interface is implemented as a wrapper around
3134** [sqlite3_exec()]. The sqlite3_get_table() routine does not have access
3135** to any internal data structures of SQLite. It uses only the public
3136** interface defined here. As a consequence, errors that occur in the
3137** wrapper layer outside of the internal [sqlite3_exec()] call are not
3138** reflected in subsequent calls to [sqlite3_errcode()] or
3139** [sqlite3_errmsg()].
3140*/
3141SQLITE_API int sqlite3_get_table(
3142 sqlite3 *db, /* An open database */
3143 const char *zSql, /* SQL to be evaluated */
3144 char ***pazResult, /* Results of the query */
3145 int *pnRow, /* Number of result rows written here */
3146 int *pnColumn, /* Number of result columns written here */
3147 char **pzErrmsg /* Error msg written here */
3148);
3149SQLITE_API void sqlite3_free_table(char **result);
3150
3151/*
3152** CAPI3REF: Formatted String Printing Functions
3153**
3154** These routines are work-alikes of the "printf()" family of functions
3155** from the standard C library.
3156** These routines understand most of the common formatting options from
3157** the standard library printf()
3158** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
3159** See the [built-in printf()] documentation for details.
3160**
3161** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
3162** results into memory obtained from [sqlite3_malloc64()].
3163** The strings returned by these two routines should be
3164** released by [sqlite3_free()]. ^Both routines return a
3165** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough
3166** memory to hold the resulting string.
3167**
3168** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
3169** the standard C library. The result is written into the
3170** buffer supplied as the second parameter whose size is given by
3171** the first parameter. Note that the order of the
3172** first two parameters is reversed from snprintf().)^ This is an
3173** historical accident that cannot be fixed without breaking
3174** backwards compatibility. ^(Note also that sqlite3_snprintf()
3175** returns a pointer to its buffer instead of the number of
3176** characters actually written into the buffer.)^ We admit that
3177** the number of characters written would be a more useful return
3178** value but we cannot change the implementation of sqlite3_snprintf()
3179** now without breaking compatibility.
3180**
3181** ^As long as the buffer size is greater than zero, sqlite3_snprintf()
3182** guarantees that the buffer is always zero-terminated. ^The first
3183** parameter "n" is the total size of the buffer, including space for
3184** the zero terminator. So the longest string that can be completely
3185** written will be n-1 characters.
3186**
3187** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
3188**
3189** See also: [built-in printf()], [printf() SQL function]
3190*/
3191SQLITE_API char *sqlite3_mprintf(const char*,...);
3192SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
3193SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
3194SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
3195
3196/*
3197** CAPI3REF: Memory Allocation Subsystem
3198**
3199** The SQLite core uses these three routines for all of its own
3200** internal memory allocation needs. "Core" in the previous sentence
3201** does not include operating-system specific [VFS] implementation. The
3202** Windows VFS uses native malloc() and free() for some operations.
3203**
3204** ^The sqlite3_malloc() routine returns a pointer to a block
3205** of memory at least N bytes in length, where N is the parameter.
3206** ^If sqlite3_malloc() is unable to obtain sufficient free
3207** memory, it returns a NULL pointer. ^If the parameter N to
3208** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
3209** a NULL pointer.
3210**
3211** ^The sqlite3_malloc64(N) routine works just like
3212** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead
3213** of a signed 32-bit integer.
3214**
3215** ^Calling sqlite3_free() with a pointer previously returned
3216** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
3217** that it might be reused. ^The sqlite3_free() routine is
3218** a no-op if is called with a NULL pointer. Passing a NULL pointer
3219** to sqlite3_free() is harmless. After being freed, memory
3220** should neither be read nor written. Even reading previously freed
3221** memory might result in a segmentation fault or other severe error.
3222** Memory corruption, a segmentation fault, or other severe error
3223** might result if sqlite3_free() is called with a non-NULL pointer that
3224** was not obtained from sqlite3_malloc() or sqlite3_realloc().
3225**
3226** ^The sqlite3_realloc(X,N) interface attempts to resize a
3227** prior memory allocation X to be at least N bytes.
3228** ^If the X parameter to sqlite3_realloc(X,N)
3229** is a NULL pointer then its behavior is identical to calling
3230** sqlite3_malloc(N).
3231** ^If the N parameter to sqlite3_realloc(X,N) is zero or
3232** negative then the behavior is exactly the same as calling
3233** sqlite3_free(X).
3234** ^sqlite3_realloc(X,N) returns a pointer to a memory allocation
3235** of at least N bytes in size or NULL if insufficient memory is available.
3236** ^If M is the size of the prior allocation, then min(N,M) bytes
3237** of the prior allocation are copied into the beginning of buffer returned
3238** by sqlite3_realloc(X,N) and the prior allocation is freed.
3239** ^If sqlite3_realloc(X,N) returns NULL and N is positive, then the
3240** prior allocation is not freed.
3241**
3242** ^The sqlite3_realloc64(X,N) interfaces works the same as
3243** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead
3244** of a 32-bit signed integer.
3245**
3246** ^If X is a memory allocation previously obtained from sqlite3_malloc(),
3247** sqlite3_malloc64(), sqlite3_realloc(), or sqlite3_realloc64(), then
3248** sqlite3_msize(X) returns the size of that memory allocation in bytes.
3249** ^The value returned by sqlite3_msize(X) might be larger than the number
3250** of bytes requested when X was allocated. ^If X is a NULL pointer then
3251** sqlite3_msize(X) returns zero. If X points to something that is not
3252** the beginning of memory allocation, or if it points to a formerly
3253** valid memory allocation that has now been freed, then the behavior
3254** of sqlite3_msize(X) is undefined and possibly harmful.
3255**
3256** ^The memory returned by sqlite3_malloc(), sqlite3_realloc(),
3257** sqlite3_malloc64(), and sqlite3_realloc64()
3258** is always aligned to at least an 8 byte boundary, or to a
3259** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
3260** option is used.
3261**
3262** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()]
3263** must be either NULL or else pointers obtained from a prior
3264** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have
3265** not yet been released.
3266**
3267** The application must not read or write any part of
3268** a block of memory after it has been released using
3269** [sqlite3_free()] or [sqlite3_realloc()].
3270*/
3271SQLITE_API void *sqlite3_malloc(int);
3272SQLITE_API void *sqlite3_malloc64(sqlite3_uint64);
3273SQLITE_API void *sqlite3_realloc(void*, int);
3274SQLITE_API void *sqlite3_realloc64(void*, sqlite3_uint64);
3275SQLITE_API void sqlite3_free(void*);
3276SQLITE_API sqlite3_uint64 sqlite3_msize(void*);
3277
3278/*
3279** CAPI3REF: Memory Allocator Statistics
3280**
3281** SQLite provides these two interfaces for reporting on the status
3282** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
3283** routines, which form the built-in memory allocation subsystem.
3284**
3285** ^The [sqlite3_memory_used()] routine returns the number of bytes
3286** of memory currently outstanding (malloced but not freed).
3287** ^The [sqlite3_memory_highwater()] routine returns the maximum
3288** value of [sqlite3_memory_used()] since the high-water mark
3289** was last reset. ^The values returned by [sqlite3_memory_used()] and
3290** [sqlite3_memory_highwater()] include any overhead
3291** added by SQLite in its implementation of [sqlite3_malloc()],
3292** but not overhead added by the any underlying system library
3293** routines that [sqlite3_malloc()] may call.
3294**
3295** ^The memory high-water mark is reset to the current value of
3296** [sqlite3_memory_used()] if and only if the parameter to
3297** [sqlite3_memory_highwater()] is true. ^The value returned
3298** by [sqlite3_memory_highwater(1)] is the high-water mark
3299** prior to the reset.
3300*/
3301SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
3302SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
3303
3304/*
3305** CAPI3REF: Pseudo-Random Number Generator
3306**
3307** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
3308** select random [ROWID | ROWIDs] when inserting new records into a table that
3309** already uses the largest possible [ROWID]. The PRNG is also used for
3310** the built-in random() and randomblob() SQL functions. This interface allows
3311** applications to access the same PRNG for other purposes.
3312**
3313** ^A call to this routine stores N bytes of randomness into buffer P.
3314** ^The P parameter can be a NULL pointer.
3315**
3316** ^If this routine has not been previously called or if the previous
3317** call had N less than one or a NULL pointer for P, then the PRNG is
3318** seeded using randomness obtained from the xRandomness method of
3319** the default [sqlite3_vfs] object.
3320** ^If the previous call to this routine had an N of 1 or more and a
3321** non-NULL P then the pseudo-randomness is generated
3322** internally and without recourse to the [sqlite3_vfs] xRandomness
3323** method.
3324*/
3325SQLITE_API void sqlite3_randomness(int N, void *P);
3326
3327/*
3328** CAPI3REF: Compile-Time Authorization Callbacks
3329** METHOD: sqlite3
3330** KEYWORDS: {authorizer callback}
3331**
3332** ^This routine registers an authorizer callback with a particular
3333** [database connection], supplied in the first argument.
3334** ^The authorizer callback is invoked as SQL statements are being compiled
3335** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
3336** [sqlite3_prepare_v3()], [sqlite3_prepare16()], [sqlite3_prepare16_v2()],
3337** and [sqlite3_prepare16_v3()]. ^At various
3338** points during the compilation process, as logic is being created
3339** to perform various actions, the authorizer callback is invoked to
3340** see if those actions are allowed. ^The authorizer callback should
3341** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the
3342** specific action but allow the SQL statement to continue to be
3343** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
3344** rejected with an error. ^If the authorizer callback returns
3345** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY]
3346** then the [sqlite3_prepare_v2()] or equivalent call that triggered
3347** the authorizer will fail with an error message.
3348**
3349** When the callback returns [SQLITE_OK], that means the operation
3350** requested is ok. ^When the callback returns [SQLITE_DENY], the
3351** [sqlite3_prepare_v2()] or equivalent call that triggered the
3352** authorizer will fail with an error message explaining that
3353** access is denied.
3354**
3355** ^The first parameter to the authorizer callback is a copy of the third
3356** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
3357** to the callback is an integer [SQLITE_COPY | action code] that specifies
3358** the particular action to be authorized. ^The third through sixth parameters
3359** to the callback are either NULL pointers or zero-terminated strings
3360** that contain additional details about the action to be authorized.
3361** Applications must always be prepared to encounter a NULL pointer in any
3362** of the third through the sixth parameters of the authorization callback.
3363**
3364** ^If the action code is [SQLITE_READ]
3365** and the callback returns [SQLITE_IGNORE] then the
3366** [prepared statement] statement is constructed to substitute
3367** a NULL value in place of the table column that would have
3368** been read if [SQLITE_OK] had been returned. The [SQLITE_IGNORE]
3369** return can be used to deny an untrusted user access to individual
3370** columns of a table.
3371** ^When a table is referenced by a [SELECT] but no column values are
3372** extracted from that table (for example in a query like
3373** "SELECT count(*) FROM tab") then the [SQLITE_READ] authorizer callback
3374** is invoked once for that table with a column name that is an empty string.
3375** ^If the action code is [SQLITE_DELETE] and the callback returns
3376** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
3377** [truncate optimization] is disabled and all rows are deleted individually.
3378**
3379** An authorizer is used when [sqlite3_prepare | preparing]
3380** SQL statements from an untrusted source, to ensure that the SQL statements
3381** do not try to access data they are not allowed to see, or that they do not
3382** try to execute malicious statements that damage the database. For
3383** example, an application may allow a user to enter arbitrary
3384** SQL queries for evaluation by a database. But the application does
3385** not want the user to be able to make arbitrary changes to the
3386** database. An authorizer could then be put in place while the
3387** user-entered SQL is being [sqlite3_prepare | prepared] that
3388** disallows everything except [SELECT] statements.
3389**
3390** Applications that need to process SQL from untrusted sources
3391** might also consider lowering resource limits using [sqlite3_limit()]
3392** and limiting database size using the [max_page_count] [PRAGMA]
3393** in addition to using an authorizer.
3394**
3395** ^(Only a single authorizer can be in place on a database connection
3396** at a time. Each call to sqlite3_set_authorizer overrides the
3397** previous call.)^ ^Disable the authorizer by installing a NULL callback.
3398** The authorizer is disabled by default.
3399**
3400** The authorizer callback must not do anything that will modify
3401** the database connection that invoked the authorizer callback.
3402** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
3403** database connections for the meaning of "modify" in this paragraph.
3404**
3405** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the
3406** statement might be re-prepared during [sqlite3_step()] due to a
3407** schema change. Hence, the application should ensure that the
3408** correct authorizer callback remains in place during the [sqlite3_step()].
3409**
3410** ^Note that the authorizer callback is invoked only during
3411** [sqlite3_prepare()] or its variants. Authorization is not
3412** performed during statement evaluation in [sqlite3_step()], unless
3413** as stated in the previous paragraph, sqlite3_step() invokes
3414** sqlite3_prepare_v2() to reprepare a statement after a schema change.
3415*/
3416SQLITE_API int sqlite3_set_authorizer(
3417 sqlite3*,
3418 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
3419 void *pUserData
3420);
3421
3422/*
3423** CAPI3REF: Authorizer Return Codes
3424**
3425** The [sqlite3_set_authorizer | authorizer callback function] must
3426** return either [SQLITE_OK] or one of these two constants in order
3427** to signal SQLite whether or not the action is permitted. See the
3428** [sqlite3_set_authorizer | authorizer documentation] for additional
3429** information.
3430**
3431** Note that SQLITE_IGNORE is also used as a [conflict resolution mode]
3432** returned from the [sqlite3_vtab_on_conflict()] interface.
3433*/
3434#define SQLITE_DENY1 1 /* Abort the SQL statement with an error */
3435#define SQLITE_IGNORE2 2 /* Don't allow access, but don't generate an error */
3436
3437/*
3438** CAPI3REF: Authorizer Action Codes
3439**
3440** The [sqlite3_set_authorizer()] interface registers a callback function
3441** that is invoked to authorize certain SQL statement actions. The
3442** second parameter to the callback is an integer code that specifies
3443** what action is being authorized. These are the integer action codes that
3444** the authorizer callback may be passed.
3445**
3446** These action code values signify what kind of operation is to be
3447** authorized. The 3rd and 4th parameters to the authorization
3448** callback function will be parameters or NULL depending on which of these
3449** codes is used as the second parameter. ^(The 5th parameter to the
3450** authorizer callback is the name of the database ("main", "temp",
3451** etc.) if applicable.)^ ^The 6th parameter to the authorizer callback
3452** is the name of the inner-most trigger or view that is responsible for
3453** the access attempt or NULL if this access attempt is directly from
3454** top-level SQL code.
3455*/
3456/******************************************* 3rd ************ 4th ***********/
3457#define SQLITE_CREATE_INDEX1 1 /* Index Name Table Name */
3458#define SQLITE_CREATE_TABLE2 2 /* Table Name NULL */
3459#define SQLITE_CREATE_TEMP_INDEX3 3 /* Index Name Table Name */
3460#define SQLITE_CREATE_TEMP_TABLE4 4 /* Table Name NULL */
3461#define SQLITE_CREATE_TEMP_TRIGGER5 5 /* Trigger Name Table Name */
3462#define SQLITE_CREATE_TEMP_VIEW6 6 /* View Name NULL */
3463#define SQLITE_CREATE_TRIGGER7 7 /* Trigger Name Table Name */
3464#define SQLITE_CREATE_VIEW8 8 /* View Name NULL */
3465#define SQLITE_DELETE9 9 /* Table Name NULL */
3466#define SQLITE_DROP_INDEX10 10 /* Index Name Table Name */
3467#define SQLITE_DROP_TABLE11 11 /* Table Name NULL */
3468#define SQLITE_DROP_TEMP_INDEX12 12 /* Index Name Table Name */
3469#define SQLITE_DROP_TEMP_TABLE13 13 /* Table Name NULL */
3470#define SQLITE_DROP_TEMP_TRIGGER14 14 /* Trigger Name Table Name */
3471#define SQLITE_DROP_TEMP_VIEW15 15 /* View Name NULL */
3472#define SQLITE_DROP_TRIGGER16 16 /* Trigger Name Table Name */
3473#define SQLITE_DROP_VIEW17 17 /* View Name NULL */
3474#define SQLITE_INSERT18 18 /* Table Name NULL */
3475#define SQLITE_PRAGMA19 19 /* Pragma Name 1st arg or NULL */
3476#define SQLITE_READ20 20 /* Table Name Column Name */
3477#define SQLITE_SELECT21 21 /* NULL NULL */
3478#define SQLITE_TRANSACTION22 22 /* Operation NULL */
3479#define SQLITE_UPDATE23 23 /* Table Name Column Name */
3480#define SQLITE_ATTACH24 24 /* Filename NULL */
3481#define SQLITE_DETACH25 25 /* Database Name NULL */
3482#define SQLITE_ALTER_TABLE26 26 /* Database Name Table Name */
3483#define SQLITE_REINDEX27 27 /* Index Name NULL */
3484#define SQLITE_ANALYZE28 28 /* Table Name NULL */
3485#define SQLITE_CREATE_VTABLE29 29 /* Table Name Module Name */
3486#define SQLITE_DROP_VTABLE30 30 /* Table Name Module Name */
3487#define SQLITE_FUNCTION31 31 /* NULL Function Name */
3488#define SQLITE_SAVEPOINT32 32 /* Operation Savepoint Name */
3489#define SQLITE_COPY0 0 /* No longer used */
3490#define SQLITE_RECURSIVE33 33 /* NULL NULL */
3491
3492/*
3493** CAPI3REF: Tracing And Profiling Functions
3494** METHOD: sqlite3
3495**
3496** These routines are deprecated. Use the [sqlite3_trace_v2()] interface
3497** instead of the routines described here.
3498**
3499** These routines register callback functions that can be used for
3500** tracing and profiling the execution of SQL statements.
3501**
3502** ^The callback function registered by sqlite3_trace() is invoked at
3503** various times when an SQL statement is being run by [sqlite3_step()].
3504** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
3505** SQL statement text as the statement first begins executing.
3506** ^(Additional sqlite3_trace() callbacks might occur
3507** as each triggered subprogram is entered. The callbacks for triggers
3508** contain a UTF-8 SQL comment that identifies the trigger.)^
3509**
3510** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
3511** the length of [bound parameter] expansion in the output of sqlite3_trace().
3512**
3513** ^The callback function registered by sqlite3_profile() is invoked
3514** as each SQL statement finishes. ^The profile callback contains
3515** the original statement text and an estimate of wall-clock time
3516** of how long that statement took to run. ^The profile callback
3517** time is in units of nanoseconds, however the current implementation
3518** is only capable of millisecond resolution so the six least significant
3519** digits in the time are meaningless. Future versions of SQLite
3520** might provide greater resolution on the profiler callback. Invoking
3521** either [sqlite3_trace()] or [sqlite3_trace_v2()] will cancel the
3522** profile callback.
3523*/
3524SQLITE_API SQLITE_DEPRECATED void *sqlite3_trace(sqlite3*,
3525 void(*xTrace)(void*,const char*), void*);
3526SQLITE_API SQLITE_DEPRECATED void *sqlite3_profile(sqlite3*,
3527 void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
3528
3529/*
3530** CAPI3REF: SQL Trace Event Codes
3531** KEYWORDS: SQLITE_TRACE
3532**
3533** These constants identify classes of events that can be monitored
3534** using the [sqlite3_trace_v2()] tracing logic. The M argument
3535** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
3536** the following constants. ^The first argument to the trace callback
3537** is one of the following constants.
3538**
3539** New tracing constants may be added in future releases.
3540**
3541** ^A trace callback has four arguments: xCallback(T,C,P,X).
3542** ^The T argument is one of the integer type codes above.
3543** ^The C argument is a copy of the context pointer passed in as the
3544** fourth argument to [sqlite3_trace_v2()].
3545** The P and X arguments are pointers whose meanings depend on T.
3546**
3547** <dl>
3548** [[SQLITE_TRACE_STMT]] <dt>SQLITE_TRACE_STMT</dt>
3549** <dd>^An SQLITE_TRACE_STMT callback is invoked when a prepared statement
3550** first begins running and possibly at other times during the
3551** execution of the prepared statement, such as at the start of each
3552** trigger subprogram. ^The P argument is a pointer to the
3553** [prepared statement]. ^The X argument is a pointer to a string which
3554** is the unexpanded SQL text of the prepared statement or an SQL comment
3555** that indicates the invocation of a trigger. ^The callback can compute
3556** the same text that would have been returned by the legacy [sqlite3_trace()]
3557** interface by using the X argument when X begins with "--" and invoking
3558** [sqlite3_expanded_sql(P)] otherwise.
3559**
3560** [[SQLITE_TRACE_PROFILE]] <dt>SQLITE_TRACE_PROFILE</dt>
3561** <dd>^An SQLITE_TRACE_PROFILE callback provides approximately the same
3562** information as is provided by the [sqlite3_profile()] callback.
3563** ^The P argument is a pointer to the [prepared statement] and the
3564** X argument points to a 64-bit integer which is the estimated of
3565** the number of nanosecond that the prepared statement took to run.
3566** ^The SQLITE_TRACE_PROFILE callback is invoked when the statement finishes.
3567**
3568** [[SQLITE_TRACE_ROW]] <dt>SQLITE_TRACE_ROW</dt>
3569** <dd>^An SQLITE_TRACE_ROW callback is invoked whenever a prepared
3570** statement generates a single row of result.
3571** ^The P argument is a pointer to the [prepared statement] and the
3572** X argument is unused.
3573**
3574** [[SQLITE_TRACE_CLOSE]] <dt>SQLITE_TRACE_CLOSE</dt>
3575** <dd>^An SQLITE_TRACE_CLOSE callback is invoked when a database
3576** connection closes.
3577** ^The P argument is a pointer to the [database connection] object
3578** and the X argument is unused.
3579** </dl>
3580*/
3581#define SQLITE_TRACE_STMT0x01 0x01
3582#define SQLITE_TRACE_PROFILE0x02 0x02
3583#define SQLITE_TRACE_ROW0x04 0x04
3584#define SQLITE_TRACE_CLOSE0x08 0x08
3585
3586/*
3587** CAPI3REF: SQL Trace Hook
3588** METHOD: sqlite3
3589**
3590** ^The sqlite3_trace_v2(D,M,X,P) interface registers a trace callback
3591** function X against [database connection] D, using property mask M
3592** and context pointer P. ^If the X callback is
3593** NULL or if the M mask is zero, then tracing is disabled. The
3594** M argument should be the bitwise OR-ed combination of
3595** zero or more [SQLITE_TRACE] constants.
3596**
3597** ^Each call to either sqlite3_trace() or sqlite3_trace_v2() overrides
3598** (cancels) any prior calls to sqlite3_trace() or sqlite3_trace_v2().
3599**
3600** ^The X callback is invoked whenever any of the events identified by
3601** mask M occur. ^The integer return value from the callback is currently
3602** ignored, though this may change in future releases. Callback
3603** implementations should return zero to ensure future compatibility.
3604**
3605** ^A trace callback is invoked with four arguments: callback(T,C,P,X).
3606** ^The T argument is one of the [SQLITE_TRACE]
3607** constants to indicate why the callback was invoked.
3608** ^The C argument is a copy of the context pointer.
3609** The P and X arguments are pointers whose meanings depend on T.
3610**
3611** The sqlite3_trace_v2() interface is intended to replace the legacy
3612** interfaces [sqlite3_trace()] and [sqlite3_profile()], both of which
3613** are deprecated.
3614*/
3615SQLITE_API int sqlite3_trace_v2(
3616 sqlite3*,
3617 unsigned uMask,
3618 int(*xCallback)(unsigned,void*,void*,void*),
3619 void *pCtx
3620);
3621
3622/*
3623** CAPI3REF: Query Progress Callbacks
3624** METHOD: sqlite3
3625**
3626** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
3627** function X to be invoked periodically during long running calls to
3628** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for
3629** database connection D. An example use for this
3630** interface is to keep a GUI updated during a large query.
3631**
3632** ^The parameter P is passed through as the only parameter to the
3633** callback function X. ^The parameter N is the approximate number of
3634** [virtual machine instructions] that are evaluated between successive
3635** invocations of the callback X. ^If N is less than one then the progress
3636** handler is disabled.
3637**
3638** ^Only a single progress handler may be defined at one time per
3639** [database connection]; setting a new progress handler cancels the
3640** old one. ^Setting parameter X to NULL disables the progress handler.
3641** ^The progress handler is also disabled by setting N to a value less
3642** than 1.
3643**
3644** ^If the progress callback returns non-zero, the operation is
3645** interrupted. This feature can be used to implement a
3646** "Cancel" button on a GUI progress dialog box.
3647**
3648** The progress handler callback must not do anything that will modify
3649** the database connection that invoked the progress handler.
3650** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
3651** database connections for the meaning of "modify" in this paragraph.
3652**
3653*/
3654SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
3655
3656/*
3657** CAPI3REF: Opening A New Database Connection
3658** CONSTRUCTOR: sqlite3
3659**
3660** ^These routines open an SQLite database file as specified by the
3661** filename argument. ^The filename argument is interpreted as UTF-8 for
3662** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
3663** order for sqlite3_open16(). ^(A [database connection] handle is usually
3664** returned in *ppDb, even if an error occurs. The only exception is that
3665** if SQLite is unable to allocate memory to hold the [sqlite3] object,
3666** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
3667** object.)^ ^(If the database is opened (and/or created) successfully, then
3668** [SQLITE_OK] is returned. Otherwise an [error code] is returned.)^ ^The
3669** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
3670** an English language description of the error following a failure of any
3671** of the sqlite3_open() routines.
3672**
3673** ^The default encoding will be UTF-8 for databases created using
3674** sqlite3_open() or sqlite3_open_v2(). ^The default encoding for databases
3675** created using sqlite3_open16() will be UTF-16 in the native byte order.
3676**
3677** Whether or not an error occurs when it is opened, resources
3678** associated with the [database connection] handle should be released by
3679** passing it to [sqlite3_close()] when it is no longer required.
3680**
3681** The sqlite3_open_v2() interface works like sqlite3_open()
3682** except that it accepts two additional parameters for additional control
3683** over the new database connection. ^(The flags parameter to
3684** sqlite3_open_v2() must include, at a minimum, one of the following
3685** three flag combinations:)^
3686**
3687** <dl>
3688** ^(<dt>[SQLITE_OPEN_READONLY]</dt>
3689** <dd>The database is opened in read-only mode. If the database does not
3690** already exist, an error is returned.</dd>)^
3691**
3692** ^(<dt>[SQLITE_OPEN_READWRITE]</dt>
3693** <dd>The database is opened for reading and writing if possible, or reading
3694** only if the file is write protected by the operating system. In either
3695** case the database must already exist, otherwise an error is returned.</dd>)^
3696**
3697** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
3698** <dd>The database is opened for reading and writing, and is created if
3699** it does not already exist. This is the behavior that is always used for
3700** sqlite3_open() and sqlite3_open16().</dd>)^
3701** </dl>
3702**
3703** In addition to the required flags, the following optional flags are
3704** also supported:
3705**
3706** <dl>
3707** ^(<dt>[SQLITE_OPEN_URI]</dt>
3708** <dd>The filename can be interpreted as a URI if this flag is set.</dd>)^
3709**
3710** ^(<dt>[SQLITE_OPEN_MEMORY]</dt>
3711** <dd>The database will be opened as an in-memory database. The database
3712** is named by the "filename" argument for the purposes of cache-sharing,
3713** if shared cache mode is enabled, but the "filename" is otherwise ignored.
3714** </dd>)^
3715**
3716** ^(<dt>[SQLITE_OPEN_NOMUTEX]</dt>
3717** <dd>The new database connection will use the "multi-thread"
3718** [threading mode].)^ This means that separate threads are allowed
3719** to use SQLite at the same time, as long as each thread is using
3720** a different [database connection].
3721**
3722** ^(<dt>[SQLITE_OPEN_FULLMUTEX]</dt>
3723** <dd>The new database connection will use the "serialized"
3724** [threading mode].)^ This means the multiple threads can safely
3725** attempt to use the same database connection at the same time.
3726** (Mutexes will block any actual concurrency, but in this mode
3727** there is no harm in trying.)
3728**
3729** ^(<dt>[SQLITE_OPEN_SHAREDCACHE]</dt>
3730** <dd>The database is opened [shared cache] enabled, overriding
3731** the default shared cache setting provided by
3732** [sqlite3_enable_shared_cache()].)^
3733**
3734** ^(<dt>[SQLITE_OPEN_PRIVATECACHE]</dt>
3735** <dd>The database is opened [shared cache] disabled, overriding
3736** the default shared cache setting provided by
3737** [sqlite3_enable_shared_cache()].)^
3738**
3739** [[OPEN_EXRESCODE]] ^(<dt>[SQLITE_OPEN_EXRESCODE]</dt>
3740** <dd>The database connection comes up in "extended result code mode".
3741** In other words, the database behaves has if
3742** [sqlite3_extended_result_codes(db,1)] where called on the database
3743** connection as soon as the connection is created. In addition to setting
3744** the extended result code mode, this flag also causes [sqlite3_open_v2()]
3745** to return an extended result code.</dd>
3746**
3747** [[OPEN_NOFOLLOW]] ^(<dt>[SQLITE_OPEN_NOFOLLOW]</dt>
3748** <dd>The database filename is not allowed to be a symbolic link</dd>
3749** </dl>)^
3750**
3751** If the 3rd parameter to sqlite3_open_v2() is not one of the
3752** required combinations shown above optionally combined with other
3753** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
3754** then the behavior is undefined. Historic versions of SQLite
3755** have silently ignored surplus bits in the flags parameter to
3756** sqlite3_open_v2(), however that behavior might not be carried through
3757** into future versions of SQLite and so applications should not rely
3758** upon it. Note in particular that the SQLITE_OPEN_EXCLUSIVE flag is a no-op
3759** for sqlite3_open_v2(). The SQLITE_OPEN_EXCLUSIVE does *not* cause
3760** the open to fail if the database already exists. The SQLITE_OPEN_EXCLUSIVE
3761** flag is intended for use by the [sqlite3_vfs|VFS interface] only, and not
3762** by sqlite3_open_v2().
3763**
3764** ^The fourth parameter to sqlite3_open_v2() is the name of the
3765** [sqlite3_vfs] object that defines the operating system interface that
3766** the new database connection should use. ^If the fourth parameter is
3767** a NULL pointer then the default [sqlite3_vfs] object is used.
3768**
3769** ^If the filename is ":memory:", then a private, temporary in-memory database
3770** is created for the connection. ^This in-memory database will vanish when
3771** the database connection is closed. Future versions of SQLite might
3772** make use of additional special filenames that begin with the ":" character.
3773** It is recommended that when a database filename actually does begin with
3774** a ":" character you should prefix the filename with a pathname such as
3775** "./" to avoid ambiguity.
3776**
3777** ^If the filename is an empty string, then a private, temporary
3778** on-disk database will be created. ^This private database will be
3779** automatically deleted as soon as the database connection is closed.
3780**
3781** [[URI filenames in sqlite3_open()]] <h3>URI Filenames</h3>
3782**
3783** ^If [URI filename] interpretation is enabled, and the filename argument
3784** begins with "file:", then the filename is interpreted as a URI. ^URI
3785** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is
3786** set in the third argument to sqlite3_open_v2(), or if it has
3787** been enabled globally using the [SQLITE_CONFIG_URI] option with the
3788** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
3789** URI filename interpretation is turned off
3790** by default, but future releases of SQLite might enable URI filename
3791** interpretation by default. See "[URI filenames]" for additional
3792** information.
3793**
3794** URI filenames are parsed according to RFC 3986. ^If the URI contains an
3795** authority, then it must be either an empty string or the string
3796** "localhost". ^If the authority is not an empty string or "localhost", an
3797** error is returned to the caller. ^The fragment component of a URI, if
3798** present, is ignored.
3799**
3800** ^SQLite uses the path component of the URI as the name of the disk file
3801** which contains the database. ^If the path begins with a '/' character,
3802** then it is interpreted as an absolute path. ^If the path does not begin
3803** with a '/' (meaning that the authority section is omitted from the URI)
3804** then the path is interpreted as a relative path.
3805** ^(On windows, the first component of an absolute path
3806** is a drive specification (e.g. "C:").)^
3807**
3808** [[core URI query parameters]]
3809** The query component of a URI may contain parameters that are interpreted
3810** either by SQLite itself, or by a [VFS | custom VFS implementation].
3811** SQLite and its built-in [VFSes] interpret the
3812** following query parameters:
3813**
3814** <ul>
3815** <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
3816** a VFS object that provides the operating system interface that should
3817** be used to access the database file on disk. ^If this option is set to
3818** an empty string the default VFS object is used. ^Specifying an unknown
3819** VFS is an error. ^If sqlite3_open_v2() is used and the vfs option is
3820** present, then the VFS specified by the option takes precedence over
3821** the value passed as the fourth parameter to sqlite3_open_v2().
3822**
3823** <li> <b>mode</b>: ^(The mode parameter may be set to either "ro", "rw",
3824** "rwc", or "memory". Attempting to set it to any other value is
3825** an error)^.
3826** ^If "ro" is specified, then the database is opened for read-only
3827** access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the
3828** third argument to sqlite3_open_v2(). ^If the mode option is set to
3829** "rw", then the database is opened for read-write (but not create)
3830** access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had
3831** been set. ^Value "rwc" is equivalent to setting both
3832** SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE. ^If the mode option is
3833** set to "memory" then a pure [in-memory database] that never reads
3834** or writes from disk is used. ^It is an error to specify a value for
3835** the mode parameter that is less restrictive than that specified by
3836** the flags passed in the third parameter to sqlite3_open_v2().
3837**
3838** <li> <b>cache</b>: ^The cache parameter may be set to either "shared" or
3839** "private". ^Setting it to "shared" is equivalent to setting the
3840** SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to
3841** sqlite3_open_v2(). ^Setting the cache parameter to "private" is
3842** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
3843** ^If sqlite3_open_v2() is used and the "cache" parameter is present in
3844** a URI filename, its value overrides any behavior requested by setting
3845** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
3846**
3847** <li> <b>psow</b>: ^The psow parameter indicates whether or not the
3848** [powersafe overwrite] property does or does not apply to the
3849** storage media on which the database file resides.
3850**
3851** <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
3852** which if set disables file locking in rollback journal modes. This
3853** is useful for accessing a database on a filesystem that does not
3854** support locking. Caution: Database corruption might result if two
3855** or more processes write to the same database and any one of those
3856** processes uses nolock=1.
3857**
3858** <li> <b>immutable</b>: ^The immutable parameter is a boolean query
3859** parameter that indicates that the database file is stored on
3860** read-only media. ^When immutable is set, SQLite assumes that the
3861** database file cannot be changed, even by a process with higher
3862** privilege, and so the database is opened read-only and all locking
3863** and change detection is disabled. Caution: Setting the immutable
3864** property on a database file that does in fact change can result
3865** in incorrect query results and/or [SQLITE_CORRUPT] errors.
3866** See also: [SQLITE_IOCAP_IMMUTABLE].
3867**
3868** </ul>
3869**
3870** ^Specifying an unknown parameter in the query component of a URI is not an
3871** error. Future versions of SQLite might understand additional query
3872** parameters. See "[query parameters with special meaning to SQLite]" for
3873** additional information.
3874**
3875** [[URI filename examples]] <h3>URI filename examples</h3>
3876**
3877** <table border="1" align=center cellpadding=5>
3878** <tr><th> URI filenames <th> Results
3879** <tr><td> file:data.db <td>
3880** Open the file "data.db" in the current directory.
3881** <tr><td> file:/home/fred/data.db<br>
3882** file:///home/fred/data.db <br>
3883** file://localhost/home/fred/data.db <br> <td>
3884** Open the database file "/home/fred/data.db".
3885** <tr><td> file://darkstar/home/fred/data.db <td>
3886** An error. "darkstar" is not a recognized authority.
3887** <tr><td style="white-space:nowrap">
3888** file:///C:/Documents%20and%20Settings/fred/Desktop/data.db
3889** <td> Windows only: Open the file "data.db" on fred's desktop on drive
3890** C:. Note that the %20 escaping in this example is not strictly
3891** necessary - space characters can be used literally
3892** in URI filenames.
3893** <tr><td> file:data.db?mode=ro&cache=private <td>
3894** Open file "data.db" in the current directory for read-only access.
3895** Regardless of whether or not shared-cache mode is enabled by
3896** default, use a private cache.
3897** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
3898** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
3899** that uses dot-files in place of posix advisory locking.
3900** <tr><td> file:data.db?mode=readonly <td>
3901** An error. "readonly" is not a valid option for the "mode" parameter.
3902** Use "ro" instead: "file:data.db?mode=ro".
3903** </table>
3904**
3905** ^URI hexadecimal escape sequences (%HH) are supported within the path and
3906** query components of a URI. A hexadecimal escape sequence consists of a
3907** percent sign - "%" - followed by exactly two hexadecimal digits
3908** specifying an octet value. ^Before the path or query components of a
3909** URI filename are interpreted, they are encoded using UTF-8 and all
3910** hexadecimal escape sequences replaced by a single byte containing the
3911** corresponding octet. If this process generates an invalid UTF-8 encoding,
3912** the results are undefined.
3913**
3914** <b>Note to Windows users:</b> The encoding used for the filename argument
3915** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
3916** codepage is currently defined. Filenames containing international
3917** characters must be converted to UTF-8 prior to passing them into
3918** sqlite3_open() or sqlite3_open_v2().
3919**
3920** <b>Note to Windows Runtime users:</b> The temporary directory must be set
3921** prior to calling sqlite3_open() or sqlite3_open_v2(). Otherwise, various
3922** features that require the use of temporary files may fail.
3923**
3924** See also: [sqlite3_temp_directory]
3925*/
3926SQLITE_API int sqlite3_open(
3927 const char *filename, /* Database filename (UTF-8) */
3928 sqlite3 **ppDb /* OUT: SQLite db handle */
3929);
3930SQLITE_API int sqlite3_open16(
3931 const void *filename, /* Database filename (UTF-16) */
3932 sqlite3 **ppDb /* OUT: SQLite db handle */
3933);
3934SQLITE_API int sqlite3_open_v2(
3935 const char *filename, /* Database filename (UTF-8) */
3936 sqlite3 **ppDb, /* OUT: SQLite db handle */
3937 int flags, /* Flags */
3938 const char *zVfs /* Name of VFS module to use */
3939);
3940
3941/*
3942** CAPI3REF: Obtain Values For URI Parameters
3943**
3944** These are utility routines, useful to [VFS|custom VFS implementations],
3945** that check if a database file was a URI that contained a specific query
3946** parameter, and if so obtains the value of that query parameter.
3947**
3948** The first parameter to these interfaces (hereafter referred to
3949** as F) must be one of:
3950** <ul>
3951** <li> A database filename pointer created by the SQLite core and
3952** passed into the xOpen() method of a VFS implemention, or
3953** <li> A filename obtained from [sqlite3_db_filename()], or
3954** <li> A new filename constructed using [sqlite3_create_filename()].
3955** </ul>
3956** If the F parameter is not one of the above, then the behavior is
3957** undefined and probably undesirable. Older versions of SQLite were
3958** more tolerant of invalid F parameters than newer versions.
3959**
3960** If F is a suitable filename (as described in the previous paragraph)
3961** and if P is the name of the query parameter, then
3962** sqlite3_uri_parameter(F,P) returns the value of the P
3963** parameter if it exists or a NULL pointer if P does not appear as a
3964** query parameter on F. If P is a query parameter of F and it
3965** has no explicit value, then sqlite3_uri_parameter(F,P) returns
3966** a pointer to an empty string.
3967**
3968** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean
3969** parameter and returns true (1) or false (0) according to the value
3970** of P. The sqlite3_uri_boolean(F,P,B) routine returns true (1) if the
3971** value of query parameter P is one of "yes", "true", or "on" in any
3972** case or if the value begins with a non-zero number. The
3973** sqlite3_uri_boolean(F,P,B) routines returns false (0) if the value of
3974** query parameter P is one of "no", "false", or "off" in any case or
3975** if the value begins with a numeric zero. If P is not a query
3976** parameter on F or if the value of P does not match any of the
3977** above, then sqlite3_uri_boolean(F,P,B) returns (B!=0).
3978**
3979** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a
3980** 64-bit signed integer and returns that integer, or D if P does not
3981** exist. If the value of P is something other than an integer, then
3982** zero is returned.
3983**
3984** The sqlite3_uri_key(F,N) returns a pointer to the name (not
3985** the value) of the N-th query parameter for filename F, or a NULL
3986** pointer if N is less than zero or greater than the number of query
3987** parameters minus 1. The N value is zero-based so N should be 0 to obtain
3988** the name of the first query parameter, 1 for the second parameter, and
3989** so forth.
3990**
3991** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and
3992** sqlite3_uri_boolean(F,P,B) returns B. If F is not a NULL pointer and
3993** is not a database file pathname pointer that the SQLite core passed
3994** into the xOpen VFS method, then the behavior of this routine is undefined
3995** and probably undesirable.
3996**
3997** Beginning with SQLite [version 3.31.0] ([dateof:3.31.0]) the input F
3998** parameter can also be the name of a rollback journal file or WAL file
3999** in addition to the main database file. Prior to version 3.31.0, these
4000** routines would only work if F was the name of the main database file.
4001** When the F parameter is the name of the rollback journal or WAL file,
4002** it has access to all the same query parameters as were found on the
4003** main database file.
4004**
4005** See the [URI filename] documentation for additional information.
4006*/
4007SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
4008SQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
4009SQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
4010SQLITE_API const char *sqlite3_uri_key(const char *zFilename, int N);
4011
4012/*
4013** CAPI3REF: Translate filenames
4014**
4015** These routines are available to [VFS|custom VFS implementations] for
4016** translating filenames between the main database file, the journal file,
4017** and the WAL file.
4018**
4019** If F is the name of an sqlite database file, journal file, or WAL file
4020** passed by the SQLite core into the VFS, then sqlite3_filename_database(F)
4021** returns the name of the corresponding database file.
4022**
4023** If F is the name of an sqlite database file, journal file, or WAL file
4024** passed by the SQLite core into the VFS, or if F is a database filename
4025** obtained from [sqlite3_db_filename()], then sqlite3_filename_journal(F)
4026** returns the name of the corresponding rollback journal file.
4027**
4028** If F is the name of an sqlite database file, journal file, or WAL file
4029** that was passed by the SQLite core into the VFS, or if F is a database
4030** filename obtained from [sqlite3_db_filename()], then
4031** sqlite3_filename_wal(F) returns the name of the corresponding
4032** WAL file.
4033**
4034** In all of the above, if F is not the name of a database, journal or WAL
4035** filename passed into the VFS from the SQLite core and F is not the
4036** return value from [sqlite3_db_filename()], then the result is
4037** undefined and is likely a memory access violation.
4038*/
4039SQLITE_API const char *sqlite3_filename_database(const char*);
4040SQLITE_API const char *sqlite3_filename_journal(const char*);
4041SQLITE_API const char *sqlite3_filename_wal(const char*);
4042
4043/*
4044** CAPI3REF: Database File Corresponding To A Journal
4045**
4046** ^If X is the name of a rollback or WAL-mode journal file that is
4047** passed into the xOpen method of [sqlite3_vfs], then
4048** sqlite3_database_file_object(X) returns a pointer to the [sqlite3_file]
4049** object that represents the main database file.
4050**
4051** This routine is intended for use in custom [VFS] implementations
4052** only. It is not a general-purpose interface.
4053** The argument sqlite3_file_object(X) must be a filename pointer that
4054** has been passed into [sqlite3_vfs].xOpen method where the
4055** flags parameter to xOpen contains one of the bits
4056** [SQLITE_OPEN_MAIN_JOURNAL] or [SQLITE_OPEN_WAL]. Any other use
4057** of this routine results in undefined and probably undesirable
4058** behavior.
4059*/
4060SQLITE_API sqlite3_file *sqlite3_database_file_object(const char*);
4061
4062/*
4063** CAPI3REF: Create and Destroy VFS Filenames
4064**
4065** These interfces are provided for use by [VFS shim] implementations and
4066** are not useful outside of that context.
4067**
4068** The sqlite3_create_filename(D,J,W,N,P) allocates memory to hold a version of
4069** database filename D with corresponding journal file J and WAL file W and
4070** with N URI parameters key/values pairs in the array P. The result from
4071** sqlite3_create_filename(D,J,W,N,P) is a pointer to a database filename that
4072** is safe to pass to routines like:
4073** <ul>
4074** <li> [sqlite3_uri_parameter()],
4075** <li> [sqlite3_uri_boolean()],
4076** <li> [sqlite3_uri_int64()],
4077** <li> [sqlite3_uri_key()],
4078** <li> [sqlite3_filename_database()],
4079** <li> [sqlite3_filename_journal()], or
4080** <li> [sqlite3_filename_wal()].
4081** </ul>
4082** If a memory allocation error occurs, sqlite3_create_filename() might
4083** return a NULL pointer. The memory obtained from sqlite3_create_filename(X)
4084** must be released by a corresponding call to sqlite3_free_filename(Y).
4085**
4086** The P parameter in sqlite3_create_filename(D,J,W,N,P) should be an array
4087** of 2*N pointers to strings. Each pair of pointers in this array corresponds
4088** to a key and value for a query parameter. The P parameter may be a NULL
4089** pointer if N is zero. None of the 2*N pointers in the P array may be
4090** NULL pointers and key pointers should not be empty strings.
4091** None of the D, J, or W parameters to sqlite3_create_filename(D,J,W,N,P) may
4092** be NULL pointers, though they can be empty strings.
4093**
4094** The sqlite3_free_filename(Y) routine releases a memory allocation
4095** previously obtained from sqlite3_create_filename(). Invoking
4096** sqlite3_free_filename(Y) where Y is a NULL pointer is a harmless no-op.
4097**
4098** If the Y parameter to sqlite3_free_filename(Y) is anything other
4099** than a NULL pointer or a pointer previously acquired from
4100** sqlite3_create_filename(), then bad things such as heap
4101** corruption or segfaults may occur. The value Y should not be
4102** used again after sqlite3_free_filename(Y) has been called. This means
4103** that if the [sqlite3_vfs.xOpen()] method of a VFS has been called using Y,
4104** then the corresponding [sqlite3_module.xClose() method should also be
4105** invoked prior to calling sqlite3_free_filename(Y).
4106*/
4107SQLITE_API char *sqlite3_create_filename(
4108 const char *zDatabase,
4109 const char *zJournal,
4110 const char *zWal,
4111 int nParam,
4112 const char **azParam
4113);
4114SQLITE_API void sqlite3_free_filename(char*);
4115
4116/*
4117** CAPI3REF: Error Codes And Messages
4118** METHOD: sqlite3
4119**
4120** ^If the most recent sqlite3_* API call associated with
4121** [database connection] D failed, then the sqlite3_errcode(D) interface
4122** returns the numeric [result code] or [extended result code] for that
4123** API call.
4124** ^The sqlite3_extended_errcode()
4125** interface is the same except that it always returns the
4126** [extended result code] even when extended result codes are
4127** disabled.
4128**
4129** The values returned by sqlite3_errcode() and/or
4130** sqlite3_extended_errcode() might change with each API call.
4131** Except, there are some interfaces that are guaranteed to never
4132** change the value of the error code. The error-code preserving
4133** interfaces include the following:
4134**
4135** <ul>
4136** <li> sqlite3_errcode()
4137** <li> sqlite3_extended_errcode()
4138** <li> sqlite3_errmsg()
4139** <li> sqlite3_errmsg16()
4140** <li> sqlite3_error_offset()
4141** </ul>
4142**
4143** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
4144** text that describes the error, as either UTF-8 or UTF-16 respectively.
4145** ^(Memory to hold the error message string is managed internally.
4146** The application does not need to worry about freeing the result.
4147** However, the error string might be overwritten or deallocated by
4148** subsequent calls to other SQLite interface functions.)^
4149**
4150** ^The sqlite3_errstr() interface returns the English-language text
4151** that describes the [result code], as UTF-8.
4152** ^(Memory to hold the error message string is managed internally
4153** and must not be freed by the application)^.
4154**
4155** ^If the most recent error references a specific token in the input
4156** SQL, the sqlite3_error_offset() interface returns the byte offset
4157** of the start of that token. ^The byte offset returned by
4158** sqlite3_error_offset() assumes that the input SQL is UTF8.
4159** ^If the most recent error does not reference a specific token in the input
4160** SQL, then the sqlite3_error_offset() function returns -1.
4161**
4162** When the serialized [threading mode] is in use, it might be the
4163** case that a second error occurs on a separate thread in between
4164** the time of the first error and the call to these interfaces.
4165** When that happens, the second error will be reported since these
4166** interfaces always report the most recent result. To avoid
4167** this, each thread can obtain exclusive use of the [database connection] D
4168** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning
4169** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after
4170** all calls to the interfaces listed here are completed.
4171**
4172** If an interface fails with SQLITE_MISUSE, that means the interface
4173** was invoked incorrectly by the application. In that case, the
4174** error code and message may or may not be set.
4175*/
4176SQLITE_API int sqlite3_errcode(sqlite3 *db);
4177SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
4178SQLITE_API const char *sqlite3_errmsg(sqlite3*);
4179SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
4180SQLITE_API const char *sqlite3_errstr(int);
4181SQLITE_API int sqlite3_error_offset(sqlite3 *db);
4182
4183/*
4184** CAPI3REF: Prepared Statement Object
4185** KEYWORDS: {prepared statement} {prepared statements}
4186**
4187** An instance of this object represents a single SQL statement that
4188** has been compiled into binary form and is ready to be evaluated.
4189**
4190** Think of each SQL statement as a separate computer program. The
4191** original SQL text is source code. A prepared statement object
4192** is the compiled object code. All SQL must be converted into a
4193** prepared statement before it can be run.
4194**
4195** The life-cycle of a prepared statement object usually goes like this:
4196**
4197** <ol>
4198** <li> Create the prepared statement object using [sqlite3_prepare_v2()].
4199** <li> Bind values to [parameters] using the sqlite3_bind_*()
4200** interfaces.
4201** <li> Run the SQL by calling [sqlite3_step()] one or more times.
4202** <li> Reset the prepared statement using [sqlite3_reset()] then go back
4203** to step 2. Do this zero or more times.
4204** <li> Destroy the object using [sqlite3_finalize()].
4205** </ol>
4206*/
4207typedef struct sqlite3_stmt sqlite3_stmt;
4208
4209/*
4210** CAPI3REF: Run-time Limits
4211** METHOD: sqlite3
4212**
4213** ^(This interface allows the size of various constructs to be limited
4214** on a connection by connection basis. The first parameter is the
4215** [database connection] whose limit is to be set or queried. The
4216** second parameter is one of the [limit categories] that define a
4217** class of constructs to be size limited. The third parameter is the
4218** new limit for that construct.)^
4219**
4220** ^If the new limit is a negative number, the limit is unchanged.
4221** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a
4222** [limits | hard upper bound]
4223** set at compile-time by a C preprocessor macro called
4224** [limits | SQLITE_MAX_<i>NAME</i>].
4225** (The "_LIMIT_" in the name is changed to "_MAX_".))^
4226** ^Attempts to increase a limit above its hard upper bound are
4227** silently truncated to the hard upper bound.
4228**
4229** ^Regardless of whether or not the limit was changed, the
4230** [sqlite3_limit()] interface returns the prior value of the limit.
4231** ^Hence, to find the current value of a limit without changing it,
4232** simply invoke this interface with the third parameter set to -1.
4233**
4234** Run-time limits are intended for use in applications that manage
4235** both their own internal database and also databases that are controlled
4236** by untrusted external sources. An example application might be a
4237** web browser that has its own databases for storing history and
4238** separate databases controlled by JavaScript applications downloaded
4239** off the Internet. The internal databases can be given the
4240** large, default limits. Databases managed by external sources can
4241** be given much smaller limits designed to prevent a denial of service
4242** attack. Developers might also want to use the [sqlite3_set_authorizer()]
4243** interface to further control untrusted SQL. The size of the database
4244** created by an untrusted script can be contained using the
4245** [max_page_count] [PRAGMA].
4246**
4247** New run-time limit categories may be added in future releases.
4248*/
4249SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
4250
4251/*
4252** CAPI3REF: Run-Time Limit Categories
4253** KEYWORDS: {limit category} {*limit categories}
4254**
4255** These constants define various performance limits
4256** that can be lowered at run-time using [sqlite3_limit()].
4257** The synopsis of the meanings of the various limits is shown below.
4258** Additional information is available at [limits | Limits in SQLite].
4259**
4260** <dl>
4261** [[SQLITE_LIMIT_LENGTH]] ^(<dt>SQLITE_LIMIT_LENGTH</dt>
4262** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^
4263**
4264** [[SQLITE_LIMIT_SQL_LENGTH]] ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt>
4265** <dd>The maximum length of an SQL statement, in bytes.</dd>)^
4266**
4267** [[SQLITE_LIMIT_COLUMN]] ^(<dt>SQLITE_LIMIT_COLUMN</dt>
4268** <dd>The maximum number of columns in a table definition or in the
4269** result set of a [SELECT] or the maximum number of columns in an index
4270** or in an ORDER BY or GROUP BY clause.</dd>)^
4271**
4272** [[SQLITE_LIMIT_EXPR_DEPTH]] ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt>
4273** <dd>The maximum depth of the parse tree on any expression.</dd>)^
4274**
4275** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
4276** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
4277**
4278** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
4279** <dd>The maximum number of instructions in a virtual machine program
4280** used to implement an SQL statement. If [sqlite3_prepare_v2()] or
4281** the equivalent tries to allocate space for more than this many opcodes
4282** in a single prepared statement, an SQLITE_NOMEM error is returned.</dd>)^
4283**
4284** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
4285** <dd>The maximum number of arguments on a function.</dd>)^
4286**
4287** [[SQLITE_LIMIT_ATTACHED]] ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
4288** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
4289**
4290** [[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]]
4291** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt>
4292** <dd>The maximum length of the pattern argument to the [LIKE] or
4293** [GLOB] operators.</dd>)^
4294**
4295** [[SQLITE_LIMIT_VARIABLE_NUMBER]]
4296** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt>
4297** <dd>The maximum index number of any [parameter] in an SQL statement.)^
4298**
4299** [[SQLITE_LIMIT_TRIGGER_DEPTH]] ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt>
4300** <dd>The maximum depth of recursion for triggers.</dd>)^
4301**
4302** [[SQLITE_LIMIT_WORKER_THREADS]] ^(<dt>SQLITE_LIMIT_WORKER_THREADS</dt>
4303** <dd>The maximum number of auxiliary worker threads that a single
4304** [prepared statement] may start.</dd>)^
4305** </dl>
4306*/
4307#define SQLITE_LIMIT_LENGTH0 0
4308#define SQLITE_LIMIT_SQL_LENGTH1 1
4309#define SQLITE_LIMIT_COLUMN2 2
4310#define SQLITE_LIMIT_EXPR_DEPTH3 3
4311#define SQLITE_LIMIT_COMPOUND_SELECT4 4
4312#define SQLITE_LIMIT_VDBE_OP5 5
4313#define SQLITE_LIMIT_FUNCTION_ARG6 6
4314#define SQLITE_LIMIT_ATTACHED7 7
4315#define SQLITE_LIMIT_LIKE_PATTERN_LENGTH8 8
4316#define SQLITE_LIMIT_VARIABLE_NUMBER9 9
4317#define SQLITE_LIMIT_TRIGGER_DEPTH10 10
4318#define SQLITE_LIMIT_WORKER_THREADS11 11
4319
4320/*
4321** CAPI3REF: Prepare Flags
4322**
4323** These constants define various flags that can be passed into
4324** "prepFlags" parameter of the [sqlite3_prepare_v3()] and
4325** [sqlite3_prepare16_v3()] interfaces.
4326**
4327** New flags may be added in future releases of SQLite.
4328**
4329** <dl>
4330** [[SQLITE_PREPARE_PERSISTENT]] ^(<dt>SQLITE_PREPARE_PERSISTENT</dt>
4331** <dd>The SQLITE_PREPARE_PERSISTENT flag is a hint to the query planner
4332** that the prepared statement will be retained for a long time and
4333** probably reused many times.)^ ^Without this flag, [sqlite3_prepare_v3()]
4334** and [sqlite3_prepare16_v3()] assume that the prepared statement will
4335** be used just once or at most a few times and then destroyed using
4336** [sqlite3_finalize()] relatively soon. The current implementation acts
4337** on this hint by avoiding the use of [lookaside memory] so as not to
4338** deplete the limited store of lookaside memory. Future versions of
4339** SQLite may act on this hint differently.
4340**
4341** [[SQLITE_PREPARE_NORMALIZE]] <dt>SQLITE_PREPARE_NORMALIZE</dt>
4342** <dd>The SQLITE_PREPARE_NORMALIZE flag is a no-op. This flag used
4343** to be required for any prepared statement that wanted to use the
4344** [sqlite3_normalized_sql()] interface. However, the
4345** [sqlite3_normalized_sql()] interface is now available to all
4346** prepared statements, regardless of whether or not they use this
4347** flag.
4348**
4349** [[SQLITE_PREPARE_NO_VTAB]] <dt>SQLITE_PREPARE_NO_VTAB</dt>
4350** <dd>The SQLITE_PREPARE_NO_VTAB flag causes the SQL compiler
4351** to return an error (error code SQLITE_ERROR) if the statement uses
4352** any virtual tables.
4353** </dl>
4354*/
4355#define SQLITE_PREPARE_PERSISTENT0x01 0x01
4356#define SQLITE_PREPARE_NORMALIZE0x02 0x02
4357#define SQLITE_PREPARE_NO_VTAB0x04 0x04
4358
4359/*
4360** CAPI3REF: Compiling An SQL Statement
4361** KEYWORDS: {SQL statement compiler}
4362** METHOD: sqlite3
4363** CONSTRUCTOR: sqlite3_stmt
4364**
4365** To execute an SQL statement, it must first be compiled into a byte-code
4366** program using one of these routines. Or, in other words, these routines
4367** are constructors for the [prepared statement] object.
4368**
4369** The preferred routine to use is [sqlite3_prepare_v2()]. The
4370** [sqlite3_prepare()] interface is legacy and should be avoided.
4371** [sqlite3_prepare_v3()] has an extra "prepFlags" option that is used
4372** for special purposes.
4373**
4374** The use of the UTF-8 interfaces is preferred, as SQLite currently
4375** does all parsing using UTF-8. The UTF-16 interfaces are provided
4376** as a convenience. The UTF-16 interfaces work by converting the
4377** input text into UTF-8, then invoking the corresponding UTF-8 interface.
4378**
4379** The first argument, "db", is a [database connection] obtained from a
4380** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
4381** [sqlite3_open16()]. The database connection must not have been closed.
4382**
4383** The second argument, "zSql", is the statement to be compiled, encoded
4384** as either UTF-8 or UTF-16. The sqlite3_prepare(), sqlite3_prepare_v2(),
4385** and sqlite3_prepare_v3()
4386** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
4387** and sqlite3_prepare16_v3() use UTF-16.
4388**
4389** ^If the nByte argument is negative, then zSql is read up to the
4390** first zero terminator. ^If nByte is positive, then it is the
4391** number of bytes read from zSql. ^If nByte is zero, then no prepared
4392** statement is generated.
4393** If the caller knows that the supplied string is nul-terminated, then
4394** there is a small performance advantage to passing an nByte parameter that
4395** is the number of bytes in the input string <i>including</i>
4396** the nul-terminator.
4397**
4398** ^If pzTail is not NULL then *pzTail is made to point to the first byte
4399** past the end of the first SQL statement in zSql. These routines only
4400** compile the first statement in zSql, so *pzTail is left pointing to
4401** what remains uncompiled.
4402**
4403** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
4404** executed using [sqlite3_step()]. ^If there is an error, *ppStmt is set
4405** to NULL. ^If the input text contains no SQL (if the input is an empty
4406** string or a comment) then *ppStmt is set to NULL.
4407** The calling procedure is responsible for deleting the compiled
4408** SQL statement using [sqlite3_finalize()] after it has finished with it.
4409** ppStmt may not be NULL.
4410**
4411** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
4412** otherwise an [error code] is returned.
4413**
4414** The sqlite3_prepare_v2(), sqlite3_prepare_v3(), sqlite3_prepare16_v2(),
4415** and sqlite3_prepare16_v3() interfaces are recommended for all new programs.
4416** The older interfaces (sqlite3_prepare() and sqlite3_prepare16())
4417** are retained for backwards compatibility, but their use is discouraged.
4418** ^In the "vX" interfaces, the prepared statement
4419** that is returned (the [sqlite3_stmt] object) contains a copy of the
4420** original SQL text. This causes the [sqlite3_step()] interface to
4421** behave differently in three ways:
4422**
4423** <ol>
4424** <li>
4425** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
4426** always used to do, [sqlite3_step()] will automatically recompile the SQL
4427** statement and try to run it again. As many as [SQLITE_MAX_SCHEMA_RETRY]
4428** retries will occur before sqlite3_step() gives up and returns an error.
4429** </li>
4430**
4431** <li>
4432** ^When an error occurs, [sqlite3_step()] will return one of the detailed
4433** [error codes] or [extended error codes]. ^The legacy behavior was that
4434** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
4435** and the application would have to make a second call to [sqlite3_reset()]
4436** in order to find the underlying cause of the problem. With the "v2" prepare
4437** interfaces, the underlying reason for the error is returned immediately.
4438** </li>
4439**
4440** <li>
4441** ^If the specific value bound to a [parameter | host parameter] in the
4442** WHERE clause might influence the choice of query plan for a statement,
4443** then the statement will be automatically recompiled, as if there had been
4444** a schema change, on the first [sqlite3_step()] call following any change
4445** to the [sqlite3_bind_text | bindings] of that [parameter].
4446** ^The specific value of a WHERE-clause [parameter] might influence the
4447** choice of query plan if the parameter is the left-hand side of a [LIKE]
4448** or [GLOB] operator or if the parameter is compared to an indexed column
4449** and the [SQLITE_ENABLE_STAT4] compile-time option is enabled.
4450** </li>
4451** </ol>
4452**
4453** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
4454** the extra prepFlags parameter, which is a bit array consisting of zero or
4455** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags. ^The
4456** sqlite3_prepare_v2() interface works exactly the same as
4457** sqlite3_prepare_v3() with a zero prepFlags parameter.
4458*/
4459SQLITE_API int sqlite3_prepare(
4460 sqlite3 *db, /* Database handle */
4461 const char *zSql, /* SQL statement, UTF-8 encoded */
4462 int nByte, /* Maximum length of zSql in bytes. */
4463 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4464 const char **pzTail /* OUT: Pointer to unused portion of zSql */
4465);
4466SQLITE_API int sqlite3_prepare_v2(
4467 sqlite3 *db, /* Database handle */
4468 const char *zSql, /* SQL statement, UTF-8 encoded */
4469 int nByte, /* Maximum length of zSql in bytes. */
4470 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4471 const char **pzTail /* OUT: Pointer to unused portion of zSql */
4472);
4473SQLITE_API int sqlite3_prepare_v3(
4474 sqlite3 *db, /* Database handle */
4475 const char *zSql, /* SQL statement, UTF-8 encoded */
4476 int nByte, /* Maximum length of zSql in bytes. */
4477 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
4478 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4479 const char **pzTail /* OUT: Pointer to unused portion of zSql */
4480);
4481SQLITE_API int sqlite3_prepare16(
4482 sqlite3 *db, /* Database handle */
4483 const void *zSql, /* SQL statement, UTF-16 encoded */
4484 int nByte, /* Maximum length of zSql in bytes. */
4485 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4486 const void **pzTail /* OUT: Pointer to unused portion of zSql */
4487);
4488SQLITE_API int sqlite3_prepare16_v2(
4489 sqlite3 *db, /* Database handle */
4490 const void *zSql, /* SQL statement, UTF-16 encoded */
4491 int nByte, /* Maximum length of zSql in bytes. */
4492 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4493 const void **pzTail /* OUT: Pointer to unused portion of zSql */
4494);
4495SQLITE_API int sqlite3_prepare16_v3(
4496 sqlite3 *db, /* Database handle */
4497 const void *zSql, /* SQL statement, UTF-16 encoded */
4498 int nByte, /* Maximum length of zSql in bytes. */
4499 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
4500 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4501 const void **pzTail /* OUT: Pointer to unused portion of zSql */
4502);
4503
4504/*
4505** CAPI3REF: Retrieving Statement SQL
4506** METHOD: sqlite3_stmt
4507**
4508** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
4509** SQL text used to create [prepared statement] P if P was
4510** created by [sqlite3_prepare_v2()], [sqlite3_prepare_v3()],
4511** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
4512** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
4513** string containing the SQL text of prepared statement P with
4514** [bound parameters] expanded.
4515** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8
4516** string containing the normalized SQL text of prepared statement P. The
4517** semantics used to normalize a SQL statement are unspecified and subject
4518** to change. At a minimum, literal values will be replaced with suitable
4519** placeholders.
4520**
4521** ^(For example, if a prepared statement is created using the SQL
4522** text "SELECT $abc,:xyz" and if parameter $abc is bound to integer 2345
4523** and parameter :xyz is unbound, then sqlite3_sql() will return
4524** the original string, "SELECT $abc,:xyz" but sqlite3_expanded_sql()
4525** will return "SELECT 2345,NULL".)^
4526**
4527** ^The sqlite3_expanded_sql() interface returns NULL if insufficient memory
4528** is available to hold the result, or if the result would exceed the
4529** the maximum string length determined by the [SQLITE_LIMIT_LENGTH].
4530**
4531** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
4532** bound parameter expansions. ^The [SQLITE_OMIT_TRACE] compile-time
4533** option causes sqlite3_expanded_sql() to always return NULL.
4534**
4535** ^The strings returned by sqlite3_sql(P) and sqlite3_normalized_sql(P)
4536** are managed by SQLite and are automatically freed when the prepared
4537** statement is finalized.
4538** ^The string returned by sqlite3_expanded_sql(P), on the other hand,
4539** is obtained from [sqlite3_malloc()] and must be freed by the application
4540** by passing it to [sqlite3_free()].
4541**
4542** ^The sqlite3_normalized_sql() interface is only available if
4543** the [SQLITE_ENABLE_NORMALIZE] compile-time option is defined.
4544*/
4545SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
4546SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt);
4547#ifdef SQLITE_ENABLE_NORMALIZE
4548SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt);
4549#endif
4550
4551/*
4552** CAPI3REF: Determine If An SQL Statement Writes The Database
4553** METHOD: sqlite3_stmt
4554**
4555** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
4556** and only if the [prepared statement] X makes no direct changes to
4557** the content of the database file.
4558**
4559** Note that [application-defined SQL functions] or
4560** [virtual tables] might change the database indirectly as a side effect.
4561** ^(For example, if an application defines a function "eval()" that
4562** calls [sqlite3_exec()], then the following SQL statement would
4563** change the database file through side-effects:
4564**
4565** <blockquote><pre>
4566** SELECT eval('DELETE FROM t1') FROM t2;
4567** </pre></blockquote>
4568**
4569** But because the [SELECT] statement does not change the database file
4570** directly, sqlite3_stmt_readonly() would still return true.)^
4571**
4572** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK],
4573** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true,
4574** since the statements themselves do not actually modify the database but
4575** rather they control the timing of when other statements modify the
4576** database. ^The [ATTACH] and [DETACH] statements also cause
4577** sqlite3_stmt_readonly() to return true since, while those statements
4578** change the configuration of a database connection, they do not make
4579** changes to the content of the database files on disk.
4580** ^The sqlite3_stmt_readonly() interface returns true for [BEGIN] since
4581** [BEGIN] merely sets internal flags, but the [BEGIN|BEGIN IMMEDIATE] and
4582** [BEGIN|BEGIN EXCLUSIVE] commands do touch the database and so
4583** sqlite3_stmt_readonly() returns false for those commands.
4584**
4585** ^This routine returns false if there is any possibility that the
4586** statement might change the database file. ^A false return does
4587** not guarantee that the statement will change the database file.
4588** ^For example, an UPDATE statement might have a WHERE clause that
4589** makes it a no-op, but the sqlite3_stmt_readonly() result would still
4590** be false. ^Similarly, a CREATE TABLE IF NOT EXISTS statement is a
4591** read-only no-op if the table already exists, but
4592** sqlite3_stmt_readonly() still returns false for such a statement.
4593**
4594** ^If prepared statement X is an [EXPLAIN] or [EXPLAIN QUERY PLAN]
4595** statement, then sqlite3_stmt_readonly(X) returns the same value as
4596** if the EXPLAIN or EXPLAIN QUERY PLAN prefix were omitted.
4597*/
4598SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
4599
4600/*
4601** CAPI3REF: Query The EXPLAIN Setting For A Prepared Statement
4602** METHOD: sqlite3_stmt
4603**
4604** ^The sqlite3_stmt_isexplain(S) interface returns 1 if the
4605** prepared statement S is an EXPLAIN statement, or 2 if the
4606** statement S is an EXPLAIN QUERY PLAN.
4607** ^The sqlite3_stmt_isexplain(S) interface returns 0 if S is
4608** an ordinary statement or a NULL pointer.
4609*/
4610SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt);
4611
4612/*
4613** CAPI3REF: Determine If A Prepared Statement Has Been Reset
4614** METHOD: sqlite3_stmt
4615**
4616** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
4617** [prepared statement] S has been stepped at least once using
4618** [sqlite3_step(S)] but has neither run to completion (returned
4619** [SQLITE_DONE] from [sqlite3_step(S)]) nor
4620** been reset using [sqlite3_reset(S)]. ^The sqlite3_stmt_busy(S)
4621** interface returns false if S is a NULL pointer. If S is not a
4622** NULL pointer and is not a pointer to a valid [prepared statement]
4623** object, then the behavior is undefined and probably undesirable.
4624**
4625** This interface can be used in combination [sqlite3_next_stmt()]
4626** to locate all prepared statements associated with a database
4627** connection that are in need of being reset. This can be used,
4628** for example, in diagnostic routines to search for prepared
4629** statements that are holding a transaction open.
4630*/
4631SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*);
4632
4633/*
4634** CAPI3REF: Dynamically Typed Value Object
4635** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
4636**
4637** SQLite uses the sqlite3_value object to represent all values
4638** that can be stored in a database table. SQLite uses dynamic typing
4639** for the values it stores. ^Values stored in sqlite3_value objects
4640** can be integers, floating point values, strings, BLOBs, or NULL.
4641**
4642** An sqlite3_value object may be either "protected" or "unprotected".
4643** Some interfaces require a protected sqlite3_value. Other interfaces
4644** will accept either a protected or an unprotected sqlite3_value.
4645** Every interface that accepts sqlite3_value arguments specifies
4646** whether or not it requires a protected sqlite3_value. The
4647** [sqlite3_value_dup()] interface can be used to construct a new
4648** protected sqlite3_value from an unprotected sqlite3_value.
4649**
4650** The terms "protected" and "unprotected" refer to whether or not
4651** a mutex is held. An internal mutex is held for a protected
4652** sqlite3_value object but no mutex is held for an unprotected
4653** sqlite3_value object. If SQLite is compiled to be single-threaded
4654** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
4655** or if SQLite is run in one of reduced mutex modes
4656** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
4657** then there is no distinction between protected and unprotected
4658** sqlite3_value objects and they can be used interchangeably. However,
4659** for maximum code portability it is recommended that applications
4660** still make the distinction between protected and unprotected
4661** sqlite3_value objects even when not strictly required.
4662**
4663** ^The sqlite3_value objects that are passed as parameters into the
4664** implementation of [application-defined SQL functions] are protected.
4665** ^The sqlite3_value objects returned by [sqlite3_vtab_rhs_value()]
4666** are protected.
4667** ^The sqlite3_value object returned by
4668** [sqlite3_column_value()] is unprotected.
4669** Unprotected sqlite3_value objects may only be used as arguments
4670** to [sqlite3_result_value()], [sqlite3_bind_value()], and
4671** [sqlite3_value_dup()].
4672** The [sqlite3_value_blob | sqlite3_value_type()] family of
4673** interfaces require protected sqlite3_value objects.
4674*/
4675typedef struct sqlite3_value sqlite3_value;
4676
4677/*
4678** CAPI3REF: SQL Function Context Object
4679**
4680** The context in which an SQL function executes is stored in an
4681** sqlite3_context object. ^A pointer to an sqlite3_context object
4682** is always first parameter to [application-defined SQL functions].
4683** The application-defined SQL function implementation will pass this
4684** pointer through into calls to [sqlite3_result_int | sqlite3_result()],
4685** [sqlite3_aggregate_context()], [sqlite3_user_data()],
4686** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()],
4687** and/or [sqlite3_set_auxdata()].
4688*/
4689typedef struct sqlite3_context sqlite3_context;
4690
4691/*
4692** CAPI3REF: Binding Values To Prepared Statements
4693** KEYWORDS: {host parameter} {host parameters} {host parameter name}
4694** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
4695** METHOD: sqlite3_stmt
4696**
4697** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
4698** literals may be replaced by a [parameter] that matches one of following
4699** templates:
4700**
4701** <ul>
4702** <li> ?
4703** <li> ?NNN
4704** <li> :VVV
4705** <li> @VVV
4706** <li> $VVV
4707** </ul>
4708**
4709** In the templates above, NNN represents an integer literal,
4710** and VVV represents an alphanumeric identifier.)^ ^The values of these
4711** parameters (also called "host parameter names" or "SQL parameters")
4712** can be set using the sqlite3_bind_*() routines defined here.
4713**
4714** ^The first argument to the sqlite3_bind_*() routines is always
4715** a pointer to the [sqlite3_stmt] object returned from
4716** [sqlite3_prepare_v2()] or its variants.
4717**
4718** ^The second argument is the index of the SQL parameter to be set.
4719** ^The leftmost SQL parameter has an index of 1. ^When the same named
4720** SQL parameter is used more than once, second and subsequent
4721** occurrences have the same index as the first occurrence.
4722** ^The index for named parameters can be looked up using the
4723** [sqlite3_bind_parameter_index()] API if desired. ^The index
4724** for "?NNN" parameters is the value of NNN.
4725** ^The NNN value must be between 1 and the [sqlite3_limit()]
4726** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 32766).
4727**
4728** ^The third argument is the value to bind to the parameter.
4729** ^If the third parameter to sqlite3_bind_text() or sqlite3_bind_text16()
4730** or sqlite3_bind_blob() is a NULL pointer then the fourth parameter
4731** is ignored and the end result is the same as sqlite3_bind_null().
4732** ^If the third parameter to sqlite3_bind_text() is not NULL, then
4733** it should be a pointer to well-formed UTF8 text.
4734** ^If the third parameter to sqlite3_bind_text16() is not NULL, then
4735** it should be a pointer to well-formed UTF16 text.
4736** ^If the third parameter to sqlite3_bind_text64() is not NULL, then
4737** it should be a pointer to a well-formed unicode string that is
4738** either UTF8 if the sixth parameter is SQLITE_UTF8, or UTF16
4739** otherwise.
4740**
4741** [[byte-order determination rules]] ^The byte-order of
4742** UTF16 input text is determined by the byte-order mark (BOM, U+FEFF)
4743** found in first character, which is removed, or in the absence of a BOM
4744** the byte order is the native byte order of the host
4745** machine for sqlite3_bind_text16() or the byte order specified in
4746** the 6th parameter for sqlite3_bind_text64().)^
4747** ^If UTF16 input text contains invalid unicode
4748** characters, then SQLite might change those invalid characters
4749** into the unicode replacement character: U+FFFD.
4750**
4751** ^(In those routines that have a fourth argument, its value is the
4752** number of bytes in the parameter. To be clear: the value is the
4753** number of <u>bytes</u> in the value, not the number of characters.)^
4754** ^If the fourth parameter to sqlite3_bind_text() or sqlite3_bind_text16()
4755** is negative, then the length of the string is
4756** the number of bytes up to the first zero terminator.
4757** If the fourth parameter to sqlite3_bind_blob() is negative, then
4758** the behavior is undefined.
4759** If a non-negative fourth parameter is provided to sqlite3_bind_text()
4760** or sqlite3_bind_text16() or sqlite3_bind_text64() then
4761** that parameter must be the byte offset
4762** where the NUL terminator would occur assuming the string were NUL
4763** terminated. If any NUL characters occurs at byte offsets less than
4764** the value of the fourth parameter then the resulting string value will
4765** contain embedded NULs. The result of expressions involving strings
4766** with embedded NULs is undefined.
4767**
4768** ^The fifth argument to the BLOB and string binding interfaces controls
4769** or indicates the lifetime of the object referenced by the third parameter.
4770** These three options exist:
4771** ^ (1) A destructor to dispose of the BLOB or string after SQLite has finished
4772** with it may be passed. ^It is called to dispose of the BLOB or string even
4773** if the call to the bind API fails, except the destructor is not called if
4774** the third parameter is a NULL pointer or the fourth parameter is negative.
4775** ^ (2) The special constant, [SQLITE_STATIC], may be passsed to indicate that
4776** the application remains responsible for disposing of the object. ^In this
4777** case, the object and the provided pointer to it must remain valid until
4778** either the prepared statement is finalized or the same SQL parameter is
4779** bound to something else, whichever occurs sooner.
4780** ^ (3) The constant, [SQLITE_TRANSIENT], may be passed to indicate that the
4781** object is to be copied prior to the return from sqlite3_bind_*(). ^The
4782** object and pointer to it must remain valid until then. ^SQLite will then
4783** manage the lifetime of its private copy.
4784**
4785** ^The sixth argument to sqlite3_bind_text64() must be one of
4786** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]
4787** to specify the encoding of the text in the third parameter. If
4788** the sixth argument to sqlite3_bind_text64() is not one of the
4789** allowed values shown above, or if the text encoding is different
4790** from the encoding specified by the sixth parameter, then the behavior
4791** is undefined.
4792**
4793** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
4794** is filled with zeroes. ^A zeroblob uses a fixed amount of memory
4795** (just an integer to hold its size) while it is being processed.
4796** Zeroblobs are intended to serve as placeholders for BLOBs whose
4797** content is later written using
4798** [sqlite3_blob_open | incremental BLOB I/O] routines.
4799** ^A negative value for the zeroblob results in a zero-length BLOB.
4800**
4801** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in
4802** [prepared statement] S to have an SQL value of NULL, but to also be
4803** associated with the pointer P of type T. ^D is either a NULL pointer or
4804** a pointer to a destructor function for P. ^SQLite will invoke the
4805** destructor D with a single argument of P when it is finished using
4806** P. The T parameter should be a static string, preferably a string
4807** literal. The sqlite3_bind_pointer() routine is part of the
4808** [pointer passing interface] added for SQLite 3.20.0.
4809**
4810** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
4811** for the [prepared statement] or with a prepared statement for which
4812** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
4813** then the call will return [SQLITE_MISUSE]. If any sqlite3_bind_()
4814** routine is passed a [prepared statement] that has been finalized, the
4815** result is undefined and probably harmful.
4816**
4817** ^Bindings are not cleared by the [sqlite3_reset()] routine.
4818** ^Unbound parameters are interpreted as NULL.
4819**
4820** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
4821** [error code] if anything goes wrong.
4822** ^[SQLITE_TOOBIG] might be returned if the size of a string or BLOB
4823** exceeds limits imposed by [sqlite3_limit]([SQLITE_LIMIT_LENGTH]) or
4824** [SQLITE_MAX_LENGTH].
4825** ^[SQLITE_RANGE] is returned if the parameter
4826** index is out of range. ^[SQLITE_NOMEM] is returned if malloc() fails.
4827**
4828** See also: [sqlite3_bind_parameter_count()],
4829** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
4830*/
4831SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
4832SQLITE_API int sqlite3_bind_blob64(sqlite3_stmt*, int, const void*, sqlite3_uint64,
4833 void(*)(void*));
4834SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
4835SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
4836SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
4837SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
4838SQLITE_API int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
4839SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
4840SQLITE_API int sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64,
4841 void(*)(void*), unsigned char encoding);
4842SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
4843SQLITE_API int sqlite3_bind_pointer(sqlite3_stmt*, int, void*, const char*,void(*)(void*));
4844SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
4845SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt*, int, sqlite3_uint64);
4846
4847/*
4848** CAPI3REF: Number Of SQL Parameters
4849** METHOD: sqlite3_stmt
4850**
4851** ^This routine can be used to find the number of [SQL parameters]
4852** in a [prepared statement]. SQL parameters are tokens of the
4853** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
4854** placeholders for values that are [sqlite3_bind_blob | bound]
4855** to the parameters at a later time.
4856**
4857** ^(This routine actually returns the index of the largest (rightmost)
4858** parameter. For all forms except ?NNN, this will correspond to the
4859** number of unique parameters. If parameters of the ?NNN form are used,
4860** there may be gaps in the list.)^
4861**
4862** See also: [sqlite3_bind_blob|sqlite3_bind()],
4863** [sqlite3_bind_parameter_name()], and
4864** [sqlite3_bind_parameter_index()].
4865*/
4866SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
4867
4868/*
4869** CAPI3REF: Name Of A Host Parameter
4870** METHOD: sqlite3_stmt
4871**
4872** ^The sqlite3_bind_parameter_name(P,N) interface returns
4873** the name of the N-th [SQL parameter] in the [prepared statement] P.
4874** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
4875** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
4876** respectively.
4877** In other words, the initial ":" or "$" or "@" or "?"
4878** is included as part of the name.)^
4879** ^Parameters of the form "?" without a following integer have no name
4880** and are referred to as "nameless" or "anonymous parameters".
4881**
4882** ^The first host parameter has an index of 1, not 0.
4883**
4884** ^If the value N is out of range or if the N-th parameter is
4885** nameless, then NULL is returned. ^The returned string is
4886** always in UTF-8 encoding even if the named parameter was
4887** originally specified as UTF-16 in [sqlite3_prepare16()],
4888** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
4889**
4890** See also: [sqlite3_bind_blob|sqlite3_bind()],
4891** [sqlite3_bind_parameter_count()], and
4892** [sqlite3_bind_parameter_index()].
4893*/
4894SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
4895
4896/*
4897** CAPI3REF: Index Of A Parameter With A Given Name
4898** METHOD: sqlite3_stmt
4899**
4900** ^Return the index of an SQL parameter given its name. ^The
4901** index value returned is suitable for use as the second
4902** parameter to [sqlite3_bind_blob|sqlite3_bind()]. ^A zero
4903** is returned if no matching parameter is found. ^The parameter
4904** name must be given in UTF-8 even if the original statement
4905** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or
4906** [sqlite3_prepare16_v3()].
4907**
4908** See also: [sqlite3_bind_blob|sqlite3_bind()],
4909** [sqlite3_bind_parameter_count()], and
4910** [sqlite3_bind_parameter_name()].
4911*/
4912SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
4913
4914/*
4915** CAPI3REF: Reset All Bindings On A Prepared Statement
4916** METHOD: sqlite3_stmt
4917**
4918** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
4919** the [sqlite3_bind_blob | bindings] on a [prepared statement].
4920** ^Use this routine to reset all host parameters to NULL.
4921*/
4922SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
4923
4924/*
4925** CAPI3REF: Number Of Columns In A Result Set
4926** METHOD: sqlite3_stmt
4927**
4928** ^Return the number of columns in the result set returned by the
4929** [prepared statement]. ^If this routine returns 0, that means the
4930** [prepared statement] returns no data (for example an [UPDATE]).
4931** ^However, just because this routine returns a positive number does not
4932** mean that one or more rows of data will be returned. ^A SELECT statement
4933** will always have a positive sqlite3_column_count() but depending on the
4934** WHERE clause constraints and the table content, it might return no rows.
4935**
4936** See also: [sqlite3_data_count()]
4937*/
4938SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
4939
4940/*
4941** CAPI3REF: Column Names In A Result Set
4942** METHOD: sqlite3_stmt
4943**
4944** ^These routines return the name assigned to a particular column
4945** in the result set of a [SELECT] statement. ^The sqlite3_column_name()
4946** interface returns a pointer to a zero-terminated UTF-8 string
4947** and sqlite3_column_name16() returns a pointer to a zero-terminated
4948** UTF-16 string. ^The first parameter is the [prepared statement]
4949** that implements the [SELECT] statement. ^The second parameter is the
4950** column number. ^The leftmost column is number 0.
4951**
4952** ^The returned string pointer is valid until either the [prepared statement]
4953** is destroyed by [sqlite3_finalize()] or until the statement is automatically
4954** reprepared by the first call to [sqlite3_step()] for a particular run
4955** or until the next call to
4956** sqlite3_column_name() or sqlite3_column_name16() on the same column.
4957**
4958** ^If sqlite3_malloc() fails during the processing of either routine
4959** (for example during a conversion from UTF-8 to UTF-16) then a
4960** NULL pointer is returned.
4961**
4962** ^The name of a result column is the value of the "AS" clause for
4963** that column, if there is an AS clause. If there is no AS clause
4964** then the name of the column is unspecified and may change from
4965** one release of SQLite to the next.
4966*/
4967SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
4968SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
4969
4970/*
4971** CAPI3REF: Source Of Data In A Query Result
4972** METHOD: sqlite3_stmt
4973**
4974** ^These routines provide a means to determine the database, table, and
4975** table column that is the origin of a particular result column in
4976** [SELECT] statement.
4977** ^The name of the database or table or column can be returned as
4978** either a UTF-8 or UTF-16 string. ^The _database_ routines return
4979** the database name, the _table_ routines return the table name, and
4980** the origin_ routines return the column name.
4981** ^The returned string is valid until the [prepared statement] is destroyed
4982** using [sqlite3_finalize()] or until the statement is automatically
4983** reprepared by the first call to [sqlite3_step()] for a particular run
4984** or until the same information is requested
4985** again in a different encoding.
4986**
4987** ^The names returned are the original un-aliased names of the
4988** database, table, and column.
4989**
4990** ^The first argument to these interfaces is a [prepared statement].
4991** ^These functions return information about the Nth result column returned by
4992** the statement, where N is the second function argument.
4993** ^The left-most column is column 0 for these routines.
4994**
4995** ^If the Nth column returned by the statement is an expression or
4996** subquery and is not a column value, then all of these functions return
4997** NULL. ^These routines might also return NULL if a memory allocation error
4998** occurs. ^Otherwise, they return the name of the attached database, table,
4999** or column that query result column was extracted from.
5000**
5001** ^As with all other SQLite APIs, those whose names end with "16" return
5002** UTF-16 encoded strings and the other functions return UTF-8.
5003**
5004** ^These APIs are only available if the library was compiled with the
5005** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
5006**
5007** If two or more threads call one or more
5008** [sqlite3_column_database_name | column metadata interfaces]
5009** for the same [prepared statement] and result column
5010** at the same time then the results are undefined.
5011*/
5012SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
5013SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
5014SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
5015SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
5016SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
5017SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
5018
5019/*
5020** CAPI3REF: Declared Datatype Of A Query Result
5021** METHOD: sqlite3_stmt
5022**
5023** ^(The first parameter is a [prepared statement].
5024** If this statement is a [SELECT] statement and the Nth column of the
5025** returned result set of that [SELECT] is a table column (not an
5026** expression or subquery) then the declared type of the table
5027** column is returned.)^ ^If the Nth column of the result set is an
5028** expression or subquery, then a NULL pointer is returned.
5029** ^The returned string is always UTF-8 encoded.
5030**
5031** ^(For example, given the database schema:
5032**
5033** CREATE TABLE t1(c1 VARIANT);
5034**
5035** and the following statement to be compiled:
5036**
5037** SELECT c1 + 1, c1 FROM t1;
5038**
5039** this routine would return the string "VARIANT" for the second result
5040** column (i==1), and a NULL pointer for the first result column (i==0).)^
5041**
5042** ^SQLite uses dynamic run-time typing. ^So just because a column
5043** is declared to contain a particular type does not mean that the
5044** data stored in that column is of the declared type. SQLite is
5045** strongly typed, but the typing is dynamic not static. ^Type
5046** is associated with individual values, not with the containers
5047** used to hold those values.
5048*/
5049SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
5050SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
5051
5052/*
5053** CAPI3REF: Evaluate An SQL Statement
5054** METHOD: sqlite3_stmt
5055**
5056** After a [prepared statement] has been prepared using any of
5057** [sqlite3_prepare_v2()], [sqlite3_prepare_v3()], [sqlite3_prepare16_v2()],
5058** or [sqlite3_prepare16_v3()] or one of the legacy
5059** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
5060** must be called one or more times to evaluate the statement.
5061**
5062** The details of the behavior of the sqlite3_step() interface depend
5063** on whether the statement was prepared using the newer "vX" interfaces
5064** [sqlite3_prepare_v3()], [sqlite3_prepare_v2()], [sqlite3_prepare16_v3()],
5065** [sqlite3_prepare16_v2()] or the older legacy
5066** interfaces [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the
5067** new "vX" interface is recommended for new applications but the legacy
5068** interface will continue to be supported.
5069**
5070** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
5071** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
5072** ^With the "v2" interface, any of the other [result codes] or
5073** [extended result codes] might be returned as well.
5074**
5075** ^[SQLITE_BUSY] means that the database engine was unable to acquire the
5076** database locks it needs to do its job. ^If the statement is a [COMMIT]
5077** or occurs outside of an explicit transaction, then you can retry the
5078** statement. If the statement is not a [COMMIT] and occurs within an
5079** explicit transaction then you should rollback the transaction before
5080** continuing.
5081**
5082** ^[SQLITE_DONE] means that the statement has finished executing
5083** successfully. sqlite3_step() should not be called again on this virtual
5084** machine without first calling [sqlite3_reset()] to reset the virtual
5085** machine back to its initial state.
5086**
5087** ^If the SQL statement being executed returns any data, then [SQLITE_ROW]
5088** is returned each time a new row of data is ready for processing by the
5089** caller. The values may be accessed using the [column access functions].
5090** sqlite3_step() is called again to retrieve the next row of data.
5091**
5092** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
5093** violation) has occurred. sqlite3_step() should not be called again on
5094** the VM. More information may be found by calling [sqlite3_errmsg()].
5095** ^With the legacy interface, a more specific error code (for example,
5096** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
5097** can be obtained by calling [sqlite3_reset()] on the
5098** [prepared statement]. ^In the "v2" interface,
5099** the more specific error code is returned directly by sqlite3_step().
5100**
5101** [SQLITE_MISUSE] means that the this routine was called inappropriately.
5102** Perhaps it was called on a [prepared statement] that has
5103** already been [sqlite3_finalize | finalized] or on one that had
5104** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could
5105** be the case that the same database connection is being used by two or
5106** more threads at the same moment in time.
5107**
5108** For all versions of SQLite up to and including 3.6.23.1, a call to
5109** [sqlite3_reset()] was required after sqlite3_step() returned anything
5110** other than [SQLITE_ROW] before any subsequent invocation of
5111** sqlite3_step(). Failure to reset the prepared statement using
5112** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
5113** sqlite3_step(). But after [version 3.6.23.1] ([dateof:3.6.23.1],
5114** sqlite3_step() began
5115** calling [sqlite3_reset()] automatically in this circumstance rather
5116** than returning [SQLITE_MISUSE]. This is not considered a compatibility
5117** break because any application that ever receives an SQLITE_MISUSE error
5118** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
5119** can be used to restore the legacy behavior.
5120**
5121** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
5122** API always returns a generic error code, [SQLITE_ERROR], following any
5123** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call
5124** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
5125** specific [error codes] that better describes the error.
5126** We admit that this is a goofy design. The problem has been fixed
5127** with the "v2" interface. If you prepare all of your SQL statements
5128** using [sqlite3_prepare_v3()] or [sqlite3_prepare_v2()]
5129** or [sqlite3_prepare16_v2()] or [sqlite3_prepare16_v3()] instead
5130** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
5131** then the more specific [error codes] are returned directly
5132** by sqlite3_step(). The use of the "vX" interfaces is recommended.
5133*/
5134SQLITE_API int sqlite3_step(sqlite3_stmt*);
5135
5136/*
5137** CAPI3REF: Number of columns in a result set
5138** METHOD: sqlite3_stmt
5139**
5140** ^The sqlite3_data_count(P) interface returns the number of columns in the
5141** current row of the result set of [prepared statement] P.
5142** ^If prepared statement P does not have results ready to return
5143** (via calls to the [sqlite3_column_int | sqlite3_column()] family of
5144** interfaces) then sqlite3_data_count(P) returns 0.
5145** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
5146** ^The sqlite3_data_count(P) routine returns 0 if the previous call to
5147** [sqlite3_step](P) returned [SQLITE_DONE]. ^The sqlite3_data_count(P)
5148** will return non-zero if previous call to [sqlite3_step](P) returned
5149** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum]
5150** where it always returns zero since each step of that multi-step
5151** pragma returns 0 columns of data.
5152**
5153** See also: [sqlite3_column_count()]
5154*/
5155SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
5156
5157/*
5158** CAPI3REF: Fundamental Datatypes
5159** KEYWORDS: SQLITE_TEXT
5160**
5161** ^(Every value in SQLite has one of five fundamental datatypes:
5162**
5163** <ul>
5164** <li> 64-bit signed integer
5165** <li> 64-bit IEEE floating point number
5166** <li> string
5167** <li> BLOB
5168** <li> NULL
5169** </ul>)^
5170**
5171** These constants are codes for each of those types.
5172**
5173** Note that the SQLITE_TEXT constant was also used in SQLite version 2
5174** for a completely different meaning. Software that links against both
5175** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not
5176** SQLITE_TEXT.
5177*/
5178#define SQLITE_INTEGER1 1
5179#define SQLITE_FLOAT2 2
5180#define SQLITE_BLOB4 4
5181#define SQLITE_NULL5 5
5182#ifdef SQLITE_TEXT3
5183# undef SQLITE_TEXT3
5184#else
5185# define SQLITE_TEXT3 3
5186#endif
5187#define SQLITE3_TEXT3 3
5188
5189/*
5190** CAPI3REF: Result Values From A Query
5191** KEYWORDS: {column access functions}
5192** METHOD: sqlite3_stmt
5193**
5194** <b>Summary:</b>
5195** <blockquote><table border=0 cellpadding=0 cellspacing=0>
5196** <tr><td><b>sqlite3_column_blob</b><td>&rarr;<td>BLOB result
5197** <tr><td><b>sqlite3_column_double</b><td>&rarr;<td>REAL result
5198** <tr><td><b>sqlite3_column_int</b><td>&rarr;<td>32-bit INTEGER result
5199** <tr><td><b>sqlite3_column_int64</b><td>&rarr;<td>64-bit INTEGER result
5200** <tr><td><b>sqlite3_column_text</b><td>&rarr;<td>UTF-8 TEXT result
5201** <tr><td><b>sqlite3_column_text16</b><td>&rarr;<td>UTF-16 TEXT result
5202** <tr><td><b>sqlite3_column_value</b><td>&rarr;<td>The result as an
5203** [sqlite3_value|unprotected sqlite3_value] object.
5204** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
5205** <tr><td><b>sqlite3_column_bytes</b><td>&rarr;<td>Size of a BLOB
5206** or a UTF-8 TEXT result in bytes
5207** <tr><td><b>sqlite3_column_bytes16&nbsp;&nbsp;</b>
5208** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
5209** TEXT in bytes
5210** <tr><td><b>sqlite3_column_type</b><td>&rarr;<td>Default
5211** datatype of the result
5212** </table></blockquote>
5213**
5214** <b>Details:</b>
5215**
5216** ^These routines return information about a single column of the current
5217** result row of a query. ^In every case the first argument is a pointer
5218** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
5219** that was returned from [sqlite3_prepare_v2()] or one of its variants)
5220** and the second argument is the index of the column for which information
5221** should be returned. ^The leftmost column of the result set has the index 0.
5222** ^The number of columns in the result can be determined using
5223** [sqlite3_column_count()].
5224**
5225** If the SQL statement does not currently point to a valid row, or if the
5226** column index is out of range, the result is undefined.
5227** These routines may only be called when the most recent call to
5228** [sqlite3_step()] has returned [SQLITE_ROW] and neither
5229** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently.
5230** If any of these routines are called after [sqlite3_reset()] or
5231** [sqlite3_finalize()] or after [sqlite3_step()] has returned
5232** something other than [SQLITE_ROW], the results are undefined.
5233** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
5234** are called from a different thread while any of these routines
5235** are pending, then the results are undefined.
5236**
5237** The first six interfaces (_blob, _double, _int, _int64, _text, and _text16)
5238** each return the value of a result column in a specific data format. If
5239** the result column is not initially in the requested format (for example,
5240** if the query returns an integer but the sqlite3_column_text() interface
5241** is used to extract the value) then an automatic type conversion is performed.
5242**
5243** ^The sqlite3_column_type() routine returns the
5244** [SQLITE_INTEGER | datatype code] for the initial data type
5245** of the result column. ^The returned value is one of [SQLITE_INTEGER],
5246** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].
5247** The return value of sqlite3_column_type() can be used to decide which
5248** of the first six interface should be used to extract the column value.
5249** The value returned by sqlite3_column_type() is only meaningful if no
5250** automatic type conversions have occurred for the value in question.
5251** After a type conversion, the result of calling sqlite3_column_type()
5252** is undefined, though harmless. Future
5253** versions of SQLite may change the behavior of sqlite3_column_type()
5254** following a type conversion.
5255**
5256** If the result is a BLOB or a TEXT string, then the sqlite3_column_bytes()
5257** or sqlite3_column_bytes16() interfaces can be used to determine the size
5258** of that BLOB or string.
5259**
5260** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
5261** routine returns the number of bytes in that BLOB or string.
5262** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
5263** the string to UTF-8 and then returns the number of bytes.
5264** ^If the result is a numeric value then sqlite3_column_bytes() uses
5265** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
5266** the number of bytes in that string.
5267** ^If the result is NULL, then sqlite3_column_bytes() returns zero.
5268**
5269** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
5270** routine returns the number of bytes in that BLOB or string.
5271** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
5272** the string to UTF-16 and then returns the number of bytes.
5273** ^If the result is a numeric value then sqlite3_column_bytes16() uses
5274** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
5275** the number of bytes in that string.
5276** ^If the result is NULL, then sqlite3_column_bytes16() returns zero.
5277**
5278** ^The values returned by [sqlite3_column_bytes()] and
5279** [sqlite3_column_bytes16()] do not include the zero terminators at the end
5280** of the string. ^For clarity: the values returned by
5281** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
5282** bytes in the string, not the number of characters.
5283**
5284** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
5285** even empty strings, are always zero-terminated. ^The return
5286** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
5287**
5288** ^Strings returned by sqlite3_column_text16() always have the endianness
5289** which is native to the platform, regardless of the text encoding set
5290** for the database.
5291**
5292** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an
5293** [unprotected sqlite3_value] object. In a multithreaded environment,
5294** an unprotected sqlite3_value object may only be used safely with
5295** [sqlite3_bind_value()] and [sqlite3_result_value()].
5296** If the [unprotected sqlite3_value] object returned by
5297** [sqlite3_column_value()] is used in any other way, including calls
5298** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
5299** or [sqlite3_value_bytes()], the behavior is not threadsafe.
5300** Hence, the sqlite3_column_value() interface
5301** is normally only useful within the implementation of
5302** [application-defined SQL functions] or [virtual tables], not within
5303** top-level application code.
5304**
5305** These routines may attempt to convert the datatype of the result.
5306** ^For example, if the internal representation is FLOAT and a text result
5307** is requested, [sqlite3_snprintf()] is used internally to perform the
5308** conversion automatically. ^(The following table details the conversions
5309** that are applied:
5310**
5311** <blockquote>
5312** <table border="1">
5313** <tr><th> Internal<br>Type <th> Requested<br>Type <th> Conversion
5314**
5315** <tr><td> NULL <td> INTEGER <td> Result is 0
5316** <tr><td> NULL <td> FLOAT <td> Result is 0.0
5317** <tr><td> NULL <td> TEXT <td> Result is a NULL pointer
5318** <tr><td> NULL <td> BLOB <td> Result is a NULL pointer
5319** <tr><td> INTEGER <td> FLOAT <td> Convert from integer to float
5320** <tr><td> INTEGER <td> TEXT <td> ASCII rendering of the integer
5321** <tr><td> INTEGER <td> BLOB <td> Same as INTEGER->TEXT
5322** <tr><td> FLOAT <td> INTEGER <td> [CAST] to INTEGER
5323** <tr><td> FLOAT <td> TEXT <td> ASCII rendering of the float
5324** <tr><td> FLOAT <td> BLOB <td> [CAST] to BLOB
5325** <tr><td> TEXT <td> INTEGER <td> [CAST] to INTEGER
5326** <tr><td> TEXT <td> FLOAT <td> [CAST] to REAL
5327** <tr><td> TEXT <td> BLOB <td> No change
5328** <tr><td> BLOB <td> INTEGER <td> [CAST] to INTEGER
5329** <tr><td> BLOB <td> FLOAT <td> [CAST] to REAL
5330** <tr><td> BLOB <td> TEXT <td> [CAST] to TEXT, ensure zero terminator
5331** </table>
5332** </blockquote>)^
5333**
5334** Note that when type conversions occur, pointers returned by prior
5335** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
5336** sqlite3_column_text16() may be invalidated.
5337** Type conversions and pointer invalidations might occur
5338** in the following cases:
5339**
5340** <ul>
5341** <li> The initial content is a BLOB and sqlite3_column_text() or
5342** sqlite3_column_text16() is called. A zero-terminator might
5343** need to be added to the string.</li>
5344** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
5345** sqlite3_column_text16() is called. The content must be converted
5346** to UTF-16.</li>
5347** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
5348** sqlite3_column_text() is called. The content must be converted
5349** to UTF-8.</li>
5350** </ul>
5351**
5352** ^Conversions between UTF-16be and UTF-16le are always done in place and do
5353** not invalidate a prior pointer, though of course the content of the buffer
5354** that the prior pointer references will have been modified. Other kinds
5355** of conversion are done in place when it is possible, but sometimes they
5356** are not possible and in those cases prior pointers are invalidated.
5357**
5358** The safest policy is to invoke these routines
5359** in one of the following ways:
5360**
5361** <ul>
5362** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
5363** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
5364** <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
5365** </ul>
5366**
5367** In other words, you should call sqlite3_column_text(),
5368** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result
5369** into the desired format, then invoke sqlite3_column_bytes() or
5370** sqlite3_column_bytes16() to find the size of the result. Do not mix calls
5371** to sqlite3_column_text() or sqlite3_column_blob() with calls to
5372** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
5373** with calls to sqlite3_column_bytes().
5374**
5375** ^The pointers returned are valid until a type conversion occurs as
5376** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
5377** [sqlite3_finalize()] is called. ^The memory space used to hold strings
5378** and BLOBs is freed automatically. Do not pass the pointers returned
5379** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
5380** [sqlite3_free()].
5381**
5382** As long as the input parameters are correct, these routines will only
5383** fail if an out-of-memory error occurs during a format conversion.
5384** Only the following subset of interfaces are subject to out-of-memory
5385** errors:
5386**
5387** <ul>
5388** <li> sqlite3_column_blob()
5389** <li> sqlite3_column_text()
5390** <li> sqlite3_column_text16()
5391** <li> sqlite3_column_bytes()
5392** <li> sqlite3_column_bytes16()
5393** </ul>
5394**
5395** If an out-of-memory error occurs, then the return value from these
5396** routines is the same as if the column had contained an SQL NULL value.
5397** Valid SQL NULL returns can be distinguished from out-of-memory errors
5398** by invoking the [sqlite3_errcode()] immediately after the suspect
5399** return value is obtained and before any
5400** other SQLite interface is called on the same [database connection].
5401*/
5402SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
5403SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
5404SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
5405SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
5406SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
5407SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
5408SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
5409SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
5410SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
5411SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
5412
5413/*
5414** CAPI3REF: Destroy A Prepared Statement Object
5415** DESTRUCTOR: sqlite3_stmt
5416**
5417** ^The sqlite3_finalize() function is called to delete a [prepared statement].
5418** ^If the most recent evaluation of the statement encountered no errors
5419** or if the statement is never been evaluated, then sqlite3_finalize() returns
5420** SQLITE_OK. ^If the most recent evaluation of statement S failed, then
5421** sqlite3_finalize(S) returns the appropriate [error code] or
5422** [extended error code].
5423**
5424** ^The sqlite3_finalize(S) routine can be called at any point during
5425** the life cycle of [prepared statement] S:
5426** before statement S is ever evaluated, after
5427** one or more calls to [sqlite3_reset()], or after any call
5428** to [sqlite3_step()] regardless of whether or not the statement has
5429** completed execution.
5430**
5431** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
5432**
5433** The application must finalize every [prepared statement] in order to avoid
5434** resource leaks. It is a grievous error for the application to try to use
5435** a prepared statement after it has been finalized. Any use of a prepared
5436** statement after it has been finalized can result in undefined and
5437** undesirable behavior such as segfaults and heap corruption.
5438*/
5439SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
5440
5441/*
5442** CAPI3REF: Reset A Prepared Statement Object
5443** METHOD: sqlite3_stmt
5444**
5445** The sqlite3_reset() function is called to reset a [prepared statement]
5446** object back to its initial state, ready to be re-executed.
5447** ^Any SQL statement variables that had values bound to them using
5448** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
5449** Use [sqlite3_clear_bindings()] to reset the bindings.
5450**
5451** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
5452** back to the beginning of its program.
5453**
5454** ^If the most recent call to [sqlite3_step(S)] for the
5455** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE],
5456** or if [sqlite3_step(S)] has never before been called on S,
5457** then [sqlite3_reset(S)] returns [SQLITE_OK].
5458**
5459** ^If the most recent call to [sqlite3_step(S)] for the
5460** [prepared statement] S indicated an error, then
5461** [sqlite3_reset(S)] returns an appropriate [error code].
5462**
5463** ^The [sqlite3_reset(S)] interface does not change the values
5464** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
5465*/
5466SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
5467
5468/*
5469** CAPI3REF: Create Or Redefine SQL Functions
5470** KEYWORDS: {function creation routines}
5471** METHOD: sqlite3
5472**
5473** ^These functions (collectively known as "function creation routines")
5474** are used to add SQL functions or aggregates or to redefine the behavior
5475** of existing SQL functions or aggregates. The only differences between
5476** the three "sqlite3_create_function*" routines are the text encoding
5477** expected for the second parameter (the name of the function being
5478** created) and the presence or absence of a destructor callback for
5479** the application data pointer. Function sqlite3_create_window_function()
5480** is similar, but allows the user to supply the extra callback functions
5481** needed by [aggregate window functions].
5482**
5483** ^The first parameter is the [database connection] to which the SQL
5484** function is to be added. ^If an application uses more than one database
5485** connection then application-defined SQL functions must be added
5486** to each database connection separately.
5487**
5488** ^The second parameter is the name of the SQL function to be created or
5489** redefined. ^The length of the name is limited to 255 bytes in a UTF-8
5490** representation, exclusive of the zero-terminator. ^Note that the name
5491** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
5492** ^Any attempt to create a function with a longer name
5493** will result in [SQLITE_MISUSE] being returned.
5494**
5495** ^The third parameter (nArg)
5496** is the number of arguments that the SQL function or
5497** aggregate takes. ^If this parameter is -1, then the SQL function or
5498** aggregate may take any number of arguments between 0 and the limit
5499** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]). If the third
5500** parameter is less than -1 or greater than 127 then the behavior is
5501** undefined.
5502**
5503** ^The fourth parameter, eTextRep, specifies what
5504** [SQLITE_UTF8 | text encoding] this SQL function prefers for
5505** its parameters. The application should set this parameter to
5506** [SQLITE_UTF16LE] if the function implementation invokes
5507** [sqlite3_value_text16le()] on an input, or [SQLITE_UTF16BE] if the
5508** implementation invokes [sqlite3_value_text16be()] on an input, or
5509** [SQLITE_UTF16] if [sqlite3_value_text16()] is used, or [SQLITE_UTF8]
5510** otherwise. ^The same SQL function may be registered multiple times using
5511** different preferred text encodings, with different implementations for
5512** each encoding.
5513** ^When multiple implementations of the same function are available, SQLite
5514** will pick the one that involves the least amount of data conversion.
5515**
5516** ^The fourth parameter may optionally be ORed with [SQLITE_DETERMINISTIC]
5517** to signal that the function will always return the same result given
5518** the same inputs within a single SQL statement. Most SQL functions are
5519** deterministic. The built-in [random()] SQL function is an example of a
5520** function that is not deterministic. The SQLite query planner is able to
5521** perform additional optimizations on deterministic functions, so use
5522** of the [SQLITE_DETERMINISTIC] flag is recommended where possible.
5523**
5524** ^The fourth parameter may also optionally include the [SQLITE_DIRECTONLY]
5525** flag, which if present prevents the function from being invoked from
5526** within VIEWs, TRIGGERs, CHECK constraints, generated column expressions,
5527** index expressions, or the WHERE clause of partial indexes.
5528**
5529** For best security, the [SQLITE_DIRECTONLY] flag is recommended for
5530** all application-defined SQL functions that do not need to be
5531** used inside of triggers, view, CHECK constraints, or other elements of
5532** the database schema. This flags is especially recommended for SQL
5533** functions that have side effects or reveal internal application state.
5534** Without this flag, an attacker might be able to modify the schema of
5535** a database file to include invocations of the function with parameters
5536** chosen by the attacker, which the application will then execute when
5537** the database file is opened and read.
5538**
5539** ^(The fifth parameter is an arbitrary pointer. The implementation of the
5540** function can gain access to this pointer using [sqlite3_user_data()].)^
5541**
5542** ^The sixth, seventh and eighth parameters passed to the three
5543** "sqlite3_create_function*" functions, xFunc, xStep and xFinal, are
5544** pointers to C-language functions that implement the SQL function or
5545** aggregate. ^A scalar SQL function requires an implementation of the xFunc
5546** callback only; NULL pointers must be passed as the xStep and xFinal
5547** parameters. ^An aggregate SQL function requires an implementation of xStep
5548** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
5549** SQL function or aggregate, pass NULL pointers for all three function
5550** callbacks.
5551**
5552** ^The sixth, seventh, eighth and ninth parameters (xStep, xFinal, xValue
5553** and xInverse) passed to sqlite3_create_window_function are pointers to
5554** C-language callbacks that implement the new function. xStep and xFinal
5555** must both be non-NULL. xValue and xInverse may either both be NULL, in
5556** which case a regular aggregate function is created, or must both be
5557** non-NULL, in which case the new function may be used as either an aggregate
5558** or aggregate window function. More details regarding the implementation
5559** of aggregate window functions are
5560** [user-defined window functions|available here].
5561**
5562** ^(If the final parameter to sqlite3_create_function_v2() or
5563** sqlite3_create_window_function() is not NULL, then it is destructor for
5564** the application data pointer. The destructor is invoked when the function
5565** is deleted, either by being overloaded or when the database connection
5566** closes.)^ ^The destructor is also invoked if the call to
5567** sqlite3_create_function_v2() fails. ^When the destructor callback is
5568** invoked, it is passed a single argument which is a copy of the application
5569** data pointer which was the fifth parameter to sqlite3_create_function_v2().
5570**
5571** ^It is permitted to register multiple implementations of the same
5572** functions with the same name but with either differing numbers of
5573** arguments or differing preferred text encodings. ^SQLite will use
5574** the implementation that most closely matches the way in which the
5575** SQL function is used. ^A function implementation with a non-negative
5576** nArg parameter is a better match than a function implementation with
5577** a negative nArg. ^A function where the preferred text encoding
5578** matches the database encoding is a better
5579** match than a function where the encoding is different.
5580** ^A function where the encoding difference is between UTF16le and UTF16be
5581** is a closer match than a function where the encoding difference is
5582** between UTF8 and UTF16.
5583**
5584** ^Built-in functions may be overloaded by new application-defined functions.
5585**
5586** ^An application-defined function is permitted to call other
5587** SQLite interfaces. However, such calls must not
5588** close the database connection nor finalize or reset the prepared
5589** statement in which the function is running.
5590*/
5591SQLITE_API int sqlite3_create_function(
5592 sqlite3 *db,
5593 const char *zFunctionName,
5594 int nArg,
5595 int eTextRep,
5596 void *pApp,
5597 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5598 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5599 void (*xFinal)(sqlite3_context*)
5600);
5601SQLITE_API int sqlite3_create_function16(
5602 sqlite3 *db,
5603 const void *zFunctionName,
5604 int nArg,
5605 int eTextRep,
5606 void *pApp,
5607 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5608 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5609 void (*xFinal)(sqlite3_context*)
5610);
5611SQLITE_API int sqlite3_create_function_v2(
5612 sqlite3 *db,
5613 const char *zFunctionName,
5614 int nArg,
5615 int eTextRep,
5616 void *pApp,
5617 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5618 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5619 void (*xFinal)(sqlite3_context*),
5620 void(*xDestroy)(void*)
5621);
5622SQLITE_API int sqlite3_create_window_function(
5623 sqlite3 *db,
5624 const char *zFunctionName,
5625 int nArg,
5626 int eTextRep,
5627 void *pApp,
5628 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5629 void (*xFinal)(sqlite3_context*),
5630 void (*xValue)(sqlite3_context*),
5631 void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
5632 void(*xDestroy)(void*)
5633);
5634
5635/*
5636** CAPI3REF: Text Encodings
5637**
5638** These constant define integer codes that represent the various
5639** text encodings supported by SQLite.
5640*/
5641#define SQLITE_UTF81 1 /* IMP: R-37514-35566 */
5642#define SQLITE_UTF16LE2 2 /* IMP: R-03371-37637 */
5643#define SQLITE_UTF16BE3 3 /* IMP: R-51971-34154 */
5644#define SQLITE_UTF164 4 /* Use native byte order */
5645#define SQLITE_ANY5 5 /* Deprecated */
5646#define SQLITE_UTF16_ALIGNED8 8 /* sqlite3_create_collation only */
5647
5648/*
5649** CAPI3REF: Function Flags
5650**
5651** These constants may be ORed together with the
5652** [SQLITE_UTF8 | preferred text encoding] as the fourth argument
5653** to [sqlite3_create_function()], [sqlite3_create_function16()], or
5654** [sqlite3_create_function_v2()].
5655**
5656** <dl>
5657** [[SQLITE_DETERMINISTIC]] <dt>SQLITE_DETERMINISTIC</dt><dd>
5658** The SQLITE_DETERMINISTIC flag means that the new function always gives
5659** the same output when the input parameters are the same.
5660** The [abs|abs() function] is deterministic, for example, but
5661** [randomblob|randomblob()] is not. Functions must
5662** be deterministic in order to be used in certain contexts such as
5663** with the WHERE clause of [partial indexes] or in [generated columns].
5664** SQLite might also optimize deterministic functions by factoring them
5665** out of inner loops.
5666** </dd>
5667**
5668** [[SQLITE_DIRECTONLY]] <dt>SQLITE_DIRECTONLY</dt><dd>
5669** The SQLITE_DIRECTONLY flag means that the function may only be invoked
5670** from top-level SQL, and cannot be used in VIEWs or TRIGGERs nor in
5671** schema structures such as [CHECK constraints], [DEFAULT clauses],
5672** [expression indexes], [partial indexes], or [generated columns].
5673** The SQLITE_DIRECTONLY flags is a security feature which is recommended
5674** for all [application-defined SQL functions], and especially for functions
5675** that have side-effects or that could potentially leak sensitive
5676** information.
5677** </dd>
5678**
5679** [[SQLITE_INNOCUOUS]] <dt>SQLITE_INNOCUOUS</dt><dd>
5680** The SQLITE_INNOCUOUS flag means that the function is unlikely
5681** to cause problems even if misused. An innocuous function should have
5682** no side effects and should not depend on any values other than its
5683** input parameters. The [abs|abs() function] is an example of an
5684** innocuous function.
5685** The [load_extension() SQL function] is not innocuous because of its
5686** side effects.
5687** <p> SQLITE_INNOCUOUS is similar to SQLITE_DETERMINISTIC, but is not
5688** exactly the same. The [random|random() function] is an example of a
5689** function that is innocuous but not deterministic.
5690** <p>Some heightened security settings
5691** ([SQLITE_DBCONFIG_TRUSTED_SCHEMA] and [PRAGMA trusted_schema=OFF])
5692** disable the use of SQL functions inside views and triggers and in
5693** schema structures such as [CHECK constraints], [DEFAULT clauses],
5694** [expression indexes], [partial indexes], and [generated columns] unless
5695** the function is tagged with SQLITE_INNOCUOUS. Most built-in functions
5696** are innocuous. Developers are advised to avoid using the
5697** SQLITE_INNOCUOUS flag for application-defined functions unless the
5698** function has been carefully audited and found to be free of potentially
5699** security-adverse side-effects and information-leaks.
5700** </dd>
5701**
5702** [[SQLITE_SUBTYPE]] <dt>SQLITE_SUBTYPE</dt><dd>
5703** The SQLITE_SUBTYPE flag indicates to SQLite that a function may call
5704** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
5705** Specifying this flag makes no difference for scalar or aggregate user
5706** functions. However, if it is not specified for a user-defined window
5707** function, then any sub-types belonging to arguments passed to the window
5708** function may be discarded before the window function is called (i.e.
5709** sqlite3_value_subtype() will always return 0).
5710** </dd>
5711** </dl>
5712*/
5713#define SQLITE_DETERMINISTIC0x000000800 0x000000800
5714#define SQLITE_DIRECTONLY0x000080000 0x000080000
5715#define SQLITE_SUBTYPE0x000100000 0x000100000
5716#define SQLITE_INNOCUOUS0x000200000 0x000200000
5717
5718/*
5719** CAPI3REF: Deprecated Functions
5720** DEPRECATED
5721**
5722** These functions are [deprecated]. In order to maintain
5723** backwards compatibility with older code, these functions continue
5724** to be supported. However, new applications should avoid
5725** the use of these functions. To encourage programmers to avoid
5726** these functions, we will not explain what they do.
5727*/
5728#ifndef SQLITE_OMIT_DEPRECATED1
5729SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
5730SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
5731SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
5732SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
5733SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
5734SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
5735 void*,sqlite3_int64);
5736#endif
5737
5738/*
5739** CAPI3REF: Obtaining SQL Values
5740** METHOD: sqlite3_value
5741**
5742** <b>Summary:</b>
5743** <blockquote><table border=0 cellpadding=0 cellspacing=0>
5744** <tr><td><b>sqlite3_value_blob</b><td>&rarr;<td>BLOB value
5745** <tr><td><b>sqlite3_value_double</b><td>&rarr;<td>REAL value
5746** <tr><td><b>sqlite3_value_int</b><td>&rarr;<td>32-bit INTEGER value
5747** <tr><td><b>sqlite3_value_int64</b><td>&rarr;<td>64-bit INTEGER value
5748** <tr><td><b>sqlite3_value_pointer</b><td>&rarr;<td>Pointer value
5749** <tr><td><b>sqlite3_value_text</b><td>&rarr;<td>UTF-8 TEXT value
5750** <tr><td><b>sqlite3_value_text16</b><td>&rarr;<td>UTF-16 TEXT value in
5751** the native byteorder
5752** <tr><td><b>sqlite3_value_text16be</b><td>&rarr;<td>UTF-16be TEXT value
5753** <tr><td><b>sqlite3_value_text16le</b><td>&rarr;<td>UTF-16le TEXT value
5754** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
5755** <tr><td><b>sqlite3_value_bytes</b><td>&rarr;<td>Size of a BLOB
5756** or a UTF-8 TEXT in bytes
5757** <tr><td><b>sqlite3_value_bytes16&nbsp;&nbsp;</b>
5758** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
5759** TEXT in bytes
5760** <tr><td><b>sqlite3_value_type</b><td>&rarr;<td>Default
5761** datatype of the value
5762** <tr><td><b>sqlite3_value_numeric_type&nbsp;&nbsp;</b>
5763** <td>&rarr;&nbsp;&nbsp;<td>Best numeric datatype of the value
5764** <tr><td><b>sqlite3_value_nochange&nbsp;&nbsp;</b>
5765** <td>&rarr;&nbsp;&nbsp;<td>True if the column is unchanged in an UPDATE
5766** against a virtual table.
5767** <tr><td><b>sqlite3_value_frombind&nbsp;&nbsp;</b>
5768** <td>&rarr;&nbsp;&nbsp;<td>True if value originated from a [bound parameter]
5769** </table></blockquote>
5770**
5771** <b>Details:</b>
5772**
5773** These routines extract type, size, and content information from
5774** [protected sqlite3_value] objects. Protected sqlite3_value objects
5775** are used to pass parameter information into the functions that
5776** implement [application-defined SQL functions] and [virtual tables].
5777**
5778** These routines work only with [protected sqlite3_value] objects.
5779** Any attempt to use these routines on an [unprotected sqlite3_value]
5780** is not threadsafe.
5781**
5782** ^These routines work just like the corresponding [column access functions]
5783** except that these routines take a single [protected sqlite3_value] object
5784** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
5785**
5786** ^The sqlite3_value_text16() interface extracts a UTF-16 string
5787** in the native byte-order of the host machine. ^The
5788** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
5789** extract UTF-16 strings as big-endian and little-endian respectively.
5790**
5791** ^If [sqlite3_value] object V was initialized
5792** using [sqlite3_bind_pointer(S,I,P,X,D)] or [sqlite3_result_pointer(C,P,X,D)]
5793** and if X and Y are strings that compare equal according to strcmp(X,Y),
5794** then sqlite3_value_pointer(V,Y) will return the pointer P. ^Otherwise,
5795** sqlite3_value_pointer(V,Y) returns a NULL. The sqlite3_bind_pointer()
5796** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
5797**
5798** ^(The sqlite3_value_type(V) interface returns the
5799** [SQLITE_INTEGER | datatype code] for the initial datatype of the
5800** [sqlite3_value] object V. The returned value is one of [SQLITE_INTEGER],
5801** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].)^
5802** Other interfaces might change the datatype for an sqlite3_value object.
5803** For example, if the datatype is initially SQLITE_INTEGER and
5804** sqlite3_value_text(V) is called to extract a text value for that
5805** integer, then subsequent calls to sqlite3_value_type(V) might return
5806** SQLITE_TEXT. Whether or not a persistent internal datatype conversion
5807** occurs is undefined and may change from one release of SQLite to the next.
5808**
5809** ^(The sqlite3_value_numeric_type() interface attempts to apply
5810** numeric affinity to the value. This means that an attempt is
5811** made to convert the value to an integer or floating point. If
5812** such a conversion is possible without loss of information (in other
5813** words, if the value is a string that looks like a number)
5814** then the conversion is performed. Otherwise no conversion occurs.
5815** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
5816**
5817** ^Within the [xUpdate] method of a [virtual table], the
5818** sqlite3_value_nochange(X) interface returns true if and only if
5819** the column corresponding to X is unchanged by the UPDATE operation
5820** that the xUpdate method call was invoked to implement and if
5821** and the prior [xColumn] method call that was invoked to extracted
5822** the value for that column returned without setting a result (probably
5823** because it queried [sqlite3_vtab_nochange()] and found that the column
5824** was unchanging). ^Within an [xUpdate] method, any value for which
5825** sqlite3_value_nochange(X) is true will in all other respects appear
5826** to be a NULL value. If sqlite3_value_nochange(X) is invoked anywhere other
5827** than within an [xUpdate] method call for an UPDATE statement, then
5828** the return value is arbitrary and meaningless.
5829**
5830** ^The sqlite3_value_frombind(X) interface returns non-zero if the
5831** value X originated from one of the [sqlite3_bind_int|sqlite3_bind()]
5832** interfaces. ^If X comes from an SQL literal value, or a table column,
5833** or an expression, then sqlite3_value_frombind(X) returns zero.
5834**
5835** Please pay particular attention to the fact that the pointer returned
5836** from [sqlite3_value_blob()], [sqlite3_value_text()], or
5837** [sqlite3_value_text16()] can be invalidated by a subsequent call to
5838** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
5839** or [sqlite3_value_text16()].
5840**
5841** These routines must be called from the same thread as
5842** the SQL function that supplied the [sqlite3_value*] parameters.
5843**
5844** As long as the input parameter is correct, these routines can only
5845** fail if an out-of-memory error occurs during a format conversion.
5846** Only the following subset of interfaces are subject to out-of-memory
5847** errors:
5848**
5849** <ul>
5850** <li> sqlite3_value_blob()
5851** <li> sqlite3_value_text()
5852** <li> sqlite3_value_text16()
5853** <li> sqlite3_value_text16le()
5854** <li> sqlite3_value_text16be()
5855** <li> sqlite3_value_bytes()
5856** <li> sqlite3_value_bytes16()
5857** </ul>
5858**
5859** If an out-of-memory error occurs, then the return value from these
5860** routines is the same as if the column had contained an SQL NULL value.
5861** Valid SQL NULL returns can be distinguished from out-of-memory errors
5862** by invoking the [sqlite3_errcode()] immediately after the suspect
5863** return value is obtained and before any
5864** other SQLite interface is called on the same [database connection].
5865*/
5866SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
5867SQLITE_API double sqlite3_value_double(sqlite3_value*);
5868SQLITE_API int sqlite3_value_int(sqlite3_value*);
5869SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
5870SQLITE_API void *sqlite3_value_pointer(sqlite3_value*, const char*);
5871SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
5872SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
5873SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
5874SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
5875SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
5876SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
5877SQLITE_API int sqlite3_value_type(sqlite3_value*);
5878SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
5879SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
5880SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
5881
5882/*
5883** CAPI3REF: Finding The Subtype Of SQL Values
5884** METHOD: sqlite3_value
5885**
5886** The sqlite3_value_subtype(V) function returns the subtype for
5887** an [application-defined SQL function] argument V. The subtype
5888** information can be used to pass a limited amount of context from
5889** one SQL function to another. Use the [sqlite3_result_subtype()]
5890** routine to set the subtype for the return value of an SQL function.
5891*/
5892SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
5893
5894/*
5895** CAPI3REF: Copy And Free SQL Values
5896** METHOD: sqlite3_value
5897**
5898** ^The sqlite3_value_dup(V) interface makes a copy of the [sqlite3_value]
5899** object D and returns a pointer to that copy. ^The [sqlite3_value] returned
5900** is a [protected sqlite3_value] object even if the input is not.
5901** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a
5902** memory allocation fails. ^If V is a [pointer value], then the result
5903** of sqlite3_value_dup(V) is a NULL value.
5904**
5905** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object
5906** previously obtained from [sqlite3_value_dup()]. ^If V is a NULL pointer
5907** then sqlite3_value_free(V) is a harmless no-op.
5908*/
5909SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value*);
5910SQLITE_API void sqlite3_value_free(sqlite3_value*);
5911
5912/*
5913** CAPI3REF: Obtain Aggregate Function Context
5914** METHOD: sqlite3_context
5915**
5916** Implementations of aggregate SQL functions use this
5917** routine to allocate memory for storing their state.
5918**
5919** ^The first time the sqlite3_aggregate_context(C,N) routine is called
5920** for a particular aggregate function, SQLite allocates
5921** N bytes of memory, zeroes out that memory, and returns a pointer
5922** to the new memory. ^On second and subsequent calls to
5923** sqlite3_aggregate_context() for the same aggregate function instance,
5924** the same buffer is returned. Sqlite3_aggregate_context() is normally
5925** called once for each invocation of the xStep callback and then one
5926** last time when the xFinal callback is invoked. ^(When no rows match
5927** an aggregate query, the xStep() callback of the aggregate function
5928** implementation is never called and xFinal() is called exactly once.
5929** In those cases, sqlite3_aggregate_context() might be called for the
5930** first time from within xFinal().)^
5931**
5932** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer
5933** when first called if N is less than or equal to zero or if a memory
5934** allocate error occurs.
5935**
5936** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
5937** determined by the N parameter on first successful call. Changing the
5938** value of N in any subsequent call to sqlite3_aggregate_context() within
5939** the same aggregate function instance will not resize the memory
5940** allocation.)^ Within the xFinal callback, it is customary to set
5941** N=0 in calls to sqlite3_aggregate_context(C,N) so that no
5942** pointless memory allocations occur.
5943**
5944** ^SQLite automatically frees the memory allocated by
5945** sqlite3_aggregate_context() when the aggregate query concludes.
5946**
5947** The first parameter must be a copy of the
5948** [sqlite3_context | SQL function context] that is the first parameter
5949** to the xStep or xFinal callback routine that implements the aggregate
5950** function.
5951**
5952** This routine must be called from the same thread in which
5953** the aggregate SQL function is running.
5954*/
5955SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
5956
5957/*
5958** CAPI3REF: User Data For Functions
5959** METHOD: sqlite3_context
5960**
5961** ^The sqlite3_user_data() interface returns a copy of
5962** the pointer that was the pUserData parameter (the 5th parameter)
5963** of the [sqlite3_create_function()]
5964** and [sqlite3_create_function16()] routines that originally
5965** registered the application defined function.
5966**
5967** This routine must be called from the same thread in which
5968** the application-defined function is running.
5969*/
5970SQLITE_API void *sqlite3_user_data(sqlite3_context*);
5971
5972/*
5973** CAPI3REF: Database Connection For Functions
5974** METHOD: sqlite3_context
5975**
5976** ^The sqlite3_context_db_handle() interface returns a copy of
5977** the pointer to the [database connection] (the 1st parameter)
5978** of the [sqlite3_create_function()]
5979** and [sqlite3_create_function16()] routines that originally
5980** registered the application defined function.
5981*/
5982SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
5983
5984/*
5985** CAPI3REF: Function Auxiliary Data
5986** METHOD: sqlite3_context
5987**
5988** These functions may be used by (non-aggregate) SQL functions to
5989** associate metadata with argument values. If the same value is passed to
5990** multiple invocations of the same SQL function during query execution, under
5991** some circumstances the associated metadata may be preserved. An example
5992** of where this might be useful is in a regular-expression matching
5993** function. The compiled version of the regular expression can be stored as
5994** metadata associated with the pattern string.
5995** Then as long as the pattern string remains the same,
5996** the compiled regular expression can be reused on multiple
5997** invocations of the same function.
5998**
5999** ^The sqlite3_get_auxdata(C,N) interface returns a pointer to the metadata
6000** associated by the sqlite3_set_auxdata(C,N,P,X) function with the Nth argument
6001** value to the application-defined function. ^N is zero for the left-most
6002** function argument. ^If there is no metadata
6003** associated with the function argument, the sqlite3_get_auxdata(C,N) interface
6004** returns a NULL pointer.
6005**
6006** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th
6007** argument of the application-defined function. ^Subsequent
6008** calls to sqlite3_get_auxdata(C,N) return P from the most recent
6009** sqlite3_set_auxdata(C,N,P,X) call if the metadata is still valid or
6010** NULL if the metadata has been discarded.
6011** ^After each call to sqlite3_set_auxdata(C,N,P,X) where X is not NULL,
6012** SQLite will invoke the destructor function X with parameter P exactly
6013** once, when the metadata is discarded.
6014** SQLite is free to discard the metadata at any time, including: <ul>
6015** <li> ^(when the corresponding function parameter changes)^, or
6016** <li> ^(when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
6017** SQL statement)^, or
6018** <li> ^(when sqlite3_set_auxdata() is invoked again on the same
6019** parameter)^, or
6020** <li> ^(during the original sqlite3_set_auxdata() call when a memory
6021** allocation error occurs.)^ </ul>
6022**
6023** Note the last bullet in particular. The destructor X in
6024** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
6025** sqlite3_set_auxdata() interface even returns. Hence sqlite3_set_auxdata()
6026** should be called near the end of the function implementation and the
6027** function implementation should not make any use of P after
6028** sqlite3_set_auxdata() has been called.
6029**
6030** ^(In practice, metadata is preserved between function calls for
6031** function parameters that are compile-time constants, including literal
6032** values and [parameters] and expressions composed from the same.)^
6033**
6034** The value of the N parameter to these interfaces should be non-negative.
6035** Future enhancements may make use of negative N values to define new
6036** kinds of function caching behavior.
6037**
6038** These routines must be called from the same thread in which
6039** the SQL function is running.
6040*/
6041SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
6042SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
6043
6044
6045/*
6046** CAPI3REF: Constants Defining Special Destructor Behavior
6047**
6048** These are special values for the destructor that is passed in as the
6049** final argument to routines like [sqlite3_result_blob()]. ^If the destructor
6050** argument is SQLITE_STATIC, it means that the content pointer is constant
6051** and will never change. It does not need to be destroyed. ^The
6052** SQLITE_TRANSIENT value means that the content will likely change in
6053** the near future and that SQLite should make its own private copy of
6054** the content before returning.
6055**
6056** The typedef is necessary to work around problems in certain
6057** C++ compilers.
6058*/
6059typedef void (*sqlite3_destructor_type)(void*);
6060#define SQLITE_STATIC((sqlite3_destructor_type)0) ((sqlite3_destructor_type)0)
6061#define SQLITE_TRANSIENT((sqlite3_destructor_type)-1) ((sqlite3_destructor_type)-1)
6062
6063/*
6064** CAPI3REF: Setting The Result Of An SQL Function
6065** METHOD: sqlite3_context
6066**
6067** These routines are used by the xFunc or xFinal callbacks that
6068** implement SQL functions and aggregates. See
6069** [sqlite3_create_function()] and [sqlite3_create_function16()]
6070** for additional information.
6071**
6072** These functions work very much like the [parameter binding] family of
6073** functions used to bind values to host parameters in prepared statements.
6074** Refer to the [SQL parameter] documentation for additional information.
6075**
6076** ^The sqlite3_result_blob() interface sets the result from
6077** an application-defined function to be the BLOB whose content is pointed
6078** to by the second parameter and which is N bytes long where N is the
6079** third parameter.
6080**
6081** ^The sqlite3_result_zeroblob(C,N) and sqlite3_result_zeroblob64(C,N)
6082** interfaces set the result of the application-defined function to be
6083** a BLOB containing all zero bytes and N bytes in size.
6084**
6085** ^The sqlite3_result_double() interface sets the result from
6086** an application-defined function to be a floating point value specified
6087** by its 2nd argument.
6088**
6089** ^The sqlite3_result_error() and sqlite3_result_error16() functions
6090** cause the implemented SQL function to throw an exception.
6091** ^SQLite uses the string pointed to by the
6092** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16()
6093** as the text of an error message. ^SQLite interprets the error
6094** message string from sqlite3_result_error() as UTF-8. ^SQLite
6095** interprets the string from sqlite3_result_error16() as UTF-16 using
6096** the same [byte-order determination rules] as [sqlite3_bind_text16()].
6097** ^If the third parameter to sqlite3_result_error()
6098** or sqlite3_result_error16() is negative then SQLite takes as the error
6099** message all text up through the first zero character.
6100** ^If the third parameter to sqlite3_result_error() or
6101** sqlite3_result_error16() is non-negative then SQLite takes that many
6102** bytes (not characters) from the 2nd parameter as the error message.
6103** ^The sqlite3_result_error() and sqlite3_result_error16()
6104** routines make a private copy of the error message text before
6105** they return. Hence, the calling function can deallocate or
6106** modify the text after they return without harm.
6107** ^The sqlite3_result_error_code() function changes the error code
6108** returned by SQLite as a result of an error in a function. ^By default,
6109** the error code is SQLITE_ERROR. ^A subsequent call to sqlite3_result_error()
6110** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
6111**
6112** ^The sqlite3_result_error_toobig() interface causes SQLite to throw an
6113** error indicating that a string or BLOB is too long to represent.
6114**
6115** ^The sqlite3_result_error_nomem() interface causes SQLite to throw an
6116** error indicating that a memory allocation failed.
6117**
6118** ^The sqlite3_result_int() interface sets the return value
6119** of the application-defined function to be the 32-bit signed integer
6120** value given in the 2nd argument.
6121** ^The sqlite3_result_int64() interface sets the return value
6122** of the application-defined function to be the 64-bit signed integer
6123** value given in the 2nd argument.
6124**
6125** ^The sqlite3_result_null() interface sets the return value
6126** of the application-defined function to be NULL.
6127**
6128** ^The sqlite3_result_text(), sqlite3_result_text16(),
6129** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
6130** set the return value of the application-defined function to be
6131** a text string which is represented as UTF-8, UTF-16 native byte order,
6132** UTF-16 little endian, or UTF-16 big endian, respectively.
6133** ^The sqlite3_result_text64() interface sets the return value of an
6134** application-defined function to be a text string in an encoding
6135** specified by the fifth (and last) parameter, which must be one
6136** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE].
6137** ^SQLite takes the text result from the application from
6138** the 2nd parameter of the sqlite3_result_text* interfaces.
6139** ^If the 3rd parameter to the sqlite3_result_text* interfaces
6140** is negative, then SQLite takes result text from the 2nd parameter
6141** through the first zero character.
6142** ^If the 3rd parameter to the sqlite3_result_text* interfaces
6143** is non-negative, then as many bytes (not characters) of the text
6144** pointed to by the 2nd parameter are taken as the application-defined
6145** function result. If the 3rd parameter is non-negative, then it
6146** must be the byte offset into the string where the NUL terminator would
6147** appear if the string where NUL terminated. If any NUL characters occur
6148** in the string at a byte offset that is less than the value of the 3rd
6149** parameter, then the resulting string will contain embedded NULs and the
6150** result of expressions operating on strings with embedded NULs is undefined.
6151** ^If the 4th parameter to the sqlite3_result_text* interfaces
6152** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
6153** function as the destructor on the text or BLOB result when it has
6154** finished using that result.
6155** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
6156** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
6157** assumes that the text or BLOB result is in constant space and does not
6158** copy the content of the parameter nor call a destructor on the content
6159** when it has finished using that result.
6160** ^If the 4th parameter to the sqlite3_result_text* interfaces
6161** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
6162** then SQLite makes a copy of the result into space obtained
6163** from [sqlite3_malloc()] before it returns.
6164**
6165** ^For the sqlite3_result_text16(), sqlite3_result_text16le(), and
6166** sqlite3_result_text16be() routines, and for sqlite3_result_text64()
6167** when the encoding is not UTF8, if the input UTF16 begins with a
6168** byte-order mark (BOM, U+FEFF) then the BOM is removed from the
6169** string and the rest of the string is interpreted according to the
6170** byte-order specified by the BOM. ^The byte-order specified by
6171** the BOM at the beginning of the text overrides the byte-order
6172** specified by the interface procedure. ^So, for example, if
6173** sqlite3_result_text16le() is invoked with text that begins
6174** with bytes 0xfe, 0xff (a big-endian byte-order mark) then the
6175** first two bytes of input are skipped and the remaining input
6176** is interpreted as UTF16BE text.
6177**
6178** ^For UTF16 input text to the sqlite3_result_text16(),
6179** sqlite3_result_text16be(), sqlite3_result_text16le(), and
6180** sqlite3_result_text64() routines, if the text contains invalid
6181** UTF16 characters, the invalid characters might be converted
6182** into the unicode replacement character, U+FFFD.
6183**
6184** ^The sqlite3_result_value() interface sets the result of
6185** the application-defined function to be a copy of the
6186** [unprotected sqlite3_value] object specified by the 2nd parameter. ^The
6187** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
6188** so that the [sqlite3_value] specified in the parameter may change or
6189** be deallocated after sqlite3_result_value() returns without harm.
6190** ^A [protected sqlite3_value] object may always be used where an
6191** [unprotected sqlite3_value] object is required, so either
6192** kind of [sqlite3_value] object can be used with this interface.
6193**
6194** ^The sqlite3_result_pointer(C,P,T,D) interface sets the result to an
6195** SQL NULL value, just like [sqlite3_result_null(C)], except that it
6196** also associates the host-language pointer P or type T with that
6197** NULL value such that the pointer can be retrieved within an
6198** [application-defined SQL function] using [sqlite3_value_pointer()].
6199** ^If the D parameter is not NULL, then it is a pointer to a destructor
6200** for the P parameter. ^SQLite invokes D with P as its only argument
6201** when SQLite is finished with P. The T parameter should be a static
6202** string and preferably a string literal. The sqlite3_result_pointer()
6203** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
6204**
6205** If these routines are called from within the different thread
6206** than the one containing the application-defined function that received
6207** the [sqlite3_context] pointer, the results are undefined.
6208*/
6209SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
6210SQLITE_API void sqlite3_result_blob64(sqlite3_context*,const void*,
6211 sqlite3_uint64,void(*)(void*));
6212SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
6213SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
6214SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
6215SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
6216SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
6217SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
6218SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
6219SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
6220SQLITE_API void sqlite3_result_null(sqlite3_context*);
6221SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
6222SQLITE_API void sqlite3_result_text64(sqlite3_context*, const char*,sqlite3_uint64,
6223 void(*)(void*), unsigned char encoding);
6224SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
6225SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
6226SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
6227SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
6228SQLITE_API void sqlite3_result_pointer(sqlite3_context*, void*,const char*,void(*)(void*));
6229SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
6230SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n);
6231
6232
6233/*
6234** CAPI3REF: Setting The Subtype Of An SQL Function
6235** METHOD: sqlite3_context
6236**
6237** The sqlite3_result_subtype(C,T) function causes the subtype of
6238** the result from the [application-defined SQL function] with
6239** [sqlite3_context] C to be the value T. Only the lower 8 bits
6240** of the subtype T are preserved in current versions of SQLite;
6241** higher order bits are discarded.
6242** The number of subtype bytes preserved by SQLite might increase
6243** in future releases of SQLite.
6244*/
6245SQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int);
6246
6247/*
6248** CAPI3REF: Define New Collating Sequences
6249** METHOD: sqlite3
6250**
6251** ^These functions add, remove, or modify a [collation] associated
6252** with the [database connection] specified as the first argument.
6253**
6254** ^The name of the collation is a UTF-8 string
6255** for sqlite3_create_collation() and sqlite3_create_collation_v2()
6256** and a UTF-16 string in native byte order for sqlite3_create_collation16().
6257** ^Collation names that compare equal according to [sqlite3_strnicmp()] are
6258** considered to be the same name.
6259**
6260** ^(The third argument (eTextRep) must be one of the constants:
6261** <ul>
6262** <li> [SQLITE_UTF8],
6263** <li> [SQLITE_UTF16LE],
6264** <li> [SQLITE_UTF16BE],
6265** <li> [SQLITE_UTF16], or
6266** <li> [SQLITE_UTF16_ALIGNED].
6267** </ul>)^
6268** ^The eTextRep argument determines the encoding of strings passed
6269** to the collating function callback, xCompare.
6270** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep
6271** force strings to be UTF16 with native byte order.
6272** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin
6273** on an even byte address.
6274**
6275** ^The fourth argument, pArg, is an application data pointer that is passed
6276** through as the first argument to the collating function callback.
6277**
6278** ^The fifth argument, xCompare, is a pointer to the collating function.
6279** ^Multiple collating functions can be registered using the same name but
6280** with different eTextRep parameters and SQLite will use whichever
6281** function requires the least amount of data transformation.
6282** ^If the xCompare argument is NULL then the collating function is
6283** deleted. ^When all collating functions having the same name are deleted,
6284** that collation is no longer usable.
6285**
6286** ^The collating function callback is invoked with a copy of the pArg
6287** application data pointer and with two strings in the encoding specified
6288** by the eTextRep argument. The two integer parameters to the collating
6289** function callback are the length of the two strings, in bytes. The collating
6290** function must return an integer that is negative, zero, or positive
6291** if the first string is less than, equal to, or greater than the second,
6292** respectively. A collating function must always return the same answer
6293** given the same inputs. If two or more collating functions are registered
6294** to the same collation name (using different eTextRep values) then all
6295** must give an equivalent answer when invoked with equivalent strings.
6296** The collating function must obey the following properties for all
6297** strings A, B, and C:
6298**
6299** <ol>
6300** <li> If A==B then B==A.
6301** <li> If A==B and B==C then A==C.
6302** <li> If A&lt;B THEN B&gt;A.
6303** <li> If A&lt;B and B&lt;C then A&lt;C.
6304** </ol>
6305**
6306** If a collating function fails any of the above constraints and that
6307** collating function is registered and used, then the behavior of SQLite
6308** is undefined.
6309**
6310** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation()
6311** with the addition that the xDestroy callback is invoked on pArg when
6312** the collating function is deleted.
6313** ^Collating functions are deleted when they are overridden by later
6314** calls to the collation creation functions or when the
6315** [database connection] is closed using [sqlite3_close()].
6316**
6317** ^The xDestroy callback is <u>not</u> called if the
6318** sqlite3_create_collation_v2() function fails. Applications that invoke
6319** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
6320** check the return code and dispose of the application data pointer
6321** themselves rather than expecting SQLite to deal with it for them.
6322** This is different from every other SQLite interface. The inconsistency
6323** is unfortunate but cannot be changed without breaking backwards
6324** compatibility.
6325**
6326** See also: [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
6327*/
6328SQLITE_API int sqlite3_create_collation(
6329 sqlite3*,
6330 const char *zName,
6331 int eTextRep,
6332 void *pArg,
6333 int(*xCompare)(void*,int,const void*,int,const void*)
6334);
6335SQLITE_API int sqlite3_create_collation_v2(
6336 sqlite3*,
6337 const char *zName,
6338 int eTextRep,
6339 void *pArg,
6340 int(*xCompare)(void*,int,const void*,int,const void*),
6341 void(*xDestroy)(void*)
6342);
6343SQLITE_API int sqlite3_create_collation16(
6344 sqlite3*,
6345 const void *zName,
6346 int eTextRep,
6347 void *pArg,
6348 int(*xCompare)(void*,int,const void*,int,const void*)
6349);
6350
6351/*
6352** CAPI3REF: Collation Needed Callbacks
6353** METHOD: sqlite3
6354**
6355** ^To avoid having to register all collation sequences before a database
6356** can be used, a single callback function may be registered with the
6357** [database connection] to be invoked whenever an undefined collation
6358** sequence is required.
6359**
6360** ^If the function is registered using the sqlite3_collation_needed() API,
6361** then it is passed the names of undefined collation sequences as strings
6362** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
6363** the names are passed as UTF-16 in machine native byte order.
6364** ^A call to either function replaces the existing collation-needed callback.
6365**
6366** ^(When the callback is invoked, the first argument passed is a copy
6367** of the second argument to sqlite3_collation_needed() or
6368** sqlite3_collation_needed16(). The second argument is the database
6369** connection. The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE],
6370** or [SQLITE_UTF16LE], indicating the most desirable form of the collation
6371** sequence function required. The fourth parameter is the name of the
6372** required collation sequence.)^
6373**
6374** The callback function should register the desired collation using
6375** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
6376** [sqlite3_create_collation_v2()].
6377*/
6378SQLITE_API int sqlite3_collation_needed(
6379 sqlite3*,
6380 void*,
6381 void(*)(void*,sqlite3*,int eTextRep,const char*)
6382);
6383SQLITE_API int sqlite3_collation_needed16(
6384 sqlite3*,
6385 void*,
6386 void(*)(void*,sqlite3*,int eTextRep,const void*)
6387);
6388
6389#ifdef SQLITE_ENABLE_CEROD
6390/*
6391** Specify the activation key for a CEROD database. Unless
6392** activated, none of the CEROD routines will work.
6393*/
6394SQLITE_API void sqlite3_activate_cerod(
6395 const char *zPassPhrase /* Activation phrase */
6396);
6397#endif
6398
6399/*
6400** CAPI3REF: Suspend Execution For A Short Time
6401**
6402** The sqlite3_sleep() function causes the current thread to suspend execution
6403** for at least a number of milliseconds specified in its parameter.
6404**
6405** If the operating system does not support sleep requests with
6406** millisecond time resolution, then the time will be rounded up to
6407** the nearest second. The number of milliseconds of sleep actually
6408** requested from the operating system is returned.
6409**
6410** ^SQLite implements this interface by calling the xSleep()
6411** method of the default [sqlite3_vfs] object. If the xSleep() method
6412** of the default VFS is not implemented correctly, or not implemented at
6413** all, then the behavior of sqlite3_sleep() may deviate from the description
6414** in the previous paragraphs.
6415*/
6416SQLITE_API int sqlite3_sleep(int);
6417
6418/*
6419** CAPI3REF: Name Of The Folder Holding Temporary Files
6420**
6421** ^(If this global variable is made to point to a string which is
6422** the name of a folder (a.k.a. directory), then all temporary files
6423** created by SQLite when using a built-in [sqlite3_vfs | VFS]
6424** will be placed in that directory.)^ ^If this variable
6425** is a NULL pointer, then SQLite performs a search for an appropriate
6426** temporary file directory.
6427**
6428** Applications are strongly discouraged from using this global variable.
6429** It is required to set a temporary folder on Windows Runtime (WinRT).
6430** But for all other platforms, it is highly recommended that applications
6431** neither read nor write this variable. This global variable is a relic
6432** that exists for backwards compatibility of legacy applications and should
6433** be avoided in new projects.
6434**
6435** It is not safe to read or modify this variable in more than one
6436** thread at a time. It is not safe to read or modify this variable
6437** if a [database connection] is being used at the same time in a separate
6438** thread.
6439** It is intended that this variable be set once
6440** as part of process initialization and before any SQLite interface
6441** routines have been called and that this variable remain unchanged
6442** thereafter.
6443**
6444** ^The [temp_store_directory pragma] may modify this variable and cause
6445** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore,
6446** the [temp_store_directory pragma] always assumes that any string
6447** that this variable points to is held in memory obtained from
6448** [sqlite3_malloc] and the pragma may attempt to free that memory
6449** using [sqlite3_free].
6450** Hence, if this variable is modified directly, either it should be
6451** made NULL or made to point to memory obtained from [sqlite3_malloc]
6452** or else the use of the [temp_store_directory pragma] should be avoided.
6453** Except when requested by the [temp_store_directory pragma], SQLite
6454** does not free the memory that sqlite3_temp_directory points to. If
6455** the application wants that memory to be freed, it must do
6456** so itself, taking care to only do so after all [database connection]
6457** objects have been destroyed.
6458**
6459** <b>Note to Windows Runtime users:</b> The temporary directory must be set
6460** prior to calling [sqlite3_open] or [sqlite3_open_v2]. Otherwise, various
6461** features that require the use of temporary files may fail. Here is an
6462** example of how to do this using C++ with the Windows Runtime:
6463**
6464** <blockquote><pre>
6465** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
6466** &nbsp; TemporaryFolder->Path->Data();
6467** char zPathBuf&#91;MAX_PATH + 1&#93;;
6468** memset(zPathBuf, 0, sizeof(zPathBuf));
6469** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
6470** &nbsp; NULL, NULL);
6471** sqlite3_temp_directory = sqlite3_mprintf("%s", zPathBuf);
6472** </pre></blockquote>
6473*/
6474SQLITE_API char *sqlite3_temp_directory;
6475
6476/*
6477** CAPI3REF: Name Of The Folder Holding Database Files
6478**
6479** ^(If this global variable is made to point to a string which is
6480** the name of a folder (a.k.a. directory), then all database files
6481** specified with a relative pathname and created or accessed by
6482** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
6483** to be relative to that directory.)^ ^If this variable is a NULL
6484** pointer, then SQLite assumes that all database files specified
6485** with a relative pathname are relative to the current directory
6486** for the process. Only the windows VFS makes use of this global
6487** variable; it is ignored by the unix VFS.
6488**
6489** Changing the value of this variable while a database connection is
6490** open can result in a corrupt database.
6491**
6492** It is not safe to read or modify this variable in more than one
6493** thread at a time. It is not safe to read or modify this variable
6494** if a [database connection] is being used at the same time in a separate
6495** thread.
6496** It is intended that this variable be set once
6497** as part of process initialization and before any SQLite interface
6498** routines have been called and that this variable remain unchanged
6499** thereafter.
6500**
6501** ^The [data_store_directory pragma] may modify this variable and cause
6502** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore,
6503** the [data_store_directory pragma] always assumes that any string
6504** that this variable points to is held in memory obtained from
6505** [sqlite3_malloc] and the pragma may attempt to free that memory
6506** using [sqlite3_free].
6507** Hence, if this variable is modified directly, either it should be
6508** made NULL or made to point to memory obtained from [sqlite3_malloc]
6509** or else the use of the [data_store_directory pragma] should be avoided.
6510*/
6511SQLITE_API char *sqlite3_data_directory;
6512
6513/*
6514** CAPI3REF: Win32 Specific Interface
6515**
6516** These interfaces are available only on Windows. The
6517** [sqlite3_win32_set_directory] interface is used to set the value associated
6518** with the [sqlite3_temp_directory] or [sqlite3_data_directory] variable, to
6519** zValue, depending on the value of the type parameter. The zValue parameter
6520** should be NULL to cause the previous value to be freed via [sqlite3_free];
6521** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
6522** prior to being used. The [sqlite3_win32_set_directory] interface returns
6523** [SQLITE_OK] to indicate success, [SQLITE_ERROR] if the type is unsupported,
6524** or [SQLITE_NOMEM] if memory could not be allocated. The value of the
6525** [sqlite3_data_directory] variable is intended to act as a replacement for
6526** the current directory on the sub-platforms of Win32 where that concept is
6527** not present, e.g. WinRT and UWP. The [sqlite3_win32_set_directory8] and
6528** [sqlite3_win32_set_directory16] interfaces behave exactly the same as the
6529** sqlite3_win32_set_directory interface except the string parameter must be
6530** UTF-8 or UTF-16, respectively.
6531*/
6532SQLITE_API int sqlite3_win32_set_directory(
6533 unsigned long type, /* Identifier for directory being set or reset */
6534 void *zValue /* New value for directory being set or reset */
6535);
6536SQLITE_API int sqlite3_win32_set_directory8(unsigned long type, const char *zValue);
6537SQLITE_API int sqlite3_win32_set_directory16(unsigned long type, const void *zValue);
6538
6539/*
6540** CAPI3REF: Win32 Directory Types
6541**
6542** These macros are only available on Windows. They define the allowed values
6543** for the type argument to the [sqlite3_win32_set_directory] interface.
6544*/
6545#define SQLITE_WIN32_DATA_DIRECTORY_TYPE1 1
6546#define SQLITE_WIN32_TEMP_DIRECTORY_TYPE2 2
6547
6548/*
6549** CAPI3REF: Test For Auto-Commit Mode
6550** KEYWORDS: {autocommit mode}
6551** METHOD: sqlite3
6552**
6553** ^The sqlite3_get_autocommit() interface returns non-zero or
6554** zero if the given database connection is or is not in autocommit mode,
6555** respectively. ^Autocommit mode is on by default.
6556** ^Autocommit mode is disabled by a [BEGIN] statement.
6557** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
6558**
6559** If certain kinds of errors occur on a statement within a multi-statement
6560** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR],
6561** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
6562** transaction might be rolled back automatically. The only way to
6563** find out whether SQLite automatically rolled back the transaction after
6564** an error is to use this function.
6565**
6566** If another thread changes the autocommit status of the database
6567** connection while this routine is running, then the return value
6568** is undefined.
6569*/
6570SQLITE_API int sqlite3_get_autocommit(sqlite3*);
6571
6572/*
6573** CAPI3REF: Find The Database Handle Of A Prepared Statement
6574** METHOD: sqlite3_stmt
6575**
6576** ^The sqlite3_db_handle interface returns the [database connection] handle
6577** to which a [prepared statement] belongs. ^The [database connection]
6578** returned by sqlite3_db_handle is the same [database connection]
6579** that was the first argument
6580** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
6581** create the statement in the first place.
6582*/
6583SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
6584
6585/*
6586** CAPI3REF: Return The Schema Name For A Database Connection
6587** METHOD: sqlite3
6588**
6589** ^The sqlite3_db_name(D,N) interface returns a pointer to the schema name
6590** for the N-th database on database connection D, or a NULL pointer of N is
6591** out of range. An N value of 0 means the main database file. An N of 1 is
6592** the "temp" schema. Larger values of N correspond to various ATTACH-ed
6593** databases.
6594**
6595** Space to hold the string that is returned by sqlite3_db_name() is managed
6596** by SQLite itself. The string might be deallocated by any operation that
6597** changes the schema, including [ATTACH] or [DETACH] or calls to
6598** [sqlite3_serialize()] or [sqlite3_deserialize()], even operations that
6599** occur on a different thread. Applications that need to
6600** remember the string long-term should make their own copy. Applications that
6601** are accessing the same database connection simultaneously on multiple
6602** threads should mutex-protect calls to this API and should make their own
6603** private copy of the result prior to releasing the mutex.
6604*/
6605SQLITE_API const char *sqlite3_db_name(sqlite3 *db, int N);
6606
6607/*
6608** CAPI3REF: Return The Filename For A Database Connection
6609** METHOD: sqlite3
6610**
6611** ^The sqlite3_db_filename(D,N) interface returns a pointer to the filename
6612** associated with database N of connection D.
6613** ^If there is no attached database N on the database
6614** connection D, or if database N is a temporary or in-memory database, then
6615** this function will return either a NULL pointer or an empty string.
6616**
6617** ^The string value returned by this routine is owned and managed by
6618** the database connection. ^The value will be valid until the database N
6619** is [DETACH]-ed or until the database connection closes.
6620**
6621** ^The filename returned by this function is the output of the
6622** xFullPathname method of the [VFS]. ^In other words, the filename
6623** will be an absolute pathname, even if the filename used
6624** to open the database originally was a URI or relative pathname.
6625**
6626** If the filename pointer returned by this routine is not NULL, then it
6627** can be used as the filename input parameter to these routines:
6628** <ul>
6629** <li> [sqlite3_uri_parameter()]
6630** <li> [sqlite3_uri_boolean()]
6631** <li> [sqlite3_uri_int64()]
6632** <li> [sqlite3_filename_database()]
6633** <li> [sqlite3_filename_journal()]
6634** <li> [sqlite3_filename_wal()]
6635** </ul>
6636*/
6637SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
6638
6639/*
6640** CAPI3REF: Determine if a database is read-only
6641** METHOD: sqlite3
6642**
6643** ^The sqlite3_db_readonly(D,N) interface returns 1 if the database N
6644** of connection D is read-only, 0 if it is read/write, or -1 if N is not
6645** the name of a database on connection D.
6646*/
6647SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
6648
6649/*
6650** CAPI3REF: Determine the transaction state of a database
6651** METHOD: sqlite3
6652**
6653** ^The sqlite3_txn_state(D,S) interface returns the current
6654** [transaction state] of schema S in database connection D. ^If S is NULL,
6655** then the highest transaction state of any schema on database connection D
6656** is returned. Transaction states are (in order of lowest to highest):
6657** <ol>
6658** <li value="0"> SQLITE_TXN_NONE
6659** <li value="1"> SQLITE_TXN_READ
6660** <li value="2"> SQLITE_TXN_WRITE
6661** </ol>
6662** ^If the S argument to sqlite3_txn_state(D,S) is not the name of
6663** a valid schema, then -1 is returned.
6664*/
6665SQLITE_API int sqlite3_txn_state(sqlite3*,const char *zSchema);
6666
6667/*
6668** CAPI3REF: Allowed return values from [sqlite3_txn_state()]
6669** KEYWORDS: {transaction state}
6670**
6671** These constants define the current transaction state of a database file.
6672** ^The [sqlite3_txn_state(D,S)] interface returns one of these
6673** constants in order to describe the transaction state of schema S
6674** in [database connection] D.
6675**
6676** <dl>
6677** [[SQLITE_TXN_NONE]] <dt>SQLITE_TXN_NONE</dt>
6678** <dd>The SQLITE_TXN_NONE state means that no transaction is currently
6679** pending.</dd>
6680**
6681** [[SQLITE_TXN_READ]] <dt>SQLITE_TXN_READ</dt>
6682** <dd>The SQLITE_TXN_READ state means that the database is currently
6683** in a read transaction. Content has been read from the database file
6684** but nothing in the database file has changed. The transaction state
6685** will advanced to SQLITE_TXN_WRITE if any changes occur and there are
6686** no other conflicting concurrent write transactions. The transaction
6687** state will revert to SQLITE_TXN_NONE following a [ROLLBACK] or
6688** [COMMIT].</dd>
6689**
6690** [[SQLITE_TXN_WRITE]] <dt>SQLITE_TXN_WRITE</dt>
6691** <dd>The SQLITE_TXN_WRITE state means that the database is currently
6692** in a write transaction. Content has been written to the database file
6693** but has not yet committed. The transaction state will change to
6694** to SQLITE_TXN_NONE at the next [ROLLBACK] or [COMMIT].</dd>
6695*/
6696#define SQLITE_TXN_NONE0 0
6697#define SQLITE_TXN_READ1 1
6698#define SQLITE_TXN_WRITE2 2
6699
6700/*
6701** CAPI3REF: Find the next prepared statement
6702** METHOD: sqlite3
6703**
6704** ^This interface returns a pointer to the next [prepared statement] after
6705** pStmt associated with the [database connection] pDb. ^If pStmt is NULL
6706** then this interface returns a pointer to the first prepared statement
6707** associated with the database connection pDb. ^If no prepared statement
6708** satisfies the conditions of this routine, it returns NULL.
6709**
6710** The [database connection] pointer D in a call to
6711** [sqlite3_next_stmt(D,S)] must refer to an open database
6712** connection and in particular must not be a NULL pointer.
6713*/
6714SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
6715
6716/*
6717** CAPI3REF: Commit And Rollback Notification Callbacks
6718** METHOD: sqlite3
6719**
6720** ^The sqlite3_commit_hook() interface registers a callback
6721** function to be invoked whenever a transaction is [COMMIT | committed].
6722** ^Any callback set by a previous call to sqlite3_commit_hook()
6723** for the same database connection is overridden.
6724** ^The sqlite3_rollback_hook() interface registers a callback
6725** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
6726** ^Any callback set by a previous call to sqlite3_rollback_hook()
6727** for the same database connection is overridden.
6728** ^The pArg argument is passed through to the callback.
6729** ^If the callback on a commit hook function returns non-zero,
6730** then the commit is converted into a rollback.
6731**
6732** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
6733** return the P argument from the previous call of the same function
6734** on the same [database connection] D, or NULL for
6735** the first call for each function on D.
6736**
6737** The commit and rollback hook callbacks are not reentrant.
6738** The callback implementation must not do anything that will modify
6739** the database connection that invoked the callback. Any actions
6740** to modify the database connection must be deferred until after the
6741** completion of the [sqlite3_step()] call that triggered the commit
6742** or rollback hook in the first place.
6743** Note that running any other SQL statements, including SELECT statements,
6744** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify
6745** the database connections for the meaning of "modify" in this paragraph.
6746**
6747** ^Registering a NULL function disables the callback.
6748**
6749** ^When the commit hook callback routine returns zero, the [COMMIT]
6750** operation is allowed to continue normally. ^If the commit hook
6751** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
6752** ^The rollback hook is invoked on a rollback that results from a commit
6753** hook returning non-zero, just as it would be with any other rollback.
6754**
6755** ^For the purposes of this API, a transaction is said to have been
6756** rolled back if an explicit "ROLLBACK" statement is executed, or
6757** an error or constraint causes an implicit rollback to occur.
6758** ^The rollback callback is not invoked if a transaction is
6759** automatically rolled back because the database connection is closed.
6760**
6761** See also the [sqlite3_update_hook()] interface.
6762*/
6763SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
6764SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
6765
6766/*
6767** CAPI3REF: Autovacuum Compaction Amount Callback
6768** METHOD: sqlite3
6769**
6770** ^The sqlite3_autovacuum_pages(D,C,P,X) interface registers a callback
6771** function C that is invoked prior to each autovacuum of the database
6772** file. ^The callback is passed a copy of the generic data pointer (P),
6773** the schema-name of the attached database that is being autovacuumed,
6774** the the size of the database file in pages, the number of free pages,
6775** and the number of bytes per page, respectively. The callback should
6776** return the number of free pages that should be removed by the
6777** autovacuum. ^If the callback returns zero, then no autovacuum happens.
6778** ^If the value returned is greater than or equal to the number of
6779** free pages, then a complete autovacuum happens.
6780**
6781** <p>^If there are multiple ATTACH-ed database files that are being
6782** modified as part of a transaction commit, then the autovacuum pages
6783** callback is invoked separately for each file.
6784**
6785** <p><b>The callback is not reentrant.</b> The callback function should
6786** not attempt to invoke any other SQLite interface. If it does, bad
6787** things may happen, including segmentation faults and corrupt database
6788** files. The callback function should be a simple function that
6789** does some arithmetic on its input parameters and returns a result.
6790**
6791** ^The X parameter to sqlite3_autovacuum_pages(D,C,P,X) is an optional
6792** destructor for the P parameter. ^If X is not NULL, then X(P) is
6793** invoked whenever the database connection closes or when the callback
6794** is overwritten by another invocation of sqlite3_autovacuum_pages().
6795**
6796** <p>^There is only one autovacuum pages callback per database connection.
6797** ^Each call to the sqlite3_autovacuum_pages() interface overrides all
6798** previous invocations for that database connection. ^If the callback
6799** argument (C) to sqlite3_autovacuum_pages(D,C,P,X) is a NULL pointer,
6800** then the autovacuum steps callback is cancelled. The return value
6801** from sqlite3_autovacuum_pages() is normally SQLITE_OK, but might
6802** be some other error code if something goes wrong. The current
6803** implementation will only return SQLITE_OK or SQLITE_MISUSE, but other
6804** return codes might be added in future releases.
6805**
6806** <p>If no autovacuum pages callback is specified (the usual case) or
6807** a NULL pointer is provided for the callback,
6808** then the default behavior is to vacuum all free pages. So, in other
6809** words, the default behavior is the same as if the callback function
6810** were something like this:
6811**
6812** <blockquote><pre>
6813** &nbsp; unsigned int demonstration_autovac_pages_callback(
6814** &nbsp; void *pClientData,
6815** &nbsp; const char *zSchema,
6816** &nbsp; unsigned int nDbPage,
6817** &nbsp; unsigned int nFreePage,
6818** &nbsp; unsigned int nBytePerPage
6819** &nbsp; ){
6820** &nbsp; return nFreePage;
6821** &nbsp; }
6822** </pre></blockquote>
6823*/
6824SQLITE_API int sqlite3_autovacuum_pages(
6825 sqlite3 *db,
6826 unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int),
6827 void*,
6828 void(*)(void*)
6829);
6830
6831
6832/*
6833** CAPI3REF: Data Change Notification Callbacks
6834** METHOD: sqlite3
6835**
6836** ^The sqlite3_update_hook() interface registers a callback function
6837** with the [database connection] identified by the first argument
6838** to be invoked whenever a row is updated, inserted or deleted in
6839** a [rowid table].
6840** ^Any callback set by a previous call to this function
6841** for the same database connection is overridden.
6842**
6843** ^The second argument is a pointer to the function to invoke when a
6844** row is updated, inserted or deleted in a rowid table.
6845** ^The first argument to the callback is a copy of the third argument
6846** to sqlite3_update_hook().
6847** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
6848** or [SQLITE_UPDATE], depending on the operation that caused the callback
6849** to be invoked.
6850** ^The third and fourth arguments to the callback contain pointers to the
6851** database and table name containing the affected row.
6852** ^The final callback parameter is the [rowid] of the row.
6853** ^In the case of an update, this is the [rowid] after the update takes place.
6854**
6855** ^(The update hook is not invoked when internal system tables are
6856** modified (i.e. sqlite_sequence).)^
6857** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
6858**
6859** ^In the current implementation, the update hook
6860** is not invoked when conflicting rows are deleted because of an
6861** [ON CONFLICT | ON CONFLICT REPLACE] clause. ^Nor is the update hook
6862** invoked when rows are deleted using the [truncate optimization].
6863** The exceptions defined in this paragraph might change in a future
6864** release of SQLite.
6865**
6866** The update hook implementation must not do anything that will modify
6867** the database connection that invoked the update hook. Any actions
6868** to modify the database connection must be deferred until after the
6869** completion of the [sqlite3_step()] call that triggered the update hook.
6870** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
6871** database connections for the meaning of "modify" in this paragraph.
6872**
6873** ^The sqlite3_update_hook(D,C,P) function
6874** returns the P argument from the previous call
6875** on the same [database connection] D, or NULL for
6876** the first call on D.
6877**
6878** See also the [sqlite3_commit_hook()], [sqlite3_rollback_hook()],
6879** and [sqlite3_preupdate_hook()] interfaces.
6880*/
6881SQLITE_API void *sqlite3_update_hook(
6882 sqlite3*,
6883 void(*)(void *,int ,char const *,char const *,sqlite3_int64),
6884 void*
6885);
6886
6887/*
6888** CAPI3REF: Enable Or Disable Shared Pager Cache
6889**
6890** ^(This routine enables or disables the sharing of the database cache
6891** and schema data structures between [database connection | connections]
6892** to the same database. Sharing is enabled if the argument is true
6893** and disabled if the argument is false.)^
6894**
6895** ^Cache sharing is enabled and disabled for an entire process.
6896** This is a change as of SQLite [version 3.5.0] ([dateof:3.5.0]).
6897** In prior versions of SQLite,
6898** sharing was enabled or disabled for each thread separately.
6899**
6900** ^(The cache sharing mode set by this interface effects all subsequent
6901** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
6902** Existing database connections continue to use the sharing mode
6903** that was in effect at the time they were opened.)^
6904**
6905** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled
6906** successfully. An [error code] is returned otherwise.)^
6907**
6908** ^Shared cache is disabled by default. It is recommended that it stay
6909** that way. In other words, do not use this routine. This interface
6910** continues to be provided for historical compatibility, but its use is
6911** discouraged. Any use of shared cache is discouraged. If shared cache
6912** must be used, it is recommended that shared cache only be enabled for
6913** individual database connections using the [sqlite3_open_v2()] interface
6914** with the [SQLITE_OPEN_SHAREDCACHE] flag.
6915**
6916** Note: This method is disabled on MacOS X 10.7 and iOS version 5.0
6917** and will always return SQLITE_MISUSE. On those systems,
6918** shared cache mode should be enabled per-database connection via
6919** [sqlite3_open_v2()] with [SQLITE_OPEN_SHAREDCACHE].
6920**
6921** This interface is threadsafe on processors where writing a
6922** 32-bit integer is atomic.
6923**
6924** See Also: [SQLite Shared-Cache Mode]
6925*/
6926SQLITE_API int sqlite3_enable_shared_cache(int);
6927
6928/*
6929** CAPI3REF: Attempt To Free Heap Memory
6930**
6931** ^The sqlite3_release_memory() interface attempts to free N bytes
6932** of heap memory by deallocating non-essential memory allocations
6933** held by the database library. Memory used to cache database
6934** pages to improve performance is an example of non-essential memory.
6935** ^sqlite3_release_memory() returns the number of bytes actually freed,
6936** which might be more or less than the amount requested.
6937** ^The sqlite3_release_memory() routine is a no-op returning zero
6938** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
6939**
6940** See also: [sqlite3_db_release_memory()]
6941*/
6942SQLITE_API int sqlite3_release_memory(int);
6943
6944/*
6945** CAPI3REF: Free Memory Used By A Database Connection
6946** METHOD: sqlite3
6947**
6948** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
6949** memory as possible from database connection D. Unlike the
6950** [sqlite3_release_memory()] interface, this interface is in effect even
6951** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
6952** omitted.
6953**
6954** See also: [sqlite3_release_memory()]
6955*/
6956SQLITE_API int sqlite3_db_release_memory(sqlite3*);
6957
6958/*
6959** CAPI3REF: Impose A Limit On Heap Size
6960**
6961** These interfaces impose limits on the amount of heap memory that will be
6962** by all database connections within a single process.
6963**
6964** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
6965** soft limit on the amount of heap memory that may be allocated by SQLite.
6966** ^SQLite strives to keep heap memory utilization below the soft heap
6967** limit by reducing the number of pages held in the page cache
6968** as heap memory usages approaches the limit.
6969** ^The soft heap limit is "soft" because even though SQLite strives to stay
6970** below the limit, it will exceed the limit rather than generate
6971** an [SQLITE_NOMEM] error. In other words, the soft heap limit
6972** is advisory only.
6973**
6974** ^The sqlite3_hard_heap_limit64(N) interface sets a hard upper bound of
6975** N bytes on the amount of memory that will be allocated. ^The
6976** sqlite3_hard_heap_limit64(N) interface is similar to
6977** sqlite3_soft_heap_limit64(N) except that memory allocations will fail
6978** when the hard heap limit is reached.
6979**
6980** ^The return value from both sqlite3_soft_heap_limit64() and
6981** sqlite3_hard_heap_limit64() is the size of
6982** the heap limit prior to the call, or negative in the case of an
6983** error. ^If the argument N is negative
6984** then no change is made to the heap limit. Hence, the current
6985** size of heap limits can be determined by invoking
6986** sqlite3_soft_heap_limit64(-1) or sqlite3_hard_heap_limit(-1).
6987**
6988** ^Setting the heap limits to zero disables the heap limiter mechanism.
6989**
6990** ^The soft heap limit may not be greater than the hard heap limit.
6991** ^If the hard heap limit is enabled and if sqlite3_soft_heap_limit(N)
6992** is invoked with a value of N that is greater than the hard heap limit,
6993** the the soft heap limit is set to the value of the hard heap limit.
6994** ^The soft heap limit is automatically enabled whenever the hard heap
6995** limit is enabled. ^When sqlite3_hard_heap_limit64(N) is invoked and
6996** the soft heap limit is outside the range of 1..N, then the soft heap
6997** limit is set to N. ^Invoking sqlite3_soft_heap_limit64(0) when the
6998** hard heap limit is enabled makes the soft heap limit equal to the
6999** hard heap limit.
7000**
7001** The memory allocation limits can also be adjusted using
7002** [PRAGMA soft_heap_limit] and [PRAGMA hard_heap_limit].
7003**
7004** ^(The heap limits are not enforced in the current implementation
7005** if one or more of following conditions are true:
7006**
7007** <ul>
7008** <li> The limit value is set to zero.
7009** <li> Memory accounting is disabled using a combination of the
7010** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
7011** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
7012** <li> An alternative page cache implementation is specified using
7013** [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...).
7014** <li> The page cache allocates from its own memory pool supplied
7015** by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
7016** from the heap.
7017** </ul>)^
7018**
7019** The circumstances under which SQLite will enforce the heap limits may
7020** changes in future releases of SQLite.
7021*/
7022SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N);
7023SQLITE_API sqlite3_int64 sqlite3_hard_heap_limit64(sqlite3_int64 N);
7024
7025/*
7026** CAPI3REF: Deprecated Soft Heap Limit Interface
7027** DEPRECATED
7028**
7029** This is a deprecated version of the [sqlite3_soft_heap_limit64()]
7030** interface. This routine is provided for historical compatibility
7031** only. All new applications should use the
7032** [sqlite3_soft_heap_limit64()] interface rather than this one.
7033*/
7034SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
7035
7036
7037/*
7038** CAPI3REF: Extract Metadata About A Column Of A Table
7039** METHOD: sqlite3
7040**
7041** ^(The sqlite3_table_column_metadata(X,D,T,C,....) routine returns
7042** information about column C of table T in database D
7043** on [database connection] X.)^ ^The sqlite3_table_column_metadata()
7044** interface returns SQLITE_OK and fills in the non-NULL pointers in
7045** the final five arguments with appropriate values if the specified
7046** column exists. ^The sqlite3_table_column_metadata() interface returns
7047** SQLITE_ERROR if the specified column does not exist.
7048** ^If the column-name parameter to sqlite3_table_column_metadata() is a
7049** NULL pointer, then this routine simply checks for the existence of the
7050** table and returns SQLITE_OK if the table exists and SQLITE_ERROR if it
7051** does not. If the table name parameter T in a call to
7052** sqlite3_table_column_metadata(X,D,T,C,...) is NULL then the result is
7053** undefined behavior.
7054**
7055** ^The column is identified by the second, third and fourth parameters to
7056** this function. ^(The second parameter is either the name of the database
7057** (i.e. "main", "temp", or an attached database) containing the specified
7058** table or NULL.)^ ^If it is NULL, then all attached databases are searched
7059** for the table using the same algorithm used by the database engine to
7060** resolve unqualified table references.
7061**
7062** ^The third and fourth parameters to this function are the table and column
7063** name of the desired column, respectively.
7064**
7065** ^Metadata is returned by writing to the memory locations passed as the 5th
7066** and subsequent parameters to this function. ^Any of these arguments may be
7067** NULL, in which case the corresponding element of metadata is omitted.
7068**
7069** ^(<blockquote>
7070** <table border="1">
7071** <tr><th> Parameter <th> Output<br>Type <th> Description
7072**
7073** <tr><td> 5th <td> const char* <td> Data type
7074** <tr><td> 6th <td> const char* <td> Name of default collation sequence
7075** <tr><td> 7th <td> int <td> True if column has a NOT NULL constraint
7076** <tr><td> 8th <td> int <td> True if column is part of the PRIMARY KEY
7077** <tr><td> 9th <td> int <td> True if column is [AUTOINCREMENT]
7078** </table>
7079** </blockquote>)^
7080**
7081** ^The memory pointed to by the character pointers returned for the
7082** declaration type and collation sequence is valid until the next
7083** call to any SQLite API function.
7084**
7085** ^If the specified table is actually a view, an [error code] is returned.
7086**
7087** ^If the specified column is "rowid", "oid" or "_rowid_" and the table
7088** is not a [WITHOUT ROWID] table and an
7089** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output
7090** parameters are set for the explicitly declared column. ^(If there is no
7091** [INTEGER PRIMARY KEY] column, then the outputs
7092** for the [rowid] are set as follows:
7093**
7094** <pre>
7095** data type: "INTEGER"
7096** collation sequence: "BINARY"
7097** not null: 0
7098** primary key: 1
7099** auto increment: 0
7100** </pre>)^
7101**
7102** ^This function causes all database schemas to be read from disk and
7103** parsed, if that has not already been done, and returns an error if
7104** any errors are encountered while loading the schema.
7105*/
7106SQLITE_API int sqlite3_table_column_metadata(
7107 sqlite3 *db, /* Connection handle */
7108 const char *zDbName, /* Database name or NULL */
7109 const char *zTableName, /* Table name */
7110 const char *zColumnName, /* Column name */
7111 char const **pzDataType, /* OUTPUT: Declared data type */
7112 char const **pzCollSeq, /* OUTPUT: Collation sequence name */
7113 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
7114 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
7115 int *pAutoinc /* OUTPUT: True if column is auto-increment */
7116);
7117
7118/*
7119** CAPI3REF: Load An Extension
7120** METHOD: sqlite3
7121**
7122** ^This interface loads an SQLite extension library from the named file.
7123**
7124** ^The sqlite3_load_extension() interface attempts to load an
7125** [SQLite extension] library contained in the file zFile. If
7126** the file cannot be loaded directly, attempts are made to load
7127** with various operating-system specific extensions added.
7128** So for example, if "samplelib" cannot be loaded, then names like
7129** "samplelib.so" or "samplelib.dylib" or "samplelib.dll" might
7130** be tried also.
7131**
7132** ^The entry point is zProc.
7133** ^(zProc may be 0, in which case SQLite will try to come up with an
7134** entry point name on its own. It first tries "sqlite3_extension_init".
7135** If that does not work, it constructs a name "sqlite3_X_init" where the
7136** X is consists of the lower-case equivalent of all ASCII alphabetic
7137** characters in the filename from the last "/" to the first following
7138** "." and omitting any initial "lib".)^
7139** ^The sqlite3_load_extension() interface returns
7140** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
7141** ^If an error occurs and pzErrMsg is not 0, then the
7142** [sqlite3_load_extension()] interface shall attempt to
7143** fill *pzErrMsg with error message text stored in memory
7144** obtained from [sqlite3_malloc()]. The calling function
7145** should free this memory by calling [sqlite3_free()].
7146**
7147** ^Extension loading must be enabled using
7148** [sqlite3_enable_load_extension()] or
7149** [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],1,NULL)
7150** prior to calling this API,
7151** otherwise an error will be returned.
7152**
7153** <b>Security warning:</b> It is recommended that the
7154** [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method be used to enable only this
7155** interface. The use of the [sqlite3_enable_load_extension()] interface
7156** should be avoided. This will keep the SQL function [load_extension()]
7157** disabled and prevent SQL injections from giving attackers
7158** access to extension loading capabilities.
7159**
7160** See also the [load_extension() SQL function].
7161*/
7162SQLITE_API int sqlite3_load_extension(
7163 sqlite3 *db, /* Load the extension into this database connection */
7164 const char *zFile, /* Name of the shared library containing extension */
7165 const char *zProc, /* Entry point. Derived from zFile if 0 */
7166 char **pzErrMsg /* Put error message here if not 0 */
7167);
7168
7169/*
7170** CAPI3REF: Enable Or Disable Extension Loading
7171** METHOD: sqlite3
7172**
7173** ^So as not to open security holes in older applications that are
7174** unprepared to deal with [extension loading], and as a means of disabling
7175** [extension loading] while evaluating user-entered SQL, the following API
7176** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
7177**
7178** ^Extension loading is off by default.
7179** ^Call the sqlite3_enable_load_extension() routine with onoff==1
7180** to turn extension loading on and call it with onoff==0 to turn
7181** it back off again.
7182**
7183** ^This interface enables or disables both the C-API
7184** [sqlite3_load_extension()] and the SQL function [load_extension()].
7185** ^(Use [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],..)
7186** to enable or disable only the C-API.)^
7187**
7188** <b>Security warning:</b> It is recommended that extension loading
7189** be enabled using the [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method
7190** rather than this interface, so the [load_extension()] SQL function
7191** remains disabled. This will prevent SQL injections from giving attackers
7192** access to extension loading capabilities.
7193*/
7194SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
7195
7196/*
7197** CAPI3REF: Automatically Load Statically Linked Extensions
7198**
7199** ^This interface causes the xEntryPoint() function to be invoked for
7200** each new [database connection] that is created. The idea here is that
7201** xEntryPoint() is the entry point for a statically linked [SQLite extension]
7202** that is to be automatically loaded into all new database connections.
7203**
7204** ^(Even though the function prototype shows that xEntryPoint() takes
7205** no arguments and returns void, SQLite invokes xEntryPoint() with three
7206** arguments and expects an integer result as if the signature of the
7207** entry point where as follows:
7208**
7209** <blockquote><pre>
7210** &nbsp; int xEntryPoint(
7211** &nbsp; sqlite3 *db,
7212** &nbsp; const char **pzErrMsg,
7213** &nbsp; const struct sqlite3_api_routines *pThunk
7214** &nbsp; );
7215** </pre></blockquote>)^
7216**
7217** If the xEntryPoint routine encounters an error, it should make *pzErrMsg
7218** point to an appropriate error message (obtained from [sqlite3_mprintf()])
7219** and return an appropriate [error code]. ^SQLite ensures that *pzErrMsg
7220** is NULL before calling the xEntryPoint(). ^SQLite will invoke
7221** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns. ^If any
7222** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()],
7223** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail.
7224**
7225** ^Calling sqlite3_auto_extension(X) with an entry point X that is already
7226** on the list of automatic extensions is a harmless no-op. ^No entry point
7227** will be called more than once for each database connection that is opened.
7228**
7229** See also: [sqlite3_reset_auto_extension()]
7230** and [sqlite3_cancel_auto_extension()]
7231*/
7232SQLITE_API int sqlite3_auto_extension(void(*xEntryPoint)(void));
7233
7234/*
7235** CAPI3REF: Cancel Automatic Extension Loading
7236**
7237** ^The [sqlite3_cancel_auto_extension(X)] interface unregisters the
7238** initialization routine X that was registered using a prior call to
7239** [sqlite3_auto_extension(X)]. ^The [sqlite3_cancel_auto_extension(X)]
7240** routine returns 1 if initialization routine X was successfully
7241** unregistered and it returns 0 if X was not on the list of initialization
7242** routines.
7243*/
7244SQLITE_API int sqlite3_cancel_auto_extension(void(*xEntryPoint)(void));
7245
7246/*
7247** CAPI3REF: Reset Automatic Extension Loading
7248**
7249** ^This interface disables all automatic extensions previously
7250** registered using [sqlite3_auto_extension()].
7251*/
7252SQLITE_API void sqlite3_reset_auto_extension(void);
7253
7254/*
7255** The interface to the virtual-table mechanism is currently considered
7256** to be experimental. The interface might change in incompatible ways.
7257** If this is a problem for you, do not use the interface at this time.
7258**
7259** When the virtual-table mechanism stabilizes, we will declare the
7260** interface fixed, support it indefinitely, and remove this comment.
7261*/
7262
7263/*
7264** Structures used by the virtual table interface
7265*/
7266typedef struct sqlite3_vtab sqlite3_vtab;
7267typedef struct sqlite3_index_info sqlite3_index_info;
7268typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
7269typedef struct sqlite3_module sqlite3_module;
7270
7271/*
7272** CAPI3REF: Virtual Table Object
7273** KEYWORDS: sqlite3_module {virtual table module}
7274**
7275** This structure, sometimes called a "virtual table module",
7276** defines the implementation of a [virtual table].
7277** This structure consists mostly of methods for the module.
7278**
7279** ^A virtual table module is created by filling in a persistent
7280** instance of this structure and passing a pointer to that instance
7281** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
7282** ^The registration remains valid until it is replaced by a different
7283** module or until the [database connection] closes. The content
7284** of this structure must not change while it is registered with
7285** any database connection.
7286*/
7287struct sqlite3_module {
7288 int iVersion;
7289 int (*xCreate)(sqlite3*, void *pAux,
7290 int argc, const char *const*argv,
7291 sqlite3_vtab **ppVTab, char**);
7292 int (*xConnect)(sqlite3*, void *pAux,
7293 int argc, const char *const*argv,
7294 sqlite3_vtab **ppVTab, char**);
7295 int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
7296 int (*xDisconnect)(sqlite3_vtab *pVTab);
7297 int (*xDestroy)(sqlite3_vtab *pVTab);
7298 int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
7299 int (*xClose)(sqlite3_vtab_cursor*);
7300 int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
7301 int argc, sqlite3_value **argv);
7302 int (*xNext)(sqlite3_vtab_cursor*);
7303 int (*xEof)(sqlite3_vtab_cursor*);
7304 int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
7305 int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
7306 int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
7307 int (*xBegin)(sqlite3_vtab *pVTab);
7308 int (*xSync)(sqlite3_vtab *pVTab);
7309 int (*xCommit)(sqlite3_vtab *pVTab);
7310 int (*xRollback)(sqlite3_vtab *pVTab);
7311 int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
7312 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
7313 void **ppArg);
7314 int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
7315 /* The methods above are in version 1 of the sqlite_module object. Those
7316 ** below are for version 2 and greater. */
7317 int (*xSavepoint)(sqlite3_vtab *pVTab, int);
7318 int (*xRelease)(sqlite3_vtab *pVTab, int);
7319 int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
7320 /* The methods above are in versions 1 and 2 of the sqlite_module object.
7321 ** Those below are for version 3 and greater. */
7322 int (*xShadowName)(const char*);
7323};
7324
7325/*
7326** CAPI3REF: Virtual Table Indexing Information
7327** KEYWORDS: sqlite3_index_info
7328**
7329** The sqlite3_index_info structure and its substructures is used as part
7330** of the [virtual table] interface to
7331** pass information into and receive the reply from the [xBestIndex]
7332** method of a [virtual table module]. The fields under **Inputs** are the
7333** inputs to xBestIndex and are read-only. xBestIndex inserts its
7334** results into the **Outputs** fields.
7335**
7336** ^(The aConstraint[] array records WHERE clause constraints of the form:
7337**
7338** <blockquote>column OP expr</blockquote>
7339**
7340** where OP is =, &lt;, &lt;=, &gt;, or &gt;=.)^ ^(The particular operator is
7341** stored in aConstraint[].op using one of the
7342** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^
7343** ^(The index of the column is stored in
7344** aConstraint[].iColumn.)^ ^(aConstraint[].usable is TRUE if the
7345** expr on the right-hand side can be evaluated (and thus the constraint
7346** is usable) and false if it cannot.)^
7347**
7348** ^The optimizer automatically inverts terms of the form "expr OP column"
7349** and makes other simplifications to the WHERE clause in an attempt to
7350** get as many WHERE clause terms into the form shown above as possible.
7351** ^The aConstraint[] array only reports WHERE clause terms that are
7352** relevant to the particular virtual table being queried.
7353**
7354** ^Information about the ORDER BY clause is stored in aOrderBy[].
7355** ^Each term of aOrderBy records a column of the ORDER BY clause.
7356**
7357** The colUsed field indicates which columns of the virtual table may be
7358** required by the current scan. Virtual table columns are numbered from
7359** zero in the order in which they appear within the CREATE TABLE statement
7360** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62),
7361** the corresponding bit is set within the colUsed mask if the column may be
7362** required by SQLite. If the table has at least 64 columns and any column
7363** to the right of the first 63 is required, then bit 63 of colUsed is also
7364** set. In other words, column iCol may be required if the expression
7365** (colUsed & ((sqlite3_uint64)1 << (iCol>=63 ? 63 : iCol))) evaluates to
7366** non-zero.
7367**
7368** The [xBestIndex] method must fill aConstraintUsage[] with information
7369** about what parameters to pass to xFilter. ^If argvIndex>0 then
7370** the right-hand side of the corresponding aConstraint[] is evaluated
7371** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit
7372** is true, then the constraint is assumed to be fully handled by the
7373** virtual table and might not be checked again by the byte code.)^ ^(The
7374** aConstraintUsage[].omit flag is an optimization hint. When the omit flag
7375** is left in its default setting of false, the constraint will always be
7376** checked separately in byte code. If the omit flag is change to true, then
7377** the constraint may or may not be checked in byte code. In other words,
7378** when the omit flag is true there is no guarantee that the constraint will
7379** not be checked again using byte code.)^
7380**
7381** ^The idxNum and idxPtr values are recorded and passed into the
7382** [xFilter] method.
7383** ^[sqlite3_free()] is used to free idxPtr if and only if
7384** needToFreeIdxPtr is true.
7385**
7386** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in
7387** the correct order to satisfy the ORDER BY clause so that no separate
7388** sorting step is required.
7389**
7390** ^The estimatedCost value is an estimate of the cost of a particular
7391** strategy. A cost of N indicates that the cost of the strategy is similar
7392** to a linear scan of an SQLite table with N rows. A cost of log(N)
7393** indicates that the expense of the operation is similar to that of a
7394** binary search on a unique indexed field of an SQLite table with N rows.
7395**
7396** ^The estimatedRows value is an estimate of the number of rows that
7397** will be returned by the strategy.
7398**
7399** The xBestIndex method may optionally populate the idxFlags field with a
7400** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag -
7401** SQLITE_INDEX_SCAN_UNIQUE. If the xBestIndex method sets this flag, SQLite
7402** assumes that the strategy may visit at most one row.
7403**
7404** Additionally, if xBestIndex sets the SQLITE_INDEX_SCAN_UNIQUE flag, then
7405** SQLite also assumes that if a call to the xUpdate() method is made as
7406** part of the same statement to delete or update a virtual table row and the
7407** implementation returns SQLITE_CONSTRAINT, then there is no need to rollback
7408** any database changes. In other words, if the xUpdate() returns
7409** SQLITE_CONSTRAINT, the database contents must be exactly as they were
7410** before xUpdate was called. By contrast, if SQLITE_INDEX_SCAN_UNIQUE is not
7411** set and xUpdate returns SQLITE_CONSTRAINT, any database changes made by
7412** the xUpdate method are automatically rolled back by SQLite.
7413**
7414** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info
7415** structure for SQLite [version 3.8.2] ([dateof:3.8.2]).
7416** If a virtual table extension is
7417** used with an SQLite version earlier than 3.8.2, the results of attempting
7418** to read or write the estimatedRows field are undefined (but are likely
7419** to include crashing the application). The estimatedRows field should
7420** therefore only be used if [sqlite3_libversion_number()] returns a
7421** value greater than or equal to 3008002. Similarly, the idxFlags field
7422** was added for [version 3.9.0] ([dateof:3.9.0]).
7423** It may therefore only be used if
7424** sqlite3_libversion_number() returns a value greater than or equal to
7425** 3009000.
7426*/
7427struct sqlite3_index_info {
7428 /* Inputs */
7429 int nConstraint; /* Number of entries in aConstraint */
7430 struct sqlite3_index_constraint {
7431 int iColumn; /* Column constrained. -1 for ROWID */
7432 unsigned char op; /* Constraint operator */
7433 unsigned char usable; /* True if this constraint is usable */
7434 int iTermOffset; /* Used internally - xBestIndex should ignore */
7435 } *aConstraint; /* Table of WHERE clause constraints */
7436 int nOrderBy; /* Number of terms in the ORDER BY clause */
7437 struct sqlite3_index_orderby {
7438 int iColumn; /* Column number */
7439 unsigned char desc; /* True for DESC. False for ASC. */
7440 } *aOrderBy; /* The ORDER BY clause */
7441 /* Outputs */
7442 struct sqlite3_index_constraint_usage {
7443 int argvIndex; /* if >0, constraint is part of argv to xFilter */
7444 unsigned char omit; /* Do not code a test for this constraint */
7445 } *aConstraintUsage;
7446 int idxNum; /* Number used to identify the index */
7447 char *idxStr; /* String, possibly obtained from sqlite3_malloc */
7448 int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */
7449 int orderByConsumed; /* True if output is already ordered */
7450 double estimatedCost; /* Estimated cost of using this index */
7451 /* Fields below are only available in SQLite 3.8.2 and later */
7452 sqlite3_int64 estimatedRows; /* Estimated number of rows returned */
7453 /* Fields below are only available in SQLite 3.9.0 and later */
7454 int idxFlags; /* Mask of SQLITE_INDEX_SCAN_* flags */
7455 /* Fields below are only available in SQLite 3.10.0 and later */
7456 sqlite3_uint64 colUsed; /* Input: Mask of columns used by statement */
7457};
7458
7459/*
7460** CAPI3REF: Virtual Table Scan Flags
7461**
7462** Virtual table implementations are allowed to set the
7463** [sqlite3_index_info].idxFlags field to some combination of
7464** these bits.
7465*/
7466#define SQLITE_INDEX_SCAN_UNIQUE1 1 /* Scan visits at most 1 row */
7467
7468/*
7469** CAPI3REF: Virtual Table Constraint Operator Codes
7470**
7471** These macros define the allowed values for the
7472** [sqlite3_index_info].aConstraint[].op field. Each value represents
7473** an operator that is part of a constraint term in the WHERE clause of
7474** a query that uses a [virtual table].
7475**
7476** ^The left-hand operand of the operator is given by the corresponding
7477** aConstraint[].iColumn field. ^An iColumn of -1 indicates the left-hand
7478** operand is the rowid.
7479** The SQLITE_INDEX_CONSTRAINT_LIMIT and SQLITE_INDEX_CONSTRAINT_OFFSET
7480** operators have no left-hand operand, and so for those operators the
7481** corresponding aConstraint[].iColumn is meaningless and should not be
7482** used.
7483**
7484** All operator values from SQLITE_INDEX_CONSTRAINT_FUNCTION through
7485** value 255 are reserved to represent functions that are overloaded
7486** by the [xFindFunction|xFindFunction method] of the virtual table
7487** implementation.
7488**
7489** The right-hand operands for each constraint might be accessible using
7490** the [sqlite3_vtab_rhs_value()] interface. Usually the right-hand
7491** operand is only available if it appears as a single constant literal
7492** in the input SQL. If the right-hand operand is another column or an
7493** expression (even a constant expression) or a parameter, then the
7494** sqlite3_vtab_rhs_value() probably will not be able to extract it.
7495** ^The SQLITE_INDEX_CONSTRAINT_ISNULL and
7496** SQLITE_INDEX_CONSTRAINT_ISNOTNULL operators have no right-hand operand
7497** and hence calls to sqlite3_vtab_rhs_value() for those operators will
7498** always return SQLITE_NOTFOUND.
7499**
7500** The collating sequence to be used for comparison can be found using
7501** the [sqlite3_vtab_collation()] interface. For most real-world virtual
7502** tables, the collating sequence of constraints does not matter (for example
7503** because the constraints are numeric) and so the sqlite3_vtab_collation()
7504** interface is no commonly needed.
7505*/
7506#define SQLITE_INDEX_CONSTRAINT_EQ2 2
7507#define SQLITE_INDEX_CONSTRAINT_GT4 4
7508#define SQLITE_INDEX_CONSTRAINT_LE8 8
7509#define SQLITE_INDEX_CONSTRAINT_LT16 16
7510#define SQLITE_INDEX_CONSTRAINT_GE32 32
7511#define SQLITE_INDEX_CONSTRAINT_MATCH64 64
7512#define SQLITE_INDEX_CONSTRAINT_LIKE65 65
7513#define SQLITE_INDEX_CONSTRAINT_GLOB66 66
7514#define SQLITE_INDEX_CONSTRAINT_REGEXP67 67
7515#define SQLITE_INDEX_CONSTRAINT_NE68 68
7516#define SQLITE_INDEX_CONSTRAINT_ISNOT69 69
7517#define SQLITE_INDEX_CONSTRAINT_ISNOTNULL70 70
7518#define SQLITE_INDEX_CONSTRAINT_ISNULL71 71
7519#define SQLITE_INDEX_CONSTRAINT_IS72 72
7520#define SQLITE_INDEX_CONSTRAINT_LIMIT73 73
7521#define SQLITE_INDEX_CONSTRAINT_OFFSET74 74
7522#define SQLITE_INDEX_CONSTRAINT_FUNCTION150 150
7523
7524/*
7525** CAPI3REF: Register A Virtual Table Implementation
7526** METHOD: sqlite3
7527**
7528** ^These routines are used to register a new [virtual table module] name.
7529** ^Module names must be registered before
7530** creating a new [virtual table] using the module and before using a
7531** preexisting [virtual table] for the module.
7532**
7533** ^The module name is registered on the [database connection] specified
7534** by the first parameter. ^The name of the module is given by the
7535** second parameter. ^The third parameter is a pointer to
7536** the implementation of the [virtual table module]. ^The fourth
7537** parameter is an arbitrary client data pointer that is passed through
7538** into the [xCreate] and [xConnect] methods of the virtual table module
7539** when a new virtual table is be being created or reinitialized.
7540**
7541** ^The sqlite3_create_module_v2() interface has a fifth parameter which
7542** is a pointer to a destructor for the pClientData. ^SQLite will
7543** invoke the destructor function (if it is not NULL) when SQLite
7544** no longer needs the pClientData pointer. ^The destructor will also
7545** be invoked if the call to sqlite3_create_module_v2() fails.
7546** ^The sqlite3_create_module()
7547** interface is equivalent to sqlite3_create_module_v2() with a NULL
7548** destructor.
7549**
7550** ^If the third parameter (the pointer to the sqlite3_module object) is
7551** NULL then no new module is created and any existing modules with the
7552** same name are dropped.
7553**
7554** See also: [sqlite3_drop_modules()]
7555*/
7556SQLITE_API int sqlite3_create_module(
7557 sqlite3 *db, /* SQLite connection to register module with */
7558 const char *zName, /* Name of the module */
7559 const sqlite3_module *p, /* Methods for the module */
7560 void *pClientData /* Client data for xCreate/xConnect */
7561);
7562SQLITE_API int sqlite3_create_module_v2(
7563 sqlite3 *db, /* SQLite connection to register module with */
7564 const char *zName, /* Name of the module */
7565 const sqlite3_module *p, /* Methods for the module */
7566 void *pClientData, /* Client data for xCreate/xConnect */
7567 void(*xDestroy)(void*) /* Module destructor function */
7568);
7569
7570/*
7571** CAPI3REF: Remove Unnecessary Virtual Table Implementations
7572** METHOD: sqlite3
7573**
7574** ^The sqlite3_drop_modules(D,L) interface removes all virtual
7575** table modules from database connection D except those named on list L.
7576** The L parameter must be either NULL or a pointer to an array of pointers
7577** to strings where the array is terminated by a single NULL pointer.
7578** ^If the L parameter is NULL, then all virtual table modules are removed.
7579**
7580** See also: [sqlite3_create_module()]
7581*/
7582SQLITE_API int sqlite3_drop_modules(
7583 sqlite3 *db, /* Remove modules from this connection */
7584 const char **azKeep /* Except, do not remove the ones named here */
7585);
7586
7587/*
7588** CAPI3REF: Virtual Table Instance Object
7589** KEYWORDS: sqlite3_vtab
7590**
7591** Every [virtual table module] implementation uses a subclass
7592** of this object to describe a particular instance
7593** of the [virtual table]. Each subclass will
7594** be tailored to the specific needs of the module implementation.
7595** The purpose of this superclass is to define certain fields that are
7596** common to all module implementations.
7597**
7598** ^Virtual tables methods can set an error message by assigning a
7599** string obtained from [sqlite3_mprintf()] to zErrMsg. The method should
7600** take care that any prior string is freed by a call to [sqlite3_free()]
7601** prior to assigning a new string to zErrMsg. ^After the error message
7602** is delivered up to the client application, the string will be automatically
7603** freed by sqlite3_free() and the zErrMsg field will be zeroed.
7604*/
7605struct sqlite3_vtab {
7606 const sqlite3_module *pModule; /* The module for this virtual table */
7607 int nRef; /* Number of open cursors */
7608 char *zErrMsg; /* Error message from sqlite3_mprintf() */
7609 /* Virtual table implementations will typically add additional fields */
7610};
7611
7612/*
7613** CAPI3REF: Virtual Table Cursor Object
7614** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
7615**
7616** Every [virtual table module] implementation uses a subclass of the
7617** following structure to describe cursors that point into the
7618** [virtual table] and are used
7619** to loop through the virtual table. Cursors are created using the
7620** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed
7621** by the [sqlite3_module.xClose | xClose] method. Cursors are used
7622** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods
7623** of the module. Each module implementation will define
7624** the content of a cursor structure to suit its own needs.
7625**
7626** This superclass exists in order to define fields of the cursor that
7627** are common to all implementations.
7628*/
7629struct sqlite3_vtab_cursor {
7630 sqlite3_vtab *pVtab; /* Virtual table of this cursor */
7631 /* Virtual table implementations will typically add additional fields */
7632};
7633
7634/*
7635** CAPI3REF: Declare The Schema Of A Virtual Table
7636**
7637** ^The [xCreate] and [xConnect] methods of a
7638** [virtual table module] call this interface
7639** to declare the format (the names and datatypes of the columns) of
7640** the virtual tables they implement.
7641*/
7642SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
7643
7644/*
7645** CAPI3REF: Overload A Function For A Virtual Table
7646** METHOD: sqlite3
7647**
7648** ^(Virtual tables can provide alternative implementations of functions
7649** using the [xFindFunction] method of the [virtual table module].
7650** But global versions of those functions
7651** must exist in order to be overloaded.)^
7652**
7653** ^(This API makes sure a global version of a function with a particular
7654** name and number of parameters exists. If no such function exists
7655** before this API is called, a new function is created.)^ ^The implementation
7656** of the new function always causes an exception to be thrown. So
7657** the new function is not good for anything by itself. Its only
7658** purpose is to be a placeholder function that can be overloaded
7659** by a [virtual table].
7660*/
7661SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
7662
7663/*
7664** The interface to the virtual-table mechanism defined above (back up
7665** to a comment remarkably similar to this one) is currently considered
7666** to be experimental. The interface might change in incompatible ways.
7667** If this is a problem for you, do not use the interface at this time.
7668**
7669** When the virtual-table mechanism stabilizes, we will declare the
7670** interface fixed, support it indefinitely, and remove this comment.
7671*/
7672
7673/*
7674** CAPI3REF: A Handle To An Open BLOB
7675** KEYWORDS: {BLOB handle} {BLOB handles}
7676**
7677** An instance of this object represents an open BLOB on which
7678** [sqlite3_blob_open | incremental BLOB I/O] can be performed.
7679** ^Objects of this type are created by [sqlite3_blob_open()]
7680** and destroyed by [sqlite3_blob_close()].
7681** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
7682** can be used to read or write small subsections of the BLOB.
7683** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
7684*/
7685typedef struct sqlite3_blob sqlite3_blob;
7686
7687/*
7688** CAPI3REF: Open A BLOB For Incremental I/O
7689** METHOD: sqlite3
7690** CONSTRUCTOR: sqlite3_blob
7691**
7692** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
7693** in row iRow, column zColumn, table zTable in database zDb;
7694** in other words, the same BLOB that would be selected by:
7695**
7696** <pre>
7697** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
7698** </pre>)^
7699**
7700** ^(Parameter zDb is not the filename that contains the database, but
7701** rather the symbolic name of the database. For attached databases, this is
7702** the name that appears after the AS keyword in the [ATTACH] statement.
7703** For the main database file, the database name is "main". For TEMP
7704** tables, the database name is "temp".)^
7705**
7706** ^If the flags parameter is non-zero, then the BLOB is opened for read
7707** and write access. ^If the flags parameter is zero, the BLOB is opened for
7708** read-only access.
7709**
7710** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is stored
7711** in *ppBlob. Otherwise an [error code] is returned and, unless the error
7712** code is SQLITE_MISUSE, *ppBlob is set to NULL.)^ ^This means that, provided
7713** the API is not misused, it is always safe to call [sqlite3_blob_close()]
7714** on *ppBlob after this function it returns.
7715**
7716** This function fails with SQLITE_ERROR if any of the following are true:
7717** <ul>
7718** <li> ^(Database zDb does not exist)^,
7719** <li> ^(Table zTable does not exist within database zDb)^,
7720** <li> ^(Table zTable is a WITHOUT ROWID table)^,
7721** <li> ^(Column zColumn does not exist)^,
7722** <li> ^(Row iRow is not present in the table)^,
7723** <li> ^(The specified column of row iRow contains a value that is not
7724** a TEXT or BLOB value)^,
7725** <li> ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE
7726** constraint and the blob is being opened for read/write access)^,
7727** <li> ^([foreign key constraints | Foreign key constraints] are enabled,
7728** column zColumn is part of a [child key] definition and the blob is
7729** being opened for read/write access)^.
7730** </ul>
7731**
7732** ^Unless it returns SQLITE_MISUSE, this function sets the
7733** [database connection] error code and message accessible via
7734** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
7735**
7736** A BLOB referenced by sqlite3_blob_open() may be read using the
7737** [sqlite3_blob_read()] interface and modified by using
7738** [sqlite3_blob_write()]. The [BLOB handle] can be moved to a
7739** different row of the same table using the [sqlite3_blob_reopen()]
7740** interface. However, the column, table, or database of a [BLOB handle]
7741** cannot be changed after the [BLOB handle] is opened.
7742**
7743** ^(If the row that a BLOB handle points to is modified by an
7744** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
7745** then the BLOB handle is marked as "expired".
7746** This is true if any column of the row is changed, even a column
7747** other than the one the BLOB handle is open on.)^
7748** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
7749** an expired BLOB handle fail with a return code of [SQLITE_ABORT].
7750** ^(Changes written into a BLOB prior to the BLOB expiring are not
7751** rolled back by the expiration of the BLOB. Such changes will eventually
7752** commit if the transaction continues to completion.)^
7753**
7754** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
7755** the opened blob. ^The size of a blob may not be changed by this
7756** interface. Use the [UPDATE] SQL command to change the size of a
7757** blob.
7758**
7759** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
7760** and the built-in [zeroblob] SQL function may be used to create a
7761** zero-filled blob to read or write using the incremental-blob interface.
7762**
7763** To avoid a resource leak, every open [BLOB handle] should eventually
7764** be released by a call to [sqlite3_blob_close()].
7765**
7766** See also: [sqlite3_blob_close()],
7767** [sqlite3_blob_reopen()], [sqlite3_blob_read()],
7768** [sqlite3_blob_bytes()], [sqlite3_blob_write()].
7769*/
7770SQLITE_API int sqlite3_blob_open(
7771 sqlite3*,
7772 const char *zDb,
7773 const char *zTable,
7774 const char *zColumn,
7775 sqlite3_int64 iRow,
7776 int flags,
7777 sqlite3_blob **ppBlob
7778);
7779
7780/*
7781** CAPI3REF: Move a BLOB Handle to a New Row
7782** METHOD: sqlite3_blob
7783**
7784** ^This function is used to move an existing [BLOB handle] so that it points
7785** to a different row of the same database table. ^The new row is identified
7786** by the rowid value passed as the second argument. Only the row can be
7787** changed. ^The database, table and column on which the blob handle is open
7788** remain the same. Moving an existing [BLOB handle] to a new row is
7789** faster than closing the existing handle and opening a new one.
7790**
7791** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
7792** it must exist and there must be either a blob or text value stored in
7793** the nominated column.)^ ^If the new row is not present in the table, or if
7794** it does not contain a blob or text value, or if another error occurs, an
7795** SQLite error code is returned and the blob handle is considered aborted.
7796** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or
7797** [sqlite3_blob_reopen()] on an aborted blob handle immediately return
7798** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
7799** always returns zero.
7800**
7801** ^This function sets the database handle error code and message.
7802*/
7803SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
7804
7805/*
7806** CAPI3REF: Close A BLOB Handle
7807** DESTRUCTOR: sqlite3_blob
7808**
7809** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed
7810** unconditionally. Even if this routine returns an error code, the
7811** handle is still closed.)^
7812**
7813** ^If the blob handle being closed was opened for read-write access, and if
7814** the database is in auto-commit mode and there are no other open read-write
7815** blob handles or active write statements, the current transaction is
7816** committed. ^If an error occurs while committing the transaction, an error
7817** code is returned and the transaction rolled back.
7818**
7819** Calling this function with an argument that is not a NULL pointer or an
7820** open blob handle results in undefined behaviour. ^Calling this routine
7821** with a null pointer (such as would be returned by a failed call to
7822** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
7823** is passed a valid open blob handle, the values returned by the
7824** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning.
7825*/
7826SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
7827
7828/*
7829** CAPI3REF: Return The Size Of An Open BLOB
7830** METHOD: sqlite3_blob
7831**
7832** ^Returns the size in bytes of the BLOB accessible via the
7833** successfully opened [BLOB handle] in its only argument. ^The
7834** incremental blob I/O routines can only read or overwriting existing
7835** blob content; they cannot change the size of a blob.
7836**
7837** This routine only works on a [BLOB handle] which has been created
7838** by a prior successful call to [sqlite3_blob_open()] and which has not
7839** been closed by [sqlite3_blob_close()]. Passing any other pointer in
7840** to this routine results in undefined and probably undesirable behavior.
7841*/
7842SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
7843
7844/*
7845** CAPI3REF: Read Data From A BLOB Incrementally
7846** METHOD: sqlite3_blob
7847**
7848** ^(This function is used to read data from an open [BLOB handle] into a
7849** caller-supplied buffer. N bytes of data are copied into buffer Z
7850** from the open BLOB, starting at offset iOffset.)^
7851**
7852** ^If offset iOffset is less than N bytes from the end of the BLOB,
7853** [SQLITE_ERROR] is returned and no data is read. ^If N or iOffset is
7854** less than zero, [SQLITE_ERROR] is returned and no data is read.
7855** ^The size of the blob (and hence the maximum value of N+iOffset)
7856** can be determined using the [sqlite3_blob_bytes()] interface.
7857**
7858** ^An attempt to read from an expired [BLOB handle] fails with an
7859** error code of [SQLITE_ABORT].
7860**
7861** ^(On success, sqlite3_blob_read() returns SQLITE_OK.
7862** Otherwise, an [error code] or an [extended error code] is returned.)^
7863**
7864** This routine only works on a [BLOB handle] which has been created
7865** by a prior successful call to [sqlite3_blob_open()] and which has not
7866** been closed by [sqlite3_blob_close()]. Passing any other pointer in
7867** to this routine results in undefined and probably undesirable behavior.
7868**
7869** See also: [sqlite3_blob_write()].
7870*/
7871SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
7872
7873/*
7874** CAPI3REF: Write Data Into A BLOB Incrementally
7875** METHOD: sqlite3_blob
7876**
7877** ^(This function is used to write data into an open [BLOB handle] from a
7878** caller-supplied buffer. N bytes of data are copied from the buffer Z
7879** into the open BLOB, starting at offset iOffset.)^
7880**
7881** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
7882** Otherwise, an [error code] or an [extended error code] is returned.)^
7883** ^Unless SQLITE_MISUSE is returned, this function sets the
7884** [database connection] error code and message accessible via
7885** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
7886**
7887** ^If the [BLOB handle] passed as the first argument was not opened for
7888** writing (the flags parameter to [sqlite3_blob_open()] was zero),
7889** this function returns [SQLITE_READONLY].
7890**
7891** This function may only modify the contents of the BLOB; it is
7892** not possible to increase the size of a BLOB using this API.
7893** ^If offset iOffset is less than N bytes from the end of the BLOB,
7894** [SQLITE_ERROR] is returned and no data is written. The size of the
7895** BLOB (and hence the maximum value of N+iOffset) can be determined
7896** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less
7897** than zero [SQLITE_ERROR] is returned and no data is written.
7898**
7899** ^An attempt to write to an expired [BLOB handle] fails with an
7900** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred
7901** before the [BLOB handle] expired are not rolled back by the
7902** expiration of the handle, though of course those changes might
7903** have been overwritten by the statement that expired the BLOB handle
7904** or by other independent statements.
7905**
7906** This routine only works on a [BLOB handle] which has been created
7907** by a prior successful call to [sqlite3_blob_open()] and which has not
7908** been closed by [sqlite3_blob_close()]. Passing any other pointer in
7909** to this routine results in undefined and probably undesirable behavior.
7910**
7911** See also: [sqlite3_blob_read()].
7912*/
7913SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
7914
7915/*
7916** CAPI3REF: Virtual File System Objects
7917**
7918** A virtual filesystem (VFS) is an [sqlite3_vfs] object
7919** that SQLite uses to interact
7920** with the underlying operating system. Most SQLite builds come with a
7921** single default VFS that is appropriate for the host computer.
7922** New VFSes can be registered and existing VFSes can be unregistered.
7923** The following interfaces are provided.
7924**
7925** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
7926** ^Names are case sensitive.
7927** ^Names are zero-terminated UTF-8 strings.
7928** ^If there is no match, a NULL pointer is returned.
7929** ^If zVfsName is NULL then the default VFS is returned.
7930**
7931** ^New VFSes are registered with sqlite3_vfs_register().
7932** ^Each new VFS becomes the default VFS if the makeDflt flag is set.
7933** ^The same VFS can be registered multiple times without injury.
7934** ^To make an existing VFS into the default VFS, register it again
7935** with the makeDflt flag set. If two different VFSes with the
7936** same name are registered, the behavior is undefined. If a
7937** VFS is registered with a name that is NULL or an empty string,
7938** then the behavior is undefined.
7939**
7940** ^Unregister a VFS with the sqlite3_vfs_unregister() interface.
7941** ^(If the default VFS is unregistered, another VFS is chosen as
7942** the default. The choice for the new VFS is arbitrary.)^
7943*/
7944SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
7945SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
7946SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
7947
7948/*
7949** CAPI3REF: Mutexes
7950**
7951** The SQLite core uses these routines for thread
7952** synchronization. Though they are intended for internal
7953** use by SQLite, code that links against SQLite is
7954** permitted to use any of these routines.
7955**
7956** The SQLite source code contains multiple implementations
7957** of these mutex routines. An appropriate implementation
7958** is selected automatically at compile-time. The following
7959** implementations are available in the SQLite core:
7960**
7961** <ul>
7962** <li> SQLITE_MUTEX_PTHREADS
7963** <li> SQLITE_MUTEX_W32
7964** <li> SQLITE_MUTEX_NOOP
7965** </ul>
7966**
7967** The SQLITE_MUTEX_NOOP implementation is a set of routines
7968** that does no real locking and is appropriate for use in
7969** a single-threaded application. The SQLITE_MUTEX_PTHREADS and
7970** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix
7971** and Windows.
7972**
7973** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
7974** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
7975** implementation is included with the library. In this case the
7976** application must supply a custom mutex implementation using the
7977** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
7978** before calling sqlite3_initialize() or any other public sqlite3_
7979** function that calls sqlite3_initialize().
7980**
7981** ^The sqlite3_mutex_alloc() routine allocates a new
7982** mutex and returns a pointer to it. ^The sqlite3_mutex_alloc()
7983** routine returns NULL if it is unable to allocate the requested
7984** mutex. The argument to sqlite3_mutex_alloc() must one of these
7985** integer constants:
7986**
7987** <ul>
7988** <li> SQLITE_MUTEX_FAST
7989** <li> SQLITE_MUTEX_RECURSIVE
7990** <li> SQLITE_MUTEX_STATIC_MAIN
7991** <li> SQLITE_MUTEX_STATIC_MEM
7992** <li> SQLITE_MUTEX_STATIC_OPEN
7993** <li> SQLITE_MUTEX_STATIC_PRNG
7994** <li> SQLITE_MUTEX_STATIC_LRU
7995** <li> SQLITE_MUTEX_STATIC_PMEM
7996** <li> SQLITE_MUTEX_STATIC_APP1
7997** <li> SQLITE_MUTEX_STATIC_APP2
7998** <li> SQLITE_MUTEX_STATIC_APP3
7999** <li> SQLITE_MUTEX_STATIC_VFS1
8000** <li> SQLITE_MUTEX_STATIC_VFS2
8001** <li> SQLITE_MUTEX_STATIC_VFS3
8002** </ul>
8003**
8004** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
8005** cause sqlite3_mutex_alloc() to create
8006** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
8007** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
8008** The mutex implementation does not need to make a distinction
8009** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
8010** not want to. SQLite will only request a recursive mutex in
8011** cases where it really needs one. If a faster non-recursive mutex
8012** implementation is available on the host platform, the mutex subsystem
8013** might return such a mutex in response to SQLITE_MUTEX_FAST.
8014**
8015** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other
8016** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return
8017** a pointer to a static preexisting mutex. ^Nine static mutexes are
8018** used by the current version of SQLite. Future versions of SQLite
8019** may add additional static mutexes. Static mutexes are for internal
8020** use by SQLite only. Applications that use SQLite mutexes should
8021** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
8022** SQLITE_MUTEX_RECURSIVE.
8023**
8024** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
8025** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
8026** returns a different mutex on every call. ^For the static
8027** mutex types, the same mutex is returned on every call that has
8028** the same type number.
8029**
8030** ^The sqlite3_mutex_free() routine deallocates a previously
8031** allocated dynamic mutex. Attempting to deallocate a static
8032** mutex results in undefined behavior.
8033**
8034** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
8035** to enter a mutex. ^If another thread is already within the mutex,
8036** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
8037** SQLITE_BUSY. ^The sqlite3_mutex_try() interface returns [SQLITE_OK]
8038** upon successful entry. ^(Mutexes created using
8039** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread.
8040** In such cases, the
8041** mutex must be exited an equal number of times before another thread
8042** can enter.)^ If the same thread tries to enter any mutex other
8043** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined.
8044**
8045** ^(Some systems (for example, Windows 95) do not support the operation
8046** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try()
8047** will always return SQLITE_BUSY. The SQLite core only ever uses
8048** sqlite3_mutex_try() as an optimization so this is acceptable
8049** behavior.)^
8050**
8051** ^The sqlite3_mutex_leave() routine exits a mutex that was
8052** previously entered by the same thread. The behavior
8053** is undefined if the mutex is not currently entered by the
8054** calling thread or is not currently allocated.
8055**
8056** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or
8057** sqlite3_mutex_leave() is a NULL pointer, then all three routines
8058** behave as no-ops.
8059**
8060** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
8061*/
8062SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
8063SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
8064SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
8065SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
8066SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
8067
8068/*
8069** CAPI3REF: Mutex Methods Object
8070**
8071** An instance of this structure defines the low-level routines
8072** used to allocate and use mutexes.
8073**
8074** Usually, the default mutex implementations provided by SQLite are
8075** sufficient, however the application has the option of substituting a custom
8076** implementation for specialized deployments or systems for which SQLite
8077** does not provide a suitable implementation. In this case, the application
8078** creates and populates an instance of this structure to pass
8079** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option.
8080** Additionally, an instance of this structure can be used as an
8081** output variable when querying the system for the current mutex
8082** implementation, using the [SQLITE_CONFIG_GETMUTEX] option.
8083**
8084** ^The xMutexInit method defined by this structure is invoked as
8085** part of system initialization by the sqlite3_initialize() function.
8086** ^The xMutexInit routine is called by SQLite exactly once for each
8087** effective call to [sqlite3_initialize()].
8088**
8089** ^The xMutexEnd method defined by this structure is invoked as
8090** part of system shutdown by the sqlite3_shutdown() function. The
8091** implementation of this method is expected to release all outstanding
8092** resources obtained by the mutex methods implementation, especially
8093** those obtained by the xMutexInit method. ^The xMutexEnd()
8094** interface is invoked exactly once for each call to [sqlite3_shutdown()].
8095**
8096** ^(The remaining seven methods defined by this structure (xMutexAlloc,
8097** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
8098** xMutexNotheld) implement the following interfaces (respectively):
8099**
8100** <ul>
8101** <li> [sqlite3_mutex_alloc()] </li>
8102** <li> [sqlite3_mutex_free()] </li>
8103** <li> [sqlite3_mutex_enter()] </li>
8104** <li> [sqlite3_mutex_try()] </li>
8105** <li> [sqlite3_mutex_leave()] </li>
8106** <li> [sqlite3_mutex_held()] </li>
8107** <li> [sqlite3_mutex_notheld()] </li>
8108** </ul>)^
8109**
8110** The only difference is that the public sqlite3_XXX functions enumerated
8111** above silently ignore any invocations that pass a NULL pointer instead
8112** of a valid mutex handle. The implementations of the methods defined
8113** by this structure are not required to handle this case. The results
8114** of passing a NULL pointer instead of a valid mutex handle are undefined
8115** (i.e. it is acceptable to provide an implementation that segfaults if
8116** it is passed a NULL pointer).
8117**
8118** The xMutexInit() method must be threadsafe. It must be harmless to
8119** invoke xMutexInit() multiple times within the same process and without
8120** intervening calls to xMutexEnd(). Second and subsequent calls to
8121** xMutexInit() must be no-ops.
8122**
8123** xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
8124** and its associates). Similarly, xMutexAlloc() must not use SQLite memory
8125** allocation for a static mutex. ^However xMutexAlloc() may use SQLite
8126** memory allocation for a fast or recursive mutex.
8127**
8128** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
8129** called, but only if the prior call to xMutexInit returned SQLITE_OK.
8130** If xMutexInit fails in any way, it is expected to clean up after itself
8131** prior to returning.
8132*/
8133typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
8134struct sqlite3_mutex_methods {
8135 int (*xMutexInit)(void);
8136 int (*xMutexEnd)(void);
8137 sqlite3_mutex *(*xMutexAlloc)(int);
8138 void (*xMutexFree)(sqlite3_mutex *);
8139 void (*xMutexEnter)(sqlite3_mutex *);
8140 int (*xMutexTry)(sqlite3_mutex *);
8141 void (*xMutexLeave)(sqlite3_mutex *);
8142 int (*xMutexHeld)(sqlite3_mutex *);
8143 int (*xMutexNotheld)(sqlite3_mutex *);
8144};
8145
8146/*
8147** CAPI3REF: Mutex Verification Routines
8148**
8149** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
8150** are intended for use inside assert() statements. The SQLite core
8151** never uses these routines except inside an assert() and applications
8152** are advised to follow the lead of the core. The SQLite core only
8153** provides implementations for these routines when it is compiled
8154** with the SQLITE_DEBUG flag. External mutex implementations
8155** are only required to provide these routines if SQLITE_DEBUG is
8156** defined and if NDEBUG is not defined.
8157**
8158** These routines should return true if the mutex in their argument
8159** is held or not held, respectively, by the calling thread.
8160**
8161** The implementation is not required to provide versions of these
8162** routines that actually work. If the implementation does not provide working
8163** versions of these routines, it should at least provide stubs that always
8164** return true so that one does not get spurious assertion failures.
8165**
8166** If the argument to sqlite3_mutex_held() is a NULL pointer then
8167** the routine should return 1. This seems counter-intuitive since
8168** clearly the mutex cannot be held if it does not exist. But
8169** the reason the mutex does not exist is because the build is not
8170** using mutexes. And we do not want the assert() containing the
8171** call to sqlite3_mutex_held() to fail, so a non-zero return is
8172** the appropriate thing to do. The sqlite3_mutex_notheld()
8173** interface should also return 1 when given a NULL pointer.
8174*/
8175#ifndef NDEBUG1
8176SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
8177SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
8178#endif
8179
8180/*
8181** CAPI3REF: Mutex Types
8182**
8183** The [sqlite3_mutex_alloc()] interface takes a single argument
8184** which is one of these integer constants.
8185**
8186** The set of static mutexes may change from one SQLite release to the
8187** next. Applications that override the built-in mutex logic must be
8188** prepared to accommodate additional static mutexes.
8189*/
8190#define SQLITE_MUTEX_FAST0 0
8191#define SQLITE_MUTEX_RECURSIVE1 1
8192#define SQLITE_MUTEX_STATIC_MAIN2 2
8193#define SQLITE_MUTEX_STATIC_MEM3 3 /* sqlite3_malloc() */
8194#define SQLITE_MUTEX_STATIC_MEM24 4 /* NOT USED */
8195#define SQLITE_MUTEX_STATIC_OPEN4 4 /* sqlite3BtreeOpen() */
8196#define SQLITE_MUTEX_STATIC_PRNG5 5 /* sqlite3_randomness() */
8197#define SQLITE_MUTEX_STATIC_LRU6 6 /* lru page list */
8198#define SQLITE_MUTEX_STATIC_LRU27 7 /* NOT USED */
8199#define SQLITE_MUTEX_STATIC_PMEM7 7 /* sqlite3PageMalloc() */
8200#define SQLITE_MUTEX_STATIC_APP18 8 /* For use by application */
8201#define SQLITE_MUTEX_STATIC_APP29 9 /* For use by application */
8202#define SQLITE_MUTEX_STATIC_APP310 10 /* For use by application */
8203#define SQLITE_MUTEX_STATIC_VFS111 11 /* For use by built-in VFS */
8204#define SQLITE_MUTEX_STATIC_VFS212 12 /* For use by extension VFS */
8205#define SQLITE_MUTEX_STATIC_VFS313 13 /* For use by application VFS */
8206
8207/* Legacy compatibility: */
8208#define SQLITE_MUTEX_STATIC_MASTER2 2
8209
8210
8211/*
8212** CAPI3REF: Retrieve the mutex for a database connection
8213** METHOD: sqlite3
8214**
8215** ^This interface returns a pointer the [sqlite3_mutex] object that
8216** serializes access to the [database connection] given in the argument
8217** when the [threading mode] is Serialized.
8218** ^If the [threading mode] is Single-thread or Multi-thread then this
8219** routine returns a NULL pointer.
8220*/
8221SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
8222
8223/*
8224** CAPI3REF: Low-Level Control Of Database Files
8225** METHOD: sqlite3
8226** KEYWORDS: {file control}
8227**
8228** ^The [sqlite3_file_control()] interface makes a direct call to the
8229** xFileControl method for the [sqlite3_io_methods] object associated
8230** with a particular database identified by the second argument. ^The
8231** name of the database is "main" for the main database or "temp" for the
8232** TEMP database, or the name that appears after the AS keyword for
8233** databases that are added using the [ATTACH] SQL command.
8234** ^A NULL pointer can be used in place of "main" to refer to the
8235** main database file.
8236** ^The third and fourth parameters to this routine
8237** are passed directly through to the second and third parameters of
8238** the xFileControl method. ^The return value of the xFileControl
8239** method becomes the return value of this routine.
8240**
8241** A few opcodes for [sqlite3_file_control()] are handled directly
8242** by the SQLite core and never invoke the
8243** sqlite3_io_methods.xFileControl method.
8244** ^The [SQLITE_FCNTL_FILE_POINTER] value for the op parameter causes
8245** a pointer to the underlying [sqlite3_file] object to be written into
8246** the space pointed to by the 4th parameter. The
8247** [SQLITE_FCNTL_JOURNAL_POINTER] works similarly except that it returns
8248** the [sqlite3_file] object associated with the journal file instead of
8249** the main database. The [SQLITE_FCNTL_VFS_POINTER] opcode returns
8250** a pointer to the underlying [sqlite3_vfs] object for the file.
8251** The [SQLITE_FCNTL_DATA_VERSION] returns the data version counter
8252** from the pager.
8253**
8254** ^If the second parameter (zDbName) does not match the name of any
8255** open database file, then SQLITE_ERROR is returned. ^This error
8256** code is not remembered and will not be recalled by [sqlite3_errcode()]
8257** or [sqlite3_errmsg()]. The underlying xFileControl method might
8258** also return SQLITE_ERROR. There is no way to distinguish between
8259** an incorrect zDbName and an SQLITE_ERROR return from the underlying
8260** xFileControl method.
8261**
8262** See also: [file control opcodes]
8263*/
8264SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
8265
8266/*
8267** CAPI3REF: Testing Interface
8268**
8269** ^The sqlite3_test_control() interface is used to read out internal
8270** state of SQLite and to inject faults into SQLite for testing
8271** purposes. ^The first parameter is an operation code that determines
8272** the number, meaning, and operation of all subsequent parameters.
8273**
8274** This interface is not for use by applications. It exists solely
8275** for verifying the correct operation of the SQLite library. Depending
8276** on how the SQLite library is compiled, this interface might not exist.
8277**
8278** The details of the operation codes, their meanings, the parameters
8279** they take, and what they do are all subject to change without notice.
8280** Unlike most of the SQLite API, this function is not guaranteed to
8281** operate consistently from one release to the next.
8282*/
8283SQLITE_API int sqlite3_test_control(int op, ...);
8284
8285/*
8286** CAPI3REF: Testing Interface Operation Codes
8287**
8288** These constants are the valid operation code parameters used
8289** as the first argument to [sqlite3_test_control()].
8290**
8291** These parameters and their meanings are subject to change
8292** without notice. These values are for testing purposes only.
8293** Applications should not use any of these parameters or the
8294** [sqlite3_test_control()] interface.
8295*/
8296#define SQLITE_TESTCTRL_FIRST5 5
8297#define SQLITE_TESTCTRL_PRNG_SAVE5 5
8298#define SQLITE_TESTCTRL_PRNG_RESTORE6 6
8299#define SQLITE_TESTCTRL_PRNG_RESET7 7 /* NOT USED */
8300#define SQLITE_TESTCTRL_BITVEC_TEST8 8
8301#define SQLITE_TESTCTRL_FAULT_INSTALL9 9
8302#define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS10 10
8303#define SQLITE_TESTCTRL_PENDING_BYTE11 11
8304#define SQLITE_TESTCTRL_ASSERT12 12
8305#define SQLITE_TESTCTRL_ALWAYS13 13
8306#define SQLITE_TESTCTRL_RESERVE14 14 /* NOT USED */
8307#define SQLITE_TESTCTRL_OPTIMIZATIONS15 15
8308#define SQLITE_TESTCTRL_ISKEYWORD16 16 /* NOT USED */
8309#define SQLITE_TESTCTRL_SCRATCHMALLOC17 17 /* NOT USED */
8310#define SQLITE_TESTCTRL_INTERNAL_FUNCTIONS17 17
8311#define SQLITE_TESTCTRL_LOCALTIME_FAULT18 18
8312#define SQLITE_TESTCTRL_EXPLAIN_STMT19 19 /* NOT USED */
8313#define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD19 19
8314#define SQLITE_TESTCTRL_NEVER_CORRUPT20 20
8315#define SQLITE_TESTCTRL_VDBE_COVERAGE21 21
8316#define SQLITE_TESTCTRL_BYTEORDER22 22
8317#define SQLITE_TESTCTRL_ISINIT23 23
8318#define SQLITE_TESTCTRL_SORTER_MMAP24 24
8319#define SQLITE_TESTCTRL_IMPOSTER25 25
8320#define SQLITE_TESTCTRL_PARSER_COVERAGE26 26
8321#define SQLITE_TESTCTRL_RESULT_INTREAL27 27
8322#define SQLITE_TESTCTRL_PRNG_SEED28 28
8323#define SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS29 29
8324#define SQLITE_TESTCTRL_SEEK_COUNT30 30
8325#define SQLITE_TESTCTRL_TRACEFLAGS31 31
8326#define SQLITE_TESTCTRL_TUNE32 32
8327#define SQLITE_TESTCTRL_LOGEST33 33
8328#define SQLITE_TESTCTRL_LAST33 33 /* Largest TESTCTRL */
8329
8330/*
8331** CAPI3REF: SQL Keyword Checking
8332**
8333** These routines provide access to the set of SQL language keywords
8334** recognized by SQLite. Applications can uses these routines to determine
8335** whether or not a specific identifier needs to be escaped (for example,
8336** by enclosing in double-quotes) so as not to confuse the parser.
8337**
8338** The sqlite3_keyword_count() interface returns the number of distinct
8339** keywords understood by SQLite.
8340**
8341** The sqlite3_keyword_name(N,Z,L) interface finds the N-th keyword and
8342** makes *Z point to that keyword expressed as UTF8 and writes the number
8343** of bytes in the keyword into *L. The string that *Z points to is not
8344** zero-terminated. The sqlite3_keyword_name(N,Z,L) routine returns
8345** SQLITE_OK if N is within bounds and SQLITE_ERROR if not. If either Z
8346** or L are NULL or invalid pointers then calls to
8347** sqlite3_keyword_name(N,Z,L) result in undefined behavior.
8348**
8349** The sqlite3_keyword_check(Z,L) interface checks to see whether or not
8350** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero
8351** if it is and zero if not.
8352**
8353** The parser used by SQLite is forgiving. It is often possible to use
8354** a keyword as an identifier as long as such use does not result in a
8355** parsing ambiguity. For example, the statement
8356** "CREATE TABLE BEGIN(REPLACE,PRAGMA,END);" is accepted by SQLite, and
8357** creates a new table named "BEGIN" with three columns named
8358** "REPLACE", "PRAGMA", and "END". Nevertheless, best practice is to avoid
8359** using keywords as identifiers. Common techniques used to avoid keyword
8360** name collisions include:
8361** <ul>
8362** <li> Put all identifier names inside double-quotes. This is the official
8363** SQL way to escape identifier names.
8364** <li> Put identifier names inside &#91;...&#93;. This is not standard SQL,
8365** but it is what SQL Server does and so lots of programmers use this
8366** technique.
8367** <li> Begin every identifier with the letter "Z" as no SQL keywords start
8368** with "Z".
8369** <li> Include a digit somewhere in every identifier name.
8370** </ul>
8371**
8372** Note that the number of keywords understood by SQLite can depend on
8373** compile-time options. For example, "VACUUM" is not a keyword if
8374** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option. Also,
8375** new keywords may be added to future releases of SQLite.
8376*/
8377SQLITE_API int sqlite3_keyword_count(void);
8378SQLITE_API int sqlite3_keyword_name(int,const char**,int*);
8379SQLITE_API int sqlite3_keyword_check(const char*,int);
8380
8381/*
8382** CAPI3REF: Dynamic String Object
8383** KEYWORDS: {dynamic string}
8384**
8385** An instance of the sqlite3_str object contains a dynamically-sized
8386** string under construction.
8387**
8388** The lifecycle of an sqlite3_str object is as follows:
8389** <ol>
8390** <li> ^The sqlite3_str object is created using [sqlite3_str_new()].
8391** <li> ^Text is appended to the sqlite3_str object using various
8392** methods, such as [sqlite3_str_appendf()].
8393** <li> ^The sqlite3_str object is destroyed and the string it created
8394** is returned using the [sqlite3_str_finish()] interface.
8395** </ol>
8396*/
8397typedef struct sqlite3_str sqlite3_str;
8398
8399/*
8400** CAPI3REF: Create A New Dynamic String Object
8401** CONSTRUCTOR: sqlite3_str
8402**
8403** ^The [sqlite3_str_new(D)] interface allocates and initializes
8404** a new [sqlite3_str] object. To avoid memory leaks, the object returned by
8405** [sqlite3_str_new()] must be freed by a subsequent call to
8406** [sqlite3_str_finish(X)].
8407**
8408** ^The [sqlite3_str_new(D)] interface always returns a pointer to a
8409** valid [sqlite3_str] object, though in the event of an out-of-memory
8410** error the returned object might be a special singleton that will
8411** silently reject new text, always return SQLITE_NOMEM from
8412** [sqlite3_str_errcode()], always return 0 for
8413** [sqlite3_str_length()], and always return NULL from
8414** [sqlite3_str_finish(X)]. It is always safe to use the value
8415** returned by [sqlite3_str_new(D)] as the sqlite3_str parameter
8416** to any of the other [sqlite3_str] methods.
8417**
8418** The D parameter to [sqlite3_str_new(D)] may be NULL. If the
8419** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum
8420** length of the string contained in the [sqlite3_str] object will be
8421** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead
8422** of [SQLITE_MAX_LENGTH].
8423*/
8424SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3*);
8425
8426/*
8427** CAPI3REF: Finalize A Dynamic String
8428** DESTRUCTOR: sqlite3_str
8429**
8430** ^The [sqlite3_str_finish(X)] interface destroys the sqlite3_str object X
8431** and returns a pointer to a memory buffer obtained from [sqlite3_malloc64()]
8432** that contains the constructed string. The calling application should
8433** pass the returned value to [sqlite3_free()] to avoid a memory leak.
8434** ^The [sqlite3_str_finish(X)] interface may return a NULL pointer if any
8435** errors were encountered during construction of the string. ^The
8436** [sqlite3_str_finish(X)] interface will also return a NULL pointer if the
8437** string in [sqlite3_str] object X is zero bytes long.
8438*/
8439SQLITE_API char *sqlite3_str_finish(sqlite3_str*);
8440
8441/*
8442** CAPI3REF: Add Content To A Dynamic String
8443** METHOD: sqlite3_str
8444**
8445** These interfaces add content to an sqlite3_str object previously obtained
8446** from [sqlite3_str_new()].
8447**
8448** ^The [sqlite3_str_appendf(X,F,...)] and
8449** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf]
8450** functionality of SQLite to append formatted text onto the end of
8451** [sqlite3_str] object X.
8452**
8453** ^The [sqlite3_str_append(X,S,N)] method appends exactly N bytes from string S
8454** onto the end of the [sqlite3_str] object X. N must be non-negative.
8455** S must contain at least N non-zero bytes of content. To append a
8456** zero-terminated string in its entirety, use the [sqlite3_str_appendall()]
8457** method instead.
8458**
8459** ^The [sqlite3_str_appendall(X,S)] method appends the complete content of
8460** zero-terminated string S onto the end of [sqlite3_str] object X.
8461**
8462** ^The [sqlite3_str_appendchar(X,N,C)] method appends N copies of the
8463** single-byte character C onto the end of [sqlite3_str] object X.
8464** ^This method can be used, for example, to add whitespace indentation.
8465**
8466** ^The [sqlite3_str_reset(X)] method resets the string under construction
8467** inside [sqlite3_str] object X back to zero bytes in length.
8468**
8469** These methods do not return a result code. ^If an error occurs, that fact
8470** is recorded in the [sqlite3_str] object and can be recovered by a
8471** subsequent call to [sqlite3_str_errcode(X)].
8472*/
8473SQLITE_API void sqlite3_str_appendf(sqlite3_str*, const char *zFormat, ...);
8474SQLITE_API void sqlite3_str_vappendf(sqlite3_str*, const char *zFormat, va_list);
8475SQLITE_API void sqlite3_str_append(sqlite3_str*, const char *zIn, int N);
8476SQLITE_API void sqlite3_str_appendall(sqlite3_str*, const char *zIn);
8477SQLITE_API void sqlite3_str_appendchar(sqlite3_str*, int N, char C);
8478SQLITE_API void sqlite3_str_reset(sqlite3_str*);
8479
8480/*
8481** CAPI3REF: Status Of A Dynamic String
8482** METHOD: sqlite3_str
8483**
8484** These interfaces return the current status of an [sqlite3_str] object.
8485**
8486** ^If any prior errors have occurred while constructing the dynamic string
8487** in sqlite3_str X, then the [sqlite3_str_errcode(X)] method will return
8488** an appropriate error code. ^The [sqlite3_str_errcode(X)] method returns
8489** [SQLITE_NOMEM] following any out-of-memory error, or
8490** [SQLITE_TOOBIG] if the size of the dynamic string exceeds
8491** [SQLITE_MAX_LENGTH], or [SQLITE_OK] if there have been no errors.
8492**
8493** ^The [sqlite3_str_length(X)] method returns the current length, in bytes,
8494** of the dynamic string under construction in [sqlite3_str] object X.
8495** ^The length returned by [sqlite3_str_length(X)] does not include the
8496** zero-termination byte.
8497**
8498** ^The [sqlite3_str_value(X)] method returns a pointer to the current
8499** content of the dynamic string under construction in X. The value
8500** returned by [sqlite3_str_value(X)] is managed by the sqlite3_str object X
8501** and might be freed or altered by any subsequent method on the same
8502** [sqlite3_str] object. Applications must not used the pointer returned
8503** [sqlite3_str_value(X)] after any subsequent method call on the same
8504** object. ^Applications may change the content of the string returned
8505** by [sqlite3_str_value(X)] as long as they do not write into any bytes
8506** outside the range of 0 to [sqlite3_str_length(X)] and do not read or
8507** write any byte after any subsequent sqlite3_str method call.
8508*/
8509SQLITE_API int sqlite3_str_errcode(sqlite3_str*);
8510SQLITE_API int sqlite3_str_length(sqlite3_str*);
8511SQLITE_API char *sqlite3_str_value(sqlite3_str*);
8512
8513/*
8514** CAPI3REF: SQLite Runtime Status
8515**
8516** ^These interfaces are used to retrieve runtime status information
8517** about the performance of SQLite, and optionally to reset various
8518** highwater marks. ^The first argument is an integer code for
8519** the specific parameter to measure. ^(Recognized integer codes
8520** are of the form [status parameters | SQLITE_STATUS_...].)^
8521** ^The current value of the parameter is returned into *pCurrent.
8522** ^The highest recorded value is returned in *pHighwater. ^If the
8523** resetFlag is true, then the highest record value is reset after
8524** *pHighwater is written. ^(Some parameters do not record the highest
8525** value. For those parameters
8526** nothing is written into *pHighwater and the resetFlag is ignored.)^
8527** ^(Other parameters record only the highwater mark and not the current
8528** value. For these latter parameters nothing is written into *pCurrent.)^
8529**
8530** ^The sqlite3_status() and sqlite3_status64() routines return
8531** SQLITE_OK on success and a non-zero [error code] on failure.
8532**
8533** If either the current value or the highwater mark is too large to
8534** be represented by a 32-bit integer, then the values returned by
8535** sqlite3_status() are undefined.
8536**
8537** See also: [sqlite3_db_status()]
8538*/
8539SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
8540SQLITE_API int sqlite3_status64(
8541 int op,
8542 sqlite3_int64 *pCurrent,
8543 sqlite3_int64 *pHighwater,
8544 int resetFlag
8545);
8546
8547
8548/*
8549** CAPI3REF: Status Parameters
8550** KEYWORDS: {status parameters}
8551**
8552** These integer constants designate various run-time status parameters
8553** that can be returned by [sqlite3_status()].
8554**
8555** <dl>
8556** [[SQLITE_STATUS_MEMORY_USED]] ^(<dt>SQLITE_STATUS_MEMORY_USED</dt>
8557** <dd>This parameter is the current amount of memory checked out
8558** using [sqlite3_malloc()], either directly or indirectly. The
8559** figure includes calls made to [sqlite3_malloc()] by the application
8560** and internal memory usage by the SQLite library. Auxiliary page-cache
8561** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
8562** this parameter. The amount returned is the sum of the allocation
8563** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
8564**
8565** [[SQLITE_STATUS_MALLOC_SIZE]] ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt>
8566** <dd>This parameter records the largest memory allocation request
8567** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
8568** internal equivalents). Only the value returned in the
8569** *pHighwater parameter to [sqlite3_status()] is of interest.
8570** The value written into the *pCurrent parameter is undefined.</dd>)^
8571**
8572** [[SQLITE_STATUS_MALLOC_COUNT]] ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt>
8573** <dd>This parameter records the number of separate memory allocations
8574** currently checked out.</dd>)^
8575**
8576** [[SQLITE_STATUS_PAGECACHE_USED]] ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt>
8577** <dd>This parameter returns the number of pages used out of the
8578** [pagecache memory allocator] that was configured using
8579** [SQLITE_CONFIG_PAGECACHE]. The
8580** value returned is in pages, not in bytes.</dd>)^
8581**
8582** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]]
8583** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt>
8584** <dd>This parameter returns the number of bytes of page cache
8585** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE]
8586** buffer and where forced to overflow to [sqlite3_malloc()]. The
8587** returned value includes allocations that overflowed because they
8588** where too large (they were larger than the "sz" parameter to
8589** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because
8590** no space was left in the page cache.</dd>)^
8591**
8592** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
8593** <dd>This parameter records the largest memory allocation request
8594** handed to the [pagecache memory allocator]. Only the value returned in the
8595** *pHighwater parameter to [sqlite3_status()] is of interest.
8596** The value written into the *pCurrent parameter is undefined.</dd>)^
8597**
8598** [[SQLITE_STATUS_SCRATCH_USED]] <dt>SQLITE_STATUS_SCRATCH_USED</dt>
8599** <dd>No longer used.</dd>
8600**
8601** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
8602** <dd>No longer used.</dd>
8603**
8604** [[SQLITE_STATUS_SCRATCH_SIZE]] <dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
8605** <dd>No longer used.</dd>
8606**
8607** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
8608** <dd>The *pHighwater parameter records the deepest parser stack.
8609** The *pCurrent value is undefined. The *pHighwater value is only
8610** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
8611** </dl>
8612**
8613** New status parameters may be added from time to time.
8614*/
8615#define SQLITE_STATUS_MEMORY_USED0 0
8616#define SQLITE_STATUS_PAGECACHE_USED1 1
8617#define SQLITE_STATUS_PAGECACHE_OVERFLOW2 2
8618#define SQLITE_STATUS_SCRATCH_USED3 3 /* NOT USED */
8619#define SQLITE_STATUS_SCRATCH_OVERFLOW4 4 /* NOT USED */
8620#define SQLITE_STATUS_MALLOC_SIZE5 5
8621#define SQLITE_STATUS_PARSER_STACK6 6
8622#define SQLITE_STATUS_PAGECACHE_SIZE7 7
8623#define SQLITE_STATUS_SCRATCH_SIZE8 8 /* NOT USED */
8624#define SQLITE_STATUS_MALLOC_COUNT9 9
8625
8626/*
8627** CAPI3REF: Database Connection Status
8628** METHOD: sqlite3
8629**
8630** ^This interface is used to retrieve runtime status information
8631** about a single [database connection]. ^The first argument is the
8632** database connection object to be interrogated. ^The second argument
8633** is an integer constant, taken from the set of
8634** [SQLITE_DBSTATUS options], that
8635** determines the parameter to interrogate. The set of
8636** [SQLITE_DBSTATUS options] is likely
8637** to grow in future releases of SQLite.
8638**
8639** ^The current value of the requested parameter is written into *pCur
8640** and the highest instantaneous value is written into *pHiwtr. ^If
8641** the resetFlg is true, then the highest instantaneous value is
8642** reset back down to the current value.
8643**
8644** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
8645** non-zero [error code] on failure.
8646**
8647** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
8648*/
8649SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
8650
8651/*
8652** CAPI3REF: Status Parameters for database connections
8653** KEYWORDS: {SQLITE_DBSTATUS options}
8654**
8655** These constants are the available integer "verbs" that can be passed as
8656** the second argument to the [sqlite3_db_status()] interface.
8657**
8658** New verbs may be added in future releases of SQLite. Existing verbs
8659** might be discontinued. Applications should check the return code from
8660** [sqlite3_db_status()] to make sure that the call worked.
8661** The [sqlite3_db_status()] interface will return a non-zero error code
8662** if a discontinued or unsupported verb is invoked.
8663**
8664** <dl>
8665** [[SQLITE_DBSTATUS_LOOKASIDE_USED]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
8666** <dd>This parameter returns the number of lookaside memory slots currently
8667** checked out.</dd>)^
8668**
8669** [[SQLITE_DBSTATUS_LOOKASIDE_HIT]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
8670** <dd>This parameter returns the number of malloc attempts that were
8671** satisfied using lookaside memory. Only the high-water value is meaningful;
8672** the current value is always zero.)^
8673**
8674** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE]]
8675** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
8676** <dd>This parameter returns the number malloc attempts that might have
8677** been satisfied using lookaside memory but failed due to the amount of
8678** memory requested being larger than the lookaside slot size.
8679** Only the high-water value is meaningful;
8680** the current value is always zero.)^
8681**
8682** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL]]
8683** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
8684** <dd>This parameter returns the number malloc attempts that might have
8685** been satisfied using lookaside memory but failed due to all lookaside
8686** memory already being in use.
8687** Only the high-water value is meaningful;
8688** the current value is always zero.)^
8689**
8690** [[SQLITE_DBSTATUS_CACHE_USED]] ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
8691** <dd>This parameter returns the approximate number of bytes of heap
8692** memory used by all pager caches associated with the database connection.)^
8693** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
8694**
8695** [[SQLITE_DBSTATUS_CACHE_USED_SHARED]]
8696** ^(<dt>SQLITE_DBSTATUS_CACHE_USED_SHARED</dt>
8697** <dd>This parameter is similar to DBSTATUS_CACHE_USED, except that if a
8698** pager cache is shared between two or more connections the bytes of heap
8699** memory used by that pager cache is divided evenly between the attached
8700** connections.)^ In other words, if none of the pager caches associated
8701** with the database connection are shared, this request returns the same
8702** value as DBSTATUS_CACHE_USED. Or, if one or more or the pager caches are
8703** shared, the value returned by this call will be smaller than that returned
8704** by DBSTATUS_CACHE_USED. ^The highwater mark associated with
8705** SQLITE_DBSTATUS_CACHE_USED_SHARED is always 0.
8706**
8707** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
8708** <dd>This parameter returns the approximate number of bytes of heap
8709** memory used to store the schema for all databases associated
8710** with the connection - main, temp, and any [ATTACH]-ed databases.)^
8711** ^The full amount of memory used by the schemas is reported, even if the
8712** schema memory is shared with other database connections due to
8713** [shared cache mode] being enabled.
8714** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0.
8715**
8716** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
8717** <dd>This parameter returns the approximate number of bytes of heap
8718** and lookaside memory used by all prepared statements associated with
8719** the database connection.)^
8720** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
8721** </dd>
8722**
8723** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
8724** <dd>This parameter returns the number of pager cache hits that have
8725** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT
8726** is always 0.
8727** </dd>
8728**
8729** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
8730** <dd>This parameter returns the number of pager cache misses that have
8731** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS
8732** is always 0.
8733** </dd>
8734**
8735** [[SQLITE_DBSTATUS_CACHE_WRITE]] ^(<dt>SQLITE_DBSTATUS_CACHE_WRITE</dt>
8736** <dd>This parameter returns the number of dirty cache entries that have
8737** been written to disk. Specifically, the number of pages written to the
8738** wal file in wal mode databases, or the number of pages written to the
8739** database file in rollback mode databases. Any pages written as part of
8740** transaction rollback or database recovery operations are not included.
8741** If an IO or other error occurs while writing a page to disk, the effect
8742** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
8743** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
8744** </dd>
8745**
8746** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(<dt>SQLITE_DBSTATUS_CACHE_SPILL</dt>
8747** <dd>This parameter returns the number of dirty cache entries that have
8748** been written to disk in the middle of a transaction due to the page
8749** cache overflowing. Transactions are more efficient if they are written
8750** to disk all at once. When pages spill mid-transaction, that introduces
8751** additional overhead. This parameter can be used help identify
8752** inefficiencies that can be resolved by increasing the cache size.
8753** </dd>
8754**
8755** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
8756** <dd>This parameter returns zero for the current value if and only if
8757** all foreign key constraints (deferred or immediate) have been
8758** resolved.)^ ^The highwater mark is always 0.
8759** </dd>
8760** </dl>
8761*/
8762#define SQLITE_DBSTATUS_LOOKASIDE_USED0 0
8763#define SQLITE_DBSTATUS_CACHE_USED1 1
8764#define SQLITE_DBSTATUS_SCHEMA_USED2 2
8765#define SQLITE_DBSTATUS_STMT_USED3 3
8766#define SQLITE_DBSTATUS_LOOKASIDE_HIT4 4
8767#define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE5 5
8768#define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL6 6
8769#define SQLITE_DBSTATUS_CACHE_HIT7 7
8770#define SQLITE_DBSTATUS_CACHE_MISS8 8
8771#define SQLITE_DBSTATUS_CACHE_WRITE9 9
8772#define SQLITE_DBSTATUS_DEFERRED_FKS10 10
8773#define SQLITE_DBSTATUS_CACHE_USED_SHARED11 11
8774#define SQLITE_DBSTATUS_CACHE_SPILL12 12
8775#define SQLITE_DBSTATUS_MAX12 12 /* Largest defined DBSTATUS */
8776
8777
8778/*
8779** CAPI3REF: Prepared Statement Status
8780** METHOD: sqlite3_stmt
8781**
8782** ^(Each prepared statement maintains various
8783** [SQLITE_STMTSTATUS counters] that measure the number
8784** of times it has performed specific operations.)^ These counters can
8785** be used to monitor the performance characteristics of the prepared
8786** statements. For example, if the number of table steps greatly exceeds
8787** the number of table searches or result rows, that would tend to indicate
8788** that the prepared statement is using a full table scan rather than
8789** an index.
8790**
8791** ^(This interface is used to retrieve and reset counter values from
8792** a [prepared statement]. The first argument is the prepared statement
8793** object to be interrogated. The second argument
8794** is an integer code for a specific [SQLITE_STMTSTATUS counter]
8795** to be interrogated.)^
8796** ^The current value of the requested counter is returned.
8797** ^If the resetFlg is true, then the counter is reset to zero after this
8798** interface call returns.
8799**
8800** See also: [sqlite3_status()] and [sqlite3_db_status()].
8801*/
8802SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
8803
8804/*
8805** CAPI3REF: Status Parameters for prepared statements
8806** KEYWORDS: {SQLITE_STMTSTATUS counter} {SQLITE_STMTSTATUS counters}
8807**
8808** These preprocessor macros define integer codes that name counter
8809** values associated with the [sqlite3_stmt_status()] interface.
8810** The meanings of the various counters are as follows:
8811**
8812** <dl>
8813** [[SQLITE_STMTSTATUS_FULLSCAN_STEP]] <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt>
8814** <dd>^This is the number of times that SQLite has stepped forward in
8815** a table as part of a full table scan. Large numbers for this counter
8816** may indicate opportunities for performance improvement through
8817** careful use of indices.</dd>
8818**
8819** [[SQLITE_STMTSTATUS_SORT]] <dt>SQLITE_STMTSTATUS_SORT</dt>
8820** <dd>^This is the number of sort operations that have occurred.
8821** A non-zero value in this counter may indicate an opportunity to
8822** improvement performance through careful use of indices.</dd>
8823**
8824** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
8825** <dd>^This is the number of rows inserted into transient indices that
8826** were created automatically in order to help joins run faster.
8827** A non-zero value in this counter may indicate an opportunity to
8828** improvement performance by adding permanent indices that do not
8829** need to be reinitialized each time the statement is run.</dd>
8830**
8831** [[SQLITE_STMTSTATUS_VM_STEP]] <dt>SQLITE_STMTSTATUS_VM_STEP</dt>
8832** <dd>^This is the number of virtual machine operations executed
8833** by the prepared statement if that number is less than or equal
8834** to 2147483647. The number of virtual machine operations can be
8835** used as a proxy for the total work done by the prepared statement.
8836** If the number of virtual machine operations exceeds 2147483647
8837** then the value returned by this statement status code is undefined.
8838**
8839** [[SQLITE_STMTSTATUS_REPREPARE]] <dt>SQLITE_STMTSTATUS_REPREPARE</dt>
8840** <dd>^This is the number of times that the prepare statement has been
8841** automatically regenerated due to schema changes or changes to
8842** [bound parameters] that might affect the query plan.
8843**
8844** [[SQLITE_STMTSTATUS_RUN]] <dt>SQLITE_STMTSTATUS_RUN</dt>
8845** <dd>^This is the number of times that the prepared statement has
8846** been run. A single "run" for the purposes of this counter is one
8847** or more calls to [sqlite3_step()] followed by a call to [sqlite3_reset()].
8848** The counter is incremented on the first [sqlite3_step()] call of each
8849** cycle.
8850**
8851** [[SQLITE_STMTSTATUS_FILTER_MISS]]
8852** [[SQLITE_STMTSTATUS_FILTER HIT]]
8853** <dt>SQLITE_STMTSTATUS_FILTER_HIT<br>
8854** SQLITE_STMTSTATUS_FILTER_MISS</dt>
8855** <dd>^SQLITE_STMTSTATUS_FILTER_HIT is the number of times that a join
8856** step was bypassed because a Bloom filter returned not-found. The
8857** corresponding SQLITE_STMTSTATUS_FILTER_MISS value is the number of
8858** times that the Bloom filter returned a find, and thus the join step
8859** had to be processed as normal.
8860**
8861** [[SQLITE_STMTSTATUS_MEMUSED]] <dt>SQLITE_STMTSTATUS_MEMUSED</dt>
8862** <dd>^This is the approximate number of bytes of heap memory
8863** used to store the prepared statement. ^This value is not actually
8864** a counter, and so the resetFlg parameter to sqlite3_stmt_status()
8865** is ignored when the opcode is SQLITE_STMTSTATUS_MEMUSED.
8866** </dd>
8867** </dl>
8868*/
8869#define SQLITE_STMTSTATUS_FULLSCAN_STEP1 1
8870#define SQLITE_STMTSTATUS_SORT2 2
8871#define SQLITE_STMTSTATUS_AUTOINDEX3 3
8872#define SQLITE_STMTSTATUS_VM_STEP4 4
8873#define SQLITE_STMTSTATUS_REPREPARE5 5
8874#define SQLITE_STMTSTATUS_RUN6 6
8875#define SQLITE_STMTSTATUS_FILTER_MISS7 7
8876#define SQLITE_STMTSTATUS_FILTER_HIT8 8
8877#define SQLITE_STMTSTATUS_MEMUSED99 99
8878
8879/*
8880** CAPI3REF: Custom Page Cache Object
8881**
8882** The sqlite3_pcache type is opaque. It is implemented by
8883** the pluggable module. The SQLite core has no knowledge of
8884** its size or internal structure and never deals with the
8885** sqlite3_pcache object except by holding and passing pointers
8886** to the object.
8887**
8888** See [sqlite3_pcache_methods2] for additional information.
8889*/
8890typedef struct sqlite3_pcache sqlite3_pcache;
8891
8892/*
8893** CAPI3REF: Custom Page Cache Object
8894**
8895** The sqlite3_pcache_page object represents a single page in the
8896** page cache. The page cache will allocate instances of this
8897** object. Various methods of the page cache use pointers to instances
8898** of this object as parameters or as their return value.
8899**
8900** See [sqlite3_pcache_methods2] for additional information.
8901*/
8902typedef struct sqlite3_pcache_page sqlite3_pcache_page;
8903struct sqlite3_pcache_page {
8904 void *pBuf; /* The content of the page */
8905 void *pExtra; /* Extra information associated with the page */
8906};
8907
8908/*
8909** CAPI3REF: Application Defined Page Cache.
8910** KEYWORDS: {page cache}
8911**
8912** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can
8913** register an alternative page cache implementation by passing in an
8914** instance of the sqlite3_pcache_methods2 structure.)^
8915** In many applications, most of the heap memory allocated by
8916** SQLite is used for the page cache.
8917** By implementing a
8918** custom page cache using this API, an application can better control
8919** the amount of memory consumed by SQLite, the way in which
8920** that memory is allocated and released, and the policies used to
8921** determine exactly which parts of a database file are cached and for
8922** how long.
8923**
8924** The alternative page cache mechanism is an
8925** extreme measure that is only needed by the most demanding applications.
8926** The built-in page cache is recommended for most uses.
8927**
8928** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an
8929** internal buffer by SQLite within the call to [sqlite3_config]. Hence
8930** the application may discard the parameter after the call to
8931** [sqlite3_config()] returns.)^
8932**
8933** [[the xInit() page cache method]]
8934** ^(The xInit() method is called once for each effective
8935** call to [sqlite3_initialize()])^
8936** (usually only once during the lifetime of the process). ^(The xInit()
8937** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^
8938** The intent of the xInit() method is to set up global data structures
8939** required by the custom page cache implementation.
8940** ^(If the xInit() method is NULL, then the
8941** built-in default page cache is used instead of the application defined
8942** page cache.)^
8943**
8944** [[the xShutdown() page cache method]]
8945** ^The xShutdown() method is called by [sqlite3_shutdown()].
8946** It can be used to clean up
8947** any outstanding resources before process shutdown, if required.
8948** ^The xShutdown() method may be NULL.
8949**
8950** ^SQLite automatically serializes calls to the xInit method,
8951** so the xInit method need not be threadsafe. ^The
8952** xShutdown method is only called from [sqlite3_shutdown()] so it does
8953** not need to be threadsafe either. All other methods must be threadsafe
8954** in multithreaded applications.
8955**
8956** ^SQLite will never invoke xInit() more than once without an intervening
8957** call to xShutdown().
8958**
8959** [[the xCreate() page cache methods]]
8960** ^SQLite invokes the xCreate() method to construct a new cache instance.
8961** SQLite will typically create one cache instance for each open database file,
8962** though this is not guaranteed. ^The
8963** first parameter, szPage, is the size in bytes of the pages that must
8964** be allocated by the cache. ^szPage will always a power of two. ^The
8965** second parameter szExtra is a number of bytes of extra storage
8966** associated with each page cache entry. ^The szExtra parameter will
8967** a number less than 250. SQLite will use the
8968** extra szExtra bytes on each page to store metadata about the underlying
8969** database page on disk. The value passed into szExtra depends
8970** on the SQLite version, the target platform, and how SQLite was compiled.
8971** ^The third argument to xCreate(), bPurgeable, is true if the cache being
8972** created will be used to cache database pages of a file stored on disk, or
8973** false if it is used for an in-memory database. The cache implementation
8974** does not have to do anything special based with the value of bPurgeable;
8975** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will
8976** never invoke xUnpin() except to deliberately delete a page.
8977** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
8978** false will always have the "discard" flag set to true.
8979** ^Hence, a cache created with bPurgeable false will
8980** never contain any unpinned pages.
8981**
8982** [[the xCachesize() page cache method]]
8983** ^(The xCachesize() method may be called at any time by SQLite to set the
8984** suggested maximum cache-size (number of pages stored by) the cache
8985** instance passed as the first argument. This is the value configured using
8986** the SQLite "[PRAGMA cache_size]" command.)^ As with the bPurgeable
8987** parameter, the implementation is not required to do anything with this
8988** value; it is advisory only.
8989**
8990** [[the xPagecount() page cache methods]]
8991** The xPagecount() method must return the number of pages currently
8992** stored in the cache, both pinned and unpinned.
8993**
8994** [[the xFetch() page cache methods]]
8995** The xFetch() method locates a page in the cache and returns a pointer to
8996** an sqlite3_pcache_page object associated with that page, or a NULL pointer.
8997** The pBuf element of the returned sqlite3_pcache_page object will be a
8998** pointer to a buffer of szPage bytes used to store the content of a
8999** single database page. The pExtra element of sqlite3_pcache_page will be
9000** a pointer to the szExtra bytes of extra storage that SQLite has requested
9001** for each entry in the page cache.
9002**
9003** The page to be fetched is determined by the key. ^The minimum key value
9004** is 1. After it has been retrieved using xFetch, the page is considered
9005** to be "pinned".
9006**
9007** If the requested page is already in the page cache, then the page cache
9008** implementation must return a pointer to the page buffer with its content
9009** intact. If the requested page is not already in the cache, then the
9010** cache implementation should use the value of the createFlag
9011** parameter to help it determined what action to take:
9012**
9013** <table border=1 width=85% align=center>
9014** <tr><th> createFlag <th> Behavior when page is not already in cache
9015** <tr><td> 0 <td> Do not allocate a new page. Return NULL.
9016** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
9017** Otherwise return NULL.
9018** <tr><td> 2 <td> Make every effort to allocate a new page. Only return
9019** NULL if allocating a new page is effectively impossible.
9020** </table>
9021**
9022** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1. SQLite
9023** will only use a createFlag of 2 after a prior call with a createFlag of 1
9024** failed.)^ In between the xFetch() calls, SQLite may
9025** attempt to unpin one or more cache pages by spilling the content of
9026** pinned pages to disk and synching the operating system disk cache.
9027**
9028** [[the xUnpin() page cache method]]
9029** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
9030** as its second argument. If the third parameter, discard, is non-zero,
9031** then the page must be evicted from the cache.
9032** ^If the discard parameter is
9033** zero, then the page may be discarded or retained at the discretion of
9034** page cache implementation. ^The page cache implementation
9035** may choose to evict unpinned pages at any time.
9036**
9037** The cache must not perform any reference counting. A single
9038** call to xUnpin() unpins the page regardless of the number of prior calls
9039** to xFetch().
9040**
9041** [[the xRekey() page cache methods]]
9042** The xRekey() method is used to change the key value associated with the
9043** page passed as the second argument. If the cache
9044** previously contains an entry associated with newKey, it must be
9045** discarded. ^Any prior cache entry associated with newKey is guaranteed not
9046** to be pinned.
9047**
9048** When SQLite calls the xTruncate() method, the cache must discard all
9049** existing cache entries with page numbers (keys) greater than or equal
9050** to the value of the iLimit parameter passed to xTruncate(). If any
9051** of these pages are pinned, they are implicitly unpinned, meaning that
9052** they can be safely discarded.
9053**
9054** [[the xDestroy() page cache method]]
9055** ^The xDestroy() method is used to delete a cache allocated by xCreate().
9056** All resources associated with the specified cache should be freed. ^After
9057** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
9058** handle invalid, and will not use it with any other sqlite3_pcache_methods2
9059** functions.
9060**
9061** [[the xShrink() page cache method]]
9062** ^SQLite invokes the xShrink() method when it wants the page cache to
9063** free up as much of heap memory as possible. The page cache implementation
9064** is not obligated to free any memory, but well-behaved implementations should
9065** do their best.
9066*/
9067typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2;
9068struct sqlite3_pcache_methods2 {
9069 int iVersion;
9070 void *pArg;
9071 int (*xInit)(void*);
9072 void (*xShutdown)(void*);
9073 sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
9074 void (*xCachesize)(sqlite3_pcache*, int nCachesize);
9075 int (*xPagecount)(sqlite3_pcache*);
9076 sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
9077 void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
9078 void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*,
9079 unsigned oldKey, unsigned newKey);
9080 void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
9081 void (*xDestroy)(sqlite3_pcache*);
9082 void (*xShrink)(sqlite3_pcache*);
9083};
9084
9085/*
9086** This is the obsolete pcache_methods object that has now been replaced
9087** by sqlite3_pcache_methods2. This object is not used by SQLite. It is
9088** retained in the header file for backwards compatibility only.
9089*/
9090typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
9091struct sqlite3_pcache_methods {
9092 void *pArg;
9093 int (*xInit)(void*);
9094 void (*xShutdown)(void*);
9095 sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
9096 void (*xCachesize)(sqlite3_pcache*, int nCachesize);
9097 int (*xPagecount)(sqlite3_pcache*);
9098 void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
9099 void (*xUnpin)(sqlite3_pcache*, void*, int discard);
9100 void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
9101 void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
9102 void (*xDestroy)(sqlite3_pcache*);
9103};
9104
9105
9106/*
9107** CAPI3REF: Online Backup Object
9108**
9109** The sqlite3_backup object records state information about an ongoing
9110** online backup operation. ^The sqlite3_backup object is created by
9111** a call to [sqlite3_backup_init()] and is destroyed by a call to
9112** [sqlite3_backup_finish()].
9113**
9114** See Also: [Using the SQLite Online Backup API]
9115*/
9116typedef struct sqlite3_backup sqlite3_backup;
9117
9118/*
9119** CAPI3REF: Online Backup API.
9120**
9121** The backup API copies the content of one database into another.
9122** It is useful either for creating backups of databases or
9123** for copying in-memory databases to or from persistent files.
9124**
9125** See Also: [Using the SQLite Online Backup API]
9126**
9127** ^SQLite holds a write transaction open on the destination database file
9128** for the duration of the backup operation.
9129** ^The source database is read-locked only while it is being read;
9130** it is not locked continuously for the entire backup operation.
9131** ^Thus, the backup may be performed on a live source database without
9132** preventing other database connections from
9133** reading or writing to the source database while the backup is underway.
9134**
9135** ^(To perform a backup operation:
9136** <ol>
9137** <li><b>sqlite3_backup_init()</b> is called once to initialize the
9138** backup,
9139** <li><b>sqlite3_backup_step()</b> is called one or more times to transfer
9140** the data between the two databases, and finally
9141** <li><b>sqlite3_backup_finish()</b> is called to release all resources
9142** associated with the backup operation.
9143** </ol>)^
9144** There should be exactly one call to sqlite3_backup_finish() for each
9145** successful call to sqlite3_backup_init().
9146**
9147** [[sqlite3_backup_init()]] <b>sqlite3_backup_init()</b>
9148**
9149** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the
9150** [database connection] associated with the destination database
9151** and the database name, respectively.
9152** ^The database name is "main" for the main database, "temp" for the
9153** temporary database, or the name specified after the AS keyword in
9154** an [ATTACH] statement for an attached database.
9155** ^The S and M arguments passed to
9156** sqlite3_backup_init(D,N,S,M) identify the [database connection]
9157** and database name of the source database, respectively.
9158** ^The source and destination [database connections] (parameters S and D)
9159** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
9160** an error.
9161**
9162** ^A call to sqlite3_backup_init() will fail, returning NULL, if
9163** there is already a read or read-write transaction open on the
9164** destination database.
9165**
9166** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
9167** returned and an error code and error message are stored in the
9168** destination [database connection] D.
9169** ^The error code and message for the failed call to sqlite3_backup_init()
9170** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or
9171** [sqlite3_errmsg16()] functions.
9172** ^A successful call to sqlite3_backup_init() returns a pointer to an
9173** [sqlite3_backup] object.
9174** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and
9175** sqlite3_backup_finish() functions to perform the specified backup
9176** operation.
9177**
9178** [[sqlite3_backup_step()]] <b>sqlite3_backup_step()</b>
9179**
9180** ^Function sqlite3_backup_step(B,N) will copy up to N pages between
9181** the source and destination databases specified by [sqlite3_backup] object B.
9182** ^If N is negative, all remaining source pages are copied.
9183** ^If sqlite3_backup_step(B,N) successfully copies N pages and there
9184** are still more pages to be copied, then the function returns [SQLITE_OK].
9185** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages
9186** from source to destination, then it returns [SQLITE_DONE].
9187** ^If an error occurs while running sqlite3_backup_step(B,N),
9188** then an [error code] is returned. ^As well as [SQLITE_OK] and
9189** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
9190** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
9191** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
9192**
9193** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
9194** <ol>
9195** <li> the destination database was opened read-only, or
9196** <li> the destination database is using write-ahead-log journaling
9197** and the destination and source page sizes differ, or
9198** <li> the destination database is an in-memory database and the
9199** destination and source page sizes differ.
9200** </ol>)^
9201**
9202** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
9203** the [sqlite3_busy_handler | busy-handler function]
9204** is invoked (if one is specified). ^If the
9205** busy-handler returns non-zero before the lock is available, then
9206** [SQLITE_BUSY] is returned to the caller. ^In this case the call to
9207** sqlite3_backup_step() can be retried later. ^If the source
9208** [database connection]
9209** is being used to write to the source database when sqlite3_backup_step()
9210** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this
9211** case the call to sqlite3_backup_step() can be retried later on. ^(If
9212** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or
9213** [SQLITE_READONLY] is returned, then
9214** there is no point in retrying the call to sqlite3_backup_step(). These
9215** errors are considered fatal.)^ The application must accept
9216** that the backup operation has failed and pass the backup operation handle
9217** to the sqlite3_backup_finish() to release associated resources.
9218**
9219** ^The first call to sqlite3_backup_step() obtains an exclusive lock
9220** on the destination file. ^The exclusive lock is not released until either
9221** sqlite3_backup_finish() is called or the backup operation is complete
9222** and sqlite3_backup_step() returns [SQLITE_DONE]. ^Every call to
9223** sqlite3_backup_step() obtains a [shared lock] on the source database that
9224** lasts for the duration of the sqlite3_backup_step() call.
9225** ^Because the source database is not locked between calls to
9226** sqlite3_backup_step(), the source database may be modified mid-way
9227** through the backup process. ^If the source database is modified by an
9228** external process or via a database connection other than the one being
9229** used by the backup operation, then the backup will be automatically
9230** restarted by the next call to sqlite3_backup_step(). ^If the source
9231** database is modified by the using the same database connection as is used
9232** by the backup operation, then the backup database is automatically
9233** updated at the same time.
9234**
9235** [[sqlite3_backup_finish()]] <b>sqlite3_backup_finish()</b>
9236**
9237** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the
9238** application wishes to abandon the backup operation, the application
9239** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish().
9240** ^The sqlite3_backup_finish() interfaces releases all
9241** resources associated with the [sqlite3_backup] object.
9242** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any
9243** active write-transaction on the destination database is rolled back.
9244** The [sqlite3_backup] object is invalid
9245** and may not be used following a call to sqlite3_backup_finish().
9246**
9247** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no
9248** sqlite3_backup_step() errors occurred, regardless or whether or not
9249** sqlite3_backup_step() completed.
9250** ^If an out-of-memory condition or IO error occurred during any prior
9251** sqlite3_backup_step() call on the same [sqlite3_backup] object, then
9252** sqlite3_backup_finish() returns the corresponding [error code].
9253**
9254** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step()
9255** is not a permanent error and does not affect the return value of
9256** sqlite3_backup_finish().
9257**
9258** [[sqlite3_backup_remaining()]] [[sqlite3_backup_pagecount()]]
9259** <b>sqlite3_backup_remaining() and sqlite3_backup_pagecount()</b>
9260**
9261** ^The sqlite3_backup_remaining() routine returns the number of pages still
9262** to be backed up at the conclusion of the most recent sqlite3_backup_step().
9263** ^The sqlite3_backup_pagecount() routine returns the total number of pages
9264** in the source database at the conclusion of the most recent
9265** sqlite3_backup_step().
9266** ^(The values returned by these functions are only updated by
9267** sqlite3_backup_step(). If the source database is modified in a way that
9268** changes the size of the source database or the number of pages remaining,
9269** those changes are not reflected in the output of sqlite3_backup_pagecount()
9270** and sqlite3_backup_remaining() until after the next
9271** sqlite3_backup_step().)^
9272**
9273** <b>Concurrent Usage of Database Handles</b>
9274**
9275** ^The source [database connection] may be used by the application for other
9276** purposes while a backup operation is underway or being initialized.
9277** ^If SQLite is compiled and configured to support threadsafe database
9278** connections, then the source database connection may be used concurrently
9279** from within other threads.
9280**
9281** However, the application must guarantee that the destination
9282** [database connection] is not passed to any other API (by any thread) after
9283** sqlite3_backup_init() is called and before the corresponding call to
9284** sqlite3_backup_finish(). SQLite does not currently check to see
9285** if the application incorrectly accesses the destination [database connection]
9286** and so no error code is reported, but the operations may malfunction
9287** nevertheless. Use of the destination database connection while a
9288** backup is in progress might also also cause a mutex deadlock.
9289**
9290** If running in [shared cache mode], the application must
9291** guarantee that the shared cache used by the destination database
9292** is not accessed while the backup is running. In practice this means
9293** that the application must guarantee that the disk file being
9294** backed up to is not accessed by any connection within the process,
9295** not just the specific connection that was passed to sqlite3_backup_init().
9296**
9297** The [sqlite3_backup] object itself is partially threadsafe. Multiple
9298** threads may safely make multiple concurrent calls to sqlite3_backup_step().
9299** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
9300** APIs are not strictly speaking threadsafe. If they are invoked at the
9301** same time as another thread is invoking sqlite3_backup_step() it is
9302** possible that they return invalid values.
9303*/
9304SQLITE_API sqlite3_backup *sqlite3_backup_init(
9305 sqlite3 *pDest, /* Destination database handle */
9306 const char *zDestName, /* Destination database name */
9307 sqlite3 *pSource, /* Source database handle */
9308 const char *zSourceName /* Source database name */
9309);
9310SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
9311SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
9312SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
9313SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
9314
9315/*
9316** CAPI3REF: Unlock Notification
9317** METHOD: sqlite3
9318**
9319** ^When running in shared-cache mode, a database operation may fail with
9320** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
9321** individual tables within the shared-cache cannot be obtained. See
9322** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
9323** ^This API may be used to register a callback that SQLite will invoke
9324** when the connection currently holding the required lock relinquishes it.
9325** ^This API is only available if the library was compiled with the
9326** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
9327**
9328** See Also: [Using the SQLite Unlock Notification Feature].
9329**
9330** ^Shared-cache locks are released when a database connection concludes
9331** its current transaction, either by committing it or rolling it back.
9332**
9333** ^When a connection (known as the blocked connection) fails to obtain a
9334** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
9335** identity of the database connection (the blocking connection) that
9336** has locked the required resource is stored internally. ^After an
9337** application receives an SQLITE_LOCKED error, it may call the
9338** sqlite3_unlock_notify() method with the blocked connection handle as
9339** the first argument to register for a callback that will be invoked
9340** when the blocking connections current transaction is concluded. ^The
9341** callback is invoked from within the [sqlite3_step] or [sqlite3_close]
9342** call that concludes the blocking connection's transaction.
9343**
9344** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
9345** there is a chance that the blocking connection will have already
9346** concluded its transaction by the time sqlite3_unlock_notify() is invoked.
9347** If this happens, then the specified callback is invoked immediately,
9348** from within the call to sqlite3_unlock_notify().)^
9349**
9350** ^If the blocked connection is attempting to obtain a write-lock on a
9351** shared-cache table, and more than one other connection currently holds
9352** a read-lock on the same table, then SQLite arbitrarily selects one of
9353** the other connections to use as the blocking connection.
9354**
9355** ^(There may be at most one unlock-notify callback registered by a
9356** blocked connection. If sqlite3_unlock_notify() is called when the
9357** blocked connection already has a registered unlock-notify callback,
9358** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
9359** called with a NULL pointer as its second argument, then any existing
9360** unlock-notify callback is canceled. ^The blocked connections
9361** unlock-notify callback may also be canceled by closing the blocked
9362** connection using [sqlite3_close()].
9363**
9364** The unlock-notify callback is not reentrant. If an application invokes
9365** any sqlite3_xxx API functions from within an unlock-notify callback, a
9366** crash or deadlock may be the result.
9367**
9368** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always
9369** returns SQLITE_OK.
9370**
9371** <b>Callback Invocation Details</b>
9372**
9373** When an unlock-notify callback is registered, the application provides a
9374** single void* pointer that is passed to the callback when it is invoked.
9375** However, the signature of the callback function allows SQLite to pass
9376** it an array of void* context pointers. The first argument passed to
9377** an unlock-notify callback is a pointer to an array of void* pointers,
9378** and the second is the number of entries in the array.
9379**
9380** When a blocking connection's transaction is concluded, there may be
9381** more than one blocked connection that has registered for an unlock-notify
9382** callback. ^If two or more such blocked connections have specified the
9383** same callback function, then instead of invoking the callback function
9384** multiple times, it is invoked once with the set of void* context pointers
9385** specified by the blocked connections bundled together into an array.
9386** This gives the application an opportunity to prioritize any actions
9387** related to the set of unblocked database connections.
9388**
9389** <b>Deadlock Detection</b>
9390**
9391** Assuming that after registering for an unlock-notify callback a
9392** database waits for the callback to be issued before taking any further
9393** action (a reasonable assumption), then using this API may cause the
9394** application to deadlock. For example, if connection X is waiting for
9395** connection Y's transaction to be concluded, and similarly connection
9396** Y is waiting on connection X's transaction, then neither connection
9397** will proceed and the system may remain deadlocked indefinitely.
9398**
9399** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
9400** detection. ^If a given call to sqlite3_unlock_notify() would put the
9401** system in a deadlocked state, then SQLITE_LOCKED is returned and no
9402** unlock-notify callback is registered. The system is said to be in
9403** a deadlocked state if connection A has registered for an unlock-notify
9404** callback on the conclusion of connection B's transaction, and connection
9405** B has itself registered for an unlock-notify callback when connection
9406** A's transaction is concluded. ^Indirect deadlock is also detected, so
9407** the system is also considered to be deadlocked if connection B has
9408** registered for an unlock-notify callback on the conclusion of connection
9409** C's transaction, where connection C is waiting on connection A. ^Any
9410** number of levels of indirection are allowed.
9411**
9412** <b>The "DROP TABLE" Exception</b>
9413**
9414** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost
9415** always appropriate to call sqlite3_unlock_notify(). There is however,
9416** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
9417** SQLite checks if there are any currently executing SELECT statements
9418** that belong to the same connection. If there are, SQLITE_LOCKED is
9419** returned. In this case there is no "blocking connection", so invoking
9420** sqlite3_unlock_notify() results in the unlock-notify callback being
9421** invoked immediately. If the application then re-attempts the "DROP TABLE"
9422** or "DROP INDEX" query, an infinite loop might be the result.
9423**
9424** One way around this problem is to check the extended error code returned
9425** by an sqlite3_step() call. ^(If there is a blocking connection, then the
9426** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
9427** the special "DROP TABLE/INDEX" case, the extended error code is just
9428** SQLITE_LOCKED.)^
9429*/
9430SQLITE_API int sqlite3_unlock_notify(
9431 sqlite3 *pBlocked, /* Waiting connection */
9432 void (*xNotify)(void **apArg, int nArg), /* Callback function to invoke */
9433 void *pNotifyArg /* Argument to pass to xNotify */
9434);
9435
9436
9437/*
9438** CAPI3REF: String Comparison
9439**
9440** ^The [sqlite3_stricmp()] and [sqlite3_strnicmp()] APIs allow applications
9441** and extensions to compare the contents of two buffers containing UTF-8
9442** strings in a case-independent fashion, using the same definition of "case
9443** independence" that SQLite uses internally when comparing identifiers.
9444*/
9445SQLITE_API int sqlite3_stricmp(const char *, const char *);
9446SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
9447
9448/*
9449** CAPI3REF: String Globbing
9450*
9451** ^The [sqlite3_strglob(P,X)] interface returns zero if and only if
9452** string X matches the [GLOB] pattern P.
9453** ^The definition of [GLOB] pattern matching used in
9454** [sqlite3_strglob(P,X)] is the same as for the "X GLOB P" operator in the
9455** SQL dialect understood by SQLite. ^The [sqlite3_strglob(P,X)] function
9456** is case sensitive.
9457**
9458** Note that this routine returns zero on a match and non-zero if the strings
9459** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
9460**
9461** See also: [sqlite3_strlike()].
9462*/
9463SQLITE_API int sqlite3_strglob(const char *zGlob, const char *zStr);
9464
9465/*
9466** CAPI3REF: String LIKE Matching
9467*
9468** ^The [sqlite3_strlike(P,X,E)] interface returns zero if and only if
9469** string X matches the [LIKE] pattern P with escape character E.
9470** ^The definition of [LIKE] pattern matching used in
9471** [sqlite3_strlike(P,X,E)] is the same as for the "X LIKE P ESCAPE E"
9472** operator in the SQL dialect understood by SQLite. ^For "X LIKE P" without
9473** the ESCAPE clause, set the E parameter of [sqlite3_strlike(P,X,E)] to 0.
9474** ^As with the LIKE operator, the [sqlite3_strlike(P,X,E)] function is case
9475** insensitive - equivalent upper and lower case ASCII characters match
9476** one another.
9477**
9478** ^The [sqlite3_strlike(P,X,E)] function matches Unicode characters, though
9479** only ASCII characters are case folded.
9480**
9481** Note that this routine returns zero on a match and non-zero if the strings
9482** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
9483**
9484** See also: [sqlite3_strglob()].
9485*/
9486SQLITE_API int sqlite3_strlike(const char *zGlob, const char *zStr, unsigned int cEsc);
9487
9488/*
9489** CAPI3REF: Error Logging Interface
9490**
9491** ^The [sqlite3_log()] interface writes a message into the [error log]
9492** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
9493** ^If logging is enabled, the zFormat string and subsequent arguments are
9494** used with [sqlite3_snprintf()] to generate the final output string.
9495**
9496** The sqlite3_log() interface is intended for use by extensions such as
9497** virtual tables, collating functions, and SQL functions. While there is
9498** nothing to prevent an application from calling sqlite3_log(), doing so
9499** is considered bad form.
9500**
9501** The zFormat string must not be NULL.
9502**
9503** To avoid deadlocks and other threading problems, the sqlite3_log() routine
9504** will not use dynamically allocated memory. The log message is stored in
9505** a fixed-length buffer on the stack. If the log message is longer than
9506** a few hundred characters, it will be truncated to the length of the
9507** buffer.
9508*/
9509SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
9510
9511/*
9512** CAPI3REF: Write-Ahead Log Commit Hook
9513** METHOD: sqlite3
9514**
9515** ^The [sqlite3_wal_hook()] function is used to register a callback that
9516** is invoked each time data is committed to a database in wal mode.
9517**
9518** ^(The callback is invoked by SQLite after the commit has taken place and
9519** the associated write-lock on the database released)^, so the implementation
9520** may read, write or [checkpoint] the database as required.
9521**
9522** ^The first parameter passed to the callback function when it is invoked
9523** is a copy of the third parameter passed to sqlite3_wal_hook() when
9524** registering the callback. ^The second is a copy of the database handle.
9525** ^The third parameter is the name of the database that was written to -
9526** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
9527** is the number of pages currently in the write-ahead log file,
9528** including those that were just committed.
9529**
9530** The callback function should normally return [SQLITE_OK]. ^If an error
9531** code is returned, that error will propagate back up through the
9532** SQLite code base to cause the statement that provoked the callback
9533** to report an error, though the commit will have still occurred. If the
9534** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
9535** that does not correspond to any valid SQLite error code, the results
9536** are undefined.
9537**
9538** A single database handle may have at most a single write-ahead log callback
9539** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
9540** previously registered write-ahead log callback. ^The return value is
9541** a copy of the third parameter from the previous call, if any, or 0.
9542** ^Note that the [sqlite3_wal_autocheckpoint()] interface and the
9543** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
9544** overwrite any prior [sqlite3_wal_hook()] settings.
9545*/
9546SQLITE_API void *sqlite3_wal_hook(
9547 sqlite3*,
9548 int(*)(void *,sqlite3*,const char*,int),
9549 void*
9550);
9551
9552/*
9553** CAPI3REF: Configure an auto-checkpoint
9554** METHOD: sqlite3
9555**
9556** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
9557** [sqlite3_wal_hook()] that causes any database on [database connection] D
9558** to automatically [checkpoint]
9559** after committing a transaction if there are N or
9560** more frames in the [write-ahead log] file. ^Passing zero or
9561** a negative value as the nFrame parameter disables automatic
9562** checkpoints entirely.
9563**
9564** ^The callback registered by this function replaces any existing callback
9565** registered using [sqlite3_wal_hook()]. ^Likewise, registering a callback
9566** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
9567** configured by this function.
9568**
9569** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
9570** from SQL.
9571**
9572** ^Checkpoints initiated by this mechanism are
9573** [sqlite3_wal_checkpoint_v2|PASSIVE].
9574**
9575** ^Every new [database connection] defaults to having the auto-checkpoint
9576** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
9577** pages. The use of this interface
9578** is only necessary if the default setting is found to be suboptimal
9579** for a particular application.
9580*/
9581SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
9582
9583/*
9584** CAPI3REF: Checkpoint a database
9585** METHOD: sqlite3
9586**
9587** ^(The sqlite3_wal_checkpoint(D,X) is equivalent to
9588** [sqlite3_wal_checkpoint_v2](D,X,[SQLITE_CHECKPOINT_PASSIVE],0,0).)^
9589**
9590** In brief, sqlite3_wal_checkpoint(D,X) causes the content in the
9591** [write-ahead log] for database X on [database connection] D to be
9592** transferred into the database file and for the write-ahead log to
9593** be reset. See the [checkpointing] documentation for addition
9594** information.
9595**
9596** This interface used to be the only way to cause a checkpoint to
9597** occur. But then the newer and more powerful [sqlite3_wal_checkpoint_v2()]
9598** interface was added. This interface is retained for backwards
9599** compatibility and as a convenience for applications that need to manually
9600** start a callback but which do not need the full power (and corresponding
9601** complication) of [sqlite3_wal_checkpoint_v2()].
9602*/
9603SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
9604
9605/*
9606** CAPI3REF: Checkpoint a database
9607** METHOD: sqlite3
9608**
9609** ^(The sqlite3_wal_checkpoint_v2(D,X,M,L,C) interface runs a checkpoint
9610** operation on database X of [database connection] D in mode M. Status
9611** information is written back into integers pointed to by L and C.)^
9612** ^(The M parameter must be a valid [checkpoint mode]:)^
9613**
9614** <dl>
9615** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
9616** ^Checkpoint as many frames as possible without waiting for any database
9617** readers or writers to finish, then sync the database file if all frames
9618** in the log were checkpointed. ^The [busy-handler callback]
9619** is never invoked in the SQLITE_CHECKPOINT_PASSIVE mode.
9620** ^On the other hand, passive mode might leave the checkpoint unfinished
9621** if there are concurrent readers or writers.
9622**
9623** <dt>SQLITE_CHECKPOINT_FULL<dd>
9624** ^This mode blocks (it invokes the
9625** [sqlite3_busy_handler|busy-handler callback]) until there is no
9626** database writer and all readers are reading from the most recent database
9627** snapshot. ^It then checkpoints all frames in the log file and syncs the
9628** database file. ^This mode blocks new database writers while it is pending,
9629** but new database readers are allowed to continue unimpeded.
9630**
9631** <dt>SQLITE_CHECKPOINT_RESTART<dd>
9632** ^This mode works the same way as SQLITE_CHECKPOINT_FULL with the addition
9633** that after checkpointing the log file it blocks (calls the
9634** [busy-handler callback])
9635** until all readers are reading from the database file only. ^This ensures
9636** that the next writer will restart the log file from the beginning.
9637** ^Like SQLITE_CHECKPOINT_FULL, this mode blocks new
9638** database writer attempts while it is pending, but does not impede readers.
9639**
9640** <dt>SQLITE_CHECKPOINT_TRUNCATE<dd>
9641** ^This mode works the same way as SQLITE_CHECKPOINT_RESTART with the
9642** addition that it also truncates the log file to zero bytes just prior
9643** to a successful return.
9644** </dl>
9645**
9646** ^If pnLog is not NULL, then *pnLog is set to the total number of frames in
9647** the log file or to -1 if the checkpoint could not run because
9648** of an error or because the database is not in [WAL mode]. ^If pnCkpt is not
9649** NULL,then *pnCkpt is set to the total number of checkpointed frames in the
9650** log file (including any that were already checkpointed before the function
9651** was called) or to -1 if the checkpoint could not run due to an error or
9652** because the database is not in WAL mode. ^Note that upon successful
9653** completion of an SQLITE_CHECKPOINT_TRUNCATE, the log file will have been
9654** truncated to zero bytes and so both *pnLog and *pnCkpt will be set to zero.
9655**
9656** ^All calls obtain an exclusive "checkpoint" lock on the database file. ^If
9657** any other process is running a checkpoint operation at the same time, the
9658** lock cannot be obtained and SQLITE_BUSY is returned. ^Even if there is a
9659** busy-handler configured, it will not be invoked in this case.
9660**
9661** ^The SQLITE_CHECKPOINT_FULL, RESTART and TRUNCATE modes also obtain the
9662** exclusive "writer" lock on the database file. ^If the writer lock cannot be
9663** obtained immediately, and a busy-handler is configured, it is invoked and
9664** the writer lock retried until either the busy-handler returns 0 or the lock
9665** is successfully obtained. ^The busy-handler is also invoked while waiting for
9666** database readers as described above. ^If the busy-handler returns 0 before
9667** the writer lock is obtained or while waiting for database readers, the
9668** checkpoint operation proceeds from that point in the same way as
9669** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
9670** without blocking any further. ^SQLITE_BUSY is returned in this case.
9671**
9672** ^If parameter zDb is NULL or points to a zero length string, then the
9673** specified operation is attempted on all WAL databases [attached] to
9674** [database connection] db. In this case the
9675** values written to output parameters *pnLog and *pnCkpt are undefined. ^If
9676** an SQLITE_BUSY error is encountered when processing one or more of the
9677** attached WAL databases, the operation is still attempted on any remaining
9678** attached databases and SQLITE_BUSY is returned at the end. ^If any other
9679** error occurs while processing an attached database, processing is abandoned
9680** and the error code is returned to the caller immediately. ^If no error
9681** (SQLITE_BUSY or otherwise) is encountered while processing the attached
9682** databases, SQLITE_OK is returned.
9683**
9684** ^If database zDb is the name of an attached database that is not in WAL
9685** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If
9686** zDb is not NULL (or a zero length string) and is not the name of any
9687** attached database, SQLITE_ERROR is returned to the caller.
9688**
9689** ^Unless it returns SQLITE_MISUSE,
9690** the sqlite3_wal_checkpoint_v2() interface
9691** sets the error information that is queried by
9692** [sqlite3_errcode()] and [sqlite3_errmsg()].
9693**
9694** ^The [PRAGMA wal_checkpoint] command can be used to invoke this interface
9695** from SQL.
9696*/
9697SQLITE_API int sqlite3_wal_checkpoint_v2(
9698 sqlite3 *db, /* Database handle */
9699 const char *zDb, /* Name of attached database (or NULL) */
9700 int eMode, /* SQLITE_CHECKPOINT_* value */
9701 int *pnLog, /* OUT: Size of WAL log in frames */
9702 int *pnCkpt /* OUT: Total number of frames checkpointed */
9703);
9704
9705/*
9706** CAPI3REF: Checkpoint Mode Values
9707** KEYWORDS: {checkpoint mode}
9708**
9709** These constants define all valid values for the "checkpoint mode" passed
9710** as the third parameter to the [sqlite3_wal_checkpoint_v2()] interface.
9711** See the [sqlite3_wal_checkpoint_v2()] documentation for details on the
9712** meaning of each of these checkpoint modes.
9713*/
9714#define SQLITE_CHECKPOINT_PASSIVE0 0 /* Do as much as possible w/o blocking */
9715#define SQLITE_CHECKPOINT_FULL1 1 /* Wait for writers, then checkpoint */
9716#define SQLITE_CHECKPOINT_RESTART2 2 /* Like FULL but wait for for readers */
9717#define SQLITE_CHECKPOINT_TRUNCATE3 3 /* Like RESTART but also truncate WAL */
9718
9719/*
9720** CAPI3REF: Virtual Table Interface Configuration
9721**
9722** This function may be called by either the [xConnect] or [xCreate] method
9723** of a [virtual table] implementation to configure
9724** various facets of the virtual table interface.
9725**
9726** If this interface is invoked outside the context of an xConnect or
9727** xCreate virtual table method then the behavior is undefined.
9728**
9729** In the call sqlite3_vtab_config(D,C,...) the D parameter is the
9730** [database connection] in which the virtual table is being created and
9731** which is passed in as the first argument to the [xConnect] or [xCreate]
9732** method that is invoking sqlite3_vtab_config(). The C parameter is one
9733** of the [virtual table configuration options]. The presence and meaning
9734** of parameters after C depend on which [virtual table configuration option]
9735** is used.
9736*/
9737SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...);
9738
9739/*
9740** CAPI3REF: Virtual Table Configuration Options
9741** KEYWORDS: {virtual table configuration options}
9742** KEYWORDS: {virtual table configuration option}
9743**
9744** These macros define the various options to the
9745** [sqlite3_vtab_config()] interface that [virtual table] implementations
9746** can use to customize and optimize their behavior.
9747**
9748** <dl>
9749** [[SQLITE_VTAB_CONSTRAINT_SUPPORT]]
9750** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT</dt>
9751** <dd>Calls of the form
9752** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
9753** where X is an integer. If X is zero, then the [virtual table] whose
9754** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
9755** support constraints. In this configuration (which is the default) if
9756** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
9757** statement is rolled back as if [ON CONFLICT | OR ABORT] had been
9758** specified as part of the users SQL statement, regardless of the actual
9759** ON CONFLICT mode specified.
9760**
9761** If X is non-zero, then the virtual table implementation guarantees
9762** that if [xUpdate] returns [SQLITE_CONSTRAINT], it will do so before
9763** any modifications to internal or persistent data structures have been made.
9764** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite
9765** is able to roll back a statement or database transaction, and abandon
9766** or continue processing the current SQL statement as appropriate.
9767** If the ON CONFLICT mode is REPLACE and the [xUpdate] method returns
9768** [SQLITE_CONSTRAINT], SQLite handles this as if the ON CONFLICT mode
9769** had been ABORT.
9770**
9771** Virtual table implementations that are required to handle OR REPLACE
9772** must do so within the [xUpdate] method. If a call to the
9773** [sqlite3_vtab_on_conflict()] function indicates that the current ON
9774** CONFLICT policy is REPLACE, the virtual table implementation should
9775** silently replace the appropriate rows within the xUpdate callback and
9776** return SQLITE_OK. Or, if this is not possible, it may return
9777** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT
9778** constraint handling.
9779** </dd>
9780**
9781** [[SQLITE_VTAB_DIRECTONLY]]<dt>SQLITE_VTAB_DIRECTONLY</dt>
9782** <dd>Calls of the form
9783** [sqlite3_vtab_config](db,SQLITE_VTAB_DIRECTONLY) from within the
9784** the [xConnect] or [xCreate] methods of a [virtual table] implmentation
9785** prohibits that virtual table from being used from within triggers and
9786** views.
9787** </dd>
9788**
9789** [[SQLITE_VTAB_INNOCUOUS]]<dt>SQLITE_VTAB_INNOCUOUS</dt>
9790** <dd>Calls of the form
9791** [sqlite3_vtab_config](db,SQLITE_VTAB_INNOCUOUS) from within the
9792** the [xConnect] or [xCreate] methods of a [virtual table] implmentation
9793** identify that virtual table as being safe to use from within triggers
9794** and views. Conceptually, the SQLITE_VTAB_INNOCUOUS tag means that the
9795** virtual table can do no serious harm even if it is controlled by a
9796** malicious hacker. Developers should avoid setting the SQLITE_VTAB_INNOCUOUS
9797** flag unless absolutely necessary.
9798** </dd>
9799** </dl>
9800*/
9801#define SQLITE_VTAB_CONSTRAINT_SUPPORT1 1
9802#define SQLITE_VTAB_INNOCUOUS2 2
9803#define SQLITE_VTAB_DIRECTONLY3 3
9804
9805/*
9806** CAPI3REF: Determine The Virtual Table Conflict Policy
9807**
9808** This function may only be called from within a call to the [xUpdate] method
9809** of a [virtual table] implementation for an INSERT or UPDATE operation. ^The
9810** value returned is one of [SQLITE_ROLLBACK], [SQLITE_IGNORE], [SQLITE_FAIL],
9811** [SQLITE_ABORT], or [SQLITE_REPLACE], according to the [ON CONFLICT] mode
9812** of the SQL statement that triggered the call to the [xUpdate] method of the
9813** [virtual table].
9814*/
9815SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
9816
9817/*
9818** CAPI3REF: Determine If Virtual Table Column Access Is For UPDATE
9819**
9820** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn]
9821** method of a [virtual table], then it might return true if the
9822** column is being fetched as part of an UPDATE operation during which the
9823** column value will not change. The virtual table implementation can use
9824** this hint as permission to substitute a return value that is less
9825** expensive to compute and that the corresponding
9826** [xUpdate] method understands as a "no-change" value.
9827**
9828** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that
9829** the column is not changed by the UPDATE statement, then the xColumn
9830** method can optionally return without setting a result, without calling
9831** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces].
9832** In that case, [sqlite3_value_nochange(X)] will return true for the
9833** same column in the [xUpdate] method.
9834**
9835** The sqlite3_vtab_nochange() routine is an optimization. Virtual table
9836** implementations should continue to give a correct answer even if the
9837** sqlite3_vtab_nochange() interface were to always return false. In the
9838** current implementation, the sqlite3_vtab_nochange() interface does always
9839** returns false for the enhanced [UPDATE FROM] statement.
9840*/
9841SQLITE_API int sqlite3_vtab_nochange(sqlite3_context*);
9842
9843/*
9844** CAPI3REF: Determine The Collation For a Virtual Table Constraint
9845** METHOD: sqlite3_index_info
9846**
9847** This function may only be called from within a call to the [xBestIndex]
9848** method of a [virtual table]. This function returns a pointer to a string
9849** that is the name of the appropriate collation sequence to use for text
9850** comparisons on the constraint identified by its arguments.
9851**
9852** The first argument must be the pointer to the [sqlite3_index_info] object
9853** that is the first parameter to the xBestIndex() method. The second argument
9854** must be an index into the aConstraint[] array belonging to the
9855** sqlite3_index_info structure passed to xBestIndex.
9856**
9857** Important:
9858** The first parameter must be the same pointer that is passed into the
9859** xBestMethod() method. The first parameter may not be a pointer to a
9860** different [sqlite3_index_info] object, even an exact copy.
9861**
9862** The return value is computed as follows:
9863**
9864** <ol>
9865** <li><p> If the constraint comes from a WHERE clause expression that contains
9866** a [COLLATE operator], then the name of the collation specified by
9867** that COLLATE operator is returned.
9868** <li><p> If there is no COLLATE operator, but the column that is the subject
9869** of the constraint specifies an alternative collating sequence via
9870** a [COLLATE clause] on the column definition within the CREATE TABLE
9871** statement that was passed into [sqlite3_declare_vtab()], then the
9872** name of that alternative collating sequence is returned.
9873** <li><p> Otherwise, "BINARY" is returned.
9874** </ol>
9875*/
9876SQLITE_API SQLITE_EXPERIMENTAL const char *sqlite3_vtab_collation(sqlite3_index_info*,int);
9877
9878/*
9879** CAPI3REF: Determine if a virtual table query is DISTINCT
9880** METHOD: sqlite3_index_info
9881**
9882** This API may only be used from within an [xBestIndex|xBestIndex method]
9883** of a [virtual table] implementation. The result of calling this
9884** interface from outside of xBestIndex() is undefined and probably harmful.
9885**
9886** ^The sqlite3_vtab_distinct() interface returns an integer between 0 and
9887** 3. The integer returned by sqlite3_vtab_distinct()
9888** gives the virtual table additional information about how the query
9889** planner wants the output to be ordered. As long as the virtual table
9890** can meet the ordering requirements of the query planner, it may set
9891** the "orderByConsumed" flag.
9892**
9893** <ol><li value="0"><p>
9894** ^If the sqlite3_vtab_distinct() interface returns 0, that means
9895** that the query planner needs the virtual table to return all rows in the
9896** sort order defined by the "nOrderBy" and "aOrderBy" fields of the
9897** [sqlite3_index_info] object. This is the default expectation. If the
9898** virtual table outputs all rows in sorted order, then it is always safe for
9899** the xBestIndex method to set the "orderByConsumed" flag, regardless of
9900** the return value from sqlite3_vtab_distinct().
9901** <li value="1"><p>
9902** ^(If the sqlite3_vtab_distinct() interface returns 1, that means
9903** that the query planner does not need the rows to be returned in sorted order
9904** as long as all rows with the same values in all columns identified by the
9905** "aOrderBy" field are adjacent.)^ This mode is used when the query planner
9906** is doing a GROUP BY.
9907** <li value="2"><p>
9908** ^(If the sqlite3_vtab_distinct() interface returns 2, that means
9909** that the query planner does not need the rows returned in any particular
9910** order, as long as rows with the same values in all "aOrderBy" columns
9911** are adjacent.)^ ^(Furthermore, only a single row for each particular
9912** combination of values in the columns identified by the "aOrderBy" field
9913** needs to be returned.)^ ^It is always ok for two or more rows with the same
9914** values in all "aOrderBy" columns to be returned, as long as all such rows
9915** are adjacent. ^The virtual table may, if it chooses, omit extra rows
9916** that have the same value for all columns identified by "aOrderBy".
9917** ^However omitting the extra rows is optional.
9918** This mode is used for a DISTINCT query.
9919** <li value="3"><p>
9920** ^(If the sqlite3_vtab_distinct() interface returns 3, that means
9921** that the query planner needs only distinct rows but it does need the
9922** rows to be sorted.)^ ^The virtual table implementation is free to omit
9923** rows that are identical in all aOrderBy columns, if it wants to, but
9924** it is not required to omit any rows. This mode is used for queries
9925** that have both DISTINCT and ORDER BY clauses.
9926** </ol>
9927**
9928** ^For the purposes of comparing virtual table output values to see if the
9929** values are same value for sorting purposes, two NULL values are considered
9930** to be the same. In other words, the comparison operator is "IS"
9931** (or "IS NOT DISTINCT FROM") and not "==".
9932**
9933** If a virtual table implementation is unable to meet the requirements
9934** specified above, then it must not set the "orderByConsumed" flag in the
9935** [sqlite3_index_info] object or an incorrect answer may result.
9936**
9937** ^A virtual table implementation is always free to return rows in any order
9938** it wants, as long as the "orderByConsumed" flag is not set. ^When the
9939** the "orderByConsumed" flag is unset, the query planner will add extra
9940** [bytecode] to ensure that the final results returned by the SQL query are
9941** ordered correctly. The use of the "orderByConsumed" flag and the
9942** sqlite3_vtab_distinct() interface is merely an optimization. ^Careful
9943** use of the sqlite3_vtab_distinct() interface and the "orderByConsumed"
9944** flag might help queries against a virtual table to run faster. Being
9945** overly aggressive and setting the "orderByConsumed" flag when it is not
9946** valid to do so, on the other hand, might cause SQLite to return incorrect
9947** results.
9948*/
9949SQLITE_API int sqlite3_vtab_distinct(sqlite3_index_info*);
9950
9951/*
9952** CAPI3REF: Identify and handle IN constraints in xBestIndex
9953**
9954** This interface may only be used from within an
9955** [xBestIndex|xBestIndex() method] of a [virtual table] implementation.
9956** The result of invoking this interface from any other context is
9957** undefined and probably harmful.
9958**
9959** ^(A constraint on a virtual table of the form
9960** "[IN operator|column IN (...)]" is
9961** communicated to the xBestIndex method as a
9962** [SQLITE_INDEX_CONSTRAINT_EQ] constraint.)^ If xBestIndex wants to use
9963** this constraint, it must set the corresponding
9964** aConstraintUsage[].argvIndex to a postive integer. ^(Then, under
9965** the usual mode of handling IN operators, SQLite generates [bytecode]
9966** that invokes the [xFilter|xFilter() method] once for each value
9967** on the right-hand side of the IN operator.)^ Thus the virtual table
9968** only sees a single value from the right-hand side of the IN operator
9969** at a time.
9970**
9971** In some cases, however, it would be advantageous for the virtual
9972** table to see all values on the right-hand of the IN operator all at
9973** once. The sqlite3_vtab_in() interfaces facilitates this in two ways:
9974**
9975** <ol>
9976** <li><p>
9977** ^A call to sqlite3_vtab_in(P,N,-1) will return true (non-zero)
9978** if and only if the [sqlite3_index_info|P->aConstraint][N] constraint
9979** is an [IN operator] that can be processed all at once. ^In other words,
9980** sqlite3_vtab_in() with -1 in the third argument is a mechanism
9981** by which the virtual table can ask SQLite if all-at-once processing
9982** of the IN operator is even possible.
9983**
9984** <li><p>
9985** ^A call to sqlite3_vtab_in(P,N,F) with F==1 or F==0 indicates
9986** to SQLite that the virtual table does or does not want to process
9987** the IN operator all-at-once, respectively. ^Thus when the third
9988** parameter (F) is non-negative, this interface is the mechanism by
9989** which the virtual table tells SQLite how it wants to process the
9990** IN operator.
9991** </ol>
9992**
9993** ^The sqlite3_vtab_in(P,N,F) interface can be invoked multiple times
9994** within the same xBestIndex method call. ^For any given P,N pair,
9995** the return value from sqlite3_vtab_in(P,N,F) will always be the same
9996** within the same xBestIndex call. ^If the interface returns true
9997** (non-zero), that means that the constraint is an IN operator
9998** that can be processed all-at-once. ^If the constraint is not an IN
9999** operator or cannot be processed all-at-once, then the interface returns
10000** false.
10001**
10002** ^(All-at-once processing of the IN operator is selected if both of the
10003** following conditions are met:
10004**
10005** <ol>
10006** <li><p> The P->aConstraintUsage[N].argvIndex value is set to a positive
10007** integer. This is how the virtual table tells SQLite that it wants to
10008** use the N-th constraint.
10009**
10010** <li><p> The last call to sqlite3_vtab_in(P,N,F) for which F was
10011** non-negative had F>=1.
10012** </ol>)^
10013**
10014** ^If either or both of the conditions above are false, then SQLite uses
10015** the traditional one-at-a-time processing strategy for the IN constraint.
10016** ^If both conditions are true, then the argvIndex-th parameter to the
10017** xFilter method will be an [sqlite3_value] that appears to be NULL,
10018** but which can be passed to [sqlite3_vtab_in_first()] and
10019** [sqlite3_vtab_in_next()] to find all values on the right-hand side
10020** of the IN constraint.
10021*/
10022SQLITE_API int sqlite3_vtab_in(sqlite3_index_info*, int iCons, int bHandle);
10023
10024/*
10025** CAPI3REF: Find all elements on the right-hand side of an IN constraint.
10026**
10027** These interfaces are only useful from within the
10028** [xFilter|xFilter() method] of a [virtual table] implementation.
10029** The result of invoking these interfaces from any other context
10030** is undefined and probably harmful.
10031**
10032** The X parameter in a call to sqlite3_vtab_in_first(X,P) or
10033** sqlite3_vtab_in_next(X,P) must be one of the parameters to the
10034** xFilter method which invokes these routines, and specifically
10035** a parameter that was previously selected for all-at-once IN constraint
10036** processing use the [sqlite3_vtab_in()] interface in the
10037** [xBestIndex|xBestIndex method]. ^(If the X parameter is not
10038** an xFilter argument that was selected for all-at-once IN constraint
10039** processing, then these routines return [SQLITE_MISUSE])^ or perhaps
10040** exhibit some other undefined or harmful behavior.
10041**
10042** ^(Use these routines to access all values on the right-hand side
10043** of the IN constraint using code like the following:
10044**
10045** <blockquote><pre>
10046** &nbsp; for(rc=sqlite3_vtab_in_first(pList, &pVal);
10047** &nbsp; rc==SQLITE_OK && pVal
10048** &nbsp; rc=sqlite3_vtab_in_next(pList, &pVal)
10049** &nbsp; ){
10050** &nbsp; // do something with pVal
10051** &nbsp; }
10052** &nbsp; if( rc!=SQLITE_OK ){
10053** &nbsp; // an error has occurred
10054** &nbsp; }
10055** </pre></blockquote>)^
10056**
10057** ^On success, the sqlite3_vtab_in_first(X,P) and sqlite3_vtab_in_next(X,P)
10058** routines return SQLITE_OK and set *P to point to the first or next value
10059** on the RHS of the IN constraint. ^If there are no more values on the
10060** right hand side of the IN constraint, then *P is set to NULL and these
10061** routines return [SQLITE_DONE]. ^The return value might be
10062** some other value, such as SQLITE_NOMEM, in the event of a malfunction.
10063**
10064** The *ppOut values returned by these routines are only valid until the
10065** next call to either of these routines or until the end of the xFilter
10066** method from which these routines were called. If the virtual table
10067** implementation needs to retain the *ppOut values for longer, it must make
10068** copies. The *ppOut values are [protected sqlite3_value|protected].
10069*/
10070SQLITE_API int sqlite3_vtab_in_first(sqlite3_value *pVal, sqlite3_value **ppOut);
10071SQLITE_API int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut);
10072
10073/*
10074** CAPI3REF: Constraint values in xBestIndex()
10075** METHOD: sqlite3_index_info
10076**
10077** This API may only be used from within the [xBestIndex|xBestIndex method]
10078** of a [virtual table] implementation. The result of calling this interface
10079** from outside of an xBestIndex method are undefined and probably harmful.
10080**
10081** ^When the sqlite3_vtab_rhs_value(P,J,V) interface is invoked from within
10082** the [xBestIndex] method of a [virtual table] implementation, with P being
10083** a copy of the [sqlite3_index_info] object pointer passed into xBestIndex and
10084** J being a 0-based index into P->aConstraint[], then this routine
10085** attempts to set *V to the value of the right-hand operand of
10086** that constraint if the right-hand operand is known. ^If the
10087** right-hand operand is not known, then *V is set to a NULL pointer.
10088** ^The sqlite3_vtab_rhs_value(P,J,V) interface returns SQLITE_OK if
10089** and only if *V is set to a value. ^The sqlite3_vtab_rhs_value(P,J,V)
10090** inteface returns SQLITE_NOTFOUND if the right-hand side of the J-th
10091** constraint is not available. ^The sqlite3_vtab_rhs_value() interface
10092** can return an result code other than SQLITE_OK or SQLITE_NOTFOUND if
10093** something goes wrong.
10094**
10095** The sqlite3_vtab_rhs_value() interface is usually only successful if
10096** the right-hand operand of a constraint is a literal value in the original
10097** SQL statement. If the right-hand operand is an expression or a reference
10098** to some other column or a [host parameter], then sqlite3_vtab_rhs_value()
10099** will probably return [SQLITE_NOTFOUND].
10100**
10101** ^(Some constraints, such as [SQLITE_INDEX_CONSTRAINT_ISNULL] and
10102** [SQLITE_INDEX_CONSTRAINT_ISNOTNULL], have no right-hand operand. For such
10103** constraints, sqlite3_vtab_rhs_value() always returns SQLITE_NOTFOUND.)^
10104**
10105** ^The [sqlite3_value] object returned in *V is a protected sqlite3_value
10106** and remains valid for the duration of the xBestIndex method call.
10107** ^When xBestIndex returns, the sqlite3_value object returned by
10108** sqlite3_vtab_rhs_value() is automatically deallocated.
10109**
10110** The "_rhs_" in the name of this routine is an abbreviation for
10111** "Right-Hand Side".
10112*/
10113SQLITE_API int sqlite3_vtab_rhs_value(sqlite3_index_info*, int, sqlite3_value **ppVal);
10114
10115/*
10116** CAPI3REF: Conflict resolution modes
10117** KEYWORDS: {conflict resolution mode}
10118**
10119** These constants are returned by [sqlite3_vtab_on_conflict()] to
10120** inform a [virtual table] implementation what the [ON CONFLICT] mode
10121** is for the SQL statement being evaluated.
10122**
10123** Note that the [SQLITE_IGNORE] constant is also used as a potential
10124** return value from the [sqlite3_set_authorizer()] callback and that
10125** [SQLITE_ABORT] is also a [result code].
10126*/
10127#define SQLITE_ROLLBACK1 1
10128/* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
10129#define SQLITE_FAIL3 3
10130/* #define SQLITE_ABORT 4 // Also an error code */
10131#define SQLITE_REPLACE5 5
10132
10133/*
10134** CAPI3REF: Prepared Statement Scan Status Opcodes
10135** KEYWORDS: {scanstatus options}
10136**
10137** The following constants can be used for the T parameter to the
10138** [sqlite3_stmt_scanstatus(S,X,T,V)] interface. Each constant designates a
10139** different metric for sqlite3_stmt_scanstatus() to return.
10140**
10141** When the value returned to V is a string, space to hold that string is
10142** managed by the prepared statement S and will be automatically freed when
10143** S is finalized.
10144**
10145** <dl>
10146** [[SQLITE_SCANSTAT_NLOOP]] <dt>SQLITE_SCANSTAT_NLOOP</dt>
10147** <dd>^The [sqlite3_int64] variable pointed to by the V parameter will be
10148** set to the total number of times that the X-th loop has run.</dd>
10149**
10150** [[SQLITE_SCANSTAT_NVISIT]] <dt>SQLITE_SCANSTAT_NVISIT</dt>
10151** <dd>^The [sqlite3_int64] variable pointed to by the V parameter will be set
10152** to the total number of rows examined by all iterations of the X-th loop.</dd>
10153**
10154** [[SQLITE_SCANSTAT_EST]] <dt>SQLITE_SCANSTAT_EST</dt>
10155** <dd>^The "double" variable pointed to by the V parameter will be set to the
10156** query planner's estimate for the average number of rows output from each
10157** iteration of the X-th loop. If the query planner's estimates was accurate,
10158** then this value will approximate the quotient NVISIT/NLOOP and the
10159** product of this value for all prior loops with the same SELECTID will
10160** be the NLOOP value for the current loop.
10161**
10162** [[SQLITE_SCANSTAT_NAME]] <dt>SQLITE_SCANSTAT_NAME</dt>
10163** <dd>^The "const char *" variable pointed to by the V parameter will be set
10164** to a zero-terminated UTF-8 string containing the name of the index or table
10165** used for the X-th loop.
10166**
10167** [[SQLITE_SCANSTAT_EXPLAIN]] <dt>SQLITE_SCANSTAT_EXPLAIN</dt>
10168** <dd>^The "const char *" variable pointed to by the V parameter will be set
10169** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN]
10170** description for the X-th loop.
10171**
10172** [[SQLITE_SCANSTAT_SELECTID]] <dt>SQLITE_SCANSTAT_SELECT</dt>
10173** <dd>^The "int" variable pointed to by the V parameter will be set to the
10174** "select-id" for the X-th loop. The select-id identifies which query or
10175** subquery the loop is part of. The main query has a select-id of zero.
10176** The select-id is the same value as is output in the first column
10177** of an [EXPLAIN QUERY PLAN] query.
10178** </dl>
10179*/
10180#define SQLITE_SCANSTAT_NLOOP0 0
10181#define SQLITE_SCANSTAT_NVISIT1 1
10182#define SQLITE_SCANSTAT_EST2 2
10183#define SQLITE_SCANSTAT_NAME3 3
10184#define SQLITE_SCANSTAT_EXPLAIN4 4
10185#define SQLITE_SCANSTAT_SELECTID5 5
10186
10187/*
10188** CAPI3REF: Prepared Statement Scan Status
10189** METHOD: sqlite3_stmt
10190**
10191** This interface returns information about the predicted and measured
10192** performance for pStmt. Advanced applications can use this
10193** interface to compare the predicted and the measured performance and
10194** issue warnings and/or rerun [ANALYZE] if discrepancies are found.
10195**
10196** Since this interface is expected to be rarely used, it is only
10197** available if SQLite is compiled using the [SQLITE_ENABLE_STMT_SCANSTATUS]
10198** compile-time option.
10199**
10200** The "iScanStatusOp" parameter determines which status information to return.
10201** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior
10202** of this interface is undefined.
10203** ^The requested measurement is written into a variable pointed to by
10204** the "pOut" parameter.
10205** Parameter "idx" identifies the specific loop to retrieve statistics for.
10206** Loops are numbered starting from zero. ^If idx is out of range - less than
10207** zero or greater than or equal to the total number of loops used to implement
10208** the statement - a non-zero value is returned and the variable that pOut
10209** points to is unchanged.
10210**
10211** ^Statistics might not be available for all loops in all statements. ^In cases
10212** where there exist loops with no available statistics, this function behaves
10213** as if the loop did not exist - it returns non-zero and leave the variable
10214** that pOut points to unchanged.
10215**
10216** See also: [sqlite3_stmt_scanstatus_reset()]
10217*/
10218SQLITE_API int sqlite3_stmt_scanstatus(
10219 sqlite3_stmt *pStmt, /* Prepared statement for which info desired */
10220 int idx, /* Index of loop to report on */
10221 int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */
10222 void *pOut /* Result written here */
10223);
10224
10225/*
10226** CAPI3REF: Zero Scan-Status Counters
10227** METHOD: sqlite3_stmt
10228**
10229** ^Zero all [sqlite3_stmt_scanstatus()] related event counters.
10230**
10231** This API is only available if the library is built with pre-processor
10232** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
10233*/
10234SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
10235
10236/*
10237** CAPI3REF: Flush caches to disk mid-transaction
10238** METHOD: sqlite3
10239**
10240** ^If a write-transaction is open on [database connection] D when the
10241** [sqlite3_db_cacheflush(D)] interface invoked, any dirty
10242** pages in the pager-cache that are not currently in use are written out
10243** to disk. A dirty page may be in use if a database cursor created by an
10244** active SQL statement is reading from it, or if it is page 1 of a database
10245** file (page 1 is always "in use"). ^The [sqlite3_db_cacheflush(D)]
10246** interface flushes caches for all schemas - "main", "temp", and
10247** any [attached] databases.
10248**
10249** ^If this function needs to obtain extra database locks before dirty pages
10250** can be flushed to disk, it does so. ^If those locks cannot be obtained
10251** immediately and there is a busy-handler callback configured, it is invoked
10252** in the usual manner. ^If the required lock still cannot be obtained, then
10253** the database is skipped and an attempt made to flush any dirty pages
10254** belonging to the next (if any) database. ^If any databases are skipped
10255** because locks cannot be obtained, but no other error occurs, this
10256** function returns SQLITE_BUSY.
10257**
10258** ^If any other error occurs while flushing dirty pages to disk (for
10259** example an IO error or out-of-memory condition), then processing is
10260** abandoned and an SQLite [error code] is returned to the caller immediately.
10261**
10262** ^Otherwise, if no error occurs, [sqlite3_db_cacheflush()] returns SQLITE_OK.
10263**
10264** ^This function does not set the database handle error code or message
10265** returned by the [sqlite3_errcode()] and [sqlite3_errmsg()] functions.
10266*/
10267SQLITE_API int sqlite3_db_cacheflush(sqlite3*);
10268
10269/*
10270** CAPI3REF: The pre-update hook.
10271** METHOD: sqlite3
10272**
10273** ^These interfaces are only available if SQLite is compiled using the
10274** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
10275**
10276** ^The [sqlite3_preupdate_hook()] interface registers a callback function
10277** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
10278** on a database table.
10279** ^At most one preupdate hook may be registered at a time on a single
10280** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
10281** the previous setting.
10282** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
10283** with a NULL pointer as the second parameter.
10284** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
10285** the first parameter to callbacks.
10286**
10287** ^The preupdate hook only fires for changes to real database tables; the
10288** preupdate hook is not invoked for changes to [virtual tables] or to
10289** system tables like sqlite_sequence or sqlite_stat1.
10290**
10291** ^The second parameter to the preupdate callback is a pointer to
10292** the [database connection] that registered the preupdate hook.
10293** ^The third parameter to the preupdate callback is one of the constants
10294** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the
10295** kind of update operation that is about to occur.
10296** ^(The fourth parameter to the preupdate callback is the name of the
10297** database within the database connection that is being modified. This
10298** will be "main" for the main database or "temp" for TEMP tables or
10299** the name given after the AS keyword in the [ATTACH] statement for attached
10300** databases.)^
10301** ^The fifth parameter to the preupdate callback is the name of the
10302** table that is being modified.
10303**
10304** For an UPDATE or DELETE operation on a [rowid table], the sixth
10305** parameter passed to the preupdate callback is the initial [rowid] of the
10306** row being modified or deleted. For an INSERT operation on a rowid table,
10307** or any operation on a WITHOUT ROWID table, the value of the sixth
10308** parameter is undefined. For an INSERT or UPDATE on a rowid table the
10309** seventh parameter is the final rowid value of the row being inserted
10310** or updated. The value of the seventh parameter passed to the callback
10311** function is not defined for operations on WITHOUT ROWID tables, or for
10312** DELETE operations on rowid tables.
10313**
10314** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
10315** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
10316** provide additional information about a preupdate event. These routines
10317** may only be called from within a preupdate callback. Invoking any of
10318** these routines from outside of a preupdate callback or with a
10319** [database connection] pointer that is different from the one supplied
10320** to the preupdate callback results in undefined and probably undesirable
10321** behavior.
10322**
10323** ^The [sqlite3_preupdate_count(D)] interface returns the number of columns
10324** in the row that is being inserted, updated, or deleted.
10325**
10326** ^The [sqlite3_preupdate_old(D,N,P)] interface writes into P a pointer to
10327** a [protected sqlite3_value] that contains the value of the Nth column of
10328** the table row before it is updated. The N parameter must be between 0
10329** and one less than the number of columns or the behavior will be
10330** undefined. This must only be used within SQLITE_UPDATE and SQLITE_DELETE
10331** preupdate callbacks; if it is used by an SQLITE_INSERT callback then the
10332** behavior is undefined. The [sqlite3_value] that P points to
10333** will be destroyed when the preupdate callback returns.
10334**
10335** ^The [sqlite3_preupdate_new(D,N,P)] interface writes into P a pointer to
10336** a [protected sqlite3_value] that contains the value of the Nth column of
10337** the table row after it is updated. The N parameter must be between 0
10338** and one less than the number of columns or the behavior will be
10339** undefined. This must only be used within SQLITE_INSERT and SQLITE_UPDATE
10340** preupdate callbacks; if it is used by an SQLITE_DELETE callback then the
10341** behavior is undefined. The [sqlite3_value] that P points to
10342** will be destroyed when the preupdate callback returns.
10343**
10344** ^The [sqlite3_preupdate_depth(D)] interface returns 0 if the preupdate
10345** callback was invoked as a result of a direct insert, update, or delete
10346** operation; or 1 for inserts, updates, or deletes invoked by top-level
10347** triggers; or 2 for changes resulting from triggers called by top-level
10348** triggers; and so forth.
10349**
10350** When the [sqlite3_blob_write()] API is used to update a blob column,
10351** the pre-update hook is invoked with SQLITE_DELETE. This is because the
10352** in this case the new values are not available. In this case, when a
10353** callback made with op==SQLITE_DELETE is actuall a write using the
10354** sqlite3_blob_write() API, the [sqlite3_preupdate_blobwrite()] returns
10355** the index of the column being written. In other cases, where the
10356** pre-update hook is being invoked for some other reason, including a
10357** regular DELETE, sqlite3_preupdate_blobwrite() returns -1.
10358**
10359** See also: [sqlite3_update_hook()]
10360*/
10361#if defined(SQLITE_ENABLE_PREUPDATE_HOOK1)
10362SQLITE_API void *sqlite3_preupdate_hook(
10363 sqlite3 *db,
10364 void(*xPreUpdate)(
10365 void *pCtx, /* Copy of third arg to preupdate_hook() */
10366 sqlite3 *db, /* Database handle */
10367 int op, /* SQLITE_UPDATE, DELETE or INSERT */
10368 char const *zDb, /* Database name */
10369 char const *zName, /* Table name */
10370 sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */
10371 sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */
10372 ),
10373 void*
10374);
10375SQLITE_API int sqlite3_preupdate_old(sqlite3 *, int, sqlite3_value **);
10376SQLITE_API int sqlite3_preupdate_count(sqlite3 *);
10377SQLITE_API int sqlite3_preupdate_depth(sqlite3 *);
10378SQLITE_API int sqlite3_preupdate_new(sqlite3 *, int, sqlite3_value **);
10379SQLITE_API int sqlite3_preupdate_blobwrite(sqlite3 *);
10380#endif
10381
10382/*
10383** CAPI3REF: Low-level system error code
10384** METHOD: sqlite3
10385**
10386** ^Attempt to return the underlying operating system error code or error
10387** number that caused the most recent I/O error or failure to open a file.
10388** The return value is OS-dependent. For example, on unix systems, after
10389** [sqlite3_open_v2()] returns [SQLITE_CANTOPEN], this interface could be
10390** called to get back the underlying "errno" that caused the problem, such
10391** as ENOSPC, EAUTH, EISDIR, and so forth.
10392*/
10393SQLITE_API int sqlite3_system_errno(sqlite3*);
10394
10395/*
10396** CAPI3REF: Database Snapshot
10397** KEYWORDS: {snapshot} {sqlite3_snapshot}
10398**
10399** An instance of the snapshot object records the state of a [WAL mode]
10400** database for some specific point in history.
10401**
10402** In [WAL mode], multiple [database connections] that are open on the
10403** same database file can each be reading a different historical version
10404** of the database file. When a [database connection] begins a read
10405** transaction, that connection sees an unchanging copy of the database
10406** as it existed for the point in time when the transaction first started.
10407** Subsequent changes to the database from other connections are not seen
10408** by the reader until a new read transaction is started.
10409**
10410** The sqlite3_snapshot object records state information about an historical
10411** version of the database file so that it is possible to later open a new read
10412** transaction that sees that historical version of the database rather than
10413** the most recent version.
10414*/
10415typedef struct sqlite3_snapshot {
10416 unsigned char hidden[48];
10417} sqlite3_snapshot;
10418
10419/*
10420** CAPI3REF: Record A Database Snapshot
10421** CONSTRUCTOR: sqlite3_snapshot
10422**
10423** ^The [sqlite3_snapshot_get(D,S,P)] interface attempts to make a
10424** new [sqlite3_snapshot] object that records the current state of
10425** schema S in database connection D. ^On success, the
10426** [sqlite3_snapshot_get(D,S,P)] interface writes a pointer to the newly
10427** created [sqlite3_snapshot] object into *P and returns SQLITE_OK.
10428** If there is not already a read-transaction open on schema S when
10429** this function is called, one is opened automatically.
10430**
10431** The following must be true for this function to succeed. If any of
10432** the following statements are false when sqlite3_snapshot_get() is
10433** called, SQLITE_ERROR is returned. The final value of *P is undefined
10434** in this case.
10435**
10436** <ul>
10437** <li> The database handle must not be in [autocommit mode].
10438**
10439** <li> Schema S of [database connection] D must be a [WAL mode] database.
10440**
10441** <li> There must not be a write transaction open on schema S of database
10442** connection D.
10443**
10444** <li> One or more transactions must have been written to the current wal
10445** file since it was created on disk (by any connection). This means
10446** that a snapshot cannot be taken on a wal mode database with no wal
10447** file immediately after it is first opened. At least one transaction
10448** must be written to it first.
10449** </ul>
10450**
10451** This function may also return SQLITE_NOMEM. If it is called with the
10452** database handle in autocommit mode but fails for some other reason,
10453** whether or not a read transaction is opened on schema S is undefined.
10454**
10455** The [sqlite3_snapshot] object returned from a successful call to
10456** [sqlite3_snapshot_get()] must be freed using [sqlite3_snapshot_free()]
10457** to avoid a memory leak.
10458**
10459** The [sqlite3_snapshot_get()] interface is only available when the
10460** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10461*/
10462SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_get(
10463 sqlite3 *db,
10464 const char *zSchema,
10465 sqlite3_snapshot **ppSnapshot
10466);
10467
10468/*
10469** CAPI3REF: Start a read transaction on an historical snapshot
10470** METHOD: sqlite3_snapshot
10471**
10472** ^The [sqlite3_snapshot_open(D,S,P)] interface either starts a new read
10473** transaction or upgrades an existing one for schema S of
10474** [database connection] D such that the read transaction refers to
10475** historical [snapshot] P, rather than the most recent change to the
10476** database. ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK
10477** on success or an appropriate [error code] if it fails.
10478**
10479** ^In order to succeed, the database connection must not be in
10480** [autocommit mode] when [sqlite3_snapshot_open(D,S,P)] is called. If there
10481** is already a read transaction open on schema S, then the database handle
10482** must have no active statements (SELECT statements that have been passed
10483** to sqlite3_step() but not sqlite3_reset() or sqlite3_finalize()).
10484** SQLITE_ERROR is returned if either of these conditions is violated, or
10485** if schema S does not exist, or if the snapshot object is invalid.
10486**
10487** ^A call to sqlite3_snapshot_open() will fail to open if the specified
10488** snapshot has been overwritten by a [checkpoint]. In this case
10489** SQLITE_ERROR_SNAPSHOT is returned.
10490**
10491** If there is already a read transaction open when this function is
10492** invoked, then the same read transaction remains open (on the same
10493** database snapshot) if SQLITE_ERROR, SQLITE_BUSY or SQLITE_ERROR_SNAPSHOT
10494** is returned. If another error code - for example SQLITE_PROTOCOL or an
10495** SQLITE_IOERR error code - is returned, then the final state of the
10496** read transaction is undefined. If SQLITE_OK is returned, then the
10497** read transaction is now open on database snapshot P.
10498**
10499** ^(A call to [sqlite3_snapshot_open(D,S,P)] will fail if the
10500** database connection D does not know that the database file for
10501** schema S is in [WAL mode]. A database connection might not know
10502** that the database file is in [WAL mode] if there has been no prior
10503** I/O on that database connection, or if the database entered [WAL mode]
10504** after the most recent I/O on the database connection.)^
10505** (Hint: Run "[PRAGMA application_id]" against a newly opened
10506** database connection in order to make it ready to use snapshots.)
10507**
10508** The [sqlite3_snapshot_open()] interface is only available when the
10509** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10510*/
10511SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_open(
10512 sqlite3 *db,
10513 const char *zSchema,
10514 sqlite3_snapshot *pSnapshot
10515);
10516
10517/*
10518** CAPI3REF: Destroy a snapshot
10519** DESTRUCTOR: sqlite3_snapshot
10520**
10521** ^The [sqlite3_snapshot_free(P)] interface destroys [sqlite3_snapshot] P.
10522** The application must eventually free every [sqlite3_snapshot] object
10523** using this routine to avoid a memory leak.
10524**
10525** The [sqlite3_snapshot_free()] interface is only available when the
10526** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10527*/
10528SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_snapshot_free(sqlite3_snapshot*);
10529
10530/*
10531** CAPI3REF: Compare the ages of two snapshot handles.
10532** METHOD: sqlite3_snapshot
10533**
10534** The sqlite3_snapshot_cmp(P1, P2) interface is used to compare the ages
10535** of two valid snapshot handles.
10536**
10537** If the two snapshot handles are not associated with the same database
10538** file, the result of the comparison is undefined.
10539**
10540** Additionally, the result of the comparison is only valid if both of the
10541** snapshot handles were obtained by calling sqlite3_snapshot_get() since the
10542** last time the wal file was deleted. The wal file is deleted when the
10543** database is changed back to rollback mode or when the number of database
10544** clients drops to zero. If either snapshot handle was obtained before the
10545** wal file was last deleted, the value returned by this function
10546** is undefined.
10547**
10548** Otherwise, this API returns a negative value if P1 refers to an older
10549** snapshot than P2, zero if the two handles refer to the same database
10550** snapshot, and a positive value if P1 is a newer snapshot than P2.
10551**
10552** This interface is only available if SQLite is compiled with the
10553** [SQLITE_ENABLE_SNAPSHOT] option.
10554*/
10555SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_cmp(
10556 sqlite3_snapshot *p1,
10557 sqlite3_snapshot *p2
10558);
10559
10560/*
10561** CAPI3REF: Recover snapshots from a wal file
10562** METHOD: sqlite3_snapshot
10563**
10564** If a [WAL file] remains on disk after all database connections close
10565** (either through the use of the [SQLITE_FCNTL_PERSIST_WAL] [file control]
10566** or because the last process to have the database opened exited without
10567** calling [sqlite3_close()]) and a new connection is subsequently opened
10568** on that database and [WAL file], the [sqlite3_snapshot_open()] interface
10569** will only be able to open the last transaction added to the WAL file
10570** even though the WAL file contains other valid transactions.
10571**
10572** This function attempts to scan the WAL file associated with database zDb
10573** of database handle db and make all valid snapshots available to
10574** sqlite3_snapshot_open(). It is an error if there is already a read
10575** transaction open on the database, or if the database is not a WAL mode
10576** database.
10577**
10578** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
10579**
10580** This interface is only available if SQLite is compiled with the
10581** [SQLITE_ENABLE_SNAPSHOT] option.
10582*/
10583SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
10584
10585/*
10586** CAPI3REF: Serialize a database
10587**
10588** The sqlite3_serialize(D,S,P,F) interface returns a pointer to memory
10589** that is a serialization of the S database on [database connection] D.
10590** If P is not a NULL pointer, then the size of the database in bytes
10591** is written into *P.
10592**
10593** For an ordinary on-disk database file, the serialization is just a
10594** copy of the disk file. For an in-memory database or a "TEMP" database,
10595** the serialization is the same sequence of bytes which would be written
10596** to disk if that database where backed up to disk.
10597**
10598** The usual case is that sqlite3_serialize() copies the serialization of
10599** the database into memory obtained from [sqlite3_malloc64()] and returns
10600** a pointer to that memory. The caller is responsible for freeing the
10601** returned value to avoid a memory leak. However, if the F argument
10602** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations
10603** are made, and the sqlite3_serialize() function will return a pointer
10604** to the contiguous memory representation of the database that SQLite
10605** is currently using for that database, or NULL if the no such contiguous
10606** memory representation of the database exists. A contiguous memory
10607** representation of the database will usually only exist if there has
10608** been a prior call to [sqlite3_deserialize(D,S,...)] with the same
10609** values of D and S.
10610** The size of the database is written into *P even if the
10611** SQLITE_SERIALIZE_NOCOPY bit is set but no contiguous copy
10612** of the database exists.
10613**
10614** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
10615** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
10616** allocation error occurs.
10617**
10618** This interface is omitted if SQLite is compiled with the
10619** [SQLITE_OMIT_DESERIALIZE] option.
10620*/
10621SQLITE_API unsigned char *sqlite3_serialize(
10622 sqlite3 *db, /* The database connection */
10623 const char *zSchema, /* Which DB to serialize. ex: "main", "temp", ... */
10624 sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */
10625 unsigned int mFlags /* Zero or more SQLITE_SERIALIZE_* flags */
10626);
10627
10628/*
10629** CAPI3REF: Flags for sqlite3_serialize
10630**
10631** Zero or more of the following constants can be OR-ed together for
10632** the F argument to [sqlite3_serialize(D,S,P,F)].
10633**
10634** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return
10635** a pointer to contiguous in-memory database that it is currently using,
10636** without making a copy of the database. If SQLite is not currently using
10637** a contiguous in-memory database, then this option causes
10638** [sqlite3_serialize()] to return a NULL pointer. SQLite will only be
10639** using a contiguous in-memory database if it has been initialized by a
10640** prior call to [sqlite3_deserialize()].
10641*/
10642#define SQLITE_SERIALIZE_NOCOPY0x001 0x001 /* Do no memory allocations */
10643
10644/*
10645** CAPI3REF: Deserialize a database
10646**
10647** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the
10648** [database connection] D to disconnect from database S and then
10649** reopen S as an in-memory database based on the serialization contained
10650** in P. The serialized database P is N bytes in size. M is the size of
10651** the buffer P, which might be larger than N. If M is larger than N, and
10652** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
10653** permitted to add content to the in-memory database as long as the total
10654** size does not exceed M bytes.
10655**
10656** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will
10657** invoke sqlite3_free() on the serialization buffer when the database
10658** connection closes. If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then
10659** SQLite will try to increase the buffer size using sqlite3_realloc64()
10660** if writes on the database cause it to grow larger than M bytes.
10661**
10662** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the
10663** database is currently in a read transaction or is involved in a backup
10664** operation.
10665**
10666** It is not possible to deserialized into the TEMP database. If the
10667** S argument to sqlite3_deserialize(D,S,P,N,M,F) is "temp" then the
10668** function returns SQLITE_ERROR.
10669**
10670** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the
10671** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then
10672** [sqlite3_free()] is invoked on argument P prior to returning.
10673**
10674** This interface is omitted if SQLite is compiled with the
10675** [SQLITE_OMIT_DESERIALIZE] option.
10676*/
10677SQLITE_API int sqlite3_deserialize(
10678 sqlite3 *db, /* The database connection */
10679 const char *zSchema, /* Which DB to reopen with the deserialization */
10680 unsigned char *pData, /* The serialized database content */
10681 sqlite3_int64 szDb, /* Number bytes in the deserialization */
10682 sqlite3_int64 szBuf, /* Total size of buffer pData[] */
10683 unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */
10684);
10685
10686/*
10687** CAPI3REF: Flags for sqlite3_deserialize()
10688**
10689** The following are allowed values for 6th argument (the F argument) to
10690** the [sqlite3_deserialize(D,S,P,N,M,F)] interface.
10691**
10692** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
10693** in the P argument is held in memory obtained from [sqlite3_malloc64()]
10694** and that SQLite should take ownership of this memory and automatically
10695** free it when it has finished using it. Without this flag, the caller
10696** is responsible for freeing any dynamically allocated memory.
10697**
10698** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
10699** grow the size of the database using calls to [sqlite3_realloc64()]. This
10700** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
10701** Without this flag, the deserialized database cannot increase in size beyond
10702** the number of bytes specified by the M parameter.
10703**
10704** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database
10705** should be treated as read-only.
10706*/
10707#define SQLITE_DESERIALIZE_FREEONCLOSE1 1 /* Call sqlite3_free() on close */
10708#define SQLITE_DESERIALIZE_RESIZEABLE2 2 /* Resize using sqlite3_realloc64() */
10709#define SQLITE_DESERIALIZE_READONLY4 4 /* Database is read-only */
10710
10711/*
10712** Undo the hack that converts floating point types to integer for
10713** builds on processors without floating point support.
10714*/
10715#ifdef SQLITE_OMIT_FLOATING_POINT
10716# undef double
10717#endif
10718
10719#if 0
10720} /* End of the 'extern "C"' block */
10721#endif
10722#endif /* SQLITE3_H */
10723
10724/******** Begin file sqlite3rtree.h *********/
10725/*
10726** 2010 August 30
10727**
10728** The author disclaims copyright to this source code. In place of
10729** a legal notice, here is a blessing:
10730**
10731** May you do good and not evil.
10732** May you find forgiveness for yourself and forgive others.
10733** May you share freely, never taking more than you give.
10734**
10735*************************************************************************
10736*/
10737
10738#ifndef _SQLITE3RTREE_H_
10739#define _SQLITE3RTREE_H_
10740
10741
10742#if 0
10743extern "C" {
10744#endif
10745
10746typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
10747typedef struct sqlite3_rtree_query_info sqlite3_rtree_query_info;
10748
10749/* The double-precision datatype used by RTree depends on the
10750** SQLITE_RTREE_INT_ONLY compile-time option.
10751*/
10752#ifdef SQLITE_RTREE_INT_ONLY
10753 typedef sqlite3_int64 sqlite3_rtree_dbl;
10754#else
10755 typedef double sqlite3_rtree_dbl;
10756#endif
10757
10758/*
10759** Register a geometry callback named zGeom that can be used as part of an
10760** R-Tree geometry query as follows:
10761**
10762** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
10763*/
10764SQLITE_API int sqlite3_rtree_geometry_callback(
10765 sqlite3 *db,
10766 const char *zGeom,
10767 int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*),
10768 void *pContext
10769);
10770
10771
10772/*
10773** A pointer to a structure of the following type is passed as the first
10774** argument to callbacks registered using rtree_geometry_callback().
10775*/
10776struct sqlite3_rtree_geometry {
10777 void *pContext; /* Copy of pContext passed to s_r_g_c() */
10778 int nParam; /* Size of array aParam[] */
10779 sqlite3_rtree_dbl *aParam; /* Parameters passed to SQL geom function */
10780 void *pUser; /* Callback implementation user data */
10781 void (*xDelUser)(void *); /* Called by SQLite to clean up pUser */
10782};
10783
10784/*
10785** Register a 2nd-generation geometry callback named zScore that can be
10786** used as part of an R-Tree geometry query as follows:
10787**
10788** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zQueryFunc(... params ...)
10789*/
10790SQLITE_API int sqlite3_rtree_query_callback(
10791 sqlite3 *db,
10792 const char *zQueryFunc,
10793 int (*xQueryFunc)(sqlite3_rtree_query_info*),
10794 void *pContext,
10795 void (*xDestructor)(void*)
10796);
10797
10798
10799/*
10800** A pointer to a structure of the following type is passed as the
10801** argument to scored geometry callback registered using
10802** sqlite3_rtree_query_callback().
10803**
10804** Note that the first 5 fields of this structure are identical to
10805** sqlite3_rtree_geometry. This structure is a subclass of
10806** sqlite3_rtree_geometry.
10807*/
10808struct sqlite3_rtree_query_info {
10809 void *pContext; /* pContext from when function registered */
10810 int nParam; /* Number of function parameters */
10811 sqlite3_rtree_dbl *aParam; /* value of function parameters */
10812 void *pUser; /* callback can use this, if desired */
10813 void (*xDelUser)(void*); /* function to free pUser */
10814 sqlite3_rtree_dbl *aCoord; /* Coordinates of node or entry to check */
10815 unsigned int *anQueue; /* Number of pending entries in the queue */
10816 int nCoord; /* Number of coordinates */
10817 int iLevel; /* Level of current node or entry */
10818 int mxLevel; /* The largest iLevel value in the tree */
10819 sqlite3_int64 iRowid; /* Rowid for current entry */
10820 sqlite3_rtree_dbl rParentScore; /* Score of parent node */
10821 int eParentWithin; /* Visibility of parent node */
10822 int eWithin; /* OUT: Visibility */
10823 sqlite3_rtree_dbl rScore; /* OUT: Write the score here */
10824 /* The following fields are only available in 3.8.11 and later */
10825 sqlite3_value **apSqlParam; /* Original SQL values of parameters */
10826};
10827
10828/*
10829** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
10830*/
10831#define NOT_WITHIN0 0 /* Object completely outside of query region */
10832#define PARTLY_WITHIN1 1 /* Object partially overlaps query region */
10833#define FULLY_WITHIN2 2 /* Object fully contained within query region */
10834
10835
10836#if 0
10837} /* end of the 'extern "C"' block */
10838#endif
10839
10840#endif /* ifndef _SQLITE3RTREE_H_ */
10841
10842/******** End of sqlite3rtree.h *********/
10843/******** Begin file sqlite3session.h *********/
10844
10845#if !defined(__SQLITESESSION_H_1) && defined(SQLITE_ENABLE_SESSION1)
10846#define __SQLITESESSION_H_1 1
10847
10848/*
10849** Make sure we can call this stuff from C++.
10850*/
10851#if 0
10852extern "C" {
10853#endif
10854
10855
10856/*
10857** CAPI3REF: Session Object Handle
10858**
10859** An instance of this object is a [session] that can be used to
10860** record changes to a database.
10861*/
10862typedef struct sqlite3_session sqlite3_session;
10863
10864/*
10865** CAPI3REF: Changeset Iterator Handle
10866**
10867** An instance of this object acts as a cursor for iterating
10868** over the elements of a [changeset] or [patchset].
10869*/
10870typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
10871
10872/*
10873** CAPI3REF: Create A New Session Object
10874** CONSTRUCTOR: sqlite3_session
10875**
10876** Create a new session object attached to database handle db. If successful,
10877** a pointer to the new object is written to *ppSession and SQLITE_OK is
10878** returned. If an error occurs, *ppSession is set to NULL and an SQLite
10879** error code (e.g. SQLITE_NOMEM) is returned.
10880**
10881** It is possible to create multiple session objects attached to a single
10882** database handle.
10883**
10884** Session objects created using this function should be deleted using the
10885** [sqlite3session_delete()] function before the database handle that they
10886** are attached to is itself closed. If the database handle is closed before
10887** the session object is deleted, then the results of calling any session
10888** module function, including [sqlite3session_delete()] on the session object
10889** are undefined.
10890**
10891** Because the session module uses the [sqlite3_preupdate_hook()] API, it
10892** is not possible for an application to register a pre-update hook on a
10893** database handle that has one or more session objects attached. Nor is
10894** it possible to create a session object attached to a database handle for
10895** which a pre-update hook is already defined. The results of attempting
10896** either of these things are undefined.
10897**
10898** The session object will be used to create changesets for tables in
10899** database zDb, where zDb is either "main", or "temp", or the name of an
10900** attached database. It is not an error if database zDb is not attached
10901** to the database when the session object is created.
10902*/
10903SQLITE_API int sqlite3session_create(
10904 sqlite3 *db, /* Database handle */
10905 const char *zDb, /* Name of db (e.g. "main") */
10906 sqlite3_session **ppSession /* OUT: New session object */
10907);
10908
10909/*
10910** CAPI3REF: Delete A Session Object
10911** DESTRUCTOR: sqlite3_session
10912**
10913** Delete a session object previously allocated using
10914** [sqlite3session_create()]. Once a session object has been deleted, the
10915** results of attempting to use pSession with any other session module
10916** function are undefined.
10917**
10918** Session objects must be deleted before the database handle to which they
10919** are attached is closed. Refer to the documentation for
10920** [sqlite3session_create()] for details.
10921*/
10922SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
10923
10924/*
10925** CAPIREF: Conigure a Session Object
10926** METHOD: sqlite3_session
10927**
10928** This method is used to configure a session object after it has been
10929** created. At present the only valid value for the second parameter is
10930** [SQLITE_SESSION_OBJCONFIG_SIZE].
10931**
10932** Arguments for sqlite3session_object_config()
10933**
10934** The following values may passed as the the 4th parameter to
10935** sqlite3session_object_config().
10936**
10937** <dt>SQLITE_SESSION_OBJCONFIG_SIZE <dd>
10938** This option is used to set, clear or query the flag that enables
10939** the [sqlite3session_changeset_size()] API. Because it imposes some
10940** computational overhead, this API is disabled by default. Argument
10941** pArg must point to a value of type (int). If the value is initially
10942** 0, then the sqlite3session_changeset_size() API is disabled. If it
10943** is greater than 0, then the same API is enabled. Or, if the initial
10944** value is less than zero, no change is made. In all cases the (int)
10945** variable is set to 1 if the sqlite3session_changeset_size() API is
10946** enabled following the current call, or 0 otherwise.
10947**
10948** It is an error (SQLITE_MISUSE) to attempt to modify this setting after
10949** the first table has been attached to the session object.
10950*/
10951SQLITE_API int sqlite3session_object_config(sqlite3_session*, int op, void *pArg);
10952
10953/*
10954*/
10955#define SQLITE_SESSION_OBJCONFIG_SIZE1 1
10956
10957/*
10958** CAPI3REF: Enable Or Disable A Session Object
10959** METHOD: sqlite3_session
10960**
10961** Enable or disable the recording of changes by a session object. When
10962** enabled, a session object records changes made to the database. When
10963** disabled - it does not. A newly created session object is enabled.
10964** Refer to the documentation for [sqlite3session_changeset()] for further
10965** details regarding how enabling and disabling a session object affects
10966** the eventual changesets.
10967**
10968** Passing zero to this function disables the session. Passing a value
10969** greater than zero enables it. Passing a value less than zero is a
10970** no-op, and may be used to query the current state of the session.
10971**
10972** The return value indicates the final state of the session object: 0 if
10973** the session is disabled, or 1 if it is enabled.
10974*/
10975SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
10976
10977/*
10978** CAPI3REF: Set Or Clear the Indirect Change Flag
10979** METHOD: sqlite3_session
10980**
10981** Each change recorded by a session object is marked as either direct or
10982** indirect. A change is marked as indirect if either:
10983**
10984** <ul>
10985** <li> The session object "indirect" flag is set when the change is
10986** made, or
10987** <li> The change is made by an SQL trigger or foreign key action
10988** instead of directly as a result of a users SQL statement.
10989** </ul>
10990**
10991** If a single row is affected by more than one operation within a session,
10992** then the change is considered indirect if all operations meet the criteria
10993** for an indirect change above, or direct otherwise.
10994**
10995** This function is used to set, clear or query the session object indirect
10996** flag. If the second argument passed to this function is zero, then the
10997** indirect flag is cleared. If it is greater than zero, the indirect flag
10998** is set. Passing a value less than zero does not modify the current value
10999** of the indirect flag, and may be used to query the current state of the
11000** indirect flag for the specified session object.
11001**
11002** The return value indicates the final state of the indirect flag: 0 if
11003** it is clear, or 1 if it is set.
11004*/
11005SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
11006
11007/*
11008** CAPI3REF: Attach A Table To A Session Object
11009** METHOD: sqlite3_session
11010**
11011** If argument zTab is not NULL, then it is the name of a table to attach
11012** to the session object passed as the first argument. All subsequent changes
11013** made to the table while the session object is enabled will be recorded. See
11014** documentation for [sqlite3session_changeset()] for further details.
11015**
11016** Or, if argument zTab is NULL, then changes are recorded for all tables
11017** in the database. If additional tables are added to the database (by
11018** executing "CREATE TABLE" statements) after this call is made, changes for
11019** the new tables are also recorded.
11020**
11021** Changes can only be recorded for tables that have a PRIMARY KEY explicitly
11022** defined as part of their CREATE TABLE statement. It does not matter if the
11023** PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) or not. The PRIMARY
11024** KEY may consist of a single column, or may be a composite key.
11025**
11026** It is not an error if the named table does not exist in the database. Nor
11027** is it an error if the named table does not have a PRIMARY KEY. However,
11028** no changes will be recorded in either of these scenarios.
11029**
11030** Changes are not recorded for individual rows that have NULL values stored
11031** in one or more of their PRIMARY KEY columns.
11032**
11033** SQLITE_OK is returned if the call completes without error. Or, if an error
11034** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
11035**
11036** <h3>Special sqlite_stat1 Handling</h3>
11037**
11038** As of SQLite version 3.22.0, the "sqlite_stat1" table is an exception to
11039** some of the rules above. In SQLite, the schema of sqlite_stat1 is:
11040** <pre>
11041** &nbsp; CREATE TABLE sqlite_stat1(tbl,idx,stat)
11042** </pre>
11043**
11044** Even though sqlite_stat1 does not have a PRIMARY KEY, changes are
11045** recorded for it as if the PRIMARY KEY is (tbl,idx). Additionally, changes
11046** are recorded for rows for which (idx IS NULL) is true. However, for such
11047** rows a zero-length blob (SQL value X'') is stored in the changeset or
11048** patchset instead of a NULL value. This allows such changesets to be
11049** manipulated by legacy implementations of sqlite3changeset_invert(),
11050** concat() and similar.
11051**
11052** The sqlite3changeset_apply() function automatically converts the
11053** zero-length blob back to a NULL value when updating the sqlite_stat1
11054** table. However, if the application calls sqlite3changeset_new(),
11055** sqlite3changeset_old() or sqlite3changeset_conflict on a changeset
11056** iterator directly (including on a changeset iterator passed to a
11057** conflict-handler callback) then the X'' value is returned. The application
11058** must translate X'' to NULL itself if required.
11059**
11060** Legacy (older than 3.22.0) versions of the sessions module cannot capture
11061** changes made to the sqlite_stat1 table. Legacy versions of the
11062** sqlite3changeset_apply() function silently ignore any modifications to the
11063** sqlite_stat1 table that are part of a changeset or patchset.
11064*/
11065SQLITE_API int sqlite3session_attach(
11066 sqlite3_session *pSession, /* Session object */
11067 const char *zTab /* Table name */
11068);
11069
11070/*
11071** CAPI3REF: Set a table filter on a Session Object.
11072** METHOD: sqlite3_session
11073**
11074** The second argument (xFilter) is the "filter callback". For changes to rows
11075** in tables that are not attached to the Session object, the filter is called
11076** to determine whether changes to the table's rows should be tracked or not.
11077** If xFilter returns 0, changes are not tracked. Note that once a table is
11078** attached, xFilter will not be called again.
11079*/
11080SQLITE_API void sqlite3session_table_filter(
11081 sqlite3_session *pSession, /* Session object */
11082 int(*xFilter)(
11083 void *pCtx, /* Copy of third arg to _filter_table() */
11084 const char *zTab /* Table name */
11085 ),
11086 void *pCtx /* First argument passed to xFilter */
11087);
11088
11089/*
11090** CAPI3REF: Generate A Changeset From A Session Object
11091** METHOD: sqlite3_session
11092**
11093** Obtain a changeset containing changes to the tables attached to the
11094** session object passed as the first argument. If successful,
11095** set *ppChangeset to point to a buffer containing the changeset
11096** and *pnChangeset to the size of the changeset in bytes before returning
11097** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
11098** zero and return an SQLite error code.
11099**
11100** A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes,
11101** each representing a change to a single row of an attached table. An INSERT
11102** change contains the values of each field of a new database row. A DELETE
11103** contains the original values of each field of a deleted database row. An
11104** UPDATE change contains the original values of each field of an updated
11105** database row along with the updated values for each updated non-primary-key
11106** column. It is not possible for an UPDATE change to represent a change that
11107** modifies the values of primary key columns. If such a change is made, it
11108** is represented in a changeset as a DELETE followed by an INSERT.
11109**
11110** Changes are not recorded for rows that have NULL values stored in one or
11111** more of their PRIMARY KEY columns. If such a row is inserted or deleted,
11112** no corresponding change is present in the changesets returned by this
11113** function. If an existing row with one or more NULL values stored in
11114** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
11115** only an INSERT is appears in the changeset. Similarly, if an existing row
11116** with non-NULL PRIMARY KEY values is updated so that one or more of its
11117** PRIMARY KEY columns are set to NULL, the resulting changeset contains a
11118** DELETE change only.
11119**
11120** The contents of a changeset may be traversed using an iterator created
11121** using the [sqlite3changeset_start()] API. A changeset may be applied to
11122** a database with a compatible schema using the [sqlite3changeset_apply()]
11123** API.
11124**
11125** Within a changeset generated by this function, all changes related to a
11126** single table are grouped together. In other words, when iterating through
11127** a changeset or when applying a changeset to a database, all changes related
11128** to a single table are processed before moving on to the next table. Tables
11129** are sorted in the same order in which they were attached (or auto-attached)
11130** to the sqlite3_session object. The order in which the changes related to
11131** a single table are stored is undefined.
11132**
11133** Following a successful call to this function, it is the responsibility of
11134** the caller to eventually free the buffer that *ppChangeset points to using
11135** [sqlite3_free()].
11136**
11137** <h3>Changeset Generation</h3>
11138**
11139** Once a table has been attached to a session object, the session object
11140** records the primary key values of all new rows inserted into the table.
11141** It also records the original primary key and other column values of any
11142** deleted or updated rows. For each unique primary key value, data is only
11143** recorded once - the first time a row with said primary key is inserted,
11144** updated or deleted in the lifetime of the session.
11145**
11146** There is one exception to the previous paragraph: when a row is inserted,
11147** updated or deleted, if one or more of its primary key columns contain a
11148** NULL value, no record of the change is made.
11149**
11150** The session object therefore accumulates two types of records - those
11151** that consist of primary key values only (created when the user inserts
11152** a new record) and those that consist of the primary key values and the
11153** original values of other table columns (created when the users deletes
11154** or updates a record).
11155**
11156** When this function is called, the requested changeset is created using
11157** both the accumulated records and the current contents of the database
11158** file. Specifically:
11159**
11160** <ul>
11161** <li> For each record generated by an insert, the database is queried
11162** for a row with a matching primary key. If one is found, an INSERT
11163** change is added to the changeset. If no such row is found, no change
11164** is added to the changeset.
11165**
11166** <li> For each record generated by an update or delete, the database is
11167** queried for a row with a matching primary key. If such a row is
11168** found and one or more of the non-primary key fields have been
11169** modified from their original values, an UPDATE change is added to
11170** the changeset. Or, if no such row is found in the table, a DELETE
11171** change is added to the changeset. If there is a row with a matching
11172** primary key in the database, but all fields contain their original
11173** values, no change is added to the changeset.
11174** </ul>
11175**
11176** This means, amongst other things, that if a row is inserted and then later
11177** deleted while a session object is active, neither the insert nor the delete
11178** will be present in the changeset. Or if a row is deleted and then later a
11179** row with the same primary key values inserted while a session object is
11180** active, the resulting changeset will contain an UPDATE change instead of
11181** a DELETE and an INSERT.
11182**
11183** When a session object is disabled (see the [sqlite3session_enable()] API),
11184** it does not accumulate records when rows are inserted, updated or deleted.
11185** This may appear to have some counter-intuitive effects if a single row
11186** is written to more than once during a session. For example, if a row
11187** is inserted while a session object is enabled, then later deleted while
11188** the same session object is disabled, no INSERT record will appear in the
11189** changeset, even though the delete took place while the session was disabled.
11190** Or, if one field of a row is updated while a session is disabled, and
11191** another field of the same row is updated while the session is enabled, the
11192** resulting changeset will contain an UPDATE change that updates both fields.
11193*/
11194SQLITE_API int sqlite3session_changeset(
11195 sqlite3_session *pSession, /* Session object */
11196 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
11197 void **ppChangeset /* OUT: Buffer containing changeset */
11198);
11199
11200/*
11201** CAPI3REF: Return An Upper-limit For The Size Of The Changeset
11202** METHOD: sqlite3_session
11203**
11204** By default, this function always returns 0. For it to return
11205** a useful result, the sqlite3_session object must have been configured
11206** to enable this API using sqlite3session_object_config() with the
11207** SQLITE_SESSION_OBJCONFIG_SIZE verb.
11208**
11209** When enabled, this function returns an upper limit, in bytes, for the size
11210** of the changeset that might be produced if sqlite3session_changeset() were
11211** called. The final changeset size might be equal to or smaller than the
11212** size in bytes returned by this function.
11213*/
11214SQLITE_API sqlite3_int64 sqlite3session_changeset_size(sqlite3_session *pSession);
11215
11216/*
11217** CAPI3REF: Load The Difference Between Tables Into A Session
11218** METHOD: sqlite3_session
11219**
11220** If it is not already attached to the session object passed as the first
11221** argument, this function attaches table zTbl in the same manner as the
11222** [sqlite3session_attach()] function. If zTbl does not exist, or if it
11223** does not have a primary key, this function is a no-op (but does not return
11224** an error).
11225**
11226** Argument zFromDb must be the name of a database ("main", "temp" etc.)
11227** attached to the same database handle as the session object that contains
11228** a table compatible with the table attached to the session by this function.
11229** A table is considered compatible if it:
11230**
11231** <ul>
11232** <li> Has the same name,
11233** <li> Has the same set of columns declared in the same order, and
11234** <li> Has the same PRIMARY KEY definition.
11235** </ul>
11236**
11237** If the tables are not compatible, SQLITE_SCHEMA is returned. If the tables
11238** are compatible but do not have any PRIMARY KEY columns, it is not an error
11239** but no changes are added to the session object. As with other session
11240** APIs, tables without PRIMARY KEYs are simply ignored.
11241**
11242** This function adds a set of changes to the session object that could be
11243** used to update the table in database zFrom (call this the "from-table")
11244** so that its content is the same as the table attached to the session
11245** object (call this the "to-table"). Specifically:
11246**
11247** <ul>
11248** <li> For each row (primary key) that exists in the to-table but not in
11249** the from-table, an INSERT record is added to the session object.
11250**
11251** <li> For each row (primary key) that exists in the to-table but not in
11252** the from-table, a DELETE record is added to the session object.
11253**
11254** <li> For each row (primary key) that exists in both tables, but features
11255** different non-PK values in each, an UPDATE record is added to the
11256** session.
11257** </ul>
11258**
11259** To clarify, if this function is called and then a changeset constructed
11260** using [sqlite3session_changeset()], then after applying that changeset to
11261** database zFrom the contents of the two compatible tables would be
11262** identical.
11263**
11264** It an error if database zFrom does not exist or does not contain the
11265** required compatible table.
11266**
11267** If the operation is successful, SQLITE_OK is returned. Otherwise, an SQLite
11268** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
11269** may be set to point to a buffer containing an English language error
11270** message. It is the responsibility of the caller to free this buffer using
11271** sqlite3_free().
11272*/
11273SQLITE_API int sqlite3session_diff(
11274 sqlite3_session *pSession,
11275 const char *zFromDb,
11276 const char *zTbl,
11277 char **pzErrMsg
11278);
11279
11280
11281/*
11282** CAPI3REF: Generate A Patchset From A Session Object
11283** METHOD: sqlite3_session
11284**
11285** The differences between a patchset and a changeset are that:
11286**
11287** <ul>
11288** <li> DELETE records consist of the primary key fields only. The
11289** original values of other fields are omitted.
11290** <li> The original values of any modified fields are omitted from
11291** UPDATE records.
11292** </ul>
11293**
11294** A patchset blob may be used with up to date versions of all
11295** sqlite3changeset_xxx API functions except for sqlite3changeset_invert(),
11296** which returns SQLITE_CORRUPT if it is passed a patchset. Similarly,
11297** attempting to use a patchset blob with old versions of the
11298** sqlite3changeset_xxx APIs also provokes an SQLITE_CORRUPT error.
11299**
11300** Because the non-primary key "old.*" fields are omitted, no
11301** SQLITE_CHANGESET_DATA conflicts can be detected or reported if a patchset
11302** is passed to the sqlite3changeset_apply() API. Other conflict types work
11303** in the same way as for changesets.
11304**
11305** Changes within a patchset are ordered in the same way as for changesets
11306** generated by the sqlite3session_changeset() function (i.e. all changes for
11307** a single table are grouped together, tables appear in the order in which
11308** they were attached to the session object).
11309*/
11310SQLITE_API int sqlite3session_patchset(
11311 sqlite3_session *pSession, /* Session object */
11312 int *pnPatchset, /* OUT: Size of buffer at *ppPatchset */
11313 void **ppPatchset /* OUT: Buffer containing patchset */
11314);
11315
11316/*
11317** CAPI3REF: Test if a changeset has recorded any changes.
11318**
11319** Return non-zero if no changes to attached tables have been recorded by
11320** the session object passed as the first argument. Otherwise, if one or
11321** more changes have been recorded, return zero.
11322**
11323** Even if this function returns zero, it is possible that calling
11324** [sqlite3session_changeset()] on the session handle may still return a
11325** changeset that contains no changes. This can happen when a row in
11326** an attached table is modified and then later on the original values
11327** are restored. However, if this function returns non-zero, then it is
11328** guaranteed that a call to sqlite3session_changeset() will return a
11329** changeset containing zero changes.
11330*/
11331SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
11332
11333/*
11334** CAPI3REF: Query for the amount of heap memory used by a session object.
11335**
11336** This API returns the total amount of heap memory in bytes currently
11337** used by the session object passed as the only argument.
11338*/
11339SQLITE_API sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession);
11340
11341/*
11342** CAPI3REF: Create An Iterator To Traverse A Changeset
11343** CONSTRUCTOR: sqlite3_changeset_iter
11344**
11345** Create an iterator used to iterate through the contents of a changeset.
11346** If successful, *pp is set to point to the iterator handle and SQLITE_OK
11347** is returned. Otherwise, if an error occurs, *pp is set to zero and an
11348** SQLite error code is returned.
11349**
11350** The following functions can be used to advance and query a changeset
11351** iterator created by this function:
11352**
11353** <ul>
11354** <li> [sqlite3changeset_next()]
11355** <li> [sqlite3changeset_op()]
11356** <li> [sqlite3changeset_new()]
11357** <li> [sqlite3changeset_old()]
11358** </ul>
11359**
11360** It is the responsibility of the caller to eventually destroy the iterator
11361** by passing it to [sqlite3changeset_finalize()]. The buffer containing the
11362** changeset (pChangeset) must remain valid until after the iterator is
11363** destroyed.
11364**
11365** Assuming the changeset blob was created by one of the
11366** [sqlite3session_changeset()], [sqlite3changeset_concat()] or
11367** [sqlite3changeset_invert()] functions, all changes within the changeset
11368** that apply to a single table are grouped together. This means that when
11369** an application iterates through a changeset using an iterator created by
11370** this function, all changes that relate to a single table are visited
11371** consecutively. There is no chance that the iterator will visit a change
11372** the applies to table X, then one for table Y, and then later on visit
11373** another change for table X.
11374**
11375** The behavior of sqlite3changeset_start_v2() and its streaming equivalent
11376** may be modified by passing a combination of
11377** [SQLITE_CHANGESETSTART_INVERT | supported flags] as the 4th parameter.
11378**
11379** Note that the sqlite3changeset_start_v2() API is still <b>experimental</b>
11380** and therefore subject to change.
11381*/
11382SQLITE_API int sqlite3changeset_start(
11383 sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */
11384 int nChangeset, /* Size of changeset blob in bytes */
11385 void *pChangeset /* Pointer to blob containing changeset */
11386);
11387SQLITE_API int sqlite3changeset_start_v2(
11388 sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */
11389 int nChangeset, /* Size of changeset blob in bytes */
11390 void *pChangeset, /* Pointer to blob containing changeset */
11391 int flags /* SESSION_CHANGESETSTART_* flags */
11392);
11393
11394/*
11395** CAPI3REF: Flags for sqlite3changeset_start_v2
11396**
11397** The following flags may passed via the 4th parameter to
11398** [sqlite3changeset_start_v2] and [sqlite3changeset_start_v2_strm]:
11399**
11400** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
11401** Invert the changeset while iterating through it. This is equivalent to
11402** inverting a changeset using sqlite3changeset_invert() before applying it.
11403** It is an error to specify this flag with a patchset.
11404*/
11405#define SQLITE_CHANGESETSTART_INVERT0x0002 0x0002
11406
11407
11408/*
11409** CAPI3REF: Advance A Changeset Iterator
11410** METHOD: sqlite3_changeset_iter
11411**
11412** This function may only be used with iterators created by the function
11413** [sqlite3changeset_start()]. If it is called on an iterator passed to
11414** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
11415** is returned and the call has no effect.
11416**
11417** Immediately after an iterator is created by sqlite3changeset_start(), it
11418** does not point to any change in the changeset. Assuming the changeset
11419** is not empty, the first call to this function advances the iterator to
11420** point to the first change in the changeset. Each subsequent call advances
11421** the iterator to point to the next change in the changeset (if any). If
11422** no error occurs and the iterator points to a valid change after a call
11423** to sqlite3changeset_next() has advanced it, SQLITE_ROW is returned.
11424** Otherwise, if all changes in the changeset have already been visited,
11425** SQLITE_DONE is returned.
11426**
11427** If an error occurs, an SQLite error code is returned. Possible error
11428** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or
11429** SQLITE_NOMEM.
11430*/
11431SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
11432
11433/*
11434** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
11435** METHOD: sqlite3_changeset_iter
11436**
11437** The pIter argument passed to this function may either be an iterator
11438** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11439** created by [sqlite3changeset_start()]. In the latter case, the most recent
11440** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
11441** is not the case, this function returns [SQLITE_MISUSE].
11442**
11443** Arguments pOp, pnCol and pzTab may not be NULL. Upon return, three
11444** outputs are set through these pointers:
11445**
11446** *pOp is set to one of [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE],
11447** depending on the type of change that the iterator currently points to;
11448**
11449** *pnCol is set to the number of columns in the table affected by the change; and
11450**
11451** *pzTab is set to point to a nul-terminated utf-8 encoded string containing
11452** the name of the table affected by the current change. The buffer remains
11453** valid until either sqlite3changeset_next() is called on the iterator
11454** or until the conflict-handler function returns.
11455**
11456** If pbIndirect is not NULL, then *pbIndirect is set to true (1) if the change
11457** is an indirect change, or false (0) otherwise. See the documentation for
11458** [sqlite3session_indirect()] for a description of direct and indirect
11459** changes.
11460**
11461** If no error occurs, SQLITE_OK is returned. If an error does occur, an
11462** SQLite error code is returned. The values of the output variables may not
11463** be trusted in this case.
11464*/
11465SQLITE_API int sqlite3changeset_op(
11466 sqlite3_changeset_iter *pIter, /* Iterator object */
11467 const char **pzTab, /* OUT: Pointer to table name */
11468 int *pnCol, /* OUT: Number of columns in table */
11469 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
11470 int *pbIndirect /* OUT: True for an 'indirect' change */
11471);
11472
11473/*
11474** CAPI3REF: Obtain The Primary Key Definition Of A Table
11475** METHOD: sqlite3_changeset_iter
11476**
11477** For each modified table, a changeset includes the following:
11478**
11479** <ul>
11480** <li> The number of columns in the table, and
11481** <li> Which of those columns make up the tables PRIMARY KEY.
11482** </ul>
11483**
11484** This function is used to find which columns comprise the PRIMARY KEY of
11485** the table modified by the change that iterator pIter currently points to.
11486** If successful, *pabPK is set to point to an array of nCol entries, where
11487** nCol is the number of columns in the table. Elements of *pabPK are set to
11488** 0x01 if the corresponding column is part of the tables primary key, or
11489** 0x00 if it is not.
11490**
11491** If argument pnCol is not NULL, then *pnCol is set to the number of columns
11492** in the table.
11493**
11494** If this function is called when the iterator does not point to a valid
11495** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
11496** SQLITE_OK is returned and the output variables populated as described
11497** above.
11498*/
11499SQLITE_API int sqlite3changeset_pk(
11500 sqlite3_changeset_iter *pIter, /* Iterator object */
11501 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
11502 int *pnCol /* OUT: Number of entries in output array */
11503);
11504
11505/*
11506** CAPI3REF: Obtain old.* Values From A Changeset Iterator
11507** METHOD: sqlite3_changeset_iter
11508**
11509** The pIter argument passed to this function may either be an iterator
11510** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11511** created by [sqlite3changeset_start()]. In the latter case, the most recent
11512** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
11513** Furthermore, it may only be called if the type of change that the iterator
11514** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
11515** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
11516**
11517** Argument iVal must be greater than or equal to 0, and less than the number
11518** of columns in the table affected by the current change. Otherwise,
11519** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
11520**
11521** If successful, this function sets *ppValue to point to a protected
11522** sqlite3_value object containing the iVal'th value from the vector of
11523** original row values stored as part of the UPDATE or DELETE change and
11524** returns SQLITE_OK. The name of the function comes from the fact that this
11525** is similar to the "old.*" columns available to update or delete triggers.
11526**
11527** If some other error occurs (e.g. an OOM condition), an SQLite error code
11528** is returned and *ppValue is set to NULL.
11529*/
11530SQLITE_API int sqlite3changeset_old(
11531 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11532 int iVal, /* Column number */
11533 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
11534);
11535
11536/*
11537** CAPI3REF: Obtain new.* Values From A Changeset Iterator
11538** METHOD: sqlite3_changeset_iter
11539**
11540** The pIter argument passed to this function may either be an iterator
11541** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11542** created by [sqlite3changeset_start()]. In the latter case, the most recent
11543** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
11544** Furthermore, it may only be called if the type of change that the iterator
11545** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
11546** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
11547**
11548** Argument iVal must be greater than or equal to 0, and less than the number
11549** of columns in the table affected by the current change. Otherwise,
11550** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
11551**
11552** If successful, this function sets *ppValue to point to a protected
11553** sqlite3_value object containing the iVal'th value from the vector of
11554** new row values stored as part of the UPDATE or INSERT change and
11555** returns SQLITE_OK. If the change is an UPDATE and does not include
11556** a new value for the requested column, *ppValue is set to NULL and
11557** SQLITE_OK returned. The name of the function comes from the fact that
11558** this is similar to the "new.*" columns available to update or delete
11559** triggers.
11560**
11561** If some other error occurs (e.g. an OOM condition), an SQLite error code
11562** is returned and *ppValue is set to NULL.
11563*/
11564SQLITE_API int sqlite3changeset_new(
11565 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11566 int iVal, /* Column number */
11567 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
11568);
11569
11570/*
11571** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
11572** METHOD: sqlite3_changeset_iter
11573**
11574** This function should only be used with iterator objects passed to a
11575** conflict-handler callback by [sqlite3changeset_apply()] with either
11576** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
11577** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
11578** is set to NULL.
11579**
11580** Argument iVal must be greater than or equal to 0, and less than the number
11581** of columns in the table affected by the current change. Otherwise,
11582** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
11583**
11584** If successful, this function sets *ppValue to point to a protected
11585** sqlite3_value object containing the iVal'th value from the
11586** "conflicting row" associated with the current conflict-handler callback
11587** and returns SQLITE_OK.
11588**
11589** If some other error occurs (e.g. an OOM condition), an SQLite error code
11590** is returned and *ppValue is set to NULL.
11591*/
11592SQLITE_API int sqlite3changeset_conflict(
11593 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11594 int iVal, /* Column number */
11595 sqlite3_value **ppValue /* OUT: Value from conflicting row */
11596);
11597
11598/*
11599** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
11600** METHOD: sqlite3_changeset_iter
11601**
11602** This function may only be called with an iterator passed to an
11603** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
11604** it sets the output variable to the total number of known foreign key
11605** violations in the destination database and returns SQLITE_OK.
11606**
11607** In all other cases this function returns SQLITE_MISUSE.
11608*/
11609SQLITE_API int sqlite3changeset_fk_conflicts(
11610 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11611 int *pnOut /* OUT: Number of FK violations */
11612);
11613
11614
11615/*
11616** CAPI3REF: Finalize A Changeset Iterator
11617** METHOD: sqlite3_changeset_iter
11618**
11619** This function is used to finalize an iterator allocated with
11620** [sqlite3changeset_start()].
11621**
11622** This function should only be called on iterators created using the
11623** [sqlite3changeset_start()] function. If an application calls this
11624** function with an iterator passed to a conflict-handler by
11625** [sqlite3changeset_apply()], [SQLITE_MISUSE] is immediately returned and the
11626** call has no effect.
11627**
11628** If an error was encountered within a call to an sqlite3changeset_xxx()
11629** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an
11630** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
11631** to that error is returned by this function. Otherwise, SQLITE_OK is
11632** returned. This is to allow the following pattern (pseudo-code):
11633**
11634** <pre>
11635** sqlite3changeset_start();
11636** while( SQLITE_ROW==sqlite3changeset_next() ){
11637** // Do something with change.
11638** }
11639** rc = sqlite3changeset_finalize();
11640** if( rc!=SQLITE_OK ){
11641** // An error has occurred
11642** }
11643** </pre>
11644*/
11645SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
11646
11647/*
11648** CAPI3REF: Invert A Changeset
11649**
11650** This function is used to "invert" a changeset object. Applying an inverted
11651** changeset to a database reverses the effects of applying the uninverted
11652** changeset. Specifically:
11653**
11654** <ul>
11655** <li> Each DELETE change is changed to an INSERT, and
11656** <li> Each INSERT change is changed to a DELETE, and
11657** <li> For each UPDATE change, the old.* and new.* values are exchanged.
11658** </ul>
11659**
11660** This function does not change the order in which changes appear within
11661** the changeset. It merely reverses the sense of each individual change.
11662**
11663** If successful, a pointer to a buffer containing the inverted changeset
11664** is stored in *ppOut, the size of the same buffer is stored in *pnOut, and
11665** SQLITE_OK is returned. If an error occurs, both *pnOut and *ppOut are
11666** zeroed and an SQLite error code returned.
11667**
11668** It is the responsibility of the caller to eventually call sqlite3_free()
11669** on the *ppOut pointer to free the buffer allocation following a successful
11670** call to this function.
11671**
11672** WARNING/TODO: This function currently assumes that the input is a valid
11673** changeset. If it is not, the results are undefined.
11674*/
11675SQLITE_API int sqlite3changeset_invert(
11676 int nIn, const void *pIn, /* Input changeset */
11677 int *pnOut, void **ppOut /* OUT: Inverse of input */
11678);
11679
11680/*
11681** CAPI3REF: Concatenate Two Changeset Objects
11682**
11683** This function is used to concatenate two changesets, A and B, into a
11684** single changeset. The result is a changeset equivalent to applying
11685** changeset A followed by changeset B.
11686**
11687** This function combines the two input changesets using an
11688** sqlite3_changegroup object. Calling it produces similar results as the
11689** following code fragment:
11690**
11691** <pre>
11692** sqlite3_changegroup *pGrp;
11693** rc = sqlite3_changegroup_new(&pGrp);
11694** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
11695** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
11696** if( rc==SQLITE_OK ){
11697** rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
11698** }else{
11699** *ppOut = 0;
11700** *pnOut = 0;
11701** }
11702** </pre>
11703**
11704** Refer to the sqlite3_changegroup documentation below for details.
11705*/
11706SQLITE_API int sqlite3changeset_concat(
11707 int nA, /* Number of bytes in buffer pA */
11708 void *pA, /* Pointer to buffer containing changeset A */
11709 int nB, /* Number of bytes in buffer pB */
11710 void *pB, /* Pointer to buffer containing changeset B */
11711 int *pnOut, /* OUT: Number of bytes in output changeset */
11712 void **ppOut /* OUT: Buffer containing output changeset */
11713);
11714
11715
11716/*
11717** CAPI3REF: Changegroup Handle
11718**
11719** A changegroup is an object used to combine two or more
11720** [changesets] or [patchsets]
11721*/
11722typedef struct sqlite3_changegroup sqlite3_changegroup;
11723
11724/*
11725** CAPI3REF: Create A New Changegroup Object
11726** CONSTRUCTOR: sqlite3_changegroup
11727**
11728** An sqlite3_changegroup object is used to combine two or more changesets
11729** (or patchsets) into a single changeset (or patchset). A single changegroup
11730** object may combine changesets or patchsets, but not both. The output is
11731** always in the same format as the input.
11732**
11733** If successful, this function returns SQLITE_OK and populates (*pp) with
11734** a pointer to a new sqlite3_changegroup object before returning. The caller
11735** should eventually free the returned object using a call to
11736** sqlite3changegroup_delete(). If an error occurs, an SQLite error code
11737** (i.e. SQLITE_NOMEM) is returned and *pp is set to NULL.
11738**
11739** The usual usage pattern for an sqlite3_changegroup object is as follows:
11740**
11741** <ul>
11742** <li> It is created using a call to sqlite3changegroup_new().
11743**
11744** <li> Zero or more changesets (or patchsets) are added to the object
11745** by calling sqlite3changegroup_add().
11746**
11747** <li> The result of combining all input changesets together is obtained
11748** by the application via a call to sqlite3changegroup_output().
11749**
11750** <li> The object is deleted using a call to sqlite3changegroup_delete().
11751** </ul>
11752**
11753** Any number of calls to add() and output() may be made between the calls to
11754** new() and delete(), and in any order.
11755**
11756** As well as the regular sqlite3changegroup_add() and
11757** sqlite3changegroup_output() functions, also available are the streaming
11758** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
11759*/
11760SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
11761
11762/*
11763** CAPI3REF: Add A Changeset To A Changegroup
11764** METHOD: sqlite3_changegroup
11765**
11766** Add all changes within the changeset (or patchset) in buffer pData (size
11767** nData bytes) to the changegroup.
11768**
11769** If the buffer contains a patchset, then all prior calls to this function
11770** on the same changegroup object must also have specified patchsets. Or, if
11771** the buffer contains a changeset, so must have the earlier calls to this
11772** function. Otherwise, SQLITE_ERROR is returned and no changes are added
11773** to the changegroup.
11774**
11775** Rows within the changeset and changegroup are identified by the values in
11776** their PRIMARY KEY columns. A change in the changeset is considered to
11777** apply to the same row as a change already present in the changegroup if
11778** the two rows have the same primary key.
11779**
11780** Changes to rows that do not already appear in the changegroup are
11781** simply copied into it. Or, if both the new changeset and the changegroup
11782** contain changes that apply to a single row, the final contents of the
11783** changegroup depends on the type of each change, as follows:
11784**
11785** <table border=1 style="margin-left:8ex;margin-right:8ex">
11786** <tr><th style="white-space:pre">Existing Change </th>
11787** <th style="white-space:pre">New Change </th>
11788** <th>Output Change
11789** <tr><td>INSERT <td>INSERT <td>
11790** The new change is ignored. This case does not occur if the new
11791** changeset was recorded immediately after the changesets already
11792** added to the changegroup.
11793** <tr><td>INSERT <td>UPDATE <td>
11794** The INSERT change remains in the changegroup. The values in the
11795** INSERT change are modified as if the row was inserted by the
11796** existing change and then updated according to the new change.
11797** <tr><td>INSERT <td>DELETE <td>
11798** The existing INSERT is removed from the changegroup. The DELETE is
11799** not added.
11800** <tr><td>UPDATE <td>INSERT <td>
11801** The new change is ignored. This case does not occur if the new
11802** changeset was recorded immediately after the changesets already
11803** added to the changegroup.
11804** <tr><td>UPDATE <td>UPDATE <td>
11805** The existing UPDATE remains within the changegroup. It is amended
11806** so that the accompanying values are as if the row was updated once
11807** by the existing change and then again by the new change.
11808** <tr><td>UPDATE <td>DELETE <td>
11809** The existing UPDATE is replaced by the new DELETE within the
11810** changegroup.
11811** <tr><td>DELETE <td>INSERT <td>
11812** If one or more of the column values in the row inserted by the
11813** new change differ from those in the row deleted by the existing
11814** change, the existing DELETE is replaced by an UPDATE within the
11815** changegroup. Otherwise, if the inserted row is exactly the same
11816** as the deleted row, the existing DELETE is simply discarded.
11817** <tr><td>DELETE <td>UPDATE <td>
11818** The new change is ignored. This case does not occur if the new
11819** changeset was recorded immediately after the changesets already
11820** added to the changegroup.
11821** <tr><td>DELETE <td>DELETE <td>
11822** The new change is ignored. This case does not occur if the new
11823** changeset was recorded immediately after the changesets already
11824** added to the changegroup.
11825** </table>
11826**
11827** If the new changeset contains changes to a table that is already present
11828** in the changegroup, then the number of columns and the position of the
11829** primary key columns for the table must be consistent. If this is not the
11830** case, this function fails with SQLITE_SCHEMA. If the input changeset
11831** appears to be corrupt and the corruption is detected, SQLITE_CORRUPT is
11832** returned. Or, if an out-of-memory condition occurs during processing, this
11833** function returns SQLITE_NOMEM. In all cases, if an error occurs the state
11834** of the final contents of the changegroup is undefined.
11835**
11836** If no error occurs, SQLITE_OK is returned.
11837*/
11838SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
11839
11840/*
11841** CAPI3REF: Obtain A Composite Changeset From A Changegroup
11842** METHOD: sqlite3_changegroup
11843**
11844** Obtain a buffer containing a changeset (or patchset) representing the
11845** current contents of the changegroup. If the inputs to the changegroup
11846** were themselves changesets, the output is a changeset. Or, if the
11847** inputs were patchsets, the output is also a patchset.
11848**
11849** As with the output of the sqlite3session_changeset() and
11850** sqlite3session_patchset() functions, all changes related to a single
11851** table are grouped together in the output of this function. Tables appear
11852** in the same order as for the very first changeset added to the changegroup.
11853** If the second or subsequent changesets added to the changegroup contain
11854** changes for tables that do not appear in the first changeset, they are
11855** appended onto the end of the output changeset, again in the order in
11856** which they are first encountered.
11857**
11858** If an error occurs, an SQLite error code is returned and the output
11859** variables (*pnData) and (*ppData) are set to 0. Otherwise, SQLITE_OK
11860** is returned and the output variables are set to the size of and a
11861** pointer to the output buffer, respectively. In this case it is the
11862** responsibility of the caller to eventually free the buffer using a
11863** call to sqlite3_free().
11864*/
11865SQLITE_API int sqlite3changegroup_output(
11866 sqlite3_changegroup*,
11867 int *pnData, /* OUT: Size of output buffer in bytes */
11868 void **ppData /* OUT: Pointer to output buffer */
11869);
11870
11871/*
11872** CAPI3REF: Delete A Changegroup Object
11873** DESTRUCTOR: sqlite3_changegroup
11874*/
11875SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
11876
11877/*
11878** CAPI3REF: Apply A Changeset To A Database
11879**
11880** Apply a changeset or patchset to a database. These functions attempt to
11881** update the "main" database attached to handle db with the changes found in
11882** the changeset passed via the second and third arguments.
11883**
11884** The fourth argument (xFilter) passed to these functions is the "filter
11885** callback". If it is not NULL, then for each table affected by at least one
11886** change in the changeset, the filter callback is invoked with
11887** the table name as the second argument, and a copy of the context pointer
11888** passed as the sixth argument as the first. If the "filter callback"
11889** returns zero, then no attempt is made to apply any changes to the table.
11890** Otherwise, if the return value is non-zero or the xFilter argument to
11891** is NULL, all changes related to the table are attempted.
11892**
11893** For each table that is not excluded by the filter callback, this function
11894** tests that the target database contains a compatible table. A table is
11895** considered compatible if all of the following are true:
11896**
11897** <ul>
11898** <li> The table has the same name as the name recorded in the
11899** changeset, and
11900** <li> The table has at least as many columns as recorded in the
11901** changeset, and
11902** <li> The table has primary key columns in the same position as
11903** recorded in the changeset.
11904** </ul>
11905**
11906** If there is no compatible table, it is not an error, but none of the
11907** changes associated with the table are applied. A warning message is issued
11908** via the sqlite3_log() mechanism with the error code SQLITE_SCHEMA. At most
11909** one such warning is issued for each table in the changeset.
11910**
11911** For each change for which there is a compatible table, an attempt is made
11912** to modify the table contents according to the UPDATE, INSERT or DELETE
11913** change. If a change cannot be applied cleanly, the conflict handler
11914** function passed as the fifth argument to sqlite3changeset_apply() may be
11915** invoked. A description of exactly when the conflict handler is invoked for
11916** each type of change is below.
11917**
11918** Unlike the xFilter argument, xConflict may not be passed NULL. The results
11919** of passing anything other than a valid function pointer as the xConflict
11920** argument are undefined.
11921**
11922** Each time the conflict handler function is invoked, it must return one
11923** of [SQLITE_CHANGESET_OMIT], [SQLITE_CHANGESET_ABORT] or
11924** [SQLITE_CHANGESET_REPLACE]. SQLITE_CHANGESET_REPLACE may only be returned
11925** if the second argument passed to the conflict handler is either
11926** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
11927** returns an illegal value, any changes already made are rolled back and
11928** the call to sqlite3changeset_apply() returns SQLITE_MISUSE. Different
11929** actions are taken by sqlite3changeset_apply() depending on the value
11930** returned by each invocation of the conflict-handler function. Refer to
11931** the documentation for the three
11932** [SQLITE_CHANGESET_OMIT|available return values] for details.
11933**
11934** <dl>
11935** <dt>DELETE Changes<dd>
11936** For each DELETE change, the function checks if the target database
11937** contains a row with the same primary key value (or values) as the
11938** original row values stored in the changeset. If it does, and the values
11939** stored in all non-primary key columns also match the values stored in
11940** the changeset the row is deleted from the target database.
11941**
11942** If a row with matching primary key values is found, but one or more of
11943** the non-primary key fields contains a value different from the original
11944** row value stored in the changeset, the conflict-handler function is
11945** invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the
11946** database table has more columns than are recorded in the changeset,
11947** only the values of those non-primary key fields are compared against
11948** the current database contents - any trailing database table columns
11949** are ignored.
11950**
11951** If no row with matching primary key values is found in the database,
11952** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
11953** passed as the second argument.
11954**
11955** If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT
11956** (which can only happen if a foreign key constraint is violated), the
11957** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
11958** passed as the second argument. This includes the case where the DELETE
11959** operation is attempted because an earlier call to the conflict handler
11960** function returned [SQLITE_CHANGESET_REPLACE].
11961**
11962** <dt>INSERT Changes<dd>
11963** For each INSERT change, an attempt is made to insert the new row into
11964** the database. If the changeset row contains fewer fields than the
11965** database table, the trailing fields are populated with their default
11966** values.
11967**
11968** If the attempt to insert the row fails because the database already
11969** contains a row with the same primary key values, the conflict handler
11970** function is invoked with the second argument set to
11971** [SQLITE_CHANGESET_CONFLICT].
11972**
11973** If the attempt to insert the row fails because of some other constraint
11974** violation (e.g. NOT NULL or UNIQUE), the conflict handler function is
11975** invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT].
11976** This includes the case where the INSERT operation is re-attempted because
11977** an earlier call to the conflict handler function returned
11978** [SQLITE_CHANGESET_REPLACE].
11979**
11980** <dt>UPDATE Changes<dd>
11981** For each UPDATE change, the function checks if the target database
11982** contains a row with the same primary key value (or values) as the
11983** original row values stored in the changeset. If it does, and the values
11984** stored in all modified non-primary key columns also match the values
11985** stored in the changeset the row is updated within the target database.
11986**
11987** If a row with matching primary key values is found, but one or more of
11988** the modified non-primary key fields contains a value different from an
11989** original row value stored in the changeset, the conflict-handler function
11990** is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
11991** UPDATE changes only contain values for non-primary key fields that are
11992** to be modified, only those fields need to match the original values to
11993** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
11994**
11995** If no row with matching primary key values is found in the database,
11996** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
11997** passed as the second argument.
11998**
11999** If the UPDATE operation is attempted, but SQLite returns
12000** SQLITE_CONSTRAINT, the conflict-handler function is invoked with
12001** [SQLITE_CHANGESET_CONSTRAINT] passed as the second argument.
12002** This includes the case where the UPDATE operation is attempted after
12003** an earlier call to the conflict handler function returned
12004** [SQLITE_CHANGESET_REPLACE].
12005** </dl>
12006**
12007** It is safe to execute SQL statements, including those that write to the
12008** table that the callback related to, from within the xConflict callback.
12009** This can be used to further customize the application's conflict
12010** resolution strategy.
12011**
12012** All changes made by these functions are enclosed in a savepoint transaction.
12013** If any other error (aside from a constraint failure when attempting to
12014** write to the target database) occurs, then the savepoint transaction is
12015** rolled back, restoring the target database to its original state, and an
12016** SQLite error code returned.
12017**
12018** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
12019** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2()
12020** may set (*ppRebase) to point to a "rebase" that may be used with the
12021** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase)
12022** is set to the size of the buffer in bytes. It is the responsibility of the
12023** caller to eventually free any such buffer using sqlite3_free(). The buffer
12024** is only allocated and populated if one or more conflicts were encountered
12025** while applying the patchset. See comments surrounding the sqlite3_rebaser
12026** APIs for further details.
12027**
12028** The behavior of sqlite3changeset_apply_v2() and its streaming equivalent
12029** may be modified by passing a combination of
12030** [SQLITE_CHANGESETAPPLY_NOSAVEPOINT | supported flags] as the 9th parameter.
12031**
12032** Note that the sqlite3changeset_apply_v2() API is still <b>experimental</b>
12033** and therefore subject to change.
12034*/
12035SQLITE_API int sqlite3changeset_apply(
12036 sqlite3 *db, /* Apply change to "main" db of this handle */
12037 int nChangeset, /* Size of changeset in bytes */
12038 void *pChangeset, /* Changeset blob */
12039 int(*xFilter)(
12040 void *pCtx, /* Copy of sixth arg to _apply() */
12041 const char *zTab /* Table name */
12042 ),
12043 int(*xConflict)(
12044 void *pCtx, /* Copy of sixth arg to _apply() */
12045 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
12046 sqlite3_changeset_iter *p /* Handle describing change and conflict */
12047 ),
12048 void *pCtx /* First argument passed to xConflict */
12049);
12050SQLITE_API int sqlite3changeset_apply_v2(
12051 sqlite3 *db, /* Apply change to "main" db of this handle */
12052 int nChangeset, /* Size of changeset in bytes */
12053 void *pChangeset, /* Changeset blob */
12054 int(*xFilter)(
12055 void *pCtx, /* Copy of sixth arg to _apply() */
12056 const char *zTab /* Table name */
12057 ),
12058 int(*xConflict)(
12059 void *pCtx, /* Copy of sixth arg to _apply() */
12060 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
12061 sqlite3_changeset_iter *p /* Handle describing change and conflict */
12062 ),
12063 void *pCtx, /* First argument passed to xConflict */
12064 void **ppRebase, int *pnRebase, /* OUT: Rebase data */
12065 int flags /* SESSION_CHANGESETAPPLY_* flags */
12066);
12067
12068/*
12069** CAPI3REF: Flags for sqlite3changeset_apply_v2
12070**
12071** The following flags may passed via the 9th parameter to
12072** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]:
12073**
12074** <dl>
12075** <dt>SQLITE_CHANGESETAPPLY_NOSAVEPOINT <dd>
12076** Usually, the sessions module encloses all operations performed by
12077** a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The
12078** SAVEPOINT is committed if the changeset or patchset is successfully
12079** applied, or rolled back if an error occurs. Specifying this flag
12080** causes the sessions module to omit this savepoint. In this case, if the
12081** caller has an open transaction or savepoint when apply_v2() is called,
12082** it may revert the partially applied changeset by rolling it back.
12083**
12084** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
12085** Invert the changeset before applying it. This is equivalent to inverting
12086** a changeset using sqlite3changeset_invert() before applying it. It is
12087** an error to specify this flag with a patchset.
12088*/
12089#define SQLITE_CHANGESETAPPLY_NOSAVEPOINT0x0001 0x0001
12090#define SQLITE_CHANGESETAPPLY_INVERT0x0002 0x0002
12091
12092/*
12093** CAPI3REF: Constants Passed To The Conflict Handler
12094**
12095** Values that may be passed as the second argument to a conflict-handler.
12096**
12097** <dl>
12098** <dt>SQLITE_CHANGESET_DATA<dd>
12099** The conflict handler is invoked with CHANGESET_DATA as the second argument
12100** when processing a DELETE or UPDATE change if a row with the required
12101** PRIMARY KEY fields is present in the database, but one or more other
12102** (non primary-key) fields modified by the update do not contain the
12103** expected "before" values.
12104**
12105** The conflicting row, in this case, is the database row with the matching
12106** primary key.
12107**
12108** <dt>SQLITE_CHANGESET_NOTFOUND<dd>
12109** The conflict handler is invoked with CHANGESET_NOTFOUND as the second
12110** argument when processing a DELETE or UPDATE change if a row with the
12111** required PRIMARY KEY fields is not present in the database.
12112**
12113** There is no conflicting row in this case. The results of invoking the
12114** sqlite3changeset_conflict() API are undefined.
12115**
12116** <dt>SQLITE_CHANGESET_CONFLICT<dd>
12117** CHANGESET_CONFLICT is passed as the second argument to the conflict
12118** handler while processing an INSERT change if the operation would result
12119** in duplicate primary key values.
12120**
12121** The conflicting row in this case is the database row with the matching
12122** primary key.
12123**
12124** <dt>SQLITE_CHANGESET_FOREIGN_KEY<dd>
12125** If foreign key handling is enabled, and applying a changeset leaves the
12126** database in a state containing foreign key violations, the conflict
12127** handler is invoked with CHANGESET_FOREIGN_KEY as the second argument
12128** exactly once before the changeset is committed. If the conflict handler
12129** returns CHANGESET_OMIT, the changes, including those that caused the
12130** foreign key constraint violation, are committed. Or, if it returns
12131** CHANGESET_ABORT, the changeset is rolled back.
12132**
12133** No current or conflicting row information is provided. The only function
12134** it is possible to call on the supplied sqlite3_changeset_iter handle
12135** is sqlite3changeset_fk_conflicts().
12136**
12137** <dt>SQLITE_CHANGESET_CONSTRAINT<dd>
12138** If any other constraint violation occurs while applying a change (i.e.
12139** a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is
12140** invoked with CHANGESET_CONSTRAINT as the second argument.
12141**
12142** There is no conflicting row in this case. The results of invoking the
12143** sqlite3changeset_conflict() API are undefined.
12144**
12145** </dl>
12146*/
12147#define SQLITE_CHANGESET_DATA1 1
12148#define SQLITE_CHANGESET_NOTFOUND2 2
12149#define SQLITE_CHANGESET_CONFLICT3 3
12150#define SQLITE_CHANGESET_CONSTRAINT4 4
12151#define SQLITE_CHANGESET_FOREIGN_KEY5 5
12152
12153/*
12154** CAPI3REF: Constants Returned By The Conflict Handler
12155**
12156** A conflict handler callback must return one of the following three values.
12157**
12158** <dl>
12159** <dt>SQLITE_CHANGESET_OMIT<dd>
12160** If a conflict handler returns this value no special action is taken. The
12161** change that caused the conflict is not applied. The session module
12162** continues to the next change in the changeset.
12163**
12164** <dt>SQLITE_CHANGESET_REPLACE<dd>
12165** This value may only be returned if the second argument to the conflict
12166** handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this
12167** is not the case, any changes applied so far are rolled back and the
12168** call to sqlite3changeset_apply() returns SQLITE_MISUSE.
12169**
12170** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict
12171** handler, then the conflicting row is either updated or deleted, depending
12172** on the type of change.
12173**
12174** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict
12175** handler, then the conflicting row is removed from the database and a
12176** second attempt to apply the change is made. If this second attempt fails,
12177** the original row is restored to the database before continuing.
12178**
12179** <dt>SQLITE_CHANGESET_ABORT<dd>
12180** If this value is returned, any changes applied so far are rolled back
12181** and the call to sqlite3changeset_apply() returns SQLITE_ABORT.
12182** </dl>
12183*/
12184#define SQLITE_CHANGESET_OMIT0 0
12185#define SQLITE_CHANGESET_REPLACE1 1
12186#define SQLITE_CHANGESET_ABORT2 2
12187
12188/*
12189** CAPI3REF: Rebasing changesets
12190** EXPERIMENTAL
12191**
12192** Suppose there is a site hosting a database in state S0. And that
12193** modifications are made that move that database to state S1 and a
12194** changeset recorded (the "local" changeset). Then, a changeset based
12195** on S0 is received from another site (the "remote" changeset) and
12196** applied to the database. The database is then in state
12197** (S1+"remote"), where the exact state depends on any conflict
12198** resolution decisions (OMIT or REPLACE) made while applying "remote".
12199** Rebasing a changeset is to update it to take those conflict
12200** resolution decisions into account, so that the same conflicts
12201** do not have to be resolved elsewhere in the network.
12202**
12203** For example, if both the local and remote changesets contain an
12204** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)":
12205**
12206** local: INSERT INTO t1 VALUES(1, 'v1');
12207** remote: INSERT INTO t1 VALUES(1, 'v2');
12208**
12209** and the conflict resolution is REPLACE, then the INSERT change is
12210** removed from the local changeset (it was overridden). Or, if the
12211** conflict resolution was "OMIT", then the local changeset is modified
12212** to instead contain:
12213**
12214** UPDATE t1 SET b = 'v2' WHERE a=1;
12215**
12216** Changes within the local changeset are rebased as follows:
12217**
12218** <dl>
12219** <dt>Local INSERT<dd>
12220** This may only conflict with a remote INSERT. If the conflict
12221** resolution was OMIT, then add an UPDATE change to the rebased
12222** changeset. Or, if the conflict resolution was REPLACE, add
12223** nothing to the rebased changeset.
12224**
12225** <dt>Local DELETE<dd>
12226** This may conflict with a remote UPDATE or DELETE. In both cases the
12227** only possible resolution is OMIT. If the remote operation was a
12228** DELETE, then add no change to the rebased changeset. If the remote
12229** operation was an UPDATE, then the old.* fields of change are updated
12230** to reflect the new.* values in the UPDATE.
12231**
12232** <dt>Local UPDATE<dd>
12233** This may conflict with a remote UPDATE or DELETE. If it conflicts
12234** with a DELETE, and the conflict resolution was OMIT, then the update
12235** is changed into an INSERT. Any undefined values in the new.* record
12236** from the update change are filled in using the old.* values from
12237** the conflicting DELETE. Or, if the conflict resolution was REPLACE,
12238** the UPDATE change is simply omitted from the rebased changeset.
12239**
12240** If conflict is with a remote UPDATE and the resolution is OMIT, then
12241** the old.* values are rebased using the new.* values in the remote
12242** change. Or, if the resolution is REPLACE, then the change is copied
12243** into the rebased changeset with updates to columns also updated by
12244** the conflicting remote UPDATE removed. If this means no columns would
12245** be updated, the change is omitted.
12246** </dl>
12247**
12248** A local change may be rebased against multiple remote changes
12249** simultaneously. If a single key is modified by multiple remote
12250** changesets, they are combined as follows before the local changeset
12251** is rebased:
12252**
12253** <ul>
12254** <li> If there has been one or more REPLACE resolutions on a
12255** key, it is rebased according to a REPLACE.
12256**
12257** <li> If there have been no REPLACE resolutions on a key, then
12258** the local changeset is rebased according to the most recent
12259** of the OMIT resolutions.
12260** </ul>
12261**
12262** Note that conflict resolutions from multiple remote changesets are
12263** combined on a per-field basis, not per-row. This means that in the
12264** case of multiple remote UPDATE operations, some fields of a single
12265** local change may be rebased for REPLACE while others are rebased for
12266** OMIT.
12267**
12268** In order to rebase a local changeset, the remote changeset must first
12269** be applied to the local database using sqlite3changeset_apply_v2() and
12270** the buffer of rebase information captured. Then:
12271**
12272** <ol>
12273** <li> An sqlite3_rebaser object is created by calling
12274** sqlite3rebaser_create().
12275** <li> The new object is configured with the rebase buffer obtained from
12276** sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure().
12277** If the local changeset is to be rebased against multiple remote
12278** changesets, then sqlite3rebaser_configure() should be called
12279** multiple times, in the same order that the multiple
12280** sqlite3changeset_apply_v2() calls were made.
12281** <li> Each local changeset is rebased by calling sqlite3rebaser_rebase().
12282** <li> The sqlite3_rebaser object is deleted by calling
12283** sqlite3rebaser_delete().
12284** </ol>
12285*/
12286typedef struct sqlite3_rebaser sqlite3_rebaser;
12287
12288/*
12289** CAPI3REF: Create a changeset rebaser object.
12290** EXPERIMENTAL
12291**
12292** Allocate a new changeset rebaser object. If successful, set (*ppNew) to
12293** point to the new object and return SQLITE_OK. Otherwise, if an error
12294** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew)
12295** to NULL.
12296*/
12297SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew);
12298
12299/*
12300** CAPI3REF: Configure a changeset rebaser object.
12301** EXPERIMENTAL
12302**
12303** Configure the changeset rebaser object to rebase changesets according
12304** to the conflict resolutions described by buffer pRebase (size nRebase
12305** bytes), which must have been obtained from a previous call to
12306** sqlite3changeset_apply_v2().
12307*/
12308SQLITE_API int sqlite3rebaser_configure(
12309 sqlite3_rebaser*,
12310 int nRebase, const void *pRebase
12311);
12312
12313/*
12314** CAPI3REF: Rebase a changeset
12315** EXPERIMENTAL
12316**
12317** Argument pIn must point to a buffer containing a changeset nIn bytes
12318** in size. This function allocates and populates a buffer with a copy
12319** of the changeset rebased according to the configuration of the
12320** rebaser object passed as the first argument. If successful, (*ppOut)
12321** is set to point to the new buffer containing the rebased changeset and
12322** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
12323** responsibility of the caller to eventually free the new buffer using
12324** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
12325** are set to zero and an SQLite error code returned.
12326*/
12327SQLITE_API int sqlite3rebaser_rebase(
12328 sqlite3_rebaser*,
12329 int nIn, const void *pIn,
12330 int *pnOut, void **ppOut
12331);
12332
12333/*
12334** CAPI3REF: Delete a changeset rebaser object.
12335** EXPERIMENTAL
12336**
12337** Delete the changeset rebaser object and all associated resources. There
12338** should be one call to this function for each successful invocation
12339** of sqlite3rebaser_create().
12340*/
12341SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p);
12342
12343/*
12344** CAPI3REF: Streaming Versions of API functions.
12345**
12346** The six streaming API xxx_strm() functions serve similar purposes to the
12347** corresponding non-streaming API functions:
12348**
12349** <table border=1 style="margin-left:8ex;margin-right:8ex">
12350** <tr><th>Streaming function<th>Non-streaming equivalent</th>
12351** <tr><td>sqlite3changeset_apply_strm<td>[sqlite3changeset_apply]
12352** <tr><td>sqlite3changeset_apply_strm_v2<td>[sqlite3changeset_apply_v2]
12353** <tr><td>sqlite3changeset_concat_strm<td>[sqlite3changeset_concat]
12354** <tr><td>sqlite3changeset_invert_strm<td>[sqlite3changeset_invert]
12355** <tr><td>sqlite3changeset_start_strm<td>[sqlite3changeset_start]
12356** <tr><td>sqlite3session_changeset_strm<td>[sqlite3session_changeset]
12357** <tr><td>sqlite3session_patchset_strm<td>[sqlite3session_patchset]
12358** </table>
12359**
12360** Non-streaming functions that accept changesets (or patchsets) as input
12361** require that the entire changeset be stored in a single buffer in memory.
12362** Similarly, those that return a changeset or patchset do so by returning
12363** a pointer to a single large buffer allocated using sqlite3_malloc().
12364** Normally this is convenient. However, if an application running in a
12365** low-memory environment is required to handle very large changesets, the
12366** large contiguous memory allocations required can become onerous.
12367**
12368** In order to avoid this problem, instead of a single large buffer, input
12369** is passed to a streaming API functions by way of a callback function that
12370** the sessions module invokes to incrementally request input data as it is
12371** required. In all cases, a pair of API function parameters such as
12372**
12373** <pre>
12374** &nbsp; int nChangeset,
12375** &nbsp; void *pChangeset,
12376** </pre>
12377**
12378** Is replaced by:
12379**
12380** <pre>
12381** &nbsp; int (*xInput)(void *pIn, void *pData, int *pnData),
12382** &nbsp; void *pIn,
12383** </pre>
12384**
12385** Each time the xInput callback is invoked by the sessions module, the first
12386** argument passed is a copy of the supplied pIn context pointer. The second
12387** argument, pData, points to a buffer (*pnData) bytes in size. Assuming no
12388** error occurs the xInput method should copy up to (*pnData) bytes of data
12389** into the buffer and set (*pnData) to the actual number of bytes copied
12390** before returning SQLITE_OK. If the input is completely exhausted, (*pnData)
12391** should be set to zero to indicate this. Or, if an error occurs, an SQLite
12392** error code should be returned. In all cases, if an xInput callback returns
12393** an error, all processing is abandoned and the streaming API function
12394** returns a copy of the error code to the caller.
12395**
12396** In the case of sqlite3changeset_start_strm(), the xInput callback may be
12397** invoked by the sessions module at any point during the lifetime of the
12398** iterator. If such an xInput callback returns an error, the iterator enters
12399** an error state, whereby all subsequent calls to iterator functions
12400** immediately fail with the same error code as returned by xInput.
12401**
12402** Similarly, streaming API functions that return changesets (or patchsets)
12403** return them in chunks by way of a callback function instead of via a
12404** pointer to a single large buffer. In this case, a pair of parameters such
12405** as:
12406**
12407** <pre>
12408** &nbsp; int *pnChangeset,
12409** &nbsp; void **ppChangeset,
12410** </pre>
12411**
12412** Is replaced by:
12413**
12414** <pre>
12415** &nbsp; int (*xOutput)(void *pOut, const void *pData, int nData),
12416** &nbsp; void *pOut
12417** </pre>
12418**
12419** The xOutput callback is invoked zero or more times to return data to
12420** the application. The first parameter passed to each call is a copy of the
12421** pOut pointer supplied by the application. The second parameter, pData,
12422** points to a buffer nData bytes in size containing the chunk of output
12423** data being returned. If the xOutput callback successfully processes the
12424** supplied data, it should return SQLITE_OK to indicate success. Otherwise,
12425** it should return some other SQLite error code. In this case processing
12426** is immediately abandoned and the streaming API function returns a copy
12427** of the xOutput error code to the application.
12428**
12429** The sessions module never invokes an xOutput callback with the third
12430** parameter set to a value less than or equal to zero. Other than this,
12431** no guarantees are made as to the size of the chunks of data returned.
12432*/
12433SQLITE_API int sqlite3changeset_apply_strm(
12434 sqlite3 *db, /* Apply change to "main" db of this handle */
12435 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
12436 void *pIn, /* First arg for xInput */
12437 int(*xFilter)(
12438 void *pCtx, /* Copy of sixth arg to _apply() */
12439 const char *zTab /* Table name */
12440 ),
12441 int(*xConflict)(
12442 void *pCtx, /* Copy of sixth arg to _apply() */
12443 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
12444 sqlite3_changeset_iter *p /* Handle describing change and conflict */
12445 ),
12446 void *pCtx /* First argument passed to xConflict */
12447);
12448SQLITE_API int sqlite3changeset_apply_v2_strm(
12449 sqlite3 *db, /* Apply change to "main" db of this handle */
12450 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
12451 void *pIn, /* First arg for xInput */
12452 int(*xFilter)(
12453 void *pCtx, /* Copy of sixth arg to _apply() */
12454 const char *zTab /* Table name */
12455 ),
12456 int(*xConflict)(
12457 void *pCtx, /* Copy of sixth arg to _apply() */
12458 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
12459 sqlite3_changeset_iter *p /* Handle describing change and conflict */
12460 ),
12461 void *pCtx, /* First argument passed to xConflict */
12462 void **ppRebase, int *pnRebase,
12463 int flags
12464);
12465SQLITE_API int sqlite3changeset_concat_strm(
12466 int (*xInputA)(void *pIn, void *pData, int *pnData),
12467 void *pInA,
12468 int (*xInputB)(void *pIn, void *pData, int *pnData),
12469 void *pInB,
12470 int (*xOutput)(void *pOut, const void *pData, int nData),
12471 void *pOut
12472);
12473SQLITE_API int sqlite3changeset_invert_strm(
12474 int (*xInput)(void *pIn, void *pData, int *pnData),
12475 void *pIn,
12476 int (*xOutput)(void *pOut, const void *pData, int nData),
12477 void *pOut
12478);
12479SQLITE_API int sqlite3changeset_start_strm(
12480 sqlite3_changeset_iter **pp,
12481 int (*xInput)(void *pIn, void *pData, int *pnData),
12482 void *pIn
12483);
12484SQLITE_API int sqlite3changeset_start_v2_strm(
12485 sqlite3_changeset_iter **pp,
12486 int (*xInput)(void *pIn, void *pData, int *pnData),
12487 void *pIn,
12488 int flags
12489);
12490SQLITE_API int sqlite3session_changeset_strm(
12491 sqlite3_session *pSession,
12492 int (*xOutput)(void *pOut, const void *pData, int nData),
12493 void *pOut
12494);
12495SQLITE_API int sqlite3session_patchset_strm(
12496 sqlite3_session *pSession,
12497 int (*xOutput)(void *pOut, const void *pData, int nData),
12498 void *pOut
12499);
12500SQLITE_API int sqlite3changegroup_add_strm(sqlite3_changegroup*,
12501 int (*xInput)(void *pIn, void *pData, int *pnData),
12502 void *pIn
12503);
12504SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
12505 int (*xOutput)(void *pOut, const void *pData, int nData),
12506 void *pOut
12507);
12508SQLITE_API int sqlite3rebaser_rebase_strm(
12509 sqlite3_rebaser *pRebaser,
12510 int (*xInput)(void *pIn, void *pData, int *pnData),
12511 void *pIn,
12512 int (*xOutput)(void *pOut, const void *pData, int nData),
12513 void *pOut
12514);
12515
12516/*
12517** CAPI3REF: Configure global parameters
12518**
12519** The sqlite3session_config() interface is used to make global configuration
12520** changes to the sessions module in order to tune it to the specific needs
12521** of the application.
12522**
12523** The sqlite3session_config() interface is not threadsafe. If it is invoked
12524** while any other thread is inside any other sessions method then the
12525** results are undefined. Furthermore, if it is invoked after any sessions
12526** related objects have been created, the results are also undefined.
12527**
12528** The first argument to the sqlite3session_config() function must be one
12529** of the SQLITE_SESSION_CONFIG_XXX constants defined below. The
12530** interpretation of the (void*) value passed as the second parameter and
12531** the effect of calling this function depends on the value of the first
12532** parameter.
12533**
12534** <dl>
12535** <dt>SQLITE_SESSION_CONFIG_STRMSIZE<dd>
12536** By default, the sessions module streaming interfaces attempt to input
12537** and output data in approximately 1 KiB chunks. This operand may be used
12538** to set and query the value of this configuration setting. The pointer
12539** passed as the second argument must point to a value of type (int).
12540** If this value is greater than 0, it is used as the new streaming data
12541** chunk size for both input and output. Before returning, the (int) value
12542** pointed to by pArg is set to the final value of the streaming interface
12543** chunk size.
12544** </dl>
12545**
12546** This function returns SQLITE_OK if successful, or an SQLite error code
12547** otherwise.
12548*/
12549SQLITE_API int sqlite3session_config(int op, void *pArg);
12550
12551/*
12552** CAPI3REF: Values for sqlite3session_config().
12553*/
12554#define SQLITE_SESSION_CONFIG_STRMSIZE1 1
12555
12556/*
12557** Make sure we can call this stuff from C++.
12558*/
12559#if 0
12560}
12561#endif
12562
12563#endif /* !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) */
12564
12565/******** End of sqlite3session.h *********/
12566/******** Begin file fts5.h *********/
12567/*
12568** 2014 May 31
12569**
12570** The author disclaims copyright to this source code. In place of
12571** a legal notice, here is a blessing:
12572**
12573** May you do good and not evil.
12574** May you find forgiveness for yourself and forgive others.
12575** May you share freely, never taking more than you give.
12576**
12577******************************************************************************
12578**
12579** Interfaces to extend FTS5. Using the interfaces defined in this file,
12580** FTS5 may be extended with:
12581**
12582** * custom tokenizers, and
12583** * custom auxiliary functions.
12584*/
12585
12586
12587#ifndef _FTS5_H
12588#define _FTS5_H
12589
12590
12591#if 0
12592extern "C" {
12593#endif
12594
12595/*************************************************************************
12596** CUSTOM AUXILIARY FUNCTIONS
12597**
12598** Virtual table implementations may overload SQL functions by implementing
12599** the sqlite3_module.xFindFunction() method.
12600*/
12601
12602typedef struct Fts5ExtensionApi Fts5ExtensionApi;
12603typedef struct Fts5Context Fts5Context;
12604typedef struct Fts5PhraseIter Fts5PhraseIter;
12605
12606typedef void (*fts5_extension_function)(
12607 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
12608 Fts5Context *pFts, /* First arg to pass to pApi functions */
12609 sqlite3_context *pCtx, /* Context for returning result/error */
12610 int nVal, /* Number of values in apVal[] array */
12611 sqlite3_value **apVal /* Array of trailing arguments */
12612);
12613
12614struct Fts5PhraseIter {
12615 const unsigned char *a;
12616 const unsigned char *b;
12617};
12618
12619/*
12620** EXTENSION API FUNCTIONS
12621**
12622** xUserData(pFts):
12623** Return a copy of the context pointer the extension function was
12624** registered with.
12625**
12626** xColumnTotalSize(pFts, iCol, pnToken):
12627** If parameter iCol is less than zero, set output variable *pnToken
12628** to the total number of tokens in the FTS5 table. Or, if iCol is
12629** non-negative but less than the number of columns in the table, return
12630** the total number of tokens in column iCol, considering all rows in
12631** the FTS5 table.
12632**
12633** If parameter iCol is greater than or equal to the number of columns
12634** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
12635** an OOM condition or IO error), an appropriate SQLite error code is
12636** returned.
12637**
12638** xColumnCount(pFts):
12639** Return the number of columns in the table.
12640**
12641** xColumnSize(pFts, iCol, pnToken):
12642** If parameter iCol is less than zero, set output variable *pnToken
12643** to the total number of tokens in the current row. Or, if iCol is
12644** non-negative but less than the number of columns in the table, set
12645** *pnToken to the number of tokens in column iCol of the current row.
12646**
12647** If parameter iCol is greater than or equal to the number of columns
12648** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
12649** an OOM condition or IO error), an appropriate SQLite error code is
12650** returned.
12651**
12652** This function may be quite inefficient if used with an FTS5 table
12653** created with the "columnsize=0" option.
12654**
12655** xColumnText:
12656** This function attempts to retrieve the text of column iCol of the
12657** current document. If successful, (*pz) is set to point to a buffer
12658** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
12659** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
12660** if an error occurs, an SQLite error code is returned and the final values
12661** of (*pz) and (*pn) are undefined.
12662**
12663** xPhraseCount:
12664** Returns the number of phrases in the current query expression.
12665**
12666** xPhraseSize:
12667** Returns the number of tokens in phrase iPhrase of the query. Phrases
12668** are numbered starting from zero.
12669**
12670** xInstCount:
12671** Set *pnInst to the total number of occurrences of all phrases within
12672** the query within the current row. Return SQLITE_OK if successful, or
12673** an error code (i.e. SQLITE_NOMEM) if an error occurs.
12674**
12675** This API can be quite slow if used with an FTS5 table created with the
12676** "detail=none" or "detail=column" option. If the FTS5 table is created
12677** with either "detail=none" or "detail=column" and "content=" option
12678** (i.e. if it is a contentless table), then this API always returns 0.
12679**
12680** xInst:
12681** Query for the details of phrase match iIdx within the current row.
12682** Phrase matches are numbered starting from zero, so the iIdx argument
12683** should be greater than or equal to zero and smaller than the value
12684** output by xInstCount().
12685**
12686** Usually, output parameter *piPhrase is set to the phrase number, *piCol
12687** to the column in which it occurs and *piOff the token offset of the
12688** first token of the phrase. Returns SQLITE_OK if successful, or an error
12689** code (i.e. SQLITE_NOMEM) if an error occurs.
12690**
12691** This API can be quite slow if used with an FTS5 table created with the
12692** "detail=none" or "detail=column" option.
12693**
12694** xRowid:
12695** Returns the rowid of the current row.
12696**
12697** xTokenize:
12698** Tokenize text using the tokenizer belonging to the FTS5 table.
12699**
12700** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
12701** This API function is used to query the FTS table for phrase iPhrase
12702** of the current query. Specifically, a query equivalent to:
12703**
12704** ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
12705**
12706** with $p set to a phrase equivalent to the phrase iPhrase of the
12707** current query is executed. Any column filter that applies to
12708** phrase iPhrase of the current query is included in $p. For each
12709** row visited, the callback function passed as the fourth argument
12710** is invoked. The context and API objects passed to the callback
12711** function may be used to access the properties of each matched row.
12712** Invoking Api.xUserData() returns a copy of the pointer passed as
12713** the third argument to pUserData.
12714**
12715** If the callback function returns any value other than SQLITE_OK, the
12716** query is abandoned and the xQueryPhrase function returns immediately.
12717** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
12718** Otherwise, the error code is propagated upwards.
12719**
12720** If the query runs to completion without incident, SQLITE_OK is returned.
12721** Or, if some error occurs before the query completes or is aborted by
12722** the callback, an SQLite error code is returned.
12723**
12724**
12725** xSetAuxdata(pFts5, pAux, xDelete)
12726**
12727** Save the pointer passed as the second argument as the extension function's
12728** "auxiliary data". The pointer may then be retrieved by the current or any
12729** future invocation of the same fts5 extension function made as part of
12730** the same MATCH query using the xGetAuxdata() API.
12731**
12732** Each extension function is allocated a single auxiliary data slot for
12733** each FTS query (MATCH expression). If the extension function is invoked
12734** more than once for a single FTS query, then all invocations share a
12735** single auxiliary data context.
12736**
12737** If there is already an auxiliary data pointer when this function is
12738** invoked, then it is replaced by the new pointer. If an xDelete callback
12739** was specified along with the original pointer, it is invoked at this
12740** point.
12741**
12742** The xDelete callback, if one is specified, is also invoked on the
12743** auxiliary data pointer after the FTS5 query has finished.
12744**
12745** If an error (e.g. an OOM condition) occurs within this function,
12746** the auxiliary data is set to NULL and an error code returned. If the
12747** xDelete parameter was not NULL, it is invoked on the auxiliary data
12748** pointer before returning.
12749**
12750**
12751** xGetAuxdata(pFts5, bClear)
12752**
12753** Returns the current auxiliary data pointer for the fts5 extension
12754** function. See the xSetAuxdata() method for details.
12755**
12756** If the bClear argument is non-zero, then the auxiliary data is cleared
12757** (set to NULL) before this function returns. In this case the xDelete,
12758** if any, is not invoked.
12759**
12760**
12761** xRowCount(pFts5, pnRow)
12762**
12763** This function is used to retrieve the total number of rows in the table.
12764** In other words, the same value that would be returned by:
12765**
12766** SELECT count(*) FROM ftstable;
12767**
12768** xPhraseFirst()
12769** This function is used, along with type Fts5PhraseIter and the xPhraseNext
12770** method, to iterate through all instances of a single query phrase within
12771** the current row. This is the same information as is accessible via the
12772** xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient
12773** to use, this API may be faster under some circumstances. To iterate
12774** through instances of phrase iPhrase, use the following code:
12775**
12776** Fts5PhraseIter iter;
12777** int iCol, iOff;
12778** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
12779** iCol>=0;
12780** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
12781** ){
12782** // An instance of phrase iPhrase at offset iOff of column iCol
12783** }
12784**
12785** The Fts5PhraseIter structure is defined above. Applications should not
12786** modify this structure directly - it should only be used as shown above
12787** with the xPhraseFirst() and xPhraseNext() API methods (and by
12788** xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below).
12789**
12790** This API can be quite slow if used with an FTS5 table created with the
12791** "detail=none" or "detail=column" option. If the FTS5 table is created
12792** with either "detail=none" or "detail=column" and "content=" option
12793** (i.e. if it is a contentless table), then this API always iterates
12794** through an empty set (all calls to xPhraseFirst() set iCol to -1).
12795**
12796** xPhraseNext()
12797** See xPhraseFirst above.
12798**
12799** xPhraseFirstColumn()
12800** This function and xPhraseNextColumn() are similar to the xPhraseFirst()
12801** and xPhraseNext() APIs described above. The difference is that instead
12802** of iterating through all instances of a phrase in the current row, these
12803** APIs are used to iterate through the set of columns in the current row
12804** that contain one or more instances of a specified phrase. For example:
12805**
12806** Fts5PhraseIter iter;
12807** int iCol;
12808** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
12809** iCol>=0;
12810** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
12811** ){
12812** // Column iCol contains at least one instance of phrase iPhrase
12813** }
12814**
12815** This API can be quite slow if used with an FTS5 table created with the
12816** "detail=none" option. If the FTS5 table is created with either
12817** "detail=none" "content=" option (i.e. if it is a contentless table),
12818** then this API always iterates through an empty set (all calls to
12819** xPhraseFirstColumn() set iCol to -1).
12820**
12821** The information accessed using this API and its companion
12822** xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext
12823** (or xInst/xInstCount). The chief advantage of this API is that it is
12824** significantly more efficient than those alternatives when used with
12825** "detail=column" tables.
12826**
12827** xPhraseNextColumn()
12828** See xPhraseFirstColumn above.
12829*/
12830struct Fts5ExtensionApi {
12831 int iVersion; /* Currently always set to 3 */
12832
12833 void *(*xUserData)(Fts5Context*);
12834
12835 int (*xColumnCount)(Fts5Context*);
12836 int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
12837 int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
12838
12839 int (*xTokenize)(Fts5Context*,
12840 const char *pText, int nText, /* Text to tokenize */
12841 void *pCtx, /* Context passed to xToken() */
12842 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
12843 );
12844
12845 int (*xPhraseCount)(Fts5Context*);
12846 int (*xPhraseSize)(Fts5Context*, int iPhrase);
12847
12848 int (*xInstCount)(Fts5Context*, int *pnInst);
12849 int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
12850
12851 sqlite3_int64 (*xRowid)(Fts5Context*);
12852 int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
12853 int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
12854
12855 int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
12856 int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
12857 );
12858 int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
12859 void *(*xGetAuxdata)(Fts5Context*, int bClear);
12860
12861 int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
12862 void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
12863
12864 int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
12865 void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
12866};
12867
12868/*
12869** CUSTOM AUXILIARY FUNCTIONS
12870*************************************************************************/
12871
12872/*************************************************************************
12873** CUSTOM TOKENIZERS
12874**
12875** Applications may also register custom tokenizer types. A tokenizer
12876** is registered by providing fts5 with a populated instance of the
12877** following structure. All structure methods must be defined, setting
12878** any member of the fts5_tokenizer struct to NULL leads to undefined
12879** behaviour. The structure methods are expected to function as follows:
12880**
12881** xCreate:
12882** This function is used to allocate and initialize a tokenizer instance.
12883** A tokenizer instance is required to actually tokenize text.
12884**
12885** The first argument passed to this function is a copy of the (void*)
12886** pointer provided by the application when the fts5_tokenizer object
12887** was registered with FTS5 (the third argument to xCreateTokenizer()).
12888** The second and third arguments are an array of nul-terminated strings
12889** containing the tokenizer arguments, if any, specified following the
12890** tokenizer name as part of the CREATE VIRTUAL TABLE statement used
12891** to create the FTS5 table.
12892**
12893** The final argument is an output variable. If successful, (*ppOut)
12894** should be set to point to the new tokenizer handle and SQLITE_OK
12895** returned. If an error occurs, some value other than SQLITE_OK should
12896** be returned. In this case, fts5 assumes that the final value of *ppOut
12897** is undefined.
12898**
12899** xDelete:
12900** This function is invoked to delete a tokenizer handle previously
12901** allocated using xCreate(). Fts5 guarantees that this function will
12902** be invoked exactly once for each successful call to xCreate().
12903**
12904** xTokenize:
12905** This function is expected to tokenize the nText byte string indicated
12906** by argument pText. pText may or may not be nul-terminated. The first
12907** argument passed to this function is a pointer to an Fts5Tokenizer object
12908** returned by an earlier call to xCreate().
12909**
12910** The second argument indicates the reason that FTS5 is requesting
12911** tokenization of the supplied text. This is always one of the following
12912** four values:
12913**
12914** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
12915** or removed from the FTS table. The tokenizer is being invoked to
12916** determine the set of tokens to add to (or delete from) the
12917** FTS index.
12918**
12919** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
12920** against the FTS index. The tokenizer is being called to tokenize
12921** a bareword or quoted string specified as part of the query.
12922**
12923** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
12924** FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is
12925** followed by a "*" character, indicating that the last token
12926** returned by the tokenizer will be treated as a token prefix.
12927**
12928** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
12929** satisfy an fts5_api.xTokenize() request made by an auxiliary
12930** function. Or an fts5_api.xColumnSize() request made by the same
12931** on a columnsize=0 database.
12932** </ul>
12933**
12934** For each token in the input string, the supplied callback xToken() must
12935** be invoked. The first argument to it should be a copy of the pointer
12936** passed as the second argument to xTokenize(). The third and fourth
12937** arguments are a pointer to a buffer containing the token text, and the
12938** size of the token in bytes. The 4th and 5th arguments are the byte offsets
12939** of the first byte of and first byte immediately following the text from
12940** which the token is derived within the input.
12941**
12942** The second argument passed to the xToken() callback ("tflags") should
12943** normally be set to 0. The exception is if the tokenizer supports
12944** synonyms. In this case see the discussion below for details.
12945**
12946** FTS5 assumes the xToken() callback is invoked for each token in the
12947** order that they occur within the input text.
12948**
12949** If an xToken() callback returns any value other than SQLITE_OK, then
12950** the tokenization should be abandoned and the xTokenize() method should
12951** immediately return a copy of the xToken() return value. Or, if the
12952** input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally,
12953** if an error occurs with the xTokenize() implementation itself, it
12954** may abandon the tokenization and return any error code other than
12955** SQLITE_OK or SQLITE_DONE.
12956**
12957** SYNONYM SUPPORT
12958**
12959** Custom tokenizers may also support synonyms. Consider a case in which a
12960** user wishes to query for a phrase such as "first place". Using the
12961** built-in tokenizers, the FTS5 query 'first + place' will match instances
12962** of "first place" within the document set, but not alternative forms
12963** such as "1st place". In some applications, it would be better to match
12964** all instances of "first place" or "1st place" regardless of which form
12965** the user specified in the MATCH query text.
12966**
12967** There are several ways to approach this in FTS5:
12968**
12969** <ol><li> By mapping all synonyms to a single token. In this case, using
12970** the above example, this means that the tokenizer returns the
12971** same token for inputs "first" and "1st". Say that token is in
12972** fact "first", so that when the user inserts the document "I won
12973** 1st place" entries are added to the index for tokens "i", "won",
12974** "first" and "place". If the user then queries for '1st + place',
12975** the tokenizer substitutes "first" for "1st" and the query works
12976** as expected.
12977**
12978** <li> By querying the index for all synonyms of each query term
12979** separately. In this case, when tokenizing query text, the
12980** tokenizer may provide multiple synonyms for a single term
12981** within the document. FTS5 then queries the index for each
12982** synonym individually. For example, faced with the query:
12983**
12984** <codeblock>
12985** ... MATCH 'first place'</codeblock>
12986**
12987** the tokenizer offers both "1st" and "first" as synonyms for the
12988** first token in the MATCH query and FTS5 effectively runs a query
12989** similar to:
12990**
12991** <codeblock>
12992** ... MATCH '(first OR 1st) place'</codeblock>
12993**
12994** except that, for the purposes of auxiliary functions, the query
12995** still appears to contain just two phrases - "(first OR 1st)"
12996** being treated as a single phrase.
12997**
12998** <li> By adding multiple synonyms for a single term to the FTS index.
12999** Using this method, when tokenizing document text, the tokenizer
13000** provides multiple synonyms for each token. So that when a
13001** document such as "I won first place" is tokenized, entries are
13002** added to the FTS index for "i", "won", "first", "1st" and
13003** "place".
13004**
13005** This way, even if the tokenizer does not provide synonyms
13006** when tokenizing query text (it should not - to do so would be
13007** inefficient), it doesn't matter if the user queries for
13008** 'first + place' or '1st + place', as there are entries in the
13009** FTS index corresponding to both forms of the first token.
13010** </ol>
13011**
13012** Whether it is parsing document or query text, any call to xToken that
13013** specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
13014** is considered to supply a synonym for the previous token. For example,
13015** when parsing the document "I won first place", a tokenizer that supports
13016** synonyms would call xToken() 5 times, as follows:
13017**
13018** <codeblock>
13019** xToken(pCtx, 0, "i", 1, 0, 1);
13020** xToken(pCtx, 0, "won", 3, 2, 5);
13021** xToken(pCtx, 0, "first", 5, 6, 11);
13022** xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3, 6, 11);
13023** xToken(pCtx, 0, "place", 5, 12, 17);
13024**</codeblock>
13025**
13026** It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time
13027** xToken() is called. Multiple synonyms may be specified for a single token
13028** by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence.
13029** There is no limit to the number of synonyms that may be provided for a
13030** single token.
13031**
13032** In many cases, method (1) above is the best approach. It does not add
13033** extra data to the FTS index or require FTS5 to query for multiple terms,
13034** so it is efficient in terms of disk space and query speed. However, it
13035** does not support prefix queries very well. If, as suggested above, the
13036** token "first" is substituted for "1st" by the tokenizer, then the query:
13037**
13038** <codeblock>
13039** ... MATCH '1s*'</codeblock>
13040**
13041** will not match documents that contain the token "1st" (as the tokenizer
13042** will probably not map "1s" to any prefix of "first").
13043**
13044** For full prefix support, method (3) may be preferred. In this case,
13045** because the index contains entries for both "first" and "1st", prefix
13046** queries such as 'fi*' or '1s*' will match correctly. However, because
13047** extra entries are added to the FTS index, this method uses more space
13048** within the database.
13049**
13050** Method (2) offers a midpoint between (1) and (3). Using this method,
13051** a query such as '1s*' will match documents that contain the literal
13052** token "1st", but not "first" (assuming the tokenizer is not able to
13053** provide synonyms for prefixes). However, a non-prefix query like '1st'
13054** will match against "1st" and "first". This method does not require
13055** extra disk space, as no extra entries are added to the FTS index.
13056** On the other hand, it may require more CPU cycles to run MATCH queries,
13057** as separate queries of the FTS index are required for each synonym.
13058**
13059** When using methods (2) or (3), it is important that the tokenizer only
13060** provide synonyms when tokenizing document text (method (2)) or query
13061** text (method (3)), not both. Doing so will not cause any errors, but is
13062** inefficient.
13063*/
13064typedef struct Fts5Tokenizer Fts5Tokenizer;
13065typedef struct fts5_tokenizer fts5_tokenizer;
13066struct fts5_tokenizer {
13067 int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
13068 void (*xDelete)(Fts5Tokenizer*);
13069 int (*xTokenize)(Fts5Tokenizer*,
13070 void *pCtx,
13071 int flags, /* Mask of FTS5_TOKENIZE_* flags */
13072 const char *pText, int nText,
13073 int (*xToken)(
13074 void *pCtx, /* Copy of 2nd argument to xTokenize() */
13075 int tflags, /* Mask of FTS5_TOKEN_* flags */
13076 const char *pToken, /* Pointer to buffer containing token */
13077 int nToken, /* Size of token in bytes */
13078 int iStart, /* Byte offset of token within input text */
13079 int iEnd /* Byte offset of end of token within input text */
13080 )
13081 );
13082};
13083
13084/* Flags that may be passed as the third argument to xTokenize() */
13085#define FTS5_TOKENIZE_QUERY0x0001 0x0001
13086#define FTS5_TOKENIZE_PREFIX0x0002 0x0002
13087#define FTS5_TOKENIZE_DOCUMENT0x0004 0x0004
13088#define FTS5_TOKENIZE_AUX0x0008 0x0008
13089
13090/* Flags that may be passed by the tokenizer implementation back to FTS5
13091** as the third argument to the supplied xToken callback. */
13092#define FTS5_TOKEN_COLOCATED0x0001 0x0001 /* Same position as prev. token */
13093
13094/*
13095** END OF CUSTOM TOKENIZERS
13096*************************************************************************/
13097
13098/*************************************************************************
13099** FTS5 EXTENSION REGISTRATION API
13100*/
13101typedef struct fts5_api fts5_api;
13102struct fts5_api {
13103 int iVersion; /* Currently always set to 2 */
13104
13105 /* Create a new tokenizer */
13106 int (*xCreateTokenizer)(
13107 fts5_api *pApi,
13108 const char *zName,
13109 void *pContext,
13110 fts5_tokenizer *pTokenizer,
13111 void (*xDestroy)(void*)
13112 );
13113
13114 /* Find an existing tokenizer */
13115 int (*xFindTokenizer)(
13116 fts5_api *pApi,
13117 const char *zName,
13118 void **ppContext,
13119 fts5_tokenizer *pTokenizer
13120 );
13121
13122 /* Create a new auxiliary function */
13123 int (*xCreateFunction)(
13124 fts5_api *pApi,
13125 const char *zName,
13126 void *pContext,
13127 fts5_extension_function xFunction,
13128 void (*xDestroy)(void*)
13129 );
13130};
13131
13132/*
13133** END OF REGISTRATION API
13134*************************************************************************/
13135
13136#if 0
13137} /* end of the 'extern "C"' block */
13138#endif
13139
13140#endif /* _FTS5_H */
13141
13142/******** End of fts5.h *********/
13143
13144/************** End of sqlite3.h *********************************************/
13145/************** Continuing where we left off in sqliteInt.h ******************/
13146
13147/*
13148** Include the configuration header output by 'configure' if we're using the
13149** autoconf-based build
13150*/
13151#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
13152#include "config.h"
13153#define SQLITECONFIG_H 1
13154#endif
13155
13156/************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
13157/************** Begin file sqliteLimit.h *************************************/
13158/*
13159** 2007 May 7
13160**
13161** The author disclaims copyright to this source code. In place of
13162** a legal notice, here is a blessing:
13163**
13164** May you do good and not evil.
13165** May you find forgiveness for yourself and forgive others.
13166** May you share freely, never taking more than you give.
13167**
13168*************************************************************************
13169**
13170** This file defines various limits of what SQLite can process.
13171*/
13172
13173/*
13174** The maximum length of a TEXT or BLOB in bytes. This also
13175** limits the size of a row in a table or index.
13176**
13177** The hard limit is the ability of a 32-bit signed integer
13178** to count the size: 2^31-1 or 2147483647.
13179*/
13180#ifndef SQLITE_MAX_LENGTH2147483645
13181# define SQLITE_MAX_LENGTH2147483645 1000000000
13182#endif
13183
13184/*
13185** This is the maximum number of
13186**
13187** * Columns in a table
13188** * Columns in an index
13189** * Columns in a view
13190** * Terms in the SET clause of an UPDATE statement
13191** * Terms in the result set of a SELECT statement
13192** * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
13193** * Terms in the VALUES clause of an INSERT statement
13194**
13195** The hard upper limit here is 32676. Most database people will
13196** tell you that in a well-normalized database, you usually should
13197** not have more than a dozen or so columns in any table. And if
13198** that is the case, there is no point in having more than a few
13199** dozen values in any of the other situations described above.
13200*/
13201#ifndef SQLITE_MAX_COLUMN2000
13202# define SQLITE_MAX_COLUMN2000 2000
13203#endif
13204
13205/*
13206** The maximum length of a single SQL statement in bytes.
13207**
13208** It used to be the case that setting this value to zero would
13209** turn the limit off. That is no longer true. It is not possible
13210** to turn this limit off.
13211*/
13212#ifndef SQLITE_MAX_SQL_LENGTH1000000000
13213# define SQLITE_MAX_SQL_LENGTH1000000000 1000000000
13214#endif
13215
13216/*
13217** The maximum depth of an expression tree. This is limited to
13218** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might
13219** want to place more severe limits on the complexity of an
13220** expression. A value of 0 means that there is no limit.
13221*/
13222#ifndef SQLITE_MAX_EXPR_DEPTH1000
13223# define SQLITE_MAX_EXPR_DEPTH1000 1000
13224#endif
13225
13226/*
13227** The maximum number of terms in a compound SELECT statement.
13228** The code generator for compound SELECT statements does one
13229** level of recursion for each term. A stack overflow can result
13230** if the number of terms is too large. In practice, most SQL
13231** never has more than 3 or 4 terms. Use a value of 0 to disable
13232** any limit on the number of terms in a compount SELECT.
13233*/
13234#ifndef SQLITE_MAX_COMPOUND_SELECT500
13235# define SQLITE_MAX_COMPOUND_SELECT500 500
13236#endif
13237
13238/*
13239** The maximum number of opcodes in a VDBE program.
13240** Not currently enforced.
13241*/
13242#ifndef SQLITE_MAX_VDBE_OP250000000
13243# define SQLITE_MAX_VDBE_OP250000000 250000000
13244#endif
13245
13246/*
13247** The maximum number of arguments to an SQL function.
13248*/
13249#ifndef SQLITE_MAX_FUNCTION_ARG127
13250# define SQLITE_MAX_FUNCTION_ARG127 127
13251#endif
13252
13253/*
13254** The suggested maximum number of in-memory pages to use for
13255** the main database table and for temporary tables.
13256**
13257** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000,
13258** which means the cache size is limited to 2048000 bytes of memory.
13259** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be
13260** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options.
13261*/
13262#ifndef SQLITE_DEFAULT_CACHE_SIZE128
13263# define SQLITE_DEFAULT_CACHE_SIZE128 -2000
13264#endif
13265
13266/*
13267** The default number of frames to accumulate in the log file before
13268** checkpointing the database in WAL mode.
13269*/
13270#ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT1000
13271# define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT1000 1000
13272#endif
13273
13274/*
13275** The maximum number of attached databases. This must be between 0
13276** and 125. The upper bound of 125 is because the attached databases are
13277** counted using a signed 8-bit integer which has a maximum value of 127
13278** and we have to allow 2 extra counts for the "main" and "temp" databases.
13279*/
13280#ifndef SQLITE_MAX_ATTACHED10
13281# define SQLITE_MAX_ATTACHED10 10
13282#endif
13283
13284
13285/*
13286** The maximum value of a ?nnn wildcard that the parser will accept.
13287** If the value exceeds 32767 then extra space is required for the Expr
13288** structure. But otherwise, we believe that the number can be as large
13289** as a signed 32-bit integer can hold.
13290*/
13291#ifndef SQLITE_MAX_VARIABLE_NUMBER500000
13292# define SQLITE_MAX_VARIABLE_NUMBER500000 32766
13293#endif
13294
13295/* Maximum page size. The upper bound on this value is 65536. This a limit
13296** imposed by the use of 16-bit offsets within each page.
13297**
13298** Earlier versions of SQLite allowed the user to change this value at
13299** compile time. This is no longer permitted, on the grounds that it creates
13300** a library that is technically incompatible with an SQLite library
13301** compiled with a different limit. If a process operating on a database
13302** with a page-size of 65536 bytes crashes, then an instance of SQLite
13303** compiled with the default page-size limit will not be able to rollback
13304** the aborted transaction. This could lead to database corruption.
13305*/
13306#ifdef SQLITE_MAX_PAGE_SIZE65536
13307# undef SQLITE_MAX_PAGE_SIZE65536
13308#endif
13309#define SQLITE_MAX_PAGE_SIZE65536 65536
13310
13311
13312/*
13313** The default size of a database page.
13314*/
13315#ifndef SQLITE_DEFAULT_PAGE_SIZE4096
13316# define SQLITE_DEFAULT_PAGE_SIZE4096 4096
13317#endif
13318#if SQLITE_DEFAULT_PAGE_SIZE4096>SQLITE_MAX_PAGE_SIZE65536
13319# undef SQLITE_DEFAULT_PAGE_SIZE4096
13320# define SQLITE_DEFAULT_PAGE_SIZE4096 SQLITE_MAX_PAGE_SIZE65536
13321#endif
13322
13323/*
13324** Ordinarily, if no value is explicitly provided, SQLite creates databases
13325** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
13326** device characteristics (sector-size and atomic write() support),
13327** SQLite may choose a larger value. This constant is the maximum value
13328** SQLite will choose on its own.
13329*/
13330#ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE8192
13331# define SQLITE_MAX_DEFAULT_PAGE_SIZE8192 8192
13332#endif
13333#if SQLITE_MAX_DEFAULT_PAGE_SIZE8192>SQLITE_MAX_PAGE_SIZE65536
13334# undef SQLITE_MAX_DEFAULT_PAGE_SIZE8192
13335# define SQLITE_MAX_DEFAULT_PAGE_SIZE8192 SQLITE_MAX_PAGE_SIZE65536
13336#endif
13337
13338
13339/*
13340** Maximum number of pages in one database file.
13341**
13342** This is really just the default value for the max_page_count pragma.
13343** This value can be lowered (or raised) at run-time using that the
13344** max_page_count macro.
13345*/
13346#ifndef SQLITE_MAX_PAGE_COUNT1073741823
13347# define SQLITE_MAX_PAGE_COUNT1073741823 1073741823
13348#endif
13349
13350/*
13351** Maximum length (in bytes) of the pattern in a LIKE or GLOB
13352** operator.
13353*/
13354#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH50000
13355# define SQLITE_MAX_LIKE_PATTERN_LENGTH50000 50000
13356#endif
13357
13358/*
13359** Maximum depth of recursion for triggers.
13360**
13361** A value of 1 means that a trigger program will not be able to itself
13362** fire any triggers. A value of 0 means that no trigger programs at all
13363** may be executed.
13364*/
13365#ifndef SQLITE_MAX_TRIGGER_DEPTH1000
13366# define SQLITE_MAX_TRIGGER_DEPTH1000 1000
13367#endif
13368
13369/************** End of sqliteLimit.h *****************************************/
13370/************** Continuing where we left off in sqliteInt.h ******************/
13371
13372/* Disable nuisance warnings on Borland compilers */
13373#if defined(__BORLANDC__)
13374#pragma warn -rch /* unreachable code */
13375#pragma warn -ccc /* Condition is always true or false */
13376#pragma warn -aus /* Assigned value is never used */
13377#pragma warn -csu /* Comparing signed and unsigned */
13378#pragma warn -spa /* Suspicious pointer arithmetic */
13379#endif
13380
13381/*
13382** WAL mode depends on atomic aligned 32-bit loads and stores in a few
13383** places. The following macros try to make this explicit.
13384*/
13385#ifndef __has_extension
13386# define0 __has_extension(x)0 0 /* compatibility with non-clang compilers */
13387#endif
13388#if GCC_VERSION(4*1000000+2*1000+1)>=4007000 || __has_extension(c_atomic)1
13389# define SQLITE_ATOMIC_INTRINSICS1 1
13390# define AtomicLoad(PTR)__atomic_load_n((PTR),0) __atomic_load_n((PTR),__ATOMIC_RELAXED0)
13391# define AtomicStore(PTR,VAL)__atomic_store_n((PTR),(VAL),0) __atomic_store_n((PTR),(VAL),__ATOMIC_RELAXED0)
13392#else
13393# define SQLITE_ATOMIC_INTRINSICS1 0
13394# define AtomicLoad(PTR)__atomic_load_n((PTR),0) (*(PTR))
13395# define AtomicStore(PTR,VAL)__atomic_store_n((PTR),(VAL),0) (*(PTR) = (VAL))
13396#endif
13397
13398/*
13399** Include standard header files as necessary
13400*/
13401#ifdef HAVE_STDINT_H
13402#include <stdint.h>
13403#endif
13404#ifdef HAVE_INTTYPES_H
13405#include <inttypes.h>
13406#endif
13407
13408/*
13409** The following macros are used to cast pointers to integers and
13410** integers to pointers. The way you do this varies from one compiler
13411** to the next, so we have developed the following set of #if statements
13412** to generate appropriate macros for a wide range of compilers.
13413**
13414** The correct "ANSI" way to do this is to use the intptr_t type.
13415** Unfortunately, that typedef is not available on all compilers, or
13416** if it is available, it requires an #include of specific headers
13417** that vary from one machine to the next.
13418**
13419** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
13420** the ((void*)&((char*)0)[X]) construct. But MSVC chokes on ((void*)(X)).
13421** So we have to define the macros in different ways depending on the
13422** compiler.
13423*/
13424#if defined(HAVE_STDINT_H) /* Use this case if we have ANSI headers */
13425# define SQLITE_INT_TO_PTR(X)((void*)(long int)(X)) ((void*)(intptr_t)(X))
13426# define SQLITE_PTR_TO_INT(X)((int)(long int)(X)) ((int)(intptr_t)(X))
13427#elif defined(__PTRDIFF_TYPE__long int) /* This case should work for GCC */
13428# define SQLITE_INT_TO_PTR(X)((void*)(long int)(X)) ((void*)(__PTRDIFF_TYPE__long int)(X))
13429# define SQLITE_PTR_TO_INT(X)((int)(long int)(X)) ((int)(__PTRDIFF_TYPE__long int)(X))
13430#elif !defined(__GNUC__4) /* Works for compilers other than LLVM */
13431# define SQLITE_INT_TO_PTR(X)((void*)(long int)(X)) ((void*)&((char*)0)[X])
13432# define SQLITE_PTR_TO_INT(X)((int)(long int)(X)) ((int)(((char*)X)-(char*)0))
13433#else /* Generates a warning - but it always works */
13434# define SQLITE_INT_TO_PTR(X)((void*)(long int)(X)) ((void*)(X))
13435# define SQLITE_PTR_TO_INT(X)((int)(long int)(X)) ((int)(X))
13436#endif
13437
13438/*
13439** A macro to hint to the compiler that a function should not be
13440** inlined.
13441*/
13442#if defined(__GNUC__4)
13443# define SQLITE_NOINLINE__attribute__((noinline)) __attribute__((noinline))
13444#elif defined(_MSC_VER) && _MSC_VER>=1310
13445# define SQLITE_NOINLINE__attribute__((noinline)) __declspec(noinline)
13446#else
13447# define SQLITE_NOINLINE__attribute__((noinline))
13448#endif
13449
13450/*
13451** Make sure that the compiler intrinsics we desire are enabled when
13452** compiling with an appropriate version of MSVC unless prevented by
13453** the SQLITE_DISABLE_INTRINSIC define.
13454*/
13455#if !defined(SQLITE_DISABLE_INTRINSIC)
13456# if defined(_MSC_VER) && _MSC_VER>=1400
13457# if !defined(_WIN32_WCE)
13458# include <intrin.h>
13459# pragma intrinsic(_byteswap_ushort)
13460# pragma intrinsic(_byteswap_ulong)
13461# pragma intrinsic(_byteswap_uint64)
13462# pragma intrinsic(_ReadWriteBarrier)
13463# else
13464# include <cmnintrin.h>
13465# endif
13466# endif
13467#endif
13468
13469/*
13470** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
13471** 0 means mutexes are permanently disable and the library is never
13472** threadsafe. 1 means the library is serialized which is the highest
13473** level of threadsafety. 2 means the library is multithreaded - multiple
13474** threads can use SQLite as long as no two threads try to use the same
13475** database connection at the same time.
13476**
13477** Older versions of SQLite used an optional THREADSAFE macro.
13478** We support that for legacy.
13479**
13480** To ensure that the correct value of "THREADSAFE" is reported when querying
13481** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this
13482** logic is partially replicated in ctime.c. If it is updated here, it should
13483** also be updated there.
13484*/
13485#if !defined(SQLITE_THREADSAFE2)
13486# if defined(THREADSAFE)
13487# define SQLITE_THREADSAFE2 THREADSAFE
13488# else
13489# define SQLITE_THREADSAFE2 1 /* IMP: R-07272-22309 */
13490# endif
13491#endif
13492
13493/*
13494** Powersafe overwrite is on by default. But can be turned off using
13495** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
13496*/
13497#ifndef SQLITE_POWERSAFE_OVERWRITE1
13498# define SQLITE_POWERSAFE_OVERWRITE1 1
13499#endif
13500
13501/*
13502** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
13503** default unless SQLite is compiled with SQLITE_DEFAULT_MEMSTATUS=0 in
13504** which case memory allocation statistics are disabled by default.
13505*/
13506#if !defined(SQLITE_DEFAULT_MEMSTATUS1)
13507# define SQLITE_DEFAULT_MEMSTATUS1 1
13508#endif
13509
13510/*
13511** Exactly one of the following macros must be defined in order to
13512** specify which memory allocation subsystem to use.
13513**
13514** SQLITE_SYSTEM_MALLOC // Use normal system malloc()
13515** SQLITE_WIN32_MALLOC // Use Win32 native heap API
13516** SQLITE_ZERO_MALLOC // Use a stub allocator that always fails
13517** SQLITE_MEMDEBUG // Debugging version of system malloc()
13518**
13519** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
13520** assert() macro is enabled, each call into the Win32 native heap subsystem
13521** will cause HeapValidate to be called. If heap validation should fail, an
13522** assertion will be triggered.
13523**
13524** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
13525** the default.
13526*/
13527#if defined(SQLITE_SYSTEM_MALLOC1) \
13528 + defined(SQLITE_WIN32_MALLOC) \
13529 + defined(SQLITE_ZERO_MALLOC) \
13530 + defined(SQLITE_MEMDEBUG)>1
13531# error "Two or more of the following compile-time configuration options\
13532 are defined but at most one is allowed:\
13533 SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\
13534 SQLITE_ZERO_MALLOC"
13535#endif
13536#if defined(SQLITE_SYSTEM_MALLOC1) \
13537 + defined(SQLITE_WIN32_MALLOC) \
13538 + defined(SQLITE_ZERO_MALLOC) \
13539 + defined(SQLITE_MEMDEBUG)==0
13540# define SQLITE_SYSTEM_MALLOC1 1
13541#endif
13542
13543/*
13544** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
13545** sizes of memory allocations below this value where possible.
13546*/
13547#if !defined(SQLITE_MALLOC_SOFT_LIMIT1024)
13548# define SQLITE_MALLOC_SOFT_LIMIT1024 1024
13549#endif
13550
13551/*
13552** We need to define _XOPEN_SOURCE as follows in order to enable
13553** recursive mutexes on most Unix systems and fchmod() on OpenBSD.
13554** But _XOPEN_SOURCE define causes problems for Mac OS X, so omit
13555** it.
13556*/
13557#if !defined(_XOPEN_SOURCE700) && !defined(__DARWIN__) && !defined(__APPLE__)
13558# define _XOPEN_SOURCE700 600
13559#endif
13560
13561/*
13562** NDEBUG and SQLITE_DEBUG are opposites. It should always be true that
13563** defined(NDEBUG)==!defined(SQLITE_DEBUG). If this is not currently true,
13564** make it true by defining or undefining NDEBUG.
13565**
13566** Setting NDEBUG makes the code smaller and faster by disabling the
13567** assert() statements in the code. So we want the default action
13568** to be for NDEBUG to be set and NDEBUG to be undefined only if SQLITE_DEBUG
13569** is set. Thus NDEBUG becomes an opt-in rather than an opt-out
13570** feature.
13571*/
13572#if !defined(NDEBUG1) && !defined(SQLITE_DEBUG)
13573# define NDEBUG1 1
13574#endif
13575#if defined(NDEBUG1) && defined(SQLITE_DEBUG)
13576# undef NDEBUG1
13577#endif
13578
13579/*
13580** Enable SQLITE_ENABLE_EXPLAIN_COMMENTS if SQLITE_DEBUG is turned on.
13581*/
13582#if !defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) && defined(SQLITE_DEBUG)
13583# define SQLITE_ENABLE_EXPLAIN_COMMENTS 1
13584#endif
13585
13586/*
13587** The testcase() macro is used to aid in coverage testing. When
13588** doing coverage testing, the condition inside the argument to
13589** testcase() must be evaluated both true and false in order to
13590** get full branch coverage. The testcase() macro is inserted
13591** to help ensure adequate test coverage in places where simple
13592** condition/decision coverage is inadequate. For example, testcase()
13593** can be used to make sure boundary values are tested. For
13594** bitmask tests, testcase() can be used to make sure each bit
13595** is significant and used at least once. On switch statements
13596** where multiple cases go to the same block of code, testcase()
13597** can insure that all cases are evaluated.
13598*/
13599#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_DEBUG)
13600# ifndef SQLITE_AMALGAMATION1
13601 extern unsigned int sqlite3CoverageCounter;
13602# endif
13603# define testcase(X) if( X ){ sqlite3CoverageCounter += (unsigned)__LINE__13603; }
13604#else
13605# define testcase(X)
13606#endif
13607
13608/*
13609** The TESTONLY macro is used to enclose variable declarations or
13610** other bits of code that are needed to support the arguments
13611** within testcase() and assert() macros.
13612*/
13613#if !defined(NDEBUG1) || defined(SQLITE_COVERAGE_TEST)
13614# define TESTONLY(X) X
13615#else
13616# define TESTONLY(X)
13617#endif
13618
13619/*
13620** Sometimes we need a small amount of code such as a variable initialization
13621** to setup for a later assert() statement. We do not want this code to
13622** appear when assert() is disabled. The following macro is therefore
13623** used to contain that setup code. The "VVA" acronym stands for
13624** "Verification, Validation, and Accreditation". In other words, the
13625** code within VVA_ONLY() will only run during verification processes.
13626*/
13627#ifndef NDEBUG1
13628# define VVA_ONLY(X) X
13629#else
13630# define VVA_ONLY(X)
13631#endif
13632
13633/*
13634** Disable ALWAYS() and NEVER() (make them pass-throughs) for coverage
13635** and mutation testing
13636*/
13637#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
13638# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
13639#endif
13640
13641/*
13642** The ALWAYS and NEVER macros surround boolean expressions which
13643** are intended to always be true or false, respectively. Such
13644** expressions could be omitted from the code completely. But they
13645** are included in a few cases in order to enhance the resilience
13646** of SQLite to unexpected behavior - to make the code "self-healing"
13647** or "ductile" rather than being "brittle" and crashing at the first
13648** hint of unplanned behavior.
13649**
13650** In other words, ALWAYS and NEVER are added for defensive code.
13651**
13652** When doing coverage testing ALWAYS and NEVER are hard-coded to
13653** be true and false so that the unreachable code they specify will
13654** not be counted as untested code.
13655*/
13656#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
13657# define ALWAYS(X)(X) (1)
13658# define NEVER(X)(X) (0)
13659#elif !defined(NDEBUG1)
13660# define ALWAYS(X)(X) ((X)?1:(assert(0)((void) (0)),0))
13661# define NEVER(X)(X) ((X)?(assert(0)((void) (0)),1):0)
13662#else
13663# define ALWAYS(X)(X) (X)
13664# define NEVER(X)(X) (X)
13665#endif
13666
13667/*
13668** Some conditionals are optimizations only. In other words, if the
13669** conditionals are replaced with a constant 1 (true) or 0 (false) then
13670** the correct answer is still obtained, though perhaps not as quickly.
13671**
13672** The following macros mark these optimizations conditionals.
13673*/
13674#if defined(SQLITE_MUTATION_TEST)
13675# define OK_IF_ALWAYS_TRUE(X)(X) (1)
13676# define OK_IF_ALWAYS_FALSE(X)(X) (0)
13677#else
13678# define OK_IF_ALWAYS_TRUE(X)(X) (X)
13679# define OK_IF_ALWAYS_FALSE(X)(X) (X)
13680#endif
13681
13682/*
13683** Some malloc failures are only possible if SQLITE_TEST_REALLOC_STRESS is
13684** defined. We need to defend against those failures when testing with
13685** SQLITE_TEST_REALLOC_STRESS, but we don't want the unreachable branches
13686** during a normal build. The following macro can be used to disable tests
13687** that are always false except when SQLITE_TEST_REALLOC_STRESS is set.
13688*/
13689#if defined(SQLITE_TEST_REALLOC_STRESS)
13690# define ONLY_IF_REALLOC_STRESS(X)(0) (X)
13691#elif !defined(NDEBUG1)
13692# define ONLY_IF_REALLOC_STRESS(X)(0) ((X)?(assert(0)((void) (0)),1):0)
13693#else
13694# define ONLY_IF_REALLOC_STRESS(X)(0) (0)
13695#endif
13696
13697/*
13698** Declarations used for tracing the operating system interfaces.
13699*/
13700#if defined(SQLITE_FORCE_OS_TRACE) || defined(SQLITE_TEST) || \
13701 (defined(SQLITE_DEBUG) && SQLITE_OS_WIN0)
13702 extern int sqlite3OSTrace;
13703# define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
13704# define SQLITE_HAVE_OS_TRACE
13705#else
13706# define OSTRACE(X)
13707# undef SQLITE_HAVE_OS_TRACE
13708#endif
13709
13710/*
13711** Is the sqlite3ErrName() function needed in the build? Currently,
13712** it is needed by "mutex_w32.c" (when debugging), "os_win.c" (when
13713** OSTRACE is enabled), and by several "test*.c" files (which are
13714** compiled using SQLITE_TEST).
13715*/
13716#if defined(SQLITE_HAVE_OS_TRACE) || defined(SQLITE_TEST) || \
13717 (defined(SQLITE_DEBUG) && SQLITE_OS_WIN0)
13718# define SQLITE_NEED_ERR_NAME
13719#else
13720# undef SQLITE_NEED_ERR_NAME
13721#endif
13722
13723/*
13724** SQLITE_ENABLE_EXPLAIN_COMMENTS is incompatible with SQLITE_OMIT_EXPLAIN
13725*/
13726#ifdef SQLITE_OMIT_EXPLAIN
13727# undef SQLITE_ENABLE_EXPLAIN_COMMENTS
13728#endif
13729
13730/*
13731** SQLITE_OMIT_VIRTUALTABLE implies SQLITE_OMIT_ALTERTABLE
13732*/
13733#if defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_ALTERTABLE)
13734# define SQLITE_OMIT_ALTERTABLE
13735#endif
13736
13737/*
13738** Return true (non-zero) if the input is an integer that is too large
13739** to fit in 32-bits. This macro is used inside of various testcase()
13740** macros to verify that we have tested SQLite for large-file support.
13741*/
13742#define IS_BIG_INT(X)(((X)&~(i64)0xffffffff)!=0) (((X)&~(i64)0xffffffff)!=0)
13743
13744/*
13745** The macro unlikely() is a hint that surrounds a boolean
13746** expression that is usually false. Macro likely() surrounds
13747** a boolean expression that is usually true. These hints could,
13748** in theory, be used by the compiler to generate better code, but
13749** currently they are just comments for human readers.
13750*/
13751#define likely(X)(X) (X)
13752#define unlikely(X)(X) (X)
13753
13754/************** Include hash.h in the middle of sqliteInt.h ******************/
13755/************** Begin file hash.h ********************************************/
13756/*
13757** 2001 September 22
13758**
13759** The author disclaims copyright to this source code. In place of
13760** a legal notice, here is a blessing:
13761**
13762** May you do good and not evil.
13763** May you find forgiveness for yourself and forgive others.
13764** May you share freely, never taking more than you give.
13765**
13766*************************************************************************
13767** This is the header file for the generic hash-table implementation
13768** used in SQLite.
13769*/
13770#ifndef SQLITE_HASH_H
13771#define SQLITE_HASH_H
13772
13773/* Forward declarations of structures. */
13774typedef struct Hash Hash;
13775typedef struct HashElem HashElem;
13776
13777/* A complete hash table is an instance of the following structure.
13778** The internals of this structure are intended to be opaque -- client
13779** code should not attempt to access or modify the fields of this structure
13780** directly. Change this structure only by using the routines below.
13781** However, some of the "procedures" and "functions" for modifying and
13782** accessing this structure are really macros, so we can't really make
13783** this structure opaque.
13784**
13785** All elements of the hash table are on a single doubly-linked list.
13786** Hash.first points to the head of this list.
13787**
13788** There are Hash.htsize buckets. Each bucket points to a spot in
13789** the global doubly-linked list. The contents of the bucket are the
13790** element pointed to plus the next _ht.count-1 elements in the list.
13791**
13792** Hash.htsize and Hash.ht may be zero. In that case lookup is done
13793** by a linear search of the global list. For small tables, the
13794** Hash.ht table is never allocated because if there are few elements
13795** in the table, it is faster to do a linear search than to manage
13796** the hash table.
13797*/
13798struct Hash {
13799 unsigned int htsize; /* Number of buckets in the hash table */
13800 unsigned int count; /* Number of entries in this table */
13801 HashElem *first; /* The first element of the array */
13802 struct _ht { /* the hash table */
13803 unsigned int count; /* Number of entries with this hash */
13804 HashElem *chain; /* Pointer to first entry with this hash */
13805 } *ht;
13806};
13807
13808/* Each element in the hash table is an instance of the following
13809** structure. All elements are stored on a single doubly-linked list.
13810**
13811** Again, this structure is intended to be opaque, but it can't really
13812** be opaque because it is used by macros.
13813*/
13814struct HashElem {
13815 HashElem *next, *prev; /* Next and previous elements in the table */
13816 void *data; /* Data associated with this element */
13817 const char *pKey; /* Key associated with this element */
13818};
13819
13820/*
13821** Access routines. To delete, insert a NULL pointer.
13822*/
13823SQLITE_PRIVATEstatic void sqlite3HashInit(Hash*);
13824SQLITE_PRIVATEstatic void *sqlite3HashInsert(Hash*, const char *pKey, void *pData);
13825SQLITE_PRIVATEstatic void *sqlite3HashFind(const Hash*, const char *pKey);
13826SQLITE_PRIVATEstatic void sqlite3HashClear(Hash*);
13827
13828/*
13829** Macros for looping over all elements of a hash table. The idiom is
13830** like this:
13831**
13832** Hash h;
13833** HashElem *p;
13834** ...
13835** for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
13836** SomeStructure *pData = sqliteHashData(p);
13837** // do something with pData
13838** }
13839*/
13840#define sqliteHashFirst(H)((H)->first) ((H)->first)
13841#define sqliteHashNext(E)((E)->next) ((E)->next)
13842#define sqliteHashData(E)((E)->data) ((E)->data)
13843/* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */
13844/* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */
13845
13846/*
13847** Number of entries in a hash table
13848*/
13849#define sqliteHashCount(H)((H)->count) ((H)->count)
13850
13851#endif /* SQLITE_HASH_H */
13852
13853/************** End of hash.h ************************************************/
13854/************** Continuing where we left off in sqliteInt.h ******************/
13855/************** Include parse.h in the middle of sqliteInt.h *****************/
13856/************** Begin file parse.h *******************************************/
13857#define TK_SEMI1 1
13858#define TK_EXPLAIN2 2
13859#define TK_QUERY3 3
13860#define TK_PLAN4 4
13861#define TK_BEGIN5 5
13862#define TK_TRANSACTION6 6
13863#define TK_DEFERRED7 7
13864#define TK_IMMEDIATE8 8
13865#define TK_EXCLUSIVE9 9
13866#define TK_COMMIT10 10
13867#define TK_END11 11
13868#define TK_ROLLBACK12 12
13869#define TK_SAVEPOINT13 13
13870#define TK_RELEASE14 14
13871#define TK_TO15 15
13872#define TK_TABLE16 16
13873#define TK_CREATE17 17
13874#define TK_IF18 18
13875#define TK_NOT19 19
13876#define TK_EXISTS20 20
13877#define TK_TEMP21 21
13878#define TK_LP22 22
13879#define TK_RP23 23
13880#define TK_AS24 24
13881#define TK_COMMA25 25
13882#define TK_WITHOUT26 26
13883#define TK_ABORT27 27
13884#define TK_ACTION28 28
13885#define TK_AFTER29 29
13886#define TK_ANALYZE30 30
13887#define TK_ASC31 31
13888#define TK_ATTACH32 32
13889#define TK_BEFORE33 33
13890#define TK_BY34 34
13891#define TK_CASCADE35 35
13892#define TK_CAST36 36
13893#define TK_CONFLICT37 37
13894#define TK_DATABASE38 38
13895#define TK_DESC39 39
13896#define TK_DETACH40 40
13897#define TK_EACH41 41
13898#define TK_FAIL42 42
13899#define TK_OR43 43
13900#define TK_AND44 44
13901#define TK_IS45 45
13902#define TK_MATCH46 46
13903#define TK_LIKE_KW47 47
13904#define TK_BETWEEN48 48
13905#define TK_IN49 49
13906#define TK_ISNULL50 50
13907#define TK_NOTNULL51 51
13908#define TK_NE52 52
13909#define TK_EQ53 53
13910#define TK_GT54 54
13911#define TK_LE55 55
13912#define TK_LT56 56
13913#define TK_GE57 57
13914#define TK_ESCAPE58 58
13915#define TK_ID59 59
13916#define TK_COLUMNKW60 60
13917#define TK_DO61 61
13918#define TK_FOR62 62
13919#define TK_IGNORE63 63
13920#define TK_INITIALLY64 64
13921#define TK_INSTEAD65 65
13922#define TK_NO66 66
13923#define TK_KEY67 67
13924#define TK_OF68 68
13925#define TK_OFFSET69 69
13926#define TK_PRAGMA70 70
13927#define TK_RAISE71 71
13928#define TK_RECURSIVE72 72
13929#define TK_REPLACE73 73
13930#define TK_RESTRICT74 74
13931#define TK_ROW75 75
13932#define TK_ROWS76 76
13933#define TK_TRIGGER77 77
13934#define TK_VACUUM78 78
13935#define TK_VIEW79 79
13936#define TK_VIRTUAL80 80
13937#define TK_WITH81 81
13938#define TK_NULLS82 82
13939#define TK_FIRST83 83
13940#define TK_LAST84 84
13941#define TK_CURRENT85 85
13942#define TK_FOLLOWING86 86
13943#define TK_PARTITION87 87
13944#define TK_PRECEDING88 88
13945#define TK_RANGE89 89
13946#define TK_UNBOUNDED90 90
13947#define TK_EXCLUDE91 91
13948#define TK_GROUPS92 92
13949#define TK_OTHERS93 93
13950#define TK_TIES94 94
13951#define TK_GENERATED95 95
13952#define TK_ALWAYS96 96
13953#define TK_MATERIALIZED97 97
13954#define TK_REINDEX98 98
13955#define TK_RENAME99 99
13956#define TK_CTIME_KW100 100
13957#define TK_ANY101 101
13958#define TK_BITAND102 102
13959#define TK_BITOR103 103
13960#define TK_LSHIFT104 104
13961#define TK_RSHIFT105 105
13962#define TK_PLUS106 106
13963#define TK_MINUS107 107
13964#define TK_STAR108 108
13965#define TK_SLASH109 109
13966#define TK_REM110 110
13967#define TK_CONCAT111 111
13968#define TK_PTR112 112
13969#define TK_COLLATE113 113
13970#define TK_BITNOT114 114
13971#define TK_ON115 115
13972#define TK_INDEXED116 116
13973#define TK_STRING117 117
13974#define TK_JOIN_KW118 118
13975#define TK_CONSTRAINT119 119
13976#define TK_DEFAULT120 120
13977#define TK_NULL121 121
13978#define TK_PRIMARY122 122
13979#define TK_UNIQUE123 123
13980#define TK_CHECK124 124
13981#define TK_REFERENCES125 125
13982#define TK_AUTOINCR126 126
13983#define TK_INSERT127 127
13984#define TK_DELETE128 128
13985#define TK_UPDATE129 129
13986#define TK_SET130 130
13987#define TK_DEFERRABLE131 131
13988#define TK_FOREIGN132 132
13989#define TK_DROP133 133
13990#define TK_UNION134 134
13991#define TK_ALL135 135
13992#define TK_EXCEPT136 136
13993#define TK_INTERSECT137 137
13994#define TK_SELECT138 138
13995#define TK_VALUES139 139
13996#define TK_DISTINCT140 140
13997#define TK_DOT141 141
13998#define TK_FROM142 142
13999#define TK_JOIN143 143
14000#define TK_USING144 144
14001#define TK_ORDER145 145
14002#define TK_GROUP146 146
14003#define TK_HAVING147 147
14004#define TK_LIMIT148 148
14005#define TK_WHERE149 149
14006#define TK_RETURNING150 150
14007#define TK_INTO151 151
14008#define TK_NOTHING152 152
14009#define TK_FLOAT153 153
14010#define TK_BLOB154 154
14011#define TK_INTEGER155 155
14012#define TK_VARIABLE156 156
14013#define TK_CASE157 157
14014#define TK_WHEN158 158
14015#define TK_THEN159 159
14016#define TK_ELSE160 160
14017#define TK_INDEX161 161
14018#define TK_ALTER162 162
14019#define TK_ADD163 163
14020#define TK_WINDOW164 164
14021#define TK_OVER165 165
14022#define TK_FILTER166 166
14023#define TK_COLUMN167 167
14024#define TK_AGG_FUNCTION168 168
14025#define TK_AGG_COLUMN169 169
14026#define TK_TRUEFALSE170 170
14027#define TK_ISNOT171 171
14028#define TK_FUNCTION172 172
14029#define TK_UMINUS173 173
14030#define TK_UPLUS174 174
14031#define TK_TRUTH175 175
14032#define TK_REGISTER176 176
14033#define TK_VECTOR177 177
14034#define TK_SELECT_COLUMN178 178
14035#define TK_IF_NULL_ROW179 179
14036#define TK_ASTERISK180 180
14037#define TK_SPAN181 181
14038#define TK_ERROR182 182
14039#define TK_SPACE183 183
14040#define TK_ILLEGAL184 184
14041
14042/************** End of parse.h ***********************************************/
14043/************** Continuing where we left off in sqliteInt.h ******************/
14044#include <stdio.h>
14045#include <stdlib.h>
14046#include <string.h>
14047#include <assert.h>
14048#include <stddef.h>
14049
14050/*
14051** Use a macro to replace memcpy() if compiled with SQLITE_INLINE_MEMCPY.
14052** This allows better measurements of where memcpy() is used when running
14053** cachegrind. But this macro version of memcpy() is very slow so it
14054** should not be used in production. This is a performance measurement
14055** hack only.
14056*/
14057#ifdef SQLITE_INLINE_MEMCPY
14058# define memcpy(D,S,N) {char*xxd=(char*)(D);const char*xxs=(const char*)(S);\
14059 int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
14060#endif
14061
14062/*
14063** If compiling for a processor that lacks floating point support,
14064** substitute integer for floating-point
14065*/
14066#ifdef SQLITE_OMIT_FLOATING_POINT
14067# define double sqlite_int64
14068# define float sqlite_int64
14069# define LONGDOUBLE_TYPElong double sqlite_int64
14070# ifndef SQLITE_BIG_DBL(1e99)
14071# define SQLITE_BIG_DBL(1e99) (((sqlite3_int64)1)<<50)
14072# endif
14073# define SQLITE_OMIT_DATETIME_FUNCS 1
14074# define SQLITE_OMIT_TRACE 1
14075# undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
14076# undef SQLITE_HAVE_ISNAN1
14077#endif
14078#ifndef SQLITE_BIG_DBL(1e99)
14079# define SQLITE_BIG_DBL(1e99) (1e99)
14080#endif
14081
14082/*
14083** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
14084** afterward. Having this macro allows us to cause the C compiler
14085** to omit code used by TEMP tables without messy #ifndef statements.
14086*/
14087#ifdef SQLITE_OMIT_TEMPDB
14088#define OMIT_TEMPDB0 1
14089#else
14090#define OMIT_TEMPDB0 0
14091#endif
14092
14093/*
14094** The "file format" number is an integer that is incremented whenever
14095** the VDBE-level file format changes. The following macros define the
14096** the default file format for new databases and the maximum file format
14097** that the library can read.
14098*/
14099#define SQLITE_MAX_FILE_FORMAT4 4
14100#ifndef SQLITE_DEFAULT_FILE_FORMAT4
14101# define SQLITE_DEFAULT_FILE_FORMAT4 4
14102#endif
14103
14104/*
14105** Determine whether triggers are recursive by default. This can be
14106** changed at run-time using a pragma.
14107*/
14108#ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS0
14109# define SQLITE_DEFAULT_RECURSIVE_TRIGGERS0 0
14110#endif
14111
14112/*
14113** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
14114** on the command-line
14115*/
14116#ifndef SQLITE_TEMP_STORE1
14117# define SQLITE_TEMP_STORE1 1
14118#endif
14119
14120/*
14121** If no value has been provided for SQLITE_MAX_WORKER_THREADS, or if
14122** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it
14123** to zero.
14124*/
14125#if SQLITE_TEMP_STORE1==3 || SQLITE_THREADSAFE2==0
14126# undef SQLITE_MAX_WORKER_THREADS8
14127# define SQLITE_MAX_WORKER_THREADS8 0
14128#endif
14129#ifndef SQLITE_MAX_WORKER_THREADS8
14130# define SQLITE_MAX_WORKER_THREADS8 8
14131#endif
14132#ifndef SQLITE_DEFAULT_WORKER_THREADS0
14133# define SQLITE_DEFAULT_WORKER_THREADS0 0
14134#endif
14135#if SQLITE_DEFAULT_WORKER_THREADS0>SQLITE_MAX_WORKER_THREADS8
14136# undef SQLITE_MAX_WORKER_THREADS8
14137# define SQLITE_MAX_WORKER_THREADS8 SQLITE_DEFAULT_WORKER_THREADS0
14138#endif
14139
14140/*
14141** The default initial allocation for the pagecache when using separate
14142** pagecaches for each database connection. A positive number is the
14143** number of pages. A negative number N translations means that a buffer
14144** of -1024*N bytes is allocated and used for as many pages as it will hold.
14145**
14146** The default value of "20" was chosen to minimize the run-time of the
14147** speedtest1 test program with options: --shrink-memory --reprepare
14148*/
14149#ifndef SQLITE_DEFAULT_PCACHE_INITSZ20
14150# define SQLITE_DEFAULT_PCACHE_INITSZ20 20
14151#endif
14152
14153/*
14154** Default value for the SQLITE_CONFIG_SORTERREF_SIZE option.
14155*/
14156#ifndef SQLITE_DEFAULT_SORTERREF_SIZE0x7fffffff
14157# define SQLITE_DEFAULT_SORTERREF_SIZE0x7fffffff 0x7fffffff
14158#endif
14159
14160/*
14161** The compile-time options SQLITE_MMAP_READWRITE and
14162** SQLITE_ENABLE_BATCH_ATOMIC_WRITE are not compatible with one another.
14163** You must choose one or the other (or neither) but not both.
14164*/
14165#if defined(SQLITE_MMAP_READWRITE) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
14166#error Cannot use both SQLITE_MMAP_READWRITE and SQLITE_ENABLE_BATCH_ATOMIC_WRITE
14167#endif
14168
14169/*
14170** GCC does not define the offsetof() macro so we'll have to do it
14171** ourselves.
14172*/
14173#ifndef offsetof
14174#define offsetof(STRUCTURE,FIELD)__builtin_offsetof(STRUCTURE, FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
14175#endif
14176
14177/*
14178** Macros to compute minimum and maximum of two numbers.
14179*/
14180#ifndef MIN
14181# define MIN(A,B)((A)<(B)?(A):(B)) ((A)<(B)?(A):(B))
14182#endif
14183#ifndef MAX
14184# define MAX(A,B)((A)>(B)?(A):(B)) ((A)>(B)?(A):(B))
14185#endif
14186
14187/*
14188** Swap two objects of type TYPE.
14189*/
14190#define SWAP(TYPE,A,B){TYPE t=A; A=B; B=t;} {TYPE t=A; A=B; B=t;}
14191
14192/*
14193** Check to see if this machine uses EBCDIC. (Yes, believe it or
14194** not, there are still machines out there that use EBCDIC.)
14195*/
14196#if 'A' == '\301'
14197# define SQLITE_EBCDIC 1
14198#else
14199# define SQLITE_ASCII1 1
14200#endif
14201
14202/*
14203** Integers of known sizes. These typedefs might change for architectures
14204** where the sizes very. Preprocessor macros are available so that the
14205** types can be conveniently redefined at compile-type. Like this:
14206**
14207** cc '-DUINTPTR_TYPE=long long int' ...
14208*/
14209#ifndef UINT32_TYPEunsigned int
14210# ifdef HAVE_UINT32_T
14211# define UINT32_TYPEunsigned int uint32_t
14212# else
14213# define UINT32_TYPEunsigned int unsigned int
14214# endif
14215#endif
14216#ifndef UINT16_TYPEunsigned short int
14217# ifdef HAVE_UINT16_T
14218# define UINT16_TYPEunsigned short int uint16_t
14219# else
14220# define UINT16_TYPEunsigned short int unsigned short int
14221# endif
14222#endif
14223#ifndef INT16_TYPEshort int
14224# ifdef HAVE_INT16_T
14225# define INT16_TYPEshort int int16_t
14226# else
14227# define INT16_TYPEshort int short int
14228# endif
14229#endif
14230#ifndef UINT8_TYPEunsigned char
14231# ifdef HAVE_UINT8_T
14232# define UINT8_TYPEunsigned char uint8_t
14233# else
14234# define UINT8_TYPEunsigned char unsigned char
14235# endif
14236#endif
14237#ifndef INT8_TYPEsigned char
14238# ifdef HAVE_INT8_T
14239# define INT8_TYPEsigned char int8_t
14240# else
14241# define INT8_TYPEsigned char signed char
14242# endif
14243#endif
14244#ifndef LONGDOUBLE_TYPElong double
14245# define LONGDOUBLE_TYPElong double long double
14246#endif
14247typedef sqlite_int64 i64; /* 8-byte signed integer */
14248typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
14249typedef UINT32_TYPEunsigned int u32; /* 4-byte unsigned integer */
14250typedef UINT16_TYPEunsigned short int u16; /* 2-byte unsigned integer */
14251typedef INT16_TYPEshort int i16; /* 2-byte signed integer */
14252typedef UINT8_TYPEunsigned char u8; /* 1-byte unsigned integer */
14253typedef INT8_TYPEsigned char i8; /* 1-byte signed integer */
14254
14255/*
14256** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
14257** that can be stored in a u32 without loss of data. The value
14258** is 0x00000000ffffffff. But because of quirks of some compilers, we
14259** have to specify the value in the less intuitive manner shown:
14260*/
14261#define SQLITE_MAX_U32((((u64)1)<<32)-1) ((((u64)1)<<32)-1)
14262
14263/*
14264** The datatype used to store estimates of the number of rows in a
14265** table or index. This is an unsigned integer type. For 99.9% of
14266** the world, a 32-bit integer is sufficient. But a 64-bit integer
14267** can be used at compile-time if desired.
14268*/
14269#ifdef SQLITE_64BIT_STATS
14270 typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */
14271#else
14272 typedef u32 tRowcnt; /* 32-bit is the default */
14273#endif
14274
14275/*
14276** Estimated quantities used for query planning are stored as 16-bit
14277** logarithms. For quantity X, the value stored is 10*log2(X). This
14278** gives a possible range of values of approximately 1.0e986 to 1e-986.
14279** But the allowed values are "grainy". Not every value is representable.
14280** For example, quantities 16 and 17 are both represented by a LogEst
14281** of 40. However, since LogEst quantities are suppose to be estimates,
14282** not exact values, this imprecision is not a problem.
14283**
14284** "LogEst" is short for "Logarithmic Estimate".
14285**
14286** Examples:
14287** 1 -> 0 20 -> 43 10000 -> 132
14288** 2 -> 10 25 -> 46 25000 -> 146
14289** 3 -> 16 100 -> 66 1000000 -> 199
14290** 4 -> 20 1000 -> 99 1048576 -> 200
14291** 10 -> 33 1024 -> 100 4294967296 -> 320
14292**
14293** The LogEst can be negative to indicate fractional values.
14294** Examples:
14295**
14296** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
14297*/
14298typedef INT16_TYPEshort int LogEst;
14299
14300/*
14301** Set the SQLITE_PTRSIZE macro to the number of bytes in a pointer
14302*/
14303#ifndef SQLITE_PTRSIZE8
14304# if defined(__SIZEOF_POINTER__8)
14305# define SQLITE_PTRSIZE8 __SIZEOF_POINTER__8
14306# elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \
14307 defined(_M_ARM) || defined(__arm__) || defined(__x86) || \
14308 (defined(__APPLE__) && defined(__POWERPC__)) || \
14309 (defined(__TOS_AIX__) && !defined(__64BIT__))
14310# define SQLITE_PTRSIZE8 4
14311# else
14312# define SQLITE_PTRSIZE8 8
14313# endif
14314#endif
14315
14316/* The uptr type is an unsigned integer large enough to hold a pointer
14317*/
14318#if defined(HAVE_STDINT_H)
14319 typedef uintptr_t uptr;
14320#elif SQLITE_PTRSIZE8==4
14321 typedef u32 uptr;
14322#else
14323 typedef u64 uptr;
14324#endif
14325
14326/*
14327** The SQLITE_WITHIN(P,S,E) macro checks to see if pointer P points to
14328** something between S (inclusive) and E (exclusive).
14329**
14330** In other words, S is a buffer and E is a pointer to the first byte after
14331** the end of buffer S. This macro returns true if P points to something
14332** contained within the buffer S.
14333*/
14334#define SQLITE_WITHIN(P,S,E)(((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E))) (((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E)))
14335
14336
14337/*
14338** Macros to determine whether the machine is big or little endian,
14339** and whether or not that determination is run-time or compile-time.
14340**
14341** For best performance, an attempt is made to guess at the byte-order
14342** using C-preprocessor macros. If that is unsuccessful, or if
14343** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
14344** at run-time.
14345*/
14346#ifndef SQLITE_BYTEORDER1234
14347# if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
14348 defined(__x86_641) || defined(__x86_64__1) || defined(_M_X64) || \
14349 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
14350 defined(__ARMEL__) || defined(__AARCH64EL__) || defined(_M_ARM64)
14351# define SQLITE_BYTEORDER1234 1234
14352# elif defined(sparc) || defined(__ppc__) || \
14353 defined(__ARMEB__) || defined(__AARCH64EB__)
14354# define SQLITE_BYTEORDER1234 4321
14355# else
14356# define SQLITE_BYTEORDER1234 0
14357# endif
14358#endif
14359#if SQLITE_BYTEORDER1234==4321
14360# define SQLITE_BIGENDIAN0 1
14361# define SQLITE_LITTLEENDIAN1 0
14362# define SQLITE_UTF16NATIVE2 SQLITE_UTF16BE3
14363#elif SQLITE_BYTEORDER1234==1234
14364# define SQLITE_BIGENDIAN0 0
14365# define SQLITE_LITTLEENDIAN1 1
14366# define SQLITE_UTF16NATIVE2 SQLITE_UTF16LE2
14367#else
14368# ifdef SQLITE_AMALGAMATION1
14369 const int sqlite3one = 1;
14370# else
14371 extern const int sqlite3one;
14372# endif
14373# define SQLITE_BIGENDIAN0 (*(char *)(&sqlite3one)==0)
14374# define SQLITE_LITTLEENDIAN1 (*(char *)(&sqlite3one)==1)
14375# define SQLITE_UTF16NATIVE2 (SQLITE_BIGENDIAN0?SQLITE_UTF16BE3:SQLITE_UTF16LE2)
14376#endif
14377
14378/*
14379** Constants for the largest and smallest possible 64-bit signed integers.
14380** These macros are designed to work correctly on both 32-bit and 64-bit
14381** compilers.
14382*/
14383#define LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) (0xffffffff|(((i64)0x7fffffff)<<32))
14384#define LARGEST_UINT64(0xffffffff|(((u64)0xffffffff)<<32)) (0xffffffff|(((u64)0xffffffff)<<32))
14385#define SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) (((i64)-1) - LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)))
14386
14387/*
14388** Round up a number to the next larger multiple of 8. This is used
14389** to force 8-byte alignment on 64-bit architectures.
14390**
14391** ROUND8() always does the rounding, for any argument.
14392**
14393** ROUND8P() assumes that the argument is already an integer number of
14394** pointers in size, and so it is a no-op on systems where the pointer
14395** size is 8.
14396*/
14397#define ROUND8(x)(((x)+7)&~7) (((x)+7)&~7)
14398#if SQLITE_PTRSIZE8==8
14399# define ROUND8P(x)(x) (x)
14400#else
14401# define ROUND8P(x)(x) (((x)+7)&~7)
14402#endif
14403
14404/*
14405** Round down to the nearest multiple of 8
14406*/
14407#define ROUNDDOWN8(x)((x)&~7) ((x)&~7)
14408
14409/*
14410** Assert that the pointer X is aligned to an 8-byte boundary. This
14411** macro is used only within assert() to verify that the code gets
14412** all alignment restrictions correct.
14413**
14414** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
14415** underlying malloc() implementation might return us 4-byte aligned
14416** pointers. In that case, only verify 4-byte alignment.
14417*/
14418#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
14419# define EIGHT_BYTE_ALIGNMENT(X)((((char*)(X) - (char*)0)&7)==0) ((((char*)(X) - (char*)0)&3)==0)
14420#else
14421# define EIGHT_BYTE_ALIGNMENT(X)((((char*)(X) - (char*)0)&7)==0) ((((char*)(X) - (char*)0)&7)==0)
14422#endif
14423
14424/*
14425** Disable MMAP on platforms where it is known to not work
14426*/
14427#if defined(__OpenBSD__) || defined(__QNXNTO__)
14428# undef SQLITE_MAX_MMAP_SIZE20971520
14429# define SQLITE_MAX_MMAP_SIZE20971520 0
14430#endif
14431
14432/*
14433** Default maximum size of memory used by memory-mapped I/O in the VFS
14434*/
14435#ifdef __APPLE__
14436# include <TargetConditionals.h>
14437#endif
14438#ifndef SQLITE_MAX_MMAP_SIZE20971520
14439# if defined(__linux__1) \
14440 || defined(_WIN32) \
14441 || (defined(__APPLE__) && defined(__MACH__)) \
14442 || defined(__sun) \
14443 || defined(__FreeBSD__) \
14444 || defined(__DragonFly__)
14445# define SQLITE_MAX_MMAP_SIZE20971520 0x7fff0000 /* 2147418112 */
14446# else
14447# define SQLITE_MAX_MMAP_SIZE20971520 0
14448# endif
14449#endif
14450
14451/*
14452** The default MMAP_SIZE is zero on all platforms. Or, even if a larger
14453** default MMAP_SIZE is specified at compile-time, make sure that it does
14454** not exceed the maximum mmap size.
14455*/
14456#ifndef SQLITE_DEFAULT_MMAP_SIZE0
14457# define SQLITE_DEFAULT_MMAP_SIZE0 0
14458#endif
14459#if SQLITE_DEFAULT_MMAP_SIZE0>SQLITE_MAX_MMAP_SIZE20971520
14460# undef SQLITE_DEFAULT_MMAP_SIZE0
14461# define SQLITE_DEFAULT_MMAP_SIZE0 SQLITE_MAX_MMAP_SIZE20971520
14462#endif
14463
14464/*
14465** TREETRACE_ENABLED will be either 1 or 0 depending on whether or not
14466** the Abstract Syntax Tree tracing logic is turned on.
14467*/
14468#if !defined(SQLITE_AMALGAMATION1)
14469SQLITE_PRIVATEstatic u32 sqlite3TreeTrace;
14470#endif
14471#if defined(SQLITE_DEBUG) \
14472 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_SELECTTRACE) \
14473 || defined(SQLITE_ENABLE_TREETRACE))
14474# define TREETRACE_ENABLED0 1
14475# define SELECTTRACE(K,P,S,X) \
14476 if(sqlite3TreeTrace&(K)) \
14477 sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
14478 sqlite3DebugPrintf X
14479#else
14480# define SELECTTRACE(K,P,S,X)
14481# define TREETRACE_ENABLED0 0
14482#endif
14483
14484/*
14485** Macros for "wheretrace"
14486*/
14487SQLITE_PRIVATEstatic u32 sqlite3WhereTrace;
14488#if defined(SQLITE_DEBUG) \
14489 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
14490# define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
14491# define WHERETRACE_ENABLED 1
14492#else
14493# define WHERETRACE(K,X)
14494#endif
14495
14496
14497/*
14498** An instance of the following structure is used to store the busy-handler
14499** callback for a given sqlite handle.
14500**
14501** The sqlite.busyHandler member of the sqlite struct contains the busy
14502** callback for the database handle. Each pager opened via the sqlite
14503** handle is passed a pointer to sqlite.busyHandler. The busy-handler
14504** callback is currently invoked only from within pager.c.
14505*/
14506typedef struct BusyHandler BusyHandler;
14507struct BusyHandler {
14508 int (*xBusyHandler)(void *,int); /* The busy callback */
14509 void *pBusyArg; /* First arg to busy callback */
14510 int nBusy; /* Incremented with each busy call */
14511};
14512
14513/*
14514** Name of table that holds the database schema.
14515**
14516** The PREFERRED names are used whereever possible. But LEGACY is also
14517** used for backwards compatibility.
14518**
14519** 1. Queries can use either the PREFERRED or the LEGACY names
14520** 2. The sqlite3_set_authorizer() callback uses the LEGACY name
14521** 3. The PRAGMA table_list statement uses the PREFERRED name
14522**
14523** The LEGACY names are stored in the internal symbol hash table
14524** in support of (2). Names are translated using sqlite3PreferredTableName()
14525** for (3). The sqlite3FindTable() function takes care of translating
14526** names for (1).
14527**
14528** Note that "sqlite_temp_schema" can also be called "temp.sqlite_schema".
14529*/
14530#define LEGACY_SCHEMA_TABLE"sqlite_master" "sqlite_master"
14531#define LEGACY_TEMP_SCHEMA_TABLE"sqlite_temp_master" "sqlite_temp_master"
14532#define PREFERRED_SCHEMA_TABLE"sqlite_schema" "sqlite_schema"
14533#define PREFERRED_TEMP_SCHEMA_TABLE"sqlite_temp_schema" "sqlite_temp_schema"
14534
14535
14536/*
14537** The root-page of the schema table.
14538*/
14539#define SCHEMA_ROOT1 1
14540
14541/*
14542** The name of the schema table. The name is different for TEMP.
14543*/
14544#define SCHEMA_TABLE(x)((!0)&&(x==1)?"sqlite_temp_master":"sqlite_master") \
14545 ((!OMIT_TEMPDB0)&&(x==1)?LEGACY_TEMP_SCHEMA_TABLE"sqlite_temp_master":LEGACY_SCHEMA_TABLE"sqlite_master")
14546
14547/*
14548** A convenience macro that returns the number of elements in
14549** an array.
14550*/
14551#define ArraySize(X)((int)(sizeof(X)/sizeof(X[0]))) ((int)(sizeof(X)/sizeof(X[0])))
14552
14553/*
14554** Determine if the argument is a power of two
14555*/
14556#define IsPowerOfTwo(X)(((X)&((X)-1))==0) (((X)&((X)-1))==0)
14557
14558/*
14559** The following value as a destructor means to use sqlite3DbFree().
14560** The sqlite3DbFree() routine requires two parameters instead of the
14561** one parameter that destructors normally want. So we have to introduce
14562** this magic value that the code knows to handle differently. Any
14563** pointer will work here as long as it is distinct from SQLITE_STATIC
14564** and SQLITE_TRANSIENT.
14565*/
14566#define SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear) ((sqlite3_destructor_type)sqlite3OomClear)
14567
14568/*
14569** When SQLITE_OMIT_WSD is defined, it means that the target platform does
14570** not support Writable Static Data (WSD) such as global and static variables.
14571** All variables must either be on the stack or dynamically allocated from
14572** the heap. When WSD is unsupported, the variable declarations scattered
14573** throughout the SQLite code must become constants instead. The SQLITE_WSD
14574** macro is used for this purpose. And instead of referencing the variable
14575** directly, we use its constant as a key to lookup the run-time allocated
14576** buffer that holds real variable. The constant is also the initializer
14577** for the run-time allocated buffer.
14578**
14579** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
14580** macros become no-ops and have zero performance impact.
14581*/
14582#ifdef SQLITE_OMIT_WSD
14583 #define SQLITE_WSD const
14584 #define GLOBAL(t,v)v (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
14585 #define sqlite3GlobalConfigsqlite3Config GLOBAL(struct Sqlite3Config, sqlite3Config)sqlite3Config
14586SQLITE_API int sqlite3_wsd_init(int N, int J);
14587SQLITE_API void *sqlite3_wsd_find(void *K, int L);
14588#else
14589 #define SQLITE_WSD
14590 #define GLOBAL(t,v)v v
14591 #define sqlite3GlobalConfigsqlite3Config sqlite3Config
14592#endif
14593
14594/*
14595** The following macros are used to suppress compiler warnings and to
14596** make it clear to human readers when a function parameter is deliberately
14597** left unused within the body of a function. This usually happens when
14598** a function is called via a function pointer. For example the
14599** implementation of an SQL aggregate step callback may not use the
14600** parameter indicating the number of arguments passed to the aggregate,
14601** if it knows that this is enforced elsewhere.
14602**
14603** When a function parameter is not used at all within the body of a function,
14604** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
14605** However, these macros may also be used to suppress warnings related to
14606** parameters that may or may not be used depending on compilation options.
14607** For example those parameters only used in assert() statements. In these
14608** cases the parameters are named as per the usual conventions.
14609*/
14610#define UNUSED_PARAMETER(x)(void)(x) (void)(x)
14611#define UNUSED_PARAMETER2(x,y)(void)(x),(void)(y) UNUSED_PARAMETER(x)(void)(x),UNUSED_PARAMETER(y)(void)(y)
14612
14613/*
14614** Forward references to structures
14615*/
14616typedef struct AggInfo AggInfo;
14617typedef struct AuthContext AuthContext;
14618typedef struct AutoincInfo AutoincInfo;
14619typedef struct Bitvec Bitvec;
14620typedef struct CollSeq CollSeq;
14621typedef struct Column Column;
14622typedef struct Cte Cte;
14623typedef struct CteUse CteUse;
14624typedef struct Db Db;
14625typedef struct DbFixer DbFixer;
14626typedef struct Schema Schema;
14627typedef struct Expr Expr;
14628typedef struct ExprList ExprList;
14629typedef struct FKey FKey;
14630typedef struct FuncDestructor FuncDestructor;
14631typedef struct FuncDef FuncDef;
14632typedef struct FuncDefHash FuncDefHash;
14633typedef struct IdList IdList;
14634typedef struct Index Index;
14635typedef struct IndexSample IndexSample;
14636typedef struct KeyClass KeyClass;
14637typedef struct KeyInfo KeyInfo;
14638typedef struct Lookaside Lookaside;
14639typedef struct LookasideSlot LookasideSlot;
14640typedef struct Module Module;
14641typedef struct NameContext NameContext;
14642typedef struct OnOrUsing OnOrUsing;
14643typedef struct Parse Parse;
14644typedef struct ParseCleanup ParseCleanup;
14645typedef struct PreUpdate PreUpdate;
14646typedef struct PrintfArguments PrintfArguments;
14647typedef struct RenameToken RenameToken;
14648typedef struct Returning Returning;
14649typedef struct RowSet RowSet;
14650typedef struct Savepoint Savepoint;
14651typedef struct Select Select;
14652typedef struct SQLiteThread SQLiteThread;
14653typedef struct SelectDest SelectDest;
14654typedef struct SrcItem SrcItem;
14655typedef struct SrcList SrcList;
14656typedef struct sqlite3_str StrAccum; /* Internal alias for sqlite3_str */
14657typedef struct Table Table;
14658typedef struct TableLock TableLock;
14659typedef struct Token Token;
14660typedef struct TreeView TreeView;
14661typedef struct Trigger Trigger;
14662typedef struct TriggerPrg TriggerPrg;
14663typedef struct TriggerStep TriggerStep;
14664typedef struct UnpackedRecord UnpackedRecord;
14665typedef struct Upsert Upsert;
14666typedef struct VTable VTable;
14667typedef struct VtabCtx VtabCtx;
14668typedef struct Walker Walker;
14669typedef struct WhereInfo WhereInfo;
14670typedef struct Window Window;
14671typedef struct With With;
14672
14673
14674/*
14675** The bitmask datatype defined below is used for various optimizations.
14676**
14677** Changing this from a 64-bit to a 32-bit type limits the number of
14678** tables in a join to 32 instead of 64. But it also reduces the size
14679** of the library by 738 bytes on ix86.
14680*/
14681#ifdef SQLITE_BITMASK_TYPE
14682 typedef SQLITE_BITMASK_TYPE Bitmask;
14683#else
14684 typedef u64 Bitmask;
14685#endif
14686
14687/*
14688** The number of bits in a Bitmask. "BMS" means "BitMask Size".
14689*/
14690#define BMS((int)(sizeof(Bitmask)*8)) ((int)(sizeof(Bitmask)*8))
14691
14692/*
14693** A bit in a Bitmask
14694*/
14695#define MASKBIT(n)(((Bitmask)1)<<(n)) (((Bitmask)1)<<(n))
14696#define MASKBIT64(n)(((u64)1)<<(n)) (((u64)1)<<(n))
14697#define MASKBIT32(n)(((unsigned int)1)<<(n)) (((unsigned int)1)<<(n))
14698#define SMASKBIT32(n)((n)<=31?((unsigned int)1)<<(n):0) ((n)<=31?((unsigned int)1)<<(n):0)
14699#define ALLBITS((Bitmask)-1) ((Bitmask)-1)
14700
14701/* A VList object records a mapping between parameters/variables/wildcards
14702** in the SQL statement (such as $abc, @pqr, or :xyz) and the integer
14703** variable number associated with that parameter. See the format description
14704** on the sqlite3VListAdd() routine for more information. A VList is really
14705** just an array of integers.
14706*/
14707typedef int VList;
14708
14709/*
14710** Defer sourcing vdbe.h and btree.h until after the "u8" and
14711** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
14712** pointer types (i.e. FuncDef) defined above.
14713*/
14714/************** Include pager.h in the middle of sqliteInt.h *****************/
14715/************** Begin file pager.h *******************************************/
14716/*
14717** 2001 September 15
14718**
14719** The author disclaims copyright to this source code. In place of
14720** a legal notice, here is a blessing:
14721**
14722** May you do good and not evil.
14723** May you find forgiveness for yourself and forgive others.
14724** May you share freely, never taking more than you give.
14725**
14726*************************************************************************
14727** This header file defines the interface that the sqlite page cache
14728** subsystem. The page cache subsystem reads and writes a file a page
14729** at a time and provides a journal for rollback.
14730*/
14731
14732#ifndef SQLITE_PAGER_H
14733#define SQLITE_PAGER_H
14734
14735/*
14736** Default maximum size for persistent journal files. A negative
14737** value means no limit. This value may be overridden using the
14738** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
14739*/
14740#ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT32768
14741 #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT32768 -1
14742#endif
14743
14744/*
14745** The type used to represent a page number. The first page in a file
14746** is called page 1. 0 is used to represent "not a page".
14747*/
14748typedef u32 Pgno;
14749
14750/*
14751** Each open file is managed by a separate instance of the "Pager" structure.
14752*/
14753typedef struct Pager Pager;
14754
14755/*
14756** Handle type for pages.
14757*/
14758typedef struct PgHdr DbPage;
14759
14760/*
14761** Page number PAGER_SJ_PGNO is never used in an SQLite database (it is
14762** reserved for working around a windows/posix incompatibility). It is
14763** used in the journal to signify that the remainder of the journal file
14764** is devoted to storing a super-journal name - there are no more pages to
14765** roll back. See comments for function writeSuperJournal() in pager.c
14766** for details.
14767*/
14768#define PAGER_SJ_PGNO_COMPUTED(x)((Pgno)((sqlite3PendingByte/((x)->pageSize))+1)) ((Pgno)((PENDING_BYTEsqlite3PendingByte/((x)->pageSize))+1))
14769#define PAGER_SJ_PGNO(x)((x)->lckPgno) ((x)->lckPgno)
14770
14771/*
14772** Allowed values for the flags parameter to sqlite3PagerOpen().
14773**
14774** NOTE: These values must match the corresponding BTREE_ values in btree.h.
14775*/
14776#define PAGER_OMIT_JOURNAL0x0001 0x0001 /* Do not use a rollback journal */
14777#define PAGER_MEMORY0x0002 0x0002 /* In-memory database */
14778
14779/*
14780** Valid values for the second argument to sqlite3PagerLockingMode().
14781*/
14782#define PAGER_LOCKINGMODE_QUERY-1 -1
14783#define PAGER_LOCKINGMODE_NORMAL0 0
14784#define PAGER_LOCKINGMODE_EXCLUSIVE1 1
14785
14786/*
14787** Numeric constants that encode the journalmode.
14788**
14789** The numeric values encoded here (other than PAGER_JOURNALMODE_QUERY)
14790** are exposed in the API via the "PRAGMA journal_mode" command and
14791** therefore cannot be changed without a compatibility break.
14792*/
14793#define PAGER_JOURNALMODE_QUERY(-1) (-1) /* Query the value of journalmode */
14794#define PAGER_JOURNALMODE_DELETE0 0 /* Commit by deleting journal file */
14795#define PAGER_JOURNALMODE_PERSIST1 1 /* Commit by zeroing journal header */
14796#define PAGER_JOURNALMODE_OFF2 2 /* Journal omitted. */
14797#define PAGER_JOURNALMODE_TRUNCATE3 3 /* Commit by truncating journal */
14798#define PAGER_JOURNALMODE_MEMORY4 4 /* In-memory journal file */
14799#define PAGER_JOURNALMODE_WAL5 5 /* Use write-ahead logging */
14800
14801/*
14802** Flags that make up the mask passed to sqlite3PagerGet().
14803*/
14804#define PAGER_GET_NOCONTENT0x01 0x01 /* Do not load data from disk */
14805#define PAGER_GET_READONLY0x02 0x02 /* Read-only page is acceptable */
14806
14807/*
14808** Flags for sqlite3PagerSetFlags()
14809**
14810** Value constraints (enforced via assert()):
14811** PAGER_FULLFSYNC == SQLITE_FullFSync
14812** PAGER_CKPT_FULLFSYNC == SQLITE_CkptFullFSync
14813** PAGER_CACHE_SPILL == SQLITE_CacheSpill
14814*/
14815#define PAGER_SYNCHRONOUS_OFF0x01 0x01 /* PRAGMA synchronous=OFF */
14816#define PAGER_SYNCHRONOUS_NORMAL0x02 0x02 /* PRAGMA synchronous=NORMAL */
14817#define PAGER_SYNCHRONOUS_FULL0x03 0x03 /* PRAGMA synchronous=FULL */
14818#define PAGER_SYNCHRONOUS_EXTRA0x04 0x04 /* PRAGMA synchronous=EXTRA */
14819#define PAGER_SYNCHRONOUS_MASK0x07 0x07 /* Mask for four values above */
14820#define PAGER_FULLFSYNC0x08 0x08 /* PRAGMA fullfsync=ON */
14821#define PAGER_CKPT_FULLFSYNC0x10 0x10 /* PRAGMA checkpoint_fullfsync=ON */
14822#define PAGER_CACHESPILL0x20 0x20 /* PRAGMA cache_spill=ON */
14823#define PAGER_FLAGS_MASK0x38 0x38 /* All above except SYNCHRONOUS */
14824
14825/*
14826** The remainder of this file contains the declarations of the functions
14827** that make up the Pager sub-system API. See source code comments for
14828** a detailed description of each routine.
14829*/
14830
14831/* Open and close a Pager connection. */
14832SQLITE_PRIVATEstatic int sqlite3PagerOpen(
14833 sqlite3_vfs*,
14834 Pager **ppPager,
14835 const char*,
14836 int,
14837 int,
14838 int,
14839 void(*)(DbPage*)
14840);
14841SQLITE_PRIVATEstatic int sqlite3PagerClose(Pager *pPager, sqlite3*);
14842SQLITE_PRIVATEstatic int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
14843
14844/* Functions used to configure a Pager object. */
14845SQLITE_PRIVATEstatic void sqlite3PagerSetBusyHandler(Pager*, int(*)(void *), void *);
14846SQLITE_PRIVATEstatic int sqlite3PagerSetPagesize(Pager*, u32*, int);
14847SQLITE_PRIVATEstatic Pgno sqlite3PagerMaxPageCount(Pager*, Pgno);
14848SQLITE_PRIVATEstatic void sqlite3PagerSetCachesize(Pager*, int);
14849SQLITE_PRIVATEstatic int sqlite3PagerSetSpillsize(Pager*, int);
14850SQLITE_PRIVATEstatic void sqlite3PagerSetMmapLimit(Pager *, sqlite3_int64);
14851SQLITE_PRIVATEstatic void sqlite3PagerShrink(Pager*);
14852SQLITE_PRIVATEstatic void sqlite3PagerSetFlags(Pager*,unsigned);
14853SQLITE_PRIVATEstatic int sqlite3PagerLockingMode(Pager *, int);
14854SQLITE_PRIVATEstatic int sqlite3PagerSetJournalMode(Pager *, int);
14855SQLITE_PRIVATEstatic int sqlite3PagerGetJournalMode(Pager*);
14856SQLITE_PRIVATEstatic int sqlite3PagerOkToChangeJournalMode(Pager*);
14857SQLITE_PRIVATEstatic i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
14858SQLITE_PRIVATEstatic sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
14859SQLITE_PRIVATEstatic int sqlite3PagerFlush(Pager*);
14860
14861/* Functions used to obtain and release page references. */
14862SQLITE_PRIVATEstatic int sqlite3PagerGet(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
14863SQLITE_PRIVATEstatic DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
14864SQLITE_PRIVATEstatic void sqlite3PagerRef(DbPage*);
14865SQLITE_PRIVATEstatic void sqlite3PagerUnref(DbPage*);
14866SQLITE_PRIVATEstatic void sqlite3PagerUnrefNotNull(DbPage*);
14867SQLITE_PRIVATEstatic void sqlite3PagerUnrefPageOne(DbPage*);
14868
14869/* Operations on page references. */
14870SQLITE_PRIVATEstatic int sqlite3PagerWrite(DbPage*);
14871SQLITE_PRIVATEstatic void sqlite3PagerDontWrite(DbPage*);
14872SQLITE_PRIVATEstatic int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
14873SQLITE_PRIVATEstatic int sqlite3PagerPageRefcount(DbPage*);
14874SQLITE_PRIVATEstatic void *sqlite3PagerGetData(DbPage *);
14875SQLITE_PRIVATEstatic void *sqlite3PagerGetExtra(DbPage *);
14876
14877/* Functions used to manage pager transactions and savepoints. */
14878SQLITE_PRIVATEstatic void sqlite3PagerPagecount(Pager*, int*);
14879SQLITE_PRIVATEstatic int sqlite3PagerBegin(Pager*, int exFlag, int);
14880SQLITE_PRIVATEstatic int sqlite3PagerCommitPhaseOne(Pager*,const char *zSuper, int);
14881SQLITE_PRIVATEstatic int sqlite3PagerExclusiveLock(Pager*);
14882SQLITE_PRIVATEstatic int sqlite3PagerSync(Pager *pPager, const char *zSuper);
14883SQLITE_PRIVATEstatic int sqlite3PagerCommitPhaseTwo(Pager*);
14884SQLITE_PRIVATEstatic int sqlite3PagerRollback(Pager*);
14885SQLITE_PRIVATEstatic int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
14886SQLITE_PRIVATEstatic int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
14887SQLITE_PRIVATEstatic int sqlite3PagerSharedLock(Pager *pPager);
14888
14889#ifndef SQLITE_OMIT_WAL
14890SQLITE_PRIVATEstatic int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*);
14891SQLITE_PRIVATEstatic int sqlite3PagerWalSupported(Pager *pPager);
14892SQLITE_PRIVATEstatic int sqlite3PagerWalCallback(Pager *pPager);
14893SQLITE_PRIVATEstatic int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
14894SQLITE_PRIVATEstatic int sqlite3PagerCloseWal(Pager *pPager, sqlite3*);
14895# ifdef SQLITE_ENABLE_SNAPSHOT1
14896SQLITE_PRIVATEstatic int sqlite3PagerSnapshotGet(Pager*, sqlite3_snapshot **ppSnapshot);
14897SQLITE_PRIVATEstatic int sqlite3PagerSnapshotOpen(Pager*, sqlite3_snapshot *pSnapshot);
14898SQLITE_PRIVATEstatic int sqlite3PagerSnapshotRecover(Pager *pPager);
14899SQLITE_PRIVATEstatic int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot);
14900SQLITE_PRIVATEstatic void sqlite3PagerSnapshotUnlock(Pager *pPager);
14901# endif
14902#endif
14903
14904#if !defined(SQLITE_OMIT_WAL) && defined(SQLITE_ENABLE_SETLK_TIMEOUT)
14905SQLITE_PRIVATEstatic int sqlite3PagerWalWriteLock(Pager*, int)0;
14906SQLITE_PRIVATEstatic void sqlite3PagerWalDb(Pager*, sqlite3*);
14907#else
14908# define sqlite3PagerWalWriteLock(y,z)0 SQLITE_OK0
14909# define sqlite3PagerWalDb(x,y)
14910#endif
14911
14912#ifdef SQLITE_DIRECT_OVERFLOW_READ
14913SQLITE_PRIVATEstatic int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno);
14914#endif
14915
14916#ifdef SQLITE_ENABLE_ZIPVFS
14917SQLITE_PRIVATEstatic int sqlite3PagerWalFramesize(Pager *pPager);
14918#endif
14919
14920/* Functions used to query pager state and configuration. */
14921SQLITE_PRIVATEstatic u8 sqlite3PagerIsreadonly(Pager*);
14922SQLITE_PRIVATEstatic u32 sqlite3PagerDataVersion(Pager*);
14923#ifdef SQLITE_DEBUG
14924SQLITE_PRIVATEstatic int sqlite3PagerRefcount(Pager*);
14925#endif
14926SQLITE_PRIVATEstatic int sqlite3PagerMemUsed(Pager*);
14927SQLITE_PRIVATEstatic const char *sqlite3PagerFilename(const Pager*, int);
14928SQLITE_PRIVATEstatic sqlite3_vfs *sqlite3PagerVfs(Pager*);
14929SQLITE_PRIVATEstatic sqlite3_file *sqlite3PagerFile(Pager*);
14930SQLITE_PRIVATEstatic sqlite3_file *sqlite3PagerJrnlFile(Pager*);
14931SQLITE_PRIVATEstatic const char *sqlite3PagerJournalname(Pager*);
14932SQLITE_PRIVATEstatic void *sqlite3PagerTempSpace(Pager*);
14933SQLITE_PRIVATEstatic int sqlite3PagerIsMemdb(Pager*);
14934SQLITE_PRIVATEstatic void sqlite3PagerCacheStat(Pager *, int, int, int *);
14935SQLITE_PRIVATEstatic void sqlite3PagerClearCache(Pager*);
14936SQLITE_PRIVATEstatic int sqlite3SectorSize(sqlite3_file *);
14937
14938/* Functions used to truncate the database file. */
14939SQLITE_PRIVATEstatic void sqlite3PagerTruncateImage(Pager*,Pgno);
14940
14941SQLITE_PRIVATEstatic void sqlite3PagerRekey(DbPage*, Pgno, u16);
14942
14943/* Functions to support testing and debugging. */
14944#if !defined(NDEBUG1) || defined(SQLITE_TEST)
14945SQLITE_PRIVATEstatic Pgno sqlite3PagerPagenumber(DbPage*);
14946SQLITE_PRIVATEstatic int sqlite3PagerIswriteable(DbPage*);
14947#endif
14948#ifdef SQLITE_TEST
14949SQLITE_PRIVATEstatic int *sqlite3PagerStats(Pager*);
14950SQLITE_PRIVATEstatic void sqlite3PagerRefdump(Pager*);
14951 void disable_simulated_io_errors(void);
14952 void enable_simulated_io_errors(void);
14953#else
14954# define disable_simulated_io_errors()
14955# define enable_simulated_io_errors()
14956#endif
14957
14958#endif /* SQLITE_PAGER_H */
14959
14960/************** End of pager.h ***********************************************/
14961/************** Continuing where we left off in sqliteInt.h ******************/
14962/************** Include btree.h in the middle of sqliteInt.h *****************/
14963/************** Begin file btree.h *******************************************/
14964/*
14965** 2001 September 15
14966**
14967** The author disclaims copyright to this source code. In place of
14968** a legal notice, here is a blessing:
14969**
14970** May you do good and not evil.
14971** May you find forgiveness for yourself and forgive others.
14972** May you share freely, never taking more than you give.
14973**
14974*************************************************************************
14975** This header file defines the interface that the sqlite B-Tree file
14976** subsystem. See comments in the source code for a detailed description
14977** of what each interface routine does.
14978*/
14979#ifndef SQLITE_BTREE_H
14980#define SQLITE_BTREE_H
14981
14982/* TODO: This definition is just included so other modules compile. It
14983** needs to be revisited.
14984*/
14985#define SQLITE_N_BTREE_META16 16
14986
14987/*
14988** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
14989** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
14990*/
14991#ifndef SQLITE_DEFAULT_AUTOVACUUM0
14992 #define SQLITE_DEFAULT_AUTOVACUUM0 0
14993#endif
14994
14995#define BTREE_AUTOVACUUM_NONE0 0 /* Do not do auto-vacuum */
14996#define BTREE_AUTOVACUUM_FULL1 1 /* Do full auto-vacuum */
14997#define BTREE_AUTOVACUUM_INCR2 2 /* Incremental vacuum */
14998
14999/*
15000** Forward declarations of structure
15001*/
15002typedef struct Btree Btree;
15003typedef struct BtCursor BtCursor;
15004typedef struct BtShared BtShared;
15005typedef struct BtreePayload BtreePayload;
15006
15007
15008SQLITE_PRIVATEstatic int sqlite3BtreeOpen(
15009 sqlite3_vfs *pVfs, /* VFS to use with this b-tree */
15010 const char *zFilename, /* Name of database file to open */
15011 sqlite3 *db, /* Associated database connection */
15012 Btree **ppBtree, /* Return open Btree* here */
15013 int flags, /* Flags */
15014 int vfsFlags /* Flags passed through to VFS open */
15015);
15016
15017/* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
15018** following values.
15019**
15020** NOTE: These values must match the corresponding PAGER_ values in
15021** pager.h.
15022*/
15023#define BTREE_OMIT_JOURNAL1 1 /* Do not create or use a rollback journal */
15024#define BTREE_MEMORY2 2 /* This is an in-memory DB */
15025#define BTREE_SINGLE4 4 /* The file contains at most 1 b-tree */
15026#define BTREE_UNORDERED8 8 /* Use of a hash implementation is OK */
15027
15028SQLITE_PRIVATEstatic int sqlite3BtreeClose(Btree*);
15029SQLITE_PRIVATEstatic int sqlite3BtreeSetCacheSize(Btree*,int);
15030SQLITE_PRIVATEstatic int sqlite3BtreeSetSpillSize(Btree*,int);
15031#if SQLITE_MAX_MMAP_SIZE20971520>0
15032SQLITE_PRIVATEstatic int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
15033#endif
15034SQLITE_PRIVATEstatic int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
15035SQLITE_PRIVATEstatic int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
15036SQLITE_PRIVATEstatic int sqlite3BtreeGetPageSize(Btree*);
15037SQLITE_PRIVATEstatic Pgno sqlite3BtreeMaxPageCount(Btree*,Pgno);
15038SQLITE_PRIVATEstatic Pgno sqlite3BtreeLastPage(Btree*);
15039SQLITE_PRIVATEstatic int sqlite3BtreeSecureDelete(Btree*,int);
15040SQLITE_PRIVATEstatic int sqlite3BtreeGetRequestedReserve(Btree*);
15041SQLITE_PRIVATEstatic int sqlite3BtreeGetReserveNoMutex(Btree *p);
15042SQLITE_PRIVATEstatic int sqlite3BtreeSetAutoVacuum(Btree *, int);
15043SQLITE_PRIVATEstatic int sqlite3BtreeGetAutoVacuum(Btree *);
15044SQLITE_PRIVATEstatic int sqlite3BtreeBeginTrans(Btree*,int,int*);
15045SQLITE_PRIVATEstatic int sqlite3BtreeCommitPhaseOne(Btree*, const char*);
15046SQLITE_PRIVATEstatic int sqlite3BtreeCommitPhaseTwo(Btree*, int);
15047SQLITE_PRIVATEstatic int sqlite3BtreeCommit(Btree*);
15048SQLITE_PRIVATEstatic int sqlite3BtreeRollback(Btree*,int,int);
15049SQLITE_PRIVATEstatic int sqlite3BtreeBeginStmt(Btree*,int);
15050SQLITE_PRIVATEstatic int sqlite3BtreeCreateTable(Btree*, Pgno*, int flags);
15051SQLITE_PRIVATEstatic int sqlite3BtreeTxnState(Btree*);
15052SQLITE_PRIVATEstatic int sqlite3BtreeIsInBackup(Btree*);
15053
15054SQLITE_PRIVATEstatic void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
15055SQLITE_PRIVATEstatic int sqlite3BtreeSchemaLocked(Btree *pBtree);
15056#ifndef SQLITE_OMIT_SHARED_CACHE
15057SQLITE_PRIVATEstatic int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
15058#endif
15059
15060/* Savepoints are named, nestable SQL transactions mostly implemented */
15061/* in vdbe.c and pager.c See https://sqlite.org/lang_savepoint.html */
15062SQLITE_PRIVATEstatic int sqlite3BtreeSavepoint(Btree *, int, int);
15063
15064/* "Checkpoint" only refers to WAL. See https://sqlite.org/wal.html#ckpt */
15065#ifndef SQLITE_OMIT_WAL
15066SQLITE_PRIVATEstatic int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
15067#endif
15068
15069SQLITE_PRIVATEstatic const char *sqlite3BtreeGetFilename(Btree *);
15070SQLITE_PRIVATEstatic const char *sqlite3BtreeGetJournalname(Btree *);
15071SQLITE_PRIVATEstatic int sqlite3BtreeCopyFile(Btree *, Btree *);
15072
15073SQLITE_PRIVATEstatic int sqlite3BtreeIncrVacuum(Btree *);
15074
15075/* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
15076** of the flags shown below.
15077**
15078** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
15079** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
15080** is stored in the leaves. (BTREE_INTKEY is used for SQL tables.) With
15081** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
15082** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL
15083** indices.)
15084*/
15085#define BTREE_INTKEY1 1 /* Table has only 64-bit signed integer keys */
15086#define BTREE_BLOBKEY2 2 /* Table has keys only - no data */
15087
15088SQLITE_PRIVATEstatic int sqlite3BtreeDropTable(Btree*, int, int*);
15089SQLITE_PRIVATEstatic int sqlite3BtreeClearTable(Btree*, int, i64*);
15090SQLITE_PRIVATEstatic int sqlite3BtreeClearTableOfCursor(BtCursor*);
15091SQLITE_PRIVATEstatic int sqlite3BtreeTripAllCursors(Btree*, int, int);
15092
15093SQLITE_PRIVATEstatic void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
15094SQLITE_PRIVATEstatic int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
15095
15096SQLITE_PRIVATEstatic int sqlite3BtreeNewDb(Btree *p);
15097
15098/*
15099** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
15100** should be one of the following values. The integer values are assigned
15101** to constants so that the offset of the corresponding field in an
15102** SQLite database header may be found using the following formula:
15103**
15104** offset = 36 + (idx * 4)
15105**
15106** For example, the free-page-count field is located at byte offset 36 of
15107** the database file header. The incr-vacuum-flag field is located at
15108** byte offset 64 (== 36+4*7).
15109**
15110** The BTREE_DATA_VERSION value is not really a value stored in the header.
15111** It is a read-only number computed by the pager. But we merge it with
15112** the header value access routines since its access pattern is the same.
15113** Call it a "virtual meta value".
15114*/
15115#define BTREE_FREE_PAGE_COUNT0 0
15116#define BTREE_SCHEMA_VERSION1 1
15117#define BTREE_FILE_FORMAT2 2
15118#define BTREE_DEFAULT_CACHE_SIZE3 3
15119#define BTREE_LARGEST_ROOT_PAGE4 4
15120#define BTREE_TEXT_ENCODING5 5
15121#define BTREE_USER_VERSION6 6
15122#define BTREE_INCR_VACUUM7 7
15123#define BTREE_APPLICATION_ID8 8
15124#define BTREE_DATA_VERSION15 15 /* A virtual meta-value */
15125
15126/*
15127** Kinds of hints that can be passed into the sqlite3BtreeCursorHint()
15128** interface.
15129**
15130** BTREE_HINT_RANGE (arguments: Expr*, Mem*)
15131**
15132** The first argument is an Expr* (which is guaranteed to be constant for
15133** the lifetime of the cursor) that defines constraints on which rows
15134** might be fetched with this cursor. The Expr* tree may contain
15135** TK_REGISTER nodes that refer to values stored in the array of registers
15136** passed as the second parameter. In other words, if Expr.op==TK_REGISTER
15137** then the value of the node is the value in Mem[pExpr.iTable]. Any
15138** TK_COLUMN node in the expression tree refers to the Expr.iColumn-th
15139** column of the b-tree of the cursor. The Expr tree will not contain
15140** any function calls nor subqueries nor references to b-trees other than
15141** the cursor being hinted.
15142**
15143** The design of the _RANGE hint is aid b-tree implementations that try
15144** to prefetch content from remote machines - to provide those
15145** implementations with limits on what needs to be prefetched and thereby
15146** reduce network bandwidth.
15147**
15148** Note that BTREE_HINT_FLAGS with BTREE_BULKLOAD is the only hint used by
15149** standard SQLite. The other hints are provided for extentions that use
15150** the SQLite parser and code generator but substitute their own storage
15151** engine.
15152*/
15153#define BTREE_HINT_RANGE0 0 /* Range constraints on queries */
15154
15155/*
15156** Values that may be OR'd together to form the argument to the
15157** BTREE_HINT_FLAGS hint for sqlite3BtreeCursorHint():
15158**
15159** The BTREE_BULKLOAD flag is set on index cursors when the index is going
15160** to be filled with content that is already in sorted order.
15161**
15162** The BTREE_SEEK_EQ flag is set on cursors that will get OP_SeekGE or
15163** OP_SeekLE opcodes for a range search, but where the range of entries
15164** selected will all have the same key. In other words, the cursor will
15165** be used only for equality key searches.
15166**
15167*/
15168#define BTREE_BULKLOAD0x00000001 0x00000001 /* Used to full index in sorted order */
15169#define BTREE_SEEK_EQ0x00000002 0x00000002 /* EQ seeks only - no range seeks */
15170
15171/*
15172** Flags passed as the third argument to sqlite3BtreeCursor().
15173**
15174** For read-only cursors the wrFlag argument is always zero. For read-write
15175** cursors it may be set to either (BTREE_WRCSR|BTREE_FORDELETE) or just
15176** (BTREE_WRCSR). If the BTREE_FORDELETE bit is set, then the cursor will
15177** only be used by SQLite for the following:
15178**
15179** * to seek to and then delete specific entries, and/or
15180**
15181** * to read values that will be used to create keys that other
15182** BTREE_FORDELETE cursors will seek to and delete.
15183**
15184** The BTREE_FORDELETE flag is an optimization hint. It is not used by
15185** by this, the native b-tree engine of SQLite, but it is available to
15186** alternative storage engines that might be substituted in place of this
15187** b-tree system. For alternative storage engines in which a delete of
15188** the main table row automatically deletes corresponding index rows,
15189** the FORDELETE flag hint allows those alternative storage engines to
15190** skip a lot of work. Namely: FORDELETE cursors may treat all SEEK
15191** and DELETE operations as no-ops, and any READ operation against a
15192** FORDELETE cursor may return a null row: 0x01 0x00.
15193*/
15194#define BTREE_WRCSR0x00000004 0x00000004 /* read-write cursor */
15195#define BTREE_FORDELETE0x00000008 0x00000008 /* Cursor is for seek/delete only */
15196
15197SQLITE_PRIVATEstatic int sqlite3BtreeCursor(
15198 Btree*, /* BTree containing table to open */
15199 Pgno iTable, /* Index of root page */
15200 int wrFlag, /* 1 for writing. 0 for read-only */
15201 struct KeyInfo*, /* First argument to compare function */
15202 BtCursor *pCursor /* Space to write cursor structure */
15203);
15204SQLITE_PRIVATEstatic BtCursor *sqlite3BtreeFakeValidCursor(void);
15205SQLITE_PRIVATEstatic int sqlite3BtreeCursorSize(void);
15206SQLITE_PRIVATEstatic void sqlite3BtreeCursorZero(BtCursor*);
15207SQLITE_PRIVATEstatic void sqlite3BtreeCursorHintFlags(BtCursor*, unsigned);
15208#ifdef SQLITE_ENABLE_CURSOR_HINTS
15209SQLITE_PRIVATEstatic void sqlite3BtreeCursorHint(BtCursor*, int, ...);
15210#endif
15211
15212SQLITE_PRIVATEstatic int sqlite3BtreeCloseCursor(BtCursor*);
15213SQLITE_PRIVATEstatic int sqlite3BtreeTableMoveto(
15214 BtCursor*,
15215 i64 intKey,
15216 int bias,
15217 int *pRes
15218);
15219SQLITE_PRIVATEstatic int sqlite3BtreeIndexMoveto(
15220 BtCursor*,
15221 UnpackedRecord *pUnKey,
15222 int *pRes
15223);
15224SQLITE_PRIVATEstatic int sqlite3BtreeCursorHasMoved(BtCursor*);
15225SQLITE_PRIVATEstatic int sqlite3BtreeCursorRestore(BtCursor*, int*);
15226SQLITE_PRIVATEstatic int sqlite3BtreeDelete(BtCursor*, u8 flags);
15227
15228/* Allowed flags for sqlite3BtreeDelete() and sqlite3BtreeInsert() */
15229#define BTREE_SAVEPOSITION0x02 0x02 /* Leave cursor pointing at NEXT or PREV */
15230#define BTREE_AUXDELETE0x04 0x04 /* not the primary delete operation */
15231#define BTREE_APPEND0x08 0x08 /* Insert is likely an append */
15232#define BTREE_PREFORMAT0x80 0x80 /* Inserted data is a preformated cell */
15233
15234/* An instance of the BtreePayload object describes the content of a single
15235** entry in either an index or table btree.
15236**
15237** Index btrees (used for indexes and also WITHOUT ROWID tables) contain
15238** an arbitrary key and no data. These btrees have pKey,nKey set to the
15239** key and the pData,nData,nZero fields are uninitialized. The aMem,nMem
15240** fields give an array of Mem objects that are a decomposition of the key.
15241** The nMem field might be zero, indicating that no decomposition is available.
15242**
15243** Table btrees (used for rowid tables) contain an integer rowid used as
15244** the key and passed in the nKey field. The pKey field is zero.
15245** pData,nData hold the content of the new entry. nZero extra zero bytes
15246** are appended to the end of the content when constructing the entry.
15247** The aMem,nMem fields are uninitialized for table btrees.
15248**
15249** Field usage summary:
15250**
15251** Table BTrees Index Btrees
15252**
15253** pKey always NULL encoded key
15254** nKey the ROWID length of pKey
15255** pData data not used
15256** aMem not used decomposed key value
15257** nMem not used entries in aMem
15258** nData length of pData not used
15259** nZero extra zeros after pData not used
15260**
15261** This object is used to pass information into sqlite3BtreeInsert(). The
15262** same information used to be passed as five separate parameters. But placing
15263** the information into this object helps to keep the interface more
15264** organized and understandable, and it also helps the resulting code to
15265** run a little faster by using fewer registers for parameter passing.
15266*/
15267struct BtreePayload {
15268 const void *pKey; /* Key content for indexes. NULL for tables */
15269 sqlite3_int64 nKey; /* Size of pKey for indexes. PRIMARY KEY for tabs */
15270 const void *pData; /* Data for tables. */
15271 sqlite3_value *aMem; /* First of nMem value in the unpacked pKey */
15272 u16 nMem; /* Number of aMem[] value. Might be zero */
15273 int nData; /* Size of pData. 0 if none. */
15274 int nZero; /* Extra zero data appended after pData,nData */
15275};
15276
15277SQLITE_PRIVATEstatic int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
15278 int flags, int seekResult);
15279SQLITE_PRIVATEstatic int sqlite3BtreeFirst(BtCursor*, int *pRes);
15280SQLITE_PRIVATEstatic int sqlite3BtreeLast(BtCursor*, int *pRes);
15281SQLITE_PRIVATEstatic int sqlite3BtreeNext(BtCursor*, int flags);
15282SQLITE_PRIVATEstatic int sqlite3BtreeEof(BtCursor*);
15283SQLITE_PRIVATEstatic int sqlite3BtreePrevious(BtCursor*, int flags);
15284SQLITE_PRIVATEstatic i64 sqlite3BtreeIntegerKey(BtCursor*);
15285SQLITE_PRIVATEstatic void sqlite3BtreeCursorPin(BtCursor*);
15286SQLITE_PRIVATEstatic void sqlite3BtreeCursorUnpin(BtCursor*);
15287#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
15288SQLITE_PRIVATEstatic i64 sqlite3BtreeOffset(BtCursor*);
15289#endif
15290SQLITE_PRIVATEstatic int sqlite3BtreePayload(BtCursor*, u32 offset, u32 amt, void*);
15291SQLITE_PRIVATEstatic const void *sqlite3BtreePayloadFetch(BtCursor*, u32 *pAmt);
15292SQLITE_PRIVATEstatic u32 sqlite3BtreePayloadSize(BtCursor*);
15293SQLITE_PRIVATEstatic sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor*);
15294
15295SQLITE_PRIVATEstatic char *sqlite3BtreeIntegrityCheck(sqlite3*,Btree*,Pgno*aRoot,int nRoot,int,int*);
15296SQLITE_PRIVATEstatic struct Pager *sqlite3BtreePager(Btree*);
15297SQLITE_PRIVATEstatic i64 sqlite3BtreeRowCountEst(BtCursor*);
15298
15299#ifndef SQLITE_OMIT_INCRBLOB
15300SQLITE_PRIVATEstatic int sqlite3BtreePayloadChecked(BtCursor*, u32 offset, u32 amt, void*);
15301SQLITE_PRIVATEstatic int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
15302SQLITE_PRIVATEstatic void sqlite3BtreeIncrblobCursor(BtCursor *);
15303#endif
15304SQLITE_PRIVATEstatic void sqlite3BtreeClearCursor(BtCursor *);
15305SQLITE_PRIVATEstatic int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
15306SQLITE_PRIVATEstatic int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask);
15307SQLITE_PRIVATEstatic int sqlite3BtreeIsReadonly(Btree *pBt);
15308SQLITE_PRIVATEstatic int sqlite3HeaderSizeBtree(void);
15309
15310#ifdef SQLITE_DEBUG
15311SQLITE_PRIVATEstatic sqlite3_uint64 sqlite3BtreeSeekCount(Btree*)0;
15312#else
15313# define sqlite3BtreeSeekCount(X)0 0
15314#endif
15315
15316#ifndef NDEBUG1
15317SQLITE_PRIVATEstatic int sqlite3BtreeCursorIsValid(BtCursor*);
15318#endif
15319SQLITE_PRIVATEstatic int sqlite3BtreeCursorIsValidNN(BtCursor*);
15320
15321SQLITE_PRIVATEstatic int sqlite3BtreeCount(sqlite3*, BtCursor*, i64*);
15322
15323#ifdef SQLITE_TEST
15324SQLITE_PRIVATEstatic int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
15325SQLITE_PRIVATEstatic void sqlite3BtreeCursorList(Btree*);
15326#endif
15327
15328#ifndef SQLITE_OMIT_WAL
15329SQLITE_PRIVATEstatic int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
15330#endif
15331
15332SQLITE_PRIVATEstatic int sqlite3BtreeTransferRow(BtCursor*, BtCursor*, i64);
15333
15334/*
15335** If we are not using shared cache, then there is no need to
15336** use mutexes to access the BtShared structures. So make the
15337** Enter and Leave procedures no-ops.
15338*/
15339#ifndef SQLITE_OMIT_SHARED_CACHE
15340SQLITE_PRIVATEstatic void sqlite3BtreeEnter(Btree*);
15341SQLITE_PRIVATEstatic void sqlite3BtreeEnterAll(sqlite3*);
15342SQLITE_PRIVATEstatic int sqlite3BtreeSharable(Btree*);
15343SQLITE_PRIVATEstatic void sqlite3BtreeEnterCursor(BtCursor*);
15344SQLITE_PRIVATEstatic int sqlite3BtreeConnectionCount(Btree*);
15345#else
15346# define sqlite3BtreeEnter(X)
15347# define sqlite3BtreeEnterAll(X)
15348# define sqlite3BtreeSharable(X) 0
15349# define sqlite3BtreeEnterCursor(X)
15350# define sqlite3BtreeConnectionCount(X) 1
15351#endif
15352
15353#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE2
15354SQLITE_PRIVATEstatic void sqlite3BtreeLeave(Btree*);
15355SQLITE_PRIVATEstatic void sqlite3BtreeLeaveCursor(BtCursor*);
15356SQLITE_PRIVATEstatic void sqlite3BtreeLeaveAll(sqlite3*);
15357#ifndef NDEBUG1
15358 /* These routines are used inside assert() statements only. */
15359SQLITE_PRIVATEstatic int sqlite3BtreeHoldsMutex(Btree*);
15360SQLITE_PRIVATEstatic int sqlite3BtreeHoldsAllMutexes(sqlite3*);
15361SQLITE_PRIVATEstatic int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
15362#endif
15363#else
15364
15365# define sqlite3BtreeLeave(X)
15366# define sqlite3BtreeLeaveCursor(X)
15367# define sqlite3BtreeLeaveAll(X)
15368
15369# define sqlite3BtreeHoldsMutex(X) 1
15370# define sqlite3BtreeHoldsAllMutexes(X) 1
15371# define sqlite3SchemaMutexHeld(X,Y,Z) 1
15372#endif
15373
15374
15375#endif /* SQLITE_BTREE_H */
15376
15377/************** End of btree.h ***********************************************/
15378/************** Continuing where we left off in sqliteInt.h ******************/
15379/************** Include vdbe.h in the middle of sqliteInt.h ******************/
15380/************** Begin file vdbe.h ********************************************/
15381/*
15382** 2001 September 15
15383**
15384** The author disclaims copyright to this source code. In place of
15385** a legal notice, here is a blessing:
15386**
15387** May you do good and not evil.
15388** May you find forgiveness for yourself and forgive others.
15389** May you share freely, never taking more than you give.
15390**
15391*************************************************************************
15392** Header file for the Virtual DataBase Engine (VDBE)
15393**
15394** This header defines the interface to the virtual database engine
15395** or VDBE. The VDBE implements an abstract machine that runs a
15396** simple program to access and modify the underlying database.
15397*/
15398#ifndef SQLITE_VDBE_H
15399#define SQLITE_VDBE_H
15400/* #include <stdio.h> */
15401
15402/*
15403** A single VDBE is an opaque structure named "Vdbe". Only routines
15404** in the source file sqliteVdbe.c are allowed to see the insides
15405** of this structure.
15406*/
15407typedef struct Vdbe Vdbe;
15408
15409/*
15410** The names of the following types declared in vdbeInt.h are required
15411** for the VdbeOp definition.
15412*/
15413typedef struct sqlite3_value Mem;
15414typedef struct SubProgram SubProgram;
15415
15416/*
15417** A single instruction of the virtual machine has an opcode
15418** and as many as three operands. The instruction is recorded
15419** as an instance of the following structure:
15420*/
15421struct VdbeOp {
15422 u8 opcode; /* What operation to perform */
15423 signed char p4type; /* One of the P4_xxx constants for p4 */
15424 u16 p5; /* Fifth parameter is an unsigned 16-bit integer */
15425 int p1; /* First operand */
15426 int p2; /* Second parameter (often the jump destination) */
15427 int p3; /* The third parameter */
15428 union p4union { /* fourth parameter */
15429 int i; /* Integer value if p4type==P4_INT32 */
15430 void *p; /* Generic pointer */
15431 char *z; /* Pointer to data for string (char array) types */
15432 i64 *pI64; /* Used when p4type is P4_INT64 */
15433 double *pReal; /* Used when p4type is P4_REAL */
15434 FuncDef *pFunc; /* Used when p4type is P4_FUNCDEF */
15435 sqlite3_context *pCtx; /* Used when p4type is P4_FUNCCTX */
15436 CollSeq *pColl; /* Used when p4type is P4_COLLSEQ */
15437 Mem *pMem; /* Used when p4type is P4_MEM */
15438 VTable *pVtab; /* Used when p4type is P4_VTAB */
15439 KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
15440 u32 *ai; /* Used when p4type is P4_INTARRAY */
15441 SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */
15442 Table *pTab; /* Used when p4type is P4_TABLE */
15443#ifdef SQLITE_ENABLE_CURSOR_HINTS
15444 Expr *pExpr; /* Used when p4type is P4_EXPR */
15445#endif
15446 } p4;
15447#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
15448 char *zComment; /* Comment to improve readability */
15449#endif
15450#ifdef VDBE_PROFILE
15451 u32 cnt; /* Number of times this instruction was executed */
15452 u64 cycles; /* Total time spent executing this instruction */
15453#endif
15454#ifdef SQLITE_VDBE_COVERAGE
15455 u32 iSrcLine; /* Source-code line that generated this opcode
15456 ** with flags in the upper 8 bits */
15457#endif
15458};
15459typedef struct VdbeOp VdbeOp;
15460
15461
15462/*
15463** A sub-routine used to implement a trigger program.
15464*/
15465struct SubProgram {
15466 VdbeOp *aOp; /* Array of opcodes for sub-program */
15467 int nOp; /* Elements in aOp[] */
15468 int nMem; /* Number of memory cells required */
15469 int nCsr; /* Number of cursors required */
15470 u8 *aOnce; /* Array of OP_Once flags */
15471 void *token; /* id that may be used to recursive triggers */
15472 SubProgram *pNext; /* Next sub-program already visited */
15473};
15474
15475/*
15476** A smaller version of VdbeOp used for the VdbeAddOpList() function because
15477** it takes up less space.
15478*/
15479struct VdbeOpList {
15480 u8 opcode; /* What operation to perform */
15481 signed char p1; /* First operand */
15482 signed char p2; /* Second parameter (often the jump destination) */
15483 signed char p3; /* Third parameter */
15484};
15485typedef struct VdbeOpList VdbeOpList;
15486
15487/*
15488** Allowed values of VdbeOp.p4type
15489*/
15490#define P4_NOTUSED0 0 /* The P4 parameter is not used */
15491#define P4_TRANSIENT0 0 /* P4 is a pointer to a transient string */
15492#define P4_STATIC(-1) (-1) /* Pointer to a static string */
15493#define P4_COLLSEQ(-2) (-2) /* P4 is a pointer to a CollSeq structure */
15494#define P4_INT32(-3) (-3) /* P4 is a 32-bit signed integer */
15495#define P4_SUBPROGRAM(-4) (-4) /* P4 is a pointer to a SubProgram structure */
15496#define P4_TABLE(-5) (-5) /* P4 is a pointer to a Table structure */
15497/* Above do not own any resources. Must free those below */
15498#define P4_FREE_IF_LE(-6) (-6)
15499#define P4_DYNAMIC(-6) (-6) /* Pointer to memory from sqliteMalloc() */
15500#define P4_FUNCDEF(-7) (-7) /* P4 is a pointer to a FuncDef structure */
15501#define P4_KEYINFO(-8) (-8) /* P4 is a pointer to a KeyInfo structure */
15502#define P4_EXPR(-9) (-9) /* P4 is a pointer to an Expr tree */
15503#define P4_MEM(-10) (-10) /* P4 is a pointer to a Mem* structure */
15504#define P4_VTAB(-11) (-11) /* P4 is a pointer to an sqlite3_vtab structure */
15505#define P4_REAL(-12) (-12) /* P4 is a 64-bit floating point value */
15506#define P4_INT64(-13) (-13) /* P4 is a 64-bit signed integer */
15507#define P4_INTARRAY(-14) (-14) /* P4 is a vector of 32-bit integers */
15508#define P4_FUNCCTX(-15) (-15) /* P4 is a pointer to an sqlite3_context object */
15509
15510/* Error message codes for OP_Halt */
15511#define P5_ConstraintNotNull1 1
15512#define P5_ConstraintUnique2 2
15513#define P5_ConstraintCheck3 3
15514#define P5_ConstraintFK4 4
15515
15516/*
15517** The Vdbe.aColName array contains 5n Mem structures, where n is the
15518** number of columns of data returned by the statement.
15519*/
15520#define COLNAME_NAME0 0
15521#define COLNAME_DECLTYPE1 1
15522#define COLNAME_DATABASE2 2
15523#define COLNAME_TABLE3 3
15524#define COLNAME_COLUMN4 4
15525#ifdef SQLITE_ENABLE_COLUMN_METADATA1
15526# define COLNAME_N5 5 /* Number of COLNAME_xxx symbols */
15527#else
15528# ifdef SQLITE_OMIT_DECLTYPE
15529# define COLNAME_N5 1 /* Store only the name */
15530# else
15531# define COLNAME_N5 2 /* Store the name and decltype */
15532# endif
15533#endif
15534
15535/*
15536** The following macro converts a label returned by sqlite3VdbeMakeLabel()
15537** into an index into the Parse.aLabel[] array that contains the resolved
15538** address of that label.
15539*/
15540#define ADDR(X)(~(X)) (~(X))
15541
15542/*
15543** The makefile scans the vdbe.c source file and creates the "opcodes.h"
15544** header file that defines a number for each opcode used by the VDBE.
15545*/
15546/************** Include opcodes.h in the middle of vdbe.h ********************/
15547/************** Begin file opcodes.h *****************************************/
15548/* Automatically generated. Do not edit */
15549/* See the tool/mkopcodeh.tcl script for details */
15550#define OP_Savepoint0 0
15551#define OP_AutoCommit1 1
15552#define OP_Transaction2 2
15553#define OP_Checkpoint3 3
15554#define OP_JournalMode4 4
15555#define OP_Vacuum5 5
15556#define OP_VFilter6 6 /* jump, synopsis: iplan=r[P3] zplan='P4' */
15557#define OP_VUpdate7 7 /* synopsis: data=r[P3@P2] */
15558#define OP_Goto8 8 /* jump */
15559#define OP_Gosub9 9 /* jump */
15560#define OP_InitCoroutine10 10 /* jump */
15561#define OP_Yield11 11 /* jump */
15562#define OP_MustBeInt12 12 /* jump */
15563#define OP_Jump13 13 /* jump */
15564#define OP_Once14 14 /* jump */
15565#define OP_If15 15 /* jump */
15566#define OP_IfNot16 16 /* jump */
15567#define OP_IsNullOrType17 17 /* jump, synopsis: if typeof(r[P1]) IN (P3,5) goto P2 */
15568#define OP_IfNullRow18 18 /* jump, synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */
15569#define OP_Not19 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */
15570#define OP_SeekLT20 20 /* jump, synopsis: key=r[P3@P4] */
15571#define OP_SeekLE21 21 /* jump, synopsis: key=r[P3@P4] */
15572#define OP_SeekGE22 22 /* jump, synopsis: key=r[P3@P4] */
15573#define OP_SeekGT23 23 /* jump, synopsis: key=r[P3@P4] */
15574#define OP_IfNotOpen24 24 /* jump, synopsis: if( !csr[P1] ) goto P2 */
15575#define OP_IfNoHope25 25 /* jump, synopsis: key=r[P3@P4] */
15576#define OP_NoConflict26 26 /* jump, synopsis: key=r[P3@P4] */
15577#define OP_NotFound27 27 /* jump, synopsis: key=r[P3@P4] */
15578#define OP_Found28 28 /* jump, synopsis: key=r[P3@P4] */
15579#define OP_SeekRowid29 29 /* jump, synopsis: intkey=r[P3] */
15580#define OP_NotExists30 30 /* jump, synopsis: intkey=r[P3] */
15581#define OP_Last31 31 /* jump */
15582#define OP_IfSmaller32 32 /* jump */
15583#define OP_SorterSort33 33 /* jump */
15584#define OP_Sort34 34 /* jump */
15585#define OP_Rewind35 35 /* jump */
15586#define OP_SorterNext36 36 /* jump */
15587#define OP_Prev37 37 /* jump */
15588#define OP_Next38 38 /* jump */
15589#define OP_IdxLE39 39 /* jump, synopsis: key=r[P3@P4] */
15590#define OP_IdxGT40 40 /* jump, synopsis: key=r[P3@P4] */
15591#define OP_IdxLT41 41 /* jump, synopsis: key=r[P3@P4] */
15592#define OP_IdxGE42 42 /* jump, synopsis: key=r[P3@P4] */
15593#define OP_Or43 43 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
15594#define OP_And44 44 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
15595#define OP_RowSetRead45 45 /* jump, synopsis: r[P3]=rowset(P1) */
15596#define OP_RowSetTest46 46 /* jump, synopsis: if r[P3] in rowset(P1) goto P2 */
15597#define OP_Program47 47 /* jump */
15598#define OP_FkIfZero48 48 /* jump, synopsis: if fkctr[P1]==0 goto P2 */
15599#define OP_IfPos49 49 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
15600#define OP_IsNull50 50 /* jump, same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
15601#define OP_NotNull51 51 /* jump, same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
15602#define OP_Ne52 52 /* jump, same as TK_NE, synopsis: IF r[P3]!=r[P1] */
15603#define OP_Eq53 53 /* jump, same as TK_EQ, synopsis: IF r[P3]==r[P1] */
15604#define OP_Gt54 54 /* jump, same as TK_GT, synopsis: IF r[P3]>r[P1] */
15605#define OP_Le55 55 /* jump, same as TK_LE, synopsis: IF r[P3]<=r[P1] */
15606#define OP_Lt56 56 /* jump, same as TK_LT, synopsis: IF r[P3]<r[P1] */
15607#define OP_Ge57 57 /* jump, same as TK_GE, synopsis: IF r[P3]>=r[P1] */
15608#define OP_ElseEq58 58 /* jump, same as TK_ESCAPE */
15609#define OP_IfNotZero59 59 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
15610#define OP_DecrJumpZero60 60 /* jump, synopsis: if (--r[P1])==0 goto P2 */
15611#define OP_IncrVacuum61 61 /* jump */
15612#define OP_VNext62 62 /* jump */
15613#define OP_Filter63 63 /* jump, synopsis: if key(P3@P4) not in filter(P1) goto P2 */
15614#define OP_Init64 64 /* jump, synopsis: Start at P2 */
15615#define OP_PureFunc65 65 /* synopsis: r[P3]=func(r[P2@NP]) */
15616#define OP_Function66 66 /* synopsis: r[P3]=func(r[P2@NP]) */
15617#define OP_Return67 67
15618#define OP_EndCoroutine68 68
15619#define OP_HaltIfNull69 69 /* synopsis: if r[P3]=null halt */
15620#define OP_Halt70 70
15621#define OP_Integer71 71 /* synopsis: r[P2]=P1 */
15622#define OP_Int6472 72 /* synopsis: r[P2]=P4 */
15623#define OP_String73 73 /* synopsis: r[P2]='P4' (len=P1) */
15624#define OP_BeginSubrtn74 74 /* synopsis: r[P2]=NULL */
15625#define OP_Null75 75 /* synopsis: r[P2..P3]=NULL */
15626#define OP_SoftNull76 76 /* synopsis: r[P1]=NULL */
15627#define OP_Blob77 77 /* synopsis: r[P2]=P4 (len=P1) */
15628#define OP_Variable78 78 /* synopsis: r[P2]=parameter(P1,P4) */
15629#define OP_Move79 79 /* synopsis: r[P2@P3]=r[P1@P3] */
15630#define OP_Copy80 80 /* synopsis: r[P2@P3+1]=r[P1@P3+1] */
15631#define OP_SCopy81 81 /* synopsis: r[P2]=r[P1] */
15632#define OP_IntCopy82 82 /* synopsis: r[P2]=r[P1] */
15633#define OP_FkCheck83 83
15634#define OP_ResultRow84 84 /* synopsis: output=r[P1@P2] */
15635#define OP_CollSeq85 85
15636#define OP_AddImm86 86 /* synopsis: r[P1]=r[P1]+P2 */
15637#define OP_RealAffinity87 87
15638#define OP_Cast88 88 /* synopsis: affinity(r[P1]) */
15639#define OP_Permutation89 89
15640#define OP_Compare90 90 /* synopsis: r[P1@P3] <-> r[P2@P3] */
15641#define OP_IsTrue91 91 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */
15642#define OP_ZeroOrNull92 92 /* synopsis: r[P2] = 0 OR NULL */
15643#define OP_Offset93 93 /* synopsis: r[P3] = sqlite_offset(P1) */
15644#define OP_Column94 94 /* synopsis: r[P3]=PX cursor P1 column P2 */
15645#define OP_TypeCheck95 95 /* synopsis: typecheck(r[P1@P2]) */
15646#define OP_Affinity96 96 /* synopsis: affinity(r[P1@P2]) */
15647#define OP_MakeRecord97 97 /* synopsis: r[P3]=mkrec(r[P1@P2]) */
15648#define OP_Count98 98 /* synopsis: r[P2]=count() */
15649#define OP_ReadCookie99 99
15650#define OP_SetCookie100 100
15651#define OP_ReopenIdx101 101 /* synopsis: root=P2 iDb=P3 */
15652#define OP_BitAnd102 102 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
15653#define OP_BitOr103 103 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
15654#define OP_ShiftLeft104 104 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
15655#define OP_ShiftRight105 105 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
15656#define OP_Add106 106 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
15657#define OP_Subtract107 107 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
15658#define OP_Multiply108 108 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
15659#define OP_Divide109 109 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
15660#define OP_Remainder110 110 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
15661#define OP_Concat111 111 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
15662#define OP_OpenRead112 112 /* synopsis: root=P2 iDb=P3 */
15663#define OP_OpenWrite113 113 /* synopsis: root=P2 iDb=P3 */
15664#define OP_BitNot114 114 /* same as TK_BITNOT, synopsis: r[P2]= ~r[P1] */
15665#define OP_OpenDup115 115
15666#define OP_OpenAutoindex116 116 /* synopsis: nColumn=P2 */
15667#define OP_String8117 117 /* same as TK_STRING, synopsis: r[P2]='P4' */
15668#define OP_OpenEphemeral118 118 /* synopsis: nColumn=P2 */
15669#define OP_SorterOpen119 119
15670#define OP_SequenceTest120 120 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */
15671#define OP_OpenPseudo121 121 /* synopsis: P3 columns in r[P2] */
15672#define OP_Close122 122
15673#define OP_ColumnsUsed123 123
15674#define OP_SeekScan124 124 /* synopsis: Scan-ahead up to P1 rows */
15675#define OP_SeekHit125 125 /* synopsis: set P2<=seekHit<=P3 */
15676#define OP_Sequence126 126 /* synopsis: r[P2]=cursor[P1].ctr++ */
15677#define OP_NewRowid127 127 /* synopsis: r[P2]=rowid */
15678#define OP_Insert128 128 /* synopsis: intkey=r[P3] data=r[P2] */
15679#define OP_RowCell129 129
15680#define OP_Delete130 130
15681#define OP_ResetCount131 131
15682#define OP_SorterCompare132 132 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
15683#define OP_SorterData133 133 /* synopsis: r[P2]=data */
15684#define OP_RowData134 134 /* synopsis: r[P2]=data */
15685#define OP_Rowid135 135 /* synopsis: r[P2]=PX rowid of P1 */
15686#define OP_NullRow136 136
15687#define OP_SeekEnd137 137
15688#define OP_IdxInsert138 138 /* synopsis: key=r[P2] */
15689#define OP_SorterInsert139 139 /* synopsis: key=r[P2] */
15690#define OP_IdxDelete140 140 /* synopsis: key=r[P2@P3] */
15691#define OP_DeferredSeek141 141 /* synopsis: Move P3 to P1.rowid if needed */
15692#define OP_IdxRowid142 142 /* synopsis: r[P2]=rowid */
15693#define OP_FinishSeek143 143
15694#define OP_Destroy144 144
15695#define OP_Clear145 145
15696#define OP_ResetSorter146 146
15697#define OP_CreateBtree147 147 /* synopsis: r[P2]=root iDb=P1 flags=P3 */
15698#define OP_SqlExec148 148
15699#define OP_ParseSchema149 149
15700#define OP_LoadAnalysis150 150
15701#define OP_DropTable151 151
15702#define OP_DropIndex152 152
15703#define OP_Real153 153 /* same as TK_FLOAT, synopsis: r[P2]=P4 */
15704#define OP_DropTrigger154 154
15705#define OP_IntegrityCk155 155
15706#define OP_RowSetAdd156 156 /* synopsis: rowset(P1)=r[P2] */
15707#define OP_Param157 157
15708#define OP_FkCounter158 158 /* synopsis: fkctr[P1]+=P2 */
15709#define OP_MemMax159 159 /* synopsis: r[P1]=max(r[P1],r[P2]) */
15710#define OP_OffsetLimit160 160 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
15711#define OP_AggInverse161 161 /* synopsis: accum=r[P3] inverse(r[P2@P5]) */
15712#define OP_AggStep162 162 /* synopsis: accum=r[P3] step(r[P2@P5]) */
15713#define OP_AggStep1163 163 /* synopsis: accum=r[P3] step(r[P2@P5]) */
15714#define OP_AggValue164 164 /* synopsis: r[P3]=value N=P2 */
15715#define OP_AggFinal165 165 /* synopsis: accum=r[P1] N=P2 */
15716#define OP_Expire166 166
15717#define OP_CursorLock167 167
15718#define OP_CursorUnlock168 168
15719#define OP_TableLock169 169 /* synopsis: iDb=P1 root=P2 write=P3 */
15720#define OP_VBegin170 170
15721#define OP_VCreate171 171
15722#define OP_VDestroy172 172
15723#define OP_VOpen173 173
15724#define OP_VInitIn174 174 /* synopsis: r[P2]=ValueList(P1,P3) */
15725#define OP_VColumn175 175 /* synopsis: r[P3]=vcolumn(P2) */
15726#define OP_VRename176 176
15727#define OP_Pagecount177 177
15728#define OP_MaxPgcnt178 178
15729#define OP_ClrSubtype179 179 /* synopsis: r[P1].subtype = 0 */
15730#define OP_FilterAdd180 180 /* synopsis: filter(P1) += key(P3@P4) */
15731#define OP_Trace181 181
15732#define OP_CursorHint182 182
15733#define OP_ReleaseReg183 183 /* synopsis: release r[P1@P2] mask P3 */
15734#define OP_Noop184 184
15735#define OP_Explain185 185
15736#define OP_Abortable186 186
15737
15738/* Properties such as "out2" or "jump" that are specified in
15739** comments following the "case" for each opcode in the vdbe.c
15740** are encoded into bitvectors as follows:
15741*/
15742#define OPFLG_JUMP0x01 0x01 /* jump: P2 holds jmp target */
15743#define OPFLG_IN10x02 0x02 /* in1: P1 is an input */
15744#define OPFLG_IN20x04 0x04 /* in2: P2 is an input */
15745#define OPFLG_IN30x08 0x08 /* in3: P3 is an input */
15746#define OPFLG_OUT20x10 0x10 /* out2: P2 is an output */
15747#define OPFLG_OUT30x20 0x20 /* out3: P3 is an output */
15748#define OPFLG_INITIALIZER{ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00, 0x01, 0x01,
0x01, 0x03, 0x03, 0x01, 0x01, 0x03, 0x03, 0x03, 0x01, 0x12, 0x09
, 0x09, 0x09, 0x09, 0x01, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
, 0x01, 0x26, 0x26, 0x23, 0x0b, 0x01, 0x01, 0x03, 0x03, 0x03,
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x01, 0x03, 0x03, 0x01, 0x01
, 0x01, 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,
0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x26, 0x26, 0x26
, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00, 0x00, 0x12,
0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
0x00, 0x00, 0x04, 0x04, 0x00, 0x00, 0x10, 0x00, 0x10, 0x00, 0x00
, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x06,
0x10, 0x00, 0x04, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x10,
0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,}
{\
15749/* 0 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00,\
15750/* 8 */ 0x01, 0x01, 0x01, 0x03, 0x03, 0x01, 0x01, 0x03,\
15751/* 16 */ 0x03, 0x03, 0x01, 0x12, 0x09, 0x09, 0x09, 0x09,\
15752/* 24 */ 0x01, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x01,\
15753/* 32 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\
15754/* 40 */ 0x01, 0x01, 0x01, 0x26, 0x26, 0x23, 0x0b, 0x01,\
15755/* 48 */ 0x01, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
15756/* 56 */ 0x0b, 0x0b, 0x01, 0x03, 0x03, 0x01, 0x01, 0x01,\
15757/* 64 */ 0x01, 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10,\
15758/* 72 */ 0x10, 0x10, 0x00, 0x10, 0x00, 0x10, 0x10, 0x00,\
15759/* 80 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x02, 0x02,\
15760/* 88 */ 0x02, 0x00, 0x00, 0x12, 0x1e, 0x20, 0x00, 0x00,\
15761/* 96 */ 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x26, 0x26,\
15762/* 104 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26,\
15763/* 112 */ 0x00, 0x00, 0x12, 0x00, 0x00, 0x10, 0x00, 0x00,\
15764/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10,\
15765/* 128 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\
15766/* 136 */ 0x00, 0x00, 0x04, 0x04, 0x00, 0x00, 0x10, 0x00,\
15767/* 144 */ 0x10, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,\
15768/* 152 */ 0x00, 0x10, 0x00, 0x00, 0x06, 0x10, 0x00, 0x04,\
15769/* 160 */ 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
15770/* 168 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,\
15771/* 176 */ 0x00, 0x10, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00,\
15772/* 184 */ 0x00, 0x00, 0x00,}
15773
15774/* The resolve3P2Values() routine is able to run faster if it knows
15775** the value of the largest JUMP opcode. The smaller the maximum
15776** JUMP opcode the better, so the mkopcodeh.tcl script that
15777** generated this include file strives to group all JUMP opcodes
15778** together near the beginning of the list.
15779*/
15780#define SQLITE_MX_JUMP_OPCODE64 64 /* Maximum JUMP opcode */
15781
15782/************** End of opcodes.h *********************************************/
15783/************** Continuing where we left off in vdbe.h ***********************/
15784
15785/*
15786** Additional non-public SQLITE_PREPARE_* flags
15787*/
15788#define SQLITE_PREPARE_SAVESQL0x80 0x80 /* Preserve SQL text */
15789#define SQLITE_PREPARE_MASK0x0f 0x0f /* Mask of public flags */
15790
15791/*
15792** Prototypes for the VDBE interface. See comments on the implementation
15793** for a description of what each of these routines does.
15794*/
15795SQLITE_PRIVATEstatic Vdbe *sqlite3VdbeCreate(Parse*);
15796SQLITE_PRIVATEstatic Parse *sqlite3VdbeParser(Vdbe*);
15797SQLITE_PRIVATEstatic int sqlite3VdbeAddOp0(Vdbe*,int);
15798SQLITE_PRIVATEstatic int sqlite3VdbeAddOp1(Vdbe*,int,int);
15799SQLITE_PRIVATEstatic int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
15800SQLITE_PRIVATEstatic int sqlite3VdbeGoto(Vdbe*,int);
15801SQLITE_PRIVATEstatic int sqlite3VdbeLoadString(Vdbe*,int,const char*);
15802SQLITE_PRIVATEstatic void sqlite3VdbeMultiLoad(Vdbe*,int,const char*,...);
15803SQLITE_PRIVATEstatic int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
15804SQLITE_PRIVATEstatic int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
15805SQLITE_PRIVATEstatic int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int);
15806SQLITE_PRIVATEstatic int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
15807SQLITE_PRIVATEstatic int sqlite3VdbeAddFunctionCall(Parse*,int,int,int,int,const FuncDef*,int);
15808SQLITE_PRIVATEstatic void sqlite3VdbeEndCoroutine(Vdbe*,int);
15809#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
15810SQLITE_PRIVATEstatic void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N);
15811SQLITE_PRIVATEstatic void sqlite3VdbeVerifyNoResultRow(Vdbe *p);
15812#else
15813# define sqlite3VdbeVerifyNoMallocRequired(A,B)
15814# define sqlite3VdbeVerifyNoResultRow(A)
15815#endif
15816#if defined(SQLITE_DEBUG)
15817SQLITE_PRIVATEstatic void sqlite3VdbeVerifyAbortable(Vdbe *p, int);
15818SQLITE_PRIVATEstatic void sqlite3VdbeNoJumpsOutsideSubrtn(Vdbe*,int,int,int);
15819#else
15820# define sqlite3VdbeVerifyAbortable(A,B)
15821# define sqlite3VdbeNoJumpsOutsideSubrtn(A,B,C,D)
15822#endif
15823SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp,int iLineno);
15824#ifndef SQLITE_OMIT_EXPLAIN
15825SQLITE_PRIVATEstatic void sqlite3VdbeExplain(Parse*,u8,const char*,...);
15826SQLITE_PRIVATEstatic void sqlite3VdbeExplainPop(Parse*);
15827SQLITE_PRIVATEstatic int sqlite3VdbeExplainParent(Parse*);
15828# define ExplainQueryPlan(P)sqlite3VdbeExplain P sqlite3VdbeExplain P
15829# define ExplainQueryPlanPop(P)sqlite3VdbeExplainPop(P) sqlite3VdbeExplainPop(P)
15830# define ExplainQueryPlanParent(P)sqlite3VdbeExplainParent(P) sqlite3VdbeExplainParent(P)
15831#else
15832# define ExplainQueryPlan(P)sqlite3VdbeExplain P
15833# define ExplainQueryPlanPop(P)sqlite3VdbeExplainPop(P)
15834# define ExplainQueryPlanParent(P)sqlite3VdbeExplainParent(P) 0
15835# define sqlite3ExplainBreakpoint(A,B) /*no-op*/
15836#endif
15837#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_EXPLAIN)
15838SQLITE_PRIVATEstatic void sqlite3ExplainBreakpoint(const char*,const char*);
15839#else
15840# define sqlite3ExplainBreakpoint(A,B) /*no-op*/
15841#endif
15842SQLITE_PRIVATEstatic void sqlite3VdbeAddParseSchemaOp(Vdbe*, int, char*, u16);
15843SQLITE_PRIVATEstatic void sqlite3VdbeChangeOpcode(Vdbe*, int addr, u8);
15844SQLITE_PRIVATEstatic void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1);
15845SQLITE_PRIVATEstatic void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
15846SQLITE_PRIVATEstatic void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3);
15847SQLITE_PRIVATEstatic void sqlite3VdbeChangeP5(Vdbe*, u16 P5);
15848SQLITE_PRIVATEstatic void sqlite3VdbeJumpHere(Vdbe*, int addr);
15849SQLITE_PRIVATEstatic void sqlite3VdbeJumpHereOrPopInst(Vdbe*, int addr);
15850SQLITE_PRIVATEstatic int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
15851SQLITE_PRIVATEstatic int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
15852#ifdef SQLITE_DEBUG
15853SQLITE_PRIVATEstatic void sqlite3VdbeReleaseRegisters(Parse*,int addr, int n, u32 mask, int);
15854#else
15855# define sqlite3VdbeReleaseRegisters(P,A,N,M,F)
15856#endif
15857SQLITE_PRIVATEstatic void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
15858SQLITE_PRIVATEstatic void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type);
15859SQLITE_PRIVATEstatic void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
15860SQLITE_PRIVATEstatic void sqlite3VdbeUsesBtree(Vdbe*, int);
15861SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
15862SQLITE_PRIVATEstatic int sqlite3VdbeMakeLabel(Parse*);
15863SQLITE_PRIVATEstatic void sqlite3VdbeRunOnlyOnce(Vdbe*);
15864SQLITE_PRIVATEstatic void sqlite3VdbeReusable(Vdbe*);
15865SQLITE_PRIVATEstatic void sqlite3VdbeDelete(Vdbe*);
15866SQLITE_PRIVATEstatic void sqlite3VdbeMakeReady(Vdbe*,Parse*);
15867SQLITE_PRIVATEstatic int sqlite3VdbeFinalize(Vdbe*);
15868SQLITE_PRIVATEstatic void sqlite3VdbeResolveLabel(Vdbe*, int);
15869SQLITE_PRIVATEstatic int sqlite3VdbeCurrentAddr(Vdbe*);
15870#ifdef SQLITE_DEBUG
15871SQLITE_PRIVATEstatic int sqlite3VdbeAssertMayAbort(Vdbe *, int);
15872#endif
15873SQLITE_PRIVATEstatic void sqlite3VdbeResetStepResult(Vdbe*);
15874SQLITE_PRIVATEstatic void sqlite3VdbeRewind(Vdbe*);
15875SQLITE_PRIVATEstatic int sqlite3VdbeReset(Vdbe*);
15876SQLITE_PRIVATEstatic void sqlite3VdbeSetNumCols(Vdbe*,int);
15877SQLITE_PRIVATEstatic int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
15878SQLITE_PRIVATEstatic void sqlite3VdbeCountChanges(Vdbe*);
15879SQLITE_PRIVATEstatic sqlite3 *sqlite3VdbeDb(Vdbe*);
15880SQLITE_PRIVATEstatic u8 sqlite3VdbePrepareFlags(Vdbe*);
15881SQLITE_PRIVATEstatic void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, u8);
15882#ifdef SQLITE_ENABLE_NORMALIZE
15883SQLITE_PRIVATEstatic void sqlite3VdbeAddDblquoteStr(sqlite3*,Vdbe*,const char*);
15884SQLITE_PRIVATEstatic int sqlite3VdbeUsesDoubleQuotedString(Vdbe*,const char*);
15885#endif
15886SQLITE_PRIVATEstatic void sqlite3VdbeSwap(Vdbe*,Vdbe*);
15887SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
15888SQLITE_PRIVATEstatic sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
15889SQLITE_PRIVATEstatic void sqlite3VdbeSetVarmask(Vdbe*, int);
15890#ifndef SQLITE_OMIT_TRACE
15891SQLITE_PRIVATEstatic char *sqlite3VdbeExpandSql(Vdbe*, const char*);
15892#endif
15893SQLITE_PRIVATEstatic int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
15894SQLITE_PRIVATEstatic int sqlite3BlobCompare(const Mem*, const Mem*);
15895
15896SQLITE_PRIVATEstatic void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
15897SQLITE_PRIVATEstatic int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
15898SQLITE_PRIVATEstatic int sqlite3VdbeRecordCompareWithSkip(int, const void *, UnpackedRecord *, int);
15899SQLITE_PRIVATEstatic UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo*);
15900
15901typedef int (*RecordCompare)(int,const void*,UnpackedRecord*);
15902SQLITE_PRIVATEstatic RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
15903
15904SQLITE_PRIVATEstatic void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
15905SQLITE_PRIVATEstatic int sqlite3VdbeHasSubProgram(Vdbe*);
15906
15907SQLITE_PRIVATEstatic int sqlite3NotPureFunc(sqlite3_context*);
15908#ifdef SQLITE_ENABLE_BYTECODE_VTAB
15909SQLITE_PRIVATEstatic int sqlite3VdbeBytecodeVtabInit(sqlite3*);
15910#endif
15911
15912/* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on
15913** each VDBE opcode.
15914**
15915** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
15916** comments in VDBE programs that show key decision points in the code
15917** generator.
15918*/
15919#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
15920SQLITE_PRIVATEstatic void sqlite3VdbeComment(Vdbe*, const char*, ...);
15921# define VdbeComment(X) sqlite3VdbeComment X
15922SQLITE_PRIVATEstatic void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
15923# define VdbeNoopComment(X) sqlite3VdbeNoopComment X
15924# ifdef SQLITE_ENABLE_MODULE_COMMENTS
15925# define VdbeModuleComment(X) sqlite3VdbeNoopComment X
15926# else
15927# define VdbeModuleComment(X)
15928# endif
15929#else
15930# define VdbeComment(X)
15931# define VdbeNoopComment(X)
15932# define VdbeModuleComment(X)
15933#endif
15934
15935/*
15936** The VdbeCoverage macros are used to set a coverage testing point
15937** for VDBE branch instructions. The coverage testing points are line
15938** numbers in the sqlite3.c source file. VDBE branch coverage testing
15939** only works with an amalagmation build. That's ok since a VDBE branch
15940** coverage build designed for testing the test suite only. No application
15941** should ever ship with VDBE branch coverage measuring turned on.
15942**
15943** VdbeCoverage(v) // Mark the previously coded instruction
15944** // as a branch
15945**
15946** VdbeCoverageIf(v, conditional) // Mark previous if conditional true
15947**
15948** VdbeCoverageAlwaysTaken(v) // Previous branch is always taken
15949**
15950** VdbeCoverageNeverTaken(v) // Previous branch is never taken
15951**
15952** VdbeCoverageNeverNull(v) // Previous three-way branch is only
15953** // taken on the first two ways. The
15954** // NULL option is not possible
15955**
15956** VdbeCoverageEqNe(v) // Previous OP_Jump is only interested
15957** // in distingishing equal and not-equal.
15958**
15959** Every VDBE branch operation must be tagged with one of the macros above.
15960** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
15961** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
15962** routine in vdbe.c, alerting the developer to the missed tag.
15963**
15964** During testing, the test application will invoke
15965** sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE,...) to set a callback
15966** routine that is invoked as each bytecode branch is taken. The callback
15967** contains the sqlite3.c source line number ov the VdbeCoverage macro and
15968** flags to indicate whether or not the branch was taken. The test application
15969** is responsible for keeping track of this and reporting byte-code branches
15970** that are never taken.
15971**
15972** See the VdbeBranchTaken() macro and vdbeTakeBranch() function in the
15973** vdbe.c source file for additional information.
15974*/
15975#ifdef SQLITE_VDBE_COVERAGE
15976SQLITE_PRIVATEstatic void sqlite3VdbeSetLineNumber(Vdbe*,int);
15977# define VdbeCoverage(v) sqlite3VdbeSetLineNumber(v,__LINE__15977)
15978# define VdbeCoverageIf(v,x) if(x)sqlite3VdbeSetLineNumber(v,__LINE__15978)
15979# define VdbeCoverageAlwaysTaken(v) \
15980 sqlite3VdbeSetLineNumber(v,__LINE__15980|0x5000000);
15981# define VdbeCoverageNeverTaken(v) \
15982 sqlite3VdbeSetLineNumber(v,__LINE__15982|0x6000000);
15983# define VdbeCoverageNeverNull(v) \
15984 sqlite3VdbeSetLineNumber(v,__LINE__15984|0x4000000);
15985# define VdbeCoverageNeverNullIf(v,x) \
15986 if(x)sqlite3VdbeSetLineNumber(v,__LINE__15986|0x4000000);
15987# define VdbeCoverageEqNe(v) \
15988 sqlite3VdbeSetLineNumber(v,__LINE__15988|0x8000000);
15989# define VDBE_OFFSET_LINENO(x)0 (__LINE__15989+x)
15990#else
15991# define VdbeCoverage(v)
15992# define VdbeCoverageIf(v,x)
15993# define VdbeCoverageAlwaysTaken(v)
15994# define VdbeCoverageNeverTaken(v)
15995# define VdbeCoverageNeverNull(v)
15996# define VdbeCoverageNeverNullIf(v,x)
15997# define VdbeCoverageEqNe(v)
15998# define VDBE_OFFSET_LINENO(x)0 0
15999#endif
16000
16001#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
16002SQLITE_PRIVATEstatic void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const char*);
16003#else
16004# define sqlite3VdbeScanStatus(a,b,c,d,e)
16005#endif
16006
16007#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
16008SQLITE_PRIVATEstatic void sqlite3VdbePrintOp(FILE*, int, VdbeOp*);
16009#endif
16010
16011#endif /* SQLITE_VDBE_H */
16012
16013/************** End of vdbe.h ************************************************/
16014/************** Continuing where we left off in sqliteInt.h ******************/
16015/************** Include pcache.h in the middle of sqliteInt.h ****************/
16016/************** Begin file pcache.h ******************************************/
16017/*
16018** 2008 August 05
16019**
16020** The author disclaims copyright to this source code. In place of
16021** a legal notice, here is a blessing:
16022**
16023** May you do good and not evil.
16024** May you find forgiveness for yourself and forgive others.
16025** May you share freely, never taking more than you give.
16026**
16027*************************************************************************
16028** This header file defines the interface that the sqlite page cache
16029** subsystem.
16030*/
16031
16032#ifndef _PCACHE_H_
16033
16034typedef struct PgHdr PgHdr;
16035typedef struct PCache PCache;
16036
16037/*
16038** Every page in the cache is controlled by an instance of the following
16039** structure.
16040*/
16041struct PgHdr {
16042 sqlite3_pcache_page *pPage; /* Pcache object page handle */
16043 void *pData; /* Page data */
16044 void *pExtra; /* Extra content */
16045 PCache *pCache; /* PRIVATE: Cache that owns this page */
16046 PgHdr *pDirty; /* Transient list of dirty sorted by pgno */
16047 Pager *pPager; /* The pager this page is part of */
16048 Pgno pgno; /* Page number for this page */
16049#ifdef SQLITE_CHECK_PAGES
16050 u32 pageHash; /* Hash of page content */
16051#endif
16052 u16 flags; /* PGHDR flags defined below */
16053
16054 /**********************************************************************
16055 ** Elements above, except pCache, are public. All that follow are
16056 ** private to pcache.c and should not be accessed by other modules.
16057 ** pCache is grouped with the public elements for efficiency.
16058 */
16059 i16 nRef; /* Number of users of this page */
16060 PgHdr *pDirtyNext; /* Next element in list of dirty pages */
16061 PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */
16062 /* NB: pDirtyNext and pDirtyPrev are undefined if the
16063 ** PgHdr object is not dirty */
16064};
16065
16066/* Bit values for PgHdr.flags */
16067#define PGHDR_CLEAN0x001 0x001 /* Page not on the PCache.pDirty list */
16068#define PGHDR_DIRTY0x002 0x002 /* Page is on the PCache.pDirty list */
16069#define PGHDR_WRITEABLE0x004 0x004 /* Journaled and ready to modify */
16070#define PGHDR_NEED_SYNC0x008 0x008 /* Fsync the rollback journal before
16071 ** writing this page to the database */
16072#define PGHDR_DONT_WRITE0x010 0x010 /* Do not write content to disk */
16073#define PGHDR_MMAP0x020 0x020 /* This is an mmap page object */
16074
16075#define PGHDR_WAL_APPEND0x040 0x040 /* Appended to wal file */
16076
16077/* Initialize and shutdown the page cache subsystem */
16078SQLITE_PRIVATEstatic int sqlite3PcacheInitialize(void);
16079SQLITE_PRIVATEstatic void sqlite3PcacheShutdown(void);
16080
16081/* Page cache buffer management:
16082** These routines implement SQLITE_CONFIG_PAGECACHE.
16083*/
16084SQLITE_PRIVATEstatic void sqlite3PCacheBufferSetup(void *, int sz, int n);
16085
16086/* Create a new pager cache.
16087** Under memory stress, invoke xStress to try to make pages clean.
16088** Only clean and unpinned pages can be reclaimed.
16089*/
16090SQLITE_PRIVATEstatic int sqlite3PcacheOpen(
16091 int szPage, /* Size of every page */
16092 int szExtra, /* Extra space associated with each page */
16093 int bPurgeable, /* True if pages are on backing store */
16094 int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
16095 void *pStress, /* Argument to xStress */
16096 PCache *pToInit /* Preallocated space for the PCache */
16097);
16098
16099/* Modify the page-size after the cache has been created. */
16100SQLITE_PRIVATEstatic int sqlite3PcacheSetPageSize(PCache *, int);
16101
16102/* Return the size in bytes of a PCache object. Used to preallocate
16103** storage space.
16104*/
16105SQLITE_PRIVATEstatic int sqlite3PcacheSize(void);
16106
16107/* One release per successful fetch. Page is pinned until released.
16108** Reference counted.
16109*/
16110SQLITE_PRIVATEstatic sqlite3_pcache_page *sqlite3PcacheFetch(PCache*, Pgno, int createFlag);
16111SQLITE_PRIVATEstatic int sqlite3PcacheFetchStress(PCache*, Pgno, sqlite3_pcache_page**);
16112SQLITE_PRIVATEstatic PgHdr *sqlite3PcacheFetchFinish(PCache*, Pgno, sqlite3_pcache_page *pPage);
16113SQLITE_PRIVATEstatic void sqlite3PcacheRelease(PgHdr*);
16114
16115SQLITE_PRIVATEstatic void sqlite3PcacheDrop(PgHdr*); /* Remove page from cache */
16116SQLITE_PRIVATEstatic void sqlite3PcacheMakeDirty(PgHdr*); /* Make sure page is marked dirty */
16117SQLITE_PRIVATEstatic void sqlite3PcacheMakeClean(PgHdr*); /* Mark a single page as clean */
16118SQLITE_PRIVATEstatic void sqlite3PcacheCleanAll(PCache*); /* Mark all dirty list pages as clean */
16119SQLITE_PRIVATEstatic void sqlite3PcacheClearWritable(PCache*);
16120
16121/* Change a page number. Used by incr-vacuum. */
16122SQLITE_PRIVATEstatic void sqlite3PcacheMove(PgHdr*, Pgno);
16123
16124/* Remove all pages with pgno>x. Reset the cache if x==0 */
16125SQLITE_PRIVATEstatic void sqlite3PcacheTruncate(PCache*, Pgno x);
16126
16127/* Get a list of all dirty pages in the cache, sorted by page number */
16128SQLITE_PRIVATEstatic PgHdr *sqlite3PcacheDirtyList(PCache*);
16129
16130/* Reset and close the cache object */
16131SQLITE_PRIVATEstatic void sqlite3PcacheClose(PCache*);
16132
16133/* Clear flags from pages of the page cache */
16134SQLITE_PRIVATEstatic void sqlite3PcacheClearSyncFlags(PCache *);
16135
16136/* Discard the contents of the cache */
16137SQLITE_PRIVATEstatic void sqlite3PcacheClear(PCache*);
16138
16139/* Return the total number of outstanding page references */
16140SQLITE_PRIVATEstatic int sqlite3PcacheRefCount(PCache*);
16141
16142/* Increment the reference count of an existing page */
16143SQLITE_PRIVATEstatic void sqlite3PcacheRef(PgHdr*);
16144
16145SQLITE_PRIVATEstatic int sqlite3PcachePageRefcount(PgHdr*);
16146
16147/* Return the total number of pages stored in the cache */
16148SQLITE_PRIVATEstatic int sqlite3PcachePagecount(PCache*);
16149
16150#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
16151/* Iterate through all dirty pages currently stored in the cache. This
16152** interface is only available if SQLITE_CHECK_PAGES is defined when the
16153** library is built.
16154*/
16155SQLITE_PRIVATEstatic void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
16156#endif
16157
16158#if defined(SQLITE_DEBUG)
16159/* Check invariants on a PgHdr object */
16160SQLITE_PRIVATEstatic int sqlite3PcachePageSanity(PgHdr*);
16161#endif
16162
16163/* Set and get the suggested cache-size for the specified pager-cache.
16164**
16165** If no global maximum is configured, then the system attempts to limit
16166** the total number of pages cached by purgeable pager-caches to the sum
16167** of the suggested cache-sizes.
16168*/
16169SQLITE_PRIVATEstatic void sqlite3PcacheSetCachesize(PCache *, int);
16170#ifdef SQLITE_TEST
16171SQLITE_PRIVATEstatic int sqlite3PcacheGetCachesize(PCache *);
16172#endif
16173
16174/* Set or get the suggested spill-size for the specified pager-cache.
16175**
16176** The spill-size is the minimum number of pages in cache before the cache
16177** will attempt to spill dirty pages by calling xStress.
16178*/
16179SQLITE_PRIVATEstatic int sqlite3PcacheSetSpillsize(PCache *, int);
16180
16181/* Free up as much memory as possible from the page cache */
16182SQLITE_PRIVATEstatic void sqlite3PcacheShrink(PCache*);
16183
16184#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
16185/* Try to return memory used by the pcache module to the main memory heap */
16186SQLITE_PRIVATEstatic int sqlite3PcacheReleaseMemory(int);
16187#endif
16188
16189#ifdef SQLITE_TEST
16190SQLITE_PRIVATEstatic void sqlite3PcacheStats(int*,int*,int*,int*);
16191#endif
16192
16193SQLITE_PRIVATEstatic void sqlite3PCacheSetDefault(void);
16194
16195/* Return the header size */
16196SQLITE_PRIVATEstatic int sqlite3HeaderSizePcache(void);
16197SQLITE_PRIVATEstatic int sqlite3HeaderSizePcache1(void);
16198
16199/* Number of dirty pages as a percentage of the configured cache size */
16200SQLITE_PRIVATEstatic int sqlite3PCachePercentDirty(PCache*);
16201
16202#ifdef SQLITE_DIRECT_OVERFLOW_READ
16203SQLITE_PRIVATEstatic int sqlite3PCacheIsDirty(PCache *pCache);
16204#endif
16205
16206#endif /* _PCACHE_H_ */
16207
16208/************** End of pcache.h **********************************************/
16209/************** Continuing where we left off in sqliteInt.h ******************/
16210/************** Include os.h in the middle of sqliteInt.h ********************/
16211/************** Begin file os.h **********************************************/
16212/*
16213** 2001 September 16
16214**
16215** The author disclaims copyright to this source code. In place of
16216** a legal notice, here is a blessing:
16217**
16218** May you do good and not evil.
16219** May you find forgiveness for yourself and forgive others.
16220** May you share freely, never taking more than you give.
16221**
16222******************************************************************************
16223**
16224** This header file (together with is companion C source-code file
16225** "os.c") attempt to abstract the underlying operating system so that
16226** the SQLite library will work on both POSIX and windows systems.
16227**
16228** This header file is #include-ed by sqliteInt.h and thus ends up
16229** being included by every source file.
16230*/
16231#ifndef _SQLITE_OS_H_
16232#define _SQLITE_OS_H_
16233
16234/*
16235** Attempt to automatically detect the operating system and setup the
16236** necessary pre-processor macros for it.
16237*/
16238/************** Include os_setup.h in the middle of os.h *********************/
16239/************** Begin file os_setup.h ****************************************/
16240/*
16241** 2013 November 25
16242**
16243** The author disclaims copyright to this source code. In place of
16244** a legal notice, here is a blessing:
16245**
16246** May you do good and not evil.
16247** May you find forgiveness for yourself and forgive others.
16248** May you share freely, never taking more than you give.
16249**
16250******************************************************************************
16251**
16252** This file contains pre-processor directives related to operating system
16253** detection and/or setup.
16254*/
16255#ifndef SQLITE_OS_SETUP_H
16256#define SQLITE_OS_SETUP_H
16257
16258/*
16259** Figure out if we are dealing with Unix, Windows, or some other operating
16260** system.
16261**
16262** After the following block of preprocess macros, all of SQLITE_OS_UNIX,
16263** SQLITE_OS_WIN, and SQLITE_OS_OTHER will defined to either 1 or 0. One of
16264** the three will be 1. The other two will be 0.
16265*/
16266#if defined(SQLITE_OS_OTHER0)
16267# if SQLITE_OS_OTHER0==1
16268# undef SQLITE_OS_UNIX1
16269# define SQLITE_OS_UNIX1 0
16270# undef SQLITE_OS_WIN0
16271# define SQLITE_OS_WIN0 0
16272# else
16273# undef SQLITE_OS_OTHER0
16274# endif
16275#endif
16276#if !defined(SQLITE_OS_UNIX1) && !defined(SQLITE_OS_OTHER0)
16277# define SQLITE_OS_OTHER0 0
16278# ifndef SQLITE_OS_WIN0
16279# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
16280 defined(__MINGW32__) || defined(__BORLANDC__)
16281# define SQLITE_OS_WIN0 1
16282# define SQLITE_OS_UNIX1 0
16283# else
16284# define SQLITE_OS_WIN0 0
16285# define SQLITE_OS_UNIX1 1
16286# endif
16287# else
16288# define SQLITE_OS_UNIX1 0
16289# endif
16290#else
16291# ifndef SQLITE_OS_WIN0
16292# define SQLITE_OS_WIN0 0
16293# endif
16294#endif
16295
16296#endif /* SQLITE_OS_SETUP_H */
16297
16298/************** End of os_setup.h ********************************************/
16299/************** Continuing where we left off in os.h *************************/
16300
16301/* If the SET_FULLSYNC macro is not defined above, then make it
16302** a no-op
16303*/
16304#ifndef SET_FULLSYNC
16305# define SET_FULLSYNC(x,y)
16306#endif
16307
16308/* Maximum pathname length. Note: FILENAME_MAX defined by stdio.h
16309*/
16310#ifndef SQLITE_MAX_PATHLEN4096
16311# define SQLITE_MAX_PATHLEN4096 FILENAME_MAX4096
16312#endif
16313
16314/* Maximum number of symlinks that will be resolved while trying to
16315** expand a filename in xFullPathname() in the VFS.
16316*/
16317#ifndef SQLITE_MAX_SYMLINK200
16318# define SQLITE_MAX_SYMLINK200 200
16319#endif
16320
16321/*
16322** The default size of a disk sector
16323*/
16324#ifndef SQLITE_DEFAULT_SECTOR_SIZE4096
16325# define SQLITE_DEFAULT_SECTOR_SIZE4096 4096
16326#endif
16327
16328/*
16329** Temporary files are named starting with this prefix followed by 16 random
16330** alphanumeric characters, and no file extension. They are stored in the
16331** OS's standard temporary file directory, and are deleted prior to exit.
16332** If sqlite is being embedded in another program, you may wish to change the
16333** prefix to reflect your program's name, so that if your program exits
16334** prematurely, old temporary files can be easily identified. This can be done
16335** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
16336**
16337** 2006-10-31: The default prefix used to be "sqlite_". But then
16338** Mcafee started using SQLite in their anti-virus product and it
16339** started putting files with the "sqlite" name in the c:/temp folder.
16340** This annoyed many windows users. Those users would then do a
16341** Google search for "sqlite", find the telephone numbers of the
16342** developers and call to wake them up at night and complain.
16343** For this reason, the default name prefix is changed to be "sqlite"
16344** spelled backwards. So the temp files are still identified, but
16345** anybody smart enough to figure out the code is also likely smart
16346** enough to know that calling the developer will not help get rid
16347** of the file.
16348*/
16349#ifndef SQLITE_TEMP_FILE_PREFIX"etilqs_"
16350# define SQLITE_TEMP_FILE_PREFIX"etilqs_" "etilqs_"
16351#endif
16352
16353/*
16354** The following values may be passed as the second argument to
16355** sqlite3OsLock(). The various locks exhibit the following semantics:
16356**
16357** SHARED: Any number of processes may hold a SHARED lock simultaneously.
16358** RESERVED: A single process may hold a RESERVED lock on a file at
16359** any time. Other processes may hold and obtain new SHARED locks.
16360** PENDING: A single process may hold a PENDING lock on a file at
16361** any one time. Existing SHARED locks may persist, but no new
16362** SHARED locks may be obtained by other processes.
16363** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
16364**
16365** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
16366** process that requests an EXCLUSIVE lock may actually obtain a PENDING
16367** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
16368** sqlite3OsLock().
16369*/
16370#define NO_LOCK0 0
16371#define SHARED_LOCK1 1
16372#define RESERVED_LOCK2 2
16373#define PENDING_LOCK3 3
16374#define EXCLUSIVE_LOCK4 4
16375
16376/*
16377** File Locking Notes: (Mostly about windows but also some info for Unix)
16378**
16379** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
16380** those functions are not available. So we use only LockFile() and
16381** UnlockFile().
16382**
16383** LockFile() prevents not just writing but also reading by other processes.
16384** A SHARED_LOCK is obtained by locking a single randomly-chosen
16385** byte out of a specific range of bytes. The lock byte is obtained at
16386** random so two separate readers can probably access the file at the
16387** same time, unless they are unlucky and choose the same lock byte.
16388** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
16389** There can only be one writer. A RESERVED_LOCK is obtained by locking
16390** a single byte of the file that is designated as the reserved lock byte.
16391** A PENDING_LOCK is obtained by locking a designated byte different from
16392** the RESERVED_LOCK byte.
16393**
16394** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
16395** which means we can use reader/writer locks. When reader/writer locks
16396** are used, the lock is placed on the same range of bytes that is used
16397** for probabilistic locking in Win95/98/ME. Hence, the locking scheme
16398** will support two or more Win95 readers or two or more WinNT readers.
16399** But a single Win95 reader will lock out all WinNT readers and a single
16400** WinNT reader will lock out all other Win95 readers.
16401**
16402** The following #defines specify the range of bytes used for locking.
16403** SHARED_SIZE is the number of bytes available in the pool from which
16404** a random byte is selected for a shared lock. The pool of bytes for
16405** shared locks begins at SHARED_FIRST.
16406**
16407** The same locking strategy and
16408** byte ranges are used for Unix. This leaves open the possibility of having
16409** clients on win95, winNT, and unix all talking to the same shared file
16410** and all locking correctly. To do so would require that samba (or whatever
16411** tool is being used for file sharing) implements locks correctly between
16412** windows and unix. I'm guessing that isn't likely to happen, but by
16413** using the same locking range we are at least open to the possibility.
16414**
16415** Locking in windows is manditory. For this reason, we cannot store
16416** actual data in the bytes used for locking. The pager never allocates
16417** the pages involved in locking therefore. SHARED_SIZE is selected so
16418** that all locks will fit on a single page even at the minimum page size.
16419** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE
16420** is set high so that we don't have to allocate an unused page except
16421** for very large databases. But one should test the page skipping logic
16422** by setting PENDING_BYTE low and running the entire regression suite.
16423**
16424** Changing the value of PENDING_BYTE results in a subtly incompatible
16425** file format. Depending on how it is changed, you might not notice
16426** the incompatibility right away, even running a full regression test.
16427** The default location of PENDING_BYTE is the first byte past the
16428** 1GB boundary.
16429**
16430*/
16431#ifdef SQLITE_OMIT_WSD
16432# define PENDING_BYTEsqlite3PendingByte (0x40000000)
16433#else
16434# define PENDING_BYTEsqlite3PendingByte sqlite3PendingByte
16435#endif
16436#define RESERVED_BYTE(sqlite3PendingByte+1) (PENDING_BYTEsqlite3PendingByte+1)
16437#define SHARED_FIRST(sqlite3PendingByte+2) (PENDING_BYTEsqlite3PendingByte+2)
16438#define SHARED_SIZE510 510
16439
16440/*
16441** Wrapper around OS specific sqlite3_os_init() function.
16442*/
16443SQLITE_PRIVATEstatic int sqlite3OsInit(void);
16444
16445/*
16446** Functions for accessing sqlite3_file methods
16447*/
16448SQLITE_PRIVATEstatic void sqlite3OsClose(sqlite3_file*);
16449SQLITE_PRIVATEstatic int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
16450SQLITE_PRIVATEstatic int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
16451SQLITE_PRIVATEstatic int sqlite3OsTruncate(sqlite3_file*, i64 size);
16452SQLITE_PRIVATEstatic int sqlite3OsSync(sqlite3_file*, int);
16453SQLITE_PRIVATEstatic int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
16454SQLITE_PRIVATEstatic int sqlite3OsLock(sqlite3_file*, int);
16455SQLITE_PRIVATEstatic int sqlite3OsUnlock(sqlite3_file*, int);
16456SQLITE_PRIVATEstatic int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
16457SQLITE_PRIVATEstatic int sqlite3OsFileControl(sqlite3_file*,int,void*);
16458SQLITE_PRIVATEstatic void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
16459#define SQLITE_FCNTL_DB_UNCHANGED0xca093fa0 0xca093fa0
16460SQLITE_PRIVATEstatic int sqlite3OsSectorSize(sqlite3_file *id);
16461SQLITE_PRIVATEstatic int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
16462#ifndef SQLITE_OMIT_WAL
16463SQLITE_PRIVATEstatic int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
16464SQLITE_PRIVATEstatic int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
16465SQLITE_PRIVATEstatic void sqlite3OsShmBarrier(sqlite3_file *id);
16466SQLITE_PRIVATEstatic int sqlite3OsShmUnmap(sqlite3_file *id, int);
16467#endif /* SQLITE_OMIT_WAL */
16468SQLITE_PRIVATEstatic int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
16469SQLITE_PRIVATEstatic int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
16470
16471
16472/*
16473** Functions for accessing sqlite3_vfs methods
16474*/
16475SQLITE_PRIVATEstatic int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
16476SQLITE_PRIVATEstatic int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
16477SQLITE_PRIVATEstatic int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
16478SQLITE_PRIVATEstatic int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
16479#ifndef SQLITE_OMIT_LOAD_EXTENSION1
16480SQLITE_PRIVATEstatic void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
16481SQLITE_PRIVATEstatic void sqlite3OsDlError(sqlite3_vfs *, int, char *);
16482SQLITE_PRIVATEstatic void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
16483SQLITE_PRIVATEstatic void sqlite3OsDlClose(sqlite3_vfs *, void *);
16484#endif /* SQLITE_OMIT_LOAD_EXTENSION */
16485SQLITE_PRIVATEstatic int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
16486SQLITE_PRIVATEstatic int sqlite3OsSleep(sqlite3_vfs *, int);
16487SQLITE_PRIVATEstatic int sqlite3OsGetLastError(sqlite3_vfs*);
16488SQLITE_PRIVATEstatic int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
16489
16490/*
16491** Convenience functions for opening and closing files using
16492** sqlite3_malloc() to obtain space for the file-handle structure.
16493*/
16494SQLITE_PRIVATEstatic int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
16495SQLITE_PRIVATEstatic void sqlite3OsCloseFree(sqlite3_file *);
16496
16497#endif /* _SQLITE_OS_H_ */
16498
16499/************** End of os.h **************************************************/
16500/************** Continuing where we left off in sqliteInt.h ******************/
16501/************** Include mutex.h in the middle of sqliteInt.h *****************/
16502/************** Begin file mutex.h *******************************************/
16503/*
16504** 2007 August 28
16505**
16506** The author disclaims copyright to this source code. In place of
16507** a legal notice, here is a blessing:
16508**
16509** May you do good and not evil.
16510** May you find forgiveness for yourself and forgive others.
16511** May you share freely, never taking more than you give.
16512**
16513*************************************************************************
16514**
16515** This file contains the common header for all mutex implementations.
16516** The sqliteInt.h header #includes this file so that it is available
16517** to all source files. We break it out in an effort to keep the code
16518** better organized.
16519**
16520** NOTE: source files should *not* #include this header file directly.
16521** Source files should #include the sqliteInt.h file and let that file
16522** include this one indirectly.
16523*/
16524
16525
16526/*
16527** Figure out what version of the code to use. The choices are
16528**
16529** SQLITE_MUTEX_OMIT No mutex logic. Not even stubs. The
16530** mutexes implementation cannot be overridden
16531** at start-time.
16532**
16533** SQLITE_MUTEX_NOOP For single-threaded applications. No
16534** mutual exclusion is provided. But this
16535** implementation can be overridden at
16536** start-time.
16537**
16538** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
16539**
16540** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
16541*/
16542#if !SQLITE_THREADSAFE2
16543# define SQLITE_MUTEX_OMIT
16544#endif
16545#if SQLITE_THREADSAFE2 && !defined(SQLITE_MUTEX_NOOP)
16546# if SQLITE_OS_UNIX1
16547# define SQLITE_MUTEX_PTHREADS
16548# elif SQLITE_OS_WIN0
16549# define SQLITE_MUTEX_W32
16550# else
16551# define SQLITE_MUTEX_NOOP
16552# endif
16553#endif
16554
16555#ifdef SQLITE_MUTEX_OMIT
16556/*
16557** If this is a no-op implementation, implement everything as macros.
16558*/
16559#define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8)
16560#define sqlite3_mutex_free(X)
16561#define sqlite3_mutex_enter(X)
16562#define sqlite3_mutex_try(X) SQLITE_OK0
16563#define sqlite3_mutex_leave(X)
16564#define sqlite3_mutex_held(X) ((void)(X),1)
16565#define sqlite3_mutex_notheld(X) ((void)(X),1)
16566#define sqlite3MutexAlloc(X) ((sqlite3_mutex*)8)
16567#define sqlite3MutexInit() SQLITE_OK0
16568#define sqlite3MutexEnd()
16569#define MUTEX_LOGIC(X)X
16570#else
16571#define MUTEX_LOGIC(X)X X
16572SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
16573#endif /* defined(SQLITE_MUTEX_OMIT) */
16574
16575/************** End of mutex.h ***********************************************/
16576/************** Continuing where we left off in sqliteInt.h ******************/
16577
16578/* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
16579** synchronous setting to EXTRA. It is no longer supported.
16580*/
16581#ifdef SQLITE_EXTRA_DURABLE
16582# warning Use SQLITE_DEFAULT_SYNCHRONOUS2=3 instead of SQLITE_EXTRA_DURABLE
16583# define SQLITE_DEFAULT_SYNCHRONOUS2 3
16584#endif
16585
16586/*
16587** Default synchronous levels.
16588**
16589** Note that (for historcal reasons) the PAGER_SYNCHRONOUS_* macros differ
16590** from the SQLITE_DEFAULT_SYNCHRONOUS value by 1.
16591**
16592** PAGER_SYNCHRONOUS DEFAULT_SYNCHRONOUS
16593** OFF 1 0
16594** NORMAL 2 1
16595** FULL 3 2
16596** EXTRA 4 3
16597**
16598** The "PRAGMA synchronous" statement also uses the zero-based numbers.
16599** In other words, the zero-based numbers are used for all external interfaces
16600** and the one-based values are used internally.
16601*/
16602#ifndef SQLITE_DEFAULT_SYNCHRONOUS2
16603# define SQLITE_DEFAULT_SYNCHRONOUS2 2
16604#endif
16605#ifndef SQLITE_DEFAULT_WAL_SYNCHRONOUS1
16606# define SQLITE_DEFAULT_WAL_SYNCHRONOUS1 SQLITE_DEFAULT_SYNCHRONOUS2
16607#endif
16608
16609/*
16610** Each database file to be accessed by the system is an instance
16611** of the following structure. There are normally two of these structures
16612** in the sqlite.aDb[] array. aDb[0] is the main database file and
16613** aDb[1] is the database file used to hold temporary tables. Additional
16614** databases may be attached.
16615*/
16616struct Db {
16617 char *zDbSName; /* Name of this database. (schema name, not filename) */
16618 Btree *pBt; /* The B*Tree structure for this database file */
16619 u8 safety_level; /* How aggressive at syncing data to disk */
16620 u8 bSyncSet; /* True if "PRAGMA synchronous=N" has been run */
16621 Schema *pSchema; /* Pointer to database schema (possibly shared) */
16622};
16623
16624/*
16625** An instance of the following structure stores a database schema.
16626**
16627** Most Schema objects are associated with a Btree. The exception is
16628** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
16629** In shared cache mode, a single Schema object can be shared by multiple
16630** Btrees that refer to the same underlying BtShared object.
16631**
16632** Schema objects are automatically deallocated when the last Btree that
16633** references them is destroyed. The TEMP Schema is manually freed by
16634** sqlite3_close().
16635*
16636** A thread must be holding a mutex on the corresponding Btree in order
16637** to access Schema content. This implies that the thread must also be
16638** holding a mutex on the sqlite3 connection pointer that owns the Btree.
16639** For a TEMP Schema, only the connection mutex is required.
16640*/
16641struct Schema {
16642 int schema_cookie; /* Database schema version number for this file */
16643 int iGeneration; /* Generation counter. Incremented with each change */
16644 Hash tblHash; /* All tables indexed by name */
16645 Hash idxHash; /* All (named) indices indexed by name */
16646 Hash trigHash; /* All triggers indexed by name */
16647 Hash fkeyHash; /* All foreign keys by referenced table name */
16648 Table *pSeqTab; /* The sqlite_sequence table used by AUTOINCREMENT */
16649 u8 file_format; /* Schema format version for this file */
16650 u8 enc; /* Text encoding used by this database */
16651 u16 schemaFlags; /* Flags associated with this schema */
16652 int cache_size; /* Number of pages to use in the cache */
16653};
16654
16655/*
16656** These macros can be used to test, set, or clear bits in the
16657** Db.pSchema->flags field.
16658*/
16659#define DbHasProperty(D,I,P)(((D)->aDb[I].pSchema->schemaFlags&(P))==(P)) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
16660#define DbHasAnyProperty(D,I,P)(((D)->aDb[I].pSchema->schemaFlags&(P))!=0) (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
16661#define DbSetProperty(D,I,P)(D)->aDb[I].pSchema->schemaFlags|=(P) (D)->aDb[I].pSchema->schemaFlags|=(P)
16662#define DbClearProperty(D,I,P)(D)->aDb[I].pSchema->schemaFlags&=~(P) (D)->aDb[I].pSchema->schemaFlags&=~(P)
16663
16664/*
16665** Allowed values for the DB.pSchema->flags field.
16666**
16667** The DB_SchemaLoaded flag is set after the database schema has been
16668** read into internal hash tables.
16669**
16670** DB_UnresetViews means that one or more views have column names that
16671** have been filled out. If the schema changes, these column names might
16672** changes and so the view will need to be reset.
16673*/
16674#define DB_SchemaLoaded0x0001 0x0001 /* The schema has been loaded */
16675#define DB_UnresetViews0x0002 0x0002 /* Some views have defined column names */
16676#define DB_ResetWanted0x0008 0x0008 /* Reset the schema when nSchemaLock==0 */
16677
16678/*
16679** The number of different kinds of things that can be limited
16680** using the sqlite3_limit() interface.
16681*/
16682#define SQLITE_N_LIMIT(11 +1) (SQLITE_LIMIT_WORKER_THREADS11+1)
16683
16684/*
16685** Lookaside malloc is a set of fixed-size buffers that can be used
16686** to satisfy small transient memory allocation requests for objects
16687** associated with a particular database connection. The use of
16688** lookaside malloc provides a significant performance enhancement
16689** (approx 10%) by avoiding numerous malloc/free requests while parsing
16690** SQL statements.
16691**
16692** The Lookaside structure holds configuration information about the
16693** lookaside malloc subsystem. Each available memory allocation in
16694** the lookaside subsystem is stored on a linked list of LookasideSlot
16695** objects.
16696**
16697** Lookaside allocations are only allowed for objects that are associated
16698** with a particular database connection. Hence, schema information cannot
16699** be stored in lookaside because in shared cache mode the schema information
16700** is shared by multiple database connections. Therefore, while parsing
16701** schema information, the Lookaside.bEnabled flag is cleared so that
16702** lookaside allocations are not used to construct the schema objects.
16703**
16704** New lookaside allocations are only allowed if bDisable==0. When
16705** bDisable is greater than zero, sz is set to zero which effectively
16706** disables lookaside without adding a new test for the bDisable flag
16707** in a performance-critical path. sz should be set by to szTrue whenever
16708** bDisable changes back to zero.
16709**
16710** Lookaside buffers are initially held on the pInit list. As they are
16711** used and freed, they are added back to the pFree list. New allocations
16712** come off of pFree first, then pInit as a fallback. This dual-list
16713** allows use to compute a high-water mark - the maximum number of allocations
16714** outstanding at any point in the past - by subtracting the number of
16715** allocations on the pInit list from the total number of allocations.
16716**
16717** Enhancement on 2019-12-12: Two-size-lookaside
16718** The default lookaside configuration is 100 slots of 1200 bytes each.
16719** The larger slot sizes are important for performance, but they waste
16720** a lot of space, as most lookaside allocations are less than 128 bytes.
16721** The two-size-lookaside enhancement breaks up the lookaside allocation
16722** into two pools: One of 128-byte slots and the other of the default size
16723** (1200-byte) slots. Allocations are filled from the small-pool first,
16724** failing over to the full-size pool if that does not work. Thus more
16725** lookaside slots are available while also using less memory.
16726** This enhancement can be omitted by compiling with
16727** SQLITE_OMIT_TWOSIZE_LOOKASIDE.
16728*/
16729struct Lookaside {
16730 u32 bDisable; /* Only operate the lookaside when zero */
16731 u16 sz; /* Size of each buffer in bytes */
16732 u16 szTrue; /* True value of sz, even if disabled */
16733 u8 bMalloced; /* True if pStart obtained from sqlite3_malloc() */
16734 u32 nSlot; /* Number of lookaside slots allocated */
16735 u32 anStat[3]; /* 0: hits. 1: size misses. 2: full misses */
16736 LookasideSlot *pInit; /* List of buffers not previously used */
16737 LookasideSlot *pFree; /* List of available buffers */
16738#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
16739 LookasideSlot *pSmallInit; /* List of small buffers not prediously used */
16740 LookasideSlot *pSmallFree; /* List of available small buffers */
16741 void *pMiddle; /* First byte past end of full-size buffers and
16742 ** the first byte of LOOKASIDE_SMALL buffers */
16743#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
16744 void *pStart; /* First byte of available memory space */
16745 void *pEnd; /* First byte past end of available space */
16746};
16747struct LookasideSlot {
16748 LookasideSlot *pNext; /* Next buffer in the list of free buffers */
16749};
16750
16751#define DisableLookasidedb->lookaside.bDisable++;db->lookaside.sz=0 db->lookaside.bDisable++;db->lookaside.sz=0
16752#define EnableLookasidedb->lookaside.bDisable--; db->lookaside.sz=db->lookaside
.bDisable?0:db->lookaside.szTrue
db->lookaside.bDisable--;\
16753 db->lookaside.sz=db->lookaside.bDisable?0:db->lookaside.szTrue
16754
16755/* Size of the smaller allocations in two-size lookside */
16756#ifdef SQLITE_OMIT_TWOSIZE_LOOKASIDE
16757# define LOOKASIDE_SMALL128 0
16758#else
16759# define LOOKASIDE_SMALL128 128
16760#endif
16761
16762/*
16763** A hash table for built-in function definitions. (Application-defined
16764** functions use a regular table table from hash.h.)
16765**
16766** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
16767** Collisions are on the FuncDef.u.pHash chain. Use the SQLITE_FUNC_HASH()
16768** macro to compute a hash on the function name.
16769*/
16770#define SQLITE_FUNC_HASH_SZ23 23
16771struct FuncDefHash {
16772 FuncDef *a[SQLITE_FUNC_HASH_SZ23]; /* Hash table for functions */
16773};
16774#define SQLITE_FUNC_HASH(C,L)(((C)+(L))%23) (((C)+(L))%SQLITE_FUNC_HASH_SZ23)
16775
16776#ifdef SQLITE_USER_AUTHENTICATION
16777/*
16778** Information held in the "sqlite3" database connection object and used
16779** to manage user authentication.
16780*/
16781typedef struct sqlite3_userauth sqlite3_userauth;
16782struct sqlite3_userauth {
16783 u8 authLevel; /* Current authentication level */
16784 int nAuthPW; /* Size of the zAuthPW in bytes */
16785 char *zAuthPW; /* Password used to authenticate */
16786 char *zAuthUser; /* User name used to authenticate */
16787};
16788
16789/* Allowed values for sqlite3_userauth.authLevel */
16790#define UAUTH_Unknown 0 /* Authentication not yet checked */
16791#define UAUTH_Fail 1 /* User authentication failed */
16792#define UAUTH_User 2 /* Authenticated as a normal user */
16793#define UAUTH_Admin 3 /* Authenticated as an administrator */
16794
16795/* Functions used only by user authorization logic */
16796SQLITE_PRIVATEstatic int sqlite3UserAuthTable(const char*);
16797SQLITE_PRIVATEstatic int sqlite3UserAuthCheckLogin(sqlite3*,const char*,u8*);
16798SQLITE_PRIVATEstatic void sqlite3UserAuthInit(sqlite3*);
16799SQLITE_PRIVATEstatic void sqlite3CryptFunc(sqlite3_context*,int,sqlite3_value**);
16800
16801#endif /* SQLITE_USER_AUTHENTICATION */
16802
16803/*
16804** typedef for the authorization callback function.
16805*/
16806#ifdef SQLITE_USER_AUTHENTICATION
16807 typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
16808 const char*, const char*);
16809#else
16810 typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
16811 const char*);
16812#endif
16813
16814#ifndef SQLITE_OMIT_DEPRECATED1
16815/* This is an extra SQLITE_TRACE macro that indicates "legacy" tracing
16816** in the style of sqlite3_trace()
16817*/
16818#define SQLITE_TRACE_LEGACY0 0x40 /* Use the legacy xTrace */
16819#define SQLITE_TRACE_XPROFILE0 0x80 /* Use the legacy xProfile */
16820#else
16821#define SQLITE_TRACE_LEGACY0 0
16822#define SQLITE_TRACE_XPROFILE0 0
16823#endif /* SQLITE_OMIT_DEPRECATED */
16824#define SQLITE_TRACE_NONLEGACY_MASK0x0f 0x0f /* Normal flags */
16825
16826/*
16827** Maximum number of sqlite3.aDb[] entries. This is the number of attached
16828** databases plus 2 for "main" and "temp".
16829*/
16830#define SQLITE_MAX_DB(10 +2) (SQLITE_MAX_ATTACHED10+2)
16831
16832/*
16833** Each database connection is an instance of the following structure.
16834*/
16835struct sqlite3 {
16836 sqlite3_vfs *pVfs; /* OS Interface */
16837 struct Vdbe *pVdbe; /* List of active virtual machines */
16838 CollSeq *pDfltColl; /* BINARY collseq for the database encoding */
16839 sqlite3_mutex *mutex; /* Connection mutex */
16840 Db *aDb; /* All backends */
16841 int nDb; /* Number of backends currently in use */
16842 u32 mDbFlags; /* flags recording internal state */
16843 u64 flags; /* flags settable by pragmas. See below */
16844 i64 lastRowid; /* ROWID of most recent insert (see above) */
16845 i64 szMmap; /* Default mmap_size setting */
16846 u32 nSchemaLock; /* Do not reset the schema when non-zero */
16847 unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
16848 int errCode; /* Most recent error code (SQLITE_*) */
16849 int errByteOffset; /* Byte offset of error in SQL statement */
16850 int errMask; /* & result codes with this before returning */
16851 int iSysErrno; /* Errno value from last system error */
16852 u32 dbOptFlags; /* Flags to enable/disable optimizations */
16853 u8 enc; /* Text encoding */
16854 u8 autoCommit; /* The auto-commit flag. */
16855 u8 temp_store; /* 1: file 2: memory 0: default */
16856 u8 mallocFailed; /* True if we have seen a malloc failure */
16857 u8 bBenignMalloc; /* Do not require OOMs if true */
16858 u8 dfltLockMode; /* Default locking-mode for attached dbs */
16859 signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
16860 u8 suppressErr; /* Do not issue error messages if true */
16861 u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */
16862 u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
16863 u8 mTrace; /* zero or more SQLITE_TRACE flags */
16864 u8 noSharedCache; /* True if no shared-cache backends */
16865 u8 nSqlExec; /* Number of pending OP_SqlExec opcodes */
16866 u8 eOpenState; /* Current condition of the connection */
16867 int nextPagesize; /* Pagesize after VACUUM if >0 */
16868 i64 nChange; /* Value returned by sqlite3_changes() */
16869 i64 nTotalChange; /* Value returned by sqlite3_total_changes() */
16870 int aLimit[SQLITE_N_LIMIT(11 +1)]; /* Limits */
16871 int nMaxSorterMmap; /* Maximum size of regions mapped by sorter */
16872 struct sqlite3InitInfo { /* Information used during initialization */
16873 Pgno newTnum; /* Rootpage of table being initialized */
16874 u8 iDb; /* Which db file is being initialized */
16875 u8 busy; /* TRUE if currently initializing */
16876 unsigned orphanTrigger : 1; /* Last statement is orphaned TEMP trigger */
16877 unsigned imposterTable : 1; /* Building an imposter table */
16878 unsigned reopenMemdb : 1; /* ATTACH is really a reopen using MemDB */
16879 const char **azInit; /* "type", "name", and "tbl_name" columns */
16880 } init;
16881 int nVdbeActive; /* Number of VDBEs currently running */
16882 int nVdbeRead; /* Number of active VDBEs that read or write */
16883 int nVdbeWrite; /* Number of active VDBEs that read and write */
16884 int nVdbeExec; /* Number of nested calls to VdbeExec() */
16885 int nVDestroy; /* Number of active OP_VDestroy operations */
16886 int nExtension; /* Number of loaded extensions */
16887 void **aExtension; /* Array of shared library handles */
16888 union {
16889 void (*xLegacy)(void*,const char*); /* mTrace==SQLITE_TRACE_LEGACY */
16890 int (*xV2)(u32,void*,void*,void*); /* All other mTrace values */
16891 } trace;
16892 void *pTraceArg; /* Argument to the trace function */
16893#ifndef SQLITE_OMIT_DEPRECATED1
16894 void (*xProfile)(void*,const char*,u64); /* Profiling function */
16895 void *pProfileArg; /* Argument to profile function */
16896#endif
16897 void *pCommitArg; /* Argument to xCommitCallback() */
16898 int (*xCommitCallback)(void*); /* Invoked at every commit. */
16899 void *pRollbackArg; /* Argument to xRollbackCallback() */
16900 void (*xRollbackCallback)(void*); /* Invoked at every commit. */
16901 void *pUpdateArg;
16902 void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
16903 void *pAutovacPagesArg; /* Client argument to autovac_pages */
16904 void (*xAutovacDestr)(void*); /* Destructor for pAutovacPAgesArg */
16905 unsigned int (*xAutovacPages)(void*,const char*,u32,u32,u32);
16906 Parse *pParse; /* Current parse */
16907#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
16908 void *pPreUpdateArg; /* First argument to xPreUpdateCallback */
16909 void (*xPreUpdateCallback)( /* Registered using sqlite3_preupdate_hook() */
16910 void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64
16911 );
16912 PreUpdate *pPreUpdate; /* Context for active pre-update callback */
16913#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
16914#ifndef SQLITE_OMIT_WAL
16915 int (*xWalCallback)(void *, sqlite3 *, const char *, int);
16916 void *pWalArg;
16917#endif
16918 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
16919 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
16920 void *pCollNeededArg;
16921 sqlite3_value *pErr; /* Most recent error message */
16922 union {
16923 volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
16924 double notUsed1; /* Spacer */
16925 } u1;
16926 Lookaside lookaside; /* Lookaside malloc configuration */
16927#ifndef SQLITE_OMIT_AUTHORIZATION
16928 sqlite3_xauth xAuth; /* Access authorization function */
16929 void *pAuthArg; /* 1st argument to the access auth function */
16930#endif
16931#ifndef SQLITE_OMIT_PROGRESS_CALLBACK1
16932 int (*xProgress)(void *); /* The progress callback */
16933 void *pProgressArg; /* Argument to the progress callback */
16934 unsigned nProgressOps; /* Number of opcodes for progress callback */
16935#endif
16936#ifndef SQLITE_OMIT_VIRTUALTABLE
16937 int nVTrans; /* Allocated size of aVTrans */
16938 Hash aModule; /* populated by sqlite3_create_module() */
16939 VtabCtx *pVtabCtx; /* Context for active vtab connect/create */
16940 VTable **aVTrans; /* Virtual tables with open transactions */
16941 VTable *pDisconnect; /* Disconnect these in next sqlite3_prepare() */
16942#endif
16943 Hash aFunc; /* Hash table of connection functions */
16944 Hash aCollSeq; /* All collating sequences */
16945 BusyHandler busyHandler; /* Busy callback */
16946 Db aDbStatic[2]; /* Static space for the 2 default backends */
16947 Savepoint *pSavepoint; /* List of active savepoints */
16948 int nAnalysisLimit; /* Number of index rows to ANALYZE */
16949 int busyTimeout; /* Busy handler timeout, in msec */
16950 int nSavepoint; /* Number of non-transaction savepoints */
16951 int nStatement; /* Number of nested statement-transactions */
16952 i64 nDeferredCons; /* Net deferred constraints this transaction. */
16953 i64 nDeferredImmCons; /* Net deferred immediate constraints */
16954 int *pnBytesFreed; /* If not NULL, increment this in DbFree() */
16955#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
16956 /* The following variables are all protected by the STATIC_MAIN
16957 ** mutex, not by sqlite3.mutex. They are used by code in notify.c.
16958 **
16959 ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
16960 ** unlock so that it can proceed.
16961 **
16962 ** When X.pBlockingConnection==Y, that means that something that X tried
16963 ** tried to do recently failed with an SQLITE_LOCKED error due to locks
16964 ** held by Y.
16965 */
16966 sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
16967 sqlite3 *pUnlockConnection; /* Connection to watch for unlock */
16968 void *pUnlockArg; /* Argument to xUnlockNotify */
16969 void (*xUnlockNotify)(void **, int); /* Unlock notify callback */
16970 sqlite3 *pNextBlocked; /* Next in list of all blocked connections */
16971#endif
16972#ifdef SQLITE_USER_AUTHENTICATION
16973 sqlite3_userauth auth; /* User authentication information */
16974#endif
16975};
16976
16977/*
16978** A macro to discover the encoding of a database.
16979*/
16980#define SCHEMA_ENC(db)((db)->aDb[0].pSchema->enc) ((db)->aDb[0].pSchema->enc)
16981#define ENC(db)((db)->enc) ((db)->enc)
16982
16983/*
16984** A u64 constant where the lower 32 bits are all zeros. Only the
16985** upper 32 bits are included in the argument. Necessary because some
16986** C-compilers still do not accept LL integer literals.
16987*/
16988#define HI(X)((u64)(X)<<32) ((u64)(X)<<32)
16989
16990/*
16991** Possible values for the sqlite3.flags.
16992**
16993** Value constraints (enforced via assert()):
16994** SQLITE_FullFSync == PAGER_FULLFSYNC
16995** SQLITE_CkptFullFSync == PAGER_CKPT_FULLFSYNC
16996** SQLITE_CacheSpill == PAGER_CACHE_SPILL
16997*/
16998#define SQLITE_WriteSchema0x00000001 0x00000001 /* OK to update SQLITE_SCHEMA */
16999#define SQLITE_LegacyFileFmt0x00000002 0x00000002 /* Create new databases in format 1 */
17000#define SQLITE_FullColNames0x00000004 0x00000004 /* Show full column names on SELECT */
17001#define SQLITE_FullFSync0x00000008 0x00000008 /* Use full fsync on the backend */
17002#define SQLITE_CkptFullFSync0x00000010 0x00000010 /* Use full fsync for checkpoint */
17003#define SQLITE_CacheSpill0x00000020 0x00000020 /* OK to spill pager cache */
17004#define SQLITE_ShortColNames0x00000040 0x00000040 /* Show short columns names */
17005#define SQLITE_TrustedSchema0x00000080 0x00000080 /* Allow unsafe functions and
17006 ** vtabs in the schema definition */
17007#define SQLITE_NullCallback0x00000100 0x00000100 /* Invoke the callback once if the */
17008 /* result set is empty */
17009#define SQLITE_IgnoreChecks0x00000200 0x00000200 /* Do not enforce check constraints */
17010#define SQLITE_ReadUncommit0x00000400 0x00000400 /* READ UNCOMMITTED in shared-cache */
17011#define SQLITE_NoCkptOnClose0x00000800 0x00000800 /* No checkpoint on close()/DETACH */
17012#define SQLITE_ReverseOrder0x00001000 0x00001000 /* Reverse unordered SELECTs */
17013#define SQLITE_RecTriggers0x00002000 0x00002000 /* Enable recursive triggers */
17014#define SQLITE_ForeignKeys0x00004000 0x00004000 /* Enforce foreign key constraints */
17015#define SQLITE_AutoIndex0x00008000 0x00008000 /* Enable automatic indexes */
17016#define SQLITE_LoadExtension0x00010000 0x00010000 /* Enable load_extension */
17017#define SQLITE_LoadExtFunc0x00020000 0x00020000 /* Enable load_extension() SQL func */
17018#define SQLITE_EnableTrigger0x00040000 0x00040000 /* True to enable triggers */
17019#define SQLITE_DeferFKs0x00080000 0x00080000 /* Defer all FK constraints */
17020#define SQLITE_QueryOnly0x00100000 0x00100000 /* Disable database changes */
17021#define SQLITE_CellSizeCk0x00200000 0x00200000 /* Check btree cell sizes on load */
17022#define SQLITE_Fts3Tokenizer0x00400000 0x00400000 /* Enable fts3_tokenizer(2) */
17023#define SQLITE_EnableQPSG0x00800000 0x00800000 /* Query Planner Stability Guarantee*/
17024#define SQLITE_TriggerEQP0x01000000 0x01000000 /* Show trigger EXPLAIN QUERY PLAN */
17025#define SQLITE_ResetDatabase0x02000000 0x02000000 /* Reset the database */
17026#define SQLITE_LegacyAlter0x04000000 0x04000000 /* Legacy ALTER TABLE behaviour */
17027#define SQLITE_NoSchemaError0x08000000 0x08000000 /* Do not report schema parse errors*/
17028#define SQLITE_Defensive0x10000000 0x10000000 /* Input SQL is likely hostile */
17029#define SQLITE_DqsDDL0x20000000 0x20000000 /* dbl-quoted strings allowed in DDL*/
17030#define SQLITE_DqsDML0x40000000 0x40000000 /* dbl-quoted strings allowed in DML*/
17031#define SQLITE_EnableView0x80000000 0x80000000 /* Enable the use of views */
17032#define SQLITE_CountRows((u64)(0x00001)<<32) HI(0x00001)((u64)(0x00001)<<32) /* Count rows changed by INSERT, */
17033 /* DELETE, or UPDATE and return */
17034 /* the count using a callback. */
17035#define SQLITE_CorruptRdOnly((u64)(0x00002)<<32) HI(0x00002)((u64)(0x00002)<<32) /* Prohibit writes due to error */
17036
17037/* Flags used only if debugging */
17038#ifdef SQLITE_DEBUG
17039#define SQLITE_SqlTrace HI(0x0100000)((u64)(0x0100000)<<32) /* Debug print SQL as it executes */
17040#define SQLITE_VdbeListing HI(0x0200000)((u64)(0x0200000)<<32) /* Debug listings of VDBE progs */
17041#define SQLITE_VdbeTrace HI(0x0400000)((u64)(0x0400000)<<32) /* True to trace VDBE execution */
17042#define SQLITE_VdbeAddopTrace HI(0x0800000)((u64)(0x0800000)<<32) /* Trace sqlite3VdbeAddOp() calls */
17043#define SQLITE_VdbeEQP HI(0x1000000)((u64)(0x1000000)<<32) /* Debug EXPLAIN QUERY PLAN */
17044#define SQLITE_ParserTrace HI(0x2000000)((u64)(0x2000000)<<32) /* PRAGMA parser_trace=ON */
17045#endif
17046
17047/*
17048** Allowed values for sqlite3.mDbFlags
17049*/
17050#define DBFLAG_SchemaChange0x0001 0x0001 /* Uncommitted Hash table changes */
17051#define DBFLAG_PreferBuiltin0x0002 0x0002 /* Preference to built-in funcs */
17052#define DBFLAG_Vacuum0x0004 0x0004 /* Currently in a VACUUM */
17053#define DBFLAG_VacuumInto0x0008 0x0008 /* Currently running VACUUM INTO */
17054#define DBFLAG_SchemaKnownOk0x0010 0x0010 /* Schema is known to be valid */
17055#define DBFLAG_InternalFunc0x0020 0x0020 /* Allow use of internal functions */
17056#define DBFLAG_EncodingFixed0x0040 0x0040 /* No longer possible to change enc. */
17057
17058/*
17059** Bits of the sqlite3.dbOptFlags field that are used by the
17060** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
17061** selectively disable various optimizations.
17062*/
17063#define SQLITE_QueryFlattener0x00000001 0x00000001 /* Query flattening */
17064#define SQLITE_WindowFunc0x00000002 0x00000002 /* Use xInverse for window functions */
17065#define SQLITE_GroupByOrder0x00000004 0x00000004 /* GROUPBY cover of ORDERBY */
17066#define SQLITE_FactorOutConst0x00000008 0x00000008 /* Constant factoring */
17067#define SQLITE_DistinctOpt0x00000010 0x00000010 /* DISTINCT using indexes */
17068#define SQLITE_CoverIdxScan0x00000020 0x00000020 /* Covering index scans */
17069#define SQLITE_OrderByIdxJoin0x00000040 0x00000040 /* ORDER BY of joins via index */
17070#define SQLITE_Transitive0x00000080 0x00000080 /* Transitive constraints */
17071#define SQLITE_OmitNoopJoin0x00000100 0x00000100 /* Omit unused tables in joins */
17072#define SQLITE_CountOfView0x00000200 0x00000200 /* The count-of-view optimization */
17073#define SQLITE_CursorHints0x00000400 0x00000400 /* Add OP_CursorHint opcodes */
17074#define SQLITE_Stat40x00000800 0x00000800 /* Use STAT4 data */
17075 /* TH3 expects this value ^^^^^^^^^^ to be 0x0000800. Don't change it */
17076#define SQLITE_PushDown0x00001000 0x00001000 /* The push-down optimization */
17077#define SQLITE_SimplifyJoin0x00002000 0x00002000 /* Convert LEFT JOIN to JOIN */
17078#define SQLITE_SkipScan0x00004000 0x00004000 /* Skip-scans */
17079#define SQLITE_PropagateConst0x00008000 0x00008000 /* The constant propagation opt */
17080#define SQLITE_MinMaxOpt0x00010000 0x00010000 /* The min/max optimization */
17081#define SQLITE_SeekScan0x00020000 0x00020000 /* The OP_SeekScan optimization */
17082#define SQLITE_OmitOrderBy0x00040000 0x00040000 /* Omit pointless ORDER BY */
17083 /* TH3 expects this value ^^^^^^^^^^ to be 0x40000. Coordinate any change */
17084#define SQLITE_BloomFilter0x00080000 0x00080000 /* Use a Bloom filter on searches */
17085#define SQLITE_BloomPulldown0x00100000 0x00100000 /* Run Bloom filters early */
17086#define SQLITE_BalancedMerge0x00200000 0x00200000 /* Balance multi-way merges */
17087#define SQLITE_ReleaseReg0x00400000 0x00400000 /* Use OP_ReleaseReg for testing */
17088#define SQLITE_FlttnUnionAll0x00800000 0x00800000 /* Disable the UNION ALL flattener */
17089 /* TH3 expects this value ^^^^^^^^^^ See flatten04.test */
17090#define SQLITE_AllOpts0xffffffff 0xffffffff /* All optimizations */
17091
17092/*
17093** Macros for testing whether or not optimizations are enabled or disabled.
17094*/
17095#define OptimizationDisabled(db, mask)(((db)->dbOptFlags&(mask))!=0) (((db)->dbOptFlags&(mask))!=0)
17096#define OptimizationEnabled(db, mask)(((db)->dbOptFlags&(mask))==0) (((db)->dbOptFlags&(mask))==0)
17097
17098/*
17099** Return true if it OK to factor constant expressions into the initialization
17100** code. The argument is a Parse object for the code generator.
17101*/
17102#define ConstFactorOk(P)((P)->okConstFactor) ((P)->okConstFactor)
17103
17104/* Possible values for the sqlite3.eOpenState field.
17105** The numbers are randomly selected such that a minimum of three bits must
17106** change to convert any number to another or to zero
17107*/
17108#define SQLITE_STATE_OPEN0x76 0x76 /* Database is open */
17109#define SQLITE_STATE_CLOSED0xce 0xce /* Database is closed */
17110#define SQLITE_STATE_SICK0xba 0xba /* Error and awaiting close */
17111#define SQLITE_STATE_BUSY0x6d 0x6d /* Database currently in use */
17112#define SQLITE_STATE_ERROR0xd5 0xd5 /* An SQLITE_MISUSE error occurred */
17113#define SQLITE_STATE_ZOMBIE0xa7 0xa7 /* Close with last statement close */
17114
17115/*
17116** Each SQL function is defined by an instance of the following
17117** structure. For global built-in functions (ex: substr(), max(), count())
17118** a pointer to this structure is held in the sqlite3BuiltinFunctions object.
17119** For per-connection application-defined functions, a pointer to this
17120** structure is held in the db->aHash hash table.
17121**
17122** The u.pHash field is used by the global built-ins. The u.pDestructor
17123** field is used by per-connection app-def functions.
17124*/
17125struct FuncDef {
17126 i8 nArg; /* Number of arguments. -1 means unlimited */
17127 u32 funcFlags; /* Some combination of SQLITE_FUNC_* */
17128 void *pUserData; /* User data parameter */
17129 FuncDef *pNext; /* Next function with same name */
17130 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
17131 void (*xFinalize)(sqlite3_context*); /* Agg finalizer */
17132 void (*xValue)(sqlite3_context*); /* Current agg value */
17133 void (*xInverse)(sqlite3_context*,int,sqlite3_value**); /* inverse agg-step */
17134 const char *zName; /* SQL name of the function. */
17135 union {
17136 FuncDef *pHash; /* Next with a different name but the same hash */
17137 FuncDestructor *pDestructor; /* Reference counted destructor function */
17138 } u; /* pHash if SQLITE_FUNC_BUILTIN, pDestructor otherwise */
17139};
17140
17141/*
17142** This structure encapsulates a user-function destructor callback (as
17143** configured using create_function_v2()) and a reference counter. When
17144** create_function_v2() is called to create a function with a destructor,
17145** a single object of this type is allocated. FuncDestructor.nRef is set to
17146** the number of FuncDef objects created (either 1 or 3, depending on whether
17147** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
17148** member of each of the new FuncDef objects is set to point to the allocated
17149** FuncDestructor.
17150**
17151** Thereafter, when one of the FuncDef objects is deleted, the reference
17152** count on this object is decremented. When it reaches 0, the destructor
17153** is invoked and the FuncDestructor structure freed.
17154*/
17155struct FuncDestructor {
17156 int nRef;
17157 void (*xDestroy)(void *);
17158 void *pUserData;
17159};
17160
17161/*
17162** Possible values for FuncDef.flags. Note that the _LENGTH and _TYPEOF
17163** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG. And
17164** SQLITE_FUNC_CONSTANT must be the same as SQLITE_DETERMINISTIC. There
17165** are assert() statements in the code to verify this.
17166**
17167** Value constraints (enforced via assert()):
17168** SQLITE_FUNC_MINMAX == NC_MinMaxAgg == SF_MinMaxAgg
17169** SQLITE_FUNC_ANYORDER == NC_OrderAgg == SF_OrderByReqd
17170** SQLITE_FUNC_LENGTH == OPFLAG_LENGTHARG
17171** SQLITE_FUNC_TYPEOF == OPFLAG_TYPEOFARG
17172** SQLITE_FUNC_CONSTANT == SQLITE_DETERMINISTIC from the API
17173** SQLITE_FUNC_DIRECT == SQLITE_DIRECTONLY from the API
17174** SQLITE_FUNC_UNSAFE == SQLITE_INNOCUOUS
17175** SQLITE_FUNC_ENCMASK depends on SQLITE_UTF* macros in the API
17176*/
17177#define SQLITE_FUNC_ENCMASK0x0003 0x0003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */
17178#define SQLITE_FUNC_LIKE0x0004 0x0004 /* Candidate for the LIKE optimization */
17179#define SQLITE_FUNC_CASE0x0008 0x0008 /* Case-sensitive LIKE-type function */
17180#define SQLITE_FUNC_EPHEM0x0010 0x0010 /* Ephemeral. Delete with VDBE */
17181#define SQLITE_FUNC_NEEDCOLL0x0020 0x0020 /* sqlite3GetFuncCollSeq() might be called*/
17182#define SQLITE_FUNC_LENGTH0x0040 0x0040 /* Built-in length() function */
17183#define SQLITE_FUNC_TYPEOF0x0080 0x0080 /* Built-in typeof() function */
17184#define SQLITE_FUNC_COUNT0x0100 0x0100 /* Built-in count(*) aggregate */
17185/* 0x0200 -- available for reuse */
17186#define SQLITE_FUNC_UNLIKELY0x0400 0x0400 /* Built-in unlikely() function */
17187#define SQLITE_FUNC_CONSTANT0x0800 0x0800 /* Constant inputs give a constant output */
17188#define SQLITE_FUNC_MINMAX0x1000 0x1000 /* True for min() and max() aggregates */
17189#define SQLITE_FUNC_SLOCHNG0x2000 0x2000 /* "Slow Change". Value constant during a
17190 ** single query - might change over time */
17191#define SQLITE_FUNC_TEST0x4000 0x4000 /* Built-in testing functions */
17192/* 0x8000 -- available for reuse */
17193#define SQLITE_FUNC_WINDOW0x00010000 0x00010000 /* Built-in window-only function */
17194#define SQLITE_FUNC_INTERNAL0x00040000 0x00040000 /* For use by NestedParse() only */
17195#define SQLITE_FUNC_DIRECT0x00080000 0x00080000 /* Not for use in TRIGGERs or VIEWs */
17196#define SQLITE_FUNC_SUBTYPE0x00100000 0x00100000 /* Result likely to have sub-type */
17197#define SQLITE_FUNC_UNSAFE0x00200000 0x00200000 /* Function has side effects */
17198#define SQLITE_FUNC_INLINE0x00400000 0x00400000 /* Functions implemented in-line */
17199#define SQLITE_FUNC_BUILTIN0x00800000 0x00800000 /* This is a built-in function */
17200#define SQLITE_FUNC_ANYORDER0x08000000 0x08000000 /* count/min/max aggregate */
17201
17202/* Identifier numbers for each in-line function */
17203#define INLINEFUNC_coalesce0 0
17204#define INLINEFUNC_implies_nonnull_row1 1
17205#define INLINEFUNC_expr_implies_expr2 2
17206#define INLINEFUNC_expr_compare3 3
17207#define INLINEFUNC_affinity4 4
17208#define INLINEFUNC_iif5 5
17209#define INLINEFUNC_sqlite_offset6 6
17210#define INLINEFUNC_unlikely99 99 /* Default case */
17211
17212/*
17213** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
17214** used to create the initializers for the FuncDef structures.
17215**
17216** FUNCTION(zName, nArg, iArg, bNC, xFunc)
17217** Used to create a scalar function definition of a function zName
17218** implemented by C function xFunc that accepts nArg arguments. The
17219** value passed as iArg is cast to a (void*) and made available
17220** as the user-data (sqlite3_user_data()) for the function. If
17221** argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
17222**
17223** VFUNCTION(zName, nArg, iArg, bNC, xFunc)
17224** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag.
17225**
17226** SFUNCTION(zName, nArg, iArg, bNC, xFunc)
17227** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and
17228** adds the SQLITE_DIRECTONLY flag.
17229**
17230** INLINE_FUNC(zName, nArg, iFuncId, mFlags)
17231** zName is the name of a function that is implemented by in-line
17232** byte code rather than by the usual callbacks. The iFuncId
17233** parameter determines the function id. The mFlags parameter is
17234** optional SQLITE_FUNC_ flags for this function.
17235**
17236** TEST_FUNC(zName, nArg, iFuncId, mFlags)
17237** zName is the name of a test-only function implemented by in-line
17238** byte code rather than by the usual callbacks. The iFuncId
17239** parameter determines the function id. The mFlags parameter is
17240** optional SQLITE_FUNC_ flags for this function.
17241**
17242** DFUNCTION(zName, nArg, iArg, bNC, xFunc)
17243** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and
17244** adds the SQLITE_FUNC_SLOCHNG flag. Used for date & time functions
17245** and functions like sqlite_version() that can change, but not during
17246** a single query. The iArg is ignored. The user-data is always set
17247** to a NULL pointer. The bNC parameter is not used.
17248**
17249** MFUNCTION(zName, nArg, xPtr, xFunc)
17250** For math-library functions. xPtr is an arbitrary pointer.
17251**
17252** PURE_DATE(zName, nArg, iArg, bNC, xFunc)
17253** Used for "pure" date/time functions, this macro is like DFUNCTION
17254** except that it does set the SQLITE_FUNC_CONSTANT flags. iArg is
17255** ignored and the user-data for these functions is set to an
17256** arbitrary non-NULL pointer. The bNC parameter is not used.
17257**
17258** AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
17259** Used to create an aggregate function definition implemented by
17260** the C functions xStep and xFinal. The first four parameters
17261** are interpreted in the same way as the first 4 parameters to
17262** FUNCTION().
17263**
17264** WAGGREGATE(zName, nArg, iArg, xStep, xFinal, xValue, xInverse)
17265** Used to create an aggregate function definition implemented by
17266** the C functions xStep and xFinal. The first four parameters
17267** are interpreted in the same way as the first 4 parameters to
17268** FUNCTION().
17269**
17270** LIKEFUNC(zName, nArg, pArg, flags)
17271** Used to create a scalar function definition of a function zName
17272** that accepts nArg arguments and is implemented by a call to C
17273** function likeFunc. Argument pArg is cast to a (void *) and made
17274** available as the function user-data (sqlite3_user_data()). The
17275** FuncDef.flags variable is set to the value passed as the flags
17276** parameter.
17277*/
17278#define FUNCTION(zName, nArg, iArg, bNC, xFunc){nArg, 0x00800000| 0x0800|1|(bNC*0x0020), ((void*)(long int)(
iArg)), 0, xFunc, 0, 0, 0, "zName", {0} }
\
17279 {nArg, SQLITE_FUNC_BUILTIN0x00800000|\
17280 SQLITE_FUNC_CONSTANT0x0800|SQLITE_UTF81|(bNC*SQLITE_FUNC_NEEDCOLL0x0020), \
17281 SQLITE_INT_TO_PTR(iArg)((void*)(long int)(iArg)), 0, xFunc, 0, 0, 0, #zName, {0} }
17282#define VFUNCTION(zName, nArg, iArg, bNC, xFunc){nArg, 0x00800000|1|(bNC*0x0020), ((void*)(long int)(iArg)), 0
, xFunc, 0, 0, 0, "zName", {0} }
\
17283 {nArg, SQLITE_FUNC_BUILTIN0x00800000|SQLITE_UTF81|(bNC*SQLITE_FUNC_NEEDCOLL0x0020), \
17284 SQLITE_INT_TO_PTR(iArg)((void*)(long int)(iArg)), 0, xFunc, 0, 0, 0, #zName, {0} }
17285#define SFUNCTION(zName, nArg, iArg, bNC, xFunc){nArg, 0x00800000|1|0x000080000|0x00200000, ((void*)(long int
)(iArg)), 0, xFunc, 0, 0, 0, "zName", {0} }
\
17286 {nArg, SQLITE_FUNC_BUILTIN0x00800000|SQLITE_UTF81|SQLITE_DIRECTONLY0x000080000|SQLITE_FUNC_UNSAFE0x00200000, \
17287 SQLITE_INT_TO_PTR(iArg)((void*)(long int)(iArg)), 0, xFunc, 0, 0, 0, #zName, {0} }
17288#define MFUNCTION(zName, nArg, xPtr, xFunc){nArg, 0x00800000|0x0800|1, xPtr, 0, xFunc, 0, 0, 0, "zName",
{0} }
\
17289 {nArg, SQLITE_FUNC_BUILTIN0x00800000|SQLITE_FUNC_CONSTANT0x0800|SQLITE_UTF81, \
17290 xPtr, 0, xFunc, 0, 0, 0, #zName, {0} }
17291#define JFUNCTION(zName, nArg, iArg, xFunc){nArg, 0x00800000|0x000000800|0x000200000| 0x0800|1, ((void*)
(long int)(iArg)), 0, xFunc, 0, 0, 0, "zName", {0} }
\
17292 {nArg, SQLITE_FUNC_BUILTIN0x00800000|SQLITE_DETERMINISTIC0x000000800|SQLITE_INNOCUOUS0x000200000|\
17293 SQLITE_FUNC_CONSTANT0x0800|SQLITE_UTF81, \
17294 SQLITE_INT_TO_PTR(iArg)((void*)(long int)(iArg)), 0, xFunc, 0, 0, 0, #zName, {0} }
17295#define INLINE_FUNC(zName, nArg, iArg, mFlags){nArg, 0x00800000| 1|0x00400000|0x0800|(mFlags), ((void*)(long
int)(iArg)), 0, versionFunc, 0, 0, 0, "zName", {0} }
\
17296 {nArg, SQLITE_FUNC_BUILTIN0x00800000|\
17297 SQLITE_UTF81|SQLITE_FUNC_INLINE0x00400000|SQLITE_FUNC_CONSTANT0x0800|(mFlags), \
17298 SQLITE_INT_TO_PTR(iArg)((void*)(long int)(iArg)), 0, noopFuncversionFunc, 0, 0, 0, #zName, {0} }
17299#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} }
\
17300 {nArg, SQLITE_FUNC_BUILTIN0x00800000|\
17301 SQLITE_UTF81|SQLITE_FUNC_INTERNAL0x00040000|SQLITE_FUNC_TEST0x4000| \
17302 SQLITE_FUNC_INLINE0x00400000|SQLITE_FUNC_CONSTANT0x0800|(mFlags), \
17303 SQLITE_INT_TO_PTR(iArg)((void*)(long int)(iArg)), 0, noopFuncversionFunc, 0, 0, 0, #zName, {0} }
17304#define DFUNCTION(zName, nArg, iArg, bNC, xFunc){nArg, 0x00800000|0x2000|1, 0, 0, xFunc, 0, 0, 0, "zName", {0
} }
\
17305 {nArg, SQLITE_FUNC_BUILTIN0x00800000|SQLITE_FUNC_SLOCHNG0x2000|SQLITE_UTF81, \
17306 0, 0, xFunc, 0, 0, 0, #zName, {0} }
17307#define PURE_DATE(zName, nArg, iArg, bNC, xFunc){nArg, 0x00800000| 0x2000|1|0x0800, (void*)&sqlite3Config
, 0, xFunc, 0, 0, 0, "zName", {0} }
\
17308 {nArg, SQLITE_FUNC_BUILTIN0x00800000|\
17309 SQLITE_FUNC_SLOCHNG0x2000|SQLITE_UTF81|SQLITE_FUNC_CONSTANT0x0800, \
17310 (void*)&sqlite3Config, 0, xFunc, 0, 0, 0, #zName, {0} }
17311#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} }
\
17312 {nArg, SQLITE_FUNC_BUILTIN0x00800000|\
17313 SQLITE_FUNC_CONSTANT0x0800|SQLITE_UTF81|(bNC*SQLITE_FUNC_NEEDCOLL0x0020)|extraFlags,\
17314 SQLITE_INT_TO_PTR(iArg)((void*)(long int)(iArg)), 0, xFunc, 0, 0, 0, #zName, {0} }
17315#define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc){nArg, 0x00800000| 0x2000|1|(bNC*0x0020), pArg, 0, xFunc, 0, 0
, 0, "zName", }
\
17316 {nArg, SQLITE_FUNC_BUILTIN0x00800000|\
17317 SQLITE_FUNC_SLOCHNG0x2000|SQLITE_UTF81|(bNC*SQLITE_FUNC_NEEDCOLL0x0020), \
17318 pArg, 0, xFunc, 0, 0, 0, #zName, }
17319#define LIKEFUNC(zName, nArg, arg, flags){nArg, 0x00800000|0x0800|1|flags, (void *)arg, 0, likeFunc, 0
, 0, 0, "zName", {0} }
\
17320 {nArg, SQLITE_FUNC_BUILTIN0x00800000|SQLITE_FUNC_CONSTANT0x0800|SQLITE_UTF81|flags, \
17321 (void *)arg, 0, likeFunc, 0, 0, 0, #zName, {0} }
17322#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}}
\
17323 {nArg, SQLITE_FUNC_BUILTIN0x00800000|SQLITE_UTF81|(nc*SQLITE_FUNC_NEEDCOLL0x0020)|f, \
17324 SQLITE_INT_TO_PTR(arg)((void*)(long int)(arg)), 0, xStep,xFinal,xValue,xInverse,#zName, {0}}
17325#define INTERNAL_FUNCTION(zName, nArg, xFunc){nArg, 0x00800000| 0x00040000|1|0x0800, 0, 0, xFunc, 0, 0, 0,
"zName", {0} }
\
17326 {nArg, SQLITE_FUNC_BUILTIN0x00800000|\
17327 SQLITE_FUNC_INTERNAL0x00040000|SQLITE_UTF81|SQLITE_FUNC_CONSTANT0x0800, \
17328 0, 0, xFunc, 0, 0, 0, #zName, {0} }
17329
17330
17331/*
17332** All current savepoints are stored in a linked list starting at
17333** sqlite3.pSavepoint. The first element in the list is the most recently
17334** opened savepoint. Savepoints are added to the list by the vdbe
17335** OP_Savepoint instruction.
17336*/
17337struct Savepoint {
17338 char *zName; /* Savepoint name (nul-terminated) */
17339 i64 nDeferredCons; /* Number of deferred fk violations */
17340 i64 nDeferredImmCons; /* Number of deferred imm fk. */
17341 Savepoint *pNext; /* Parent savepoint (if any) */
17342};
17343
17344/*
17345** The following are used as the second parameter to sqlite3Savepoint(),
17346** and as the P1 argument to the OP_Savepoint instruction.
17347*/
17348#define SAVEPOINT_BEGIN0 0
17349#define SAVEPOINT_RELEASE1 1
17350#define SAVEPOINT_ROLLBACK2 2
17351
17352
17353/*
17354** Each SQLite module (virtual table definition) is defined by an
17355** instance of the following structure, stored in the sqlite3.aModule
17356** hash table.
17357*/
17358struct Module {
17359 const sqlite3_module *pModule; /* Callback pointers */
17360 const char *zName; /* Name passed to create_module() */
17361 int nRefModule; /* Number of pointers to this object */
17362 void *pAux; /* pAux passed to create_module() */
17363 void (*xDestroy)(void *); /* Module destructor function */
17364 Table *pEpoTab; /* Eponymous table for this module */
17365};
17366
17367/*
17368** Information about each column of an SQL table is held in an instance
17369** of the Column structure, in the Table.aCol[] array.
17370**
17371** Definitions:
17372**
17373** "table column index" This is the index of the column in the
17374** Table.aCol[] array, and also the index of
17375** the column in the original CREATE TABLE stmt.
17376**
17377** "storage column index" This is the index of the column in the
17378** record BLOB generated by the OP_MakeRecord
17379** opcode. The storage column index is less than
17380** or equal to the table column index. It is
17381** equal if and only if there are no VIRTUAL
17382** columns to the left.
17383**
17384** Notes on zCnName:
17385** The zCnName field stores the name of the column, the datatype of the
17386** column, and the collating sequence for the column, in that order, all in
17387** a single allocation. Each string is 0x00 terminated. The datatype
17388** is only included if the COLFLAG_HASTYPE bit of colFlags is set and the
17389** collating sequence name is only included if the COLFLAG_HASCOLL bit is
17390** set.
17391*/
17392struct Column {
17393 char *zCnName; /* Name of this column */
17394 unsigned notNull :4; /* An OE_ code for handling a NOT NULL constraint */
17395 unsigned eCType :4; /* One of the standard types */
17396 char affinity; /* One of the SQLITE_AFF_... values */
17397 u8 szEst; /* Est size of value in this column. sizeof(INT)==1 */
17398 u8 hName; /* Column name hash for faster lookup */
17399 u16 iDflt; /* 1-based index of DEFAULT. 0 means "none" */
17400 u16 colFlags; /* Boolean properties. See COLFLAG_ defines below */
17401};
17402
17403/* Allowed values for Column.eCType.
17404**
17405** Values must match entries in the global constant arrays
17406** sqlite3StdTypeLen[] and sqlite3StdType[]. Each value is one more
17407** than the offset into these arrays for the corresponding name.
17408** Adjust the SQLITE_N_STDTYPE value if adding or removing entries.
17409*/
17410#define COLTYPE_CUSTOM0 0 /* Type appended to zName */
17411#define COLTYPE_ANY1 1
17412#define COLTYPE_BLOB2 2
17413#define COLTYPE_INT3 3
17414#define COLTYPE_INTEGER4 4
17415#define COLTYPE_REAL5 5
17416#define COLTYPE_TEXT6 6
17417#define SQLITE_N_STDTYPE6 6 /* Number of standard types */
17418
17419/* Allowed values for Column.colFlags.
17420**
17421** Constraints:
17422** TF_HasVirtual == COLFLAG_VIRTUAL
17423** TF_HasStored == COLFLAG_STORED
17424** TF_HasHidden == COLFLAG_HIDDEN
17425*/
17426#define COLFLAG_PRIMKEY0x0001 0x0001 /* Column is part of the primary key */
17427#define COLFLAG_HIDDEN0x0002 0x0002 /* A hidden column in a virtual table */
17428#define COLFLAG_HASTYPE0x0004 0x0004 /* Type name follows column name */
17429#define COLFLAG_UNIQUE0x0008 0x0008 /* Column def contains "UNIQUE" or "PK" */
17430#define COLFLAG_SORTERREF0x0010 0x0010 /* Use sorter-refs with this column */
17431#define COLFLAG_VIRTUAL0x0020 0x0020 /* GENERATED ALWAYS AS ... VIRTUAL */
17432#define COLFLAG_STORED0x0040 0x0040 /* GENERATED ALWAYS AS ... STORED */
17433#define COLFLAG_NOTAVAIL0x0080 0x0080 /* STORED column not yet calculated */
17434#define COLFLAG_BUSY0x0100 0x0100 /* Blocks recursion on GENERATED columns */
17435#define COLFLAG_HASCOLL0x0200 0x0200 /* Has collating sequence name in zCnName */
17436#define COLFLAG_NOEXPAND0x0400 0x0400 /* Omit this column when expanding "*" */
17437#define COLFLAG_GENERATED0x0060 0x0060 /* Combo: _STORED, _VIRTUAL */
17438#define COLFLAG_NOINSERT0x0062 0x0062 /* Combo: _HIDDEN, _STORED, _VIRTUAL */
17439
17440/*
17441** A "Collating Sequence" is defined by an instance of the following
17442** structure. Conceptually, a collating sequence consists of a name and
17443** a comparison routine that defines the order of that sequence.
17444**
17445** If CollSeq.xCmp is NULL, it means that the
17446** collating sequence is undefined. Indices built on an undefined
17447** collating sequence may not be read or written.
17448*/
17449struct CollSeq {
17450 char *zName; /* Name of the collating sequence, UTF-8 encoded */
17451 u8 enc; /* Text encoding handled by xCmp() */
17452 void *pUser; /* First argument to xCmp() */
17453 int (*xCmp)(void*,int, const void*, int, const void*);
17454 void (*xDel)(void*); /* Destructor for pUser */
17455};
17456
17457/*
17458** A sort order can be either ASC or DESC.
17459*/
17460#define SQLITE_SO_ASC0 0 /* Sort in ascending order */
17461#define SQLITE_SO_DESC1 1 /* Sort in ascending order */
17462#define SQLITE_SO_UNDEFINED-1 -1 /* No sort order specified */
17463
17464/*
17465** Column affinity types.
17466**
17467** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
17468** 't' for SQLITE_AFF_TEXT. But we can save a little space and improve
17469** the speed a little by numbering the values consecutively.
17470**
17471** But rather than start with 0 or 1, we begin with 'A'. That way,
17472** when multiple affinity types are concatenated into a string and
17473** used as the P4 operand, they will be more readable.
17474**
17475** Note also that the numeric types are grouped together so that testing
17476** for a numeric type is a single comparison. And the BLOB type is first.
17477*/
17478#define SQLITE_AFF_NONE0x40 0x40 /* '@' */
17479#define SQLITE_AFF_BLOB0x41 0x41 /* 'A' */
17480#define SQLITE_AFF_TEXT0x42 0x42 /* 'B' */
17481#define SQLITE_AFF_NUMERIC0x43 0x43 /* 'C' */
17482#define SQLITE_AFF_INTEGER0x44 0x44 /* 'D' */
17483#define SQLITE_AFF_REAL0x45 0x45 /* 'E' */
17484
17485#define sqlite3IsNumericAffinity(X)((X)>=0x43) ((X)>=SQLITE_AFF_NUMERIC0x43)
17486
17487/*
17488** The SQLITE_AFF_MASK values masks off the significant bits of an
17489** affinity value.
17490*/
17491#define SQLITE_AFF_MASK0x47 0x47
17492
17493/*
17494** Additional bit values that can be ORed with an affinity without
17495** changing the affinity.
17496**
17497** The SQLITE_NOTNULL flag is a combination of NULLEQ and JUMPIFNULL.
17498** It causes an assert() to fire if either operand to a comparison
17499** operator is NULL. It is added to certain comparison operators to
17500** prove that the operands are always NOT NULL.
17501*/
17502#define SQLITE_JUMPIFNULL0x10 0x10 /* jumps if either operand is NULL */
17503#define SQLITE_NULLEQ0x80 0x80 /* NULL=NULL */
17504#define SQLITE_NOTNULL0x90 0x90 /* Assert that operands are never NULL */
17505
17506/*
17507** An object of this type is created for each virtual table present in
17508** the database schema.
17509**
17510** If the database schema is shared, then there is one instance of this
17511** structure for each database connection (sqlite3*) that uses the shared
17512** schema. This is because each database connection requires its own unique
17513** instance of the sqlite3_vtab* handle used to access the virtual table
17514** implementation. sqlite3_vtab* handles can not be shared between
17515** database connections, even when the rest of the in-memory database
17516** schema is shared, as the implementation often stores the database
17517** connection handle passed to it via the xConnect() or xCreate() method
17518** during initialization internally. This database connection handle may
17519** then be used by the virtual table implementation to access real tables
17520** within the database. So that they appear as part of the callers
17521** transaction, these accesses need to be made via the same database
17522** connection as that used to execute SQL operations on the virtual table.
17523**
17524** All VTable objects that correspond to a single table in a shared
17525** database schema are initially stored in a linked-list pointed to by
17526** the Table.pVTable member variable of the corresponding Table object.
17527** When an sqlite3_prepare() operation is required to access the virtual
17528** table, it searches the list for the VTable that corresponds to the
17529** database connection doing the preparing so as to use the correct
17530** sqlite3_vtab* handle in the compiled query.
17531**
17532** When an in-memory Table object is deleted (for example when the
17533** schema is being reloaded for some reason), the VTable objects are not
17534** deleted and the sqlite3_vtab* handles are not xDisconnect()ed
17535** immediately. Instead, they are moved from the Table.pVTable list to
17536** another linked list headed by the sqlite3.pDisconnect member of the
17537** corresponding sqlite3 structure. They are then deleted/xDisconnected
17538** next time a statement is prepared using said sqlite3*. This is done
17539** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
17540** Refer to comments above function sqlite3VtabUnlockList() for an
17541** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
17542** list without holding the corresponding sqlite3.mutex mutex.
17543**
17544** The memory for objects of this type is always allocated by
17545** sqlite3DbMalloc(), using the connection handle stored in VTable.db as
17546** the first argument.
17547*/
17548struct VTable {
17549 sqlite3 *db; /* Database connection associated with this table */
17550 Module *pMod; /* Pointer to module implementation */
17551 sqlite3_vtab *pVtab; /* Pointer to vtab instance */
17552 int nRef; /* Number of pointers to this structure */
17553 u8 bConstraint; /* True if constraints are supported */
17554 u8 eVtabRisk; /* Riskiness of allowing hacker access */
17555 int iSavepoint; /* Depth of the SAVEPOINT stack */
17556 VTable *pNext; /* Next in linked list (see above) */
17557};
17558
17559/* Allowed values for VTable.eVtabRisk
17560*/
17561#define SQLITE_VTABRISK_Low0 0
17562#define SQLITE_VTABRISK_Normal1 1
17563#define SQLITE_VTABRISK_High2 2
17564
17565/*
17566** The schema for each SQL table, virtual table, and view is represented
17567** in memory by an instance of the following structure.
17568*/
17569struct Table {
17570 char *zName; /* Name of the table or view */
17571 Column *aCol; /* Information about each column */
17572 Index *pIndex; /* List of SQL indexes on this table. */
17573 char *zColAff; /* String defining the affinity of each column */
17574 ExprList *pCheck; /* All CHECK constraints */
17575 /* ... also used as column name list in a VIEW */
17576 Pgno tnum; /* Root BTree page for this table */
17577 u32 nTabRef; /* Number of pointers to this Table */
17578 u32 tabFlags; /* Mask of TF_* values */
17579 i16 iPKey; /* If not negative, use aCol[iPKey] as the rowid */
17580 i16 nCol; /* Number of columns in this table */
17581 i16 nNVCol; /* Number of columns that are not VIRTUAL */
17582 LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */
17583 LogEst szTabRow; /* Estimated size of each table row in bytes */
17584#ifdef SQLITE_ENABLE_COSTMULT
17585 LogEst costMult; /* Cost multiplier for using this table */
17586#endif
17587 u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
17588 u8 eTabType; /* 0: normal, 1: virtual, 2: view */
17589 union {
17590 struct { /* Used by ordinary tables: */
17591 int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */
17592 FKey *pFKey; /* Linked list of all foreign keys in this table */
17593 ExprList *pDfltList; /* DEFAULT clauses on various columns.
17594 ** Or the AS clause for generated columns. */
17595 } tab;
17596 struct { /* Used by views: */
17597 Select *pSelect; /* View definition */
17598 } view;
17599 struct { /* Used by virtual tables only: */
17600 int nArg; /* Number of arguments to the module */
17601 char **azArg; /* 0: module 1: schema 2: vtab name 3...: args */
17602 VTable *p; /* List of VTable objects. */
17603 } vtab;
17604 } u;
17605 Trigger *pTrigger; /* List of triggers on this object */
17606 Schema *pSchema; /* Schema that contains this table */
17607};
17608
17609/*
17610** Allowed values for Table.tabFlags.
17611**
17612** TF_OOOHidden applies to tables or view that have hidden columns that are
17613** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING
17614** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden,
17615** the TF_OOOHidden attribute would apply in this case. Such tables require
17616** special handling during INSERT processing. The "OOO" means "Out Of Order".
17617**
17618** Constraints:
17619**
17620** TF_HasVirtual == COLFLAG_VIRTUAL
17621** TF_HasStored == COLFLAG_STORED
17622** TF_HasHidden == COLFLAG_HIDDEN
17623*/
17624#define TF_Readonly0x00000001 0x00000001 /* Read-only system table */
17625#define TF_HasHidden0x00000002 0x00000002 /* Has one or more hidden columns */
17626#define TF_HasPrimaryKey0x00000004 0x00000004 /* Table has a primary key */
17627#define TF_Autoincrement0x00000008 0x00000008 /* Integer primary key is autoincrement */
17628#define TF_HasStat10x00000010 0x00000010 /* nRowLogEst set from sqlite_stat1 */
17629#define TF_HasVirtual0x00000020 0x00000020 /* Has one or more VIRTUAL columns */
17630#define TF_HasStored0x00000040 0x00000040 /* Has one or more STORED columns */
17631#define TF_HasGenerated0x00000060 0x00000060 /* Combo: HasVirtual + HasStored */
17632#define TF_WithoutRowid0x00000080 0x00000080 /* No rowid. PRIMARY KEY is the key */
17633#define TF_StatsUsed0x00000100 0x00000100 /* Query planner decisions affected by
17634 ** Index.aiRowLogEst[] values */
17635#define TF_NoVisibleRowid0x00000200 0x00000200 /* No user-visible "rowid" column */
17636#define TF_OOOHidden0x00000400 0x00000400 /* Out-of-Order hidden columns */
17637#define TF_HasNotNull0x00000800 0x00000800 /* Contains NOT NULL constraints */
17638#define TF_Shadow0x00001000 0x00001000 /* True for a shadow table */
17639#define TF_HasStat40x00002000 0x00002000 /* STAT4 info available for this table */
17640#define TF_Ephemeral0x00004000 0x00004000 /* An ephemeral table */
17641#define TF_Eponymous0x00008000 0x00008000 /* An eponymous virtual table */
17642#define TF_Strict0x00010000 0x00010000 /* STRICT mode */
17643
17644/*
17645** Allowed values for Table.eTabType
17646*/
17647#define TABTYP_NORM0 0 /* Ordinary table */
17648#define TABTYP_VTAB1 1 /* Virtual table */
17649#define TABTYP_VIEW2 2 /* A view */
17650
17651#define IsView(X)((X)->eTabType==2) ((X)->eTabType==TABTYP_VIEW2)
17652#define IsOrdinaryTable(X)((X)->eTabType==0) ((X)->eTabType==TABTYP_NORM0)
17653
17654/*
17655** Test to see whether or not a table is a virtual table. This is
17656** done as a macro so that it will be optimized out when virtual
17657** table support is omitted from the build.
17658*/
17659#ifndef SQLITE_OMIT_VIRTUALTABLE
17660# define IsVirtual(X)((X)->eTabType==1) ((X)->eTabType==TABTYP_VTAB1)
17661# define ExprIsVtab(X)((X)->op==167 && (X)->y.pTab!=0 && (X)->
y.pTab->eTabType==1)
\
17662 ((X)->op==TK_COLUMN167 && (X)->y.pTab!=0 && (X)->y.pTab->eTabType==TABTYP_VTAB1)
17663#else
17664# define IsVirtual(X)((X)->eTabType==1) 0
17665# define ExprIsVtab(X)((X)->op==167 && (X)->y.pTab!=0 && (X)->
y.pTab->eTabType==1)
0
17666#endif
17667
17668/*
17669** Macros to determine if a column is hidden. IsOrdinaryHiddenColumn()
17670** only works for non-virtual tables (ordinary tables and views) and is
17671** always false unless SQLITE_ENABLE_HIDDEN_COLUMNS is defined. The
17672** IsHiddenColumn() macro is general purpose.
17673*/
17674#if defined(SQLITE_ENABLE_HIDDEN_COLUMNS)
17675# define IsHiddenColumn(X)(((X)->colFlags & 0x0002)!=0) (((X)->colFlags & COLFLAG_HIDDEN0x0002)!=0)
17676# define IsOrdinaryHiddenColumn(X)0 (((X)->colFlags & COLFLAG_HIDDEN0x0002)!=0)
17677#elif !defined(SQLITE_OMIT_VIRTUALTABLE)
17678# define IsHiddenColumn(X)(((X)->colFlags & 0x0002)!=0) (((X)->colFlags & COLFLAG_HIDDEN0x0002)!=0)
17679# define IsOrdinaryHiddenColumn(X)0 0
17680#else
17681# define IsHiddenColumn(X)(((X)->colFlags & 0x0002)!=0) 0
17682# define IsOrdinaryHiddenColumn(X)0 0
17683#endif
17684
17685
17686/* Does the table have a rowid */
17687#define HasRowid(X)(((X)->tabFlags & 0x00000080)==0) (((X)->tabFlags & TF_WithoutRowid0x00000080)==0)
17688#define VisibleRowid(X)(((X)->tabFlags & 0x00000200)==0) (((X)->tabFlags & TF_NoVisibleRowid0x00000200)==0)
17689
17690/*
17691** Each foreign key constraint is an instance of the following structure.
17692**
17693** A foreign key is associated with two tables. The "from" table is
17694** the table that contains the REFERENCES clause that creates the foreign
17695** key. The "to" table is the table that is named in the REFERENCES clause.
17696** Consider this example:
17697**
17698** CREATE TABLE ex1(
17699** a INTEGER PRIMARY KEY,
17700** b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
17701** );
17702**
17703** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
17704** Equivalent names:
17705**
17706** from-table == child-table
17707** to-table == parent-table
17708**
17709** Each REFERENCES clause generates an instance of the following structure
17710** which is attached to the from-table. The to-table need not exist when
17711** the from-table is created. The existence of the to-table is not checked.
17712**
17713** The list of all parents for child Table X is held at X.pFKey.
17714**
17715** A list of all children for a table named Z (which might not even exist)
17716** is held in Schema.fkeyHash with a hash key of Z.
17717*/
17718struct FKey {
17719 Table *pFrom; /* Table containing the REFERENCES clause (aka: Child) */
17720 FKey *pNextFrom; /* Next FKey with the same in pFrom. Next parent of pFrom */
17721 char *zTo; /* Name of table that the key points to (aka: Parent) */
17722 FKey *pNextTo; /* Next with the same zTo. Next child of zTo. */
17723 FKey *pPrevTo; /* Previous with the same zTo */
17724 int nCol; /* Number of columns in this key */
17725 /* EV: R-30323-21917 */
17726 u8 isDeferred; /* True if constraint checking is deferred till COMMIT */
17727 u8 aAction[2]; /* ON DELETE and ON UPDATE actions, respectively */
17728 Trigger *apTrigger[2];/* Triggers for aAction[] actions */
17729 struct sColMap { /* Mapping of columns in pFrom to columns in zTo */
17730 int iFrom; /* Index of column in pFrom */
17731 char *zCol; /* Name of column in zTo. If NULL use PRIMARY KEY */
17732 } aCol[1]; /* One entry for each of nCol columns */
17733};
17734
17735/*
17736** SQLite supports many different ways to resolve a constraint
17737** error. ROLLBACK processing means that a constraint violation
17738** causes the operation in process to fail and for the current transaction
17739** to be rolled back. ABORT processing means the operation in process
17740** fails and any prior changes from that one operation are backed out,
17741** but the transaction is not rolled back. FAIL processing means that
17742** the operation in progress stops and returns an error code. But prior
17743** changes due to the same operation are not backed out and no rollback
17744** occurs. IGNORE means that the particular row that caused the constraint
17745** error is not inserted or updated. Processing continues and no error
17746** is returned. REPLACE means that preexisting database rows that caused
17747** a UNIQUE constraint violation are removed so that the new insert or
17748** update can proceed. Processing continues and no error is reported.
17749** UPDATE applies to insert operations only and means that the insert
17750** is omitted and the DO UPDATE clause of an upsert is run instead.
17751**
17752** RESTRICT, SETNULL, SETDFLT, and CASCADE actions apply only to foreign keys.
17753** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
17754** same as ROLLBACK for DEFERRED keys. SETNULL means that the foreign
17755** key is set to NULL. SETDFLT means that the foreign key is set
17756** to its default value. CASCADE means that a DELETE or UPDATE of the
17757** referenced table row is propagated into the row that holds the
17758** foreign key.
17759**
17760** The OE_Default value is a place holder that means to use whatever
17761** conflict resolution algorthm is required from context.
17762**
17763** The following symbolic values are used to record which type
17764** of conflict resolution action to take.
17765*/
17766#define OE_None0 0 /* There is no constraint to check */
17767#define OE_Rollback1 1 /* Fail the operation and rollback the transaction */
17768#define OE_Abort2 2 /* Back out changes but do no rollback transaction */
17769#define OE_Fail3 3 /* Stop the operation but leave all prior changes */
17770#define OE_Ignore4 4 /* Ignore the error. Do not do the INSERT or UPDATE */
17771#define OE_Replace5 5 /* Delete existing record, then do INSERT or UPDATE */
17772#define OE_Update6 6 /* Process as a DO UPDATE in an upsert */
17773#define OE_Restrict7 7 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
17774#define OE_SetNull8 8 /* Set the foreign key value to NULL */
17775#define OE_SetDflt9 9 /* Set the foreign key value to its default */
17776#define OE_Cascade10 10 /* Cascade the changes */
17777#define OE_Default11 11 /* Do whatever the default action is */
17778
17779
17780/*
17781** An instance of the following structure is passed as the first
17782** argument to sqlite3VdbeKeyCompare and is used to control the
17783** comparison of the two index keys.
17784**
17785** Note that aSortOrder[] and aColl[] have nField+1 slots. There
17786** are nField slots for the columns of an index then one extra slot
17787** for the rowid at the end.
17788*/
17789struct KeyInfo {
17790 u32 nRef; /* Number of references to this KeyInfo object */
17791 u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
17792 u16 nKeyField; /* Number of key columns in the index */
17793 u16 nAllField; /* Total columns, including key plus others */
17794 sqlite3 *db; /* The database connection */
17795 u8 *aSortFlags; /* Sort order for each column. */
17796 CollSeq *aColl[1]; /* Collating sequence for each term of the key */
17797};
17798
17799/*
17800** Allowed bit values for entries in the KeyInfo.aSortFlags[] array.
17801*/
17802#define KEYINFO_ORDER_DESC0x01 0x01 /* DESC sort order */
17803#define KEYINFO_ORDER_BIGNULL0x02 0x02 /* NULL is larger than any other value */
17804
17805/*
17806** This object holds a record which has been parsed out into individual
17807** fields, for the purposes of doing a comparison.
17808**
17809** A record is an object that contains one or more fields of data.
17810** Records are used to store the content of a table row and to store
17811** the key of an index. A blob encoding of a record is created by
17812** the OP_MakeRecord opcode of the VDBE and is disassembled by the
17813** OP_Column opcode.
17814**
17815** An instance of this object serves as a "key" for doing a search on
17816** an index b+tree. The goal of the search is to find the entry that
17817** is closed to the key described by this object. This object might hold
17818** just a prefix of the key. The number of fields is given by
17819** pKeyInfo->nField.
17820**
17821** The r1 and r2 fields are the values to return if this key is less than
17822** or greater than a key in the btree, respectively. These are normally
17823** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree
17824** is in DESC order.
17825**
17826** The key comparison functions actually return default_rc when they find
17827** an equals comparison. default_rc can be -1, 0, or +1. If there are
17828** multiple entries in the b-tree with the same key (when only looking
17829** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
17830** cause the search to find the last match, or +1 to cause the search to
17831** find the first match.
17832**
17833** The key comparison functions will set eqSeen to true if they ever
17834** get and equal results when comparing this structure to a b-tree record.
17835** When default_rc!=0, the search might end up on the record immediately
17836** before the first match or immediately after the last match. The
17837** eqSeen field will indicate whether or not an exact match exists in the
17838** b-tree.
17839*/
17840struct UnpackedRecord {
17841 KeyInfo *pKeyInfo; /* Collation and sort-order information */
17842 Mem *aMem; /* Values */
17843 union {
17844 char *z; /* Cache of aMem[0].z for vdbeRecordCompareString() */
17845 i64 i; /* Cache of aMem[0].u.i for vdbeRecordCompareInt() */
17846 } u;
17847 int n; /* Cache of aMem[0].n used by vdbeRecordCompareString() */
17848 u16 nField; /* Number of entries in apMem[] */
17849 i8 default_rc; /* Comparison result if keys are equal */
17850 u8 errCode; /* Error detected by xRecordCompare (CORRUPT or NOMEM) */
17851 i8 r1; /* Value to return if (lhs < rhs) */
17852 i8 r2; /* Value to return if (lhs > rhs) */
17853 u8 eqSeen; /* True if an equality comparison has been seen */
17854};
17855
17856
17857/*
17858** Each SQL index is represented in memory by an
17859** instance of the following structure.
17860**
17861** The columns of the table that are to be indexed are described
17862** by the aiColumn[] field of this structure. For example, suppose
17863** we have the following table and index:
17864**
17865** CREATE TABLE Ex1(c1 int, c2 int, c3 text);
17866** CREATE INDEX Ex2 ON Ex1(c3,c1);
17867**
17868** In the Table structure describing Ex1, nCol==3 because there are
17869** three columns in the table. In the Index structure describing
17870** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
17871** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the
17872** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
17873** The second column to be indexed (c1) has an index of 0 in
17874** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
17875**
17876** The Index.onError field determines whether or not the indexed columns
17877** must be unique and what to do if they are not. When Index.onError=OE_None,
17878** it means this is not a unique index. Otherwise it is a unique index
17879** and the value of Index.onError indicate the which conflict resolution
17880** algorithm to employ whenever an attempt is made to insert a non-unique
17881** element.
17882**
17883** While parsing a CREATE TABLE or CREATE INDEX statement in order to
17884** generate VDBE code (as opposed to parsing one read from an sqlite_schema
17885** table as part of parsing an existing database schema), transient instances
17886** of this structure may be created. In this case the Index.tnum variable is
17887** used to store the address of a VDBE instruction, not a database page
17888** number (it cannot - the database page is not allocated until the VDBE
17889** program is executed). See convertToWithoutRowidTable() for details.
17890*/
17891struct Index {
17892 char *zName; /* Name of this index */
17893 i16 *aiColumn; /* Which columns are used by this index. 1st is 0 */
17894 LogEst *aiRowLogEst; /* From ANALYZE: Est. rows selected by each column */
17895 Table *pTable; /* The SQL table being indexed */
17896 char *zColAff; /* String defining the affinity of each column */
17897 Index *pNext; /* The next index associated with the same table */
17898 Schema *pSchema; /* Schema containing this index */
17899 u8 *aSortOrder; /* for each column: True==DESC, False==ASC */
17900 const char **azColl; /* Array of collation sequence names for index */
17901 Expr *pPartIdxWhere; /* WHERE clause for partial indices */
17902 ExprList *aColExpr; /* Column expressions */
17903 Pgno tnum; /* DB Page containing root of this index */
17904 LogEst szIdxRow; /* Estimated average row size in bytes */
17905 u16 nKeyCol; /* Number of columns forming the key */
17906 u16 nColumn; /* Number of columns stored in the index */
17907 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
17908 unsigned idxType:2; /* 0:Normal 1:UNIQUE, 2:PRIMARY KEY, 3:IPK */
17909 unsigned bUnordered:1; /* Use this index for == or IN queries only */
17910 unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
17911 unsigned isResized:1; /* True if resizeIndexObject() has been called */
17912 unsigned isCovering:1; /* True if this is a covering index */
17913 unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
17914 unsigned hasStat1:1; /* aiRowLogEst values come from sqlite_stat1 */
17915 unsigned bNoQuery:1; /* Do not use this index to optimize queries */
17916 unsigned bAscKeyBug:1; /* True if the bba7b69f9849b5bf bug applies */
17917 unsigned bHasVCol:1; /* Index references one or more VIRTUAL columns */
17918#ifdef SQLITE_ENABLE_STAT4
17919 int nSample; /* Number of elements in aSample[] */
17920 int nSampleCol; /* Size of IndexSample.anEq[] and so on */
17921 tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */
17922 IndexSample *aSample; /* Samples of the left-most key */
17923 tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
17924 tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
17925#endif
17926 Bitmask colNotIdxed; /* 0 for unindexed columns in pTab */
17927};
17928
17929/*
17930** Allowed values for Index.idxType
17931*/
17932#define SQLITE_IDXTYPE_APPDEF0 0 /* Created using CREATE INDEX */
17933#define SQLITE_IDXTYPE_UNIQUE1 1 /* Implements a UNIQUE constraint */
17934#define SQLITE_IDXTYPE_PRIMARYKEY2 2 /* Is the PRIMARY KEY for the table */
17935#define SQLITE_IDXTYPE_IPK3 3 /* INTEGER PRIMARY KEY index */
17936
17937/* Return true if index X is a PRIMARY KEY index */
17938#define IsPrimaryKeyIndex(X)((X)->idxType==2) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY2)
17939
17940/* Return true if index X is a UNIQUE index */
17941#define IsUniqueIndex(X)((X)->onError!=0) ((X)->onError!=OE_None0)
17942
17943/* The Index.aiColumn[] values are normally positive integer. But
17944** there are some negative values that have special meaning:
17945*/
17946#define XN_ROWID(-1) (-1) /* Indexed column is the rowid */
17947#define XN_EXPR(-2) (-2) /* Indexed column is an expression */
17948
17949/*
17950** Each sample stored in the sqlite_stat4 table is represented in memory
17951** using a structure of this type. See documentation at the top of the
17952** analyze.c source file for additional information.
17953*/
17954struct IndexSample {
17955 void *p; /* Pointer to sampled record */
17956 int n; /* Size of record in bytes */
17957 tRowcnt *anEq; /* Est. number of rows where the key equals this sample */
17958 tRowcnt *anLt; /* Est. number of rows where key is less than this sample */
17959 tRowcnt *anDLt; /* Est. number of distinct keys less than this sample */
17960};
17961
17962/*
17963** Possible values to use within the flags argument to sqlite3GetToken().
17964*/
17965#define SQLITE_TOKEN_QUOTED0x1 0x1 /* Token is a quoted identifier. */
17966#define SQLITE_TOKEN_KEYWORD0x2 0x2 /* Token is a keyword. */
17967
17968/*
17969** Each token coming out of the lexer is an instance of
17970** this structure. Tokens are also used as part of an expression.
17971**
17972** The memory that "z" points to is owned by other objects. Take care
17973** that the owner of the "z" string does not deallocate the string before
17974** the Token goes out of scope! Very often, the "z" points to some place
17975** in the middle of the Parse.zSql text. But it might also point to a
17976** static string.
17977*/
17978struct Token {
17979 const char *z; /* Text of the token. Not NULL-terminated! */
17980 unsigned int n; /* Number of characters in this token */
17981};
17982
17983/*
17984** An instance of this structure contains information needed to generate
17985** code for a SELECT that contains aggregate functions.
17986**
17987** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
17988** pointer to this structure. The Expr.iAgg field is the index in
17989** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
17990** code for that node.
17991**
17992** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
17993** original Select structure that describes the SELECT statement. These
17994** fields do not need to be freed when deallocating the AggInfo structure.
17995*/
17996struct AggInfo {
17997 u8 directMode; /* Direct rendering mode means take data directly
17998 ** from source tables rather than from accumulators */
17999 u8 useSortingIdx; /* In direct mode, reference the sorting index rather
18000 ** than the source table */
18001 int sortingIdx; /* Cursor number of the sorting index */
18002 int sortingIdxPTab; /* Cursor number of pseudo-table */
18003 int nSortingColumn; /* Number of columns in the sorting index */
18004 int mnReg, mxReg; /* Range of registers allocated for aCol and aFunc */
18005 ExprList *pGroupBy; /* The group by clause */
18006 struct AggInfo_col { /* For each column used in source tables */
18007 Table *pTab; /* Source table */
18008 Expr *pCExpr; /* The original expression */
18009 int iTable; /* Cursor number of the source table */
18010 int iMem; /* Memory location that acts as accumulator */
18011 i16 iColumn; /* Column number within the source table */
18012 i16 iSorterColumn; /* Column number in the sorting index */
18013 } *aCol;
18014 int nColumn; /* Number of used entries in aCol[] */
18015 int nAccumulator; /* Number of columns that show through to the output.
18016 ** Additional columns are used only as parameters to
18017 ** aggregate functions */
18018 struct AggInfo_func { /* For each aggregate function */
18019 Expr *pFExpr; /* Expression encoding the function */
18020 FuncDef *pFunc; /* The aggregate function implementation */
18021 int iMem; /* Memory location that acts as accumulator */
18022 int iDistinct; /* Ephemeral table used to enforce DISTINCT */
18023 int iDistAddr; /* Address of OP_OpenEphemeral */
18024 } *aFunc;
18025 int nFunc; /* Number of entries in aFunc[] */
18026 u32 selId; /* Select to which this AggInfo belongs */
18027};
18028
18029/*
18030** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
18031** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
18032** than 32767 we have to make it 32-bit. 16-bit is preferred because
18033** it uses less memory in the Expr object, which is a big memory user
18034** in systems with lots of prepared statements. And few applications
18035** need more than about 10 or 20 variables. But some extreme users want
18036** to have prepared statements with over 32766 variables, and for them
18037** the option is available (at compile-time).
18038*/
18039#if SQLITE_MAX_VARIABLE_NUMBER500000<32767
18040typedef i16 ynVar;
18041#else
18042typedef int ynVar;
18043#endif
18044
18045/*
18046** Each node of an expression in the parse tree is an instance
18047** of this structure.
18048**
18049** Expr.op is the opcode. The integer parser token codes are reused
18050** as opcodes here. For example, the parser defines TK_GE to be an integer
18051** code representing the ">=" operator. This same integer code is reused
18052** to represent the greater-than-or-equal-to operator in the expression
18053** tree.
18054**
18055** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB,
18056** or TK_STRING), then Expr.u.zToken contains the text of the SQL literal. If
18057** the expression is a variable (TK_VARIABLE), then Expr.u.zToken contains the
18058** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
18059** then Expr.u.zToken contains the name of the function.
18060**
18061** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
18062** binary operator. Either or both may be NULL.
18063**
18064** Expr.x.pList is a list of arguments if the expression is an SQL function,
18065** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
18066** Expr.x.pSelect is used if the expression is a sub-select or an expression of
18067** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
18068** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is
18069** valid.
18070**
18071** An expression of the form ID or ID.ID refers to a column in a table.
18072** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
18073** the integer cursor number of a VDBE cursor pointing to that table and
18074** Expr.iColumn is the column number for the specific column. If the
18075** expression is used as a result in an aggregate SELECT, then the
18076** value is also stored in the Expr.iAgg column in the aggregate so that
18077** it can be accessed after all aggregates are computed.
18078**
18079** If the expression is an unbound variable marker (a question mark
18080** character '?' in the original SQL) then the Expr.iTable holds the index
18081** number for that variable.
18082**
18083** If the expression is a subquery then Expr.iColumn holds an integer
18084** register number containing the result of the subquery. If the
18085** subquery gives a constant result, then iTable is -1. If the subquery
18086** gives a different answer at different times during statement processing
18087** then iTable is the address of a subroutine that computes the subquery.
18088**
18089** If the Expr is of type OP_Column, and the table it is selecting from
18090** is a disk table or the "old.*" pseudo-table, then pTab points to the
18091** corresponding table definition.
18092**
18093** ALLOCATION NOTES:
18094**
18095** Expr objects can use a lot of memory space in database schema. To
18096** help reduce memory requirements, sometimes an Expr object will be
18097** truncated. And to reduce the number of memory allocations, sometimes
18098** two or more Expr objects will be stored in a single memory allocation,
18099** together with Expr.u.zToken strings.
18100**
18101** If the EP_Reduced and EP_TokenOnly flags are set when
18102** an Expr object is truncated. When EP_Reduced is set, then all
18103** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
18104** are contained within the same memory allocation. Note, however, that
18105** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
18106** allocated, regardless of whether or not EP_Reduced is set.
18107*/
18108struct Expr {
18109 u8 op; /* Operation performed by this node */
18110 char affExpr; /* affinity, or RAISE type */
18111 u8 op2; /* TK_REGISTER/TK_TRUTH: original value of Expr.op
18112 ** TK_COLUMN: the value of p5 for OP_Column
18113 ** TK_AGG_FUNCTION: nesting depth
18114 ** TK_FUNCTION: NC_SelfRef flag if needs OP_PureFunc */
18115#ifdef SQLITE_DEBUG
18116 u8 vvaFlags; /* Verification flags. */
18117#endif
18118 u32 flags; /* Various flags. EP_* See below */
18119 union {
18120 char *zToken; /* Token value. Zero terminated and dequoted */
18121 int iValue; /* Non-negative integer value if EP_IntValue */
18122 } u;
18123
18124 /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
18125 ** space is allocated for the fields below this point. An attempt to
18126 ** access them will result in a segfault or malfunction.
18127 *********************************************************************/
18128
18129 Expr *pLeft; /* Left subnode */
18130 Expr *pRight; /* Right subnode */
18131 union {
18132 ExprList *pList; /* op = IN, EXISTS, SELECT, CASE, FUNCTION, BETWEEN */
18133 Select *pSelect; /* EP_xIsSelect and op = IN, EXISTS, SELECT */
18134 } x;
18135
18136 /* If the EP_Reduced flag is set in the Expr.flags mask, then no
18137 ** space is allocated for the fields below this point. An attempt to
18138 ** access them will result in a segfault or malfunction.
18139 *********************************************************************/
18140
18141#if SQLITE_MAX_EXPR_DEPTH1000>0
18142 int nHeight; /* Height of the tree headed by this node */
18143#endif
18144 int iTable; /* TK_COLUMN: cursor number of table holding column
18145 ** TK_REGISTER: register number
18146 ** TK_TRIGGER: 1 -> new, 0 -> old
18147 ** EP_Unlikely: 134217728 times likelihood
18148 ** TK_IN: ephemerial table holding RHS
18149 ** TK_SELECT_COLUMN: Number of columns on the LHS
18150 ** TK_SELECT: 1st register of result vector */
18151 ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
18152 ** TK_VARIABLE: variable number (always >= 1).
18153 ** TK_SELECT_COLUMN: column of the result vector */
18154 i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
18155 union {
18156 int iJoin; /* If EP_OuterON or EP_InnerON, the right table */
18157 int iOfst; /* else: start of token from start of statement */
18158 } w;
18159 AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
18160 union {
18161 Table *pTab; /* TK_COLUMN: Table containing column. Can be NULL
18162 ** for a column of an index on an expression */
18163 Window *pWin; /* EP_WinFunc: Window/Filter defn for a function */
18164 struct { /* TK_IN, TK_SELECT, and TK_EXISTS */
18165 int iAddr; /* Subroutine entry address */
18166 int regReturn; /* Register used to hold return address */
18167 } sub;
18168 } y;
18169};
18170
18171/* The following are the meanings of bits in the Expr.flags field.
18172** Value restrictions:
18173**
18174** EP_Agg == NC_HasAgg == SF_HasAgg
18175** EP_Win == NC_HasWin
18176*/
18177#define EP_OuterON0x000001 0x000001 /* Originates in ON/USING clause of outer join */
18178#define EP_InnerON0x000002 0x000002 /* Originates in ON/USING of an inner join */
18179#define EP_Distinct0x000004 0x000004 /* Aggregate function with DISTINCT keyword */
18180#define EP_HasFunc0x000008 0x000008 /* Contains one or more functions of any kind */
18181#define EP_Agg0x000010 0x000010 /* Contains one or more aggregate functions */
18182#define EP_FixedCol0x000020 0x000020 /* TK_Column with a known fixed value */
18183#define EP_VarSelect0x000040 0x000040 /* pSelect is correlated, not constant */
18184#define EP_DblQuoted0x000080 0x000080 /* token.z was originally in "..." */
18185#define EP_InfixFunc0x000100 0x000100 /* True for an infix function: LIKE, GLOB, etc */
18186#define EP_Collate0x000200 0x000200 /* Tree contains a TK_COLLATE operator */
18187#define EP_Commuted0x000400 0x000400 /* Comparison operator has been commuted */
18188#define EP_IntValue0x000800 0x000800 /* Integer value contained in u.iValue */
18189#define EP_xIsSelect0x001000 0x001000 /* x.pSelect is valid (otherwise x.pList is) */
18190#define EP_Skip0x002000 0x002000 /* Operator does not contribute to affinity */
18191#define EP_Reduced0x004000 0x004000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
18192#define EP_Win0x008000 0x008000 /* Contains window functions */
18193#define EP_TokenOnly0x010000 0x010000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
18194#define EP_MemToken0x020000 0x020000 /* Need to sqlite3DbFree() Expr.zToken */
18195#define EP_IfNullRow0x040000 0x040000 /* The TK_IF_NULL_ROW opcode */
18196#define EP_Unlikely0x080000 0x080000 /* unlikely() or likelihood() function */
18197#define EP_ConstFunc0x100000 0x100000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
18198#define EP_CanBeNull0x200000 0x200000 /* Can be null despite NOT NULL constraint */
18199#define EP_Subquery0x400000 0x400000 /* Tree contains a TK_SELECT operator */
18200#define EP_Leaf0x800000 0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
18201#define EP_WinFunc0x1000000 0x1000000 /* TK_FUNCTION with Expr.y.pWin set */
18202#define EP_Subrtn0x2000000 0x2000000 /* Uses Expr.y.sub. TK_IN, _SELECT, or _EXISTS */
18203#define EP_Quoted0x4000000 0x4000000 /* TK_ID was originally quoted */
18204#define EP_Static0x8000000 0x8000000 /* Held in memory not obtained from malloc() */
18205#define EP_IsTrue0x10000000 0x10000000 /* Always has boolean value of TRUE */
18206#define EP_IsFalse0x20000000 0x20000000 /* Always has boolean value of FALSE */
18207#define EP_FromDDL0x40000000 0x40000000 /* Originates from sqlite_schema */
18208 /* 0x80000000 // Available */
18209
18210/* The EP_Propagate mask is a set of properties that automatically propagate
18211** upwards into parent nodes.
18212*/
18213#define EP_Propagate(0x000200|0x400000|0x000008) (EP_Collate0x000200|EP_Subquery0x400000|EP_HasFunc0x000008)
18214
18215/* Macros can be used to test, set, or clear bits in the
18216** Expr.flags field.
18217*/
18218#define ExprHasProperty(E,P)(((E)->flags&(P))!=0) (((E)->flags&(P))!=0)
18219#define ExprHasAllProperty(E,P)(((E)->flags&(P))==(P)) (((E)->flags&(P))==(P))
18220#define ExprSetProperty(E,P)(E)->flags|=(P) (E)->flags|=(P)
18221#define ExprClearProperty(E,P)(E)->flags&=~(P) (E)->flags&=~(P)
18222#define ExprAlwaysTrue(E)(((E)->flags&(0x000001|0x10000000))==0x10000000) (((E)->flags&(EP_OuterON0x000001|EP_IsTrue0x10000000))==EP_IsTrue0x10000000)
18223#define ExprAlwaysFalse(E)(((E)->flags&(0x000001|0x20000000))==0x20000000) (((E)->flags&(EP_OuterON0x000001|EP_IsFalse0x20000000))==EP_IsFalse0x20000000)
18224
18225/* Macros used to ensure that the correct members of unions are accessed
18226** in Expr.
18227*/
18228#define ExprUseUToken(E)(((E)->flags&0x000800)==0) (((E)->flags&EP_IntValue0x000800)==0)
18229#define ExprUseUValue(E)(((E)->flags&0x000800)!=0) (((E)->flags&EP_IntValue0x000800)!=0)
18230#define ExprUseXList(E)(((E)->flags&0x001000)==0) (((E)->flags&EP_xIsSelect0x001000)==0)
18231#define ExprUseXSelect(E)(((E)->flags&0x001000)!=0) (((E)->flags&EP_xIsSelect0x001000)!=0)
18232#define ExprUseYTab(E)(((E)->flags&(0x1000000|0x2000000))==0) (((E)->flags&(EP_WinFunc0x1000000|EP_Subrtn0x2000000))==0)
18233#define ExprUseYWin(E)(((E)->flags&0x1000000)!=0) (((E)->flags&EP_WinFunc0x1000000)!=0)
18234#define ExprUseYSub(E)(((E)->flags&0x2000000)!=0) (((E)->flags&EP_Subrtn0x2000000)!=0)
18235
18236/* Flags for use with Expr.vvaFlags
18237*/
18238#define EP_NoReduce0x01 0x01 /* Cannot EXPRDUP_REDUCE this Expr */
18239#define EP_Immutable0x02 0x02 /* Do not change this Expr node */
18240
18241/* The ExprSetVVAProperty() macro is used for Verification, Validation,
18242** and Accreditation only. It works like ExprSetProperty() during VVA
18243** processes but is a no-op for delivery.
18244*/
18245#ifdef SQLITE_DEBUG
18246# define ExprSetVVAProperty(E,P) (E)->vvaFlags|=(P)
18247# define ExprHasVVAProperty(E,P)0 (((E)->vvaFlags&(P))!=0)
18248# define ExprClearVVAProperties(E) (E)->vvaFlags = 0
18249#else
18250# define ExprSetVVAProperty(E,P)
18251# define ExprHasVVAProperty(E,P)0 0
18252# define ExprClearVVAProperties(E)
18253#endif
18254
18255/*
18256** Macros to determine the number of bytes required by a normal Expr
18257** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
18258** and an Expr struct with the EP_TokenOnly flag set.
18259*/
18260#define EXPR_FULLSIZEsizeof(Expr) sizeof(Expr) /* Full size */
18261#define EXPR_REDUCEDSIZE__builtin_offsetof(Expr, iTable) offsetof(Expr,iTable)__builtin_offsetof(Expr, iTable) /* Common features */
18262#define EXPR_TOKENONLYSIZE__builtin_offsetof(Expr, pLeft) offsetof(Expr,pLeft)__builtin_offsetof(Expr, pLeft) /* Fewer features */
18263
18264/*
18265** Flags passed to the sqlite3ExprDup() function. See the header comment
18266** above sqlite3ExprDup() for details.
18267*/
18268#define EXPRDUP_REDUCE0x0001 0x0001 /* Used reduced-size Expr nodes */
18269
18270/*
18271** True if the expression passed as an argument was a function with
18272** an OVER() clause (a window function).
18273*/
18274#ifdef SQLITE_OMIT_WINDOWFUNC
18275# define IsWindowFunc(p)( ((((p))->flags&(0x1000000))!=0) && p->y.pWin
->eFrmType!=166 )
0
18276#else
18277# define IsWindowFunc(p)( ((((p))->flags&(0x1000000))!=0) && p->y.pWin
->eFrmType!=166 )
( \
18278 ExprHasProperty((p), EP_WinFunc)((((p))->flags&(0x1000000))!=0) && p->y.pWin->eFrmType!=TK_FILTER166 \
18279 )
18280#endif
18281
18282/*
18283** A list of expressions. Each expression may optionally have a
18284** name. An expr/name combination can be used in several ways, such
18285** as the list of "expr AS ID" fields following a "SELECT" or in the
18286** list of "ID = expr" items in an UPDATE. A list of expressions can
18287** also be used as the argument to a function, in which case the a.zName
18288** field is not used.
18289**
18290** In order to try to keep memory usage down, the Expr.a.zEName field
18291** is used for multiple purposes:
18292**
18293** eEName Usage
18294** ---------- -------------------------
18295** ENAME_NAME (1) the AS of result set column
18296** (2) COLUMN= of an UPDATE
18297**
18298** ENAME_TAB DB.TABLE.NAME used to resolve names
18299** of subqueries
18300**
18301** ENAME_SPAN Text of the original result set
18302** expression.
18303*/
18304struct ExprList {
18305 int nExpr; /* Number of expressions on the list */
18306 int nAlloc; /* Number of a[] slots allocated */
18307 struct ExprList_item { /* For each expression in the list */
18308 Expr *pExpr; /* The parse tree for this expression */
18309 char *zEName; /* Token associated with this expression */
18310 struct {
18311 u8 sortFlags; /* Mask of KEYINFO_ORDER_* flags */
18312 unsigned eEName :2; /* Meaning of zEName */
18313 unsigned done :1; /* Indicates when processing is finished */
18314 unsigned reusable :1; /* Constant expression is reusable */
18315 unsigned bSorterRef :1; /* Defer evaluation until after sorting */
18316 unsigned bNulls :1; /* True if explicit "NULLS FIRST/LAST" */
18317 unsigned bUsed :1; /* This column used in a SF_NestedFrom subquery */
18318 unsigned bUsingTerm:1; /* Term from the USING clause of a NestedFrom */
18319 unsigned bNoExpand: 1; /* Term is an auxiliary in NestedFrom and should
18320 ** not be expanded by "*" in parent queries */
18321 } fg;
18322 union {
18323 struct { /* Used by any ExprList other than Parse.pConsExpr */
18324 u16 iOrderByCol; /* For ORDER BY, column number in result set */
18325 u16 iAlias; /* Index into Parse.aAlias[] for zName */
18326 } x;
18327 int iConstExprReg; /* Register in which Expr value is cached. Used only
18328 ** by Parse.pConstExpr */
18329 } u;
18330 } a[1]; /* One slot for each expression in the list */
18331};
18332
18333/*
18334** Allowed values for Expr.a.eEName
18335*/
18336#define ENAME_NAME0 0 /* The AS clause of a result set */
18337#define ENAME_SPAN1 1 /* Complete text of the result set expression */
18338#define ENAME_TAB2 2 /* "DB.TABLE.NAME" for the result set */
18339
18340/*
18341** An instance of this structure can hold a simple list of identifiers,
18342** such as the list "a,b,c" in the following statements:
18343**
18344** INSERT INTO t(a,b,c) VALUES ...;
18345** CREATE INDEX idx ON t(a,b,c);
18346** CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
18347**
18348** The IdList.a.idx field is used when the IdList represents the list of
18349** column names after a table name in an INSERT statement. In the statement
18350**
18351** INSERT INTO t(a,b,c) ...
18352**
18353** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
18354*/
18355struct IdList {
18356 int nId; /* Number of identifiers on the list */
18357 u8 eU4; /* Which element of a.u4 is valid */
18358 struct IdList_item {
18359 char *zName; /* Name of the identifier */
18360 union {
18361 int idx; /* Index in some Table.aCol[] of a column named zName */
18362 Expr *pExpr; /* Expr to implement a USING variable -- NOT USED */
18363 } u4;
18364 } a[1];
18365};
18366
18367/*
18368** Allowed values for IdList.eType, which determines which value of the a.u4
18369** is valid.
18370*/
18371#define EU4_NONE0 0 /* Does not use IdList.a.u4 */
18372#define EU4_IDX1 1 /* Uses IdList.a.u4.idx */
18373#define EU4_EXPR2 2 /* Uses IdList.a.u4.pExpr -- NOT CURRENTLY USED */
18374
18375/*
18376** The SrcItem object represents a single term in the FROM clause of a query.
18377** The SrcList object is mostly an array of SrcItems.
18378**
18379** Union member validity:
18380**
18381** u1.zIndexedBy fg.isIndexedBy && !fg.isTabFunc
18382** u1.pFuncArg fg.isTabFunc && !fg.isIndexedBy
18383** u2.pIBIndex fg.isIndexedBy && !fg.isCte
18384** u2.pCteUse fg.isCte && !fg.isIndexedBy
18385*/
18386struct SrcItem {
18387 Schema *pSchema; /* Schema to which this item is fixed */
18388 char *zDatabase; /* Name of database holding this table */
18389 char *zName; /* Name of the table */
18390 char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */
18391 Table *pTab; /* An SQL table corresponding to zName */
18392 Select *pSelect; /* A SELECT statement used in place of a table name */
18393 int addrFillSub; /* Address of subroutine to manifest a subquery */
18394 int regReturn; /* Register holding return address of addrFillSub */
18395 int regResult; /* Registers holding results of a co-routine */
18396 struct {
18397 u8 jointype; /* Type of join between this table and the previous */
18398 unsigned notIndexed :1; /* True if there is a NOT INDEXED clause */
18399 unsigned isIndexedBy :1; /* True if there is an INDEXED BY clause */
18400 unsigned isTabFunc :1; /* True if table-valued-function syntax */
18401 unsigned isCorrelated :1; /* True if sub-query is correlated */
18402 unsigned isMaterialized:1; /* This is a materialized view */
18403 unsigned viaCoroutine :1; /* Implemented as a co-routine */
18404 unsigned isRecursive :1; /* True for recursive reference in WITH */
18405 unsigned fromDDL :1; /* Comes from sqlite_schema */
18406 unsigned isCte :1; /* This is a CTE */
18407 unsigned notCte :1; /* This item may not match a CTE */
18408 unsigned isUsing :1; /* u3.pUsing is valid */
18409 unsigned isOn :1; /* u3.pOn was once valid and non-NULL */
18410 unsigned isSynthUsing :1; /* u3.pUsing is synthensized from NATURAL */
18411 unsigned isNestedFrom :1; /* pSelect is a SF_NestedFrom subquery */
18412 } fg;
18413 int iCursor; /* The VDBE cursor number used to access this table */
18414 union {
18415 Expr *pOn; /* fg.isUsing==0 => The ON clause of a join */
18416 IdList *pUsing; /* fg.isUsing==1 => The USING clause of a join */
18417 } u3;
18418 Bitmask colUsed; /* Bit N (1<<N) set if column N of pTab is used */
18419 union {
18420 char *zIndexedBy; /* Identifier from "INDEXED BY <zIndex>" clause */
18421 ExprList *pFuncArg; /* Arguments to table-valued-function */
18422 } u1;
18423 union {
18424 Index *pIBIndex; /* Index structure corresponding to u1.zIndexedBy */
18425 CteUse *pCteUse; /* CTE Usage info info fg.isCte is true */
18426 } u2;
18427};
18428
18429/*
18430** The OnOrUsing object represents either an ON clause or a USING clause.
18431** It can never be both at the same time, but it can be neither.
18432*/
18433struct OnOrUsing {
18434 Expr *pOn; /* The ON clause of a join */
18435 IdList *pUsing; /* The USING clause of a join */
18436};
18437
18438/*
18439** The following structure describes the FROM clause of a SELECT statement.
18440** Each table or subquery in the FROM clause is a separate element of
18441** the SrcList.a[] array.
18442**
18443** With the addition of multiple database support, the following structure
18444** can also be used to describe a particular table such as the table that
18445** is modified by an INSERT, DELETE, or UPDATE statement. In standard SQL,
18446** such a table must be a simple name: ID. But in SQLite, the table can
18447** now be identified by a database name, a dot, then the table name: ID.ID.
18448**
18449** The jointype starts out showing the join type between the current table
18450** and the next table on the list. The parser builds the list this way.
18451** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
18452** jointype expresses the join between the table and the previous table.
18453**
18454** In the colUsed field, the high-order bit (bit 63) is set if the table
18455** contains more than 63 columns and the 64-th or later column is used.
18456*/
18457struct SrcList {
18458 int nSrc; /* Number of tables or subqueries in the FROM clause */
18459 u32 nAlloc; /* Number of entries allocated in a[] below */
18460 SrcItem a[1]; /* One entry for each identifier on the list */
18461};
18462
18463/*
18464** Permitted values of the SrcList.a.jointype field
18465*/
18466#define JT_INNER0x01 0x01 /* Any kind of inner or cross join */
18467#define JT_CROSS0x02 0x02 /* Explicit use of the CROSS keyword */
18468#define JT_NATURAL0x04 0x04 /* True for a "natural" join */
18469#define JT_LEFT0x08 0x08 /* Left outer join */
18470#define JT_RIGHT0x10 0x10 /* Right outer join */
18471#define JT_OUTER0x20 0x20 /* The "OUTER" keyword is present */
18472#define JT_LTORJ0x40 0x40 /* One of the LEFT operands of a RIGHT JOIN
18473 ** Mnemonic: Left Table Of Right Join */
18474#define JT_ERROR0x80 0x80 /* unknown or unsupported join type */
18475
18476/*
18477** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
18478** and the WhereInfo.wctrlFlags member.
18479**
18480** Value constraints (enforced via assert()):
18481** WHERE_USE_LIMIT == SF_FixedLimit
18482*/
18483#define WHERE_ORDERBY_NORMAL0x0000 0x0000 /* No-op */
18484#define WHERE_ORDERBY_MIN0x0001 0x0001 /* ORDER BY processing for min() func */
18485#define WHERE_ORDERBY_MAX0x0002 0x0002 /* ORDER BY processing for max() func */
18486#define WHERE_ONEPASS_DESIRED0x0004 0x0004 /* Want to do one-pass UPDATE/DELETE */
18487#define WHERE_ONEPASS_MULTIROW0x0008 0x0008 /* ONEPASS is ok with multiple rows */
18488#define WHERE_DUPLICATES_OK0x0010 0x0010 /* Ok to return a row more than once */
18489#define WHERE_OR_SUBCLAUSE0x0020 0x0020 /* Processing a sub-WHERE as part of
18490 ** the OR optimization */
18491#define WHERE_GROUPBY0x0040 0x0040 /* pOrderBy is really a GROUP BY */
18492#define WHERE_DISTINCTBY0x0080 0x0080 /* pOrderby is really a DISTINCT clause */
18493#define WHERE_WANT_DISTINCT0x0100 0x0100 /* All output needs to be distinct */
18494#define WHERE_SORTBYGROUP0x0200 0x0200 /* Support sqlite3WhereIsSorted() */
18495#define WHERE_AGG_DISTINCT0x0400 0x0400 /* Query is "SELECT agg(DISTINCT ...)" */
18496#define WHERE_ORDERBY_LIMIT0x0800 0x0800 /* ORDERBY+LIMIT on the inner loop */
18497#define WHERE_RIGHT_JOIN0x1000 0x1000 /* Processing a RIGHT JOIN */
18498 /* 0x2000 not currently used */
18499#define WHERE_USE_LIMIT0x4000 0x4000 /* Use the LIMIT in cost estimates */
18500 /* 0x8000 not currently used */
18501
18502/* Allowed return values from sqlite3WhereIsDistinct()
18503*/
18504#define WHERE_DISTINCT_NOOP0 0 /* DISTINCT keyword not used */
18505#define WHERE_DISTINCT_UNIQUE1 1 /* No duplicates */
18506#define WHERE_DISTINCT_ORDERED2 2 /* All duplicates are adjacent */
18507#define WHERE_DISTINCT_UNORDERED3 3 /* Duplicates are scattered */
18508
18509/*
18510** A NameContext defines a context in which to resolve table and column
18511** names. The context consists of a list of tables (the pSrcList) field and
18512** a list of named expression (pEList). The named expression list may
18513** be NULL. The pSrc corresponds to the FROM clause of a SELECT or
18514** to the table being operated on by INSERT, UPDATE, or DELETE. The
18515** pEList corresponds to the result set of a SELECT and is NULL for
18516** other statements.
18517**
18518** NameContexts can be nested. When resolving names, the inner-most
18519** context is searched first. If no match is found, the next outer
18520** context is checked. If there is still no match, the next context
18521** is checked. This process continues until either a match is found
18522** or all contexts are check. When a match is found, the nRef member of
18523** the context containing the match is incremented.
18524**
18525** Each subquery gets a new NameContext. The pNext field points to the
18526** NameContext in the parent query. Thus the process of scanning the
18527** NameContext list corresponds to searching through successively outer
18528** subqueries looking for a match.
18529*/
18530struct NameContext {
18531 Parse *pParse; /* The parser */
18532 SrcList *pSrcList; /* One or more tables used to resolve names */
18533 union {
18534 ExprList *pEList; /* Optional list of result-set columns */
18535 AggInfo *pAggInfo; /* Information about aggregates at this level */
18536 Upsert *pUpsert; /* ON CONFLICT clause information from an upsert */
18537 int iBaseReg; /* For TK_REGISTER when parsing RETURNING */
18538 } uNC;
18539 NameContext *pNext; /* Next outer name context. NULL for outermost */
18540 int nRef; /* Number of names resolved by this context */
18541 int nNcErr; /* Number of errors encountered while resolving names */
18542 int ncFlags; /* Zero or more NC_* flags defined below */
18543 Select *pWinSelect; /* SELECT statement for any window functions */
18544};
18545
18546/*
18547** Allowed values for the NameContext, ncFlags field.
18548**
18549** Value constraints (all checked via assert()):
18550** NC_HasAgg == SF_HasAgg == EP_Agg
18551** NC_MinMaxAgg == SF_MinMaxAgg == SQLITE_FUNC_MINMAX
18552** NC_OrderAgg == SF_OrderByReqd == SQLITE_FUNC_ANYORDER
18553** NC_HasWin == EP_Win
18554**
18555*/
18556#define NC_AllowAgg0x000001 0x000001 /* Aggregate functions are allowed here */
18557#define NC_PartIdx0x000002 0x000002 /* True if resolving a partial index WHERE */
18558#define NC_IsCheck0x000004 0x000004 /* True if resolving a CHECK constraint */
18559#define NC_GenCol0x000008 0x000008 /* True for a GENERATED ALWAYS AS clause */
18560#define NC_HasAgg0x000010 0x000010 /* One or more aggregate functions seen */
18561#define NC_IdxExpr0x000020 0x000020 /* True if resolving columns of CREATE INDEX */
18562#define NC_SelfRef0x00002e 0x00002e /* Combo: PartIdx, isCheck, GenCol, and IdxExpr */
18563#define NC_VarSelect0x000040 0x000040 /* A correlated subquery has been seen */
18564#define NC_UEList0x000080 0x000080 /* True if uNC.pEList is used */
18565#define NC_UAggInfo0x000100 0x000100 /* True if uNC.pAggInfo is used */
18566#define NC_UUpsert0x000200 0x000200 /* True if uNC.pUpsert is used */
18567#define NC_UBaseReg0x000400 0x000400 /* True if uNC.iBaseReg is used */
18568#define NC_MinMaxAgg0x001000 0x001000 /* min/max aggregates seen. See note above */
18569#define NC_Complex0x002000 0x002000 /* True if a function or subquery seen */
18570#define NC_AllowWin0x004000 0x004000 /* Window functions are allowed here */
18571#define NC_HasWin0x008000 0x008000 /* One or more window functions seen */
18572#define NC_IsDDL0x010000 0x010000 /* Resolving names in a CREATE statement */
18573#define NC_InAggFunc0x020000 0x020000 /* True if analyzing arguments to an agg func */
18574#define NC_FromDDL0x040000 0x040000 /* SQL text comes from sqlite_schema */
18575#define NC_NoSelect0x080000 0x080000 /* Do not descend into sub-selects */
18576#define NC_OrderAgg0x8000000 0x8000000 /* Has an aggregate other than count/min/max */
18577
18578/*
18579** An instance of the following object describes a single ON CONFLICT
18580** clause in an upsert.
18581**
18582** The pUpsertTarget field is only set if the ON CONFLICT clause includes
18583** conflict-target clause. (In "ON CONFLICT(a,b)" the "(a,b)" is the
18584** conflict-target clause.) The pUpsertTargetWhere is the optional
18585** WHERE clause used to identify partial unique indexes.
18586**
18587** pUpsertSet is the list of column=expr terms of the UPDATE statement.
18588** The pUpsertSet field is NULL for a ON CONFLICT DO NOTHING. The
18589** pUpsertWhere is the WHERE clause for the UPDATE and is NULL if the
18590** WHERE clause is omitted.
18591*/
18592struct Upsert {
18593 ExprList *pUpsertTarget; /* Optional description of conflict target */
18594 Expr *pUpsertTargetWhere; /* WHERE clause for partial index targets */
18595 ExprList *pUpsertSet; /* The SET clause from an ON CONFLICT UPDATE */
18596 Expr *pUpsertWhere; /* WHERE clause for the ON CONFLICT UPDATE */
18597 Upsert *pNextUpsert; /* Next ON CONFLICT clause in the list */
18598 u8 isDoUpdate; /* True for DO UPDATE. False for DO NOTHING */
18599 /* Above this point is the parse tree for the ON CONFLICT clauses.
18600 ** The next group of fields stores intermediate data. */
18601 void *pToFree; /* Free memory when deleting the Upsert object */
18602 /* All fields above are owned by the Upsert object and must be freed
18603 ** when the Upsert is destroyed. The fields below are used to transfer
18604 ** information from the INSERT processing down into the UPDATE processing
18605 ** while generating code. The fields below are owned by the INSERT
18606 ** statement and will be freed by INSERT processing. */
18607 Index *pUpsertIdx; /* UNIQUE constraint specified by pUpsertTarget */
18608 SrcList *pUpsertSrc; /* Table to be updated */
18609 int regData; /* First register holding array of VALUES */
18610 int iDataCur; /* Index of the data cursor */
18611 int iIdxCur; /* Index of the first index cursor */
18612};
18613
18614/*
18615** An instance of the following structure contains all information
18616** needed to generate code for a single SELECT statement.
18617**
18618** See the header comment on the computeLimitRegisters() routine for a
18619** detailed description of the meaning of the iLimit and iOffset fields.
18620**
18621** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
18622** These addresses must be stored so that we can go back and fill in
18623** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor
18624** the number of columns in P2 can be computed at the same time
18625** as the OP_OpenEphm instruction is coded because not
18626** enough information about the compound query is known at that point.
18627** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
18628** for the result set. The KeyInfo for addrOpenEphm[2] contains collating
18629** sequences for the ORDER BY clause.
18630*/
18631struct Select {
18632 u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
18633 LogEst nSelectRow; /* Estimated number of result rows */
18634 u32 selFlags; /* Various SF_* values */
18635 int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
18636 u32 selId; /* Unique identifier number for this SELECT */
18637 int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */
18638 ExprList *pEList; /* The fields of the result */
18639 SrcList *pSrc; /* The FROM clause */
18640 Expr *pWhere; /* The WHERE clause */
18641 ExprList *pGroupBy; /* The GROUP BY clause */
18642 Expr *pHaving; /* The HAVING clause */
18643 ExprList *pOrderBy; /* The ORDER BY clause */
18644 Select *pPrior; /* Prior select in a compound select statement */
18645 Select *pNext; /* Next select to the left in a compound */
18646 Expr *pLimit; /* LIMIT expression. NULL means not used. */
18647 With *pWith; /* WITH clause attached to this select. Or NULL. */
18648#ifndef SQLITE_OMIT_WINDOWFUNC
18649 Window *pWin; /* List of window functions */
18650 Window *pWinDefn; /* List of named window definitions */
18651#endif
18652};
18653
18654/*
18655** Allowed values for Select.selFlags. The "SF" prefix stands for
18656** "Select Flag".
18657**
18658** Value constraints (all checked via assert())
18659** SF_HasAgg == NC_HasAgg
18660** SF_MinMaxAgg == NC_MinMaxAgg == SQLITE_FUNC_MINMAX
18661** SF_OrderByReqd == NC_OrderAgg == SQLITE_FUNC_ANYORDER
18662** SF_FixedLimit == WHERE_USE_LIMIT
18663*/
18664#define SF_Distinct0x0000001 0x0000001 /* Output should be DISTINCT */
18665#define SF_All0x0000002 0x0000002 /* Includes the ALL keyword */
18666#define SF_Resolved0x0000004 0x0000004 /* Identifiers have been resolved */
18667#define SF_Aggregate0x0000008 0x0000008 /* Contains agg functions or a GROUP BY */
18668#define SF_HasAgg0x0000010 0x0000010 /* Contains aggregate functions */
18669#define SF_UsesEphemeral0x0000020 0x0000020 /* Uses the OpenEphemeral opcode */
18670#define SF_Expanded0x0000040 0x0000040 /* sqlite3SelectExpand() called on this */
18671#define SF_HasTypeInfo0x0000080 0x0000080 /* FROM subqueries have Table metadata */
18672#define SF_Compound0x0000100 0x0000100 /* Part of a compound query */
18673#define SF_Values0x0000200 0x0000200 /* Synthesized from VALUES clause */
18674#define SF_MultiValue0x0000400 0x0000400 /* Single VALUES term with multiple rows */
18675#define SF_NestedFrom0x0000800 0x0000800 /* Part of a parenthesized FROM clause */
18676#define SF_MinMaxAgg0x0001000 0x0001000 /* Aggregate containing min() or max() */
18677#define SF_Recursive0x0002000 0x0002000 /* The recursive part of a recursive CTE */
18678#define SF_FixedLimit0x0004000 0x0004000 /* nSelectRow set by a constant LIMIT */
18679#define SF_MaybeConvert0x0008000 0x0008000 /* Need convertCompoundSelectToSubquery() */
18680#define SF_Converted0x0010000 0x0010000 /* By convertCompoundSelectToSubquery() */
18681#define SF_IncludeHidden0x0020000 0x0020000 /* Include hidden columns in output */
18682#define SF_ComplexResult0x0040000 0x0040000 /* Result contains subquery or function */
18683#define SF_WhereBegin0x0080000 0x0080000 /* Really a WhereBegin() call. Debug Only */
18684#define SF_WinRewrite0x0100000 0x0100000 /* Window function rewrite accomplished */
18685#define SF_View0x0200000 0x0200000 /* SELECT statement is a view */
18686#define SF_NoopOrderBy0x0400000 0x0400000 /* ORDER BY is ignored for this query */
18687#define SF_UFSrcCheck0x0800000 0x0800000 /* Check pSrc as required by UPDATE...FROM */
18688#define SF_PushDown0x1000000 0x1000000 /* SELECT has be modified by push-down opt */
18689#define SF_MultiPart0x2000000 0x2000000 /* Has multiple incompatible PARTITIONs */
18690#define SF_CopyCte0x4000000 0x4000000 /* SELECT statement is a copy of a CTE */
18691#define SF_OrderByReqd0x8000000 0x8000000 /* The ORDER BY clause may not be omitted */
18692
18693/* True if S exists and has SF_NestedFrom */
18694#define IsNestedFrom(S)((S)!=0 && ((S)->selFlags&0x0000800)!=0) ((S)!=0 && ((S)->selFlags&SF_NestedFrom0x0000800)!=0)
18695
18696/*
18697** The results of a SELECT can be distributed in several ways, as defined
18698** by one of the following macros. The "SRT" prefix means "SELECT Result
18699** Type".
18700**
18701** SRT_Union Store results as a key in a temporary index
18702** identified by pDest->iSDParm.
18703**
18704** SRT_Except Remove results from the temporary index pDest->iSDParm.
18705**
18706** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result
18707** set is not empty.
18708**
18709** SRT_Discard Throw the results away. This is used by SELECT
18710** statements within triggers whose only purpose is
18711** the side-effects of functions.
18712**
18713** SRT_Output Generate a row of output (using the OP_ResultRow
18714** opcode) for each row in the result set.
18715**
18716** SRT_Mem Only valid if the result is a single column.
18717** Store the first column of the first result row
18718** in register pDest->iSDParm then abandon the rest
18719** of the query. This destination implies "LIMIT 1".
18720**
18721** SRT_Set The result must be a single column. Store each
18722** row of result as the key in table pDest->iSDParm.
18723** Apply the affinity pDest->affSdst before storing
18724** results. Used to implement "IN (SELECT ...)".
18725**
18726** SRT_EphemTab Create an temporary table pDest->iSDParm and store
18727** the result there. The cursor is left open after
18728** returning. This is like SRT_Table except that
18729** this destination uses OP_OpenEphemeral to create
18730** the table first.
18731**
18732** SRT_Coroutine Generate a co-routine that returns a new row of
18733** results each time it is invoked. The entry point
18734** of the co-routine is stored in register pDest->iSDParm
18735** and the result row is stored in pDest->nDest registers
18736** starting with pDest->iSdst.
18737**
18738** SRT_Table Store results in temporary table pDest->iSDParm.
18739** SRT_Fifo This is like SRT_EphemTab except that the table
18740** is assumed to already be open. SRT_Fifo has
18741** the additional property of being able to ignore
18742** the ORDER BY clause.
18743**
18744** SRT_DistFifo Store results in a temporary table pDest->iSDParm.
18745** But also use temporary table pDest->iSDParm+1 as
18746** a record of all prior results and ignore any duplicate
18747** rows. Name means: "Distinct Fifo".
18748**
18749** SRT_Queue Store results in priority queue pDest->iSDParm (really
18750** an index). Append a sequence number so that all entries
18751** are distinct.
18752**
18753** SRT_DistQueue Store results in priority queue pDest->iSDParm only if
18754** the same record has never been stored before. The
18755** index at pDest->iSDParm+1 hold all prior stores.
18756**
18757** SRT_Upfrom Store results in the temporary table already opened by
18758** pDest->iSDParm. If (pDest->iSDParm<0), then the temp
18759** table is an intkey table - in this case the first
18760** column returned by the SELECT is used as the integer
18761** key. If (pDest->iSDParm>0), then the table is an index
18762** table. (pDest->iSDParm) is the number of key columns in
18763** each index record in this case.
18764*/
18765#define SRT_Union1 1 /* Store result as keys in an index */
18766#define SRT_Except2 2 /* Remove result from a UNION index */
18767#define SRT_Exists3 3 /* Store 1 if the result is not empty */
18768#define SRT_Discard4 4 /* Do not save the results anywhere */
18769#define SRT_DistFifo5 5 /* Like SRT_Fifo, but unique results only */
18770#define SRT_DistQueue6 6 /* Like SRT_Queue, but unique results only */
18771
18772/* The DISTINCT clause is ignored for all of the above. Not that
18773** IgnorableDistinct() implies IgnorableOrderby() */
18774#define IgnorableDistinct(X)((X->eDest)<=6) ((X->eDest)<=SRT_DistQueue6)
18775
18776#define SRT_Queue7 7 /* Store result in an queue */
18777#define SRT_Fifo8 8 /* Store result as data with an automatic rowid */
18778
18779/* The ORDER BY clause is ignored for all of the above */
18780#define IgnorableOrderby(X)((X->eDest)<=8) ((X->eDest)<=SRT_Fifo8)
18781
18782#define SRT_Output9 9 /* Output each row of result */
18783#define SRT_Mem10 10 /* Store result in a memory cell */
18784#define SRT_Set11 11 /* Store results as keys in an index */
18785#define SRT_EphemTab12 12 /* Create transient tab and store like SRT_Table */
18786#define SRT_Coroutine13 13 /* Generate a single row of result */
18787#define SRT_Table14 14 /* Store result as data with an automatic rowid */
18788#define SRT_Upfrom15 15 /* Store result as data with rowid */
18789
18790/*
18791** An instance of this object describes where to put of the results of
18792** a SELECT statement.
18793*/
18794struct SelectDest {
18795 u8 eDest; /* How to dispose of the results. One of SRT_* above. */
18796 int iSDParm; /* A parameter used by the eDest disposal method */
18797 int iSDParm2; /* A second parameter for the eDest disposal method */
18798 int iSdst; /* Base register where results are written */
18799 int nSdst; /* Number of registers allocated */
18800 char *zAffSdst; /* Affinity used when eDest==SRT_Set */
18801 ExprList *pOrderBy; /* Key columns for SRT_Queue and SRT_DistQueue */
18802};
18803
18804/*
18805** During code generation of statements that do inserts into AUTOINCREMENT
18806** tables, the following information is attached to the Table.u.autoInc.p
18807** pointer of each autoincrement table to record some side information that
18808** the code generator needs. We have to keep per-table autoincrement
18809** information in case inserts are done within triggers. Triggers do not
18810** normally coordinate their activities, but we do need to coordinate the
18811** loading and saving of autoincrement information.
18812*/
18813struct AutoincInfo {
18814 AutoincInfo *pNext; /* Next info block in a list of them all */
18815 Table *pTab; /* Table this info block refers to */
18816 int iDb; /* Index in sqlite3.aDb[] of database holding pTab */
18817 int regCtr; /* Memory register holding the rowid counter */
18818};
18819
18820/*
18821** At least one instance of the following structure is created for each
18822** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
18823** statement. All such objects are stored in the linked list headed at
18824** Parse.pTriggerPrg and deleted once statement compilation has been
18825** completed.
18826**
18827** A Vdbe sub-program that implements the body and WHEN clause of trigger
18828** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
18829** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
18830** The Parse.pTriggerPrg list never contains two entries with the same
18831** values for both pTrigger and orconf.
18832**
18833** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
18834** accessed (or set to 0 for triggers fired as a result of INSERT
18835** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
18836** a mask of new.* columns used by the program.
18837*/
18838struct TriggerPrg {
18839 Trigger *pTrigger; /* Trigger this program was coded from */
18840 TriggerPrg *pNext; /* Next entry in Parse.pTriggerPrg list */
18841 SubProgram *pProgram; /* Program implementing pTrigger/orconf */
18842 int orconf; /* Default ON CONFLICT policy */
18843 u32 aColmask[2]; /* Masks of old.*, new.* columns accessed */
18844};
18845
18846/*
18847** The yDbMask datatype for the bitmask of all attached databases.
18848*/
18849#if SQLITE_MAX_ATTACHED10>30
18850 typedef unsigned char yDbMask[(SQLITE_MAX_ATTACHED10+9)/8];
18851# define DbMaskTest(M,I)(((M)&(((yDbMask)1)<<(I)))!=0) (((M)[(I)/8]&(1<<((I)&7)))!=0)
18852# define DbMaskZero(M)(M)=0 memset((M),0,sizeof(M))
18853# define DbMaskSet(M,I)(M)|=(((yDbMask)1)<<(I)) (M)[(I)/8]|=(1<<((I)&7))
18854# define DbMaskAllZero(M)(M)==0 sqlite3DbMaskAllZero(M)
18855# define DbMaskNonZero(M)(M)!=0 (sqlite3DbMaskAllZero(M)==0)
18856#else
18857 typedef unsigned int yDbMask;
18858# define DbMaskTest(M,I)(((M)&(((yDbMask)1)<<(I)))!=0) (((M)&(((yDbMask)1)<<(I)))!=0)
18859# define DbMaskZero(M)(M)=0 (M)=0
18860# define DbMaskSet(M,I)(M)|=(((yDbMask)1)<<(I)) (M)|=(((yDbMask)1)<<(I))
18861# define DbMaskAllZero(M)(M)==0 (M)==0
18862# define DbMaskNonZero(M)(M)!=0 (M)!=0
18863#endif
18864
18865/*
18866** An instance of the ParseCleanup object specifies an operation that
18867** should be performed after parsing to deallocation resources obtained
18868** during the parse and which are no longer needed.
18869*/
18870struct ParseCleanup {
18871 ParseCleanup *pNext; /* Next cleanup task */
18872 void *pPtr; /* Pointer to object to deallocate */
18873 void (*xCleanup)(sqlite3*,void*); /* Deallocation routine */
18874};
18875
18876/*
18877** An SQL parser context. A copy of this structure is passed through
18878** the parser and down into all the parser action routine in order to
18879** carry around information that is global to the entire parse.
18880**
18881** The structure is divided into two parts. When the parser and code
18882** generate call themselves recursively, the first part of the structure
18883** is constant but the second part is reset at the beginning and end of
18884** each recursion.
18885**
18886** The nTableLock and aTableLock variables are only used if the shared-cache
18887** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
18888** used to store the set of table-locks required by the statement being
18889** compiled. Function sqlite3TableLock() is used to add entries to the
18890** list.
18891*/
18892struct Parse {
18893 sqlite3 *db; /* The main database structure */
18894 char *zErrMsg; /* An error message */
18895 Vdbe *pVdbe; /* An engine for executing database bytecode */
18896 int rc; /* Return code from execution */
18897 u8 colNamesSet; /* TRUE after OP_ColumnName has been issued to pVdbe */
18898 u8 checkSchema; /* Causes schema cookie check after an error */
18899 u8 nested; /* Number of nested calls to the parser/code generator */
18900 u8 nTempReg; /* Number of temporary registers in aTempReg[] */
18901 u8 isMultiWrite; /* True if statement may modify/insert multiple rows */
18902 u8 mayAbort; /* True if statement may throw an ABORT exception */
18903 u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */
18904 u8 okConstFactor; /* OK to factor out constants */
18905 u8 disableLookaside; /* Number of times lookaside has been disabled */
18906 u8 disableVtab; /* Disable all virtual tables for this parse */
18907 u8 withinRJSubrtn; /* Nesting level for RIGHT JOIN body subroutines */
18908#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
18909 u8 earlyCleanup; /* OOM inside sqlite3ParserAddCleanup() */
18910#endif
18911 int nRangeReg; /* Size of the temporary register block */
18912 int iRangeReg; /* First register in temporary register block */
18913 int nErr; /* Number of errors seen */
18914 int nTab; /* Number of previously allocated VDBE cursors */
18915 int nMem; /* Number of memory cells used so far */
18916 int szOpAlloc; /* Bytes of memory space allocated for Vdbe.aOp[] */
18917 int iSelfTab; /* Table associated with an index on expr, or negative
18918 ** of the base register during check-constraint eval */
18919 int nLabel; /* The *negative* of the number of labels used */
18920 int nLabelAlloc; /* Number of slots in aLabel */
18921 int *aLabel; /* Space to hold the labels */
18922 ExprList *pConstExpr;/* Constant expressions */
18923 Token constraintName;/* Name of the constraint currently being parsed */
18924 yDbMask writeMask; /* Start a write transaction on these databases */
18925 yDbMask cookieMask; /* Bitmask of schema verified databases */
18926 int regRowid; /* Register holding rowid of CREATE TABLE entry */
18927 int regRoot; /* Register holding root page number for new objects */
18928 int nMaxArg; /* Max args passed to user function by sub-program */
18929 int nSelect; /* Number of SELECT stmts. Counter for Select.selId */
18930#ifndef SQLITE_OMIT_SHARED_CACHE
18931 int nTableLock; /* Number of locks in aTableLock */
18932 TableLock *aTableLock; /* Required table locks for shared-cache mode */
18933#endif
18934 AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */
18935 Parse *pToplevel; /* Parse structure for main program (or NULL) */
18936 Table *pTriggerTab; /* Table triggers are being coded for */
18937 TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */
18938 ParseCleanup *pCleanup; /* List of cleanup operations to run after parse */
18939 union {
18940 int addrCrTab; /* Address of OP_CreateBtree on CREATE TABLE */
18941 Returning *pReturning; /* The RETURNING clause */
18942 } u1;
18943 u32 nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */
18944 u32 oldmask; /* Mask of old.* columns referenced */
18945 u32 newmask; /* Mask of new.* columns referenced */
18946 u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */
18947 u8 bReturning; /* Coding a RETURNING trigger */
18948 u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */
18949 u8 disableTriggers; /* True to disable triggers */
18950
18951 /**************************************************************************
18952 ** Fields above must be initialized to zero. The fields that follow,
18953 ** down to the beginning of the recursive section, do not need to be
18954 ** initialized as they will be set before being used. The boundary is
18955 ** determined by offsetof(Parse,aTempReg).
18956 **************************************************************************/
18957
18958 int aTempReg[8]; /* Holding area for temporary registers */
18959 Parse *pOuterParse; /* Outer Parse object when nested */
18960 Token sNameToken; /* Token with unqualified schema object name */
18961
18962 /************************************************************************
18963 ** Above is constant between recursions. Below is reset before and after
18964 ** each recursion. The boundary between these two regions is determined
18965 ** using offsetof(Parse,sLastToken) so the sLastToken field must be the
18966 ** first field in the recursive region.
18967 ************************************************************************/
18968
18969 Token sLastToken; /* The last token parsed */
18970 ynVar nVar; /* Number of '?' variables seen in the SQL so far */
18971 u8 iPkSortOrder; /* ASC or DESC for INTEGER PRIMARY KEY */
18972 u8 explain; /* True if the EXPLAIN flag is found on the query */
18973 u8 eParseMode; /* PARSE_MODE_XXX constant */
18974#ifndef SQLITE_OMIT_VIRTUALTABLE
18975 int nVtabLock; /* Number of virtual tables to lock */
18976#endif
18977 int nHeight; /* Expression tree height of current sub-select */
18978#ifndef SQLITE_OMIT_EXPLAIN
18979 int addrExplain; /* Address of current OP_Explain opcode */
18980#endif
18981 VList *pVList; /* Mapping between variable names and numbers */
18982 Vdbe *pReprepare; /* VM being reprepared (sqlite3Reprepare()) */
18983 const char *zTail; /* All SQL text past the last semicolon parsed */
18984 Table *pNewTable; /* A table being constructed by CREATE TABLE */
18985 Index *pNewIndex; /* An index being constructed by CREATE INDEX.
18986 ** Also used to hold redundant UNIQUE constraints
18987 ** during a RENAME COLUMN */
18988 Trigger *pNewTrigger; /* Trigger under construct by a CREATE TRIGGER */
18989 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
18990#ifndef SQLITE_OMIT_VIRTUALTABLE
18991 Token sArg; /* Complete text of a module argument */
18992 Table **apVtabLock; /* Pointer to virtual tables needing locking */
18993#endif
18994 With *pWith; /* Current WITH clause, or NULL */
18995#ifndef SQLITE_OMIT_ALTERTABLE
18996 RenameToken *pRename; /* Tokens subject to renaming by ALTER TABLE */
18997#endif
18998};
18999
19000/* Allowed values for Parse.eParseMode
19001*/
19002#define PARSE_MODE_NORMAL0 0
19003#define PARSE_MODE_DECLARE_VTAB1 1
19004#define PARSE_MODE_RENAME2 2
19005#define PARSE_MODE_UNMAP3 3
19006
19007/*
19008** Sizes and pointers of various parts of the Parse object.
19009*/
19010#define PARSE_HDR(X)(((char*)(X))+__builtin_offsetof(Parse, zErrMsg)) (((char*)(X))+offsetof(Parse,zErrMsg)__builtin_offsetof(Parse, zErrMsg))
19011#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*/
19012#define PARSE_RECURSE_SZ__builtin_offsetof(Parse, sLastToken) offsetof(Parse,sLastToken)__builtin_offsetof(Parse, sLastToken) /* Recursive part */
19013#define PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken)) (sizeof(Parse)-PARSE_RECURSE_SZ__builtin_offsetof(Parse, sLastToken)) /* Non-recursive part */
19014#define PARSE_TAIL(X)(((char*)(X))+__builtin_offsetof(Parse, sLastToken)) (((char*)(X))+PARSE_RECURSE_SZ__builtin_offsetof(Parse, sLastToken)) /* Pointer to tail */
19015
19016/*
19017** Return true if currently inside an sqlite3_declare_vtab() call.
19018*/
19019#ifdef SQLITE_OMIT_VIRTUALTABLE
19020 #define IN_DECLARE_VTAB(pParse->eParseMode==1) 0
19021#else
19022 #define IN_DECLARE_VTAB(pParse->eParseMode==1) (pParse->eParseMode==PARSE_MODE_DECLARE_VTAB1)
19023#endif
19024
19025#if defined(SQLITE_OMIT_ALTERTABLE)
19026 #define IN_RENAME_OBJECT(pParse->eParseMode>=2) 0
19027#else
19028 #define IN_RENAME_OBJECT(pParse->eParseMode>=2) (pParse->eParseMode>=PARSE_MODE_RENAME2)
19029#endif
19030
19031#if defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_OMIT_ALTERTABLE)
19032 #define IN_SPECIAL_PARSE(pParse->eParseMode!=0) 0
19033#else
19034 #define IN_SPECIAL_PARSE(pParse->eParseMode!=0) (pParse->eParseMode!=PARSE_MODE_NORMAL0)
19035#endif
19036
19037/*
19038** An instance of the following structure can be declared on a stack and used
19039** to save the Parse.zAuthContext value so that it can be restored later.
19040*/
19041struct AuthContext {
19042 const char *zAuthContext; /* Put saved Parse.zAuthContext here */
19043 Parse *pParse; /* The Parse structure */
19044};
19045
19046/*
19047** Bitfield flags for P5 value in various opcodes.
19048**
19049** Value constraints (enforced via assert()):
19050** OPFLAG_LENGTHARG == SQLITE_FUNC_LENGTH
19051** OPFLAG_TYPEOFARG == SQLITE_FUNC_TYPEOF
19052** OPFLAG_BULKCSR == BTREE_BULKLOAD
19053** OPFLAG_SEEKEQ == BTREE_SEEK_EQ
19054** OPFLAG_FORDELETE == BTREE_FORDELETE
19055** OPFLAG_SAVEPOSITION == BTREE_SAVEPOSITION
19056** OPFLAG_AUXDELETE == BTREE_AUXDELETE
19057*/
19058#define OPFLAG_NCHANGE0x01 0x01 /* OP_Insert: Set to update db->nChange */
19059 /* Also used in P2 (not P5) of OP_Delete */
19060#define OPFLAG_NOCHNG0x01 0x01 /* OP_VColumn nochange for UPDATE */
19061#define OPFLAG_EPHEM0x01 0x01 /* OP_Column: Ephemeral output is ok */
19062#define OPFLAG_LASTROWID0x20 0x20 /* Set to update db->lastRowid */
19063#define OPFLAG_ISUPDATE0x04 0x04 /* This OP_Insert is an sql UPDATE */
19064#define OPFLAG_APPEND0x08 0x08 /* This is likely to be an append */
19065#define OPFLAG_USESEEKRESULT0x10 0x10 /* Try to avoid a seek in BtreeInsert() */
19066#define OPFLAG_ISNOOP0x40 0x40 /* OP_Delete does pre-update-hook only */
19067#define OPFLAG_LENGTHARG0x40 0x40 /* OP_Column only used for length() */
19068#define OPFLAG_TYPEOFARG0x80 0x80 /* OP_Column only used for typeof() */
19069#define OPFLAG_BULKCSR0x01 0x01 /* OP_Open** used to open bulk cursor */
19070#define OPFLAG_SEEKEQ0x02 0x02 /* OP_Open** cursor uses EQ seek only */
19071#define OPFLAG_FORDELETE0x08 0x08 /* OP_Open should use BTREE_FORDELETE */
19072#define OPFLAG_P2ISREG0x10 0x10 /* P2 to OP_Open** is a register number */
19073#define OPFLAG_PERMUTE0x01 0x01 /* OP_Compare: use the permutation */
19074#define OPFLAG_SAVEPOSITION0x02 0x02 /* OP_Delete/Insert: save cursor pos */
19075#define OPFLAG_AUXDELETE0x04 0x04 /* OP_Delete: index in a DELETE op */
19076#define OPFLAG_NOCHNG_MAGIC0x6d 0x6d /* OP_MakeRecord: serialtype 10 is ok */
19077#define OPFLAG_PREFORMAT0x80 0x80 /* OP_Insert uses preformatted cell */
19078
19079/*
19080** Each trigger present in the database schema is stored as an instance of
19081** struct Trigger.
19082**
19083** Pointers to instances of struct Trigger are stored in two ways.
19084** 1. In the "trigHash" hash table (part of the sqlite3* that represents the
19085** database). This allows Trigger structures to be retrieved by name.
19086** 2. All triggers associated with a single table form a linked list, using the
19087** pNext member of struct Trigger. A pointer to the first element of the
19088** linked list is stored as the "pTrigger" member of the associated
19089** struct Table.
19090**
19091** The "step_list" member points to the first element of a linked list
19092** containing the SQL statements specified as the trigger program.
19093*/
19094struct Trigger {
19095 char *zName; /* The name of the trigger */
19096 char *table; /* The table or view to which the trigger applies */
19097 u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT */
19098 u8 tr_tm; /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
19099 u8 bReturning; /* This trigger implements a RETURNING clause */
19100 Expr *pWhen; /* The WHEN clause of the expression (may be NULL) */
19101 IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
19102 the <column-list> is stored here */
19103 Schema *pSchema; /* Schema containing the trigger */
19104 Schema *pTabSchema; /* Schema containing the table */
19105 TriggerStep *step_list; /* Link list of trigger program steps */
19106 Trigger *pNext; /* Next trigger associated with the table */
19107};
19108
19109/*
19110** A trigger is either a BEFORE or an AFTER trigger. The following constants
19111** determine which.
19112**
19113** If there are multiple triggers, you might of some BEFORE and some AFTER.
19114** In that cases, the constants below can be ORed together.
19115*/
19116#define TRIGGER_BEFORE1 1
19117#define TRIGGER_AFTER2 2
19118
19119/*
19120** An instance of struct TriggerStep is used to store a single SQL statement
19121** that is a part of a trigger-program.
19122**
19123** Instances of struct TriggerStep are stored in a singly linked list (linked
19124** using the "pNext" member) referenced by the "step_list" member of the
19125** associated struct Trigger instance. The first element of the linked list is
19126** the first step of the trigger-program.
19127**
19128** The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
19129** "SELECT" statement. The meanings of the other members is determined by the
19130** value of "op" as follows:
19131**
19132** (op == TK_INSERT)
19133** orconf -> stores the ON CONFLICT algorithm
19134** pSelect -> The content to be inserted - either a SELECT statement or
19135** a VALUES clause.
19136** zTarget -> Dequoted name of the table to insert into.
19137** pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ...
19138** statement, then this stores the column-names to be
19139** inserted into.
19140** pUpsert -> The ON CONFLICT clauses for an Upsert
19141**
19142** (op == TK_DELETE)
19143** zTarget -> Dequoted name of the table to delete from.
19144** pWhere -> The WHERE clause of the DELETE statement if one is specified.
19145** Otherwise NULL.
19146**
19147** (op == TK_UPDATE)
19148** zTarget -> Dequoted name of the table to update.
19149** pWhere -> The WHERE clause of the UPDATE statement if one is specified.
19150** Otherwise NULL.
19151** pExprList -> A list of the columns to update and the expressions to update
19152** them to. See sqlite3Update() documentation of "pChanges"
19153** argument.
19154**
19155** (op == TK_SELECT)
19156** pSelect -> The SELECT statement
19157**
19158** (op == TK_RETURNING)
19159** pExprList -> The list of expressions that follow the RETURNING keyword.
19160**
19161*/
19162struct TriggerStep {
19163 u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT,
19164 ** or TK_RETURNING */
19165 u8 orconf; /* OE_Rollback etc. */
19166 Trigger *pTrig; /* The trigger that this step is a part of */
19167 Select *pSelect; /* SELECT statement or RHS of INSERT INTO SELECT ... */
19168 char *zTarget; /* Target table for DELETE, UPDATE, INSERT */
19169 SrcList *pFrom; /* FROM clause for UPDATE statement (if any) */
19170 Expr *pWhere; /* The WHERE clause for DELETE or UPDATE steps */
19171 ExprList *pExprList; /* SET clause for UPDATE, or RETURNING clause */
19172 IdList *pIdList; /* Column names for INSERT */
19173 Upsert *pUpsert; /* Upsert clauses on an INSERT */
19174 char *zSpan; /* Original SQL text of this command */
19175 TriggerStep *pNext; /* Next in the link-list */
19176 TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */
19177};
19178
19179/*
19180** Information about a RETURNING clause
19181*/
19182struct Returning {
19183 Parse *pParse; /* The parse that includes the RETURNING clause */
19184 ExprList *pReturnEL; /* List of expressions to return */
19185 Trigger retTrig; /* The transient trigger that implements RETURNING */
19186 TriggerStep retTStep; /* The trigger step */
19187 int iRetCur; /* Transient table holding RETURNING results */
19188 int nRetCol; /* Number of in pReturnEL after expansion */
19189 int iRetReg; /* Register array for holding a row of RETURNING */
19190};
19191
19192/*
19193** An objected used to accumulate the text of a string where we
19194** do not necessarily know how big the string will be in the end.
19195*/
19196struct sqlite3_str {
19197 sqlite3 *db; /* Optional database for lookaside. Can be NULL */
19198 char *zText; /* The string collected so far */
19199 u32 nAlloc; /* Amount of space allocated in zText */
19200 u32 mxAlloc; /* Maximum allowed allocation. 0 for no malloc usage */
19201 u32 nChar; /* Length of the string so far */
19202 u8 accError; /* SQLITE_NOMEM or SQLITE_TOOBIG */
19203 u8 printfFlags; /* SQLITE_PRINTF flags below */
19204};
19205#define SQLITE_PRINTF_INTERNAL0x01 0x01 /* Internal-use-only converters allowed */
19206#define SQLITE_PRINTF_SQLFUNC0x02 0x02 /* SQL function arguments to VXPrintf */
19207#define SQLITE_PRINTF_MALLOCED0x04 0x04 /* True if xText is allocated space */
19208
19209#define isMalloced(X)(((X)->printfFlags & 0x04)!=0) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED0x04)!=0)
19210
19211
19212/*
19213** A pointer to this structure is used to communicate information
19214** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
19215*/
19216typedef struct {
19217 sqlite3 *db; /* The database being initialized */
19218 char **pzErrMsg; /* Error message stored here */
19219 int iDb; /* 0 for main database. 1 for TEMP, 2.. for ATTACHed */
19220 int rc; /* Result code stored here */
19221 u32 mInitFlags; /* Flags controlling error messages */
19222 u32 nInitRow; /* Number of rows processed */
19223 Pgno mxPage; /* Maximum page number. 0 for no limit. */
19224} InitData;
19225
19226/*
19227** Allowed values for mInitFlags
19228*/
19229#define INITFLAG_AlterMask0x0003 0x0003 /* Types of ALTER */
19230#define INITFLAG_AlterRename0x0001 0x0001 /* Reparse after a RENAME */
19231#define INITFLAG_AlterDrop0x0002 0x0002 /* Reparse after a DROP COLUMN */
19232#define INITFLAG_AlterAdd0x0003 0x0003 /* Reparse after an ADD COLUMN */
19233
19234/* Tuning parameters are set using SQLITE_TESTCTRL_TUNE and are controlled
19235** on debug-builds of the CLI using ".testctrl tune ID VALUE". Tuning
19236** parameters are for temporary use during development, to help find
19237** optimial values for parameters in the query planner. The should not
19238** be used on trunk check-ins. They are a temporary mechanism available
19239** for transient development builds only.
19240**
19241** Tuning parameters are numbered starting with 1.
19242*/
19243#define SQLITE_NTUNE6 6 /* Should be zero for all trunk check-ins */
19244#ifdef SQLITE_DEBUG
19245# define Tuning(X)0 (sqlite3Config.aTune[(X)-1])
19246#else
19247# define Tuning(X)0 0
19248#endif
19249
19250/*
19251** Structure containing global configuration data for the SQLite library.
19252**
19253** This structure also contains some state information.
19254*/
19255struct Sqlite3Config {
19256 int bMemstat; /* True to enable memory status */
19257 u8 bCoreMutex; /* True to enable core mutexing */
19258 u8 bFullMutex; /* True to enable full mutexing */
19259 u8 bOpenUri; /* True to interpret filenames as URIs */
19260 u8 bUseCis; /* Use covering indices for full-scans */
19261 u8 bSmallMalloc; /* Avoid large memory allocations if true */
19262 u8 bExtraSchemaChecks; /* Verify type,name,tbl_name in schema */
19263 int mxStrlen; /* Maximum string length */
19264 int neverCorrupt; /* Database is always well-formed */
19265 int szLookaside; /* Default lookaside buffer size */
19266 int nLookaside; /* Default lookaside buffer count */
19267 int nStmtSpill; /* Stmt-journal spill-to-disk threshold */
19268 sqlite3_mem_methods m; /* Low-level memory allocation interface */
19269 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
19270 sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
19271 void *pHeap; /* Heap storage space */
19272 int nHeap; /* Size of pHeap[] */
19273 int mnReq, mxReq; /* Min and max heap requests sizes */
19274 sqlite3_int64 szMmap; /* mmap() space per open file */
19275 sqlite3_int64 mxMmap; /* Maximum value for szMmap */
19276 void *pPage; /* Page cache memory */
19277 int szPage; /* Size of each page in pPage[] */
19278 int nPage; /* Number of pages in pPage[] */
19279 int mxParserStack; /* maximum depth of the parser stack */
19280 int sharedCacheEnabled; /* true if shared-cache mode enabled */
19281 u32 szPma; /* Maximum Sorter PMA size */
19282 /* The above might be initialized to non-zero. The following need to always
19283 ** initially be zero, however. */
19284 int isInit; /* True after initialization has finished */
19285 int inProgress; /* True while initialization in progress */
19286 int isMutexInit; /* True after mutexes are initialized */
19287 int isMallocInit; /* True after malloc is initialized */
19288 int isPCacheInit; /* True after malloc is initialized */
19289 int nRefInitMutex; /* Number of users of pInitMutex */
19290 sqlite3_mutex *pInitMutex; /* Mutex used by sqlite3_initialize() */
19291 void (*xLog)(void*,int,const char*); /* Function for logging */
19292 void *pLogArg; /* First argument to xLog() */
19293#ifdef SQLITE_ENABLE_SQLLOG
19294 void(*xSqllog)(void*,sqlite3*,const char*, int);
19295 void *pSqllogArg;
19296#endif
19297#ifdef SQLITE_VDBE_COVERAGE
19298 /* The following callback (if not NULL) is invoked on every VDBE branch
19299 ** operation. Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE.
19300 */
19301 void (*xVdbeBranch)(void*,unsigned iSrcLine,u8 eThis,u8 eMx); /* Callback */
19302 void *pVdbeBranchArg; /* 1st argument */
19303#endif
19304#ifndef SQLITE_OMIT_DESERIALIZE
19305 sqlite3_int64 mxMemdbSize; /* Default max memdb size */
19306#endif
19307#ifndef SQLITE_UNTESTABLE
19308 int (*xTestCallback)(int); /* Invoked by sqlite3FaultSim() */
19309#endif
19310 int bLocaltimeFault; /* True to fail localtime() calls */
19311 int (*xAltLocaltime)(const void*,void*); /* Alternative localtime() routine */
19312 int iOnceResetThreshold; /* When to reset OP_Once counters */
19313 u32 szSorterRef; /* Min size in bytes to use sorter-refs */
19314 unsigned int iPrngSeed; /* Alternative fixed seed for the PRNG */
19315 /* vvvv--- must be last ---vvv */
19316#ifdef SQLITE_DEBUG
19317 sqlite3_int64 aTune[SQLITE_NTUNE6]; /* Tuning parameters */
19318#endif
19319};
19320
19321/*
19322** This macro is used inside of assert() statements to indicate that
19323** the assert is only valid on a well-formed database. Instead of:
19324**
19325** assert( X );
19326**
19327** One writes:
19328**
19329** assert( X || CORRUPT_DB );
19330**
19331** CORRUPT_DB is true during normal operation. CORRUPT_DB does not indicate
19332** that the database is definitely corrupt, only that it might be corrupt.
19333** For most test cases, CORRUPT_DB is set to false using a special
19334** sqlite3_test_control(). This enables assert() statements to prove
19335** things that are always true for well-formed databases.
19336*/
19337#define CORRUPT_DB(sqlite3Config.neverCorrupt==0) (sqlite3Config.neverCorrupt==0)
19338
19339/*
19340** Context pointer passed down through the tree-walk.
19341*/
19342struct Walker {
19343 Parse *pParse; /* Parser context. */
19344 int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */
19345 int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */
19346 void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */
19347 int walkerDepth; /* Number of subqueries */
19348 u16 eCode; /* A small processing code */
19349 union { /* Extra data for callback */
19350 NameContext *pNC; /* Naming context */
19351 int n; /* A counter */
19352 int iCur; /* A cursor number */
19353 SrcList *pSrcList; /* FROM clause */
19354 struct CCurHint *pCCurHint; /* Used by codeCursorHint() */
19355 struct RefSrcList *pRefSrcList; /* sqlite3ReferencesSrcList() */
19356 int *aiCol; /* array of column indexes */
19357 struct IdxCover *pIdxCover; /* Check for index coverage */
19358 struct IdxExprTrans *pIdxTrans; /* Convert idxed expr to column */
19359 ExprList *pGroupBy; /* GROUP BY clause */
19360 Select *pSelect; /* HAVING to WHERE clause ctx */
19361 struct WindowRewrite *pRewrite; /* Window rewrite context */
19362 struct WhereConst *pConst; /* WHERE clause constants */
19363 struct RenameCtx *pRename; /* RENAME COLUMN context */
19364 struct Table *pTab; /* Table of generated column */
19365 SrcItem *pSrcItem; /* A single FROM clause item */
19366 DbFixer *pFix;
19367 } u;
19368};
19369
19370/*
19371** The following structure contains information used by the sqliteFix...
19372** routines as they walk the parse tree to make database references
19373** explicit.
19374*/
19375struct DbFixer {
19376 Parse *pParse; /* The parsing context. Error messages written here */
19377 Walker w; /* Walker object */
19378 Schema *pSchema; /* Fix items to this schema */
19379 u8 bTemp; /* True for TEMP schema entries */
19380 const char *zDb; /* Make sure all objects are contained in this database */
19381 const char *zType; /* Type of the container - used for error messages */
19382 const Token *pName; /* Name of the container - used for error messages */
19383};
19384
19385/* Forward declarations */
19386SQLITE_PRIVATEstatic int sqlite3WalkExpr(Walker*, Expr*);
19387SQLITE_PRIVATEstatic int sqlite3WalkExprList(Walker*, ExprList*);
19388SQLITE_PRIVATEstatic int sqlite3WalkSelect(Walker*, Select*);
19389SQLITE_PRIVATEstatic int sqlite3WalkSelectExpr(Walker*, Select*);
19390SQLITE_PRIVATEstatic int sqlite3WalkSelectFrom(Walker*, Select*);
19391SQLITE_PRIVATEstatic int sqlite3ExprWalkNoop(Walker*, Expr*);
19392SQLITE_PRIVATEstatic int sqlite3SelectWalkNoop(Walker*, Select*);
19393SQLITE_PRIVATEstatic int sqlite3SelectWalkFail(Walker*, Select*);
19394SQLITE_PRIVATEstatic int sqlite3WalkerDepthIncrease(Walker*,Select*);
19395SQLITE_PRIVATEstatic void sqlite3WalkerDepthDecrease(Walker*,Select*);
19396SQLITE_PRIVATEstatic void sqlite3WalkWinDefnDummyCallback(Walker*,Select*);
19397
19398#ifdef SQLITE_DEBUG
19399SQLITE_PRIVATEstatic void sqlite3SelectWalkAssert2(Walker*, Select*);
19400#endif
19401
19402#ifndef SQLITE_OMIT_CTE
19403SQLITE_PRIVATEstatic void sqlite3SelectPopWith(Walker*, Select*);
19404#else
19405# define sqlite3SelectPopWith 0
19406#endif
19407
19408/*
19409** Return code from the parse-tree walking primitives and their
19410** callbacks.
19411*/
19412#define WRC_Continue0 0 /* Continue down into children */
19413#define WRC_Prune1 1 /* Omit children but continue walking siblings */
19414#define WRC_Abort2 2 /* Abandon the tree walk */
19415
19416/*
19417** A single common table expression
19418*/
19419struct Cte {
19420 char *zName; /* Name of this CTE */
19421 ExprList *pCols; /* List of explicit column names, or NULL */
19422 Select *pSelect; /* The definition of this CTE */
19423 const char *zCteErr; /* Error message for circular references */
19424 CteUse *pUse; /* Usage information for this CTE */
19425 u8 eM10d; /* The MATERIALIZED flag */
19426};
19427
19428/*
19429** Allowed values for the materialized flag (eM10d):
19430*/
19431#define M10d_Yes0 0 /* AS MATERIALIZED */
19432#define M10d_Any1 1 /* Not specified. Query planner's choice */
19433#define M10d_No2 2 /* AS NOT MATERIALIZED */
19434
19435/*
19436** An instance of the With object represents a WITH clause containing
19437** one or more CTEs (common table expressions).
19438*/
19439struct With {
19440 int nCte; /* Number of CTEs in the WITH clause */
19441 int bView; /* Belongs to the outermost Select of a view */
19442 With *pOuter; /* Containing WITH clause, or NULL */
19443 Cte a[1]; /* For each CTE in the WITH clause.... */
19444};
19445
19446/*
19447** The Cte object is not guaranteed to persist for the entire duration
19448** of code generation. (The query flattener or other parser tree
19449** edits might delete it.) The following object records information
19450** about each Common Table Expression that must be preserved for the
19451** duration of the parse.
19452**
19453** The CteUse objects are freed using sqlite3ParserAddCleanup() rather
19454** than sqlite3SelectDelete(), which is what enables them to persist
19455** until the end of code generation.
19456*/
19457struct CteUse {
19458 int nUse; /* Number of users of this CTE */
19459 int addrM9e; /* Start of subroutine to compute materialization */
19460 int regRtn; /* Return address register for addrM9e subroutine */
19461 int iCur; /* Ephemeral table holding the materialization */
19462 LogEst nRowEst; /* Estimated number of rows in the table */
19463 u8 eM10d; /* The MATERIALIZED flag */
19464};
19465
19466
19467#ifdef SQLITE_DEBUG
19468/*
19469** An instance of the TreeView object is used for printing the content of
19470** data structures on sqlite3DebugPrintf() using a tree-like view.
19471*/
19472struct TreeView {
19473 int iLevel; /* Which level of the tree we are on */
19474 u8 bLine[100]; /* Draw vertical in column i if bLine[i] is true */
19475};
19476#endif /* SQLITE_DEBUG */
19477
19478/*
19479** This object is used in various ways, most (but not all) related to window
19480** functions.
19481**
19482** (1) A single instance of this structure is attached to the
19483** the Expr.y.pWin field for each window function in an expression tree.
19484** This object holds the information contained in the OVER clause,
19485** plus additional fields used during code generation.
19486**
19487** (2) All window functions in a single SELECT form a linked-list
19488** attached to Select.pWin. The Window.pFunc and Window.pExpr
19489** fields point back to the expression that is the window function.
19490**
19491** (3) The terms of the WINDOW clause of a SELECT are instances of this
19492** object on a linked list attached to Select.pWinDefn.
19493**
19494** (4) For an aggregate function with a FILTER clause, an instance
19495** of this object is stored in Expr.y.pWin with eFrmType set to
19496** TK_FILTER. In this case the only field used is Window.pFilter.
19497**
19498** The uses (1) and (2) are really the same Window object that just happens
19499** to be accessible in two different ways. Use case (3) are separate objects.
19500*/
19501struct Window {
19502 char *zName; /* Name of window (may be NULL) */
19503 char *zBase; /* Name of base window for chaining (may be NULL) */
19504 ExprList *pPartition; /* PARTITION BY clause */
19505 ExprList *pOrderBy; /* ORDER BY clause */
19506 u8 eFrmType; /* TK_RANGE, TK_GROUPS, TK_ROWS, or 0 */
19507 u8 eStart; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
19508 u8 eEnd; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
19509 u8 bImplicitFrame; /* True if frame was implicitly specified */
19510 u8 eExclude; /* TK_NO, TK_CURRENT, TK_TIES, TK_GROUP, or 0 */
19511 Expr *pStart; /* Expression for "<expr> PRECEDING" */
19512 Expr *pEnd; /* Expression for "<expr> FOLLOWING" */
19513 Window **ppThis; /* Pointer to this object in Select.pWin list */
19514 Window *pNextWin; /* Next window function belonging to this SELECT */
19515 Expr *pFilter; /* The FILTER expression */
19516 FuncDef *pWFunc; /* The function */
19517 int iEphCsr; /* Partition buffer or Peer buffer */
19518 int regAccum; /* Accumulator */
19519 int regResult; /* Interim result */
19520 int csrApp; /* Function cursor (used by min/max) */
19521 int regApp; /* Function register (also used by min/max) */
19522 int regPart; /* Array of registers for PARTITION BY values */
19523 Expr *pOwner; /* Expression object this window is attached to */
19524 int nBufferCol; /* Number of columns in buffer table */
19525 int iArgCol; /* Offset of first argument for this function */
19526 int regOne; /* Register containing constant value 1 */
19527 int regStartRowid;
19528 int regEndRowid;
19529 u8 bExprArgs; /* Defer evaluation of window function arguments
19530 ** due to the SQLITE_SUBTYPE flag */
19531};
19532
19533#ifndef SQLITE_OMIT_WINDOWFUNC
19534SQLITE_PRIVATEstatic void sqlite3WindowDelete(sqlite3*, Window*);
19535SQLITE_PRIVATEstatic void sqlite3WindowUnlinkFromSelect(Window*);
19536SQLITE_PRIVATEstatic void sqlite3WindowListDelete(sqlite3 *db, Window *p);
19537SQLITE_PRIVATEstatic Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*, u8);
19538SQLITE_PRIVATEstatic void sqlite3WindowAttach(Parse*, Expr*, Window*);
19539SQLITE_PRIVATEstatic void sqlite3WindowLink(Select *pSel, Window *pWin);
19540SQLITE_PRIVATEstatic int sqlite3WindowCompare(const Parse*, const Window*, const Window*, int);
19541SQLITE_PRIVATEstatic void sqlite3WindowCodeInit(Parse*, Select*);
19542SQLITE_PRIVATEstatic void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int);
19543SQLITE_PRIVATEstatic int sqlite3WindowRewrite(Parse*, Select*);
19544SQLITE_PRIVATEstatic void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*);
19545SQLITE_PRIVATEstatic Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p);
19546SQLITE_PRIVATEstatic Window *sqlite3WindowListDup(sqlite3 *db, Window *p);
19547SQLITE_PRIVATEstatic void sqlite3WindowFunctions(void);
19548SQLITE_PRIVATEstatic void sqlite3WindowChain(Parse*, Window*, Window*);
19549SQLITE_PRIVATEstatic Window *sqlite3WindowAssemble(Parse*, Window*, ExprList*, ExprList*, Token*);
19550#else
19551# define sqlite3WindowDelete(a,b)
19552# define sqlite3WindowFunctions()
19553# define sqlite3WindowAttach(a,b,c)
19554#endif
19555
19556/*
19557** Assuming zIn points to the first byte of a UTF-8 character,
19558** advance zIn to point to the first byte of the next UTF-8 character.
19559*/
19560#define SQLITE_SKIP_UTF8(zIn){ if( (*(zIn++))>=0xc0 ){ while( (*zIn & 0xc0)==0x80 )
{ zIn++; } } }
{ \
19561 if( (*(zIn++))>=0xc0 ){ \
19562 while( (*zIn & 0xc0)==0x80 ){ zIn++; } \
19563 } \
19564}
19565
19566/*
19567** The SQLITE_*_BKPT macros are substitutes for the error codes with
19568** the same name but without the _BKPT suffix. These macros invoke
19569** routines that report the line-number on which the error originated
19570** using sqlite3_log(). The routines also provide a convenient place
19571** to set a debugger breakpoint.
19572*/
19573SQLITE_PRIVATEstatic int sqlite3ReportError(int iErr, int lineno, const char *zType);
19574SQLITE_PRIVATEstatic int sqlite3CorruptError(int);
19575SQLITE_PRIVATEstatic int sqlite3MisuseError(int);
19576SQLITE_PRIVATEstatic int sqlite3CantopenError(int);
19577#define SQLITE_CORRUPT_BKPTsqlite3CorruptError(19577) sqlite3CorruptError(__LINE__19577)
19578#define SQLITE_MISUSE_BKPTsqlite3MisuseError(19578) sqlite3MisuseError(__LINE__19578)
19579#define SQLITE_CANTOPEN_BKPTsqlite3CantopenError(19579) sqlite3CantopenError(__LINE__19579)
19580#ifdef SQLITE_DEBUG
19581SQLITE_PRIVATEstatic int sqlite3NomemError(int);
19582SQLITE_PRIVATEstatic int sqlite3IoerrnomemError(int);
19583# define SQLITE_NOMEM_BKPT7 sqlite3NomemError(__LINE__19583)
19584# define SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8)) sqlite3IoerrnomemError(__LINE__19584)
19585#else
19586# define SQLITE_NOMEM_BKPT7 SQLITE_NOMEM7
19587# define SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8)) SQLITE_IOERR_NOMEM(10 | (12<<8))
19588#endif
19589#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_CORRUPT_PGNO)
19590SQLITE_PRIVATEstatic int sqlite3CorruptPgnoError(int,Pgno);
19591# define SQLITE_CORRUPT_PGNO(P)sqlite3CorruptError(19591) sqlite3CorruptPgnoError(__LINE__19591,(P))
19592#else
19593# define SQLITE_CORRUPT_PGNO(P)sqlite3CorruptError(19593) sqlite3CorruptError(__LINE__19593)
19594#endif
19595
19596/*
19597** FTS3 and FTS4 both require virtual table support
19598*/
19599#if defined(SQLITE_OMIT_VIRTUALTABLE)
19600# undef SQLITE_ENABLE_FTS31
19601# undef SQLITE_ENABLE_FTS41
19602#endif
19603
19604/*
19605** FTS4 is really an extension for FTS3. It is enabled using the
19606** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also call
19607** the SQLITE_ENABLE_FTS4 macro to serve as an alias for SQLITE_ENABLE_FTS3.
19608*/
19609#if defined(SQLITE_ENABLE_FTS41) && !defined(SQLITE_ENABLE_FTS31)
19610# define SQLITE_ENABLE_FTS31 1
19611#endif
19612
19613/*
19614** The ctype.h header is needed for non-ASCII systems. It is also
19615** needed by FTS3 when FTS3 is included in the amalgamation.
19616*/
19617#if !defined(SQLITE_ASCII1) || \
19618 (defined(SQLITE_ENABLE_FTS31) && defined(SQLITE_AMALGAMATION1))
19619# include <ctype.h>
19620#endif
19621
19622/*
19623** The following macros mimic the standard library functions toupper(),
19624** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
19625** sqlite versions only work for ASCII characters, regardless of locale.
19626*/
19627#ifdef SQLITE_ASCII1
19628# define sqlite3Toupper(x)((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20)) ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
19629# define sqlite3Isspace(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x01) (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
19630# define sqlite3Isalnum(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x06) (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
19631# define sqlite3Isalpha(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x02) (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
19632# define sqlite3Isdigit(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x04) (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
19633# define sqlite3Isxdigit(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x08) (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
19634# define sqlite3Tolower(x)(sqlite3UpperToLower[(unsigned char)(x)]) (sqlite3UpperToLower[(unsigned char)(x)])
19635# define sqlite3Isquote(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x80) (sqlite3CtypeMap[(unsigned char)(x)]&0x80)
19636#else
19637# 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; }))
19638# define sqlite3Isspace(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x01) isspace((unsigned char)(x))((*__ctype_b_loc ())[(int) (((unsigned char)(x)))] & (unsigned
short int) _ISspace)
19639# define sqlite3Isalnum(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x06) isalnum((unsigned char)(x))((*__ctype_b_loc ())[(int) (((unsigned char)(x)))] & (unsigned
short int) _ISalnum)
19640# define sqlite3Isalpha(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x02) isalpha((unsigned char)(x))((*__ctype_b_loc ())[(int) (((unsigned char)(x)))] & (unsigned
short int) _ISalpha)
19641# define sqlite3Isdigit(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x04) isdigit((unsigned char)(x))((*__ctype_b_loc ())[(int) (((unsigned char)(x)))] & (unsigned
short int) _ISdigit)
19642# define sqlite3Isxdigit(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x08) isxdigit((unsigned char)(x))((*__ctype_b_loc ())[(int) (((unsigned char)(x)))] & (unsigned
short int) _ISxdigit)
19643# 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; }))
19644# define sqlite3Isquote(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x80) ((x)=='"'||(x)=='\''||(x)=='['||(x)=='`')
19645#endif
19646SQLITE_PRIVATEstatic int sqlite3IsIdChar(u8);
19647
19648/*
19649** Internal function prototypes
19650*/
19651SQLITE_PRIVATEstatic int sqlite3StrICmp(const char*,const char*);
19652SQLITE_PRIVATEstatic int sqlite3Strlen30(const char*);
19653#define sqlite3Strlen30NN(C)(strlen(C)&0x3fffffff) (strlen(C)&0x3fffffff)
19654SQLITE_PRIVATEstatic char *sqlite3ColumnType(Column*,char*);
19655#define sqlite3StrNICmpsqlite3_strnicmp sqlite3_strnicmp
19656
19657SQLITE_PRIVATEstatic int sqlite3MallocInit(void);
19658SQLITE_PRIVATEstatic void sqlite3MallocEnd(void);
19659SQLITE_PRIVATEstatic void *sqlite3Malloc(u64);
19660SQLITE_PRIVATEstatic void *sqlite3MallocZero(u64);
19661SQLITE_PRIVATEstatic void *sqlite3DbMallocZero(sqlite3*, u64);
19662SQLITE_PRIVATEstatic void *sqlite3DbMallocRaw(sqlite3*, u64);
19663SQLITE_PRIVATEstatic void *sqlite3DbMallocRawNN(sqlite3*, u64);
19664SQLITE_PRIVATEstatic char *sqlite3DbStrDup(sqlite3*,const char*);
19665SQLITE_PRIVATEstatic char *sqlite3DbStrNDup(sqlite3*,const char*, u64);
19666SQLITE_PRIVATEstatic char *sqlite3DbSpanDup(sqlite3*,const char*,const char*);
19667SQLITE_PRIVATEstatic void *sqlite3Realloc(void*, u64);
19668SQLITE_PRIVATEstatic void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
19669SQLITE_PRIVATEstatic void *sqlite3DbRealloc(sqlite3 *, void *, u64);
19670SQLITE_PRIVATEstatic void sqlite3DbFree(sqlite3*, void*);
19671SQLITE_PRIVATEstatic void sqlite3DbFreeNN(sqlite3*, void*);
19672SQLITE_PRIVATEstatic int sqlite3MallocSize(const void*);
19673SQLITE_PRIVATEstatic int sqlite3DbMallocSize(sqlite3*, const void*);
19674SQLITE_PRIVATEstatic void *sqlite3PageMalloc(int);
19675SQLITE_PRIVATEstatic void sqlite3PageFree(void*);
19676SQLITE_PRIVATEstatic void sqlite3MemSetDefault(void);
19677#ifndef SQLITE_UNTESTABLE
19678SQLITE_PRIVATEstatic void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
19679#endif
19680SQLITE_PRIVATEstatic int sqlite3HeapNearlyFull(void);
19681
19682/*
19683** On systems with ample stack space and that support alloca(), make
19684** use of alloca() to obtain space for large automatic objects. By default,
19685** obtain space from malloc().
19686**
19687** The alloca() routine never returns NULL. This will cause code paths
19688** that deal with sqlite3StackAlloc() failures to be unreachable.
19689*/
19690#ifdef SQLITE_USE_ALLOCA
19691# define sqlite3StackAllocRaw(D,N)sqlite3DbMallocRaw(D,N) alloca(N)__builtin_alloca (N)
19692# define sqlite3StackAllocZero(D,N)sqlite3DbMallocZero(D,N) memset(alloca(N)__builtin_alloca (N), 0, N)
19693# define sqlite3StackFree(D,P)sqlite3DbFree(D,P)
19694#else
19695# define sqlite3StackAllocRaw(D,N)sqlite3DbMallocRaw(D,N) sqlite3DbMallocRaw(D,N)
19696# define sqlite3StackAllocZero(D,N)sqlite3DbMallocZero(D,N) sqlite3DbMallocZero(D,N)
19697# define sqlite3StackFree(D,P)sqlite3DbFree(D,P) sqlite3DbFree(D,P)
19698#endif
19699
19700/* Do not allow both MEMSYS5 and MEMSYS3 to be defined together. If they
19701** are, disable MEMSYS3
19702*/
19703#ifdef SQLITE_ENABLE_MEMSYS5
19704SQLITE_PRIVATEstatic const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
19705#undef SQLITE_ENABLE_MEMSYS3
19706#endif
19707#ifdef SQLITE_ENABLE_MEMSYS3
19708SQLITE_PRIVATEstatic const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
19709#endif
19710
19711
19712#ifndef SQLITE_MUTEX_OMIT
19713SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
19714SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3NoopMutex(void);
19715SQLITE_PRIVATEstatic sqlite3_mutex *sqlite3MutexAlloc(int);
19716SQLITE_PRIVATEstatic int sqlite3MutexInit(void);
19717SQLITE_PRIVATEstatic int sqlite3MutexEnd(void);
19718#endif
19719#if !defined(SQLITE_MUTEX_OMIT) && !defined(SQLITE_MUTEX_NOOP)
19720SQLITE_PRIVATEstatic void sqlite3MemoryBarrier(void);
19721#else
19722# define sqlite3MemoryBarrier()
19723#endif
19724
19725SQLITE_PRIVATEstatic sqlite3_int64 sqlite3StatusValue(int);
19726SQLITE_PRIVATEstatic void sqlite3StatusUp(int, int);
19727SQLITE_PRIVATEstatic void sqlite3StatusDown(int, int);
19728SQLITE_PRIVATEstatic void sqlite3StatusHighwater(int, int);
19729SQLITE_PRIVATEstatic int sqlite3LookasideUsed(sqlite3*,int*);
19730
19731/* Access to mutexes used by sqlite3_status() */
19732SQLITE_PRIVATEstatic sqlite3_mutex *sqlite3Pcache1Mutex(void);
19733SQLITE_PRIVATEstatic sqlite3_mutex *sqlite3MallocMutex(void);
19734
19735#if defined(SQLITE_ENABLE_MULTITHREADED_CHECKS) && !defined(SQLITE_MUTEX_OMIT)
19736SQLITE_PRIVATEstatic void sqlite3MutexWarnOnContention(sqlite3_mutex*);
19737#else
19738# define sqlite3MutexWarnOnContention(x)
19739#endif
19740
19741#ifndef SQLITE_OMIT_FLOATING_POINT
19742# define EXP754(((u64)0x7ff)<<52) (((u64)0x7ff)<<52)
19743# define MAN754((((u64)1)<<52)-1) ((((u64)1)<<52)-1)
19744# 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)
19745SQLITE_PRIVATEstatic int sqlite3IsNaN(double);
19746#else
19747# define IsNaN(X)(((X)&(((u64)0x7ff)<<52))==(((u64)0x7ff)<<52)
&& ((X)&((((u64)1)<<52)-1))!=0)
0
19748# define sqlite3IsNaN(X) 0
19749#endif
19750
19751/*
19752** An instance of the following structure holds information about SQL
19753** functions arguments that are the parameters to the printf() function.
19754*/
19755struct PrintfArguments {
19756 int nArg; /* Total number of arguments */
19757 int nUsed; /* Number of arguments used so far */
19758 sqlite3_value **apArg; /* The argument values */
19759};
19760
19761SQLITE_PRIVATEstatic char *sqlite3MPrintf(sqlite3*,const char*, ...);
19762SQLITE_PRIVATEstatic char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
19763#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
19764SQLITE_PRIVATEstatic void sqlite3DebugPrintf(const char*, ...);
19765#endif
19766#if defined(SQLITE_TEST)
19767SQLITE_PRIVATEstatic void *sqlite3TestTextToPtr(const char*);
19768#endif
19769
19770#if defined(SQLITE_DEBUG)
19771SQLITE_PRIVATEstatic void sqlite3TreeViewLine(TreeView*, const char *zFormat, ...);
19772SQLITE_PRIVATEstatic void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
19773SQLITE_PRIVATEstatic void sqlite3TreeViewBareExprList(TreeView*, const ExprList*, const char*);
19774SQLITE_PRIVATEstatic void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
19775SQLITE_PRIVATEstatic void sqlite3TreeViewBareIdList(TreeView*, const IdList*, const char*);
19776SQLITE_PRIVATEstatic void sqlite3TreeViewIdList(TreeView*, const IdList*, u8, const char*);
19777SQLITE_PRIVATEstatic void sqlite3TreeViewColumnList(TreeView*, const Column*, int, u8);
19778SQLITE_PRIVATEstatic void sqlite3TreeViewSrcList(TreeView*, const SrcList*);
19779SQLITE_PRIVATEstatic void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
19780SQLITE_PRIVATEstatic void sqlite3TreeViewWith(TreeView*, const With*, u8);
19781SQLITE_PRIVATEstatic void sqlite3TreeViewUpsert(TreeView*, const Upsert*, u8);
19782#if TREETRACE_ENABLED0
19783SQLITE_PRIVATEstatic void sqlite3TreeViewDelete(const With*, const SrcList*, const Expr*,
19784 const ExprList*,const Expr*, const Trigger*);
19785SQLITE_PRIVATEstatic void sqlite3TreeViewInsert(const With*, const SrcList*,
19786 const IdList*, const Select*, const ExprList*,
19787 int, const Upsert*, const Trigger*);
19788SQLITE_PRIVATEstatic void sqlite3TreeViewUpdate(const With*, const SrcList*, const ExprList*,
19789 const Expr*, int, const ExprList*, const Expr*,
19790 const Upsert*, const Trigger*);
19791#endif
19792#ifndef SQLITE_OMIT_TRIGGER
19793SQLITE_PRIVATEstatic void sqlite3TreeViewTriggerStep(TreeView*, const TriggerStep*, u8, u8);
19794SQLITE_PRIVATEstatic void sqlite3TreeViewTrigger(TreeView*, const Trigger*, u8, u8);
19795#endif
19796#ifndef SQLITE_OMIT_WINDOWFUNC
19797SQLITE_PRIVATEstatic void sqlite3TreeViewWindow(TreeView*, const Window*, u8);
19798SQLITE_PRIVATEstatic void sqlite3TreeViewWinFunc(TreeView*, const Window*, u8);
19799#endif
19800SQLITE_PRIVATEstatic void sqlite3ShowExpr(const Expr*);
19801SQLITE_PRIVATEstatic void sqlite3ShowExprList(const ExprList*);
19802SQLITE_PRIVATEstatic void sqlite3ShowIdList(const IdList*);
19803SQLITE_PRIVATEstatic void sqlite3ShowSrcList(const SrcList*);
19804SQLITE_PRIVATEstatic void sqlite3ShowSelect(const Select*);
19805SQLITE_PRIVATEstatic void sqlite3ShowWith(const With*);
19806SQLITE_PRIVATEstatic void sqlite3ShowUpsert(const Upsert*);
19807#ifndef SQLITE_OMIT_TRIGGER
19808SQLITE_PRIVATEstatic void sqlite3ShowTriggerStep(const TriggerStep*);
19809SQLITE_PRIVATEstatic void sqlite3ShowTriggerStepList(const TriggerStep*);
19810SQLITE_PRIVATEstatic void sqlite3ShowTrigger(const Trigger*);
19811SQLITE_PRIVATEstatic void sqlite3ShowTriggerList(const Trigger*);
19812#endif
19813#ifndef SQLITE_OMIT_WINDOWFUNC
19814SQLITE_PRIVATEstatic void sqlite3ShowWindow(const Window*);
19815SQLITE_PRIVATEstatic void sqlite3ShowWinFunc(const Window*);
19816#endif
19817#endif
19818
19819SQLITE_PRIVATEstatic void sqlite3SetString(char **, sqlite3*, const char*);
19820SQLITE_PRIVATEstatic void sqlite3ErrorMsg(Parse*, const char*, ...);
19821SQLITE_PRIVATEstatic int sqlite3ErrorToParser(sqlite3*,int);
19822SQLITE_PRIVATEstatic void sqlite3Dequote(char*);
19823SQLITE_PRIVATEstatic void sqlite3DequoteExpr(Expr*);
19824SQLITE_PRIVATEstatic void sqlite3DequoteToken(Token*);
19825SQLITE_PRIVATEstatic void sqlite3TokenInit(Token*,char*);
19826SQLITE_PRIVATEstatic int sqlite3KeywordCode(const unsigned char*, int);
19827SQLITE_PRIVATEstatic int sqlite3RunParser(Parse*, const char*);
19828SQLITE_PRIVATEstatic void sqlite3FinishCoding(Parse*);
19829SQLITE_PRIVATEstatic int sqlite3GetTempReg(Parse*);
19830SQLITE_PRIVATEstatic void sqlite3ReleaseTempReg(Parse*,int);
19831SQLITE_PRIVATEstatic int sqlite3GetTempRange(Parse*,int);
19832SQLITE_PRIVATEstatic void sqlite3ReleaseTempRange(Parse*,int,int);
19833SQLITE_PRIVATEstatic void sqlite3ClearTempRegCache(Parse*);
19834#ifdef SQLITE_DEBUG
19835SQLITE_PRIVATEstatic int sqlite3NoTempsInRange(Parse*,int,int);
19836#endif
19837SQLITE_PRIVATEstatic Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
19838SQLITE_PRIVATEstatic Expr *sqlite3Expr(sqlite3*,int,const char*);
19839SQLITE_PRIVATEstatic void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
19840SQLITE_PRIVATEstatic Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*);
19841SQLITE_PRIVATEstatic void sqlite3PExprAddSelect(Parse*, Expr*, Select*);
19842SQLITE_PRIVATEstatic Expr *sqlite3ExprAnd(Parse*,Expr*, Expr*);
19843SQLITE_PRIVATEstatic Expr *sqlite3ExprSimplifiedAndOr(Expr*);
19844SQLITE_PRIVATEstatic Expr *sqlite3ExprFunction(Parse*,ExprList*, const Token*, int);
19845SQLITE_PRIVATEstatic void sqlite3ExprFunctionUsable(Parse*,const Expr*,const FuncDef*);
19846SQLITE_PRIVATEstatic void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32);
19847SQLITE_PRIVATEstatic void sqlite3ExprDelete(sqlite3*, Expr*);
19848SQLITE_PRIVATEstatic void sqlite3ExprDeferredDelete(Parse*, Expr*);
19849SQLITE_PRIVATEstatic void sqlite3ExprUnmapAndDelete(Parse*, Expr*);
19850SQLITE_PRIVATEstatic ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
19851SQLITE_PRIVATEstatic ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*);
19852SQLITE_PRIVATEstatic Select *sqlite3ExprListToValues(Parse*, int, ExprList*);
19853SQLITE_PRIVATEstatic void sqlite3ExprListSetSortOrder(ExprList*,int,int);
19854SQLITE_PRIVATEstatic void sqlite3ExprListSetName(Parse*,ExprList*,const Token*,int);
19855SQLITE_PRIVATEstatic void sqlite3ExprListSetSpan(Parse*,ExprList*,const char*,const char*);
19856SQLITE_PRIVATEstatic void sqlite3ExprListDelete(sqlite3*, ExprList*);
19857SQLITE_PRIVATEstatic u32 sqlite3ExprListFlags(const ExprList*);
19858SQLITE_PRIVATEstatic int sqlite3IndexHasDuplicateRootPage(Index*);
19859SQLITE_PRIVATEstatic int sqlite3Init(sqlite3*, char**);
19860SQLITE_PRIVATEstatic int sqlite3InitCallback(void*, int, char**, char**);
19861SQLITE_PRIVATEstatic int sqlite3InitOne(sqlite3*, int, char**, u32);
19862SQLITE_PRIVATEstatic void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
19863#ifndef SQLITE_OMIT_VIRTUALTABLE
19864SQLITE_PRIVATEstatic Module *sqlite3PragmaVtabRegister(sqlite3*,const char *zName);
19865#endif
19866SQLITE_PRIVATEstatic void sqlite3ResetAllSchemasOfConnection(sqlite3*);
19867SQLITE_PRIVATEstatic void sqlite3ResetOneSchema(sqlite3*,int);
19868SQLITE_PRIVATEstatic void sqlite3CollapseDatabaseArray(sqlite3*);
19869SQLITE_PRIVATEstatic void sqlite3CommitInternalChanges(sqlite3*);
19870SQLITE_PRIVATEstatic void sqlite3ColumnSetExpr(Parse*,Table*,Column*,Expr*);
19871SQLITE_PRIVATEstatic Expr *sqlite3ColumnExpr(Table*,Column*);
19872SQLITE_PRIVATEstatic void sqlite3ColumnSetColl(sqlite3*,Column*,const char*zColl);
19873SQLITE_PRIVATEstatic const char *sqlite3ColumnColl(Column*);
19874SQLITE_PRIVATEstatic void sqlite3DeleteColumnNames(sqlite3*,Table*);
19875SQLITE_PRIVATEstatic void sqlite3GenerateColumnNames(Parse *pParse, Select *pSelect);
19876SQLITE_PRIVATEstatic int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**);
19877SQLITE_PRIVATEstatic void sqlite3SelectAddColumnTypeAndCollation(Parse*,Table*,Select*,char);
19878SQLITE_PRIVATEstatic Table *sqlite3ResultSetOfSelect(Parse*,Select*,char);
19879SQLITE_PRIVATEstatic void sqlite3OpenSchemaTable(Parse *, int);
19880SQLITE_PRIVATEstatic Index *sqlite3PrimaryKeyIndex(Table*);
19881SQLITE_PRIVATEstatic i16 sqlite3TableColumnToIndex(Index*, i16);
19882#ifdef SQLITE_OMIT_GENERATED_COLUMNS
19883# define sqlite3TableColumnToStorage(T,X) (X) /* No-op pass-through */
19884# define sqlite3StorageColumnToTable(T,X) (X) /* No-op pass-through */
19885#else
19886SQLITE_PRIVATEstatic i16 sqlite3TableColumnToStorage(Table*, i16);
19887SQLITE_PRIVATEstatic i16 sqlite3StorageColumnToTable(Table*, i16);
19888#endif
19889SQLITE_PRIVATEstatic void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
19890#if SQLITE_ENABLE_HIDDEN_COLUMNS
19891SQLITE_PRIVATEstatic void sqlite3ColumnPropertiesFromName(Table*, Column*);
19892#else
19893# define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
19894#endif
19895SQLITE_PRIVATEstatic void sqlite3AddColumn(Parse*,Token,Token);
19896SQLITE_PRIVATEstatic void sqlite3AddNotNull(Parse*, int);
19897SQLITE_PRIVATEstatic void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
19898SQLITE_PRIVATEstatic void sqlite3AddCheckConstraint(Parse*, Expr*, const char*, const char*);
19899SQLITE_PRIVATEstatic void sqlite3AddDefaultValue(Parse*,Expr*,const char*,const char*);
19900SQLITE_PRIVATEstatic void sqlite3AddCollateType(Parse*, Token*);
19901SQLITE_PRIVATEstatic void sqlite3AddGenerated(Parse*,Expr*,Token*);
19902SQLITE_PRIVATEstatic void sqlite3EndTable(Parse*,Token*,Token*,u32,Select*);
19903SQLITE_PRIVATEstatic void sqlite3AddReturning(Parse*,ExprList*);
19904SQLITE_PRIVATEstatic int sqlite3ParseUri(const char*,const char*,unsigned int*,
19905 sqlite3_vfs**,char**,char **);
19906#define sqlite3CodecQueryParameters(A,B,C)0 0
19907SQLITE_PRIVATEstatic Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
19908
19909#ifdef SQLITE_UNTESTABLE
19910# define sqlite3FaultSim(X) SQLITE_OK0
19911#else
19912SQLITE_PRIVATEstatic int sqlite3FaultSim(int);
19913#endif
19914
19915SQLITE_PRIVATEstatic Bitvec *sqlite3BitvecCreate(u32);
19916SQLITE_PRIVATEstatic int sqlite3BitvecTest(Bitvec*, u32);
19917SQLITE_PRIVATEstatic int sqlite3BitvecTestNotNull(Bitvec*, u32);
19918SQLITE_PRIVATEstatic int sqlite3BitvecSet(Bitvec*, u32);
19919SQLITE_PRIVATEstatic void sqlite3BitvecClear(Bitvec*, u32, void*);
19920SQLITE_PRIVATEstatic void sqlite3BitvecDestroy(Bitvec*);
19921SQLITE_PRIVATEstatic u32 sqlite3BitvecSize(Bitvec*);
19922#ifndef SQLITE_UNTESTABLE
19923SQLITE_PRIVATEstatic int sqlite3BitvecBuiltinTest(int,int*);
19924#endif
19925
19926SQLITE_PRIVATEstatic RowSet *sqlite3RowSetInit(sqlite3*);
19927SQLITE_PRIVATEstatic void sqlite3RowSetDelete(void*);
19928SQLITE_PRIVATEstatic void sqlite3RowSetClear(void*);
19929SQLITE_PRIVATEstatic void sqlite3RowSetInsert(RowSet*, i64);
19930SQLITE_PRIVATEstatic int sqlite3RowSetTest(RowSet*, int iBatch, i64);
19931SQLITE_PRIVATEstatic int sqlite3RowSetNext(RowSet*, i64*);
19932
19933SQLITE_PRIVATEstatic void sqlite3CreateView(Parse*,Token*,Token*,Token*,ExprList*,Select*,int,int);
19934
19935#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
19936SQLITE_PRIVATEstatic int sqlite3ViewGetColumnNames(Parse*,Table*);
19937#else
19938# define sqlite3ViewGetColumnNames(A,B) 0
19939#endif
19940
19941#if SQLITE_MAX_ATTACHED10>30
19942SQLITE_PRIVATEstatic int sqlite3DbMaskAllZero(yDbMask);
19943#endif
19944SQLITE_PRIVATEstatic void sqlite3DropTable(Parse*, SrcList*, int, int);
19945SQLITE_PRIVATEstatic void sqlite3CodeDropTable(Parse*, Table*, int, int);
19946SQLITE_PRIVATEstatic void sqlite3DeleteTable(sqlite3*, Table*);
19947SQLITE_PRIVATEstatic void sqlite3FreeIndex(sqlite3*, Index*);
19948#ifndef SQLITE_OMIT_AUTOINCREMENT
19949SQLITE_PRIVATEstatic void sqlite3AutoincrementBegin(Parse *pParse);
19950SQLITE_PRIVATEstatic void sqlite3AutoincrementEnd(Parse *pParse);
19951#else
19952# define sqlite3AutoincrementBegin(X)
19953# define sqlite3AutoincrementEnd(X)
19954#endif
19955SQLITE_PRIVATEstatic void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int, Upsert*);
19956#ifndef SQLITE_OMIT_GENERATED_COLUMNS
19957SQLITE_PRIVATEstatic void sqlite3ComputeGeneratedColumns(Parse*, int, Table*);
19958#endif
19959SQLITE_PRIVATEstatic void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
19960SQLITE_PRIVATEstatic IdList *sqlite3IdListAppend(Parse*, IdList*, Token*);
19961SQLITE_PRIVATEstatic int sqlite3IdListIndex(IdList*,const char*);
19962SQLITE_PRIVATEstatic SrcList *sqlite3SrcListEnlarge(Parse*, SrcList*, int, int);
19963SQLITE_PRIVATEstatic SrcList *sqlite3SrcListAppendList(Parse *pParse, SrcList *p1, SrcList *p2);
19964SQLITE_PRIVATEstatic SrcList *sqlite3SrcListAppend(Parse*, SrcList*, Token*, Token*);
19965SQLITE_PRIVATEstatic SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
19966 Token*, Select*, OnOrUsing*);
19967SQLITE_PRIVATEstatic void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
19968SQLITE_PRIVATEstatic void sqlite3SrcListFuncArgs(Parse*, SrcList*, ExprList*);
19969SQLITE_PRIVATEstatic int sqlite3IndexedByLookup(Parse *, SrcItem *);
19970SQLITE_PRIVATEstatic void sqlite3SrcListShiftJoinType(Parse*,SrcList*);
19971SQLITE_PRIVATEstatic void sqlite3SrcListAssignCursors(Parse*, SrcList*);
19972SQLITE_PRIVATEstatic void sqlite3IdListDelete(sqlite3*, IdList*);
19973SQLITE_PRIVATEstatic void sqlite3ClearOnOrUsing(sqlite3*, OnOrUsing*);
19974SQLITE_PRIVATEstatic void sqlite3SrcListDelete(sqlite3*, SrcList*);
19975SQLITE_PRIVATEstatic Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**);
19976SQLITE_PRIVATEstatic void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
19977 Expr*, int, int, u8);
19978SQLITE_PRIVATEstatic void sqlite3DropIndex(Parse*, SrcList*, int);
19979SQLITE_PRIVATEstatic int sqlite3Select(Parse*, Select*, SelectDest*);
19980SQLITE_PRIVATEstatic Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
19981 Expr*,ExprList*,u32,Expr*);
19982SQLITE_PRIVATEstatic void sqlite3SelectDelete(sqlite3*, Select*);
19983SQLITE_PRIVATEstatic Table *sqlite3SrcListLookup(Parse*, SrcList*);
19984SQLITE_PRIVATEstatic int sqlite3IsReadOnly(Parse*, Table*, int);
19985SQLITE_PRIVATEstatic void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
19986#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT1) && !defined(SQLITE_OMIT_SUBQUERY)
19987SQLITE_PRIVATEstatic Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,char*);
19988#endif
19989SQLITE_PRIVATEstatic void sqlite3CodeChangeCount(Vdbe*,int,const char*);
19990SQLITE_PRIVATEstatic void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*);
19991SQLITE_PRIVATEstatic void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*,
19992 Upsert*);
19993SQLITE_PRIVATEstatic WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,
19994 ExprList*,Select*,u16,int);
19995SQLITE_PRIVATEstatic void sqlite3WhereEnd(WhereInfo*);
19996SQLITE_PRIVATEstatic LogEst sqlite3WhereOutputRowCount(WhereInfo*);
19997SQLITE_PRIVATEstatic int sqlite3WhereIsDistinct(WhereInfo*);
19998SQLITE_PRIVATEstatic int sqlite3WhereIsOrdered(WhereInfo*);
19999SQLITE_PRIVATEstatic int sqlite3WhereOrderByLimitOptLabel(WhereInfo*);
20000SQLITE_PRIVATEstatic void sqlite3WhereMinMaxOptEarlyOut(Vdbe*,WhereInfo*);
20001SQLITE_PRIVATEstatic int sqlite3WhereIsSorted(WhereInfo*);
20002SQLITE_PRIVATEstatic int sqlite3WhereContinueLabel(WhereInfo*);
20003SQLITE_PRIVATEstatic int sqlite3WhereBreakLabel(WhereInfo*);
20004SQLITE_PRIVATEstatic int sqlite3WhereOkOnePass(WhereInfo*, int*);
20005#define ONEPASS_OFF0 0 /* Use of ONEPASS not allowed */
20006#define ONEPASS_SINGLE1 1 /* ONEPASS valid for a single row update */
20007#define ONEPASS_MULTI2 2 /* ONEPASS is valid for multiple rows */
20008SQLITE_PRIVATEstatic int sqlite3WhereUsesDeferredSeek(WhereInfo*);
20009SQLITE_PRIVATEstatic void sqlite3ExprCodeLoadIndexColumn(Parse*, Index*, int, int, int);
20010SQLITE_PRIVATEstatic int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
20011SQLITE_PRIVATEstatic void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
20012SQLITE_PRIVATEstatic void sqlite3ExprCodeMove(Parse*, int, int, int);
20013SQLITE_PRIVATEstatic void sqlite3ExprCode(Parse*, Expr*, int);
20014#ifndef SQLITE_OMIT_GENERATED_COLUMNS
20015SQLITE_PRIVATEstatic void sqlite3ExprCodeGeneratedColumn(Parse*, Table*, Column*, int);
20016#endif
20017SQLITE_PRIVATEstatic void sqlite3ExprCodeCopy(Parse*, Expr*, int);
20018SQLITE_PRIVATEstatic void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
20019SQLITE_PRIVATEstatic int sqlite3ExprCodeRunJustOnce(Parse*, Expr*, int);
20020SQLITE_PRIVATEstatic int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
20021SQLITE_PRIVATEstatic int sqlite3ExprCodeTarget(Parse*, Expr*, int);
20022SQLITE_PRIVATEstatic int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
20023#define SQLITE_ECEL_DUP0x01 0x01 /* Deep, not shallow copies */
20024#define SQLITE_ECEL_FACTOR0x02 0x02 /* Factor out constant terms */
20025#define SQLITE_ECEL_REF0x04 0x04 /* Use ExprList.u.x.iOrderByCol */
20026#define SQLITE_ECEL_OMITREF0x08 0x08 /* Omit if ExprList.u.x.iOrderByCol */
20027SQLITE_PRIVATEstatic void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
20028SQLITE_PRIVATEstatic void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
20029SQLITE_PRIVATEstatic void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int);
20030SQLITE_PRIVATEstatic Table *sqlite3FindTable(sqlite3*,const char*, const char*);
20031#define LOCATE_VIEW0x01 0x01
20032#define LOCATE_NOERR0x02 0x02
20033SQLITE_PRIVATEstatic Table *sqlite3LocateTable(Parse*,u32 flags,const char*, const char*);
20034SQLITE_PRIVATEstatic const char *sqlite3PreferredTableName(const char*);
20035SQLITE_PRIVATEstatic Table *sqlite3LocateTableItem(Parse*,u32 flags,SrcItem *);
20036SQLITE_PRIVATEstatic Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
20037SQLITE_PRIVATEstatic void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
20038SQLITE_PRIVATEstatic void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
20039SQLITE_PRIVATEstatic void sqlite3Vacuum(Parse*,Token*,Expr*);
20040SQLITE_PRIVATEstatic int sqlite3RunVacuum(char**, sqlite3*, int, sqlite3_value*);
20041SQLITE_PRIVATEstatic char *sqlite3NameFromToken(sqlite3*, const Token*);
20042SQLITE_PRIVATEstatic int sqlite3ExprCompare(const Parse*,const Expr*,const Expr*, int);
20043SQLITE_PRIVATEstatic int sqlite3ExprCompareSkip(Expr*,Expr*,int);
20044SQLITE_PRIVATEstatic int sqlite3ExprListCompare(const ExprList*,const ExprList*, int);
20045SQLITE_PRIVATEstatic int sqlite3ExprImpliesExpr(const Parse*,const Expr*,const Expr*, int);
20046SQLITE_PRIVATEstatic int sqlite3ExprImpliesNonNullRow(Expr*,int);
20047SQLITE_PRIVATEstatic void sqlite3AggInfoPersistWalkerInit(Walker*,Parse*);
20048SQLITE_PRIVATEstatic void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
20049SQLITE_PRIVATEstatic void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
20050SQLITE_PRIVATEstatic int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx);
20051SQLITE_PRIVATEstatic int sqlite3ReferencesSrcList(Parse*, Expr*, SrcList*);
20052SQLITE_PRIVATEstatic Vdbe *sqlite3GetVdbe(Parse*);
20053#ifndef SQLITE_UNTESTABLE
20054SQLITE_PRIVATEstatic void sqlite3PrngSaveState(void);
20055SQLITE_PRIVATEstatic void sqlite3PrngRestoreState(void);
20056#endif
20057SQLITE_PRIVATEstatic void sqlite3RollbackAll(sqlite3*,int);
20058SQLITE_PRIVATEstatic void sqlite3CodeVerifySchema(Parse*, int);
20059SQLITE_PRIVATEstatic void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
20060SQLITE_PRIVATEstatic void sqlite3BeginTransaction(Parse*, int);
20061SQLITE_PRIVATEstatic void sqlite3EndTransaction(Parse*,int);
20062SQLITE_PRIVATEstatic void sqlite3Savepoint(Parse*, int, Token*);
20063SQLITE_PRIVATEstatic void sqlite3CloseSavepoints(sqlite3 *);
20064SQLITE_PRIVATEstatic void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
20065SQLITE_PRIVATEstatic u32 sqlite3IsTrueOrFalse(const char*);
20066SQLITE_PRIVATEstatic int sqlite3ExprIdToTrueFalse(Expr*);
20067SQLITE_PRIVATEstatic int sqlite3ExprTruthValue(const Expr*);
20068SQLITE_PRIVATEstatic int sqlite3ExprIsConstant(Expr*);
20069SQLITE_PRIVATEstatic int sqlite3ExprIsConstantNotJoin(Expr*);
20070SQLITE_PRIVATEstatic int sqlite3ExprIsConstantOrFunction(Expr*, u8);
20071SQLITE_PRIVATEstatic int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
20072SQLITE_PRIVATEstatic int sqlite3ExprIsTableConstant(Expr*,int);
20073SQLITE_PRIVATEstatic int sqlite3ExprIsTableConstraint(Expr*,const SrcItem*);
20074#ifdef SQLITE_ENABLE_CURSOR_HINTS
20075SQLITE_PRIVATEstatic int sqlite3ExprContainsSubquery(Expr*);
20076#endif
20077SQLITE_PRIVATEstatic int sqlite3ExprIsInteger(const Expr*, int*);
20078SQLITE_PRIVATEstatic int sqlite3ExprCanBeNull(const Expr*);
20079SQLITE_PRIVATEstatic int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
20080SQLITE_PRIVATEstatic int sqlite3IsRowid(const char*);
20081SQLITE_PRIVATEstatic void sqlite3GenerateRowDelete(
20082 Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
20083SQLITE_PRIVATEstatic void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
20084SQLITE_PRIVATEstatic int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
20085SQLITE_PRIVATEstatic void sqlite3ResolvePartIdxLabel(Parse*,int);
20086SQLITE_PRIVATEstatic int sqlite3ExprReferencesUpdatedColumn(Expr*,int*,int);
20087SQLITE_PRIVATEstatic void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
20088 u8,u8,int,int*,int*,Upsert*);
20089#ifdef SQLITE_ENABLE_NULL_TRIM
20090SQLITE_PRIVATEstatic void sqlite3SetMakeRecordP5(Vdbe*,Table*);
20091#else
20092# define sqlite3SetMakeRecordP5(A,B)
20093#endif
20094SQLITE_PRIVATEstatic void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
20095SQLITE_PRIVATEstatic int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
20096SQLITE_PRIVATEstatic void sqlite3BeginWriteOperation(Parse*, int, int);
20097SQLITE_PRIVATEstatic void sqlite3MultiWrite(Parse*);
20098SQLITE_PRIVATEstatic void sqlite3MayAbort(Parse*);
20099SQLITE_PRIVATEstatic void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8);
20100SQLITE_PRIVATEstatic void sqlite3UniqueConstraint(Parse*, int, Index*);
20101SQLITE_PRIVATEstatic void sqlite3RowidConstraint(Parse*, int, Table*);
20102SQLITE_PRIVATEstatic Expr *sqlite3ExprDup(sqlite3*,const Expr*,int);
20103SQLITE_PRIVATEstatic ExprList *sqlite3ExprListDup(sqlite3*,const ExprList*,int);
20104SQLITE_PRIVATEstatic SrcList *sqlite3SrcListDup(sqlite3*,const SrcList*,int);
20105SQLITE_PRIVATEstatic IdList *sqlite3IdListDup(sqlite3*,const IdList*);
20106SQLITE_PRIVATEstatic Select *sqlite3SelectDup(sqlite3*,const Select*,int);
20107SQLITE_PRIVATEstatic FuncDef *sqlite3FunctionSearch(int,const char*);
20108SQLITE_PRIVATEstatic void sqlite3InsertBuiltinFuncs(FuncDef*,int);
20109SQLITE_PRIVATEstatic FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
20110SQLITE_PRIVATEstatic void sqlite3QuoteValue(StrAccum*,sqlite3_value*);
20111SQLITE_PRIVATEstatic void sqlite3RegisterBuiltinFunctions(void);
20112SQLITE_PRIVATEstatic void sqlite3RegisterDateTimeFunctions(void);
20113SQLITE_PRIVATEstatic void sqlite3RegisterJsonFunctions(void);
20114SQLITE_PRIVATEstatic void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
20115#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
20116SQLITE_PRIVATEstatic int sqlite3JsonTableFunctions(sqlite3*);
20117#endif
20118SQLITE_PRIVATEstatic int sqlite3SafetyCheckOk(sqlite3*);
20119SQLITE_PRIVATEstatic int sqlite3SafetyCheckSickOrOk(sqlite3*);
20120SQLITE_PRIVATEstatic void sqlite3ChangeCookie(Parse*, int);
20121SQLITE_PRIVATEstatic With *sqlite3WithDup(sqlite3 *db, With *p);
20122
20123#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
20124SQLITE_PRIVATEstatic void sqlite3MaterializeView(Parse*, Table*, Expr*, ExprList*,Expr*,int);
20125#endif
20126
20127#ifndef SQLITE_OMIT_TRIGGER
20128SQLITE_PRIVATEstatic void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
20129 Expr*,int, int);
20130SQLITE_PRIVATEstatic void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
20131SQLITE_PRIVATEstatic void sqlite3DropTrigger(Parse*, SrcList*, int);
20132SQLITE_PRIVATEstatic void sqlite3DropTriggerPtr(Parse*, Trigger*);
20133SQLITE_PRIVATEstatic Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
20134SQLITE_PRIVATEstatic Trigger *sqlite3TriggerList(Parse *, Table *);
20135SQLITE_PRIVATEstatic void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
20136 int, int, int);
20137SQLITE_PRIVATEstatic void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
20138 void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
20139SQLITE_PRIVATEstatic void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
20140SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*,
20141 const char*,const char*);
20142SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerInsertStep(Parse*,Token*, IdList*,
20143 Select*,u8,Upsert*,
20144 const char*,const char*);
20145SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerUpdateStep(Parse*,Token*,SrcList*,ExprList*,
20146 Expr*, u8, const char*,const char*);
20147SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerDeleteStep(Parse*,Token*, Expr*,
20148 const char*,const char*);
20149SQLITE_PRIVATEstatic void sqlite3DeleteTrigger(sqlite3*, Trigger*);
20150SQLITE_PRIVATEstatic void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
20151SQLITE_PRIVATEstatic u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
20152SQLITE_PRIVATEstatic SrcList *sqlite3TriggerStepSrc(Parse*, TriggerStep*);
20153# define sqlite3ParseToplevel(p)((p)->pToplevel ? (p)->pToplevel : (p)) ((p)->pToplevel ? (p)->pToplevel : (p))
20154# define sqlite3IsToplevel(p)((p)->pToplevel==0) ((p)->pToplevel==0)
20155#else
20156# define sqlite3TriggersExist(B,C,D,E,F) 0
20157# define sqlite3DeleteTrigger(A,B)
20158# define sqlite3DropTriggerPtr(A,B)
20159# define sqlite3UnlinkAndDeleteTrigger(A,B,C)
20160# define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
20161# define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
20162# define sqlite3TriggerList(X, Y) 0
20163# define sqlite3ParseToplevel(p)((p)->pToplevel ? (p)->pToplevel : (p)) p
20164# define sqlite3IsToplevel(p)((p)->pToplevel==0) 1
20165# define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
20166# define sqlite3TriggerStepSrc(A,B) 0
20167#endif
20168
20169SQLITE_PRIVATEstatic int sqlite3JoinType(Parse*, Token*, Token*, Token*);
20170SQLITE_PRIVATEstatic int sqlite3ColumnIndex(Table *pTab, const char *zCol);
20171SQLITE_PRIVATEstatic void sqlite3SrcItemColumnUsed(SrcItem*,int);
20172SQLITE_PRIVATEstatic void sqlite3SetJoinExpr(Expr*,int,u32);
20173SQLITE_PRIVATEstatic void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
20174SQLITE_PRIVATEstatic void sqlite3DeferForeignKey(Parse*, int);
20175#ifndef SQLITE_OMIT_AUTHORIZATION
20176SQLITE_PRIVATEstatic void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
20177SQLITE_PRIVATEstatic int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
20178SQLITE_PRIVATEstatic void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
20179SQLITE_PRIVATEstatic void sqlite3AuthContextPop(AuthContext*);
20180SQLITE_PRIVATEstatic int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
20181#else
20182# define sqlite3AuthRead(a,b,c,d)
20183# define sqlite3AuthCheck(a,b,c,d,e) SQLITE_OK0
20184# define sqlite3AuthContextPush(a,b,c)
20185# define sqlite3AuthContextPop(a) ((void)(a))
20186#endif
20187SQLITE_PRIVATEstatic int sqlite3DbIsNamed(sqlite3 *db, int iDb, const char *zName);
20188SQLITE_PRIVATEstatic void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
20189SQLITE_PRIVATEstatic void sqlite3Detach(Parse*, Expr*);
20190SQLITE_PRIVATEstatic void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
20191SQLITE_PRIVATEstatic int sqlite3FixSrcList(DbFixer*, SrcList*);
20192SQLITE_PRIVATEstatic int sqlite3FixSelect(DbFixer*, Select*);
20193SQLITE_PRIVATEstatic int sqlite3FixExpr(DbFixer*, Expr*);
20194SQLITE_PRIVATEstatic int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
20195SQLITE_PRIVATEstatic int sqlite3RealSameAsInt(double,sqlite3_int64);
20196SQLITE_PRIVATEstatic void sqlite3Int64ToText(i64,char*);
20197SQLITE_PRIVATEstatic int sqlite3AtoF(const char *z, double*, int, u8);
20198SQLITE_PRIVATEstatic int sqlite3GetInt32(const char *, int*);
20199SQLITE_PRIVATEstatic int sqlite3GetUInt32(const char*, u32*);
20200SQLITE_PRIVATEstatic int sqlite3Atoi(const char*);
20201#ifndef SQLITE_OMIT_UTF16
20202SQLITE_PRIVATEstatic int sqlite3Utf16ByteLen(const void *pData, int nChar);
20203#endif
20204SQLITE_PRIVATEstatic int sqlite3Utf8CharLen(const char *pData, int nByte);
20205SQLITE_PRIVATEstatic u32 sqlite3Utf8Read(const u8**);
20206SQLITE_PRIVATEstatic LogEst sqlite3LogEst(u64);
20207SQLITE_PRIVATEstatic LogEst sqlite3LogEstAdd(LogEst,LogEst);
20208SQLITE_PRIVATEstatic LogEst sqlite3LogEstFromDouble(double);
20209SQLITE_PRIVATEstatic u64 sqlite3LogEstToInt(LogEst);
20210SQLITE_PRIVATEstatic VList *sqlite3VListAdd(sqlite3*,VList*,const char*,int,int);
20211SQLITE_PRIVATEstatic const char *sqlite3VListNumToName(VList*,int);
20212SQLITE_PRIVATEstatic int sqlite3VListNameToNum(VList*,const char*,int);
20213
20214/*
20215** Routines to read and write variable-length integers. These used to
20216** be defined locally, but now we use the varint routines in the util.c
20217** file.
20218*/
20219SQLITE_PRIVATEstatic int sqlite3PutVarint(unsigned char*, u64);
20220SQLITE_PRIVATEstatic u8 sqlite3GetVarint(const unsigned char *, u64 *);
20221SQLITE_PRIVATEstatic u8 sqlite3GetVarint32(const unsigned char *, u32 *);
20222SQLITE_PRIVATEstatic int sqlite3VarintLen(u64 v);
20223
20224/*
20225** The common case is for a varint to be a single byte. They following
20226** macros handle the common case without a procedure call, but then call
20227** the procedure for larger varints.
20228*/
20229#define getVarint32(A,B)(u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32(
(A),(u32 *)&(B)))
\
20230 (u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32((A),(u32 *)&(B)))
20231#define getVarint32NR(A,B)B=(u32)*(A);if(B>=0x80)sqlite3GetVarint32((A),(u32*)&(
B))
\
20232 B=(u32)*(A);if(B>=0x80)sqlite3GetVarint32((A),(u32*)&(B))
20233#define putVarint32(A,B)(u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1: sqlite3PutVarint
((A),(B)))
\
20234 (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\
20235 sqlite3PutVarint((A),(B)))
20236#define getVarintsqlite3GetVarint sqlite3GetVarint
20237#define putVarintsqlite3PutVarint sqlite3PutVarint
20238
20239
20240SQLITE_PRIVATEstatic const char *sqlite3IndexAffinityStr(sqlite3*, Index*);
20241SQLITE_PRIVATEstatic void sqlite3TableAffinity(Vdbe*, Table*, int);
20242SQLITE_PRIVATEstatic char sqlite3CompareAffinity(const Expr *pExpr, char aff2);
20243SQLITE_PRIVATEstatic int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity);
20244SQLITE_PRIVATEstatic char sqlite3TableColumnAffinity(const Table*,int);
20245SQLITE_PRIVATEstatic char sqlite3ExprAffinity(const Expr *pExpr);
20246SQLITE_PRIVATEstatic int sqlite3Atoi64(const char*, i64*, int, u8);
20247SQLITE_PRIVATEstatic int sqlite3DecOrHexToI64(const char*, i64*);
20248SQLITE_PRIVATEstatic void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
20249SQLITE_PRIVATEstatic void sqlite3Error(sqlite3*,int);
20250SQLITE_PRIVATEstatic void sqlite3ErrorClear(sqlite3*);
20251SQLITE_PRIVATEstatic void sqlite3SystemError(sqlite3*,int);
20252SQLITE_PRIVATEstatic void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
20253SQLITE_PRIVATEstatic u8 sqlite3HexToInt(int h);
20254SQLITE_PRIVATEstatic int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
20255
20256#if defined(SQLITE_NEED_ERR_NAME)
20257SQLITE_PRIVATEstatic const char *sqlite3ErrName(int);
20258#endif
20259
20260#ifndef SQLITE_OMIT_DESERIALIZE
20261SQLITE_PRIVATEstatic int sqlite3MemdbInit(void);
20262#endif
20263
20264SQLITE_PRIVATEstatic const char *sqlite3ErrStr(int);
20265SQLITE_PRIVATEstatic int sqlite3ReadSchema(Parse *pParse);
20266SQLITE_PRIVATEstatic CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
20267SQLITE_PRIVATEstatic int sqlite3IsBinary(const CollSeq*);
20268SQLITE_PRIVATEstatic CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
20269SQLITE_PRIVATEstatic void sqlite3SetTextEncoding(sqlite3 *db, u8);
20270SQLITE_PRIVATEstatic CollSeq *sqlite3ExprCollSeq(Parse *pParse, const Expr *pExpr);
20271SQLITE_PRIVATEstatic CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, const Expr *pExpr);
20272SQLITE_PRIVATEstatic int sqlite3ExprCollSeqMatch(Parse*,const Expr*,const Expr*);
20273SQLITE_PRIVATEstatic Expr *sqlite3ExprAddCollateToken(const Parse *pParse, Expr*, const Token*, int);
20274SQLITE_PRIVATEstatic Expr *sqlite3ExprAddCollateString(const Parse*,Expr*,const char*);
20275SQLITE_PRIVATEstatic Expr *sqlite3ExprSkipCollate(Expr*);
20276SQLITE_PRIVATEstatic Expr *sqlite3ExprSkipCollateAndLikely(Expr*);
20277SQLITE_PRIVATEstatic int sqlite3CheckCollSeq(Parse *, CollSeq *);
20278SQLITE_PRIVATEstatic int sqlite3WritableSchema(sqlite3*);
20279SQLITE_PRIVATEstatic int sqlite3CheckObjectName(Parse*, const char*,const char*,const char*);
20280SQLITE_PRIVATEstatic void sqlite3VdbeSetChanges(sqlite3 *, i64);
20281SQLITE_PRIVATEstatic int sqlite3AddInt64(i64*,i64);
20282SQLITE_PRIVATEstatic int sqlite3SubInt64(i64*,i64);
20283SQLITE_PRIVATEstatic int sqlite3MulInt64(i64*,i64);
20284SQLITE_PRIVATEstatic int sqlite3AbsInt32(int);
20285#ifdef SQLITE_ENABLE_8_3_NAMES
20286SQLITE_PRIVATEstatic void sqlite3FileSuffix3(const char*, char*);
20287#else
20288# define sqlite3FileSuffix3(X,Y)
20289#endif
20290SQLITE_PRIVATEstatic u8 sqlite3GetBoolean(const char *z,u8);
20291
20292SQLITE_PRIVATEstatic const void *sqlite3ValueText(sqlite3_value*, u8);
20293SQLITE_PRIVATEstatic int sqlite3ValueBytes(sqlite3_value*, u8);
20294SQLITE_PRIVATEstatic void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
20295 void(*)(void*));
20296SQLITE_PRIVATEstatic void sqlite3ValueSetNull(sqlite3_value*);
20297SQLITE_PRIVATEstatic void sqlite3ValueFree(sqlite3_value*);
20298#ifndef SQLITE_UNTESTABLE
20299SQLITE_PRIVATEstatic void sqlite3ResultIntReal(sqlite3_context*);
20300#endif
20301SQLITE_PRIVATEstatic sqlite3_value *sqlite3ValueNew(sqlite3 *);
20302#ifndef SQLITE_OMIT_UTF16
20303SQLITE_PRIVATEstatic char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
20304#endif
20305SQLITE_PRIVATEstatic int sqlite3ValueFromExpr(sqlite3 *, const Expr *, u8, u8, sqlite3_value **);
20306SQLITE_PRIVATEstatic void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
20307#ifndef SQLITE_AMALGAMATION1
20308SQLITE_PRIVATEstatic const unsigned char sqlite3OpcodeProperty[];
20309SQLITE_PRIVATEstatic const char sqlite3StrBINARY[];
20310SQLITE_PRIVATEstatic const unsigned char sqlite3StdTypeLen[];
20311SQLITE_PRIVATEstatic const char sqlite3StdTypeAffinity[];
20312SQLITE_PRIVATEstatic const char sqlite3StdTypeMap[];
20313SQLITE_PRIVATEstatic const char *sqlite3StdType[];
20314SQLITE_PRIVATEstatic const unsigned char sqlite3UpperToLower[];
20315SQLITE_PRIVATEstatic const unsigned char *sqlite3aLTb;
20316SQLITE_PRIVATEstatic const unsigned char *sqlite3aEQb;
20317SQLITE_PRIVATEstatic const unsigned char *sqlite3aGTb;
20318SQLITE_PRIVATEstatic const unsigned char sqlite3CtypeMap[];
20319SQLITE_PRIVATEstatic SQLITE_WSD struct Sqlite3Config sqlite3Config;
20320SQLITE_PRIVATEstatic FuncDefHash sqlite3BuiltinFunctions;
20321#ifndef SQLITE_OMIT_WSD
20322SQLITE_PRIVATEstatic int sqlite3PendingByte;
20323#endif
20324#endif /* SQLITE_AMALGAMATION */
20325#ifdef VDBE_PROFILE
20326SQLITE_PRIVATEstatic sqlite3_uint64 sqlite3NProfileCnt;
20327#endif
20328SQLITE_PRIVATEstatic void sqlite3RootPageMoved(sqlite3*, int, Pgno, Pgno);
20329SQLITE_PRIVATEstatic void sqlite3Reindex(Parse*, Token*, Token*);
20330SQLITE_PRIVATEstatic void sqlite3AlterFunctions(void);
20331SQLITE_PRIVATEstatic void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
20332SQLITE_PRIVATEstatic void sqlite3AlterRenameColumn(Parse*, SrcList*, Token*, Token*);
20333SQLITE_PRIVATEstatic int sqlite3GetToken(const unsigned char *, int *);
20334SQLITE_PRIVATEstatic void sqlite3NestedParse(Parse*, const char*, ...);
20335SQLITE_PRIVATEstatic void sqlite3ExpirePreparedStatements(sqlite3*, int);
20336SQLITE_PRIVATEstatic void sqlite3CodeRhsOfIN(Parse*, Expr*, int);
20337SQLITE_PRIVATEstatic int sqlite3CodeSubselect(Parse*, Expr*);
20338SQLITE_PRIVATEstatic void sqlite3SelectPrep(Parse*, Select*, NameContext*);
20339SQLITE_PRIVATEstatic int sqlite3ExpandSubquery(Parse*, SrcItem*);
20340SQLITE_PRIVATEstatic void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
20341SQLITE_PRIVATEstatic int sqlite3MatchEName(
20342 const struct ExprList_item*,
20343 const char*,
20344 const char*,
20345 const char*
20346);
20347SQLITE_PRIVATEstatic Bitmask sqlite3ExprColUsed(Expr*);
20348SQLITE_PRIVATEstatic u8 sqlite3StrIHash(const char*);
20349SQLITE_PRIVATEstatic int sqlite3ResolveExprNames(NameContext*, Expr*);
20350SQLITE_PRIVATEstatic int sqlite3ResolveExprListNames(NameContext*, ExprList*);
20351SQLITE_PRIVATEstatic void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
20352SQLITE_PRIVATEstatic int sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
20353SQLITE_PRIVATEstatic int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
20354SQLITE_PRIVATEstatic void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
20355SQLITE_PRIVATEstatic void sqlite3AlterFinishAddColumn(Parse *, Token *);
20356SQLITE_PRIVATEstatic void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
20357SQLITE_PRIVATEstatic void sqlite3AlterDropColumn(Parse*, SrcList*, const Token*);
20358SQLITE_PRIVATEstatic const void *sqlite3RenameTokenMap(Parse*, const void*, const Token*);
20359SQLITE_PRIVATEstatic void sqlite3RenameTokenRemap(Parse*, const void *pTo, const void *pFrom);
20360SQLITE_PRIVATEstatic void sqlite3RenameExprUnmap(Parse*, Expr*);
20361SQLITE_PRIVATEstatic void sqlite3RenameExprlistUnmap(Parse*, ExprList*);
20362SQLITE_PRIVATEstatic CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
20363SQLITE_PRIVATEstatic char sqlite3AffinityType(const char*, Column*);
20364SQLITE_PRIVATEstatic void sqlite3Analyze(Parse*, Token*, Token*);
20365SQLITE_PRIVATEstatic int sqlite3InvokeBusyHandler(BusyHandler*);
20366SQLITE_PRIVATEstatic int sqlite3FindDb(sqlite3*, Token*);
20367SQLITE_PRIVATEstatic int sqlite3FindDbName(sqlite3 *, const char *);
20368SQLITE_PRIVATEstatic int sqlite3AnalysisLoad(sqlite3*,int iDB);
20369SQLITE_PRIVATEstatic void sqlite3DeleteIndexSamples(sqlite3*,Index*);
20370SQLITE_PRIVATEstatic void sqlite3DefaultRowEst(Index*);
20371SQLITE_PRIVATEstatic void sqlite3RegisterLikeFunctions(sqlite3*, int);
20372SQLITE_PRIVATEstatic int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
20373SQLITE_PRIVATEstatic void sqlite3SchemaClear(void *);
20374SQLITE_PRIVATEstatic Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
20375SQLITE_PRIVATEstatic int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
20376SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int);
20377SQLITE_PRIVATEstatic void sqlite3KeyInfoUnref(KeyInfo*);
20378SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoRef(KeyInfo*);
20379SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoOfIndex(Parse*, Index*);
20380SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoFromExprList(Parse*, ExprList*, int, int);
20381SQLITE_PRIVATEstatic const char *sqlite3SelectOpName(int);
20382SQLITE_PRIVATEstatic int sqlite3HasExplicitNulls(Parse*, ExprList*);
20383
20384#ifdef SQLITE_DEBUG
20385SQLITE_PRIVATEstatic int sqlite3KeyInfoIsWriteable(KeyInfo*);
20386#endif
20387SQLITE_PRIVATEstatic int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
20388 void (*)(sqlite3_context*,int,sqlite3_value **),
20389 void (*)(sqlite3_context*,int,sqlite3_value **),
20390 void (*)(sqlite3_context*),
20391 void (*)(sqlite3_context*),
20392 void (*)(sqlite3_context*,int,sqlite3_value **),
20393 FuncDestructor *pDestructor
20394);
20395SQLITE_PRIVATEstatic void sqlite3NoopDestructor(void*);
20396SQLITE_PRIVATEstatic void *sqlite3OomFault(sqlite3*);
20397SQLITE_PRIVATEstatic void sqlite3OomClear(sqlite3*);
20398SQLITE_PRIVATEstatic int sqlite3ApiExit(sqlite3 *db, int);
20399SQLITE_PRIVATEstatic int sqlite3OpenTempDatabase(Parse *);
20400
20401SQLITE_PRIVATEstatic void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
20402SQLITE_PRIVATEstatic int sqlite3StrAccumEnlarge(StrAccum*, int);
20403SQLITE_PRIVATEstatic char *sqlite3StrAccumFinish(StrAccum*);
20404SQLITE_PRIVATEstatic void sqlite3StrAccumSetError(StrAccum*, u8);
20405SQLITE_PRIVATEstatic void sqlite3ResultStrAccum(sqlite3_context*,StrAccum*);
20406SQLITE_PRIVATEstatic void sqlite3SelectDestInit(SelectDest*,int,int);
20407SQLITE_PRIVATEstatic Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
20408SQLITE_PRIVATEstatic void sqlite3RecordErrorByteOffset(sqlite3*,const char*);
20409SQLITE_PRIVATEstatic void sqlite3RecordErrorOffsetOfExpr(sqlite3*,const Expr*);
20410
20411SQLITE_PRIVATEstatic void sqlite3BackupRestart(sqlite3_backup *);
20412SQLITE_PRIVATEstatic void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
20413
20414#ifndef SQLITE_OMIT_SUBQUERY
20415SQLITE_PRIVATEstatic int sqlite3ExprCheckIN(Parse*, Expr*);
20416#else
20417# define sqlite3ExprCheckIN(x,y) SQLITE_OK0
20418#endif
20419
20420#ifdef SQLITE_ENABLE_STAT4
20421SQLITE_PRIVATEstatic int sqlite3Stat4ProbeSetValue(
20422 Parse*,Index*,UnpackedRecord**,Expr*,int,int,int*);
20423SQLITE_PRIVATEstatic int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**);
20424SQLITE_PRIVATEstatic void sqlite3Stat4ProbeFree(UnpackedRecord*);
20425SQLITE_PRIVATEstatic int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
20426SQLITE_PRIVATEstatic char sqlite3IndexColumnAffinity(sqlite3*, Index*, int);
20427#endif
20428
20429/*
20430** The interface to the LEMON-generated parser
20431*/
20432#ifndef SQLITE_AMALGAMATION1
20433SQLITE_PRIVATEstatic void *sqlite3ParserAlloc(void*(*)(u64), Parse*);
20434SQLITE_PRIVATEstatic void sqlite3ParserFree(void*, void(*)(void*));
20435#endif
20436SQLITE_PRIVATEstatic void sqlite3Parser(void*, int, Token);
20437SQLITE_PRIVATEstatic int sqlite3ParserFallback(int);
20438#ifdef YYTRACKMAXSTACKDEPTH
20439SQLITE_PRIVATEstatic int sqlite3ParserStackPeak(void*);
20440#endif
20441
20442SQLITE_PRIVATEstatic void sqlite3AutoLoadExtensions(sqlite3*);
20443#ifndef SQLITE_OMIT_LOAD_EXTENSION1
20444SQLITE_PRIVATEstatic void sqlite3CloseExtensions(sqlite3*);
20445#else
20446# define sqlite3CloseExtensions(X)
20447#endif
20448
20449#ifndef SQLITE_OMIT_SHARED_CACHE
20450SQLITE_PRIVATEstatic void sqlite3TableLock(Parse *, int, Pgno, u8, const char *);
20451#else
20452 #define sqlite3TableLock(v,w,x,y,z)
20453#endif
20454
20455#ifdef SQLITE_TEST
20456SQLITE_PRIVATEstatic int sqlite3Utf8To8(unsigned char*);
20457#endif
20458
20459#ifdef SQLITE_OMIT_VIRTUALTABLE
20460# define sqlite3VtabClear(D,T)
20461# define sqlite3VtabSync(X,Y) SQLITE_OK0
20462# define sqlite3VtabRollback(X)
20463# define sqlite3VtabCommit(X)
20464# define sqlite3VtabInSync(db)((db)->nVTrans>0 && (db)->aVTrans==0) 0
20465# define sqlite3VtabLock(X)
20466# define sqlite3VtabUnlock(X)
20467# define sqlite3VtabModuleUnref(D,X)
20468# define sqlite3VtabUnlockList(X)
20469# define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK0
20470# define sqlite3GetVTable(X,Y) ((VTable*)0)
20471#else
20472SQLITE_PRIVATEstatic void sqlite3VtabClear(sqlite3 *db, Table*);
20473SQLITE_PRIVATEstatic void sqlite3VtabDisconnect(sqlite3 *db, Table *p);
20474SQLITE_PRIVATEstatic int sqlite3VtabSync(sqlite3 *db, Vdbe*);
20475SQLITE_PRIVATEstatic int sqlite3VtabRollback(sqlite3 *db);
20476SQLITE_PRIVATEstatic int sqlite3VtabCommit(sqlite3 *db);
20477SQLITE_PRIVATEstatic void sqlite3VtabLock(VTable *);
20478SQLITE_PRIVATEstatic void sqlite3VtabUnlock(VTable *);
20479SQLITE_PRIVATEstatic void sqlite3VtabModuleUnref(sqlite3*,Module*);
20480SQLITE_PRIVATEstatic void sqlite3VtabUnlockList(sqlite3*);
20481SQLITE_PRIVATEstatic int sqlite3VtabSavepoint(sqlite3 *, int, int);
20482SQLITE_PRIVATEstatic void sqlite3VtabImportErrmsg(Vdbe*, sqlite3_vtab*);
20483SQLITE_PRIVATEstatic VTable *sqlite3GetVTable(sqlite3*, Table*);
20484SQLITE_PRIVATEstatic Module *sqlite3VtabCreateModule(
20485 sqlite3*,
20486 const char*,
20487 const sqlite3_module*,
20488 void*,
20489 void(*)(void*)
20490 );
20491# define sqlite3VtabInSync(db)((db)->nVTrans>0 && (db)->aVTrans==0) ((db)->nVTrans>0 && (db)->aVTrans==0)
20492#endif
20493SQLITE_PRIVATEstatic int sqlite3ReadOnlyShadowTables(sqlite3 *db);
20494#ifndef SQLITE_OMIT_VIRTUALTABLE
20495SQLITE_PRIVATEstatic int sqlite3ShadowTableName(sqlite3 *db, const char *zName);
20496SQLITE_PRIVATEstatic int sqlite3IsShadowTableOf(sqlite3*,Table*,const char*);
20497SQLITE_PRIVATEstatic void sqlite3MarkAllShadowTablesOf(sqlite3*, Table*);
20498#else
20499# define sqlite3ShadowTableName(A,B) 0
20500# define sqlite3IsShadowTableOf(A,B,C) 0
20501# define sqlite3MarkAllShadowTablesOf(A,B)
20502#endif
20503SQLITE_PRIVATEstatic int sqlite3VtabEponymousTableInit(Parse*,Module*);
20504SQLITE_PRIVATEstatic void sqlite3VtabEponymousTableClear(sqlite3*,Module*);
20505SQLITE_PRIVATEstatic void sqlite3VtabMakeWritable(Parse*,Table*);
20506SQLITE_PRIVATEstatic void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int);
20507SQLITE_PRIVATEstatic void sqlite3VtabFinishParse(Parse*, Token*);
20508SQLITE_PRIVATEstatic void sqlite3VtabArgInit(Parse*);
20509SQLITE_PRIVATEstatic void sqlite3VtabArgExtend(Parse*, Token*);
20510SQLITE_PRIVATEstatic int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
20511SQLITE_PRIVATEstatic int sqlite3VtabCallConnect(Parse*, Table*);
20512SQLITE_PRIVATEstatic int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
20513SQLITE_PRIVATEstatic int sqlite3VtabBegin(sqlite3 *, VTable *);
20514
20515SQLITE_PRIVATEstatic FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
20516#if (defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)) \
20517 && !defined(SQLITE_OMIT_VIRTUALTABLE)
20518SQLITE_PRIVATEstatic void sqlite3VtabUsesAllSchemas(sqlite3_index_info*);
20519#endif
20520SQLITE_PRIVATEstatic sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
20521SQLITE_PRIVATEstatic int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
20522SQLITE_PRIVATEstatic int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
20523SQLITE_PRIVATEstatic void sqlite3ParseObjectInit(Parse*,sqlite3*);
20524SQLITE_PRIVATEstatic void sqlite3ParseObjectReset(Parse*);
20525SQLITE_PRIVATEstatic void *sqlite3ParserAddCleanup(Parse*,void(*)(sqlite3*,void*),void*);
20526#ifdef SQLITE_ENABLE_NORMALIZE
20527SQLITE_PRIVATEstatic char *sqlite3Normalize(Vdbe*, const char*);
20528#endif
20529SQLITE_PRIVATEstatic int sqlite3Reprepare(Vdbe*);
20530SQLITE_PRIVATEstatic void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
20531SQLITE_PRIVATEstatic CollSeq *sqlite3ExprCompareCollSeq(Parse*,const Expr*);
20532SQLITE_PRIVATEstatic CollSeq *sqlite3BinaryCompareCollSeq(Parse *, const Expr*, const Expr*);
20533SQLITE_PRIVATEstatic int sqlite3TempInMemory(const sqlite3*);
20534SQLITE_PRIVATEstatic const char *sqlite3JournalModename(int);
20535#ifndef SQLITE_OMIT_WAL
20536SQLITE_PRIVATEstatic int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
20537SQLITE_PRIVATEstatic int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
20538#endif
20539#ifndef SQLITE_OMIT_CTE
20540SQLITE_PRIVATEstatic Cte *sqlite3CteNew(Parse*,Token*,ExprList*,Select*,u8);
20541SQLITE_PRIVATEstatic void sqlite3CteDelete(sqlite3*,Cte*);
20542SQLITE_PRIVATEstatic With *sqlite3WithAdd(Parse*,With*,Cte*);
20543SQLITE_PRIVATEstatic void sqlite3WithDelete(sqlite3*,With*);
20544SQLITE_PRIVATEstatic With *sqlite3WithPush(Parse*, With*, u8);
20545#else
20546# define sqlite3CteNew(P,T,E,S) ((void*)0)
20547# define sqlite3CteDelete(D,C)
20548# define sqlite3CteWithAdd(P,W,C) ((void*)0)
20549# define sqlite3WithDelete(x,y)
20550# define sqlite3WithPush(x,y,z) ((void*)0)
20551#endif
20552#ifndef SQLITE_OMIT_UPSERT
20553SQLITE_PRIVATEstatic Upsert *sqlite3UpsertNew(sqlite3*,ExprList*,Expr*,ExprList*,Expr*,Upsert*);
20554SQLITE_PRIVATEstatic void sqlite3UpsertDelete(sqlite3*,Upsert*);
20555SQLITE_PRIVATEstatic Upsert *sqlite3UpsertDup(sqlite3*,Upsert*);
20556SQLITE_PRIVATEstatic int sqlite3UpsertAnalyzeTarget(Parse*,SrcList*,Upsert*);
20557SQLITE_PRIVATEstatic void sqlite3UpsertDoUpdate(Parse*,Upsert*,Table*,Index*,int);
20558SQLITE_PRIVATEstatic Upsert *sqlite3UpsertOfIndex(Upsert*,Index*);
20559SQLITE_PRIVATEstatic int sqlite3UpsertNextIsIPK(Upsert*);
20560#else
20561#define sqlite3UpsertNew(u,v,w,x,y,z) ((Upsert*)0)
20562#define sqlite3UpsertDelete(x,y)
20563#define sqlite3UpsertDup(x,y) ((Upsert*)0)
20564#define sqlite3UpsertOfIndex(x,y) ((Upsert*)0)
20565#define sqlite3UpsertNextIsIPK(x) 0
20566#endif
20567
20568
20569/* Declarations for functions in fkey.c. All of these are replaced by
20570** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
20571** key functionality is available. If OMIT_TRIGGER is defined but
20572** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
20573** this case foreign keys are parsed, but no other functionality is
20574** provided (enforcement of FK constraints requires the triggers sub-system).
20575*/
20576#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
20577SQLITE_PRIVATEstatic void sqlite3FkCheck(Parse*, Table*, int, int, int*, int);
20578SQLITE_PRIVATEstatic void sqlite3FkDropTable(Parse*, SrcList *, Table*);
20579SQLITE_PRIVATEstatic void sqlite3FkActions(Parse*, Table*, ExprList*, int, int*, int);
20580SQLITE_PRIVATEstatic int sqlite3FkRequired(Parse*, Table*, int*, int);
20581SQLITE_PRIVATEstatic u32 sqlite3FkOldmask(Parse*, Table*);
20582SQLITE_PRIVATEstatic FKey *sqlite3FkReferences(Table *);
20583SQLITE_PRIVATEstatic void sqlite3FkClearTriggerCache(sqlite3*,int);
20584#else
20585 #define sqlite3FkActions(a,b,c,d,e,f)
20586 #define sqlite3FkCheck(a,b,c,d,e,f)
20587 #define sqlite3FkDropTable(a,b,c)
20588 #define sqlite3FkOldmask(a,b) 0
20589 #define sqlite3FkRequired(a,b,c,d) 0
20590 #define sqlite3FkReferences(a) 0
20591 #define sqlite3FkClearTriggerCache(a,b)
20592#endif
20593#ifndef SQLITE_OMIT_FOREIGN_KEY
20594SQLITE_PRIVATEstatic void sqlite3FkDelete(sqlite3 *, Table*);
20595SQLITE_PRIVATEstatic int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
20596#else
20597 #define sqlite3FkDelete(a,b)
20598 #define sqlite3FkLocateIndex(a,b,c,d,e)
20599#endif
20600
20601
20602/*
20603** Available fault injectors. Should be numbered beginning with 0.
20604*/
20605#define SQLITE_FAULTINJECTOR_MALLOC0 0
20606#define SQLITE_FAULTINJECTOR_COUNT1 1
20607
20608/*
20609** The interface to the code in fault.c used for identifying "benign"
20610** malloc failures. This is only present if SQLITE_UNTESTABLE
20611** is not defined.
20612*/
20613#ifndef SQLITE_UNTESTABLE
20614SQLITE_PRIVATEstatic void sqlite3BeginBenignMalloc(void);
20615SQLITE_PRIVATEstatic void sqlite3EndBenignMalloc(void);
20616#else
20617 #define sqlite3BeginBenignMalloc()
20618 #define sqlite3EndBenignMalloc()
20619#endif
20620
20621/*
20622** Allowed return values from sqlite3FindInIndex()
20623*/
20624#define IN_INDEX_ROWID1 1 /* Search the rowid of the table */
20625#define IN_INDEX_EPH2 2 /* Search an ephemeral b-tree */
20626#define IN_INDEX_INDEX_ASC3 3 /* Existing index ASCENDING */
20627#define IN_INDEX_INDEX_DESC4 4 /* Existing index DESCENDING */
20628#define IN_INDEX_NOOP5 5 /* No table available. Use comparisons */
20629/*
20630** Allowed flags for the 3rd parameter to sqlite3FindInIndex().
20631*/
20632#define IN_INDEX_NOOP_OK0x0001 0x0001 /* OK to return IN_INDEX_NOOP */
20633#define IN_INDEX_MEMBERSHIP0x0002 0x0002 /* IN operator used for membership test */
20634#define IN_INDEX_LOOP0x0004 0x0004 /* IN operator used as a loop */
20635SQLITE_PRIVATEstatic int sqlite3FindInIndex(Parse *, Expr *, u32, int*, int*, int*);
20636
20637SQLITE_PRIVATEstatic int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
20638SQLITE_PRIVATEstatic int sqlite3JournalSize(sqlite3_vfs *);
20639#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
20640 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
20641SQLITE_PRIVATEstatic int sqlite3JournalCreate(sqlite3_file *);
20642#endif
20643
20644SQLITE_PRIVATEstatic int sqlite3JournalIsInMemory(sqlite3_file *p);
20645SQLITE_PRIVATEstatic void sqlite3MemJournalOpen(sqlite3_file *);
20646
20647SQLITE_PRIVATEstatic void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p);
20648#if SQLITE_MAX_EXPR_DEPTH1000>0
20649SQLITE_PRIVATEstatic int sqlite3SelectExprHeight(const Select *);
20650SQLITE_PRIVATEstatic int sqlite3ExprCheckHeight(Parse*, int);
20651#else
20652 #define sqlite3SelectExprHeight(x) 0
20653 #define sqlite3ExprCheckHeight(x,y)
20654#endif
20655
20656SQLITE_PRIVATEstatic u32 sqlite3Get4byte(const u8*);
20657SQLITE_PRIVATEstatic void sqlite3Put4byte(u8*, u32);
20658
20659#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
20660SQLITE_PRIVATEstatic void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
20661SQLITE_PRIVATEstatic void sqlite3ConnectionUnlocked(sqlite3 *db);
20662SQLITE_PRIVATEstatic void sqlite3ConnectionClosed(sqlite3 *db);
20663#else
20664 #define sqlite3ConnectionBlocked(x,y)
20665 #define sqlite3ConnectionUnlocked(x)
20666 #define sqlite3ConnectionClosed(x)
20667#endif
20668
20669#ifdef SQLITE_DEBUG
20670SQLITE_PRIVATEstatic void sqlite3ParserTrace(FILE*, char *);
20671#endif
20672#if defined(YYCOVERAGE)
20673SQLITE_PRIVATEstatic int sqlite3ParserCoverage(FILE*);
20674#endif
20675
20676/*
20677** If the SQLITE_ENABLE IOTRACE exists then the global variable
20678** sqlite3IoTrace is a pointer to a printf-like routine used to
20679** print I/O tracing messages.
20680*/
20681#ifdef SQLITE_ENABLE_IOTRACE
20682# define IOTRACE(A) if( sqlite3IoTrace ){ sqlite3IoTrace A; }
20683SQLITE_PRIVATEstatic void sqlite3VdbeIOTraceSql(Vdbe*);
20684SQLITE_API SQLITE_EXTERNextern void (SQLITE_CDECL *sqlite3IoTrace)(const char*,...);
20685#else
20686# define IOTRACE(A)
20687# define sqlite3VdbeIOTraceSql(X)
20688#endif
20689
20690/*
20691** These routines are available for the mem2.c debugging memory allocator
20692** only. They are used to verify that different "types" of memory
20693** allocations are properly tracked by the system.
20694**
20695** sqlite3MemdebugSetType() sets the "type" of an allocation to one of
20696** the MEMTYPE_* macros defined below. The type must be a bitmask with
20697** a single bit set.
20698**
20699** sqlite3MemdebugHasType() returns true if any of the bits in its second
20700** argument match the type set by the previous sqlite3MemdebugSetType().
20701** sqlite3MemdebugHasType() is intended for use inside assert() statements.
20702**
20703** sqlite3MemdebugNoType() returns true if none of the bits in its second
20704** argument match the type set by the previous sqlite3MemdebugSetType().
20705**
20706** Perhaps the most important point is the difference between MEMTYPE_HEAP
20707** and MEMTYPE_LOOKASIDE. If an allocation is MEMTYPE_LOOKASIDE, that means
20708** it might have been allocated by lookaside, except the allocation was
20709** too large or lookaside was already full. It is important to verify
20710** that allocations that might have been satisfied by lookaside are not
20711** passed back to non-lookaside free() routines. Asserts such as the
20712** example above are placed on the non-lookaside free() routines to verify
20713** this constraint.
20714**
20715** All of this is no-op for a production build. It only comes into
20716** play when the SQLITE_MEMDEBUG compile-time option is used.
20717*/
20718#ifdef SQLITE_MEMDEBUG
20719SQLITE_PRIVATEstatic void sqlite3MemdebugSetType(void*,u8);
20720SQLITE_PRIVATEstatic int sqlite3MemdebugHasType(const void*,u8)1;
20721SQLITE_PRIVATEstatic int sqlite3MemdebugNoType(const void*,u8)1;
20722#else
20723# define sqlite3MemdebugSetType(X,Y) /* no-op */
20724# define sqlite3MemdebugHasType(X,Y)1 1
20725# define sqlite3MemdebugNoType(X,Y)1 1
20726#endif
20727#define MEMTYPE_HEAP0x01 0x01 /* General heap allocations */
20728#define MEMTYPE_LOOKASIDE0x02 0x02 /* Heap that might have been lookaside */
20729#define MEMTYPE_PCACHE0x04 0x04 /* Page cache allocations */
20730
20731/*
20732** Threading interface
20733*/
20734#if SQLITE_MAX_WORKER_THREADS8>0
20735SQLITE_PRIVATEstatic int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*);
20736SQLITE_PRIVATEstatic int sqlite3ThreadJoin(SQLiteThread*, void**);
20737#endif
20738
20739#if defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)
20740SQLITE_PRIVATEstatic int sqlite3DbpageRegister(sqlite3*);
20741#endif
20742#if defined(SQLITE_ENABLE_DBSTAT_VTAB1) || defined(SQLITE_TEST)
20743SQLITE_PRIVATEstatic int sqlite3DbstatRegister(sqlite3*);
20744#endif
20745
20746SQLITE_PRIVATEstatic int sqlite3ExprVectorSize(const Expr *pExpr);
20747SQLITE_PRIVATEstatic int sqlite3ExprIsVector(const Expr *pExpr);
20748SQLITE_PRIVATEstatic Expr *sqlite3VectorFieldSubexpr(Expr*, int);
20749SQLITE_PRIVATEstatic Expr *sqlite3ExprForVectorField(Parse*,Expr*,int,int);
20750SQLITE_PRIVATEstatic void sqlite3VectorErrorMsg(Parse*, Expr*);
20751
20752#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
20753SQLITE_PRIVATEstatic const char **sqlite3CompileOptions(int *pnOpt);
20754#endif
20755
20756#endif /* SQLITEINT_H */
20757
20758/************** End of sqliteInt.h *******************************************/
20759/************** Begin file os_common.h ***************************************/
20760/*
20761** 2004 May 22
20762**
20763** The author disclaims copyright to this source code. In place of
20764** a legal notice, here is a blessing:
20765**
20766** May you do good and not evil.
20767** May you find forgiveness for yourself and forgive others.
20768** May you share freely, never taking more than you give.
20769**
20770******************************************************************************
20771**
20772** This file contains macros and a little bit of code that is common to
20773** all of the platform-specific files (os_*.c) and is #included into those
20774** files.
20775**
20776** This file should be #included by the os_*.c files only. It is not a
20777** general purpose header file.
20778*/
20779#ifndef _OS_COMMON_H_
20780#define _OS_COMMON_H_
20781
20782/*
20783** At least two bugs have slipped in because we changed the MEMORY_DEBUG
20784** macro to SQLITE_DEBUG and some older makefiles have not yet made the
20785** switch. The following code should catch this problem at compile-time.
20786*/
20787#ifdef MEMORY_DEBUG
20788# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
20789#endif
20790
20791/*
20792** Macros for performance tracing. Normally turned off. Only works
20793** on i486 hardware.
20794*/
20795#ifdef SQLITE_PERFORMANCE_TRACE
20796
20797/*
20798** hwtime.h contains inline assembler code for implementing
20799** high-performance timing routines.
20800*/
20801/************** Include hwtime.h in the middle of os_common.h ****************/
20802/************** Begin file hwtime.h ******************************************/
20803/*
20804** 2008 May 27
20805**
20806** The author disclaims copyright to this source code. In place of
20807** a legal notice, here is a blessing:
20808**
20809** May you do good and not evil.
20810** May you find forgiveness for yourself and forgive others.
20811** May you share freely, never taking more than you give.
20812**
20813******************************************************************************
20814**
20815** This file contains inline asm code for retrieving "high-performance"
20816** counters for x86 and x86_64 class CPUs.
20817*/
20818#ifndef SQLITE_HWTIME_H
20819#define SQLITE_HWTIME_H
20820
20821/*
20822** The following routine only works on pentium-class (or newer) processors.
20823** It uses the RDTSC opcode to read the cycle count value out of the
20824** processor and returns that value. This can be used for high-res
20825** profiling.
20826*/
20827#if !defined(__STRICT_ANSI__) && \
20828 (defined(__GNUC__4) || defined(_MSC_VER)) && \
20829 (defined(i386) || defined(__i386__) || defined(_M_IX86))
20830
20831 #if defined(__GNUC__4)
20832
20833 __inline__ sqlite_uint64 sqlite3Hwtime(void){
20834 unsigned int lo, hi;
20835 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
20836 return (sqlite_uint64)hi << 32 | lo;
20837 }
20838
20839 #elif defined(_MSC_VER)
20840
20841 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
20842 __asm {
20843 rdtsc
20844 ret ; return value at EDX:EAX
20845 }
20846 }
20847
20848 #endif
20849
20850#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__4) && defined(__x86_64__1))
20851
20852 __inline__ sqlite_uint64 sqlite3Hwtime(void){
20853 unsigned long val;
20854 __asm__ __volatile__ ("rdtsc" : "=A" (val));
20855 return val;
20856 }
20857
20858#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__4) && defined(__ppc__))
20859
20860 __inline__ sqlite_uint64 sqlite3Hwtime(void){
20861 unsigned long long retval;
20862 unsigned long junk;
20863 __asm__ __volatile__ ("\n\
20864 1: mftbu %1\n\
20865 mftb %L0\n\
20866 mftbu %0\n\
20867 cmpw %0,%1\n\
20868 bne 1b"
20869 : "=r" (retval), "=r" (junk));
20870 return retval;
20871 }
20872
20873#else
20874
20875 /*
20876 ** asm() is needed for hardware timing support. Without asm(),
20877 ** disable the sqlite3Hwtime() routine.
20878 **
20879 ** sqlite3Hwtime() is only used for some obscure debugging
20880 ** and analysis configurations, not in any deliverable, so this
20881 ** should not be a great loss.
20882 */
20883SQLITE_PRIVATEstatic sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
20884
20885#endif
20886
20887#endif /* !defined(SQLITE_HWTIME_H) */
20888
20889/************** End of hwtime.h **********************************************/
20890/************** Continuing where we left off in os_common.h ******************/
20891
20892static sqlite_uint64 g_start;
20893static sqlite_uint64 g_elapsed;
20894#define TIMER_START g_start=sqlite3Hwtime()
20895#define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
20896#define TIMER_ELAPSED((sqlite_uint64)0) g_elapsed
20897#else
20898#define TIMER_START
20899#define TIMER_END
20900#define TIMER_ELAPSED((sqlite_uint64)0) ((sqlite_uint64)0)
20901#endif
20902
20903/*
20904** If we compile with the SQLITE_TEST macro set, then the following block
20905** of code will give us the ability to simulate a disk I/O error. This
20906** is used for testing the I/O recovery logic.
20907*/
20908#if defined(SQLITE_TEST)
20909SQLITE_API extern int sqlite3_io_error_hit;
20910SQLITE_API extern int sqlite3_io_error_hardhit;
20911SQLITE_API extern int sqlite3_io_error_pending;
20912SQLITE_API extern int sqlite3_io_error_persist;
20913SQLITE_API extern int sqlite3_io_error_benign;
20914SQLITE_API extern int sqlite3_diskfull_pending;
20915SQLITE_API extern int sqlite3_diskfull;
20916#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
20917#define SimulateIOError(CODE) \
20918 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
20919 || sqlite3_io_error_pending-- == 1 ) \
20920 { local_ioerr(); CODE; }
20921static void local_ioerr(){
20922 IOTRACE(("IOERR\n"));
20923 sqlite3_io_error_hit++;
20924 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
20925}
20926#define SimulateDiskfullError(CODE) \
20927 if( sqlite3_diskfull_pending ){ \
20928 if( sqlite3_diskfull_pending == 1 ){ \
20929 local_ioerr(); \
20930 sqlite3_diskfull = 1; \
20931 sqlite3_io_error_hit = 1; \
20932 CODE; \
20933 }else{ \
20934 sqlite3_diskfull_pending--; \
20935 } \
20936 }
20937#else
20938#define SimulateIOErrorBenign(X)
20939#define SimulateIOError(A)
20940#define SimulateDiskfullError(A)
20941#endif /* defined(SQLITE_TEST) */
20942
20943/*
20944** When testing, keep a count of the number of open files.
20945*/
20946#if defined(SQLITE_TEST)
20947SQLITE_API extern int sqlite3_open_file_count;
20948#define OpenCounter(X) sqlite3_open_file_count+=(X)
20949#else
20950#define OpenCounter(X)
20951#endif /* defined(SQLITE_TEST) */
20952
20953#endif /* !defined(_OS_COMMON_H_) */
20954
20955/************** End of os_common.h *******************************************/
20956/************** Begin file ctime.c *******************************************/
20957/* DO NOT EDIT!
20958** This file is automatically generated by the script in the canonical
20959** SQLite source tree at tool/mkctimec.tcl.
20960**
20961** To modify this header, edit any of the various lists in that script
20962** which specify categories of generated conditionals in this file.
20963*/
20964
20965/*
20966** 2010 February 23
20967**
20968** The author disclaims copyright to this source code. In place of
20969** a legal notice, here is a blessing:
20970**
20971** May you do good and not evil.
20972** May you find forgiveness for yourself and forgive others.
20973** May you share freely, never taking more than you give.
20974**
20975*************************************************************************
20976**
20977** This file implements routines used to report what compile-time options
20978** SQLite was built with.
20979*/
20980#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS /* IMP: R-16824-07538 */
20981
20982/*
20983** Include the configuration header output by 'configure' if we're using the
20984** autoconf-based build
20985*/
20986#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
20987/* #include "config.h" */
20988#define SQLITECONFIG_H 1
20989#endif
20990
20991/* These macros are provided to "stringify" the value of the define
20992** for those options in which the value is meaningful. */
20993#define CTIMEOPT_VAL_(opt)"opt" #opt
20994#define CTIMEOPT_VAL(opt)"opt" CTIMEOPT_VAL_(opt)"opt"
20995
20996/* Like CTIMEOPT_VAL, but especially for SQLITE_DEFAULT_LOOKASIDE. This
20997** option requires a separate macro because legal values contain a single
20998** comma. e.g. (-DSQLITE_DEFAULT_LOOKASIDE="100,100") */
20999#define CTIMEOPT_VAL2_(opt1,opt2)"opt1" "," "opt2" #opt1 "," #opt2
21000#define CTIMEOPT_VAL2(opt)CTIMEOPT_VAL2_ CTIMEOPT_VAL2_(opt)
21001/* #include "sqliteInt.h" */
21002
21003/*
21004** An array of names of all compile-time options. This array should
21005** be sorted A-Z.
21006**
21007** This array looks large, but in a typical installation actually uses
21008** only a handful of compile-time options, so most times this array is usually
21009** rather short and uses little memory space.
21010*/
21011static const char * const sqlite3azCompileOpt[] = {
21012
21013#ifdef SQLITE_32BIT_ROWID
21014 "32BIT_ROWID",
21015#endif
21016#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
21017 "4_BYTE_ALIGNED_MALLOC",
21018#endif
21019#ifdef SQLITE_64BIT_STATS
21020 "64BIT_STATS",
21021#endif
21022#ifdef SQLITE_ALLOW_COVERING_INDEX_SCAN1
21023# if SQLITE_ALLOW_COVERING_INDEX_SCAN1 != 1
21024 "ALLOW_COVERING_INDEX_SCAN=" CTIMEOPT_VAL(SQLITE_ALLOW_COVERING_INDEX_SCAN)"1",
21025# endif
21026#endif
21027#ifdef SQLITE_ALLOW_URI_AUTHORITY
21028 "ALLOW_URI_AUTHORITY",
21029#endif
21030#ifdef SQLITE_ATOMIC_INTRINSICS1
21031 "ATOMIC_INTRINSICS=" CTIMEOPT_VAL(SQLITE_ATOMIC_INTRINSICS)"1",
21032#endif
21033#ifdef SQLITE_BITMASK_TYPE
21034 "BITMASK_TYPE=" CTIMEOPT_VAL(SQLITE_BITMASK_TYPE)"SQLITE_BITMASK_TYPE",
21035#endif
21036#ifdef SQLITE_BUG_COMPATIBLE_20160819
21037 "BUG_COMPATIBLE_20160819",
21038#endif
21039#ifdef SQLITE_CASE_SENSITIVE_LIKE
21040 "CASE_SENSITIVE_LIKE",
21041#endif
21042#ifdef SQLITE_CHECK_PAGES
21043 "CHECK_PAGES",
21044#endif
21045#if defined(__clang__1) && defined(__clang_major__18)
21046 "COMPILER=clang-" CTIMEOPT_VAL(__clang_major__)"18" "."
21047 CTIMEOPT_VAL(__clang_minor__)"1" "."
21048 CTIMEOPT_VAL(__clang_patchlevel__)"2",
21049#elif defined(_MSC_VER)
21050 "COMPILER=msvc-" CTIMEOPT_VAL(_MSC_VER)"_MSC_VER",
21051#elif defined(__GNUC__4) && defined(__VERSION__"Clang 18.1.2")
21052 "COMPILER=gcc-" __VERSION__"Clang 18.1.2",
21053#endif
21054#ifdef SQLITE_COVERAGE_TEST
21055 "COVERAGE_TEST",
21056#endif
21057#ifdef SQLITE_DEBUG
21058 "DEBUG",
21059#endif
21060#ifdef SQLITE_DEFAULT_AUTOMATIC_INDEX
21061 "DEFAULT_AUTOMATIC_INDEX",
21062#endif
21063#ifdef SQLITE_DEFAULT_AUTOVACUUM0
21064 "DEFAULT_AUTOVACUUM",
21065#endif
21066#ifdef SQLITE_DEFAULT_CACHE_SIZE128
21067 "DEFAULT_CACHE_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_CACHE_SIZE)"128",
21068#endif
21069#ifdef SQLITE_DEFAULT_CKPTFULLFSYNC1
21070 "DEFAULT_CKPTFULLFSYNC",
21071#endif
21072#ifdef SQLITE_DEFAULT_FILE_FORMAT4
21073 "DEFAULT_FILE_FORMAT=" CTIMEOPT_VAL(SQLITE_DEFAULT_FILE_FORMAT)"4",
21074#endif
21075#ifdef SQLITE_DEFAULT_FILE_PERMISSIONS0644
21076 "DEFAULT_FILE_PERMISSIONS=" CTIMEOPT_VAL(SQLITE_DEFAULT_FILE_PERMISSIONS)"0644",
21077#endif
21078#ifdef SQLITE_DEFAULT_FOREIGN_KEYS
21079 "DEFAULT_FOREIGN_KEYS",
21080#endif
21081#ifdef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT32768
21082 "DEFAULT_JOURNAL_SIZE_LIMIT=" CTIMEOPT_VAL(SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT)"32768",
21083#endif
21084#ifdef SQLITE_DEFAULT_LOCKING_MODE
21085 "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE)"SQLITE_DEFAULT_LOCKING_MODE",
21086#endif
21087#ifdef SQLITE_DEFAULT_LOOKASIDE1200,40
21088 "DEFAULT_LOOKASIDE=" CTIMEOPT_VAL2(SQLITE_DEFAULT_LOOKASIDE)"1200" "," "40",
21089#endif
21090#ifdef SQLITE_DEFAULT_MEMSTATUS1
21091# if SQLITE_DEFAULT_MEMSTATUS1 != 1
21092 "DEFAULT_MEMSTATUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_MEMSTATUS)"1",
21093# endif
21094#endif
21095#ifdef SQLITE_DEFAULT_MMAP_SIZE0
21096 "DEFAULT_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_MMAP_SIZE)"0",
21097#endif
21098#ifdef SQLITE_DEFAULT_PAGE_SIZE4096
21099 "DEFAULT_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_PAGE_SIZE)"4096",
21100#endif
21101#ifdef SQLITE_DEFAULT_PCACHE_INITSZ20
21102 "DEFAULT_PCACHE_INITSZ=" CTIMEOPT_VAL(SQLITE_DEFAULT_PCACHE_INITSZ)"20",
21103#endif
21104#ifdef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS0755
21105 "DEFAULT_PROXYDIR_PERMISSIONS=" CTIMEOPT_VAL(SQLITE_DEFAULT_PROXYDIR_PERMISSIONS)"0755",
21106#endif
21107#ifdef SQLITE_DEFAULT_RECURSIVE_TRIGGERS0
21108 "DEFAULT_RECURSIVE_TRIGGERS",
21109#endif
21110#ifdef SQLITE_DEFAULT_ROWEST
21111 "DEFAULT_ROWEST=" CTIMEOPT_VAL(SQLITE_DEFAULT_ROWEST)"SQLITE_DEFAULT_ROWEST",
21112#endif
21113#ifdef SQLITE_DEFAULT_SECTOR_SIZE4096
21114 "DEFAULT_SECTOR_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_SECTOR_SIZE)"4096",
21115#endif
21116#ifdef SQLITE_DEFAULT_SYNCHRONOUS2
21117 "DEFAULT_SYNCHRONOUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_SYNCHRONOUS)"2",
21118#endif
21119#ifdef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT1000
21120 "DEFAULT_WAL_AUTOCHECKPOINT=" CTIMEOPT_VAL(SQLITE_DEFAULT_WAL_AUTOCHECKPOINT)"1000",
21121#endif
21122#ifdef SQLITE_DEFAULT_WAL_SYNCHRONOUS1
21123 "DEFAULT_WAL_SYNCHRONOUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_WAL_SYNCHRONOUS)"1",
21124#endif
21125#ifdef SQLITE_DEFAULT_WORKER_THREADS0
21126 "DEFAULT_WORKER_THREADS=" CTIMEOPT_VAL(SQLITE_DEFAULT_WORKER_THREADS)"0",
21127#endif
21128#ifdef SQLITE_DIRECT_OVERFLOW_READ
21129 "DIRECT_OVERFLOW_READ",
21130#endif
21131#ifdef SQLITE_DISABLE_DIRSYNC
21132 "DISABLE_DIRSYNC",
21133#endif
21134#ifdef SQLITE_DISABLE_FTS3_UNICODE
21135 "DISABLE_FTS3_UNICODE",
21136#endif
21137#ifdef SQLITE_DISABLE_FTS4_DEFERRED
21138 "DISABLE_FTS4_DEFERRED",
21139#endif
21140#ifdef SQLITE_DISABLE_INTRINSIC
21141 "DISABLE_INTRINSIC",
21142#endif
21143#ifdef SQLITE_DISABLE_LFS
21144 "DISABLE_LFS",
21145#endif
21146#ifdef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
21147 "DISABLE_PAGECACHE_OVERFLOW_STATS",
21148#endif
21149#ifdef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
21150 "DISABLE_SKIPAHEAD_DISTINCT",
21151#endif
21152#ifdef SQLITE_ENABLE_8_3_NAMES
21153 "ENABLE_8_3_NAMES=" CTIMEOPT_VAL(SQLITE_ENABLE_8_3_NAMES)"SQLITE_ENABLE_8_3_NAMES",
21154#endif
21155#ifdef SQLITE_ENABLE_API_ARMOR1
21156 "ENABLE_API_ARMOR",
21157#endif
21158#ifdef SQLITE_ENABLE_ATOMIC_WRITE
21159 "ENABLE_ATOMIC_WRITE",
21160#endif
21161#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
21162 "ENABLE_BATCH_ATOMIC_WRITE",
21163#endif
21164#ifdef SQLITE_ENABLE_BYTECODE_VTAB
21165 "ENABLE_BYTECODE_VTAB",
21166#endif
21167#ifdef SQLITE_ENABLE_CEROD
21168 "ENABLE_CEROD=" CTIMEOPT_VAL(SQLITE_ENABLE_CEROD)"SQLITE_ENABLE_CEROD",
21169#endif
21170#ifdef SQLITE_ENABLE_COLUMN_METADATA1
21171 "ENABLE_COLUMN_METADATA",
21172#endif
21173#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
21174 "ENABLE_COLUMN_USED_MASK",
21175#endif
21176#ifdef SQLITE_ENABLE_COSTMULT
21177 "ENABLE_COSTMULT",
21178#endif
21179#ifdef SQLITE_ENABLE_CURSOR_HINTS
21180 "ENABLE_CURSOR_HINTS",
21181#endif
21182#ifdef SQLITE_ENABLE_DBPAGE_VTAB
21183 "ENABLE_DBPAGE_VTAB",
21184#endif
21185#ifdef SQLITE_ENABLE_DBSTAT_VTAB1
21186 "ENABLE_DBSTAT_VTAB",
21187#endif
21188#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
21189 "ENABLE_EXPENSIVE_ASSERT",
21190#endif
21191#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
21192 "ENABLE_EXPLAIN_COMMENTS",
21193#endif
21194#ifdef SQLITE_ENABLE_FTS31
21195 "ENABLE_FTS3",
21196#endif
21197#ifdef SQLITE_ENABLE_FTS3_PARENTHESIS1
21198 "ENABLE_FTS3_PARENTHESIS",
21199#endif
21200#ifdef SQLITE_ENABLE_FTS3_TOKENIZER1
21201 "ENABLE_FTS3_TOKENIZER",
21202#endif
21203#ifdef SQLITE_ENABLE_FTS41
21204 "ENABLE_FTS4",
21205#endif
21206#ifdef SQLITE_ENABLE_FTS51
21207 "ENABLE_FTS5",
21208#endif
21209#ifdef SQLITE_ENABLE_GEOPOLY
21210 "ENABLE_GEOPOLY",
21211#endif
21212#ifdef SQLITE_ENABLE_HIDDEN_COLUMNS
21213 "ENABLE_HIDDEN_COLUMNS",
21214#endif
21215#ifdef SQLITE_ENABLE_ICU
21216 "ENABLE_ICU",
21217#endif
21218#ifdef SQLITE_ENABLE_IOTRACE
21219 "ENABLE_IOTRACE",
21220#endif
21221#ifdef SQLITE_ENABLE_LOAD_EXTENSION
21222 "ENABLE_LOAD_EXTENSION",
21223#endif
21224#ifdef SQLITE_ENABLE_LOCKING_STYLE0
21225 "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE)"0",
21226#endif
21227#ifdef SQLITE_ENABLE_MATH_FUNCTIONS1
21228 "ENABLE_MATH_FUNCTIONS",
21229#endif
21230#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
21231 "ENABLE_MEMORY_MANAGEMENT",
21232#endif
21233#ifdef SQLITE_ENABLE_MEMSYS3
21234 "ENABLE_MEMSYS3",
21235#endif
21236#ifdef SQLITE_ENABLE_MEMSYS5
21237 "ENABLE_MEMSYS5",
21238#endif
21239#ifdef SQLITE_ENABLE_MULTIPLEX
21240 "ENABLE_MULTIPLEX",
21241#endif
21242#ifdef SQLITE_ENABLE_NORMALIZE
21243 "ENABLE_NORMALIZE",
21244#endif
21245#ifdef SQLITE_ENABLE_NULL_TRIM
21246 "ENABLE_NULL_TRIM",
21247#endif
21248#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
21249 "ENABLE_OFFSET_SQL_FUNC",
21250#endif
21251#ifdef SQLITE_ENABLE_OVERSIZE_CELL_CHECK
21252 "ENABLE_OVERSIZE_CELL_CHECK",
21253#endif
21254#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
21255 "ENABLE_PREUPDATE_HOOK",
21256#endif
21257#ifdef SQLITE_ENABLE_QPSG
21258 "ENABLE_QPSG",
21259#endif
21260#ifdef SQLITE_ENABLE_RBU
21261 "ENABLE_RBU",
21262#endif
21263#ifdef SQLITE_ENABLE_RTREE1
21264 "ENABLE_RTREE",
21265#endif
21266#ifdef SQLITE_ENABLE_SESSION1
21267 "ENABLE_SESSION",
21268#endif
21269#ifdef SQLITE_ENABLE_SNAPSHOT1
21270 "ENABLE_SNAPSHOT",
21271#endif
21272#ifdef SQLITE_ENABLE_SORTER_REFERENCES
21273 "ENABLE_SORTER_REFERENCES",
21274#endif
21275#ifdef SQLITE_ENABLE_SQLLOG
21276 "ENABLE_SQLLOG",
21277#endif
21278#ifdef SQLITE_ENABLE_STAT4
21279 "ENABLE_STAT4",
21280#endif
21281#ifdef SQLITE_ENABLE_STMTVTAB
21282 "ENABLE_STMTVTAB",
21283#endif
21284#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
21285 "ENABLE_STMT_SCANSTATUS",
21286#endif
21287#ifdef SQLITE_ENABLE_TREETRACE
21288 "ENABLE_TREETRACE",
21289#endif
21290#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION1
21291 "ENABLE_UNKNOWN_SQL_FUNCTION",
21292#endif
21293#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
21294 "ENABLE_UNLOCK_NOTIFY",
21295#endif
21296#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT1
21297 "ENABLE_UPDATE_DELETE_LIMIT",
21298#endif
21299#ifdef SQLITE_ENABLE_URI_00_ERROR
21300 "ENABLE_URI_00_ERROR",
21301#endif
21302#ifdef SQLITE_ENABLE_VFSTRACE
21303 "ENABLE_VFSTRACE",
21304#endif
21305#ifdef SQLITE_ENABLE_WHERETRACE
21306 "ENABLE_WHERETRACE",
21307#endif
21308#ifdef SQLITE_ENABLE_ZIPVFS
21309 "ENABLE_ZIPVFS",
21310#endif
21311#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
21312 "EXPLAIN_ESTIMATED_ROWS",
21313#endif
21314#ifdef SQLITE_EXTRA_IFNULLROW
21315 "EXTRA_IFNULLROW",
21316#endif
21317#ifdef SQLITE_EXTRA_INIT
21318 "EXTRA_INIT=" CTIMEOPT_VAL(SQLITE_EXTRA_INIT)"SQLITE_EXTRA_INIT",
21319#endif
21320#ifdef SQLITE_EXTRA_SHUTDOWN
21321 "EXTRA_SHUTDOWN=" CTIMEOPT_VAL(SQLITE_EXTRA_SHUTDOWN)"SQLITE_EXTRA_SHUTDOWN",
21322#endif
21323#ifdef SQLITE_FTS3_MAX_EXPR_DEPTH12
21324 "FTS3_MAX_EXPR_DEPTH=" CTIMEOPT_VAL(SQLITE_FTS3_MAX_EXPR_DEPTH)"12",
21325#endif
21326#ifdef SQLITE_FTS5_ENABLE_TEST_MI
21327 "FTS5_ENABLE_TEST_MI",
21328#endif
21329#ifdef SQLITE_FTS5_NO_WITHOUT_ROWID
21330 "FTS5_NO_WITHOUT_ROWID",
21331#endif
21332#if HAVE_ISNAN || SQLITE_HAVE_ISNAN1
21333 "HAVE_ISNAN",
21334#endif
21335#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
21336# if SQLITE_HOMEGROWN_RECURSIVE_MUTEX != 1
21337 "HOMEGROWN_RECURSIVE_MUTEX=" CTIMEOPT_VAL(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)"SQLITE_HOMEGROWN_RECURSIVE_MUTEX",
21338# endif
21339#endif
21340#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
21341 "IGNORE_AFP_LOCK_ERRORS",
21342#endif
21343#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
21344 "IGNORE_FLOCK_LOCK_ERRORS",
21345#endif
21346#ifdef SQLITE_INLINE_MEMCPY
21347 "INLINE_MEMCPY",
21348#endif
21349#ifdef SQLITE_INT64_TYPE
21350 "INT64_TYPE",
21351#endif
21352#ifdef SQLITE_INTEGRITY_CHECK_ERROR_MAX100
21353 "INTEGRITY_CHECK_ERROR_MAX=" CTIMEOPT_VAL(SQLITE_INTEGRITY_CHECK_ERROR_MAX)"100",
21354#endif
21355#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
21356 "LIKE_DOESNT_MATCH_BLOBS",
21357#endif
21358#ifdef SQLITE_LOCK_TRACE
21359 "LOCK_TRACE",
21360#endif
21361#ifdef SQLITE_LOG_CACHE_SPILL
21362 "LOG_CACHE_SPILL",
21363#endif
21364#ifdef SQLITE_MALLOC_SOFT_LIMIT1024
21365 "MALLOC_SOFT_LIMIT=" CTIMEOPT_VAL(SQLITE_MALLOC_SOFT_LIMIT)"1024",
21366#endif
21367#ifdef SQLITE_MAX_ATTACHED10
21368 "MAX_ATTACHED=" CTIMEOPT_VAL(SQLITE_MAX_ATTACHED)"10",
21369#endif
21370#ifdef SQLITE_MAX_COLUMN2000
21371 "MAX_COLUMN=" CTIMEOPT_VAL(SQLITE_MAX_COLUMN)"2000",
21372#endif
21373#ifdef SQLITE_MAX_COMPOUND_SELECT500
21374 "MAX_COMPOUND_SELECT=" CTIMEOPT_VAL(SQLITE_MAX_COMPOUND_SELECT)"500",
21375#endif
21376#ifdef SQLITE_MAX_DEFAULT_PAGE_SIZE8192
21377 "MAX_DEFAULT_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_DEFAULT_PAGE_SIZE)"8192",
21378#endif
21379#ifdef SQLITE_MAX_EXPR_DEPTH1000
21380 "MAX_EXPR_DEPTH=" CTIMEOPT_VAL(SQLITE_MAX_EXPR_DEPTH)"1000",
21381#endif
21382#ifdef SQLITE_MAX_FUNCTION_ARG127
21383 "MAX_FUNCTION_ARG=" CTIMEOPT_VAL(SQLITE_MAX_FUNCTION_ARG)"127",
21384#endif
21385#ifdef SQLITE_MAX_LENGTH2147483645
21386 "MAX_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_LENGTH)"2147483645",
21387#endif
21388#ifdef SQLITE_MAX_LIKE_PATTERN_LENGTH50000
21389 "MAX_LIKE_PATTERN_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_LIKE_PATTERN_LENGTH)"50000",
21390#endif
21391#ifdef SQLITE_MAX_MEMORY0
21392 "MAX_MEMORY=" CTIMEOPT_VAL(SQLITE_MAX_MEMORY)"0",
21393#endif
21394#ifdef SQLITE_MAX_MMAP_SIZE20971520
21395 "MAX_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE)"20971520",
21396#endif
21397#ifdef SQLITE_MAX_MMAP_SIZE_
21398 "MAX_MMAP_SIZE_=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE_)"SQLITE_MAX_MMAP_SIZE_",
21399#endif
21400#ifdef SQLITE_MAX_PAGE_COUNT1073741823
21401 "MAX_PAGE_COUNT=" CTIMEOPT_VAL(SQLITE_MAX_PAGE_COUNT)"1073741823",
21402#endif
21403#ifdef SQLITE_MAX_PAGE_SIZE65536
21404 "MAX_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_PAGE_SIZE)"65536",
21405#endif
21406#ifdef SQLITE_MAX_SCHEMA_RETRY50
21407 "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY)"50",
21408#endif
21409#ifdef SQLITE_MAX_SQL_LENGTH1000000000
21410 "MAX_SQL_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_SQL_LENGTH)"1000000000",
21411#endif
21412#ifdef SQLITE_MAX_TRIGGER_DEPTH1000
21413 "MAX_TRIGGER_DEPTH=" CTIMEOPT_VAL(SQLITE_MAX_TRIGGER_DEPTH)"1000",
21414#endif
21415#ifdef SQLITE_MAX_VARIABLE_NUMBER500000
21416 "MAX_VARIABLE_NUMBER=" CTIMEOPT_VAL(SQLITE_MAX_VARIABLE_NUMBER)"500000",
21417#endif
21418#ifdef SQLITE_MAX_VDBE_OP250000000
21419 "MAX_VDBE_OP=" CTIMEOPT_VAL(SQLITE_MAX_VDBE_OP)"250000000",
21420#endif
21421#ifdef SQLITE_MAX_WORKER_THREADS8
21422 "MAX_WORKER_THREADS=" CTIMEOPT_VAL(SQLITE_MAX_WORKER_THREADS)"8",
21423#endif
21424#ifdef SQLITE_MEMDEBUG
21425 "MEMDEBUG",
21426#endif
21427#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
21428 "MIXED_ENDIAN_64BIT_FLOAT",
21429#endif
21430#ifdef SQLITE_MMAP_READWRITE
21431 "MMAP_READWRITE",
21432#endif
21433#ifdef SQLITE_MUTEX_NOOP
21434 "MUTEX_NOOP",
21435#endif
21436#ifdef SQLITE_MUTEX_OMIT
21437 "MUTEX_OMIT",
21438#endif
21439#ifdef SQLITE_MUTEX_PTHREADS
21440 "MUTEX_PTHREADS",
21441#endif
21442#ifdef SQLITE_MUTEX_W32
21443 "MUTEX_W32",
21444#endif
21445#ifdef SQLITE_NEED_ERR_NAME
21446 "NEED_ERR_NAME",
21447#endif
21448#ifdef SQLITE_NO_SYNC
21449 "NO_SYNC",
21450#endif
21451#ifdef SQLITE_OMIT_ALTERTABLE
21452 "OMIT_ALTERTABLE",
21453#endif
21454#ifdef SQLITE_OMIT_ANALYZE
21455 "OMIT_ANALYZE",
21456#endif
21457#ifdef SQLITE_OMIT_ATTACH
21458 "OMIT_ATTACH",
21459#endif
21460#ifdef SQLITE_OMIT_AUTHORIZATION
21461 "OMIT_AUTHORIZATION",
21462#endif
21463#ifdef SQLITE_OMIT_AUTOINCREMENT
21464 "OMIT_AUTOINCREMENT",
21465#endif
21466#ifdef SQLITE_OMIT_AUTOINIT
21467 "OMIT_AUTOINIT",
21468#endif
21469#ifdef SQLITE_OMIT_AUTOMATIC_INDEX
21470 "OMIT_AUTOMATIC_INDEX",
21471#endif
21472#ifdef SQLITE_OMIT_AUTORESET1
21473 "OMIT_AUTORESET",
21474#endif
21475#ifdef SQLITE_OMIT_AUTOVACUUM
21476 "OMIT_AUTOVACUUM",
21477#endif
21478#ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
21479 "OMIT_BETWEEN_OPTIMIZATION",
21480#endif
21481#ifdef SQLITE_OMIT_BLOB_LITERAL
21482 "OMIT_BLOB_LITERAL",
21483#endif
21484#ifdef SQLITE_OMIT_CAST
21485 "OMIT_CAST",
21486#endif
21487#ifdef SQLITE_OMIT_CHECK
21488 "OMIT_CHECK",
21489#endif
21490#ifdef SQLITE_OMIT_COMPLETE
21491 "OMIT_COMPLETE",
21492#endif
21493#ifdef SQLITE_OMIT_COMPOUND_SELECT
21494 "OMIT_COMPOUND_SELECT",
21495#endif
21496#ifdef SQLITE_OMIT_CONFLICT_CLAUSE
21497 "OMIT_CONFLICT_CLAUSE",
21498#endif
21499#ifdef SQLITE_OMIT_CTE
21500 "OMIT_CTE",
21501#endif
21502#if defined(SQLITE_OMIT_DATETIME_FUNCS) || defined(SQLITE_OMIT_FLOATING_POINT)
21503 "OMIT_DATETIME_FUNCS",
21504#endif
21505#ifdef SQLITE_OMIT_DECLTYPE
21506 "OMIT_DECLTYPE",
21507#endif
21508#ifdef SQLITE_OMIT_DEPRECATED1
21509 "OMIT_DEPRECATED",
21510#endif
21511#ifdef SQLITE_OMIT_DESERIALIZE
21512 "OMIT_DESERIALIZE",
21513#endif
21514#ifdef SQLITE_OMIT_DISKIO
21515 "OMIT_DISKIO",
21516#endif
21517#ifdef SQLITE_OMIT_EXPLAIN
21518 "OMIT_EXPLAIN",
21519#endif
21520#ifdef SQLITE_OMIT_FLAG_PRAGMAS
21521 "OMIT_FLAG_PRAGMAS",
21522#endif
21523#ifdef SQLITE_OMIT_FLOATING_POINT
21524 "OMIT_FLOATING_POINT",
21525#endif
21526#ifdef SQLITE_OMIT_FOREIGN_KEY
21527 "OMIT_FOREIGN_KEY",
21528#endif
21529#ifdef SQLITE_OMIT_GET_TABLE
21530 "OMIT_GET_TABLE",
21531#endif
21532#ifdef SQLITE_OMIT_HEX_INTEGER
21533 "OMIT_HEX_INTEGER",
21534#endif
21535#ifdef SQLITE_OMIT_INCRBLOB
21536 "OMIT_INCRBLOB",
21537#endif
21538#ifdef SQLITE_OMIT_INTEGRITY_CHECK
21539 "OMIT_INTEGRITY_CHECK",
21540#endif
21541#ifdef SQLITE_OMIT_INTROSPECTION_PRAGMAS
21542 "OMIT_INTROSPECTION_PRAGMAS",
21543#endif
21544#ifdef SQLITE_OMIT_JSON
21545 "OMIT_JSON",
21546#endif
21547#ifdef SQLITE_OMIT_LIKE_OPTIMIZATION
21548 "OMIT_LIKE_OPTIMIZATION",
21549#endif
21550#ifdef SQLITE_OMIT_LOAD_EXTENSION1
21551 "OMIT_LOAD_EXTENSION",
21552#endif
21553#ifdef SQLITE_OMIT_LOCALTIME
21554 "OMIT_LOCALTIME",
21555#endif
21556#ifdef SQLITE_OMIT_LOOKASIDE
21557 "OMIT_LOOKASIDE",
21558#endif
21559#ifdef SQLITE_OMIT_MEMORYDB
21560 "OMIT_MEMORYDB",
21561#endif
21562#ifdef SQLITE_OMIT_OR_OPTIMIZATION
21563 "OMIT_OR_OPTIMIZATION",
21564#endif
21565#ifdef SQLITE_OMIT_PAGER_PRAGMAS
21566 "OMIT_PAGER_PRAGMAS",
21567#endif
21568#ifdef SQLITE_OMIT_PARSER_TRACE
21569 "OMIT_PARSER_TRACE",
21570#endif
21571#ifdef SQLITE_OMIT_POPEN
21572 "OMIT_POPEN",
21573#endif
21574#ifdef SQLITE_OMIT_PRAGMA
21575 "OMIT_PRAGMA",
21576#endif
21577#ifdef SQLITE_OMIT_PROGRESS_CALLBACK1
21578 "OMIT_PROGRESS_CALLBACK",
21579#endif
21580#ifdef SQLITE_OMIT_QUICKBALANCE
21581 "OMIT_QUICKBALANCE",
21582#endif
21583#ifdef SQLITE_OMIT_REINDEX
21584 "OMIT_REINDEX",
21585#endif
21586#ifdef SQLITE_OMIT_SCHEMA_PRAGMAS
21587 "OMIT_SCHEMA_PRAGMAS",
21588#endif
21589#ifdef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
21590 "OMIT_SCHEMA_VERSION_PRAGMAS",
21591#endif
21592#ifdef SQLITE_OMIT_SHARED_CACHE
21593 "OMIT_SHARED_CACHE",
21594#endif
21595#ifdef SQLITE_OMIT_SHUTDOWN_DIRECTORIES
21596 "OMIT_SHUTDOWN_DIRECTORIES",
21597#endif
21598#ifdef SQLITE_OMIT_SUBQUERY
21599 "OMIT_SUBQUERY",
21600#endif
21601#ifdef SQLITE_OMIT_TCL_VARIABLE
21602 "OMIT_TCL_VARIABLE",
21603#endif
21604#ifdef SQLITE_OMIT_TEMPDB
21605 "OMIT_TEMPDB",
21606#endif
21607#ifdef SQLITE_OMIT_TEST_CONTROL
21608 "OMIT_TEST_CONTROL",
21609#endif
21610#ifdef SQLITE_OMIT_TRACE
21611# if SQLITE_OMIT_TRACE != 1
21612 "OMIT_TRACE=" CTIMEOPT_VAL(SQLITE_OMIT_TRACE)"SQLITE_OMIT_TRACE",
21613# endif
21614#endif
21615#ifdef SQLITE_OMIT_TRIGGER
21616 "OMIT_TRIGGER",
21617#endif
21618#ifdef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
21619 "OMIT_TRUNCATE_OPTIMIZATION",
21620#endif
21621#ifdef SQLITE_OMIT_UTF16
21622 "OMIT_UTF16",
21623#endif
21624#ifdef SQLITE_OMIT_VACUUM
21625 "OMIT_VACUUM",
21626#endif
21627#ifdef SQLITE_OMIT_VIEW
21628 "OMIT_VIEW",
21629#endif
21630#ifdef SQLITE_OMIT_VIRTUALTABLE
21631 "OMIT_VIRTUALTABLE",
21632#endif
21633#ifdef SQLITE_OMIT_WAL
21634 "OMIT_WAL",
21635#endif
21636#ifdef SQLITE_OMIT_WSD
21637 "OMIT_WSD",
21638#endif
21639#ifdef SQLITE_OMIT_XFER_OPT
21640 "OMIT_XFER_OPT",
21641#endif
21642#ifdef SQLITE_PCACHE_SEPARATE_HEADER
21643 "PCACHE_SEPARATE_HEADER",
21644#endif
21645#ifdef SQLITE_PERFORMANCE_TRACE
21646 "PERFORMANCE_TRACE",
21647#endif
21648#ifdef SQLITE_POWERSAFE_OVERWRITE1
21649# if SQLITE_POWERSAFE_OVERWRITE1 != 1
21650 "POWERSAFE_OVERWRITE=" CTIMEOPT_VAL(SQLITE_POWERSAFE_OVERWRITE)"1",
21651# endif
21652#endif
21653#ifdef SQLITE_PREFER_PROXY_LOCKING
21654 "PREFER_PROXY_LOCKING",
21655#endif
21656#ifdef SQLITE_PROXY_DEBUG
21657 "PROXY_DEBUG",
21658#endif
21659#ifdef SQLITE_REVERSE_UNORDERED_SELECTS
21660 "REVERSE_UNORDERED_SELECTS",
21661#endif
21662#ifdef SQLITE_RTREE_INT_ONLY
21663 "RTREE_INT_ONLY",
21664#endif
21665#ifdef SQLITE_SECURE_DELETE
21666 "SECURE_DELETE",
21667#endif
21668#ifdef SQLITE_SMALL_STACK
21669 "SMALL_STACK",
21670#endif
21671#ifdef SQLITE_SORTER_PMASZ250
21672 "SORTER_PMASZ=" CTIMEOPT_VAL(SQLITE_SORTER_PMASZ)"250",
21673#endif
21674#ifdef SQLITE_SOUNDEX
21675 "SOUNDEX",
21676#endif
21677#ifdef SQLITE_STAT4_SAMPLES1
21678 "STAT4_SAMPLES=" CTIMEOPT_VAL(SQLITE_STAT4_SAMPLES)"1",
21679#endif
21680#ifdef SQLITE_STMTJRNL_SPILL131072
21681 "STMTJRNL_SPILL=" CTIMEOPT_VAL(SQLITE_STMTJRNL_SPILL)"131072",
21682#endif
21683#ifdef SQLITE_SUBSTR_COMPATIBILITY1
21684 "SUBSTR_COMPATIBILITY",
21685#endif
21686#if (!defined(SQLITE_WIN32_MALLOC) \
21687 && !defined(SQLITE_ZERO_MALLOC) \
21688 && !defined(SQLITE_MEMDEBUG) \
21689 ) || defined(SQLITE_SYSTEM_MALLOC1)
21690 "SYSTEM_MALLOC",
21691#endif
21692#ifdef SQLITE_TCL
21693 "TCL",
21694#endif
21695#ifdef SQLITE_TEMP_STORE1
21696 "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE)"1",
21697#endif
21698#ifdef SQLITE_TEST
21699 "TEST",
21700#endif
21701#if defined(SQLITE_THREADSAFE2)
21702 "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE)"2",
21703#elif defined(THREADSAFE)
21704 "THREADSAFE=" CTIMEOPT_VAL(THREADSAFE)"THREADSAFE",
21705#else
21706 "THREADSAFE=1",
21707#endif
21708#ifdef SQLITE_UNLINK_AFTER_CLOSE
21709 "UNLINK_AFTER_CLOSE",
21710#endif
21711#ifdef SQLITE_UNTESTABLE
21712 "UNTESTABLE",
21713#endif
21714#ifdef SQLITE_USER_AUTHENTICATION
21715 "USER_AUTHENTICATION",
21716#endif
21717#ifdef SQLITE_USE_ALLOCA
21718 "USE_ALLOCA",
21719#endif
21720#ifdef SQLITE_USE_FCNTL_TRACE
21721 "USE_FCNTL_TRACE",
21722#endif
21723#ifdef SQLITE_USE_URI1
21724 "USE_URI",
21725#endif
21726#ifdef SQLITE_VDBE_COVERAGE
21727 "VDBE_COVERAGE",
21728#endif
21729#ifdef SQLITE_WIN32_MALLOC
21730 "WIN32_MALLOC",
21731#endif
21732#ifdef SQLITE_ZERO_MALLOC
21733 "ZERO_MALLOC",
21734#endif
21735
21736} ;
21737
21738SQLITE_PRIVATEstatic const char **sqlite3CompileOptions(int *pnOpt){
21739 *pnOpt = sizeof(sqlite3azCompileOpt) / sizeof(sqlite3azCompileOpt[0]);
21740 return (const char**)sqlite3azCompileOpt;
21741}
21742
21743#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
21744
21745/************** End of ctime.c ***********************************************/
21746/************** Begin file global.c ******************************************/
21747/*
21748** 2008 June 13
21749**
21750** The author disclaims copyright to this source code. In place of
21751** a legal notice, here is a blessing:
21752**
21753** May you do good and not evil.
21754** May you find forgiveness for yourself and forgive others.
21755** May you share freely, never taking more than you give.
21756**
21757*************************************************************************
21758**
21759** This file contains definitions of global variables and constants.
21760*/
21761/* #include "sqliteInt.h" */
21762
21763/* An array to map all upper-case characters into their corresponding
21764** lower-case character.
21765**
21766** SQLite only considers US-ASCII (or EBCDIC) characters. We do not
21767** handle case conversions for the UTF character set since the tables
21768** involved are nearly as big or bigger than SQLite itself.
21769*/
21770SQLITE_PRIVATEstatic const unsigned char sqlite3UpperToLower[] = {
21771#ifdef SQLITE_ASCII1
21772 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
21773 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
21774 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
21775 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
21776 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
21777 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
21778 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
21779 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
21780 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
21781 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
21782 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
21783 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
21784 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
21785 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
21786 252,253,254,255,
21787#endif
21788#ifdef SQLITE_EBCDIC
21789 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 0x */
21790 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
21791 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
21792 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
21793 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
21794 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
21795 96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, /* 6x */
21796 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, /* 7x */
21797 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
21798 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, /* 9x */
21799 160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
21800 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
21801 192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
21802 208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
21803 224,225,162,163,164,165,166,167,168,169,234,235,236,237,238,239, /* Ex */
21804 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255, /* Fx */
21805#endif
21806/* All of the upper-to-lower conversion data is above. The following
21807** 18 integers are completely unrelated. They are appended to the
21808** sqlite3UpperToLower[] array to avoid UBSAN warnings. Here's what is
21809** going on:
21810**
21811** The SQL comparison operators (<>, =, >, <=, <, and >=) are implemented
21812** by invoking sqlite3MemCompare(A,B) which compares values A and B and
21813** returns negative, zero, or positive if A is less then, equal to, or
21814** greater than B, respectively. Then the true false results is found by
21815** consulting sqlite3aLTb[opcode], sqlite3aEQb[opcode], or
21816** sqlite3aGTb[opcode] depending on whether the result of compare(A,B)
21817** is negative, zero, or positive, where opcode is the specific opcode.
21818** The only works because the comparison opcodes are consecutive and in
21819** this order: NE EQ GT LE LT GE. Various assert()s throughout the code
21820** ensure that is the case.
21821**
21822** These elements must be appended to another array. Otherwise the
21823** index (here shown as [256-OP_Ne]) would be out-of-bounds and thus
21824** be undefined behavior. That's goofy, but the C-standards people thought
21825** it was a good idea, so here we are.
21826*/
21827/* NE EQ GT LE LT GE */
21828 1, 0, 0, 1, 1, 0, /* aLTb[]: Use when compare(A,B) less than zero */
21829 0, 1, 0, 1, 0, 1, /* aEQb[]: Use when compare(A,B) equals zero */
21830 1, 0, 1, 0, 0, 1 /* aGTb[]: Use when compare(A,B) greater than zero*/
21831};
21832SQLITE_PRIVATEstatic const unsigned char *sqlite3aLTb = &sqlite3UpperToLower[256-OP_Ne52];
21833SQLITE_PRIVATEstatic const unsigned char *sqlite3aEQb = &sqlite3UpperToLower[256+6-OP_Ne52];
21834SQLITE_PRIVATEstatic const unsigned char *sqlite3aGTb = &sqlite3UpperToLower[256+12-OP_Ne52];
21835
21836/*
21837** The following 256 byte lookup table is used to support SQLites built-in
21838** equivalents to the following standard library functions:
21839**
21840** isspace() 0x01
21841** isalpha() 0x02
21842** isdigit() 0x04
21843** isalnum() 0x06
21844** isxdigit() 0x08
21845** toupper() 0x20
21846** SQLite identifier character 0x40
21847** Quote character 0x80
21848**
21849** Bit 0x20 is set if the mapped character requires translation to upper
21850** case. i.e. if the character is a lower-case ASCII character.
21851** If x is a lower-case ASCII character, then its upper-case equivalent
21852** is (x - 0x20). Therefore toupper() can be implemented as:
21853**
21854** (x & ~(map[x]&0x20))
21855**
21856** The equivalent of tolower() is implemented using the sqlite3UpperToLower[]
21857** array. tolower() is used more often than toupper() by SQLite.
21858**
21859** Bit 0x40 is set if the character is non-alphanumeric and can be used in an
21860** SQLite identifier. Identifiers are alphanumerics, "_", "$", and any
21861** non-ASCII UTF character. Hence the test for whether or not a character is
21862** part of an identifier is 0x46.
21863*/
21864SQLITE_PRIVATEstatic const unsigned char sqlite3CtypeMap[256] = {
21865 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00..07 ........ */
21866 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, /* 08..0f ........ */
21867 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10..17 ........ */
21868 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 18..1f ........ */
21869 0x01, 0x00, 0x80, 0x00, 0x40, 0x00, 0x00, 0x80, /* 20..27 !"#$%&' */
21870 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28..2f ()*+,-./ */
21871 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, /* 30..37 01234567 */
21872 0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 38..3f 89:;<=>? */
21873
21874 0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02, /* 40..47 @ABCDEFG */
21875 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 48..4f HIJKLMNO */
21876 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 50..57 PQRSTUVW */
21877 0x02, 0x02, 0x02, 0x80, 0x00, 0x00, 0x00, 0x40, /* 58..5f XYZ[\]^_ */
21878 0x80, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22, /* 60..67 `abcdefg */
21879 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 68..6f hijklmno */
21880 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 70..77 pqrstuvw */
21881 0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, /* 78..7f xyz{|}~. */
21882
21883 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 80..87 ........ */
21884 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 88..8f ........ */
21885 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 90..97 ........ */
21886 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 98..9f ........ */
21887 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* a0..a7 ........ */
21888 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* a8..af ........ */
21889 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* b0..b7 ........ */
21890 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* b8..bf ........ */
21891
21892 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* c0..c7 ........ */
21893 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* c8..cf ........ */
21894 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* d0..d7 ........ */
21895 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* d8..df ........ */
21896 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* e0..e7 ........ */
21897 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* e8..ef ........ */
21898 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* f0..f7 ........ */
21899 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40 /* f8..ff ........ */
21900};
21901
21902/* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards
21903** compatibility for legacy applications, the URI filename capability is
21904** disabled by default.
21905**
21906** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
21907** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
21908**
21909** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
21910** disabled. The default value may be changed by compiling with the
21911** SQLITE_USE_URI symbol defined.
21912*/
21913#ifndef SQLITE_USE_URI1
21914# define SQLITE_USE_URI1 0
21915#endif
21916
21917/* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
21918** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if
21919** that compile-time option is omitted.
21920*/
21921#if !defined(SQLITE_ALLOW_COVERING_INDEX_SCAN1)
21922# define SQLITE_ALLOW_COVERING_INDEX_SCAN1 1
21923#else
21924# if !SQLITE_ALLOW_COVERING_INDEX_SCAN1
21925# error "Compile-time disabling of covering index scan using the\
21926 -DSQLITE_ALLOW_COVERING_INDEX_SCAN=0 option is deprecated.\
21927 Contact SQLite developers if this is a problem for you, and\
21928 delete this #error macro to continue with your build."
21929# endif
21930#endif
21931
21932/* The minimum PMA size is set to this value multiplied by the database
21933** page size in bytes.
21934*/
21935#ifndef SQLITE_SORTER_PMASZ250
21936# define SQLITE_SORTER_PMASZ250 250
21937#endif
21938
21939/* Statement journals spill to disk when their size exceeds the following
21940** threshold (in bytes). 0 means that statement journals are created and
21941** written to disk immediately (the default behavior for SQLite versions
21942** before 3.12.0). -1 means always keep the entire statement journal in
21943** memory. (The statement journal is also always held entirely in memory
21944** if journal_mode=MEMORY or if temp_store=MEMORY, regardless of this
21945** setting.)
21946*/
21947#ifndef SQLITE_STMTJRNL_SPILL131072
21948# define SQLITE_STMTJRNL_SPILL131072 (64*1024)
21949#endif
21950
21951/*
21952** The default lookaside-configuration, the format "SZ,N". SZ is the
21953** number of bytes in each lookaside slot (should be a multiple of 8)
21954** and N is the number of slots. The lookaside-configuration can be
21955** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
21956** or at run-time for an individual database connection using
21957** sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE);
21958**
21959** With the two-size-lookaside enhancement, less lookaside is required.
21960** The default configuration of 1200,40 actually provides 30 1200-byte slots
21961** and 93 128-byte slots, which is more lookaside than is available
21962** using the older 1200,100 configuration without two-size-lookaside.
21963*/
21964#ifndef SQLITE_DEFAULT_LOOKASIDE1200,40
21965# ifdef SQLITE_OMIT_TWOSIZE_LOOKASIDE
21966# define SQLITE_DEFAULT_LOOKASIDE1200,40 1200,100 /* 120KB of memory */
21967# else
21968# define SQLITE_DEFAULT_LOOKASIDE1200,40 1200,40 /* 48KB of memory */
21969# endif
21970#endif
21971
21972
21973/* The default maximum size of an in-memory database created using
21974** sqlite3_deserialize()
21975*/
21976#ifndef SQLITE_MEMDB_DEFAULT_MAXSIZE1073741824
21977# define SQLITE_MEMDB_DEFAULT_MAXSIZE1073741824 1073741824
21978#endif
21979
21980/*
21981** The following singleton contains the global configuration for
21982** the SQLite library.
21983*/
21984SQLITE_PRIVATEstatic SQLITE_WSD struct Sqlite3Config sqlite3Config = {
21985 SQLITE_DEFAULT_MEMSTATUS1, /* bMemstat */
21986 1, /* bCoreMutex */
21987 SQLITE_THREADSAFE2==1, /* bFullMutex */
21988 SQLITE_USE_URI1, /* bOpenUri */
21989 SQLITE_ALLOW_COVERING_INDEX_SCAN1, /* bUseCis */
21990 0, /* bSmallMalloc */
21991 1, /* bExtraSchemaChecks */
21992 0x7ffffffe, /* mxStrlen */
21993 0, /* neverCorrupt */
21994 SQLITE_DEFAULT_LOOKASIDE1200,40, /* szLookaside, nLookaside */
21995 SQLITE_STMTJRNL_SPILL131072, /* nStmtSpill */
21996 {0,0,0,0,0,0,0,0}, /* m */
21997 {0,0,0,0,0,0,0,0,0}, /* mutex */
21998 {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
21999 (void*)0, /* pHeap */
22000 0, /* nHeap */
22001 0, 0, /* mnHeap, mxHeap */
22002 SQLITE_DEFAULT_MMAP_SIZE0, /* szMmap */
22003 SQLITE_MAX_MMAP_SIZE20971520, /* mxMmap */
22004 (void*)0, /* pPage */
22005 0, /* szPage */
22006 SQLITE_DEFAULT_PCACHE_INITSZ20, /* nPage */
22007 0, /* mxParserStack */
22008 0, /* sharedCacheEnabled */
22009 SQLITE_SORTER_PMASZ250, /* szPma */
22010 /* All the rest should always be initialized to zero */
22011 0, /* isInit */
22012 0, /* inProgress */
22013 0, /* isMutexInit */
22014 0, /* isMallocInit */
22015 0, /* isPCacheInit */
22016 0, /* nRefInitMutex */
22017 0, /* pInitMutex */
22018 0, /* xLog */
22019 0, /* pLogArg */
22020#ifdef SQLITE_ENABLE_SQLLOG
22021 0, /* xSqllog */
22022 0, /* pSqllogArg */
22023#endif
22024#ifdef SQLITE_VDBE_COVERAGE
22025 0, /* xVdbeBranch */
22026 0, /* pVbeBranchArg */
22027#endif
22028#ifndef SQLITE_OMIT_DESERIALIZE
22029 SQLITE_MEMDB_DEFAULT_MAXSIZE1073741824, /* mxMemdbSize */
22030#endif
22031#ifndef SQLITE_UNTESTABLE
22032 0, /* xTestCallback */
22033#endif
22034 0, /* bLocaltimeFault */
22035 0, /* xAltLocaltime */
22036 0x7ffffffe, /* iOnceResetThreshold */
22037 SQLITE_DEFAULT_SORTERREF_SIZE0x7fffffff, /* szSorterRef */
22038 0, /* iPrngSeed */
22039#ifdef SQLITE_DEBUG
22040 {0,0,0,0,0,0} /* aTune */
22041#endif
22042};
22043
22044/*
22045** Hash table for global functions - functions common to all
22046** database connections. After initialization, this table is
22047** read-only.
22048*/
22049SQLITE_PRIVATEstatic FuncDefHash sqlite3BuiltinFunctions;
22050
22051#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_DEBUG)
22052/*
22053** Counter used for coverage testing. Does not come into play for
22054** release builds.
22055**
22056** Access to this global variable is not mutex protected. This might
22057** result in TSAN warnings. But as the variable does not exist in
22058** release builds, that should not be a concern.
22059*/
22060SQLITE_PRIVATEstatic unsigned int sqlite3CoverageCounter;
22061#endif /* SQLITE_COVERAGE_TEST || SQLITE_DEBUG */
22062
22063#ifdef VDBE_PROFILE
22064/*
22065** The following performance counter can be used in place of
22066** sqlite3Hwtime() for profiling. This is a no-op on standard builds.
22067*/
22068SQLITE_PRIVATEstatic sqlite3_uint64 sqlite3NProfileCnt = 0;
22069#endif
22070
22071/*
22072** The value of the "pending" byte must be 0x40000000 (1 byte past the
22073** 1-gibabyte boundary) in a compatible database. SQLite never uses
22074** the database page that contains the pending byte. It never attempts
22075** to read or write that page. The pending byte page is set aside
22076** for use by the VFS layers as space for managing file locks.
22077**
22078** During testing, it is often desirable to move the pending byte to
22079** a different position in the file. This allows code that has to
22080** deal with the pending byte to run on files that are much smaller
22081** than 1 GiB. The sqlite3_test_control() interface can be used to
22082** move the pending byte.
22083**
22084** IMPORTANT: Changing the pending byte to any value other than
22085** 0x40000000 results in an incompatible database file format!
22086** Changing the pending byte during operation will result in undefined
22087** and incorrect behavior.
22088*/
22089#ifndef SQLITE_OMIT_WSD
22090SQLITE_PRIVATEstatic int sqlite3PendingByte = 0x40000000;
22091#endif
22092
22093/*
22094** Tracing flags set by SQLITE_TESTCTRL_TRACEFLAGS.
22095*/
22096SQLITE_PRIVATEstatic u32 sqlite3TreeTrace = 0;
22097SQLITE_PRIVATEstatic u32 sqlite3WhereTrace = 0;
22098
22099/* #include "opcodes.h" */
22100/*
22101** Properties of opcodes. The OPFLG_INITIALIZER macro is
22102** created by mkopcodeh.awk during compilation. Data is obtained
22103** from the comments following the "case OP_xxxx:" statements in
22104** the vdbe.c file.
22105*/
22106SQLITE_PRIVATEstatic const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER{ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00, 0x01, 0x01,
0x01, 0x03, 0x03, 0x01, 0x01, 0x03, 0x03, 0x03, 0x01, 0x12, 0x09
, 0x09, 0x09, 0x09, 0x01, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
, 0x01, 0x26, 0x26, 0x23, 0x0b, 0x01, 0x01, 0x03, 0x03, 0x03,
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x01, 0x03, 0x03, 0x01, 0x01
, 0x01, 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,
0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x26, 0x26, 0x26
, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00, 0x00, 0x12,
0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
0x00, 0x00, 0x04, 0x04, 0x00, 0x00, 0x10, 0x00, 0x10, 0x00, 0x00
, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x06,
0x10, 0x00, 0x04, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x10,
0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,}
;
22107
22108/*
22109** Name of the default collating sequence
22110*/
22111SQLITE_PRIVATEstatic const char sqlite3StrBINARY[] = "BINARY";
22112
22113/*
22114** Standard typenames. These names must match the COLTYPE_* definitions.
22115** Adjust the SQLITE_N_STDTYPE value if adding or removing entries.
22116**
22117** sqlite3StdType[] The actual names of the datatypes.
22118**
22119** sqlite3StdTypeLen[] The length (in bytes) of each entry
22120** in sqlite3StdType[].
22121**
22122** sqlite3StdTypeAffinity[] The affinity associated with each entry
22123** in sqlite3StdType[].
22124**
22125** sqlite3StdTypeMap[] The type value (as returned from
22126** sqlite3_column_type() or sqlite3_value_type())
22127** for each entry in sqlite3StdType[].
22128*/
22129SQLITE_PRIVATEstatic const unsigned char sqlite3StdTypeLen[] = { 3, 4, 3, 7, 4, 4 };
22130SQLITE_PRIVATEstatic const char sqlite3StdTypeAffinity[] = {
22131 SQLITE_AFF_NUMERIC0x43,
22132 SQLITE_AFF_BLOB0x41,
22133 SQLITE_AFF_INTEGER0x44,
22134 SQLITE_AFF_INTEGER0x44,
22135 SQLITE_AFF_REAL0x45,
22136 SQLITE_AFF_TEXT0x42
22137};
22138SQLITE_PRIVATEstatic const char sqlite3StdTypeMap[] = {
22139 0,
22140 SQLITE_BLOB4,
22141 SQLITE_INTEGER1,
22142 SQLITE_INTEGER1,
22143 SQLITE_FLOAT2,
22144 SQLITE_TEXT3
22145};
22146SQLITE_PRIVATEstatic const char *sqlite3StdType[] = {
22147 "ANY",
22148 "BLOB",
22149 "INT",
22150 "INTEGER",
22151 "REAL",
22152 "TEXT"
22153};
22154
22155/************** End of global.c **********************************************/
22156/************** Begin file status.c ******************************************/
22157/*
22158** 2008 June 18
22159**
22160** The author disclaims copyright to this source code. In place of
22161** a legal notice, here is a blessing:
22162**
22163** May you do good and not evil.
22164** May you find forgiveness for yourself and forgive others.
22165** May you share freely, never taking more than you give.
22166**
22167*************************************************************************
22168**
22169** This module implements the sqlite3_status() interface and related
22170** functionality.
22171*/
22172/* #include "sqliteInt.h" */
22173/************** Include vdbeInt.h in the middle of status.c ******************/
22174/************** Begin file vdbeInt.h *****************************************/
22175/*
22176** 2003 September 6
22177**
22178** The author disclaims copyright to this source code. In place of
22179** a legal notice, here is a blessing:
22180**
22181** May you do good and not evil.
22182** May you find forgiveness for yourself and forgive others.
22183** May you share freely, never taking more than you give.
22184**
22185*************************************************************************
22186** This is the header file for information that is private to the
22187** VDBE. This information used to all be at the top of the single
22188** source code file "vdbe.c". When that file became too big (over
22189** 6000 lines long) it was split up into several smaller files and
22190** this header information was factored out.
22191*/
22192#ifndef SQLITE_VDBEINT_H
22193#define SQLITE_VDBEINT_H
22194
22195/*
22196** The maximum number of times that a statement will try to reparse
22197** itself before giving up and returning SQLITE_SCHEMA.
22198*/
22199#ifndef SQLITE_MAX_SCHEMA_RETRY50
22200# define SQLITE_MAX_SCHEMA_RETRY50 50
22201#endif
22202
22203/*
22204** VDBE_DISPLAY_P4 is true or false depending on whether or not the
22205** "explain" P4 display logic is enabled.
22206*/
22207#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG1) \
22208 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) \
22209 || defined(SQLITE_ENABLE_BYTECODE_VTAB)
22210# define VDBE_DISPLAY_P41 1
22211#else
22212# define VDBE_DISPLAY_P41 0
22213#endif
22214
22215/*
22216** SQL is translated into a sequence of instructions to be
22217** executed by a virtual machine. Each instruction is an instance
22218** of the following structure.
22219*/
22220typedef struct VdbeOp Op;
22221
22222/*
22223** Boolean values
22224*/
22225typedef unsigned Bool;
22226
22227/* Opaque type used by code in vdbesort.c */
22228typedef struct VdbeSorter VdbeSorter;
22229
22230/* Elements of the linked list at Vdbe.pAuxData */
22231typedef struct AuxData AuxData;
22232
22233/* Types of VDBE cursors */
22234#define CURTYPE_BTREE0 0
22235#define CURTYPE_SORTER1 1
22236#define CURTYPE_VTAB2 2
22237#define CURTYPE_PSEUDO3 3
22238
22239/*
22240** A VdbeCursor is an superclass (a wrapper) for various cursor objects:
22241**
22242** * A b-tree cursor
22243** - In the main database or in an ephemeral database
22244** - On either an index or a table
22245** * A sorter
22246** * A virtual table
22247** * A one-row "pseudotable" stored in a single register
22248*/
22249typedef struct VdbeCursor VdbeCursor;
22250struct VdbeCursor {
22251 u8 eCurType; /* One of the CURTYPE_* values above */
22252 i8 iDb; /* Index of cursor database in db->aDb[] */
22253 u8 nullRow; /* True if pointing to a row with no data */
22254 u8 deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */
22255 u8 isTable; /* True for rowid tables. False for indexes */
22256#ifdef SQLITE_DEBUG
22257 u8 seekOp; /* Most recent seek operation on this cursor */
22258 u8 wrFlag; /* The wrFlag argument to sqlite3BtreeCursor() */
22259#endif
22260 Bool isEphemeral:1; /* True for an ephemeral table */
22261 Bool useRandomRowid:1; /* Generate new record numbers semi-randomly */
22262 Bool isOrdered:1; /* True if the table is not BTREE_UNORDERED */
22263 Bool noReuse:1; /* OpenEphemeral may not reuse this cursor */
22264 u16 seekHit; /* See the OP_SeekHit and OP_IfNoHope opcodes */
22265 union { /* pBtx for isEphermeral. pAltMap otherwise */
22266 Btree *pBtx; /* Separate file holding temporary table */
22267 u32 *aAltMap; /* Mapping from table to index column numbers */
22268 } ub;
22269 i64 seqCount; /* Sequence counter */
22270
22271 /* Cached OP_Column parse information is only valid if cacheStatus matches
22272 ** Vdbe.cacheCtr. Vdbe.cacheCtr will never take on the value of
22273 ** CACHE_STALE (0) and so setting cacheStatus=CACHE_STALE guarantees that
22274 ** the cache is out of date. */
22275 u32 cacheStatus; /* Cache is valid if this matches Vdbe.cacheCtr */
22276 int seekResult; /* Result of previous sqlite3BtreeMoveto() or 0
22277 ** if there have been no prior seeks on the cursor. */
22278 /* seekResult does not distinguish between "no seeks have ever occurred
22279 ** on this cursor" and "the most recent seek was an exact match".
22280 ** For CURTYPE_PSEUDO, seekResult is the register holding the record */
22281
22282 /* When a new VdbeCursor is allocated, only the fields above are zeroed.
22283 ** The fields that follow are uninitialized, and must be individually
22284 ** initialized prior to first use. */
22285 VdbeCursor *pAltCursor; /* Associated index cursor from which to read */
22286 union {
22287 BtCursor *pCursor; /* CURTYPE_BTREE or _PSEUDO. Btree cursor */
22288 sqlite3_vtab_cursor *pVCur; /* CURTYPE_VTAB. Vtab cursor */
22289 VdbeSorter *pSorter; /* CURTYPE_SORTER. Sorter object */
22290 } uc;
22291 KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */
22292 u32 iHdrOffset; /* Offset to next unparsed byte of the header */
22293 Pgno pgnoRoot; /* Root page of the open btree cursor */
22294 i16 nField; /* Number of fields in the header */
22295 u16 nHdrParsed; /* Number of header fields parsed so far */
22296 i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
22297 u32 *aOffset; /* Pointer to aType[nField] */
22298 const u8 *aRow; /* Data for the current row, if all on one page */
22299 u32 payloadSize; /* Total number of bytes in the record */
22300 u32 szRow; /* Byte available in aRow */
22301#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
22302 u64 maskUsed; /* Mask of columns used by this cursor */
22303#endif
22304
22305 /* 2*nField extra array elements allocated for aType[], beyond the one
22306 ** static element declared in the structure. nField total array slots for
22307 ** aType[] and nField+1 array slots for aOffset[] */
22308 u32 aType[1]; /* Type values record decode. MUST BE LAST */
22309};
22310
22311/* Return true if P is a null-only cursor
22312*/
22313#define IsNullCursor(P)((P)->eCurType==3 && (P)->nullRow && (P
)->seekResult==0)
\
22314 ((P)->eCurType==CURTYPE_PSEUDO3 && (P)->nullRow && (P)->seekResult==0)
22315
22316
22317/*
22318** A value for VdbeCursor.cacheStatus that means the cache is always invalid.
22319*/
22320#define CACHE_STALE0 0
22321
22322/*
22323** When a sub-program is executed (OP_Program), a structure of this type
22324** is allocated to store the current value of the program counter, as
22325** well as the current memory cell array and various other frame specific
22326** values stored in the Vdbe struct. When the sub-program is finished,
22327** these values are copied back to the Vdbe from the VdbeFrame structure,
22328** restoring the state of the VM to as it was before the sub-program
22329** began executing.
22330**
22331** The memory for a VdbeFrame object is allocated and managed by a memory
22332** cell in the parent (calling) frame. When the memory cell is deleted or
22333** overwritten, the VdbeFrame object is not freed immediately. Instead, it
22334** is linked into the Vdbe.pDelFrame list. The contents of the Vdbe.pDelFrame
22335** list is deleted when the VM is reset in VdbeHalt(). The reason for doing
22336** this instead of deleting the VdbeFrame immediately is to avoid recursive
22337** calls to sqlite3VdbeMemRelease() when the memory cells belonging to the
22338** child frame are released.
22339**
22340** The currently executing frame is stored in Vdbe.pFrame. Vdbe.pFrame is
22341** set to NULL if the currently executing frame is the main program.
22342*/
22343typedef struct VdbeFrame VdbeFrame;
22344struct VdbeFrame {
22345 Vdbe *v; /* VM this frame belongs to */
22346 VdbeFrame *pParent; /* Parent of this frame, or NULL if parent is main */
22347 Op *aOp; /* Program instructions for parent frame */
22348 i64 *anExec; /* Event counters from parent frame */
22349 Mem *aMem; /* Array of memory cells for parent frame */
22350 VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */
22351 u8 *aOnce; /* Bitmask used by OP_Once */
22352 void *token; /* Copy of SubProgram.token */
22353 i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */
22354 AuxData *pAuxData; /* Linked list of auxdata allocations */
22355#if SQLITE_DEBUG
22356 u32 iFrameMagic; /* magic number for sanity checking */
22357#endif
22358 int nCursor; /* Number of entries in apCsr */
22359 int pc; /* Program Counter in parent (calling) frame */
22360 int nOp; /* Size of aOp array */
22361 int nMem; /* Number of entries in aMem */
22362 int nChildMem; /* Number of memory cells for child frame */
22363 int nChildCsr; /* Number of cursors for child frame */
22364 i64 nChange; /* Statement changes (Vdbe.nChange) */
22365 i64 nDbChange; /* Value of db->nChange */
22366};
22367
22368/* Magic number for sanity checking on VdbeFrame objects */
22369#define SQLITE_FRAME_MAGIC0x879fb71e 0x879fb71e
22370
22371/*
22372** Return a pointer to the array of registers allocated for use
22373** by a VdbeFrame.
22374*/
22375#define VdbeFrameMem(p)((Mem *)&((u8 *)p)[(((sizeof(VdbeFrame))+7)&~7)]) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))(((sizeof(VdbeFrame))+7)&~7)])
22376
22377/*
22378** Internally, the vdbe manipulates nearly all SQL values as Mem
22379** structures. Each Mem struct may cache multiple representations (string,
22380** integer etc.) of the same value.
22381*/
22382struct sqlite3_value {
22383 union MemValue {
22384 double r; /* Real value used when MEM_Real is set in flags */
22385 i64 i; /* Integer value used when MEM_Int is set in flags */
22386 int nZero; /* Extra zero bytes when MEM_Zero and MEM_Blob set */
22387 const char *zPType; /* Pointer type when MEM_Term|MEM_Subtype|MEM_Null */
22388 FuncDef *pDef; /* Used only when flags==MEM_Agg */
22389 } u;
22390 char *z; /* String or BLOB value */
22391 int n; /* Number of characters in string value, excluding '\0' */
22392 u16 flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
22393 u8 enc; /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
22394 u8 eSubtype; /* Subtype for this value */
22395 /* ShallowCopy only needs to copy the information above */
22396 sqlite3 *db; /* The associated database connection */
22397 int szMalloc; /* Size of the zMalloc allocation */
22398 u32 uTemp; /* Transient storage for serial_type in OP_MakeRecord */
22399 char *zMalloc; /* Space to hold MEM_Str or MEM_Blob if szMalloc>0 */
22400 void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
22401#ifdef SQLITE_DEBUG
22402 Mem *pScopyFrom; /* This Mem is a shallow copy of pScopyFrom */
22403 u16 mScopyFlags; /* flags value immediately after the shallow copy */
22404#endif
22405};
22406
22407/*
22408** Size of struct Mem not including the Mem.zMalloc member or anything that
22409** follows.
22410*/
22411#define MEMCELLSIZE__builtin_offsetof(Mem, db) offsetof(Mem,db)__builtin_offsetof(Mem, db)
22412
22413/* One or more of the following flags are set to indicate the
22414** representations of the value stored in the Mem struct.
22415**
22416** * MEM_Null An SQL NULL value
22417**
22418** * MEM_Null|MEM_Zero An SQL NULL with the virtual table
22419** UPDATE no-change flag set
22420**
22421** * MEM_Null|MEM_Term| An SQL NULL, but also contains a
22422** MEM_Subtype pointer accessible using
22423** sqlite3_value_pointer().
22424**
22425** * MEM_Null|MEM_Cleared Special SQL NULL that compares non-equal
22426** to other NULLs even using the IS operator.
22427**
22428** * MEM_Str A string, stored in Mem.z with
22429** length Mem.n. Zero-terminated if
22430** MEM_Term is set. This flag is
22431** incompatible with MEM_Blob and
22432** MEM_Null, but can appear with MEM_Int,
22433** MEM_Real, and MEM_IntReal.
22434**
22435** * MEM_Blob A blob, stored in Mem.z length Mem.n.
22436** Incompatible with MEM_Str, MEM_Null,
22437** MEM_Int, MEM_Real, and MEM_IntReal.
22438**
22439** * MEM_Blob|MEM_Zero A blob in Mem.z of length Mem.n plus
22440** MEM.u.i extra 0x00 bytes at the end.
22441**
22442** * MEM_Int Integer stored in Mem.u.i.
22443**
22444** * MEM_Real Real stored in Mem.u.r.
22445**
22446** * MEM_IntReal Real stored as an integer in Mem.u.i.
22447**
22448** If the MEM_Null flag is set, then the value is an SQL NULL value.
22449** For a pointer type created using sqlite3_bind_pointer() or
22450** sqlite3_result_pointer() the MEM_Term and MEM_Subtype flags are also set.
22451**
22452** If the MEM_Str flag is set then Mem.z points at a string representation.
22453** Usually this is encoded in the same unicode encoding as the main
22454** database (see below for exceptions). If the MEM_Term flag is also
22455** set, then the string is nul terminated. The MEM_Int and MEM_Real
22456** flags may coexist with the MEM_Str flag.
22457*/
22458#define MEM_Undefined0x0000 0x0000 /* Value is undefined */
22459#define MEM_Null0x0001 0x0001 /* Value is NULL (or a pointer) */
22460#define MEM_Str0x0002 0x0002 /* Value is a string */
22461#define MEM_Int0x0004 0x0004 /* Value is an integer */
22462#define MEM_Real0x0008 0x0008 /* Value is a real number */
22463#define MEM_Blob0x0010 0x0010 /* Value is a BLOB */
22464#define MEM_IntReal0x0020 0x0020 /* MEM_Int that stringifies like MEM_Real */
22465#define MEM_AffMask0x003f 0x003f /* Mask of affinity bits */
22466
22467/* Extra bits that modify the meanings of the core datatypes above
22468*/
22469#define MEM_FromBind0x0040 0x0040 /* Value originates from sqlite3_bind() */
22470 /* 0x0080 // Available */
22471#define MEM_Cleared0x0100 0x0100 /* NULL set by OP_Null, not from data */
22472#define MEM_Term0x0200 0x0200 /* String in Mem.z is zero terminated */
22473#define MEM_Zero0x0400 0x0400 /* Mem.i contains count of 0s appended to blob */
22474#define MEM_Subtype0x0800 0x0800 /* Mem.eSubtype is valid */
22475#define MEM_TypeMask0x0dbf 0x0dbf /* Mask of type bits */
22476
22477/* Bits that determine the storage for Mem.z for a string or blob or
22478** aggregate accumulator.
22479*/
22480#define MEM_Dyn0x1000 0x1000 /* Need to call Mem.xDel() on Mem.z */
22481#define MEM_Static0x2000 0x2000 /* Mem.z points to a static string */
22482#define MEM_Ephem0x4000 0x4000 /* Mem.z points to an ephemeral string */
22483#define MEM_Agg0x8000 0x8000 /* Mem.z points to an agg function context */
22484
22485/* Return TRUE if Mem X contains dynamically allocated content - anything
22486** that needs to be deallocated to avoid a leak.
22487*/
22488#define VdbeMemDynamic(X)(((X)->flags&(0x8000|0x1000))!=0) \
22489 (((X)->flags&(MEM_Agg0x8000|MEM_Dyn0x1000))!=0)
22490
22491/*
22492** Clear any existing type flags from a Mem and replace them with f
22493*/
22494#define MemSetTypeFlag(p, f)((p)->flags = ((p)->flags&~(0x0dbf|0x0400))|f) \
22495 ((p)->flags = ((p)->flags&~(MEM_TypeMask0x0dbf|MEM_Zero0x0400))|f)
22496
22497/*
22498** True if Mem X is a NULL-nochng type.
22499*/
22500#define MemNullNochng(X)(((X)->flags&0x0dbf)==(0x0001|0x0400) && (X)->
n==0 && (X)->u.nZero==0)
\
22501 (((X)->flags&MEM_TypeMask0x0dbf)==(MEM_Null0x0001|MEM_Zero0x0400) \
22502 && (X)->n==0 && (X)->u.nZero==0)
22503
22504/*
22505** Return true if a memory cell has been initialized and is valid.
22506** is for use inside assert() statements only.
22507**
22508** A Memory cell is initialized if at least one of the
22509** MEM_Null, MEM_Str, MEM_Int, MEM_Real, MEM_Blob, or MEM_IntReal bits
22510** is set. It is "undefined" if all those bits are zero.
22511*/
22512#ifdef SQLITE_DEBUG
22513#define memIsValid(M) ((M)->flags & MEM_AffMask0x003f)!=0
22514#endif
22515
22516/*
22517** Each auxiliary data pointer stored by a user defined function
22518** implementation calling sqlite3_set_auxdata() is stored in an instance
22519** of this structure. All such structures associated with a single VM
22520** are stored in a linked list headed at Vdbe.pAuxData. All are destroyed
22521** when the VM is halted (if not before).
22522*/
22523struct AuxData {
22524 int iAuxOp; /* Instruction number of OP_Function opcode */
22525 int iAuxArg; /* Index of function argument. */
22526 void *pAux; /* Aux data pointer */
22527 void (*xDeleteAux)(void*); /* Destructor for the aux data */
22528 AuxData *pNextAux; /* Next element in list */
22529};
22530
22531/*
22532** The "context" argument for an installable function. A pointer to an
22533** instance of this structure is the first argument to the routines used
22534** implement the SQL functions.
22535**
22536** There is a typedef for this structure in sqlite.h. So all routines,
22537** even the public interface to SQLite, can use a pointer to this structure.
22538** But this file is the only place where the internal details of this
22539** structure are known.
22540**
22541** This structure is defined inside of vdbeInt.h because it uses substructures
22542** (Mem) which are only defined there.
22543*/
22544struct sqlite3_context {
22545 Mem *pOut; /* The return value is stored here */
22546 FuncDef *pFunc; /* Pointer to function information */
22547 Mem *pMem; /* Memory cell used to store aggregate context */
22548 Vdbe *pVdbe; /* The VM that owns this context */
22549 int iOp; /* Instruction number of OP_Function */
22550 int isError; /* Error code returned by the function. */
22551 u8 enc; /* Encoding to use for results */
22552 u8 skipFlag; /* Skip accumulator loading if true */
22553 u8 argc; /* Number of arguments */
22554 sqlite3_value *argv[1]; /* Argument set */
22555};
22556
22557/* A bitfield type for use inside of structures. Always follow with :N where
22558** N is the number of bits.
22559*/
22560typedef unsigned bft; /* Bit Field Type */
22561
22562/* The ScanStatus object holds a single value for the
22563** sqlite3_stmt_scanstatus() interface.
22564*/
22565typedef struct ScanStatus ScanStatus;
22566struct ScanStatus {
22567 int addrExplain; /* OP_Explain for loop */
22568 int addrLoop; /* Address of "loops" counter */
22569 int addrVisit; /* Address of "rows visited" counter */
22570 int iSelectID; /* The "Select-ID" for this loop */
22571 LogEst nEst; /* Estimated output rows per loop */
22572 char *zName; /* Name of table or index */
22573};
22574
22575/* The DblquoteStr object holds the text of a double-quoted
22576** string for a prepared statement. A linked list of these objects
22577** is constructed during statement parsing and is held on Vdbe.pDblStr.
22578** When computing a normalized SQL statement for an SQL statement, that
22579** list is consulted for each double-quoted identifier to see if the
22580** identifier should really be a string literal.
22581*/
22582typedef struct DblquoteStr DblquoteStr;
22583struct DblquoteStr {
22584 DblquoteStr *pNextStr; /* Next string literal in the list */
22585 char z[8]; /* Dequoted value for the string */
22586};
22587
22588/*
22589** An instance of the virtual machine. This structure contains the complete
22590** state of the virtual machine.
22591**
22592** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
22593** is really a pointer to an instance of this structure.
22594*/
22595struct Vdbe {
22596 sqlite3 *db; /* The database connection that owns this statement */
22597 Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */
22598 Parse *pParse; /* Parsing context used to create this Vdbe */
22599 ynVar nVar; /* Number of entries in aVar[] */
22600 int nMem; /* Number of memory locations currently allocated */
22601 int nCursor; /* Number of slots in apCsr[] */
22602 u32 cacheCtr; /* VdbeCursor row cache generation counter */
22603 int pc; /* The program counter */
22604 int rc; /* Value to return */
22605 i64 nChange; /* Number of db changes made since last reset */
22606 int iStatement; /* Statement number (or 0 if has no opened stmt) */
22607 i64 iCurrentTime; /* Value of julianday('now') for this statement */
22608 i64 nFkConstraint; /* Number of imm. FK constraints this VM */
22609 i64 nStmtDefCons; /* Number of def. constraints when stmt started */
22610 i64 nStmtDefImmCons; /* Number of def. imm constraints when stmt started */
22611 Mem *aMem; /* The memory locations */
22612 Mem **apArg; /* Arguments to currently executing user function */
22613 VdbeCursor **apCsr; /* One element of this array for each open cursor */
22614 Mem *aVar; /* Values for the OP_Variable opcode. */
22615
22616 /* When allocating a new Vdbe object, all of the fields below should be
22617 ** initialized to zero or NULL */
22618
22619 Op *aOp; /* Space to hold the virtual machine's program */
22620 int nOp; /* Number of instructions in the program */
22621 int nOpAlloc; /* Slots allocated for aOp[] */
22622 Mem *aColName; /* Column names to return */
22623 Mem *pResultSet; /* Pointer to an array of results */
22624 char *zErrMsg; /* Error message written here */
22625 VList *pVList; /* Name of variables */
22626#ifndef SQLITE_OMIT_TRACE
22627 i64 startTime; /* Time when query started - used for profiling */
22628#endif
22629#ifdef SQLITE_DEBUG
22630 int rcApp; /* errcode set by sqlite3_result_error_code() */
22631 u32 nWrite; /* Number of write operations that have occurred */
22632#endif
22633 u16 nResColumn; /* Number of columns in one row of the result set */
22634 u8 errorAction; /* Recovery action to do in case of an error */
22635 u8 minWriteFileFormat; /* Minimum file format for writable database files */
22636 u8 prepFlags; /* SQLITE_PREPARE_* flags */
22637 u8 eVdbeState; /* On of the VDBE_*_STATE values */
22638 bft expired:2; /* 1: recompile VM immediately 2: when convenient */
22639 bft explain:2; /* True if EXPLAIN present on SQL command */
22640 bft changeCntOn:1; /* True to update the change-counter */
22641 bft usesStmtJournal:1; /* True if uses a statement journal */
22642 bft readOnly:1; /* True for statements that do not write */
22643 bft bIsReader:1; /* True for statements that read */
22644 yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
22645 yDbMask lockMask; /* Subset of btreeMask that requires a lock */
22646 u32 aCounter[9]; /* Counters used by sqlite3_stmt_status() */
22647 char *zSql; /* Text of the SQL statement that generated this */
22648#ifdef SQLITE_ENABLE_NORMALIZE
22649 char *zNormSql; /* Normalization of the associated SQL statement */
22650 DblquoteStr *pDblStr; /* List of double-quoted string literals */
22651#endif
22652 void *pFree; /* Free this when deleting the vdbe */
22653 VdbeFrame *pFrame; /* Parent frame */
22654 VdbeFrame *pDelFrame; /* List of frame objects to free on VM reset */
22655 int nFrame; /* Number of frames in pFrame list */
22656 u32 expmask; /* Binding to these vars invalidates VM */
22657 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
22658 AuxData *pAuxData; /* Linked list of auxdata allocations */
22659#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
22660 i64 *anExec; /* Number of times each op has been executed */
22661 int nScan; /* Entries in aScan[] */
22662 ScanStatus *aScan; /* Scan definitions for sqlite3_stmt_scanstatus() */
22663#endif
22664};
22665
22666/*
22667** The following are allowed values for Vdbe.eVdbeState
22668*/
22669#define VDBE_INIT_STATE0 0 /* Prepared statement under construction */
22670#define VDBE_READY_STATE1 1 /* Ready to run but not yet started */
22671#define VDBE_RUN_STATE2 2 /* Run in progress */
22672#define VDBE_HALT_STATE3 3 /* Finished. Need reset() or finalize() */
22673
22674/*
22675** Structure used to store the context required by the
22676** sqlite3_preupdate_*() API functions.
22677*/
22678struct PreUpdate {
22679 Vdbe *v;
22680 VdbeCursor *pCsr; /* Cursor to read old values from */
22681 int op; /* One of SQLITE_INSERT, UPDATE, DELETE */
22682 u8 *aRecord; /* old.* database record */
22683 KeyInfo keyinfo;
22684 UnpackedRecord *pUnpacked; /* Unpacked version of aRecord[] */
22685 UnpackedRecord *pNewUnpacked; /* Unpacked version of new.* record */
22686 int iNewReg; /* Register for new.* values */
22687 int iBlobWrite; /* Value returned by preupdate_blobwrite() */
22688 i64 iKey1; /* First key value passed to hook */
22689 i64 iKey2; /* Second key value passed to hook */
22690 Mem *aNew; /* Array of new.* values */
22691 Table *pTab; /* Schema object being upated */
22692 Index *pPk; /* PK index if pTab is WITHOUT ROWID */
22693};
22694
22695/*
22696** An instance of this object is used to pass an vector of values into
22697** OP_VFilter, the xFilter method of a virtual table. The vector is the
22698** set of values on the right-hand side of an IN constraint.
22699**
22700** The value as passed into xFilter is an sqlite3_value with a "pointer"
22701** type, such as is generated by sqlite3_result_pointer() and read by
22702** sqlite3_value_pointer. Such values have MEM_Term|MEM_Subtype|MEM_Null
22703** and a subtype of 'p'. The sqlite3_vtab_in_first() and _next() interfaces
22704** know how to use this object to step through all the values in the
22705** right operand of the IN constraint.
22706*/
22707typedef struct ValueList ValueList;
22708struct ValueList {
22709 BtCursor *pCsr; /* An ephemeral table holding all values */
22710 sqlite3_value *pOut; /* Register to hold each decoded output value */
22711};
22712
22713/* Size of content associated with serial types that fit into a
22714** single-byte varint.
22715*/
22716#ifndef SQLITE_AMALGAMATION1
22717SQLITE_PRIVATEstatic const u8 sqlite3SmallTypeSizes[];
22718#endif
22719
22720/*
22721** Function prototypes
22722*/
22723SQLITE_PRIVATEstatic void sqlite3VdbeError(Vdbe*, const char *, ...);
22724SQLITE_PRIVATEstatic void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
22725SQLITE_PRIVATEstatic void sqlite3VdbeFreeCursorNN(Vdbe*,VdbeCursor*);
22726void sqliteVdbePopStack(Vdbe*,int);
22727SQLITE_PRIVATEstatic int SQLITE_NOINLINE__attribute__((noinline)) sqlite3VdbeHandleMovedCursor(VdbeCursor *p);
22728SQLITE_PRIVATEstatic int SQLITE_NOINLINE__attribute__((noinline)) sqlite3VdbeFinishMoveto(VdbeCursor*);
22729SQLITE_PRIVATEstatic int sqlite3VdbeCursorRestore(VdbeCursor*);
22730SQLITE_PRIVATEstatic u32 sqlite3VdbeSerialTypeLen(u32);
22731SQLITE_PRIVATEstatic u8 sqlite3VdbeOneByteSerialTypeLen(u8);
22732#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
22733SQLITE_PRIVATEstatic u64 sqlite3FloatSwap(u64 in);
22734# define swapMixedEndianFloat(X) X = sqlite3FloatSwap(X)
22735#else
22736# define swapMixedEndianFloat(X)
22737#endif
22738SQLITE_PRIVATEstatic void sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
22739SQLITE_PRIVATEstatic void sqlite3VdbeDeleteAuxData(sqlite3*, AuxData**, int, int);
22740
22741int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
22742SQLITE_PRIVATEstatic int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*);
22743SQLITE_PRIVATEstatic int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*);
22744SQLITE_PRIVATEstatic int sqlite3VdbeExec(Vdbe*);
22745#if !defined(SQLITE_OMIT_EXPLAIN) || defined(SQLITE_ENABLE_BYTECODE_VTAB)
22746SQLITE_PRIVATEstatic int sqlite3VdbeNextOpcode(Vdbe*,Mem*,int,int*,int*,Op**);
22747SQLITE_PRIVATEstatic char *sqlite3VdbeDisplayP4(sqlite3*,Op*);
22748#endif
22749#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
22750SQLITE_PRIVATEstatic char *sqlite3VdbeDisplayComment(sqlite3*,const Op*,const char*);
22751#endif
22752#if !defined(SQLITE_OMIT_EXPLAIN)
22753SQLITE_PRIVATEstatic int sqlite3VdbeList(Vdbe*);
22754#endif
22755SQLITE_PRIVATEstatic int sqlite3VdbeHalt(Vdbe*);
22756SQLITE_PRIVATEstatic int sqlite3VdbeChangeEncoding(Mem *, int);
22757SQLITE_PRIVATEstatic int sqlite3VdbeMemTooBig(Mem*);
22758SQLITE_PRIVATEstatic int sqlite3VdbeMemCopy(Mem*, const Mem*);
22759SQLITE_PRIVATEstatic void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
22760SQLITE_PRIVATEstatic void sqlite3VdbeMemMove(Mem*, Mem*);
22761SQLITE_PRIVATEstatic int sqlite3VdbeMemNulTerminate(Mem*);
22762SQLITE_PRIVATEstatic int sqlite3VdbeMemSetStr(Mem*, const char*, i64, u8, void(*)(void*));
22763SQLITE_PRIVATEstatic void sqlite3VdbeMemSetInt64(Mem*, i64);
22764#ifdef SQLITE_OMIT_FLOATING_POINT
22765# define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
22766#else
22767SQLITE_PRIVATEstatic void sqlite3VdbeMemSetDouble(Mem*, double);
22768#endif
22769SQLITE_PRIVATEstatic void sqlite3VdbeMemSetPointer(Mem*, void*, const char*, void(*)(void*));
22770SQLITE_PRIVATEstatic void sqlite3VdbeMemInit(Mem*,sqlite3*,u16);
22771SQLITE_PRIVATEstatic void sqlite3VdbeMemSetNull(Mem*);
22772#ifndef SQLITE_OMIT_INCRBLOB
22773SQLITE_PRIVATEstatic void sqlite3VdbeMemSetZeroBlob(Mem*,int);
22774#else
22775SQLITE_PRIVATEstatic int sqlite3VdbeMemSetZeroBlob(Mem*,int);
22776#endif
22777#ifdef SQLITE_DEBUG
22778SQLITE_PRIVATEstatic int sqlite3VdbeMemIsRowSet(const Mem*);
22779#endif
22780SQLITE_PRIVATEstatic int sqlite3VdbeMemSetRowSet(Mem*);
22781SQLITE_PRIVATEstatic int sqlite3VdbeMemMakeWriteable(Mem*);
22782SQLITE_PRIVATEstatic int sqlite3VdbeMemStringify(Mem*, u8, u8);
22783SQLITE_PRIVATEstatic int sqlite3IntFloatCompare(i64,double);
22784SQLITE_PRIVATEstatic i64 sqlite3VdbeIntValue(const Mem*);
22785SQLITE_PRIVATEstatic int sqlite3VdbeMemIntegerify(Mem*);
22786SQLITE_PRIVATEstatic double sqlite3VdbeRealValue(Mem*);
22787SQLITE_PRIVATEstatic int sqlite3VdbeBooleanValue(Mem*, int ifNull);
22788SQLITE_PRIVATEstatic void sqlite3VdbeIntegerAffinity(Mem*);
22789SQLITE_PRIVATEstatic int sqlite3VdbeMemRealify(Mem*);
22790SQLITE_PRIVATEstatic int sqlite3VdbeMemNumerify(Mem*);
22791SQLITE_PRIVATEstatic int sqlite3VdbeMemCast(Mem*,u8,u8);
22792SQLITE_PRIVATEstatic int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*);
22793SQLITE_PRIVATEstatic int sqlite3VdbeMemFromBtreeZeroOffset(BtCursor*,u32,Mem*);
22794SQLITE_PRIVATEstatic void sqlite3VdbeMemRelease(Mem *p);
22795SQLITE_PRIVATEstatic void sqlite3VdbeMemReleaseMalloc(Mem*p);
22796SQLITE_PRIVATEstatic int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
22797#ifndef SQLITE_OMIT_WINDOWFUNC
22798SQLITE_PRIVATEstatic int sqlite3VdbeMemAggValue(Mem*, Mem*, FuncDef*);
22799#endif
22800#if !defined(SQLITE_OMIT_EXPLAIN) || defined(SQLITE_ENABLE_BYTECODE_VTAB)
22801SQLITE_PRIVATEstatic const char *sqlite3OpcodeName(int);
22802#endif
22803SQLITE_PRIVATEstatic int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
22804SQLITE_PRIVATEstatic int sqlite3VdbeMemClearAndResize(Mem *pMem, int n);
22805SQLITE_PRIVATEstatic int sqlite3VdbeCloseStatement(Vdbe *, int);
22806#ifdef SQLITE_DEBUG
22807SQLITE_PRIVATEstatic int sqlite3VdbeFrameIsValid(VdbeFrame*);
22808#endif
22809SQLITE_PRIVATEstatic void sqlite3VdbeFrameMemDel(void*); /* Destructor on Mem */
22810SQLITE_PRIVATEstatic void sqlite3VdbeFrameDelete(VdbeFrame*); /* Actually deletes the Frame */
22811SQLITE_PRIVATEstatic int sqlite3VdbeFrameRestore(VdbeFrame *);
22812#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
22813SQLITE_PRIVATEstatic void sqlite3VdbePreUpdateHook(
22814 Vdbe*,VdbeCursor*,int,const char*,Table*,i64,int,int);
22815#endif
22816SQLITE_PRIVATEstatic int sqlite3VdbeTransferError(Vdbe *p);
22817
22818SQLITE_PRIVATEstatic int sqlite3VdbeSorterInit(sqlite3 *, int, VdbeCursor *);
22819SQLITE_PRIVATEstatic void sqlite3VdbeSorterReset(sqlite3 *, VdbeSorter *);
22820SQLITE_PRIVATEstatic void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
22821SQLITE_PRIVATEstatic int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
22822SQLITE_PRIVATEstatic int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *);
22823SQLITE_PRIVATEstatic int sqlite3VdbeSorterRewind(const VdbeCursor *, int *);
22824SQLITE_PRIVATEstatic int sqlite3VdbeSorterWrite(const VdbeCursor *, Mem *);
22825SQLITE_PRIVATEstatic int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *);
22826
22827#ifdef SQLITE_DEBUG
22828SQLITE_PRIVATEstatic void sqlite3VdbeIncrWriteCounter(Vdbe*, VdbeCursor*);
22829SQLITE_PRIVATEstatic void sqlite3VdbeAssertAbortable(Vdbe*);
22830#else
22831# define sqlite3VdbeIncrWriteCounter(V,C)
22832# define sqlite3VdbeAssertAbortable(V)
22833#endif
22834
22835#if !defined(SQLITE_OMIT_SHARED_CACHE)
22836SQLITE_PRIVATEstatic void sqlite3VdbeEnter(Vdbe*);
22837#else
22838# define sqlite3VdbeEnter(X)
22839#endif
22840
22841#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE2>0
22842SQLITE_PRIVATEstatic void sqlite3VdbeLeave(Vdbe*);
22843#else
22844# define sqlite3VdbeLeave(X)
22845#endif
22846
22847#ifdef SQLITE_DEBUG
22848SQLITE_PRIVATEstatic void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
22849SQLITE_PRIVATEstatic int sqlite3VdbeCheckMemInvariants(Mem*);
22850#endif
22851
22852#ifndef SQLITE_OMIT_FOREIGN_KEY
22853SQLITE_PRIVATEstatic int sqlite3VdbeCheckFk(Vdbe *, int);
22854#else
22855# define sqlite3VdbeCheckFk(p,i) 0
22856#endif
22857
22858#ifdef SQLITE_DEBUG
22859SQLITE_PRIVATEstatic void sqlite3VdbePrintSql(Vdbe*);
22860SQLITE_PRIVATEstatic void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr);
22861#endif
22862#ifndef SQLITE_OMIT_UTF16
22863SQLITE_PRIVATEstatic int sqlite3VdbeMemTranslate(Mem*, u8);
22864SQLITE_PRIVATEstatic int sqlite3VdbeMemHandleBom(Mem *pMem);
22865#endif
22866
22867#ifndef SQLITE_OMIT_INCRBLOB
22868SQLITE_PRIVATEstatic int sqlite3VdbeMemExpandBlob(Mem *);
22869 #define ExpandBlob(P)(((P)->flags&0x0400)?sqlite3VdbeMemExpandBlob(P):0) (((P)->flags&MEM_Zero0x0400)?sqlite3VdbeMemExpandBlob(P):0)
22870#else
22871 #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK0
22872 #define ExpandBlob(P)(((P)->flags&0x0400)?sqlite3VdbeMemExpandBlob(P):0) SQLITE_OK0
22873#endif
22874
22875#endif /* !defined(SQLITE_VDBEINT_H) */
22876
22877/************** End of vdbeInt.h *********************************************/
22878/************** Continuing where we left off in status.c *********************/
22879
22880/*
22881** Variables in which to record status information.
22882*/
22883#if SQLITE_PTRSIZE8>4
22884typedef sqlite3_int64 sqlite3StatValueType;
22885#else
22886typedef u32 sqlite3StatValueType;
22887#endif
22888typedef struct sqlite3StatType sqlite3StatType;
22889static SQLITE_WSD struct sqlite3StatType {
22890 sqlite3StatValueType nowValue[10]; /* Current value */
22891 sqlite3StatValueType mxValue[10]; /* Maximum value */
22892} sqlite3Stat = { {0,}, {0,} };
22893
22894/*
22895** Elements of sqlite3Stat[] are protected by either the memory allocator
22896** mutex, or by the pcache1 mutex. The following array determines which.
22897*/
22898static const char statMutex[] = {
22899 0, /* SQLITE_STATUS_MEMORY_USED */
22900 1, /* SQLITE_STATUS_PAGECACHE_USED */
22901 1, /* SQLITE_STATUS_PAGECACHE_OVERFLOW */
22902 0, /* SQLITE_STATUS_SCRATCH_USED */
22903 0, /* SQLITE_STATUS_SCRATCH_OVERFLOW */
22904 0, /* SQLITE_STATUS_MALLOC_SIZE */
22905 0, /* SQLITE_STATUS_PARSER_STACK */
22906 1, /* SQLITE_STATUS_PAGECACHE_SIZE */
22907 0, /* SQLITE_STATUS_SCRATCH_SIZE */
22908 0, /* SQLITE_STATUS_MALLOC_COUNT */
22909};
22910
22911
22912/* The "wsdStat" macro will resolve to the status information
22913** state vector. If writable static data is unsupported on the target,
22914** we have to locate the state vector at run-time. In the more common
22915** case where writable static data is supported, wsdStat can refer directly
22916** to the "sqlite3Stat" state vector declared above.
22917*/
22918#ifdef SQLITE_OMIT_WSD
22919# define wsdStatInit sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)sqlite3Stat
22920# define wsdStatsqlite3Stat x[0]
22921#else
22922# define wsdStatInit
22923# define wsdStatsqlite3Stat sqlite3Stat
22924#endif
22925
22926/*
22927** Return the current value of a status parameter. The caller must
22928** be holding the appropriate mutex.
22929*/
22930SQLITE_PRIVATEstatic sqlite3_int64 sqlite3StatusValue(int op){
22931 wsdStatInit;
22932 assert( op>=0 && op<ArraySize(wsdStat.nowValue) )((void) (0));
22933 assert( op>=0 && op<ArraySize(statMutex) )((void) (0));
22934 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()((void) (0))
22935 : sqlite3MallocMutex()) )((void) (0));
22936 return wsdStatsqlite3Stat.nowValue[op];
22937}
22938
22939/*
22940** Add N to the value of a status record. The caller must hold the
22941** appropriate mutex. (Locking is checked by assert()).
22942**
22943** The StatusUp() routine can accept positive or negative values for N.
22944** The value of N is added to the current status value and the high-water
22945** mark is adjusted if necessary.
22946**
22947** The StatusDown() routine lowers the current value by N. The highwater
22948** mark is unchanged. N must be non-negative for StatusDown().
22949*/
22950SQLITE_PRIVATEstatic void sqlite3StatusUp(int op, int N){
22951 wsdStatInit;
22952 assert( op>=0 && op<ArraySize(wsdStat.nowValue) )((void) (0));
22953 assert( op>=0 && op<ArraySize(statMutex) )((void) (0));
22954 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()((void) (0))
22955 : sqlite3MallocMutex()) )((void) (0));
22956 wsdStatsqlite3Stat.nowValue[op] += N;
22957 if( wsdStatsqlite3Stat.nowValue[op]>wsdStatsqlite3Stat.mxValue[op] ){
22958 wsdStatsqlite3Stat.mxValue[op] = wsdStatsqlite3Stat.nowValue[op];
22959 }
22960}
22961SQLITE_PRIVATEstatic void sqlite3StatusDown(int op, int N){
22962 wsdStatInit;
22963 assert( N>=0 )((void) (0));
22964 assert( op>=0 && op<ArraySize(statMutex) )((void) (0));
22965 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()((void) (0))
22966 : sqlite3MallocMutex()) )((void) (0));
22967 assert( op>=0 && op<ArraySize(wsdStat.nowValue) )((void) (0));
22968 wsdStatsqlite3Stat.nowValue[op] -= N;
22969}
22970
22971/*
22972** Adjust the highwater mark if necessary.
22973** The caller must hold the appropriate mutex.
22974*/
22975SQLITE_PRIVATEstatic void sqlite3StatusHighwater(int op, int X){
22976 sqlite3StatValueType newValue;
22977 wsdStatInit;
22978 assert( X>=0 )((void) (0));
22979 newValue = (sqlite3StatValueType)X;
22980 assert( op>=0 && op<ArraySize(wsdStat.nowValue) )((void) (0));
22981 assert( op>=0 && op<ArraySize(statMutex) )((void) (0));
22982 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()((void) (0))
22983 : sqlite3MallocMutex()) )((void) (0));
22984 assert( op==SQLITE_STATUS_MALLOC_SIZE((void) (0))
22985 || op==SQLITE_STATUS_PAGECACHE_SIZE((void) (0))
22986 || op==SQLITE_STATUS_PARSER_STACK )((void) (0));
22987 if( newValue>wsdStatsqlite3Stat.mxValue[op] ){
22988 wsdStatsqlite3Stat.mxValue[op] = newValue;
22989 }
22990}
22991
22992/*
22993** Query status information.
22994*/
22995SQLITE_API int sqlite3_status64(
22996 int op,
22997 sqlite3_int64 *pCurrent,
22998 sqlite3_int64 *pHighwater,
22999 int resetFlag
23000){
23001 sqlite3_mutex *pMutex;
23002 wsdStatInit;
23003 if( op<0 || op>=ArraySize(wsdStat.nowValue)((int)(sizeof(sqlite3Stat.nowValue)/sizeof(sqlite3Stat.nowValue
[0])))
){
23004 return SQLITE_MISUSE_BKPTsqlite3MisuseError(23004);
23005 }
23006#ifdef SQLITE_ENABLE_API_ARMOR1
23007 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(23007);
23008#endif
23009 pMutex = statMutex[op] ? sqlite3Pcache1Mutex() : sqlite3MallocMutex();
23010 sqlite3_mutex_enter(pMutex);
23011 *pCurrent = wsdStatsqlite3Stat.nowValue[op];
23012 *pHighwater = wsdStatsqlite3Stat.mxValue[op];
23013 if( resetFlag ){
23014 wsdStatsqlite3Stat.mxValue[op] = wsdStatsqlite3Stat.nowValue[op];
23015 }
23016 sqlite3_mutex_leave(pMutex);
23017 (void)pMutex; /* Prevent warning when SQLITE_THREADSAFE=0 */
23018 return SQLITE_OK0;
23019}
23020SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
23021 sqlite3_int64 iCur = 0, iHwtr = 0;
23022 int rc;
23023#ifdef SQLITE_ENABLE_API_ARMOR1
23024 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(23024);
23025#endif
23026 rc = sqlite3_status64(op, &iCur, &iHwtr, resetFlag);
23027 if( rc==0 ){
23028 *pCurrent = (int)iCur;
23029 *pHighwater = (int)iHwtr;
23030 }
23031 return rc;
23032}
23033
23034/*
23035** Return the number of LookasideSlot elements on the linked list
23036*/
23037static u32 countLookasideSlots(LookasideSlot *p){
23038 u32 cnt = 0;
23039 while( p ){
23040 p = p->pNext;
23041 cnt++;
23042 }
23043 return cnt;
23044}
23045
23046/*
23047** Count the number of slots of lookaside memory that are outstanding
23048*/
23049SQLITE_PRIVATEstatic int sqlite3LookasideUsed(sqlite3 *db, int *pHighwater){
23050 u32 nInit = countLookasideSlots(db->lookaside.pInit);
23051 u32 nFree = countLookasideSlots(db->lookaside.pFree);
23052#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
23053 nInit += countLookasideSlots(db->lookaside.pSmallInit);
23054 nFree += countLookasideSlots(db->lookaside.pSmallFree);
23055#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
23056 if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit;
23057 return db->lookaside.nSlot - (nInit+nFree);
23058}
23059
23060/*
23061** Query status information for a single database connection
23062*/
23063SQLITE_API int sqlite3_db_status(
23064 sqlite3 *db, /* The database connection whose status is desired */
23065 int op, /* Status verb */
23066 int *pCurrent, /* Write current value here */
23067 int *pHighwater, /* Write high-water mark here */
23068 int resetFlag /* Reset high-water mark if true */
23069){
23070 int rc = SQLITE_OK0; /* Return code */
23071#ifdef SQLITE_ENABLE_API_ARMOR1
23072 if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){
23073 return SQLITE_MISUSE_BKPTsqlite3MisuseError(23073);
23074 }
23075#endif
23076 sqlite3_mutex_enter(db->mutex);
23077 switch( op ){
23078 case SQLITE_DBSTATUS_LOOKASIDE_USED0: {
23079 *pCurrent = sqlite3LookasideUsed(db, pHighwater);
23080 if( resetFlag ){
23081 LookasideSlot *p = db->lookaside.pFree;
23082 if( p ){
23083 while( p->pNext ) p = p->pNext;
23084 p->pNext = db->lookaside.pInit;
23085 db->lookaside.pInit = db->lookaside.pFree;
23086 db->lookaside.pFree = 0;
23087 }
23088#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
23089 p = db->lookaside.pSmallFree;
23090 if( p ){
23091 while( p->pNext ) p = p->pNext;
23092 p->pNext = db->lookaside.pSmallInit;
23093 db->lookaside.pSmallInit = db->lookaside.pSmallFree;
23094 db->lookaside.pSmallFree = 0;
23095 }
23096#endif
23097 }
23098 break;
23099 }
23100
23101 case SQLITE_DBSTATUS_LOOKASIDE_HIT4:
23102 case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE5:
23103 case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL6: {
23104 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
23105 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
23106 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
23107 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 )((void) (0));
23108 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 )((void) (0));
23109 *pCurrent = 0;
23110 *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT4];
23111 if( resetFlag ){
23112 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT4] = 0;
23113 }
23114 break;
23115 }
23116
23117 /*
23118 ** Return an approximation for the amount of memory currently used
23119 ** by all pagers associated with the given database connection. The
23120 ** highwater mark is meaningless and is returned as zero.
23121 */
23122 case SQLITE_DBSTATUS_CACHE_USED_SHARED11:
23123 case SQLITE_DBSTATUS_CACHE_USED1: {
23124 int totalUsed = 0;
23125 int i;
23126 sqlite3BtreeEnterAll(db);
23127 for(i=0; i<db->nDb; i++){
23128 Btree *pBt = db->aDb[i].pBt;
23129 if( pBt ){
23130 Pager *pPager = sqlite3BtreePager(pBt);
23131 int nByte = sqlite3PagerMemUsed(pPager);
23132 if( op==SQLITE_DBSTATUS_CACHE_USED_SHARED11 ){
23133 nByte = nByte / sqlite3BtreeConnectionCount(pBt);
23134 }
23135 totalUsed += nByte;
23136 }
23137 }
23138 sqlite3BtreeLeaveAll(db);
23139 *pCurrent = totalUsed;
23140 *pHighwater = 0;
23141 break;
23142 }
23143
23144 /*
23145 ** *pCurrent gets an accurate estimate of the amount of memory used
23146 ** to store the schema for all databases (main, temp, and any ATTACHed
23147 ** databases. *pHighwater is set to zero.
23148 */
23149 case SQLITE_DBSTATUS_SCHEMA_USED2: {
23150 int i; /* Used to iterate through schemas */
23151 int nByte = 0; /* Used to accumulate return value */
23152
23153 sqlite3BtreeEnterAll(db);
23154 db->pnBytesFreed = &nByte;
23155 for(i=0; i<db->nDb; i++){
23156 Schema *pSchema = db->aDb[i].pSchema;
23157 if( ALWAYS(pSchema!=0)(pSchema!=0) ){
23158 HashElem *p;
23159
23160 nByte += sqlite3GlobalConfigsqlite3Config.m.xRoundup(sizeof(HashElem)) * (
23161 pSchema->tblHash.count
23162 + pSchema->trigHash.count
23163 + pSchema->idxHash.count
23164 + pSchema->fkeyHash.count
23165 );
23166 nByte += sqlite3_msize(pSchema->tblHash.ht);
23167 nByte += sqlite3_msize(pSchema->trigHash.ht);
23168 nByte += sqlite3_msize(pSchema->idxHash.ht);
23169 nByte += sqlite3_msize(pSchema->fkeyHash.ht);
23170
23171 for(p=sqliteHashFirst(&pSchema->trigHash)((&pSchema->trigHash)->first); p; p=sqliteHashNext(p)((p)->next)){
23172 sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p)((p)->data));
23173 }
23174 for(p=sqliteHashFirst(&pSchema->tblHash)((&pSchema->tblHash)->first); p; p=sqliteHashNext(p)((p)->next)){
23175 sqlite3DeleteTable(db, (Table *)sqliteHashData(p)((p)->data));
23176 }
23177 }
23178 }
23179 db->pnBytesFreed = 0;
23180 sqlite3BtreeLeaveAll(db);
23181
23182 *pHighwater = 0;
23183 *pCurrent = nByte;
23184 break;
23185 }
23186
23187 /*
23188 ** *pCurrent gets an accurate estimate of the amount of memory used
23189 ** to store all prepared statements.
23190 ** *pHighwater is set to zero.
23191 */
23192 case SQLITE_DBSTATUS_STMT_USED3: {
23193 struct Vdbe *pVdbe; /* Used to iterate through VMs */
23194 int nByte = 0; /* Used to accumulate return value */
23195
23196 db->pnBytesFreed = &nByte;
23197 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
23198 sqlite3VdbeDelete(pVdbe);
23199 }
23200 db->pnBytesFreed = 0;
23201
23202 *pHighwater = 0; /* IMP: R-64479-57858 */
23203 *pCurrent = nByte;
23204
23205 break;
23206 }
23207
23208 /*
23209 ** Set *pCurrent to the total cache hits or misses encountered by all
23210 ** pagers the database handle is connected to. *pHighwater is always set
23211 ** to zero.
23212 */
23213 case SQLITE_DBSTATUS_CACHE_SPILL12:
23214 op = SQLITE_DBSTATUS_CACHE_WRITE9+1;
23215 /* no break */ deliberate_fall_through
23216 case SQLITE_DBSTATUS_CACHE_HIT7:
23217 case SQLITE_DBSTATUS_CACHE_MISS8:
23218 case SQLITE_DBSTATUS_CACHE_WRITE9:{
23219 int i;
23220 int nRet = 0;
23221 assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 )((void) (0));
23222 assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 )((void) (0));
23223
23224 for(i=0; i<db->nDb; i++){
23225 if( db->aDb[i].pBt ){
23226 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
23227 sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
23228 }
23229 }
23230 *pHighwater = 0; /* IMP: R-42420-56072 */
23231 /* IMP: R-54100-20147 */
23232 /* IMP: R-29431-39229 */
23233 *pCurrent = nRet;
23234 break;
23235 }
23236
23237 /* Set *pCurrent to non-zero if there are unresolved deferred foreign
23238 ** key constraints. Set *pCurrent to zero if all foreign key constraints
23239 ** have been satisfied. The *pHighwater is always set to zero.
23240 */
23241 case SQLITE_DBSTATUS_DEFERRED_FKS10: {
23242 *pHighwater = 0; /* IMP: R-11967-56545 */
23243 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
23244 break;
23245 }
23246
23247 default: {
23248 rc = SQLITE_ERROR1;
23249 }
23250 }
23251 sqlite3_mutex_leave(db->mutex);
23252 return rc;
23253}
23254
23255/************** End of status.c **********************************************/
23256/************** Begin file date.c ********************************************/
23257/*
23258** 2003 October 31
23259**
23260** The author disclaims copyright to this source code. In place of
23261** a legal notice, here is a blessing:
23262**
23263** May you do good and not evil.
23264** May you find forgiveness for yourself and forgive others.
23265** May you share freely, never taking more than you give.
23266**
23267*************************************************************************
23268** This file contains the C functions that implement date and time
23269** functions for SQLite.
23270**
23271** There is only one exported symbol in this file - the function
23272** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
23273** All other code has file scope.
23274**
23275** SQLite processes all times and dates as julian day numbers. The
23276** dates and times are stored as the number of days since noon
23277** in Greenwich on November 24, 4714 B.C. according to the Gregorian
23278** calendar system.
23279**
23280** 1970-01-01 00:00:00 is JD 2440587.5
23281** 2000-01-01 00:00:00 is JD 2451544.5
23282**
23283** This implementation requires years to be expressed as a 4-digit number
23284** which means that only dates between 0000-01-01 and 9999-12-31 can
23285** be represented, even though julian day numbers allow a much wider
23286** range of dates.
23287**
23288** The Gregorian calendar system is used for all dates and times,
23289** even those that predate the Gregorian calendar. Historians usually
23290** use the julian calendar for dates prior to 1582-10-15 and for some
23291** dates afterwards, depending on locale. Beware of this difference.
23292**
23293** The conversion algorithms are implemented based on descriptions
23294** in the following text:
23295**
23296** Jean Meeus
23297** Astronomical Algorithms, 2nd Edition, 1998
23298** ISBN 0-943396-61-1
23299** Willmann-Bell, Inc
23300** Richmond, Virginia (USA)
23301*/
23302/* #include "sqliteInt.h" */
23303/* #include <stdlib.h> */
23304/* #include <assert.h> */
23305#include <time.h>
23306
23307#ifndef SQLITE_OMIT_DATETIME_FUNCS
23308
23309/*
23310** The MSVC CRT on Windows CE may not have a localtime() function.
23311** So declare a substitute. The substitute function itself is
23312** defined in "os_win.c".
23313*/
23314#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
23315 (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
23316struct tm *__cdecl localtime(const time_t *);
23317#endif
23318
23319/*
23320** A structure for holding a single date and time.
23321*/
23322typedef struct DateTime DateTime;
23323struct DateTime {
23324 sqlite3_int64 iJD; /* The julian day number times 86400000 */
23325 int Y, M, D; /* Year, month, and day */
23326 int h, m; /* Hour and minutes */
23327 int tz; /* Timezone offset in minutes */
23328 double s; /* Seconds */
23329 char validJD; /* True (1) if iJD is valid */
23330 char rawS; /* Raw numeric value stored in s */
23331 char validYMD; /* True (1) if Y,M,D are valid */
23332 char validHMS; /* True (1) if h,m,s are valid */
23333 char validTZ; /* True (1) if tz is valid */
23334 char tzSet; /* Timezone was set explicitly */
23335 char isError; /* An overflow has occurred */
23336};
23337
23338
23339/*
23340** Convert zDate into one or more integers according to the conversion
23341** specifier zFormat.
23342**
23343** zFormat[] contains 4 characters for each integer converted, except for
23344** the last integer which is specified by three characters. The meaning
23345** of a four-character format specifiers ABCD is:
23346**
23347** A: number of digits to convert. Always "2" or "4".
23348** B: minimum value. Always "0" or "1".
23349** C: maximum value, decoded as:
23350** a: 12
23351** b: 14
23352** c: 24
23353** d: 31
23354** e: 59
23355** f: 9999
23356** D: the separator character, or \000 to indicate this is the
23357** last number to convert.
23358**
23359** Example: To translate an ISO-8601 date YYYY-MM-DD, the format would
23360** be "40f-21a-20c". The "40f-" indicates the 4-digit year followed by "-".
23361** The "21a-" indicates the 2-digit month followed by "-". The "20c" indicates
23362** the 2-digit day which is the last integer in the set.
23363**
23364** The function returns the number of successful conversions.
23365*/
23366static int getDigits(const char *zDate, const char *zFormat, ...){
23367 /* The aMx[] array translates the 3rd character of each format
23368 ** spec into a max size: a b c d e f */
23369 static const u16 aMx[] = { 12, 14, 24, 31, 59, 9999 };
23370 va_list ap;
23371 int cnt = 0;
23372 char nextC;
23373 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
23374 do{
23375 char N = zFormat[0] - '0';
23376 char min = zFormat[1] - '0';
23377 int val = 0;
23378 u16 max;
23379
23380 assert( zFormat[2]>='a' && zFormat[2]<='f' )((void) (0));
23381 max = aMx[zFormat[2] - 'a'];
23382 nextC = zFormat[3];
23383 val = 0;
23384 while( N-- ){
23385 if( !sqlite3Isdigit(*zDate)(sqlite3CtypeMap[(unsigned char)(*zDate)]&0x04) ){
23386 goto end_getDigits;
23387 }
23388 val = val*10 + *zDate - '0';
23389 zDate++;
23390 }
23391 if( val<(int)min || val>(int)max || (nextC!=0 && nextC!=*zDate) ){
23392 goto end_getDigits;
23393 }
23394 *va_arg(ap,int*)__builtin_va_arg(ap, int*) = val;
23395 zDate++;
23396 cnt++;
23397 zFormat += 4;
23398 }while( nextC );
23399end_getDigits:
23400 va_end(ap)__builtin_va_end(ap);
23401 return cnt;
23402}
23403
23404/*
23405** Parse a timezone extension on the end of a date-time.
23406** The extension is of the form:
23407**
23408** (+/-)HH:MM
23409**
23410** Or the "zulu" notation:
23411**
23412** Z
23413**
23414** If the parse is successful, write the number of minutes
23415** of change in p->tz and return 0. If a parser error occurs,
23416** return non-zero.
23417**
23418** A missing specifier is not considered an error.
23419*/
23420static int parseTimezone(const char *zDate, DateTime *p){
23421 int sgn = 0;
23422 int nHr, nMn;
23423 int c;
23424 while( sqlite3Isspace(*zDate)(sqlite3CtypeMap[(unsigned char)(*zDate)]&0x01) ){ zDate++; }
23425 p->tz = 0;
23426 c = *zDate;
23427 if( c=='-' ){
23428 sgn = -1;
23429 }else if( c=='+' ){
23430 sgn = +1;
23431 }else if( c=='Z' || c=='z' ){
23432 zDate++;
23433 goto zulu_time;
23434 }else{
23435 return c!=0;
23436 }
23437 zDate++;
23438 if( getDigits(zDate, "20b:20e", &nHr, &nMn)!=2 ){
23439 return 1;
23440 }
23441 zDate += 5;
23442 p->tz = sgn*(nMn + nHr*60);
23443zulu_time:
23444 while( sqlite3Isspace(*zDate)(sqlite3CtypeMap[(unsigned char)(*zDate)]&0x01) ){ zDate++; }
23445 p->tzSet = 1;
23446 return *zDate!=0;
23447}
23448
23449/*
23450** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
23451** The HH, MM, and SS must each be exactly 2 digits. The
23452** fractional seconds FFFF can be one or more digits.
23453**
23454** Return 1 if there is a parsing error and 0 on success.
23455*/
23456static int parseHhMmSs(const char *zDate, DateTime *p){
23457 int h, m, s;
23458 double ms = 0.0;
23459 if( getDigits(zDate, "20c:20e", &h, &m)!=2 ){
23460 return 1;
23461 }
23462 zDate += 5;
23463 if( *zDate==':' ){
23464 zDate++;
23465 if( getDigits(zDate, "20e", &s)!=1 ){
23466 return 1;
23467 }
23468 zDate += 2;
23469 if( *zDate=='.' && sqlite3Isdigit(zDate[1])(sqlite3CtypeMap[(unsigned char)(zDate[1])]&0x04) ){
23470 double rScale = 1.0;
23471 zDate++;
23472 while( sqlite3Isdigit(*zDate)(sqlite3CtypeMap[(unsigned char)(*zDate)]&0x04) ){
23473 ms = ms*10.0 + *zDate - '0';
23474 rScale *= 10.0;
23475 zDate++;
23476 }
23477 ms /= rScale;
23478 }
23479 }else{
23480 s = 0;
23481 }
23482 p->validJD = 0;
23483 p->rawS = 0;
23484 p->validHMS = 1;
23485 p->h = h;
23486 p->m = m;
23487 p->s = s + ms;
23488 if( parseTimezone(zDate, p) ) return 1;
23489 p->validTZ = (p->tz!=0)?1:0;
23490 return 0;
23491}
23492
23493/*
23494** Put the DateTime object into its error state.
23495*/
23496static void datetimeError(DateTime *p){
23497 memset(p, 0, sizeof(*p));
23498 p->isError = 1;
23499}
23500
23501/*
23502** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
23503** that the YYYY-MM-DD is according to the Gregorian calendar.
23504**
23505** Reference: Meeus page 61
23506*/
23507static void computeJD(DateTime *p){
23508 int Y, M, D, A, B, X1, X2;
23509
23510 if( p->validJD ) return;
23511 if( p->validYMD ){
23512 Y = p->Y;
23513 M = p->M;
23514 D = p->D;
23515 }else{
23516 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */
23517 M = 1;
23518 D = 1;
23519 }
23520 if( Y<-4713 || Y>9999 || p->rawS ){
23521 datetimeError(p);
23522 return;
23523 }
23524 if( M<=2 ){
23525 Y--;
23526 M += 12;
23527 }
23528 A = Y/100;
23529 B = 2 - A + (A/4);
23530 X1 = 36525*(Y+4716)/100;
23531 X2 = 306001*(M+1)/10000;
23532 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
23533 p->validJD = 1;
23534 if( p->validHMS ){
23535 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
23536 if( p->validTZ ){
23537 p->iJD -= p->tz*60000;
23538 p->validYMD = 0;
23539 p->validHMS = 0;
23540 p->validTZ = 0;
23541 }
23542 }
23543}
23544
23545/*
23546** Parse dates of the form
23547**
23548** YYYY-MM-DD HH:MM:SS.FFF
23549** YYYY-MM-DD HH:MM:SS
23550** YYYY-MM-DD HH:MM
23551** YYYY-MM-DD
23552**
23553** Write the result into the DateTime structure and return 0
23554** on success and 1 if the input string is not a well-formed
23555** date.
23556*/
23557static int parseYyyyMmDd(const char *zDate, DateTime *p){
23558 int Y, M, D, neg;
23559
23560 if( zDate[0]=='-' ){
23561 zDate++;
23562 neg = 1;
23563 }else{
23564 neg = 0;
23565 }
23566 if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){
23567 return 1;
23568 }
23569 zDate += 10;
23570 while( sqlite3Isspace(*zDate)(sqlite3CtypeMap[(unsigned char)(*zDate)]&0x01) || 'T'==*(u8*)zDate ){ zDate++; }
23571 if( parseHhMmSs(zDate, p)==0 ){
23572 /* We got the time */
23573 }else if( *zDate==0 ){
23574 p->validHMS = 0;
23575 }else{
23576 return 1;
23577 }
23578 p->validJD = 0;
23579 p->validYMD = 1;
23580 p->Y = neg ? -Y : Y;
23581 p->M = M;
23582 p->D = D;
23583 if( p->validTZ ){
23584 computeJD(p);
23585 }
23586 return 0;
23587}
23588
23589/*
23590** Set the time to the current time reported by the VFS.
23591**
23592** Return the number of errors.
23593*/
23594static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
23595 p->iJD = sqlite3StmtCurrentTime(context);
23596 if( p->iJD>0 ){
23597 p->validJD = 1;
23598 return 0;
23599 }else{
23600 return 1;
23601 }
23602}
23603
23604/*
23605** Input "r" is a numeric quantity which might be a julian day number,
23606** or the number of seconds since 1970. If the value if r is within
23607** range of a julian day number, install it as such and set validJD.
23608** If the value is a valid unix timestamp, put it in p->s and set p->rawS.
23609*/
23610static void setRawDateNumber(DateTime *p, double r){
23611 p->s = r;
23612 p->rawS = 1;
23613 if( r>=0.0 && r<5373484.5 ){
23614 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
23615 p->validJD = 1;
23616 }
23617}
23618
23619/*
23620** Attempt to parse the given string into a julian day number. Return
23621** the number of errors.
23622**
23623** The following are acceptable forms for the input string:
23624**
23625** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
23626** DDDD.DD
23627** now
23628**
23629** In the first form, the +/-HH:MM is always optional. The fractional
23630** seconds extension (the ".FFF") is optional. The seconds portion
23631** (":SS.FFF") is option. The year and date can be omitted as long
23632** as there is a time string. The time string can be omitted as long
23633** as there is a year and date.
23634*/
23635static int parseDateOrTime(
23636 sqlite3_context *context,
23637 const char *zDate,
23638 DateTime *p
23639){
23640 double r;
23641 if( parseYyyyMmDd(zDate,p)==0 ){
23642 return 0;
23643 }else if( parseHhMmSs(zDate, p)==0 ){
23644 return 0;
23645 }else if( sqlite3StrICmp(zDate,"now")==0 && sqlite3NotPureFunc(context) ){
23646 return setDateTimeToCurrent(context, p);
23647 }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF81)>0 ){
23648 setRawDateNumber(p, r);
23649 return 0;
23650 }
23651 return 1;
23652}
23653
23654/* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999.
23655** Multiplying this by 86400000 gives 464269060799999 as the maximum value
23656** for DateTime.iJD.
23657**
23658** But some older compilers (ex: gcc 4.2.1 on older Macs) cannot deal with
23659** such a large integer literal, so we have to encode it.
23660*/
23661#define INT_464269060799999((((i64)0x1a640)<<32)|0x1072fdff) ((((i64)0x1a640)<<32)|0x1072fdff)
23662
23663/*
23664** Return TRUE if the given julian day number is within range.
23665**
23666** The input is the JulianDay times 86400000.
23667*/
23668static int validJulianDay(sqlite3_int64 iJD){
23669 return iJD>=0 && iJD<=INT_464269060799999((((i64)0x1a640)<<32)|0x1072fdff);
23670}
23671
23672/*
23673** Compute the Year, Month, and Day from the julian day number.
23674*/
23675static void computeYMD(DateTime *p){
23676 int Z, A, B, C, D, E, X1;
23677 if( p->validYMD ) return;
23678 if( !p->validJD ){
23679 p->Y = 2000;
23680 p->M = 1;
23681 p->D = 1;
23682 }else if( !validJulianDay(p->iJD) ){
23683 datetimeError(p);
23684 return;
23685 }else{
23686 Z = (int)((p->iJD + 43200000)/86400000);
23687 A = (int)((Z - 1867216.25)/36524.25);
23688 A = Z + 1 + A - (A/4);
23689 B = A + 1524;
23690 C = (int)((B - 122.1)/365.25);
23691 D = (36525*(C&32767))/100;
23692 E = (int)((B-D)/30.6001);
23693 X1 = (int)(30.6001*E);
23694 p->D = B - D - X1;
23695 p->M = E<14 ? E-1 : E-13;
23696 p->Y = p->M>2 ? C - 4716 : C - 4715;
23697 }
23698 p->validYMD = 1;
23699}
23700
23701/*
23702** Compute the Hour, Minute, and Seconds from the julian day number.
23703*/
23704static void computeHMS(DateTime *p){
23705 int s;
23706 if( p->validHMS ) return;
23707 computeJD(p);
23708 s = (int)((p->iJD + 43200000) % 86400000);
23709 p->s = s/1000.0;
23710 s = (int)p->s;
23711 p->s -= s;
23712 p->h = s/3600;
23713 s -= p->h*3600;
23714 p->m = s/60;
23715 p->s += s - p->m*60;
23716 p->rawS = 0;
23717 p->validHMS = 1;
23718}
23719
23720/*
23721** Compute both YMD and HMS
23722*/
23723static void computeYMD_HMS(DateTime *p){
23724 computeYMD(p);
23725 computeHMS(p);
23726}
23727
23728/*
23729** Clear the YMD and HMS and the TZ
23730*/
23731static void clearYMD_HMS_TZ(DateTime *p){
23732 p->validYMD = 0;
23733 p->validHMS = 0;
23734 p->validTZ = 0;
23735}
23736
23737#ifndef SQLITE_OMIT_LOCALTIME
23738/*
23739** On recent Windows platforms, the localtime_s() function is available
23740** as part of the "Secure CRT". It is essentially equivalent to
23741** localtime_r() available under most POSIX platforms, except that the
23742** order of the parameters is reversed.
23743**
23744** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
23745**
23746** If the user has not indicated to use localtime_r() or localtime_s()
23747** already, check for an MSVC build environment that provides
23748** localtime_s().
23749*/
23750#if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S \
23751 && defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
23752#undef HAVE_LOCALTIME_S
23753#define HAVE_LOCALTIME_S 1
23754#endif
23755
23756/*
23757** The following routine implements the rough equivalent of localtime_r()
23758** using whatever operating-system specific localtime facility that
23759** is available. This routine returns 0 on success and
23760** non-zero on any kind of error.
23761**
23762** If the sqlite3GlobalConfig.bLocaltimeFault variable is non-zero then this
23763** routine will always fail. If bLocaltimeFault is nonzero and
23764** sqlite3GlobalConfig.xAltLocaltime is not NULL, then xAltLocaltime() is
23765** invoked in place of the OS-defined localtime() function.
23766**
23767** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
23768** library function localtime_r() is used to assist in the calculation of
23769** local time.
23770*/
23771static int osLocaltime(time_t *t, struct tm *pTm){
23772 int rc;
23773#if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S
23774 struct tm *pX;
23775#if SQLITE_THREADSAFE2>0
23776 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2);
23777#endif
23778 sqlite3_mutex_enter(mutex);
23779 pX = localtime(t);
23780#ifndef SQLITE_UNTESTABLE
23781 if( sqlite3GlobalConfigsqlite3Config.bLocaltimeFault ){
23782 if( sqlite3GlobalConfigsqlite3Config.xAltLocaltime!=0
23783 && 0==sqlite3GlobalConfigsqlite3Config.xAltLocaltime((const void*)t,(void*)pTm)
23784 ){
23785 pX = pTm;
23786 }else{
23787 pX = 0;
23788 }
23789 }
23790#endif
23791 if( pX ) *pTm = *pX;
23792#if SQLITE_THREADSAFE2>0
23793 sqlite3_mutex_leave(mutex);
23794#endif
23795 rc = pX==0;
23796#else
23797#ifndef SQLITE_UNTESTABLE
23798 if( sqlite3GlobalConfigsqlite3Config.bLocaltimeFault ){
23799 if( sqlite3GlobalConfigsqlite3Config.xAltLocaltime!=0 ){
23800 return sqlite3GlobalConfigsqlite3Config.xAltLocaltime((const void*)t,(void*)pTm);
23801 }else{
23802 return 1;
23803 }
23804 }
23805#endif
23806#if HAVE_LOCALTIME_R
23807 rc = localtime_r(t, pTm)==0;
23808#else
23809 rc = localtime_s(pTm, t);
23810#endif /* HAVE_LOCALTIME_R */
23811#endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */
23812 return rc;
23813}
23814#endif /* SQLITE_OMIT_LOCALTIME */
23815
23816
23817#ifndef SQLITE_OMIT_LOCALTIME
23818/*
23819** Assuming the input DateTime is UTC, move it to its localtime equivalent.
23820*/
23821static int toLocaltime(
23822 DateTime *p, /* Date at which to calculate offset */
23823 sqlite3_context *pCtx /* Write error here if one occurs */
23824){
23825 time_t t;
23826 struct tm sLocal;
23827 int iYearDiff;
23828
23829 /* Initialize the contents of sLocal to avoid a compiler warning. */
23830 memset(&sLocal, 0, sizeof(sLocal));
23831
23832 computeJD(p);
23833 if( p->iJD<2108667600*(i64)100000 /* 1970-01-01 */
23834 || p->iJD>2130141456*(i64)100000 /* 2038-01-18 */
23835 ){
23836 /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only
23837 ** works for years between 1970 and 2037. For dates outside this range,
23838 ** SQLite attempts to map the year into an equivalent year within this
23839 ** range, do the calculation, then map the year back.
23840 */
23841 DateTime x = *p;
23842 computeYMD_HMS(&x);
23843 iYearDiff = (2000 + x.Y%4) - x.Y;
23844 x.Y += iYearDiff;
23845 x.validJD = 0;
23846 computeJD(&x);
23847 t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
23848 }else{
23849 iYearDiff = 0;
23850 t = (time_t)(p->iJD/1000 - 21086676*(i64)10000);
23851 }
23852 if( osLocaltime(&t, &sLocal) ){
23853 sqlite3_result_error(pCtx, "local time unavailable", -1);
23854 return SQLITE_ERROR1;
23855 }
23856 p->Y = sLocal.tm_year + 1900 - iYearDiff;
23857 p->M = sLocal.tm_mon + 1;
23858 p->D = sLocal.tm_mday;
23859 p->h = sLocal.tm_hour;
23860 p->m = sLocal.tm_min;
23861 p->s = sLocal.tm_sec + (p->iJD%1000)*0.001;
23862 p->validYMD = 1;
23863 p->validHMS = 1;
23864 p->validJD = 0;
23865 p->rawS = 0;
23866 p->validTZ = 0;
23867 p->isError = 0;
23868 return SQLITE_OK0;
23869}
23870#endif /* SQLITE_OMIT_LOCALTIME */
23871
23872/*
23873** The following table defines various date transformations of the form
23874**
23875** 'NNN days'
23876**
23877** Where NNN is an arbitrary floating-point number and "days" can be one
23878** of several units of time.
23879*/
23880static const struct {
23881 u8 nName; /* Length of the name */
23882 char zName[7]; /* Name of the transformation */
23883 float rLimit; /* Maximum NNN value for this transform */
23884 float rXform; /* Constant used for this transform */
23885} aXformType[] = {
23886 { 6, "second", 4.6427e+14, 1.0 },
23887 { 6, "minute", 7.7379e+12, 60.0 },
23888 { 4, "hour", 1.2897e+11, 3600.0 },
23889 { 3, "day", 5373485.0, 86400.0 },
23890 { 5, "month", 176546.0, 2592000.0 },
23891 { 4, "year", 14713.0, 31536000.0 },
23892};
23893
23894/*
23895** Process a modifier to a date-time stamp. The modifiers are
23896** as follows:
23897**
23898** NNN days
23899** NNN hours
23900** NNN minutes
23901** NNN.NNNN seconds
23902** NNN months
23903** NNN years
23904** start of month
23905** start of year
23906** start of week
23907** start of day
23908** weekday N
23909** unixepoch
23910** localtime
23911** utc
23912**
23913** Return 0 on success and 1 if there is any kind of error. If the error
23914** is in a system call (i.e. localtime()), then an error message is written
23915** to context pCtx. If the error is an unrecognized modifier, no error is
23916** written to pCtx.
23917*/
23918static int parseModifier(
23919 sqlite3_context *pCtx, /* Function context */
23920 const char *z, /* The text of the modifier */
23921 int n, /* Length of zMod in bytes */
23922 DateTime *p, /* The date/time value to be modified */
23923 int idx /* Parameter index of the modifier */
23924){
23925 int rc = 1;
23926 double r;
23927 switch(sqlite3UpperToLower[(u8)z[0]] ){
23928 case 'a': {
23929 /*
23930 ** auto
23931 **
23932 ** If rawS is available, then interpret as a julian day number, or
23933 ** a unix timestamp, depending on its magnitude.
23934 */
23935 if( sqlite3_stricmp(z, "auto")==0 ){
23936 if( idx>1 ) return 1; /* IMP: R-33611-57934 */
23937 if( !p->rawS || p->validJD ){
23938 rc = 0;
23939 p->rawS = 0;
23940 }else if( p->s>=-21086676*(i64)10000 /* -4713-11-24 12:00:00 */
23941 && p->s<=(25340230*(i64)10000)+799 /* 9999-12-31 23:59:59 */
23942 ){
23943 r = p->s*1000.0 + 210866760000000.0;
23944 clearYMD_HMS_TZ(p);
23945 p->iJD = (sqlite3_int64)(r + 0.5);
23946 p->validJD = 1;
23947 p->rawS = 0;
23948 rc = 0;
23949 }
23950 }
23951 break;
23952 }
23953 case 'j': {
23954 /*
23955 ** julianday
23956 **
23957 ** Always interpret the prior number as a julian-day value. If this
23958 ** is not the first modifier, or if the prior argument is not a numeric
23959 ** value in the allowed range of julian day numbers understood by
23960 ** SQLite (0..5373484.5) then the result will be NULL.
23961 */
23962 if( sqlite3_stricmp(z, "julianday")==0 ){
23963 if( idx>1 ) return 1; /* IMP: R-31176-64601 */
23964 if( p->validJD && p->rawS ){
23965 rc = 0;
23966 p->rawS = 0;
23967 }
23968 }
23969 break;
23970 }
23971#ifndef SQLITE_OMIT_LOCALTIME
23972 case 'l': {
23973 /* localtime
23974 **
23975 ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
23976 ** show local time.
23977 */
23978 if( sqlite3_stricmp(z, "localtime")==0 && sqlite3NotPureFunc(pCtx) ){
23979 rc = toLocaltime(p, pCtx);
23980 }
23981 break;
23982 }
23983#endif
23984 case 'u': {
23985 /*
23986 ** unixepoch
23987 **
23988 ** Treat the current value of p->s as the number of
23989 ** seconds since 1970. Convert to a real julian day number.
23990 */
23991 if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){
23992 if( idx>1 ) return 1; /* IMP: R-49255-55373 */
23993 r = p->s*1000.0 + 210866760000000.0;
23994 if( r>=0.0 && r<464269060800000.0 ){
23995 clearYMD_HMS_TZ(p);
23996 p->iJD = (sqlite3_int64)(r + 0.5);
23997 p->validJD = 1;
23998 p->rawS = 0;
23999 rc = 0;
24000 }
24001 }
24002#ifndef SQLITE_OMIT_LOCALTIME
24003 else if( sqlite3_stricmp(z, "utc")==0 && sqlite3NotPureFunc(pCtx) ){
24004 if( p->tzSet==0 ){
24005 i64 iOrigJD; /* Original localtime */
24006 i64 iGuess; /* Guess at the corresponding utc time */
24007 int cnt = 0; /* Safety to prevent infinite loop */
24008 int iErr; /* Guess is off by this much */
24009
24010 computeJD(p);
24011 iGuess = iOrigJD = p->iJD;
24012 iErr = 0;
24013 do{
24014 DateTime new;
24015 memset(&new, 0, sizeof(new));
24016 iGuess -= iErr;
24017 new.iJD = iGuess;
24018 new.validJD = 1;
24019 rc = toLocaltime(&new, pCtx);
24020 if( rc ) return rc;
24021 computeJD(&new);
24022 iErr = new.iJD - iOrigJD;
24023 }while( iErr && cnt++<3 );
24024 memset(p, 0, sizeof(*p));
24025 p->iJD = iGuess;
24026 p->validJD = 1;
24027 p->tzSet = 1;
24028 }
24029 rc = SQLITE_OK0;
24030 }
24031#endif
24032 break;
24033 }
24034 case 'w': {
24035 /*
24036 ** weekday N
24037 **
24038 ** Move the date to the same time on the next occurrence of
24039 ** weekday N where 0==Sunday, 1==Monday, and so forth. If the
24040 ** date is already on the appropriate weekday, this is a no-op.
24041 */
24042 if( sqlite3_strnicmp(z, "weekday ", 8)==0
24043 && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF81)>0
24044 && (n=(int)r)==r && n>=0 && r<7 ){
24045 sqlite3_int64 Z;
24046 computeYMD_HMS(p);
24047 p->validTZ = 0;
24048 p->validJD = 0;
24049 computeJD(p);
24050 Z = ((p->iJD + 129600000)/86400000) % 7;
24051 if( Z>n ) Z -= 7;
24052 p->iJD += (n - Z)*86400000;
24053 clearYMD_HMS_TZ(p);
24054 rc = 0;
24055 }
24056 break;
24057 }
24058 case 's': {
24059 /*
24060 ** start of TTTTT
24061 **
24062 ** Move the date backwards to the beginning of the current day,
24063 ** or month or year.
24064 */
24065 if( sqlite3_strnicmp(z, "start of ", 9)!=0 ) break;
24066 if( !p->validJD && !p->validYMD && !p->validHMS ) break;
24067 z += 9;
24068 computeYMD(p);
24069 p->validHMS = 1;
24070 p->h = p->m = 0;
24071 p->s = 0.0;
24072 p->rawS = 0;
24073 p->validTZ = 0;
24074 p->validJD = 0;
24075 if( sqlite3_stricmp(z,"month")==0 ){
24076 p->D = 1;
24077 rc = 0;
24078 }else if( sqlite3_stricmp(z,"year")==0 ){
24079 p->M = 1;
24080 p->D = 1;
24081 rc = 0;
24082 }else if( sqlite3_stricmp(z,"day")==0 ){
24083 rc = 0;
24084 }
24085 break;
24086 }
24087 case '+':
24088 case '-':
24089 case '0':
24090 case '1':
24091 case '2':
24092 case '3':
24093 case '4':
24094 case '5':
24095 case '6':
24096 case '7':
24097 case '8':
24098 case '9': {
24099 double rRounder;
24100 int i;
24101 for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n])(sqlite3CtypeMap[(unsigned char)(z[n])]&0x01); n++){}
24102 if( sqlite3AtoF(z, &r, n, SQLITE_UTF81)<=0 ){
24103 rc = 1;
24104 break;
24105 }
24106 if( z[n]==':' ){
24107 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
24108 ** specified number of hours, minutes, seconds, and fractional seconds
24109 ** to the time. The ".FFF" may be omitted. The ":SS.FFF" may be
24110 ** omitted.
24111 */
24112 const char *z2 = z;
24113 DateTime tx;
24114 sqlite3_int64 day;
24115 if( !sqlite3Isdigit(*z2)(sqlite3CtypeMap[(unsigned char)(*z2)]&0x04) ) z2++;
24116 memset(&tx, 0, sizeof(tx));
24117 if( parseHhMmSs(z2, &tx) ) break;
24118 computeJD(&tx);
24119 tx.iJD -= 43200000;
24120 day = tx.iJD/86400000;
24121 tx.iJD -= day*86400000;
24122 if( z[0]=='-' ) tx.iJD = -tx.iJD;
24123 computeJD(p);
24124 clearYMD_HMS_TZ(p);
24125 p->iJD += tx.iJD;
24126 rc = 0;
24127 break;
24128 }
24129
24130 /* If control reaches this point, it means the transformation is
24131 ** one of the forms like "+NNN days". */
24132 z += n;
24133 while( sqlite3Isspace(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x01) ) z++;
24134 n = sqlite3Strlen30(z);
24135 if( n>10 || n<3 ) break;
24136 if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--;
24137 computeJD(p);
24138 rc = 1;
24139 rRounder = r<0 ? -0.5 : +0.5;
24140 for(i=0; i<ArraySize(aXformType)((int)(sizeof(aXformType)/sizeof(aXformType[0]))); i++){
24141 if( aXformType[i].nName==n
24142 && sqlite3_strnicmp(aXformType[i].zName, z, n)==0
24143 && r>-aXformType[i].rLimit && r<aXformType[i].rLimit
24144 ){
24145 switch( i ){
24146 case 4: { /* Special processing to add months */
24147 int x;
24148 assert( strcmp(aXformType[i].zName,"month")==0 )((void) (0));
24149 computeYMD_HMS(p);
24150 p->M += (int)r;
24151 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
24152 p->Y += x;
24153 p->M -= x*12;
24154 p->validJD = 0;
24155 r -= (int)r;
24156 break;
24157 }
24158 case 5: { /* Special processing to add years */
24159 int y = (int)r;
24160 assert( strcmp(aXformType[i].zName,"year")==0 )((void) (0));
24161 computeYMD_HMS(p);
24162 p->Y += y;
24163 p->validJD = 0;
24164 r -= (int)r;
24165 break;
24166 }
24167 }
24168 computeJD(p);
24169 p->iJD += (sqlite3_int64)(r*1000.0*aXformType[i].rXform + rRounder);
24170 rc = 0;
24171 break;
24172 }
24173 }
24174 clearYMD_HMS_TZ(p);
24175 break;
24176 }
24177 default: {
24178 break;
24179 }
24180 }
24181 return rc;
24182}
24183
24184/*
24185** Process time function arguments. argv[0] is a date-time stamp.
24186** argv[1] and following are modifiers. Parse them all and write
24187** the resulting time into the DateTime structure p. Return 0
24188** on success and 1 if there are any errors.
24189**
24190** If there are zero parameters (if even argv[0] is undefined)
24191** then assume a default value of "now" for argv[0].
24192*/
24193static int isDate(
24194 sqlite3_context *context,
24195 int argc,
24196 sqlite3_value **argv,
24197 DateTime *p
24198){
24199 int i, n;
24200 const unsigned char *z;
24201 int eType;
24202 memset(p, 0, sizeof(*p));
24203 if( argc==0 ){
24204 if( !sqlite3NotPureFunc(context) ) return 1;
24205 return setDateTimeToCurrent(context, p);
24206 }
24207 if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT2
24208 || eType==SQLITE_INTEGER1 ){
24209 setRawDateNumber(p, sqlite3_value_double(argv[0]));
24210 }else{
24211 z = sqlite3_value_text(argv[0]);
24212 if( !z || parseDateOrTime(context, (char*)z, p) ){
24213 return 1;
24214 }
24215 }
24216 for(i=1; i<argc; i++){
24217 z = sqlite3_value_text(argv[i]);
24218 n = sqlite3_value_bytes(argv[i]);
24219 if( z==0 || parseModifier(context, (char*)z, n, p, i) ) return 1;
24220 }
24221 computeJD(p);
24222 if( p->isError || !validJulianDay(p->iJD) ) return 1;
24223 return 0;
24224}
24225
24226
24227/*
24228** The following routines implement the various date and time functions
24229** of SQLite.
24230*/
24231
24232/*
24233** julianday( TIMESTRING, MOD, MOD, ...)
24234**
24235** Return the julian day number of the date specified in the arguments
24236*/
24237static void juliandayFunc(
24238 sqlite3_context *context,
24239 int argc,
24240 sqlite3_value **argv
24241){
24242 DateTime x;
24243 if( isDate(context, argc, argv, &x)==0 ){
24244 computeJD(&x);
24245 sqlite3_result_double(context, x.iJD/86400000.0);
24246 }
24247}
24248
24249/*
24250** unixepoch( TIMESTRING, MOD, MOD, ...)
24251**
24252** Return the number of seconds (including fractional seconds) since
24253** the unix epoch of 1970-01-01 00:00:00 GMT.
24254*/
24255static void unixepochFunc(
24256 sqlite3_context *context,
24257 int argc,
24258 sqlite3_value **argv
24259){
24260 DateTime x;
24261 if( isDate(context, argc, argv, &x)==0 ){
24262 computeJD(&x);
24263 sqlite3_result_int64(context, x.iJD/1000 - 21086676*(i64)10000);
24264 }
24265}
24266
24267/*
24268** datetime( TIMESTRING, MOD, MOD, ...)
24269**
24270** Return YYYY-MM-DD HH:MM:SS
24271*/
24272static void datetimeFunc(
24273 sqlite3_context *context,
24274 int argc,
24275 sqlite3_value **argv
24276){
24277 DateTime x;
24278 if( isDate(context, argc, argv, &x)==0 ){
24279 int Y, s;
24280 char zBuf[24];
24281 computeYMD_HMS(&x);
24282 Y = x.Y;
24283 if( Y<0 ) Y = -Y;
24284 zBuf[1] = '0' + (Y/1000)%10;
24285 zBuf[2] = '0' + (Y/100)%10;
24286 zBuf[3] = '0' + (Y/10)%10;
24287 zBuf[4] = '0' + (Y)%10;
24288 zBuf[5] = '-';
24289 zBuf[6] = '0' + (x.M/10)%10;
24290 zBuf[7] = '0' + (x.M)%10;
24291 zBuf[8] = '-';
24292 zBuf[9] = '0' + (x.D/10)%10;
24293 zBuf[10] = '0' + (x.D)%10;
24294 zBuf[11] = ' ';
24295 zBuf[12] = '0' + (x.h/10)%10;
24296 zBuf[13] = '0' + (x.h)%10;
24297 zBuf[14] = ':';
24298 zBuf[15] = '0' + (x.m/10)%10;
24299 zBuf[16] = '0' + (x.m)%10;
24300 zBuf[17] = ':';
24301 s = (int)x.s;
24302 zBuf[18] = '0' + (s/10)%10;
24303 zBuf[19] = '0' + (s)%10;
24304 zBuf[20] = 0;
24305 if( x.Y<0 ){
24306 zBuf[0] = '-';
24307 sqlite3_result_text(context, zBuf, 20, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
24308 }else{
24309 sqlite3_result_text(context, &zBuf[1], 19, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
24310 }
24311 }
24312}
24313
24314/*
24315** time( TIMESTRING, MOD, MOD, ...)
24316**
24317** Return HH:MM:SS
24318*/
24319static void timeFunc(
24320 sqlite3_context *context,
24321 int argc,
24322 sqlite3_value **argv
24323){
24324 DateTime x;
24325 if( isDate(context, argc, argv, &x)==0 ){
24326 int s;
24327 char zBuf[16];
24328 computeHMS(&x);
24329 zBuf[0] = '0' + (x.h/10)%10;
24330 zBuf[1] = '0' + (x.h)%10;
24331 zBuf[2] = ':';
24332 zBuf[3] = '0' + (x.m/10)%10;
24333 zBuf[4] = '0' + (x.m)%10;
24334 zBuf[5] = ':';
24335 s = (int)x.s;
24336 zBuf[6] = '0' + (s/10)%10;
24337 zBuf[7] = '0' + (s)%10;
24338 zBuf[8] = 0;
24339 sqlite3_result_text(context, zBuf, 8, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
24340 }
24341}
24342
24343/*
24344** date( TIMESTRING, MOD, MOD, ...)
24345**
24346** Return YYYY-MM-DD
24347*/
24348static void dateFunc(
24349 sqlite3_context *context,
24350 int argc,
24351 sqlite3_value **argv
24352){
24353 DateTime x;
24354 if( isDate(context, argc, argv, &x)==0 ){
24355 int Y;
24356 char zBuf[16];
24357 computeYMD(&x);
24358 Y = x.Y;
24359 if( Y<0 ) Y = -Y;
24360 zBuf[1] = '0' + (Y/1000)%10;
24361 zBuf[2] = '0' + (Y/100)%10;
24362 zBuf[3] = '0' + (Y/10)%10;
24363 zBuf[4] = '0' + (Y)%10;
24364 zBuf[5] = '-';
24365 zBuf[6] = '0' + (x.M/10)%10;
24366 zBuf[7] = '0' + (x.M)%10;
24367 zBuf[8] = '-';
24368 zBuf[9] = '0' + (x.D/10)%10;
24369 zBuf[10] = '0' + (x.D)%10;
24370 zBuf[11] = 0;
24371 if( x.Y<0 ){
24372 zBuf[0] = '-';
24373 sqlite3_result_text(context, zBuf, 11, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
24374 }else{
24375 sqlite3_result_text(context, &zBuf[1], 10, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
24376 }
24377 }
24378}
24379
24380/*
24381** strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
24382**
24383** Return a string described by FORMAT. Conversions as follows:
24384**
24385** %d day of month
24386** %f ** fractional seconds SS.SSS
24387** %H hour 00-24
24388** %j day of year 000-366
24389** %J ** julian day number
24390** %m month 01-12
24391** %M minute 00-59
24392** %s seconds since 1970-01-01
24393** %S seconds 00-59
24394** %w day of week 0-6 sunday==0
24395** %W week of year 00-53
24396** %Y year 0000-9999
24397** %% %
24398*/
24399static void strftimeFunc(
24400 sqlite3_context *context,
24401 int argc,
24402 sqlite3_value **argv
24403){
24404 DateTime x;
24405 size_t i,j;
24406 sqlite3 *db;
24407 const char *zFmt;
24408 sqlite3_str sRes;
24409
24410
24411 if( argc==0 ) return;
24412 zFmt = (const char*)sqlite3_value_text(argv[0]);
24413 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
24414 db = sqlite3_context_db_handle(context);
24415 sqlite3StrAccumInit(&sRes, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH0]);
24416
24417 computeJD(&x);
24418 computeYMD_HMS(&x);
24419 for(i=j=0; zFmt[i]; i++){
24420 if( zFmt[i]!='%' ) continue;
24421 if( j<i ) sqlite3_str_append(&sRes, zFmt+j, (int)(i-j));
24422 i++;
24423 j = i + 1;
24424 switch( zFmt[i] ){
24425 case 'd': {
24426 sqlite3_str_appendf(&sRes, "%02d", x.D);
24427 break;
24428 }
24429 case 'f': {
24430 double s = x.s;
24431 if( s>59.999 ) s = 59.999;
24432 sqlite3_str_appendf(&sRes, "%06.3f", s);
24433 break;
24434 }
24435 case 'H': {
24436 sqlite3_str_appendf(&sRes, "%02d", x.h);
24437 break;
24438 }
24439 case 'W': /* Fall thru */
24440 case 'j': {
24441 int nDay; /* Number of days since 1st day of year */
24442 DateTime y = x;
24443 y.validJD = 0;
24444 y.M = 1;
24445 y.D = 1;
24446 computeJD(&y);
24447 nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
24448 if( zFmt[i]=='W' ){
24449 int wd; /* 0=Monday, 1=Tuesday, ... 6=Sunday */
24450 wd = (int)(((x.iJD+43200000)/86400000)%7);
24451 sqlite3_str_appendf(&sRes,"%02d",(nDay+7-wd)/7);
24452 }else{
24453 sqlite3_str_appendf(&sRes,"%03d",nDay+1);
24454 }
24455 break;
24456 }
24457 case 'J': {
24458 sqlite3_str_appendf(&sRes,"%.16g",x.iJD/86400000.0);
24459 break;
24460 }
24461 case 'm': {
24462 sqlite3_str_appendf(&sRes,"%02d",x.M);
24463 break;
24464 }
24465 case 'M': {
24466 sqlite3_str_appendf(&sRes,"%02d",x.m);
24467 break;
24468 }
24469 case 's': {
24470 i64 iS = (i64)(x.iJD/1000 - 21086676*(i64)10000);
24471 sqlite3_str_appendf(&sRes,"%lld",iS);
24472 break;
24473 }
24474 case 'S': {
24475 sqlite3_str_appendf(&sRes,"%02d",(int)x.s);
24476 break;
24477 }
24478 case 'w': {
24479 sqlite3_str_appendchar(&sRes, 1,
24480 (char)(((x.iJD+129600000)/86400000) % 7) + '0');
24481 break;
24482 }
24483 case 'Y': {
24484 sqlite3_str_appendf(&sRes,"%04d",x.Y);
24485 break;
24486 }
24487 case '%': {
24488 sqlite3_str_appendchar(&sRes, 1, '%');
24489 break;
24490 }
24491 default: {
24492 sqlite3_str_reset(&sRes);
24493 return;
24494 }
24495 }
24496 }
24497 if( j<i ) sqlite3_str_append(&sRes, zFmt+j, (int)(i-j));
24498 sqlite3ResultStrAccum(context, &sRes);
24499}
24500
24501/*
24502** current_time()
24503**
24504** This function returns the same value as time('now').
24505*/
24506static void ctimeFunc(
24507 sqlite3_context *context,
24508 int NotUsed,
24509 sqlite3_value **NotUsed2
24510){
24511 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
24512 timeFunc(context, 0, 0);
24513}
24514
24515/*
24516** current_date()
24517**
24518** This function returns the same value as date('now').
24519*/
24520static void cdateFunc(
24521 sqlite3_context *context,
24522 int NotUsed,
24523 sqlite3_value **NotUsed2
24524){
24525 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
24526 dateFunc(context, 0, 0);
24527}
24528
24529/*
24530** current_timestamp()
24531**
24532** This function returns the same value as datetime('now').
24533*/
24534static void ctimestampFunc(
24535 sqlite3_context *context,
24536 int NotUsed,
24537 sqlite3_value **NotUsed2
24538){
24539 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
24540 datetimeFunc(context, 0, 0);
24541}
24542#endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
24543
24544#ifdef SQLITE_OMIT_DATETIME_FUNCS
24545/*
24546** If the library is compiled to omit the full-scale date and time
24547** handling (to get a smaller binary), the following minimal version
24548** of the functions current_time(), current_date() and current_timestamp()
24549** are included instead. This is to support column declarations that
24550** include "DEFAULT CURRENT_TIME" etc.
24551**
24552** This function uses the C-library functions time(), gmtime()
24553** and strftime(). The format string to pass to strftime() is supplied
24554** as the user-data for the function.
24555*/
24556static void currentTimeFunc(
24557 sqlite3_context *context,
24558 int argc,
24559 sqlite3_value **argv
24560){
24561 time_t t;
24562 char *zFormat = (char *)sqlite3_user_data(context);
24563 sqlite3_int64 iT;
24564 struct tm *pTm;
24565 struct tm sNow;
24566 char zBuf[20];
24567
24568 UNUSED_PARAMETER(argc)(void)(argc);
24569 UNUSED_PARAMETER(argv)(void)(argv);
24570
24571 iT = sqlite3StmtCurrentTime(context);
24572 if( iT<=0 ) return;
24573 t = iT/1000 - 10000*(sqlite3_int64)21086676;
24574#if HAVE_GMTIME_R
24575 pTm = gmtime_r(&t, &sNow);
24576#else
24577 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2));
24578 pTm = gmtime(&t);
24579 if( pTm ) memcpy(&sNow, pTm, sizeof(sNow));
24580 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2));
24581#endif
24582 if( pTm ){
24583 strftime(zBuf, 20, zFormat, &sNow);
24584 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
24585 }
24586}
24587#endif
24588
24589/*
24590** This function registered all of the above C functions as SQL
24591** functions. This should be the only routine in this file with
24592** external linkage.
24593*/
24594SQLITE_PRIVATEstatic void sqlite3RegisterDateTimeFunctions(void){
24595 static FuncDef aDateTimeFuncs[] = {
24596#ifndef SQLITE_OMIT_DATETIME_FUNCS
24597 PURE_DATE(julianday, -1, 0, 0, juliandayFunc ){-1, 0x00800000| 0x2000|1|0x0800, (void*)&sqlite3Config, 0
, juliandayFunc, 0, 0, 0, "julianday", {0} }
,
24598 PURE_DATE(unixepoch, -1, 0, 0, unixepochFunc ){-1, 0x00800000| 0x2000|1|0x0800, (void*)&sqlite3Config, 0
, unixepochFunc, 0, 0, 0, "unixepoch", {0} }
,
24599 PURE_DATE(date, -1, 0, 0, dateFunc ){-1, 0x00800000| 0x2000|1|0x0800, (void*)&sqlite3Config, 0
, dateFunc, 0, 0, 0, "date", {0} }
,
24600 PURE_DATE(time, -1, 0, 0, timeFunc ){-1, 0x00800000| 0x2000|1|0x0800, (void*)&sqlite3Config, 0
, timeFunc, 0, 0, 0, "time", {0} }
,
24601 PURE_DATE(datetime, -1, 0, 0, datetimeFunc ){-1, 0x00800000| 0x2000|1|0x0800, (void*)&sqlite3Config, 0
, datetimeFunc, 0, 0, 0, "datetime", {0} }
,
24602 PURE_DATE(strftime, -1, 0, 0, strftimeFunc ){-1, 0x00800000| 0x2000|1|0x0800, (void*)&sqlite3Config, 0
, strftimeFunc, 0, 0, 0, "strftime", {0} }
,
24603 DFUNCTION(current_time, 0, 0, 0, ctimeFunc ){0, 0x00800000|0x2000|1, 0, 0, ctimeFunc, 0, 0, 0, "current_time"
, {0} }
,
24604 DFUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc){0, 0x00800000|0x2000|1, 0, 0, ctimestampFunc, 0, 0, 0, "current_timestamp"
, {0} }
,
24605 DFUNCTION(current_date, 0, 0, 0, cdateFunc ){0, 0x00800000|0x2000|1, 0, 0, cdateFunc, 0, 0, 0, "current_date"
, {0} }
,
24606#else
24607 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", }
,
24608 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", }
,
24609 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", }
,
24610#endif
24611 };
24612 sqlite3InsertBuiltinFuncs(aDateTimeFuncs, ArraySize(aDateTimeFuncs)((int)(sizeof(aDateTimeFuncs)/sizeof(aDateTimeFuncs[0]))));
24613}
24614
24615/************** End of date.c ************************************************/
24616/************** Begin file os.c **********************************************/
24617/*
24618** 2005 November 29
24619**
24620** The author disclaims copyright to this source code. In place of
24621** a legal notice, here is a blessing:
24622**
24623** May you do good and not evil.
24624** May you find forgiveness for yourself and forgive others.
24625** May you share freely, never taking more than you give.
24626**
24627******************************************************************************
24628**
24629** This file contains OS interface code that is common to all
24630** architectures.
24631*/
24632/* #include "sqliteInt.h" */
24633
24634/*
24635** If we compile with the SQLITE_TEST macro set, then the following block
24636** of code will give us the ability to simulate a disk I/O error. This
24637** is used for testing the I/O recovery logic.
24638*/
24639#if defined(SQLITE_TEST)
24640SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */
24641SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
24642SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */
24643SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */
24644SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */
24645SQLITE_API int sqlite3_diskfull_pending = 0;
24646SQLITE_API int sqlite3_diskfull = 0;
24647#endif /* defined(SQLITE_TEST) */
24648
24649/*
24650** When testing, also keep a count of the number of open files.
24651*/
24652#if defined(SQLITE_TEST)
24653SQLITE_API int sqlite3_open_file_count = 0;
24654#endif /* defined(SQLITE_TEST) */
24655
24656/*
24657** The default SQLite sqlite3_vfs implementations do not allocate
24658** memory (actually, os_unix.c allocates a small amount of memory
24659** from within OsOpen()), but some third-party implementations may.
24660** So we test the effects of a malloc() failing and the sqlite3OsXXX()
24661** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
24662**
24663** The following functions are instrumented for malloc() failure
24664** testing:
24665**
24666** sqlite3OsRead()
24667** sqlite3OsWrite()
24668** sqlite3OsSync()
24669** sqlite3OsFileSize()
24670** sqlite3OsLock()
24671** sqlite3OsCheckReservedLock()
24672** sqlite3OsFileControl()
24673** sqlite3OsShmMap()
24674** sqlite3OsOpen()
24675** sqlite3OsDelete()
24676** sqlite3OsAccess()
24677** sqlite3OsFullPathname()
24678**
24679*/
24680#if defined(SQLITE_TEST)
24681SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
24682 #define DO_OS_MALLOC_TEST(x) \
24683 if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3JournalIsInMemory(x))) { \
24684 void *pTstAlloc = sqlite3Malloc(10); \
24685 if (!pTstAlloc) return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8)); \
24686 sqlite3_free(pTstAlloc); \
24687 }
24688#else
24689 #define DO_OS_MALLOC_TEST(x)
24690#endif
24691
24692/*
24693** The following routines are convenience wrappers around methods
24694** of the sqlite3_file object. This is mostly just syntactic sugar. All
24695** of this would be completely automatic if SQLite were coded using
24696** C++ instead of plain old C.
24697*/
24698SQLITE_PRIVATEstatic void sqlite3OsClose(sqlite3_file *pId){
24699 if( pId->pMethods ){
24700 pId->pMethods->xClose(pId);
24701 pId->pMethods = 0;
24702 }
24703}
24704SQLITE_PRIVATEstatic int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
24705 DO_OS_MALLOC_TEST(id);
24706 return id->pMethods->xRead(id, pBuf, amt, offset);
24707}
24708SQLITE_PRIVATEstatic int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
24709 DO_OS_MALLOC_TEST(id);
24710 return id->pMethods->xWrite(id, pBuf, amt, offset);
24711}
24712SQLITE_PRIVATEstatic int sqlite3OsTruncate(sqlite3_file *id, i64 size){
24713 return id->pMethods->xTruncate(id, size);
24714}
24715SQLITE_PRIVATEstatic int sqlite3OsSync(sqlite3_file *id, int flags){
24716 DO_OS_MALLOC_TEST(id);
24717 return flags ? id->pMethods->xSync(id, flags) : SQLITE_OK0;
24718}
24719SQLITE_PRIVATEstatic int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
24720 DO_OS_MALLOC_TEST(id);
24721 return id->pMethods->xFileSize(id, pSize);
24722}
24723SQLITE_PRIVATEstatic int sqlite3OsLock(sqlite3_file *id, int lockType){
24724 DO_OS_MALLOC_TEST(id);
24725 return id->pMethods->xLock(id, lockType);
24726}
24727SQLITE_PRIVATEstatic int sqlite3OsUnlock(sqlite3_file *id, int lockType){
24728 return id->pMethods->xUnlock(id, lockType);
24729}
24730SQLITE_PRIVATEstatic int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
24731 DO_OS_MALLOC_TEST(id);
24732 return id->pMethods->xCheckReservedLock(id, pResOut);
24733}
24734
24735/*
24736** Use sqlite3OsFileControl() when we are doing something that might fail
24737** and we need to know about the failures. Use sqlite3OsFileControlHint()
24738** when simply tossing information over the wall to the VFS and we do not
24739** really care if the VFS receives and understands the information since it
24740** is only a hint and can be safely ignored. The sqlite3OsFileControlHint()
24741** routine has no return value since the return value would be meaningless.
24742*/
24743SQLITE_PRIVATEstatic int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
24744 if( id->pMethods==0 ) return SQLITE_NOTFOUND12;
24745#ifdef SQLITE_TEST
24746 if( op!=SQLITE_FCNTL_COMMIT_PHASETWO22
24747 && op!=SQLITE_FCNTL_LOCK_TIMEOUT34
24748 && op!=SQLITE_FCNTL_CKPT_DONE37
24749 && op!=SQLITE_FCNTL_CKPT_START39
24750 ){
24751 /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
24752 ** is using a regular VFS, it is called after the corresponding
24753 ** transaction has been committed. Injecting a fault at this point
24754 ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
24755 ** but the transaction is committed anyway.
24756 **
24757 ** The core must call OsFileControl() though, not OsFileControlHint(),
24758 ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably
24759 ** means the commit really has failed and an error should be returned
24760 ** to the user.
24761 **
24762 ** The CKPT_DONE and CKPT_START file-controls are write-only signals
24763 ** to the cksumvfs. Their return code is meaningless and is ignored
24764 ** by the SQLite core, so there is no point in simulating OOMs for them.
24765 */
24766 DO_OS_MALLOC_TEST(id);
24767 }
24768#endif
24769 return id->pMethods->xFileControl(id, op, pArg);
24770}
24771SQLITE_PRIVATEstatic void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
24772 if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg);
24773}
24774
24775SQLITE_PRIVATEstatic int sqlite3OsSectorSize(sqlite3_file *id){
24776 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
24777 return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE4096);
24778}
24779SQLITE_PRIVATEstatic int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
24780 if( NEVER(id->pMethods==0)(id->pMethods==0) ) return 0;
24781 return id->pMethods->xDeviceCharacteristics(id);
24782}
24783#ifndef SQLITE_OMIT_WAL
24784SQLITE_PRIVATEstatic int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
24785 return id->pMethods->xShmLock(id, offset, n, flags);
24786}
24787SQLITE_PRIVATEstatic void sqlite3OsShmBarrier(sqlite3_file *id){
24788 id->pMethods->xShmBarrier(id);
24789}
24790SQLITE_PRIVATEstatic int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
24791 return id->pMethods->xShmUnmap(id, deleteFlag);
24792}
24793SQLITE_PRIVATEstatic int sqlite3OsShmMap(
24794 sqlite3_file *id, /* Database file handle */
24795 int iPage,
24796 int pgsz,
24797 int bExtend, /* True to extend file if necessary */
24798 void volatile **pp /* OUT: Pointer to mapping */
24799){
24800 DO_OS_MALLOC_TEST(id);
24801 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
24802}
24803#endif /* SQLITE_OMIT_WAL */
24804
24805#if SQLITE_MAX_MMAP_SIZE20971520>0
24806/* The real implementation of xFetch and xUnfetch */
24807SQLITE_PRIVATEstatic int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
24808 DO_OS_MALLOC_TEST(id);
24809 return id->pMethods->xFetch(id, iOff, iAmt, pp);
24810}
24811SQLITE_PRIVATEstatic int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
24812 return id->pMethods->xUnfetch(id, iOff, p);
24813}
24814#else
24815/* No-op stubs to use when memory-mapped I/O is disabled */
24816SQLITE_PRIVATEstatic int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
24817 *pp = 0;
24818 return SQLITE_OK0;
24819}
24820SQLITE_PRIVATEstatic int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
24821 return SQLITE_OK0;
24822}
24823#endif
24824
24825/*
24826** The next group of routines are convenience wrappers around the
24827** VFS methods.
24828*/
24829SQLITE_PRIVATEstatic int sqlite3OsOpen(
24830 sqlite3_vfs *pVfs,
24831 const char *zPath,
24832 sqlite3_file *pFile,
24833 int flags,
24834 int *pFlagsOut
24835){
24836 int rc;
24837 DO_OS_MALLOC_TEST(0);
24838 /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
24839 ** down into the VFS layer. Some SQLITE_OPEN_ flags (for example,
24840 ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
24841 ** reaching the VFS. */
24842 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x1087f7f, pFlagsOut);
24843 assert( rc==SQLITE_OK || pFile->pMethods==0 )((void) (0));
24844 return rc;
24845}
24846SQLITE_PRIVATEstatic int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
24847 DO_OS_MALLOC_TEST(0);
24848 assert( dirSync==0 || dirSync==1 )((void) (0));
24849 return pVfs->xDelete!=0 ? pVfs->xDelete(pVfs, zPath, dirSync) : SQLITE_OK0;
24850}
24851SQLITE_PRIVATEstatic int sqlite3OsAccess(
24852 sqlite3_vfs *pVfs,
24853 const char *zPath,
24854 int flags,
24855 int *pResOut
24856){
24857 DO_OS_MALLOC_TEST(0);
24858 return pVfs->xAccess(pVfs, zPath, flags, pResOut);
24859}
24860SQLITE_PRIVATEstatic int sqlite3OsFullPathname(
24861 sqlite3_vfs *pVfs,
24862 const char *zPath,
24863 int nPathOut,
24864 char *zPathOut
24865){
24866 DO_OS_MALLOC_TEST(0);
24867 zPathOut[0] = 0;
24868 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
24869}
24870#ifndef SQLITE_OMIT_LOAD_EXTENSION1
24871SQLITE_PRIVATEstatic void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
24872 assert( zPath!=0 )((void) (0));
24873 assert( strlen(zPath)<=SQLITE_MAX_PATHLEN )((void) (0)); /* tag-20210611-1 */
24874 return pVfs->xDlOpen(pVfs, zPath);
24875}
24876SQLITE_PRIVATEstatic void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
24877 pVfs->xDlError(pVfs, nByte, zBufOut);
24878}
24879SQLITE_PRIVATEstatic void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
24880 return pVfs->xDlSym(pVfs, pHdle, zSym);
24881}
24882SQLITE_PRIVATEstatic void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
24883 pVfs->xDlClose(pVfs, pHandle);
24884}
24885#endif /* SQLITE_OMIT_LOAD_EXTENSION */
24886SQLITE_PRIVATEstatic int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
24887 if( sqlite3Config.iPrngSeed ){
24888 memset(zBufOut, 0, nByte);
24889 if( ALWAYS(nByte>(signed)sizeof(unsigned))(nByte>(signed)sizeof(unsigned)) ) nByte = sizeof(unsigned int);
24890 memcpy(zBufOut, &sqlite3Config.iPrngSeed, nByte);
24891 return SQLITE_OK0;
24892 }else{
24893 return pVfs->xRandomness(pVfs, nByte, zBufOut);
24894 }
24895
24896}
24897SQLITE_PRIVATEstatic int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
24898 return pVfs->xSleep(pVfs, nMicro);
24899}
24900SQLITE_PRIVATEstatic int sqlite3OsGetLastError(sqlite3_vfs *pVfs){
24901 return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0;
24902}
24903SQLITE_PRIVATEstatic int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
24904 int rc;
24905 /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
24906 ** method to get the current date and time if that method is available
24907 ** (if iVersion is 2 or greater and the function pointer is not NULL) and
24908 ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
24909 ** unavailable.
24910 */
24911 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
24912 rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
24913 }else{
24914 double r;
24915 rc = pVfs->xCurrentTime(pVfs, &r);
24916 *pTimeOut = (sqlite3_int64)(r*86400000.0);
24917 }
24918 return rc;
24919}
24920
24921SQLITE_PRIVATEstatic int sqlite3OsOpenMalloc(
24922 sqlite3_vfs *pVfs,
24923 const char *zFile,
24924 sqlite3_file **ppFile,
24925 int flags,
24926 int *pOutFlags
24927){
24928 int rc;
24929 sqlite3_file *pFile;
24930 pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
24931 if( pFile ){
24932 rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
24933 if( rc!=SQLITE_OK0 ){
24934 sqlite3_free(pFile);
24935 *ppFile = 0;
24936 }else{
24937 *ppFile = pFile;
24938 }
24939 }else{
24940 *ppFile = 0;
24941 rc = SQLITE_NOMEM_BKPT7;
24942 }
24943 assert( *ppFile!=0 || rc!=SQLITE_OK )((void) (0));
24944 return rc;
24945}
24946SQLITE_PRIVATEstatic void sqlite3OsCloseFree(sqlite3_file *pFile){
24947 assert( pFile )((void) (0));
24948 sqlite3OsClose(pFile);
24949 sqlite3_free(pFile);
24950}
24951
24952/*
24953** This function is a wrapper around the OS specific implementation of
24954** sqlite3_os_init(). The purpose of the wrapper is to provide the
24955** ability to simulate a malloc failure, so that the handling of an
24956** error in sqlite3_os_init() by the upper layers can be tested.
24957*/
24958SQLITE_PRIVATEstatic int sqlite3OsInit(void){
24959 void *p = sqlite3_malloc(10);
24960 if( p==0 ) return SQLITE_NOMEM_BKPT7;
24961 sqlite3_free(p);
24962 return sqlite3_os_init();
24963}
24964
24965/*
24966** The list of all registered VFS implementations.
24967*/
24968static sqlite3_vfs * SQLITE_WSD vfsListvfsList = 0;
24969#define vfsListvfsList GLOBAL(sqlite3_vfs *, vfsList)vfsList
24970
24971/*
24972** Locate a VFS by name. If no name is given, simply return the
24973** first VFS on the list.
24974*/
24975SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
24976 sqlite3_vfs *pVfs = 0;
24977#if SQLITE_THREADSAFE2
24978 sqlite3_mutex *mutex;
24979#endif
24980#ifndef SQLITE_OMIT_AUTOINIT
24981 int rc = sqlite3_initialize();
24982 if( rc ) return 0;
24983#endif
24984#if SQLITE_THREADSAFE2
24985 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2);
24986#endif
24987 sqlite3_mutex_enter(mutex);
24988 for(pVfs = vfsListvfsList; pVfs; pVfs=pVfs->pNext){
24989 if( zVfs==0 ) break;
24990 if( strcmp(zVfs, pVfs->zName)==0 ) break;
24991 }
24992 sqlite3_mutex_leave(mutex);
24993 return pVfs;
24994}
24995
24996/*
24997** Unlink a VFS from the linked list
24998*/
24999static void vfsUnlink(sqlite3_vfs *pVfs){
25000 assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN)) )((void) (0));
25001 if( pVfs==0 ){
25002 /* No-op */
25003 }else if( vfsListvfsList==pVfs ){
25004 vfsListvfsList = pVfs->pNext;
25005 }else if( vfsListvfsList ){
25006 sqlite3_vfs *p = vfsListvfsList;
25007 while( p->pNext && p->pNext!=pVfs ){
25008 p = p->pNext;
25009 }
25010 if( p->pNext==pVfs ){
25011 p->pNext = pVfs->pNext;
25012 }
25013 }
25014}
25015
25016/*
25017** Register a VFS with the system. It is harmless to register the same
25018** VFS multiple times. The new VFS becomes the default if makeDflt is
25019** true.
25020*/
25021SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
25022 MUTEX_LOGIC(sqlite3_mutex *mutex;)sqlite3_mutex *mutex;
25023#ifndef SQLITE_OMIT_AUTOINIT
25024 int rc = sqlite3_initialize();
25025 if( rc ) return rc;
25026#endif
25027#ifdef SQLITE_ENABLE_API_ARMOR1
25028 if( pVfs==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(25028);
25029#endif
25030
25031 MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )mutex = sqlite3MutexAlloc(2);
25032 sqlite3_mutex_enter(mutex);
25033 vfsUnlink(pVfs);
25034 if( makeDflt || vfsListvfsList==0 ){
25035 pVfs->pNext = vfsListvfsList;
25036 vfsListvfsList = pVfs;
25037 }else{
25038 pVfs->pNext = vfsListvfsList->pNext;
25039 vfsListvfsList->pNext = pVfs;
25040 }
25041 assert(vfsList)((void) (0));
25042 sqlite3_mutex_leave(mutex);
25043 return SQLITE_OK0;
25044}
25045
25046/*
25047** Unregister a VFS so that it is no longer accessible.
25048*/
25049SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
25050 MUTEX_LOGIC(sqlite3_mutex *mutex;)sqlite3_mutex *mutex;
25051#ifndef SQLITE_OMIT_AUTOINIT
25052 int rc = sqlite3_initialize();
25053 if( rc ) return rc;
25054#endif
25055 MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )mutex = sqlite3MutexAlloc(2);
25056 sqlite3_mutex_enter(mutex);
25057 vfsUnlink(pVfs);
25058 sqlite3_mutex_leave(mutex);
25059 return SQLITE_OK0;
25060}
25061
25062/************** End of os.c **************************************************/
25063/************** Begin file fault.c *******************************************/
25064/*
25065** 2008 Jan 22
25066**
25067** The author disclaims copyright to this source code. In place of
25068** a legal notice, here is a blessing:
25069**
25070** May you do good and not evil.
25071** May you find forgiveness for yourself and forgive others.
25072** May you share freely, never taking more than you give.
25073**
25074*************************************************************************
25075**
25076** This file contains code to support the concept of "benign"
25077** malloc failures (when the xMalloc() or xRealloc() method of the
25078** sqlite3_mem_methods structure fails to allocate a block of memory
25079** and returns 0).
25080**
25081** Most malloc failures are non-benign. After they occur, SQLite
25082** abandons the current operation and returns an error code (usually
25083** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
25084** fatal. For example, if a malloc fails while resizing a hash table, this
25085** is completely recoverable simply by not carrying out the resize. The
25086** hash table will continue to function normally. So a malloc failure
25087** during a hash table resize is a benign fault.
25088*/
25089
25090/* #include "sqliteInt.h" */
25091
25092#ifndef SQLITE_UNTESTABLE
25093
25094/*
25095** Global variables.
25096*/
25097typedef struct BenignMallocHooks BenignMallocHooks;
25098static SQLITE_WSD struct BenignMallocHooks {
25099 void (*xBenignBegin)(void);
25100 void (*xBenignEnd)(void);
25101} sqlite3Hooks = { 0, 0 };
25102
25103/* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks
25104** structure. If writable static data is unsupported on the target,
25105** we have to locate the state vector at run-time. In the more common
25106** case where writable static data is supported, wsdHooks can refer directly
25107** to the "sqlite3Hooks" state vector declared above.
25108*/
25109#ifdef SQLITE_OMIT_WSD
25110# define wsdHooksInit \
25111 BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks)sqlite3Hooks
25112# define wsdHookssqlite3Hooks x[0]
25113#else
25114# define wsdHooksInit
25115# define wsdHookssqlite3Hooks sqlite3Hooks
25116#endif
25117
25118
25119/*
25120** Register hooks to call when sqlite3BeginBenignMalloc() and
25121** sqlite3EndBenignMalloc() are called, respectively.
25122*/
25123SQLITE_PRIVATEstatic void sqlite3BenignMallocHooks(
25124 void (*xBenignBegin)(void),
25125 void (*xBenignEnd)(void)
25126){
25127 wsdHooksInit;
25128 wsdHookssqlite3Hooks.xBenignBegin = xBenignBegin;
25129 wsdHookssqlite3Hooks.xBenignEnd = xBenignEnd;
25130}
25131
25132/*
25133** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
25134** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
25135** indicates that subsequent malloc failures are non-benign.
25136*/
25137SQLITE_PRIVATEstatic void sqlite3BeginBenignMalloc(void){
25138 wsdHooksInit;
25139 if( wsdHookssqlite3Hooks.xBenignBegin ){
25140 wsdHookssqlite3Hooks.xBenignBegin();
25141 }
25142}
25143SQLITE_PRIVATEstatic void sqlite3EndBenignMalloc(void){
25144 wsdHooksInit;
25145 if( wsdHookssqlite3Hooks.xBenignEnd ){
25146 wsdHookssqlite3Hooks.xBenignEnd();
25147 }
25148}
25149
25150#endif /* #ifndef SQLITE_UNTESTABLE */
25151
25152/************** End of fault.c ***********************************************/
25153/************** Begin file mem0.c ********************************************/
25154/*
25155** 2008 October 28
25156**
25157** The author disclaims copyright to this source code. In place of
25158** a legal notice, here is a blessing:
25159**
25160** May you do good and not evil.
25161** May you find forgiveness for yourself and forgive others.
25162** May you share freely, never taking more than you give.
25163**
25164*************************************************************************
25165**
25166** This file contains a no-op memory allocation drivers for use when
25167** SQLITE_ZERO_MALLOC is defined. The allocation drivers implemented
25168** here always fail. SQLite will not operate with these drivers. These
25169** are merely placeholders. Real drivers must be substituted using
25170** sqlite3_config() before SQLite will operate.
25171*/
25172/* #include "sqliteInt.h" */
25173
25174/*
25175** This version of the memory allocator is the default. It is
25176** used when no other memory allocator is specified using compile-time
25177** macros.
25178*/
25179#ifdef SQLITE_ZERO_MALLOC
25180
25181/*
25182** No-op versions of all memory allocation routines
25183*/
25184static void *sqlite3MemMalloc(int nByte){ return 0; }
25185static void sqlite3MemFree(void *pPrior){ return; }
25186static void *sqlite3MemRealloc(void *pPrior, int nByte){ return 0; }
25187static int sqlite3MemSize(void *pPrior){ return 0; }
25188static int sqlite3MemRoundup(int n){ return n; }
25189static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK0; }
25190static void sqlite3MemShutdown(void *NotUsed){ return; }
25191
25192/*
25193** This routine is the only routine in this file with external linkage.
25194**
25195** Populate the low-level memory allocation function pointers in
25196** sqlite3GlobalConfig.m with pointers to the routines in this file.
25197*/
25198SQLITE_PRIVATEstatic void sqlite3MemSetDefault(void){
25199 static const sqlite3_mem_methods defaultMethods = {
25200 sqlite3MemMalloc,
25201 sqlite3MemFree,
25202 sqlite3MemRealloc,
25203 sqlite3MemSize,
25204 sqlite3MemRoundup,
25205 sqlite3MemInit,
25206 sqlite3MemShutdown,
25207 0
25208 };
25209 sqlite3_config(SQLITE_CONFIG_MALLOC4, &defaultMethods);
25210}
25211
25212#endif /* SQLITE_ZERO_MALLOC */
25213
25214/************** End of mem0.c ************************************************/
25215/************** Begin file mem1.c ********************************************/
25216/*
25217** 2007 August 14
25218**
25219** The author disclaims copyright to this source code. In place of
25220** a legal notice, here is a blessing:
25221**
25222** May you do good and not evil.
25223** May you find forgiveness for yourself and forgive others.
25224** May you share freely, never taking more than you give.
25225**
25226*************************************************************************
25227**
25228** This file contains low-level memory allocation drivers for when
25229** SQLite will use the standard C-library malloc/realloc/free interface
25230** to obtain the memory it needs.
25231**
25232** This file contains implementations of the low-level memory allocation
25233** routines specified in the sqlite3_mem_methods object. The content of
25234** this file is only used if SQLITE_SYSTEM_MALLOC is defined. The
25235** SQLITE_SYSTEM_MALLOC macro is defined automatically if neither the
25236** SQLITE_MEMDEBUG nor the SQLITE_WIN32_MALLOC macros are defined. The
25237** default configuration is to use memory allocation routines in this
25238** file.
25239**
25240** C-preprocessor macro summary:
25241**
25242** HAVE_MALLOC_USABLE_SIZE The configure script sets this symbol if
25243** the malloc_usable_size() interface exists
25244** on the target platform. Or, this symbol
25245** can be set manually, if desired.
25246** If an equivalent interface exists by
25247** a different name, using a separate -D
25248** option to rename it.
25249**
25250** SQLITE_WITHOUT_ZONEMALLOC Some older macs lack support for the zone
25251** memory allocator. Set this symbol to enable
25252** building on older macs.
25253**
25254** SQLITE_WITHOUT_MSIZE Set this symbol to disable the use of
25255** _msize() on windows systems. This might
25256** be necessary when compiling for Delphi,
25257** for example.
25258*/
25259/* #include "sqliteInt.h" */
25260
25261/*
25262** This version of the memory allocator is the default. It is
25263** used when no other memory allocator is specified using compile-time
25264** macros.
25265*/
25266#ifdef SQLITE_SYSTEM_MALLOC1
25267#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
25268
25269/*
25270** Use the zone allocator available on apple products unless the
25271** SQLITE_WITHOUT_ZONEMALLOC symbol is defined.
25272*/
25273#include <sys/sysctl.h>
25274#include <malloc/malloc.h>
25275#ifdef SQLITE_MIGHT_BE_SINGLE_CORE
25276#include <libkern/OSAtomic.h>
25277#endif /* SQLITE_MIGHT_BE_SINGLE_CORE */
25278static malloc_zone_t* _sqliteZone_;
25279#define SQLITE_MALLOC(x)malloc(x) malloc_zone_malloc(_sqliteZone_, (x))
25280#define SQLITE_FREE(x)free(x) malloc_zone_free(_sqliteZone_, (x));
25281#define SQLITE_REALLOC(x,y)realloc((x),(y)) malloc_zone_realloc(_sqliteZone_, (x), (y))
25282#define SQLITE_MALLOCSIZE(x) \
25283 (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
25284
25285#else /* if not __APPLE__ */
25286
25287/*
25288** Use standard C library malloc and free on non-Apple systems.
25289** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined.
25290*/
25291#define SQLITE_MALLOC(x)malloc(x) malloc(x)
25292#define SQLITE_FREE(x)free(x) free(x)
25293#define SQLITE_REALLOC(x,y)realloc((x),(y)) realloc((x),(y))
25294
25295/*
25296** The malloc.h header file is needed for malloc_usable_size() function
25297** on some systems (e.g. Linux).
25298*/
25299#if HAVE_MALLOC_H && HAVE_MALLOC_USABLE_SIZE
25300# define SQLITE_USE_MALLOC_H 1
25301# define SQLITE_USE_MALLOC_USABLE_SIZE 1
25302/*
25303** The MSVCRT has malloc_usable_size(), but it is called _msize(). The
25304** use of _msize() is automatic, but can be disabled by compiling with
25305** -DSQLITE_WITHOUT_MSIZE. Using the _msize() function also requires
25306** the malloc.h header file.
25307*/
25308#elif defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)
25309# define SQLITE_USE_MALLOC_H
25310# define SQLITE_USE_MSIZE
25311#endif
25312
25313/*
25314** Include the malloc.h header file, if necessary. Also set define macro
25315** SQLITE_MALLOCSIZE to the appropriate function name, which is _msize()
25316** for MSVC and malloc_usable_size() for most other systems (e.g. Linux).
25317** The memory size function can always be overridden manually by defining
25318** the macro SQLITE_MALLOCSIZE to the desired function name.
25319*/
25320#if defined(SQLITE_USE_MALLOC_H)
25321# include <malloc.h>
25322# if defined(SQLITE_USE_MALLOC_USABLE_SIZE)
25323# if !defined(SQLITE_MALLOCSIZE)
25324# define SQLITE_MALLOCSIZE(x) malloc_usable_size(x)
25325# endif
25326# elif defined(SQLITE_USE_MSIZE)
25327# if !defined(SQLITE_MALLOCSIZE)
25328# define SQLITE_MALLOCSIZE _msize
25329# endif
25330# endif
25331#endif /* defined(SQLITE_USE_MALLOC_H) */
25332
25333#endif /* __APPLE__ or not __APPLE__ */
25334
25335/*
25336** Like malloc(), but remember the size of the allocation
25337** so that we can find it later using sqlite3MemSize().
25338**
25339** For this low-level routine, we are guaranteed that nByte>0 because
25340** cases of nByte<=0 will be intercepted and dealt with by higher level
25341** routines.
25342*/
25343static void *sqlite3MemMalloc(int nByte){
25344#ifdef SQLITE_MALLOCSIZE
25345 void *p;
25346 testcase( ROUND8(nByte)==nByte );
25347 p = SQLITE_MALLOC( nByte )malloc(nByte);
25348 if( p==0 ){
25349 testcase( sqlite3GlobalConfig.xLog!=0 );
25350 sqlite3_log(SQLITE_NOMEM7, "failed to allocate %u bytes of memory", nByte);
25351 }
25352 return p;
25353#else
25354 sqlite3_int64 *p;
25355 assert( nByte>0 )((void) (0));
25356 testcase( ROUND8(nByte)!=nByte );
25357 p = SQLITE_MALLOC( nByte+8 )malloc(nByte+8);
25358 if( p ){
25359 p[0] = nByte;
25360 p++;
25361 }else{
25362 testcase( sqlite3GlobalConfig.xLog!=0 );
25363 sqlite3_log(SQLITE_NOMEM7, "failed to allocate %u bytes of memory", nByte);
25364 }
25365 return (void *)p;
25366#endif
25367}
25368
25369/*
25370** Like free() but works for allocations obtained from sqlite3MemMalloc()
25371** or sqlite3MemRealloc().
25372**
25373** For this low-level routine, we already know that pPrior!=0 since
25374** cases where pPrior==0 will have been intecepted and dealt with
25375** by higher-level routines.
25376*/
25377static void sqlite3MemFree(void *pPrior){
25378#ifdef SQLITE_MALLOCSIZE
25379 SQLITE_FREE(pPrior)free(pPrior);
25380#else
25381 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
25382 assert( pPrior!=0 )((void) (0));
25383 p--;
25384 SQLITE_FREE(p)free(p);
25385#endif
25386}
25387
25388/*
25389** Report the allocated size of a prior return from xMalloc()
25390** or xRealloc().
25391*/
25392static int sqlite3MemSize(void *pPrior){
25393#ifdef SQLITE_MALLOCSIZE
25394 assert( pPrior!=0 )((void) (0));
25395 return (int)SQLITE_MALLOCSIZE(pPrior);
25396#else
25397 sqlite3_int64 *p;
25398 assert( pPrior!=0 )((void) (0));
25399 p = (sqlite3_int64*)pPrior;
25400 p--;
25401 return (int)p[0];
25402#endif
25403}
25404
25405/*
25406** Like realloc(). Resize an allocation previously obtained from
25407** sqlite3MemMalloc().
25408**
25409** For this low-level interface, we know that pPrior!=0. Cases where
25410** pPrior==0 while have been intercepted by higher-level routine and
25411** redirected to xMalloc. Similarly, we know that nByte>0 because
25412** cases where nByte<=0 will have been intercepted by higher-level
25413** routines and redirected to xFree.
25414*/
25415static void *sqlite3MemRealloc(void *pPrior, int nByte){
25416#ifdef SQLITE_MALLOCSIZE
25417 void *p = SQLITE_REALLOC(pPrior, nByte)realloc((pPrior),(nByte));
25418 if( p==0 ){
25419 testcase( sqlite3GlobalConfig.xLog!=0 );
25420 sqlite3_log(SQLITE_NOMEM7,
25421 "failed memory resize %u to %u bytes",
25422 SQLITE_MALLOCSIZE(pPrior), nByte);
25423 }
25424 return p;
25425#else
25426 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
25427 assert( pPrior!=0 && nByte>0 )((void) (0));
25428 assert( nByte==ROUND8(nByte) )((void) (0)); /* EV: R-46199-30249 */
25429 p--;
25430 p = SQLITE_REALLOC(p, nByte+8 )realloc((p),(nByte+8));
25431 if( p ){
25432 p[0] = nByte;
25433 p++;
25434 }else{
25435 testcase( sqlite3GlobalConfig.xLog!=0 );
25436 sqlite3_log(SQLITE_NOMEM7,
25437 "failed memory resize %u to %u bytes",
25438 sqlite3MemSize(pPrior), nByte);
25439 }
25440 return (void*)p;
25441#endif
25442}
25443
25444/*
25445** Round up a request size to the next valid allocation size.
25446*/
25447static int sqlite3MemRoundup(int n){
25448 return ROUND8(n)(((n)+7)&~7);
25449}
25450
25451/*
25452** Initialize this module.
25453*/
25454static int sqlite3MemInit(void *NotUsed){
25455#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
25456 int cpuCount;
25457 size_t len;
25458 if( _sqliteZone_ ){
25459 return SQLITE_OK0;
25460 }
25461 len = sizeof(cpuCount);
25462 /* One usually wants to use hw.acctivecpu for MT decisions, but not here */
25463 sysctlbyname("hw.ncpu", &cpuCount, &len, NULL((void*)0), 0);
25464 if( cpuCount>1 ){
25465 /* defer MT decisions to system malloc */
25466 _sqliteZone_ = malloc_default_zone();
25467 }else{
25468 /* only 1 core, use our own zone to contention over global locks,
25469 ** e.g. we have our own dedicated locks */
25470 _sqliteZone_ = malloc_create_zone(4096, 0);
25471 malloc_set_zone_name(_sqliteZone_, "Sqlite_Heap");
25472 }
25473#endif /* defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC) */
25474 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
25475 return SQLITE_OK0;
25476}
25477
25478/*
25479** Deinitialize this module.
25480*/
25481static void sqlite3MemShutdown(void *NotUsed){
25482 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
25483 return;
25484}
25485
25486/*
25487** This routine is the only routine in this file with external linkage.
25488**
25489** Populate the low-level memory allocation function pointers in
25490** sqlite3GlobalConfig.m with pointers to the routines in this file.
25491*/
25492SQLITE_PRIVATEstatic void sqlite3MemSetDefault(void){
25493 static const sqlite3_mem_methods defaultMethods = {
25494 sqlite3MemMalloc,
25495 sqlite3MemFree,
25496 sqlite3MemRealloc,
25497 sqlite3MemSize,
25498 sqlite3MemRoundup,
25499 sqlite3MemInit,
25500 sqlite3MemShutdown,
25501 0
25502 };
25503 sqlite3_config(SQLITE_CONFIG_MALLOC4, &defaultMethods);
25504}
25505
25506#endif /* SQLITE_SYSTEM_MALLOC */
25507
25508/************** End of mem1.c ************************************************/
25509/************** Begin file mem2.c ********************************************/
25510/*
25511** 2007 August 15
25512**
25513** The author disclaims copyright to this source code. In place of
25514** a legal notice, here is a blessing:
25515**
25516** May you do good and not evil.
25517** May you find forgiveness for yourself and forgive others.
25518** May you share freely, never taking more than you give.
25519**
25520*************************************************************************
25521**
25522** This file contains low-level memory allocation drivers for when
25523** SQLite will use the standard C-library malloc/realloc/free interface
25524** to obtain the memory it needs while adding lots of additional debugging
25525** information to each allocation in order to help detect and fix memory
25526** leaks and memory usage errors.
25527**
25528** This file contains implementations of the low-level memory allocation
25529** routines specified in the sqlite3_mem_methods object.
25530*/
25531/* #include "sqliteInt.h" */
25532
25533/*
25534** This version of the memory allocator is used only if the
25535** SQLITE_MEMDEBUG macro is defined
25536*/
25537#ifdef SQLITE_MEMDEBUG
25538
25539/*
25540** The backtrace functionality is only available with GLIBC
25541*/
25542#ifdef __GLIBC__2
25543 extern int backtrace(void**,int);
25544 extern void backtrace_symbols_fd(void*const*,int,int);
25545#else
25546# define backtrace(A,B) 1
25547# define backtrace_symbols_fd(A,B,C)
25548#endif
25549/* #include <stdio.h> */
25550
25551/*
25552** Each memory allocation looks like this:
25553**
25554** ------------------------------------------------------------------------
25555** | Title | backtrace pointers | MemBlockHdr | allocation | EndGuard |
25556** ------------------------------------------------------------------------
25557**
25558** The application code sees only a pointer to the allocation. We have
25559** to back up from the allocation pointer to find the MemBlockHdr. The
25560** MemBlockHdr tells us the size of the allocation and the number of
25561** backtrace pointers. There is also a guard word at the end of the
25562** MemBlockHdr.
25563*/
25564struct MemBlockHdr {
25565 i64 iSize; /* Size of this allocation */
25566 struct MemBlockHdr *pNext, *pPrev; /* Linked list of all unfreed memory */
25567 char nBacktrace; /* Number of backtraces on this alloc */
25568 char nBacktraceSlots; /* Available backtrace slots */
25569 u8 nTitle; /* Bytes of title; includes '\0' */
25570 u8 eType; /* Allocation type code */
25571 int iForeGuard; /* Guard word for sanity */
25572};
25573
25574/*
25575** Guard words
25576*/
25577#define FOREGUARD 0x80F5E153
25578#define REARGUARD 0xE4676B53
25579
25580/*
25581** Number of malloc size increments to track.
25582*/
25583#define NCSIZE 1000
25584
25585/*
25586** All of the static variables used by this module are collected
25587** into a single structure named "mem". This is to keep the
25588** static variables organized and to reduce namespace pollution
25589** when this module is combined with other in the amalgamation.
25590*/
25591static struct {
25592
25593 /*
25594 ** Mutex to control access to the memory allocation subsystem.
25595 */
25596 sqlite3_mutex *mutex;
25597
25598 /*
25599 ** Head and tail of a linked list of all outstanding allocations
25600 */
25601 struct MemBlockHdr *pFirst;
25602 struct MemBlockHdr *pLast;
25603
25604 /*
25605 ** The number of levels of backtrace to save in new allocations.
25606 */
25607 int nBacktrace;
25608 void (*xBacktrace)(int, int, void **);
25609
25610 /*
25611 ** Title text to insert in front of each block
25612 */
25613 int nTitle; /* Bytes of zTitle to save. Includes '\0' and padding */
25614 char zTitle[100]; /* The title text */
25615
25616 /*
25617 ** sqlite3MallocDisallow() increments the following counter.
25618 ** sqlite3MallocAllow() decrements it.
25619 */
25620 int disallow; /* Do not allow memory allocation */
25621
25622 /*
25623 ** Gather statistics on the sizes of memory allocations.
25624 ** nAlloc[i] is the number of allocation attempts of i*8
25625 ** bytes. i==NCSIZE is the number of allocation attempts for
25626 ** sizes more than NCSIZE*8 bytes.
25627 */
25628 int nAlloc[NCSIZE]; /* Total number of allocations */
25629 int nCurrent[NCSIZE]; /* Current number of allocations */
25630 int mxCurrent[NCSIZE]; /* Highwater mark for nCurrent */
25631
25632} mem;
25633
25634
25635/*
25636** Adjust memory usage statistics
25637*/
25638static void adjustStats(int iSize, int increment){
25639 int i = ROUND8(iSize)(((iSize)+7)&~7)/8;
25640 if( i>NCSIZE-1 ){
25641 i = NCSIZE - 1;
25642 }
25643 if( increment>0 ){
25644 mem.nAlloc[i]++;
25645 mem.nCurrent[i]++;
25646 if( mem.nCurrent[i]>mem.mxCurrent[i] ){
25647 mem.mxCurrent[i] = mem.nCurrent[i];
25648 }
25649 }else{
25650 mem.nCurrent[i]--;
25651 assert( mem.nCurrent[i]>=0 )((void) (0));
25652 }
25653}
25654
25655/*
25656** Given an allocation, find the MemBlockHdr for that allocation.
25657**
25658** This routine checks the guards at either end of the allocation and
25659** if they are incorrect it asserts.
25660*/
25661static struct MemBlockHdr *sqlite3MemsysGetHeader(const void *pAllocation){
25662 struct MemBlockHdr *p;
25663 int *pInt;
25664 u8 *pU8;
25665 int nReserve;
25666
25667 p = (struct MemBlockHdr*)pAllocation;
25668 p--;
25669 assert( p->iForeGuard==(int)FOREGUARD )((void) (0));
25670 nReserve = ROUND8(p->iSize)(((p->iSize)+7)&~7);
25671 pInt = (int*)pAllocation;
25672 pU8 = (u8*)pAllocation;
25673 assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD )((void) (0));
25674 /* This checks any of the "extra" bytes allocated due
25675 ** to rounding up to an 8 byte boundary to ensure
25676 ** they haven't been overwritten.
25677 */
25678 while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 )((void) (0));
25679 return p;
25680}
25681
25682/*
25683** Return the number of bytes currently allocated at address p.
25684*/
25685static int sqlite3MemSize(void *p){
25686 struct MemBlockHdr *pHdr;
25687 if( !p ){
25688 return 0;
25689 }
25690 pHdr = sqlite3MemsysGetHeader(p);
25691 return (int)pHdr->iSize;
25692}
25693
25694/*
25695** Initialize the memory allocation subsystem.
25696*/
25697static int sqlite3MemInit(void *NotUsed){
25698 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
25699 assert( (sizeof(struct MemBlockHdr)&7) == 0 )((void) (0));
25700 if( !sqlite3GlobalConfigsqlite3Config.bMemstat ){
25701 /* If memory status is enabled, then the malloc.c wrapper will already
25702 ** hold the STATIC_MEM mutex when the routines here are invoked. */
25703 mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM3);
25704 }
25705 return SQLITE_OK0;
25706}
25707
25708/*
25709** Deinitialize the memory allocation subsystem.
25710*/
25711static void sqlite3MemShutdown(void *NotUsed){
25712 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
25713 mem.mutex = 0;
25714}
25715
25716/*
25717** Round up a request size to the next valid allocation size.
25718*/
25719static int sqlite3MemRoundup(int n){
25720 return ROUND8(n)(((n)+7)&~7);
25721}
25722
25723/*
25724** Fill a buffer with pseudo-random bytes. This is used to preset
25725** the content of a new memory allocation to unpredictable values and
25726** to clear the content of a freed allocation to unpredictable values.
25727*/
25728static void randomFill(char *pBuf, int nByte){
25729 unsigned int x, y, r;
25730 x = SQLITE_PTR_TO_INT(pBuf)((int)(long int)(pBuf));
25731 y = nByte | 1;
25732 while( nByte >= 4 ){
25733 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
25734 y = y*1103515245 + 12345;
25735 r = x ^ y;
25736 *(int*)pBuf = r;
25737 pBuf += 4;
25738 nByte -= 4;
25739 }
25740 while( nByte-- > 0 ){
25741 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
25742 y = y*1103515245 + 12345;
25743 r = x ^ y;
25744 *(pBuf++) = r & 0xff;
25745 }
25746}
25747
25748/*
25749** Allocate nByte bytes of memory.
25750*/
25751static void *sqlite3MemMalloc(int nByte){
25752 struct MemBlockHdr *pHdr;
25753 void **pBt;
25754 char *z;
25755 int *pInt;
25756 void *p = 0;
25757 int totalSize;
25758 int nReserve;
25759 sqlite3_mutex_enter(mem.mutex);
25760 assert( mem.disallow==0 )((void) (0));
25761 nReserve = ROUND8(nByte)(((nByte)+7)&~7);
25762 totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
25763 mem.nBacktrace*sizeof(void*) + mem.nTitle;
25764 p = malloc(totalSize);
25765 if( p ){
25766 z = p;
25767 pBt = (void**)&z[mem.nTitle];
25768 pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
25769 pHdr->pNext = 0;
25770 pHdr->pPrev = mem.pLast;
25771 if( mem.pLast ){
25772 mem.pLast->pNext = pHdr;
25773 }else{
25774 mem.pFirst = pHdr;
25775 }
25776 mem.pLast = pHdr;
25777 pHdr->iForeGuard = FOREGUARD;
25778 pHdr->eType = MEMTYPE_HEAP0x01;
25779 pHdr->nBacktraceSlots = mem.nBacktrace;
25780 pHdr->nTitle = mem.nTitle;
25781 if( mem.nBacktrace ){
25782 void *aAddr[40];
25783 pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
25784 memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
25785 assert(pBt[0])((void) (0));
25786 if( mem.xBacktrace ){
25787 mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
25788 }
25789 }else{
25790 pHdr->nBacktrace = 0;
25791 }
25792 if( mem.nTitle ){
25793 memcpy(z, mem.zTitle, mem.nTitle);
25794 }
25795 pHdr->iSize = nByte;
25796 adjustStats(nByte, +1);
25797 pInt = (int*)&pHdr[1];
25798 pInt[nReserve/sizeof(int)] = REARGUARD;
25799 randomFill((char*)pInt, nByte);
25800 memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
25801 p = (void*)pInt;
25802 }
25803 sqlite3_mutex_leave(mem.mutex);
25804 return p;
25805}
25806
25807/*
25808** Free memory.
25809*/
25810static void sqlite3MemFree(void *pPrior){
25811 struct MemBlockHdr *pHdr;
25812 void **pBt;
25813 char *z;
25814 assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0((void) (0))
25815 || mem.mutex!=0 )((void) (0));
25816 pHdr = sqlite3MemsysGetHeader(pPrior);
25817 pBt = (void**)pHdr;
25818 pBt -= pHdr->nBacktraceSlots;
25819 sqlite3_mutex_enter(mem.mutex);
25820 if( pHdr->pPrev ){
25821 assert( pHdr->pPrev->pNext==pHdr )((void) (0));
25822 pHdr->pPrev->pNext = pHdr->pNext;
25823 }else{
25824 assert( mem.pFirst==pHdr )((void) (0));
25825 mem.pFirst = pHdr->pNext;
25826 }
25827 if( pHdr->pNext ){
25828 assert( pHdr->pNext->pPrev==pHdr )((void) (0));
25829 pHdr->pNext->pPrev = pHdr->pPrev;
25830 }else{
25831 assert( mem.pLast==pHdr )((void) (0));
25832 mem.pLast = pHdr->pPrev;
25833 }
25834 z = (char*)pBt;
25835 z -= pHdr->nTitle;
25836 adjustStats((int)pHdr->iSize, -1);
25837 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
25838 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle);
25839 free(z);
25840 sqlite3_mutex_leave(mem.mutex);
25841}
25842
25843/*
25844** Change the size of an existing memory allocation.
25845**
25846** For this debugging implementation, we *always* make a copy of the
25847** allocation into a new place in memory. In this way, if the
25848** higher level code is using pointer to the old allocation, it is
25849** much more likely to break and we are much more liking to find
25850** the error.
25851*/
25852static void *sqlite3MemRealloc(void *pPrior, int nByte){
25853 struct MemBlockHdr *pOldHdr;
25854 void *pNew;
25855 assert( mem.disallow==0 )((void) (0));
25856 assert( (nByte & 7)==0 )((void) (0)); /* EV: R-46199-30249 */
25857 pOldHdr = sqlite3MemsysGetHeader(pPrior);
25858 pNew = sqlite3MemMalloc(nByte);
25859 if( pNew ){
25860 memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize));
25861 if( nByte>pOldHdr->iSize ){
25862 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize);
25863 }
25864 sqlite3MemFree(pPrior);
25865 }
25866 return pNew;
25867}
25868
25869/*
25870** Populate the low-level memory allocation function pointers in
25871** sqlite3GlobalConfig.m with pointers to the routines in this file.
25872*/
25873SQLITE_PRIVATEstatic void sqlite3MemSetDefault(void){
25874 static const sqlite3_mem_methods defaultMethods = {
25875 sqlite3MemMalloc,
25876 sqlite3MemFree,
25877 sqlite3MemRealloc,
25878 sqlite3MemSize,
25879 sqlite3MemRoundup,
25880 sqlite3MemInit,
25881 sqlite3MemShutdown,
25882 0
25883 };
25884 sqlite3_config(SQLITE_CONFIG_MALLOC4, &defaultMethods);
25885}
25886
25887/*
25888** Set the "type" of an allocation.
25889*/
25890SQLITE_PRIVATEstatic void sqlite3MemdebugSetType(void *p, u8 eType){
25891 if( p && sqlite3GlobalConfigsqlite3Config.m.xFree==sqlite3MemFree ){
25892 struct MemBlockHdr *pHdr;
25893 pHdr = sqlite3MemsysGetHeader(p);
25894 assert( pHdr->iForeGuard==FOREGUARD )((void) (0));
25895 pHdr->eType = eType;
25896 }
25897}
25898
25899/*
25900** Return TRUE if the mask of type in eType matches the type of the
25901** allocation p. Also return true if p==NULL.
25902**
25903** This routine is designed for use within an assert() statement, to
25904** verify the type of an allocation. For example:
25905**
25906** assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
25907*/
25908SQLITE_PRIVATEstatic int sqlite3MemdebugHasType(const void *p, u8 eType)1{
25909 int rc = 1;
25910 if( p && sqlite3GlobalConfigsqlite3Config.m.xFree==sqlite3MemFree ){
25911 struct MemBlockHdr *pHdr;
25912 pHdr = sqlite3MemsysGetHeader(p);
25913 assert( pHdr->iForeGuard==FOREGUARD )((void) (0)); /* Allocation is valid */
25914 if( (pHdr->eType&eType)==0 ){
25915 rc = 0;
25916 }
25917 }
25918 return rc;
25919}
25920
25921/*
25922** Return TRUE if the mask of type in eType matches no bits of the type of the
25923** allocation p. Also return true if p==NULL.
25924**
25925** This routine is designed for use within an assert() statement, to
25926** verify the type of an allocation. For example:
25927**
25928** assert( sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
25929*/
25930SQLITE_PRIVATEstatic int sqlite3MemdebugNoType(const void *p, u8 eType)1{
25931 int rc = 1;
25932 if( p && sqlite3GlobalConfigsqlite3Config.m.xFree==sqlite3MemFree ){
25933 struct MemBlockHdr *pHdr;
25934 pHdr = sqlite3MemsysGetHeader(p);
25935 assert( pHdr->iForeGuard==FOREGUARD )((void) (0)); /* Allocation is valid */
25936 if( (pHdr->eType&eType)!=0 ){
25937 rc = 0;
25938 }
25939 }
25940 return rc;
25941}
25942
25943/*
25944** Set the number of backtrace levels kept for each allocation.
25945** A value of zero turns off backtracing. The number is always rounded
25946** up to a multiple of 2.
25947*/
25948SQLITE_PRIVATEstatic void sqlite3MemdebugBacktrace(int depth){
25949 if( depth<0 ){ depth = 0; }
25950 if( depth>20 ){ depth = 20; }
25951 depth = (depth+1)&0xfe;
25952 mem.nBacktrace = depth;
25953}
25954
25955SQLITE_PRIVATEstatic void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
25956 mem.xBacktrace = xBacktrace;
25957}
25958
25959/*
25960** Set the title string for subsequent allocations.
25961*/
25962SQLITE_PRIVATEstatic void sqlite3MemdebugSettitle(const char *zTitle){
25963 unsigned int n = sqlite3Strlen30(zTitle) + 1;
25964 sqlite3_mutex_enter(mem.mutex);
25965 if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
25966 memcpy(mem.zTitle, zTitle, n);
25967 mem.zTitle[n] = 0;
25968 mem.nTitle = ROUND8(n)(((n)+7)&~7);
25969 sqlite3_mutex_leave(mem.mutex);
25970}
25971
25972SQLITE_PRIVATEstatic void sqlite3MemdebugSync(){
25973 struct MemBlockHdr *pHdr;
25974 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
25975 void **pBt = (void**)pHdr;
25976 pBt -= pHdr->nBacktraceSlots;
25977 mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
25978 }
25979}
25980
25981/*
25982** Open the file indicated and write a log of all unfreed memory
25983** allocations into that log.
25984*/
25985SQLITE_PRIVATEstatic void sqlite3MemdebugDump(const char *zFilename){
25986 FILE *out;
25987 struct MemBlockHdr *pHdr;
25988 void **pBt;
25989 int i;
25990 out = fopen(zFilename, "w");
25991 if( out==0 ){
25992 fprintf(stderrstderr, "** Unable to output memory debug output log: %s **\n",
25993 zFilename);
25994 return;
25995 }
25996 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
25997 char *z = (char*)pHdr;
25998 z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
25999 fprintf(out, "**** %lld bytes at %p from %s ****\n",
26000 pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
26001 if( pHdr->nBacktrace ){
26002 fflush(out);
26003 pBt = (void**)pHdr;
26004 pBt -= pHdr->nBacktraceSlots;
26005 backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
26006 fprintf(out, "\n");
26007 }
26008 }
26009 fprintf(out, "COUNTS:\n");
26010 for(i=0; i<NCSIZE-1; i++){
26011 if( mem.nAlloc[i] ){
26012 fprintf(out, " %5d: %10d %10d %10d\n",
26013 i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
26014 }
26015 }
26016 if( mem.nAlloc[NCSIZE-1] ){
26017 fprintf(out, " %5d: %10d %10d %10d\n",
26018 NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
26019 mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
26020 }
26021 fclose(out);
26022}
26023
26024/*
26025** Return the number of times sqlite3MemMalloc() has been called.
26026*/
26027SQLITE_PRIVATEstatic int sqlite3MemdebugMallocCount(){
26028 int i;
26029 int nTotal = 0;
26030 for(i=0; i<NCSIZE; i++){
26031 nTotal += mem.nAlloc[i];
26032 }
26033 return nTotal;
26034}
26035
26036
26037#endif /* SQLITE_MEMDEBUG */
26038
26039/************** End of mem2.c ************************************************/
26040/************** Begin file mem3.c ********************************************/
26041/*
26042** 2007 October 14
26043**
26044** The author disclaims copyright to this source code. In place of
26045** a legal notice, here is a blessing:
26046**
26047** May you do good and not evil.
26048** May you find forgiveness for yourself and forgive others.
26049** May you share freely, never taking more than you give.
26050**
26051*************************************************************************
26052** This file contains the C functions that implement a memory
26053** allocation subsystem for use by SQLite.
26054**
26055** This version of the memory allocation subsystem omits all
26056** use of malloc(). The SQLite user supplies a block of memory
26057** before calling sqlite3_initialize() from which allocations
26058** are made and returned by the xMalloc() and xRealloc()
26059** implementations. Once sqlite3_initialize() has been called,
26060** the amount of memory available to SQLite is fixed and cannot
26061** be changed.
26062**
26063** This version of the memory allocation subsystem is included
26064** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
26065*/
26066/* #include "sqliteInt.h" */
26067
26068/*
26069** This version of the memory allocator is only built into the library
26070** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
26071** mean that the library will use a memory-pool by default, just that
26072** it is available. The mempool allocator is activated by calling
26073** sqlite3_config().
26074*/
26075#ifdef SQLITE_ENABLE_MEMSYS3
26076
26077/*
26078** Maximum size (in Mem3Blocks) of a "small" chunk.
26079*/
26080#define MX_SMALL 10
26081
26082
26083/*
26084** Number of freelist hash slots
26085*/
26086#define N_HASH 61
26087
26088/*
26089** A memory allocation (also called a "chunk") consists of two or
26090** more blocks where each block is 8 bytes. The first 8 bytes are
26091** a header that is not returned to the user.
26092**
26093** A chunk is two or more blocks that is either checked out or
26094** free. The first block has format u.hdr. u.hdr.size4x is 4 times the
26095** size of the allocation in blocks if the allocation is free.
26096** The u.hdr.size4x&1 bit is true if the chunk is checked out and
26097** false if the chunk is on the freelist. The u.hdr.size4x&2 bit
26098** is true if the previous chunk is checked out and false if the
26099** previous chunk is free. The u.hdr.prevSize field is the size of
26100** the previous chunk in blocks if the previous chunk is on the
26101** freelist. If the previous chunk is checked out, then
26102** u.hdr.prevSize can be part of the data for that chunk and should
26103** not be read or written.
26104**
26105** We often identify a chunk by its index in mem3.aPool[]. When
26106** this is done, the chunk index refers to the second block of
26107** the chunk. In this way, the first chunk has an index of 1.
26108** A chunk index of 0 means "no such chunk" and is the equivalent
26109** of a NULL pointer.
26110**
26111** The second block of free chunks is of the form u.list. The
26112** two fields form a double-linked list of chunks of related sizes.
26113** Pointers to the head of the list are stored in mem3.aiSmall[]
26114** for smaller chunks and mem3.aiHash[] for larger chunks.
26115**
26116** The second block of a chunk is user data if the chunk is checked
26117** out. If a chunk is checked out, the user data may extend into
26118** the u.hdr.prevSize value of the following chunk.
26119*/
26120typedef struct Mem3Block Mem3Block;
26121struct Mem3Block {
26122 union {
26123 struct {
26124 u32 prevSize; /* Size of previous chunk in Mem3Block elements */
26125 u32 size4x; /* 4x the size of current chunk in Mem3Block elements */
26126 } hdr;
26127 struct {
26128 u32 next; /* Index in mem3.aPool[] of next free chunk */
26129 u32 prev; /* Index in mem3.aPool[] of previous free chunk */
26130 } list;
26131 } u;
26132};
26133
26134/*
26135** All of the static variables used by this module are collected
26136** into a single structure named "mem3". This is to keep the
26137** static variables organized and to reduce namespace pollution
26138** when this module is combined with other in the amalgamation.
26139*/
26140static SQLITE_WSD struct Mem3Global {
26141 /*
26142 ** Memory available for allocation. nPool is the size of the array
26143 ** (in Mem3Blocks) pointed to by aPool less 2.
26144 */
26145 u32 nPool;
26146 Mem3Block *aPool;
26147
26148 /*
26149 ** True if we are evaluating an out-of-memory callback.
26150 */
26151 int alarmBusy;
26152
26153 /*
26154 ** Mutex to control access to the memory allocation subsystem.
26155 */
26156 sqlite3_mutex *mutex;
26157
26158 /*
26159 ** The minimum amount of free space that we have seen.
26160 */
26161 u32 mnKeyBlk;
26162
26163 /*
26164 ** iKeyBlk is the index of the key chunk. Most new allocations
26165 ** occur off of this chunk. szKeyBlk is the size (in Mem3Blocks)
26166 ** of the current key chunk. iKeyBlk is 0 if there is no key chunk.
26167 ** The key chunk is not in either the aiHash[] or aiSmall[].
26168 */
26169 u32 iKeyBlk;
26170 u32 szKeyBlk;
26171
26172 /*
26173 ** Array of lists of free blocks according to the block size
26174 ** for smaller chunks, or a hash on the block size for larger
26175 ** chunks.
26176 */
26177 u32 aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */
26178 u32 aiHash[N_HASH]; /* For sizes MX_SMALL+1 and larger */
26179} mem3 = { 97535575 };
26180
26181#define mem3 GLOBAL(struct Mem3Global, mem3)mem3
26182
26183/*
26184** Unlink the chunk at mem3.aPool[i] from list it is currently
26185** on. *pRoot is the list that i is a member of.
26186*/
26187static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
26188 u32 next = mem3.aPool[i].u.list.next;
26189 u32 prev = mem3.aPool[i].u.list.prev;
26190 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
26191 if( prev==0 ){
26192 *pRoot = next;
26193 }else{
26194 mem3.aPool[prev].u.list.next = next;
26195 }
26196 if( next ){
26197 mem3.aPool[next].u.list.prev = prev;
26198 }
26199 mem3.aPool[i].u.list.next = 0;
26200 mem3.aPool[i].u.list.prev = 0;
26201}
26202
26203/*
26204** Unlink the chunk at index i from
26205** whatever list is currently a member of.
26206*/
26207static void memsys3Unlink(u32 i){
26208 u32 size, hash;
26209 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
26210 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 )((void) (0));
26211 assert( i>=1 )((void) (0));
26212 size = mem3.aPool[i-1].u.hdr.size4x/4;
26213 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize )((void) (0));
26214 assert( size>=2 )((void) (0));
26215 if( size <= MX_SMALL ){
26216 memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
26217 }else{
26218 hash = size % N_HASH;
26219 memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
26220 }
26221}
26222
26223/*
26224** Link the chunk at mem3.aPool[i] so that is on the list rooted
26225** at *pRoot.
26226*/
26227static void memsys3LinkIntoList(u32 i, u32 *pRoot){
26228 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
26229 mem3.aPool[i].u.list.next = *pRoot;
26230 mem3.aPool[i].u.list.prev = 0;
26231 if( *pRoot ){
26232 mem3.aPool[*pRoot].u.list.prev = i;
26233 }
26234 *pRoot = i;
26235}
26236
26237/*
26238** Link the chunk at index i into either the appropriate
26239** small chunk list, or into the large chunk hash table.
26240*/
26241static void memsys3Link(u32 i){
26242 u32 size, hash;
26243 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
26244 assert( i>=1 )((void) (0));
26245 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 )((void) (0));
26246 size = mem3.aPool[i-1].u.hdr.size4x/4;
26247 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize )((void) (0));
26248 assert( size>=2 )((void) (0));
26249 if( size <= MX_SMALL ){
26250 memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
26251 }else{
26252 hash = size % N_HASH;
26253 memsys3LinkIntoList(i, &mem3.aiHash[hash]);
26254 }
26255}
26256
26257/*
26258** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
26259** will already be held (obtained by code in malloc.c) if
26260** sqlite3GlobalConfig.bMemStat is true.
26261*/
26262static void memsys3Enter(void){
26263 if( sqlite3GlobalConfigsqlite3Config.bMemstat==0 && mem3.mutex==0 ){
26264 mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM3);
26265 }
26266 sqlite3_mutex_enter(mem3.mutex);
26267}
26268static void memsys3Leave(void){
26269 sqlite3_mutex_leave(mem3.mutex);
26270}
26271
26272/*
26273** Called when we are unable to satisfy an allocation of nBytes.
26274*/
26275static void memsys3OutOfMemory(int nByte){
26276 if( !mem3.alarmBusy ){
26277 mem3.alarmBusy = 1;
26278 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
26279 sqlite3_mutex_leave(mem3.mutex);
26280 sqlite3_release_memory(nByte);
26281 sqlite3_mutex_enter(mem3.mutex);
26282 mem3.alarmBusy = 0;
26283 }
26284}
26285
26286
26287/*
26288** Chunk i is a free chunk that has been unlinked. Adjust its
26289** size parameters for check-out and return a pointer to the
26290** user portion of the chunk.
26291*/
26292static void *memsys3Checkout(u32 i, u32 nBlock){
26293 u32 x;
26294 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
26295 assert( i>=1 )((void) (0));
26296 assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock )((void) (0));
26297 assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock )((void) (0));
26298 x = mem3.aPool[i-1].u.hdr.size4x;
26299 mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
26300 mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
26301 mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
26302 return &mem3.aPool[i];
26303}
26304
26305/*
26306** Carve a piece off of the end of the mem3.iKeyBlk free chunk.
26307** Return a pointer to the new allocation. Or, if the key chunk
26308** is not large enough, return 0.
26309*/
26310static void *memsys3FromKeyBlk(u32 nBlock){
26311 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
26312 assert( mem3.szKeyBlk>=nBlock )((void) (0));
26313 if( nBlock>=mem3.szKeyBlk-1 ){
26314 /* Use the entire key chunk */
26315 void *p = memsys3Checkout(mem3.iKeyBlk, mem3.szKeyBlk);
26316 mem3.iKeyBlk = 0;
26317 mem3.szKeyBlk = 0;
26318 mem3.mnKeyBlk = 0;
26319 return p;
26320 }else{
26321 /* Split the key block. Return the tail. */
26322 u32 newi, x;
26323 newi = mem3.iKeyBlk + mem3.szKeyBlk - nBlock;
26324 assert( newi > mem3.iKeyBlk+1 )((void) (0));
26325 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = nBlock;
26326 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x |= 2;
26327 mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
26328 mem3.szKeyBlk -= nBlock;
26329 mem3.aPool[newi-1].u.hdr.prevSize = mem3.szKeyBlk;
26330 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
26331 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
26332 if( mem3.szKeyBlk < mem3.mnKeyBlk ){
26333 mem3.mnKeyBlk = mem3.szKeyBlk;
26334 }
26335 return (void*)&mem3.aPool[newi];
26336 }
26337}
26338
26339/*
26340** *pRoot is the head of a list of free chunks of the same size
26341** or same size hash. In other words, *pRoot is an entry in either
26342** mem3.aiSmall[] or mem3.aiHash[].
26343**
26344** This routine examines all entries on the given list and tries
26345** to coalesce each entries with adjacent free chunks.
26346**
26347** If it sees a chunk that is larger than mem3.iKeyBlk, it replaces
26348** the current mem3.iKeyBlk with the new larger chunk. In order for
26349** this mem3.iKeyBlk replacement to work, the key chunk must be
26350** linked into the hash tables. That is not the normal state of
26351** affairs, of course. The calling routine must link the key
26352** chunk before invoking this routine, then must unlink the (possibly
26353** changed) key chunk once this routine has finished.
26354*/
26355static void memsys3Merge(u32 *pRoot){
26356 u32 iNext, prev, size, i, x;
26357
26358 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
26359 for(i=*pRoot; i>0; i=iNext){
26360 iNext = mem3.aPool[i].u.list.next;
26361 size = mem3.aPool[i-1].u.hdr.size4x;
26362 assert( (size&1)==0 )((void) (0));
26363 if( (size&2)==0 ){
26364 memsys3UnlinkFromList(i, pRoot);
26365 assert( i > mem3.aPool[i-1].u.hdr.prevSize )((void) (0));
26366 prev = i - mem3.aPool[i-1].u.hdr.prevSize;
26367 if( prev==iNext ){
26368 iNext = mem3.aPool[prev].u.list.next;
26369 }
26370 memsys3Unlink(prev);
26371 size = i + size/4 - prev;
26372 x = mem3.aPool[prev-1].u.hdr.size4x & 2;
26373 mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
26374 mem3.aPool[prev+size-1].u.hdr.prevSize = size;
26375 memsys3Link(prev);
26376 i = prev;
26377 }else{
26378 size /= 4;
26379 }
26380 if( size>mem3.szKeyBlk ){
26381 mem3.iKeyBlk = i;
26382 mem3.szKeyBlk = size;
26383 }
26384 }
26385}
26386
26387/*
26388** Return a block of memory of at least nBytes in size.
26389** Return NULL if unable.
26390**
26391** This function assumes that the necessary mutexes, if any, are
26392** already held by the caller. Hence "Unsafe".
26393*/
26394static void *memsys3MallocUnsafe(int nByte){
26395 u32 i;
26396 u32 nBlock;
26397 u32 toFree;
26398
26399 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
26400 assert( sizeof(Mem3Block)==8 )((void) (0));
26401 if( nByte<=12 ){
26402 nBlock = 2;
26403 }else{
26404 nBlock = (nByte + 11)/8;
26405 }
26406 assert( nBlock>=2 )((void) (0));
26407
26408 /* STEP 1:
26409 ** Look for an entry of the correct size in either the small
26410 ** chunk table or in the large chunk hash table. This is
26411 ** successful most of the time (about 9 times out of 10).
26412 */
26413 if( nBlock <= MX_SMALL ){
26414 i = mem3.aiSmall[nBlock-2];
26415 if( i>0 ){
26416 memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
26417 return memsys3Checkout(i, nBlock);
26418 }
26419 }else{
26420 int hash = nBlock % N_HASH;
26421 for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
26422 if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
26423 memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
26424 return memsys3Checkout(i, nBlock);
26425 }
26426 }
26427 }
26428
26429 /* STEP 2:
26430 ** Try to satisfy the allocation by carving a piece off of the end
26431 ** of the key chunk. This step usually works if step 1 fails.
26432 */
26433 if( mem3.szKeyBlk>=nBlock ){
26434 return memsys3FromKeyBlk(nBlock);
26435 }
26436
26437
26438 /* STEP 3:
26439 ** Loop through the entire memory pool. Coalesce adjacent free
26440 ** chunks. Recompute the key chunk as the largest free chunk.
26441 ** Then try again to satisfy the allocation by carving a piece off
26442 ** of the end of the key chunk. This step happens very
26443 ** rarely (we hope!)
26444 */
26445 for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
26446 memsys3OutOfMemory(toFree);
26447 if( mem3.iKeyBlk ){
26448 memsys3Link(mem3.iKeyBlk);
26449 mem3.iKeyBlk = 0;
26450 mem3.szKeyBlk = 0;
26451 }
26452 for(i=0; i<N_HASH; i++){
26453 memsys3Merge(&mem3.aiHash[i]);
26454 }
26455 for(i=0; i<MX_SMALL-1; i++){
26456 memsys3Merge(&mem3.aiSmall[i]);
26457 }
26458 if( mem3.szKeyBlk ){
26459 memsys3Unlink(mem3.iKeyBlk);
26460 if( mem3.szKeyBlk>=nBlock ){
26461 return memsys3FromKeyBlk(nBlock);
26462 }
26463 }
26464 }
26465
26466 /* If none of the above worked, then we fail. */
26467 return 0;
26468}
26469
26470/*
26471** Free an outstanding memory allocation.
26472**
26473** This function assumes that the necessary mutexes, if any, are
26474** already held by the caller. Hence "Unsafe".
26475*/
26476static void memsys3FreeUnsafe(void *pOld){
26477 Mem3Block *p = (Mem3Block*)pOld;
26478 int i;
26479 u32 size, x;
26480 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
26481 assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] )((void) (0));
26482 i = p - mem3.aPool;
26483 assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 )((void) (0));
26484 size = mem3.aPool[i-1].u.hdr.size4x/4;
26485 assert( i+size<=mem3.nPool+1 )((void) (0));
26486 mem3.aPool[i-1].u.hdr.size4x &= ~1;
26487 mem3.aPool[i+size-1].u.hdr.prevSize = size;
26488 mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
26489 memsys3Link(i);
26490
26491 /* Try to expand the key using the newly freed chunk */
26492 if( mem3.iKeyBlk ){
26493 while( (mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x&2)==0 ){
26494 size = mem3.aPool[mem3.iKeyBlk-1].u.hdr.prevSize;
26495 mem3.iKeyBlk -= size;
26496 mem3.szKeyBlk += size;
26497 memsys3Unlink(mem3.iKeyBlk);
26498 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
26499 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
26500 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk;
26501 }
26502 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
26503 while( (mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x&1)==0 ){
26504 memsys3Unlink(mem3.iKeyBlk+mem3.szKeyBlk);
26505 mem3.szKeyBlk += mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x/4;
26506 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
26507 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk;
26508 }
26509 }
26510}
26511
26512/*
26513** Return the size of an outstanding allocation, in bytes. The
26514** size returned omits the 8-byte header overhead. This only
26515** works for chunks that are currently checked out.
26516*/
26517static int memsys3Size(void *p){
26518 Mem3Block *pBlock;
26519 assert( p!=0 )((void) (0));
26520 pBlock = (Mem3Block*)p;
26521 assert( (pBlock[-1].u.hdr.size4x&1)!=0 )((void) (0));
26522 return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
26523}
26524
26525/*
26526** Round up a request size to the next valid allocation size.
26527*/
26528static int memsys3Roundup(int n){
26529 if( n<=12 ){
26530 return 12;
26531 }else{
26532 return ((n+11)&~7) - 4;
26533 }
26534}
26535
26536/*
26537** Allocate nBytes of memory.
26538*/
26539static void *memsys3Malloc(int nBytes){
26540 sqlite3_int64 *p;
26541 assert( nBytes>0 )((void) (0)); /* malloc.c filters out 0 byte requests */
26542 memsys3Enter();
26543 p = memsys3MallocUnsafe(nBytes);
26544 memsys3Leave();
26545 return (void*)p;
26546}
26547
26548/*
26549** Free memory.
26550*/
26551static void memsys3Free(void *pPrior){
26552 assert( pPrior )((void) (0));
26553 memsys3Enter();
26554 memsys3FreeUnsafe(pPrior);
26555 memsys3Leave();
26556}
26557
26558/*
26559** Change the size of an existing memory allocation
26560*/
26561static void *memsys3Realloc(void *pPrior, int nBytes){
26562 int nOld;
26563 void *p;
26564 if( pPrior==0 ){
26565 return sqlite3_malloc(nBytes);
26566 }
26567 if( nBytes<=0 ){
26568 sqlite3_free(pPrior);
26569 return 0;
26570 }
26571 nOld = memsys3Size(pPrior);
26572 if( nBytes<=nOld && nBytes>=nOld-128 ){
26573 return pPrior;
26574 }
26575 memsys3Enter();
26576 p = memsys3MallocUnsafe(nBytes);
26577 if( p ){
26578 if( nOld<nBytes ){
26579 memcpy(p, pPrior, nOld);
26580 }else{
26581 memcpy(p, pPrior, nBytes);
26582 }
26583 memsys3FreeUnsafe(pPrior);
26584 }
26585 memsys3Leave();
26586 return p;
26587}
26588
26589/*
26590** Initialize this module.
26591*/
26592static int memsys3Init(void *NotUsed){
26593 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
26594 if( !sqlite3GlobalConfigsqlite3Config.pHeap ){
26595 return SQLITE_ERROR1;
26596 }
26597
26598 /* Store a pointer to the memory block in global structure mem3. */
26599 assert( sizeof(Mem3Block)==8 )((void) (0));
26600 mem3.aPool = (Mem3Block *)sqlite3GlobalConfigsqlite3Config.pHeap;
26601 mem3.nPool = (sqlite3GlobalConfigsqlite3Config.nHeap / sizeof(Mem3Block)) - 2;
26602
26603 /* Initialize the key block. */
26604 mem3.szKeyBlk = mem3.nPool;
26605 mem3.mnKeyBlk = mem3.szKeyBlk;
26606 mem3.iKeyBlk = 1;
26607 mem3.aPool[0].u.hdr.size4x = (mem3.szKeyBlk<<2) + 2;
26608 mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
26609 mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
26610
26611 return SQLITE_OK0;
26612}
26613
26614/*
26615** Deinitialize this module.
26616*/
26617static void memsys3Shutdown(void *NotUsed){
26618 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
26619 mem3.mutex = 0;
26620 return;
26621}
26622
26623
26624
26625/*
26626** Open the file indicated and write a log of all unfreed memory
26627** allocations into that log.
26628*/
26629SQLITE_PRIVATEstatic void sqlite3Memsys3Dump(const char *zFilename){
26630#ifdef SQLITE_DEBUG
26631 FILE *out;
26632 u32 i, j;
26633 u32 size;
26634 if( zFilename==0 || zFilename[0]==0 ){
26635 out = stdoutstdout;
26636 }else{
26637 out = fopen(zFilename, "w");
26638 if( out==0 ){
26639 fprintf(stderrstderr, "** Unable to output memory debug output log: %s **\n",
26640 zFilename);
26641 return;
26642 }
26643 }
26644 memsys3Enter();
26645 fprintf(out, "CHUNKS:\n");
26646 for(i=1; i<=mem3.nPool; i+=size/4){
26647 size = mem3.aPool[i-1].u.hdr.size4x;
26648 if( size/4<=1 ){
26649 fprintf(out, "%p size error\n", &mem3.aPool[i]);
26650 assert( 0 )((void) (0));
26651 break;
26652 }
26653 if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
26654 fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
26655 assert( 0 )((void) (0));
26656 break;
26657 }
26658 if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
26659 fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
26660 assert( 0 )((void) (0));
26661 break;
26662 }
26663 if( size&1 ){
26664 fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
26665 }else{
26666 fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
26667 i==mem3.iKeyBlk ? " **key**" : "");
26668 }
26669 }
26670 for(i=0; i<MX_SMALL-1; i++){
26671 if( mem3.aiSmall[i]==0 ) continue;
26672 fprintf(out, "small(%2d):", i);
26673 for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
26674 fprintf(out, " %p(%d)", &mem3.aPool[j],
26675 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
26676 }
26677 fprintf(out, "\n");
26678 }
26679 for(i=0; i<N_HASH; i++){
26680 if( mem3.aiHash[i]==0 ) continue;
26681 fprintf(out, "hash(%2d):", i);
26682 for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
26683 fprintf(out, " %p(%d)", &mem3.aPool[j],
26684 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
26685 }
26686 fprintf(out, "\n");
26687 }
26688 fprintf(out, "key=%d\n", mem3.iKeyBlk);
26689 fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szKeyBlk*8);
26690 fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnKeyBlk*8);
26691 sqlite3_mutex_leave(mem3.mutex);
26692 if( out==stdoutstdout ){
26693 fflush(stdoutstdout);
26694 }else{
26695 fclose(out);
26696 }
26697#else
26698 UNUSED_PARAMETER(zFilename)(void)(zFilename);
26699#endif
26700}
26701
26702/*
26703** This routine is the only routine in this file with external
26704** linkage.
26705**
26706** Populate the low-level memory allocation function pointers in
26707** sqlite3GlobalConfig.m with pointers to the routines in this file. The
26708** arguments specify the block of memory to manage.
26709**
26710** This routine is only called by sqlite3_config(), and therefore
26711** is not required to be threadsafe (it is not).
26712*/
26713SQLITE_PRIVATEstatic const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
26714 static const sqlite3_mem_methods mempoolMethods = {
26715 memsys3Malloc,
26716 memsys3Free,
26717 memsys3Realloc,
26718 memsys3Size,
26719 memsys3Roundup,
26720 memsys3Init,
26721 memsys3Shutdown,
26722 0
26723 };
26724 return &mempoolMethods;
26725}
26726
26727#endif /* SQLITE_ENABLE_MEMSYS3 */
26728
26729/************** End of mem3.c ************************************************/
26730/************** Begin file mem5.c ********************************************/
26731/*
26732** 2007 October 14
26733**
26734** The author disclaims copyright to this source code. In place of
26735** a legal notice, here is a blessing:
26736**
26737** May you do good and not evil.
26738** May you find forgiveness for yourself and forgive others.
26739** May you share freely, never taking more than you give.
26740**
26741*************************************************************************
26742** This file contains the C functions that implement a memory
26743** allocation subsystem for use by SQLite.
26744**
26745** This version of the memory allocation subsystem omits all
26746** use of malloc(). The application gives SQLite a block of memory
26747** before calling sqlite3_initialize() from which allocations
26748** are made and returned by the xMalloc() and xRealloc()
26749** implementations. Once sqlite3_initialize() has been called,
26750** the amount of memory available to SQLite is fixed and cannot
26751** be changed.
26752**
26753** This version of the memory allocation subsystem is included
26754** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
26755**
26756** This memory allocator uses the following algorithm:
26757**
26758** 1. All memory allocation sizes are rounded up to a power of 2.
26759**
26760** 2. If two adjacent free blocks are the halves of a larger block,
26761** then the two blocks are coalesced into the single larger block.
26762**
26763** 3. New memory is allocated from the first available free block.
26764**
26765** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
26766** Concerning Dynamic Storage Allocation". Journal of the Association for
26767** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
26768**
26769** Let n be the size of the largest allocation divided by the minimum
26770** allocation size (after rounding all sizes up to a power of 2.) Let M
26771** be the maximum amount of memory ever outstanding at one time. Let
26772** N be the total amount of memory available for allocation. Robson
26773** proved that this memory allocator will never breakdown due to
26774** fragmentation as long as the following constraint holds:
26775**
26776** N >= M*(1 + log2(n)/2) - n + 1
26777**
26778** The sqlite3_status() logic tracks the maximum values of n and M so
26779** that an application can, at any time, verify this constraint.
26780*/
26781/* #include "sqliteInt.h" */
26782
26783/*
26784** This version of the memory allocator is used only when
26785** SQLITE_ENABLE_MEMSYS5 is defined.
26786*/
26787#ifdef SQLITE_ENABLE_MEMSYS5
26788
26789/*
26790** A minimum allocation is an instance of the following structure.
26791** Larger allocations are an array of these structures where the
26792** size of the array is a power of 2.
26793**
26794** The size of this object must be a power of two. That fact is
26795** verified in memsys5Init().
26796*/
26797typedef struct Mem5Link Mem5Link;
26798struct Mem5Link {
26799 int next; /* Index of next free chunk */
26800 int prev; /* Index of previous free chunk */
26801};
26802
26803/*
26804** Maximum size of any allocation is ((1<<LOGMAX)*mem5.szAtom). Since
26805** mem5.szAtom is always at least 8 and 32-bit integers are used,
26806** it is not actually possible to reach this limit.
26807*/
26808#define LOGMAX 30
26809
26810/*
26811** Masks used for mem5.aCtrl[] elements.
26812*/
26813#define CTRL_LOGSIZE 0x1f /* Log2 Size of this block */
26814#define CTRL_FREE 0x20 /* True if not checked out */
26815
26816/*
26817** All of the static variables used by this module are collected
26818** into a single structure named "mem5". This is to keep the
26819** static variables organized and to reduce namespace pollution
26820** when this module is combined with other in the amalgamation.
26821*/
26822static SQLITE_WSD struct Mem5Global {
26823 /*
26824 ** Memory available for allocation
26825 */
26826 int szAtom; /* Smallest possible allocation in bytes */
26827 int nBlock; /* Number of szAtom sized blocks in zPool */
26828 u8 *zPool; /* Memory available to be allocated */
26829
26830 /*
26831 ** Mutex to control access to the memory allocation subsystem.
26832 */
26833 sqlite3_mutex *mutex;
26834
26835#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
26836 /*
26837 ** Performance statistics
26838 */
26839 u64 nAlloc; /* Total number of calls to malloc */
26840 u64 totalAlloc; /* Total of all malloc calls - includes internal frag */
26841 u64 totalExcess; /* Total internal fragmentation */
26842 u32 currentOut; /* Current checkout, including internal fragmentation */
26843 u32 currentCount; /* Current number of distinct checkouts */
26844 u32 maxOut; /* Maximum instantaneous currentOut */
26845 u32 maxCount; /* Maximum instantaneous currentCount */
26846 u32 maxRequest; /* Largest allocation (exclusive of internal frag) */
26847#endif
26848
26849 /*
26850 ** Lists of free blocks. aiFreelist[0] is a list of free blocks of
26851 ** size mem5.szAtom. aiFreelist[1] holds blocks of size szAtom*2.
26852 ** aiFreelist[2] holds free blocks of size szAtom*4. And so forth.
26853 */
26854 int aiFreelist[LOGMAX+1];
26855
26856 /*
26857 ** Space for tracking which blocks are checked out and the size
26858 ** of each block. One byte per block.
26859 */
26860 u8 *aCtrl;
26861
26862} mem5;
26863
26864/*
26865** Access the static variable through a macro for SQLITE_OMIT_WSD.
26866*/
26867#define mem5 GLOBAL(struct Mem5Global, mem5)mem5
26868
26869/*
26870** Assuming mem5.zPool is divided up into an array of Mem5Link
26871** structures, return a pointer to the idx-th such link.
26872*/
26873#define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
26874
26875/*
26876** Unlink the chunk at mem5.aPool[i] from list it is currently
26877** on. It should be found on mem5.aiFreelist[iLogsize].
26878*/
26879static void memsys5Unlink(int i, int iLogsize){
26880 int next, prev;
26881 assert( i>=0 && i<mem5.nBlock )((void) (0));
26882 assert( iLogsize>=0 && iLogsize<=LOGMAX )((void) (0));
26883 assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize )((void) (0));
26884
26885 next = MEM5LINK(i)->next;
26886 prev = MEM5LINK(i)->prev;
26887 if( prev<0 ){
26888 mem5.aiFreelist[iLogsize] = next;
26889 }else{
26890 MEM5LINK(prev)->next = next;
26891 }
26892 if( next>=0 ){
26893 MEM5LINK(next)->prev = prev;
26894 }
26895}
26896
26897/*
26898** Link the chunk at mem5.aPool[i] so that is on the iLogsize
26899** free list.
26900*/
26901static void memsys5Link(int i, int iLogsize){
26902 int x;
26903 assert( sqlite3_mutex_held(mem5.mutex) )((void) (0));
26904 assert( i>=0 && i<mem5.nBlock )((void) (0));
26905 assert( iLogsize>=0 && iLogsize<=LOGMAX )((void) (0));
26906 assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize )((void) (0));
26907
26908 x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
26909 MEM5LINK(i)->prev = -1;
26910 if( x>=0 ){
26911 assert( x<mem5.nBlock )((void) (0));
26912 MEM5LINK(x)->prev = i;
26913 }
26914 mem5.aiFreelist[iLogsize] = i;
26915}
26916
26917/*
26918** Obtain or release the mutex needed to access global data structures.
26919*/
26920static void memsys5Enter(void){
26921 sqlite3_mutex_enter(mem5.mutex);
26922}
26923static void memsys5Leave(void){
26924 sqlite3_mutex_leave(mem5.mutex);
26925}
26926
26927/*
26928** Return the size of an outstanding allocation, in bytes.
26929** This only works for chunks that are currently checked out.
26930*/
26931static int memsys5Size(void *p){
26932 int iSize, i;
26933 assert( p!=0 )((void) (0));
26934 i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom);
26935 assert( i>=0 && i<mem5.nBlock )((void) (0));
26936 iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
26937 return iSize;
26938}
26939
26940/*
26941** Return a block of memory of at least nBytes in size.
26942** Return NULL if unable. Return NULL if nBytes==0.
26943**
26944** The caller guarantees that nByte is positive.
26945**
26946** The caller has obtained a mutex prior to invoking this
26947** routine so there is never any chance that two or more
26948** threads can be in this routine at the same time.
26949*/
26950static void *memsys5MallocUnsafe(int nByte){
26951 int i; /* Index of a mem5.aPool[] slot */
26952 int iBin; /* Index into mem5.aiFreelist[] */
26953 int iFullSz; /* Size of allocation rounded up to power of 2 */
26954 int iLogsize; /* Log2 of iFullSz/POW2_MIN */
26955
26956 /* nByte must be a positive */
26957 assert( nByte>0 )((void) (0));
26958
26959 /* No more than 1GiB per allocation */
26960 if( nByte > 0x40000000 ) return 0;
26961
26962#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
26963 /* Keep track of the maximum allocation request. Even unfulfilled
26964 ** requests are counted */
26965 if( (u32)nByte>mem5.maxRequest ){
26966 mem5.maxRequest = nByte;
26967 }
26968#endif
26969
26970
26971 /* Round nByte up to the next valid power of two */
26972 for(iFullSz=mem5.szAtom,iLogsize=0; iFullSz<nByte; iFullSz*=2,iLogsize++){}
26973
26974 /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
26975 ** block. If not, then split a block of the next larger power of
26976 ** two in order to create a new free block of size iLogsize.
26977 */
26978 for(iBin=iLogsize; iBin<=LOGMAX && mem5.aiFreelist[iBin]<0; iBin++){}
26979 if( iBin>LOGMAX ){
26980 testcase( sqlite3GlobalConfig.xLog!=0 );
26981 sqlite3_log(SQLITE_NOMEM7, "failed to allocate %u bytes", nByte);
26982 return 0;
26983 }
26984 i = mem5.aiFreelist[iBin];
26985 memsys5Unlink(i, iBin);
26986 while( iBin>iLogsize ){
26987 int newSize;
26988
26989 iBin--;
26990 newSize = 1 << iBin;
26991 mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
26992 memsys5Link(i+newSize, iBin);
26993 }
26994 mem5.aCtrl[i] = iLogsize;
26995
26996#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
26997 /* Update allocator performance statistics. */
26998 mem5.nAlloc++;
26999 mem5.totalAlloc += iFullSz;
27000 mem5.totalExcess += iFullSz - nByte;
27001 mem5.currentCount++;
27002 mem5.currentOut += iFullSz;
27003 if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
27004 if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
27005#endif
27006
27007#ifdef SQLITE_DEBUG
27008 /* Make sure the allocated memory does not assume that it is set to zero
27009 ** or retains a value from a previous allocation */
27010 memset(&mem5.zPool[i*mem5.szAtom], 0xAA, iFullSz);
27011#endif
27012
27013 /* Return a pointer to the allocated memory. */
27014 return (void*)&mem5.zPool[i*mem5.szAtom];
27015}
27016
27017/*
27018** Free an outstanding memory allocation.
27019*/
27020static void memsys5FreeUnsafe(void *pOld){
27021 u32 size, iLogsize;
27022 int iBlock;
27023
27024 /* Set iBlock to the index of the block pointed to by pOld in
27025 ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool.
27026 */
27027 iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom);
27028
27029 /* Check that the pointer pOld points to a valid, non-free block. */
27030 assert( iBlock>=0 && iBlock<mem5.nBlock )((void) (0));
27031 assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 )((void) (0));
27032 assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 )((void) (0));
27033
27034 iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
27035 size = 1<<iLogsize;
27036 assert( iBlock+size-1<(u32)mem5.nBlock )((void) (0));
27037
27038 mem5.aCtrl[iBlock] |= CTRL_FREE;
27039 mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
27040
27041#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
27042 assert( mem5.currentCount>0 )((void) (0));
27043 assert( mem5.currentOut>=(size*mem5.szAtom) )((void) (0));
27044 mem5.currentCount--;
27045 mem5.currentOut -= size*mem5.szAtom;
27046 assert( mem5.currentOut>0 || mem5.currentCount==0 )((void) (0));
27047 assert( mem5.currentCount>0 || mem5.currentOut==0 )((void) (0));
27048#endif
27049
27050 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
27051 while( ALWAYS(iLogsize<LOGMAX)(iLogsize<LOGMAX) ){
27052 int iBuddy;
27053 if( (iBlock>>iLogsize) & 1 ){
27054 iBuddy = iBlock - size;
27055 assert( iBuddy>=0 )((void) (0));
27056 }else{
27057 iBuddy = iBlock + size;
27058 if( iBuddy>=mem5.nBlock ) break;
27059 }
27060 if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
27061 memsys5Unlink(iBuddy, iLogsize);
27062 iLogsize++;
27063 if( iBuddy<iBlock ){
27064 mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
27065 mem5.aCtrl[iBlock] = 0;
27066 iBlock = iBuddy;
27067 }else{
27068 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
27069 mem5.aCtrl[iBuddy] = 0;
27070 }
27071 size *= 2;
27072 }
27073
27074#ifdef SQLITE_DEBUG
27075 /* Overwrite freed memory with the 0x55 bit pattern to verify that it is
27076 ** not used after being freed */
27077 memset(&mem5.zPool[iBlock*mem5.szAtom], 0x55, size);
27078#endif
27079
27080 memsys5Link(iBlock, iLogsize);
27081}
27082
27083/*
27084** Allocate nBytes of memory.
27085*/
27086static void *memsys5Malloc(int nBytes){
27087 sqlite3_int64 *p = 0;
27088 if( nBytes>0 ){
27089 memsys5Enter();
27090 p = memsys5MallocUnsafe(nBytes);
27091 memsys5Leave();
27092 }
27093 return (void*)p;
27094}
27095
27096/*
27097** Free memory.
27098**
27099** The outer layer memory allocator prevents this routine from
27100** being called with pPrior==0.
27101*/
27102static void memsys5Free(void *pPrior){
27103 assert( pPrior!=0 )((void) (0));
27104 memsys5Enter();
27105 memsys5FreeUnsafe(pPrior);
27106 memsys5Leave();
27107}
27108
27109/*
27110** Change the size of an existing memory allocation.
27111**
27112** The outer layer memory allocator prevents this routine from
27113** being called with pPrior==0.
27114**
27115** nBytes is always a value obtained from a prior call to
27116** memsys5Round(). Hence nBytes is always a non-negative power
27117** of two. If nBytes==0 that means that an oversize allocation
27118** (an allocation larger than 0x40000000) was requested and this
27119** routine should return 0 without freeing pPrior.
27120*/
27121static void *memsys5Realloc(void *pPrior, int nBytes){
27122 int nOld;
27123 void *p;
27124 assert( pPrior!=0 )((void) (0));
27125 assert( (nBytes&(nBytes-1))==0 )((void) (0)); /* EV: R-46199-30249 */
27126 assert( nBytes>=0 )((void) (0));
27127 if( nBytes==0 ){
27128 return 0;
27129 }
27130 nOld = memsys5Size(pPrior);
27131 if( nBytes<=nOld ){
27132 return pPrior;
27133 }
27134 p = memsys5Malloc(nBytes);
27135 if( p ){
27136 memcpy(p, pPrior, nOld);
27137 memsys5Free(pPrior);
27138 }
27139 return p;
27140}
27141
27142/*
27143** Round up a request size to the next valid allocation size. If
27144** the allocation is too large to be handled by this allocation system,
27145** return 0.
27146**
27147** All allocations must be a power of two and must be expressed by a
27148** 32-bit signed integer. Hence the largest allocation is 0x40000000
27149** or 1073741824 bytes.
27150*/
27151static int memsys5Roundup(int n){
27152 int iFullSz;
27153 if( n<=mem5.szAtom*2 ){
27154 if( n<=mem5.szAtom ) return mem5.szAtom;
27155 return mem5.szAtom*2;
27156 }
27157 if( n>0x40000000 ) return 0;
27158 for(iFullSz=mem5.szAtom*8; iFullSz<n; iFullSz *= 4);
27159 if( (iFullSz/2)>=n ) return iFullSz/2;
27160 return iFullSz;
27161}
27162
27163/*
27164** Return the ceiling of the logarithm base 2 of iValue.
27165**
27166** Examples: memsys5Log(1) -> 0
27167** memsys5Log(2) -> 1
27168** memsys5Log(4) -> 2
27169** memsys5Log(5) -> 3
27170** memsys5Log(8) -> 3
27171** memsys5Log(9) -> 4
27172*/
27173static int memsys5Log(int iValue){
27174 int iLog;
27175 for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
27176 return iLog;
27177}
27178
27179/*
27180** Initialize the memory allocator.
27181**
27182** This routine is not threadsafe. The caller must be holding a mutex
27183** to prevent multiple threads from entering at the same time.
27184*/
27185static int memsys5Init(void *NotUsed){
27186 int ii; /* Loop counter */
27187 int nByte; /* Number of bytes of memory available to this allocator */
27188 u8 *zByte; /* Memory usable by this allocator */
27189 int nMinLog; /* Log base 2 of minimum allocation size in bytes */
27190 int iOffset; /* An offset into mem5.aCtrl[] */
27191
27192 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
27193
27194 /* For the purposes of this routine, disable the mutex */
27195 mem5.mutex = 0;
27196
27197 /* The size of a Mem5Link object must be a power of two. Verify that
27198 ** this is case.
27199 */
27200 assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 )((void) (0));
27201
27202 nByte = sqlite3GlobalConfigsqlite3Config.nHeap;
27203 zByte = (u8*)sqlite3GlobalConfigsqlite3Config.pHeap;
27204 assert( zByte!=0 )((void) (0)); /* sqlite3_config() does not allow otherwise */
27205
27206 /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
27207 nMinLog = memsys5Log(sqlite3GlobalConfigsqlite3Config.mnReq);
27208 mem5.szAtom = (1<<nMinLog);
27209 while( (int)sizeof(Mem5Link)>mem5.szAtom ){
27210 mem5.szAtom = mem5.szAtom << 1;
27211 }
27212
27213 mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
27214 mem5.zPool = zByte;
27215 mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
27216
27217 for(ii=0; ii<=LOGMAX; ii++){
27218 mem5.aiFreelist[ii] = -1;
27219 }
27220
27221 iOffset = 0;
27222 for(ii=LOGMAX; ii>=0; ii--){
27223 int nAlloc = (1<<ii);
27224 if( (iOffset+nAlloc)<=mem5.nBlock ){
27225 mem5.aCtrl[iOffset] = ii | CTRL_FREE;
27226 memsys5Link(iOffset, ii);
27227 iOffset += nAlloc;
27228 }
27229 assert((iOffset+nAlloc)>mem5.nBlock)((void) (0));
27230 }
27231
27232 /* If a mutex is required for normal operation, allocate one */
27233 if( sqlite3GlobalConfigsqlite3Config.bMemstat==0 ){
27234 mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM3);
27235 }
27236
27237 return SQLITE_OK0;
27238}
27239
27240/*
27241** Deinitialize this module.
27242*/
27243static void memsys5Shutdown(void *NotUsed){
27244 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
27245 mem5.mutex = 0;
27246 return;
27247}
27248
27249#ifdef SQLITE_TEST
27250/*
27251** Open the file indicated and write a log of all unfreed memory
27252** allocations into that log.
27253*/
27254SQLITE_PRIVATEstatic void sqlite3Memsys5Dump(const char *zFilename){
27255 FILE *out;
27256 int i, j, n;
27257 int nMinLog;
27258
27259 if( zFilename==0 || zFilename[0]==0 ){
27260 out = stdoutstdout;
27261 }else{
27262 out = fopen(zFilename, "w");
27263 if( out==0 ){
27264 fprintf(stderrstderr, "** Unable to output memory debug output log: %s **\n",
27265 zFilename);
27266 return;
27267 }
27268 }
27269 memsys5Enter();
27270 nMinLog = memsys5Log(mem5.szAtom);
27271 for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
27272 for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
27273 fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
27274 }
27275 fprintf(out, "mem5.nAlloc = %llu\n", mem5.nAlloc);
27276 fprintf(out, "mem5.totalAlloc = %llu\n", mem5.totalAlloc);
27277 fprintf(out, "mem5.totalExcess = %llu\n", mem5.totalExcess);
27278 fprintf(out, "mem5.currentOut = %u\n", mem5.currentOut);
27279 fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
27280 fprintf(out, "mem5.maxOut = %u\n", mem5.maxOut);
27281 fprintf(out, "mem5.maxCount = %u\n", mem5.maxCount);
27282 fprintf(out, "mem5.maxRequest = %u\n", mem5.maxRequest);
27283 memsys5Leave();
27284 if( out==stdoutstdout ){
27285 fflush(stdoutstdout);
27286 }else{
27287 fclose(out);
27288 }
27289}
27290#endif
27291
27292/*
27293** This routine is the only routine in this file with external
27294** linkage. It returns a pointer to a static sqlite3_mem_methods
27295** struct populated with the memsys5 methods.
27296*/
27297SQLITE_PRIVATEstatic const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
27298 static const sqlite3_mem_methods memsys5Methods = {
27299 memsys5Malloc,
27300 memsys5Free,
27301 memsys5Realloc,
27302 memsys5Size,
27303 memsys5Roundup,
27304 memsys5Init,
27305 memsys5Shutdown,
27306 0
27307 };
27308 return &memsys5Methods;
27309}
27310
27311#endif /* SQLITE_ENABLE_MEMSYS5 */
27312
27313/************** End of mem5.c ************************************************/
27314/************** Begin file mutex.c *******************************************/
27315/*
27316** 2007 August 14
27317**
27318** The author disclaims copyright to this source code. In place of
27319** a legal notice, here is a blessing:
27320**
27321** May you do good and not evil.
27322** May you find forgiveness for yourself and forgive others.
27323** May you share freely, never taking more than you give.
27324**
27325*************************************************************************
27326** This file contains the C functions that implement mutexes.
27327**
27328** This file contains code that is common across all mutex implementations.
27329*/
27330/* #include "sqliteInt.h" */
27331
27332#if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
27333/*
27334** For debugging purposes, record when the mutex subsystem is initialized
27335** and uninitialized so that we can assert() if there is an attempt to
27336** allocate a mutex while the system is uninitialized.
27337*/
27338static SQLITE_WSD int mutexIsInit = 0;
27339#endif /* SQLITE_DEBUG && !defined(SQLITE_MUTEX_OMIT) */
27340
27341
27342#ifndef SQLITE_MUTEX_OMIT
27343
27344#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
27345/*
27346** This block (enclosed by SQLITE_ENABLE_MULTITHREADED_CHECKS) contains
27347** the implementation of a wrapper around the system default mutex
27348** implementation (sqlite3DefaultMutex()).
27349**
27350** Most calls are passed directly through to the underlying default
27351** mutex implementation. Except, if a mutex is configured by calling
27352** sqlite3MutexWarnOnContention() on it, then if contention is ever
27353** encountered within xMutexEnter() a warning is emitted via sqlite3_log().
27354**
27355** This type of mutex is used as the database handle mutex when testing
27356** apps that usually use SQLITE_CONFIG_MULTITHREAD mode.
27357*/
27358
27359/*
27360** Type for all mutexes used when SQLITE_ENABLE_MULTITHREADED_CHECKS
27361** is defined. Variable CheckMutex.mutex is a pointer to the real mutex
27362** allocated by the system mutex implementation. Variable iType is usually set
27363** to the type of mutex requested - SQLITE_MUTEX_RECURSIVE, SQLITE_MUTEX_FAST
27364** or one of the static mutex identifiers. Or, if this is a recursive mutex
27365** that has been configured using sqlite3MutexWarnOnContention(), it is
27366** set to SQLITE_MUTEX_WARNONCONTENTION.
27367*/
27368typedef struct CheckMutex CheckMutex;
27369struct CheckMutex {
27370 int iType;
27371 sqlite3_mutex *mutex;
27372};
27373
27374#define SQLITE_MUTEX_WARNONCONTENTION (-1)
27375
27376/*
27377** Pointer to real mutex methods object used by the CheckMutex
27378** implementation. Set by checkMutexInit().
27379*/
27380static SQLITE_WSD const sqlite3_mutex_methods *pGlobalMutexMethods;
27381
27382#ifdef SQLITE_DEBUG
27383static int checkMutexHeld(sqlite3_mutex *p){
27384 return pGlobalMutexMethods->xMutexHeld(((CheckMutex*)p)->mutex);
27385}
27386static int checkMutexNotheld(sqlite3_mutex *p){
27387 return pGlobalMutexMethods->xMutexNotheld(((CheckMutex*)p)->mutex);
27388}
27389#endif
27390
27391/*
27392** Initialize and deinitialize the mutex subsystem.
27393*/
27394static int checkMutexInit(void){
27395 pGlobalMutexMethods = sqlite3DefaultMutex();
27396 return SQLITE_OK0;
27397}
27398static int checkMutexEnd(void){
27399 pGlobalMutexMethods = 0;
27400 return SQLITE_OK0;
27401}
27402
27403/*
27404** Allocate a mutex.
27405*/
27406static sqlite3_mutex *checkMutexAlloc(int iType){
27407 static CheckMutex staticMutexes[] = {
27408 {2, 0}, {3, 0}, {4, 0}, {5, 0},
27409 {6, 0}, {7, 0}, {8, 0}, {9, 0},
27410 {10, 0}, {11, 0}, {12, 0}, {13, 0}
27411 };
27412 CheckMutex *p = 0;
27413
27414 assert( SQLITE_MUTEX_RECURSIVE==1 && SQLITE_MUTEX_FAST==0 )((void) (0));
27415 if( iType<2 ){
27416 p = sqlite3MallocZero(sizeof(CheckMutex));
27417 if( p==0 ) return 0;
27418 p->iType = iType;
27419 }else{
27420#ifdef SQLITE_ENABLE_API_ARMOR1
27421 if( iType-2>=ArraySize(staticMutexes)((int)(sizeof(staticMutexes)/sizeof(staticMutexes[0]))) ){
27422 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(27422);
27423 return 0;
27424 }
27425#endif
27426 p = &staticMutexes[iType-2];
27427 }
27428
27429 if( p->mutex==0 ){
27430 p->mutex = pGlobalMutexMethods->xMutexAlloc(iType);
27431 if( p->mutex==0 ){
27432 if( iType<2 ){
27433 sqlite3_free(p);
27434 }
27435 p = 0;
27436 }
27437 }
27438
27439 return (sqlite3_mutex*)p;
27440}
27441
27442/*
27443** Free a mutex.
27444*/
27445static void checkMutexFree(sqlite3_mutex *p){
27446 assert( SQLITE_MUTEX_RECURSIVE<2 )((void) (0));
27447 assert( SQLITE_MUTEX_FAST<2 )((void) (0));
27448 assert( SQLITE_MUTEX_WARNONCONTENTION<2 )((void) (0));
27449
27450#if SQLITE_ENABLE_API_ARMOR1
27451 if( ((CheckMutex*)p)->iType<2 )
27452#endif
27453 {
27454 CheckMutex *pCheck = (CheckMutex*)p;
27455 pGlobalMutexMethods->xMutexFree(pCheck->mutex);
27456 sqlite3_free(pCheck);
27457 }
27458#ifdef SQLITE_ENABLE_API_ARMOR1
27459 else{
27460 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(27460);
27461 }
27462#endif
27463}
27464
27465/*
27466** Enter the mutex.
27467*/
27468static void checkMutexEnter(sqlite3_mutex *p){
27469 CheckMutex *pCheck = (CheckMutex*)p;
27470 if( pCheck->iType==SQLITE_MUTEX_WARNONCONTENTION ){
27471 if( SQLITE_OK0==pGlobalMutexMethods->xMutexTry(pCheck->mutex) ){
27472 return;
27473 }
27474 sqlite3_log(SQLITE_MISUSE21,
27475 "illegal multi-threaded access to database connection"
27476 );
27477 }
27478 pGlobalMutexMethods->xMutexEnter(pCheck->mutex);
27479}
27480
27481/*
27482** Enter the mutex (do not block).
27483*/
27484static int checkMutexTry(sqlite3_mutex *p){
27485 CheckMutex *pCheck = (CheckMutex*)p;
27486 return pGlobalMutexMethods->xMutexTry(pCheck->mutex);
27487}
27488
27489/*
27490** Leave the mutex.
27491*/
27492static void checkMutexLeave(sqlite3_mutex *p){
27493 CheckMutex *pCheck = (CheckMutex*)p;
27494 pGlobalMutexMethods->xMutexLeave(pCheck->mutex);
27495}
27496
27497sqlite3_mutex_methods const *multiThreadedCheckMutex(void){
27498 static const sqlite3_mutex_methods sMutex = {
27499 checkMutexInit,
27500 checkMutexEnd,
27501 checkMutexAlloc,
27502 checkMutexFree,
27503 checkMutexEnter,
27504 checkMutexTry,
27505 checkMutexLeave,
27506#ifdef SQLITE_DEBUG
27507 checkMutexHeld,
27508 checkMutexNotheld
27509#else
27510 0,
27511 0
27512#endif
27513 };
27514 return &sMutex;
27515}
27516
27517/*
27518** Mark the SQLITE_MUTEX_RECURSIVE mutex passed as the only argument as
27519** one on which there should be no contention.
27520*/
27521SQLITE_PRIVATEstatic void sqlite3MutexWarnOnContention(sqlite3_mutex *p){
27522 if( sqlite3GlobalConfigsqlite3Config.mutex.xMutexAlloc==checkMutexAlloc ){
27523 CheckMutex *pCheck = (CheckMutex*)p;
27524 assert( pCheck->iType==SQLITE_MUTEX_RECURSIVE )((void) (0));
27525 pCheck->iType = SQLITE_MUTEX_WARNONCONTENTION;
27526 }
27527}
27528#endif /* ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS */
27529
27530/*
27531** Initialize the mutex system.
27532*/
27533SQLITE_PRIVATEstatic int sqlite3MutexInit(void){
27534 int rc = SQLITE_OK0;
27535 if( !sqlite3GlobalConfigsqlite3Config.mutex.xMutexAlloc ){
27536 /* If the xMutexAlloc method has not been set, then the user did not
27537 ** install a mutex implementation via sqlite3_config() prior to
27538 ** sqlite3_initialize() being called. This block copies pointers to
27539 ** the default implementation into the sqlite3GlobalConfig structure.
27540 */
27541 sqlite3_mutex_methods const *pFrom;
27542 sqlite3_mutex_methods *pTo = &sqlite3GlobalConfigsqlite3Config.mutex;
27543
27544 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
27545#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
27546 pFrom = multiThreadedCheckMutex();
27547#else
27548 pFrom = sqlite3DefaultMutex();
27549#endif
27550 }else{
27551 pFrom = sqlite3NoopMutex();
27552 }
27553 pTo->xMutexInit = pFrom->xMutexInit;
27554 pTo->xMutexEnd = pFrom->xMutexEnd;
27555 pTo->xMutexFree = pFrom->xMutexFree;
27556 pTo->xMutexEnter = pFrom->xMutexEnter;
27557 pTo->xMutexTry = pFrom->xMutexTry;
27558 pTo->xMutexLeave = pFrom->xMutexLeave;
27559 pTo->xMutexHeld = pFrom->xMutexHeld;
27560 pTo->xMutexNotheld = pFrom->xMutexNotheld;
27561 sqlite3MemoryBarrier();
27562 pTo->xMutexAlloc = pFrom->xMutexAlloc;
27563 }
27564 assert( sqlite3GlobalConfig.mutex.xMutexInit )((void) (0));
27565 rc = sqlite3GlobalConfigsqlite3Config.mutex.xMutexInit();
27566
27567#ifdef SQLITE_DEBUG
27568 GLOBAL(int, mutexIsInit)mutexIsInit = 1;
27569#endif
27570
27571 sqlite3MemoryBarrier();
27572 return rc;
27573}
27574
27575/*
27576** Shutdown the mutex system. This call frees resources allocated by
27577** sqlite3MutexInit().
27578*/
27579SQLITE_PRIVATEstatic int sqlite3MutexEnd(void){
27580 int rc = SQLITE_OK0;
27581 if( sqlite3GlobalConfigsqlite3Config.mutex.xMutexEnd ){
27582 rc = sqlite3GlobalConfigsqlite3Config.mutex.xMutexEnd();
27583 }
27584
27585#ifdef SQLITE_DEBUG
27586 GLOBAL(int, mutexIsInit)mutexIsInit = 0;
27587#endif
27588
27589 return rc;
27590}
27591
27592/*
27593** Retrieve a pointer to a static mutex or allocate a new dynamic one.
27594*/
27595SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
27596#ifndef SQLITE_OMIT_AUTOINIT
27597 if( id<=SQLITE_MUTEX_RECURSIVE1 && sqlite3_initialize() ) return 0;
27598 if( id>SQLITE_MUTEX_RECURSIVE1 && sqlite3MutexInit() ) return 0;
27599#endif
27600 assert( sqlite3GlobalConfig.mutex.xMutexAlloc )((void) (0));
27601 return sqlite3GlobalConfigsqlite3Config.mutex.xMutexAlloc(id);
27602}
27603
27604SQLITE_PRIVATEstatic sqlite3_mutex *sqlite3MutexAlloc(int id){
27605 if( !sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
27606 return 0;
27607 }
27608 assert( GLOBAL(int, mutexIsInit) )((void) (0));
27609 assert( sqlite3GlobalConfig.mutex.xMutexAlloc )((void) (0));
27610 return sqlite3GlobalConfigsqlite3Config.mutex.xMutexAlloc(id);
27611}
27612
27613/*
27614** Free a dynamic mutex.
27615*/
27616SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
27617 if( p ){
27618 assert( sqlite3GlobalConfig.mutex.xMutexFree )((void) (0));
27619 sqlite3GlobalConfigsqlite3Config.mutex.xMutexFree(p);
27620 }
27621}
27622
27623/*
27624** Obtain the mutex p. If some other thread already has the mutex, block
27625** until it can be obtained.
27626*/
27627SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
27628 if( p ){
27629 assert( sqlite3GlobalConfig.mutex.xMutexEnter )((void) (0));
27630 sqlite3GlobalConfigsqlite3Config.mutex.xMutexEnter(p);
27631 }
27632}
27633
27634/*
27635** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
27636** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
27637*/
27638SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
27639 int rc = SQLITE_OK0;
27640 if( p ){
27641 assert( sqlite3GlobalConfig.mutex.xMutexTry )((void) (0));
27642 return sqlite3GlobalConfigsqlite3Config.mutex.xMutexTry(p);
27643 }
27644 return rc;
27645}
27646
27647/*
27648** The sqlite3_mutex_leave() routine exits a mutex that was previously
27649** entered by the same thread. The behavior is undefined if the mutex
27650** is not currently entered. If a NULL pointer is passed as an argument
27651** this function is a no-op.
27652*/
27653SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
27654 if( p ){
27655 assert( sqlite3GlobalConfig.mutex.xMutexLeave )((void) (0));
27656 sqlite3GlobalConfigsqlite3Config.mutex.xMutexLeave(p);
27657 }
27658}
27659
27660#ifndef NDEBUG1
27661/*
27662** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
27663** intended for use inside assert() statements.
27664*/
27665SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
27666 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexHeld )((void) (0));
27667 return p==0 || sqlite3GlobalConfigsqlite3Config.mutex.xMutexHeld(p);
27668}
27669SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
27670 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld )((void) (0));
27671 return p==0 || sqlite3GlobalConfigsqlite3Config.mutex.xMutexNotheld(p);
27672}
27673#endif
27674
27675#endif /* !defined(SQLITE_MUTEX_OMIT) */
27676
27677/************** End of mutex.c ***********************************************/
27678/************** Begin file mutex_noop.c **************************************/
27679/*
27680** 2008 October 07
27681**
27682** The author disclaims copyright to this source code. In place of
27683** a legal notice, here is a blessing:
27684**
27685** May you do good and not evil.
27686** May you find forgiveness for yourself and forgive others.
27687** May you share freely, never taking more than you give.
27688**
27689*************************************************************************
27690** This file contains the C functions that implement mutexes.
27691**
27692** This implementation in this file does not provide any mutual
27693** exclusion and is thus suitable for use only in applications
27694** that use SQLite in a single thread. The routines defined
27695** here are place-holders. Applications can substitute working
27696** mutex routines at start-time using the
27697**
27698** sqlite3_config(SQLITE_CONFIG_MUTEX,...)
27699**
27700** interface.
27701**
27702** If compiled with SQLITE_DEBUG, then additional logic is inserted
27703** that does error checking on mutexes to make sure they are being
27704** called correctly.
27705*/
27706/* #include "sqliteInt.h" */
27707
27708#ifndef SQLITE_MUTEX_OMIT
27709
27710#ifndef SQLITE_DEBUG
27711/*
27712** Stub routines for all mutex methods.
27713**
27714** This routines provide no mutual exclusion or error checking.
27715*/
27716static int noopMutexInit(void){ return SQLITE_OK0; }
27717static int noopMutexEnd(void){ return SQLITE_OK0; }
27718static sqlite3_mutex *noopMutexAlloc(int id){
27719 UNUSED_PARAMETER(id)(void)(id);
27720 return (sqlite3_mutex*)8;
27721}
27722static void noopMutexFree(sqlite3_mutex *p){ UNUSED_PARAMETER(p)(void)(p); return; }
27723static void noopMutexEnter(sqlite3_mutex *p){ UNUSED_PARAMETER(p)(void)(p); return; }
27724static int noopMutexTry(sqlite3_mutex *p){
27725 UNUSED_PARAMETER(p)(void)(p);
27726 return SQLITE_OK0;
27727}
27728static void noopMutexLeave(sqlite3_mutex *p){ UNUSED_PARAMETER(p)(void)(p); return; }
27729
27730SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3NoopMutex(void){
27731 static const sqlite3_mutex_methods sMutex = {
27732 noopMutexInit,
27733 noopMutexEnd,
27734 noopMutexAlloc,
27735 noopMutexFree,
27736 noopMutexEnter,
27737 noopMutexTry,
27738 noopMutexLeave,
27739
27740 0,
27741 0,
27742 };
27743
27744 return &sMutex;
27745}
27746#endif /* !SQLITE_DEBUG */
27747
27748#ifdef SQLITE_DEBUG
27749/*
27750** In this implementation, error checking is provided for testing
27751** and debugging purposes. The mutexes still do not provide any
27752** mutual exclusion.
27753*/
27754
27755/*
27756** The mutex object
27757*/
27758typedef struct sqlite3_debug_mutex {
27759 int id; /* The mutex type */
27760 int cnt; /* Number of entries without a matching leave */
27761} sqlite3_debug_mutex;
27762
27763/*
27764** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
27765** intended for use inside assert() statements.
27766*/
27767static int debugMutexHeld(sqlite3_mutex *pX){
27768 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
27769 return p==0 || p->cnt>0;
27770}
27771static int debugMutexNotheld(sqlite3_mutex *pX){
27772 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
27773 return p==0 || p->cnt==0;
27774}
27775
27776/*
27777** Initialize and deinitialize the mutex subsystem.
27778*/
27779static int debugMutexInit(void){ return SQLITE_OK0; }
27780static int debugMutexEnd(void){ return SQLITE_OK0; }
27781
27782/*
27783** The sqlite3_mutex_alloc() routine allocates a new
27784** mutex and returns a pointer to it. If it returns NULL
27785** that means that a mutex could not be allocated.
27786*/
27787static sqlite3_mutex *debugMutexAlloc(int id){
27788 static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_VFS313 - 1];
27789 sqlite3_debug_mutex *pNew = 0;
27790 switch( id ){
27791 case SQLITE_MUTEX_FAST0:
27792 case SQLITE_MUTEX_RECURSIVE1: {
27793 pNew = sqlite3Malloc(sizeof(*pNew));
27794 if( pNew ){
27795 pNew->id = id;
27796 pNew->cnt = 0;
27797 }
27798 break;
27799 }
27800 default: {
27801#ifdef SQLITE_ENABLE_API_ARMOR1
27802 if( id-2<0 || id-2>=ArraySize(aStatic)((int)(sizeof(aStatic)/sizeof(aStatic[0]))) ){
27803 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(27803);
27804 return 0;
27805 }
27806#endif
27807 pNew = &aStatic[id-2];
27808 pNew->id = id;
27809 break;
27810 }
27811 }
27812 return (sqlite3_mutex*)pNew;
27813}
27814
27815/*
27816** This routine deallocates a previously allocated mutex.
27817*/
27818static void debugMutexFree(sqlite3_mutex *pX){
27819 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
27820 assert( p->cnt==0 )((void) (0));
27821 if( p->id==SQLITE_MUTEX_RECURSIVE1 || p->id==SQLITE_MUTEX_FAST0 ){
27822 sqlite3_free(p);
27823 }else{
27824#ifdef SQLITE_ENABLE_API_ARMOR1
27825 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(27825);
27826#endif
27827 }
27828}
27829
27830/*
27831** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
27832** to enter a mutex. If another thread is already within the mutex,
27833** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
27834** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
27835** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
27836** be entered multiple times by the same thread. In such cases the,
27837** mutex must be exited an equal number of times before another thread
27838** can enter. If the same thread tries to enter any other kind of mutex
27839** more than once, the behavior is undefined.
27840*/
27841static void debugMutexEnter(sqlite3_mutex *pX){
27842 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
27843 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) )((void) (0));
27844 p->cnt++;
27845}
27846static int debugMutexTry(sqlite3_mutex *pX){
27847 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
27848 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) )((void) (0));
27849 p->cnt++;
27850 return SQLITE_OK0;
27851}
27852
27853/*
27854** The sqlite3_mutex_leave() routine exits a mutex that was
27855** previously entered by the same thread. The behavior
27856** is undefined if the mutex is not currently entered or
27857** is not currently allocated. SQLite will never do either.
27858*/
27859static void debugMutexLeave(sqlite3_mutex *pX){
27860 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
27861 assert( debugMutexHeld(pX) )((void) (0));
27862 p->cnt--;
27863 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) )((void) (0));
27864}
27865
27866SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3NoopMutex(void){
27867 static const sqlite3_mutex_methods sMutex = {
27868 debugMutexInit,
27869 debugMutexEnd,
27870 debugMutexAlloc,
27871 debugMutexFree,
27872 debugMutexEnter,
27873 debugMutexTry,
27874 debugMutexLeave,
27875
27876 debugMutexHeld,
27877 debugMutexNotheld
27878 };
27879
27880 return &sMutex;
27881}
27882#endif /* SQLITE_DEBUG */
27883
27884/*
27885** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
27886** is used regardless of the run-time threadsafety setting.
27887*/
27888#ifdef SQLITE_MUTEX_NOOP
27889SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
27890 return sqlite3NoopMutex();
27891}
27892#endif /* defined(SQLITE_MUTEX_NOOP) */
27893#endif /* !defined(SQLITE_MUTEX_OMIT) */
27894
27895/************** End of mutex_noop.c ******************************************/
27896/************** Begin file mutex_unix.c **************************************/
27897/*
27898** 2007 August 28
27899**
27900** The author disclaims copyright to this source code. In place of
27901** a legal notice, here is a blessing:
27902**
27903** May you do good and not evil.
27904** May you find forgiveness for yourself and forgive others.
27905** May you share freely, never taking more than you give.
27906**
27907*************************************************************************
27908** This file contains the C functions that implement mutexes for pthreads
27909*/
27910/* #include "sqliteInt.h" */
27911
27912/*
27913** The code in this file is only used if we are compiling threadsafe
27914** under unix with pthreads.
27915**
27916** Note that this implementation requires a version of pthreads that
27917** supports recursive mutexes.
27918*/
27919#ifdef SQLITE_MUTEX_PTHREADS
27920
27921#include <pthread.h>
27922
27923/*
27924** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
27925** are necessary under two condidtions: (1) Debug builds and (2) using
27926** home-grown mutexes. Encapsulate these conditions into a single #define.
27927*/
27928#if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
27929# define SQLITE_MUTEX_NREF0 1
27930#else
27931# define SQLITE_MUTEX_NREF0 0
27932#endif
27933
27934/*
27935** Each recursive mutex is an instance of the following structure.
27936*/
27937struct sqlite3_mutex {
27938 pthread_mutex_t mutex; /* Mutex controlling the lock */
27939#if SQLITE_MUTEX_NREF0 || defined(SQLITE_ENABLE_API_ARMOR1)
27940 int id; /* Mutex type */
27941#endif
27942#if SQLITE_MUTEX_NREF0
27943 volatile int nRef; /* Number of entrances */
27944 volatile pthread_t owner; /* Thread that is within this mutex */
27945 int trace; /* True to trace changes */
27946#endif
27947};
27948#if SQLITE_MUTEX_NREF0
27949# define SQLITE3_MUTEX_INITIALIZER(id){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
id }
\
27950 {PTHREAD_MUTEX_INITIALIZER{ { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },id,0,(pthread_t)0,0}
27951#elif defined(SQLITE_ENABLE_API_ARMOR1)
27952# 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 }
27953#else
27954#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 } } } }
27955#endif
27956
27957/*
27958** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
27959** intended for use only inside assert() statements. On some platforms,
27960** there might be race conditions that can cause these routines to
27961** deliver incorrect results. In particular, if pthread_equal() is
27962** not an atomic operation, then these routines might delivery
27963** incorrect results. On most platforms, pthread_equal() is a
27964** comparison of two integers and is therefore atomic. But we are
27965** told that HPUX is not such a platform. If so, then these routines
27966** will not always work correctly on HPUX.
27967**
27968** On those platforms where pthread_equal() is not atomic, SQLite
27969** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
27970** make sure no assert() statements are evaluated and hence these
27971** routines are never called.
27972*/
27973#if !defined(NDEBUG1) || defined(SQLITE_DEBUG)
27974static int pthreadMutexHeld(sqlite3_mutex *p){
27975 return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
27976}
27977static int pthreadMutexNotheld(sqlite3_mutex *p){
27978 return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
27979}
27980#endif
27981
27982/*
27983** Try to provide a memory barrier operation, needed for initialization
27984** and also for the implementation of xShmBarrier in the VFS in cases
27985** where SQLite is compiled without mutexes.
27986*/
27987SQLITE_PRIVATEstatic void sqlite3MemoryBarrier(void){
27988#if defined(SQLITE_MEMORY_BARRIER)
27989 SQLITE_MEMORY_BARRIER;
27990#elif defined(__GNUC__4) && GCC_VERSION(4*1000000+2*1000+1)>=4001000
27991 __sync_synchronize();
27992#endif
27993}
27994
27995/*
27996** Initialize and deinitialize the mutex subsystem.
27997*/
27998static int pthreadMutexInit(void){ return SQLITE_OK0; }
27999static int pthreadMutexEnd(void){ return SQLITE_OK0; }
28000
28001/*
28002** The sqlite3_mutex_alloc() routine allocates a new
28003** mutex and returns a pointer to it. If it returns NULL
28004** that means that a mutex could not be allocated. SQLite
28005** will unwind its stack and return an error. The argument
28006** to sqlite3_mutex_alloc() is one of these integer constants:
28007**
28008** <ul>
28009** <li> SQLITE_MUTEX_FAST
28010** <li> SQLITE_MUTEX_RECURSIVE
28011** <li> SQLITE_MUTEX_STATIC_MAIN
28012** <li> SQLITE_MUTEX_STATIC_MEM
28013** <li> SQLITE_MUTEX_STATIC_OPEN
28014** <li> SQLITE_MUTEX_STATIC_PRNG
28015** <li> SQLITE_MUTEX_STATIC_LRU
28016** <li> SQLITE_MUTEX_STATIC_PMEM
28017** <li> SQLITE_MUTEX_STATIC_APP1
28018** <li> SQLITE_MUTEX_STATIC_APP2
28019** <li> SQLITE_MUTEX_STATIC_APP3
28020** <li> SQLITE_MUTEX_STATIC_VFS1
28021** <li> SQLITE_MUTEX_STATIC_VFS2
28022** <li> SQLITE_MUTEX_STATIC_VFS3
28023** </ul>
28024**
28025** The first two constants cause sqlite3_mutex_alloc() to create
28026** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
28027** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
28028** The mutex implementation does not need to make a distinction
28029** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
28030** not want to. But SQLite will only request a recursive mutex in
28031** cases where it really needs one. If a faster non-recursive mutex
28032** implementation is available on the host platform, the mutex subsystem
28033** might return such a mutex in response to SQLITE_MUTEX_FAST.
28034**
28035** The other allowed parameters to sqlite3_mutex_alloc() each return
28036** a pointer to a static preexisting mutex. Six static mutexes are
28037** used by the current version of SQLite. Future versions of SQLite
28038** may add additional static mutexes. Static mutexes are for internal
28039** use by SQLite only. Applications that use SQLite mutexes should
28040** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
28041** SQLITE_MUTEX_RECURSIVE.
28042**
28043** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
28044** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
28045** returns a different mutex on every call. But for the static
28046** mutex types, the same mutex is returned on every call that has
28047** the same type number.
28048*/
28049static sqlite3_mutex *pthreadMutexAlloc(int iType){
28050 static sqlite3_mutex staticMutexes[] = {
28051 SQLITE3_MUTEX_INITIALIZER(2){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
2 }
,
28052 SQLITE3_MUTEX_INITIALIZER(3){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
3 }
,
28053 SQLITE3_MUTEX_INITIALIZER(4){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
4 }
,
28054 SQLITE3_MUTEX_INITIALIZER(5){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
5 }
,
28055 SQLITE3_MUTEX_INITIALIZER(6){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
6 }
,
28056 SQLITE3_MUTEX_INITIALIZER(7){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
7 }
,
28057 SQLITE3_MUTEX_INITIALIZER(8){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
8 }
,
28058 SQLITE3_MUTEX_INITIALIZER(9){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
9 }
,
28059 SQLITE3_MUTEX_INITIALIZER(10){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
10 }
,
28060 SQLITE3_MUTEX_INITIALIZER(11){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
11 }
,
28061 SQLITE3_MUTEX_INITIALIZER(12){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
12 }
,
28062 SQLITE3_MUTEX_INITIALIZER(13){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
13 }
28063 };
28064 sqlite3_mutex *p;
28065 switch( iType ){
28066 case SQLITE_MUTEX_RECURSIVE1: {
28067 p = sqlite3MallocZero( sizeof(*p) );
28068 if( p ){
28069#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
28070 /* If recursive mutexes are not available, we will have to
28071 ** build our own. See below. */
28072 pthread_mutex_init(&p->mutex, 0);
28073#else
28074 /* Use a recursive mutex if it is available */
28075 pthread_mutexattr_t recursiveAttr;
28076 pthread_mutexattr_init(&recursiveAttr);
28077 pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
28078 pthread_mutex_init(&p->mutex, &recursiveAttr);
28079 pthread_mutexattr_destroy(&recursiveAttr);
28080#endif
28081#if SQLITE_MUTEX_NREF0 || defined(SQLITE_ENABLE_API_ARMOR1)
28082 p->id = SQLITE_MUTEX_RECURSIVE1;
28083#endif
28084 }
28085 break;
28086 }
28087 case SQLITE_MUTEX_FAST0: {
28088 p = sqlite3MallocZero( sizeof(*p) );
28089 if( p ){
28090 pthread_mutex_init(&p->mutex, 0);
28091#if SQLITE_MUTEX_NREF0 || defined(SQLITE_ENABLE_API_ARMOR1)
28092 p->id = SQLITE_MUTEX_FAST0;
28093#endif
28094 }
28095 break;
28096 }
28097 default: {
28098#ifdef SQLITE_ENABLE_API_ARMOR1
28099 if( iType-2<0 || iType-2>=ArraySize(staticMutexes)((int)(sizeof(staticMutexes)/sizeof(staticMutexes[0]))) ){
28100 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(28100);
28101 return 0;
28102 }
28103#endif
28104 p = &staticMutexes[iType-2];
28105 break;
28106 }
28107 }
28108#if SQLITE_MUTEX_NREF0 || defined(SQLITE_ENABLE_API_ARMOR1)
28109 assert( p==0 || p->id==iType )((void) (0));
28110#endif
28111 return p;
28112}
28113
28114
28115/*
28116** This routine deallocates a previously
28117** allocated mutex. SQLite is careful to deallocate every
28118** mutex that it allocates.
28119*/
28120static void pthreadMutexFree(sqlite3_mutex *p){
28121 assert( p->nRef==0 )((void) (0));
28122#if SQLITE_ENABLE_API_ARMOR1
28123 if( p->id==SQLITE_MUTEX_FAST0 || p->id==SQLITE_MUTEX_RECURSIVE1 )
28124#endif
28125 {
28126 pthread_mutex_destroy(&p->mutex);
28127 sqlite3_free(p);
28128 }
28129#ifdef SQLITE_ENABLE_API_ARMOR1
28130 else{
28131 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(28131);
28132 }
28133#endif
28134}
28135
28136/*
28137** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
28138** to enter a mutex. If another thread is already within the mutex,
28139** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
28140** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
28141** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
28142** be entered multiple times by the same thread. In such cases the,
28143** mutex must be exited an equal number of times before another thread
28144** can enter. If the same thread tries to enter any other kind of mutex
28145** more than once, the behavior is undefined.
28146*/
28147static void pthreadMutexEnter(sqlite3_mutex *p){
28148 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) )((void) (0));
28149
28150#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
28151 /* If recursive mutexes are not available, then we have to grow
28152 ** our own. This implementation assumes that pthread_equal()
28153 ** is atomic - that it cannot be deceived into thinking self
28154 ** and p->owner are equal if p->owner changes between two values
28155 ** that are not equal to self while the comparison is taking place.
28156 ** This implementation also assumes a coherent cache - that
28157 ** separate processes cannot read different values from the same
28158 ** address at the same time. If either of these two conditions
28159 ** are not met, then the mutexes will fail and problems will result.
28160 */
28161 {
28162 pthread_t self = pthread_self();
28163 if( p->nRef>0 && pthread_equal(p->owner, self) ){
28164 p->nRef++;
28165 }else{
28166 pthread_mutex_lock(&p->mutex);
28167 assert( p->nRef==0 )((void) (0));
28168 p->owner = self;
28169 p->nRef = 1;
28170 }
28171 }
28172#else
28173 /* Use the built-in recursive mutexes if they are available.
28174 */
28175 pthread_mutex_lock(&p->mutex);
28176#if SQLITE_MUTEX_NREF0
28177 assert( p->nRef>0 || p->owner==0 )((void) (0));
28178 p->owner = pthread_self();
28179 p->nRef++;
28180#endif
28181#endif
28182
28183#ifdef SQLITE_DEBUG
28184 if( p->trace ){
28185 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
28186 }
28187#endif
28188}
28189static int pthreadMutexTry(sqlite3_mutex *p){
28190 int rc;
28191 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) )((void) (0));
28192
28193#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
28194 /* If recursive mutexes are not available, then we have to grow
28195 ** our own. This implementation assumes that pthread_equal()
28196 ** is atomic - that it cannot be deceived into thinking self
28197 ** and p->owner are equal if p->owner changes between two values
28198 ** that are not equal to self while the comparison is taking place.
28199 ** This implementation also assumes a coherent cache - that
28200 ** separate processes cannot read different values from the same
28201 ** address at the same time. If either of these two conditions
28202 ** are not met, then the mutexes will fail and problems will result.
28203 */
28204 {
28205 pthread_t self = pthread_self();
28206 if( p->nRef>0 && pthread_equal(p->owner, self) ){
28207 p->nRef++;
28208 rc = SQLITE_OK0;
28209 }else if( pthread_mutex_trylock(&p->mutex)==0 ){
28210 assert( p->nRef==0 )((void) (0));
28211 p->owner = self;
28212 p->nRef = 1;
28213 rc = SQLITE_OK0;
28214 }else{
28215 rc = SQLITE_BUSY5;
28216 }
28217 }
28218#else
28219 /* Use the built-in recursive mutexes if they are available.
28220 */
28221 if( pthread_mutex_trylock(&p->mutex)==0 ){
28222#if SQLITE_MUTEX_NREF0
28223 p->owner = pthread_self();
28224 p->nRef++;
28225#endif
28226 rc = SQLITE_OK0;
28227 }else{
28228 rc = SQLITE_BUSY5;
28229 }
28230#endif
28231
28232#ifdef SQLITE_DEBUG
28233 if( rc==SQLITE_OK0 && p->trace ){
28234 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
28235 }
28236#endif
28237 return rc;
28238}
28239
28240/*
28241** The sqlite3_mutex_leave() routine exits a mutex that was
28242** previously entered by the same thread. The behavior
28243** is undefined if the mutex is not currently entered or
28244** is not currently allocated. SQLite will never do either.
28245*/
28246static void pthreadMutexLeave(sqlite3_mutex *p){
28247 assert( pthreadMutexHeld(p) )((void) (0));
28248#if SQLITE_MUTEX_NREF0
28249 p->nRef--;
28250 if( p->nRef==0 ) p->owner = 0;
28251#endif
28252 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE )((void) (0));
28253
28254#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
28255 if( p->nRef==0 ){
28256 pthread_mutex_unlock(&p->mutex);
28257 }
28258#else
28259 pthread_mutex_unlock(&p->mutex);
28260#endif
28261
28262#ifdef SQLITE_DEBUG
28263 if( p->trace ){
28264 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
28265 }
28266#endif
28267}
28268
28269SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
28270 static const sqlite3_mutex_methods sMutex = {
28271 pthreadMutexInit,
28272 pthreadMutexEnd,
28273 pthreadMutexAlloc,
28274 pthreadMutexFree,
28275 pthreadMutexEnter,
28276 pthreadMutexTry,
28277 pthreadMutexLeave,
28278#ifdef SQLITE_DEBUG
28279 pthreadMutexHeld,
28280 pthreadMutexNotheld
28281#else
28282 0,
28283 0
28284#endif
28285 };
28286
28287 return &sMutex;
28288}
28289
28290#endif /* SQLITE_MUTEX_PTHREADS */
28291
28292/************** End of mutex_unix.c ******************************************/
28293/************** Begin file mutex_w32.c ***************************************/
28294/*
28295** 2007 August 14
28296**
28297** The author disclaims copyright to this source code. In place of
28298** a legal notice, here is a blessing:
28299**
28300** May you do good and not evil.
28301** May you find forgiveness for yourself and forgive others.
28302** May you share freely, never taking more than you give.
28303**
28304*************************************************************************
28305** This file contains the C functions that implement mutexes for Win32.
28306*/
28307/* #include "sqliteInt.h" */
28308
28309#if SQLITE_OS_WIN0
28310/*
28311** Include code that is common to all os_*.c files
28312*/
28313/* #include "os_common.h" */
28314
28315/*
28316** Include the header file for the Windows VFS.
28317*/
28318/************** Include os_win.h in the middle of mutex_w32.c ****************/
28319/************** Begin file os_win.h ******************************************/
28320/*
28321** 2013 November 25
28322**
28323** The author disclaims copyright to this source code. In place of
28324** a legal notice, here is a blessing:
28325**
28326** May you do good and not evil.
28327** May you find forgiveness for yourself and forgive others.
28328** May you share freely, never taking more than you give.
28329**
28330******************************************************************************
28331**
28332** This file contains code that is specific to Windows.
28333*/
28334#ifndef SQLITE_OS_WIN_H
28335#define SQLITE_OS_WIN_H
28336
28337/*
28338** Include the primary Windows SDK header file.
28339*/
28340#include "windows.h"
28341
28342#ifdef __CYGWIN__
28343# include <sys/cygwin.h>
28344# include <errno(*__errno_location ()).h> /* amalgamator: dontcache */
28345#endif
28346
28347/*
28348** Determine if we are dealing with Windows NT.
28349**
28350** We ought to be able to determine if we are compiling for Windows 9x or
28351** Windows NT using the _WIN32_WINNT macro as follows:
28352**
28353** #if defined(_WIN32_WINNT)
28354** # define SQLITE_OS_WINNT 1
28355** #else
28356** # define SQLITE_OS_WINNT 0
28357** #endif
28358**
28359** However, Visual Studio 2005 does not set _WIN32_WINNT by default, as
28360** it ought to, so the above test does not work. We'll just assume that
28361** everything is Windows NT unless the programmer explicitly says otherwise
28362** by setting SQLITE_OS_WINNT to 0.
28363*/
28364#if SQLITE_OS_WIN0 && !defined(SQLITE_OS_WINNT)
28365# define SQLITE_OS_WINNT 1
28366#endif
28367
28368/*
28369** Determine if we are dealing with Windows CE - which has a much reduced
28370** API.
28371*/
28372#if defined(_WIN32_WCE)
28373# define SQLITE_OS_WINCE 1
28374#else
28375# define SQLITE_OS_WINCE 0
28376#endif
28377
28378/*
28379** Determine if we are dealing with WinRT, which provides only a subset of
28380** the full Win32 API.
28381*/
28382#if !defined(SQLITE_OS_WINRT)
28383# define SQLITE_OS_WINRT 0
28384#endif
28385
28386/*
28387** For WinCE, some API function parameters do not appear to be declared as
28388** volatile.
28389*/
28390#if SQLITE_OS_WINCE
28391# define SQLITE_WIN32_VOLATILE
28392#else
28393# define SQLITE_WIN32_VOLATILE volatile
28394#endif
28395
28396/*
28397** For some Windows sub-platforms, the _beginthreadex() / _endthreadex()
28398** functions are not available (e.g. those not using MSVC, Cygwin, etc).
28399*/
28400#if SQLITE_OS_WIN0 && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
28401 SQLITE_THREADSAFE2>0 && !defined(__CYGWIN__)
28402# define SQLITE_OS_WIN_THREADS 1
28403#else
28404# define SQLITE_OS_WIN_THREADS 0
28405#endif
28406
28407#endif /* SQLITE_OS_WIN_H */
28408
28409/************** End of os_win.h **********************************************/
28410/************** Continuing where we left off in mutex_w32.c ******************/
28411#endif
28412
28413/*
28414** The code in this file is only used if we are compiling multithreaded
28415** on a Win32 system.
28416*/
28417#ifdef SQLITE_MUTEX_W32
28418
28419/*
28420** Each recursive mutex is an instance of the following structure.
28421*/
28422struct sqlite3_mutex {
28423 CRITICAL_SECTION mutex; /* Mutex controlling the lock */
28424 int id; /* Mutex type */
28425#ifdef SQLITE_DEBUG
28426 volatile int nRef; /* Number of enterances */
28427 volatile DWORD owner; /* Thread holding this mutex */
28428 volatile LONG trace; /* True to trace changes */
28429#endif
28430};
28431
28432/*
28433** These are the initializer values used when declaring a "static" mutex
28434** on Win32. It should be noted that all mutexes require initialization
28435** on the Win32 platform.
28436*/
28437#define SQLITE_W32_MUTEX_INITIALIZER { 0 }
28438
28439#ifdef SQLITE_DEBUG
28440#define SQLITE3_MUTEX_INITIALIZER(id){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
id }
{ SQLITE_W32_MUTEX_INITIALIZER, id, \
28441 0L, (DWORD)0, 0 }
28442#else
28443#define SQLITE3_MUTEX_INITIALIZER(id){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
id }
{ SQLITE_W32_MUTEX_INITIALIZER, id }
28444#endif
28445
28446#ifdef SQLITE_DEBUG
28447/*
28448** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
28449** intended for use only inside assert() statements.
28450*/
28451static int winMutexHeld(sqlite3_mutex *p){
28452 return p->nRef!=0 && p->owner==GetCurrentThreadId();
28453}
28454
28455static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
28456 return p->nRef==0 || p->owner!=tid;
28457}
28458
28459static int winMutexNotheld(sqlite3_mutex *p){
28460 DWORD tid = GetCurrentThreadId();
28461 return winMutexNotheld2(p, tid);
28462}
28463#endif
28464
28465/*
28466** Try to provide a memory barrier operation, needed for initialization
28467** and also for the xShmBarrier method of the VFS in cases when SQLite is
28468** compiled without mutexes (SQLITE_THREADSAFE=0).
28469*/
28470SQLITE_PRIVATEstatic void sqlite3MemoryBarrier(void){
28471#if defined(SQLITE_MEMORY_BARRIER)
28472 SQLITE_MEMORY_BARRIER;
28473#elif defined(__GNUC__4)
28474 __sync_synchronize();
28475#elif MSVC_VERSION0>=1300
28476 _ReadWriteBarrier();
28477#elif defined(MemoryBarrier)
28478 MemoryBarrier();
28479#endif
28480}
28481
28482/*
28483** Initialize and deinitialize the mutex subsystem.
28484*/
28485static sqlite3_mutex winMutex_staticMutexes[] = {
28486 SQLITE3_MUTEX_INITIALIZER(2){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
2 }
,
28487 SQLITE3_MUTEX_INITIALIZER(3){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
3 }
,
28488 SQLITE3_MUTEX_INITIALIZER(4){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
4 }
,
28489 SQLITE3_MUTEX_INITIALIZER(5){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
5 }
,
28490 SQLITE3_MUTEX_INITIALIZER(6){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
6 }
,
28491 SQLITE3_MUTEX_INITIALIZER(7){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
7 }
,
28492 SQLITE3_MUTEX_INITIALIZER(8){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
8 }
,
28493 SQLITE3_MUTEX_INITIALIZER(9){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
9 }
,
28494 SQLITE3_MUTEX_INITIALIZER(10){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
10 }
,
28495 SQLITE3_MUTEX_INITIALIZER(11){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
11 }
,
28496 SQLITE3_MUTEX_INITIALIZER(12){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
12 }
,
28497 SQLITE3_MUTEX_INITIALIZER(13){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
13 }
28498};
28499
28500static int winMutex_isInit = 0;
28501static int winMutex_isNt = -1; /* <0 means "need to query" */
28502
28503/* As the winMutexInit() and winMutexEnd() functions are called as part
28504** of the sqlite3_initialize() and sqlite3_shutdown() processing, the
28505** "interlocked" magic used here is probably not strictly necessary.
28506*/
28507static LONG SQLITE_WIN32_VOLATILE winMutex_lock = 0;
28508
28509SQLITE_API int sqlite3_win32_is_nt(void); /* os_win.c */
28510SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
28511
28512static int winMutexInit(void){
28513 /* The first to increment to 1 does actual initialization */
28514 if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
28515 int i;
28516 for(i=0; i<ArraySize(winMutex_staticMutexes)((int)(sizeof(winMutex_staticMutexes)/sizeof(winMutex_staticMutexes
[0])))
; i++){
28517#if SQLITE_OS_WINRT
28518 InitializeCriticalSectionEx(&winMutex_staticMutexes[i].mutex, 0, 0);
28519#else
28520 InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
28521#endif
28522 }
28523 winMutex_isInit = 1;
28524 }else{
28525 /* Another thread is (in the process of) initializing the static
28526 ** mutexes */
28527 while( !winMutex_isInit ){
28528 sqlite3_win32_sleep(1);
28529 }
28530 }
28531 return SQLITE_OK0;
28532}
28533
28534static int winMutexEnd(void){
28535 /* The first to decrement to 0 does actual shutdown
28536 ** (which should be the last to shutdown.) */
28537 if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
28538 if( winMutex_isInit==1 ){
28539 int i;
28540 for(i=0; i<ArraySize(winMutex_staticMutexes)((int)(sizeof(winMutex_staticMutexes)/sizeof(winMutex_staticMutexes
[0])))
; i++){
28541 DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
28542 }
28543 winMutex_isInit = 0;
28544 }
28545 }
28546 return SQLITE_OK0;
28547}
28548
28549/*
28550** The sqlite3_mutex_alloc() routine allocates a new
28551** mutex and returns a pointer to it. If it returns NULL
28552** that means that a mutex could not be allocated. SQLite
28553** will unwind its stack and return an error. The argument
28554** to sqlite3_mutex_alloc() is one of these integer constants:
28555**
28556** <ul>
28557** <li> SQLITE_MUTEX_FAST
28558** <li> SQLITE_MUTEX_RECURSIVE
28559** <li> SQLITE_MUTEX_STATIC_MAIN
28560** <li> SQLITE_MUTEX_STATIC_MEM
28561** <li> SQLITE_MUTEX_STATIC_OPEN
28562** <li> SQLITE_MUTEX_STATIC_PRNG
28563** <li> SQLITE_MUTEX_STATIC_LRU
28564** <li> SQLITE_MUTEX_STATIC_PMEM
28565** <li> SQLITE_MUTEX_STATIC_APP1
28566** <li> SQLITE_MUTEX_STATIC_APP2
28567** <li> SQLITE_MUTEX_STATIC_APP3
28568** <li> SQLITE_MUTEX_STATIC_VFS1
28569** <li> SQLITE_MUTEX_STATIC_VFS2
28570** <li> SQLITE_MUTEX_STATIC_VFS3
28571** </ul>
28572**
28573** The first two constants cause sqlite3_mutex_alloc() to create
28574** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
28575** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
28576** The mutex implementation does not need to make a distinction
28577** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
28578** not want to. But SQLite will only request a recursive mutex in
28579** cases where it really needs one. If a faster non-recursive mutex
28580** implementation is available on the host platform, the mutex subsystem
28581** might return such a mutex in response to SQLITE_MUTEX_FAST.
28582**
28583** The other allowed parameters to sqlite3_mutex_alloc() each return
28584** a pointer to a static preexisting mutex. Six static mutexes are
28585** used by the current version of SQLite. Future versions of SQLite
28586** may add additional static mutexes. Static mutexes are for internal
28587** use by SQLite only. Applications that use SQLite mutexes should
28588** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
28589** SQLITE_MUTEX_RECURSIVE.
28590**
28591** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
28592** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
28593** returns a different mutex on every call. But for the static
28594** mutex types, the same mutex is returned on every call that has
28595** the same type number.
28596*/
28597static sqlite3_mutex *winMutexAlloc(int iType){
28598 sqlite3_mutex *p;
28599
28600 switch( iType ){
28601 case SQLITE_MUTEX_FAST0:
28602 case SQLITE_MUTEX_RECURSIVE1: {
28603 p = sqlite3MallocZero( sizeof(*p) );
28604 if( p ){
28605 p->id = iType;
28606#ifdef SQLITE_DEBUG
28607#ifdef SQLITE_WIN32_MUTEX_TRACE_DYNAMIC
28608 p->trace = 1;
28609#endif
28610#endif
28611#if SQLITE_OS_WINRT
28612 InitializeCriticalSectionEx(&p->mutex, 0, 0);
28613#else
28614 InitializeCriticalSection(&p->mutex);
28615#endif
28616 }
28617 break;
28618 }
28619 default: {
28620#ifdef SQLITE_ENABLE_API_ARMOR1
28621 if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes)((int)(sizeof(winMutex_staticMutexes)/sizeof(winMutex_staticMutexes
[0])))
){
28622 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(28622);
28623 return 0;
28624 }
28625#endif
28626 p = &winMutex_staticMutexes[iType-2];
28627#ifdef SQLITE_DEBUG
28628#ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
28629 InterlockedCompareExchange(&p->trace, 1, 0);
28630#endif
28631#endif
28632 break;
28633 }
28634 }
28635 assert( p==0 || p->id==iType )((void) (0));
28636 return p;
28637}
28638
28639
28640/*
28641** This routine deallocates a previously
28642** allocated mutex. SQLite is careful to deallocate every
28643** mutex that it allocates.
28644*/
28645static void winMutexFree(sqlite3_mutex *p){
28646 assert( p )((void) (0));
28647 assert( p->nRef==0 && p->owner==0 )((void) (0));
28648 if( p->id==SQLITE_MUTEX_FAST0 || p->id==SQLITE_MUTEX_RECURSIVE1 ){
28649 DeleteCriticalSection(&p->mutex);
28650 sqlite3_free(p);
28651 }else{
28652#ifdef SQLITE_ENABLE_API_ARMOR1
28653 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(28653);
28654#endif
28655 }
28656}
28657
28658/*
28659** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
28660** to enter a mutex. If another thread is already within the mutex,
28661** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
28662** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
28663** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
28664** be entered multiple times by the same thread. In such cases the,
28665** mutex must be exited an equal number of times before another thread
28666** can enter. If the same thread tries to enter any other kind of mutex
28667** more than once, the behavior is undefined.
28668*/
28669static void winMutexEnter(sqlite3_mutex *p){
28670#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
28671 DWORD tid = GetCurrentThreadId();
28672#endif
28673#ifdef SQLITE_DEBUG
28674 assert( p )((void) (0));
28675 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) )((void) (0));
28676#else
28677 assert( p )((void) (0));
28678#endif
28679 assert( winMutex_isInit==1 )((void) (0));
28680 EnterCriticalSection(&p->mutex);
28681#ifdef SQLITE_DEBUG
28682 assert( p->nRef>0 || p->owner==0 )((void) (0));
28683 p->owner = tid;
28684 p->nRef++;
28685 if( p->trace ){
28686 OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
28687 tid, p->id, p, p->trace, p->nRef));
28688 }
28689#endif
28690}
28691
28692static int winMutexTry(sqlite3_mutex *p){
28693#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
28694 DWORD tid = GetCurrentThreadId();
28695#endif
28696 int rc = SQLITE_BUSY5;
28697 assert( p )((void) (0));
28698 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) )((void) (0));
28699 /*
28700 ** The sqlite3_mutex_try() routine is very rarely used, and when it
28701 ** is used it is merely an optimization. So it is OK for it to always
28702 ** fail.
28703 **
28704 ** The TryEnterCriticalSection() interface is only available on WinNT.
28705 ** And some windows compilers complain if you try to use it without
28706 ** first doing some #defines that prevent SQLite from building on Win98.
28707 ** For that reason, we will omit this optimization for now. See
28708 ** ticket #2685.
28709 */
28710#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400
28711 assert( winMutex_isInit==1 )((void) (0));
28712 assert( winMutex_isNt>=-1 && winMutex_isNt<=1 )((void) (0));
28713 if( winMutex_isNt<0 ){
28714 winMutex_isNt = sqlite3_win32_is_nt();
28715 }
28716 assert( winMutex_isNt==0 || winMutex_isNt==1 )((void) (0));
28717 if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){
28718#ifdef SQLITE_DEBUG
28719 p->owner = tid;
28720 p->nRef++;
28721#endif
28722 rc = SQLITE_OK0;
28723 }
28724#else
28725 UNUSED_PARAMETER(p)(void)(p);
28726#endif
28727#ifdef SQLITE_DEBUG
28728 if( p->trace ){
28729 OSTRACE(("TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
28730 tid, p->id, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
28731 }
28732#endif
28733 return rc;
28734}
28735
28736/*
28737** The sqlite3_mutex_leave() routine exits a mutex that was
28738** previously entered by the same thread. The behavior
28739** is undefined if the mutex is not currently entered or
28740** is not currently allocated. SQLite will never do either.
28741*/
28742static void winMutexLeave(sqlite3_mutex *p){
28743#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
28744 DWORD tid = GetCurrentThreadId();
28745#endif
28746 assert( p )((void) (0));
28747#ifdef SQLITE_DEBUG
28748 assert( p->nRef>0 )((void) (0));
28749 assert( p->owner==tid )((void) (0));
28750 p->nRef--;
28751 if( p->nRef==0 ) p->owner = 0;
28752 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE )((void) (0));
28753#endif
28754 assert( winMutex_isInit==1 )((void) (0));
28755 LeaveCriticalSection(&p->mutex);
28756#ifdef SQLITE_DEBUG
28757 if( p->trace ){
28758 OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
28759 tid, p->id, p, p->trace, p->nRef));
28760 }
28761#endif
28762}
28763
28764SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
28765 static const sqlite3_mutex_methods sMutex = {
28766 winMutexInit,
28767 winMutexEnd,
28768 winMutexAlloc,
28769 winMutexFree,
28770 winMutexEnter,
28771 winMutexTry,
28772 winMutexLeave,
28773#ifdef SQLITE_DEBUG
28774 winMutexHeld,
28775 winMutexNotheld
28776#else
28777 0,
28778 0
28779#endif
28780 };
28781 return &sMutex;
28782}
28783
28784#endif /* SQLITE_MUTEX_W32 */
28785
28786/************** End of mutex_w32.c *******************************************/
28787/************** Begin file malloc.c ******************************************/
28788/*
28789** 2001 September 15
28790**
28791** The author disclaims copyright to this source code. In place of
28792** a legal notice, here is a blessing:
28793**
28794** May you do good and not evil.
28795** May you find forgiveness for yourself and forgive others.
28796** May you share freely, never taking more than you give.
28797**
28798*************************************************************************
28799**
28800** Memory allocation functions used throughout sqlite.
28801*/
28802/* #include "sqliteInt.h" */
28803/* #include <stdarg.h> */
28804
28805/*
28806** Attempt to release up to n bytes of non-essential memory currently
28807** held by SQLite. An example of non-essential memory is memory used to
28808** cache database pages that are not currently in use.
28809*/
28810SQLITE_API int sqlite3_release_memory(int n){
28811#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
28812 return sqlite3PcacheReleaseMemory(n);
28813#else
28814 /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
28815 ** is a no-op returning zero if SQLite is not compiled with
28816 ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
28817 UNUSED_PARAMETER(n)(void)(n);
28818 return 0;
28819#endif
28820}
28821
28822/*
28823** Default value of the hard heap limit. 0 means "no limit".
28824*/
28825#ifndef SQLITE_MAX_MEMORY0
28826# define SQLITE_MAX_MEMORY0 0
28827#endif
28828
28829/*
28830** State information local to the memory allocation subsystem.
28831*/
28832static SQLITE_WSD struct Mem0Global {
28833 sqlite3_mutex *mutex; /* Mutex to serialize access */
28834 sqlite3_int64 alarmThreshold; /* The soft heap limit */
28835 sqlite3_int64 hardLimit; /* The hard upper bound on memory */
28836
28837 /*
28838 ** True if heap is nearly "full" where "full" is defined by the
28839 ** sqlite3_soft_heap_limit() setting.
28840 */
28841 int nearlyFull;
28842} mem0mem0 = { 0, SQLITE_MAX_MEMORY0, SQLITE_MAX_MEMORY0, 0 };
28843
28844#define mem0mem0 GLOBAL(struct Mem0Global, mem0)mem0
28845
28846/*
28847** Return the memory allocator mutex. sqlite3_status() needs it.
28848*/
28849SQLITE_PRIVATEstatic sqlite3_mutex *sqlite3MallocMutex(void){
28850 return mem0mem0.mutex;
28851}
28852
28853#ifndef SQLITE_OMIT_DEPRECATED1
28854/*
28855** Deprecated external interface. It used to set an alarm callback
28856** that was invoked when memory usage grew too large. Now it is a
28857** no-op.
28858*/
28859SQLITE_API int sqlite3_memory_alarm(
28860 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
28861 void *pArg,
28862 sqlite3_int64 iThreshold
28863){
28864 (void)xCallback;
28865 (void)pArg;
28866 (void)iThreshold;
28867 return SQLITE_OK0;
28868}
28869#endif
28870
28871/*
28872** Set the soft heap-size limit for the library. An argument of
28873** zero disables the limit. A negative argument is a no-op used to
28874** obtain the return value.
28875**
28876** The return value is the value of the heap limit just before this
28877** interface was called.
28878**
28879** If the hard heap limit is enabled, then the soft heap limit cannot
28880** be disabled nor raised above the hard heap limit.
28881*/
28882SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
28883 sqlite3_int64 priorLimit;
28884 sqlite3_int64 excess;
28885 sqlite3_int64 nUsed;
28886#ifndef SQLITE_OMIT_AUTOINIT
28887 int rc = sqlite3_initialize();
28888 if( rc ) return -1;
28889#endif
28890 sqlite3_mutex_enter(mem0mem0.mutex);
28891 priorLimit = mem0mem0.alarmThreshold;
28892 if( n<0 ){
28893 sqlite3_mutex_leave(mem0mem0.mutex);
28894 return priorLimit;
28895 }
28896 if( mem0mem0.hardLimit>0 && (n>mem0mem0.hardLimit || n==0) ){
28897 n = mem0mem0.hardLimit;
28898 }
28899 mem0mem0.alarmThreshold = n;
28900 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED0);
28901 AtomicStore(&mem0.nearlyFull, n>0 && n<=nUsed)__atomic_store_n((&mem0.nearlyFull),(n>0 && n<=
nUsed),0)
;
28902 sqlite3_mutex_leave(mem0mem0.mutex);
28903 excess = sqlite3_memory_used() - n;
28904 if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
28905 return priorLimit;
28906}
28907SQLITE_API void sqlite3_soft_heap_limit(int n){
28908 if( n<0 ) n = 0;
28909 sqlite3_soft_heap_limit64(n);
28910}
28911
28912/*
28913** Set the hard heap-size limit for the library. An argument of zero
28914** disables the hard heap limit. A negative argument is a no-op used
28915** to obtain the return value without affecting the hard heap limit.
28916**
28917** The return value is the value of the hard heap limit just prior to
28918** calling this interface.
28919**
28920** Setting the hard heap limit will also activate the soft heap limit
28921** and constrain the soft heap limit to be no more than the hard heap
28922** limit.
28923*/
28924SQLITE_API sqlite3_int64 sqlite3_hard_heap_limit64(sqlite3_int64 n){
28925 sqlite3_int64 priorLimit;
28926#ifndef SQLITE_OMIT_AUTOINIT
28927 int rc = sqlite3_initialize();
28928 if( rc ) return -1;
28929#endif
28930 sqlite3_mutex_enter(mem0mem0.mutex);
28931 priorLimit = mem0mem0.hardLimit;
28932 if( n>=0 ){
28933 mem0mem0.hardLimit = n;
28934 if( n<mem0mem0.alarmThreshold || mem0mem0.alarmThreshold==0 ){
28935 mem0mem0.alarmThreshold = n;
28936 }
28937 }
28938 sqlite3_mutex_leave(mem0mem0.mutex);
28939 return priorLimit;
28940}
28941
28942
28943/*
28944** Initialize the memory allocation subsystem.
28945*/
28946SQLITE_PRIVATEstatic int sqlite3MallocInit(void){
28947 int rc;
28948 if( sqlite3GlobalConfigsqlite3Config.m.xMalloc==0 ){
28949 sqlite3MemSetDefault();
28950 }
28951 mem0mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM3);
28952 if( sqlite3GlobalConfigsqlite3Config.pPage==0 || sqlite3GlobalConfigsqlite3Config.szPage<512
28953 || sqlite3GlobalConfigsqlite3Config.nPage<=0 ){
28954 sqlite3GlobalConfigsqlite3Config.pPage = 0;
28955 sqlite3GlobalConfigsqlite3Config.szPage = 0;
28956 }
28957 rc = sqlite3GlobalConfigsqlite3Config.m.xInit(sqlite3GlobalConfigsqlite3Config.m.pAppData);
28958 if( rc!=SQLITE_OK0 ) memset(&mem0mem0, 0, sizeof(mem0mem0));
28959 return rc;
28960}
28961
28962/*
28963** Return true if the heap is currently under memory pressure - in other
28964** words if the amount of heap used is close to the limit set by
28965** sqlite3_soft_heap_limit().
28966*/
28967SQLITE_PRIVATEstatic int sqlite3HeapNearlyFull(void){
28968 return AtomicLoad(&mem0.nearlyFull)__atomic_load_n((&mem0.nearlyFull),0);
28969}
28970
28971/*
28972** Deinitialize the memory allocation subsystem.
28973*/
28974SQLITE_PRIVATEstatic void sqlite3MallocEnd(void){
28975 if( sqlite3GlobalConfigsqlite3Config.m.xShutdown ){
28976 sqlite3GlobalConfigsqlite3Config.m.xShutdown(sqlite3GlobalConfigsqlite3Config.m.pAppData);
28977 }
28978 memset(&mem0mem0, 0, sizeof(mem0mem0));
28979}
28980
28981/*
28982** Return the amount of memory currently checked out.
28983*/
28984SQLITE_API sqlite3_int64 sqlite3_memory_used(void){
28985 sqlite3_int64 res, mx;
28986 sqlite3_status64(SQLITE_STATUS_MEMORY_USED0, &res, &mx, 0);
28987 return res;
28988}
28989
28990/*
28991** Return the maximum amount of memory that has ever been
28992** checked out since either the beginning of this process
28993** or since the most recent reset.
28994*/
28995SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
28996 sqlite3_int64 res, mx;
28997 sqlite3_status64(SQLITE_STATUS_MEMORY_USED0, &res, &mx, resetFlag);
28998 return mx;
28999}
29000
29001/*
29002** Trigger the alarm
29003*/
29004static void sqlite3MallocAlarm(int nByte){
29005 if( mem0mem0.alarmThreshold<=0 ) return;
29006 sqlite3_mutex_leave(mem0mem0.mutex);
29007 sqlite3_release_memory(nByte);
29008 sqlite3_mutex_enter(mem0mem0.mutex);
29009}
29010
29011/*
29012** Do a memory allocation with statistics and alarms. Assume the
29013** lock is already held.
29014*/
29015static void mallocWithAlarm(int n, void **pp){
29016 void *p;
29017 int nFull;
29018 assert( sqlite3_mutex_held(mem0.mutex) )((void) (0));
29019 assert( n>0 )((void) (0));
29020
29021 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
29022 ** implementation of malloc_good_size(), which must be called in debug
29023 ** mode and specifically when the DMD "Dark Matter Detector" is enabled
29024 ** or else a crash results. Hence, do not attempt to optimize out the
29025 ** following xRoundup() call. */
29026 nFull = sqlite3GlobalConfigsqlite3Config.m.xRoundup(n);
29027
29028 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE5, n);
29029 if( mem0mem0.alarmThreshold>0 ){
29030 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED0);
29031 if( nUsed >= mem0mem0.alarmThreshold - nFull ){
29032 AtomicStore(&mem0.nearlyFull, 1)__atomic_store_n((&mem0.nearlyFull),(1),0);
29033 sqlite3MallocAlarm(nFull);
29034 if( mem0mem0.hardLimit ){
29035 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED0);
29036 if( nUsed >= mem0mem0.hardLimit - nFull ){
29037 *pp = 0;
29038 return;
29039 }
29040 }
29041 }else{
29042 AtomicStore(&mem0.nearlyFull, 0)__atomic_store_n((&mem0.nearlyFull),(0),0);
29043 }
29044 }
29045 p = sqlite3GlobalConfigsqlite3Config.m.xMalloc(nFull);
29046#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
29047 if( p==0 && mem0mem0.alarmThreshold>0 ){
29048 sqlite3MallocAlarm(nFull);
29049 p = sqlite3GlobalConfigsqlite3Config.m.xMalloc(nFull);
29050 }
29051#endif
29052 if( p ){
29053 nFull = sqlite3MallocSize(p);
29054 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED0, nFull);
29055 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT9, 1);
29056 }
29057 *pp = p;
29058}
29059
29060/*
29061** Allocate memory. This routine is like sqlite3_malloc() except that it
29062** assumes the memory subsystem has already been initialized.
29063*/
29064SQLITE_PRIVATEstatic void *sqlite3Malloc(u64 n){
29065 void *p;
29066 if( n==0 || n>=0x7fffff00 ){
29067 /* A memory allocation of a number of bytes which is near the maximum
29068 ** signed integer value might cause an integer overflow inside of the
29069 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
29070 ** 255 bytes of overhead. SQLite itself will never use anything near
29071 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
29072 p = 0;
29073 }else if( sqlite3GlobalConfigsqlite3Config.bMemstat ){
29074 sqlite3_mutex_enter(mem0mem0.mutex);
29075 mallocWithAlarm((int)n, &p);
29076 sqlite3_mutex_leave(mem0mem0.mutex);
29077 }else{
29078 p = sqlite3GlobalConfigsqlite3Config.m.xMalloc((int)n);
29079 }
29080 assert( EIGHT_BYTE_ALIGNMENT(p) )((void) (0)); /* IMP: R-11148-40995 */
29081 return p;
29082}
29083
29084/*
29085** This version of the memory allocation is for use by the application.
29086** First make sure the memory subsystem is initialized, then do the
29087** allocation.
29088*/
29089SQLITE_API void *sqlite3_malloc(int n){
29090#ifndef SQLITE_OMIT_AUTOINIT
29091 if( sqlite3_initialize() ) return 0;
29092#endif
29093 return n<=0 ? 0 : sqlite3Malloc(n);
29094}
29095SQLITE_API void *sqlite3_malloc64(sqlite3_uint64 n){
29096#ifndef SQLITE_OMIT_AUTOINIT
29097 if( sqlite3_initialize() ) return 0;
29098#endif
29099 return sqlite3Malloc(n);
29100}
29101
29102/*
29103** TRUE if p is a lookaside memory allocation from db
29104*/
29105#ifndef SQLITE_OMIT_LOOKASIDE
29106static int isLookaside(sqlite3 *db, const void *p){
29107 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd)(((uptr)(p)>=(uptr)(db->lookaside.pStart))&&((uptr
)(p)<(uptr)(db->lookaside.pEnd)))
;
29108}
29109#else
29110#define isLookaside(A,B) 0
29111#endif
29112
29113/*
29114** Return the size of a memory allocation previously obtained from
29115** sqlite3Malloc() or sqlite3_malloc().
29116*/
29117SQLITE_PRIVATEstatic int sqlite3MallocSize(const void *p){
29118 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) )((void) (0));
29119 return sqlite3GlobalConfigsqlite3Config.m.xSize((void*)p);
29120}
29121static int lookasideMallocSize(sqlite3 *db, const void *p){
29122#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
29123 return p<db->lookaside.pMiddle ? db->lookaside.szTrue : LOOKASIDE_SMALL128;
29124#else
29125 return db->lookaside.szTrue;
29126#endif
29127}
29128SQLITE_PRIVATEstatic int sqlite3DbMallocSize(sqlite3 *db, const void *p){
29129 assert( p!=0 )((void) (0));
29130#ifdef SQLITE_DEBUG
29131 if( db==0 || !isLookaside(db,p) ){
29132 if( db==0 ){
29133 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) )((void) (0));
29134 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) )((void) (0));
29135 }else{
29136 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
29137 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
29138 }
29139 }
29140#endif
29141 if( db ){
29142 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
29143#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
29144 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
29145 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
29146 return LOOKASIDE_SMALL128;
29147 }
29148#endif
29149 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
29150 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
29151 return db->lookaside.szTrue;
29152 }
29153 }
29154 }
29155 return sqlite3GlobalConfigsqlite3Config.m.xSize((void*)p);
29156}
29157SQLITE_API sqlite3_uint64 sqlite3_msize(void *p){
29158 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) )((void) (0));
29159 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) )((void) (0));
29160 return p ? sqlite3GlobalConfigsqlite3Config.m.xSize(p) : 0;
29161}
29162
29163/*
29164** Free memory previously obtained from sqlite3Malloc().
29165*/
29166SQLITE_API void sqlite3_free(void *p){
29167 if( p==0 ) return; /* IMP: R-49053-54554 */
29168 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) )((void) (0));
29169 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) )((void) (0));
29170 if( sqlite3GlobalConfigsqlite3Config.bMemstat ){
29171 sqlite3_mutex_enter(mem0mem0.mutex);
29172 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED0, sqlite3MallocSize(p));
29173 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT9, 1);
29174 sqlite3GlobalConfigsqlite3Config.m.xFree(p);
29175 sqlite3_mutex_leave(mem0mem0.mutex);
29176 }else{
29177 sqlite3GlobalConfigsqlite3Config.m.xFree(p);
29178 }
29179}
29180
29181/*
29182** Add the size of memory allocation "p" to the count in
29183** *db->pnBytesFreed.
29184*/
29185static SQLITE_NOINLINE__attribute__((noinline)) void measureAllocationSize(sqlite3 *db, void *p){
29186 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
29187}
29188
29189/*
29190** Free memory that might be associated with a particular database
29191** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
29192** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
29193*/
29194SQLITE_PRIVATEstatic void sqlite3DbFreeNN(sqlite3 *db, void *p){
29195 assert( db==0 || sqlite3_mutex_held(db->mutex) )((void) (0));
29196 assert( p!=0 )((void) (0));
29197 if( db ){
29198 if( db->pnBytesFreed ){
29199 measureAllocationSize(db, p);
29200 return;
29201 }
29202 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
29203#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
29204 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
29205 LookasideSlot *pBuf = (LookasideSlot*)p;
29206#ifdef SQLITE_DEBUG
29207 memset(p, 0xaa, LOOKASIDE_SMALL128); /* Trash freed content */
29208#endif
29209 pBuf->pNext = db->lookaside.pSmallFree;
29210 db->lookaside.pSmallFree = pBuf;
29211 return;
29212 }
29213#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
29214 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
29215 LookasideSlot *pBuf = (LookasideSlot*)p;
29216#ifdef SQLITE_DEBUG
29217 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
29218#endif
29219 pBuf->pNext = db->lookaside.pFree;
29220 db->lookaside.pFree = pBuf;
29221 return;
29222 }
29223 }
29224 }
29225 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
29226 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
29227 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) )((void) (0));
29228 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
29229 sqlite3_free(p);
29230}
29231SQLITE_PRIVATEstatic void sqlite3DbFree(sqlite3 *db, void *p){
29232 assert( db==0 || sqlite3_mutex_held(db->mutex) )((void) (0));
29233 if( p ) sqlite3DbFreeNN(db, p);
29234}
29235
29236/*
29237** Change the size of an existing memory allocation
29238*/
29239SQLITE_PRIVATEstatic void *sqlite3Realloc(void *pOld, u64 nBytes){
29240 int nOld, nNew, nDiff;
29241 void *pNew;
29242 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) )((void) (0));
29243 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) )((void) (0));
29244 if( pOld==0 ){
29245 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
29246 }
29247 if( nBytes==0 ){
29248 sqlite3_free(pOld); /* IMP: R-26507-47431 */
29249 return 0;
29250 }
29251 if( nBytes>=0x7fffff00 ){
29252 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
29253 return 0;
29254 }
29255 nOld = sqlite3MallocSize(pOld);
29256 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
29257 ** argument to xRealloc is always a value returned by a prior call to
29258 ** xRoundup. */
29259 nNew = sqlite3GlobalConfigsqlite3Config.m.xRoundup((int)nBytes);
29260 if( nOld==nNew ){
29261 pNew = pOld;
29262 }else if( sqlite3GlobalConfigsqlite3Config.bMemstat ){
29263 sqlite3_int64 nUsed;
29264 sqlite3_mutex_enter(mem0mem0.mutex);
29265 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE5, (int)nBytes);
29266 nDiff = nNew - nOld;
29267 if( nDiff>0 && (nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED0)) >=
29268 mem0mem0.alarmThreshold-nDiff ){
29269 sqlite3MallocAlarm(nDiff);
29270 if( mem0mem0.hardLimit>0 && nUsed >= mem0mem0.hardLimit - nDiff ){
29271 sqlite3_mutex_leave(mem0mem0.mutex);
29272 return 0;
29273 }
29274 }
29275 pNew = sqlite3GlobalConfigsqlite3Config.m.xRealloc(pOld, nNew);
29276#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
29277 if( pNew==0 && mem0mem0.alarmThreshold>0 ){
29278 sqlite3MallocAlarm((int)nBytes);
29279 pNew = sqlite3GlobalConfigsqlite3Config.m.xRealloc(pOld, nNew);
29280 }
29281#endif
29282 if( pNew ){
29283 nNew = sqlite3MallocSize(pNew);
29284 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED0, nNew-nOld);
29285 }
29286 sqlite3_mutex_leave(mem0mem0.mutex);
29287 }else{
29288 pNew = sqlite3GlobalConfigsqlite3Config.m.xRealloc(pOld, nNew);
29289 }
29290 assert( EIGHT_BYTE_ALIGNMENT(pNew) )((void) (0)); /* IMP: R-11148-40995 */
29291 return pNew;
29292}
29293
29294/*
29295** The public interface to sqlite3Realloc. Make sure that the memory
29296** subsystem is initialized prior to invoking sqliteRealloc.
29297*/
29298SQLITE_API void *sqlite3_realloc(void *pOld, int n){
29299#ifndef SQLITE_OMIT_AUTOINIT
29300 if( sqlite3_initialize() ) return 0;
29301#endif
29302 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
29303 return sqlite3Realloc(pOld, n);
29304}
29305SQLITE_API void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
29306#ifndef SQLITE_OMIT_AUTOINIT
29307 if( sqlite3_initialize() ) return 0;
29308#endif
29309 return sqlite3Realloc(pOld, n);
29310}
29311
29312
29313/*
29314** Allocate and zero memory.
29315*/
29316SQLITE_PRIVATEstatic void *sqlite3MallocZero(u64 n){
29317 void *p = sqlite3Malloc(n);
29318 if( p ){
29319 memset(p, 0, (size_t)n);
29320 }
29321 return p;
29322}
29323
29324/*
29325** Allocate and zero memory. If the allocation fails, make
29326** the mallocFailed flag in the connection pointer.
29327*/
29328SQLITE_PRIVATEstatic void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
29329 void *p;
29330 testcase( db==0 );
29331 p = sqlite3DbMallocRaw(db, n);
29332 if( p ) memset(p, 0, (size_t)n);
29333 return p;
29334}
29335
29336
29337/* Finish the work of sqlite3DbMallocRawNN for the unusual and
29338** slower case when the allocation cannot be fulfilled using lookaside.
29339*/
29340static SQLITE_NOINLINE__attribute__((noinline)) void *dbMallocRawFinish(sqlite3 *db, u64 n){
29341 void *p;
29342 assert( db!=0 )((void) (0));
29343 p = sqlite3Malloc(n);
29344 if( !p ) sqlite3OomFault(db);
29345 sqlite3MemdebugSetType(p,
29346 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
29347 return p;
29348}
29349
29350/*
29351** Allocate memory, either lookaside (if possible) or heap.
29352** If the allocation fails, set the mallocFailed flag in
29353** the connection pointer.
29354**
29355** If db!=0 and db->mallocFailed is true (indicating a prior malloc
29356** failure on the same database connection) then always return 0.
29357** Hence for a particular database connection, once malloc starts
29358** failing, it fails consistently until mallocFailed is reset.
29359** This is an important assumption. There are many places in the
29360** code that do things like this:
29361**
29362** int *a = (int*)sqlite3DbMallocRaw(db, 100);
29363** int *b = (int*)sqlite3DbMallocRaw(db, 200);
29364** if( b ) a[10] = 9;
29365**
29366** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
29367** that all prior mallocs (ex: "a") worked too.
29368**
29369** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
29370** not a NULL pointer.
29371*/
29372SQLITE_PRIVATEstatic void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
29373 void *p;
29374 if( db ) return sqlite3DbMallocRawNN(db, n);
29375 p = sqlite3Malloc(n);
29376 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
29377 return p;
29378}
29379SQLITE_PRIVATEstatic void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
29380#ifndef SQLITE_OMIT_LOOKASIDE
29381 LookasideSlot *pBuf;
29382 assert( db!=0 )((void) (0));
29383 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
29384 assert( db->pnBytesFreed==0 )((void) (0));
29385 if( n>db->lookaside.sz ){
29386 if( !db->lookaside.bDisable ){
29387 db->lookaside.anStat[1]++;
29388 }else if( db->mallocFailed ){
29389 return 0;
29390 }
29391 return dbMallocRawFinish(db, n);
29392 }
29393#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
29394 if( n<=LOOKASIDE_SMALL128 ){
29395 if( (pBuf = db->lookaside.pSmallFree)!=0 ){
29396 db->lookaside.pSmallFree = pBuf->pNext;
29397 db->lookaside.anStat[0]++;
29398 return (void*)pBuf;
29399 }else if( (pBuf = db->lookaside.pSmallInit)!=0 ){
29400 db->lookaside.pSmallInit = pBuf->pNext;
29401 db->lookaside.anStat[0]++;
29402 return (void*)pBuf;
29403 }
29404 }
29405#endif
29406 if( (pBuf = db->lookaside.pFree)!=0 ){
29407 db->lookaside.pFree = pBuf->pNext;
29408 db->lookaside.anStat[0]++;
29409 return (void*)pBuf;
29410 }else if( (pBuf = db->lookaside.pInit)!=0 ){
29411 db->lookaside.pInit = pBuf->pNext;
29412 db->lookaside.anStat[0]++;
29413 return (void*)pBuf;
29414 }else{
29415 db->lookaside.anStat[2]++;
29416 }
29417#else
29418 assert( db!=0 )((void) (0));
29419 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
29420 assert( db->pnBytesFreed==0 )((void) (0));
29421 if( db->mallocFailed ){
29422 return 0;
29423 }
29424#endif
29425 return dbMallocRawFinish(db, n);
29426}
29427
29428/* Forward declaration */
29429static SQLITE_NOINLINE__attribute__((noinline)) void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
29430
29431/*
29432** Resize the block of memory pointed to by p to n bytes. If the
29433** resize fails, set the mallocFailed flag in the connection object.
29434*/
29435SQLITE_PRIVATEstatic void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
29436 assert( db!=0 )((void) (0));
29437 if( p==0 ) return sqlite3DbMallocRawNN(db, n);
29438 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
29439 if( ((uptr)p)<(uptr)db->lookaside.pEnd ){
29440#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
29441 if( ((uptr)p)>=(uptr)db->lookaside.pMiddle ){
29442 if( n<=LOOKASIDE_SMALL128 ) return p;
29443 }else
29444#endif
29445 if( ((uptr)p)>=(uptr)db->lookaside.pStart ){
29446 if( n<=db->lookaside.szTrue ) return p;
29447 }
29448 }
29449 return dbReallocFinish(db, p, n);
29450}
29451static SQLITE_NOINLINE__attribute__((noinline)) void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
29452 void *pNew = 0;
29453 assert( db!=0 )((void) (0));
29454 assert( p!=0 )((void) (0));
29455 if( db->mallocFailed==0 ){
29456 if( isLookaside(db, p) ){
29457 pNew = sqlite3DbMallocRawNN(db, n);
29458 if( pNew ){
29459 memcpy(pNew, p, lookasideMallocSize(db, p));
29460 sqlite3DbFree(db, p);
29461 }
29462 }else{
29463 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
29464 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
29465 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
29466 pNew = sqlite3Realloc(p, n);
29467 if( !pNew ){
29468 sqlite3OomFault(db);
29469 }
29470 sqlite3MemdebugSetType(pNew,
29471 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
29472 }
29473 }
29474 return pNew;
29475}
29476
29477/*
29478** Attempt to reallocate p. If the reallocation fails, then free p
29479** and set the mallocFailed flag in the database connection.
29480*/
29481SQLITE_PRIVATEstatic void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
29482 void *pNew;
29483 pNew = sqlite3DbRealloc(db, p, n);
29484 if( !pNew ){
29485 sqlite3DbFree(db, p);
29486 }
29487 return pNew;
29488}
29489
29490/*
29491** Make a copy of a string in memory obtained from sqliteMalloc(). These
29492** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
29493** is because when memory debugging is turned on, these two functions are
29494** called via macros that record the current file and line number in the
29495** ThreadData structure.
29496*/
29497SQLITE_PRIVATEstatic char *sqlite3DbStrDup(sqlite3 *db, const char *z){
29498 char *zNew;
29499 size_t n;
29500 if( z==0 ){
29501 return 0;
29502 }
29503 n = strlen(z) + 1;
29504 zNew = sqlite3DbMallocRaw(db, n);
29505 if( zNew ){
29506 memcpy(zNew, z, n);
29507 }
29508 return zNew;
29509}
29510SQLITE_PRIVATEstatic char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
29511 char *zNew;
29512 assert( db!=0 )((void) (0));
29513 assert( z!=0 || n==0 )((void) (0));
29514 assert( (n&0x7fffffff)==n )((void) (0));
29515 zNew = z ? sqlite3DbMallocRawNN(db, n+1) : 0;
29516 if( zNew ){
29517 memcpy(zNew, z, (size_t)n);
29518 zNew[n] = 0;
29519 }
29520 return zNew;
29521}
29522
29523/*
29524** The text between zStart and zEnd represents a phrase within a larger
29525** SQL statement. Make a copy of this phrase in space obtained form
29526** sqlite3DbMalloc(). Omit leading and trailing whitespace.
29527*/
29528SQLITE_PRIVATEstatic char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
29529 int n;
29530 while( sqlite3Isspace(zStart[0])(sqlite3CtypeMap[(unsigned char)(zStart[0])]&0x01) ) zStart++;
29531 n = (int)(zEnd - zStart);
29532 while( ALWAYS(n>0)(n>0) && sqlite3Isspace(zStart[n-1])(sqlite3CtypeMap[(unsigned char)(zStart[n-1])]&0x01) ) n--;
29533 return sqlite3DbStrNDup(db, zStart, n);
29534}
29535
29536/*
29537** Free any prior content in *pz and replace it with a copy of zNew.
29538*/
29539SQLITE_PRIVATEstatic void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
29540 char *z = sqlite3DbStrDup(db, zNew);
29541 sqlite3DbFree(db, *pz);
29542 *pz = z;
29543}
29544
29545/*
29546** Call this routine to record the fact that an OOM (out-of-memory) error
29547** has happened. This routine will set db->mallocFailed, and also
29548** temporarily disable the lookaside memory allocator and interrupt
29549** any running VDBEs.
29550**
29551** Always return a NULL pointer so that this routine can be invoked using
29552**
29553** return sqlite3OomFault(db);
29554**
29555** and thereby avoid unnecessary stack frame allocations for the overwhelmingly
29556** common case where no OOM occurs.
29557*/
29558SQLITE_PRIVATEstatic void *sqlite3OomFault(sqlite3 *db){
29559 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
29560 db->mallocFailed = 1;
29561 if( db->nVdbeExec>0 ){
29562 AtomicStore(&db->u1.isInterrupted, 1)__atomic_store_n((&db->u1.isInterrupted),(1),0);
29563 }
29564 DisableLookasidedb->lookaside.bDisable++;db->lookaside.sz=0;
29565 if( db->pParse ){
29566 sqlite3ErrorMsg(db->pParse, "out of memory");
29567 db->pParse->rc = SQLITE_NOMEM_BKPT7;
29568 }
29569 }
29570 return 0;
29571}
29572
29573/*
29574** This routine reactivates the memory allocator and clears the
29575** db->mallocFailed flag as necessary.
29576**
29577** The memory allocator is not restarted if there are running
29578** VDBEs.
29579*/
29580SQLITE_PRIVATEstatic void sqlite3OomClear(sqlite3 *db){
29581 if( db->mallocFailed && db->nVdbeExec==0 ){
29582 db->mallocFailed = 0;
29583 AtomicStore(&db->u1.isInterrupted, 0)__atomic_store_n((&db->u1.isInterrupted),(0),0);
29584 assert( db->lookaside.bDisable>0 )((void) (0));
29585 EnableLookasidedb->lookaside.bDisable--; db->lookaside.sz=db->lookaside
.bDisable?0:db->lookaside.szTrue
;
29586 }
29587}
29588
29589/*
29590** Take actions at the end of an API call to deal with error codes.
29591*/
29592static SQLITE_NOINLINE__attribute__((noinline)) int apiHandleError(sqlite3 *db, int rc){
29593 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
29594 sqlite3OomClear(db);
29595 sqlite3Error(db, SQLITE_NOMEM7);
29596 return SQLITE_NOMEM_BKPT7;
29597 }
29598 return rc & db->errMask;
29599}
29600
29601/*
29602** This function must be called before exiting any API function (i.e.
29603** returning control to the user) that has called sqlite3_malloc or
29604** sqlite3_realloc.
29605**
29606** The returned value is normally a copy of the second argument to this
29607** function. However, if a malloc() failure has occurred since the previous
29608** invocation SQLITE_NOMEM is returned instead.
29609**
29610** If an OOM as occurred, then the connection error-code (the value
29611** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
29612*/
29613SQLITE_PRIVATEstatic int sqlite3ApiExit(sqlite3* db, int rc){
29614 /* If the db handle must hold the connection handle mutex here.
29615 ** Otherwise the read (and possible write) of db->mallocFailed
29616 ** is unsafe, as is the call to sqlite3Error().
29617 */
29618 assert( db!=0 )((void) (0));
29619 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
29620 if( db->mallocFailed || rc ){
29621 return apiHandleError(db, rc);
29622 }
29623 return rc & db->errMask;
29624}
29625
29626/************** End of malloc.c **********************************************/
29627/************** Begin file printf.c ******************************************/
29628/*
29629** The "printf" code that follows dates from the 1980's. It is in
29630** the public domain.
29631**
29632**************************************************************************
29633**
29634** This file contains code for a set of "printf"-like routines. These
29635** routines format strings much like the printf() from the standard C
29636** library, though the implementation here has enhancements to support
29637** SQLite.
29638*/
29639/* #include "sqliteInt.h" */
29640
29641/*
29642** Conversion types fall into various categories as defined by the
29643** following enumeration.
29644*/
29645#define etRADIX0 0 /* non-decimal integer types. %x %o */
29646#define etFLOAT1 1 /* Floating point. %f */
29647#define etEXP2 2 /* Exponentional notation. %e and %E */
29648#define etGENERIC3 3 /* Floating or exponential, depending on exponent. %g */
29649#define etSIZE4 4 /* Return number of characters processed so far. %n */
29650#define etSTRING5 5 /* Strings. %s */
29651#define etDYNSTRING6 6 /* Dynamically allocated strings. %z */
29652#define etPERCENT7 7 /* Percent symbol. %% */
29653#define etCHARX8 8 /* Characters. %c */
29654/* The rest are extensions, not normally found in printf() */
29655#define etSQLESCAPE9 9 /* Strings with '\'' doubled. %q */
29656#define etSQLESCAPE210 10 /* Strings with '\'' doubled and enclosed in '',
29657 NULL pointers replaced by SQL NULL. %Q */
29658#define etTOKEN11 11 /* a pointer to a Token structure */
29659#define etSRCITEM12 12 /* a pointer to a SrcItem */
29660#define etPOINTER13 13 /* The %p conversion */
29661#define etSQLESCAPE314 14 /* %w -> Strings with '\"' doubled */
29662#define etORDINAL15 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
29663#define etDECIMAL16 16 /* %d or %u, but not %x, %o */
29664
29665#define etINVALID17 17 /* Any unrecognized conversion type */
29666
29667
29668/*
29669** An "etByte" is an 8-bit unsigned value.
29670*/
29671typedef unsigned char etByte;
29672
29673/*
29674** Each builtin conversion character (ex: the 'd' in "%d") is described
29675** by an instance of the following structure
29676*/
29677typedef struct et_info { /* Information about each format field */
29678 char fmttype; /* The format field code letter */
29679 etByte base; /* The base for radix conversion */
29680 etByte flags; /* One or more of FLAG_ constants below */
29681 etByte type; /* Conversion paradigm */
29682 etByte charset; /* Offset into aDigits[] of the digits string */
29683 etByte prefix; /* Offset into aPrefix[] of the prefix string */
29684} et_info;
29685
29686/*
29687** Allowed values for et_info.flags
29688*/
29689#define FLAG_SIGNED1 1 /* True if the value to convert is signed */
29690#define FLAG_STRING4 4 /* Allow infinite precision */
29691
29692
29693/*
29694** The following table is searched linearly, so it is good to put the
29695** most frequently used conversion types first.
29696*/
29697static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
29698static const char aPrefix[] = "-x0\000X0";
29699static const et_info fmtinfo[] = {
29700 { 'd', 10, 1, etDECIMAL16, 0, 0 },
29701 { 's', 0, 4, etSTRING5, 0, 0 },
29702 { 'g', 0, 1, etGENERIC3, 30, 0 },
29703 { 'z', 0, 4, etDYNSTRING6, 0, 0 },
29704 { 'q', 0, 4, etSQLESCAPE9, 0, 0 },
29705 { 'Q', 0, 4, etSQLESCAPE210, 0, 0 },
29706 { 'w', 0, 4, etSQLESCAPE314, 0, 0 },
29707 { 'c', 0, 0, etCHARX8, 0, 0 },
29708 { 'o', 8, 0, etRADIX0, 0, 2 },
29709 { 'u', 10, 0, etDECIMAL16, 0, 0 },
29710 { 'x', 16, 0, etRADIX0, 16, 1 },
29711 { 'X', 16, 0, etRADIX0, 0, 4 },
29712#ifndef SQLITE_OMIT_FLOATING_POINT
29713 { 'f', 0, 1, etFLOAT1, 0, 0 },
29714 { 'e', 0, 1, etEXP2, 30, 0 },
29715 { 'E', 0, 1, etEXP2, 14, 0 },
29716 { 'G', 0, 1, etGENERIC3, 14, 0 },
29717#endif
29718 { 'i', 10, 1, etDECIMAL16, 0, 0 },
29719 { 'n', 0, 0, etSIZE4, 0, 0 },
29720 { '%', 0, 0, etPERCENT7, 0, 0 },
29721 { 'p', 16, 0, etPOINTER13, 0, 1 },
29722
29723 /* All the rest are undocumented and are for internal use only */
29724 { 'T', 0, 0, etTOKEN11, 0, 0 },
29725 { 'S', 0, 0, etSRCITEM12, 0, 0 },
29726 { 'r', 10, 1, etORDINAL15, 0, 0 },
29727};
29728
29729/* Notes:
29730**
29731** %S Takes a pointer to SrcItem. Shows name or database.name
29732** %!S Like %S but prefer the zName over the zAlias
29733*/
29734
29735/* Floating point constants used for rounding */
29736static const double arRound[] = {
29737 5.0e-01, 5.0e-02, 5.0e-03, 5.0e-04, 5.0e-05,
29738 5.0e-06, 5.0e-07, 5.0e-08, 5.0e-09, 5.0e-10,
29739};
29740
29741/*
29742** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
29743** conversions will work.
29744*/
29745#ifndef SQLITE_OMIT_FLOATING_POINT
29746/*
29747** "*val" is a double such that 0.1 <= *val < 10.0
29748** Return the ascii code for the leading digit of *val, then
29749** multiply "*val" by 10.0 to renormalize.
29750**
29751** Example:
29752** input: *val = 3.14159
29753** output: *val = 1.4159 function return = '3'
29754**
29755** The counter *cnt is incremented each time. After counter exceeds
29756** 16 (the number of significant digits in a 64-bit float) '0' is
29757** always returned.
29758*/
29759static char et_getdigit(LONGDOUBLE_TYPElong double *val, int *cnt){
29760 int digit;
29761 LONGDOUBLE_TYPElong double d;
29762 if( (*cnt)<=0 ) return '0';
29763 (*cnt)--;
29764 digit = (int)*val;
29765 d = digit;
29766 digit += '0';
29767 *val = (*val - d)*10.0;
29768 return (char)digit;
29769}
29770#endif /* SQLITE_OMIT_FLOATING_POINT */
29771
29772/*
29773** Set the StrAccum object to an error mode.
29774*/
29775SQLITE_PRIVATEstatic void sqlite3StrAccumSetError(StrAccum *p, u8 eError){
29776 assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG )((void) (0));
29777 p->accError = eError;
29778 if( p->mxAlloc ) sqlite3_str_reset(p);
29779 if( eError==SQLITE_TOOBIG18 ) sqlite3ErrorToParser(p->db, eError);
29780}
29781
29782/*
29783** Extra argument values from a PrintfArguments object
29784*/
29785static sqlite3_int64 getIntArg(PrintfArguments *p){
29786 if( p->nArg<=p->nUsed ) return 0;
29787 return sqlite3_value_int64(p->apArg[p->nUsed++]);
29788}
29789static double getDoubleArg(PrintfArguments *p){
29790 if( p->nArg<=p->nUsed ) return 0.0;
29791 return sqlite3_value_double(p->apArg[p->nUsed++]);
29792}
29793static char *getTextArg(PrintfArguments *p){
29794 if( p->nArg<=p->nUsed ) return 0;
29795 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
29796}
29797
29798/*
29799** Allocate memory for a temporary buffer needed for printf rendering.
29800**
29801** If the requested size of the temp buffer is larger than the size
29802** of the output buffer in pAccum, then cause an SQLITE_TOOBIG error.
29803** Do the size check before the memory allocation to prevent rogue
29804** SQL from requesting large allocations using the precision or width
29805** field of the printf() function.
29806*/
29807static char *printfTempBuf(sqlite3_str *pAccum, sqlite3_int64 n){
29808 char *z;
29809 if( pAccum->accError ) return 0;
29810 if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){
29811 sqlite3StrAccumSetError(pAccum, SQLITE_TOOBIG18);
29812 return 0;
29813 }
29814 z = sqlite3DbMallocRaw(pAccum->db, n);
29815 if( z==0 ){
29816 sqlite3StrAccumSetError(pAccum, SQLITE_NOMEM7);
29817 }
29818 return z;
29819}
29820
29821/*
29822** On machines with a small stack size, you can redefine the
29823** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
29824*/
29825#ifndef SQLITE_PRINT_BUF_SIZE70
29826# define SQLITE_PRINT_BUF_SIZE70 70
29827#endif
29828#define etBUFSIZE70 SQLITE_PRINT_BUF_SIZE70 /* Size of the output buffer */
29829
29830/*
29831** Hard limit on the precision of floating-point conversions.
29832*/
29833#ifndef SQLITE_PRINTF_PRECISION_LIMIT
29834# define SQLITE_FP_PRECISION_LIMIT100000000 100000000
29835#endif
29836
29837/*
29838** Render a string given by "fmt" into the StrAccum object.
29839*/
29840SQLITE_API void sqlite3_str_vappendf(
29841 sqlite3_str *pAccum, /* Accumulate results here */
29842 const char *fmt, /* Format string */
29843 va_list ap /* arguments */
29844){
29845 int c; /* Next character in the format string */
29846 char *bufpt; /* Pointer to the conversion buffer */
29847 int precision; /* Precision of the current field */
29848 int length; /* Length of the field */
29849 int idx; /* A general purpose loop counter */
29850 int width; /* Width of the current field */
29851 etByte flag_leftjustify; /* True if "-" flag is present */
29852 etByte flag_prefix; /* '+' or ' ' or 0 for prefix */
29853 etByte flag_alternateform; /* True if "#" flag is present */
29854 etByte flag_altform2; /* True if "!" flag is present */
29855 etByte flag_zeropad; /* True if field width constant starts with zero */
29856 etByte flag_long; /* 1 for the "l" flag, 2 for "ll", 0 by default */
29857 etByte done; /* Loop termination flag */
29858 etByte cThousand; /* Thousands separator for %d and %u */
29859 etByte xtype = etINVALID17; /* Conversion paradigm */
29860 u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */
29861 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
29862 sqlite_uint64 longvalue; /* Value for integer types */
29863 LONGDOUBLE_TYPElong double realvalue; /* Value for real types */
29864 const et_info *infop; /* Pointer to the appropriate info structure */
29865 char *zOut; /* Rendering buffer */
29866 int nOut; /* Size of the rendering buffer */
29867 char *zExtra = 0; /* Malloced memory used by some conversion */
29868#ifndef SQLITE_OMIT_FLOATING_POINT
29869 int exp, e2; /* exponent of real numbers */
29870 int nsd; /* Number of significant digits returned */
29871 double rounder; /* Used for rounding floating point values */
29872 etByte flag_dp; /* True if decimal point should be shown */
29873 etByte flag_rtz; /* True if trailing zeros should be removed */
29874#endif
29875 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
29876 char buf[etBUFSIZE70]; /* Conversion buffer */
29877
29878 /* pAccum never starts out with an empty buffer that was obtained from
29879 ** malloc(). This precondition is required by the mprintf("%z...")
29880 ** optimization. */
29881 assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 )((void) (0));
29882
29883 bufpt = 0;
29884 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC0x02)!=0 ){
29885 pArgList = va_arg(ap, PrintfArguments*)__builtin_va_arg(ap, PrintfArguments*);
29886 bArgList = 1;
29887 }else{
29888 bArgList = 0;
29889 }
29890 for(; (c=(*fmt))!=0; ++fmt){
29891 if( c!='%' ){
29892 bufpt = (char *)fmt;
29893#if HAVE_STRCHRNUL
29894 fmt = strchrnul(fmt, '%');
29895#else
29896 do{ fmt++; }while( *fmt && *fmt != '%' );
29897#endif
29898 sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt));
29899 if( *fmt==0 ) break;
29900 }
29901 if( (c=(*++fmt))==0 ){
29902 sqlite3_str_append(pAccum, "%", 1);
29903 break;
29904 }
29905 /* Find out what flags are present */
29906 flag_leftjustify = flag_prefix = cThousand =
29907 flag_alternateform = flag_altform2 = flag_zeropad = 0;
29908 done = 0;
29909 width = 0;
29910 flag_long = 0;
29911 precision = -1;
29912 do{
29913 switch( c ){
29914 case '-': flag_leftjustify = 1; break;
29915 case '+': flag_prefix = '+'; break;
29916 case ' ': flag_prefix = ' '; break;
29917 case '#': flag_alternateform = 1; break;
29918 case '!': flag_altform2 = 1; break;
29919 case '0': flag_zeropad = 1; break;
29920 case ',': cThousand = ','; break;
29921 default: done = 1; break;
29922 case 'l': {
29923 flag_long = 1;
29924 c = *++fmt;
29925 if( c=='l' ){
29926 c = *++fmt;
29927 flag_long = 2;
29928 }
29929 done = 1;
29930 break;
29931 }
29932 case '1': case '2': case '3': case '4': case '5':
29933 case '6': case '7': case '8': case '9': {
29934 unsigned wx = c - '0';
29935 while( (c = *++fmt)>='0' && c<='9' ){
29936 wx = wx*10 + c - '0';
29937 }
29938 testcase( wx>0x7fffffff );
29939 width = wx & 0x7fffffff;
29940#ifdef SQLITE_PRINTF_PRECISION_LIMIT
29941 if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
29942 width = SQLITE_PRINTF_PRECISION_LIMIT;
29943 }
29944#endif
29945 if( c!='.' && c!='l' ){
29946 done = 1;
29947 }else{
29948 fmt--;
29949 }
29950 break;
29951 }
29952 case '*': {
29953 if( bArgList ){
29954 width = (int)getIntArg(pArgList);
29955 }else{
29956 width = va_arg(ap,int)__builtin_va_arg(ap, int);
29957 }
29958 if( width<0 ){
29959 flag_leftjustify = 1;
29960 width = width >= -2147483647 ? -width : 0;
29961 }
29962#ifdef SQLITE_PRINTF_PRECISION_LIMIT
29963 if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
29964 width = SQLITE_PRINTF_PRECISION_LIMIT;
29965 }
29966#endif
29967 if( (c = fmt[1])!='.' && c!='l' ){
29968 c = *++fmt;
29969 done = 1;
29970 }
29971 break;
29972 }
29973 case '.': {
29974 c = *++fmt;
29975 if( c=='*' ){
29976 if( bArgList ){
29977 precision = (int)getIntArg(pArgList);
29978 }else{
29979 precision = va_arg(ap,int)__builtin_va_arg(ap, int);
29980 }
29981 if( precision<0 ){
29982 precision = precision >= -2147483647 ? -precision : -1;
29983 }
29984 c = *++fmt;
29985 }else{
29986 unsigned px = 0;
29987 while( c>='0' && c<='9' ){
29988 px = px*10 + c - '0';
29989 c = *++fmt;
29990 }
29991 testcase( px>0x7fffffff );
29992 precision = px & 0x7fffffff;
29993 }
29994#ifdef SQLITE_PRINTF_PRECISION_LIMIT
29995 if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){
29996 precision = SQLITE_PRINTF_PRECISION_LIMIT;
29997 }
29998#endif
29999 if( c=='l' ){
30000 --fmt;
30001 }else{
30002 done = 1;
30003 }
30004 break;
30005 }
30006 }
30007 }while( !done && (c=(*++fmt))!=0 );
30008
30009 /* Fetch the info entry for the field */
30010 infop = &fmtinfo[0];
30011 xtype = etINVALID17;
30012 for(idx=0; idx<ArraySize(fmtinfo)((int)(sizeof(fmtinfo)/sizeof(fmtinfo[0]))); idx++){
30013 if( c==fmtinfo[idx].fmttype ){
30014 infop = &fmtinfo[idx];
30015 xtype = infop->type;
30016 break;
30017 }
30018 }
30019
30020 /*
30021 ** At this point, variables are initialized as follows:
30022 **
30023 ** flag_alternateform TRUE if a '#' is present.
30024 ** flag_altform2 TRUE if a '!' is present.
30025 ** flag_prefix '+' or ' ' or zero
30026 ** flag_leftjustify TRUE if a '-' is present or if the
30027 ** field width was negative.
30028 ** flag_zeropad TRUE if the width began with 0.
30029 ** flag_long 1 for "l", 2 for "ll"
30030 ** width The specified field width. This is
30031 ** always non-negative. Zero is the default.
30032 ** precision The specified precision. The default
30033 ** is -1.
30034 ** xtype The class of the conversion.
30035 ** infop Pointer to the appropriate info struct.
30036 */
30037 assert( width>=0 )((void) (0));
30038 assert( precision>=(-1) )((void) (0));
30039 switch( xtype ){
30040 case etPOINTER13:
30041 flag_long = sizeof(char*)==sizeof(i64) ? 2 :
30042 sizeof(char*)==sizeof(long int) ? 1 : 0;
30043 /* no break */ deliberate_fall_through
30044 case etORDINAL15:
30045 case etRADIX0:
30046 cThousand = 0;
30047 /* no break */ deliberate_fall_through
30048 case etDECIMAL16:
30049 if( infop->flags & FLAG_SIGNED1 ){
30050 i64 v;
30051 if( bArgList ){
30052 v = getIntArg(pArgList);
30053 }else if( flag_long ){
30054 if( flag_long==2 ){
30055 v = va_arg(ap,i64)__builtin_va_arg(ap, i64) ;
30056 }else{
30057 v = va_arg(ap,long int)__builtin_va_arg(ap, long int);
30058 }
30059 }else{
30060 v = va_arg(ap,int)__builtin_va_arg(ap, int);
30061 }
30062 if( v<0 ){
30063 testcase( v==SMALLEST_INT64 );
30064 testcase( v==(-1) );
30065 longvalue = ~v;
30066 longvalue++;
30067 prefix = '-';
30068 }else{
30069 longvalue = v;
30070 prefix = flag_prefix;
30071 }
30072 }else{
30073 if( bArgList ){
30074 longvalue = (u64)getIntArg(pArgList);
30075 }else if( flag_long ){
30076 if( flag_long==2 ){
30077 longvalue = va_arg(ap,u64)__builtin_va_arg(ap, u64);
30078 }else{
30079 longvalue = va_arg(ap,unsigned long int)__builtin_va_arg(ap, unsigned long int);
30080 }
30081 }else{
30082 longvalue = va_arg(ap,unsigned int)__builtin_va_arg(ap, unsigned int);
30083 }
30084 prefix = 0;
30085 }
30086 if( longvalue==0 ) flag_alternateform = 0;
30087 if( flag_zeropad && precision<width-(prefix!=0) ){
30088 precision = width-(prefix!=0);
30089 }
30090 if( precision<etBUFSIZE70-10-etBUFSIZE70/3 ){
30091 nOut = etBUFSIZE70;
30092 zOut = buf;
30093 }else{
30094 u64 n;
30095 n = (u64)precision + 10;
30096 if( cThousand ) n += precision/3;
30097 zOut = zExtra = printfTempBuf(pAccum, n);
30098 if( zOut==0 ) return;
30099 nOut = (int)n;
30100 }
30101 bufpt = &zOut[nOut-1];
30102 if( xtype==etORDINAL15 ){
30103 static const char zOrd[] = "thstndrd";
30104 int x = (int)(longvalue % 10);
30105 if( x>=4 || (longvalue/10)%10==1 ){
30106 x = 0;
30107 }
30108 *(--bufpt) = zOrd[x*2+1];
30109 *(--bufpt) = zOrd[x*2];
30110 }
30111 {
30112 const char *cset = &aDigits[infop->charset];
30113 u8 base = infop->base;
30114 do{ /* Convert to ascii */
30115 *(--bufpt) = cset[longvalue%base];
30116 longvalue = longvalue/base;
30117 }while( longvalue>0 );
30118 }
30119 length = (int)(&zOut[nOut-1]-bufpt);
30120 while( precision>length ){
30121 *(--bufpt) = '0'; /* Zero pad */
30122 length++;
30123 }
30124 if( cThousand ){
30125 int nn = (length - 1)/3; /* Number of "," to insert */
30126 int ix = (length - 1)%3 + 1;
30127 bufpt -= nn;
30128 for(idx=0; nn>0; idx++){
30129 bufpt[idx] = bufpt[idx+nn];
30130 ix--;
30131 if( ix==0 ){
30132 bufpt[++idx] = cThousand;
30133 nn--;
30134 ix = 3;
30135 }
30136 }
30137 }
30138 if( prefix ) *(--bufpt) = prefix; /* Add sign */
30139 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
30140 const char *pre;
30141 char x;
30142 pre = &aPrefix[infop->prefix];
30143 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
30144 }
30145 length = (int)(&zOut[nOut-1]-bufpt);
30146 break;
30147 case etFLOAT1:
30148 case etEXP2:
30149 case etGENERIC3:
30150 if( bArgList ){
30151 realvalue = getDoubleArg(pArgList);
30152 }else{
30153 realvalue = va_arg(ap,double)__builtin_va_arg(ap, double);
30154 }
30155#ifdef SQLITE_OMIT_FLOATING_POINT
30156 length = 0;
30157#else
30158 if( precision<0 ) precision = 6; /* Set default precision */
30159#ifdef SQLITE_FP_PRECISION_LIMIT100000000
30160 if( precision>SQLITE_FP_PRECISION_LIMIT100000000 ){
30161 precision = SQLITE_FP_PRECISION_LIMIT100000000;
30162 }
30163#endif
30164 if( realvalue<0.0 ){
30165 realvalue = -realvalue;
30166 prefix = '-';
30167 }else{
30168 prefix = flag_prefix;
30169 }
30170 if( xtype==etGENERIC3 && precision>0 ) precision--;
30171 testcase( precision>0xfff );
30172 idx = precision & 0xfff;
30173 rounder = arRound[idx%10];
30174 while( idx>=10 ){ rounder *= 1.0e-10; idx -= 10; }
30175 if( xtype==etFLOAT1 ){
30176 double rx = (double)realvalue;
30177 sqlite3_uint64 u;
30178 int ex;
30179 memcpy(&u, &rx, sizeof(u));
30180 ex = -1023 + (int)((u>>52)&0x7ff);
30181 if( precision+(ex/3) < 15 ) rounder += realvalue*3e-16;
30182 realvalue += rounder;
30183 }
30184 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
30185 exp = 0;
30186 if( sqlite3IsNaN((double)realvalue) ){
30187 bufpt = "NaN";
30188 length = 3;
30189 break;
30190 }
30191 if( realvalue>0.0 ){
30192 LONGDOUBLE_TYPElong double scale = 1.0;
30193 while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
30194 while( realvalue>=1e10*scale && exp<=350 ){ scale *= 1e10; exp+=10; }
30195 while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
30196 realvalue /= scale;
30197 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
30198 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
30199 if( exp>350 ){
30200 bufpt = buf;
30201 buf[0] = prefix;
30202 memcpy(buf+(prefix!=0),"Inf",4);
30203 length = 3+(prefix!=0);
30204 break;
30205 }
30206 }
30207 bufpt = buf;
30208 /*
30209 ** If the field type is etGENERIC, then convert to either etEXP
30210 ** or etFLOAT, as appropriate.
30211 */
30212 if( xtype!=etFLOAT1 ){
30213 realvalue += rounder;
30214 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
30215 }
30216 if( xtype==etGENERIC3 ){
30217 flag_rtz = !flag_alternateform;
30218 if( exp<-4 || exp>precision ){
30219 xtype = etEXP2;
30220 }else{
30221 precision = precision - exp;
30222 xtype = etFLOAT1;
30223 }
30224 }else{
30225 flag_rtz = flag_altform2;
30226 }
30227 if( xtype==etEXP2 ){
30228 e2 = 0;
30229 }else{
30230 e2 = exp;
30231 }
30232 {
30233 i64 szBufNeeded; /* Size of a temporary buffer needed */
30234 szBufNeeded = MAX(e2,0)((e2)>(0)?(e2):(0))+(i64)precision+(i64)width+15;
30235 if( szBufNeeded > etBUFSIZE70 ){
30236 bufpt = zExtra = printfTempBuf(pAccum, szBufNeeded);
30237 if( bufpt==0 ) return;
30238 }
30239 }
30240 zOut = bufpt;
30241 nsd = 16 + flag_altform2*10;
30242 flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
30243 /* The sign in front of the number */
30244 if( prefix ){
30245 *(bufpt++) = prefix;
30246 }
30247 /* Digits prior to the decimal point */
30248 if( e2<0 ){
30249 *(bufpt++) = '0';
30250 }else{
30251 for(; e2>=0; e2--){
30252 *(bufpt++) = et_getdigit(&realvalue,&nsd);
30253 }
30254 }
30255 /* The decimal point */
30256 if( flag_dp ){
30257 *(bufpt++) = '.';
30258 }
30259 /* "0" digits after the decimal point but before the first
30260 ** significant digit of the number */
30261 for(e2++; e2<0; precision--, e2++){
30262 assert( precision>0 )((void) (0));
30263 *(bufpt++) = '0';
30264 }
30265 /* Significant digits after the decimal point */
30266 while( (precision--)>0 ){
30267 *(bufpt++) = et_getdigit(&realvalue,&nsd);
30268 }
30269 /* Remove trailing zeros and the "." if no digits follow the "." */
30270 if( flag_rtz && flag_dp ){
30271 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
30272 assert( bufpt>zOut )((void) (0));
30273 if( bufpt[-1]=='.' ){
30274 if( flag_altform2 ){
30275 *(bufpt++) = '0';
30276 }else{
30277 *(--bufpt) = 0;
30278 }
30279 }
30280 }
30281 /* Add the "eNNN" suffix */
30282 if( xtype==etEXP2 ){
30283 *(bufpt++) = aDigits[infop->charset];
30284 if( exp<0 ){
30285 *(bufpt++) = '-'; exp = -exp;
30286 }else{
30287 *(bufpt++) = '+';
30288 }
30289 if( exp>=100 ){
30290 *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */
30291 exp %= 100;
30292 }
30293 *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */
30294 *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */
30295 }
30296 *bufpt = 0;
30297
30298 /* The converted number is in buf[] and zero terminated. Output it.
30299 ** Note that the number is in the usual order, not reversed as with
30300 ** integer conversions. */
30301 length = (int)(bufpt-zOut);
30302 bufpt = zOut;
30303
30304 /* Special case: Add leading zeros if the flag_zeropad flag is
30305 ** set and we are not left justified */
30306 if( flag_zeropad && !flag_leftjustify && length < width){
30307 int i;
30308 int nPad = width - length;
30309 for(i=width; i>=nPad; i--){
30310 bufpt[i] = bufpt[i-nPad];
30311 }
30312 i = prefix!=0;
30313 while( nPad-- ) bufpt[i++] = '0';
30314 length = width;
30315 }
30316#endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
30317 break;
30318 case etSIZE4:
30319 if( !bArgList ){
30320 *(va_arg(ap,int*)__builtin_va_arg(ap, int*)) = pAccum->nChar;
30321 }
30322 length = width = 0;
30323 break;
30324 case etPERCENT7:
30325 buf[0] = '%';
30326 bufpt = buf;
30327 length = 1;
30328 break;
30329 case etCHARX8:
30330 if( bArgList ){
30331 bufpt = getTextArg(pArgList);
30332 length = 1;
30333 if( bufpt ){
30334 buf[0] = c = *(bufpt++);
30335 if( (c&0xc0)==0xc0 ){
30336 while( length<4 && (bufpt[0]&0xc0)==0x80 ){
30337 buf[length++] = *(bufpt++);
30338 }
30339 }
30340 }else{
30341 buf[0] = 0;
30342 }
30343 }else{
30344 unsigned int ch = va_arg(ap,unsigned int)__builtin_va_arg(ap, unsigned int);
30345 if( ch<0x00080 ){
30346 buf[0] = ch & 0xff;
30347 length = 1;
30348 }else if( ch<0x00800 ){
30349 buf[0] = 0xc0 + (u8)((ch>>6)&0x1f);
30350 buf[1] = 0x80 + (u8)(ch & 0x3f);
30351 length = 2;
30352 }else if( ch<0x10000 ){
30353 buf[0] = 0xe0 + (u8)((ch>>12)&0x0f);
30354 buf[1] = 0x80 + (u8)((ch>>6) & 0x3f);
30355 buf[2] = 0x80 + (u8)(ch & 0x3f);
30356 length = 3;
30357 }else{
30358 buf[0] = 0xf0 + (u8)((ch>>18) & 0x07);
30359 buf[1] = 0x80 + (u8)((ch>>12) & 0x3f);
30360 buf[2] = 0x80 + (u8)((ch>>6) & 0x3f);
30361 buf[3] = 0x80 + (u8)(ch & 0x3f);
30362 length = 4;
30363 }
30364 }
30365 if( precision>1 ){
30366 width -= precision-1;
30367 if( width>1 && !flag_leftjustify ){
30368 sqlite3_str_appendchar(pAccum, width-1, ' ');
30369 width = 0;
30370 }
30371 while( precision-- > 1 ){
30372 sqlite3_str_append(pAccum, buf, length);
30373 }
30374 }
30375 bufpt = buf;
30376 flag_altform2 = 1;
30377 goto adjust_width_for_utf8;
30378 case etSTRING5:
30379 case etDYNSTRING6:
30380 if( bArgList ){
30381 bufpt = getTextArg(pArgList);
30382 xtype = etSTRING5;
30383 }else{
30384 bufpt = va_arg(ap,char*)__builtin_va_arg(ap, char*);
30385 }
30386 if( bufpt==0 ){
30387 bufpt = "";
30388 }else if( xtype==etDYNSTRING6 ){
30389 if( pAccum->nChar==0
30390 && pAccum->mxAlloc
30391 && width==0
30392 && precision<0
30393 && pAccum->accError==0
30394 ){
30395 /* Special optimization for sqlite3_mprintf("%z..."):
30396 ** Extend an existing memory allocation rather than creating
30397 ** a new one. */
30398 assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 )((void) (0));
30399 pAccum->zText = bufpt;
30400 pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt);
30401 pAccum->nChar = 0x7fffffff & (int)strlen(bufpt);
30402 pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED0x04;
30403 length = 0;
30404 break;
30405 }
30406 zExtra = bufpt;
30407 }
30408 if( precision>=0 ){
30409 if( flag_altform2 ){
30410 /* Set length to the number of bytes needed in order to display
30411 ** precision characters */
30412 unsigned char *z = (unsigned char*)bufpt;
30413 while( precision-- > 0 && z[0] ){
30414 SQLITE_SKIP_UTF8(z){ if( (*(z++))>=0xc0 ){ while( (*z & 0xc0)==0x80 ){ z++
; } } }
;
30415 }
30416 length = (int)(z - (unsigned char*)bufpt);
30417 }else{
30418 for(length=0; length<precision && bufpt[length]; length++){}
30419 }
30420 }else{
30421 length = 0x7fffffff & (int)strlen(bufpt);
30422 }
30423 adjust_width_for_utf8:
30424 if( flag_altform2 && width>0 ){
30425 /* Adjust width to account for extra bytes in UTF-8 characters */
30426 int ii = length - 1;
30427 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
30428 }
30429 break;
30430 case etSQLESCAPE9: /* %q: Escape ' characters */
30431 case etSQLESCAPE210: /* %Q: Escape ' and enclose in '...' */
30432 case etSQLESCAPE314: { /* %w: Escape " characters */
30433 i64 i, j, k, n;
30434 int needQuote, isnull;
30435 char ch;
30436 char q = ((xtype==etSQLESCAPE314)?'"':'\''); /* Quote character */
30437 char *escarg;
30438
30439 if( bArgList ){
30440 escarg = getTextArg(pArgList);
30441 }else{
30442 escarg = va_arg(ap,char*)__builtin_va_arg(ap, char*);
30443 }
30444 isnull = escarg==0;
30445 if( isnull ) escarg = (xtype==etSQLESCAPE210 ? "NULL" : "(NULL)");
30446 /* For %q, %Q, and %w, the precision is the number of bytes (or
30447 ** characters if the ! flags is present) to use from the input.
30448 ** Because of the extra quoting characters inserted, the number
30449 ** of output characters may be larger than the precision.
30450 */
30451 k = precision;
30452 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
30453 if( ch==q ) n++;
30454 if( flag_altform2 && (ch&0xc0)==0xc0 ){
30455 while( (escarg[i+1]&0xc0)==0x80 ){ i++; }
30456 }
30457 }
30458 needQuote = !isnull && xtype==etSQLESCAPE210;
30459 n += i + 3;
30460 if( n>etBUFSIZE70 ){
30461 bufpt = zExtra = printfTempBuf(pAccum, n);
30462 if( bufpt==0 ) return;
30463 }else{
30464 bufpt = buf;
30465 }
30466 j = 0;
30467 if( needQuote ) bufpt[j++] = q;
30468 k = i;
30469 for(i=0; i<k; i++){
30470 bufpt[j++] = ch = escarg[i];
30471 if( ch==q ) bufpt[j++] = ch;
30472 }
30473 if( needQuote ) bufpt[j++] = q;
30474 bufpt[j] = 0;
30475 length = j;
30476 goto adjust_width_for_utf8;
30477 }
30478 case etTOKEN11: {
30479 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL0x01)==0 ) return;
30480 if( flag_alternateform ){
30481 /* %#T means an Expr pointer that uses Expr.u.zToken */
30482 Expr *pExpr = va_arg(ap,Expr*)__builtin_va_arg(ap, Expr*);
30483 if( ALWAYS(pExpr)(pExpr) && ALWAYS(!ExprHasProperty(pExpr,EP_IntValue))(!(((pExpr)->flags&(0x000800))!=0)) ){
30484 sqlite3_str_appendall(pAccum, (const char*)pExpr->u.zToken);
30485 sqlite3RecordErrorOffsetOfExpr(pAccum->db, pExpr);
30486 }
30487 }else{
30488 /* %T means a Token pointer */
30489 Token *pToken = va_arg(ap, Token*)__builtin_va_arg(ap, Token*);
30490 assert( bArgList==0 )((void) (0));
30491 if( pToken && pToken->n ){
30492 sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n);
30493 sqlite3RecordErrorByteOffset(pAccum->db, pToken->z);
30494 }
30495 }
30496 length = width = 0;
30497 break;
30498 }
30499 case etSRCITEM12: {
30500 SrcItem *pItem;
30501 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL0x01)==0 ) return;
30502 pItem = va_arg(ap, SrcItem*)__builtin_va_arg(ap, SrcItem*);
30503 assert( bArgList==0 )((void) (0));
30504 if( pItem->zAlias && !flag_altform2 ){
30505 sqlite3_str_appendall(pAccum, pItem->zAlias);
30506 }else if( pItem->zName ){
30507 if( pItem->zDatabase ){
30508 sqlite3_str_appendall(pAccum, pItem->zDatabase);
30509 sqlite3_str_append(pAccum, ".", 1);
30510 }
30511 sqlite3_str_appendall(pAccum, pItem->zName);
30512 }else if( pItem->zAlias ){
30513 sqlite3_str_appendall(pAccum, pItem->zAlias);
30514 }else{
30515 Select *pSel = pItem->pSelect;
30516 assert( pSel!=0 )((void) (0));
30517 if( pSel->selFlags & SF_NestedFrom0x0000800 ){
30518 sqlite3_str_appendf(pAccum, "(join-%u)", pSel->selId);
30519 }else{
30520 sqlite3_str_appendf(pAccum, "(subquery-%u)", pSel->selId);
30521 }
30522 }
30523 length = width = 0;
30524 break;
30525 }
30526 default: {
30527 assert( xtype==etINVALID )((void) (0));
30528 return;
30529 }
30530 }/* End switch over the format type */
30531 /*
30532 ** The text of the conversion is pointed to by "bufpt" and is
30533 ** "length" characters long. The field width is "width". Do
30534 ** the output. Both length and width are in bytes, not characters,
30535 ** at this point. If the "!" flag was present on string conversions
30536 ** indicating that width and precision should be expressed in characters,
30537 ** then the values have been translated prior to reaching this point.
30538 */
30539 width -= length;
30540 if( width>0 ){
30541 if( !flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
30542 sqlite3_str_append(pAccum, bufpt, length);
30543 if( flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
30544 }else{
30545 sqlite3_str_append(pAccum, bufpt, length);
30546 }
30547
30548 if( zExtra ){
30549 sqlite3DbFree(pAccum->db, zExtra);
30550 zExtra = 0;
30551 }
30552 }/* End for loop over the format string */
30553} /* End of function */
30554
30555
30556/*
30557** The z string points to the first character of a token that is
30558** associated with an error. If db does not already have an error
30559** byte offset recorded, try to compute the error byte offset for
30560** z and set the error byte offset in db.
30561*/
30562SQLITE_PRIVATEstatic void sqlite3RecordErrorByteOffset(sqlite3 *db, const char *z){
30563 const Parse *pParse;
30564 const char *zText;
30565 const char *zEnd;
30566 assert( z!=0 )((void) (0));
30567 if( NEVER(db==0)(db==0) ) return;
30568 if( db->errByteOffset!=(-2) ) return;
30569 pParse = db->pParse;
30570 if( NEVER(pParse==0)(pParse==0) ) return;
30571 zText =pParse->zTail;
30572 if( NEVER(zText==0)(zText==0) ) return;
30573 zEnd = &zText[strlen(zText)];
30574 if( SQLITE_WITHIN(z,zText,zEnd)(((uptr)(z)>=(uptr)(zText))&&((uptr)(z)<(uptr)(
zEnd)))
){
30575 db->errByteOffset = (int)(z-zText);
30576 }
30577}
30578
30579/*
30580** If pExpr has a byte offset for the start of a token, record that as
30581** as the error offset.
30582*/
30583SQLITE_PRIVATEstatic void sqlite3RecordErrorOffsetOfExpr(sqlite3 *db, const Expr *pExpr){
30584 while( pExpr
30585 && (ExprHasProperty(pExpr,EP_OuterON|EP_InnerON)(((pExpr)->flags&(0x000001|0x000002))!=0) || pExpr->w.iOfst<=0)
30586 ){
30587 pExpr = pExpr->pLeft;
30588 }
30589 if( pExpr==0 ) return;
30590 db->errByteOffset = pExpr->w.iOfst;
30591}
30592
30593/*
30594** Enlarge the memory allocation on a StrAccum object so that it is
30595** able to accept at least N more bytes of text.
30596**
30597** Return the number of bytes of text that StrAccum is able to accept
30598** after the attempted enlargement. The value returned might be zero.
30599*/
30600SQLITE_PRIVATEstatic int sqlite3StrAccumEnlarge(StrAccum *p, int N){
30601 char *zNew;
30602 assert( p->nChar+(i64)N >= p->nAlloc )((void) (0)); /* Only called if really needed */
30603 if( p->accError ){
30604 testcase(p->accError==SQLITE_TOOBIG);
30605 testcase(p->accError==SQLITE_NOMEM);
30606 return 0;
30607 }
30608 if( p->mxAlloc==0 ){
30609 sqlite3StrAccumSetError(p, SQLITE_TOOBIG18);
30610 return p->nAlloc - p->nChar - 1;
30611 }else{
30612 char *zOld = isMalloced(p)(((p)->printfFlags & 0x04)!=0) ? p->zText : 0;
30613 i64 szNew = p->nChar;
30614 szNew += (sqlite3_int64)N + 1;
30615 if( szNew+p->nChar<=p->mxAlloc ){
30616 /* Force exponential buffer size growth as long as it does not overflow,
30617 ** to avoid having to call this routine too often */
30618 szNew += p->nChar;
30619 }
30620 if( szNew > p->mxAlloc ){
30621 sqlite3_str_reset(p);
30622 sqlite3StrAccumSetError(p, SQLITE_TOOBIG18);
30623 return 0;
30624 }else{
30625 p->nAlloc = (int)szNew;
30626 }
30627 if( p->db ){
30628 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
30629 }else{
30630 zNew = sqlite3Realloc(zOld, p->nAlloc);
30631 }
30632 if( zNew ){
30633 assert( p->zText!=0 || p->nChar==0 )((void) (0));
30634 if( !isMalloced(p)(((p)->printfFlags & 0x04)!=0) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
30635 p->zText = zNew;
30636 p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
30637 p->printfFlags |= SQLITE_PRINTF_MALLOCED0x04;
30638 }else{
30639 sqlite3_str_reset(p);
30640 sqlite3StrAccumSetError(p, SQLITE_NOMEM7);
30641 return 0;
30642 }
30643 }
30644 return N;
30645}
30646
30647/*
30648** Append N copies of character c to the given string buffer.
30649*/
30650SQLITE_API void sqlite3_str_appendchar(sqlite3_str *p, int N, char c){
30651 testcase( p->nChar + (i64)N > 0x7fffffff );
30652 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
30653 return;
30654 }
30655 while( (N--)>0 ) p->zText[p->nChar++] = c;
30656}
30657
30658/*
30659** The StrAccum "p" is not large enough to accept N new bytes of z[].
30660** So enlarge if first, then do the append.
30661**
30662** This is a helper routine to sqlite3_str_append() that does special-case
30663** work (enlarging the buffer) using tail recursion, so that the
30664** sqlite3_str_append() routine can use fast calling semantics.
30665*/
30666static void SQLITE_NOINLINE__attribute__((noinline)) enlargeAndAppend(StrAccum *p, const char *z, int N){
30667 N = sqlite3StrAccumEnlarge(p, N);
30668 if( N>0 ){
30669 memcpy(&p->zText[p->nChar], z, N);
30670 p->nChar += N;
30671 }
30672}
30673
30674/*
30675** Append N bytes of text from z to the StrAccum object. Increase the
30676** size of the memory allocation for StrAccum if necessary.
30677*/
30678SQLITE_API void sqlite3_str_append(sqlite3_str *p, const char *z, int N){
30679 assert( z!=0 || N==0 )((void) (0));
30680 assert( p->zText!=0 || p->nChar==0 || p->accError )((void) (0));
30681 assert( N>=0 )((void) (0));
30682 assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 )((void) (0));
30683 if( p->nChar+N >= p->nAlloc ){
30684 enlargeAndAppend(p,z,N);
30685 }else if( N ){
30686 assert( p->zText )((void) (0));
30687 p->nChar += N;
30688 memcpy(&p->zText[p->nChar-N], z, N);
30689 }
30690}
30691
30692/*
30693** Append the complete text of zero-terminated string z[] to the p string.
30694*/
30695SQLITE_API void sqlite3_str_appendall(sqlite3_str *p, const char *z){
30696 sqlite3_str_append(p, z, sqlite3Strlen30(z));
30697}
30698
30699
30700/*
30701** Finish off a string by making sure it is zero-terminated.
30702** Return a pointer to the resulting string. Return a NULL
30703** pointer if any kind of error was encountered.
30704*/
30705static SQLITE_NOINLINE__attribute__((noinline)) char *strAccumFinishRealloc(StrAccum *p){
30706 char *zText;
30707 assert( p->mxAlloc>0 && !isMalloced(p) )((void) (0));
30708 zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
30709 if( zText ){
30710 memcpy(zText, p->zText, p->nChar+1);
30711 p->printfFlags |= SQLITE_PRINTF_MALLOCED0x04;
30712 }else{
30713 sqlite3StrAccumSetError(p, SQLITE_NOMEM7);
30714 }
30715 p->zText = zText;
30716 return zText;
30717}
30718SQLITE_PRIVATEstatic char *sqlite3StrAccumFinish(StrAccum *p){
30719 if( p->zText ){
30720 p->zText[p->nChar] = 0;
30721 if( p->mxAlloc>0 && !isMalloced(p)(((p)->printfFlags & 0x04)!=0) ){
30722 return strAccumFinishRealloc(p);
30723 }
30724 }
30725 return p->zText;
30726}
30727
30728/*
30729** Use the content of the StrAccum passed as the second argument
30730** as the result of an SQL function.
30731*/
30732SQLITE_PRIVATEstatic void sqlite3ResultStrAccum(sqlite3_context *pCtx, StrAccum *p){
30733 if( p->accError ){
30734 sqlite3_result_error_code(pCtx, p->accError);
30735 sqlite3_str_reset(p);
30736 }else if( isMalloced(p)(((p)->printfFlags & 0x04)!=0) ){
30737 sqlite3_result_text(pCtx, p->zText, p->nChar, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
30738 }else{
30739 sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC((sqlite3_destructor_type)0));
30740 sqlite3_str_reset(p);
30741 }
30742}
30743
30744/*
30745** This singleton is an sqlite3_str object that is returned if
30746** sqlite3_malloc() fails to provide space for a real one. This
30747** sqlite3_str object accepts no new text and always returns
30748** an SQLITE_NOMEM error.
30749*/
30750static sqlite3_str sqlite3OomStr = {
30751 0, 0, 0, 0, 0, SQLITE_NOMEM7, 0
30752};
30753
30754/* Finalize a string created using sqlite3_str_new().
30755*/
30756SQLITE_API char *sqlite3_str_finish(sqlite3_str *p){
30757 char *z;
30758 if( p!=0 && p!=&sqlite3OomStr ){
30759 z = sqlite3StrAccumFinish(p);
30760 sqlite3_free(p);
30761 }else{
30762 z = 0;
30763 }
30764 return z;
30765}
30766
30767/* Return any error code associated with p */
30768SQLITE_API int sqlite3_str_errcode(sqlite3_str *p){
30769 return p ? p->accError : SQLITE_NOMEM7;
30770}
30771
30772/* Return the current length of p in bytes */
30773SQLITE_API int sqlite3_str_length(sqlite3_str *p){
30774 return p ? p->nChar : 0;
30775}
30776
30777/* Return the current value for p */
30778SQLITE_API char *sqlite3_str_value(sqlite3_str *p){
30779 if( p==0 || p->nChar==0 ) return 0;
30780 p->zText[p->nChar] = 0;
30781 return p->zText;
30782}
30783
30784/*
30785** Reset an StrAccum string. Reclaim all malloced memory.
30786*/
30787SQLITE_API void sqlite3_str_reset(StrAccum *p){
30788 if( isMalloced(p)(((p)->printfFlags & 0x04)!=0) ){
30789 sqlite3DbFree(p->db, p->zText);
30790 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED0x04;
30791 }
30792 p->nAlloc = 0;
30793 p->nChar = 0;
30794 p->zText = 0;
30795}
30796
30797/*
30798** Initialize a string accumulator.
30799**
30800** p: The accumulator to be initialized.
30801** db: Pointer to a database connection. May be NULL. Lookaside
30802** memory is used if not NULL. db->mallocFailed is set appropriately
30803** when not NULL.
30804** zBase: An initial buffer. May be NULL in which case the initial buffer
30805** is malloced.
30806** n: Size of zBase in bytes. If total space requirements never exceed
30807** n then no memory allocations ever occur.
30808** mx: Maximum number of bytes to accumulate. If mx==0 then no memory
30809** allocations will ever occur.
30810*/
30811SQLITE_PRIVATEstatic void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){
30812 p->zText = zBase;
30813 p->db = db;
30814 p->nAlloc = n;
30815 p->mxAlloc = mx;
30816 p->nChar = 0;
30817 p->accError = 0;
30818 p->printfFlags = 0;
30819}
30820
30821/* Allocate and initialize a new dynamic string object */
30822SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3 *db){
30823 sqlite3_str *p = sqlite3_malloc64(sizeof(*p));
30824 if( p ){
30825 sqlite3StrAccumInit(p, 0, 0, 0,
30826 db ? db->aLimit[SQLITE_LIMIT_LENGTH0] : SQLITE_MAX_LENGTH2147483645);
30827 }else{
30828 p = &sqlite3OomStr;
30829 }
30830 return p;
30831}
30832
30833/*
30834** Print into memory obtained from sqliteMalloc(). Use the internal
30835** %-conversion extensions.
30836*/
30837SQLITE_PRIVATEstatic char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
30838 char *z;
30839 char zBase[SQLITE_PRINT_BUF_SIZE70];
30840 StrAccum acc;
30841 assert( db!=0 )((void) (0));
30842 sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase),
30843 db->aLimit[SQLITE_LIMIT_LENGTH0]);
30844 acc.printfFlags = SQLITE_PRINTF_INTERNAL0x01;
30845 sqlite3_str_vappendf(&acc, zFormat, ap);
30846 z = sqlite3StrAccumFinish(&acc);
30847 if( acc.accError==SQLITE_NOMEM7 ){
30848 sqlite3OomFault(db);
30849 }
30850 return z;
30851}
30852
30853/*
30854** Print into memory obtained from sqliteMalloc(). Use the internal
30855** %-conversion extensions.
30856*/
30857SQLITE_PRIVATEstatic char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
30858 va_list ap;
30859 char *z;
30860 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
30861 z = sqlite3VMPrintf(db, zFormat, ap);
30862 va_end(ap)__builtin_va_end(ap);
30863 return z;
30864}
30865
30866/*
30867** Print into memory obtained from sqlite3_malloc(). Omit the internal
30868** %-conversion extensions.
30869*/
30870SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
30871 char *z;
30872 char zBase[SQLITE_PRINT_BUF_SIZE70];
30873 StrAccum acc;
30874
30875#ifdef SQLITE_ENABLE_API_ARMOR1
30876 if( zFormat==0 ){
30877 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(30877);
30878 return 0;
30879 }
30880#endif
30881#ifndef SQLITE_OMIT_AUTOINIT
30882 if( sqlite3_initialize() ) return 0;
30883#endif
30884 sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH2147483645);
30885 sqlite3_str_vappendf(&acc, zFormat, ap);
30886 z = sqlite3StrAccumFinish(&acc);
30887 return z;
30888}
30889
30890/*
30891** Print into memory obtained from sqlite3_malloc()(). Omit the internal
30892** %-conversion extensions.
30893*/
30894SQLITE_API char *sqlite3_mprintf(const char *zFormat, ...){
30895 va_list ap;
30896 char *z;
30897#ifndef SQLITE_OMIT_AUTOINIT
30898 if( sqlite3_initialize() ) return 0;
30899#endif
30900 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
30901 z = sqlite3_vmprintf(zFormat, ap);
30902 va_end(ap)__builtin_va_end(ap);
30903 return z;
30904}
30905
30906/*
30907** sqlite3_snprintf() works like snprintf() except that it ignores the
30908** current locale settings. This is important for SQLite because we
30909** are not able to use a "," as the decimal point in place of "." as
30910** specified by some locales.
30911**
30912** Oops: The first two arguments of sqlite3_snprintf() are backwards
30913** from the snprintf() standard. Unfortunately, it is too late to change
30914** this without breaking compatibility, so we just have to live with the
30915** mistake.
30916**
30917** sqlite3_vsnprintf() is the varargs version.
30918*/
30919SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
30920 StrAccum acc;
30921 if( n<=0 ) return zBuf;
30922#ifdef SQLITE_ENABLE_API_ARMOR1
30923 if( zBuf==0 || zFormat==0 ) {
30924 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(30924);
30925 if( zBuf ) zBuf[0] = 0;
30926 return zBuf;
30927 }
30928#endif
30929 sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
30930 sqlite3_str_vappendf(&acc, zFormat, ap);
30931 zBuf[acc.nChar] = 0;
30932 return zBuf;
30933}
30934SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
30935 char *z;
30936 va_list ap;
30937 va_start(ap,zFormat)__builtin_va_start(ap, zFormat);
30938 z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
30939 va_end(ap)__builtin_va_end(ap);
30940 return z;
30941}
30942
30943/*
30944** This is the routine that actually formats the sqlite3_log() message.
30945** We house it in a separate routine from sqlite3_log() to avoid using
30946** stack space on small-stack systems when logging is disabled.
30947**
30948** sqlite3_log() must render into a static buffer. It cannot dynamically
30949** allocate memory because it might be called while the memory allocator
30950** mutex is held.
30951**
30952** sqlite3_str_vappendf() might ask for *temporary* memory allocations for
30953** certain format characters (%q) or for very large precisions or widths.
30954** Care must be taken that any sqlite3_log() calls that occur while the
30955** memory mutex is held do not use these mechanisms.
30956*/
30957static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
30958 StrAccum acc; /* String accumulator */
30959 char zMsg[SQLITE_PRINT_BUF_SIZE70*3]; /* Complete log message */
30960
30961 sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0);
30962 sqlite3_str_vappendf(&acc, zFormat, ap);
30963 sqlite3GlobalConfigsqlite3Config.xLog(sqlite3GlobalConfigsqlite3Config.pLogArg, iErrCode,
30964 sqlite3StrAccumFinish(&acc));
30965}
30966
30967/*
30968** Format and write a message to the log if logging is enabled.
30969*/
30970SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...){
30971 va_list ap; /* Vararg list */
30972 if( sqlite3GlobalConfigsqlite3Config.xLog ){
30973 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
30974 renderLogMsg(iErrCode, zFormat, ap);
30975 va_end(ap)__builtin_va_end(ap);
30976 }
30977}
30978
30979#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
30980/*
30981** A version of printf() that understands %lld. Used for debugging.
30982** The printf() built into some versions of windows does not understand %lld
30983** and segfaults if you give it a long long int.
30984*/
30985SQLITE_PRIVATEstatic void sqlite3DebugPrintf(const char *zFormat, ...){
30986 va_list ap;
30987 StrAccum acc;
30988 char zBuf[SQLITE_PRINT_BUF_SIZE70*10];
30989 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
30990 va_start(ap,zFormat)__builtin_va_start(ap, zFormat);
30991 sqlite3_str_vappendf(&acc, zFormat, ap);
30992 va_end(ap)__builtin_va_end(ap);
30993 sqlite3StrAccumFinish(&acc);
30994#ifdef SQLITE_OS_TRACE_PROC
30995 {
30996 extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf);
30997 SQLITE_OS_TRACE_PROC(zBuf, sizeof(zBuf));
30998 }
30999#else
31000 fprintf(stdoutstdout,"%s", zBuf);
31001 fflush(stdoutstdout);
31002#endif
31003}
31004#endif
31005
31006
31007/*
31008** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument
31009** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats.
31010*/
31011SQLITE_API void sqlite3_str_appendf(StrAccum *p, const char *zFormat, ...){
31012 va_list ap;
31013 va_start(ap,zFormat)__builtin_va_start(ap, zFormat);
31014 sqlite3_str_vappendf(p, zFormat, ap);
31015 va_end(ap)__builtin_va_end(ap);
31016}
31017
31018/************** End of printf.c **********************************************/
31019/************** Begin file treeview.c ****************************************/
31020/*
31021** 2015-06-08
31022**
31023** The author disclaims copyright to this source code. In place of
31024** a legal notice, here is a blessing:
31025**
31026** May you do good and not evil.
31027** May you find forgiveness for yourself and forgive others.
31028** May you share freely, never taking more than you give.
31029**
31030*************************************************************************
31031**
31032** This file contains C code to implement the TreeView debugging routines.
31033** These routines print a parse tree to standard output for debugging and
31034** analysis.
31035**
31036** The interfaces in this file is only available when compiling
31037** with SQLITE_DEBUG.
31038*/
31039/* #include "sqliteInt.h" */
31040#ifdef SQLITE_DEBUG
31041
31042/*
31043** Add a new subitem to the tree. The moreToFollow flag indicates that this
31044** is not the last item in the tree.
31045*/
31046static void sqlite3TreeViewPush(TreeView **pp, u8 moreToFollow){
31047 TreeView *p = *pp;
31048 if( p==0 ){
31049 *pp = p = sqlite3_malloc64( sizeof(*p) );
31050 if( p==0 ) return;
31051 memset(p, 0, sizeof(*p));
31052 }else{
31053 p->iLevel++;
31054 }
31055 assert( moreToFollow==0 || moreToFollow==1 )((void) (0));
31056 if( p->iLevel<(int)sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
31057}
31058
31059/*
31060** Finished with one layer of the tree
31061*/
31062static void sqlite3TreeViewPop(TreeView **pp){
31063 TreeView *p = *pp;
31064 if( p==0 ) return;
31065 p->iLevel--;
31066 if( p->iLevel<0 ){
31067 sqlite3_free(p);
31068 *pp = 0;
31069 }
31070}
31071
31072/*
31073** Generate a single line of output for the tree, with a prefix that contains
31074** all the appropriate tree lines
31075*/
31076SQLITE_PRIVATEstatic void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
31077 va_list ap;
31078 int i;
31079 StrAccum acc;
31080 char zBuf[1000];
31081 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
31082 if( p ){
31083 for(i=0; i<p->iLevel && i<(int)sizeof(p->bLine)-1; i++){
31084 sqlite3_str_append(&acc, p->bLine[i] ? "| " : " ", 4);
31085 }
31086 sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
31087 }
31088 if( zFormat!=0 ){
31089 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
31090 sqlite3_str_vappendf(&acc, zFormat, ap);
31091 va_end(ap)__builtin_va_end(ap);
31092 assert( acc.nChar>0 || acc.accError )((void) (0));
31093 sqlite3_str_append(&acc, "\n", 1);
31094 }
31095 sqlite3StrAccumFinish(&acc);
31096 fprintf(stdoutstdout,"%s", zBuf);
31097 fflush(stdoutstdout);
31098}
31099
31100/*
31101** Shorthand for starting a new tree item that consists of a single label
31102*/
31103static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){
31104 sqlite3TreeViewPush(&p, moreFollows);
31105 sqlite3TreeViewLine(p, "%s", zLabel);
31106}
31107
31108/*
31109** Show a list of Column objects in tree format.
31110*/
31111SQLITE_PRIVATEstatic void sqlite3TreeViewColumnList(
31112 TreeView *pView,
31113 const Column *aCol,
31114 int nCol,
31115 u8 moreToFollow
31116){
31117 int i;
31118 sqlite3TreeViewPush(&pView, moreToFollow);
31119 sqlite3TreeViewLine(pView, "COLUMNS");
31120 for(i=0; i<nCol; i++){
31121 u16 flg = aCol[i].colFlags;
31122 int colMoreToFollow = i<(nCol - 1);
31123 sqlite3TreeViewPush(&pView, colMoreToFollow);
31124 sqlite3TreeViewLine(pView, 0);
31125 printf(" %s", aCol[i].zCnName);
31126 switch( aCol[i].eCType ){
31127 case COLTYPE_ANY1: printf(" ANY"); break;
31128 case COLTYPE_BLOB2: printf(" BLOB"); break;
31129 case COLTYPE_INT3: printf(" INT"); break;
31130 case COLTYPE_INTEGER4: printf(" INTEGER"); break;
31131 case COLTYPE_REAL5: printf(" REAL"); break;
31132 case COLTYPE_TEXT6: printf(" TEXT"); break;
31133 case COLTYPE_CUSTOM0: {
31134 if( flg & COLFLAG_HASTYPE0x0004 ){
31135 const char *z = aCol[i].zCnName;
31136 z += strlen(z)+1;
31137 printf(" X-%s", z);
31138 break;
31139 }
31140 }
31141 }
31142 if( flg & COLFLAG_PRIMKEY0x0001 ) printf(" PRIMARY KEY");
31143 if( flg & COLFLAG_HIDDEN0x0002 ) printf(" HIDDEN");
31144#ifdef COLFLAG_NOEXPAND0x0400
31145 if( flg & COLFLAG_NOEXPAND0x0400 ) printf(" NO-EXPAND");
31146#endif
31147 if( flg ) printf(" flags=%04x", flg);
31148 printf("\n");
31149 fflush(stdoutstdout);
31150 sqlite3TreeViewPop(&pView);
31151 }
31152 sqlite3TreeViewPop(&pView);
31153}
31154
31155/*
31156** Generate a human-readable description of a WITH clause.
31157*/
31158SQLITE_PRIVATEstatic void sqlite3TreeViewWith(TreeView *pView, const With *pWith, u8 moreToFollow){
31159 int i;
31160 if( pWith==0 ) return;
31161 if( pWith->nCte==0 ) return;
31162 if( pWith->pOuter ){
31163 sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter);
31164 }else{
31165 sqlite3TreeViewLine(pView, "WITH (0x%p)", pWith);
31166 }
31167 if( pWith->nCte>0 ){
31168 sqlite3TreeViewPush(&pView, moreToFollow);
31169 for(i=0; i<pWith->nCte; i++){
31170 StrAccum x;
31171 char zLine[1000];
31172 const struct Cte *pCte = &pWith->a[i];
31173 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
31174 sqlite3_str_appendf(&x, "%s", pCte->zName);
31175 if( pCte->pCols && pCte->pCols->nExpr>0 ){
31176 char cSep = '(';
31177 int j;
31178 for(j=0; j<pCte->pCols->nExpr; j++){
31179 sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zEName);
31180 cSep = ',';
31181 }
31182 sqlite3_str_appendf(&x, ")");
31183 }
31184 if( pCte->eM10d!=M10d_Any1 ){
31185 sqlite3_str_appendf(&x, " %sMATERIALIZED",
31186 pCte->eM10d==M10d_No2 ? "NOT " : "");
31187 }
31188 if( pCte->pUse ){
31189 sqlite3_str_appendf(&x, " (pUse=0x%p, nUse=%d)", pCte->pUse,
31190 pCte->pUse->nUse);
31191 }
31192 sqlite3StrAccumFinish(&x);
31193 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
31194 sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
31195 sqlite3TreeViewPop(&pView);
31196 }
31197 sqlite3TreeViewPop(&pView);
31198 }
31199}
31200
31201/*
31202** Generate a human-readable description of a SrcList object.
31203*/
31204SQLITE_PRIVATEstatic void sqlite3TreeViewSrcList(TreeView *pView, const SrcList *pSrc){
31205 int i;
31206 if( pSrc==0 ) return;
31207 for(i=0; i<pSrc->nSrc; i++){
31208 const SrcItem *pItem = &pSrc->a[i];
31209 StrAccum x;
31210 int n = 0;
31211 char zLine[1000];
31212 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
31213 x.printfFlags |= SQLITE_PRINTF_INTERNAL0x01;
31214 sqlite3_str_appendf(&x, "{%d:*} %!S", pItem->iCursor, pItem);
31215 if( pItem->pTab ){
31216 sqlite3_str_appendf(&x, " tab=%Q nCol=%d ptr=%p used=%llx",
31217 pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab, pItem->colUsed);
31218 }
31219 if( (pItem->fg.jointype & (JT_LEFT0x08|JT_RIGHT0x10))==(JT_LEFT0x08|JT_RIGHT0x10) ){
31220 sqlite3_str_appendf(&x, " FULL-OUTER-JOIN");
31221 }else if( pItem->fg.jointype & JT_LEFT0x08 ){
31222 sqlite3_str_appendf(&x, " LEFT-JOIN");
31223 }else if( pItem->fg.jointype & JT_RIGHT0x10 ){
31224 sqlite3_str_appendf(&x, " RIGHT-JOIN");
31225 }else if( pItem->fg.jointype & JT_CROSS0x02 ){
31226 sqlite3_str_appendf(&x, " CROSS-JOIN");
31227 }
31228 if( pItem->fg.jointype & JT_LTORJ0x40 ){
31229 sqlite3_str_appendf(&x, " LTORJ");
31230 }
31231 if( pItem->fg.fromDDL ){
31232 sqlite3_str_appendf(&x, " DDL");
31233 }
31234 if( pItem->fg.isCte ){
31235 sqlite3_str_appendf(&x, " CteUse=0x%p", pItem->u2.pCteUse);
31236 }
31237 if( pItem->fg.isOn || (pItem->fg.isUsing==0 && pItem->u3.pOn!=0) ){
31238 sqlite3_str_appendf(&x, " ON");
31239 }
31240 sqlite3StrAccumFinish(&x);
31241 sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1);
31242 n = 0;
31243 if( pItem->pSelect ) n++;
31244 if( pItem->fg.isTabFunc ) n++;
31245 if( pItem->fg.isUsing ) n++;
31246 if( pItem->fg.isUsing ){
31247 sqlite3TreeViewIdList(pView, pItem->u3.pUsing, (--n)>0, "USING");
31248 }
31249 if( pItem->pSelect ){
31250 if( pItem->pTab ){
31251 Table *pTab = pItem->pTab;
31252 sqlite3TreeViewColumnList(pView, pTab->aCol, pTab->nCol, 1);
31253 }
31254 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) )((void) (0));
31255 sqlite3TreeViewSelect(pView, pItem->pSelect, (--n)>0);
31256 }
31257 if( pItem->fg.isTabFunc ){
31258 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
31259 }
31260 sqlite3TreeViewPop(&pView);
31261 }
31262}
31263
31264/*
31265** Generate a human-readable description of a Select object.
31266*/
31267SQLITE_PRIVATEstatic void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
31268 int n = 0;
31269 int cnt = 0;
31270 if( p==0 ){
31271 sqlite3TreeViewLine(pView, "nil-SELECT");
31272 return;
31273 }
31274 sqlite3TreeViewPush(&pView, moreToFollow);
31275 if( p->pWith ){
31276 sqlite3TreeViewWith(pView, p->pWith, 1);
31277 cnt = 1;
31278 sqlite3TreeViewPush(&pView, 1);
31279 }
31280 do{
31281 if( p->selFlags & SF_WhereBegin0x0080000 ){
31282 sqlite3TreeViewLine(pView, "sqlite3WhereBegin()");
31283 }else{
31284 sqlite3TreeViewLine(pView,
31285 "SELECT%s%s (%u/%p) selFlags=0x%x nSelectRow=%d",
31286 ((p->selFlags & SF_Distinct0x0000001) ? " DISTINCT" : ""),
31287 ((p->selFlags & SF_Aggregate0x0000008) ? " agg_flag" : ""),
31288 p->selId, p, p->selFlags,
31289 (int)p->nSelectRow
31290 );
31291 }
31292 if( cnt++ ) sqlite3TreeViewPop(&pView);
31293 if( p->pPrior ){
31294 n = 1000;
31295 }else{
31296 n = 0;
31297 if( p->pSrc && p->pSrc->nSrc ) n++;
31298 if( p->pWhere ) n++;
31299 if( p->pGroupBy ) n++;
31300 if( p->pHaving ) n++;
31301 if( p->pOrderBy ) n++;
31302 if( p->pLimit ) n++;
31303#ifndef SQLITE_OMIT_WINDOWFUNC
31304 if( p->pWin ) n++;
31305 if( p->pWinDefn ) n++;
31306#endif
31307 }
31308 if( p->pEList ){
31309 sqlite3TreeViewExprList(pView, p->pEList, n>0, "result-set");
31310 }
31311 n--;
31312#ifndef SQLITE_OMIT_WINDOWFUNC
31313 if( p->pWin ){
31314 Window *pX;
31315 sqlite3TreeViewPush(&pView, (n--)>0);
31316 sqlite3TreeViewLine(pView, "window-functions");
31317 for(pX=p->pWin; pX; pX=pX->pNextWin){
31318 sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0);
31319 }
31320 sqlite3TreeViewPop(&pView);
31321 }
31322#endif
31323 if( p->pSrc && p->pSrc->nSrc ){
31324 sqlite3TreeViewPush(&pView, (n--)>0);
31325 sqlite3TreeViewLine(pView, "FROM");
31326 sqlite3TreeViewSrcList(pView, p->pSrc);
31327 sqlite3TreeViewPop(&pView);
31328 }
31329 if( p->pWhere ){
31330 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
31331 sqlite3TreeViewExpr(pView, p->pWhere, 0);
31332 sqlite3TreeViewPop(&pView);
31333 }
31334 if( p->pGroupBy ){
31335 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
31336 }
31337 if( p->pHaving ){
31338 sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
31339 sqlite3TreeViewExpr(pView, p->pHaving, 0);
31340 sqlite3TreeViewPop(&pView);
31341 }
31342#ifndef SQLITE_OMIT_WINDOWFUNC
31343 if( p->pWinDefn ){
31344 Window *pX;
31345 sqlite3TreeViewItem(pView, "WINDOW", (n--)>0);
31346 for(pX=p->pWinDefn; pX; pX=pX->pNextWin){
31347 sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0);
31348 }
31349 sqlite3TreeViewPop(&pView);
31350 }
31351#endif
31352 if( p->pOrderBy ){
31353 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
31354 }
31355 if( p->pLimit ){
31356 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
31357 sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
31358 if( p->pLimit->pRight ){
31359 sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
31360 sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
31361 sqlite3TreeViewPop(&pView);
31362 }
31363 sqlite3TreeViewPop(&pView);
31364 }
31365 if( p->pPrior ){
31366 const char *zOp = "UNION";
31367 switch( p->op ){
31368 case TK_ALL135: zOp = "UNION ALL"; break;
31369 case TK_INTERSECT137: zOp = "INTERSECT"; break;
31370 case TK_EXCEPT136: zOp = "EXCEPT"; break;
31371 }
31372 sqlite3TreeViewItem(pView, zOp, 1);
31373 }
31374 p = p->pPrior;
31375 }while( p!=0 );
31376 sqlite3TreeViewPop(&pView);
31377}
31378
31379#ifndef SQLITE_OMIT_WINDOWFUNC
31380/*
31381** Generate a description of starting or stopping bounds
31382*/
31383SQLITE_PRIVATEstatic void sqlite3TreeViewBound(
31384 TreeView *pView, /* View context */
31385 u8 eBound, /* UNBOUNDED, CURRENT, PRECEDING, FOLLOWING */
31386 Expr *pExpr, /* Value for PRECEDING or FOLLOWING */
31387 u8 moreToFollow /* True if more to follow */
31388){
31389 switch( eBound ){
31390 case TK_UNBOUNDED90: {
31391 sqlite3TreeViewItem(pView, "UNBOUNDED", moreToFollow);
31392 sqlite3TreeViewPop(&pView);
31393 break;
31394 }
31395 case TK_CURRENT85: {
31396 sqlite3TreeViewItem(pView, "CURRENT", moreToFollow);
31397 sqlite3TreeViewPop(&pView);
31398 break;
31399 }
31400 case TK_PRECEDING88: {
31401 sqlite3TreeViewItem(pView, "PRECEDING", moreToFollow);
31402 sqlite3TreeViewExpr(pView, pExpr, 0);
31403 sqlite3TreeViewPop(&pView);
31404 break;
31405 }
31406 case TK_FOLLOWING86: {
31407 sqlite3TreeViewItem(pView, "FOLLOWING", moreToFollow);
31408 sqlite3TreeViewExpr(pView, pExpr, 0);
31409 sqlite3TreeViewPop(&pView);
31410 break;
31411 }
31412 }
31413}
31414#endif /* SQLITE_OMIT_WINDOWFUNC */
31415
31416#ifndef SQLITE_OMIT_WINDOWFUNC
31417/*
31418** Generate a human-readable explanation for a Window object
31419*/
31420SQLITE_PRIVATEstatic void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u8 more){
31421 int nElement = 0;
31422 if( pWin==0 ) return;
31423 if( pWin->pFilter ){
31424 sqlite3TreeViewItem(pView, "FILTER", 1);
31425 sqlite3TreeViewExpr(pView, pWin->pFilter, 0);
31426 sqlite3TreeViewPop(&pView);
31427 }
31428 sqlite3TreeViewPush(&pView, more);
31429 if( pWin->zName ){
31430 sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin);
31431 }else{
31432 sqlite3TreeViewLine(pView, "OVER (%p)", pWin);
31433 }
31434 if( pWin->zBase ) nElement++;
31435 if( pWin->pOrderBy ) nElement++;
31436 if( pWin->eFrmType ) nElement++;
31437 if( pWin->eExclude ) nElement++;
31438 if( pWin->zBase ){
31439 sqlite3TreeViewPush(&pView, (--nElement)>0);
31440 sqlite3TreeViewLine(pView, "window: %s", pWin->zBase);
31441 sqlite3TreeViewPop(&pView);
31442 }
31443 if( pWin->pPartition ){
31444 sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY");
31445 }
31446 if( pWin->pOrderBy ){
31447 sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY");
31448 }
31449 if( pWin->eFrmType ){
31450 char zBuf[30];
31451 const char *zFrmType = "ROWS";
31452 if( pWin->eFrmType==TK_RANGE89 ) zFrmType = "RANGE";
31453 if( pWin->eFrmType==TK_GROUPS92 ) zFrmType = "GROUPS";
31454 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s%s",zFrmType,
31455 pWin->bImplicitFrame ? " (implied)" : "");
31456 sqlite3TreeViewItem(pView, zBuf, (--nElement)>0);
31457 sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1);
31458 sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0);
31459 sqlite3TreeViewPop(&pView);
31460 }
31461 if( pWin->eExclude ){
31462 char zBuf[30];
31463 const char *zExclude;
31464 switch( pWin->eExclude ){
31465 case TK_NO66: zExclude = "NO OTHERS"; break;
31466 case TK_CURRENT85: zExclude = "CURRENT ROW"; break;
31467 case TK_GROUP146: zExclude = "GROUP"; break;
31468 case TK_TIES94: zExclude = "TIES"; break;
31469 default:
31470 sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude);
31471 zExclude = zBuf;
31472 break;
31473 }
31474 sqlite3TreeViewPush(&pView, 0);
31475 sqlite3TreeViewLine(pView, "EXCLUDE %s", zExclude);
31476 sqlite3TreeViewPop(&pView);
31477 }
31478 sqlite3TreeViewPop(&pView);
31479}
31480#endif /* SQLITE_OMIT_WINDOWFUNC */
31481
31482#ifndef SQLITE_OMIT_WINDOWFUNC
31483/*
31484** Generate a human-readable explanation for a Window Function object
31485*/
31486SQLITE_PRIVATEstatic void sqlite3TreeViewWinFunc(TreeView *pView, const Window *pWin, u8 more){
31487 if( pWin==0 ) return;
31488 sqlite3TreeViewPush(&pView, more);
31489 sqlite3TreeViewLine(pView, "WINFUNC %s(%d)",
31490 pWin->pWFunc->zName, pWin->pWFunc->nArg);
31491 sqlite3TreeViewWindow(pView, pWin, 0);
31492 sqlite3TreeViewPop(&pView);
31493}
31494#endif /* SQLITE_OMIT_WINDOWFUNC */
31495
31496/*
31497** Generate a human-readable explanation of an expression tree.
31498*/
31499SQLITE_PRIVATEstatic void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
31500 const char *zBinOp = 0; /* Binary operator */
31501 const char *zUniOp = 0; /* Unary operator */
31502 char zFlgs[200];
31503 sqlite3TreeViewPush(&pView, moreToFollow);
31504 if( pExpr==0 ){
31505 sqlite3TreeViewLine(pView, "nil");
31506 sqlite3TreeViewPop(&pView);
31507 return;
31508 }
31509 if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags ){
31510 StrAccum x;
31511 sqlite3StrAccumInit(&x, 0, zFlgs, sizeof(zFlgs), 0);
31512 sqlite3_str_appendf(&x, " fg.af=%x.%c",
31513 pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n');
31514 if( ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(0x000001))!=0) ){
31515 sqlite3_str_appendf(&x, " outer.iJoin=%d", pExpr->w.iJoin);
31516 }
31517 if( ExprHasProperty(pExpr, EP_InnerON)(((pExpr)->flags&(0x000002))!=0) ){
31518 sqlite3_str_appendf(&x, " inner.iJoin=%d", pExpr->w.iJoin);
31519 }
31520 if( ExprHasProperty(pExpr, EP_FromDDL)(((pExpr)->flags&(0x40000000))!=0) ){
31521 sqlite3_str_appendf(&x, " DDL");
31522 }
31523 if( ExprHasVVAProperty(pExpr, EP_Immutable)0 ){
31524 sqlite3_str_appendf(&x, " IMMUTABLE");
31525 }
31526 sqlite3StrAccumFinish(&x);
31527 }else{
31528 zFlgs[0] = 0;
31529 }
31530 switch( pExpr->op ){
31531 case TK_AGG_COLUMN169: {
31532 sqlite3TreeViewLine(pView, "AGG{%d:%d}%s",
31533 pExpr->iTable, pExpr->iColumn, zFlgs);
31534 break;
31535 }
31536 case TK_COLUMN167: {
31537 if( pExpr->iTable<0 ){
31538 /* This only happens when coding check constraints */
31539 char zOp2[16];
31540 if( pExpr->op2 ){
31541 sqlite3_snprintf(sizeof(zOp2),zOp2," op2=0x%02x",pExpr->op2);
31542 }else{
31543 zOp2[0] = 0;
31544 }
31545 sqlite3TreeViewLine(pView, "COLUMN(%d)%s%s",
31546 pExpr->iColumn, zFlgs, zOp2);
31547 }else{
31548 assert( ExprUseYTab(pExpr) )((void) (0));
31549 sqlite3TreeViewLine(pView, "{%d:%d} pTab=%p%s",
31550 pExpr->iTable, pExpr->iColumn,
31551 pExpr->y.pTab, zFlgs);
31552 }
31553 if( ExprHasProperty(pExpr, EP_FixedCol)(((pExpr)->flags&(0x000020))!=0) ){
31554 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
31555 }
31556 break;
31557 }
31558 case TK_INTEGER155: {
31559 if( pExpr->flags & EP_IntValue0x000800 ){
31560 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
31561 }else{
31562 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
31563 }
31564 break;
31565 }
31566#ifndef SQLITE_OMIT_FLOATING_POINT
31567 case TK_FLOAT153: {
31568 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
31569 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
31570 break;
31571 }
31572#endif
31573 case TK_STRING117: {
31574 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
31575 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
31576 break;
31577 }
31578 case TK_NULL121: {
31579 sqlite3TreeViewLine(pView,"NULL");
31580 break;
31581 }
31582 case TK_TRUEFALSE170: {
31583 sqlite3TreeViewLine(pView,"%s%s",
31584 sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE", zFlgs);
31585 break;
31586 }
31587#ifndef SQLITE_OMIT_BLOB_LITERAL
31588 case TK_BLOB154: {
31589 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
31590 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
31591 break;
31592 }
31593#endif
31594 case TK_VARIABLE156: {
31595 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
31596 sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
31597 pExpr->u.zToken, pExpr->iColumn);
31598 break;
31599 }
31600 case TK_REGISTER176: {
31601 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
31602 break;
31603 }
31604 case TK_ID59: {
31605 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
31606 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
31607 break;
31608 }
31609#ifndef SQLITE_OMIT_CAST
31610 case TK_CAST36: {
31611 /* Expressions of the form: CAST(pLeft AS token) */
31612 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
31613 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
31614 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
31615 break;
31616 }
31617#endif /* SQLITE_OMIT_CAST */
31618 case TK_LT56: zBinOp = "LT"; break;
31619 case TK_LE55: zBinOp = "LE"; break;
31620 case TK_GT54: zBinOp = "GT"; break;
31621 case TK_GE57: zBinOp = "GE"; break;
31622 case TK_NE52: zBinOp = "NE"; break;
31623 case TK_EQ53: zBinOp = "EQ"; break;
31624 case TK_IS45: zBinOp = "IS"; break;
31625 case TK_ISNOT171: zBinOp = "ISNOT"; break;
31626 case TK_AND44: zBinOp = "AND"; break;
31627 case TK_OR43: zBinOp = "OR"; break;
31628 case TK_PLUS106: zBinOp = "ADD"; break;
31629 case TK_STAR108: zBinOp = "MUL"; break;
31630 case TK_MINUS107: zBinOp = "SUB"; break;
31631 case TK_REM110: zBinOp = "REM"; break;
31632 case TK_BITAND102: zBinOp = "BITAND"; break;
31633 case TK_BITOR103: zBinOp = "BITOR"; break;
31634 case TK_SLASH109: zBinOp = "DIV"; break;
31635 case TK_LSHIFT104: zBinOp = "LSHIFT"; break;
31636 case TK_RSHIFT105: zBinOp = "RSHIFT"; break;
31637 case TK_CONCAT111: zBinOp = "CONCAT"; break;
31638 case TK_DOT141: zBinOp = "DOT"; break;
31639 case TK_LIMIT148: zBinOp = "LIMIT"; break;
31640
31641 case TK_UMINUS173: zUniOp = "UMINUS"; break;
31642 case TK_UPLUS174: zUniOp = "UPLUS"; break;
31643 case TK_BITNOT114: zUniOp = "BITNOT"; break;
31644 case TK_NOT19: zUniOp = "NOT"; break;
31645 case TK_ISNULL50: zUniOp = "ISNULL"; break;
31646 case TK_NOTNULL51: zUniOp = "NOTNULL"; break;
31647
31648 case TK_TRUTH175: {
31649 int x;
31650 const char *azOp[] = {
31651 "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
31652 };
31653 assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT )((void) (0));
31654 assert( pExpr->pRight )((void) (0));
31655 assert( sqlite3ExprSkipCollate(pExpr->pRight)->op==TK_TRUEFALSE )((void) (0));
31656 x = (pExpr->op2==TK_ISNOT171)*2 + sqlite3ExprTruthValue(pExpr->pRight);
31657 zUniOp = azOp[x];
31658 break;
31659 }
31660
31661 case TK_SPAN181: {
31662 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
31663 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
31664 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
31665 break;
31666 }
31667
31668 case TK_COLLATE113: {
31669 /* COLLATE operators without the EP_Collate flag are intended to
31670 ** emulate collation associated with a table column. These show
31671 ** up in the treeview output as "SOFT-COLLATE". Explicit COLLATE
31672 ** operators that appear in the original SQL always have the
31673 ** EP_Collate bit set and appear in treeview output as just "COLLATE" */
31674 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
31675 sqlite3TreeViewLine(pView, "%sCOLLATE %Q%s",
31676 !ExprHasProperty(pExpr, EP_Collate)(((pExpr)->flags&(0x000200))!=0) ? "SOFT-" : "",
31677 pExpr->u.zToken, zFlgs);
31678 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
31679 break;
31680 }
31681
31682 case TK_AGG_FUNCTION168:
31683 case TK_FUNCTION172: {
31684 ExprList *pFarg; /* List of function arguments */
31685 Window *pWin;
31686 if( ExprHasProperty(pExpr, EP_TokenOnly)(((pExpr)->flags&(0x010000))!=0) ){
31687 pFarg = 0;
31688 pWin = 0;
31689 }else{
31690 assert( ExprUseXList(pExpr) )((void) (0));
31691 pFarg = pExpr->x.pList;
31692#ifndef SQLITE_OMIT_WINDOWFUNC
31693 pWin = ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ? pExpr->y.pWin : 0;
31694#else
31695 pWin = 0;
31696#endif
31697 }
31698 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
31699 if( pExpr->op==TK_AGG_FUNCTION168 ){
31700 sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q%s agg=%d[%d]/%p",
31701 pExpr->op2, pExpr->u.zToken, zFlgs,
31702 pExpr->pAggInfo ? pExpr->pAggInfo->selId : 0,
31703 pExpr->iAgg, pExpr->pAggInfo);
31704 }else if( pExpr->op2!=0 ){
31705 const char *zOp2;
31706 char zBuf[8];
31707 sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2);
31708 zOp2 = zBuf;
31709 if( pExpr->op2==NC_IsCheck0x000004 ) zOp2 = "NC_IsCheck";
31710 if( pExpr->op2==NC_IdxExpr0x000020 ) zOp2 = "NC_IdxExpr";
31711 if( pExpr->op2==NC_PartIdx0x000002 ) zOp2 = "NC_PartIdx";
31712 if( pExpr->op2==NC_GenCol0x000008 ) zOp2 = "NC_GenCol";
31713 sqlite3TreeViewLine(pView, "FUNCTION %Q%s op2=%s",
31714 pExpr->u.zToken, zFlgs, zOp2);
31715 }else{
31716 sqlite3TreeViewLine(pView, "FUNCTION %Q%s", pExpr->u.zToken, zFlgs);
31717 }
31718 if( pFarg ){
31719 sqlite3TreeViewExprList(pView, pFarg, pWin!=0, 0);
31720 }
31721#ifndef SQLITE_OMIT_WINDOWFUNC
31722 if( pWin ){
31723 sqlite3TreeViewWindow(pView, pWin, 0);
31724 }
31725#endif
31726 break;
31727 }
31728#ifndef SQLITE_OMIT_SUBQUERY
31729 case TK_EXISTS20: {
31730 assert( ExprUseXSelect(pExpr) )((void) (0));
31731 sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags);
31732 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
31733 break;
31734 }
31735 case TK_SELECT138: {
31736 assert( ExprUseXSelect(pExpr) )((void) (0));
31737 sqlite3TreeViewLine(pView, "subquery-expr flags=0x%x", pExpr->flags);
31738 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
31739 break;
31740 }
31741 case TK_IN49: {
31742 sqlite3_str *pStr = sqlite3_str_new(0);
31743 char *z;
31744 sqlite3_str_appendf(pStr, "IN flags=0x%x", pExpr->flags);
31745 if( pExpr->iTable ) sqlite3_str_appendf(pStr, " iTable=%d",pExpr->iTable);
31746 if( ExprHasProperty(pExpr, EP_Subrtn)(((pExpr)->flags&(0x2000000))!=0) ){
31747 sqlite3_str_appendf(pStr, " subrtn(%d,%d)",
31748 pExpr->y.sub.regReturn, pExpr->y.sub.iAddr);
31749 }
31750 z = sqlite3_str_finish(pStr);
31751 sqlite3TreeViewLine(pView, z);
31752 sqlite3_free(z);
31753 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
31754 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
31755 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
31756 }else{
31757 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
31758 }
31759 break;
31760 }
31761#endif /* SQLITE_OMIT_SUBQUERY */
31762
31763 /*
31764 ** x BETWEEN y AND z
31765 **
31766 ** This is equivalent to
31767 **
31768 ** x>=y AND x<=z
31769 **
31770 ** X is stored in pExpr->pLeft.
31771 ** Y is stored in pExpr->pList->a[0].pExpr.
31772 ** Z is stored in pExpr->pList->a[1].pExpr.
31773 */
31774 case TK_BETWEEN48: {
31775 const Expr *pX, *pY, *pZ;
31776 pX = pExpr->pLeft;
31777 assert( ExprUseXList(pExpr) )((void) (0));
31778 assert( pExpr->x.pList->nExpr==2 )((void) (0));
31779 pY = pExpr->x.pList->a[0].pExpr;
31780 pZ = pExpr->x.pList->a[1].pExpr;
31781 sqlite3TreeViewLine(pView, "BETWEEN");
31782 sqlite3TreeViewExpr(pView, pX, 1);
31783 sqlite3TreeViewExpr(pView, pY, 1);
31784 sqlite3TreeViewExpr(pView, pZ, 0);
31785 break;
31786 }
31787 case TK_TRIGGER77: {
31788 /* If the opcode is TK_TRIGGER, then the expression is a reference
31789 ** to a column in the new.* or old.* pseudo-tables available to
31790 ** trigger programs. In this case Expr.iTable is set to 1 for the
31791 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
31792 ** is set to the column of the pseudo-table to read, or to -1 to
31793 ** read the rowid field.
31794 */
31795 sqlite3TreeViewLine(pView, "%s(%d)",
31796 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
31797 break;
31798 }
31799 case TK_CASE157: {
31800 sqlite3TreeViewLine(pView, "CASE");
31801 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
31802 assert( ExprUseXList(pExpr) )((void) (0));
31803 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
31804 break;
31805 }
31806#ifndef SQLITE_OMIT_TRIGGER
31807 case TK_RAISE71: {
31808 const char *zType = "unk";
31809 switch( pExpr->affExpr ){
31810 case OE_Rollback1: zType = "rollback"; break;
31811 case OE_Abort2: zType = "abort"; break;
31812 case OE_Fail3: zType = "fail"; break;
31813 case OE_Ignore4: zType = "ignore"; break;
31814 }
31815 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
31816 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
31817 break;
31818 }
31819#endif
31820 case TK_MATCH46: {
31821 sqlite3TreeViewLine(pView, "MATCH {%d:%d}%s",
31822 pExpr->iTable, pExpr->iColumn, zFlgs);
31823 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
31824 break;
31825 }
31826 case TK_VECTOR177: {
31827 char *z = sqlite3_mprintf("VECTOR%s",zFlgs);
31828 assert( ExprUseXList(pExpr) )((void) (0));
31829 sqlite3TreeViewBareExprList(pView, pExpr->x.pList, z);
31830 sqlite3_free(z);
31831 break;
31832 }
31833 case TK_SELECT_COLUMN178: {
31834 sqlite3TreeViewLine(pView, "SELECT-COLUMN %d of [0..%d]%s",
31835 pExpr->iColumn, pExpr->iTable-1,
31836 pExpr->pRight==pExpr->pLeft ? " (SELECT-owner)" : "");
31837 assert( ExprUseXSelect(pExpr->pLeft) )((void) (0));
31838 sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
31839 break;
31840 }
31841 case TK_IF_NULL_ROW179: {
31842 sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable);
31843 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
31844 break;
31845 }
31846 case TK_ERROR182: {
31847 Expr tmp;
31848 sqlite3TreeViewLine(pView, "ERROR");
31849 tmp = *pExpr;
31850 tmp.op = pExpr->op2;
31851 sqlite3TreeViewExpr(pView, &tmp, 0);
31852 break;
31853 }
31854 case TK_ROW75: {
31855 if( pExpr->iColumn<=0 ){
31856 sqlite3TreeViewLine(pView, "First FROM table rowid");
31857 }else{
31858 sqlite3TreeViewLine(pView, "First FROM table column %d",
31859 pExpr->iColumn-1);
31860 }
31861 break;
31862 }
31863 default: {
31864 sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
31865 break;
31866 }
31867 }
31868 if( zBinOp ){
31869 sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
31870 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
31871 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
31872 }else if( zUniOp ){
31873 sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
31874 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
31875 }
31876 sqlite3TreeViewPop(&pView);
31877}
31878
31879
31880/*
31881** Generate a human-readable explanation of an expression list.
31882*/
31883SQLITE_PRIVATEstatic void sqlite3TreeViewBareExprList(
31884 TreeView *pView,
31885 const ExprList *pList,
31886 const char *zLabel
31887){
31888 if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
31889 if( pList==0 ){
31890 sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
31891 }else{
31892 int i;
31893 sqlite3TreeViewLine(pView, "%s", zLabel);
31894 for(i=0; i<pList->nExpr; i++){
31895 int j = pList->a[i].u.x.iOrderByCol;
31896 char *zName = pList->a[i].zEName;
31897 int moreToFollow = i<pList->nExpr - 1;
31898 if( j || zName ){
31899 sqlite3TreeViewPush(&pView, moreToFollow);
31900 moreToFollow = 0;
31901 sqlite3TreeViewLine(pView, 0);
31902 if( zName ){
31903 switch( pList->a[i].fg.eEName ){
31904 default:
31905 fprintf(stdoutstdout, "AS %s ", zName);
31906 break;
31907 case ENAME_TAB2:
31908 fprintf(stdoutstdout, "TABLE-ALIAS-NAME(\"%s\") ", zName);
31909 if( pList->a[i].fg.bUsed ) fprintf(stdoutstdout, "(used) ");
31910 if( pList->a[i].fg.bUsingTerm ) fprintf(stdoutstdout, "(USING-term) ");
31911 if( pList->a[i].fg.bNoExpand ) fprintf(stdoutstdout, "(NoExpand) ");
31912 break;
31913 case ENAME_SPAN1:
31914 fprintf(stdoutstdout, "SPAN(\"%s\") ", zName);
31915 break;
31916 }
31917 }
31918 if( j ){
31919 fprintf(stdoutstdout, "iOrderByCol=%d", j);
31920 }
31921 fprintf(stdoutstdout, "\n");
31922 fflush(stdoutstdout);
31923 }
31924 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow);
31925 if( j || zName ){
31926 sqlite3TreeViewPop(&pView);
31927 }
31928 }
31929 }
31930}
31931SQLITE_PRIVATEstatic void sqlite3TreeViewExprList(
31932 TreeView *pView,
31933 const ExprList *pList,
31934 u8 moreToFollow,
31935 const char *zLabel
31936){
31937 sqlite3TreeViewPush(&pView, moreToFollow);
31938 sqlite3TreeViewBareExprList(pView, pList, zLabel);
31939 sqlite3TreeViewPop(&pView);
31940}
31941
31942/*
31943** Generate a human-readable explanation of an id-list.
31944*/
31945SQLITE_PRIVATEstatic void sqlite3TreeViewBareIdList(
31946 TreeView *pView,
31947 const IdList *pList,
31948 const char *zLabel
31949){
31950 if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
31951 if( pList==0 ){
31952 sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
31953 }else{
31954 int i;
31955 sqlite3TreeViewLine(pView, "%s", zLabel);
31956 for(i=0; i<pList->nId; i++){
31957 char *zName = pList->a[i].zName;
31958 int moreToFollow = i<pList->nId - 1;
31959 if( zName==0 ) zName = "(null)";
31960 sqlite3TreeViewPush(&pView, moreToFollow);
31961 sqlite3TreeViewLine(pView, 0);
31962 if( pList->eU4==EU4_NONE0 ){
31963 fprintf(stdoutstdout, "%s\n", zName);
31964 }else if( pList->eU4==EU4_IDX1 ){
31965 fprintf(stdoutstdout, "%s (%d)\n", zName, pList->a[i].u4.idx);
31966 }else{
31967 assert( pList->eU4==EU4_EXPR )((void) (0));
31968 if( pList->a[i].u4.pExpr==0 ){
31969 fprintf(stdoutstdout, "%s (pExpr=NULL)\n", zName);
31970 }else{
31971 fprintf(stdoutstdout, "%s\n", zName);
31972 sqlite3TreeViewPush(&pView, i<pList->nId-1);
31973 sqlite3TreeViewExpr(pView, pList->a[i].u4.pExpr, 0);
31974 sqlite3TreeViewPop(&pView);
31975 }
31976 }
31977 sqlite3TreeViewPop(&pView);
31978 }
31979 }
31980}
31981SQLITE_PRIVATEstatic void sqlite3TreeViewIdList(
31982 TreeView *pView,
31983 const IdList *pList,
31984 u8 moreToFollow,
31985 const char *zLabel
31986){
31987 sqlite3TreeViewPush(&pView, moreToFollow);
31988 sqlite3TreeViewBareIdList(pView, pList, zLabel);
31989 sqlite3TreeViewPop(&pView);
31990}
31991
31992/*
31993** Generate a human-readable explanation of a list of Upsert objects
31994*/
31995SQLITE_PRIVATEstatic void sqlite3TreeViewUpsert(
31996 TreeView *pView,
31997 const Upsert *pUpsert,
31998 u8 moreToFollow
31999){
32000 if( pUpsert==0 ) return;
32001 sqlite3TreeViewPush(&pView, moreToFollow);
32002 while( pUpsert ){
32003 int n;
32004 sqlite3TreeViewPush(&pView, pUpsert->pNextUpsert!=0 || moreToFollow);
32005 sqlite3TreeViewLine(pView, "ON CONFLICT DO %s",
32006 pUpsert->isDoUpdate ? "UPDATE" : "NOTHING");
32007 n = (pUpsert->pUpsertSet!=0) + (pUpsert->pUpsertWhere!=0);
32008 sqlite3TreeViewExprList(pView, pUpsert->pUpsertTarget, (n--)>0, "TARGET");
32009 sqlite3TreeViewExprList(pView, pUpsert->pUpsertSet, (n--)>0, "SET");
32010 if( pUpsert->pUpsertWhere ){
32011 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
32012 sqlite3TreeViewExpr(pView, pUpsert->pUpsertWhere, 0);
32013 sqlite3TreeViewPop(&pView);
32014 }
32015 sqlite3TreeViewPop(&pView);
32016 pUpsert = pUpsert->pNextUpsert;
32017 }
32018 sqlite3TreeViewPop(&pView);
32019}
32020
32021#if TREETRACE_ENABLED0
32022/*
32023** Generate a human-readable diagram of the data structure that go
32024** into generating an DELETE statement.
32025*/
32026SQLITE_PRIVATEstatic void sqlite3TreeViewDelete(
32027 const With *pWith,
32028 const SrcList *pTabList,
32029 const Expr *pWhere,
32030 const ExprList *pOrderBy,
32031 const Expr *pLimit,
32032 const Trigger *pTrigger
32033){
32034 int n = 0;
32035 TreeView *pView = 0;
32036 sqlite3TreeViewPush(&pView, 0);
32037 sqlite3TreeViewLine(pView, "DELETE");
32038 if( pWith ) n++;
32039 if( pTabList ) n++;
32040 if( pWhere ) n++;
32041 if( pOrderBy ) n++;
32042 if( pLimit ) n++;
32043 if( pTrigger ) n++;
32044 if( pWith ){
32045 sqlite3TreeViewPush(&pView, (--n)>0);
32046 sqlite3TreeViewWith(pView, pWith, 0);
32047 sqlite3TreeViewPop(&pView);
32048 }
32049 if( pTabList ){
32050 sqlite3TreeViewPush(&pView, (--n)>0);
32051 sqlite3TreeViewLine(pView, "FROM");
32052 sqlite3TreeViewSrcList(pView, pTabList);
32053 sqlite3TreeViewPop(&pView);
32054 }
32055 if( pWhere ){
32056 sqlite3TreeViewPush(&pView, (--n)>0);
32057 sqlite3TreeViewLine(pView, "WHERE");
32058 sqlite3TreeViewExpr(pView, pWhere, 0);
32059 sqlite3TreeViewPop(&pView);
32060 }
32061 if( pOrderBy ){
32062 sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY");
32063 }
32064 if( pLimit ){
32065 sqlite3TreeViewPush(&pView, (--n)>0);
32066 sqlite3TreeViewLine(pView, "LIMIT");
32067 sqlite3TreeViewExpr(pView, pLimit, 0);
32068 sqlite3TreeViewPop(&pView);
32069 }
32070 if( pTrigger ){
32071 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
32072 }
32073 sqlite3TreeViewPop(&pView);
32074}
32075#endif /* TREETRACE_ENABLED */
32076
32077#if TREETRACE_ENABLED0
32078/*
32079** Generate a human-readable diagram of the data structure that go
32080** into generating an INSERT statement.
32081*/
32082SQLITE_PRIVATEstatic void sqlite3TreeViewInsert(
32083 const With *pWith,
32084 const SrcList *pTabList,
32085 const IdList *pColumnList,
32086 const Select *pSelect,
32087 const ExprList *pExprList,
32088 int onError,
32089 const Upsert *pUpsert,
32090 const Trigger *pTrigger
32091){
32092 TreeView *pView = 0;
32093 int n = 0;
32094 const char *zLabel = "INSERT";
32095 switch( onError ){
32096 case OE_Replace5: zLabel = "REPLACE"; break;
32097 case OE_Ignore4: zLabel = "INSERT OR IGNORE"; break;
32098 case OE_Rollback1: zLabel = "INSERT OR ROLLBACK"; break;
32099 case OE_Abort2: zLabel = "INSERT OR ABORT"; break;
32100 case OE_Fail3: zLabel = "INSERT OR FAIL"; break;
32101 }
32102 sqlite3TreeViewPush(&pView, 0);
32103 sqlite3TreeViewLine(pView, zLabel);
32104 if( pWith ) n++;
32105 if( pTabList ) n++;
32106 if( pColumnList ) n++;
32107 if( pSelect ) n++;
32108 if( pExprList ) n++;
32109 if( pUpsert ) n++;
32110 if( pTrigger ) n++;
32111 if( pWith ){
32112 sqlite3TreeViewPush(&pView, (--n)>0);
32113 sqlite3TreeViewWith(pView, pWith, 0);
32114 sqlite3TreeViewPop(&pView);
32115 }
32116 if( pTabList ){
32117 sqlite3TreeViewPush(&pView, (--n)>0);
32118 sqlite3TreeViewLine(pView, "INTO");
32119 sqlite3TreeViewSrcList(pView, pTabList);
32120 sqlite3TreeViewPop(&pView);
32121 }
32122 if( pColumnList ){
32123 sqlite3TreeViewIdList(pView, pColumnList, (--n)>0, "COLUMNS");
32124 }
32125 if( pSelect ){
32126 sqlite3TreeViewPush(&pView, (--n)>0);
32127 sqlite3TreeViewLine(pView, "DATA-SOURCE");
32128 sqlite3TreeViewSelect(pView, pSelect, 0);
32129 sqlite3TreeViewPop(&pView);
32130 }
32131 if( pExprList ){
32132 sqlite3TreeViewExprList(pView, pExprList, (--n)>0, "VALUES");
32133 }
32134 if( pUpsert ){
32135 sqlite3TreeViewPush(&pView, (--n)>0);
32136 sqlite3TreeViewLine(pView, "UPSERT");
32137 sqlite3TreeViewUpsert(pView, pUpsert, 0);
32138 sqlite3TreeViewPop(&pView);
32139 }
32140 if( pTrigger ){
32141 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
32142 }
32143 sqlite3TreeViewPop(&pView);
32144}
32145#endif /* TREETRACE_ENABLED */
32146
32147#if TREETRACE_ENABLED0
32148/*
32149** Generate a human-readable diagram of the data structure that go
32150** into generating an UPDATE statement.
32151*/
32152SQLITE_PRIVATEstatic void sqlite3TreeViewUpdate(
32153 const With *pWith,
32154 const SrcList *pTabList,
32155 const ExprList *pChanges,
32156 const Expr *pWhere,
32157 int onError,
32158 const ExprList *pOrderBy,
32159 const Expr *pLimit,
32160 const Upsert *pUpsert,
32161 const Trigger *pTrigger
32162){
32163 int n = 0;
32164 TreeView *pView = 0;
32165 const char *zLabel = "UPDATE";
32166 switch( onError ){
32167 case OE_Replace5: zLabel = "UPDATE OR REPLACE"; break;
32168 case OE_Ignore4: zLabel = "UPDATE OR IGNORE"; break;
32169 case OE_Rollback1: zLabel = "UPDATE OR ROLLBACK"; break;
32170 case OE_Abort2: zLabel = "UPDATE OR ABORT"; break;
32171 case OE_Fail3: zLabel = "UPDATE OR FAIL"; break;
32172 }
32173 sqlite3TreeViewPush(&pView, 0);
32174 sqlite3TreeViewLine(pView, zLabel);
32175 if( pWith ) n++;
32176 if( pTabList ) n++;
32177 if( pChanges ) n++;
32178 if( pWhere ) n++;
32179 if( pOrderBy ) n++;
32180 if( pLimit ) n++;
32181 if( pUpsert ) n++;
32182 if( pTrigger ) n++;
32183 if( pWith ){
32184 sqlite3TreeViewPush(&pView, (--n)>0);
32185 sqlite3TreeViewWith(pView, pWith, 0);
32186 sqlite3TreeViewPop(&pView);
32187 }
32188 if( pTabList ){
32189 sqlite3TreeViewPush(&pView, (--n)>0);
32190 sqlite3TreeViewLine(pView, "FROM");
32191 sqlite3TreeViewSrcList(pView, pTabList);
32192 sqlite3TreeViewPop(&pView);
32193 }
32194 if( pChanges ){
32195 sqlite3TreeViewExprList(pView, pChanges, (--n)>0, "SET");
32196 }
32197 if( pWhere ){
32198 sqlite3TreeViewPush(&pView, (--n)>0);
32199 sqlite3TreeViewLine(pView, "WHERE");
32200 sqlite3TreeViewExpr(pView, pWhere, 0);
32201 sqlite3TreeViewPop(&pView);
32202 }
32203 if( pOrderBy ){
32204 sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY");
32205 }
32206 if( pLimit ){
32207 sqlite3TreeViewPush(&pView, (--n)>0);
32208 sqlite3TreeViewLine(pView, "LIMIT");
32209 sqlite3TreeViewExpr(pView, pLimit, 0);
32210 sqlite3TreeViewPop(&pView);
32211 }
32212 if( pUpsert ){
32213 sqlite3TreeViewPush(&pView, (--n)>0);
32214 sqlite3TreeViewLine(pView, "UPSERT");
32215 sqlite3TreeViewUpsert(pView, pUpsert, 0);
32216 sqlite3TreeViewPop(&pView);
32217 }
32218 if( pTrigger ){
32219 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
32220 }
32221 sqlite3TreeViewPop(&pView);
32222}
32223#endif /* TREETRACE_ENABLED */
32224
32225#ifndef SQLITE_OMIT_TRIGGER
32226/*
32227** Show a human-readable graph of a TriggerStep
32228*/
32229SQLITE_PRIVATEstatic void sqlite3TreeViewTriggerStep(
32230 TreeView *pView,
32231 const TriggerStep *pStep,
32232 u8 moreToFollow,
32233 u8 showFullList
32234){
32235 int cnt = 0;
32236 if( pStep==0 ) return;
32237 sqlite3TreeViewPush(&pView,
32238 moreToFollow || (showFullList && pStep->pNext!=0));
32239 do{
32240 if( cnt++ && pStep->pNext==0 ){
32241 sqlite3TreeViewPop(&pView);
32242 sqlite3TreeViewPush(&pView, 0);
32243 }
32244 sqlite3TreeViewLine(pView, "%s", pStep->zSpan ? pStep->zSpan : "RETURNING");
32245 }while( showFullList && (pStep = pStep->pNext)!=0 );
32246 sqlite3TreeViewPop(&pView);
32247}
32248
32249/*
32250** Show a human-readable graph of a Trigger
32251*/
32252SQLITE_PRIVATEstatic void sqlite3TreeViewTrigger(
32253 TreeView *pView,
32254 const Trigger *pTrigger,
32255 u8 moreToFollow,
32256 u8 showFullList
32257){
32258 int cnt = 0;
32259 if( pTrigger==0 ) return;
32260 sqlite3TreeViewPush(&pView,
32261 moreToFollow || (showFullList && pTrigger->pNext!=0));
32262 do{
32263 if( cnt++ && pTrigger->pNext==0 ){
32264 sqlite3TreeViewPop(&pView);
32265 sqlite3TreeViewPush(&pView, 0);
32266 }
32267 sqlite3TreeViewLine(pView, "TRIGGER %s", pTrigger->zName);
32268 sqlite3TreeViewPush(&pView, 0);
32269 sqlite3TreeViewTriggerStep(pView, pTrigger->step_list, 0, 1);
32270 sqlite3TreeViewPop(&pView);
32271 }while( showFullList && (pTrigger = pTrigger->pNext)!=0 );
32272 sqlite3TreeViewPop(&pView);
32273}
32274#endif /* SQLITE_OMIT_TRIGGER */
32275
32276
32277/*
32278** These simplified versions of the tree-view routines omit unnecessary
32279** parameters. These variants are intended to be used from a symbolic
32280** debugger, such as "gdb", during interactive debugging sessions.
32281**
32282** This routines are given external linkage so that they will always be
32283** accessible to the debugging, and to avoid warnings about unused
32284** functions. But these routines only exist in debugging builds, so they
32285** do not contaminate the interface.
32286*/
32287SQLITE_PRIVATEstatic void sqlite3ShowExpr(const Expr *p){ sqlite3TreeViewExpr(0,p,0); }
32288SQLITE_PRIVATEstatic void sqlite3ShowExprList(const ExprList *p){ sqlite3TreeViewExprList(0,p,0,0);}
32289SQLITE_PRIVATEstatic void sqlite3ShowIdList(const IdList *p){ sqlite3TreeViewIdList(0,p,0,0); }
32290SQLITE_PRIVATEstatic void sqlite3ShowSrcList(const SrcList *p){ sqlite3TreeViewSrcList(0,p); }
32291SQLITE_PRIVATEstatic void sqlite3ShowSelect(const Select *p){ sqlite3TreeViewSelect(0,p,0); }
32292SQLITE_PRIVATEstatic void sqlite3ShowWith(const With *p){ sqlite3TreeViewWith(0,p,0); }
32293SQLITE_PRIVATEstatic void sqlite3ShowUpsert(const Upsert *p){ sqlite3TreeViewUpsert(0,p,0); }
32294#ifndef SQLITE_OMIT_TRIGGER
32295SQLITE_PRIVATEstatic void sqlite3ShowTriggerStep(const TriggerStep *p){
32296 sqlite3TreeViewTriggerStep(0,p,0,0);
32297}
32298SQLITE_PRIVATEstatic void sqlite3ShowTriggerStepList(const TriggerStep *p){
32299 sqlite3TreeViewTriggerStep(0,p,0,1);
32300}
32301SQLITE_PRIVATEstatic void sqlite3ShowTrigger(const Trigger *p){ sqlite3TreeViewTrigger(0,p,0,0); }
32302SQLITE_PRIVATEstatic void sqlite3ShowTriggerList(const Trigger *p){ sqlite3TreeViewTrigger(0,p,0,1);}
32303#endif
32304#ifndef SQLITE_OMIT_WINDOWFUNC
32305SQLITE_PRIVATEstatic void sqlite3ShowWindow(const Window *p){ sqlite3TreeViewWindow(0,p,0); }
32306SQLITE_PRIVATEstatic void sqlite3ShowWinFunc(const Window *p){ sqlite3TreeViewWinFunc(0,p,0); }
32307#endif
32308
32309#endif /* SQLITE_DEBUG */
32310
32311/************** End of treeview.c ********************************************/
32312/************** Begin file random.c ******************************************/
32313/*
32314** 2001 September 15
32315**
32316** The author disclaims copyright to this source code. In place of
32317** a legal notice, here is a blessing:
32318**
32319** May you do good and not evil.
32320** May you find forgiveness for yourself and forgive others.
32321** May you share freely, never taking more than you give.
32322**
32323*************************************************************************
32324** This file contains code to implement a pseudo-random number
32325** generator (PRNG) for SQLite.
32326**
32327** Random numbers are used by some of the database backends in order
32328** to generate random integer keys for tables or random filenames.
32329*/
32330/* #include "sqliteInt.h" */
32331
32332
32333/* All threads share a single random number generator.
32334** This structure is the current state of the generator.
32335*/
32336static SQLITE_WSD struct sqlite3PrngType {
32337 unsigned char isInit; /* True if initialized */
32338 unsigned char i, j; /* State variables */
32339 unsigned char s[256]; /* State variables */
32340} sqlite3Prng;
32341
32342/*
32343** Return N random bytes.
32344*/
32345SQLITE_API void sqlite3_randomness(int N, void *pBuf){
32346 unsigned char t;
32347 unsigned char *zBuf = pBuf;
32348
32349 /* The "wsdPrng" macro will resolve to the pseudo-random number generator
32350 ** state vector. If writable static data is unsupported on the target,
32351 ** we have to locate the state vector at run-time. In the more common
32352 ** case where writable static data is supported, wsdPrng can refer directly
32353 ** to the "sqlite3Prng" state vector declared above.
32354 */
32355#ifdef SQLITE_OMIT_WSD
32356 struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng)sqlite3Prng;
32357# define wsdPrngsqlite3Prng p[0]
32358#else
32359# define wsdPrngsqlite3Prng sqlite3Prng
32360#endif
32361
32362#if SQLITE_THREADSAFE2
32363 sqlite3_mutex *mutex;
32364#endif
32365
32366#ifndef SQLITE_OMIT_AUTOINIT
32367 if( sqlite3_initialize() ) return;
32368#endif
32369
32370#if SQLITE_THREADSAFE2
32371 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG5);
32372#endif
32373
32374 sqlite3_mutex_enter(mutex);
32375 if( N<=0 || pBuf==0 ){
32376 wsdPrngsqlite3Prng.isInit = 0;
32377 sqlite3_mutex_leave(mutex);
32378 return;
32379 }
32380
32381 /* Initialize the state of the random number generator once,
32382 ** the first time this routine is called. The seed value does
32383 ** not need to contain a lot of randomness since we are not
32384 ** trying to do secure encryption or anything like that...
32385 **
32386 ** Nothing in this file or anywhere else in SQLite does any kind of
32387 ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random
32388 ** number generator) not as an encryption device.
32389 */
32390 if( !wsdPrngsqlite3Prng.isInit ){
32391 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
32392 int i;
32393 char k[256];
32394 wsdPrngsqlite3Prng.j = 0;
32395 wsdPrngsqlite3Prng.i = 0;
32396 if( NEVER(pVfs==0)(pVfs==0) ){
32397 memset(k, 0, sizeof(k));
32398 }else{
32399 sqlite3OsRandomness(pVfs, 256, k);
32400 }
32401 for(i=0; i<256; i++){
32402 wsdPrngsqlite3Prng.s[i] = (u8)i;
32403 }
32404 for(i=0; i<256; i++){
32405 wsdPrngsqlite3Prng.j += wsdPrngsqlite3Prng.s[i] + k[i];
32406 t = wsdPrngsqlite3Prng.s[wsdPrngsqlite3Prng.j];
32407 wsdPrngsqlite3Prng.s[wsdPrngsqlite3Prng.j] = wsdPrngsqlite3Prng.s[i];
32408 wsdPrngsqlite3Prng.s[i] = t;
32409 }
32410 wsdPrngsqlite3Prng.isInit = 1;
32411 }
32412
32413 assert( N>0 )((void) (0));
32414 do{
32415 wsdPrngsqlite3Prng.i++;
32416 t = wsdPrngsqlite3Prng.s[wsdPrngsqlite3Prng.i];
32417 wsdPrngsqlite3Prng.j += t;
32418 wsdPrngsqlite3Prng.s[wsdPrngsqlite3Prng.i] = wsdPrngsqlite3Prng.s[wsdPrngsqlite3Prng.j];
32419 wsdPrngsqlite3Prng.s[wsdPrngsqlite3Prng.j] = t;
32420 t += wsdPrngsqlite3Prng.s[wsdPrngsqlite3Prng.i];
32421 *(zBuf++) = wsdPrngsqlite3Prng.s[t];
32422 }while( --N );
32423 sqlite3_mutex_leave(mutex);
32424}
32425
32426#ifndef SQLITE_UNTESTABLE
32427/*
32428** For testing purposes, we sometimes want to preserve the state of
32429** PRNG and restore the PRNG to its saved state at a later time, or
32430** to reset the PRNG to its initial state. These routines accomplish
32431** those tasks.
32432**
32433** The sqlite3_test_control() interface calls these routines to
32434** control the PRNG.
32435*/
32436static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
32437SQLITE_PRIVATEstatic void sqlite3PrngSaveState(void){
32438 memcpy(
32439 &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng)sqlite3SavedPrng,
32440 &GLOBAL(struct sqlite3PrngType, sqlite3Prng)sqlite3Prng,
32441 sizeof(sqlite3Prng)
32442 );
32443}
32444SQLITE_PRIVATEstatic void sqlite3PrngRestoreState(void){
32445 memcpy(
32446 &GLOBAL(struct sqlite3PrngType, sqlite3Prng)sqlite3Prng,
32447 &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng)sqlite3SavedPrng,
32448 sizeof(sqlite3Prng)
32449 );
32450}
32451#endif /* SQLITE_UNTESTABLE */
32452
32453/************** End of random.c **********************************************/
32454/************** Begin file threads.c *****************************************/
32455/*
32456** 2012 July 21
32457**
32458** The author disclaims copyright to this source code. In place of
32459** a legal notice, here is a blessing:
32460**
32461** May you do good and not evil.
32462** May you find forgiveness for yourself and forgive others.
32463** May you share freely, never taking more than you give.
32464**
32465******************************************************************************
32466**
32467** This file presents a simple cross-platform threading interface for
32468** use internally by SQLite.
32469**
32470** A "thread" can be created using sqlite3ThreadCreate(). This thread
32471** runs independently of its creator until it is joined using
32472** sqlite3ThreadJoin(), at which point it terminates.
32473**
32474** Threads do not have to be real. It could be that the work of the
32475** "thread" is done by the main thread at either the sqlite3ThreadCreate()
32476** or sqlite3ThreadJoin() call. This is, in fact, what happens in
32477** single threaded systems. Nothing in SQLite requires multiple threads.
32478** This interface exists so that applications that want to take advantage
32479** of multiple cores can do so, while also allowing applications to stay
32480** single-threaded if desired.
32481*/
32482/* #include "sqliteInt.h" */
32483#if SQLITE_OS_WIN0
32484/* # include "os_win.h" */
32485#endif
32486
32487#if SQLITE_MAX_WORKER_THREADS8>0
32488
32489/********************************* Unix Pthreads ****************************/
32490#if SQLITE_OS_UNIX1 && defined(SQLITE_MUTEX_PTHREADS) && SQLITE_THREADSAFE2>0
32491
32492#define SQLITE_THREADS_IMPLEMENTED1 1 /* Prevent the single-thread code below */
32493/* #include <pthread.h> */
32494
32495/* A running thread */
32496struct SQLiteThread {
32497 pthread_t tid; /* Thread ID */
32498 int done; /* Set to true when thread finishes */
32499 void *pOut; /* Result returned by the thread */
32500 void *(*xTask)(void*); /* The thread routine */
32501 void *pIn; /* Argument to the thread */
32502};
32503
32504/* Create a new thread */
32505SQLITE_PRIVATEstatic int sqlite3ThreadCreate(
32506 SQLiteThread **ppThread, /* OUT: Write the thread object here */
32507 void *(*xTask)(void*), /* Routine to run in a separate thread */
32508 void *pIn /* Argument passed into xTask() */
32509){
32510 SQLiteThread *p;
32511 int rc;
32512
32513 assert( ppThread!=0 )((void) (0));
32514 assert( xTask!=0 )((void) (0));
32515 /* This routine is never used in single-threaded mode */
32516 assert( sqlite3GlobalConfig.bCoreMutex!=0 )((void) (0));
32517
32518 *ppThread = 0;
32519 p = sqlite3Malloc(sizeof(*p));
32520 if( p==0 ) return SQLITE_NOMEM_BKPT7;
32521 memset(p, 0, sizeof(*p));
32522 p->xTask = xTask;
32523 p->pIn = pIn;
32524 /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
32525 ** function that returns SQLITE_ERROR when passed the argument 200, that
32526 ** forces worker threads to run sequentially and deterministically
32527 ** for testing purposes. */
32528 if( sqlite3FaultSim(200) ){
32529 rc = 1;
32530 }else{
32531 rc = pthread_create(&p->tid, 0, xTask, pIn);
32532 }
32533 if( rc ){
32534 p->done = 1;
32535 p->pOut = xTask(pIn);
32536 }
32537 *ppThread = p;
32538 return SQLITE_OK0;
32539}
32540
32541/* Get the results of the thread */
32542SQLITE_PRIVATEstatic int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
32543 int rc;
32544
32545 assert( ppOut!=0 )((void) (0));
32546 if( NEVER(p==0)(p==0) ) return SQLITE_NOMEM_BKPT7;
32547 if( p->done ){
32548 *ppOut = p->pOut;
32549 rc = SQLITE_OK0;
32550 }else{
32551 rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR1 : SQLITE_OK0;
32552 }
32553 sqlite3_free(p);
32554 return rc;
32555}
32556
32557#endif /* SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) */
32558/******************************** End Unix Pthreads *************************/
32559
32560
32561/********************************* Win32 Threads ****************************/
32562#if SQLITE_OS_WIN_THREADS
32563
32564#define SQLITE_THREADS_IMPLEMENTED1 1 /* Prevent the single-thread code below */
32565#include <process.h>
32566
32567/* A running thread */
32568struct SQLiteThread {
32569 void *tid; /* The thread handle */
32570 unsigned id; /* The thread identifier */
32571 void *(*xTask)(void*); /* The routine to run as a thread */
32572 void *pIn; /* Argument to xTask */
32573 void *pResult; /* Result of xTask */
32574};
32575
32576/* Thread procedure Win32 compatibility shim */
32577static unsigned __stdcall sqlite3ThreadProc(
32578 void *pArg /* IN: Pointer to the SQLiteThread structure */
32579){
32580 SQLiteThread *p = (SQLiteThread *)pArg;
32581
32582 assert( p!=0 )((void) (0));
32583#if 0
32584 /*
32585 ** This assert appears to trigger spuriously on certain
32586 ** versions of Windows, possibly due to _beginthreadex()
32587 ** and/or CreateThread() not fully setting their thread
32588 ** ID parameter before starting the thread.
32589 */
32590 assert( p->id==GetCurrentThreadId() )((void) (0));
32591#endif
32592 assert( p->xTask!=0 )((void) (0));
32593 p->pResult = p->xTask(p->pIn);
32594
32595 _endthreadex(0);
32596 return 0; /* NOT REACHED */
32597}
32598
32599/* Create a new thread */
32600SQLITE_PRIVATEstatic int sqlite3ThreadCreate(
32601 SQLiteThread **ppThread, /* OUT: Write the thread object here */
32602 void *(*xTask)(void*), /* Routine to run in a separate thread */
32603 void *pIn /* Argument passed into xTask() */
32604){
32605 SQLiteThread *p;
32606
32607 assert( ppThread!=0 )((void) (0));
32608 assert( xTask!=0 )((void) (0));
32609 *ppThread = 0;
32610 p = sqlite3Malloc(sizeof(*p));
32611 if( p==0 ) return SQLITE_NOMEM_BKPT7;
32612 /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
32613 ** function that returns SQLITE_ERROR when passed the argument 200, that
32614 ** forces worker threads to run sequentially and deterministically
32615 ** (via the sqlite3FaultSim() term of the conditional) for testing
32616 ** purposes. */
32617 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex==0 || sqlite3FaultSim(200) ){
32618 memset(p, 0, sizeof(*p));
32619 }else{
32620 p->xTask = xTask;
32621 p->pIn = pIn;
32622 p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id);
32623 if( p->tid==0 ){
32624 memset(p, 0, sizeof(*p));
32625 }
32626 }
32627 if( p->xTask==0 ){
32628 p->id = GetCurrentThreadId();
32629 p->pResult = xTask(pIn);
32630 }
32631 *ppThread = p;
32632 return SQLITE_OK0;
32633}
32634
32635SQLITE_PRIVATEstatic DWORD sqlite3Win32Wait(HANDLE hObject); /* os_win.c */
32636
32637/* Get the results of the thread */
32638SQLITE_PRIVATEstatic int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
32639 DWORD rc;
32640 BOOL bRc;
32641
32642 assert( ppOut!=0 )((void) (0));
32643 if( NEVER(p==0)(p==0) ) return SQLITE_NOMEM_BKPT7;
32644 if( p->xTask==0 ){
32645 /* assert( p->id==GetCurrentThreadId() ); */
32646 rc = WAIT_OBJECT_0;
32647 assert( p->tid==0 )((void) (0));
32648 }else{
32649 assert( p->id!=0 && p->id!=GetCurrentThreadId() )((void) (0));
32650 rc = sqlite3Win32Wait((HANDLE)p->tid);
32651 assert( rc!=WAIT_IO_COMPLETION )((void) (0));
32652 bRc = CloseHandle((HANDLE)p->tid);
32653 assert( bRc )((void) (0));
32654 }
32655 if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult;
32656 sqlite3_free(p);
32657 return (rc==WAIT_OBJECT_0) ? SQLITE_OK0 : SQLITE_ERROR1;
32658}
32659
32660#endif /* SQLITE_OS_WIN_THREADS */
32661/******************************** End Win32 Threads *************************/
32662
32663
32664/********************************* Single-Threaded **************************/
32665#ifndef SQLITE_THREADS_IMPLEMENTED1
32666/*
32667** This implementation does not actually create a new thread. It does the
32668** work of the thread in the main thread, when either the thread is created
32669** or when it is joined
32670*/
32671
32672/* A running thread */
32673struct SQLiteThread {
32674 void *(*xTask)(void*); /* The routine to run as a thread */
32675 void *pIn; /* Argument to xTask */
32676 void *pResult; /* Result of xTask */
32677};
32678
32679/* Create a new thread */
32680SQLITE_PRIVATEstatic int sqlite3ThreadCreate(
32681 SQLiteThread **ppThread, /* OUT: Write the thread object here */
32682 void *(*xTask)(void*), /* Routine to run in a separate thread */
32683 void *pIn /* Argument passed into xTask() */
32684){
32685 SQLiteThread *p;
32686
32687 assert( ppThread!=0 )((void) (0));
32688 assert( xTask!=0 )((void) (0));
32689 *ppThread = 0;
32690 p = sqlite3Malloc(sizeof(*p));
32691 if( p==0 ) return SQLITE_NOMEM_BKPT7;
32692 if( (SQLITE_PTR_TO_INT(p)((int)(long int)(p))/17)&1 ){
32693 p->xTask = xTask;
32694 p->pIn = pIn;
32695 }else{
32696 p->xTask = 0;
32697 p->pResult = xTask(pIn);
32698 }
32699 *ppThread = p;
32700 return SQLITE_OK0;
32701}
32702
32703/* Get the results of the thread */
32704SQLITE_PRIVATEstatic int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
32705
32706 assert( ppOut!=0 )((void) (0));
32707 if( NEVER(p==0)(p==0) ) return SQLITE_NOMEM_BKPT7;
32708 if( p->xTask ){
32709 *ppOut = p->xTask(p->pIn);
32710 }else{
32711 *ppOut = p->pResult;
32712 }
32713 sqlite3_free(p);
32714
32715#if defined(SQLITE_TEST)
32716 {
32717 void *pTstAlloc = sqlite3Malloc(10);
32718 if (!pTstAlloc) return SQLITE_NOMEM_BKPT7;
32719 sqlite3_free(pTstAlloc);
32720 }
32721#endif
32722
32723 return SQLITE_OK0;
32724}
32725
32726#endif /* !defined(SQLITE_THREADS_IMPLEMENTED) */
32727/****************************** End Single-Threaded *************************/
32728#endif /* SQLITE_MAX_WORKER_THREADS>0 */
32729
32730/************** End of threads.c *********************************************/
32731/************** Begin file utf.c *********************************************/
32732/*
32733** 2004 April 13
32734**
32735** The author disclaims copyright to this source code. In place of
32736** a legal notice, here is a blessing:
32737**
32738** May you do good and not evil.
32739** May you find forgiveness for yourself and forgive others.
32740** May you share freely, never taking more than you give.
32741**
32742*************************************************************************
32743** This file contains routines used to translate between UTF-8,
32744** UTF-16, UTF-16BE, and UTF-16LE.
32745**
32746** Notes on UTF-8:
32747**
32748** Byte-0 Byte-1 Byte-2 Byte-3 Value
32749** 0xxxxxxx 00000000 00000000 0xxxxxxx
32750** 110yyyyy 10xxxxxx 00000000 00000yyy yyxxxxxx
32751** 1110zzzz 10yyyyyy 10xxxxxx 00000000 zzzzyyyy yyxxxxxx
32752** 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx 000uuuuu zzzzyyyy yyxxxxxx
32753**
32754**
32755** Notes on UTF-16: (with wwww+1==uuuuu)
32756**
32757** Word-0 Word-1 Value
32758** 110110ww wwzzzzyy 110111yy yyxxxxxx 000uuuuu zzzzyyyy yyxxxxxx
32759** zzzzyyyy yyxxxxxx 00000000 zzzzyyyy yyxxxxxx
32760**
32761**
32762** BOM or Byte Order Mark:
32763** 0xff 0xfe little-endian utf-16 follows
32764** 0xfe 0xff big-endian utf-16 follows
32765**
32766*/
32767/* #include "sqliteInt.h" */
32768/* #include <assert.h> */
32769/* #include "vdbeInt.h" */
32770
32771#if !defined(SQLITE_AMALGAMATION1) && SQLITE_BYTEORDER1234==0
32772/*
32773** The following constant value is used by the SQLITE_BIGENDIAN and
32774** SQLITE_LITTLEENDIAN macros.
32775*/
32776SQLITE_PRIVATEstatic const int sqlite3one = 1;
32777#endif /* SQLITE_AMALGAMATION && SQLITE_BYTEORDER==0 */
32778
32779/*
32780** This lookup table is used to help decode the first byte of
32781** a multi-byte UTF8 character.
32782*/
32783static const unsigned char sqlite3Utf8Trans1[] = {
32784 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
32785 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
32786 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
32787 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
32788 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
32789 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
32790 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
32791 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
32792};
32793
32794
32795#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); } }
{ \
32796 if( c<0x00080 ){ \
32797 *zOut++ = (u8)(c&0xFF); \
32798 } \
32799 else if( c<0x00800 ){ \
32800 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \
32801 *zOut++ = 0x80 + (u8)(c & 0x3F); \
32802 } \
32803 else if( c<0x10000 ){ \
32804 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \
32805 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
32806 *zOut++ = 0x80 + (u8)(c & 0x3F); \
32807 }else{ \
32808 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); \
32809 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); \
32810 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
32811 *zOut++ = 0x80 + (u8)(c & 0x3F); \
32812 } \
32813}
32814
32815#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)); } }
{ \
32816 if( c<=0xFFFF ){ \
32817 *zOut++ = (u8)(c&0x00FF); \
32818 *zOut++ = (u8)((c>>8)&0x00FF); \
32819 }else{ \
32820 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
32821 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
32822 *zOut++ = (u8)(c&0x00FF); \
32823 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \
32824 } \
32825}
32826
32827#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); } }
{ \
32828 if( c<=0xFFFF ){ \
32829 *zOut++ = (u8)((c>>8)&0x00FF); \
32830 *zOut++ = (u8)(c&0x00FF); \
32831 }else{ \
32832 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
32833 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
32834 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \
32835 *zOut++ = (u8)(c&0x00FF); \
32836 } \
32837}
32838
32839/*
32840** Translate a single UTF-8 character. Return the unicode value.
32841**
32842** During translation, assume that the byte that zTerm points
32843** is a 0x00.
32844**
32845** Write a pointer to the next unread byte back into *pzNext.
32846**
32847** Notes On Invalid UTF-8:
32848**
32849** * This routine never allows a 7-bit character (0x00 through 0x7f) to
32850** be encoded as a multi-byte character. Any multi-byte character that
32851** attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
32852**
32853** * This routine never allows a UTF16 surrogate value to be encoded.
32854** If a multi-byte character attempts to encode a value between
32855** 0xd800 and 0xe000 then it is rendered as 0xfffd.
32856**
32857** * Bytes in the range of 0x80 through 0xbf which occur as the first
32858** byte of a character are interpreted as single-byte characters
32859** and rendered as themselves even though they are technically
32860** invalid characters.
32861**
32862** * This routine accepts over-length UTF8 encodings
32863** for unicode values 0x80 and greater. It does not change over-length
32864** encodings to 0xfffd as some systems recommend.
32865*/
32866#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
; } }
\
32867 c = *(zIn++); \
32868 if( c>=0xc0 ){ \
32869 c = sqlite3Utf8Trans1[c-0xc0]; \
32870 while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \
32871 c = (c<<6) + (0x3f & *(zIn++)); \
32872 } \
32873 if( c<0x80 \
32874 || (c&0xFFFFF800)==0xD800 \
32875 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
32876 }
32877SQLITE_PRIVATEstatic u32 sqlite3Utf8Read(
32878 const unsigned char **pz /* Pointer to string from which to read char */
32879){
32880 unsigned int c;
32881
32882 /* Same as READ_UTF8() above but without the zTerm parameter.
32883 ** For this routine, we assume the UTF8 string is always zero-terminated.
32884 */
32885 c = *((*pz)++);
32886 if( c>=0xc0 ){
32887 c = sqlite3Utf8Trans1[c-0xc0];
32888 while( (*(*pz) & 0xc0)==0x80 ){
32889 c = (c<<6) + (0x3f & *((*pz)++));
32890 }
32891 if( c<0x80
32892 || (c&0xFFFFF800)==0xD800
32893 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; }
32894 }
32895 return c;
32896}
32897
32898
32899
32900
32901/*
32902** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
32903** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
32904*/
32905/* #define TRANSLATE_TRACE 1 */
32906
32907#ifndef SQLITE_OMIT_UTF16
32908/*
32909** This routine transforms the internal text encoding used by pMem to
32910** desiredEnc. It is an error if the string is already of the desired
32911** encoding, or if *pMem does not contain a string value.
32912*/
32913SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
32914 sqlite3_int64 len; /* Maximum length of output string in bytes */
32915 unsigned char *zOut; /* Output buffer */
32916 unsigned char *zIn; /* Input iterator */
32917 unsigned char *zTerm; /* End of input */
32918 unsigned char *z; /* Output iterator */
32919 unsigned int c;
32920
32921 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
32922 assert( pMem->flags&MEM_Str )((void) (0));
32923 assert( pMem->enc!=desiredEnc )((void) (0));
32924 assert( pMem->enc!=0 )((void) (0));
32925 assert( pMem->n>=0 )((void) (0));
32926
32927#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
32928 {
32929 StrAccum acc;
32930 char zBuf[1000];
32931 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
32932 sqlite3VdbeMemPrettyPrint(pMem, &acc);
32933 fprintf(stderrstderr, "INPUT: %s\n", sqlite3StrAccumFinish(&acc));
32934 }
32935#endif
32936
32937 /* If the translation is between UTF-16 little and big endian, then
32938 ** all that is required is to swap the byte order. This case is handled
32939 ** differently from the others.
32940 */
32941 if( pMem->enc!=SQLITE_UTF81 && desiredEnc!=SQLITE_UTF81 ){
32942 u8 temp;
32943 int rc;
32944 rc = sqlite3VdbeMemMakeWriteable(pMem);
32945 if( rc!=SQLITE_OK0 ){
32946 assert( rc==SQLITE_NOMEM )((void) (0));
32947 return SQLITE_NOMEM_BKPT7;
32948 }
32949 zIn = (u8*)pMem->z;
32950 zTerm = &zIn[pMem->n&~1];
32951 while( zIn<zTerm ){
32952 temp = *zIn;
32953 *zIn = *(zIn+1);
32954 zIn++;
32955 *zIn++ = temp;
32956 }
32957 pMem->enc = desiredEnc;
32958 goto translate_out;
32959 }
32960
32961 /* Set len to the maximum number of bytes required in the output buffer. */
32962 if( desiredEnc==SQLITE_UTF81 ){
32963 /* When converting from UTF-16, the maximum growth results from
32964 ** translating a 2-byte character to a 4-byte UTF-8 character.
32965 ** A single byte is required for the output string
32966 ** nul-terminator.
32967 */
32968 pMem->n &= ~1;
32969 len = 2 * (sqlite3_int64)pMem->n + 1;
32970 }else{
32971 /* When converting from UTF-8 to UTF-16 the maximum growth is caused
32972 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
32973 ** character. Two bytes are required in the output buffer for the
32974 ** nul-terminator.
32975 */
32976 len = 2 * (sqlite3_int64)pMem->n + 2;
32977 }
32978
32979 /* Set zIn to point at the start of the input buffer and zTerm to point 1
32980 ** byte past the end.
32981 **
32982 ** Variable zOut is set to point at the output buffer, space obtained
32983 ** from sqlite3_malloc().
32984 */
32985 zIn = (u8*)pMem->z;
32986 zTerm = &zIn[pMem->n];
32987 zOut = sqlite3DbMallocRaw(pMem->db, len);
32988 if( !zOut ){
32989 return SQLITE_NOMEM_BKPT7;
32990 }
32991 z = zOut;
32992
32993 if( pMem->enc==SQLITE_UTF81 ){
32994 if( desiredEnc==SQLITE_UTF16LE2 ){
32995 /* UTF-8 -> UTF-16 Little-endian */
32996 while( zIn<zTerm ){
32997 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
; } }
;
32998 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)); } }
;
32999 }
33000 }else{
33001 assert( desiredEnc==SQLITE_UTF16BE )((void) (0));
33002 /* UTF-8 -> UTF-16 Big-endian */
33003 while( zIn<zTerm ){
33004 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
; } }
;
33005 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); } }
;
33006 }
33007 }
33008 pMem->n = (int)(z - zOut);
33009 *z++ = 0;
33010 }else{
33011 assert( desiredEnc==SQLITE_UTF8 )((void) (0));
33012 if( pMem->enc==SQLITE_UTF16LE2 ){
33013 /* UTF-16 Little-endian -> UTF-8 */
33014 while( zIn<zTerm ){
33015 c = *(zIn++);
33016 c += (*(zIn++))<<8;
33017 if( c>=0xd800 && c<0xe000 ){
33018#ifdef SQLITE_REPLACE_INVALID_UTF
33019 if( c>=0xdc00 || zIn>=zTerm ){
33020 c = 0xfffd;
33021 }else{
33022 int c2 = *(zIn++);
33023 c2 += (*(zIn++))<<8;
33024 if( c2<0xdc00 || c2>=0xe000 ){
33025 zIn -= 2;
33026 c = 0xfffd;
33027 }else{
33028 c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000;
33029 }
33030 }
33031#else
33032 if( zIn<zTerm ){
33033 int c2 = (*zIn++);
33034 c2 += ((*zIn++)<<8);
33035 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);
33036 }
33037#endif
33038 }
33039 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); } }
;
33040 }
33041 }else{
33042 /* UTF-16 Big-endian -> UTF-8 */
33043 while( zIn<zTerm ){
33044 c = (*(zIn++))<<8;
33045 c += *(zIn++);
33046 if( c>=0xd800 && c<0xe000 ){
33047#ifdef SQLITE_REPLACE_INVALID_UTF
33048 if( c>=0xdc00 || zIn>=zTerm ){
33049 c = 0xfffd;
33050 }else{
33051 int c2 = (*(zIn++))<<8;
33052 c2 += *(zIn++);
33053 if( c2<0xdc00 || c2>=0xe000 ){
33054 zIn -= 2;
33055 c = 0xfffd;
33056 }else{
33057 c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000;
33058 }
33059 }
33060#else
33061 if( zIn<zTerm ){
33062 int c2 = ((*zIn++)<<8);
33063 c2 += (*zIn++);
33064 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);
33065 }
33066#endif
33067 }
33068 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); } }
;
33069 }
33070 }
33071 pMem->n = (int)(z - zOut);
33072 }
33073 *z = 0;
33074 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len )((void) (0));
33075
33076 c = MEM_Str0x0002|MEM_Term0x0200|(pMem->flags&(MEM_AffMask0x003f|MEM_Subtype0x0800));
33077 sqlite3VdbeMemRelease(pMem);
33078 pMem->flags = c;
33079 pMem->enc = desiredEnc;
33080 pMem->z = (char*)zOut;
33081 pMem->zMalloc = pMem->z;
33082 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z);
33083
33084translate_out:
33085#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
33086 {
33087 StrAccum acc;
33088 char zBuf[1000];
33089 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
33090 sqlite3VdbeMemPrettyPrint(pMem, &acc);
33091 fprintf(stderrstderr, "OUTPUT: %s\n", sqlite3StrAccumFinish(&acc));
33092 }
33093#endif
33094 return SQLITE_OK0;
33095}
33096#endif /* SQLITE_OMIT_UTF16 */
33097
33098#ifndef SQLITE_OMIT_UTF16
33099/*
33100** This routine checks for a byte-order mark at the beginning of the
33101** UTF-16 string stored in *pMem. If one is present, it is removed and
33102** the encoding of the Mem adjusted. This routine does not do any
33103** byte-swapping, it just sets Mem.enc appropriately.
33104**
33105** The allocation (static, dynamic etc.) and encoding of the Mem may be
33106** changed by this function.
33107*/
33108SQLITE_PRIVATEstatic int sqlite3VdbeMemHandleBom(Mem *pMem){
33109 int rc = SQLITE_OK0;
33110 u8 bom = 0;
33111
33112 assert( pMem->n>=0 )((void) (0));
33113 if( pMem->n>1 ){
33114 u8 b1 = *(u8 *)pMem->z;
33115 u8 b2 = *(((u8 *)pMem->z) + 1);
33116 if( b1==0xFE && b2==0xFF ){
33117 bom = SQLITE_UTF16BE3;
33118 }
33119 if( b1==0xFF && b2==0xFE ){
33120 bom = SQLITE_UTF16LE2;
33121 }
33122 }
33123
33124 if( bom ){
33125 rc = sqlite3VdbeMemMakeWriteable(pMem);
33126 if( rc==SQLITE_OK0 ){
33127 pMem->n -= 2;
33128 memmove(pMem->z, &pMem->z[2], pMem->n);
33129 pMem->z[pMem->n] = '\0';
33130 pMem->z[pMem->n+1] = '\0';
33131 pMem->flags |= MEM_Term0x0200;
33132 pMem->enc = bom;
33133 }
33134 }
33135 return rc;
33136}
33137#endif /* SQLITE_OMIT_UTF16 */
33138
33139/*
33140** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
33141** return the number of unicode characters in pZ up to (but not including)
33142** the first 0x00 byte. If nByte is not less than zero, return the
33143** number of unicode characters in the first nByte of pZ (or up to
33144** the first 0x00, whichever comes first).
33145*/
33146SQLITE_PRIVATEstatic int sqlite3Utf8CharLen(const char *zIn, int nByte){
33147 int r = 0;
33148 const u8 *z = (const u8*)zIn;
33149 const u8 *zTerm;
33150 if( nByte>=0 ){
33151 zTerm = &z[nByte];
33152 }else{
33153 zTerm = (const u8*)(-1);
33154 }
33155 assert( z<=zTerm )((void) (0));
33156 while( *z!=0 && z<zTerm ){
33157 SQLITE_SKIP_UTF8(z){ if( (*(z++))>=0xc0 ){ while( (*z & 0xc0)==0x80 ){ z++
; } } }
;
33158 r++;
33159 }
33160 return r;
33161}
33162
33163/* This test function is not currently used by the automated test-suite.
33164** Hence it is only available in debug builds.
33165*/
33166#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
33167/*
33168** Translate UTF-8 to UTF-8.
33169**
33170** This has the effect of making sure that the string is well-formed
33171** UTF-8. Miscoded characters are removed.
33172**
33173** The translation is done in-place and aborted if the output
33174** overruns the input.
33175*/
33176SQLITE_PRIVATEstatic int sqlite3Utf8To8(unsigned char *zIn){
33177 unsigned char *zOut = zIn;
33178 unsigned char *zStart = zIn;
33179 u32 c;
33180
33181 while( zIn[0] && zOut<=zIn ){
33182 c = sqlite3Utf8Read((const u8**)&zIn);
33183 if( c!=0xfffd ){
33184 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); } }
;
33185 }
33186 }
33187 *zOut = 0;
33188 return (int)(zOut - zStart);
33189}
33190#endif
33191
33192#ifndef SQLITE_OMIT_UTF16
33193/*
33194** Convert a UTF-16 string in the native encoding into a UTF-8 string.
33195** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
33196** be freed by the calling function.
33197**
33198** NULL is returned if there is an allocation error.
33199*/
33200SQLITE_PRIVATEstatic char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){
33201 Mem m;
33202 memset(&m, 0, sizeof(m));
33203 m.db = db;
33204 sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC((sqlite3_destructor_type)0));
33205 sqlite3VdbeChangeEncoding(&m, SQLITE_UTF81);
33206 if( db->mallocFailed ){
33207 sqlite3VdbeMemRelease(&m);
33208 m.z = 0;
33209 }
33210 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed )((void) (0));
33211 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed )((void) (0));
33212 assert( m.z || db->mallocFailed )((void) (0));
33213 return m.z;
33214}
33215
33216/*
33217** zIn is a UTF-16 encoded unicode string at least nChar characters long.
33218** Return the number of bytes in the first nChar unicode characters
33219** in pZ. nChar must be non-negative.
33220*/
33221SQLITE_PRIVATEstatic int sqlite3Utf16ByteLen(const void *zIn, int nChar){
33222 int c;
33223 unsigned char const *z = zIn;
33224 int n = 0;
33225
33226 if( SQLITE_UTF16NATIVE2==SQLITE_UTF16LE2 ) z++;
33227 while( n<nChar ){
33228 c = z[0];
33229 z += 2;
33230 if( c>=0xd8 && c<0xdc && z[0]>=0xdc && z[0]<0xe0 ) z += 2;
33231 n++;
33232 }
33233 return (int)(z-(unsigned char const *)zIn)
33234 - (SQLITE_UTF16NATIVE2==SQLITE_UTF16LE2);
33235}
33236
33237#if defined(SQLITE_TEST)
33238/*
33239** This routine is called from the TCL test function "translate_selftest".
33240** It checks that the primitives for serializing and deserializing
33241** characters in each encoding are inverses of each other.
33242*/
33243SQLITE_PRIVATEstatic void sqlite3UtfSelfTest(void){
33244 unsigned int i, t;
33245 unsigned char zBuf[20];
33246 unsigned char *z;
33247 int n;
33248 unsigned int c;
33249
33250 for(i=0; i<0x00110000; i++){
33251 z = zBuf;
33252 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); } }
;
33253 n = (int)(z-zBuf);
33254 assert( n>0 && n<=4 )((void) (0));
33255 z[0] = 0;
33256 z = zBuf;
33257 c = sqlite3Utf8Read((const u8**)&z);
33258 t = i;
33259 if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
33260 if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
33261 assert( c==t )((void) (0));
33262 assert( (z-zBuf)==n )((void) (0));
33263 }
33264}
33265#endif /* SQLITE_TEST */
33266#endif /* SQLITE_OMIT_UTF16 */
33267
33268/************** End of utf.c *************************************************/
33269/************** Begin file util.c ********************************************/
33270/*
33271** 2001 September 15
33272**
33273** The author disclaims copyright to this source code. In place of
33274** a legal notice, here is a blessing:
33275**
33276** May you do good and not evil.
33277** May you find forgiveness for yourself and forgive others.
33278** May you share freely, never taking more than you give.
33279**
33280*************************************************************************
33281** Utility functions used throughout sqlite.
33282**
33283** This file contains functions for allocating memory, comparing
33284** strings, and stuff like that.
33285**
33286*/
33287/* #include "sqliteInt.h" */
33288/* #include <stdarg.h> */
33289#ifndef SQLITE_OMIT_FLOATING_POINT
33290#include <math.h>
33291#endif
33292
33293/*
33294** Calls to sqlite3FaultSim() are used to simulate a failure during testing,
33295** or to bypass normal error detection during testing in order to let
33296** execute proceed futher downstream.
33297**
33298** In deployment, sqlite3FaultSim() *always* return SQLITE_OK (0). The
33299** sqlite3FaultSim() function only returns non-zero during testing.
33300**
33301** During testing, if the test harness has set a fault-sim callback using
33302** a call to sqlite3_test_control(SQLITE_TESTCTRL_FAULT_INSTALL), then
33303** each call to sqlite3FaultSim() is relayed to that application-supplied
33304** callback and the integer return value form the application-supplied
33305** callback is returned by sqlite3FaultSim().
33306**
33307** The integer argument to sqlite3FaultSim() is a code to identify which
33308** sqlite3FaultSim() instance is being invoked. Each call to sqlite3FaultSim()
33309** should have a unique code. To prevent legacy testing applications from
33310** breaking, the codes should not be changed or reused.
33311*/
33312#ifndef SQLITE_UNTESTABLE
33313SQLITE_PRIVATEstatic int sqlite3FaultSim(int iTest){
33314 int (*xCallback)(int) = sqlite3GlobalConfigsqlite3Config.xTestCallback;
33315 return xCallback ? xCallback(iTest) : SQLITE_OK0;
33316}
33317#endif
33318
33319#ifndef SQLITE_OMIT_FLOATING_POINT
33320/*
33321** Return true if the floating point value is Not a Number (NaN).
33322**
33323** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
33324** Otherwise, we have our own implementation that works on most systems.
33325*/
33326SQLITE_PRIVATEstatic int sqlite3IsNaN(double x){
33327 int rc; /* The value return */
33328#if !SQLITE_HAVE_ISNAN1 && !HAVE_ISNAN
33329 u64 y;
33330 memcpy(&y,&x,sizeof(y));
33331 rc = IsNaN(y)(((y)&(((u64)0x7ff)<<52))==(((u64)0x7ff)<<52)
&& ((y)&((((u64)1)<<52)-1))!=0)
;
33332#else
33333 rc = isnan(x)__builtin_isnan (x);
33334#endif /* HAVE_ISNAN */
33335 testcase( rc );
33336 return rc;
33337}
33338#endif /* SQLITE_OMIT_FLOATING_POINT */
33339
33340/*
33341** Compute a string length that is limited to what can be stored in
33342** lower 30 bits of a 32-bit signed integer.
33343**
33344** The value returned will never be negative. Nor will it ever be greater
33345** than the actual length of the string. For very long strings (greater
33346** than 1GiB) the value returned might be less than the true string length.
33347*/
33348SQLITE_PRIVATEstatic int sqlite3Strlen30(const char *z){
33349 if( z==0 ) return 0;
33350 return 0x3fffffff & (int)strlen(z);
33351}
33352
33353/*
33354** Return the declared type of a column. Or return zDflt if the column
33355** has no declared type.
33356**
33357** The column type is an extra string stored after the zero-terminator on
33358** the column name if and only if the COLFLAG_HASTYPE flag is set.
33359*/
33360SQLITE_PRIVATEstatic char *sqlite3ColumnType(Column *pCol, char *zDflt){
33361 if( pCol->colFlags & COLFLAG_HASTYPE0x0004 ){
33362 return pCol->zCnName + strlen(pCol->zCnName) + 1;
33363 }else if( pCol->eCType ){
33364 assert( pCol->eCType<=SQLITE_N_STDTYPE )((void) (0));
33365 return (char*)sqlite3StdType[pCol->eCType-1];
33366 }else{
33367 return zDflt;
33368 }
33369}
33370
33371/*
33372** Helper function for sqlite3Error() - called rarely. Broken out into
33373** a separate routine to avoid unnecessary register saves on entry to
33374** sqlite3Error().
33375*/
33376static SQLITE_NOINLINE__attribute__((noinline)) void sqlite3ErrorFinish(sqlite3 *db, int err_code){
33377 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
33378 sqlite3SystemError(db, err_code);
33379}
33380
33381/*
33382** Set the current error code to err_code and clear any prior error message.
33383** Also set iSysErrno (by calling sqlite3System) if the err_code indicates
33384** that would be appropriate.
33385*/
33386SQLITE_PRIVATEstatic void sqlite3Error(sqlite3 *db, int err_code){
33387 assert( db!=0 )((void) (0));
33388 db->errCode = err_code;
33389 if( err_code || db->pErr ){
33390 sqlite3ErrorFinish(db, err_code);
33391 }else{
33392 db->errByteOffset = -1;
33393 }
33394}
33395
33396/*
33397** The equivalent of sqlite3Error(db, SQLITE_OK). Clear the error state
33398** and error message.
33399*/
33400SQLITE_PRIVATEstatic void sqlite3ErrorClear(sqlite3 *db){
33401 assert( db!=0 )((void) (0));
33402 db->errCode = SQLITE_OK0;
33403 db->errByteOffset = -1;
33404 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
33405}
33406
33407/*
33408** Load the sqlite3.iSysErrno field if that is an appropriate thing
33409** to do based on the SQLite error code in rc.
33410*/
33411SQLITE_PRIVATEstatic void sqlite3SystemError(sqlite3 *db, int rc){
33412 if( rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ) return;
33413 rc &= 0xff;
33414 if( rc==SQLITE_CANTOPEN14 || rc==SQLITE_IOERR10 ){
33415 db->iSysErrno = sqlite3OsGetLastError(db->pVfs);
33416 }
33417}
33418
33419/*
33420** Set the most recent error code and error string for the sqlite
33421** handle "db". The error code is set to "err_code".
33422**
33423** If it is not NULL, string zFormat specifies the format of the
33424** error string. zFormat and any string tokens that follow it are
33425** assumed to be encoded in UTF-8.
33426**
33427** To clear the most recent error for sqlite handle "db", sqlite3Error
33428** should be called with err_code set to SQLITE_OK and zFormat set
33429** to NULL.
33430*/
33431SQLITE_PRIVATEstatic void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){
33432 assert( db!=0 )((void) (0));
33433 db->errCode = err_code;
33434 sqlite3SystemError(db, err_code);
33435 if( zFormat==0 ){
33436 sqlite3Error(db, err_code);
33437 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){
33438 char *z;
33439 va_list ap;
33440 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
33441 z = sqlite3VMPrintf(db, zFormat, ap);
33442 va_end(ap)__builtin_va_end(ap);
33443 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF81, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
33444 }
33445}
33446
33447/*
33448** Add an error message to pParse->zErrMsg and increment pParse->nErr.
33449**
33450** This function should be used to report any error that occurs while
33451** compiling an SQL statement (i.e. within sqlite3_prepare()). The
33452** last thing the sqlite3_prepare() function does is copy the error
33453** stored by this function into the database handle using sqlite3Error().
33454** Functions sqlite3Error() or sqlite3ErrorWithMsg() should be used
33455** during statement execution (sqlite3_step() etc.).
33456*/
33457SQLITE_PRIVATEstatic void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
33458 char *zMsg;
33459 va_list ap;
33460 sqlite3 *db = pParse->db;
33461 assert( db!=0 )((void) (0));
33462 assert( db->pParse==pParse )((void) (0));
33463 db->errByteOffset = -2;
33464 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
33465 zMsg = sqlite3VMPrintf(db, zFormat, ap);
33466 va_end(ap)__builtin_va_end(ap);
33467 if( db->errByteOffset<-1 ) db->errByteOffset = -1;
33468 if( db->suppressErr ){
33469 sqlite3DbFree(db, zMsg);
33470 if( db->mallocFailed ){
33471 pParse->nErr++;
33472 pParse->rc = SQLITE_NOMEM7;
33473 }
33474 }else{
33475 pParse->nErr++;
33476 sqlite3DbFree(db, pParse->zErrMsg);
33477 pParse->zErrMsg = zMsg;
33478 pParse->rc = SQLITE_ERROR1;
33479 pParse->pWith = 0;
33480 }
33481}
33482
33483/*
33484** If database connection db is currently parsing SQL, then transfer
33485** error code errCode to that parser if the parser has not already
33486** encountered some other kind of error.
33487*/
33488SQLITE_PRIVATEstatic int sqlite3ErrorToParser(sqlite3 *db, int errCode){
33489 Parse *pParse;
33490 if( db==0 || (pParse = db->pParse)==0 ) return errCode;
33491 pParse->rc = errCode;
33492 pParse->nErr++;
33493 return errCode;
33494}
33495
33496/*
33497** Convert an SQL-style quoted string into a normal string by removing
33498** the quote characters. The conversion is done in-place. If the
33499** input does not begin with a quote character, then this routine
33500** is a no-op.
33501**
33502** The input string must be zero-terminated. A new zero-terminator
33503** is added to the dequoted string.
33504**
33505** The return value is -1 if no dequoting occurs or the length of the
33506** dequoted string, exclusive of the zero terminator, if dequoting does
33507** occur.
33508**
33509** 2002-02-14: This routine is extended to remove MS-Access style
33510** brackets from around identifiers. For example: "[a-b-c]" becomes
33511** "a-b-c".
33512*/
33513SQLITE_PRIVATEstatic void sqlite3Dequote(char *z){
33514 char quote;
33515 int i, j;
33516 if( z==0 ) return;
33517 quote = z[0];
33518 if( !sqlite3Isquote(quote)(sqlite3CtypeMap[(unsigned char)(quote)]&0x80) ) return;
33519 if( quote=='[' ) quote = ']';
33520 for(i=1, j=0;; i++){
33521 assert( z[i] )((void) (0));
33522 if( z[i]==quote ){
33523 if( z[i+1]==quote ){
33524 z[j++] = quote;
33525 i++;
33526 }else{
33527 break;
33528 }
33529 }else{
33530 z[j++] = z[i];
33531 }
33532 }
33533 z[j] = 0;
33534}
33535SQLITE_PRIVATEstatic void sqlite3DequoteExpr(Expr *p){
33536 assert( !ExprHasProperty(p, EP_IntValue) )((void) (0));
33537 assert( sqlite3Isquote(p->u.zToken[0]) )((void) (0));
33538 p->flags |= p->u.zToken[0]=='"' ? EP_Quoted0x4000000|EP_DblQuoted0x000080 : EP_Quoted0x4000000;
33539 sqlite3Dequote(p->u.zToken);
33540}
33541
33542/*
33543** If the input token p is quoted, try to adjust the token to remove
33544** the quotes. This is not always possible:
33545**
33546** "abc" -> abc
33547** "ab""cd" -> (not possible because of the interior "")
33548**
33549** Remove the quotes if possible. This is a optimization. The overall
33550** system should still return the correct answer even if this routine
33551** is always a no-op.
33552*/
33553SQLITE_PRIVATEstatic void sqlite3DequoteToken(Token *p){
33554 unsigned int i;
33555 if( p->n<2 ) return;
33556 if( !sqlite3Isquote(p->z[0])(sqlite3CtypeMap[(unsigned char)(p->z[0])]&0x80) ) return;
33557 for(i=1; i<p->n-1; i++){
33558 if( sqlite3Isquote(p->z[i])(sqlite3CtypeMap[(unsigned char)(p->z[i])]&0x80) ) return;
33559 }
33560 p->n -= 2;
33561 p->z++;
33562}
33563
33564/*
33565** Generate a Token object from a string
33566*/
33567SQLITE_PRIVATEstatic void sqlite3TokenInit(Token *p, char *z){
33568 p->z = z;
33569 p->n = sqlite3Strlen30(z);
33570}
33571
33572/* Convenient short-hand */
33573#define UpperToLowersqlite3UpperToLower sqlite3UpperToLower
33574
33575/*
33576** Some systems have stricmp(). Others have strcasecmp(). Because
33577** there is no consistency, we will define our own.
33578**
33579** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
33580** sqlite3_strnicmp() APIs allow applications and extensions to compare
33581** the contents of two buffers containing UTF-8 strings in a
33582** case-independent fashion, using the same definition of "case
33583** independence" that SQLite uses internally when comparing identifiers.
33584*/
33585SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){
33586 if( zLeft==0 ){
33587 return zRight ? -1 : 0;
33588 }else if( zRight==0 ){
33589 return 1;
33590 }
33591 return sqlite3StrICmp(zLeft, zRight);
33592}
33593SQLITE_PRIVATEstatic int sqlite3StrICmp(const char *zLeft, const char *zRight){
33594 unsigned char *a, *b;
33595 int c, x;
33596 a = (unsigned char *)zLeft;
33597 b = (unsigned char *)zRight;
33598 for(;;){
33599 c = *a;
33600 x = *b;
33601 if( c==x ){
33602 if( c==0 ) break;
33603 }else{
33604 c = (int)UpperToLowersqlite3UpperToLower[c] - (int)UpperToLowersqlite3UpperToLower[x];
33605 if( c ) break;
33606 }
33607 a++;
33608 b++;
33609 }
33610 return c;
33611}
33612SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
33613 register unsigned char *a, *b;
33614 if( zLeft==0 ){
33615 return zRight ? -1 : 0;
33616 }else if( zRight==0 ){
33617 return 1;
33618 }
33619 a = (unsigned char *)zLeft;
33620 b = (unsigned char *)zRight;
33621 while( N-- > 0 && *a!=0 && UpperToLowersqlite3UpperToLower[*a]==UpperToLowersqlite3UpperToLower[*b]){ a++; b++; }
33622 return N<0 ? 0 : UpperToLowersqlite3UpperToLower[*a] - UpperToLowersqlite3UpperToLower[*b];
33623}
33624
33625/*
33626** Compute an 8-bit hash on a string that is insensitive to case differences
33627*/
33628SQLITE_PRIVATEstatic u8 sqlite3StrIHash(const char *z){
33629 u8 h = 0;
33630 if( z==0 ) return 0;
33631 while( z[0] ){
33632 h += UpperToLowersqlite3UpperToLower[(unsigned char)z[0]];
33633 z++;
33634 }
33635 return h;
33636}
33637
33638/*
33639** Compute 10 to the E-th power. Examples: E==1 results in 10.
33640** E==2 results in 100. E==50 results in 1.0e50.
33641**
33642** This routine only works for values of E between 1 and 341.
33643*/
33644static LONGDOUBLE_TYPElong double sqlite3Pow10(int E){
33645#if defined(_MSC_VER)
33646 static const LONGDOUBLE_TYPElong double x[] = {
33647 1.0e+001L,
33648 1.0e+002L,
33649 1.0e+004L,
33650 1.0e+008L,
33651 1.0e+016L,
33652 1.0e+032L,
33653 1.0e+064L,
33654 1.0e+128L,
33655 1.0e+256L
33656 };
33657 LONGDOUBLE_TYPElong double r = 1.0;
33658 int i;
33659 assert( E>=0 && E<=307 )((void) (0));
33660 for(i=0; E!=0; i++, E >>=1){
33661 if( E & 1 ) r *= x[i];
33662 }
33663 return r;
33664#else
33665 LONGDOUBLE_TYPElong double x = 10.0;
33666 LONGDOUBLE_TYPElong double r = 1.0;
33667 while(1){
33668 if( E & 1 ) r *= x;
33669 E >>= 1;
33670 if( E==0 ) break;
33671 x *= x;
33672 }
33673 return r;
33674#endif
33675}
33676
33677/*
33678** The string z[] is an text representation of a real number.
33679** Convert this string to a double and write it into *pResult.
33680**
33681** The string z[] is length bytes in length (bytes, not characters) and
33682** uses the encoding enc. The string is not necessarily zero-terminated.
33683**
33684** Return TRUE if the result is a valid real number (or integer) and FALSE
33685** if the string is empty or contains extraneous text. More specifically
33686** return
33687** 1 => The input string is a pure integer
33688** 2 or more => The input has a decimal point or eNNN clause
33689** 0 or less => The input string is not a valid number
33690** -1 => Not a valid number, but has a valid prefix which
33691** includes a decimal point and/or an eNNN clause
33692**
33693** Valid numbers are in one of these formats:
33694**
33695** [+-]digits[E[+-]digits]
33696** [+-]digits.[digits][E[+-]digits]
33697** [+-].digits[E[+-]digits]
33698**
33699** Leading and trailing whitespace is ignored for the purpose of determining
33700** validity.
33701**
33702** If some prefix of the input string is a valid number, this routine
33703** returns FALSE but it still converts the prefix and writes the result
33704** into *pResult.
33705*/
33706#if defined(_MSC_VER)
33707#pragma warning(disable : 4756)
33708#endif
33709SQLITE_PRIVATEstatic int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
33710#ifndef SQLITE_OMIT_FLOATING_POINT
33711 int incr;
33712 const char *zEnd;
33713 /* sign * significand * (10 ^ (esign * exponent)) */
33714 int sign = 1; /* sign of significand */
33715 i64 s = 0; /* significand */
33716 int d = 0; /* adjust exponent for shifting decimal point */
33717 int esign = 1; /* sign of exponent */
33718 int e = 0; /* exponent */
33719 int eValid = 1; /* True exponent is either not used or is well-formed */
33720 double result;
33721 int nDigit = 0; /* Number of digits processed */
33722 int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */
33723
33724 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE )((void) (0));
33725 *pResult = 0.0; /* Default return value, in case of an error */
33726 if( length==0 ) return 0;
33727
33728 if( enc==SQLITE_UTF81 ){
33729 incr = 1;
33730 zEnd = z + length;
33731 }else{
33732 int i;
33733 incr = 2;
33734 length &= ~1;
33735 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 )((void) (0));
33736 testcase( enc==SQLITE_UTF16LE );
33737 testcase( enc==SQLITE_UTF16BE );
33738 for(i=3-enc; i<length && z[i]==0; i+=2){}
33739 if( i<length ) eType = -100;
33740 zEnd = &z[i^1];
33741 z += (enc&1);
33742 }
33743
33744 /* skip leading spaces */
33745 while( z<zEnd && sqlite3Isspace(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x01) ) z+=incr;
33746 if( z>=zEnd ) return 0;
33747
33748 /* get sign of significand */
33749 if( *z=='-' ){
33750 sign = -1;
33751 z+=incr;
33752 }else if( *z=='+' ){
33753 z+=incr;
33754 }
33755
33756 /* copy max significant digits to significand */
33757 while( z<zEnd && sqlite3Isdigit(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x04) ){
33758 s = s*10 + (*z - '0');
33759 z+=incr; nDigit++;
33760 if( s>=((LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32))-9)/10) ){
33761 /* skip non-significant significand digits
33762 ** (increase exponent by d to shift decimal left) */
33763 while( z<zEnd && sqlite3Isdigit(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x04) ){ z+=incr; d++; }
33764 }
33765 }
33766 if( z>=zEnd ) goto do_atof_calc;
33767
33768 /* if decimal point is present */
33769 if( *z=='.' ){
33770 z+=incr;
33771 eType++;
33772 /* copy digits from after decimal to significand
33773 ** (decrease exponent by d to shift decimal right) */
33774 while( z<zEnd && sqlite3Isdigit(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x04) ){
33775 if( s<((LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32))-9)/10) ){
33776 s = s*10 + (*z - '0');
33777 d--;
33778 nDigit++;
33779 }
33780 z+=incr;
33781 }
33782 }
33783 if( z>=zEnd ) goto do_atof_calc;
33784
33785 /* if exponent is present */
33786 if( *z=='e' || *z=='E' ){
33787 z+=incr;
33788 eValid = 0;
33789 eType++;
33790
33791 /* This branch is needed to avoid a (harmless) buffer overread. The
33792 ** special comment alerts the mutation tester that the correct answer
33793 ** is obtained even if the branch is omitted */
33794 if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/
33795
33796 /* get sign of exponent */
33797 if( *z=='-' ){
33798 esign = -1;
33799 z+=incr;
33800 }else if( *z=='+' ){
33801 z+=incr;
33802 }
33803 /* copy digits to exponent */
33804 while( z<zEnd && sqlite3Isdigit(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x04) ){
33805 e = e<10000 ? (e*10 + (*z - '0')) : 10000;
33806 z+=incr;
33807 eValid = 1;
33808 }
33809 }
33810
33811 /* skip trailing spaces */
33812 while( z<zEnd && sqlite3Isspace(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x01) ) z+=incr;
33813
33814do_atof_calc:
33815 /* adjust exponent by d, and update sign */
33816 e = (e*esign) + d;
33817 if( e<0 ) {
33818 esign = -1;
33819 e *= -1;
33820 } else {
33821 esign = 1;
33822 }
33823
33824 if( s==0 ) {
33825 /* In the IEEE 754 standard, zero is signed. */
33826 result = sign<0 ? -(double)0 : (double)0;
33827 } else {
33828 /* Attempt to reduce exponent.
33829 **
33830 ** Branches that are not required for the correct answer but which only
33831 ** help to obtain the correct answer faster are marked with special
33832 ** comments, as a hint to the mutation tester.
33833 */
33834 while( e>0 ){ /*OPTIMIZATION-IF-TRUE*/
33835 if( esign>0 ){
33836 if( s>=(LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32))/10) ) break; /*OPTIMIZATION-IF-FALSE*/
33837 s *= 10;
33838 }else{
33839 if( s%10!=0 ) break; /*OPTIMIZATION-IF-FALSE*/
33840 s /= 10;
33841 }
33842 e--;
33843 }
33844
33845 /* adjust the sign of significand */
33846 s = sign<0 ? -s : s;
33847
33848 if( e==0 ){ /*OPTIMIZATION-IF-TRUE*/
33849 result = (double)s;
33850 }else{
33851 /* attempt to handle extremely small/large numbers better */
33852 if( e>307 ){ /*OPTIMIZATION-IF-TRUE*/
33853 if( e<342 ){ /*OPTIMIZATION-IF-TRUE*/
33854 LONGDOUBLE_TYPElong double scale = sqlite3Pow10(e-308);
33855 if( esign<0 ){
33856 result = s / scale;
33857 result /= 1.0e+308;
33858 }else{
33859 result = s * scale;
33860 result *= 1.0e+308;
33861 }
33862 }else{ assert( e>=342 )((void) (0));
33863 if( esign<0 ){
33864 result = 0.0*s;
33865 }else{
33866#ifdef INFINITY(__builtin_inff ())
33867 result = INFINITY(__builtin_inff ())*s;
33868#else
33869 result = 1e308*1e308*s; /* Infinity */
33870#endif
33871 }
33872 }
33873 }else{
33874 LONGDOUBLE_TYPElong double scale = sqlite3Pow10(e);
33875 if( esign<0 ){
33876 result = s / scale;
33877 }else{
33878 result = s * scale;
33879 }
33880 }
33881 }
33882 }
33883
33884 /* store the result */
33885 *pResult = result;
33886
33887 /* return true if number and no extra non-whitespace chracters after */
33888 if( z==zEnd && nDigit>0 && eValid && eType>0 ){
33889 return eType;
33890 }else if( eType>=2 && (eType==3 || eValid) && nDigit>0 ){
33891 return -1;
33892 }else{
33893 return 0;
33894 }
33895#else
33896 return !sqlite3Atoi64(z, pResult, length, enc);
33897#endif /* SQLITE_OMIT_FLOATING_POINT */
33898}
33899#if defined(_MSC_VER)
33900#pragma warning(default : 4756)
33901#endif
33902
33903/*
33904** Render an signed 64-bit integer as text. Store the result in zOut[].
33905**
33906** The caller must ensure that zOut[] is at least 21 bytes in size.
33907*/
33908SQLITE_PRIVATEstatic void sqlite3Int64ToText(i64 v, char *zOut){
33909 int i;
33910 u64 x;
33911 char zTemp[22];
33912 if( v<0 ){
33913 x = (v==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32)))) ? ((u64)1)<<63 : (u64)-v;
33914 }else{
33915 x = v;
33916 }
33917 i = sizeof(zTemp)-2;
33918 zTemp[sizeof(zTemp)-1] = 0;
33919 do{
33920 zTemp[i--] = (x%10) + '0';
33921 x = x/10;
33922 }while( x );
33923 if( v<0 ) zTemp[i--] = '-';
33924 memcpy(zOut, &zTemp[i+1], sizeof(zTemp)-1-i);
33925}
33926
33927/*
33928** Compare the 19-character string zNum against the text representation
33929** value 2^63: 9223372036854775808. Return negative, zero, or positive
33930** if zNum is less than, equal to, or greater than the string.
33931** Note that zNum must contain exactly 19 characters.
33932**
33933** Unlike memcmp() this routine is guaranteed to return the difference
33934** in the values of the last digit if the only difference is in the
33935** last digit. So, for example,
33936**
33937** compare2pow63("9223372036854775800", 1)
33938**
33939** will return -8.
33940*/
33941static int compare2pow63(const char *zNum, int incr){
33942 int c = 0;
33943 int i;
33944 /* 012345678901234567 */
33945 const char *pow63 = "922337203685477580";
33946 for(i=0; c==0 && i<18; i++){
33947 c = (zNum[i*incr]-pow63[i])*10;
33948 }
33949 if( c==0 ){
33950 c = zNum[18*incr] - '8';
33951 testcase( c==(-1) );
33952 testcase( c==0 );
33953 testcase( c==(+1) );
33954 }
33955 return c;
33956}
33957
33958/*
33959** Convert zNum to a 64-bit signed integer. zNum must be decimal. This
33960** routine does *not* accept hexadecimal notation.
33961**
33962** Returns:
33963**
33964** -1 Not even a prefix of the input text looks like an integer
33965** 0 Successful transformation. Fits in a 64-bit signed integer.
33966** 1 Excess non-space text after the integer value
33967** 2 Integer too large for a 64-bit signed integer or is malformed
33968** 3 Special case of 9223372036854775808
33969**
33970** length is the number of bytes in the string (bytes, not characters).
33971** The string is not necessarily zero-terminated. The encoding is
33972** given by enc.
33973*/
33974SQLITE_PRIVATEstatic int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
33975 int incr;
33976 u64 u = 0;
33977 int neg = 0; /* assume positive */
33978 int i;
33979 int c = 0;
33980 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */
33981 int rc; /* Baseline return code */
33982 const char *zStart;
33983 const char *zEnd = zNum + length;
33984 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE )((void) (0));
33985 if( enc==SQLITE_UTF81 ){
33986 incr = 1;
33987 }else{
33988 incr = 2;
33989 length &= ~1;
33990 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 )((void) (0));
33991 for(i=3-enc; i<length && zNum[i]==0; i+=2){}
33992 nonNum = i<length;
33993 zEnd = &zNum[i^1];
33994 zNum += (enc&1);
33995 }
33996 while( zNum<zEnd && sqlite3Isspace(*zNum)(sqlite3CtypeMap[(unsigned char)(*zNum)]&0x01) ) zNum+=incr;
33997 if( zNum<zEnd ){
33998 if( *zNum=='-' ){
33999 neg = 1;
34000 zNum+=incr;
34001 }else if( *zNum=='+' ){
34002 zNum+=incr;
34003 }
34004 }
34005 zStart = zNum;
34006 while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
34007 for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
34008 u = u*10 + c - '0';
34009 }
34010 testcase( i==18*incr );
34011 testcase( i==19*incr );
34012 testcase( i==20*incr );
34013 if( u>LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) ){
34014 /* This test and assignment is needed only to suppress UB warnings
34015 ** from clang and -fsanitize=undefined. This test and assignment make
34016 ** the code a little larger and slower, and no harm comes from omitting
34017 ** them, but we must appaise the undefined-behavior pharisees. */
34018 *pNum = neg ? SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) : LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
34019 }else if( neg ){
34020 *pNum = -(i64)u;
34021 }else{
34022 *pNum = (i64)u;
34023 }
34024 rc = 0;
34025 if( i==0 && zStart==zNum ){ /* No digits */
34026 rc = -1;
34027 }else if( nonNum ){ /* UTF16 with high-order bytes non-zero */
34028 rc = 1;
34029 }else if( &zNum[i]<zEnd ){ /* Extra bytes at the end */
34030 int jj = i;
34031 do{
34032 if( !sqlite3Isspace(zNum[jj])(sqlite3CtypeMap[(unsigned char)(zNum[jj])]&0x01) ){
34033 rc = 1; /* Extra non-space text after the integer */
34034 break;
34035 }
34036 jj += incr;
34037 }while( &zNum[jj]<zEnd );
34038 }
34039 if( i<19*incr ){
34040 /* Less than 19 digits, so we know that it fits in 64 bits */
34041 assert( u<=LARGEST_INT64 )((void) (0));
34042 return rc;
34043 }else{
34044 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */
34045 c = i>19*incr ? 1 : compare2pow63(zNum, incr);
34046 if( c<0 ){
34047 /* zNum is less than 9223372036854775808 so it fits */
34048 assert( u<=LARGEST_INT64 )((void) (0));
34049 return rc;
34050 }else{
34051 *pNum = neg ? SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) : LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
34052 if( c>0 ){
34053 /* zNum is greater than 9223372036854775808 so it overflows */
34054 return 2;
34055 }else{
34056 /* zNum is exactly 9223372036854775808. Fits if negative. The
34057 ** special case 2 overflow if positive */
34058 assert( u-1==LARGEST_INT64 )((void) (0));
34059 return neg ? rc : 3;
34060 }
34061 }
34062 }
34063}
34064
34065/*
34066** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
34067** into a 64-bit signed integer. This routine accepts hexadecimal literals,
34068** whereas sqlite3Atoi64() does not.
34069**
34070** Returns:
34071**
34072** 0 Successful transformation. Fits in a 64-bit signed integer.
34073** 1 Excess text after the integer value
34074** 2 Integer too large for a 64-bit signed integer or is malformed
34075** 3 Special case of 9223372036854775808
34076*/
34077SQLITE_PRIVATEstatic int sqlite3DecOrHexToI64(const char *z, i64 *pOut){
34078#ifndef SQLITE_OMIT_HEX_INTEGER
34079 if( z[0]=='0'
34080 && (z[1]=='x' || z[1]=='X')
34081 ){
34082 u64 u = 0;
34083 int i, k;
34084 for(i=2; z[i]=='0'; i++){}
34085 for(k=i; sqlite3Isxdigit(z[k])(sqlite3CtypeMap[(unsigned char)(z[k])]&0x08); k++){
34086 u = u*16 + sqlite3HexToInt(z[k]);
34087 }
34088 memcpy(pOut, &u, 8);
34089 return (z[k]==0 && k-i<=16) ? 0 : 2;
34090 }else
34091#endif /* SQLITE_OMIT_HEX_INTEGER */
34092 {
34093 return sqlite3Atoi64(z, pOut, sqlite3Strlen30(z), SQLITE_UTF81);
34094 }
34095}
34096
34097/*
34098** If zNum represents an integer that will fit in 32-bits, then set
34099** *pValue to that integer and return true. Otherwise return false.
34100**
34101** This routine accepts both decimal and hexadecimal notation for integers.
34102**
34103** Any non-numeric characters that following zNum are ignored.
34104** This is different from sqlite3Atoi64() which requires the
34105** input number to be zero-terminated.
34106*/
34107SQLITE_PRIVATEstatic int sqlite3GetInt32(const char *zNum, int *pValue){
34108 sqlite_int64 v = 0;
34109 int i, c;
34110 int neg = 0;
34111 if( zNum[0]=='-' ){
34112 neg = 1;
34113 zNum++;
34114 }else if( zNum[0]=='+' ){
34115 zNum++;
34116 }
34117#ifndef SQLITE_OMIT_HEX_INTEGER
34118 else if( zNum[0]=='0'
34119 && (zNum[1]=='x' || zNum[1]=='X')
34120 && sqlite3Isxdigit(zNum[2])(sqlite3CtypeMap[(unsigned char)(zNum[2])]&0x08)
34121 ){
34122 u32 u = 0;
34123 zNum += 2;
34124 while( zNum[0]=='0' ) zNum++;
34125 for(i=0; sqlite3Isxdigit(zNum[i])(sqlite3CtypeMap[(unsigned char)(zNum[i])]&0x08) && i<8; i++){
34126 u = u*16 + sqlite3HexToInt(zNum[i]);
34127 }
34128 if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])(sqlite3CtypeMap[(unsigned char)(zNum[i])]&0x08)==0 ){
34129 memcpy(pValue, &u, 4);
34130 return 1;
34131 }else{
34132 return 0;
34133 }
34134 }
34135#endif
34136 if( !sqlite3Isdigit(zNum[0])(sqlite3CtypeMap[(unsigned char)(zNum[0])]&0x04) ) return 0;
34137 while( zNum[0]=='0' ) zNum++;
34138 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
34139 v = v*10 + c;
34140 }
34141
34142 /* The longest decimal representation of a 32 bit integer is 10 digits:
34143 **
34144 ** 1234567890
34145 ** 2^31 -> 2147483648
34146 */
34147 testcase( i==10 );
34148 if( i>10 ){
34149 return 0;
34150 }
34151 testcase( v-neg==2147483647 );
34152 if( v-neg>2147483647 ){
34153 return 0;
34154 }
34155 if( neg ){
34156 v = -v;
34157 }
34158 *pValue = (int)v;
34159 return 1;
34160}
34161
34162/*
34163** Return a 32-bit integer value extracted from a string. If the
34164** string is not an integer, just return 0.
34165*/
34166SQLITE_PRIVATEstatic int sqlite3Atoi(const char *z){
34167 int x = 0;
34168 sqlite3GetInt32(z, &x);
34169 return x;
34170}
34171
34172/*
34173** Try to convert z into an unsigned 32-bit integer. Return true on
34174** success and false if there is an error.
34175**
34176** Only decimal notation is accepted.
34177*/
34178SQLITE_PRIVATEstatic int sqlite3GetUInt32(const char *z, u32 *pI){
34179 u64 v = 0;
34180 int i;
34181 for(i=0; sqlite3Isdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x04); i++){
34182 v = v*10 + z[i] - '0';
34183 if( v>4294967296LL ){ *pI = 0; return 0; }
34184 }
34185 if( i==0 || z[i]!=0 ){ *pI = 0; return 0; }
34186 *pI = (u32)v;
34187 return 1;
34188}
34189
34190/*
34191** The variable-length integer encoding is as follows:
34192**
34193** KEY:
34194** A = 0xxxxxxx 7 bits of data and one flag bit
34195** B = 1xxxxxxx 7 bits of data and one flag bit
34196** C = xxxxxxxx 8 bits of data
34197**
34198** 7 bits - A
34199** 14 bits - BA
34200** 21 bits - BBA
34201** 28 bits - BBBA
34202** 35 bits - BBBBA
34203** 42 bits - BBBBBA
34204** 49 bits - BBBBBBA
34205** 56 bits - BBBBBBBA
34206** 64 bits - BBBBBBBBC
34207*/
34208
34209/*
34210** Write a 64-bit variable-length integer to memory starting at p[0].
34211** The length of data write will be between 1 and 9 bytes. The number
34212** of bytes written is returned.
34213**
34214** A variable-length integer consists of the lower 7 bits of each byte
34215** for all bytes that have the 8th bit set and one byte with the 8th
34216** bit clear. Except, if we get to the 9th byte, it stores the full
34217** 8 bits and is the last byte.
34218*/
34219static int SQLITE_NOINLINE__attribute__((noinline)) putVarint64(unsigned char *p, u64 v){
34220 int i, j, n;
34221 u8 buf[10];
34222 if( v & (((u64)0xff000000)<<32) ){
34223 p[8] = (u8)v;
34224 v >>= 8;
34225 for(i=7; i>=0; i--){
34226 p[i] = (u8)((v & 0x7f) | 0x80);
34227 v >>= 7;
34228 }
34229 return 9;
34230 }
34231 n = 0;
34232 do{
34233 buf[n++] = (u8)((v & 0x7f) | 0x80);
34234 v >>= 7;
34235 }while( v!=0 );
34236 buf[0] &= 0x7f;
34237 assert( n<=9 )((void) (0));
34238 for(i=0, j=n-1; j>=0; j--, i++){
34239 p[i] = buf[j];
34240 }
34241 return n;
34242}
34243SQLITE_PRIVATEstatic int sqlite3PutVarint(unsigned char *p, u64 v){
34244 if( v<=0x7f ){
34245 p[0] = v&0x7f;
34246 return 1;
34247 }
34248 if( v<=0x3fff ){
34249 p[0] = ((v>>7)&0x7f)|0x80;
34250 p[1] = v&0x7f;
34251 return 2;
34252 }
34253 return putVarint64(p,v);
34254}
34255
34256/*
34257** Bitmasks used by sqlite3GetVarint(). These precomputed constants
34258** are defined here rather than simply putting the constant expressions
34259** inline in order to work around bugs in the RVT compiler.
34260**
34261** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
34262**
34263** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
34264*/
34265#define SLOT_2_00x001fc07f 0x001fc07f
34266#define SLOT_4_2_00xf01fc07f 0xf01fc07f
34267
34268
34269/*
34270** Read a 64-bit variable-length integer from memory starting at p[0].
34271** Return the number of bytes read. The value is stored in *v.
34272*/
34273SQLITE_PRIVATEstatic u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
34274 u32 a,b,s;
34275
34276 if( ((signed char*)p)[0]>=0 ){
34277 *v = *p;
34278 return 1;
34279 }
34280 if( ((signed char*)p)[1]>=0 ){
34281 *v = ((u32)(p[0]&0x7f)<<7) | p[1];
34282 return 2;
34283 }
34284
34285 /* Verify that constants are precomputed correctly */
34286 assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) )((void) (0));
34287 assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) )((void) (0));
34288
34289 a = ((u32)p[0])<<14;
34290 b = p[1];
34291 p += 2;
34292 a |= *p;
34293 /* a: p0<<14 | p2 (unmasked) */
34294 if (!(a&0x80))
34295 {
34296 a &= SLOT_2_00x001fc07f;
34297 b &= 0x7f;
34298 b = b<<7;
34299 a |= b;
34300 *v = a;
34301 return 3;
34302 }
34303
34304 /* CSE1 from below */
34305 a &= SLOT_2_00x001fc07f;
34306 p++;
34307 b = b<<14;
34308 b |= *p;
34309 /* b: p1<<14 | p3 (unmasked) */
34310 if (!(b&0x80))
34311 {
34312 b &= SLOT_2_00x001fc07f;
34313 /* moved CSE1 up */
34314 /* a &= (0x7f<<14)|(0x7f); */
34315 a = a<<7;
34316 a |= b;
34317 *v = a;
34318 return 4;
34319 }
34320
34321 /* a: p0<<14 | p2 (masked) */
34322 /* b: p1<<14 | p3 (unmasked) */
34323 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
34324 /* moved CSE1 up */
34325 /* a &= (0x7f<<14)|(0x7f); */
34326 b &= SLOT_2_00x001fc07f;
34327 s = a;
34328 /* s: p0<<14 | p2 (masked) */
34329
34330 p++;
34331 a = a<<14;
34332 a |= *p;
34333 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
34334 if (!(a&0x80))
34335 {
34336 /* we can skip these cause they were (effectively) done above
34337 ** while calculating s */
34338 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
34339 /* b &= (0x7f<<14)|(0x7f); */
34340 b = b<<7;
34341 a |= b;
34342 s = s>>18;
34343 *v = ((u64)s)<<32 | a;
34344 return 5;
34345 }
34346
34347 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
34348 s = s<<7;
34349 s |= b;
34350 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
34351
34352 p++;
34353 b = b<<14;
34354 b |= *p;
34355 /* b: p1<<28 | p3<<14 | p5 (unmasked) */
34356 if (!(b&0x80))
34357 {
34358 /* we can skip this cause it was (effectively) done above in calc'ing s */
34359 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
34360 a &= SLOT_2_00x001fc07f;
34361 a = a<<7;
34362 a |= b;
34363 s = s>>18;
34364 *v = ((u64)s)<<32 | a;
34365 return 6;
34366 }
34367
34368 p++;
34369 a = a<<14;
34370 a |= *p;
34371 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
34372 if (!(a&0x80))
34373 {
34374 a &= SLOT_4_2_00xf01fc07f;
34375 b &= SLOT_2_00x001fc07f;
34376 b = b<<7;
34377 a |= b;
34378 s = s>>11;
34379 *v = ((u64)s)<<32 | a;
34380 return 7;
34381 }
34382
34383 /* CSE2 from below */
34384 a &= SLOT_2_00x001fc07f;
34385 p++;
34386 b = b<<14;
34387 b |= *p;
34388 /* b: p3<<28 | p5<<14 | p7 (unmasked) */
34389 if (!(b&0x80))
34390 {
34391 b &= SLOT_4_2_00xf01fc07f;
34392 /* moved CSE2 up */
34393 /* a &= (0x7f<<14)|(0x7f); */
34394 a = a<<7;
34395 a |= b;
34396 s = s>>4;
34397 *v = ((u64)s)<<32 | a;
34398 return 8;
34399 }
34400
34401 p++;
34402 a = a<<15;
34403 a |= *p;
34404 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
34405
34406 /* moved CSE2 up */
34407 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
34408 b &= SLOT_2_00x001fc07f;
34409 b = b<<8;
34410 a |= b;
34411
34412 s = s<<4;
34413 b = p[-4];
34414 b &= 0x7f;
34415 b = b>>3;
34416 s |= b;
34417
34418 *v = ((u64)s)<<32 | a;
34419
34420 return 9;
34421}
34422
34423/*
34424** Read a 32-bit variable-length integer from memory starting at p[0].
34425** Return the number of bytes read. The value is stored in *v.
34426**
34427** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
34428** integer, then set *v to 0xffffffff.
34429**
34430** A MACRO version, getVarint32, is provided which inlines the
34431** single-byte case. All code should use the MACRO version as
34432** this function assumes the single-byte case has already been handled.
34433*/
34434SQLITE_PRIVATEstatic u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
34435 u32 a,b;
34436
34437 /* The 1-byte case. Overwhelmingly the most common. Handled inline
34438 ** by the getVarin32() macro */
34439 a = *p;
34440 /* a: p0 (unmasked) */
34441#ifndef getVarint32
34442 if (!(a&0x80))
34443 {
34444 /* Values between 0 and 127 */
34445 *v = a;
34446 return 1;
34447 }
34448#endif
34449
34450 /* The 2-byte case */
34451 p++;
34452 b = *p;
34453 /* b: p1 (unmasked) */
34454 if (!(b&0x80))
34455 {
34456 /* Values between 128 and 16383 */
34457 a &= 0x7f;
34458 a = a<<7;
34459 *v = a | b;
34460 return 2;
34461 }
34462
34463 /* The 3-byte case */
34464 p++;
34465 a = a<<14;
34466 a |= *p;
34467 /* a: p0<<14 | p2 (unmasked) */
34468 if (!(a&0x80))
34469 {
34470 /* Values between 16384 and 2097151 */
34471 a &= (0x7f<<14)|(0x7f);
34472 b &= 0x7f;
34473 b = b<<7;
34474 *v = a | b;
34475 return 3;
34476 }
34477
34478 /* A 32-bit varint is used to store size information in btrees.
34479 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
34480 ** A 3-byte varint is sufficient, for example, to record the size
34481 ** of a 1048569-byte BLOB or string.
34482 **
34483 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
34484 ** rare larger cases can be handled by the slower 64-bit varint
34485 ** routine.
34486 */
34487#if 1
34488 {
34489 u64 v64;
34490 u8 n;
34491
34492 n = sqlite3GetVarint(p-2, &v64);
34493 assert( n>3 && n<=9 )((void) (0));
34494 if( (v64 & SQLITE_MAX_U32((((u64)1)<<32)-1))!=v64 ){
34495 *v = 0xffffffff;
34496 }else{
34497 *v = (u32)v64;
34498 }
34499 return n;
34500 }
34501
34502#else
34503 /* For following code (kept for historical record only) shows an
34504 ** unrolling for the 3- and 4-byte varint cases. This code is
34505 ** slightly faster, but it is also larger and much harder to test.
34506 */
34507 p++;
34508 b = b<<14;
34509 b |= *p;
34510 /* b: p1<<14 | p3 (unmasked) */
34511 if (!(b&0x80))
34512 {
34513 /* Values between 2097152 and 268435455 */
34514 b &= (0x7f<<14)|(0x7f);
34515 a &= (0x7f<<14)|(0x7f);
34516 a = a<<7;
34517 *v = a | b;
34518 return 4;
34519 }
34520
34521 p++;
34522 a = a<<14;
34523 a |= *p;
34524 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
34525 if (!(a&0x80))
34526 {
34527 /* Values between 268435456 and 34359738367 */
34528 a &= SLOT_4_2_00xf01fc07f;
34529 b &= SLOT_4_2_00xf01fc07f;
34530 b = b<<7;
34531 *v = a | b;
34532 return 5;
34533 }
34534
34535 /* We can only reach this point when reading a corrupt database
34536 ** file. In that case we are not in any hurry. Use the (relatively
34537 ** slow) general-purpose sqlite3GetVarint() routine to extract the
34538 ** value. */
34539 {
34540 u64 v64;
34541 u8 n;
34542
34543 p -= 4;
34544 n = sqlite3GetVarint(p, &v64);
34545 assert( n>5 && n<=9 )((void) (0));
34546 *v = (u32)v64;
34547 return n;
34548 }
34549#endif
34550}
34551
34552/*
34553** Return the number of bytes that will be needed to store the given
34554** 64-bit integer.
34555*/
34556SQLITE_PRIVATEstatic int sqlite3VarintLen(u64 v){
34557 int i;
34558 for(i=1; (v >>= 7)!=0; i++){ assert( i<10 )((void) (0)); }
34559 return i;
34560}
34561
34562
34563/*
34564** Read or write a four-byte big-endian integer value.
34565*/
34566SQLITE_PRIVATEstatic u32 sqlite3Get4byte(const u8 *p){
34567#if SQLITE_BYTEORDER1234==4321
34568 u32 x;
34569 memcpy(&x,p,4);
34570 return x;
34571#elif SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
34572 u32 x;
34573 memcpy(&x,p,4);
34574 return __builtin_bswap32(x);
34575#elif SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
34576 u32 x;
34577 memcpy(&x,p,4);
34578 return _byteswap_ulong(x);
34579#else
34580 testcase( p[0]&0x80 );
34581 return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
34582#endif
34583}
34584SQLITE_PRIVATEstatic void sqlite3Put4byte(unsigned char *p, u32 v){
34585#if SQLITE_BYTEORDER1234==4321
34586 memcpy(p,&v,4);
34587#elif SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
34588 u32 x = __builtin_bswap32(v);
34589 memcpy(p,&x,4);
34590#elif SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
34591 u32 x = _byteswap_ulong(v);
34592 memcpy(p,&x,4);
34593#else
34594 p[0] = (u8)(v>>24);
34595 p[1] = (u8)(v>>16);
34596 p[2] = (u8)(v>>8);
34597 p[3] = (u8)v;
34598#endif
34599}
34600
34601
34602
34603/*
34604** Translate a single byte of Hex into an integer.
34605** This routine only works if h really is a valid hexadecimal
34606** character: 0..9a..fA..F
34607*/
34608SQLITE_PRIVATEstatic u8 sqlite3HexToInt(int h){
34609 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') )((void) (0));
34610#ifdef SQLITE_ASCII1
34611 h += 9*(1&(h>>6));
34612#endif
34613#ifdef SQLITE_EBCDIC
34614 h += 9*(1&~(h>>4));
34615#endif
34616 return (u8)(h & 0xf);
34617}
34618
34619#if !defined(SQLITE_OMIT_BLOB_LITERAL)
34620/*
34621** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
34622** value. Return a pointer to its binary value. Space to hold the
34623** binary value has been obtained from malloc and must be freed by
34624** the calling routine.
34625*/
34626SQLITE_PRIVATEstatic void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
34627 char *zBlob;
34628 int i;
34629
34630 zBlob = (char *)sqlite3DbMallocRawNN(db, n/2 + 1);
34631 n--;
34632 if( zBlob ){
34633 for(i=0; i<n; i+=2){
34634 zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
34635 }
34636 zBlob[i/2] = 0;
34637 }
34638 return zBlob;
34639}
34640#endif /* !SQLITE_OMIT_BLOB_LITERAL */
34641
34642/*
34643** Log an error that is an API call on a connection pointer that should
34644** not have been used. The "type" of connection pointer is given as the
34645** argument. The zType is a word like "NULL" or "closed" or "invalid".
34646*/
34647static void logBadConnection(const char *zType){
34648 sqlite3_log(SQLITE_MISUSE21,
34649 "API call with %s database connection pointer",
34650 zType
34651 );
34652}
34653
34654/*
34655** Check to make sure we have a valid db pointer. This test is not
34656** foolproof but it does provide some measure of protection against
34657** misuse of the interface such as passing in db pointers that are
34658** NULL or which have been previously closed. If this routine returns
34659** 1 it means that the db pointer is valid and 0 if it should not be
34660** dereferenced for any reason. The calling function should invoke
34661** SQLITE_MISUSE immediately.
34662**
34663** sqlite3SafetyCheckOk() requires that the db pointer be valid for
34664** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
34665** open properly and is not fit for general use but which can be
34666** used as an argument to sqlite3_errmsg() or sqlite3_close().
34667*/
34668SQLITE_PRIVATEstatic int sqlite3SafetyCheckOk(sqlite3 *db){
34669 u8 eOpenState;
34670 if( db==0 ){
34671 logBadConnection("NULL");
34672 return 0;
34673 }
34674 eOpenState = db->eOpenState;
34675 if( eOpenState!=SQLITE_STATE_OPEN0x76 ){
34676 if( sqlite3SafetyCheckSickOrOk(db) ){
34677 testcase( sqlite3GlobalConfig.xLog!=0 );
34678 logBadConnection("unopened");
34679 }
34680 return 0;
34681 }else{
34682 return 1;
34683 }
34684}
34685SQLITE_PRIVATEstatic int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
34686 u8 eOpenState;
34687 eOpenState = db->eOpenState;
34688 if( eOpenState!=SQLITE_STATE_SICK0xba &&
34689 eOpenState!=SQLITE_STATE_OPEN0x76 &&
34690 eOpenState!=SQLITE_STATE_BUSY0x6d ){
34691 testcase( sqlite3GlobalConfig.xLog!=0 );
34692 logBadConnection("invalid");
34693 return 0;
34694 }else{
34695 return 1;
34696 }
34697}
34698
34699/*
34700** Attempt to add, substract, or multiply the 64-bit signed value iB against
34701** the other 64-bit signed integer at *pA and store the result in *pA.
34702** Return 0 on success. Or if the operation would have resulted in an
34703** overflow, leave *pA unchanged and return 1.
34704*/
34705SQLITE_PRIVATEstatic int sqlite3AddInt64(i64 *pA, i64 iB){
34706#if GCC_VERSION(4*1000000+2*1000+1)>=5004000 && !defined(__INTEL_COMPILER)
34707 return __builtin_add_overflow(*pA, iB, pA);
34708#else
34709 i64 iA = *pA;
34710 testcase( iA==0 ); testcase( iA==1 );
34711 testcase( iB==-1 ); testcase( iB==0 );
34712 if( iB>=0 ){
34713 testcase( iA>0 && LARGEST_INT64 - iA == iB );
34714 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
34715 if( iA>0 && LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) - iA < iB ) return 1;
34716 }else{
34717 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
34718 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
34719 if( iA<0 && -(iA + LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32))) > iB + 1 ) return 1;
34720 }
34721 *pA += iB;
34722 return 0;
34723#endif
34724}
34725SQLITE_PRIVATEstatic int sqlite3SubInt64(i64 *pA, i64 iB){
34726#if GCC_VERSION(4*1000000+2*1000+1)>=5004000 && !defined(__INTEL_COMPILER)
34727 return __builtin_sub_overflow(*pA, iB, pA);
34728#else
34729 testcase( iB==SMALLEST_INT64+1 );
34730 if( iB==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ){
34731 testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
34732 if( (*pA)>=0 ) return 1;
34733 *pA -= iB;
34734 return 0;
34735 }else{
34736 return sqlite3AddInt64(pA, -iB);
34737 }
34738#endif
34739}
34740SQLITE_PRIVATEstatic int sqlite3MulInt64(i64 *pA, i64 iB){
34741#if GCC_VERSION(4*1000000+2*1000+1)>=5004000 && !defined(__INTEL_COMPILER)
34742 return __builtin_mul_overflow(*pA, iB, pA);
34743#else
34744 i64 iA = *pA;
34745 if( iB>0 ){
34746 if( iA>LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32))/iB ) return 1;
34747 if( iA<SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32)))/iB ) return 1;
34748 }else if( iB<0 ){
34749 if( iA>0 ){
34750 if( iB<SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32)))/iA ) return 1;
34751 }else if( iA<0 ){
34752 if( iB==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ) return 1;
34753 if( iA==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ) return 1;
34754 if( -iA>LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32))/-iB ) return 1;
34755 }
34756 }
34757 *pA = iA*iB;
34758 return 0;
34759#endif
34760}
34761
34762/*
34763** Compute the absolute value of a 32-bit signed integer, of possible. Or
34764** if the integer has a value of -2147483648, return +2147483647
34765*/
34766SQLITE_PRIVATEstatic int sqlite3AbsInt32(int x){
34767 if( x>=0 ) return x;
34768 if( x==(int)0x80000000 ) return 0x7fffffff;
34769 return -x;
34770}
34771
34772#ifdef SQLITE_ENABLE_8_3_NAMES
34773/*
34774** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
34775** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
34776** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
34777** three characters, then shorten the suffix on z[] to be the last three
34778** characters of the original suffix.
34779**
34780** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
34781** do the suffix shortening regardless of URI parameter.
34782**
34783** Examples:
34784**
34785** test.db-journal => test.nal
34786** test.db-wal => test.wal
34787** test.db-shm => test.shm
34788** test.db-mj7f3319fa => test.9fa
34789*/
34790SQLITE_PRIVATEstatic void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
34791#if SQLITE_ENABLE_8_3_NAMES<2
34792 if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
34793#endif
34794 {
34795 int i, sz;
34796 sz = sqlite3Strlen30(z);
34797 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
34798 if( z[i]=='.' && ALWAYS(sz>i+4)(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
34799 }
34800}
34801#endif
34802
34803/*
34804** Find (an approximate) sum of two LogEst values. This computation is
34805** not a simple "+" operator because LogEst is stored as a logarithmic
34806** value.
34807**
34808*/
34809SQLITE_PRIVATEstatic LogEst sqlite3LogEstAdd(LogEst a, LogEst b){
34810 static const unsigned char x[] = {
34811 10, 10, /* 0,1 */
34812 9, 9, /* 2,3 */
34813 8, 8, /* 4,5 */
34814 7, 7, 7, /* 6,7,8 */
34815 6, 6, 6, /* 9,10,11 */
34816 5, 5, 5, /* 12-14 */
34817 4, 4, 4, 4, /* 15-18 */
34818 3, 3, 3, 3, 3, 3, /* 19-24 */
34819 2, 2, 2, 2, 2, 2, 2, /* 25-31 */
34820 };
34821 if( a>=b ){
34822 if( a>b+49 ) return a;
34823 if( a>b+31 ) return a+1;
34824 return a+x[a-b];
34825 }else{
34826 if( b>a+49 ) return b;
34827 if( b>a+31 ) return b+1;
34828 return b+x[b-a];
34829 }
34830}
34831
34832/*
34833** Convert an integer into a LogEst. In other words, compute an
34834** approximation for 10*log2(x).
34835*/
34836SQLITE_PRIVATEstatic LogEst sqlite3LogEst(u64 x){
34837 static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
34838 LogEst y = 40;
34839 if( x<8 ){
34840 if( x<2 ) return 0;
34841 while( x<8 ){ y -= 10; x <<= 1; }
34842 }else{
34843#if GCC_VERSION(4*1000000+2*1000+1)>=5004000
34844 int i = 60 - __builtin_clzll(x);
34845 y += i*10;
34846 x >>= i;
34847#else
34848 while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/
34849 while( x>15 ){ y += 10; x >>= 1; }
34850#endif
34851 }
34852 return a[x&7] + y - 10;
34853}
34854
34855/*
34856** Convert a double into a LogEst
34857** In other words, compute an approximation for 10*log2(x).
34858*/
34859SQLITE_PRIVATEstatic LogEst sqlite3LogEstFromDouble(double x){
34860 u64 a;
34861 LogEst e;
34862 assert( sizeof(x)==8 && sizeof(a)==8 )((void) (0));
34863 if( x<=1 ) return 0;
34864 if( x<=2000000000 ) return sqlite3LogEst((u64)x);
34865 memcpy(&a, &x, 8);
34866 e = (a>>52) - 1022;
34867 return e*10;
34868}
34869
34870/*
34871** Convert a LogEst into an integer.
34872*/
34873SQLITE_PRIVATEstatic u64 sqlite3LogEstToInt(LogEst x){
34874 u64 n;
34875 n = x%10;
34876 x /= 10;
34877 if( n>=5 ) n -= 2;
34878 else if( n>=1 ) n -= 1;
34879 if( x>60 ) return (u64)LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
34880 return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x);
34881}
34882
34883/*
34884** Add a new name/number pair to a VList. This might require that the
34885** VList object be reallocated, so return the new VList. If an OOM
34886** error occurs, the original VList returned and the
34887** db->mallocFailed flag is set.
34888**
34889** A VList is really just an array of integers. To destroy a VList,
34890** simply pass it to sqlite3DbFree().
34891**
34892** The first integer is the number of integers allocated for the whole
34893** VList. The second integer is the number of integers actually used.
34894** Each name/number pair is encoded by subsequent groups of 3 or more
34895** integers.
34896**
34897** Each name/number pair starts with two integers which are the numeric
34898** value for the pair and the size of the name/number pair, respectively.
34899** The text name overlays one or more following integers. The text name
34900** is always zero-terminated.
34901**
34902** Conceptually:
34903**
34904** struct VList {
34905** int nAlloc; // Number of allocated slots
34906** int nUsed; // Number of used slots
34907** struct VListEntry {
34908** int iValue; // Value for this entry
34909** int nSlot; // Slots used by this entry
34910** // ... variable name goes here
34911** } a[0];
34912** }
34913**
34914** During code generation, pointers to the variable names within the
34915** VList are taken. When that happens, nAlloc is set to zero as an
34916** indication that the VList may never again be enlarged, since the
34917** accompanying realloc() would invalidate the pointers.
34918*/
34919SQLITE_PRIVATEstatic VList *sqlite3VListAdd(
34920 sqlite3 *db, /* The database connection used for malloc() */
34921 VList *pIn, /* The input VList. Might be NULL */
34922 const char *zName, /* Name of symbol to add */
34923 int nName, /* Bytes of text in zName */
34924 int iVal /* Value to associate with zName */
34925){
34926 int nInt; /* number of sizeof(int) objects needed for zName */
34927 char *z; /* Pointer to where zName will be stored */
34928 int i; /* Index in pIn[] where zName is stored */
34929
34930 nInt = nName/4 + 3;
34931 assert( pIn==0 || pIn[0]>=3 )((void) (0)); /* Verify ok to add new elements */
34932 if( pIn==0 || pIn[1]+nInt > pIn[0] ){
34933 /* Enlarge the allocation */
34934 sqlite3_int64 nAlloc = (pIn ? 2*(sqlite3_int64)pIn[0] : 10) + nInt;
34935 VList *pOut = sqlite3DbRealloc(db, pIn, nAlloc*sizeof(int));
34936 if( pOut==0 ) return pIn;
34937 if( pIn==0 ) pOut[1] = 2;
34938 pIn = pOut;
34939 pIn[0] = nAlloc;
34940 }
34941 i = pIn[1];
34942 pIn[i] = iVal;
34943 pIn[i+1] = nInt;
34944 z = (char*)&pIn[i+2];
34945 pIn[1] = i+nInt;
34946 assert( pIn[1]<=pIn[0] )((void) (0));
34947 memcpy(z, zName, nName);
34948 z[nName] = 0;
34949 return pIn;
34950}
34951
34952/*
34953** Return a pointer to the name of a variable in the given VList that
34954** has the value iVal. Or return a NULL if there is no such variable in
34955** the list
34956*/
34957SQLITE_PRIVATEstatic const char *sqlite3VListNumToName(VList *pIn, int iVal){
34958 int i, mx;
34959 if( pIn==0 ) return 0;
34960 mx = pIn[1];
34961 i = 2;
34962 do{
34963 if( pIn[i]==iVal ) return (char*)&pIn[i+2];
34964 i += pIn[i+1];
34965 }while( i<mx );
34966 return 0;
34967}
34968
34969/*
34970** Return the number of the variable named zName, if it is in VList.
34971** or return 0 if there is no such variable.
34972*/
34973SQLITE_PRIVATEstatic int sqlite3VListNameToNum(VList *pIn, const char *zName, int nName){
34974 int i, mx;
34975 if( pIn==0 ) return 0;
34976 mx = pIn[1];
34977 i = 2;
34978 do{
34979 const char *z = (const char*)&pIn[i+2];
34980 if( strncmp(z,zName,nName)==0 && z[nName]==0 ) return pIn[i];
34981 i += pIn[i+1];
34982 }while( i<mx );
34983 return 0;
34984}
34985
34986/************** End of util.c ************************************************/
34987/************** Begin file hash.c ********************************************/
34988/*
34989** 2001 September 22
34990**
34991** The author disclaims copyright to this source code. In place of
34992** a legal notice, here is a blessing:
34993**
34994** May you do good and not evil.
34995** May you find forgiveness for yourself and forgive others.
34996** May you share freely, never taking more than you give.
34997**
34998*************************************************************************
34999** This is the implementation of generic hash-tables
35000** used in SQLite.
35001*/
35002/* #include "sqliteInt.h" */
35003/* #include <assert.h> */
35004
35005/* Turn bulk memory into a hash table object by initializing the
35006** fields of the Hash structure.
35007**
35008** "pNew" is a pointer to the hash table that is to be initialized.
35009*/
35010SQLITE_PRIVATEstatic void sqlite3HashInit(Hash *pNew){
35011 assert( pNew!=0 )((void) (0));
35012 pNew->first = 0;
35013 pNew->count = 0;
35014 pNew->htsize = 0;
35015 pNew->ht = 0;
35016}
35017
35018/* Remove all entries from a hash table. Reclaim all memory.
35019** Call this routine to delete a hash table or to reset a hash table
35020** to the empty state.
35021*/
35022SQLITE_PRIVATEstatic void sqlite3HashClear(Hash *pH){
35023 HashElem *elem; /* For looping over all elements of the table */
35024
35025 assert( pH!=0 )((void) (0));
35026 elem = pH->first;
35027 pH->first = 0;
35028 sqlite3_free(pH->ht);
35029 pH->ht = 0;
35030 pH->htsize = 0;
35031 while( elem ){
35032 HashElem *next_elem = elem->next;
35033 sqlite3_free(elem);
35034 elem = next_elem;
35035 }
35036 pH->count = 0;
35037}
35038
35039/*
35040** The hashing function.
35041*/
35042static unsigned int strHash(const char *z){
35043 unsigned int h = 0;
35044 unsigned char c;
35045 while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/
35046 /* Knuth multiplicative hashing. (Sorting & Searching, p. 510).
35047 ** 0x9e3779b1 is 2654435761 which is the closest prime number to
35048 ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
35049 h += sqlite3UpperToLower[c];
35050 h *= 0x9e3779b1;
35051 }
35052 return h;
35053}
35054
35055
35056/* Link pNew element into the hash table pH. If pEntry!=0 then also
35057** insert pNew into the pEntry hash bucket.
35058*/
35059static void insertElement(
35060 Hash *pH, /* The complete hash table */
35061 struct _ht *pEntry, /* The entry into which pNew is inserted */
35062 HashElem *pNew /* The element to be inserted */
35063){
35064 HashElem *pHead; /* First element already in pEntry */
35065 if( pEntry ){
35066 pHead = pEntry->count ? pEntry->chain : 0;
35067 pEntry->count++;
35068 pEntry->chain = pNew;
35069 }else{
35070 pHead = 0;
35071 }
35072 if( pHead ){
35073 pNew->next = pHead;
35074 pNew->prev = pHead->prev;
35075 if( pHead->prev ){ pHead->prev->next = pNew; }
35076 else { pH->first = pNew; }
35077 pHead->prev = pNew;
35078 }else{
35079 pNew->next = pH->first;
35080 if( pH->first ){ pH->first->prev = pNew; }
35081 pNew->prev = 0;
35082 pH->first = pNew;
35083 }
35084}
35085
35086
35087/* Resize the hash table so that it cantains "new_size" buckets.
35088**
35089** The hash table might fail to resize if sqlite3_malloc() fails or
35090** if the new size is the same as the prior size.
35091** Return TRUE if the resize occurs and false if not.
35092*/
35093static int rehash(Hash *pH, unsigned int new_size){
35094 struct _ht *new_ht; /* The new hash table */
35095 HashElem *elem, *next_elem; /* For looping over existing elements */
35096
35097#if SQLITE_MALLOC_SOFT_LIMIT1024>0
35098 if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT1024 ){
35099 new_size = SQLITE_MALLOC_SOFT_LIMIT1024/sizeof(struct _ht);
35100 }
35101 if( new_size==pH->htsize ) return 0;
35102#endif
35103
35104 /* The inability to allocates space for a larger hash table is
35105 ** a performance hit but it is not a fatal error. So mark the
35106 ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of
35107 ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero()
35108 ** only zeroes the requested number of bytes whereas this module will
35109 ** use the actual amount of space allocated for the hash table (which
35110 ** may be larger than the requested amount).
35111 */
35112 sqlite3BeginBenignMalloc();
35113 new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
35114 sqlite3EndBenignMalloc();
35115
35116 if( new_ht==0 ) return 0;
35117 sqlite3_free(pH->ht);
35118 pH->ht = new_ht;
35119 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
35120 memset(new_ht, 0, new_size*sizeof(struct _ht));
35121 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
35122 unsigned int h = strHash(elem->pKey) % new_size;
35123 next_elem = elem->next;
35124 insertElement(pH, &new_ht[h], elem);
35125 }
35126 return 1;
35127}
35128
35129/* This function (for internal use only) locates an element in an
35130** hash table that matches the given key. If no element is found,
35131** a pointer to a static null element with HashElem.data==0 is returned.
35132** If pH is not NULL, then the hash for this key is written to *pH.
35133*/
35134static HashElem *findElementWithHash(
35135 const Hash *pH, /* The pH to be searched */
35136 const char *pKey, /* The key we are searching for */
35137 unsigned int *pHash /* Write the hash value here */
35138){
35139 HashElem *elem; /* Used to loop thru the element list */
35140 unsigned int count; /* Number of elements left to test */
35141 unsigned int h; /* The computed hash */
35142 static HashElem nullElement = { 0, 0, 0, 0 };
35143
35144 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/
35145 struct _ht *pEntry;
35146 h = strHash(pKey) % pH->htsize;
35147 pEntry = &pH->ht[h];
35148 elem = pEntry->chain;
35149 count = pEntry->count;
35150 }else{
35151 h = 0;
35152 elem = pH->first;
35153 count = pH->count;
35154 }
35155 if( pHash ) *pHash = h;
35156 while( count-- ){
35157 assert( elem!=0 )((void) (0));
35158 if( sqlite3StrICmp(elem->pKey,pKey)==0 ){
35159 return elem;
35160 }
35161 elem = elem->next;
35162 }
35163 return &nullElement;
35164}
35165
35166/* Remove a single entry from the hash table given a pointer to that
35167** element and a hash on the element's key.
35168*/
35169static void removeElementGivenHash(
35170 Hash *pH, /* The pH containing "elem" */
35171 HashElem* elem, /* The element to be removed from the pH */
35172 unsigned int h /* Hash value for the element */
35173){
35174 struct _ht *pEntry;
35175 if( elem->prev ){
35176 elem->prev->next = elem->next;
35177 }else{
35178 pH->first = elem->next;
35179 }
35180 if( elem->next ){
35181 elem->next->prev = elem->prev;
35182 }
35183 if( pH->ht ){
35184 pEntry = &pH->ht[h];
35185 if( pEntry->chain==elem ){
35186 pEntry->chain = elem->next;
35187 }
35188 assert( pEntry->count>0 )((void) (0));
35189 pEntry->count--;
35190 }
35191 sqlite3_free( elem );
35192 pH->count--;
35193 if( pH->count==0 ){
35194 assert( pH->first==0 )((void) (0));
35195 assert( pH->count==0 )((void) (0));
35196 sqlite3HashClear(pH);
35197 }
35198}
35199
35200/* Attempt to locate an element of the hash table pH with a key
35201** that matches pKey. Return the data for this element if it is
35202** found, or NULL if there is no match.
35203*/
35204SQLITE_PRIVATEstatic void *sqlite3HashFind(const Hash *pH, const char *pKey){
35205 assert( pH!=0 )((void) (0));
35206 assert( pKey!=0 )((void) (0));
35207 return findElementWithHash(pH, pKey, 0)->data;
35208}
35209
35210/* Insert an element into the hash table pH. The key is pKey
35211** and the data is "data".
35212**
35213** If no element exists with a matching key, then a new
35214** element is created and NULL is returned.
35215**
35216** If another element already exists with the same key, then the
35217** new data replaces the old data and the old data is returned.
35218** The key is not copied in this instance. If a malloc fails, then
35219** the new data is returned and the hash table is unchanged.
35220**
35221** If the "data" parameter to this function is NULL, then the
35222** element corresponding to "key" is removed from the hash table.
35223*/
35224SQLITE_PRIVATEstatic void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){
35225 unsigned int h; /* the hash of the key modulo hash table size */
35226 HashElem *elem; /* Used to loop thru the element list */
35227 HashElem *new_elem; /* New element added to the pH */
35228
35229 assert( pH!=0 )((void) (0));
35230 assert( pKey!=0 )((void) (0));
35231 elem = findElementWithHash(pH,pKey,&h);
35232 if( elem->data ){
35233 void *old_data = elem->data;
35234 if( data==0 ){
35235 removeElementGivenHash(pH,elem,h);
35236 }else{
35237 elem->data = data;
35238 elem->pKey = pKey;
35239 }
35240 return old_data;
35241 }
35242 if( data==0 ) return 0;
35243 new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
35244 if( new_elem==0 ) return data;
35245 new_elem->pKey = pKey;
35246 new_elem->data = data;
35247 pH->count++;
35248 if( pH->count>=10 && pH->count > 2*pH->htsize ){
35249 if( rehash(pH, pH->count*2) ){
35250 assert( pH->htsize>0 )((void) (0));
35251 h = strHash(pKey) % pH->htsize;
35252 }
35253 }
35254 insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem);
35255 return 0;
35256}
35257
35258/************** End of hash.c ************************************************/
35259/************** Begin file opcodes.c *****************************************/
35260/* Automatically generated. Do not edit */
35261/* See the tool/mkopcodec.tcl script for details. */
35262#if !defined(SQLITE_OMIT_EXPLAIN) \
35263 || defined(VDBE_PROFILE) \
35264 || defined(SQLITE_DEBUG)
35265#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) || defined(SQLITE_DEBUG)
35266# define OpHelp(X) "\0" X
35267#else
35268# define OpHelp(X)
35269#endif
35270SQLITE_PRIVATEstatic const char *sqlite3OpcodeName(int i){
35271 static const char *const azName[] = {
35272 /* 0 */ "Savepoint" OpHelp(""),
35273 /* 1 */ "AutoCommit" OpHelp(""),
35274 /* 2 */ "Transaction" OpHelp(""),
35275 /* 3 */ "Checkpoint" OpHelp(""),
35276 /* 4 */ "JournalMode" OpHelp(""),
35277 /* 5 */ "Vacuum" OpHelp(""),
35278 /* 6 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"),
35279 /* 7 */ "VUpdate" OpHelp("data=r[P3@P2]"),
35280 /* 8 */ "Goto" OpHelp(""),
35281 /* 9 */ "Gosub" OpHelp(""),
35282 /* 10 */ "InitCoroutine" OpHelp(""),
35283 /* 11 */ "Yield" OpHelp(""),
35284 /* 12 */ "MustBeInt" OpHelp(""),
35285 /* 13 */ "Jump" OpHelp(""),
35286 /* 14 */ "Once" OpHelp(""),
35287 /* 15 */ "If" OpHelp(""),
35288 /* 16 */ "IfNot" OpHelp(""),
35289 /* 17 */ "IsNullOrType" OpHelp("if typeof(r[P1]) IN (P3,5) goto P2"),
35290 /* 18 */ "IfNullRow" OpHelp("if P1.nullRow then r[P3]=NULL, goto P2"),
35291 /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"),
35292 /* 20 */ "SeekLT" OpHelp("key=r[P3@P4]"),
35293 /* 21 */ "SeekLE" OpHelp("key=r[P3@P4]"),
35294 /* 22 */ "SeekGE" OpHelp("key=r[P3@P4]"),
35295 /* 23 */ "SeekGT" OpHelp("key=r[P3@P4]"),
35296 /* 24 */ "IfNotOpen" OpHelp("if( !csr[P1] ) goto P2"),
35297 /* 25 */ "IfNoHope" OpHelp("key=r[P3@P4]"),
35298 /* 26 */ "NoConflict" OpHelp("key=r[P3@P4]"),
35299 /* 27 */ "NotFound" OpHelp("key=r[P3@P4]"),
35300 /* 28 */ "Found" OpHelp("key=r[P3@P4]"),
35301 /* 29 */ "SeekRowid" OpHelp("intkey=r[P3]"),
35302 /* 30 */ "NotExists" OpHelp("intkey=r[P3]"),
35303 /* 31 */ "Last" OpHelp(""),
35304 /* 32 */ "IfSmaller" OpHelp(""),
35305 /* 33 */ "SorterSort" OpHelp(""),
35306 /* 34 */ "Sort" OpHelp(""),
35307 /* 35 */ "Rewind" OpHelp(""),
35308 /* 36 */ "SorterNext" OpHelp(""),
35309 /* 37 */ "Prev" OpHelp(""),
35310 /* 38 */ "Next" OpHelp(""),
35311 /* 39 */ "IdxLE" OpHelp("key=r[P3@P4]"),
35312 /* 40 */ "IdxGT" OpHelp("key=r[P3@P4]"),
35313 /* 41 */ "IdxLT" OpHelp("key=r[P3@P4]"),
35314 /* 42 */ "IdxGE" OpHelp("key=r[P3@P4]"),
35315 /* 43 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"),
35316 /* 44 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"),
35317 /* 45 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
35318 /* 46 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
35319 /* 47 */ "Program" OpHelp(""),
35320 /* 48 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
35321 /* 49 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
35322 /* 50 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"),
35323 /* 51 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"),
35324 /* 52 */ "Ne" OpHelp("IF r[P3]!=r[P1]"),
35325 /* 53 */ "Eq" OpHelp("IF r[P3]==r[P1]"),
35326 /* 54 */ "Gt" OpHelp("IF r[P3]>r[P1]"),
35327 /* 55 */ "Le" OpHelp("IF r[P3]<=r[P1]"),
35328 /* 56 */ "Lt" OpHelp("IF r[P3]<r[P1]"),
35329 /* 57 */ "Ge" OpHelp("IF r[P3]>=r[P1]"),
35330 /* 58 */ "ElseEq" OpHelp(""),
35331 /* 59 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
35332 /* 60 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
35333 /* 61 */ "IncrVacuum" OpHelp(""),
35334 /* 62 */ "VNext" OpHelp(""),
35335 /* 63 */ "Filter" OpHelp("if key(P3@P4) not in filter(P1) goto P2"),
35336 /* 64 */ "Init" OpHelp("Start at P2"),
35337 /* 65 */ "PureFunc" OpHelp("r[P3]=func(r[P2@NP])"),
35338 /* 66 */ "Function" OpHelp("r[P3]=func(r[P2@NP])"),
35339 /* 67 */ "Return" OpHelp(""),
35340 /* 68 */ "EndCoroutine" OpHelp(""),
35341 /* 69 */ "HaltIfNull" OpHelp("if r[P3]=null halt"),
35342 /* 70 */ "Halt" OpHelp(""),
35343 /* 71 */ "Integer" OpHelp("r[P2]=P1"),
35344 /* 72 */ "Int64" OpHelp("r[P2]=P4"),
35345 /* 73 */ "String" OpHelp("r[P2]='P4' (len=P1)"),
35346 /* 74 */ "BeginSubrtn" OpHelp("r[P2]=NULL"),
35347 /* 75 */ "Null" OpHelp("r[P2..P3]=NULL"),
35348 /* 76 */ "SoftNull" OpHelp("r[P1]=NULL"),
35349 /* 77 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"),
35350 /* 78 */ "Variable" OpHelp("r[P2]=parameter(P1,P4)"),
35351 /* 79 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"),
35352 /* 80 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
35353 /* 81 */ "SCopy" OpHelp("r[P2]=r[P1]"),
35354 /* 82 */ "IntCopy" OpHelp("r[P2]=r[P1]"),
35355 /* 83 */ "FkCheck" OpHelp(""),
35356 /* 84 */ "ResultRow" OpHelp("output=r[P1@P2]"),
35357 /* 85 */ "CollSeq" OpHelp(""),
35358 /* 86 */ "AddImm" OpHelp("r[P1]=r[P1]+P2"),
35359 /* 87 */ "RealAffinity" OpHelp(""),
35360 /* 88 */ "Cast" OpHelp("affinity(r[P1])"),
35361 /* 89 */ "Permutation" OpHelp(""),
35362 /* 90 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
35363 /* 91 */ "IsTrue" OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"),
35364 /* 92 */ "ZeroOrNull" OpHelp("r[P2] = 0 OR NULL"),
35365 /* 93 */ "Offset" OpHelp("r[P3] = sqlite_offset(P1)"),
35366 /* 94 */ "Column" OpHelp("r[P3]=PX cursor P1 column P2"),
35367 /* 95 */ "TypeCheck" OpHelp("typecheck(r[P1@P2])"),
35368 /* 96 */ "Affinity" OpHelp("affinity(r[P1@P2])"),
35369 /* 97 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"),
35370 /* 98 */ "Count" OpHelp("r[P2]=count()"),
35371 /* 99 */ "ReadCookie" OpHelp(""),
35372 /* 100 */ "SetCookie" OpHelp(""),
35373 /* 101 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"),
35374 /* 102 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"),
35375 /* 103 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"),
35376 /* 104 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"),
35377 /* 105 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"),
35378 /* 106 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"),
35379 /* 107 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
35380 /* 108 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"),
35381 /* 109 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
35382 /* 110 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
35383 /* 111 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
35384 /* 112 */ "OpenRead" OpHelp("root=P2 iDb=P3"),
35385 /* 113 */ "OpenWrite" OpHelp("root=P2 iDb=P3"),
35386 /* 114 */ "BitNot" OpHelp("r[P2]= ~r[P1]"),
35387 /* 115 */ "OpenDup" OpHelp(""),
35388 /* 116 */ "OpenAutoindex" OpHelp("nColumn=P2"),
35389 /* 117 */ "String8" OpHelp("r[P2]='P4'"),
35390 /* 118 */ "OpenEphemeral" OpHelp("nColumn=P2"),
35391 /* 119 */ "SorterOpen" OpHelp(""),
35392 /* 120 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
35393 /* 121 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"),
35394 /* 122 */ "Close" OpHelp(""),
35395 /* 123 */ "ColumnsUsed" OpHelp(""),
35396 /* 124 */ "SeekScan" OpHelp("Scan-ahead up to P1 rows"),
35397 /* 125 */ "SeekHit" OpHelp("set P2<=seekHit<=P3"),
35398 /* 126 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
35399 /* 127 */ "NewRowid" OpHelp("r[P2]=rowid"),
35400 /* 128 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
35401 /* 129 */ "RowCell" OpHelp(""),
35402 /* 130 */ "Delete" OpHelp(""),
35403 /* 131 */ "ResetCount" OpHelp(""),
35404 /* 132 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
35405 /* 133 */ "SorterData" OpHelp("r[P2]=data"),
35406 /* 134 */ "RowData" OpHelp("r[P2]=data"),
35407 /* 135 */ "Rowid" OpHelp("r[P2]=PX rowid of P1"),
35408 /* 136 */ "NullRow" OpHelp(""),
35409 /* 137 */ "SeekEnd" OpHelp(""),
35410 /* 138 */ "IdxInsert" OpHelp("key=r[P2]"),
35411 /* 139 */ "SorterInsert" OpHelp("key=r[P2]"),
35412 /* 140 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
35413 /* 141 */ "DeferredSeek" OpHelp("Move P3 to P1.rowid if needed"),
35414 /* 142 */ "IdxRowid" OpHelp("r[P2]=rowid"),
35415 /* 143 */ "FinishSeek" OpHelp(""),
35416 /* 144 */ "Destroy" OpHelp(""),
35417 /* 145 */ "Clear" OpHelp(""),
35418 /* 146 */ "ResetSorter" OpHelp(""),
35419 /* 147 */ "CreateBtree" OpHelp("r[P2]=root iDb=P1 flags=P3"),
35420 /* 148 */ "SqlExec" OpHelp(""),
35421 /* 149 */ "ParseSchema" OpHelp(""),
35422 /* 150 */ "LoadAnalysis" OpHelp(""),
35423 /* 151 */ "DropTable" OpHelp(""),
35424 /* 152 */ "DropIndex" OpHelp(""),
35425 /* 153 */ "Real" OpHelp("r[P2]=P4"),
35426 /* 154 */ "DropTrigger" OpHelp(""),
35427 /* 155 */ "IntegrityCk" OpHelp(""),
35428 /* 156 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
35429 /* 157 */ "Param" OpHelp(""),
35430 /* 158 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
35431 /* 159 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
35432 /* 160 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
35433 /* 161 */ "AggInverse" OpHelp("accum=r[P3] inverse(r[P2@P5])"),
35434 /* 162 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"),
35435 /* 163 */ "AggStep1" OpHelp("accum=r[P3] step(r[P2@P5])"),
35436 /* 164 */ "AggValue" OpHelp("r[P3]=value N=P2"),
35437 /* 165 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
35438 /* 166 */ "Expire" OpHelp(""),
35439 /* 167 */ "CursorLock" OpHelp(""),
35440 /* 168 */ "CursorUnlock" OpHelp(""),
35441 /* 169 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
35442 /* 170 */ "VBegin" OpHelp(""),
35443 /* 171 */ "VCreate" OpHelp(""),
35444 /* 172 */ "VDestroy" OpHelp(""),
35445 /* 173 */ "VOpen" OpHelp(""),
35446 /* 174 */ "VInitIn" OpHelp("r[P2]=ValueList(P1,P3)"),
35447 /* 175 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
35448 /* 176 */ "VRename" OpHelp(""),
35449 /* 177 */ "Pagecount" OpHelp(""),
35450 /* 178 */ "MaxPgcnt" OpHelp(""),
35451 /* 179 */ "ClrSubtype" OpHelp("r[P1].subtype = 0"),
35452 /* 180 */ "FilterAdd" OpHelp("filter(P1) += key(P3@P4)"),
35453 /* 181 */ "Trace" OpHelp(""),
35454 /* 182 */ "CursorHint" OpHelp(""),
35455 /* 183 */ "ReleaseReg" OpHelp("release r[P1@P2] mask P3"),
35456 /* 184 */ "Noop" OpHelp(""),
35457 /* 185 */ "Explain" OpHelp(""),
35458 /* 186 */ "Abortable" OpHelp(""),
35459 };
35460 return azName[i];
35461}
35462#endif
35463
35464/************** End of opcodes.c *********************************************/
35465/************** Begin file os_unix.c *****************************************/
35466/*
35467** 2004 May 22
35468**
35469** The author disclaims copyright to this source code. In place of
35470** a legal notice, here is a blessing:
35471**
35472** May you do good and not evil.
35473** May you find forgiveness for yourself and forgive others.
35474** May you share freely, never taking more than you give.
35475**
35476******************************************************************************
35477**
35478** This file contains the VFS implementation for unix-like operating systems
35479** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
35480**
35481** There are actually several different VFS implementations in this file.
35482** The differences are in the way that file locking is done. The default
35483** implementation uses Posix Advisory Locks. Alternative implementations
35484** use flock(), dot-files, various proprietary locking schemas, or simply
35485** skip locking all together.
35486**
35487** This source file is organized into divisions where the logic for various
35488** subfunctions is contained within the appropriate division. PLEASE
35489** KEEP THE STRUCTURE OF THIS FILE INTACT. New code should be placed
35490** in the correct division and should be clearly labeled.
35491**
35492** The layout of divisions is as follows:
35493**
35494** * General-purpose declarations and utility functions.
35495** * Unique file ID logic used by VxWorks.
35496** * Various locking primitive implementations (all except proxy locking):
35497** + for Posix Advisory Locks
35498** + for no-op locks
35499** + for dot-file locks
35500** + for flock() locking
35501** + for named semaphore locks (VxWorks only)
35502** + for AFP filesystem locks (MacOSX only)
35503** * sqlite3_file methods not associated with locking.
35504** * Definitions of sqlite3_io_methods objects for all locking
35505** methods plus "finder" functions for each locking method.
35506** * sqlite3_vfs method implementations.
35507** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
35508** * Definitions of sqlite3_vfs objects for all locking methods
35509** plus implementations of sqlite3_os_init() and sqlite3_os_end().
35510*/
35511/* #include "sqliteInt.h" */
35512#if SQLITE_OS_UNIX1 /* This file is used on unix only */
35513
35514/*
35515** There are various methods for file locking used for concurrency
35516** control:
35517**
35518** 1. POSIX locking (the default),
35519** 2. No locking,
35520** 3. Dot-file locking,
35521** 4. flock() locking,
35522** 5. AFP locking (OSX only),
35523** 6. Named POSIX semaphores (VXWorks only),
35524** 7. proxy locking. (OSX only)
35525**
35526** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
35527** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
35528** selection of the appropriate locking style based on the filesystem
35529** where the database is located.
35530*/
35531#if !defined(SQLITE_ENABLE_LOCKING_STYLE0)
35532# if defined(__APPLE__)
35533# define SQLITE_ENABLE_LOCKING_STYLE0 1
35534# else
35535# define SQLITE_ENABLE_LOCKING_STYLE0 0
35536# endif
35537#endif
35538
35539/* Use pread() and pwrite() if they are available */
35540#if defined(__APPLE__)
35541# define HAVE_PREAD 1
35542# define HAVE_PWRITE 1
35543#endif
35544#if defined(HAVE_PREAD64) && defined(HAVE_PWRITE64)
35545# undef USE_PREAD
35546# define USE_PREAD64 1
35547#elif defined(HAVE_PREAD) && defined(HAVE_PWRITE)
35548# undef USE_PREAD64
35549# define USE_PREAD 1
35550#endif
35551
35552/*
35553** standard include files.
35554*/
35555#include <sys/types.h>
35556#include <sys/stat.h>
35557#include <fcntl.h>
35558#include <sys/ioctl.h>
35559#include <unistd.h>
35560/* #include <time.h> */
35561#include <sys/time.h>
35562#include <errno(*__errno_location ()).h>
35563#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0
35564# include <sys/mman.h>
35565#endif
35566
35567#if SQLITE_ENABLE_LOCKING_STYLE0
35568/* # include <sys/ioctl.h> */
35569# include <sys/file.h>
35570# include <sys/param.h>
35571#endif /* SQLITE_ENABLE_LOCKING_STYLE */
35572
35573/*
35574** Try to determine if gethostuuid() is available based on standard
35575** macros. This might sometimes compute the wrong value for some
35576** obscure platforms. For those cases, simply compile with one of
35577** the following:
35578**
35579** -DHAVE_GETHOSTUUID=0
35580** -DHAVE_GETHOSTUUID=1
35581**
35582** None if this matters except when building on Apple products with
35583** -DSQLITE_ENABLE_LOCKING_STYLE.
35584*/
35585#ifndef HAVE_GETHOSTUUID0
35586# define HAVE_GETHOSTUUID0 0
35587# if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \
35588 (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000))
35589# if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \
35590 && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0))\
35591 && (!defined(TARGET_OS_MACCATALYST) || (TARGET_OS_MACCATALYST==0))
35592# undef HAVE_GETHOSTUUID0
35593# define HAVE_GETHOSTUUID0 1
35594# else
35595# warning "gethostuuid() is disabled."
35596# endif
35597# endif
35598#endif
35599
35600
35601#if OS_VXWORKS0
35602/* # include <sys/ioctl.h> */
35603# include <semaphore.h>
35604# include <limits.h>
35605#endif /* OS_VXWORKS */
35606
35607#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE0
35608# include <sys/mount.h>
35609#endif
35610
35611#ifdef HAVE_UTIME
35612# include <utime.h>
35613#endif
35614
35615/*
35616** Allowed values of unixFile.fsFlags
35617*/
35618#define SQLITE_FSFLAGS_IS_MSDOS0x1 0x1
35619
35620/*
35621** If we are to be thread-safe, include the pthreads header.
35622*/
35623#if SQLITE_THREADSAFE2
35624/* # include <pthread.h> */
35625#endif
35626
35627/*
35628** Default permissions when creating a new file
35629*/
35630#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS0644
35631# define SQLITE_DEFAULT_FILE_PERMISSIONS0644 0644
35632#endif
35633
35634/*
35635** Default permissions when creating auto proxy dir
35636*/
35637#ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS0755
35638# define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS0755 0755
35639#endif
35640
35641/*
35642** Maximum supported path-length.
35643*/
35644#define MAX_PATHNAME512 512
35645
35646/*
35647** Maximum supported symbolic links
35648*/
35649#define SQLITE_MAX_SYMLINKS100 100
35650
35651/* Always cast the getpid() return type for compatibility with
35652** kernel modules in VxWorks. */
35653#define osGetpid(X)(pid_t)getpid() (pid_t)getpid()
35654
35655/*
35656** Only set the lastErrno if the error code is a real error and not
35657** a normal expected return code of SQLITE_BUSY or SQLITE_OK
35658*/
35659#define IS_LOCK_ERROR(x)((x != 0) && (x != 5)) ((x != SQLITE_OK0) && (x != SQLITE_BUSY5))
35660
35661/* Forward references */
35662typedef struct unixShm unixShm; /* Connection shared memory */
35663typedef struct unixShmNode unixShmNode; /* Shared memory instance */
35664typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
35665typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */
35666
35667/*
35668** Sometimes, after a file handle is closed by SQLite, the file descriptor
35669** cannot be closed immediately. In these cases, instances of the following
35670** structure are used to store the file descriptor while waiting for an
35671** opportunity to either close or reuse it.
35672*/
35673struct UnixUnusedFd {
35674 int fd; /* File descriptor to close */
35675 int flags; /* Flags this file descriptor was opened with */
35676 UnixUnusedFd *pNext; /* Next unused file descriptor on same file */
35677};
35678
35679/*
35680** The unixFile structure is subclass of sqlite3_file specific to the unix
35681** VFS implementations.
35682*/
35683typedef struct unixFile unixFile;
35684struct unixFile {
35685 sqlite3_io_methods const *pMethod; /* Always the first entry */
35686 sqlite3_vfs *pVfs; /* The VFS that created this unixFile */
35687 unixInodeInfo *pInode; /* Info about locks on this inode */
35688 int h; /* The file descriptor */
35689 unsigned char eFileLock; /* The type of lock held on this fd */
35690 unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */
35691 int lastErrno; /* The unix errno from last I/O error */
35692 void *lockingContext; /* Locking style specific state */
35693 UnixUnusedFd *pPreallocatedUnused; /* Pre-allocated UnixUnusedFd */
35694 const char *zPath; /* Name of the file */
35695 unixShm *pShm; /* Shared memory segment information */
35696 int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
35697#if SQLITE_MAX_MMAP_SIZE20971520>0
35698 int nFetchOut; /* Number of outstanding xFetch refs */
35699 sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */
35700 sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */
35701 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
35702 void *pMapRegion; /* Memory mapped region */
35703#endif
35704 int sectorSize; /* Device sector size */
35705 int deviceCharacteristics; /* Precomputed device characteristics */
35706#if SQLITE_ENABLE_LOCKING_STYLE0
35707 int openFlags; /* The flags specified at open() */
35708#endif
35709#if SQLITE_ENABLE_LOCKING_STYLE0 || defined(__APPLE__)
35710 unsigned fsFlags; /* cached details from statfs() */
35711#endif
35712#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
35713 unsigned iBusyTimeout; /* Wait this many millisec on locks */
35714#endif
35715#if OS_VXWORKS0
35716 struct vxworksFileId *pId; /* Unique file ID */
35717#endif
35718#ifdef SQLITE_DEBUG
35719 /* The next group of variables are used to track whether or not the
35720 ** transaction counter in bytes 24-27 of database files are updated
35721 ** whenever any part of the database changes. An assertion fault will
35722 ** occur if a file is updated without also updating the transaction
35723 ** counter. This test is made to avoid new problems similar to the
35724 ** one described by ticket #3584.
35725 */
35726 unsigned char transCntrChng; /* True if the transaction counter changed */
35727 unsigned char dbUpdate; /* True if any part of database file changed */
35728 unsigned char inNormalWrite; /* True if in a normal write operation */
35729
35730#endif
35731
35732#ifdef SQLITE_TEST
35733 /* In test mode, increase the size of this structure a bit so that
35734 ** it is larger than the struct CrashFile defined in test6.c.
35735 */
35736 char aPadding[32];
35737#endif
35738};
35739
35740/* This variable holds the process id (pid) from when the xRandomness()
35741** method was called. If xOpen() is called from a different process id,
35742** indicating that a fork() has occurred, the PRNG will be reset.
35743*/
35744static pid_t randomnessPid = 0;
35745
35746/*
35747** Allowed values for the unixFile.ctrlFlags bitmask:
35748*/
35749#define UNIXFILE_EXCL0x01 0x01 /* Connections from one process only */
35750#define UNIXFILE_RDONLY0x02 0x02 /* Connection is read only */
35751#define UNIXFILE_PERSIST_WAL0x04 0x04 /* Persistent WAL mode */
35752#ifndef SQLITE_DISABLE_DIRSYNC
35753# define UNIXFILE_DIRSYNC0x08 0x08 /* Directory sync needed */
35754#else
35755# define UNIXFILE_DIRSYNC0x08 0x00
35756#endif
35757#define UNIXFILE_PSOW0x10 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
35758#define UNIXFILE_DELETE0x20 0x20 /* Delete on close */
35759#define UNIXFILE_URI0x40 0x40 /* Filename might have query parameters */
35760#define UNIXFILE_NOLOCK0x80 0x80 /* Do no file locking */
35761
35762/*
35763** Include code that is common to all os_*.c files
35764*/
35765/* #include "os_common.h" */
35766
35767/*
35768** Define various macros that are missing from some systems.
35769*/
35770#ifndef O_LARGEFILE0
35771# define O_LARGEFILE0 0
35772#endif
35773#ifdef SQLITE_DISABLE_LFS
35774# undef O_LARGEFILE0
35775# define O_LARGEFILE0 0
35776#endif
35777#ifndef O_NOFOLLOW0400000
35778# define O_NOFOLLOW0400000 0
35779#endif
35780#ifndef O_BINARY0
35781# define O_BINARY0 0
35782#endif
35783
35784/*
35785** The threadid macro resolves to the thread-id or to 0. Used for
35786** testing and debugging only.
35787*/
35788#if SQLITE_THREADSAFE2
35789#define threadidpthread_self() pthread_self()
35790#else
35791#define threadidpthread_self() 0
35792#endif
35793
35794/*
35795** HAVE_MREMAP defaults to true on Linux and false everywhere else.
35796*/
35797#if !defined(HAVE_MREMAP1)
35798# if defined(__linux__1) && defined(_GNU_SOURCE)
35799# define HAVE_MREMAP1 1
35800# else
35801# define HAVE_MREMAP1 0
35802# endif
35803#endif
35804
35805/*
35806** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
35807** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
35808*/
35809#ifdef __ANDROID__
35810# define lseek lseek64
35811#endif
35812
35813#ifdef __linux__1
35814/*
35815** Linux-specific IOCTL magic numbers used for controlling F2FS
35816*/
35817#define F2FS_IOCTL_MAGIC0xf5 0xf5
35818#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)))
35819#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)))
35820#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)))
35821#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)))
35822#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)))
35823#define F2FS_FEATURE_ATOMIC_WRITE0x0004 0x0004
35824#endif /* __linux__ */
35825
35826
35827/*
35828** Different Unix systems declare open() in different ways. Same use
35829** open(const char*,int,mode_t). Others use open(const char*,int,...).
35830** The difference is important when using a pointer to the function.
35831**
35832** The safest way to deal with the problem is to always use this wrapper
35833** which always has the same well-defined interface.
35834*/
35835static int posixOpen(const char *zFile, int flags, int mode){
35836 return open(zFile, flags, mode);
35837}
35838
35839/* Forward reference */
35840static int openDirectory(const char*, int*);
35841static int unixGetpagesize(void);
35842
35843/*
35844** Many system calls are accessed through pointer-to-functions so that
35845** they may be overridden at runtime to facilitate fault injection during
35846** testing and sandboxing. The following array holds the names and pointers
35847** to all overrideable system calls.
35848*/
35849static struct unix_syscall {
35850 const char *zName; /* Name of the system call */
35851 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
35852 sqlite3_syscall_ptr pDefault; /* Default value */
35853} aSyscall[] = {
35854 { "open", (sqlite3_syscall_ptr)posixOpen, 0 },
35855#define osOpen((int(*)(const char*,int,int))aSyscall[0].pCurrent) ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
35856
35857 { "close", (sqlite3_syscall_ptr)close, 0 },
35858#define osClose((int(*)(int))aSyscall[1].pCurrent) ((int(*)(int))aSyscall[1].pCurrent)
35859
35860 { "access", (sqlite3_syscall_ptr)access, 0 },
35861#define osAccess((int(*)(const char*,int))aSyscall[2].pCurrent) ((int(*)(const char*,int))aSyscall[2].pCurrent)
35862
35863 { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 },
35864#define osGetcwd((char*(*)(char*,size_t))aSyscall[3].pCurrent) ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
35865
35866 { "stat", (sqlite3_syscall_ptr)stat, 0 },
35867#define osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent) ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
35868
35869/*
35870** The DJGPP compiler environment looks mostly like Unix, but it
35871** lacks the fcntl() system call. So redefine fcntl() to be something
35872** that always succeeds. This means that locking does not occur under
35873** DJGPP. But it is DOS - what did you expect?
35874*/
35875#ifdef __DJGPP__
35876 { "fstat", 0, 0 },
35877#define osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(a,b,c) 0
35878#else
35879 { "fstat", (sqlite3_syscall_ptr)fstat, 0 },
35880#define osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent) ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
35881#endif
35882
35883 { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 },
35884#define osFtruncate((int(*)(int,off_t))aSyscall[6].pCurrent) ((int(*)(int,off_t))aSyscall[6].pCurrent)
35885
35886 { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 },
35887#define osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent) ((int(*)(int,int,...))aSyscall[7].pCurrent)
35888
35889 { "read", (sqlite3_syscall_ptr)read, 0 },
35890#define osRead((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent) ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
35891
35892#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE0
35893 { "pread", (sqlite3_syscall_ptr)pread, 0 },
35894#else
35895 { "pread", (sqlite3_syscall_ptr)0, 0 },
35896#endif
35897#define osPread((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent) ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
35898
35899#if defined(USE_PREAD64)
35900 { "pread64", (sqlite3_syscall_ptr)pread64, 0 },
35901#else
35902 { "pread64", (sqlite3_syscall_ptr)0, 0 },
35903#endif
35904#define osPread64((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent) ((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent)
35905
35906 { "write", (sqlite3_syscall_ptr)write, 0 },
35907#define osWrite((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent) ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
35908
35909#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE0
35910 { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 },
35911#else
35912 { "pwrite", (sqlite3_syscall_ptr)0, 0 },
35913#endif
35914#define osPwrite((ssize_t(*)(int,const void*,size_t,off_t)) aSyscall[12].pCurrent
)
((ssize_t(*)(int,const void*,size_t,off_t))\
35915 aSyscall[12].pCurrent)
35916
35917#if defined(USE_PREAD64)
35918 { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 },
35919#else
35920 { "pwrite64", (sqlite3_syscall_ptr)0, 0 },
35921#endif
35922#define osPwrite64((ssize_t(*)(int,const void*,size_t,off64_t)) aSyscall[13].pCurrent
)
((ssize_t(*)(int,const void*,size_t,off64_t))\
35923 aSyscall[13].pCurrent)
35924
35925 { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 },
35926#define osFchmod((int(*)(int,mode_t))aSyscall[14].pCurrent) ((int(*)(int,mode_t))aSyscall[14].pCurrent)
35927
35928#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
35929 { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 },
35930#else
35931 { "fallocate", (sqlite3_syscall_ptr)0, 0 },
35932#endif
35933#define osFallocate((int(*)(int,off_t,off_t))aSyscall[15].pCurrent) ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
35934
35935 { "unlink", (sqlite3_syscall_ptr)unlink, 0 },
35936#define osUnlink((int(*)(const char*))aSyscall[16].pCurrent) ((int(*)(const char*))aSyscall[16].pCurrent)
35937
35938 { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 },
35939#define osOpenDirectory((int(*)(const char*,int*))aSyscall[17].pCurrent) ((int(*)(const char*,int*))aSyscall[17].pCurrent)
35940
35941 { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 },
35942#define osMkdir((int(*)(const char*,mode_t))aSyscall[18].pCurrent) ((int(*)(const char*,mode_t))aSyscall[18].pCurrent)
35943
35944 { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 },
35945#define osRmdir((int(*)(const char*))aSyscall[19].pCurrent) ((int(*)(const char*))aSyscall[19].pCurrent)
35946
35947#if defined(HAVE_FCHOWN1)
35948 { "fchown", (sqlite3_syscall_ptr)fchown, 0 },
35949#else
35950 { "fchown", (sqlite3_syscall_ptr)0, 0 },
35951#endif
35952#define osFchown((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent) ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
35953
35954#if defined(HAVE_FCHOWN1)
35955 { "geteuid", (sqlite3_syscall_ptr)geteuid, 0 },
35956#else
35957 { "geteuid", (sqlite3_syscall_ptr)0, 0 },
35958#endif
35959#define osGeteuid((uid_t(*)(void))aSyscall[21].pCurrent) ((uid_t(*)(void))aSyscall[21].pCurrent)
35960
35961#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0
35962 { "mmap", (sqlite3_syscall_ptr)mmap, 0 },
35963#else
35964 { "mmap", (sqlite3_syscall_ptr)0, 0 },
35965#endif
35966#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)
35967
35968#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0
35969 { "munmap", (sqlite3_syscall_ptr)munmap, 0 },
35970#else
35971 { "munmap", (sqlite3_syscall_ptr)0, 0 },
35972#endif
35973#define osMunmap((int(*)(void*,size_t))aSyscall[23].pCurrent) ((int(*)(void*,size_t))aSyscall[23].pCurrent)
35974
35975#if HAVE_MREMAP1 && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0)
35976 { "mremap", (sqlite3_syscall_ptr)mremap, 0 },
35977#else
35978 { "mremap", (sqlite3_syscall_ptr)0, 0 },
35979#endif
35980#define osMremap((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent
)
((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent)
35981
35982#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0
35983 { "getpagesize", (sqlite3_syscall_ptr)unixGetpagesize, 0 },
35984#else
35985 { "getpagesize", (sqlite3_syscall_ptr)0, 0 },
35986#endif
35987#define osGetpagesize((int(*)(void))aSyscall[25].pCurrent) ((int(*)(void))aSyscall[25].pCurrent)
35988
35989#if defined(HAVE_READLINK1)
35990 { "readlink", (sqlite3_syscall_ptr)readlink, 0 },
35991#else
35992 { "readlink", (sqlite3_syscall_ptr)0, 0 },
35993#endif
35994#define osReadlink((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent) ((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent)
35995
35996#if defined(HAVE_LSTAT1)
35997 { "lstat", (sqlite3_syscall_ptr)lstat, 0 },
35998#else
35999 { "lstat", (sqlite3_syscall_ptr)0, 0 },
36000#endif
36001#define osLstat((int(*)(const char*,struct stat*))aSyscall[27].pCurrent) ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)
36002
36003#if defined(__linux__1) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
36004# ifdef __ANDROID__
36005 { "ioctl", (sqlite3_syscall_ptr)(int(*)(int, int, ...))ioctl, 0 },
36006#define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent)
36007# else
36008 { "ioctl", (sqlite3_syscall_ptr)ioctl, 0 },
36009#define osIoctl ((int(*)(int,unsigned long,...))aSyscall[28].pCurrent)
36010# endif
36011#else
36012 { "ioctl", (sqlite3_syscall_ptr)0, 0 },
36013#endif
36014
36015}; /* End of the overrideable system calls */
36016
36017
36018/*
36019** On some systems, calls to fchown() will trigger a message in a security
36020** log if they come from non-root processes. So avoid calling fchown() if
36021** we are not running as root.
36022*/
36023static int robustFchown(int fd, uid_t uid, gid_t gid){
36024#if defined(HAVE_FCHOWN1)
36025 return osGeteuid((uid_t(*)(void))aSyscall[21].pCurrent)() ? 0 : osFchown((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)(fd,uid,gid);
36026#else
36027 return 0;
36028#endif
36029}
36030
36031/*
36032** This is the xSetSystemCall() method of sqlite3_vfs for all of the
36033** "unix" VFSes. Return SQLITE_OK opon successfully updating the
36034** system call pointer, or SQLITE_NOTFOUND if there is no configurable
36035** system call named zName.
36036*/
36037static int unixSetSystemCall(
36038 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
36039 const char *zName, /* Name of system call to override */
36040 sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
36041){
36042 unsigned int i;
36043 int rc = SQLITE_NOTFOUND12;
36044
36045 UNUSED_PARAMETER(pNotUsed)(void)(pNotUsed);
36046 if( zName==0 ){
36047 /* If no zName is given, restore all system calls to their default
36048 ** settings and return NULL
36049 */
36050 rc = SQLITE_OK0;
36051 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
36052 if( aSyscall[i].pDefault ){
36053 aSyscall[i].pCurrent = aSyscall[i].pDefault;
36054 }
36055 }
36056 }else{
36057 /* If zName is specified, operate on only the one system call
36058 ** specified.
36059 */
36060 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
36061 if( strcmp(zName, aSyscall[i].zName)==0 ){
36062 if( aSyscall[i].pDefault==0 ){
36063 aSyscall[i].pDefault = aSyscall[i].pCurrent;
36064 }
36065 rc = SQLITE_OK0;
36066 if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
36067 aSyscall[i].pCurrent = pNewFunc;
36068 break;
36069 }
36070 }
36071 }
36072 return rc;
36073}
36074
36075/*
36076** Return the value of a system call. Return NULL if zName is not a
36077** recognized system call name. NULL is also returned if the system call
36078** is currently undefined.
36079*/
36080static sqlite3_syscall_ptr unixGetSystemCall(
36081 sqlite3_vfs *pNotUsed,
36082 const char *zName
36083){
36084 unsigned int i;
36085
36086 UNUSED_PARAMETER(pNotUsed)(void)(pNotUsed);
36087 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
36088 if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
36089 }
36090 return 0;
36091}
36092
36093/*
36094** Return the name of the first system call after zName. If zName==NULL
36095** then return the name of the first system call. Return NULL if zName
36096** is the last system call or if zName is not the name of a valid
36097** system call.
36098*/
36099static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
36100 int i = -1;
36101
36102 UNUSED_PARAMETER(p)(void)(p);
36103 if( zName ){
36104 for(i=0; i<ArraySize(aSyscall)((int)(sizeof(aSyscall)/sizeof(aSyscall[0])))-1; i++){
36105 if( strcmp(zName, aSyscall[i].zName)==0 ) break;
36106 }
36107 }
36108 for(i++; i<ArraySize(aSyscall)((int)(sizeof(aSyscall)/sizeof(aSyscall[0]))); i++){
36109 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
36110 }
36111 return 0;
36112}
36113
36114/*
36115** Do not accept any file descriptor less than this value, in order to avoid
36116** opening database file using file descriptors that are commonly used for
36117** standard input, output, and error.
36118*/
36119#ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR3
36120# define SQLITE_MINIMUM_FILE_DESCRIPTOR3 3
36121#endif
36122
36123/*
36124** Invoke open(). Do so multiple times, until it either succeeds or
36125** fails for some reason other than EINTR.
36126**
36127** If the file creation mode "m" is 0 then set it to the default for
36128** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally
36129** 0644) as modified by the system umask. If m is not 0, then
36130** make the file creation mode be exactly m ignoring the umask.
36131**
36132** The m parameter will be non-zero only when creating -wal, -journal,
36133** and -shm files. We want those files to have *exactly* the same
36134** permissions as their original database, unadulterated by the umask.
36135** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
36136** transaction crashes and leaves behind hot journals, then any
36137** process that is able to write to the database will also be able to
36138** recover the hot journals.
36139*/
36140static int robust_open(const char *z, int f, mode_t m){
36141 int fd;
36142 mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS0644;
36143 while(1){
36144#if defined(O_CLOEXEC02000000)
36145 fd = osOpen((int(*)(const char*,int,int))aSyscall[0].pCurrent)(z,f|O_CLOEXEC02000000,m2);
36146#else
36147 fd = osOpen((int(*)(const char*,int,int))aSyscall[0].pCurrent)(z,f,m2);
36148#endif
36149 if( fd<0 ){
36150 if( errno(*__errno_location ())==EINTR4 ) continue;
36151 break;
36152 }
36153 if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR3 ) break;
36154 osClose((int(*)(int))aSyscall[1].pCurrent)(fd);
36155 sqlite3_log(SQLITE_WARNING28,
36156 "attempt to open \"%s\" as file descriptor %d", z, fd);
36157 fd = -1;
36158 if( osOpen((int(*)(const char*,int,int))aSyscall[0].pCurrent)("/dev/null", O_RDONLY00, m)<0 ) break;
36159 }
36160 if( fd>=0 ){
36161 if( m!=0 ){
36162 struct stat statbuf;
36163 if( osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(fd, &statbuf)==0
36164 && statbuf.st_size==0
36165 && (statbuf.st_mode&0777)!=m
36166 ){
36167 osFchmod((int(*)(int,mode_t))aSyscall[14].pCurrent)(fd, m);
36168 }
36169 }
36170#if defined(FD_CLOEXEC1) && (!defined(O_CLOEXEC02000000) || O_CLOEXEC02000000==0)
36171 osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(fd, F_SETFD2, osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(fd, F_GETFD1, 0) | FD_CLOEXEC1);
36172#endif
36173 }
36174 return fd;
36175}
36176
36177/*
36178** Helper functions to obtain and relinquish the global mutex. The
36179** global mutex is used to protect the unixInodeInfo and
36180** vxworksFileId objects used by this file, all of which may be
36181** shared by multiple threads.
36182**
36183** Function unixMutexHeld() is used to assert() that the global mutex
36184** is held when required. This function is only used as part of assert()
36185** statements. e.g.
36186**
36187** unixEnterMutex()
36188** assert( unixMutexHeld() );
36189** unixEnterLeave()
36190**
36191** To prevent deadlock, the global unixBigLock must must be acquired
36192** before the unixInodeInfo.pLockMutex mutex, if both are held. It is
36193** OK to get the pLockMutex without holding unixBigLock first, but if
36194** that happens, the unixBigLock mutex must not be acquired until after
36195** pLockMutex is released.
36196**
36197** OK: enter(unixBigLock), enter(pLockInfo)
36198** OK: enter(unixBigLock)
36199** OK: enter(pLockInfo)
36200** ERROR: enter(pLockInfo), enter(unixBigLock)
36201*/
36202static sqlite3_mutex *unixBigLock = 0;
36203static void unixEnterMutex(void){
36204 assert( sqlite3_mutex_notheld(unixBigLock) )((void) (0)); /* Not a recursive mutex */
36205 sqlite3_mutex_enter(unixBigLock);
36206}
36207static void unixLeaveMutex(void){
36208 assert( sqlite3_mutex_held(unixBigLock) )((void) (0));
36209 sqlite3_mutex_leave(unixBigLock);
36210}
36211#ifdef SQLITE_DEBUG
36212static int unixMutexHeld(void) {
36213 return sqlite3_mutex_held(unixBigLock);
36214}
36215#endif
36216
36217
36218#ifdef SQLITE_HAVE_OS_TRACE
36219/*
36220** Helper function for printing out trace information from debugging
36221** binaries. This returns the string representation of the supplied
36222** integer lock-type.
36223*/
36224static const char *azFileLock(int eFileLock){
36225 switch( eFileLock ){
36226 case NO_LOCK0: return "NONE";
36227 case SHARED_LOCK1: return "SHARED";
36228 case RESERVED_LOCK2: return "RESERVED";
36229 case PENDING_LOCK3: return "PENDING";
36230 case EXCLUSIVE_LOCK4: return "EXCLUSIVE";
36231 }
36232 return "ERROR";
36233}
36234#endif
36235
36236#ifdef SQLITE_LOCK_TRACE
36237/*
36238** Print out information about all locking operations.
36239**
36240** This routine is used for troubleshooting locks on multithreaded
36241** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
36242** command-line option on the compiler. This code is normally
36243** turned off.
36244*/
36245static int lockTrace(int fd, int op, struct flock *p){
36246 char *zOpName, *zType;
36247 int s;
36248 int savedErrno;
36249 if( op==F_GETLK5 ){
36250 zOpName = "GETLK";
36251 }else if( op==F_SETLK6 ){
36252 zOpName = "SETLK";
36253 }else{
36254 s = osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(fd, op, p);
36255 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
36256 return s;
36257 }
36258 if( p->l_type==F_RDLCK0 ){
36259 zType = "RDLCK";
36260 }else if( p->l_type==F_WRLCK1 ){
36261 zType = "WRLCK";
36262 }else if( p->l_type==F_UNLCK2 ){
36263 zType = "UNLCK";
36264 }else{
36265 assert( 0 )((void) (0));
36266 }
36267 assert( p->l_whence==SEEK_SET )((void) (0));
36268 s = osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(fd, op, p);
36269 savedErrno = errno(*__errno_location ());
36270 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
36271 threadidpthread_self(), fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
36272 (int)p->l_pid, s);
36273 if( s==(-1) && op==F_SETLK6 && (p->l_type==F_RDLCK0 || p->l_type==F_WRLCK1) ){
36274 struct flock l2;
36275 l2 = *p;
36276 osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(fd, F_GETLK5, &l2);
36277 if( l2.l_type==F_RDLCK0 ){
36278 zType = "RDLCK";
36279 }else if( l2.l_type==F_WRLCK1 ){
36280 zType = "WRLCK";
36281 }else if( l2.l_type==F_UNLCK2 ){
36282 zType = "UNLCK";
36283 }else{
36284 assert( 0 )((void) (0));
36285 }
36286 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
36287 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
36288 }
36289 errno(*__errno_location ()) = savedErrno;
36290 return s;
36291}
36292#undef osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)
36293#define osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent) lockTrace
36294#endif /* SQLITE_LOCK_TRACE */
36295
36296/*
36297** Retry ftruncate() calls that fail due to EINTR
36298**
36299** All calls to ftruncate() within this file should be made through
36300** this wrapper. On the Android platform, bypassing the logic below
36301** could lead to a corrupt database.
36302*/
36303static int robust_ftruncate(int h, sqlite3_int64 sz){
36304 int rc;
36305#ifdef __ANDROID__
36306 /* On Android, ftruncate() always uses 32-bit offsets, even if
36307 ** _FILE_OFFSET_BITS=64 is defined. This means it is unsafe to attempt to
36308 ** truncate a file to any size larger than 2GiB. Silently ignore any
36309 ** such attempts. */
36310 if( sz>(sqlite3_int64)0x7FFFFFFF ){
36311 rc = SQLITE_OK0;
36312 }else
36313#endif
36314 do{ rc = osFtruncate((int(*)(int,off_t))aSyscall[6].pCurrent)(h,sz); }while( rc<0 && errno(*__errno_location ())==EINTR4 );
36315 return rc;
36316}
36317
36318/*
36319** This routine translates a standard POSIX errno code into something
36320** useful to the clients of the sqlite3 functions. Specifically, it is
36321** intended to translate a variety of "try again" errors into SQLITE_BUSY
36322** and a variety of "please close the file descriptor NOW" errors into
36323** SQLITE_IOERR
36324**
36325** Errors during initialization of locks, or file system support for locks,
36326** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
36327*/
36328static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
36329 assert( (sqliteIOErr == SQLITE_IOERR_LOCK) ||((void) (0))
36330 (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||((void) (0))
36331 (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||((void) (0))
36332 (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) )((void) (0));
36333 switch (posixError) {
36334 case EACCES13:
36335 case EAGAIN11:
36336 case ETIMEDOUT110:
36337 case EBUSY16:
36338 case EINTR4:
36339 case ENOLCK37:
36340 /* random NFS retry error, unless during file system support
36341 * introspection, in which it actually means what it says */
36342 return SQLITE_BUSY5;
36343
36344 case EPERM1:
36345 return SQLITE_PERM3;
36346
36347 default:
36348 return sqliteIOErr;
36349 }
36350}
36351
36352
36353/******************************************************************************
36354****************** Begin Unique File ID Utility Used By VxWorks ***************
36355**
36356** On most versions of unix, we can get a unique ID for a file by concatenating
36357** the device number and the inode number. But this does not work on VxWorks.
36358** On VxWorks, a unique file id must be based on the canonical filename.
36359**
36360** A pointer to an instance of the following structure can be used as a
36361** unique file ID in VxWorks. Each instance of this structure contains
36362** a copy of the canonical filename. There is also a reference count.
36363** The structure is reclaimed when the number of pointers to it drops to
36364** zero.
36365**
36366** There are never very many files open at one time and lookups are not
36367** a performance-critical path, so it is sufficient to put these
36368** structures on a linked list.
36369*/
36370struct vxworksFileId {
36371 struct vxworksFileId *pNext; /* Next in a list of them all */
36372 int nRef; /* Number of references to this one */
36373 int nName; /* Length of the zCanonicalName[] string */
36374 char *zCanonicalName; /* Canonical filename */
36375};
36376
36377#if OS_VXWORKS0
36378/*
36379** All unique filenames are held on a linked list headed by this
36380** variable:
36381*/
36382static struct vxworksFileId *vxworksFileList = 0;
36383
36384/*
36385** Simplify a filename into its canonical form
36386** by making the following changes:
36387**
36388** * removing any trailing and duplicate /
36389** * convert /./ into just /
36390** * convert /A/../ where A is any simple name into just /
36391**
36392** Changes are made in-place. Return the new name length.
36393**
36394** The original filename is in z[0..n-1]. Return the number of
36395** characters in the simplified name.
36396*/
36397static int vxworksSimplifyName(char *z, int n){
36398 int i, j;
36399 while( n>1 && z[n-1]=='/' ){ n--; }
36400 for(i=j=0; i<n; i++){
36401 if( z[i]=='/' ){
36402 if( z[i+1]=='/' ) continue;
36403 if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
36404 i += 1;
36405 continue;
36406 }
36407 if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
36408 while( j>0 && z[j-1]!='/' ){ j--; }
36409 if( j>0 ){ j--; }
36410 i += 2;
36411 continue;
36412 }
36413 }
36414 z[j++] = z[i];
36415 }
36416 z[j] = 0;
36417 return j;
36418}
36419
36420/*
36421** Find a unique file ID for the given absolute pathname. Return
36422** a pointer to the vxworksFileId object. This pointer is the unique
36423** file ID.
36424**
36425** The nRef field of the vxworksFileId object is incremented before
36426** the object is returned. A new vxworksFileId object is created
36427** and added to the global list if necessary.
36428**
36429** If a memory allocation error occurs, return NULL.
36430*/
36431static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
36432 struct vxworksFileId *pNew; /* search key and new file ID */
36433 struct vxworksFileId *pCandidate; /* For looping over existing file IDs */
36434 int n; /* Length of zAbsoluteName string */
36435
36436 assert( zAbsoluteName[0]=='/' )((void) (0));
36437 n = (int)strlen(zAbsoluteName);
36438 pNew = sqlite3_malloc64( sizeof(*pNew) + (n+1) );
36439 if( pNew==0 ) return 0;
36440 pNew->zCanonicalName = (char*)&pNew[1];
36441 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
36442 n = vxworksSimplifyName(pNew->zCanonicalName, n);
36443
36444 /* Search for an existing entry that matching the canonical name.
36445 ** If found, increment the reference count and return a pointer to
36446 ** the existing file ID.
36447 */
36448 unixEnterMutex();
36449 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
36450 if( pCandidate->nName==n
36451 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
36452 ){
36453 sqlite3_free(pNew);
36454 pCandidate->nRef++;
36455 unixLeaveMutex();
36456 return pCandidate;
36457 }
36458 }
36459
36460 /* No match was found. We will make a new file ID */
36461 pNew->nRef = 1;
36462 pNew->nName = n;
36463 pNew->pNext = vxworksFileList;
36464 vxworksFileList = pNew;
36465 unixLeaveMutex();
36466 return pNew;
36467}
36468
36469/*
36470** Decrement the reference count on a vxworksFileId object. Free
36471** the object when the reference count reaches zero.
36472*/
36473static void vxworksReleaseFileId(struct vxworksFileId *pId){
36474 unixEnterMutex();
36475 assert( pId->nRef>0 )((void) (0));
36476 pId->nRef--;
36477 if( pId->nRef==0 ){
36478 struct vxworksFileId **pp;
36479 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
36480 assert( *pp==pId )((void) (0));
36481 *pp = pId->pNext;
36482 sqlite3_free(pId);
36483 }
36484 unixLeaveMutex();
36485}
36486#endif /* OS_VXWORKS */
36487/*************** End of Unique File ID Utility Used By VxWorks ****************
36488******************************************************************************/
36489
36490
36491/******************************************************************************
36492*************************** Posix Advisory Locking ****************************
36493**
36494** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996)
36495** section 6.5.2.2 lines 483 through 490 specify that when a process
36496** sets or clears a lock, that operation overrides any prior locks set
36497** by the same process. It does not explicitly say so, but this implies
36498** that it overrides locks set by the same process using a different
36499** file descriptor. Consider this test case:
36500**
36501** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
36502** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
36503**
36504** Suppose ./file1 and ./file2 are really the same file (because
36505** one is a hard or symbolic link to the other) then if you set
36506** an exclusive lock on fd1, then try to get an exclusive lock
36507** on fd2, it works. I would have expected the second lock to
36508** fail since there was already a lock on the file due to fd1.
36509** But not so. Since both locks came from the same process, the
36510** second overrides the first, even though they were on different
36511** file descriptors opened on different file names.
36512**
36513** This means that we cannot use POSIX locks to synchronize file access
36514** among competing threads of the same process. POSIX locks will work fine
36515** to synchronize access for threads in separate processes, but not
36516** threads within the same process.
36517**
36518** To work around the problem, SQLite has to manage file locks internally
36519** on its own. Whenever a new database is opened, we have to find the
36520** specific inode of the database file (the inode is determined by the
36521** st_dev and st_ino fields of the stat structure that fstat() fills in)
36522** and check for locks already existing on that inode. When locks are
36523** created or removed, we have to look at our own internal record of the
36524** locks to see if another thread has previously set a lock on that same
36525** inode.
36526**
36527** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
36528** For VxWorks, we have to use the alternative unique ID system based on
36529** canonical filename and implemented in the previous division.)
36530**
36531** The sqlite3_file structure for POSIX is no longer just an integer file
36532** descriptor. It is now a structure that holds the integer file
36533** descriptor and a pointer to a structure that describes the internal
36534** locks on the corresponding inode. There is one locking structure
36535** per inode, so if the same inode is opened twice, both unixFile structures
36536** point to the same locking structure. The locking structure keeps
36537** a reference count (so we will know when to delete it) and a "cnt"
36538** field that tells us its internal lock status. cnt==0 means the
36539** file is unlocked. cnt==-1 means the file has an exclusive lock.
36540** cnt>0 means there are cnt shared locks on the file.
36541**
36542** Any attempt to lock or unlock a file first checks the locking
36543** structure. The fcntl() system call is only invoked to set a
36544** POSIX lock if the internal lock structure transitions between
36545** a locked and an unlocked state.
36546**
36547** But wait: there are yet more problems with POSIX advisory locks.
36548**
36549** If you close a file descriptor that points to a file that has locks,
36550** all locks on that file that are owned by the current process are
36551** released. To work around this problem, each unixInodeInfo object
36552** maintains a count of the number of pending locks on tha inode.
36553** When an attempt is made to close an unixFile, if there are
36554** other unixFile open on the same inode that are holding locks, the call
36555** to close() the file descriptor is deferred until all of the locks clear.
36556** The unixInodeInfo structure keeps a list of file descriptors that need to
36557** be closed and that list is walked (and cleared) when the last lock
36558** clears.
36559**
36560** Yet another problem: LinuxThreads do not play well with posix locks.
36561**
36562** Many older versions of linux use the LinuxThreads library which is
36563** not posix compliant. Under LinuxThreads, a lock created by thread
36564** A cannot be modified or overridden by a different thread B.
36565** Only thread A can modify the lock. Locking behavior is correct
36566** if the appliation uses the newer Native Posix Thread Library (NPTL)
36567** on linux - with NPTL a lock created by thread A can override locks
36568** in thread B. But there is no way to know at compile-time which
36569** threading library is being used. So there is no way to know at
36570** compile-time whether or not thread A can override locks on thread B.
36571** One has to do a run-time check to discover the behavior of the
36572** current process.
36573**
36574** SQLite used to support LinuxThreads. But support for LinuxThreads
36575** was dropped beginning with version 3.7.0. SQLite will still work with
36576** LinuxThreads provided that (1) there is no more than one connection
36577** per database file in the same process and (2) database connections
36578** do not move across threads.
36579*/
36580
36581/*
36582** An instance of the following structure serves as the key used
36583** to locate a particular unixInodeInfo object.
36584*/
36585struct unixFileId {
36586 dev_t dev; /* Device number */
36587#if OS_VXWORKS0
36588 struct vxworksFileId *pId; /* Unique file ID for vxworks. */
36589#else
36590 /* We are told that some versions of Android contain a bug that
36591 ** sizes ino_t at only 32-bits instead of 64-bits. (See
36592 ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
36593 ** To work around this, always allocate 64-bits for the inode number.
36594 ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
36595 ** but that should not be a big deal. */
36596 /* WAS: ino_t ino; */
36597 u64 ino; /* Inode number */
36598#endif
36599};
36600
36601/*
36602** An instance of the following structure is allocated for each open
36603** inode.
36604**
36605** A single inode can have multiple file descriptors, so each unixFile
36606** structure contains a pointer to an instance of this object and this
36607** object keeps a count of the number of unixFile pointing to it.
36608**
36609** Mutex rules:
36610**
36611** (1) Only the pLockMutex mutex must be held in order to read or write
36612** any of the locking fields:
36613** nShared, nLock, eFileLock, bProcessLock, pUnused
36614**
36615** (2) When nRef>0, then the following fields are unchanging and can
36616** be read (but not written) without holding any mutex:
36617** fileId, pLockMutex
36618**
36619** (3) With the exceptions above, all the fields may only be read
36620** or written while holding the global unixBigLock mutex.
36621**
36622** Deadlock prevention: The global unixBigLock mutex may not
36623** be acquired while holding the pLockMutex mutex. If both unixBigLock
36624** and pLockMutex are needed, then unixBigLock must be acquired first.
36625*/
36626struct unixInodeInfo {
36627 struct unixFileId fileId; /* The lookup key */
36628 sqlite3_mutex *pLockMutex; /* Hold this mutex for... */
36629 int nShared; /* Number of SHARED locks held */
36630 int nLock; /* Number of outstanding file locks */
36631 unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
36632 unsigned char bProcessLock; /* An exclusive process lock is held */
36633 UnixUnusedFd *pUnused; /* Unused file descriptors to close */
36634 int nRef; /* Number of pointers to this structure */
36635 unixShmNode *pShmNode; /* Shared memory associated with this inode */
36636 unixInodeInfo *pNext; /* List of all unixInodeInfo objects */
36637 unixInodeInfo *pPrev; /* .... doubly linked */
36638#if SQLITE_ENABLE_LOCKING_STYLE0
36639 unsigned long long sharedByte; /* for AFP simulated shared lock */
36640#endif
36641#if OS_VXWORKS0
36642 sem_t *pSem; /* Named POSIX semaphore */
36643 char aSemName[MAX_PATHNAME512+2]; /* Name of that semaphore */
36644#endif
36645};
36646
36647/*
36648** A lists of all unixInodeInfo objects.
36649**
36650** Must hold unixBigLock in order to read or write this variable.
36651*/
36652static unixInodeInfo *inodeList = 0; /* All unixInodeInfo objects */
36653
36654#ifdef SQLITE_DEBUG
36655/*
36656** True if the inode mutex (on the unixFile.pFileMutex field) is held, or not.
36657** This routine is used only within assert() to help verify correct mutex
36658** usage.
36659*/
36660int unixFileMutexHeld(unixFile *pFile){
36661 assert( pFile->pInode )((void) (0));
36662 return sqlite3_mutex_held(pFile->pInode->pLockMutex);
36663}
36664int unixFileMutexNotheld(unixFile *pFile){
36665 assert( pFile->pInode )((void) (0));
36666 return sqlite3_mutex_notheld(pFile->pInode->pLockMutex);
36667}
36668#endif
36669
36670/*
36671**
36672** This function - unixLogErrorAtLine(), is only ever called via the macro
36673** unixLogError().
36674**
36675** It is invoked after an error occurs in an OS function and errno has been
36676** set. It logs a message using sqlite3_log() containing the current value of
36677** errno and, if possible, the human-readable equivalent from strerror() or
36678** strerror_r().
36679**
36680** The first argument passed to the macro should be the error code that
36681** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
36682** The two subsequent arguments should be the name of the OS function that
36683** failed (e.g. "unlink", "open") and the associated file-system path,
36684** if any.
36685*/
36686#define unixLogError(a,b,c)unixLogErrorAtLine(a,b,c,36686) unixLogErrorAtLine(a,b,c,__LINE__36686)
36687static int unixLogErrorAtLine(
36688 int errcode, /* SQLite error code */
36689 const char *zFunc, /* Name of OS function that failed */
36690 const char *zPath, /* File path associated with error */
36691 int iLine /* Source line number where error occurred */
36692){
36693 char *zErr; /* Message from strerror() or equivalent */
36694 int iErrno = errno(*__errno_location ()); /* Saved syscall error number */
36695
36696 /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
36697 ** the strerror() function to obtain the human-readable error message
36698 ** equivalent to errno. Otherwise, use strerror_r().
36699 */
36700#if SQLITE_THREADSAFE2 && defined(HAVE_STRERROR_R)
36701 char aErr[80];
36702 memset(aErr, 0, sizeof(aErr));
36703 zErr = aErr;
36704
36705 /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
36706 ** assume that the system provides the GNU version of strerror_r() that
36707 ** returns a pointer to a buffer containing the error message. That pointer
36708 ** may point to aErr[], or it may point to some static storage somewhere.
36709 ** Otherwise, assume that the system provides the POSIX version of
36710 ** strerror_r(), which always writes an error message into aErr[].
36711 **
36712 ** If the code incorrectly assumes that it is the POSIX version that is
36713 ** available, the error message will often be an empty string. Not a
36714 ** huge problem. Incorrectly concluding that the GNU version is available
36715 ** could lead to a segfault though.
36716 */
36717#if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU1)
36718 zErr =
36719# endif
36720 strerror_r(iErrno, aErr, sizeof(aErr)-1);
36721
36722#elif SQLITE_THREADSAFE2
36723 /* This is a threadsafe build, but strerror_r() is not available. */
36724 zErr = "";
36725#else
36726 /* Non-threadsafe build, use strerror(). */
36727 zErr = strerror(iErrno);
36728#endif
36729
36730 if( zPath==0 ) zPath = "";
36731 sqlite3_log(errcode,
36732 "os_unix.c:%d: (%d) %s(%s) - %s",
36733 iLine, iErrno, zFunc, zPath, zErr
36734 );
36735
36736 return errcode;
36737}
36738
36739/*
36740** Close a file descriptor.
36741**
36742** We assume that close() almost always works, since it is only in a
36743** very sick application or on a very sick platform that it might fail.
36744** If it does fail, simply leak the file descriptor, but do log the
36745** error.
36746**
36747** Note that it is not safe to retry close() after EINTR since the
36748** file descriptor might have already been reused by another thread.
36749** So we don't even try to recover from an EINTR. Just log the error
36750** and move on.
36751*/
36752static void robust_close(unixFile *pFile, int h, int lineno){
36753 if( osClose((int(*)(int))aSyscall[1].pCurrent)(h) ){
36754 unixLogErrorAtLine(SQLITE_IOERR_CLOSE(10 | (16<<8)), "close",
36755 pFile ? pFile->zPath : 0, lineno);
36756 }
36757}
36758
36759/*
36760** Set the pFile->lastErrno. Do this in a subroutine as that provides
36761** a convenient place to set a breakpoint.
36762*/
36763static void storeLastErrno(unixFile *pFile, int error){
36764 pFile->lastErrno = error;
36765}
36766
36767/*
36768** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
36769*/
36770static void closePendingFds(unixFile *pFile){
36771 unixInodeInfo *pInode = pFile->pInode;
36772 UnixUnusedFd *p;
36773 UnixUnusedFd *pNext;
36774 assert( unixFileMutexHeld(pFile) )((void) (0));
36775 for(p=pInode->pUnused; p; p=pNext){
36776 pNext = p->pNext;
36777 robust_close(pFile, p->fd, __LINE__36777);
36778 sqlite3_free(p);
36779 }
36780 pInode->pUnused = 0;
36781}
36782
36783/*
36784** Release a unixInodeInfo structure previously allocated by findInodeInfo().
36785**
36786** The global mutex must be held when this routine is called, but the mutex
36787** on the inode being deleted must NOT be held.
36788*/
36789static void releaseInodeInfo(unixFile *pFile){
36790 unixInodeInfo *pInode = pFile->pInode;
36791 assert( unixMutexHeld() )((void) (0));
36792 assert( unixFileMutexNotheld(pFile) )((void) (0));
36793 if( ALWAYS(pInode)(pInode) ){
36794 pInode->nRef--;
36795 if( pInode->nRef==0 ){
36796 assert( pInode->pShmNode==0 )((void) (0));
36797 sqlite3_mutex_enter(pInode->pLockMutex);
36798 closePendingFds(pFile);
36799 sqlite3_mutex_leave(pInode->pLockMutex);
36800 if( pInode->pPrev ){
36801 assert( pInode->pPrev->pNext==pInode )((void) (0));
36802 pInode->pPrev->pNext = pInode->pNext;
36803 }else{
36804 assert( inodeList==pInode )((void) (0));
36805 inodeList = pInode->pNext;
36806 }
36807 if( pInode->pNext ){
36808 assert( pInode->pNext->pPrev==pInode )((void) (0));
36809 pInode->pNext->pPrev = pInode->pPrev;
36810 }
36811 sqlite3_mutex_free(pInode->pLockMutex);
36812 sqlite3_free(pInode);
36813 }
36814 }
36815}
36816
36817/*
36818** Given a file descriptor, locate the unixInodeInfo object that
36819** describes that file descriptor. Create a new one if necessary. The
36820** return value might be uninitialized if an error occurs.
36821**
36822** The global mutex must held when calling this routine.
36823**
36824** Return an appropriate error code.
36825*/
36826static int findInodeInfo(
36827 unixFile *pFile, /* Unix file with file desc used in the key */
36828 unixInodeInfo **ppInode /* Return the unixInodeInfo object here */
36829){
36830 int rc; /* System call return code */
36831 int fd; /* The file descriptor for pFile */
36832 struct unixFileId fileId; /* Lookup key for the unixInodeInfo */
36833 struct stat statbuf; /* Low-level file information */
36834 unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */
36835
36836 assert( unixMutexHeld() )((void) (0));
36837
36838 /* Get low-level information about the file that we can used to
36839 ** create a unique name for the file.
36840 */
36841 fd = pFile->h;
36842 rc = osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(fd, &statbuf);
36843 if( rc!=0 ){
36844 storeLastErrno(pFile, errno(*__errno_location ()));
36845#if defined(EOVERFLOW75) && defined(SQLITE_DISABLE_LFS)
36846 if( pFile->lastErrno==EOVERFLOW75 ) return SQLITE_NOLFS22;
36847#endif
36848 return SQLITE_IOERR10;
36849 }
36850
36851#ifdef __APPLE__
36852 /* On OS X on an msdos filesystem, the inode number is reported
36853 ** incorrectly for zero-size files. See ticket #3260. To work
36854 ** around this problem (we consider it a bug in OS X, not SQLite)
36855 ** we always increase the file size to 1 by writing a single byte
36856 ** prior to accessing the inode number. The one byte written is
36857 ** an ASCII 'S' character which also happens to be the first byte
36858 ** in the header of every SQLite database. In this way, if there
36859 ** is a race condition such that another thread has already populated
36860 ** the first page of the database, no damage is done.
36861 */
36862 if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS0x1)!=0 ){
36863 do{ rc = osWrite((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)(fd, "S", 1); }while( rc<0 && errno(*__errno_location ())==EINTR4 );
36864 if( rc!=1 ){
36865 storeLastErrno(pFile, errno(*__errno_location ()));
36866 return SQLITE_IOERR10;
36867 }
36868 rc = osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(fd, &statbuf);
36869 if( rc!=0 ){
36870 storeLastErrno(pFile, errno(*__errno_location ()));
36871 return SQLITE_IOERR10;
36872 }
36873 }
36874#endif
36875
36876 memset(&fileId, 0, sizeof(fileId));
36877 fileId.dev = statbuf.st_dev;
36878#if OS_VXWORKS0
36879 fileId.pId = pFile->pId;
36880#else
36881 fileId.ino = (u64)statbuf.st_ino;
36882#endif
36883 assert( unixMutexHeld() )((void) (0));
36884 pInode = inodeList;
36885 while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
36886 pInode = pInode->pNext;
36887 }
36888 if( pInode==0 ){
36889 pInode = sqlite3_malloc64( sizeof(*pInode) );
36890 if( pInode==0 ){
36891 return SQLITE_NOMEM_BKPT7;
36892 }
36893 memset(pInode, 0, sizeof(*pInode));
36894 memcpy(&pInode->fileId, &fileId, sizeof(fileId));
36895 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
36896 pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST0);
36897 if( pInode->pLockMutex==0 ){
36898 sqlite3_free(pInode);
36899 return SQLITE_NOMEM_BKPT7;
36900 }
36901 }
36902 pInode->nRef = 1;
36903 assert( unixMutexHeld() )((void) (0));
36904 pInode->pNext = inodeList;
36905 pInode->pPrev = 0;
36906 if( inodeList ) inodeList->pPrev = pInode;
36907 inodeList = pInode;
36908 }else{
36909 pInode->nRef++;
36910 }
36911 *ppInode = pInode;
36912 return SQLITE_OK0;
36913}
36914
36915/*
36916** Return TRUE if pFile has been renamed or unlinked since it was first opened.
36917*/
36918static int fileHasMoved(unixFile *pFile){
36919#if OS_VXWORKS0
36920 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
36921#else
36922 struct stat buf;
36923 return pFile->pInode!=0 &&
36924 (osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)(pFile->zPath, &buf)!=0
36925 || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
36926#endif
36927}
36928
36929
36930/*
36931** Check a unixFile that is a database. Verify the following:
36932**
36933** (1) There is exactly one hard link on the file
36934** (2) The file is not a symbolic link
36935** (3) The file has not been renamed or unlinked
36936**
36937** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right.
36938*/
36939static void verifyDbFile(unixFile *pFile){
36940 struct stat buf;
36941 int rc;
36942
36943 /* These verifications occurs for the main database only */
36944 if( pFile->ctrlFlags & UNIXFILE_NOLOCK0x80 ) return;
36945
36946 rc = osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(pFile->h, &buf);
36947 if( rc!=0 ){
36948 sqlite3_log(SQLITE_WARNING28, "cannot fstat db file %s", pFile->zPath);
36949 return;
36950 }
36951 if( buf.st_nlink==0 ){
36952 sqlite3_log(SQLITE_WARNING28, "file unlinked while open: %s", pFile->zPath);
36953 return;
36954 }
36955 if( buf.st_nlink>1 ){
36956 sqlite3_log(SQLITE_WARNING28, "multiple links to file: %s", pFile->zPath);
36957 return;
36958 }
36959 if( fileHasMoved(pFile) ){
36960 sqlite3_log(SQLITE_WARNING28, "file renamed while open: %s", pFile->zPath);
36961 return;
36962 }
36963}
36964
36965
36966/*
36967** This routine checks if there is a RESERVED lock held on the specified
36968** file by this or any other process. If such a lock is held, set *pResOut
36969** to a non-zero value otherwise *pResOut is set to zero. The return value
36970** is set to SQLITE_OK unless an I/O error occurs during lock checking.
36971*/
36972static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
36973 int rc = SQLITE_OK0;
36974 int reserved = 0;
36975 unixFile *pFile = (unixFile*)id;
36976
36977 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
36978
36979 assert( pFile )((void) (0));
36980 assert( pFile->eFileLock<=SHARED_LOCK )((void) (0));
36981 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
36982
36983 /* Check if a thread in this process holds such a lock */
36984 if( pFile->pInode->eFileLock>SHARED_LOCK1 ){
36985 reserved = 1;
36986 }
36987
36988 /* Otherwise see if some other process holds it.
36989 */
36990#ifndef __DJGPP__
36991 if( !reserved && !pFile->pInode->bProcessLock ){
36992 struct flock lock;
36993 lock.l_whence = SEEK_SET0;
36994 lock.l_start = RESERVED_BYTE(sqlite3PendingByte+1);
36995 lock.l_len = 1;
36996 lock.l_type = F_WRLCK1;
36997 if( osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(pFile->h, F_GETLK5, &lock) ){
36998 rc = SQLITE_IOERR_CHECKRESERVEDLOCK(10 | (14<<8));
36999 storeLastErrno(pFile, errno(*__errno_location ()));
37000 } else if( lock.l_type!=F_UNLCK2 ){
37001 reserved = 1;
37002 }
37003 }
37004#endif
37005
37006 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
37007 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
37008
37009 *pResOut = reserved;
37010 return rc;
37011}
37012
37013/* Forward declaration*/
37014static int unixSleep(sqlite3_vfs*,int);
37015
37016/*
37017** Set a posix-advisory-lock.
37018**
37019** There are two versions of this routine. If compiled with
37020** SQLITE_ENABLE_SETLK_TIMEOUT then the routine has an extra parameter
37021** which is a pointer to a unixFile. If the unixFile->iBusyTimeout
37022** value is set, then it is the number of milliseconds to wait before
37023** failing the lock. The iBusyTimeout value is always reset back to
37024** zero on each call.
37025**
37026** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
37027** attempt to set the lock.
37028*/
37029#ifndef SQLITE_ENABLE_SETLK_TIMEOUT
37030# 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)
37031#else
37032static int osSetPosixAdvisoryLock(((int(*)(int,int,...))aSyscall[7].pCurrent)(int h,6,struct flock
*pLock)
37033 int h, /* The file descriptor on which to take the lock */((int(*)(int,int,...))aSyscall[7].pCurrent)(int h,6,struct flock
*pLock)
37034 struct flock *pLock, /* The description of the lock */((int(*)(int,int,...))aSyscall[7].pCurrent)(int h,6,struct flock
*pLock)
37035 unixFile *pFile /* Structure holding timeout value */((int(*)(int,int,...))aSyscall[7].pCurrent)(int h,6,struct flock
*pLock)
37036)((int(*)(int,int,...))aSyscall[7].pCurrent)(int h,6,struct flock
*pLock)
{
37037 int tm = pFile->iBusyTimeout;
37038 int rc = osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(h,F_SETLK6,pLock);
37039 while( rc<0 && tm>0 ){
37040 /* On systems that support some kind of blocking file lock with a timeout,
37041 ** make appropriate changes here to invoke that blocking file lock. On
37042 ** generic posix, however, there is no such API. So we simply try the
37043 ** lock once every millisecond until either the timeout expires, or until
37044 ** the lock is obtained. */
37045 unixSleep(0,1000);
37046 rc = osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(h,F_SETLK6,pLock);
37047 tm--;
37048 }
37049 return rc;
37050}
37051#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */
37052
37053
37054/*
37055** Attempt to set a system-lock on the file pFile. The lock is
37056** described by pLock.
37057**
37058** If the pFile was opened read/write from unix-excl, then the only lock
37059** ever obtained is an exclusive lock, and it is obtained exactly once
37060** the first time any lock is attempted. All subsequent system locking
37061** operations become no-ops. Locking operations still happen internally,
37062** in order to coordinate access between separate database connections
37063** within this process, but all of that is handled in memory and the
37064** operating system does not participate.
37065**
37066** This function is a pass-through to fcntl(F_SETLK) if pFile is using
37067** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
37068** and is read-only.
37069**
37070** Zero is returned if the call completes successfully, or -1 if a call
37071** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
37072*/
37073static int unixFileLock(unixFile *pFile, struct flock *pLock){
37074 int rc;
37075 unixInodeInfo *pInode = pFile->pInode;
37076 assert( pInode!=0 )((void) (0));
37077 assert( sqlite3_mutex_held(pInode->pLockMutex) )((void) (0));
37078 if( (pFile->ctrlFlags & (UNIXFILE_EXCL0x01|UNIXFILE_RDONLY0x02))==UNIXFILE_EXCL0x01 ){
37079 if( pInode->bProcessLock==0 ){
37080 struct flock lock;
37081 assert( pInode->nLock==0 )((void) (0));
37082 lock.l_whence = SEEK_SET0;
37083 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2);
37084 lock.l_len = SHARED_SIZE510;
37085 lock.l_type = F_WRLCK1;
37086 rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile)((int(*)(int,int,...))aSyscall[7].pCurrent)(pFile->h,6,&
lock)
;
37087 if( rc<0 ) return rc;
37088 pInode->bProcessLock = 1;
37089 pInode->nLock++;
37090 }else{
37091 rc = 0;
37092 }
37093 }else{
37094 rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile)((int(*)(int,int,...))aSyscall[7].pCurrent)(pFile->h,6,pLock
)
;
37095 }
37096 return rc;
37097}
37098
37099/*
37100** Lock the file with the lock specified by parameter eFileLock - one
37101** of the following:
37102**
37103** (1) SHARED_LOCK
37104** (2) RESERVED_LOCK
37105** (3) PENDING_LOCK
37106** (4) EXCLUSIVE_LOCK
37107**
37108** Sometimes when requesting one lock state, additional lock states
37109** are inserted in between. The locking might fail on one of the later
37110** transitions leaving the lock state different from what it started but
37111** still short of its goal. The following chart shows the allowed
37112** transitions and the inserted intermediate states:
37113**
37114** UNLOCKED -> SHARED
37115** SHARED -> RESERVED
37116** SHARED -> (PENDING) -> EXCLUSIVE
37117** RESERVED -> (PENDING) -> EXCLUSIVE
37118** PENDING -> EXCLUSIVE
37119**
37120** This routine will only increase a lock. Use the sqlite3OsUnlock()
37121** routine to lower a locking level.
37122*/
37123static int unixLock(sqlite3_file *id, int eFileLock){
37124 /* The following describes the implementation of the various locks and
37125 ** lock transitions in terms of the POSIX advisory shared and exclusive
37126 ** lock primitives (called read-locks and write-locks below, to avoid
37127 ** confusion with SQLite lock names). The algorithms are complicated
37128 ** slightly in order to be compatible with Windows95 systems simultaneously
37129 ** accessing the same database file, in case that is ever required.
37130 **
37131 ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
37132 ** byte', each single bytes at well known offsets, and the 'shared byte
37133 ** range', a range of 510 bytes at a well known offset.
37134 **
37135 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
37136 ** byte'. If this is successful, 'shared byte range' is read-locked
37137 ** and the lock on the 'pending byte' released. (Legacy note: When
37138 ** SQLite was first developed, Windows95 systems were still very common,
37139 ** and Widnows95 lacks a shared-lock capability. So on Windows95, a
37140 ** single randomly selected by from the 'shared byte range' is locked.
37141 ** Windows95 is now pretty much extinct, but this work-around for the
37142 ** lack of shared-locks on Windows95 lives on, for backwards
37143 ** compatibility.)
37144 **
37145 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
37146 ** A RESERVED lock is implemented by grabbing a write-lock on the
37147 ** 'reserved byte'.
37148 **
37149 ** A process may only obtain a PENDING lock after it has obtained a
37150 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
37151 ** on the 'pending byte'. This ensures that no new SHARED locks can be
37152 ** obtained, but existing SHARED locks are allowed to persist. A process
37153 ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
37154 ** This property is used by the algorithm for rolling back a journal file
37155 ** after a crash.
37156 **
37157 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
37158 ** implemented by obtaining a write-lock on the entire 'shared byte
37159 ** range'. Since all other locks require a read-lock on one of the bytes
37160 ** within this range, this ensures that no other locks are held on the
37161 ** database.
37162 */
37163 int rc = SQLITE_OK0;
37164 unixFile *pFile = (unixFile*)id;
37165 unixInodeInfo *pInode;
37166 struct flock lock;
37167 int tErrno = 0;
37168
37169 assert( pFile )((void) (0));
37170 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
37171 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
37172 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
37173 osGetpid(0)));
37174
37175 /* If there is already a lock of this type or more restrictive on the
37176 ** unixFile, do nothing. Don't use the end_lock: exit path, as
37177 ** unixEnterMutex() hasn't been called yet.
37178 */
37179 if( pFile->eFileLock>=eFileLock ){
37180 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
37181 azFileLock(eFileLock)));
37182 return SQLITE_OK0;
37183 }
37184
37185 /* Make sure the locking sequence is correct.
37186 ** (1) We never move from unlocked to anything higher than shared lock.
37187 ** (2) SQLite never explicitly requests a pendig lock.
37188 ** (3) A shared lock is always held when a reserve lock is requested.
37189 */
37190 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK )((void) (0));
37191 assert( eFileLock!=PENDING_LOCK )((void) (0));
37192 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK )((void) (0));
37193
37194 /* This mutex is needed because pFile->pInode is shared across threads
37195 */
37196 pInode = pFile->pInode;
37197 sqlite3_mutex_enter(pInode->pLockMutex);
37198
37199 /* If some thread using this PID has a lock via a different unixFile*
37200 ** handle that precludes the requested lock, return BUSY.
37201 */
37202 if( (pFile->eFileLock!=pInode->eFileLock &&
37203 (pInode->eFileLock>=PENDING_LOCK3 || eFileLock>SHARED_LOCK1))
37204 ){
37205 rc = SQLITE_BUSY5;
37206 goto end_lock;
37207 }
37208
37209 /* If a SHARED lock is requested, and some thread using this PID already
37210 ** has a SHARED or RESERVED lock, then increment reference counts and
37211 ** return SQLITE_OK.
37212 */
37213 if( eFileLock==SHARED_LOCK1 &&
37214 (pInode->eFileLock==SHARED_LOCK1 || pInode->eFileLock==RESERVED_LOCK2) ){
37215 assert( eFileLock==SHARED_LOCK )((void) (0));
37216 assert( pFile->eFileLock==0 )((void) (0));
37217 assert( pInode->nShared>0 )((void) (0));
37218 pFile->eFileLock = SHARED_LOCK1;
37219 pInode->nShared++;
37220 pInode->nLock++;
37221 goto end_lock;
37222 }
37223
37224
37225 /* A PENDING lock is needed before acquiring a SHARED lock and before
37226 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
37227 ** be released.
37228 */
37229 lock.l_len = 1L;
37230 lock.l_whence = SEEK_SET0;
37231 if( eFileLock==SHARED_LOCK1
37232 || (eFileLock==EXCLUSIVE_LOCK4 && pFile->eFileLock<PENDING_LOCK3)
37233 ){
37234 lock.l_type = (eFileLock==SHARED_LOCK1?F_RDLCK0:F_WRLCK1);
37235 lock.l_start = PENDING_BYTEsqlite3PendingByte;
37236 if( unixFileLock(pFile, &lock) ){
37237 tErrno = errno(*__errno_location ());
37238 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK(10 | (15<<8)));
37239 if( rc!=SQLITE_BUSY5 ){
37240 storeLastErrno(pFile, tErrno);
37241 }
37242 goto end_lock;
37243 }
37244 }
37245
37246
37247 /* If control gets to this point, then actually go ahead and make
37248 ** operating system calls for the specified lock.
37249 */
37250 if( eFileLock==SHARED_LOCK1 ){
37251 assert( pInode->nShared==0 )((void) (0));
37252 assert( pInode->eFileLock==0 )((void) (0));
37253 assert( rc==SQLITE_OK )((void) (0));
37254
37255 /* Now get the read-lock */
37256 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2);
37257 lock.l_len = SHARED_SIZE510;
37258 if( unixFileLock(pFile, &lock) ){
37259 tErrno = errno(*__errno_location ());
37260 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK(10 | (15<<8)));
37261 }
37262
37263 /* Drop the temporary PENDING lock */
37264 lock.l_start = PENDING_BYTEsqlite3PendingByte;
37265 lock.l_len = 1L;
37266 lock.l_type = F_UNLCK2;
37267 if( unixFileLock(pFile, &lock) && rc==SQLITE_OK0 ){
37268 /* This could happen with a network mount */
37269 tErrno = errno(*__errno_location ());
37270 rc = SQLITE_IOERR_UNLOCK(10 | (8<<8));
37271 }
37272
37273 if( rc ){
37274 if( rc!=SQLITE_BUSY5 ){
37275 storeLastErrno(pFile, tErrno);
37276 }
37277 goto end_lock;
37278 }else{
37279 pFile->eFileLock = SHARED_LOCK1;
37280 pInode->nLock++;
37281 pInode->nShared = 1;
37282 }
37283 }else if( eFileLock==EXCLUSIVE_LOCK4 && pInode->nShared>1 ){
37284 /* We are trying for an exclusive lock but another thread in this
37285 ** same process is still holding a shared lock. */
37286 rc = SQLITE_BUSY5;
37287 }else{
37288 /* The request was for a RESERVED or EXCLUSIVE lock. It is
37289 ** assumed that there is a SHARED or greater lock on the file
37290 ** already.
37291 */
37292 assert( 0!=pFile->eFileLock )((void) (0));
37293 lock.l_type = F_WRLCK1;
37294
37295 assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK )((void) (0));
37296 if( eFileLock==RESERVED_LOCK2 ){
37297 lock.l_start = RESERVED_BYTE(sqlite3PendingByte+1);
37298 lock.l_len = 1L;
37299 }else{
37300 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2);
37301 lock.l_len = SHARED_SIZE510;
37302 }
37303
37304 if( unixFileLock(pFile, &lock) ){
37305 tErrno = errno(*__errno_location ());
37306 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK(10 | (15<<8)));
37307 if( rc!=SQLITE_BUSY5 ){
37308 storeLastErrno(pFile, tErrno);
37309 }
37310 }
37311 }
37312
37313
37314#ifdef SQLITE_DEBUG
37315 /* Set up the transaction-counter change checking flags when
37316 ** transitioning from a SHARED to a RESERVED lock. The change
37317 ** from SHARED to RESERVED marks the beginning of a normal
37318 ** write operation (not a hot journal rollback).
37319 */
37320 if( rc==SQLITE_OK0
37321 && pFile->eFileLock<=SHARED_LOCK1
37322 && eFileLock==RESERVED_LOCK2
37323 ){
37324 pFile->transCntrChng = 0;
37325 pFile->dbUpdate = 0;
37326 pFile->inNormalWrite = 1;
37327 }
37328#endif
37329
37330
37331 if( rc==SQLITE_OK0 ){
37332 pFile->eFileLock = eFileLock;
37333 pInode->eFileLock = eFileLock;
37334 }else if( eFileLock==EXCLUSIVE_LOCK4 ){
37335 pFile->eFileLock = PENDING_LOCK3;
37336 pInode->eFileLock = PENDING_LOCK3;
37337 }
37338
37339end_lock:
37340 sqlite3_mutex_leave(pInode->pLockMutex);
37341 OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
37342 rc==SQLITE_OK ? "ok" : "failed"));
37343 return rc;
37344}
37345
37346/*
37347** Add the file descriptor used by file handle pFile to the corresponding
37348** pUnused list.
37349*/
37350static void setPendingFd(unixFile *pFile){
37351 unixInodeInfo *pInode = pFile->pInode;
37352 UnixUnusedFd *p = pFile->pPreallocatedUnused;
37353 assert( unixFileMutexHeld(pFile) )((void) (0));
37354 p->pNext = pInode->pUnused;
37355 pInode->pUnused = p;
37356 pFile->h = -1;
37357 pFile->pPreallocatedUnused = 0;
37358}
37359
37360/*
37361** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
37362** must be either NO_LOCK or SHARED_LOCK.
37363**
37364** If the locking level of the file descriptor is already at or below
37365** the requested locking level, this routine is a no-op.
37366**
37367** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
37368** the byte range is divided into 2 parts and the first part is unlocked then
37369** set to a read lock, then the other part is simply unlocked. This works
37370** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
37371** remove the write lock on a region when a read lock is set.
37372*/
37373static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
37374 unixFile *pFile = (unixFile*)id;
37375 unixInodeInfo *pInode;
37376 struct flock lock;
37377 int rc = SQLITE_OK0;
37378
37379 assert( pFile )((void) (0));
37380 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
37381 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
37382 osGetpid(0)));
37383
37384 assert( eFileLock<=SHARED_LOCK )((void) (0));
37385 if( pFile->eFileLock<=eFileLock ){
37386 return SQLITE_OK0;
37387 }
37388 pInode = pFile->pInode;
37389 sqlite3_mutex_enter(pInode->pLockMutex);
37390 assert( pInode->nShared!=0 )((void) (0));
37391 if( pFile->eFileLock>SHARED_LOCK1 ){
37392 assert( pInode->eFileLock==pFile->eFileLock )((void) (0));
37393
37394#ifdef SQLITE_DEBUG
37395 /* When reducing a lock such that other processes can start
37396 ** reading the database file again, make sure that the
37397 ** transaction counter was updated if any part of the database
37398 ** file changed. If the transaction counter is not updated,
37399 ** other connections to the same file might not realize that
37400 ** the file has changed and hence might not know to flush their
37401 ** cache. The use of a stale cache can lead to database corruption.
37402 */
37403 pFile->inNormalWrite = 0;
37404#endif
37405
37406 /* downgrading to a shared lock on NFS involves clearing the write lock
37407 ** before establishing the readlock - to avoid a race condition we downgrade
37408 ** the lock in 2 blocks, so that part of the range will be covered by a
37409 ** write lock until the rest is covered by a read lock:
37410 ** 1: [WWWWW]
37411 ** 2: [....W]
37412 ** 3: [RRRRW]
37413 ** 4: [RRRR.]
37414 */
37415 if( eFileLock==SHARED_LOCK1 ){
37416#if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE0
37417 (void)handleNFSUnlock;
37418 assert( handleNFSUnlock==0 )((void) (0));
37419#endif
37420#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
37421 if( handleNFSUnlock ){
37422 int tErrno; /* Error code from system call errors */
37423 off_t divSize = SHARED_SIZE510 - 1;
37424
37425 lock.l_type = F_UNLCK2;
37426 lock.l_whence = SEEK_SET0;
37427 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2);
37428 lock.l_len = divSize;
37429 if( unixFileLock(pFile, &lock)==(-1) ){
37430 tErrno = errno(*__errno_location ());
37431 rc = SQLITE_IOERR_UNLOCK(10 | (8<<8));
37432 storeLastErrno(pFile, tErrno);
37433 goto end_unlock;
37434 }
37435 lock.l_type = F_RDLCK0;
37436 lock.l_whence = SEEK_SET0;
37437 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2);
37438 lock.l_len = divSize;
37439 if( unixFileLock(pFile, &lock)==(-1) ){
37440 tErrno = errno(*__errno_location ());
37441 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK(10 | (9<<8)));
37442 if( IS_LOCK_ERROR(rc)((rc != 0) && (rc != 5)) ){
37443 storeLastErrno(pFile, tErrno);
37444 }
37445 goto end_unlock;
37446 }
37447 lock.l_type = F_UNLCK2;
37448 lock.l_whence = SEEK_SET0;
37449 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2)+divSize;
37450 lock.l_len = SHARED_SIZE510-divSize;
37451 if( unixFileLock(pFile, &lock)==(-1) ){
37452 tErrno = errno(*__errno_location ());
37453 rc = SQLITE_IOERR_UNLOCK(10 | (8<<8));
37454 storeLastErrno(pFile, tErrno);
37455 goto end_unlock;
37456 }
37457 }else
37458#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
37459 {
37460 lock.l_type = F_RDLCK0;
37461 lock.l_whence = SEEK_SET0;
37462 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2);
37463 lock.l_len = SHARED_SIZE510;
37464 if( unixFileLock(pFile, &lock) ){
37465 /* In theory, the call to unixFileLock() cannot fail because another
37466 ** process is holding an incompatible lock. If it does, this
37467 ** indicates that the other process is not following the locking
37468 ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
37469 ** SQLITE_BUSY would confuse the upper layer (in practice it causes
37470 ** an assert to fail). */
37471 rc = SQLITE_IOERR_RDLOCK(10 | (9<<8));
37472 storeLastErrno(pFile, errno(*__errno_location ()));
37473 goto end_unlock;
37474 }
37475 }
37476 }
37477 lock.l_type = F_UNLCK2;
37478 lock.l_whence = SEEK_SET0;
37479 lock.l_start = PENDING_BYTEsqlite3PendingByte;
37480 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE )((void) (0));
37481 if( unixFileLock(pFile, &lock)==0 ){
37482 pInode->eFileLock = SHARED_LOCK1;
37483 }else{
37484 rc = SQLITE_IOERR_UNLOCK(10 | (8<<8));
37485 storeLastErrno(pFile, errno(*__errno_location ()));
37486 goto end_unlock;
37487 }
37488 }
37489 if( eFileLock==NO_LOCK0 ){
37490 /* Decrement the shared lock counter. Release the lock using an
37491 ** OS call only when all threads in this same process have released
37492 ** the lock.
37493 */
37494 pInode->nShared--;
37495 if( pInode->nShared==0 ){
37496 lock.l_type = F_UNLCK2;
37497 lock.l_whence = SEEK_SET0;
37498 lock.l_start = lock.l_len = 0L;
37499 if( unixFileLock(pFile, &lock)==0 ){
37500 pInode->eFileLock = NO_LOCK0;
37501 }else{
37502 rc = SQLITE_IOERR_UNLOCK(10 | (8<<8));
37503 storeLastErrno(pFile, errno(*__errno_location ()));
37504 pInode->eFileLock = NO_LOCK0;
37505 pFile->eFileLock = NO_LOCK0;
37506 }
37507 }
37508
37509 /* Decrement the count of locks against this same file. When the
37510 ** count reaches zero, close any other file descriptors whose close
37511 ** was deferred because of outstanding locks.
37512 */
37513 pInode->nLock--;
37514 assert( pInode->nLock>=0 )((void) (0));
37515 if( pInode->nLock==0 ) closePendingFds(pFile);
37516 }
37517
37518end_unlock:
37519 sqlite3_mutex_leave(pInode->pLockMutex);
37520 if( rc==SQLITE_OK0 ){
37521 pFile->eFileLock = eFileLock;
37522 }
37523 return rc;
37524}
37525
37526/*
37527** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
37528** must be either NO_LOCK or SHARED_LOCK.
37529**
37530** If the locking level of the file descriptor is already at or below
37531** the requested locking level, this routine is a no-op.
37532*/
37533static int unixUnlock(sqlite3_file *id, int eFileLock){
37534#if SQLITE_MAX_MMAP_SIZE20971520>0
37535 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 )((void) (0));
37536#endif
37537 return posixUnlock(id, eFileLock, 0);
37538}
37539
37540#if SQLITE_MAX_MMAP_SIZE20971520>0
37541static int unixMapfile(unixFile *pFd, i64 nByte);
37542static void unixUnmapfile(unixFile *pFd);
37543#endif
37544
37545/*
37546** This function performs the parts of the "close file" operation
37547** common to all locking schemes. It closes the directory and file
37548** handles, if they are valid, and sets all fields of the unixFile
37549** structure to 0.
37550**
37551** It is *not* necessary to hold the mutex when this routine is called,
37552** even on VxWorks. A mutex will be acquired on VxWorks by the
37553** vxworksReleaseFileId() routine.
37554*/
37555static int closeUnixFile(sqlite3_file *id){
37556 unixFile *pFile = (unixFile*)id;
37557#if SQLITE_MAX_MMAP_SIZE20971520>0
37558 unixUnmapfile(pFile);
37559#endif
37560 if( pFile->h>=0 ){
37561 robust_close(pFile, pFile->h, __LINE__37561);
37562 pFile->h = -1;
37563 }
37564#if OS_VXWORKS0
37565 if( pFile->pId ){
37566 if( pFile->ctrlFlags & UNIXFILE_DELETE0x20 ){
37567 osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(pFile->pId->zCanonicalName);
37568 }
37569 vxworksReleaseFileId(pFile->pId);
37570 pFile->pId = 0;
37571 }
37572#endif
37573#ifdef SQLITE_UNLINK_AFTER_CLOSE
37574 if( pFile->ctrlFlags & UNIXFILE_DELETE0x20 ){
37575 osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(pFile->zPath);
37576 sqlite3_free(*(char**)&pFile->zPath);
37577 pFile->zPath = 0;
37578 }
37579#endif
37580 OSTRACE(("CLOSE %-3d\n", pFile->h));
37581 OpenCounter(-1);
37582 sqlite3_free(pFile->pPreallocatedUnused);
37583 memset(pFile, 0, sizeof(unixFile));
37584 return SQLITE_OK0;
37585}
37586
37587/*
37588** Close a file.
37589*/
37590static int unixClose(sqlite3_file *id){
37591 int rc = SQLITE_OK0;
37592 unixFile *pFile = (unixFile *)id;
37593 unixInodeInfo *pInode = pFile->pInode;
37594
37595 assert( pInode!=0 )((void) (0));
37596 verifyDbFile(pFile);
37597 unixUnlock(id, NO_LOCK0);
37598 assert( unixFileMutexNotheld(pFile) )((void) (0));
37599 unixEnterMutex();
37600
37601 /* unixFile.pInode is always valid here. Otherwise, a different close
37602 ** routine (e.g. nolockClose()) would be called instead.
37603 */
37604 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 )((void) (0));
37605 sqlite3_mutex_enter(pInode->pLockMutex);
37606 if( pInode->nLock ){
37607 /* If there are outstanding locks, do not actually close the file just
37608 ** yet because that would clear those locks. Instead, add the file
37609 ** descriptor to pInode->pUnused list. It will be automatically closed
37610 ** when the last lock is cleared.
37611 */
37612 setPendingFd(pFile);
37613 }
37614 sqlite3_mutex_leave(pInode->pLockMutex);
37615 releaseInodeInfo(pFile);
37616 assert( pFile->pShm==0 )((void) (0));
37617 rc = closeUnixFile(id);
37618 unixLeaveMutex();
37619 return rc;
37620}
37621
37622/************** End of the posix advisory lock implementation *****************
37623******************************************************************************/
37624
37625/******************************************************************************
37626****************************** No-op Locking **********************************
37627**
37628** Of the various locking implementations available, this is by far the
37629** simplest: locking is ignored. No attempt is made to lock the database
37630** file for reading or writing.
37631**
37632** This locking mode is appropriate for use on read-only databases
37633** (ex: databases that are burned into CD-ROM, for example.) It can
37634** also be used if the application employs some external mechanism to
37635** prevent simultaneous access of the same database by two or more
37636** database connections. But there is a serious risk of database
37637** corruption if this locking mode is used in situations where multiple
37638** database connections are accessing the same database file at the same
37639** time and one or more of those connections are writing.
37640*/
37641
37642static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
37643 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
37644 *pResOut = 0;
37645 return SQLITE_OK0;
37646}
37647static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
37648 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
37649 return SQLITE_OK0;
37650}
37651static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
37652 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
37653 return SQLITE_OK0;
37654}
37655
37656/*
37657** Close the file.
37658*/
37659static int nolockClose(sqlite3_file *id) {
37660 return closeUnixFile(id);
37661}
37662
37663/******************* End of the no-op lock implementation *********************
37664******************************************************************************/
37665
37666/******************************************************************************
37667************************* Begin dot-file Locking ******************************
37668**
37669** The dotfile locking implementation uses the existence of separate lock
37670** files (really a directory) to control access to the database. This works
37671** on just about every filesystem imaginable. But there are serious downsides:
37672**
37673** (1) There is zero concurrency. A single reader blocks all other
37674** connections from reading or writing the database.
37675**
37676** (2) An application crash or power loss can leave stale lock files
37677** sitting around that need to be cleared manually.
37678**
37679** Nevertheless, a dotlock is an appropriate locking mode for use if no
37680** other locking strategy is available.
37681**
37682** Dotfile locking works by creating a subdirectory in the same directory as
37683** the database and with the same name but with a ".lock" extension added.
37684** The existence of a lock directory implies an EXCLUSIVE lock. All other
37685** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
37686*/
37687
37688/*
37689** The file suffix added to the data base filename in order to create the
37690** lock directory.
37691*/
37692#define DOTLOCK_SUFFIX".lock" ".lock"
37693
37694/*
37695** This routine checks if there is a RESERVED lock held on the specified
37696** file by this or any other process. If such a lock is held, set *pResOut
37697** to a non-zero value otherwise *pResOut is set to zero. The return value
37698** is set to SQLITE_OK unless an I/O error occurs during lock checking.
37699**
37700** In dotfile locking, either a lock exists or it does not. So in this
37701** variation of CheckReservedLock(), *pResOut is set to true if any lock
37702** is held on the file and false if the file is unlocked.
37703*/
37704static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
37705 int rc = SQLITE_OK0;
37706 int reserved = 0;
37707 unixFile *pFile = (unixFile*)id;
37708
37709 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
37710
37711 assert( pFile )((void) (0));
37712 reserved = osAccess((int(*)(const char*,int))aSyscall[2].pCurrent)((const char*)pFile->lockingContext, 0)==0;
37713 OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
37714 *pResOut = reserved;
37715 return rc;
37716}
37717
37718/*
37719** Lock the file with the lock specified by parameter eFileLock - one
37720** of the following:
37721**
37722** (1) SHARED_LOCK
37723** (2) RESERVED_LOCK
37724** (3) PENDING_LOCK
37725** (4) EXCLUSIVE_LOCK
37726**
37727** Sometimes when requesting one lock state, additional lock states
37728** are inserted in between. The locking might fail on one of the later
37729** transitions leaving the lock state different from what it started but
37730** still short of its goal. The following chart shows the allowed
37731** transitions and the inserted intermediate states:
37732**
37733** UNLOCKED -> SHARED
37734** SHARED -> RESERVED
37735** SHARED -> (PENDING) -> EXCLUSIVE
37736** RESERVED -> (PENDING) -> EXCLUSIVE
37737** PENDING -> EXCLUSIVE
37738**
37739** This routine will only increase a lock. Use the sqlite3OsUnlock()
37740** routine to lower a locking level.
37741**
37742** With dotfile locking, we really only support state (4): EXCLUSIVE.
37743** But we track the other locking levels internally.
37744*/
37745static int dotlockLock(sqlite3_file *id, int eFileLock) {
37746 unixFile *pFile = (unixFile*)id;
37747 char *zLockFile = (char *)pFile->lockingContext;
37748 int rc = SQLITE_OK0;
37749
37750
37751 /* If we have any lock, then the lock file already exists. All we have
37752 ** to do is adjust our internal record of the lock level.
37753 */
37754 if( pFile->eFileLock > NO_LOCK0 ){
37755 pFile->eFileLock = eFileLock;
37756 /* Always update the timestamp on the old file */
37757#ifdef HAVE_UTIME
37758 utime(zLockFile, NULL((void*)0));
37759#else
37760 utimes(zLockFile, NULL((void*)0));
37761#endif
37762 return SQLITE_OK0;
37763 }
37764
37765 /* grab an exclusive lock */
37766 rc = osMkdir((int(*)(const char*,mode_t))aSyscall[18].pCurrent)(zLockFile, 0777);
37767 if( rc<0 ){
37768 /* failed to open/create the lock directory */
37769 int tErrno = errno(*__errno_location ());
37770 if( EEXIST17 == tErrno ){
37771 rc = SQLITE_BUSY5;
37772 } else {
37773 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK(10 | (15<<8)));
37774 if( rc!=SQLITE_BUSY5 ){
37775 storeLastErrno(pFile, tErrno);
37776 }
37777 }
37778 return rc;
37779 }
37780
37781 /* got it, set the type and return ok */
37782 pFile->eFileLock = eFileLock;
37783 return rc;
37784}
37785
37786/*
37787** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
37788** must be either NO_LOCK or SHARED_LOCK.
37789**
37790** If the locking level of the file descriptor is already at or below
37791** the requested locking level, this routine is a no-op.
37792**
37793** When the locking level reaches NO_LOCK, delete the lock file.
37794*/
37795static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
37796 unixFile *pFile = (unixFile*)id;
37797 char *zLockFile = (char *)pFile->lockingContext;
37798 int rc;
37799
37800 assert( pFile )((void) (0));
37801 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
37802 pFile->eFileLock, osGetpid(0)));
37803 assert( eFileLock<=SHARED_LOCK )((void) (0));
37804
37805 /* no-op if possible */
37806 if( pFile->eFileLock==eFileLock ){
37807 return SQLITE_OK0;
37808 }
37809
37810 /* To downgrade to shared, simply update our internal notion of the
37811 ** lock state. No need to mess with the file on disk.
37812 */
37813 if( eFileLock==SHARED_LOCK1 ){
37814 pFile->eFileLock = SHARED_LOCK1;
37815 return SQLITE_OK0;
37816 }
37817
37818 /* To fully unlock the database, delete the lock file */
37819 assert( eFileLock==NO_LOCK )((void) (0));
37820 rc = osRmdir((int(*)(const char*))aSyscall[19].pCurrent)(zLockFile);
37821 if( rc<0 ){
37822 int tErrno = errno(*__errno_location ());
37823 if( tErrno==ENOENT2 ){
37824 rc = SQLITE_OK0;
37825 }else{
37826 rc = SQLITE_IOERR_UNLOCK(10 | (8<<8));
37827 storeLastErrno(pFile, tErrno);
37828 }
37829 return rc;
37830 }
37831 pFile->eFileLock = NO_LOCK0;
37832 return SQLITE_OK0;
37833}
37834
37835/*
37836** Close a file. Make sure the lock has been released before closing.
37837*/
37838static int dotlockClose(sqlite3_file *id) {
37839 unixFile *pFile = (unixFile*)id;
37840 assert( id!=0 )((void) (0));
37841 dotlockUnlock(id, NO_LOCK0);
37842 sqlite3_free(pFile->lockingContext);
37843 return closeUnixFile(id);
37844}
37845/****************** End of the dot-file lock implementation *******************
37846******************************************************************************/
37847
37848/******************************************************************************
37849************************** Begin flock Locking ********************************
37850**
37851** Use the flock() system call to do file locking.
37852**
37853** flock() locking is like dot-file locking in that the various
37854** fine-grain locking levels supported by SQLite are collapsed into
37855** a single exclusive lock. In other words, SHARED, RESERVED, and
37856** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite
37857** still works when you do this, but concurrency is reduced since
37858** only a single process can be reading the database at a time.
37859**
37860** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off
37861*/
37862#if SQLITE_ENABLE_LOCKING_STYLE0
37863
37864/*
37865** Retry flock() calls that fail with EINTR
37866*/
37867#ifdef EINTR4
37868static int robust_flock(int fd, int op){
37869 int rc;
37870 do{ rc = flock(fd,op); }while( rc<0 && errno(*__errno_location ())==EINTR4 );
37871 return rc;
37872}
37873#else
37874# define robust_flock(a,b) flock(a,b)
37875#endif
37876
37877
37878/*
37879** This routine checks if there is a RESERVED lock held on the specified
37880** file by this or any other process. If such a lock is held, set *pResOut
37881** to a non-zero value otherwise *pResOut is set to zero. The return value
37882** is set to SQLITE_OK unless an I/O error occurs during lock checking.
37883*/
37884static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
37885 int rc = SQLITE_OK0;
37886 int reserved = 0;
37887 unixFile *pFile = (unixFile*)id;
37888
37889 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
37890
37891 assert( pFile )((void) (0));
37892
37893 /* Check if a thread in this process holds such a lock */
37894 if( pFile->eFileLock>SHARED_LOCK1 ){
37895 reserved = 1;
37896 }
37897
37898 /* Otherwise see if some other process holds it. */
37899 if( !reserved ){
37900 /* attempt to get the lock */
37901 int lrc = robust_flock(pFile->h, LOCK_EX2 | LOCK_NB4);
37902 if( !lrc ){
37903 /* got the lock, unlock it */
37904 lrc = robust_flock(pFile->h, LOCK_UN8);
37905 if ( lrc ) {
37906 int tErrno = errno(*__errno_location ());
37907 /* unlock failed with an error */
37908 lrc = SQLITE_IOERR_UNLOCK(10 | (8<<8));
37909 storeLastErrno(pFile, tErrno);
37910 rc = lrc;
37911 }
37912 } else {
37913 int tErrno = errno(*__errno_location ());
37914 reserved = 1;
37915 /* someone else might have it reserved */
37916 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK(10 | (15<<8)));
37917 if( IS_LOCK_ERROR(lrc)((lrc != 0) && (lrc != 5)) ){
37918 storeLastErrno(pFile, tErrno);
37919 rc = lrc;
37920 }
37921 }
37922 }
37923 OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
37924
37925#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
37926 if( (rc & 0xff) == SQLITE_IOERR10 ){
37927 rc = SQLITE_OK0;
37928 reserved=1;
37929 }
37930#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
37931 *pResOut = reserved;
37932 return rc;
37933}
37934
37935/*
37936** Lock the file with the lock specified by parameter eFileLock - one
37937** of the following:
37938**
37939** (1) SHARED_LOCK
37940** (2) RESERVED_LOCK
37941** (3) PENDING_LOCK
37942** (4) EXCLUSIVE_LOCK
37943**
37944** Sometimes when requesting one lock state, additional lock states
37945** are inserted in between. The locking might fail on one of the later
37946** transitions leaving the lock state different from what it started but
37947** still short of its goal. The following chart shows the allowed
37948** transitions and the inserted intermediate states:
37949**
37950** UNLOCKED -> SHARED
37951** SHARED -> RESERVED
37952** SHARED -> (PENDING) -> EXCLUSIVE
37953** RESERVED -> (PENDING) -> EXCLUSIVE
37954** PENDING -> EXCLUSIVE
37955**
37956** flock() only really support EXCLUSIVE locks. We track intermediate
37957** lock states in the sqlite3_file structure, but all locks SHARED or
37958** above are really EXCLUSIVE locks and exclude all other processes from
37959** access the file.
37960**
37961** This routine will only increase a lock. Use the sqlite3OsUnlock()
37962** routine to lower a locking level.
37963*/
37964static int flockLock(sqlite3_file *id, int eFileLock) {
37965 int rc = SQLITE_OK0;
37966 unixFile *pFile = (unixFile*)id;
37967
37968 assert( pFile )((void) (0));
37969
37970 /* if we already have a lock, it is exclusive.
37971 ** Just adjust level and punt on outta here. */
37972 if (pFile->eFileLock > NO_LOCK0) {
37973 pFile->eFileLock = eFileLock;
37974 return SQLITE_OK0;
37975 }
37976
37977 /* grab an exclusive lock */
37978
37979 if (robust_flock(pFile->h, LOCK_EX2 | LOCK_NB4)) {
37980 int tErrno = errno(*__errno_location ());
37981 /* didn't get, must be busy */
37982 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK(10 | (15<<8)));
37983 if( IS_LOCK_ERROR(rc)((rc != 0) && (rc != 5)) ){
37984 storeLastErrno(pFile, tErrno);
37985 }
37986 } else {
37987 /* got it, set the type and return ok */
37988 pFile->eFileLock = eFileLock;
37989 }
37990 OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
37991 rc==SQLITE_OK ? "ok" : "failed"));
37992#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
37993 if( (rc & 0xff) == SQLITE_IOERR10 ){
37994 rc = SQLITE_BUSY5;
37995 }
37996#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
37997 return rc;
37998}
37999
38000
38001/*
38002** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
38003** must be either NO_LOCK or SHARED_LOCK.
38004**
38005** If the locking level of the file descriptor is already at or below
38006** the requested locking level, this routine is a no-op.
38007*/
38008static int flockUnlock(sqlite3_file *id, int eFileLock) {
38009 unixFile *pFile = (unixFile*)id;
38010
38011 assert( pFile )((void) (0));
38012 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
38013 pFile->eFileLock, osGetpid(0)));
38014 assert( eFileLock<=SHARED_LOCK )((void) (0));
38015
38016 /* no-op if possible */
38017 if( pFile->eFileLock==eFileLock ){
38018 return SQLITE_OK0;
38019 }
38020
38021 /* shared can just be set because we always have an exclusive */
38022 if (eFileLock==SHARED_LOCK1) {
38023 pFile->eFileLock = eFileLock;
38024 return SQLITE_OK0;
38025 }
38026
38027 /* no, really, unlock. */
38028 if( robust_flock(pFile->h, LOCK_UN8) ){
38029#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
38030 return SQLITE_OK0;
38031#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
38032 return SQLITE_IOERR_UNLOCK(10 | (8<<8));
38033 }else{
38034 pFile->eFileLock = NO_LOCK0;
38035 return SQLITE_OK0;
38036 }
38037}
38038
38039/*
38040** Close a file.
38041*/
38042static int flockClose(sqlite3_file *id) {
38043 assert( id!=0 )((void) (0));
38044 flockUnlock(id, NO_LOCK0);
38045 return closeUnixFile(id);
38046}
38047
38048#endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
38049
38050/******************* End of the flock lock implementation *********************
38051******************************************************************************/
38052
38053/******************************************************************************
38054************************ Begin Named Semaphore Locking ************************
38055**
38056** Named semaphore locking is only supported on VxWorks.
38057**
38058** Semaphore locking is like dot-lock and flock in that it really only
38059** supports EXCLUSIVE locking. Only a single process can read or write
38060** the database file at a time. This reduces potential concurrency, but
38061** makes the lock implementation much easier.
38062*/
38063#if OS_VXWORKS0
38064
38065/*
38066** This routine checks if there is a RESERVED lock held on the specified
38067** file by this or any other process. If such a lock is held, set *pResOut
38068** to a non-zero value otherwise *pResOut is set to zero. The return value
38069** is set to SQLITE_OK unless an I/O error occurs during lock checking.
38070*/
38071static int semXCheckReservedLock(sqlite3_file *id, int *pResOut) {
38072 int rc = SQLITE_OK0;
38073 int reserved = 0;
38074 unixFile *pFile = (unixFile*)id;
38075
38076 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
38077
38078 assert( pFile )((void) (0));
38079
38080 /* Check if a thread in this process holds such a lock */
38081 if( pFile->eFileLock>SHARED_LOCK1 ){
38082 reserved = 1;
38083 }
38084
38085 /* Otherwise see if some other process holds it. */
38086 if( !reserved ){
38087 sem_t *pSem = pFile->pInode->pSem;
38088
38089 if( sem_trywait(pSem)==-1 ){
38090 int tErrno = errno(*__errno_location ());
38091 if( EAGAIN11 != tErrno ){
38092 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK(10 | (14<<8)));
38093 storeLastErrno(pFile, tErrno);
38094 } else {
38095 /* someone else has the lock when we are in NO_LOCK */
38096 reserved = (pFile->eFileLock < SHARED_LOCK1);
38097 }
38098 }else{
38099 /* we could have it if we want it */
38100 sem_post(pSem);
38101 }
38102 }
38103 OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
38104
38105 *pResOut = reserved;
38106 return rc;
38107}
38108
38109/*
38110** Lock the file with the lock specified by parameter eFileLock - one
38111** of the following:
38112**
38113** (1) SHARED_LOCK
38114** (2) RESERVED_LOCK
38115** (3) PENDING_LOCK
38116** (4) EXCLUSIVE_LOCK
38117**
38118** Sometimes when requesting one lock state, additional lock states
38119** are inserted in between. The locking might fail on one of the later
38120** transitions leaving the lock state different from what it started but
38121** still short of its goal. The following chart shows the allowed
38122** transitions and the inserted intermediate states:
38123**
38124** UNLOCKED -> SHARED
38125** SHARED -> RESERVED
38126** SHARED -> (PENDING) -> EXCLUSIVE
38127** RESERVED -> (PENDING) -> EXCLUSIVE
38128** PENDING -> EXCLUSIVE
38129**
38130** Semaphore locks only really support EXCLUSIVE locks. We track intermediate
38131** lock states in the sqlite3_file structure, but all locks SHARED or
38132** above are really EXCLUSIVE locks and exclude all other processes from
38133** access the file.
38134**
38135** This routine will only increase a lock. Use the sqlite3OsUnlock()
38136** routine to lower a locking level.
38137*/
38138static int semXLock(sqlite3_file *id, int eFileLock) {
38139 unixFile *pFile = (unixFile*)id;
38140 sem_t *pSem = pFile->pInode->pSem;
38141 int rc = SQLITE_OK0;
38142
38143 /* if we already have a lock, it is exclusive.
38144 ** Just adjust level and punt on outta here. */
38145 if (pFile->eFileLock > NO_LOCK0) {
38146 pFile->eFileLock = eFileLock;
38147 rc = SQLITE_OK0;
38148 goto sem_end_lock;
38149 }
38150
38151 /* lock semaphore now but bail out when already locked. */
38152 if( sem_trywait(pSem)==-1 ){
38153 rc = SQLITE_BUSY5;
38154 goto sem_end_lock;
38155 }
38156
38157 /* got it, set the type and return ok */
38158 pFile->eFileLock = eFileLock;
38159
38160 sem_end_lock:
38161 return rc;
38162}
38163
38164/*
38165** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
38166** must be either NO_LOCK or SHARED_LOCK.
38167**
38168** If the locking level of the file descriptor is already at or below
38169** the requested locking level, this routine is a no-op.
38170*/
38171static int semXUnlock(sqlite3_file *id, int eFileLock) {
38172 unixFile *pFile = (unixFile*)id;
38173 sem_t *pSem = pFile->pInode->pSem;
38174
38175 assert( pFile )((void) (0));
38176 assert( pSem )((void) (0));
38177 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
38178 pFile->eFileLock, osGetpid(0)));
38179 assert( eFileLock<=SHARED_LOCK )((void) (0));
38180
38181 /* no-op if possible */
38182 if( pFile->eFileLock==eFileLock ){
38183 return SQLITE_OK0;
38184 }
38185
38186 /* shared can just be set because we always have an exclusive */
38187 if (eFileLock==SHARED_LOCK1) {
38188 pFile->eFileLock = eFileLock;
38189 return SQLITE_OK0;
38190 }
38191
38192 /* no, really unlock. */
38193 if ( sem_post(pSem)==-1 ) {
38194 int rc, tErrno = errno(*__errno_location ());
38195 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK(10 | (8<<8)));
38196 if( IS_LOCK_ERROR(rc)((rc != 0) && (rc != 5)) ){
38197 storeLastErrno(pFile, tErrno);
38198 }
38199 return rc;
38200 }
38201 pFile->eFileLock = NO_LOCK0;
38202 return SQLITE_OK0;
38203}
38204
38205/*
38206 ** Close a file.
38207 */
38208static int semXClose(sqlite3_file *id) {
38209 if( id ){
38210 unixFile *pFile = (unixFile*)id;
38211 semXUnlock(id, NO_LOCK0);
38212 assert( pFile )((void) (0));
38213 assert( unixFileMutexNotheld(pFile) )((void) (0));
38214 unixEnterMutex();
38215 releaseInodeInfo(pFile);
38216 unixLeaveMutex();
38217 closeUnixFile(id);
38218 }
38219 return SQLITE_OK0;
38220}
38221
38222#endif /* OS_VXWORKS */
38223/*
38224** Named semaphore locking is only available on VxWorks.
38225**
38226*************** End of the named semaphore lock implementation ****************
38227******************************************************************************/
38228
38229
38230/******************************************************************************
38231*************************** Begin AFP Locking *********************************
38232**
38233** AFP is the Apple Filing Protocol. AFP is a network filesystem found
38234** on Apple Macintosh computers - both OS9 and OSX.
38235**
38236** Third-party implementations of AFP are available. But this code here
38237** only works on OSX.
38238*/
38239
38240#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
38241/*
38242** The afpLockingContext structure contains all afp lock specific state
38243*/
38244typedef struct afpLockingContext afpLockingContext;
38245struct afpLockingContext {
38246 int reserved;
38247 const char *dbPath; /* Name of the open file */
38248};
38249
38250struct ByteRangeLockPB2
38251{
38252 unsigned long long offset; /* offset to first byte to lock */
38253 unsigned long long length; /* nbr of bytes to lock */
38254 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
38255 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
38256 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
38257 int fd; /* file desc to assoc this lock with */
38258};
38259
38260#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)(((2U|1U) << (((0 +8)+8)+14)) | ((('z')) << (0 +8
)) | (((23)) << 0) | ((((sizeof(struct ByteRangeLockPB2
)))) << ((0 +8)+8)))
38261
38262/*
38263** This is a utility for setting or clearing a bit-range lock on an
38264** AFP filesystem.
38265**
38266** Return SQLITE_OK on success, SQLITE_BUSY on failure.
38267*/
38268static int afpSetLock(
38269 const char *path, /* Name of the file to be locked or unlocked */
38270 unixFile *pFile, /* Open file descriptor on path */
38271 unsigned long long offset, /* First byte to be locked */
38272 unsigned long long length, /* Number of bytes to lock */
38273 int setLockFlag /* True to set lock. False to clear lock */
38274){
38275 struct ByteRangeLockPB2 pb;
38276 int err;
38277
38278 pb.unLockFlag = setLockFlag ? 0 : 1;
38279 pb.startEndFlag = 0;
38280 pb.offset = offset;
38281 pb.length = length;
38282 pb.fd = pFile->h;
38283
38284 OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
38285 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
38286 offset, length));
38287 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
38288 if ( err==-1 ) {
38289 int rc;
38290 int tErrno = errno(*__errno_location ());
38291 OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
38292 path, tErrno, strerror(tErrno)));
38293#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
38294 rc = SQLITE_BUSY5;
38295#else
38296 rc = sqliteErrorFromPosixError(tErrno,
38297 setLockFlag ? SQLITE_IOERR_LOCK(10 | (15<<8)) : SQLITE_IOERR_UNLOCK(10 | (8<<8)));
38298#endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
38299 if( IS_LOCK_ERROR(rc)((rc != 0) && (rc != 5)) ){
38300 storeLastErrno(pFile, tErrno);
38301 }
38302 return rc;
38303 } else {
38304 return SQLITE_OK0;
38305 }
38306}
38307
38308/*
38309** This routine checks if there is a RESERVED lock held on the specified
38310** file by this or any other process. If such a lock is held, set *pResOut
38311** to a non-zero value otherwise *pResOut is set to zero. The return value
38312** is set to SQLITE_OK unless an I/O error occurs during lock checking.
38313*/
38314static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
38315 int rc = SQLITE_OK0;
38316 int reserved = 0;
38317 unixFile *pFile = (unixFile*)id;
38318 afpLockingContext *context;
38319
38320 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
38321
38322 assert( pFile )((void) (0));
38323 context = (afpLockingContext *) pFile->lockingContext;
38324 if( context->reserved ){
38325 *pResOut = 1;
38326 return SQLITE_OK0;
38327 }
38328 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
38329 /* Check if a thread in this process holds such a lock */
38330 if( pFile->pInode->eFileLock>SHARED_LOCK1 ){
38331 reserved = 1;
38332 }
38333
38334 /* Otherwise see if some other process holds it.
38335 */
38336 if( !reserved ){
38337 /* lock the RESERVED byte */
38338 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE(sqlite3PendingByte+1), 1,1);
38339 if( SQLITE_OK0==lrc ){
38340 /* if we succeeded in taking the reserved lock, unlock it to restore
38341 ** the original state */
38342 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE(sqlite3PendingByte+1), 1, 0);
38343 } else {
38344 /* if we failed to get the lock then someone else must have it */
38345 reserved = 1;
38346 }
38347 if( IS_LOCK_ERROR(lrc)((lrc != 0) && (lrc != 5)) ){
38348 rc=lrc;
38349 }
38350 }
38351
38352 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
38353 OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
38354
38355 *pResOut = reserved;
38356 return rc;
38357}
38358
38359/*
38360** Lock the file with the lock specified by parameter eFileLock - one
38361** of the following:
38362**
38363** (1) SHARED_LOCK
38364** (2) RESERVED_LOCK
38365** (3) PENDING_LOCK
38366** (4) EXCLUSIVE_LOCK
38367**
38368** Sometimes when requesting one lock state, additional lock states
38369** are inserted in between. The locking might fail on one of the later
38370** transitions leaving the lock state different from what it started but
38371** still short of its goal. The following chart shows the allowed
38372** transitions and the inserted intermediate states:
38373**
38374** UNLOCKED -> SHARED
38375** SHARED -> RESERVED
38376** SHARED -> (PENDING) -> EXCLUSIVE
38377** RESERVED -> (PENDING) -> EXCLUSIVE
38378** PENDING -> EXCLUSIVE
38379**
38380** This routine will only increase a lock. Use the sqlite3OsUnlock()
38381** routine to lower a locking level.
38382*/
38383static int afpLock(sqlite3_file *id, int eFileLock){
38384 int rc = SQLITE_OK0;
38385 unixFile *pFile = (unixFile*)id;
38386 unixInodeInfo *pInode = pFile->pInode;
38387 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
38388
38389 assert( pFile )((void) (0));
38390 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
38391 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
38392 azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
38393
38394 /* If there is already a lock of this type or more restrictive on the
38395 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
38396 ** unixEnterMutex() hasn't been called yet.
38397 */
38398 if( pFile->eFileLock>=eFileLock ){
38399 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h,
38400 azFileLock(eFileLock)));
38401 return SQLITE_OK0;
38402 }
38403
38404 /* Make sure the locking sequence is correct
38405 ** (1) We never move from unlocked to anything higher than shared lock.
38406 ** (2) SQLite never explicitly requests a pendig lock.
38407 ** (3) A shared lock is always held when a reserve lock is requested.
38408 */
38409 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK )((void) (0));
38410 assert( eFileLock!=PENDING_LOCK )((void) (0));
38411 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK )((void) (0));
38412
38413 /* This mutex is needed because pFile->pInode is shared across threads
38414 */
38415 pInode = pFile->pInode;
38416 sqlite3_mutex_enter(pInode->pLockMutex);
38417
38418 /* If some thread using this PID has a lock via a different unixFile*
38419 ** handle that precludes the requested lock, return BUSY.
38420 */
38421 if( (pFile->eFileLock!=pInode->eFileLock &&
38422 (pInode->eFileLock>=PENDING_LOCK3 || eFileLock>SHARED_LOCK1))
38423 ){
38424 rc = SQLITE_BUSY5;
38425 goto afp_end_lock;
38426 }
38427
38428 /* If a SHARED lock is requested, and some thread using this PID already
38429 ** has a SHARED or RESERVED lock, then increment reference counts and
38430 ** return SQLITE_OK.
38431 */
38432 if( eFileLock==SHARED_LOCK1 &&
38433 (pInode->eFileLock==SHARED_LOCK1 || pInode->eFileLock==RESERVED_LOCK2) ){
38434 assert( eFileLock==SHARED_LOCK )((void) (0));
38435 assert( pFile->eFileLock==0 )((void) (0));
38436 assert( pInode->nShared>0 )((void) (0));
38437 pFile->eFileLock = SHARED_LOCK1;
38438 pInode->nShared++;
38439 pInode->nLock++;
38440 goto afp_end_lock;
38441 }
38442
38443 /* A PENDING lock is needed before acquiring a SHARED lock and before
38444 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
38445 ** be released.
38446 */
38447 if( eFileLock==SHARED_LOCK1
38448 || (eFileLock==EXCLUSIVE_LOCK4 && pFile->eFileLock<PENDING_LOCK3)
38449 ){
38450 int failed;
38451 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTEsqlite3PendingByte, 1, 1);
38452 if (failed) {
38453 rc = failed;
38454 goto afp_end_lock;
38455 }
38456 }
38457
38458 /* If control gets to this point, then actually go ahead and make
38459 ** operating system calls for the specified lock.
38460 */
38461 if( eFileLock==SHARED_LOCK1 ){
38462 int lrc1, lrc2, lrc1Errno = 0;
38463 long lk, mask;
38464
38465 assert( pInode->nShared==0 )((void) (0));
38466 assert( pInode->eFileLock==0 )((void) (0));
38467
38468 mask = (sizeof(long)==8) ? LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) : 0x7fffffff;
38469 /* Now get the read-lock SHARED_LOCK */
38470 /* note that the quality of the randomness doesn't matter that much */
38471 lk = random();
38472 pInode->sharedByte = (lk & mask)%(SHARED_SIZE510 - 1);
38473 lrc1 = afpSetLock(context->dbPath, pFile,
38474 SHARED_FIRST(sqlite3PendingByte+2)+pInode->sharedByte, 1, 1);
38475 if( IS_LOCK_ERROR(lrc1)((lrc1 != 0) && (lrc1 != 5)) ){
38476 lrc1Errno = pFile->lastErrno;
38477 }
38478 /* Drop the temporary PENDING lock */
38479 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTEsqlite3PendingByte, 1, 0);
38480
38481 if( IS_LOCK_ERROR(lrc1)((lrc1 != 0) && (lrc1 != 5)) ) {
38482 storeLastErrno(pFile, lrc1Errno);
38483 rc = lrc1;
38484 goto afp_end_lock;
38485 } else if( IS_LOCK_ERROR(lrc2)((lrc2 != 0) && (lrc2 != 5)) ){
38486 rc = lrc2;
38487 goto afp_end_lock;
38488 } else if( lrc1 != SQLITE_OK0 ) {
38489 rc = lrc1;
38490 } else {
38491 pFile->eFileLock = SHARED_LOCK1;
38492 pInode->nLock++;
38493 pInode->nShared = 1;
38494 }
38495 }else if( eFileLock==EXCLUSIVE_LOCK4 && pInode->nShared>1 ){
38496 /* We are trying for an exclusive lock but another thread in this
38497 ** same process is still holding a shared lock. */
38498 rc = SQLITE_BUSY5;
38499 }else{
38500 /* The request was for a RESERVED or EXCLUSIVE lock. It is
38501 ** assumed that there is a SHARED or greater lock on the file
38502 ** already.
38503 */
38504 int failed = 0;
38505 assert( 0!=pFile->eFileLock )((void) (0));
38506 if (eFileLock >= RESERVED_LOCK2 && pFile->eFileLock < RESERVED_LOCK2) {
38507 /* Acquire a RESERVED lock */
38508 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE(sqlite3PendingByte+1), 1,1);
38509 if( !failed ){
38510 context->reserved = 1;
38511 }
38512 }
38513 if (!failed && eFileLock == EXCLUSIVE_LOCK4) {
38514 /* Acquire an EXCLUSIVE lock */
38515
38516 /* Remove the shared lock before trying the range. we'll need to
38517 ** reestablish the shared lock if we can't get the afpUnlock
38518 */
38519 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST(sqlite3PendingByte+2) +
38520 pInode->sharedByte, 1, 0)) ){
38521 int failed2 = SQLITE_OK0;
38522 /* now attemmpt to get the exclusive lock range */
38523 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST(sqlite3PendingByte+2),
38524 SHARED_SIZE510, 1);
38525 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
38526 SHARED_FIRST(sqlite3PendingByte+2) + pInode->sharedByte, 1, 1)) ){
38527 /* Can't reestablish the shared lock. Sqlite can't deal, this is
38528 ** a critical I/O error
38529 */
38530 rc = ((failed & 0xff) == SQLITE_IOERR10) ? failed2 :
38531 SQLITE_IOERR_LOCK(10 | (15<<8));
38532 goto afp_end_lock;
38533 }
38534 }else{
38535 rc = failed;
38536 }
38537 }
38538 if( failed ){
38539 rc = failed;
38540 }
38541 }
38542
38543 if( rc==SQLITE_OK0 ){
38544 pFile->eFileLock = eFileLock;
38545 pInode->eFileLock = eFileLock;
38546 }else if( eFileLock==EXCLUSIVE_LOCK4 ){
38547 pFile->eFileLock = PENDING_LOCK3;
38548 pInode->eFileLock = PENDING_LOCK3;
38549 }
38550
38551afp_end_lock:
38552 sqlite3_mutex_leave(pInode->pLockMutex);
38553 OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
38554 rc==SQLITE_OK ? "ok" : "failed"));
38555 return rc;
38556}
38557
38558/*
38559** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
38560** must be either NO_LOCK or SHARED_LOCK.
38561**
38562** If the locking level of the file descriptor is already at or below
38563** the requested locking level, this routine is a no-op.
38564*/
38565static int afpUnlock(sqlite3_file *id, int eFileLock) {
38566 int rc = SQLITE_OK0;
38567 unixFile *pFile = (unixFile*)id;
38568 unixInodeInfo *pInode;
38569 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
38570 int skipShared = 0;
38571#ifdef SQLITE_TEST
38572 int h = pFile->h;
38573#endif
38574
38575 assert( pFile )((void) (0));
38576 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
38577 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
38578 osGetpid(0)));
38579
38580 assert( eFileLock<=SHARED_LOCK )((void) (0));
38581 if( pFile->eFileLock<=eFileLock ){
38582 return SQLITE_OK0;
38583 }
38584 pInode = pFile->pInode;
38585 sqlite3_mutex_enter(pInode->pLockMutex);
38586 assert( pInode->nShared!=0 )((void) (0));
38587 if( pFile->eFileLock>SHARED_LOCK1 ){
38588 assert( pInode->eFileLock==pFile->eFileLock )((void) (0));
38589 SimulateIOErrorBenign(1);
38590 SimulateIOError( h=(-1) )
38591 SimulateIOErrorBenign(0);
38592
38593#ifdef SQLITE_DEBUG
38594 /* When reducing a lock such that other processes can start
38595 ** reading the database file again, make sure that the
38596 ** transaction counter was updated if any part of the database
38597 ** file changed. If the transaction counter is not updated,
38598 ** other connections to the same file might not realize that
38599 ** the file has changed and hence might not know to flush their
38600 ** cache. The use of a stale cache can lead to database corruption.
38601 */
38602 assert( pFile->inNormalWrite==0((void) (0))
38603 || pFile->dbUpdate==0((void) (0))
38604 || pFile->transCntrChng==1 )((void) (0));
38605 pFile->inNormalWrite = 0;
38606#endif
38607
38608 if( pFile->eFileLock==EXCLUSIVE_LOCK4 ){
38609 rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST(sqlite3PendingByte+2), SHARED_SIZE510, 0);
38610 if( rc==SQLITE_OK0 && (eFileLock==SHARED_LOCK1 || pInode->nShared>1) ){
38611 /* only re-establish the shared lock if necessary */
38612 int sharedLockByte = SHARED_FIRST(sqlite3PendingByte+2)+pInode->sharedByte;
38613 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
38614 } else {
38615 skipShared = 1;
38616 }
38617 }
38618 if( rc==SQLITE_OK0 && pFile->eFileLock>=PENDING_LOCK3 ){
38619 rc = afpSetLock(context->dbPath, pFile, PENDING_BYTEsqlite3PendingByte, 1, 0);
38620 }
38621 if( rc==SQLITE_OK0 && pFile->eFileLock>=RESERVED_LOCK2 && context->reserved ){
38622 rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE(sqlite3PendingByte+1), 1, 0);
38623 if( !rc ){
38624 context->reserved = 0;
38625 }
38626 }
38627 if( rc==SQLITE_OK0 && (eFileLock==SHARED_LOCK1 || pInode->nShared>1)){
38628 pInode->eFileLock = SHARED_LOCK1;
38629 }
38630 }
38631 if( rc==SQLITE_OK0 && eFileLock==NO_LOCK0 ){
38632
38633 /* Decrement the shared lock counter. Release the lock using an
38634 ** OS call only when all threads in this same process have released
38635 ** the lock.
38636 */
38637 unsigned long long sharedLockByte = SHARED_FIRST(sqlite3PendingByte+2)+pInode->sharedByte;
38638 pInode->nShared--;
38639 if( pInode->nShared==0 ){
38640 SimulateIOErrorBenign(1);
38641 SimulateIOError( h=(-1) )
38642 SimulateIOErrorBenign(0);
38643 if( !skipShared ){
38644 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
38645 }
38646 if( !rc ){
38647 pInode->eFileLock = NO_LOCK0;
38648 pFile->eFileLock = NO_LOCK0;
38649 }
38650 }
38651 if( rc==SQLITE_OK0 ){
38652 pInode->nLock--;
38653 assert( pInode->nLock>=0 )((void) (0));
38654 if( pInode->nLock==0 ) closePendingFds(pFile);
38655 }
38656 }
38657
38658 sqlite3_mutex_leave(pInode->pLockMutex);
38659 if( rc==SQLITE_OK0 ){
38660 pFile->eFileLock = eFileLock;
38661 }
38662 return rc;
38663}
38664
38665/*
38666** Close a file & cleanup AFP specific locking context
38667*/
38668static int afpClose(sqlite3_file *id) {
38669 int rc = SQLITE_OK0;
38670 unixFile *pFile = (unixFile*)id;
38671 assert( id!=0 )((void) (0));
38672 afpUnlock(id, NO_LOCK0);
38673 assert( unixFileMutexNotheld(pFile) )((void) (0));
38674 unixEnterMutex();
38675 if( pFile->pInode ){
38676 unixInodeInfo *pInode = pFile->pInode;
38677 sqlite3_mutex_enter(pInode->pLockMutex);
38678 if( pInode->nLock ){
38679 /* If there are outstanding locks, do not actually close the file just
38680 ** yet because that would clear those locks. Instead, add the file
38681 ** descriptor to pInode->aPending. It will be automatically closed when
38682 ** the last lock is cleared.
38683 */
38684 setPendingFd(pFile);
38685 }
38686 sqlite3_mutex_leave(pInode->pLockMutex);
38687 }
38688 releaseInodeInfo(pFile);
38689 sqlite3_free(pFile->lockingContext);
38690 rc = closeUnixFile(id);
38691 unixLeaveMutex();
38692 return rc;
38693}
38694
38695#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
38696/*
38697** The code above is the AFP lock implementation. The code is specific
38698** to MacOSX and does not work on other unix platforms. No alternative
38699** is available. If you don't compile for a mac, then the "unix-afp"
38700** VFS is not available.
38701**
38702********************* End of the AFP lock implementation **********************
38703******************************************************************************/
38704
38705/******************************************************************************
38706*************************** Begin NFS Locking ********************************/
38707
38708#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
38709/*
38710 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
38711 ** must be either NO_LOCK or SHARED_LOCK.
38712 **
38713 ** If the locking level of the file descriptor is already at or below
38714 ** the requested locking level, this routine is a no-op.
38715 */
38716static int nfsUnlock(sqlite3_file *id, int eFileLock){
38717 return posixUnlock(id, eFileLock, 1);
38718}
38719
38720#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
38721/*
38722** The code above is the NFS lock implementation. The code is specific
38723** to MacOSX and does not work on other unix platforms. No alternative
38724** is available.
38725**
38726********************* End of the NFS lock implementation **********************
38727******************************************************************************/
38728
38729/******************************************************************************
38730**************** Non-locking sqlite3_file methods *****************************
38731**
38732** The next division contains implementations for all methods of the
38733** sqlite3_file object other than the locking methods. The locking
38734** methods were defined in divisions above (one locking method per
38735** division). Those methods that are common to all locking modes
38736** are gather together into this division.
38737*/
38738
38739/*
38740** Seek to the offset passed as the second argument, then read cnt
38741** bytes into pBuf. Return the number of bytes actually read.
38742**
38743** NB: If you define USE_PREAD or USE_PREAD64, then it might also
38744** be necessary to define _XOPEN_SOURCE to be 500. This varies from
38745** one system to another. Since SQLite does not define USE_PREAD
38746** in any form by default, we will not attempt to define _XOPEN_SOURCE.
38747** See tickets #2741 and #2681.
38748**
38749** To avoid stomping the errno value on a failed read the lastErrno value
38750** is set before returning.
38751*/
38752static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
38753 int got;
38754 int prior = 0;
38755#if (!defined(USE_PREAD) && !defined(USE_PREAD64))
38756 i64 newOffset;
38757#endif
38758 TIMER_START;
38759 assert( cnt==(cnt&0x1ffff) )((void) (0));
38760 assert( id->h>2 )((void) (0));
38761 do{
38762#if defined(USE_PREAD)
38763 got = osPread((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)(id->h, pBuf, cnt, offset);
38764 SimulateIOError( got = -1 );
38765#elif defined(USE_PREAD64)
38766 got = osPread64((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent)(id->h, pBuf, cnt, offset);
38767 SimulateIOError( got = -1 );
38768#else
38769 newOffset = lseek(id->h, offset, SEEK_SET0);
38770 SimulateIOError( newOffset = -1 );
38771 if( newOffset<0 ){
38772 storeLastErrno((unixFile*)id, errno(*__errno_location ()));
38773 return -1;
38774 }
38775 got = osRead((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)(id->h, pBuf, cnt);
38776#endif
38777 if( got==cnt ) break;
38778 if( got<0 ){
38779 if( errno(*__errno_location ())==EINTR4 ){ got = 1; continue; }
38780 prior = 0;
38781 storeLastErrno((unixFile*)id, errno(*__errno_location ()));
38782 break;
38783 }else if( got>0 ){
38784 cnt -= got;
38785 offset += got;
38786 prior += got;
38787 pBuf = (void*)(got + (char*)pBuf);
38788 }
38789 }while( got>0 );
38790 TIMER_END;
38791 OSTRACE(("READ %-3d %5d %7lld %llu\n",
38792 id->h, got+prior, offset-prior, TIMER_ELAPSED));
38793 return got+prior;
38794}
38795
38796/*
38797** Read data from a file into a buffer. Return SQLITE_OK if all
38798** bytes were read successfully and SQLITE_IOERR if anything goes
38799** wrong.
38800*/
38801static int unixRead(
38802 sqlite3_file *id,
38803 void *pBuf,
38804 int amt,
38805 sqlite3_int64 offset
38806){
38807 unixFile *pFile = (unixFile *)id;
38808 int got;
38809 assert( id )((void) (0));
38810 assert( offset>=0 )((void) (0));
38811 assert( amt>0 )((void) (0));
38812
38813 /* If this is a database file (not a journal, super-journal or temp
38814 ** file), the bytes in the locking range should never be read or written. */
38815#if 0
38816 assert( pFile->pPreallocatedUnused==0((void) (0))
38817 || offset>=PENDING_BYTE+512((void) (0))
38818 || offset+amt<=PENDING_BYTE((void) (0))
38819 )((void) (0));
38820#endif
38821
38822#if SQLITE_MAX_MMAP_SIZE20971520>0
38823 /* Deal with as much of this read request as possible by transfering
38824 ** data from the memory mapping using memcpy(). */
38825 if( offset<pFile->mmapSize ){
38826 if( offset+amt <= pFile->mmapSize ){
38827 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
38828 return SQLITE_OK0;
38829 }else{
38830 int nCopy = pFile->mmapSize - offset;
38831 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
38832 pBuf = &((u8 *)pBuf)[nCopy];
38833 amt -= nCopy;
38834 offset += nCopy;
38835 }
38836 }
38837#endif
38838
38839 got = seekAndRead(pFile, offset, pBuf, amt);
38840 if( got==amt ){
38841 return SQLITE_OK0;
38842 }else if( got<0 ){
38843 /* pFile->lastErrno has been set by seekAndRead().
38844 ** Usually we return SQLITE_IOERR_READ here, though for some
38845 ** kinds of errors we return SQLITE_IOERR_CORRUPTFS. The
38846 ** SQLITE_IOERR_CORRUPTFS will be converted into SQLITE_CORRUPT
38847 ** prior to returning to the application by the sqlite3ApiExit()
38848 ** routine.
38849 */
38850 switch( pFile->lastErrno ){
38851 case ERANGE34:
38852 case EIO5:
38853#ifdef ENXIO6
38854 case ENXIO6:
38855#endif
38856#ifdef EDEVERR
38857 case EDEVERR:
38858#endif
38859 return SQLITE_IOERR_CORRUPTFS(10 | (33<<8));
38860 }
38861 return SQLITE_IOERR_READ(10 | (1<<8));
38862 }else{
38863 storeLastErrno(pFile, 0); /* not a system error */
38864 /* Unread parts of the buffer must be zero-filled */
38865 memset(&((char*)pBuf)[got], 0, amt-got);
38866 return SQLITE_IOERR_SHORT_READ(10 | (2<<8));
38867 }
38868}
38869
38870/*
38871** Attempt to seek the file-descriptor passed as the first argument to
38872** absolute offset iOff, then attempt to write nBuf bytes of data from
38873** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
38874** return the actual number of bytes written (which may be less than
38875** nBuf).
38876*/
38877static int seekAndWriteFd(
38878 int fd, /* File descriptor to write to */
38879 i64 iOff, /* File offset to begin writing at */
38880 const void *pBuf, /* Copy data from this buffer to the file */
38881 int nBuf, /* Size of buffer pBuf in bytes */
38882 int *piErrno /* OUT: Error number if error occurs */
38883){
38884 int rc = 0; /* Value returned by system call */
38885
38886 assert( nBuf==(nBuf&0x1ffff) )((void) (0));
38887 assert( fd>2 )((void) (0));
38888 assert( piErrno!=0 )((void) (0));
38889 nBuf &= 0x1ffff;
38890 TIMER_START;
38891
38892#if defined(USE_PREAD)
38893 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 );
38894#elif defined(USE_PREAD64)
38895 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);
38896#else
38897 do{
38898 i64 iSeek = lseek(fd, iOff, SEEK_SET0);
38899 SimulateIOError( iSeek = -1 );
38900 if( iSeek<0 ){
38901 rc = -1;
38902 break;
38903 }
38904 rc = osWrite((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)(fd, pBuf, nBuf);
38905 }while( rc<0 && errno(*__errno_location ())==EINTR4 );
38906#endif
38907
38908 TIMER_END;
38909 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
38910
38911 if( rc<0 ) *piErrno = errno(*__errno_location ());
38912 return rc;
38913}
38914
38915
38916/*
38917** Seek to the offset in id->offset then read cnt bytes into pBuf.
38918** Return the number of bytes actually read. Update the offset.
38919**
38920** To avoid stomping the errno value on a failed write the lastErrno value
38921** is set before returning.
38922*/
38923static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
38924 return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
38925}
38926
38927
38928/*
38929** Write data from a buffer into a file. Return SQLITE_OK on success
38930** or some other error code on failure.
38931*/
38932static int unixWrite(
38933 sqlite3_file *id,
38934 const void *pBuf,
38935 int amt,
38936 sqlite3_int64 offset
38937){
38938 unixFile *pFile = (unixFile*)id;
38939 int wrote = 0;
38940 assert( id )((void) (0));
38941 assert( amt>0 )((void) (0));
38942
38943 /* If this is a database file (not a journal, super-journal or temp
38944 ** file), the bytes in the locking range should never be read or written. */
38945#if 0
38946 assert( pFile->pPreallocatedUnused==0((void) (0))
38947 || offset>=PENDING_BYTE+512((void) (0))
38948 || offset+amt<=PENDING_BYTE((void) (0))
38949 )((void) (0));
38950#endif
38951
38952#ifdef SQLITE_DEBUG
38953 /* If we are doing a normal write to a database file (as opposed to
38954 ** doing a hot-journal rollback or a write to some file other than a
38955 ** normal database file) then record the fact that the database
38956 ** has changed. If the transaction counter is modified, record that
38957 ** fact too.
38958 */
38959 if( pFile->inNormalWrite ){
38960 pFile->dbUpdate = 1; /* The database has been modified */
38961 if( offset<=24 && offset+amt>=27 ){
38962 int rc;
38963 char oldCntr[4];
38964 SimulateIOErrorBenign(1);
38965 rc = seekAndRead(pFile, 24, oldCntr, 4);
38966 SimulateIOErrorBenign(0);
38967 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
38968 pFile->transCntrChng = 1; /* The transaction counter has changed */
38969 }
38970 }
38971 }
38972#endif
38973
38974#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE20971520>0
38975 /* Deal with as much of this write request as possible by transfering
38976 ** data from the memory mapping using memcpy(). */
38977 if( offset<pFile->mmapSize ){
38978 if( offset+amt <= pFile->mmapSize ){
38979 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
38980 return SQLITE_OK0;
38981 }else{
38982 int nCopy = pFile->mmapSize - offset;
38983 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
38984 pBuf = &((u8 *)pBuf)[nCopy];
38985 amt -= nCopy;
38986 offset += nCopy;
38987 }
38988 }
38989#endif
38990
38991 while( (wrote = seekAndWrite(pFile, offset, pBuf, amt))<amt && wrote>0 ){
38992 amt -= wrote;
38993 offset += wrote;
38994 pBuf = &((char*)pBuf)[wrote];
38995 }
38996 SimulateIOError(( wrote=(-1), amt=1 ));
38997 SimulateDiskfullError(( wrote=0, amt=1 ));
38998
38999 if( amt>wrote ){
39000 if( wrote<0 && pFile->lastErrno!=ENOSPC28 ){
39001 /* lastErrno set by seekAndWrite */
39002 return SQLITE_IOERR_WRITE(10 | (3<<8));
39003 }else{
39004 storeLastErrno(pFile, 0); /* not a system error */
39005 return SQLITE_FULL13;
39006 }
39007 }
39008
39009 return SQLITE_OK0;
39010}
39011
39012#ifdef SQLITE_TEST
39013/*
39014** Count the number of fullsyncs and normal syncs. This is used to test
39015** that syncs and fullsyncs are occurring at the right times.
39016*/
39017SQLITE_API int sqlite3_sync_count = 0;
39018SQLITE_API int sqlite3_fullsync_count = 0;
39019#endif
39020
39021/*
39022** We do not trust systems to provide a working fdatasync(). Some do.
39023** Others do no. To be safe, we will stick with the (slightly slower)
39024** fsync(). If you know that your system does support fdatasync() correctly,
39025** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
39026*/
39027#if !defined(fdatasyncfsync) && !HAVE_FDATASYNC
39028# define fdatasyncfsync fsync
39029#endif
39030
39031/*
39032** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
39033** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
39034** only available on Mac OS X. But that could change.
39035*/
39036#ifdef F_FULLFSYNC
39037# define HAVE_FULLFSYNC0 1
39038#else
39039# define HAVE_FULLFSYNC0 0
39040#endif
39041
39042
39043/*
39044** The fsync() system call does not work as advertised on many
39045** unix systems. The following procedure is an attempt to make
39046** it work better.
39047**
39048** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
39049** for testing when we want to run through the test suite quickly.
39050** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
39051** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
39052** or power failure will likely corrupt the database file.
39053**
39054** SQLite sets the dataOnly flag if the size of the file is unchanged.
39055** The idea behind dataOnly is that it should only write the file content
39056** to disk, not the inode. We only set dataOnly if the file size is
39057** unchanged since the file size is part of the inode. However,
39058** Ted Ts'o tells us that fdatasync() will also write the inode if the
39059** file size has changed. The only real difference between fdatasync()
39060** and fsync(), Ted tells us, is that fdatasync() will not flush the
39061** inode if the mtime or owner or other inode attributes have changed.
39062** We only care about the file size, not the other file attributes, so
39063** as far as SQLite is concerned, an fdatasync() is always adequate.
39064** So, we always use fdatasync() if it is available, regardless of
39065** the value of the dataOnly flag.
39066*/
39067static int full_fsync(int fd, int fullSync, int dataOnly){
39068 int rc;
39069
39070 /* The following "ifdef/elif/else/" block has the same structure as
39071 ** the one below. It is replicated here solely to avoid cluttering
39072 ** up the real code with the UNUSED_PARAMETER() macros.
39073 */
39074#ifdef SQLITE_NO_SYNC
39075 UNUSED_PARAMETER(fd)(void)(fd);
39076 UNUSED_PARAMETER(fullSync)(void)(fullSync);
39077 UNUSED_PARAMETER(dataOnly)(void)(dataOnly);
39078#elif HAVE_FULLFSYNC0
39079 UNUSED_PARAMETER(dataOnly)(void)(dataOnly);
39080#else
39081 UNUSED_PARAMETER(fullSync)(void)(fullSync);
39082 UNUSED_PARAMETER(dataOnly)(void)(dataOnly);
39083#endif
39084
39085 /* Record the number of times that we do a normal fsync() and
39086 ** FULLSYNC. This is used during testing to verify that this procedure
39087 ** gets called with the correct arguments.
39088 */
39089#ifdef SQLITE_TEST
39090 if( fullSync ) sqlite3_fullsync_count++;
39091 sqlite3_sync_count++;
39092#endif
39093
39094 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
39095 ** no-op. But go ahead and call fstat() to validate the file
39096 ** descriptor as we need a method to provoke a failure during
39097 ** coverate testing.
39098 */
39099#ifdef SQLITE_NO_SYNC
39100 {
39101 struct stat buf;
39102 rc = osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(fd, &buf);
39103 }
39104#elif HAVE_FULLFSYNC0
39105 if( fullSync ){
39106 rc = osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(fd, F_FULLFSYNC, 0);
39107 }else{
39108 rc = 1;
39109 }
39110 /* If the FULLFSYNC failed, fall back to attempting an fsync().
39111 ** It shouldn't be possible for fullfsync to fail on the local
39112 ** file system (on OSX), so failure indicates that FULLFSYNC
39113 ** isn't supported for this file system. So, attempt an fsync
39114 ** and (for now) ignore the overhead of a superfluous fcntl call.
39115 ** It'd be better to detect fullfsync support once and avoid
39116 ** the fcntl call every time sync is called.
39117 */
39118 if( rc ) rc = fsync(fd);
39119
39120#elif defined(__APPLE__)
39121 /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly
39122 ** so currently we default to the macro that redefines fdatasync to fsync
39123 */
39124 rc = fsync(fd);
39125#else
39126 rc = fdatasyncfsync(fd);
39127#if OS_VXWORKS0
39128 if( rc==-1 && errno(*__errno_location ())==ENOTSUP95 ){
39129 rc = fsync(fd);
39130 }
39131#endif /* OS_VXWORKS */
39132#endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
39133
39134 if( OS_VXWORKS0 && rc!= -1 ){
39135 rc = 0;
39136 }
39137 return rc;
39138}
39139
39140/*
39141** Open a file descriptor to the directory containing file zFilename.
39142** If successful, *pFd is set to the opened file descriptor and
39143** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
39144** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
39145** value.
39146**
39147** The directory file descriptor is used for only one thing - to
39148** fsync() a directory to make sure file creation and deletion events
39149** are flushed to disk. Such fsyncs are not needed on newer
39150** journaling filesystems, but are required on older filesystems.
39151**
39152** This routine can be overridden using the xSetSysCall interface.
39153** The ability to override this routine was added in support of the
39154** chromium sandbox. Opening a directory is a security risk (we are
39155** told) so making it overrideable allows the chromium sandbox to
39156** replace this routine with a harmless no-op. To make this routine
39157** a no-op, replace it with a stub that returns SQLITE_OK but leaves
39158** *pFd set to a negative number.
39159**
39160** If SQLITE_OK is returned, the caller is responsible for closing
39161** the file descriptor *pFd using close().
39162*/
39163static int openDirectory(const char *zFilename, int *pFd){
39164 int ii;
39165 int fd = -1;
39166 char zDirname[MAX_PATHNAME512+1];
39167
39168 sqlite3_snprintf(MAX_PATHNAME512, zDirname, "%s", zFilename);
39169 for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--);
39170 if( ii>0 ){
39171 zDirname[ii] = '\0';
39172 }else{
39173 if( zDirname[0]!='/' ) zDirname[0] = '.';
39174 zDirname[1] = 0;
39175 }
39176 fd = robust_open(zDirname, O_RDONLY00|O_BINARY0, 0);
39177 if( fd>=0 ){
39178 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
39179 }
39180 *pFd = fd;
39181 if( fd>=0 ) return SQLITE_OK0;
39182 return unixLogError(SQLITE_CANTOPEN_BKPT, "openDirectory", zDirname)unixLogErrorAtLine(sqlite3CantopenError(39182),"openDirectory"
,zDirname,39182)
;
39183}
39184
39185/*
39186** Make sure all writes to a particular file are committed to disk.
39187**
39188** If dataOnly==0 then both the file itself and its metadata (file
39189** size, access time, etc) are synced. If dataOnly!=0 then only the
39190** file data is synced.
39191**
39192** Under Unix, also make sure that the directory entry for the file
39193** has been created by fsync-ing the directory that contains the file.
39194** If we do not do this and we encounter a power failure, the directory
39195** entry for the journal might not exist after we reboot. The next
39196** SQLite to access the file will not know that the journal exists (because
39197** the directory entry for the journal was never created) and the transaction
39198** will not roll back - possibly leading to database corruption.
39199*/
39200static int unixSync(sqlite3_file *id, int flags){
39201 int rc;
39202 unixFile *pFile = (unixFile*)id;
39203
39204 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY0x00010);
39205 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL0x00003;
39206
39207 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
39208 assert((flags&0x0F)==SQLITE_SYNC_NORMAL((void) (0))
39209 || (flags&0x0F)==SQLITE_SYNC_FULL((void) (0))
39210 )((void) (0));
39211
39212 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
39213 ** line is to test that doing so does not cause any problems.
39214 */
39215 SimulateDiskfullError( return SQLITE_FULL );
39216
39217 assert( pFile )((void) (0));
39218 OSTRACE(("SYNC %-3d\n", pFile->h));
39219 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
39220 SimulateIOError( rc=1 );
39221 if( rc ){
39222 storeLastErrno(pFile, errno(*__errno_location ()));
39223 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath)unixLogErrorAtLine((10 | (4<<8)),"full_fsync",pFile->
zPath,39223)
;
39224 }
39225
39226 /* Also fsync the directory containing the file if the DIRSYNC flag
39227 ** is set. This is a one-time occurrence. Many systems (examples: AIX)
39228 ** are unable to fsync a directory, so ignore errors on the fsync.
39229 */
39230 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC0x08 ){
39231 int dirfd;
39232 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
39233 HAVE_FULLFSYNC, isFullsync));
39234 rc = osOpenDirectory((int(*)(const char*,int*))aSyscall[17].pCurrent)(pFile->zPath, &dirfd);
39235 if( rc==SQLITE_OK0 ){
39236 full_fsync(dirfd, 0, 0);
39237 robust_close(pFile, dirfd, __LINE__39237);
39238 }else{
39239 assert( rc==SQLITE_CANTOPEN )((void) (0));
39240 rc = SQLITE_OK0;
39241 }
39242 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC0x08;
39243 }
39244 return rc;
39245}
39246
39247/*
39248** Truncate an open file to a specified size
39249*/
39250static int unixTruncate(sqlite3_file *id, i64 nByte){
39251 unixFile *pFile = (unixFile *)id;
39252 int rc;
39253 assert( pFile )((void) (0));
39254 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
39255
39256 /* If the user has configured a chunk-size for this file, truncate the
39257 ** file so that it consists of an integer number of chunks (i.e. the
39258 ** actual file size after the operation may be larger than the requested
39259 ** size).
39260 */
39261 if( pFile->szChunk>0 ){
39262 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
39263 }
39264
39265 rc = robust_ftruncate(pFile->h, nByte);
39266 if( rc ){
39267 storeLastErrno(pFile, errno(*__errno_location ()));
39268 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath)unixLogErrorAtLine((10 | (6<<8)),"ftruncate",pFile->
zPath,39268)
;
39269 }else{
39270#ifdef SQLITE_DEBUG
39271 /* If we are doing a normal write to a database file (as opposed to
39272 ** doing a hot-journal rollback or a write to some file other than a
39273 ** normal database file) and we truncate the file to zero length,
39274 ** that effectively updates the change counter. This might happen
39275 ** when restoring a database using the backup API from a zero-length
39276 ** source.
39277 */
39278 if( pFile->inNormalWrite && nByte==0 ){
39279 pFile->transCntrChng = 1;
39280 }
39281#endif
39282
39283#if SQLITE_MAX_MMAP_SIZE20971520>0
39284 /* If the file was just truncated to a size smaller than the currently
39285 ** mapped region, reduce the effective mapping size as well. SQLite will
39286 ** use read() and write() to access data beyond this point from now on.
39287 */
39288 if( nByte<pFile->mmapSize ){
39289 pFile->mmapSize = nByte;
39290 }
39291#endif
39292
39293 return SQLITE_OK0;
39294 }
39295}
39296
39297/*
39298** Determine the current size of a file in bytes
39299*/
39300static int unixFileSize(sqlite3_file *id, i64 *pSize){
39301 int rc;
39302 struct stat buf;
39303 assert( id )((void) (0));
39304 rc = osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(((unixFile*)id)->h, &buf);
39305 SimulateIOError( rc=1 );
39306 if( rc!=0 ){
39307 storeLastErrno((unixFile*)id, errno(*__errno_location ()));
39308 return SQLITE_IOERR_FSTAT(10 | (7<<8));
39309 }
39310 *pSize = buf.st_size;
39311
39312 /* When opening a zero-size database, the findInodeInfo() procedure
39313 ** writes a single byte into that file in order to work around a bug
39314 ** in the OS-X msdos filesystem. In order to avoid problems with upper
39315 ** layers, we need to report this file size as zero even though it is
39316 ** really 1. Ticket #3260.
39317 */
39318 if( *pSize==1 ) *pSize = 0;
39319
39320
39321 return SQLITE_OK0;
39322}
39323
39324#if SQLITE_ENABLE_LOCKING_STYLE0 && defined(__APPLE__)
39325/*
39326** Handler for proxy-locking file-control verbs. Defined below in the
39327** proxying locking division.
39328*/
39329static int proxyFileControl(sqlite3_file*,int,void*);
39330#endif
39331
39332/*
39333** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
39334** file-control operation. Enlarge the database to nBytes in size
39335** (rounded up to the next chunk-size). If the database is already
39336** nBytes or larger, this routine is a no-op.
39337*/
39338static int fcntlSizeHint(unixFile *pFile, i64 nByte){
39339 if( pFile->szChunk>0 ){
39340 i64 nSize; /* Required file size */
39341 struct stat buf; /* Used to hold return values of fstat() */
39342
39343 if( osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(pFile->h, &buf) ){
39344 return SQLITE_IOERR_FSTAT(10 | (7<<8));
39345 }
39346
39347 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
39348 if( nSize>(i64)buf.st_size ){
39349
39350#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
39351 /* The code below is handling the return value of osFallocate()
39352 ** correctly. posix_fallocate() is defined to "returns zero on success,
39353 ** or an error number on failure". See the manpage for details. */
39354 int err;
39355 do{
39356 err = osFallocate((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)(pFile->h, buf.st_size, nSize-buf.st_size);
39357 }while( err==EINTR4 );
39358 if( err && err!=EINVAL22 ) return SQLITE_IOERR_WRITE(10 | (3<<8));
39359#else
39360 /* If the OS does not have posix_fallocate(), fake it. Write a
39361 ** single byte to the last byte in each block that falls entirely
39362 ** within the extended region. Then, if required, a single byte
39363 ** at offset (nSize-1), to set the size of the file correctly.
39364 ** This is a similar technique to that used by glibc on systems
39365 ** that do not have a real fallocate() call.
39366 */
39367 int nBlk = buf.st_blksize; /* File-system block size */
39368 int nWrite = 0; /* Number of bytes written by seekAndWrite */
39369 i64 iWrite; /* Next offset to write to */
39370
39371 iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1;
39372 assert( iWrite>=buf.st_size )((void) (0));
39373 assert( ((iWrite+1)%nBlk)==0 )((void) (0));
39374 for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){
39375 if( iWrite>=nSize ) iWrite = nSize - 1;
39376 nWrite = seekAndWrite(pFile, iWrite, "", 1);
39377 if( nWrite!=1 ) return SQLITE_IOERR_WRITE(10 | (3<<8));
39378 }
39379#endif
39380 }
39381 }
39382
39383#if SQLITE_MAX_MMAP_SIZE20971520>0
39384 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
39385 int rc;
39386 if( pFile->szChunk<=0 ){
39387 if( robust_ftruncate(pFile->h, nByte) ){
39388 storeLastErrno(pFile, errno(*__errno_location ()));
39389 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath)unixLogErrorAtLine((10 | (6<<8)),"ftruncate",pFile->
zPath,39389)
;
39390 }
39391 }
39392
39393 rc = unixMapfile(pFile, nByte);
39394 return rc;
39395 }
39396#endif
39397
39398 return SQLITE_OK0;
39399}
39400
39401/*
39402** If *pArg is initially negative then this is a query. Set *pArg to
39403** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
39404**
39405** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
39406*/
39407static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
39408 if( *pArg<0 ){
39409 *pArg = (pFile->ctrlFlags & mask)!=0;
39410 }else if( (*pArg)==0 ){
39411 pFile->ctrlFlags &= ~mask;
39412 }else{
39413 pFile->ctrlFlags |= mask;
39414 }
39415}
39416
39417/* Forward declaration */
39418static int unixGetTempname(int nBuf, char *zBuf);
39419#ifndef SQLITE_OMIT_WAL
39420 static int unixFcntlExternalReader(unixFile*, int*);
39421#endif
39422
39423/*
39424** Information and control of an open file handle.
39425*/
39426static int unixFileControl(sqlite3_file *id, int op, void *pArg){
39427 unixFile *pFile = (unixFile*)id;
39428 switch( op ){
39429#if defined(__linux__1) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
39430 case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE31: {
39431 int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((1)) << 0) | ((0) << ((0 +8)+8)))
);
39432 return rc ? SQLITE_IOERR_BEGIN_ATOMIC(10 | (29<<8)) : SQLITE_OK0;
39433 }
39434 case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE32: {
39435 int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((2)) << 0) | ((0) << ((0 +8)+8)))
);
39436 return rc ? SQLITE_IOERR_COMMIT_ATOMIC(10 | (30<<8)) : SQLITE_OK0;
39437 }
39438 case SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE33: {
39439 int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((5)) << 0) | ((0) << ((0 +8)+8)))
);
39440 return rc ? SQLITE_IOERR_ROLLBACK_ATOMIC(10 | (31<<8)) : SQLITE_OK0;
39441 }
39442#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
39443
39444 case SQLITE_FCNTL_LOCKSTATE1: {
39445 *(int*)pArg = pFile->eFileLock;
39446 return SQLITE_OK0;
39447 }
39448 case SQLITE_FCNTL_LAST_ERRNO4: {
39449 *(int*)pArg = pFile->lastErrno;
39450 return SQLITE_OK0;
39451 }
39452 case SQLITE_FCNTL_CHUNK_SIZE6: {
39453 pFile->szChunk = *(int *)pArg;
39454 return SQLITE_OK0;
39455 }
39456 case SQLITE_FCNTL_SIZE_HINT5: {
39457 int rc;
39458 SimulateIOErrorBenign(1);
39459 rc = fcntlSizeHint(pFile, *(i64 *)pArg);
39460 SimulateIOErrorBenign(0);
39461 return rc;
39462 }
39463 case SQLITE_FCNTL_PERSIST_WAL10: {
39464 unixModeBit(pFile, UNIXFILE_PERSIST_WAL0x04, (int*)pArg);
39465 return SQLITE_OK0;
39466 }
39467 case SQLITE_FCNTL_POWERSAFE_OVERWRITE13: {
39468 unixModeBit(pFile, UNIXFILE_PSOW0x10, (int*)pArg);
39469 return SQLITE_OK0;
39470 }
39471 case SQLITE_FCNTL_VFSNAME12: {
39472 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
39473 return SQLITE_OK0;
39474 }
39475 case SQLITE_FCNTL_TEMPFILENAME16: {
39476 char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname );
39477 if( zTFile ){
39478 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
39479 *(char**)pArg = zTFile;
39480 }
39481 return SQLITE_OK0;
39482 }
39483 case SQLITE_FCNTL_HAS_MOVED20: {
39484 *(int*)pArg = fileHasMoved(pFile);
39485 return SQLITE_OK0;
39486 }
39487#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
39488 case SQLITE_FCNTL_LOCK_TIMEOUT34: {
39489 int iOld = pFile->iBusyTimeout;
39490 pFile->iBusyTimeout = *(int*)pArg;
39491 *(int*)pArg = iOld;
39492 return SQLITE_OK0;
39493 }
39494#endif
39495#if SQLITE_MAX_MMAP_SIZE20971520>0
39496 case SQLITE_FCNTL_MMAP_SIZE18: {
39497 i64 newLimit = *(i64*)pArg;
39498 int rc = SQLITE_OK0;
39499 if( newLimit>sqlite3GlobalConfigsqlite3Config.mxMmap ){
39500 newLimit = sqlite3GlobalConfigsqlite3Config.mxMmap;
39501 }
39502
39503 /* The value of newLimit may be eventually cast to (size_t) and passed
39504 ** to mmap(). Restrict its value to 2GB if (size_t) is not at least a
39505 ** 64-bit type. */
39506 if( newLimit>0 && sizeof(size_t)<8 ){
39507 newLimit = (newLimit & 0x7FFFFFFF);
39508 }
39509
39510 *(i64*)pArg = pFile->mmapSizeMax;
39511 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
39512 pFile->mmapSizeMax = newLimit;
39513 if( pFile->mmapSize>0 ){
39514 unixUnmapfile(pFile);
39515 rc = unixMapfile(pFile, -1);
39516 }
39517 }
39518 return rc;
39519 }
39520#endif
39521#ifdef SQLITE_DEBUG
39522 /* The pager calls this method to signal that it has done
39523 ** a rollback and that the database is therefore unchanged and
39524 ** it hence it is OK for the transaction change counter to be
39525 ** unchanged.
39526 */
39527 case SQLITE_FCNTL_DB_UNCHANGED0xca093fa0: {
39528 ((unixFile*)id)->dbUpdate = 0;
39529 return SQLITE_OK0;
39530 }
39531#endif
39532#if SQLITE_ENABLE_LOCKING_STYLE0 && defined(__APPLE__)
39533 case SQLITE_FCNTL_SET_LOCKPROXYFILE3:
39534 case SQLITE_FCNTL_GET_LOCKPROXYFILE2: {
39535 return proxyFileControl(id,op,pArg);
39536 }
39537#endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
39538
39539 case SQLITE_FCNTL_EXTERNAL_READER40: {
39540#ifndef SQLITE_OMIT_WAL
39541 return unixFcntlExternalReader((unixFile*)id, (int*)pArg);
39542#else
39543 *(int*)pArg = 0;
39544 return SQLITE_OK0;
39545#endif
39546 }
39547 }
39548 return SQLITE_NOTFOUND12;
39549}
39550
39551/*
39552** If pFd->sectorSize is non-zero when this function is called, it is a
39553** no-op. Otherwise, the values of pFd->sectorSize and
39554** pFd->deviceCharacteristics are set according to the file-system
39555** characteristics.
39556**
39557** There are two versions of this function. One for QNX and one for all
39558** other systems.
39559*/
39560#ifndef __QNXNTO__
39561static void setDeviceCharacteristics(unixFile *pFd){
39562 assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 )((void) (0));
39563 if( pFd->sectorSize==0 ){
39564#if defined(__linux__1) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
39565 int res;
39566 u32 f = 0;
39567
39568 /* Check for support for F2FS atomic batch writes. */
39569 res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES(((2U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((12)) << 0) | ((((sizeof(u32)))) << ((0 +8)+
8)))
, &f);
39570 if( res==0 && (f & F2FS_FEATURE_ATOMIC_WRITE0x0004) ){
39571 pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC0x00004000;
39572 }
39573#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
39574
39575 /* Set the POWERSAFE_OVERWRITE flag if requested. */
39576 if( pFd->ctrlFlags & UNIXFILE_PSOW0x10 ){
39577 pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE0x00001000;
39578 }
39579
39580 pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE4096;
39581 }
39582}
39583#else
39584#include <sys/dcmd_blk.h>
39585#include <sys/statvfs.h>
39586static void setDeviceCharacteristics(unixFile *pFile){
39587 if( pFile->sectorSize == 0 ){
39588 struct statvfs fsInfo;
39589
39590 /* Set defaults for non-supported filesystems */
39591 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE4096;
39592 pFile->deviceCharacteristics = 0;
39593 if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
39594 return;
39595 }
39596
39597 if( !strcmp(fsInfo.f_basetype, "tmp") ) {
39598 pFile->sectorSize = fsInfo.f_bsize;
39599 pFile->deviceCharacteristics =
39600 SQLITE_IOCAP_ATOMIC4K0x00000010 | /* All ram filesystem writes are atomic */
39601 SQLITE_IOCAP_SAFE_APPEND0x00000200 | /* growing the file does not occur until
39602 ** the write succeeds */
39603 SQLITE_IOCAP_SEQUENTIAL0x00000400 | /* The ram filesystem has no write behind
39604 ** so it is ordered */
39605 0;
39606 }else if( strstr(fsInfo.f_basetype, "etfs") ){
39607 pFile->sectorSize = fsInfo.f_bsize;
39608 pFile->deviceCharacteristics =
39609 /* etfs cluster size writes are atomic */
39610 (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC5120x00000002) |
39611 SQLITE_IOCAP_SAFE_APPEND0x00000200 | /* growing the file does not occur until
39612 ** the write succeeds */
39613 SQLITE_IOCAP_SEQUENTIAL0x00000400 | /* The ram filesystem has no write behind
39614 ** so it is ordered */
39615 0;
39616 }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){
39617 pFile->sectorSize = fsInfo.f_bsize;
39618 pFile->deviceCharacteristics =
39619 SQLITE_IOCAP_ATOMIC0x00000001 | /* All filesystem writes are atomic */
39620 SQLITE_IOCAP_SAFE_APPEND0x00000200 | /* growing the file does not occur until
39621 ** the write succeeds */
39622 SQLITE_IOCAP_SEQUENTIAL0x00000400 | /* The ram filesystem has no write behind
39623 ** so it is ordered */
39624 0;
39625 }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){
39626 pFile->sectorSize = fsInfo.f_bsize;
39627 pFile->deviceCharacteristics =
39628 /* full bitset of atomics from max sector size and smaller */
39629 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC5120x00000002) << 1) - 2 |
39630 SQLITE_IOCAP_SEQUENTIAL0x00000400 | /* The ram filesystem has no write behind
39631 ** so it is ordered */
39632 0;
39633 }else if( strstr(fsInfo.f_basetype, "dos") ){
39634 pFile->sectorSize = fsInfo.f_bsize;
39635 pFile->deviceCharacteristics =
39636 /* full bitset of atomics from max sector size and smaller */
39637 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC5120x00000002) << 1) - 2 |
39638 SQLITE_IOCAP_SEQUENTIAL0x00000400 | /* The ram filesystem has no write behind
39639 ** so it is ordered */
39640 0;
39641 }else{
39642 pFile->deviceCharacteristics =
39643 SQLITE_IOCAP_ATOMIC5120x00000002 | /* blocks are atomic */
39644 SQLITE_IOCAP_SAFE_APPEND0x00000200 | /* growing the file does not occur until
39645 ** the write succeeds */
39646 0;
39647 }
39648 }
39649 /* Last chance verification. If the sector size isn't a multiple of 512
39650 ** then it isn't valid.*/
39651 if( pFile->sectorSize % 512 != 0 ){
39652 pFile->deviceCharacteristics = 0;
39653 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE4096;
39654 }
39655}
39656#endif
39657
39658/*
39659** Return the sector size in bytes of the underlying block device for
39660** the specified file. This is almost always 512 bytes, but may be
39661** larger for some devices.
39662**
39663** SQLite code assumes this function cannot fail. It also assumes that
39664** if two files are created in the same file-system directory (i.e.
39665** a database and its journal file) that the sector size will be the
39666** same for both.
39667*/
39668static int unixSectorSize(sqlite3_file *id){
39669 unixFile *pFd = (unixFile*)id;
39670 setDeviceCharacteristics(pFd);
39671 return pFd->sectorSize;
39672}
39673
39674/*
39675** Return the device characteristics for the file.
39676**
39677** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
39678** However, that choice is controversial since technically the underlying
39679** file system does not always provide powersafe overwrites. (In other
39680** words, after a power-loss event, parts of the file that were never
39681** written might end up being altered.) However, non-PSOW behavior is very,
39682** very rare. And asserting PSOW makes a large reduction in the amount
39683** of required I/O for journaling, since a lot of padding is eliminated.
39684** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
39685** available to turn it off and URI query parameter available to turn it off.
39686*/
39687static int unixDeviceCharacteristics(sqlite3_file *id){
39688 unixFile *pFd = (unixFile*)id;
39689 setDeviceCharacteristics(pFd);
39690 return pFd->deviceCharacteristics;
39691}
39692
39693#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0
39694
39695/*
39696** Return the system page size.
39697**
39698** This function should not be called directly by other code in this file.
39699** Instead, it should be called via macro osGetpagesize().
39700*/
39701static int unixGetpagesize(void){
39702#if OS_VXWORKS0
39703 return 1024;
39704#elif defined(_BSD_SOURCE)
39705 return getpagesize();
39706#else
39707 return (int)sysconf(_SC_PAGESIZE_SC_PAGESIZE);
39708#endif
39709}
39710
39711#endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */
39712
39713#ifndef SQLITE_OMIT_WAL
39714
39715/*
39716** Object used to represent an shared memory buffer.
39717**
39718** When multiple threads all reference the same wal-index, each thread
39719** has its own unixShm object, but they all point to a single instance
39720** of this unixShmNode object. In other words, each wal-index is opened
39721** only once per process.
39722**
39723** Each unixShmNode object is connected to a single unixInodeInfo object.
39724** We could coalesce this object into unixInodeInfo, but that would mean
39725** every open file that does not use shared memory (in other words, most
39726** open files) would have to carry around this extra information. So
39727** the unixInodeInfo object contains a pointer to this unixShmNode object
39728** and the unixShmNode object is created only when needed.
39729**
39730** unixMutexHeld() must be true when creating or destroying
39731** this object or while reading or writing the following fields:
39732**
39733** nRef
39734**
39735** The following fields are read-only after the object is created:
39736**
39737** hShm
39738** zFilename
39739**
39740** Either unixShmNode.pShmMutex must be held or unixShmNode.nRef==0 and
39741** unixMutexHeld() is true when reading or writing any other field
39742** in this structure.
39743*/
39744struct unixShmNode {
39745 unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */
39746 sqlite3_mutex *pShmMutex; /* Mutex to access this object */
39747 char *zFilename; /* Name of the mmapped file */
39748 int hShm; /* Open file descriptor */
39749 int szRegion; /* Size of shared-memory regions */
39750 u16 nRegion; /* Size of array apRegion */
39751 u8 isReadonly; /* True if read-only */
39752 u8 isUnlocked; /* True if no DMS lock held */
39753 char **apRegion; /* Array of mapped shared-memory regions */
39754 int nRef; /* Number of unixShm objects pointing to this */
39755 unixShm *pFirst; /* All unixShm objects pointing to this */
39756 int aLock[SQLITE_SHM_NLOCK8]; /* # shared locks on slot, -1==excl lock */
39757#ifdef SQLITE_DEBUG
39758 u8 exclMask; /* Mask of exclusive locks held */
39759 u8 sharedMask; /* Mask of shared locks held */
39760 u8 nextShmId; /* Next available unixShm.id value */
39761#endif
39762};
39763
39764/*
39765** Structure used internally by this VFS to record the state of an
39766** open shared memory connection.
39767**
39768** The following fields are initialized when this object is created and
39769** are read-only thereafter:
39770**
39771** unixShm.pShmNode
39772** unixShm.id
39773**
39774** All other fields are read/write. The unixShm.pShmNode->pShmMutex must
39775** be held while accessing any read/write fields.
39776*/
39777struct unixShm {
39778 unixShmNode *pShmNode; /* The underlying unixShmNode object */
39779 unixShm *pNext; /* Next unixShm with the same unixShmNode */
39780 u8 hasMutex; /* True if holding the unixShmNode->pShmMutex */
39781 u8 id; /* Id of this connection within its unixShmNode */
39782 u16 sharedMask; /* Mask of shared locks held */
39783 u16 exclMask; /* Mask of exclusive locks held */
39784};
39785
39786/*
39787** Constants used for locking
39788*/
39789#define UNIX_SHM_BASE((22+8)*4) ((22+SQLITE_SHM_NLOCK8)*4) /* first lock byte */
39790#define UNIX_SHM_DMS(((22+8)*4)+8) (UNIX_SHM_BASE((22+8)*4)+SQLITE_SHM_NLOCK8) /* deadman switch */
39791
39792/*
39793** Use F_GETLK to check whether or not there are any readers with open
39794** wal-mode transactions in other processes on database file pFile. If
39795** no error occurs, return SQLITE_OK and set (*piOut) to 1 if there are
39796** such transactions, or 0 otherwise. If an error occurs, return an
39797** SQLite error code. The final value of *piOut is undefined in this
39798** case.
39799*/
39800static int unixFcntlExternalReader(unixFile *pFile, int *piOut){
39801 int rc = SQLITE_OK0;
39802 *piOut = 0;
39803 if( pFile->pShm){
39804 unixShmNode *pShmNode = pFile->pShm->pShmNode;
39805 struct flock f;
39806
39807 memset(&f, 0, sizeof(f));
39808 f.l_type = F_WRLCK1;
39809 f.l_whence = SEEK_SET0;
39810 f.l_start = UNIX_SHM_BASE((22+8)*4) + 3;
39811 f.l_len = SQLITE_SHM_NLOCK8 - 3;
39812
39813 sqlite3_mutex_enter(pShmNode->pShmMutex);
39814 if( osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(pShmNode->hShm, F_GETLK5, &f)<0 ){
39815 rc = SQLITE_IOERR_LOCK(10 | (15<<8));
39816 }else{
39817 *piOut = (f.l_type!=F_UNLCK2);
39818 }
39819 sqlite3_mutex_leave(pShmNode->pShmMutex);
39820 }
39821
39822 return rc;
39823}
39824
39825
39826/*
39827** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
39828**
39829** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
39830** otherwise.
39831*/
39832static int unixShmSystemLock(
39833 unixFile *pFile, /* Open connection to the WAL file */
39834 int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */
39835 int ofst, /* First byte of the locking range */
39836 int n /* Number of bytes to lock */
39837){
39838 unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
39839 struct flock f; /* The posix advisory locking structure */
39840 int rc = SQLITE_OK0; /* Result code form fcntl() */
39841
39842 /* Access to the unixShmNode object is serialized by the caller */
39843 pShmNode = pFile->pInode->pShmNode;
39844 assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) )((void) (0));
39845 assert( pShmNode->nRef>0 || unixMutexHeld() )((void) (0));
39846
39847 /* Shared locks never span more than one byte */
39848 assert( n==1 || lockType!=F_RDLCK )((void) (0));
39849
39850 /* Locks are within range */
39851 assert( n>=1 && n<=SQLITE_SHM_NLOCK )((void) (0));
39852
39853 if( pShmNode->hShm>=0 ){
39854 int res;
39855 /* Initialize the locking parameters */
39856 f.l_type = lockType;
39857 f.l_whence = SEEK_SET0;
39858 f.l_start = ofst;
39859 f.l_len = n;
39860 res = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile)((int(*)(int,int,...))aSyscall[7].pCurrent)(pShmNode->hShm
,6,&f)
;
39861 if( res==-1 ){
39862#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
39863 rc = (pFile->iBusyTimeout ? SQLITE_BUSY_TIMEOUT(5 | (3<<8)) : SQLITE_BUSY5);
39864#else
39865 rc = SQLITE_BUSY5;
39866#endif
39867 }
39868 }
39869
39870 /* Update the global lock state and do debug tracing */
39871#ifdef SQLITE_DEBUG
39872 { u16 mask;
39873 OSTRACE(("SHM-LOCK "));
39874 mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
39875 if( rc==SQLITE_OK0 ){
39876 if( lockType==F_UNLCK2 ){
39877 OSTRACE(("unlock %d ok", ofst));
39878 pShmNode->exclMask &= ~mask;
39879 pShmNode->sharedMask &= ~mask;
39880 }else if( lockType==F_RDLCK0 ){
39881 OSTRACE(("read-lock %d ok", ofst));
39882 pShmNode->exclMask &= ~mask;
39883 pShmNode->sharedMask |= mask;
39884 }else{
39885 assert( lockType==F_WRLCK )((void) (0));
39886 OSTRACE(("write-lock %d ok", ofst));
39887 pShmNode->exclMask |= mask;
39888 pShmNode->sharedMask &= ~mask;
39889 }
39890 }else{
39891 if( lockType==F_UNLCK2 ){
39892 OSTRACE(("unlock %d failed", ofst));
39893 }else if( lockType==F_RDLCK0 ){
39894 OSTRACE(("read-lock failed"));
39895 }else{
39896 assert( lockType==F_WRLCK )((void) (0));
39897 OSTRACE(("write-lock %d failed", ofst));
39898 }
39899 }
39900 OSTRACE((" - afterwards %03x,%03x\n",
39901 pShmNode->sharedMask, pShmNode->exclMask));
39902 }
39903#endif
39904
39905 return rc;
39906}
39907
39908/*
39909** Return the minimum number of 32KB shm regions that should be mapped at
39910** a time, assuming that each mapping must be an integer multiple of the
39911** current system page-size.
39912**
39913** Usually, this is 1. The exception seems to be systems that are configured
39914** to use 64KB pages - in this case each mapping must cover at least two
39915** shm regions.
39916*/
39917static int unixShmRegionPerMap(void){
39918 int shmsz = 32*1024; /* SHM region size */
39919 int pgsz = osGetpagesize((int(*)(void))aSyscall[25].pCurrent)(); /* System page size */
39920 assert( ((pgsz-1)&pgsz)==0 )((void) (0)); /* Page size must be a power of 2 */
39921 if( pgsz<shmsz ) return 1;
39922 return pgsz/shmsz;
39923}
39924
39925/*
39926** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
39927**
39928** This is not a VFS shared-memory method; it is a utility function called
39929** by VFS shared-memory methods.
39930*/
39931static void unixShmPurge(unixFile *pFd){
39932 unixShmNode *p = pFd->pInode->pShmNode;
39933 assert( unixMutexHeld() )((void) (0));
39934 if( p && ALWAYS(p->nRef==0)(p->nRef==0) ){
39935 int nShmPerMap = unixShmRegionPerMap();
39936 int i;
39937 assert( p->pInode==pFd->pInode )((void) (0));
39938 sqlite3_mutex_free(p->pShmMutex);
39939 for(i=0; i<p->nRegion; i+=nShmPerMap){
39940 if( p->hShm>=0 ){
39941 osMunmap((int(*)(void*,size_t))aSyscall[23].pCurrent)(p->apRegion[i], p->szRegion);
39942 }else{
39943 sqlite3_free(p->apRegion[i]);
39944 }
39945 }
39946 sqlite3_free(p->apRegion);
39947 if( p->hShm>=0 ){
39948 robust_close(pFd, p->hShm, __LINE__39948);
39949 p->hShm = -1;
39950 }
39951 p->pInode->pShmNode = 0;
39952 sqlite3_free(p);
39953 }
39954}
39955
39956/*
39957** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
39958** take it now. Return SQLITE_OK if successful, or an SQLite error
39959** code otherwise.
39960**
39961** If the DMS cannot be locked because this is a readonly_shm=1
39962** connection and no other process already holds a lock, return
39963** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
39964*/
39965static int unixLockSharedMemory(unixFile *pDbFd, unixShmNode *pShmNode){
39966 struct flock lock;
39967 int rc = SQLITE_OK0;
39968
39969 /* Use F_GETLK to determine the locks other processes are holding
39970 ** on the DMS byte. If it indicates that another process is holding
39971 ** a SHARED lock, then this process may also take a SHARED lock
39972 ** and proceed with opening the *-shm file.
39973 **
39974 ** Or, if no other process is holding any lock, then this process
39975 ** is the first to open it. In this case take an EXCLUSIVE lock on the
39976 ** DMS byte and truncate the *-shm file to zero bytes in size. Then
39977 ** downgrade to a SHARED lock on the DMS byte.
39978 **
39979 ** If another process is holding an EXCLUSIVE lock on the DMS byte,
39980 ** return SQLITE_BUSY to the caller (it will try again). An earlier
39981 ** version of this code attempted the SHARED lock at this point. But
39982 ** this introduced a subtle race condition: if the process holding
39983 ** EXCLUSIVE failed just before truncating the *-shm file, then this
39984 ** process might open and use the *-shm file without truncating it.
39985 ** And if the *-shm file has been corrupted by a power failure or
39986 ** system crash, the database itself may also become corrupt. */
39987 lock.l_whence = SEEK_SET0;
39988 lock.l_start = UNIX_SHM_DMS(((22+8)*4)+8);
39989 lock.l_len = 1;
39990 lock.l_type = F_WRLCK1;
39991 if( osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(pShmNode->hShm, F_GETLK5, &lock)!=0 ) {
39992 rc = SQLITE_IOERR_LOCK(10 | (15<<8));
39993 }else if( lock.l_type==F_UNLCK2 ){
39994 if( pShmNode->isReadonly ){
39995 pShmNode->isUnlocked = 1;
39996 rc = SQLITE_READONLY_CANTINIT(8 | (5<<8));
39997 }else{
39998 rc = unixShmSystemLock(pDbFd, F_WRLCK1, UNIX_SHM_DMS(((22+8)*4)+8), 1);
39999 /* The first connection to attach must truncate the -shm file. We
40000 ** truncate to 3 bytes (an arbitrary small number, less than the
40001 ** -shm header size) rather than 0 as a system debugging aid, to
40002 ** help detect if a -shm file truncation is legitimate or is the work
40003 ** or a rogue process. */
40004 if( rc==SQLITE_OK0 && robust_ftruncate(pShmNode->hShm, 3) ){
40005 rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename)unixLogErrorAtLine((10 | (18<<8)),"ftruncate",pShmNode->
zFilename,40005)
;
40006 }
40007 }
40008 }else if( lock.l_type==F_WRLCK1 ){
40009 rc = SQLITE_BUSY5;
40010 }
40011
40012 if( rc==SQLITE_OK0 ){
40013 assert( lock.l_type==F_UNLCK || lock.l_type==F_RDLCK )((void) (0));
40014 rc = unixShmSystemLock(pDbFd, F_RDLCK0, UNIX_SHM_DMS(((22+8)*4)+8), 1);
40015 }
40016 return rc;
40017}
40018
40019/*
40020** Open a shared-memory area associated with open database file pDbFd.
40021** This particular implementation uses mmapped files.
40022**
40023** The file used to implement shared-memory is in the same directory
40024** as the open database file and has the same name as the open database
40025** file with the "-shm" suffix added. For example, if the database file
40026** is "/home/user1/config.db" then the file that is created and mmapped
40027** for shared memory will be called "/home/user1/config.db-shm".
40028**
40029** Another approach to is to use files in /dev/shm or /dev/tmp or an
40030** some other tmpfs mount. But if a file in a different directory
40031** from the database file is used, then differing access permissions
40032** or a chroot() might cause two different processes on the same
40033** database to end up using different files for shared memory -
40034** meaning that their memory would not really be shared - resulting
40035** in database corruption. Nevertheless, this tmpfs file usage
40036** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
40037** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time
40038** option results in an incompatible build of SQLite; builds of SQLite
40039** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the
40040** same database file at the same time, database corruption will likely
40041** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
40042** "unsupported" and may go away in a future SQLite release.
40043**
40044** When opening a new shared-memory file, if no other instances of that
40045** file are currently open, in this process or in other processes, then
40046** the file must be truncated to zero length or have its header cleared.
40047**
40048** If the original database file (pDbFd) is using the "unix-excl" VFS
40049** that means that an exclusive lock is held on the database file and
40050** that no other processes are able to read or write the database. In
40051** that case, we do not really need shared memory. No shared memory
40052** file is created. The shared memory will be simulated with heap memory.
40053*/
40054static int unixOpenSharedMemory(unixFile *pDbFd){
40055 struct unixShm *p = 0; /* The connection to be opened */
40056 struct unixShmNode *pShmNode; /* The underlying mmapped file */
40057 int rc = SQLITE_OK0; /* Result code */
40058 unixInodeInfo *pInode; /* The inode of fd */
40059 char *zShm; /* Name of the file used for SHM */
40060 int nShmFilename; /* Size of the SHM filename in bytes */
40061
40062 /* Allocate space for the new unixShm object. */
40063 p = sqlite3_malloc64( sizeof(*p) );
40064 if( p==0 ) return SQLITE_NOMEM_BKPT7;
40065 memset(p, 0, sizeof(*p));
40066 assert( pDbFd->pShm==0 )((void) (0));
40067
40068 /* Check to see if a unixShmNode object already exists. Reuse an existing
40069 ** one if present. Create a new one if necessary.
40070 */
40071 assert( unixFileMutexNotheld(pDbFd) )((void) (0));
40072 unixEnterMutex();
40073 pInode = pDbFd->pInode;
40074 pShmNode = pInode->pShmNode;
40075 if( pShmNode==0 ){
40076 struct stat sStat; /* fstat() info for database file */
40077#ifndef SQLITE_SHM_DIRECTORY
40078 const char *zBasePath = pDbFd->zPath;
40079#endif
40080
40081 /* Call fstat() to figure out the permissions on the database file. If
40082 ** a new *-shm file is created, an attempt will be made to create it
40083 ** with the same permissions.
40084 */
40085 if( osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(pDbFd->h, &sStat) ){
40086 rc = SQLITE_IOERR_FSTAT(10 | (7<<8));
40087 goto shm_open_err;
40088 }
40089
40090#ifdef SQLITE_SHM_DIRECTORY
40091 nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
40092#else
40093 nShmFilename = 6 + (int)strlen(zBasePath);
40094#endif
40095 pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename );
40096 if( pShmNode==0 ){
40097 rc = SQLITE_NOMEM_BKPT7;
40098 goto shm_open_err;
40099 }
40100 memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
40101 zShm = pShmNode->zFilename = (char*)&pShmNode[1];
40102#ifdef SQLITE_SHM_DIRECTORY
40103 sqlite3_snprintf(nShmFilename, zShm,
40104 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
40105 (u32)sStat.st_ino, (u32)sStat.st_dev);
40106#else
40107 sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath);
40108 sqlite3FileSuffix3(pDbFd->zPath, zShm);
40109#endif
40110 pShmNode->hShm = -1;
40111 pDbFd->pInode->pShmNode = pShmNode;
40112 pShmNode->pInode = pDbFd->pInode;
40113 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
40114 pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST0);
40115 if( pShmNode->pShmMutex==0 ){
40116 rc = SQLITE_NOMEM_BKPT7;
40117 goto shm_open_err;
40118 }
40119 }
40120
40121 if( pInode->bProcessLock==0 ){
40122 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
40123 pShmNode->hShm = robust_open(zShm, O_RDWR02|O_CREAT0100|O_NOFOLLOW0400000,
40124 (sStat.st_mode&0777));
40125 }
40126 if( pShmNode->hShm<0 ){
40127 pShmNode->hShm = robust_open(zShm, O_RDONLY00|O_NOFOLLOW0400000,
40128 (sStat.st_mode&0777));
40129 if( pShmNode->hShm<0 ){
40130 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShm)unixLogErrorAtLine(sqlite3CantopenError(40130),"open",zShm,40130
)
;
40131 goto shm_open_err;
40132 }
40133 pShmNode->isReadonly = 1;
40134 }
40135
40136 /* If this process is running as root, make sure that the SHM file
40137 ** is owned by the same user that owns the original database. Otherwise,
40138 ** the original owner will not be able to connect.
40139 */
40140 robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid);
40141
40142 rc = unixLockSharedMemory(pDbFd, pShmNode);
40143 if( rc!=SQLITE_OK0 && rc!=SQLITE_READONLY_CANTINIT(8 | (5<<8)) ) goto shm_open_err;
40144 }
40145 }
40146
40147 /* Make the new connection a child of the unixShmNode */
40148 p->pShmNode = pShmNode;
40149#ifdef SQLITE_DEBUG
40150 p->id = pShmNode->nextShmId++;
40151#endif
40152 pShmNode->nRef++;
40153 pDbFd->pShm = p;
40154 unixLeaveMutex();
40155
40156 /* The reference count on pShmNode has already been incremented under
40157 ** the cover of the unixEnterMutex() mutex and the pointer from the
40158 ** new (struct unixShm) object to the pShmNode has been set. All that is
40159 ** left to do is to link the new object into the linked list starting
40160 ** at pShmNode->pFirst. This must be done while holding the
40161 ** pShmNode->pShmMutex.
40162 */
40163 sqlite3_mutex_enter(pShmNode->pShmMutex);
40164 p->pNext = pShmNode->pFirst;
40165 pShmNode->pFirst = p;
40166 sqlite3_mutex_leave(pShmNode->pShmMutex);
40167 return rc;
40168
40169 /* Jump here on any error */
40170shm_open_err:
40171 unixShmPurge(pDbFd); /* This call frees pShmNode if required */
40172 sqlite3_free(p);
40173 unixLeaveMutex();
40174 return rc;
40175}
40176
40177/*
40178** This function is called to obtain a pointer to region iRegion of the
40179** shared-memory associated with the database file fd. Shared-memory regions
40180** are numbered starting from zero. Each shared-memory region is szRegion
40181** bytes in size.
40182**
40183** If an error occurs, an error code is returned and *pp is set to NULL.
40184**
40185** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
40186** region has not been allocated (by any client, including one running in a
40187** separate process), then *pp is set to NULL and SQLITE_OK returned. If
40188** bExtend is non-zero and the requested shared-memory region has not yet
40189** been allocated, it is allocated by this function.
40190**
40191** If the shared-memory region has already been allocated or is allocated by
40192** this call as described above, then it is mapped into this processes
40193** address space (if it is not already), *pp is set to point to the mapped
40194** memory and SQLITE_OK returned.
40195*/
40196static int unixShmMap(
40197 sqlite3_file *fd, /* Handle open on database file */
40198 int iRegion, /* Region to retrieve */
40199 int szRegion, /* Size of regions */
40200 int bExtend, /* True to extend file if necessary */
40201 void volatile **pp /* OUT: Mapped memory */
40202){
40203 unixFile *pDbFd = (unixFile*)fd;
40204 unixShm *p;
40205 unixShmNode *pShmNode;
40206 int rc = SQLITE_OK0;
40207 int nShmPerMap = unixShmRegionPerMap();
40208 int nReqRegion;
40209
40210 /* If the shared-memory file has not yet been opened, open it now. */
40211 if( pDbFd->pShm==0 ){
40212 rc = unixOpenSharedMemory(pDbFd);
40213 if( rc!=SQLITE_OK0 ) return rc;
40214 }
40215
40216 p = pDbFd->pShm;
40217 pShmNode = p->pShmNode;
40218 sqlite3_mutex_enter(pShmNode->pShmMutex);
40219 if( pShmNode->isUnlocked ){
40220 rc = unixLockSharedMemory(pDbFd, pShmNode);
40221 if( rc!=SQLITE_OK0 ) goto shmpage_out;
40222 pShmNode->isUnlocked = 0;
40223 }
40224 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 )((void) (0));
40225 assert( pShmNode->pInode==pDbFd->pInode )((void) (0));
40226 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 )((void) (0));
40227 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 )((void) (0));
40228
40229 /* Minimum number of regions required to be mapped. */
40230 nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap;
40231
40232 if( pShmNode->nRegion<nReqRegion ){
40233 char **apNew; /* New apRegion[] array */
40234 int nByte = nReqRegion*szRegion; /* Minimum required file size */
40235 struct stat sStat; /* Used by fstat() */
40236
40237 pShmNode->szRegion = szRegion;
40238
40239 if( pShmNode->hShm>=0 ){
40240 /* The requested region is not mapped into this processes address space.
40241 ** Check to see if it has been allocated (i.e. if the wal-index file is
40242 ** large enough to contain the requested region).
40243 */
40244 if( osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(pShmNode->hShm, &sStat) ){
40245 rc = SQLITE_IOERR_SHMSIZE(10 | (19<<8));
40246 goto shmpage_out;
40247 }
40248
40249 if( sStat.st_size<nByte ){
40250 /* The requested memory region does not exist. If bExtend is set to
40251 ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
40252 */
40253 if( !bExtend ){
40254 goto shmpage_out;
40255 }
40256
40257 /* Alternatively, if bExtend is true, extend the file. Do this by
40258 ** writing a single byte to the end of each (OS) page being
40259 ** allocated or extended. Technically, we need only write to the
40260 ** last page in order to extend the file. But writing to all new
40261 ** pages forces the OS to allocate them immediately, which reduces
40262 ** the chances of SIGBUS while accessing the mapped region later on.
40263 */
40264 else{
40265 static const int pgsz = 4096;
40266 int iPg;
40267
40268 /* Write to the last byte of each newly allocated or extended page */
40269 assert( (nByte % pgsz)==0 )((void) (0));
40270 for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){
40271 int x = 0;
40272 if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){
40273 const char *zFile = pShmNode->zFilename;
40274 rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile)unixLogErrorAtLine((10 | (19<<8)),"write",zFile,40274);
40275 goto shmpage_out;
40276 }
40277 }
40278 }
40279 }
40280 }
40281
40282 /* Map the requested memory region into this processes address space. */
40283 apNew = (char **)sqlite3_realloc(
40284 pShmNode->apRegion, nReqRegion*sizeof(char *)
40285 );
40286 if( !apNew ){
40287 rc = SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
40288 goto shmpage_out;
40289 }
40290 pShmNode->apRegion = apNew;
40291 while( pShmNode->nRegion<nReqRegion ){
40292 int nMap = szRegion*nShmPerMap;
40293 int i;
40294 void *pMem;
40295 if( pShmNode->hShm>=0 ){
40296 pMem = osMmap((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent
)
(0, nMap,
40297 pShmNode->isReadonly ? PROT_READ0x1 : PROT_READ0x1|PROT_WRITE0x2,
40298 MAP_SHARED0x01, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion
40299 );
40300 if( pMem==MAP_FAILED((void *) -1) ){
40301 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename)unixLogErrorAtLine((10 | (21<<8)),"mmap",pShmNode->zFilename
,40301)
;
40302 goto shmpage_out;
40303 }
40304 }else{
40305 pMem = sqlite3_malloc64(nMap);
40306 if( pMem==0 ){
40307 rc = SQLITE_NOMEM_BKPT7;
40308 goto shmpage_out;
40309 }
40310 memset(pMem, 0, nMap);
40311 }
40312
40313 for(i=0; i<nShmPerMap; i++){
40314 pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
40315 }
40316 pShmNode->nRegion += nShmPerMap;
40317 }
40318 }
40319
40320shmpage_out:
40321 if( pShmNode->nRegion>iRegion ){
40322 *pp = pShmNode->apRegion[iRegion];
40323 }else{
40324 *pp = 0;
40325 }
40326 if( pShmNode->isReadonly && rc==SQLITE_OK0 ) rc = SQLITE_READONLY8;
40327 sqlite3_mutex_leave(pShmNode->pShmMutex);
40328 return rc;
40329}
40330
40331/*
40332** Check that the pShmNode->aLock[] array comports with the locking bitmasks
40333** held by each client. Return true if it does, or false otherwise. This
40334** is to be used in an assert(). e.g.
40335**
40336** assert( assertLockingArrayOk(pShmNode) );
40337*/
40338#ifdef SQLITE_DEBUG
40339static int assertLockingArrayOk(unixShmNode *pShmNode){
40340 unixShm *pX;
40341 int aLock[SQLITE_SHM_NLOCK8];
40342 assert( sqlite3_mutex_held(pShmNode->pShmMutex) )((void) (0));
40343
40344 memset(aLock, 0, sizeof(aLock));
40345 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
40346 int i;
40347 for(i=0; i<SQLITE_SHM_NLOCK8; i++){
40348 if( pX->exclMask & (1<<i) ){
40349 assert( aLock[i]==0 )((void) (0));
40350 aLock[i] = -1;
40351 }else if( pX->sharedMask & (1<<i) ){
40352 assert( aLock[i]>=0 )((void) (0));
40353 aLock[i]++;
40354 }
40355 }
40356 }
40357
40358 assert( 0==memcmp(pShmNode->aLock, aLock, sizeof(aLock)) )((void) (0));
40359 return (memcmp(pShmNode->aLock, aLock, sizeof(aLock))==0);
40360}
40361#endif
40362
40363/*
40364** Change the lock state for a shared-memory segment.
40365**
40366** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
40367** different here than in posix. In xShmLock(), one can go from unlocked
40368** to shared and back or from unlocked to exclusive and back. But one may
40369** not go from shared to exclusive or from exclusive to shared.
40370*/
40371static int unixShmLock(
40372 sqlite3_file *fd, /* Database file holding the shared memory */
40373 int ofst, /* First lock to acquire or release */
40374 int n, /* Number of locks to acquire or release */
40375 int flags /* What to do with the lock */
40376){
40377 unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */
40378 unixShm *p; /* The shared memory being locked */
40379 unixShmNode *pShmNode; /* The underlying file iNode */
40380 int rc = SQLITE_OK0; /* Result code */
40381 u16 mask; /* Mask of locks to take or release */
40382 int *aLock;
40383
40384 p = pDbFd->pShm;
40385 if( p==0 ) return SQLITE_IOERR_SHMLOCK(10 | (20<<8));
40386 pShmNode = p->pShmNode;
40387 if( NEVER(pShmNode==0)(pShmNode==0) ) return SQLITE_IOERR_SHMLOCK(10 | (20<<8));
40388 aLock = pShmNode->aLock;
40389
40390 assert( pShmNode==pDbFd->pInode->pShmNode )((void) (0));
40391 assert( pShmNode->pInode==pDbFd->pInode )((void) (0));
40392 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK )((void) (0));
40393 assert( n>=1 )((void) (0));
40394 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)((void) (0))
40395 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)((void) (0))
40396 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)((void) (0))
40397 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) )((void) (0));
40398 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 )((void) (0));
40399 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 )((void) (0));
40400 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 )((void) (0));
40401
40402 /* Check that, if this to be a blocking lock, no locks that occur later
40403 ** in the following list than the lock being obtained are already held:
40404 **
40405 ** 1. Checkpointer lock (ofst==1).
40406 ** 2. Write lock (ofst==0).
40407 ** 3. Read locks (ofst>=3 && ofst<SQLITE_SHM_NLOCK).
40408 **
40409 ** In other words, if this is a blocking lock, none of the locks that
40410 ** occur later in the above list than the lock being obtained may be
40411 ** held.
40412 **
40413 ** It is not permitted to block on the RECOVER lock.
40414 */
40415#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
40416 assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (((void) (0))
40417 (ofst!=2) /* not RECOVER */((void) (0))
40418 && (ofst!=1 || (p->exclMask|p->sharedMask)==0)((void) (0))
40419 && (ofst!=0 || (p->exclMask|p->sharedMask)<3)((void) (0))
40420 && (ofst<3 || (p->exclMask|p->sharedMask)<(1<<ofst))((void) (0))
40421 ))((void) (0));
40422#endif
40423
40424 mask = (1<<(ofst+n)) - (1<<ofst);
40425 assert( n>1 || mask==(1<<ofst) )((void) (0));
40426 sqlite3_mutex_enter(pShmNode->pShmMutex);
40427 assert( assertLockingArrayOk(pShmNode) )((void) (0));
40428 if( flags & SQLITE_SHM_UNLOCK1 ){
40429 if( (p->exclMask|p->sharedMask) & mask ){
40430 int ii;
40431 int bUnlock = 1;
40432
40433 for(ii=ofst; ii<ofst+n; ii++){
40434 if( aLock[ii]>((p->sharedMask & (1<<ii)) ? 1 : 0) ){
40435 bUnlock = 0;
40436 }
40437 }
40438
40439 if( bUnlock ){
40440 rc = unixShmSystemLock(pDbFd, F_UNLCK2, ofst+UNIX_SHM_BASE((22+8)*4), n);
40441 if( rc==SQLITE_OK0 ){
40442 memset(&aLock[ofst], 0, sizeof(int)*n);
40443 }
40444 }else if( ALWAYS(p->sharedMask & (1<<ofst))(p->sharedMask & (1<<ofst)) ){
40445 assert( n==1 && aLock[ofst]>1 )((void) (0));
40446 aLock[ofst]--;
40447 }
40448
40449 /* Undo the local locks */
40450 if( rc==SQLITE_OK0 ){
40451 p->exclMask &= ~mask;
40452 p->sharedMask &= ~mask;
40453 }
40454 }
40455 }else if( flags & SQLITE_SHM_SHARED4 ){
40456 assert( n==1 )((void) (0));
40457 assert( (p->exclMask & (1<<ofst))==0 )((void) (0));
40458 if( (p->sharedMask & mask)==0 ){
40459 if( aLock[ofst]<0 ){
40460 rc = SQLITE_BUSY5;
40461 }else if( aLock[ofst]==0 ){
40462 rc = unixShmSystemLock(pDbFd, F_RDLCK0, ofst+UNIX_SHM_BASE((22+8)*4), n);
40463 }
40464
40465 /* Get the local shared locks */
40466 if( rc==SQLITE_OK0 ){
40467 p->sharedMask |= mask;
40468 aLock[ofst]++;
40469 }
40470 }
40471 }else{
40472 /* Make sure no sibling connections hold locks that will block this
40473 ** lock. If any do, return SQLITE_BUSY right away. */
40474 int ii;
40475 for(ii=ofst; ii<ofst+n; ii++){
40476 assert( (p->sharedMask & mask)==0 )((void) (0));
40477 if( ALWAYS((p->exclMask & (1<<ii))==0)((p->exclMask & (1<<ii))==0) && aLock[ii] ){
40478 rc = SQLITE_BUSY5;
40479 break;
40480 }
40481 }
40482
40483 /* Get the exclusive locks at the system level. Then if successful
40484 ** also update the in-memory values. */
40485 if( rc==SQLITE_OK0 ){
40486 rc = unixShmSystemLock(pDbFd, F_WRLCK1, ofst+UNIX_SHM_BASE((22+8)*4), n);
40487 if( rc==SQLITE_OK0 ){
40488 assert( (p->sharedMask & mask)==0 )((void) (0));
40489 p->exclMask |= mask;
40490 for(ii=ofst; ii<ofst+n; ii++){
40491 aLock[ii] = -1;
40492 }
40493 }
40494 }
40495 }
40496 assert( assertLockingArrayOk(pShmNode) )((void) (0));
40497 sqlite3_mutex_leave(pShmNode->pShmMutex);
40498 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
40499 p->id, osGetpid(0), p->sharedMask, p->exclMask));
40500 return rc;
40501}
40502
40503/*
40504** Implement a memory barrier or memory fence on shared memory.
40505**
40506** All loads and stores begun before the barrier must complete before
40507** any load or store begun after the barrier.
40508*/
40509static void unixShmBarrier(
40510 sqlite3_file *fd /* Database file holding the shared memory */
40511){
40512 UNUSED_PARAMETER(fd)(void)(fd);
40513 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
40514 assert( fd->pMethods->xLock==nolockLock((void) (0))
40515 || unixFileMutexNotheld((unixFile*)fd)((void) (0))
40516 )((void) (0));
40517 unixEnterMutex(); /* Also mutex, for redundancy */
40518 unixLeaveMutex();
40519}
40520
40521/*
40522** Close a connection to shared-memory. Delete the underlying
40523** storage if deleteFlag is true.
40524**
40525** If there is no shared memory associated with the connection then this
40526** routine is a harmless no-op.
40527*/
40528static int unixShmUnmap(
40529 sqlite3_file *fd, /* The underlying database file */
40530 int deleteFlag /* Delete shared-memory if true */
40531){
40532 unixShm *p; /* The connection to be closed */
40533 unixShmNode *pShmNode; /* The underlying shared-memory file */
40534 unixShm **pp; /* For looping over sibling connections */
40535 unixFile *pDbFd; /* The underlying database file */
40536
40537 pDbFd = (unixFile*)fd;
40538 p = pDbFd->pShm;
40539 if( p==0 ) return SQLITE_OK0;
40540 pShmNode = p->pShmNode;
40541
40542 assert( pShmNode==pDbFd->pInode->pShmNode )((void) (0));
40543 assert( pShmNode->pInode==pDbFd->pInode )((void) (0));
40544
40545 /* Remove connection p from the set of connections associated
40546 ** with pShmNode */
40547 sqlite3_mutex_enter(pShmNode->pShmMutex);
40548 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
40549 *pp = p->pNext;
40550
40551 /* Free the connection p */
40552 sqlite3_free(p);
40553 pDbFd->pShm = 0;
40554 sqlite3_mutex_leave(pShmNode->pShmMutex);
40555
40556 /* If pShmNode->nRef has reached 0, then close the underlying
40557 ** shared-memory file, too */
40558 assert( unixFileMutexNotheld(pDbFd) )((void) (0));
40559 unixEnterMutex();
40560 assert( pShmNode->nRef>0 )((void) (0));
40561 pShmNode->nRef--;
40562 if( pShmNode->nRef==0 ){
40563 if( deleteFlag && pShmNode->hShm>=0 ){
40564 osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(pShmNode->zFilename);
40565 }
40566 unixShmPurge(pDbFd);
40567 }
40568 unixLeaveMutex();
40569
40570 return SQLITE_OK0;
40571}
40572
40573
40574#else
40575# define unixShmMap 0
40576# define unixShmLock 0
40577# define unixShmBarrier 0
40578# define unixShmUnmap 0
40579#endif /* #ifndef SQLITE_OMIT_WAL */
40580
40581#if SQLITE_MAX_MMAP_SIZE20971520>0
40582/*
40583** If it is currently memory mapped, unmap file pFd.
40584*/
40585static void unixUnmapfile(unixFile *pFd){
40586 assert( pFd->nFetchOut==0 )((void) (0));
40587 if( pFd->pMapRegion ){
40588 osMunmap((int(*)(void*,size_t))aSyscall[23].pCurrent)(pFd->pMapRegion, pFd->mmapSizeActual);
40589 pFd->pMapRegion = 0;
40590 pFd->mmapSize = 0;
40591 pFd->mmapSizeActual = 0;
40592 }
40593}
40594
40595/*
40596** Attempt to set the size of the memory mapping maintained by file
40597** descriptor pFd to nNew bytes. Any existing mapping is discarded.
40598**
40599** If successful, this function sets the following variables:
40600**
40601** unixFile.pMapRegion
40602** unixFile.mmapSize
40603** unixFile.mmapSizeActual
40604**
40605** If unsuccessful, an error message is logged via sqlite3_log() and
40606** the three variables above are zeroed. In this case SQLite should
40607** continue accessing the database using the xRead() and xWrite()
40608** methods.
40609*/
40610static void unixRemapfile(
40611 unixFile *pFd, /* File descriptor object */
40612 i64 nNew /* Required mapping size */
40613){
40614 const char *zErr = "mmap";
40615 int h = pFd->h; /* File descriptor open on db file */
40616 u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */
40617 i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */
40618 u8 *pNew = 0; /* Location of new mapping */
40619 int flags = PROT_READ0x1; /* Flags to pass to mmap() */
40620
40621 assert( pFd->nFetchOut==0 )((void) (0));
40622 assert( nNew>pFd->mmapSize )((void) (0));
40623 assert( nNew<=pFd->mmapSizeMax )((void) (0));
40624 assert( nNew>0 )((void) (0));
40625 assert( pFd->mmapSizeActual>=pFd->mmapSize )((void) (0));
40626 assert( MAP_FAILED!=0 )((void) (0));
40627
40628#ifdef SQLITE_MMAP_READWRITE
40629 if( (pFd->ctrlFlags & UNIXFILE_RDONLY0x02)==0 ) flags |= PROT_WRITE0x2;
40630#endif
40631
40632 if( pOrig ){
40633#if HAVE_MREMAP1
40634 i64 nReuse = pFd->mmapSize;
40635#else
40636 const int szSyspage = osGetpagesize((int(*)(void))aSyscall[25].pCurrent)();
40637 i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
40638#endif
40639 u8 *pReq = &pOrig[nReuse];
40640
40641 /* Unmap any pages of the existing mapping that cannot be reused. */
40642 if( nReuse!=nOrig ){
40643 osMunmap((int(*)(void*,size_t))aSyscall[23].pCurrent)(pReq, nOrig-nReuse);
40644 }
40645
40646#if HAVE_MREMAP1
40647 pNew = osMremap((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent
)
(pOrig, nReuse, nNew, MREMAP_MAYMOVE1);
40648 zErr = "mremap";
40649#else
40650 pNew = osMmap((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent
)
(pReq, nNew-nReuse, flags, MAP_SHARED0x01, h, nReuse);
40651 if( pNew!=MAP_FAILED((void *) -1) ){
40652 if( pNew!=pReq ){
40653 osMunmap((int(*)(void*,size_t))aSyscall[23].pCurrent)(pNew, nNew - nReuse);
40654 pNew = 0;
40655 }else{
40656 pNew = pOrig;
40657 }
40658 }
40659#endif
40660
40661 /* The attempt to extend the existing mapping failed. Free it. */
40662 if( pNew==MAP_FAILED((void *) -1) || pNew==0 ){
40663 osMunmap((int(*)(void*,size_t))aSyscall[23].pCurrent)(pOrig, nReuse);
40664 }
40665 }
40666
40667 /* If pNew is still NULL, try to create an entirely new mapping. */
40668 if( pNew==0 ){
40669 pNew = osMmap((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent
)
(0, nNew, flags, MAP_SHARED0x01, h, 0);
40670 }
40671
40672 if( pNew==MAP_FAILED((void *) -1) ){
40673 pNew = 0;
40674 nNew = 0;
40675 unixLogError(SQLITE_OK, zErr, pFd->zPath)unixLogErrorAtLine(0,zErr,pFd->zPath,40675);
40676
40677 /* If the mmap() above failed, assume that all subsequent mmap() calls
40678 ** will probably fail too. Fall back to using xRead/xWrite exclusively
40679 ** in this case. */
40680 pFd->mmapSizeMax = 0;
40681 }
40682 pFd->pMapRegion = (void *)pNew;
40683 pFd->mmapSize = pFd->mmapSizeActual = nNew;
40684}
40685
40686/*
40687** Memory map or remap the file opened by file-descriptor pFd (if the file
40688** is already mapped, the existing mapping is replaced by the new). Or, if
40689** there already exists a mapping for this file, and there are still
40690** outstanding xFetch() references to it, this function is a no-op.
40691**
40692** If parameter nByte is non-negative, then it is the requested size of
40693** the mapping to create. Otherwise, if nByte is less than zero, then the
40694** requested size is the size of the file on disk. The actual size of the
40695** created mapping is either the requested size or the value configured
40696** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller.
40697**
40698** SQLITE_OK is returned if no error occurs (even if the mapping is not
40699** recreated as a result of outstanding references) or an SQLite error
40700** code otherwise.
40701*/
40702static int unixMapfile(unixFile *pFd, i64 nMap){
40703 assert( nMap>=0 || pFd->nFetchOut==0 )((void) (0));
40704 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) )((void) (0));
40705 if( pFd->nFetchOut>0 ) return SQLITE_OK0;
40706
40707 if( nMap<0 ){
40708 struct stat statbuf; /* Low-level file information */
40709 if( osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(pFd->h, &statbuf) ){
40710 return SQLITE_IOERR_FSTAT(10 | (7<<8));
40711 }
40712 nMap = statbuf.st_size;
40713 }
40714 if( nMap>pFd->mmapSizeMax ){
40715 nMap = pFd->mmapSizeMax;
40716 }
40717
40718 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) )((void) (0));
40719 if( nMap!=pFd->mmapSize ){
40720 unixRemapfile(pFd, nMap);
40721 }
40722
40723 return SQLITE_OK0;
40724}
40725#endif /* SQLITE_MAX_MMAP_SIZE>0 */
40726
40727/*
40728** If possible, return a pointer to a mapping of file fd starting at offset
40729** iOff. The mapping must be valid for at least nAmt bytes.
40730**
40731** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
40732** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
40733** Finally, if an error does occur, return an SQLite error code. The final
40734** value of *pp is undefined in this case.
40735**
40736** If this function does return a pointer, the caller must eventually
40737** release the reference by calling unixUnfetch().
40738*/
40739static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
40740#if SQLITE_MAX_MMAP_SIZE20971520>0
40741 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
40742#endif
40743 *pp = 0;
40744
40745#if SQLITE_MAX_MMAP_SIZE20971520>0
40746 if( pFd->mmapSizeMax>0 ){
40747 if( pFd->pMapRegion==0 ){
40748 int rc = unixMapfile(pFd, -1);
40749 if( rc!=SQLITE_OK0 ) return rc;
40750 }
40751 if( pFd->mmapSize >= iOff+nAmt ){
40752 *pp = &((u8 *)pFd->pMapRegion)[iOff];
40753 pFd->nFetchOut++;
40754 }
40755 }
40756#endif
40757 return SQLITE_OK0;
40758}
40759
40760/*
40761** If the third argument is non-NULL, then this function releases a
40762** reference obtained by an earlier call to unixFetch(). The second
40763** argument passed to this function must be the same as the corresponding
40764** argument that was passed to the unixFetch() invocation.
40765**
40766** Or, if the third argument is NULL, then this function is being called
40767** to inform the VFS layer that, according to POSIX, any existing mapping
40768** may now be invalid and should be unmapped.
40769*/
40770static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
40771#if SQLITE_MAX_MMAP_SIZE20971520>0
40772 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
40773 UNUSED_PARAMETER(iOff)(void)(iOff);
40774
40775 /* If p==0 (unmap the entire file) then there must be no outstanding
40776 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
40777 ** then there must be at least one outstanding. */
40778 assert( (p==0)==(pFd->nFetchOut==0) )((void) (0));
40779
40780 /* If p!=0, it must match the iOff value. */
40781 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] )((void) (0));
40782
40783 if( p ){
40784 pFd->nFetchOut--;
40785 }else{
40786 unixUnmapfile(pFd);
40787 }
40788
40789 assert( pFd->nFetchOut>=0 )((void) (0));
40790#else
40791 UNUSED_PARAMETER(fd)(void)(fd);
40792 UNUSED_PARAMETER(p)(void)(p);
40793 UNUSED_PARAMETER(iOff)(void)(iOff);
40794#endif
40795 return SQLITE_OK0;
40796}
40797
40798/*
40799** Here ends the implementation of all sqlite3_file methods.
40800**
40801********************** End sqlite3_file Methods *******************************
40802******************************************************************************/
40803
40804/*
40805** This division contains definitions of sqlite3_io_methods objects that
40806** implement various file locking strategies. It also contains definitions
40807** of "finder" functions. A finder-function is used to locate the appropriate
40808** sqlite3_io_methods object for a particular database file. The pAppData
40809** field of the sqlite3_vfs VFS objects are initialized to be pointers to
40810** the correct finder-function for that VFS.
40811**
40812** Most finder functions return a pointer to a fixed sqlite3_io_methods
40813** object. The only interesting finder-function is autolockIoFinder, which
40814** looks at the filesystem type and tries to guess the best locking
40815** strategy from that.
40816**
40817** For finder-function F, two objects are created:
40818**
40819** (1) The real finder-function named "FImpt()".
40820**
40821** (2) A constant pointer to this function named just "F".
40822**
40823**
40824** A pointer to the F pointer is used as the pAppData value for VFS
40825** objects. We have to do this instead of letting pAppData point
40826** directly at the finder-function since C90 rules prevent a void*
40827** from be cast into a function pointer.
40828**
40829**
40830** Each instance of this macro generates two objects:
40831**
40832** * A constant sqlite3_io_methods object call METHOD that has locking
40833** methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
40834**
40835** * An I/O method finder function called FINDER that returns a pointer
40836** to the METHOD object in the previous bullet.
40837*/
40838#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;
\
40839static const sqlite3_io_methods METHOD = { \
40840 VERSION, /* iVersion */ \
40841 CLOSE, /* xClose */ \
40842 unixRead, /* xRead */ \
40843 unixWrite, /* xWrite */ \
40844 unixTruncate, /* xTruncate */ \
40845 unixSync, /* xSync */ \
40846 unixFileSize, /* xFileSize */ \
40847 LOCK, /* xLock */ \
40848 UNLOCK, /* xUnlock */ \
40849 CKLOCK, /* xCheckReservedLock */ \
40850 unixFileControl, /* xFileControl */ \
40851 unixSectorSize, /* xSectorSize */ \
40852 unixDeviceCharacteristics, /* xDeviceCapabilities */ \
40853 SHMMAP, /* xShmMap */ \
40854 unixShmLock, /* xShmLock */ \
40855 unixShmBarrier, /* xShmBarrier */ \
40856 unixShmUnmap, /* xShmUnmap */ \
40857 unixFetch, /* xFetch */ \
40858 unixUnfetch, /* xUnfetch */ \
40859}; \
40860static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \
40861 UNUSED_PARAMETER(z)(void)(z); UNUSED_PARAMETER(p)(void)(p); \
40862 return &METHOD; \
40863} \
40864static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \
40865 = FINDER##Impl;
40866
40867/*
40868** Here are all of the sqlite3_io_methods objects for each of the
40869** locking strategies. Functions that return pointers to these methods
40870** are also created.
40871*/
40872IOMETHODS(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
;
40873 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
;
40874 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
;
40875 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
;
40876 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
;
40877 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
;
40878 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
;
40879 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
;
40880 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
;
40881)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
;
40882IOMETHODS(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
;
40883 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
;
40884 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
;
40885 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
;
40886 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
;
40887 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
;
40888 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
;
40889 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
;
40890 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
;
40891)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
;
40892IOMETHODS(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
;
40893 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
;
40894 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
;
40895 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
;
40896 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
;
40897 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
;
40898 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
;
40899 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
;
40900 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
;
40901)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
;
40902
40903#if SQLITE_ENABLE_LOCKING_STYLE0
40904IOMETHODS(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
;
40905 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
;
40906 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
;
40907 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
;
40908 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
;
40909 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
;
40910 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
;
40911 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
;
40912 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
;
40913)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
;
40914#endif
40915
40916#if OS_VXWORKS0
40917IOMETHODS(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
;
40918 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
;
40919 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
;
40920 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
;
40921 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
;
40922 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
;
40923 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
;
40924 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
;
40925 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
;
40926)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
;
40927#endif
40928
40929#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
40930IOMETHODS(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
;
40931 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
;
40932 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
;
40933 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
;
40934 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
;
40935 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
;
40936 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
;
40937 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
;
40938 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
;
40939)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
;
40940#endif
40941
40942/*
40943** The proxy locking method is a "super-method" in the sense that it
40944** opens secondary file descriptors for the conch and lock files and
40945** it uses proxy, dot-file, AFP, and flock() locking methods on those
40946** secondary files. For this reason, the division that implements
40947** proxy locking is located much further down in the file. But we need
40948** to go ahead and define the sqlite3_io_methods and finder function
40949** for proxy locking here. So we forward declare the I/O methods.
40950*/
40951#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
40952static int proxyClose(sqlite3_file*);
40953static int proxyLock(sqlite3_file*, int);
40954static int proxyUnlock(sqlite3_file*, int);
40955static int proxyCheckReservedLock(sqlite3_file*, int*);
40956IOMETHODS(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
;
40957 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
;
40958 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
;
40959 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
;
40960 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
;
40961 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
;
40962 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
;
40963 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
;
40964 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
;
40965)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
;
40966#endif
40967
40968/* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
40969#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
40970IOMETHODS(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
;
40971 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
;
40972 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
;
40973 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
;
40974 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
;
40975 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
;
40976 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
;
40977 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
;
40978 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
;
40979)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
;
40980#endif
40981
40982#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
40983/*
40984** This "finder" function attempts to determine the best locking strategy
40985** for the database file "filePath". It then returns the sqlite3_io_methods
40986** object that implements that strategy.
40987**
40988** This is for MacOSX only.
40989*/
40990static const sqlite3_io_methods *autolockIoFinderImpl(
40991 const char *filePath, /* name of the database file */
40992 unixFile *pNew /* open file object for the database file */
40993){
40994 static const struct Mapping {
40995 const char *zFilesystem; /* Filesystem type name */
40996 const sqlite3_io_methods *pMethods; /* Appropriate locking method */
40997 } aMap[] = {
40998 { "hfs", &posixIoMethods },
40999 { "ufs", &posixIoMethods },
41000 { "afpfs", &afpIoMethods },
41001 { "smbfs", &afpIoMethods },
41002 { "webdav", &nolockIoMethods },
41003 { 0, 0 }
41004 };
41005 int i;
41006 struct statfs fsInfo;
41007 struct flock lockInfo;
41008
41009 if( !filePath ){
41010 /* If filePath==NULL that means we are dealing with a transient file
41011 ** that does not need to be locked. */
41012 return &nolockIoMethods;
41013 }
41014 if( statfs(filePath, &fsInfo) != -1 ){
41015 if( fsInfo.f_flags & MNT_RDONLY ){
41016 return &nolockIoMethods;
41017 }
41018 for(i=0; aMap[i].zFilesystem; i++){
41019 if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
41020 return aMap[i].pMethods;
41021 }
41022 }
41023 }
41024
41025 /* Default case. Handles, amongst others, "nfs".
41026 ** Test byte-range lock using fcntl(). If the call succeeds,
41027 ** assume that the file-system supports POSIX style locks.
41028 */
41029 lockInfo.l_len = 1;
41030 lockInfo.l_start = 0;
41031 lockInfo.l_whence = SEEK_SET0;
41032 lockInfo.l_type = F_RDLCK0;
41033 if( osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(pNew->h, F_GETLK5, &lockInfo)!=-1 ) {
41034 if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
41035 return &nfsIoMethods;
41036 } else {
41037 return &posixIoMethods;
41038 }
41039 }else{
41040 return &dotlockIoMethods;
41041 }
41042}
41043static const sqlite3_io_methods
41044 *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
41045
41046#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
41047
41048#if OS_VXWORKS0
41049/*
41050** This "finder" function for VxWorks checks to see if posix advisory
41051** locking works. If it does, then that is what is used. If it does not
41052** work, then fallback to named semaphore locking.
41053*/
41054static const sqlite3_io_methods *vxworksIoFinderImpl(
41055 const char *filePath, /* name of the database file */
41056 unixFile *pNew /* the open file object */
41057){
41058 struct flock lockInfo;
41059
41060 if( !filePath ){
41061 /* If filePath==NULL that means we are dealing with a transient file
41062 ** that does not need to be locked. */
41063 return &nolockIoMethods;
41064 }
41065
41066 /* Test if fcntl() is supported and use POSIX style locks.
41067 ** Otherwise fall back to the named semaphore method.
41068 */
41069 lockInfo.l_len = 1;
41070 lockInfo.l_start = 0;
41071 lockInfo.l_whence = SEEK_SET0;
41072 lockInfo.l_type = F_RDLCK0;
41073 if( osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(pNew->h, F_GETLK5, &lockInfo)!=-1 ) {
41074 return &posixIoMethods;
41075 }else{
41076 return &semIoMethods;
41077 }
41078}
41079static const sqlite3_io_methods
41080 *(*const vxworksIoFinder)(const char*,unixFile*) = vxworksIoFinderImpl;
41081
41082#endif /* OS_VXWORKS */
41083
41084/*
41085** An abstract type for a pointer to an IO method finder function:
41086*/
41087typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
41088
41089
41090/****************************************************************************
41091**************************** sqlite3_vfs methods ****************************
41092**
41093** This division contains the implementation of methods on the
41094** sqlite3_vfs object.
41095*/
41096
41097/*
41098** Initialize the contents of the unixFile structure pointed to by pId.
41099*/
41100static int fillInUnixFile(
41101 sqlite3_vfs *pVfs, /* Pointer to vfs object */
41102 int h, /* Open file descriptor of file being opened */
41103 sqlite3_file *pId, /* Write to the unixFile structure here */
41104 const char *zFilename, /* Name of the file being opened */
41105 int ctrlFlags /* Zero or more UNIXFILE_* values */
41106){
41107 const sqlite3_io_methods *pLockingStyle;
41108 unixFile *pNew = (unixFile *)pId;
41109 int rc = SQLITE_OK0;
41110
41111 assert( pNew->pInode==NULL )((void) (0));
41112
41113 /* No locking occurs in temporary files */
41114 assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 )((void) (0));
41115
41116 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
41117 pNew->h = h;
41118 pNew->pVfs = pVfs;
41119 pNew->zPath = zFilename;
41120 pNew->ctrlFlags = (u8)ctrlFlags;
41121#if SQLITE_MAX_MMAP_SIZE20971520>0
41122 pNew->mmapSizeMax = sqlite3GlobalConfigsqlite3Config.szMmap;
41123#endif
41124 if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI0x40) ? zFilename : 0),
41125 "psow", SQLITE_POWERSAFE_OVERWRITE1) ){
41126 pNew->ctrlFlags |= UNIXFILE_PSOW0x10;
41127 }
41128 if( strcmp(pVfs->zName,"unix-excl")==0 ){
41129 pNew->ctrlFlags |= UNIXFILE_EXCL0x01;
41130 }
41131
41132#if OS_VXWORKS0
41133 pNew->pId = vxworksFindFileId(zFilename);
41134 if( pNew->pId==0 ){
41135 ctrlFlags |= UNIXFILE_NOLOCK0x80;
41136 rc = SQLITE_NOMEM_BKPT7;
41137 }
41138#endif
41139
41140 if( ctrlFlags & UNIXFILE_NOLOCK0x80 ){
41141 pLockingStyle = &nolockIoMethods;
41142 }else{
41143 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
41144#if SQLITE_ENABLE_LOCKING_STYLE0
41145 /* Cache zFilename in the locking context (AFP and dotlock override) for
41146 ** proxyLock activation is possible (remote proxy is based on db name)
41147 ** zFilename remains valid until file is closed, to support */
41148 pNew->lockingContext = (void*)zFilename;
41149#endif
41150 }
41151
41152 if( pLockingStyle == &posixIoMethods
41153#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
41154 || pLockingStyle == &nfsIoMethods
41155#endif
41156 ){
41157 unixEnterMutex();
41158 rc = findInodeInfo(pNew, &pNew->pInode);
41159 if( rc!=SQLITE_OK0 ){
41160 /* If an error occurred in findInodeInfo(), close the file descriptor
41161 ** immediately, before releasing the mutex. findInodeInfo() may fail
41162 ** in two scenarios:
41163 **
41164 ** (a) A call to fstat() failed.
41165 ** (b) A malloc failed.
41166 **
41167 ** Scenario (b) may only occur if the process is holding no other
41168 ** file descriptors open on the same file. If there were other file
41169 ** descriptors on this file, then no malloc would be required by
41170 ** findInodeInfo(). If this is the case, it is quite safe to close
41171 ** handle h - as it is guaranteed that no posix locks will be released
41172 ** by doing so.
41173 **
41174 ** If scenario (a) caused the error then things are not so safe. The
41175 ** implicit assumption here is that if fstat() fails, things are in
41176 ** such bad shape that dropping a lock or two doesn't matter much.
41177 */
41178 robust_close(pNew, h, __LINE__41178);
41179 h = -1;
41180 }
41181 unixLeaveMutex();
41182 }
41183
41184#if SQLITE_ENABLE_LOCKING_STYLE0 && defined(__APPLE__)
41185 else if( pLockingStyle == &afpIoMethods ){
41186 /* AFP locking uses the file path so it needs to be included in
41187 ** the afpLockingContext.
41188 */
41189 afpLockingContext *pCtx;
41190 pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
41191 if( pCtx==0 ){
41192 rc = SQLITE_NOMEM_BKPT7;
41193 }else{
41194 /* NB: zFilename exists and remains valid until the file is closed
41195 ** according to requirement F11141. So we do not need to make a
41196 ** copy of the filename. */
41197 pCtx->dbPath = zFilename;
41198 pCtx->reserved = 0;
41199 srandomdev();
41200 unixEnterMutex();
41201 rc = findInodeInfo(pNew, &pNew->pInode);
41202 if( rc!=SQLITE_OK0 ){
41203 sqlite3_free(pNew->lockingContext);
41204 robust_close(pNew, h, __LINE__41204);
41205 h = -1;
41206 }
41207 unixLeaveMutex();
41208 }
41209 }
41210#endif
41211
41212 else if( pLockingStyle == &dotlockIoMethods ){
41213 /* Dotfile locking uses the file path so it needs to be included in
41214 ** the dotlockLockingContext
41215 */
41216 char *zLockFile;
41217 int nFilename;
41218 assert( zFilename!=0 )((void) (0));
41219 nFilename = (int)strlen(zFilename) + 6;
41220 zLockFile = (char *)sqlite3_malloc64(nFilename);
41221 if( zLockFile==0 ){
41222 rc = SQLITE_NOMEM_BKPT7;
41223 }else{
41224 sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX".lock", zFilename);
41225 }
41226 pNew->lockingContext = zLockFile;
41227 }
41228
41229#if OS_VXWORKS0
41230 else if( pLockingStyle == &semIoMethods ){
41231 /* Named semaphore locking uses the file path so it needs to be
41232 ** included in the semLockingContext
41233 */
41234 unixEnterMutex();
41235 rc = findInodeInfo(pNew, &pNew->pInode);
41236 if( (rc==SQLITE_OK0) && (pNew->pInode->pSem==NULL((void*)0)) ){
41237 char *zSemName = pNew->pInode->aSemName;
41238 int n;
41239 sqlite3_snprintf(MAX_PATHNAME512, zSemName, "/%s.sem",
41240 pNew->pId->zCanonicalName);
41241 for( n=1; zSemName[n]; n++ )
41242 if( zSemName[n]=='/' ) zSemName[n] = '_';
41243 pNew->pInode->pSem = sem_open(zSemName, O_CREAT0100, 0666, 1);
41244 if( pNew->pInode->pSem == SEM_FAILED ){
41245 rc = SQLITE_NOMEM_BKPT7;
41246 pNew->pInode->aSemName[0] = '\0';
41247 }
41248 }
41249 unixLeaveMutex();
41250 }
41251#endif
41252
41253 storeLastErrno(pNew, 0);
41254#if OS_VXWORKS0
41255 if( rc!=SQLITE_OK0 ){
41256 if( h>=0 ) robust_close(pNew, h, __LINE__41256);
41257 h = -1;
41258 osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(zFilename);
41259 pNew->ctrlFlags |= UNIXFILE_DELETE0x20;
41260 }
41261#endif
41262 if( rc!=SQLITE_OK0 ){
41263 if( h>=0 ) robust_close(pNew, h, __LINE__41263);
41264 }else{
41265 pId->pMethods = pLockingStyle;
41266 OpenCounter(+1);
41267 verifyDbFile(pNew);
41268 }
41269 return rc;
41270}
41271
41272/*
41273** Directories to consider for temp files.
41274*/
41275static const char *azTempDirs[] = {
41276 0,
41277 0,
41278 "/var/tmp",
41279 "/usr/tmp",
41280 "/tmp",
41281 "."
41282};
41283
41284/*
41285** Initialize first two members of azTempDirs[] array.
41286*/
41287static void unixTempFileInit(void){
41288 azTempDirs[0] = getenv("SQLITE_TMPDIR");
41289 azTempDirs[1] = getenv("TMPDIR");
41290}
41291
41292/*
41293** Return the name of a directory in which to put temporary files.
41294** If no suitable temporary file directory can be found, return NULL.
41295*/
41296static const char *unixTempFileDir(void){
41297 unsigned int i = 0;
41298 struct stat buf;
41299 const char *zDir = sqlite3_temp_directory;
41300
41301 while(1){
41302 if( zDir!=0
41303 && osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)(zDir, &buf)==0
41304 && S_ISDIR(buf.st_mode)((((buf.st_mode)) & 0170000) == (0040000))
41305 && osAccess((int(*)(const char*,int))aSyscall[2].pCurrent)(zDir, 03)==0
41306 ){
41307 return zDir;
41308 }
41309 if( i>=sizeof(azTempDirs)/sizeof(azTempDirs[0]) ) break;
41310 zDir = azTempDirs[i++];
41311 }
41312 return 0;
41313}
41314
41315/*
41316** Create a temporary file name in zBuf. zBuf must be allocated
41317** by the calling process and must be big enough to hold at least
41318** pVfs->mxPathname bytes.
41319*/
41320static int unixGetTempname(int nBuf, char *zBuf){
41321 const char *zDir;
41322 int iLimit = 0;
41323
41324 /* It's odd to simulate an io-error here, but really this is just
41325 ** using the io-error infrastructure to test that SQLite handles this
41326 ** function failing.
41327 */
41328 zBuf[0] = 0;
41329 SimulateIOError( return SQLITE_IOERR );
41330
41331 zDir = unixTempFileDir();
41332 if( zDir==0 ) return SQLITE_IOERR_GETTEMPPATH(10 | (25<<8));
41333 do{
41334 u64 r;
41335 sqlite3_randomness(sizeof(r), &r);
41336 assert( nBuf>2 )((void) (0));
41337 zBuf[nBuf-2] = 0;
41338 sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"etilqs_""%llx%c",
41339 zDir, r, 0);
41340 if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR1;
41341 }while( osAccess((int(*)(const char*,int))aSyscall[2].pCurrent)(zBuf,0)==0 );
41342 return SQLITE_OK0;
41343}
41344
41345#if SQLITE_ENABLE_LOCKING_STYLE0 && defined(__APPLE__)
41346/*
41347** Routine to transform a unixFile into a proxy-locking unixFile.
41348** Implementation in the proxy-lock division, but used by unixOpen()
41349** if SQLITE_PREFER_PROXY_LOCKING is defined.
41350*/
41351static int proxyTransformUnixFile(unixFile*, const char*);
41352#endif
41353
41354/*
41355** Search for an unused file descriptor that was opened on the database
41356** file (not a journal or super-journal file) identified by pathname
41357** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
41358** argument to this function.
41359**
41360** Such a file descriptor may exist if a database connection was closed
41361** but the associated file descriptor could not be closed because some
41362** other file descriptor open on the same file is holding a file-lock.
41363** Refer to comments in the unixClose() function and the lengthy comment
41364** describing "Posix Advisory Locking" at the start of this file for
41365** further details. Also, ticket #4018.
41366**
41367** If a suitable file descriptor is found, then it is returned. If no
41368** such file descriptor is located, -1 is returned.
41369*/
41370static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
41371 UnixUnusedFd *pUnused = 0;
41372
41373 /* Do not search for an unused file descriptor on vxworks. Not because
41374 ** vxworks would not benefit from the change (it might, we're not sure),
41375 ** but because no way to test it is currently available. It is better
41376 ** not to risk breaking vxworks support for the sake of such an obscure
41377 ** feature. */
41378#if !OS_VXWORKS0
41379 struct stat sStat; /* Results of stat() call */
41380
41381 unixEnterMutex();
41382
41383 /* A stat() call may fail for various reasons. If this happens, it is
41384 ** almost certain that an open() call on the same path will also fail.
41385 ** For this reason, if an error occurs in the stat() call here, it is
41386 ** ignored and -1 is returned. The caller will try to open a new file
41387 ** descriptor on the same path, fail, and return an error to SQLite.
41388 **
41389 ** Even if a subsequent open() call does succeed, the consequences of
41390 ** not searching for a reusable file descriptor are not dire. */
41391 if( inodeList!=0 && 0==osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)(zPath, &sStat) ){
41392 unixInodeInfo *pInode;
41393
41394 pInode = inodeList;
41395 while( pInode && (pInode->fileId.dev!=sStat.st_dev
41396 || pInode->fileId.ino!=(u64)sStat.st_ino) ){
41397 pInode = pInode->pNext;
41398 }
41399 if( pInode ){
41400 UnixUnusedFd **pp;
41401 assert( sqlite3_mutex_notheld(pInode->pLockMutex) )((void) (0));
41402 sqlite3_mutex_enter(pInode->pLockMutex);
41403 flags &= (SQLITE_OPEN_READONLY0x00000001|SQLITE_OPEN_READWRITE0x00000002);
41404 for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
41405 pUnused = *pp;
41406 if( pUnused ){
41407 *pp = pUnused->pNext;
41408 }
41409 sqlite3_mutex_leave(pInode->pLockMutex);
41410 }
41411 }
41412 unixLeaveMutex();
41413#endif /* if !OS_VXWORKS */
41414 return pUnused;
41415}
41416
41417/*
41418** Find the mode, uid and gid of file zFile.
41419*/
41420static int getFileMode(
41421 const char *zFile, /* File name */
41422 mode_t *pMode, /* OUT: Permissions of zFile */
41423 uid_t *pUid, /* OUT: uid of zFile. */
41424 gid_t *pGid /* OUT: gid of zFile. */
41425){
41426 struct stat sStat; /* Output of stat() on database file */
41427 int rc = SQLITE_OK0;
41428 if( 0==osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)(zFile, &sStat) ){
41429 *pMode = sStat.st_mode & 0777;
41430 *pUid = sStat.st_uid;
41431 *pGid = sStat.st_gid;
41432 }else{
41433 rc = SQLITE_IOERR_FSTAT(10 | (7<<8));
41434 }
41435 return rc;
41436}
41437
41438/*
41439** This function is called by unixOpen() to determine the unix permissions
41440** to create new files with. If no error occurs, then SQLITE_OK is returned
41441** and a value suitable for passing as the third argument to open(2) is
41442** written to *pMode. If an IO error occurs, an SQLite error code is
41443** returned and the value of *pMode is not modified.
41444**
41445** In most cases, this routine sets *pMode to 0, which will become
41446** an indication to robust_open() to create the file using
41447** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask.
41448** But if the file being opened is a WAL or regular journal file, then
41449** this function queries the file-system for the permissions on the
41450** corresponding database file and sets *pMode to this value. Whenever
41451** possible, WAL and journal files are created using the same permissions
41452** as the associated database file.
41453**
41454** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the
41455** original filename is unavailable. But 8_3_NAMES is only used for
41456** FAT filesystems and permissions do not matter there, so just use
41457** the default permissions. In 8_3_NAMES mode, leave *pMode set to zero.
41458*/
41459static int findCreateFileMode(
41460 const char *zPath, /* Path of file (possibly) being created */
41461 int flags, /* Flags passed as 4th argument to xOpen() */
41462 mode_t *pMode, /* OUT: Permissions to open file with */
41463 uid_t *pUid, /* OUT: uid to set on the file */
41464 gid_t *pGid /* OUT: gid to set on the file */
41465){
41466 int rc = SQLITE_OK0; /* Return Code */
41467 *pMode = 0;
41468 *pUid = 0;
41469 *pGid = 0;
41470 if( flags & (SQLITE_OPEN_WAL0x00080000|SQLITE_OPEN_MAIN_JOURNAL0x00000800) ){
41471 char zDb[MAX_PATHNAME512+1]; /* Database file path */
41472 int nDb; /* Number of valid bytes in zDb */
41473
41474 /* zPath is a path to a WAL or journal file. The following block derives
41475 ** the path to the associated database file from zPath. This block handles
41476 ** the following naming conventions:
41477 **
41478 ** "<path to db>-journal"
41479 ** "<path to db>-wal"
41480 ** "<path to db>-journalNN"
41481 ** "<path to db>-walNN"
41482 **
41483 ** where NN is a decimal number. The NN naming schemes are
41484 ** used by the test_multiplex.c module.
41485 **
41486 ** In normal operation, the journal file name will always contain
41487 ** a '-' character. However in 8+3 filename mode, or if a corrupt
41488 ** rollback journal specifies a super-journal with a goofy name, then
41489 ** the '-' might be missing or the '-' might be the first character in
41490 ** the filename. In that case, just return SQLITE_OK with *pMode==0.
41491 */
41492 nDb = sqlite3Strlen30(zPath) - 1;
41493 while( nDb>0 && zPath[nDb]!='.' ){
41494 if( zPath[nDb]=='-' ){
41495 memcpy(zDb, zPath, nDb);
41496 zDb[nDb] = '\0';
41497 rc = getFileMode(zDb, pMode, pUid, pGid);
41498 break;
41499 }
41500 nDb--;
41501 }
41502 }else if( flags & SQLITE_OPEN_DELETEONCLOSE0x00000008 ){
41503 *pMode = 0600;
41504 }else if( flags & SQLITE_OPEN_URI0x00000040 ){
41505 /* If this is a main database file and the file was opened using a URI
41506 ** filename, check for the "modeof" parameter. If present, interpret
41507 ** its value as a filename and try to copy the mode, uid and gid from
41508 ** that file. */
41509 const char *z = sqlite3_uri_parameter(zPath, "modeof");
41510 if( z ){
41511 rc = getFileMode(z, pMode, pUid, pGid);
41512 }
41513 }
41514 return rc;
41515}
41516
41517/*
41518** Open the file zPath.
41519**
41520** Previously, the SQLite OS layer used three functions in place of this
41521** one:
41522**
41523** sqlite3OsOpenReadWrite();
41524** sqlite3OsOpenReadOnly();
41525** sqlite3OsOpenExclusive();
41526**
41527** These calls correspond to the following combinations of flags:
41528**
41529** ReadWrite() -> (READWRITE | CREATE)
41530** ReadOnly() -> (READONLY)
41531** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
41532**
41533** The old OpenExclusive() accepted a boolean argument - "delFlag". If
41534** true, the file was configured to be automatically deleted when the
41535** file handle closed. To achieve the same effect using this new
41536** interface, add the DELETEONCLOSE flag to those specified above for
41537** OpenExclusive().
41538*/
41539static int unixOpen(
41540 sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */
41541 const char *zPath, /* Pathname of file to be opened */
41542 sqlite3_file *pFile, /* The file descriptor to be filled in */
41543 int flags, /* Input flags to control the opening */
41544 int *pOutFlags /* Output flags returned to SQLite core */
41545){
41546 unixFile *p = (unixFile *)pFile;
41547 int fd = -1; /* File descriptor returned by open() */
41548 int openFlags = 0; /* Flags to pass to open() */
41549 int eType = flags&0x0FFF00; /* Type of file to open */
41550 int noLock; /* True to omit locking primitives */
41551 int rc = SQLITE_OK0; /* Function Return Code */
41552 int ctrlFlags = 0; /* UNIXFILE_* flags */
41553
41554 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE0x00000010);
41555 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE0x00000008);
41556 int isCreate = (flags & SQLITE_OPEN_CREATE0x00000004);
41557 int isReadonly = (flags & SQLITE_OPEN_READONLY0x00000001);
41558 int isReadWrite = (flags & SQLITE_OPEN_READWRITE0x00000002);
41559#if SQLITE_ENABLE_LOCKING_STYLE0
41560 int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY0x00000020);
41561#endif
41562#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE0
41563 struct statfs fsInfo;
41564#endif
41565
41566 /* If creating a super- or main-file journal, this function will open
41567 ** a file-descriptor on the directory too. The first time unixSync()
41568 ** is called the directory file descriptor will be fsync()ed and close()d.
41569 */
41570 int isNewJrnl = (isCreate && (
41571 eType==SQLITE_OPEN_SUPER_JOURNAL0x00004000
41572 || eType==SQLITE_OPEN_MAIN_JOURNAL0x00000800
41573 || eType==SQLITE_OPEN_WAL0x00080000
41574 ));
41575
41576 /* If argument zPath is a NULL pointer, this function is required to open
41577 ** a temporary file. Use this buffer to store the file name in.
41578 */
41579 char zTmpname[MAX_PATHNAME512+2];
41580 const char *zName = zPath;
41581
41582 /* Check the following statements are true:
41583 **
41584 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
41585 ** (b) if CREATE is set, then READWRITE must also be set, and
41586 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
41587 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
41588 */
41589 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly))((void) (0));
41590 assert(isCreate==0 || isReadWrite)((void) (0));
41591 assert(isExclusive==0 || isCreate)((void) (0));
41592 assert(isDelete==0 || isCreate)((void) (0));
41593
41594 /* The main DB, main journal, WAL file and super-journal are never
41595 ** automatically deleted. Nor are they ever temporary files. */
41596 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB )((void) (0));
41597 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL )((void) (0));
41598 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_SUPER_JOURNAL )((void) (0));
41599 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL )((void) (0));
41600
41601 /* Assert that the upper layer has set one of the "file-type" flags. */
41602 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB((void) (0))
41603 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL((void) (0))
41604 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_SUPER_JOURNAL((void) (0))
41605 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL((void) (0))
41606 )((void) (0));
41607
41608 /* Detect a pid change and reset the PRNG. There is a race condition
41609 ** here such that two or more threads all trying to open databases at
41610 ** the same instant might all reset the PRNG. But multiple resets
41611 ** are harmless.
41612 */
41613 if( randomnessPid!=osGetpid(0)(pid_t)getpid() ){
41614 randomnessPid = osGetpid(0)(pid_t)getpid();
41615 sqlite3_randomness(0,0);
41616 }
41617 memset(p, 0, sizeof(unixFile));
41618
41619#ifdef SQLITE_ASSERT_NO_FILES
41620 /* Applications that never read or write a persistent disk files */
41621 assert( zName==0 )((void) (0));
41622#endif
41623
41624 if( eType==SQLITE_OPEN_MAIN_DB0x00000100 ){
41625 UnixUnusedFd *pUnused;
41626 pUnused = findReusableFd(zName, flags);
41627 if( pUnused ){
41628 fd = pUnused->fd;
41629 }else{
41630 pUnused = sqlite3_malloc64(sizeof(*pUnused));
41631 if( !pUnused ){
41632 return SQLITE_NOMEM_BKPT7;
41633 }
41634 }
41635 p->pPreallocatedUnused = pUnused;
41636
41637 /* Database filenames are double-zero terminated if they are not
41638 ** URIs with parameters. Hence, they can always be passed into
41639 ** sqlite3_uri_parameter(). */
41640 assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 )((void) (0));
41641
41642 }else if( !zName ){
41643 /* If zName is NULL, the upper layer is requesting a temp file. */
41644 assert(isDelete && !isNewJrnl)((void) (0));
41645 rc = unixGetTempname(pVfs->mxPathname, zTmpname);
41646 if( rc!=SQLITE_OK0 ){
41647 return rc;
41648 }
41649 zName = zTmpname;
41650
41651 /* Generated temporary filenames are always double-zero terminated
41652 ** for use by sqlite3_uri_parameter(). */
41653 assert( zName[strlen(zName)+1]==0 )((void) (0));
41654 }
41655
41656 /* Determine the value of the flags parameter passed to POSIX function
41657 ** open(). These must be calculated even if open() is not called, as
41658 ** they may be stored as part of the file handle and used by the
41659 ** 'conch file' locking functions later on. */
41660 if( isReadonly ) openFlags |= O_RDONLY00;
41661 if( isReadWrite ) openFlags |= O_RDWR02;
41662 if( isCreate ) openFlags |= O_CREAT0100;
41663 if( isExclusive ) openFlags |= (O_EXCL0200|O_NOFOLLOW0400000);
41664 openFlags |= (O_LARGEFILE0|O_BINARY0|O_NOFOLLOW0400000);
41665
41666 if( fd<0 ){
41667 mode_t openMode; /* Permissions to create file with */
41668 uid_t uid; /* Userid for the file */
41669 gid_t gid; /* Groupid for the file */
41670 rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid);
41671 if( rc!=SQLITE_OK0 ){
41672 assert( !p->pPreallocatedUnused )((void) (0));
41673 assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL )((void) (0));
41674 return rc;
41675 }
41676 fd = robust_open(zName, openFlags, openMode);
41677 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
41678 assert( !isExclusive || (openFlags & O_CREAT)!=0 )((void) (0));
41679 if( fd<0 ){
41680 if( isNewJrnl && errno(*__errno_location ())==EACCES13 && osAccess((int(*)(const char*,int))aSyscall[2].pCurrent)(zName, F_OK0) ){
41681 /* If unable to create a journal because the directory is not
41682 ** writable, change the error code to indicate that. */
41683 rc = SQLITE_READONLY_DIRECTORY(8 | (6<<8));
41684 }else if( errno(*__errno_location ())!=EISDIR21 && isReadWrite ){
41685 /* Failed to open the file for read/write access. Try read-only. */
41686 flags &= ~(SQLITE_OPEN_READWRITE0x00000002|SQLITE_OPEN_CREATE0x00000004);
41687 openFlags &= ~(O_RDWR02|O_CREAT0100);
41688 flags |= SQLITE_OPEN_READONLY0x00000001;
41689 openFlags |= O_RDONLY00;
41690 isReadonly = 1;
41691 fd = robust_open(zName, openFlags, openMode);
41692 }
41693 }
41694 if( fd<0 ){
41695 int rc2 = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName)unixLogErrorAtLine(sqlite3CantopenError(41695),"open",zName,41695
)
;
41696 if( rc==SQLITE_OK0 ) rc = rc2;
41697 goto open_finished;
41698 }
41699
41700 /* The owner of the rollback journal or WAL file should always be the
41701 ** same as the owner of the database file. Try to ensure that this is
41702 ** the case. The chown() system call will be a no-op if the current
41703 ** process lacks root privileges, be we should at least try. Without
41704 ** this step, if a root process opens a database file, it can leave
41705 ** behinds a journal/WAL that is owned by root and hence make the
41706 ** database inaccessible to unprivileged processes.
41707 **
41708 ** If openMode==0, then that means uid and gid are not set correctly
41709 ** (probably because SQLite is configured to use 8+3 filename mode) and
41710 ** in that case we do not want to attempt the chown().
41711 */
41712 if( openMode && (flags & (SQLITE_OPEN_WAL0x00080000|SQLITE_OPEN_MAIN_JOURNAL0x00000800))!=0 ){
41713 robustFchown(fd, uid, gid);
41714 }
41715 }
41716 assert( fd>=0 )((void) (0));
41717 if( pOutFlags ){
41718 *pOutFlags = flags;
41719 }
41720
41721 if( p->pPreallocatedUnused ){
41722 p->pPreallocatedUnused->fd = fd;
41723 p->pPreallocatedUnused->flags =
41724 flags & (SQLITE_OPEN_READONLY0x00000001|SQLITE_OPEN_READWRITE0x00000002);
41725 }
41726
41727 if( isDelete ){
41728#if OS_VXWORKS0
41729 zPath = zName;
41730#elif defined(SQLITE_UNLINK_AFTER_CLOSE)
41731 zPath = sqlite3_mprintf("%s", zName);
41732 if( zPath==0 ){
41733 robust_close(p, fd, __LINE__41733);
41734 return SQLITE_NOMEM_BKPT7;
41735 }
41736#else
41737 osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(zName);
41738#endif
41739 }
41740#if SQLITE_ENABLE_LOCKING_STYLE0
41741 else{
41742 p->openFlags = openFlags;
41743 }
41744#endif
41745
41746#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE0
41747 if( fstatfs(fd, &fsInfo) == -1 ){
41748 storeLastErrno(p, errno(*__errno_location ()));
41749 robust_close(p, fd, __LINE__41749);
41750 return SQLITE_IOERR_ACCESS(10 | (13<<8));
41751 }
41752 if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
41753 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS0x1;
41754 }
41755 if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) {
41756 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS0x1;
41757 }
41758#endif
41759
41760 /* Set up appropriate ctrlFlags */
41761 if( isDelete ) ctrlFlags |= UNIXFILE_DELETE0x20;
41762 if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY0x02;
41763 noLock = eType!=SQLITE_OPEN_MAIN_DB0x00000100;
41764 if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK0x80;
41765 if( isNewJrnl ) ctrlFlags |= UNIXFILE_DIRSYNC0x08;
41766 if( flags & SQLITE_OPEN_URI0x00000040 ) ctrlFlags |= UNIXFILE_URI0x40;
41767
41768#if SQLITE_ENABLE_LOCKING_STYLE0
41769#if SQLITE_PREFER_PROXY_LOCKING
41770 isAutoProxy = 1;
41771#endif
41772 if( isAutoProxy && (zPath!=NULL((void*)0)) && (!noLock) && pVfs->xOpen ){
41773 char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
41774 int useProxy = 0;
41775
41776 /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
41777 ** never use proxy, NULL means use proxy for non-local files only. */
41778 if( envforce!=NULL((void*)0) ){
41779 useProxy = atoi(envforce)>0;
41780 }else{
41781 useProxy = !(fsInfo.f_flags&MNT_LOCAL);
41782 }
41783 if( useProxy ){
41784 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
41785 if( rc==SQLITE_OK0 ){
41786 rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
41787 if( rc!=SQLITE_OK0 ){
41788 /* Use unixClose to clean up the resources added in fillInUnixFile
41789 ** and clear all the structure's references. Specifically,
41790 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
41791 */
41792 unixClose(pFile);
41793 return rc;
41794 }
41795 }
41796 goto open_finished;
41797 }
41798 }
41799#endif
41800
41801 assert( zPath==0 || zPath[0]=='/'((void) (0))
41802 || eType==SQLITE_OPEN_SUPER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL((void) (0))
41803 )((void) (0));
41804 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
41805
41806open_finished:
41807 if( rc!=SQLITE_OK0 ){
41808 sqlite3_free(p->pPreallocatedUnused);
41809 }
41810 return rc;
41811}
41812
41813
41814/*
41815** Delete the file at zPath. If the dirSync argument is true, fsync()
41816** the directory after deleting the file.
41817*/
41818static int unixDelete(
41819 sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */
41820 const char *zPath, /* Name of file to be deleted */
41821 int dirSync /* If true, fsync() directory after deleting file */
41822){
41823 int rc = SQLITE_OK0;
41824 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
41825 SimulateIOError(return SQLITE_IOERR_DELETE);
41826 if( osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(zPath)==(-1) ){
41827 if( errno(*__errno_location ())==ENOENT2
41828#if OS_VXWORKS0
41829 || osAccess((int(*)(const char*,int))aSyscall[2].pCurrent)(zPath,0)!=0
41830#endif
41831 ){
41832 rc = SQLITE_IOERR_DELETE_NOENT(10 | (23<<8));
41833 }else{
41834 rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath)unixLogErrorAtLine((10 | (10<<8)),"unlink",zPath,41834);
41835 }
41836 return rc;
41837 }
41838#ifndef SQLITE_DISABLE_DIRSYNC
41839 if( (dirSync & 1)!=0 ){
41840 int fd;
41841 rc = osOpenDirectory((int(*)(const char*,int*))aSyscall[17].pCurrent)(zPath, &fd);
41842 if( rc==SQLITE_OK0 ){
41843 if( full_fsync(fd,0,0) ){
41844 rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath)unixLogErrorAtLine((10 | (5<<8)),"fsync",zPath,41844);
41845 }
41846 robust_close(0, fd, __LINE__41846);
41847 }else{
41848 assert( rc==SQLITE_CANTOPEN )((void) (0));
41849 rc = SQLITE_OK0;
41850 }
41851 }
41852#endif
41853 return rc;
41854}
41855
41856/*
41857** Test the existence of or access permissions of file zPath. The
41858** test performed depends on the value of flags:
41859**
41860** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
41861** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
41862** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
41863**
41864** Otherwise return 0.
41865*/
41866static int unixAccess(
41867 sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */
41868 const char *zPath, /* Path of the file to examine */
41869 int flags, /* What do we want to learn about the zPath file? */
41870 int *pResOut /* Write result boolean here */
41871){
41872 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
41873 SimulateIOError( return SQLITE_IOERR_ACCESS; );
41874 assert( pResOut!=0 )((void) (0));
41875
41876 /* The spec says there are three possible values for flags. But only
41877 ** two of them are actually used */
41878 assert( flags==SQLITE_ACCESS_EXISTS || flags==SQLITE_ACCESS_READWRITE )((void) (0));
41879
41880 if( flags==SQLITE_ACCESS_EXISTS0 ){
41881 struct stat buf;
41882 *pResOut = 0==osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)(zPath, &buf) &&
41883 (!S_ISREG(buf.st_mode)((((buf.st_mode)) & 0170000) == (0100000)) || buf.st_size>0);
41884 }else{
41885 *pResOut = osAccess((int(*)(const char*,int))aSyscall[2].pCurrent)(zPath, W_OK2|R_OK4)==0;
41886 }
41887 return SQLITE_OK0;
41888}
41889
41890/*
41891** A pathname under construction
41892*/
41893typedef struct DbPath DbPath;
41894struct DbPath {
41895 int rc; /* Non-zero following any error */
41896 int nSymlink; /* Number of symlinks resolved */
41897 char *zOut; /* Write the pathname here */
41898 int nOut; /* Bytes of space available to zOut[] */
41899 int nUsed; /* Bytes of zOut[] currently being used */
41900};
41901
41902/* Forward reference */
41903static void appendAllPathElements(DbPath*,const char*);
41904
41905/*
41906** Append a single path element to the DbPath under construction
41907*/
41908static void appendOnePathElement(
41909 DbPath *pPath, /* Path under construction, to which to append zName */
41910 const char *zName, /* Name to append to pPath. Not zero-terminated */
41911 int nName /* Number of significant bytes in zName */
41912){
41913 assert( nName>0 )((void) (0));
41914 assert( zName!=0 )((void) (0));
41915 if( zName[0]=='.' ){
41916 if( nName==1 ) return;
41917 if( zName[1]=='.' && nName==2 ){
41918 if( pPath->nUsed<=1 ){
41919 pPath->rc = SQLITE_ERROR1;
41920 return;
41921 }
41922 assert( pPath->zOut[0]=='/' )((void) (0));
41923 while( pPath->zOut[--pPath->nUsed]!='/' ){}
41924 return;
41925 }
41926 }
41927 if( pPath->nUsed + nName + 2 >= pPath->nOut ){
41928 pPath->rc = SQLITE_ERROR1;
41929 return;
41930 }
41931 pPath->zOut[pPath->nUsed++] = '/';
41932 memcpy(&pPath->zOut[pPath->nUsed], zName, nName);
41933 pPath->nUsed += nName;
41934#if defined(HAVE_READLINK1) && defined(HAVE_LSTAT1)
41935 if( pPath->rc==SQLITE_OK0 ){
41936 const char *zIn;
41937 struct stat buf;
41938 pPath->zOut[pPath->nUsed] = 0;
41939 zIn = pPath->zOut;
41940 if( osLstat((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)(zIn, &buf)!=0 ){
41941 if( errno(*__errno_location ())!=ENOENT2 ){
41942 pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn)unixLogErrorAtLine(sqlite3CantopenError(41942),"lstat",zIn,41942
)
;
41943 }
41944 }else if( S_ISLNK(buf.st_mode)((((buf.st_mode)) & 0170000) == (0120000)) ){
41945 ssize_t got;
41946 char zLnk[SQLITE_MAX_PATHLEN4096+2];
41947 if( pPath->nSymlink++ > SQLITE_MAX_SYMLINK200 ){
41948 pPath->rc = SQLITE_CANTOPEN_BKPTsqlite3CantopenError(41948);
41949 return;
41950 }
41951 got = osReadlink((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent)(zIn, zLnk, sizeof(zLnk)-2);
41952 if( got<=0 || got>=(ssize_t)sizeof(zLnk)-2 ){
41953 pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn)unixLogErrorAtLine(sqlite3CantopenError(41953),"readlink",zIn
,41953)
;
41954 return;
41955 }
41956 zLnk[got] = 0;
41957 if( zLnk[0]=='/' ){
41958 pPath->nUsed = 0;
41959 }else{
41960 pPath->nUsed -= nName + 1;
41961 }
41962 appendAllPathElements(pPath, zLnk);
41963 }
41964 }
41965#endif
41966}
41967
41968/*
41969** Append all path elements in zPath to the DbPath under construction.
41970*/
41971static void appendAllPathElements(
41972 DbPath *pPath, /* Path under construction, to which to append zName */
41973 const char *zPath /* Path to append to pPath. Is zero-terminated */
41974){
41975 int i = 0;
41976 int j = 0;
41977 do{
41978 while( zPath[i] && zPath[i]!='/' ){ i++; }
41979 if( i>j ){
41980 appendOnePathElement(pPath, &zPath[j], i-j);
41981 }
41982 j = i+1;
41983 }while( zPath[i++] );
41984}
41985
41986/*
41987** Turn a relative pathname into a full pathname. The relative path
41988** is stored as a nul-terminated string in the buffer pointed to by
41989** zPath.
41990**
41991** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
41992** (in this case, MAX_PATHNAME bytes). The full-path is written to
41993** this buffer before returning.
41994*/
41995static int unixFullPathname(
41996 sqlite3_vfs *pVfs, /* Pointer to vfs object */
41997 const char *zPath, /* Possibly relative input path */
41998 int nOut, /* Size of output buffer in bytes */
41999 char *zOut /* Output buffer */
42000){
42001 DbPath path;
42002 UNUSED_PARAMETER(pVfs)(void)(pVfs);
42003 path.rc = 0;
42004 path.nUsed = 0;
42005 path.nSymlink = 0;
42006 path.nOut = nOut;
42007 path.zOut = zOut;
42008 if( zPath[0]!='/' ){
42009 char zPwd[SQLITE_MAX_PATHLEN4096+2];
42010 if( osGetcwd((char*(*)(char*,size_t))aSyscall[3].pCurrent)(zPwd, sizeof(zPwd)-2)==0 ){
42011 return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath)unixLogErrorAtLine(sqlite3CantopenError(42011),"getcwd",zPath
,42011)
;
42012 }
42013 appendAllPathElements(&path, zPwd);
42014 }
42015 appendAllPathElements(&path, zPath);
42016 zOut[path.nUsed] = 0;
42017 if( path.rc || path.nUsed<2 ) return SQLITE_CANTOPEN_BKPTsqlite3CantopenError(42017);
42018 if( path.nSymlink ) return SQLITE_OK_SYMLINK(0 | (2<<8));
42019 return SQLITE_OK0;
42020}
42021
42022#ifndef SQLITE_OMIT_LOAD_EXTENSION1
42023/*
42024** Interfaces for opening a shared library, finding entry points
42025** within the shared library, and closing the shared library.
42026*/
42027#include <dlfcn.h>
42028static void *unixDlOpen0(sqlite3_vfs *NotUsed, const char *zFilename){
42029 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
42030 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
42031}
42032
42033/*
42034** SQLite calls this function immediately after a call to unixDlSym() or
42035** unixDlOpen() fails (returns a null pointer). If a more detailed error
42036** message is available, it is written to zBufOut. If no error message
42037** is available, zBufOut is left unmodified and SQLite uses a default
42038** error message.
42039*/
42040static void unixDlError0(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
42041 const char *zErr;
42042 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
42043 unixEnterMutex();
42044 zErr = dlerror();
42045 if( zErr ){
42046 sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
42047 }
42048 unixLeaveMutex();
42049}
42050static void (*unixDlSym0(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
42051 /*
42052 ** GCC with -pedantic-errors says that C90 does not allow a void* to be
42053 ** cast into a pointer to a function. And yet the library dlsym() routine
42054 ** returns a void* which is really a pointer to a function. So how do we
42055 ** use dlsym() with -pedantic-errors?
42056 **
42057 ** Variable x below is defined to be a pointer to a function taking
42058 ** parameters void* and const char* and returning a pointer to a function.
42059 ** We initialize x by assigning it a pointer to the dlsym() function.
42060 ** (That assignment requires a cast.) Then we call the function that
42061 ** x points to.
42062 **
42063 ** This work-around is unlikely to work correctly on any system where
42064 ** you really cannot cast a function pointer into void*. But then, on the
42065 ** other hand, dlsym() will not work on such a system either, so we have
42066 ** not really lost anything.
42067 */
42068 void (*(*x)(void*,const char*))(void);
42069 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
42070 x = (void(*(*)(void*,const char*))(void))dlsym;
42071 return (*x)(p, zSym);
42072}
42073static void unixDlClose0(sqlite3_vfs *NotUsed, void *pHandle){
42074 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
42075 dlclose(pHandle);
42076}
42077#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
42078 #define unixDlOpen0 0
42079 #define unixDlError0 0
42080 #define unixDlSym0 0
42081 #define unixDlClose0 0
42082#endif
42083
42084/*
42085** Write nBuf bytes of random data to the supplied buffer zBuf.
42086*/
42087static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
42088 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
42089 assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)))((void) (0));
42090
42091 /* We have to initialize zBuf to prevent valgrind from reporting
42092 ** errors. The reports issued by valgrind are incorrect - we would
42093 ** prefer that the randomness be increased by making use of the
42094 ** uninitialized space in zBuf - but valgrind errors tend to worry
42095 ** some users. Rather than argue, it seems easier just to initialize
42096 ** the whole array and silence valgrind, even if that means less randomness
42097 ** in the random seed.
42098 **
42099 ** When testing, initializing zBuf[] to zero is all we do. That means
42100 ** that we always use the same random number sequence. This makes the
42101 ** tests repeatable.
42102 */
42103 memset(zBuf, 0, nBuf);
42104 randomnessPid = osGetpid(0)(pid_t)getpid();
42105#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
42106 {
42107 int fd, got;
42108 fd = robust_open("/dev/urandom", O_RDONLY00, 0);
42109 if( fd<0 ){
42110 time_t t;
42111 time(&t);
42112 memcpy(zBuf, &t, sizeof(t));
42113 memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid));
42114 assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf )((void) (0));
42115 nBuf = sizeof(t) + sizeof(randomnessPid);
42116 }else{
42117 do{ got = osRead((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)(fd, zBuf, nBuf); }while( got<0 && errno(*__errno_location ())==EINTR4 );
42118 robust_close(0, fd, __LINE__42118);
42119 }
42120 }
42121#endif
42122 return nBuf;
42123}
42124
42125
42126/*
42127** Sleep for a little while. Return the amount of time slept.
42128** The argument is the number of microseconds we want to sleep.
42129** The return value is the number of microseconds of sleep actually
42130** requested from the underlying operating system, a number which
42131** might be greater than or equal to the argument, but not less
42132** than the argument.
42133*/
42134static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
42135#if OS_VXWORKS0
42136 struct timespec sp;
42137
42138 sp.tv_sec = microseconds / 1000000;
42139 sp.tv_nsec = (microseconds % 1000000) * 1000;
42140 nanosleep(&sp, NULL((void*)0));
42141 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
42142 return microseconds;
42143#elif defined(HAVE_USLEEP1) && HAVE_USLEEP1
42144 if( microseconds>=1000000 ) sleep(microseconds/1000000);
42145 if( microseconds%1000000 ) usleep(microseconds%1000000);
42146 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
42147 return microseconds;
42148#else
42149 int seconds = (microseconds+999999)/1000000;
42150 sleep(seconds);
42151 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
42152 return seconds*1000000;
42153#endif
42154}
42155
42156/*
42157** The following variable, if set to a non-zero value, is interpreted as
42158** the number of seconds since 1970 and is used to set the result of
42159** sqlite3OsCurrentTime() during testing.
42160*/
42161#ifdef SQLITE_TEST
42162SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
42163#endif
42164
42165/*
42166** Find the current time (in Universal Coordinated Time). Write into *piNow
42167** the current time and date as a Julian Day number times 86_400_000. In
42168** other words, write into *piNow the number of milliseconds since the Julian
42169** epoch of noon in Greenwich on November 24, 4714 B.C according to the
42170** proleptic Gregorian calendar.
42171**
42172** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
42173** cannot be found.
42174*/
42175static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
42176 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
42177 int rc = SQLITE_OK0;
42178#if defined(NO_GETTOD)
42179 time_t t;
42180 time(&t);
42181 *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
42182#elif OS_VXWORKS0
42183 struct timespec sNow;
42184 clock_gettime(CLOCK_REALTIME0, &sNow);
42185 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
42186#else
42187 struct timeval sNow;
42188 (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */
42189 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
42190#endif
42191
42192#ifdef SQLITE_TEST
42193 if( sqlite3_current_time ){
42194 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
42195 }
42196#endif
42197 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
42198 return rc;
42199}
42200
42201#ifndef SQLITE_OMIT_DEPRECATED1
42202/*
42203** Find the current time (in Universal Coordinated Time). Write the
42204** current time and date as a Julian Day number into *prNow and
42205** return 0. Return 1 if the time and date cannot be found.
42206*/
42207static int unixCurrentTime0(sqlite3_vfs *NotUsed, double *prNow){
42208 sqlite3_int64 i = 0;
42209 int rc;
42210 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
42211 rc = unixCurrentTimeInt64(0, &i);
42212 *prNow = i/86400000.0;
42213 return rc;
42214}
42215#else
42216# define unixCurrentTime0 0
42217#endif
42218
42219/*
42220** The xGetLastError() method is designed to return a better
42221** low-level error message when operating-system problems come up
42222** during SQLite operation. Only the integer return code is currently
42223** used.
42224*/
42225static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
42226 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
42227 UNUSED_PARAMETER(NotUsed2)(void)(NotUsed2);
42228 UNUSED_PARAMETER(NotUsed3)(void)(NotUsed3);
42229 return errno(*__errno_location ());
42230}
42231
42232
42233/*
42234************************ End of sqlite3_vfs methods ***************************
42235******************************************************************************/
42236
42237/******************************************************************************
42238************************** Begin Proxy Locking ********************************
42239**
42240** Proxy locking is a "uber-locking-method" in this sense: It uses the
42241** other locking methods on secondary lock files. Proxy locking is a
42242** meta-layer over top of the primitive locking implemented above. For
42243** this reason, the division that implements of proxy locking is deferred
42244** until late in the file (here) after all of the other I/O methods have
42245** been defined - so that the primitive locking methods are available
42246** as services to help with the implementation of proxy locking.
42247**
42248****
42249**
42250** The default locking schemes in SQLite use byte-range locks on the
42251** database file to coordinate safe, concurrent access by multiple readers
42252** and writers [http://sqlite.org/lockingv3.html]. The five file locking
42253** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
42254** as POSIX read & write locks over fixed set of locations (via fsctl),
42255** on AFP and SMB only exclusive byte-range locks are available via fsctl
42256** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
42257** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
42258** address in the shared range is taken for a SHARED lock, the entire
42259** shared range is taken for an EXCLUSIVE lock):
42260**
42261** PENDING_BYTE 0x40000000
42262** RESERVED_BYTE 0x40000001
42263** SHARED_RANGE 0x40000002 -> 0x40000200
42264**
42265** This works well on the local file system, but shows a nearly 100x
42266** slowdown in read performance on AFP because the AFP client disables
42267** the read cache when byte-range locks are present. Enabling the read
42268** cache exposes a cache coherency problem that is present on all OS X
42269** supported network file systems. NFS and AFP both observe the
42270** close-to-open semantics for ensuring cache coherency
42271** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
42272** address the requirements for concurrent database access by multiple
42273** readers and writers
42274** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
42275**
42276** To address the performance and cache coherency issues, proxy file locking
42277** changes the way database access is controlled by limiting access to a
42278** single host at a time and moving file locks off of the database file
42279** and onto a proxy file on the local file system.
42280**
42281**
42282** Using proxy locks
42283** -----------------
42284**
42285** C APIs
42286**
42287** sqlite3_file_control(db, dbname, SQLITE_FCNTL_SET_LOCKPROXYFILE,
42288** <proxy_path> | ":auto:");
42289** sqlite3_file_control(db, dbname, SQLITE_FCNTL_GET_LOCKPROXYFILE,
42290** &<proxy_path>);
42291**
42292**
42293** SQL pragmas
42294**
42295** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
42296** PRAGMA [database.]lock_proxy_file
42297**
42298** Specifying ":auto:" means that if there is a conch file with a matching
42299** host ID in it, the proxy path in the conch file will be used, otherwise
42300** a proxy path based on the user's temp dir
42301** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
42302** actual proxy file name is generated from the name and path of the
42303** database file. For example:
42304**
42305** For database path "/Users/me/foo.db"
42306** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
42307**
42308** Once a lock proxy is configured for a database connection, it can not
42309** be removed, however it may be switched to a different proxy path via
42310** the above APIs (assuming the conch file is not being held by another
42311** connection or process).
42312**
42313**
42314** How proxy locking works
42315** -----------------------
42316**
42317** Proxy file locking relies primarily on two new supporting files:
42318**
42319** * conch file to limit access to the database file to a single host
42320** at a time
42321**
42322** * proxy file to act as a proxy for the advisory locks normally
42323** taken on the database
42324**
42325** The conch file - to use a proxy file, sqlite must first "hold the conch"
42326** by taking an sqlite-style shared lock on the conch file, reading the
42327** contents and comparing the host's unique host ID (see below) and lock
42328** proxy path against the values stored in the conch. The conch file is
42329** stored in the same directory as the database file and the file name
42330** is patterned after the database file name as ".<databasename>-conch".
42331** If the conch file does not exist, or its contents do not match the
42332** host ID and/or proxy path, then the lock is escalated to an exclusive
42333** lock and the conch file contents is updated with the host ID and proxy
42334** path and the lock is downgraded to a shared lock again. If the conch
42335** is held by another process (with a shared lock), the exclusive lock
42336** will fail and SQLITE_BUSY is returned.
42337**
42338** The proxy file - a single-byte file used for all advisory file locks
42339** normally taken on the database file. This allows for safe sharing
42340** of the database file for multiple readers and writers on the same
42341** host (the conch ensures that they all use the same local lock file).
42342**
42343** Requesting the lock proxy does not immediately take the conch, it is
42344** only taken when the first request to lock database file is made.
42345** This matches the semantics of the traditional locking behavior, where
42346** opening a connection to a database file does not take a lock on it.
42347** The shared lock and an open file descriptor are maintained until
42348** the connection to the database is closed.
42349**
42350** The proxy file and the lock file are never deleted so they only need
42351** to be created the first time they are used.
42352**
42353** Configuration options
42354** ---------------------
42355**
42356** SQLITE_PREFER_PROXY_LOCKING
42357**
42358** Database files accessed on non-local file systems are
42359** automatically configured for proxy locking, lock files are
42360** named automatically using the same logic as
42361** PRAGMA lock_proxy_file=":auto:"
42362**
42363** SQLITE_PROXY_DEBUG
42364**
42365** Enables the logging of error messages during host id file
42366** retrieval and creation
42367**
42368** LOCKPROXYDIR
42369**
42370** Overrides the default directory used for lock proxy files that
42371** are named automatically via the ":auto:" setting
42372**
42373** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
42374**
42375** Permissions to use when creating a directory for storing the
42376** lock proxy files, only used when LOCKPROXYDIR is not set.
42377**
42378**
42379** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
42380** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
42381** force proxy locking to be used for every database file opened, and 0
42382** will force automatic proxy locking to be disabled for all database
42383** files (explicitly calling the SQLITE_FCNTL_SET_LOCKPROXYFILE pragma or
42384** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
42385*/
42386
42387/*
42388** Proxy locking is only available on MacOSX
42389*/
42390#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
42391
42392/*
42393** The proxyLockingContext has the path and file structures for the remote
42394** and local proxy files in it
42395*/
42396typedef struct proxyLockingContext proxyLockingContext;
42397struct proxyLockingContext {
42398 unixFile *conchFile; /* Open conch file */
42399 char *conchFilePath; /* Name of the conch file */
42400 unixFile *lockProxy; /* Open proxy lock file */
42401 char *lockProxyPath; /* Name of the proxy lock file */
42402 char *dbPath; /* Name of the open file */
42403 int conchHeld; /* 1 if the conch is held, -1 if lockless */
42404 int nFails; /* Number of conch taking failures */
42405 void *oldLockingContext; /* Original lockingcontext to restore on close */
42406 sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */
42407};
42408
42409/*
42410** The proxy lock file path for the database at dbPath is written into lPath,
42411** which must point to valid, writable memory large enough for a maxLen length
42412** file path.
42413*/
42414static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
42415 int len;
42416 int dbLen;
42417 int i;
42418
42419#ifdef LOCKPROXYDIR
42420 len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
42421#else
42422# ifdef _CS_DARWIN_USER_TEMP_DIR
42423 {
42424 if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
42425 OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n",
42426 lPath, errno, osGetpid(0)));
42427 return SQLITE_IOERR_LOCK(10 | (15<<8));
42428 }
42429 len = strlcat(lPath, "sqliteplocks", maxLen);
42430 }
42431# else
42432 len = strlcpy(lPath, "/tmp/", maxLen);
42433# endif
42434#endif
42435
42436 if( lPath[len-1]!='/' ){
42437 len = strlcat(lPath, "/", maxLen);
42438 }
42439
42440 /* transform the db path to a unique cache name */
42441 dbLen = (int)strlen(dbPath);
42442 for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
42443 char c = dbPath[i];
42444 lPath[i+len] = (c=='/')?'_':c;
42445 }
42446 lPath[i+len]='\0';
42447 strlcat(lPath, ":auto:", maxLen);
42448 OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, osGetpid(0)));
42449 return SQLITE_OK0;
42450}
42451
42452/*
42453 ** Creates the lock file and any missing directories in lockPath
42454 */
42455static int proxyCreateLockPath(const char *lockPath){
42456 int i, len;
42457 char buf[MAXPATHLEN];
42458 int start = 0;
42459
42460 assert(lockPath!=NULL)((void) (0));
42461 /* try to create all the intermediate directories */
42462 len = (int)strlen(lockPath);
42463 buf[0] = lockPath[0];
42464 for( i=1; i<len; i++ ){
42465 if( lockPath[i] == '/' && (i - start > 0) ){
42466 /* only mkdir if leaf dir != "." or "/" or ".." */
42467 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
42468 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
42469 buf[i]='\0';
42470 if( osMkdir((int(*)(const char*,mode_t))aSyscall[18].pCurrent)(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS0755) ){
42471 int err=errno(*__errno_location ());
42472 if( err!=EEXIST17 ) {
42473 OSTRACE(("CREATELOCKPATH FAILED creating %s, "
42474 "'%s' proxy lock path=%s pid=%d\n",
42475 buf, strerror(err), lockPath, osGetpid(0)));
42476 return err;
42477 }
42478 }
42479 }
42480 start=i+1;
42481 }
42482 buf[i] = lockPath[i];
42483 }
42484 OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n",lockPath,osGetpid(0)));
42485 return 0;
42486}
42487
42488/*
42489** Create a new VFS file descriptor (stored in memory obtained from
42490** sqlite3_malloc) and open the file named "path" in the file descriptor.
42491**
42492** The caller is responsible not only for closing the file descriptor
42493** but also for freeing the memory associated with the file descriptor.
42494*/
42495static int proxyCreateUnixFile(
42496 const char *path, /* path for the new unixFile */
42497 unixFile **ppFile, /* unixFile created and returned by ref */
42498 int islockfile /* if non zero missing dirs will be created */
42499) {
42500 int fd = -1;
42501 unixFile *pNew;
42502 int rc = SQLITE_OK0;
42503 int openFlags = O_RDWR02 | O_CREAT0100 | O_NOFOLLOW0400000;
42504 sqlite3_vfs dummyVfs;
42505 int terrno = 0;
42506 UnixUnusedFd *pUnused = NULL((void*)0);
42507
42508 /* 1. first try to open/create the file
42509 ** 2. if that fails, and this is a lock file (not-conch), try creating
42510 ** the parent directories and then try again.
42511 ** 3. if that fails, try to open the file read-only
42512 ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
42513 */
42514 pUnused = findReusableFd(path, openFlags);
42515 if( pUnused ){
42516 fd = pUnused->fd;
42517 }else{
42518 pUnused = sqlite3_malloc64(sizeof(*pUnused));
42519 if( !pUnused ){
42520 return SQLITE_NOMEM_BKPT7;
42521 }
42522 }
42523 if( fd<0 ){
42524 fd = robust_open(path, openFlags, 0);
42525 terrno = errno(*__errno_location ());
42526 if( fd<0 && errno(*__errno_location ())==ENOENT2 && islockfile ){
42527 if( proxyCreateLockPath(path) == SQLITE_OK0 ){
42528 fd = robust_open(path, openFlags, 0);
42529 }
42530 }
42531 }
42532 if( fd<0 ){
42533 openFlags = O_RDONLY00 | O_NOFOLLOW0400000;
42534 fd = robust_open(path, openFlags, 0);
42535 terrno = errno(*__errno_location ());
42536 }
42537 if( fd<0 ){
42538 if( islockfile ){
42539 return SQLITE_BUSY5;
42540 }
42541 switch (terrno) {
42542 case EACCES13:
42543 return SQLITE_PERM3;
42544 case EIO5:
42545 return SQLITE_IOERR_LOCK(10 | (15<<8)); /* even though it is the conch */
42546 default:
42547 return SQLITE_CANTOPEN_BKPTsqlite3CantopenError(42547);
42548 }
42549 }
42550
42551 pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew));
42552 if( pNew==NULL((void*)0) ){
42553 rc = SQLITE_NOMEM_BKPT7;
42554 goto end_create_proxy;
42555 }
42556 memset(pNew, 0, sizeof(unixFile));
42557 pNew->openFlags = openFlags;
42558 memset(&dummyVfs, 0, sizeof(dummyVfs));
42559 dummyVfs.pAppData = (void*)&autolockIoFinder;
42560 dummyVfs.zName = "dummy";
42561 pUnused->fd = fd;
42562 pUnused->flags = openFlags;
42563 pNew->pPreallocatedUnused = pUnused;
42564
42565 rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
42566 if( rc==SQLITE_OK0 ){
42567 *ppFile = pNew;
42568 return SQLITE_OK0;
42569 }
42570end_create_proxy:
42571 robust_close(pNew, fd, __LINE__42571);
42572 sqlite3_free(pNew);
42573 sqlite3_free(pUnused);
42574 return rc;
42575}
42576
42577#ifdef SQLITE_TEST
42578/* simulate multiple hosts by creating unique hostid file paths */
42579SQLITE_API int sqlite3_hostid_num = 0;
42580#endif
42581
42582#define PROXY_HOSTIDLEN 16 /* conch file host id length */
42583
42584#if HAVE_GETHOSTUUID0
42585/* Not always defined in the headers as it ought to be */
42586extern int gethostuuid(uuid_t id, const struct timespec *wait);
42587#endif
42588
42589/* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN
42590** bytes of writable memory.
42591*/
42592static int proxyGetHostID(unsigned char *pHostID, int *pError){
42593 assert(PROXY_HOSTIDLEN == sizeof(uuid_t))((void) (0));
42594 memset(pHostID, 0, PROXY_HOSTIDLEN);
42595#if HAVE_GETHOSTUUID0
42596 {
42597 struct timespec timeout = {1, 0}; /* 1 sec timeout */
42598 if( gethostuuid(pHostID, &timeout) ){
42599 int err = errno(*__errno_location ());
42600 if( pError ){
42601 *pError = err;
42602 }
42603 return SQLITE_IOERR10;
42604 }
42605 }
42606#else
42607 UNUSED_PARAMETER(pError)(void)(pError);
42608#endif
42609#ifdef SQLITE_TEST
42610 /* simulate multiple hosts by creating unique hostid file paths */
42611 if( sqlite3_hostid_num != 0){
42612 pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
42613 }
42614#endif
42615
42616 return SQLITE_OK0;
42617}
42618
42619/* The conch file contains the header, host id and lock file path
42620 */
42621#define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */
42622#define PROXY_HEADERLEN 1 /* conch file header length */
42623#define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN)
42624#define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN)
42625
42626/*
42627** Takes an open conch file, copies the contents to a new path and then moves
42628** it back. The newly created file's file descriptor is assigned to the
42629** conch file structure and finally the original conch file descriptor is
42630** closed. Returns zero if successful.
42631*/
42632static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
42633 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
42634 unixFile *conchFile = pCtx->conchFile;
42635 char tPath[MAXPATHLEN];
42636 char buf[PROXY_MAXCONCHLEN];
42637 char *cPath = pCtx->conchFilePath;
42638 size_t readLen = 0;
42639 size_t pathLen = 0;
42640 char errmsg[64] = "";
42641 int fd = -1;
42642 int rc = -1;
42643 UNUSED_PARAMETER(myHostID)(void)(myHostID);
42644
42645 /* create a new path by replace the trailing '-conch' with '-break' */
42646 pathLen = strlcpy(tPath, cPath, MAXPATHLEN);
42647 if( pathLen>MAXPATHLEN || pathLen<6 ||
42648 (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
42649 sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
42650 goto end_breaklock;
42651 }
42652 /* read the conch content */
42653 readLen = osPread((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
42654 if( readLen<PROXY_PATHINDEX ){
42655 sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
42656 goto end_breaklock;
42657 }
42658 /* write it out to the temporary break file */
42659 fd = robust_open(tPath, (O_RDWR02|O_CREAT0100|O_EXCL0200|O_NOFOLLOW0400000), 0);
42660 if( fd<0 ){
42661 sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno(*__errno_location ()));
42662 goto end_breaklock;
42663 }
42664 if( osPwrite((ssize_t(*)(int,const void*,size_t,off_t)) aSyscall[12].pCurrent
)
(fd, buf, readLen, 0) != (ssize_t)readLen ){
42665 sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno(*__errno_location ()));
42666 goto end_breaklock;
42667 }
42668 if( rename(tPath, cPath) ){
42669 sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno(*__errno_location ()));
42670 goto end_breaklock;
42671 }
42672 rc = 0;
42673 fprintf(stderrstderr, "broke stale lock on %s\n", cPath);
42674 robust_close(pFile, conchFile->h, __LINE__42674);
42675 conchFile->h = fd;
42676 conchFile->openFlags = O_RDWR02 | O_CREAT0100;
42677
42678end_breaklock:
42679 if( rc ){
42680 if( fd>=0 ){
42681 osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(tPath);
42682 robust_close(pFile, fd, __LINE__42682);
42683 }
42684 fprintf(stderrstderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
42685 }
42686 return rc;
42687}
42688
42689/* Take the requested lock on the conch file and break a stale lock if the
42690** host id matches.
42691*/
42692static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
42693 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
42694 unixFile *conchFile = pCtx->conchFile;
42695 int rc = SQLITE_OK0;
42696 int nTries = 0;
42697 struct timespec conchModTime;
42698
42699 memset(&conchModTime, 0, sizeof(conchModTime));
42700 do {
42701 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
42702 nTries ++;
42703 if( rc==SQLITE_BUSY5 ){
42704 /* If the lock failed (busy):
42705 * 1st try: get the mod time of the conch, wait 0.5s and try again.
42706 * 2nd try: fail if the mod time changed or host id is different, wait
42707 * 10 sec and try again
42708 * 3rd try: break the lock unless the mod time has changed.
42709 */
42710 struct stat buf;
42711 if( osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(conchFile->h, &buf) ){
42712 storeLastErrno(pFile, errno(*__errno_location ()));
42713 return SQLITE_IOERR_LOCK(10 | (15<<8));
42714 }
42715
42716 if( nTries==1 ){
42717 conchModTime = buf.st_mtimespec;
42718 unixSleep(0,500000); /* wait 0.5 sec and try the lock again*/
42719 continue;
42720 }
42721
42722 assert( nTries>1 )((void) (0));
42723 if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec ||
42724 conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
42725 return SQLITE_BUSY5;
42726 }
42727
42728 if( nTries==2 ){
42729 char tBuf[PROXY_MAXCONCHLEN];
42730 int len = osPread((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
42731 if( len<0 ){
42732 storeLastErrno(pFile, errno(*__errno_location ()));
42733 return SQLITE_IOERR_LOCK(10 | (15<<8));
42734 }
42735 if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
42736 /* don't break the lock if the host id doesn't match */
42737 if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){
42738 return SQLITE_BUSY5;
42739 }
42740 }else{
42741 /* don't break the lock on short read or a version mismatch */
42742 return SQLITE_BUSY5;
42743 }
42744 unixSleep(0,10000000); /* wait 10 sec and try the lock again */
42745 continue;
42746 }
42747
42748 assert( nTries==3 )((void) (0));
42749 if( 0==proxyBreakConchLock(pFile, myHostID) ){
42750 rc = SQLITE_OK0;
42751 if( lockType==EXCLUSIVE_LOCK4 ){
42752 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK1);
42753 }
42754 if( !rc ){
42755 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
42756 }
42757 }
42758 }
42759 } while( rc==SQLITE_BUSY5 && nTries<3 );
42760
42761 return rc;
42762}
42763
42764/* Takes the conch by taking a shared lock and read the contents conch, if
42765** lockPath is non-NULL, the host ID and lock file path must match. A NULL
42766** lockPath means that the lockPath in the conch file will be used if the
42767** host IDs match, or a new lock path will be generated automatically
42768** and written to the conch file.
42769*/
42770static int proxyTakeConch(unixFile *pFile){
42771 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
42772
42773 if( pCtx->conchHeld!=0 ){
42774 return SQLITE_OK0;
42775 }else{
42776 unixFile *conchFile = pCtx->conchFile;
42777 uuid_t myHostID;
42778 int pError = 0;
42779 char readBuf[PROXY_MAXCONCHLEN];
42780 char lockPath[MAXPATHLEN];
42781 char *tempLockPath = NULL((void*)0);
42782 int rc = SQLITE_OK0;
42783 int createConch = 0;
42784 int hostIdMatch = 0;
42785 int readLen = 0;
42786 int tryOldLockPath = 0;
42787 int forceNewLockPath = 0;
42788
42789 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
42790 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
42791 osGetpid(0)));
42792
42793 rc = proxyGetHostID(myHostID, &pError);
42794 if( (rc&0xff)==SQLITE_IOERR10 ){
42795 storeLastErrno(pFile, pError);
42796 goto end_takeconch;
42797 }
42798 rc = proxyConchLock(pFile, myHostID, SHARED_LOCK1);
42799 if( rc!=SQLITE_OK0 ){
42800 goto end_takeconch;
42801 }
42802 /* read the existing conch file */
42803 readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN);
42804 if( readLen<0 ){
42805 /* I/O error: lastErrno set by seekAndRead */
42806 storeLastErrno(pFile, conchFile->lastErrno);
42807 rc = SQLITE_IOERR_READ(10 | (1<<8));
42808 goto end_takeconch;
42809 }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) ||
42810 readBuf[0]!=(char)PROXY_CONCHVERSION ){
42811 /* a short read or version format mismatch means we need to create a new
42812 ** conch file.
42813 */
42814 createConch = 1;
42815 }
42816 /* if the host id matches and the lock path already exists in the conch
42817 ** we'll try to use the path there, if we can't open that path, we'll
42818 ** retry with a new auto-generated path
42819 */
42820 do { /* in case we need to try again for an :auto: named lock file */
42821
42822 if( !createConch && !forceNewLockPath ){
42823 hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID,
42824 PROXY_HOSTIDLEN);
42825 /* if the conch has data compare the contents */
42826 if( !pCtx->lockProxyPath ){
42827 /* for auto-named local lock file, just check the host ID and we'll
42828 ** use the local lock file path that's already in there
42829 */
42830 if( hostIdMatch ){
42831 size_t pathLen = (readLen - PROXY_PATHINDEX);
42832
42833 if( pathLen>=MAXPATHLEN ){
42834 pathLen=MAXPATHLEN-1;
42835 }
42836 memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen);
42837 lockPath[pathLen] = 0;
42838 tempLockPath = lockPath;
42839 tryOldLockPath = 1;
42840 /* create a copy of the lock path if the conch is taken */
42841 goto end_takeconch;
42842 }
42843 }else if( hostIdMatch
42844 && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
42845 readLen-PROXY_PATHINDEX)
42846 ){
42847 /* conch host and lock path match */
42848 goto end_takeconch;
42849 }
42850 }
42851
42852 /* if the conch isn't writable and doesn't match, we can't take it */
42853 if( (conchFile->openFlags&O_RDWR02) == 0 ){
42854 rc = SQLITE_BUSY5;
42855 goto end_takeconch;
42856 }
42857
42858 /* either the conch didn't match or we need to create a new one */
42859 if( !pCtx->lockProxyPath ){
42860 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
42861 tempLockPath = lockPath;
42862 /* create a copy of the lock path _only_ if the conch is taken */
42863 }
42864
42865 /* update conch with host and path (this will fail if other process
42866 ** has a shared lock already), if the host id matches, use the big
42867 ** stick.
42868 */
42869 futimes(conchFile->h, NULL((void*)0));
42870 if( hostIdMatch && !createConch ){
42871 if( conchFile->pInode && conchFile->pInode->nShared>1 ){
42872 /* We are trying for an exclusive lock but another thread in this
42873 ** same process is still holding a shared lock. */
42874 rc = SQLITE_BUSY5;
42875 } else {
42876 rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK4);
42877 }
42878 }else{
42879 rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK4);
42880 }
42881 if( rc==SQLITE_OK0 ){
42882 char writeBuffer[PROXY_MAXCONCHLEN];
42883 int writeSize = 0;
42884
42885 writeBuffer[0] = (char)PROXY_CONCHVERSION;
42886 memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
42887 if( pCtx->lockProxyPath!=NULL((void*)0) ){
42888 strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
42889 MAXPATHLEN);
42890 }else{
42891 strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
42892 }
42893 writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
42894 robust_ftruncate(conchFile->h, writeSize);
42895 rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
42896 full_fsync(conchFile->h,0,0);
42897 /* If we created a new conch file (not just updated the contents of a
42898 ** valid conch file), try to match the permissions of the database
42899 */
42900 if( rc==SQLITE_OK0 && createConch ){
42901 struct stat buf;
42902 int err = osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(pFile->h, &buf);
42903 if( err==0 ){
42904 mode_t cmode = buf.st_mode&(S_IRUSR0400|S_IWUSR0200 | S_IRGRP(0400 >> 3)|S_IWGRP(0200 >> 3) |
42905 S_IROTH((0400 >> 3) >> 3)|S_IWOTH((0200 >> 3) >> 3));
42906 /* try to match the database file R/W permissions, ignore failure */
42907#ifndef SQLITE_PROXY_DEBUG
42908 osFchmod((int(*)(int,mode_t))aSyscall[14].pCurrent)(conchFile->h, cmode);
42909#else
42910 do{
42911 rc = osFchmod((int(*)(int,mode_t))aSyscall[14].pCurrent)(conchFile->h, cmode);
42912 }while( rc==(-1) && errno(*__errno_location ())==EINTR4 );
42913 if( rc!=0 ){
42914 int code = errno(*__errno_location ());
42915 fprintf(stderrstderr, "fchmod %o FAILED with %d %s\n",
42916 cmode, code, strerror(code));
42917 } else {
42918 fprintf(stderrstderr, "fchmod %o SUCCEDED\n",cmode);
42919 }
42920 }else{
42921 int code = errno(*__errno_location ());
42922 fprintf(stderrstderr, "STAT FAILED[%d] with %d %s\n",
42923 err, code, strerror(code));
42924#endif
42925 }
42926 }
42927 }
42928 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK1);
42929
42930 end_takeconch:
42931 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
42932 if( rc==SQLITE_OK0 && pFile->openFlags ){
42933 int fd;
42934 if( pFile->h>=0 ){
42935 robust_close(pFile, pFile->h, __LINE__42935);
42936 }
42937 pFile->h = -1;
42938 fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
42939 OSTRACE(("TRANSPROXY: OPEN %d\n", fd));
42940 if( fd>=0 ){
42941 pFile->h = fd;
42942 }else{
42943 rc=SQLITE_CANTOPEN_BKPTsqlite3CantopenError(42943); /* SQLITE_BUSY? proxyTakeConch called
42944 during locking */
42945 }
42946 }
42947 if( rc==SQLITE_OK0 && !pCtx->lockProxy ){
42948 char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
42949 rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
42950 if( rc!=SQLITE_OK0 && rc!=SQLITE_NOMEM7 && tryOldLockPath ){
42951 /* we couldn't create the proxy lock file with the old lock file path
42952 ** so try again via auto-naming
42953 */
42954 forceNewLockPath = 1;
42955 tryOldLockPath = 0;
42956 continue; /* go back to the do {} while start point, try again */
42957 }
42958 }
42959 if( rc==SQLITE_OK0 ){
42960 /* Need to make a copy of path if we extracted the value
42961 ** from the conch file or the path was allocated on the stack
42962 */
42963 if( tempLockPath ){
42964 pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
42965 if( !pCtx->lockProxyPath ){
42966 rc = SQLITE_NOMEM_BKPT7;
42967 }
42968 }
42969 }
42970 if( rc==SQLITE_OK0 ){
42971 pCtx->conchHeld = 1;
42972
42973 if( pCtx->lockProxy->pMethod == &afpIoMethods ){
42974 afpLockingContext *afpCtx;
42975 afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
42976 afpCtx->dbPath = pCtx->lockProxyPath;
42977 }
42978 } else {
42979 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK0);
42980 }
42981 OSTRACE(("TAKECONCH %d %s\n", conchFile->h,
42982 rc==SQLITE_OK?"ok":"failed"));
42983 return rc;
42984 } while (1); /* in case we need to retry the :auto: lock file -
42985 ** we should never get here except via the 'continue' call. */
42986 }
42987}
42988
42989/*
42990** If pFile holds a lock on a conch file, then release that lock.
42991*/
42992static int proxyReleaseConch(unixFile *pFile){
42993 int rc = SQLITE_OK0; /* Subroutine return code */
42994 proxyLockingContext *pCtx; /* The locking context for the proxy lock */
42995 unixFile *conchFile; /* Name of the conch file */
42996
42997 pCtx = (proxyLockingContext *)pFile->lockingContext;
42998 conchFile = pCtx->conchFile;
42999 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
43000 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
43001 osGetpid(0)));
43002 if( pCtx->conchHeld>0 ){
43003 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK0);
43004 }
43005 pCtx->conchHeld = 0;
43006 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
43007 (rc==SQLITE_OK ? "ok" : "failed")));
43008 return rc;
43009}
43010
43011/*
43012** Given the name of a database file, compute the name of its conch file.
43013** Store the conch filename in memory obtained from sqlite3_malloc64().
43014** Make *pConchPath point to the new name. Return SQLITE_OK on success
43015** or SQLITE_NOMEM if unable to obtain memory.
43016**
43017** The caller is responsible for ensuring that the allocated memory
43018** space is eventually freed.
43019**
43020** *pConchPath is set to NULL if a memory allocation error occurs.
43021*/
43022static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
43023 int i; /* Loop counter */
43024 int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
43025 char *conchPath; /* buffer in which to construct conch name */
43026
43027 /* Allocate space for the conch filename and initialize the name to
43028 ** the name of the original database file. */
43029 *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8);
43030 if( conchPath==0 ){
43031 return SQLITE_NOMEM_BKPT7;
43032 }
43033 memcpy(conchPath, dbPath, len+1);
43034
43035 /* now insert a "." before the last / character */
43036 for( i=(len-1); i>=0; i-- ){
43037 if( conchPath[i]=='/' ){
43038 i++;
43039 break;
43040 }
43041 }
43042 conchPath[i]='.';
43043 while ( i<len ){
43044 conchPath[i+1]=dbPath[i];
43045 i++;
43046 }
43047
43048 /* append the "-conch" suffix to the file */
43049 memcpy(&conchPath[i+1], "-conch", 7);
43050 assert( (int)strlen(conchPath) == len+7 )((void) (0));
43051
43052 return SQLITE_OK0;
43053}
43054
43055
43056/* Takes a fully configured proxy locking-style unix file and switches
43057** the local lock file path
43058*/
43059static int switchLockProxyPath(unixFile *pFile, const char *path) {
43060 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
43061 char *oldPath = pCtx->lockProxyPath;
43062 int rc = SQLITE_OK0;
43063
43064 if( pFile->eFileLock!=NO_LOCK0 ){
43065 return SQLITE_BUSY5;
43066 }
43067
43068 /* nothing to do if the path is NULL, :auto: or matches the existing path */
43069 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
43070 (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
43071 return SQLITE_OK0;
43072 }else{
43073 unixFile *lockProxy = pCtx->lockProxy;
43074 pCtx->lockProxy=NULL((void*)0);
43075 pCtx->conchHeld = 0;
43076 if( lockProxy!=NULL((void*)0) ){
43077 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
43078 if( rc ) return rc;
43079 sqlite3_free(lockProxy);
43080 }
43081 sqlite3_free(oldPath);
43082 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
43083 }
43084
43085 return rc;
43086}
43087
43088/*
43089** pFile is a file that has been opened by a prior xOpen call. dbPath
43090** is a string buffer at least MAXPATHLEN+1 characters in size.
43091**
43092** This routine find the filename associated with pFile and writes it
43093** int dbPath.
43094*/
43095static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
43096#if defined(__APPLE__)
43097 if( pFile->pMethod == &afpIoMethods ){
43098 /* afp style keeps a reference to the db path in the filePath field
43099 ** of the struct */
43100 assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN )((void) (0));
43101 strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
43102 MAXPATHLEN);
43103 } else
43104#endif
43105 if( pFile->pMethod == &dotlockIoMethods ){
43106 /* dot lock style uses the locking context to store the dot lock
43107 ** file path */
43108 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX".lock");
43109 memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
43110 }else{
43111 /* all other styles use the locking context to store the db file path */
43112 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN )((void) (0));
43113 strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
43114 }
43115 return SQLITE_OK0;
43116}
43117
43118/*
43119** Takes an already filled in unix file and alters it so all file locking
43120** will be performed on the local proxy lock file. The following fields
43121** are preserved in the locking context so that they can be restored and
43122** the unix structure properly cleaned up at close time:
43123** ->lockingContext
43124** ->pMethod
43125*/
43126static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
43127 proxyLockingContext *pCtx;
43128 char dbPath[MAXPATHLEN+1]; /* Name of the database file */
43129 char *lockPath=NULL((void*)0);
43130 int rc = SQLITE_OK0;
43131
43132 if( pFile->eFileLock!=NO_LOCK0 ){
43133 return SQLITE_BUSY5;
43134 }
43135 proxyGetDbPathForUnixFile(pFile, dbPath);
43136 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
43137 lockPath=NULL((void*)0);
43138 }else{
43139 lockPath=(char *)path;
43140 }
43141
43142 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
43143 (lockPath ? lockPath : ":auto:"), osGetpid(0)));
43144
43145 pCtx = sqlite3_malloc64( sizeof(*pCtx) );
43146 if( pCtx==0 ){
43147 return SQLITE_NOMEM_BKPT7;
43148 }
43149 memset(pCtx, 0, sizeof(*pCtx));
43150
43151 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
43152 if( rc==SQLITE_OK0 ){
43153 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
43154 if( rc==SQLITE_CANTOPEN14 && ((pFile->openFlags&O_RDWR02) == 0) ){
43155 /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
43156 ** (c) the file system is read-only, then enable no-locking access.
43157 ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
43158 ** that openFlags will have only one of O_RDONLY or O_RDWR.
43159 */
43160 struct statfs fsInfo;
43161 struct stat conchInfo;
43162 int goLockless = 0;
43163
43164 if( osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)(pCtx->conchFilePath, &conchInfo) == -1 ) {
43165 int err = errno(*__errno_location ());
43166 if( (err==ENOENT2) && (statfs(dbPath, &fsInfo) != -1) ){
43167 goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY;
43168 }
43169 }
43170 if( goLockless ){
43171 pCtx->conchHeld = -1; /* read only FS/ lockless */
43172 rc = SQLITE_OK0;
43173 }
43174 }
43175 }
43176 if( rc==SQLITE_OK0 && lockPath ){
43177 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
43178 }
43179
43180 if( rc==SQLITE_OK0 ){
43181 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
43182 if( pCtx->dbPath==NULL((void*)0) ){
43183 rc = SQLITE_NOMEM_BKPT7;
43184 }
43185 }
43186 if( rc==SQLITE_OK0 ){
43187 /* all memory is allocated, proxys are created and assigned,
43188 ** switch the locking context and pMethod then return.
43189 */
43190 pCtx->oldLockingContext = pFile->lockingContext;
43191 pFile->lockingContext = pCtx;
43192 pCtx->pOldMethod = pFile->pMethod;
43193 pFile->pMethod = &proxyIoMethods;
43194 }else{
43195 if( pCtx->conchFile ){
43196 pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
43197 sqlite3_free(pCtx->conchFile);
43198 }
43199 sqlite3DbFree(0, pCtx->lockProxyPath);
43200 sqlite3_free(pCtx->conchFilePath);
43201 sqlite3_free(pCtx);
43202 }
43203 OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
43204 (rc==SQLITE_OK ? "ok" : "failed")));
43205 return rc;
43206}
43207
43208
43209/*
43210** This routine handles sqlite3_file_control() calls that are specific
43211** to proxy locking.
43212*/
43213static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
43214 switch( op ){
43215 case SQLITE_FCNTL_GET_LOCKPROXYFILE2: {
43216 unixFile *pFile = (unixFile*)id;
43217 if( pFile->pMethod == &proxyIoMethods ){
43218 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
43219 proxyTakeConch(pFile);
43220 if( pCtx->lockProxyPath ){
43221 *(const char **)pArg = pCtx->lockProxyPath;
43222 }else{
43223 *(const char **)pArg = ":auto: (not held)";
43224 }
43225 } else {
43226 *(const char **)pArg = NULL((void*)0);
43227 }
43228 return SQLITE_OK0;
43229 }
43230 case SQLITE_FCNTL_SET_LOCKPROXYFILE3: {
43231 unixFile *pFile = (unixFile*)id;
43232 int rc = SQLITE_OK0;
43233 int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
43234 if( pArg==NULL((void*)0) || (const char *)pArg==0 ){
43235 if( isProxyStyle ){
43236 /* turn off proxy locking - not supported. If support is added for
43237 ** switching proxy locking mode off then it will need to fail if
43238 ** the journal mode is WAL mode.
43239 */
43240 rc = SQLITE_ERROR1 /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
43241 }else{
43242 /* turn off proxy locking - already off - NOOP */
43243 rc = SQLITE_OK0;
43244 }
43245 }else{
43246 const char *proxyPath = (const char *)pArg;
43247 if( isProxyStyle ){
43248 proxyLockingContext *pCtx =
43249 (proxyLockingContext*)pFile->lockingContext;
43250 if( !strcmp(pArg, ":auto:")
43251 || (pCtx->lockProxyPath &&
43252 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
43253 ){
43254 rc = SQLITE_OK0;
43255 }else{
43256 rc = switchLockProxyPath(pFile, proxyPath);
43257 }
43258 }else{
43259 /* turn on proxy file locking */
43260 rc = proxyTransformUnixFile(pFile, proxyPath);
43261 }
43262 }
43263 return rc;
43264 }
43265 default: {
43266 assert( 0 )((void) (0)); /* The call assures that only valid opcodes are sent */
43267 }
43268 }
43269 /*NOTREACHED*/ assert(0)((void) (0));
43270 return SQLITE_ERROR1;
43271}
43272
43273/*
43274** Within this division (the proxying locking implementation) the procedures
43275** above this point are all utilities. The lock-related methods of the
43276** proxy-locking sqlite3_io_method object follow.
43277*/
43278
43279
43280/*
43281** This routine checks if there is a RESERVED lock held on the specified
43282** file by this or any other process. If such a lock is held, set *pResOut
43283** to a non-zero value otherwise *pResOut is set to zero. The return value
43284** is set to SQLITE_OK unless an I/O error occurs during lock checking.
43285*/
43286static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
43287 unixFile *pFile = (unixFile*)id;
43288 int rc = proxyTakeConch(pFile);
43289 if( rc==SQLITE_OK0 ){
43290 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
43291 if( pCtx->conchHeld>0 ){
43292 unixFile *proxy = pCtx->lockProxy;
43293 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
43294 }else{ /* conchHeld < 0 is lockless */
43295 pResOut=0;
43296 }
43297 }
43298 return rc;
43299}
43300
43301/*
43302** Lock the file with the lock specified by parameter eFileLock - one
43303** of the following:
43304**
43305** (1) SHARED_LOCK
43306** (2) RESERVED_LOCK
43307** (3) PENDING_LOCK
43308** (4) EXCLUSIVE_LOCK
43309**
43310** Sometimes when requesting one lock state, additional lock states
43311** are inserted in between. The locking might fail on one of the later
43312** transitions leaving the lock state different from what it started but
43313** still short of its goal. The following chart shows the allowed
43314** transitions and the inserted intermediate states:
43315**
43316** UNLOCKED -> SHARED
43317** SHARED -> RESERVED
43318** SHARED -> (PENDING) -> EXCLUSIVE
43319** RESERVED -> (PENDING) -> EXCLUSIVE
43320** PENDING -> EXCLUSIVE
43321**
43322** This routine will only increase a lock. Use the sqlite3OsUnlock()
43323** routine to lower a locking level.
43324*/
43325static int proxyLock(sqlite3_file *id, int eFileLock) {
43326 unixFile *pFile = (unixFile*)id;
43327 int rc = proxyTakeConch(pFile);
43328 if( rc==SQLITE_OK0 ){
43329 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
43330 if( pCtx->conchHeld>0 ){
43331 unixFile *proxy = pCtx->lockProxy;
43332 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
43333 pFile->eFileLock = proxy->eFileLock;
43334 }else{
43335 /* conchHeld < 0 is lockless */
43336 }
43337 }
43338 return rc;
43339}
43340
43341
43342/*
43343** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
43344** must be either NO_LOCK or SHARED_LOCK.
43345**
43346** If the locking level of the file descriptor is already at or below
43347** the requested locking level, this routine is a no-op.
43348*/
43349static int proxyUnlock(sqlite3_file *id, int eFileLock) {
43350 unixFile *pFile = (unixFile*)id;
43351 int rc = proxyTakeConch(pFile);
43352 if( rc==SQLITE_OK0 ){
43353 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
43354 if( pCtx->conchHeld>0 ){
43355 unixFile *proxy = pCtx->lockProxy;
43356 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
43357 pFile->eFileLock = proxy->eFileLock;
43358 }else{
43359 /* conchHeld < 0 is lockless */
43360 }
43361 }
43362 return rc;
43363}
43364
43365/*
43366** Close a file that uses proxy locks.
43367*/
43368static int proxyClose(sqlite3_file *id) {
43369 if( ALWAYS(id)(id) ){
43370 unixFile *pFile = (unixFile*)id;
43371 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
43372 unixFile *lockProxy = pCtx->lockProxy;
43373 unixFile *conchFile = pCtx->conchFile;
43374 int rc = SQLITE_OK0;
43375
43376 if( lockProxy ){
43377 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK0);
43378 if( rc ) return rc;
43379 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
43380 if( rc ) return rc;
43381 sqlite3_free(lockProxy);
43382 pCtx->lockProxy = 0;
43383 }
43384 if( conchFile ){
43385 if( pCtx->conchHeld ){
43386 rc = proxyReleaseConch(pFile);
43387 if( rc ) return rc;
43388 }
43389 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
43390 if( rc ) return rc;
43391 sqlite3_free(conchFile);
43392 }
43393 sqlite3DbFree(0, pCtx->lockProxyPath);
43394 sqlite3_free(pCtx->conchFilePath);
43395 sqlite3DbFree(0, pCtx->dbPath);
43396 /* restore the original locking context and pMethod then close it */
43397 pFile->lockingContext = pCtx->oldLockingContext;
43398 pFile->pMethod = pCtx->pOldMethod;
43399 sqlite3_free(pCtx);
43400 return pFile->pMethod->xClose(id);
43401 }
43402 return SQLITE_OK0;
43403}
43404
43405
43406
43407#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
43408/*
43409** The proxy locking style is intended for use with AFP filesystems.
43410** And since AFP is only supported on MacOSX, the proxy locking is also
43411** restricted to MacOSX.
43412**
43413**
43414******************* End of the proxy lock implementation **********************
43415******************************************************************************/
43416
43417/*
43418** Initialize the operating system interface.
43419**
43420** This routine registers all VFS implementations for unix-like operating
43421** systems. This routine, and the sqlite3_os_end() routine that follows,
43422** should be the only routines in this file that are visible from other
43423** files.
43424**
43425** This routine is called once during SQLite initialization and by a
43426** single thread. The memory allocation and mutex subsystems have not
43427** necessarily been initialized when this routine is called, and so they
43428** should not be used.
43429*/
43430SQLITE_API int sqlite3_os_init(void){
43431 /*
43432 ** The following macro defines an initializer for an sqlite3_vfs object.
43433 ** The name of the VFS is NAME. The pAppData is a pointer to a pointer
43434 ** to the "finder" function. (pAppData is a pointer to a pointer because
43435 ** silly C90 rules prohibit a void* from being cast to a function pointer
43436 ** and so we have to go through the intermediate pointer to avoid problems
43437 ** when compiling with -pedantic-errors on GCC.)
43438 **
43439 ** The FINDER parameter to this macro is the name of the pointer to the
43440 ** finder-function. The finder-function returns a pointer to the
43441 ** sqlite_io_methods object that implements the desired locking
43442 ** behaviors. See the division above that contains the IOMETHODS
43443 ** macro for addition information on finder-functions.
43444 **
43445 ** Most finders simply return a pointer to a fixed sqlite3_io_methods
43446 ** object. But the "autolockIoFinder" available on MacOSX does a little
43447 ** more than that; it looks at the filesystem type that hosts the
43448 ** database file and tries to choose an locking method appropriate for
43449 ** that filesystem time.
43450 */
43451 #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, }
{ \
43452 3, /* iVersion */ \
43453 sizeof(unixFile), /* szOsFile */ \
43454 MAX_PATHNAME512, /* mxPathname */ \
43455 0, /* pNext */ \
43456 VFSNAME, /* zName */ \
43457 (void*)&FINDER, /* pAppData */ \
43458 unixOpen, /* xOpen */ \
43459 unixDelete, /* xDelete */ \
43460 unixAccess, /* xAccess */ \
43461 unixFullPathname, /* xFullPathname */ \
43462 unixDlOpen0, /* xDlOpen */ \
43463 unixDlError0, /* xDlError */ \
43464 unixDlSym0, /* xDlSym */ \
43465 unixDlClose0, /* xDlClose */ \
43466 unixRandomness, /* xRandomness */ \
43467 unixSleep, /* xSleep */ \
43468 unixCurrentTime0, /* xCurrentTime */ \
43469 unixGetLastError, /* xGetLastError */ \
43470 unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \
43471 unixSetSystemCall, /* xSetSystemCall */ \
43472 unixGetSystemCall, /* xGetSystemCall */ \
43473 unixNextSystemCall, /* xNextSystemCall */ \
43474 }
43475
43476 /*
43477 ** All default VFSes for unix are contained in the following array.
43478 **
43479 ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
43480 ** by the SQLite core when the VFS is registered. So the following
43481 ** array cannot be const.
43482 */
43483 static sqlite3_vfs aVfs[] = {
43484#if SQLITE_ENABLE_LOCKING_STYLE0 && defined(__APPLE__)
43485 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, }
,
43486#elif OS_VXWORKS0
43487 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, }
,
43488#else
43489 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, }
,
43490#endif
43491 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, }
,
43492 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, }
,
43493 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, }
,
43494#if OS_VXWORKS0
43495 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, }
,
43496#endif
43497#if SQLITE_ENABLE_LOCKING_STYLE0 || OS_VXWORKS0
43498 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, }
,
43499#endif
43500#if SQLITE_ENABLE_LOCKING_STYLE0
43501 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, }
,
43502#endif
43503#if SQLITE_ENABLE_LOCKING_STYLE0 && defined(__APPLE__)
43504 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, }
,
43505 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, }
,
43506 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, }
,
43507#endif
43508 };
43509 unsigned int i; /* Loop counter */
43510
43511 /* Double-check that the aSyscall[] array has been constructed
43512 ** correctly. See ticket [bb3a86e890c8e96ab] */
43513 assert( ArraySize(aSyscall)==29 )((void) (0));
43514
43515 /* Register all VFSes defined in the aVfs[] array */
43516 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
43517 sqlite3_vfs_register(&aVfs[i], i==0);
43518 }
43519 unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS111);
43520
43521#ifndef SQLITE_OMIT_WAL
43522 /* Validate lock assumptions */
43523 assert( SQLITE_SHM_NLOCK==8 )((void) (0)); /* Number of available locks */
43524 assert( UNIX_SHM_BASE==120 )((void) (0)); /* Start of locking area */
43525 /* Locks:
43526 ** WRITE UNIX_SHM_BASE 120
43527 ** CKPT UNIX_SHM_BASE+1 121
43528 ** RECOVER UNIX_SHM_BASE+2 122
43529 ** READ-0 UNIX_SHM_BASE+3 123
43530 ** READ-1 UNIX_SHM_BASE+4 124
43531 ** READ-2 UNIX_SHM_BASE+5 125
43532 ** READ-3 UNIX_SHM_BASE+6 126
43533 ** READ-4 UNIX_SHM_BASE+7 127
43534 ** DMS UNIX_SHM_BASE+8 128
43535 */
43536 assert( UNIX_SHM_DMS==128 )((void) (0)); /* Byte offset of the deadman-switch */
43537#endif
43538
43539 /* Initialize temp file dir array. */
43540 unixTempFileInit();
43541
43542 return SQLITE_OK0;
43543}
43544
43545/*
43546** Shutdown the operating system interface.
43547**
43548** Some operating systems might need to do some cleanup in this routine,
43549** to release dynamically allocated objects. But not on unix.
43550** This routine is a no-op for unix.
43551*/
43552SQLITE_API int sqlite3_os_end(void){
43553 unixBigLock = 0;
43554 return SQLITE_OK0;
43555}
43556
43557#endif /* SQLITE_OS_UNIX */
43558
43559/************** End of os_unix.c *********************************************/
43560/************** Begin file os_win.c ******************************************/
43561/*
43562** 2004 May 22
43563**
43564** The author disclaims copyright to this source code. In place of
43565** a legal notice, here is a blessing:
43566**
43567** May you do good and not evil.
43568** May you find forgiveness for yourself and forgive others.
43569** May you share freely, never taking more than you give.
43570**
43571******************************************************************************
43572**
43573** This file contains code that is specific to Windows.
43574*/
43575/* #include "sqliteInt.h" */
43576#if SQLITE_OS_WIN0 /* This file is used for Windows only */
43577
43578/*
43579** Include code that is common to all os_*.c files
43580*/
43581/* #include "os_common.h" */
43582
43583/*
43584** Include the header file for the Windows VFS.
43585*/
43586/* #include "os_win.h" */
43587
43588/*
43589** Compiling and using WAL mode requires several APIs that are only
43590** available in Windows platforms based on the NT kernel.
43591*/
43592#if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
43593# error "WAL mode requires support from the Windows NT kernel, compile\
43594 with SQLITE_OMIT_WAL."
43595#endif
43596
43597#if !SQLITE_OS_WINNT && SQLITE_MAX_MMAP_SIZE20971520>0
43598# error "Memory mapped files require support from the Windows NT kernel,\
43599 compile with SQLITE_MAX_MMAP_SIZE=0."
43600#endif
43601
43602/*
43603** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
43604** based on the sub-platform)?
43605*/
43606#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI)
43607# define SQLITE_WIN32_HAS_ANSI
43608#endif
43609
43610/*
43611** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions
43612** based on the sub-platform)?
43613*/
43614#if (SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT) && \
43615 !defined(SQLITE_WIN32_NO_WIDE)
43616# define SQLITE_WIN32_HAS_WIDE
43617#endif
43618
43619/*
43620** Make sure at least one set of Win32 APIs is available.
43621*/
43622#if !defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_WIN32_HAS_WIDE)
43623# error "At least one of SQLITE_WIN32_HAS_ANSI and SQLITE_WIN32_HAS_WIDE\
43624 must be defined."
43625#endif
43626
43627/*
43628** Define the required Windows SDK version constants if they are not
43629** already available.
43630*/
43631#ifndef NTDDI_WIN8
43632# define NTDDI_WIN8 0x06020000
43633#endif
43634
43635#ifndef NTDDI_WINBLUE
43636# define NTDDI_WINBLUE 0x06030000
43637#endif
43638
43639#ifndef NTDDI_WINTHRESHOLD
43640# define NTDDI_WINTHRESHOLD 0x06040000
43641#endif
43642
43643/*
43644** Check to see if the GetVersionEx[AW] functions are deprecated on the
43645** target system. GetVersionEx was first deprecated in Win8.1.
43646*/
43647#ifndef SQLITE_WIN32_GETVERSIONEX
43648# if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINBLUE
43649# define SQLITE_WIN32_GETVERSIONEX 0 /* GetVersionEx() is deprecated */
43650# else
43651# define SQLITE_WIN32_GETVERSIONEX 1 /* GetVersionEx() is current */
43652# endif
43653#endif
43654
43655/*
43656** Check to see if the CreateFileMappingA function is supported on the
43657** target system. It is unavailable when using "mincore.lib" on Win10.
43658** When compiling for Windows 10, always assume "mincore.lib" is in use.
43659*/
43660#ifndef SQLITE_WIN32_CREATEFILEMAPPINGA
43661# if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINTHRESHOLD
43662# define SQLITE_WIN32_CREATEFILEMAPPINGA 0
43663# else
43664# define SQLITE_WIN32_CREATEFILEMAPPINGA 1
43665# endif
43666#endif
43667
43668/*
43669** This constant should already be defined (in the "WinDef.h" SDK file).
43670*/
43671#ifndef MAX_PATH
43672# define MAX_PATH (260)
43673#endif
43674
43675/*
43676** Maximum pathname length (in chars) for Win32. This should normally be
43677** MAX_PATH.
43678*/
43679#ifndef SQLITE_WIN32_MAX_PATH_CHARS
43680# define SQLITE_WIN32_MAX_PATH_CHARS (MAX_PATH)
43681#endif
43682
43683/*
43684** This constant should already be defined (in the "WinNT.h" SDK file).
43685*/
43686#ifndef UNICODE_STRING_MAX_CHARS
43687# define UNICODE_STRING_MAX_CHARS (32767)
43688#endif
43689
43690/*
43691** Maximum pathname length (in chars) for WinNT. This should normally be
43692** UNICODE_STRING_MAX_CHARS.
43693*/
43694#ifndef SQLITE_WINNT_MAX_PATH_CHARS
43695# define SQLITE_WINNT_MAX_PATH_CHARS (UNICODE_STRING_MAX_CHARS)
43696#endif
43697
43698/*
43699** Maximum pathname length (in bytes) for Win32. The MAX_PATH macro is in
43700** characters, so we allocate 4 bytes per character assuming worst-case of
43701** 4-bytes-per-character for UTF8.
43702*/
43703#ifndef SQLITE_WIN32_MAX_PATH_BYTES
43704# define SQLITE_WIN32_MAX_PATH_BYTES (SQLITE_WIN32_MAX_PATH_CHARS*4)
43705#endif
43706
43707/*
43708** Maximum pathname length (in bytes) for WinNT. This should normally be
43709** UNICODE_STRING_MAX_CHARS * sizeof(WCHAR).
43710*/
43711#ifndef SQLITE_WINNT_MAX_PATH_BYTES
43712# define SQLITE_WINNT_MAX_PATH_BYTES \
43713 (sizeof(WCHAR) * SQLITE_WINNT_MAX_PATH_CHARS)
43714#endif
43715
43716/*
43717** Maximum error message length (in chars) for WinRT.
43718*/
43719#ifndef SQLITE_WIN32_MAX_ERRMSG_CHARS
43720# define SQLITE_WIN32_MAX_ERRMSG_CHARS (1024)
43721#endif
43722
43723/*
43724** Returns non-zero if the character should be treated as a directory
43725** separator.
43726*/
43727#ifndef winIsDirSep
43728# define winIsDirSep(a) (((a) == '/') || ((a) == '\\'))
43729#endif
43730
43731/*
43732** This macro is used when a local variable is set to a value that is
43733** [sometimes] not used by the code (e.g. via conditional compilation).
43734*/
43735#ifndef UNUSED_VARIABLE_VALUE
43736# define UNUSED_VARIABLE_VALUE(x) (void)(x)
43737#endif
43738
43739/*
43740** Returns the character that should be used as the directory separator.
43741*/
43742#ifndef winGetDirSep
43743# define winGetDirSep() '\\'
43744#endif
43745
43746/*
43747** Do we need to manually define the Win32 file mapping APIs for use with WAL
43748** mode or memory mapped files (e.g. these APIs are available in the Windows
43749** CE SDK; however, they are not present in the header file)?
43750*/
43751#if SQLITE_WIN32_FILEMAPPING_API && \
43752 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0)
43753/*
43754** Two of the file mapping APIs are different under WinRT. Figure out which
43755** set we need.
43756*/
43757#if SQLITE_OS_WINRT
43758WINBASEAPI HANDLE WINAPI CreateFileMappingFromApp(HANDLE, \
43759 LPSECURITY_ATTRIBUTES, ULONG, ULONG64, LPCWSTR);
43760
43761WINBASEAPI LPVOID WINAPI MapViewOfFileFromApp(HANDLE, ULONG, ULONG64, SIZE_T);
43762#else
43763#if defined(SQLITE_WIN32_HAS_ANSI)
43764WINBASEAPI HANDLE WINAPI CreateFileMappingA(HANDLE, LPSECURITY_ATTRIBUTES, \
43765 DWORD, DWORD, DWORD, LPCSTR);
43766#endif /* defined(SQLITE_WIN32_HAS_ANSI) */
43767
43768#if defined(SQLITE_WIN32_HAS_WIDE)
43769WINBASEAPI HANDLE WINAPI CreateFileMappingW(HANDLE, LPSECURITY_ATTRIBUTES, \
43770 DWORD, DWORD, DWORD, LPCWSTR);
43771#endif /* defined(SQLITE_WIN32_HAS_WIDE) */
43772
43773WINBASEAPI LPVOID WINAPI MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, SIZE_T);
43774#endif /* SQLITE_OS_WINRT */
43775
43776/*
43777** These file mapping APIs are common to both Win32 and WinRT.
43778*/
43779
43780WINBASEAPI BOOL WINAPI FlushViewOfFile(LPCVOID, SIZE_T);
43781WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
43782#endif /* SQLITE_WIN32_FILEMAPPING_API */
43783
43784/*
43785** Some Microsoft compilers lack this definition.
43786*/
43787#ifndef INVALID_FILE_ATTRIBUTES
43788# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
43789#endif
43790
43791#ifndef FILE_FLAG_MASK
43792# define FILE_FLAG_MASK (0xFF3C0000)
43793#endif
43794
43795#ifndef FILE_ATTRIBUTE_MASK
43796# define FILE_ATTRIBUTE_MASK (0x0003FFF7)
43797#endif
43798
43799#ifndef SQLITE_OMIT_WAL
43800/* Forward references to structures used for WAL */
43801typedef struct winShm winShm; /* A connection to shared-memory */
43802typedef struct winShmNode winShmNode; /* A region of shared-memory */
43803#endif
43804
43805/*
43806** WinCE lacks native support for file locking so we have to fake it
43807** with some code of our own.
43808*/
43809#if SQLITE_OS_WINCE
43810typedef struct winceLock {
43811 int nReaders; /* Number of reader locks obtained */
43812 BOOL bPending; /* Indicates a pending lock has been obtained */
43813 BOOL bReserved; /* Indicates a reserved lock has been obtained */
43814 BOOL bExclusive; /* Indicates an exclusive lock has been obtained */
43815} winceLock;
43816#endif
43817
43818/*
43819** The winFile structure is a subclass of sqlite3_file* specific to the win32
43820** portability layer.
43821*/
43822typedef struct winFile winFile;
43823struct winFile {
43824 const sqlite3_io_methods *pMethod; /*** Must be first ***/
43825 sqlite3_vfs *pVfs; /* The VFS used to open this file */
43826 HANDLE h; /* Handle for accessing the file */
43827 u8 locktype; /* Type of lock currently held on this file */
43828 short sharedLockByte; /* Randomly chosen byte used as a shared lock */
43829 u8 ctrlFlags; /* Flags. See WINFILE_* below */
43830 DWORD lastErrno; /* The Windows errno from the last I/O error */
43831#ifndef SQLITE_OMIT_WAL
43832 winShm *pShm; /* Instance of shared memory on this file */
43833#endif
43834 const char *zPath; /* Full pathname of this file */
43835 int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
43836#if SQLITE_OS_WINCE
43837 LPWSTR zDeleteOnClose; /* Name of file to delete when closing */
43838 HANDLE hMutex; /* Mutex used to control access to shared lock */
43839 HANDLE hShared; /* Shared memory segment used for locking */
43840 winceLock local; /* Locks obtained by this instance of winFile */
43841 winceLock *shared; /* Global shared lock memory for the file */
43842#endif
43843#if SQLITE_MAX_MMAP_SIZE20971520>0
43844 int nFetchOut; /* Number of outstanding xFetch references */
43845 HANDLE hMap; /* Handle for accessing memory mapping */
43846 void *pMapRegion; /* Area memory mapped */
43847 sqlite3_int64 mmapSize; /* Size of mapped region */
43848 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
43849#endif
43850};
43851
43852/*
43853** The winVfsAppData structure is used for the pAppData member for all of the
43854** Win32 VFS variants.
43855*/
43856typedef struct winVfsAppData winVfsAppData;
43857struct winVfsAppData {
43858 const sqlite3_io_methods *pMethod; /* The file I/O methods to use. */
43859 void *pAppData; /* The extra pAppData, if any. */
43860 BOOL bNoLock; /* Non-zero if locking is disabled. */
43861};
43862
43863/*
43864** Allowed values for winFile.ctrlFlags
43865*/
43866#define WINFILE_RDONLY 0x02 /* Connection is read only */
43867#define WINFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
43868#define WINFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
43869
43870/*
43871 * The size of the buffer used by sqlite3_win32_write_debug().
43872 */
43873#ifndef SQLITE_WIN32_DBG_BUF_SIZE
43874# define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
43875#endif
43876
43877/*
43878 * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
43879 * various Win32 API heap functions instead of our own.
43880 */
43881#ifdef SQLITE_WIN32_MALLOC
43882
43883/*
43884 * If this is non-zero, an isolated heap will be created by the native Win32
43885 * allocator subsystem; otherwise, the default process heap will be used. This
43886 * setting has no effect when compiling for WinRT. By default, this is enabled
43887 * and an isolated heap will be created to store all allocated data.
43888 *
43889 ******************************************************************************
43890 * WARNING: It is important to note that when this setting is non-zero and the
43891 * winMemShutdown function is called (e.g. by the sqlite3_shutdown
43892 * function), all data that was allocated using the isolated heap will
43893 * be freed immediately and any attempt to access any of that freed
43894 * data will almost certainly result in an immediate access violation.
43895 ******************************************************************************
43896 */
43897#ifndef SQLITE_WIN32_HEAP_CREATE
43898# define SQLITE_WIN32_HEAP_CREATE (TRUE)
43899#endif
43900
43901/*
43902 * This is the maximum possible initial size of the Win32-specific heap, in
43903 * bytes.
43904 */
43905#ifndef SQLITE_WIN32_HEAP_MAX_INIT_SIZE
43906# define SQLITE_WIN32_HEAP_MAX_INIT_SIZE (4294967295U)
43907#endif
43908
43909/*
43910 * This is the extra space for the initial size of the Win32-specific heap,
43911 * in bytes. This value may be zero.
43912 */
43913#ifndef SQLITE_WIN32_HEAP_INIT_EXTRA
43914# define SQLITE_WIN32_HEAP_INIT_EXTRA (4194304)
43915#endif
43916
43917/*
43918 * Calculate the maximum legal cache size, in pages, based on the maximum
43919 * possible initial heap size and the default page size, setting aside the
43920 * needed extra space.
43921 */
43922#ifndef SQLITE_WIN32_MAX_CACHE_SIZE
43923# define SQLITE_WIN32_MAX_CACHE_SIZE (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \
43924 (SQLITE_WIN32_HEAP_INIT_EXTRA)) / \
43925 (SQLITE_DEFAULT_PAGE_SIZE4096))
43926#endif
43927
43928/*
43929 * This is cache size used in the calculation of the initial size of the
43930 * Win32-specific heap. It cannot be negative.
43931 */
43932#ifndef SQLITE_WIN32_CACHE_SIZE
43933# if SQLITE_DEFAULT_CACHE_SIZE128>=0
43934# define SQLITE_WIN32_CACHE_SIZE (SQLITE_DEFAULT_CACHE_SIZE128)
43935# else
43936# define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE128))
43937# endif
43938#endif
43939
43940/*
43941 * Make sure that the calculated cache size, in pages, cannot cause the
43942 * initial size of the Win32-specific heap to exceed the maximum amount
43943 * of memory that can be specified in the call to HeapCreate.
43944 */
43945#if SQLITE_WIN32_CACHE_SIZE>SQLITE_WIN32_MAX_CACHE_SIZE
43946# undef SQLITE_WIN32_CACHE_SIZE
43947# define SQLITE_WIN32_CACHE_SIZE (2000)
43948#endif
43949
43950/*
43951 * The initial size of the Win32-specific heap. This value may be zero.
43952 */
43953#ifndef SQLITE_WIN32_HEAP_INIT_SIZE
43954# define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_WIN32_CACHE_SIZE) * \
43955 (SQLITE_DEFAULT_PAGE_SIZE4096) + \
43956 (SQLITE_WIN32_HEAP_INIT_EXTRA))
43957#endif
43958
43959/*
43960 * The maximum size of the Win32-specific heap. This value may be zero.
43961 */
43962#ifndef SQLITE_WIN32_HEAP_MAX_SIZE
43963# define SQLITE_WIN32_HEAP_MAX_SIZE (0)
43964#endif
43965
43966/*
43967 * The extra flags to use in calls to the Win32 heap APIs. This value may be
43968 * zero for the default behavior.
43969 */
43970#ifndef SQLITE_WIN32_HEAP_FLAGS
43971# define SQLITE_WIN32_HEAP_FLAGS (0)
43972#endif
43973
43974
43975/*
43976** The winMemData structure stores information required by the Win32-specific
43977** sqlite3_mem_methods implementation.
43978*/
43979typedef struct winMemData winMemData;
43980struct winMemData {
43981#ifndef NDEBUG1
43982 u32 magic1; /* Magic number to detect structure corruption. */
43983#endif
43984 HANDLE hHeap; /* The handle to our heap. */
43985 BOOL bOwned; /* Do we own the heap (i.e. destroy it on shutdown)? */
43986#ifndef NDEBUG1
43987 u32 magic2; /* Magic number to detect structure corruption. */
43988#endif
43989};
43990
43991#ifndef NDEBUG1
43992#define WINMEM_MAGIC1 0x42b2830b
43993#define WINMEM_MAGIC2 0xbd4d7cf4
43994#endif
43995
43996static struct winMemData win_mem_data = {
43997#ifndef NDEBUG1
43998 WINMEM_MAGIC1,
43999#endif
44000 NULL((void*)0), FALSE
44001#ifndef NDEBUG1
44002 ,WINMEM_MAGIC2
44003#endif
44004};
44005
44006#ifndef NDEBUG1
44007#define winMemAssertMagic1() assert( win_mem_data.magic1==WINMEM_MAGIC1 )((void) (0))
44008#define winMemAssertMagic2() assert( win_mem_data.magic2==WINMEM_MAGIC2 )((void) (0))
44009#define winMemAssertMagic() winMemAssertMagic1(); winMemAssertMagic2();
44010#else
44011#define winMemAssertMagic()
44012#endif
44013
44014#define winMemGetDataPtr() &win_mem_data
44015#define winMemGetHeap() win_mem_data.hHeap
44016#define winMemGetOwned() win_mem_data.bOwned
44017
44018static void *winMemMalloc(int nBytes);
44019static void winMemFree(void *pPrior);
44020static void *winMemRealloc(void *pPrior, int nBytes);
44021static int winMemSize(void *p);
44022static int winMemRoundup(int n);
44023static int winMemInit(void *pAppData);
44024static void winMemShutdown(void *pAppData);
44025
44026SQLITE_PRIVATEstatic const sqlite3_mem_methods *sqlite3MemGetWin32(void);
44027#endif /* SQLITE_WIN32_MALLOC */
44028
44029/*
44030** The following variable is (normally) set once and never changes
44031** thereafter. It records whether the operating system is Win9x
44032** or WinNT.
44033**
44034** 0: Operating system unknown.
44035** 1: Operating system is Win9x.
44036** 2: Operating system is WinNT.
44037**
44038** In order to facilitate testing on a WinNT system, the test fixture
44039** can manually set this value to 1 to emulate Win98 behavior.
44040*/
44041#ifdef SQLITE_TEST
44042SQLITE_API LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
44043#else
44044static LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
44045#endif
44046
44047#ifndef SYSCALL
44048# define SYSCALL sqlite3_syscall_ptr
44049#endif
44050
44051/*
44052** This function is not available on Windows CE or WinRT.
44053 */
44054
44055#if SQLITE_OS_WINCE || SQLITE_OS_WINRT
44056# define osAreFileApisANSI() 1
44057#endif
44058
44059/*
44060** Many system calls are accessed through pointer-to-functions so that
44061** they may be overridden at runtime to facilitate fault injection during
44062** testing and sandboxing. The following array holds the names and pointers
44063** to all overrideable system calls.
44064*/
44065static struct win_syscall {
44066 const char *zName; /* Name of the system call */
44067 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
44068 sqlite3_syscall_ptr pDefault; /* Default value */
44069} aSyscall[] = {
44070#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
44071 { "AreFileApisANSI", (SYSCALL)AreFileApisANSI, 0 },
44072#else
44073 { "AreFileApisANSI", (SYSCALL)0, 0 },
44074#endif
44075
44076#ifndef osAreFileApisANSI
44077#define osAreFileApisANSI ((BOOL(WINAPI*)(VOID))aSyscall[0].pCurrent)
44078#endif
44079
44080#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
44081 { "CharLowerW", (SYSCALL)CharLowerW, 0 },
44082#else
44083 { "CharLowerW", (SYSCALL)0, 0 },
44084#endif
44085
44086#define osCharLowerW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[1].pCurrent)
44087
44088#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
44089 { "CharUpperW", (SYSCALL)CharUpperW, 0 },
44090#else
44091 { "CharUpperW", (SYSCALL)0, 0 },
44092#endif
44093
44094#define osCharUpperW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[2].pCurrent)
44095
44096 { "CloseHandle", (SYSCALL)CloseHandle, 0 },
44097
44098#define osCloseHandle ((BOOL(WINAPI*)(HANDLE))aSyscall[3].pCurrent)
44099
44100#if defined(SQLITE_WIN32_HAS_ANSI)
44101 { "CreateFileA", (SYSCALL)CreateFileA, 0 },
44102#else
44103 { "CreateFileA", (SYSCALL)0, 0 },
44104#endif
44105
44106#define osCreateFileA ((HANDLE(WINAPI*)(LPCSTR,DWORD,DWORD, \
44107 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[4].pCurrent)
44108
44109#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
44110 { "CreateFileW", (SYSCALL)CreateFileW, 0 },
44111#else
44112 { "CreateFileW", (SYSCALL)0, 0 },
44113#endif
44114
44115#define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
44116 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
44117
44118#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
44119 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0) && \
44120 SQLITE_WIN32_CREATEFILEMAPPINGA
44121 { "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 },
44122#else
44123 { "CreateFileMappingA", (SYSCALL)0, 0 },
44124#endif
44125
44126#define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
44127 DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent)
44128
44129#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
44130 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0))
44131 { "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 },
44132#else
44133 { "CreateFileMappingW", (SYSCALL)0, 0 },
44134#endif
44135
44136#define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
44137 DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent)
44138
44139#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
44140 { "CreateMutexW", (SYSCALL)CreateMutexW, 0 },
44141#else
44142 { "CreateMutexW", (SYSCALL)0, 0 },
44143#endif
44144
44145#define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \
44146 LPCWSTR))aSyscall[8].pCurrent)
44147
44148#if defined(SQLITE_WIN32_HAS_ANSI)
44149 { "DeleteFileA", (SYSCALL)DeleteFileA, 0 },
44150#else
44151 { "DeleteFileA", (SYSCALL)0, 0 },
44152#endif
44153
44154#define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent)
44155
44156#if defined(SQLITE_WIN32_HAS_WIDE)
44157 { "DeleteFileW", (SYSCALL)DeleteFileW, 0 },
44158#else
44159 { "DeleteFileW", (SYSCALL)0, 0 },
44160#endif
44161
44162#define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent)
44163
44164#if SQLITE_OS_WINCE
44165 { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 },
44166#else
44167 { "FileTimeToLocalFileTime", (SYSCALL)0, 0 },
44168#endif
44169
44170#define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \
44171 LPFILETIME))aSyscall[11].pCurrent)
44172
44173#if SQLITE_OS_WINCE
44174 { "FileTimeToSystemTime", (SYSCALL)FileTimeToSystemTime, 0 },
44175#else
44176 { "FileTimeToSystemTime", (SYSCALL)0, 0 },
44177#endif
44178
44179#define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \
44180 LPSYSTEMTIME))aSyscall[12].pCurrent)
44181
44182 { "FlushFileBuffers", (SYSCALL)FlushFileBuffers, 0 },
44183
44184#define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent)
44185
44186#if defined(SQLITE_WIN32_HAS_ANSI)
44187 { "FormatMessageA", (SYSCALL)FormatMessageA, 0 },
44188#else
44189 { "FormatMessageA", (SYSCALL)0, 0 },
44190#endif
44191
44192#define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \
44193 DWORD,va_list*))aSyscall[14].pCurrent)
44194
44195#if defined(SQLITE_WIN32_HAS_WIDE)
44196 { "FormatMessageW", (SYSCALL)FormatMessageW, 0 },
44197#else
44198 { "FormatMessageW", (SYSCALL)0, 0 },
44199#endif
44200
44201#define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \
44202 DWORD,va_list*))aSyscall[15].pCurrent)
44203
44204#if !defined(SQLITE_OMIT_LOAD_EXTENSION1)
44205 { "FreeLibrary", (SYSCALL)FreeLibrary, 0 },
44206#else
44207 { "FreeLibrary", (SYSCALL)0, 0 },
44208#endif
44209
44210#define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent)
44211
44212 { "GetCurrentProcessId", (SYSCALL)GetCurrentProcessId, 0 },
44213
44214#define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent)
44215
44216#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
44217 { "GetDiskFreeSpaceA", (SYSCALL)GetDiskFreeSpaceA, 0 },
44218#else
44219 { "GetDiskFreeSpaceA", (SYSCALL)0, 0 },
44220#endif
44221
44222#define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \
44223 LPDWORD))aSyscall[18].pCurrent)
44224
44225#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
44226 { "GetDiskFreeSpaceW", (SYSCALL)GetDiskFreeSpaceW, 0 },
44227#else
44228 { "GetDiskFreeSpaceW", (SYSCALL)0, 0 },
44229#endif
44230
44231#define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \
44232 LPDWORD))aSyscall[19].pCurrent)
44233
44234#if defined(SQLITE_WIN32_HAS_ANSI)
44235 { "GetFileAttributesA", (SYSCALL)GetFileAttributesA, 0 },
44236#else
44237 { "GetFileAttributesA", (SYSCALL)0, 0 },
44238#endif
44239
44240#define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent)
44241
44242#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
44243 { "GetFileAttributesW", (SYSCALL)GetFileAttributesW, 0 },
44244#else
44245 { "GetFileAttributesW", (SYSCALL)0, 0 },
44246#endif
44247
44248#define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent)
44249
44250#if defined(SQLITE_WIN32_HAS_WIDE)
44251 { "GetFileAttributesExW", (SYSCALL)GetFileAttributesExW, 0 },
44252#else
44253 { "GetFileAttributesExW", (SYSCALL)0, 0 },
44254#endif
44255
44256#define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \
44257 LPVOID))aSyscall[22].pCurrent)
44258
44259#if !SQLITE_OS_WINRT
44260 { "GetFileSize", (SYSCALL)GetFileSize, 0 },
44261#else
44262 { "GetFileSize", (SYSCALL)0, 0 },
44263#endif
44264
44265#define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent)
44266
44267#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
44268 { "GetFullPathNameA", (SYSCALL)GetFullPathNameA, 0 },
44269#else
44270 { "GetFullPathNameA", (SYSCALL)0, 0 },
44271#endif
44272
44273#define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \
44274 LPSTR*))aSyscall[24].pCurrent)
44275
44276#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
44277 { "GetFullPathNameW", (SYSCALL)GetFullPathNameW, 0 },
44278#else
44279 { "GetFullPathNameW", (SYSCALL)0, 0 },
44280#endif
44281
44282#define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
44283 LPWSTR*))aSyscall[25].pCurrent)
44284
44285 { "GetLastError", (SYSCALL)GetLastError, 0 },
44286
44287#define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
44288
44289#if !defined(SQLITE_OMIT_LOAD_EXTENSION1)
44290#if SQLITE_OS_WINCE
44291 /* The GetProcAddressA() routine is only available on Windows CE. */
44292 { "GetProcAddressA", (SYSCALL)GetProcAddressA, 0 },
44293#else
44294 /* All other Windows platforms expect GetProcAddress() to take
44295 ** an ANSI string regardless of the _UNICODE setting */
44296 { "GetProcAddressA", (SYSCALL)GetProcAddress, 0 },
44297#endif
44298#else
44299 { "GetProcAddressA", (SYSCALL)0, 0 },
44300#endif
44301
44302#define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \
44303 LPCSTR))aSyscall[27].pCurrent)
44304
44305#if !SQLITE_OS_WINRT
44306 { "GetSystemInfo", (SYSCALL)GetSystemInfo, 0 },
44307#else
44308 { "GetSystemInfo", (SYSCALL)0, 0 },
44309#endif
44310
44311#define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent)
44312
44313 { "GetSystemTime", (SYSCALL)GetSystemTime, 0 },
44314
44315#define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent)
44316
44317#if !SQLITE_OS_WINCE
44318 { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 },
44319#else
44320 { "GetSystemTimeAsFileTime", (SYSCALL)0, 0 },
44321#endif
44322
44323#define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \
44324 LPFILETIME))aSyscall[30].pCurrent)
44325
44326#if defined(SQLITE_WIN32_HAS_ANSI)
44327 { "GetTempPathA", (SYSCALL)GetTempPathA, 0 },
44328#else
44329 { "GetTempPathA", (SYSCALL)0, 0 },
44330#endif
44331
44332#define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent)
44333
44334#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
44335 { "GetTempPathW", (SYSCALL)GetTempPathW, 0 },
44336#else
44337 { "GetTempPathW", (SYSCALL)0, 0 },
44338#endif
44339
44340#define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent)
44341
44342#if !SQLITE_OS_WINRT
44343 { "GetTickCount", (SYSCALL)GetTickCount, 0 },
44344#else
44345 { "GetTickCount", (SYSCALL)0, 0 },
44346#endif
44347
44348#define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
44349
44350#if defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_GETVERSIONEX
44351 { "GetVersionExA", (SYSCALL)GetVersionExA, 0 },
44352#else
44353 { "GetVersionExA", (SYSCALL)0, 0 },
44354#endif
44355
44356#define osGetVersionExA ((BOOL(WINAPI*)( \
44357 LPOSVERSIONINFOA))aSyscall[34].pCurrent)
44358
44359#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
44360 SQLITE_WIN32_GETVERSIONEX
44361 { "GetVersionExW", (SYSCALL)GetVersionExW, 0 },
44362#else
44363 { "GetVersionExW", (SYSCALL)0, 0 },
44364#endif
44365
44366#define osGetVersionExW ((BOOL(WINAPI*)( \
44367 LPOSVERSIONINFOW))aSyscall[35].pCurrent)
44368
44369 { "HeapAlloc", (SYSCALL)HeapAlloc, 0 },
44370
44371#define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \
44372 SIZE_T))aSyscall[36].pCurrent)
44373
44374#if !SQLITE_OS_WINRT
44375 { "HeapCreate", (SYSCALL)HeapCreate, 0 },
44376#else
44377 { "HeapCreate", (SYSCALL)0, 0 },
44378#endif
44379
44380#define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \
44381 SIZE_T))aSyscall[37].pCurrent)
44382
44383#if !SQLITE_OS_WINRT
44384 { "HeapDestroy", (SYSCALL)HeapDestroy, 0 },
44385#else
44386 { "HeapDestroy", (SYSCALL)0, 0 },
44387#endif
44388
44389#define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[38].pCurrent)
44390
44391 { "HeapFree", (SYSCALL)HeapFree, 0 },
44392
44393#define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[39].pCurrent)
44394
44395 { "HeapReAlloc", (SYSCALL)HeapReAlloc, 0 },
44396
44397#define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \
44398 SIZE_T))aSyscall[40].pCurrent)
44399
44400 { "HeapSize", (SYSCALL)HeapSize, 0 },
44401
44402#define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \
44403 LPCVOID))aSyscall[41].pCurrent)
44404
44405#if !SQLITE_OS_WINRT
44406 { "HeapValidate", (SYSCALL)HeapValidate, 0 },
44407#else
44408 { "HeapValidate", (SYSCALL)0, 0 },
44409#endif
44410
44411#define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
44412 LPCVOID))aSyscall[42].pCurrent)
44413
44414#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
44415 { "HeapCompact", (SYSCALL)HeapCompact, 0 },
44416#else
44417 { "HeapCompact", (SYSCALL)0, 0 },
44418#endif
44419
44420#define osHeapCompact ((UINT(WINAPI*)(HANDLE,DWORD))aSyscall[43].pCurrent)
44421
44422#if defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_OMIT_LOAD_EXTENSION1)
44423 { "LoadLibraryA", (SYSCALL)LoadLibraryA, 0 },
44424#else
44425 { "LoadLibraryA", (SYSCALL)0, 0 },
44426#endif
44427
44428#define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[44].pCurrent)
44429
44430#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
44431 !defined(SQLITE_OMIT_LOAD_EXTENSION1)
44432 { "LoadLibraryW", (SYSCALL)LoadLibraryW, 0 },
44433#else
44434 { "LoadLibraryW", (SYSCALL)0, 0 },
44435#endif
44436
44437#define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[45].pCurrent)
44438
44439#if !SQLITE_OS_WINRT
44440 { "LocalFree", (SYSCALL)LocalFree, 0 },
44441#else
44442 { "LocalFree", (SYSCALL)0, 0 },
44443#endif
44444
44445#define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[46].pCurrent)
44446
44447#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
44448 { "LockFile", (SYSCALL)LockFile, 0 },
44449#else
44450 { "LockFile", (SYSCALL)0, 0 },
44451#endif
44452
44453#ifndef osLockFile
44454#define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
44455 DWORD))aSyscall[47].pCurrent)
44456#endif
44457
44458#if !SQLITE_OS_WINCE
44459 { "LockFileEx", (SYSCALL)LockFileEx, 0 },
44460#else
44461 { "LockFileEx", (SYSCALL)0, 0 },
44462#endif
44463
44464#ifndef osLockFileEx
44465#define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
44466 LPOVERLAPPED))aSyscall[48].pCurrent)
44467#endif
44468
44469#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && \
44470 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0))
44471 { "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 },
44472#else
44473 { "MapViewOfFile", (SYSCALL)0, 0 },
44474#endif
44475
44476#define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
44477 SIZE_T))aSyscall[49].pCurrent)
44478
44479 { "MultiByteToWideChar", (SYSCALL)MultiByteToWideChar, 0 },
44480
44481#define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
44482 int))aSyscall[50].pCurrent)
44483
44484 { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
44485
44486#define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
44487 LARGE_INTEGER*))aSyscall[51].pCurrent)
44488
44489 { "ReadFile", (SYSCALL)ReadFile, 0 },
44490
44491#define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
44492 LPOVERLAPPED))aSyscall[52].pCurrent)
44493
44494 { "SetEndOfFile", (SYSCALL)SetEndOfFile, 0 },
44495
44496#define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[53].pCurrent)
44497
44498#if !SQLITE_OS_WINRT
44499 { "SetFilePointer", (SYSCALL)SetFilePointer, 0 },
44500#else
44501 { "SetFilePointer", (SYSCALL)0, 0 },
44502#endif
44503
44504#define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
44505 DWORD))aSyscall[54].pCurrent)
44506
44507#if !SQLITE_OS_WINRT
44508 { "Sleep", (SYSCALL)Sleep, 0 },
44509#else
44510 { "Sleep", (SYSCALL)0, 0 },
44511#endif
44512
44513#define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[55].pCurrent)
44514
44515 { "SystemTimeToFileTime", (SYSCALL)SystemTimeToFileTime, 0 },
44516
44517#define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
44518 LPFILETIME))aSyscall[56].pCurrent)
44519
44520#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
44521 { "UnlockFile", (SYSCALL)UnlockFile, 0 },
44522#else
44523 { "UnlockFile", (SYSCALL)0, 0 },
44524#endif
44525
44526#ifndef osUnlockFile
44527#define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
44528 DWORD))aSyscall[57].pCurrent)
44529#endif
44530
44531#if !SQLITE_OS_WINCE
44532 { "UnlockFileEx", (SYSCALL)UnlockFileEx, 0 },
44533#else
44534 { "UnlockFileEx", (SYSCALL)0, 0 },
44535#endif
44536
44537#define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
44538 LPOVERLAPPED))aSyscall[58].pCurrent)
44539
44540#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0
44541 { "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 },
44542#else
44543 { "UnmapViewOfFile", (SYSCALL)0, 0 },
44544#endif
44545
44546#define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[59].pCurrent)
44547
44548 { "WideCharToMultiByte", (SYSCALL)WideCharToMultiByte, 0 },
44549
44550#define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
44551 LPCSTR,LPBOOL))aSyscall[60].pCurrent)
44552
44553 { "WriteFile", (SYSCALL)WriteFile, 0 },
44554
44555#define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
44556 LPOVERLAPPED))aSyscall[61].pCurrent)
44557
44558#if SQLITE_OS_WINRT
44559 { "CreateEventExW", (SYSCALL)CreateEventExW, 0 },
44560#else
44561 { "CreateEventExW", (SYSCALL)0, 0 },
44562#endif
44563
44564#define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
44565 DWORD,DWORD))aSyscall[62].pCurrent)
44566
44567#if !SQLITE_OS_WINRT
44568 { "WaitForSingleObject", (SYSCALL)WaitForSingleObject, 0 },
44569#else
44570 { "WaitForSingleObject", (SYSCALL)0, 0 },
44571#endif
44572
44573#define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
44574 DWORD))aSyscall[63].pCurrent)
44575
44576#if !SQLITE_OS_WINCE
44577 { "WaitForSingleObjectEx", (SYSCALL)WaitForSingleObjectEx, 0 },
44578#else
44579 { "WaitForSingleObjectEx", (SYSCALL)0, 0 },
44580#endif
44581
44582#define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \
44583 BOOL))aSyscall[64].pCurrent)
44584
44585#if SQLITE_OS_WINRT
44586 { "SetFilePointerEx", (SYSCALL)SetFilePointerEx, 0 },
44587#else
44588 { "SetFilePointerEx", (SYSCALL)0, 0 },
44589#endif
44590
44591#define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \
44592 PLARGE_INTEGER,DWORD))aSyscall[65].pCurrent)
44593
44594#if SQLITE_OS_WINRT
44595 { "GetFileInformationByHandleEx", (SYSCALL)GetFileInformationByHandleEx, 0 },
44596#else
44597 { "GetFileInformationByHandleEx", (SYSCALL)0, 0 },
44598#endif
44599
44600#define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
44601 FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent)
44602
44603#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0)
44604 { "MapViewOfFileFromApp", (SYSCALL)MapViewOfFileFromApp, 0 },
44605#else
44606 { "MapViewOfFileFromApp", (SYSCALL)0, 0 },
44607#endif
44608
44609#define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \
44610 SIZE_T))aSyscall[67].pCurrent)
44611
44612#if SQLITE_OS_WINRT
44613 { "CreateFile2", (SYSCALL)CreateFile2, 0 },
44614#else
44615 { "CreateFile2", (SYSCALL)0, 0 },
44616#endif
44617
44618#define osCreateFile2 ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD,DWORD, \
44619 LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[68].pCurrent)
44620
44621#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_LOAD_EXTENSION1)
44622 { "LoadPackagedLibrary", (SYSCALL)LoadPackagedLibrary, 0 },
44623#else
44624 { "LoadPackagedLibrary", (SYSCALL)0, 0 },
44625#endif
44626
44627#define osLoadPackagedLibrary ((HMODULE(WINAPI*)(LPCWSTR, \
44628 DWORD))aSyscall[69].pCurrent)
44629
44630#if SQLITE_OS_WINRT
44631 { "GetTickCount64", (SYSCALL)GetTickCount64, 0 },
44632#else
44633 { "GetTickCount64", (SYSCALL)0, 0 },
44634#endif
44635
44636#define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[70].pCurrent)
44637
44638#if SQLITE_OS_WINRT
44639 { "GetNativeSystemInfo", (SYSCALL)GetNativeSystemInfo, 0 },
44640#else
44641 { "GetNativeSystemInfo", (SYSCALL)0, 0 },
44642#endif
44643
44644#define osGetNativeSystemInfo ((VOID(WINAPI*)( \
44645 LPSYSTEM_INFO))aSyscall[71].pCurrent)
44646
44647#if defined(SQLITE_WIN32_HAS_ANSI)
44648 { "OutputDebugStringA", (SYSCALL)OutputDebugStringA, 0 },
44649#else
44650 { "OutputDebugStringA", (SYSCALL)0, 0 },
44651#endif
44652
44653#define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[72].pCurrent)
44654
44655#if defined(SQLITE_WIN32_HAS_WIDE)
44656 { "OutputDebugStringW", (SYSCALL)OutputDebugStringW, 0 },
44657#else
44658 { "OutputDebugStringW", (SYSCALL)0, 0 },
44659#endif
44660
44661#define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[73].pCurrent)
44662
44663 { "GetProcessHeap", (SYSCALL)GetProcessHeap, 0 },
44664
44665#define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent)
44666
44667#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0)
44668 { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
44669#else
44670 { "CreateFileMappingFromApp", (SYSCALL)0, 0 },
44671#endif
44672
44673#define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
44674 LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)
44675
44676/*
44677** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
44678** is really just a macro that uses a compiler intrinsic (e.g. x64).
44679** So do not try to make this is into a redefinable interface.
44680*/
44681#if defined(InterlockedCompareExchange)
44682 { "InterlockedCompareExchange", (SYSCALL)0, 0 },
44683
44684#define osInterlockedCompareExchange InterlockedCompareExchange
44685#else
44686 { "InterlockedCompareExchange", (SYSCALL)InterlockedCompareExchange, 0 },
44687
44688#define osInterlockedCompareExchange ((LONG(WINAPI*)(LONG \
44689 SQLITE_WIN32_VOLATILE*, LONG,LONG))aSyscall[76].pCurrent)
44690#endif /* defined(InterlockedCompareExchange) */
44691
44692#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
44693 { "UuidCreate", (SYSCALL)UuidCreate, 0 },
44694#else
44695 { "UuidCreate", (SYSCALL)0, 0 },
44696#endif
44697
44698#define osUuidCreate ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[77].pCurrent)
44699
44700#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
44701 { "UuidCreateSequential", (SYSCALL)UuidCreateSequential, 0 },
44702#else
44703 { "UuidCreateSequential", (SYSCALL)0, 0 },
44704#endif
44705
44706#define osUuidCreateSequential \
44707 ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[78].pCurrent)
44708
44709#if !defined(SQLITE_NO_SYNC) && SQLITE_MAX_MMAP_SIZE20971520>0
44710 { "FlushViewOfFile", (SYSCALL)FlushViewOfFile, 0 },
44711#else
44712 { "FlushViewOfFile", (SYSCALL)0, 0 },
44713#endif
44714
44715#define osFlushViewOfFile \
44716 ((BOOL(WINAPI*)(LPCVOID,SIZE_T))aSyscall[79].pCurrent)
44717
44718}; /* End of the overrideable system calls */
44719
44720/*
44721** This is the xSetSystemCall() method of sqlite3_vfs for all of the
44722** "win32" VFSes. Return SQLITE_OK opon successfully updating the
44723** system call pointer, or SQLITE_NOTFOUND if there is no configurable
44724** system call named zName.
44725*/
44726static int winSetSystemCall(
44727 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
44728 const char *zName, /* Name of system call to override */
44729 sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
44730){
44731 unsigned int i;
44732 int rc = SQLITE_NOTFOUND12;
44733
44734 UNUSED_PARAMETER(pNotUsed)(void)(pNotUsed);
44735 if( zName==0 ){
44736 /* If no zName is given, restore all system calls to their default
44737 ** settings and return NULL
44738 */
44739 rc = SQLITE_OK0;
44740 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
44741 if( aSyscall[i].pDefault ){
44742 aSyscall[i].pCurrent = aSyscall[i].pDefault;
44743 }
44744 }
44745 }else{
44746 /* If zName is specified, operate on only the one system call
44747 ** specified.
44748 */
44749 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
44750 if( strcmp(zName, aSyscall[i].zName)==0 ){
44751 if( aSyscall[i].pDefault==0 ){
44752 aSyscall[i].pDefault = aSyscall[i].pCurrent;
44753 }
44754 rc = SQLITE_OK0;
44755 if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
44756 aSyscall[i].pCurrent = pNewFunc;
44757 break;
44758 }
44759 }
44760 }
44761 return rc;
44762}
44763
44764/*
44765** Return the value of a system call. Return NULL if zName is not a
44766** recognized system call name. NULL is also returned if the system call
44767** is currently undefined.
44768*/
44769static sqlite3_syscall_ptr winGetSystemCall(
44770 sqlite3_vfs *pNotUsed,
44771 const char *zName
44772){
44773 unsigned int i;
44774
44775 UNUSED_PARAMETER(pNotUsed)(void)(pNotUsed);
44776 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
44777 if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
44778 }
44779 return 0;
44780}
44781
44782/*
44783** Return the name of the first system call after zName. If zName==NULL
44784** then return the name of the first system call. Return NULL if zName
44785** is the last system call or if zName is not the name of a valid
44786** system call.
44787*/
44788static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){
44789 int i = -1;
44790
44791 UNUSED_PARAMETER(p)(void)(p);
44792 if( zName ){
44793 for(i=0; i<ArraySize(aSyscall)((int)(sizeof(aSyscall)/sizeof(aSyscall[0])))-1; i++){
44794 if( strcmp(zName, aSyscall[i].zName)==0 ) break;
44795 }
44796 }
44797 for(i++; i<ArraySize(aSyscall)((int)(sizeof(aSyscall)/sizeof(aSyscall[0]))); i++){
44798 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
44799 }
44800 return 0;
44801}
44802
44803#ifdef SQLITE_WIN32_MALLOC
44804/*
44805** If a Win32 native heap has been configured, this function will attempt to
44806** compact it. Upon success, SQLITE_OK will be returned. Upon failure, one
44807** of SQLITE_NOMEM, SQLITE_ERROR, or SQLITE_NOTFOUND will be returned. The
44808** "pnLargest" argument, if non-zero, will be used to return the size of the
44809** largest committed free block in the heap, in bytes.
44810*/
44811SQLITE_API int sqlite3_win32_compact_heap(LPUINT pnLargest){
44812 int rc = SQLITE_OK0;
44813 UINT nLargest = 0;
44814 HANDLE hHeap;
44815
44816 winMemAssertMagic();
44817 hHeap = winMemGetHeap();
44818 assert( hHeap!=0 )((void) (0));
44819 assert( hHeap!=INVALID_HANDLE_VALUE )((void) (0));
44820#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
44821 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) )((void) (0));
44822#endif
44823#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
44824 if( (nLargest=osHeapCompact(hHeap, SQLITE_WIN32_HEAP_FLAGS))==0 ){
44825 DWORD lastErrno = osGetLastError();
44826 if( lastErrno==NO_ERROR ){
44827 sqlite3_log(SQLITE_NOMEM7, "failed to HeapCompact (no space), heap=%p",
44828 (void*)hHeap);
44829 rc = SQLITE_NOMEM_BKPT7;
44830 }else{
44831 sqlite3_log(SQLITE_ERROR1, "failed to HeapCompact (%lu), heap=%p",
44832 osGetLastError(), (void*)hHeap);
44833 rc = SQLITE_ERROR1;
44834 }
44835 }
44836#else
44837 sqlite3_log(SQLITE_NOTFOUND12, "failed to HeapCompact, heap=%p",
44838 (void*)hHeap);
44839 rc = SQLITE_NOTFOUND12;
44840#endif
44841 if( pnLargest ) *pnLargest = nLargest;
44842 return rc;
44843}
44844
44845/*
44846** If a Win32 native heap has been configured, this function will attempt to
44847** destroy and recreate it. If the Win32 native heap is not isolated and/or
44848** the sqlite3_memory_used() function does not return zero, SQLITE_BUSY will
44849** be returned and no changes will be made to the Win32 native heap.
44850*/
44851SQLITE_API int sqlite3_win32_reset_heap(){
44852 int rc;
44853 MUTEX_LOGIC( sqlite3_mutex *pMainMtx; )sqlite3_mutex *pMainMtx; /* The main static mutex */
44854 MUTEX_LOGIC( sqlite3_mutex *pMem; )sqlite3_mutex *pMem; /* The memsys static mutex */
44855 MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )pMainMtx = sqlite3MutexAlloc(2);
44856 MUTEX_LOGIC( pMem = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); )pMem = sqlite3MutexAlloc(3);
44857 sqlite3_mutex_enter(pMainMtx);
44858 sqlite3_mutex_enter(pMem);
44859 winMemAssertMagic();
44860 if( winMemGetHeap()!=NULL((void*)0) && winMemGetOwned() && sqlite3_memory_used()==0 ){
44861 /*
44862 ** At this point, there should be no outstanding memory allocations on
44863 ** the heap. Also, since both the main and memsys locks are currently
44864 ** being held by us, no other function (i.e. from another thread) should
44865 ** be able to even access the heap. Attempt to destroy and recreate our
44866 ** isolated Win32 native heap now.
44867 */
44868 assert( winMemGetHeap()!=NULL )((void) (0));
44869 assert( winMemGetOwned() )((void) (0));
44870 assert( sqlite3_memory_used()==0 )((void) (0));
44871 winMemShutdown(winMemGetDataPtr());
44872 assert( winMemGetHeap()==NULL )((void) (0));
44873 assert( !winMemGetOwned() )((void) (0));
44874 assert( sqlite3_memory_used()==0 )((void) (0));
44875 rc = winMemInit(winMemGetDataPtr());
44876 assert( rc!=SQLITE_OK || winMemGetHeap()!=NULL )((void) (0));
44877 assert( rc!=SQLITE_OK || winMemGetOwned() )((void) (0));
44878 assert( rc!=SQLITE_OK || sqlite3_memory_used()==0 )((void) (0));
44879 }else{
44880 /*
44881 ** The Win32 native heap cannot be modified because it may be in use.
44882 */
44883 rc = SQLITE_BUSY5;
44884 }
44885 sqlite3_mutex_leave(pMem);
44886 sqlite3_mutex_leave(pMainMtx);
44887 return rc;
44888}
44889#endif /* SQLITE_WIN32_MALLOC */
44890
44891/*
44892** This function outputs the specified (ANSI) string to the Win32 debugger
44893** (if available).
44894*/
44895
44896SQLITE_API void sqlite3_win32_write_debug(const char *zBuf, int nBuf){
44897 char zDbgBuf[SQLITE_WIN32_DBG_BUF_SIZE];
44898 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. */
44899 if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
44900 assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE )((void) (0));
44901#ifdef SQLITE_ENABLE_API_ARMOR1
44902 if( !zBuf ){
44903 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(44903);
44904 return;
44905 }
44906#endif
44907#if defined(SQLITE_WIN32_HAS_ANSI)
44908 if( nMin>0 ){
44909 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
44910 memcpy(zDbgBuf, zBuf, nMin);
44911 osOutputDebugStringA(zDbgBuf);
44912 }else{
44913 osOutputDebugStringA(zBuf);
44914 }
44915#elif defined(SQLITE_WIN32_HAS_WIDE)
44916 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
44917 if ( osMultiByteToWideChar(
44918 osAreFileApisANSI() ? CP_ACP : CP_OEMCP, 0, zBuf,
44919 nMin, (LPWSTR)zDbgBuf, SQLITE_WIN32_DBG_BUF_SIZE/sizeof(WCHAR))<=0 ){
44920 return;
44921 }
44922 osOutputDebugStringW((LPCWSTR)zDbgBuf);
44923#else
44924 if( nMin>0 ){
44925 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
44926 memcpy(zDbgBuf, zBuf, nMin);
44927 fprintf(stderrstderr, "%s", zDbgBuf);
44928 }else{
44929 fprintf(stderrstderr, "%s", zBuf);
44930 }
44931#endif
44932}
44933
44934/*
44935** The following routine suspends the current thread for at least ms
44936** milliseconds. This is equivalent to the Win32 Sleep() interface.
44937*/
44938#if SQLITE_OS_WINRT
44939static HANDLE sleepObj = NULL((void*)0);
44940#endif
44941
44942SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds){
44943#if SQLITE_OS_WINRT
44944 if ( sleepObj==NULL((void*)0) ){
44945 sleepObj = osCreateEventExW(NULL((void*)0), NULL((void*)0), CREATE_EVENT_MANUAL_RESET,
44946 SYNCHRONIZE);
44947 }
44948 assert( sleepObj!=NULL )((void) (0));
44949 osWaitForSingleObjectEx(sleepObj, milliseconds, FALSE);
44950#else
44951 osSleep(milliseconds);
44952#endif
44953}
44954
44955#if SQLITE_MAX_WORKER_THREADS8>0 && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
44956 SQLITE_THREADSAFE2>0
44957SQLITE_PRIVATEstatic DWORD sqlite3Win32Wait(HANDLE hObject){
44958 DWORD rc;
44959 while( (rc = osWaitForSingleObjectEx(hObject, INFINITE,
44960 TRUE))==WAIT_IO_COMPLETION ){}
44961 return rc;
44962}
44963#endif
44964
44965/*
44966** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
44967** or WinCE. Return false (zero) for Win95, Win98, or WinME.
44968**
44969** Here is an interesting observation: Win95, Win98, and WinME lack
44970** the LockFileEx() API. But we can still statically link against that
44971** API as long as we don't call it when running Win95/98/ME. A call to
44972** this routine is used to determine if the host is Win95/98/ME or
44973** WinNT/2K/XP so that we will know whether or not we can safely call
44974** the LockFileEx() API.
44975*/
44976
44977#if !SQLITE_WIN32_GETVERSIONEX
44978# define osIsNT() (1)
44979#elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
44980# define osIsNT() (1)
44981#elif !defined(SQLITE_WIN32_HAS_WIDE)
44982# define osIsNT() (0)
44983#else
44984# define osIsNT() ((sqlite3_os_type==2) || sqlite3_win32_is_nt())
44985#endif
44986
44987/*
44988** This function determines if the machine is running a version of Windows
44989** based on the NT kernel.
44990*/
44991SQLITE_API int sqlite3_win32_is_nt(void){
44992#if SQLITE_OS_WINRT
44993 /*
44994 ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
44995 ** kernel.
44996 */
44997 return 1;
44998#elif SQLITE_WIN32_GETVERSIONEX
44999 if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){
45000#if defined(SQLITE_WIN32_HAS_ANSI)
45001 OSVERSIONINFOA sInfo;
45002 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
45003 osGetVersionExA(&sInfo);
45004 osInterlockedCompareExchange(&sqlite3_os_type,
45005 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
45006#elif defined(SQLITE_WIN32_HAS_WIDE)
45007 OSVERSIONINFOW sInfo;
45008 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
45009 osGetVersionExW(&sInfo);
45010 osInterlockedCompareExchange(&sqlite3_os_type,
45011 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
45012#endif
45013 }
45014 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
45015#elif SQLITE_TEST
45016 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
45017#else
45018 /*
45019 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
45020 ** deprecated are always assumed to be based on the NT kernel.
45021 */
45022 return 1;
45023#endif
45024}
45025
45026#ifdef SQLITE_WIN32_MALLOC
45027/*
45028** Allocate nBytes of memory.
45029*/
45030static void *winMemMalloc(int nBytes){
45031 HANDLE hHeap;
45032 void *p;
45033
45034 winMemAssertMagic();
45035 hHeap = winMemGetHeap();
45036 assert( hHeap!=0 )((void) (0));
45037 assert( hHeap!=INVALID_HANDLE_VALUE )((void) (0));
45038#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
45039 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) )((void) (0));
45040#endif
45041 assert( nBytes>=0 )((void) (0));
45042 p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
45043 if( !p ){
45044 sqlite3_log(SQLITE_NOMEM7, "failed to HeapAlloc %u bytes (%lu), heap=%p",
45045 nBytes, osGetLastError(), (void*)hHeap);
45046 }
45047 return p;
45048}
45049
45050/*
45051** Free memory.
45052*/
45053static void winMemFree(void *pPrior){
45054 HANDLE hHeap;
45055
45056 winMemAssertMagic();
45057 hHeap = winMemGetHeap();
45058 assert( hHeap!=0 )((void) (0));
45059 assert( hHeap!=INVALID_HANDLE_VALUE )((void) (0));
45060#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
45061 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) )((void) (0));
45062#endif
45063 if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
45064 if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
45065 sqlite3_log(SQLITE_NOMEM7, "failed to HeapFree block %p (%lu), heap=%p",
45066 pPrior, osGetLastError(), (void*)hHeap);
45067 }
45068}
45069
45070/*
45071** Change the size of an existing memory allocation
45072*/
45073static void *winMemRealloc(void *pPrior, int nBytes){
45074 HANDLE hHeap;
45075 void *p;
45076
45077 winMemAssertMagic();
45078 hHeap = winMemGetHeap();
45079 assert( hHeap!=0 )((void) (0));
45080 assert( hHeap!=INVALID_HANDLE_VALUE )((void) (0));
45081#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
45082 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) )((void) (0));
45083#endif
45084 assert( nBytes>=0 )((void) (0));
45085 if( !pPrior ){
45086 p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
45087 }else{
45088 p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
45089 }
45090 if( !p ){
45091 sqlite3_log(SQLITE_NOMEM7, "failed to %s %u bytes (%lu), heap=%p",
45092 pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, osGetLastError(),
45093 (void*)hHeap);
45094 }
45095 return p;
45096}
45097
45098/*
45099** Return the size of an outstanding allocation, in bytes.
45100*/
45101static int winMemSize(void *p){
45102 HANDLE hHeap;
45103 SIZE_T n;
45104
45105 winMemAssertMagic();
45106 hHeap = winMemGetHeap();
45107 assert( hHeap!=0 )((void) (0));
45108 assert( hHeap!=INVALID_HANDLE_VALUE )((void) (0));
45109#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
45110 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, p) )((void) (0));
45111#endif
45112 if( !p ) return 0;
45113 n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
45114 if( n==(SIZE_T)-1 ){
45115 sqlite3_log(SQLITE_NOMEM7, "failed to HeapSize block %p (%lu), heap=%p",
45116 p, osGetLastError(), (void*)hHeap);
45117 return 0;
45118 }
45119 return (int)n;
45120}
45121
45122/*
45123** Round up a request size to the next valid allocation size.
45124*/
45125static int winMemRoundup(int n){
45126 return n;
45127}
45128
45129/*
45130** Initialize this module.
45131*/
45132static int winMemInit(void *pAppData){
45133 winMemData *pWinMemData = (winMemData *)pAppData;
45134
45135 if( !pWinMemData ) return SQLITE_ERROR1;
45136 assert( pWinMemData->magic1==WINMEM_MAGIC1 )((void) (0));
45137 assert( pWinMemData->magic2==WINMEM_MAGIC2 )((void) (0));
45138
45139#if !SQLITE_OS_WINRT && SQLITE_WIN32_HEAP_CREATE
45140 if( !pWinMemData->hHeap ){
45141 DWORD dwInitialSize = SQLITE_WIN32_HEAP_INIT_SIZE;
45142 DWORD dwMaximumSize = (DWORD)sqlite3GlobalConfigsqlite3Config.nHeap;
45143 if( dwMaximumSize==0 ){
45144 dwMaximumSize = SQLITE_WIN32_HEAP_MAX_SIZE;
45145 }else if( dwInitialSize>dwMaximumSize ){
45146 dwInitialSize = dwMaximumSize;
45147 }
45148 pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
45149 dwInitialSize, dwMaximumSize);
45150 if( !pWinMemData->hHeap ){
45151 sqlite3_log(SQLITE_NOMEM7,
45152 "failed to HeapCreate (%lu), flags=%u, initSize=%lu, maxSize=%lu",
45153 osGetLastError(), SQLITE_WIN32_HEAP_FLAGS, dwInitialSize,
45154 dwMaximumSize);
45155 return SQLITE_NOMEM_BKPT7;
45156 }
45157 pWinMemData->bOwned = TRUE;
45158 assert( pWinMemData->bOwned )((void) (0));
45159 }
45160#else
45161 pWinMemData->hHeap = osGetProcessHeap();
45162 if( !pWinMemData->hHeap ){
45163 sqlite3_log(SQLITE_NOMEM7,
45164 "failed to GetProcessHeap (%lu)", osGetLastError());
45165 return SQLITE_NOMEM_BKPT7;
45166 }
45167 pWinMemData->bOwned = FALSE;
45168 assert( !pWinMemData->bOwned )((void) (0));
45169#endif
45170 assert( pWinMemData->hHeap!=0 )((void) (0));
45171 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE )((void) (0));
45172#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
45173 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) )((void) (0));
45174#endif
45175 return SQLITE_OK0;
45176}
45177
45178/*
45179** Deinitialize this module.
45180*/
45181static void winMemShutdown(void *pAppData){
45182 winMemData *pWinMemData = (winMemData *)pAppData;
45183
45184 if( !pWinMemData ) return;
45185 assert( pWinMemData->magic1==WINMEM_MAGIC1 )((void) (0));
45186 assert( pWinMemData->magic2==WINMEM_MAGIC2 )((void) (0));
45187
45188 if( pWinMemData->hHeap ){
45189 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE )((void) (0));
45190#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
45191 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) )((void) (0));
45192#endif
45193 if( pWinMemData->bOwned ){
45194 if( !osHeapDestroy(pWinMemData->hHeap) ){
45195 sqlite3_log(SQLITE_NOMEM7, "failed to HeapDestroy (%lu), heap=%p",
45196 osGetLastError(), (void*)pWinMemData->hHeap);
45197 }
45198 pWinMemData->bOwned = FALSE;
45199 }
45200 pWinMemData->hHeap = NULL((void*)0);
45201 }
45202}
45203
45204/*
45205** Populate the low-level memory allocation function pointers in
45206** sqlite3GlobalConfig.m with pointers to the routines in this file. The
45207** arguments specify the block of memory to manage.
45208**
45209** This routine is only called by sqlite3_config(), and therefore
45210** is not required to be threadsafe (it is not).
45211*/
45212SQLITE_PRIVATEstatic const sqlite3_mem_methods *sqlite3MemGetWin32(void){
45213 static const sqlite3_mem_methods winMemMethods = {
45214 winMemMalloc,
45215 winMemFree,
45216 winMemRealloc,
45217 winMemSize,
45218 winMemRoundup,
45219 winMemInit,
45220 winMemShutdown,
45221 &win_mem_data
45222 };
45223 return &winMemMethods;
45224}
45225
45226SQLITE_PRIVATEstatic void sqlite3MemSetDefault(void){
45227 sqlite3_config(SQLITE_CONFIG_MALLOC4, sqlite3MemGetWin32());
45228}
45229#endif /* SQLITE_WIN32_MALLOC */
45230
45231/*
45232** Convert a UTF-8 string to Microsoft Unicode.
45233**
45234** Space to hold the returned string is obtained from sqlite3_malloc().
45235*/
45236static LPWSTR winUtf8ToUnicode(const char *zText){
45237 int nChar;
45238 LPWSTR zWideText;
45239
45240 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL((void*)0), 0);
45241 if( nChar==0 ){
45242 return 0;
45243 }
45244 zWideText = sqlite3MallocZero( nChar*sizeof(WCHAR) );
45245 if( zWideText==0 ){
45246 return 0;
45247 }
45248 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
45249 nChar);
45250 if( nChar==0 ){
45251 sqlite3_free(zWideText);
45252 zWideText = 0;
45253 }
45254 return zWideText;
45255}
45256
45257/*
45258** Convert a Microsoft Unicode string to UTF-8.
45259**
45260** Space to hold the returned string is obtained from sqlite3_malloc().
45261*/
45262static char *winUnicodeToUtf8(LPCWSTR zWideText){
45263 int nByte;
45264 char *zText;
45265
45266 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
45267 if( nByte == 0 ){
45268 return 0;
45269 }
45270 zText = sqlite3MallocZero( nByte );
45271 if( zText==0 ){
45272 return 0;
45273 }
45274 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
45275 0, 0);
45276 if( nByte == 0 ){
45277 sqlite3_free(zText);
45278 zText = 0;
45279 }
45280 return zText;
45281}
45282
45283/*
45284** Convert an ANSI string to Microsoft Unicode, using the ANSI or OEM
45285** code page.
45286**
45287** Space to hold the returned string is obtained from sqlite3_malloc().
45288*/
45289static LPWSTR winMbcsToUnicode(const char *zText, int useAnsi){
45290 int nByte;
45291 LPWSTR zMbcsText;
45292 int codepage = useAnsi ? CP_ACP : CP_OEMCP;
45293
45294 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL((void*)0),
45295 0)*sizeof(WCHAR);
45296 if( nByte==0 ){
45297 return 0;
45298 }
45299 zMbcsText = sqlite3MallocZero( nByte*sizeof(WCHAR) );
45300 if( zMbcsText==0 ){
45301 return 0;
45302 }
45303 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
45304 nByte);
45305 if( nByte==0 ){
45306 sqlite3_free(zMbcsText);
45307 zMbcsText = 0;
45308 }
45309 return zMbcsText;
45310}
45311
45312/*
45313** Convert a Microsoft Unicode string to a multi-byte character string,
45314** using the ANSI or OEM code page.
45315**
45316** Space to hold the returned string is obtained from sqlite3_malloc().
45317*/
45318static char *winUnicodeToMbcs(LPCWSTR zWideText, int useAnsi){
45319 int nByte;
45320 char *zText;
45321 int codepage = useAnsi ? CP_ACP : CP_OEMCP;
45322
45323 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
45324 if( nByte == 0 ){
45325 return 0;
45326 }
45327 zText = sqlite3MallocZero( nByte );
45328 if( zText==0 ){
45329 return 0;
45330 }
45331 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
45332 nByte, 0, 0);
45333 if( nByte == 0 ){
45334 sqlite3_free(zText);
45335 zText = 0;
45336 }
45337 return zText;
45338}
45339
45340/*
45341** Convert a multi-byte character string to UTF-8.
45342**
45343** Space to hold the returned string is obtained from sqlite3_malloc().
45344*/
45345static char *winMbcsToUtf8(const char *zText, int useAnsi){
45346 char *zTextUtf8;
45347 LPWSTR zTmpWide;
45348
45349 zTmpWide = winMbcsToUnicode(zText, useAnsi);
45350 if( zTmpWide==0 ){
45351 return 0;
45352 }
45353 zTextUtf8 = winUnicodeToUtf8(zTmpWide);
45354 sqlite3_free(zTmpWide);
45355 return zTextUtf8;
45356}
45357
45358/*
45359** Convert a UTF-8 string to a multi-byte character string.
45360**
45361** Space to hold the returned string is obtained from sqlite3_malloc().
45362*/
45363static char *winUtf8ToMbcs(const char *zText, int useAnsi){
45364 char *zTextMbcs;
45365 LPWSTR zTmpWide;
45366
45367 zTmpWide = winUtf8ToUnicode(zText);
45368 if( zTmpWide==0 ){
45369 return 0;
45370 }
45371 zTextMbcs = winUnicodeToMbcs(zTmpWide, useAnsi);
45372 sqlite3_free(zTmpWide);
45373 return zTextMbcs;
45374}
45375
45376/*
45377** This is a public wrapper for the winUtf8ToUnicode() function.
45378*/
45379SQLITE_API LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText){
45380#ifdef SQLITE_ENABLE_API_ARMOR1
45381 if( !zText ){
45382 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(45382);
45383 return 0;
45384 }
45385#endif
45386#ifndef SQLITE_OMIT_AUTOINIT
45387 if( sqlite3_initialize() ) return 0;
45388#endif
45389 return winUtf8ToUnicode(zText);
45390}
45391
45392/*
45393** This is a public wrapper for the winUnicodeToUtf8() function.
45394*/
45395SQLITE_API char *sqlite3_win32_unicode_to_utf8(LPCWSTR zWideText){
45396#ifdef SQLITE_ENABLE_API_ARMOR1
45397 if( !zWideText ){
45398 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(45398);
45399 return 0;
45400 }
45401#endif
45402#ifndef SQLITE_OMIT_AUTOINIT
45403 if( sqlite3_initialize() ) return 0;
45404#endif
45405 return winUnicodeToUtf8(zWideText);
45406}
45407
45408/*
45409** This is a public wrapper for the winMbcsToUtf8() function.
45410*/
45411SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zText){
45412#ifdef SQLITE_ENABLE_API_ARMOR1
45413 if( !zText ){
45414 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(45414);
45415 return 0;
45416 }
45417#endif
45418#ifndef SQLITE_OMIT_AUTOINIT
45419 if( sqlite3_initialize() ) return 0;
45420#endif
45421 return winMbcsToUtf8(zText, osAreFileApisANSI());
45422}
45423
45424/*
45425** This is a public wrapper for the winMbcsToUtf8() function.
45426*/
45427SQLITE_API char *sqlite3_win32_mbcs_to_utf8_v2(const char *zText, int useAnsi){
45428#ifdef SQLITE_ENABLE_API_ARMOR1
45429 if( !zText ){
45430 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(45430);
45431 return 0;
45432 }
45433#endif
45434#ifndef SQLITE_OMIT_AUTOINIT
45435 if( sqlite3_initialize() ) return 0;
45436#endif
45437 return winMbcsToUtf8(zText, useAnsi);
45438}
45439
45440/*
45441** This is a public wrapper for the winUtf8ToMbcs() function.
45442*/
45443SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zText){
45444#ifdef SQLITE_ENABLE_API_ARMOR1
45445 if( !zText ){
45446 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(45446);
45447 return 0;
45448 }
45449#endif
45450#ifndef SQLITE_OMIT_AUTOINIT
45451 if( sqlite3_initialize() ) return 0;
45452#endif
45453 return winUtf8ToMbcs(zText, osAreFileApisANSI());
45454}
45455
45456/*
45457** This is a public wrapper for the winUtf8ToMbcs() function.
45458*/
45459SQLITE_API char *sqlite3_win32_utf8_to_mbcs_v2(const char *zText, int useAnsi){
45460#ifdef SQLITE_ENABLE_API_ARMOR1
45461 if( !zText ){
45462 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(45462);
45463 return 0;
45464 }
45465#endif
45466#ifndef SQLITE_OMIT_AUTOINIT
45467 if( sqlite3_initialize() ) return 0;
45468#endif
45469 return winUtf8ToMbcs(zText, useAnsi);
45470}
45471
45472/*
45473** This function is the same as sqlite3_win32_set_directory (below); however,
45474** it accepts a UTF-8 string.
45475*/
45476SQLITE_API int sqlite3_win32_set_directory8(
45477 unsigned long type, /* Identifier for directory being set or reset */
45478 const char *zValue /* New value for directory being set or reset */
45479){
45480 char **ppDirectory = 0;
45481#ifndef SQLITE_OMIT_AUTOINIT
45482 int rc = sqlite3_initialize();
45483 if( rc ) return rc;
45484#endif
45485 if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE1 ){
45486 ppDirectory = &sqlite3_data_directory;
45487 }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE2 ){
45488 ppDirectory = &sqlite3_temp_directory;
45489 }
45490 assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE((void) (0))
45491 || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE((void) (0))
45492 )((void) (0));
45493 assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) )((void) (0));
45494 if( ppDirectory ){
45495 char *zCopy = 0;
45496 if( zValue && zValue[0] ){
45497 zCopy = sqlite3_mprintf("%s", zValue);
45498 if ( zCopy==0 ){
45499 return SQLITE_NOMEM_BKPT7;
45500 }
45501 }
45502 sqlite3_free(*ppDirectory);
45503 *ppDirectory = zCopy;
45504 return SQLITE_OK0;
45505 }
45506 return SQLITE_ERROR1;
45507}
45508
45509/*
45510** This function is the same as sqlite3_win32_set_directory (below); however,
45511** it accepts a UTF-16 string.
45512*/
45513SQLITE_API int sqlite3_win32_set_directory16(
45514 unsigned long type, /* Identifier for directory being set or reset */
45515 const void *zValue /* New value for directory being set or reset */
45516){
45517 int rc;
45518 char *zUtf8 = 0;
45519 if( zValue ){
45520 zUtf8 = sqlite3_win32_unicode_to_utf8(zValue);
45521 if( zUtf8==0 ) return SQLITE_NOMEM_BKPT7;
45522 }
45523 rc = sqlite3_win32_set_directory8(type, zUtf8);
45524 if( zUtf8 ) sqlite3_free(zUtf8);
45525 return rc;
45526}
45527
45528/*
45529** This function sets the data directory or the temporary directory based on
45530** the provided arguments. The type argument must be 1 in order to set the
45531** data directory or 2 in order to set the temporary directory. The zValue
45532** argument is the name of the directory to use. The return value will be
45533** SQLITE_OK if successful.
45534*/
45535SQLITE_API int sqlite3_win32_set_directory(
45536 unsigned long type, /* Identifier for directory being set or reset */
45537 void *zValue /* New value for directory being set or reset */
45538){
45539 return sqlite3_win32_set_directory16(type, zValue);
45540}
45541
45542/*
45543** The return value of winGetLastErrorMsg
45544** is zero if the error message fits in the buffer, or non-zero
45545** otherwise (if the message was truncated).
45546*/
45547static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
45548 /* FormatMessage returns 0 on failure. Otherwise it
45549 ** returns the number of TCHARs written to the output
45550 ** buffer, excluding the terminating null char.
45551 */
45552 DWORD dwLen = 0;
45553 char *zOut = 0;
45554
45555 if( osIsNT() ){
45556#if SQLITE_OS_WINRT
45557 WCHAR zTempWide[SQLITE_WIN32_MAX_ERRMSG_CHARS+1];
45558 dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
45559 FORMAT_MESSAGE_IGNORE_INSERTS,
45560 NULL((void*)0),
45561 lastErrno,
45562 0,
45563 zTempWide,
45564 SQLITE_WIN32_MAX_ERRMSG_CHARS,
45565 0);
45566#else
45567 LPWSTR zTempWide = NULL((void*)0);
45568 dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
45569 FORMAT_MESSAGE_FROM_SYSTEM |
45570 FORMAT_MESSAGE_IGNORE_INSERTS,
45571 NULL((void*)0),
45572 lastErrno,
45573 0,
45574 (LPWSTR) &zTempWide,
45575 0,
45576 0);
45577#endif
45578 if( dwLen > 0 ){
45579 /* allocate a buffer and convert to UTF8 */
45580 sqlite3BeginBenignMalloc();
45581 zOut = winUnicodeToUtf8(zTempWide);
45582 sqlite3EndBenignMalloc();
45583#if !SQLITE_OS_WINRT
45584 /* free the system buffer allocated by FormatMessage */
45585 osLocalFree(zTempWide);
45586#endif
45587 }
45588 }
45589#ifdef SQLITE_WIN32_HAS_ANSI
45590 else{
45591 char *zTemp = NULL((void*)0);
45592 dwLen = osFormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
45593 FORMAT_MESSAGE_FROM_SYSTEM |
45594 FORMAT_MESSAGE_IGNORE_INSERTS,
45595 NULL((void*)0),
45596 lastErrno,
45597 0,
45598 (LPSTR) &zTemp,
45599 0,
45600 0);
45601 if( dwLen > 0 ){
45602 /* allocate a buffer and convert to UTF8 */
45603 sqlite3BeginBenignMalloc();
45604 zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
45605 sqlite3EndBenignMalloc();
45606 /* free the system buffer allocated by FormatMessage */
45607 osLocalFree(zTemp);
45608 }
45609 }
45610#endif
45611 if( 0 == dwLen ){
45612 sqlite3_snprintf(nBuf, zBuf, "OsError 0x%lx (%lu)", lastErrno, lastErrno);
45613 }else{
45614 /* copy a maximum of nBuf chars to output buffer */
45615 sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
45616 /* free the UTF8 buffer */
45617 sqlite3_free(zOut);
45618 }
45619 return 0;
45620}
45621
45622/*
45623**
45624** This function - winLogErrorAtLine() - is only ever called via the macro
45625** winLogError().
45626**
45627** This routine is invoked after an error occurs in an OS function.
45628** It logs a message using sqlite3_log() containing the current value of
45629** error code and, if possible, the human-readable equivalent from
45630** FormatMessage.
45631**
45632** The first argument passed to the macro should be the error code that
45633** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
45634** The two subsequent arguments should be the name of the OS function that
45635** failed and the associated file-system path, if any.
45636*/
45637#define winLogError(a,b,c,d) winLogErrorAtLine(a,b,c,d,__LINE__45637)
45638static int winLogErrorAtLine(
45639 int errcode, /* SQLite error code */
45640 DWORD lastErrno, /* Win32 last error */
45641 const char *zFunc, /* Name of OS function that failed */
45642 const char *zPath, /* File path associated with error */
45643 int iLine /* Source line number where error occurred */
45644){
45645 char zMsg[500]; /* Human readable error text */
45646 int i; /* Loop counter */
45647
45648 zMsg[0] = 0;
45649 winGetLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
45650 assert( errcode!=SQLITE_OK )((void) (0));
45651 if( zPath==0 ) zPath = "";
45652 for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
45653 zMsg[i] = 0;
45654 sqlite3_log(errcode,
45655 "os_win.c:%d: (%lu) %s(%s) - %s",
45656 iLine, lastErrno, zFunc, zPath, zMsg
45657 );
45658
45659 return errcode;
45660}
45661
45662/*
45663** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
45664** will be retried following a locking error - probably caused by
45665** antivirus software. Also the initial delay before the first retry.
45666** The delay increases linearly with each retry.
45667*/
45668#ifndef SQLITE_WIN32_IOERR_RETRY
45669# define SQLITE_WIN32_IOERR_RETRY 10
45670#endif
45671#ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
45672# define SQLITE_WIN32_IOERR_RETRY_DELAY 25
45673#endif
45674static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
45675static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
45676
45677/*
45678** The "winIoerrCanRetry1" macro is used to determine if a particular I/O
45679** error code obtained via GetLastError() is eligible to be retried. It
45680** must accept the error code DWORD as its only argument and should return
45681** non-zero if the error code is transient in nature and the operation
45682** responsible for generating the original error might succeed upon being
45683** retried. The argument to this macro should be a variable.
45684**
45685** Additionally, a macro named "winIoerrCanRetry2" may be defined. If it
45686** is defined, it will be consulted only when the macro "winIoerrCanRetry1"
45687** returns zero. The "winIoerrCanRetry2" macro is completely optional and
45688** may be used to include additional error codes in the set that should
45689** result in the failing I/O operation being retried by the caller. If
45690** defined, the "winIoerrCanRetry2" macro must exhibit external semantics
45691** identical to those of the "winIoerrCanRetry1" macro.
45692*/
45693#if !defined(winIoerrCanRetry1)
45694#define winIoerrCanRetry1(a) (((a)==ERROR_ACCESS_DENIED) || \
45695 ((a)==ERROR_SHARING_VIOLATION) || \
45696 ((a)==ERROR_LOCK_VIOLATION) || \
45697 ((a)==ERROR_DEV_NOT_EXIST) || \
45698 ((a)==ERROR_NETNAME_DELETED) || \
45699 ((a)==ERROR_SEM_TIMEOUT) || \
45700 ((a)==ERROR_NETWORK_UNREACHABLE))
45701#endif
45702
45703/*
45704** If a ReadFile() or WriteFile() error occurs, invoke this routine
45705** to see if it should be retried. Return TRUE to retry. Return FALSE
45706** to give up with an error.
45707*/
45708static int winRetryIoerr(int *pnRetry, DWORD *pError){
45709 DWORD e = osGetLastError();
45710 if( *pnRetry>=winIoerrRetry ){
45711 if( pError ){
45712 *pError = e;
45713 }
45714 return 0;
45715 }
45716 if( winIoerrCanRetry1(e) ){
45717 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
45718 ++*pnRetry;
45719 return 1;
45720 }
45721#if defined(winIoerrCanRetry2)
45722 else if( winIoerrCanRetry2(e) ){
45723 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
45724 ++*pnRetry;
45725 return 1;
45726 }
45727#endif
45728 if( pError ){
45729 *pError = e;
45730 }
45731 return 0;
45732}
45733
45734/*
45735** Log a I/O error retry episode.
45736*/
45737static void winLogIoerr(int nRetry, int lineno){
45738 if( nRetry ){
45739 sqlite3_log(SQLITE_NOTICE27,
45740 "delayed %dms for lock/sharing conflict at line %d",
45741 winIoerrRetryDelay*nRetry*(nRetry+1)/2, lineno
45742 );
45743 }
45744}
45745
45746/*
45747** This #if does not rely on the SQLITE_OS_WINCE define because the
45748** corresponding section in "date.c" cannot use it.
45749*/
45750#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
45751 (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
45752/*
45753** The MSVC CRT on Windows CE may not have a localtime() function.
45754** So define a substitute.
45755*/
45756/* # include <time.h> */
45757struct tm *__cdecl localtime(const time_t *t)
45758{
45759 static struct tm y;
45760 FILETIME uTm, lTm;
45761 SYSTEMTIME pTm;
45762 sqlite3_int64 t64;
45763 t64 = *t;
45764 t64 = (t64 + 11644473600)*10000000;
45765 uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
45766 uTm.dwHighDateTime= (DWORD)(t64 >> 32);
45767 osFileTimeToLocalFileTime(&uTm,&lTm);
45768 osFileTimeToSystemTime(&lTm,&pTm);
45769 y.tm_year = pTm.wYear - 1900;
45770 y.tm_mon = pTm.wMonth - 1;
45771 y.tm_wday = pTm.wDayOfWeek;
45772 y.tm_mday = pTm.wDay;
45773 y.tm_hour = pTm.wHour;
45774 y.tm_min = pTm.wMinute;
45775 y.tm_sec = pTm.wSecond;
45776 return &y;
45777}
45778#endif
45779
45780#if SQLITE_OS_WINCE
45781/*************************************************************************
45782** This section contains code for WinCE only.
45783*/
45784#define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)__builtin_offsetof(winFile, h)]
45785
45786/*
45787** Acquire a lock on the handle h
45788*/
45789static void winceMutexAcquire(HANDLE h){
45790 DWORD dwErr;
45791 do {
45792 dwErr = osWaitForSingleObject(h, INFINITE);
45793 } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
45794}
45795/*
45796** Release a lock acquired by winceMutexAcquire()
45797*/
45798#define winceMutexRelease(h) ReleaseMutex(h)
45799
45800/*
45801** Create the mutex and shared memory used for locking in the file
45802** descriptor pFile
45803*/
45804static int winceCreateLock(const char *zFilename, winFile *pFile){
45805 LPWSTR zTok;
45806 LPWSTR zName;
45807 DWORD lastErrno;
45808 BOOL bLogged = FALSE;
45809 BOOL bInit = TRUE;
45810
45811 zName = winUtf8ToUnicode(zFilename);
45812 if( zName==0 ){
45813 /* out of memory */
45814 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
45815 }
45816
45817 /* Initialize the local lockdata */
45818 memset(&pFile->local, 0, sizeof(pFile->local));
45819
45820 /* Replace the backslashes from the filename and lowercase it
45821 ** to derive a mutex name. */
45822 zTok = osCharLowerW(zName);
45823 for (;*zTok;zTok++){
45824 if (*zTok == '\\') *zTok = '_';
45825 }
45826
45827 /* Create/open the named mutex */
45828 pFile->hMutex = osCreateMutexW(NULL((void*)0), FALSE, zName);
45829 if (!pFile->hMutex){
45830 pFile->lastErrno = osGetLastError();
45831 sqlite3_free(zName);
45832 return winLogError(SQLITE_IOERR10, pFile->lastErrno,
45833 "winceCreateLock1", zFilename);
45834 }
45835
45836 /* Acquire the mutex before continuing */
45837 winceMutexAcquire(pFile->hMutex);
45838
45839 /* Since the names of named mutexes, semaphores, file mappings etc are
45840 ** case-sensitive, take advantage of that by uppercasing the mutex name
45841 ** and using that as the shared filemapping name.
45842 */
45843 osCharUpperW(zName);
45844 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL((void*)0),
45845 PAGE_READWRITE, 0, sizeof(winceLock),
45846 zName);
45847
45848 /* Set a flag that indicates we're the first to create the memory so it
45849 ** must be zero-initialized */
45850 lastErrno = osGetLastError();
45851 if (lastErrno == ERROR_ALREADY_EXISTS){
45852 bInit = FALSE;
45853 }
45854
45855 sqlite3_free(zName);
45856
45857 /* If we succeeded in making the shared memory handle, map it. */
45858 if( pFile->hShared ){
45859 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
45860 FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
45861 /* If mapping failed, close the shared memory handle and erase it */
45862 if( !pFile->shared ){
45863 pFile->lastErrno = osGetLastError();
45864 winLogError(SQLITE_IOERR10, pFile->lastErrno,
45865 "winceCreateLock2", zFilename);
45866 bLogged = TRUE;
45867 osCloseHandle(pFile->hShared);
45868 pFile->hShared = NULL((void*)0);
45869 }
45870 }
45871
45872 /* If shared memory could not be created, then close the mutex and fail */
45873 if( pFile->hShared==NULL((void*)0) ){
45874 if( !bLogged ){
45875 pFile->lastErrno = lastErrno;
45876 winLogError(SQLITE_IOERR10, pFile->lastErrno,
45877 "winceCreateLock3", zFilename);
45878 bLogged = TRUE;
45879 }
45880 winceMutexRelease(pFile->hMutex);
45881 osCloseHandle(pFile->hMutex);
45882 pFile->hMutex = NULL((void*)0);
45883 return SQLITE_IOERR10;
45884 }
45885
45886 /* Initialize the shared memory if we're supposed to */
45887 if( bInit ){
45888 memset(pFile->shared, 0, sizeof(winceLock));
45889 }
45890
45891 winceMutexRelease(pFile->hMutex);
45892 return SQLITE_OK0;
45893}
45894
45895/*
45896** Destroy the part of winFile that deals with wince locks
45897*/
45898static void winceDestroyLock(winFile *pFile){
45899 if (pFile->hMutex){
45900 /* Acquire the mutex */
45901 winceMutexAcquire(pFile->hMutex);
45902
45903 /* The following blocks should probably assert in debug mode, but they
45904 are to cleanup in case any locks remained open */
45905 if (pFile->local.nReaders){
45906 pFile->shared->nReaders --;
45907 }
45908 if (pFile->local.bReserved){
45909 pFile->shared->bReserved = FALSE;
45910 }
45911 if (pFile->local.bPending){
45912 pFile->shared->bPending = FALSE;
45913 }
45914 if (pFile->local.bExclusive){
45915 pFile->shared->bExclusive = FALSE;
45916 }
45917
45918 /* De-reference and close our copy of the shared memory handle */
45919 osUnmapViewOfFile(pFile->shared);
45920 osCloseHandle(pFile->hShared);
45921
45922 /* Done with the mutex */
45923 winceMutexRelease(pFile->hMutex);
45924 osCloseHandle(pFile->hMutex);
45925 pFile->hMutex = NULL((void*)0);
45926 }
45927}
45928
45929/*
45930** An implementation of the LockFile() API of Windows for CE
45931*/
45932static BOOL winceLockFile(
45933 LPHANDLE phFile,
45934 DWORD dwFileOffsetLow,
45935 DWORD dwFileOffsetHigh,
45936 DWORD nNumberOfBytesToLockLow,
45937 DWORD nNumberOfBytesToLockHigh
45938){
45939 winFile *pFile = HANDLE_TO_WINFILE(phFile);
45940 BOOL bReturn = FALSE;
45941
45942 UNUSED_PARAMETER(dwFileOffsetHigh)(void)(dwFileOffsetHigh);
45943 UNUSED_PARAMETER(nNumberOfBytesToLockHigh)(void)(nNumberOfBytesToLockHigh);
45944
45945 if (!pFile->hMutex) return TRUE;
45946 winceMutexAcquire(pFile->hMutex);
45947
45948 /* Wanting an exclusive lock? */
45949 if (dwFileOffsetLow == (DWORD)SHARED_FIRST(sqlite3PendingByte+2)
45950 && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE510){
45951 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
45952 pFile->shared->bExclusive = TRUE;
45953 pFile->local.bExclusive = TRUE;
45954 bReturn = TRUE;
45955 }
45956 }
45957
45958 /* Want a read-only lock? */
45959 else if (dwFileOffsetLow == (DWORD)SHARED_FIRST(sqlite3PendingByte+2) &&
45960 nNumberOfBytesToLockLow == 1){
45961 if (pFile->shared->bExclusive == 0){
45962 pFile->local.nReaders ++;
45963 if (pFile->local.nReaders == 1){
45964 pFile->shared->nReaders ++;
45965 }
45966 bReturn = TRUE;
45967 }
45968 }
45969
45970 /* Want a pending lock? */
45971 else if (dwFileOffsetLow == (DWORD)PENDING_BYTEsqlite3PendingByte
45972 && nNumberOfBytesToLockLow == 1){
45973 /* If no pending lock has been acquired, then acquire it */
45974 if (pFile->shared->bPending == 0) {
45975 pFile->shared->bPending = TRUE;
45976 pFile->local.bPending = TRUE;
45977 bReturn = TRUE;
45978 }
45979 }
45980
45981 /* Want a reserved lock? */
45982 else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE(sqlite3PendingByte+1)
45983 && nNumberOfBytesToLockLow == 1){
45984 if (pFile->shared->bReserved == 0) {
45985 pFile->shared->bReserved = TRUE;
45986 pFile->local.bReserved = TRUE;
45987 bReturn = TRUE;
45988 }
45989 }
45990
45991 winceMutexRelease(pFile->hMutex);
45992 return bReturn;
45993}
45994
45995/*
45996** An implementation of the UnlockFile API of Windows for CE
45997*/
45998static BOOL winceUnlockFile(
45999 LPHANDLE phFile,
46000 DWORD dwFileOffsetLow,
46001 DWORD dwFileOffsetHigh,
46002 DWORD nNumberOfBytesToUnlockLow,
46003 DWORD nNumberOfBytesToUnlockHigh
46004){
46005 winFile *pFile = HANDLE_TO_WINFILE(phFile);
46006 BOOL bReturn = FALSE;
46007
46008 UNUSED_PARAMETER(dwFileOffsetHigh)(void)(dwFileOffsetHigh);
46009 UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh)(void)(nNumberOfBytesToUnlockHigh);
46010
46011 if (!pFile->hMutex) return TRUE;
46012 winceMutexAcquire(pFile->hMutex);
46013
46014 /* Releasing a reader lock or an exclusive lock */
46015 if (dwFileOffsetLow == (DWORD)SHARED_FIRST(sqlite3PendingByte+2)){
46016 /* Did we have an exclusive lock? */
46017 if (pFile->local.bExclusive){
46018 assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE)((void) (0));
46019 pFile->local.bExclusive = FALSE;
46020 pFile->shared->bExclusive = FALSE;
46021 bReturn = TRUE;
46022 }
46023
46024 /* Did we just have a reader lock? */
46025 else if (pFile->local.nReaders){
46026 assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE((void) (0))
46027 || nNumberOfBytesToUnlockLow == 1)((void) (0));
46028 pFile->local.nReaders --;
46029 if (pFile->local.nReaders == 0)
46030 {
46031 pFile->shared->nReaders --;
46032 }
46033 bReturn = TRUE;
46034 }
46035 }
46036
46037 /* Releasing a pending lock */
46038 else if (dwFileOffsetLow == (DWORD)PENDING_BYTEsqlite3PendingByte
46039 && nNumberOfBytesToUnlockLow == 1){
46040 if (pFile->local.bPending){
46041 pFile->local.bPending = FALSE;
46042 pFile->shared->bPending = FALSE;
46043 bReturn = TRUE;
46044 }
46045 }
46046 /* Releasing a reserved lock */
46047 else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE(sqlite3PendingByte+1)
46048 && nNumberOfBytesToUnlockLow == 1){
46049 if (pFile->local.bReserved) {
46050 pFile->local.bReserved = FALSE;
46051 pFile->shared->bReserved = FALSE;
46052 bReturn = TRUE;
46053 }
46054 }
46055
46056 winceMutexRelease(pFile->hMutex);
46057 return bReturn;
46058}
46059/*
46060** End of the special code for wince
46061*****************************************************************************/
46062#endif /* SQLITE_OS_WINCE */
46063
46064/*
46065** Lock a file region.
46066*/
46067static BOOL winLockFile(
46068 LPHANDLE phFile,
46069 DWORD flags,
46070 DWORD offsetLow,
46071 DWORD offsetHigh,
46072 DWORD numBytesLow,
46073 DWORD numBytesHigh
46074){
46075#if SQLITE_OS_WINCE
46076 /*
46077 ** NOTE: Windows CE is handled differently here due its lack of the Win32
46078 ** API LockFile.
46079 */
46080 return winceLockFile(phFile, offsetLow, offsetHigh,
46081 numBytesLow, numBytesHigh);
46082#else
46083 if( osIsNT() ){
46084 OVERLAPPED ovlp;
46085 memset(&ovlp, 0, sizeof(OVERLAPPED));
46086 ovlp.Offset = offsetLow;
46087 ovlp.OffsetHigh = offsetHigh;
46088 return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
46089 }else{
46090 return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
46091 numBytesHigh);
46092 }
46093#endif
46094}
46095
46096/*
46097** Unlock a file region.
46098 */
46099static BOOL winUnlockFile(
46100 LPHANDLE phFile,
46101 DWORD offsetLow,
46102 DWORD offsetHigh,
46103 DWORD numBytesLow,
46104 DWORD numBytesHigh
46105){
46106#if SQLITE_OS_WINCE
46107 /*
46108 ** NOTE: Windows CE is handled differently here due its lack of the Win32
46109 ** API UnlockFile.
46110 */
46111 return winceUnlockFile(phFile, offsetLow, offsetHigh,
46112 numBytesLow, numBytesHigh);
46113#else
46114 if( osIsNT() ){
46115 OVERLAPPED ovlp;
46116 memset(&ovlp, 0, sizeof(OVERLAPPED));
46117 ovlp.Offset = offsetLow;
46118 ovlp.OffsetHigh = offsetHigh;
46119 return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
46120 }else{
46121 return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
46122 numBytesHigh);
46123 }
46124#endif
46125}
46126
46127/*****************************************************************************
46128** The next group of routines implement the I/O methods specified
46129** by the sqlite3_io_methods object.
46130******************************************************************************/
46131
46132/*
46133** Some Microsoft compilers lack this definition.
46134*/
46135#ifndef INVALID_SET_FILE_POINTER
46136# define INVALID_SET_FILE_POINTER ((DWORD)-1)
46137#endif
46138
46139/*
46140** Move the current position of the file handle passed as the first
46141** argument to offset iOffset within the file. If successful, return 0.
46142** Otherwise, set pFile->lastErrno and return non-zero.
46143*/
46144static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
46145#if !SQLITE_OS_WINRT
46146 LONG upperBits; /* Most sig. 32 bits of new offset */
46147 LONG lowerBits; /* Least sig. 32 bits of new offset */
46148 DWORD dwRet; /* Value returned by SetFilePointer() */
46149 DWORD lastErrno; /* Value returned by GetLastError() */
46150
46151 OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
46152
46153 upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
46154 lowerBits = (LONG)(iOffset & 0xffffffff);
46155
46156 /* API oddity: If successful, SetFilePointer() returns a dword
46157 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
46158 ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
46159 ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
46160 ** whether an error has actually occurred, it is also necessary to call
46161 ** GetLastError().
46162 */
46163 dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
46164
46165 if( (dwRet==INVALID_SET_FILE_POINTER
46166 && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
46167 pFile->lastErrno = lastErrno;
46168 winLogError(SQLITE_IOERR_SEEK(10 | (22<<8)), pFile->lastErrno,
46169 "winSeekFile", pFile->zPath);
46170 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
46171 return 1;
46172 }
46173
46174 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
46175 return 0;
46176#else
46177 /*
46178 ** Same as above, except that this implementation works for WinRT.
46179 */
46180
46181 LARGE_INTEGER x; /* The new offset */
46182 BOOL bRet; /* Value returned by SetFilePointerEx() */
46183
46184 x.QuadPart = iOffset;
46185 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
46186
46187 if(!bRet){
46188 pFile->lastErrno = osGetLastError();
46189 winLogError(SQLITE_IOERR_SEEK(10 | (22<<8)), pFile->lastErrno,
46190 "winSeekFile", pFile->zPath);
46191 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
46192 return 1;
46193 }
46194
46195 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
46196 return 0;
46197#endif
46198}
46199
46200#if SQLITE_MAX_MMAP_SIZE20971520>0
46201/* Forward references to VFS helper methods used for memory mapped files */
46202static int winMapfile(winFile*, sqlite3_int64);
46203static int winUnmapfile(winFile*);
46204#endif
46205
46206/*
46207** Close a file.
46208**
46209** It is reported that an attempt to close a handle might sometimes
46210** fail. This is a very unreasonable result, but Windows is notorious
46211** for being unreasonable so I do not doubt that it might happen. If
46212** the close fails, we pause for 100 milliseconds and try again. As
46213** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
46214** giving up and returning an error.
46215*/
46216#define MX_CLOSE_ATTEMPT 3
46217static int winClose(sqlite3_file *id){
46218 int rc, cnt = 0;
46219 winFile *pFile = (winFile*)id;
46220
46221 assert( id!=0 )((void) (0));
46222#ifndef SQLITE_OMIT_WAL
46223 assert( pFile->pShm==0 )((void) (0));
46224#endif
46225 assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE )((void) (0));
46226 OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p\n",
46227 osGetCurrentProcessId(), pFile, pFile->h));
46228
46229#if SQLITE_MAX_MMAP_SIZE20971520>0
46230 winUnmapfile(pFile);
46231#endif
46232
46233 do{
46234 rc = osCloseHandle(pFile->h);
46235 /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
46236 }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
46237#if SQLITE_OS_WINCE
46238#define WINCE_DELETION_ATTEMPTS 3
46239 {
46240 winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
46241 if( pAppData==NULL((void*)0) || !pAppData->bNoLock ){
46242 winceDestroyLock(pFile);
46243 }
46244 }
46245 if( pFile->zDeleteOnClose ){
46246 int cnt = 0;
46247 while(
46248 osDeleteFileW(pFile->zDeleteOnClose)==0
46249 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
46250 && cnt++ < WINCE_DELETION_ATTEMPTS
46251 ){
46252 sqlite3_win32_sleep(100); /* Wait a little before trying again */
46253 }
46254 sqlite3_free(pFile->zDeleteOnClose);
46255 }
46256#endif
46257 if( rc ){
46258 pFile->h = NULL((void*)0);
46259 }
46260 OpenCounter(-1);
46261 OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p, rc=%s\n",
46262 osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
46263 return rc ? SQLITE_OK0
46264 : winLogError(SQLITE_IOERR_CLOSE(10 | (16<<8)), osGetLastError(),
46265 "winClose", pFile->zPath);
46266}
46267
46268/*
46269** Read data from a file into a buffer. Return SQLITE_OK if all
46270** bytes were read successfully and SQLITE_IOERR if anything goes
46271** wrong.
46272*/
46273static int winRead(
46274 sqlite3_file *id, /* File to read from */
46275 void *pBuf, /* Write content into this buffer */
46276 int amt, /* Number of bytes to read */
46277 sqlite3_int64 offset /* Begin reading at this offset */
46278){
46279#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
46280 OVERLAPPED overlapped; /* The offset for ReadFile. */
46281#endif
46282 winFile *pFile = (winFile*)id; /* file handle */
46283 DWORD nRead; /* Number of bytes actually read from file */
46284 int nRetry = 0; /* Number of retrys */
46285
46286 assert( id!=0 )((void) (0));
46287 assert( amt>0 )((void) (0));
46288 assert( offset>=0 )((void) (0));
46289 SimulateIOError(return SQLITE_IOERR_READ);
46290 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
46291 "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
46292 pFile->h, pBuf, amt, offset, pFile->locktype));
46293
46294#if SQLITE_MAX_MMAP_SIZE20971520>0
46295 /* Deal with as much of this read request as possible by transfering
46296 ** data from the memory mapping using memcpy(). */
46297 if( offset<pFile->mmapSize ){
46298 if( offset+amt <= pFile->mmapSize ){
46299 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
46300 OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
46301 osGetCurrentProcessId(), pFile, pFile->h));
46302 return SQLITE_OK0;
46303 }else{
46304 int nCopy = (int)(pFile->mmapSize - offset);
46305 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
46306 pBuf = &((u8 *)pBuf)[nCopy];
46307 amt -= nCopy;
46308 offset += nCopy;
46309 }
46310 }
46311#endif
46312
46313#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
46314 if( winSeekFile(pFile, offset) ){
46315 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
46316 osGetCurrentProcessId(), pFile, pFile->h));
46317 return SQLITE_FULL13;
46318 }
46319 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
46320#else
46321 memset(&overlapped, 0, sizeof(OVERLAPPED));
46322 overlapped.Offset = (LONG)(offset & 0xffffffff);
46323 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
46324 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
46325 osGetLastError()!=ERROR_HANDLE_EOF ){
46326#endif
46327 DWORD lastErrno;
46328 if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
46329 pFile->lastErrno = lastErrno;
46330 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_READ\n",
46331 osGetCurrentProcessId(), pFile, pFile->h));
46332 return winLogError(SQLITE_IOERR_READ(10 | (1<<8)), pFile->lastErrno,
46333 "winRead", pFile->zPath);
46334 }
46335 winLogIoerr(nRetry, __LINE__46335);
46336 if( nRead<(DWORD)amt ){
46337 /* Unread parts of the buffer must be zero-filled */
46338 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
46339 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_SHORT_READ\n",
46340 osGetCurrentProcessId(), pFile, pFile->h));
46341 return SQLITE_IOERR_SHORT_READ(10 | (2<<8));
46342 }
46343
46344 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
46345 osGetCurrentProcessId(), pFile, pFile->h));
46346 return SQLITE_OK0;
46347}
46348
46349/*
46350** Write data from a buffer into a file. Return SQLITE_OK on success
46351** or some other error code on failure.
46352*/
46353static int winWrite(
46354 sqlite3_file *id, /* File to write into */
46355 const void *pBuf, /* The bytes to be written */
46356 int amt, /* Number of bytes to write */
46357 sqlite3_int64 offset /* Offset into the file to begin writing at */
46358){
46359 int rc = 0; /* True if error has occurred, else false */
46360 winFile *pFile = (winFile*)id; /* File handle */
46361 int nRetry = 0; /* Number of retries */
46362
46363 assert( amt>0 )((void) (0));
46364 assert( pFile )((void) (0));
46365 SimulateIOError(return SQLITE_IOERR_WRITE);
46366 SimulateDiskfullError(return SQLITE_FULL);
46367
46368 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
46369 "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
46370 pFile->h, pBuf, amt, offset, pFile->locktype));
46371
46372#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE20971520>0
46373 /* Deal with as much of this write request as possible by transfering
46374 ** data from the memory mapping using memcpy(). */
46375 if( offset<pFile->mmapSize ){
46376 if( offset+amt <= pFile->mmapSize ){
46377 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
46378 OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
46379 osGetCurrentProcessId(), pFile, pFile->h));
46380 return SQLITE_OK0;
46381 }else{
46382 int nCopy = (int)(pFile->mmapSize - offset);
46383 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
46384 pBuf = &((u8 *)pBuf)[nCopy];
46385 amt -= nCopy;
46386 offset += nCopy;
46387 }
46388 }
46389#endif
46390
46391#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
46392 rc = winSeekFile(pFile, offset);
46393 if( rc==0 ){
46394#else
46395 {
46396#endif
46397#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
46398 OVERLAPPED overlapped; /* The offset for WriteFile. */
46399#endif
46400 u8 *aRem = (u8 *)pBuf; /* Data yet to be written */
46401 int nRem = amt; /* Number of bytes yet to be written */
46402 DWORD nWrite; /* Bytes written by each WriteFile() call */
46403 DWORD lastErrno = NO_ERROR; /* Value returned by GetLastError() */
46404
46405#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
46406 memset(&overlapped, 0, sizeof(OVERLAPPED));
46407 overlapped.Offset = (LONG)(offset & 0xffffffff);
46408 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
46409#endif
46410
46411 while( nRem>0 ){
46412#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
46413 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
46414#else
46415 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
46416#endif
46417 if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
46418 break;
46419 }
46420 assert( nWrite==0 || nWrite<=(DWORD)nRem )((void) (0));
46421 if( nWrite==0 || nWrite>(DWORD)nRem ){
46422 lastErrno = osGetLastError();
46423 break;
46424 }
46425#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
46426 offset += nWrite;
46427 overlapped.Offset = (LONG)(offset & 0xffffffff);
46428 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
46429#endif
46430 aRem += nWrite;
46431 nRem -= nWrite;
46432 }
46433 if( nRem>0 ){
46434 pFile->lastErrno = lastErrno;
46435 rc = 1;
46436 }
46437 }
46438
46439 if( rc ){
46440 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
46441 || ( pFile->lastErrno==ERROR_DISK_FULL )){
46442 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
46443 osGetCurrentProcessId(), pFile, pFile->h));
46444 return winLogError(SQLITE_FULL13, pFile->lastErrno,
46445 "winWrite1", pFile->zPath);
46446 }
46447 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_WRITE\n",
46448 osGetCurrentProcessId(), pFile, pFile->h));
46449 return winLogError(SQLITE_IOERR_WRITE(10 | (3<<8)), pFile->lastErrno,
46450 "winWrite2", pFile->zPath);
46451 }else{
46452 winLogIoerr(nRetry, __LINE__46452);
46453 }
46454 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
46455 osGetCurrentProcessId(), pFile, pFile->h));
46456 return SQLITE_OK0;
46457}
46458
46459/*
46460** Truncate an open file to a specified size
46461*/
46462static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
46463 winFile *pFile = (winFile*)id; /* File handle object */
46464 int rc = SQLITE_OK0; /* Return code for this function */
46465 DWORD lastErrno;
46466#if SQLITE_MAX_MMAP_SIZE20971520>0
46467 sqlite3_int64 oldMmapSize;
46468 if( pFile->nFetchOut>0 ){
46469 /* File truncation is a no-op if there are outstanding memory mapped
46470 ** pages. This is because truncating the file means temporarily unmapping
46471 ** the file, and that might delete memory out from under existing cursors.
46472 **
46473 ** This can result in incremental vacuum not truncating the file,
46474 ** if there is an active read cursor when the incremental vacuum occurs.
46475 ** No real harm comes of this - the database file is not corrupted,
46476 ** though some folks might complain that the file is bigger than it
46477 ** needs to be.
46478 **
46479 ** The only feasible work-around is to defer the truncation until after
46480 ** all references to memory-mapped content are closed. That is doable,
46481 ** but involves adding a few branches in the common write code path which
46482 ** could slow down normal operations slightly. Hence, we have decided for
46483 ** now to simply make trancations a no-op if there are pending reads. We
46484 ** can maybe revisit this decision in the future.
46485 */
46486 return SQLITE_OK0;
46487 }
46488#endif
46489
46490 assert( pFile )((void) (0));
46491 SimulateIOError(return SQLITE_IOERR_TRUNCATE);
46492 OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, size=%lld, lock=%d\n",
46493 osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
46494
46495 /* If the user has configured a chunk-size for this file, truncate the
46496 ** file so that it consists of an integer number of chunks (i.e. the
46497 ** actual file size after the operation may be larger than the requested
46498 ** size).
46499 */
46500 if( pFile->szChunk>0 ){
46501 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
46502 }
46503
46504#if SQLITE_MAX_MMAP_SIZE20971520>0
46505 if( pFile->pMapRegion ){
46506 oldMmapSize = pFile->mmapSize;
46507 }else{
46508 oldMmapSize = 0;
46509 }
46510 winUnmapfile(pFile);
46511#endif
46512
46513 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
46514 if( winSeekFile(pFile, nByte) ){
46515 rc = winLogError(SQLITE_IOERR_TRUNCATE(10 | (6<<8)), pFile->lastErrno,
46516 "winTruncate1", pFile->zPath);
46517 }else if( 0==osSetEndOfFile(pFile->h) &&
46518 ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
46519 pFile->lastErrno = lastErrno;
46520 rc = winLogError(SQLITE_IOERR_TRUNCATE(10 | (6<<8)), pFile->lastErrno,
46521 "winTruncate2", pFile->zPath);
46522 }
46523
46524#if SQLITE_MAX_MMAP_SIZE20971520>0
46525 if( rc==SQLITE_OK0 && oldMmapSize>0 ){
46526 if( oldMmapSize>nByte ){
46527 winMapfile(pFile, -1);
46528 }else{
46529 winMapfile(pFile, oldMmapSize);
46530 }
46531 }
46532#endif
46533
46534 OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, rc=%s\n",
46535 osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
46536 return rc;
46537}
46538
46539#ifdef SQLITE_TEST
46540/*
46541** Count the number of fullsyncs and normal syncs. This is used to test
46542** that syncs and fullsyncs are occuring at the right times.
46543*/
46544SQLITE_API int sqlite3_sync_count = 0;
46545SQLITE_API int sqlite3_fullsync_count = 0;
46546#endif
46547
46548/*
46549** Make sure all writes to a particular file are committed to disk.
46550*/
46551static int winSync(sqlite3_file *id, int flags){
46552#ifndef SQLITE_NO_SYNC
46553 /*
46554 ** Used only when SQLITE_NO_SYNC is not defined.
46555 */
46556 BOOL rc;
46557#endif
46558#if !defined(NDEBUG1) || !defined(SQLITE_NO_SYNC) || \
46559 defined(SQLITE_HAVE_OS_TRACE)
46560 /*
46561 ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or
46562 ** OSTRACE() macros.
46563 */
46564 winFile *pFile = (winFile*)id;
46565#else
46566 UNUSED_PARAMETER(id)(void)(id);
46567#endif
46568
46569 assert( pFile )((void) (0));
46570 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
46571 assert((flags&0x0F)==SQLITE_SYNC_NORMAL((void) (0))
46572 || (flags&0x0F)==SQLITE_SYNC_FULL((void) (0))
46573 )((void) (0));
46574
46575 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
46576 ** line is to test that doing so does not cause any problems.
46577 */
46578 SimulateDiskfullError( return SQLITE_FULL );
46579
46580 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, flags=%x, lock=%d\n",
46581 osGetCurrentProcessId(), pFile, pFile->h, flags,
46582 pFile->locktype));
46583
46584#ifndef SQLITE_TEST
46585 UNUSED_PARAMETER(flags)(void)(flags);
46586#else
46587 if( (flags&0x0F)==SQLITE_SYNC_FULL0x00003 ){
46588 sqlite3_fullsync_count++;
46589 }
46590 sqlite3_sync_count++;
46591#endif
46592
46593 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
46594 ** no-op
46595 */
46596#ifdef SQLITE_NO_SYNC
46597 OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
46598 osGetCurrentProcessId(), pFile, pFile->h));
46599 return SQLITE_OK0;
46600#else
46601#if SQLITE_MAX_MMAP_SIZE20971520>0
46602 if( pFile->pMapRegion ){
46603 if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
46604 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
46605 "rc=SQLITE_OK\n", osGetCurrentProcessId(),
46606 pFile, pFile->pMapRegion));
46607 }else{
46608 pFile->lastErrno = osGetLastError();
46609 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
46610 "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(),
46611 pFile, pFile->pMapRegion));
46612 return winLogError(SQLITE_IOERR_MMAP(10 | (24<<8)), pFile->lastErrno,
46613 "winSync1", pFile->zPath);
46614 }
46615 }
46616#endif
46617 rc = osFlushFileBuffers(pFile->h);
46618 SimulateIOError( rc=FALSE );
46619 if( rc ){
46620 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
46621 osGetCurrentProcessId(), pFile, pFile->h));
46622 return SQLITE_OK0;
46623 }else{
46624 pFile->lastErrno = osGetLastError();
46625 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_FSYNC\n",
46626 osGetCurrentProcessId(), pFile, pFile->h));
46627 return winLogError(SQLITE_IOERR_FSYNC(10 | (4<<8)), pFile->lastErrno,
46628 "winSync2", pFile->zPath);
46629 }
46630#endif
46631}
46632
46633/*
46634** Determine the current size of a file in bytes
46635*/
46636static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
46637 winFile *pFile = (winFile*)id;
46638 int rc = SQLITE_OK0;
46639
46640 assert( id!=0 )((void) (0));
46641 assert( pSize!=0 )((void) (0));
46642 SimulateIOError(return SQLITE_IOERR_FSTAT);
46643 OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
46644
46645#if SQLITE_OS_WINRT
46646 {
46647 FILE_STANDARD_INFO info;
46648 if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
46649 &info, sizeof(info)) ){
46650 *pSize = info.EndOfFile.QuadPart;
46651 }else{
46652 pFile->lastErrno = osGetLastError();
46653 rc = winLogError(SQLITE_IOERR_FSTAT(10 | (7<<8)), pFile->lastErrno,
46654 "winFileSize", pFile->zPath);
46655 }
46656 }
46657#else
46658 {
46659 DWORD upperBits;
46660 DWORD lowerBits;
46661 DWORD lastErrno;
46662
46663 lowerBits = osGetFileSize(pFile->h, &upperBits);
46664 *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
46665 if( (lowerBits == INVALID_FILE_SIZE)
46666 && ((lastErrno = osGetLastError())!=NO_ERROR) ){
46667 pFile->lastErrno = lastErrno;
46668 rc = winLogError(SQLITE_IOERR_FSTAT(10 | (7<<8)), pFile->lastErrno,
46669 "winFileSize", pFile->zPath);
46670 }
46671 }
46672#endif
46673 OSTRACE(("SIZE file=%p, pSize=%p, *pSize=%lld, rc=%s\n",
46674 pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
46675 return rc;
46676}
46677
46678/*
46679** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
46680*/
46681#ifndef LOCKFILE_FAIL_IMMEDIATELY
46682# define LOCKFILE_FAIL_IMMEDIATELY 1
46683#endif
46684
46685#ifndef LOCKFILE_EXCLUSIVE_LOCK
46686# define LOCKFILE_EXCLUSIVE_LOCK 2
46687#endif
46688
46689/*
46690** Historically, SQLite has used both the LockFile and LockFileEx functions.
46691** When the LockFile function was used, it was always expected to fail
46692** immediately if the lock could not be obtained. Also, it always expected to
46693** obtain an exclusive lock. These flags are used with the LockFileEx function
46694** and reflect those expectations; therefore, they should not be changed.
46695*/
46696#ifndef SQLITE_LOCKFILE_FLAGS
46697# define SQLITE_LOCKFILE_FLAGS (LOCKFILE_FAIL_IMMEDIATELY | \
46698 LOCKFILE_EXCLUSIVE_LOCK)
46699#endif
46700
46701/*
46702** Currently, SQLite never calls the LockFileEx function without wanting the
46703** call to fail immediately if the lock cannot be obtained.
46704*/
46705#ifndef SQLITE_LOCKFILEEX_FLAGS
46706# define SQLITE_LOCKFILEEX_FLAGS (LOCKFILE_FAIL_IMMEDIATELY)
46707#endif
46708
46709/*
46710** Acquire a reader lock.
46711** Different API routines are called depending on whether or not this
46712** is Win9x or WinNT.
46713*/
46714static int winGetReadLock(winFile *pFile){
46715 int res;
46716 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
46717 if( osIsNT() ){
46718#if SQLITE_OS_WINCE
46719 /*
46720 ** NOTE: Windows CE is handled differently here due its lack of the Win32
46721 ** API LockFileEx.
46722 */
46723 res = winceLockFile(&pFile->h, SHARED_FIRST(sqlite3PendingByte+2), 0, 1, 0);
46724#else
46725 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST(sqlite3PendingByte+2), 0,
46726 SHARED_SIZE510, 0);
46727#endif
46728 }
46729#ifdef SQLITE_WIN32_HAS_ANSI
46730 else{
46731 int lk;
46732 sqlite3_randomness(sizeof(lk), &lk);
46733 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE510 - 1));
46734 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
46735 SHARED_FIRST(sqlite3PendingByte+2)+pFile->sharedLockByte, 0, 1, 0);
46736 }
46737#endif
46738 if( res == 0 ){
46739 pFile->lastErrno = osGetLastError();
46740 /* No need to log a failure to lock */
46741 }
46742 OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
46743 return res;
46744}
46745
46746/*
46747** Undo a readlock
46748*/
46749static int winUnlockReadLock(winFile *pFile){
46750 int res;
46751 DWORD lastErrno;
46752 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
46753 if( osIsNT() ){
46754 res = winUnlockFile(&pFile->h, SHARED_FIRST(sqlite3PendingByte+2), 0, SHARED_SIZE510, 0);
46755 }
46756#ifdef SQLITE_WIN32_HAS_ANSI
46757 else{
46758 res = winUnlockFile(&pFile->h, SHARED_FIRST(sqlite3PendingByte+2)+pFile->sharedLockByte, 0, 1, 0);
46759 }
46760#endif
46761 if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
46762 pFile->lastErrno = lastErrno;
46763 winLogError(SQLITE_IOERR_UNLOCK(10 | (8<<8)), pFile->lastErrno,
46764 "winUnlockReadLock", pFile->zPath);
46765 }
46766 OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
46767 return res;
46768}
46769
46770/*
46771** Lock the file with the lock specified by parameter locktype - one
46772** of the following:
46773**
46774** (1) SHARED_LOCK
46775** (2) RESERVED_LOCK
46776** (3) PENDING_LOCK
46777** (4) EXCLUSIVE_LOCK
46778**
46779** Sometimes when requesting one lock state, additional lock states
46780** are inserted in between. The locking might fail on one of the later
46781** transitions leaving the lock state different from what it started but
46782** still short of its goal. The following chart shows the allowed
46783** transitions and the inserted intermediate states:
46784**
46785** UNLOCKED -> SHARED
46786** SHARED -> RESERVED
46787** SHARED -> (PENDING) -> EXCLUSIVE
46788** RESERVED -> (PENDING) -> EXCLUSIVE
46789** PENDING -> EXCLUSIVE
46790**
46791** This routine will only increase a lock. The winUnlock() routine
46792** erases all locks at once and returns us immediately to locking level 0.
46793** It is not possible to lower the locking level one step at a time. You
46794** must go straight to locking level 0.
46795*/
46796static int winLock(sqlite3_file *id, int locktype){
46797 int rc = SQLITE_OK0; /* Return code from subroutines */
46798 int res = 1; /* Result of a Windows lock call */
46799 int newLocktype; /* Set pFile->locktype to this value before exiting */
46800 int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
46801 winFile *pFile = (winFile*)id;
46802 DWORD lastErrno = NO_ERROR;
46803
46804 assert( id!=0 )((void) (0));
46805 OSTRACE(("LOCK file=%p, oldLock=%d(%d), newLock=%d\n",
46806 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
46807
46808 /* If there is already a lock of this type or more restrictive on the
46809 ** OsFile, do nothing. Don't use the end_lock: exit path, as
46810 ** sqlite3OsEnterMutex() hasn't been called yet.
46811 */
46812 if( pFile->locktype>=locktype ){
46813 OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
46814 return SQLITE_OK0;
46815 }
46816
46817 /* Do not allow any kind of write-lock on a read-only database
46818 */
46819 if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK2 ){
46820 return SQLITE_IOERR_LOCK(10 | (15<<8));
46821 }
46822
46823 /* Make sure the locking sequence is correct
46824 */
46825 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK )((void) (0));
46826 assert( locktype!=PENDING_LOCK )((void) (0));
46827 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK )((void) (0));
46828
46829 /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
46830 ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of
46831 ** the PENDING_LOCK byte is temporary.
46832 */
46833 newLocktype = pFile->locktype;
46834 if( pFile->locktype==NO_LOCK0
46835 || (locktype==EXCLUSIVE_LOCK4 && pFile->locktype<=RESERVED_LOCK2)
46836 ){
46837 int cnt = 3;
46838 while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
46839 PENDING_BYTEsqlite3PendingByte, 0, 1, 0))==0 ){
46840 /* Try 3 times to get the pending lock. This is needed to work
46841 ** around problems caused by indexing and/or anti-virus software on
46842 ** Windows systems.
46843 ** If you are using this code as a model for alternative VFSes, do not
46844 ** copy this retry logic. It is a hack intended for Windows only.
46845 */
46846 lastErrno = osGetLastError();
46847 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
46848 pFile->h, cnt, res));
46849 if( lastErrno==ERROR_INVALID_HANDLE ){
46850 pFile->lastErrno = lastErrno;
46851 rc = SQLITE_IOERR_LOCK(10 | (15<<8));
46852 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
46853 pFile->h, cnt, sqlite3ErrName(rc)));
46854 return rc;
46855 }
46856 if( cnt ) sqlite3_win32_sleep(1);
46857 }
46858 gotPendingLock = res;
46859 if( !res ){
46860 lastErrno = osGetLastError();
46861 }
46862 }
46863
46864 /* Acquire a shared lock
46865 */
46866 if( locktype==SHARED_LOCK1 && res ){
46867 assert( pFile->locktype==NO_LOCK )((void) (0));
46868 res = winGetReadLock(pFile);
46869 if( res ){
46870 newLocktype = SHARED_LOCK1;
46871 }else{
46872 lastErrno = osGetLastError();
46873 }
46874 }
46875
46876 /* Acquire a RESERVED lock
46877 */
46878 if( locktype==RESERVED_LOCK2 && res ){
46879 assert( pFile->locktype==SHARED_LOCK )((void) (0));
46880 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE(sqlite3PendingByte+1), 0, 1, 0);
46881 if( res ){
46882 newLocktype = RESERVED_LOCK2;
46883 }else{
46884 lastErrno = osGetLastError();
46885 }
46886 }
46887
46888 /* Acquire a PENDING lock
46889 */
46890 if( locktype==EXCLUSIVE_LOCK4 && res ){
46891 newLocktype = PENDING_LOCK3;
46892 gotPendingLock = 0;
46893 }
46894
46895 /* Acquire an EXCLUSIVE lock
46896 */
46897 if( locktype==EXCLUSIVE_LOCK4 && res ){
46898 assert( pFile->locktype>=SHARED_LOCK )((void) (0));
46899 res = winUnlockReadLock(pFile);
46900 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST(sqlite3PendingByte+2), 0,
46901 SHARED_SIZE510, 0);
46902 if( res ){
46903 newLocktype = EXCLUSIVE_LOCK4;
46904 }else{
46905 lastErrno = osGetLastError();
46906 winGetReadLock(pFile);
46907 }
46908 }
46909
46910 /* If we are holding a PENDING lock that ought to be released, then
46911 ** release it now.
46912 */
46913 if( gotPendingLock && locktype==SHARED_LOCK1 ){
46914 winUnlockFile(&pFile->h, PENDING_BYTEsqlite3PendingByte, 0, 1, 0);
46915 }
46916
46917 /* Update the state of the lock has held in the file descriptor then
46918 ** return the appropriate result code.
46919 */
46920 if( res ){
46921 rc = SQLITE_OK0;
46922 }else{
46923 pFile->lastErrno = lastErrno;
46924 rc = SQLITE_BUSY5;
46925 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
46926 pFile->h, locktype, newLocktype));
46927 }
46928 pFile->locktype = (u8)newLocktype;
46929 OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n",
46930 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
46931 return rc;
46932}
46933
46934/*
46935** This routine checks if there is a RESERVED lock held on the specified
46936** file by this or any other process. If such a lock is held, return
46937** non-zero, otherwise zero.
46938*/
46939static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
46940 int res;
46941 winFile *pFile = (winFile*)id;
46942
46943 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
46944 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
46945
46946 assert( id!=0 )((void) (0));
46947 if( pFile->locktype>=RESERVED_LOCK2 ){
46948 res = 1;
46949 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
46950 }else{
46951 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE(sqlite3PendingByte+1),0,1,0);
46952 if( res ){
46953 winUnlockFile(&pFile->h, RESERVED_BYTE(sqlite3PendingByte+1), 0, 1, 0);
46954 }
46955 res = !res;
46956 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
46957 }
46958 *pResOut = res;
46959 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
46960 pFile->h, pResOut, *pResOut));
46961 return SQLITE_OK0;
46962}
46963
46964/*
46965** Lower the locking level on file descriptor id to locktype. locktype
46966** must be either NO_LOCK or SHARED_LOCK.
46967**
46968** If the locking level of the file descriptor is already at or below
46969** the requested locking level, this routine is a no-op.
46970**
46971** It is not possible for this routine to fail if the second argument
46972** is NO_LOCK. If the second argument is SHARED_LOCK then this routine
46973** might return SQLITE_IOERR;
46974*/
46975static int winUnlock(sqlite3_file *id, int locktype){
46976 int type;
46977 winFile *pFile = (winFile*)id;
46978 int rc = SQLITE_OK0;
46979 assert( pFile!=0 )((void) (0));
46980 assert( locktype<=SHARED_LOCK )((void) (0));
46981 OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
46982 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
46983 type = pFile->locktype;
46984 if( type>=EXCLUSIVE_LOCK4 ){
46985 winUnlockFile(&pFile->h, SHARED_FIRST(sqlite3PendingByte+2), 0, SHARED_SIZE510, 0);
46986 if( locktype==SHARED_LOCK1 && !winGetReadLock(pFile) ){
46987 /* This should never happen. We should always be able to
46988 ** reacquire the read lock */
46989 rc = winLogError(SQLITE_IOERR_UNLOCK(10 | (8<<8)), osGetLastError(),
46990 "winUnlock", pFile->zPath);
46991 }
46992 }
46993 if( type>=RESERVED_LOCK2 ){
46994 winUnlockFile(&pFile->h, RESERVED_BYTE(sqlite3PendingByte+1), 0, 1, 0);
46995 }
46996 if( locktype==NO_LOCK0 && type>=SHARED_LOCK1 ){
46997 winUnlockReadLock(pFile);
46998 }
46999 if( type>=PENDING_LOCK3 ){
47000 winUnlockFile(&pFile->h, PENDING_BYTEsqlite3PendingByte, 0, 1, 0);
47001 }
47002 pFile->locktype = (u8)locktype;
47003 OSTRACE(("UNLOCK file=%p, lock=%d, rc=%s\n",
47004 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
47005 return rc;
47006}
47007
47008/******************************************************************************
47009****************************** No-op Locking **********************************
47010**
47011** Of the various locking implementations available, this is by far the
47012** simplest: locking is ignored. No attempt is made to lock the database
47013** file for reading or writing.
47014**
47015** This locking mode is appropriate for use on read-only databases
47016** (ex: databases that are burned into CD-ROM, for example.) It can
47017** also be used if the application employs some external mechanism to
47018** prevent simultaneous access of the same database by two or more
47019** database connections. But there is a serious risk of database
47020** corruption if this locking mode is used in situations where multiple
47021** database connections are accessing the same database file at the same
47022** time and one or more of those connections are writing.
47023*/
47024
47025static int winNolockLock(sqlite3_file *id, int locktype){
47026 UNUSED_PARAMETER(id)(void)(id);
47027 UNUSED_PARAMETER(locktype)(void)(locktype);
47028 return SQLITE_OK0;
47029}
47030
47031static int winNolockCheckReservedLock(sqlite3_file *id, int *pResOut){
47032 UNUSED_PARAMETER(id)(void)(id);
47033 UNUSED_PARAMETER(pResOut)(void)(pResOut);
47034 return SQLITE_OK0;
47035}
47036
47037static int winNolockUnlock(sqlite3_file *id, int locktype){
47038 UNUSED_PARAMETER(id)(void)(id);
47039 UNUSED_PARAMETER(locktype)(void)(locktype);
47040 return SQLITE_OK0;
47041}
47042
47043/******************* End of the no-op lock implementation *********************
47044******************************************************************************/
47045
47046/*
47047** If *pArg is initially negative then this is a query. Set *pArg to
47048** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
47049**
47050** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
47051*/
47052static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
47053 if( *pArg<0 ){
47054 *pArg = (pFile->ctrlFlags & mask)!=0;
47055 }else if( (*pArg)==0 ){
47056 pFile->ctrlFlags &= ~mask;
47057 }else{
47058 pFile->ctrlFlags |= mask;
47059 }
47060}
47061
47062/* Forward references to VFS helper methods used for temporary files */
47063static int winGetTempname(sqlite3_vfs *, char **);
47064static int winIsDir(const void *);
47065static BOOL winIsLongPathPrefix(const char *);
47066static BOOL winIsDriveLetterAndColon(const char *);
47067
47068/*
47069** Control and query of the open file handle.
47070*/
47071static int winFileControl(sqlite3_file *id, int op, void *pArg){
47072 winFile *pFile = (winFile*)id;
47073 OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
47074 switch( op ){
47075 case SQLITE_FCNTL_LOCKSTATE1: {
47076 *(int*)pArg = pFile->locktype;
47077 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47078 return SQLITE_OK0;
47079 }
47080 case SQLITE_FCNTL_LAST_ERRNO4: {
47081 *(int*)pArg = (int)pFile->lastErrno;
47082 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47083 return SQLITE_OK0;
47084 }
47085 case SQLITE_FCNTL_CHUNK_SIZE6: {
47086 pFile->szChunk = *(int *)pArg;
47087 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47088 return SQLITE_OK0;
47089 }
47090 case SQLITE_FCNTL_SIZE_HINT5: {
47091 if( pFile->szChunk>0 ){
47092 sqlite3_int64 oldSz;
47093 int rc = winFileSize(id, &oldSz);
47094 if( rc==SQLITE_OK0 ){
47095 sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
47096 if( newSz>oldSz ){
47097 SimulateIOErrorBenign(1);
47098 rc = winTruncate(id, newSz);
47099 SimulateIOErrorBenign(0);
47100 }
47101 }
47102 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
47103 return rc;
47104 }
47105 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47106 return SQLITE_OK0;
47107 }
47108 case SQLITE_FCNTL_PERSIST_WAL10: {
47109 winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg);
47110 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47111 return SQLITE_OK0;
47112 }
47113 case SQLITE_FCNTL_POWERSAFE_OVERWRITE13: {
47114 winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
47115 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47116 return SQLITE_OK0;
47117 }
47118 case SQLITE_FCNTL_VFSNAME12: {
47119 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
47120 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47121 return SQLITE_OK0;
47122 }
47123 case SQLITE_FCNTL_WIN32_AV_RETRY9: {
47124 int *a = (int*)pArg;
47125 if( a[0]>0 ){
47126 winIoerrRetry = a[0];
47127 }else{
47128 a[0] = winIoerrRetry;
47129 }
47130 if( a[1]>0 ){
47131 winIoerrRetryDelay = a[1];
47132 }else{
47133 a[1] = winIoerrRetryDelay;
47134 }
47135 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47136 return SQLITE_OK0;
47137 }
47138 case SQLITE_FCNTL_WIN32_GET_HANDLE29: {
47139 LPHANDLE phFile = (LPHANDLE)pArg;
47140 *phFile = pFile->h;
47141 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47142 return SQLITE_OK0;
47143 }
47144#ifdef SQLITE_TEST
47145 case SQLITE_FCNTL_WIN32_SET_HANDLE23: {
47146 LPHANDLE phFile = (LPHANDLE)pArg;
47147 HANDLE hOldFile = pFile->h;
47148 pFile->h = *phFile;
47149 *phFile = hOldFile;
47150 OSTRACE(("FCNTL oldFile=%p, newFile=%p, rc=SQLITE_OK\n",
47151 hOldFile, pFile->h));
47152 return SQLITE_OK0;
47153 }
47154#endif
47155 case SQLITE_FCNTL_TEMPFILENAME16: {
47156 char *zTFile = 0;
47157 int rc = winGetTempname(pFile->pVfs, &zTFile);
47158 if( rc==SQLITE_OK0 ){
47159 *(char**)pArg = zTFile;
47160 }
47161 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
47162 return rc;
47163 }
47164#if SQLITE_MAX_MMAP_SIZE20971520>0
47165 case SQLITE_FCNTL_MMAP_SIZE18: {
47166 i64 newLimit = *(i64*)pArg;
47167 int rc = SQLITE_OK0;
47168 if( newLimit>sqlite3GlobalConfigsqlite3Config.mxMmap ){
47169 newLimit = sqlite3GlobalConfigsqlite3Config.mxMmap;
47170 }
47171
47172 /* The value of newLimit may be eventually cast to (SIZE_T) and passed
47173 ** to MapViewOfFile(). Restrict its value to 2GB if (SIZE_T) is not at
47174 ** least a 64-bit type. */
47175 if( newLimit>0 && sizeof(SIZE_T)<8 ){
47176 newLimit = (newLimit & 0x7FFFFFFF);
47177 }
47178
47179 *(i64*)pArg = pFile->mmapSizeMax;
47180 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
47181 pFile->mmapSizeMax = newLimit;
47182 if( pFile->mmapSize>0 ){
47183 winUnmapfile(pFile);
47184 rc = winMapfile(pFile, -1);
47185 }
47186 }
47187 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
47188 return rc;
47189 }
47190#endif
47191 }
47192 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
47193 return SQLITE_NOTFOUND12;
47194}
47195
47196/*
47197** Return the sector size in bytes of the underlying block device for
47198** the specified file. This is almost always 512 bytes, but may be
47199** larger for some devices.
47200**
47201** SQLite code assumes this function cannot fail. It also assumes that
47202** if two files are created in the same file-system directory (i.e.
47203** a database and its journal file) that the sector size will be the
47204** same for both.
47205*/
47206static int winSectorSize(sqlite3_file *id){
47207 (void)id;
47208 return SQLITE_DEFAULT_SECTOR_SIZE4096;
47209}
47210
47211/*
47212** Return a vector of device characteristics.
47213*/
47214static int winDeviceCharacteristics(sqlite3_file *id){
47215 winFile *p = (winFile*)id;
47216 return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN0x00000800 |
47217 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE0x00001000:0);
47218}
47219
47220/*
47221** Windows will only let you create file view mappings
47222** on allocation size granularity boundaries.
47223** During sqlite3_os_init() we do a GetSystemInfo()
47224** to get the granularity size.
47225*/
47226static SYSTEM_INFO winSysInfo;
47227
47228#ifndef SQLITE_OMIT_WAL
47229
47230/*
47231** Helper functions to obtain and relinquish the global mutex. The
47232** global mutex is used to protect the winLockInfo objects used by
47233** this file, all of which may be shared by multiple threads.
47234**
47235** Function winShmMutexHeld() is used to assert() that the global mutex
47236** is held when required. This function is only used as part of assert()
47237** statements. e.g.
47238**
47239** winShmEnterMutex()
47240** assert( winShmMutexHeld() );
47241** winShmLeaveMutex()
47242*/
47243static sqlite3_mutex *winBigLock = 0;
47244static void winShmEnterMutex(void){
47245 sqlite3_mutex_enter(winBigLock);
47246}
47247static void winShmLeaveMutex(void){
47248 sqlite3_mutex_leave(winBigLock);
47249}
47250#ifndef NDEBUG1
47251static int winShmMutexHeld(void) {
47252 return sqlite3_mutex_held(winBigLock);
47253}
47254#endif
47255
47256/*
47257** Object used to represent a single file opened and mmapped to provide
47258** shared memory. When multiple threads all reference the same
47259** log-summary, each thread has its own winFile object, but they all
47260** point to a single instance of this object. In other words, each
47261** log-summary is opened only once per process.
47262**
47263** winShmMutexHeld() must be true when creating or destroying
47264** this object or while reading or writing the following fields:
47265**
47266** nRef
47267** pNext
47268**
47269** The following fields are read-only after the object is created:
47270**
47271** fid
47272** zFilename
47273**
47274** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
47275** winShmMutexHeld() is true when reading or writing any other field
47276** in this structure.
47277**
47278*/
47279struct winShmNode {
47280 sqlite3_mutex *mutex; /* Mutex to access this object */
47281 char *zFilename; /* Name of the file */
47282 winFile hFile; /* File handle from winOpen */
47283
47284 int szRegion; /* Size of shared-memory regions */
47285 int nRegion; /* Size of array apRegion */
47286 u8 isReadonly; /* True if read-only */
47287 u8 isUnlocked; /* True if no DMS lock held */
47288
47289 struct ShmRegion {
47290 HANDLE hMap; /* File handle from CreateFileMapping */
47291 void *pMap;
47292 } *aRegion;
47293 DWORD lastErrno; /* The Windows errno from the last I/O error */
47294
47295 int nRef; /* Number of winShm objects pointing to this */
47296 winShm *pFirst; /* All winShm objects pointing to this */
47297 winShmNode *pNext; /* Next in list of all winShmNode objects */
47298#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
47299 u8 nextShmId; /* Next available winShm.id value */
47300#endif
47301};
47302
47303/*
47304** A global array of all winShmNode objects.
47305**
47306** The winShmMutexHeld() must be true while reading or writing this list.
47307*/
47308static winShmNode *winShmNodeList = 0;
47309
47310/*
47311** Structure used internally by this VFS to record the state of an
47312** open shared memory connection.
47313**
47314** The following fields are initialized when this object is created and
47315** are read-only thereafter:
47316**
47317** winShm.pShmNode
47318** winShm.id
47319**
47320** All other fields are read/write. The winShm.pShmNode->mutex must be held
47321** while accessing any read/write fields.
47322*/
47323struct winShm {
47324 winShmNode *pShmNode; /* The underlying winShmNode object */
47325 winShm *pNext; /* Next winShm with the same winShmNode */
47326 u8 hasMutex; /* True if holding the winShmNode mutex */
47327 u16 sharedMask; /* Mask of shared locks held */
47328 u16 exclMask; /* Mask of exclusive locks held */
47329#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
47330 u8 id; /* Id of this connection with its winShmNode */
47331#endif
47332};
47333
47334/*
47335** Constants used for locking
47336*/
47337#define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK8)*4) /* first lock byte */
47338#define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK8) /* deadman switch */
47339
47340/*
47341** Apply advisory locks for all n bytes beginning at ofst.
47342*/
47343#define WINSHM_UNLCK 1
47344#define WINSHM_RDLCK 2
47345#define WINSHM_WRLCK 3
47346static int winShmSystemLock(
47347 winShmNode *pFile, /* Apply locks to this open shared-memory segment */
47348 int lockType, /* WINSHM_UNLCK, WINSHM_RDLCK, or WINSHM_WRLCK */
47349 int ofst, /* Offset to first byte to be locked/unlocked */
47350 int nByte /* Number of bytes to lock or unlock */
47351){
47352 int rc = 0; /* Result code form Lock/UnlockFileEx() */
47353
47354 /* Access to the winShmNode object is serialized by the caller */
47355 assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) )((void) (0));
47356
47357 OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
47358 pFile->hFile.h, lockType, ofst, nByte));
47359
47360 /* Release/Acquire the system-level lock */
47361 if( lockType==WINSHM_UNLCK ){
47362 rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
47363 }else{
47364 /* Initialize the locking parameters */
47365 DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
47366 if( lockType == WINSHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
47367 rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
47368 }
47369
47370 if( rc!= 0 ){
47371 rc = SQLITE_OK0;
47372 }else{
47373 pFile->lastErrno = osGetLastError();
47374 rc = SQLITE_BUSY5;
47375 }
47376
47377 OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
47378 pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
47379 "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
47380
47381 return rc;
47382}
47383
47384/* Forward references to VFS methods */
47385static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
47386static int winDelete(sqlite3_vfs *,const char*,int);
47387
47388/*
47389** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
47390**
47391** This is not a VFS shared-memory method; it is a utility function called
47392** by VFS shared-memory methods.
47393*/
47394static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
47395 winShmNode **pp;
47396 winShmNode *p;
47397 assert( winShmMutexHeld() )((void) (0));
47398 OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
47399 osGetCurrentProcessId(), deleteFlag));
47400 pp = &winShmNodeList;
47401 while( (p = *pp)!=0 ){
47402 if( p->nRef==0 ){
47403 int i;
47404 if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
47405 for(i=0; i<p->nRegion; i++){
47406 BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
47407 OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
47408 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
47409 UNUSED_VARIABLE_VALUE(bRc);
47410 bRc = osCloseHandle(p->aRegion[i].hMap);
47411 OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
47412 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
47413 UNUSED_VARIABLE_VALUE(bRc);
47414 }
47415 if( p->hFile.h!=NULL((void*)0) && p->hFile.h!=INVALID_HANDLE_VALUE ){
47416 SimulateIOErrorBenign(1);
47417 winClose((sqlite3_file *)&p->hFile);
47418 SimulateIOErrorBenign(0);
47419 }
47420 if( deleteFlag ){
47421 SimulateIOErrorBenign(1);
47422 sqlite3BeginBenignMalloc();
47423 winDelete(pVfs, p->zFilename, 0);
47424 sqlite3EndBenignMalloc();
47425 SimulateIOErrorBenign(0);
47426 }
47427 *pp = p->pNext;
47428 sqlite3_free(p->aRegion);
47429 sqlite3_free(p);
47430 }else{
47431 pp = &p->pNext;
47432 }
47433 }
47434}
47435
47436/*
47437** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
47438** take it now. Return SQLITE_OK if successful, or an SQLite error
47439** code otherwise.
47440**
47441** If the DMS cannot be locked because this is a readonly_shm=1
47442** connection and no other process already holds a lock, return
47443** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
47444*/
47445static int winLockSharedMemory(winShmNode *pShmNode){
47446 int rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, WIN_SHM_DMS, 1);
47447
47448 if( rc==SQLITE_OK0 ){
47449 if( pShmNode->isReadonly ){
47450 pShmNode->isUnlocked = 1;
47451 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
47452 return SQLITE_READONLY_CANTINIT(8 | (5<<8));
47453 }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){
47454 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
47455 return winLogError(SQLITE_IOERR_SHMOPEN(10 | (18<<8)), osGetLastError(),
47456 "winLockSharedMemory", pShmNode->zFilename);
47457 }
47458 }
47459
47460 if( rc==SQLITE_OK0 ){
47461 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
47462 }
47463
47464 return winShmSystemLock(pShmNode, WINSHM_RDLCK, WIN_SHM_DMS, 1);
47465}
47466
47467/*
47468** Open the shared-memory area associated with database file pDbFd.
47469**
47470** When opening a new shared-memory file, if no other instances of that
47471** file are currently open, in this process or in other processes, then
47472** the file must be truncated to zero length or have its header cleared.
47473*/
47474static int winOpenSharedMemory(winFile *pDbFd){
47475 struct winShm *p; /* The connection to be opened */
47476 winShmNode *pShmNode = 0; /* The underlying mmapped file */
47477 int rc = SQLITE_OK0; /* Result code */
47478 winShmNode *pNew; /* Newly allocated winShmNode */
47479 int nName; /* Size of zName in bytes */
47480
47481 assert( pDbFd->pShm==0 )((void) (0)); /* Not previously opened */
47482
47483 /* Allocate space for the new sqlite3_shm object. Also speculatively
47484 ** allocate space for a new winShmNode and filename.
47485 */
47486 p = sqlite3MallocZero( sizeof(*p) );
47487 if( p==0 ) return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
47488 nName = sqlite3Strlen30(pDbFd->zPath);
47489 pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
47490 if( pNew==0 ){
47491 sqlite3_free(p);
47492 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
47493 }
47494 pNew->zFilename = (char*)&pNew[1];
47495 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
47496 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
47497
47498 /* Look to see if there is an existing winShmNode that can be used.
47499 ** If no matching winShmNode currently exists, create a new one.
47500 */
47501 winShmEnterMutex();
47502 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
47503 /* TBD need to come up with better match here. Perhaps
47504 ** use FILE_ID_BOTH_DIR_INFO Structure.
47505 */
47506 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
47507 }
47508 if( pShmNode ){
47509 sqlite3_free(pNew);
47510 }else{
47511 int inFlags = SQLITE_OPEN_WAL0x00080000;
47512 int outFlags = 0;
47513
47514 pShmNode = pNew;
47515 pNew = 0;
47516 ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
47517 pShmNode->pNext = winShmNodeList;
47518 winShmNodeList = pShmNode;
47519
47520 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
47521 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST0);
47522 if( pShmNode->mutex==0 ){
47523 rc = SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
47524 goto shm_open_err;
47525 }
47526 }
47527
47528 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
47529 inFlags |= SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004;
47530 }else{
47531 inFlags |= SQLITE_OPEN_READONLY0x00000001;
47532 }
47533 rc = winOpen(pDbFd->pVfs, pShmNode->zFilename,
47534 (sqlite3_file*)&pShmNode->hFile,
47535 inFlags, &outFlags);
47536 if( rc!=SQLITE_OK0 ){
47537 rc = winLogError(rc, osGetLastError(), "winOpenShm",
47538 pShmNode->zFilename);
47539 goto shm_open_err;
47540 }
47541 if( outFlags==SQLITE_OPEN_READONLY0x00000001 ) pShmNode->isReadonly = 1;
47542
47543 rc = winLockSharedMemory(pShmNode);
47544 if( rc!=SQLITE_OK0 && rc!=SQLITE_READONLY_CANTINIT(8 | (5<<8)) ) goto shm_open_err;
47545 }
47546
47547 /* Make the new connection a child of the winShmNode */
47548 p->pShmNode = pShmNode;
47549#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
47550 p->id = pShmNode->nextShmId++;
47551#endif
47552 pShmNode->nRef++;
47553 pDbFd->pShm = p;
47554 winShmLeaveMutex();
47555
47556 /* The reference count on pShmNode has already been incremented under
47557 ** the cover of the winShmEnterMutex() mutex and the pointer from the
47558 ** new (struct winShm) object to the pShmNode has been set. All that is
47559 ** left to do is to link the new object into the linked list starting
47560 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
47561 ** mutex.
47562 */
47563 sqlite3_mutex_enter(pShmNode->mutex);
47564 p->pNext = pShmNode->pFirst;
47565 pShmNode->pFirst = p;
47566 sqlite3_mutex_leave(pShmNode->mutex);
47567 return rc;
47568
47569 /* Jump here on any error */
47570shm_open_err:
47571 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
47572 winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
47573 sqlite3_free(p);
47574 sqlite3_free(pNew);
47575 winShmLeaveMutex();
47576 return rc;
47577}
47578
47579/*
47580** Close a connection to shared-memory. Delete the underlying
47581** storage if deleteFlag is true.
47582*/
47583static int winShmUnmap(
47584 sqlite3_file *fd, /* Database holding shared memory */
47585 int deleteFlag /* Delete after closing if true */
47586){
47587 winFile *pDbFd; /* Database holding shared-memory */
47588 winShm *p; /* The connection to be closed */
47589 winShmNode *pShmNode; /* The underlying shared-memory file */
47590 winShm **pp; /* For looping over sibling connections */
47591
47592 pDbFd = (winFile*)fd;
47593 p = pDbFd->pShm;
47594 if( p==0 ) return SQLITE_OK0;
47595 pShmNode = p->pShmNode;
47596
47597 /* Remove connection p from the set of connections associated
47598 ** with pShmNode */
47599 sqlite3_mutex_enter(pShmNode->mutex);
47600 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
47601 *pp = p->pNext;
47602
47603 /* Free the connection p */
47604 sqlite3_free(p);
47605 pDbFd->pShm = 0;
47606 sqlite3_mutex_leave(pShmNode->mutex);
47607
47608 /* If pShmNode->nRef has reached 0, then close the underlying
47609 ** shared-memory file, too */
47610 winShmEnterMutex();
47611 assert( pShmNode->nRef>0 )((void) (0));
47612 pShmNode->nRef--;
47613 if( pShmNode->nRef==0 ){
47614 winShmPurge(pDbFd->pVfs, deleteFlag);
47615 }
47616 winShmLeaveMutex();
47617
47618 return SQLITE_OK0;
47619}
47620
47621/*
47622** Change the lock state for a shared-memory segment.
47623*/
47624static int winShmLock(
47625 sqlite3_file *fd, /* Database file holding the shared memory */
47626 int ofst, /* First lock to acquire or release */
47627 int n, /* Number of locks to acquire or release */
47628 int flags /* What to do with the lock */
47629){
47630 winFile *pDbFd = (winFile*)fd; /* Connection holding shared memory */
47631 winShm *p = pDbFd->pShm; /* The shared memory being locked */
47632 winShm *pX; /* For looping over all siblings */
47633 winShmNode *pShmNode;
47634 int rc = SQLITE_OK0; /* Result code */
47635 u16 mask; /* Mask of locks to take or release */
47636
47637 if( p==0 ) return SQLITE_IOERR_SHMLOCK(10 | (20<<8));
47638 pShmNode = p->pShmNode;
47639 if( NEVER(pShmNode==0)(pShmNode==0) ) return SQLITE_IOERR_SHMLOCK(10 | (20<<8));
47640
47641 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK )((void) (0));
47642 assert( n>=1 )((void) (0));
47643 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)((void) (0))
47644 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)((void) (0))
47645 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)((void) (0))
47646 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) )((void) (0));
47647 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 )((void) (0));
47648
47649 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
47650 assert( n>1 || mask==(1<<ofst) )((void) (0));
47651 sqlite3_mutex_enter(pShmNode->mutex);
47652 if( flags & SQLITE_SHM_UNLOCK1 ){
47653 u16 allMask = 0; /* Mask of locks held by siblings */
47654
47655 /* See if any siblings hold this same lock */
47656 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
47657 if( pX==p ) continue;
47658 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 )((void) (0));
47659 allMask |= pX->sharedMask;
47660 }
47661
47662 /* Unlock the system-level locks */
47663 if( (mask & allMask)==0 ){
47664 rc = winShmSystemLock(pShmNode, WINSHM_UNLCK, ofst+WIN_SHM_BASE, n);
47665 }else{
47666 rc = SQLITE_OK0;
47667 }
47668
47669 /* Undo the local locks */
47670 if( rc==SQLITE_OK0 ){
47671 p->exclMask &= ~mask;
47672 p->sharedMask &= ~mask;
47673 }
47674 }else if( flags & SQLITE_SHM_SHARED4 ){
47675 u16 allShared = 0; /* Union of locks held by connections other than "p" */
47676
47677 /* Find out which shared locks are already held by sibling connections.
47678 ** If any sibling already holds an exclusive lock, go ahead and return
47679 ** SQLITE_BUSY.
47680 */
47681 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
47682 if( (pX->exclMask & mask)!=0 ){
47683 rc = SQLITE_BUSY5;
47684 break;
47685 }
47686 allShared |= pX->sharedMask;
47687 }
47688
47689 /* Get shared locks at the system level, if necessary */
47690 if( rc==SQLITE_OK0 ){
47691 if( (allShared & mask)==0 ){
47692 rc = winShmSystemLock(pShmNode, WINSHM_RDLCK, ofst+WIN_SHM_BASE, n);
47693 }else{
47694 rc = SQLITE_OK0;
47695 }
47696 }
47697
47698 /* Get the local shared locks */
47699 if( rc==SQLITE_OK0 ){
47700 p->sharedMask |= mask;
47701 }
47702 }else{
47703 /* Make sure no sibling connections hold locks that will block this
47704 ** lock. If any do, return SQLITE_BUSY right away.
47705 */
47706 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
47707 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
47708 rc = SQLITE_BUSY5;
47709 break;
47710 }
47711 }
47712
47713 /* Get the exclusive locks at the system level. Then if successful
47714 ** also mark the local connection as being locked.
47715 */
47716 if( rc==SQLITE_OK0 ){
47717 rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, ofst+WIN_SHM_BASE, n);
47718 if( rc==SQLITE_OK0 ){
47719 assert( (p->sharedMask & mask)==0 )((void) (0));
47720 p->exclMask |= mask;
47721 }
47722 }
47723 }
47724 sqlite3_mutex_leave(pShmNode->mutex);
47725 OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
47726 osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
47727 sqlite3ErrName(rc)));
47728 return rc;
47729}
47730
47731/*
47732** Implement a memory barrier or memory fence on shared memory.
47733**
47734** All loads and stores begun before the barrier must complete before
47735** any load or store begun after the barrier.
47736*/
47737static void winShmBarrier(
47738 sqlite3_file *fd /* Database holding the shared memory */
47739){
47740 UNUSED_PARAMETER(fd)(void)(fd);
47741 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
47742 winShmEnterMutex(); /* Also mutex, for redundancy */
47743 winShmLeaveMutex();
47744}
47745
47746/*
47747** This function is called to obtain a pointer to region iRegion of the
47748** shared-memory associated with the database file fd. Shared-memory regions
47749** are numbered starting from zero. Each shared-memory region is szRegion
47750** bytes in size.
47751**
47752** If an error occurs, an error code is returned and *pp is set to NULL.
47753**
47754** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
47755** region has not been allocated (by any client, including one running in a
47756** separate process), then *pp is set to NULL and SQLITE_OK returned. If
47757** isWrite is non-zero and the requested shared-memory region has not yet
47758** been allocated, it is allocated by this function.
47759**
47760** If the shared-memory region has already been allocated or is allocated by
47761** this call as described above, then it is mapped into this processes
47762** address space (if it is not already), *pp is set to point to the mapped
47763** memory and SQLITE_OK returned.
47764*/
47765static int winShmMap(
47766 sqlite3_file *fd, /* Handle open on database file */
47767 int iRegion, /* Region to retrieve */
47768 int szRegion, /* Size of regions */
47769 int isWrite, /* True to extend file if necessary */
47770 void volatile **pp /* OUT: Mapped memory */
47771){
47772 winFile *pDbFd = (winFile*)fd;
47773 winShm *pShm = pDbFd->pShm;
47774 winShmNode *pShmNode;
47775 DWORD protect = PAGE_READWRITE;
47776 DWORD flags = FILE_MAP_WRITE | FILE_MAP_READ;
47777 int rc = SQLITE_OK0;
47778
47779 if( !pShm ){
47780 rc = winOpenSharedMemory(pDbFd);
47781 if( rc!=SQLITE_OK0 ) return rc;
47782 pShm = pDbFd->pShm;
47783 assert( pShm!=0 )((void) (0));
47784 }
47785 pShmNode = pShm->pShmNode;
47786
47787 sqlite3_mutex_enter(pShmNode->mutex);
47788 if( pShmNode->isUnlocked ){
47789 rc = winLockSharedMemory(pShmNode);
47790 if( rc!=SQLITE_OK0 ) goto shmpage_out;
47791 pShmNode->isUnlocked = 0;
47792 }
47793 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 )((void) (0));
47794
47795 if( pShmNode->nRegion<=iRegion ){
47796 struct ShmRegion *apNew; /* New aRegion[] array */
47797 int nByte = (iRegion+1)*szRegion; /* Minimum required file size */
47798 sqlite3_int64 sz; /* Current size of wal-index file */
47799
47800 pShmNode->szRegion = szRegion;
47801
47802 /* The requested region is not mapped into this processes address space.
47803 ** Check to see if it has been allocated (i.e. if the wal-index file is
47804 ** large enough to contain the requested region).
47805 */
47806 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
47807 if( rc!=SQLITE_OK0 ){
47808 rc = winLogError(SQLITE_IOERR_SHMSIZE(10 | (19<<8)), osGetLastError(),
47809 "winShmMap1", pDbFd->zPath);
47810 goto shmpage_out;
47811 }
47812
47813 if( sz<nByte ){
47814 /* The requested memory region does not exist. If isWrite is set to
47815 ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
47816 **
47817 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
47818 ** the requested memory region.
47819 */
47820 if( !isWrite ) goto shmpage_out;
47821 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
47822 if( rc!=SQLITE_OK0 ){
47823 rc = winLogError(SQLITE_IOERR_SHMSIZE(10 | (19<<8)), osGetLastError(),
47824 "winShmMap2", pDbFd->zPath);
47825 goto shmpage_out;
47826 }
47827 }
47828
47829 /* Map the requested memory region into this processes address space. */
47830 apNew = (struct ShmRegion *)sqlite3_realloc64(
47831 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
47832 );
47833 if( !apNew ){
47834 rc = SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
47835 goto shmpage_out;
47836 }
47837 pShmNode->aRegion = apNew;
47838
47839 if( pShmNode->isReadonly ){
47840 protect = PAGE_READONLY;
47841 flags = FILE_MAP_READ;
47842 }
47843
47844 while( pShmNode->nRegion<=iRegion ){
47845 HANDLE hMap = NULL((void*)0); /* file-mapping handle */
47846 void *pMap = 0; /* Mapped memory region */
47847
47848#if SQLITE_OS_WINRT
47849 hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
47850 NULL((void*)0), protect, nByte, NULL((void*)0)
47851 );
47852#elif defined(SQLITE_WIN32_HAS_WIDE)
47853 hMap = osCreateFileMappingW(pShmNode->hFile.h,
47854 NULL((void*)0), protect, 0, nByte, NULL((void*)0)
47855 );
47856#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
47857 hMap = osCreateFileMappingA(pShmNode->hFile.h,
47858 NULL((void*)0), protect, 0, nByte, NULL((void*)0)
47859 );
47860#endif
47861 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
47862 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
47863 hMap ? "ok" : "failed"));
47864 if( hMap ){
47865 int iOffset = pShmNode->nRegion*szRegion;
47866 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
47867#if SQLITE_OS_WINRT
47868 pMap = osMapViewOfFileFromApp(hMap, flags,
47869 iOffset - iOffsetShift, szRegion + iOffsetShift
47870 );
47871#else
47872 pMap = osMapViewOfFile(hMap, flags,
47873 0, iOffset - iOffsetShift, szRegion + iOffsetShift
47874 );
47875#endif
47876 OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
47877 osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
47878 szRegion, pMap ? "ok" : "failed"));
47879 }
47880 if( !pMap ){
47881 pShmNode->lastErrno = osGetLastError();
47882 rc = winLogError(SQLITE_IOERR_SHMMAP(10 | (21<<8)), pShmNode->lastErrno,
47883 "winShmMap3", pDbFd->zPath);
47884 if( hMap ) osCloseHandle(hMap);
47885 goto shmpage_out;
47886 }
47887
47888 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
47889 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
47890 pShmNode->nRegion++;
47891 }
47892 }
47893
47894shmpage_out:
47895 if( pShmNode->nRegion>iRegion ){
47896 int iOffset = iRegion*szRegion;
47897 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
47898 char *p = (char *)pShmNode->aRegion[iRegion].pMap;
47899 *pp = (void *)&p[iOffsetShift];
47900 }else{
47901 *pp = 0;
47902 }
47903 if( pShmNode->isReadonly && rc==SQLITE_OK0 ) rc = SQLITE_READONLY8;
47904 sqlite3_mutex_leave(pShmNode->mutex);
47905 return rc;
47906}
47907
47908#else
47909# define winShmMap 0
47910# define winShmLock 0
47911# define winShmBarrier 0
47912# define winShmUnmap 0
47913#endif /* #ifndef SQLITE_OMIT_WAL */
47914
47915/*
47916** Cleans up the mapped region of the specified file, if any.
47917*/
47918#if SQLITE_MAX_MMAP_SIZE20971520>0
47919static int winUnmapfile(winFile *pFile){
47920 assert( pFile!=0 )((void) (0));
47921 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
47922 "mmapSize=%lld, mmapSizeMax=%lld\n",
47923 osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
47924 pFile->mmapSize, pFile->mmapSizeMax));
47925 if( pFile->pMapRegion ){
47926 if( !osUnmapViewOfFile(pFile->pMapRegion) ){
47927 pFile->lastErrno = osGetLastError();
47928 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
47929 "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile,
47930 pFile->pMapRegion));
47931 return winLogError(SQLITE_IOERR_MMAP(10 | (24<<8)), pFile->lastErrno,
47932 "winUnmapfile1", pFile->zPath);
47933 }
47934 pFile->pMapRegion = 0;
47935 pFile->mmapSize = 0;
47936 }
47937 if( pFile->hMap!=NULL((void*)0) ){
47938 if( !osCloseHandle(pFile->hMap) ){
47939 pFile->lastErrno = osGetLastError();
47940 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
47941 osGetCurrentProcessId(), pFile, pFile->hMap));
47942 return winLogError(SQLITE_IOERR_MMAP(10 | (24<<8)), pFile->lastErrno,
47943 "winUnmapfile2", pFile->zPath);
47944 }
47945 pFile->hMap = NULL((void*)0);
47946 }
47947 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
47948 osGetCurrentProcessId(), pFile));
47949 return SQLITE_OK0;
47950}
47951
47952/*
47953** Memory map or remap the file opened by file-descriptor pFd (if the file
47954** is already mapped, the existing mapping is replaced by the new). Or, if
47955** there already exists a mapping for this file, and there are still
47956** outstanding xFetch() references to it, this function is a no-op.
47957**
47958** If parameter nByte is non-negative, then it is the requested size of
47959** the mapping to create. Otherwise, if nByte is less than zero, then the
47960** requested size is the size of the file on disk. The actual size of the
47961** created mapping is either the requested size or the value configured
47962** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
47963**
47964** SQLITE_OK is returned if no error occurs (even if the mapping is not
47965** recreated as a result of outstanding references) or an SQLite error
47966** code otherwise.
47967*/
47968static int winMapfile(winFile *pFd, sqlite3_int64 nByte){
47969 sqlite3_int64 nMap = nByte;
47970 int rc;
47971
47972 assert( nMap>=0 || pFd->nFetchOut==0 )((void) (0));
47973 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
47974 osGetCurrentProcessId(), pFd, nByte));
47975
47976 if( pFd->nFetchOut>0 ) return SQLITE_OK0;
47977
47978 if( nMap<0 ){
47979 rc = winFileSize((sqlite3_file*)pFd, &nMap);
47980 if( rc ){
47981 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
47982 osGetCurrentProcessId(), pFd));
47983 return SQLITE_IOERR_FSTAT(10 | (7<<8));
47984 }
47985 }
47986 if( nMap>pFd->mmapSizeMax ){
47987 nMap = pFd->mmapSizeMax;
47988 }
47989 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
47990
47991 if( nMap==0 && pFd->mmapSize>0 ){
47992 winUnmapfile(pFd);
47993 }
47994 if( nMap!=pFd->mmapSize ){
47995 void *pNew = 0;
47996 DWORD protect = PAGE_READONLY;
47997 DWORD flags = FILE_MAP_READ;
47998
47999 winUnmapfile(pFd);
48000#ifdef SQLITE_MMAP_READWRITE
48001 if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
48002 protect = PAGE_READWRITE;
48003 flags |= FILE_MAP_WRITE;
48004 }
48005#endif
48006#if SQLITE_OS_WINRT
48007 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL((void*)0), protect, nMap, NULL((void*)0));
48008#elif defined(SQLITE_WIN32_HAS_WIDE)
48009 pFd->hMap = osCreateFileMappingW(pFd->h, NULL((void*)0), protect,
48010 (DWORD)((nMap>>32) & 0xffffffff),
48011 (DWORD)(nMap & 0xffffffff), NULL((void*)0));
48012#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
48013 pFd->hMap = osCreateFileMappingA(pFd->h, NULL((void*)0), protect,
48014 (DWORD)((nMap>>32) & 0xffffffff),
48015 (DWORD)(nMap & 0xffffffff), NULL((void*)0));
48016#endif
48017 if( pFd->hMap==NULL((void*)0) ){
48018 pFd->lastErrno = osGetLastError();
48019 rc = winLogError(SQLITE_IOERR_MMAP(10 | (24<<8)), pFd->lastErrno,
48020 "winMapfile1", pFd->zPath);
48021 /* Log the error, but continue normal operation using xRead/xWrite */
48022 OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
48023 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
48024 return SQLITE_OK0;
48025 }
48026 assert( (nMap % winSysInfo.dwPageSize)==0 )((void) (0));
48027 assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff )((void) (0));
48028#if SQLITE_OS_WINRT
48029 pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
48030#else
48031 pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
48032#endif
48033 if( pNew==NULL((void*)0) ){
48034 osCloseHandle(pFd->hMap);
48035 pFd->hMap = NULL((void*)0);
48036 pFd->lastErrno = osGetLastError();
48037 rc = winLogError(SQLITE_IOERR_MMAP(10 | (24<<8)), pFd->lastErrno,
48038 "winMapfile2", pFd->zPath);
48039 /* Log the error, but continue normal operation using xRead/xWrite */
48040 OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
48041 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
48042 return SQLITE_OK0;
48043 }
48044 pFd->pMapRegion = pNew;
48045 pFd->mmapSize = nMap;
48046 }
48047
48048 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
48049 osGetCurrentProcessId(), pFd));
48050 return SQLITE_OK0;
48051}
48052#endif /* SQLITE_MAX_MMAP_SIZE>0 */
48053
48054/*
48055** If possible, return a pointer to a mapping of file fd starting at offset
48056** iOff. The mapping must be valid for at least nAmt bytes.
48057**
48058** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
48059** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
48060** Finally, if an error does occur, return an SQLite error code. The final
48061** value of *pp is undefined in this case.
48062**
48063** If this function does return a pointer, the caller must eventually
48064** release the reference by calling winUnfetch().
48065*/
48066static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
48067#if SQLITE_MAX_MMAP_SIZE20971520>0
48068 winFile *pFd = (winFile*)fd; /* The underlying database file */
48069#endif
48070 *pp = 0;
48071
48072 OSTRACE(("FETCH pid=%lu, pFile=%p, offset=%lld, amount=%d, pp=%p\n",
48073 osGetCurrentProcessId(), fd, iOff, nAmt, pp));
48074
48075#if SQLITE_MAX_MMAP_SIZE20971520>0
48076 if( pFd->mmapSizeMax>0 ){
48077 if( pFd->pMapRegion==0 ){
48078 int rc = winMapfile(pFd, -1);
48079 if( rc!=SQLITE_OK0 ){
48080 OSTRACE(("FETCH pid=%lu, pFile=%p, rc=%s\n",
48081 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
48082 return rc;
48083 }
48084 }
48085 if( pFd->mmapSize >= iOff+nAmt ){
48086 assert( pFd->pMapRegion!=0 )((void) (0));
48087 *pp = &((u8 *)pFd->pMapRegion)[iOff];
48088 pFd->nFetchOut++;
48089 }
48090 }
48091#endif
48092
48093 OSTRACE(("FETCH pid=%lu, pFile=%p, pp=%p, *pp=%p, rc=SQLITE_OK\n",
48094 osGetCurrentProcessId(), fd, pp, *pp));
48095 return SQLITE_OK0;
48096}
48097
48098/*
48099** If the third argument is non-NULL, then this function releases a
48100** reference obtained by an earlier call to winFetch(). The second
48101** argument passed to this function must be the same as the corresponding
48102** argument that was passed to the winFetch() invocation.
48103**
48104** Or, if the third argument is NULL, then this function is being called
48105** to inform the VFS layer that, according to POSIX, any existing mapping
48106** may now be invalid and should be unmapped.
48107*/
48108static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
48109#if SQLITE_MAX_MMAP_SIZE20971520>0
48110 winFile *pFd = (winFile*)fd; /* The underlying database file */
48111
48112 /* If p==0 (unmap the entire file) then there must be no outstanding
48113 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
48114 ** then there must be at least one outstanding. */
48115 assert( (p==0)==(pFd->nFetchOut==0) )((void) (0));
48116
48117 /* If p!=0, it must match the iOff value. */
48118 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] )((void) (0));
48119
48120 OSTRACE(("UNFETCH pid=%lu, pFile=%p, offset=%lld, p=%p\n",
48121 osGetCurrentProcessId(), pFd, iOff, p));
48122
48123 if( p ){
48124 pFd->nFetchOut--;
48125 }else{
48126 /* FIXME: If Windows truly always prevents truncating or deleting a
48127 ** file while a mapping is held, then the following winUnmapfile() call
48128 ** is unnecessary can be omitted - potentially improving
48129 ** performance. */
48130 winUnmapfile(pFd);
48131 }
48132
48133 assert( pFd->nFetchOut>=0 )((void) (0));
48134#endif
48135
48136 OSTRACE(("UNFETCH pid=%lu, pFile=%p, rc=SQLITE_OK\n",
48137 osGetCurrentProcessId(), fd));
48138 return SQLITE_OK0;
48139}
48140
48141/*
48142** Here ends the implementation of all sqlite3_file methods.
48143**
48144********************** End sqlite3_file Methods *******************************
48145******************************************************************************/
48146
48147/*
48148** This vector defines all the methods that can operate on an
48149** sqlite3_file for win32.
48150*/
48151static const sqlite3_io_methods winIoMethod = {
48152 3, /* iVersion */
48153 winClose, /* xClose */
48154 winRead, /* xRead */
48155 winWrite, /* xWrite */
48156 winTruncate, /* xTruncate */
48157 winSync, /* xSync */
48158 winFileSize, /* xFileSize */
48159 winLock, /* xLock */
48160 winUnlock, /* xUnlock */
48161 winCheckReservedLock, /* xCheckReservedLock */
48162 winFileControl, /* xFileControl */
48163 winSectorSize, /* xSectorSize */
48164 winDeviceCharacteristics, /* xDeviceCharacteristics */
48165 winShmMap, /* xShmMap */
48166 winShmLock, /* xShmLock */
48167 winShmBarrier, /* xShmBarrier */
48168 winShmUnmap, /* xShmUnmap */
48169 winFetch, /* xFetch */
48170 winUnfetch /* xUnfetch */
48171};
48172
48173/*
48174** This vector defines all the methods that can operate on an
48175** sqlite3_file for win32 without performing any locking.
48176*/
48177static const sqlite3_io_methods winIoNolockMethod = {
48178 3, /* iVersion */
48179 winClose, /* xClose */
48180 winRead, /* xRead */
48181 winWrite, /* xWrite */
48182 winTruncate, /* xTruncate */
48183 winSync, /* xSync */
48184 winFileSize, /* xFileSize */
48185 winNolockLock, /* xLock */
48186 winNolockUnlock, /* xUnlock */
48187 winNolockCheckReservedLock, /* xCheckReservedLock */
48188 winFileControl, /* xFileControl */
48189 winSectorSize, /* xSectorSize */
48190 winDeviceCharacteristics, /* xDeviceCharacteristics */
48191 winShmMap, /* xShmMap */
48192 winShmLock, /* xShmLock */
48193 winShmBarrier, /* xShmBarrier */
48194 winShmUnmap, /* xShmUnmap */
48195 winFetch, /* xFetch */
48196 winUnfetch /* xUnfetch */
48197};
48198
48199static winVfsAppData winAppData = {
48200 &winIoMethod, /* pMethod */
48201 0, /* pAppData */
48202 0 /* bNoLock */
48203};
48204
48205static winVfsAppData winNolockAppData = {
48206 &winIoNolockMethod, /* pMethod */
48207 0, /* pAppData */
48208 1 /* bNoLock */
48209};
48210
48211/****************************************************************************
48212**************************** sqlite3_vfs methods ****************************
48213**
48214** This division contains the implementation of methods on the
48215** sqlite3_vfs object.
48216*/
48217
48218#if defined(__CYGWIN__)
48219/*
48220** Convert a filename from whatever the underlying operating system
48221** supports for filenames into UTF-8. Space to hold the result is
48222** obtained from malloc and must be freed by the calling function.
48223*/
48224static char *winConvertToUtf8Filename(const void *zFilename){
48225 char *zConverted = 0;
48226 if( osIsNT() ){
48227 zConverted = winUnicodeToUtf8(zFilename);
48228 }
48229#ifdef SQLITE_WIN32_HAS_ANSI
48230 else{
48231 zConverted = winMbcsToUtf8(zFilename, osAreFileApisANSI());
48232 }
48233#endif
48234 /* caller will handle out of memory */
48235 return zConverted;
48236}
48237#endif
48238
48239/*
48240** Convert a UTF-8 filename into whatever form the underlying
48241** operating system wants filenames in. Space to hold the result
48242** is obtained from malloc and must be freed by the calling
48243** function.
48244*/
48245static void *winConvertFromUtf8Filename(const char *zFilename){
48246 void *zConverted = 0;
48247 if( osIsNT() ){
48248 zConverted = winUtf8ToUnicode(zFilename);
48249 }
48250#ifdef SQLITE_WIN32_HAS_ANSI
48251 else{
48252 zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI());
48253 }
48254#endif
48255 /* caller will handle out of memory */
48256 return zConverted;
48257}
48258
48259/*
48260** This function returns non-zero if the specified UTF-8 string buffer
48261** ends with a directory separator character or one was successfully
48262** added to it.
48263*/
48264static int winMakeEndInDirSep(int nBuf, char *zBuf){
48265 if( zBuf ){
48266 int nLen = sqlite3Strlen30(zBuf);
48267 if( nLen>0 ){
48268 if( winIsDirSep(zBuf[nLen-1]) ){
48269 return 1;
48270 }else if( nLen+1<nBuf ){
48271 zBuf[nLen] = winGetDirSep();
48272 zBuf[nLen+1] = '\0';
48273 return 1;
48274 }
48275 }
48276 }
48277 return 0;
48278}
48279
48280/*
48281** Create a temporary file name and store the resulting pointer into pzBuf.
48282** The pointer returned in pzBuf must be freed via sqlite3_free().
48283*/
48284static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
48285 static char zChars[] =
48286 "abcdefghijklmnopqrstuvwxyz"
48287 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
48288 "0123456789";
48289 size_t i, j;
48290 int nPre = sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX"etilqs_");
48291 int nMax, nBuf, nDir, nLen;
48292 char *zBuf;
48293
48294 /* It's odd to simulate an io-error here, but really this is just
48295 ** using the io-error infrastructure to test that SQLite handles this
48296 ** function failing.
48297 */
48298 SimulateIOError( return SQLITE_IOERR );
48299
48300 /* Allocate a temporary buffer to store the fully qualified file
48301 ** name for the temporary file. If this fails, we cannot continue.
48302 */
48303 nMax = pVfs->mxPathname; nBuf = nMax + 2;
48304 zBuf = sqlite3MallocZero( nBuf );
48305 if( !zBuf ){
48306 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
48307 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
48308 }
48309
48310 /* Figure out the effective temporary directory. First, check if one
48311 ** has been explicitly set by the application; otherwise, use the one
48312 ** configured by the operating system.
48313 */
48314 nDir = nMax - (nPre + 15);
48315 assert( nDir>0 )((void) (0));
48316 if( sqlite3_temp_directory ){
48317 int nDirLen = sqlite3Strlen30(sqlite3_temp_directory);
48318 if( nDirLen>0 ){
48319 if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
48320 nDirLen++;
48321 }
48322 if( nDirLen>nDir ){
48323 sqlite3_free(zBuf);
48324 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
48325 return winLogError(SQLITE_ERROR1, 0, "winGetTempname1", 0);
48326 }
48327 sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory);
48328 }
48329 }
48330#if defined(__CYGWIN__)
48331 else{
48332 static const char *azDirs[] = {
48333 0, /* getenv("SQLITE_TMPDIR") */
48334 0, /* getenv("TMPDIR") */
48335 0, /* getenv("TMP") */
48336 0, /* getenv("TEMP") */
48337 0, /* getenv("USERPROFILE") */
48338 "/var/tmp",
48339 "/usr/tmp",
48340 "/tmp",
48341 ".",
48342 0 /* List terminator */
48343 };
48344 unsigned int i;
48345 const char *zDir = 0;
48346
48347 if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
48348 if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
48349 if( !azDirs[2] ) azDirs[2] = getenv("TMP");
48350 if( !azDirs[3] ) azDirs[3] = getenv("TEMP");
48351 if( !azDirs[4] ) azDirs[4] = getenv("USERPROFILE");
48352 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
48353 void *zConverted;
48354 if( zDir==0 ) continue;
48355 /* If the path starts with a drive letter followed by the colon
48356 ** character, assume it is already a native Win32 path; otherwise,
48357 ** it must be converted to a native Win32 path via the Cygwin API
48358 ** prior to using it.
48359 */
48360 if( winIsDriveLetterAndColon(zDir) ){
48361 zConverted = winConvertFromUtf8Filename(zDir);
48362 if( !zConverted ){
48363 sqlite3_free(zBuf);
48364 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
48365 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
48366 }
48367 if( winIsDir(zConverted) ){
48368 sqlite3_snprintf(nMax, zBuf, "%s", zDir);
48369 sqlite3_free(zConverted);
48370 break;
48371 }
48372 sqlite3_free(zConverted);
48373 }else{
48374 zConverted = sqlite3MallocZero( nMax+1 );
48375 if( !zConverted ){
48376 sqlite3_free(zBuf);
48377 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
48378 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
48379 }
48380 if( cygwin_conv_path(
48381 osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A, zDir,
48382 zConverted, nMax+1)<0 ){
48383 sqlite3_free(zConverted);
48384 sqlite3_free(zBuf);
48385 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
48386 return winLogError(SQLITE_IOERR_CONVPATH(10 | (26<<8)), (DWORD)errno(*__errno_location ()),
48387 "winGetTempname2", zDir);
48388 }
48389 if( winIsDir(zConverted) ){
48390 /* At this point, we know the candidate directory exists and should
48391 ** be used. However, we may need to convert the string containing
48392 ** its name into UTF-8 (i.e. if it is UTF-16 right now).
48393 */
48394 char *zUtf8 = winConvertToUtf8Filename(zConverted);
48395 if( !zUtf8 ){
48396 sqlite3_free(zConverted);
48397 sqlite3_free(zBuf);
48398 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
48399 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
48400 }
48401 sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
48402 sqlite3_free(zUtf8);
48403 sqlite3_free(zConverted);
48404 break;
48405 }
48406 sqlite3_free(zConverted);
48407 }
48408 }
48409 }
48410#elif !SQLITE_OS_WINRT && !defined(__CYGWIN__)
48411 else if( osIsNT() ){
48412 char *zMulti;
48413 LPWSTR zWidePath = sqlite3MallocZero( nMax*sizeof(WCHAR) );
48414 if( !zWidePath ){
48415 sqlite3_free(zBuf);
48416 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
48417 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
48418 }
48419 if( osGetTempPathW(nMax, zWidePath)==0 ){
48420 sqlite3_free(zWidePath);
48421 sqlite3_free(zBuf);
48422 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
48423 return winLogError(SQLITE_IOERR_GETTEMPPATH(10 | (25<<8)), osGetLastError(),
48424 "winGetTempname2", 0);
48425 }
48426 zMulti = winUnicodeToUtf8(zWidePath);
48427 if( zMulti ){
48428 sqlite3_snprintf(nMax, zBuf, "%s", zMulti);
48429 sqlite3_free(zMulti);
48430 sqlite3_free(zWidePath);
48431 }else{
48432 sqlite3_free(zWidePath);
48433 sqlite3_free(zBuf);
48434 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
48435 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
48436 }
48437 }
48438#ifdef SQLITE_WIN32_HAS_ANSI
48439 else{
48440 char *zUtf8;
48441 char *zMbcsPath = sqlite3MallocZero( nMax );
48442 if( !zMbcsPath ){
48443 sqlite3_free(zBuf);
48444 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
48445 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
48446 }
48447 if( osGetTempPathA(nMax, zMbcsPath)==0 ){
48448 sqlite3_free(zBuf);
48449 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
48450 return winLogError(SQLITE_IOERR_GETTEMPPATH(10 | (25<<8)), osGetLastError(),
48451 "winGetTempname3", 0);
48452 }
48453 zUtf8 = winMbcsToUtf8(zMbcsPath, osAreFileApisANSI());
48454 if( zUtf8 ){
48455 sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
48456 sqlite3_free(zUtf8);
48457 }else{
48458 sqlite3_free(zBuf);
48459 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
48460 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
48461 }
48462 }
48463#endif /* SQLITE_WIN32_HAS_ANSI */
48464#endif /* !SQLITE_OS_WINRT */
48465
48466 /*
48467 ** Check to make sure the temporary directory ends with an appropriate
48468 ** separator. If it does not and there is not enough space left to add
48469 ** one, fail.
48470 */
48471 if( !winMakeEndInDirSep(nDir+1, zBuf) ){
48472 sqlite3_free(zBuf);
48473 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
48474 return winLogError(SQLITE_ERROR1, 0, "winGetTempname4", 0);
48475 }
48476
48477 /*
48478 ** Check that the output buffer is large enough for the temporary file
48479 ** name in the following format:
48480 **
48481 ** "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0"
48482 **
48483 ** If not, return SQLITE_ERROR. The number 17 is used here in order to
48484 ** account for the space used by the 15 character random suffix and the
48485 ** two trailing NUL characters. The final directory separator character
48486 ** has already added if it was not already present.
48487 */
48488 nLen = sqlite3Strlen30(zBuf);
48489 if( (nLen + nPre + 17) > nBuf ){
48490 sqlite3_free(zBuf);
48491 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
48492 return winLogError(SQLITE_ERROR1, 0, "winGetTempname5", 0);
48493 }
48494
48495 sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX"etilqs_");
48496
48497 j = sqlite3Strlen30(zBuf);
48498 sqlite3_randomness(15, &zBuf[j]);
48499 for(i=0; i<15; i++, j++){
48500 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
48501 }
48502 zBuf[j] = 0;
48503 zBuf[j+1] = 0;
48504 *pzBuf = zBuf;
48505
48506 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
48507 return SQLITE_OK0;
48508}
48509
48510/*
48511** Return TRUE if the named file is really a directory. Return false if
48512** it is something other than a directory, or if there is any kind of memory
48513** allocation failure.
48514*/
48515static int winIsDir(const void *zConverted){
48516 DWORD attr;
48517 int rc = 0;
48518 DWORD lastErrno;
48519
48520 if( osIsNT() ){
48521 int cnt = 0;
48522 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
48523 memset(&sAttrData, 0, sizeof(sAttrData));
48524 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
48525 GetFileExInfoStandard,
48526 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
48527 if( !rc ){
48528 return 0; /* Invalid name? */
48529 }
48530 attr = sAttrData.dwFileAttributes;
48531#if SQLITE_OS_WINCE==0
48532 }else{
48533 attr = osGetFileAttributesA((char*)zConverted);
48534#endif
48535 }
48536 return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY);
48537}
48538
48539/* forward reference */
48540static int winAccess(
48541 sqlite3_vfs *pVfs, /* Not used on win32 */
48542 const char *zFilename, /* Name of file to check */
48543 int flags, /* Type of test to make on this file */
48544 int *pResOut /* OUT: Result */
48545);
48546
48547/*
48548** Open a file.
48549*/
48550static int winOpen(
48551 sqlite3_vfs *pVfs, /* Used to get maximum path length and AppData */
48552 const char *zName, /* Name of the file (UTF-8) */
48553 sqlite3_file *id, /* Write the SQLite file handle here */
48554 int flags, /* Open mode flags */
48555 int *pOutFlags /* Status return flags */
48556){
48557 HANDLE h;
48558 DWORD lastErrno = 0;
48559 DWORD dwDesiredAccess;
48560 DWORD dwShareMode;
48561 DWORD dwCreationDisposition;
48562 DWORD dwFlagsAndAttributes = 0;
48563#if SQLITE_OS_WINCE
48564 int isTemp = 0;
48565#endif
48566 winVfsAppData *pAppData;
48567 winFile *pFile = (winFile*)id;
48568 void *zConverted; /* Filename in OS encoding */
48569 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
48570 int cnt = 0;
48571
48572 /* If argument zPath is a NULL pointer, this function is required to open
48573 ** a temporary file. Use this buffer to store the file name in.
48574 */
48575 char *zTmpname = 0; /* For temporary filename, if necessary. */
48576
48577 int rc = SQLITE_OK0; /* Function Return Code */
48578#if !defined(NDEBUG1) || SQLITE_OS_WINCE
48579 int eType = flags&0xFFFFFF00; /* Type of file to open */
48580#endif
48581
48582 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE0x00000010);
48583 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE0x00000008);
48584 int isCreate = (flags & SQLITE_OPEN_CREATE0x00000004);
48585 int isReadonly = (flags & SQLITE_OPEN_READONLY0x00000001);
48586 int isReadWrite = (flags & SQLITE_OPEN_READWRITE0x00000002);
48587
48588#ifndef NDEBUG1
48589 int isOpenJournal = (isCreate && (
48590 eType==SQLITE_OPEN_SUPER_JOURNAL0x00004000
48591 || eType==SQLITE_OPEN_MAIN_JOURNAL0x00000800
48592 || eType==SQLITE_OPEN_WAL0x00080000
48593 ));
48594#endif
48595
48596 OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n",
48597 zUtf8Name, id, flags, pOutFlags));
48598
48599 /* Check the following statements are true:
48600 **
48601 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
48602 ** (b) if CREATE is set, then READWRITE must also be set, and
48603 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
48604 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
48605 */
48606 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly))((void) (0));
48607 assert(isCreate==0 || isReadWrite)((void) (0));
48608 assert(isExclusive==0 || isCreate)((void) (0));
48609 assert(isDelete==0 || isCreate)((void) (0));
48610
48611 /* The main DB, main journal, WAL file and super-journal are never
48612 ** automatically deleted. Nor are they ever temporary files. */
48613 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB )((void) (0));
48614 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL )((void) (0));
48615 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_SUPER_JOURNAL )((void) (0));
48616 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL )((void) (0));
48617
48618 /* Assert that the upper layer has set one of the "file-type" flags. */
48619 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB((void) (0))
48620 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL((void) (0))
48621 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_SUPER_JOURNAL((void) (0))
48622 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL((void) (0))
48623 )((void) (0));
48624
48625 assert( pFile!=0 )((void) (0));
48626 memset(pFile, 0, sizeof(winFile));
48627 pFile->h = INVALID_HANDLE_VALUE;
48628
48629#if SQLITE_OS_WINRT
48630 if( !zUtf8Name && !sqlite3_temp_directory ){
48631 sqlite3_log(SQLITE_ERROR1,
48632 "sqlite3_temp_directory variable should be set for WinRT");
48633 }
48634#endif
48635
48636 /* If the second argument to this function is NULL, generate a
48637 ** temporary file name to use
48638 */
48639 if( !zUtf8Name ){
48640 assert( isDelete && !isOpenJournal )((void) (0));
48641 rc = winGetTempname(pVfs, &zTmpname);
48642 if( rc!=SQLITE_OK0 ){
48643 OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
48644 return rc;
48645 }
48646 zUtf8Name = zTmpname;
48647 }
48648
48649 /* Database filenames are double-zero terminated if they are not
48650 ** URIs with parameters. Hence, they can always be passed into
48651 ** sqlite3_uri_parameter().
48652 */
48653 assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||((void) (0))
48654 zUtf8Name[sqlite3Strlen30(zUtf8Name)+1]==0 )((void) (0));
48655
48656 /* Convert the filename to the system encoding. */
48657 zConverted = winConvertFromUtf8Filename(zUtf8Name);
48658 if( zConverted==0 ){
48659 sqlite3_free(zTmpname);
48660 OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
48661 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
48662 }
48663
48664 if( winIsDir(zConverted) ){
48665 sqlite3_free(zConverted);
48666 sqlite3_free(zTmpname);
48667 OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name));
48668 return SQLITE_CANTOPEN_ISDIR(14 | (2<<8));
48669 }
48670
48671 if( isReadWrite ){
48672 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
48673 }else{
48674 dwDesiredAccess = GENERIC_READ;
48675 }
48676
48677 /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
48678 ** created. SQLite doesn't use it to indicate "exclusive access"
48679 ** as it is usually understood.
48680 */
48681 if( isExclusive ){
48682 /* Creates a new file, only if it does not already exist. */
48683 /* If the file exists, it fails. */
48684 dwCreationDisposition = CREATE_NEW;
48685 }else if( isCreate ){
48686 /* Open existing file, or create if it doesn't exist */
48687 dwCreationDisposition = OPEN_ALWAYS;
48688 }else{
48689 /* Opens a file, only if it exists. */
48690 dwCreationDisposition = OPEN_EXISTING;
48691 }
48692
48693 if( 0==sqlite3_uri_boolean(zName, "exclusive", 0) ){
48694 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
48695 }else{
48696 dwShareMode = 0;
48697 }
48698
48699 if( isDelete ){
48700#if SQLITE_OS_WINCE
48701 dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
48702 isTemp = 1;
48703#else
48704 dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
48705 | FILE_ATTRIBUTE_HIDDEN
48706 | FILE_FLAG_DELETE_ON_CLOSE;
48707#endif
48708 }else{
48709 dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
48710 }
48711 /* Reports from the internet are that performance is always
48712 ** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */
48713#if SQLITE_OS_WINCE
48714 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
48715#endif
48716
48717 if( osIsNT() ){
48718#if SQLITE_OS_WINRT
48719 CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
48720 extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
48721 extendedParameters.dwFileAttributes =
48722 dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
48723 extendedParameters.dwFileFlags = dwFlagsAndAttributes & FILE_FLAG_MASK;
48724 extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
48725 extendedParameters.lpSecurityAttributes = NULL((void*)0);
48726 extendedParameters.hTemplateFile = NULL((void*)0);
48727 do{
48728 h = osCreateFile2((LPCWSTR)zConverted,
48729 dwDesiredAccess,
48730 dwShareMode,
48731 dwCreationDisposition,
48732 &extendedParameters);
48733 if( h!=INVALID_HANDLE_VALUE ) break;
48734 if( isReadWrite ){
48735 int rc2, isRO = 0;
48736 sqlite3BeginBenignMalloc();
48737 rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ2, &isRO);
48738 sqlite3EndBenignMalloc();
48739 if( rc2==SQLITE_OK0 && isRO ) break;
48740 }
48741 }while( winRetryIoerr(&cnt, &lastErrno) );
48742#else
48743 do{
48744 h = osCreateFileW((LPCWSTR)zConverted,
48745 dwDesiredAccess,
48746 dwShareMode, NULL((void*)0),
48747 dwCreationDisposition,
48748 dwFlagsAndAttributes,
48749 NULL((void*)0));
48750 if( h!=INVALID_HANDLE_VALUE ) break;
48751 if( isReadWrite ){
48752 int rc2, isRO = 0;
48753 sqlite3BeginBenignMalloc();
48754 rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ2, &isRO);
48755 sqlite3EndBenignMalloc();
48756 if( rc2==SQLITE_OK0 && isRO ) break;
48757 }
48758 }while( winRetryIoerr(&cnt, &lastErrno) );
48759#endif
48760 }
48761#ifdef SQLITE_WIN32_HAS_ANSI
48762 else{
48763 do{
48764 h = osCreateFileA((LPCSTR)zConverted,
48765 dwDesiredAccess,
48766 dwShareMode, NULL((void*)0),
48767 dwCreationDisposition,
48768 dwFlagsAndAttributes,
48769 NULL((void*)0));
48770 if( h!=INVALID_HANDLE_VALUE ) break;
48771 if( isReadWrite ){
48772 int rc2, isRO = 0;
48773 sqlite3BeginBenignMalloc();
48774 rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ2, &isRO);
48775 sqlite3EndBenignMalloc();
48776 if( rc2==SQLITE_OK0 && isRO ) break;
48777 }
48778 }while( winRetryIoerr(&cnt, &lastErrno) );
48779 }
48780#endif
48781 winLogIoerr(cnt, __LINE__48781);
48782
48783 OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
48784 dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
48785
48786 if( h==INVALID_HANDLE_VALUE ){
48787 sqlite3_free(zConverted);
48788 sqlite3_free(zTmpname);
48789 if( isReadWrite && !isExclusive ){
48790 return winOpen(pVfs, zName, id,
48791 ((flags|SQLITE_OPEN_READONLY0x00000001) &
48792 ~(SQLITE_OPEN_CREATE0x00000004|SQLITE_OPEN_READWRITE0x00000002)),
48793 pOutFlags);
48794 }else{
48795 pFile->lastErrno = lastErrno;
48796 winLogError(SQLITE_CANTOPEN14, pFile->lastErrno, "winOpen", zUtf8Name);
48797 return SQLITE_CANTOPEN_BKPTsqlite3CantopenError(48797);
48798 }
48799 }
48800
48801 if( pOutFlags ){
48802 if( isReadWrite ){
48803 *pOutFlags = SQLITE_OPEN_READWRITE0x00000002;
48804 }else{
48805 *pOutFlags = SQLITE_OPEN_READONLY0x00000001;
48806 }
48807 }
48808
48809 OSTRACE(("OPEN file=%p, name=%s, access=%lx, pOutFlags=%p, *pOutFlags=%d, "
48810 "rc=%s\n", h, zUtf8Name, dwDesiredAccess, pOutFlags, pOutFlags ?
48811 *pOutFlags : 0, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
48812
48813 pAppData = (winVfsAppData*)pVfs->pAppData;
48814
48815#if SQLITE_OS_WINCE
48816 {
48817 if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB0x00000100
48818 && ((pAppData==NULL((void*)0)) || !pAppData->bNoLock)
48819 && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK0
48820 ){
48821 osCloseHandle(h);
48822 sqlite3_free(zConverted);
48823 sqlite3_free(zTmpname);
48824 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
48825 return rc;
48826 }
48827 }
48828 if( isTemp ){
48829 pFile->zDeleteOnClose = zConverted;
48830 }else
48831#endif
48832 {
48833 sqlite3_free(zConverted);
48834 }
48835
48836 sqlite3_free(zTmpname);
48837 id->pMethods = pAppData ? pAppData->pMethod : &winIoMethod;
48838 pFile->pVfs = pVfs;
48839 pFile->h = h;
48840 if( isReadonly ){
48841 pFile->ctrlFlags |= WINFILE_RDONLY;
48842 }
48843 if( (flags & SQLITE_OPEN_MAIN_DB0x00000100)
48844 && sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE1)
48845 ){
48846 pFile->ctrlFlags |= WINFILE_PSOW;
48847 }
48848 pFile->lastErrno = NO_ERROR;
48849 pFile->zPath = zName;
48850#if SQLITE_MAX_MMAP_SIZE20971520>0
48851 pFile->hMap = NULL((void*)0);
48852 pFile->pMapRegion = 0;
48853 pFile->mmapSize = 0;
48854 pFile->mmapSizeMax = sqlite3GlobalConfigsqlite3Config.szMmap;
48855#endif
48856
48857 OpenCounter(+1);
48858 return rc;
48859}
48860
48861/*
48862** Delete the named file.
48863**
48864** Note that Windows does not allow a file to be deleted if some other
48865** process has it open. Sometimes a virus scanner or indexing program
48866** will open a journal file shortly after it is created in order to do
48867** whatever it does. While this other process is holding the
48868** file open, we will be unable to delete it. To work around this
48869** problem, we delay 100 milliseconds and try to delete again. Up
48870** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
48871** up and returning an error.
48872*/
48873static int winDelete(
48874 sqlite3_vfs *pVfs, /* Not used on win32 */
48875 const char *zFilename, /* Name of file to delete */
48876 int syncDir /* Not used on win32 */
48877){
48878 int cnt = 0;
48879 int rc;
48880 DWORD attr;
48881 DWORD lastErrno = 0;
48882 void *zConverted;
48883 UNUSED_PARAMETER(pVfs)(void)(pVfs);
48884 UNUSED_PARAMETER(syncDir)(void)(syncDir);
48885
48886 SimulateIOError(return SQLITE_IOERR_DELETE);
48887 OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir));
48888
48889 zConverted = winConvertFromUtf8Filename(zFilename);
48890 if( zConverted==0 ){
48891 OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
48892 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
48893 }
48894 if( osIsNT() ){
48895 do {
48896#if SQLITE_OS_WINRT
48897 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
48898 memset(&sAttrData, 0, sizeof(sAttrData));
48899 if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard,
48900 &sAttrData) ){
48901 attr = sAttrData.dwFileAttributes;
48902 }else{
48903 lastErrno = osGetLastError();
48904 if( lastErrno==ERROR_FILE_NOT_FOUND
48905 || lastErrno==ERROR_PATH_NOT_FOUND ){
48906 rc = SQLITE_IOERR_DELETE_NOENT(10 | (23<<8)); /* Already gone? */
48907 }else{
48908 rc = SQLITE_ERROR1;
48909 }
48910 break;
48911 }
48912#else
48913 attr = osGetFileAttributesW(zConverted);
48914#endif
48915 if ( attr==INVALID_FILE_ATTRIBUTES ){
48916 lastErrno = osGetLastError();
48917 if( lastErrno==ERROR_FILE_NOT_FOUND
48918 || lastErrno==ERROR_PATH_NOT_FOUND ){
48919 rc = SQLITE_IOERR_DELETE_NOENT(10 | (23<<8)); /* Already gone? */
48920 }else{
48921 rc = SQLITE_ERROR1;
48922 }
48923 break;
48924 }
48925 if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
48926 rc = SQLITE_ERROR1; /* Files only. */
48927 break;
48928 }
48929 if ( osDeleteFileW(zConverted) ){
48930 rc = SQLITE_OK0; /* Deleted OK. */
48931 break;
48932 }
48933 if ( !winRetryIoerr(&cnt, &lastErrno) ){
48934 rc = SQLITE_ERROR1; /* No more retries. */
48935 break;
48936 }
48937 } while(1);
48938 }
48939#ifdef SQLITE_WIN32_HAS_ANSI
48940 else{
48941 do {
48942 attr = osGetFileAttributesA(zConverted);
48943 if ( attr==INVALID_FILE_ATTRIBUTES ){
48944 lastErrno = osGetLastError();
48945 if( lastErrno==ERROR_FILE_NOT_FOUND
48946 || lastErrno==ERROR_PATH_NOT_FOUND ){
48947 rc = SQLITE_IOERR_DELETE_NOENT(10 | (23<<8)); /* Already gone? */
48948 }else{
48949 rc = SQLITE_ERROR1;
48950 }
48951 break;
48952 }
48953 if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
48954 rc = SQLITE_ERROR1; /* Files only. */
48955 break;
48956 }
48957 if ( osDeleteFileA(zConverted) ){
48958 rc = SQLITE_OK0; /* Deleted OK. */
48959 break;
48960 }
48961 if ( !winRetryIoerr(&cnt, &lastErrno) ){
48962 rc = SQLITE_ERROR1; /* No more retries. */
48963 break;
48964 }
48965 } while(1);
48966 }
48967#endif
48968 if( rc && rc!=SQLITE_IOERR_DELETE_NOENT(10 | (23<<8)) ){
48969 rc = winLogError(SQLITE_IOERR_DELETE(10 | (10<<8)), lastErrno, "winDelete", zFilename);
48970 }else{
48971 winLogIoerr(cnt, __LINE__48971);
48972 }
48973 sqlite3_free(zConverted);
48974 OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
48975 return rc;
48976}
48977
48978/*
48979** Check the existence and status of a file.
48980*/
48981static int winAccess(
48982 sqlite3_vfs *pVfs, /* Not used on win32 */
48983 const char *zFilename, /* Name of file to check */
48984 int flags, /* Type of test to make on this file */
48985 int *pResOut /* OUT: Result */
48986){
48987 DWORD attr;
48988 int rc = 0;
48989 DWORD lastErrno = 0;
48990 void *zConverted;
48991 UNUSED_PARAMETER(pVfs)(void)(pVfs);
48992
48993 SimulateIOError( return SQLITE_IOERR_ACCESS; );
48994 OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
48995 zFilename, flags, pResOut));
48996
48997 zConverted = winConvertFromUtf8Filename(zFilename);
48998 if( zConverted==0 ){
48999 OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
49000 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
49001 }
49002 if( osIsNT() ){
49003 int cnt = 0;
49004 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
49005 memset(&sAttrData, 0, sizeof(sAttrData));
49006 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
49007 GetFileExInfoStandard,
49008 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
49009 if( rc ){
49010 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
49011 ** as if it does not exist.
49012 */
49013 if( flags==SQLITE_ACCESS_EXISTS0
49014 && sAttrData.nFileSizeHigh==0
49015 && sAttrData.nFileSizeLow==0 ){
49016 attr = INVALID_FILE_ATTRIBUTES;
49017 }else{
49018 attr = sAttrData.dwFileAttributes;
49019 }
49020 }else{
49021 winLogIoerr(cnt, __LINE__49021);
49022 if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
49023 sqlite3_free(zConverted);
49024 return winLogError(SQLITE_IOERR_ACCESS(10 | (13<<8)), lastErrno, "winAccess",
49025 zFilename);
49026 }else{
49027 attr = INVALID_FILE_ATTRIBUTES;
49028 }
49029 }
49030 }
49031#ifdef SQLITE_WIN32_HAS_ANSI
49032 else{
49033 attr = osGetFileAttributesA((char*)zConverted);
49034 }
49035#endif
49036 sqlite3_free(zConverted);
49037 switch( flags ){
49038 case SQLITE_ACCESS_READ2:
49039 case SQLITE_ACCESS_EXISTS0:
49040 rc = attr!=INVALID_FILE_ATTRIBUTES;
49041 break;
49042 case SQLITE_ACCESS_READWRITE1:
49043 rc = attr!=INVALID_FILE_ATTRIBUTES &&
49044 (attr & FILE_ATTRIBUTE_READONLY)==0;
49045 break;
49046 default:
49047 assert(!"Invalid flags argument")((void) (0));
49048 }
49049 *pResOut = rc;
49050 OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
49051 zFilename, pResOut, *pResOut));
49052 return SQLITE_OK0;
49053}
49054
49055/*
49056** Returns non-zero if the specified path name starts with the "long path"
49057** prefix.
49058*/
49059static BOOL winIsLongPathPrefix(
49060 const char *zPathname
49061){
49062 return ( zPathname[0]=='\\' && zPathname[1]=='\\'
49063 && zPathname[2]=='?' && zPathname[3]=='\\' );
49064}
49065
49066/*
49067** Returns non-zero if the specified path name starts with a drive letter
49068** followed by a colon character.
49069*/
49070static BOOL winIsDriveLetterAndColon(
49071 const char *zPathname
49072){
49073 return ( sqlite3Isalpha(zPathname[0])(sqlite3CtypeMap[(unsigned char)(zPathname[0])]&0x02) && zPathname[1]==':' );
49074}
49075
49076/*
49077** Returns non-zero if the specified path name should be used verbatim. If
49078** non-zero is returned from this function, the calling function must simply
49079** use the provided path name verbatim -OR- resolve it into a full path name
49080** using the GetFullPathName Win32 API function (if available).
49081*/
49082static BOOL winIsVerbatimPathname(
49083 const char *zPathname
49084){
49085 /*
49086 ** If the path name starts with a forward slash or a backslash, it is either
49087 ** a legal UNC name, a volume relative path, or an absolute path name in the
49088 ** "Unix" format on Windows. There is no easy way to differentiate between
49089 ** the final two cases; therefore, we return the safer return value of TRUE
49090 ** so that callers of this function will simply use it verbatim.
49091 */
49092 if ( winIsDirSep(zPathname[0]) ){
49093 return TRUE;
49094 }
49095
49096 /*
49097 ** If the path name starts with a letter and a colon it is either a volume
49098 ** relative path or an absolute path. Callers of this function must not
49099 ** attempt to treat it as a relative path name (i.e. they should simply use
49100 ** it verbatim).
49101 */
49102 if ( winIsDriveLetterAndColon(zPathname) ){
49103 return TRUE;
49104 }
49105
49106 /*
49107 ** If we get to this point, the path name should almost certainly be a purely
49108 ** relative one (i.e. not a UNC name, not absolute, and not volume relative).
49109 */
49110 return FALSE;
49111}
49112
49113/*
49114** Turn a relative pathname into a full pathname. Write the full
49115** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
49116** bytes in size.
49117*/
49118static int winFullPathname(
49119 sqlite3_vfs *pVfs, /* Pointer to vfs object */
49120 const char *zRelative, /* Possibly relative input path */
49121 int nFull, /* Size of output buffer in bytes */
49122 char *zFull /* Output buffer */
49123){
49124#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
49125 DWORD nByte;
49126 void *zConverted;
49127 char *zOut;
49128#endif
49129
49130 /* If this path name begins with "/X:" or "\\?\", where "X" is any
49131 ** alphabetic character, discard the initial "/" from the pathname.
49132 */
49133 if( zRelative[0]=='/' && (winIsDriveLetterAndColon(zRelative+1)
49134 || winIsLongPathPrefix(zRelative+1)) ){
49135 zRelative++;
49136 }
49137
49138#if defined(__CYGWIN__)
49139 SimulateIOError( return SQLITE_ERROR );
49140 UNUSED_PARAMETER(nFull)(void)(nFull);
49141 assert( nFull>=pVfs->mxPathname )((void) (0));
49142 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
49143 /*
49144 ** NOTE: We are dealing with a relative path name and the data
49145 ** directory has been set. Therefore, use it as the basis
49146 ** for converting the relative path name to an absolute
49147 ** one by prepending the data directory and a slash.
49148 */
49149 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
49150 if( !zOut ){
49151 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
49152 }
49153 if( cygwin_conv_path(
49154 (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A) |
49155 CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
49156 sqlite3_free(zOut);
49157 return winLogError(SQLITE_CANTOPEN_CONVPATH(14 | (4<<8)), (DWORD)errno(*__errno_location ()),
49158 "winFullPathname1", zRelative);
49159 }else{
49160 char *zUtf8 = winConvertToUtf8Filename(zOut);
49161 if( !zUtf8 ){
49162 sqlite3_free(zOut);
49163 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
49164 }
49165 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname)((nFull)<(pVfs->mxPathname)?(nFull):(pVfs->mxPathname
))
, zFull, "%s%c%s",
49166 sqlite3_data_directory, winGetDirSep(), zUtf8);
49167 sqlite3_free(zUtf8);
49168 sqlite3_free(zOut);
49169 }
49170 }else{
49171 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
49172 if( !zOut ){
49173 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
49174 }
49175 if( cygwin_conv_path(
49176 (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A),
49177 zRelative, zOut, pVfs->mxPathname+1)<0 ){
49178 sqlite3_free(zOut);
49179 return winLogError(SQLITE_CANTOPEN_CONVPATH(14 | (4<<8)), (DWORD)errno(*__errno_location ()),
49180 "winFullPathname2", zRelative);
49181 }else{
49182 char *zUtf8 = winConvertToUtf8Filename(zOut);
49183 if( !zUtf8 ){
49184 sqlite3_free(zOut);
49185 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
49186 }
49187 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname)((nFull)<(pVfs->mxPathname)?(nFull):(pVfs->mxPathname
))
, zFull, "%s", zUtf8);
49188 sqlite3_free(zUtf8);
49189 sqlite3_free(zOut);
49190 }
49191 }
49192 return SQLITE_OK0;
49193#endif
49194
49195#if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && !defined(__CYGWIN__)
49196 SimulateIOError( return SQLITE_ERROR );
49197 /* WinCE has no concept of a relative pathname, or so I am told. */
49198 /* WinRT has no way to convert a relative path to an absolute one. */
49199 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
49200 /*
49201 ** NOTE: We are dealing with a relative path name and the data
49202 ** directory has been set. Therefore, use it as the basis
49203 ** for converting the relative path name to an absolute
49204 ** one by prepending the data directory and a backslash.
49205 */
49206 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname)((nFull)<(pVfs->mxPathname)?(nFull):(pVfs->mxPathname
))
, zFull, "%s%c%s",
49207 sqlite3_data_directory, winGetDirSep(), zRelative);
49208 }else{
49209 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname)((nFull)<(pVfs->mxPathname)?(nFull):(pVfs->mxPathname
))
, zFull, "%s", zRelative);
49210 }
49211 return SQLITE_OK0;
49212#endif
49213
49214#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
49215 /* It's odd to simulate an io-error here, but really this is just
49216 ** using the io-error infrastructure to test that SQLite handles this
49217 ** function failing. This function could fail if, for example, the
49218 ** current working directory has been unlinked.
49219 */
49220 SimulateIOError( return SQLITE_ERROR );
49221 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
49222 /*
49223 ** NOTE: We are dealing with a relative path name and the data
49224 ** directory has been set. Therefore, use it as the basis
49225 ** for converting the relative path name to an absolute
49226 ** one by prepending the data directory and a backslash.
49227 */
49228 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname)((nFull)<(pVfs->mxPathname)?(nFull):(pVfs->mxPathname
))
, zFull, "%s%c%s",
49229 sqlite3_data_directory, winGetDirSep(), zRelative);
49230 return SQLITE_OK0;
49231 }
49232 zConverted = winConvertFromUtf8Filename(zRelative);
49233 if( zConverted==0 ){
49234 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
49235 }
49236 if( osIsNT() ){
49237 LPWSTR zTemp;
49238 nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
49239 if( nByte==0 ){
49240 sqlite3_free(zConverted);
49241 return winLogError(SQLITE_CANTOPEN_FULLPATH(14 | (3<<8)), osGetLastError(),
49242 "winFullPathname1", zRelative);
49243 }
49244 nByte += 3;
49245 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
49246 if( zTemp==0 ){
49247 sqlite3_free(zConverted);
49248 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
49249 }
49250 nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
49251 if( nByte==0 ){
49252 sqlite3_free(zConverted);
49253 sqlite3_free(zTemp);
49254 return winLogError(SQLITE_CANTOPEN_FULLPATH(14 | (3<<8)), osGetLastError(),
49255 "winFullPathname2", zRelative);
49256 }
49257 sqlite3_free(zConverted);
49258 zOut = winUnicodeToUtf8(zTemp);
49259 sqlite3_free(zTemp);
49260 }
49261#ifdef SQLITE_WIN32_HAS_ANSI
49262 else{
49263 char *zTemp;
49264 nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
49265 if( nByte==0 ){
49266 sqlite3_free(zConverted);
49267 return winLogError(SQLITE_CANTOPEN_FULLPATH(14 | (3<<8)), osGetLastError(),
49268 "winFullPathname3", zRelative);
49269 }
49270 nByte += 3;
49271 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
49272 if( zTemp==0 ){
49273 sqlite3_free(zConverted);
49274 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
49275 }
49276 nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
49277 if( nByte==0 ){
49278 sqlite3_free(zConverted);
49279 sqlite3_free(zTemp);
49280 return winLogError(SQLITE_CANTOPEN_FULLPATH(14 | (3<<8)), osGetLastError(),
49281 "winFullPathname4", zRelative);
49282 }
49283 sqlite3_free(zConverted);
49284 zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
49285 sqlite3_free(zTemp);
49286 }
49287#endif
49288 if( zOut ){
49289 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname)((nFull)<(pVfs->mxPathname)?(nFull):(pVfs->mxPathname
))
, zFull, "%s", zOut);
49290 sqlite3_free(zOut);
49291 return SQLITE_OK0;
49292 }else{
49293 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
49294 }
49295#endif
49296}
49297
49298#ifndef SQLITE_OMIT_LOAD_EXTENSION1
49299/*
49300** Interfaces for opening a shared library, finding entry points
49301** within the shared library, and closing the shared library.
49302*/
49303static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
49304 HANDLE h;
49305#if defined(__CYGWIN__)
49306 int nFull = pVfs->mxPathname+1;
49307 char *zFull = sqlite3MallocZero( nFull );
49308 void *zConverted = 0;
49309 if( zFull==0 ){
49310 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
49311 return 0;
49312 }
49313 if( winFullPathname(pVfs, zFilename, nFull, zFull)!=SQLITE_OK0 ){
49314 sqlite3_free(zFull);
49315 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
49316 return 0;
49317 }
49318 zConverted = winConvertFromUtf8Filename(zFull);
49319 sqlite3_free(zFull);
49320#else
49321 void *zConverted = winConvertFromUtf8Filename(zFilename);
49322 UNUSED_PARAMETER(pVfs)(void)(pVfs);
49323#endif
49324 if( zConverted==0 ){
49325 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
49326 return 0;
49327 }
49328 if( osIsNT() ){
49329#if SQLITE_OS_WINRT
49330 h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
49331#else
49332 h = osLoadLibraryW((LPCWSTR)zConverted);
49333#endif
49334 }
49335#ifdef SQLITE_WIN32_HAS_ANSI
49336 else{
49337 h = osLoadLibraryA((char*)zConverted);
49338 }
49339#endif
49340 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)h));
49341 sqlite3_free(zConverted);
49342 return (void*)h;
49343}
49344static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
49345 UNUSED_PARAMETER(pVfs)(void)(pVfs);
49346 winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut);
49347}
49348static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
49349 FARPROC proc;
49350 UNUSED_PARAMETER(pVfs)(void)(pVfs);
49351 proc = osGetProcAddressA((HANDLE)pH, zSym);
49352 OSTRACE(("DLSYM handle=%p, symbol=%s, address=%p\n",
49353 (void*)pH, zSym, (void*)proc));
49354 return (void(*)(void))proc;
49355}
49356static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
49357 UNUSED_PARAMETER(pVfs)(void)(pVfs);
49358 osFreeLibrary((HANDLE)pHandle);
49359 OSTRACE(("DLCLOSE handle=%p\n", (void*)pHandle));
49360}
49361#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
49362 #define winDlOpen 0
49363 #define winDlError 0
49364 #define winDlSym 0
49365 #define winDlClose 0
49366#endif
49367
49368/* State information for the randomness gatherer. */
49369typedef struct EntropyGatherer EntropyGatherer;
49370struct EntropyGatherer {
49371 unsigned char *a; /* Gather entropy into this buffer */
49372 int na; /* Size of a[] in bytes */
49373 int i; /* XOR next input into a[i] */
49374 int nXor; /* Number of XOR operations done */
49375};
49376
49377#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
49378/* Mix sz bytes of entropy into p. */
49379static void xorMemory(EntropyGatherer *p, unsigned char *x, int sz){
49380 int j, k;
49381 for(j=0, k=p->i; j<sz; j++){
49382 p->a[k++] ^= x[j];
49383 if( k>=p->na ) k = 0;
49384 }
49385 p->i = k;
49386 p->nXor += sz;
49387}
49388#endif /* !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) */
49389
49390/*
49391** Write up to nBuf bytes of randomness into zBuf.
49392*/
49393static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
49394#if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS)
49395 UNUSED_PARAMETER(pVfs)(void)(pVfs);
49396 memset(zBuf, 0, nBuf);
49397 return nBuf;
49398#else
49399 EntropyGatherer e;
49400 UNUSED_PARAMETER(pVfs)(void)(pVfs);
49401 memset(zBuf, 0, nBuf);
49402 e.a = (unsigned char*)zBuf;
49403 e.na = nBuf;
49404 e.nXor = 0;
49405 e.i = 0;
49406 {
49407 SYSTEMTIME x;
49408 osGetSystemTime(&x);
49409 xorMemory(&e, (unsigned char*)&x, sizeof(SYSTEMTIME));
49410 }
49411 {
49412 DWORD pid = osGetCurrentProcessId();
49413 xorMemory(&e, (unsigned char*)&pid, sizeof(DWORD));
49414 }
49415#if SQLITE_OS_WINRT
49416 {
49417 ULONGLONG cnt = osGetTickCount64();
49418 xorMemory(&e, (unsigned char*)&cnt, sizeof(ULONGLONG));
49419 }
49420#else
49421 {
49422 DWORD cnt = osGetTickCount();
49423 xorMemory(&e, (unsigned char*)&cnt, sizeof(DWORD));
49424 }
49425#endif /* SQLITE_OS_WINRT */
49426 {
49427 LARGE_INTEGER i;
49428 osQueryPerformanceCounter(&i);
49429 xorMemory(&e, (unsigned char*)&i, sizeof(LARGE_INTEGER));
49430 }
49431#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
49432 {
49433 UUID id;
49434 memset(&id, 0, sizeof(UUID));
49435 osUuidCreate(&id);
49436 xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
49437 memset(&id, 0, sizeof(UUID));
49438 osUuidCreateSequential(&id);
49439 xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
49440 }
49441#endif /* !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID */
49442 return e.nXor>nBuf ? nBuf : e.nXor;
49443#endif /* defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) */
49444}
49445
49446
49447/*
49448** Sleep for a little while. Return the amount of time slept.
49449*/
49450static int winSleep(sqlite3_vfs *pVfs, int microsec){
49451 sqlite3_win32_sleep((microsec+999)/1000);
49452 UNUSED_PARAMETER(pVfs)(void)(pVfs);
49453 return ((microsec+999)/1000)*1000;
49454}
49455
49456/*
49457** The following variable, if set to a non-zero value, is interpreted as
49458** the number of seconds since 1970 and is used to set the result of
49459** sqlite3OsCurrentTime() during testing.
49460*/
49461#ifdef SQLITE_TEST
49462SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
49463#endif
49464
49465/*
49466** Find the current time (in Universal Coordinated Time). Write into *piNow
49467** the current time and date as a Julian Day number times 86_400_000. In
49468** other words, write into *piNow the number of milliseconds since the Julian
49469** epoch of noon in Greenwich on November 24, 4714 B.C according to the
49470** proleptic Gregorian calendar.
49471**
49472** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
49473** cannot be found.
49474*/
49475static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
49476 /* FILETIME structure is a 64-bit value representing the number of
49477 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
49478 */
49479 FILETIME ft;
49480 static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
49481#ifdef SQLITE_TEST
49482 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
49483#endif
49484 /* 2^32 - to avoid use of LL and warnings in gcc */
49485 static const sqlite3_int64 max32BitValue =
49486 (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 +
49487 (sqlite3_int64)294967296;
49488
49489#if SQLITE_OS_WINCE
49490 SYSTEMTIME time;
49491 osGetSystemTime(&time);
49492 /* if SystemTimeToFileTime() fails, it returns zero. */
49493 if (!osSystemTimeToFileTime(&time,&ft)){
49494 return SQLITE_ERROR1;
49495 }
49496#else
49497 osGetSystemTimeAsFileTime( &ft );
49498#endif
49499
49500 *piNow = winFiletimeEpoch +
49501 ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
49502 (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
49503
49504#ifdef SQLITE_TEST
49505 if( sqlite3_current_time ){
49506 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
49507 }
49508#endif
49509 UNUSED_PARAMETER(pVfs)(void)(pVfs);
49510 return SQLITE_OK0;
49511}
49512
49513/*
49514** Find the current time (in Universal Coordinated Time). Write the
49515** current time and date as a Julian Day number into *prNow and
49516** return 0. Return 1 if the time and date cannot be found.
49517*/
49518static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
49519 int rc;
49520 sqlite3_int64 i;
49521 rc = winCurrentTimeInt64(pVfs, &i);
49522 if( !rc ){
49523 *prNow = i/86400000.0;
49524 }
49525 return rc;
49526}
49527
49528/*
49529** The idea is that this function works like a combination of
49530** GetLastError() and FormatMessage() on Windows (or errno and
49531** strerror_r() on Unix). After an error is returned by an OS
49532** function, SQLite calls this function with zBuf pointing to
49533** a buffer of nBuf bytes. The OS layer should populate the
49534** buffer with a nul-terminated UTF-8 encoded error message
49535** describing the last IO error to have occurred within the calling
49536** thread.
49537**
49538** If the error message is too large for the supplied buffer,
49539** it should be truncated. The return value of xGetLastError
49540** is zero if the error message fits in the buffer, or non-zero
49541** otherwise (if the message was truncated). If non-zero is returned,
49542** then it is not necessary to include the nul-terminator character
49543** in the output buffer.
49544**
49545** Not supplying an error message will have no adverse effect
49546** on SQLite. It is fine to have an implementation that never
49547** returns an error message:
49548**
49549** int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
49550** assert(zBuf[0]=='\0');
49551** return 0;
49552** }
49553**
49554** However if an error message is supplied, it will be incorporated
49555** by sqlite into the error message available to the user using
49556** sqlite3_errmsg(), possibly making IO errors easier to debug.
49557*/
49558static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
49559 DWORD e = osGetLastError();
49560 UNUSED_PARAMETER(pVfs)(void)(pVfs);
49561 if( nBuf>0 ) winGetLastErrorMsg(e, nBuf, zBuf);
49562 return e;
49563}
49564
49565/*
49566** Initialize and deinitialize the operating system interface.
49567*/
49568SQLITE_API int sqlite3_os_init(void){
49569 static sqlite3_vfs winVfs = {
49570 3, /* iVersion */
49571 sizeof(winFile), /* szOsFile */
49572 SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
49573 0, /* pNext */
49574 "win32", /* zName */
49575 &winAppData, /* pAppData */
49576 winOpen, /* xOpen */
49577 winDelete, /* xDelete */
49578 winAccess, /* xAccess */
49579 winFullPathname, /* xFullPathname */
49580 winDlOpen, /* xDlOpen */
49581 winDlError, /* xDlError */
49582 winDlSym, /* xDlSym */
49583 winDlClose, /* xDlClose */
49584 winRandomness, /* xRandomness */
49585 winSleep, /* xSleep */
49586 winCurrentTime, /* xCurrentTime */
49587 winGetLastError, /* xGetLastError */
49588 winCurrentTimeInt64, /* xCurrentTimeInt64 */
49589 winSetSystemCall, /* xSetSystemCall */
49590 winGetSystemCall, /* xGetSystemCall */
49591 winNextSystemCall, /* xNextSystemCall */
49592 };
49593#if defined(SQLITE_WIN32_HAS_WIDE)
49594 static sqlite3_vfs winLongPathVfs = {
49595 3, /* iVersion */
49596 sizeof(winFile), /* szOsFile */
49597 SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
49598 0, /* pNext */
49599 "win32-longpath", /* zName */
49600 &winAppData, /* pAppData */
49601 winOpen, /* xOpen */
49602 winDelete, /* xDelete */
49603 winAccess, /* xAccess */
49604 winFullPathname, /* xFullPathname */
49605 winDlOpen, /* xDlOpen */
49606 winDlError, /* xDlError */
49607 winDlSym, /* xDlSym */
49608 winDlClose, /* xDlClose */
49609 winRandomness, /* xRandomness */
49610 winSleep, /* xSleep */
49611 winCurrentTime, /* xCurrentTime */
49612 winGetLastError, /* xGetLastError */
49613 winCurrentTimeInt64, /* xCurrentTimeInt64 */
49614 winSetSystemCall, /* xSetSystemCall */
49615 winGetSystemCall, /* xGetSystemCall */
49616 winNextSystemCall, /* xNextSystemCall */
49617 };
49618#endif
49619 static sqlite3_vfs winNolockVfs = {
49620 3, /* iVersion */
49621 sizeof(winFile), /* szOsFile */
49622 SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
49623 0, /* pNext */
49624 "win32-none", /* zName */
49625 &winNolockAppData, /* pAppData */
49626 winOpen, /* xOpen */
49627 winDelete, /* xDelete */
49628 winAccess, /* xAccess */
49629 winFullPathname, /* xFullPathname */
49630 winDlOpen, /* xDlOpen */
49631 winDlError, /* xDlError */
49632 winDlSym, /* xDlSym */
49633 winDlClose, /* xDlClose */
49634 winRandomness, /* xRandomness */
49635 winSleep, /* xSleep */
49636 winCurrentTime, /* xCurrentTime */
49637 winGetLastError, /* xGetLastError */
49638 winCurrentTimeInt64, /* xCurrentTimeInt64 */
49639 winSetSystemCall, /* xSetSystemCall */
49640 winGetSystemCall, /* xGetSystemCall */
49641 winNextSystemCall, /* xNextSystemCall */
49642 };
49643#if defined(SQLITE_WIN32_HAS_WIDE)
49644 static sqlite3_vfs winLongPathNolockVfs = {
49645 3, /* iVersion */
49646 sizeof(winFile), /* szOsFile */
49647 SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
49648 0, /* pNext */
49649 "win32-longpath-none", /* zName */
49650 &winNolockAppData, /* pAppData */
49651 winOpen, /* xOpen */
49652 winDelete, /* xDelete */
49653 winAccess, /* xAccess */
49654 winFullPathname, /* xFullPathname */
49655 winDlOpen, /* xDlOpen */
49656 winDlError, /* xDlError */
49657 winDlSym, /* xDlSym */
49658 winDlClose, /* xDlClose */
49659 winRandomness, /* xRandomness */
49660 winSleep, /* xSleep */
49661 winCurrentTime, /* xCurrentTime */
49662 winGetLastError, /* xGetLastError */
49663 winCurrentTimeInt64, /* xCurrentTimeInt64 */
49664 winSetSystemCall, /* xSetSystemCall */
49665 winGetSystemCall, /* xGetSystemCall */
49666 winNextSystemCall, /* xNextSystemCall */
49667 };
49668#endif
49669
49670 /* Double-check that the aSyscall[] array has been constructed
49671 ** correctly. See ticket [bb3a86e890c8e96ab] */
49672 assert( ArraySize(aSyscall)==80 )((void) (0));
49673
49674 /* get memory map allocation granularity */
49675 memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
49676#if SQLITE_OS_WINRT
49677 osGetNativeSystemInfo(&winSysInfo);
49678#else
49679 osGetSystemInfo(&winSysInfo);
49680#endif
49681 assert( winSysInfo.dwAllocationGranularity>0 )((void) (0));
49682 assert( winSysInfo.dwPageSize>0 )((void) (0));
49683
49684 sqlite3_vfs_register(&winVfs, 1);
49685
49686#if defined(SQLITE_WIN32_HAS_WIDE)
49687 sqlite3_vfs_register(&winLongPathVfs, 0);
49688#endif
49689
49690 sqlite3_vfs_register(&winNolockVfs, 0);
49691
49692#if defined(SQLITE_WIN32_HAS_WIDE)
49693 sqlite3_vfs_register(&winLongPathNolockVfs, 0);
49694#endif
49695
49696#ifndef SQLITE_OMIT_WAL
49697 winBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS111);
49698#endif
49699
49700 return SQLITE_OK0;
49701}
49702
49703SQLITE_API int sqlite3_os_end(void){
49704#if SQLITE_OS_WINRT
49705 if( sleepObj!=NULL((void*)0) ){
49706 osCloseHandle(sleepObj);
49707 sleepObj = NULL((void*)0);
49708 }
49709#endif
49710
49711#ifndef SQLITE_OMIT_WAL
49712 winBigLock = 0;
49713#endif
49714
49715 return SQLITE_OK0;
49716}
49717
49718#endif /* SQLITE_OS_WIN */
49719
49720/************** End of os_win.c **********************************************/
49721/************** Begin file memdb.c *******************************************/
49722/*
49723** 2016-09-07
49724**
49725** The author disclaims copyright to this source code. In place of
49726** a legal notice, here is a blessing:
49727**
49728** May you do good and not evil.
49729** May you find forgiveness for yourself and forgive others.
49730** May you share freely, never taking more than you give.
49731**
49732******************************************************************************
49733**
49734** This file implements an in-memory VFS. A database is held as a contiguous
49735** block of memory.
49736**
49737** This file also implements interface sqlite3_serialize() and
49738** sqlite3_deserialize().
49739*/
49740/* #include "sqliteInt.h" */
49741#ifndef SQLITE_OMIT_DESERIALIZE
49742
49743/*
49744** Forward declaration of objects used by this utility
49745*/
49746typedef struct sqlite3_vfs MemVfs;
49747typedef struct MemFile MemFile;
49748typedef struct MemStore MemStore;
49749
49750/* Access to a lower-level VFS that (might) implement dynamic loading,
49751** access to randomness, etc.
49752*/
49753#define ORIGVFS(p)((sqlite3_vfs*)((p)->pAppData)) ((sqlite3_vfs*)((p)->pAppData))
49754
49755/* Storage for a memdb file.
49756**
49757** An memdb object can be shared or separate. Shared memdb objects can be
49758** used by more than one database connection. Mutexes are used by shared
49759** memdb objects to coordinate access. Separate memdb objects are only
49760** connected to a single database connection and do not require additional
49761** mutexes.
49762**
49763** Shared memdb objects have .zFName!=0 and .pMutex!=0. They are created
49764** using "file:/name?vfs=memdb". The first character of the name must be
49765** "/" or else the object will be a separate memdb object. All shared
49766** memdb objects are stored in memdb_g.apMemStore[] in an arbitrary order.
49767**
49768** Separate memdb objects are created using a name that does not begin
49769** with "/" or using sqlite3_deserialize().
49770**
49771** Access rules for shared MemStore objects:
49772**
49773** * .zFName is initialized when the object is created and afterwards
49774** is unchanged until the object is destroyed. So it can be accessed
49775** at any time as long as we know the object is not being destroyed,
49776** which means while either the SQLITE_MUTEX_STATIC_VFS1 or
49777** .pMutex is held or the object is not part of memdb_g.apMemStore[].
49778**
49779** * Can .pMutex can only be changed while holding the
49780** SQLITE_MUTEX_STATIC_VFS1 mutex or while the object is not part
49781** of memdb_g.apMemStore[].
49782**
49783** * Other fields can only be changed while holding the .pMutex mutex
49784** or when the .nRef is less than zero and the object is not part of
49785** memdb_g.apMemStore[].
49786**
49787** * The .aData pointer has the added requirement that it can can only
49788** be changed (for resizing) when nMmap is zero.
49789**
49790*/
49791struct MemStore {
49792 sqlite3_int64 sz; /* Size of the file */
49793 sqlite3_int64 szAlloc; /* Space allocated to aData */
49794 sqlite3_int64 szMax; /* Maximum allowed size of the file */
49795 unsigned char *aData; /* content of the file */
49796 sqlite3_mutex *pMutex; /* Used by shared stores only */
49797 int nMmap; /* Number of memory mapped pages */
49798 unsigned mFlags; /* Flags */
49799 int nRdLock; /* Number of readers */
49800 int nWrLock; /* Number of writers. (Always 0 or 1) */
49801 int nRef; /* Number of users of this MemStore */
49802 char *zFName; /* The filename for shared stores */
49803};
49804
49805/* An open file */
49806struct MemFile {
49807 sqlite3_file base; /* IO methods */
49808 MemStore *pStore; /* The storage */
49809 int eLock; /* Most recent lock against this file */
49810};
49811
49812/*
49813** File-scope variables for holding the memdb files that are accessible
49814** to multiple database connections in separate threads.
49815**
49816** Must hold SQLITE_MUTEX_STATIC_VFS1 to access any part of this object.
49817*/
49818static struct MemFS {
49819 int nMemStore; /* Number of shared MemStore objects */
49820 MemStore **apMemStore; /* Array of all shared MemStore objects */
49821} memdb_g;
49822
49823/*
49824** Methods for MemFile
49825*/
49826static int memdbClose(sqlite3_file*);
49827static int memdbRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
49828static int memdbWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
49829static int memdbTruncate(sqlite3_file*, sqlite3_int64 size);
49830static int memdbSync(sqlite3_file*, int flags);
49831static int memdbFileSize(sqlite3_file*, sqlite3_int64 *pSize);
49832static int memdbLock(sqlite3_file*, int);
49833/* static int memdbCheckReservedLock(sqlite3_file*, int *pResOut);// not used */
49834static int memdbFileControl(sqlite3_file*, int op, void *pArg);
49835/* static int memdbSectorSize(sqlite3_file*); // not used */
49836static int memdbDeviceCharacteristics(sqlite3_file*);
49837static int memdbFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
49838static int memdbUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
49839
49840/*
49841** Methods for MemVfs
49842*/
49843static int memdbOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
49844/* static int memdbDelete(sqlite3_vfs*, const char *zName, int syncDir); */
49845static int memdbAccess(sqlite3_vfs*, const char *zName, int flags, int *);
49846static int memdbFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
49847static void *memdbDlOpen(sqlite3_vfs*, const char *zFilename);
49848static void memdbDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
49849static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
49850static void memdbDlClose(sqlite3_vfs*, void*);
49851static int memdbRandomness(sqlite3_vfs*, int nByte, char *zOut);
49852static int memdbSleep(sqlite3_vfs*, int microseconds);
49853/* static int memdbCurrentTime(sqlite3_vfs*, double*); */
49854static int memdbGetLastError(sqlite3_vfs*, int, char *);
49855static int memdbCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
49856
49857static sqlite3_vfs memdb_vfs = {
49858 2, /* iVersion */
49859 0, /* szOsFile (set when registered) */
49860 1024, /* mxPathname */
49861 0, /* pNext */
49862 "memdb", /* zName */
49863 0, /* pAppData (set when registered) */
49864 memdbOpen, /* xOpen */
49865 0, /* memdbDelete, */ /* xDelete */
49866 memdbAccess, /* xAccess */
49867 memdbFullPathname, /* xFullPathname */
49868 memdbDlOpen, /* xDlOpen */
49869 memdbDlError, /* xDlError */
49870 memdbDlSym, /* xDlSym */
49871 memdbDlClose, /* xDlClose */
49872 memdbRandomness, /* xRandomness */
49873 memdbSleep, /* xSleep */
49874 0, /* memdbCurrentTime, */ /* xCurrentTime */
49875 memdbGetLastError, /* xGetLastError */
49876 memdbCurrentTimeInt64, /* xCurrentTimeInt64 */
49877 0, /* xSetSystemCall */
49878 0, /* xGetSystemCall */
49879 0, /* xNextSystemCall */
49880};
49881
49882static const sqlite3_io_methods memdb_io_methods = {
49883 3, /* iVersion */
49884 memdbClose, /* xClose */
49885 memdbRead, /* xRead */
49886 memdbWrite, /* xWrite */
49887 memdbTruncate, /* xTruncate */
49888 memdbSync, /* xSync */
49889 memdbFileSize, /* xFileSize */
49890 memdbLock, /* xLock */
49891 memdbLock, /* xUnlock - same as xLock in this case */
49892 0, /* memdbCheckReservedLock, */ /* xCheckReservedLock */
49893 memdbFileControl, /* xFileControl */
49894 0, /* memdbSectorSize,*/ /* xSectorSize */
49895 memdbDeviceCharacteristics, /* xDeviceCharacteristics */
49896 0, /* xShmMap */
49897 0, /* xShmLock */
49898 0, /* xShmBarrier */
49899 0, /* xShmUnmap */
49900 memdbFetch, /* xFetch */
49901 memdbUnfetch /* xUnfetch */
49902};
49903
49904/*
49905** Enter/leave the mutex on a MemStore
49906*/
49907#if defined(SQLITE_THREADSAFE2) && SQLITE_THREADSAFE2==0
49908static void memdbEnter(MemStore *p){
49909 UNUSED_PARAMETER(p)(void)(p);
49910}
49911static void memdbLeave(MemStore *p){
49912 UNUSED_PARAMETER(p)(void)(p);
49913}
49914#else
49915static void memdbEnter(MemStore *p){
49916 sqlite3_mutex_enter(p->pMutex);
49917}
49918static void memdbLeave(MemStore *p){
49919 sqlite3_mutex_leave(p->pMutex);
49920}
49921#endif
49922
49923
49924
49925/*
49926** Close an memdb-file.
49927** Free the underlying MemStore object when its refcount drops to zero
49928** or less.
49929*/
49930static int memdbClose(sqlite3_file *pFile){
49931 MemStore *p = ((MemFile*)pFile)->pStore;
49932 if( p->zFName ){
49933 int i;
49934#ifndef SQLITE_MUTEX_OMIT
49935 sqlite3_mutex *pVfsMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS111);
49936#endif
49937 sqlite3_mutex_enter(pVfsMutex);
49938 for(i=0; ALWAYS(i<memdb_g.nMemStore)(i<memdb_g.nMemStore); i++){
49939 if( memdb_g.apMemStore[i]==p ){
49940 memdbEnter(p);
49941 if( p->nRef==1 ){
49942 memdb_g.apMemStore[i] = memdb_g.apMemStore[--memdb_g.nMemStore];
49943 if( memdb_g.nMemStore==0 ){
49944 sqlite3_free(memdb_g.apMemStore);
49945 memdb_g.apMemStore = 0;
49946 }
49947 }
49948 break;
49949 }
49950 }
49951 sqlite3_mutex_leave(pVfsMutex);
49952 }else{
49953 memdbEnter(p);
49954 }
49955 p->nRef--;
49956 if( p->nRef<=0 ){
49957 if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE1 ){
49958 sqlite3_free(p->aData);
49959 }
49960 memdbLeave(p);
49961 sqlite3_mutex_free(p->pMutex);
49962 sqlite3_free(p);
49963 }else{
49964 memdbLeave(p);
49965 }
49966 return SQLITE_OK0;
49967}
49968
49969/*
49970** Read data from an memdb-file.
49971*/
49972static int memdbRead(
49973 sqlite3_file *pFile,
49974 void *zBuf,
49975 int iAmt,
49976 sqlite_int64 iOfst
49977){
49978 MemStore *p = ((MemFile*)pFile)->pStore;
49979 memdbEnter(p);
49980 if( iOfst+iAmt>p->sz ){
49981 memset(zBuf, 0, iAmt);
49982 if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst);
49983 memdbLeave(p);
49984 return SQLITE_IOERR_SHORT_READ(10 | (2<<8));
49985 }
49986 memcpy(zBuf, p->aData+iOfst, iAmt);
49987 memdbLeave(p);
49988 return SQLITE_OK0;
49989}
49990
49991/*
49992** Try to enlarge the memory allocation to hold at least sz bytes
49993*/
49994static int memdbEnlarge(MemStore *p, sqlite3_int64 newSz){
49995 unsigned char *pNew;
49996 if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE2)==0 || NEVER(p->nMmap>0)(p->nMmap>0) ){
49997 return SQLITE_FULL13;
49998 }
49999 if( newSz>p->szMax ){
50000 return SQLITE_FULL13;
50001 }
50002 newSz *= 2;
50003 if( newSz>p->szMax ) newSz = p->szMax;
50004 pNew = sqlite3Realloc(p->aData, newSz);
50005 if( pNew==0 ) return SQLITE_IOERR_NOMEM(10 | (12<<8));
50006 p->aData = pNew;
50007 p->szAlloc = newSz;
50008 return SQLITE_OK0;
50009}
50010
50011/*
50012** Write data to an memdb-file.
50013*/
50014static int memdbWrite(
50015 sqlite3_file *pFile,
50016 const void *z,
50017 int iAmt,
50018 sqlite_int64 iOfst
50019){
50020 MemStore *p = ((MemFile*)pFile)->pStore;
50021 memdbEnter(p);
50022 if( NEVER(p->mFlags & SQLITE_DESERIALIZE_READONLY)(p->mFlags & 4) ){
50023 /* Can't happen: memdbLock() will return SQLITE_READONLY before
50024 ** reaching this point */
50025 memdbLeave(p);
50026 return SQLITE_IOERR_WRITE(10 | (3<<8));
50027 }
50028 if( iOfst+iAmt>p->sz ){
50029 int rc;
50030 if( iOfst+iAmt>p->szAlloc
50031 && (rc = memdbEnlarge(p, iOfst+iAmt))!=SQLITE_OK0
50032 ){
50033 memdbLeave(p);
50034 return rc;
50035 }
50036 if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz);
50037 p->sz = iOfst+iAmt;
50038 }
50039 memcpy(p->aData+iOfst, z, iAmt);
50040 memdbLeave(p);
50041 return SQLITE_OK0;
50042}
50043
50044/*
50045** Truncate an memdb-file.
50046**
50047** In rollback mode (which is always the case for memdb, as it does not
50048** support WAL mode) the truncate() method is only used to reduce
50049** the size of a file, never to increase the size.
50050*/
50051static int memdbTruncate(sqlite3_file *pFile, sqlite_int64 size){
50052 MemStore *p = ((MemFile*)pFile)->pStore;
50053 int rc = SQLITE_OK0;
50054 memdbEnter(p);
50055 if( size>p->sz ){
50056 /* This can only happen with a corrupt wal mode db */
50057 rc = SQLITE_CORRUPT11;
50058 }else{
50059 p->sz = size;
50060 }
50061 memdbLeave(p);
50062 return rc;
50063}
50064
50065/*
50066** Sync an memdb-file.
50067*/
50068static int memdbSync(sqlite3_file *pFile, int flags){
50069 UNUSED_PARAMETER(pFile)(void)(pFile);
50070 UNUSED_PARAMETER(flags)(void)(flags);
50071 return SQLITE_OK0;
50072}
50073
50074/*
50075** Return the current file-size of an memdb-file.
50076*/
50077static int memdbFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
50078 MemStore *p = ((MemFile*)pFile)->pStore;
50079 memdbEnter(p);
50080 *pSize = p->sz;
50081 memdbLeave(p);
50082 return SQLITE_OK0;
50083}
50084
50085/*
50086** Lock an memdb-file.
50087*/
50088static int memdbLock(sqlite3_file *pFile, int eLock){
50089 MemFile *pThis = (MemFile*)pFile;
50090 MemStore *p = pThis->pStore;
50091 int rc = SQLITE_OK0;
50092 if( eLock==pThis->eLock ) return SQLITE_OK0;
50093 memdbEnter(p);
50094 if( eLock>SQLITE_LOCK_SHARED1 ){
50095 if( p->mFlags & SQLITE_DESERIALIZE_READONLY4 ){
50096 rc = SQLITE_READONLY8;
50097 }else if( pThis->eLock<=SQLITE_LOCK_SHARED1 ){
50098 if( p->nWrLock ){
50099 rc = SQLITE_BUSY5;
50100 }else{
50101 p->nWrLock = 1;
50102 }
50103 }
50104 }else if( eLock==SQLITE_LOCK_SHARED1 ){
50105 if( pThis->eLock > SQLITE_LOCK_SHARED1 ){
50106 assert( p->nWrLock==1 )((void) (0));
50107 p->nWrLock = 0;
50108 }else if( p->nWrLock ){
50109 rc = SQLITE_BUSY5;
50110 }else{
50111 p->nRdLock++;
50112 }
50113 }else{
50114 assert( eLock==SQLITE_LOCK_NONE )((void) (0));
50115 if( pThis->eLock>SQLITE_LOCK_SHARED1 ){
50116 assert( p->nWrLock==1 )((void) (0));
50117 p->nWrLock = 0;
50118 }
50119 assert( p->nRdLock>0 )((void) (0));
50120 p->nRdLock--;
50121 }
50122 if( rc==SQLITE_OK0 ) pThis->eLock = eLock;
50123 memdbLeave(p);
50124 return rc;
50125}
50126
50127#if 0
50128/*
50129** This interface is only used for crash recovery, which does not
50130** occur on an in-memory database.
50131*/
50132static int memdbCheckReservedLock(sqlite3_file *pFile, int *pResOut){
50133 *pResOut = 0;
50134 return SQLITE_OK0;
50135}
50136#endif
50137
50138
50139/*
50140** File control method. For custom operations on an memdb-file.
50141*/
50142static int memdbFileControl(sqlite3_file *pFile, int op, void *pArg){
50143 MemStore *p = ((MemFile*)pFile)->pStore;
50144 int rc = SQLITE_NOTFOUND12;
50145 memdbEnter(p);
50146 if( op==SQLITE_FCNTL_VFSNAME12 ){
50147 *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz);
50148 rc = SQLITE_OK0;
50149 }
50150 if( op==SQLITE_FCNTL_SIZE_LIMIT36 ){
50151 sqlite3_int64 iLimit = *(sqlite3_int64*)pArg;
50152 if( iLimit<p->sz ){
50153 if( iLimit<0 ){
50154 iLimit = p->szMax;
50155 }else{
50156 iLimit = p->sz;
50157 }
50158 }
50159 p->szMax = iLimit;
50160 *(sqlite3_int64*)pArg = iLimit;
50161 rc = SQLITE_OK0;
50162 }
50163 memdbLeave(p);
50164 return rc;
50165}
50166
50167#if 0 /* Not used because of SQLITE_IOCAP_POWERSAFE_OVERWRITE */
50168/*
50169** Return the sector-size in bytes for an memdb-file.
50170*/
50171static int memdbSectorSize(sqlite3_file *pFile){
50172 return 1024;
50173}
50174#endif
50175
50176/*
50177** Return the device characteristic flags supported by an memdb-file.
50178*/
50179static int memdbDeviceCharacteristics(sqlite3_file *pFile){
50180 UNUSED_PARAMETER(pFile)(void)(pFile);
50181 return SQLITE_IOCAP_ATOMIC0x00000001 |
50182 SQLITE_IOCAP_POWERSAFE_OVERWRITE0x00001000 |
50183 SQLITE_IOCAP_SAFE_APPEND0x00000200 |
50184 SQLITE_IOCAP_SEQUENTIAL0x00000400;
50185}
50186
50187/* Fetch a page of a memory-mapped file */
50188static int memdbFetch(
50189 sqlite3_file *pFile,
50190 sqlite3_int64 iOfst,
50191 int iAmt,
50192 void **pp
50193){
50194 MemStore *p = ((MemFile*)pFile)->pStore;
50195 memdbEnter(p);
50196 if( iOfst+iAmt>p->sz || (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE2)!=0 ){
50197 *pp = 0;
50198 }else{
50199 p->nMmap++;
50200 *pp = (void*)(p->aData + iOfst);
50201 }
50202 memdbLeave(p);
50203 return SQLITE_OK0;
50204}
50205
50206/* Release a memory-mapped page */
50207static int memdbUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
50208 MemStore *p = ((MemFile*)pFile)->pStore;
50209 UNUSED_PARAMETER(iOfst)(void)(iOfst);
50210 UNUSED_PARAMETER(pPage)(void)(pPage);
50211 memdbEnter(p);
50212 p->nMmap--;
50213 memdbLeave(p);
50214 return SQLITE_OK0;
50215}
50216
50217/*
50218** Open an mem file handle.
50219*/
50220static int memdbOpen(
50221 sqlite3_vfs *pVfs,
50222 const char *zName,
50223 sqlite3_file *pFd,
50224 int flags,
50225 int *pOutFlags
50226){
50227 MemFile *pFile = (MemFile*)pFd;
50228 MemStore *p = 0;
50229 int szName;
50230 UNUSED_PARAMETER(pVfs)(void)(pVfs);
50231
50232 memset(pFile, 0, sizeof(*pFile));
50233 szName = sqlite3Strlen30(zName);
50234 if( szName>1 && zName[0]=='/' ){
50235 int i;
50236#ifndef SQLITE_MUTEX_OMIT
50237 sqlite3_mutex *pVfsMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS111);
50238#endif
50239 sqlite3_mutex_enter(pVfsMutex);
50240 for(i=0; i<memdb_g.nMemStore; i++){
50241 if( strcmp(memdb_g.apMemStore[i]->zFName,zName)==0 ){
50242 p = memdb_g.apMemStore[i];
50243 break;
50244 }
50245 }
50246 if( p==0 ){
50247 MemStore **apNew;
50248 p = sqlite3Malloc( sizeof(*p) + szName + 3 );
50249 if( p==0 ){
50250 sqlite3_mutex_leave(pVfsMutex);
50251 return SQLITE_NOMEM7;
50252 }
50253 apNew = sqlite3Realloc(memdb_g.apMemStore,
50254 sizeof(apNew[0])*(memdb_g.nMemStore+1) );
50255 if( apNew==0 ){
50256 sqlite3_free(p);
50257 sqlite3_mutex_leave(pVfsMutex);
50258 return SQLITE_NOMEM7;
50259 }
50260 apNew[memdb_g.nMemStore++] = p;
50261 memdb_g.apMemStore = apNew;
50262 memset(p, 0, sizeof(*p));
50263 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE2|SQLITE_DESERIALIZE_FREEONCLOSE1;
50264 p->szMax = sqlite3GlobalConfigsqlite3Config.mxMemdbSize;
50265 p->zFName = (char*)&p[1];
50266 memcpy(p->zFName, zName, szName+1);
50267 p->pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST0);
50268 if( p->pMutex==0 ){
50269 memdb_g.nMemStore--;
50270 sqlite3_free(p);
50271 sqlite3_mutex_leave(pVfsMutex);
50272 return SQLITE_NOMEM7;
50273 }
50274 p->nRef = 1;
50275 memdbEnter(p);
50276 }else{
50277 memdbEnter(p);
50278 p->nRef++;
50279 }
50280 sqlite3_mutex_leave(pVfsMutex);
50281 }else{
50282 p = sqlite3Malloc( sizeof(*p) );
50283 if( p==0 ){
50284 return SQLITE_NOMEM7;
50285 }
50286 memset(p, 0, sizeof(*p));
50287 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE2 | SQLITE_DESERIALIZE_FREEONCLOSE1;
50288 p->szMax = sqlite3GlobalConfigsqlite3Config.mxMemdbSize;
50289 }
50290 pFile->pStore = p;
50291 if( pOutFlags!=0 ){
50292 *pOutFlags = flags | SQLITE_OPEN_MEMORY0x00000080;
50293 }
50294 pFd->pMethods = &memdb_io_methods;
50295 memdbLeave(p);
50296 return SQLITE_OK0;
50297}
50298
50299#if 0 /* Only used to delete rollback journals, super-journals, and WAL
50300 ** files, none of which exist in memdb. So this routine is never used */
50301/*
50302** Delete the file located at zPath. If the dirSync argument is true,
50303** ensure the file-system modifications are synced to disk before
50304** returning.
50305*/
50306static int memdbDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
50307 return SQLITE_IOERR_DELETE(10 | (10<<8));
50308}
50309#endif
50310
50311/*
50312** Test for access permissions. Return true if the requested permission
50313** is available, or false otherwise.
50314**
50315** With memdb, no files ever exist on disk. So always return false.
50316*/
50317static int memdbAccess(
50318 sqlite3_vfs *pVfs,
50319 const char *zPath,
50320 int flags,
50321 int *pResOut
50322){
50323 UNUSED_PARAMETER(pVfs)(void)(pVfs);
50324 UNUSED_PARAMETER(zPath)(void)(zPath);
50325 UNUSED_PARAMETER(flags)(void)(flags);
50326 *pResOut = 0;
50327 return SQLITE_OK0;
50328}
50329
50330/*
50331** Populate buffer zOut with the full canonical pathname corresponding
50332** to the pathname in zPath. zOut is guaranteed to point to a buffer
50333** of at least (INST_MAX_PATHNAME+1) bytes.
50334*/
50335static int memdbFullPathname(
50336 sqlite3_vfs *pVfs,
50337 const char *zPath,
50338 int nOut,
50339 char *zOut
50340){
50341 UNUSED_PARAMETER(pVfs)(void)(pVfs);
50342 sqlite3_snprintf(nOut, zOut, "%s", zPath);
50343 return SQLITE_OK0;
50344}
50345
50346/*
50347** Open the dynamic library located at zPath and return a handle.
50348*/
50349static void *memdbDlOpen(sqlite3_vfs *pVfs, const char *zPath){
50350 return ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xDlOpen(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), zPath);
50351}
50352
50353/*
50354** Populate the buffer zErrMsg (size nByte bytes) with a human readable
50355** utf-8 string describing the most recent error encountered associated
50356** with dynamic libraries.
50357*/
50358static void memdbDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
50359 ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xDlError(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), nByte, zErrMsg);
50360}
50361
50362/*
50363** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
50364*/
50365static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
50366 return ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xDlSym(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), p, zSym);
50367}
50368
50369/*
50370** Close the dynamic library handle pHandle.
50371*/
50372static void memdbDlClose(sqlite3_vfs *pVfs, void *pHandle){
50373 ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xDlClose(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), pHandle);
50374}
50375
50376/*
50377** Populate the buffer pointed to by zBufOut with nByte bytes of
50378** random data.
50379*/
50380static int memdbRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
50381 return ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xRandomness(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), nByte, zBufOut);
50382}
50383
50384/*
50385** Sleep for nMicro microseconds. Return the number of microseconds
50386** actually slept.
50387*/
50388static int memdbSleep(sqlite3_vfs *pVfs, int nMicro){
50389 return ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xSleep(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), nMicro);
50390}
50391
50392#if 0 /* Never used. Modern cores only call xCurrentTimeInt64() */
50393/*
50394** Return the current time as a Julian Day number in *pTimeOut.
50395*/
50396static int memdbCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
50397 return ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xCurrentTime(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), pTimeOut);
50398}
50399#endif
50400
50401static int memdbGetLastError(sqlite3_vfs *pVfs, int a, char *b){
50402 return ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xGetLastError(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), a, b);
50403}
50404static int memdbCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
50405 return ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xCurrentTimeInt64(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), p);
50406}
50407
50408/*
50409** Translate a database connection pointer and schema name into a
50410** MemFile pointer.
50411*/
50412static MemFile *memdbFromDbSchema(sqlite3 *db, const char *zSchema){
50413 MemFile *p = 0;
50414 MemStore *pStore;
50415 int rc = sqlite3_file_control(db, zSchema, SQLITE_FCNTL_FILE_POINTER7, &p);
50416 if( rc ) return 0;
50417 if( p->base.pMethods!=&memdb_io_methods ) return 0;
50418 pStore = p->pStore;
50419 memdbEnter(pStore);
50420 if( pStore->zFName!=0 ) p = 0;
50421 memdbLeave(pStore);
50422 return p;
50423}
50424
50425/*
50426** Return the serialization of a database
50427*/
50428SQLITE_API unsigned char *sqlite3_serialize(
50429 sqlite3 *db, /* The database connection */
50430 const char *zSchema, /* Which database within the connection */
50431 sqlite3_int64 *piSize, /* Write size here, if not NULL */
50432 unsigned int mFlags /* Maybe SQLITE_SERIALIZE_NOCOPY */
50433){
50434 MemFile *p;
50435 int iDb;
50436 Btree *pBt;
50437 sqlite3_int64 sz;
50438 int szPage = 0;
50439 sqlite3_stmt *pStmt = 0;
50440 unsigned char *pOut;
50441 char *zSql;
50442 int rc;
50443
50444#ifdef SQLITE_ENABLE_API_ARMOR1
50445 if( !sqlite3SafetyCheckOk(db) ){
50446 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(50446);
50447 return 0;
50448 }
50449#endif
50450
50451 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
50452 p = memdbFromDbSchema(db, zSchema);
50453 iDb = sqlite3FindDbName(db, zSchema);
50454 if( piSize ) *piSize = -1;
50455 if( iDb<0 ) return 0;
50456 if( p ){
50457 MemStore *pStore = p->pStore;
50458 assert( pStore->pMutex==0 )((void) (0));
50459 if( piSize ) *piSize = pStore->sz;
50460 if( mFlags & SQLITE_SERIALIZE_NOCOPY0x001 ){
50461 pOut = pStore->aData;
50462 }else{
50463 pOut = sqlite3_malloc64( pStore->sz );
50464 if( pOut ) memcpy(pOut, pStore->aData, pStore->sz);
50465 }
50466 return pOut;
50467 }
50468 pBt = db->aDb[iDb].pBt;
50469 if( pBt==0 ) return 0;
50470 szPage = sqlite3BtreeGetPageSize(pBt);
50471 zSql = sqlite3_mprintf("PRAGMA \"%w\".page_count", zSchema);
50472 rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM7;
50473 sqlite3_free(zSql);
50474 if( rc ) return 0;
50475 rc = sqlite3_step(pStmt);
50476 if( rc!=SQLITE_ROW100 ){
50477 pOut = 0;
50478 }else{
50479 sz = sqlite3_column_int64(pStmt, 0)*szPage;
50480 if( piSize ) *piSize = sz;
50481 if( mFlags & SQLITE_SERIALIZE_NOCOPY0x001 ){
50482 pOut = 0;
50483 }else{
50484 pOut = sqlite3_malloc64( sz );
50485 if( pOut ){
50486 int nPage = sqlite3_column_int(pStmt, 0);
50487 Pager *pPager = sqlite3BtreePager(pBt);
50488 int pgno;
50489 for(pgno=1; pgno<=nPage; pgno++){
50490 DbPage *pPage = 0;
50491 unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
50492 rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pPage, 0);
50493 if( rc==SQLITE_OK0 ){
50494 memcpy(pTo, sqlite3PagerGetData(pPage), szPage);
50495 }else{
50496 memset(pTo, 0, szPage);
50497 }
50498 sqlite3PagerUnref(pPage);
50499 }
50500 }
50501 }
50502 }
50503 sqlite3_finalize(pStmt);
50504 return pOut;
50505}
50506
50507/* Convert zSchema to a MemDB and initialize its content.
50508*/
50509SQLITE_API int sqlite3_deserialize(
50510 sqlite3 *db, /* The database connection */
50511 const char *zSchema, /* Which DB to reopen with the deserialization */
50512 unsigned char *pData, /* The serialized database content */
50513 sqlite3_int64 szDb, /* Number bytes in the deserialization */
50514 sqlite3_int64 szBuf, /* Total size of buffer pData[] */
50515 unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */
50516){
50517 MemFile *p;
50518 char *zSql;
50519 sqlite3_stmt *pStmt = 0;
50520 int rc;
50521 int iDb;
50522
50523#ifdef SQLITE_ENABLE_API_ARMOR1
50524 if( !sqlite3SafetyCheckOk(db) ){
50525 return SQLITE_MISUSE_BKPTsqlite3MisuseError(50525);
50526 }
50527 if( szDb<0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(50527);
50528 if( szBuf<0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(50528);
50529#endif
50530
50531 sqlite3_mutex_enter(db->mutex);
50532 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
50533 iDb = sqlite3FindDbName(db, zSchema);
50534 testcase( iDb==1 );
50535 if( iDb<2 && iDb!=0 ){
50536 rc = SQLITE_ERROR1;
50537 goto end_deserialize;
50538 }
50539 zSql = sqlite3_mprintf("ATTACH x AS %Q", zSchema);
50540 if( zSql==0 ){
50541 rc = SQLITE_NOMEM7;
50542 }else{
50543 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
50544 sqlite3_free(zSql);
50545 }
50546 if( rc ) goto end_deserialize;
50547 db->init.iDb = (u8)iDb;
50548 db->init.reopenMemdb = 1;
50549 rc = sqlite3_step(pStmt);
50550 db->init.reopenMemdb = 0;
50551 if( rc!=SQLITE_DONE101 ){
50552 rc = SQLITE_ERROR1;
50553 goto end_deserialize;
50554 }
50555 p = memdbFromDbSchema(db, zSchema);
50556 if( p==0 ){
50557 rc = SQLITE_ERROR1;
50558 }else{
50559 MemStore *pStore = p->pStore;
50560 pStore->aData = pData;
50561 pData = 0;
50562 pStore->sz = szDb;
50563 pStore->szAlloc = szBuf;
50564 pStore->szMax = szBuf;
50565 if( pStore->szMax<sqlite3GlobalConfigsqlite3Config.mxMemdbSize ){
50566 pStore->szMax = sqlite3GlobalConfigsqlite3Config.mxMemdbSize;
50567 }
50568 pStore->mFlags = mFlags;
50569 rc = SQLITE_OK0;
50570 }
50571
50572end_deserialize:
50573 sqlite3_finalize(pStmt);
50574 if( pData && (mFlags & SQLITE_DESERIALIZE_FREEONCLOSE1)!=0 ){
50575 sqlite3_free(pData);
50576 }
50577 sqlite3_mutex_leave(db->mutex);
50578 return rc;
50579}
50580
50581/*
50582** This routine is called when the extension is loaded.
50583** Register the new VFS.
50584*/
50585SQLITE_PRIVATEstatic int sqlite3MemdbInit(void){
50586 sqlite3_vfs *pLower = sqlite3_vfs_find(0);
50587 unsigned int sz;
50588 if( NEVER(pLower==0)(pLower==0) ) return SQLITE_ERROR1;
50589 sz = pLower->szOsFile;
50590 memdb_vfs.pAppData = pLower;
50591 /* The following conditional can only be true when compiled for
50592 ** Windows x86 and SQLITE_MAX_MMAP_SIZE=0. We always leave
50593 ** it in, to be safe, but it is marked as NO_TEST since there
50594 ** is no way to reach it under most builds. */
50595 if( sz<sizeof(MemFile) ) sz = sizeof(MemFile); /*NO_TEST*/
50596 memdb_vfs.szOsFile = sz;
50597 return sqlite3_vfs_register(&memdb_vfs, 0);
50598}
50599#endif /* SQLITE_OMIT_DESERIALIZE */
50600
50601/************** End of memdb.c ***********************************************/
50602/************** Begin file bitvec.c ******************************************/
50603/*
50604** 2008 February 16
50605**
50606** The author disclaims copyright to this source code. In place of
50607** a legal notice, here is a blessing:
50608**
50609** May you do good and not evil.
50610** May you find forgiveness for yourself and forgive others.
50611** May you share freely, never taking more than you give.
50612**
50613*************************************************************************
50614** This file implements an object that represents a fixed-length
50615** bitmap. Bits are numbered starting with 1.
50616**
50617** A bitmap is used to record which pages of a database file have been
50618** journalled during a transaction, or which pages have the "dont-write"
50619** property. Usually only a few pages are meet either condition.
50620** So the bitmap is usually sparse and has low cardinality.
50621** But sometimes (for example when during a DROP of a large table) most
50622** or all of the pages in a database can get journalled. In those cases,
50623** the bitmap becomes dense with high cardinality. The algorithm needs
50624** to handle both cases well.
50625**
50626** The size of the bitmap is fixed when the object is created.
50627**
50628** All bits are clear when the bitmap is created. Individual bits
50629** may be set or cleared one at a time.
50630**
50631** Test operations are about 100 times more common that set operations.
50632** Clear operations are exceedingly rare. There are usually between
50633** 5 and 500 set operations per Bitvec object, though the number of sets can
50634** sometimes grow into tens of thousands or larger. The size of the
50635** Bitvec object is the number of pages in the database file at the
50636** start of a transaction, and is thus usually less than a few thousand,
50637** but can be as large as 2 billion for a really big database.
50638*/
50639/* #include "sqliteInt.h" */
50640
50641/* Size of the Bitvec structure in bytes. */
50642#define BITVEC_SZ512 512
50643
50644/* Round the union size down to the nearest pointer boundary, since that's how
50645** it will be aligned within the Bitvec struct. */
50646#define BITVEC_USIZE(((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*)) \
50647 (((BITVEC_SZ512-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
50648
50649/* Type of the array "element" for the bitmap representation.
50650** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE.
50651** Setting this to the "natural word" size of your CPU may improve
50652** performance. */
50653#define BITVEC_TELEMu8 u8
50654/* Size, in bits, of the bitmap element. */
50655#define BITVEC_SZELEM8 8
50656/* Number of elements in a bitmap array. */
50657#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))
50658/* Number of bits in the bitmap array. */
50659#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)
50660
50661/* Number of u32 values in hash table. */
50662#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))
50663/* Maximum number of entries in hash table before
50664** sub-dividing and re-hashing. */
50665#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)
50666/* Hashing function for the aHash representation.
50667** Empirical testing showed that the *37 multiplier
50668** (an arbitrary prime)in the hash function provided
50669** no fewer collisions than the no-op *1. */
50670#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))
)
50671
50672#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 *))
50673
50674
50675/*
50676** A bitmap is an instance of the following structure.
50677**
50678** This bitmap records the existence of zero or more bits
50679** with values between 1 and iSize, inclusive.
50680**
50681** There are three possible representations of the bitmap.
50682** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
50683** bitmap. The least significant bit is bit 1.
50684**
50685** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
50686** a hash table that will hold up to BITVEC_MXHASH distinct values.
50687**
50688** Otherwise, the value i is redirected into one of BITVEC_NPTR
50689** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
50690** handles up to iDivisor separate values of i. apSub[0] holds
50691** values between 1 and iDivisor. apSub[1] holds values between
50692** iDivisor+1 and 2*iDivisor. apSub[N] holds values between
50693** N*iDivisor+1 and (N+1)*iDivisor. Each subbitmap is normalized
50694** to hold deal with values between 1 and iDivisor.
50695*/
50696struct Bitvec {
50697 u32 iSize; /* Maximum bit index. Max iSize is 4,294,967,296. */
50698 u32 nSet; /* Number of bits that are set - only valid for aHash
50699 ** element. Max is BITVEC_NINT. For BITVEC_SZ of 512,
50700 ** this would be 125. */
50701 u32 iDivisor; /* Number of bits handled by each apSub[] entry. */
50702 /* Should >=0 for apSub element. */
50703 /* Max iDivisor is max(u32) / BITVEC_NPTR + 1. */
50704 /* For a BITVEC_SZ of 512, this would be 34,359,739. */
50705 union {
50706 BITVEC_TELEMu8 aBitmap[BITVEC_NELEM((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u8))
]; /* Bitmap representation */
50707 u32 aHash[BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
]; /* Hash table representation */
50708 Bitvec *apSub[BITVEC_NPTR((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(Bitvec *))
]; /* Recursive representation */
50709 } u;
50710};
50711
50712/*
50713** Create a new bitmap object able to handle bits between 0 and iSize,
50714** inclusive. Return a pointer to the new object. Return NULL if
50715** malloc fails.
50716*/
50717SQLITE_PRIVATEstatic Bitvec *sqlite3BitvecCreate(u32 iSize){
50718 Bitvec *p;
50719 assert( sizeof(*p)==BITVEC_SZ )((void) (0));
50720 p = sqlite3MallocZero( sizeof(*p) );
50721 if( p ){
50722 p->iSize = iSize;
50723 }
50724 return p;
50725}
50726
50727/*
50728** Check to see if the i-th bit is set. Return true or false.
50729** If p is NULL (if the bitmap has not been created) or if
50730** i is out of range, then return false.
50731*/
50732SQLITE_PRIVATEstatic int sqlite3BitvecTestNotNull(Bitvec *p, u32 i){
50733 assert( p!=0 )((void) (0));
50734 i--;
50735 if( i>=p->iSize ) return 0;
50736 while( p->iDivisor ){
50737 u32 bin = i/p->iDivisor;
50738 i = i%p->iDivisor;
50739 p = p->u.apSub[bin];
50740 if (!p) {
50741 return 0;
50742 }
50743 }
50744 if( p->iSize<=BITVEC_NBIT(((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/
sizeof(u8))*8)
){
50745 return (p->u.aBitmap[i/BITVEC_SZELEM8] & (1<<(i&(BITVEC_SZELEM8-1))))!=0;
50746 } else{
50747 u32 h = BITVEC_HASH(i++)(((i++)*1)%((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(
Bitvec*))/sizeof(u32)))
;
50748 while( p->u.aHash[h] ){
50749 if( p->u.aHash[h]==i ) return 1;
50750 h = (h+1) % BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
;
50751 }
50752 return 0;
50753 }
50754}
50755SQLITE_PRIVATEstatic int sqlite3BitvecTest(Bitvec *p, u32 i){
50756 return p!=0 && sqlite3BitvecTestNotNull(p,i);
50757}
50758
50759/*
50760** Set the i-th bit. Return 0 on success and an error code if
50761** anything goes wrong.
50762**
50763** This routine might cause sub-bitmaps to be allocated. Failing
50764** to get the memory needed to hold the sub-bitmap is the only
50765** that can go wrong with an insert, assuming p and i are valid.
50766**
50767** The calling function must ensure that p is a valid Bitvec object
50768** and that the value for "i" is within range of the Bitvec object.
50769** Otherwise the behavior is undefined.
50770*/
50771SQLITE_PRIVATEstatic int sqlite3BitvecSet(Bitvec *p, u32 i){
50772 u32 h;
50773 if( p==0 ) return SQLITE_OK0;
50774 assert( i>0 )((void) (0));
50775 assert( i<=p->iSize )((void) (0));
50776 i--;
50777 while((p->iSize > BITVEC_NBIT(((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/
sizeof(u8))*8)
) && p->iDivisor) {
50778 u32 bin = i/p->iDivisor;
50779 i = i%p->iDivisor;
50780 if( p->u.apSub[bin]==0 ){
50781 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
50782 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT7;
50783 }
50784 p = p->u.apSub[bin];
50785 }
50786 if( p->iSize<=BITVEC_NBIT(((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/
sizeof(u8))*8)
){
50787 p->u.aBitmap[i/BITVEC_SZELEM8] |= 1 << (i&(BITVEC_SZELEM8-1));
50788 return SQLITE_OK0;
50789 }
50790 h = BITVEC_HASH(i++)(((i++)*1)%((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(
Bitvec*))/sizeof(u32)))
;
50791 /* if there wasn't a hash collision, and this doesn't */
50792 /* completely fill the hash, then just add it without */
50793 /* worring about sub-dividing and re-hashing. */
50794 if( !p->u.aHash[h] ){
50795 if (p->nSet<(BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
-1)) {
50796 goto bitvec_set_end;
50797 } else {
50798 goto bitvec_set_rehash;
50799 }
50800 }
50801 /* there was a collision, check to see if it's already */
50802 /* in hash, if not, try to find a spot for it */
50803 do {
50804 if( p->u.aHash[h]==i ) return SQLITE_OK0;
50805 h++;
50806 if( h>=BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
) h = 0;
50807 } while( p->u.aHash[h] );
50808 /* we didn't find it in the hash. h points to the first */
50809 /* available free spot. check to see if this is going to */
50810 /* make our hash too "full". */
50811bitvec_set_rehash:
50812 if( p->nSet>=BITVEC_MXHASH(((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/
sizeof(u32))/2)
){
50813 unsigned int j;
50814 int rc;
50815 u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash))sqlite3DbMallocRaw(0,sizeof(p->u.aHash));
50816 if( aiValues==0 ){
50817 return SQLITE_NOMEM_BKPT7;
50818 }else{
50819 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
50820 memset(p->u.apSub, 0, sizeof(p->u.apSub));
50821 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 *))
;
50822 rc = sqlite3BitvecSet(p, i);
50823 for(j=0; j<BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
; j++){
50824 if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
50825 }
50826 sqlite3StackFree(0, aiValues)sqlite3DbFree(0,aiValues);
50827 return rc;
50828 }
50829 }
50830bitvec_set_end:
50831 p->nSet++;
50832 p->u.aHash[h] = i;
50833 return SQLITE_OK0;
50834}
50835
50836/*
50837** Clear the i-th bit.
50838**
50839** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
50840** that BitvecClear can use to rebuilt its hash table.
50841*/
50842SQLITE_PRIVATEstatic void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
50843 if( p==0 ) return;
50844 assert( i>0 )((void) (0));
50845 i--;
50846 while( p->iDivisor ){
50847 u32 bin = i/p->iDivisor;
50848 i = i%p->iDivisor;
50849 p = p->u.apSub[bin];
50850 if (!p) {
50851 return;
50852 }
50853 }
50854 if( p->iSize<=BITVEC_NBIT(((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/
sizeof(u8))*8)
){
50855 p->u.aBitmap[i/BITVEC_SZELEM8] &= ~(1 << (i&(BITVEC_SZELEM8-1)));
50856 }else{
50857 unsigned int j;
50858 u32 *aiValues = pBuf;
50859 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
50860 memset(p->u.aHash, 0, sizeof(p->u.aHash));
50861 p->nSet = 0;
50862 for(j=0; j<BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
; j++){
50863 if( aiValues[j] && aiValues[j]!=(i+1) ){
50864 u32 h = BITVEC_HASH(aiValues[j]-1)(((aiValues[j]-1)*1)%((((512 -(3*sizeof(u32)))/sizeof(Bitvec*
))*sizeof(Bitvec*))/sizeof(u32)))
;
50865 p->nSet++;
50866 while( p->u.aHash[h] ){
50867 h++;
50868 if( h>=BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
) h = 0;
50869 }
50870 p->u.aHash[h] = aiValues[j];
50871 }
50872 }
50873 }
50874}
50875
50876/*
50877** Destroy a bitmap object. Reclaim all memory used.
50878*/
50879SQLITE_PRIVATEstatic void sqlite3BitvecDestroy(Bitvec *p){
50880 if( p==0 ) return;
50881 if( p->iDivisor ){
50882 unsigned int i;
50883 for(i=0; i<BITVEC_NPTR((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(Bitvec *))
; i++){
50884 sqlite3BitvecDestroy(p->u.apSub[i]);
50885 }
50886 }
50887 sqlite3_free(p);
50888}
50889
50890/*
50891** Return the value of the iSize parameter specified when Bitvec *p
50892** was created.
50893*/
50894SQLITE_PRIVATEstatic u32 sqlite3BitvecSize(Bitvec *p){
50895 return p->iSize;
50896}
50897
50898#ifndef SQLITE_UNTESTABLE
50899/*
50900** Let V[] be an array of unsigned characters sufficient to hold
50901** up to N bits. Let I be an integer between 0 and N. 0<=I<N.
50902** Then the following macros can be used to set, clear, or test
50903** individual bits within V.
50904*/
50905#define SETBIT(V,I)V[I>>3] |= (1<<(I&7)) V[I>>3] |= (1<<(I&7))
50906#define CLEARBIT(V,I)V[I>>3] &= ~(1<<(I&7)) V[I>>3] &= ~(1<<(I&7))
50907#define TESTBIT(V,I)(V[I>>3]&(1<<(I&7)))!=0 (V[I>>3]&(1<<(I&7)))!=0
50908
50909/*
50910** This routine runs an extensive test of the Bitvec code.
50911**
50912** The input is an array of integers that acts as a program
50913** to test the Bitvec. The integers are opcodes followed
50914** by 0, 1, or 3 operands, depending on the opcode. Another
50915** opcode follows immediately after the last operand.
50916**
50917** There are 6 opcodes numbered from 0 through 5. 0 is the
50918** "halt" opcode and causes the test to end.
50919**
50920** 0 Halt and return the number of errors
50921** 1 N S X Set N bits beginning with S and incrementing by X
50922** 2 N S X Clear N bits beginning with S and incrementing by X
50923** 3 N Set N randomly chosen bits
50924** 4 N Clear N randomly chosen bits
50925** 5 N S X Set N bits from S increment X in array only, not in bitvec
50926**
50927** The opcodes 1 through 4 perform set and clear operations are performed
50928** on both a Bitvec object and on a linear array of bits obtained from malloc.
50929** Opcode 5 works on the linear array only, not on the Bitvec.
50930** Opcode 5 is used to deliberately induce a fault in order to
50931** confirm that error detection works.
50932**
50933** At the conclusion of the test the linear array is compared
50934** against the Bitvec object. If there are any differences,
50935** an error is returned. If they are the same, zero is returned.
50936**
50937** If a memory allocation error occurs, return -1.
50938*/
50939SQLITE_PRIVATEstatic int sqlite3BitvecBuiltinTest(int sz, int *aOp){
50940 Bitvec *pBitvec = 0;
50941 unsigned char *pV = 0;
50942 int rc = -1;
50943 int i, nx, pc, op;
50944 void *pTmpSpace;
50945
50946 /* Allocate the Bitvec to be tested and a linear array of
50947 ** bits to act as the reference */
50948 pBitvec = sqlite3BitvecCreate( sz );
50949 pV = sqlite3MallocZero( (sz+7)/8 + 1 );
50950 pTmpSpace = sqlite3_malloc64(BITVEC_SZ512);
50951 if( pBitvec==0 || pV==0 || pTmpSpace==0 ) goto bitvec_end;
50952
50953 /* NULL pBitvec tests */
50954 sqlite3BitvecSet(0, 1);
50955 sqlite3BitvecClear(0, 1, pTmpSpace);
50956
50957 /* Run the program */
50958 pc = i = 0;
50959 while( (op = aOp[pc])!=0 ){
50960 switch( op ){
50961 case 1:
50962 case 2:
50963 case 5: {
50964 nx = 4;
50965 i = aOp[pc+2] - 1;
50966 aOp[pc+2] += aOp[pc+3];
50967 break;
50968 }
50969 case 3:
50970 case 4:
50971 default: {
50972 nx = 2;
50973 sqlite3_randomness(sizeof(i), &i);
50974 break;
50975 }
50976 }
50977 if( (--aOp[pc+1]) > 0 ) nx = 0;
50978 pc += nx;
50979 i = (i & 0x7fffffff)%sz;
50980 if( (op & 1)!=0 ){
50981 SETBIT(pV, (i+1))pV[(i+1)>>3] |= (1<<((i+1)&7));
50982 if( op!=5 ){
50983 if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
50984 }
50985 }else{
50986 CLEARBIT(pV, (i+1))pV[(i+1)>>3] &= ~(1<<((i+1)&7));
50987 sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
50988 }
50989 }
50990
50991 /* Test to make sure the linear array exactly matches the
50992 ** Bitvec object. Start with the assumption that they do
50993 ** match (rc==0). Change rc to non-zero if a discrepancy
50994 ** is found.
50995 */
50996 rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
50997 + sqlite3BitvecTest(pBitvec, 0)
50998 + (sqlite3BitvecSize(pBitvec) - sz);
50999 for(i=1; i<=sz; i++){
51000 if( (TESTBIT(pV,i)(pV[i>>3]&(1<<(i&7)))!=0)!=sqlite3BitvecTest(pBitvec,i) ){
51001 rc = i;
51002 break;
51003 }
51004 }
51005
51006 /* Free allocated structure */
51007bitvec_end:
51008 sqlite3_free(pTmpSpace);
51009 sqlite3_free(pV);
51010 sqlite3BitvecDestroy(pBitvec);
51011 return rc;
51012}
51013#endif /* SQLITE_UNTESTABLE */
51014
51015/************** End of bitvec.c **********************************************/
51016/************** Begin file pcache.c ******************************************/
51017/*
51018** 2008 August 05
51019**
51020** The author disclaims copyright to this source code. In place of
51021** a legal notice, here is a blessing:
51022**
51023** May you do good and not evil.
51024** May you find forgiveness for yourself and forgive others.
51025** May you share freely, never taking more than you give.
51026**
51027*************************************************************************
51028** This file implements that page cache.
51029*/
51030/* #include "sqliteInt.h" */
51031
51032/*
51033** A complete page cache is an instance of this structure. Every
51034** entry in the cache holds a single page of the database file. The
51035** btree layer only operates on the cached copy of the database pages.
51036**
51037** A page cache entry is "clean" if it exactly matches what is currently
51038** on disk. A page is "dirty" if it has been modified and needs to be
51039** persisted to disk.
51040**
51041** pDirty, pDirtyTail, pSynced:
51042** All dirty pages are linked into the doubly linked list using
51043** PgHdr.pDirtyNext and pDirtyPrev. The list is maintained in LRU order
51044** such that p was added to the list more recently than p->pDirtyNext.
51045** PCache.pDirty points to the first (newest) element in the list and
51046** pDirtyTail to the last (oldest).
51047**
51048** The PCache.pSynced variable is used to optimize searching for a dirty
51049** page to eject from the cache mid-transaction. It is better to eject
51050** a page that does not require a journal sync than one that does.
51051** Therefore, pSynced is maintained so that it *almost* always points
51052** to either the oldest page in the pDirty/pDirtyTail list that has a
51053** clear PGHDR_NEED_SYNC flag or to a page that is older than this one
51054** (so that the right page to eject can be found by following pDirtyPrev
51055** pointers).
51056*/
51057struct PCache {
51058 PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
51059 PgHdr *pSynced; /* Last synced page in dirty page list */
51060 int nRefSum; /* Sum of ref counts over all pages */
51061 int szCache; /* Configured cache size */
51062 int szSpill; /* Size before spilling occurs */
51063 int szPage; /* Size of every page in this cache */
51064 int szExtra; /* Size of extra space for each page */
51065 u8 bPurgeable; /* True if pages are on backing store */
51066 u8 eCreate; /* eCreate value for for xFetch() */
51067 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
51068 void *pStress; /* Argument to xStress */
51069 sqlite3_pcache *pCache; /* Pluggable cache module */
51070};
51071
51072/********************************** Test and Debug Logic **********************/
51073/*
51074** Debug tracing macros. Enable by by changing the "0" to "1" and
51075** recompiling.
51076**
51077** When sqlite3PcacheTrace is 1, single line trace messages are issued.
51078** When sqlite3PcacheTrace is 2, a dump of the pcache showing all cache entries
51079** is displayed for many operations, resulting in a lot of output.
51080*/
51081#if defined(SQLITE_DEBUG) && 0
51082 int sqlite3PcacheTrace = 2; /* 0: off 1: simple 2: cache dumps */
51083 int sqlite3PcacheMxDump = 9999; /* Max cache entries for pcacheDump() */
51084# define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;}
51085 void pcacheDump(PCache *pCache){
51086 int N;
51087 int i, j;
51088 sqlite3_pcache_page *pLower;
51089 PgHdr *pPg;
51090 unsigned char *a;
51091
51092 if( sqlite3PcacheTrace<2 ) return;
51093 if( pCache->pCache==0 ) return;
51094 N = sqlite3PcachePagecount(pCache);
51095 if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump;
51096 for(i=1; i<=N; i++){
51097 pLower = sqlite3GlobalConfigsqlite3Config.pcache2.xFetch(pCache->pCache, i, 0);
51098 if( pLower==0 ) continue;
51099 pPg = (PgHdr*)pLower->pExtra;
51100 printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
51101 a = (unsigned char *)pLower->pBuf;
51102 for(j=0; j<12; j++) printf("%02x", a[j]);
51103 printf("\n");
51104 if( pPg->pPage==0 ){
51105 sqlite3GlobalConfigsqlite3Config.pcache2.xUnpin(pCache->pCache, pLower, 0);
51106 }
51107 }
51108 }
51109 #else
51110# define pcacheTrace(X)
51111# define pcacheDump(X)
51112#endif
51113
51114/*
51115** Check invariants on a PgHdr entry. Return true if everything is OK.
51116** Return false if any invariant is violated.
51117**
51118** This routine is for use inside of assert() statements only. For
51119** example:
51120**
51121** assert( sqlite3PcachePageSanity(pPg) );
51122*/
51123#ifdef SQLITE_DEBUG
51124SQLITE_PRIVATEstatic int sqlite3PcachePageSanity(PgHdr *pPg){
51125 PCache *pCache;
51126 assert( pPg!=0 )((void) (0));
51127 assert( pPg->pgno>0 || pPg->pPager==0 )((void) (0)); /* Page number is 1 or more */
51128 pCache = pPg->pCache;
51129 assert( pCache!=0 )((void) (0)); /* Every page has an associated PCache */
51130 if( pPg->flags & PGHDR_CLEAN0x001 ){
51131 assert( (pPg->flags & PGHDR_DIRTY)==0 )((void) (0));/* Cannot be both CLEAN and DIRTY */
51132 assert( pCache->pDirty!=pPg )((void) (0)); /* CLEAN pages not on dirty list */
51133 assert( pCache->pDirtyTail!=pPg )((void) (0));
51134 }
51135 /* WRITEABLE pages must also be DIRTY */
51136 if( pPg->flags & PGHDR_WRITEABLE0x004 ){
51137 assert( pPg->flags & PGHDR_DIRTY )((void) (0)); /* WRITEABLE implies DIRTY */
51138 }
51139 /* NEED_SYNC can be set independently of WRITEABLE. This can happen,
51140 ** for example, when using the sqlite3PagerDontWrite() optimization:
51141 ** (1) Page X is journalled, and gets WRITEABLE and NEED_SEEK.
51142 ** (2) Page X moved to freelist, WRITEABLE is cleared
51143 ** (3) Page X reused, WRITEABLE is set again
51144 ** If NEED_SYNC had been cleared in step 2, then it would not be reset
51145 ** in step 3, and page might be written into the database without first
51146 ** syncing the rollback journal, which might cause corruption on a power
51147 ** loss.
51148 **
51149 ** Another example is when the database page size is smaller than the
51150 ** disk sector size. When any page of a sector is journalled, all pages
51151 ** in that sector are marked NEED_SYNC even if they are still CLEAN, just
51152 ** in case they are later modified, since all pages in the same sector
51153 ** must be journalled and synced before any of those pages can be safely
51154 ** written.
51155 */
51156 return 1;
51157}
51158#endif /* SQLITE_DEBUG */
51159
51160
51161/********************************** Linked List Management ********************/
51162
51163/* Allowed values for second argument to pcacheManageDirtyList() */
51164#define PCACHE_DIRTYLIST_REMOVE1 1 /* Remove pPage from dirty list */
51165#define PCACHE_DIRTYLIST_ADD2 2 /* Add pPage to the dirty list */
51166#define PCACHE_DIRTYLIST_FRONT3 3 /* Move pPage to the front of the list */
51167
51168/*
51169** Manage pPage's participation on the dirty list. Bits of the addRemove
51170** argument determines what operation to do. The 0x01 bit means first
51171** remove pPage from the dirty list. The 0x02 means add pPage back to
51172** the dirty list. Doing both moves pPage to the front of the dirty list.
51173*/
51174static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
51175 PCache *p = pPage->pCache;
51176
51177 pcacheTrace(("%p.DIRTYLIST.%s %d\n", p,
51178 addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT",
51179 pPage->pgno));
51180 if( addRemove & PCACHE_DIRTYLIST_REMOVE1 ){
51181 assert( pPage->pDirtyNext || pPage==p->pDirtyTail )((void) (0));
51182 assert( pPage->pDirtyPrev || pPage==p->pDirty )((void) (0));
51183
51184 /* Update the PCache1.pSynced variable if necessary. */
51185 if( p->pSynced==pPage ){
51186 p->pSynced = pPage->pDirtyPrev;
51187 }
51188
51189 if( pPage->pDirtyNext ){
51190 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
51191 }else{
51192 assert( pPage==p->pDirtyTail )((void) (0));
51193 p->pDirtyTail = pPage->pDirtyPrev;
51194 }
51195 if( pPage->pDirtyPrev ){
51196 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
51197 }else{
51198 /* If there are now no dirty pages in the cache, set eCreate to 2.
51199 ** This is an optimization that allows sqlite3PcacheFetch() to skip
51200 ** searching for a dirty page to eject from the cache when it might
51201 ** otherwise have to. */
51202 assert( pPage==p->pDirty )((void) (0));
51203 p->pDirty = pPage->pDirtyNext;
51204 assert( p->bPurgeable || p->eCreate==2 )((void) (0));
51205 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
51206 assert( p->bPurgeable==0 || p->eCreate==1 )((void) (0));
51207 p->eCreate = 2;
51208 }
51209 }
51210 }
51211 if( addRemove & PCACHE_DIRTYLIST_ADD2 ){
51212 pPage->pDirtyPrev = 0;
51213 pPage->pDirtyNext = p->pDirty;
51214 if( pPage->pDirtyNext ){
51215 assert( pPage->pDirtyNext->pDirtyPrev==0 )((void) (0));
51216 pPage->pDirtyNext->pDirtyPrev = pPage;
51217 }else{
51218 p->pDirtyTail = pPage;
51219 if( p->bPurgeable ){
51220 assert( p->eCreate==2 )((void) (0));
51221 p->eCreate = 1;
51222 }
51223 }
51224 p->pDirty = pPage;
51225
51226 /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set
51227 ** pSynced to point to it. Checking the NEED_SYNC flag is an
51228 ** optimization, as if pSynced points to a page with the NEED_SYNC
51229 ** flag set sqlite3PcacheFetchStress() searches through all newer
51230 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
51231 if( !p->pSynced
51232 && 0==(pPage->flags&PGHDR_NEED_SYNC0x008) /*OPTIMIZATION-IF-FALSE*/
51233 ){
51234 p->pSynced = pPage;
51235 }
51236 }
51237 pcacheDump(p);
51238}
51239
51240/*
51241** Wrapper around the pluggable caches xUnpin method. If the cache is
51242** being used for an in-memory database, this function is a no-op.
51243*/
51244static void pcacheUnpin(PgHdr *p){
51245 if( p->pCache->bPurgeable ){
51246 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
51247 sqlite3GlobalConfigsqlite3Config.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
51248 pcacheDump(p->pCache);
51249 }
51250}
51251
51252/*
51253** Compute the number of pages of cache requested. p->szCache is the
51254** cache size requested by the "PRAGMA cache_size" statement.
51255*/
51256static int numberOfCachePages(PCache *p){
51257 if( p->szCache>=0 ){
51258 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
51259 ** suggested cache size is set to N. */
51260 return p->szCache;
51261 }else{
51262 i64 n;
51263 /* IMPLEMANTATION-OF: R-59858-46238 If the argument N is negative, then the
51264 ** number of cache pages is adjusted to be a number of pages that would
51265 ** use approximately abs(N*1024) bytes of memory based on the current
51266 ** page size. */
51267 n = ((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
51268 if( n>1000000000 ) n = 1000000000;
51269 return (int)n;
51270 }
51271}
51272
51273/*************************************************** General Interfaces ******
51274**
51275** Initialize and shutdown the page cache subsystem. Neither of these
51276** functions are threadsafe.
51277*/
51278SQLITE_PRIVATEstatic int sqlite3PcacheInitialize(void){
51279 if( sqlite3GlobalConfigsqlite3Config.pcache2.xInit==0 ){
51280 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
51281 ** built-in default page cache is used instead of the application defined
51282 ** page cache. */
51283 sqlite3PCacheSetDefault();
51284 assert( sqlite3GlobalConfig.pcache2.xInit!=0 )((void) (0));
51285 }
51286 return sqlite3GlobalConfigsqlite3Config.pcache2.xInit(sqlite3GlobalConfigsqlite3Config.pcache2.pArg);
51287}
51288SQLITE_PRIVATEstatic void sqlite3PcacheShutdown(void){
51289 if( sqlite3GlobalConfigsqlite3Config.pcache2.xShutdown ){
51290 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
51291 sqlite3GlobalConfigsqlite3Config.pcache2.xShutdown(sqlite3GlobalConfigsqlite3Config.pcache2.pArg);
51292 }
51293}
51294
51295/*
51296** Return the size in bytes of a PCache object.
51297*/
51298SQLITE_PRIVATEstatic int sqlite3PcacheSize(void){ return sizeof(PCache); }
51299
51300/*
51301** Create a new PCache object. Storage space to hold the object
51302** has already been allocated and is passed in as the p pointer.
51303** The caller discovers how much space needs to be allocated by
51304** calling sqlite3PcacheSize().
51305**
51306** szExtra is some extra space allocated for each page. The first
51307** 8 bytes of the extra space will be zeroed as the page is allocated,
51308** but remaining content will be uninitialized. Though it is opaque
51309** to this module, the extra space really ends up being the MemPage
51310** structure in the pager.
51311*/
51312SQLITE_PRIVATEstatic int sqlite3PcacheOpen(
51313 int szPage, /* Size of every page */
51314 int szExtra, /* Extra space associated with each page */
51315 int bPurgeable, /* True if pages are on backing store */
51316 int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
51317 void *pStress, /* Argument to xStress */
51318 PCache *p /* Preallocated space for the PCache */
51319){
51320 memset(p, 0, sizeof(PCache));
51321 p->szPage = 1;
51322 p->szExtra = szExtra;
51323 assert( szExtra>=8 )((void) (0)); /* First 8 bytes will be zeroed */
51324 p->bPurgeable = bPurgeable;
51325 p->eCreate = 2;
51326 p->xStress = xStress;
51327 p->pStress = pStress;
51328 p->szCache = 100;
51329 p->szSpill = 1;
51330 pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable));
51331 return sqlite3PcacheSetPageSize(p, szPage);
51332}
51333
51334/*
51335** Change the page size for PCache object. The caller must ensure that there
51336** are no outstanding page references when this function is called.
51337*/
51338SQLITE_PRIVATEstatic int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
51339 assert( pCache->nRefSum==0 && pCache->pDirty==0 )((void) (0));
51340 if( pCache->szPage ){
51341 sqlite3_pcache *pNew;
51342 pNew = sqlite3GlobalConfigsqlite3Config.pcache2.xCreate(
51343 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr))(((sizeof(PgHdr))+7)&~7),
51344 pCache->bPurgeable
51345 );
51346 if( pNew==0 ) return SQLITE_NOMEM_BKPT7;
51347 sqlite3GlobalConfigsqlite3Config.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
51348 if( pCache->pCache ){
51349 sqlite3GlobalConfigsqlite3Config.pcache2.xDestroy(pCache->pCache);
51350 }
51351 pCache->pCache = pNew;
51352 pCache->szPage = szPage;
51353 pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage));
51354 }
51355 return SQLITE_OK0;
51356}
51357
51358/*
51359** Try to obtain a page from the cache.
51360**
51361** This routine returns a pointer to an sqlite3_pcache_page object if
51362** such an object is already in cache, or if a new one is created.
51363** This routine returns a NULL pointer if the object was not in cache
51364** and could not be created.
51365**
51366** The createFlags should be 0 to check for existing pages and should
51367** be 3 (not 1, but 3) to try to create a new page.
51368**
51369** If the createFlag is 0, then NULL is always returned if the page
51370** is not already in the cache. If createFlag is 1, then a new page
51371** is created only if that can be done without spilling dirty pages
51372** and without exceeding the cache size limit.
51373**
51374** The caller needs to invoke sqlite3PcacheFetchFinish() to properly
51375** initialize the sqlite3_pcache_page object and convert it into a
51376** PgHdr object. The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish()
51377** routines are split this way for performance reasons. When separated
51378** they can both (usually) operate without having to push values to
51379** the stack on entry and pop them back off on exit, which saves a
51380** lot of pushing and popping.
51381*/
51382SQLITE_PRIVATEstatic sqlite3_pcache_page *sqlite3PcacheFetch(
51383 PCache *pCache, /* Obtain the page from this cache */
51384 Pgno pgno, /* Page number to obtain */
51385 int createFlag /* If true, create page if it does not exist already */
51386){
51387 int eCreate;
51388 sqlite3_pcache_page *pRes;
51389
51390 assert( pCache!=0 )((void) (0));
51391 assert( pCache->pCache!=0 )((void) (0));
51392 assert( createFlag==3 || createFlag==0 )((void) (0));
51393 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) )((void) (0));
51394
51395 /* eCreate defines what to do if the page does not exist.
51396 ** 0 Do not allocate a new page. (createFlag==0)
51397 ** 1 Allocate a new page if doing so is inexpensive.
51398 ** (createFlag==1 AND bPurgeable AND pDirty)
51399 ** 2 Allocate a new page even it doing so is difficult.
51400 ** (createFlag==1 AND !(bPurgeable AND pDirty)
51401 */
51402 eCreate = createFlag & pCache->eCreate;
51403 assert( eCreate==0 || eCreate==1 || eCreate==2 )((void) (0));
51404 assert( createFlag==0 || pCache->eCreate==eCreate )((void) (0));
51405 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) )((void) (0));
51406 pRes = sqlite3GlobalConfigsqlite3Config.pcache2.xFetch(pCache->pCache, pgno, eCreate);
51407 pcacheTrace(("%p.FETCH %d%s (result: %p)\n",pCache,pgno,
51408 createFlag?" create":"",pRes));
51409 return pRes;
51410}
51411
51412/*
51413** If the sqlite3PcacheFetch() routine is unable to allocate a new
51414** page because no clean pages are available for reuse and the cache
51415** size limit has been reached, then this routine can be invoked to
51416** try harder to allocate a page. This routine might invoke the stress
51417** callback to spill dirty pages to the journal. It will then try to
51418** allocate the new page and will only fail to allocate a new page on
51419** an OOM error.
51420**
51421** This routine should be invoked only after sqlite3PcacheFetch() fails.
51422*/
51423SQLITE_PRIVATEstatic int sqlite3PcacheFetchStress(
51424 PCache *pCache, /* Obtain the page from this cache */
51425 Pgno pgno, /* Page number to obtain */
51426 sqlite3_pcache_page **ppPage /* Write result here */
51427){
51428 PgHdr *pPg;
51429 if( pCache->eCreate==2 ) return 0;
51430
51431 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
51432 /* Find a dirty page to write-out and recycle. First try to find a
51433 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
51434 ** cleared), but if that is not possible settle for any other
51435 ** unreferenced dirty page.
51436 **
51437 ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC
51438 ** flag is currently referenced, then the following may leave pSynced
51439 ** set incorrectly (pointing to other than the LRU page with NEED_SYNC
51440 ** cleared). This is Ok, as pSynced is just an optimization. */
51441 for(pPg=pCache->pSynced;
51442 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC0x008));
51443 pPg=pPg->pDirtyPrev
51444 );
51445 pCache->pSynced = pPg;
51446 if( !pPg ){
51447 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
51448 }
51449 if( pPg ){
51450 int rc;
51451#ifdef SQLITE_LOG_CACHE_SPILL
51452 sqlite3_log(SQLITE_FULL13,
51453 "spill page %d making room for %d - cache used: %d/%d",
51454 pPg->pgno, pgno,
51455 sqlite3GlobalConfigsqlite3Config.pcache2.xPagecount(pCache->pCache),
51456 numberOfCachePages(pCache));
51457#endif
51458 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
51459 rc = pCache->xStress(pCache->pStress, pPg);
51460 pcacheDump(pCache);
51461 if( rc!=SQLITE_OK0 && rc!=SQLITE_BUSY5 ){
51462 return rc;
51463 }
51464 }
51465 }
51466 *ppPage = sqlite3GlobalConfigsqlite3Config.pcache2.xFetch(pCache->pCache, pgno, 2);
51467 return *ppPage==0 ? SQLITE_NOMEM_BKPT7 : SQLITE_OK0;
51468}
51469
51470/*
51471** This is a helper routine for sqlite3PcacheFetchFinish()
51472**
51473** In the uncommon case where the page being fetched has not been
51474** initialized, this routine is invoked to do the initialization.
51475** This routine is broken out into a separate function since it
51476** requires extra stack manipulation that can be avoided in the common
51477** case.
51478*/
51479static SQLITE_NOINLINE__attribute__((noinline)) PgHdr *pcacheFetchFinishWithInit(
51480 PCache *pCache, /* Obtain the page from this cache */
51481 Pgno pgno, /* Page number obtained */
51482 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
51483){
51484 PgHdr *pPgHdr;
51485 assert( pPage!=0 )((void) (0));
51486 pPgHdr = (PgHdr*)pPage->pExtra;
51487 assert( pPgHdr->pPage==0 )((void) (0));
51488 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty)__builtin_offsetof(PgHdr, pDirty));
51489 pPgHdr->pPage = pPage;
51490 pPgHdr->pData = pPage->pBuf;
51491 pPgHdr->pExtra = (void *)&pPgHdr[1];
51492 memset(pPgHdr->pExtra, 0, 8);
51493 pPgHdr->pCache = pCache;
51494 pPgHdr->pgno = pgno;
51495 pPgHdr->flags = PGHDR_CLEAN0x001;
51496 return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
51497}
51498
51499/*
51500** This routine converts the sqlite3_pcache_page object returned by
51501** sqlite3PcacheFetch() into an initialized PgHdr object. This routine
51502** must be called after sqlite3PcacheFetch() in order to get a usable
51503** result.
51504*/
51505SQLITE_PRIVATEstatic PgHdr *sqlite3PcacheFetchFinish(
51506 PCache *pCache, /* Obtain the page from this cache */
51507 Pgno pgno, /* Page number obtained */
51508 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
51509){
51510 PgHdr *pPgHdr;
51511
51512 assert( pPage!=0 )((void) (0));
51513 pPgHdr = (PgHdr *)pPage->pExtra;
51514
51515 if( !pPgHdr->pPage ){
51516 return pcacheFetchFinishWithInit(pCache, pgno, pPage);
51517 }
51518 pCache->nRefSum++;
51519 pPgHdr->nRef++;
51520 assert( sqlite3PcachePageSanity(pPgHdr) )((void) (0));
51521 return pPgHdr;
51522}
51523
51524/*
51525** Decrement the reference count on a page. If the page is clean and the
51526** reference count drops to 0, then it is made eligible for recycling.
51527*/
51528SQLITE_PRIVATEstatic void SQLITE_NOINLINE__attribute__((noinline)) sqlite3PcacheRelease(PgHdr *p){
51529 assert( p->nRef>0 )((void) (0));
51530 p->pCache->nRefSum--;
51531 if( (--p->nRef)==0 ){
51532 if( p->flags&PGHDR_CLEAN0x001 ){
51533 pcacheUnpin(p);
51534 }else{
51535 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT3);
51536 }
51537 }
51538}
51539
51540/*
51541** Increase the reference count of a supplied page by 1.
51542*/
51543SQLITE_PRIVATEstatic void sqlite3PcacheRef(PgHdr *p){
51544 assert(p->nRef>0)((void) (0));
51545 assert( sqlite3PcachePageSanity(p) )((void) (0));
51546 p->nRef++;
51547 p->pCache->nRefSum++;
51548}
51549
51550/*
51551** Drop a page from the cache. There must be exactly one reference to the
51552** page. This function deletes that reference, so after it returns the
51553** page pointed to by p is invalid.
51554*/
51555SQLITE_PRIVATEstatic void sqlite3PcacheDrop(PgHdr *p){
51556 assert( p->nRef==1 )((void) (0));
51557 assert( sqlite3PcachePageSanity(p) )((void) (0));
51558 if( p->flags&PGHDR_DIRTY0x002 ){
51559 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE1);
51560 }
51561 p->pCache->nRefSum--;
51562 sqlite3GlobalConfigsqlite3Config.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
51563}
51564
51565/*
51566** Make sure the page is marked as dirty. If it isn't dirty already,
51567** make it so.
51568*/
51569SQLITE_PRIVATEstatic void sqlite3PcacheMakeDirty(PgHdr *p){
51570 assert( p->nRef>0 )((void) (0));
51571 assert( sqlite3PcachePageSanity(p) )((void) (0));
51572 if( p->flags & (PGHDR_CLEAN0x001|PGHDR_DONT_WRITE0x010) ){ /*OPTIMIZATION-IF-FALSE*/
51573 p->flags &= ~PGHDR_DONT_WRITE0x010;
51574 if( p->flags & PGHDR_CLEAN0x001 ){
51575 p->flags ^= (PGHDR_DIRTY0x002|PGHDR_CLEAN0x001);
51576 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
51577 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY )((void) (0));
51578 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD2);
51579 }
51580 assert( sqlite3PcachePageSanity(p) )((void) (0));
51581 }
51582}
51583
51584/*
51585** Make sure the page is marked as clean. If it isn't clean already,
51586** make it so.
51587*/
51588SQLITE_PRIVATEstatic void sqlite3PcacheMakeClean(PgHdr *p){
51589 assert( sqlite3PcachePageSanity(p) )((void) (0));
51590 assert( (p->flags & PGHDR_DIRTY)!=0 )((void) (0));
51591 assert( (p->flags & PGHDR_CLEAN)==0 )((void) (0));
51592 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE1);
51593 p->flags &= ~(PGHDR_DIRTY0x002|PGHDR_NEED_SYNC0x008|PGHDR_WRITEABLE0x004);
51594 p->flags |= PGHDR_CLEAN0x001;
51595 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
51596 assert( sqlite3PcachePageSanity(p) )((void) (0));
51597 if( p->nRef==0 ){
51598 pcacheUnpin(p);
51599 }
51600}
51601
51602/*
51603** Make every page in the cache clean.
51604*/
51605SQLITE_PRIVATEstatic void sqlite3PcacheCleanAll(PCache *pCache){
51606 PgHdr *p;
51607 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
51608 while( (p = pCache->pDirty)!=0 ){
51609 sqlite3PcacheMakeClean(p);
51610 }
51611}
51612
51613/*
51614** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages.
51615*/
51616SQLITE_PRIVATEstatic void sqlite3PcacheClearWritable(PCache *pCache){
51617 PgHdr *p;
51618 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
51619 for(p=pCache->pDirty; p; p=p->pDirtyNext){
51620 p->flags &= ~(PGHDR_NEED_SYNC0x008|PGHDR_WRITEABLE0x004);
51621 }
51622 pCache->pSynced = pCache->pDirtyTail;
51623}
51624
51625/*
51626** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
51627*/
51628SQLITE_PRIVATEstatic void sqlite3PcacheClearSyncFlags(PCache *pCache){
51629 PgHdr *p;
51630 for(p=pCache->pDirty; p; p=p->pDirtyNext){
51631 p->flags &= ~PGHDR_NEED_SYNC0x008;
51632 }
51633 pCache->pSynced = pCache->pDirtyTail;
51634}
51635
51636/*
51637** Change the page number of page p to newPgno.
51638*/
51639SQLITE_PRIVATEstatic void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
51640 PCache *pCache = p->pCache;
51641 assert( p->nRef>0 )((void) (0));
51642 assert( newPgno>0 )((void) (0));
51643 assert( sqlite3PcachePageSanity(p) )((void) (0));
51644 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
51645 sqlite3GlobalConfigsqlite3Config.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
51646 p->pgno = newPgno;
51647 if( (p->flags&PGHDR_DIRTY0x002) && (p->flags&PGHDR_NEED_SYNC0x008) ){
51648 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT3);
51649 }
51650}
51651
51652/*
51653** Drop every cache entry whose page number is greater than "pgno". The
51654** caller must ensure that there are no outstanding references to any pages
51655** other than page 1 with a page number greater than pgno.
51656**
51657** If there is a reference to page 1 and the pgno parameter passed to this
51658** function is 0, then the data area associated with page 1 is zeroed, but
51659** the page object is not dropped.
51660*/
51661SQLITE_PRIVATEstatic void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
51662 if( pCache->pCache ){
51663 PgHdr *p;
51664 PgHdr *pNext;
51665 pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno));
51666 for(p=pCache->pDirty; p; p=pNext){
51667 pNext = p->pDirtyNext;
51668 /* This routine never gets call with a positive pgno except right
51669 ** after sqlite3PcacheCleanAll(). So if there are dirty pages,
51670 ** it must be that pgno==0.
51671 */
51672 assert( p->pgno>0 )((void) (0));
51673 if( p->pgno>pgno ){
51674 assert( p->flags&PGHDR_DIRTY )((void) (0));
51675 sqlite3PcacheMakeClean(p);
51676 }
51677 }
51678 if( pgno==0 && pCache->nRefSum ){
51679 sqlite3_pcache_page *pPage1;
51680 pPage1 = sqlite3GlobalConfigsqlite3Config.pcache2.xFetch(pCache->pCache,1,0);
51681 if( ALWAYS(pPage1)(pPage1) ){ /* Page 1 is always available in cache, because
51682 ** pCache->nRefSum>0 */
51683 memset(pPage1->pBuf, 0, pCache->szPage);
51684 pgno = 1;
51685 }
51686 }
51687 sqlite3GlobalConfigsqlite3Config.pcache2.xTruncate(pCache->pCache, pgno+1);
51688 }
51689}
51690
51691/*
51692** Close a cache.
51693*/
51694SQLITE_PRIVATEstatic void sqlite3PcacheClose(PCache *pCache){
51695 assert( pCache->pCache!=0 )((void) (0));
51696 pcacheTrace(("%p.CLOSE\n",pCache));
51697 sqlite3GlobalConfigsqlite3Config.pcache2.xDestroy(pCache->pCache);
51698}
51699
51700/*
51701** Discard the contents of the cache.
51702*/
51703SQLITE_PRIVATEstatic void sqlite3PcacheClear(PCache *pCache){
51704 sqlite3PcacheTruncate(pCache, 0);
51705}
51706
51707/*
51708** Merge two lists of pages connected by pDirty and in pgno order.
51709** Do not bother fixing the pDirtyPrev pointers.
51710*/
51711static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
51712 PgHdr result, *pTail;
51713 pTail = &result;
51714 assert( pA!=0 && pB!=0 )((void) (0));
51715 for(;;){
51716 if( pA->pgno<pB->pgno ){
51717 pTail->pDirty = pA;
51718 pTail = pA;
51719 pA = pA->pDirty;
51720 if( pA==0 ){
51721 pTail->pDirty = pB;
51722 break;
51723 }
51724 }else{
51725 pTail->pDirty = pB;
51726 pTail = pB;
51727 pB = pB->pDirty;
51728 if( pB==0 ){
51729 pTail->pDirty = pA;
51730 break;
51731 }
51732 }
51733 }
51734 return result.pDirty;
51735}
51736
51737/*
51738** Sort the list of pages in accending order by pgno. Pages are
51739** connected by pDirty pointers. The pDirtyPrev pointers are
51740** corrupted by this sort.
51741**
51742** Since there cannot be more than 2^31 distinct pages in a database,
51743** there cannot be more than 31 buckets required by the merge sorter.
51744** One extra bucket is added to catch overflow in case something
51745** ever changes to make the previous sentence incorrect.
51746*/
51747#define N_SORT_BUCKET32 32
51748static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
51749 PgHdr *a[N_SORT_BUCKET32], *p;
51750 int i;
51751 memset(a, 0, sizeof(a));
51752 while( pIn ){
51753 p = pIn;
51754 pIn = p->pDirty;
51755 p->pDirty = 0;
51756 for(i=0; ALWAYS(i<N_SORT_BUCKET-1)(i<32 -1); i++){
51757 if( a[i]==0 ){
51758 a[i] = p;
51759 break;
51760 }else{
51761 p = pcacheMergeDirtyList(a[i], p);
51762 a[i] = 0;
51763 }
51764 }
51765 if( NEVER(i==N_SORT_BUCKET-1)(i==32 -1) ){
51766 /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
51767 ** the input list. But that is impossible.
51768 */
51769 a[i] = pcacheMergeDirtyList(a[i], p);
51770 }
51771 }
51772 p = a[0];
51773 for(i=1; i<N_SORT_BUCKET32; i++){
51774 if( a[i]==0 ) continue;
51775 p = p ? pcacheMergeDirtyList(p, a[i]) : a[i];
51776 }
51777 return p;
51778}
51779
51780/*
51781** Return a list of all dirty pages in the cache, sorted by page number.
51782*/
51783SQLITE_PRIVATEstatic PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
51784 PgHdr *p;
51785 for(p=pCache->pDirty; p; p=p->pDirtyNext){
51786 p->pDirty = p->pDirtyNext;
51787 }
51788 return pcacheSortDirtyList(pCache->pDirty);
51789}
51790
51791/*
51792** Return the total number of references to all pages held by the cache.
51793**
51794** This is not the total number of pages referenced, but the sum of the
51795** reference count for all pages.
51796*/
51797SQLITE_PRIVATEstatic int sqlite3PcacheRefCount(PCache *pCache){
51798 return pCache->nRefSum;
51799}
51800
51801/*
51802** Return the number of references to the page supplied as an argument.
51803*/
51804SQLITE_PRIVATEstatic int sqlite3PcachePageRefcount(PgHdr *p){
51805 return p->nRef;
51806}
51807
51808/*
51809** Return the total number of pages in the cache.
51810*/
51811SQLITE_PRIVATEstatic int sqlite3PcachePagecount(PCache *pCache){
51812 assert( pCache->pCache!=0 )((void) (0));
51813 return sqlite3GlobalConfigsqlite3Config.pcache2.xPagecount(pCache->pCache);
51814}
51815
51816#ifdef SQLITE_TEST
51817/*
51818** Get the suggested cache-size value.
51819*/
51820SQLITE_PRIVATEstatic int sqlite3PcacheGetCachesize(PCache *pCache){
51821 return numberOfCachePages(pCache);
51822}
51823#endif
51824
51825/*
51826** Set the suggested cache-size value.
51827*/
51828SQLITE_PRIVATEstatic void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
51829 assert( pCache->pCache!=0 )((void) (0));
51830 pCache->szCache = mxPage;
51831 sqlite3GlobalConfigsqlite3Config.pcache2.xCachesize(pCache->pCache,
51832 numberOfCachePages(pCache));
51833}
51834
51835/*
51836** Set the suggested cache-spill value. Make no changes if if the
51837** argument is zero. Return the effective cache-spill size, which will
51838** be the larger of the szSpill and szCache.
51839*/
51840SQLITE_PRIVATEstatic int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){
51841 int res;
51842 assert( p->pCache!=0 )((void) (0));
51843 if( mxPage ){
51844 if( mxPage<0 ){
51845 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
51846 }
51847 p->szSpill = mxPage;
51848 }
51849 res = numberOfCachePages(p);
51850 if( res<p->szSpill ) res = p->szSpill;
51851 return res;
51852}
51853
51854/*
51855** Free up as much memory as possible from the page cache.
51856*/
51857SQLITE_PRIVATEstatic void sqlite3PcacheShrink(PCache *pCache){
51858 assert( pCache->pCache!=0 )((void) (0));
51859 sqlite3GlobalConfigsqlite3Config.pcache2.xShrink(pCache->pCache);
51860}
51861
51862/*
51863** Return the size of the header added by this middleware layer
51864** in the page-cache hierarchy.
51865*/
51866SQLITE_PRIVATEstatic int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr))(((sizeof(PgHdr))+7)&~7); }
51867
51868/*
51869** Return the number of dirty pages currently in the cache, as a percentage
51870** of the configured cache size.
51871*/
51872SQLITE_PRIVATEstatic int sqlite3PCachePercentDirty(PCache *pCache){
51873 PgHdr *pDirty;
51874 int nDirty = 0;
51875 int nCache = numberOfCachePages(pCache);
51876 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
51877 return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;
51878}
51879
51880#ifdef SQLITE_DIRECT_OVERFLOW_READ
51881/*
51882** Return true if there are one or more dirty pages in the cache. Else false.
51883*/
51884SQLITE_PRIVATEstatic int sqlite3PCacheIsDirty(PCache *pCache){
51885 return (pCache->pDirty!=0);
51886}
51887#endif
51888
51889#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
51890/*
51891** For all dirty pages currently in the cache, invoke the specified
51892** callback. This is only used if the SQLITE_CHECK_PAGES macro is
51893** defined.
51894*/
51895SQLITE_PRIVATEstatic void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
51896 PgHdr *pDirty;
51897 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
51898 xIter(pDirty);
51899 }
51900}
51901#endif
51902
51903/************** End of pcache.c **********************************************/
51904/************** Begin file pcache1.c *****************************************/
51905/*
51906** 2008 November 05
51907**
51908** The author disclaims copyright to this source code. In place of
51909** a legal notice, here is a blessing:
51910**
51911** May you do good and not evil.
51912** May you find forgiveness for yourself and forgive others.
51913** May you share freely, never taking more than you give.
51914**
51915*************************************************************************
51916**
51917** This file implements the default page cache implementation (the
51918** sqlite3_pcache interface). It also contains part of the implementation
51919** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
51920** If the default page cache implementation is overridden, then neither of
51921** these two features are available.
51922**
51923** A Page cache line looks like this:
51924**
51925** -------------------------------------------------------------
51926** | database page content | PgHdr1 | MemPage | PgHdr |
51927** -------------------------------------------------------------
51928**
51929** The database page content is up front (so that buffer overreads tend to
51930** flow harmlessly into the PgHdr1, MemPage, and PgHdr extensions). MemPage
51931** is the extension added by the btree.c module containing information such
51932** as the database page number and how that database page is used. PgHdr
51933** is added by the pcache.c layer and contains information used to keep track
51934** of which pages are "dirty". PgHdr1 is an extension added by this
51935** module (pcache1.c). The PgHdr1 header is a subclass of sqlite3_pcache_page.
51936** PgHdr1 contains information needed to look up a page by its page number.
51937** The superclass sqlite3_pcache_page.pBuf points to the start of the
51938** database page content and sqlite3_pcache_page.pExtra points to PgHdr.
51939**
51940** The size of the extension (MemPage+PgHdr+PgHdr1) can be determined at
51941** runtime using sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &size). The
51942** sizes of the extensions sum to 272 bytes on x64 for 3.8.10, but this
51943** size can vary according to architecture, compile-time options, and
51944** SQLite library version number.
51945**
51946** If SQLITE_PCACHE_SEPARATE_HEADER is defined, then the extension is obtained
51947** using a separate memory allocation from the database page content. This
51948** seeks to overcome the "clownshoe" problem (also called "internal
51949** fragmentation" in academic literature) of allocating a few bytes more
51950** than a power of two with the memory allocator rounding up to the next
51951** power of two, and leaving the rounded-up space unused.
51952**
51953** This module tracks pointers to PgHdr1 objects. Only pcache.c communicates
51954** with this module. Information is passed back and forth as PgHdr1 pointers.
51955**
51956** The pcache.c and pager.c modules deal pointers to PgHdr objects.
51957** The btree.c module deals with pointers to MemPage objects.
51958**
51959** SOURCE OF PAGE CACHE MEMORY:
51960**
51961** Memory for a page might come from any of three sources:
51962**
51963** (1) The general-purpose memory allocator - sqlite3Malloc()
51964** (2) Global page-cache memory provided using sqlite3_config() with
51965** SQLITE_CONFIG_PAGECACHE.
51966** (3) PCache-local bulk allocation.
51967**
51968** The third case is a chunk of heap memory (defaulting to 100 pages worth)
51969** that is allocated when the page cache is created. The size of the local
51970** bulk allocation can be adjusted using
51971**
51972** sqlite3_config(SQLITE_CONFIG_PAGECACHE, (void*)0, 0, N).
51973**
51974** If N is positive, then N pages worth of memory are allocated using a single
51975** sqlite3Malloc() call and that memory is used for the first N pages allocated.
51976** Or if N is negative, then -1024*N bytes of memory are allocated and used
51977** for as many pages as can be accomodated.
51978**
51979** Only one of (2) or (3) can be used. Once the memory available to (2) or
51980** (3) is exhausted, subsequent allocations fail over to the general-purpose
51981** memory allocator (1).
51982**
51983** Earlier versions of SQLite used only methods (1) and (2). But experiments
51984** show that method (3) with N==100 provides about a 5% performance boost for
51985** common workloads.
51986*/
51987/* #include "sqliteInt.h" */
51988
51989typedef struct PCache1 PCache1;
51990typedef struct PgHdr1 PgHdr1;
51991typedef struct PgFreeslot PgFreeslot;
51992typedef struct PGroup PGroup;
51993
51994/*
51995** Each cache entry is represented by an instance of the following
51996** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
51997** PgHdr1.pCache->szPage bytes is allocated directly before this structure
51998** in memory.
51999**
52000** Note: Variables isBulkLocal and isAnchor were once type "u8". That works,
52001** but causes a 2-byte gap in the structure for most architectures (since
52002** pointers must be either 4 or 8-byte aligned). As this structure is located
52003** in memory directly after the associated page data, if the database is
52004** corrupt, code at the b-tree layer may overread the page buffer and
52005** read part of this structure before the corruption is detected. This
52006** can cause a valgrind error if the unitialized gap is accessed. Using u16
52007** ensures there is no such gap, and therefore no bytes of unitialized memory
52008** in the structure.
52009*/
52010struct PgHdr1 {
52011 sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */
52012 unsigned int iKey; /* Key value (page number) */
52013 u16 isBulkLocal; /* This page from bulk local storage */
52014 u16 isAnchor; /* This is the PGroup.lru element */
52015 PgHdr1 *pNext; /* Next in hash table chain */
52016 PCache1 *pCache; /* Cache that currently owns this page */
52017 PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */
52018 PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
52019 /* NB: pLruPrev is only valid if pLruNext!=0 */
52020};
52021
52022/*
52023** A page is pinned if it is not on the LRU list. To be "pinned" means
52024** that the page is in active use and must not be deallocated.
52025*/
52026#define PAGE_IS_PINNED(p)((p)->pLruNext==0) ((p)->pLruNext==0)
52027#define PAGE_IS_UNPINNED(p)((p)->pLruNext!=0) ((p)->pLruNext!=0)
52028
52029/* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
52030** of one or more PCaches that are able to recycle each other's unpinned
52031** pages when they are under memory pressure. A PGroup is an instance of
52032** the following object.
52033**
52034** This page cache implementation works in one of two modes:
52035**
52036** (1) Every PCache is the sole member of its own PGroup. There is
52037** one PGroup per PCache.
52038**
52039** (2) There is a single global PGroup that all PCaches are a member
52040** of.
52041**
52042** Mode 1 uses more memory (since PCache instances are not able to rob
52043** unused pages from other PCaches) but it also operates without a mutex,
52044** and is therefore often faster. Mode 2 requires a mutex in order to be
52045** threadsafe, but recycles pages more efficiently.
52046**
52047** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single
52048** PGroup which is the pcache1.grp global variable and its mutex is
52049** SQLITE_MUTEX_STATIC_LRU.
52050*/
52051struct PGroup {
52052 sqlite3_mutex *mutex; /* MUTEX_STATIC_LRU or NULL */
52053 unsigned int nMaxPage; /* Sum of nMax for purgeable caches */
52054 unsigned int nMinPage; /* Sum of nMin for purgeable caches */
52055 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
52056 unsigned int nPurgeable; /* Number of purgeable pages allocated */
52057 PgHdr1 lru; /* The beginning and end of the LRU list */
52058};
52059
52060/* Each page cache is an instance of the following object. Every
52061** open database file (including each in-memory database and each
52062** temporary or transient database) has a single page cache which
52063** is an instance of this object.
52064**
52065** Pointers to structures of this type are cast and returned as
52066** opaque sqlite3_pcache* handles.
52067*/
52068struct PCache1 {
52069 /* Cache configuration parameters. Page size (szPage) and the purgeable
52070 ** flag (bPurgeable) and the pnPurgeable pointer are all set when the
52071 ** cache is created and are never changed thereafter. nMax may be
52072 ** modified at any time by a call to the pcache1Cachesize() method.
52073 ** The PGroup mutex must be held when accessing nMax.
52074 */
52075 PGroup *pGroup; /* PGroup this cache belongs to */
52076 unsigned int *pnPurgeable; /* Pointer to pGroup->nPurgeable */
52077 int szPage; /* Size of database content section */
52078 int szExtra; /* sizeof(MemPage)+sizeof(PgHdr) */
52079 int szAlloc; /* Total size of one pcache line */
52080 int bPurgeable; /* True if cache is purgeable */
52081 unsigned int nMin; /* Minimum number of pages reserved */
52082 unsigned int nMax; /* Configured "cache_size" value */
52083 unsigned int n90pct; /* nMax*9/10 */
52084 unsigned int iMaxKey; /* Largest key seen since xTruncate() */
52085 unsigned int nPurgeableDummy; /* pnPurgeable points here when not used*/
52086
52087 /* Hash table of all pages. The following variables may only be accessed
52088 ** when the accessor is holding the PGroup mutex.
52089 */
52090 unsigned int nRecyclable; /* Number of pages in the LRU list */
52091 unsigned int nPage; /* Total number of pages in apHash */
52092 unsigned int nHash; /* Number of slots in apHash[] */
52093 PgHdr1 **apHash; /* Hash table for fast lookup by key */
52094 PgHdr1 *pFree; /* List of unused pcache-local pages */
52095 void *pBulk; /* Bulk memory used by pcache-local */
52096};
52097
52098/*
52099** Free slots in the allocator used to divide up the global page cache
52100** buffer provided using the SQLITE_CONFIG_PAGECACHE mechanism.
52101*/
52102struct PgFreeslot {
52103 PgFreeslot *pNext; /* Next free slot */
52104};
52105
52106/*
52107** Global data used by this cache.
52108*/
52109static SQLITE_WSD struct PCacheGlobal {
52110 PGroup grp; /* The global PGroup for mode (2) */
52111
52112 /* Variables related to SQLITE_CONFIG_PAGECACHE settings. The
52113 ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
52114 ** fixed at sqlite3_initialize() time and do not require mutex protection.
52115 ** The nFreeSlot and pFree values do require mutex protection.
52116 */
52117 int isInit; /* True if initialized */
52118 int separateCache; /* Use a new PGroup for each PCache */
52119 int nInitPage; /* Initial bulk allocation size */
52120 int szSlot; /* Size of each free slot */
52121 int nSlot; /* The number of pcache slots */
52122 int nReserve; /* Try to keep nFreeSlot above this */
52123 void *pStart, *pEnd; /* Bounds of global page cache memory */
52124 /* Above requires no mutex. Use mutex below for variable that follow. */
52125 sqlite3_mutex *mutex; /* Mutex for accessing the following: */
52126 PgFreeslot *pFree; /* Free page blocks */
52127 int nFreeSlot; /* Number of unused pcache slots */
52128 /* The following value requires a mutex to change. We skip the mutex on
52129 ** reading because (1) most platforms read a 32-bit integer atomically and
52130 ** (2) even if an incorrect value is read, no great harm is done since this
52131 ** is really just an optimization. */
52132 int bUnderPressure; /* True if low on PAGECACHE memory */
52133} pcache1_g;
52134
52135/*
52136** All code in this file should access the global structure above via the
52137** alias "pcache1". This ensures that the WSD emulation is used when
52138** compiling for systems that do not support real WSD.
52139*/
52140#define pcache1(pcache1_g) (GLOBAL(struct PCacheGlobal, pcache1_g)pcache1_g)
52141
52142/*
52143** Macros to enter and leave the PCache LRU mutex.
52144*/
52145#if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE2==0
52146# define pcache1EnterMutex(X)((void) (0)) assert((X)->mutex==0)((void) (0))
52147# define pcache1LeaveMutex(X)((void) (0)) assert((X)->mutex==0)((void) (0))
52148# define PCACHE1_MIGHT_USE_GROUP_MUTEX0 0
52149#else
52150# define pcache1EnterMutex(X)((void) (0)) sqlite3_mutex_enter((X)->mutex)
52151# define pcache1LeaveMutex(X)((void) (0)) sqlite3_mutex_leave((X)->mutex)
52152# define PCACHE1_MIGHT_USE_GROUP_MUTEX0 1
52153#endif
52154
52155/******************************************************************************/
52156/******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
52157
52158
52159/*
52160** This function is called during initialization if a static buffer is
52161** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
52162** verb to sqlite3_config(). Parameter pBuf points to an allocation large
52163** enough to contain 'n' buffers of 'sz' bytes each.
52164**
52165** This routine is called from sqlite3_initialize() and so it is guaranteed
52166** to be serialized already. There is no need for further mutexing.
52167*/
52168SQLITE_PRIVATEstatic void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
52169 if( pcache1(pcache1_g).isInit ){
52170 PgFreeslot *p;
52171 if( pBuf==0 ) sz = n = 0;
52172 if( n==0 ) sz = 0;
52173 sz = ROUNDDOWN8(sz)((sz)&~7);
52174 pcache1(pcache1_g).szSlot = sz;
52175 pcache1(pcache1_g).nSlot = pcache1(pcache1_g).nFreeSlot = n;
52176 pcache1(pcache1_g).nReserve = n>90 ? 10 : (n/10 + 1);
52177 pcache1(pcache1_g).pStart = pBuf;
52178 pcache1(pcache1_g).pFree = 0;
52179 pcache1(pcache1_g).bUnderPressure = 0;
52180 while( n-- ){
52181 p = (PgFreeslot*)pBuf;
52182 p->pNext = pcache1(pcache1_g).pFree;
52183 pcache1(pcache1_g).pFree = p;
52184 pBuf = (void*)&((char*)pBuf)[sz];
52185 }
52186 pcache1(pcache1_g).pEnd = pBuf;
52187 }
52188}
52189
52190/*
52191** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
52192** true if pCache->pFree ends up containing one or more free pages.
52193*/
52194static int pcache1InitBulk(PCache1 *pCache){
52195 i64 szBulk;
52196 char *zBulk;
52197 if( pcache1(pcache1_g).nInitPage==0 ) return 0;
52198 /* Do not bother with a bulk allocation if the cache size very small */
52199 if( pCache->nMax<3 ) return 0;
52200 sqlite3BeginBenignMalloc();
52201 if( pcache1(pcache1_g).nInitPage>0 ){
52202 szBulk = pCache->szAlloc * (i64)pcache1(pcache1_g).nInitPage;
52203 }else{
52204 szBulk = -1024 * (i64)pcache1(pcache1_g).nInitPage;
52205 }
52206 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
52207 szBulk = pCache->szAlloc*(i64)pCache->nMax;
52208 }
52209 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
52210 sqlite3EndBenignMalloc();
52211 if( zBulk ){
52212 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
52213 do{
52214 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
52215 pX->page.pBuf = zBulk;
52216 pX->page.pExtra = &pX[1];
52217 pX->isBulkLocal = 1;
52218 pX->isAnchor = 0;
52219 pX->pNext = pCache->pFree;
52220 pX->pLruPrev = 0; /* Initializing this saves a valgrind error */
52221 pCache->pFree = pX;
52222 zBulk += pCache->szAlloc;
52223 }while( --nBulk );
52224 }
52225 return pCache->pFree!=0;
52226}
52227
52228/*
52229** Malloc function used within this file to allocate space from the buffer
52230** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
52231** such buffer exists or there is no space left in it, this function falls
52232** back to sqlite3Malloc().
52233**
52234** Multiple threads can run this routine at the same time. Global variables
52235** in pcache1 need to be protected via mutex.
52236*/
52237static void *pcache1Alloc(int nByte){
52238 void *p = 0;
52239 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) )((void) (0));
52240 if( nByte<=pcache1(pcache1_g).szSlot ){
52241 sqlite3_mutex_enter(pcache1(pcache1_g).mutex);
52242 p = (PgHdr1 *)pcache1(pcache1_g).pFree;
52243 if( p ){
52244 pcache1(pcache1_g).pFree = pcache1(pcache1_g).pFree->pNext;
52245 pcache1(pcache1_g).nFreeSlot--;
52246 pcache1(pcache1_g).bUnderPressure = pcache1(pcache1_g).nFreeSlot<pcache1(pcache1_g).nReserve;
52247 assert( pcache1.nFreeSlot>=0 )((void) (0));
52248 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE7, nByte);
52249 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED1, 1);
52250 }
52251 sqlite3_mutex_leave(pcache1(pcache1_g).mutex);
52252 }
52253 if( p==0 ){
52254 /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get
52255 ** it from sqlite3Malloc instead.
52256 */
52257 p = sqlite3Malloc(nByte);
52258#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
52259 if( p ){
52260 int sz = sqlite3MallocSize(p);
52261 sqlite3_mutex_enter(pcache1(pcache1_g).mutex);
52262 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE7, nByte);
52263 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW2, sz);
52264 sqlite3_mutex_leave(pcache1(pcache1_g).mutex);
52265 }
52266#endif
52267 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
52268 }
52269 return p;
52270}
52271
52272/*
52273** Free an allocated buffer obtained from pcache1Alloc().
52274*/
52275static void pcache1Free(void *p){
52276 if( p==0 ) return;
52277 if( SQLITE_WITHIN(p, pcache1.pStart, pcache1.pEnd)(((uptr)(p)>=(uptr)((pcache1_g).pStart))&&((uptr)(
p)<(uptr)((pcache1_g).pEnd)))
){
52278 PgFreeslot *pSlot;
52279 sqlite3_mutex_enter(pcache1(pcache1_g).mutex);
52280 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED1, 1);
52281 pSlot = (PgFreeslot*)p;
52282 pSlot->pNext = pcache1(pcache1_g).pFree;
52283 pcache1(pcache1_g).pFree = pSlot;
52284 pcache1(pcache1_g).nFreeSlot++;
52285 pcache1(pcache1_g).bUnderPressure = pcache1(pcache1_g).nFreeSlot<pcache1(pcache1_g).nReserve;
52286 assert( pcache1.nFreeSlot<=pcache1.nSlot )((void) (0));
52287 sqlite3_mutex_leave(pcache1(pcache1_g).mutex);
52288 }else{
52289 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) )((void) (0));
52290 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
52291#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
52292 {
52293 int nFreed = 0;
52294 nFreed = sqlite3MallocSize(p);
52295 sqlite3_mutex_enter(pcache1(pcache1_g).mutex);
52296 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW2, nFreed);
52297 sqlite3_mutex_leave(pcache1(pcache1_g).mutex);
52298 }
52299#endif
52300 sqlite3_free(p);
52301 }
52302}
52303
52304#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
52305/*
52306** Return the size of a pcache allocation
52307*/
52308static int pcache1MemSize(void *p){
52309 if( p>=pcache1(pcache1_g).pStart && p<pcache1(pcache1_g).pEnd ){
52310 return pcache1(pcache1_g).szSlot;
52311 }else{
52312 int iSize;
52313 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) )((void) (0));
52314 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
52315 iSize = sqlite3MallocSize(p);
52316 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
52317 return iSize;
52318 }
52319}
52320#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
52321
52322/*
52323** Allocate a new page object initially associated with cache pCache.
52324*/
52325static PgHdr1 *pcache1AllocPage(PCache1 *pCache, int benignMalloc){
52326 PgHdr1 *p = 0;
52327 void *pPg;
52328
52329 assert( sqlite3_mutex_held(pCache->pGroup->mutex) )((void) (0));
52330 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
52331 assert( pCache->pFree!=0 )((void) (0));
52332 p = pCache->pFree;
52333 pCache->pFree = p->pNext;
52334 p->pNext = 0;
52335 }else{
52336#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
52337 /* The group mutex must be released before pcache1Alloc() is called. This
52338 ** is because it might call sqlite3_release_memory(), which assumes that
52339 ** this mutex is not held. */
52340 assert( pcache1.separateCache==0 )((void) (0));
52341 assert( pCache->pGroup==&pcache1.grp )((void) (0));
52342 pcache1LeaveMutex(pCache->pGroup)((void) (0));
52343#endif
52344 if( benignMalloc ){ sqlite3BeginBenignMalloc(); }
52345#ifdef SQLITE_PCACHE_SEPARATE_HEADER
52346 pPg = pcache1Alloc(pCache->szPage);
52347 p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
52348 if( !pPg || !p ){
52349 pcache1Free(pPg);
52350 sqlite3_free(p);
52351 pPg = 0;
52352 }
52353#else
52354 pPg = pcache1Alloc(pCache->szAlloc);
52355#endif
52356 if( benignMalloc ){ sqlite3EndBenignMalloc(); }
52357#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
52358 pcache1EnterMutex(pCache->pGroup)((void) (0));
52359#endif
52360 if( pPg==0 ) return 0;
52361#ifndef SQLITE_PCACHE_SEPARATE_HEADER
52362 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
52363#endif
52364 p->page.pBuf = pPg;
52365 p->page.pExtra = &p[1];
52366 p->isBulkLocal = 0;
52367 p->isAnchor = 0;
52368 p->pLruPrev = 0; /* Initializing this saves a valgrind error */
52369 }
52370 (*pCache->pnPurgeable)++;
52371 return p;
52372}
52373
52374/*
52375** Free a page object allocated by pcache1AllocPage().
52376*/
52377static void pcache1FreePage(PgHdr1 *p){
52378 PCache1 *pCache;
52379 assert( p!=0 )((void) (0));
52380 pCache = p->pCache;
52381 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) )((void) (0));
52382 if( p->isBulkLocal ){
52383 p->pNext = pCache->pFree;
52384 pCache->pFree = p;
52385 }else{
52386 pcache1Free(p->page.pBuf);
52387#ifdef SQLITE_PCACHE_SEPARATE_HEADER
52388 sqlite3_free(p);
52389#endif
52390 }
52391 (*pCache->pnPurgeable)--;
52392}
52393
52394/*
52395** Malloc function used by SQLite to obtain space from the buffer configured
52396** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
52397** exists, this function falls back to sqlite3Malloc().
52398*/
52399SQLITE_PRIVATEstatic void *sqlite3PageMalloc(int sz){
52400 assert( sz<=65536+8 )((void) (0)); /* These allocations are never very large */
52401 return pcache1Alloc(sz);
52402}
52403
52404/*
52405** Free an allocated buffer obtained from sqlite3PageMalloc().
52406*/
52407SQLITE_PRIVATEstatic void sqlite3PageFree(void *p){
52408 pcache1Free(p);
52409}
52410
52411
52412/*
52413** Return true if it desirable to avoid allocating a new page cache
52414** entry.
52415**
52416** If memory was allocated specifically to the page cache using
52417** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
52418** it is desirable to avoid allocating a new page cache entry because
52419** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
52420** for all page cache needs and we should not need to spill the
52421** allocation onto the heap.
52422**
52423** Or, the heap is used for all page cache memory but the heap is
52424** under memory pressure, then again it is desirable to avoid
52425** allocating a new page cache entry in order to avoid stressing
52426** the heap even further.
52427*/
52428static int pcache1UnderMemoryPressure(PCache1 *pCache){
52429 if( pcache1(pcache1_g).nSlot && (pCache->szPage+pCache->szExtra)<=pcache1(pcache1_g).szSlot ){
52430 return pcache1(pcache1_g).bUnderPressure;
52431 }else{
52432 return sqlite3HeapNearlyFull();
52433 }
52434}
52435
52436/******************************************************************************/
52437/******** General Implementation Functions ************************************/
52438
52439/*
52440** This function is used to resize the hash table used by the cache passed
52441** as the first argument.
52442**
52443** The PCache mutex must be held when this function is called.
52444*/
52445static void pcache1ResizeHash(PCache1 *p){
52446 PgHdr1 **apNew;
52447 unsigned int nNew;
52448 unsigned int i;
52449
52450 assert( sqlite3_mutex_held(p->pGroup->mutex) )((void) (0));
52451
52452 nNew = p->nHash*2;
52453 if( nNew<256 ){
52454 nNew = 256;
52455 }
52456
52457 pcache1LeaveMutex(p->pGroup)((void) (0));
52458 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
52459 apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew);
52460 if( p->nHash ){ sqlite3EndBenignMalloc(); }
52461 pcache1EnterMutex(p->pGroup)((void) (0));
52462 if( apNew ){
52463 for(i=0; i<p->nHash; i++){
52464 PgHdr1 *pPage;
52465 PgHdr1 *pNext = p->apHash[i];
52466 while( (pPage = pNext)!=0 ){
52467 unsigned int h = pPage->iKey % nNew;
52468 pNext = pPage->pNext;
52469 pPage->pNext = apNew[h];
52470 apNew[h] = pPage;
52471 }
52472 }
52473 sqlite3_free(p->apHash);
52474 p->apHash = apNew;
52475 p->nHash = nNew;
52476 }
52477}
52478
52479/*
52480** This function is used internally to remove the page pPage from the
52481** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
52482** LRU list, then this function is a no-op.
52483**
52484** The PGroup mutex must be held when this function is called.
52485*/
52486static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){
52487 assert( pPage!=0 )((void) (0));
52488 assert( PAGE_IS_UNPINNED(pPage) )((void) (0));
52489 assert( pPage->pLruNext )((void) (0));
52490 assert( pPage->pLruPrev )((void) (0));
52491 assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) )((void) (0));
52492 pPage->pLruPrev->pLruNext = pPage->pLruNext;
52493 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
52494 pPage->pLruNext = 0;
52495 /* pPage->pLruPrev = 0;
52496 ** No need to clear pLruPrev as it is never accessed if pLruNext is 0 */
52497 assert( pPage->isAnchor==0 )((void) (0));
52498 assert( pPage->pCache->pGroup->lru.isAnchor==1 )((void) (0));
52499 pPage->pCache->nRecyclable--;
52500 return pPage;
52501}
52502
52503
52504/*
52505** Remove the page supplied as an argument from the hash table
52506** (PCache1.apHash structure) that it is currently stored in.
52507** Also free the page if freePage is true.
52508**
52509** The PGroup mutex must be held when this function is called.
52510*/
52511static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){
52512 unsigned int h;
52513 PCache1 *pCache = pPage->pCache;
52514 PgHdr1 **pp;
52515
52516 assert( sqlite3_mutex_held(pCache->pGroup->mutex) )((void) (0));
52517 h = pPage->iKey % pCache->nHash;
52518 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
52519 *pp = (*pp)->pNext;
52520
52521 pCache->nPage--;
52522 if( freeFlag ) pcache1FreePage(pPage);
52523}
52524
52525/*
52526** If there are currently more than nMaxPage pages allocated, try
52527** to recycle pages to reduce the number allocated to nMaxPage.
52528*/
52529static void pcache1EnforceMaxPage(PCache1 *pCache){
52530 PGroup *pGroup = pCache->pGroup;
52531 PgHdr1 *p;
52532 assert( sqlite3_mutex_held(pGroup->mutex) )((void) (0));
52533 while( pGroup->nPurgeable>pGroup->nMaxPage
52534 && (p=pGroup->lru.pLruPrev)->isAnchor==0
52535 ){
52536 assert( p->pCache->pGroup==pGroup )((void) (0));
52537 assert( PAGE_IS_UNPINNED(p) )((void) (0));
52538 pcache1PinPage(p);
52539 pcache1RemoveFromHash(p, 1);
52540 }
52541 if( pCache->nPage==0 && pCache->pBulk ){
52542 sqlite3_free(pCache->pBulk);
52543 pCache->pBulk = pCache->pFree = 0;
52544 }
52545}
52546
52547/*
52548** Discard all pages from cache pCache with a page number (key value)
52549** greater than or equal to iLimit. Any pinned pages that meet this
52550** criteria are unpinned before they are discarded.
52551**
52552** The PCache mutex must be held when this function is called.
52553*/
52554static void pcache1TruncateUnsafe(
52555 PCache1 *pCache, /* The cache to truncate */
52556 unsigned int iLimit /* Drop pages with this pgno or larger */
52557){
52558 TESTONLY( int nPage = 0; ) /* To assert pCache->nPage is correct */
52559 unsigned int h, iStop;
52560 assert( sqlite3_mutex_held(pCache->pGroup->mutex) )((void) (0));
52561 assert( pCache->iMaxKey >= iLimit )((void) (0));
52562 assert( pCache->nHash > 0 )((void) (0));
52563 if( pCache->iMaxKey - iLimit < pCache->nHash ){
52564 /* If we are just shaving the last few pages off the end of the
52565 ** cache, then there is no point in scanning the entire hash table.
52566 ** Only scan those hash slots that might contain pages that need to
52567 ** be removed. */
52568 h = iLimit % pCache->nHash;
52569 iStop = pCache->iMaxKey % pCache->nHash;
52570 TESTONLY( nPage = -10; ) /* Disable the pCache->nPage validity check */
52571 }else{
52572 /* This is the general case where many pages are being removed.
52573 ** It is necessary to scan the entire hash table */
52574 h = pCache->nHash/2;
52575 iStop = h - 1;
52576 }
52577 for(;;){
52578 PgHdr1 **pp;
52579 PgHdr1 *pPage;
52580 assert( h<pCache->nHash )((void) (0));
52581 pp = &pCache->apHash[h];
52582 while( (pPage = *pp)!=0 ){
52583 if( pPage->iKey>=iLimit ){
52584 pCache->nPage--;
52585 *pp = pPage->pNext;
52586 if( PAGE_IS_UNPINNED(pPage)((pPage)->pLruNext!=0) ) pcache1PinPage(pPage);
52587 pcache1FreePage(pPage);
52588 }else{
52589 pp = &pPage->pNext;
52590 TESTONLY( if( nPage>=0 ) nPage++; )
52591 }
52592 }
52593 if( h==iStop ) break;
52594 h = (h+1) % pCache->nHash;
52595 }
52596 assert( nPage<0 || pCache->nPage==(unsigned)nPage )((void) (0));
52597}
52598
52599/******************************************************************************/
52600/******** sqlite3_pcache Methods **********************************************/
52601
52602/*
52603** Implementation of the sqlite3_pcache.xInit method.
52604*/
52605static int pcache1Init(void *NotUsed){
52606 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
52607 assert( pcache1.isInit==0 )((void) (0));
52608 memset(&pcache1(pcache1_g), 0, sizeof(pcache1(pcache1_g)));
52609
52610
52611 /*
52612 ** The pcache1.separateCache variable is true if each PCache has its own
52613 ** private PGroup (mode-1). pcache1.separateCache is false if the single
52614 ** PGroup in pcache1.grp is used for all page caches (mode-2).
52615 **
52616 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
52617 **
52618 ** * Use a unified cache in single-threaded applications that have
52619 ** configured a start-time buffer for use as page-cache memory using
52620 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
52621 ** pBuf argument.
52622 **
52623 ** * Otherwise use separate caches (mode-1)
52624 */
52625#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT)
52626 pcache1(pcache1_g).separateCache = 0;
52627#elif SQLITE_THREADSAFE2
52628 pcache1(pcache1_g).separateCache = sqlite3GlobalConfigsqlite3Config.pPage==0
52629 || sqlite3GlobalConfigsqlite3Config.bCoreMutex>0;
52630#else
52631 pcache1(pcache1_g).separateCache = sqlite3GlobalConfigsqlite3Config.pPage==0;
52632#endif
52633
52634#if SQLITE_THREADSAFE2
52635 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
52636 pcache1(pcache1_g).grp.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU6);
52637 pcache1(pcache1_g).mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PMEM7);
52638 }
52639#endif
52640 if( pcache1(pcache1_g).separateCache
52641 && sqlite3GlobalConfigsqlite3Config.nPage!=0
52642 && sqlite3GlobalConfigsqlite3Config.pPage==0
52643 ){
52644 pcache1(pcache1_g).nInitPage = sqlite3GlobalConfigsqlite3Config.nPage;
52645 }else{
52646 pcache1(pcache1_g).nInitPage = 0;
52647 }
52648 pcache1(pcache1_g).grp.mxPinned = 10;
52649 pcache1(pcache1_g).isInit = 1;
52650 return SQLITE_OK0;
52651}
52652
52653/*
52654** Implementation of the sqlite3_pcache.xShutdown method.
52655** Note that the static mutex allocated in xInit does
52656** not need to be freed.
52657*/
52658static void pcache1Shutdown(void *NotUsed){
52659 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
52660 assert( pcache1.isInit!=0 )((void) (0));
52661 memset(&pcache1(pcache1_g), 0, sizeof(pcache1(pcache1_g)));
52662}
52663
52664/* forward declaration */
52665static void pcache1Destroy(sqlite3_pcache *p);
52666
52667/*
52668** Implementation of the sqlite3_pcache.xCreate method.
52669**
52670** Allocate a new cache.
52671*/
52672static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
52673 PCache1 *pCache; /* The newly created page cache */
52674 PGroup *pGroup; /* The group the new page cache will belong to */
52675 int sz; /* Bytes of memory required to allocate the new cache */
52676
52677 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 )((void) (0));
52678 assert( szExtra < 300 )((void) (0));
52679
52680 sz = sizeof(PCache1) + sizeof(PGroup)*pcache1(pcache1_g).separateCache;
52681 pCache = (PCache1 *)sqlite3MallocZero(sz);
52682 if( pCache ){
52683 if( pcache1(pcache1_g).separateCache ){
52684 pGroup = (PGroup*)&pCache[1];
52685 pGroup->mxPinned = 10;
52686 }else{
52687 pGroup = &pcache1(pcache1_g).grp;
52688 }
52689 pcache1EnterMutex(pGroup)((void) (0));
52690 if( pGroup->lru.isAnchor==0 ){
52691 pGroup->lru.isAnchor = 1;
52692 pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
52693 }
52694 pCache->pGroup = pGroup;
52695 pCache->szPage = szPage;
52696 pCache->szExtra = szExtra;
52697 pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1))(((sizeof(PgHdr1))+7)&~7);
52698 pCache->bPurgeable = (bPurgeable ? 1 : 0);
52699 pcache1ResizeHash(pCache);
52700 if( bPurgeable ){
52701 pCache->nMin = 10;
52702 pGroup->nMinPage += pCache->nMin;
52703 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
52704 pCache->pnPurgeable = &pGroup->nPurgeable;
52705 }else{
52706 pCache->pnPurgeable = &pCache->nPurgeableDummy;
52707 }
52708 pcache1LeaveMutex(pGroup)((void) (0));
52709 if( pCache->nHash==0 ){
52710 pcache1Destroy((sqlite3_pcache*)pCache);
52711 pCache = 0;
52712 }
52713 }
52714 return (sqlite3_pcache *)pCache;
52715}
52716
52717/*
52718** Implementation of the sqlite3_pcache.xCachesize method.
52719**
52720** Configure the cache_size limit for a cache.
52721*/
52722static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
52723 PCache1 *pCache = (PCache1 *)p;
52724 u32 n;
52725 assert( nMax>=0 )((void) (0));
52726 if( pCache->bPurgeable ){
52727 PGroup *pGroup = pCache->pGroup;
52728 pcache1EnterMutex(pGroup)((void) (0));
52729 n = (u32)nMax;
52730 if( n > 0x7fff0000 - pGroup->nMaxPage + pCache->nMax ){
52731 n = 0x7fff0000 - pGroup->nMaxPage + pCache->nMax;
52732 }
52733 pGroup->nMaxPage += (n - pCache->nMax);
52734 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
52735 pCache->nMax = n;
52736 pCache->n90pct = pCache->nMax*9/10;
52737 pcache1EnforceMaxPage(pCache);
52738 pcache1LeaveMutex(pGroup)((void) (0));
52739 }
52740}
52741
52742/*
52743** Implementation of the sqlite3_pcache.xShrink method.
52744**
52745** Free up as much memory as possible.
52746*/
52747static void pcache1Shrink(sqlite3_pcache *p){
52748 PCache1 *pCache = (PCache1*)p;
52749 if( pCache->bPurgeable ){
52750 PGroup *pGroup = pCache->pGroup;
52751 unsigned int savedMaxPage;
52752 pcache1EnterMutex(pGroup)((void) (0));
52753 savedMaxPage = pGroup->nMaxPage;
52754 pGroup->nMaxPage = 0;
52755 pcache1EnforceMaxPage(pCache);
52756 pGroup->nMaxPage = savedMaxPage;
52757 pcache1LeaveMutex(pGroup)((void) (0));
52758 }
52759}
52760
52761/*
52762** Implementation of the sqlite3_pcache.xPagecount method.
52763*/
52764static int pcache1Pagecount(sqlite3_pcache *p){
52765 int n;
52766 PCache1 *pCache = (PCache1*)p;
52767 pcache1EnterMutex(pCache->pGroup)((void) (0));
52768 n = pCache->nPage;
52769 pcache1LeaveMutex(pCache->pGroup)((void) (0));
52770 return n;
52771}
52772
52773
52774/*
52775** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described
52776** in the header of the pcache1Fetch() procedure.
52777**
52778** This steps are broken out into a separate procedure because they are
52779** usually not needed, and by avoiding the stack initialization required
52780** for these steps, the main pcache1Fetch() procedure can run faster.
52781*/
52782static SQLITE_NOINLINE__attribute__((noinline)) PgHdr1 *pcache1FetchStage2(
52783 PCache1 *pCache,
52784 unsigned int iKey,
52785 int createFlag
52786){
52787 unsigned int nPinned;
52788 PGroup *pGroup = pCache->pGroup;
52789 PgHdr1 *pPage = 0;
52790
52791 /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
52792 assert( pCache->nPage >= pCache->nRecyclable )((void) (0));
52793 nPinned = pCache->nPage - pCache->nRecyclable;
52794 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage )((void) (0));
52795 assert( pCache->n90pct == pCache->nMax*9/10 )((void) (0));
52796 if( createFlag==1 && (
52797 nPinned>=pGroup->mxPinned
52798 || nPinned>=pCache->n90pct
52799 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
52800 )){
52801 return 0;
52802 }
52803
52804 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
52805 assert( pCache->nHash>0 && pCache->apHash )((void) (0));
52806
52807 /* Step 4. Try to recycle a page. */
52808 if( pCache->bPurgeable
52809 && !pGroup->lru.pLruPrev->isAnchor
52810 && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
52811 ){
52812 PCache1 *pOther;
52813 pPage = pGroup->lru.pLruPrev;
52814 assert( PAGE_IS_UNPINNED(pPage) )((void) (0));
52815 pcache1RemoveFromHash(pPage, 0);
52816 pcache1PinPage(pPage);
52817 pOther = pPage->pCache;
52818 if( pOther->szAlloc != pCache->szAlloc ){
52819 pcache1FreePage(pPage);
52820 pPage = 0;
52821 }else{
52822 pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
52823 }
52824 }
52825
52826 /* Step 5. If a usable page buffer has still not been found,
52827 ** attempt to allocate a new one.
52828 */
52829 if( !pPage ){
52830 pPage = pcache1AllocPage(pCache, createFlag==1);
52831 }
52832
52833 if( pPage ){
52834 unsigned int h = iKey % pCache->nHash;
52835 pCache->nPage++;
52836 pPage->iKey = iKey;
52837 pPage->pNext = pCache->apHash[h];
52838 pPage->pCache = pCache;
52839 pPage->pLruNext = 0;
52840 /* pPage->pLruPrev = 0;
52841 ** No need to clear pLruPrev since it is not accessed when pLruNext==0 */
52842 *(void **)pPage->page.pExtra = 0;
52843 pCache->apHash[h] = pPage;
52844 if( iKey>pCache->iMaxKey ){
52845 pCache->iMaxKey = iKey;
52846 }
52847 }
52848 return pPage;
52849}
52850
52851/*
52852** Implementation of the sqlite3_pcache.xFetch method.
52853**
52854** Fetch a page by key value.
52855**
52856** Whether or not a new page may be allocated by this function depends on
52857** the value of the createFlag argument. 0 means do not allocate a new
52858** page. 1 means allocate a new page if space is easily available. 2
52859** means to try really hard to allocate a new page.
52860**
52861** For a non-purgeable cache (a cache used as the storage for an in-memory
52862** database) there is really no difference between createFlag 1 and 2. So
52863** the calling function (pcache.c) will never have a createFlag of 1 on
52864** a non-purgeable cache.
52865**
52866** There are three different approaches to obtaining space for a page,
52867** depending on the value of parameter createFlag (which may be 0, 1 or 2).
52868**
52869** 1. Regardless of the value of createFlag, the cache is searched for a
52870** copy of the requested page. If one is found, it is returned.
52871**
52872** 2. If createFlag==0 and the page is not already in the cache, NULL is
52873** returned.
52874**
52875** 3. If createFlag is 1, and the page is not already in the cache, then
52876** return NULL (do not allocate a new page) if any of the following
52877** conditions are true:
52878**
52879** (a) the number of pages pinned by the cache is greater than
52880** PCache1.nMax, or
52881**
52882** (b) the number of pages pinned by the cache is greater than
52883** the sum of nMax for all purgeable caches, less the sum of
52884** nMin for all other purgeable caches, or
52885**
52886** 4. If none of the first three conditions apply and the cache is marked
52887** as purgeable, and if one of the following is true:
52888**
52889** (a) The number of pages allocated for the cache is already
52890** PCache1.nMax, or
52891**
52892** (b) The number of pages allocated for all purgeable caches is
52893** already equal to or greater than the sum of nMax for all
52894** purgeable caches,
52895**
52896** (c) The system is under memory pressure and wants to avoid
52897** unnecessary pages cache entry allocations
52898**
52899** then attempt to recycle a page from the LRU list. If it is the right
52900** size, return the recycled buffer. Otherwise, free the buffer and
52901** proceed to step 5.
52902**
52903** 5. Otherwise, allocate and return a new page buffer.
52904**
52905** There are two versions of this routine. pcache1FetchWithMutex() is
52906** the general case. pcache1FetchNoMutex() is a faster implementation for
52907** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
52908** invokes the appropriate routine.
52909*/
52910static PgHdr1 *pcache1FetchNoMutex(
52911 sqlite3_pcache *p,
52912 unsigned int iKey,
52913 int createFlag
52914){
52915 PCache1 *pCache = (PCache1 *)p;
52916 PgHdr1 *pPage = 0;
52917
52918 /* Step 1: Search the hash table for an existing entry. */
52919 pPage = pCache->apHash[iKey % pCache->nHash];
52920 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
52921
52922 /* Step 2: If the page was found in the hash table, then return it.
52923 ** If the page was not in the hash table and createFlag is 0, abort.
52924 ** Otherwise (page not in hash and createFlag!=0) continue with
52925 ** subsequent steps to try to create the page. */
52926 if( pPage ){
52927 if( PAGE_IS_UNPINNED(pPage)((pPage)->pLruNext!=0) ){
52928 return pcache1PinPage(pPage);
52929 }else{
52930 return pPage;
52931 }
52932 }else if( createFlag ){
52933 /* Steps 3, 4, and 5 implemented by this subroutine */
52934 return pcache1FetchStage2(pCache, iKey, createFlag);
52935 }else{
52936 return 0;
52937 }
52938}
52939#if PCACHE1_MIGHT_USE_GROUP_MUTEX0
52940static PgHdr1 *pcache1FetchWithMutex(
52941 sqlite3_pcache *p,
52942 unsigned int iKey,
52943 int createFlag
52944){
52945 PCache1 *pCache = (PCache1 *)p;
52946 PgHdr1 *pPage;
52947
52948 pcache1EnterMutex(pCache->pGroup)((void) (0));
52949 pPage = pcache1FetchNoMutex(p, iKey, createFlag);
52950 assert( pPage==0 || pCache->iMaxKey>=iKey )((void) (0));
52951 pcache1LeaveMutex(pCache->pGroup)((void) (0));
52952 return pPage;
52953}
52954#endif
52955static sqlite3_pcache_page *pcache1Fetch(
52956 sqlite3_pcache *p,
52957 unsigned int iKey,
52958 int createFlag
52959){
52960#if PCACHE1_MIGHT_USE_GROUP_MUTEX0 || defined(SQLITE_DEBUG)
52961 PCache1 *pCache = (PCache1 *)p;
52962#endif
52963
52964 assert( offsetof(PgHdr1,page)==0 )((void) (0));
52965 assert( pCache->bPurgeable || createFlag!=1 )((void) (0));
52966 assert( pCache->bPurgeable || pCache->nMin==0 )((void) (0));
52967 assert( pCache->bPurgeable==0 || pCache->nMin==10 )((void) (0));
52968 assert( pCache->nMin==0 || pCache->bPurgeable )((void) (0));
52969 assert( pCache->nHash>0 )((void) (0));
52970#if PCACHE1_MIGHT_USE_GROUP_MUTEX0
52971 if( pCache->pGroup->mutex ){
52972 return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag);
52973 }else
52974#endif
52975 {
52976 return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag);
52977 }
52978}
52979
52980
52981/*
52982** Implementation of the sqlite3_pcache.xUnpin method.
52983**
52984** Mark a page as unpinned (eligible for asynchronous recycling).
52985*/
52986static void pcache1Unpin(
52987 sqlite3_pcache *p,
52988 sqlite3_pcache_page *pPg,
52989 int reuseUnlikely
52990){
52991 PCache1 *pCache = (PCache1 *)p;
52992 PgHdr1 *pPage = (PgHdr1 *)pPg;
52993 PGroup *pGroup = pCache->pGroup;
52994
52995 assert( pPage->pCache==pCache )((void) (0));
52996 pcache1EnterMutex(pGroup)((void) (0));
52997
52998 /* It is an error to call this function if the page is already
52999 ** part of the PGroup LRU list.
53000 */
53001 assert( pPage->pLruNext==0 )((void) (0));
53002 assert( PAGE_IS_PINNED(pPage) )((void) (0));
53003
53004 if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
53005 pcache1RemoveFromHash(pPage, 1);
53006 }else{
53007 /* Add the page to the PGroup LRU list. */
53008 PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
53009 pPage->pLruPrev = &pGroup->lru;
53010 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
53011 *ppFirst = pPage;
53012 pCache->nRecyclable++;
53013 }
53014
53015 pcache1LeaveMutex(pCache->pGroup)((void) (0));
53016}
53017
53018/*
53019** Implementation of the sqlite3_pcache.xRekey method.
53020*/
53021static void pcache1Rekey(
53022 sqlite3_pcache *p,
53023 sqlite3_pcache_page *pPg,
53024 unsigned int iOld,
53025 unsigned int iNew
53026){
53027 PCache1 *pCache = (PCache1 *)p;
53028 PgHdr1 *pPage = (PgHdr1 *)pPg;
53029 PgHdr1 **pp;
53030 unsigned int h;
53031 assert( pPage->iKey==iOld )((void) (0));
53032 assert( pPage->pCache==pCache )((void) (0));
53033
53034 pcache1EnterMutex(pCache->pGroup)((void) (0));
53035
53036 h = iOld%pCache->nHash;
53037 pp = &pCache->apHash[h];
53038 while( (*pp)!=pPage ){
53039 pp = &(*pp)->pNext;
53040 }
53041 *pp = pPage->pNext;
53042
53043 h = iNew%pCache->nHash;
53044 pPage->iKey = iNew;
53045 pPage->pNext = pCache->apHash[h];
53046 pCache->apHash[h] = pPage;
53047 if( iNew>pCache->iMaxKey ){
53048 pCache->iMaxKey = iNew;
53049 }
53050
53051 pcache1LeaveMutex(pCache->pGroup)((void) (0));
53052}
53053
53054/*
53055** Implementation of the sqlite3_pcache.xTruncate method.
53056**
53057** Discard all unpinned pages in the cache with a page number equal to
53058** or greater than parameter iLimit. Any pinned pages with a page number
53059** equal to or greater than iLimit are implicitly unpinned.
53060*/
53061static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
53062 PCache1 *pCache = (PCache1 *)p;
53063 pcache1EnterMutex(pCache->pGroup)((void) (0));
53064 if( iLimit<=pCache->iMaxKey ){
53065 pcache1TruncateUnsafe(pCache, iLimit);
53066 pCache->iMaxKey = iLimit-1;
53067 }
53068 pcache1LeaveMutex(pCache->pGroup)((void) (0));
53069}
53070
53071/*
53072** Implementation of the sqlite3_pcache.xDestroy method.
53073**
53074** Destroy a cache allocated using pcache1Create().
53075*/
53076static void pcache1Destroy(sqlite3_pcache *p){
53077 PCache1 *pCache = (PCache1 *)p;
53078 PGroup *pGroup = pCache->pGroup;
53079 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) )((void) (0));
53080 pcache1EnterMutex(pGroup)((void) (0));
53081 if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
53082 assert( pGroup->nMaxPage >= pCache->nMax )((void) (0));
53083 pGroup->nMaxPage -= pCache->nMax;
53084 assert( pGroup->nMinPage >= pCache->nMin )((void) (0));
53085 pGroup->nMinPage -= pCache->nMin;
53086 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
53087 pcache1EnforceMaxPage(pCache);
53088 pcache1LeaveMutex(pGroup)((void) (0));
53089 sqlite3_free(pCache->pBulk);
53090 sqlite3_free(pCache->apHash);
53091 sqlite3_free(pCache);
53092}
53093
53094/*
53095** This function is called during initialization (sqlite3_initialize()) to
53096** install the default pluggable cache module, assuming the user has not
53097** already provided an alternative.
53098*/
53099SQLITE_PRIVATEstatic void sqlite3PCacheSetDefault(void){
53100 static const sqlite3_pcache_methods2 defaultMethods = {
53101 1, /* iVersion */
53102 0, /* pArg */
53103 pcache1Init, /* xInit */
53104 pcache1Shutdown, /* xShutdown */
53105 pcache1Create, /* xCreate */
53106 pcache1Cachesize, /* xCachesize */
53107 pcache1Pagecount, /* xPagecount */
53108 pcache1Fetch, /* xFetch */
53109 pcache1Unpin, /* xUnpin */
53110 pcache1Rekey, /* xRekey */
53111 pcache1Truncate, /* xTruncate */
53112 pcache1Destroy, /* xDestroy */
53113 pcache1Shrink /* xShrink */
53114 };
53115 sqlite3_config(SQLITE_CONFIG_PCACHE218, &defaultMethods);
53116}
53117
53118/*
53119** Return the size of the header on each page of this PCACHE implementation.
53120*/
53121SQLITE_PRIVATEstatic int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1))(((sizeof(PgHdr1))+7)&~7); }
53122
53123/*
53124** Return the global mutex used by this PCACHE implementation. The
53125** sqlite3_status() routine needs access to this mutex.
53126*/
53127SQLITE_PRIVATEstatic sqlite3_mutex *sqlite3Pcache1Mutex(void){
53128 return pcache1(pcache1_g).mutex;
53129}
53130
53131#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
53132/*
53133** This function is called to free superfluous dynamically allocated memory
53134** held by the pager system. Memory in use by any SQLite pager allocated
53135** by the current thread may be sqlite3_free()ed.
53136**
53137** nReq is the number of bytes of memory required. Once this much has
53138** been released, the function returns. The return value is the total number
53139** of bytes of memory released.
53140*/
53141SQLITE_PRIVATEstatic int sqlite3PcacheReleaseMemory(int nReq){
53142 int nFree = 0;
53143 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) )((void) (0));
53144 assert( sqlite3_mutex_notheld(pcache1.mutex) )((void) (0));
53145 if( sqlite3GlobalConfigsqlite3Config.pPage==0 ){
53146 PgHdr1 *p;
53147 pcache1EnterMutex(&pcache1.grp)((void) (0));
53148 while( (nReq<0 || nFree<nReq)
53149 && (p=pcache1(pcache1_g).grp.lru.pLruPrev)!=0
53150 && p->isAnchor==0
53151 ){
53152 nFree += pcache1MemSize(p->page.pBuf);
53153#ifdef SQLITE_PCACHE_SEPARATE_HEADER
53154 nFree += sqlite3MemSize(p);
53155#endif
53156 assert( PAGE_IS_UNPINNED(p) )((void) (0));
53157 pcache1PinPage(p);
53158 pcache1RemoveFromHash(p, 1);
53159 }
53160 pcache1LeaveMutex(&pcache1.grp)((void) (0));
53161 }
53162 return nFree;
53163}
53164#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
53165
53166#ifdef SQLITE_TEST
53167/*
53168** This function is used by test procedures to inspect the internal state
53169** of the global cache.
53170*/
53171SQLITE_PRIVATEstatic void sqlite3PcacheStats(
53172 int *pnCurrent, /* OUT: Total number of pages cached */
53173 int *pnMax, /* OUT: Global maximum cache size */
53174 int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */
53175 int *pnRecyclable /* OUT: Total number of pages available for recycling */
53176){
53177 PgHdr1 *p;
53178 int nRecyclable = 0;
53179 for(p=pcache1(pcache1_g).grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
53180 assert( PAGE_IS_UNPINNED(p) )((void) (0));
53181 nRecyclable++;
53182 }
53183 *pnCurrent = pcache1(pcache1_g).grp.nPurgeable;
53184 *pnMax = (int)pcache1(pcache1_g).grp.nMaxPage;
53185 *pnMin = (int)pcache1(pcache1_g).grp.nMinPage;
53186 *pnRecyclable = nRecyclable;
53187}
53188#endif
53189
53190/************** End of pcache1.c *********************************************/
53191/************** Begin file rowset.c ******************************************/
53192/*
53193** 2008 December 3
53194**
53195** The author disclaims copyright to this source code. In place of
53196** a legal notice, here is a blessing:
53197**
53198** May you do good and not evil.
53199** May you find forgiveness for yourself and forgive others.
53200** May you share freely, never taking more than you give.
53201**
53202*************************************************************************
53203**
53204** This module implements an object we call a "RowSet".
53205**
53206** The RowSet object is a collection of rowids. Rowids
53207** are inserted into the RowSet in an arbitrary order. Inserts
53208** can be intermixed with tests to see if a given rowid has been
53209** previously inserted into the RowSet.
53210**
53211** After all inserts are finished, it is possible to extract the
53212** elements of the RowSet in sorted order. Once this extraction
53213** process has started, no new elements may be inserted.
53214**
53215** Hence, the primitive operations for a RowSet are:
53216**
53217** CREATE
53218** INSERT
53219** TEST
53220** SMALLEST
53221** DESTROY
53222**
53223** The CREATE and DESTROY primitives are the constructor and destructor,
53224** obviously. The INSERT primitive adds a new element to the RowSet.
53225** TEST checks to see if an element is already in the RowSet. SMALLEST
53226** extracts the least value from the RowSet.
53227**
53228** The INSERT primitive might allocate additional memory. Memory is
53229** allocated in chunks so most INSERTs do no allocation. There is an
53230** upper bound on the size of allocated memory. No memory is freed
53231** until DESTROY.
53232**
53233** The TEST primitive includes a "batch" number. The TEST primitive
53234** will only see elements that were inserted before the last change
53235** in the batch number. In other words, if an INSERT occurs between
53236** two TESTs where the TESTs have the same batch nubmer, then the
53237** value added by the INSERT will not be visible to the second TEST.
53238** The initial batch number is zero, so if the very first TEST contains
53239** a non-zero batch number, it will see all prior INSERTs.
53240**
53241** No INSERTs may occurs after a SMALLEST. An assertion will fail if
53242** that is attempted.
53243**
53244** The cost of an INSERT is roughly constant. (Sometimes new memory
53245** has to be allocated on an INSERT.) The cost of a TEST with a new
53246** batch number is O(NlogN) where N is the number of elements in the RowSet.
53247** The cost of a TEST using the same batch number is O(logN). The cost
53248** of the first SMALLEST is O(NlogN). Second and subsequent SMALLEST
53249** primitives are constant time. The cost of DESTROY is O(N).
53250**
53251** TEST and SMALLEST may not be used by the same RowSet. This used to
53252** be possible, but the feature was not used, so it was removed in order
53253** to simplify the code.
53254*/
53255/* #include "sqliteInt.h" */
53256
53257
53258/*
53259** Target size for allocation chunks.
53260*/
53261#define ROWSET_ALLOCATION_SIZE1024 1024
53262
53263/*
53264** The number of rowset entries per allocation chunk.
53265*/
53266#define ROWSET_ENTRY_PER_CHUNK((1024 -8)/sizeof(struct RowSetEntry)) \
53267 ((ROWSET_ALLOCATION_SIZE1024-8)/sizeof(struct RowSetEntry))
53268
53269/*
53270** Each entry in a RowSet is an instance of the following object.
53271**
53272** This same object is reused to store a linked list of trees of RowSetEntry
53273** objects. In that alternative use, pRight points to the next entry
53274** in the list, pLeft points to the tree, and v is unused. The
53275** RowSet.pForest value points to the head of this forest list.
53276*/
53277struct RowSetEntry {
53278 i64 v; /* ROWID value for this entry */
53279 struct RowSetEntry *pRight; /* Right subtree (larger entries) or list */
53280 struct RowSetEntry *pLeft; /* Left subtree (smaller entries) */
53281};
53282
53283/*
53284** RowSetEntry objects are allocated in large chunks (instances of the
53285** following structure) to reduce memory allocation overhead. The
53286** chunks are kept on a linked list so that they can be deallocated
53287** when the RowSet is destroyed.
53288*/
53289struct RowSetChunk {
53290 struct RowSetChunk *pNextChunk; /* Next chunk on list of them all */
53291 struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK((1024 -8)/sizeof(struct RowSetEntry))]; /* Allocated entries */
53292};
53293
53294/*
53295** A RowSet in an instance of the following structure.
53296**
53297** A typedef of this structure if found in sqliteInt.h.
53298*/
53299struct RowSet {
53300 struct RowSetChunk *pChunk; /* List of all chunk allocations */
53301 sqlite3 *db; /* The database connection */
53302 struct RowSetEntry *pEntry; /* List of entries using pRight */
53303 struct RowSetEntry *pLast; /* Last entry on the pEntry list */
53304 struct RowSetEntry *pFresh; /* Source of new entry objects */
53305 struct RowSetEntry *pForest; /* List of binary trees of entries */
53306 u16 nFresh; /* Number of objects on pFresh */
53307 u16 rsFlags; /* Various flags */
53308 int iBatch; /* Current insert batch */
53309};
53310
53311/*
53312** Allowed values for RowSet.rsFlags
53313*/
53314#define ROWSET_SORTED0x01 0x01 /* True if RowSet.pEntry is sorted */
53315#define ROWSET_NEXT0x02 0x02 /* True if sqlite3RowSetNext() has been called */
53316
53317/*
53318** Allocate a RowSet object. Return NULL if a memory allocation
53319** error occurs.
53320*/
53321SQLITE_PRIVATEstatic RowSet *sqlite3RowSetInit(sqlite3 *db){
53322 RowSet *p = sqlite3DbMallocRawNN(db, sizeof(*p));
53323 if( p ){
53324 int N = sqlite3DbMallocSize(db, p);
53325 p->pChunk = 0;
53326 p->db = db;
53327 p->pEntry = 0;
53328 p->pLast = 0;
53329 p->pForest = 0;
53330 p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p))(((sizeof(*p))+7)&~7) + (char*)p);
53331 p->nFresh = (u16)((N - ROUND8(sizeof(*p))(((sizeof(*p))+7)&~7))/sizeof(struct RowSetEntry));
53332 p->rsFlags = ROWSET_SORTED0x01;
53333 p->iBatch = 0;
53334 }
53335 return p;
53336}
53337
53338/*
53339** Deallocate all chunks from a RowSet. This frees all memory that
53340** the RowSet has allocated over its lifetime. This routine is
53341** the destructor for the RowSet.
53342*/
53343SQLITE_PRIVATEstatic void sqlite3RowSetClear(void *pArg){
53344 RowSet *p = (RowSet*)pArg;
53345 struct RowSetChunk *pChunk, *pNextChunk;
53346 for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
53347 pNextChunk = pChunk->pNextChunk;
53348 sqlite3DbFree(p->db, pChunk);
53349 }
53350 p->pChunk = 0;
53351 p->nFresh = 0;
53352 p->pEntry = 0;
53353 p->pLast = 0;
53354 p->pForest = 0;
53355 p->rsFlags = ROWSET_SORTED0x01;
53356}
53357
53358/*
53359** Deallocate all chunks from a RowSet. This frees all memory that
53360** the RowSet has allocated over its lifetime. This routine is
53361** the destructor for the RowSet.
53362*/
53363SQLITE_PRIVATEstatic void sqlite3RowSetDelete(void *pArg){
53364 sqlite3RowSetClear(pArg);
53365 sqlite3DbFree(((RowSet*)pArg)->db, pArg);
53366}
53367
53368/*
53369** Allocate a new RowSetEntry object that is associated with the
53370** given RowSet. Return a pointer to the new and completely uninitialized
53371** object.
53372**
53373** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
53374** routine returns NULL.
53375*/
53376static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
53377 assert( p!=0 )((void) (0));
53378 if( p->nFresh==0 ){ /*OPTIMIZATION-IF-FALSE*/
53379 /* We could allocate a fresh RowSetEntry each time one is needed, but it
53380 ** is more efficient to pull a preallocated entry from the pool */
53381 struct RowSetChunk *pNew;
53382 pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
53383 if( pNew==0 ){
53384 return 0;
53385 }
53386 pNew->pNextChunk = p->pChunk;
53387 p->pChunk = pNew;
53388 p->pFresh = pNew->aEntry;
53389 p->nFresh = ROWSET_ENTRY_PER_CHUNK((1024 -8)/sizeof(struct RowSetEntry));
53390 }
53391 p->nFresh--;
53392 return p->pFresh++;
53393}
53394
53395/*
53396** Insert a new value into a RowSet.
53397**
53398** The mallocFailed flag of the database connection is set if a
53399** memory allocation fails.
53400*/
53401SQLITE_PRIVATEstatic void sqlite3RowSetInsert(RowSet *p, i64 rowid){
53402 struct RowSetEntry *pEntry; /* The new entry */
53403 struct RowSetEntry *pLast; /* The last prior entry */
53404
53405 /* This routine is never called after sqlite3RowSetNext() */
53406 assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 )((void) (0));
53407
53408 pEntry = rowSetEntryAlloc(p);
53409 if( pEntry==0 ) return;
53410 pEntry->v = rowid;
53411 pEntry->pRight = 0;
53412 pLast = p->pLast;
53413 if( pLast ){
53414 if( rowid<=pLast->v ){ /*OPTIMIZATION-IF-FALSE*/
53415 /* Avoid unnecessary sorts by preserving the ROWSET_SORTED flags
53416 ** where possible */
53417 p->rsFlags &= ~ROWSET_SORTED0x01;
53418 }
53419 pLast->pRight = pEntry;
53420 }else{
53421 p->pEntry = pEntry;
53422 }
53423 p->pLast = pEntry;
53424}
53425
53426/*
53427** Merge two lists of RowSetEntry objects. Remove duplicates.
53428**
53429** The input lists are connected via pRight pointers and are
53430** assumed to each already be in sorted order.
53431*/
53432static struct RowSetEntry *rowSetEntryMerge(
53433 struct RowSetEntry *pA, /* First sorted list to be merged */
53434 struct RowSetEntry *pB /* Second sorted list to be merged */
53435){
53436 struct RowSetEntry head;
53437 struct RowSetEntry *pTail;
53438
53439 pTail = &head;
53440 assert( pA!=0 && pB!=0 )((void) (0));
53441 for(;;){
53442 assert( pA->pRight==0 || pA->v<=pA->pRight->v )((void) (0));
53443 assert( pB->pRight==0 || pB->v<=pB->pRight->v )((void) (0));
53444 if( pA->v<=pB->v ){
53445 if( pA->v<pB->v ) pTail = pTail->pRight = pA;
53446 pA = pA->pRight;
53447 if( pA==0 ){
53448 pTail->pRight = pB;
53449 break;
53450 }
53451 }else{
53452 pTail = pTail->pRight = pB;
53453 pB = pB->pRight;
53454 if( pB==0 ){
53455 pTail->pRight = pA;
53456 break;
53457 }
53458 }
53459 }
53460 return head.pRight;
53461}
53462
53463/*
53464** Sort all elements on the list of RowSetEntry objects into order of
53465** increasing v.
53466*/
53467static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){
53468 unsigned int i;
53469 struct RowSetEntry *pNext, *aBucket[40];
53470
53471 memset(aBucket, 0, sizeof(aBucket));
53472 while( pIn ){
53473 pNext = pIn->pRight;
53474 pIn->pRight = 0;
53475 for(i=0; aBucket[i]; i++){
53476 pIn = rowSetEntryMerge(aBucket[i], pIn);
53477 aBucket[i] = 0;
53478 }
53479 aBucket[i] = pIn;
53480 pIn = pNext;
53481 }
53482 pIn = aBucket[0];
53483 for(i=1; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
53484 if( aBucket[i]==0 ) continue;
53485 pIn = pIn ? rowSetEntryMerge(pIn, aBucket[i]) : aBucket[i];
53486 }
53487 return pIn;
53488}
53489
53490
53491/*
53492** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
53493** Convert this tree into a linked list connected by the pRight pointers
53494** and return pointers to the first and last elements of the new list.
53495*/
53496static void rowSetTreeToList(
53497 struct RowSetEntry *pIn, /* Root of the input tree */
53498 struct RowSetEntry **ppFirst, /* Write head of the output list here */
53499 struct RowSetEntry **ppLast /* Write tail of the output list here */
53500){
53501 assert( pIn!=0 )((void) (0));
53502 if( pIn->pLeft ){
53503 struct RowSetEntry *p;
53504 rowSetTreeToList(pIn->pLeft, ppFirst, &p);
53505 p->pRight = pIn;
53506 }else{
53507 *ppFirst = pIn;
53508 }
53509 if( pIn->pRight ){
53510 rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
53511 }else{
53512 *ppLast = pIn;
53513 }
53514 assert( (*ppLast)->pRight==0 )((void) (0));
53515}
53516
53517
53518/*
53519** Convert a sorted list of elements (connected by pRight) into a binary
53520** tree with depth of iDepth. A depth of 1 means the tree contains a single
53521** node taken from the head of *ppList. A depth of 2 means a tree with
53522** three nodes. And so forth.
53523**
53524** Use as many entries from the input list as required and update the
53525** *ppList to point to the unused elements of the list. If the input
53526** list contains too few elements, then construct an incomplete tree
53527** and leave *ppList set to NULL.
53528**
53529** Return a pointer to the root of the constructed binary tree.
53530*/
53531static struct RowSetEntry *rowSetNDeepTree(
53532 struct RowSetEntry **ppList,
53533 int iDepth
53534){
53535 struct RowSetEntry *p; /* Root of the new tree */
53536 struct RowSetEntry *pLeft; /* Left subtree */
53537 if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
53538 /* Prevent unnecessary deep recursion when we run out of entries */
53539 return 0;
53540 }
53541 if( iDepth>1 ){ /*OPTIMIZATION-IF-TRUE*/
53542 /* This branch causes a *balanced* tree to be generated. A valid tree
53543 ** is still generated without this branch, but the tree is wildly
53544 ** unbalanced and inefficient. */
53545 pLeft = rowSetNDeepTree(ppList, iDepth-1);
53546 p = *ppList;
53547 if( p==0 ){ /*OPTIMIZATION-IF-FALSE*/
53548 /* It is safe to always return here, but the resulting tree
53549 ** would be unbalanced */
53550 return pLeft;
53551 }
53552 p->pLeft = pLeft;
53553 *ppList = p->pRight;
53554 p->pRight = rowSetNDeepTree(ppList, iDepth-1);
53555 }else{
53556 p = *ppList;
53557 *ppList = p->pRight;
53558 p->pLeft = p->pRight = 0;
53559 }
53560 return p;
53561}
53562
53563/*
53564** Convert a sorted list of elements into a binary tree. Make the tree
53565** as deep as it needs to be in order to contain the entire list.
53566*/
53567static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){
53568 int iDepth; /* Depth of the tree so far */
53569 struct RowSetEntry *p; /* Current tree root */
53570 struct RowSetEntry *pLeft; /* Left subtree */
53571
53572 assert( pList!=0 )((void) (0));
53573 p = pList;
53574 pList = p->pRight;
53575 p->pLeft = p->pRight = 0;
53576 for(iDepth=1; pList; iDepth++){
53577 pLeft = p;
53578 p = pList;
53579 pList = p->pRight;
53580 p->pLeft = pLeft;
53581 p->pRight = rowSetNDeepTree(&pList, iDepth);
53582 }
53583 return p;
53584}
53585
53586/*
53587** Extract the smallest element from the RowSet.
53588** Write the element into *pRowid. Return 1 on success. Return
53589** 0 if the RowSet is already empty.
53590**
53591** After this routine has been called, the sqlite3RowSetInsert()
53592** routine may not be called again.
53593**
53594** This routine may not be called after sqlite3RowSetTest() has
53595** been used. Older versions of RowSet allowed that, but as the
53596** capability was not used by the code generator, it was removed
53597** for code economy.
53598*/
53599SQLITE_PRIVATEstatic int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
53600 assert( p!=0 )((void) (0));
53601 assert( p->pForest==0 )((void) (0)); /* Cannot be used with sqlite3RowSetText() */
53602
53603 /* Merge the forest into a single sorted list on first call */
53604 if( (p->rsFlags & ROWSET_NEXT0x02)==0 ){ /*OPTIMIZATION-IF-FALSE*/
53605 if( (p->rsFlags & ROWSET_SORTED0x01)==0 ){ /*OPTIMIZATION-IF-FALSE*/
53606 p->pEntry = rowSetEntrySort(p->pEntry);
53607 }
53608 p->rsFlags |= ROWSET_SORTED0x01|ROWSET_NEXT0x02;
53609 }
53610
53611 /* Return the next entry on the list */
53612 if( p->pEntry ){
53613 *pRowid = p->pEntry->v;
53614 p->pEntry = p->pEntry->pRight;
53615 if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
53616 /* Free memory immediately, rather than waiting on sqlite3_finalize() */
53617 sqlite3RowSetClear(p);
53618 }
53619 return 1;
53620 }else{
53621 return 0;
53622 }
53623}
53624
53625/*
53626** Check to see if element iRowid was inserted into the rowset as
53627** part of any insert batch prior to iBatch. Return 1 or 0.
53628**
53629** If this is the first test of a new batch and if there exist entries
53630** on pRowSet->pEntry, then sort those entries into the forest at
53631** pRowSet->pForest so that they can be tested.
53632*/
53633SQLITE_PRIVATEstatic int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64 iRowid){
53634 struct RowSetEntry *p, *pTree;
53635
53636 /* This routine is never called after sqlite3RowSetNext() */
53637 assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 )((void) (0));
53638
53639 /* Sort entries into the forest on the first test of a new batch.
53640 ** To save unnecessary work, only do this when the batch number changes.
53641 */
53642 if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/
53643 p = pRowSet->pEntry;
53644 if( p ){
53645 struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
53646 if( (pRowSet->rsFlags & ROWSET_SORTED0x01)==0 ){ /*OPTIMIZATION-IF-FALSE*/
53647 /* Only sort the current set of entries if they need it */
53648 p = rowSetEntrySort(p);
53649 }
53650 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
53651 ppPrevTree = &pTree->pRight;
53652 if( pTree->pLeft==0 ){
53653 pTree->pLeft = rowSetListToTree(p);
53654 break;
53655 }else{
53656 struct RowSetEntry *pAux, *pTail;
53657 rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
53658 pTree->pLeft = 0;
53659 p = rowSetEntryMerge(pAux, p);
53660 }
53661 }
53662 if( pTree==0 ){
53663 *ppPrevTree = pTree = rowSetEntryAlloc(pRowSet);
53664 if( pTree ){
53665 pTree->v = 0;
53666 pTree->pRight = 0;
53667 pTree->pLeft = rowSetListToTree(p);
53668 }
53669 }
53670 pRowSet->pEntry = 0;
53671 pRowSet->pLast = 0;
53672 pRowSet->rsFlags |= ROWSET_SORTED0x01;
53673 }
53674 pRowSet->iBatch = iBatch;
53675 }
53676
53677 /* Test to see if the iRowid value appears anywhere in the forest.
53678 ** Return 1 if it does and 0 if not.
53679 */
53680 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
53681 p = pTree->pLeft;
53682 while( p ){
53683 if( p->v<iRowid ){
53684 p = p->pRight;
53685 }else if( p->v>iRowid ){
53686 p = p->pLeft;
53687 }else{
53688 return 1;
53689 }
53690 }
53691 }
53692 return 0;
53693}
53694
53695/************** End of rowset.c **********************************************/
53696/************** Begin file pager.c *******************************************/
53697/*
53698** 2001 September 15
53699**
53700** The author disclaims copyright to this source code. In place of
53701** a legal notice, here is a blessing:
53702**
53703** May you do good and not evil.
53704** May you find forgiveness for yourself and forgive others.
53705** May you share freely, never taking more than you give.
53706**
53707*************************************************************************
53708** This is the implementation of the page cache subsystem or "pager".
53709**
53710** The pager is used to access a database disk file. It implements
53711** atomic commit and rollback through the use of a journal file that
53712** is separate from the database file. The pager also implements file
53713** locking to prevent two processes from writing the same database
53714** file simultaneously, or one process from reading the database while
53715** another is writing.
53716*/
53717#ifndef SQLITE_OMIT_DISKIO
53718/* #include "sqliteInt.h" */
53719/************** Include wal.h in the middle of pager.c ***********************/
53720/************** Begin file wal.h *********************************************/
53721/*
53722** 2010 February 1
53723**
53724** The author disclaims copyright to this source code. In place of
53725** a legal notice, here is a blessing:
53726**
53727** May you do good and not evil.
53728** May you find forgiveness for yourself and forgive others.
53729** May you share freely, never taking more than you give.
53730**
53731*************************************************************************
53732** This header file defines the interface to the write-ahead logging
53733** system. Refer to the comments below and the header comment attached to
53734** the implementation of each function in log.c for further details.
53735*/
53736
53737#ifndef SQLITE_WAL_H
53738#define SQLITE_WAL_H
53739
53740/* #include "sqliteInt.h" */
53741
53742/* Macros for extracting appropriate sync flags for either transaction
53743** commits (WAL_SYNC_FLAGS(X)) or for checkpoint ops (CKPT_SYNC_FLAGS(X)):
53744*/
53745#define WAL_SYNC_FLAGS(X)((X)&0x03) ((X)&0x03)
53746#define CKPT_SYNC_FLAGS(X)(((X)>>2)&0x03) (((X)>>2)&0x03)
53747
53748#ifdef SQLITE_OMIT_WAL
53749# define sqlite3WalOpen(x,y,z) 0
53750# define sqlite3WalLimit(x,y)
53751# define sqlite3WalClose(v,w,x,y,z) 0
53752# define sqlite3WalBeginReadTransaction(y,z) 0
53753# define sqlite3WalEndReadTransaction(z)
53754# define sqlite3WalDbsize(y) 0
53755# define sqlite3WalBeginWriteTransaction(y) 0
53756# define sqlite3WalEndWriteTransaction(x) 0
53757# define sqlite3WalUndo(x,y,z) 0
53758# define sqlite3WalSavepoint(y,z)
53759# define sqlite3WalSavepointUndo(y,z) 0
53760# define sqlite3WalFrames(u,v,w,x,y,z) 0
53761# define sqlite3WalCheckpoint(q,r,s,t,u,v,w,x,y,z) 0
53762# define sqlite3WalCallback(z) 0
53763# define sqlite3WalExclusiveMode(y,z) 0
53764# define sqlite3WalHeapMemory(z) 0
53765# define sqlite3WalFramesize(z) 0
53766# define sqlite3WalFindFrame(x,y,z) 0
53767# define sqlite3WalFile(x) 0
53768#else
53769
53770#define WAL_SAVEPOINT_NDATA4 4
53771
53772/* Connection to a write-ahead log (WAL) file.
53773** There is one object of this type for each pager.
53774*/
53775typedef struct Wal Wal;
53776
53777/* Open and close a connection to a write-ahead log. */
53778SQLITE_PRIVATEstatic int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**);
53779SQLITE_PRIVATEstatic int sqlite3WalClose(Wal *pWal, sqlite3*, int sync_flags, int, u8 *);
53780
53781/* Set the limiting size of a WAL file. */
53782SQLITE_PRIVATEstatic void sqlite3WalLimit(Wal*, i64);
53783
53784/* Used by readers to open (lock) and close (unlock) a snapshot. A
53785** snapshot is like a read-transaction. It is the state of the database
53786** at an instant in time. sqlite3WalOpenSnapshot gets a read lock and
53787** preserves the current state even if the other threads or processes
53788** write to or checkpoint the WAL. sqlite3WalCloseSnapshot() closes the
53789** transaction and releases the lock.
53790*/
53791SQLITE_PRIVATEstatic int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
53792SQLITE_PRIVATEstatic void sqlite3WalEndReadTransaction(Wal *pWal);
53793
53794/* Read a page from the write-ahead log, if it is present. */
53795SQLITE_PRIVATEstatic int sqlite3WalFindFrame(Wal *, Pgno, u32 *);
53796SQLITE_PRIVATEstatic int sqlite3WalReadFrame(Wal *, u32, int, u8 *);
53797
53798/* If the WAL is not empty, return the size of the database. */
53799SQLITE_PRIVATEstatic Pgno sqlite3WalDbsize(Wal *pWal);
53800
53801/* Obtain or release the WRITER lock. */
53802SQLITE_PRIVATEstatic int sqlite3WalBeginWriteTransaction(Wal *pWal);
53803SQLITE_PRIVATEstatic int sqlite3WalEndWriteTransaction(Wal *pWal);
53804
53805/* Undo any frames written (but not committed) to the log */
53806SQLITE_PRIVATEstatic int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx);
53807
53808/* Return an integer that records the current (uncommitted) write
53809** position in the WAL */
53810SQLITE_PRIVATEstatic void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData);
53811
53812/* Move the write position of the WAL back to iFrame. Called in
53813** response to a ROLLBACK TO command. */
53814SQLITE_PRIVATEstatic int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData);
53815
53816/* Write a frame or frames to the log. */
53817SQLITE_PRIVATEstatic int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
53818
53819/* Copy pages from the log to the database file */
53820SQLITE_PRIVATEstatic int sqlite3WalCheckpoint(
53821 Wal *pWal, /* Write-ahead log connection */
53822 sqlite3 *db, /* Check this handle's interrupt flag */
53823 int eMode, /* One of PASSIVE, FULL and RESTART */
53824 int (*xBusy)(void*), /* Function to call when busy */
53825 void *pBusyArg, /* Context argument for xBusyHandler */
53826 int sync_flags, /* Flags to sync db file with (or 0) */
53827 int nBuf, /* Size of buffer nBuf */
53828 u8 *zBuf, /* Temporary buffer to use */
53829 int *pnLog, /* OUT: Number of frames in WAL */
53830 int *pnCkpt /* OUT: Number of backfilled frames in WAL */
53831);
53832
53833/* Return the value to pass to a sqlite3_wal_hook callback, the
53834** number of frames in the WAL at the point of the last commit since
53835** sqlite3WalCallback() was called. If no commits have occurred since
53836** the last call, then return 0.
53837*/
53838SQLITE_PRIVATEstatic int sqlite3WalCallback(Wal *pWal);
53839
53840/* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released)
53841** by the pager layer on the database file.
53842*/
53843SQLITE_PRIVATEstatic int sqlite3WalExclusiveMode(Wal *pWal, int op);
53844
53845/* Return true if the argument is non-NULL and the WAL module is using
53846** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
53847** WAL module is using shared-memory, return false.
53848*/
53849SQLITE_PRIVATEstatic int sqlite3WalHeapMemory(Wal *pWal);
53850
53851#ifdef SQLITE_ENABLE_SNAPSHOT1
53852SQLITE_PRIVATEstatic int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot);
53853SQLITE_PRIVATEstatic void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot);
53854SQLITE_PRIVATEstatic int sqlite3WalSnapshotRecover(Wal *pWal);
53855SQLITE_PRIVATEstatic int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot);
53856SQLITE_PRIVATEstatic void sqlite3WalSnapshotUnlock(Wal *pWal);
53857#endif
53858
53859#ifdef SQLITE_ENABLE_ZIPVFS
53860/* If the WAL file is not empty, return the number of bytes of content
53861** stored in each frame (i.e. the db page-size when the WAL was created).
53862*/
53863SQLITE_PRIVATEstatic int sqlite3WalFramesize(Wal *pWal);
53864#endif
53865
53866/* Return the sqlite3_file object for the WAL file */
53867SQLITE_PRIVATEstatic sqlite3_file *sqlite3WalFile(Wal *pWal);
53868
53869#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
53870SQLITE_PRIVATEstatic int sqlite3WalWriteLock(Wal *pWal, int bLock);
53871SQLITE_PRIVATEstatic void sqlite3WalDb(Wal *pWal, sqlite3 *db);
53872#endif
53873
53874#endif /* ifndef SQLITE_OMIT_WAL */
53875#endif /* SQLITE_WAL_H */
53876
53877/************** End of wal.h *************************************************/
53878/************** Continuing where we left off in pager.c **********************/
53879
53880
53881/******************* NOTES ON THE DESIGN OF THE PAGER ************************
53882**
53883** This comment block describes invariants that hold when using a rollback
53884** journal. These invariants do not apply for journal_mode=WAL,
53885** journal_mode=MEMORY, or journal_mode=OFF.
53886**
53887** Within this comment block, a page is deemed to have been synced
53888** automatically as soon as it is written when PRAGMA synchronous=OFF.
53889** Otherwise, the page is not synced until the xSync method of the VFS
53890** is called successfully on the file containing the page.
53891**
53892** Definition: A page of the database file is said to be "overwriteable" if
53893** one or more of the following are true about the page:
53894**
53895** (a) The original content of the page as it was at the beginning of
53896** the transaction has been written into the rollback journal and
53897** synced.
53898**
53899** (b) The page was a freelist leaf page at the start of the transaction.
53900**
53901** (c) The page number is greater than the largest page that existed in
53902** the database file at the start of the transaction.
53903**
53904** (1) A page of the database file is never overwritten unless one of the
53905** following are true:
53906**
53907** (a) The page and all other pages on the same sector are overwriteable.
53908**
53909** (b) The atomic page write optimization is enabled, and the entire
53910** transaction other than the update of the transaction sequence
53911** number consists of a single page change.
53912**
53913** (2) The content of a page written into the rollback journal exactly matches
53914** both the content in the database when the rollback journal was written
53915** and the content in the database at the beginning of the current
53916** transaction.
53917**
53918** (3) Writes to the database file are an integer multiple of the page size
53919** in length and are aligned on a page boundary.
53920**
53921** (4) Reads from the database file are either aligned on a page boundary and
53922** an integer multiple of the page size in length or are taken from the
53923** first 100 bytes of the database file.
53924**
53925** (5) All writes to the database file are synced prior to the rollback journal
53926** being deleted, truncated, or zeroed.
53927**
53928** (6) If a super-journal file is used, then all writes to the database file
53929** are synced prior to the super-journal being deleted.
53930**
53931** Definition: Two databases (or the same database at two points it time)
53932** are said to be "logically equivalent" if they give the same answer to
53933** all queries. Note in particular the content of freelist leaf
53934** pages can be changed arbitrarily without affecting the logical equivalence
53935** of the database.
53936**
53937** (7) At any time, if any subset, including the empty set and the total set,
53938** of the unsynced changes to a rollback journal are removed and the
53939** journal is rolled back, the resulting database file will be logically
53940** equivalent to the database file at the beginning of the transaction.
53941**
53942** (8) When a transaction is rolled back, the xTruncate method of the VFS
53943** is called to restore the database file to the same size it was at
53944** the beginning of the transaction. (In some VFSes, the xTruncate
53945** method is a no-op, but that does not change the fact the SQLite will
53946** invoke it.)
53947**
53948** (9) Whenever the database file is modified, at least one bit in the range
53949** of bytes from 24 through 39 inclusive will be changed prior to releasing
53950** the EXCLUSIVE lock, thus signaling other connections on the same
53951** database to flush their caches.
53952**
53953** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
53954** than one billion transactions.
53955**
53956** (11) A database file is well-formed at the beginning and at the conclusion
53957** of every transaction.
53958**
53959** (12) An EXCLUSIVE lock is held on the database file when writing to
53960** the database file.
53961**
53962** (13) A SHARED lock is held on the database file while reading any
53963** content out of the database file.
53964**
53965******************************************************************************/
53966
53967/*
53968** Macros for troubleshooting. Normally turned off
53969*/
53970#if 0
53971int sqlite3PagerTrace=1; /* True to enable tracing */
53972#define sqlite3DebugPrintf printf
53973#define PAGERTRACE(X) if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; }
53974#else
53975#define PAGERTRACE(X)
53976#endif
53977
53978/*
53979** The following two macros are used within the PAGERTRACE() macros above
53980** to print out file-descriptors.
53981**
53982** PAGERID() takes a pointer to a Pager struct as its argument. The
53983** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
53984** struct as its argument.
53985*/
53986#define PAGERID(p)(((int)(long int)(p->fd))) (SQLITE_PTR_TO_INT(p->fd)((int)(long int)(p->fd)))
53987#define FILEHANDLEID(fd)(((int)(long int)(fd))) (SQLITE_PTR_TO_INT(fd)((int)(long int)(fd)))
53988
53989/*
53990** The Pager.eState variable stores the current 'state' of a pager. A
53991** pager may be in any one of the seven states shown in the following
53992** state diagram.
53993**
53994** OPEN <------+------+
53995** | | |
53996** V | |
53997** +---------> READER-------+ |
53998** | | |
53999** | V |
54000** |<-------WRITER_LOCKED------> ERROR
54001** | | ^
54002** | V |
54003** |<------WRITER_CACHEMOD-------->|
54004** | | |
54005** | V |
54006** |<-------WRITER_DBMOD---------->|
54007** | | |
54008** | V |
54009** +<------WRITER_FINISHED-------->+
54010**
54011**
54012** List of state transitions and the C [function] that performs each:
54013**
54014** OPEN -> READER [sqlite3PagerSharedLock]
54015** READER -> OPEN [pager_unlock]
54016**
54017** READER -> WRITER_LOCKED [sqlite3PagerBegin]
54018** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal]
54019** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal]
54020** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne]
54021** WRITER_*** -> READER [pager_end_transaction]
54022**
54023** WRITER_*** -> ERROR [pager_error]
54024** ERROR -> OPEN [pager_unlock]
54025**
54026**
54027** OPEN:
54028**
54029** The pager starts up in this state. Nothing is guaranteed in this
54030** state - the file may or may not be locked and the database size is
54031** unknown. The database may not be read or written.
54032**
54033** * No read or write transaction is active.
54034** * Any lock, or no lock at all, may be held on the database file.
54035** * The dbSize, dbOrigSize and dbFileSize variables may not be trusted.
54036**
54037** READER:
54038**
54039** In this state all the requirements for reading the database in
54040** rollback (non-WAL) mode are met. Unless the pager is (or recently
54041** was) in exclusive-locking mode, a user-level read transaction is
54042** open. The database size is known in this state.
54043**
54044** A connection running with locking_mode=normal enters this state when
54045** it opens a read-transaction on the database and returns to state
54046** OPEN after the read-transaction is completed. However a connection
54047** running in locking_mode=exclusive (including temp databases) remains in
54048** this state even after the read-transaction is closed. The only way
54049** a locking_mode=exclusive connection can transition from READER to OPEN
54050** is via the ERROR state (see below).
54051**
54052** * A read transaction may be active (but a write-transaction cannot).
54053** * A SHARED or greater lock is held on the database file.
54054** * The dbSize variable may be trusted (even if a user-level read
54055** transaction is not active). The dbOrigSize and dbFileSize variables
54056** may not be trusted at this point.
54057** * If the database is a WAL database, then the WAL connection is open.
54058** * Even if a read-transaction is not open, it is guaranteed that
54059** there is no hot-journal in the file-system.
54060**
54061** WRITER_LOCKED:
54062**
54063** The pager moves to this state from READER when a write-transaction
54064** is first opened on the database. In WRITER_LOCKED state, all locks
54065** required to start a write-transaction are held, but no actual
54066** modifications to the cache or database have taken place.
54067**
54068** In rollback mode, a RESERVED or (if the transaction was opened with
54069** BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when
54070** moving to this state, but the journal file is not written to or opened
54071** to in this state. If the transaction is committed or rolled back while
54072** in WRITER_LOCKED state, all that is required is to unlock the database
54073** file.
54074**
54075** IN WAL mode, WalBeginWriteTransaction() is called to lock the log file.
54076** If the connection is running with locking_mode=exclusive, an attempt
54077** is made to obtain an EXCLUSIVE lock on the database file.
54078**
54079** * A write transaction is active.
54080** * If the connection is open in rollback-mode, a RESERVED or greater
54081** lock is held on the database file.
54082** * If the connection is open in WAL-mode, a WAL write transaction
54083** is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully
54084** called).
54085** * The dbSize, dbOrigSize and dbFileSize variables are all valid.
54086** * The contents of the pager cache have not been modified.
54087** * The journal file may or may not be open.
54088** * Nothing (not even the first header) has been written to the journal.
54089**
54090** WRITER_CACHEMOD:
54091**
54092** A pager moves from WRITER_LOCKED state to this state when a page is
54093** first modified by the upper layer. In rollback mode the journal file
54094** is opened (if it is not already open) and a header written to the
54095** start of it. The database file on disk has not been modified.
54096**
54097** * A write transaction is active.
54098** * A RESERVED or greater lock is held on the database file.
54099** * The journal file is open and the first header has been written
54100** to it, but the header has not been synced to disk.
54101** * The contents of the page cache have been modified.
54102**
54103** WRITER_DBMOD:
54104**
54105** The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state
54106** when it modifies the contents of the database file. WAL connections
54107** never enter this state (since they do not modify the database file,
54108** just the log file).
54109**
54110** * A write transaction is active.
54111** * An EXCLUSIVE or greater lock is held on the database file.
54112** * The journal file is open and the first header has been written
54113** and synced to disk.
54114** * The contents of the page cache have been modified (and possibly
54115** written to disk).
54116**
54117** WRITER_FINISHED:
54118**
54119** It is not possible for a WAL connection to enter this state.
54120**
54121** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
54122** state after the entire transaction has been successfully written into the
54123** database file. In this state the transaction may be committed simply
54124** by finalizing the journal file. Once in WRITER_FINISHED state, it is
54125** not possible to modify the database further. At this point, the upper
54126** layer must either commit or rollback the transaction.
54127**
54128** * A write transaction is active.
54129** * An EXCLUSIVE or greater lock is held on the database file.
54130** * All writing and syncing of journal and database data has finished.
54131** If no error occurred, all that remains is to finalize the journal to
54132** commit the transaction. If an error did occur, the caller will need
54133** to rollback the transaction.
54134**
54135** ERROR:
54136**
54137** The ERROR state is entered when an IO or disk-full error (including
54138** SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it
54139** difficult to be sure that the in-memory pager state (cache contents,
54140** db size etc.) are consistent with the contents of the file-system.
54141**
54142** Temporary pager files may enter the ERROR state, but in-memory pagers
54143** cannot.
54144**
54145** For example, if an IO error occurs while performing a rollback,
54146** the contents of the page-cache may be left in an inconsistent state.
54147** At this point it would be dangerous to change back to READER state
54148** (as usually happens after a rollback). Any subsequent readers might
54149** report database corruption (due to the inconsistent cache), and if
54150** they upgrade to writers, they may inadvertently corrupt the database
54151** file. To avoid this hazard, the pager switches into the ERROR state
54152** instead of READER following such an error.
54153**
54154** Once it has entered the ERROR state, any attempt to use the pager
54155** to read or write data returns an error. Eventually, once all
54156** outstanding transactions have been abandoned, the pager is able to
54157** transition back to OPEN state, discarding the contents of the
54158** page-cache and any other in-memory state at the same time. Everything
54159** is reloaded from disk (and, if necessary, hot-journal rollback peformed)
54160** when a read-transaction is next opened on the pager (transitioning
54161** the pager into READER state). At that point the system has recovered
54162** from the error.
54163**
54164** Specifically, the pager jumps into the ERROR state if:
54165**
54166** 1. An error occurs while attempting a rollback. This happens in
54167** function sqlite3PagerRollback().
54168**
54169** 2. An error occurs while attempting to finalize a journal file
54170** following a commit in function sqlite3PagerCommitPhaseTwo().
54171**
54172** 3. An error occurs while attempting to write to the journal or
54173** database file in function pagerStress() in order to free up
54174** memory.
54175**
54176** In other cases, the error is returned to the b-tree layer. The b-tree
54177** layer then attempts a rollback operation. If the error condition
54178** persists, the pager enters the ERROR state via condition (1) above.
54179**
54180** Condition (3) is necessary because it can be triggered by a read-only
54181** statement executed within a transaction. In this case, if the error
54182** code were simply returned to the user, the b-tree layer would not
54183** automatically attempt a rollback, as it assumes that an error in a
54184** read-only statement cannot leave the pager in an internally inconsistent
54185** state.
54186**
54187** * The Pager.errCode variable is set to something other than SQLITE_OK.
54188** * There are one or more outstanding references to pages (after the
54189** last reference is dropped the pager should move back to OPEN state).
54190** * The pager is not an in-memory pager.
54191**
54192**
54193** Notes:
54194**
54195** * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the
54196** connection is open in WAL mode. A WAL connection is always in one
54197** of the first four states.
54198**
54199** * Normally, a connection open in exclusive mode is never in PAGER_OPEN
54200** state. There are two exceptions: immediately after exclusive-mode has
54201** been turned on (and before any read or write transactions are
54202** executed), and when the pager is leaving the "error state".
54203**
54204** * See also: assert_pager_state().
54205*/
54206#define PAGER_OPEN0 0
54207#define PAGER_READER1 1
54208#define PAGER_WRITER_LOCKED2 2
54209#define PAGER_WRITER_CACHEMOD3 3
54210#define PAGER_WRITER_DBMOD4 4
54211#define PAGER_WRITER_FINISHED5 5
54212#define PAGER_ERROR6 6
54213
54214/*
54215** The Pager.eLock variable is almost always set to one of the
54216** following locking-states, according to the lock currently held on
54217** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
54218** This variable is kept up to date as locks are taken and released by
54219** the pagerLockDb() and pagerUnlockDb() wrappers.
54220**
54221** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY
54222** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not
54223** the operation was successful. In these circumstances pagerLockDb() and
54224** pagerUnlockDb() take a conservative approach - eLock is always updated
54225** when unlocking the file, and only updated when locking the file if the
54226** VFS call is successful. This way, the Pager.eLock variable may be set
54227** to a less exclusive (lower) value than the lock that is actually held
54228** at the system level, but it is never set to a more exclusive value.
54229**
54230** This is usually safe. If an xUnlock fails or appears to fail, there may
54231** be a few redundant xLock() calls or a lock may be held for longer than
54232** required, but nothing really goes wrong.
54233**
54234** The exception is when the database file is unlocked as the pager moves
54235** from ERROR to OPEN state. At this point there may be a hot-journal file
54236** in the file-system that needs to be rolled back (as part of an OPEN->SHARED
54237** transition, by the same pager or any other). If the call to xUnlock()
54238** fails at this point and the pager is left holding an EXCLUSIVE lock, this
54239** can confuse the call to xCheckReservedLock() call made later as part
54240** of hot-journal detection.
54241**
54242** xCheckReservedLock() is defined as returning true "if there is a RESERVED
54243** lock held by this process or any others". So xCheckReservedLock may
54244** return true because the caller itself is holding an EXCLUSIVE lock (but
54245** doesn't know it because of a previous error in xUnlock). If this happens
54246** a hot-journal may be mistaken for a journal being created by an active
54247** transaction in another process, causing SQLite to read from the database
54248** without rolling it back.
54249**
54250** To work around this, if a call to xUnlock() fails when unlocking the
54251** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It
54252** is only changed back to a real locking state after a successful call
54253** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
54254** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
54255** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
54256** lock on the database file before attempting to roll it back. See function
54257** PagerSharedLock() for more detail.
54258**
54259** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in
54260** PAGER_OPEN state.
54261*/
54262#define UNKNOWN_LOCK(4 +1) (EXCLUSIVE_LOCK4+1)
54263
54264/*
54265** The maximum allowed sector size. 64KiB. If the xSectorsize() method
54266** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
54267** This could conceivably cause corruption following a power failure on
54268** such a system. This is currently an undocumented limit.
54269*/
54270#define MAX_SECTOR_SIZE0x10000 0x10000
54271
54272
54273/*
54274** An instance of the following structure is allocated for each active
54275** savepoint and statement transaction in the system. All such structures
54276** are stored in the Pager.aSavepoint[] array, which is allocated and
54277** resized using sqlite3Realloc().
54278**
54279** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
54280** set to 0. If a journal-header is written into the main journal while
54281** the savepoint is active, then iHdrOffset is set to the byte offset
54282** immediately following the last journal record written into the main
54283** journal before the journal-header. This is required during savepoint
54284** rollback (see pagerPlaybackSavepoint()).
54285*/
54286typedef struct PagerSavepoint PagerSavepoint;
54287struct PagerSavepoint {
54288 i64 iOffset; /* Starting offset in main journal */
54289 i64 iHdrOffset; /* See above */
54290 Bitvec *pInSavepoint; /* Set of pages in this savepoint */
54291 Pgno nOrig; /* Original number of pages in file */
54292 Pgno iSubRec; /* Index of first record in sub-journal */
54293 int bTruncateOnRelease; /* If stmt journal may be truncated on RELEASE */
54294#ifndef SQLITE_OMIT_WAL
54295 u32 aWalData[WAL_SAVEPOINT_NDATA4]; /* WAL savepoint context */
54296#endif
54297};
54298
54299/*
54300** Bits of the Pager.doNotSpill flag. See further description below.
54301*/
54302#define SPILLFLAG_OFF0x01 0x01 /* Never spill cache. Set via pragma */
54303#define SPILLFLAG_ROLLBACK0x02 0x02 /* Current rolling back, so do not spill */
54304#define SPILLFLAG_NOSYNC0x04 0x04 /* Spill is ok, but do not sync */
54305
54306/*
54307** An open page cache is an instance of struct Pager. A description of
54308** some of the more important member variables follows:
54309**
54310** eState
54311**
54312** The current 'state' of the pager object. See the comment and state
54313** diagram above for a description of the pager state.
54314**
54315** eLock
54316**
54317** For a real on-disk database, the current lock held on the database file -
54318** NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
54319**
54320** For a temporary or in-memory database (neither of which require any
54321** locks), this variable is always set to EXCLUSIVE_LOCK. Since such
54322** databases always have Pager.exclusiveMode==1, this tricks the pager
54323** logic into thinking that it already has all the locks it will ever
54324** need (and no reason to release them).
54325**
54326** In some (obscure) circumstances, this variable may also be set to
54327** UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for
54328** details.
54329**
54330** changeCountDone
54331**
54332** This boolean variable is used to make sure that the change-counter
54333** (the 4-byte header field at byte offset 24 of the database file) is
54334** not updated more often than necessary.
54335**
54336** It is set to true when the change-counter field is updated, which
54337** can only happen if an exclusive lock is held on the database file.
54338** It is cleared (set to false) whenever an exclusive lock is
54339** relinquished on the database file. Each time a transaction is committed,
54340** The changeCountDone flag is inspected. If it is true, the work of
54341** updating the change-counter is omitted for the current transaction.
54342**
54343** This mechanism means that when running in exclusive mode, a connection
54344** need only update the change-counter once, for the first transaction
54345** committed.
54346**
54347** setSuper
54348**
54349** When PagerCommitPhaseOne() is called to commit a transaction, it may
54350** (or may not) specify a super-journal name to be written into the
54351** journal file before it is synced to disk.
54352**
54353** Whether or not a journal file contains a super-journal pointer affects
54354** the way in which the journal file is finalized after the transaction is
54355** committed or rolled back when running in "journal_mode=PERSIST" mode.
54356** If a journal file does not contain a super-journal pointer, it is
54357** finalized by overwriting the first journal header with zeroes. If
54358** it does contain a super-journal pointer the journal file is finalized
54359** by truncating it to zero bytes, just as if the connection were
54360** running in "journal_mode=truncate" mode.
54361**
54362** Journal files that contain super-journal pointers cannot be finalized
54363** simply by overwriting the first journal-header with zeroes, as the
54364** super-journal pointer could interfere with hot-journal rollback of any
54365** subsequently interrupted transaction that reuses the journal file.
54366**
54367** The flag is cleared as soon as the journal file is finalized (either
54368** by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
54369** journal file from being successfully finalized, the setSuper flag
54370** is cleared anyway (and the pager will move to ERROR state).
54371**
54372** doNotSpill
54373**
54374** This variables control the behavior of cache-spills (calls made by
54375** the pcache module to the pagerStress() routine to write cached data
54376** to the file-system in order to free up memory).
54377**
54378** When bits SPILLFLAG_OFF or SPILLFLAG_ROLLBACK of doNotSpill are set,
54379** writing to the database from pagerStress() is disabled altogether.
54380** The SPILLFLAG_ROLLBACK case is done in a very obscure case that
54381** comes up during savepoint rollback that requires the pcache module
54382** to allocate a new page to prevent the journal file from being written
54383** while it is being traversed by code in pager_playback(). The SPILLFLAG_OFF
54384** case is a user preference.
54385**
54386** If the SPILLFLAG_NOSYNC bit is set, writing to the database from
54387** pagerStress() is permitted, but syncing the journal file is not.
54388** This flag is set by sqlite3PagerWrite() when the file-system sector-size
54389** is larger than the database page-size in order to prevent a journal sync
54390** from happening in between the journalling of two pages on the same sector.
54391**
54392** subjInMemory
54393**
54394** This is a boolean variable. If true, then any required sub-journal
54395** is opened as an in-memory journal file. If false, then in-memory
54396** sub-journals are only used for in-memory pager files.
54397**
54398** This variable is updated by the upper layer each time a new
54399** write-transaction is opened.
54400**
54401** dbSize, dbOrigSize, dbFileSize
54402**
54403** Variable dbSize is set to the number of pages in the database file.
54404** It is valid in PAGER_READER and higher states (all states except for
54405** OPEN and ERROR).
54406**
54407** dbSize is set based on the size of the database file, which may be
54408** larger than the size of the database (the value stored at offset
54409** 28 of the database header by the btree). If the size of the file
54410** is not an integer multiple of the page-size, the value stored in
54411** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
54412** Except, any file that is greater than 0 bytes in size is considered
54413** to have at least one page. (i.e. a 1KB file with 2K page-size leads
54414** to dbSize==1).
54415**
54416** During a write-transaction, if pages with page-numbers greater than
54417** dbSize are modified in the cache, dbSize is updated accordingly.
54418** Similarly, if the database is truncated using PagerTruncateImage(),
54419** dbSize is updated.
54420**
54421** Variables dbOrigSize and dbFileSize are valid in states
54422** PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize
54423** variable at the start of the transaction. It is used during rollback,
54424** and to determine whether or not pages need to be journalled before
54425** being modified.
54426**
54427** Throughout a write-transaction, dbFileSize contains the size of
54428** the file on disk in pages. It is set to a copy of dbSize when the
54429** write-transaction is first opened, and updated when VFS calls are made
54430** to write or truncate the database file on disk.
54431**
54432** The only reason the dbFileSize variable is required is to suppress
54433** unnecessary calls to xTruncate() after committing a transaction. If,
54434** when a transaction is committed, the dbFileSize variable indicates
54435** that the database file is larger than the database image (Pager.dbSize),
54436** pager_truncate() is called. The pager_truncate() call uses xFilesize()
54437** to measure the database file on disk, and then truncates it if required.
54438** dbFileSize is not used when rolling back a transaction. In this case
54439** pager_truncate() is called unconditionally (which means there may be
54440** a call to xFilesize() that is not strictly required). In either case,
54441** pager_truncate() may cause the file to become smaller or larger.
54442**
54443** dbHintSize
54444**
54445** The dbHintSize variable is used to limit the number of calls made to
54446** the VFS xFileControl(FCNTL_SIZE_HINT) method.
54447**
54448** dbHintSize is set to a copy of the dbSize variable when a
54449** write-transaction is opened (at the same time as dbFileSize and
54450** dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called,
54451** dbHintSize is increased to the number of pages that correspond to the
54452** size-hint passed to the method call. See pager_write_pagelist() for
54453** details.
54454**
54455** errCode
54456**
54457** The Pager.errCode variable is only ever used in PAGER_ERROR state. It
54458** is set to zero in all other states. In PAGER_ERROR state, Pager.errCode
54459** is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX
54460** sub-codes.
54461**
54462** syncFlags, walSyncFlags
54463**
54464** syncFlags is either SQLITE_SYNC_NORMAL (0x02) or SQLITE_SYNC_FULL (0x03).
54465** syncFlags is used for rollback mode. walSyncFlags is used for WAL mode
54466** and contains the flags used to sync the checkpoint operations in the
54467** lower two bits, and sync flags used for transaction commits in the WAL
54468** file in bits 0x04 and 0x08. In other words, to get the correct sync flags
54469** for checkpoint operations, use (walSyncFlags&0x03) and to get the correct
54470** sync flags for transaction commit, use ((walSyncFlags>>2)&0x03). Note
54471** that with synchronous=NORMAL in WAL mode, transaction commit is not synced
54472** meaning that the 0x04 and 0x08 bits are both zero.
54473*/
54474struct Pager {
54475 sqlite3_vfs *pVfs; /* OS functions to use for IO */
54476 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
54477 u8 journalMode; /* One of the PAGER_JOURNALMODE_* values */
54478 u8 useJournal; /* Use a rollback journal on this file */
54479 u8 noSync; /* Do not sync the journal if true */
54480 u8 fullSync; /* Do extra syncs of the journal for robustness */
54481 u8 extraSync; /* sync directory after journal delete */
54482 u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */
54483 u8 walSyncFlags; /* See description above */
54484 u8 tempFile; /* zFilename is a temporary or immutable file */
54485 u8 noLock; /* Do not lock (except in WAL mode) */
54486 u8 readOnly; /* True for a read-only database */
54487 u8 memDb; /* True to inhibit all file I/O */
54488 u8 memVfs; /* VFS-implemented memory database */
54489
54490 /**************************************************************************
54491 ** The following block contains those class members that change during
54492 ** routine operation. Class members not in this block are either fixed
54493 ** when the pager is first created or else only change when there is a
54494 ** significant mode change (such as changing the page_size, locking_mode,
54495 ** or the journal_mode). From another view, these class members describe
54496 ** the "state" of the pager, while other class members describe the
54497 ** "configuration" of the pager.
54498 */
54499 u8 eState; /* Pager state (OPEN, READER, WRITER_LOCKED..) */
54500 u8 eLock; /* Current lock held on database file */
54501 u8 changeCountDone; /* Set after incrementing the change-counter */
54502 u8 setSuper; /* Super-jrnl name is written into jrnl */
54503 u8 doNotSpill; /* Do not spill the cache when non-zero */
54504 u8 subjInMemory; /* True to use in-memory sub-journals */
54505 u8 bUseFetch; /* True to use xFetch() */
54506 u8 hasHeldSharedLock; /* True if a shared lock has ever been held */
54507 Pgno dbSize; /* Number of pages in the database */
54508 Pgno dbOrigSize; /* dbSize before the current transaction */
54509 Pgno dbFileSize; /* Number of pages in the database file */
54510 Pgno dbHintSize; /* Value passed to FCNTL_SIZE_HINT call */
54511 int errCode; /* One of several kinds of errors */
54512 int nRec; /* Pages journalled since last j-header written */
54513 u32 cksumInit; /* Quasi-random value added to every checksum */
54514 u32 nSubRec; /* Number of records written to sub-journal */
54515 Bitvec *pInJournal; /* One bit for each page in the database file */
54516 sqlite3_file *fd; /* File descriptor for database */
54517 sqlite3_file *jfd; /* File descriptor for main journal */
54518 sqlite3_file *sjfd; /* File descriptor for sub-journal */
54519 i64 journalOff; /* Current write offset in the journal file */
54520 i64 journalHdr; /* Byte offset to previous journal header */
54521 sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
54522 PagerSavepoint *aSavepoint; /* Array of active savepoints */
54523 int nSavepoint; /* Number of elements in aSavepoint[] */
54524 u32 iDataVersion; /* Changes whenever database content changes */
54525 char dbFileVers[16]; /* Changes whenever database file changes */
54526
54527 int nMmapOut; /* Number of mmap pages currently outstanding */
54528 sqlite3_int64 szMmap; /* Desired maximum mmap size */
54529 PgHdr *pMmapFreelist; /* List of free mmap page headers (pDirty) */
54530 /*
54531 ** End of the routinely-changing class members
54532 ***************************************************************************/
54533
54534 u16 nExtra; /* Add this many bytes to each in-memory page */
54535 i16 nReserve; /* Number of unused bytes at end of each page */
54536 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
54537 u32 sectorSize; /* Assumed sector size during rollback */
54538 Pgno mxPgno; /* Maximum allowed size of the database */
54539 Pgno lckPgno; /* Page number for the locking page */
54540 i64 pageSize; /* Number of bytes in a page */
54541 i64 journalSizeLimit; /* Size limit for persistent journal files */
54542 char *zFilename; /* Name of the database file */
54543 char *zJournal; /* Name of the journal file */
54544 int (*xBusyHandler)(void*); /* Function to call when busy */
54545 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
54546 int aStat[4]; /* Total cache hits, misses, writes, spills */
54547#ifdef SQLITE_TEST
54548 int nRead; /* Database pages read */
54549#endif
54550 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
54551 int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */
54552 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
54553 PCache *pPCache; /* Pointer to page cache object */
54554#ifndef SQLITE_OMIT_WAL
54555 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
54556 char *zWal; /* File name for write-ahead log */
54557#endif
54558};
54559
54560/*
54561** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains
54562** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS
54563** or CACHE_WRITE to sqlite3_db_status().
54564*/
54565#define PAGER_STAT_HIT0 0
54566#define PAGER_STAT_MISS1 1
54567#define PAGER_STAT_WRITE2 2
54568#define PAGER_STAT_SPILL3 3
54569
54570/*
54571** The following global variables hold counters used for
54572** testing purposes only. These variables do not exist in
54573** a non-testing build. These variables are not thread-safe.
54574*/
54575#ifdef SQLITE_TEST
54576SQLITE_API int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
54577SQLITE_API int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
54578SQLITE_API int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
54579# define PAGER_INCR(v) v++
54580#else
54581# define PAGER_INCR(v)
54582#endif
54583
54584
54585
54586/*
54587** Journal files begin with the following magic string. The data
54588** was obtained from /dev/random. It is used only as a sanity check.
54589**
54590** Since version 2.8.0, the journal format contains additional sanity
54591** checking information. If the power fails while the journal is being
54592** written, semi-random garbage data might appear in the journal
54593** file after power is restored. If an attempt is then made
54594** to roll the journal back, the database could be corrupted. The additional
54595** sanity checking data is an attempt to discover the garbage in the
54596** journal and ignore it.
54597**
54598** The sanity checking information for the new journal format consists
54599** of a 32-bit checksum on each page of data. The checksum covers both
54600** the page number and the pPager->pageSize bytes of data for the page.
54601** This cksum is initialized to a 32-bit random value that appears in the
54602** journal file right after the header. The random initializer is important,
54603** because garbage data that appears at the end of a journal is likely
54604** data that was once in other files that have now been deleted. If the
54605** garbage data came from an obsolete journal file, the checksums might
54606** be correct. But by initializing the checksum to random value which
54607** is different for every journal, we minimize that risk.
54608*/
54609static const unsigned char aJournalMagic[] = {
54610 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
54611};
54612
54613/*
54614** The size of the of each page record in the journal is given by
54615** the following macro.
54616*/
54617#define JOURNAL_PG_SZ(pPager)((pPager->pageSize) + 8) ((pPager->pageSize) + 8)
54618
54619/*
54620** The journal header size for this pager. This is usually the same
54621** size as a single disk sector. See also setSectorSize().
54622*/
54623#define JOURNAL_HDR_SZ(pPager)(pPager->sectorSize) (pPager->sectorSize)
54624
54625/*
54626** The macro MEMDB is true if we are dealing with an in-memory database.
54627** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
54628** the value of MEMDB will be a constant and the compiler will optimize
54629** out code that would never execute.
54630*/
54631#ifdef SQLITE_OMIT_MEMORYDB
54632# define MEMDBpPager->memDb 0
54633#else
54634# define MEMDBpPager->memDb pPager->memDb
54635#endif
54636
54637/*
54638** The macro USEFETCH is true if we are allowed to use the xFetch and xUnfetch
54639** interfaces to access the database using memory-mapped I/O.
54640*/
54641#if SQLITE_MAX_MMAP_SIZE20971520>0
54642# define USEFETCH(x)((x)->bUseFetch) ((x)->bUseFetch)
54643#else
54644# define USEFETCH(x)((x)->bUseFetch) 0
54645#endif
54646
54647/*
54648** The argument to this macro is a file descriptor (type sqlite3_file*).
54649** Return 0 if it is not open, or non-zero (but not 1) if it is.
54650**
54651** This is so that expressions can be written as:
54652**
54653** if( isOpen(pPager->jfd) ){ ...
54654**
54655** instead of
54656**
54657** if( pPager->jfd->pMethods ){ ...
54658*/
54659#define isOpen(pFd)((pFd)->pMethods!=0) ((pFd)->pMethods!=0)
54660
54661#ifdef SQLITE_DIRECT_OVERFLOW_READ
54662/*
54663** Return true if page pgno can be read directly from the database file
54664** by the b-tree layer. This is the case if:
54665**
54666** * the database file is open,
54667** * there are no dirty pages in the cache, and
54668** * the desired page is not currently in the wal file.
54669*/
54670SQLITE_PRIVATEstatic int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){
54671 if( pPager->fd->pMethods==0 ) return 0;
54672 if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
54673#ifndef SQLITE_OMIT_WAL
54674 if( pPager->pWal ){
54675 u32 iRead = 0;
54676 int rc;
54677 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
54678 return (rc==SQLITE_OK0 && iRead==0);
54679 }
54680#endif
54681 return 1;
54682}
54683#endif
54684
54685#ifndef SQLITE_OMIT_WAL
54686# define pagerUseWal(x)((x)->pWal!=0) ((x)->pWal!=0)
54687#else
54688# define pagerUseWal(x)((x)->pWal!=0) 0
54689# define pagerRollbackWal(x) 0
54690# define pagerWalFrames(v,w,x,y) 0
54691# define pagerOpenWalIfPresent(z) SQLITE_OK0
54692# define pagerBeginReadTransaction(z) SQLITE_OK0
54693#endif
54694
54695#ifndef NDEBUG1
54696/*
54697** Usage:
54698**
54699** assert( assert_pager_state(pPager) );
54700**
54701** This function runs many asserts to try to find inconsistencies in
54702** the internal state of the Pager object.
54703*/
54704static int assert_pager_state(Pager *p){
54705 Pager *pPager = p;
54706
54707 /* State must be valid. */
54708 assert( p->eState==PAGER_OPEN((void) (0))
54709 || p->eState==PAGER_READER((void) (0))
54710 || p->eState==PAGER_WRITER_LOCKED((void) (0))
54711 || p->eState==PAGER_WRITER_CACHEMOD((void) (0))
54712 || p->eState==PAGER_WRITER_DBMOD((void) (0))
54713 || p->eState==PAGER_WRITER_FINISHED((void) (0))
54714 || p->eState==PAGER_ERROR((void) (0))
54715 )((void) (0));
54716
54717 /* Regardless of the current state, a temp-file connection always behaves
54718 ** as if it has an exclusive lock on the database file. It never updates
54719 ** the change-counter field, so the changeCountDone flag is always set.
54720 */
54721 assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK )((void) (0));
54722 assert( p->tempFile==0 || pPager->changeCountDone )((void) (0));
54723
54724 /* If the useJournal flag is clear, the journal-mode must be "OFF".
54725 ** And if the journal-mode is "OFF", the journal file must not be open.
54726 */
54727 assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal )((void) (0));
54728 assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) )((void) (0));
54729
54730 /* Check that MEMDB implies noSync. And an in-memory journal. Since
54731 ** this means an in-memory pager performs no IO at all, it cannot encounter
54732 ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing
54733 ** a journal file. (although the in-memory journal implementation may
54734 ** return SQLITE_IOERR_NOMEM while the journal file is being written). It
54735 ** is therefore not possible for an in-memory pager to enter the ERROR
54736 ** state.
54737 */
54738 if( MEMDBpPager->memDb ){
54739 assert( !isOpen(p->fd) )((void) (0));
54740 assert( p->noSync )((void) (0));
54741 assert( p->journalMode==PAGER_JOURNALMODE_OFF((void) (0))
54742 || p->journalMode==PAGER_JOURNALMODE_MEMORY((void) (0))
54743 )((void) (0));
54744 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN )((void) (0));
54745 assert( pagerUseWal(p)==0 )((void) (0));
54746 }
54747
54748 /* If changeCountDone is set, a RESERVED lock or greater must be held
54749 ** on the file.
54750 */
54751 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK )((void) (0));
54752 assert( p->eLock!=PENDING_LOCK )((void) (0));
54753
54754 switch( p->eState ){
54755 case PAGER_OPEN0:
54756 assert( !MEMDB )((void) (0));
54757 assert( pPager->errCode==SQLITE_OK )((void) (0));
54758 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile )((void) (0));
54759 break;
54760
54761 case PAGER_READER1:
54762 assert( pPager->errCode==SQLITE_OK )((void) (0));
54763 assert( p->eLock!=UNKNOWN_LOCK )((void) (0));
54764 assert( p->eLock>=SHARED_LOCK )((void) (0));
54765 break;
54766
54767 case PAGER_WRITER_LOCKED2:
54768 assert( p->eLock!=UNKNOWN_LOCK )((void) (0));
54769 assert( pPager->errCode==SQLITE_OK )((void) (0));
54770 if( !pagerUseWal(pPager)((pPager)->pWal!=0) ){
54771 assert( p->eLock>=RESERVED_LOCK )((void) (0));
54772 }
54773 assert( pPager->dbSize==pPager->dbOrigSize )((void) (0));
54774 assert( pPager->dbOrigSize==pPager->dbFileSize )((void) (0));
54775 assert( pPager->dbOrigSize==pPager->dbHintSize )((void) (0));
54776 assert( pPager->setSuper==0 )((void) (0));
54777 break;
54778
54779 case PAGER_WRITER_CACHEMOD3:
54780 assert( p->eLock!=UNKNOWN_LOCK )((void) (0));
54781 assert( pPager->errCode==SQLITE_OK )((void) (0));
54782 if( !pagerUseWal(pPager)((pPager)->pWal!=0) ){
54783 /* It is possible that if journal_mode=wal here that neither the
54784 ** journal file nor the WAL file are open. This happens during
54785 ** a rollback transaction that switches from journal_mode=off
54786 ** to journal_mode=wal.
54787 */
54788 assert( p->eLock>=RESERVED_LOCK )((void) (0));
54789 assert( isOpen(p->jfd)((void) (0))
54790 || p->journalMode==PAGER_JOURNALMODE_OFF((void) (0))
54791 || p->journalMode==PAGER_JOURNALMODE_WAL((void) (0))
54792 )((void) (0));
54793 }
54794 assert( pPager->dbOrigSize==pPager->dbFileSize )((void) (0));
54795 assert( pPager->dbOrigSize==pPager->dbHintSize )((void) (0));
54796 break;
54797
54798 case PAGER_WRITER_DBMOD4:
54799 assert( p->eLock==EXCLUSIVE_LOCK )((void) (0));
54800 assert( pPager->errCode==SQLITE_OK )((void) (0));
54801 assert( !pagerUseWal(pPager) )((void) (0));
54802 assert( p->eLock>=EXCLUSIVE_LOCK )((void) (0));
54803 assert( isOpen(p->jfd)((void) (0))
54804 || p->journalMode==PAGER_JOURNALMODE_OFF((void) (0))
54805 || p->journalMode==PAGER_JOURNALMODE_WAL((void) (0))
54806 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)((void) (0))
54807 )((void) (0));
54808 assert( pPager->dbOrigSize<=pPager->dbHintSize )((void) (0));
54809 break;
54810
54811 case PAGER_WRITER_FINISHED5:
54812 assert( p->eLock==EXCLUSIVE_LOCK )((void) (0));
54813 assert( pPager->errCode==SQLITE_OK )((void) (0));
54814 assert( !pagerUseWal(pPager) )((void) (0));
54815 assert( isOpen(p->jfd)((void) (0))
54816 || p->journalMode==PAGER_JOURNALMODE_OFF((void) (0))
54817 || p->journalMode==PAGER_JOURNALMODE_WAL((void) (0))
54818 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)((void) (0))
54819 )((void) (0));
54820 break;
54821
54822 case PAGER_ERROR6:
54823 /* There must be at least one outstanding reference to the pager if
54824 ** in ERROR state. Otherwise the pager should have already dropped
54825 ** back to OPEN state.
54826 */
54827 assert( pPager->errCode!=SQLITE_OK )((void) (0));
54828 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile )((void) (0));
54829 break;
54830 }
54831
54832 return 1;
54833}
54834#endif /* ifndef NDEBUG */
54835
54836#ifdef SQLITE_DEBUG
54837/*
54838** Return a pointer to a human readable string in a static buffer
54839** containing the state of the Pager object passed as an argument. This
54840** is intended to be used within debuggers. For example, as an alternative
54841** to "print *pPager" in gdb:
54842**
54843** (gdb) printf "%s", print_pager_state(pPager)
54844**
54845** This routine has external linkage in order to suppress compiler warnings
54846** about an unused function. It is enclosed within SQLITE_DEBUG and so does
54847** not appear in normal builds.
54848*/
54849char *print_pager_state(Pager *p){
54850 static char zRet[1024];
54851
54852 sqlite3_snprintf(1024, zRet,
54853 "Filename: %s\n"
54854 "State: %s errCode=%d\n"
54855 "Lock: %s\n"
54856 "Locking mode: locking_mode=%s\n"
54857 "Journal mode: journal_mode=%s\n"
54858 "Backing store: tempFile=%d memDb=%d useJournal=%d\n"
54859 "Journal: journalOff=%lld journalHdr=%lld\n"
54860 "Size: dbsize=%d dbOrigSize=%d dbFileSize=%d\n"
54861 , p->zFilename
54862 , p->eState==PAGER_OPEN0 ? "OPEN" :
54863 p->eState==PAGER_READER1 ? "READER" :
54864 p->eState==PAGER_WRITER_LOCKED2 ? "WRITER_LOCKED" :
54865 p->eState==PAGER_WRITER_CACHEMOD3 ? "WRITER_CACHEMOD" :
54866 p->eState==PAGER_WRITER_DBMOD4 ? "WRITER_DBMOD" :
54867 p->eState==PAGER_WRITER_FINISHED5 ? "WRITER_FINISHED" :
54868 p->eState==PAGER_ERROR6 ? "ERROR" : "?error?"
54869 , (int)p->errCode
54870 , p->eLock==NO_LOCK0 ? "NO_LOCK" :
54871 p->eLock==RESERVED_LOCK2 ? "RESERVED" :
54872 p->eLock==EXCLUSIVE_LOCK4 ? "EXCLUSIVE" :
54873 p->eLock==SHARED_LOCK1 ? "SHARED" :
54874 p->eLock==UNKNOWN_LOCK(4 +1) ? "UNKNOWN" : "?error?"
54875 , p->exclusiveMode ? "exclusive" : "normal"
54876 , p->journalMode==PAGER_JOURNALMODE_MEMORY4 ? "memory" :
54877 p->journalMode==PAGER_JOURNALMODE_OFF2 ? "off" :
54878 p->journalMode==PAGER_JOURNALMODE_DELETE0 ? "delete" :
54879 p->journalMode==PAGER_JOURNALMODE_PERSIST1 ? "persist" :
54880 p->journalMode==PAGER_JOURNALMODE_TRUNCATE3 ? "truncate" :
54881 p->journalMode==PAGER_JOURNALMODE_WAL5 ? "wal" : "?error?"
54882 , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
54883 , p->journalOff, p->journalHdr
54884 , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
54885 );
54886
54887 return zRet;
54888}
54889#endif
54890
54891/* Forward references to the various page getters */
54892static int getPageNormal(Pager*,Pgno,DbPage**,int);
54893static int getPageError(Pager*,Pgno,DbPage**,int);
54894#if SQLITE_MAX_MMAP_SIZE20971520>0
54895static int getPageMMap(Pager*,Pgno,DbPage**,int);
54896#endif
54897
54898/*
54899** Set the Pager.xGet method for the appropriate routine used to fetch
54900** content from the pager.
54901*/
54902static void setGetterMethod(Pager *pPager){
54903 if( pPager->errCode ){
54904 pPager->xGet = getPageError;
54905#if SQLITE_MAX_MMAP_SIZE20971520>0
54906 }else if( USEFETCH(pPager)((pPager)->bUseFetch) ){
54907 pPager->xGet = getPageMMap;
54908#endif /* SQLITE_MAX_MMAP_SIZE>0 */
54909 }else{
54910 pPager->xGet = getPageNormal;
54911 }
54912}
54913
54914/*
54915** Return true if it is necessary to write page *pPg into the sub-journal.
54916** A page needs to be written into the sub-journal if there exists one
54917** or more open savepoints for which:
54918**
54919** * The page-number is less than or equal to PagerSavepoint.nOrig, and
54920** * The bit corresponding to the page-number is not set in
54921** PagerSavepoint.pInSavepoint.
54922*/
54923static int subjRequiresPage(PgHdr *pPg){
54924 Pager *pPager = pPg->pPager;
54925 PagerSavepoint *p;
54926 Pgno pgno = pPg->pgno;
54927 int i;
54928 for(i=0; i<pPager->nSavepoint; i++){
54929 p = &pPager->aSavepoint[i];
54930 if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){
54931 for(i=i+1; i<pPager->nSavepoint; i++){
54932 pPager->aSavepoint[i].bTruncateOnRelease = 0;
54933 }
54934 return 1;
54935 }
54936 }
54937 return 0;
54938}
54939
54940#ifdef SQLITE_DEBUG
54941/*
54942** Return true if the page is already in the journal file.
54943*/
54944static int pageInJournal(Pager *pPager, PgHdr *pPg){
54945 return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
54946}
54947#endif
54948
54949/*
54950** Read a 32-bit integer from the given file descriptor. Store the integer
54951** that is read in *pRes. Return SQLITE_OK if everything worked, or an
54952** error code is something goes wrong.
54953**
54954** All values are stored on disk as big-endian.
54955*/
54956static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
54957 unsigned char ac[4];
54958 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
54959 if( rc==SQLITE_OK0 ){
54960 *pRes = sqlite3Get4byte(ac);
54961 }
54962 return rc;
54963}
54964
54965/*
54966** Write a 32-bit integer into a string buffer in big-endian byte order.
54967*/
54968#define put32bits(A,B)sqlite3Put4byte((u8*)A,B) sqlite3Put4byte((u8*)A,B)
54969
54970
54971/*
54972** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
54973** on success or an error code is something goes wrong.
54974*/
54975static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
54976 char ac[4];
54977 put32bits(ac, val)sqlite3Put4byte((u8*)ac,val);
54978 return sqlite3OsWrite(fd, ac, 4, offset);
54979}
54980
54981/*
54982** Unlock the database file to level eLock, which must be either NO_LOCK
54983** or SHARED_LOCK. Regardless of whether or not the call to xUnlock()
54984** succeeds, set the Pager.eLock variable to match the (attempted) new lock.
54985**
54986** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
54987** called, do not modify it. See the comment above the #define of
54988** UNKNOWN_LOCK for an explanation of this.
54989*/
54990static int pagerUnlockDb(Pager *pPager, int eLock){
54991 int rc = SQLITE_OK0;
54992
54993 assert( !pPager->exclusiveMode || pPager->eLock==eLock )((void) (0));
54994 assert( eLock==NO_LOCK || eLock==SHARED_LOCK )((void) (0));
54995 assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 )((void) (0));
54996 if( isOpen(pPager->fd)((pPager->fd)->pMethods!=0) ){
54997 assert( pPager->eLock>=eLock )((void) (0));
54998 rc = pPager->noLock ? SQLITE_OK0 : sqlite3OsUnlock(pPager->fd, eLock);
54999 if( pPager->eLock!=UNKNOWN_LOCK(4 +1) ){
55000 pPager->eLock = (u8)eLock;
55001 }
55002 IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
55003 }
55004 pPager->changeCountDone = pPager->tempFile; /* ticket fb3b3024ea238d5c */
55005 return rc;
55006}
55007
55008/*
55009** Lock the database file to level eLock, which must be either SHARED_LOCK,
55010** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the
55011** Pager.eLock variable to the new locking state.
55012**
55013** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
55014** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK.
55015** See the comment above the #define of UNKNOWN_LOCK for an explanation
55016** of this.
55017*/
55018static int pagerLockDb(Pager *pPager, int eLock){
55019 int rc = SQLITE_OK0;
55020
55021 assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK )((void) (0));
55022 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK(4 +1) ){
55023 rc = pPager->noLock ? SQLITE_OK0 : sqlite3OsLock(pPager->fd, eLock);
55024 if( rc==SQLITE_OK0 && (pPager->eLock!=UNKNOWN_LOCK(4 +1)||eLock==EXCLUSIVE_LOCK4) ){
55025 pPager->eLock = (u8)eLock;
55026 IOTRACE(("LOCK %p %d\n", pPager, eLock))
55027 }
55028 }
55029 return rc;
55030}
55031
55032/*
55033** This function determines whether or not the atomic-write or
55034** atomic-batch-write optimizations can be used with this pager. The
55035** atomic-write optimization can be used if:
55036**
55037** (a) the value returned by OsDeviceCharacteristics() indicates that
55038** a database page may be written atomically, and
55039** (b) the value returned by OsSectorSize() is less than or equal
55040** to the page size.
55041**
55042** If it can be used, then the value returned is the size of the journal
55043** file when it contains rollback data for exactly one page.
55044**
55045** The atomic-batch-write optimization can be used if OsDeviceCharacteristics()
55046** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is
55047** returned in this case.
55048**
55049** If neither optimization can be used, 0 is returned.
55050*/
55051static int jrnlBufferSize(Pager *pPager){
55052 assert( !MEMDB )((void) (0));
55053
55054#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
55055 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
55056 int dc; /* Device characteristics */
55057
55058 assert( isOpen(pPager->fd) )((void) (0));
55059 dc = sqlite3OsDeviceCharacteristics(pPager->fd);
55060#else
55061 UNUSED_PARAMETER(pPager)(void)(pPager);
55062#endif
55063
55064#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
55065 if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC0x00004000) ){
55066 return -1;
55067 }
55068#endif
55069
55070#ifdef SQLITE_ENABLE_ATOMIC_WRITE
55071 {
55072 int nSector = pPager->sectorSize;
55073 int szPage = pPager->pageSize;
55074
55075 assert(SQLITE_IOCAP_ATOMIC512==(512>>8))((void) (0));
55076 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8))((void) (0));
55077 if( 0==(dc&(SQLITE_IOCAP_ATOMIC0x00000001|(szPage>>8)) || nSector>szPage) ){
55078 return 0;
55079 }
55080 }
55081
55082 return JOURNAL_HDR_SZ(pPager)(pPager->sectorSize) + JOURNAL_PG_SZ(pPager)((pPager->pageSize) + 8);
55083#endif
55084
55085 return 0;
55086}
55087
55088/*
55089** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
55090** on the cache using a hash function. This is used for testing
55091** and debugging only.
55092*/
55093#ifdef SQLITE_CHECK_PAGES
55094/*
55095** Return a 32-bit hash of the page data for pPage.
55096*/
55097static u32 pager_datahash(int nByte, unsigned char *pData)0{
55098 u32 hash = 0;
55099 int i;
55100 for(i=0; i<nByte; i++){
55101 hash = (hash*1039) + pData[i];
55102 }
55103 return hash;
55104}
55105static u32 pager_pagehash(PgHdr *pPage)0{
55106 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData)0;
55107}
55108static void pager_set_pagehash(PgHdr *pPage){
55109 pPage->pageHash = pager_pagehash(pPage)0;
55110}
55111
55112/*
55113** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
55114** is defined, and NDEBUG is not defined, an assert() statement checks
55115** that the page is either dirty or still matches the calculated page-hash.
55116*/
55117#define CHECK_PAGE(x) checkPage(x)
55118static void checkPage(PgHdr *pPg){
55119 Pager *pPager = pPg->pPager;
55120 assert( pPager->eState!=PAGER_ERROR )((void) (0));
55121 assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) )((void) (0));
55122}
55123
55124#else
55125#define pager_datahash(X,Y)0 0
55126#define pager_pagehash(X)0 0
55127#define pager_set_pagehash(X)
55128#define CHECK_PAGE(x)
55129#endif /* SQLITE_CHECK_PAGES */
55130
55131/*
55132** When this is called the journal file for pager pPager must be open.
55133** This function attempts to read a super-journal file name from the
55134** end of the file and, if successful, copies it into memory supplied
55135** by the caller. See comments above writeSuperJournal() for the format
55136** used to store a super-journal file name at the end of a journal file.
55137**
55138** zSuper must point to a buffer of at least nSuper bytes allocated by
55139** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
55140** enough space to write the super-journal name). If the super-journal
55141** name in the journal is longer than nSuper bytes (including a
55142** nul-terminator), then this is handled as if no super-journal name
55143** were present in the journal.
55144**
55145** If a super-journal file name is present at the end of the journal
55146** file, then it is copied into the buffer pointed to by zSuper. A
55147** nul-terminator byte is appended to the buffer following the
55148** super-journal file name.
55149**
55150** If it is determined that no super-journal file name is present
55151** zSuper[0] is set to 0 and SQLITE_OK returned.
55152**
55153** If an error occurs while reading from the journal file, an SQLite
55154** error code is returned.
55155*/
55156static int readSuperJournal(sqlite3_file *pJrnl, char *zSuper, u32 nSuper){
55157 int rc; /* Return code */
55158 u32 len; /* Length in bytes of super-journal name */
55159 i64 szJ; /* Total size in bytes of journal file pJrnl */
55160 u32 cksum; /* MJ checksum value read from journal */
55161 u32 u; /* Unsigned loop counter */
55162 unsigned char aMagic[8]; /* A buffer to hold the magic header */
55163 zSuper[0] = '\0';
55164
55165 if( SQLITE_OK0!=(rc = sqlite3OsFileSize(pJrnl, &szJ))
55166 || szJ<16
55167 || SQLITE_OK0!=(rc = read32bits(pJrnl, szJ-16, &len))
55168 || len>=nSuper
55169 || len>szJ-16
55170 || len==0
55171 || SQLITE_OK0!=(rc = read32bits(pJrnl, szJ-12, &cksum))
55172 || SQLITE_OK0!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
55173 || memcmp(aMagic, aJournalMagic, 8)
55174 || SQLITE_OK0!=(rc = sqlite3OsRead(pJrnl, zSuper, len, szJ-16-len))
55175 ){
55176 return rc;
55177 }
55178
55179 /* See if the checksum matches the super-journal name */
55180 for(u=0; u<len; u++){
55181 cksum -= zSuper[u];
55182 }
55183 if( cksum ){
55184 /* If the checksum doesn't add up, then one or more of the disk sectors
55185 ** containing the super-journal filename is corrupted. This means
55186 ** definitely roll back, so just return SQLITE_OK and report a (nul)
55187 ** super-journal filename.
55188 */
55189 len = 0;
55190 }
55191 zSuper[len] = '\0';
55192 zSuper[len+1] = '\0';
55193
55194 return SQLITE_OK0;
55195}
55196
55197/*
55198** Return the offset of the sector boundary at or immediately
55199** following the value in pPager->journalOff, assuming a sector
55200** size of pPager->sectorSize bytes.
55201**
55202** i.e for a sector size of 512:
55203**
55204** Pager.journalOff Return value
55205** ---------------------------------------
55206** 0 0
55207** 512 512
55208** 100 512
55209** 2000 2048
55210**
55211*/
55212static i64 journalHdrOffset(Pager *pPager){
55213 i64 offset = 0;
55214 i64 c = pPager->journalOff;
55215 if( c ){
55216 offset = ((c-1)/JOURNAL_HDR_SZ(pPager)(pPager->sectorSize) + 1) * JOURNAL_HDR_SZ(pPager)(pPager->sectorSize);
55217 }
55218 assert( offset%JOURNAL_HDR_SZ(pPager)==0 )((void) (0));
55219 assert( offset>=c )((void) (0));
55220 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) )((void) (0));
55221 return offset;
55222}
55223
55224/*
55225** The journal file must be open when this function is called.
55226**
55227** This function is a no-op if the journal file has not been written to
55228** within the current transaction (i.e. if Pager.journalOff==0).
55229**
55230** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
55231** set to 0, then truncate the journal file to zero bytes in size. Otherwise,
55232** zero the 28-byte header at the start of the journal file. In either case,
55233** if the pager is not in no-sync mode, sync the journal file immediately
55234** after writing or truncating it.
55235**
55236** If Pager.journalSizeLimit is set to a positive, non-zero value, and
55237** following the truncation or zeroing described above the size of the
55238** journal file in bytes is larger than this value, then truncate the
55239** journal file to Pager.journalSizeLimit bytes. The journal file does
55240** not need to be synced following this operation.
55241**
55242** If an IO error occurs, abandon processing and return the IO error code.
55243** Otherwise, return SQLITE_OK.
55244*/
55245static int zeroJournalHdr(Pager *pPager, int doTruncate){
55246 int rc = SQLITE_OK0; /* Return code */
55247 assert( isOpen(pPager->jfd) )((void) (0));
55248 assert( !sqlite3JournalIsInMemory(pPager->jfd) )((void) (0));
55249 if( pPager->journalOff ){
55250 const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */
55251
55252 IOTRACE(("JZEROHDR %p\n", pPager))
55253 if( doTruncate || iLimit==0 ){
55254 rc = sqlite3OsTruncate(pPager->jfd, 0);
55255 }else{
55256 static const char zeroHdr[28] = {0};
55257 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
55258 }
55259 if( rc==SQLITE_OK0 && !pPager->noSync ){
55260 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY0x00010|pPager->syncFlags);
55261 }
55262
55263 /* At this point the transaction is committed but the write lock
55264 ** is still held on the file. If there is a size limit configured for
55265 ** the persistent journal and the journal file currently consumes more
55266 ** space than that limit allows for, truncate it now. There is no need
55267 ** to sync the file following this operation.
55268 */
55269 if( rc==SQLITE_OK0 && iLimit>0 ){
55270 i64 sz;
55271 rc = sqlite3OsFileSize(pPager->jfd, &sz);
55272 if( rc==SQLITE_OK0 && sz>iLimit ){
55273 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
55274 }
55275 }
55276 }
55277 return rc;
55278}
55279
55280/*
55281** The journal file must be open when this routine is called. A journal
55282** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
55283** current location.
55284**
55285** The format for the journal header is as follows:
55286** - 8 bytes: Magic identifying journal format.
55287** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
55288** - 4 bytes: Random number used for page hash.
55289** - 4 bytes: Initial database page count.
55290** - 4 bytes: Sector size used by the process that wrote this journal.
55291** - 4 bytes: Database page size.
55292**
55293** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
55294*/
55295static int writeJournalHdr(Pager *pPager){
55296 int rc = SQLITE_OK0; /* Return code */
55297 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
55298 u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
55299 u32 nWrite; /* Bytes of header sector written */
55300 int ii; /* Loop counter */
55301
55302 assert( isOpen(pPager->jfd) )((void) (0)); /* Journal file must be open. */
55303
55304 if( nHeader>JOURNAL_HDR_SZ(pPager)(pPager->sectorSize) ){
55305 nHeader = JOURNAL_HDR_SZ(pPager)(pPager->sectorSize);
55306 }
55307
55308 /* If there are active savepoints and any of them were created
55309 ** since the most recent journal header was written, update the
55310 ** PagerSavepoint.iHdrOffset fields now.
55311 */
55312 for(ii=0; ii<pPager->nSavepoint; ii++){
55313 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
55314 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
55315 }
55316 }
55317
55318 pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
55319
55320 /*
55321 ** Write the nRec Field - the number of page records that follow this
55322 ** journal header. Normally, zero is written to this value at this time.
55323 ** After the records are added to the journal (and the journal synced,
55324 ** if in full-sync mode), the zero is overwritten with the true number
55325 ** of records (see syncJournal()).
55326 **
55327 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
55328 ** reading the journal this value tells SQLite to assume that the
55329 ** rest of the journal file contains valid page records. This assumption
55330 ** is dangerous, as if a failure occurred whilst writing to the journal
55331 ** file it may contain some garbage data. There are two scenarios
55332 ** where this risk can be ignored:
55333 **
55334 ** * When the pager is in no-sync mode. Corruption can follow a
55335 ** power failure in this case anyway.
55336 **
55337 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
55338 ** that garbage data is never appended to the journal file.
55339 */
55340 assert( isOpen(pPager->fd) || pPager->noSync )((void) (0));
55341 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY4)
55342 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND0x00000200)
55343 ){
55344 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
55345 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff)sqlite3Put4byte((u8*)&zHeader[sizeof(aJournalMagic)],0xffffffff
)
;
55346 }else{
55347 memset(zHeader, 0, sizeof(aJournalMagic)+4);
55348 }
55349
55350 /* The random check-hash initializer */
55351 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
55352 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit)sqlite3Put4byte((u8*)&zHeader[sizeof(aJournalMagic)+4],pPager
->cksumInit)
;
55353 /* The initial database size */
55354 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize)sqlite3Put4byte((u8*)&zHeader[sizeof(aJournalMagic)+8],pPager
->dbOrigSize)
;
55355 /* The assumed sector size for this process */
55356 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize)sqlite3Put4byte((u8*)&zHeader[sizeof(aJournalMagic)+12],pPager
->sectorSize)
;
55357
55358 /* The page size */
55359 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize)sqlite3Put4byte((u8*)&zHeader[sizeof(aJournalMagic)+16],pPager
->pageSize)
;
55360
55361 /* Initializing the tail of the buffer is not necessary. Everything
55362 ** works find if the following memset() is omitted. But initializing
55363 ** the memory prevents valgrind from complaining, so we are willing to
55364 ** take the performance hit.
55365 */
55366 memset(&zHeader[sizeof(aJournalMagic)+20], 0,
55367 nHeader-(sizeof(aJournalMagic)+20));
55368
55369 /* In theory, it is only necessary to write the 28 bytes that the
55370 ** journal header consumes to the journal file here. Then increment the
55371 ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next
55372 ** record is written to the following sector (leaving a gap in the file
55373 ** that will be implicitly filled in by the OS).
55374 **
55375 ** However it has been discovered that on some systems this pattern can
55376 ** be significantly slower than contiguously writing data to the file,
55377 ** even if that means explicitly writing data to the block of
55378 ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
55379 ** is done.
55380 **
55381 ** The loop is required here in case the sector-size is larger than the
55382 ** database page size. Since the zHeader buffer is only Pager.pageSize
55383 ** bytes in size, more than one call to sqlite3OsWrite() may be required
55384 ** to populate the entire journal header sector.
55385 */
55386 for(nWrite=0; rc==SQLITE_OK0&&nWrite<JOURNAL_HDR_SZ(pPager)(pPager->sectorSize); nWrite+=nHeader){
55387 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
55388 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
55389 assert( pPager->journalHdr <= pPager->journalOff )((void) (0));
55390 pPager->journalOff += nHeader;
55391 }
55392
55393 return rc;
55394}
55395
55396/*
55397** The journal file must be open when this is called. A journal header file
55398** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
55399** file. The current location in the journal file is given by
55400** pPager->journalOff. See comments above function writeJournalHdr() for
55401** a description of the journal header format.
55402**
55403** If the header is read successfully, *pNRec is set to the number of
55404** page records following this header and *pDbSize is set to the size of the
55405** database before the transaction began, in pages. Also, pPager->cksumInit
55406** is set to the value read from the journal header. SQLITE_OK is returned
55407** in this case.
55408**
55409** If the journal header file appears to be corrupted, SQLITE_DONE is
55410** returned and *pNRec and *PDbSize are undefined. If JOURNAL_HDR_SZ bytes
55411** cannot be read from the journal file an error code is returned.
55412*/
55413static int readJournalHdr(
55414 Pager *pPager, /* Pager object */
55415 int isHot,
55416 i64 journalSize, /* Size of the open journal file in bytes */
55417 u32 *pNRec, /* OUT: Value read from the nRec field */
55418 u32 *pDbSize /* OUT: Value of original database size field */
55419){
55420 int rc; /* Return code */
55421 unsigned char aMagic[8]; /* A buffer to hold the magic header */
55422 i64 iHdrOff; /* Offset of journal header being read */
55423
55424 assert( isOpen(pPager->jfd) )((void) (0)); /* Journal file must be open. */
55425
55426 /* Advance Pager.journalOff to the start of the next sector. If the
55427 ** journal file is too small for there to be a header stored at this
55428 ** point, return SQLITE_DONE.
55429 */
55430 pPager->journalOff = journalHdrOffset(pPager);
55431 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager)(pPager->sectorSize) > journalSize ){
55432 return SQLITE_DONE101;
55433 }
55434 iHdrOff = pPager->journalOff;
55435
55436 /* Read in the first 8 bytes of the journal header. If they do not match
55437 ** the magic string found at the start of each journal header, return
55438 ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
55439 ** proceed.
55440 */
55441 if( isHot || iHdrOff!=pPager->journalHdr ){
55442 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
55443 if( rc ){
55444 return rc;
55445 }
55446 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
55447 return SQLITE_DONE101;
55448 }
55449 }
55450
55451 /* Read the first three 32-bit fields of the journal header: The nRec
55452 ** field, the checksum-initializer and the database size at the start
55453 ** of the transaction. Return an error code if anything goes wrong.
55454 */
55455 if( SQLITE_OK0!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
55456 || SQLITE_OK0!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
55457 || SQLITE_OK0!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
55458 ){
55459 return rc;
55460 }
55461
55462 if( pPager->journalOff==0 ){
55463 u32 iPageSize; /* Page-size field of journal header */
55464 u32 iSectorSize; /* Sector-size field of journal header */
55465
55466 /* Read the page-size and sector-size journal header fields. */
55467 if( SQLITE_OK0!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
55468 || SQLITE_OK0!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
55469 ){
55470 return rc;
55471 }
55472
55473 /* Versions of SQLite prior to 3.5.8 set the page-size field of the
55474 ** journal header to zero. In this case, assume that the Pager.pageSize
55475 ** variable is already set to the correct page size.
55476 */
55477 if( iPageSize==0 ){
55478 iPageSize = pPager->pageSize;
55479 }
55480
55481 /* Check that the values read from the page-size and sector-size fields
55482 ** are within range. To be 'in range', both values need to be a power
55483 ** of two greater than or equal to 512 or 32, and not greater than their
55484 ** respective compile time maximum limits.
55485 */
55486 if( iPageSize<512 || iSectorSize<32
55487 || iPageSize>SQLITE_MAX_PAGE_SIZE65536 || iSectorSize>MAX_SECTOR_SIZE0x10000
55488 || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0
55489 ){
55490 /* If the either the page-size or sector-size in the journal-header is
55491 ** invalid, then the process that wrote the journal-header must have
55492 ** crashed before the header was synced. In this case stop reading
55493 ** the journal file here.
55494 */
55495 return SQLITE_DONE101;
55496 }
55497
55498 /* Update the page-size to match the value read from the journal.
55499 ** Use a testcase() macro to make sure that malloc failure within
55500 ** PagerSetPagesize() is tested.
55501 */
55502 rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
55503 testcase( rc!=SQLITE_OK );
55504
55505 /* Update the assumed sector-size to match the value used by
55506 ** the process that created this journal. If this journal was
55507 ** created by a process other than this one, then this routine
55508 ** is being called from within pager_playback(). The local value
55509 ** of Pager.sectorSize is restored at the end of that routine.
55510 */
55511 pPager->sectorSize = iSectorSize;
55512 }
55513
55514 pPager->journalOff += JOURNAL_HDR_SZ(pPager)(pPager->sectorSize);
55515 return rc;
55516}
55517
55518
55519/*
55520** Write the supplied super-journal name into the journal file for pager
55521** pPager at the current location. The super-journal name must be the last
55522** thing written to a journal file. If the pager is in full-sync mode, the
55523** journal file descriptor is advanced to the next sector boundary before
55524** anything is written. The format is:
55525**
55526** + 4 bytes: PAGER_SJ_PGNO.
55527** + N bytes: super-journal filename in utf-8.
55528** + 4 bytes: N (length of super-journal name in bytes, no nul-terminator).
55529** + 4 bytes: super-journal name checksum.
55530** + 8 bytes: aJournalMagic[].
55531**
55532** The super-journal page checksum is the sum of the bytes in thesuper-journal
55533** name, where each byte is interpreted as a signed 8-bit integer.
55534**
55535** If zSuper is a NULL pointer (occurs for a single database transaction),
55536** this call is a no-op.
55537*/
55538static int writeSuperJournal(Pager *pPager, const char *zSuper){
55539 int rc; /* Return code */
55540 int nSuper; /* Length of string zSuper */
55541 i64 iHdrOff; /* Offset of header in journal file */
55542 i64 jrnlSize; /* Size of journal file on disk */
55543 u32 cksum = 0; /* Checksum of string zSuper */
55544
55545 assert( pPager->setSuper==0 )((void) (0));
55546 assert( !pagerUseWal(pPager) )((void) (0));
55547
55548 if( !zSuper
55549 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY4
55550 || !isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0)
55551 ){
55552 return SQLITE_OK0;
55553 }
55554 pPager->setSuper = 1;
55555 assert( pPager->journalHdr <= pPager->journalOff )((void) (0));
55556
55557 /* Calculate the length in bytes and the checksum of zSuper */
55558 for(nSuper=0; zSuper[nSuper]; nSuper++){
55559 cksum += zSuper[nSuper];
55560 }
55561
55562 /* If in full-sync mode, advance to the next disk sector before writing
55563 ** the super-journal name. This is in case the previous page written to
55564 ** the journal has already been synced.
55565 */
55566 if( pPager->fullSync ){
55567 pPager->journalOff = journalHdrOffset(pPager);
55568 }
55569 iHdrOff = pPager->journalOff;
55570
55571 /* Write the super-journal data to the end of the journal file. If
55572 ** an error occurs, return the error code to the caller.
55573 */
55574 if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_SJ_PGNO(pPager)((pPager)->lckPgno))))
55575 || (0 != (rc = sqlite3OsWrite(pPager->jfd, zSuper, nSuper, iHdrOff+4)))
55576 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper, nSuper)))
55577 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper+4, cksum)))
55578 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8,
55579 iHdrOff+4+nSuper+8)))
55580 ){
55581 return rc;
55582 }
55583 pPager->journalOff += (nSuper+20);
55584
55585 /* If the pager is in peristent-journal mode, then the physical
55586 ** journal-file may extend past the end of the super-journal name
55587 ** and 8 bytes of magic data just written to the file. This is
55588 ** dangerous because the code to rollback a hot-journal file
55589 ** will not be able to find the super-journal name to determine
55590 ** whether or not the journal is hot.
55591 **
55592 ** Easiest thing to do in this scenario is to truncate the journal
55593 ** file to the required size.
55594 */
55595 if( SQLITE_OK0==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
55596 && jrnlSize>pPager->journalOff
55597 ){
55598 rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
55599 }
55600 return rc;
55601}
55602
55603/*
55604** Discard the entire contents of the in-memory page-cache.
55605*/
55606static void pager_reset(Pager *pPager){
55607 pPager->iDataVersion++;
55608 sqlite3BackupRestart(pPager->pBackup);
55609 sqlite3PcacheClear(pPager->pPCache);
55610}
55611
55612/*
55613** Return the pPager->iDataVersion value
55614*/
55615SQLITE_PRIVATEstatic u32 sqlite3PagerDataVersion(Pager *pPager){
55616 return pPager->iDataVersion;
55617}
55618
55619/*
55620** Free all structures in the Pager.aSavepoint[] array and set both
55621** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
55622** if it is open and the pager is not in exclusive mode.
55623*/
55624static void releaseAllSavepoints(Pager *pPager){
55625 int ii; /* Iterator for looping through Pager.aSavepoint */
55626 for(ii=0; ii<pPager->nSavepoint; ii++){
55627 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
55628 }
55629 if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){
55630 sqlite3OsClose(pPager->sjfd);
55631 }
55632 sqlite3_free(pPager->aSavepoint);
55633 pPager->aSavepoint = 0;
55634 pPager->nSavepoint = 0;
55635 pPager->nSubRec = 0;
55636}
55637
55638/*
55639** Set the bit number pgno in the PagerSavepoint.pInSavepoint
55640** bitvecs of all open savepoints. Return SQLITE_OK if successful
55641** or SQLITE_NOMEM if a malloc failure occurs.
55642*/
55643static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
55644 int ii; /* Loop counter */
55645 int rc = SQLITE_OK0; /* Result code */
55646
55647 for(ii=0; ii<pPager->nSavepoint; ii++){
55648 PagerSavepoint *p = &pPager->aSavepoint[ii];
55649 if( pgno<=p->nOrig ){
55650 rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
55651 testcase( rc==SQLITE_NOMEM );
55652 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM )((void) (0));
55653 }
55654 }
55655 return rc;
55656}
55657
55658/*
55659** This function is a no-op if the pager is in exclusive mode and not
55660** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN
55661** state.
55662**
55663** If the pager is not in exclusive-access mode, the database file is
55664** completely unlocked. If the file is unlocked and the file-system does
55665** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is
55666** closed (if it is open).
55667**
55668** If the pager is in ERROR state when this function is called, the
55669** contents of the pager cache are discarded before switching back to
55670** the OPEN state. Regardless of whether the pager is in exclusive-mode
55671** or not, any journal file left in the file-system will be treated
55672** as a hot-journal and rolled back the next time a read-transaction
55673** is opened (by this or by any other connection).
55674*/
55675static void pager_unlock(Pager *pPager){
55676
55677 assert( pPager->eState==PAGER_READER((void) (0))
55678 || pPager->eState==PAGER_OPEN((void) (0))
55679 || pPager->eState==PAGER_ERROR((void) (0))
55680 )((void) (0));
55681
55682 sqlite3BitvecDestroy(pPager->pInJournal);
55683 pPager->pInJournal = 0;
55684 releaseAllSavepoints(pPager);
55685
55686 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
55687 assert( !isOpen(pPager->jfd) )((void) (0));
55688 sqlite3WalEndReadTransaction(pPager->pWal);
55689 pPager->eState = PAGER_OPEN0;
55690 }else if( !pPager->exclusiveMode ){
55691 int rc; /* Error code returned by pagerUnlockDb() */
55692 int iDc = isOpen(pPager->fd)((pPager->fd)->pMethods!=0)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
55693
55694 /* If the operating system support deletion of open files, then
55695 ** close the journal file when dropping the database lock. Otherwise
55696 ** another connection with journal_mode=delete might delete the file
55697 ** out from under us.
55698 */
55699 assert( (PAGER_JOURNALMODE_MEMORY & 5)!=1 )((void) (0));
55700 assert( (PAGER_JOURNALMODE_OFF & 5)!=1 )((void) (0));
55701 assert( (PAGER_JOURNALMODE_WAL & 5)!=1 )((void) (0));
55702 assert( (PAGER_JOURNALMODE_DELETE & 5)!=1 )((void) (0));
55703 assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 )((void) (0));
55704 assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 )((void) (0));
55705 if( 0==(iDc & SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN0x00000800)
55706 || 1!=(pPager->journalMode & 5)
55707 ){
55708 sqlite3OsClose(pPager->jfd);
55709 }
55710
55711 /* If the pager is in the ERROR state and the call to unlock the database
55712 ** file fails, set the current lock to UNKNOWN_LOCK. See the comment
55713 ** above the #define for UNKNOWN_LOCK for an explanation of why this
55714 ** is necessary.
55715 */
55716 rc = pagerUnlockDb(pPager, NO_LOCK0);
55717 if( rc!=SQLITE_OK0 && pPager->eState==PAGER_ERROR6 ){
55718 pPager->eLock = UNKNOWN_LOCK(4 +1);
55719 }
55720
55721 /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here
55722 ** without clearing the error code. This is intentional - the error
55723 ** code is cleared and the cache reset in the block below.
55724 */
55725 assert( pPager->errCode || pPager->eState!=PAGER_ERROR )((void) (0));
55726 pPager->eState = PAGER_OPEN0;
55727 }
55728
55729 /* If Pager.errCode is set, the contents of the pager cache cannot be
55730 ** trusted. Now that there are no outstanding references to the pager,
55731 ** it can safely move back to PAGER_OPEN state. This happens in both
55732 ** normal and exclusive-locking mode.
55733 */
55734 assert( pPager->errCode==SQLITE_OK || !MEMDB )((void) (0));
55735 if( pPager->errCode ){
55736 if( pPager->tempFile==0 ){
55737 pager_reset(pPager);
55738 pPager->changeCountDone = 0;
55739 pPager->eState = PAGER_OPEN0;
55740 }else{
55741 pPager->eState = (isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) ? PAGER_OPEN0 : PAGER_READER1);
55742 }
55743 if( USEFETCH(pPager)((pPager)->bUseFetch) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
55744 pPager->errCode = SQLITE_OK0;
55745 setGetterMethod(pPager);
55746 }
55747
55748 pPager->journalOff = 0;
55749 pPager->journalHdr = 0;
55750 pPager->setSuper = 0;
55751}
55752
55753/*
55754** This function is called whenever an IOERR or FULL error that requires
55755** the pager to transition into the ERROR state may ahve occurred.
55756** The first argument is a pointer to the pager structure, the second
55757** the error-code about to be returned by a pager API function. The
55758** value returned is a copy of the second argument to this function.
55759**
55760** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the
55761** IOERR sub-codes, the pager enters the ERROR state and the error code
55762** is stored in Pager.errCode. While the pager remains in the ERROR state,
55763** all major API calls on the Pager will immediately return Pager.errCode.
55764**
55765** The ERROR state indicates that the contents of the pager-cache
55766** cannot be trusted. This state can be cleared by completely discarding
55767** the contents of the pager-cache. If a transaction was active when
55768** the persistent error occurred, then the rollback journal may need
55769** to be replayed to restore the contents of the database file (as if
55770** it were a hot-journal).
55771*/
55772static int pager_error(Pager *pPager, int rc){
55773 int rc2 = rc & 0xff;
55774 assert( rc==SQLITE_OK || !MEMDB )((void) (0));
55775 assert(((void) (0))
55776 pPager->errCode==SQLITE_FULL ||((void) (0))
55777 pPager->errCode==SQLITE_OK ||((void) (0))
55778 (pPager->errCode & 0xff)==SQLITE_IOERR((void) (0))
55779 )((void) (0));
55780 if( rc2==SQLITE_FULL13 || rc2==SQLITE_IOERR10 ){
55781 pPager->errCode = rc;
55782 pPager->eState = PAGER_ERROR6;
55783 setGetterMethod(pPager);
55784 }
55785 return rc;
55786}
55787
55788static int pager_truncate(Pager *pPager, Pgno nPage);
55789
55790/*
55791** The write transaction open on pPager is being committed (bCommit==1)
55792** or rolled back (bCommit==0).
55793**
55794** Return TRUE if and only if all dirty pages should be flushed to disk.
55795**
55796** Rules:
55797**
55798** * For non-TEMP databases, always sync to disk. This is necessary
55799** for transactions to be durable.
55800**
55801** * Sync TEMP database only on a COMMIT (not a ROLLBACK) when the backing
55802** file has been created already (via a spill on pagerStress()) and
55803** when the number of dirty pages in memory exceeds 25% of the total
55804** cache size.
55805*/
55806static int pagerFlushOnCommit(Pager *pPager, int bCommit){
55807 if( pPager->tempFile==0 ) return 1;
55808 if( !bCommit ) return 0;
55809 if( !isOpen(pPager->fd)((pPager->fd)->pMethods!=0) ) return 0;
55810 return (sqlite3PCachePercentDirty(pPager->pPCache)>=25);
55811}
55812
55813/*
55814** This routine ends a transaction. A transaction is usually ended by
55815** either a COMMIT or a ROLLBACK operation. This routine may be called
55816** after rollback of a hot-journal, or if an error occurs while opening
55817** the journal file or writing the very first journal-header of a
55818** database transaction.
55819**
55820** This routine is never called in PAGER_ERROR state. If it is called
55821** in PAGER_NONE or PAGER_SHARED state and the lock held is less
55822** exclusive than a RESERVED lock, it is a no-op.
55823**
55824** Otherwise, any active savepoints are released.
55825**
55826** If the journal file is open, then it is "finalized". Once a journal
55827** file has been finalized it is not possible to use it to roll back a
55828** transaction. Nor will it be considered to be a hot-journal by this
55829** or any other database connection. Exactly how a journal is finalized
55830** depends on whether or not the pager is running in exclusive mode and
55831** the current journal-mode (Pager.journalMode value), as follows:
55832**
55833** journalMode==MEMORY
55834** Journal file descriptor is simply closed. This destroys an
55835** in-memory journal.
55836**
55837** journalMode==TRUNCATE
55838** Journal file is truncated to zero bytes in size.
55839**
55840** journalMode==PERSIST
55841** The first 28 bytes of the journal file are zeroed. This invalidates
55842** the first journal header in the file, and hence the entire journal
55843** file. An invalid journal file cannot be rolled back.
55844**
55845** journalMode==DELETE
55846** The journal file is closed and deleted using sqlite3OsDelete().
55847**
55848** If the pager is running in exclusive mode, this method of finalizing
55849** the journal file is never used. Instead, if the journalMode is
55850** DELETE and the pager is in exclusive mode, the method described under
55851** journalMode==PERSIST is used instead.
55852**
55853** After the journal is finalized, the pager moves to PAGER_READER state.
55854** If running in non-exclusive rollback mode, the lock on the file is
55855** downgraded to a SHARED_LOCK.
55856**
55857** SQLITE_OK is returned if no error occurs. If an error occurs during
55858** any of the IO operations to finalize the journal file or unlock the
55859** database then the IO error code is returned to the user. If the
55860** operation to finalize the journal file fails, then the code still
55861** tries to unlock the database file if not in exclusive mode. If the
55862** unlock operation fails as well, then the first error code related
55863** to the first error encountered (the journal finalization one) is
55864** returned.
55865*/
55866static int pager_end_transaction(Pager *pPager, int hasSuper, int bCommit){
55867 int rc = SQLITE_OK0; /* Error code from journal finalization operation */
55868 int rc2 = SQLITE_OK0; /* Error code from db file unlock operation */
55869
55870 /* Do nothing if the pager does not have an open write transaction
55871 ** or at least a RESERVED lock. This function may be called when there
55872 ** is no write-transaction active but a RESERVED or greater lock is
55873 ** held under two circumstances:
55874 **
55875 ** 1. After a successful hot-journal rollback, it is called with
55876 ** eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK.
55877 **
55878 ** 2. If a connection with locking_mode=exclusive holding an EXCLUSIVE
55879 ** lock switches back to locking_mode=normal and then executes a
55880 ** read-transaction, this function is called with eState==PAGER_READER
55881 ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
55882 */
55883 assert( assert_pager_state(pPager) )((void) (0));
55884 assert( pPager->eState!=PAGER_ERROR )((void) (0));
55885 if( pPager->eState<PAGER_WRITER_LOCKED2 && pPager->eLock<RESERVED_LOCK2 ){
55886 return SQLITE_OK0;
55887 }
55888
55889 releaseAllSavepoints(pPager);
55890 assert( isOpen(pPager->jfd) || pPager->pInJournal==0((void) (0))
55891 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_BATCH_ATOMIC)((void) (0))
55892 )((void) (0));
55893 if( isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) ){
55894 assert( !pagerUseWal(pPager) )((void) (0));
55895
55896 /* Finalize the journal file. */
55897 if( sqlite3JournalIsInMemory(pPager->jfd) ){
55898 /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */
55899 sqlite3OsClose(pPager->jfd);
55900 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE3 ){
55901 if( pPager->journalOff==0 ){
55902 rc = SQLITE_OK0;
55903 }else{
55904 rc = sqlite3OsTruncate(pPager->jfd, 0);
55905 if( rc==SQLITE_OK0 && pPager->fullSync ){
55906 /* Make sure the new file size is written into the inode right away.
55907 ** Otherwise the journal might resurrect following a power loss and
55908 ** cause the last transaction to roll back. See
55909 ** https://bugzilla.mozilla.org/show_bug.cgi?id=1072773
55910 */
55911 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
55912 }
55913 }
55914 pPager->journalOff = 0;
55915 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST1
55916 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL5)
55917 ){
55918 rc = zeroJournalHdr(pPager, hasSuper||pPager->tempFile);
55919 pPager->journalOff = 0;
55920 }else{
55921 /* This branch may be executed with Pager.journalMode==MEMORY if
55922 ** a hot-journal was just rolled back. In this case the journal
55923 ** file should be closed and deleted. If this connection writes to
55924 ** the database file, it will do so using an in-memory journal.
55925 */
55926 int bDelete = !pPager->tempFile;
55927 assert( sqlite3JournalIsInMemory(pPager->jfd)==0 )((void) (0));
55928 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE((void) (0))
55929 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY((void) (0))
55930 || pPager->journalMode==PAGER_JOURNALMODE_WAL((void) (0))
55931 )((void) (0));
55932 sqlite3OsClose(pPager->jfd);
55933 if( bDelete ){
55934 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync);
55935 }
55936 }
55937 }
55938
55939#ifdef SQLITE_CHECK_PAGES
55940 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
55941 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
55942 PgHdr *p = sqlite3PagerLookup(pPager, 1);
55943 if( p ){
55944 p->pageHash = 0;
55945 sqlite3PagerUnrefNotNull(p);
55946 }
55947 }
55948#endif
55949
55950 sqlite3BitvecDestroy(pPager->pInJournal);
55951 pPager->pInJournal = 0;
55952 pPager->nRec = 0;
55953 if( rc==SQLITE_OK0 ){
55954 if( MEMDBpPager->memDb || pagerFlushOnCommit(pPager, bCommit) ){
55955 sqlite3PcacheCleanAll(pPager->pPCache);
55956 }else{
55957 sqlite3PcacheClearWritable(pPager->pPCache);
55958 }
55959 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
55960 }
55961
55962 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
55963 /* Drop the WAL write-lock, if any. Also, if the connection was in
55964 ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE
55965 ** lock held on the database file.
55966 */
55967 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
55968 assert( rc2==SQLITE_OK )((void) (0));
55969 }else if( rc==SQLITE_OK0 && bCommit && pPager->dbFileSize>pPager->dbSize ){
55970 /* This branch is taken when committing a transaction in rollback-journal
55971 ** mode if the database file on disk is larger than the database image.
55972 ** At this point the journal has been finalized and the transaction
55973 ** successfully committed, but the EXCLUSIVE lock is still held on the
55974 ** file. So it is safe to truncate the database file to its minimum
55975 ** required size. */
55976 assert( pPager->eLock==EXCLUSIVE_LOCK )((void) (0));
55977 rc = pager_truncate(pPager, pPager->dbSize);
55978 }
55979
55980 if( rc==SQLITE_OK0 && bCommit ){
55981 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO22, 0);
55982 if( rc==SQLITE_NOTFOUND12 ) rc = SQLITE_OK0;
55983 }
55984
55985 if( !pPager->exclusiveMode
55986 && (!pagerUseWal(pPager)((pPager)->pWal!=0) || sqlite3WalExclusiveMode(pPager->pWal, 0))
55987 ){
55988 rc2 = pagerUnlockDb(pPager, SHARED_LOCK1);
55989 }
55990 pPager->eState = PAGER_READER1;
55991 pPager->setSuper = 0;
55992
55993 return (rc==SQLITE_OK0?rc2:rc);
55994}
55995
55996/*
55997** Execute a rollback if a transaction is active and unlock the
55998** database file.
55999**
56000** If the pager has already entered the ERROR state, do not attempt
56001** the rollback at this time. Instead, pager_unlock() is called. The
56002** call to pager_unlock() will discard all in-memory pages, unlock
56003** the database file and move the pager back to OPEN state. If this
56004** means that there is a hot-journal left in the file-system, the next
56005** connection to obtain a shared lock on the pager (which may be this one)
56006** will roll it back.
56007**
56008** If the pager has not already entered the ERROR state, but an IO or
56009** malloc error occurs during a rollback, then this will itself cause
56010** the pager to enter the ERROR state. Which will be cleared by the
56011** call to pager_unlock(), as described above.
56012*/
56013static void pagerUnlockAndRollback(Pager *pPager){
56014 if( pPager->eState!=PAGER_ERROR6 && pPager->eState!=PAGER_OPEN0 ){
56015 assert( assert_pager_state(pPager) )((void) (0));
56016 if( pPager->eState>=PAGER_WRITER_LOCKED2 ){
56017 sqlite3BeginBenignMalloc();
56018 sqlite3PagerRollback(pPager);
56019 sqlite3EndBenignMalloc();
56020 }else if( !pPager->exclusiveMode ){
56021 assert( pPager->eState==PAGER_READER )((void) (0));
56022 pager_end_transaction(pPager, 0, 0);
56023 }
56024 }
56025 pager_unlock(pPager);
56026}
56027
56028/*
56029** Parameter aData must point to a buffer of pPager->pageSize bytes
56030** of data. Compute and return a checksum based ont the contents of the
56031** page of data and the current value of pPager->cksumInit.
56032**
56033** This is not a real checksum. It is really just the sum of the
56034** random initial value (pPager->cksumInit) and every 200th byte
56035** of the page data, starting with byte offset (pPager->pageSize%200).
56036** Each byte is interpreted as an 8-bit unsigned integer.
56037**
56038** Changing the formula used to compute this checksum results in an
56039** incompatible journal file format.
56040**
56041** If journal corruption occurs due to a power failure, the most likely
56042** scenario is that one end or the other of the record will be changed.
56043** It is much less likely that the two ends of the journal record will be
56044** correct and the middle be corrupt. Thus, this "checksum" scheme,
56045** though fast and simple, catches the mostly likely kind of corruption.
56046*/
56047static u32 pager_cksum(Pager *pPager, const u8 *aData){
56048 u32 cksum = pPager->cksumInit; /* Checksum value to return */
56049 int i = pPager->pageSize-200; /* Loop counter */
56050 while( i>0 ){
56051 cksum += aData[i];
56052 i -= 200;
56053 }
56054 return cksum;
56055}
56056
56057/*
56058** Read a single page from either the journal file (if isMainJrnl==1) or
56059** from the sub-journal (if isMainJrnl==0) and playback that page.
56060** The page begins at offset *pOffset into the file. The *pOffset
56061** value is increased to the start of the next page in the journal.
56062**
56063** The main rollback journal uses checksums - the statement journal does
56064** not.
56065**
56066** If the page number of the page record read from the (sub-)journal file
56067** is greater than the current value of Pager.dbSize, then playback is
56068** skipped and SQLITE_OK is returned.
56069**
56070** If pDone is not NULL, then it is a record of pages that have already
56071** been played back. If the page at *pOffset has already been played back
56072** (if the corresponding pDone bit is set) then skip the playback.
56073** Make sure the pDone bit corresponding to the *pOffset page is set
56074** prior to returning.
56075**
56076** If the page record is successfully read from the (sub-)journal file
56077** and played back, then SQLITE_OK is returned. If an IO error occurs
56078** while reading the record from the (sub-)journal file or while writing
56079** to the database file, then the IO error code is returned. If data
56080** is successfully read from the (sub-)journal file but appears to be
56081** corrupted, SQLITE_DONE is returned. Data is considered corrupted in
56082** two circumstances:
56083**
56084** * If the record page-number is illegal (0 or PAGER_SJ_PGNO), or
56085** * If the record is being rolled back from the main journal file
56086** and the checksum field does not match the record content.
56087**
56088** Neither of these two scenarios are possible during a savepoint rollback.
56089**
56090** If this is a savepoint rollback, then memory may have to be dynamically
56091** allocated by this function. If this is the case and an allocation fails,
56092** SQLITE_NOMEM is returned.
56093*/
56094static int pager_playback_one_page(
56095 Pager *pPager, /* The pager being played back */
56096 i64 *pOffset, /* Offset of record to playback */
56097 Bitvec *pDone, /* Bitvec of pages already played back */
56098 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
56099 int isSavepnt /* True for a savepoint rollback */
56100){
56101 int rc;
56102 PgHdr *pPg; /* An existing page in the cache */
56103 Pgno pgno; /* The page number of a page in journal */
56104 u32 cksum; /* Checksum used for sanity checking */
56105 char *aData; /* Temporary storage for the page */
56106 sqlite3_file *jfd; /* The file descriptor for the journal file */
56107 int isSynced; /* True if journal page is synced */
56108
56109 assert( (isMainJrnl&~1)==0 )((void) (0)); /* isMainJrnl is 0 or 1 */
56110 assert( (isSavepnt&~1)==0 )((void) (0)); /* isSavepnt is 0 or 1 */
56111 assert( isMainJrnl || pDone )((void) (0)); /* pDone always used on sub-journals */
56112 assert( isSavepnt || pDone==0 )((void) (0)); /* pDone never used on non-savepoint */
56113
56114 aData = pPager->pTmpSpace;
56115 assert( aData )((void) (0)); /* Temp storage must have already been allocated */
56116 assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) )((void) (0));
56117
56118 /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction
56119 ** or savepoint rollback done at the request of the caller) or this is
56120 ** a hot-journal rollback. If it is a hot-journal rollback, the pager
56121 ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
56122 ** only reads from the main journal, not the sub-journal.
56123 */
56124 assert( pPager->eState>=PAGER_WRITER_CACHEMOD((void) (0))
56125 || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)((void) (0))
56126 )((void) (0));
56127 assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl )((void) (0));
56128
56129 /* Read the page number and page data from the journal or sub-journal
56130 ** file. Return an error code to the caller if an IO error occurs.
56131 */
56132 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
56133 rc = read32bits(jfd, *pOffset, &pgno);
56134 if( rc!=SQLITE_OK0 ) return rc;
56135 rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
56136 if( rc!=SQLITE_OK0 ) return rc;
56137 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
56138
56139 /* Sanity checking on the page. This is more important that I originally
56140 ** thought. If a power failure occurs while the journal is being written,
56141 ** it could cause invalid data to be written into the journal. We need to
56142 ** detect this invalid data (with high probability) and ignore it.
56143 */
56144 if( pgno==0 || pgno==PAGER_SJ_PGNO(pPager)((pPager)->lckPgno) ){
56145 assert( !isSavepnt )((void) (0));
56146 return SQLITE_DONE101;
56147 }
56148 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
56149 return SQLITE_OK0;
56150 }
56151 if( isMainJrnl ){
56152 rc = read32bits(jfd, (*pOffset)-4, &cksum);
56153 if( rc ) return rc;
56154 if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){
56155 return SQLITE_DONE101;
56156 }
56157 }
56158
56159 /* If this page has already been played back before during the current
56160 ** rollback, then don't bother to play it back again.
56161 */
56162 if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK0 ){
56163 return rc;
56164 }
56165
56166 /* When playing back page 1, restore the nReserve setting
56167 */
56168 if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
56169 pPager->nReserve = ((u8*)aData)[20];
56170 }
56171
56172 /* If the pager is in CACHEMOD state, then there must be a copy of this
56173 ** page in the pager cache. In this case just update the pager cache,
56174 ** not the database file. The page is left marked dirty in this case.
56175 **
56176 ** An exception to the above rule: If the database is in no-sync mode
56177 ** and a page is moved during an incremental vacuum then the page may
56178 ** not be in the pager cache. Later: if a malloc() or IO error occurs
56179 ** during a Movepage() call, then the page may not be in the cache
56180 ** either. So the condition described in the above paragraph is not
56181 ** assert()able.
56182 **
56183 ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the
56184 ** pager cache if it exists and the main file. The page is then marked
56185 ** not dirty. Since this code is only executed in PAGER_OPEN state for
56186 ** a hot-journal rollback, it is guaranteed that the page-cache is empty
56187 ** if the pager is in OPEN state.
56188 **
56189 ** Ticket #1171: The statement journal might contain page content that is
56190 ** different from the page content at the start of the transaction.
56191 ** This occurs when a page is changed prior to the start of a statement
56192 ** then changed again within the statement. When rolling back such a
56193 ** statement we must not write to the original database unless we know
56194 ** for certain that original page contents are synced into the main rollback
56195 ** journal. Otherwise, a power loss might leave modified data in the
56196 ** database file without an entry in the rollback journal that can
56197 ** restore the database to its original form. Two conditions must be
56198 ** met before writing to the database files. (1) the database must be
56199 ** locked. (2) we know that the original page content is fully synced
56200 ** in the main journal either because the page is not in cache or else
56201 ** the page is marked as needSync==0.
56202 **
56203 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
56204 ** is possible to fail a statement on a database that does not yet exist.
56205 ** Do not attempt to write if database file has never been opened.
56206 */
56207 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
56208 pPg = 0;
56209 }else{
56210 pPg = sqlite3PagerLookup(pPager, pgno);
56211 }
56212 assert( pPg || !MEMDB )((void) (0));
56213 assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile )((void) (0));
56214 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
56215 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
56216 (isMainJrnl?"main-journal":"sub-journal")
56217 ));
56218 if( isMainJrnl ){
56219 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
56220 }else{
56221 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC0x008));
56222 }
56223 if( isOpen(pPager->fd)((pPager->fd)->pMethods!=0)
56224 && (pPager->eState>=PAGER_WRITER_DBMOD4 || pPager->eState==PAGER_OPEN0)
56225 && isSynced
56226 ){
56227 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
56228 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
56229 assert( !pagerUseWal(pPager) )((void) (0));
56230
56231 /* Write the data read from the journal back into the database file.
56232 ** This is usually safe even for an encrypted database - as the data
56233 ** was encrypted before it was written to the journal file. The exception
56234 ** is if the data was just read from an in-memory sub-journal. In that
56235 ** case it must be encrypted here before it is copied into the database
56236 ** file. */
56237 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
56238
56239 if( pgno>pPager->dbFileSize ){
56240 pPager->dbFileSize = pgno;
56241 }
56242 if( pPager->pBackup ){
56243 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
56244 }
56245 }else if( !isMainJrnl && pPg==0 ){
56246 /* If this is a rollback of a savepoint and data was not written to
56247 ** the database and the page is not in-memory, there is a potential
56248 ** problem. When the page is next fetched by the b-tree layer, it
56249 ** will be read from the database file, which may or may not be
56250 ** current.
56251 **
56252 ** There are a couple of different ways this can happen. All are quite
56253 ** obscure. When running in synchronous mode, this can only happen
56254 ** if the page is on the free-list at the start of the transaction, then
56255 ** populated, then moved using sqlite3PagerMovepage().
56256 **
56257 ** The solution is to add an in-memory page to the cache containing
56258 ** the data just read from the sub-journal. Mark the page as dirty
56259 ** and if the pager requires a journal-sync, then mark the page as
56260 ** requiring a journal-sync before it is written.
56261 */
56262 assert( isSavepnt )((void) (0));
56263 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 )((void) (0));
56264 pPager->doNotSpill |= SPILLFLAG_ROLLBACK0x02;
56265 rc = sqlite3PagerGet(pPager, pgno, &pPg, 1);
56266 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 )((void) (0));
56267 pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK0x02;
56268 if( rc!=SQLITE_OK0 ) return rc;
56269 sqlite3PcacheMakeDirty(pPg);
56270 }
56271 if( pPg ){
56272 /* No page should ever be explicitly rolled back that is in use, except
56273 ** for page 1 which is held in use in order to keep the lock on the
56274 ** database active. However such a page may be rolled back as a result
56275 ** of an internal error resulting in an automatic call to
56276 ** sqlite3PagerRollback().
56277 */
56278 void *pData;
56279 pData = pPg->pData;
56280 memcpy(pData, (u8*)aData, pPager->pageSize);
56281 pPager->xReiniter(pPg);
56282 /* It used to be that sqlite3PcacheMakeClean(pPg) was called here. But
56283 ** that call was dangerous and had no detectable benefit since the cache
56284 ** is normally cleaned by sqlite3PcacheCleanAll() after rollback and so
56285 ** has been removed. */
56286 pager_set_pagehash(pPg);
56287
56288 /* If this was page 1, then restore the value of Pager.dbFileVers.
56289 ** Do this before any decoding. */
56290 if( pgno==1 ){
56291 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
56292 }
56293 sqlite3PcacheRelease(pPg);
56294 }
56295 return rc;
56296}
56297
56298/*
56299** Parameter zSuper is the name of a super-journal file. A single journal
56300** file that referred to the super-journal file has just been rolled back.
56301** This routine checks if it is possible to delete the super-journal file,
56302** and does so if it is.
56303**
56304** Argument zSuper may point to Pager.pTmpSpace. So that buffer is not
56305** available for use within this function.
56306**
56307** When a super-journal file is created, it is populated with the names
56308** of all of its child journals, one after another, formatted as utf-8
56309** encoded text. The end of each child journal file is marked with a
56310** nul-terminator byte (0x00). i.e. the entire contents of a super-journal
56311** file for a transaction involving two databases might be:
56312**
56313** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
56314**
56315** A super-journal file may only be deleted once all of its child
56316** journals have been rolled back.
56317**
56318** This function reads the contents of the super-journal file into
56319** memory and loops through each of the child journal names. For
56320** each child journal, it checks if:
56321**
56322** * if the child journal exists, and if so
56323** * if the child journal contains a reference to super-journal
56324** file zSuper
56325**
56326** If a child journal can be found that matches both of the criteria
56327** above, this function returns without doing anything. Otherwise, if
56328** no such child journal can be found, file zSuper is deleted from
56329** the file-system using sqlite3OsDelete().
56330**
56331** If an IO error within this function, an error code is returned. This
56332** function allocates memory by calling sqlite3Malloc(). If an allocation
56333** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors
56334** occur, SQLITE_OK is returned.
56335**
56336** TODO: This function allocates a single block of memory to load
56337** the entire contents of the super-journal file. This could be
56338** a couple of kilobytes or so - potentially larger than the page
56339** size.
56340*/
56341static int pager_delsuper(Pager *pPager, const char *zSuper){
56342 sqlite3_vfs *pVfs = pPager->pVfs;
56343 int rc; /* Return code */
56344 sqlite3_file *pSuper; /* Malloc'd super-journal file descriptor */
56345 sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
56346 char *zSuperJournal = 0; /* Contents of super-journal file */
56347 i64 nSuperJournal; /* Size of super-journal file */
56348 char *zJournal; /* Pointer to one journal within MJ file */
56349 char *zSuperPtr; /* Space to hold super-journal filename */
56350 char *zFree = 0; /* Free this buffer */
56351 int nSuperPtr; /* Amount of space allocated to zSuperPtr[] */
56352
56353 /* Allocate space for both the pJournal and pSuper file descriptors.
56354 ** If successful, open the super-journal file for reading.
56355 */
56356 pSuper = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
56357 if( !pSuper ){
56358 rc = SQLITE_NOMEM_BKPT7;
56359 pJournal = 0;
56360 }else{
56361 const int flags = (SQLITE_OPEN_READONLY0x00000001|SQLITE_OPEN_SUPER_JOURNAL0x00004000);
56362 rc = sqlite3OsOpen(pVfs, zSuper, pSuper, flags, 0);
56363 pJournal = (sqlite3_file *)(((u8 *)pSuper) + pVfs->szOsFile);
56364 }
56365 if( rc!=SQLITE_OK0 ) goto delsuper_out;
56366
56367 /* Load the entire super-journal file into space obtained from
56368 ** sqlite3_malloc() and pointed to by zSuperJournal. Also obtain
56369 ** sufficient space (in zSuperPtr) to hold the names of super-journal
56370 ** files extracted from regular rollback-journals.
56371 */
56372 rc = sqlite3OsFileSize(pSuper, &nSuperJournal);
56373 if( rc!=SQLITE_OK0 ) goto delsuper_out;
56374 nSuperPtr = pVfs->mxPathname+1;
56375 zFree = sqlite3Malloc(4 + nSuperJournal + nSuperPtr + 2);
56376 if( !zFree ){
56377 rc = SQLITE_NOMEM_BKPT7;
56378 goto delsuper_out;
56379 }
56380 zFree[0] = zFree[1] = zFree[2] = zFree[3] = 0;
56381 zSuperJournal = &zFree[4];
56382 zSuperPtr = &zSuperJournal[nSuperJournal+2];
56383 rc = sqlite3OsRead(pSuper, zSuperJournal, (int)nSuperJournal, 0);
56384 if( rc!=SQLITE_OK0 ) goto delsuper_out;
56385 zSuperJournal[nSuperJournal] = 0;
56386 zSuperJournal[nSuperJournal+1] = 0;
56387
56388 zJournal = zSuperJournal;
56389 while( (zJournal-zSuperJournal)<nSuperJournal ){
56390 int exists;
56391 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS0, &exists);
56392 if( rc!=SQLITE_OK0 ){
56393 goto delsuper_out;
56394 }
56395 if( exists ){
56396 /* One of the journals pointed to by the super-journal exists.
56397 ** Open it and check if it points at the super-journal. If
56398 ** so, return without deleting the super-journal file.
56399 ** NB: zJournal is really a MAIN_JOURNAL. But call it a
56400 ** SUPER_JOURNAL here so that the VFS will not send the zJournal
56401 ** name into sqlite3_database_file_object().
56402 */
56403 int c;
56404 int flags = (SQLITE_OPEN_READONLY0x00000001|SQLITE_OPEN_SUPER_JOURNAL0x00004000);
56405 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
56406 if( rc!=SQLITE_OK0 ){
56407 goto delsuper_out;
56408 }
56409
56410 rc = readSuperJournal(pJournal, zSuperPtr, nSuperPtr);
56411 sqlite3OsClose(pJournal);
56412 if( rc!=SQLITE_OK0 ){
56413 goto delsuper_out;
56414 }
56415
56416 c = zSuperPtr[0]!=0 && strcmp(zSuperPtr, zSuper)==0;
56417 if( c ){
56418 /* We have a match. Do not delete the super-journal file. */
56419 goto delsuper_out;
56420 }
56421 }
56422 zJournal += (sqlite3Strlen30(zJournal)+1);
56423 }
56424
56425 sqlite3OsClose(pSuper);
56426 rc = sqlite3OsDelete(pVfs, zSuper, 0);
56427
56428delsuper_out:
56429 sqlite3_free(zFree);
56430 if( pSuper ){
56431 sqlite3OsClose(pSuper);
56432 assert( !isOpen(pJournal) )((void) (0));
56433 sqlite3_free(pSuper);
56434 }
56435 return rc;
56436}
56437
56438
56439/*
56440** This function is used to change the actual size of the database
56441** file in the file-system. This only happens when committing a transaction,
56442** or rolling back a transaction (including rolling back a hot-journal).
56443**
56444** If the main database file is not open, or the pager is not in either
56445** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
56446** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
56447** If the file on disk is currently larger than nPage pages, then use the VFS
56448** xTruncate() method to truncate it.
56449**
56450** Or, it might be the case that the file on disk is smaller than
56451** nPage pages. Some operating system implementations can get confused if
56452** you try to truncate a file to some size that is larger than it
56453** currently is, so detect this case and write a single zero byte to
56454** the end of the new file instead.
56455**
56456** If successful, return SQLITE_OK. If an IO error occurs while modifying
56457** the database file, return the error code to the caller.
56458*/
56459static int pager_truncate(Pager *pPager, Pgno nPage){
56460 int rc = SQLITE_OK0;
56461 assert( pPager->eState!=PAGER_ERROR )((void) (0));
56462 assert( pPager->eState!=PAGER_READER )((void) (0));
56463
56464 if( isOpen(pPager->fd)((pPager->fd)->pMethods!=0)
56465 && (pPager->eState>=PAGER_WRITER_DBMOD4 || pPager->eState==PAGER_OPEN0)
56466 ){
56467 i64 currentSize, newSize;
56468 int szPage = pPager->pageSize;
56469 assert( pPager->eLock==EXCLUSIVE_LOCK )((void) (0));
56470 /* TODO: Is it safe to use Pager.dbFileSize here? */
56471 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
56472 newSize = szPage*(i64)nPage;
56473 if( rc==SQLITE_OK0 && currentSize!=newSize ){
56474 if( currentSize>newSize ){
56475 rc = sqlite3OsTruncate(pPager->fd, newSize);
56476 }else if( (currentSize+szPage)<=newSize ){
56477 char *pTmp = pPager->pTmpSpace;
56478 memset(pTmp, 0, szPage);
56479 testcase( (newSize-szPage) == currentSize );
56480 testcase( (newSize-szPage) > currentSize );
56481 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT5, &newSize);
56482 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
56483 }
56484 if( rc==SQLITE_OK0 ){
56485 pPager->dbFileSize = nPage;
56486 }
56487 }
56488 }
56489 return rc;
56490}
56491
56492/*
56493** Return a sanitized version of the sector-size of OS file pFile. The
56494** return value is guaranteed to lie between 32 and MAX_SECTOR_SIZE.
56495*/
56496SQLITE_PRIVATEstatic int sqlite3SectorSize(sqlite3_file *pFile){
56497 int iRet = sqlite3OsSectorSize(pFile);
56498 if( iRet<32 ){
56499 iRet = 512;
56500 }else if( iRet>MAX_SECTOR_SIZE0x10000 ){
56501 assert( MAX_SECTOR_SIZE>=512 )((void) (0));
56502 iRet = MAX_SECTOR_SIZE0x10000;
56503 }
56504 return iRet;
56505}
56506
56507/*
56508** Set the value of the Pager.sectorSize variable for the given
56509** pager based on the value returned by the xSectorSize method
56510** of the open database file. The sector size will be used
56511** to determine the size and alignment of journal header and
56512** super-journal pointers within created journal files.
56513**
56514** For temporary files the effective sector size is always 512 bytes.
56515**
56516** Otherwise, for non-temporary files, the effective sector size is
56517** the value returned by the xSectorSize() method rounded up to 32 if
56518** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it
56519** is greater than MAX_SECTOR_SIZE.
56520**
56521** If the file has the SQLITE_IOCAP_POWERSAFE_OVERWRITE property, then set
56522** the effective sector size to its minimum value (512). The purpose of
56523** pPager->sectorSize is to define the "blast radius" of bytes that
56524** might change if a crash occurs while writing to a single byte in
56525** that range. But with POWERSAFE_OVERWRITE, the blast radius is zero
56526** (that is what POWERSAFE_OVERWRITE means), so we minimize the sector
56527** size. For backwards compatibility of the rollback journal file format,
56528** we cannot reduce the effective sector size below 512.
56529*/
56530static void setSectorSize(Pager *pPager){
56531 assert( isOpen(pPager->fd) || pPager->tempFile )((void) (0));
56532
56533 if( pPager->tempFile
56534 || (sqlite3OsDeviceCharacteristics(pPager->fd) &
56535 SQLITE_IOCAP_POWERSAFE_OVERWRITE0x00001000)!=0
56536 ){
56537 /* Sector size doesn't matter for temporary files. Also, the file
56538 ** may not have been opened yet, in which case the OsSectorSize()
56539 ** call will segfault. */
56540 pPager->sectorSize = 512;
56541 }else{
56542 pPager->sectorSize = sqlite3SectorSize(pPager->fd);
56543 }
56544}
56545
56546/*
56547** Playback the journal and thus restore the database file to
56548** the state it was in before we started making changes.
56549**
56550** The journal file format is as follows:
56551**
56552** (1) 8 byte prefix. A copy of aJournalMagic[].
56553** (2) 4 byte big-endian integer which is the number of valid page records
56554** in the journal. If this value is 0xffffffff, then compute the
56555** number of page records from the journal size.
56556** (3) 4 byte big-endian integer which is the initial value for the
56557** sanity checksum.
56558** (4) 4 byte integer which is the number of pages to truncate the
56559** database to during a rollback.
56560** (5) 4 byte big-endian integer which is the sector size. The header
56561** is this many bytes in size.
56562** (6) 4 byte big-endian integer which is the page size.
56563** (7) zero padding out to the next sector size.
56564** (8) Zero or more pages instances, each as follows:
56565** + 4 byte page number.
56566** + pPager->pageSize bytes of data.
56567** + 4 byte checksum
56568**
56569** When we speak of the journal header, we mean the first 7 items above.
56570** Each entry in the journal is an instance of the 8th item.
56571**
56572** Call the value from the second bullet "nRec". nRec is the number of
56573** valid page entries in the journal. In most cases, you can compute the
56574** value of nRec from the size of the journal file. But if a power
56575** failure occurred while the journal was being written, it could be the
56576** case that the size of the journal file had already been increased but
56577** the extra entries had not yet made it safely to disk. In such a case,
56578** the value of nRec computed from the file size would be too large. For
56579** that reason, we always use the nRec value in the header.
56580**
56581** If the nRec value is 0xffffffff it means that nRec should be computed
56582** from the file size. This value is used when the user selects the
56583** no-sync option for the journal. A power failure could lead to corruption
56584** in this case. But for things like temporary table (which will be
56585** deleted when the power is restored) we don't care.
56586**
56587** If the file opened as the journal file is not a well-formed
56588** journal file then all pages up to the first corrupted page are rolled
56589** back (or no pages if the journal header is corrupted). The journal file
56590** is then deleted and SQLITE_OK returned, just as if no corruption had
56591** been encountered.
56592**
56593** If an I/O or malloc() error occurs, the journal-file is not deleted
56594** and an error code is returned.
56595**
56596** The isHot parameter indicates that we are trying to rollback a journal
56597** that might be a hot journal. Or, it could be that the journal is
56598** preserved because of JOURNALMODE_PERSIST or JOURNALMODE_TRUNCATE.
56599** If the journal really is hot, reset the pager cache prior rolling
56600** back any content. If the journal is merely persistent, no reset is
56601** needed.
56602*/
56603static int pager_playback(Pager *pPager, int isHot){
56604 sqlite3_vfs *pVfs = pPager->pVfs;
56605 i64 szJ; /* Size of the journal file in bytes */
56606 u32 nRec; /* Number of Records in the journal */
56607 u32 u; /* Unsigned loop counter */
56608 Pgno mxPg = 0; /* Size of the original file in pages */
56609 int rc; /* Result code of a subroutine */
56610 int res = 1; /* Value returned by sqlite3OsAccess() */
56611 char *zSuper = 0; /* Name of super-journal file if any */
56612 int needPagerReset; /* True to reset page prior to first page rollback */
56613 int nPlayback = 0; /* Total number of pages restored from journal */
56614 u32 savedPageSize = pPager->pageSize;
56615
56616 /* Figure out how many records are in the journal. Abort early if
56617 ** the journal is empty.
56618 */
56619 assert( isOpen(pPager->jfd) )((void) (0));
56620 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
56621 if( rc!=SQLITE_OK0 ){
56622 goto end_playback;
56623 }
56624
56625 /* Read the super-journal name from the journal, if it is present.
56626 ** If a super-journal file name is specified, but the file is not
56627 ** present on disk, then the journal is not hot and does not need to be
56628 ** played back.
56629 **
56630 ** TODO: Technically the following is an error because it assumes that
56631 ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
56632 ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
56633 ** mxPathname is 512, which is the same as the minimum allowable value
56634 ** for pageSize.
56635 */
56636 zSuper = pPager->pTmpSpace;
56637 rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
56638 if( rc==SQLITE_OK0 && zSuper[0] ){
56639 rc = sqlite3OsAccess(pVfs, zSuper, SQLITE_ACCESS_EXISTS0, &res);
56640 }
56641 zSuper = 0;
56642 if( rc!=SQLITE_OK0 || !res ){
56643 goto end_playback;
56644 }
56645 pPager->journalOff = 0;
56646 needPagerReset = isHot;
56647
56648 /* This loop terminates either when a readJournalHdr() or
56649 ** pager_playback_one_page() call returns SQLITE_DONE or an IO error
56650 ** occurs.
56651 */
56652 while( 1 ){
56653 /* Read the next journal header from the journal file. If there are
56654 ** not enough bytes left in the journal file for a complete header, or
56655 ** it is corrupted, then a process must have failed while writing it.
56656 ** This indicates nothing more needs to be rolled back.
56657 */
56658 rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
56659 if( rc!=SQLITE_OK0 ){
56660 if( rc==SQLITE_DONE101 ){
56661 rc = SQLITE_OK0;
56662 }
56663 goto end_playback;
56664 }
56665
56666 /* If nRec is 0xffffffff, then this journal was created by a process
56667 ** working in no-sync mode. This means that the rest of the journal
56668 ** file consists of pages, there are no more journal headers. Compute
56669 ** the value of nRec based on this assumption.
56670 */
56671 if( nRec==0xffffffff ){
56672 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) )((void) (0));
56673 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager)(pPager->sectorSize))/JOURNAL_PG_SZ(pPager)((pPager->pageSize) + 8));
56674 }
56675
56676 /* If nRec is 0 and this rollback is of a transaction created by this
56677 ** process and if this is the final header in the journal, then it means
56678 ** that this part of the journal was being filled but has not yet been
56679 ** synced to disk. Compute the number of pages based on the remaining
56680 ** size of the file.
56681 **
56682 ** The third term of the test was added to fix ticket #2565.
56683 ** When rolling back a hot journal, nRec==0 always means that the next
56684 ** chunk of the journal contains zero pages to be rolled back. But
56685 ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
56686 ** the journal, it means that the journal might contain additional
56687 ** pages that need to be rolled back and that the number of pages
56688 ** should be computed based on the journal file size.
56689 */
56690 if( nRec==0 && !isHot &&
56691 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)(pPager->sectorSize)==pPager->journalOff ){
56692 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager)((pPager->pageSize) + 8));
56693 }
56694
56695 /* If this is the first header read from the journal, truncate the
56696 ** database file back to its original size.
56697 */
56698 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager)(pPager->sectorSize) ){
56699 rc = pager_truncate(pPager, mxPg);
56700 if( rc!=SQLITE_OK0 ){
56701 goto end_playback;
56702 }
56703 pPager->dbSize = mxPg;
56704 if( pPager->mxPgno<mxPg ){
56705 pPager->mxPgno = mxPg;
56706 }
56707 }
56708
56709 /* Copy original pages out of the journal and back into the
56710 ** database file and/or page cache.
56711 */
56712 for(u=0; u<nRec; u++){
56713 if( needPagerReset ){
56714 pager_reset(pPager);
56715 needPagerReset = 0;
56716 }
56717 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
56718 if( rc==SQLITE_OK0 ){
56719 nPlayback++;
56720 }else{
56721 if( rc==SQLITE_DONE101 ){
56722 pPager->journalOff = szJ;
56723 break;
56724 }else if( rc==SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ){
56725 /* If the journal has been truncated, simply stop reading and
56726 ** processing the journal. This might happen if the journal was
56727 ** not completely written and synced prior to a crash. In that
56728 ** case, the database should have never been written in the
56729 ** first place so it is OK to simply abandon the rollback. */
56730 rc = SQLITE_OK0;
56731 goto end_playback;
56732 }else{
56733 /* If we are unable to rollback, quit and return the error
56734 ** code. This will cause the pager to enter the error state
56735 ** so that no further harm will be done. Perhaps the next
56736 ** process to come along will be able to rollback the database.
56737 */
56738 goto end_playback;
56739 }
56740 }
56741 }
56742 }
56743 /*NOTREACHED*/
56744 assert( 0 )((void) (0));
56745
56746end_playback:
56747 if( rc==SQLITE_OK0 ){
56748 rc = sqlite3PagerSetPagesize(pPager, &savedPageSize, -1);
56749 }
56750 /* Following a rollback, the database file should be back in its original
56751 ** state prior to the start of the transaction, so invoke the
56752 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
56753 ** assertion that the transaction counter was modified.
56754 */
56755#ifdef SQLITE_DEBUG
56756 sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED0xca093fa0,0);
56757#endif
56758
56759 /* If this playback is happening automatically as a result of an IO or
56760 ** malloc error that occurred after the change-counter was updated but
56761 ** before the transaction was committed, then the change-counter
56762 ** modification may just have been reverted. If this happens in exclusive
56763 ** mode, then subsequent transactions performed by the connection will not
56764 ** update the change-counter at all. This may lead to cache inconsistency
56765 ** problems for other processes at some point in the future. So, just
56766 ** in case this has happened, clear the changeCountDone flag now.
56767 */
56768 pPager->changeCountDone = pPager->tempFile;
56769
56770 if( rc==SQLITE_OK0 ){
56771 /* Leave 4 bytes of space before the super-journal filename in memory.
56772 ** This is because it may end up being passed to sqlite3OsOpen(), in
56773 ** which case it requires 4 0x00 bytes in memory immediately before
56774 ** the filename. */
56775 zSuper = &pPager->pTmpSpace[4];
56776 rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
56777 testcase( rc!=SQLITE_OK );
56778 }
56779 if( rc==SQLITE_OK0
56780 && (pPager->eState>=PAGER_WRITER_DBMOD4 || pPager->eState==PAGER_OPEN0)
56781 ){
56782 rc = sqlite3PagerSync(pPager, 0);
56783 }
56784 if( rc==SQLITE_OK0 ){
56785 rc = pager_end_transaction(pPager, zSuper[0]!='\0', 0);
56786 testcase( rc!=SQLITE_OK );
56787 }
56788 if( rc==SQLITE_OK0 && zSuper[0] && res ){
56789 /* If there was a super-journal and this routine will return success,
56790 ** see if it is possible to delete the super-journal.
56791 */
56792 assert( zSuper==&pPager->pTmpSpace[4] )((void) (0));
56793 memset(&zSuper[-4], 0, 4);
56794 rc = pager_delsuper(pPager, zSuper);
56795 testcase( rc!=SQLITE_OK );
56796 }
56797 if( isHot && nPlayback ){
56798 sqlite3_log(SQLITE_NOTICE_RECOVER_ROLLBACK(27 | (2<<8)), "recovered %d pages from %s",
56799 nPlayback, pPager->zJournal);
56800 }
56801
56802 /* The Pager.sectorSize variable may have been updated while rolling
56803 ** back a journal created by a process with a different sector size
56804 ** value. Reset it to the correct value for this process.
56805 */
56806 setSectorSize(pPager);
56807 return rc;
56808}
56809
56810
56811/*
56812** Read the content for page pPg out of the database file (or out of
56813** the WAL if that is where the most recent copy if found) into
56814** pPg->pData. A shared lock or greater must be held on the database
56815** file before this function is called.
56816**
56817** If page 1 is read, then the value of Pager.dbFileVers[] is set to
56818** the value read from the database file.
56819**
56820** If an IO error occurs, then the IO error is returned to the caller.
56821** Otherwise, SQLITE_OK is returned.
56822*/
56823static int readDbPage(PgHdr *pPg){
56824 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
56825 int rc = SQLITE_OK0; /* Return code */
56826
56827#ifndef SQLITE_OMIT_WAL
56828 u32 iFrame = 0; /* Frame of WAL containing pgno */
56829
56830 assert( pPager->eState>=PAGER_READER && !MEMDB )((void) (0));
56831 assert( isOpen(pPager->fd) )((void) (0));
56832
56833 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
56834 rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
56835 if( rc ) return rc;
56836 }
56837 if( iFrame ){
56838 rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData);
56839 }else
56840#endif
56841 {
56842 i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize;
56843 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
56844 if( rc==SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ){
56845 rc = SQLITE_OK0;
56846 }
56847 }
56848
56849 if( pPg->pgno==1 ){
56850 if( rc ){
56851 /* If the read is unsuccessful, set the dbFileVers[] to something
56852 ** that will never be a valid file version. dbFileVers[] is a copy
56853 ** of bytes 24..39 of the database. Bytes 28..31 should always be
56854 ** zero or the size of the database in page. Bytes 32..35 and 35..39
56855 ** should be page numbers which are never 0xffffffff. So filling
56856 ** pPager->dbFileVers[] with all 0xff bytes should suffice.
56857 **
56858 ** For an encrypted database, the situation is more complex: bytes
56859 ** 24..39 of the database are white noise. But the probability of
56860 ** white noise equaling 16 bytes of 0xff is vanishingly small so
56861 ** we should still be ok.
56862 */
56863 memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
56864 }else{
56865 u8 *dbFileVers = &((u8*)pPg->pData)[24];
56866 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
56867 }
56868 }
56869 PAGER_INCR(sqlite3_pager_readdb_count);
56870 PAGER_INCR(pPager->nRead);
56871 IOTRACE(("PGIN %p %d\n", pPager, pPg->pgno));
56872 PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
56873 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
56874
56875 return rc;
56876}
56877
56878/*
56879** Update the value of the change-counter at offsets 24 and 92 in
56880** the header and the sqlite version number at offset 96.
56881**
56882** This is an unconditional update. See also the pager_incr_changecounter()
56883** routine which only updates the change-counter if the update is actually
56884** needed, as determined by the pPager->changeCountDone state variable.
56885*/
56886static void pager_write_changecounter(PgHdr *pPg){
56887 u32 change_counter;
56888 if( NEVER(pPg==0)(pPg==0) ) return;
56889
56890 /* Increment the value just read and write it back to byte 24. */
56891 change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
56892 put32bits(((char*)pPg->pData)+24, change_counter)sqlite3Put4byte((u8*)((char*)pPg->pData)+24,change_counter
)
;
56893
56894 /* Also store the SQLite version number in bytes 96..99 and in
56895 ** bytes 92..95 store the change counter for which the version number
56896 ** is valid. */
56897 put32bits(((char*)pPg->pData)+92, change_counter)sqlite3Put4byte((u8*)((char*)pPg->pData)+92,change_counter
)
;
56898 put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER)sqlite3Put4byte((u8*)((char*)pPg->pData)+96,3039002);
56899}
56900
56901#ifndef SQLITE_OMIT_WAL
56902/*
56903** This function is invoked once for each page that has already been
56904** written into the log file when a WAL transaction is rolled back.
56905** Parameter iPg is the page number of said page. The pCtx argument
56906** is actually a pointer to the Pager structure.
56907**
56908** If page iPg is present in the cache, and has no outstanding references,
56909** it is discarded. Otherwise, if there are one or more outstanding
56910** references, the page content is reloaded from the database. If the
56911** attempt to reload content from the database is required and fails,
56912** return an SQLite error code. Otherwise, SQLITE_OK.
56913*/
56914static int pagerUndoCallback(void *pCtx, Pgno iPg){
56915 int rc = SQLITE_OK0;
56916 Pager *pPager = (Pager *)pCtx;
56917 PgHdr *pPg;
56918
56919 assert( pagerUseWal(pPager) )((void) (0));
56920 pPg = sqlite3PagerLookup(pPager, iPg);
56921 if( pPg ){
56922 if( sqlite3PcachePageRefcount(pPg)==1 ){
56923 sqlite3PcacheDrop(pPg);
56924 }else{
56925 rc = readDbPage(pPg);
56926 if( rc==SQLITE_OK0 ){
56927 pPager->xReiniter(pPg);
56928 }
56929 sqlite3PagerUnrefNotNull(pPg);
56930 }
56931 }
56932
56933 /* Normally, if a transaction is rolled back, any backup processes are
56934 ** updated as data is copied out of the rollback journal and into the
56935 ** database. This is not generally possible with a WAL database, as
56936 ** rollback involves simply truncating the log file. Therefore, if one
56937 ** or more frames have already been written to the log (and therefore
56938 ** also copied into the backup databases) as part of this transaction,
56939 ** the backups must be restarted.
56940 */
56941 sqlite3BackupRestart(pPager->pBackup);
56942
56943 return rc;
56944}
56945
56946/*
56947** This function is called to rollback a transaction on a WAL database.
56948*/
56949static int pagerRollbackWal(Pager *pPager){
56950 int rc; /* Return Code */
56951 PgHdr *pList; /* List of dirty pages to revert */
56952
56953 /* For all pages in the cache that are currently dirty or have already
56954 ** been written (but not committed) to the log file, do one of the
56955 ** following:
56956 **
56957 ** + Discard the cached page (if refcount==0), or
56958 ** + Reload page content from the database (if refcount>0).
56959 */
56960 pPager->dbSize = pPager->dbOrigSize;
56961 rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
56962 pList = sqlite3PcacheDirtyList(pPager->pPCache);
56963 while( pList && rc==SQLITE_OK0 ){
56964 PgHdr *pNext = pList->pDirty;
56965 rc = pagerUndoCallback((void *)pPager, pList->pgno);
56966 pList = pNext;
56967 }
56968
56969 return rc;
56970}
56971
56972/*
56973** This function is a wrapper around sqlite3WalFrames(). As well as logging
56974** the contents of the list of pages headed by pList (connected by pDirty),
56975** this function notifies any active backup processes that the pages have
56976** changed.
56977**
56978** The list of pages passed into this routine is always sorted by page number.
56979** Hence, if page 1 appears anywhere on the list, it will be the first page.
56980*/
56981static int pagerWalFrames(
56982 Pager *pPager, /* Pager object */
56983 PgHdr *pList, /* List of frames to log */
56984 Pgno nTruncate, /* Database size after this commit */
56985 int isCommit /* True if this is a commit */
56986){
56987 int rc; /* Return code */
56988 int nList; /* Number of pages in pList */
56989 PgHdr *p; /* For looping over pages */
56990
56991 assert( pPager->pWal )((void) (0));
56992 assert( pList )((void) (0));
56993#ifdef SQLITE_DEBUG
56994 /* Verify that the page list is in accending order */
56995 for(p=pList; p && p->pDirty; p=p->pDirty){
56996 assert( p->pgno < p->pDirty->pgno )((void) (0));
56997 }
56998#endif
56999
57000 assert( pList->pDirty==0 || isCommit )((void) (0));
57001 if( isCommit ){
57002 /* If a WAL transaction is being committed, there is no point in writing
57003 ** any pages with page numbers greater than nTruncate into the WAL file.
57004 ** They will never be read by any client. So remove them from the pDirty
57005 ** list here. */
57006 PgHdr **ppNext = &pList;
57007 nList = 0;
57008 for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
57009 if( p->pgno<=nTruncate ){
57010 ppNext = &p->pDirty;
57011 nList++;
57012 }
57013 }
57014 assert( pList )((void) (0));
57015 }else{
57016 nList = 1;
57017 }
57018 pPager->aStat[PAGER_STAT_WRITE2] += nList;
57019
57020 if( pList->pgno==1 ) pager_write_changecounter(pList);
57021 rc = sqlite3WalFrames(pPager->pWal,
57022 pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
57023 );
57024 if( rc==SQLITE_OK0 && pPager->pBackup ){
57025 for(p=pList; p; p=p->pDirty){
57026 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
57027 }
57028 }
57029
57030#ifdef SQLITE_CHECK_PAGES
57031 pList = sqlite3PcacheDirtyList(pPager->pPCache);
57032 for(p=pList; p; p=p->pDirty){
57033 pager_set_pagehash(p);
57034 }
57035#endif
57036
57037 return rc;
57038}
57039
57040/*
57041** Begin a read transaction on the WAL.
57042**
57043** This routine used to be called "pagerOpenSnapshot()" because it essentially
57044** makes a snapshot of the database at the current point in time and preserves
57045** that snapshot for use by the reader in spite of concurrently changes by
57046** other writers or checkpointers.
57047*/
57048static int pagerBeginReadTransaction(Pager *pPager){
57049 int rc; /* Return code */
57050 int changed = 0; /* True if cache must be reset */
57051
57052 assert( pagerUseWal(pPager) )((void) (0));
57053 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER )((void) (0));
57054
57055 /* sqlite3WalEndReadTransaction() was not called for the previous
57056 ** transaction in locking_mode=EXCLUSIVE. So call it now. If we
57057 ** are in locking_mode=NORMAL and EndRead() was previously called,
57058 ** the duplicate call is harmless.
57059 */
57060 sqlite3WalEndReadTransaction(pPager->pWal);
57061
57062 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
57063 if( rc!=SQLITE_OK0 || changed ){
57064 pager_reset(pPager);
57065 if( USEFETCH(pPager)((pPager)->bUseFetch) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
57066 }
57067
57068 return rc;
57069}
57070#endif
57071
57072/*
57073** This function is called as part of the transition from PAGER_OPEN
57074** to PAGER_READER state to determine the size of the database file
57075** in pages (assuming the page size currently stored in Pager.pageSize).
57076**
57077** If no error occurs, SQLITE_OK is returned and the size of the database
57078** in pages is stored in *pnPage. Otherwise, an error code (perhaps
57079** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified.
57080*/
57081static int pagerPagecount(Pager *pPager, Pgno *pnPage){
57082 Pgno nPage; /* Value to return via *pnPage */
57083
57084 /* Query the WAL sub-system for the database size. The WalDbsize()
57085 ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or
57086 ** if the database size is not available. The database size is not
57087 ** available from the WAL sub-system if the log file is empty or
57088 ** contains no valid committed transactions.
57089 */
57090 assert( pPager->eState==PAGER_OPEN )((void) (0));
57091 assert( pPager->eLock>=SHARED_LOCK )((void) (0));
57092 assert( isOpen(pPager->fd) )((void) (0));
57093 assert( pPager->tempFile==0 )((void) (0));
57094 nPage = sqlite3WalDbsize(pPager->pWal);
57095
57096 /* If the number of pages in the database is not available from the
57097 ** WAL sub-system, determine the page count based on the size of
57098 ** the database file. If the size of the database file is not an
57099 ** integer multiple of the page-size, round up the result.
57100 */
57101 if( nPage==0 && ALWAYS(isOpen(pPager->fd))(((pPager->fd)->pMethods!=0)) ){
57102 i64 n = 0; /* Size of db file in bytes */
57103 int rc = sqlite3OsFileSize(pPager->fd, &n);
57104 if( rc!=SQLITE_OK0 ){
57105 return rc;
57106 }
57107 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
57108 }
57109
57110 /* If the current number of pages in the file is greater than the
57111 ** configured maximum pager number, increase the allowed limit so
57112 ** that the file can be read.
57113 */
57114 if( nPage>pPager->mxPgno ){
57115 pPager->mxPgno = (Pgno)nPage;
57116 }
57117
57118 *pnPage = nPage;
57119 return SQLITE_OK0;
57120}
57121
57122#ifndef SQLITE_OMIT_WAL
57123/*
57124** Check if the *-wal file that corresponds to the database opened by pPager
57125** exists if the database is not empy, or verify that the *-wal file does
57126** not exist (by deleting it) if the database file is empty.
57127**
57128** If the database is not empty and the *-wal file exists, open the pager
57129** in WAL mode. If the database is empty or if no *-wal file exists and
57130** if no error occurs, make sure Pager.journalMode is not set to
57131** PAGER_JOURNALMODE_WAL.
57132**
57133** Return SQLITE_OK or an error code.
57134**
57135** The caller must hold a SHARED lock on the database file to call this
57136** function. Because an EXCLUSIVE lock on the db file is required to delete
57137** a WAL on a none-empty database, this ensures there is no race condition
57138** between the xAccess() below and an xDelete() being executed by some
57139** other connection.
57140*/
57141static int pagerOpenWalIfPresent(Pager *pPager){
57142 int rc = SQLITE_OK0;
57143 assert( pPager->eState==PAGER_OPEN )((void) (0));
57144 assert( pPager->eLock>=SHARED_LOCK )((void) (0));
57145
57146 if( !pPager->tempFile ){
57147 int isWal; /* True if WAL file exists */
57148 rc = sqlite3OsAccess(
57149 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS0, &isWal
57150 );
57151 if( rc==SQLITE_OK0 ){
57152 if( isWal ){
57153 Pgno nPage; /* Size of the database file */
57154
57155 rc = pagerPagecount(pPager, &nPage);
57156 if( rc ) return rc;
57157 if( nPage==0 ){
57158 rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
57159 }else{
57160 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
57161 rc = sqlite3PagerOpenWal(pPager, 0);
57162 }
57163 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL5 ){
57164 pPager->journalMode = PAGER_JOURNALMODE_DELETE0;
57165 }
57166 }
57167 }
57168 return rc;
57169}
57170#endif
57171
57172/*
57173** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
57174** the entire super-journal file. The case pSavepoint==NULL occurs when
57175** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction
57176** savepoint.
57177**
57178** When pSavepoint is not NULL (meaning a non-transaction savepoint is
57179** being rolled back), then the rollback consists of up to three stages,
57180** performed in the order specified:
57181**
57182** * Pages are played back from the main journal starting at byte
57183** offset PagerSavepoint.iOffset and continuing to
57184** PagerSavepoint.iHdrOffset, or to the end of the main journal
57185** file if PagerSavepoint.iHdrOffset is zero.
57186**
57187** * If PagerSavepoint.iHdrOffset is not zero, then pages are played
57188** back starting from the journal header immediately following
57189** PagerSavepoint.iHdrOffset to the end of the main journal file.
57190**
57191** * Pages are then played back from the sub-journal file, starting
57192** with the PagerSavepoint.iSubRec and continuing to the end of
57193** the journal file.
57194**
57195** Throughout the rollback process, each time a page is rolled back, the
57196** corresponding bit is set in a bitvec structure (variable pDone in the
57197** implementation below). This is used to ensure that a page is only
57198** rolled back the first time it is encountered in either journal.
57199**
57200** If pSavepoint is NULL, then pages are only played back from the main
57201** journal file. There is no need for a bitvec in this case.
57202**
57203** In either case, before playback commences the Pager.dbSize variable
57204** is reset to the value that it held at the start of the savepoint
57205** (or transaction). No page with a page-number greater than this value
57206** is played back. If one is encountered it is simply skipped.
57207*/
57208static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
57209 i64 szJ; /* Effective size of the main journal */
57210 i64 iHdrOff; /* End of first segment of main-journal records */
57211 int rc = SQLITE_OK0; /* Return code */
57212 Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */
57213
57214 assert( pPager->eState!=PAGER_ERROR )((void) (0));
57215 assert( pPager->eState>=PAGER_WRITER_LOCKED )((void) (0));
57216
57217 /* Allocate a bitvec to use to store the set of pages rolled back */
57218 if( pSavepoint ){
57219 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
57220 if( !pDone ){
57221 return SQLITE_NOMEM_BKPT7;
57222 }
57223 }
57224
57225 /* Set the database size back to the value it was before the savepoint
57226 ** being reverted was opened.
57227 */
57228 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
57229 pPager->changeCountDone = pPager->tempFile;
57230
57231 if( !pSavepoint && pagerUseWal(pPager)((pPager)->pWal!=0) ){
57232 return pagerRollbackWal(pPager);
57233 }
57234
57235 /* Use pPager->journalOff as the effective size of the main rollback
57236 ** journal. The actual file might be larger than this in
57237 ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST. But anything
57238 ** past pPager->journalOff is off-limits to us.
57239 */
57240 szJ = pPager->journalOff;
57241 assert( pagerUseWal(pPager)==0 || szJ==0 )((void) (0));
57242
57243 /* Begin by rolling back records from the main journal starting at
57244 ** PagerSavepoint.iOffset and continuing to the next journal header.
57245 ** There might be records in the main journal that have a page number
57246 ** greater than the current database size (pPager->dbSize) but those
57247 ** will be skipped automatically. Pages are added to pDone as they
57248 ** are played back.
57249 */
57250 if( pSavepoint && !pagerUseWal(pPager)((pPager)->pWal!=0) ){
57251 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
57252 pPager->journalOff = pSavepoint->iOffset;
57253 while( rc==SQLITE_OK0 && pPager->journalOff<iHdrOff ){
57254 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
57255 }
57256 assert( rc!=SQLITE_DONE )((void) (0));
57257 }else{
57258 pPager->journalOff = 0;
57259 }
57260
57261 /* Continue rolling back records out of the main journal starting at
57262 ** the first journal header seen and continuing until the effective end
57263 ** of the main journal file. Continue to skip out-of-range pages and
57264 ** continue adding pages rolled back to pDone.
57265 */
57266 while( rc==SQLITE_OK0 && pPager->journalOff<szJ ){
57267 u32 ii; /* Loop counter */
57268 u32 nJRec = 0; /* Number of Journal Records */
57269 u32 dummy;
57270 rc = readJournalHdr(pPager, 0, szJ, &nJRec, &dummy);
57271 assert( rc!=SQLITE_DONE )((void) (0));
57272
57273 /*
57274 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
57275 ** test is related to ticket #2565. See the discussion in the
57276 ** pager_playback() function for additional information.
57277 */
57278 if( nJRec==0
57279 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)(pPager->sectorSize)==pPager->journalOff
57280 ){
57281 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager)((pPager->pageSize) + 8));
57282 }
57283 for(ii=0; rc==SQLITE_OK0 && ii<nJRec && pPager->journalOff<szJ; ii++){
57284 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
57285 }
57286 assert( rc!=SQLITE_DONE )((void) (0));
57287 }
57288 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ )((void) (0));
57289
57290 /* Finally, rollback pages from the sub-journal. Page that were
57291 ** previously rolled back out of the main journal (and are hence in pDone)
57292 ** will be skipped. Out-of-range pages are also skipped.
57293 */
57294 if( pSavepoint ){
57295 u32 ii; /* Loop counter */
57296 i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
57297
57298 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
57299 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
57300 }
57301 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK0 && ii<pPager->nSubRec; ii++){
57302 assert( offset==(i64)ii*(4+pPager->pageSize) )((void) (0));
57303 rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
57304 }
57305 assert( rc!=SQLITE_DONE )((void) (0));
57306 }
57307
57308 sqlite3BitvecDestroy(pDone);
57309 if( rc==SQLITE_OK0 ){
57310 pPager->journalOff = szJ;
57311 }
57312
57313 return rc;
57314}
57315
57316/*
57317** Change the maximum number of in-memory pages that are allowed
57318** before attempting to recycle clean and unused pages.
57319*/
57320SQLITE_PRIVATEstatic void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
57321 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
57322}
57323
57324/*
57325** Change the maximum number of in-memory pages that are allowed
57326** before attempting to spill pages to journal.
57327*/
57328SQLITE_PRIVATEstatic int sqlite3PagerSetSpillsize(Pager *pPager, int mxPage){
57329 return sqlite3PcacheSetSpillsize(pPager->pPCache, mxPage);
57330}
57331
57332/*
57333** Invoke SQLITE_FCNTL_MMAP_SIZE based on the current value of szMmap.
57334*/
57335static void pagerFixMaplimit(Pager *pPager){
57336#if SQLITE_MAX_MMAP_SIZE20971520>0
57337 sqlite3_file *fd = pPager->fd;
57338 if( isOpen(fd)((fd)->pMethods!=0) && fd->pMethods->iVersion>=3 ){
57339 sqlite3_int64 sz;
57340 sz = pPager->szMmap;
57341 pPager->bUseFetch = (sz>0);
57342 setGetterMethod(pPager);
57343 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE18, &sz);
57344 }
57345#endif
57346}
57347
57348/*
57349** Change the maximum size of any memory mapping made of the database file.
57350*/
57351SQLITE_PRIVATEstatic void sqlite3PagerSetMmapLimit(Pager *pPager, sqlite3_int64 szMmap){
57352 pPager->szMmap = szMmap;
57353 pagerFixMaplimit(pPager);
57354}
57355
57356/*
57357** Free as much memory as possible from the pager.
57358*/
57359SQLITE_PRIVATEstatic void sqlite3PagerShrink(Pager *pPager){
57360 sqlite3PcacheShrink(pPager->pPCache);
57361}
57362
57363/*
57364** Adjust settings of the pager to those specified in the pgFlags parameter.
57365**
57366** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness
57367** of the database to damage due to OS crashes or power failures by
57368** changing the number of syncs()s when writing the journals.
57369** There are four levels:
57370**
57371** OFF sqlite3OsSync() is never called. This is the default
57372** for temporary and transient files.
57373**
57374** NORMAL The journal is synced once before writes begin on the
57375** database. This is normally adequate protection, but
57376** it is theoretically possible, though very unlikely,
57377** that an inopertune power failure could leave the journal
57378** in a state which would cause damage to the database
57379** when it is rolled back.
57380**
57381** FULL The journal is synced twice before writes begin on the
57382** database (with some additional information - the nRec field
57383** of the journal header - being written in between the two
57384** syncs). If we assume that writing a
57385** single disk sector is atomic, then this mode provides
57386** assurance that the journal will not be corrupted to the
57387** point of causing damage to the database during rollback.
57388**
57389** EXTRA This is like FULL except that is also syncs the directory
57390** that contains the rollback journal after the rollback
57391** journal is unlinked.
57392**
57393** The above is for a rollback-journal mode. For WAL mode, OFF continues
57394** to mean that no syncs ever occur. NORMAL means that the WAL is synced
57395** prior to the start of checkpoint and that the database file is synced
57396** at the conclusion of the checkpoint if the entire content of the WAL
57397** was written back into the database. But no sync operations occur for
57398** an ordinary commit in NORMAL mode with WAL. FULL means that the WAL
57399** file is synced following each commit operation, in addition to the
57400** syncs associated with NORMAL. There is no difference between FULL
57401** and EXTRA for WAL mode.
57402**
57403** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL. The
57404** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
57405** using fcntl(F_FULLFSYNC). SQLITE_SYNC_NORMAL means to do an
57406** ordinary fsync() call. There is no difference between SQLITE_SYNC_FULL
57407** and SQLITE_SYNC_NORMAL on platforms other than MacOSX. But the
57408** synchronous=FULL versus synchronous=NORMAL setting determines when
57409** the xSync primitive is called and is relevant to all platforms.
57410**
57411** Numeric values associated with these states are OFF==1, NORMAL=2,
57412** and FULL=3.
57413*/
57414#ifndef SQLITE_OMIT_PAGER_PRAGMAS
57415SQLITE_PRIVATEstatic void sqlite3PagerSetFlags(
57416 Pager *pPager, /* The pager to set safety level for */
57417 unsigned pgFlags /* Various flags */
57418){
57419 unsigned level = pgFlags & PAGER_SYNCHRONOUS_MASK0x07;
57420 if( pPager->tempFile ){
57421 pPager->noSync = 1;
57422 pPager->fullSync = 0;
57423 pPager->extraSync = 0;
57424 }else{
57425 pPager->noSync = level==PAGER_SYNCHRONOUS_OFF0x01 ?1:0;
57426 pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL0x03 ?1:0;
57427 pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA0x04 ?1:0;
57428 }
57429 if( pPager->noSync ){
57430 pPager->syncFlags = 0;
57431 }else if( pgFlags & PAGER_FULLFSYNC0x08 ){
57432 pPager->syncFlags = SQLITE_SYNC_FULL0x00003;
57433 }else{
57434 pPager->syncFlags = SQLITE_SYNC_NORMAL0x00002;
57435 }
57436 pPager->walSyncFlags = (pPager->syncFlags<<2);
57437 if( pPager->fullSync ){
57438 pPager->walSyncFlags |= pPager->syncFlags;
57439 }
57440 if( (pgFlags & PAGER_CKPT_FULLFSYNC0x10) && !pPager->noSync ){
57441 pPager->walSyncFlags |= (SQLITE_SYNC_FULL0x00003<<2);
57442 }
57443 if( pgFlags & PAGER_CACHESPILL0x20 ){
57444 pPager->doNotSpill &= ~SPILLFLAG_OFF0x01;
57445 }else{
57446 pPager->doNotSpill |= SPILLFLAG_OFF0x01;
57447 }
57448}
57449#endif
57450
57451/*
57452** The following global variable is incremented whenever the library
57453** attempts to open a temporary file. This information is used for
57454** testing and analysis only.
57455*/
57456#ifdef SQLITE_TEST
57457SQLITE_API int sqlite3_opentemp_count = 0;
57458#endif
57459
57460/*
57461** Open a temporary file.
57462**
57463** Write the file descriptor into *pFile. Return SQLITE_OK on success
57464** or some other error code if we fail. The OS will automatically
57465** delete the temporary file when it is closed.
57466**
57467** The flags passed to the VFS layer xOpen() call are those specified
57468** by parameter vfsFlags ORed with the following:
57469**
57470** SQLITE_OPEN_READWRITE
57471** SQLITE_OPEN_CREATE
57472** SQLITE_OPEN_EXCLUSIVE
57473** SQLITE_OPEN_DELETEONCLOSE
57474*/
57475static int pagerOpentemp(
57476 Pager *pPager, /* The pager object */
57477 sqlite3_file *pFile, /* Write the file descriptor here */
57478 int vfsFlags /* Flags passed through to the VFS */
57479){
57480 int rc; /* Return code */
57481
57482#ifdef SQLITE_TEST
57483 sqlite3_opentemp_count++; /* Used for testing and analysis only */
57484#endif
57485
57486 vfsFlags |= SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004 |
57487 SQLITE_OPEN_EXCLUSIVE0x00000010 | SQLITE_OPEN_DELETEONCLOSE0x00000008;
57488 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
57489 assert( rc!=SQLITE_OK || isOpen(pFile) )((void) (0));
57490 return rc;
57491}
57492
57493/*
57494** Set the busy handler function.
57495**
57496** The pager invokes the busy-handler if sqlite3OsLock() returns
57497** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
57498** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE
57499** lock. It does *not* invoke the busy handler when upgrading from
57500** SHARED to RESERVED, or when upgrading from SHARED to EXCLUSIVE
57501** (which occurs during hot-journal rollback). Summary:
57502**
57503** Transition | Invokes xBusyHandler
57504** --------------------------------------------------------
57505** NO_LOCK -> SHARED_LOCK | Yes
57506** SHARED_LOCK -> RESERVED_LOCK | No
57507** SHARED_LOCK -> EXCLUSIVE_LOCK | No
57508** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes
57509**
57510** If the busy-handler callback returns non-zero, the lock is
57511** retried. If it returns zero, then the SQLITE_BUSY error is
57512** returned to the caller of the pager API function.
57513*/
57514SQLITE_PRIVATEstatic void sqlite3PagerSetBusyHandler(
57515 Pager *pPager, /* Pager object */
57516 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
57517 void *pBusyHandlerArg /* Argument to pass to xBusyHandler */
57518){
57519 void **ap;
57520 pPager->xBusyHandler = xBusyHandler;
57521 pPager->pBusyHandlerArg = pBusyHandlerArg;
57522 ap = (void **)&pPager->xBusyHandler;
57523 assert( ((int(*)(void *))(ap[0]))==xBusyHandler )((void) (0));
57524 assert( ap[1]==pBusyHandlerArg )((void) (0));
57525 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER15, (void *)ap);
57526}
57527
57528/*
57529** Change the page size used by the Pager object. The new page size
57530** is passed in *pPageSize.
57531**
57532** If the pager is in the error state when this function is called, it
57533** is a no-op. The value returned is the error state error code (i.e.
57534** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
57535**
57536** Otherwise, if all of the following are true:
57537**
57538** * the new page size (value of *pPageSize) is valid (a power
57539** of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
57540**
57541** * there are no outstanding page references, and
57542**
57543** * the database is either not an in-memory database or it is
57544** an in-memory database that currently consists of zero pages.
57545**
57546** then the pager object page size is set to *pPageSize.
57547**
57548** If the page size is changed, then this function uses sqlite3PagerMalloc()
57549** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt
57550** fails, SQLITE_NOMEM is returned and the page size remains unchanged.
57551** In all other cases, SQLITE_OK is returned.
57552**
57553** If the page size is not changed, either because one of the enumerated
57554** conditions above is not true, the pager was in error state when this
57555** function was called, or because the memory allocation attempt failed,
57556** then *pPageSize is set to the old, retained page size before returning.
57557*/
57558SQLITE_PRIVATEstatic int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
57559 int rc = SQLITE_OK0;
57560
57561 /* It is not possible to do a full assert_pager_state() here, as this
57562 ** function may be called from within PagerOpen(), before the state
57563 ** of the Pager object is internally consistent.
57564 **
57565 ** At one point this function returned an error if the pager was in
57566 ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that
57567 ** there is at least one outstanding page reference, this function
57568 ** is a no-op for that case anyhow.
57569 */
57570
57571 u32 pageSize = *pPageSize;
57572 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) )((void) (0));
57573 if( (pPager->memDb==0 || pPager->dbSize==0)
57574 && sqlite3PcacheRefCount(pPager->pPCache)==0
57575 && pageSize && pageSize!=(u32)pPager->pageSize
57576 ){
57577 char *pNew = NULL((void*)0); /* New temp space */
57578 i64 nByte = 0;
57579
57580 if( pPager->eState>PAGER_OPEN0 && isOpen(pPager->fd)((pPager->fd)->pMethods!=0) ){
57581 rc = sqlite3OsFileSize(pPager->fd, &nByte);
57582 }
57583 if( rc==SQLITE_OK0 ){
57584 /* 8 bytes of zeroed overrun space is sufficient so that the b-tree
57585 * cell header parser will never run off the end of the allocation */
57586 pNew = (char *)sqlite3PageMalloc(pageSize+8);
57587 if( !pNew ){
57588 rc = SQLITE_NOMEM_BKPT7;
57589 }else{
57590 memset(pNew+pageSize, 0, 8);
57591 }
57592 }
57593
57594 if( rc==SQLITE_OK0 ){
57595 pager_reset(pPager);
57596 rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
57597 }
57598 if( rc==SQLITE_OK0 ){
57599 sqlite3PageFree(pPager->pTmpSpace);
57600 pPager->pTmpSpace = pNew;
57601 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
57602 pPager->pageSize = pageSize;
57603 pPager->lckPgno = (Pgno)(PENDING_BYTEsqlite3PendingByte/pageSize) + 1;
57604 }else{
57605 sqlite3PageFree(pNew);
57606 }
57607 }
57608
57609 *pPageSize = pPager->pageSize;
57610 if( rc==SQLITE_OK0 ){
57611 if( nReserve<0 ) nReserve = pPager->nReserve;
57612 assert( nReserve>=0 && nReserve<1000 )((void) (0));
57613 pPager->nReserve = (i16)nReserve;
57614 pagerFixMaplimit(pPager);
57615 }
57616 return rc;
57617}
57618
57619/*
57620** Return a pointer to the "temporary page" buffer held internally
57621** by the pager. This is a buffer that is big enough to hold the
57622** entire content of a database page. This buffer is used internally
57623** during rollback and will be overwritten whenever a rollback
57624** occurs. But other modules are free to use it too, as long as
57625** no rollbacks are happening.
57626*/
57627SQLITE_PRIVATEstatic void *sqlite3PagerTempSpace(Pager *pPager){
57628 return pPager->pTmpSpace;
57629}
57630
57631/*
57632** Attempt to set the maximum database page count if mxPage is positive.
57633** Make no changes if mxPage is zero or negative. And never reduce the
57634** maximum page count below the current size of the database.
57635**
57636** Regardless of mxPage, return the current maximum page count.
57637*/
57638SQLITE_PRIVATEstatic Pgno sqlite3PagerMaxPageCount(Pager *pPager, Pgno mxPage){
57639 if( mxPage>0 ){
57640 pPager->mxPgno = mxPage;
57641 }
57642 assert( pPager->eState!=PAGER_OPEN )((void) (0)); /* Called only by OP_MaxPgcnt */
57643 /* assert( pPager->mxPgno>=pPager->dbSize ); */
57644 /* OP_MaxPgcnt ensures that the parameter passed to this function is not
57645 ** less than the total number of valid pages in the database. But this
57646 ** may be less than Pager.dbSize, and so the assert() above is not valid */
57647 return pPager->mxPgno;
57648}
57649
57650/*
57651** The following set of routines are used to disable the simulated
57652** I/O error mechanism. These routines are used to avoid simulated
57653** errors in places where we do not care about errors.
57654**
57655** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
57656** and generate no code.
57657*/
57658#ifdef SQLITE_TEST
57659SQLITE_API extern int sqlite3_io_error_pending;
57660SQLITE_API extern int sqlite3_io_error_hit;
57661static int saved_cnt;
57662void disable_simulated_io_errors(void){
57663 saved_cnt = sqlite3_io_error_pending;
57664 sqlite3_io_error_pending = -1;
57665}
57666void enable_simulated_io_errors(void){
57667 sqlite3_io_error_pending = saved_cnt;
57668}
57669#else
57670# define disable_simulated_io_errors()
57671# define enable_simulated_io_errors()
57672#endif
57673
57674/*
57675** Read the first N bytes from the beginning of the file into memory
57676** that pDest points to.
57677**
57678** If the pager was opened on a transient file (zFilename==""), or
57679** opened on a file less than N bytes in size, the output buffer is
57680** zeroed and SQLITE_OK returned. The rationale for this is that this
57681** function is used to read database headers, and a new transient or
57682** zero sized database has a header than consists entirely of zeroes.
57683**
57684** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered,
57685** the error code is returned to the caller and the contents of the
57686** output buffer undefined.
57687*/
57688SQLITE_PRIVATEstatic int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
57689 int rc = SQLITE_OK0;
57690 memset(pDest, 0, N);
57691 assert( isOpen(pPager->fd) || pPager->tempFile )((void) (0));
57692
57693 /* This routine is only called by btree immediately after creating
57694 ** the Pager object. There has not been an opportunity to transition
57695 ** to WAL mode yet.
57696 */
57697 assert( !pagerUseWal(pPager) )((void) (0));
57698
57699 if( isOpen(pPager->fd)((pPager->fd)->pMethods!=0) ){
57700 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
57701 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
57702 if( rc==SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ){
57703 rc = SQLITE_OK0;
57704 }
57705 }
57706 return rc;
57707}
57708
57709/*
57710** This function may only be called when a read-transaction is open on
57711** the pager. It returns the total number of pages in the database.
57712**
57713** However, if the file is between 1 and <page-size> bytes in size, then
57714** this is considered a 1 page file.
57715*/
57716SQLITE_PRIVATEstatic void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
57717 assert( pPager->eState>=PAGER_READER )((void) (0));
57718 assert( pPager->eState!=PAGER_WRITER_FINISHED )((void) (0));
57719 *pnPage = (int)pPager->dbSize;
57720}
57721
57722
57723/*
57724** Try to obtain a lock of type locktype on the database file. If
57725** a similar or greater lock is already held, this function is a no-op
57726** (returning SQLITE_OK immediately).
57727**
57728** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke
57729** the busy callback if the lock is currently not available. Repeat
57730** until the busy callback returns false or until the attempt to
57731** obtain the lock succeeds.
57732**
57733** Return SQLITE_OK on success and an error code if we cannot obtain
57734** the lock. If the lock is obtained successfully, set the Pager.state
57735** variable to locktype before returning.
57736*/
57737static int pager_wait_on_lock(Pager *pPager, int locktype){
57738 int rc; /* Return code */
57739
57740 /* Check that this is either a no-op (because the requested lock is
57741 ** already held), or one of the transitions that the busy-handler
57742 ** may be invoked during, according to the comment above
57743 ** sqlite3PagerSetBusyhandler().
57744 */
57745 assert( (pPager->eLock>=locktype)((void) (0))
57746 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)((void) (0))
57747 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)((void) (0))
57748 )((void) (0));
57749
57750 do {
57751 rc = pagerLockDb(pPager, locktype);
57752 }while( rc==SQLITE_BUSY5 && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
57753 return rc;
57754}
57755
57756/*
57757** Function assertTruncateConstraint(pPager) checks that one of the
57758** following is true for all dirty pages currently in the page-cache:
57759**
57760** a) The page number is less than or equal to the size of the
57761** current database image, in pages, OR
57762**
57763** b) if the page content were written at this time, it would not
57764** be necessary to write the current content out to the sub-journal.
57765**
57766** If the condition asserted by this function were not true, and the
57767** dirty page were to be discarded from the cache via the pagerStress()
57768** routine, pagerStress() would not write the current page content to
57769** the database file. If a savepoint transaction were rolled back after
57770** this happened, the correct behavior would be to restore the current
57771** content of the page. However, since this content is not present in either
57772** the database file or the portion of the rollback journal and
57773** sub-journal rolled back the content could not be restored and the
57774** database image would become corrupt. It is therefore fortunate that
57775** this circumstance cannot arise.
57776*/
57777#if defined(SQLITE_DEBUG)
57778static void assertTruncateConstraintCb(PgHdr *pPg){
57779 Pager *pPager = pPg->pPager;
57780 assert( pPg->flags&PGHDR_DIRTY )((void) (0));
57781 if( pPg->pgno>pPager->dbSize ){ /* if (a) is false */
57782 Pgno pgno = pPg->pgno;
57783 int i;
57784 for(i=0; i<pPg->pPager->nSavepoint; i++){
57785 PagerSavepoint *p = &pPager->aSavepoint[i];
57786 assert( p->nOrig<pgno || sqlite3BitvecTestNotNull(p->pInSavepoint,pgno) )((void) (0));
57787 }
57788 }
57789}
57790static void assertTruncateConstraint(Pager *pPager){
57791 sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
57792}
57793#else
57794# define assertTruncateConstraint(pPager)
57795#endif
57796
57797/*
57798** Truncate the in-memory database file image to nPage pages. This
57799** function does not actually modify the database file on disk. It
57800** just sets the internal state of the pager object so that the
57801** truncation will be done when the current transaction is committed.
57802**
57803** This function is only called right before committing a transaction.
57804** Once this function has been called, the transaction must either be
57805** rolled back or committed. It is not safe to call this function and
57806** then continue writing to the database.
57807*/
57808SQLITE_PRIVATEstatic void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
57809 assert( pPager->dbSize>=nPage || CORRUPT_DB )((void) (0));
57810 assert( pPager->eState>=PAGER_WRITER_CACHEMOD )((void) (0));
57811 pPager->dbSize = nPage;
57812
57813 /* At one point the code here called assertTruncateConstraint() to
57814 ** ensure that all pages being truncated away by this operation are,
57815 ** if one or more savepoints are open, present in the savepoint
57816 ** journal so that they can be restored if the savepoint is rolled
57817 ** back. This is no longer necessary as this function is now only
57818 ** called right before committing a transaction. So although the
57819 ** Pager object may still have open savepoints (Pager.nSavepoint!=0),
57820 ** they cannot be rolled back. So the assertTruncateConstraint() call
57821 ** is no longer correct. */
57822}
57823
57824
57825/*
57826** This function is called before attempting a hot-journal rollback. It
57827** syncs the journal file to disk, then sets pPager->journalHdr to the
57828** size of the journal file so that the pager_playback() routine knows
57829** that the entire journal file has been synced.
57830**
57831** Syncing a hot-journal to disk before attempting to roll it back ensures
57832** that if a power-failure occurs during the rollback, the process that
57833** attempts rollback following system recovery sees the same journal
57834** content as this process.
57835**
57836** If everything goes as planned, SQLITE_OK is returned. Otherwise,
57837** an SQLite error code.
57838*/
57839static int pagerSyncHotJournal(Pager *pPager){
57840 int rc = SQLITE_OK0;
57841 if( !pPager->noSync ){
57842 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL0x00002);
57843 }
57844 if( rc==SQLITE_OK0 ){
57845 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
57846 }
57847 return rc;
57848}
57849
57850#if SQLITE_MAX_MMAP_SIZE20971520>0
57851/*
57852** Obtain a reference to a memory mapped page object for page number pgno.
57853** The new object will use the pointer pData, obtained from xFetch().
57854** If successful, set *ppPage to point to the new page reference
57855** and return SQLITE_OK. Otherwise, return an SQLite error code and set
57856** *ppPage to zero.
57857**
57858** Page references obtained by calling this function should be released
57859** by calling pagerReleaseMapPage().
57860*/
57861static int pagerAcquireMapPage(
57862 Pager *pPager, /* Pager object */
57863 Pgno pgno, /* Page number */
57864 void *pData, /* xFetch()'d data for this page */
57865 PgHdr **ppPage /* OUT: Acquired page object */
57866){
57867 PgHdr *p; /* Memory mapped page to return */
57868
57869 if( pPager->pMmapFreelist ){
57870 *ppPage = p = pPager->pMmapFreelist;
57871 pPager->pMmapFreelist = p->pDirty;
57872 p->pDirty = 0;
57873 assert( pPager->nExtra>=8 )((void) (0));
57874 memset(p->pExtra, 0, 8);
57875 }else{
57876 *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
57877 if( p==0 ){
57878 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
57879 return SQLITE_NOMEM_BKPT7;
57880 }
57881 p->pExtra = (void *)&p[1];
57882 p->flags = PGHDR_MMAP0x020;
57883 p->nRef = 1;
57884 p->pPager = pPager;
57885 }
57886
57887 assert( p->pExtra==(void *)&p[1] )((void) (0));
57888 assert( p->pPage==0 )((void) (0));
57889 assert( p->flags==PGHDR_MMAP )((void) (0));
57890 assert( p->pPager==pPager )((void) (0));
57891 assert( p->nRef==1 )((void) (0));
57892
57893 p->pgno = pgno;
57894 p->pData = pData;
57895 pPager->nMmapOut++;
57896
57897 return SQLITE_OK0;
57898}
57899#endif
57900
57901/*
57902** Release a reference to page pPg. pPg must have been returned by an
57903** earlier call to pagerAcquireMapPage().
57904*/
57905static void pagerReleaseMapPage(PgHdr *pPg){
57906 Pager *pPager = pPg->pPager;
57907 pPager->nMmapOut--;
57908 pPg->pDirty = pPager->pMmapFreelist;
57909 pPager->pMmapFreelist = pPg;
57910
57911 assert( pPager->fd->pMethods->iVersion>=3 )((void) (0));
57912 sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
57913}
57914
57915/*
57916** Free all PgHdr objects stored in the Pager.pMmapFreelist list.
57917*/
57918static void pagerFreeMapHdrs(Pager *pPager){
57919 PgHdr *p;
57920 PgHdr *pNext;
57921 for(p=pPager->pMmapFreelist; p; p=pNext){
57922 pNext = p->pDirty;
57923 sqlite3_free(p);
57924 }
57925}
57926
57927/* Verify that the database file has not be deleted or renamed out from
57928** under the pager. Return SQLITE_OK if the database is still where it ought
57929** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error
57930** code from sqlite3OsAccess()) if the database has gone missing.
57931*/
57932static int databaseIsUnmoved(Pager *pPager){
57933 int bHasMoved = 0;
57934 int rc;
57935
57936 if( pPager->tempFile ) return SQLITE_OK0;
57937 if( pPager->dbSize==0 ) return SQLITE_OK0;
57938 assert( pPager->zFilename && pPager->zFilename[0] )((void) (0));
57939 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED20, &bHasMoved);
57940 if( rc==SQLITE_NOTFOUND12 ){
57941 /* If the HAS_MOVED file-control is unimplemented, assume that the file
57942 ** has not been moved. That is the historical behavior of SQLite: prior to
57943 ** version 3.8.3, it never checked */
57944 rc = SQLITE_OK0;
57945 }else if( rc==SQLITE_OK0 && bHasMoved ){
57946 rc = SQLITE_READONLY_DBMOVED(8 | (4<<8));
57947 }
57948 return rc;
57949}
57950
57951
57952/*
57953** Shutdown the page cache. Free all memory and close all files.
57954**
57955** If a transaction was in progress when this routine is called, that
57956** transaction is rolled back. All outstanding pages are invalidated
57957** and their memory is freed. Any attempt to use a page associated
57958** with this page cache after this function returns will likely
57959** result in a coredump.
57960**
57961** This function always succeeds. If a transaction is active an attempt
57962** is made to roll it back. If an error occurs during the rollback
57963** a hot journal may be left in the filesystem but no error is returned
57964** to the caller.
57965*/
57966SQLITE_PRIVATEstatic int sqlite3PagerClose(Pager *pPager, sqlite3 *db){
57967 u8 *pTmp = (u8*)pPager->pTmpSpace;
57968 assert( db || pagerUseWal(pPager)==0 )((void) (0));
57969 assert( assert_pager_state(pPager) )((void) (0));
57970 disable_simulated_io_errors();
57971 sqlite3BeginBenignMalloc();
57972 pagerFreeMapHdrs(pPager);
57973 /* pPager->errCode = 0; */
57974 pPager->exclusiveMode = 0;
57975#ifndef SQLITE_OMIT_WAL
57976 {
57977 u8 *a = 0;
57978 assert( db || pPager->pWal==0 )((void) (0));
57979 if( db && 0==(db->flags & SQLITE_NoCkptOnClose0x00000800)
57980 && SQLITE_OK0==databaseIsUnmoved(pPager)
57981 ){
57982 a = pTmp;
57983 }
57984 sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a);
57985 pPager->pWal = 0;
57986 }
57987#endif
57988 pager_reset(pPager);
57989 if( MEMDBpPager->memDb ){
57990 pager_unlock(pPager);
57991 }else{
57992 /* If it is open, sync the journal file before calling UnlockAndRollback.
57993 ** If this is not done, then an unsynced portion of the open journal
57994 ** file may be played back into the database. If a power failure occurs
57995 ** while this is happening, the database could become corrupt.
57996 **
57997 ** If an error occurs while trying to sync the journal, shift the pager
57998 ** into the ERROR state. This causes UnlockAndRollback to unlock the
57999 ** database and close the journal file without attempting to roll it
58000 ** back or finalize it. The next database user will have to do hot-journal
58001 ** rollback before accessing the database file.
58002 */
58003 if( isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) ){
58004 pager_error(pPager, pagerSyncHotJournal(pPager));
58005 }
58006 pagerUnlockAndRollback(pPager);
58007 }
58008 sqlite3EndBenignMalloc();
58009 enable_simulated_io_errors();
58010 PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
58011 IOTRACE(("CLOSE %p\n", pPager))
58012 sqlite3OsClose(pPager->jfd);
58013 sqlite3OsClose(pPager->fd);
58014 sqlite3PageFree(pTmp);
58015 sqlite3PcacheClose(pPager->pPCache);
58016 assert( !pPager->aSavepoint && !pPager->pInJournal )((void) (0));
58017 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) )((void) (0));
58018
58019 sqlite3_free(pPager);
58020 return SQLITE_OK0;
58021}
58022
58023#if !defined(NDEBUG1) || defined(SQLITE_TEST)
58024/*
58025** Return the page number for page pPg.
58026*/
58027SQLITE_PRIVATEstatic Pgno sqlite3PagerPagenumber(DbPage *pPg){
58028 return pPg->pgno;
58029}
58030#endif
58031
58032/*
58033** Increment the reference count for page pPg.
58034*/
58035SQLITE_PRIVATEstatic void sqlite3PagerRef(DbPage *pPg){
58036 sqlite3PcacheRef(pPg);
58037}
58038
58039/*
58040** Sync the journal. In other words, make sure all the pages that have
58041** been written to the journal have actually reached the surface of the
58042** disk and can be restored in the event of a hot-journal rollback.
58043**
58044** If the Pager.noSync flag is set, then this function is a no-op.
58045** Otherwise, the actions required depend on the journal-mode and the
58046** device characteristics of the file-system, as follows:
58047**
58048** * If the journal file is an in-memory journal file, no action need
58049** be taken.
58050**
58051** * Otherwise, if the device does not support the SAFE_APPEND property,
58052** then the nRec field of the most recently written journal header
58053** is updated to contain the number of journal records that have
58054** been written following it. If the pager is operating in full-sync
58055** mode, then the journal file is synced before this field is updated.
58056**
58057** * If the device does not support the SEQUENTIAL property, then
58058** journal file is synced.
58059**
58060** Or, in pseudo-code:
58061**
58062** if( NOT <in-memory journal> ){
58063** if( NOT SAFE_APPEND ){
58064** if( <full-sync mode> ) xSync(<journal file>);
58065** <update nRec field>
58066** }
58067** if( NOT SEQUENTIAL ) xSync(<journal file>);
58068** }
58069**
58070** If successful, this routine clears the PGHDR_NEED_SYNC flag of every
58071** page currently held in memory before returning SQLITE_OK. If an IO
58072** error is encountered, then the IO error code is returned to the caller.
58073*/
58074static int syncJournal(Pager *pPager, int newHdr){
58075 int rc; /* Return code */
58076
58077 assert( pPager->eState==PAGER_WRITER_CACHEMOD((void) (0))
58078 || pPager->eState==PAGER_WRITER_DBMOD((void) (0))
58079 )((void) (0));
58080 assert( assert_pager_state(pPager) )((void) (0));
58081 assert( !pagerUseWal(pPager) )((void) (0));
58082
58083 rc = sqlite3PagerExclusiveLock(pPager);
58084 if( rc!=SQLITE_OK0 ) return rc;
58085
58086 if( !pPager->noSync ){
58087 assert( !pPager->tempFile )((void) (0));
58088 if( isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY4 ){
58089 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
58090 assert( isOpen(pPager->jfd) )((void) (0));
58091
58092 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND0x00000200) ){
58093 /* This block deals with an obscure problem. If the last connection
58094 ** that wrote to this database was operating in persistent-journal
58095 ** mode, then the journal file may at this point actually be larger
58096 ** than Pager.journalOff bytes. If the next thing in the journal
58097 ** file happens to be a journal-header (written as part of the
58098 ** previous connection's transaction), and a crash or power-failure
58099 ** occurs after nRec is updated but before this connection writes
58100 ** anything else to the journal file (or commits/rolls back its
58101 ** transaction), then SQLite may become confused when doing the
58102 ** hot-journal rollback following recovery. It may roll back all
58103 ** of this connections data, then proceed to rolling back the old,
58104 ** out-of-date data that follows it. Database corruption.
58105 **
58106 ** To work around this, if the journal file does appear to contain
58107 ** a valid header following Pager.journalOff, then write a 0x00
58108 ** byte to the start of it to prevent it from being recognized.
58109 **
58110 ** Variable iNextHdrOffset is set to the offset at which this
58111 ** problematic header will occur, if it exists. aMagic is used
58112 ** as a temporary buffer to inspect the first couple of bytes of
58113 ** the potential journal header.
58114 */
58115 i64 iNextHdrOffset;
58116 u8 aMagic[8];
58117 u8 zHeader[sizeof(aJournalMagic)+4];
58118
58119 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
58120 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec)sqlite3Put4byte((u8*)&zHeader[sizeof(aJournalMagic)],pPager
->nRec)
;
58121
58122 iNextHdrOffset = journalHdrOffset(pPager);
58123 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
58124 if( rc==SQLITE_OK0 && 0==memcmp(aMagic, aJournalMagic, 8) ){
58125 static const u8 zerobyte = 0;
58126 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
58127 }
58128 if( rc!=SQLITE_OK0 && rc!=SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ){
58129 return rc;
58130 }
58131
58132 /* Write the nRec value into the journal file header. If in
58133 ** full-synchronous mode, sync the journal first. This ensures that
58134 ** all data has really hit the disk before nRec is updated to mark
58135 ** it as a candidate for rollback.
58136 **
58137 ** This is not required if the persistent media supports the
58138 ** SAFE_APPEND property. Because in this case it is not possible
58139 ** for garbage data to be appended to the file, the nRec field
58140 ** is populated with 0xFFFFFFFF when the journal header is written
58141 ** and never needs to be updated.
58142 */
58143 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL0x00000400) ){
58144 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
58145 IOTRACE(("JSYNC %p\n", pPager))
58146 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
58147 if( rc!=SQLITE_OK0 ) return rc;
58148 }
58149 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
58150 rc = sqlite3OsWrite(
58151 pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
58152 );
58153 if( rc!=SQLITE_OK0 ) return rc;
58154 }
58155 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL0x00000400) ){
58156 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
58157 IOTRACE(("JSYNC %p\n", pPager))
58158 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
58159 (pPager->syncFlags==SQLITE_SYNC_FULL0x00003?SQLITE_SYNC_DATAONLY0x00010:0)
58160 );
58161 if( rc!=SQLITE_OK0 ) return rc;
58162 }
58163
58164 pPager->journalHdr = pPager->journalOff;
58165 if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND0x00000200) ){
58166 pPager->nRec = 0;
58167 rc = writeJournalHdr(pPager);
58168 if( rc!=SQLITE_OK0 ) return rc;
58169 }
58170 }else{
58171 pPager->journalHdr = pPager->journalOff;
58172 }
58173 }
58174
58175 /* Unless the pager is in noSync mode, the journal file was just
58176 ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on
58177 ** all pages.
58178 */
58179 sqlite3PcacheClearSyncFlags(pPager->pPCache);
58180 pPager->eState = PAGER_WRITER_DBMOD4;
58181 assert( assert_pager_state(pPager) )((void) (0));
58182 return SQLITE_OK0;
58183}
58184
58185/*
58186** The argument is the first in a linked list of dirty pages connected
58187** by the PgHdr.pDirty pointer. This function writes each one of the
58188** in-memory pages in the list to the database file. The argument may
58189** be NULL, representing an empty list. In this case this function is
58190** a no-op.
58191**
58192** The pager must hold at least a RESERVED lock when this function
58193** is called. Before writing anything to the database file, this lock
58194** is upgraded to an EXCLUSIVE lock. If the lock cannot be obtained,
58195** SQLITE_BUSY is returned and no data is written to the database file.
58196**
58197** If the pager is a temp-file pager and the actual file-system file
58198** is not yet open, it is created and opened before any data is
58199** written out.
58200**
58201** Once the lock has been upgraded and, if necessary, the file opened,
58202** the pages are written out to the database file in list order. Writing
58203** a page is skipped if it meets either of the following criteria:
58204**
58205** * The page number is greater than Pager.dbSize, or
58206** * The PGHDR_DONT_WRITE flag is set on the page.
58207**
58208** If writing out a page causes the database file to grow, Pager.dbFileSize
58209** is updated accordingly. If page 1 is written out, then the value cached
58210** in Pager.dbFileVers[] is updated to match the new value stored in
58211** the database file.
58212**
58213** If everything is successful, SQLITE_OK is returned. If an IO error
58214** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
58215** be obtained, SQLITE_BUSY is returned.
58216*/
58217static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
58218 int rc = SQLITE_OK0; /* Return code */
58219
58220 /* This function is only called for rollback pagers in WRITER_DBMOD state. */
58221 assert( !pagerUseWal(pPager) )((void) (0));
58222 assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD )((void) (0));
58223 assert( pPager->eLock==EXCLUSIVE_LOCK )((void) (0));
58224 assert( isOpen(pPager->fd) || pList->pDirty==0 )((void) (0));
58225
58226 /* If the file is a temp-file has not yet been opened, open it now. It
58227 ** is not possible for rc to be other than SQLITE_OK if this branch
58228 ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
58229 */
58230 if( !isOpen(pPager->fd)((pPager->fd)->pMethods!=0) ){
58231 assert( pPager->tempFile && rc==SQLITE_OK )((void) (0));
58232 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
58233 }
58234
58235 /* Before the first write, give the VFS a hint of what the final
58236 ** file size will be.
58237 */
58238 assert( rc!=SQLITE_OK || isOpen(pPager->fd) )((void) (0));
58239 if( rc==SQLITE_OK0
58240 && pPager->dbHintSize<pPager->dbSize
58241 && (pList->pDirty || pList->pgno>pPager->dbHintSize)
58242 ){
58243 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
58244 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT5, &szFile);
58245 pPager->dbHintSize = pPager->dbSize;
58246 }
58247
58248 while( rc==SQLITE_OK0 && pList ){
58249 Pgno pgno = pList->pgno;
58250
58251 /* If there are dirty pages in the page cache with page numbers greater
58252 ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
58253 ** make the file smaller (presumably by auto-vacuum code). Do not write
58254 ** any such pages to the file.
58255 **
58256 ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
58257 ** set (set by sqlite3PagerDontWrite()).
58258 */
58259 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE0x010) ){
58260 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
58261 char *pData; /* Data to write */
58262
58263 assert( (pList->flags&PGHDR_NEED_SYNC)==0 )((void) (0));
58264 if( pList->pgno==1 ) pager_write_changecounter(pList);
58265
58266 pData = pList->pData;
58267
58268 /* Write out the page data. */
58269 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
58270
58271 /* If page 1 was just written, update Pager.dbFileVers to match
58272 ** the value now stored in the database file. If writing this
58273 ** page caused the database file to grow, update dbFileSize.
58274 */
58275 if( pgno==1 ){
58276 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
58277 }
58278 if( pgno>pPager->dbFileSize ){
58279 pPager->dbFileSize = pgno;
58280 }
58281 pPager->aStat[PAGER_STAT_WRITE2]++;
58282
58283 /* Update any backup objects copying the contents of this pager. */
58284 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
58285
58286 PAGERTRACE(("STORE %d page %d hash(%08x)\n",
58287 PAGERID(pPager), pgno, pager_pagehash(pList)));
58288 IOTRACE(("PGOUT %p %d\n", pPager, pgno));
58289 PAGER_INCR(sqlite3_pager_writedb_count);
58290 }else{
58291 PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
58292 }
58293 pager_set_pagehash(pList);
58294 pList = pList->pDirty;
58295 }
58296
58297 return rc;
58298}
58299
58300/*
58301** Ensure that the sub-journal file is open. If it is already open, this
58302** function is a no-op.
58303**
58304** SQLITE_OK is returned if everything goes according to plan. An
58305** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen()
58306** fails.
58307*/
58308static int openSubJournal(Pager *pPager){
58309 int rc = SQLITE_OK0;
58310 if( !isOpen(pPager->sjfd)((pPager->sjfd)->pMethods!=0) ){
58311 const int flags = SQLITE_OPEN_SUBJOURNAL0x00002000 | SQLITE_OPEN_READWRITE0x00000002
58312 | SQLITE_OPEN_CREATE0x00000004 | SQLITE_OPEN_EXCLUSIVE0x00000010
58313 | SQLITE_OPEN_DELETEONCLOSE0x00000008;
58314 int nStmtSpill = sqlite3Config.nStmtSpill;
58315 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY4 || pPager->subjInMemory ){
58316 nStmtSpill = -1;
58317 }
58318 rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill);
58319 }
58320 return rc;
58321}
58322
58323/*
58324** Append a record of the current state of page pPg to the sub-journal.
58325**
58326** If successful, set the bit corresponding to pPg->pgno in the bitvecs
58327** for all open savepoints before returning.
58328**
58329** This function returns SQLITE_OK if everything is successful, an IO
58330** error code if the attempt to write to the sub-journal fails, or
58331** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint
58332** bitvec.
58333*/
58334static int subjournalPage(PgHdr *pPg){
58335 int rc = SQLITE_OK0;
58336 Pager *pPager = pPg->pPager;
58337 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF2 ){
58338
58339 /* Open the sub-journal, if it has not already been opened */
58340 assert( pPager->useJournal )((void) (0));
58341 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) )((void) (0));
58342 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 )((void) (0));
58343 assert( pagerUseWal(pPager)((void) (0))
58344 || pageInJournal(pPager, pPg)((void) (0))
58345 || pPg->pgno>pPager->dbOrigSize((void) (0))
58346 )((void) (0));
58347 rc = openSubJournal(pPager);
58348
58349 /* If the sub-journal was opened successfully (or was already open),
58350 ** write the journal record into the file. */
58351 if( rc==SQLITE_OK0 ){
58352 void *pData = pPg->pData;
58353 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
58354 char *pData2;
58355 pData2 = pData;
58356 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
58357 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
58358 if( rc==SQLITE_OK0 ){
58359 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
58360 }
58361 }
58362 }
58363 if( rc==SQLITE_OK0 ){
58364 pPager->nSubRec++;
58365 assert( pPager->nSavepoint>0 )((void) (0));
58366 rc = addToSavepointBitvecs(pPager, pPg->pgno);
58367 }
58368 return rc;
58369}
58370static int subjournalPageIfRequired(PgHdr *pPg){
58371 if( subjRequiresPage(pPg) ){
58372 return subjournalPage(pPg);
58373 }else{
58374 return SQLITE_OK0;
58375 }
58376}
58377
58378/*
58379** This function is called by the pcache layer when it has reached some
58380** soft memory limit. The first argument is a pointer to a Pager object
58381** (cast as a void*). The pager is always 'purgeable' (not an in-memory
58382** database). The second argument is a reference to a page that is
58383** currently dirty but has no outstanding references. The page
58384** is always associated with the Pager object passed as the first
58385** argument.
58386**
58387** The job of this function is to make pPg clean by writing its contents
58388** out to the database file, if possible. This may involve syncing the
58389** journal file.
58390**
58391** If successful, sqlite3PcacheMakeClean() is called on the page and
58392** SQLITE_OK returned. If an IO error occurs while trying to make the
58393** page clean, the IO error code is returned. If the page cannot be
58394** made clean for some other reason, but no error occurs, then SQLITE_OK
58395** is returned by sqlite3PcacheMakeClean() is not called.
58396*/
58397static int pagerStress(void *p, PgHdr *pPg){
58398 Pager *pPager = (Pager *)p;
58399 int rc = SQLITE_OK0;
58400
58401 assert( pPg->pPager==pPager )((void) (0));
58402 assert( pPg->flags&PGHDR_DIRTY )((void) (0));
58403
58404 /* The doNotSpill NOSYNC bit is set during times when doing a sync of
58405 ** journal (and adding a new header) is not allowed. This occurs
58406 ** during calls to sqlite3PagerWrite() while trying to journal multiple
58407 ** pages belonging to the same sector.
58408 **
58409 ** The doNotSpill ROLLBACK and OFF bits inhibits all cache spilling
58410 ** regardless of whether or not a sync is required. This is set during
58411 ** a rollback or by user request, respectively.
58412 **
58413 ** Spilling is also prohibited when in an error state since that could
58414 ** lead to database corruption. In the current implementation it
58415 ** is impossible for sqlite3PcacheFetch() to be called with createFlag==3
58416 ** while in the error state, hence it is impossible for this routine to
58417 ** be called in the error state. Nevertheless, we include a NEVER()
58418 ** test for the error state as a safeguard against future changes.
58419 */
58420 if( NEVER(pPager->errCode)(pPager->errCode) ) return SQLITE_OK0;
58421 testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
58422 testcase( pPager->doNotSpill & SPILLFLAG_OFF );
58423 testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
58424 if( pPager->doNotSpill
58425 && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK0x02|SPILLFLAG_OFF0x01))!=0
58426 || (pPg->flags & PGHDR_NEED_SYNC0x008)!=0)
58427 ){
58428 return SQLITE_OK0;
58429 }
58430
58431 pPager->aStat[PAGER_STAT_SPILL3]++;
58432 pPg->pDirty = 0;
58433 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
58434 /* Write a single frame for this page to the log. */
58435 rc = subjournalPageIfRequired(pPg);
58436 if( rc==SQLITE_OK0 ){
58437 rc = pagerWalFrames(pPager, pPg, 0, 0);
58438 }
58439 }else{
58440
58441#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
58442 if( pPager->tempFile==0 ){
58443 rc = sqlite3JournalCreate(pPager->jfd);
58444 if( rc!=SQLITE_OK0 ) return pager_error(pPager, rc);
58445 }
58446#endif
58447
58448 /* Sync the journal file if required. */
58449 if( pPg->flags&PGHDR_NEED_SYNC0x008
58450 || pPager->eState==PAGER_WRITER_CACHEMOD3
58451 ){
58452 rc = syncJournal(pPager, 1);
58453 }
58454
58455 /* Write the contents of the page out to the database file. */
58456 if( rc==SQLITE_OK0 ){
58457 assert( (pPg->flags&PGHDR_NEED_SYNC)==0 )((void) (0));
58458 rc = pager_write_pagelist(pPager, pPg);
58459 }
58460 }
58461
58462 /* Mark the page as clean. */
58463 if( rc==SQLITE_OK0 ){
58464 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
58465 sqlite3PcacheMakeClean(pPg);
58466 }
58467
58468 return pager_error(pPager, rc);
58469}
58470
58471/*
58472** Flush all unreferenced dirty pages to disk.
58473*/
58474SQLITE_PRIVATEstatic int sqlite3PagerFlush(Pager *pPager){
58475 int rc = pPager->errCode;
58476 if( !MEMDBpPager->memDb ){
58477 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
58478 assert( assert_pager_state(pPager) )((void) (0));
58479 while( rc==SQLITE_OK0 && pList ){
58480 PgHdr *pNext = pList->pDirty;
58481 if( pList->nRef==0 ){
58482 rc = pagerStress((void*)pPager, pList);
58483 }
58484 pList = pNext;
58485 }
58486 }
58487
58488 return rc;
58489}
58490
58491/*
58492** Allocate and initialize a new Pager object and put a pointer to it
58493** in *ppPager. The pager should eventually be freed by passing it
58494** to sqlite3PagerClose().
58495**
58496** The zFilename argument is the path to the database file to open.
58497** If zFilename is NULL then a randomly-named temporary file is created
58498** and used as the file to be cached. Temporary files are be deleted
58499** automatically when they are closed. If zFilename is ":memory:" then
58500** all information is held in cache. It is never written to disk.
58501** This can be used to implement an in-memory database.
58502**
58503** The nExtra parameter specifies the number of bytes of space allocated
58504** along with each page reference. This space is available to the user
58505** via the sqlite3PagerGetExtra() API. When a new page is allocated, the
58506** first 8 bytes of this space are zeroed but the remainder is uninitialized.
58507** (The extra space is used by btree as the MemPage object.)
58508**
58509** The flags argument is used to specify properties that affect the
58510** operation of the pager. It should be passed some bitwise combination
58511** of the PAGER_* flags.
58512**
58513** The vfsFlags parameter is a bitmask to pass to the flags parameter
58514** of the xOpen() method of the supplied VFS when opening files.
58515**
58516** If the pager object is allocated and the specified file opened
58517** successfully, SQLITE_OK is returned and *ppPager set to point to
58518** the new pager object. If an error occurs, *ppPager is set to NULL
58519** and error code returned. This function may return SQLITE_NOMEM
58520** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or
58521** various SQLITE_IO_XXX errors.
58522*/
58523SQLITE_PRIVATEstatic int sqlite3PagerOpen(
58524 sqlite3_vfs *pVfs, /* The virtual file system to use */
58525 Pager **ppPager, /* OUT: Return the Pager structure here */
58526 const char *zFilename, /* Name of the database file to open */
58527 int nExtra, /* Extra bytes append to each in-memory page */
58528 int flags, /* flags controlling this file */
58529 int vfsFlags, /* flags passed through to sqlite3_vfs.xOpen() */
58530 void (*xReinit)(DbPage*) /* Function to reinitialize pages */
58531){
58532 u8 *pPtr;
58533 Pager *pPager = 0; /* Pager object to allocate and return */
58534 int rc = SQLITE_OK0; /* Return code */
58535 int tempFile = 0; /* True for temp files (incl. in-memory files) */
58536 int memDb = 0; /* True if this is an in-memory file */
58537#ifndef SQLITE_OMIT_DESERIALIZE
58538 int memJM = 0; /* Memory journal mode */
58539#else
58540# define memJM 0
58541#endif
58542 int readOnly = 0; /* True if this is a read-only file */
58543 int journalFileSize; /* Bytes to allocate for each journal fd */
58544 char *zPathname = 0; /* Full path to database file */
58545 int nPathname = 0; /* Number of bytes in zPathname */
58546 int useJournal = (flags & PAGER_OMIT_JOURNAL0x0001)==0; /* False to omit journal */
58547 int pcacheSize = sqlite3PcacheSize(); /* Bytes to allocate for PCache */
58548 u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE4096; /* Default page size */
58549 const char *zUri = 0; /* URI args to copy */
58550 int nUriByte = 1; /* Number of bytes of URI args at *zUri */
58551 int nUri = 0; /* Number of URI parameters */
58552
58553 /* Figure out how much space is required for each journal file-handle
58554 ** (there are two of them, the main journal and the sub-journal). */
58555 journalFileSize = ROUND8(sqlite3JournalSize(pVfs))(((sqlite3JournalSize(pVfs))+7)&~7);
58556
58557 /* Set the output variable to NULL in case an error occurs. */
58558 *ppPager = 0;
58559
58560#ifndef SQLITE_OMIT_MEMORYDB
58561 if( flags & PAGER_MEMORY0x0002 ){
58562 memDb = 1;
58563 if( zFilename && zFilename[0] ){
58564 zPathname = sqlite3DbStrDup(0, zFilename);
58565 if( zPathname==0 ) return SQLITE_NOMEM_BKPT7;
58566 nPathname = sqlite3Strlen30(zPathname);
58567 zFilename = 0;
58568 }
58569 }
58570#endif
58571
58572 /* Compute and store the full pathname in an allocated buffer pointed
58573 ** to by zPathname, length nPathname. Or, if this is a temporary file,
58574 ** leave both nPathname and zPathname set to 0.
58575 */
58576 if( zFilename && zFilename[0] ){
58577 const char *z;
58578 nPathname = pVfs->mxPathname+1;
58579 zPathname = sqlite3DbMallocRaw(0, nPathname*2);
58580 if( zPathname==0 ){
58581 return SQLITE_NOMEM_BKPT7;
58582 }
58583 zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
58584 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
58585 if( rc!=SQLITE_OK0 ){
58586 if( rc==SQLITE_OK_SYMLINK(0 | (2<<8)) ){
58587 if( vfsFlags & SQLITE_OPEN_NOFOLLOW0x01000000 ){
58588 rc = SQLITE_CANTOPEN_SYMLINK(14 | (6<<8));
58589 }else{
58590 rc = SQLITE_OK0;
58591 }
58592 }
58593 }
58594 nPathname = sqlite3Strlen30(zPathname);
58595 z = zUri = &zFilename[sqlite3Strlen30(zFilename)+1];
58596 while( *z ){
58597 z += strlen(z)+1;
58598 z += strlen(z)+1;
58599 nUri++;
58600 }
58601 nUriByte = (int)(&z[1] - zUri);
58602 assert( nUriByte>=1 )((void) (0));
58603 if( rc==SQLITE_OK0 && nPathname+8>pVfs->mxPathname ){
58604 /* This branch is taken when the journal path required by
58605 ** the database being opened will be more than pVfs->mxPathname
58606 ** bytes in length. This means the database cannot be opened,
58607 ** as it will not be possible to open the journal file or even
58608 ** check for a hot-journal before reading.
58609 */
58610 rc = SQLITE_CANTOPEN_BKPTsqlite3CantopenError(58610);
58611 }
58612 if( rc!=SQLITE_OK0 ){
58613 sqlite3DbFree(0, zPathname);
58614 return rc;
58615 }
58616 }
58617
58618 /* Allocate memory for the Pager structure, PCache object, the
58619 ** three file descriptors, the database file name and the journal
58620 ** file name. The layout in memory is as follows:
58621 **
58622 ** Pager object (sizeof(Pager) bytes)
58623 ** PCache object (sqlite3PcacheSize() bytes)
58624 ** Database file handle (pVfs->szOsFile bytes)
58625 ** Sub-journal file handle (journalFileSize bytes)
58626 ** Main journal file handle (journalFileSize bytes)
58627 ** Ptr back to the Pager (sizeof(Pager*) bytes)
58628 ** \0\0\0\0 database prefix (4 bytes)
58629 ** Database file name (nPathname+1 bytes)
58630 ** URI query parameters (nUriByte bytes)
58631 ** Journal filename (nPathname+8+1 bytes)
58632 ** WAL filename (nPathname+4+1 bytes)
58633 ** \0\0\0 terminator (3 bytes)
58634 **
58635 ** Some 3rd-party software, over which we have no control, depends on
58636 ** the specific order of the filenames and the \0 separators between them
58637 ** so that it can (for example) find the database filename given the WAL
58638 ** filename without using the sqlite3_filename_database() API. This is a
58639 ** misuse of SQLite and a bug in the 3rd-party software, but the 3rd-party
58640 ** software is in widespread use, so we try to avoid changing the filename
58641 ** order and formatting if possible. In particular, the details of the
58642 ** filename format expected by 3rd-party software should be as follows:
58643 **
58644 ** - Main Database Path
58645 ** - \0
58646 ** - Multiple URI components consisting of:
58647 ** - Key
58648 ** - \0
58649 ** - Value
58650 ** - \0
58651 ** - \0
58652 ** - Journal Path
58653 ** - \0
58654 ** - WAL Path (zWALName)
58655 ** - \0
58656 **
58657 ** The sqlite3_create_filename() interface and the databaseFilename() utility
58658 ** that is used by sqlite3_filename_database() and kin also depend on the
58659 ** specific formatting and order of the various filenames, so if the format
58660 ** changes here, be sure to change it there as well.
58661 */
58662 pPtr = (u8 *)sqlite3MallocZero(
58663 ROUND8(sizeof(*pPager))(((sizeof(*pPager))+7)&~7) + /* Pager structure */
58664 ROUND8(pcacheSize)(((pcacheSize)+7)&~7) + /* PCache object */
58665 ROUND8(pVfs->szOsFile)(((pVfs->szOsFile)+7)&~7) + /* The main db file */
58666 journalFileSize * 2 + /* The two journal files */
58667 sizeof(pPager) + /* Space to hold a pointer */
58668 4 + /* Database prefix */
58669 nPathname + 1 + /* database filename */
58670 nUriByte + /* query parameters */
58671 nPathname + 8 + 1 + /* Journal filename */
58672#ifndef SQLITE_OMIT_WAL
58673 nPathname + 4 + 1 + /* WAL filename */
58674#endif
58675 3 /* Terminator */
58676 );
58677 assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) )((void) (0));
58678 if( !pPtr ){
58679 sqlite3DbFree(0, zPathname);
58680 return SQLITE_NOMEM_BKPT7;
58681 }
58682 pPager = (Pager*)pPtr; pPtr += ROUND8(sizeof(*pPager))(((sizeof(*pPager))+7)&~7);
58683 pPager->pPCache = (PCache*)pPtr; pPtr += ROUND8(pcacheSize)(((pcacheSize)+7)&~7);
58684 pPager->fd = (sqlite3_file*)pPtr; pPtr += ROUND8(pVfs->szOsFile)(((pVfs->szOsFile)+7)&~7);
58685 pPager->sjfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
58686 pPager->jfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
58687 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) )((void) (0));
58688 memcpy(pPtr, &pPager, sizeof(pPager)); pPtr += sizeof(pPager);
58689
58690 /* Fill in the Pager.zFilename and pPager.zQueryParam fields */
58691 pPtr += 4; /* Skip zero prefix */
58692 pPager->zFilename = (char*)pPtr;
58693 if( nPathname>0 ){
58694 memcpy(pPtr, zPathname, nPathname); pPtr += nPathname + 1;
58695 if( zUri ){
58696 memcpy(pPtr, zUri, nUriByte); pPtr += nUriByte;
58697 }else{
58698 pPtr++;
58699 }
58700 }
58701
58702
58703 /* Fill in Pager.zJournal */
58704 if( nPathname>0 ){
58705 pPager->zJournal = (char*)pPtr;
58706 memcpy(pPtr, zPathname, nPathname); pPtr += nPathname;
58707 memcpy(pPtr, "-journal",8); pPtr += 8 + 1;
58708#ifdef SQLITE_ENABLE_8_3_NAMES
58709 sqlite3FileSuffix3(zFilename,pPager->zJournal);
58710 pPtr = (u8*)(pPager->zJournal + sqlite3Strlen30(pPager->zJournal)+1);
58711#endif
58712 }else{
58713 pPager->zJournal = 0;
58714 }
58715
58716#ifndef SQLITE_OMIT_WAL
58717 /* Fill in Pager.zWal */
58718 if( nPathname>0 ){
58719 pPager->zWal = (char*)pPtr;
58720 memcpy(pPtr, zPathname, nPathname); pPtr += nPathname;
58721 memcpy(pPtr, "-wal", 4); pPtr += 4 + 1;
58722#ifdef SQLITE_ENABLE_8_3_NAMES
58723 sqlite3FileSuffix3(zFilename, pPager->zWal);
58724 pPtr = (u8*)(pPager->zWal + sqlite3Strlen30(pPager->zWal)+1);
58725#endif
58726 }else{
58727 pPager->zWal = 0;
58728 }
58729#endif
58730 (void)pPtr; /* Suppress warning about unused pPtr value */
58731
58732 if( nPathname ) sqlite3DbFree(0, zPathname);
58733 pPager->pVfs = pVfs;
58734 pPager->vfsFlags = vfsFlags;
58735
58736 /* Open the pager file.
58737 */
58738 if( zFilename && zFilename[0] ){
58739 int fout = 0; /* VFS flags returned by xOpen() */
58740 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
58741 assert( !memDb )((void) (0));
58742#ifndef SQLITE_OMIT_DESERIALIZE
58743 pPager->memVfs = memJM = (fout&SQLITE_OPEN_MEMORY0x00000080)!=0;
58744#endif
58745 readOnly = (fout&SQLITE_OPEN_READONLY0x00000001)!=0;
58746
58747 /* If the file was successfully opened for read/write access,
58748 ** choose a default page size in case we have to create the
58749 ** database file. The default page size is the maximum of:
58750 **
58751 ** + SQLITE_DEFAULT_PAGE_SIZE,
58752 ** + The value returned by sqlite3OsSectorSize()
58753 ** + The largest page size that can be written atomically.
58754 */
58755 if( rc==SQLITE_OK0 ){
58756 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
58757 if( !readOnly ){
58758 setSectorSize(pPager);
58759 assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE)((void) (0));
58760 if( szPageDflt<pPager->sectorSize ){
58761 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE8192 ){
58762 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE8192;
58763 }else{
58764 szPageDflt = (u32)pPager->sectorSize;
58765 }
58766 }
58767#ifdef SQLITE_ENABLE_ATOMIC_WRITE
58768 {
58769 int ii;
58770 assert(SQLITE_IOCAP_ATOMIC512==(512>>8))((void) (0));
58771 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8))((void) (0));
58772 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536)((void) (0));
58773 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE8192; ii=ii*2){
58774 if( iDc&(SQLITE_IOCAP_ATOMIC0x00000001|(ii>>8)) ){
58775 szPageDflt = ii;
58776 }
58777 }
58778 }
58779#endif
58780 }
58781 pPager->noLock = sqlite3_uri_boolean(pPager->zFilename, "nolock", 0);
58782 if( (iDc & SQLITE_IOCAP_IMMUTABLE0x00002000)!=0
58783 || sqlite3_uri_boolean(pPager->zFilename, "immutable", 0) ){
58784 vfsFlags |= SQLITE_OPEN_READONLY0x00000001;
58785 goto act_like_temp_file;
58786 }
58787 }
58788 }else{
58789 /* If a temporary file is requested, it is not opened immediately.
58790 ** In this case we accept the default page size and delay actually
58791 ** opening the file until the first call to OsWrite().
58792 **
58793 ** This branch is also run for an in-memory database. An in-memory
58794 ** database is the same as a temp-file that is never written out to
58795 ** disk and uses an in-memory rollback journal.
58796 **
58797 ** This branch also runs for files marked as immutable.
58798 */
58799act_like_temp_file:
58800 tempFile = 1;
58801 pPager->eState = PAGER_READER1; /* Pretend we already have a lock */
58802 pPager->eLock = EXCLUSIVE_LOCK4; /* Pretend we are in EXCLUSIVE mode */
58803 pPager->noLock = 1; /* Do no locking */
58804 readOnly = (vfsFlags&SQLITE_OPEN_READONLY0x00000001);
58805 }
58806
58807 /* The following call to PagerSetPagesize() serves to set the value of
58808 ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
58809 */
58810 if( rc==SQLITE_OK0 ){
58811 assert( pPager->memDb==0 )((void) (0));
58812 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
58813 testcase( rc!=SQLITE_OK );
58814 }
58815
58816 /* Initialize the PCache object. */
58817 if( rc==SQLITE_OK0 ){
58818 nExtra = ROUND8(nExtra)(((nExtra)+7)&~7);
58819 assert( nExtra>=8 && nExtra<1000 )((void) (0));
58820 rc = sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
58821 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
58822 }
58823
58824 /* If an error occurred above, free the Pager structure and close the file.
58825 */
58826 if( rc!=SQLITE_OK0 ){
58827 sqlite3OsClose(pPager->fd);
58828 sqlite3PageFree(pPager->pTmpSpace);
58829 sqlite3_free(pPager);
58830 return rc;
58831 }
58832
58833 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
58834 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
58835
58836 pPager->useJournal = (u8)useJournal;
58837 /* pPager->stmtOpen = 0; */
58838 /* pPager->stmtInUse = 0; */
58839 /* pPager->nRef = 0; */
58840 /* pPager->stmtSize = 0; */
58841 /* pPager->stmtJSize = 0; */
58842 /* pPager->nPage = 0; */
58843 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT1073741823;
58844 /* pPager->state = PAGER_UNLOCK; */
58845 /* pPager->errMask = 0; */
58846 pPager->tempFile = (u8)tempFile;
58847 assert( tempFile==PAGER_LOCKINGMODE_NORMAL((void) (0))
58848 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE )((void) (0));
58849 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 )((void) (0));
58850 pPager->exclusiveMode = (u8)tempFile;
58851 pPager->changeCountDone = pPager->tempFile;
58852 pPager->memDb = (u8)memDb;
58853 pPager->readOnly = (u8)readOnly;
58854 assert( useJournal || pPager->tempFile )((void) (0));
58855 pPager->noSync = pPager->tempFile;
58856 if( pPager->noSync ){
58857 assert( pPager->fullSync==0 )((void) (0));
58858 assert( pPager->extraSync==0 )((void) (0));
58859 assert( pPager->syncFlags==0 )((void) (0));
58860 assert( pPager->walSyncFlags==0 )((void) (0));
58861 }else{
58862 pPager->fullSync = 1;
58863 pPager->extraSync = 0;
58864 pPager->syncFlags = SQLITE_SYNC_NORMAL0x00002;
58865 pPager->walSyncFlags = SQLITE_SYNC_NORMAL0x00002 | (SQLITE_SYNC_NORMAL0x00002<<2);
58866 }
58867 /* pPager->pFirst = 0; */
58868 /* pPager->pFirstSynced = 0; */
58869 /* pPager->pLast = 0; */
58870 pPager->nExtra = (u16)nExtra;
58871 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT32768;
58872 assert( isOpen(pPager->fd) || tempFile )((void) (0));
58873 setSectorSize(pPager);
58874 if( !useJournal ){
58875 pPager->journalMode = PAGER_JOURNALMODE_OFF2;
58876 }else if( memDb || memJM ){
58877 pPager->journalMode = PAGER_JOURNALMODE_MEMORY4;
58878 }
58879 /* pPager->xBusyHandler = 0; */
58880 /* pPager->pBusyHandlerArg = 0; */
58881 pPager->xReiniter = xReinit;
58882 setGetterMethod(pPager);
58883 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
58884 /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
58885
58886 *ppPager = pPager;
58887 return SQLITE_OK0;
58888}
58889
58890/*
58891** Return the sqlite3_file for the main database given the name
58892** of the corresonding WAL or Journal name as passed into
58893** xOpen.
58894*/
58895SQLITE_API sqlite3_file *sqlite3_database_file_object(const char *zName){
58896 Pager *pPager;
58897 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
58898 zName--;
58899 }
58900 pPager = *(Pager**)(zName - 4 - sizeof(Pager*));
58901 return pPager->fd;
58902}
58903
58904
58905/*
58906** This function is called after transitioning from PAGER_UNLOCK to
58907** PAGER_SHARED state. It tests if there is a hot journal present in
58908** the file-system for the given pager. A hot journal is one that
58909** needs to be played back. According to this function, a hot-journal
58910** file exists if the following criteria are met:
58911**
58912** * The journal file exists in the file system, and
58913** * No process holds a RESERVED or greater lock on the database file, and
58914** * The database file itself is greater than 0 bytes in size, and
58915** * The first byte of the journal file exists and is not 0x00.
58916**
58917** If the current size of the database file is 0 but a journal file
58918** exists, that is probably an old journal left over from a prior
58919** database with the same name. In this case the journal file is
58920** just deleted using OsDelete, *pExists is set to 0 and SQLITE_OK
58921** is returned.
58922**
58923** This routine does not check if there is a super-journal filename
58924** at the end of the file. If there is, and that super-journal file
58925** does not exist, then the journal file is not really hot. In this
58926** case this routine will return a false-positive. The pager_playback()
58927** routine will discover that the journal file is not really hot and
58928** will not roll it back.
58929**
58930** If a hot-journal file is found to exist, *pExists is set to 1 and
58931** SQLITE_OK returned. If no hot-journal file is present, *pExists is
58932** set to 0 and SQLITE_OK returned. If an IO error occurs while trying
58933** to determine whether or not a hot-journal file exists, the IO error
58934** code is returned and the value of *pExists is undefined.
58935*/
58936static int hasHotJournal(Pager *pPager, int *pExists){
58937 sqlite3_vfs * const pVfs = pPager->pVfs;
58938 int rc = SQLITE_OK0; /* Return code */
58939 int exists = 1; /* True if a journal file is present */
58940 int jrnlOpen = !!isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0);
58941
58942 assert( pPager->useJournal )((void) (0));
58943 assert( isOpen(pPager->fd) )((void) (0));
58944 assert( pPager->eState==PAGER_OPEN )((void) (0));
58945
58946 assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &((void) (0))
58947 SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN((void) (0))
58948 ))((void) (0));
58949
58950 *pExists = 0;
58951 if( !jrnlOpen ){
58952 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS0, &exists);
58953 }
58954 if( rc==SQLITE_OK0 && exists ){
58955 int locked = 0; /* True if some process holds a RESERVED lock */
58956
58957 /* Race condition here: Another process might have been holding the
58958 ** the RESERVED lock and have a journal open at the sqlite3OsAccess()
58959 ** call above, but then delete the journal and drop the lock before
58960 ** we get to the following sqlite3OsCheckReservedLock() call. If that
58961 ** is the case, this routine might think there is a hot journal when
58962 ** in fact there is none. This results in a false-positive which will
58963 ** be dealt with by the playback routine. Ticket #3883.
58964 */
58965 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
58966 if( rc==SQLITE_OK0 && !locked ){
58967 Pgno nPage; /* Number of pages in database file */
58968
58969 assert( pPager->tempFile==0 )((void) (0));
58970 rc = pagerPagecount(pPager, &nPage);
58971 if( rc==SQLITE_OK0 ){
58972 /* If the database is zero pages in size, that means that either (1) the
58973 ** journal is a remnant from a prior database with the same name where
58974 ** the database file but not the journal was deleted, or (2) the initial
58975 ** transaction that populates a new database is being rolled back.
58976 ** In either case, the journal file can be deleted. However, take care
58977 ** not to delete the journal file if it is already open due to
58978 ** journal_mode=PERSIST.
58979 */
58980 if( nPage==0 && !jrnlOpen ){
58981 sqlite3BeginBenignMalloc();
58982 if( pagerLockDb(pPager, RESERVED_LOCK2)==SQLITE_OK0 ){
58983 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
58984 if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK1);
58985 }
58986 sqlite3EndBenignMalloc();
58987 }else{
58988 /* The journal file exists and no other connection has a reserved
58989 ** or greater lock on the database file. Now check that there is
58990 ** at least one non-zero bytes at the start of the journal file.
58991 ** If there is, then we consider this journal to be hot. If not,
58992 ** it can be ignored.
58993 */
58994 if( !jrnlOpen ){
58995 int f = SQLITE_OPEN_READONLY0x00000001|SQLITE_OPEN_MAIN_JOURNAL0x00000800;
58996 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
58997 }
58998 if( rc==SQLITE_OK0 ){
58999 u8 first = 0;
59000 rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
59001 if( rc==SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ){
59002 rc = SQLITE_OK0;
59003 }
59004 if( !jrnlOpen ){
59005 sqlite3OsClose(pPager->jfd);
59006 }
59007 *pExists = (first!=0);
59008 }else if( rc==SQLITE_CANTOPEN14 ){
59009 /* If we cannot open the rollback journal file in order to see if
59010 ** it has a zero header, that might be due to an I/O error, or
59011 ** it might be due to the race condition described above and in
59012 ** ticket #3883. Either way, assume that the journal is hot.
59013 ** This might be a false positive. But if it is, then the
59014 ** automatic journal playback and recovery mechanism will deal
59015 ** with it under an EXCLUSIVE lock where we do not need to
59016 ** worry so much with race conditions.
59017 */
59018 *pExists = 1;
59019 rc = SQLITE_OK0;
59020 }
59021 }
59022 }
59023 }
59024 }
59025
59026 return rc;
59027}
59028
59029/*
59030** This function is called to obtain a shared lock on the database file.
59031** It is illegal to call sqlite3PagerGet() until after this function
59032** has been successfully called. If a shared-lock is already held when
59033** this function is called, it is a no-op.
59034**
59035** The following operations are also performed by this function.
59036**
59037** 1) If the pager is currently in PAGER_OPEN state (no lock held
59038** on the database file), then an attempt is made to obtain a
59039** SHARED lock on the database file. Immediately after obtaining
59040** the SHARED lock, the file-system is checked for a hot-journal,
59041** which is played back if present. Following any hot-journal
59042** rollback, the contents of the cache are validated by checking
59043** the 'change-counter' field of the database file header and
59044** discarded if they are found to be invalid.
59045**
59046** 2) If the pager is running in exclusive-mode, and there are currently
59047** no outstanding references to any pages, and is in the error state,
59048** then an attempt is made to clear the error state by discarding
59049** the contents of the page cache and rolling back any open journal
59050** file.
59051**
59052** If everything is successful, SQLITE_OK is returned. If an IO error
59053** occurs while locking the database, checking for a hot-journal file or
59054** rolling back a journal file, the IO error code is returned.
59055*/
59056SQLITE_PRIVATEstatic int sqlite3PagerSharedLock(Pager *pPager){
59057 int rc = SQLITE_OK0; /* Return code */
59058
59059 /* This routine is only called from b-tree and only when there are no
59060 ** outstanding pages. This implies that the pager state should either
59061 ** be OPEN or READER. READER is only possible if the pager is or was in
59062 ** exclusive access mode. */
59063 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 )((void) (0));
59064 assert( assert_pager_state(pPager) )((void) (0));
59065 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER )((void) (0));
59066 assert( pPager->errCode==SQLITE_OK )((void) (0));
59067
59068 if( !pagerUseWal(pPager)((pPager)->pWal!=0) && pPager->eState==PAGER_OPEN0 ){
59069 int bHotJournal = 1; /* True if there exists a hot journal-file */
59070
59071 assert( !MEMDB )((void) (0));
59072 assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK )((void) (0));
59073
59074 rc = pager_wait_on_lock(pPager, SHARED_LOCK1);
59075 if( rc!=SQLITE_OK0 ){
59076 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK )((void) (0));
59077 goto failed;
59078 }
59079
59080 /* If a journal file exists, and there is no RESERVED lock on the
59081 ** database file, then it either needs to be played back or deleted.
59082 */
59083 if( pPager->eLock<=SHARED_LOCK1 ){
59084 rc = hasHotJournal(pPager, &bHotJournal);
59085 }
59086 if( rc!=SQLITE_OK0 ){
59087 goto failed;
59088 }
59089 if( bHotJournal ){
59090 if( pPager->readOnly ){
59091 rc = SQLITE_READONLY_ROLLBACK(8 | (3<<8));
59092 goto failed;
59093 }
59094
59095 /* Get an EXCLUSIVE lock on the database file. At this point it is
59096 ** important that a RESERVED lock is not obtained on the way to the
59097 ** EXCLUSIVE lock. If it were, another process might open the
59098 ** database file, detect the RESERVED lock, and conclude that the
59099 ** database is safe to read while this process is still rolling the
59100 ** hot-journal back.
59101 **
59102 ** Because the intermediate RESERVED lock is not requested, any
59103 ** other process attempting to access the database file will get to
59104 ** this point in the code and fail to obtain its own EXCLUSIVE lock
59105 ** on the database file.
59106 **
59107 ** Unless the pager is in locking_mode=exclusive mode, the lock is
59108 ** downgraded to SHARED_LOCK before this function returns.
59109 */
59110 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK4);
59111 if( rc!=SQLITE_OK0 ){
59112 goto failed;
59113 }
59114
59115 /* If it is not already open and the file exists on disk, open the
59116 ** journal for read/write access. Write access is required because
59117 ** in exclusive-access mode the file descriptor will be kept open
59118 ** and possibly used for a transaction later on. Also, write-access
59119 ** is usually required to finalize the journal in journal_mode=persist
59120 ** mode (and also for journal_mode=truncate on some systems).
59121 **
59122 ** If the journal does not exist, it usually means that some
59123 ** other connection managed to get in and roll it back before
59124 ** this connection obtained the exclusive lock above. Or, it
59125 ** may mean that the pager was in the error-state when this
59126 ** function was called and the journal file does not exist.
59127 */
59128 if( !isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) && pPager->journalMode!=PAGER_JOURNALMODE_OFF2 ){
59129 sqlite3_vfs * const pVfs = pPager->pVfs;
59130 int bExists; /* True if journal file exists */
59131 rc = sqlite3OsAccess(
59132 pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS0, &bExists);
59133 if( rc==SQLITE_OK0 && bExists ){
59134 int fout = 0;
59135 int f = SQLITE_OPEN_READWRITE0x00000002|SQLITE_OPEN_MAIN_JOURNAL0x00000800;
59136 assert( !pPager->tempFile )((void) (0));
59137 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
59138 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) )((void) (0));
59139 if( rc==SQLITE_OK0 && fout&SQLITE_OPEN_READONLY0x00000001 ){
59140 rc = SQLITE_CANTOPEN_BKPTsqlite3CantopenError(59140);
59141 sqlite3OsClose(pPager->jfd);
59142 }
59143 }
59144 }
59145
59146 /* Playback and delete the journal. Drop the database write
59147 ** lock and reacquire the read lock. Purge the cache before
59148 ** playing back the hot-journal so that we don't end up with
59149 ** an inconsistent cache. Sync the hot journal before playing
59150 ** it back since the process that crashed and left the hot journal
59151 ** probably did not sync it and we are required to always sync
59152 ** the journal before playing it back.
59153 */
59154 if( isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) ){
59155 assert( rc==SQLITE_OK )((void) (0));
59156 rc = pagerSyncHotJournal(pPager);
59157 if( rc==SQLITE_OK0 ){
59158 rc = pager_playback(pPager, !pPager->tempFile);
59159 pPager->eState = PAGER_OPEN0;
59160 }
59161 }else if( !pPager->exclusiveMode ){
59162 pagerUnlockDb(pPager, SHARED_LOCK1);
59163 }
59164
59165 if( rc!=SQLITE_OK0 ){
59166 /* This branch is taken if an error occurs while trying to open
59167 ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
59168 ** pager_unlock() routine will be called before returning to unlock
59169 ** the file. If the unlock attempt fails, then Pager.eLock must be
59170 ** set to UNKNOWN_LOCK (see the comment above the #define for
59171 ** UNKNOWN_LOCK above for an explanation).
59172 **
59173 ** In order to get pager_unlock() to do this, set Pager.eState to
59174 ** PAGER_ERROR now. This is not actually counted as a transition
59175 ** to ERROR state in the state diagram at the top of this file,
59176 ** since we know that the same call to pager_unlock() will very
59177 ** shortly transition the pager object to the OPEN state. Calling
59178 ** assert_pager_state() would fail now, as it should not be possible
59179 ** to be in ERROR state when there are zero outstanding page
59180 ** references.
59181 */
59182 pager_error(pPager, rc);
59183 goto failed;
59184 }
59185
59186 assert( pPager->eState==PAGER_OPEN )((void) (0));
59187 assert( (pPager->eLock==SHARED_LOCK)((void) (0))
59188 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)((void) (0))
59189 )((void) (0));
59190 }
59191
59192 if( !pPager->tempFile && pPager->hasHeldSharedLock ){
59193 /* The shared-lock has just been acquired then check to
59194 ** see if the database has been modified. If the database has changed,
59195 ** flush the cache. The hasHeldSharedLock flag prevents this from
59196 ** occurring on the very first access to a file, in order to save a
59197 ** single unnecessary sqlite3OsRead() call at the start-up.
59198 **
59199 ** Database changes are detected by looking at 15 bytes beginning
59200 ** at offset 24 into the file. The first 4 of these 16 bytes are
59201 ** a 32-bit counter that is incremented with each change. The
59202 ** other bytes change randomly with each file change when
59203 ** a codec is in use.
59204 **
59205 ** There is a vanishingly small chance that a change will not be
59206 ** detected. The chance of an undetected change is so small that
59207 ** it can be neglected.
59208 */
59209 char dbFileVers[sizeof(pPager->dbFileVers)];
59210
59211 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
59212 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
59213 if( rc!=SQLITE_OK0 ){
59214 if( rc!=SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ){
59215 goto failed;
59216 }
59217 memset(dbFileVers, 0, sizeof(dbFileVers));
59218 }
59219
59220 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
59221 pager_reset(pPager);
59222
59223 /* Unmap the database file. It is possible that external processes
59224 ** may have truncated the database file and then extended it back
59225 ** to its original size while this process was not holding a lock.
59226 ** In this case there may exist a Pager.pMap mapping that appears
59227 ** to be the right size but is not actually valid. Avoid this
59228 ** possibility by unmapping the db here. */
59229 if( USEFETCH(pPager)((pPager)->bUseFetch) ){
59230 sqlite3OsUnfetch(pPager->fd, 0, 0);
59231 }
59232 }
59233 }
59234
59235 /* If there is a WAL file in the file-system, open this database in WAL
59236 ** mode. Otherwise, the following function call is a no-op.
59237 */
59238 rc = pagerOpenWalIfPresent(pPager);
59239#ifndef SQLITE_OMIT_WAL
59240 assert( pPager->pWal==0 || rc==SQLITE_OK )((void) (0));
59241#endif
59242 }
59243
59244 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
59245 assert( rc==SQLITE_OK )((void) (0));
59246 rc = pagerBeginReadTransaction(pPager);
59247 }
59248
59249 if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN0 && rc==SQLITE_OK0 ){
59250 rc = pagerPagecount(pPager, &pPager->dbSize);
59251 }
59252
59253 failed:
59254 if( rc!=SQLITE_OK0 ){
59255 assert( !MEMDB )((void) (0));
59256 pager_unlock(pPager);
59257 assert( pPager->eState==PAGER_OPEN )((void) (0));
59258 }else{
59259 pPager->eState = PAGER_READER1;
59260 pPager->hasHeldSharedLock = 1;
59261 }
59262 return rc;
59263}
59264
59265/*
59266** If the reference count has reached zero, rollback any active
59267** transaction and unlock the pager.
59268**
59269** Except, in locking_mode=EXCLUSIVE when there is nothing to in
59270** the rollback journal, the unlock is not performed and there is
59271** nothing to rollback, so this routine is a no-op.
59272*/
59273static void pagerUnlockIfUnused(Pager *pPager){
59274 if( sqlite3PcacheRefCount(pPager->pPCache)==0 ){
59275 assert( pPager->nMmapOut==0 )((void) (0)); /* because page1 is never memory mapped */
59276 pagerUnlockAndRollback(pPager);
59277 }
59278}
59279
59280/*
59281** The page getter methods each try to acquire a reference to a
59282** page with page number pgno. If the requested reference is
59283** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
59284**
59285** There are different implementations of the getter method depending
59286** on the current state of the pager.
59287**
59288** getPageNormal() -- The normal getter
59289** getPageError() -- Used if the pager is in an error state
59290** getPageMmap() -- Used if memory-mapped I/O is enabled
59291**
59292** If the requested page is already in the cache, it is returned.
59293** Otherwise, a new page object is allocated and populated with data
59294** read from the database file. In some cases, the pcache module may
59295** choose not to allocate a new page object and may reuse an existing
59296** object with no outstanding references.
59297**
59298** The extra data appended to a page is always initialized to zeros the
59299** first time a page is loaded into memory. If the page requested is
59300** already in the cache when this function is called, then the extra
59301** data is left as it was when the page object was last used.
59302**
59303** If the database image is smaller than the requested page or if
59304** the flags parameter contains the PAGER_GET_NOCONTENT bit and the
59305** requested page is not already stored in the cache, then no
59306** actual disk read occurs. In this case the memory image of the
59307** page is initialized to all zeros.
59308**
59309** If PAGER_GET_NOCONTENT is true, it means that we do not care about
59310** the contents of the page. This occurs in two scenarios:
59311**
59312** a) When reading a free-list leaf page from the database, and
59313**
59314** b) When a savepoint is being rolled back and we need to load
59315** a new page into the cache to be filled with the data read
59316** from the savepoint journal.
59317**
59318** If PAGER_GET_NOCONTENT is true, then the data returned is zeroed instead
59319** of being read from the database. Additionally, the bits corresponding
59320** to pgno in Pager.pInJournal (bitvec of pages already written to the
59321** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
59322** savepoints are set. This means if the page is made writable at any
59323** point in the future, using a call to sqlite3PagerWrite(), its contents
59324** will not be journaled. This saves IO.
59325**
59326** The acquisition might fail for several reasons. In all cases,
59327** an appropriate error code is returned and *ppPage is set to NULL.
59328**
59329** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
59330** to find a page in the in-memory cache first. If the page is not already
59331** in memory, this routine goes to disk to read it in whereas Lookup()
59332** just returns 0. This routine acquires a read-lock the first time it
59333** has to go to disk, and could also playback an old journal if necessary.
59334** Since Lookup() never goes to disk, it never has to deal with locks
59335** or journal files.
59336*/
59337static int getPageNormal(
59338 Pager *pPager, /* The pager open on the database file */
59339 Pgno pgno, /* Page number to fetch */
59340 DbPage **ppPage, /* Write a pointer to the page here */
59341 int flags /* PAGER_GET_XXX flags */
59342){
59343 int rc = SQLITE_OK0;
59344 PgHdr *pPg;
59345 u8 noContent; /* True if PAGER_GET_NOCONTENT is set */
59346 sqlite3_pcache_page *pBase;
59347
59348 assert( pPager->errCode==SQLITE_OK )((void) (0));
59349 assert( pPager->eState>=PAGER_READER )((void) (0));
59350 assert( assert_pager_state(pPager) )((void) (0));
59351 assert( pPager->hasHeldSharedLock==1 )((void) (0));
59352
59353 if( pgno==0 ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(59353);
59354 pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
59355 if( pBase==0 ){
59356 pPg = 0;
59357 rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
59358 if( rc!=SQLITE_OK0 ) goto pager_acquire_err;
59359 if( pBase==0 ){
59360 rc = SQLITE_NOMEM_BKPT7;
59361 goto pager_acquire_err;
59362 }
59363 }
59364 pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
59365 assert( pPg==(*ppPage) )((void) (0));
59366 assert( pPg->pgno==pgno )((void) (0));
59367 assert( pPg->pPager==pPager || pPg->pPager==0 )((void) (0));
59368
59369 noContent = (flags & PAGER_GET_NOCONTENT0x01)!=0;
59370 if( pPg->pPager && !noContent ){
59371 /* In this case the pcache already contains an initialized copy of
59372 ** the page. Return without further ado. */
59373 assert( pgno!=PAGER_SJ_PGNO(pPager) )((void) (0));
59374 pPager->aStat[PAGER_STAT_HIT0]++;
59375 return SQLITE_OK0;
59376
59377 }else{
59378 /* The pager cache has created a new page. Its content needs to
59379 ** be initialized. But first some error checks:
59380 **
59381 ** (*) obsolete. Was: maximum page number is 2^31
59382 ** (2) Never try to fetch the locking page
59383 */
59384 if( pgno==PAGER_SJ_PGNO(pPager)((pPager)->lckPgno) ){
59385 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(59385);
59386 goto pager_acquire_err;
59387 }
59388
59389 pPg->pPager = pPager;
59390
59391 assert( !isOpen(pPager->fd) || !MEMDB )((void) (0));
59392 if( !isOpen(pPager->fd)((pPager->fd)->pMethods!=0) || pPager->dbSize<pgno || noContent ){
59393 if( pgno>pPager->mxPgno ){
59394 rc = SQLITE_FULL13;
59395 goto pager_acquire_err;
59396 }
59397 if( noContent ){
59398 /* Failure to set the bits in the InJournal bit-vectors is benign.
59399 ** It merely means that we might do some extra work to journal a
59400 ** page that does not need to be journaled. Nevertheless, be sure
59401 ** to test the case where a malloc error occurs while trying to set
59402 ** a bit in a bit vector.
59403 */
59404 sqlite3BeginBenignMalloc();
59405 if( pgno<=pPager->dbOrigSize ){
59406 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
59407 testcase( rc==SQLITE_NOMEM );
59408 }
59409 TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
59410 testcase( rc==SQLITE_NOMEM );
59411 sqlite3EndBenignMalloc();
59412 }
59413 memset(pPg->pData, 0, pPager->pageSize);
59414 IOTRACE(("ZERO %p %d\n", pPager, pgno));
59415 }else{
59416 assert( pPg->pPager==pPager )((void) (0));
59417 pPager->aStat[PAGER_STAT_MISS1]++;
59418 rc = readDbPage(pPg);
59419 if( rc!=SQLITE_OK0 ){
59420 goto pager_acquire_err;
59421 }
59422 }
59423 pager_set_pagehash(pPg);
59424 }
59425 return SQLITE_OK0;
59426
59427pager_acquire_err:
59428 assert( rc!=SQLITE_OK )((void) (0));
59429 if( pPg ){
59430 sqlite3PcacheDrop(pPg);
59431 }
59432 pagerUnlockIfUnused(pPager);
59433 *ppPage = 0;
59434 return rc;
59435}
59436
59437#if SQLITE_MAX_MMAP_SIZE20971520>0
59438/* The page getter for when memory-mapped I/O is enabled */
59439static int getPageMMap(
59440 Pager *pPager, /* The pager open on the database file */
59441 Pgno pgno, /* Page number to fetch */
59442 DbPage **ppPage, /* Write a pointer to the page here */
59443 int flags /* PAGER_GET_XXX flags */
59444){
59445 int rc = SQLITE_OK0;
59446 PgHdr *pPg = 0;
59447 u32 iFrame = 0; /* Frame to read from WAL file */
59448
59449 /* It is acceptable to use a read-only (mmap) page for any page except
59450 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
59451 ** flag was specified by the caller. And so long as the db is not a
59452 ** temporary or in-memory database. */
59453 const int bMmapOk = (pgno>1
59454 && (pPager->eState==PAGER_READER1 || (flags & PAGER_GET_READONLY0x02))
59455 );
59456
59457 assert( USEFETCH(pPager) )((void) (0));
59458
59459 /* Optimization note: Adding the "pgno<=1" term before "pgno==0" here
59460 ** allows the compiler optimizer to reuse the results of the "pgno>1"
59461 ** test in the previous statement, and avoid testing pgno==0 in the
59462 ** common case where pgno is large. */
59463 if( pgno<=1 && pgno==0 ){
59464 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(59464);
59465 }
59466 assert( pPager->eState>=PAGER_READER )((void) (0));
59467 assert( assert_pager_state(pPager) )((void) (0));
59468 assert( pPager->hasHeldSharedLock==1 )((void) (0));
59469 assert( pPager->errCode==SQLITE_OK )((void) (0));
59470
59471 if( bMmapOk && pagerUseWal(pPager)((pPager)->pWal!=0) ){
59472 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
59473 if( rc!=SQLITE_OK0 ){
59474 *ppPage = 0;
59475 return rc;
59476 }
59477 }
59478 if( bMmapOk && iFrame==0 ){
59479 void *pData = 0;
59480 rc = sqlite3OsFetch(pPager->fd,
59481 (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
59482 );
59483 if( rc==SQLITE_OK0 && pData ){
59484 if( pPager->eState>PAGER_READER1 || pPager->tempFile ){
59485 pPg = sqlite3PagerLookup(pPager, pgno);
59486 }
59487 if( pPg==0 ){
59488 rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg);
59489 }else{
59490 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
59491 }
59492 if( pPg ){
59493 assert( rc==SQLITE_OK )((void) (0));
59494 *ppPage = pPg;
59495 return SQLITE_OK0;
59496 }
59497 }
59498 if( rc!=SQLITE_OK0 ){
59499 *ppPage = 0;
59500 return rc;
59501 }
59502 }
59503 return getPageNormal(pPager, pgno, ppPage, flags);
59504}
59505#endif /* SQLITE_MAX_MMAP_SIZE>0 */
59506
59507/* The page getter method for when the pager is an error state */
59508static int getPageError(
59509 Pager *pPager, /* The pager open on the database file */
59510 Pgno pgno, /* Page number to fetch */
59511 DbPage **ppPage, /* Write a pointer to the page here */
59512 int flags /* PAGER_GET_XXX flags */
59513){
59514 UNUSED_PARAMETER(pgno)(void)(pgno);
59515 UNUSED_PARAMETER(flags)(void)(flags);
59516 assert( pPager->errCode!=SQLITE_OK )((void) (0));
59517 *ppPage = 0;
59518 return pPager->errCode;
59519}
59520
59521
59522/* Dispatch all page fetch requests to the appropriate getter method.
59523*/
59524SQLITE_PRIVATEstatic int sqlite3PagerGet(
59525 Pager *pPager, /* The pager open on the database file */
59526 Pgno pgno, /* Page number to fetch */
59527 DbPage **ppPage, /* Write a pointer to the page here */
59528 int flags /* PAGER_GET_XXX flags */
59529){
59530 /* printf("PAGE %u\n", pgno); fflush(stdout); */
59531 return pPager->xGet(pPager, pgno, ppPage, flags);
59532}
59533
59534/*
59535** Acquire a page if it is already in the in-memory cache. Do
59536** not read the page from disk. Return a pointer to the page,
59537** or 0 if the page is not in cache.
59538**
59539** See also sqlite3PagerGet(). The difference between this routine
59540** and sqlite3PagerGet() is that _get() will go to the disk and read
59541** in the page if the page is not already in cache. This routine
59542** returns NULL if the page is not in cache or if a disk I/O error
59543** has ever happened.
59544*/
59545SQLITE_PRIVATEstatic DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
59546 sqlite3_pcache_page *pPage;
59547 assert( pPager!=0 )((void) (0));
59548 assert( pgno!=0 )((void) (0));
59549 assert( pPager->pPCache!=0 )((void) (0));
59550 pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
59551 assert( pPage==0 || pPager->hasHeldSharedLock )((void) (0));
59552 if( pPage==0 ) return 0;
59553 return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
59554}
59555
59556/*
59557** Release a page reference.
59558**
59559** The sqlite3PagerUnref() and sqlite3PagerUnrefNotNull() may only be
59560** used if we know that the page being released is not the last page.
59561** The btree layer always holds page1 open until the end, so these first
59562** to routines can be used to release any page other than BtShared.pPage1.
59563**
59564** Use sqlite3PagerUnrefPageOne() to release page1. This latter routine
59565** checks the total number of outstanding pages and if the number of
59566** pages reaches zero it drops the database lock.
59567*/
59568SQLITE_PRIVATEstatic void sqlite3PagerUnrefNotNull(DbPage *pPg){
59569 TESTONLY( Pager *pPager = pPg->pPager; )
59570 assert( pPg!=0 )((void) (0));
59571 if( pPg->flags & PGHDR_MMAP0x020 ){
59572 assert( pPg->pgno!=1 )((void) (0)); /* Page1 is never memory mapped */
59573 pagerReleaseMapPage(pPg);
59574 }else{
59575 sqlite3PcacheRelease(pPg);
59576 }
59577 /* Do not use this routine to release the last reference to page1 */
59578 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 )((void) (0));
59579}
59580SQLITE_PRIVATEstatic void sqlite3PagerUnref(DbPage *pPg){
59581 if( pPg ) sqlite3PagerUnrefNotNull(pPg);
59582}
59583SQLITE_PRIVATEstatic void sqlite3PagerUnrefPageOne(DbPage *pPg){
59584 Pager *pPager;
59585 assert( pPg!=0 )((void) (0));
59586 assert( pPg->pgno==1 )((void) (0));
59587 assert( (pPg->flags & PGHDR_MMAP)==0 )((void) (0)); /* Page1 is never memory mapped */
59588 pPager = pPg->pPager;
59589 sqlite3PcacheRelease(pPg);
59590 pagerUnlockIfUnused(pPager);
59591}
59592
59593/*
59594** This function is called at the start of every write transaction.
59595** There must already be a RESERVED or EXCLUSIVE lock on the database
59596** file when this routine is called.
59597**
59598** Open the journal file for pager pPager and write a journal header
59599** to the start of it. If there are active savepoints, open the sub-journal
59600** as well. This function is only used when the journal file is being
59601** opened to write a rollback log for a transaction. It is not used
59602** when opening a hot journal file to roll it back.
59603**
59604** If the journal file is already open (as it may be in exclusive mode),
59605** then this function just writes a journal header to the start of the
59606** already open file.
59607**
59608** Whether or not the journal file is opened by this function, the
59609** Pager.pInJournal bitvec structure is allocated.
59610**
59611** Return SQLITE_OK if everything is successful. Otherwise, return
59612** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or
59613** an IO error code if opening or writing the journal file fails.
59614*/
59615static int pager_open_journal(Pager *pPager){
59616 int rc = SQLITE_OK0; /* Return code */
59617 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
59618
59619 assert( pPager->eState==PAGER_WRITER_LOCKED )((void) (0));
59620 assert( assert_pager_state(pPager) )((void) (0));
59621 assert( pPager->pInJournal==0 )((void) (0));
59622
59623 /* If already in the error state, this function is a no-op. But on
59624 ** the other hand, this routine is never called if we are already in
59625 ** an error state. */
59626 if( NEVER(pPager->errCode)(pPager->errCode) ) return pPager->errCode;
59627
59628 if( !pagerUseWal(pPager)((pPager)->pWal!=0) && pPager->journalMode!=PAGER_JOURNALMODE_OFF2 ){
59629 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
59630 if( pPager->pInJournal==0 ){
59631 return SQLITE_NOMEM_BKPT7;
59632 }
59633
59634 /* Open the journal file if it is not already open. */
59635 if( !isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) ){
59636 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY4 ){
59637 sqlite3MemJournalOpen(pPager->jfd);
59638 }else{
59639 int flags = SQLITE_OPEN_READWRITE0x00000002|SQLITE_OPEN_CREATE0x00000004;
59640 int nSpill;
59641
59642 if( pPager->tempFile ){
59643 flags |= (SQLITE_OPEN_DELETEONCLOSE0x00000008|SQLITE_OPEN_TEMP_JOURNAL0x00001000);
59644 nSpill = sqlite3Config.nStmtSpill;
59645 }else{
59646 flags |= SQLITE_OPEN_MAIN_JOURNAL0x00000800;
59647 nSpill = jrnlBufferSize(pPager);
59648 }
59649
59650 /* Verify that the database still has the same name as it did when
59651 ** it was originally opened. */
59652 rc = databaseIsUnmoved(pPager);
59653 if( rc==SQLITE_OK0 ){
59654 rc = sqlite3JournalOpen (
59655 pVfs, pPager->zJournal, pPager->jfd, flags, nSpill
59656 );
59657 }
59658 }
59659 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) )((void) (0));
59660 }
59661
59662
59663 /* Write the first journal header to the journal file and open
59664 ** the sub-journal if necessary.
59665 */
59666 if( rc==SQLITE_OK0 ){
59667 /* TODO: Check if all of these are really required. */
59668 pPager->nRec = 0;
59669 pPager->journalOff = 0;
59670 pPager->setSuper = 0;
59671 pPager->journalHdr = 0;
59672 rc = writeJournalHdr(pPager);
59673 }
59674 }
59675
59676 if( rc!=SQLITE_OK0 ){
59677 sqlite3BitvecDestroy(pPager->pInJournal);
59678 pPager->pInJournal = 0;
59679 }else{
59680 assert( pPager->eState==PAGER_WRITER_LOCKED )((void) (0));
59681 pPager->eState = PAGER_WRITER_CACHEMOD3;
59682 }
59683
59684 return rc;
59685}
59686
59687/*
59688** Begin a write-transaction on the specified pager object. If a
59689** write-transaction has already been opened, this function is a no-op.
59690**
59691** If the exFlag argument is false, then acquire at least a RESERVED
59692** lock on the database file. If exFlag is true, then acquire at least
59693** an EXCLUSIVE lock. If such a lock is already held, no locking
59694** functions need be called.
59695**
59696** If the subjInMemory argument is non-zero, then any sub-journal opened
59697** within this transaction will be opened as an in-memory file. This
59698** has no effect if the sub-journal is already opened (as it may be when
59699** running in exclusive mode) or if the transaction does not require a
59700** sub-journal. If the subjInMemory argument is zero, then any required
59701** sub-journal is implemented in-memory if pPager is an in-memory database,
59702** or using a temporary file otherwise.
59703*/
59704SQLITE_PRIVATEstatic int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
59705 int rc = SQLITE_OK0;
59706
59707 if( pPager->errCode ) return pPager->errCode;
59708 assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR )((void) (0));
59709 pPager->subjInMemory = (u8)subjInMemory;
59710
59711 if( pPager->eState==PAGER_READER1 ){
59712 assert( pPager->pInJournal==0 )((void) (0));
59713
59714 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
59715 /* If the pager is configured to use locking_mode=exclusive, and an
59716 ** exclusive lock on the database is not already held, obtain it now.
59717 */
59718 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
59719 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK4);
59720 if( rc!=SQLITE_OK0 ){
59721 return rc;
59722 }
59723 (void)sqlite3WalExclusiveMode(pPager->pWal, 1);
59724 }
59725
59726 /* Grab the write lock on the log file. If successful, upgrade to
59727 ** PAGER_RESERVED state. Otherwise, return an error code to the caller.
59728 ** The busy-handler is not invoked if another connection already
59729 ** holds the write-lock. If possible, the upper layer will call it.
59730 */
59731 rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
59732 }else{
59733 /* Obtain a RESERVED lock on the database file. If the exFlag parameter
59734 ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
59735 ** busy-handler callback can be used when upgrading to the EXCLUSIVE
59736 ** lock, but not when obtaining the RESERVED lock.
59737 */
59738 rc = pagerLockDb(pPager, RESERVED_LOCK2);
59739 if( rc==SQLITE_OK0 && exFlag ){
59740 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK4);
59741 }
59742 }
59743
59744 if( rc==SQLITE_OK0 ){
59745 /* Change to WRITER_LOCKED state.
59746 **
59747 ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD
59748 ** when it has an open transaction, but never to DBMOD or FINISHED.
59749 ** This is because in those states the code to roll back savepoint
59750 ** transactions may copy data from the sub-journal into the database
59751 ** file as well as into the page cache. Which would be incorrect in
59752 ** WAL mode.
59753 */
59754 pPager->eState = PAGER_WRITER_LOCKED2;
59755 pPager->dbHintSize = pPager->dbSize;
59756 pPager->dbFileSize = pPager->dbSize;
59757 pPager->dbOrigSize = pPager->dbSize;
59758 pPager->journalOff = 0;
59759 }
59760
59761 assert( rc==SQLITE_OK || pPager->eState==PAGER_READER )((void) (0));
59762 assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED )((void) (0));
59763 assert( assert_pager_state(pPager) )((void) (0));
59764 }
59765
59766 PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
59767 return rc;
59768}
59769
59770/*
59771** Write page pPg onto the end of the rollback journal.
59772*/
59773static SQLITE_NOINLINE__attribute__((noinline)) int pagerAddPageToRollbackJournal(PgHdr *pPg){
59774 Pager *pPager = pPg->pPager;
59775 int rc;
59776 u32 cksum;
59777 char *pData2;
59778 i64 iOff = pPager->journalOff;
59779
59780 /* We should never write to the journal file the page that
59781 ** contains the database locks. The following assert verifies
59782 ** that we do not. */
59783 assert( pPg->pgno!=PAGER_SJ_PGNO(pPager) )((void) (0));
59784
59785 assert( pPager->journalHdr<=pPager->journalOff )((void) (0));
59786 pData2 = pPg->pData;
59787 cksum = pager_cksum(pPager, (u8*)pData2);
59788
59789 /* Even if an IO or diskfull error occurs while journalling the
59790 ** page in the block above, set the need-sync flag for the page.
59791 ** Otherwise, when the transaction is rolled back, the logic in
59792 ** playback_one_page() will think that the page needs to be restored
59793 ** in the database file. And if an IO error occurs while doing so,
59794 ** then corruption may follow.
59795 */
59796 pPg->flags |= PGHDR_NEED_SYNC0x008;
59797
59798 rc = write32bits(pPager->jfd, iOff, pPg->pgno);
59799 if( rc!=SQLITE_OK0 ) return rc;
59800 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
59801 if( rc!=SQLITE_OK0 ) return rc;
59802 rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
59803 if( rc!=SQLITE_OK0 ) return rc;
59804
59805 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
59806 pPager->journalOff, pPager->pageSize));
59807 PAGER_INCR(sqlite3_pager_writej_count);
59808 PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
59809 PAGERID(pPager), pPg->pgno,
59810 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
59811
59812 pPager->journalOff += 8 + pPager->pageSize;
59813 pPager->nRec++;
59814 assert( pPager->pInJournal!=0 )((void) (0));
59815 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
59816 testcase( rc==SQLITE_NOMEM );
59817 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM )((void) (0));
59818 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
59819 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM )((void) (0));
59820 return rc;
59821}
59822
59823/*
59824** Mark a single data page as writeable. The page is written into the
59825** main journal or sub-journal as required. If the page is written into
59826** one of the journals, the corresponding bit is set in the
59827** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs
59828** of any open savepoints as appropriate.
59829*/
59830static int pager_write(PgHdr *pPg){
59831 Pager *pPager = pPg->pPager;
59832 int rc = SQLITE_OK0;
59833
59834 /* This routine is not called unless a write-transaction has already
59835 ** been started. The journal file may or may not be open at this point.
59836 ** It is never called in the ERROR state.
59837 */
59838 assert( pPager->eState==PAGER_WRITER_LOCKED((void) (0))
59839 || pPager->eState==PAGER_WRITER_CACHEMOD((void) (0))
59840 || pPager->eState==PAGER_WRITER_DBMOD((void) (0))
59841 )((void) (0));
59842 assert( assert_pager_state(pPager) )((void) (0));
59843 assert( pPager->errCode==0 )((void) (0));
59844 assert( pPager->readOnly==0 )((void) (0));
59845 CHECK_PAGE(pPg);
59846
59847 /* The journal file needs to be opened. Higher level routines have already
59848 ** obtained the necessary locks to begin the write-transaction, but the
59849 ** rollback journal might not yet be open. Open it now if this is the case.
59850 **
59851 ** This is done before calling sqlite3PcacheMakeDirty() on the page.
59852 ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then
59853 ** an error might occur and the pager would end up in WRITER_LOCKED state
59854 ** with pages marked as dirty in the cache.
59855 */
59856 if( pPager->eState==PAGER_WRITER_LOCKED2 ){
59857 rc = pager_open_journal(pPager);
59858 if( rc!=SQLITE_OK0 ) return rc;
59859 }
59860 assert( pPager->eState>=PAGER_WRITER_CACHEMOD )((void) (0));
59861 assert( assert_pager_state(pPager) )((void) (0));
59862
59863 /* Mark the page that is about to be modified as dirty. */
59864 sqlite3PcacheMakeDirty(pPg);
59865
59866 /* If a rollback journal is in use, them make sure the page that is about
59867 ** to change is in the rollback journal, or if the page is a new page off
59868 ** then end of the file, make sure it is marked as PGHDR_NEED_SYNC.
59869 */
59870 assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) )((void) (0));
59871 if( pPager->pInJournal!=0
59872 && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0
59873 ){
59874 assert( pagerUseWal(pPager)==0 )((void) (0));
59875 if( pPg->pgno<=pPager->dbOrigSize ){
59876 rc = pagerAddPageToRollbackJournal(pPg);
59877 if( rc!=SQLITE_OK0 ){
59878 return rc;
59879 }
59880 }else{
59881 if( pPager->eState!=PAGER_WRITER_DBMOD4 ){
59882 pPg->flags |= PGHDR_NEED_SYNC0x008;
59883 }
59884 PAGERTRACE(("APPEND %d page %d needSync=%d\n",
59885 PAGERID(pPager), pPg->pgno,
59886 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
59887 }
59888 }
59889
59890 /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
59891 ** and before writing the page into the rollback journal. Wait until now,
59892 ** after the page has been successfully journalled, before setting the
59893 ** PGHDR_WRITEABLE bit that indicates that the page can be safely modified.
59894 */
59895 pPg->flags |= PGHDR_WRITEABLE0x004;
59896
59897 /* If the statement journal is open and the page is not in it,
59898 ** then write the page into the statement journal.
59899 */
59900 if( pPager->nSavepoint>0 ){
59901 rc = subjournalPageIfRequired(pPg);
59902 }
59903
59904 /* Update the database size and return. */
59905 if( pPager->dbSize<pPg->pgno ){
59906 pPager->dbSize = pPg->pgno;
59907 }
59908 return rc;
59909}
59910
59911/*
59912** This is a variant of sqlite3PagerWrite() that runs when the sector size
59913** is larger than the page size. SQLite makes the (reasonable) assumption that
59914** all bytes of a sector are written together by hardware. Hence, all bytes of
59915** a sector need to be journalled in case of a power loss in the middle of
59916** a write.
59917**
59918** Usually, the sector size is less than or equal to the page size, in which
59919** case pages can be individually written. This routine only runs in the
59920** exceptional case where the page size is smaller than the sector size.
59921*/
59922static SQLITE_NOINLINE__attribute__((noinline)) int pagerWriteLargeSector(PgHdr *pPg){
59923 int rc = SQLITE_OK0; /* Return code */
59924 Pgno nPageCount; /* Total number of pages in database file */
59925 Pgno pg1; /* First page of the sector pPg is located on. */
59926 int nPage = 0; /* Number of pages starting at pg1 to journal */
59927 int ii; /* Loop counter */
59928 int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */
59929 Pager *pPager = pPg->pPager; /* The pager that owns pPg */
59930 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
59931
59932 /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
59933 ** a journal header to be written between the pages journaled by
59934 ** this function.
59935 */
59936 assert( !MEMDB )((void) (0));
59937 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 )((void) (0));
59938 pPager->doNotSpill |= SPILLFLAG_NOSYNC0x04;
59939
59940 /* This trick assumes that both the page-size and sector-size are
59941 ** an integer power of 2. It sets variable pg1 to the identifier
59942 ** of the first page of the sector pPg is located on.
59943 */
59944 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
59945
59946 nPageCount = pPager->dbSize;
59947 if( pPg->pgno>nPageCount ){
59948 nPage = (pPg->pgno - pg1)+1;
59949 }else if( (pg1+nPagePerSector-1)>nPageCount ){
59950 nPage = nPageCount+1-pg1;
59951 }else{
59952 nPage = nPagePerSector;
59953 }
59954 assert(nPage>0)((void) (0));
59955 assert(pg1<=pPg->pgno)((void) (0));
59956 assert((pg1+nPage)>pPg->pgno)((void) (0));
59957
59958 for(ii=0; ii<nPage && rc==SQLITE_OK0; ii++){
59959 Pgno pg = pg1+ii;
59960 PgHdr *pPage;
59961 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
59962 if( pg!=PAGER_SJ_PGNO(pPager)((pPager)->lckPgno) ){
59963 rc = sqlite3PagerGet(pPager, pg, &pPage, 0);
59964 if( rc==SQLITE_OK0 ){
59965 rc = pager_write(pPage);
59966 if( pPage->flags&PGHDR_NEED_SYNC0x008 ){
59967 needSync = 1;
59968 }
59969 sqlite3PagerUnrefNotNull(pPage);
59970 }
59971 }
59972 }else if( (pPage = sqlite3PagerLookup(pPager, pg))!=0 ){
59973 if( pPage->flags&PGHDR_NEED_SYNC0x008 ){
59974 needSync = 1;
59975 }
59976 sqlite3PagerUnrefNotNull(pPage);
59977 }
59978 }
59979
59980 /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages
59981 ** starting at pg1, then it needs to be set for all of them. Because
59982 ** writing to any of these nPage pages may damage the others, the
59983 ** journal file must contain sync()ed copies of all of them
59984 ** before any of them can be written out to the database file.
59985 */
59986 if( rc==SQLITE_OK0 && needSync ){
59987 assert( !MEMDB )((void) (0));
59988 for(ii=0; ii<nPage; ii++){
59989 PgHdr *pPage = sqlite3PagerLookup(pPager, pg1+ii);
59990 if( pPage ){
59991 pPage->flags |= PGHDR_NEED_SYNC0x008;
59992 sqlite3PagerUnrefNotNull(pPage);
59993 }
59994 }
59995 }
59996
59997 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 )((void) (0));
59998 pPager->doNotSpill &= ~SPILLFLAG_NOSYNC0x04;
59999 return rc;
60000}
60001
60002/*
60003** Mark a data page as writeable. This routine must be called before
60004** making changes to a page. The caller must check the return value
60005** of this function and be careful not to change any page data unless
60006** this routine returns SQLITE_OK.
60007**
60008** The difference between this function and pager_write() is that this
60009** function also deals with the special case where 2 or more pages
60010** fit on a single disk sector. In this case all co-resident pages
60011** must have been written to the journal file before returning.
60012**
60013** If an error occurs, SQLITE_NOMEM or an IO error code is returned
60014** as appropriate. Otherwise, SQLITE_OK.
60015*/
60016SQLITE_PRIVATEstatic int sqlite3PagerWrite(PgHdr *pPg){
60017 Pager *pPager = pPg->pPager;
60018 assert( (pPg->flags & PGHDR_MMAP)==0 )((void) (0));
60019 assert( pPager->eState>=PAGER_WRITER_LOCKED )((void) (0));
60020 assert( assert_pager_state(pPager) )((void) (0));
60021 if( (pPg->flags & PGHDR_WRITEABLE0x004)!=0 && pPager->dbSize>=pPg->pgno ){
60022 if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
60023 return SQLITE_OK0;
60024 }else if( pPager->errCode ){
60025 return pPager->errCode;
60026 }else if( pPager->sectorSize > (u32)pPager->pageSize ){
60027 assert( pPager->tempFile==0 )((void) (0));
60028 return pagerWriteLargeSector(pPg);
60029 }else{
60030 return pager_write(pPg);
60031 }
60032}
60033
60034/*
60035** Return TRUE if the page given in the argument was previously passed
60036** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
60037** to change the content of the page.
60038*/
60039#ifndef NDEBUG1
60040SQLITE_PRIVATEstatic int sqlite3PagerIswriteable(DbPage *pPg){
60041 return pPg->flags & PGHDR_WRITEABLE0x004;
60042}
60043#endif
60044
60045/*
60046** A call to this routine tells the pager that it is not necessary to
60047** write the information on page pPg back to the disk, even though
60048** that page might be marked as dirty. This happens, for example, when
60049** the page has been added as a leaf of the freelist and so its
60050** content no longer matters.
60051**
60052** The overlying software layer calls this routine when all of the data
60053** on the given page is unused. The pager marks the page as clean so
60054** that it does not get written to disk.
60055**
60056** Tests show that this optimization can quadruple the speed of large
60057** DELETE operations.
60058**
60059** This optimization cannot be used with a temp-file, as the page may
60060** have been dirty at the start of the transaction. In that case, if
60061** memory pressure forces page pPg out of the cache, the data does need
60062** to be written out to disk so that it may be read back in if the
60063** current transaction is rolled back.
60064*/
60065SQLITE_PRIVATEstatic void sqlite3PagerDontWrite(PgHdr *pPg){
60066 Pager *pPager = pPg->pPager;
60067 if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY0x002) && pPager->nSavepoint==0 ){
60068 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
60069 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
60070 pPg->flags |= PGHDR_DONT_WRITE0x010;
60071 pPg->flags &= ~PGHDR_WRITEABLE0x004;
60072 testcase( pPg->flags & PGHDR_NEED_SYNC );
60073 pager_set_pagehash(pPg);
60074 }
60075}
60076
60077/*
60078** This routine is called to increment the value of the database file
60079** change-counter, stored as a 4-byte big-endian integer starting at
60080** byte offset 24 of the pager file. The secondary change counter at
60081** 92 is also updated, as is the SQLite version number at offset 96.
60082**
60083** But this only happens if the pPager->changeCountDone flag is false.
60084** To avoid excess churning of page 1, the update only happens once.
60085** See also the pager_write_changecounter() routine that does an
60086** unconditional update of the change counters.
60087**
60088** If the isDirectMode flag is zero, then this is done by calling
60089** sqlite3PagerWrite() on page 1, then modifying the contents of the
60090** page data. In this case the file will be updated when the current
60091** transaction is committed.
60092**
60093** The isDirectMode flag may only be non-zero if the library was compiled
60094** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case,
60095** if isDirect is non-zero, then the database file is updated directly
60096** by writing an updated version of page 1 using a call to the
60097** sqlite3OsWrite() function.
60098*/
60099static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
60100 int rc = SQLITE_OK0;
60101
60102 assert( pPager->eState==PAGER_WRITER_CACHEMOD((void) (0))
60103 || pPager->eState==PAGER_WRITER_DBMOD((void) (0))
60104 )((void) (0));
60105 assert( assert_pager_state(pPager) )((void) (0));
60106
60107 /* Declare and initialize constant integer 'isDirect'. If the
60108 ** atomic-write optimization is enabled in this build, then isDirect
60109 ** is initialized to the value passed as the isDirectMode parameter
60110 ** to this function. Otherwise, it is always set to zero.
60111 **
60112 ** The idea is that if the atomic-write optimization is not
60113 ** enabled at compile time, the compiler can omit the tests of
60114 ** 'isDirect' below, as well as the block enclosed in the
60115 ** "if( isDirect )" condition.
60116 */
60117#ifndef SQLITE_ENABLE_ATOMIC_WRITE
60118# define DIRECT_MODE0 0
60119 assert( isDirectMode==0 )((void) (0));
60120 UNUSED_PARAMETER(isDirectMode)(void)(isDirectMode);
60121#else
60122# define DIRECT_MODE0 isDirectMode
60123#endif
60124
60125 if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0)(pPager->dbSize>0) ){
60126 PgHdr *pPgHdr; /* Reference to page 1 */
60127
60128 assert( !pPager->tempFile && isOpen(pPager->fd) )((void) (0));
60129
60130 /* Open page 1 of the file for writing. */
60131 rc = sqlite3PagerGet(pPager, 1, &pPgHdr, 0);
60132 assert( pPgHdr==0 || rc==SQLITE_OK )((void) (0));
60133
60134 /* If page one was fetched successfully, and this function is not
60135 ** operating in direct-mode, make page 1 writable. When not in
60136 ** direct mode, page 1 is always held in cache and hence the PagerGet()
60137 ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
60138 */
60139 if( !DIRECT_MODE0 && ALWAYS(rc==SQLITE_OK)(rc==0) ){
60140 rc = sqlite3PagerWrite(pPgHdr);
60141 }
60142
60143 if( rc==SQLITE_OK0 ){
60144 /* Actually do the update of the change counter */
60145 pager_write_changecounter(pPgHdr);
60146
60147 /* If running in direct mode, write the contents of page 1 to the file. */
60148 if( DIRECT_MODE0 ){
60149 const void *zBuf;
60150 assert( pPager->dbFileSize>0 )((void) (0));
60151 zBuf = pPgHdr->pData;
60152 if( rc==SQLITE_OK0 ){
60153 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
60154 pPager->aStat[PAGER_STAT_WRITE2]++;
60155 }
60156 if( rc==SQLITE_OK0 ){
60157 /* Update the pager's copy of the change-counter. Otherwise, the
60158 ** next time a read transaction is opened the cache will be
60159 ** flushed (as the change-counter values will not match). */
60160 const void *pCopy = (const void *)&((const char *)zBuf)[24];
60161 memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers));
60162 pPager->changeCountDone = 1;
60163 }
60164 }else{
60165 pPager->changeCountDone = 1;
60166 }
60167 }
60168
60169 /* Release the page reference. */
60170 sqlite3PagerUnref(pPgHdr);
60171 }
60172 return rc;
60173}
60174
60175/*
60176** Sync the database file to disk. This is a no-op for in-memory databases
60177** or pages with the Pager.noSync flag set.
60178**
60179** If successful, or if called on a pager for which it is a no-op, this
60180** function returns SQLITE_OK. Otherwise, an IO error code is returned.
60181*/
60182SQLITE_PRIVATEstatic int sqlite3PagerSync(Pager *pPager, const char *zSuper){
60183 int rc = SQLITE_OK0;
60184 void *pArg = (void*)zSuper;
60185 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC21, pArg);
60186 if( rc==SQLITE_NOTFOUND12 ) rc = SQLITE_OK0;
60187 if( rc==SQLITE_OK0 && !pPager->noSync ){
60188 assert( !MEMDB )((void) (0));
60189 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
60190 }
60191 return rc;
60192}
60193
60194/*
60195** This function may only be called while a write-transaction is active in
60196** rollback. If the connection is in WAL mode, this call is a no-op.
60197** Otherwise, if the connection does not already have an EXCLUSIVE lock on
60198** the database file, an attempt is made to obtain one.
60199**
60200** If the EXCLUSIVE lock is already held or the attempt to obtain it is
60201** successful, or the connection is in WAL mode, SQLITE_OK is returned.
60202** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is
60203** returned.
60204*/
60205SQLITE_PRIVATEstatic int sqlite3PagerExclusiveLock(Pager *pPager){
60206 int rc = pPager->errCode;
60207 assert( assert_pager_state(pPager) )((void) (0));
60208 if( rc==SQLITE_OK0 ){
60209 assert( pPager->eState==PAGER_WRITER_CACHEMOD((void) (0))
60210 || pPager->eState==PAGER_WRITER_DBMOD((void) (0))
60211 || pPager->eState==PAGER_WRITER_LOCKED((void) (0))
60212 )((void) (0));
60213 assert( assert_pager_state(pPager) )((void) (0));
60214 if( 0==pagerUseWal(pPager)((pPager)->pWal!=0) ){
60215 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK4);
60216 }
60217 }
60218 return rc;
60219}
60220
60221/*
60222** Sync the database file for the pager pPager. zSuper points to the name
60223** of a super-journal file that should be written into the individual
60224** journal file. zSuper may be NULL, which is interpreted as no
60225** super-journal (a single database transaction).
60226**
60227** This routine ensures that:
60228**
60229** * The database file change-counter is updated,
60230** * the journal is synced (unless the atomic-write optimization is used),
60231** * all dirty pages are written to the database file,
60232** * the database file is truncated (if required), and
60233** * the database file synced.
60234**
60235** The only thing that remains to commit the transaction is to finalize
60236** (delete, truncate or zero the first part of) the journal file (or
60237** delete the super-journal file if specified).
60238**
60239** Note that if zSuper==NULL, this does not overwrite a previous value
60240** passed to an sqlite3PagerCommitPhaseOne() call.
60241**
60242** If the final parameter - noSync - is true, then the database file itself
60243** is not synced. The caller must call sqlite3PagerSync() directly to
60244** sync the database file before calling CommitPhaseTwo() to delete the
60245** journal file in this case.
60246*/
60247SQLITE_PRIVATEstatic int sqlite3PagerCommitPhaseOne(
60248 Pager *pPager, /* Pager object */
60249 const char *zSuper, /* If not NULL, the super-journal name */
60250 int noSync /* True to omit the xSync on the db file */
60251){
60252 int rc = SQLITE_OK0; /* Return code */
60253
60254 assert( pPager->eState==PAGER_WRITER_LOCKED((void) (0))
60255 || pPager->eState==PAGER_WRITER_CACHEMOD((void) (0))
60256 || pPager->eState==PAGER_WRITER_DBMOD((void) (0))
60257 || pPager->eState==PAGER_ERROR((void) (0))
60258 )((void) (0));
60259 assert( assert_pager_state(pPager) )((void) (0));
60260
60261 /* If a prior error occurred, report that error again. */
60262 if( NEVER(pPager->errCode)(pPager->errCode) ) return pPager->errCode;
60263
60264 /* Provide the ability to easily simulate an I/O error during testing */
60265 if( sqlite3FaultSim(400) ) return SQLITE_IOERR10;
60266
60267 PAGERTRACE(("DATABASE SYNC: File=%s zSuper=%s nSize=%d\n",
60268 pPager->zFilename, zSuper, pPager->dbSize));
60269
60270 /* If no database changes have been made, return early. */
60271 if( pPager->eState<PAGER_WRITER_CACHEMOD3 ) return SQLITE_OK0;
60272
60273 assert( MEMDB==0 || pPager->tempFile )((void) (0));
60274 assert( isOpen(pPager->fd) || pPager->tempFile )((void) (0));
60275 if( 0==pagerFlushOnCommit(pPager, 1) ){
60276 /* If this is an in-memory db, or no pages have been written to, or this
60277 ** function has already been called, it is mostly a no-op. However, any
60278 ** backup in progress needs to be restarted. */
60279 sqlite3BackupRestart(pPager->pBackup);
60280 }else{
60281 PgHdr *pList;
60282 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
60283 PgHdr *pPageOne = 0;
60284 pList = sqlite3PcacheDirtyList(pPager->pPCache);
60285 if( pList==0 ){
60286 /* Must have at least one page for the WAL commit flag.
60287 ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
60288 rc = sqlite3PagerGet(pPager, 1, &pPageOne, 0);
60289 pList = pPageOne;
60290 pList->pDirty = 0;
60291 }
60292 assert( rc==SQLITE_OK )((void) (0));
60293 if( ALWAYS(pList)(pList) ){
60294 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
60295 }
60296 sqlite3PagerUnref(pPageOne);
60297 if( rc==SQLITE_OK0 ){
60298 sqlite3PcacheCleanAll(pPager->pPCache);
60299 }
60300 }else{
60301 /* The bBatch boolean is true if the batch-atomic-write commit method
60302 ** should be used. No rollback journal is created if batch-atomic-write
60303 ** is enabled.
60304 */
60305#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
60306 sqlite3_file *fd = pPager->fd;
60307 int bBatch0 = zSuper==0 /* An SQLITE_IOCAP_BATCH_ATOMIC commit */
60308 && (sqlite3OsDeviceCharacteristics(fd) & SQLITE_IOCAP_BATCH_ATOMIC0x00004000)
60309 && !pPager->noSync
60310 && sqlite3JournalIsInMemory(pPager->jfd);
60311#else
60312# define bBatch0 0
60313#endif
60314
60315#ifdef SQLITE_ENABLE_ATOMIC_WRITE
60316 /* The following block updates the change-counter. Exactly how it
60317 ** does this depends on whether or not the atomic-update optimization
60318 ** was enabled at compile time, and if this transaction meets the
60319 ** runtime criteria to use the operation:
60320 **
60321 ** * The file-system supports the atomic-write property for
60322 ** blocks of size page-size, and
60323 ** * This commit is not part of a multi-file transaction, and
60324 ** * Exactly one page has been modified and store in the journal file.
60325 **
60326 ** If the optimization was not enabled at compile time, then the
60327 ** pager_incr_changecounter() function is called to update the change
60328 ** counter in 'indirect-mode'. If the optimization is compiled in but
60329 ** is not applicable to this transaction, call sqlite3JournalCreate()
60330 ** to make sure the journal file has actually been created, then call
60331 ** pager_incr_changecounter() to update the change-counter in indirect
60332 ** mode.
60333 **
60334 ** Otherwise, if the optimization is both enabled and applicable,
60335 ** then call pager_incr_changecounter() to update the change-counter
60336 ** in 'direct' mode. In this case the journal file will never be
60337 ** created for this transaction.
60338 */
60339 if( bBatch0==0 ){
60340 PgHdr *pPg;
60341 assert( isOpen(pPager->jfd)((void) (0))
60342 || pPager->journalMode==PAGER_JOURNALMODE_OFF((void) (0))
60343 || pPager->journalMode==PAGER_JOURNALMODE_WAL((void) (0))
60344 )((void) (0));
60345 if( !zSuper && isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0)
60346 && pPager->journalOff==jrnlBufferSize(pPager)
60347 && pPager->dbSize>=pPager->dbOrigSize
60348 && (!(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
60349 ){
60350 /* Update the db file change counter via the direct-write method. The
60351 ** following call will modify the in-memory representation of page 1
60352 ** to include the updated change counter and then write page 1
60353 ** directly to the database file. Because of the atomic-write
60354 ** property of the host file-system, this is safe.
60355 */
60356 rc = pager_incr_changecounter(pPager, 1);
60357 }else{
60358 rc = sqlite3JournalCreate(pPager->jfd);
60359 if( rc==SQLITE_OK0 ){
60360 rc = pager_incr_changecounter(pPager, 0);
60361 }
60362 }
60363 }
60364#else /* SQLITE_ENABLE_ATOMIC_WRITE */
60365#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
60366 if( zSuper ){
60367 rc = sqlite3JournalCreate(pPager->jfd);
60368 if( rc!=SQLITE_OK0 ) goto commit_phase_one_exit;
60369 assert( bBatch==0 )((void) (0));
60370 }
60371#endif
60372 rc = pager_incr_changecounter(pPager, 0);
60373#endif /* !SQLITE_ENABLE_ATOMIC_WRITE */
60374 if( rc!=SQLITE_OK0 ) goto commit_phase_one_exit;
60375
60376 /* Write the super-journal name into the journal file. If a
60377 ** super-journal file name has already been written to the journal file,
60378 ** or if zSuper is NULL (no super-journal), then this call is a no-op.
60379 */
60380 rc = writeSuperJournal(pPager, zSuper);
60381 if( rc!=SQLITE_OK0 ) goto commit_phase_one_exit;
60382
60383 /* Sync the journal file and write all dirty pages to the database.
60384 ** If the atomic-update optimization is being used, this sync will not
60385 ** create the journal file or perform any real IO.
60386 **
60387 ** Because the change-counter page was just modified, unless the
60388 ** atomic-update optimization is used it is almost certain that the
60389 ** journal requires a sync here. However, in locking_mode=exclusive
60390 ** on a system under memory pressure it is just possible that this is
60391 ** not the case. In this case it is likely enough that the redundant
60392 ** xSync() call will be changed to a no-op by the OS anyhow.
60393 */
60394 rc = syncJournal(pPager, 0);
60395 if( rc!=SQLITE_OK0 ) goto commit_phase_one_exit;
60396
60397 pList = sqlite3PcacheDirtyList(pPager->pPCache);
60398#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
60399 if( bBatch0 ){
60400 rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_BEGIN_ATOMIC_WRITE31, 0);
60401 if( rc==SQLITE_OK0 ){
60402 rc = pager_write_pagelist(pPager, pList);
60403 if( rc==SQLITE_OK0 ){
60404 rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_COMMIT_ATOMIC_WRITE32, 0);
60405 }
60406 if( rc!=SQLITE_OK0 ){
60407 sqlite3OsFileControlHint(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE33, 0);
60408 }
60409 }
60410
60411 if( (rc&0xFF)==SQLITE_IOERR10 && rc!=SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
60412 rc = sqlite3JournalCreate(pPager->jfd);
60413 if( rc!=SQLITE_OK0 ){
60414 sqlite3OsClose(pPager->jfd);
60415 goto commit_phase_one_exit;
60416 }
60417 bBatch0 = 0;
60418 }else{
60419 sqlite3OsClose(pPager->jfd);
60420 }
60421 }
60422#endif /* SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
60423
60424 if( bBatch0==0 ){
60425 rc = pager_write_pagelist(pPager, pList);
60426 }
60427 if( rc!=SQLITE_OK0 ){
60428 assert( rc!=SQLITE_IOERR_BLOCKED )((void) (0));
60429 goto commit_phase_one_exit;
60430 }
60431 sqlite3PcacheCleanAll(pPager->pPCache);
60432
60433 /* If the file on disk is smaller than the database image, use
60434 ** pager_truncate to grow the file here. This can happen if the database
60435 ** image was extended as part of the current transaction and then the
60436 ** last page in the db image moved to the free-list. In this case the
60437 ** last page is never written out to disk, leaving the database file
60438 ** undersized. Fix this now if it is the case. */
60439 if( pPager->dbSize>pPager->dbFileSize ){
60440 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_SJ_PGNO(pPager)((pPager)->lckPgno));
60441 assert( pPager->eState==PAGER_WRITER_DBMOD )((void) (0));
60442 rc = pager_truncate(pPager, nNew);
60443 if( rc!=SQLITE_OK0 ) goto commit_phase_one_exit;
60444 }
60445
60446 /* Finally, sync the database file. */
60447 if( !noSync ){
60448 rc = sqlite3PagerSync(pPager, zSuper);
60449 }
60450 IOTRACE(("DBSYNC %p\n", pPager))
60451 }
60452 }
60453
60454commit_phase_one_exit:
60455 if( rc==SQLITE_OK0 && !pagerUseWal(pPager)((pPager)->pWal!=0) ){
60456 pPager->eState = PAGER_WRITER_FINISHED5;
60457 }
60458 return rc;
60459}
60460
60461
60462/*
60463** When this function is called, the database file has been completely
60464** updated to reflect the changes made by the current transaction and
60465** synced to disk. The journal file still exists in the file-system
60466** though, and if a failure occurs at this point it will eventually
60467** be used as a hot-journal and the current transaction rolled back.
60468**
60469** This function finalizes the journal file, either by deleting,
60470** truncating or partially zeroing it, so that it cannot be used
60471** for hot-journal rollback. Once this is done the transaction is
60472** irrevocably committed.
60473**
60474** If an error occurs, an IO error code is returned and the pager
60475** moves into the error state. Otherwise, SQLITE_OK is returned.
60476*/
60477SQLITE_PRIVATEstatic int sqlite3PagerCommitPhaseTwo(Pager *pPager){
60478 int rc = SQLITE_OK0; /* Return code */
60479
60480 /* This routine should not be called if a prior error has occurred.
60481 ** But if (due to a coding error elsewhere in the system) it does get
60482 ** called, just return the same error code without doing anything. */
60483 if( NEVER(pPager->errCode)(pPager->errCode) ) return pPager->errCode;
60484 pPager->iDataVersion++;
60485
60486 assert( pPager->eState==PAGER_WRITER_LOCKED((void) (0))
60487 || pPager->eState==PAGER_WRITER_FINISHED((void) (0))
60488 || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)((void) (0))
60489 )((void) (0));
60490 assert( assert_pager_state(pPager) )((void) (0));
60491
60492 /* An optimization. If the database was not actually modified during
60493 ** this transaction, the pager is running in exclusive-mode and is
60494 ** using persistent journals, then this function is a no-op.
60495 **
60496 ** The start of the journal file currently contains a single journal
60497 ** header with the nRec field set to 0. If such a journal is used as
60498 ** a hot-journal during hot-journal rollback, 0 changes will be made
60499 ** to the database file. So there is no need to zero the journal
60500 ** header. Since the pager is in exclusive mode, there is no need
60501 ** to drop any locks either.
60502 */
60503 if( pPager->eState==PAGER_WRITER_LOCKED2
60504 && pPager->exclusiveMode
60505 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST1
60506 ){
60507 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff )((void) (0));
60508 pPager->eState = PAGER_READER1;
60509 return SQLITE_OK0;
60510 }
60511
60512 PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
60513 rc = pager_end_transaction(pPager, pPager->setSuper, 1);
60514 return pager_error(pPager, rc);
60515}
60516
60517/*
60518** If a write transaction is open, then all changes made within the
60519** transaction are reverted and the current write-transaction is closed.
60520** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR
60521** state if an error occurs.
60522**
60523** If the pager is already in PAGER_ERROR state when this function is called,
60524** it returns Pager.errCode immediately. No work is performed in this case.
60525**
60526** Otherwise, in rollback mode, this function performs two functions:
60527**
60528** 1) It rolls back the journal file, restoring all database file and
60529** in-memory cache pages to the state they were in when the transaction
60530** was opened, and
60531**
60532** 2) It finalizes the journal file, so that it is not used for hot
60533** rollback at any point in the future.
60534**
60535** Finalization of the journal file (task 2) is only performed if the
60536** rollback is successful.
60537**
60538** In WAL mode, all cache-entries containing data modified within the
60539** current transaction are either expelled from the cache or reverted to
60540** their pre-transaction state by re-reading data from the database or
60541** WAL files. The WAL transaction is then closed.
60542*/
60543SQLITE_PRIVATEstatic int sqlite3PagerRollback(Pager *pPager){
60544 int rc = SQLITE_OK0; /* Return code */
60545 PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
60546
60547 /* PagerRollback() is a no-op if called in READER or OPEN state. If
60548 ** the pager is already in the ERROR state, the rollback is not
60549 ** attempted here. Instead, the error code is returned to the caller.
60550 */
60551 assert( assert_pager_state(pPager) )((void) (0));
60552 if( pPager->eState==PAGER_ERROR6 ) return pPager->errCode;
60553 if( pPager->eState<=PAGER_READER1 ) return SQLITE_OK0;
60554
60555 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
60556 int rc2;
60557 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK2, -1);
60558 rc2 = pager_end_transaction(pPager, pPager->setSuper, 0);
60559 if( rc==SQLITE_OK0 ) rc = rc2;
60560 }else if( !isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) || pPager->eState==PAGER_WRITER_LOCKED2 ){
60561 int eState = pPager->eState;
60562 rc = pager_end_transaction(pPager, 0, 0);
60563 if( !MEMDBpPager->memDb && eState>PAGER_WRITER_LOCKED2 ){
60564 /* This can happen using journal_mode=off. Move the pager to the error
60565 ** state to indicate that the contents of the cache may not be trusted.
60566 ** Any active readers will get SQLITE_ABORT.
60567 */
60568 pPager->errCode = SQLITE_ABORT4;
60569 pPager->eState = PAGER_ERROR6;
60570 setGetterMethod(pPager);
60571 return rc;
60572 }
60573 }else{
60574 rc = pager_playback(pPager, 0);
60575 }
60576
60577 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK )((void) (0));
60578 assert( rc==SQLITE_OK || rc==SQLITE_FULL || rc==SQLITE_CORRUPT((void) (0))
60579 || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR((void) (0))
60580 || rc==SQLITE_CANTOPEN((void) (0))
60581 )((void) (0));
60582
60583 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
60584 ** cache. So call pager_error() on the way out to make any error persistent.
60585 */
60586 return pager_error(pPager, rc);
60587}
60588
60589/*
60590** Return TRUE if the database file is opened read-only. Return FALSE
60591** if the database is (in theory) writable.
60592*/
60593SQLITE_PRIVATEstatic u8 sqlite3PagerIsreadonly(Pager *pPager){
60594 return pPager->readOnly;
60595}
60596
60597#ifdef SQLITE_DEBUG
60598/*
60599** Return the sum of the reference counts for all pages held by pPager.
60600*/
60601SQLITE_PRIVATEstatic int sqlite3PagerRefcount(Pager *pPager){
60602 return sqlite3PcacheRefCount(pPager->pPCache);
60603}
60604#endif
60605
60606/*
60607** Return the approximate number of bytes of memory currently
60608** used by the pager and its associated cache.
60609*/
60610SQLITE_PRIVATEstatic int sqlite3PagerMemUsed(Pager *pPager){
60611 int perPageSize = pPager->pageSize + pPager->nExtra
60612 + (int)(sizeof(PgHdr) + 5*sizeof(void*));
60613 return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
60614 + sqlite3MallocSize(pPager)
60615 + pPager->pageSize;
60616}
60617
60618/*
60619** Return the number of references to the specified page.
60620*/
60621SQLITE_PRIVATEstatic int sqlite3PagerPageRefcount(DbPage *pPage){
60622 return sqlite3PcachePageRefcount(pPage);
60623}
60624
60625#ifdef SQLITE_TEST
60626/*
60627** This routine is used for testing and analysis only.
60628*/
60629SQLITE_PRIVATEstatic int *sqlite3PagerStats(Pager *pPager){
60630 static int a[11];
60631 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
60632 a[1] = sqlite3PcachePagecount(pPager->pPCache);
60633 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
60634 a[3] = pPager->eState==PAGER_OPEN0 ? -1 : (int) pPager->dbSize;
60635 a[4] = pPager->eState;
60636 a[5] = pPager->errCode;
60637 a[6] = pPager->aStat[PAGER_STAT_HIT0];
60638 a[7] = pPager->aStat[PAGER_STAT_MISS1];
60639 a[8] = 0; /* Used to be pPager->nOvfl */
60640 a[9] = pPager->nRead;
60641 a[10] = pPager->aStat[PAGER_STAT_WRITE2];
60642 return a;
60643}
60644#endif
60645
60646/*
60647** Parameter eStat must be one of SQLITE_DBSTATUS_CACHE_HIT, _MISS, _WRITE,
60648** or _WRITE+1. The SQLITE_DBSTATUS_CACHE_WRITE+1 case is a translation
60649** of SQLITE_DBSTATUS_CACHE_SPILL. The _SPILL case is not contiguous because
60650** it was added later.
60651**
60652** Before returning, *pnVal is incremented by the
60653** current cache hit or miss count, according to the value of eStat. If the
60654** reset parameter is non-zero, the cache hit or miss count is zeroed before
60655** returning.
60656*/
60657SQLITE_PRIVATEstatic void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
60658
60659 assert( eStat==SQLITE_DBSTATUS_CACHE_HIT((void) (0))
60660 || eStat==SQLITE_DBSTATUS_CACHE_MISS((void) (0))
60661 || eStat==SQLITE_DBSTATUS_CACHE_WRITE((void) (0))
60662 || eStat==SQLITE_DBSTATUS_CACHE_WRITE+1((void) (0))
60663 )((void) (0));
60664
60665 assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS )((void) (0));
60666 assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE )((void) (0));
60667 assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1((void) (0))
60668 && PAGER_STAT_WRITE==2 && PAGER_STAT_SPILL==3 )((void) (0));
60669
60670 eStat -= SQLITE_DBSTATUS_CACHE_HIT7;
60671 *pnVal += pPager->aStat[eStat];
60672 if( reset ){
60673 pPager->aStat[eStat] = 0;
60674 }
60675}
60676
60677/*
60678** Return true if this is an in-memory or temp-file backed pager.
60679*/
60680SQLITE_PRIVATEstatic int sqlite3PagerIsMemdb(Pager *pPager){
60681 return pPager->tempFile || pPager->memVfs;
60682}
60683
60684/*
60685** Check that there are at least nSavepoint savepoints open. If there are
60686** currently less than nSavepoints open, then open one or more savepoints
60687** to make up the difference. If the number of savepoints is already
60688** equal to nSavepoint, then this function is a no-op.
60689**
60690** If a memory allocation fails, SQLITE_NOMEM is returned. If an error
60691** occurs while opening the sub-journal file, then an IO error code is
60692** returned. Otherwise, SQLITE_OK.
60693*/
60694static SQLITE_NOINLINE__attribute__((noinline)) int pagerOpenSavepoint(Pager *pPager, int nSavepoint){
60695 int rc = SQLITE_OK0; /* Return code */
60696 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
60697 int ii; /* Iterator variable */
60698 PagerSavepoint *aNew; /* New Pager.aSavepoint array */
60699
60700 assert( pPager->eState>=PAGER_WRITER_LOCKED )((void) (0));
60701 assert( assert_pager_state(pPager) )((void) (0));
60702 assert( nSavepoint>nCurrent && pPager->useJournal )((void) (0));
60703
60704 /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
60705 ** if the allocation fails. Otherwise, zero the new portion in case a
60706 ** malloc failure occurs while populating it in the for(...) loop below.
60707 */
60708 aNew = (PagerSavepoint *)sqlite3Realloc(
60709 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
60710 );
60711 if( !aNew ){
60712 return SQLITE_NOMEM_BKPT7;
60713 }
60714 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
60715 pPager->aSavepoint = aNew;
60716
60717 /* Populate the PagerSavepoint structures just allocated. */
60718 for(ii=nCurrent; ii<nSavepoint; ii++){
60719 aNew[ii].nOrig = pPager->dbSize;
60720 if( isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) && pPager->journalOff>0 ){
60721 aNew[ii].iOffset = pPager->journalOff;
60722 }else{
60723 aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager)(pPager->sectorSize);
60724 }
60725 aNew[ii].iSubRec = pPager->nSubRec;
60726 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
60727 aNew[ii].bTruncateOnRelease = 1;
60728 if( !aNew[ii].pInSavepoint ){
60729 return SQLITE_NOMEM_BKPT7;
60730 }
60731 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
60732 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
60733 }
60734 pPager->nSavepoint = ii+1;
60735 }
60736 assert( pPager->nSavepoint==nSavepoint )((void) (0));
60737 assertTruncateConstraint(pPager);
60738 return rc;
60739}
60740SQLITE_PRIVATEstatic int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
60741 assert( pPager->eState>=PAGER_WRITER_LOCKED )((void) (0));
60742 assert( assert_pager_state(pPager) )((void) (0));
60743
60744 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
60745 return pagerOpenSavepoint(pPager, nSavepoint);
60746 }else{
60747 return SQLITE_OK0;
60748 }
60749}
60750
60751
60752/*
60753** This function is called to rollback or release (commit) a savepoint.
60754** The savepoint to release or rollback need not be the most recently
60755** created savepoint.
60756**
60757** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
60758** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
60759** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
60760** that have occurred since the specified savepoint was created.
60761**
60762** The savepoint to rollback or release is identified by parameter
60763** iSavepoint. A value of 0 means to operate on the outermost savepoint
60764** (the first created). A value of (Pager.nSavepoint-1) means operate
60765** on the most recently created savepoint. If iSavepoint is greater than
60766** (Pager.nSavepoint-1), then this function is a no-op.
60767**
60768** If a negative value is passed to this function, then the current
60769** transaction is rolled back. This is different to calling
60770** sqlite3PagerRollback() because this function does not terminate
60771** the transaction or unlock the database, it just restores the
60772** contents of the database to its original state.
60773**
60774** In any case, all savepoints with an index greater than iSavepoint
60775** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE),
60776** then savepoint iSavepoint is also destroyed.
60777**
60778** This function may return SQLITE_NOMEM if a memory allocation fails,
60779** or an IO error code if an IO error occurs while rolling back a
60780** savepoint. If no errors occur, SQLITE_OK is returned.
60781*/
60782SQLITE_PRIVATEstatic int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
60783 int rc = pPager->errCode;
60784
60785#ifdef SQLITE_ENABLE_ZIPVFS
60786 if( op==SAVEPOINT_RELEASE1 ) rc = SQLITE_OK0;
60787#endif
60788
60789 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK )((void) (0));
60790 assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK )((void) (0));
60791
60792 if( rc==SQLITE_OK0 && iSavepoint<pPager->nSavepoint ){
60793 int ii; /* Iterator variable */
60794 int nNew; /* Number of remaining savepoints after this op. */
60795
60796 /* Figure out how many savepoints will still be active after this
60797 ** operation. Store this value in nNew. Then free resources associated
60798 ** with any savepoints that are destroyed by this operation.
60799 */
60800 nNew = iSavepoint + (( op==SAVEPOINT_RELEASE1 ) ? 0 : 1);
60801 for(ii=nNew; ii<pPager->nSavepoint; ii++){
60802 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
60803 }
60804 pPager->nSavepoint = nNew;
60805
60806 /* Truncate the sub-journal so that it only includes the parts
60807 ** that are still in use. */
60808 if( op==SAVEPOINT_RELEASE1 ){
60809 PagerSavepoint *pRel = &pPager->aSavepoint[nNew];
60810 if( pRel->bTruncateOnRelease && isOpen(pPager->sjfd)((pPager->sjfd)->pMethods!=0) ){
60811 /* Only truncate if it is an in-memory sub-journal. */
60812 if( sqlite3JournalIsInMemory(pPager->sjfd) ){
60813 i64 sz = (pPager->pageSize+4)*(i64)pRel->iSubRec;
60814 rc = sqlite3OsTruncate(pPager->sjfd, sz);
60815 assert( rc==SQLITE_OK )((void) (0));
60816 }
60817 pPager->nSubRec = pRel->iSubRec;
60818 }
60819 }
60820 /* Else this is a rollback operation, playback the specified savepoint.
60821 ** If this is a temp-file, it is possible that the journal file has
60822 ** not yet been opened. In this case there have been no changes to
60823 ** the database file, so the playback operation can be skipped.
60824 */
60825 else if( pagerUseWal(pPager)((pPager)->pWal!=0) || isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) ){
60826 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
60827 rc = pagerPlaybackSavepoint(pPager, pSavepoint);
60828 assert(rc!=SQLITE_DONE)((void) (0));
60829 }
60830
60831#ifdef SQLITE_ENABLE_ZIPVFS
60832 /* If the cache has been modified but the savepoint cannot be rolled
60833 ** back journal_mode=off, put the pager in the error state. This way,
60834 ** if the VFS used by this pager includes ZipVFS, the entire transaction
60835 ** can be rolled back at the ZipVFS level. */
60836 else if(
60837 pPager->journalMode==PAGER_JOURNALMODE_OFF2
60838 && pPager->eState>=PAGER_WRITER_CACHEMOD3
60839 ){
60840 pPager->errCode = SQLITE_ABORT4;
60841 pPager->eState = PAGER_ERROR6;
60842 setGetterMethod(pPager);
60843 }
60844#endif
60845 }
60846
60847 return rc;
60848}
60849
60850/*
60851** Return the full pathname of the database file.
60852**
60853** Except, if the pager is in-memory only, then return an empty string if
60854** nullIfMemDb is true. This routine is called with nullIfMemDb==1 when
60855** used to report the filename to the user, for compatibility with legacy
60856** behavior. But when the Btree needs to know the filename for matching to
60857** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
60858** participate in shared-cache.
60859**
60860** The return value to this routine is always safe to use with
60861** sqlite3_uri_parameter() and sqlite3_filename_database() and friends.
60862*/
60863SQLITE_PRIVATEstatic const char *sqlite3PagerFilename(const Pager *pPager, int nullIfMemDb){
60864 static const char zFake[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
60865 return (nullIfMemDb && pPager->memDb) ? &zFake[4] : pPager->zFilename;
60866}
60867
60868/*
60869** Return the VFS structure for the pager.
60870*/
60871SQLITE_PRIVATEstatic sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
60872 return pPager->pVfs;
60873}
60874
60875/*
60876** Return the file handle for the database file associated
60877** with the pager. This might return NULL if the file has
60878** not yet been opened.
60879*/
60880SQLITE_PRIVATEstatic sqlite3_file *sqlite3PagerFile(Pager *pPager){
60881 return pPager->fd;
60882}
60883
60884/*
60885** Return the file handle for the journal file (if it exists).
60886** This will be either the rollback journal or the WAL file.
60887*/
60888SQLITE_PRIVATEstatic sqlite3_file *sqlite3PagerJrnlFile(Pager *pPager){
60889#if SQLITE_OMIT_WAL
60890 return pPager->jfd;
60891#else
60892 return pPager->pWal ? sqlite3WalFile(pPager->pWal) : pPager->jfd;
60893#endif
60894}
60895
60896/*
60897** Return the full pathname of the journal file.
60898*/
60899SQLITE_PRIVATEstatic const char *sqlite3PagerJournalname(Pager *pPager){
60900 return pPager->zJournal;
60901}
60902
60903#ifndef SQLITE_OMIT_AUTOVACUUM
60904/*
60905** Move the page pPg to location pgno in the file.
60906**
60907** There must be no references to the page previously located at
60908** pgno (which we call pPgOld) though that page is allowed to be
60909** in cache. If the page previously located at pgno is not already
60910** in the rollback journal, it is not put there by by this routine.
60911**
60912** References to the page pPg remain valid. Updating any
60913** meta-data associated with pPg (i.e. data stored in the nExtra bytes
60914** allocated along with the page) is the responsibility of the caller.
60915**
60916** A transaction must be active when this routine is called. It used to be
60917** required that a statement transaction was not active, but this restriction
60918** has been removed (CREATE INDEX needs to move a page when a statement
60919** transaction is active).
60920**
60921** If the fourth argument, isCommit, is non-zero, then this page is being
60922** moved as part of a database reorganization just before the transaction
60923** is being committed. In this case, it is guaranteed that the database page
60924** pPg refers to will not be written to again within this transaction.
60925**
60926** This function may return SQLITE_NOMEM or an IO error code if an error
60927** occurs. Otherwise, it returns SQLITE_OK.
60928*/
60929SQLITE_PRIVATEstatic int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
60930 PgHdr *pPgOld; /* The page being overwritten. */
60931 Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */
60932 int rc; /* Return code */
60933 Pgno origPgno; /* The original page number */
60934
60935 assert( pPg->nRef>0 )((void) (0));
60936 assert( pPager->eState==PAGER_WRITER_CACHEMOD((void) (0))
60937 || pPager->eState==PAGER_WRITER_DBMOD((void) (0))
60938 )((void) (0));
60939 assert( assert_pager_state(pPager) )((void) (0));
60940
60941 /* In order to be able to rollback, an in-memory database must journal
60942 ** the page we are moving from.
60943 */
60944 assert( pPager->tempFile || !MEMDB )((void) (0));
60945 if( pPager->tempFile ){
60946 rc = sqlite3PagerWrite(pPg);
60947 if( rc ) return rc;
60948 }
60949
60950 /* If the page being moved is dirty and has not been saved by the latest
60951 ** savepoint, then save the current contents of the page into the
60952 ** sub-journal now. This is required to handle the following scenario:
60953 **
60954 ** BEGIN;
60955 ** <journal page X, then modify it in memory>
60956 ** SAVEPOINT one;
60957 ** <Move page X to location Y>
60958 ** ROLLBACK TO one;
60959 **
60960 ** If page X were not written to the sub-journal here, it would not
60961 ** be possible to restore its contents when the "ROLLBACK TO one"
60962 ** statement were is processed.
60963 **
60964 ** subjournalPage() may need to allocate space to store pPg->pgno into
60965 ** one or more savepoint bitvecs. This is the reason this function
60966 ** may return SQLITE_NOMEM.
60967 */
60968 if( (pPg->flags & PGHDR_DIRTY0x002)!=0
60969 && SQLITE_OK0!=(rc = subjournalPageIfRequired(pPg))
60970 ){
60971 return rc;
60972 }
60973
60974 PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
60975 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
60976 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
60977
60978 /* If the journal needs to be sync()ed before page pPg->pgno can
60979 ** be written to, store pPg->pgno in local variable needSyncPgno.
60980 **
60981 ** If the isCommit flag is set, there is no need to remember that
60982 ** the journal needs to be sync()ed before database page pPg->pgno
60983 ** can be written to. The caller has already promised not to write to it.
60984 */
60985 if( (pPg->flags&PGHDR_NEED_SYNC0x008) && !isCommit ){
60986 needSyncPgno = pPg->pgno;
60987 assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||((void) (0))
60988 pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize )((void) (0));
60989 assert( pPg->flags&PGHDR_DIRTY )((void) (0));
60990 }
60991
60992 /* If the cache contains a page with page-number pgno, remove it
60993 ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for
60994 ** page pgno before the 'move' operation, it needs to be retained
60995 ** for the page moved there.
60996 */
60997 pPg->flags &= ~PGHDR_NEED_SYNC0x008;
60998 pPgOld = sqlite3PagerLookup(pPager, pgno);
60999 assert( !pPgOld || pPgOld->nRef==1 || CORRUPT_DB )((void) (0));
61000 if( pPgOld ){
61001 if( NEVER(pPgOld->nRef>1)(pPgOld->nRef>1) ){
61002 sqlite3PagerUnrefNotNull(pPgOld);
61003 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(61003);
61004 }
61005 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC0x008);
61006 if( pPager->tempFile ){
61007 /* Do not discard pages from an in-memory database since we might
61008 ** need to rollback later. Just move the page out of the way. */
61009 sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
61010 }else{
61011 sqlite3PcacheDrop(pPgOld);
61012 }
61013 }
61014
61015 origPgno = pPg->pgno;
61016 sqlite3PcacheMove(pPg, pgno);
61017 sqlite3PcacheMakeDirty(pPg);
61018
61019 /* For an in-memory database, make sure the original page continues
61020 ** to exist, in case the transaction needs to roll back. Use pPgOld
61021 ** as the original page since it has already been allocated.
61022 */
61023 if( pPager->tempFile && pPgOld ){
61024 sqlite3PcacheMove(pPgOld, origPgno);
61025 sqlite3PagerUnrefNotNull(pPgOld);
61026 }
61027
61028 if( needSyncPgno ){
61029 /* If needSyncPgno is non-zero, then the journal file needs to be
61030 ** sync()ed before any data is written to database file page needSyncPgno.
61031 ** Currently, no such page exists in the page-cache and the
61032 ** "is journaled" bitvec flag has been set. This needs to be remedied by
61033 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
61034 ** flag.
61035 **
61036 ** If the attempt to load the page into the page-cache fails, (due
61037 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
61038 ** array. Otherwise, if the page is loaded and written again in
61039 ** this transaction, it may be written to the database file before
61040 ** it is synced into the journal file. This way, it may end up in
61041 ** the journal file twice, but that is not a problem.
61042 */
61043 PgHdr *pPgHdr;
61044 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr, 0);
61045 if( rc!=SQLITE_OK0 ){
61046 if( needSyncPgno<=pPager->dbOrigSize ){
61047 assert( pPager->pTmpSpace!=0 )((void) (0));
61048 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
61049 }
61050 return rc;
61051 }
61052 pPgHdr->flags |= PGHDR_NEED_SYNC0x008;
61053 sqlite3PcacheMakeDirty(pPgHdr);
61054 sqlite3PagerUnrefNotNull(pPgHdr);
61055 }
61056
61057 return SQLITE_OK0;
61058}
61059#endif
61060
61061/*
61062** The page handle passed as the first argument refers to a dirty page
61063** with a page number other than iNew. This function changes the page's
61064** page number to iNew and sets the value of the PgHdr.flags field to
61065** the value passed as the third parameter.
61066*/
61067SQLITE_PRIVATEstatic void sqlite3PagerRekey(DbPage *pPg, Pgno iNew, u16 flags){
61068 assert( pPg->pgno!=iNew )((void) (0));
61069 pPg->flags = flags;
61070 sqlite3PcacheMove(pPg, iNew);
61071}
61072
61073/*
61074** Return a pointer to the data for the specified page.
61075*/
61076SQLITE_PRIVATEstatic void *sqlite3PagerGetData(DbPage *pPg){
61077 assert( pPg->nRef>0 || pPg->pPager->memDb )((void) (0));
61078 return pPg->pData;
61079}
61080
61081/*
61082** Return a pointer to the Pager.nExtra bytes of "extra" space
61083** allocated along with the specified page.
61084*/
61085SQLITE_PRIVATEstatic void *sqlite3PagerGetExtra(DbPage *pPg){
61086 return pPg->pExtra;
61087}
61088
61089/*
61090** Get/set the locking-mode for this pager. Parameter eMode must be one
61091** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
61092** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
61093** the locking-mode is set to the value specified.
61094**
61095** The returned value is either PAGER_LOCKINGMODE_NORMAL or
61096** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
61097** locking-mode.
61098*/
61099SQLITE_PRIVATEstatic int sqlite3PagerLockingMode(Pager *pPager, int eMode){
61100 assert( eMode==PAGER_LOCKINGMODE_QUERY((void) (0))
61101 || eMode==PAGER_LOCKINGMODE_NORMAL((void) (0))
61102 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE )((void) (0));
61103 assert( PAGER_LOCKINGMODE_QUERY<0 )((void) (0));
61104 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 )((void) (0));
61105 assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) )((void) (0));
61106 if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
61107 pPager->exclusiveMode = (u8)eMode;
61108 }
61109 return (int)pPager->exclusiveMode;
61110}
61111
61112/*
61113** Set the journal-mode for this pager. Parameter eMode must be one of:
61114**
61115** PAGER_JOURNALMODE_DELETE
61116** PAGER_JOURNALMODE_TRUNCATE
61117** PAGER_JOURNALMODE_PERSIST
61118** PAGER_JOURNALMODE_OFF
61119** PAGER_JOURNALMODE_MEMORY
61120** PAGER_JOURNALMODE_WAL
61121**
61122** The journalmode is set to the value specified if the change is allowed.
61123** The change may be disallowed for the following reasons:
61124**
61125** * An in-memory database can only have its journal_mode set to _OFF
61126** or _MEMORY.
61127**
61128** * Temporary databases cannot have _WAL journalmode.
61129**
61130** The returned indicate the current (possibly updated) journal-mode.
61131*/
61132SQLITE_PRIVATEstatic int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
61133 u8 eOld = pPager->journalMode; /* Prior journalmode */
61134
61135 /* The eMode parameter is always valid */
61136 assert( eMode==PAGER_JOURNALMODE_DELETE /* 0 */((void) (0))
61137 || eMode==PAGER_JOURNALMODE_PERSIST /* 1 */((void) (0))
61138 || eMode==PAGER_JOURNALMODE_OFF /* 2 */((void) (0))
61139 || eMode==PAGER_JOURNALMODE_TRUNCATE /* 3 */((void) (0))
61140 || eMode==PAGER_JOURNALMODE_MEMORY /* 4 */((void) (0))
61141 || eMode==PAGER_JOURNALMODE_WAL /* 5 */ )((void) (0));
61142
61143 /* This routine is only called from the OP_JournalMode opcode, and
61144 ** the logic there will never allow a temporary file to be changed
61145 ** to WAL mode.
61146 */
61147 assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL )((void) (0));
61148
61149 /* Do allow the journalmode of an in-memory database to be set to
61150 ** anything other than MEMORY or OFF
61151 */
61152 if( MEMDBpPager->memDb ){
61153 assert( eOld==PAGER_JOURNALMODE_MEMORY || eOld==PAGER_JOURNALMODE_OFF )((void) (0));
61154 if( eMode!=PAGER_JOURNALMODE_MEMORY4 && eMode!=PAGER_JOURNALMODE_OFF2 ){
61155 eMode = eOld;
61156 }
61157 }
61158
61159 if( eMode!=eOld ){
61160
61161 /* Change the journal mode. */
61162 assert( pPager->eState!=PAGER_ERROR )((void) (0));
61163 pPager->journalMode = (u8)eMode;
61164
61165 /* When transistioning from TRUNCATE or PERSIST to any other journal
61166 ** mode except WAL, unless the pager is in locking_mode=exclusive mode,
61167 ** delete the journal file.
61168 */
61169 assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 )((void) (0));
61170 assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 )((void) (0));
61171 assert( (PAGER_JOURNALMODE_DELETE & 5)==0 )((void) (0));
61172 assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 )((void) (0));
61173 assert( (PAGER_JOURNALMODE_OFF & 5)==0 )((void) (0));
61174 assert( (PAGER_JOURNALMODE_WAL & 5)==5 )((void) (0));
61175
61176 assert( isOpen(pPager->fd) || pPager->exclusiveMode )((void) (0));
61177 if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
61178 /* In this case we would like to delete the journal file. If it is
61179 ** not possible, then that is not a problem. Deleting the journal file
61180 ** here is an optimization only.
61181 **
61182 ** Before deleting the journal file, obtain a RESERVED lock on the
61183 ** database file. This ensures that the journal file is not deleted
61184 ** while it is in use by some other client.
61185 */
61186 sqlite3OsClose(pPager->jfd);
61187 if( pPager->eLock>=RESERVED_LOCK2 ){
61188 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
61189 }else{
61190 int rc = SQLITE_OK0;
61191 int state = pPager->eState;
61192 assert( state==PAGER_OPEN || state==PAGER_READER )((void) (0));
61193 if( state==PAGER_OPEN0 ){
61194 rc = sqlite3PagerSharedLock(pPager);
61195 }
61196 if( pPager->eState==PAGER_READER1 ){
61197 assert( rc==SQLITE_OK )((void) (0));
61198 rc = pagerLockDb(pPager, RESERVED_LOCK2);
61199 }
61200 if( rc==SQLITE_OK0 ){
61201 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
61202 }
61203 if( rc==SQLITE_OK0 && state==PAGER_READER1 ){
61204 pagerUnlockDb(pPager, SHARED_LOCK1);
61205 }else if( state==PAGER_OPEN0 ){
61206 pager_unlock(pPager);
61207 }
61208 assert( state==pPager->eState )((void) (0));
61209 }
61210 }else if( eMode==PAGER_JOURNALMODE_OFF2 ){
61211 sqlite3OsClose(pPager->jfd);
61212 }
61213 }
61214
61215 /* Return the new journal mode */
61216 return (int)pPager->journalMode;
61217}
61218
61219/*
61220** Return the current journal mode.
61221*/
61222SQLITE_PRIVATEstatic int sqlite3PagerGetJournalMode(Pager *pPager){
61223 return (int)pPager->journalMode;
61224}
61225
61226/*
61227** Return TRUE if the pager is in a state where it is OK to change the
61228** journalmode. Journalmode changes can only happen when the database
61229** is unmodified.
61230*/
61231SQLITE_PRIVATEstatic int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
61232 assert( assert_pager_state(pPager) )((void) (0));
61233 if( pPager->eState>=PAGER_WRITER_CACHEMOD3 ) return 0;
61234 if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0)(((pPager->jfd)->pMethods!=0) && pPager->journalOff
>0)
) return 0;
61235 return 1;
61236}
61237
61238/*
61239** Get/set the size-limit used for persistent journal files.
61240**
61241** Setting the size limit to -1 means no limit is enforced.
61242** An attempt to set a limit smaller than -1 is a no-op.
61243*/
61244SQLITE_PRIVATEstatic i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
61245 if( iLimit>=-1 ){
61246 pPager->journalSizeLimit = iLimit;
61247 sqlite3WalLimit(pPager->pWal, iLimit);
61248 }
61249 return pPager->journalSizeLimit;
61250}
61251
61252/*
61253** Return a pointer to the pPager->pBackup variable. The backup module
61254** in backup.c maintains the content of this variable. This module
61255** uses it opaquely as an argument to sqlite3BackupRestart() and
61256** sqlite3BackupUpdate() only.
61257*/
61258SQLITE_PRIVATEstatic sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
61259 return &pPager->pBackup;
61260}
61261
61262#ifndef SQLITE_OMIT_VACUUM
61263/*
61264** Unless this is an in-memory or temporary database, clear the pager cache.
61265*/
61266SQLITE_PRIVATEstatic void sqlite3PagerClearCache(Pager *pPager){
61267 assert( MEMDB==0 || pPager->tempFile )((void) (0));
61268 if( pPager->tempFile==0 ) pager_reset(pPager);
61269}
61270#endif
61271
61272
61273#ifndef SQLITE_OMIT_WAL
61274/*
61275** This function is called when the user invokes "PRAGMA wal_checkpoint",
61276** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
61277** or wal_blocking_checkpoint() API functions.
61278**
61279** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
61280*/
61281SQLITE_PRIVATEstatic int sqlite3PagerCheckpoint(
61282 Pager *pPager, /* Checkpoint on this pager */
61283 sqlite3 *db, /* Db handle used to check for interrupts */
61284 int eMode, /* Type of checkpoint */
61285 int *pnLog, /* OUT: Final number of frames in log */
61286 int *pnCkpt /* OUT: Final number of checkpointed frames */
61287){
61288 int rc = SQLITE_OK0;
61289 if( pPager->pWal==0 && pPager->journalMode==PAGER_JOURNALMODE_WAL5 ){
61290 /* This only happens when a database file is zero bytes in size opened and
61291 ** then "PRAGMA journal_mode=WAL" is run and then sqlite3_wal_checkpoint()
61292 ** is invoked without any intervening transactions. We need to start
61293 ** a transaction to initialize pWal. The PRAGMA table_list statement is
61294 ** used for this since it starts transactions on every database file,
61295 ** including all ATTACHed databases. This seems expensive for a single
61296 ** sqlite3_wal_checkpoint() call, but it happens very rarely.
61297 ** https://sqlite.org/forum/forumpost/fd0f19d229156939
61298 */
61299 sqlite3_exec(db, "PRAGMA table_list",0,0,0);
61300 }
61301 if( pPager->pWal ){
61302 rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
61303 (eMode==SQLITE_CHECKPOINT_PASSIVE0 ? 0 : pPager->xBusyHandler),
61304 pPager->pBusyHandlerArg,
61305 pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
61306 pnLog, pnCkpt
61307 );
61308 }
61309 return rc;
61310}
61311
61312SQLITE_PRIVATEstatic int sqlite3PagerWalCallback(Pager *pPager){
61313 return sqlite3WalCallback(pPager->pWal);
61314}
61315
61316/*
61317** Return true if the underlying VFS for the given pager supports the
61318** primitives necessary for write-ahead logging.
61319*/
61320SQLITE_PRIVATEstatic int sqlite3PagerWalSupported(Pager *pPager){
61321 const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
61322 if( pPager->noLock ) return 0;
61323 return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
61324}
61325
61326/*
61327** Attempt to take an exclusive lock on the database file. If a PENDING lock
61328** is obtained instead, immediately release it.
61329*/
61330static int pagerExclusiveLock(Pager *pPager){
61331 int rc; /* Return code */
61332
61333 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK )((void) (0));
61334 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK4);
61335 if( rc!=SQLITE_OK0 ){
61336 /* If the attempt to grab the exclusive lock failed, release the
61337 ** pending lock that may have been obtained instead. */
61338 pagerUnlockDb(pPager, SHARED_LOCK1);
61339 }
61340
61341 return rc;
61342}
61343
61344/*
61345** Call sqlite3WalOpen() to open the WAL handle. If the pager is in
61346** exclusive-locking mode when this function is called, take an EXCLUSIVE
61347** lock on the database file and use heap-memory to store the wal-index
61348** in. Otherwise, use the normal shared-memory.
61349*/
61350static int pagerOpenWal(Pager *pPager){
61351 int rc = SQLITE_OK0;
61352
61353 assert( pPager->pWal==0 && pPager->tempFile==0 )((void) (0));
61354 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK )((void) (0));
61355
61356 /* If the pager is already in exclusive-mode, the WAL module will use
61357 ** heap-memory for the wal-index instead of the VFS shared-memory
61358 ** implementation. Take the exclusive lock now, before opening the WAL
61359 ** file, to make sure this is safe.
61360 */
61361 if( pPager->exclusiveMode ){
61362 rc = pagerExclusiveLock(pPager);
61363 }
61364
61365 /* Open the connection to the log file. If this operation fails,
61366 ** (e.g. due to malloc() failure), return an error code.
61367 */
61368 if( rc==SQLITE_OK0 ){
61369 rc = sqlite3WalOpen(pPager->pVfs,
61370 pPager->fd, pPager->zWal, pPager->exclusiveMode,
61371 pPager->journalSizeLimit, &pPager->pWal
61372 );
61373 }
61374 pagerFixMaplimit(pPager);
61375
61376 return rc;
61377}
61378
61379
61380/*
61381** The caller must be holding a SHARED lock on the database file to call
61382** this function.
61383**
61384** If the pager passed as the first argument is open on a real database
61385** file (not a temp file or an in-memory database), and the WAL file
61386** is not already open, make an attempt to open it now. If successful,
61387** return SQLITE_OK. If an error occurs or the VFS used by the pager does
61388** not support the xShmXXX() methods, return an error code. *pbOpen is
61389** not modified in either case.
61390**
61391** If the pager is open on a temp-file (or in-memory database), or if
61392** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK
61393** without doing anything.
61394*/
61395SQLITE_PRIVATEstatic int sqlite3PagerOpenWal(
61396 Pager *pPager, /* Pager object */
61397 int *pbOpen /* OUT: Set to true if call is a no-op */
61398){
61399 int rc = SQLITE_OK0; /* Return code */
61400
61401 assert( assert_pager_state(pPager) )((void) (0));
61402 assert( pPager->eState==PAGER_OPEN || pbOpen )((void) (0));
61403 assert( pPager->eState==PAGER_READER || !pbOpen )((void) (0));
61404 assert( pbOpen==0 || *pbOpen==0 )((void) (0));
61405 assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) )((void) (0));
61406
61407 if( !pPager->tempFile && !pPager->pWal ){
61408 if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN14;
61409
61410 /* Close any rollback journal previously open */
61411 sqlite3OsClose(pPager->jfd);
61412
61413 rc = pagerOpenWal(pPager);
61414 if( rc==SQLITE_OK0 ){
61415 pPager->journalMode = PAGER_JOURNALMODE_WAL5;
61416 pPager->eState = PAGER_OPEN0;
61417 }
61418 }else{
61419 *pbOpen = 1;
61420 }
61421
61422 return rc;
61423}
61424
61425/*
61426** This function is called to close the connection to the log file prior
61427** to switching from WAL to rollback mode.
61428**
61429** Before closing the log file, this function attempts to take an
61430** EXCLUSIVE lock on the database file. If this cannot be obtained, an
61431** error (SQLITE_BUSY) is returned and the log connection is not closed.
61432** If successful, the EXCLUSIVE lock is not released before returning.
61433*/
61434SQLITE_PRIVATEstatic int sqlite3PagerCloseWal(Pager *pPager, sqlite3 *db){
61435 int rc = SQLITE_OK0;
61436
61437 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL )((void) (0));
61438
61439 /* If the log file is not already open, but does exist in the file-system,
61440 ** it may need to be checkpointed before the connection can switch to
61441 ** rollback mode. Open it now so this can happen.
61442 */
61443 if( !pPager->pWal ){
61444 int logexists = 0;
61445 rc = pagerLockDb(pPager, SHARED_LOCK1);
61446 if( rc==SQLITE_OK0 ){
61447 rc = sqlite3OsAccess(
61448 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS0, &logexists
61449 );
61450 }
61451 if( rc==SQLITE_OK0 && logexists ){
61452 rc = pagerOpenWal(pPager);
61453 }
61454 }
61455
61456 /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
61457 ** the database file, the log and log-summary files will be deleted.
61458 */
61459 if( rc==SQLITE_OK0 && pPager->pWal ){
61460 rc = pagerExclusiveLock(pPager);
61461 if( rc==SQLITE_OK0 ){
61462 rc = sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags,
61463 pPager->pageSize, (u8*)pPager->pTmpSpace);
61464 pPager->pWal = 0;
61465 pagerFixMaplimit(pPager);
61466 if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK1);
61467 }
61468 }
61469 return rc;
61470}
61471
61472#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
61473/*
61474** If pager pPager is a wal-mode database not in exclusive locking mode,
61475** invoke the sqlite3WalWriteLock() function on the associated Wal object
61476** with the same db and bLock parameters as were passed to this function.
61477** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
61478*/
61479SQLITE_PRIVATEstatic int sqlite3PagerWalWriteLock(Pager *pPager, int bLock)0{
61480 int rc = SQLITE_OK0;
61481 if( pagerUseWal(pPager)((pPager)->pWal!=0) && pPager->exclusiveMode==0 ){
61482 rc = sqlite3WalWriteLock(pPager->pWal, bLock);
61483 }
61484 return rc;
61485}
61486
61487/*
61488** Set the database handle used by the wal layer to determine if
61489** blocking locks are required.
61490*/
61491SQLITE_PRIVATEstatic void sqlite3PagerWalDb(Pager *pPager, sqlite3 *db){
61492 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
61493 sqlite3WalDb(pPager->pWal, db);
61494 }
61495}
61496#endif
61497
61498#ifdef SQLITE_ENABLE_SNAPSHOT1
61499/*
61500** If this is a WAL database, obtain a snapshot handle for the snapshot
61501** currently open. Otherwise, return an error.
61502*/
61503SQLITE_PRIVATEstatic int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot){
61504 int rc = SQLITE_ERROR1;
61505 if( pPager->pWal ){
61506 rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot);
61507 }
61508 return rc;
61509}
61510
61511/*
61512** If this is a WAL database, store a pointer to pSnapshot. Next time a
61513** read transaction is opened, attempt to read from the snapshot it
61514** identifies. If this is not a WAL database, return an error.
61515*/
61516SQLITE_PRIVATEstatic int sqlite3PagerSnapshotOpen(
61517 Pager *pPager,
61518 sqlite3_snapshot *pSnapshot
61519){
61520 int rc = SQLITE_OK0;
61521 if( pPager->pWal ){
61522 sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot);
61523 }else{
61524 rc = SQLITE_ERROR1;
61525 }
61526 return rc;
61527}
61528
61529/*
61530** If this is a WAL database, call sqlite3WalSnapshotRecover(). If this
61531** is not a WAL database, return an error.
61532*/
61533SQLITE_PRIVATEstatic int sqlite3PagerSnapshotRecover(Pager *pPager){
61534 int rc;
61535 if( pPager->pWal ){
61536 rc = sqlite3WalSnapshotRecover(pPager->pWal);
61537 }else{
61538 rc = SQLITE_ERROR1;
61539 }
61540 return rc;
61541}
61542
61543/*
61544** The caller currently has a read transaction open on the database.
61545** If this is not a WAL database, SQLITE_ERROR is returned. Otherwise,
61546** this function takes a SHARED lock on the CHECKPOINTER slot and then
61547** checks if the snapshot passed as the second argument is still
61548** available. If so, SQLITE_OK is returned.
61549**
61550** If the snapshot is not available, SQLITE_ERROR is returned. Or, if
61551** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error
61552** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER
61553** lock is released before returning.
61554*/
61555SQLITE_PRIVATEstatic int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot){
61556 int rc;
61557 if( pPager->pWal ){
61558 rc = sqlite3WalSnapshotCheck(pPager->pWal, pSnapshot);
61559 }else{
61560 rc = SQLITE_ERROR1;
61561 }
61562 return rc;
61563}
61564
61565/*
61566** Release a lock obtained by an earlier successful call to
61567** sqlite3PagerSnapshotCheck().
61568*/
61569SQLITE_PRIVATEstatic void sqlite3PagerSnapshotUnlock(Pager *pPager){
61570 assert( pPager->pWal )((void) (0));
61571 sqlite3WalSnapshotUnlock(pPager->pWal);
61572}
61573
61574#endif /* SQLITE_ENABLE_SNAPSHOT */
61575#endif /* !SQLITE_OMIT_WAL */
61576
61577#ifdef SQLITE_ENABLE_ZIPVFS
61578/*
61579** A read-lock must be held on the pager when this function is called. If
61580** the pager is in WAL mode and the WAL file currently contains one or more
61581** frames, return the size in bytes of the page images stored within the
61582** WAL frames. Otherwise, if this is not a WAL database or the WAL file
61583** is empty, return 0.
61584*/
61585SQLITE_PRIVATEstatic int sqlite3PagerWalFramesize(Pager *pPager){
61586 assert( pPager->eState>=PAGER_READER )((void) (0));
61587 return sqlite3WalFramesize(pPager->pWal);
61588}
61589#endif
61590
61591#endif /* SQLITE_OMIT_DISKIO */
61592
61593/************** End of pager.c ***********************************************/
61594/************** Begin file wal.c *********************************************/
61595/*
61596** 2010 February 1
61597**
61598** The author disclaims copyright to this source code. In place of
61599** a legal notice, here is a blessing:
61600**
61601** May you do good and not evil.
61602** May you find forgiveness for yourself and forgive others.
61603** May you share freely, never taking more than you give.
61604**
61605*************************************************************************
61606**
61607** This file contains the implementation of a write-ahead log (WAL) used in
61608** "journal_mode=WAL" mode.
61609**
61610** WRITE-AHEAD LOG (WAL) FILE FORMAT
61611**
61612** A WAL file consists of a header followed by zero or more "frames".
61613** Each frame records the revised content of a single page from the
61614** database file. All changes to the database are recorded by writing
61615** frames into the WAL. Transactions commit when a frame is written that
61616** contains a commit marker. A single WAL can and usually does record
61617** multiple transactions. Periodically, the content of the WAL is
61618** transferred back into the database file in an operation called a
61619** "checkpoint".
61620**
61621** A single WAL file can be used multiple times. In other words, the
61622** WAL can fill up with frames and then be checkpointed and then new
61623** frames can overwrite the old ones. A WAL always grows from beginning
61624** toward the end. Checksums and counters attached to each frame are
61625** used to determine which frames within the WAL are valid and which
61626** are leftovers from prior checkpoints.
61627**
61628** The WAL header is 32 bytes in size and consists of the following eight
61629** big-endian 32-bit unsigned integer values:
61630**
61631** 0: Magic number. 0x377f0682 or 0x377f0683
61632** 4: File format version. Currently 3007000
61633** 8: Database page size. Example: 1024
61634** 12: Checkpoint sequence number
61635** 16: Salt-1, random integer incremented with each checkpoint
61636** 20: Salt-2, a different random integer changing with each ckpt
61637** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
61638** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
61639**
61640** Immediately following the wal-header are zero or more frames. Each
61641** frame consists of a 24-byte frame-header followed by a <page-size> bytes
61642** of page data. The frame-header is six big-endian 32-bit unsigned
61643** integer values, as follows:
61644**
61645** 0: Page number.
61646** 4: For commit records, the size of the database image in pages
61647** after the commit. For all other records, zero.
61648** 8: Salt-1 (copied from the header)
61649** 12: Salt-2 (copied from the header)
61650** 16: Checksum-1.
61651** 20: Checksum-2.
61652**
61653** A frame is considered valid if and only if the following conditions are
61654** true:
61655**
61656** (1) The salt-1 and salt-2 values in the frame-header match
61657** salt values in the wal-header
61658**
61659** (2) The checksum values in the final 8 bytes of the frame-header
61660** exactly match the checksum computed consecutively on the
61661** WAL header and the first 8 bytes and the content of all frames
61662** up to and including the current frame.
61663**
61664** The checksum is computed using 32-bit big-endian integers if the
61665** magic number in the first 4 bytes of the WAL is 0x377f0683 and it
61666** is computed using little-endian if the magic number is 0x377f0682.
61667** The checksum values are always stored in the frame header in a
61668** big-endian format regardless of which byte order is used to compute
61669** the checksum. The checksum is computed by interpreting the input as
61670** an even number of unsigned 32-bit integers: x[0] through x[N]. The
61671** algorithm used for the checksum is as follows:
61672**
61673** for i from 0 to n-1 step 2:
61674** s0 += x[i] + s1;
61675** s1 += x[i+1] + s0;
61676** endfor
61677**
61678** Note that s0 and s1 are both weighted checksums using fibonacci weights
61679** in reverse order (the largest fibonacci weight occurs on the first element
61680** of the sequence being summed.) The s1 value spans all 32-bit
61681** terms of the sequence whereas s0 omits the final term.
61682**
61683** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
61684** WAL is transferred into the database, then the database is VFS.xSync-ed.
61685** The VFS.xSync operations serve as write barriers - all writes launched
61686** before the xSync must complete before any write that launches after the
61687** xSync begins.
61688**
61689** After each checkpoint, the salt-1 value is incremented and the salt-2
61690** value is randomized. This prevents old and new frames in the WAL from
61691** being considered valid at the same time and being checkpointing together
61692** following a crash.
61693**
61694** READER ALGORITHM
61695**
61696** To read a page from the database (call it page number P), a reader
61697** first checks the WAL to see if it contains page P. If so, then the
61698** last valid instance of page P that is a followed by a commit frame
61699** or is a commit frame itself becomes the value read. If the WAL
61700** contains no copies of page P that are valid and which are a commit
61701** frame or are followed by a commit frame, then page P is read from
61702** the database file.
61703**
61704** To start a read transaction, the reader records the index of the last
61705** valid frame in the WAL. The reader uses this recorded "mxFrame" value
61706** for all subsequent read operations. New transactions can be appended
61707** to the WAL, but as long as the reader uses its original mxFrame value
61708** and ignores the newly appended content, it will see a consistent snapshot
61709** of the database from a single point in time. This technique allows
61710** multiple concurrent readers to view different versions of the database
61711** content simultaneously.
61712**
61713** The reader algorithm in the previous paragraphs works correctly, but
61714** because frames for page P can appear anywhere within the WAL, the
61715** reader has to scan the entire WAL looking for page P frames. If the
61716** WAL is large (multiple megabytes is typical) that scan can be slow,
61717** and read performance suffers. To overcome this problem, a separate
61718** data structure called the wal-index is maintained to expedite the
61719** search for frames of a particular page.
61720**
61721** WAL-INDEX FORMAT
61722**
61723** Conceptually, the wal-index is shared memory, though VFS implementations
61724** might choose to implement the wal-index using a mmapped file. Because
61725** the wal-index is shared memory, SQLite does not support journal_mode=WAL
61726** on a network filesystem. All users of the database must be able to
61727** share memory.
61728**
61729** In the default unix and windows implementation, the wal-index is a mmapped
61730** file whose name is the database name with a "-shm" suffix added. For that
61731** reason, the wal-index is sometimes called the "shm" file.
61732**
61733** The wal-index is transient. After a crash, the wal-index can (and should
61734** be) reconstructed from the original WAL file. In fact, the VFS is required
61735** to either truncate or zero the header of the wal-index when the last
61736** connection to it closes. Because the wal-index is transient, it can
61737** use an architecture-specific format; it does not have to be cross-platform.
61738** Hence, unlike the database and WAL file formats which store all values
61739** as big endian, the wal-index can store multi-byte values in the native
61740** byte order of the host computer.
61741**
61742** The purpose of the wal-index is to answer this question quickly: Given
61743** a page number P and a maximum frame index M, return the index of the
61744** last frame in the wal before frame M for page P in the WAL, or return
61745** NULL if there are no frames for page P in the WAL prior to M.
61746**
61747** The wal-index consists of a header region, followed by an one or
61748** more index blocks.
61749**
61750** The wal-index header contains the total number of frames within the WAL
61751** in the mxFrame field.
61752**
61753** Each index block except for the first contains information on
61754** HASHTABLE_NPAGE frames. The first index block contains information on
61755** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and
61756** HASHTABLE_NPAGE are selected so that together the wal-index header and
61757** first index block are the same size as all other index blocks in the
61758** wal-index. The values are:
61759**
61760** HASHTABLE_NPAGE 4096
61761** HASHTABLE_NPAGE_ONE 4062
61762**
61763** Each index block contains two sections, a page-mapping that contains the
61764** database page number associated with each wal frame, and a hash-table
61765** that allows readers to query an index block for a specific page number.
61766** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
61767** for the first index block) 32-bit page numbers. The first entry in the
61768** first index-block contains the database page number corresponding to the
61769** first frame in the WAL file. The first entry in the second index block
61770** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in
61771** the log, and so on.
61772**
61773** The last index block in a wal-index usually contains less than the full
61774** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
61775** depending on the contents of the WAL file. This does not change the
61776** allocated size of the page-mapping array - the page-mapping array merely
61777** contains unused entries.
61778**
61779** Even without using the hash table, the last frame for page P
61780** can be found by scanning the page-mapping sections of each index block
61781** starting with the last index block and moving toward the first, and
61782** within each index block, starting at the end and moving toward the
61783** beginning. The first entry that equals P corresponds to the frame
61784** holding the content for that page.
61785**
61786** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
61787** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the
61788** hash table for each page number in the mapping section, so the hash
61789** table is never more than half full. The expected number of collisions
61790** prior to finding a match is 1. Each entry of the hash table is an
61791** 1-based index of an entry in the mapping section of the same
61792** index block. Let K be the 1-based index of the largest entry in
61793** the mapping section. (For index blocks other than the last, K will
61794** always be exactly HASHTABLE_NPAGE (4096) and for the last index block
61795** K will be (mxFrame%HASHTABLE_NPAGE).) Unused slots of the hash table
61796** contain a value of 0.
61797**
61798** To look for page P in the hash table, first compute a hash iKey on
61799** P as follows:
61800**
61801** iKey = (P * 383) % HASHTABLE_NSLOT
61802**
61803** Then start scanning entries of the hash table, starting with iKey
61804** (wrapping around to the beginning when the end of the hash table is
61805** reached) until an unused hash slot is found. Let the first unused slot
61806** be at index iUnused. (iUnused might be less than iKey if there was
61807** wrap-around.) Because the hash table is never more than half full,
61808** the search is guaranteed to eventually hit an unused entry. Let
61809** iMax be the value between iKey and iUnused, closest to iUnused,
61810** where aHash[iMax]==P. If there is no iMax entry (if there exists
61811** no hash slot such that aHash[i]==p) then page P is not in the
61812** current index block. Otherwise the iMax-th mapping entry of the
61813** current index block corresponds to the last entry that references
61814** page P.
61815**
61816** A hash search begins with the last index block and moves toward the
61817** first index block, looking for entries corresponding to page P. On
61818** average, only two or three slots in each index block need to be
61819** examined in order to either find the last entry for page P, or to
61820** establish that no such entry exists in the block. Each index block
61821** holds over 4000 entries. So two or three index blocks are sufficient
61822** to cover a typical 10 megabyte WAL file, assuming 1K pages. 8 or 10
61823** comparisons (on average) suffice to either locate a frame in the
61824** WAL or to establish that the frame does not exist in the WAL. This
61825** is much faster than scanning the entire 10MB WAL.
61826**
61827** Note that entries are added in order of increasing K. Hence, one
61828** reader might be using some value K0 and a second reader that started
61829** at a later time (after additional transactions were added to the WAL
61830** and to the wal-index) might be using a different value K1, where K1>K0.
61831** Both readers can use the same hash table and mapping section to get
61832** the correct result. There may be entries in the hash table with
61833** K>K0 but to the first reader, those entries will appear to be unused
61834** slots in the hash table and so the first reader will get an answer as
61835** if no values greater than K0 had ever been inserted into the hash table
61836** in the first place - which is what reader one wants. Meanwhile, the
61837** second reader using K1 will see additional values that were inserted
61838** later, which is exactly what reader two wants.
61839**
61840** When a rollback occurs, the value of K is decreased. Hash table entries
61841** that correspond to frames greater than the new K value are removed
61842** from the hash table at this point.
61843*/
61844#ifndef SQLITE_OMIT_WAL
61845
61846/* #include "wal.h" */
61847
61848/*
61849** Trace output macros
61850*/
61851#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
61852SQLITE_PRIVATEstatic int sqlite3WalTrace = 0;
61853# define WALTRACE(X) if(sqlite3WalTrace) sqlite3DebugPrintf X
61854#else
61855# define WALTRACE(X)
61856#endif
61857
61858/*
61859** The maximum (and only) versions of the wal and wal-index formats
61860** that may be interpreted by this version of SQLite.
61861**
61862** If a client begins recovering a WAL file and finds that (a) the checksum
61863** values in the wal-header are correct and (b) the version field is not
61864** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
61865**
61866** Similarly, if a client successfully reads a wal-index header (i.e. the
61867** checksum test is successful) and finds that the version field is not
61868** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
61869** returns SQLITE_CANTOPEN.
61870*/
61871#define WAL_MAX_VERSION3007000 3007000
61872#define WALINDEX_MAX_VERSION3007000 3007000
61873
61874/*
61875** Index numbers for various locking bytes. WAL_NREADER is the number
61876** of available reader locks and should be at least 3. The default
61877** is SQLITE_SHM_NLOCK==8 and WAL_NREADER==5.
61878**
61879** Technically, the various VFSes are free to implement these locks however
61880** they see fit. However, compatibility is encouraged so that VFSes can
61881** interoperate. The standard implemention used on both unix and windows
61882** is for the index number to indicate a byte offset into the
61883** WalCkptInfo.aLock[] array in the wal-index header. In other words, all
61884** locks are on the shm file. The WALINDEX_LOCK_OFFSET constant (which
61885** should be 120) is the location in the shm file for the first locking
61886** byte.
61887*/
61888#define WAL_WRITE_LOCK0 0
61889#define WAL_ALL_BUT_WRITE1 1
61890#define WAL_CKPT_LOCK1 1
61891#define WAL_RECOVER_LOCK2 2
61892#define WAL_READ_LOCK(I)(3+(I)) (3+(I))
61893#define WAL_NREADER(8 -3) (SQLITE_SHM_NLOCK8-3)
61894
61895
61896/* Object declarations */
61897typedef struct WalIndexHdr WalIndexHdr;
61898typedef struct WalIterator WalIterator;
61899typedef struct WalCkptInfo WalCkptInfo;
61900
61901
61902/*
61903** The following object holds a copy of the wal-index header content.
61904**
61905** The actual header in the wal-index consists of two copies of this
61906** object followed by one instance of the WalCkptInfo object.
61907** For all versions of SQLite through 3.10.0 and probably beyond,
61908** the locking bytes (WalCkptInfo.aLock) start at offset 120 and
61909** the total header size is 136 bytes.
61910**
61911** The szPage value can be any power of 2 between 512 and 32768, inclusive.
61912** Or it can be 1 to represent a 65536-byte page. The latter case was
61913** added in 3.7.1 when support for 64K pages was added.
61914*/
61915struct WalIndexHdr {
61916 u32 iVersion; /* Wal-index version */
61917 u32 unused; /* Unused (padding) field */
61918 u32 iChange; /* Counter incremented each transaction */
61919 u8 isInit; /* 1 when initialized */
61920 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
61921 u16 szPage; /* Database page size in bytes. 1==64K */
61922 u32 mxFrame; /* Index of last valid frame in the WAL */
61923 u32 nPage; /* Size of database in pages */
61924 u32 aFrameCksum[2]; /* Checksum of last frame in log */
61925 u32 aSalt[2]; /* Two salt values copied from WAL header */
61926 u32 aCksum[2]; /* Checksum over all prior fields */
61927};
61928
61929/*
61930** A copy of the following object occurs in the wal-index immediately
61931** following the second copy of the WalIndexHdr. This object stores
61932** information used by checkpoint.
61933**
61934** nBackfill is the number of frames in the WAL that have been written
61935** back into the database. (We call the act of moving content from WAL to
61936** database "backfilling".) The nBackfill number is never greater than
61937** WalIndexHdr.mxFrame. nBackfill can only be increased by threads
61938** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
61939** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
61940** mxFrame back to zero when the WAL is reset.
61941**
61942** nBackfillAttempted is the largest value of nBackfill that a checkpoint
61943** has attempted to achieve. Normally nBackfill==nBackfillAtempted, however
61944** the nBackfillAttempted is set before any backfilling is done and the
61945** nBackfill is only set after all backfilling completes. So if a checkpoint
61946** crashes, nBackfillAttempted might be larger than nBackfill. The
61947** WalIndexHdr.mxFrame must never be less than nBackfillAttempted.
61948**
61949** The aLock[] field is a set of bytes used for locking. These bytes should
61950** never be read or written.
61951**
61952** There is one entry in aReadMark[] for each reader lock. If a reader
61953** holds read-lock K, then the value in aReadMark[K] is no greater than
61954** the mxFrame for that reader. The value READMARK_NOT_USED (0xffffffff)
61955** for any aReadMark[] means that entry is unused. aReadMark[0] is
61956** a special case; its value is never used and it exists as a place-holder
61957** to avoid having to offset aReadMark[] indexs by one. Readers holding
61958** WAL_READ_LOCK(0) always ignore the entire WAL and read all content
61959** directly from the database.
61960**
61961** The value of aReadMark[K] may only be changed by a thread that
61962** is holding an exclusive lock on WAL_READ_LOCK(K). Thus, the value of
61963** aReadMark[K] cannot changed while there is a reader is using that mark
61964** since the reader will be holding a shared lock on WAL_READ_LOCK(K).
61965**
61966** The checkpointer may only transfer frames from WAL to database where
61967** the frame numbers are less than or equal to every aReadMark[] that is
61968** in use (that is, every aReadMark[j] for which there is a corresponding
61969** WAL_READ_LOCK(j)). New readers (usually) pick the aReadMark[] with the
61970** largest value and will increase an unused aReadMark[] to mxFrame if there
61971** is not already an aReadMark[] equal to mxFrame. The exception to the
61972** previous sentence is when nBackfill equals mxFrame (meaning that everything
61973** in the WAL has been backfilled into the database) then new readers
61974** will choose aReadMark[0] which has value 0 and hence such reader will
61975** get all their all content directly from the database file and ignore
61976** the WAL.
61977**
61978** Writers normally append new frames to the end of the WAL. However,
61979** if nBackfill equals mxFrame (meaning that all WAL content has been
61980** written back into the database) and if no readers are using the WAL
61981** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then
61982** the writer will first "reset" the WAL back to the beginning and start
61983** writing new content beginning at frame 1.
61984**
61985** We assume that 32-bit loads are atomic and so no locks are needed in
61986** order to read from any aReadMark[] entries.
61987*/
61988struct WalCkptInfo {
61989 u32 nBackfill; /* Number of WAL frames backfilled into DB */
61990 u32 aReadMark[WAL_NREADER(8 -3)]; /* Reader marks */
61991 u8 aLock[SQLITE_SHM_NLOCK8]; /* Reserved space for locks */
61992 u32 nBackfillAttempted; /* WAL frames perhaps written, or maybe not */
61993 u32 notUsed0; /* Available for future enhancements */
61994};
61995#define READMARK_NOT_USED0xffffffff 0xffffffff
61996
61997/*
61998** This is a schematic view of the complete 136-byte header of the
61999** wal-index file (also known as the -shm file):
62000**
62001** +-----------------------------+
62002** 0: | iVersion | \
62003** +-----------------------------+ |
62004** 4: | (unused padding) | |
62005** +-----------------------------+ |
62006** 8: | iChange | |
62007** +-------+-------+-------------+ |
62008** 12: | bInit | bBig | szPage | |
62009** +-------+-------+-------------+ |
62010** 16: | mxFrame | | First copy of the
62011** +-----------------------------+ | WalIndexHdr object
62012** 20: | nPage | |
62013** +-----------------------------+ |
62014** 24: | aFrameCksum | |
62015** | | |
62016** +-----------------------------+ |
62017** 32: | aSalt | |
62018** | | |
62019** +-----------------------------+ |
62020** 40: | aCksum | |
62021** | | /
62022** +-----------------------------+
62023** 48: | iVersion | \
62024** +-----------------------------+ |
62025** 52: | (unused padding) | |
62026** +-----------------------------+ |
62027** 56: | iChange | |
62028** +-------+-------+-------------+ |
62029** 60: | bInit | bBig | szPage | |
62030** +-------+-------+-------------+ | Second copy of the
62031** 64: | mxFrame | | WalIndexHdr
62032** +-----------------------------+ |
62033** 68: | nPage | |
62034** +-----------------------------+ |
62035** 72: | aFrameCksum | |
62036** | | |
62037** +-----------------------------+ |
62038** 80: | aSalt | |
62039** | | |
62040** +-----------------------------+ |
62041** 88: | aCksum | |
62042** | | /
62043** +-----------------------------+
62044** 96: | nBackfill |
62045** +-----------------------------+
62046** 100: | 5 read marks |
62047** | |
62048** | |
62049** | |
62050** | |
62051** +-------+-------+------+------+
62052** 120: | Write | Ckpt | Rcvr | Rd0 | \
62053** +-------+-------+------+------+ ) 8 lock bytes
62054** | Read1 | Read2 | Rd3 | Rd4 | /
62055** +-------+-------+------+------+
62056** 128: | nBackfillAttempted |
62057** +-----------------------------+
62058** 132: | (unused padding) |
62059** +-----------------------------+
62060*/
62061
62062/* A block of WALINDEX_LOCK_RESERVED bytes beginning at
62063** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems
62064** only support mandatory file-locks, we do not read or write data
62065** from the region of the file on which locks are applied.
62066*/
62067#define WALINDEX_LOCK_OFFSET(sizeof(WalIndexHdr)*2+__builtin_offsetof(WalCkptInfo, aLock)
)
(sizeof(WalIndexHdr)*2+offsetof(WalCkptInfo,aLock)__builtin_offsetof(WalCkptInfo, aLock))
62068#define WALINDEX_HDR_SIZE(sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo)) (sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))
62069
62070/* Size of header before each frame in wal */
62071#define WAL_FRAME_HDRSIZE24 24
62072
62073/* Size of write ahead log header, including checksum. */
62074#define WAL_HDRSIZE32 32
62075
62076/* WAL magic value. Either this value, or the same value with the least
62077** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
62078** big-endian format in the first 4 bytes of a WAL file.
62079**
62080** If the LSB is set, then the checksums for each frame within the WAL
62081** file are calculated by treating all data as an array of 32-bit
62082** big-endian words. Otherwise, they are calculated by interpreting
62083** all data as 32-bit little-endian words.
62084*/
62085#define WAL_MAGIC0x377f0682 0x377f0682
62086
62087/*
62088** Return the offset of frame iFrame in the write-ahead log file,
62089** assuming a database page size of szPage bytes. The offset returned
62090** is to the start of the write-ahead log frame-header.
62091*/
62092#define walFrameOffset(iFrame, szPage)( 32 + ((iFrame)-1)*(i64)((szPage)+24) ) ( \
62093 WAL_HDRSIZE32 + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE24) \
62094)
62095
62096/*
62097** An open write-ahead log file is represented by an instance of the
62098** following object.
62099*/
62100struct Wal {
62101 sqlite3_vfs *pVfs; /* The VFS used to create pDbFd */
62102 sqlite3_file *pDbFd; /* File handle for the database file */
62103 sqlite3_file *pWalFd; /* File handle for WAL file */
62104 u32 iCallback; /* Value to pass to log callback (or 0) */
62105 i64 mxWalSize; /* Truncate WAL to this size upon reset */
62106 int nWiData; /* Size of array apWiData */
62107 int szFirstBlock; /* Size of first block written to WAL file */
62108 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
62109 u32 szPage; /* Database page size */
62110 i16 readLock; /* Which read lock is being held. -1 for none */
62111 u8 syncFlags; /* Flags to use to sync header writes */
62112 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
62113 u8 writeLock; /* True if in a write transaction */
62114 u8 ckptLock; /* True if holding a checkpoint lock */
62115 u8 readOnly; /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */
62116 u8 truncateOnCommit; /* True to truncate WAL file on commit */
62117 u8 syncHeader; /* Fsync the WAL header if true */
62118 u8 padToSectorBoundary; /* Pad transactions out to the next sector */
62119 u8 bShmUnreliable; /* SHM content is read-only and unreliable */
62120 WalIndexHdr hdr; /* Wal-index header for current transaction */
62121 u32 minFrame; /* Ignore wal frames before this one */
62122 u32 iReCksum; /* On commit, recalculate checksums from here */
62123 const char *zWalName; /* Name of WAL file */
62124 u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
62125#ifdef SQLITE_DEBUG
62126 u8 lockError; /* True if a locking error has occurred */
62127#endif
62128#ifdef SQLITE_ENABLE_SNAPSHOT1
62129 WalIndexHdr *pSnapshot; /* Start transaction here if not NULL */
62130#endif
62131#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
62132 sqlite3 *db;
62133#endif
62134};
62135
62136/*
62137** Candidate values for Wal.exclusiveMode.
62138*/
62139#define WAL_NORMAL_MODE0 0
62140#define WAL_EXCLUSIVE_MODE1 1
62141#define WAL_HEAPMEMORY_MODE2 2
62142
62143/*
62144** Possible values for WAL.readOnly
62145*/
62146#define WAL_RDWR0 0 /* Normal read/write connection */
62147#define WAL_RDONLY1 1 /* The WAL file is readonly */
62148#define WAL_SHM_RDONLY2 2 /* The SHM file is readonly */
62149
62150/*
62151** Each page of the wal-index mapping contains a hash-table made up of
62152** an array of HASHTABLE_NSLOT elements of the following type.
62153*/
62154typedef u16 ht_slot;
62155
62156/*
62157** This structure is used to implement an iterator that loops through
62158** all frames in the WAL in database page order. Where two or more frames
62159** correspond to the same database page, the iterator visits only the
62160** frame most recently written to the WAL (in other words, the frame with
62161** the largest index).
62162**
62163** The internals of this structure are only accessed by:
62164**
62165** walIteratorInit() - Create a new iterator,
62166** walIteratorNext() - Step an iterator,
62167** walIteratorFree() - Free an iterator.
62168**
62169** This functionality is used by the checkpoint code (see walCheckpoint()).
62170*/
62171struct WalIterator {
62172 u32 iPrior; /* Last result returned from the iterator */
62173 int nSegment; /* Number of entries in aSegment[] */
62174 struct WalSegment {
62175 int iNext; /* Next slot in aIndex[] not yet returned */
62176 ht_slot *aIndex; /* i0, i1, i2... such that aPgno[iN] ascend */
62177 u32 *aPgno; /* Array of page numbers. */
62178 int nEntry; /* Nr. of entries in aPgno[] and aIndex[] */
62179 int iZero; /* Frame number associated with aPgno[0] */
62180 } aSegment[1]; /* One for every 32KB page in the wal-index */
62181};
62182
62183/*
62184** Define the parameters of the hash tables in the wal-index file. There
62185** is a hash-table following every HASHTABLE_NPAGE page numbers in the
62186** wal-index.
62187**
62188** Changing any of these constants will alter the wal-index format and
62189** create incompatibilities.
62190*/
62191#define HASHTABLE_NPAGE4096 4096 /* Must be power of 2 */
62192#define HASHTABLE_HASH_1383 383 /* Should be prime */
62193#define HASHTABLE_NSLOT(4096*2) (HASHTABLE_NPAGE4096*2) /* Must be a power of 2 */
62194
62195/*
62196** The block of page numbers associated with the first hash-table in a
62197** wal-index is smaller than usual. This is so that there is a complete
62198** hash-table on each aligned 32KB page of the wal-index.
62199*/
62200#define HASHTABLE_NPAGE_ONE(4096 - ((sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32
)))
(HASHTABLE_NPAGE4096 - (WALINDEX_HDR_SIZE(sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32)))
62201
62202/* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
62203#define WALINDEX_PGSZ( sizeof(ht_slot)*(4096*2) + 4096*sizeof(u32) ) ( \
62204 sizeof(ht_slot)*HASHTABLE_NSLOT(4096*2) + HASHTABLE_NPAGE4096*sizeof(u32) \
62205)
62206
62207/*
62208** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
62209** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
62210** numbered from zero.
62211**
62212** If the wal-index is currently smaller the iPage pages then the size
62213** of the wal-index might be increased, but only if it is safe to do
62214** so. It is safe to enlarge the wal-index if pWal->writeLock is true
62215** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
62216**
62217** Three possible result scenarios:
62218**
62219** (1) rc==SQLITE_OK and *ppPage==Requested-Wal-Index-Page
62220** (2) rc>=SQLITE_ERROR and *ppPage==NULL
62221** (3) rc==SQLITE_OK and *ppPage==NULL // only if iPage==0
62222**
62223** Scenario (3) can only occur when pWal->writeLock is false and iPage==0
62224*/
62225static SQLITE_NOINLINE__attribute__((noinline)) int walIndexPageRealloc(
62226 Wal *pWal, /* The WAL context */
62227 int iPage, /* The page we seek */
62228 volatile u32 **ppPage /* Write the page pointer here */
62229){
62230 int rc = SQLITE_OK0;
62231
62232 /* Enlarge the pWal->apWiData[] array if required */
62233 if( pWal->nWiData<=iPage ){
62234 sqlite3_int64 nByte = sizeof(u32*)*(iPage+1);
62235 volatile u32 **apNew;
62236 apNew = (volatile u32 **)sqlite3Realloc((void *)pWal->apWiData, nByte);
62237 if( !apNew ){
62238 *ppPage = 0;
62239 return SQLITE_NOMEM_BKPT7;
62240 }
62241 memset((void*)&apNew[pWal->nWiData], 0,
62242 sizeof(u32*)*(iPage+1-pWal->nWiData));
62243 pWal->apWiData = apNew;
62244 pWal->nWiData = iPage+1;
62245 }
62246
62247 /* Request a pointer to the required page from the VFS */
62248 assert( pWal->apWiData[iPage]==0 )((void) (0));
62249 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE2 ){
62250 pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ( sizeof(ht_slot)*(4096*2) + 4096*sizeof(u32) ));
62251 if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT7;
62252 }else{
62253 rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ( sizeof(ht_slot)*(4096*2) + 4096*sizeof(u32) ),
62254 pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
62255 );
62256 assert( pWal->apWiData[iPage]!=0((void) (0))
62257 || rc!=SQLITE_OK((void) (0))
62258 || (pWal->writeLock==0 && iPage==0) )((void) (0));
62259 testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
62260 if( rc==SQLITE_OK0 ){
62261 if( iPage>0 && sqlite3FaultSim(600) ) rc = SQLITE_NOMEM7;
62262 }else if( (rc&0xff)==SQLITE_READONLY8 ){
62263 pWal->readOnly |= WAL_SHM_RDONLY2;
62264 if( rc==SQLITE_READONLY8 ){
62265 rc = SQLITE_OK0;
62266 }
62267 }
62268 }
62269
62270 *ppPage = pWal->apWiData[iPage];
62271 assert( iPage==0 || *ppPage || rc!=SQLITE_OK )((void) (0));
62272 return rc;
62273}
62274static int walIndexPage(
62275 Wal *pWal, /* The WAL context */
62276 int iPage, /* The page we seek */
62277 volatile u32 **ppPage /* Write the page pointer here */
62278){
62279 if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
62280 return walIndexPageRealloc(pWal, iPage, ppPage);
62281 }
62282 return SQLITE_OK0;
62283}
62284
62285/*
62286** Return a pointer to the WalCkptInfo structure in the wal-index.
62287*/
62288static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
62289 assert( pWal->nWiData>0 && pWal->apWiData[0] )((void) (0));
62290 return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
62291}
62292
62293/*
62294** Return a pointer to the WalIndexHdr structure in the wal-index.
62295*/
62296static volatile WalIndexHdr *walIndexHdr(Wal *pWal){
62297 assert( pWal->nWiData>0 && pWal->apWiData[0] )((void) (0));
62298 return (volatile WalIndexHdr*)pWal->apWiData[0];
62299}
62300
62301/*
62302** The argument to this macro must be of type u32. On a little-endian
62303** architecture, it returns the u32 value that results from interpreting
62304** the 4 bytes as a big-endian value. On a big-endian architecture, it
62305** returns the value that would be produced by interpreting the 4 bytes
62306** of the input value as a little-endian integer.
62307*/
62308#define BYTESWAP32(x)( (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<
8) + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>
24) )
( \
62309 (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8) \
62310 + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>24) \
62311)
62312
62313/*
62314** Generate or extend an 8 byte checksum based on the data in
62315** array aByte[] and the initial values of aIn[0] and aIn[1] (or
62316** initial values of 0 and 0 if aIn==NULL).
62317**
62318** The checksum is written back into aOut[] before returning.
62319**
62320** nByte must be a positive multiple of 8.
62321*/
62322static void walChecksumBytes(
62323 int nativeCksum, /* True for native byte-order, false for non-native */
62324 u8 *a, /* Content to be checksummed */
62325 int nByte, /* Bytes of content in a[]. Must be a multiple of 8. */
62326 const u32 *aIn, /* Initial checksum value input */
62327 u32 *aOut /* OUT: Final checksum value output */
62328){
62329 u32 s1, s2;
62330 u32 *aData = (u32 *)a;
62331 u32 *aEnd = (u32 *)&a[nByte];
62332
62333 if( aIn ){
62334 s1 = aIn[0];
62335 s2 = aIn[1];
62336 }else{
62337 s1 = s2 = 0;
62338 }
62339
62340 assert( nByte>=8 )((void) (0));
62341 assert( (nByte&0x00000007)==0 )((void) (0));
62342 assert( nByte<=65536 )((void) (0));
62343
62344 if( nativeCksum ){
62345 do {
62346 s1 += *aData++ + s2;
62347 s2 += *aData++ + s1;
62348 }while( aData<aEnd );
62349 }else{
62350 do {
62351 s1 += BYTESWAP32(aData[0])( (((aData[0])&0x000000FF)<<24) + (((aData[0])&
0x0000FF00)<<8) + (((aData[0])&0x00FF0000)>>8
) + (((aData[0])&0xFF000000)>>24) )
+ s2;
62352 s2 += BYTESWAP32(aData[1])( (((aData[1])&0x000000FF)<<24) + (((aData[1])&
0x0000FF00)<<8) + (((aData[1])&0x00FF0000)>>8
) + (((aData[1])&0xFF000000)>>24) )
+ s1;
62353 aData += 2;
62354 }while( aData<aEnd );
62355 }
62356
62357 aOut[0] = s1;
62358 aOut[1] = s2;
62359}
62360
62361/*
62362** If there is the possibility of concurrent access to the SHM file
62363** from multiple threads and/or processes, then do a memory barrier.
62364*/
62365static void walShmBarrier(Wal *pWal){
62366 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE2 ){
62367 sqlite3OsShmBarrier(pWal->pDbFd);
62368 }
62369}
62370
62371/*
62372** Add the SQLITE_NO_TSAN as part of the return-type of a function
62373** definition as a hint that the function contains constructs that
62374** might give false-positive TSAN warnings.
62375**
62376** See tag-20200519-1.
62377*/
62378#if defined(__clang__1) && !defined(SQLITE_NO_TSAN__attribute__((no_sanitize_thread)))
62379# define SQLITE_NO_TSAN__attribute__((no_sanitize_thread)) __attribute__((no_sanitize_thread))
62380#else
62381# define SQLITE_NO_TSAN__attribute__((no_sanitize_thread))
62382#endif
62383
62384/*
62385** Write the header information in pWal->hdr into the wal-index.
62386**
62387** The checksum on pWal->hdr is updated before it is written.
62388*/
62389static SQLITE_NO_TSAN__attribute__((no_sanitize_thread)) void walIndexWriteHdr(Wal *pWal){
62390 volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
62391 const int nCksum = offsetof(WalIndexHdr, aCksum)__builtin_offsetof(WalIndexHdr, aCksum);
62392
62393 assert( pWal->writeLock )((void) (0));
62394 pWal->hdr.isInit = 1;
62395 pWal->hdr.iVersion = WALINDEX_MAX_VERSION3007000;
62396 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
62397 /* Possible TSAN false-positive. See tag-20200519-1 */
62398 memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
62399 walShmBarrier(pWal);
62400 memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
62401}
62402
62403/*
62404** This function encodes a single frame header and writes it to a buffer
62405** supplied by the caller. A frame-header is made up of a series of
62406** 4-byte big-endian integers, as follows:
62407**
62408** 0: Page number.
62409** 4: For commit records, the size of the database image in pages
62410** after the commit. For all other records, zero.
62411** 8: Salt-1 (copied from the wal-header)
62412** 12: Salt-2 (copied from the wal-header)
62413** 16: Checksum-1.
62414** 20: Checksum-2.
62415*/
62416static void walEncodeFrame(
62417 Wal *pWal, /* The write-ahead log */
62418 u32 iPage, /* Database page number for frame */
62419 u32 nTruncate, /* New db size (or 0 for non-commit frames) */
62420 u8 *aData, /* Pointer to page data */
62421 u8 *aFrame /* OUT: Write encoded frame here */
62422){
62423 int nativeCksum; /* True for native byte-order checksums */
62424 u32 *aCksum = pWal->hdr.aFrameCksum;
62425 assert( WAL_FRAME_HDRSIZE==24 )((void) (0));
62426 sqlite3Put4byte(&aFrame[0], iPage);
62427 sqlite3Put4byte(&aFrame[4], nTruncate);
62428 if( pWal->iReCksum==0 ){
62429 memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
62430
62431 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN0);
62432 walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
62433 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
62434
62435 sqlite3Put4byte(&aFrame[16], aCksum[0]);
62436 sqlite3Put4byte(&aFrame[20], aCksum[1]);
62437 }else{
62438 memset(&aFrame[8], 0, 16);
62439 }
62440}
62441
62442/*
62443** Check to see if the frame with header in aFrame[] and content
62444** in aData[] is valid. If it is a valid frame, fill *piPage and
62445** *pnTruncate and return true. Return if the frame is not valid.
62446*/
62447static int walDecodeFrame(
62448 Wal *pWal, /* The write-ahead log */
62449 u32 *piPage, /* OUT: Database page number for frame */
62450 u32 *pnTruncate, /* OUT: New db size (or 0 if not commit) */
62451 u8 *aData, /* Pointer to page data (for checksum) */
62452 u8 *aFrame /* Frame data */
62453){
62454 int nativeCksum; /* True for native byte-order checksums */
62455 u32 *aCksum = pWal->hdr.aFrameCksum;
62456 u32 pgno; /* Page number of the frame */
62457 assert( WAL_FRAME_HDRSIZE==24 )((void) (0));
62458
62459 /* A frame is only valid if the salt values in the frame-header
62460 ** match the salt values in the wal-header.
62461 */
62462 if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
62463 return 0;
62464 }
62465
62466 /* A frame is only valid if the page number is creater than zero.
62467 */
62468 pgno = sqlite3Get4byte(&aFrame[0]);
62469 if( pgno==0 ){
62470 return 0;
62471 }
62472
62473 /* A frame is only valid if a checksum of the WAL header,
62474 ** all prior frams, the first 16 bytes of this frame-header,
62475 ** and the frame-data matches the checksum in the last 8
62476 ** bytes of this frame-header.
62477 */
62478 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN0);
62479 walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
62480 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
62481 if( aCksum[0]!=sqlite3Get4byte(&aFrame[16])
62482 || aCksum[1]!=sqlite3Get4byte(&aFrame[20])
62483 ){
62484 /* Checksum failed. */
62485 return 0;
62486 }
62487
62488 /* If we reach this point, the frame is valid. Return the page number
62489 ** and the new database size.
62490 */
62491 *piPage = pgno;
62492 *pnTruncate = sqlite3Get4byte(&aFrame[4]);
62493 return 1;
62494}
62495
62496
62497#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
62498/*
62499** Names of locks. This routine is used to provide debugging output and is not
62500** a part of an ordinary build.
62501*/
62502static const char *walLockName(int lockIdx){
62503 if( lockIdx==WAL_WRITE_LOCK0 ){
62504 return "WRITE-LOCK";
62505 }else if( lockIdx==WAL_CKPT_LOCK1 ){
62506 return "CKPT-LOCK";
62507 }else if( lockIdx==WAL_RECOVER_LOCK2 ){
62508 return "RECOVER-LOCK";
62509 }else{
62510 static char zName[15];
62511 sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
62512 lockIdx-WAL_READ_LOCK(0)(3+(0)));
62513 return zName;
62514 }
62515}
62516#endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
62517
62518
62519/*
62520** Set or release locks on the WAL. Locks are either shared or exclusive.
62521** A lock cannot be moved directly between shared and exclusive - it must go
62522** through the unlocked state first.
62523**
62524** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
62525*/
62526static int walLockShared(Wal *pWal, int lockIdx){
62527 int rc;
62528 if( pWal->exclusiveMode ) return SQLITE_OK0;
62529 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
62530 SQLITE_SHM_LOCK2 | SQLITE_SHM_SHARED4);
62531 WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
62532 walLockName(lockIdx), rc ? "failed" : "ok"));
62533 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
62534 return rc;
62535}
62536static void walUnlockShared(Wal *pWal, int lockIdx){
62537 if( pWal->exclusiveMode ) return;
62538 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
62539 SQLITE_SHM_UNLOCK1 | SQLITE_SHM_SHARED4);
62540 WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
62541}
62542static int walLockExclusive(Wal *pWal, int lockIdx, int n){
62543 int rc;
62544 if( pWal->exclusiveMode ) return SQLITE_OK0;
62545 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
62546 SQLITE_SHM_LOCK2 | SQLITE_SHM_EXCLUSIVE8);
62547 WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
62548 walLockName(lockIdx), n, rc ? "failed" : "ok"));
62549 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
62550 return rc;
62551}
62552static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){
62553 if( pWal->exclusiveMode ) return;
62554 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
62555 SQLITE_SHM_UNLOCK1 | SQLITE_SHM_EXCLUSIVE8);
62556 WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
62557 walLockName(lockIdx), n));
62558}
62559
62560/*
62561** Compute a hash on a page number. The resulting hash value must land
62562** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
62563** the hash to the next value in the event of a collision.
62564*/
62565static int walHash(u32 iPage){
62566 assert( iPage>0 )((void) (0));
62567 assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 )((void) (0));
62568 return (iPage*HASHTABLE_HASH_1383) & (HASHTABLE_NSLOT(4096*2)-1);
62569}
62570static int walNextHash(int iPriorHash){
62571 return (iPriorHash+1)&(HASHTABLE_NSLOT(4096*2)-1);
62572}
62573
62574/*
62575** An instance of the WalHashLoc object is used to describe the location
62576** of a page hash table in the wal-index. This becomes the return value
62577** from walHashGet().
62578*/
62579typedef struct WalHashLoc WalHashLoc;
62580struct WalHashLoc {
62581 volatile ht_slot *aHash; /* Start of the wal-index hash table */
62582 volatile u32 *aPgno; /* aPgno[1] is the page of first frame indexed */
62583 u32 iZero; /* One less than the frame number of first indexed*/
62584};
62585
62586/*
62587** Return pointers to the hash table and page number array stored on
62588** page iHash of the wal-index. The wal-index is broken into 32KB pages
62589** numbered starting from 0.
62590**
62591** Set output variable pLoc->aHash to point to the start of the hash table
62592** in the wal-index file. Set pLoc->iZero to one less than the frame
62593** number of the first frame indexed by this hash table. If a
62594** slot in the hash table is set to N, it refers to frame number
62595** (pLoc->iZero+N) in the log.
62596**
62597** Finally, set pLoc->aPgno so that pLoc->aPgno[0] is the page number of the
62598** first frame indexed by the hash table, frame (pLoc->iZero).
62599*/
62600static int walHashGet(
62601 Wal *pWal, /* WAL handle */
62602 int iHash, /* Find the iHash'th table */
62603 WalHashLoc *pLoc /* OUT: Hash table location */
62604){
62605 int rc; /* Return code */
62606
62607 rc = walIndexPage(pWal, iHash, &pLoc->aPgno);
62608 assert( rc==SQLITE_OK || iHash>0 )((void) (0));
62609
62610 if( pLoc->aPgno ){
62611 pLoc->aHash = (volatile ht_slot *)&pLoc->aPgno[HASHTABLE_NPAGE4096];
62612 if( iHash==0 ){
62613 pLoc->aPgno = &pLoc->aPgno[WALINDEX_HDR_SIZE(sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32)];
62614 pLoc->iZero = 0;
62615 }else{
62616 pLoc->iZero = HASHTABLE_NPAGE_ONE(4096 - ((sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32
)))
+ (iHash-1)*HASHTABLE_NPAGE4096;
62617 }
62618 }else if( NEVER(rc==SQLITE_OK)(rc==0) ){
62619 rc = SQLITE_ERROR1;
62620 }
62621 return rc;
62622}
62623
62624/*
62625** Return the number of the wal-index page that contains the hash-table
62626** and page-number array that contain entries corresponding to WAL frame
62627** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
62628** are numbered starting from 0.
62629*/
62630static int walFramePage(u32 iFrame){
62631 int iHash = (iFrame+HASHTABLE_NPAGE4096-HASHTABLE_NPAGE_ONE(4096 - ((sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32
)))
-1) / HASHTABLE_NPAGE4096;
62632 assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE)((void) (0))
62633 && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE)((void) (0))
62634 && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE))((void) (0))
62635 && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)((void) (0))
62636 && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE))((void) (0))
62637 )((void) (0));
62638 assert( iHash>=0 )((void) (0));
62639 return iHash;
62640}
62641
62642/*
62643** Return the page number associated with frame iFrame in this WAL.
62644*/
62645static u32 walFramePgno(Wal *pWal, u32 iFrame){
62646 int iHash = walFramePage(iFrame);
62647 if( iHash==0 ){
62648 return pWal->apWiData[0][WALINDEX_HDR_SIZE(sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32) + iFrame - 1];
62649 }
62650 return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE(4096 - ((sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32
)))
)%HASHTABLE_NPAGE4096];
62651}
62652
62653/*
62654** Remove entries from the hash table that point to WAL slots greater
62655** than pWal->hdr.mxFrame.
62656**
62657** This function is called whenever pWal->hdr.mxFrame is decreased due
62658** to a rollback or savepoint.
62659**
62660** At most only the hash table containing pWal->hdr.mxFrame needs to be
62661** updated. Any later hash tables will be automatically cleared when
62662** pWal->hdr.mxFrame advances to the point where those hash tables are
62663** actually needed.
62664*/
62665static void walCleanupHash(Wal *pWal){
62666 WalHashLoc sLoc; /* Hash table location */
62667 int iLimit = 0; /* Zero values greater than this */
62668 int nByte; /* Number of bytes to zero in aPgno[] */
62669 int i; /* Used to iterate through aHash[] */
62670
62671 assert( pWal->writeLock )((void) (0));
62672 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
62673 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
62674 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
62675
62676 if( pWal->hdr.mxFrame==0 ) return;
62677
62678 /* Obtain pointers to the hash-table and page-number array containing
62679 ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
62680 ** that the page said hash-table and array reside on is already mapped.(1)
62681 */
62682 assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) )((void) (0));
62683 assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] )((void) (0));
62684 i = walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
62685 if( NEVER(i)(i) ) return; /* Defense-in-depth, in case (1) above is wrong */
62686
62687 /* Zero all hash-table entries that correspond to frame numbers greater
62688 ** than pWal->hdr.mxFrame.
62689 */
62690 iLimit = pWal->hdr.mxFrame - sLoc.iZero;
62691 assert( iLimit>0 )((void) (0));
62692 for(i=0; i<HASHTABLE_NSLOT(4096*2); i++){
62693 if( sLoc.aHash[i]>iLimit ){
62694 sLoc.aHash[i] = 0;
62695 }
62696 }
62697
62698 /* Zero the entries in the aPgno array that correspond to frames with
62699 ** frame numbers greater than pWal->hdr.mxFrame.
62700 */
62701 nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit]);
62702 assert( nByte>=0 )((void) (0));
62703 memset((void *)&sLoc.aPgno[iLimit], 0, nByte);
62704
62705#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
62706 /* Verify that the every entry in the mapping region is still reachable
62707 ** via the hash table even after the cleanup.
62708 */
62709 if( iLimit ){
62710 int j; /* Loop counter */
62711 int iKey; /* Hash key */
62712 for(j=0; j<iLimit; j++){
62713 for(iKey=walHash(sLoc.aPgno[j]);sLoc.aHash[iKey];iKey=walNextHash(iKey)){
62714 if( sLoc.aHash[iKey]==j+1 ) break;
62715 }
62716 assert( sLoc.aHash[iKey]==j+1 )((void) (0));
62717 }
62718 }
62719#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
62720}
62721
62722
62723/*
62724** Set an entry in the wal-index that will map database page number
62725** pPage into WAL frame iFrame.
62726*/
62727static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
62728 int rc; /* Return code */
62729 WalHashLoc sLoc; /* Wal-index hash table location */
62730
62731 rc = walHashGet(pWal, walFramePage(iFrame), &sLoc);
62732
62733 /* Assuming the wal-index file was successfully mapped, populate the
62734 ** page number array and hash table entry.
62735 */
62736 if( rc==SQLITE_OK0 ){
62737 int iKey; /* Hash table key */
62738 int idx; /* Value to write to hash-table slot */
62739 int nCollide; /* Number of hash collisions */
62740
62741 idx = iFrame - sLoc.iZero;
62742 assert( idx <= HASHTABLE_NSLOT/2 + 1 )((void) (0));
62743
62744 /* If this is the first entry to be added to this hash-table, zero the
62745 ** entire hash table and aPgno[] array before proceeding.
62746 */
62747 if( idx==1 ){
62748 int nByte = (int)((u8*)&sLoc.aHash[HASHTABLE_NSLOT(4096*2)] - (u8*)sLoc.aPgno);
62749 assert( nByte>=0 )((void) (0));
62750 memset((void*)sLoc.aPgno, 0, nByte);
62751 }
62752
62753 /* If the entry in aPgno[] is already set, then the previous writer
62754 ** must have exited unexpectedly in the middle of a transaction (after
62755 ** writing one or more dirty pages to the WAL to free up memory).
62756 ** Remove the remnants of that writers uncommitted transaction from
62757 ** the hash-table before writing any new entries.
62758 */
62759 if( sLoc.aPgno[idx-1] ){
62760 walCleanupHash(pWal);
62761 assert( !sLoc.aPgno[idx-1] )((void) (0));
62762 }
62763
62764 /* Write the aPgno[] array entry and the hash-table slot. */
62765 nCollide = idx;
62766 for(iKey=walHash(iPage); sLoc.aHash[iKey]; iKey=walNextHash(iKey)){
62767 if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(62767);
62768 }
62769 sLoc.aPgno[idx-1] = iPage;
62770 AtomicStore(&sLoc.aHash[iKey], (ht_slot)idx)__atomic_store_n((&sLoc.aHash[iKey]),((ht_slot)idx),0);
62771
62772#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
62773 /* Verify that the number of entries in the hash table exactly equals
62774 ** the number of entries in the mapping region.
62775 */
62776 {
62777 int i; /* Loop counter */
62778 int nEntry = 0; /* Number of entries in the hash table */
62779 for(i=0; i<HASHTABLE_NSLOT(4096*2); i++){ if( sLoc.aHash[i] ) nEntry++; }
62780 assert( nEntry==idx )((void) (0));
62781 }
62782
62783 /* Verify that the every entry in the mapping region is reachable
62784 ** via the hash table. This turns out to be a really, really expensive
62785 ** thing to check, so only do this occasionally - not on every
62786 ** iteration.
62787 */
62788 if( (idx&0x3ff)==0 ){
62789 int i; /* Loop counter */
62790 for(i=0; i<idx; i++){
62791 for(iKey=walHash(sLoc.aPgno[i]);
62792 sLoc.aHash[iKey];
62793 iKey=walNextHash(iKey)){
62794 if( sLoc.aHash[iKey]==i+1 ) break;
62795 }
62796 assert( sLoc.aHash[iKey]==i+1 )((void) (0));
62797 }
62798 }
62799#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
62800 }
62801
62802 return rc;
62803}
62804
62805
62806/*
62807** Recover the wal-index by reading the write-ahead log file.
62808**
62809** This routine first tries to establish an exclusive lock on the
62810** wal-index to prevent other threads/processes from doing anything
62811** with the WAL or wal-index while recovery is running. The
62812** WAL_RECOVER_LOCK is also held so that other threads will know
62813** that this thread is running recovery. If unable to establish
62814** the necessary locks, this routine returns SQLITE_BUSY.
62815*/
62816static int walIndexRecover(Wal *pWal){
62817 int rc; /* Return Code */
62818 i64 nSize; /* Size of log file */
62819 u32 aFrameCksum[2] = {0, 0};
62820 int iLock; /* Lock offset to lock for checkpoint */
62821
62822 /* Obtain an exclusive lock on all byte in the locking range not already
62823 ** locked by the caller. The caller is guaranteed to have locked the
62824 ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
62825 ** If successful, the same bytes that are locked here are unlocked before
62826 ** this function returns.
62827 */
62828 assert( pWal->ckptLock==1 || pWal->ckptLock==0 )((void) (0));
62829 assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 )((void) (0));
62830 assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE )((void) (0));
62831 assert( pWal->writeLock )((void) (0));
62832 iLock = WAL_ALL_BUT_WRITE1 + pWal->ckptLock;
62833 rc = walLockExclusive(pWal, iLock, WAL_READ_LOCK(0)(3+(0))-iLock);
62834 if( rc ){
62835 return rc;
62836 }
62837
62838 WALTRACE(("WAL%p: recovery begin...\n", pWal));
62839
62840 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
62841
62842 rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
62843 if( rc!=SQLITE_OK0 ){
62844 goto recovery_error;
62845 }
62846
62847 if( nSize>WAL_HDRSIZE32 ){
62848 u8 aBuf[WAL_HDRSIZE32]; /* Buffer to load WAL header into */
62849 u32 *aPrivate = 0; /* Heap copy of *-shm hash being populated */
62850 u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
62851 int szFrame; /* Number of bytes in buffer aFrame[] */
62852 u8 *aData; /* Pointer to data part of aFrame buffer */
62853 int szPage; /* Page size according to the log */
62854 u32 magic; /* Magic value read from WAL header */
62855 u32 version; /* Magic value read from WAL header */
62856 int isValid; /* True if this frame is valid */
62857 u32 iPg; /* Current 32KB wal-index page */
62858 u32 iLastFrame; /* Last frame in wal, based on nSize alone */
62859
62860 /* Read in the WAL header. */
62861 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE32, 0);
62862 if( rc!=SQLITE_OK0 ){
62863 goto recovery_error;
62864 }
62865
62866 /* If the database page size is not a power of two, or is greater than
62867 ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid
62868 ** data. Similarly, if the 'magic' value is invalid, ignore the whole
62869 ** WAL file.
62870 */
62871 magic = sqlite3Get4byte(&aBuf[0]);
62872 szPage = sqlite3Get4byte(&aBuf[8]);
62873 if( (magic&0xFFFFFFFE)!=WAL_MAGIC0x377f0682
62874 || szPage&(szPage-1)
62875 || szPage>SQLITE_MAX_PAGE_SIZE65536
62876 || szPage<512
62877 ){
62878 goto finished;
62879 }
62880 pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
62881 pWal->szPage = szPage;
62882 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
62883 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
62884
62885 /* Verify that the WAL header checksum is correct */
62886 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN0,
62887 aBuf, WAL_HDRSIZE32-2*4, 0, pWal->hdr.aFrameCksum
62888 );
62889 if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
62890 || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
62891 ){
62892 goto finished;
62893 }
62894
62895 /* Verify that the version number on the WAL format is one that
62896 ** are able to understand */
62897 version = sqlite3Get4byte(&aBuf[4]);
62898 if( version!=WAL_MAX_VERSION3007000 ){
62899 rc = SQLITE_CANTOPEN_BKPTsqlite3CantopenError(62899);
62900 goto finished;
62901 }
62902
62903 /* Malloc a buffer to read frames into. */
62904 szFrame = szPage + WAL_FRAME_HDRSIZE24;
62905 aFrame = (u8 *)sqlite3_malloc64(szFrame + WALINDEX_PGSZ( sizeof(ht_slot)*(4096*2) + 4096*sizeof(u32) ));
62906 if( !aFrame ){
62907 rc = SQLITE_NOMEM_BKPT7;
62908 goto recovery_error;
62909 }
62910 aData = &aFrame[WAL_FRAME_HDRSIZE24];
62911 aPrivate = (u32*)&aData[szPage];
62912
62913 /* Read all frames from the log file. */
62914 iLastFrame = (nSize - WAL_HDRSIZE32) / szFrame;
62915 for(iPg=0; iPg<=(u32)walFramePage(iLastFrame); iPg++){
62916 u32 *aShare;
62917 u32 iFrame; /* Index of last frame read */
62918 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))
;
62919 u32 iFirst = 1 + (iPg==0?0:HASHTABLE_NPAGE_ONE(4096 - ((sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32
)))
+(iPg-1)*HASHTABLE_NPAGE4096);
62920 u32 nHdr, nHdr32;
62921 rc = walIndexPage(pWal, iPg, (volatile u32**)&aShare);
62922 assert( aShare!=0 || rc!=SQLITE_OK )((void) (0));
62923 if( aShare==0 ) break;
62924 pWal->apWiData[iPg] = aPrivate;
62925
62926 for(iFrame=iFirst; iFrame<=iLast; iFrame++){
62927 i64 iOffset = walFrameOffset(iFrame, szPage)( 32 + ((iFrame)-1)*(i64)((szPage)+24) );
62928 u32 pgno; /* Database page number for frame */
62929 u32 nTruncate; /* dbsize field from frame header */
62930
62931 /* Read and decode the next log frame. */
62932 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
62933 if( rc!=SQLITE_OK0 ) break;
62934 isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
62935 if( !isValid ) break;
62936 rc = walIndexAppend(pWal, iFrame, pgno);
62937 if( NEVER(rc!=SQLITE_OK)(rc!=0) ) break;
62938
62939 /* If nTruncate is non-zero, this is a commit record. */
62940 if( nTruncate ){
62941 pWal->hdr.mxFrame = iFrame;
62942 pWal->hdr.nPage = nTruncate;
62943 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
62944 testcase( szPage<=32768 );
62945 testcase( szPage>=65536 );
62946 aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
62947 aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
62948 }
62949 }
62950 pWal->apWiData[iPg] = aShare;
62951 nHdr = (iPg==0 ? WALINDEX_HDR_SIZE(sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo)) : 0);
62952 nHdr32 = nHdr / sizeof(u32);
62953#ifndef SQLITE_SAFER_WALINDEX_RECOVERY
62954 /* Memcpy() should work fine here, on all reasonable implementations.
62955 ** Technically, memcpy() might change the destination to some
62956 ** intermediate value before setting to the final value, and that might
62957 ** cause a concurrent reader to malfunction. Memcpy() is allowed to
62958 ** do that, according to the spec, but no memcpy() implementation that
62959 ** we know of actually does that, which is why we say that memcpy()
62960 ** is safe for this. Memcpy() is certainly a lot faster.
62961 */
62962 memcpy(&aShare[nHdr32], &aPrivate[nHdr32], WALINDEX_PGSZ( sizeof(ht_slot)*(4096*2) + 4096*sizeof(u32) )-nHdr);
62963#else
62964 /* In the event that some platform is found for which memcpy()
62965 ** changes the destination to some intermediate value before
62966 ** setting the final value, this alternative copy routine is
62967 ** provided.
62968 */
62969 {
62970 int i;
62971 for(i=nHdr32; i<WALINDEX_PGSZ( sizeof(ht_slot)*(4096*2) + 4096*sizeof(u32) )/sizeof(u32); i++){
62972 if( aShare[i]!=aPrivate[i] ){
62973 /* Atomic memory operations are not required here because if
62974 ** the value needs to be changed, that means it is not being
62975 ** accessed concurrently. */
62976 aShare[i] = aPrivate[i];
62977 }
62978 }
62979 }
62980#endif
62981 if( iFrame<=iLast ) break;
62982 }
62983
62984 sqlite3_free(aFrame);
62985 }
62986
62987finished:
62988 if( rc==SQLITE_OK0 ){
62989 volatile WalCkptInfo *pInfo;
62990 int i;
62991 pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
62992 pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
62993 walIndexWriteHdr(pWal);
62994
62995 /* Reset the checkpoint-header. This is safe because this thread is
62996 ** currently holding locks that exclude all other writers and
62997 ** checkpointers. Then set the values of read-mark slots 1 through N.
62998 */
62999 pInfo = walCkptInfo(pWal);
63000 pInfo->nBackfill = 0;
63001 pInfo->nBackfillAttempted = pWal->hdr.mxFrame;
63002 pInfo->aReadMark[0] = 0;
63003 for(i=1; i<WAL_NREADER(8 -3); i++){
63004 rc = walLockExclusive(pWal, WAL_READ_LOCK(i)(3+(i)), 1);
63005 if( rc==SQLITE_OK0 ){
63006 if( i==1 && pWal->hdr.mxFrame ){
63007 pInfo->aReadMark[i] = pWal->hdr.mxFrame;
63008 }else{
63009 pInfo->aReadMark[i] = READMARK_NOT_USED0xffffffff;
63010 }
63011 walUnlockExclusive(pWal, WAL_READ_LOCK(i)(3+(i)), 1);
63012 }else if( rc!=SQLITE_BUSY5 ){
63013 goto recovery_error;
63014 }
63015 }
63016
63017 /* If more than one frame was recovered from the log file, report an
63018 ** event via sqlite3_log(). This is to help with identifying performance
63019 ** problems caused by applications routinely shutting down without
63020 ** checkpointing the log file.
63021 */
63022 if( pWal->hdr.nPage ){
63023 sqlite3_log(SQLITE_NOTICE_RECOVER_WAL(27 | (1<<8)),
63024 "recovered %d frames from WAL file %s",
63025 pWal->hdr.mxFrame, pWal->zWalName
63026 );
63027 }
63028 }
63029
63030recovery_error:
63031 WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
63032 walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)(3+(0))-iLock);
63033 return rc;
63034}
63035
63036/*
63037** Close an open wal-index.
63038*/
63039static void walIndexClose(Wal *pWal, int isDelete){
63040 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE2 || pWal->bShmUnreliable ){
63041 int i;
63042 for(i=0; i<pWal->nWiData; i++){
63043 sqlite3_free((void *)pWal->apWiData[i]);
63044 pWal->apWiData[i] = 0;
63045 }
63046 }
63047 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE2 ){
63048 sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
63049 }
63050}
63051
63052/*
63053** Open a connection to the WAL file zWalName. The database file must
63054** already be opened on connection pDbFd. The buffer that zWalName points
63055** to must remain valid for the lifetime of the returned Wal* handle.
63056**
63057** A SHARED lock should be held on the database file when this function
63058** is called. The purpose of this SHARED lock is to prevent any other
63059** client from unlinking the WAL or wal-index file. If another process
63060** were to do this just after this client opened one of these files, the
63061** system would be badly broken.
63062**
63063** If the log file is successfully opened, SQLITE_OK is returned and
63064** *ppWal is set to point to a new WAL handle. If an error occurs,
63065** an SQLite error code is returned and *ppWal is left unmodified.
63066*/
63067SQLITE_PRIVATEstatic int sqlite3WalOpen(
63068 sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
63069 sqlite3_file *pDbFd, /* The open database file */
63070 const char *zWalName, /* Name of the WAL file */
63071 int bNoShm, /* True to run in heap-memory mode */
63072 i64 mxWalSize, /* Truncate WAL to this size on reset */
63073 Wal **ppWal /* OUT: Allocated Wal handle */
63074){
63075 int rc; /* Return Code */
63076 Wal *pRet; /* Object to allocate and return */
63077 int flags; /* Flags passed to OsOpen() */
63078
63079 assert( zWalName && zWalName[0] )((void) (0));
63080 assert( pDbFd )((void) (0));
63081
63082 /* Verify the values of various constants. Any changes to the values
63083 ** of these constants would result in an incompatible on-disk format
63084 ** for the -shm file. Any change that causes one of these asserts to
63085 ** fail is a backward compatibility problem, even if the change otherwise
63086 ** works.
63087 **
63088 ** This table also serves as a helpful cross-reference when trying to
63089 ** interpret hex dumps of the -shm file.
63090 */
63091 assert( 48 == sizeof(WalIndexHdr) )((void) (0));
63092 assert( 40 == sizeof(WalCkptInfo) )((void) (0));
63093 assert( 120 == WALINDEX_LOCK_OFFSET )((void) (0));
63094 assert( 136 == WALINDEX_HDR_SIZE )((void) (0));
63095 assert( 4096 == HASHTABLE_NPAGE )((void) (0));
63096 assert( 4062 == HASHTABLE_NPAGE_ONE )((void) (0));
63097 assert( 8192 == HASHTABLE_NSLOT )((void) (0));
63098 assert( 383 == HASHTABLE_HASH_1 )((void) (0));
63099 assert( 32768 == WALINDEX_PGSZ )((void) (0));
63100 assert( 8 == SQLITE_SHM_NLOCK )((void) (0));
63101 assert( 5 == WAL_NREADER )((void) (0));
63102 assert( 24 == WAL_FRAME_HDRSIZE )((void) (0));
63103 assert( 32 == WAL_HDRSIZE )((void) (0));
63104 assert( 120 == WALINDEX_LOCK_OFFSET + WAL_WRITE_LOCK )((void) (0));
63105 assert( 121 == WALINDEX_LOCK_OFFSET + WAL_CKPT_LOCK )((void) (0));
63106 assert( 122 == WALINDEX_LOCK_OFFSET + WAL_RECOVER_LOCK )((void) (0));
63107 assert( 123 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(0) )((void) (0));
63108 assert( 124 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(1) )((void) (0));
63109 assert( 125 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(2) )((void) (0));
63110 assert( 126 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(3) )((void) (0));
63111 assert( 127 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(4) )((void) (0));
63112
63113 /* In the amalgamation, the os_unix.c and os_win.c source files come before
63114 ** this source file. Verify that the #defines of the locking byte offsets
63115 ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
63116 ** For that matter, if the lock offset ever changes from its initial design
63117 ** value of 120, we need to know that so there is an assert() to check it.
63118 */
63119#ifdef WIN_SHM_BASE
63120 assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET )((void) (0));
63121#endif
63122#ifdef UNIX_SHM_BASE((22+8)*4)
63123 assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET )((void) (0));
63124#endif
63125
63126
63127 /* Allocate an instance of struct Wal to return. */
63128 *ppWal = 0;
63129 pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
63130 if( !pRet ){
63131 return SQLITE_NOMEM_BKPT7;
63132 }
63133
63134 pRet->pVfs = pVfs;
63135 pRet->pWalFd = (sqlite3_file *)&pRet[1];
63136 pRet->pDbFd = pDbFd;
63137 pRet->readLock = -1;
63138 pRet->mxWalSize = mxWalSize;
63139 pRet->zWalName = zWalName;
63140 pRet->syncHeader = 1;
63141 pRet->padToSectorBoundary = 1;
63142 pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE2: WAL_NORMAL_MODE0);
63143
63144 /* Open file handle on the write-ahead log file. */
63145 flags = (SQLITE_OPEN_READWRITE0x00000002|SQLITE_OPEN_CREATE0x00000004|SQLITE_OPEN_WAL0x00080000);
63146 rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
63147 if( rc==SQLITE_OK0 && flags&SQLITE_OPEN_READONLY0x00000001 ){
63148 pRet->readOnly = WAL_RDONLY1;
63149 }
63150
63151 if( rc!=SQLITE_OK0 ){
63152 walIndexClose(pRet, 0);
63153 sqlite3OsClose(pRet->pWalFd);
63154 sqlite3_free(pRet);
63155 }else{
63156 int iDC = sqlite3OsDeviceCharacteristics(pDbFd);
63157 if( iDC & SQLITE_IOCAP_SEQUENTIAL0x00000400 ){ pRet->syncHeader = 0; }
63158 if( iDC & SQLITE_IOCAP_POWERSAFE_OVERWRITE0x00001000 ){
63159 pRet->padToSectorBoundary = 0;
63160 }
63161 *ppWal = pRet;
63162 WALTRACE(("WAL%d: opened\n", pRet));
63163 }
63164 return rc;
63165}
63166
63167/*
63168** Change the size to which the WAL file is trucated on each reset.
63169*/
63170SQLITE_PRIVATEstatic void sqlite3WalLimit(Wal *pWal, i64 iLimit){
63171 if( pWal ) pWal->mxWalSize = iLimit;
63172}
63173
63174/*
63175** Find the smallest page number out of all pages held in the WAL that
63176** has not been returned by any prior invocation of this method on the
63177** same WalIterator object. Write into *piFrame the frame index where
63178** that page was last written into the WAL. Write into *piPage the page
63179** number.
63180**
63181** Return 0 on success. If there are no pages in the WAL with a page
63182** number larger than *piPage, then return 1.
63183*/
63184static int walIteratorNext(
63185 WalIterator *p, /* Iterator */
63186 u32 *piPage, /* OUT: The page number of the next page */
63187 u32 *piFrame /* OUT: Wal frame index of next page */
63188){
63189 u32 iMin; /* Result pgno must be greater than iMin */
63190 u32 iRet = 0xFFFFFFFF; /* 0xffffffff is never a valid page number */
63191 int i; /* For looping through segments */
63192
63193 iMin = p->iPrior;
63194 assert( iMin<0xffffffff )((void) (0));
63195 for(i=p->nSegment-1; i>=0; i--){
63196 struct WalSegment *pSegment = &p->aSegment[i];
63197 while( pSegment->iNext<pSegment->nEntry ){
63198 u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
63199 if( iPg>iMin ){
63200 if( iPg<iRet ){
63201 iRet = iPg;
63202 *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
63203 }
63204 break;
63205 }
63206 pSegment->iNext++;
63207 }
63208 }
63209
63210 *piPage = p->iPrior = iRet;
63211 return (iRet==0xFFFFFFFF);
63212}
63213
63214/*
63215** This function merges two sorted lists into a single sorted list.
63216**
63217** aLeft[] and aRight[] are arrays of indices. The sort key is
63218** aContent[aLeft[]] and aContent[aRight[]]. Upon entry, the following
63219** is guaranteed for all J<K:
63220**
63221** aContent[aLeft[J]] < aContent[aLeft[K]]
63222** aContent[aRight[J]] < aContent[aRight[K]]
63223**
63224** This routine overwrites aRight[] with a new (probably longer) sequence
63225** of indices such that the aRight[] contains every index that appears in
63226** either aLeft[] or the old aRight[] and such that the second condition
63227** above is still met.
63228**
63229** The aContent[aLeft[X]] values will be unique for all X. And the
63230** aContent[aRight[X]] values will be unique too. But there might be
63231** one or more combinations of X and Y such that
63232**
63233** aLeft[X]!=aRight[Y] && aContent[aLeft[X]] == aContent[aRight[Y]]
63234**
63235** When that happens, omit the aLeft[X] and use the aRight[Y] index.
63236*/
63237static void walMerge(
63238 const u32 *aContent, /* Pages in wal - keys for the sort */
63239 ht_slot *aLeft, /* IN: Left hand input list */
63240 int nLeft, /* IN: Elements in array *paLeft */
63241 ht_slot **paRight, /* IN/OUT: Right hand input list */
63242 int *pnRight, /* IN/OUT: Elements in *paRight */
63243 ht_slot *aTmp /* Temporary buffer */
63244){
63245 int iLeft = 0; /* Current index in aLeft */
63246 int iRight = 0; /* Current index in aRight */
63247 int iOut = 0; /* Current index in output buffer */
63248 int nRight = *pnRight;
63249 ht_slot *aRight = *paRight;
63250
63251 assert( nLeft>0 && nRight>0 )((void) (0));
63252 while( iRight<nRight || iLeft<nLeft ){
63253 ht_slot logpage;
63254 Pgno dbpage;
63255
63256 if( (iLeft<nLeft)
63257 && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
63258 ){
63259 logpage = aLeft[iLeft++];
63260 }else{
63261 logpage = aRight[iRight++];
63262 }
63263 dbpage = aContent[logpage];
63264
63265 aTmp[iOut++] = logpage;
63266 if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
63267
63268 assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage )((void) (0));
63269 assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage )((void) (0));
63270 }
63271
63272 *paRight = aLeft;
63273 *pnRight = iOut;
63274 memcpy(aLeft, aTmp, sizeof(aTmp[0])*iOut);
63275}
63276
63277/*
63278** Sort the elements in list aList using aContent[] as the sort key.
63279** Remove elements with duplicate keys, preferring to keep the
63280** larger aList[] values.
63281**
63282** The aList[] entries are indices into aContent[]. The values in
63283** aList[] are to be sorted so that for all J<K:
63284**
63285** aContent[aList[J]] < aContent[aList[K]]
63286**
63287** For any X and Y such that
63288**
63289** aContent[aList[X]] == aContent[aList[Y]]
63290**
63291** Keep the larger of the two values aList[X] and aList[Y] and discard
63292** the smaller.
63293*/
63294static void walMergesort(
63295 const u32 *aContent, /* Pages in wal */
63296 ht_slot *aBuffer, /* Buffer of at least *pnList items to use */
63297 ht_slot *aList, /* IN/OUT: List to sort */
63298 int *pnList /* IN/OUT: Number of elements in aList[] */
63299){
63300 struct Sublist {
63301 int nList; /* Number of elements in aList */
63302 ht_slot *aList; /* Pointer to sub-list content */
63303 };
63304
63305 const int nList = *pnList; /* Size of input list */
63306 int nMerge = 0; /* Number of elements in list aMerge */
63307 ht_slot *aMerge = 0; /* List to be merged */
63308 int iList; /* Index into input list */
63309 u32 iSub = 0; /* Index into aSub array */
63310 struct Sublist aSub[13]; /* Array of sub-lists */
63311
63312 memset(aSub, 0, sizeof(aSub));
63313 assert( nList<=HASHTABLE_NPAGE && nList>0 )((void) (0));
63314 assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) )((void) (0));
63315
63316 for(iList=0; iList<nList; iList++){
63317 nMerge = 1;
63318 aMerge = &aList[iList];
63319 for(iSub=0; iList & (1<<iSub); iSub++){
63320 struct Sublist *p;
63321 assert( iSub<ArraySize(aSub) )((void) (0));
63322 p = &aSub[iSub];
63323 assert( p->aList && p->nList<=(1<<iSub) )((void) (0));
63324 assert( p->aList==&aList[iList&~((2<<iSub)-1)] )((void) (0));
63325 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
63326 }
63327 aSub[iSub].aList = aMerge;
63328 aSub[iSub].nList = nMerge;
63329 }
63330
63331 for(iSub++; iSub<ArraySize(aSub)((int)(sizeof(aSub)/sizeof(aSub[0]))); iSub++){
63332 if( nList & (1<<iSub) ){
63333 struct Sublist *p;
63334 assert( iSub<ArraySize(aSub) )((void) (0));
63335 p = &aSub[iSub];
63336 assert( p->nList<=(1<<iSub) )((void) (0));
63337 assert( p->aList==&aList[nList&~((2<<iSub)-1)] )((void) (0));
63338 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
63339 }
63340 }
63341 assert( aMerge==aList )((void) (0));
63342 *pnList = nMerge;
63343
63344#ifdef SQLITE_DEBUG
63345 {
63346 int i;
63347 for(i=1; i<*pnList; i++){
63348 assert( aContent[aList[i]] > aContent[aList[i-1]] )((void) (0));
63349 }
63350 }
63351#endif
63352}
63353
63354/*
63355** Free an iterator allocated by walIteratorInit().
63356*/
63357static void walIteratorFree(WalIterator *p){
63358 sqlite3_free(p);
63359}
63360
63361/*
63362** Construct a WalInterator object that can be used to loop over all
63363** pages in the WAL following frame nBackfill in ascending order. Frames
63364** nBackfill or earlier may be included - excluding them is an optimization
63365** only. The caller must hold the checkpoint lock.
63366**
63367** On success, make *pp point to the newly allocated WalInterator object
63368** return SQLITE_OK. Otherwise, return an error code. If this routine
63369** returns an error, the value of *pp is undefined.
63370**
63371** The calling routine should invoke walIteratorFree() to destroy the
63372** WalIterator object when it has finished with it.
63373*/
63374static int walIteratorInit(Wal *pWal, u32 nBackfill, WalIterator **pp){
63375 WalIterator *p; /* Return value */
63376 int nSegment; /* Number of segments to merge */
63377 u32 iLast; /* Last frame in log */
63378 sqlite3_int64 nByte; /* Number of bytes to allocate */
63379 int i; /* Iterator variable */
63380 ht_slot *aTmp; /* Temp space used by merge-sort */
63381 int rc = SQLITE_OK0; /* Return Code */
63382
63383 /* This routine only runs while holding the checkpoint lock. And
63384 ** it only runs if there is actually content in the log (mxFrame>0).
63385 */
63386 assert( pWal->ckptLock && pWal->hdr.mxFrame>0 )((void) (0));
63387 iLast = pWal->hdr.mxFrame;
63388
63389 /* Allocate space for the WalIterator object. */
63390 nSegment = walFramePage(iLast) + 1;
63391 nByte = sizeof(WalIterator)
63392 + (nSegment-1)*sizeof(struct WalSegment)
63393 + iLast*sizeof(ht_slot);
63394 p = (WalIterator *)sqlite3_malloc64(nByte);
63395 if( !p ){
63396 return SQLITE_NOMEM_BKPT7;
63397 }
63398 memset(p, 0, nByte);
63399 p->nSegment = nSegment;
63400
63401 /* Allocate temporary space used by the merge-sort routine. This block
63402 ** of memory will be freed before this function returns.
63403 */
63404 aTmp = (ht_slot *)sqlite3_malloc64(
63405 sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE4096?HASHTABLE_NPAGE4096:iLast)
63406 );
63407 if( !aTmp ){
63408 rc = SQLITE_NOMEM_BKPT7;
63409 }
63410
63411 for(i=walFramePage(nBackfill+1); rc==SQLITE_OK0 && i<nSegment; i++){
63412 WalHashLoc sLoc;
63413
63414 rc = walHashGet(pWal, i, &sLoc);
63415 if( rc==SQLITE_OK0 ){
63416 int j; /* Counter variable */
63417 int nEntry; /* Number of entries in this segment */
63418 ht_slot *aIndex; /* Sorted index for this segment */
63419
63420 if( (i+1)==nSegment ){
63421 nEntry = (int)(iLast - sLoc.iZero);
63422 }else{
63423 nEntry = (int)((u32*)sLoc.aHash - (u32*)sLoc.aPgno);
63424 }
63425 aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[sLoc.iZero];
63426 sLoc.iZero++;
63427
63428 for(j=0; j<nEntry; j++){
63429 aIndex[j] = (ht_slot)j;
63430 }
63431 walMergesort((u32 *)sLoc.aPgno, aTmp, aIndex, &nEntry);
63432 p->aSegment[i].iZero = sLoc.iZero;
63433 p->aSegment[i].nEntry = nEntry;
63434 p->aSegment[i].aIndex = aIndex;
63435 p->aSegment[i].aPgno = (u32 *)sLoc.aPgno;
63436 }
63437 }
63438 sqlite3_free(aTmp);
63439
63440 if( rc!=SQLITE_OK0 ){
63441 walIteratorFree(p);
63442 p = 0;
63443 }
63444 *pp = p;
63445 return rc;
63446}
63447
63448#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
63449/*
63450** Attempt to enable blocking locks. Blocking locks are enabled only if (a)
63451** they are supported by the VFS, and (b) the database handle is configured
63452** with a busy-timeout. Return 1 if blocking locks are successfully enabled,
63453** or 0 otherwise.
63454*/
63455static int walEnableBlocking(Wal *pWal)0{
63456 int res = 0;
63457 if( pWal->db ){
63458 int tmout = pWal->db->busyTimeout;
63459 if( tmout ){
63460 int rc;
63461 rc = sqlite3OsFileControl(
63462 pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT34, (void*)&tmout
63463 );
63464 res = (rc==SQLITE_OK0);
63465 }
63466 }
63467 return res;
63468}
63469
63470/*
63471** Disable blocking locks.
63472*/
63473static void walDisableBlocking(Wal *pWal){
63474 int tmout = 0;
63475 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT34, (void*)&tmout);
63476}
63477
63478/*
63479** If parameter bLock is true, attempt to enable blocking locks, take
63480** the WRITER lock, and then disable blocking locks. If blocking locks
63481** cannot be enabled, no attempt to obtain the WRITER lock is made. Return
63482** an SQLite error code if an error occurs, or SQLITE_OK otherwise. It is not
63483** an error if blocking locks can not be enabled.
63484**
63485** If the bLock parameter is false and the WRITER lock is held, release it.
63486*/
63487SQLITE_PRIVATEstatic int sqlite3WalWriteLock(Wal *pWal, int bLock){
63488 int rc = SQLITE_OK0;
63489 assert( pWal->readLock<0 || bLock==0 )((void) (0));
63490 if( bLock ){
63491 assert( pWal->db )((void) (0));
63492 if( walEnableBlocking(pWal)0 ){
63493 rc = walLockExclusive(pWal, WAL_WRITE_LOCK0, 1);
63494 if( rc==SQLITE_OK0 ){
63495 pWal->writeLock = 1;
63496 }
63497 walDisableBlocking(pWal);
63498 }
63499 }else if( pWal->writeLock ){
63500 walUnlockExclusive(pWal, WAL_WRITE_LOCK0, 1);
63501 pWal->writeLock = 0;
63502 }
63503 return rc;
63504}
63505
63506/*
63507** Set the database handle used to determine if blocking locks are required.
63508*/
63509SQLITE_PRIVATEstatic void sqlite3WalDb(Wal *pWal, sqlite3 *db){
63510 pWal->db = db;
63511}
63512
63513/*
63514** Take an exclusive WRITE lock. Blocking if so configured.
63515*/
63516static int walLockWriter(Wal *pWal)walLockExclusive((Wal *pWal), 0, 1){
63517 int rc;
63518 walEnableBlocking(pWal)0;
63519 rc = walLockExclusive(pWal, WAL_WRITE_LOCK0, 1);
63520 walDisableBlocking(pWal);
63521 return rc;
63522}
63523#else
63524# define walEnableBlocking(x)0 0
63525# define walDisableBlocking(x)
63526# define walLockWriter(pWal)walLockExclusive((pWal), 0, 1) walLockExclusive((pWal), WAL_WRITE_LOCK0, 1)
63527# define sqlite3WalDb(pWal, db)
63528#endif /* ifdef SQLITE_ENABLE_SETLK_TIMEOUT */
63529
63530
63531/*
63532** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
63533** n. If the attempt fails and parameter xBusy is not NULL, then it is a
63534** busy-handler function. Invoke it and retry the lock until either the
63535** lock is successfully obtained or the busy-handler returns 0.
63536*/
63537static int walBusyLock(
63538 Wal *pWal, /* WAL connection */
63539 int (*xBusy)(void*), /* Function to call when busy */
63540 void *pBusyArg, /* Context argument for xBusyHandler */
63541 int lockIdx, /* Offset of first byte to lock */
63542 int n /* Number of bytes to lock */
63543){
63544 int rc;
63545 do {
63546 rc = walLockExclusive(pWal, lockIdx, n);
63547 }while( xBusy && rc==SQLITE_BUSY5 && xBusy(pBusyArg) );
63548#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
63549 if( rc==SQLITE_BUSY_TIMEOUT(5 | (3<<8)) ){
63550 walDisableBlocking(pWal);
63551 rc = SQLITE_BUSY5;
63552 }
63553#endif
63554 return rc;
63555}
63556
63557/*
63558** The cache of the wal-index header must be valid to call this function.
63559** Return the page-size in bytes used by the database.
63560*/
63561static int walPagesize(Wal *pWal){
63562 return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
63563}
63564
63565/*
63566** The following is guaranteed when this function is called:
63567**
63568** a) the WRITER lock is held,
63569** b) the entire log file has been checkpointed, and
63570** c) any existing readers are reading exclusively from the database
63571** file - there are no readers that may attempt to read a frame from
63572** the log file.
63573**
63574** This function updates the shared-memory structures so that the next
63575** client to write to the database (which may be this one) does so by
63576** writing frames into the start of the log file.
63577**
63578** The value of parameter salt1 is used as the aSalt[1] value in the
63579** new wal-index header. It should be passed a pseudo-random value (i.e.
63580** one obtained from sqlite3_randomness()).
63581*/
63582static void walRestartHdr(Wal *pWal, u32 salt1){
63583 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
63584 int i; /* Loop counter */
63585 u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
63586 pWal->nCkpt++;
63587 pWal->hdr.mxFrame = 0;
63588 sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
63589 memcpy(&pWal->hdr.aSalt[1], &salt1, 4);
63590 walIndexWriteHdr(pWal);
63591 AtomicStore(&pInfo->nBackfill, 0)__atomic_store_n((&pInfo->nBackfill),(0),0);
63592 pInfo->nBackfillAttempted = 0;
63593 pInfo->aReadMark[1] = 0;
63594 for(i=2; i<WAL_NREADER(8 -3); i++) pInfo->aReadMark[i] = READMARK_NOT_USED0xffffffff;
63595 assert( pInfo->aReadMark[0]==0 )((void) (0));
63596}
63597
63598/*
63599** Copy as much content as we can from the WAL back into the database file
63600** in response to an sqlite3_wal_checkpoint() request or the equivalent.
63601**
63602** The amount of information copies from WAL to database might be limited
63603** by active readers. This routine will never overwrite a database page
63604** that a concurrent reader might be using.
63605**
63606** All I/O barrier operations (a.k.a fsyncs) occur in this routine when
63607** SQLite is in WAL-mode in synchronous=NORMAL. That means that if
63608** checkpoints are always run by a background thread or background
63609** process, foreground threads will never block on a lengthy fsync call.
63610**
63611** Fsync is called on the WAL before writing content out of the WAL and
63612** into the database. This ensures that if the new content is persistent
63613** in the WAL and can be recovered following a power-loss or hard reset.
63614**
63615** Fsync is also called on the database file if (and only if) the entire
63616** WAL content is copied into the database file. This second fsync makes
63617** it safe to delete the WAL since the new content will persist in the
63618** database file.
63619**
63620** This routine uses and updates the nBackfill field of the wal-index header.
63621** This is the only routine that will increase the value of nBackfill.
63622** (A WAL reset or recovery will revert nBackfill to zero, but not increase
63623** its value.)
63624**
63625** The caller must be holding sufficient locks to ensure that no other
63626** checkpoint is running (in any other thread or process) at the same
63627** time.
63628*/
63629static int walCheckpoint(
63630 Wal *pWal, /* Wal connection */
63631 sqlite3 *db, /* Check for interrupts on this handle */
63632 int eMode, /* One of PASSIVE, FULL or RESTART */
63633 int (*xBusy)(void*), /* Function to call when busy */
63634 void *pBusyArg, /* Context argument for xBusyHandler */
63635 int sync_flags, /* Flags for OsSync() (or 0) */
63636 u8 *zBuf /* Temporary buffer to use */
63637){
63638 int rc = SQLITE_OK0; /* Return code */
63639 int szPage; /* Database page-size */
63640 WalIterator *pIter = 0; /* Wal iterator context */
63641 u32 iDbpage = 0; /* Next database page to write */
63642 u32 iFrame = 0; /* Wal frame containing data for iDbpage */
63643 u32 mxSafeFrame; /* Max frame that can be backfilled */
63644 u32 mxPage; /* Max database page to write */
63645 int i; /* Loop counter */
63646 volatile WalCkptInfo *pInfo; /* The checkpoint status information */
63647
63648 szPage = walPagesize(pWal);
63649 testcase( szPage<=32768 );
63650 testcase( szPage>=65536 );
63651 pInfo = walCkptInfo(pWal);
63652 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
63653
63654 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
63655 ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
63656 assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 )((void) (0));
63657
63658 /* Compute in mxSafeFrame the index of the last frame of the WAL that is
63659 ** safe to write into the database. Frames beyond mxSafeFrame might
63660 ** overwrite database pages that are in use by active readers and thus
63661 ** cannot be backfilled from the WAL.
63662 */
63663 mxSafeFrame = pWal->hdr.mxFrame;
63664 mxPage = pWal->hdr.nPage;
63665 for(i=1; i<WAL_NREADER(8 -3); i++){
63666 u32 y = AtomicLoad(pInfo->aReadMark+i)__atomic_load_n((pInfo->aReadMark+i),0);
63667 if( mxSafeFrame>y ){
63668 assert( y<=pWal->hdr.mxFrame )((void) (0));
63669 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i)(3+(i)), 1);
63670 if( rc==SQLITE_OK0 ){
63671 u32 iMark = (i==1 ? mxSafeFrame : READMARK_NOT_USED0xffffffff);
63672 AtomicStore(pInfo->aReadMark+i, iMark)__atomic_store_n((pInfo->aReadMark+i),(iMark),0);
63673 walUnlockExclusive(pWal, WAL_READ_LOCK(i)(3+(i)), 1);
63674 }else if( rc==SQLITE_BUSY5 ){
63675 mxSafeFrame = y;
63676 xBusy = 0;
63677 }else{
63678 goto walcheckpoint_out;
63679 }
63680 }
63681 }
63682
63683 /* Allocate the iterator */
63684 if( pInfo->nBackfill<mxSafeFrame ){
63685 rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter);
63686 assert( rc==SQLITE_OK || pIter==0 )((void) (0));
63687 }
63688
63689 if( pIter
63690 && (rc = walBusyLock(pWal,xBusy,pBusyArg,WAL_READ_LOCK(0)(3+(0)),1))==SQLITE_OK0
63691 ){
63692 u32 nBackfill = pInfo->nBackfill;
63693
63694 pInfo->nBackfillAttempted = mxSafeFrame;
63695
63696 /* Sync the WAL to disk */
63697 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags)(((sync_flags)>>2)&0x03));
63698
63699 /* If the database may grow as a result of this checkpoint, hint
63700 ** about the eventual size of the db file to the VFS layer.
63701 */
63702 if( rc==SQLITE_OK0 ){
63703 i64 nReq = ((i64)mxPage * szPage);
63704 i64 nSize; /* Current size of database file */
63705 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_START39, 0);
63706 rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
63707 if( rc==SQLITE_OK0 && nSize<nReq ){
63708 if( (nSize+65536+(i64)pWal->hdr.mxFrame*szPage)<nReq ){
63709 /* If the size of the final database is larger than the current
63710 ** database plus the amount of data in the wal file, plus the
63711 ** maximum size of the pending-byte page (65536 bytes), then
63712 ** must be corruption somewhere. */
63713 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(63713);
63714 }else{
63715 sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT5,&nReq);
63716 }
63717 }
63718
63719 }
63720
63721 /* Iterate through the contents of the WAL, copying data to the db file */
63722 while( rc==SQLITE_OK0 && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
63723 i64 iOffset;
63724 assert( walFramePgno(pWal, iFrame)==iDbpage )((void) (0));
63725 if( AtomicLoad(&db->u1.isInterrupted)__atomic_load_n((&db->u1.isInterrupted),0) ){
63726 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT7 : SQLITE_INTERRUPT9;
63727 break;
63728 }
63729 if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ){
63730 continue;
63731 }
63732 iOffset = walFrameOffset(iFrame, szPage)( 32 + ((iFrame)-1)*(i64)((szPage)+24) ) + WAL_FRAME_HDRSIZE24;
63733 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
63734 rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
63735 if( rc!=SQLITE_OK0 ) break;
63736 iOffset = (iDbpage-1)*(i64)szPage;
63737 testcase( IS_BIG_INT(iOffset) );
63738 rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
63739 if( rc!=SQLITE_OK0 ) break;
63740 }
63741 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_DONE37, 0);
63742
63743 /* If work was actually accomplished... */
63744 if( rc==SQLITE_OK0 ){
63745 if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
63746 i64 szDb = pWal->hdr.nPage*(i64)szPage;
63747 testcase( IS_BIG_INT(szDb) );
63748 rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
63749 if( rc==SQLITE_OK0 ){
63750 rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags)(((sync_flags)>>2)&0x03));
63751 }
63752 }
63753 if( rc==SQLITE_OK0 ){
63754 AtomicStore(&pInfo->nBackfill, mxSafeFrame)__atomic_store_n((&pInfo->nBackfill),(mxSafeFrame),0);
63755 }
63756 }
63757
63758 /* Release the reader lock held while backfilling */
63759 walUnlockExclusive(pWal, WAL_READ_LOCK(0)(3+(0)), 1);
63760 }
63761
63762 if( rc==SQLITE_BUSY5 ){
63763 /* Reset the return code so as not to report a checkpoint failure
63764 ** just because there are active readers. */
63765 rc = SQLITE_OK0;
63766 }
63767 }
63768
63769 /* If this is an SQLITE_CHECKPOINT_RESTART or TRUNCATE operation, and the
63770 ** entire wal file has been copied into the database file, then block
63771 ** until all readers have finished using the wal file. This ensures that
63772 ** the next process to write to the database restarts the wal file.
63773 */
63774 if( rc==SQLITE_OK0 && eMode!=SQLITE_CHECKPOINT_PASSIVE0 ){
63775 assert( pWal->writeLock )((void) (0));
63776 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
63777 rc = SQLITE_BUSY5;
63778 }else if( eMode>=SQLITE_CHECKPOINT_RESTART2 ){
63779 u32 salt1;
63780 sqlite3_randomness(4, &salt1);
63781 assert( pInfo->nBackfill==pWal->hdr.mxFrame )((void) (0));
63782 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1)(3+(1)), WAL_NREADER(8 -3)-1);
63783 if( rc==SQLITE_OK0 ){
63784 if( eMode==SQLITE_CHECKPOINT_TRUNCATE3 ){
63785 /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
63786 ** SQLITE_CHECKPOINT_RESTART with the addition that it also
63787 ** truncates the log file to zero bytes just prior to a
63788 ** successful return.
63789 **
63790 ** In theory, it might be safe to do this without updating the
63791 ** wal-index header in shared memory, as all subsequent reader or
63792 ** writer clients should see that the entire log file has been
63793 ** checkpointed and behave accordingly. This seems unsafe though,
63794 ** as it would leave the system in a state where the contents of
63795 ** the wal-index header do not match the contents of the
63796 ** file-system. To avoid this, update the wal-index header to
63797 ** indicate that the log file contains zero valid frames. */
63798 walRestartHdr(pWal, salt1);
63799 rc = sqlite3OsTruncate(pWal->pWalFd, 0);
63800 }
63801 walUnlockExclusive(pWal, WAL_READ_LOCK(1)(3+(1)), WAL_NREADER(8 -3)-1);
63802 }
63803 }
63804 }
63805
63806 walcheckpoint_out:
63807 walIteratorFree(pIter);
63808 return rc;
63809}
63810
63811/*
63812** If the WAL file is currently larger than nMax bytes in size, truncate
63813** it to exactly nMax bytes. If an error occurs while doing so, ignore it.
63814*/
63815static void walLimitSize(Wal *pWal, i64 nMax){
63816 i64 sz;
63817 int rx;
63818 sqlite3BeginBenignMalloc();
63819 rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
63820 if( rx==SQLITE_OK0 && (sz > nMax ) ){
63821 rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
63822 }
63823 sqlite3EndBenignMalloc();
63824 if( rx ){
63825 sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
63826 }
63827}
63828
63829/*
63830** Close a connection to a log file.
63831*/
63832SQLITE_PRIVATEstatic int sqlite3WalClose(
63833 Wal *pWal, /* Wal to close */
63834 sqlite3 *db, /* For interrupt flag */
63835 int sync_flags, /* Flags to pass to OsSync() (or 0) */
63836 int nBuf,
63837 u8 *zBuf /* Buffer of at least nBuf bytes */
63838){
63839 int rc = SQLITE_OK0;
63840 if( pWal ){
63841 int isDelete = 0; /* True to unlink wal and wal-index files */
63842
63843 /* If an EXCLUSIVE lock can be obtained on the database file (using the
63844 ** ordinary, rollback-mode locking methods, this guarantees that the
63845 ** connection associated with this log file is the only connection to
63846 ** the database. In this case checkpoint the database and unlink both
63847 ** the wal and wal-index files.
63848 **
63849 ** The EXCLUSIVE lock is not released before returning.
63850 */
63851 if( zBuf!=0
63852 && SQLITE_OK0==(rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE4))
63853 ){
63854 if( pWal->exclusiveMode==WAL_NORMAL_MODE0 ){
63855 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE1;
63856 }
63857 rc = sqlite3WalCheckpoint(pWal, db,
63858 SQLITE_CHECKPOINT_PASSIVE0, 0, 0, sync_flags, nBuf, zBuf, 0, 0
63859 );
63860 if( rc==SQLITE_OK0 ){
63861 int bPersist = -1;
63862 sqlite3OsFileControlHint(
63863 pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL10, &bPersist
63864 );
63865 if( bPersist!=1 ){
63866 /* Try to delete the WAL file if the checkpoint completed and
63867 ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal
63868 ** mode (!bPersist) */
63869 isDelete = 1;
63870 }else if( pWal->mxWalSize>=0 ){
63871 /* Try to truncate the WAL file to zero bytes if the checkpoint
63872 ** completed and fsynced (rc==SQLITE_OK) and we are in persistent
63873 ** WAL mode (bPersist) and if the PRAGMA journal_size_limit is a
63874 ** non-negative value (pWal->mxWalSize>=0). Note that we truncate
63875 ** to zero bytes as truncating to the journal_size_limit might
63876 ** leave a corrupt WAL file on disk. */
63877 walLimitSize(pWal, 0);
63878 }
63879 }
63880 }
63881
63882 walIndexClose(pWal, isDelete);
63883 sqlite3OsClose(pWal->pWalFd);
63884 if( isDelete ){
63885 sqlite3BeginBenignMalloc();
63886 sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
63887 sqlite3EndBenignMalloc();
63888 }
63889 WALTRACE(("WAL%p: closed\n", pWal));
63890 sqlite3_free((void *)pWal->apWiData);
63891 sqlite3_free(pWal);
63892 }
63893 return rc;
63894}
63895
63896/*
63897** Try to read the wal-index header. Return 0 on success and 1 if
63898** there is a problem.
63899**
63900** The wal-index is in shared memory. Another thread or process might
63901** be writing the header at the same time this procedure is trying to
63902** read it, which might result in inconsistency. A dirty read is detected
63903** by verifying that both copies of the header are the same and also by
63904** a checksum on the header.
63905**
63906** If and only if the read is consistent and the header is different from
63907** pWal->hdr, then pWal->hdr is updated to the content of the new header
63908** and *pChanged is set to 1.
63909**
63910** If the checksum cannot be verified return non-zero. If the header
63911** is read successfully and the checksum verified, return zero.
63912*/
63913static SQLITE_NO_TSAN__attribute__((no_sanitize_thread)) int walIndexTryHdr(Wal *pWal, int *pChanged){
63914 u32 aCksum[2]; /* Checksum on the header content */
63915 WalIndexHdr h1, h2; /* Two copies of the header content */
63916 WalIndexHdr volatile *aHdr; /* Header in shared memory */
63917
63918 /* The first page of the wal-index must be mapped at this point. */
63919 assert( pWal->nWiData>0 && pWal->apWiData[0] )((void) (0));
63920
63921 /* Read the header. This might happen concurrently with a write to the
63922 ** same area of shared memory on a different CPU in a SMP,
63923 ** meaning it is possible that an inconsistent snapshot is read
63924 ** from the file. If this happens, return non-zero.
63925 **
63926 ** tag-20200519-1:
63927 ** There are two copies of the header at the beginning of the wal-index.
63928 ** When reading, read [0] first then [1]. Writes are in the reverse order.
63929 ** Memory barriers are used to prevent the compiler or the hardware from
63930 ** reordering the reads and writes. TSAN and similar tools can sometimes
63931 ** give false-positive warnings about these accesses because the tools do not
63932 ** account for the double-read and the memory barrier. The use of mutexes
63933 ** here would be problematic as the memory being accessed is potentially
63934 ** shared among multiple processes and not all mutex implementions work
63935 ** reliably in that environment.
63936 */
63937 aHdr = walIndexHdr(pWal);
63938 memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); /* Possible TSAN false-positive */
63939 walShmBarrier(pWal);
63940 memcpy(&h2, (void *)&aHdr[1], sizeof(h2));
63941
63942 if( memcmp(&h1, &h2, sizeof(h1))!=0 ){
63943 return 1; /* Dirty read */
63944 }
63945 if( h1.isInit==0 ){
63946 return 1; /* Malformed header - probably all zeros */
63947 }
63948 walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
63949 if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){
63950 return 1; /* Checksum does not match */
63951 }
63952
63953 if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
63954 *pChanged = 1;
63955 memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
63956 pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
63957 testcase( pWal->szPage<=32768 );
63958 testcase( pWal->szPage>=65536 );
63959 }
63960
63961 /* The header was successfully read. Return zero. */
63962 return 0;
63963}
63964
63965/*
63966** This is the value that walTryBeginRead returns when it needs to
63967** be retried.
63968*/
63969#define WAL_RETRY(-1) (-1)
63970
63971/*
63972** Read the wal-index header from the wal-index and into pWal->hdr.
63973** If the wal-header appears to be corrupt, try to reconstruct the
63974** wal-index from the WAL before returning.
63975**
63976** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
63977** changed by this operation. If pWal->hdr is unchanged, set *pChanged
63978** to 0.
63979**
63980** If the wal-index header is successfully read, return SQLITE_OK.
63981** Otherwise an SQLite error code.
63982*/
63983static int walIndexReadHdr(Wal *pWal, int *pChanged){
63984 int rc; /* Return code */
63985 int badHdr; /* True if a header read failed */
63986 volatile u32 *page0; /* Chunk of wal-index containing header */
63987
63988 /* Ensure that page 0 of the wal-index (the page that contains the
63989 ** wal-index header) is mapped. Return early if an error occurs here.
63990 */
63991 assert( pChanged )((void) (0));
63992 rc = walIndexPage(pWal, 0, &page0);
63993 if( rc!=SQLITE_OK0 ){
63994 assert( rc!=SQLITE_READONLY )((void) (0)); /* READONLY changed to OK in walIndexPage */
63995 if( rc==SQLITE_READONLY_CANTINIT(8 | (5<<8)) ){
63996 /* The SQLITE_READONLY_CANTINIT return means that the shared-memory
63997 ** was openable but is not writable, and this thread is unable to
63998 ** confirm that another write-capable connection has the shared-memory
63999 ** open, and hence the content of the shared-memory is unreliable,
64000 ** since the shared-memory might be inconsistent with the WAL file
64001 ** and there is no writer on hand to fix it. */
64002 assert( page0==0 )((void) (0));
64003 assert( pWal->writeLock==0 )((void) (0));
64004 assert( pWal->readOnly & WAL_SHM_RDONLY )((void) (0));
64005 pWal->bShmUnreliable = 1;
64006 pWal->exclusiveMode = WAL_HEAPMEMORY_MODE2;
64007 *pChanged = 1;
64008 }else{
64009 return rc; /* Any other non-OK return is just an error */
64010 }
64011 }else{
64012 /* page0 can be NULL if the SHM is zero bytes in size and pWal->writeLock
64013 ** is zero, which prevents the SHM from growing */
64014 testcase( page0!=0 );
64015 }
64016 assert( page0!=0 || pWal->writeLock==0 )((void) (0));
64017
64018 /* If the first page of the wal-index has been mapped, try to read the
64019 ** wal-index header immediately, without holding any lock. This usually
64020 ** works, but may fail if the wal-index header is corrupt or currently
64021 ** being modified by another thread or process.
64022 */
64023 badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
64024
64025 /* If the first attempt failed, it might have been due to a race
64026 ** with a writer. So get a WRITE lock and try again.
64027 */
64028 if( badHdr ){
64029 if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY2) ){
64030 if( SQLITE_OK0==(rc = walLockShared(pWal, WAL_WRITE_LOCK0)) ){
64031 walUnlockShared(pWal, WAL_WRITE_LOCK0);
64032 rc = SQLITE_READONLY_RECOVERY(8 | (1<<8));
64033 }
64034 }else{
64035 int bWriteLock = pWal->writeLock;
64036 if( bWriteLock || SQLITE_OK0==(rc = walLockWriter(pWal)walLockExclusive((pWal), 0, 1)) ){
64037 pWal->writeLock = 1;
64038 if( SQLITE_OK0==(rc = walIndexPage(pWal, 0, &page0)) ){
64039 badHdr = walIndexTryHdr(pWal, pChanged);
64040 if( badHdr ){
64041 /* If the wal-index header is still malformed even while holding
64042 ** a WRITE lock, it can only mean that the header is corrupted and
64043 ** needs to be reconstructed. So run recovery to do exactly that.
64044 */
64045 rc = walIndexRecover(pWal);
64046 *pChanged = 1;
64047 }
64048 }
64049 if( bWriteLock==0 ){
64050 pWal->writeLock = 0;
64051 walUnlockExclusive(pWal, WAL_WRITE_LOCK0, 1);
64052 }
64053 }
64054 }
64055 }
64056
64057 /* If the header is read successfully, check the version number to make
64058 ** sure the wal-index was not constructed with some future format that
64059 ** this version of SQLite cannot understand.
64060 */
64061 if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION3007000 ){
64062 rc = SQLITE_CANTOPEN_BKPTsqlite3CantopenError(64062);
64063 }
64064 if( pWal->bShmUnreliable ){
64065 if( rc!=SQLITE_OK0 ){
64066 walIndexClose(pWal, 0);
64067 pWal->bShmUnreliable = 0;
64068 assert( pWal->nWiData>0 && pWal->apWiData[0]==0 )((void) (0));
64069 /* walIndexRecover() might have returned SHORT_READ if a concurrent
64070 ** writer truncated the WAL out from under it. If that happens, it
64071 ** indicates that a writer has fixed the SHM file for us, so retry */
64072 if( rc==SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ) rc = WAL_RETRY(-1);
64073 }
64074 pWal->exclusiveMode = WAL_NORMAL_MODE0;
64075 }
64076
64077 return rc;
64078}
64079
64080/*
64081** Open a transaction in a connection where the shared-memory is read-only
64082** and where we cannot verify that there is a separate write-capable connection
64083** on hand to keep the shared-memory up-to-date with the WAL file.
64084**
64085** This can happen, for example, when the shared-memory is implemented by
64086** memory-mapping a *-shm file, where a prior writer has shut down and
64087** left the *-shm file on disk, and now the present connection is trying
64088** to use that database but lacks write permission on the *-shm file.
64089** Other scenarios are also possible, depending on the VFS implementation.
64090**
64091** Precondition:
64092**
64093** The *-wal file has been read and an appropriate wal-index has been
64094** constructed in pWal->apWiData[] using heap memory instead of shared
64095** memory.
64096**
64097** If this function returns SQLITE_OK, then the read transaction has
64098** been successfully opened. In this case output variable (*pChanged)
64099** is set to true before returning if the caller should discard the
64100** contents of the page cache before proceeding. Or, if it returns
64101** WAL_RETRY, then the heap memory wal-index has been discarded and
64102** the caller should retry opening the read transaction from the
64103** beginning (including attempting to map the *-shm file).
64104**
64105** If an error occurs, an SQLite error code is returned.
64106*/
64107static int walBeginShmUnreliable(Wal *pWal, int *pChanged){
64108 i64 szWal; /* Size of wal file on disk in bytes */
64109 i64 iOffset; /* Current offset when reading wal file */
64110 u8 aBuf[WAL_HDRSIZE32]; /* Buffer to load WAL header into */
64111 u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
64112 int szFrame; /* Number of bytes in buffer aFrame[] */
64113 u8 *aData; /* Pointer to data part of aFrame buffer */
64114 volatile void *pDummy; /* Dummy argument for xShmMap */
64115 int rc; /* Return code */
64116 u32 aSaveCksum[2]; /* Saved copy of pWal->hdr.aFrameCksum */
64117
64118 assert( pWal->bShmUnreliable )((void) (0));
64119 assert( pWal->readOnly & WAL_SHM_RDONLY )((void) (0));
64120 assert( pWal->nWiData>0 && pWal->apWiData[0] )((void) (0));
64121
64122 /* Take WAL_READ_LOCK(0). This has the effect of preventing any
64123 ** writers from running a checkpoint, but does not stop them
64124 ** from running recovery. */
64125 rc = walLockShared(pWal, WAL_READ_LOCK(0)(3+(0)));
64126 if( rc!=SQLITE_OK0 ){
64127 if( rc==SQLITE_BUSY5 ) rc = WAL_RETRY(-1);
64128 goto begin_unreliable_shm_out;
64129 }
64130 pWal->readLock = 0;
64131
64132 /* Check to see if a separate writer has attached to the shared-memory area,
64133 ** thus making the shared-memory "reliable" again. Do this by invoking
64134 ** the xShmMap() routine of the VFS and looking to see if the return
64135 ** is SQLITE_READONLY instead of SQLITE_READONLY_CANTINIT.
64136 **
64137 ** If the shared-memory is now "reliable" return WAL_RETRY, which will
64138 ** cause the heap-memory WAL-index to be discarded and the actual
64139 ** shared memory to be used in its place.
64140 **
64141 ** This step is important because, even though this connection is holding
64142 ** the WAL_READ_LOCK(0) which prevents a checkpoint, a writer might
64143 ** have already checkpointed the WAL file and, while the current
64144 ** is active, wrap the WAL and start overwriting frames that this
64145 ** process wants to use.
64146 **
64147 ** Once sqlite3OsShmMap() has been called for an sqlite3_file and has
64148 ** returned any SQLITE_READONLY value, it must return only SQLITE_READONLY
64149 ** or SQLITE_READONLY_CANTINIT or some error for all subsequent invocations,
64150 ** even if some external agent does a "chmod" to make the shared-memory
64151 ** writable by us, until sqlite3OsShmUnmap() has been called.
64152 ** This is a requirement on the VFS implementation.
64153 */
64154 rc = sqlite3OsShmMap(pWal->pDbFd, 0, WALINDEX_PGSZ( sizeof(ht_slot)*(4096*2) + 4096*sizeof(u32) ), 0, &pDummy);
64155 assert( rc!=SQLITE_OK )((void) (0)); /* SQLITE_OK not possible for read-only connection */
64156 if( rc!=SQLITE_READONLY_CANTINIT(8 | (5<<8)) ){
64157 rc = (rc==SQLITE_READONLY8 ? WAL_RETRY(-1) : rc);
64158 goto begin_unreliable_shm_out;
64159 }
64160
64161 /* We reach this point only if the real shared-memory is still unreliable.
64162 ** Assume the in-memory WAL-index substitute is correct and load it
64163 ** into pWal->hdr.
64164 */
64165 memcpy(&pWal->hdr, (void*)walIndexHdr(pWal), sizeof(WalIndexHdr));
64166
64167 /* Make sure some writer hasn't come in and changed the WAL file out
64168 ** from under us, then disconnected, while we were not looking.
64169 */
64170 rc = sqlite3OsFileSize(pWal->pWalFd, &szWal);
64171 if( rc!=SQLITE_OK0 ){
64172 goto begin_unreliable_shm_out;
64173 }
64174 if( szWal<WAL_HDRSIZE32 ){
64175 /* If the wal file is too small to contain a wal-header and the
64176 ** wal-index header has mxFrame==0, then it must be safe to proceed
64177 ** reading the database file only. However, the page cache cannot
64178 ** be trusted, as a read/write connection may have connected, written
64179 ** the db, run a checkpoint, truncated the wal file and disconnected
64180 ** since this client's last read transaction. */
64181 *pChanged = 1;
64182 rc = (pWal->hdr.mxFrame==0 ? SQLITE_OK0 : WAL_RETRY(-1));
64183 goto begin_unreliable_shm_out;
64184 }
64185
64186 /* Check the salt keys at the start of the wal file still match. */
64187 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE32, 0);
64188 if( rc!=SQLITE_OK0 ){
64189 goto begin_unreliable_shm_out;
64190 }
64191 if( memcmp(&pWal->hdr.aSalt, &aBuf[16], 8) ){
64192 /* Some writer has wrapped the WAL file while we were not looking.
64193 ** Return WAL_RETRY which will cause the in-memory WAL-index to be
64194 ** rebuilt. */
64195 rc = WAL_RETRY(-1);
64196 goto begin_unreliable_shm_out;
64197 }
64198
64199 /* Allocate a buffer to read frames into */
64200 assert( (pWal->szPage & (pWal->szPage-1))==0 )((void) (0));
64201 assert( pWal->szPage>=512 && pWal->szPage<=65536 )((void) (0));
64202 szFrame = pWal->szPage + WAL_FRAME_HDRSIZE24;
64203 aFrame = (u8 *)sqlite3_malloc64(szFrame);
64204 if( aFrame==0 ){
64205 rc = SQLITE_NOMEM_BKPT7;
64206 goto begin_unreliable_shm_out;
64207 }
64208 aData = &aFrame[WAL_FRAME_HDRSIZE24];
64209
64210 /* Check to see if a complete transaction has been appended to the
64211 ** wal file since the heap-memory wal-index was created. If so, the
64212 ** heap-memory wal-index is discarded and WAL_RETRY returned to
64213 ** the caller. */
64214 aSaveCksum[0] = pWal->hdr.aFrameCksum[0];
64215 aSaveCksum[1] = pWal->hdr.aFrameCksum[1];
64216 for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->szPage)( 32 + ((pWal->hdr.mxFrame+1)-1)*(i64)((pWal->szPage)+24
) )
;
64217 iOffset+szFrame<=szWal;
64218 iOffset+=szFrame
64219 ){
64220 u32 pgno; /* Database page number for frame */
64221 u32 nTruncate; /* dbsize field from frame header */
64222
64223 /* Read and decode the next log frame. */
64224 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
64225 if( rc!=SQLITE_OK0 ) break;
64226 if( !walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame) ) break;
64227
64228 /* If nTruncate is non-zero, then a complete transaction has been
64229 ** appended to this wal file. Set rc to WAL_RETRY and break out of
64230 ** the loop. */
64231 if( nTruncate ){
64232 rc = WAL_RETRY(-1);
64233 break;
64234 }
64235 }
64236 pWal->hdr.aFrameCksum[0] = aSaveCksum[0];
64237 pWal->hdr.aFrameCksum[1] = aSaveCksum[1];
64238
64239 begin_unreliable_shm_out:
64240 sqlite3_free(aFrame);
64241 if( rc!=SQLITE_OK0 ){
64242 int i;
64243 for(i=0; i<pWal->nWiData; i++){
64244 sqlite3_free((void*)pWal->apWiData[i]);
64245 pWal->apWiData[i] = 0;
64246 }
64247 pWal->bShmUnreliable = 0;
64248 sqlite3WalEndReadTransaction(pWal);
64249 *pChanged = 1;
64250 }
64251 return rc;
64252}
64253
64254/*
64255** Attempt to start a read transaction. This might fail due to a race or
64256** other transient condition. When that happens, it returns WAL_RETRY to
64257** indicate to the caller that it is safe to retry immediately.
64258**
64259** On success return SQLITE_OK. On a permanent failure (such an
64260** I/O error or an SQLITE_BUSY because another process is running
64261** recovery) return a positive error code.
64262**
64263** The useWal parameter is true to force the use of the WAL and disable
64264** the case where the WAL is bypassed because it has been completely
64265** checkpointed. If useWal==0 then this routine calls walIndexReadHdr()
64266** to make a copy of the wal-index header into pWal->hdr. If the
64267** wal-index header has changed, *pChanged is set to 1 (as an indication
64268** to the caller that the local page cache is obsolete and needs to be
64269** flushed.) When useWal==1, the wal-index header is assumed to already
64270** be loaded and the pChanged parameter is unused.
64271**
64272** The caller must set the cnt parameter to the number of prior calls to
64273** this routine during the current read attempt that returned WAL_RETRY.
64274** This routine will start taking more aggressive measures to clear the
64275** race conditions after multiple WAL_RETRY returns, and after an excessive
64276** number of errors will ultimately return SQLITE_PROTOCOL. The
64277** SQLITE_PROTOCOL return indicates that some other process has gone rogue
64278** and is not honoring the locking protocol. There is a vanishingly small
64279** chance that SQLITE_PROTOCOL could be returned because of a run of really
64280** bad luck when there is lots of contention for the wal-index, but that
64281** possibility is so small that it can be safely neglected, we believe.
64282**
64283** On success, this routine obtains a read lock on
64284** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
64285** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
64286** that means the Wal does not hold any read lock. The reader must not
64287** access any database page that is modified by a WAL frame up to and
64288** including frame number aReadMark[pWal->readLock]. The reader will
64289** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
64290** Or if pWal->readLock==0, then the reader will ignore the WAL
64291** completely and get all content directly from the database file.
64292** If the useWal parameter is 1 then the WAL will never be ignored and
64293** this routine will always set pWal->readLock>0 on success.
64294** When the read transaction is completed, the caller must release the
64295** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
64296**
64297** This routine uses the nBackfill and aReadMark[] fields of the header
64298** to select a particular WAL_READ_LOCK() that strives to let the
64299** checkpoint process do as much work as possible. This routine might
64300** update values of the aReadMark[] array in the header, but if it does
64301** so it takes care to hold an exclusive lock on the corresponding
64302** WAL_READ_LOCK() while changing values.
64303*/
64304static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
64305 volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
64306 u32 mxReadMark; /* Largest aReadMark[] value */
64307 int mxI; /* Index of largest aReadMark[] value */
64308 int i; /* Loop counter */
64309 int rc = SQLITE_OK0; /* Return code */
64310 u32 mxFrame; /* Wal frame to lock to */
64311
64312 assert( pWal->readLock<0 )((void) (0)); /* Not currently locked */
64313
64314 /* useWal may only be set for read/write connections */
64315 assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 )((void) (0));
64316
64317 /* Take steps to avoid spinning forever if there is a protocol error.
64318 **
64319 ** Circumstances that cause a RETRY should only last for the briefest
64320 ** instances of time. No I/O or other system calls are done while the
64321 ** locks are held, so the locks should not be held for very long. But
64322 ** if we are unlucky, another process that is holding a lock might get
64323 ** paged out or take a page-fault that is time-consuming to resolve,
64324 ** during the few nanoseconds that it is holding the lock. In that case,
64325 ** it might take longer than normal for the lock to free.
64326 **
64327 ** After 5 RETRYs, we begin calling sqlite3OsSleep(). The first few
64328 ** calls to sqlite3OsSleep() have a delay of 1 microsecond. Really this
64329 ** is more of a scheduler yield than an actual delay. But on the 10th
64330 ** an subsequent retries, the delays start becoming longer and longer,
64331 ** so that on the 100th (and last) RETRY we delay for 323 milliseconds.
64332 ** The total delay time before giving up is less than 10 seconds.
64333 */
64334 if( cnt>5 ){
64335 int nDelay = 1; /* Pause time in microseconds */
64336 if( cnt>100 ){
64337 VVA_ONLY( pWal->lockError = 1; )
64338 return SQLITE_PROTOCOL15;
64339 }
64340 if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
64341 sqlite3OsSleep(pWal->pVfs, nDelay);
64342 }
64343
64344 if( !useWal ){
64345 assert( rc==SQLITE_OK )((void) (0));
64346 if( pWal->bShmUnreliable==0 ){
64347 rc = walIndexReadHdr(pWal, pChanged);
64348 }
64349 if( rc==SQLITE_BUSY5 ){
64350 /* If there is not a recovery running in another thread or process
64351 ** then convert BUSY errors to WAL_RETRY. If recovery is known to
64352 ** be running, convert BUSY to BUSY_RECOVERY. There is a race here
64353 ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
64354 ** would be technically correct. But the race is benign since with
64355 ** WAL_RETRY this routine will be called again and will probably be
64356 ** right on the second iteration.
64357 */
64358 if( pWal->apWiData[0]==0 ){
64359 /* This branch is taken when the xShmMap() method returns SQLITE_BUSY.
64360 ** We assume this is a transient condition, so return WAL_RETRY. The
64361 ** xShmMap() implementation used by the default unix and win32 VFS
64362 ** modules may return SQLITE_BUSY due to a race condition in the
64363 ** code that determines whether or not the shared-memory region
64364 ** must be zeroed before the requested page is returned.
64365 */
64366 rc = WAL_RETRY(-1);
64367 }else if( SQLITE_OK0==(rc = walLockShared(pWal, WAL_RECOVER_LOCK2)) ){
64368 walUnlockShared(pWal, WAL_RECOVER_LOCK2);
64369 rc = WAL_RETRY(-1);
64370 }else if( rc==SQLITE_BUSY5 ){
64371 rc = SQLITE_BUSY_RECOVERY(5 | (1<<8));
64372 }
64373 }
64374 if( rc!=SQLITE_OK0 ){
64375 return rc;
64376 }
64377 else if( pWal->bShmUnreliable ){
64378 return walBeginShmUnreliable(pWal, pChanged);
64379 }
64380 }
64381
64382 assert( pWal->nWiData>0 )((void) (0));
64383 assert( pWal->apWiData[0]!=0 )((void) (0));
64384 pInfo = walCkptInfo(pWal);
64385 if( !useWal && AtomicLoad(&pInfo->nBackfill)__atomic_load_n((&pInfo->nBackfill),0)==pWal->hdr.mxFrame
64386#ifdef SQLITE_ENABLE_SNAPSHOT1
64387 && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0)
64388#endif
64389 ){
64390 /* The WAL has been completely backfilled (or it is empty).
64391 ** and can be safely ignored.
64392 */
64393 rc = walLockShared(pWal, WAL_READ_LOCK(0)(3+(0)));
64394 walShmBarrier(pWal);
64395 if( rc==SQLITE_OK0 ){
64396 if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
64397 /* It is not safe to allow the reader to continue here if frames
64398 ** may have been appended to the log before READ_LOCK(0) was obtained.
64399 ** When holding READ_LOCK(0), the reader ignores the entire log file,
64400 ** which implies that the database file contains a trustworthy
64401 ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from
64402 ** happening, this is usually correct.
64403 **
64404 ** However, if frames have been appended to the log (or if the log
64405 ** is wrapped and written for that matter) before the READ_LOCK(0)
64406 ** is obtained, that is not necessarily true. A checkpointer may
64407 ** have started to backfill the appended frames but crashed before
64408 ** it finished. Leaving a corrupt image in the database file.
64409 */
64410 walUnlockShared(pWal, WAL_READ_LOCK(0)(3+(0)));
64411 return WAL_RETRY(-1);
64412 }
64413 pWal->readLock = 0;
64414 return SQLITE_OK0;
64415 }else if( rc!=SQLITE_BUSY5 ){
64416 return rc;
64417 }
64418 }
64419
64420 /* If we get this far, it means that the reader will want to use
64421 ** the WAL to get at content from recent commits. The job now is
64422 ** to select one of the aReadMark[] entries that is closest to
64423 ** but not exceeding pWal->hdr.mxFrame and lock that entry.
64424 */
64425 mxReadMark = 0;
64426 mxI = 0;
64427 mxFrame = pWal->hdr.mxFrame;
64428#ifdef SQLITE_ENABLE_SNAPSHOT1
64429 if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
64430 mxFrame = pWal->pSnapshot->mxFrame;
64431 }
64432#endif
64433 for(i=1; i<WAL_NREADER(8 -3); i++){
64434 u32 thisMark = AtomicLoad(pInfo->aReadMark+i)__atomic_load_n((pInfo->aReadMark+i),0);
64435 if( mxReadMark<=thisMark && thisMark<=mxFrame ){
64436 assert( thisMark!=READMARK_NOT_USED )((void) (0));
64437 mxReadMark = thisMark;
64438 mxI = i;
64439 }
64440 }
64441 if( (pWal->readOnly & WAL_SHM_RDONLY2)==0
64442 && (mxReadMark<mxFrame || mxI==0)
64443 ){
64444 for(i=1; i<WAL_NREADER(8 -3); i++){
64445 rc = walLockExclusive(pWal, WAL_READ_LOCK(i)(3+(i)), 1);
64446 if( rc==SQLITE_OK0 ){
64447 AtomicStore(pInfo->aReadMark+i,mxFrame)__atomic_store_n((pInfo->aReadMark+i),(mxFrame),0);
64448 mxReadMark = mxFrame;
64449 mxI = i;
64450 walUnlockExclusive(pWal, WAL_READ_LOCK(i)(3+(i)), 1);
64451 break;
64452 }else if( rc!=SQLITE_BUSY5 ){
64453 return rc;
64454 }
64455 }
64456 }
64457 if( mxI==0 ){
64458 assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 )((void) (0));
64459 return rc==SQLITE_BUSY5 ? WAL_RETRY(-1) : SQLITE_READONLY_CANTINIT(8 | (5<<8));
64460 }
64461
64462 rc = walLockShared(pWal, WAL_READ_LOCK(mxI)(3+(mxI)));
64463 if( rc ){
64464 return rc==SQLITE_BUSY5 ? WAL_RETRY(-1) : rc;
64465 }
64466 /* Now that the read-lock has been obtained, check that neither the
64467 ** value in the aReadMark[] array or the contents of the wal-index
64468 ** header have changed.
64469 **
64470 ** It is necessary to check that the wal-index header did not change
64471 ** between the time it was read and when the shared-lock was obtained
64472 ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
64473 ** that the log file may have been wrapped by a writer, or that frames
64474 ** that occur later in the log than pWal->hdr.mxFrame may have been
64475 ** copied into the database by a checkpointer. If either of these things
64476 ** happened, then reading the database with the current value of
64477 ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
64478 ** instead.
64479 **
64480 ** Before checking that the live wal-index header has not changed
64481 ** since it was read, set Wal.minFrame to the first frame in the wal
64482 ** file that has not yet been checkpointed. This client will not need
64483 ** to read any frames earlier than minFrame from the wal file - they
64484 ** can be safely read directly from the database file.
64485 **
64486 ** Because a ShmBarrier() call is made between taking the copy of
64487 ** nBackfill and checking that the wal-header in shared-memory still
64488 ** matches the one cached in pWal->hdr, it is guaranteed that the
64489 ** checkpointer that set nBackfill was not working with a wal-index
64490 ** header newer than that cached in pWal->hdr. If it were, that could
64491 ** cause a problem. The checkpointer could omit to checkpoint
64492 ** a version of page X that lies before pWal->minFrame (call that version
64493 ** A) on the basis that there is a newer version (version B) of the same
64494 ** page later in the wal file. But if version B happens to like past
64495 ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
64496 ** that it can read version A from the database file. However, since
64497 ** we can guarantee that the checkpointer that set nBackfill could not
64498 ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
64499 */
64500 pWal->minFrame = AtomicLoad(&pInfo->nBackfill)__atomic_load_n((&pInfo->nBackfill),0)+1;
64501 walShmBarrier(pWal);
64502 if( AtomicLoad(pInfo->aReadMark+mxI)__atomic_load_n((pInfo->aReadMark+mxI),0)!=mxReadMark
64503 || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
64504 ){
64505 walUnlockShared(pWal, WAL_READ_LOCK(mxI)(3+(mxI)));
64506 return WAL_RETRY(-1);
64507 }else{
64508 assert( mxReadMark<=pWal->hdr.mxFrame )((void) (0));
64509 pWal->readLock = (i16)mxI;
64510 }
64511 return rc;
64512}
64513
64514#ifdef SQLITE_ENABLE_SNAPSHOT1
64515/*
64516** Attempt to reduce the value of the WalCkptInfo.nBackfillAttempted
64517** variable so that older snapshots can be accessed. To do this, loop
64518** through all wal frames from nBackfillAttempted to (nBackfill+1),
64519** comparing their content to the corresponding page with the database
64520** file, if any. Set nBackfillAttempted to the frame number of the
64521** first frame for which the wal file content matches the db file.
64522**
64523** This is only really safe if the file-system is such that any page
64524** writes made by earlier checkpointers were atomic operations, which
64525** is not always true. It is also possible that nBackfillAttempted
64526** may be left set to a value larger than expected, if a wal frame
64527** contains content that duplicate of an earlier version of the same
64528** page.
64529**
64530** SQLITE_OK is returned if successful, or an SQLite error code if an
64531** error occurs. It is not an error if nBackfillAttempted cannot be
64532** decreased at all.
64533*/
64534SQLITE_PRIVATEstatic int sqlite3WalSnapshotRecover(Wal *pWal){
64535 int rc;
64536
64537 assert( pWal->readLock>=0 )((void) (0));
64538 rc = walLockExclusive(pWal, WAL_CKPT_LOCK1, 1);
64539 if( rc==SQLITE_OK0 ){
64540 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
64541 int szPage = (int)pWal->szPage;
64542 i64 szDb; /* Size of db file in bytes */
64543
64544 rc = sqlite3OsFileSize(pWal->pDbFd, &szDb);
64545 if( rc==SQLITE_OK0 ){
64546 void *pBuf1 = sqlite3_malloc(szPage);
64547 void *pBuf2 = sqlite3_malloc(szPage);
64548 if( pBuf1==0 || pBuf2==0 ){
64549 rc = SQLITE_NOMEM7;
64550 }else{
64551 u32 i = pInfo->nBackfillAttempted;
64552 for(i=pInfo->nBackfillAttempted; i>AtomicLoad(&pInfo->nBackfill)__atomic_load_n((&pInfo->nBackfill),0); i--){
64553 WalHashLoc sLoc; /* Hash table location */
64554 u32 pgno; /* Page number in db file */
64555 i64 iDbOff; /* Offset of db file entry */
64556 i64 iWalOff; /* Offset of wal file entry */
64557
64558 rc = walHashGet(pWal, walFramePage(i), &sLoc);
64559 if( rc!=SQLITE_OK0 ) break;
64560 assert( i - sLoc.iZero - 1 >=0 )((void) (0));
64561 pgno = sLoc.aPgno[i-sLoc.iZero-1];
64562 iDbOff = (i64)(pgno-1) * szPage;
64563
64564 if( iDbOff+szPage<=szDb ){
64565 iWalOff = walFrameOffset(i, szPage)( 32 + ((i)-1)*(i64)((szPage)+24) ) + WAL_FRAME_HDRSIZE24;
64566 rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff);
64567
64568 if( rc==SQLITE_OK0 ){
64569 rc = sqlite3OsRead(pWal->pDbFd, pBuf2, szPage, iDbOff);
64570 }
64571
64572 if( rc!=SQLITE_OK0 || 0==memcmp(pBuf1, pBuf2, szPage) ){
64573 break;
64574 }
64575 }
64576
64577 pInfo->nBackfillAttempted = i-1;
64578 }
64579 }
64580
64581 sqlite3_free(pBuf1);
64582 sqlite3_free(pBuf2);
64583 }
64584 walUnlockExclusive(pWal, WAL_CKPT_LOCK1, 1);
64585 }
64586
64587 return rc;
64588}
64589#endif /* SQLITE_ENABLE_SNAPSHOT */
64590
64591/*
64592** Begin a read transaction on the database.
64593**
64594** This routine used to be called sqlite3OpenSnapshot() and with good reason:
64595** it takes a snapshot of the state of the WAL and wal-index for the current
64596** instant in time. The current thread will continue to use this snapshot.
64597** Other threads might append new content to the WAL and wal-index but
64598** that extra content is ignored by the current thread.
64599**
64600** If the database contents have changes since the previous read
64601** transaction, then *pChanged is set to 1 before returning. The
64602** Pager layer will use this to know that its cache is stale and
64603** needs to be flushed.
64604*/
64605SQLITE_PRIVATEstatic int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
64606 int rc; /* Return code */
64607 int cnt = 0; /* Number of TryBeginRead attempts */
64608#ifdef SQLITE_ENABLE_SNAPSHOT1
64609 int bChanged = 0;
64610 WalIndexHdr *pSnapshot = pWal->pSnapshot;
64611#endif
64612
64613 assert( pWal->ckptLock==0 )((void) (0));
64614
64615#ifdef SQLITE_ENABLE_SNAPSHOT1
64616 if( pSnapshot ){
64617 if( memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
64618 bChanged = 1;
64619 }
64620
64621 /* It is possible that there is a checkpointer thread running
64622 ** concurrent with this code. If this is the case, it may be that the
64623 ** checkpointer has already determined that it will checkpoint
64624 ** snapshot X, where X is later in the wal file than pSnapshot, but
64625 ** has not yet set the pInfo->nBackfillAttempted variable to indicate
64626 ** its intent. To avoid the race condition this leads to, ensure that
64627 ** there is no checkpointer process by taking a shared CKPT lock
64628 ** before checking pInfo->nBackfillAttempted. */
64629 (void)walEnableBlocking(pWal)0;
64630 rc = walLockShared(pWal, WAL_CKPT_LOCK1);
64631 walDisableBlocking(pWal);
64632
64633 if( rc!=SQLITE_OK0 ){
64634 return rc;
64635 }
64636 pWal->ckptLock = 1;
64637 }
64638#endif
64639
64640 do{
64641 rc = walTryBeginRead(pWal, pChanged, 0, ++cnt);
64642 }while( rc==WAL_RETRY(-1) );
64643 testcase( (rc&0xff)==SQLITE_BUSY );
64644 testcase( (rc&0xff)==SQLITE_IOERR );
64645 testcase( rc==SQLITE_PROTOCOL );
64646 testcase( rc==SQLITE_OK );
64647
64648#ifdef SQLITE_ENABLE_SNAPSHOT1
64649 if( rc==SQLITE_OK0 ){
64650 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
64651 /* At this point the client has a lock on an aReadMark[] slot holding
64652 ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr
64653 ** is populated with the wal-index header corresponding to the head
64654 ** of the wal file. Verify that pSnapshot is still valid before
64655 ** continuing. Reasons why pSnapshot might no longer be valid:
64656 **
64657 ** (1) The WAL file has been reset since the snapshot was taken.
64658 ** In this case, the salt will have changed.
64659 **
64660 ** (2) A checkpoint as been attempted that wrote frames past
64661 ** pSnapshot->mxFrame into the database file. Note that the
64662 ** checkpoint need not have completed for this to cause problems.
64663 */
64664 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
64665
64666 assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 )((void) (0));
64667 assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame )((void) (0));
64668
64669 /* Check that the wal file has not been wrapped. Assuming that it has
64670 ** not, also check that no checkpointer has attempted to checkpoint any
64671 ** frames beyond pSnapshot->mxFrame. If either of these conditions are
64672 ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr
64673 ** with *pSnapshot and set *pChanged as appropriate for opening the
64674 ** snapshot. */
64675 if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
64676 && pSnapshot->mxFrame>=pInfo->nBackfillAttempted
64677 ){
64678 assert( pWal->readLock>0 )((void) (0));
64679 memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr));
64680 *pChanged = bChanged;
64681 }else{
64682 rc = SQLITE_ERROR_SNAPSHOT(1 | (3<<8));
64683 }
64684
64685 /* A client using a non-current snapshot may not ignore any frames
64686 ** from the start of the wal file. This is because, for a system
64687 ** where (minFrame < iSnapshot < maxFrame), a checkpointer may
64688 ** have omitted to checkpoint a frame earlier than minFrame in
64689 ** the file because there exists a frame after iSnapshot that
64690 ** is the same database page. */
64691 pWal->minFrame = 1;
64692
64693 if( rc!=SQLITE_OK0 ){
64694 sqlite3WalEndReadTransaction(pWal);
64695 }
64696 }
64697 }
64698
64699 /* Release the shared CKPT lock obtained above. */
64700 if( pWal->ckptLock ){
64701 assert( pSnapshot )((void) (0));
64702 walUnlockShared(pWal, WAL_CKPT_LOCK1);
64703 pWal->ckptLock = 0;
64704 }
64705#endif
64706 return rc;
64707}
64708
64709/*
64710** Finish with a read transaction. All this does is release the
64711** read-lock.
64712*/
64713SQLITE_PRIVATEstatic void sqlite3WalEndReadTransaction(Wal *pWal){
64714 sqlite3WalEndWriteTransaction(pWal);
64715 if( pWal->readLock>=0 ){
64716 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)(3+(pWal->readLock)));
64717 pWal->readLock = -1;
64718 }
64719}
64720
64721/*
64722** Search the wal file for page pgno. If found, set *piRead to the frame that
64723** contains the page. Otherwise, if pgno is not in the wal file, set *piRead
64724** to zero.
64725**
64726** Return SQLITE_OK if successful, or an error code if an error occurs. If an
64727** error does occur, the final value of *piRead is undefined.
64728*/
64729SQLITE_PRIVATEstatic int sqlite3WalFindFrame(
64730 Wal *pWal, /* WAL handle */
64731 Pgno pgno, /* Database page number to read data for */
64732 u32 *piRead /* OUT: Frame number (or zero) */
64733){
64734 u32 iRead = 0; /* If !=0, WAL frame to return data from */
64735 u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
64736 int iHash; /* Used to loop through N hash tables */
64737 int iMinHash;
64738
64739 /* This routine is only be called from within a read transaction. */
64740 assert( pWal->readLock>=0 || pWal->lockError )((void) (0));
64741
64742 /* If the "last page" field of the wal-index header snapshot is 0, then
64743 ** no data will be read from the wal under any circumstances. Return early
64744 ** in this case as an optimization. Likewise, if pWal->readLock==0,
64745 ** then the WAL is ignored by the reader so return early, as if the
64746 ** WAL were empty.
64747 */
64748 if( iLast==0 || (pWal->readLock==0 && pWal->bShmUnreliable==0) ){
64749 *piRead = 0;
64750 return SQLITE_OK0;
64751 }
64752
64753 /* Search the hash table or tables for an entry matching page number
64754 ** pgno. Each iteration of the following for() loop searches one
64755 ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
64756 **
64757 ** This code might run concurrently to the code in walIndexAppend()
64758 ** that adds entries to the wal-index (and possibly to this hash
64759 ** table). This means the value just read from the hash
64760 ** slot (aHash[iKey]) may have been added before or after the
64761 ** current read transaction was opened. Values added after the
64762 ** read transaction was opened may have been written incorrectly -
64763 ** i.e. these slots may contain garbage data. However, we assume
64764 ** that any slots written before the current read transaction was
64765 ** opened remain unmodified.
64766 **
64767 ** For the reasons above, the if(...) condition featured in the inner
64768 ** loop of the following block is more stringent that would be required
64769 ** if we had exclusive access to the hash-table:
64770 **
64771 ** (aPgno[iFrame]==pgno):
64772 ** This condition filters out normal hash-table collisions.
64773 **
64774 ** (iFrame<=iLast):
64775 ** This condition filters out entries that were added to the hash
64776 ** table after the current read-transaction had started.
64777 */
64778 iMinHash = walFramePage(pWal->minFrame);
64779 for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){
64780 WalHashLoc sLoc; /* Hash table location */
64781 int iKey; /* Hash slot index */
64782 int nCollide; /* Number of hash collisions remaining */
64783 int rc; /* Error code */
64784 u32 iH;
64785
64786 rc = walHashGet(pWal, iHash, &sLoc);
64787 if( rc!=SQLITE_OK0 ){
64788 return rc;
64789 }
64790 nCollide = HASHTABLE_NSLOT(4096*2);
64791 iKey = walHash(pgno);
64792 while( (iH = AtomicLoad(&sLoc.aHash[iKey])__atomic_load_n((&sLoc.aHash[iKey]),0))!=0 ){
64793 u32 iFrame = iH + sLoc.iZero;
64794 if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH-1]==pgno ){
64795 assert( iFrame>iRead || CORRUPT_DB )((void) (0));
64796 iRead = iFrame;
64797 }
64798 if( (nCollide--)==0 ){
64799 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(64799);
64800 }
64801 iKey = walNextHash(iKey);
64802 }
64803 if( iRead ) break;
64804 }
64805
64806#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
64807 /* If expensive assert() statements are available, do a linear search
64808 ** of the wal-index file content. Make sure the results agree with the
64809 ** result obtained using the hash indexes above. */
64810 {
64811 u32 iRead2 = 0;
64812 u32 iTest;
64813 assert( pWal->bShmUnreliable || pWal->minFrame>0 )((void) (0));
64814 for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){
64815 if( walFramePgno(pWal, iTest)==pgno ){
64816 iRead2 = iTest;
64817 break;
64818 }
64819 }
64820 assert( iRead==iRead2 )((void) (0));
64821 }
64822#endif
64823
64824 *piRead = iRead;
64825 return SQLITE_OK0;
64826}
64827
64828/*
64829** Read the contents of frame iRead from the wal file into buffer pOut
64830** (which is nOut bytes in size). Return SQLITE_OK if successful, or an
64831** error code otherwise.
64832*/
64833SQLITE_PRIVATEstatic int sqlite3WalReadFrame(
64834 Wal *pWal, /* WAL handle */
64835 u32 iRead, /* Frame to read */
64836 int nOut, /* Size of buffer pOut in bytes */
64837 u8 *pOut /* Buffer to write page data to */
64838){
64839 int sz;
64840 i64 iOffset;
64841 sz = pWal->hdr.szPage;
64842 sz = (sz&0xfe00) + ((sz&0x0001)<<16);
64843 testcase( sz<=32768 );
64844 testcase( sz>=65536 );
64845 iOffset = walFrameOffset(iRead, sz)( 32 + ((iRead)-1)*(i64)((sz)+24) ) + WAL_FRAME_HDRSIZE24;
64846 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
64847 return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
64848}
64849
64850/*
64851** Return the size of the database in pages (or zero, if unknown).
64852*/
64853SQLITE_PRIVATEstatic Pgno sqlite3WalDbsize(Wal *pWal){
64854 if( pWal && ALWAYS(pWal->readLock>=0)(pWal->readLock>=0) ){
64855 return pWal->hdr.nPage;
64856 }
64857 return 0;
64858}
64859
64860
64861/*
64862** This function starts a write transaction on the WAL.
64863**
64864** A read transaction must have already been started by a prior call
64865** to sqlite3WalBeginReadTransaction().
64866**
64867** If another thread or process has written into the database since
64868** the read transaction was started, then it is not possible for this
64869** thread to write as doing so would cause a fork. So this routine
64870** returns SQLITE_BUSY in that case and no write transaction is started.
64871**
64872** There can only be a single writer active at a time.
64873*/
64874SQLITE_PRIVATEstatic int sqlite3WalBeginWriteTransaction(Wal *pWal){
64875 int rc;
64876
64877#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
64878 /* If the write-lock is already held, then it was obtained before the
64879 ** read-transaction was even opened, making this call a no-op.
64880 ** Return early. */
64881 if( pWal->writeLock ){
64882 assert( !memcmp(&pWal->hdr,(void *)walIndexHdr(pWal),sizeof(WalIndexHdr)) )((void) (0));
64883 return SQLITE_OK0;
64884 }
64885#endif
64886
64887 /* Cannot start a write transaction without first holding a read
64888 ** transaction. */
64889 assert( pWal->readLock>=0 )((void) (0));
64890 assert( pWal->writeLock==0 && pWal->iReCksum==0 )((void) (0));
64891
64892 if( pWal->readOnly ){
64893 return SQLITE_READONLY8;
64894 }
64895
64896 /* Only one writer allowed at a time. Get the write lock. Return
64897 ** SQLITE_BUSY if unable.
64898 */
64899 rc = walLockExclusive(pWal, WAL_WRITE_LOCK0, 1);
64900 if( rc ){
64901 return rc;
64902 }
64903 pWal->writeLock = 1;
64904
64905 /* If another connection has written to the database file since the
64906 ** time the read transaction on this connection was started, then
64907 ** the write is disallowed.
64908 */
64909 if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
64910 walUnlockExclusive(pWal, WAL_WRITE_LOCK0, 1);
64911 pWal->writeLock = 0;
64912 rc = SQLITE_BUSY_SNAPSHOT(5 | (2<<8));
64913 }
64914
64915 return rc;
64916}
64917
64918/*
64919** End a write transaction. The commit has already been done. This
64920** routine merely releases the lock.
64921*/
64922SQLITE_PRIVATEstatic int sqlite3WalEndWriteTransaction(Wal *pWal){
64923 if( pWal->writeLock ){
64924 walUnlockExclusive(pWal, WAL_WRITE_LOCK0, 1);
64925 pWal->writeLock = 0;
64926 pWal->iReCksum = 0;
64927 pWal->truncateOnCommit = 0;
64928 }
64929 return SQLITE_OK0;
64930}
64931
64932/*
64933** If any data has been written (but not committed) to the log file, this
64934** function moves the write-pointer back to the start of the transaction.
64935**
64936** Additionally, the callback function is invoked for each frame written
64937** to the WAL since the start of the transaction. If the callback returns
64938** other than SQLITE_OK, it is not invoked again and the error code is
64939** returned to the caller.
64940**
64941** Otherwise, if the callback function does not return an error, this
64942** function returns SQLITE_OK.
64943*/
64944SQLITE_PRIVATEstatic int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
64945 int rc = SQLITE_OK0;
64946 if( ALWAYS(pWal->writeLock)(pWal->writeLock) ){
64947 Pgno iMax = pWal->hdr.mxFrame;
64948 Pgno iFrame;
64949
64950 /* Restore the clients cache of the wal-index header to the state it
64951 ** was in before the client began writing to the database.
64952 */
64953 memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
64954
64955 for(iFrame=pWal->hdr.mxFrame+1;
64956 ALWAYS(rc==SQLITE_OK)(rc==0) && iFrame<=iMax;
64957 iFrame++
64958 ){
64959 /* This call cannot fail. Unless the page for which the page number
64960 ** is passed as the second argument is (a) in the cache and
64961 ** (b) has an outstanding reference, then xUndo is either a no-op
64962 ** (if (a) is false) or simply expels the page from the cache (if (b)
64963 ** is false).
64964 **
64965 ** If the upper layer is doing a rollback, it is guaranteed that there
64966 ** are no outstanding references to any page other than page 1. And
64967 ** page 1 is never written to the log until the transaction is
64968 ** committed. As a result, the call to xUndo may not fail.
64969 */
64970 assert( walFramePgno(pWal, iFrame)!=1 )((void) (0));
64971 rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
64972 }
64973 if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
64974 }
64975 return rc;
64976}
64977
64978/*
64979** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32
64980** values. This function populates the array with values required to
64981** "rollback" the write position of the WAL handle back to the current
64982** point in the event of a savepoint rollback (via WalSavepointUndo()).
64983*/
64984SQLITE_PRIVATEstatic void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){
64985 assert( pWal->writeLock )((void) (0));
64986 aWalData[0] = pWal->hdr.mxFrame;
64987 aWalData[1] = pWal->hdr.aFrameCksum[0];
64988 aWalData[2] = pWal->hdr.aFrameCksum[1];
64989 aWalData[3] = pWal->nCkpt;
64990}
64991
64992/*
64993** Move the write position of the WAL back to the point identified by
64994** the values in the aWalData[] array. aWalData must point to an array
64995** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated
64996** by a call to WalSavepoint().
64997*/
64998SQLITE_PRIVATEstatic int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){
64999 int rc = SQLITE_OK0;
65000
65001 assert( pWal->writeLock )((void) (0));
65002 assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame )((void) (0));
65003
65004 if( aWalData[3]!=pWal->nCkpt ){
65005 /* This savepoint was opened immediately after the write-transaction
65006 ** was started. Right after that, the writer decided to wrap around
65007 ** to the start of the log. Update the savepoint values to match.
65008 */
65009 aWalData[0] = 0;
65010 aWalData[3] = pWal->nCkpt;
65011 }
65012
65013 if( aWalData[0]<pWal->hdr.mxFrame ){
65014 pWal->hdr.mxFrame = aWalData[0];
65015 pWal->hdr.aFrameCksum[0] = aWalData[1];
65016 pWal->hdr.aFrameCksum[1] = aWalData[2];
65017 walCleanupHash(pWal);
65018 }
65019
65020 return rc;
65021}
65022
65023/*
65024** This function is called just before writing a set of frames to the log
65025** file (see sqlite3WalFrames()). It checks to see if, instead of appending
65026** to the current log file, it is possible to overwrite the start of the
65027** existing log file with the new frames (i.e. "reset" the log). If so,
65028** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
65029** unchanged.
65030**
65031** SQLITE_OK is returned if no error is encountered (regardless of whether
65032** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
65033** if an error occurs.
65034*/
65035static int walRestartLog(Wal *pWal){
65036 int rc = SQLITE_OK0;
65037 int cnt;
65038
65039 if( pWal->readLock==0 ){
65040 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
65041 assert( pInfo->nBackfill==pWal->hdr.mxFrame )((void) (0));
65042 if( pInfo->nBackfill>0 ){
65043 u32 salt1;
65044 sqlite3_randomness(4, &salt1);
65045 rc = walLockExclusive(pWal, WAL_READ_LOCK(1)(3+(1)), WAL_NREADER(8 -3)-1);
65046 if( rc==SQLITE_OK0 ){
65047 /* If all readers are using WAL_READ_LOCK(0) (in other words if no
65048 ** readers are currently using the WAL), then the transactions
65049 ** frames will overwrite the start of the existing log. Update the
65050 ** wal-index header to reflect this.
65051 **
65052 ** In theory it would be Ok to update the cache of the header only
65053 ** at this point. But updating the actual wal-index header is also
65054 ** safe and means there is no special case for sqlite3WalUndo()
65055 ** to handle if this transaction is rolled back. */
65056 walRestartHdr(pWal, salt1);
65057 walUnlockExclusive(pWal, WAL_READ_LOCK(1)(3+(1)), WAL_NREADER(8 -3)-1);
65058 }else if( rc!=SQLITE_BUSY5 ){
65059 return rc;
65060 }
65061 }
65062 walUnlockShared(pWal, WAL_READ_LOCK(0)(3+(0)));
65063 pWal->readLock = -1;
65064 cnt = 0;
65065 do{
65066 int notUsed;
65067 rc = walTryBeginRead(pWal, &notUsed, 1, ++cnt);
65068 }while( rc==WAL_RETRY(-1) );
65069 assert( (rc&0xff)!=SQLITE_BUSY )((void) (0)); /* BUSY not possible when useWal==1 */
65070 testcase( (rc&0xff)==SQLITE_IOERR );
65071 testcase( rc==SQLITE_PROTOCOL );
65072 testcase( rc==SQLITE_OK );
65073 }
65074 return rc;
65075}
65076
65077/*
65078** Information about the current state of the WAL file and where
65079** the next fsync should occur - passed from sqlite3WalFrames() into
65080** walWriteToLog().
65081*/
65082typedef struct WalWriter {
65083 Wal *pWal; /* The complete WAL information */
65084 sqlite3_file *pFd; /* The WAL file to which we write */
65085 sqlite3_int64 iSyncPoint; /* Fsync at this offset */
65086 int syncFlags; /* Flags for the fsync */
65087 int szPage; /* Size of one page */
65088} WalWriter;
65089
65090/*
65091** Write iAmt bytes of content into the WAL file beginning at iOffset.
65092** Do a sync when crossing the p->iSyncPoint boundary.
65093**
65094** In other words, if iSyncPoint is in between iOffset and iOffset+iAmt,
65095** first write the part before iSyncPoint, then sync, then write the
65096** rest.
65097*/
65098static int walWriteToLog(
65099 WalWriter *p, /* WAL to write to */
65100 void *pContent, /* Content to be written */
65101 int iAmt, /* Number of bytes to write */
65102 sqlite3_int64 iOffset /* Start writing at this offset */
65103){
65104 int rc;
65105 if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
65106 int iFirstAmt = (int)(p->iSyncPoint - iOffset);
65107 rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
65108 if( rc ) return rc;
65109 iOffset += iFirstAmt;
65110 iAmt -= iFirstAmt;
65111 pContent = (void*)(iFirstAmt + (char*)pContent);
65112 assert( WAL_SYNC_FLAGS(p->syncFlags)!=0 )((void) (0));
65113 rc = sqlite3OsSync(p->pFd, WAL_SYNC_FLAGS(p->syncFlags)((p->syncFlags)&0x03));
65114 if( iAmt==0 || rc ) return rc;
65115 }
65116 rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
65117 return rc;
65118}
65119
65120/*
65121** Write out a single frame of the WAL
65122*/
65123static int walWriteOneFrame(
65124 WalWriter *p, /* Where to write the frame */
65125 PgHdr *pPage, /* The page of the frame to be written */
65126 int nTruncate, /* The commit flag. Usually 0. >0 for commit */
65127 sqlite3_int64 iOffset /* Byte offset at which to write */
65128){
65129 int rc; /* Result code from subfunctions */
65130 void *pData; /* Data actually written */
65131 u8 aFrame[WAL_FRAME_HDRSIZE24]; /* Buffer to assemble frame-header in */
65132 pData = pPage->pData;
65133 walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
65134 rc = walWriteToLog(p, aFrame, sizeof(aFrame), iOffset);
65135 if( rc ) return rc;
65136 /* Write the page data */
65137 rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
65138 return rc;
65139}
65140
65141/*
65142** This function is called as part of committing a transaction within which
65143** one or more frames have been overwritten. It updates the checksums for
65144** all frames written to the wal file by the current transaction starting
65145** with the earliest to have been overwritten.
65146**
65147** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
65148*/
65149static int walRewriteChecksums(Wal *pWal, u32 iLast){
65150 const int szPage = pWal->szPage;/* Database page size */
65151 int rc = SQLITE_OK0; /* Return code */
65152 u8 *aBuf; /* Buffer to load data from wal file into */
65153 u8 aFrame[WAL_FRAME_HDRSIZE24]; /* Buffer to assemble frame-headers in */
65154 u32 iRead; /* Next frame to read from wal file */
65155 i64 iCksumOff;
65156
65157 aBuf = sqlite3_malloc(szPage + WAL_FRAME_HDRSIZE24);
65158 if( aBuf==0 ) return SQLITE_NOMEM_BKPT7;
65159
65160 /* Find the checksum values to use as input for the recalculating the
65161 ** first checksum. If the first frame is frame 1 (implying that the current
65162 ** transaction restarted the wal file), these values must be read from the
65163 ** wal-file header. Otherwise, read them from the frame header of the
65164 ** previous frame. */
65165 assert( pWal->iReCksum>0 )((void) (0));
65166 if( pWal->iReCksum==1 ){
65167 iCksumOff = 24;
65168 }else{
65169 iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage)( 32 + ((pWal->iReCksum-1)-1)*(i64)((szPage)+24) ) + 16;
65170 }
65171 rc = sqlite3OsRead(pWal->pWalFd, aBuf, sizeof(u32)*2, iCksumOff);
65172 pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(aBuf);
65173 pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(&aBuf[sizeof(u32)]);
65174
65175 iRead = pWal->iReCksum;
65176 pWal->iReCksum = 0;
65177 for(; rc==SQLITE_OK0 && iRead<=iLast; iRead++){
65178 i64 iOff = walFrameOffset(iRead, szPage)( 32 + ((iRead)-1)*(i64)((szPage)+24) );
65179 rc = sqlite3OsRead(pWal->pWalFd, aBuf, szPage+WAL_FRAME_HDRSIZE24, iOff);
65180 if( rc==SQLITE_OK0 ){
65181 u32 iPgno, nDbSize;
65182 iPgno = sqlite3Get4byte(aBuf);
65183 nDbSize = sqlite3Get4byte(&aBuf[4]);
65184
65185 walEncodeFrame(pWal, iPgno, nDbSize, &aBuf[WAL_FRAME_HDRSIZE24], aFrame);
65186 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOff);
65187 }
65188 }
65189
65190 sqlite3_free(aBuf);
65191 return rc;
65192}
65193
65194/*
65195** Write a set of frames to the log. The caller must hold the write-lock
65196** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
65197*/
65198SQLITE_PRIVATEstatic int sqlite3WalFrames(
65199 Wal *pWal, /* Wal handle to write to */
65200 int szPage, /* Database page-size in bytes */
65201 PgHdr *pList, /* List of dirty pages to write */
65202 Pgno nTruncate, /* Database size after this commit */
65203 int isCommit, /* True if this is a commit */
65204 int sync_flags /* Flags to pass to OsSync() (or 0) */
65205){
65206 int rc; /* Used to catch return codes */
65207 u32 iFrame; /* Next frame address */
65208 PgHdr *p; /* Iterator to run through pList with. */
65209 PgHdr *pLast = 0; /* Last frame in list */
65210 int nExtra = 0; /* Number of extra copies of last page */
65211 int szFrame; /* The size of a single frame */
65212 i64 iOffset; /* Next byte to write in WAL file */
65213 WalWriter w; /* The writer */
65214 u32 iFirst = 0; /* First frame that may be overwritten */
65215 WalIndexHdr *pLive; /* Pointer to shared header */
65216
65217 assert( pList )((void) (0));
65218 assert( pWal->writeLock )((void) (0));
65219
65220 /* If this frame set completes a transaction, then nTruncate>0. If
65221 ** nTruncate==0 then this frame set does not complete the transaction. */
65222 assert( (isCommit!=0)==(nTruncate!=0) )((void) (0));
65223
65224#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
65225 { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
65226 WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
65227 pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
65228 }
65229#endif
65230
65231 pLive = (WalIndexHdr*)walIndexHdr(pWal);
65232 if( memcmp(&pWal->hdr, (void *)pLive, sizeof(WalIndexHdr))!=0 ){
65233 iFirst = pLive->mxFrame+1;
65234 }
65235
65236 /* See if it is possible to write these frames into the start of the
65237 ** log file, instead of appending to it at pWal->hdr.mxFrame.
65238 */
65239 if( SQLITE_OK0!=(rc = walRestartLog(pWal)) ){
65240 return rc;
65241 }
65242
65243 /* If this is the first frame written into the log, write the WAL
65244 ** header to the start of the WAL file. See comments at the top of
65245 ** this source file for a description of the WAL header format.
65246 */
65247 iFrame = pWal->hdr.mxFrame;
65248 if( iFrame==0 ){
65249 u8 aWalHdr[WAL_HDRSIZE32]; /* Buffer to assemble wal-header in */
65250 u32 aCksum[2]; /* Checksum for wal-header */
65251
65252 sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC0x377f0682 | SQLITE_BIGENDIAN0));
65253 sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION3007000);
65254 sqlite3Put4byte(&aWalHdr[8], szPage);
65255 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
65256 if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
65257 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
65258 walChecksumBytes(1, aWalHdr, WAL_HDRSIZE32-2*4, 0, aCksum);
65259 sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
65260 sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
65261
65262 pWal->szPage = szPage;
65263 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN0;
65264 pWal->hdr.aFrameCksum[0] = aCksum[0];
65265 pWal->hdr.aFrameCksum[1] = aCksum[1];
65266 pWal->truncateOnCommit = 1;
65267
65268 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
65269 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
65270 if( rc!=SQLITE_OK0 ){
65271 return rc;
65272 }
65273
65274 /* Sync the header (unless SQLITE_IOCAP_SEQUENTIAL is true or unless
65275 ** all syncing is turned off by PRAGMA synchronous=OFF). Otherwise
65276 ** an out-of-order write following a WAL restart could result in
65277 ** database corruption. See the ticket:
65278 **
65279 ** https://sqlite.org/src/info/ff5be73dee
65280 */
65281 if( pWal->syncHeader ){
65282 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags)(((sync_flags)>>2)&0x03));
65283 if( rc ) return rc;
65284 }
65285 }
65286 assert( (int)pWal->szPage==szPage )((void) (0));
65287
65288 /* Setup information needed to write frames into the WAL */
65289 w.pWal = pWal;
65290 w.pFd = pWal->pWalFd;
65291 w.iSyncPoint = 0;
65292 w.syncFlags = sync_flags;
65293 w.szPage = szPage;
65294 iOffset = walFrameOffset(iFrame+1, szPage)( 32 + ((iFrame+1)-1)*(i64)((szPage)+24) );
65295 szFrame = szPage + WAL_FRAME_HDRSIZE24;
65296
65297 /* Write all frames into the log file exactly once */
65298 for(p=pList; p; p=p->pDirty){
65299 int nDbSize; /* 0 normally. Positive == commit flag */
65300
65301 /* Check if this page has already been written into the wal file by
65302 ** the current transaction. If so, overwrite the existing frame and
65303 ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that
65304 ** checksums must be recomputed when the transaction is committed. */
65305 if( iFirst && (p->pDirty || isCommit==0) ){
65306 u32 iWrite = 0;
65307 VVA_ONLY(rc =) sqlite3WalFindFrame(pWal, p->pgno, &iWrite);
65308 assert( rc==SQLITE_OK || iWrite==0 )((void) (0));
65309 if( iWrite>=iFirst ){
65310 i64 iOff = walFrameOffset(iWrite, szPage)( 32 + ((iWrite)-1)*(i64)((szPage)+24) ) + WAL_FRAME_HDRSIZE24;
65311 void *pData;
65312 if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
65313 pWal->iReCksum = iWrite;
65314 }
65315 pData = p->pData;
65316 rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
65317 if( rc ) return rc;
65318 p->flags &= ~PGHDR_WAL_APPEND0x040;
65319 continue;
65320 }
65321 }
65322
65323 iFrame++;
65324 assert( iOffset==walFrameOffset(iFrame, szPage) )((void) (0));
65325 nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
65326 rc = walWriteOneFrame(&w, p, nDbSize, iOffset);
65327 if( rc ) return rc;
65328 pLast = p;
65329 iOffset += szFrame;
65330 p->flags |= PGHDR_WAL_APPEND0x040;
65331 }
65332
65333 /* Recalculate checksums within the wal file if required. */
65334 if( isCommit && pWal->iReCksum ){
65335 rc = walRewriteChecksums(pWal, iFrame);
65336 if( rc ) return rc;
65337 }
65338
65339 /* If this is the end of a transaction, then we might need to pad
65340 ** the transaction and/or sync the WAL file.
65341 **
65342 ** Padding and syncing only occur if this set of frames complete a
65343 ** transaction and if PRAGMA synchronous=FULL. If synchronous==NORMAL
65344 ** or synchronous==OFF, then no padding or syncing are needed.
65345 **
65346 ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not
65347 ** needed and only the sync is done. If padding is needed, then the
65348 ** final frame is repeated (with its commit mark) until the next sector
65349 ** boundary is crossed. Only the part of the WAL prior to the last
65350 ** sector boundary is synced; the part of the last frame that extends
65351 ** past the sector boundary is written after the sync.
65352 */
65353 if( isCommit && WAL_SYNC_FLAGS(sync_flags)((sync_flags)&0x03)!=0 ){
65354 int bSync = 1;
65355 if( pWal->padToSectorBoundary ){
65356 int sectorSize = sqlite3SectorSize(pWal->pWalFd);
65357 w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
65358 bSync = (w.iSyncPoint==iOffset);
65359 testcase( bSync );
65360 while( iOffset<w.iSyncPoint ){
65361 rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
65362 if( rc ) return rc;
65363 iOffset += szFrame;
65364 nExtra++;
65365 assert( pLast!=0 )((void) (0));
65366 }
65367 }
65368 if( bSync ){
65369 assert( rc==SQLITE_OK )((void) (0));
65370 rc = sqlite3OsSync(w.pFd, WAL_SYNC_FLAGS(sync_flags)((sync_flags)&0x03));
65371 }
65372 }
65373
65374 /* If this frame set completes the first transaction in the WAL and
65375 ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
65376 ** journal size limit, if possible.
65377 */
65378 if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
65379 i64 sz = pWal->mxWalSize;
65380 if( walFrameOffset(iFrame+nExtra+1, szPage)( 32 + ((iFrame+nExtra+1)-1)*(i64)((szPage)+24) )>pWal->mxWalSize ){
65381 sz = walFrameOffset(iFrame+nExtra+1, szPage)( 32 + ((iFrame+nExtra+1)-1)*(i64)((szPage)+24) );
65382 }
65383 walLimitSize(pWal, sz);
65384 pWal->truncateOnCommit = 0;
65385 }
65386
65387 /* Append data to the wal-index. It is not necessary to lock the
65388 ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
65389 ** guarantees that there are no other writers, and no data that may
65390 ** be in use by existing readers is being overwritten.
65391 */
65392 iFrame = pWal->hdr.mxFrame;
65393 for(p=pList; p && rc==SQLITE_OK0; p=p->pDirty){
65394 if( (p->flags & PGHDR_WAL_APPEND0x040)==0 ) continue;
65395 iFrame++;
65396 rc = walIndexAppend(pWal, iFrame, p->pgno);
65397 }
65398 assert( pLast!=0 || nExtra==0 )((void) (0));
65399 while( rc==SQLITE_OK0 && nExtra>0 ){
65400 iFrame++;
65401 nExtra--;
65402 rc = walIndexAppend(pWal, iFrame, pLast->pgno);
65403 }
65404
65405 if( rc==SQLITE_OK0 ){
65406 /* Update the private copy of the header. */
65407 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
65408 testcase( szPage<=32768 );
65409 testcase( szPage>=65536 );
65410 pWal->hdr.mxFrame = iFrame;
65411 if( isCommit ){
65412 pWal->hdr.iChange++;
65413 pWal->hdr.nPage = nTruncate;
65414 }
65415 /* If this is a commit, update the wal-index header too. */
65416 if( isCommit ){
65417 walIndexWriteHdr(pWal);
65418 pWal->iCallback = iFrame;
65419 }
65420 }
65421
65422 WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
65423 return rc;
65424}
65425
65426/*
65427** This routine is called to implement sqlite3_wal_checkpoint() and
65428** related interfaces.
65429**
65430** Obtain a CHECKPOINT lock and then backfill as much information as
65431** we can from WAL into the database.
65432**
65433** If parameter xBusy is not NULL, it is a pointer to a busy-handler
65434** callback. In this case this function runs a blocking checkpoint.
65435*/
65436SQLITE_PRIVATEstatic int sqlite3WalCheckpoint(
65437 Wal *pWal, /* Wal connection */
65438 sqlite3 *db, /* Check this handle's interrupt flag */
65439 int eMode, /* PASSIVE, FULL, RESTART, or TRUNCATE */
65440 int (*xBusy)(void*), /* Function to call when busy */
65441 void *pBusyArg, /* Context argument for xBusyHandler */
65442 int sync_flags, /* Flags to sync db file with (or 0) */
65443 int nBuf, /* Size of temporary buffer */
65444 u8 *zBuf, /* Temporary buffer to use */
65445 int *pnLog, /* OUT: Number of frames in WAL */
65446 int *pnCkpt /* OUT: Number of backfilled frames in WAL */
65447){
65448 int rc; /* Return code */
65449 int isChanged = 0; /* True if a new wal-index header is loaded */
65450 int eMode2 = eMode; /* Mode to pass to walCheckpoint() */
65451 int (*xBusy2)(void*) = xBusy; /* Busy handler for eMode2 */
65452
65453 assert( pWal->ckptLock==0 )((void) (0));
65454 assert( pWal->writeLock==0 )((void) (0));
65455
65456 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
65457 ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
65458 assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 )((void) (0));
65459
65460 if( pWal->readOnly ) return SQLITE_READONLY8;
65461 WALTRACE(("WAL%p: checkpoint begins\n", pWal));
65462
65463 /* Enable blocking locks, if possible. If blocking locks are successfully
65464 ** enabled, set xBusy2=0 so that the busy-handler is never invoked. */
65465 sqlite3WalDb(pWal, db);
65466 (void)walEnableBlocking(pWal)0;
65467
65468 /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
65469 ** "checkpoint" lock on the database file.
65470 ** EVIDENCE-OF: R-10421-19736 If any other process is running a
65471 ** checkpoint operation at the same time, the lock cannot be obtained and
65472 ** SQLITE_BUSY is returned.
65473 ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
65474 ** it will not be invoked in this case.
65475 */
65476 rc = walLockExclusive(pWal, WAL_CKPT_LOCK1, 1);
65477 testcase( rc==SQLITE_BUSY );
65478 testcase( rc!=SQLITE_OK && xBusy2!=0 );
65479 if( rc==SQLITE_OK0 ){
65480 pWal->ckptLock = 1;
65481
65482 /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
65483 ** TRUNCATE modes also obtain the exclusive "writer" lock on the database
65484 ** file.
65485 **
65486 ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
65487 ** immediately, and a busy-handler is configured, it is invoked and the
65488 ** writer lock retried until either the busy-handler returns 0 or the
65489 ** lock is successfully obtained.
65490 */
65491 if( eMode!=SQLITE_CHECKPOINT_PASSIVE0 ){
65492 rc = walBusyLock(pWal, xBusy2, pBusyArg, WAL_WRITE_LOCK0, 1);
65493 if( rc==SQLITE_OK0 ){
65494 pWal->writeLock = 1;
65495 }else if( rc==SQLITE_BUSY5 ){
65496 eMode2 = SQLITE_CHECKPOINT_PASSIVE0;
65497 xBusy2 = 0;
65498 rc = SQLITE_OK0;
65499 }
65500 }
65501 }
65502
65503
65504 /* Read the wal-index header. */
65505 if( rc==SQLITE_OK0 ){
65506 walDisableBlocking(pWal);
65507 rc = walIndexReadHdr(pWal, &isChanged);
65508 (void)walEnableBlocking(pWal)0;
65509 if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
65510 sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
65511 }
65512 }
65513
65514 /* Copy data from the log to the database file. */
65515 if( rc==SQLITE_OK0 ){
65516
65517 if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
65518 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(65518);
65519 }else{
65520 rc = walCheckpoint(pWal, db, eMode2, xBusy2, pBusyArg, sync_flags, zBuf);
65521 }
65522
65523 /* If no error occurred, set the output variables. */
65524 if( rc==SQLITE_OK0 || rc==SQLITE_BUSY5 ){
65525 if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
65526 if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
65527 }
65528 }
65529
65530 if( isChanged ){
65531 /* If a new wal-index header was loaded before the checkpoint was
65532 ** performed, then the pager-cache associated with pWal is now
65533 ** out of date. So zero the cached wal-index header to ensure that
65534 ** next time the pager opens a snapshot on this database it knows that
65535 ** the cache needs to be reset.
65536 */
65537 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
65538 }
65539
65540 walDisableBlocking(pWal);
65541 sqlite3WalDb(pWal, 0);
65542
65543 /* Release the locks. */
65544 sqlite3WalEndWriteTransaction(pWal);
65545 if( pWal->ckptLock ){
65546 walUnlockExclusive(pWal, WAL_CKPT_LOCK1, 1);
65547 pWal->ckptLock = 0;
65548 }
65549 WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
65550#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
65551 if( rc==SQLITE_BUSY_TIMEOUT(5 | (3<<8)) ) rc = SQLITE_BUSY5;
65552#endif
65553 return (rc==SQLITE_OK0 && eMode!=eMode2 ? SQLITE_BUSY5 : rc);
65554}
65555
65556/* Return the value to pass to a sqlite3_wal_hook callback, the
65557** number of frames in the WAL at the point of the last commit since
65558** sqlite3WalCallback() was called. If no commits have occurred since
65559** the last call, then return 0.
65560*/
65561SQLITE_PRIVATEstatic int sqlite3WalCallback(Wal *pWal){
65562 u32 ret = 0;
65563 if( pWal ){
65564 ret = pWal->iCallback;
65565 pWal->iCallback = 0;
65566 }
65567 return (int)ret;
65568}
65569
65570/*
65571** This function is called to change the WAL subsystem into or out
65572** of locking_mode=EXCLUSIVE.
65573**
65574** If op is zero, then attempt to change from locking_mode=EXCLUSIVE
65575** into locking_mode=NORMAL. This means that we must acquire a lock
65576** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL
65577** or if the acquisition of the lock fails, then return 0. If the
65578** transition out of exclusive-mode is successful, return 1. This
65579** operation must occur while the pager is still holding the exclusive
65580** lock on the main database file.
65581**
65582** If op is one, then change from locking_mode=NORMAL into
65583** locking_mode=EXCLUSIVE. This means that the pWal->readLock must
65584** be released. Return 1 if the transition is made and 0 if the
65585** WAL is already in exclusive-locking mode - meaning that this
65586** routine is a no-op. The pager must already hold the exclusive lock
65587** on the main database file before invoking this operation.
65588**
65589** If op is negative, then do a dry-run of the op==1 case but do
65590** not actually change anything. The pager uses this to see if it
65591** should acquire the database exclusive lock prior to invoking
65592** the op==1 case.
65593*/
65594SQLITE_PRIVATEstatic int sqlite3WalExclusiveMode(Wal *pWal, int op){
65595 int rc;
65596 assert( pWal->writeLock==0 )((void) (0));
65597 assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 )((void) (0));
65598
65599 /* pWal->readLock is usually set, but might be -1 if there was a
65600 ** prior error while attempting to acquire are read-lock. This cannot
65601 ** happen if the connection is actually in exclusive mode (as no xShmLock
65602 ** locks are taken in this case). Nor should the pager attempt to
65603 ** upgrade to exclusive-mode following such an error.
65604 */
65605 assert( pWal->readLock>=0 || pWal->lockError )((void) (0));
65606 assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) )((void) (0));
65607
65608 if( op==0 ){
65609 if( pWal->exclusiveMode!=WAL_NORMAL_MODE0 ){
65610 pWal->exclusiveMode = WAL_NORMAL_MODE0;
65611 if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock)(3+(pWal->readLock)))!=SQLITE_OK0 ){
65612 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE1;
65613 }
65614 rc = pWal->exclusiveMode==WAL_NORMAL_MODE0;
65615 }else{
65616 /* Already in locking_mode=NORMAL */
65617 rc = 0;
65618 }
65619 }else if( op>0 ){
65620 assert( pWal->exclusiveMode==WAL_NORMAL_MODE )((void) (0));
65621 assert( pWal->readLock>=0 )((void) (0));
65622 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)(3+(pWal->readLock)));
65623 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE1;
65624 rc = 1;
65625 }else{
65626 rc = pWal->exclusiveMode==WAL_NORMAL_MODE0;
65627 }
65628 return rc;
65629}
65630
65631/*
65632** Return true if the argument is non-NULL and the WAL module is using
65633** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
65634** WAL module is using shared-memory, return false.
65635*/
65636SQLITE_PRIVATEstatic int sqlite3WalHeapMemory(Wal *pWal){
65637 return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE2 );
65638}
65639
65640#ifdef SQLITE_ENABLE_SNAPSHOT1
65641/* Create a snapshot object. The content of a snapshot is opaque to
65642** every other subsystem, so the WAL module can put whatever it needs
65643** in the object.
65644*/
65645SQLITE_PRIVATEstatic int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot){
65646 int rc = SQLITE_OK0;
65647 WalIndexHdr *pRet;
65648 static const u32 aZero[4] = { 0, 0, 0, 0 };
65649
65650 assert( pWal->readLock>=0 && pWal->writeLock==0 )((void) (0));
65651
65652 if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){
65653 *ppSnapshot = 0;
65654 return SQLITE_ERROR1;
65655 }
65656 pRet = (WalIndexHdr*)sqlite3_malloc(sizeof(WalIndexHdr));
65657 if( pRet==0 ){
65658 rc = SQLITE_NOMEM_BKPT7;
65659 }else{
65660 memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
65661 *ppSnapshot = (sqlite3_snapshot*)pRet;
65662 }
65663
65664 return rc;
65665}
65666
65667/* Try to open on pSnapshot when the next read-transaction starts
65668*/
65669SQLITE_PRIVATEstatic void sqlite3WalSnapshotOpen(
65670 Wal *pWal,
65671 sqlite3_snapshot *pSnapshot
65672){
65673 pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
65674}
65675
65676/*
65677** Return a +ve value if snapshot p1 is newer than p2. A -ve value if
65678** p1 is older than p2 and zero if p1 and p2 are the same snapshot.
65679*/
65680SQLITE_API int sqlite3_snapshot_cmp(sqlite3_snapshot *p1, sqlite3_snapshot *p2){
65681 WalIndexHdr *pHdr1 = (WalIndexHdr*)p1;
65682 WalIndexHdr *pHdr2 = (WalIndexHdr*)p2;
65683
65684 /* aSalt[0] is a copy of the value stored in the wal file header. It
65685 ** is incremented each time the wal file is restarted. */
65686 if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1;
65687 if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1;
65688 if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1;
65689 if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1;
65690 return 0;
65691}
65692
65693/*
65694** The caller currently has a read transaction open on the database.
65695** This function takes a SHARED lock on the CHECKPOINTER slot and then
65696** checks if the snapshot passed as the second argument is still
65697** available. If so, SQLITE_OK is returned.
65698**
65699** If the snapshot is not available, SQLITE_ERROR is returned. Or, if
65700** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error
65701** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER
65702** lock is released before returning.
65703*/
65704SQLITE_PRIVATEstatic int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot){
65705 int rc;
65706 rc = walLockShared(pWal, WAL_CKPT_LOCK1);
65707 if( rc==SQLITE_OK0 ){
65708 WalIndexHdr *pNew = (WalIndexHdr*)pSnapshot;
65709 if( memcmp(pNew->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
65710 || pNew->mxFrame<walCkptInfo(pWal)->nBackfillAttempted
65711 ){
65712 rc = SQLITE_ERROR_SNAPSHOT(1 | (3<<8));
65713 walUnlockShared(pWal, WAL_CKPT_LOCK1);
65714 }
65715 }
65716 return rc;
65717}
65718
65719/*
65720** Release a lock obtained by an earlier successful call to
65721** sqlite3WalSnapshotCheck().
65722*/
65723SQLITE_PRIVATEstatic void sqlite3WalSnapshotUnlock(Wal *pWal){
65724 assert( pWal )((void) (0));
65725 walUnlockShared(pWal, WAL_CKPT_LOCK1);
65726}
65727
65728
65729#endif /* SQLITE_ENABLE_SNAPSHOT */
65730
65731#ifdef SQLITE_ENABLE_ZIPVFS
65732/*
65733** If the argument is not NULL, it points to a Wal object that holds a
65734** read-lock. This function returns the database page-size if it is known,
65735** or zero if it is not (or if pWal is NULL).
65736*/
65737SQLITE_PRIVATEstatic int sqlite3WalFramesize(Wal *pWal){
65738 assert( pWal==0 || pWal->readLock>=0 )((void) (0));
65739 return (pWal ? pWal->szPage : 0);
65740}
65741#endif
65742
65743/* Return the sqlite3_file object for the WAL file
65744*/
65745SQLITE_PRIVATEstatic sqlite3_file *sqlite3WalFile(Wal *pWal){
65746 return pWal->pWalFd;
65747}
65748
65749#endif /* #ifndef SQLITE_OMIT_WAL */
65750
65751/************** End of wal.c *************************************************/
65752/************** Begin file btmutex.c *****************************************/
65753/*
65754** 2007 August 27
65755**
65756** The author disclaims copyright to this source code. In place of
65757** a legal notice, here is a blessing:
65758**
65759** May you do good and not evil.
65760** May you find forgiveness for yourself and forgive others.
65761** May you share freely, never taking more than you give.
65762**
65763*************************************************************************
65764**
65765** This file contains code used to implement mutexes on Btree objects.
65766** This code really belongs in btree.c. But btree.c is getting too
65767** big and we want to break it down some. This packaged seemed like
65768** a good breakout.
65769*/
65770/************** Include btreeInt.h in the middle of btmutex.c ****************/
65771/************** Begin file btreeInt.h ****************************************/
65772/*
65773** 2004 April 6
65774**
65775** The author disclaims copyright to this source code. In place of
65776** a legal notice, here is a blessing:
65777**
65778** May you do good and not evil.
65779** May you find forgiveness for yourself and forgive others.
65780** May you share freely, never taking more than you give.
65781**
65782*************************************************************************
65783** This file implements an external (disk-based) database using BTrees.
65784** For a detailed discussion of BTrees, refer to
65785**
65786** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
65787** "Sorting And Searching", pages 473-480. Addison-Wesley
65788** Publishing Company, Reading, Massachusetts.
65789**
65790** The basic idea is that each page of the file contains N database
65791** entries and N+1 pointers to subpages.
65792**
65793** ----------------------------------------------------------------
65794** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
65795** ----------------------------------------------------------------
65796**
65797** All of the keys on the page that Ptr(0) points to have values less
65798** than Key(0). All of the keys on page Ptr(1) and its subpages have
65799** values greater than Key(0) and less than Key(1). All of the keys
65800** on Ptr(N) and its subpages have values greater than Key(N-1). And
65801** so forth.
65802**
65803** Finding a particular key requires reading O(log(M)) pages from the
65804** disk where M is the number of entries in the tree.
65805**
65806** In this implementation, a single file can hold one or more separate
65807** BTrees. Each BTree is identified by the index of its root page. The
65808** key and data for any entry are combined to form the "payload". A
65809** fixed amount of payload can be carried directly on the database
65810** page. If the payload is larger than the preset amount then surplus
65811** bytes are stored on overflow pages. The payload for an entry
65812** and the preceding pointer are combined to form a "Cell". Each
65813** page has a small header which contains the Ptr(N) pointer and other
65814** information such as the size of key and data.
65815**
65816** FORMAT DETAILS
65817**
65818** The file is divided into pages. The first page is called page 1,
65819** the second is page 2, and so forth. A page number of zero indicates
65820** "no such page". The page size can be any power of 2 between 512 and 65536.
65821** Each page can be either a btree page, a freelist page, an overflow
65822** page, or a pointer-map page.
65823**
65824** The first page is always a btree page. The first 100 bytes of the first
65825** page contain a special header (the "file header") that describes the file.
65826** The format of the file header is as follows:
65827**
65828** OFFSET SIZE DESCRIPTION
65829** 0 16 Header string: "SQLite format 3\000"
65830** 16 2 Page size in bytes. (1 means 65536)
65831** 18 1 File format write version
65832** 19 1 File format read version
65833** 20 1 Bytes of unused space at the end of each page
65834** 21 1 Max embedded payload fraction (must be 64)
65835** 22 1 Min embedded payload fraction (must be 32)
65836** 23 1 Min leaf payload fraction (must be 32)
65837** 24 4 File change counter
65838** 28 4 Reserved for future use
65839** 32 4 First freelist page
65840** 36 4 Number of freelist pages in the file
65841** 40 60 15 4-byte meta values passed to higher layers
65842**
65843** 40 4 Schema cookie
65844** 44 4 File format of schema layer
65845** 48 4 Size of page cache
65846** 52 4 Largest root-page (auto/incr_vacuum)
65847** 56 4 1=UTF-8 2=UTF16le 3=UTF16be
65848** 60 4 User version
65849** 64 4 Incremental vacuum mode
65850** 68 4 Application-ID
65851** 72 20 unused
65852** 92 4 The version-valid-for number
65853** 96 4 SQLITE_VERSION_NUMBER
65854**
65855** All of the integer values are big-endian (most significant byte first).
65856**
65857** The file change counter is incremented when the database is changed
65858** This counter allows other processes to know when the file has changed
65859** and thus when they need to flush their cache.
65860**
65861** The max embedded payload fraction is the amount of the total usable
65862** space in a page that can be consumed by a single cell for standard
65863** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default
65864** is to limit the maximum cell size so that at least 4 cells will fit
65865** on one page. Thus the default max embedded payload fraction is 64.
65866**
65867** If the payload for a cell is larger than the max payload, then extra
65868** payload is spilled to overflow pages. Once an overflow page is allocated,
65869** as many bytes as possible are moved into the overflow pages without letting
65870** the cell size drop below the min embedded payload fraction.
65871**
65872** The min leaf payload fraction is like the min embedded payload fraction
65873** except that it applies to leaf nodes in a LEAFDATA tree. The maximum
65874** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
65875** not specified in the header.
65876**
65877** Each btree pages is divided into three sections: The header, the
65878** cell pointer array, and the cell content area. Page 1 also has a 100-byte
65879** file header that occurs before the page header.
65880**
65881** |----------------|
65882** | file header | 100 bytes. Page 1 only.
65883** |----------------|
65884** | page header | 8 bytes for leaves. 12 bytes for interior nodes
65885** |----------------|
65886** | cell pointer | | 2 bytes per cell. Sorted order.
65887** | array | | Grows downward
65888** | | v
65889** |----------------|
65890** | unallocated |
65891** | space |
65892** |----------------| ^ Grows upwards
65893** | cell content | | Arbitrary order interspersed with freeblocks.
65894** | area | | and free space fragments.
65895** |----------------|
65896**
65897** The page headers looks like this:
65898**
65899** OFFSET SIZE DESCRIPTION
65900** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
65901** 1 2 byte offset to the first freeblock
65902** 3 2 number of cells on this page
65903** 5 2 first byte of the cell content area
65904** 7 1 number of fragmented free bytes
65905** 8 4 Right child (the Ptr(N) value). Omitted on leaves.
65906**
65907** The flags define the format of this btree page. The leaf flag means that
65908** this page has no children. The zerodata flag means that this page carries
65909** only keys and no data. The intkey flag means that the key is an integer
65910** which is stored in the key size entry of the cell header rather than in
65911** the payload area.
65912**
65913** The cell pointer array begins on the first byte after the page header.
65914** The cell pointer array contains zero or more 2-byte numbers which are
65915** offsets from the beginning of the page to the cell content in the cell
65916** content area. The cell pointers occur in sorted order. The system strives
65917** to keep free space after the last cell pointer so that new cells can
65918** be easily added without having to defragment the page.
65919**
65920** Cell content is stored at the very end of the page and grows toward the
65921** beginning of the page.
65922**
65923** Unused space within the cell content area is collected into a linked list of
65924** freeblocks. Each freeblock is at least 4 bytes in size. The byte offset
65925** to the first freeblock is given in the header. Freeblocks occur in
65926** increasing order. Because a freeblock must be at least 4 bytes in size,
65927** any group of 3 or fewer unused bytes in the cell content area cannot
65928** exist on the freeblock chain. A group of 3 or fewer free bytes is called
65929** a fragment. The total number of bytes in all fragments is recorded.
65930** in the page header at offset 7.
65931**
65932** SIZE DESCRIPTION
65933** 2 Byte offset of the next freeblock
65934** 2 Bytes in this freeblock
65935**
65936** Cells are of variable length. Cells are stored in the cell content area at
65937** the end of the page. Pointers to the cells are in the cell pointer array
65938** that immediately follows the page header. Cells is not necessarily
65939** contiguous or in order, but cell pointers are contiguous and in order.
65940**
65941** Cell content makes use of variable length integers. A variable
65942** length integer is 1 to 9 bytes where the lower 7 bits of each
65943** byte are used. The integer consists of all bytes that have bit 8 set and
65944** the first byte with bit 8 clear. The most significant byte of the integer
65945** appears first. A variable-length integer may not be more than 9 bytes long.
65946** As a special case, all 8 bytes of the 9th byte are used as data. This
65947** allows a 64-bit integer to be encoded in 9 bytes.
65948**
65949** 0x00 becomes 0x00000000
65950** 0x7f becomes 0x0000007f
65951** 0x81 0x00 becomes 0x00000080
65952** 0x82 0x00 becomes 0x00000100
65953** 0x80 0x7f becomes 0x0000007f
65954** 0x8a 0x91 0xd1 0xac 0x78 becomes 0x12345678
65955** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081
65956**
65957** Variable length integers are used for rowids and to hold the number of
65958** bytes of key and data in a btree cell.
65959**
65960** The content of a cell looks like this:
65961**
65962** SIZE DESCRIPTION
65963** 4 Page number of the left child. Omitted if leaf flag is set.
65964** var Number of bytes of data. Omitted if the zerodata flag is set.
65965** var Number of bytes of key. Or the key itself if intkey flag is set.
65966** * Payload
65967** 4 First page of the overflow chain. Omitted if no overflow
65968**
65969** Overflow pages form a linked list. Each page except the last is completely
65970** filled with data (pagesize - 4 bytes). The last page can have as little
65971** as 1 byte of data.
65972**
65973** SIZE DESCRIPTION
65974** 4 Page number of next overflow page
65975** * Data
65976**
65977** Freelist pages come in two subtypes: trunk pages and leaf pages. The
65978** file header points to the first in a linked list of trunk page. Each trunk
65979** page points to multiple leaf pages. The content of a leaf page is
65980** unspecified. A trunk page looks like this:
65981**
65982** SIZE DESCRIPTION
65983** 4 Page number of next trunk page
65984** 4 Number of leaf pointers on this page
65985** * zero or more pages numbers of leaves
65986*/
65987/* #include "sqliteInt.h" */
65988
65989
65990/* The following value is the maximum cell size assuming a maximum page
65991** size give above.
65992*/
65993#define MX_CELL_SIZE(pBt)((int)(pBt->pageSize-8)) ((int)(pBt->pageSize-8))
65994
65995/* The maximum number of cells on a single page of the database. This
65996** assumes a minimum cell size of 6 bytes (4 bytes for the cell itself
65997** plus 2 bytes for the index to the cell in the page header). Such
65998** small cells will be rare, but they are possible.
65999*/
66000#define MX_CELL(pBt)((pBt->pageSize-8)/6) ((pBt->pageSize-8)/6)
66001
66002/* Forward declarations */
66003typedef struct MemPage MemPage;
66004typedef struct BtLock BtLock;
66005typedef struct CellInfo CellInfo;
66006
66007/*
66008** This is a magic string that appears at the beginning of every
66009** SQLite database in order to identify the file as a real database.
66010**
66011** You can change this value at compile-time by specifying a
66012** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The
66013** header must be exactly 16 bytes including the zero-terminator so
66014** the string itself should be 15 characters long. If you change
66015** the header, then your custom library will not be able to read
66016** databases generated by the standard tools and the standard tools
66017** will not be able to read databases created by your custom library.
66018*/
66019#ifndef SQLITE_FILE_HEADER"SQLite format 3" /* 123456789 123456 */
66020# define SQLITE_FILE_HEADER"SQLite format 3" "SQLite format 3"
66021#endif
66022
66023/*
66024** Page type flags. An ORed combination of these flags appear as the
66025** first byte of on-disk image of every BTree page.
66026*/
66027#define PTF_INTKEY0x01 0x01
66028#define PTF_ZERODATA0x02 0x02
66029#define PTF_LEAFDATA0x04 0x04
66030#define PTF_LEAF0x08 0x08
66031
66032/*
66033** An instance of this object stores information about each a single database
66034** page that has been loaded into memory. The information in this object
66035** is derived from the raw on-disk page content.
66036**
66037** As each database page is loaded into memory, the pager allocats an
66038** instance of this object and zeros the first 8 bytes. (This is the
66039** "extra" information associated with each page of the pager.)
66040**
66041** Access to all fields of this structure is controlled by the mutex
66042** stored in MemPage.pBt->mutex.
66043*/
66044struct MemPage {
66045 u8 isInit; /* True if previously initialized. MUST BE FIRST! */
66046 u8 intKey; /* True if table b-trees. False for index b-trees */
66047 u8 intKeyLeaf; /* True if the leaf of an intKey table */
66048 Pgno pgno; /* Page number for this page */
66049 /* Only the first 8 bytes (above) are zeroed by pager.c when a new page
66050 ** is allocated. All fields that follow must be initialized before use */
66051 u8 leaf; /* True if a leaf page */
66052 u8 hdrOffset; /* 100 for page 1. 0 otherwise */
66053 u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */
66054 u8 max1bytePayload; /* min(maxLocal,127) */
66055 u8 nOverflow; /* Number of overflow cell bodies in aCell[] */
66056 u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
66057 u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */
66058 u16 cellOffset; /* Index in aData of first cell pointer */
66059 int nFree; /* Number of free bytes on the page. -1 for unknown */
66060 u16 nCell; /* Number of cells on this page, local and ovfl */
66061 u16 maskPage; /* Mask for page offset */
66062 u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th
66063 ** non-overflow cell */
66064 u8 *apOvfl[4]; /* Pointers to the body of overflow cells */
66065 BtShared *pBt; /* Pointer to BtShared that this page is part of */
66066 u8 *aData; /* Pointer to disk image of the page data */
66067 u8 *aDataEnd; /* One byte past the end of the entire page - not just
66068 ** the usable space, the entire page. Used to prevent
66069 ** corruption-induced buffer overflow. */
66070 u8 *aCellIdx; /* The cell index area */
66071 u8 *aDataOfst; /* Same as aData for leaves. aData+4 for interior */
66072 DbPage *pDbPage; /* Pager page handle */
66073 u16 (*xCellSize)(MemPage*,u8*); /* cellSizePtr method */
66074 void (*xParseCell)(MemPage*,u8*,CellInfo*); /* btreeParseCell method */
66075};
66076
66077/*
66078** A linked list of the following structures is stored at BtShared.pLock.
66079** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor
66080** is opened on the table with root page BtShared.iTable. Locks are removed
66081** from this list when a transaction is committed or rolled back, or when
66082** a btree handle is closed.
66083*/
66084struct BtLock {
66085 Btree *pBtree; /* Btree handle holding this lock */
66086 Pgno iTable; /* Root page of table */
66087 u8 eLock; /* READ_LOCK or WRITE_LOCK */
66088 BtLock *pNext; /* Next in BtShared.pLock list */
66089};
66090
66091/* Candidate values for BtLock.eLock */
66092#define READ_LOCK1 1
66093#define WRITE_LOCK2 2
66094
66095/* A Btree handle
66096**
66097** A database connection contains a pointer to an instance of
66098** this object for every database file that it has open. This structure
66099** is opaque to the database connection. The database connection cannot
66100** see the internals of this structure and only deals with pointers to
66101** this structure.
66102**
66103** For some database files, the same underlying database cache might be
66104** shared between multiple connections. In that case, each connection
66105** has it own instance of this object. But each instance of this object
66106** points to the same BtShared object. The database cache and the
66107** schema associated with the database file are all contained within
66108** the BtShared object.
66109**
66110** All fields in this structure are accessed under sqlite3.mutex.
66111** The pBt pointer itself may not be changed while there exists cursors
66112** in the referenced BtShared that point back to this Btree since those
66113** cursors have to go through this Btree to find their BtShared and
66114** they often do so without holding sqlite3.mutex.
66115*/
66116struct Btree {
66117 sqlite3 *db; /* The database connection holding this btree */
66118 BtShared *pBt; /* Sharable content of this btree */
66119 u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
66120 u8 sharable; /* True if we can share pBt with another db */
66121 u8 locked; /* True if db currently has pBt locked */
66122 u8 hasIncrblobCur; /* True if there are one or more Incrblob cursors */
66123 int wantToLock; /* Number of nested calls to sqlite3BtreeEnter() */
66124 int nBackup; /* Number of backup operations reading this btree */
66125 u32 iBDataVersion; /* Combines with pBt->pPager->iDataVersion */
66126 Btree *pNext; /* List of other sharable Btrees from the same db */
66127 Btree *pPrev; /* Back pointer of the same list */
66128#ifdef SQLITE_DEBUG
66129 u64 nSeek; /* Calls to sqlite3BtreeMovetoUnpacked() */
66130#endif
66131#ifndef SQLITE_OMIT_SHARED_CACHE
66132 BtLock lock; /* Object used to lock page 1 */
66133#endif
66134};
66135
66136/*
66137** Btree.inTrans may take one of the following values.
66138**
66139** If the shared-data extension is enabled, there may be multiple users
66140** of the Btree structure. At most one of these may open a write transaction,
66141** but any number may have active read transactions.
66142**
66143** These values must match SQLITE_TXN_NONE, SQLITE_TXN_READ, and
66144** SQLITE_TXN_WRITE
66145*/
66146#define TRANS_NONE0 0
66147#define TRANS_READ1 1
66148#define TRANS_WRITE2 2
66149
66150#if TRANS_NONE0!=SQLITE_TXN_NONE0
66151# error wrong numeric code for no-transaction
66152#endif
66153#if TRANS_READ1!=SQLITE_TXN_READ1
66154# error wrong numeric code for read-transaction
66155#endif
66156#if TRANS_WRITE2!=SQLITE_TXN_WRITE2
66157# error wrong numeric code for write-transaction
66158#endif
66159
66160
66161/*
66162** An instance of this object represents a single database file.
66163**
66164** A single database file can be in use at the same time by two
66165** or more database connections. When two or more connections are
66166** sharing the same database file, each connection has it own
66167** private Btree object for the file and each of those Btrees points
66168** to this one BtShared object. BtShared.nRef is the number of
66169** connections currently sharing this database file.
66170**
66171** Fields in this structure are accessed under the BtShared.mutex
66172** mutex, except for nRef and pNext which are accessed under the
66173** global SQLITE_MUTEX_STATIC_MAIN mutex. The pPager field
66174** may not be modified once it is initially set as long as nRef>0.
66175** The pSchema field may be set once under BtShared.mutex and
66176** thereafter is unchanged as long as nRef>0.
66177**
66178** isPending:
66179**
66180** If a BtShared client fails to obtain a write-lock on a database
66181** table (because there exists one or more read-locks on the table),
66182** the shared-cache enters 'pending-lock' state and isPending is
66183** set to true.
66184**
66185** The shared-cache leaves the 'pending lock' state when either of
66186** the following occur:
66187**
66188** 1) The current writer (BtShared.pWriter) concludes its transaction, OR
66189** 2) The number of locks held by other connections drops to zero.
66190**
66191** while in the 'pending-lock' state, no connection may start a new
66192** transaction.
66193**
66194** This feature is included to help prevent writer-starvation.
66195*/
66196struct BtShared {
66197 Pager *pPager; /* The page cache */
66198 sqlite3 *db; /* Database connection currently using this Btree */
66199 BtCursor *pCursor; /* A list of all open cursors */
66200 MemPage *pPage1; /* First page of the database */
66201 u8 openFlags; /* Flags to sqlite3BtreeOpen() */
66202#ifndef SQLITE_OMIT_AUTOVACUUM
66203 u8 autoVacuum; /* True if auto-vacuum is enabled */
66204 u8 incrVacuum; /* True if incr-vacuum is enabled */
66205 u8 bDoTruncate; /* True to truncate db on commit */
66206#endif
66207 u8 inTransaction; /* Transaction state */
66208 u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
66209 u8 nReserveWanted; /* Desired number of extra bytes per page */
66210 u16 btsFlags; /* Boolean parameters. See BTS_* macros below */
66211 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
66212 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
66213 u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */
66214 u16 minLeaf; /* Minimum local payload in a LEAFDATA table */
66215 u32 pageSize; /* Total number of bytes on a page */
66216 u32 usableSize; /* Number of usable bytes on each page */
66217 int nTransaction; /* Number of open transactions (read + write) */
66218 u32 nPage; /* Number of pages in the database */
66219 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
66220 void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
66221 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
66222 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */
66223#ifndef SQLITE_OMIT_SHARED_CACHE
66224 int nRef; /* Number of references to this structure */
66225 BtShared *pNext; /* Next on a list of sharable BtShared structs */
66226 BtLock *pLock; /* List of locks held on this shared-btree struct */
66227 Btree *pWriter; /* Btree with currently open write transaction */
66228#endif
66229 u8 *pTmpSpace; /* Temp space sufficient to hold a single cell */
66230 int nPreformatSize; /* Size of last cell written by TransferRow() */
66231};
66232
66233/*
66234** Allowed values for BtShared.btsFlags
66235*/
66236#define BTS_READ_ONLY0x0001 0x0001 /* Underlying file is readonly */
66237#define BTS_PAGESIZE_FIXED0x0002 0x0002 /* Page size can no longer be changed */
66238#define BTS_SECURE_DELETE0x0004 0x0004 /* PRAGMA secure_delete is enabled */
66239#define BTS_OVERWRITE0x0008 0x0008 /* Overwrite deleted content with zeros */
66240#define BTS_FAST_SECURE0x000c 0x000c /* Combination of the previous two */
66241#define BTS_INITIALLY_EMPTY0x0010 0x0010 /* Database was empty at trans start */
66242#define BTS_NO_WAL0x0020 0x0020 /* Do not open write-ahead-log files */
66243#define BTS_EXCLUSIVE0x0040 0x0040 /* pWriter has an exclusive lock */
66244#define BTS_PENDING0x0080 0x0080 /* Waiting for read-locks to clear */
66245
66246/*
66247** An instance of the following structure is used to hold information
66248** about a cell. The parseCellPtr() function fills in this structure
66249** based on information extract from the raw disk page.
66250*/
66251struct CellInfo {
66252 i64 nKey; /* The key for INTKEY tables, or nPayload otherwise */
66253 u8 *pPayload; /* Pointer to the start of payload */
66254 u32 nPayload; /* Bytes of payload */
66255 u16 nLocal; /* Amount of payload held locally, not on overflow */
66256 u16 nSize; /* Size of the cell content on the main b-tree page */
66257};
66258
66259/*
66260** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
66261** this will be declared corrupt. This value is calculated based on a
66262** maximum database size of 2^31 pages a minimum fanout of 2 for a
66263** root-node and 3 for all other internal nodes.
66264**
66265** If a tree that appears to be taller than this is encountered, it is
66266** assumed that the database is corrupt.
66267*/
66268#define BTCURSOR_MAX_DEPTH20 20
66269
66270/*
66271** A cursor is a pointer to a particular entry within a particular
66272** b-tree within a database file.
66273**
66274** The entry is identified by its MemPage and the index in
66275** MemPage.aCell[] of the entry.
66276**
66277** A single database file can be shared by two more database connections,
66278** but cursors cannot be shared. Each cursor is associated with a
66279** particular database connection identified BtCursor.pBtree.db.
66280**
66281** Fields in this structure are accessed under the BtShared.mutex
66282** found at self->pBt->mutex.
66283**
66284** skipNext meaning:
66285** The meaning of skipNext depends on the value of eState:
66286**
66287** eState Meaning of skipNext
66288** VALID skipNext is meaningless and is ignored
66289** INVALID skipNext is meaningless and is ignored
66290** SKIPNEXT sqlite3BtreeNext() is a no-op if skipNext>0 and
66291** sqlite3BtreePrevious() is no-op if skipNext<0.
66292** REQUIRESEEK restoreCursorPosition() restores the cursor to
66293** eState=SKIPNEXT if skipNext!=0
66294** FAULT skipNext holds the cursor fault error code.
66295*/
66296struct BtCursor {
66297 u8 eState; /* One of the CURSOR_XXX constants (see below) */
66298 u8 curFlags; /* zero or more BTCF_* flags defined below */
66299 u8 curPagerFlags; /* Flags to send to sqlite3PagerGet() */
66300 u8 hints; /* As configured by CursorSetHints() */
66301 int skipNext; /* Prev() is noop if negative. Next() is noop if positive.
66302 ** Error code if eState==CURSOR_FAULT */
66303 Btree *pBtree; /* The Btree to which this cursor belongs */
66304 Pgno *aOverflow; /* Cache of overflow page locations */
66305 void *pKey; /* Saved key that was cursor last known position */
66306 /* All fields above are zeroed when the cursor is allocated. See
66307 ** sqlite3BtreeCursorZero(). Fields that follow must be manually
66308 ** initialized. */
66309#define BTCURSOR_FIRST_UNINITpBt pBt /* Name of first uninitialized field */
66310 BtShared *pBt; /* The BtShared this cursor points to */
66311 BtCursor *pNext; /* Forms a linked list of all cursors */
66312 CellInfo info; /* A parse of the cell we are pointing at */
66313 i64 nKey; /* Size of pKey, or last integer key */
66314 Pgno pgnoRoot; /* The root page of this tree */
66315 i8 iPage; /* Index of current page in apPage */
66316 u8 curIntKey; /* Value of apPage[0]->intKey */
66317 u16 ix; /* Current index for apPage[iPage] */
66318 u16 aiIdx[BTCURSOR_MAX_DEPTH20-1]; /* Current index in apPage[i] */
66319 struct KeyInfo *pKeyInfo; /* Arg passed to comparison function */
66320 MemPage *pPage; /* Current page */
66321 MemPage *apPage[BTCURSOR_MAX_DEPTH20-1]; /* Stack of parents of current page */
66322};
66323
66324/*
66325** Legal values for BtCursor.curFlags
66326*/
66327#define BTCF_WriteFlag0x01 0x01 /* True if a write cursor */
66328#define BTCF_ValidNKey0x02 0x02 /* True if info.nKey is valid */
66329#define BTCF_ValidOvfl0x04 0x04 /* True if aOverflow is valid */
66330#define BTCF_AtLast0x08 0x08 /* Cursor is pointing ot the last entry */
66331#define BTCF_Incrblob0x10 0x10 /* True if an incremental I/O handle */
66332#define BTCF_Multiple0x20 0x20 /* Maybe another cursor on the same btree */
66333#define BTCF_Pinned0x40 0x40 /* Cursor is busy and cannot be moved */
66334
66335/*
66336** Potential values for BtCursor.eState.
66337**
66338** CURSOR_INVALID:
66339** Cursor does not point to a valid entry. This can happen (for example)
66340** because the table is empty or because BtreeCursorFirst() has not been
66341** called.
66342**
66343** CURSOR_VALID:
66344** Cursor points to a valid entry. getPayload() etc. may be called.
66345**
66346** CURSOR_SKIPNEXT:
66347** Cursor is valid except that the Cursor.skipNext field is non-zero
66348** indicating that the next sqlite3BtreeNext() or sqlite3BtreePrevious()
66349** operation should be a no-op.
66350**
66351** CURSOR_REQUIRESEEK:
66352** The table that this cursor was opened on still exists, but has been
66353** modified since the cursor was last used. The cursor position is saved
66354** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in
66355** this state, restoreCursorPosition() can be called to attempt to
66356** seek the cursor to the saved position.
66357**
66358** CURSOR_FAULT:
66359** An unrecoverable error (an I/O error or a malloc failure) has occurred
66360** on a different connection that shares the BtShared cache with this
66361** cursor. The error has left the cache in an inconsistent state.
66362** Do nothing else with this cursor. Any attempt to use the cursor
66363** should return the error code stored in BtCursor.skipNext
66364*/
66365#define CURSOR_VALID0 0
66366#define CURSOR_INVALID1 1
66367#define CURSOR_SKIPNEXT2 2
66368#define CURSOR_REQUIRESEEK3 3
66369#define CURSOR_FAULT4 4
66370
66371/*
66372** The database page the PENDING_BYTE occupies. This page is never used.
66373*/
66374#define PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ((Pgno)((PENDING_BYTEsqlite3PendingByte/((pBt)->pageSize))+1))
66375
66376/*
66377** These macros define the location of the pointer-map entry for a
66378** database page. The first argument to each is the number of usable
66379** bytes on each page of the database (often 1024). The second is the
66380** page number to look up in the pointer map.
66381**
66382** PTRMAP_PAGENO returns the database page number of the pointer-map
66383** page that stores the required pointer. PTRMAP_PTROFFSET returns
66384** the offset of the requested map entry.
66385**
66386** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
66387** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
66388** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
66389** this test.
66390*/
66391#define PTRMAP_PAGENO(pBt, pgno)ptrmapPageno(pBt, pgno) ptrmapPageno(pBt, pgno)
66392#define PTRMAP_PTROFFSET(pgptrmap, pgno)(5*(pgno-pgptrmap-1)) (5*(pgno-pgptrmap-1))
66393#define PTRMAP_ISPAGE(pBt, pgno)(ptrmapPageno((pBt), (pgno))==(pgno)) (PTRMAP_PAGENO((pBt),(pgno))ptrmapPageno((pBt), (pgno))==(pgno))
66394
66395/*
66396** The pointer map is a lookup table that identifies the parent page for
66397** each child page in the database file. The parent page is the page that
66398** contains a pointer to the child. Every page in the database contains
66399** 0 or 1 parent pages. (In this context 'database page' refers
66400** to any page that is not part of the pointer map itself.) Each pointer map
66401** entry consists of a single byte 'type' and a 4 byte parent page number.
66402** The PTRMAP_XXX identifiers below are the valid types.
66403**
66404** The purpose of the pointer map is to facility moving pages from one
66405** position in the file to another as part of autovacuum. When a page
66406** is moved, the pointer in its parent must be updated to point to the
66407** new location. The pointer map is used to locate the parent page quickly.
66408**
66409** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
66410** used in this case.
66411**
66412** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
66413** is not used in this case.
66414**
66415** PTRMAP_OVERFLOW1: The database page is the first page in a list of
66416** overflow pages. The page number identifies the page that
66417** contains the cell with a pointer to this overflow page.
66418**
66419** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
66420** overflow pages. The page-number identifies the previous
66421** page in the overflow page list.
66422**
66423** PTRMAP_BTREE: The database page is a non-root btree page. The page number
66424** identifies the parent page in the btree.
66425*/
66426#define PTRMAP_ROOTPAGE1 1
66427#define PTRMAP_FREEPAGE2 2
66428#define PTRMAP_OVERFLOW13 3
66429#define PTRMAP_OVERFLOW24 4
66430#define PTRMAP_BTREE5 5
66431
66432/* A bunch of assert() statements to check the transaction state variables
66433** of handle p (type Btree*) are internally consistent.
66434*/
66435#define btreeIntegrity(p)((void) (0)); ((void) (0)); \
66436 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 )((void) (0)); \
66437 assert( p->pBt->inTransaction>=p->inTrans )((void) (0));
66438
66439
66440/*
66441** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
66442** if the database supports auto-vacuum or not. Because it is used
66443** within an expression that is an argument to another macro
66444** (sqliteMallocRaw), it is not possible to use conditional compilation.
66445** So, this macro is defined instead.
66446*/
66447#ifndef SQLITE_OMIT_AUTOVACUUM
66448#define ISAUTOVACUUM(pBt->autoVacuum) (pBt->autoVacuum)
66449#else
66450#define ISAUTOVACUUM(pBt->autoVacuum) 0
66451#endif
66452
66453
66454/*
66455** This structure is passed around through all the sanity checking routines
66456** in order to keep track of some global state information.
66457**
66458** The aRef[] array is allocated so that there is 1 bit for each page in
66459** the database. As the integrity-check proceeds, for each page used in
66460** the database the corresponding bit is set. This allows integrity-check to
66461** detect pages that are used twice and orphaned pages (both of which
66462** indicate corruption).
66463*/
66464typedef struct IntegrityCk IntegrityCk;
66465struct IntegrityCk {
66466 BtShared *pBt; /* The tree being checked out */
66467 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
66468 u8 *aPgRef; /* 1 bit per page in the db (see above) */
66469 Pgno nPage; /* Number of pages in the database */
66470 int mxErr; /* Stop accumulating errors when this reaches zero */
66471 int nErr; /* Number of messages written to zErrMsg so far */
66472 int bOomFault; /* A memory allocation error has occurred */
66473 const char *zPfx; /* Error message prefix */
66474 Pgno v1; /* Value for first %u substitution in zPfx */
66475 int v2; /* Value for second %d substitution in zPfx */
66476 StrAccum errMsg; /* Accumulate the error message text here */
66477 u32 *heap; /* Min-heap used for analyzing cell coverage */
66478 sqlite3 *db; /* Database connection running the check */
66479};
66480
66481/*
66482** Routines to read or write a two- and four-byte big-endian integer values.
66483*/
66484#define get2byte(x)((x)[0]<<8 | (x)[1]) ((x)[0]<<8 | (x)[1])
66485#define put2byte(p,v)((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v)) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
66486#define get4bytesqlite3Get4byte sqlite3Get4byte
66487#define put4bytesqlite3Put4byte sqlite3Put4byte
66488
66489/*
66490** get2byteAligned(), unlike get2byte(), requires that its argument point to a
66491** two-byte aligned address. get2bytea() is only used for accessing the
66492** cell addresses in a btree header.
66493*/
66494#if SQLITE_BYTEORDER1234==4321
66495# define get2byteAligned(x)((x)[0]<<8 | (x)[1]) (*(u16*)(x))
66496#elif SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4008000
66497# define get2byteAligned(x)((x)[0]<<8 | (x)[1]) __builtin_bswap16(*(u16*)(x))
66498#elif SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
66499# define get2byteAligned(x)((x)[0]<<8 | (x)[1]) _byteswap_ushort(*(u16*)(x))
66500#else
66501# define get2byteAligned(x)((x)[0]<<8 | (x)[1]) ((x)[0]<<8 | (x)[1])
66502#endif
66503
66504/************** End of btreeInt.h ********************************************/
66505/************** Continuing where we left off in btmutex.c ********************/
66506#ifndef SQLITE_OMIT_SHARED_CACHE
66507#if SQLITE_THREADSAFE2
66508
66509/*
66510** Obtain the BtShared mutex associated with B-Tree handle p. Also,
66511** set BtShared.db to the database handle associated with p and the
66512** p->locked boolean to true.
66513*/
66514static void lockBtreeMutex(Btree *p){
66515 assert( p->locked==0 )((void) (0));
66516 assert( sqlite3_mutex_notheld(p->pBt->mutex) )((void) (0));
66517 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
66518
66519 sqlite3_mutex_enter(p->pBt->mutex);
66520 p->pBt->db = p->db;
66521 p->locked = 1;
66522}
66523
66524/*
66525** Release the BtShared mutex associated with B-Tree handle p and
66526** clear the p->locked boolean.
66527*/
66528static void SQLITE_NOINLINE__attribute__((noinline)) unlockBtreeMutex(Btree *p){
66529 BtShared *pBt = p->pBt;
66530 assert( p->locked==1 )((void) (0));
66531 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
66532 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
66533 assert( p->db==pBt->db )((void) (0));
66534
66535 sqlite3_mutex_leave(pBt->mutex);
66536 p->locked = 0;
66537}
66538
66539/* Forward reference */
66540static void SQLITE_NOINLINE__attribute__((noinline)) btreeLockCarefully(Btree *p);
66541
66542/*
66543** Enter a mutex on the given BTree object.
66544**
66545** If the object is not sharable, then no mutex is ever required
66546** and this routine is a no-op. The underlying mutex is non-recursive.
66547** But we keep a reference count in Btree.wantToLock so the behavior
66548** of this interface is recursive.
66549**
66550** To avoid deadlocks, multiple Btrees are locked in the same order
66551** by all database connections. The p->pNext is a list of other
66552** Btrees belonging to the same database connection as the p Btree
66553** which need to be locked after p. If we cannot get a lock on
66554** p, then first unlock all of the others on p->pNext, then wait
66555** for the lock to become available on p, then relock all of the
66556** subsequent Btrees that desire a lock.
66557*/
66558SQLITE_PRIVATEstatic void sqlite3BtreeEnter(Btree *p){
66559 /* Some basic sanity checking on the Btree. The list of Btrees
66560 ** connected by pNext and pPrev should be in sorted order by
66561 ** Btree.pBt value. All elements of the list should belong to
66562 ** the same connection. Only shared Btrees are on the list. */
66563 assert( p->pNext==0 || p->pNext->pBt>p->pBt )((void) (0));
66564 assert( p->pPrev==0 || p->pPrev->pBt<p->pBt )((void) (0));
66565 assert( p->pNext==0 || p->pNext->db==p->db )((void) (0));
66566 assert( p->pPrev==0 || p->pPrev->db==p->db )((void) (0));
66567 assert( p->sharable || (p->pNext==0 && p->pPrev==0) )((void) (0));
66568
66569 /* Check for locking consistency */
66570 assert( !p->locked || p->wantToLock>0 )((void) (0));
66571 assert( p->sharable || p->wantToLock==0 )((void) (0));
66572
66573 /* We should already hold a lock on the database connection */
66574 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
66575
66576 /* Unless the database is sharable and unlocked, then BtShared.db
66577 ** should already be set correctly. */
66578 assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db )((void) (0));
66579
66580 if( !p->sharable ) return;
66581 p->wantToLock++;
66582 if( p->locked ) return;
66583 btreeLockCarefully(p);
66584}
66585
66586/* This is a helper function for sqlite3BtreeLock(). By moving
66587** complex, but seldom used logic, out of sqlite3BtreeLock() and
66588** into this routine, we avoid unnecessary stack pointer changes
66589** and thus help the sqlite3BtreeLock() routine to run much faster
66590** in the common case.
66591*/
66592static void SQLITE_NOINLINE__attribute__((noinline)) btreeLockCarefully(Btree *p){
66593 Btree *pLater;
66594
66595 /* In most cases, we should be able to acquire the lock we
66596 ** want without having to go through the ascending lock
66597 ** procedure that follows. Just be sure not to block.
66598 */
66599 if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK0 ){
66600 p->pBt->db = p->db;
66601 p->locked = 1;
66602 return;
66603 }
66604
66605 /* To avoid deadlock, first release all locks with a larger
66606 ** BtShared address. Then acquire our lock. Then reacquire
66607 ** the other BtShared locks that we used to hold in ascending
66608 ** order.
66609 */
66610 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
66611 assert( pLater->sharable )((void) (0));
66612 assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt )((void) (0));
66613 assert( !pLater->locked || pLater->wantToLock>0 )((void) (0));
66614 if( pLater->locked ){
66615 unlockBtreeMutex(pLater);
66616 }
66617 }
66618 lockBtreeMutex(p);
66619 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
66620 if( pLater->wantToLock ){
66621 lockBtreeMutex(pLater);
66622 }
66623 }
66624}
66625
66626
66627/*
66628** Exit the recursive mutex on a Btree.
66629*/
66630SQLITE_PRIVATEstatic void sqlite3BtreeLeave(Btree *p){
66631 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
66632 if( p->sharable ){
66633 assert( p->wantToLock>0 )((void) (0));
66634 p->wantToLock--;
66635 if( p->wantToLock==0 ){
66636 unlockBtreeMutex(p);
66637 }
66638 }
66639}
66640
66641#ifndef NDEBUG1
66642/*
66643** Return true if the BtShared mutex is held on the btree, or if the
66644** B-Tree is not marked as sharable.
66645**
66646** This routine is used only from within assert() statements.
66647*/
66648SQLITE_PRIVATEstatic int sqlite3BtreeHoldsMutex(Btree *p){
66649 assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 )((void) (0));
66650 assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db )((void) (0));
66651 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) )((void) (0));
66652 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) )((void) (0));
66653
66654 return (p->sharable==0 || p->locked);
66655}
66656#endif
66657
66658
66659/*
66660** Enter the mutex on every Btree associated with a database
66661** connection. This is needed (for example) prior to parsing
66662** a statement since we will be comparing table and column names
66663** against all schemas and we do not want those schemas being
66664** reset out from under us.
66665**
66666** There is a corresponding leave-all procedures.
66667**
66668** Enter the mutexes in accending order by BtShared pointer address
66669** to avoid the possibility of deadlock when two threads with
66670** two or more btrees in common both try to lock all their btrees
66671** at the same instant.
66672*/
66673static void SQLITE_NOINLINE__attribute__((noinline)) btreeEnterAll(sqlite3 *db){
66674 int i;
66675 int skipOk = 1;
66676 Btree *p;
66677 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
66678 for(i=0; i<db->nDb; i++){
66679 p = db->aDb[i].pBt;
66680 if( p && p->sharable ){
66681 sqlite3BtreeEnter(p);
66682 skipOk = 0;
66683 }
66684 }
66685 db->noSharedCache = skipOk;
66686}
66687SQLITE_PRIVATEstatic void sqlite3BtreeEnterAll(sqlite3 *db){
66688 if( db->noSharedCache==0 ) btreeEnterAll(db);
66689}
66690static void SQLITE_NOINLINE__attribute__((noinline)) btreeLeaveAll(sqlite3 *db){
66691 int i;
66692 Btree *p;
66693 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
66694 for(i=0; i<db->nDb; i++){
66695 p = db->aDb[i].pBt;
66696 if( p ) sqlite3BtreeLeave(p);
66697 }
66698}
66699SQLITE_PRIVATEstatic void sqlite3BtreeLeaveAll(sqlite3 *db){
66700 if( db->noSharedCache==0 ) btreeLeaveAll(db);
66701}
66702
66703#ifndef NDEBUG1
66704/*
66705** Return true if the current thread holds the database connection
66706** mutex and all required BtShared mutexes.
66707**
66708** This routine is used inside assert() statements only.
66709*/
66710SQLITE_PRIVATEstatic int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
66711 int i;
66712 if( !sqlite3_mutex_held(db->mutex) ){
66713 return 0;
66714 }
66715 for(i=0; i<db->nDb; i++){
66716 Btree *p;
66717 p = db->aDb[i].pBt;
66718 if( p && p->sharable &&
66719 (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
66720 return 0;
66721 }
66722 }
66723 return 1;
66724}
66725#endif /* NDEBUG */
66726
66727#ifndef NDEBUG1
66728/*
66729** Return true if the correct mutexes are held for accessing the
66730** db->aDb[iDb].pSchema structure. The mutexes required for schema
66731** access are:
66732**
66733** (1) The mutex on db
66734** (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
66735**
66736** If pSchema is not NULL, then iDb is computed from pSchema and
66737** db using sqlite3SchemaToIndex().
66738*/
66739SQLITE_PRIVATEstatic int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
66740 Btree *p;
66741 assert( db!=0 )((void) (0));
66742 if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
66743 assert( iDb>=0 && iDb<db->nDb )((void) (0));
66744 if( !sqlite3_mutex_held(db->mutex) ) return 0;
66745 if( iDb==1 ) return 1;
66746 p = db->aDb[iDb].pBt;
66747 assert( p!=0 )((void) (0));
66748 return p->sharable==0 || p->locked==1;
66749}
66750#endif /* NDEBUG */
66751
66752#else /* SQLITE_THREADSAFE>0 above. SQLITE_THREADSAFE==0 below */
66753/*
66754** The following are special cases for mutex enter routines for use
66755** in single threaded applications that use shared cache. Except for
66756** these two routines, all mutex operations are no-ops in that case and
66757** are null #defines in btree.h.
66758**
66759** If shared cache is disabled, then all btree mutex routines, including
66760** the ones below, are no-ops and are null #defines in btree.h.
66761*/
66762
66763SQLITE_PRIVATEstatic void sqlite3BtreeEnter(Btree *p){
66764 p->pBt->db = p->db;
66765}
66766SQLITE_PRIVATEstatic void sqlite3BtreeEnterAll(sqlite3 *db){
66767 int i;
66768 for(i=0; i<db->nDb; i++){
66769 Btree *p = db->aDb[i].pBt;
66770 if( p ){
66771 p->pBt->db = p->db;
66772 }
66773 }
66774}
66775#endif /* if SQLITE_THREADSAFE */
66776
66777#ifndef SQLITE_OMIT_INCRBLOB
66778/*
66779** Enter a mutex on a Btree given a cursor owned by that Btree.
66780**
66781** These entry points are used by incremental I/O only. Enter() is required
66782** any time OMIT_SHARED_CACHE is not defined, regardless of whether or not
66783** the build is threadsafe. Leave() is only required by threadsafe builds.
66784*/
66785SQLITE_PRIVATEstatic void sqlite3BtreeEnterCursor(BtCursor *pCur){
66786 sqlite3BtreeEnter(pCur->pBtree);
66787}
66788# if SQLITE_THREADSAFE2
66789SQLITE_PRIVATEstatic void sqlite3BtreeLeaveCursor(BtCursor *pCur){
66790 sqlite3BtreeLeave(pCur->pBtree);
66791}
66792# endif
66793#endif /* ifndef SQLITE_OMIT_INCRBLOB */
66794
66795#endif /* ifndef SQLITE_OMIT_SHARED_CACHE */
66796
66797/************** End of btmutex.c *********************************************/
66798/************** Begin file btree.c *******************************************/
66799/*
66800** 2004 April 6
66801**
66802** The author disclaims copyright to this source code. In place of
66803** a legal notice, here is a blessing:
66804**
66805** May you do good and not evil.
66806** May you find forgiveness for yourself and forgive others.
66807** May you share freely, never taking more than you give.
66808**
66809*************************************************************************
66810** This file implements an external (disk-based) database using BTrees.
66811** See the header comment on "btreeInt.h" for additional information.
66812** Including a description of file format and an overview of operation.
66813*/
66814/* #include "btreeInt.h" */
66815
66816/*
66817** The header string that appears at the beginning of every
66818** SQLite database.
66819*/
66820static const char zMagicHeader[] = SQLITE_FILE_HEADER"SQLite format 3";
66821
66822/*
66823** Set this global variable to 1 to enable tracing using the TRACE
66824** macro.
66825*/
66826#if 0
66827int sqlite3BtreeTrace=1; /* True to enable tracing */
66828# define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdoutstdout);}
66829#else
66830# define TRACE(X)
66831#endif
66832
66833/*
66834** Extract a 2-byte big-endian integer from an array of unsigned bytes.
66835** But if the value is zero, make it 65536.
66836**
66837** This routine is used to extract the "offset to cell content area" value
66838** from the header of a btree page. If the page size is 65536 and the page
66839** is empty, the offset should be 65536, but the 2-byte value stores zero.
66840** This routine makes the necessary adjustment to 65536.
66841*/
66842#define get2byteNotZero(X)(((((int)((X)[0]<<8 | (X)[1]))-1)&0xffff)+1) (((((int)get2byte(X)((X)[0]<<8 | (X)[1]))-1)&0xffff)+1)
66843
66844/*
66845** Values passed as the 5th argument to allocateBtreePage()
66846*/
66847#define BTALLOC_ANY0 0 /* Allocate any page */
66848#define BTALLOC_EXACT1 1 /* Allocate exact page if possible */
66849#define BTALLOC_LE2 2 /* Allocate any page <= the parameter */
66850
66851/*
66852** Macro IfNotOmitAV(x) returns (x) if SQLITE_OMIT_AUTOVACUUM is not
66853** defined, or 0 if it is. For example:
66854**
66855** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
66856*/
66857#ifndef SQLITE_OMIT_AUTOVACUUM
66858#define IfNotOmitAV(expr)(expr) (expr)
66859#else
66860#define IfNotOmitAV(expr)(expr) 0
66861#endif
66862
66863#ifndef SQLITE_OMIT_SHARED_CACHE
66864/*
66865** A list of BtShared objects that are eligible for participation
66866** in shared cache. This variable has file scope during normal builds,
66867** but the test harness needs to access it so we make it global for
66868** test builds.
66869**
66870** Access to this variable is protected by SQLITE_MUTEX_STATIC_MAIN.
66871*/
66872#ifdef SQLITE_TEST
66873SQLITE_PRIVATEstatic BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
66874#else
66875static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
66876#endif
66877#endif /* SQLITE_OMIT_SHARED_CACHE */
66878
66879#ifndef SQLITE_OMIT_SHARED_CACHE
66880/*
66881** Enable or disable the shared pager and schema features.
66882**
66883** This routine has no effect on existing database connections.
66884** The shared cache setting effects only future calls to
66885** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
66886*/
66887SQLITE_API int sqlite3_enable_shared_cache(int enable){
66888 sqlite3GlobalConfigsqlite3Config.sharedCacheEnabled = enable;
66889 return SQLITE_OK0;
66890}
66891#endif
66892
66893
66894
66895#ifdef SQLITE_OMIT_SHARED_CACHE
66896 /*
66897 ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
66898 ** and clearAllSharedCacheTableLocks()
66899 ** manipulate entries in the BtShared.pLock linked list used to store
66900 ** shared-cache table level locks. If the library is compiled with the
66901 ** shared-cache feature disabled, then there is only ever one user
66902 ** of each BtShared structure and so this locking is not necessary.
66903 ** So define the lock related functions as no-ops.
66904 */
66905 #define querySharedCacheTableLock(a,b,c) SQLITE_OK0
66906 #define setSharedCacheTableLock(a,b,c) SQLITE_OK0
66907 #define clearAllSharedCacheTableLocks(a)
66908 #define downgradeAllSharedCacheTableLocks(a)
66909 #define hasSharedCacheTableLock(a,b,c,d) 1
66910 #define hasReadConflicts(a, b) 0
66911#endif
66912
66913#ifdef SQLITE_DEBUG
66914/*
66915** Return and reset the seek counter for a Btree object.
66916*/
66917SQLITE_PRIVATEstatic sqlite3_uint64 sqlite3BtreeSeekCount(Btree *pBt)0{
66918 u64 n = pBt->nSeek;
66919 pBt->nSeek = 0;
66920 return n;
66921}
66922#endif
66923
66924/*
66925** Implementation of the SQLITE_CORRUPT_PAGE() macro. Takes a single
66926** (MemPage*) as an argument. The (MemPage*) must not be NULL.
66927**
66928** If SQLITE_DEBUG is not defined, then this macro is equivalent to
66929** SQLITE_CORRUPT_BKPT. Or, if SQLITE_DEBUG is set, then the log message
66930** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
66931** with the page number and filename associated with the (MemPage*).
66932*/
66933#ifdef SQLITE_DEBUG
66934int corruptPageError(int lineno, MemPage *p){
66935 char *zMsg;
66936 sqlite3BeginBenignMalloc();
66937 zMsg = sqlite3_mprintf("database corruption page %d of %s",
66938 (int)p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
66939 );
66940 sqlite3EndBenignMalloc();
66941 if( zMsg ){
66942 sqlite3ReportError(SQLITE_CORRUPT11, lineno, zMsg);
66943 }
66944 sqlite3_free(zMsg);
66945 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(66945);
66946}
66947# define SQLITE_CORRUPT_PAGE(pMemPage)sqlite3CorruptError(66947) corruptPageError(__LINE__66947, pMemPage)
66948#else
66949# define SQLITE_CORRUPT_PAGE(pMemPage)sqlite3CorruptError(66949) SQLITE_CORRUPT_PGNO(pMemPage->pgno)sqlite3CorruptError(66949)
66950#endif
66951
66952#ifndef SQLITE_OMIT_SHARED_CACHE
66953
66954#ifdef SQLITE_DEBUG
66955/*
66956**** This function is only used as part of an assert() statement. ***
66957**
66958** Check to see if pBtree holds the required locks to read or write to the
66959** table with root page iRoot. Return 1 if it does and 0 if not.
66960**
66961** For example, when writing to a table with root-page iRoot via
66962** Btree connection pBtree:
66963**
66964** assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
66965**
66966** When writing to an index that resides in a sharable database, the
66967** caller should have first obtained a lock specifying the root page of
66968** the corresponding table. This makes things a bit more complicated,
66969** as this module treats each table as a separate structure. To determine
66970** the table corresponding to the index being written, this
66971** function has to search through the database schema.
66972**
66973** Instead of a lock on the table/index rooted at page iRoot, the caller may
66974** hold a write-lock on the schema table (root page 1). This is also
66975** acceptable.
66976*/
66977static int hasSharedCacheTableLock(
66978 Btree *pBtree, /* Handle that must hold lock */
66979 Pgno iRoot, /* Root page of b-tree */
66980 int isIndex, /* True if iRoot is the root of an index b-tree */
66981 int eLockType /* Required lock type (READ_LOCK or WRITE_LOCK) */
66982){
66983 Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
66984 Pgno iTab = 0;
66985 BtLock *pLock;
66986
66987 /* If this database is not shareable, or if the client is reading
66988 ** and has the read-uncommitted flag set, then no lock is required.
66989 ** Return true immediately.
66990 */
66991 if( (pBtree->sharable==0)
66992 || (eLockType==READ_LOCK1 && (pBtree->db->flags & SQLITE_ReadUncommit0x00000400))
66993 ){
66994 return 1;
66995 }
66996
66997 /* If the client is reading or writing an index and the schema is
66998 ** not loaded, then it is too difficult to actually check to see if
66999 ** the correct locks are held. So do not bother - just return true.
67000 ** This case does not come up very often anyhow.
67001 */
67002 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded0x0001)==0) ){
67003 return 1;
67004 }
67005
67006 /* Figure out the root-page that the lock should be held on. For table
67007 ** b-trees, this is just the root page of the b-tree being read or
67008 ** written. For index b-trees, it is the root page of the associated
67009 ** table. */
67010 if( isIndex ){
67011 HashElem *p;
67012 int bSeen = 0;
67013 for(p=sqliteHashFirst(&pSchema->idxHash)((&pSchema->idxHash)->first); p; p=sqliteHashNext(p)((p)->next)){
67014 Index *pIdx = (Index *)sqliteHashData(p)((p)->data);
67015 if( pIdx->tnum==iRoot ){
67016 if( bSeen ){
67017 /* Two or more indexes share the same root page. There must
67018 ** be imposter tables. So just return true. The assert is not
67019 ** useful in that case. */
67020 return 1;
67021 }
67022 iTab = pIdx->pTable->tnum;
67023 bSeen = 1;
67024 }
67025 }
67026 }else{
67027 iTab = iRoot;
67028 }
67029
67030 /* Search for the required lock. Either a write-lock on root-page iTab, a
67031 ** write-lock on the schema table, or (if the client is reading) a
67032 ** read-lock on iTab will suffice. Return 1 if any of these are found. */
67033 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
67034 if( pLock->pBtree==pBtree
67035 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK2 && pLock->iTable==1))
67036 && pLock->eLock>=eLockType
67037 ){
67038 return 1;
67039 }
67040 }
67041
67042 /* Failed to find the required lock. */
67043 return 0;
67044}
67045#endif /* SQLITE_DEBUG */
67046
67047#ifdef SQLITE_DEBUG
67048/*
67049**** This function may be used as part of assert() statements only. ****
67050**
67051** Return true if it would be illegal for pBtree to write into the
67052** table or index rooted at iRoot because other shared connections are
67053** simultaneously reading that same table or index.
67054**
67055** It is illegal for pBtree to write if some other Btree object that
67056** shares the same BtShared object is currently reading or writing
67057** the iRoot table. Except, if the other Btree object has the
67058** read-uncommitted flag set, then it is OK for the other object to
67059** have a read cursor.
67060**
67061** For example, before writing to any part of the table or index
67062** rooted at page iRoot, one should call:
67063**
67064** assert( !hasReadConflicts(pBtree, iRoot) );
67065*/
67066static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
67067 BtCursor *p;
67068 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
67069 if( p->pgnoRoot==iRoot
67070 && p->pBtree!=pBtree
67071 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit0x00000400)
67072 ){
67073 return 1;
67074 }
67075 }
67076 return 0;
67077}
67078#endif /* #ifdef SQLITE_DEBUG */
67079
67080/*
67081** Query to see if Btree handle p may obtain a lock of type eLock
67082** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
67083** SQLITE_OK if the lock may be obtained (by calling
67084** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
67085*/
67086static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
67087 BtShared *pBt = p->pBt;
67088 BtLock *pIter;
67089
67090 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
67091 assert( eLock==READ_LOCK || eLock==WRITE_LOCK )((void) (0));
67092 assert( p->db!=0 )((void) (0));
67093 assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 )((void) (0));
67094
67095 /* If requesting a write-lock, then the Btree must have an open write
67096 ** transaction on this file. And, obviously, for this to be so there
67097 ** must be an open write transaction on the file itself.
67098 */
67099 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) )((void) (0));
67100 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE )((void) (0));
67101
67102 /* This routine is a no-op if the shared-cache is not enabled */
67103 if( !p->sharable ){
67104 return SQLITE_OK0;
67105 }
67106
67107 /* If some other connection is holding an exclusive lock, the
67108 ** requested lock may not be obtained.
67109 */
67110 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE0x0040)!=0 ){
67111 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
67112 return SQLITE_LOCKED_SHAREDCACHE(6 | (1<<8));
67113 }
67114
67115 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
67116 /* The condition (pIter->eLock!=eLock) in the following if(...)
67117 ** statement is a simplification of:
67118 **
67119 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
67120 **
67121 ** since we know that if eLock==WRITE_LOCK, then no other connection
67122 ** may hold a WRITE_LOCK on any table in this file (since there can
67123 ** only be a single writer).
67124 */
67125 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK )((void) (0));
67126 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK)((void) (0));
67127 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
67128 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
67129 if( eLock==WRITE_LOCK2 ){
67130 assert( p==pBt->pWriter )((void) (0));
67131 pBt->btsFlags |= BTS_PENDING0x0080;
67132 }
67133 return SQLITE_LOCKED_SHAREDCACHE(6 | (1<<8));
67134 }
67135 }
67136 return SQLITE_OK0;
67137}
67138#endif /* !SQLITE_OMIT_SHARED_CACHE */
67139
67140#ifndef SQLITE_OMIT_SHARED_CACHE
67141/*
67142** Add a lock on the table with root-page iTable to the shared-btree used
67143** by Btree handle p. Parameter eLock must be either READ_LOCK or
67144** WRITE_LOCK.
67145**
67146** This function assumes the following:
67147**
67148** (a) The specified Btree object p is connected to a sharable
67149** database (one with the BtShared.sharable flag set), and
67150**
67151** (b) No other Btree objects hold a lock that conflicts
67152** with the requested lock (i.e. querySharedCacheTableLock() has
67153** already been called and returned SQLITE_OK).
67154**
67155** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM
67156** is returned if a malloc attempt fails.
67157*/
67158static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
67159 BtShared *pBt = p->pBt;
67160 BtLock *pLock = 0;
67161 BtLock *pIter;
67162
67163 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
67164 assert( eLock==READ_LOCK || eLock==WRITE_LOCK )((void) (0));
67165 assert( p->db!=0 )((void) (0));
67166
67167 /* A connection with the read-uncommitted flag set will never try to
67168 ** obtain a read-lock using this function. The only read-lock obtained
67169 ** by a connection in read-uncommitted mode is on the sqlite_schema
67170 ** table, and that lock is obtained in BtreeBeginTrans(). */
67171 assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK )((void) (0));
67172
67173 /* This function should only be called on a sharable b-tree after it
67174 ** has been determined that no other b-tree holds a conflicting lock. */
67175 assert( p->sharable )((void) (0));
67176 assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) )((void) (0));
67177
67178 /* First search the list for an existing lock on this table. */
67179 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
67180 if( pIter->iTable==iTable && pIter->pBtree==p ){
67181 pLock = pIter;
67182 break;
67183 }
67184 }
67185
67186 /* If the above search did not find a BtLock struct associating Btree p
67187 ** with table iTable, allocate one and link it into the list.
67188 */
67189 if( !pLock ){
67190 pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
67191 if( !pLock ){
67192 return SQLITE_NOMEM_BKPT7;
67193 }
67194 pLock->iTable = iTable;
67195 pLock->pBtree = p;
67196 pLock->pNext = pBt->pLock;
67197 pBt->pLock = pLock;
67198 }
67199
67200 /* Set the BtLock.eLock variable to the maximum of the current lock
67201 ** and the requested lock. This means if a write-lock was already held
67202 ** and a read-lock requested, we don't incorrectly downgrade the lock.
67203 */
67204 assert( WRITE_LOCK>READ_LOCK )((void) (0));
67205 if( eLock>pLock->eLock ){
67206 pLock->eLock = eLock;
67207 }
67208
67209 return SQLITE_OK0;
67210}
67211#endif /* !SQLITE_OMIT_SHARED_CACHE */
67212
67213#ifndef SQLITE_OMIT_SHARED_CACHE
67214/*
67215** Release all the table locks (locks obtained via calls to
67216** the setSharedCacheTableLock() procedure) held by Btree object p.
67217**
67218** This function assumes that Btree p has an open read or write
67219** transaction. If it does not, then the BTS_PENDING flag
67220** may be incorrectly cleared.
67221*/
67222static void clearAllSharedCacheTableLocks(Btree *p){
67223 BtShared *pBt = p->pBt;
67224 BtLock **ppIter = &pBt->pLock;
67225
67226 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
67227 assert( p->sharable || 0==*ppIter )((void) (0));
67228 assert( p->inTrans>0 )((void) (0));
67229
67230 while( *ppIter ){
67231 BtLock *pLock = *ppIter;
67232 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree )((void) (0));
67233 assert( pLock->pBtree->inTrans>=pLock->eLock )((void) (0));
67234 if( pLock->pBtree==p ){
67235 *ppIter = pLock->pNext;
67236 assert( pLock->iTable!=1 || pLock==&p->lock )((void) (0));
67237 if( pLock->iTable!=1 ){
67238 sqlite3_free(pLock);
67239 }
67240 }else{
67241 ppIter = &pLock->pNext;
67242 }
67243 }
67244
67245 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter )((void) (0));
67246 if( pBt->pWriter==p ){
67247 pBt->pWriter = 0;
67248 pBt->btsFlags &= ~(BTS_EXCLUSIVE0x0040|BTS_PENDING0x0080);
67249 }else if( pBt->nTransaction==2 ){
67250 /* This function is called when Btree p is concluding its
67251 ** transaction. If there currently exists a writer, and p is not
67252 ** that writer, then the number of locks held by connections other
67253 ** than the writer must be about to drop to zero. In this case
67254 ** set the BTS_PENDING flag to 0.
67255 **
67256 ** If there is not currently a writer, then BTS_PENDING must
67257 ** be zero already. So this next line is harmless in that case.
67258 */
67259 pBt->btsFlags &= ~BTS_PENDING0x0080;
67260 }
67261}
67262
67263/*
67264** This function changes all write-locks held by Btree p into read-locks.
67265*/
67266static void downgradeAllSharedCacheTableLocks(Btree *p){
67267 BtShared *pBt = p->pBt;
67268 if( pBt->pWriter==p ){
67269 BtLock *pLock;
67270 pBt->pWriter = 0;
67271 pBt->btsFlags &= ~(BTS_EXCLUSIVE0x0040|BTS_PENDING0x0080);
67272 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
67273 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p )((void) (0));
67274 pLock->eLock = READ_LOCK1;
67275 }
67276 }
67277}
67278
67279#endif /* SQLITE_OMIT_SHARED_CACHE */
67280
67281static void releasePage(MemPage *pPage); /* Forward reference */
67282static void releasePageOne(MemPage *pPage); /* Forward reference */
67283static void releasePageNotNull(MemPage *pPage); /* Forward reference */
67284
67285/*
67286***** This routine is used inside of assert() only ****
67287**
67288** Verify that the cursor holds the mutex on its BtShared
67289*/
67290#ifdef SQLITE_DEBUG
67291static int cursorHoldsMutex(BtCursor *p){
67292 return sqlite3_mutex_held(p->pBt->mutex);
67293}
67294
67295/* Verify that the cursor and the BtShared agree about what is the current
67296** database connetion. This is important in shared-cache mode. If the database
67297** connection pointers get out-of-sync, it is possible for routines like
67298** btreeInitPage() to reference an stale connection pointer that references a
67299** a connection that has already closed. This routine is used inside assert()
67300** statements only and for the purpose of double-checking that the btree code
67301** does keep the database connection pointers up-to-date.
67302*/
67303static int cursorOwnsBtShared(BtCursor *p){
67304 assert( cursorHoldsMutex(p) )((void) (0));
67305 return (p->pBtree->db==p->pBt->db);
67306}
67307#endif
67308
67309/*
67310** Invalidate the overflow cache of the cursor passed as the first argument.
67311** on the shared btree structure pBt.
67312*/
67313#define invalidateOverflowCache(pCur)(pCur->curFlags &= ~0x04) (pCur->curFlags &= ~BTCF_ValidOvfl0x04)
67314
67315/*
67316** Invalidate the overflow page-list cache for all cursors opened
67317** on the shared btree structure pBt.
67318*/
67319static void invalidateAllOverflowCache(BtShared *pBt){
67320 BtCursor *p;
67321 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
67322 for(p=pBt->pCursor; p; p=p->pNext){
67323 invalidateOverflowCache(p)(p->curFlags &= ~0x04);
67324 }
67325}
67326
67327#ifndef SQLITE_OMIT_INCRBLOB
67328/*
67329** This function is called before modifying the contents of a table
67330** to invalidate any incrblob cursors that are open on the
67331** row or one of the rows being modified.
67332**
67333** If argument isClearTable is true, then the entire contents of the
67334** table is about to be deleted. In this case invalidate all incrblob
67335** cursors open on any row within the table with root-page pgnoRoot.
67336**
67337** Otherwise, if argument isClearTable is false, then the row with
67338** rowid iRow is being replaced or deleted. In this case invalidate
67339** only those incrblob cursors open on that specific row.
67340*/
67341static void invalidateIncrblobCursors(
67342 Btree *pBtree, /* The database file to check */
67343 Pgno pgnoRoot, /* The table that might be changing */
67344 i64 iRow, /* The rowid that might be changing */
67345 int isClearTable /* True if all rows are being deleted */
67346){
67347 BtCursor *p;
67348 assert( pBtree->hasIncrblobCur )((void) (0));
67349 assert( sqlite3BtreeHoldsMutex(pBtree) )((void) (0));
67350 pBtree->hasIncrblobCur = 0;
67351 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
67352 if( (p->curFlags & BTCF_Incrblob0x10)!=0 ){
67353 pBtree->hasIncrblobCur = 1;
67354 if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
67355 p->eState = CURSOR_INVALID1;
67356 }
67357 }
67358 }
67359}
67360
67361#else
67362 /* Stub function when INCRBLOB is omitted */
67363 #define invalidateIncrblobCursors(w,x,y,z)
67364#endif /* SQLITE_OMIT_INCRBLOB */
67365
67366/*
67367** Set bit pgno of the BtShared.pHasContent bitvec. This is called
67368** when a page that previously contained data becomes a free-list leaf
67369** page.
67370**
67371** The BtShared.pHasContent bitvec exists to work around an obscure
67372** bug caused by the interaction of two useful IO optimizations surrounding
67373** free-list leaf pages:
67374**
67375** 1) When all data is deleted from a page and the page becomes
67376** a free-list leaf page, the page is not written to the database
67377** (as free-list leaf pages contain no meaningful data). Sometimes
67378** such a page is not even journalled (as it will not be modified,
67379** why bother journalling it?).
67380**
67381** 2) When a free-list leaf page is reused, its content is not read
67382** from the database or written to the journal file (why should it
67383** be, if it is not at all meaningful?).
67384**
67385** By themselves, these optimizations work fine and provide a handy
67386** performance boost to bulk delete or insert operations. However, if
67387** a page is moved to the free-list and then reused within the same
67388** transaction, a problem comes up. If the page is not journalled when
67389** it is moved to the free-list and it is also not journalled when it
67390** is extracted from the free-list and reused, then the original data
67391** may be lost. In the event of a rollback, it may not be possible
67392** to restore the database to its original configuration.
67393**
67394** The solution is the BtShared.pHasContent bitvec. Whenever a page is
67395** moved to become a free-list leaf page, the corresponding bit is
67396** set in the bitvec. Whenever a leaf page is extracted from the free-list,
67397** optimization 2 above is omitted if the corresponding bit is already
67398** set in BtShared.pHasContent. The contents of the bitvec are cleared
67399** at the end of every transaction.
67400*/
67401static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
67402 int rc = SQLITE_OK0;
67403 if( !pBt->pHasContent ){
67404 assert( pgno<=pBt->nPage )((void) (0));
67405 pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
67406 if( !pBt->pHasContent ){
67407 rc = SQLITE_NOMEM_BKPT7;
67408 }
67409 }
67410 if( rc==SQLITE_OK0 && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
67411 rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
67412 }
67413 return rc;
67414}
67415
67416/*
67417** Query the BtShared.pHasContent vector.
67418**
67419** This function is called when a free-list leaf page is removed from the
67420** free-list for reuse. It returns false if it is safe to retrieve the
67421** page from the pager layer with the 'no-content' flag set. True otherwise.
67422*/
67423static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
67424 Bitvec *p = pBt->pHasContent;
67425 return p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTestNotNull(p, pgno));
67426}
67427
67428/*
67429** Clear (destroy) the BtShared.pHasContent bitvec. This should be
67430** invoked at the conclusion of each write-transaction.
67431*/
67432static void btreeClearHasContent(BtShared *pBt){
67433 sqlite3BitvecDestroy(pBt->pHasContent);
67434 pBt->pHasContent = 0;
67435}
67436
67437/*
67438** Release all of the apPage[] pages for a cursor.
67439*/
67440static void btreeReleaseAllCursorPages(BtCursor *pCur){
67441 int i;
67442 if( pCur->iPage>=0 ){
67443 for(i=0; i<pCur->iPage; i++){
67444 releasePageNotNull(pCur->apPage[i]);
67445 }
67446 releasePageNotNull(pCur->pPage);
67447 pCur->iPage = -1;
67448 }
67449}
67450
67451/*
67452** The cursor passed as the only argument must point to a valid entry
67453** when this function is called (i.e. have eState==CURSOR_VALID). This
67454** function saves the current cursor key in variables pCur->nKey and
67455** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
67456** code otherwise.
67457**
67458** If the cursor is open on an intkey table, then the integer key
67459** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
67460** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
67461** set to point to a malloced buffer pCur->nKey bytes in size containing
67462** the key.
67463*/
67464static int saveCursorKey(BtCursor *pCur){
67465 int rc = SQLITE_OK0;
67466 assert( CURSOR_VALID==pCur->eState )((void) (0));
67467 assert( 0==pCur->pKey )((void) (0));
67468 assert( cursorHoldsMutex(pCur) )((void) (0));
67469
67470 if( pCur->curIntKey ){
67471 /* Only the rowid is required for a table btree */
67472 pCur->nKey = sqlite3BtreeIntegerKey(pCur);
67473 }else{
67474 /* For an index btree, save the complete key content. It is possible
67475 ** that the current key is corrupt. In that case, it is possible that
67476 ** the sqlite3VdbeRecordUnpack() function may overread the buffer by
67477 ** up to the size of 1 varint plus 1 8-byte value when the cursor
67478 ** position is restored. Hence the 17 bytes of padding allocated
67479 ** below. */
67480 void *pKey;
67481 pCur->nKey = sqlite3BtreePayloadSize(pCur);
67482 pKey = sqlite3Malloc( pCur->nKey + 9 + 8 );
67483 if( pKey ){
67484 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
67485 if( rc==SQLITE_OK0 ){
67486 memset(((u8*)pKey)+pCur->nKey, 0, 9+8);
67487 pCur->pKey = pKey;
67488 }else{
67489 sqlite3_free(pKey);
67490 }
67491 }else{
67492 rc = SQLITE_NOMEM_BKPT7;
67493 }
67494 }
67495 assert( !pCur->curIntKey || !pCur->pKey )((void) (0));
67496 return rc;
67497}
67498
67499/*
67500** Save the current cursor position in the variables BtCursor.nKey
67501** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
67502**
67503** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
67504** prior to calling this routine.
67505*/
67506static int saveCursorPosition(BtCursor *pCur){
67507 int rc;
67508
67509 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState )((void) (0));
67510 assert( 0==pCur->pKey )((void) (0));
67511 assert( cursorHoldsMutex(pCur) )((void) (0));
67512
67513 if( pCur->curFlags & BTCF_Pinned0x40 ){
67514 return SQLITE_CONSTRAINT_PINNED(19 |(11<<8));
67515 }
67516 if( pCur->eState==CURSOR_SKIPNEXT2 ){
67517 pCur->eState = CURSOR_VALID0;
67518 }else{
67519 pCur->skipNext = 0;
67520 }
67521
67522 rc = saveCursorKey(pCur);
67523 if( rc==SQLITE_OK0 ){
67524 btreeReleaseAllCursorPages(pCur);
67525 pCur->eState = CURSOR_REQUIRESEEK3;
67526 }
67527
67528 pCur->curFlags &= ~(BTCF_ValidNKey0x02|BTCF_ValidOvfl0x04|BTCF_AtLast0x08);
67529 return rc;
67530}
67531
67532/* Forward reference */
67533static int SQLITE_NOINLINE__attribute__((noinline)) saveCursorsOnList(BtCursor*,Pgno,BtCursor*);
67534
67535/*
67536** Save the positions of all cursors (except pExcept) that are open on
67537** the table with root-page iRoot. "Saving the cursor position" means that
67538** the location in the btree is remembered in such a way that it can be
67539** moved back to the same spot after the btree has been modified. This
67540** routine is called just before cursor pExcept is used to modify the
67541** table, for example in BtreeDelete() or BtreeInsert().
67542**
67543** If there are two or more cursors on the same btree, then all such
67544** cursors should have their BTCF_Multiple flag set. The btreeCursor()
67545** routine enforces that rule. This routine only needs to be called in
67546** the uncommon case when pExpect has the BTCF_Multiple flag set.
67547**
67548** If pExpect!=NULL and if no other cursors are found on the same root-page,
67549** then the BTCF_Multiple flag on pExpect is cleared, to avoid another
67550** pointless call to this routine.
67551**
67552** Implementation note: This routine merely checks to see if any cursors
67553** need to be saved. It calls out to saveCursorsOnList() in the (unusual)
67554** event that cursors are in need to being saved.
67555*/
67556static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
67557 BtCursor *p;
67558 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
67559 assert( pExcept==0 || pExcept->pBt==pBt )((void) (0));
67560 for(p=pBt->pCursor; p; p=p->pNext){
67561 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
67562 }
67563 if( p ) return saveCursorsOnList(p, iRoot, pExcept);
67564 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple0x20;
67565 return SQLITE_OK0;
67566}
67567
67568/* This helper routine to saveAllCursors does the actual work of saving
67569** the cursors if and when a cursor is found that actually requires saving.
67570** The common case is that no cursors need to be saved, so this routine is
67571** broken out from its caller to avoid unnecessary stack pointer movement.
67572*/
67573static int SQLITE_NOINLINE__attribute__((noinline)) saveCursorsOnList(
67574 BtCursor *p, /* The first cursor that needs saving */
67575 Pgno iRoot, /* Only save cursor with this iRoot. Save all if zero */
67576 BtCursor *pExcept /* Do not save this cursor */
67577){
67578 do{
67579 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
67580 if( p->eState==CURSOR_VALID0 || p->eState==CURSOR_SKIPNEXT2 ){
67581 int rc = saveCursorPosition(p);
67582 if( SQLITE_OK0!=rc ){
67583 return rc;
67584 }
67585 }else{
67586 testcase( p->iPage>=0 );
67587 btreeReleaseAllCursorPages(p);
67588 }
67589 }
67590 p = p->pNext;
67591 }while( p );
67592 return SQLITE_OK0;
67593}
67594
67595/*
67596** Clear the current cursor position.
67597*/
67598SQLITE_PRIVATEstatic void sqlite3BtreeClearCursor(BtCursor *pCur){
67599 assert( cursorHoldsMutex(pCur) )((void) (0));
67600 sqlite3_free(pCur->pKey);
67601 pCur->pKey = 0;
67602 pCur->eState = CURSOR_INVALID1;
67603}
67604
67605/*
67606** In this version of BtreeMoveto, pKey is a packed index record
67607** such as is generated by the OP_MakeRecord opcode. Unpack the
67608** record and then call sqlite3BtreeIndexMoveto() to do the work.
67609*/
67610static int btreeMoveto(
67611 BtCursor *pCur, /* Cursor open on the btree to be searched */
67612 const void *pKey, /* Packed key if the btree is an index */
67613 i64 nKey, /* Integer key for tables. Size of pKey for indices */
67614 int bias, /* Bias search to the high end */
67615 int *pRes /* Write search results here */
67616){
67617 int rc; /* Status code */
67618 UnpackedRecord *pIdxKey; /* Unpacked index key */
67619
67620 if( pKey ){
67621 KeyInfo *pKeyInfo = pCur->pKeyInfo;
67622 assert( nKey==(i64)(int)nKey )((void) (0));
67623 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
67624 if( pIdxKey==0 ) return SQLITE_NOMEM_BKPT7;
67625 sqlite3VdbeRecordUnpack(pKeyInfo, (int)nKey, pKey, pIdxKey);
67626 if( pIdxKey->nField==0 || pIdxKey->nField>pKeyInfo->nAllField ){
67627 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(67627);
67628 }else{
67629 rc = sqlite3BtreeIndexMoveto(pCur, pIdxKey, pRes);
67630 }
67631 sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
67632 }else{
67633 pIdxKey = 0;
67634 rc = sqlite3BtreeTableMoveto(pCur, nKey, bias, pRes);
67635 }
67636 return rc;
67637}
67638
67639/*
67640** Restore the cursor to the position it was in (or as close to as possible)
67641** when saveCursorPosition() was called. Note that this call deletes the
67642** saved position info stored by saveCursorPosition(), so there can be
67643** at most one effective restoreCursorPosition() call after each
67644** saveCursorPosition().
67645*/
67646static int btreeRestoreCursorPosition(BtCursor *pCur){
67647 int rc;
67648 int skipNext = 0;
67649 assert( cursorOwnsBtShared(pCur) )((void) (0));
67650 assert( pCur->eState>=CURSOR_REQUIRESEEK )((void) (0));
67651 if( pCur->eState==CURSOR_FAULT4 ){
67652 return pCur->skipNext;
67653 }
67654 pCur->eState = CURSOR_INVALID1;
67655 if( sqlite3FaultSim(410) ){
67656 rc = SQLITE_IOERR10;
67657 }else{
67658 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
67659 }
67660 if( rc==SQLITE_OK0 ){
67661 sqlite3_free(pCur->pKey);
67662 pCur->pKey = 0;
67663 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID )((void) (0));
67664 if( skipNext ) pCur->skipNext = skipNext;
67665 if( pCur->skipNext && pCur->eState==CURSOR_VALID0 ){
67666 pCur->eState = CURSOR_SKIPNEXT2;
67667 }
67668 }
67669 return rc;
67670}
67671
67672#define restoreCursorPosition(p)(p->eState>=3 ? btreeRestoreCursorPosition(p) : 0) \
67673 (p->eState>=CURSOR_REQUIRESEEK3 ? \
67674 btreeRestoreCursorPosition(p) : \
67675 SQLITE_OK0)
67676
67677/*
67678** Determine whether or not a cursor has moved from the position where
67679** it was last placed, or has been invalidated for any other reason.
67680** Cursors can move when the row they are pointing at is deleted out
67681** from under them, for example. Cursor might also move if a btree
67682** is rebalanced.
67683**
67684** Calling this routine with a NULL cursor pointer returns false.
67685**
67686** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor
67687** back to where it ought to be if this routine returns true.
67688*/
67689SQLITE_PRIVATEstatic int sqlite3BtreeCursorHasMoved(BtCursor *pCur){
67690 assert( EIGHT_BYTE_ALIGNMENT(pCur)((void) (0))
67691 || pCur==sqlite3BtreeFakeValidCursor() )((void) (0));
67692 assert( offsetof(BtCursor, eState)==0 )((void) (0));
67693 assert( sizeof(pCur->eState)==1 )((void) (0));
67694 return CURSOR_VALID0 != *(u8*)pCur;
67695}
67696
67697/*
67698** Return a pointer to a fake BtCursor object that will always answer
67699** false to the sqlite3BtreeCursorHasMoved() routine above. The fake
67700** cursor returned must not be used with any other Btree interface.
67701*/
67702SQLITE_PRIVATEstatic BtCursor *sqlite3BtreeFakeValidCursor(void){
67703 static u8 fakeCursor = CURSOR_VALID0;
67704 assert( offsetof(BtCursor, eState)==0 )((void) (0));
67705 return (BtCursor*)&fakeCursor;
67706}
67707
67708/*
67709** This routine restores a cursor back to its original position after it
67710** has been moved by some outside activity (such as a btree rebalance or
67711** a row having been deleted out from under the cursor).
67712**
67713** On success, the *pDifferentRow parameter is false if the cursor is left
67714** pointing at exactly the same row. *pDifferntRow is the row the cursor
67715** was pointing to has been deleted, forcing the cursor to point to some
67716** nearby row.
67717**
67718** This routine should only be called for a cursor that just returned
67719** TRUE from sqlite3BtreeCursorHasMoved().
67720*/
67721SQLITE_PRIVATEstatic int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){
67722 int rc;
67723
67724 assert( pCur!=0 )((void) (0));
67725 assert( pCur->eState!=CURSOR_VALID )((void) (0));
67726 rc = restoreCursorPosition(pCur)(pCur->eState>=3 ? btreeRestoreCursorPosition(pCur) : 0
)
;
67727 if( rc ){
67728 *pDifferentRow = 1;
67729 return rc;
67730 }
67731 if( pCur->eState!=CURSOR_VALID0 ){
67732 *pDifferentRow = 1;
67733 }else{
67734 *pDifferentRow = 0;
67735 }
67736 return SQLITE_OK0;
67737}
67738
67739#ifdef SQLITE_ENABLE_CURSOR_HINTS
67740/*
67741** Provide hints to the cursor. The particular hint given (and the type
67742** and number of the varargs parameters) is determined by the eHintType
67743** parameter. See the definitions of the BTREE_HINT_* macros for details.
67744*/
67745SQLITE_PRIVATEstatic void sqlite3BtreeCursorHint(BtCursor *pCur, int eHintType, ...){
67746 /* Used only by system that substitute their own storage engine */
67747}
67748#endif
67749
67750/*
67751** Provide flag hints to the cursor.
67752*/
67753SQLITE_PRIVATEstatic void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){
67754 assert( x==BTREE_SEEK_EQ || x==BTREE_BULKLOAD || x==0 )((void) (0));
67755 pCur->hints = x;
67756}
67757
67758
67759#ifndef SQLITE_OMIT_AUTOVACUUM
67760/*
67761** Given a page number of a regular database page, return the page
67762** number for the pointer-map page that contains the entry for the
67763** input page number.
67764**
67765** Return 0 (not a valid page) for pgno==1 since there is
67766** no pointer map associated with page 1. The integrity_check logic
67767** requires that ptrmapPageno(*,1)!=1.
67768*/
67769static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
67770 int nPagesPerMapPage;
67771 Pgno iPtrMap, ret;
67772 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
67773 if( pgno<2 ) return 0;
67774 nPagesPerMapPage = (pBt->usableSize/5)+1;
67775 iPtrMap = (pgno-2)/nPagesPerMapPage;
67776 ret = (iPtrMap*nPagesPerMapPage) + 2;
67777 if( ret==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
67778 ret++;
67779 }
67780 return ret;
67781}
67782
67783/*
67784** Write an entry into the pointer map.
67785**
67786** This routine updates the pointer map entry for page number 'key'
67787** so that it maps to type 'eType' and parent page number 'pgno'.
67788**
67789** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
67790** a no-op. If an error occurs, the appropriate error code is written
67791** into *pRC.
67792*/
67793static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
67794 DbPage *pDbPage; /* The pointer map page */
67795 u8 *pPtrmap; /* The pointer map data */
67796 Pgno iPtrmap; /* The pointer map page number */
67797 int offset; /* Offset in pointer map page */
67798 int rc; /* Return code from subfunctions */
67799
67800 if( *pRC ) return;
67801
67802 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
67803 /* The super-journal page number must never be used as a pointer map page */
67804 assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) )((void) (0));
67805
67806 assert( pBt->autoVacuum )((void) (0));
67807 if( key==0 ){
67808 *pRC = SQLITE_CORRUPT_BKPTsqlite3CorruptError(67808);
67809 return;
67810 }
67811 iPtrmap = PTRMAP_PAGENO(pBt, key)ptrmapPageno(pBt, key);
67812 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
67813 if( rc!=SQLITE_OK0 ){
67814 *pRC = rc;
67815 return;
67816 }
67817 if( ((char*)sqlite3PagerGetExtra(pDbPage))[0]!=0 ){
67818 /* The first byte of the extra data is the MemPage.isInit byte.
67819 ** If that byte is set, it means this page is also being used
67820 ** as a btree page. */
67821 *pRC = SQLITE_CORRUPT_BKPTsqlite3CorruptError(67821);
67822 goto ptrmap_exit;
67823 }
67824 offset = PTRMAP_PTROFFSET(iPtrmap, key)(5*(key-iPtrmap-1));
67825 if( offset<0 ){
67826 *pRC = SQLITE_CORRUPT_BKPTsqlite3CorruptError(67826);
67827 goto ptrmap_exit;
67828 }
67829 assert( offset <= (int)pBt->usableSize-5 )((void) (0));
67830 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
67831
67832 if( eType!=pPtrmap[offset] || get4bytesqlite3Get4byte(&pPtrmap[offset+1])!=parent ){
67833 TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
67834 *pRC= rc = sqlite3PagerWrite(pDbPage);
67835 if( rc==SQLITE_OK0 ){
67836 pPtrmap[offset] = eType;
67837 put4bytesqlite3Put4byte(&pPtrmap[offset+1], parent);
67838 }
67839 }
67840
67841ptrmap_exit:
67842 sqlite3PagerUnref(pDbPage);
67843}
67844
67845/*
67846** Read an entry from the pointer map.
67847**
67848** This routine retrieves the pointer map entry for page 'key', writing
67849** the type and parent page number to *pEType and *pPgno respectively.
67850** An error code is returned if something goes wrong, otherwise SQLITE_OK.
67851*/
67852static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
67853 DbPage *pDbPage; /* The pointer map page */
67854 int iPtrmap; /* Pointer map page index */
67855 u8 *pPtrmap; /* Pointer map page data */
67856 int offset; /* Offset of entry in pointer map */
67857 int rc;
67858
67859 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
67860
67861 iPtrmap = PTRMAP_PAGENO(pBt, key)ptrmapPageno(pBt, key);
67862 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
67863 if( rc!=0 ){
67864 return rc;
67865 }
67866 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
67867
67868 offset = PTRMAP_PTROFFSET(iPtrmap, key)(5*(key-iPtrmap-1));
67869 if( offset<0 ){
67870 sqlite3PagerUnref(pDbPage);
67871 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(67871);
67872 }
67873 assert( offset <= (int)pBt->usableSize-5 )((void) (0));
67874 assert( pEType!=0 )((void) (0));
67875 *pEType = pPtrmap[offset];
67876 if( pPgno ) *pPgno = get4bytesqlite3Get4byte(&pPtrmap[offset+1]);
67877
67878 sqlite3PagerUnref(pDbPage);
67879 if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_PGNO(iPtrmap)sqlite3CorruptError(67879);
67880 return SQLITE_OK0;
67881}
67882
67883#else /* if defined SQLITE_OMIT_AUTOVACUUM */
67884 #define ptrmapPut(w,x,y,z,rc)
67885 #define ptrmapGet(w,x,y,z) SQLITE_OK0
67886 #define ptrmapPutOvflPtr(x, y, z, rc)
67887#endif
67888
67889/*
67890** Given a btree page and a cell index (0 means the first cell on
67891** the page, 1 means the second cell, and so forth) return a pointer
67892** to the cell content.
67893**
67894** findCellPastPtr() does the same except it skips past the initial
67895** 4-byte child pointer found on interior pages, if there is one.
67896**
67897** This routine works only for pages that do not contain overflow cells.
67898*/
67899#define findCell(P,I)((P)->aData + ((P)->maskPage & ((&(P)->aCellIdx
[2*(I)])[0]<<8 | (&(P)->aCellIdx[2*(I)])[1])))
\
67900 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])((&(P)->aCellIdx[2*(I)])[0]<<8 | (&(P)->aCellIdx
[2*(I)])[1])
))
67901#define findCellPastPtr(P,I)((P)->aDataOfst + ((P)->maskPage & ((&(P)->aCellIdx
[2*(I)])[0]<<8 | (&(P)->aCellIdx[2*(I)])[1])))
\
67902 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])((&(P)->aCellIdx[2*(I)])[0]<<8 | (&(P)->aCellIdx
[2*(I)])[1])
))
67903
67904
67905/*
67906** This is common tail processing for btreeParseCellPtr() and
67907** btreeParseCellPtrIndex() for the case when the cell does not fit entirely
67908** on a single B-tree page. Make necessary adjustments to the CellInfo
67909** structure.
67910*/
67911static SQLITE_NOINLINE__attribute__((noinline)) void btreeParseCellAdjustSizeForOverflow(
67912 MemPage *pPage, /* Page containing the cell */
67913 u8 *pCell, /* Pointer to the cell text. */
67914 CellInfo *pInfo /* Fill in this structure */
67915){
67916 /* If the payload will not fit completely on the local page, we have
67917 ** to decide how much to store locally and how much to spill onto
67918 ** overflow pages. The strategy is to minimize the amount of unused
67919 ** space on overflow pages while keeping the amount of local storage
67920 ** in between minLocal and maxLocal.
67921 **
67922 ** Warning: changing the way overflow payload is distributed in any
67923 ** way will result in an incompatible file format.
67924 */
67925 int minLocal; /* Minimum amount of payload held locally */
67926 int maxLocal; /* Maximum amount of payload held locally */
67927 int surplus; /* Overflow payload available for local storage */
67928
67929 minLocal = pPage->minLocal;
67930 maxLocal = pPage->maxLocal;
67931 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
67932 testcase( surplus==maxLocal );
67933 testcase( surplus==maxLocal+1 );
67934 if( surplus <= maxLocal ){
67935 pInfo->nLocal = (u16)surplus;
67936 }else{
67937 pInfo->nLocal = (u16)minLocal;
67938 }
67939 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
67940}
67941
67942/*
67943** Given a record with nPayload bytes of payload stored within btree
67944** page pPage, return the number of bytes of payload stored locally.
67945*/
67946static int btreePayloadToLocal(MemPage *pPage, i64 nPayload){
67947 int maxLocal; /* Maximum amount of payload held locally */
67948 maxLocal = pPage->maxLocal;
67949 if( nPayload<=maxLocal ){
67950 return nPayload;
67951 }else{
67952 int minLocal; /* Minimum amount of payload held locally */
67953 int surplus; /* Overflow payload available for local storage */
67954 minLocal = pPage->minLocal;
67955 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize-4);
67956 return ( surplus <= maxLocal ) ? surplus : minLocal;
67957 }
67958}
67959
67960/*
67961** The following routines are implementations of the MemPage.xParseCell()
67962** method.
67963**
67964** Parse a cell content block and fill in the CellInfo structure.
67965**
67966** btreeParseCellPtr() => table btree leaf nodes
67967** btreeParseCellNoPayload() => table btree internal nodes
67968** btreeParseCellPtrIndex() => index btree nodes
67969**
67970** There is also a wrapper function btreeParseCell() that works for
67971** all MemPage types and that references the cell by index rather than
67972** by pointer.
67973*/
67974static void btreeParseCellPtrNoPayload(
67975 MemPage *pPage, /* Page containing the cell */
67976 u8 *pCell, /* Pointer to the cell text. */
67977 CellInfo *pInfo /* Fill in this structure */
67978){
67979 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
67980 assert( pPage->leaf==0 )((void) (0));
67981 assert( pPage->childPtrSize==4 )((void) (0));
67982#ifndef SQLITE_DEBUG
67983 UNUSED_PARAMETER(pPage)(void)(pPage);
67984#endif
67985 pInfo->nSize = 4 + getVarintsqlite3GetVarint(&pCell[4], (u64*)&pInfo->nKey);
67986 pInfo->nPayload = 0;
67987 pInfo->nLocal = 0;
67988 pInfo->pPayload = 0;
67989 return;
67990}
67991static void btreeParseCellPtr(
67992 MemPage *pPage, /* Page containing the cell */
67993 u8 *pCell, /* Pointer to the cell text. */
67994 CellInfo *pInfo /* Fill in this structure */
67995){
67996 u8 *pIter; /* For scanning through pCell */
67997 u32 nPayload; /* Number of bytes of cell payload */
67998 u64 iKey; /* Extracted Key value */
67999
68000 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
68001 assert( pPage->leaf==0 || pPage->leaf==1 )((void) (0));
68002 assert( pPage->intKeyLeaf )((void) (0));
68003 assert( pPage->childPtrSize==0 )((void) (0));
68004 pIter = pCell;
68005
68006 /* The next block of code is equivalent to:
68007 **
68008 ** pIter += getVarint32(pIter, nPayload);
68009 **
68010 ** The code is inlined to avoid a function call.
68011 */
68012 nPayload = *pIter;
68013 if( nPayload>=0x80 ){
68014 u8 *pEnd = &pIter[8];
68015 nPayload &= 0x7f;
68016 do{
68017 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
68018 }while( (*pIter)>=0x80 && pIter<pEnd );
68019 }
68020 pIter++;
68021
68022 /* The next block of code is equivalent to:
68023 **
68024 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
68025 **
68026 ** The code is inlined and the loop is unrolled for performance.
68027 ** This routine is a high-runner.
68028 */
68029 iKey = *pIter;
68030 if( iKey>=0x80 ){
68031 u8 x;
68032 iKey = ((iKey&0x7f)<<7) | ((x = *++pIter) & 0x7f);
68033 if( x>=0x80 ){
68034 iKey = (iKey<<7) | ((x =*++pIter) & 0x7f);
68035 if( x>=0x80 ){
68036 iKey = (iKey<<7) | ((x = *++pIter) & 0x7f);
68037 if( x>=0x80 ){
68038 iKey = (iKey<<7) | ((x = *++pIter) & 0x7f);
68039 if( x>=0x80 ){
68040 iKey = (iKey<<7) | ((x = *++pIter) & 0x7f);
68041 if( x>=0x80 ){
68042 iKey = (iKey<<7) | ((x = *++pIter) & 0x7f);
68043 if( x>=0x80 ){
68044 iKey = (iKey<<7) | ((x = *++pIter) & 0x7f);
68045 if( x>=0x80 ){
68046 iKey = (iKey<<8) | (*++pIter);
68047 }
68048 }
68049 }
68050 }
68051 }
68052 }
68053 }
68054 }
68055 pIter++;
68056
68057 pInfo->nKey = *(i64*)&iKey;
68058 pInfo->nPayload = nPayload;
68059 pInfo->pPayload = pIter;
68060 testcase( nPayload==pPage->maxLocal );
68061 testcase( nPayload==(u32)pPage->maxLocal+1 );
68062 if( nPayload<=pPage->maxLocal ){
68063 /* This is the (easy) common case where the entire payload fits
68064 ** on the local page. No overflow is required.
68065 */
68066 pInfo->nSize = nPayload + (u16)(pIter - pCell);
68067 if( pInfo->nSize<4 ) pInfo->nSize = 4;
68068 pInfo->nLocal = (u16)nPayload;
68069 }else{
68070 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
68071 }
68072}
68073static void btreeParseCellPtrIndex(
68074 MemPage *pPage, /* Page containing the cell */
68075 u8 *pCell, /* Pointer to the cell text. */
68076 CellInfo *pInfo /* Fill in this structure */
68077){
68078 u8 *pIter; /* For scanning through pCell */
68079 u32 nPayload; /* Number of bytes of cell payload */
68080
68081 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
68082 assert( pPage->leaf==0 || pPage->leaf==1 )((void) (0));
68083 assert( pPage->intKeyLeaf==0 )((void) (0));
68084 pIter = pCell + pPage->childPtrSize;
68085 nPayload = *pIter;
68086 if( nPayload>=0x80 ){
68087 u8 *pEnd = &pIter[8];
68088 nPayload &= 0x7f;
68089 do{
68090 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
68091 }while( *(pIter)>=0x80 && pIter<pEnd );
68092 }
68093 pIter++;
68094 pInfo->nKey = nPayload;
68095 pInfo->nPayload = nPayload;
68096 pInfo->pPayload = pIter;
68097 testcase( nPayload==pPage->maxLocal );
68098 testcase( nPayload==(u32)pPage->maxLocal+1 );
68099 if( nPayload<=pPage->maxLocal ){
68100 /* This is the (easy) common case where the entire payload fits
68101 ** on the local page. No overflow is required.
68102 */
68103 pInfo->nSize = nPayload + (u16)(pIter - pCell);
68104 if( pInfo->nSize<4 ) pInfo->nSize = 4;
68105 pInfo->nLocal = (u16)nPayload;
68106 }else{
68107 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
68108 }
68109}
68110static void btreeParseCell(
68111 MemPage *pPage, /* Page containing the cell */
68112 int iCell, /* The cell index. First cell is 0 */
68113 CellInfo *pInfo /* Fill in this structure */
68114){
68115 pPage->xParseCell(pPage, findCell(pPage, iCell)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(iCell)])[0]<<8 | (&(pPage)->aCellIdx
[2*(iCell)])[1])))
, pInfo);
68116}
68117
68118/*
68119** The following routines are implementations of the MemPage.xCellSize
68120** method.
68121**
68122** Compute the total number of bytes that a Cell needs in the cell
68123** data area of the btree-page. The return number includes the cell
68124** data header and the local payload, but not any overflow page or
68125** the space used by the cell pointer.
68126**
68127** cellSizePtrNoPayload() => table internal nodes
68128** cellSizePtrTableLeaf() => table leaf nodes
68129** cellSizePtr() => all index nodes & table leaf nodes
68130*/
68131static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
68132 u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */
68133 u8 *pEnd; /* End mark for a varint */
68134 u32 nSize; /* Size value to return */
68135
68136#ifdef SQLITE_DEBUG
68137 /* The value returned by this function should always be the same as
68138 ** the (CellInfo.nSize) value found by doing a full parse of the
68139 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
68140 ** this function verifies that this invariant is not violated. */
68141 CellInfo debuginfo;
68142 pPage->xParseCell(pPage, pCell, &debuginfo);
68143#endif
68144
68145 nSize = *pIter;
68146 if( nSize>=0x80 ){
68147 pEnd = &pIter[8];
68148 nSize &= 0x7f;
68149 do{
68150 nSize = (nSize<<7) | (*++pIter & 0x7f);
68151 }while( *(pIter)>=0x80 && pIter<pEnd );
68152 }
68153 pIter++;
68154 testcase( nSize==pPage->maxLocal );
68155 testcase( nSize==(u32)pPage->maxLocal+1 );
68156 if( nSize<=pPage->maxLocal ){
68157 nSize += (u32)(pIter - pCell);
68158 if( nSize<4 ) nSize = 4;
68159 }else{
68160 int minLocal = pPage->minLocal;
68161 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
68162 testcase( nSize==pPage->maxLocal );
68163 testcase( nSize==(u32)pPage->maxLocal+1 );
68164 if( nSize>pPage->maxLocal ){
68165 nSize = minLocal;
68166 }
68167 nSize += 4 + (u16)(pIter - pCell);
68168 }
68169 assert( nSize==debuginfo.nSize || CORRUPT_DB )((void) (0));
68170 return (u16)nSize;
68171}
68172static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){
68173 u8 *pIter = pCell + 4; /* For looping over bytes of pCell */
68174 u8 *pEnd; /* End mark for a varint */
68175
68176#ifdef SQLITE_DEBUG
68177 /* The value returned by this function should always be the same as
68178 ** the (CellInfo.nSize) value found by doing a full parse of the
68179 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
68180 ** this function verifies that this invariant is not violated. */
68181 CellInfo debuginfo;
68182 pPage->xParseCell(pPage, pCell, &debuginfo);
68183#else
68184 UNUSED_PARAMETER(pPage)(void)(pPage);
68185#endif
68186
68187 assert( pPage->childPtrSize==4 )((void) (0));
68188 pEnd = pIter + 9;
68189 while( (*pIter++)&0x80 && pIter<pEnd );
68190 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB )((void) (0));
68191 return (u16)(pIter - pCell);
68192}
68193static u16 cellSizePtrTableLeaf(MemPage *pPage, u8 *pCell){
68194 u8 *pIter = pCell; /* For looping over bytes of pCell */
68195 u8 *pEnd; /* End mark for a varint */
68196 u32 nSize; /* Size value to return */
68197
68198#ifdef SQLITE_DEBUG
68199 /* The value returned by this function should always be the same as
68200 ** the (CellInfo.nSize) value found by doing a full parse of the
68201 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
68202 ** this function verifies that this invariant is not violated. */
68203 CellInfo debuginfo;
68204 pPage->xParseCell(pPage, pCell, &debuginfo);
68205#endif
68206
68207 nSize = *pIter;
68208 if( nSize>=0x80 ){
68209 pEnd = &pIter[8];
68210 nSize &= 0x7f;
68211 do{
68212 nSize = (nSize<<7) | (*++pIter & 0x7f);
68213 }while( *(pIter)>=0x80 && pIter<pEnd );
68214 }
68215 pIter++;
68216 /* pIter now points at the 64-bit integer key value, a variable length
68217 ** integer. The following block moves pIter to point at the first byte
68218 ** past the end of the key value. */
68219 if( (*pIter++)&0x80
68220 && (*pIter++)&0x80
68221 && (*pIter++)&0x80
68222 && (*pIter++)&0x80
68223 && (*pIter++)&0x80
68224 && (*pIter++)&0x80
68225 && (*pIter++)&0x80
68226 && (*pIter++)&0x80 ){ pIter++; }
68227 testcase( nSize==pPage->maxLocal );
68228 testcase( nSize==(u32)pPage->maxLocal+1 );
68229 if( nSize<=pPage->maxLocal ){
68230 nSize += (u32)(pIter - pCell);
68231 if( nSize<4 ) nSize = 4;
68232 }else{
68233 int minLocal = pPage->minLocal;
68234 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
68235 testcase( nSize==pPage->maxLocal );
68236 testcase( nSize==(u32)pPage->maxLocal+1 );
68237 if( nSize>pPage->maxLocal ){
68238 nSize = minLocal;
68239 }
68240 nSize += 4 + (u16)(pIter - pCell);
68241 }
68242 assert( nSize==debuginfo.nSize || CORRUPT_DB )((void) (0));
68243 return (u16)nSize;
68244}
68245
68246
68247#ifdef SQLITE_DEBUG
68248/* This variation on cellSizePtr() is used inside of assert() statements
68249** only. */
68250static u16 cellSize(MemPage *pPage, int iCell){
68251 return pPage->xCellSize(pPage, findCell(pPage, iCell)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(iCell)])[0]<<8 | (&(pPage)->aCellIdx
[2*(iCell)])[1])))
);
68252}
68253#endif
68254
68255#ifndef SQLITE_OMIT_AUTOVACUUM
68256/*
68257** The cell pCell is currently part of page pSrc but will ultimately be part
68258** of pPage. (pSrc and pPage are often the same.) If pCell contains a
68259** pointer to an overflow page, insert an entry into the pointer-map for
68260** the overflow page that will be valid after pCell has been moved to pPage.
68261*/
68262static void ptrmapPutOvflPtr(MemPage *pPage, MemPage *pSrc, u8 *pCell,int *pRC){
68263 CellInfo info;
68264 if( *pRC ) return;
68265 assert( pCell!=0 )((void) (0));
68266 pPage->xParseCell(pPage, pCell, &info);
68267 if( info.nLocal<info.nPayload ){
68268 Pgno ovfl;
68269 if( SQLITE_WITHIN(pSrc->aDataEnd, pCell, pCell+info.nLocal)(((uptr)(pSrc->aDataEnd)>=(uptr)(pCell))&&((uptr
)(pSrc->aDataEnd)<(uptr)(pCell+info.nLocal)))
){
68270 testcase( pSrc!=pPage );
68271 *pRC = SQLITE_CORRUPT_BKPTsqlite3CorruptError(68271);
68272 return;
68273 }
68274 ovfl = get4bytesqlite3Get4byte(&pCell[info.nSize-4]);
68275 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW13, pPage->pgno, pRC);
68276 }
68277}
68278#endif
68279
68280
68281/*
68282** Defragment the page given. This routine reorganizes cells within the
68283** page so that there are no free-blocks on the free-block list.
68284**
68285** Parameter nMaxFrag is the maximum amount of fragmented space that may be
68286** present in the page after this routine returns.
68287**
68288** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
68289** b-tree page so that there are no freeblocks or fragment bytes, all
68290** unused bytes are contained in the unallocated space region, and all
68291** cells are packed tightly at the end of the page.
68292*/
68293static int defragmentPage(MemPage *pPage, int nMaxFrag){
68294 int i; /* Loop counter */
68295 int pc; /* Address of the i-th cell */
68296 int hdr; /* Offset to the page header */
68297 int size; /* Size of a cell */
68298 int usableSize; /* Number of usable bytes on a page */
68299 int cellOffset; /* Offset to the cell pointer array */
68300 int cbrk; /* Offset to the cell content area */
68301 int nCell; /* Number of cells on the page */
68302 unsigned char *data; /* The page data */
68303 unsigned char *temp; /* Temp area for cell content */
68304 unsigned char *src; /* Source of content */
68305 int iCellFirst; /* First allowable cell index */
68306 int iCellLast; /* Last possible cell index */
68307 int iCellStart; /* First cell offset in input */
68308
68309 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
68310 assert( pPage->pBt!=0 )((void) (0));
68311 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE )((void) (0));
68312 assert( pPage->nOverflow==0 )((void) (0));
68313 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
68314 temp = 0;
68315 src = data = pPage->aData;
68316 hdr = pPage->hdrOffset;
68317 cellOffset = pPage->cellOffset;
68318 nCell = pPage->nCell;
68319 assert( nCell==get2byte(&data[hdr+3]) || CORRUPT_DB )((void) (0));
68320 iCellFirst = cellOffset + 2*nCell;
68321 usableSize = pPage->pBt->usableSize;
68322
68323 /* This block handles pages with two or fewer free blocks and nMaxFrag
68324 ** or fewer fragmented bytes. In this case it is faster to move the
68325 ** two (or one) blocks of cells using memmove() and add the required
68326 ** offsets to each pointer in the cell-pointer array than it is to
68327 ** reconstruct the entire page. */
68328 if( (int)data[hdr+7]<=nMaxFrag ){
68329 int iFree = get2byte(&data[hdr+1])((&data[hdr+1])[0]<<8 | (&data[hdr+1])[1]);
68330 if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68330);
68331 if( iFree ){
68332 int iFree2 = get2byte(&data[iFree])((&data[iFree])[0]<<8 | (&data[iFree])[1]);
68333 if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68333);
68334 if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
68335 u8 *pEnd = &data[cellOffset + nCell*2];
68336 u8 *pAddr;
68337 int sz2 = 0;
68338 int sz = get2byte(&data[iFree+2])((&data[iFree+2])[0]<<8 | (&data[iFree+2])[1]);
68339 int top = get2byte(&data[hdr+5])((&data[hdr+5])[0]<<8 | (&data[hdr+5])[1]);
68340 if( top>=iFree ){
68341 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68341);
68342 }
68343 if( iFree2 ){
68344 if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68344);
68345 sz2 = get2byte(&data[iFree2+2])((&data[iFree2+2])[0]<<8 | (&data[iFree2+2])[1]
)
;
68346 if( iFree2+sz2 > usableSize ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68346);
68347 memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
68348 sz += sz2;
68349 }else if( NEVER(iFree+sz>usableSize)(iFree+sz>usableSize) ){
68350 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68350);
68351 }
68352
68353 cbrk = top+sz;
68354 assert( cbrk+(iFree-top) <= usableSize )((void) (0));
68355 memmove(&data[cbrk], &data[top], iFree-top);
68356 for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){
68357 pc = get2byte(pAddr)((pAddr)[0]<<8 | (pAddr)[1]);
68358 if( pc<iFree ){ put2byte(pAddr, pc+sz)((pAddr)[0] = (u8)((pc+sz)>>8), (pAddr)[1] = (u8)(pc+sz
))
; }
68359 else if( pc<iFree2 ){ put2byte(pAddr, pc+sz2)((pAddr)[0] = (u8)((pc+sz2)>>8), (pAddr)[1] = (u8)(pc+sz2
))
; }
68360 }
68361 goto defragment_out;
68362 }
68363 }
68364 }
68365
68366 cbrk = usableSize;
68367 iCellLast = usableSize - 4;
68368 iCellStart = get2byte(&data[hdr+5])((&data[hdr+5])[0]<<8 | (&data[hdr+5])[1]);
68369 for(i=0; i<nCell; i++){
68370 u8 *pAddr; /* The i-th cell pointer */
68371 pAddr = &data[cellOffset + i*2];
68372 pc = get2byte(pAddr)((pAddr)[0]<<8 | (pAddr)[1]);
68373 testcase( pc==iCellFirst );
68374 testcase( pc==iCellLast );
68375 /* These conditions have already been verified in btreeInitPage()
68376 ** if PRAGMA cell_size_check=ON.
68377 */
68378 if( pc<iCellStart || pc>iCellLast ){
68379 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68379);
68380 }
68381 assert( pc>=iCellStart && pc<=iCellLast )((void) (0));
68382 size = pPage->xCellSize(pPage, &src[pc]);
68383 cbrk -= size;
68384 if( cbrk<iCellStart || pc+size>usableSize ){
68385 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68385);
68386 }
68387 assert( cbrk+size<=usableSize && cbrk>=iCellStart )((void) (0));
68388 testcase( cbrk+size==usableSize );
68389 testcase( pc+size==usableSize );
68390 put2byte(pAddr, cbrk)((pAddr)[0] = (u8)((cbrk)>>8), (pAddr)[1] = (u8)(cbrk));
68391 if( temp==0 ){
68392 if( cbrk==pc ) continue;
68393 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
68394 memcpy(&temp[iCellStart], &data[iCellStart], usableSize - iCellStart);
68395 src = temp;
68396 }
68397 memcpy(&data[cbrk], &src[pc], size);
68398 }
68399 data[hdr+7] = 0;
68400
68401 defragment_out:
68402 assert( pPage->nFree>=0 )((void) (0));
68403 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
68404 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68404);
68405 }
68406 assert( cbrk>=iCellFirst )((void) (0));
68407 put2byte(&data[hdr+5], cbrk)((&data[hdr+5])[0] = (u8)((cbrk)>>8), (&data[hdr
+5])[1] = (u8)(cbrk))
;
68408 data[hdr+1] = 0;
68409 data[hdr+2] = 0;
68410 memset(&data[iCellFirst], 0, cbrk-iCellFirst);
68411 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
68412 return SQLITE_OK0;
68413}
68414
68415/*
68416** Search the free-list on page pPg for space to store a cell nByte bytes in
68417** size. If one can be found, return a pointer to the space and remove it
68418** from the free-list.
68419**
68420** If no suitable space can be found on the free-list, return NULL.
68421**
68422** This function may detect corruption within pPg. If corruption is
68423** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
68424**
68425** Slots on the free list that are between 1 and 3 bytes larger than nByte
68426** will be ignored if adding the extra space to the fragmentation count
68427** causes the fragmentation count to exceed 60.
68428*/
68429static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
68430 const int hdr = pPg->hdrOffset; /* Offset to page header */
68431 u8 * const aData = pPg->aData; /* Page data */
68432 int iAddr = hdr + 1; /* Address of ptr to pc */
68433 u8 *pTmp = &aData[iAddr]; /* Temporary ptr into aData[] */
68434 int pc = get2byte(pTmp)((pTmp)[0]<<8 | (pTmp)[1]); /* Address of a free slot */
68435 int x; /* Excess size of the slot */
68436 int maxPC = pPg->pBt->usableSize - nByte; /* Max address for a usable slot */
68437 int size; /* Size of the free slot */
68438
68439 assert( pc>0 )((void) (0));
68440 while( pc<=maxPC ){
68441 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
68442 ** freeblock form a big-endian integer which is the size of the freeblock
68443 ** in bytes, including the 4-byte header. */
68444 pTmp = &aData[pc+2];
68445 size = get2byte(pTmp)((pTmp)[0]<<8 | (pTmp)[1]);
68446 if( (x = size - nByte)>=0 ){
68447 testcase( x==4 );
68448 testcase( x==3 );
68449 if( x<4 ){
68450 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
68451 ** number of bytes in fragments may not exceed 60. */
68452 if( aData[hdr+7]>57 ) return 0;
68453
68454 /* Remove the slot from the free-list. Update the number of
68455 ** fragmented bytes within the page. */
68456 memcpy(&aData[iAddr], &aData[pc], 2);
68457 aData[hdr+7] += (u8)x;
68458 testcase( pc+x>maxPC );
68459 return &aData[pc];
68460 }else if( x+pc > maxPC ){
68461 /* This slot extends off the end of the usable part of the page */
68462 *pRc = SQLITE_CORRUPT_PAGE(pPg)sqlite3CorruptError(68462);
68463 return 0;
68464 }else{
68465 /* The slot remains on the free-list. Reduce its size to account
68466 ** for the portion used by the new allocation. */
68467 put2byte(&aData[pc+2], x)((&aData[pc+2])[0] = (u8)((x)>>8), (&aData[pc+2
])[1] = (u8)(x))
;
68468 }
68469 return &aData[pc + x];
68470 }
68471 iAddr = pc;
68472 pTmp = &aData[pc];
68473 pc = get2byte(pTmp)((pTmp)[0]<<8 | (pTmp)[1]);
68474 if( pc<=iAddr+size ){
68475 if( pc ){
68476 /* The next slot in the chain is not past the end of the current slot */
68477 *pRc = SQLITE_CORRUPT_PAGE(pPg)sqlite3CorruptError(68477);
68478 }
68479 return 0;
68480 }
68481 }
68482 if( pc>maxPC+nByte-4 ){
68483 /* The free slot chain extends off the end of the page */
68484 *pRc = SQLITE_CORRUPT_PAGE(pPg)sqlite3CorruptError(68484);
68485 }
68486 return 0;
68487}
68488
68489/*
68490** Allocate nByte bytes of space from within the B-Tree page passed
68491** as the first argument. Write into *pIdx the index into pPage->aData[]
68492** of the first byte of allocated space. Return either SQLITE_OK or
68493** an error code (usually SQLITE_CORRUPT).
68494**
68495** The caller guarantees that there is sufficient space to make the
68496** allocation. This routine might need to defragment in order to bring
68497** all the space together, however. This routine will avoid using
68498** the first two bytes past the cell pointer area since presumably this
68499** allocation is being made in order to insert a new cell, so we will
68500** also end up needing a new cell pointer.
68501*/
68502static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
68503 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
68504 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
68505 int top; /* First byte of cell content area */
68506 int rc = SQLITE_OK0; /* Integer return code */
68507 u8 *pTmp; /* Temp ptr into data[] */
68508 int gap; /* First byte of gap between cell pointers and cell content */
68509
68510 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
68511 assert( pPage->pBt )((void) (0));
68512 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
68513 assert( nByte>=0 )((void) (0)); /* Minimum cell size is 4 */
68514 assert( pPage->nFree>=nByte )((void) (0));
68515 assert( pPage->nOverflow==0 )((void) (0));
68516 assert( nByte < (int)(pPage->pBt->usableSize-8) )((void) (0));
68517
68518 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf )((void) (0));
68519 gap = pPage->cellOffset + 2*pPage->nCell;
68520 assert( gap<=65536 )((void) (0));
68521 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
68522 ** and the reserved space is zero (the usual value for reserved space)
68523 ** then the cell content offset of an empty page wants to be 65536.
68524 ** However, that integer is too large to be stored in a 2-byte unsigned
68525 ** integer, so a value of 0 is used in its place. */
68526 pTmp = &data[hdr+5];
68527 top = get2byte(pTmp)((pTmp)[0]<<8 | (pTmp)[1]);
68528 assert( top<=(int)pPage->pBt->usableSize )((void) (0)); /* by btreeComputeFreeSpace() */
68529 if( gap>top ){
68530 if( top==0 && pPage->pBt->usableSize==65536 ){
68531 top = 65536;
68532 }else{
68533 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68533);
68534 }
68535 }
68536
68537 /* If there is enough space between gap and top for one more cell pointer,
68538 ** and if the freelist is not empty, then search the
68539 ** freelist looking for a slot big enough to satisfy the request.
68540 */
68541 testcase( gap+2==top );
68542 testcase( gap+1==top );
68543 testcase( gap==top );
68544 if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
68545 u8 *pSpace = pageFindSlot(pPage, nByte, &rc);
68546 if( pSpace ){
68547 int g2;
68548 assert( pSpace+nByte<=data+pPage->pBt->usableSize )((void) (0));
68549 *pIdx = g2 = (int)(pSpace-data);
68550 if( g2<=gap ){
68551 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68551);
68552 }else{
68553 return SQLITE_OK0;
68554 }
68555 }else if( rc ){
68556 return rc;
68557 }
68558 }
68559
68560 /* The request could not be fulfilled using a freelist slot. Check
68561 ** to see if defragmentation is necessary.
68562 */
68563 testcase( gap+2+nByte==top );
68564 if( gap+2+nByte>top ){
68565 assert( pPage->nCell>0 || CORRUPT_DB )((void) (0));
68566 assert( pPage->nFree>=0 )((void) (0));
68567 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte))((4)<(pPage->nFree - (2+nByte))?(4):(pPage->nFree - (
2+nByte)))
);
68568 if( rc ) return rc;
68569 top = get2byteNotZero(&data[hdr+5])(((((int)((&data[hdr+5])[0]<<8 | (&data[hdr+5])
[1]))-1)&0xffff)+1)
;
68570 assert( gap+2+nByte<=top )((void) (0));
68571 }
68572
68573
68574 /* Allocate memory from the gap in between the cell pointer array
68575 ** and the cell content area. The btreeComputeFreeSpace() call has already
68576 ** validated the freelist. Given that the freelist is valid, there
68577 ** is no way that the allocation can extend off the end of the page.
68578 ** The assert() below verifies the previous sentence.
68579 */
68580 top -= nByte;
68581 put2byte(&data[hdr+5], top)((&data[hdr+5])[0] = (u8)((top)>>8), (&data[hdr
+5])[1] = (u8)(top))
;
68582 assert( top+nByte <= (int)pPage->pBt->usableSize )((void) (0));
68583 *pIdx = top;
68584 return SQLITE_OK0;
68585}
68586
68587/*
68588** Return a section of the pPage->aData to the freelist.
68589** The first byte of the new free block is pPage->aData[iStart]
68590** and the size of the block is iSize bytes.
68591**
68592** Adjacent freeblocks are coalesced.
68593**
68594** Even though the freeblock list was checked by btreeComputeFreeSpace(),
68595** that routine will not detect overlap between cells or freeblocks. Nor
68596** does it detect cells or freeblocks that encrouch into the reserved bytes
68597** at the end of the page. So do additional corruption checks inside this
68598** routine and return SQLITE_CORRUPT if any problems are found.
68599*/
68600static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
68601 u16 iPtr; /* Address of ptr to next freeblock */
68602 u16 iFreeBlk; /* Address of the next freeblock */
68603 u8 hdr; /* Page header size. 0 or 100 */
68604 u8 nFrag = 0; /* Reduction in fragmentation */
68605 u16 iOrigSize = iSize; /* Original value of iSize */
68606 u16 x; /* Offset to cell content area */
68607 u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */
68608 unsigned char *data = pPage->aData; /* Page content */
68609 u8 *pTmp; /* Temporary ptr into data[] */
68610
68611 assert( pPage->pBt!=0 )((void) (0));
68612 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
68613 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize )((void) (0));
68614 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize )((void) (0));
68615 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
68616 assert( iSize>=4 )((void) (0)); /* Minimum cell size is 4 */
68617 assert( iStart<=pPage->pBt->usableSize-4 )((void) (0));
68618
68619 /* The list of freeblocks must be in ascending order. Find the
68620 ** spot on the list where iStart should be inserted.
68621 */
68622 hdr = pPage->hdrOffset;
68623 iPtr = hdr + 1;
68624 if( data[iPtr+1]==0 && data[iPtr]==0 ){
68625 iFreeBlk = 0; /* Shortcut for the case when the freelist is empty */
68626 }else{
68627 while( (iFreeBlk = get2byte(&data[iPtr])((&data[iPtr])[0]<<8 | (&data[iPtr])[1]))<iStart ){
68628 if( iFreeBlk<iPtr+4 ){
68629 if( iFreeBlk==0 ) break; /* TH3: corrupt082.100 */
68630 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68630);
68631 }
68632 iPtr = iFreeBlk;
68633 }
68634 if( iFreeBlk>pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
68635 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68635);
68636 }
68637 assert( iFreeBlk>iPtr || iFreeBlk==0 || CORRUPT_DB )((void) (0));
68638
68639 /* At this point:
68640 ** iFreeBlk: First freeblock after iStart, or zero if none
68641 ** iPtr: The address of a pointer to iFreeBlk
68642 **
68643 ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
68644 */
68645 if( iFreeBlk && iEnd+3>=iFreeBlk ){
68646 nFrag = iFreeBlk - iEnd;
68647 if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68647);
68648 iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2])((&data[iFreeBlk+2])[0]<<8 | (&data[iFreeBlk+2]
)[1])
;
68649 if( iEnd > pPage->pBt->usableSize ){
68650 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68650);
68651 }
68652 iSize = iEnd - iStart;
68653 iFreeBlk = get2byte(&data[iFreeBlk])((&data[iFreeBlk])[0]<<8 | (&data[iFreeBlk])[1]
)
;
68654 }
68655
68656 /* If iPtr is another freeblock (that is, if iPtr is not the freelist
68657 ** pointer in the page header) then check to see if iStart should be
68658 ** coalesced onto the end of iPtr.
68659 */
68660 if( iPtr>hdr+1 ){
68661 int iPtrEnd = iPtr + get2byte(&data[iPtr+2])((&data[iPtr+2])[0]<<8 | (&data[iPtr+2])[1]);
68662 if( iPtrEnd+3>=iStart ){
68663 if( iPtrEnd>iStart ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68663);
68664 nFrag += iStart - iPtrEnd;
68665 iSize = iEnd - iPtr;
68666 iStart = iPtr;
68667 }
68668 }
68669 if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68669);
68670 data[hdr+7] -= nFrag;
68671 }
68672 pTmp = &data[hdr+5];
68673 x = get2byte(pTmp)((pTmp)[0]<<8 | (pTmp)[1]);
68674 if( iStart<=x ){
68675 /* The new freeblock is at the beginning of the cell content area,
68676 ** so just extend the cell content area rather than create another
68677 ** freelist entry */
68678 if( iStart<x ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68678);
68679 if( iPtr!=hdr+1 ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68679);
68680 put2byte(&data[hdr+1], iFreeBlk)((&data[hdr+1])[0] = (u8)((iFreeBlk)>>8), (&data
[hdr+1])[1] = (u8)(iFreeBlk))
;
68681 put2byte(&data[hdr+5], iEnd)((&data[hdr+5])[0] = (u8)((iEnd)>>8), (&data[hdr
+5])[1] = (u8)(iEnd))
;
68682 }else{
68683 /* Insert the new freeblock into the freelist */
68684 put2byte(&data[iPtr], iStart)((&data[iPtr])[0] = (u8)((iStart)>>8), (&data[iPtr
])[1] = (u8)(iStart))
;
68685 }
68686 if( pPage->pBt->btsFlags & BTS_FAST_SECURE0x000c ){
68687 /* Overwrite deleted information with zeros when the secure_delete
68688 ** option is enabled */
68689 memset(&data[iStart], 0, iSize);
68690 }
68691 put2byte(&data[iStart], iFreeBlk)((&data[iStart])[0] = (u8)((iFreeBlk)>>8), (&data
[iStart])[1] = (u8)(iFreeBlk))
;
68692 put2byte(&data[iStart+2], iSize)((&data[iStart+2])[0] = (u8)((iSize)>>8), (&data
[iStart+2])[1] = (u8)(iSize))
;
68693 pPage->nFree += iOrigSize;
68694 return SQLITE_OK0;
68695}
68696
68697/*
68698** Decode the flags byte (the first byte of the header) for a page
68699** and initialize fields of the MemPage structure accordingly.
68700**
68701** Only the following combinations are supported. Anything different
68702** indicates a corrupt database files:
68703**
68704** PTF_ZERODATA
68705** PTF_ZERODATA | PTF_LEAF
68706** PTF_LEAFDATA | PTF_INTKEY
68707** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
68708*/
68709static int decodeFlags(MemPage *pPage, int flagByte){
68710 BtShared *pBt; /* A copy of pPage->pBt */
68711
68712 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) )((void) (0));
68713 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
68714 pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 )((void) (0));
68715 flagByte &= ~PTF_LEAF0x08;
68716 pPage->childPtrSize = 4-4*pPage->leaf;
68717 pBt = pPage->pBt;
68718 if( flagByte==(PTF_LEAFDATA0x04 | PTF_INTKEY0x01) ){
68719 /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an
68720 ** interior table b-tree page. */
68721 assert( (PTF_LEAFDATA|PTF_INTKEY)==5 )((void) (0));
68722 /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a
68723 ** leaf table b-tree page. */
68724 assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 )((void) (0));
68725 pPage->intKey = 1;
68726 if( pPage->leaf ){
68727 pPage->intKeyLeaf = 1;
68728 pPage->xCellSize = cellSizePtrTableLeaf;
68729 pPage->xParseCell = btreeParseCellPtr;
68730 }else{
68731 pPage->intKeyLeaf = 0;
68732 pPage->xCellSize = cellSizePtrNoPayload;
68733 pPage->xParseCell = btreeParseCellPtrNoPayload;
68734 }
68735 pPage->maxLocal = pBt->maxLeaf;
68736 pPage->minLocal = pBt->minLeaf;
68737 }else if( flagByte==PTF_ZERODATA0x02 ){
68738 /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an
68739 ** interior index b-tree page. */
68740 assert( (PTF_ZERODATA)==2 )((void) (0));
68741 /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a
68742 ** leaf index b-tree page. */
68743 assert( (PTF_ZERODATA|PTF_LEAF)==10 )((void) (0));
68744 pPage->intKey = 0;
68745 pPage->intKeyLeaf = 0;
68746 pPage->xCellSize = cellSizePtr;
68747 pPage->xParseCell = btreeParseCellPtrIndex;
68748 pPage->maxLocal = pBt->maxLocal;
68749 pPage->minLocal = pBt->minLocal;
68750 }else{
68751 /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
68752 ** an error. */
68753 pPage->intKey = 0;
68754 pPage->intKeyLeaf = 0;
68755 pPage->xCellSize = cellSizePtr;
68756 pPage->xParseCell = btreeParseCellPtrIndex;
68757 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68757);
68758 }
68759 pPage->max1bytePayload = pBt->max1bytePayload;
68760 return SQLITE_OK0;
68761}
68762
68763/*
68764** Compute the amount of freespace on the page. In other words, fill
68765** in the pPage->nFree field.
68766*/
68767static int btreeComputeFreeSpace(MemPage *pPage){
68768 int pc; /* Address of a freeblock within pPage->aData[] */
68769 u8 hdr; /* Offset to beginning of page header */
68770 u8 *data; /* Equal to pPage->aData */
68771 int usableSize; /* Amount of usable space on each page */
68772 int nFree; /* Number of unused bytes on the page */
68773 int top; /* First byte of the cell content area */
68774 int iCellFirst; /* First allowable cell or freeblock offset */
68775 int iCellLast; /* Last possible cell or freeblock offset */
68776
68777 assert( pPage->pBt!=0 )((void) (0));
68778 assert( pPage->pBt->db!=0 )((void) (0));
68779 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
68780 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) )((void) (0));
68781 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) )((void) (0));
68782 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) )((void) (0));
68783 assert( pPage->isInit==1 )((void) (0));
68784 assert( pPage->nFree<0 )((void) (0));
68785
68786 usableSize = pPage->pBt->usableSize;
68787 hdr = pPage->hdrOffset;
68788 data = pPage->aData;
68789 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
68790 ** the start of the cell content area. A zero value for this integer is
68791 ** interpreted as 65536. */
68792 top = get2byteNotZero(&data[hdr+5])(((((int)((&data[hdr+5])[0]<<8 | (&data[hdr+5])
[1]))-1)&0xffff)+1)
;
68793 iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
68794 iCellLast = usableSize - 4;
68795
68796 /* Compute the total free space on the page
68797 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
68798 ** start of the first freeblock on the page, or is zero if there are no
68799 ** freeblocks. */
68800 pc = get2byte(&data[hdr+1])((&data[hdr+1])[0]<<8 | (&data[hdr+1])[1]);
68801 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */
68802 if( pc>0 ){
68803 u32 next, size;
68804 if( pc<top ){
68805 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
68806 ** always be at least one cell before the first freeblock.
68807 */
68808 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68808);
68809 }
68810 while( 1 ){
68811 if( pc>iCellLast ){
68812 /* Freeblock off the end of the page */
68813 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68813);
68814 }
68815 next = get2byte(&data[pc])((&data[pc])[0]<<8 | (&data[pc])[1]);
68816 size = get2byte(&data[pc+2])((&data[pc+2])[0]<<8 | (&data[pc+2])[1]);
68817 nFree = nFree + size;
68818 if( next<=pc+size+3 ) break;
68819 pc = next;
68820 }
68821 if( next>0 ){
68822 /* Freeblock not in ascending order */
68823 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68823);
68824 }
68825 if( pc+size>(unsigned int)usableSize ){
68826 /* Last freeblock extends past page end */
68827 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68827);
68828 }
68829 }
68830
68831 /* At this point, nFree contains the sum of the offset to the start
68832 ** of the cell-content area plus the number of free bytes within
68833 ** the cell-content area. If this is greater than the usable-size
68834 ** of the page, then the page must be corrupted. This check also
68835 ** serves to verify that the offset to the start of the cell-content
68836 ** area, according to the page header, lies within the page.
68837 */
68838 if( nFree>usableSize || nFree<iCellFirst ){
68839 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68839);
68840 }
68841 pPage->nFree = (u16)(nFree - iCellFirst);
68842 return SQLITE_OK0;
68843}
68844
68845/*
68846** Do additional sanity check after btreeInitPage() if
68847** PRAGMA cell_size_check=ON
68848*/
68849static SQLITE_NOINLINE__attribute__((noinline)) int btreeCellSizeCheck(MemPage *pPage){
68850 int iCellFirst; /* First allowable cell or freeblock offset */
68851 int iCellLast; /* Last possible cell or freeblock offset */
68852 int i; /* Index into the cell pointer array */
68853 int sz; /* Size of a cell */
68854 int pc; /* Address of a freeblock within pPage->aData[] */
68855 u8 *data; /* Equal to pPage->aData */
68856 int usableSize; /* Maximum usable space on the page */
68857 int cellOffset; /* Start of cell content area */
68858
68859 iCellFirst = pPage->cellOffset + 2*pPage->nCell;
68860 usableSize = pPage->pBt->usableSize;
68861 iCellLast = usableSize - 4;
68862 data = pPage->aData;
68863 cellOffset = pPage->cellOffset;
68864 if( !pPage->leaf ) iCellLast--;
68865 for(i=0; i<pPage->nCell; i++){
68866 pc = get2byteAligned(&data[cellOffset+i*2])((&data[cellOffset+i*2])[0]<<8 | (&data[cellOffset
+i*2])[1])
;
68867 testcase( pc==iCellFirst );
68868 testcase( pc==iCellLast );
68869 if( pc<iCellFirst || pc>iCellLast ){
68870 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68870);
68871 }
68872 sz = pPage->xCellSize(pPage, &data[pc]);
68873 testcase( pc+sz==usableSize );
68874 if( pc+sz>usableSize ){
68875 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68875);
68876 }
68877 }
68878 return SQLITE_OK0;
68879}
68880
68881/*
68882** Initialize the auxiliary information for a disk block.
68883**
68884** Return SQLITE_OK on success. If we see that the page does
68885** not contain a well-formed database page, then return
68886** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
68887** guarantee that the page is well-formed. It only shows that
68888** we failed to detect any corruption.
68889*/
68890static int btreeInitPage(MemPage *pPage){
68891 u8 *data; /* Equal to pPage->aData */
68892 BtShared *pBt; /* The main btree structure */
68893
68894 assert( pPage->pBt!=0 )((void) (0));
68895 assert( pPage->pBt->db!=0 )((void) (0));
68896 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
68897 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) )((void) (0));
68898 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) )((void) (0));
68899 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) )((void) (0));
68900 assert( pPage->isInit==0 )((void) (0));
68901
68902 pBt = pPage->pBt;
68903 data = pPage->aData + pPage->hdrOffset;
68904 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
68905 ** the b-tree page type. */
68906 if( decodeFlags(pPage, data[0]) ){
68907 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68907);
68908 }
68909 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 )((void) (0));
68910 pPage->maskPage = (u16)(pBt->pageSize - 1);
68911 pPage->nOverflow = 0;
68912 pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
68913 pPage->aCellIdx = data + pPage->childPtrSize + 8;
68914 pPage->aDataEnd = pPage->aData + pBt->pageSize;
68915 pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
68916 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
68917 ** number of cells on the page. */
68918 pPage->nCell = get2byte(&data[3])((&data[3])[0]<<8 | (&data[3])[1]);
68919 if( pPage->nCell>MX_CELL(pBt)((pBt->pageSize-8)/6) ){
68920 /* To many cells for a single page. The page must be corrupt */
68921 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68921);
68922 }
68923 testcase( pPage->nCell==MX_CELL(pBt) );
68924 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
68925 ** possible for a root page of a table that contains no rows) then the
68926 ** offset to the cell content area will equal the page size minus the
68927 ** bytes of reserved space. */
68928 assert( pPage->nCell>0((void) (0))
68929 || get2byteNotZero(&data[5])==(int)pBt->usableSize((void) (0))
68930 || CORRUPT_DB )((void) (0));
68931 pPage->nFree = -1; /* Indicate that this value is yet uncomputed */
68932 pPage->isInit = 1;
68933 if( pBt->db->flags & SQLITE_CellSizeCk0x00200000 ){
68934 return btreeCellSizeCheck(pPage);
68935 }
68936 return SQLITE_OK0;
68937}
68938
68939/*
68940** Set up a raw page so that it looks like a database page holding
68941** no entries.
68942*/
68943static void zeroPage(MemPage *pPage, int flags){
68944 unsigned char *data = pPage->aData;
68945 BtShared *pBt = pPage->pBt;
68946 u8 hdr = pPage->hdrOffset;
68947 u16 first;
68948
68949 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno || CORRUPT_DB )((void) (0));
68950 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage )((void) (0));
68951 assert( sqlite3PagerGetData(pPage->pDbPage) == data )((void) (0));
68952 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
68953 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
68954 if( pBt->btsFlags & BTS_FAST_SECURE0x000c ){
68955 memset(&data[hdr], 0, pBt->usableSize - hdr);
68956 }
68957 data[hdr] = (char)flags;
68958 first = hdr + ((flags&PTF_LEAF0x08)==0 ? 12 : 8);
68959 memset(&data[hdr+1], 0, 4);
68960 data[hdr+7] = 0;
68961 put2byte(&data[hdr+5], pBt->usableSize)((&data[hdr+5])[0] = (u8)((pBt->usableSize)>>8),
(&data[hdr+5])[1] = (u8)(pBt->usableSize))
;
68962 pPage->nFree = (u16)(pBt->usableSize - first);
68963 decodeFlags(pPage, flags);
68964 pPage->cellOffset = first;
68965 pPage->aDataEnd = &data[pBt->pageSize];
68966 pPage->aCellIdx = &data[first];
68967 pPage->aDataOfst = &data[pPage->childPtrSize];
68968 pPage->nOverflow = 0;
68969 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 )((void) (0));
68970 pPage->maskPage = (u16)(pBt->pageSize - 1);
68971 pPage->nCell = 0;
68972 pPage->isInit = 1;
68973}
68974
68975
68976/*
68977** Convert a DbPage obtained from the pager into a MemPage used by
68978** the btree layer.
68979*/
68980static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
68981 MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
68982 if( pgno!=pPage->pgno ){
68983 pPage->aData = sqlite3PagerGetData(pDbPage);
68984 pPage->pDbPage = pDbPage;
68985 pPage->pBt = pBt;
68986 pPage->pgno = pgno;
68987 pPage->hdrOffset = pgno==1 ? 100 : 0;
68988 }
68989 assert( pPage->aData==sqlite3PagerGetData(pDbPage) )((void) (0));
68990 return pPage;
68991}
68992
68993/*
68994** Get a page from the pager. Initialize the MemPage.pBt and
68995** MemPage.aData elements if needed. See also: btreeGetUnusedPage().
68996**
68997** If the PAGER_GET_NOCONTENT flag is set, it means that we do not care
68998** about the content of the page at this time. So do not go to the disk
68999** to fetch the content. Just fill in the content with zeros for now.
69000** If in the future we call sqlite3PagerWrite() on this page, that
69001** means we have started to be concerned about content and the disk
69002** read should occur at that point.
69003*/
69004static int btreeGetPage(
69005 BtShared *pBt, /* The btree */
69006 Pgno pgno, /* Number of the page to fetch */
69007 MemPage **ppPage, /* Return the page in this parameter */
69008 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
69009){
69010 int rc;
69011 DbPage *pDbPage;
69012
69013 assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY )((void) (0));
69014 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
69015 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
69016 if( rc ) return rc;
69017 *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
69018 return SQLITE_OK0;
69019}
69020
69021/*
69022** Retrieve a page from the pager cache. If the requested page is not
69023** already in the pager cache return NULL. Initialize the MemPage.pBt and
69024** MemPage.aData elements if needed.
69025*/
69026static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
69027 DbPage *pDbPage;
69028 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
69029 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
69030 if( pDbPage ){
69031 return btreePageFromDbPage(pDbPage, pgno, pBt);
69032 }
69033 return 0;
69034}
69035
69036/*
69037** Return the size of the database file in pages. If there is any kind of
69038** error, return ((unsigned int)-1).
69039*/
69040static Pgno btreePagecount(BtShared *pBt){
69041 return pBt->nPage;
69042}
69043SQLITE_PRIVATEstatic Pgno sqlite3BtreeLastPage(Btree *p){
69044 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
69045 return btreePagecount(p->pBt);
69046}
69047
69048/*
69049** Get a page from the pager and initialize it.
69050**
69051** If pCur!=0 then the page is being fetched as part of a moveToChild()
69052** call. Do additional sanity checking on the page in this case.
69053** And if the fetch fails, this routine must decrement pCur->iPage.
69054**
69055** The page is fetched as read-write unless pCur is not NULL and is
69056** a read-only cursor.
69057**
69058** If an error occurs, then *ppPage is undefined. It
69059** may remain unchanged, or it may be set to an invalid value.
69060*/
69061static int getAndInitPage(
69062 BtShared *pBt, /* The database file */
69063 Pgno pgno, /* Number of the page to get */
69064 MemPage **ppPage, /* Write the page pointer here */
69065 BtCursor *pCur, /* Cursor to receive the page, or NULL */
69066 int bReadOnly /* True for a read-only page */
69067){
69068 int rc;
69069 DbPage *pDbPage;
69070 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
69071 assert( pCur==0 || ppPage==&pCur->pPage )((void) (0));
69072 assert( pCur==0 || bReadOnly==pCur->curPagerFlags )((void) (0));
69073 assert( pCur==0 || pCur->iPage>0 )((void) (0));
69074
69075 if( pgno>btreePagecount(pBt) ){
69076 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(69076);
69077 goto getAndInitPage_error1;
69078 }
69079 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
69080 if( rc ){
69081 goto getAndInitPage_error1;
69082 }
69083 *ppPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
69084 if( (*ppPage)->isInit==0 ){
69085 btreePageFromDbPage(pDbPage, pgno, pBt);
69086 rc = btreeInitPage(*ppPage);
69087 if( rc!=SQLITE_OK0 ){
69088 goto getAndInitPage_error2;
69089 }
69090 }
69091 assert( (*ppPage)->pgno==pgno || CORRUPT_DB )((void) (0));
69092 assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) )((void) (0));
69093
69094 /* If obtaining a child page for a cursor, we must verify that the page is
69095 ** compatible with the root page. */
69096 if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){
69097 rc = SQLITE_CORRUPT_PGNO(pgno)sqlite3CorruptError(69097);
69098 goto getAndInitPage_error2;
69099 }
69100 return SQLITE_OK0;
69101
69102getAndInitPage_error2:
69103 releasePage(*ppPage);
69104getAndInitPage_error1:
69105 if( pCur ){
69106 pCur->iPage--;
69107 pCur->pPage = pCur->apPage[pCur->iPage];
69108 }
69109 testcase( pgno==0 );
69110 assert( pgno!=0 || rc==SQLITE_CORRUPT((void) (0))
69111 || rc==SQLITE_IOERR_NOMEM((void) (0))
69112 || rc==SQLITE_NOMEM )((void) (0));
69113 return rc;
69114}
69115
69116/*
69117** Release a MemPage. This should be called once for each prior
69118** call to btreeGetPage.
69119**
69120** Page1 is a special case and must be released using releasePageOne().
69121*/
69122static void releasePageNotNull(MemPage *pPage){
69123 assert( pPage->aData )((void) (0));
69124 assert( pPage->pBt )((void) (0));
69125 assert( pPage->pDbPage!=0 )((void) (0));
69126 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage )((void) (0));
69127 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData )((void) (0));
69128 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
69129 sqlite3PagerUnrefNotNull(pPage->pDbPage);
69130}
69131static void releasePage(MemPage *pPage){
69132 if( pPage ) releasePageNotNull(pPage);
69133}
69134static void releasePageOne(MemPage *pPage){
69135 assert( pPage!=0 )((void) (0));
69136 assert( pPage->aData )((void) (0));
69137 assert( pPage->pBt )((void) (0));
69138 assert( pPage->pDbPage!=0 )((void) (0));
69139 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage )((void) (0));
69140 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData )((void) (0));
69141 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
69142 sqlite3PagerUnrefPageOne(pPage->pDbPage);
69143}
69144
69145/*
69146** Get an unused page.
69147**
69148** This works just like btreeGetPage() with the addition:
69149**
69150** * If the page is already in use for some other purpose, immediately
69151** release it and return an SQLITE_CURRUPT error.
69152** * Make sure the isInit flag is clear
69153*/
69154static int btreeGetUnusedPage(
69155 BtShared *pBt, /* The btree */
69156 Pgno pgno, /* Number of the page to fetch */
69157 MemPage **ppPage, /* Return the page in this parameter */
69158 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
69159){
69160 int rc = btreeGetPage(pBt, pgno, ppPage, flags);
69161 if( rc==SQLITE_OK0 ){
69162 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
69163 releasePage(*ppPage);
69164 *ppPage = 0;
69165 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(69165);
69166 }
69167 (*ppPage)->isInit = 0;
69168 }else{
69169 *ppPage = 0;
69170 }
69171 return rc;
69172}
69173
69174
69175/*
69176** During a rollback, when the pager reloads information into the cache
69177** so that the cache is restored to its original state at the start of
69178** the transaction, for each page restored this routine is called.
69179**
69180** This routine needs to reset the extra data section at the end of the
69181** page to agree with the restored data.
69182*/
69183static void pageReinit(DbPage *pData){
69184 MemPage *pPage;
69185 pPage = (MemPage *)sqlite3PagerGetExtra(pData);
69186 assert( sqlite3PagerPageRefcount(pData)>0 )((void) (0));
69187 if( pPage->isInit ){
69188 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
69189 pPage->isInit = 0;
69190 if( sqlite3PagerPageRefcount(pData)>1 ){
69191 /* pPage might not be a btree page; it might be an overflow page
69192 ** or ptrmap page or a free page. In those cases, the following
69193 ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
69194 ** But no harm is done by this. And it is very important that
69195 ** btreeInitPage() be called on every btree page so we make
69196 ** the call for every page that comes in for re-initing. */
69197 btreeInitPage(pPage);
69198 }
69199 }
69200}
69201
69202/*
69203** Invoke the busy handler for a btree.
69204*/
69205static int btreeInvokeBusyHandler(void *pArg){
69206 BtShared *pBt = (BtShared*)pArg;
69207 assert( pBt->db )((void) (0));
69208 assert( sqlite3_mutex_held(pBt->db->mutex) )((void) (0));
69209 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
69210}
69211
69212/*
69213** Open a database file.
69214**
69215** zFilename is the name of the database file. If zFilename is NULL
69216** then an ephemeral database is created. The ephemeral database might
69217** be exclusively in memory, or it might use a disk-based memory cache.
69218** Either way, the ephemeral database will be automatically deleted
69219** when sqlite3BtreeClose() is called.
69220**
69221** If zFilename is ":memory:" then an in-memory database is created
69222** that is automatically destroyed when it is closed.
69223**
69224** The "flags" parameter is a bitmask that might contain bits like
69225** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY.
69226**
69227** If the database is already opened in the same database connection
69228** and we are in shared cache mode, then the open will fail with an
69229** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared
69230** objects in the same database connection since doing so will lead
69231** to problems with locking.
69232*/
69233SQLITE_PRIVATEstatic int sqlite3BtreeOpen(
69234 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
69235 const char *zFilename, /* Name of the file containing the BTree database */
69236 sqlite3 *db, /* Associated database handle */
69237 Btree **ppBtree, /* Pointer to new Btree object written here */
69238 int flags, /* Options */
69239 int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */
69240){
69241 BtShared *pBt = 0; /* Shared part of btree structure */
69242 Btree *p; /* Handle to return */
69243 sqlite3_mutex *mutexOpen = 0; /* Prevents a race condition. Ticket #3537 */
69244 int rc = SQLITE_OK0; /* Result code from this function */
69245 u8 nReserve; /* Byte of unused space on each page */
69246 unsigned char zDbHeader[100]; /* Database header content */
69247
69248 /* True if opening an ephemeral, temporary database */
69249 const int isTempDb = zFilename==0 || zFilename[0]==0;
69250
69251 /* Set the variable isMemdb to true for an in-memory database, or
69252 ** false for a file-based database.
69253 */
69254#ifdef SQLITE_OMIT_MEMORYDB
69255 const int isMemdb = 0;
69256#else
69257 const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
69258 || (isTempDb && sqlite3TempInMemory(db))
69259 || (vfsFlags & SQLITE_OPEN_MEMORY0x00000080)!=0;
69260#endif
69261
69262 assert( db!=0 )((void) (0));
69263 assert( pVfs!=0 )((void) (0));
69264 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
69265 assert( (flags&0xff)==flags )((void) (0)); /* flags fit in 8 bits */
69266
69267 /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
69268 assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 )((void) (0));
69269
69270 /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
69271 assert( (flags & BTREE_SINGLE)==0 || isTempDb )((void) (0));
69272
69273 if( isMemdb ){
69274 flags |= BTREE_MEMORY2;
69275 }
69276 if( (vfsFlags & SQLITE_OPEN_MAIN_DB0x00000100)!=0 && (isMemdb || isTempDb) ){
69277 vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB0x00000100) | SQLITE_OPEN_TEMP_DB0x00000200;
69278 }
69279 p = sqlite3MallocZero(sizeof(Btree));
69280 if( !p ){
69281 return SQLITE_NOMEM_BKPT7;
69282 }
69283 p->inTrans = TRANS_NONE0;
69284 p->db = db;
69285#ifndef SQLITE_OMIT_SHARED_CACHE
69286 p->lock.pBtree = p;
69287 p->lock.iTable = 1;
69288#endif
69289
69290#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
69291 /*
69292 ** If this Btree is a candidate for shared cache, try to find an
69293 ** existing BtShared object that we can share with
69294 */
69295 if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI0x00000040)!=0) ){
69296 if( vfsFlags & SQLITE_OPEN_SHAREDCACHE0x00020000 ){
69297 int nFilename = sqlite3Strlen30(zFilename)+1;
69298 int nFullPathname = pVfs->mxPathname+1;
69299 char *zFullPathname = sqlite3Malloc(MAX(nFullPathname,nFilename)((nFullPathname)>(nFilename)?(nFullPathname):(nFilename)));
69300 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )sqlite3_mutex *mutexShared;
69301
69302 p->sharable = 1;
69303 if( !zFullPathname ){
69304 sqlite3_free(p);
69305 return SQLITE_NOMEM_BKPT7;
69306 }
69307 if( isMemdb ){
69308 memcpy(zFullPathname, zFilename, nFilename);
69309 }else{
69310 rc = sqlite3OsFullPathname(pVfs, zFilename,
69311 nFullPathname, zFullPathname);
69312 if( rc ){
69313 if( rc==SQLITE_OK_SYMLINK(0 | (2<<8)) ){
69314 rc = SQLITE_OK0;
69315 }else{
69316 sqlite3_free(zFullPathname);
69317 sqlite3_free(p);
69318 return rc;
69319 }
69320 }
69321 }
69322#if SQLITE_THREADSAFE2
69323 mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN4);
69324 sqlite3_mutex_enter(mutexOpen);
69325 mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2);
69326 sqlite3_mutex_enter(mutexShared);
69327#endif
69328 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList)sqlite3SharedCacheList; pBt; pBt=pBt->pNext){
69329 assert( pBt->nRef>0 )((void) (0));
69330 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
69331 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
69332 int iDb;
69333 for(iDb=db->nDb-1; iDb>=0; iDb--){
69334 Btree *pExisting = db->aDb[iDb].pBt;
69335 if( pExisting && pExisting->pBt==pBt ){
69336 sqlite3_mutex_leave(mutexShared);
69337 sqlite3_mutex_leave(mutexOpen);
69338 sqlite3_free(zFullPathname);
69339 sqlite3_free(p);
69340 return SQLITE_CONSTRAINT19;
69341 }
69342 }
69343 p->pBt = pBt;
69344 pBt->nRef++;
69345 break;
69346 }
69347 }
69348 sqlite3_mutex_leave(mutexShared);
69349 sqlite3_free(zFullPathname);
69350 }
69351#ifdef SQLITE_DEBUG
69352 else{
69353 /* In debug mode, we mark all persistent databases as sharable
69354 ** even when they are not. This exercises the locking code and
69355 ** gives more opportunity for asserts(sqlite3_mutex_held())
69356 ** statements to find locking problems.
69357 */
69358 p->sharable = 1;
69359 }
69360#endif
69361 }
69362#endif
69363 if( pBt==0 ){
69364 /*
69365 ** The following asserts make sure that structures used by the btree are
69366 ** the right size. This is to guard against size changes that result
69367 ** when compiling on a different architecture.
69368 */
69369 assert( sizeof(i64)==8 )((void) (0));
69370 assert( sizeof(u64)==8 )((void) (0));
69371 assert( sizeof(u32)==4 )((void) (0));
69372 assert( sizeof(u16)==2 )((void) (0));
69373 assert( sizeof(Pgno)==4 )((void) (0));
69374
69375 pBt = sqlite3MallocZero( sizeof(*pBt) );
69376 if( pBt==0 ){
69377 rc = SQLITE_NOMEM_BKPT7;
69378 goto btree_open_out;
69379 }
69380 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
69381 sizeof(MemPage), flags, vfsFlags, pageReinit);
69382 if( rc==SQLITE_OK0 ){
69383 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
69384 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
69385 }
69386 if( rc!=SQLITE_OK0 ){
69387 goto btree_open_out;
69388 }
69389 pBt->openFlags = (u8)flags;
69390 pBt->db = db;
69391 sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
69392 p->pBt = pBt;
69393
69394 pBt->pCursor = 0;
69395 pBt->pPage1 = 0;
69396 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY0x0001;
69397#if defined(SQLITE_SECURE_DELETE)
69398 pBt->btsFlags |= BTS_SECURE_DELETE0x0004;
69399#elif defined(SQLITE_FAST_SECURE_DELETE)
69400 pBt->btsFlags |= BTS_OVERWRITE0x0008;
69401#endif
69402 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
69403 ** determined by the 2-byte integer located at an offset of 16 bytes from
69404 ** the beginning of the database file. */
69405 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
69406 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE65536
69407 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
69408 pBt->pageSize = 0;
69409#ifndef SQLITE_OMIT_AUTOVACUUM
69410 /* If the magic name ":memory:" will create an in-memory database, then
69411 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
69412 ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
69413 ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
69414 ** regular file-name. In this case the auto-vacuum applies as per normal.
69415 */
69416 if( zFilename && !isMemdb ){
69417 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM0 ? 1 : 0);
69418 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM0==2 ? 1 : 0);
69419 }
69420#endif
69421 nReserve = 0;
69422 }else{
69423 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
69424 ** determined by the one-byte unsigned integer found at an offset of 20
69425 ** into the database file header. */
69426 nReserve = zDbHeader[20];
69427 pBt->btsFlags |= BTS_PAGESIZE_FIXED0x0002;
69428#ifndef SQLITE_OMIT_AUTOVACUUM
69429 pBt->autoVacuum = (get4bytesqlite3Get4byte(&zDbHeader[36 + 4*4])?1:0);
69430 pBt->incrVacuum = (get4bytesqlite3Get4byte(&zDbHeader[36 + 7*4])?1:0);
69431#endif
69432 }
69433 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
69434 if( rc ) goto btree_open_out;
69435 pBt->usableSize = pBt->pageSize - nReserve;
69436 assert( (pBt->pageSize & 7)==0 )((void) (0)); /* 8-byte alignment of pageSize */
69437
69438#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
69439 /* Add the new BtShared object to the linked list sharable BtShareds.
69440 */
69441 pBt->nRef = 1;
69442 if( p->sharable ){
69443 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )sqlite3_mutex *mutexShared;
69444 MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);)mutexShared = sqlite3MutexAlloc(2);
69445 if( SQLITE_THREADSAFE2 && sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
69446 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST0);
69447 if( pBt->mutex==0 ){
69448 rc = SQLITE_NOMEM_BKPT7;
69449 goto btree_open_out;
69450 }
69451 }
69452 sqlite3_mutex_enter(mutexShared);
69453 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList)sqlite3SharedCacheList;
69454 GLOBAL(BtShared*,sqlite3SharedCacheList)sqlite3SharedCacheList = pBt;
69455 sqlite3_mutex_leave(mutexShared);
69456 }
69457#endif
69458 }
69459
69460#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
69461 /* If the new Btree uses a sharable pBtShared, then link the new
69462 ** Btree into the list of all sharable Btrees for the same connection.
69463 ** The list is kept in ascending order by pBt address.
69464 */
69465 if( p->sharable ){
69466 int i;
69467 Btree *pSib;
69468 for(i=0; i<db->nDb; i++){
69469 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
69470 while( pSib->pPrev ){ pSib = pSib->pPrev; }
69471 if( (uptr)p->pBt<(uptr)pSib->pBt ){
69472 p->pNext = pSib;
69473 p->pPrev = 0;
69474 pSib->pPrev = p;
69475 }else{
69476 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
69477 pSib = pSib->pNext;
69478 }
69479 p->pNext = pSib->pNext;
69480 p->pPrev = pSib;
69481 if( p->pNext ){
69482 p->pNext->pPrev = p;
69483 }
69484 pSib->pNext = p;
69485 }
69486 break;
69487 }
69488 }
69489 }
69490#endif
69491 *ppBtree = p;
69492
69493btree_open_out:
69494 if( rc!=SQLITE_OK0 ){
69495 if( pBt && pBt->pPager ){
69496 sqlite3PagerClose(pBt->pPager, 0);
69497 }
69498 sqlite3_free(pBt);
69499 sqlite3_free(p);
69500 *ppBtree = 0;
69501 }else{
69502 sqlite3_file *pFile;
69503
69504 /* If the B-Tree was successfully opened, set the pager-cache size to the
69505 ** default value. Except, when opening on an existing shared pager-cache,
69506 ** do not change the pager-cache size.
69507 */
69508 if( sqlite3BtreeSchema(p, 0, 0)==0 ){
69509 sqlite3BtreeSetCacheSize(p, SQLITE_DEFAULT_CACHE_SIZE128);
69510 }
69511
69512 pFile = sqlite3PagerFile(pBt->pPager);
69513 if( pFile->pMethods ){
69514 sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB30, (void*)&pBt->db);
69515 }
69516 }
69517 if( mutexOpen ){
69518 assert( sqlite3_mutex_held(mutexOpen) )((void) (0));
69519 sqlite3_mutex_leave(mutexOpen);
69520 }
69521 assert( rc!=SQLITE_OK || sqlite3BtreeConnectionCount(*ppBtree)>0 )((void) (0));
69522 return rc;
69523}
69524
69525/*
69526** Decrement the BtShared.nRef counter. When it reaches zero,
69527** remove the BtShared structure from the sharing list. Return
69528** true if the BtShared.nRef counter reaches zero and return
69529** false if it is still positive.
69530*/
69531static int removeFromSharingList(BtShared *pBt){
69532#ifndef SQLITE_OMIT_SHARED_CACHE
69533 MUTEX_LOGIC( sqlite3_mutex *pMainMtx; )sqlite3_mutex *pMainMtx;
69534 BtShared *pList;
69535 int removed = 0;
69536
69537 assert( sqlite3_mutex_notheld(pBt->mutex) )((void) (0));
69538 MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )pMainMtx = sqlite3MutexAlloc(2);
69539 sqlite3_mutex_enter(pMainMtx);
69540 pBt->nRef--;
69541 if( pBt->nRef<=0 ){
69542 if( GLOBAL(BtShared*,sqlite3SharedCacheList)sqlite3SharedCacheList==pBt ){
69543 GLOBAL(BtShared*,sqlite3SharedCacheList)sqlite3SharedCacheList = pBt->pNext;
69544 }else{
69545 pList = GLOBAL(BtShared*,sqlite3SharedCacheList)sqlite3SharedCacheList;
69546 while( ALWAYS(pList)(pList) && pList->pNext!=pBt ){
69547 pList=pList->pNext;
69548 }
69549 if( ALWAYS(pList)(pList) ){
69550 pList->pNext = pBt->pNext;
69551 }
69552 }
69553 if( SQLITE_THREADSAFE2 ){
69554 sqlite3_mutex_free(pBt->mutex);
69555 }
69556 removed = 1;
69557 }
69558 sqlite3_mutex_leave(pMainMtx);
69559 return removed;
69560#else
69561 return 1;
69562#endif
69563}
69564
69565/*
69566** Make sure pBt->pTmpSpace points to an allocation of
69567** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
69568** pointer.
69569*/
69570static SQLITE_NOINLINE__attribute__((noinline)) int allocateTempSpace(BtShared *pBt){
69571 assert( pBt!=0 )((void) (0));
69572 assert( pBt->pTmpSpace==0 )((void) (0));
69573 /* This routine is called only by btreeCursor() when allocating the
69574 ** first write cursor for the BtShared object */
69575 assert( pBt->pCursor!=0 && (pBt->pCursor->curFlags & BTCF_WriteFlag)!=0 )((void) (0));
69576 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
69577 if( pBt->pTmpSpace==0 ){
69578 BtCursor *pCur = pBt->pCursor;
69579 pBt->pCursor = pCur->pNext; /* Unlink the cursor */
69580 memset(pCur, 0, sizeof(*pCur));
69581 return SQLITE_NOMEM_BKPT7;
69582 }
69583
69584 /* One of the uses of pBt->pTmpSpace is to format cells before
69585 ** inserting them into a leaf page (function fillInCell()). If
69586 ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes
69587 ** by the various routines that manipulate binary cells. Which
69588 ** can mean that fillInCell() only initializes the first 2 or 3
69589 ** bytes of pTmpSpace, but that the first 4 bytes are copied from
69590 ** it into a database page. This is not actually a problem, but it
69591 ** does cause a valgrind error when the 1 or 2 bytes of unitialized
69592 ** data is passed to system call write(). So to avoid this error,
69593 ** zero the first 4 bytes of temp space here.
69594 **
69595 ** Also: Provide four bytes of initialized space before the
69596 ** beginning of pTmpSpace as an area available to prepend the
69597 ** left-child pointer to the beginning of a cell.
69598 */
69599 memset(pBt->pTmpSpace, 0, 8);
69600 pBt->pTmpSpace += 4;
69601 return SQLITE_OK0;
69602}
69603
69604/*
69605** Free the pBt->pTmpSpace allocation
69606*/
69607static void freeTempSpace(BtShared *pBt){
69608 if( pBt->pTmpSpace ){
69609 pBt->pTmpSpace -= 4;
69610 sqlite3PageFree(pBt->pTmpSpace);
69611 pBt->pTmpSpace = 0;
69612 }
69613}
69614
69615/*
69616** Close an open database and invalidate all cursors.
69617*/
69618SQLITE_PRIVATEstatic int sqlite3BtreeClose(Btree *p){
69619 BtShared *pBt = p->pBt;
69620
69621 /* Close all cursors opened via this handle. */
69622 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
69623 sqlite3BtreeEnter(p);
69624
69625 /* Verify that no other cursors have this Btree open */
69626#ifdef SQLITE_DEBUG
69627 {
69628 BtCursor *pCur = pBt->pCursor;
69629 while( pCur ){
69630 BtCursor *pTmp = pCur;
69631 pCur = pCur->pNext;
69632 assert( pTmp->pBtree!=p )((void) (0));
69633
69634 }
69635 }
69636#endif
69637
69638 /* Rollback any active transaction and free the handle structure.
69639 ** The call to sqlite3BtreeRollback() drops any table-locks held by
69640 ** this handle.
69641 */
69642 sqlite3BtreeRollback(p, SQLITE_OK0, 0);
69643 sqlite3BtreeLeave(p);
69644
69645 /* If there are still other outstanding references to the shared-btree
69646 ** structure, return now. The remainder of this procedure cleans
69647 ** up the shared-btree.
69648 */
69649 assert( p->wantToLock==0 && p->locked==0 )((void) (0));
69650 if( !p->sharable || removeFromSharingList(pBt) ){
69651 /* The pBt is no longer on the sharing list, so we can access
69652 ** it without having to hold the mutex.
69653 **
69654 ** Clean out and delete the BtShared object.
69655 */
69656 assert( !pBt->pCursor )((void) (0));
69657 sqlite3PagerClose(pBt->pPager, p->db);
69658 if( pBt->xFreeSchema && pBt->pSchema ){
69659 pBt->xFreeSchema(pBt->pSchema);
69660 }
69661 sqlite3DbFree(0, pBt->pSchema);
69662 freeTempSpace(pBt);
69663 sqlite3_free(pBt);
69664 }
69665
69666#ifndef SQLITE_OMIT_SHARED_CACHE
69667 assert( p->wantToLock==0 )((void) (0));
69668 assert( p->locked==0 )((void) (0));
69669 if( p->pPrev ) p->pPrev->pNext = p->pNext;
69670 if( p->pNext ) p->pNext->pPrev = p->pPrev;
69671#endif
69672
69673 sqlite3_free(p);
69674 return SQLITE_OK0;
69675}
69676
69677/*
69678** Change the "soft" limit on the number of pages in the cache.
69679** Unused and unmodified pages will be recycled when the number of
69680** pages in the cache exceeds this soft limit. But the size of the
69681** cache is allowed to grow larger than this limit if it contains
69682** dirty pages or pages still in active use.
69683*/
69684SQLITE_PRIVATEstatic int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
69685 BtShared *pBt = p->pBt;
69686 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
69687 sqlite3BtreeEnter(p);
69688 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
69689 sqlite3BtreeLeave(p);
69690 return SQLITE_OK0;
69691}
69692
69693/*
69694** Change the "spill" limit on the number of pages in the cache.
69695** If the number of pages exceeds this limit during a write transaction,
69696** the pager might attempt to "spill" pages to the journal early in
69697** order to free up memory.
69698**
69699** The value returned is the current spill size. If zero is passed
69700** as an argument, no changes are made to the spill size setting, so
69701** using mxPage of 0 is a way to query the current spill size.
69702*/
69703SQLITE_PRIVATEstatic int sqlite3BtreeSetSpillSize(Btree *p, int mxPage){
69704 BtShared *pBt = p->pBt;
69705 int res;
69706 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
69707 sqlite3BtreeEnter(p);
69708 res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
69709 sqlite3BtreeLeave(p);
69710 return res;
69711}
69712
69713#if SQLITE_MAX_MMAP_SIZE20971520>0
69714/*
69715** Change the limit on the amount of the database file that may be
69716** memory mapped.
69717*/
69718SQLITE_PRIVATEstatic int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){
69719 BtShared *pBt = p->pBt;
69720 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
69721 sqlite3BtreeEnter(p);
69722 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
69723 sqlite3BtreeLeave(p);
69724 return SQLITE_OK0;
69725}
69726#endif /* SQLITE_MAX_MMAP_SIZE>0 */
69727
69728/*
69729** Change the way data is synced to disk in order to increase or decrease
69730** how well the database resists damage due to OS crashes and power
69731** failures. Level 1 is the same as asynchronous (no syncs() occur and
69732** there is a high probability of damage) Level 2 is the default. There
69733** is a very low but non-zero probability of damage. Level 3 reduces the
69734** probability of damage to near zero but with a write performance reduction.
69735*/
69736#ifndef SQLITE_OMIT_PAGER_PRAGMAS
69737SQLITE_PRIVATEstatic int sqlite3BtreeSetPagerFlags(
69738 Btree *p, /* The btree to set the safety level on */
69739 unsigned pgFlags /* Various PAGER_* flags */
69740){
69741 BtShared *pBt = p->pBt;
69742 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
69743 sqlite3BtreeEnter(p);
69744 sqlite3PagerSetFlags(pBt->pPager, pgFlags);
69745 sqlite3BtreeLeave(p);
69746 return SQLITE_OK0;
69747}
69748#endif
69749
69750/*
69751** Change the default pages size and the number of reserved bytes per page.
69752** Or, if the page size has already been fixed, return SQLITE_READONLY
69753** without changing anything.
69754**
69755** The page size must be a power of 2 between 512 and 65536. If the page
69756** size supplied does not meet this constraint then the page size is not
69757** changed.
69758**
69759** Page sizes are constrained to be a power of two so that the region
69760** of the database file used for locking (beginning at PENDING_BYTE,
69761** the first byte past the 1GB boundary, 0x40000000) needs to occur
69762** at the beginning of a page.
69763**
69764** If parameter nReserve is less than zero, then the number of reserved
69765** bytes per page is left unchanged.
69766**
69767** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
69768** and autovacuum mode can no longer be changed.
69769*/
69770SQLITE_PRIVATEstatic int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
69771 int rc = SQLITE_OK0;
69772 int x;
69773 BtShared *pBt = p->pBt;
69774 assert( nReserve>=0 && nReserve<=255 )((void) (0));
69775 sqlite3BtreeEnter(p);
69776 pBt->nReserveWanted = nReserve;
69777 x = pBt->pageSize - pBt->usableSize;
69778 if( nReserve<x ) nReserve = x;
69779 if( pBt->btsFlags & BTS_PAGESIZE_FIXED0x0002 ){
69780 sqlite3BtreeLeave(p);
69781 return SQLITE_READONLY8;
69782 }
69783 assert( nReserve>=0 && nReserve<=255 )((void) (0));
69784 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE65536 &&
69785 ((pageSize-1)&pageSize)==0 ){
69786 assert( (pageSize & 7)==0 )((void) (0));
69787 assert( !pBt->pCursor )((void) (0));
69788 if( nReserve>32 && pageSize==512 ) pageSize = 1024;
69789 pBt->pageSize = (u32)pageSize;
69790 freeTempSpace(pBt);
69791 }
69792 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
69793 pBt->usableSize = pBt->pageSize - (u16)nReserve;
69794 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED0x0002;
69795 sqlite3BtreeLeave(p);
69796 return rc;
69797}
69798
69799/*
69800** Return the currently defined page size
69801*/
69802SQLITE_PRIVATEstatic int sqlite3BtreeGetPageSize(Btree *p){
69803 return p->pBt->pageSize;
69804}
69805
69806/*
69807** This function is similar to sqlite3BtreeGetReserve(), except that it
69808** may only be called if it is guaranteed that the b-tree mutex is already
69809** held.
69810**
69811** This is useful in one special case in the backup API code where it is
69812** known that the shared b-tree mutex is held, but the mutex on the
69813** database handle that owns *p is not. In this case if sqlite3BtreeEnter()
69814** were to be called, it might collide with some other operation on the
69815** database handle that owns *p, causing undefined behavior.
69816*/
69817SQLITE_PRIVATEstatic int sqlite3BtreeGetReserveNoMutex(Btree *p){
69818 int n;
69819 assert( sqlite3_mutex_held(p->pBt->mutex) )((void) (0));
69820 n = p->pBt->pageSize - p->pBt->usableSize;
69821 return n;
69822}
69823
69824/*
69825** Return the number of bytes of space at the end of every page that
69826** are intentually left unused. This is the "reserved" space that is
69827** sometimes used by extensions.
69828**
69829** The value returned is the larger of the current reserve size and
69830** the latest reserve size requested by SQLITE_FILECTRL_RESERVE_BYTES.
69831** The amount of reserve can only grow - never shrink.
69832*/
69833SQLITE_PRIVATEstatic int sqlite3BtreeGetRequestedReserve(Btree *p){
69834 int n1, n2;
69835 sqlite3BtreeEnter(p);
69836 n1 = (int)p->pBt->nReserveWanted;
69837 n2 = sqlite3BtreeGetReserveNoMutex(p);
69838 sqlite3BtreeLeave(p);
69839 return n1>n2 ? n1 : n2;
69840}
69841
69842
69843/*
69844** Set the maximum page count for a database if mxPage is positive.
69845** No changes are made if mxPage is 0 or negative.
69846** Regardless of the value of mxPage, return the maximum page count.
69847*/
69848SQLITE_PRIVATEstatic Pgno sqlite3BtreeMaxPageCount(Btree *p, Pgno mxPage){
69849 Pgno n;
69850 sqlite3BtreeEnter(p);
69851 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
69852 sqlite3BtreeLeave(p);
69853 return n;
69854}
69855
69856/*
69857** Change the values for the BTS_SECURE_DELETE and BTS_OVERWRITE flags:
69858**
69859** newFlag==0 Both BTS_SECURE_DELETE and BTS_OVERWRITE are cleared
69860** newFlag==1 BTS_SECURE_DELETE set and BTS_OVERWRITE is cleared
69861** newFlag==2 BTS_SECURE_DELETE cleared and BTS_OVERWRITE is set
69862** newFlag==(-1) No changes
69863**
69864** This routine acts as a query if newFlag is less than zero
69865**
69866** With BTS_OVERWRITE set, deleted content is overwritten by zeros, but
69867** freelist leaf pages are not written back to the database. Thus in-page
69868** deleted content is cleared, but freelist deleted content is not.
69869**
69870** With BTS_SECURE_DELETE, operation is like BTS_OVERWRITE with the addition
69871** that freelist leaf pages are written back into the database, increasing
69872** the amount of disk I/O.
69873*/
69874SQLITE_PRIVATEstatic int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
69875 int b;
69876 if( p==0 ) return 0;
69877 sqlite3BtreeEnter(p);
69878 assert( BTS_OVERWRITE==BTS_SECURE_DELETE*2 )((void) (0));
69879 assert( BTS_FAST_SECURE==(BTS_OVERWRITE|BTS_SECURE_DELETE) )((void) (0));
69880 if( newFlag>=0 ){
69881 p->pBt->btsFlags &= ~BTS_FAST_SECURE0x000c;
69882 p->pBt->btsFlags |= BTS_SECURE_DELETE0x0004*newFlag;
69883 }
69884 b = (p->pBt->btsFlags & BTS_FAST_SECURE0x000c)/BTS_SECURE_DELETE0x0004;
69885 sqlite3BtreeLeave(p);
69886 return b;
69887}
69888
69889/*
69890** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
69891** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
69892** is disabled. The default value for the auto-vacuum property is
69893** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
69894*/
69895SQLITE_PRIVATEstatic int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
69896#ifdef SQLITE_OMIT_AUTOVACUUM
69897 return SQLITE_READONLY8;
69898#else
69899 BtShared *pBt = p->pBt;
69900 int rc = SQLITE_OK0;
69901 u8 av = (u8)autoVacuum;
69902
69903 sqlite3BtreeEnter(p);
69904 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED0x0002)!=0 && (av ?1:0)!=pBt->autoVacuum ){
69905 rc = SQLITE_READONLY8;
69906 }else{
69907 pBt->autoVacuum = av ?1:0;
69908 pBt->incrVacuum = av==2 ?1:0;
69909 }
69910 sqlite3BtreeLeave(p);
69911 return rc;
69912#endif
69913}
69914
69915/*
69916** Return the value of the 'auto-vacuum' property. If auto-vacuum is
69917** enabled 1 is returned. Otherwise 0.
69918*/
69919SQLITE_PRIVATEstatic int sqlite3BtreeGetAutoVacuum(Btree *p){
69920#ifdef SQLITE_OMIT_AUTOVACUUM
69921 return BTREE_AUTOVACUUM_NONE0;
69922#else
69923 int rc;
69924 sqlite3BtreeEnter(p);
69925 rc = (
69926 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE0:
69927 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL1:
69928 BTREE_AUTOVACUUM_INCR2
69929 );
69930 sqlite3BtreeLeave(p);
69931 return rc;
69932#endif
69933}
69934
69935/*
69936** If the user has not set the safety-level for this database connection
69937** using "PRAGMA synchronous", and if the safety-level is not already
69938** set to the value passed to this function as the second parameter,
69939** set it so.
69940*/
69941#if SQLITE_DEFAULT_SYNCHRONOUS2!=SQLITE_DEFAULT_WAL_SYNCHRONOUS1 \
69942 && !defined(SQLITE_OMIT_WAL)
69943static void setDefaultSyncFlag(BtShared *pBt, u8 safety_level){
69944 sqlite3 *db;
69945 Db *pDb;
69946 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
69947 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
69948 if( pDb->bSyncSet==0
69949 && pDb->safety_level!=safety_level
69950 && pDb!=&db->aDb[1]
69951 ){
69952 pDb->safety_level = safety_level;
69953 sqlite3PagerSetFlags(pBt->pPager,
69954 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK0x38));
69955 }
69956 }
69957}
69958#else
69959# define setDefaultSyncFlag(pBt,safety_level)
69960#endif
69961
69962/* Forward declaration */
69963static int newDatabase(BtShared*);
69964
69965
69966/*
69967** Get a reference to pPage1 of the database file. This will
69968** also acquire a readlock on that file.
69969**
69970** SQLITE_OK is returned on success. If the file is not a
69971** well-formed database file, then SQLITE_CORRUPT is returned.
69972** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
69973** is returned if we run out of memory.
69974*/
69975static int lockBtree(BtShared *pBt){
69976 int rc; /* Result code from subfunctions */
69977 MemPage *pPage1; /* Page 1 of the database file */
69978 u32 nPage; /* Number of pages in the database */
69979 u32 nPageFile = 0; /* Number of pages in the database file */
69980
69981 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
69982 assert( pBt->pPage1==0 )((void) (0));
69983 rc = sqlite3PagerSharedLock(pBt->pPager);
69984 if( rc!=SQLITE_OK0 ) return rc;
69985 rc = btreeGetPage(pBt, 1, &pPage1, 0);
69986 if( rc!=SQLITE_OK0 ) return rc;
69987
69988 /* Do some checking to help insure the file we opened really is
69989 ** a valid database file.
69990 */
69991 nPage = get4bytesqlite3Get4byte(28+(u8*)pPage1->aData);
69992 sqlite3PagerPagecount(pBt->pPager, (int*)&nPageFile);
69993 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
69994 nPage = nPageFile;
69995 }
69996 if( (pBt->db->flags & SQLITE_ResetDatabase0x02000000)!=0 ){
69997 nPage = 0;
69998 }
69999 if( nPage>0 ){
70000 u32 pageSize;
70001 u32 usableSize;
70002 u8 *page1 = pPage1->aData;
70003 rc = SQLITE_NOTADB26;
70004 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
70005 ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d
70006 ** 61 74 20 33 00. */
70007 if( memcmp(page1, zMagicHeader, 16)!=0 ){
70008 goto page1_init_failed;
70009 }
70010
70011#ifdef SQLITE_OMIT_WAL
70012 if( page1[18]>1 ){
70013 pBt->btsFlags |= BTS_READ_ONLY0x0001;
70014 }
70015 if( page1[19]>1 ){
70016 goto page1_init_failed;
70017 }
70018#else
70019 if( page1[18]>2 ){
70020 pBt->btsFlags |= BTS_READ_ONLY0x0001;
70021 }
70022 if( page1[19]>2 ){
70023 goto page1_init_failed;
70024 }
70025
70026 /* If the read version is set to 2, this database should be accessed
70027 ** in WAL mode. If the log is not already open, open it now. Then
70028 ** return SQLITE_OK and return without populating BtShared.pPage1.
70029 ** The caller detects this and calls this function again. This is
70030 ** required as the version of page 1 currently in the page1 buffer
70031 ** may not be the latest version - there may be a newer one in the log
70032 ** file.
70033 */
70034 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL0x0020)==0 ){
70035 int isOpen = 0;
70036 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
70037 if( rc!=SQLITE_OK0 ){
70038 goto page1_init_failed;
70039 }else{
70040 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_WAL_SYNCHRONOUS1+1);
70041 if( isOpen==0 ){
70042 releasePageOne(pPage1);
70043 return SQLITE_OK0;
70044 }
70045 }
70046 rc = SQLITE_NOTADB26;
70047 }else{
70048 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_SYNCHRONOUS2+1);
70049 }
70050#endif
70051
70052 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
70053 ** fractions and the leaf payload fraction values must be 64, 32, and 32.
70054 **
70055 ** The original design allowed these amounts to vary, but as of
70056 ** version 3.6.0, we require them to be fixed.
70057 */
70058 if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
70059 goto page1_init_failed;
70060 }
70061 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
70062 ** determined by the 2-byte integer located at an offset of 16 bytes from
70063 ** the beginning of the database file. */
70064 pageSize = (page1[16]<<8) | (page1[17]<<16);
70065 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
70066 ** between 512 and 65536 inclusive. */
70067 if( ((pageSize-1)&pageSize)!=0
70068 || pageSize>SQLITE_MAX_PAGE_SIZE65536
70069 || pageSize<=256
70070 ){
70071 goto page1_init_failed;
70072 }
70073 pBt->btsFlags |= BTS_PAGESIZE_FIXED0x0002;
70074 assert( (pageSize & 7)==0 )((void) (0));
70075 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
70076 ** integer at offset 20 is the number of bytes of space at the end of
70077 ** each page to reserve for extensions.
70078 **
70079 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
70080 ** determined by the one-byte unsigned integer found at an offset of 20
70081 ** into the database file header. */
70082 usableSize = pageSize - page1[20];
70083 if( (u32)pageSize!=pBt->pageSize ){
70084 /* After reading the first page of the database assuming a page size
70085 ** of BtShared.pageSize, we have discovered that the page-size is
70086 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
70087 ** zero and return SQLITE_OK. The caller will call this function
70088 ** again with the correct page-size.
70089 */
70090 releasePageOne(pPage1);
70091 pBt->usableSize = usableSize;
70092 pBt->pageSize = pageSize;
70093 freeTempSpace(pBt);
70094 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
70095 pageSize-usableSize);
70096 return rc;
70097 }
70098 if( nPage>nPageFile ){
70099 if( sqlite3WritableSchema(pBt->db)==0 ){
70100 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(70100);
70101 goto page1_init_failed;
70102 }else{
70103 nPage = nPageFile;
70104 }
70105 }
70106 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
70107 ** be less than 480. In other words, if the page size is 512, then the
70108 ** reserved space size cannot exceed 32. */
70109 if( usableSize<480 ){
70110 goto page1_init_failed;
70111 }
70112 pBt->pageSize = pageSize;
70113 pBt->usableSize = usableSize;
70114#ifndef SQLITE_OMIT_AUTOVACUUM
70115 pBt->autoVacuum = (get4bytesqlite3Get4byte(&page1[36 + 4*4])?1:0);
70116 pBt->incrVacuum = (get4bytesqlite3Get4byte(&page1[36 + 7*4])?1:0);
70117#endif
70118 }
70119
70120 /* maxLocal is the maximum amount of payload to store locally for
70121 ** a cell. Make sure it is small enough so that at least minFanout
70122 ** cells can will fit on one page. We assume a 10-byte page header.
70123 ** Besides the payload, the cell must store:
70124 ** 2-byte pointer to the cell
70125 ** 4-byte child pointer
70126 ** 9-byte nKey value
70127 ** 4-byte nData value
70128 ** 4-byte overflow page pointer
70129 ** So a cell consists of a 2-byte pointer, a header which is as much as
70130 ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
70131 ** page pointer.
70132 */
70133 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
70134 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
70135 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
70136 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
70137 if( pBt->maxLocal>127 ){
70138 pBt->max1bytePayload = 127;
70139 }else{
70140 pBt->max1bytePayload = (u8)pBt->maxLocal;
70141 }
70142 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) )((void) (0));
70143 pBt->pPage1 = pPage1;
70144 pBt->nPage = nPage;
70145 return SQLITE_OK0;
70146
70147page1_init_failed:
70148 releasePageOne(pPage1);
70149 pBt->pPage1 = 0;
70150 return rc;
70151}
70152
70153#ifndef NDEBUG1
70154/*
70155** Return the number of cursors open on pBt. This is for use
70156** in assert() expressions, so it is only compiled if NDEBUG is not
70157** defined.
70158**
70159** Only write cursors are counted if wrOnly is true. If wrOnly is
70160** false then all cursors are counted.
70161**
70162** For the purposes of this routine, a cursor is any cursor that
70163** is capable of reading or writing to the database. Cursors that
70164** have been tripped into the CURSOR_FAULT state are not counted.
70165*/
70166static int countValidCursors(BtShared *pBt, int wrOnly){
70167 BtCursor *pCur;
70168 int r = 0;
70169 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
70170 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag0x01)!=0)
70171 && pCur->eState!=CURSOR_FAULT4 ) r++;
70172 }
70173 return r;
70174}
70175#endif
70176
70177/*
70178** If there are no outstanding cursors and we are not in the middle
70179** of a transaction but there is a read lock on the database, then
70180** this routine unrefs the first page of the database file which
70181** has the effect of releasing the read lock.
70182**
70183** If there is a transaction in progress, this routine is a no-op.
70184*/
70185static void unlockBtreeIfUnused(BtShared *pBt){
70186 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
70187 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE )((void) (0));
70188 if( pBt->inTransaction==TRANS_NONE0 && pBt->pPage1!=0 ){
70189 MemPage *pPage1 = pBt->pPage1;
70190 assert( pPage1->aData )((void) (0));
70191 assert( sqlite3PagerRefcount(pBt->pPager)==1 )((void) (0));
70192 pBt->pPage1 = 0;
70193 releasePageOne(pPage1);
70194 }
70195}
70196
70197/*
70198** If pBt points to an empty file then convert that empty file
70199** into a new empty database by initializing the first page of
70200** the database.
70201*/
70202static int newDatabase(BtShared *pBt){
70203 MemPage *pP1;
70204 unsigned char *data;
70205 int rc;
70206
70207 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
70208 if( pBt->nPage>0 ){
70209 return SQLITE_OK0;
70210 }
70211 pP1 = pBt->pPage1;
70212 assert( pP1!=0 )((void) (0));
70213 data = pP1->aData;
70214 rc = sqlite3PagerWrite(pP1->pDbPage);
70215 if( rc ) return rc;
70216 memcpy(data, zMagicHeader, sizeof(zMagicHeader));
70217 assert( sizeof(zMagicHeader)==16 )((void) (0));
70218 data[16] = (u8)((pBt->pageSize>>8)&0xff);
70219 data[17] = (u8)((pBt->pageSize>>16)&0xff);
70220 data[18] = 1;
70221 data[19] = 1;
70222 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize)((void) (0));
70223 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
70224 data[21] = 64;
70225 data[22] = 32;
70226 data[23] = 32;
70227 memset(&data[24], 0, 100-24);
70228 zeroPage(pP1, PTF_INTKEY0x01|PTF_LEAF0x08|PTF_LEAFDATA0x04 );
70229 pBt->btsFlags |= BTS_PAGESIZE_FIXED0x0002;
70230#ifndef SQLITE_OMIT_AUTOVACUUM
70231 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 )((void) (0));
70232 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 )((void) (0));
70233 put4bytesqlite3Put4byte(&data[36 + 4*4], pBt->autoVacuum);
70234 put4bytesqlite3Put4byte(&data[36 + 7*4], pBt->incrVacuum);
70235#endif
70236 pBt->nPage = 1;
70237 data[31] = 1;
70238 return SQLITE_OK0;
70239}
70240
70241/*
70242** Initialize the first page of the database file (creating a database
70243** consisting of a single page and no schema objects). Return SQLITE_OK
70244** if successful, or an SQLite error code otherwise.
70245*/
70246SQLITE_PRIVATEstatic int sqlite3BtreeNewDb(Btree *p){
70247 int rc;
70248 sqlite3BtreeEnter(p);
70249 p->pBt->nPage = 0;
70250 rc = newDatabase(p->pBt);
70251 sqlite3BtreeLeave(p);
70252 return rc;
70253}
70254
70255/*
70256** Attempt to start a new transaction. A write-transaction
70257** is started if the second argument is nonzero, otherwise a read-
70258** transaction. If the second argument is 2 or more and exclusive
70259** transaction is started, meaning that no other process is allowed
70260** to access the database. A preexisting transaction may not be
70261** upgraded to exclusive by calling this routine a second time - the
70262** exclusivity flag only works for a new transaction.
70263**
70264** A write-transaction must be started before attempting any
70265** changes to the database. None of the following routines
70266** will work unless a transaction is started first:
70267**
70268** sqlite3BtreeCreateTable()
70269** sqlite3BtreeCreateIndex()
70270** sqlite3BtreeClearTable()
70271** sqlite3BtreeDropTable()
70272** sqlite3BtreeInsert()
70273** sqlite3BtreeDelete()
70274** sqlite3BtreeUpdateMeta()
70275**
70276** If an initial attempt to acquire the lock fails because of lock contention
70277** and the database was previously unlocked, then invoke the busy handler
70278** if there is one. But if there was previously a read-lock, do not
70279** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
70280** returned when there is already a read-lock in order to avoid a deadlock.
70281**
70282** Suppose there are two processes A and B. A has a read lock and B has
70283** a reserved lock. B tries to promote to exclusive but is blocked because
70284** of A's read lock. A tries to promote to reserved but is blocked by B.
70285** One or the other of the two processes must give way or there can be
70286** no progress. By returning SQLITE_BUSY and not invoking the busy callback
70287** when A already has a read lock, we encourage A to give up and let B
70288** proceed.
70289*/
70290SQLITE_PRIVATEstatic int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){
70291 BtShared *pBt = p->pBt;
70292 Pager *pPager = pBt->pPager;
70293 int rc = SQLITE_OK0;
70294
70295 sqlite3BtreeEnter(p);
70296 btreeIntegrity(p)((void) (0)); ((void) (0));;
70297
70298 /* If the btree is already in a write-transaction, or it
70299 ** is already in a read-transaction and a read-transaction
70300 ** is requested, this is a no-op.
70301 */
70302 if( p->inTrans==TRANS_WRITE2 || (p->inTrans==TRANS_READ1 && !wrflag) ){
70303 goto trans_begun;
70304 }
70305 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 )((void) (0));
70306
70307 if( (p->db->flags & SQLITE_ResetDatabase0x02000000)
70308 && sqlite3PagerIsreadonly(pPager)==0
70309 ){
70310 pBt->btsFlags &= ~BTS_READ_ONLY0x0001;
70311 }
70312
70313 /* Write transactions are not possible on a read-only database */
70314 if( (pBt->btsFlags & BTS_READ_ONLY0x0001)!=0 && wrflag ){
70315 rc = SQLITE_READONLY8;
70316 goto trans_begun;
70317 }
70318
70319#ifndef SQLITE_OMIT_SHARED_CACHE
70320 {
70321 sqlite3 *pBlock = 0;
70322 /* If another database handle has already opened a write transaction
70323 ** on this shared-btree structure and a second write transaction is
70324 ** requested, return SQLITE_LOCKED.
70325 */
70326 if( (wrflag && pBt->inTransaction==TRANS_WRITE2)
70327 || (pBt->btsFlags & BTS_PENDING0x0080)!=0
70328 ){
70329 pBlock = pBt->pWriter->db;
70330 }else if( wrflag>1 ){
70331 BtLock *pIter;
70332 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
70333 if( pIter->pBtree!=p ){
70334 pBlock = pIter->pBtree->db;
70335 break;
70336 }
70337 }
70338 }
70339 if( pBlock ){
70340 sqlite3ConnectionBlocked(p->db, pBlock);
70341 rc = SQLITE_LOCKED_SHAREDCACHE(6 | (1<<8));
70342 goto trans_begun;
70343 }
70344 }
70345#endif
70346
70347 /* Any read-only or read-write transaction implies a read-lock on
70348 ** page 1. So if some other shared-cache client already has a write-lock
70349 ** on page 1, the transaction cannot be opened. */
70350 rc = querySharedCacheTableLock(p, SCHEMA_ROOT1, READ_LOCK1);
70351 if( SQLITE_OK0!=rc ) goto trans_begun;
70352
70353 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY0x0010;
70354 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY0x0010;
70355 do {
70356 sqlite3PagerWalDb(pPager, p->db);
70357
70358#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
70359 /* If transitioning from no transaction directly to a write transaction,
70360 ** block for the WRITER lock first if possible. */
70361 if( pBt->pPage1==0 && wrflag ){
70362 assert( pBt->inTransaction==TRANS_NONE )((void) (0));
70363 rc = sqlite3PagerWalWriteLock(pPager, 1)0;
70364 if( rc!=SQLITE_BUSY5 && rc!=SQLITE_OK0 ) break;
70365 }
70366#endif
70367
70368 /* Call lockBtree() until either pBt->pPage1 is populated or
70369 ** lockBtree() returns something other than SQLITE_OK. lockBtree()
70370 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
70371 ** reading page 1 it discovers that the page-size of the database
70372 ** file is not pBt->pageSize. In this case lockBtree() will update
70373 ** pBt->pageSize to the page-size of the file on disk.
70374 */
70375 while( pBt->pPage1==0 && SQLITE_OK0==(rc = lockBtree(pBt)) );
70376
70377 if( rc==SQLITE_OK0 && wrflag ){
70378 if( (pBt->btsFlags & BTS_READ_ONLY0x0001)!=0 ){
70379 rc = SQLITE_READONLY8;
70380 }else{
70381 rc = sqlite3PagerBegin(pPager, wrflag>1, sqlite3TempInMemory(p->db));
70382 if( rc==SQLITE_OK0 ){
70383 rc = newDatabase(pBt);
70384 }else if( rc==SQLITE_BUSY_SNAPSHOT(5 | (2<<8)) && pBt->inTransaction==TRANS_NONE0 ){
70385 /* if there was no transaction opened when this function was
70386 ** called and SQLITE_BUSY_SNAPSHOT is returned, change the error
70387 ** code to SQLITE_BUSY. */
70388 rc = SQLITE_BUSY5;
70389 }
70390 }
70391 }
70392
70393 if( rc!=SQLITE_OK0 ){
70394 (void)sqlite3PagerWalWriteLock(pPager, 0)0;
70395 unlockBtreeIfUnused(pBt);
70396 }
70397 }while( (rc&0xFF)==SQLITE_BUSY5 && pBt->inTransaction==TRANS_NONE0 &&
70398 btreeInvokeBusyHandler(pBt) );
70399 sqlite3PagerWalDb(pPager, 0);
70400#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
70401 if( rc==SQLITE_BUSY_TIMEOUT(5 | (3<<8)) ) rc = SQLITE_BUSY5;
70402#endif
70403
70404 if( rc==SQLITE_OK0 ){
70405 if( p->inTrans==TRANS_NONE0 ){
70406 pBt->nTransaction++;
70407#ifndef SQLITE_OMIT_SHARED_CACHE
70408 if( p->sharable ){
70409 assert( p->lock.pBtree==p && p->lock.iTable==1 )((void) (0));
70410 p->lock.eLock = READ_LOCK1;
70411 p->lock.pNext = pBt->pLock;
70412 pBt->pLock = &p->lock;
70413 }
70414#endif
70415 }
70416 p->inTrans = (wrflag?TRANS_WRITE2:TRANS_READ1);
70417 if( p->inTrans>pBt->inTransaction ){
70418 pBt->inTransaction = p->inTrans;
70419 }
70420 if( wrflag ){
70421 MemPage *pPage1 = pBt->pPage1;
70422#ifndef SQLITE_OMIT_SHARED_CACHE
70423 assert( !pBt->pWriter )((void) (0));
70424 pBt->pWriter = p;
70425 pBt->btsFlags &= ~BTS_EXCLUSIVE0x0040;
70426 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE0x0040;
70427#endif
70428
70429 /* If the db-size header field is incorrect (as it may be if an old
70430 ** client has been writing the database file), update it now. Doing
70431 ** this sooner rather than later means the database size can safely
70432 ** re-read the database size from page 1 if a savepoint or transaction
70433 ** rollback occurs within the transaction.
70434 */
70435 if( pBt->nPage!=get4bytesqlite3Get4byte(&pPage1->aData[28]) ){
70436 rc = sqlite3PagerWrite(pPage1->pDbPage);
70437 if( rc==SQLITE_OK0 ){
70438 put4bytesqlite3Put4byte(&pPage1->aData[28], pBt->nPage);
70439 }
70440 }
70441 }
70442 }
70443
70444trans_begun:
70445 if( rc==SQLITE_OK0 ){
70446 if( pSchemaVersion ){
70447 *pSchemaVersion = get4bytesqlite3Get4byte(&pBt->pPage1->aData[40]);
70448 }
70449 if( wrflag ){
70450 /* This call makes sure that the pager has the correct number of
70451 ** open savepoints. If the second parameter is greater than 0 and
70452 ** the sub-journal is not already open, then it will be opened here.
70453 */
70454 rc = sqlite3PagerOpenSavepoint(pPager, p->db->nSavepoint);
70455 }
70456 }
70457
70458 btreeIntegrity(p)((void) (0)); ((void) (0));;
70459 sqlite3BtreeLeave(p);
70460 return rc;
70461}
70462
70463#ifndef SQLITE_OMIT_AUTOVACUUM
70464
70465/*
70466** Set the pointer-map entries for all children of page pPage. Also, if
70467** pPage contains cells that point to overflow pages, set the pointer
70468** map entries for the overflow pages as well.
70469*/
70470static int setChildPtrmaps(MemPage *pPage){
70471 int i; /* Counter variable */
70472 int nCell; /* Number of cells in page pPage */
70473 int rc; /* Return code */
70474 BtShared *pBt = pPage->pBt;
70475 Pgno pgno = pPage->pgno;
70476
70477 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
70478 rc = pPage->isInit ? SQLITE_OK0 : btreeInitPage(pPage);
70479 if( rc!=SQLITE_OK0 ) return rc;
70480 nCell = pPage->nCell;
70481
70482 for(i=0; i<nCell; i++){
70483 u8 *pCell = findCell(pPage, i)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(i)])[0]<<8 | (&(pPage)->aCellIdx
[2*(i)])[1])))
;
70484
70485 ptrmapPutOvflPtr(pPage, pPage, pCell, &rc);
70486
70487 if( !pPage->leaf ){
70488 Pgno childPgno = get4bytesqlite3Get4byte(pCell);
70489 ptrmapPut(pBt, childPgno, PTRMAP_BTREE5, pgno, &rc);
70490 }
70491 }
70492
70493 if( !pPage->leaf ){
70494 Pgno childPgno = get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]);
70495 ptrmapPut(pBt, childPgno, PTRMAP_BTREE5, pgno, &rc);
70496 }
70497
70498 return rc;
70499}
70500
70501/*
70502** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so
70503** that it points to iTo. Parameter eType describes the type of pointer to
70504** be modified, as follows:
70505**
70506** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
70507** page of pPage.
70508**
70509** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
70510** page pointed to by one of the cells on pPage.
70511**
70512** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
70513** overflow page in the list.
70514*/
70515static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
70516 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
70517 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
70518 if( eType==PTRMAP_OVERFLOW24 ){
70519 /* The pointer is always the first 4 bytes of the page in this case. */
70520 if( get4bytesqlite3Get4byte(pPage->aData)!=iFrom ){
70521 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(70521);
70522 }
70523 put4bytesqlite3Put4byte(pPage->aData, iTo);
70524 }else{
70525 int i;
70526 int nCell;
70527 int rc;
70528
70529 rc = pPage->isInit ? SQLITE_OK0 : btreeInitPage(pPage);
70530 if( rc ) return rc;
70531 nCell = pPage->nCell;
70532
70533 for(i=0; i<nCell; i++){
70534 u8 *pCell = findCell(pPage, i)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(i)])[0]<<8 | (&(pPage)->aCellIdx
[2*(i)])[1])))
;
70535 if( eType==PTRMAP_OVERFLOW13 ){
70536 CellInfo info;
70537 pPage->xParseCell(pPage, pCell, &info);
70538 if( info.nLocal<info.nPayload ){
70539 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
70540 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(70540);
70541 }
70542 if( iFrom==get4bytesqlite3Get4byte(pCell+info.nSize-4) ){
70543 put4bytesqlite3Put4byte(pCell+info.nSize-4, iTo);
70544 break;
70545 }
70546 }
70547 }else{
70548 if( get4bytesqlite3Get4byte(pCell)==iFrom ){
70549 put4bytesqlite3Put4byte(pCell, iTo);
70550 break;
70551 }
70552 }
70553 }
70554
70555 if( i==nCell ){
70556 if( eType!=PTRMAP_BTREE5 ||
70557 get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
70558 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(70558);
70559 }
70560 put4bytesqlite3Put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
70561 }
70562 }
70563 return SQLITE_OK0;
70564}
70565
70566
70567/*
70568** Move the open database page pDbPage to location iFreePage in the
70569** database. The pDbPage reference remains valid.
70570**
70571** The isCommit flag indicates that there is no need to remember that
70572** the journal needs to be sync()ed before database page pDbPage->pgno
70573** can be written to. The caller has already promised not to write to that
70574** page.
70575*/
70576static int relocatePage(
70577 BtShared *pBt, /* Btree */
70578 MemPage *pDbPage, /* Open page to move */
70579 u8 eType, /* Pointer map 'type' entry for pDbPage */
70580 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
70581 Pgno iFreePage, /* The location to move pDbPage to */
70582 int isCommit /* isCommit flag passed to sqlite3PagerMovepage */
70583){
70584 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
70585 Pgno iDbPage = pDbPage->pgno;
70586 Pager *pPager = pBt->pPager;
70587 int rc;
70588
70589 assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||((void) (0))
70590 eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE )((void) (0));
70591 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
70592 assert( pDbPage->pBt==pBt )((void) (0));
70593 if( iDbPage<3 ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(70593);
70594
70595 /* Move page iDbPage from its current location to page number iFreePage */
70596 TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
70597 iDbPage, iFreePage, iPtrPage, eType));
70598 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
70599 if( rc!=SQLITE_OK0 ){
70600 return rc;
70601 }
70602 pDbPage->pgno = iFreePage;
70603
70604 /* If pDbPage was a btree-page, then it may have child pages and/or cells
70605 ** that point to overflow pages. The pointer map entries for all these
70606 ** pages need to be changed.
70607 **
70608 ** If pDbPage is an overflow page, then the first 4 bytes may store a
70609 ** pointer to a subsequent overflow page. If this is the case, then
70610 ** the pointer map needs to be updated for the subsequent overflow page.
70611 */
70612 if( eType==PTRMAP_BTREE5 || eType==PTRMAP_ROOTPAGE1 ){
70613 rc = setChildPtrmaps(pDbPage);
70614 if( rc!=SQLITE_OK0 ){
70615 return rc;
70616 }
70617 }else{
70618 Pgno nextOvfl = get4bytesqlite3Get4byte(pDbPage->aData);
70619 if( nextOvfl!=0 ){
70620 ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW24, iFreePage, &rc);
70621 if( rc!=SQLITE_OK0 ){
70622 return rc;
70623 }
70624 }
70625 }
70626
70627 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
70628 ** that it points at iFreePage. Also fix the pointer map entry for
70629 ** iPtrPage.
70630 */
70631 if( eType!=PTRMAP_ROOTPAGE1 ){
70632 rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
70633 if( rc!=SQLITE_OK0 ){
70634 return rc;
70635 }
70636 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
70637 if( rc!=SQLITE_OK0 ){
70638 releasePage(pPtrPage);
70639 return rc;
70640 }
70641 rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
70642 releasePage(pPtrPage);
70643 if( rc==SQLITE_OK0 ){
70644 ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
70645 }
70646 }
70647 return rc;
70648}
70649
70650/* Forward declaration required by incrVacuumStep(). */
70651static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
70652
70653/*
70654** Perform a single step of an incremental-vacuum. If successful, return
70655** SQLITE_OK. If there is no work to do (and therefore no point in
70656** calling this function again), return SQLITE_DONE. Or, if an error
70657** occurs, return some other error code.
70658**
70659** More specifically, this function attempts to re-organize the database so
70660** that the last page of the file currently in use is no longer in use.
70661**
70662** Parameter nFin is the number of pages that this database would contain
70663** were this function called until it returns SQLITE_DONE.
70664**
70665** If the bCommit parameter is non-zero, this function assumes that the
70666** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE
70667** or an error. bCommit is passed true for an auto-vacuum-on-commit
70668** operation, or false for an incremental vacuum.
70669*/
70670static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){
70671 Pgno nFreeList; /* Number of pages still on the free-list */
70672 int rc;
70673
70674 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
70675 assert( iLastPg>nFin )((void) (0));
70676
70677 if( !PTRMAP_ISPAGE(pBt, iLastPg)(ptrmapPageno((pBt), (iLastPg))==(iLastPg)) && iLastPg!=PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
70678 u8 eType;
70679 Pgno iPtrPage;
70680
70681 nFreeList = get4bytesqlite3Get4byte(&pBt->pPage1->aData[36]);
70682 if( nFreeList==0 ){
70683 return SQLITE_DONE101;
70684 }
70685
70686 rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
70687 if( rc!=SQLITE_OK0 ){
70688 return rc;
70689 }
70690 if( eType==PTRMAP_ROOTPAGE1 ){
70691 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(70691);
70692 }
70693
70694 if( eType==PTRMAP_FREEPAGE2 ){
70695 if( bCommit==0 ){
70696 /* Remove the page from the files free-list. This is not required
70697 ** if bCommit is non-zero. In that case, the free-list will be
70698 ** truncated to zero after this function returns, so it doesn't
70699 ** matter if it still contains some garbage entries.
70700 */
70701 Pgno iFreePg;
70702 MemPage *pFreePg;
70703 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, BTALLOC_EXACT1);
70704 if( rc!=SQLITE_OK0 ){
70705 return rc;
70706 }
70707 assert( iFreePg==iLastPg )((void) (0));
70708 releasePage(pFreePg);
70709 }
70710 } else {
70711 Pgno iFreePg; /* Index of free page to move pLastPg to */
70712 MemPage *pLastPg;
70713 u8 eMode = BTALLOC_ANY0; /* Mode parameter for allocateBtreePage() */
70714 Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */
70715
70716 rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
70717 if( rc!=SQLITE_OK0 ){
70718 return rc;
70719 }
70720
70721 /* If bCommit is zero, this loop runs exactly once and page pLastPg
70722 ** is swapped with the first free page pulled off the free list.
70723 **
70724 ** On the other hand, if bCommit is greater than zero, then keep
70725 ** looping until a free-page located within the first nFin pages
70726 ** of the file is found.
70727 */
70728 if( bCommit==0 ){
70729 eMode = BTALLOC_LE2;
70730 iNear = nFin;
70731 }
70732 do {
70733 MemPage *pFreePg;
70734 Pgno dbSize = btreePagecount(pBt);
70735 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode);
70736 if( rc!=SQLITE_OK0 ){
70737 releasePage(pLastPg);
70738 return rc;
70739 }
70740 releasePage(pFreePg);
70741 if( iFreePg>dbSize ){
70742 releasePage(pLastPg);
70743 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(70743);
70744 }
70745 }while( bCommit && iFreePg>nFin );
70746 assert( iFreePg<iLastPg )((void) (0));
70747
70748 rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, bCommit);
70749 releasePage(pLastPg);
70750 if( rc!=SQLITE_OK0 ){
70751 return rc;
70752 }
70753 }
70754 }
70755
70756 if( bCommit==0 ){
70757 do {
70758 iLastPg--;
70759 }while( iLastPg==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) || PTRMAP_ISPAGE(pBt, iLastPg)(ptrmapPageno((pBt), (iLastPg))==(iLastPg)) );
70760 pBt->bDoTruncate = 1;
70761 pBt->nPage = iLastPg;
70762 }
70763 return SQLITE_OK0;
70764}
70765
70766/*
70767** The database opened by the first argument is an auto-vacuum database
70768** nOrig pages in size containing nFree free pages. Return the expected
70769** size of the database in pages following an auto-vacuum operation.
70770*/
70771static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){
70772 int nEntry; /* Number of entries on one ptrmap page */
70773 Pgno nPtrmap; /* Number of PtrMap pages to be freed */
70774 Pgno nFin; /* Return value */
70775
70776 nEntry = pBt->usableSize/5;
70777 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)ptrmapPageno(pBt, nOrig)+nEntry)/nEntry;
70778 nFin = nOrig - nFree - nPtrmap;
70779 if( nOrig>PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) && nFin<PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
70780 nFin--;
70781 }
70782 while( PTRMAP_ISPAGE(pBt, nFin)(ptrmapPageno((pBt), (nFin))==(nFin)) || nFin==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
70783 nFin--;
70784 }
70785
70786 return nFin;
70787}
70788
70789/*
70790** A write-transaction must be opened before calling this function.
70791** It performs a single unit of work towards an incremental vacuum.
70792**
70793** If the incremental vacuum is finished after this function has run,
70794** SQLITE_DONE is returned. If it is not finished, but no error occurred,
70795** SQLITE_OK is returned. Otherwise an SQLite error code.
70796*/
70797SQLITE_PRIVATEstatic int sqlite3BtreeIncrVacuum(Btree *p){
70798 int rc;
70799 BtShared *pBt = p->pBt;
70800
70801 sqlite3BtreeEnter(p);
70802 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE )((void) (0));
70803 if( !pBt->autoVacuum ){
70804 rc = SQLITE_DONE101;
70805 }else{
70806 Pgno nOrig = btreePagecount(pBt);
70807 Pgno nFree = get4bytesqlite3Get4byte(&pBt->pPage1->aData[36]);
70808 Pgno nFin = finalDbSize(pBt, nOrig, nFree);
70809
70810 if( nOrig<nFin || nFree>=nOrig ){
70811 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(70811);
70812 }else if( nFree>0 ){
70813 rc = saveAllCursors(pBt, 0, 0);
70814 if( rc==SQLITE_OK0 ){
70815 invalidateAllOverflowCache(pBt);
70816 rc = incrVacuumStep(pBt, nFin, nOrig, 0);
70817 }
70818 if( rc==SQLITE_OK0 ){
70819 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
70820 put4bytesqlite3Put4byte(&pBt->pPage1->aData[28], pBt->nPage);
70821 }
70822 }else{
70823 rc = SQLITE_DONE101;
70824 }
70825 }
70826 sqlite3BtreeLeave(p);
70827 return rc;
70828}
70829
70830/*
70831** This routine is called prior to sqlite3PagerCommit when a transaction
70832** is committed for an auto-vacuum database.
70833*/
70834static int autoVacuumCommit(Btree *p){
70835 int rc = SQLITE_OK0;
70836 Pager *pPager;
70837 BtShared *pBt;
70838 sqlite3 *db;
70839 VVA_ONLY( int nRef );
70840
70841 assert( p!=0 )((void) (0));
70842 pBt = p->pBt;
70843 pPager = pBt->pPager;
70844 VVA_ONLY( nRef = sqlite3PagerRefcount(pPager); )
70845
70846 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
70847 invalidateAllOverflowCache(pBt);
70848 assert(pBt->autoVacuum)((void) (0));
70849 if( !pBt->incrVacuum ){
70850 Pgno nFin; /* Number of pages in database after autovacuuming */
70851 Pgno nFree; /* Number of pages on the freelist initially */
70852 Pgno nVac; /* Number of pages to vacuum */
70853 Pgno iFree; /* The next page to be freed */
70854 Pgno nOrig; /* Database size before freeing */
70855
70856 nOrig = btreePagecount(pBt);
70857 if( PTRMAP_ISPAGE(pBt, nOrig)(ptrmapPageno((pBt), (nOrig))==(nOrig)) || nOrig==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
70858 /* It is not possible to create a database for which the final page
70859 ** is either a pointer-map page or the pending-byte page. If one
70860 ** is encountered, this indicates corruption.
70861 */
70862 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(70862);
70863 }
70864
70865 nFree = get4bytesqlite3Get4byte(&pBt->pPage1->aData[36]);
70866 db = p->db;
70867 if( db->xAutovacPages ){
70868 int iDb;
70869 for(iDb=0; ALWAYS(iDb<db->nDb)(iDb<db->nDb); iDb++){
70870 if( db->aDb[iDb].pBt==p ) break;
70871 }
70872 nVac = db->xAutovacPages(
70873 db->pAutovacPagesArg,
70874 db->aDb[iDb].zDbSName,
70875 nOrig,
70876 nFree,
70877 pBt->pageSize
70878 );
70879 if( nVac>nFree ){
70880 nVac = nFree;
70881 }
70882 if( nVac==0 ){
70883 return SQLITE_OK0;
70884 }
70885 }else{
70886 nVac = nFree;
70887 }
70888 nFin = finalDbSize(pBt, nOrig, nVac);
70889 if( nFin>nOrig ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(70889);
70890 if( nFin<nOrig ){
70891 rc = saveAllCursors(pBt, 0, 0);
70892 }
70893 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK0; iFree--){
70894 rc = incrVacuumStep(pBt, nFin, iFree, nVac==nFree);
70895 }
70896 if( (rc==SQLITE_DONE101 || rc==SQLITE_OK0) && nFree>0 ){
70897 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
70898 if( nVac==nFree ){
70899 put4bytesqlite3Put4byte(&pBt->pPage1->aData[32], 0);
70900 put4bytesqlite3Put4byte(&pBt->pPage1->aData[36], 0);
70901 }
70902 put4bytesqlite3Put4byte(&pBt->pPage1->aData[28], nFin);
70903 pBt->bDoTruncate = 1;
70904 pBt->nPage = nFin;
70905 }
70906 if( rc!=SQLITE_OK0 ){
70907 sqlite3PagerRollback(pPager);
70908 }
70909 }
70910
70911 assert( nRef>=sqlite3PagerRefcount(pPager) )((void) (0));
70912 return rc;
70913}
70914
70915#else /* ifndef SQLITE_OMIT_AUTOVACUUM */
70916# define setChildPtrmaps(x) SQLITE_OK0
70917#endif
70918
70919/*
70920** This routine does the first phase of a two-phase commit. This routine
70921** causes a rollback journal to be created (if it does not already exist)
70922** and populated with enough information so that if a power loss occurs
70923** the database can be restored to its original state by playing back
70924** the journal. Then the contents of the journal are flushed out to
70925** the disk. After the journal is safely on oxide, the changes to the
70926** database are written into the database file and flushed to oxide.
70927** At the end of this call, the rollback journal still exists on the
70928** disk and we are still holding all locks, so the transaction has not
70929** committed. See sqlite3BtreeCommitPhaseTwo() for the second phase of the
70930** commit process.
70931**
70932** This call is a no-op if no write-transaction is currently active on pBt.
70933**
70934** Otherwise, sync the database file for the btree pBt. zSuperJrnl points to
70935** the name of a super-journal file that should be written into the
70936** individual journal file, or is NULL, indicating no super-journal file
70937** (single database transaction).
70938**
70939** When this is called, the super-journal should already have been
70940** created, populated with this journal pointer and synced to disk.
70941**
70942** Once this is routine has returned, the only thing required to commit
70943** the write-transaction for this database file is to delete the journal.
70944*/
70945SQLITE_PRIVATEstatic int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zSuperJrnl){
70946 int rc = SQLITE_OK0;
70947 if( p->inTrans==TRANS_WRITE2 ){
70948 BtShared *pBt = p->pBt;
70949 sqlite3BtreeEnter(p);
70950#ifndef SQLITE_OMIT_AUTOVACUUM
70951 if( pBt->autoVacuum ){
70952 rc = autoVacuumCommit(p);
70953 if( rc!=SQLITE_OK0 ){
70954 sqlite3BtreeLeave(p);
70955 return rc;
70956 }
70957 }
70958 if( pBt->bDoTruncate ){
70959 sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
70960 }
70961#endif
70962 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zSuperJrnl, 0);
70963 sqlite3BtreeLeave(p);
70964 }
70965 return rc;
70966}
70967
70968/*
70969** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
70970** at the conclusion of a transaction.
70971*/
70972static void btreeEndTransaction(Btree *p){
70973 BtShared *pBt = p->pBt;
70974 sqlite3 *db = p->db;
70975 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
70976
70977#ifndef SQLITE_OMIT_AUTOVACUUM
70978 pBt->bDoTruncate = 0;
70979#endif
70980 if( p->inTrans>TRANS_NONE0 && db->nVdbeRead>1 ){
70981 /* If there are other active statements that belong to this database
70982 ** handle, downgrade to a read-only transaction. The other statements
70983 ** may still be reading from the database. */
70984 downgradeAllSharedCacheTableLocks(p);
70985 p->inTrans = TRANS_READ1;
70986 }else{
70987 /* If the handle had any kind of transaction open, decrement the
70988 ** transaction count of the shared btree. If the transaction count
70989 ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
70990 ** call below will unlock the pager. */
70991 if( p->inTrans!=TRANS_NONE0 ){
70992 clearAllSharedCacheTableLocks(p);
70993 pBt->nTransaction--;
70994 if( 0==pBt->nTransaction ){
70995 pBt->inTransaction = TRANS_NONE0;
70996 }
70997 }
70998
70999 /* Set the current transaction state to TRANS_NONE and unlock the
71000 ** pager if this call closed the only read or write transaction. */
71001 p->inTrans = TRANS_NONE0;
71002 unlockBtreeIfUnused(pBt);
71003 }
71004
71005 btreeIntegrity(p)((void) (0)); ((void) (0));;
71006}
71007
71008/*
71009** Commit the transaction currently in progress.
71010**
71011** This routine implements the second phase of a 2-phase commit. The
71012** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
71013** be invoked prior to calling this routine. The sqlite3BtreeCommitPhaseOne()
71014** routine did all the work of writing information out to disk and flushing the
71015** contents so that they are written onto the disk platter. All this
71016** routine has to do is delete or truncate or zero the header in the
71017** the rollback journal (which causes the transaction to commit) and
71018** drop locks.
71019**
71020** Normally, if an error occurs while the pager layer is attempting to
71021** finalize the underlying journal file, this function returns an error and
71022** the upper layer will attempt a rollback. However, if the second argument
71023** is non-zero then this b-tree transaction is part of a multi-file
71024** transaction. In this case, the transaction has already been committed
71025** (by deleting a super-journal file) and the caller will ignore this
71026** functions return code. So, even if an error occurs in the pager layer,
71027** reset the b-tree objects internal state to indicate that the write
71028** transaction has been closed. This is quite safe, as the pager will have
71029** transitioned to the error state.
71030**
71031** This will release the write lock on the database file. If there
71032** are no active cursors, it also releases the read lock.
71033*/
71034SQLITE_PRIVATEstatic int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
71035
71036 if( p->inTrans==TRANS_NONE0 ) return SQLITE_OK0;
71037 sqlite3BtreeEnter(p);
71038 btreeIntegrity(p)((void) (0)); ((void) (0));;
71039
71040 /* If the handle has a write-transaction open, commit the shared-btrees
71041 ** transaction and set the shared state to TRANS_READ.
71042 */
71043 if( p->inTrans==TRANS_WRITE2 ){
71044 int rc;
71045 BtShared *pBt = p->pBt;
71046 assert( pBt->inTransaction==TRANS_WRITE )((void) (0));
71047 assert( pBt->nTransaction>0 )((void) (0));
71048 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
71049 if( rc!=SQLITE_OK0 && bCleanup==0 ){
71050 sqlite3BtreeLeave(p);
71051 return rc;
71052 }
71053 p->iBDataVersion--; /* Compensate for pPager->iDataVersion++; */
71054 pBt->inTransaction = TRANS_READ1;
71055 btreeClearHasContent(pBt);
71056 }
71057
71058 btreeEndTransaction(p);
71059 sqlite3BtreeLeave(p);
71060 return SQLITE_OK0;
71061}
71062
71063/*
71064** Do both phases of a commit.
71065*/
71066SQLITE_PRIVATEstatic int sqlite3BtreeCommit(Btree *p){
71067 int rc;
71068 sqlite3BtreeEnter(p);
71069 rc = sqlite3BtreeCommitPhaseOne(p, 0);
71070 if( rc==SQLITE_OK0 ){
71071 rc = sqlite3BtreeCommitPhaseTwo(p, 0);
71072 }
71073 sqlite3BtreeLeave(p);
71074 return rc;
71075}
71076
71077/*
71078** This routine sets the state to CURSOR_FAULT and the error
71079** code to errCode for every cursor on any BtShared that pBtree
71080** references. Or if the writeOnly flag is set to 1, then only
71081** trip write cursors and leave read cursors unchanged.
71082**
71083** Every cursor is a candidate to be tripped, including cursors
71084** that belong to other database connections that happen to be
71085** sharing the cache with pBtree.
71086**
71087** This routine gets called when a rollback occurs. If the writeOnly
71088** flag is true, then only write-cursors need be tripped - read-only
71089** cursors save their current positions so that they may continue
71090** following the rollback. Or, if writeOnly is false, all cursors are
71091** tripped. In general, writeOnly is false if the transaction being
71092** rolled back modified the database schema. In this case b-tree root
71093** pages may be moved or deleted from the database altogether, making
71094** it unsafe for read cursors to continue.
71095**
71096** If the writeOnly flag is true and an error is encountered while
71097** saving the current position of a read-only cursor, all cursors,
71098** including all read-cursors are tripped.
71099**
71100** SQLITE_OK is returned if successful, or if an error occurs while
71101** saving a cursor position, an SQLite error code.
71102*/
71103SQLITE_PRIVATEstatic int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){
71104 BtCursor *p;
71105 int rc = SQLITE_OK0;
71106
71107 assert( (writeOnly==0 || writeOnly==1) && BTCF_WriteFlag==1 )((void) (0));
71108 if( pBtree ){
71109 sqlite3BtreeEnter(pBtree);
71110 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
71111 if( writeOnly && (p->curFlags & BTCF_WriteFlag0x01)==0 ){
71112 if( p->eState==CURSOR_VALID0 || p->eState==CURSOR_SKIPNEXT2 ){
71113 rc = saveCursorPosition(p);
71114 if( rc!=SQLITE_OK0 ){
71115 (void)sqlite3BtreeTripAllCursors(pBtree, rc, 0);
71116 break;
71117 }
71118 }
71119 }else{
71120 sqlite3BtreeClearCursor(p);
71121 p->eState = CURSOR_FAULT4;
71122 p->skipNext = errCode;
71123 }
71124 btreeReleaseAllCursorPages(p);
71125 }
71126 sqlite3BtreeLeave(pBtree);
71127 }
71128 return rc;
71129}
71130
71131/*
71132** Set the pBt->nPage field correctly, according to the current
71133** state of the database. Assume pBt->pPage1 is valid.
71134*/
71135static void btreeSetNPage(BtShared *pBt, MemPage *pPage1){
71136 int nPage = get4bytesqlite3Get4byte(&pPage1->aData[28]);
71137 testcase( nPage==0 );
71138 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
71139 testcase( pBt->nPage!=(u32)nPage );
71140 pBt->nPage = nPage;
71141}
71142
71143/*
71144** Rollback the transaction in progress.
71145**
71146** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped).
71147** Only write cursors are tripped if writeOnly is true but all cursors are
71148** tripped if writeOnly is false. Any attempt to use
71149** a tripped cursor will result in an error.
71150**
71151** This will release the write lock on the database file. If there
71152** are no active cursors, it also releases the read lock.
71153*/
71154SQLITE_PRIVATEstatic int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){
71155 int rc;
71156 BtShared *pBt = p->pBt;
71157 MemPage *pPage1;
71158
71159 assert( writeOnly==1 || writeOnly==0 )((void) (0));
71160 assert( tripCode==SQLITE_ABORT_ROLLBACK || tripCode==SQLITE_OK )((void) (0));
71161 sqlite3BtreeEnter(p);
71162 if( tripCode==SQLITE_OK0 ){
71163 rc = tripCode = saveAllCursors(pBt, 0, 0);
71164 if( rc ) writeOnly = 0;
71165 }else{
71166 rc = SQLITE_OK0;
71167 }
71168 if( tripCode ){
71169 int rc2 = sqlite3BtreeTripAllCursors(p, tripCode, writeOnly);
71170 assert( rc==SQLITE_OK || (writeOnly==0 && rc2==SQLITE_OK) )((void) (0));
71171 if( rc2!=SQLITE_OK0 ) rc = rc2;
71172 }
71173 btreeIntegrity(p)((void) (0)); ((void) (0));;
71174
71175 if( p->inTrans==TRANS_WRITE2 ){
71176 int rc2;
71177
71178 assert( TRANS_WRITE==pBt->inTransaction )((void) (0));
71179 rc2 = sqlite3PagerRollback(pBt->pPager);
71180 if( rc2!=SQLITE_OK0 ){
71181 rc = rc2;
71182 }
71183
71184 /* The rollback may have destroyed the pPage1->aData value. So
71185 ** call btreeGetPage() on page 1 again to make
71186 ** sure pPage1->aData is set correctly. */
71187 if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK0 ){
71188 btreeSetNPage(pBt, pPage1);
71189 releasePageOne(pPage1);
71190 }
71191 assert( countValidCursors(pBt, 1)==0 )((void) (0));
71192 pBt->inTransaction = TRANS_READ1;
71193 btreeClearHasContent(pBt);
71194 }
71195
71196 btreeEndTransaction(p);
71197 sqlite3BtreeLeave(p);
71198 return rc;
71199}
71200
71201/*
71202** Start a statement subtransaction. The subtransaction can be rolled
71203** back independently of the main transaction. You must start a transaction
71204** before starting a subtransaction. The subtransaction is ended automatically
71205** if the main transaction commits or rolls back.
71206**
71207** Statement subtransactions are used around individual SQL statements
71208** that are contained within a BEGIN...COMMIT block. If a constraint
71209** error occurs within the statement, the effect of that one statement
71210** can be rolled back without having to rollback the entire transaction.
71211**
71212** A statement sub-transaction is implemented as an anonymous savepoint. The
71213** value passed as the second parameter is the total number of savepoints,
71214** including the new anonymous savepoint, open on the B-Tree. i.e. if there
71215** are no active savepoints and no other statement-transactions open,
71216** iStatement is 1. This anonymous savepoint can be released or rolled back
71217** using the sqlite3BtreeSavepoint() function.
71218*/
71219SQLITE_PRIVATEstatic int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
71220 int rc;
71221 BtShared *pBt = p->pBt;
71222 sqlite3BtreeEnter(p);
71223 assert( p->inTrans==TRANS_WRITE )((void) (0));
71224 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 )((void) (0));
71225 assert( iStatement>0 )((void) (0));
71226 assert( iStatement>p->db->nSavepoint )((void) (0));
71227 assert( pBt->inTransaction==TRANS_WRITE )((void) (0));
71228 /* At the pager level, a statement transaction is a savepoint with
71229 ** an index greater than all savepoints created explicitly using
71230 ** SQL statements. It is illegal to open, release or rollback any
71231 ** such savepoints while the statement transaction savepoint is active.
71232 */
71233 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
71234 sqlite3BtreeLeave(p);
71235 return rc;
71236}
71237
71238/*
71239** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
71240** or SAVEPOINT_RELEASE. This function either releases or rolls back the
71241** savepoint identified by parameter iSavepoint, depending on the value
71242** of op.
71243**
71244** Normally, iSavepoint is greater than or equal to zero. However, if op is
71245** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
71246** contents of the entire transaction are rolled back. This is different
71247** from a normal transaction rollback, as no locks are released and the
71248** transaction remains open.
71249*/
71250SQLITE_PRIVATEstatic int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
71251 int rc = SQLITE_OK0;
71252 if( p && p->inTrans==TRANS_WRITE2 ){
71253 BtShared *pBt = p->pBt;
71254 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK )((void) (0));
71255 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) )((void) (0));
71256 sqlite3BtreeEnter(p);
71257 if( op==SAVEPOINT_ROLLBACK2 ){
71258 rc = saveAllCursors(pBt, 0, 0);
71259 }
71260 if( rc==SQLITE_OK0 ){
71261 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
71262 }
71263 if( rc==SQLITE_OK0 ){
71264 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY0x0010)!=0 ){
71265 pBt->nPage = 0;
71266 }
71267 rc = newDatabase(pBt);
71268 btreeSetNPage(pBt, pBt->pPage1);
71269
71270 /* pBt->nPage might be zero if the database was corrupt when
71271 ** the transaction was started. Otherwise, it must be at least 1. */
71272 assert( CORRUPT_DB || pBt->nPage>0 )((void) (0));
71273 }
71274 sqlite3BtreeLeave(p);
71275 }
71276 return rc;
71277}
71278
71279/*
71280** Create a new cursor for the BTree whose root is on the page
71281** iTable. If a read-only cursor is requested, it is assumed that
71282** the caller already has at least a read-only transaction open
71283** on the database already. If a write-cursor is requested, then
71284** the caller is assumed to have an open write transaction.
71285**
71286** If the BTREE_WRCSR bit of wrFlag is clear, then the cursor can only
71287** be used for reading. If the BTREE_WRCSR bit is set, then the cursor
71288** can be used for reading or for writing if other conditions for writing
71289** are also met. These are the conditions that must be met in order
71290** for writing to be allowed:
71291**
71292** 1: The cursor must have been opened with wrFlag containing BTREE_WRCSR
71293**
71294** 2: Other database connections that share the same pager cache
71295** but which are not in the READ_UNCOMMITTED state may not have
71296** cursors open with wrFlag==0 on the same table. Otherwise
71297** the changes made by this write cursor would be visible to
71298** the read cursors in the other database connection.
71299**
71300** 3: The database must be writable (not on read-only media)
71301**
71302** 4: There must be an active transaction.
71303**
71304** The BTREE_FORDELETE bit of wrFlag may optionally be set if BTREE_WRCSR
71305** is set. If FORDELETE is set, that is a hint to the implementation that
71306** this cursor will only be used to seek to and delete entries of an index
71307** as part of a larger DELETE statement. The FORDELETE hint is not used by
71308** this implementation. But in a hypothetical alternative storage engine
71309** in which index entries are automatically deleted when corresponding table
71310** rows are deleted, the FORDELETE flag is a hint that all SEEK and DELETE
71311** operations on this cursor can be no-ops and all READ operations can
71312** return a null row (2-bytes: 0x01 0x00).
71313**
71314** No checking is done to make sure that page iTable really is the
71315** root page of a b-tree. If it is not, then the cursor acquired
71316** will not work correctly.
71317**
71318** It is assumed that the sqlite3BtreeCursorZero() has been called
71319** on pCur to initialize the memory space prior to invoking this routine.
71320*/
71321static int btreeCursor(
71322 Btree *p, /* The btree */
71323 Pgno iTable, /* Root page of table to open */
71324 int wrFlag, /* 1 to write. 0 read-only */
71325 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
71326 BtCursor *pCur /* Space for new cursor */
71327){
71328 BtShared *pBt = p->pBt; /* Shared b-tree handle */
71329 BtCursor *pX; /* Looping over other all cursors */
71330
71331 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
71332 assert( wrFlag==0((void) (0))
71333 || wrFlag==BTREE_WRCSR((void) (0))
71334 || wrFlag==(BTREE_WRCSR|BTREE_FORDELETE)((void) (0))
71335 )((void) (0));
71336
71337 /* The following assert statements verify that if this is a sharable
71338 ** b-tree database, the connection is holding the required table locks,
71339 ** and that no other connection has any open cursor that conflicts with
71340 ** this lock. The iTable<1 term disables the check for corrupt schemas. */
71341 assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, (wrFlag?2:1))((void) (0))
71342 || iTable<1 )((void) (0));
71343 assert( wrFlag==0 || !hasReadConflicts(p, iTable) )((void) (0));
71344
71345 /* Assert that the caller has opened the required transaction. */
71346 assert( p->inTrans>TRANS_NONE )((void) (0));
71347 assert( wrFlag==0 || p->inTrans==TRANS_WRITE )((void) (0));
71348 assert( pBt->pPage1 && pBt->pPage1->aData )((void) (0));
71349 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 )((void) (0));
71350
71351 if( iTable<=1 ){
71352 if( iTable<1 ){
71353 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(71353);
71354 }else if( btreePagecount(pBt)==0 ){
71355 assert( wrFlag==0 )((void) (0));
71356 iTable = 0;
71357 }
71358 }
71359
71360 /* Now that no other errors can occur, finish filling in the BtCursor
71361 ** variables and link the cursor into the BtShared list. */
71362 pCur->pgnoRoot = iTable;
71363 pCur->iPage = -1;
71364 pCur->pKeyInfo = pKeyInfo;
71365 pCur->pBtree = p;
71366 pCur->pBt = pBt;
71367 pCur->curFlags = 0;
71368 /* If there are two or more cursors on the same btree, then all such
71369 ** cursors *must* have the BTCF_Multiple flag set. */
71370 for(pX=pBt->pCursor; pX; pX=pX->pNext){
71371 if( pX->pgnoRoot==iTable ){
71372 pX->curFlags |= BTCF_Multiple0x20;
71373 pCur->curFlags = BTCF_Multiple0x20;
71374 }
71375 }
71376 pCur->eState = CURSOR_INVALID1;
71377 pCur->pNext = pBt->pCursor;
71378 pBt->pCursor = pCur;
71379 if( wrFlag ){
71380 pCur->curFlags |= BTCF_WriteFlag0x01;
71381 pCur->curPagerFlags = 0;
71382 if( pBt->pTmpSpace==0 ) return allocateTempSpace(pBt);
71383 }else{
71384 pCur->curPagerFlags = PAGER_GET_READONLY0x02;
71385 }
71386 return SQLITE_OK0;
71387}
71388static int btreeCursorWithLock(
71389 Btree *p, /* The btree */
71390 Pgno iTable, /* Root page of table to open */
71391 int wrFlag, /* 1 to write. 0 read-only */
71392 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
71393 BtCursor *pCur /* Space for new cursor */
71394){
71395 int rc;
71396 sqlite3BtreeEnter(p);
71397 rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
71398 sqlite3BtreeLeave(p);
71399 return rc;
71400}
71401SQLITE_PRIVATEstatic int sqlite3BtreeCursor(
71402 Btree *p, /* The btree */
71403 Pgno iTable, /* Root page of table to open */
71404 int wrFlag, /* 1 to write. 0 read-only */
71405 struct KeyInfo *pKeyInfo, /* First arg to xCompare() */
71406 BtCursor *pCur /* Write new cursor here */
71407){
71408 if( p->sharable ){
71409 return btreeCursorWithLock(p, iTable, wrFlag, pKeyInfo, pCur);
71410 }else{
71411 return btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
71412 }
71413}
71414
71415/*
71416** Return the size of a BtCursor object in bytes.
71417**
71418** This interfaces is needed so that users of cursors can preallocate
71419** sufficient storage to hold a cursor. The BtCursor object is opaque
71420** to users so they cannot do the sizeof() themselves - they must call
71421** this routine.
71422*/
71423SQLITE_PRIVATEstatic int sqlite3BtreeCursorSize(void){
71424 return ROUND8(sizeof(BtCursor))(((sizeof(BtCursor))+7)&~7);
71425}
71426
71427/*
71428** Initialize memory that will be converted into a BtCursor object.
71429**
71430** The simple approach here would be to memset() the entire object
71431** to zero. But it turns out that the apPage[] and aiIdx[] arrays
71432** do not need to be zeroed and they are large, so we can save a lot
71433** of run-time by skipping the initialization of those elements.
71434*/
71435SQLITE_PRIVATEstatic void sqlite3BtreeCursorZero(BtCursor *p){
71436 memset(p, 0, offsetof(BtCursor, BTCURSOR_FIRST_UNINIT)__builtin_offsetof(BtCursor, pBt));
71437}
71438
71439/*
71440** Close a cursor. The read lock on the database file is released
71441** when the last cursor is closed.
71442*/
71443SQLITE_PRIVATEstatic int sqlite3BtreeCloseCursor(BtCursor *pCur){
71444 Btree *pBtree = pCur->pBtree;
71445 if( pBtree ){
71446 BtShared *pBt = pCur->pBt;
71447 sqlite3BtreeEnter(pBtree);
71448 assert( pBt->pCursor!=0 )((void) (0));
71449 if( pBt->pCursor==pCur ){
71450 pBt->pCursor = pCur->pNext;
71451 }else{
71452 BtCursor *pPrev = pBt->pCursor;
71453 do{
71454 if( pPrev->pNext==pCur ){
71455 pPrev->pNext = pCur->pNext;
71456 break;
71457 }
71458 pPrev = pPrev->pNext;
71459 }while( ALWAYS(pPrev)(pPrev) );
71460 }
71461 btreeReleaseAllCursorPages(pCur);
71462 unlockBtreeIfUnused(pBt);
71463 sqlite3_free(pCur->aOverflow);
71464 sqlite3_free(pCur->pKey);
71465 if( (pBt->openFlags & BTREE_SINGLE4) && pBt->pCursor==0 ){
71466 /* Since the BtShared is not sharable, there is no need to
71467 ** worry about the missing sqlite3BtreeLeave() call here. */
71468 assert( pBtree->sharable==0 )((void) (0));
71469 sqlite3BtreeClose(pBtree);
71470 }else{
71471 sqlite3BtreeLeave(pBtree);
71472 }
71473 pCur->pBtree = 0;
71474 }
71475 return SQLITE_OK0;
71476}
71477
71478/*
71479** Make sure the BtCursor* given in the argument has a valid
71480** BtCursor.info structure. If it is not already valid, call
71481** btreeParseCell() to fill it in.
71482**
71483** BtCursor.info is a cache of the information in the current cell.
71484** Using this cache reduces the number of calls to btreeParseCell().
71485*/
71486#ifndef NDEBUG1
71487 static int cellInfoEqual(CellInfo *a, CellInfo *b){
71488 if( a->nKey!=b->nKey ) return 0;
71489 if( a->pPayload!=b->pPayload ) return 0;
71490 if( a->nPayload!=b->nPayload ) return 0;
71491 if( a->nLocal!=b->nLocal ) return 0;
71492 if( a->nSize!=b->nSize ) return 0;
71493 return 1;
71494 }
71495 static void assertCellInfo(BtCursor *pCur){
71496 CellInfo info;
71497 memset(&info, 0, sizeof(info));
71498 btreeParseCell(pCur->pPage, pCur->ix, &info);
71499 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) )((void) (0));
71500 }
71501#else
71502 #define assertCellInfo(x)
71503#endif
71504static SQLITE_NOINLINE__attribute__((noinline)) void getCellInfo(BtCursor *pCur){
71505 if( pCur->info.nSize==0 ){
71506 pCur->curFlags |= BTCF_ValidNKey0x02;
71507 btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
71508 }else{
71509 assertCellInfo(pCur);
71510 }
71511}
71512
71513#ifndef NDEBUG1 /* The next routine used only within assert() statements */
71514/*
71515** Return true if the given BtCursor is valid. A valid cursor is one
71516** that is currently pointing to a row in a (non-empty) table.
71517** This is a verification routine is used only within assert() statements.
71518*/
71519SQLITE_PRIVATEstatic int sqlite3BtreeCursorIsValid(BtCursor *pCur){
71520 return pCur && pCur->eState==CURSOR_VALID0;
71521}
71522#endif /* NDEBUG */
71523SQLITE_PRIVATEstatic int sqlite3BtreeCursorIsValidNN(BtCursor *pCur){
71524 assert( pCur!=0 )((void) (0));
71525 return pCur->eState==CURSOR_VALID0;
71526}
71527
71528/*
71529** Return the value of the integer key or "rowid" for a table btree.
71530** This routine is only valid for a cursor that is pointing into a
71531** ordinary table btree. If the cursor points to an index btree or
71532** is invalid, the result of this routine is undefined.
71533*/
71534SQLITE_PRIVATEstatic i64 sqlite3BtreeIntegerKey(BtCursor *pCur){
71535 assert( cursorHoldsMutex(pCur) )((void) (0));
71536 assert( pCur->eState==CURSOR_VALID )((void) (0));
71537 assert( pCur->curIntKey )((void) (0));
71538 getCellInfo(pCur);
71539 return pCur->info.nKey;
71540}
71541
71542/*
71543** Pin or unpin a cursor.
71544*/
71545SQLITE_PRIVATEstatic void sqlite3BtreeCursorPin(BtCursor *pCur){
71546 assert( (pCur->curFlags & BTCF_Pinned)==0 )((void) (0));
71547 pCur->curFlags |= BTCF_Pinned0x40;
71548}
71549SQLITE_PRIVATEstatic void sqlite3BtreeCursorUnpin(BtCursor *pCur){
71550 assert( (pCur->curFlags & BTCF_Pinned)!=0 )((void) (0));
71551 pCur->curFlags &= ~BTCF_Pinned0x40;
71552}
71553
71554#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
71555/*
71556** Return the offset into the database file for the start of the
71557** payload to which the cursor is pointing.
71558*/
71559SQLITE_PRIVATEstatic i64 sqlite3BtreeOffset(BtCursor *pCur){
71560 assert( cursorHoldsMutex(pCur) )((void) (0));
71561 assert( pCur->eState==CURSOR_VALID )((void) (0));
71562 getCellInfo(pCur);
71563 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
71564 (i64)(pCur->info.pPayload - pCur->pPage->aData);
71565}
71566#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
71567
71568/*
71569** Return the number of bytes of payload for the entry that pCur is
71570** currently pointing to. For table btrees, this will be the amount
71571** of data. For index btrees, this will be the size of the key.
71572**
71573** The caller must guarantee that the cursor is pointing to a non-NULL
71574** valid entry. In other words, the calling procedure must guarantee
71575** that the cursor has Cursor.eState==CURSOR_VALID.
71576*/
71577SQLITE_PRIVATEstatic u32 sqlite3BtreePayloadSize(BtCursor *pCur){
71578 assert( cursorHoldsMutex(pCur) )((void) (0));
71579 assert( pCur->eState==CURSOR_VALID )((void) (0));
71580 getCellInfo(pCur);
71581 return pCur->info.nPayload;
71582}
71583
71584/*
71585** Return an upper bound on the size of any record for the table
71586** that the cursor is pointing into.
71587**
71588** This is an optimization. Everything will still work if this
71589** routine always returns 2147483647 (which is the largest record
71590** that SQLite can handle) or more. But returning a smaller value might
71591** prevent large memory allocations when trying to interpret a
71592** corrupt datrabase.
71593**
71594** The current implementation merely returns the size of the underlying
71595** database file.
71596*/
71597SQLITE_PRIVATEstatic sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor *pCur){
71598 assert( cursorHoldsMutex(pCur) )((void) (0));
71599 assert( pCur->eState==CURSOR_VALID )((void) (0));
71600 return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage;
71601}
71602
71603/*
71604** Given the page number of an overflow page in the database (parameter
71605** ovfl), this function finds the page number of the next page in the
71606** linked list of overflow pages. If possible, it uses the auto-vacuum
71607** pointer-map data instead of reading the content of page ovfl to do so.
71608**
71609** If an error occurs an SQLite error code is returned. Otherwise:
71610**
71611** The page number of the next overflow page in the linked list is
71612** written to *pPgnoNext. If page ovfl is the last page in its linked
71613** list, *pPgnoNext is set to zero.
71614**
71615** If ppPage is not NULL, and a reference to the MemPage object corresponding
71616** to page number pOvfl was obtained, then *ppPage is set to point to that
71617** reference. It is the responsibility of the caller to call releasePage()
71618** on *ppPage to free the reference. In no reference was obtained (because
71619** the pointer-map was used to obtain the value for *pPgnoNext), then
71620** *ppPage is set to zero.
71621*/
71622static int getOverflowPage(
71623 BtShared *pBt, /* The database file */
71624 Pgno ovfl, /* Current overflow page number */
71625 MemPage **ppPage, /* OUT: MemPage handle (may be NULL) */
71626 Pgno *pPgnoNext /* OUT: Next overflow page number */
71627){
71628 Pgno next = 0;
71629 MemPage *pPage = 0;
71630 int rc = SQLITE_OK0;
71631
71632 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
71633 assert(pPgnoNext)((void) (0));
71634
71635#ifndef SQLITE_OMIT_AUTOVACUUM
71636 /* Try to find the next page in the overflow list using the
71637 ** autovacuum pointer-map pages. Guess that the next page in
71638 ** the overflow list is page number (ovfl+1). If that guess turns
71639 ** out to be wrong, fall back to loading the data of page
71640 ** number ovfl to determine the next page number.
71641 */
71642 if( pBt->autoVacuum ){
71643 Pgno pgno;
71644 Pgno iGuess = ovfl+1;
71645 u8 eType;
71646
71647 while( PTRMAP_ISPAGE(pBt, iGuess)(ptrmapPageno((pBt), (iGuess))==(iGuess)) || iGuess==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
71648 iGuess++;
71649 }
71650
71651 if( iGuess<=btreePagecount(pBt) ){
71652 rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
71653 if( rc==SQLITE_OK0 && eType==PTRMAP_OVERFLOW24 && pgno==ovfl ){
71654 next = iGuess;
71655 rc = SQLITE_DONE101;
71656 }
71657 }
71658 }
71659#endif
71660
71661 assert( next==0 || rc==SQLITE_DONE )((void) (0));
71662 if( rc==SQLITE_OK0 ){
71663 rc = btreeGetPage(pBt, ovfl, &pPage, (ppPage==0) ? PAGER_GET_READONLY0x02 : 0);
71664 assert( rc==SQLITE_OK || pPage==0 )((void) (0));
71665 if( rc==SQLITE_OK0 ){
71666 next = get4bytesqlite3Get4byte(pPage->aData);
71667 }
71668 }
71669
71670 *pPgnoNext = next;
71671 if( ppPage ){
71672 *ppPage = pPage;
71673 }else{
71674 releasePage(pPage);
71675 }
71676 return (rc==SQLITE_DONE101 ? SQLITE_OK0 : rc);
71677}
71678
71679/*
71680** Copy data from a buffer to a page, or from a page to a buffer.
71681**
71682** pPayload is a pointer to data stored on database page pDbPage.
71683** If argument eOp is false, then nByte bytes of data are copied
71684** from pPayload to the buffer pointed at by pBuf. If eOp is true,
71685** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
71686** of data are copied from the buffer pBuf to pPayload.
71687**
71688** SQLITE_OK is returned on success, otherwise an error code.
71689*/
71690static int copyPayload(
71691 void *pPayload, /* Pointer to page data */
71692 void *pBuf, /* Pointer to buffer */
71693 int nByte, /* Number of bytes to copy */
71694 int eOp, /* 0 -> copy from page, 1 -> copy to page */
71695 DbPage *pDbPage /* Page containing pPayload */
71696){
71697 if( eOp ){
71698 /* Copy data from buffer to page (a write operation) */
71699 int rc = sqlite3PagerWrite(pDbPage);
71700 if( rc!=SQLITE_OK0 ){
71701 return rc;
71702 }
71703 memcpy(pPayload, pBuf, nByte);
71704 }else{
71705 /* Copy data from page to buffer (a read operation) */
71706 memcpy(pBuf, pPayload, nByte);
71707 }
71708 return SQLITE_OK0;
71709}
71710
71711/*
71712** This function is used to read or overwrite payload information
71713** for the entry that the pCur cursor is pointing to. The eOp
71714** argument is interpreted as follows:
71715**
71716** 0: The operation is a read. Populate the overflow cache.
71717** 1: The operation is a write. Populate the overflow cache.
71718**
71719** A total of "amt" bytes are read or written beginning at "offset".
71720** Data is read to or from the buffer pBuf.
71721**
71722** The content being read or written might appear on the main page
71723** or be scattered out on multiple overflow pages.
71724**
71725** If the current cursor entry uses one or more overflow pages
71726** this function may allocate space for and lazily populate
71727** the overflow page-list cache array (BtCursor.aOverflow).
71728** Subsequent calls use this cache to make seeking to the supplied offset
71729** more efficient.
71730**
71731** Once an overflow page-list cache has been allocated, it must be
71732** invalidated if some other cursor writes to the same table, or if
71733** the cursor is moved to a different row. Additionally, in auto-vacuum
71734** mode, the following events may invalidate an overflow page-list cache.
71735**
71736** * An incremental vacuum,
71737** * A commit in auto_vacuum="full" mode,
71738** * Creating a table (may require moving an overflow page).
71739*/
71740static int accessPayload(
71741 BtCursor *pCur, /* Cursor pointing to entry to read from */
71742 u32 offset, /* Begin reading this far into payload */
71743 u32 amt, /* Read this many bytes */
71744 unsigned char *pBuf, /* Write the bytes into this buffer */
71745 int eOp /* zero to read. non-zero to write. */
71746){
71747 unsigned char *aPayload;
71748 int rc = SQLITE_OK0;
71749 int iIdx = 0;
71750 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
71751 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
71752#ifdef SQLITE_DIRECT_OVERFLOW_READ
71753 unsigned char * const pBufStart = pBuf; /* Start of original out buffer */
71754#endif
71755
71756 assert( pPage )((void) (0));
71757 assert( eOp==0 || eOp==1 )((void) (0));
71758 assert( pCur->eState==CURSOR_VALID )((void) (0));
71759 if( pCur->ix>=pPage->nCell ){
71760 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(71760);
71761 }
71762 assert( cursorHoldsMutex(pCur) )((void) (0));
71763
71764 getCellInfo(pCur);
71765 aPayload = pCur->info.pPayload;
71766 assert( offset+amt <= pCur->info.nPayload )((void) (0));
71767
71768 assert( aPayload > pPage->aData )((void) (0));
71769 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
71770 /* Trying to read or write past the end of the data is an error. The
71771 ** conditional above is really:
71772 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
71773 ** but is recast into its current form to avoid integer overflow problems
71774 */
71775 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(71775);
71776 }
71777
71778 /* Check if data must be read/written to/from the btree page itself. */
71779 if( offset<pCur->info.nLocal ){
71780 int a = amt;
71781 if( a+offset>pCur->info.nLocal ){
71782 a = pCur->info.nLocal - offset;
71783 }
71784 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
71785 offset = 0;
71786 pBuf += a;
71787 amt -= a;
71788 }else{
71789 offset -= pCur->info.nLocal;
71790 }
71791
71792
71793 if( rc==SQLITE_OK0 && amt>0 ){
71794 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
71795 Pgno nextPage;
71796
71797 nextPage = get4bytesqlite3Get4byte(&aPayload[pCur->info.nLocal]);
71798
71799 /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
71800 **
71801 ** The aOverflow[] array is sized at one entry for each overflow page
71802 ** in the overflow chain. The page number of the first overflow page is
71803 ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
71804 ** means "not yet known" (the cache is lazily populated).
71805 */
71806 if( (pCur->curFlags & BTCF_ValidOvfl0x04)==0 ){
71807 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
71808 if( pCur->aOverflow==0
71809 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
71810 ){
71811 Pgno *aNew = (Pgno*)sqlite3Realloc(
71812 pCur->aOverflow, nOvfl*2*sizeof(Pgno)
71813 );
71814 if( aNew==0 ){
71815 return SQLITE_NOMEM_BKPT7;
71816 }else{
71817 pCur->aOverflow = aNew;
71818 }
71819 }
71820 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
71821 pCur->curFlags |= BTCF_ValidOvfl0x04;
71822 }else{
71823 /* If the overflow page-list cache has been allocated and the
71824 ** entry for the first required overflow page is valid, skip
71825 ** directly to it.
71826 */
71827 if( pCur->aOverflow[offset/ovflSize] ){
71828 iIdx = (offset/ovflSize);
71829 nextPage = pCur->aOverflow[iIdx];
71830 offset = (offset%ovflSize);
71831 }
71832 }
71833
71834 assert( rc==SQLITE_OK && amt>0 )((void) (0));
71835 while( nextPage ){
71836 /* If required, populate the overflow page-list cache. */
71837 if( nextPage > pBt->nPage ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(71837);
71838 assert( pCur->aOverflow[iIdx]==0((void) (0))
71839 || pCur->aOverflow[iIdx]==nextPage((void) (0))
71840 || CORRUPT_DB )((void) (0));
71841 pCur->aOverflow[iIdx] = nextPage;
71842
71843 if( offset>=ovflSize ){
71844 /* The only reason to read this page is to obtain the page
71845 ** number for the next page in the overflow chain. The page
71846 ** data is not required. So first try to lookup the overflow
71847 ** page-list cache, if any, then fall back to the getOverflowPage()
71848 ** function.
71849 */
71850 assert( pCur->curFlags & BTCF_ValidOvfl )((void) (0));
71851 assert( pCur->pBtree->db==pBt->db )((void) (0));
71852 if( pCur->aOverflow[iIdx+1] ){
71853 nextPage = pCur->aOverflow[iIdx+1];
71854 }else{
71855 rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
71856 }
71857 offset -= ovflSize;
71858 }else{
71859 /* Need to read this page properly. It contains some of the
71860 ** range of data that is being read (eOp==0) or written (eOp!=0).
71861 */
71862 int a = amt;
71863 if( a + offset > ovflSize ){
71864 a = ovflSize - offset;
71865 }
71866
71867#ifdef SQLITE_DIRECT_OVERFLOW_READ
71868 /* If all the following are true:
71869 **
71870 ** 1) this is a read operation, and
71871 ** 2) data is required from the start of this overflow page, and
71872 ** 3) there are no dirty pages in the page-cache
71873 ** 4) the database is file-backed, and
71874 ** 5) the page is not in the WAL file
71875 ** 6) at least 4 bytes have already been read into the output buffer
71876 **
71877 ** then data can be read directly from the database file into the
71878 ** output buffer, bypassing the page-cache altogether. This speeds
71879 ** up loading large records that span many overflow pages.
71880 */
71881 if( eOp==0 /* (1) */
71882 && offset==0 /* (2) */
71883 && sqlite3PagerDirectReadOk(pBt->pPager, nextPage) /* (3,4,5) */
71884 && &pBuf[-4]>=pBufStart /* (6) */
71885 ){
71886 sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
71887 u8 aSave[4];
71888 u8 *aWrite = &pBuf[-4];
71889 assert( aWrite>=pBufStart )((void) (0)); /* due to (6) */
71890 memcpy(aSave, aWrite, 4);
71891 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
71892 if( rc && nextPage>pBt->nPage ) rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(71892);
71893 nextPage = get4bytesqlite3Get4byte(aWrite);
71894 memcpy(aWrite, aSave, 4);
71895 }else
71896#endif
71897
71898 {
71899 DbPage *pDbPage;
71900 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
71901 (eOp==0 ? PAGER_GET_READONLY0x02 : 0)
71902 );
71903 if( rc==SQLITE_OK0 ){
71904 aPayload = sqlite3PagerGetData(pDbPage);
71905 nextPage = get4bytesqlite3Get4byte(aPayload);
71906 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
71907 sqlite3PagerUnref(pDbPage);
71908 offset = 0;
71909 }
71910 }
71911 amt -= a;
71912 if( amt==0 ) return rc;
71913 pBuf += a;
71914 }
71915 if( rc ) break;
71916 iIdx++;
71917 }
71918 }
71919
71920 if( rc==SQLITE_OK0 && amt>0 ){
71921 /* Overflow chain ends prematurely */
71922 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(71922);
71923 }
71924 return rc;
71925}
71926
71927/*
71928** Read part of the payload for the row at which that cursor pCur is currently
71929** pointing. "amt" bytes will be transferred into pBuf[]. The transfer
71930** begins at "offset".
71931**
71932** pCur can be pointing to either a table or an index b-tree.
71933** If pointing to a table btree, then the content section is read. If
71934** pCur is pointing to an index b-tree then the key section is read.
71935**
71936** For sqlite3BtreePayload(), the caller must ensure that pCur is pointing
71937** to a valid row in the table. For sqlite3BtreePayloadChecked(), the
71938** cursor might be invalid or might need to be restored before being read.
71939**
71940** Return SQLITE_OK on success or an error code if anything goes
71941** wrong. An error is returned if "offset+amt" is larger than
71942** the available payload.
71943*/
71944SQLITE_PRIVATEstatic int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
71945 assert( cursorHoldsMutex(pCur) )((void) (0));
71946 assert( pCur->eState==CURSOR_VALID )((void) (0));
71947 assert( pCur->iPage>=0 && pCur->pPage )((void) (0));
71948 return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
71949}
71950
71951/*
71952** This variant of sqlite3BtreePayload() works even if the cursor has not
71953** in the CURSOR_VALID state. It is only used by the sqlite3_blob_read()
71954** interface.
71955*/
71956#ifndef SQLITE_OMIT_INCRBLOB
71957static SQLITE_NOINLINE__attribute__((noinline)) int accessPayloadChecked(
71958 BtCursor *pCur,
71959 u32 offset,
71960 u32 amt,
71961 void *pBuf
71962){
71963 int rc;
71964 if ( pCur->eState==CURSOR_INVALID1 ){
71965 return SQLITE_ABORT4;
71966 }
71967 assert( cursorOwnsBtShared(pCur) )((void) (0));
71968 rc = btreeRestoreCursorPosition(pCur);
71969 return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0);
71970}
71971SQLITE_PRIVATEstatic int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
71972 if( pCur->eState==CURSOR_VALID0 ){
71973 assert( cursorOwnsBtShared(pCur) )((void) (0));
71974 return accessPayload(pCur, offset, amt, pBuf, 0);
71975 }else{
71976 return accessPayloadChecked(pCur, offset, amt, pBuf);
71977 }
71978}
71979#endif /* SQLITE_OMIT_INCRBLOB */
71980
71981/*
71982** Return a pointer to payload information from the entry that the
71983** pCur cursor is pointing to. The pointer is to the beginning of
71984** the key if index btrees (pPage->intKey==0) and is the data for
71985** table btrees (pPage->intKey==1). The number of bytes of available
71986** key/data is written into *pAmt. If *pAmt==0, then the value
71987** returned will not be a valid pointer.
71988**
71989** This routine is an optimization. It is common for the entire key
71990** and data to fit on the local page and for there to be no overflow
71991** pages. When that is so, this routine can be used to access the
71992** key and data without making a copy. If the key and/or data spills
71993** onto overflow pages, then accessPayload() must be used to reassemble
71994** the key/data and copy it into a preallocated buffer.
71995**
71996** The pointer returned by this routine looks directly into the cached
71997** page of the database. The data might change or move the next time
71998** any btree routine is called.
71999*/
72000static const void *fetchPayload(
72001 BtCursor *pCur, /* Cursor pointing to entry to read from */
72002 u32 *pAmt /* Write the number of available bytes here */
72003){
72004 int amt;
72005 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage)((void) (0));
72006 assert( pCur->eState==CURSOR_VALID )((void) (0));
72007 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) )((void) (0));
72008 assert( cursorOwnsBtShared(pCur) )((void) (0));
72009 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB )((void) (0));
72010 assert( pCur->info.nSize>0 )((void) (0));
72011 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB )((void) (0));
72012 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB)((void) (0));
72013 amt = pCur->info.nLocal;
72014 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
72015 /* There is too little space on the page for the expected amount
72016 ** of local content. Database must be corrupt. */
72017 assert( CORRUPT_DB )((void) (0));
72018 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
)))
;
72019 }
72020 *pAmt = (u32)amt;
72021 return (void*)pCur->info.pPayload;
72022}
72023
72024
72025/*
72026** For the entry that cursor pCur is point to, return as
72027** many bytes of the key or data as are available on the local
72028** b-tree page. Write the number of available bytes into *pAmt.
72029**
72030** The pointer returned is ephemeral. The key/data may move
72031** or be destroyed on the next call to any Btree routine,
72032** including calls from other threads against the same cache.
72033** Hence, a mutex on the BtShared should be held prior to calling
72034** this routine.
72035**
72036** These routines is used to get quick access to key and data
72037** in the common case where no overflow pages are used.
72038*/
72039SQLITE_PRIVATEstatic const void *sqlite3BtreePayloadFetch(BtCursor *pCur, u32 *pAmt){
72040 return fetchPayload(pCur, pAmt);
72041}
72042
72043
72044/*
72045** Move the cursor down to a new child page. The newPgno argument is the
72046** page number of the child page to move to.
72047**
72048** This function returns SQLITE_CORRUPT if the page-header flags field of
72049** the new child page does not match the flags field of the parent (i.e.
72050** if an intkey page appears to be the parent of a non-intkey page, or
72051** vice-versa).
72052*/
72053static int moveToChild(BtCursor *pCur, u32 newPgno){
72054 BtShared *pBt = pCur->pBt;
72055
72056 assert( cursorOwnsBtShared(pCur) )((void) (0));
72057 assert( pCur->eState==CURSOR_VALID )((void) (0));
72058 assert( pCur->iPage<BTCURSOR_MAX_DEPTH )((void) (0));
72059 assert( pCur->iPage>=0 )((void) (0));
72060 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH20-1) ){
72061 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(72061);
72062 }
72063 pCur->info.nSize = 0;
72064 pCur->curFlags &= ~(BTCF_ValidNKey0x02|BTCF_ValidOvfl0x04);
72065 pCur->aiIdx[pCur->iPage] = pCur->ix;
72066 pCur->apPage[pCur->iPage] = pCur->pPage;
72067 pCur->ix = 0;
72068 pCur->iPage++;
72069 return getAndInitPage(pBt, newPgno, &pCur->pPage, pCur, pCur->curPagerFlags);
72070}
72071
72072#ifdef SQLITE_DEBUG
72073/*
72074** Page pParent is an internal (non-leaf) tree page. This function
72075** asserts that page number iChild is the left-child if the iIdx'th
72076** cell in page pParent. Or, if iIdx is equal to the total number of
72077** cells in pParent, that page number iChild is the right-child of
72078** the page.
72079*/
72080static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
72081 if( CORRUPT_DB(sqlite3Config.neverCorrupt==0) ) return; /* The conditions tested below might not be true
72082 ** in a corrupt database */
72083 assert( iIdx<=pParent->nCell )((void) (0));
72084 if( iIdx==pParent->nCell ){
72085 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild )((void) (0));
72086 }else{
72087 assert( get4byte(findCell(pParent, iIdx))==iChild )((void) (0));
72088 }
72089}
72090#else
72091# define assertParentIndex(x,y,z)
72092#endif
72093
72094/*
72095** Move the cursor up to the parent page.
72096**
72097** pCur->idx is set to the cell index that contains the pointer
72098** to the page we are coming from. If we are coming from the
72099** right-most child page then pCur->idx is set to one more than
72100** the largest cell index.
72101*/
72102static void moveToParent(BtCursor *pCur){
72103 MemPage *pLeaf;
72104 assert( cursorOwnsBtShared(pCur) )((void) (0));
72105 assert( pCur->eState==CURSOR_VALID )((void) (0));
72106 assert( pCur->iPage>0 )((void) (0));
72107 assert( pCur->pPage )((void) (0));
72108 assertParentIndex(
72109 pCur->apPage[pCur->iPage-1],
72110 pCur->aiIdx[pCur->iPage-1],
72111 pCur->pPage->pgno
72112 );
72113 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
72114 pCur->info.nSize = 0;
72115 pCur->curFlags &= ~(BTCF_ValidNKey0x02|BTCF_ValidOvfl0x04);
72116 pCur->ix = pCur->aiIdx[pCur->iPage-1];
72117 pLeaf = pCur->pPage;
72118 pCur->pPage = pCur->apPage[--pCur->iPage];
72119 releasePageNotNull(pLeaf);
72120}
72121
72122/*
72123** Move the cursor to point to the root page of its b-tree structure.
72124**
72125** If the table has a virtual root page, then the cursor is moved to point
72126** to the virtual root page instead of the actual root page. A table has a
72127** virtual root page when the actual root page contains no cells and a
72128** single child page. This can only happen with the table rooted at page 1.
72129**
72130** If the b-tree structure is empty, the cursor state is set to
72131** CURSOR_INVALID and this routine returns SQLITE_EMPTY. Otherwise,
72132** the cursor is set to point to the first cell located on the root
72133** (or virtual root) page and the cursor state is set to CURSOR_VALID.
72134**
72135** If this function returns successfully, it may be assumed that the
72136** page-header flags indicate that the [virtual] root-page is the expected
72137** kind of b-tree page (i.e. if when opening the cursor the caller did not
72138** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
72139** indicating a table b-tree, or if the caller did specify a KeyInfo
72140** structure the flags byte is set to 0x02 or 0x0A, indicating an index
72141** b-tree).
72142*/
72143static int moveToRoot(BtCursor *pCur){
72144 MemPage *pRoot;
72145 int rc = SQLITE_OK0;
72146
72147 assert( cursorOwnsBtShared(pCur) )((void) (0));
72148 assert( CURSOR_INVALID < CURSOR_REQUIRESEEK )((void) (0));
72149 assert( CURSOR_VALID < CURSOR_REQUIRESEEK )((void) (0));
72150 assert( CURSOR_FAULT > CURSOR_REQUIRESEEK )((void) (0));
72151 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 )((void) (0));
72152 assert( pCur->pgnoRoot>0 || pCur->iPage<0 )((void) (0));
72153
72154 if( pCur->iPage>=0 ){
72155 if( pCur->iPage ){
72156 releasePageNotNull(pCur->pPage);
72157 while( --pCur->iPage ){
72158 releasePageNotNull(pCur->apPage[pCur->iPage]);
72159 }
72160 pRoot = pCur->pPage = pCur->apPage[0];
72161 goto skip_init;
72162 }
72163 }else if( pCur->pgnoRoot==0 ){
72164 pCur->eState = CURSOR_INVALID1;
72165 return SQLITE_EMPTY16;
72166 }else{
72167 assert( pCur->iPage==(-1) )((void) (0));
72168 if( pCur->eState>=CURSOR_REQUIRESEEK3 ){
72169 if( pCur->eState==CURSOR_FAULT4 ){
72170 assert( pCur->skipNext!=SQLITE_OK )((void) (0));
72171 return pCur->skipNext;
72172 }
72173 sqlite3BtreeClearCursor(pCur);
72174 }
72175 rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->pPage,
72176 0, pCur->curPagerFlags);
72177 if( rc!=SQLITE_OK0 ){
72178 pCur->eState = CURSOR_INVALID1;
72179 return rc;
72180 }
72181 pCur->iPage = 0;
72182 pCur->curIntKey = pCur->pPage->intKey;
72183 }
72184 pRoot = pCur->pPage;
72185 assert( pRoot->pgno==pCur->pgnoRoot || CORRUPT_DB )((void) (0));
72186
72187 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
72188 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
72189 ** NULL, the caller expects a table b-tree. If this is not the case,
72190 ** return an SQLITE_CORRUPT error.
72191 **
72192 ** Earlier versions of SQLite assumed that this test could not fail
72193 ** if the root page was already loaded when this function was called (i.e.
72194 ** if pCur->iPage>=0). But this is not so if the database is corrupted
72195 ** in such a way that page pRoot is linked into a second b-tree table
72196 ** (or the freelist). */
72197 assert( pRoot->intKey==1 || pRoot->intKey==0 )((void) (0));
72198 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
72199 return SQLITE_CORRUPT_PAGE(pCur->pPage)sqlite3CorruptError(72199);
72200 }
72201
72202skip_init:
72203 pCur->ix = 0;
72204 pCur->info.nSize = 0;
72205 pCur->curFlags &= ~(BTCF_AtLast0x08|BTCF_ValidNKey0x02|BTCF_ValidOvfl0x04);
72206
72207 if( pRoot->nCell>0 ){
72208 pCur->eState = CURSOR_VALID0;
72209 }else if( !pRoot->leaf ){
72210 Pgno subpage;
72211 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(72211);
72212 subpage = get4bytesqlite3Get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
72213 pCur->eState = CURSOR_VALID0;
72214 rc = moveToChild(pCur, subpage);
72215 }else{
72216 pCur->eState = CURSOR_INVALID1;
72217 rc = SQLITE_EMPTY16;
72218 }
72219 return rc;
72220}
72221
72222/*
72223** Move the cursor down to the left-most leaf entry beneath the
72224** entry to which it is currently pointing.
72225**
72226** The left-most leaf is the one with the smallest key - the first
72227** in ascending order.
72228*/
72229static int moveToLeftmost(BtCursor *pCur){
72230 Pgno pgno;
72231 int rc = SQLITE_OK0;
72232 MemPage *pPage;
72233
72234 assert( cursorOwnsBtShared(pCur) )((void) (0));
72235 assert( pCur->eState==CURSOR_VALID )((void) (0));
72236 while( rc==SQLITE_OK0 && !(pPage = pCur->pPage)->leaf ){
72237 assert( pCur->ix<pPage->nCell )((void) (0));
72238 pgno = get4bytesqlite3Get4byte(findCell(pPage, pCur->ix)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(pCur->ix)])[0]<<8 | (&(pPage)->
aCellIdx[2*(pCur->ix)])[1])))
);
72239 rc = moveToChild(pCur, pgno);
72240 }
72241 return rc;
72242}
72243
72244/*
72245** Move the cursor down to the right-most leaf entry beneath the
72246** page to which it is currently pointing. Notice the difference
72247** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
72248** finds the left-most entry beneath the *entry* whereas moveToRightmost()
72249** finds the right-most entry beneath the *page*.
72250**
72251** The right-most entry is the one with the largest key - the last
72252** key in ascending order.
72253*/
72254static int moveToRightmost(BtCursor *pCur){
72255 Pgno pgno;
72256 int rc = SQLITE_OK0;
72257 MemPage *pPage = 0;
72258
72259 assert( cursorOwnsBtShared(pCur) )((void) (0));
72260 assert( pCur->eState==CURSOR_VALID )((void) (0));
72261 while( !(pPage = pCur->pPage)->leaf ){
72262 pgno = get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]);
72263 pCur->ix = pPage->nCell;
72264 rc = moveToChild(pCur, pgno);
72265 if( rc ) return rc;
72266 }
72267 pCur->ix = pPage->nCell-1;
72268 assert( pCur->info.nSize==0 )((void) (0));
72269 assert( (pCur->curFlags & BTCF_ValidNKey)==0 )((void) (0));
72270 return SQLITE_OK0;
72271}
72272
72273/* Move the cursor to the first entry in the table. Return SQLITE_OK
72274** on success. Set *pRes to 0 if the cursor actually points to something
72275** or set *pRes to 1 if the table is empty.
72276*/
72277SQLITE_PRIVATEstatic int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
72278 int rc;
72279
72280 assert( cursorOwnsBtShared(pCur) )((void) (0));
72281 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) )((void) (0));
72282 rc = moveToRoot(pCur);
72283 if( rc==SQLITE_OK0 ){
72284 assert( pCur->pPage->nCell>0 )((void) (0));
72285 *pRes = 0;
72286 rc = moveToLeftmost(pCur);
72287 }else if( rc==SQLITE_EMPTY16 ){
72288 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 )((void) (0));
72289 *pRes = 1;
72290 rc = SQLITE_OK0;
72291 }
72292 return rc;
72293}
72294
72295/* Move the cursor to the last entry in the table. Return SQLITE_OK
72296** on success. Set *pRes to 0 if the cursor actually points to something
72297** or set *pRes to 1 if the table is empty.
72298*/
72299SQLITE_PRIVATEstatic int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
72300 int rc;
72301
72302 assert( cursorOwnsBtShared(pCur) )((void) (0));
72303 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) )((void) (0));
72304
72305 /* If the cursor already points to the last entry, this is a no-op. */
72306 if( CURSOR_VALID0==pCur->eState && (pCur->curFlags & BTCF_AtLast0x08)!=0 ){
72307#ifdef SQLITE_DEBUG
72308 /* This block serves to assert() that the cursor really does point
72309 ** to the last entry in the b-tree. */
72310 int ii;
72311 for(ii=0; ii<pCur->iPage; ii++){
72312 assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell )((void) (0));
72313 }
72314 assert( pCur->ix==pCur->pPage->nCell-1 || CORRUPT_DB )((void) (0));
72315 testcase( pCur->ix!=pCur->pPage->nCell-1 );
72316 /* ^-- dbsqlfuzz b92b72e4de80b5140c30ab71372ca719b8feb618 */
72317 assert( pCur->pPage->leaf )((void) (0));
72318#endif
72319 *pRes = 0;
72320 return SQLITE_OK0;
72321 }
72322
72323 rc = moveToRoot(pCur);
72324 if( rc==SQLITE_OK0 ){
72325 assert( pCur->eState==CURSOR_VALID )((void) (0));
72326 *pRes = 0;
72327 rc = moveToRightmost(pCur);
72328 if( rc==SQLITE_OK0 ){
72329 pCur->curFlags |= BTCF_AtLast0x08;
72330 }else{
72331 pCur->curFlags &= ~BTCF_AtLast0x08;
72332 }
72333 }else if( rc==SQLITE_EMPTY16 ){
72334 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 )((void) (0));
72335 *pRes = 1;
72336 rc = SQLITE_OK0;
72337 }
72338 return rc;
72339}
72340
72341/* Move the cursor so that it points to an entry in a table (a.k.a INTKEY)
72342** table near the key intKey. Return a success code.
72343**
72344** If an exact match is not found, then the cursor is always
72345** left pointing at a leaf page which would hold the entry if it
72346** were present. The cursor might point to an entry that comes
72347** before or after the key.
72348**
72349** An integer is written into *pRes which is the result of
72350** comparing the key with the entry to which the cursor is
72351** pointing. The meaning of the integer written into
72352** *pRes is as follows:
72353**
72354** *pRes<0 The cursor is left pointing at an entry that
72355** is smaller than intKey or if the table is empty
72356** and the cursor is therefore left point to nothing.
72357**
72358** *pRes==0 The cursor is left pointing at an entry that
72359** exactly matches intKey.
72360**
72361** *pRes>0 The cursor is left pointing at an entry that
72362** is larger than intKey.
72363*/
72364SQLITE_PRIVATEstatic int sqlite3BtreeTableMoveto(
72365 BtCursor *pCur, /* The cursor to be moved */
72366 i64 intKey, /* The table key */
72367 int biasRight, /* If true, bias the search to the high end */
72368 int *pRes /* Write search results here */
72369){
72370 int rc;
72371
72372 assert( cursorOwnsBtShared(pCur) )((void) (0));
72373 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) )((void) (0));
72374 assert( pRes )((void) (0));
72375 assert( pCur->pKeyInfo==0 )((void) (0));
72376 assert( pCur->eState!=CURSOR_VALID || pCur->curIntKey!=0 )((void) (0));
72377
72378 /* If the cursor is already positioned at the point we are trying
72379 ** to move to, then just return without doing any work */
72380 if( pCur->eState==CURSOR_VALID0 && (pCur->curFlags & BTCF_ValidNKey0x02)!=0 ){
72381 if( pCur->info.nKey==intKey ){
72382 *pRes = 0;
72383 return SQLITE_OK0;
72384 }
72385 if( pCur->info.nKey<intKey ){
72386 if( (pCur->curFlags & BTCF_AtLast0x08)!=0 ){
72387 *pRes = -1;
72388 return SQLITE_OK0;
72389 }
72390 /* If the requested key is one more than the previous key, then
72391 ** try to get there using sqlite3BtreeNext() rather than a full
72392 ** binary search. This is an optimization only. The correct answer
72393 ** is still obtained without this case, only a little more slowely */
72394 if( pCur->info.nKey+1==intKey ){
72395 *pRes = 0;
72396 rc = sqlite3BtreeNext(pCur, 0);
72397 if( rc==SQLITE_OK0 ){
72398 getCellInfo(pCur);
72399 if( pCur->info.nKey==intKey ){
72400 return SQLITE_OK0;
72401 }
72402 }else if( rc!=SQLITE_DONE101 ){
72403 return rc;
72404 }
72405 }
72406 }
72407 }
72408
72409#ifdef SQLITE_DEBUG
72410 pCur->pBtree->nSeek++; /* Performance measurement during testing */
72411#endif
72412
72413 rc = moveToRoot(pCur);
72414 if( rc ){
72415 if( rc==SQLITE_EMPTY16 ){
72416 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 )((void) (0));
72417 *pRes = -1;
72418 return SQLITE_OK0;
72419 }
72420 return rc;
72421 }
72422 assert( pCur->pPage )((void) (0));
72423 assert( pCur->pPage->isInit )((void) (0));
72424 assert( pCur->eState==CURSOR_VALID )((void) (0));
72425 assert( pCur->pPage->nCell > 0 )((void) (0));
72426 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey )((void) (0));
72427 assert( pCur->curIntKey )((void) (0));
72428
72429 for(;;){
72430 int lwr, upr, idx, c;
72431 Pgno chldPg;
72432 MemPage *pPage = pCur->pPage;
72433 u8 *pCell; /* Pointer to current cell in pPage */
72434
72435 /* pPage->nCell must be greater than zero. If this is the root-page
72436 ** the cursor would have been INVALID above and this for(;;) loop
72437 ** not run. If this is not the root-page, then the moveToChild() routine
72438 ** would have already detected db corruption. Similarly, pPage must
72439 ** be the right kind (index or table) of b-tree page. Otherwise
72440 ** a moveToChild() or moveToRoot() call would have detected corruption. */
72441 assert( pPage->nCell>0 )((void) (0));
72442 assert( pPage->intKey )((void) (0));
72443 lwr = 0;
72444 upr = pPage->nCell-1;
72445 assert( biasRight==0 || biasRight==1 )((void) (0));
72446 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
72447 for(;;){
72448 i64 nCellKey;
72449 pCell = findCellPastPtr(pPage, idx)((pPage)->aDataOfst + ((pPage)->maskPage & ((&(
pPage)->aCellIdx[2*(idx)])[0]<<8 | (&(pPage)->
aCellIdx[2*(idx)])[1])))
;
72450 if( pPage->intKeyLeaf ){
72451 while( 0x80 <= *(pCell++) ){
72452 if( pCell>=pPage->aDataEnd ){
72453 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72453);
72454 }
72455 }
72456 }
72457 getVarintsqlite3GetVarint(pCell, (u64*)&nCellKey);
72458 if( nCellKey<intKey ){
72459 lwr = idx+1;
72460 if( lwr>upr ){ c = -1; break; }
72461 }else if( nCellKey>intKey ){
72462 upr = idx-1;
72463 if( lwr>upr ){ c = +1; break; }
72464 }else{
72465 assert( nCellKey==intKey )((void) (0));
72466 pCur->ix = (u16)idx;
72467 if( !pPage->leaf ){
72468 lwr = idx;
72469 goto moveto_table_next_layer;
72470 }else{
72471 pCur->curFlags |= BTCF_ValidNKey0x02;
72472 pCur->info.nKey = nCellKey;
72473 pCur->info.nSize = 0;
72474 *pRes = 0;
72475 return SQLITE_OK0;
72476 }
72477 }
72478 assert( lwr+upr>=0 )((void) (0));
72479 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */
72480 }
72481 assert( lwr==upr+1 || !pPage->leaf )((void) (0));
72482 assert( pPage->isInit )((void) (0));
72483 if( pPage->leaf ){
72484 assert( pCur->ix<pCur->pPage->nCell )((void) (0));
72485 pCur->ix = (u16)idx;
72486 *pRes = c;
72487 rc = SQLITE_OK0;
72488 goto moveto_table_finish;
72489 }
72490moveto_table_next_layer:
72491 if( lwr>=pPage->nCell ){
72492 chldPg = get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]);
72493 }else{
72494 chldPg = get4bytesqlite3Get4byte(findCell(pPage, lwr)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(lwr)])[0]<<8 | (&(pPage)->aCellIdx
[2*(lwr)])[1])))
);
72495 }
72496 pCur->ix = (u16)lwr;
72497 rc = moveToChild(pCur, chldPg);
72498 if( rc ) break;
72499 }
72500moveto_table_finish:
72501 pCur->info.nSize = 0;
72502 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 )((void) (0));
72503 return rc;
72504}
72505
72506/*
72507** Compare the "idx"-th cell on the page the cursor pCur is currently
72508** pointing to to pIdxKey using xRecordCompare. Return negative or
72509** zero if the cell is less than or equal pIdxKey. Return positive
72510** if unknown.
72511**
72512** Return value negative: Cell at pCur[idx] less than pIdxKey
72513**
72514** Return value is zero: Cell at pCur[idx] equals pIdxKey
72515**
72516** Return value positive: Nothing is known about the relationship
72517** of the cell at pCur[idx] and pIdxKey.
72518**
72519** This routine is part of an optimization. It is always safe to return
72520** a positive value as that will cause the optimization to be skipped.
72521*/
72522static int indexCellCompare(
72523 BtCursor *pCur,
72524 int idx,
72525 UnpackedRecord *pIdxKey,
72526 RecordCompare xRecordCompare
72527){
72528 MemPage *pPage = pCur->pPage;
72529 int c;
72530 int nCell; /* Size of the pCell cell in bytes */
72531 u8 *pCell = findCellPastPtr(pPage, idx)((pPage)->aDataOfst + ((pPage)->maskPage & ((&(
pPage)->aCellIdx[2*(idx)])[0]<<8 | (&(pPage)->
aCellIdx[2*(idx)])[1])))
;
72532
72533 nCell = pCell[0];
72534 if( nCell<=pPage->max1bytePayload ){
72535 /* This branch runs if the record-size field of the cell is a
72536 ** single byte varint and the record fits entirely on the main
72537 ** b-tree page. */
72538 testcase( pCell+nCell+1==pPage->aDataEnd );
72539 c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
72540 }else if( !(pCell[1] & 0x80)
72541 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
72542 ){
72543 /* The record-size field is a 2 byte varint and the record
72544 ** fits entirely on the main b-tree page. */
72545 testcase( pCell+nCell+2==pPage->aDataEnd );
72546 c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
72547 }else{
72548 /* If the record extends into overflow pages, do not attempt
72549 ** the optimization. */
72550 c = 99;
72551 }
72552 return c;
72553}
72554
72555/*
72556** Return true (non-zero) if pCur is current pointing to the last
72557** page of a table.
72558*/
72559static int cursorOnLastPage(BtCursor *pCur){
72560 int i;
72561 assert( pCur->eState==CURSOR_VALID )((void) (0));
72562 for(i=0; i<pCur->iPage; i++){
72563 MemPage *pPage = pCur->apPage[i];
72564 if( pCur->aiIdx[i]<pPage->nCell ) return 0;
72565 }
72566 return 1;
72567}
72568
72569/* Move the cursor so that it points to an entry in an index table
72570** near the key pIdxKey. Return a success code.
72571**
72572** If an exact match is not found, then the cursor is always
72573** left pointing at a leaf page which would hold the entry if it
72574** were present. The cursor might point to an entry that comes
72575** before or after the key.
72576**
72577** An integer is written into *pRes which is the result of
72578** comparing the key with the entry to which the cursor is
72579** pointing. The meaning of the integer written into
72580** *pRes is as follows:
72581**
72582** *pRes<0 The cursor is left pointing at an entry that
72583** is smaller than pIdxKey or if the table is empty
72584** and the cursor is therefore left point to nothing.
72585**
72586** *pRes==0 The cursor is left pointing at an entry that
72587** exactly matches pIdxKey.
72588**
72589** *pRes>0 The cursor is left pointing at an entry that
72590** is larger than pIdxKey.
72591**
72592** The pIdxKey->eqSeen field is set to 1 if there
72593** exists an entry in the table that exactly matches pIdxKey.
72594*/
72595SQLITE_PRIVATEstatic int sqlite3BtreeIndexMoveto(
72596 BtCursor *pCur, /* The cursor to be moved */
72597 UnpackedRecord *pIdxKey, /* Unpacked index key */
72598 int *pRes /* Write search results here */
72599){
72600 int rc;
72601 RecordCompare xRecordCompare;
72602
72603 assert( cursorOwnsBtShared(pCur) )((void) (0));
72604 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) )((void) (0));
72605 assert( pRes )((void) (0));
72606 assert( pCur->pKeyInfo!=0 )((void) (0));
72607
72608#ifdef SQLITE_DEBUG
72609 pCur->pBtree->nSeek++; /* Performance measurement during testing */
72610#endif
72611
72612 xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
72613 pIdxKey->errCode = 0;
72614 assert( pIdxKey->default_rc==1((void) (0))
72615 || pIdxKey->default_rc==0((void) (0))
72616 || pIdxKey->default_rc==-1((void) (0))
72617 )((void) (0));
72618
72619
72620 /* Check to see if we can skip a lot of work. Two cases:
72621 **
72622 ** (1) If the cursor is already pointing to the very last cell
72623 ** in the table and the pIdxKey search key is greater than or
72624 ** equal to that last cell, then no movement is required.
72625 **
72626 ** (2) If the cursor is on the last page of the table and the first
72627 ** cell on that last page is less than or equal to the pIdxKey
72628 ** search key, then we can start the search on the current page
72629 ** without needing to go back to root.
72630 */
72631 if( pCur->eState==CURSOR_VALID0
72632 && pCur->pPage->leaf
72633 && cursorOnLastPage(pCur)
72634 ){
72635 int c;
72636 if( pCur->ix==pCur->pPage->nCell-1
72637 && (c = indexCellCompare(pCur, pCur->ix, pIdxKey, xRecordCompare))<=0
72638 && pIdxKey->errCode==SQLITE_OK0
72639 ){
72640 *pRes = c;
72641 return SQLITE_OK0; /* Cursor already pointing at the correct spot */
72642 }
72643 if( pCur->iPage>0
72644 && indexCellCompare(pCur, 0, pIdxKey, xRecordCompare)<=0
72645 && pIdxKey->errCode==SQLITE_OK0
72646 ){
72647 pCur->curFlags &= ~BTCF_ValidOvfl0x04;
72648 if( !pCur->pPage->isInit ){
72649 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(72649);
72650 }
72651 goto bypass_moveto_root; /* Start search on the current page */
72652 }
72653 pIdxKey->errCode = SQLITE_OK0;
72654 }
72655
72656 rc = moveToRoot(pCur);
72657 if( rc ){
72658 if( rc==SQLITE_EMPTY16 ){
72659 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 )((void) (0));
72660 *pRes = -1;
72661 return SQLITE_OK0;
72662 }
72663 return rc;
72664 }
72665
72666bypass_moveto_root:
72667 assert( pCur->pPage )((void) (0));
72668 assert( pCur->pPage->isInit )((void) (0));
72669 assert( pCur->eState==CURSOR_VALID )((void) (0));
72670 assert( pCur->pPage->nCell > 0 )((void) (0));
72671 assert( pCur->curIntKey==0 )((void) (0));
72672 assert( pIdxKey!=0 )((void) (0));
72673 for(;;){
72674 int lwr, upr, idx, c;
72675 Pgno chldPg;
72676 MemPage *pPage = pCur->pPage;
72677 u8 *pCell; /* Pointer to current cell in pPage */
72678
72679 /* pPage->nCell must be greater than zero. If this is the root-page
72680 ** the cursor would have been INVALID above and this for(;;) loop
72681 ** not run. If this is not the root-page, then the moveToChild() routine
72682 ** would have already detected db corruption. Similarly, pPage must
72683 ** be the right kind (index or table) of b-tree page. Otherwise
72684 ** a moveToChild() or moveToRoot() call would have detected corruption. */
72685 assert( pPage->nCell>0 )((void) (0));
72686 assert( pPage->intKey==0 )((void) (0));
72687 lwr = 0;
72688 upr = pPage->nCell-1;
72689 idx = upr>>1; /* idx = (lwr+upr)/2; */
72690 for(;;){
72691 int nCell; /* Size of the pCell cell in bytes */
72692 pCell = findCellPastPtr(pPage, idx)((pPage)->aDataOfst + ((pPage)->maskPage & ((&(
pPage)->aCellIdx[2*(idx)])[0]<<8 | (&(pPage)->
aCellIdx[2*(idx)])[1])))
;
72693
72694 /* The maximum supported page-size is 65536 bytes. This means that
72695 ** the maximum number of record bytes stored on an index B-Tree
72696 ** page is less than 16384 bytes and may be stored as a 2-byte
72697 ** varint. This information is used to attempt to avoid parsing
72698 ** the entire cell by checking for the cases where the record is
72699 ** stored entirely within the b-tree page by inspecting the first
72700 ** 2 bytes of the cell.
72701 */
72702 nCell = pCell[0];
72703 if( nCell<=pPage->max1bytePayload ){
72704 /* This branch runs if the record-size field of the cell is a
72705 ** single byte varint and the record fits entirely on the main
72706 ** b-tree page. */
72707 testcase( pCell+nCell+1==pPage->aDataEnd );
72708 c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
72709 }else if( !(pCell[1] & 0x80)
72710 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
72711 ){
72712 /* The record-size field is a 2 byte varint and the record
72713 ** fits entirely on the main b-tree page. */
72714 testcase( pCell+nCell+2==pPage->aDataEnd );
72715 c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
72716 }else{
72717 /* The record flows over onto one or more overflow pages. In
72718 ** this case the whole cell needs to be parsed, a buffer allocated
72719 ** and accessPayload() used to retrieve the record into the
72720 ** buffer before VdbeRecordCompare() can be called.
72721 **
72722 ** If the record is corrupt, the xRecordCompare routine may read
72723 ** up to two varints past the end of the buffer. An extra 18
72724 ** bytes of padding is allocated at the end of the buffer in
72725 ** case this happens. */
72726 void *pCellKey;
72727 u8 * const pCellBody = pCell - pPage->childPtrSize;
72728 const int nOverrun = 18; /* Size of the overrun padding */
72729 pPage->xParseCell(pPage, pCellBody, &pCur->info);
72730 nCell = (int)pCur->info.nKey;
72731 testcase( nCell<0 ); /* True if key size is 2^32 or more */
72732 testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */
72733 testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */
72734 testcase( nCell==2 ); /* Minimum legal index key size */
72735 if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){
72736 rc = SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72736);
72737 goto moveto_index_finish;
72738 }
72739 pCellKey = sqlite3Malloc( nCell+nOverrun );
72740 if( pCellKey==0 ){
72741 rc = SQLITE_NOMEM_BKPT7;
72742 goto moveto_index_finish;
72743 }
72744 pCur->ix = (u16)idx;
72745 rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
72746 memset(((u8*)pCellKey)+nCell,0,nOverrun); /* Fix uninit warnings */
72747 pCur->curFlags &= ~BTCF_ValidOvfl0x04;
72748 if( rc ){
72749 sqlite3_free(pCellKey);
72750 goto moveto_index_finish;
72751 }
72752 c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
72753 sqlite3_free(pCellKey);
72754 }
72755 assert(((void) (0))
72756 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)((void) (0))
72757 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)((void) (0))
72758 )((void) (0));
72759 if( c<0 ){
72760 lwr = idx+1;
72761 }else if( c>0 ){
72762 upr = idx-1;
72763 }else{
72764 assert( c==0 )((void) (0));
72765 *pRes = 0;
72766 rc = SQLITE_OK0;
72767 pCur->ix = (u16)idx;
72768 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(72768);
72769 goto moveto_index_finish;
72770 }
72771 if( lwr>upr ) break;
72772 assert( lwr+upr>=0 )((void) (0));
72773 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2 */
72774 }
72775 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) )((void) (0));
72776 assert( pPage->isInit )((void) (0));
72777 if( pPage->leaf ){
72778 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB )((void) (0));
72779 pCur->ix = (u16)idx;
72780 *pRes = c;
72781 rc = SQLITE_OK0;
72782 goto moveto_index_finish;
72783 }
72784 if( lwr>=pPage->nCell ){
72785 chldPg = get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]);
72786 }else{
72787 chldPg = get4bytesqlite3Get4byte(findCell(pPage, lwr)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(lwr)])[0]<<8 | (&(pPage)->aCellIdx
[2*(lwr)])[1])))
);
72788 }
72789 pCur->ix = (u16)lwr;
72790 rc = moveToChild(pCur, chldPg);
72791 if( rc ) break;
72792 }
72793moveto_index_finish:
72794 pCur->info.nSize = 0;
72795 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 )((void) (0));
72796 return rc;
72797}
72798
72799
72800/*
72801** Return TRUE if the cursor is not pointing at an entry of the table.
72802**
72803** TRUE will be returned after a call to sqlite3BtreeNext() moves
72804** past the last entry in the table or sqlite3BtreePrev() moves past
72805** the first entry. TRUE is also returned if the table is empty.
72806*/
72807SQLITE_PRIVATEstatic int sqlite3BtreeEof(BtCursor *pCur){
72808 /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
72809 ** have been deleted? This API will need to change to return an error code
72810 ** as well as the boolean result value.
72811 */
72812 return (CURSOR_VALID0!=pCur->eState);
72813}
72814
72815/*
72816** Return an estimate for the number of rows in the table that pCur is
72817** pointing to. Return a negative number if no estimate is currently
72818** available.
72819*/
72820SQLITE_PRIVATEstatic i64 sqlite3BtreeRowCountEst(BtCursor *pCur){
72821 i64 n;
72822 u8 i;
72823
72824 assert( cursorOwnsBtShared(pCur) )((void) (0));
72825 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) )((void) (0));
72826
72827 /* Currently this interface is only called by the OP_IfSmaller
72828 ** opcode, and it that case the cursor will always be valid and
72829 ** will always point to a leaf node. */
72830 if( NEVER(pCur->eState!=CURSOR_VALID)(pCur->eState!=0) ) return -1;
72831 if( NEVER(pCur->pPage->leaf==0)(pCur->pPage->leaf==0) ) return -1;
72832
72833 n = pCur->pPage->nCell;
72834 for(i=0; i<pCur->iPage; i++){
72835 n *= pCur->apPage[i]->nCell;
72836 }
72837 return n;
72838}
72839
72840/*
72841** Advance the cursor to the next entry in the database.
72842** Return value:
72843**
72844** SQLITE_OK success
72845** SQLITE_DONE cursor is already pointing at the last element
72846** otherwise some kind of error occurred
72847**
72848** The main entry point is sqlite3BtreeNext(). That routine is optimized
72849** for the common case of merely incrementing the cell counter BtCursor.aiIdx
72850** to the next cell on the current page. The (slower) btreeNext() helper
72851** routine is called when it is necessary to move to a different page or
72852** to restore the cursor.
72853**
72854** If bit 0x01 of the F argument in sqlite3BtreeNext(C,F) is 1, then the
72855** cursor corresponds to an SQL index and this routine could have been
72856** skipped if the SQL index had been a unique index. The F argument
72857** is a hint to the implement. SQLite btree implementation does not use
72858** this hint, but COMDB2 does.
72859*/
72860static SQLITE_NOINLINE__attribute__((noinline)) int btreeNext(BtCursor *pCur){
72861 int rc;
72862 int idx;
72863 MemPage *pPage;
72864
72865 assert( cursorOwnsBtShared(pCur) )((void) (0));
72866 if( pCur->eState!=CURSOR_VALID0 ){
72867 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 )((void) (0));
72868 rc = restoreCursorPosition(pCur)(pCur->eState>=3 ? btreeRestoreCursorPosition(pCur) : 0
)
;
72869 if( rc!=SQLITE_OK0 ){
72870 return rc;
72871 }
72872 if( CURSOR_INVALID1==pCur->eState ){
72873 return SQLITE_DONE101;
72874 }
72875 if( pCur->eState==CURSOR_SKIPNEXT2 ){
72876 pCur->eState = CURSOR_VALID0;
72877 if( pCur->skipNext>0 ) return SQLITE_OK0;
72878 }
72879 }
72880
72881 pPage = pCur->pPage;
72882 idx = ++pCur->ix;
72883 if( !pPage->isInit || sqlite3FaultSim(412) ){
72884 /* The only known way for this to happen is for there to be a
72885 ** recursive SQL function that does a DELETE operation as part of a
72886 ** SELECT which deletes content out from under an active cursor
72887 ** in a corrupt database file where the table being DELETE-ed from
72888 ** has pages in common with the table being queried. See TH3
72889 ** module cov1/btree78.test testcase 220 (2018-06-08) for an
72890 ** example. */
72891 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(72891);
72892 }
72893
72894 if( idx>=pPage->nCell ){
72895 if( !pPage->leaf ){
72896 rc = moveToChild(pCur, get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]));
72897 if( rc ) return rc;
72898 return moveToLeftmost(pCur);
72899 }
72900 do{
72901 if( pCur->iPage==0 ){
72902 pCur->eState = CURSOR_INVALID1;
72903 return SQLITE_DONE101;
72904 }
72905 moveToParent(pCur);
72906 pPage = pCur->pPage;
72907 }while( pCur->ix>=pPage->nCell );
72908 if( pPage->intKey ){
72909 return sqlite3BtreeNext(pCur, 0);
72910 }else{
72911 return SQLITE_OK0;
72912 }
72913 }
72914 if( pPage->leaf ){
72915 return SQLITE_OK0;
72916 }else{
72917 return moveToLeftmost(pCur);
72918 }
72919}
72920SQLITE_PRIVATEstatic int sqlite3BtreeNext(BtCursor *pCur, int flags){
72921 MemPage *pPage;
72922 UNUSED_PARAMETER( flags )(void)(flags); /* Used in COMDB2 but not native SQLite */
72923 assert( cursorOwnsBtShared(pCur) )((void) (0));
72924 assert( flags==0 || flags==1 )((void) (0));
72925 pCur->info.nSize = 0;
72926 pCur->curFlags &= ~(BTCF_ValidNKey0x02|BTCF_ValidOvfl0x04);
72927 if( pCur->eState!=CURSOR_VALID0 ) return btreeNext(pCur);
72928 pPage = pCur->pPage;
72929 if( (++pCur->ix)>=pPage->nCell ){
72930 pCur->ix--;
72931 return btreeNext(pCur);
72932 }
72933 if( pPage->leaf ){
72934 return SQLITE_OK0;
72935 }else{
72936 return moveToLeftmost(pCur);
72937 }
72938}
72939
72940/*
72941** Step the cursor to the back to the previous entry in the database.
72942** Return values:
72943**
72944** SQLITE_OK success
72945** SQLITE_DONE the cursor is already on the first element of the table
72946** otherwise some kind of error occurred
72947**
72948** The main entry point is sqlite3BtreePrevious(). That routine is optimized
72949** for the common case of merely decrementing the cell counter BtCursor.aiIdx
72950** to the previous cell on the current page. The (slower) btreePrevious()
72951** helper routine is called when it is necessary to move to a different page
72952** or to restore the cursor.
72953**
72954** If bit 0x01 of the F argument to sqlite3BtreePrevious(C,F) is 1, then
72955** the cursor corresponds to an SQL index and this routine could have been
72956** skipped if the SQL index had been a unique index. The F argument is a
72957** hint to the implement. The native SQLite btree implementation does not
72958** use this hint, but COMDB2 does.
72959*/
72960static SQLITE_NOINLINE__attribute__((noinline)) int btreePrevious(BtCursor *pCur){
72961 int rc;
72962 MemPage *pPage;
72963
72964 assert( cursorOwnsBtShared(pCur) )((void) (0));
72965 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 )((void) (0));
72966 assert( pCur->info.nSize==0 )((void) (0));
72967 if( pCur->eState!=CURSOR_VALID0 ){
72968 rc = restoreCursorPosition(pCur)(pCur->eState>=3 ? btreeRestoreCursorPosition(pCur) : 0
)
;
72969 if( rc!=SQLITE_OK0 ){
72970 return rc;
72971 }
72972 if( CURSOR_INVALID1==pCur->eState ){
72973 return SQLITE_DONE101;
72974 }
72975 if( CURSOR_SKIPNEXT2==pCur->eState ){
72976 pCur->eState = CURSOR_VALID0;
72977 if( pCur->skipNext<0 ) return SQLITE_OK0;
72978 }
72979 }
72980
72981 pPage = pCur->pPage;
72982 assert( pPage->isInit )((void) (0));
72983 if( !pPage->leaf ){
72984 int idx = pCur->ix;
72985 rc = moveToChild(pCur, get4bytesqlite3Get4byte(findCell(pPage, idx)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(idx)])[0]<<8 | (&(pPage)->aCellIdx
[2*(idx)])[1])))
));
72986 if( rc ) return rc;
72987 rc = moveToRightmost(pCur);
72988 }else{
72989 while( pCur->ix==0 ){
72990 if( pCur->iPage==0 ){
72991 pCur->eState = CURSOR_INVALID1;
72992 return SQLITE_DONE101;
72993 }
72994 moveToParent(pCur);
72995 }
72996 assert( pCur->info.nSize==0 )((void) (0));
72997 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 )((void) (0));
72998
72999 pCur->ix--;
73000 pPage = pCur->pPage;
73001 if( pPage->intKey && !pPage->leaf ){
73002 rc = sqlite3BtreePrevious(pCur, 0);
73003 }else{
73004 rc = SQLITE_OK0;
73005 }
73006 }
73007 return rc;
73008}
73009SQLITE_PRIVATEstatic int sqlite3BtreePrevious(BtCursor *pCur, int flags){
73010 assert( cursorOwnsBtShared(pCur) )((void) (0));
73011 assert( flags==0 || flags==1 )((void) (0));
73012 UNUSED_PARAMETER( flags )(void)(flags); /* Used in COMDB2 but not native SQLite */
73013 pCur->curFlags &= ~(BTCF_AtLast0x08|BTCF_ValidOvfl0x04|BTCF_ValidNKey0x02);
73014 pCur->info.nSize = 0;
73015 if( pCur->eState!=CURSOR_VALID0
73016 || pCur->ix==0
73017 || pCur->pPage->leaf==0
73018 ){
73019 return btreePrevious(pCur);
73020 }
73021 pCur->ix--;
73022 return SQLITE_OK0;
73023}
73024
73025/*
73026** Allocate a new page from the database file.
73027**
73028** The new page is marked as dirty. (In other words, sqlite3PagerWrite()
73029** has already been called on the new page.) The new page has also
73030** been referenced and the calling routine is responsible for calling
73031** sqlite3PagerUnref() on the new page when it is done.
73032**
73033** SQLITE_OK is returned on success. Any other return value indicates
73034** an error. *ppPage is set to NULL in the event of an error.
73035**
73036** If the "nearby" parameter is not 0, then an effort is made to
73037** locate a page close to the page number "nearby". This can be used in an
73038** attempt to keep related pages close to each other in the database file,
73039** which in turn can make database access faster.
73040**
73041** If the eMode parameter is BTALLOC_EXACT and the nearby page exists
73042** anywhere on the free-list, then it is guaranteed to be returned. If
73043** eMode is BTALLOC_LT then the page returned will be less than or equal
73044** to nearby if any such page exists. If eMode is BTALLOC_ANY then there
73045** are no restrictions on which page is returned.
73046*/
73047static int allocateBtreePage(
73048 BtShared *pBt, /* The btree */
73049 MemPage **ppPage, /* Store pointer to the allocated page here */
73050 Pgno *pPgno, /* Store the page number here */
73051 Pgno nearby, /* Search for a page near this one */
73052 u8 eMode /* BTALLOC_EXACT, BTALLOC_LT, or BTALLOC_ANY */
73053){
73054 MemPage *pPage1;
73055 int rc;
73056 u32 n; /* Number of pages on the freelist */
73057 u32 k; /* Number of leaves on the trunk of the freelist */
73058 MemPage *pTrunk = 0;
73059 MemPage *pPrevTrunk = 0;
73060 Pgno mxPage; /* Total size of the database file */
73061
73062 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
73063 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) )((void) (0));
73064 pPage1 = pBt->pPage1;
73065 mxPage = btreePagecount(pBt);
73066 /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36
73067 ** stores stores the total number of pages on the freelist. */
73068 n = get4bytesqlite3Get4byte(&pPage1->aData[36]);
73069 testcase( n==mxPage-1 );
73070 if( n>=mxPage ){
73071 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(73071);
73072 }
73073 if( n>0 ){
73074 /* There are pages on the freelist. Reuse one of those pages. */
73075 Pgno iTrunk;
73076 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
73077 u32 nSearch = 0; /* Count of the number of search attempts */
73078
73079 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
73080 ** shows that the page 'nearby' is somewhere on the free-list, then
73081 ** the entire-list will be searched for that page.
73082 */
73083#ifndef SQLITE_OMIT_AUTOVACUUM
73084 if( eMode==BTALLOC_EXACT1 ){
73085 if( nearby<=mxPage ){
73086 u8 eType;
73087 assert( nearby>0 )((void) (0));
73088 assert( pBt->autoVacuum )((void) (0));
73089 rc = ptrmapGet(pBt, nearby, &eType, 0);
73090 if( rc ) return rc;
73091 if( eType==PTRMAP_FREEPAGE2 ){
73092 searchList = 1;
73093 }
73094 }
73095 }else if( eMode==BTALLOC_LE2 ){
73096 searchList = 1;
73097 }
73098#endif
73099
73100 /* Decrement the free-list count by 1. Set iTrunk to the index of the
73101 ** first free-list trunk page. iPrevTrunk is initially 1.
73102 */
73103 rc = sqlite3PagerWrite(pPage1->pDbPage);
73104 if( rc ) return rc;
73105 put4bytesqlite3Put4byte(&pPage1->aData[36], n-1);
73106
73107 /* The code within this loop is run only once if the 'searchList' variable
73108 ** is not true. Otherwise, it runs once for each trunk-page on the
73109 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
73110 ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT)
73111 */
73112 do {
73113 pPrevTrunk = pTrunk;
73114 if( pPrevTrunk ){
73115 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
73116 ** is the page number of the next freelist trunk page in the list or
73117 ** zero if this is the last freelist trunk page. */
73118 iTrunk = get4bytesqlite3Get4byte(&pPrevTrunk->aData[0]);
73119 }else{
73120 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
73121 ** stores the page number of the first page of the freelist, or zero if
73122 ** the freelist is empty. */
73123 iTrunk = get4bytesqlite3Get4byte(&pPage1->aData[32]);
73124 }
73125 testcase( iTrunk==mxPage );
73126 if( iTrunk>mxPage || nSearch++ > n ){
73127 rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1)sqlite3CorruptError(73127);
73128 }else{
73129 rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0);
73130 }
73131 if( rc ){
73132 pTrunk = 0;
73133 goto end_allocate_page;
73134 }
73135 assert( pTrunk!=0 )((void) (0));
73136 assert( pTrunk->aData!=0 )((void) (0));
73137 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
73138 ** is the number of leaf page pointers to follow. */
73139 k = get4bytesqlite3Get4byte(&pTrunk->aData[4]);
73140 if( k==0 && !searchList ){
73141 /* The trunk has no leaves and the list is not being searched.
73142 ** So extract the trunk page itself and use it as the newly
73143 ** allocated page */
73144 assert( pPrevTrunk==0 )((void) (0));
73145 rc = sqlite3PagerWrite(pTrunk->pDbPage);
73146 if( rc ){
73147 goto end_allocate_page;
73148 }
73149 *pPgno = iTrunk;
73150 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
73151 *ppPage = pTrunk;
73152 pTrunk = 0;
73153 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
73154 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
73155 /* Value of k is out of range. Database corruption */
73156 rc = SQLITE_CORRUPT_PGNO(iTrunk)sqlite3CorruptError(73156);
73157 goto end_allocate_page;
73158#ifndef SQLITE_OMIT_AUTOVACUUM
73159 }else if( searchList
73160 && (nearby==iTrunk || (iTrunk<nearby && eMode==BTALLOC_LE2))
73161 ){
73162 /* The list is being searched and this trunk page is the page
73163 ** to allocate, regardless of whether it has leaves.
73164 */
73165 *pPgno = iTrunk;
73166 *ppPage = pTrunk;
73167 searchList = 0;
73168 rc = sqlite3PagerWrite(pTrunk->pDbPage);
73169 if( rc ){
73170 goto end_allocate_page;
73171 }
73172 if( k==0 ){
73173 if( !pPrevTrunk ){
73174 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
73175 }else{
73176 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
73177 if( rc!=SQLITE_OK0 ){
73178 goto end_allocate_page;
73179 }
73180 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
73181 }
73182 }else{
73183 /* The trunk page is required by the caller but it contains
73184 ** pointers to free-list leaves. The first leaf becomes a trunk
73185 ** page in this case.
73186 */
73187 MemPage *pNewTrunk;
73188 Pgno iNewTrunk = get4bytesqlite3Get4byte(&pTrunk->aData[8]);
73189 if( iNewTrunk>mxPage ){
73190 rc = SQLITE_CORRUPT_PGNO(iTrunk)sqlite3CorruptError(73190);
73191 goto end_allocate_page;
73192 }
73193 testcase( iNewTrunk==mxPage );
73194 rc = btreeGetUnusedPage(pBt, iNewTrunk, &pNewTrunk, 0);
73195 if( rc!=SQLITE_OK0 ){
73196 goto end_allocate_page;
73197 }
73198 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
73199 if( rc!=SQLITE_OK0 ){
73200 releasePage(pNewTrunk);
73201 goto end_allocate_page;
73202 }
73203 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
73204 put4bytesqlite3Put4byte(&pNewTrunk->aData[4], k-1);
73205 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
73206 releasePage(pNewTrunk);
73207 if( !pPrevTrunk ){
73208 assert( sqlite3PagerIswriteable(pPage1->pDbPage) )((void) (0));
73209 put4bytesqlite3Put4byte(&pPage1->aData[32], iNewTrunk);
73210 }else{
73211 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
73212 if( rc ){
73213 goto end_allocate_page;
73214 }
73215 put4bytesqlite3Put4byte(&pPrevTrunk->aData[0], iNewTrunk);
73216 }
73217 }
73218 pTrunk = 0;
73219 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
73220#endif
73221 }else if( k>0 ){
73222 /* Extract a leaf from the trunk */
73223 u32 closest;
73224 Pgno iPage;
73225 unsigned char *aData = pTrunk->aData;
73226 if( nearby>0 ){
73227 u32 i;
73228 closest = 0;
73229 if( eMode==BTALLOC_LE2 ){
73230 for(i=0; i<k; i++){
73231 iPage = get4bytesqlite3Get4byte(&aData[8+i*4]);
73232 if( iPage<=nearby ){
73233 closest = i;
73234 break;
73235 }
73236 }
73237 }else{
73238 int dist;
73239 dist = sqlite3AbsInt32(get4bytesqlite3Get4byte(&aData[8]) - nearby);
73240 for(i=1; i<k; i++){
73241 int d2 = sqlite3AbsInt32(get4bytesqlite3Get4byte(&aData[8+i*4]) - nearby);
73242 if( d2<dist ){
73243 closest = i;
73244 dist = d2;
73245 }
73246 }
73247 }
73248 }else{
73249 closest = 0;
73250 }
73251
73252 iPage = get4bytesqlite3Get4byte(&aData[8+closest*4]);
73253 testcase( iPage==mxPage );
73254 if( iPage>mxPage || iPage<2 ){
73255 rc = SQLITE_CORRUPT_PGNO(iTrunk)sqlite3CorruptError(73255);
73256 goto end_allocate_page;
73257 }
73258 testcase( iPage==mxPage );
73259 if( !searchList
73260 || (iPage==nearby || (iPage<nearby && eMode==BTALLOC_LE2))
73261 ){
73262 int noContent;
73263 *pPgno = iPage;
73264 TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
73265 ": %d more free pages\n",
73266 *pPgno, closest+1, k, pTrunk->pgno, n-1));
73267 rc = sqlite3PagerWrite(pTrunk->pDbPage);
73268 if( rc ) goto end_allocate_page;
73269 if( closest<k-1 ){
73270 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
73271 }
73272 put4bytesqlite3Put4byte(&aData[4], k-1);
73273 noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT0x01 : 0;
73274 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent);
73275 if( rc==SQLITE_OK0 ){
73276 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
73277 if( rc!=SQLITE_OK0 ){
73278 releasePage(*ppPage);
73279 *ppPage = 0;
73280 }
73281 }
73282 searchList = 0;
73283 }
73284 }
73285 releasePage(pPrevTrunk);
73286 pPrevTrunk = 0;
73287 }while( searchList );
73288 }else{
73289 /* There are no pages on the freelist, so append a new page to the
73290 ** database image.
73291 **
73292 ** Normally, new pages allocated by this block can be requested from the
73293 ** pager layer with the 'no-content' flag set. This prevents the pager
73294 ** from trying to read the pages content from disk. However, if the
73295 ** current transaction has already run one or more incremental-vacuum
73296 ** steps, then the page we are about to allocate may contain content
73297 ** that is required in the event of a rollback. In this case, do
73298 ** not set the no-content flag. This causes the pager to load and journal
73299 ** the current page content before overwriting it.
73300 **
73301 ** Note that the pager will not actually attempt to load or journal
73302 ** content for any page that really does lie past the end of the database
73303 ** file on disk. So the effects of disabling the no-content optimization
73304 ** here are confined to those pages that lie between the end of the
73305 ** database image and the end of the database file.
73306 */
73307 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate)(pBt->bDoTruncate))? PAGER_GET_NOCONTENT0x01:0;
73308
73309 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
73310 if( rc ) return rc;
73311 pBt->nPage++;
73312 if( pBt->nPage==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ) pBt->nPage++;
73313
73314#ifndef SQLITE_OMIT_AUTOVACUUM
73315 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage)(ptrmapPageno((pBt), (pBt->nPage))==(pBt->nPage)) ){
73316 /* If *pPgno refers to a pointer-map page, allocate two new pages
73317 ** at the end of the file instead of one. The first allocated page
73318 ** becomes a new pointer-map page, the second is used by the caller.
73319 */
73320 MemPage *pPg = 0;
73321 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
73322 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) )((void) (0));
73323 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
73324 if( rc==SQLITE_OK0 ){
73325 rc = sqlite3PagerWrite(pPg->pDbPage);
73326 releasePage(pPg);
73327 }
73328 if( rc ) return rc;
73329 pBt->nPage++;
73330 if( pBt->nPage==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){ pBt->nPage++; }
73331 }
73332#endif
73333 put4bytesqlite3Put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
73334 *pPgno = pBt->nPage;
73335
73336 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) )((void) (0));
73337 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, bNoContent);
73338 if( rc ) return rc;
73339 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
73340 if( rc!=SQLITE_OK0 ){
73341 releasePage(*ppPage);
73342 *ppPage = 0;
73343 }
73344 TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
73345 }
73346
73347 assert( CORRUPT_DB || *pPgno!=PENDING_BYTE_PAGE(pBt) )((void) (0));
73348
73349end_allocate_page:
73350 releasePage(pTrunk);
73351 releasePage(pPrevTrunk);
73352 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 )((void) (0));
73353 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 )((void) (0));
73354 return rc;
73355}
73356
73357/*
73358** This function is used to add page iPage to the database file free-list.
73359** It is assumed that the page is not already a part of the free-list.
73360**
73361** The value passed as the second argument to this function is optional.
73362** If the caller happens to have a pointer to the MemPage object
73363** corresponding to page iPage handy, it may pass it as the second value.
73364** Otherwise, it may pass NULL.
73365**
73366** If a pointer to a MemPage object is passed as the second argument,
73367** its reference count is not altered by this function.
73368*/
73369static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
73370 MemPage *pTrunk = 0; /* Free-list trunk page */
73371 Pgno iTrunk = 0; /* Page number of free-list trunk page */
73372 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
73373 MemPage *pPage; /* Page being freed. May be NULL. */
73374 int rc; /* Return Code */
73375 u32 nFree; /* Initial number of pages on free-list */
73376
73377 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
73378 assert( CORRUPT_DB || iPage>1 )((void) (0));
73379 assert( !pMemPage || pMemPage->pgno==iPage )((void) (0));
73380
73381 if( iPage<2 || iPage>pBt->nPage ){
73382 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(73382);
73383 }
73384 if( pMemPage ){
73385 pPage = pMemPage;
73386 sqlite3PagerRef(pPage->pDbPage);
73387 }else{
73388 pPage = btreePageLookup(pBt, iPage);
73389 }
73390
73391 /* Increment the free page count on pPage1 */
73392 rc = sqlite3PagerWrite(pPage1->pDbPage);
73393 if( rc ) goto freepage_out;
73394 nFree = get4bytesqlite3Get4byte(&pPage1->aData[36]);
73395 put4bytesqlite3Put4byte(&pPage1->aData[36], nFree+1);
73396
73397 if( pBt->btsFlags & BTS_SECURE_DELETE0x0004 ){
73398 /* If the secure_delete option is enabled, then
73399 ** always fully overwrite deleted information with zeros.
73400 */
73401 if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
73402 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
73403 ){
73404 goto freepage_out;
73405 }
73406 memset(pPage->aData, 0, pPage->pBt->pageSize);
73407 }
73408
73409 /* If the database supports auto-vacuum, write an entry in the pointer-map
73410 ** to indicate that the page is free.
73411 */
73412 if( ISAUTOVACUUM(pBt->autoVacuum) ){
73413 ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE2, 0, &rc);
73414 if( rc ) goto freepage_out;
73415 }
73416
73417 /* Now manipulate the actual database free-list structure. There are two
73418 ** possibilities. If the free-list is currently empty, or if the first
73419 ** trunk page in the free-list is full, then this page will become a
73420 ** new free-list trunk page. Otherwise, it will become a leaf of the
73421 ** first trunk page in the current free-list. This block tests if it
73422 ** is possible to add the page as a new free-list leaf.
73423 */
73424 if( nFree!=0 ){
73425 u32 nLeaf; /* Initial number of leaf cells on trunk page */
73426
73427 iTrunk = get4bytesqlite3Get4byte(&pPage1->aData[32]);
73428 if( iTrunk>btreePagecount(pBt) ){
73429 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(73429);
73430 goto freepage_out;
73431 }
73432 rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
73433 if( rc!=SQLITE_OK0 ){
73434 goto freepage_out;
73435 }
73436
73437 nLeaf = get4bytesqlite3Get4byte(&pTrunk->aData[4]);
73438 assert( pBt->usableSize>32 )((void) (0));
73439 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
73440 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(73440);
73441 goto freepage_out;
73442 }
73443 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
73444 /* In this case there is room on the trunk page to insert the page
73445 ** being freed as a new leaf.
73446 **
73447 ** Note that the trunk page is not really full until it contains
73448 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
73449 ** coded. But due to a coding error in versions of SQLite prior to
73450 ** 3.6.0, databases with freelist trunk pages holding more than
73451 ** usableSize/4 - 8 entries will be reported as corrupt. In order
73452 ** to maintain backwards compatibility with older versions of SQLite,
73453 ** we will continue to restrict the number of entries to usableSize/4 - 8
73454 ** for now. At some point in the future (once everyone has upgraded
73455 ** to 3.6.0 or later) we should consider fixing the conditional above
73456 ** to read "usableSize/4-2" instead of "usableSize/4-8".
73457 **
73458 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
73459 ** avoid using the last six entries in the freelist trunk page array in
73460 ** order that database files created by newer versions of SQLite can be
73461 ** read by older versions of SQLite.
73462 */
73463 rc = sqlite3PagerWrite(pTrunk->pDbPage);
73464 if( rc==SQLITE_OK0 ){
73465 put4bytesqlite3Put4byte(&pTrunk->aData[4], nLeaf+1);
73466 put4bytesqlite3Put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
73467 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE0x0004)==0 ){
73468 sqlite3PagerDontWrite(pPage->pDbPage);
73469 }
73470 rc = btreeSetHasContent(pBt, iPage);
73471 }
73472 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
73473 goto freepage_out;
73474 }
73475 }
73476
73477 /* If control flows to this point, then it was not possible to add the
73478 ** the page being freed as a leaf page of the first trunk in the free-list.
73479 ** Possibly because the free-list is empty, or possibly because the
73480 ** first trunk in the free-list is full. Either way, the page being freed
73481 ** will become the new first trunk page in the free-list.
73482 */
73483 if( pPage==0 && SQLITE_OK0!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
73484 goto freepage_out;
73485 }
73486 rc = sqlite3PagerWrite(pPage->pDbPage);
73487 if( rc!=SQLITE_OK0 ){
73488 goto freepage_out;
73489 }
73490 put4bytesqlite3Put4byte(pPage->aData, iTrunk);
73491 put4bytesqlite3Put4byte(&pPage->aData[4], 0);
73492 put4bytesqlite3Put4byte(&pPage1->aData[32], iPage);
73493 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
73494
73495freepage_out:
73496 if( pPage ){
73497 pPage->isInit = 0;
73498 }
73499 releasePage(pPage);
73500 releasePage(pTrunk);
73501 return rc;
73502}
73503static void freePage(MemPage *pPage, int *pRC){
73504 if( (*pRC)==SQLITE_OK0 ){
73505 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
73506 }
73507}
73508
73509/*
73510** Free the overflow pages associated with the given Cell.
73511*/
73512static SQLITE_NOINLINE__attribute__((noinline)) int clearCellOverflow(
73513 MemPage *pPage, /* The page that contains the Cell */
73514 unsigned char *pCell, /* First byte of the Cell */
73515 CellInfo *pInfo /* Size information about the cell */
73516){
73517 BtShared *pBt;
73518 Pgno ovflPgno;
73519 int rc;
73520 int nOvfl;
73521 u32 ovflPageSize;
73522
73523 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
73524 assert( pInfo->nLocal!=pInfo->nPayload )((void) (0));
73525 testcase( pCell + pInfo->nSize == pPage->aDataEnd );
73526 testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
73527 if( pCell + pInfo->nSize > pPage->aDataEnd ){
73528 /* Cell extends past end of page */
73529 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73529);
73530 }
73531 ovflPgno = get4bytesqlite3Get4byte(pCell + pInfo->nSize - 4);
73532 pBt = pPage->pBt;
73533 assert( pBt->usableSize > 4 )((void) (0));
73534 ovflPageSize = pBt->usableSize - 4;
73535 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
73536 assert( nOvfl>0 ||((void) (0))
73537 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)((void) (0))
73538 )((void) (0));
73539 while( nOvfl-- ){
73540 Pgno iNext = 0;
73541 MemPage *pOvfl = 0;
73542 if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
73543 /* 0 is not a legal page number and page 1 cannot be an
73544 ** overflow page. Therefore if ovflPgno<2 or past the end of the
73545 ** file the database must be corrupt. */
73546 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(73546);
73547 }
73548 if( nOvfl ){
73549 rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
73550 if( rc ) return rc;
73551 }
73552
73553 if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) )
73554 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
73555 ){
73556 /* There is no reason any cursor should have an outstanding reference
73557 ** to an overflow page belonging to a cell that is being deleted/updated.
73558 ** So if there exists more than one reference to this page, then it
73559 ** must not really be an overflow page and the database must be corrupt.
73560 ** It is helpful to detect this before calling freePage2(), as
73561 ** freePage2() may zero the page contents if secure-delete mode is
73562 ** enabled. If this 'overflow' page happens to be a page that the
73563 ** caller is iterating through or using in some other way, this
73564 ** can be problematic.
73565 */
73566 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(73566);
73567 }else{
73568 rc = freePage2(pBt, pOvfl, ovflPgno);
73569 }
73570
73571 if( pOvfl ){
73572 sqlite3PagerUnref(pOvfl->pDbPage);
73573 }
73574 if( rc ) return rc;
73575 ovflPgno = iNext;
73576 }
73577 return SQLITE_OK0;
73578}
73579
73580/* Call xParseCell to compute the size of a cell. If the cell contains
73581** overflow, then invoke cellClearOverflow to clear out that overflow.
73582** STore the result code (SQLITE_OK or some error code) in rc.
73583**
73584** Implemented as macro to force inlining for performance.
73585*/
73586#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; }
\
73587 pPage->xParseCell(pPage, pCell, &sInfo); \
73588 if( sInfo.nLocal!=sInfo.nPayload ){ \
73589 rc = clearCellOverflow(pPage, pCell, &sInfo); \
73590 }else{ \
73591 rc = SQLITE_OK0; \
73592 }
73593
73594
73595/*
73596** Create the byte sequence used to represent a cell on page pPage
73597** and write that byte sequence into pCell[]. Overflow pages are
73598** allocated and filled in as necessary. The calling procedure
73599** is responsible for making sure sufficient space has been allocated
73600** for pCell[].
73601**
73602** Note that pCell does not necessary need to point to the pPage->aData
73603** area. pCell might point to some temporary storage. The cell will
73604** be constructed in this temporary area then copied into pPage->aData
73605** later.
73606*/
73607static int fillInCell(
73608 MemPage *pPage, /* The page that contains the cell */
73609 unsigned char *pCell, /* Complete text of the cell */
73610 const BtreePayload *pX, /* Payload with which to construct the cell */
73611 int *pnSize /* Write cell size here */
73612){
73613 int nPayload;
73614 const u8 *pSrc;
73615 int nSrc, n, rc, mn;
73616 int spaceLeft;
73617 MemPage *pToRelease;
73618 unsigned char *pPrior;
73619 unsigned char *pPayload;
73620 BtShared *pBt;
73621 Pgno pgnoOvfl;
73622 int nHeader;
73623
73624 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
73625
73626 /* pPage is not necessarily writeable since pCell might be auxiliary
73627 ** buffer space that is separate from the pPage buffer area */
73628 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]((void) (0))
73629 || sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
73630
73631 /* Fill in the header. */
73632 nHeader = pPage->childPtrSize;
73633 if( pPage->intKey ){
73634 nPayload = pX->nData + pX->nZero;
73635 pSrc = pX->pData;
73636 nSrc = pX->nData;
73637 assert( pPage->intKeyLeaf )((void) (0)); /* fillInCell() only called for leaves */
73638 nHeader += putVarint32(&pCell[nHeader], nPayload)(u8)(((u32)(nPayload)<(u32)0x80)?(*(&pCell[nHeader])=(
unsigned char)(nPayload)),1: sqlite3PutVarint((&pCell[nHeader
]),(nPayload)))
;
73639 nHeader += putVarintsqlite3PutVarint(&pCell[nHeader], *(u64*)&pX->nKey);
73640 }else{
73641 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 )((void) (0));
73642 nSrc = nPayload = (int)pX->nKey;
73643 pSrc = pX->pKey;
73644 nHeader += putVarint32(&pCell[nHeader], nPayload)(u8)(((u32)(nPayload)<(u32)0x80)?(*(&pCell[nHeader])=(
unsigned char)(nPayload)),1: sqlite3PutVarint((&pCell[nHeader
]),(nPayload)))
;
73645 }
73646
73647 /* Fill in the payload */
73648 pPayload = &pCell[nHeader];
73649 if( nPayload<=pPage->maxLocal ){
73650 /* This is the common case where everything fits on the btree page
73651 ** and no overflow pages are required. */
73652 n = nHeader + nPayload;
73653 testcase( n==3 );
73654 testcase( n==4 );
73655 if( n<4 ) n = 4;
73656 *pnSize = n;
73657 assert( nSrc<=nPayload )((void) (0));
73658 testcase( nSrc<nPayload );
73659 memcpy(pPayload, pSrc, nSrc);
73660 memset(pPayload+nSrc, 0, nPayload-nSrc);
73661 return SQLITE_OK0;
73662 }
73663
73664 /* If we reach this point, it means that some of the content will need
73665 ** to spill onto overflow pages.
73666 */
73667 mn = pPage->minLocal;
73668 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
73669 testcase( n==pPage->maxLocal );
73670 testcase( n==pPage->maxLocal+1 );
73671 if( n > pPage->maxLocal ) n = mn;
73672 spaceLeft = n;
73673 *pnSize = n + nHeader + 4;
73674 pPrior = &pCell[nHeader+n];
73675 pToRelease = 0;
73676 pgnoOvfl = 0;
73677 pBt = pPage->pBt;
73678
73679 /* At this point variables should be set as follows:
73680 **
73681 ** nPayload Total payload size in bytes
73682 ** pPayload Begin writing payload here
73683 ** spaceLeft Space available at pPayload. If nPayload>spaceLeft,
73684 ** that means content must spill into overflow pages.
73685 ** *pnSize Size of the local cell (not counting overflow pages)
73686 ** pPrior Where to write the pgno of the first overflow page
73687 **
73688 ** Use a call to btreeParseCellPtr() to verify that the values above
73689 ** were computed correctly.
73690 */
73691#ifdef SQLITE_DEBUG
73692 {
73693 CellInfo info;
73694 pPage->xParseCell(pPage, pCell, &info);
73695 assert( nHeader==(int)(info.pPayload - pCell) )((void) (0));
73696 assert( info.nKey==pX->nKey )((void) (0));
73697 assert( *pnSize == info.nSize )((void) (0));
73698 assert( spaceLeft == info.nLocal )((void) (0));
73699 }
73700#endif
73701
73702 /* Write the payload into the local Cell and any extra into overflow pages */
73703 while( 1 ){
73704 n = nPayload;
73705 if( n>spaceLeft ) n = spaceLeft;
73706
73707 /* If pToRelease is not zero than pPayload points into the data area
73708 ** of pToRelease. Make sure pToRelease is still writeable. */
73709 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) )((void) (0));
73710
73711 /* If pPayload is part of the data area of pPage, then make sure pPage
73712 ** is still writeable */
73713 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]((void) (0))
73714 || sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
73715
73716 if( nSrc>=n ){
73717 memcpy(pPayload, pSrc, n);
73718 }else if( nSrc>0 ){
73719 n = nSrc;
73720 memcpy(pPayload, pSrc, n);
73721 }else{
73722 memset(pPayload, 0, n);
73723 }
73724 nPayload -= n;
73725 if( nPayload<=0 ) break;
73726 pPayload += n;
73727 pSrc += n;
73728 nSrc -= n;
73729 spaceLeft -= n;
73730 if( spaceLeft==0 ){
73731 MemPage *pOvfl = 0;
73732#ifndef SQLITE_OMIT_AUTOVACUUM
73733 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
73734 if( pBt->autoVacuum ){
73735 do{
73736 pgnoOvfl++;
73737 } while(
73738 PTRMAP_ISPAGE(pBt, pgnoOvfl)(ptrmapPageno((pBt), (pgnoOvfl))==(pgnoOvfl)) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1))
73739 );
73740 }
73741#endif
73742 rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
73743#ifndef SQLITE_OMIT_AUTOVACUUM
73744 /* If the database supports auto-vacuum, and the second or subsequent
73745 ** overflow page is being allocated, add an entry to the pointer-map
73746 ** for that page now.
73747 **
73748 ** If this is the first overflow page, then write a partial entry
73749 ** to the pointer-map. If we write nothing to this pointer-map slot,
73750 ** then the optimistic overflow chain processing in clearCell()
73751 ** may misinterpret the uninitialized values and delete the
73752 ** wrong pages from the database.
73753 */
73754 if( pBt->autoVacuum && rc==SQLITE_OK0 ){
73755 u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW24:PTRMAP_OVERFLOW13);
73756 ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
73757 if( rc ){
73758 releasePage(pOvfl);
73759 }
73760 }
73761#endif
73762 if( rc ){
73763 releasePage(pToRelease);
73764 return rc;
73765 }
73766
73767 /* If pToRelease is not zero than pPrior points into the data area
73768 ** of pToRelease. Make sure pToRelease is still writeable. */
73769 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) )((void) (0));
73770
73771 /* If pPrior is part of the data area of pPage, then make sure pPage
73772 ** is still writeable */
73773 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]((void) (0))
73774 || sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
73775
73776 put4bytesqlite3Put4byte(pPrior, pgnoOvfl);
73777 releasePage(pToRelease);
73778 pToRelease = pOvfl;
73779 pPrior = pOvfl->aData;
73780 put4bytesqlite3Put4byte(pPrior, 0);
73781 pPayload = &pOvfl->aData[4];
73782 spaceLeft = pBt->usableSize - 4;
73783 }
73784 }
73785 releasePage(pToRelease);
73786 return SQLITE_OK0;
73787}
73788
73789/*
73790** Remove the i-th cell from pPage. This routine effects pPage only.
73791** The cell content is not freed or deallocated. It is assumed that
73792** the cell content has been copied someplace else. This routine just
73793** removes the reference to the cell from pPage.
73794**
73795** "sz" must be the number of bytes in the cell.
73796*/
73797static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
73798 u32 pc; /* Offset to cell content of cell being deleted */
73799 u8 *data; /* pPage->aData */
73800 u8 *ptr; /* Used to move bytes around within data[] */
73801 int rc; /* The return code */
73802 int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */
73803
73804 if( *pRC ) return;
73805 assert( idx>=0 )((void) (0));
73806 assert( idx<pPage->nCell )((void) (0));
73807 assert( CORRUPT_DB || sz==cellSize(pPage, idx) )((void) (0));
73808 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
73809 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
73810 assert( pPage->nFree>=0 )((void) (0));
73811 data = pPage->aData;
73812 ptr = &pPage->aCellIdx[2*idx];
73813 assert( pPage->pBt->usableSize > (u32)(ptr-data) )((void) (0));
73814 pc = get2byte(ptr)((ptr)[0]<<8 | (ptr)[1]);
73815 hdr = pPage->hdrOffset;
73816#if 0 /* Not required. Omit for efficiency */
73817 if( pc<hdr+pPage->nCell*2 ){
73818 *pRC = SQLITE_CORRUPT_BKPTsqlite3CorruptError(73818);
73819 return;
73820 }
73821#endif
73822 testcase( pc==(u32)get2byte(&data[hdr+5]) );
73823 testcase( pc+sz==pPage->pBt->usableSize );
73824 if( pc+sz > pPage->pBt->usableSize ){
73825 *pRC = SQLITE_CORRUPT_BKPTsqlite3CorruptError(73825);
73826 return;
73827 }
73828 rc = freeSpace(pPage, pc, sz);
73829 if( rc ){
73830 *pRC = rc;
73831 return;
73832 }
73833 pPage->nCell--;
73834 if( pPage->nCell==0 ){
73835 memset(&data[hdr+1], 0, 4);
73836 data[hdr+7] = 0;
73837 put2byte(&data[hdr+5], pPage->pBt->usableSize)((&data[hdr+5])[0] = (u8)((pPage->pBt->usableSize)>>
8), (&data[hdr+5])[1] = (u8)(pPage->pBt->usableSize
))
;
73838 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
73839 - pPage->childPtrSize - 8;
73840 }else{
73841 memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
73842 put2byte(&data[hdr+3], pPage->nCell)((&data[hdr+3])[0] = (u8)((pPage->nCell)>>8), (&
data[hdr+3])[1] = (u8)(pPage->nCell))
;
73843 pPage->nFree += 2;
73844 }
73845}
73846
73847/*
73848** Insert a new cell on pPage at cell index "i". pCell points to the
73849** content of the cell.
73850**
73851** If the cell content will fit on the page, then put it there. If it
73852** will not fit, then make a copy of the cell content into pTemp if
73853** pTemp is not null. Regardless of pTemp, allocate a new entry
73854** in pPage->apOvfl[] and make it point to the cell content (either
73855** in pTemp or the original pCell) and also record its index.
73856** Allocating a new entry in pPage->aCell[] implies that
73857** pPage->nOverflow is incremented.
73858**
73859** *pRC must be SQLITE_OK when this routine is called.
73860*/
73861static void insertCell(
73862 MemPage *pPage, /* Page into which we are copying */
73863 int i, /* New cell becomes the i-th cell of the page */
73864 u8 *pCell, /* Content of the new cell */
73865 int sz, /* Bytes of content in pCell */
73866 u8 *pTemp, /* Temp storage space for pCell, if needed */
73867 Pgno iChild, /* If non-zero, replace first 4 bytes with this value */
73868 int *pRC /* Read and write return code from here */
73869){
73870 int idx = 0; /* Where to write new cell content in data[] */
73871 int j; /* Loop counter */
73872 u8 *data; /* The content of the whole page */
73873 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
73874
73875 assert( *pRC==SQLITE_OK )((void) (0));
73876 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow )((void) (0));
73877 assert( MX_CELL(pPage->pBt)<=10921 )((void) (0));
73878 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB )((void) (0));
73879 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) )((void) (0));
73880 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) )((void) (0));
73881 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
73882 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB )((void) (0));
73883 assert( pPage->nFree>=0 )((void) (0));
73884 if( pPage->nOverflow || sz+2>pPage->nFree ){
73885 if( pTemp ){
73886 memcpy(pTemp, pCell, sz);
73887 pCell = pTemp;
73888 }
73889 if( iChild ){
73890 put4bytesqlite3Put4byte(pCell, iChild);
73891 }
73892 j = pPage->nOverflow++;
73893 /* Comparison against ArraySize-1 since we hold back one extra slot
73894 ** as a contingency. In other words, never need more than 3 overflow
73895 ** slots but 4 are allocated, just to be safe. */
73896 assert( j < ArraySize(pPage->apOvfl)-1 )((void) (0));
73897 pPage->apOvfl[j] = pCell;
73898 pPage->aiOvfl[j] = (u16)i;
73899
73900 /* When multiple overflows occur, they are always sequential and in
73901 ** sorted order. This invariants arise because multiple overflows can
73902 ** only occur when inserting divider cells into the parent page during
73903 ** balancing, and the dividers are adjacent and sorted.
73904 */
73905 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i )((void) (0)); /* Overflows in sorted order */
73906 assert( j==0 || i==pPage->aiOvfl[j-1]+1 )((void) (0)); /* Overflows are sequential */
73907 }else{
73908 int rc = sqlite3PagerWrite(pPage->pDbPage);
73909 if( rc!=SQLITE_OK0 ){
73910 *pRC = rc;
73911 return;
73912 }
73913 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
73914 data = pPage->aData;
73915 assert( &data[pPage->cellOffset]==pPage->aCellIdx )((void) (0));
73916 rc = allocateSpace(pPage, sz, &idx);
73917 if( rc ){ *pRC = rc; return; }
73918 /* The allocateSpace() routine guarantees the following properties
73919 ** if it returns successfully */
73920 assert( idx >= 0 )((void) (0));
73921 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB )((void) (0));
73922 assert( idx+sz <= (int)pPage->pBt->usableSize )((void) (0));
73923 pPage->nFree -= (u16)(2 + sz);
73924 if( iChild ){
73925 /* In a corrupt database where an entry in the cell index section of
73926 ** a btree page has a value of 3 or less, the pCell value might point
73927 ** as many as 4 bytes in front of the start of the aData buffer for
73928 ** the source page. Make sure this does not cause problems by not
73929 ** reading the first 4 bytes */
73930 memcpy(&data[idx+4], pCell+4, sz-4);
73931 put4bytesqlite3Put4byte(&data[idx], iChild);
73932 }else{
73933 memcpy(&data[idx], pCell, sz);
73934 }
73935 pIns = pPage->aCellIdx + i*2;
73936 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
73937 put2byte(pIns, idx)((pIns)[0] = (u8)((idx)>>8), (pIns)[1] = (u8)(idx));
73938 pPage->nCell++;
73939 /* increment the cell count */
73940 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
73941 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB )((void) (0));
73942#ifndef SQLITE_OMIT_AUTOVACUUM
73943 if( pPage->pBt->autoVacuum ){
73944 /* The cell may contain a pointer to an overflow page. If so, write
73945 ** the entry for the overflow page into the pointer map.
73946 */
73947 ptrmapPutOvflPtr(pPage, pPage, pCell, pRC);
73948 }
73949#endif
73950 }
73951}
73952
73953/*
73954** The following parameters determine how many adjacent pages get involved
73955** in a balancing operation. NN is the number of neighbors on either side
73956** of the page that participate in the balancing operation. NB is the
73957** total number of pages that participate, including the target page and
73958** NN neighbors on either side.
73959**
73960** The minimum value of NN is 1 (of course). Increasing NN above 1
73961** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
73962** in exchange for a larger degradation in INSERT and UPDATE performance.
73963** The value of NN appears to give the best results overall.
73964**
73965** (Later:) The description above makes it seem as if these values are
73966** tunable - as if you could change them and recompile and it would all work.
73967** But that is unlikely. NB has been 3 since the inception of SQLite and
73968** we have never tested any other value.
73969*/
73970#define NN1 1 /* Number of neighbors on either side of pPage */
73971#define NB3 3 /* (NN*2+1): Total pages involved in the balance */
73972
73973/*
73974** A CellArray object contains a cache of pointers and sizes for a
73975** consecutive sequence of cells that might be held on multiple pages.
73976**
73977** The cells in this array are the divider cell or cells from the pParent
73978** page plus up to three child pages. There are a total of nCell cells.
73979**
73980** pRef is a pointer to one of the pages that contributes cells. This is
73981** used to access information such as MemPage.intKey and MemPage.pBt->pageSize
73982** which should be common to all pages that contribute cells to this array.
73983**
73984** apCell[] and szCell[] hold, respectively, pointers to the start of each
73985** cell and the size of each cell. Some of the apCell[] pointers might refer
73986** to overflow cells. In other words, some apCel[] pointers might not point
73987** to content area of the pages.
73988**
73989** A szCell[] of zero means the size of that cell has not yet been computed.
73990**
73991** The cells come from as many as four different pages:
73992**
73993** -----------
73994** | Parent |
73995** -----------
73996** / | \
73997** / | \
73998** --------- --------- ---------
73999** |Child-1| |Child-2| |Child-3|
74000** --------- --------- ---------
74001**
74002** The order of cells is in the array is for an index btree is:
74003**
74004** 1. All cells from Child-1 in order
74005** 2. The first divider cell from Parent
74006** 3. All cells from Child-2 in order
74007** 4. The second divider cell from Parent
74008** 5. All cells from Child-3 in order
74009**
74010** For a table-btree (with rowids) the items 2 and 4 are empty because
74011** content exists only in leaves and there are no divider cells.
74012**
74013** For an index btree, the apEnd[] array holds pointer to the end of page
74014** for Child-1, the Parent, Child-2, the Parent (again), and Child-3,
74015** respectively. The ixNx[] array holds the number of cells contained in
74016** each of these 5 stages, and all stages to the left. Hence:
74017**
74018** ixNx[0] = Number of cells in Child-1.
74019** ixNx[1] = Number of cells in Child-1 plus 1 for first divider.
74020** ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider.
74021** ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells
74022** ixNx[4] = Total number of cells.
74023**
74024** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2]
74025** are used and they point to the leaf pages only, and the ixNx value are:
74026**
74027** ixNx[0] = Number of cells in Child-1.
74028** ixNx[1] = Number of cells in Child-1 and Child-2.
74029** ixNx[2] = Total number of cells.
74030**
74031** Sometimes when deleting, a child page can have zero cells. In those
74032** cases, ixNx[] entries with higher indexes, and the corresponding apEnd[]
74033** entries, shift down. The end result is that each ixNx[] entry should
74034** be larger than the previous
74035*/
74036typedef struct CellArray CellArray;
74037struct CellArray {
74038 int nCell; /* Number of cells in apCell[] */
74039 MemPage *pRef; /* Reference page */
74040 u8 **apCell; /* All cells begin balanced */
74041 u16 *szCell; /* Local size of all cells in apCell[] */
74042 u8 *apEnd[NB3*2]; /* MemPage.aDataEnd values */
74043 int ixNx[NB3*2]; /* Index of at which we move to the next apEnd[] */
74044};
74045
74046/*
74047** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
74048** computed.
74049*/
74050static void populateCellCache(CellArray *p, int idx, int N){
74051 assert( idx>=0 && idx+N<=p->nCell )((void) (0));
74052 while( N>0 ){
74053 assert( p->apCell[idx]!=0 )((void) (0));
74054 if( p->szCell[idx]==0 ){
74055 p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]);
74056 }else{
74057 assert( CORRUPT_DB ||((void) (0))
74058 p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) )((void) (0));
74059 }
74060 idx++;
74061 N--;
74062 }
74063}
74064
74065/*
74066** Return the size of the Nth element of the cell array
74067*/
74068static SQLITE_NOINLINE__attribute__((noinline)) u16 computeCellSize(CellArray *p, int N){
74069 assert( N>=0 && N<p->nCell )((void) (0));
74070 assert( p->szCell[N]==0 )((void) (0));
74071 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
74072 return p->szCell[N];
74073}
74074static u16 cachedCellSize(CellArray *p, int N){
74075 assert( N>=0 && N<p->nCell )((void) (0));
74076 if( p->szCell[N] ) return p->szCell[N];
74077 return computeCellSize(p, N);
74078}
74079
74080/*
74081** Array apCell[] contains pointers to nCell b-tree page cells. The
74082** szCell[] array contains the size in bytes of each cell. This function
74083** replaces the current contents of page pPg with the contents of the cell
74084** array.
74085**
74086** Some of the cells in apCell[] may currently be stored in pPg. This
74087** function works around problems caused by this by making a copy of any
74088** such cells before overwriting the page data.
74089**
74090** The MemPage.nFree field is invalidated by this function. It is the
74091** responsibility of the caller to set it correctly.
74092*/
74093static int rebuildPage(
74094 CellArray *pCArray, /* Content to be added to page pPg */
74095 int iFirst, /* First cell in pCArray to use */
74096 int nCell, /* Final number of cells on page */
74097 MemPage *pPg /* The page to be reconstructed */
74098){
74099 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
74100 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
74101 const int usableSize = pPg->pBt->usableSize;
74102 u8 * const pEnd = &aData[usableSize];
74103 int i = iFirst; /* Which cell to copy from pCArray*/
74104 u32 j; /* Start of cell content area */
74105 int iEnd = i+nCell; /* Loop terminator */
74106 u8 *pCellptr = pPg->aCellIdx;
74107 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
74108 u8 *pData;
74109 int k; /* Current slot in pCArray->apEnd[] */
74110 u8 *pSrcEnd; /* Current pCArray->apEnd[k] value */
74111
74112 assert( i<iEnd )((void) (0));
74113 j = get2byte(&aData[hdr+5])((&aData[hdr+5])[0]<<8 | (&aData[hdr+5])[1]);
74114 if( j>(u32)usableSize ){ j = 0; }
74115 memcpy(&pTmp[j], &aData[j], usableSize - j);
74116
74117 for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2)(k<3*2); k++){}
74118 pSrcEnd = pCArray->apEnd[k];
74119
74120 pData = pEnd;
74121 while( 1/*exit by break*/ ){
74122 u8 *pCell = pCArray->apCell[i];
74123 u16 sz = pCArray->szCell[i];
74124 assert( sz>0 )((void) (0));
74125 if( SQLITE_WITHIN(pCell,aData+j,pEnd)(((uptr)(pCell)>=(uptr)(aData+j))&&((uptr)(pCell)<
(uptr)(pEnd)))
){
74126 if( ((uptr)(pCell+sz))>(uptr)pEnd ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(74126);
74127 pCell = &pTmp[pCell - aData];
74128 }else if( (uptr)(pCell+sz)>(uptr)pSrcEnd
74129 && (uptr)(pCell)<(uptr)pSrcEnd
74130 ){
74131 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(74131);
74132 }
74133
74134 pData -= sz;
74135 put2byte(pCellptr, (pData - aData))((pCellptr)[0] = (u8)(((pData - aData))>>8), (pCellptr)
[1] = (u8)((pData - aData)))
;
74136 pCellptr += 2;
74137 if( pData < pCellptr ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(74137);
74138 memmove(pData, pCell, sz);
74139 assert( sz==pPg->xCellSize(pPg, pCell) || CORRUPT_DB )((void) (0));
74140 i++;
74141 if( i>=iEnd ) break;
74142 if( pCArray->ixNx[k]<=i ){
74143 k++;
74144 pSrcEnd = pCArray->apEnd[k];
74145 }
74146 }
74147
74148 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
74149 pPg->nCell = nCell;
74150 pPg->nOverflow = 0;
74151
74152 put2byte(&aData[hdr+1], 0)((&aData[hdr+1])[0] = (u8)((0)>>8), (&aData[hdr
+1])[1] = (u8)(0))
;
74153 put2byte(&aData[hdr+3], pPg->nCell)((&aData[hdr+3])[0] = (u8)((pPg->nCell)>>8), (&
aData[hdr+3])[1] = (u8)(pPg->nCell))
;
74154 put2byte(&aData[hdr+5], pData - aData)((&aData[hdr+5])[0] = (u8)((pData - aData)>>8), (&
aData[hdr+5])[1] = (u8)(pData - aData))
;
74155 aData[hdr+7] = 0x00;
74156 return SQLITE_OK0;
74157}
74158
74159/*
74160** The pCArray objects contains pointers to b-tree cells and the cell sizes.
74161** This function attempts to add the cells stored in the array to page pPg.
74162** If it cannot (because the page needs to be defragmented before the cells
74163** will fit), non-zero is returned. Otherwise, if the cells are added
74164** successfully, zero is returned.
74165**
74166** Argument pCellptr points to the first entry in the cell-pointer array
74167** (part of page pPg) to populate. After cell apCell[0] is written to the
74168** page body, a 16-bit offset is written to pCellptr. And so on, for each
74169** cell in the array. It is the responsibility of the caller to ensure
74170** that it is safe to overwrite this part of the cell-pointer array.
74171**
74172** When this function is called, *ppData points to the start of the
74173** content area on page pPg. If the size of the content area is extended,
74174** *ppData is updated to point to the new start of the content area
74175** before returning.
74176**
74177** Finally, argument pBegin points to the byte immediately following the
74178** end of the space required by this page for the cell-pointer area (for
74179** all cells - not just those inserted by the current call). If the content
74180** area must be extended to before this point in order to accomodate all
74181** cells in apCell[], then the cells do not fit and non-zero is returned.
74182*/
74183static int pageInsertArray(
74184 MemPage *pPg, /* Page to add cells to */
74185 u8 *pBegin, /* End of cell-pointer array */
74186 u8 **ppData, /* IN/OUT: Page content-area pointer */
74187 u8 *pCellptr, /* Pointer to cell-pointer area */
74188 int iFirst, /* Index of first cell to add */
74189 int nCell, /* Number of cells to add to pPg */
74190 CellArray *pCArray /* Array of cells */
74191){
74192 int i = iFirst; /* Loop counter - cell index to insert */
74193 u8 *aData = pPg->aData; /* Complete page */
74194 u8 *pData = *ppData; /* Content area. A subset of aData[] */
74195 int iEnd = iFirst + nCell; /* End of loop. One past last cell to ins */
74196 int k; /* Current slot in pCArray->apEnd[] */
74197 u8 *pEnd; /* Maximum extent of cell data */
74198 assert( CORRUPT_DB || pPg->hdrOffset==0 )((void) (0)); /* Never called on page 1 */
74199 if( iEnd<=iFirst ) return 0;
74200 for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2)(k<3*2); k++){}
74201 pEnd = pCArray->apEnd[k];
74202 while( 1 /*Exit by break*/ ){
74203 int sz, rc;
74204 u8 *pSlot;
74205 assert( pCArray->szCell[i]!=0 )((void) (0));
74206 sz = pCArray->szCell[i];
74207 if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){
74208 if( (pData - pBegin)<sz ) return 1;
74209 pData -= sz;
74210 pSlot = pData;
74211 }
74212 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
74213 ** database. But they might for a corrupt database. Hence use memmove()
74214 ** since memcpy() sends SIGABORT with overlapping buffers on OpenBSD */
74215 assert( (pSlot+sz)<=pCArray->apCell[i]((void) (0))
74216 || pSlot>=(pCArray->apCell[i]+sz)((void) (0))
74217 || CORRUPT_DB )((void) (0));
74218 if( (uptr)(pCArray->apCell[i]+sz)>(uptr)pEnd
74219 && (uptr)(pCArray->apCell[i])<(uptr)pEnd
74220 ){
74221 assert( CORRUPT_DB )((void) (0));
74222 (void)SQLITE_CORRUPT_BKPTsqlite3CorruptError(74222);
74223 return 1;
74224 }
74225 memmove(pSlot, pCArray->apCell[i], sz);
74226 put2byte(pCellptr, (pSlot - aData))((pCellptr)[0] = (u8)(((pSlot - aData))>>8), (pCellptr)
[1] = (u8)((pSlot - aData)))
;
74227 pCellptr += 2;
74228 i++;
74229 if( i>=iEnd ) break;
74230 if( pCArray->ixNx[k]<=i ){
74231 k++;
74232 pEnd = pCArray->apEnd[k];
74233 }
74234 }
74235 *ppData = pData;
74236 return 0;
74237}
74238
74239/*
74240** The pCArray object contains pointers to b-tree cells and their sizes.
74241**
74242** This function adds the space associated with each cell in the array
74243** that is currently stored within the body of pPg to the pPg free-list.
74244** The cell-pointers and other fields of the page are not updated.
74245**
74246** This function returns the total number of cells added to the free-list.
74247*/
74248static int pageFreeArray(
74249 MemPage *pPg, /* Page to edit */
74250 int iFirst, /* First cell to delete */
74251 int nCell, /* Cells to delete */
74252 CellArray *pCArray /* Array of cells */
74253){
74254 u8 * const aData = pPg->aData;
74255 u8 * const pEnd = &aData[pPg->pBt->usableSize];
74256 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
74257 int nRet = 0;
74258 int i;
74259 int iEnd = iFirst + nCell;
74260 u8 *pFree = 0;
74261 int szFree = 0;
74262
74263 for(i=iFirst; i<iEnd; i++){
74264 u8 *pCell = pCArray->apCell[i];
74265 if( SQLITE_WITHIN(pCell, pStart, pEnd)(((uptr)(pCell)>=(uptr)(pStart))&&((uptr)(pCell)<
(uptr)(pEnd)))
){
74266 int sz;
74267 /* No need to use cachedCellSize() here. The sizes of all cells that
74268 ** are to be freed have already been computing while deciding which
74269 ** cells need freeing */
74270 sz = pCArray->szCell[i]; assert( sz>0 )((void) (0));
74271 if( pFree!=(pCell + sz) ){
74272 if( pFree ){
74273 assert( pFree>aData && (pFree - aData)<65536 )((void) (0));
74274 freeSpace(pPg, (u16)(pFree - aData), szFree);
74275 }
74276 pFree = pCell;
74277 szFree = sz;
74278 if( pFree+sz>pEnd ){
74279 return 0;
74280 }
74281 }else{
74282 pFree = pCell;
74283 szFree += sz;
74284 }
74285 nRet++;
74286 }
74287 }
74288 if( pFree ){
74289 assert( pFree>aData && (pFree - aData)<65536 )((void) (0));
74290 freeSpace(pPg, (u16)(pFree - aData), szFree);
74291 }
74292 return nRet;
74293}
74294
74295/*
74296** pCArray contains pointers to and sizes of all cells in the page being
74297** balanced. The current page, pPg, has pPg->nCell cells starting with
74298** pCArray->apCell[iOld]. After balancing, this page should hold nNew cells
74299** starting at apCell[iNew].
74300**
74301** This routine makes the necessary adjustments to pPg so that it contains
74302** the correct cells after being balanced.
74303**
74304** The pPg->nFree field is invalid when this function returns. It is the
74305** responsibility of the caller to set it correctly.
74306*/
74307static int editPage(
74308 MemPage *pPg, /* Edit this page */
74309 int iOld, /* Index of first cell currently on page */
74310 int iNew, /* Index of new first cell on page */
74311 int nNew, /* Final number of cells on page */
74312 CellArray *pCArray /* Array of cells and sizes */
74313){
74314 u8 * const aData = pPg->aData;
74315 const int hdr = pPg->hdrOffset;
74316 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
74317 int nCell = pPg->nCell; /* Cells stored on pPg */
74318 u8 *pData;
74319 u8 *pCellptr;
74320 int i;
74321 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
74322 int iNewEnd = iNew + nNew;
74323
74324#ifdef SQLITE_DEBUG
74325 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
74326 memcpy(pTmp, aData, pPg->pBt->usableSize);
74327#endif
74328
74329 /* Remove cells from the start and end of the page */
74330 assert( nCell>=0 )((void) (0));
74331 if( iOld<iNew ){
74332 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
74333 if( NEVER(nShift>nCell)(nShift>nCell) ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(74333);
74334 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
74335 nCell -= nShift;
74336 }
74337 if( iNewEnd < iOldEnd ){
74338 int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
74339 assert( nCell>=nTail )((void) (0));
74340 nCell -= nTail;
74341 }
74342
74343 pData = &aData[get2byteNotZero(&aData[hdr+5])(((((int)((&aData[hdr+5])[0]<<8 | (&aData[hdr+5
])[1]))-1)&0xffff)+1)
];
74344 if( pData<pBegin ) goto editpage_fail;
74345 if( pData>pPg->aDataEnd ) goto editpage_fail;
74346
74347 /* Add cells to the start of the page */
74348 if( iNew<iOld ){
74349 int nAdd = MIN(nNew,iOld-iNew)((nNew)<(iOld-iNew)?(nNew):(iOld-iNew));
74350 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB )((void) (0));
74351 assert( nAdd>=0 )((void) (0));
74352 pCellptr = pPg->aCellIdx;
74353 memmove(&pCellptr[nAdd*2], pCellptr, nCell*2);
74354 if( pageInsertArray(
74355 pPg, pBegin, &pData, pCellptr,
74356 iNew, nAdd, pCArray
74357 ) ) goto editpage_fail;
74358 nCell += nAdd;
74359 }
74360
74361 /* Add any overflow cells */
74362 for(i=0; i<pPg->nOverflow; i++){
74363 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
74364 if( iCell>=0 && iCell<nNew ){
74365 pCellptr = &pPg->aCellIdx[iCell * 2];
74366 if( nCell>iCell ){
74367 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
74368 }
74369 nCell++;
74370 cachedCellSize(pCArray, iCell+iNew);
74371 if( pageInsertArray(
74372 pPg, pBegin, &pData, pCellptr,
74373 iCell+iNew, 1, pCArray
74374 ) ) goto editpage_fail;
74375 }
74376 }
74377
74378 /* Append cells to the end of the page */
74379 assert( nCell>=0 )((void) (0));
74380 pCellptr = &pPg->aCellIdx[nCell*2];
74381 if( pageInsertArray(
74382 pPg, pBegin, &pData, pCellptr,
74383 iNew+nCell, nNew-nCell, pCArray
74384 ) ) goto editpage_fail;
74385
74386 pPg->nCell = nNew;
74387 pPg->nOverflow = 0;
74388
74389 put2byte(&aData[hdr+3], pPg->nCell)((&aData[hdr+3])[0] = (u8)((pPg->nCell)>>8), (&
aData[hdr+3])[1] = (u8)(pPg->nCell))
;
74390 put2byte(&aData[hdr+5], pData - aData)((&aData[hdr+5])[0] = (u8)((pData - aData)>>8), (&
aData[hdr+5])[1] = (u8)(pData - aData))
;
74391
74392#ifdef SQLITE_DEBUG
74393 for(i=0; i<nNew && !CORRUPT_DB(sqlite3Config.neverCorrupt==0); i++){
74394 u8 *pCell = pCArray->apCell[i+iNew];
74395 int iOff = get2byteAligned(&pPg->aCellIdx[i*2])((&pPg->aCellIdx[i*2])[0]<<8 | (&pPg->aCellIdx
[i*2])[1])
;
74396 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize])(((uptr)(pCell)>=(uptr)(aData))&&((uptr)(pCell)<
(uptr)(&aData[pPg->pBt->usableSize])))
){
74397 pCell = &pTmp[pCell - aData];
74398 }
74399 assert( 0==memcmp(pCell, &aData[iOff],((void) (0))
74400 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) )((void) (0));
74401 }
74402#endif
74403
74404 return SQLITE_OK0;
74405 editpage_fail:
74406 /* Unable to edit this page. Rebuild it from scratch instead. */
74407 populateCellCache(pCArray, iNew, nNew);
74408 return rebuildPage(pCArray, iNew, nNew, pPg);
74409}
74410
74411
74412#ifndef SQLITE_OMIT_QUICKBALANCE
74413/*
74414** This version of balance() handles the common special case where
74415** a new entry is being inserted on the extreme right-end of the
74416** tree, in other words, when the new entry will become the largest
74417** entry in the tree.
74418**
74419** Instead of trying to balance the 3 right-most leaf pages, just add
74420** a new page to the right-hand side and put the one new entry in
74421** that page. This leaves the right side of the tree somewhat
74422** unbalanced. But odds are that we will be inserting new entries
74423** at the end soon afterwards so the nearly empty page will quickly
74424** fill up. On average.
74425**
74426** pPage is the leaf page which is the right-most page in the tree.
74427** pParent is its parent. pPage must have a single overflow entry
74428** which is also the right-most entry on the page.
74429**
74430** The pSpace buffer is used to store a temporary copy of the divider
74431** cell that will be inserted into pParent. Such a cell consists of a 4
74432** byte page number followed by a variable length integer. In other
74433** words, at most 13 bytes. Hence the pSpace buffer must be at
74434** least 13 bytes in size.
74435*/
74436static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
74437 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
74438 MemPage *pNew; /* Newly allocated page */
74439 int rc; /* Return Code */
74440 Pgno pgnoNew; /* Page number of pNew */
74441
74442 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
74443 assert( sqlite3PagerIswriteable(pParent->pDbPage) )((void) (0));
74444 assert( pPage->nOverflow==1 )((void) (0));
74445
74446 if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(74446); /* dbfuzz001.test */
74447 assert( pPage->nFree>=0 )((void) (0));
74448 assert( pParent->nFree>=0 )((void) (0));
74449
74450 /* Allocate a new page. This page will become the right-sibling of
74451 ** pPage. Make the parent page writable, so that the new divider cell
74452 ** may be inserted. If both these operations are successful, proceed.
74453 */
74454 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
74455
74456 if( rc==SQLITE_OK0 ){
74457
74458 u8 *pOut = &pSpace[4];
74459 u8 *pCell = pPage->apOvfl[0];
74460 u16 szCell = pPage->xCellSize(pPage, pCell);
74461 u8 *pStop;
74462 CellArray b;
74463
74464 assert( sqlite3PagerIswriteable(pNew->pDbPage) )((void) (0));
74465 assert( CORRUPT_DB || pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) )((void) (0));
74466 zeroPage(pNew, PTF_INTKEY0x01|PTF_LEAFDATA0x04|PTF_LEAF0x08);
74467 b.nCell = 1;
74468 b.pRef = pPage;
74469 b.apCell = &pCell;
74470 b.szCell = &szCell;
74471 b.apEnd[0] = pPage->aDataEnd;
74472 b.ixNx[0] = 2;
74473 rc = rebuildPage(&b, 0, 1, pNew);
74474 if( NEVER(rc)(rc) ){
74475 releasePage(pNew);
74476 return rc;
74477 }
74478 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
74479
74480 /* If this is an auto-vacuum database, update the pointer map
74481 ** with entries for the new page, and any pointer from the
74482 ** cell on the page to an overflow page. If either of these
74483 ** operations fails, the return code is set, but the contents
74484 ** of the parent page are still manipulated by thh code below.
74485 ** That is Ok, at this point the parent page is guaranteed to
74486 ** be marked as dirty. Returning an error code will cause a
74487 ** rollback, undoing any changes made to the parent page.
74488 */
74489 if( ISAUTOVACUUM(pBt->autoVacuum) ){
74490 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE5, pParent->pgno, &rc);
74491 if( szCell>pNew->minLocal ){
74492 ptrmapPutOvflPtr(pNew, pNew, pCell, &rc);
74493 }
74494 }
74495
74496 /* Create a divider cell to insert into pParent. The divider cell
74497 ** consists of a 4-byte page number (the page number of pPage) and
74498 ** a variable length key value (which must be the same value as the
74499 ** largest key on pPage).
74500 **
74501 ** To find the largest key value on pPage, first find the right-most
74502 ** cell on pPage. The first two fields of this cell are the
74503 ** record-length (a variable length integer at most 32-bits in size)
74504 ** and the key value (a variable length integer, may have any value).
74505 ** The first of the while(...) loops below skips over the record-length
74506 ** field. The second while(...) loop copies the key value from the
74507 ** cell on pPage into the pSpace buffer.
74508 */
74509 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])))
;
74510 pStop = &pCell[9];
74511 while( (*(pCell++)&0x80) && pCell<pStop );
74512 pStop = &pCell[9];
74513 while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
74514
74515 /* Insert the new divider cell into pParent. */
74516 if( rc==SQLITE_OK0 ){
74517 insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
74518 0, pPage->pgno, &rc);
74519 }
74520
74521 /* Set the right-child pointer of pParent to point to the new page. */
74522 put4bytesqlite3Put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
74523
74524 /* Release the reference to the new page. */
74525 releasePage(pNew);
74526 }
74527
74528 return rc;
74529}
74530#endif /* SQLITE_OMIT_QUICKBALANCE */
74531
74532#if 0
74533/*
74534** This function does not contribute anything to the operation of SQLite.
74535** it is sometimes activated temporarily while debugging code responsible
74536** for setting pointer-map entries.
74537*/
74538static int ptrmapCheckPages(MemPage **apPage, int nPage){
74539 int i, j;
74540 for(i=0; i<nPage; i++){
74541 Pgno n;
74542 u8 e;
74543 MemPage *pPage = apPage[i];
74544 BtShared *pBt = pPage->pBt;
74545 assert( pPage->isInit )((void) (0));
74546
74547 for(j=0; j<pPage->nCell; j++){
74548 CellInfo info;
74549 u8 *z;
74550
74551 z = findCell(pPage, j)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(j)])[0]<<8 | (&(pPage)->aCellIdx
[2*(j)])[1])))
;
74552 pPage->xParseCell(pPage, z, &info);
74553 if( info.nLocal<info.nPayload ){
74554 Pgno ovfl = get4bytesqlite3Get4byte(&z[info.nSize-4]);
74555 ptrmapGet(pBt, ovfl, &e, &n);
74556 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 )((void) (0));
74557 }
74558 if( !pPage->leaf ){
74559 Pgno child = get4bytesqlite3Get4byte(z);
74560 ptrmapGet(pBt, child, &e, &n);
74561 assert( n==pPage->pgno && e==PTRMAP_BTREE )((void) (0));
74562 }
74563 }
74564 if( !pPage->leaf ){
74565 Pgno child = get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]);
74566 ptrmapGet(pBt, child, &e, &n);
74567 assert( n==pPage->pgno && e==PTRMAP_BTREE )((void) (0));
74568 }
74569 }
74570 return 1;
74571}
74572#endif
74573
74574/*
74575** This function is used to copy the contents of the b-tree node stored
74576** on page pFrom to page pTo. If page pFrom was not a leaf page, then
74577** the pointer-map entries for each child page are updated so that the
74578** parent page stored in the pointer map is page pTo. If pFrom contained
74579** any cells with overflow page pointers, then the corresponding pointer
74580** map entries are also updated so that the parent page is page pTo.
74581**
74582** If pFrom is currently carrying any overflow cells (entries in the
74583** MemPage.apOvfl[] array), they are not copied to pTo.
74584**
74585** Before returning, page pTo is reinitialized using btreeInitPage().
74586**
74587** The performance of this function is not critical. It is only used by
74588** the balance_shallower() and balance_deeper() procedures, neither of
74589** which are called often under normal circumstances.
74590*/
74591static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
74592 if( (*pRC)==SQLITE_OK0 ){
74593 BtShared * const pBt = pFrom->pBt;
74594 u8 * const aFrom = pFrom->aData;
74595 u8 * const aTo = pTo->aData;
74596 int const iFromHdr = pFrom->hdrOffset;
74597 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
74598 int rc;
74599 int iData;
74600
74601
74602 assert( pFrom->isInit )((void) (0));
74603 assert( pFrom->nFree>=iToHdr )((void) (0));
74604 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize )((void) (0));
74605
74606 /* Copy the b-tree node content from page pFrom to page pTo. */
74607 iData = get2byte(&aFrom[iFromHdr+5])((&aFrom[iFromHdr+5])[0]<<8 | (&aFrom[iFromHdr+
5])[1])
;
74608 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
74609 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
74610
74611 /* Reinitialize page pTo so that the contents of the MemPage structure
74612 ** match the new data. The initialization of pTo can actually fail under
74613 ** fairly obscure circumstances, even though it is a copy of initialized
74614 ** page pFrom.
74615 */
74616 pTo->isInit = 0;
74617 rc = btreeInitPage(pTo);
74618 if( rc==SQLITE_OK0 ) rc = btreeComputeFreeSpace(pTo);
74619 if( rc!=SQLITE_OK0 ){
74620 *pRC = rc;
74621 return;
74622 }
74623
74624 /* If this is an auto-vacuum database, update the pointer-map entries
74625 ** for any b-tree or overflow pages that pTo now contains the pointers to.
74626 */
74627 if( ISAUTOVACUUM(pBt->autoVacuum) ){
74628 *pRC = setChildPtrmaps(pTo);
74629 }
74630 }
74631}
74632
74633/*
74634** This routine redistributes cells on the iParentIdx'th child of pParent
74635** (hereafter "the page") and up to 2 siblings so that all pages have about the
74636** same amount of free space. Usually a single sibling on either side of the
74637** page are used in the balancing, though both siblings might come from one
74638** side if the page is the first or last child of its parent. If the page
74639** has fewer than 2 siblings (something which can only happen if the page
74640** is a root page or a child of a root page) then all available siblings
74641** participate in the balancing.
74642**
74643** The number of siblings of the page might be increased or decreased by
74644** one or two in an effort to keep pages nearly full but not over full.
74645**
74646** Note that when this routine is called, some of the cells on the page
74647** might not actually be stored in MemPage.aData[]. This can happen
74648** if the page is overfull. This routine ensures that all cells allocated
74649** to the page and its siblings fit into MemPage.aData[] before returning.
74650**
74651** In the course of balancing the page and its siblings, cells may be
74652** inserted into or removed from the parent page (pParent). Doing so
74653** may cause the parent page to become overfull or underfull. If this
74654** happens, it is the responsibility of the caller to invoke the correct
74655** balancing routine to fix this problem (see the balance() routine).
74656**
74657** If this routine fails for any reason, it might leave the database
74658** in a corrupted state. So if this routine fails, the database should
74659** be rolled back.
74660**
74661** The third argument to this function, aOvflSpace, is a pointer to a
74662** buffer big enough to hold one page. If while inserting cells into the parent
74663** page (pParent) the parent page becomes overfull, this buffer is
74664** used to store the parent's overflow cells. Because this function inserts
74665** a maximum of four divider cells into the parent page, and the maximum
74666** size of a cell stored within an internal node is always less than 1/4
74667** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
74668** enough for all overflow cells.
74669**
74670** If aOvflSpace is set to a null pointer, this function returns
74671** SQLITE_NOMEM.
74672*/
74673static int balance_nonroot(
74674 MemPage *pParent, /* Parent page of siblings being balanced */
74675 int iParentIdx, /* Index of "the page" in pParent */
74676 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
74677 int isRoot, /* True if pParent is a root-page */
74678 int bBulk /* True if this call is part of a bulk load */
74679){
74680 BtShared *pBt; /* The whole database */
74681 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
74682 int nNew = 0; /* Number of pages in apNew[] */
74683 int nOld; /* Number of pages in apOld[] */
74684 int i, j, k; /* Loop counters */
74685 int nxDiv; /* Next divider slot in pParent->aCell[] */
74686 int rc = SQLITE_OK0; /* The return code */
74687 u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */
74688 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
74689 int usableSpace; /* Bytes in pPage beyond the header */
74690 int pageFlags; /* Value of pPage->aData[0] */
74691 int iSpace1 = 0; /* First unused byte of aSpace1[] */
74692 int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
74693 int szScratch; /* Size of scratch memory requested */
74694 MemPage *apOld[NB3]; /* pPage and up to two siblings */
74695 MemPage *apNew[NB3+2]; /* pPage and up to NB siblings after balancing */
74696 u8 *pRight; /* Location in parent of right-sibling pointer */
74697 u8 *apDiv[NB3-1]; /* Divider cells in pParent */
74698 int cntNew[NB3+2]; /* Index in b.paCell[] of cell after i-th page */
74699 int cntOld[NB3+2]; /* Old index in b.apCell[] */
74700 int szNew[NB3+2]; /* Combined size of cells placed on i-th page */
74701 u8 *aSpace1; /* Space for copies of dividers cells */
74702 Pgno pgno; /* Temp var to store a page number in */
74703 u8 abDone[NB3+2]; /* True after i'th new page is populated */
74704 Pgno aPgno[NB3+2]; /* Page numbers of new pages before shuffling */
74705 Pgno aPgOrder[NB3+2]; /* Copy of aPgno[] used for sorting pages */
74706 u16 aPgFlags[NB3+2]; /* flags field of new pages before shuffling */
74707 CellArray b; /* Parsed information on cells being balanced */
74708
74709 memset(abDone, 0, sizeof(abDone));
74710 memset(&b, 0, sizeof(b));
74711 pBt = pParent->pBt;
74712 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
74713 assert( sqlite3PagerIswriteable(pParent->pDbPage) )((void) (0));
74714
74715 /* At this point pParent may have at most one overflow cell. And if
74716 ** this overflow cell is present, it must be the cell with
74717 ** index iParentIdx. This scenario comes about when this function
74718 ** is called (indirectly) from sqlite3BtreeDelete().
74719 */
74720 assert( pParent->nOverflow==0 || pParent->nOverflow==1 )((void) (0));
74721 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx )((void) (0));
74722
74723 if( !aOvflSpace ){
74724 return SQLITE_NOMEM_BKPT7;
74725 }
74726 assert( pParent->nFree>=0 )((void) (0));
74727
74728 /* Find the sibling pages to balance. Also locate the cells in pParent
74729 ** that divide the siblings. An attempt is made to find NN siblings on
74730 ** either side of pPage. More siblings are taken from one side, however,
74731 ** if there are fewer than NN siblings on the other side. If pParent
74732 ** has NB or fewer children then all children of pParent are taken.
74733 **
74734 ** This loop also drops the divider cells from the parent page. This
74735 ** way, the remainder of the function does not have to deal with any
74736 ** overflow cells in the parent page, since if any existed they will
74737 ** have already been removed.
74738 */
74739 i = pParent->nOverflow + pParent->nCell;
74740 if( i<2 ){
74741 nxDiv = 0;
74742 }else{
74743 assert( bBulk==0 || bBulk==1 )((void) (0));
74744 if( iParentIdx==0 ){
74745 nxDiv = 0;
74746 }else if( iParentIdx==i ){
74747 nxDiv = i-2+bBulk;
74748 }else{
74749 nxDiv = iParentIdx-1;
74750 }
74751 i = 2-bBulk;
74752 }
74753 nOld = i+1;
74754 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
74755 pRight = &pParent->aData[pParent->hdrOffset+8];
74756 }else{
74757 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])))
;
74758 }
74759 pgno = get4bytesqlite3Get4byte(pRight);
74760 while( 1 ){
74761 if( rc==SQLITE_OK0 ){
74762 rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0);
74763 }
74764 if( rc ){
74765 memset(apOld, 0, (i+1)*sizeof(MemPage*));
74766 goto balance_cleanup;
74767 }
74768 if( apOld[i]->nFree<0 ){
74769 rc = btreeComputeFreeSpace(apOld[i]);
74770 if( rc ){
74771 memset(apOld, 0, (i)*sizeof(MemPage*));
74772 goto balance_cleanup;
74773 }
74774 }
74775 nMaxCells += apOld[i]->nCell + ArraySize(pParent->apOvfl)((int)(sizeof(pParent->apOvfl)/sizeof(pParent->apOvfl[0
])))
;
74776 if( (i--)==0 ) break;
74777
74778 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
74779 apDiv[i] = pParent->apOvfl[0];
74780 pgno = get4bytesqlite3Get4byte(apDiv[i]);
74781 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
74782 pParent->nOverflow = 0;
74783 }else{
74784 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])))
;
74785 pgno = get4bytesqlite3Get4byte(apDiv[i]);
74786 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
74787
74788 /* Drop the cell from the parent page. apDiv[i] still points to
74789 ** the cell within the parent, even though it has been dropped.
74790 ** This is safe because dropping a cell only overwrites the first
74791 ** four bytes of it, and this function does not need the first
74792 ** four bytes of the divider cell. So the pointer is safe to use
74793 ** later on.
74794 **
74795 ** But not if we are in secure-delete mode. In secure-delete mode,
74796 ** the dropCell() routine will overwrite the entire cell with zeroes.
74797 ** In this case, temporarily copy the cell into the aOvflSpace[]
74798 ** buffer. It will be copied out again as soon as the aSpace[] buffer
74799 ** is allocated. */
74800 if( pBt->btsFlags & BTS_FAST_SECURE0x000c ){
74801 int iOff;
74802
74803 /* If the following if() condition is not true, the db is corrupted.
74804 ** The call to dropCell() below will detect this. */
74805 iOff = SQLITE_PTR_TO_INT(apDiv[i])((int)(long int)(apDiv[i])) - SQLITE_PTR_TO_INT(pParent->aData)((int)(long int)(pParent->aData));
74806 if( (iOff+szNew[i])<=(int)pBt->usableSize ){
74807 memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
74808 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
74809 }
74810 }
74811 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
74812 }
74813 }
74814
74815 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
74816 ** alignment */
74817 nMaxCells = (nMaxCells + 3)&~3;
74818
74819 /*
74820 ** Allocate space for memory structures
74821 */
74822 szScratch =
74823 nMaxCells*sizeof(u8*) /* b.apCell */
74824 + nMaxCells*sizeof(u16) /* b.szCell */
74825 + pBt->pageSize; /* aSpace1 */
74826
74827 assert( szScratch<=7*(int)pBt->pageSize )((void) (0));
74828 b.apCell = sqlite3StackAllocRaw(0, szScratch )sqlite3DbMallocRaw(0,szScratch);
74829 if( b.apCell==0 ){
74830 rc = SQLITE_NOMEM_BKPT7;
74831 goto balance_cleanup;
74832 }
74833 b.szCell = (u16*)&b.apCell[nMaxCells];
74834 aSpace1 = (u8*)&b.szCell[nMaxCells];
74835 assert( EIGHT_BYTE_ALIGNMENT(aSpace1) )((void) (0));
74836
74837 /*
74838 ** Load pointers to all cells on sibling pages and the divider cells
74839 ** into the local b.apCell[] array. Make copies of the divider cells
74840 ** into space obtained from aSpace1[]. The divider cells have already
74841 ** been removed from pParent.
74842 **
74843 ** If the siblings are on leaf pages, then the child pointers of the
74844 ** divider cells are stripped from the cells before they are copied
74845 ** into aSpace1[]. In this way, all cells in b.apCell[] are without
74846 ** child pointers. If siblings are not leaves, then all cell in
74847 ** b.apCell[] include child pointers. Either way, all cells in b.apCell[]
74848 ** are alike.
74849 **
74850 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
74851 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
74852 */
74853 b.pRef = apOld[0];
74854 leafCorrection = b.pRef->leaf*4;
74855 leafData = b.pRef->intKeyLeaf;
74856 for(i=0; i<nOld; i++){
74857 MemPage *pOld = apOld[i];
74858 int limit = pOld->nCell;
74859 u8 *aData = pOld->aData;
74860 u16 maskPage = pOld->maskPage;
74861 u8 *piCell = aData + pOld->cellOffset;
74862 u8 *piEnd;
74863 VVA_ONLY( int nCellAtStart = b.nCell; )
74864
74865 /* Verify that all sibling pages are of the same "type" (table-leaf,
74866 ** table-interior, index-leaf, or index-interior).
74867 */
74868 if( pOld->aData[0]!=apOld[0]->aData[0] ){
74869 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(74869);
74870 goto balance_cleanup;
74871 }
74872
74873 /* Load b.apCell[] with pointers to all cells in pOld. If pOld
74874 ** contains overflow cells, include them in the b.apCell[] array
74875 ** in the correct spot.
74876 **
74877 ** Note that when there are multiple overflow cells, it is always the
74878 ** case that they are sequential and adjacent. This invariant arises
74879 ** because multiple overflows can only occurs when inserting divider
74880 ** cells into a parent on a prior balance, and divider cells are always
74881 ** adjacent and are inserted in order. There is an assert() tagged
74882 ** with "NOTE 1" in the overflow cell insertion loop to prove this
74883 ** invariant.
74884 **
74885 ** This must be done in advance. Once the balance starts, the cell
74886 ** offset section of the btree page will be overwritten and we will no
74887 ** long be able to find the cells if a pointer to each cell is not saved
74888 ** first.
74889 */
74890 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
74891 if( pOld->nOverflow>0 ){
74892 if( NEVER(limit<pOld->aiOvfl[0])(limit<pOld->aiOvfl[0]) ){
74893 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(74893);
74894 goto balance_cleanup;
74895 }
74896 limit = pOld->aiOvfl[0];
74897 for(j=0; j<limit; j++){
74898 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell)((piCell)[0]<<8 | (piCell)[1]));
74899 piCell += 2;
74900 b.nCell++;
74901 }
74902 for(k=0; k<pOld->nOverflow; k++){
74903 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] )((void) (0));/* NOTE 1 */
74904 b.apCell[b.nCell] = pOld->apOvfl[k];
74905 b.nCell++;
74906 }
74907 }
74908 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
74909 while( piCell<piEnd ){
74910 assert( b.nCell<nMaxCells )((void) (0));
74911 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell)((piCell)[0]<<8 | (piCell)[1]));
74912 piCell += 2;
74913 b.nCell++;
74914 }
74915 assert( (b.nCell-nCellAtStart)==(pOld->nCell+pOld->nOverflow) )((void) (0));
74916
74917 cntOld[i] = b.nCell;
74918 if( i<nOld-1 && !leafData){
74919 u16 sz = (u16)szNew[i];
74920 u8 *pTemp;
74921 assert( b.nCell<nMaxCells )((void) (0));
74922 b.szCell[b.nCell] = sz;
74923 pTemp = &aSpace1[iSpace1];
74924 iSpace1 += sz;
74925 assert( sz<=pBt->maxLocal+23 )((void) (0));
74926 assert( iSpace1 <= (int)pBt->pageSize )((void) (0));
74927 memcpy(pTemp, apDiv[i], sz);
74928 b.apCell[b.nCell] = pTemp+leafCorrection;
74929 assert( leafCorrection==0 || leafCorrection==4 )((void) (0));
74930 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
74931 if( !pOld->leaf ){
74932 assert( leafCorrection==0 )((void) (0));
74933 assert( pOld->hdrOffset==0 || CORRUPT_DB )((void) (0));
74934 /* The right pointer of the child page pOld becomes the left
74935 ** pointer of the divider cell */
74936 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
74937 }else{
74938 assert( leafCorrection==4 )((void) (0));
74939 while( b.szCell[b.nCell]<4 ){
74940 /* Do not allow any cells smaller than 4 bytes. If a smaller cell
74941 ** does exist, pad it with 0x00 bytes. */
74942 assert( b.szCell[b.nCell]==3 || CORRUPT_DB )((void) (0));
74943 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB )((void) (0));
74944 aSpace1[iSpace1++] = 0x00;
74945 b.szCell[b.nCell]++;
74946 }
74947 }
74948 b.nCell++;
74949 }
74950 }
74951
74952 /*
74953 ** Figure out the number of pages needed to hold all b.nCell cells.
74954 ** Store this number in "k". Also compute szNew[] which is the total
74955 ** size of all cells on the i-th page and cntNew[] which is the index
74956 ** in b.apCell[] of the cell that divides page i from page i+1.
74957 ** cntNew[k] should equal b.nCell.
74958 **
74959 ** Values computed by this block:
74960 **
74961 ** k: The total number of sibling pages
74962 ** szNew[i]: Spaced used on the i-th sibling page.
74963 ** cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to
74964 ** the right of the i-th sibling page.
74965 ** usableSpace: Number of bytes of space available on each sibling.
74966 **
74967 */
74968 usableSpace = pBt->usableSize - 12 + leafCorrection;
74969 for(i=k=0; i<nOld; i++, k++){
74970 MemPage *p = apOld[i];
74971 b.apEnd[k] = p->aDataEnd;
74972 b.ixNx[k] = cntOld[i];
74973 if( k && b.ixNx[k]==b.ixNx[k-1] ){
74974 k--; /* Omit b.ixNx[] entry for child pages with no cells */
74975 }
74976 if( !leafData ){
74977 k++;
74978 b.apEnd[k] = pParent->aDataEnd;
74979 b.ixNx[k] = cntOld[i]+1;
74980 }
74981 assert( p->nFree>=0 )((void) (0));
74982 szNew[i] = usableSpace - p->nFree;
74983 for(j=0; j<p->nOverflow; j++){
74984 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
74985 }
74986 cntNew[i] = cntOld[i];
74987 }
74988 k = nOld;
74989 for(i=0; i<k; i++){
74990 int sz;
74991 while( szNew[i]>usableSpace ){
74992 if( i+1>=k ){
74993 k = i+2;
74994 if( k>NB3+2 ){ rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(74994); goto balance_cleanup; }
74995 szNew[k-1] = 0;
74996 cntNew[k-1] = b.nCell;
74997 }
74998 sz = 2 + cachedCellSize(&b, cntNew[i]-1);
74999 szNew[i] -= sz;
75000 if( !leafData ){
75001 if( cntNew[i]<b.nCell ){
75002 sz = 2 + cachedCellSize(&b, cntNew[i]);
75003 }else{
75004 sz = 0;
75005 }
75006 }
75007 szNew[i+1] += sz;
75008 cntNew[i]--;
75009 }
75010 while( cntNew[i]<b.nCell ){
75011 sz = 2 + cachedCellSize(&b, cntNew[i]);
75012 if( szNew[i]+sz>usableSpace ) break;
75013 szNew[i] += sz;
75014 cntNew[i]++;
75015 if( !leafData ){
75016 if( cntNew[i]<b.nCell ){
75017 sz = 2 + cachedCellSize(&b, cntNew[i]);
75018 }else{
75019 sz = 0;
75020 }
75021 }
75022 szNew[i+1] -= sz;
75023 }
75024 if( cntNew[i]>=b.nCell ){
75025 k = i+1;
75026 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
75027 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(75027);
75028 goto balance_cleanup;
75029 }
75030 }
75031
75032 /*
75033 ** The packing computed by the previous block is biased toward the siblings
75034 ** on the left side (siblings with smaller keys). The left siblings are
75035 ** always nearly full, while the right-most sibling might be nearly empty.
75036 ** The next block of code attempts to adjust the packing of siblings to
75037 ** get a better balance.
75038 **
75039 ** This adjustment is more than an optimization. The packing above might
75040 ** be so out of balance as to be illegal. For example, the right-most
75041 ** sibling might be completely empty. This adjustment is not optional.
75042 */
75043 for(i=k-1; i>0; i--){
75044 int szRight = szNew[i]; /* Size of sibling on the right */
75045 int szLeft = szNew[i-1]; /* Size of sibling on the left */
75046 int r; /* Index of right-most cell in left sibling */
75047 int d; /* Index of first cell to the left of right sibling */
75048
75049 r = cntNew[i-1] - 1;
75050 d = r + 1 - leafData;
75051 (void)cachedCellSize(&b, d);
75052 do{
75053 assert( d<nMaxCells )((void) (0));
75054 assert( r<nMaxCells )((void) (0));
75055 (void)cachedCellSize(&b, r);
75056 if( szRight!=0
75057 && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+(i==k-1?0:2)))){
75058 break;
75059 }
75060 szRight += b.szCell[d] + 2;
75061 szLeft -= b.szCell[r] + 2;
75062 cntNew[i-1] = r;
75063 r--;
75064 d--;
75065 }while( r>=0 );
75066 szNew[i] = szRight;
75067 szNew[i-1] = szLeft;
75068 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
75069 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(75069);
75070 goto balance_cleanup;
75071 }
75072 }
75073
75074 /* Sanity check: For a non-corrupt database file one of the follwing
75075 ** must be true:
75076 ** (1) We found one or more cells (cntNew[0])>0), or
75077 ** (2) pPage is a virtual root page. A virtual root page is when
75078 ** the real root page is page 1 and we are the only child of
75079 ** that page.
75080 */
75081 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB)((void) (0));
75082 TRACE(("BALANCE: old: %d(nc=%d) %d(nc=%d) %d(nc=%d)\n",
75083 apOld[0]->pgno, apOld[0]->nCell,
75084 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
75085 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
75086 ));
75087
75088 /*
75089 ** Allocate k new pages. Reuse old pages where possible.
75090 */
75091 pageFlags = apOld[0]->aData[0];
75092 for(i=0; i<k; i++){
75093 MemPage *pNew;
75094 if( i<nOld ){
75095 pNew = apNew[i] = apOld[i];
75096 apOld[i] = 0;
75097 rc = sqlite3PagerWrite(pNew->pDbPage);
75098 nNew++;
75099 if( sqlite3PagerPageRefcount(pNew->pDbPage)!=1+(i==(iParentIdx-nxDiv))
75100 && rc==SQLITE_OK0
75101 ){
75102 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(75102);
75103 }
75104 if( rc ) goto balance_cleanup;
75105 }else{
75106 assert( i>0 )((void) (0));
75107 rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
75108 if( rc ) goto balance_cleanup;
75109 zeroPage(pNew, pageFlags);
75110 apNew[i] = pNew;
75111 nNew++;
75112 cntOld[i] = b.nCell;
75113
75114 /* Set the pointer-map entry for the new sibling page. */
75115 if( ISAUTOVACUUM(pBt->autoVacuum) ){
75116 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE5, pParent->pgno, &rc);
75117 if( rc!=SQLITE_OK0 ){
75118 goto balance_cleanup;
75119 }
75120 }
75121 }
75122 }
75123
75124 /*
75125 ** Reassign page numbers so that the new pages are in ascending order.
75126 ** This helps to keep entries in the disk file in order so that a scan
75127 ** of the table is closer to a linear scan through the file. That in turn
75128 ** helps the operating system to deliver pages from the disk more rapidly.
75129 **
75130 ** An O(n^2) insertion sort algorithm is used, but since n is never more
75131 ** than (NB+2) (a small constant), that should not be a problem.
75132 **
75133 ** When NB==3, this one optimization makes the database about 25% faster
75134 ** for large insertions and deletions.
75135 */
75136 for(i=0; i<nNew; i++){
75137 aPgOrder[i] = aPgno[i] = apNew[i]->pgno;
75138 aPgFlags[i] = apNew[i]->pDbPage->flags;
75139 for(j=0; j<i; j++){
75140 if( NEVER(aPgno[j]==aPgno[i])(aPgno[j]==aPgno[i]) ){
75141 /* This branch is taken if the set of sibling pages somehow contains
75142 ** duplicate entries. This can happen if the database is corrupt.
75143 ** It would be simpler to detect this as part of the loop below, but
75144 ** we do the detection here in order to avoid populating the pager
75145 ** cache with two separate objects associated with the same
75146 ** page number. */
75147 assert( CORRUPT_DB )((void) (0));
75148 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(75148);
75149 goto balance_cleanup;
75150 }
75151 }
75152 }
75153 for(i=0; i<nNew; i++){
75154 int iBest = 0; /* aPgno[] index of page number to use */
75155 for(j=1; j<nNew; j++){
75156 if( aPgOrder[j]<aPgOrder[iBest] ) iBest = j;
75157 }
75158 pgno = aPgOrder[iBest];
75159 aPgOrder[iBest] = 0xffffffff;
75160 if( iBest!=i ){
75161 if( iBest>i ){
75162 sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0);
75163 }
75164 sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]);
75165 apNew[i]->pgno = pgno;
75166 }
75167 }
75168
75169 TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) "
75170 "%d(%d nc=%d) %d(%d nc=%d)\n",
75171 apNew[0]->pgno, szNew[0], cntNew[0],
75172 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
75173 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
75174 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
75175 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
75176 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
75177 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
75178 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
75179 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
75180 ));
75181
75182 assert( sqlite3PagerIswriteable(pParent->pDbPage) )((void) (0));
75183 assert( nNew>=1 && nNew<=ArraySize(apNew) )((void) (0));
75184 assert( apNew[nNew-1]!=0 )((void) (0));
75185 put4bytesqlite3Put4byte(pRight, apNew[nNew-1]->pgno);
75186
75187 /* If the sibling pages are not leaves, ensure that the right-child pointer
75188 ** of the right-most new sibling page is set to the value that was
75189 ** originally in the same field of the right-most old sibling page. */
75190 if( (pageFlags & PTF_LEAF0x08)==0 && nOld!=nNew ){
75191 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
75192 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
75193 }
75194
75195 /* Make any required updates to pointer map entries associated with
75196 ** cells stored on sibling pages following the balance operation. Pointer
75197 ** map entries associated with divider cells are set by the insertCell()
75198 ** routine. The associated pointer map entries are:
75199 **
75200 ** a) if the cell contains a reference to an overflow chain, the
75201 ** entry associated with the first page in the overflow chain, and
75202 **
75203 ** b) if the sibling pages are not leaves, the child page associated
75204 ** with the cell.
75205 **
75206 ** If the sibling pages are not leaves, then the pointer map entry
75207 ** associated with the right-child of each sibling may also need to be
75208 ** updated. This happens below, after the sibling pages have been
75209 ** populated, not here.
75210 */
75211 if( ISAUTOVACUUM(pBt->autoVacuum) ){
75212 MemPage *pOld;
75213 MemPage *pNew = pOld = apNew[0];
75214 int cntOldNext = pNew->nCell + pNew->nOverflow;
75215 int iNew = 0;
75216 int iOld = 0;
75217
75218 for(i=0; i<b.nCell; i++){
75219 u8 *pCell = b.apCell[i];
75220 while( i==cntOldNext ){
75221 iOld++;
75222 assert( iOld<nNew || iOld<nOld )((void) (0));
75223 assert( iOld>=0 && iOld<NB )((void) (0));
75224 pOld = iOld<nNew ? apNew[iOld] : apOld[iOld];
75225 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
75226 }
75227 if( i==cntNew[iNew] ){
75228 pNew = apNew[++iNew];
75229 if( !leafData ) continue;
75230 }
75231
75232 /* Cell pCell is destined for new sibling page pNew. Originally, it
75233 ** was either part of sibling page iOld (possibly an overflow cell),
75234 ** or else the divider cell to the left of sibling page iOld. So,
75235 ** if sibling page iOld had the same page number as pNew, and if
75236 ** pCell really was a part of sibling page iOld (not a divider or
75237 ** overflow cell), we can skip updating the pointer map entries. */
75238 if( iOld>=nNew
75239 || pNew->pgno!=aPgno[iOld]
75240 || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd)(((uptr)(pCell)>=(uptr)(pOld->aData))&&((uptr)(
pCell)<(uptr)(pOld->aDataEnd)))
75241 ){
75242 if( !leafCorrection ){
75243 ptrmapPut(pBt, get4bytesqlite3Get4byte(pCell), PTRMAP_BTREE5, pNew->pgno, &rc);
75244 }
75245 if( cachedCellSize(&b,i)>pNew->minLocal ){
75246 ptrmapPutOvflPtr(pNew, pOld, pCell, &rc);
75247 }
75248 if( rc ) goto balance_cleanup;
75249 }
75250 }
75251 }
75252
75253 /* Insert new divider cells into pParent. */
75254 for(i=0; i<nNew-1; i++){
75255 u8 *pCell;
75256 u8 *pTemp;
75257 int sz;
75258 u8 *pSrcEnd;
75259 MemPage *pNew = apNew[i];
75260 j = cntNew[i];
75261
75262 assert( j<nMaxCells )((void) (0));
75263 assert( b.apCell[j]!=0 )((void) (0));
75264 pCell = b.apCell[j];
75265 sz = b.szCell[j] + leafCorrection;
75266 pTemp = &aOvflSpace[iOvflSpace];
75267 if( !pNew->leaf ){
75268 memcpy(&pNew->aData[8], pCell, 4);
75269 }else if( leafData ){
75270 /* If the tree is a leaf-data tree, and the siblings are leaves,
75271 ** then there is no divider cell in b.apCell[]. Instead, the divider
75272 ** cell consists of the integer key for the right-most cell of
75273 ** the sibling-page assembled above only.
75274 */
75275 CellInfo info;
75276 j--;
75277 pNew->xParseCell(pNew, b.apCell[j], &info);
75278 pCell = pTemp;
75279 sz = 4 + putVarintsqlite3PutVarint(&pCell[4], info.nKey);
75280 pTemp = 0;
75281 }else{
75282 pCell -= 4;
75283 /* Obscure case for non-leaf-data trees: If the cell at pCell was
75284 ** previously stored on a leaf node, and its reported size was 4
75285 ** bytes, then it may actually be smaller than this
75286 ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
75287 ** any cell). But it is important to pass the correct size to
75288 ** insertCell(), so reparse the cell now.
75289 **
75290 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
75291 ** and WITHOUT ROWID tables with exactly one column which is the
75292 ** primary key.
75293 */
75294 if( b.szCell[j]==4 ){
75295 assert(leafCorrection==4)((void) (0));
75296 sz = pParent->xCellSize(pParent, pCell);
75297 }
75298 }
75299 iOvflSpace += sz;
75300 assert( sz<=pBt->maxLocal+23 )((void) (0));
75301 assert( iOvflSpace <= (int)pBt->pageSize )((void) (0));
75302 for(k=0; b.ixNx[k]<=j && ALWAYS(k<NB*2)(k<3*2); k++){}
75303 pSrcEnd = b.apEnd[k];
75304 if( SQLITE_WITHIN(pSrcEnd, pCell, pCell+sz)(((uptr)(pSrcEnd)>=(uptr)(pCell))&&((uptr)(pSrcEnd
)<(uptr)(pCell+sz)))
){
75305 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(75305);
75306 goto balance_cleanup;
75307 }
75308 insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc);
75309 if( rc!=SQLITE_OK0 ) goto balance_cleanup;
75310 assert( sqlite3PagerIswriteable(pParent->pDbPage) )((void) (0));
75311 }
75312
75313 /* Now update the actual sibling pages. The order in which they are updated
75314 ** is important, as this code needs to avoid disrupting any page from which
75315 ** cells may still to be read. In practice, this means:
75316 **
75317 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
75318 ** then it is not safe to update page apNew[iPg] until after
75319 ** the left-hand sibling apNew[iPg-1] has been updated.
75320 **
75321 ** (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1])
75322 ** then it is not safe to update page apNew[iPg] until after
75323 ** the right-hand sibling apNew[iPg+1] has been updated.
75324 **
75325 ** If neither of the above apply, the page is safe to update.
75326 **
75327 ** The iPg value in the following loop starts at nNew-1 goes down
75328 ** to 0, then back up to nNew-1 again, thus making two passes over
75329 ** the pages. On the initial downward pass, only condition (1) above
75330 ** needs to be tested because (2) will always be true from the previous
75331 ** step. On the upward pass, both conditions are always true, so the
75332 ** upwards pass simply processes pages that were missed on the downward
75333 ** pass.
75334 */
75335 for(i=1-nNew; i<nNew; i++){
75336 int iPg = i<0 ? -i : i;
75337 assert( iPg>=0 && iPg<nNew )((void) (0));
75338 if( abDone[iPg] ) continue; /* Skip pages already processed */
75339 if( i>=0 /* On the upwards pass, or... */
75340 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
75341 ){
75342 int iNew;
75343 int iOld;
75344 int nNewCell;
75345
75346 /* Verify condition (1): If cells are moving left, update iPg
75347 ** only after iPg-1 has already been updated. */
75348 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] )((void) (0));
75349
75350 /* Verify condition (2): If cells are moving right, update iPg
75351 ** only after iPg+1 has already been updated. */
75352 assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] )((void) (0));
75353
75354 if( iPg==0 ){
75355 iNew = iOld = 0;
75356 nNewCell = cntNew[0];
75357 }else{
75358 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
75359 iNew = cntNew[iPg-1] + !leafData;
75360 nNewCell = cntNew[iPg] - iNew;
75361 }
75362
75363 rc = editPage(apNew[iPg], iOld, iNew, nNewCell, &b);
75364 if( rc ) goto balance_cleanup;
75365 abDone[iPg]++;
75366 apNew[iPg]->nFree = usableSpace-szNew[iPg];
75367 assert( apNew[iPg]->nOverflow==0 )((void) (0));
75368 assert( apNew[iPg]->nCell==nNewCell )((void) (0));
75369 }
75370 }
75371
75372 /* All pages have been processed exactly once */
75373 assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 )((void) (0));
75374
75375 assert( nOld>0 )((void) (0));
75376 assert( nNew>0 )((void) (0));
75377
75378 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
75379 /* The root page of the b-tree now contains no cells. The only sibling
75380 ** page is the right-child of the parent. Copy the contents of the
75381 ** child page into the parent, decreasing the overall height of the
75382 ** b-tree structure by one. This is described as the "balance-shallower"
75383 ** sub-algorithm in some documentation.
75384 **
75385 ** If this is an auto-vacuum database, the call to copyNodeContent()
75386 ** sets all pointer-map entries corresponding to database image pages
75387 ** for which the pointer is stored within the content being copied.
75388 **
75389 ** It is critical that the child page be defragmented before being
75390 ** copied into the parent, because if the parent is page 1 then it will
75391 ** by smaller than the child due to the database header, and so all the
75392 ** free space needs to be up front.
75393 */
75394 assert( nNew==1 || CORRUPT_DB )((void) (0));
75395 rc = defragmentPage(apNew[0], -1);
75396 testcase( rc!=SQLITE_OK );
75397 assert( apNew[0]->nFree ==((void) (0))
75398 (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset((void) (0))
75399 - apNew[0]->nCell*2)((void) (0))
75400 || rc!=SQLITE_OK((void) (0))
75401 )((void) (0));
75402 copyNodeContent(apNew[0], pParent, &rc);
75403 freePage(apNew[0], &rc);
75404 }else if( ISAUTOVACUUM(pBt->autoVacuum) && !leafCorrection ){
75405 /* Fix the pointer map entries associated with the right-child of each
75406 ** sibling page. All other pointer map entries have already been taken
75407 ** care of. */
75408 for(i=0; i<nNew; i++){
75409 u32 key = get4bytesqlite3Get4byte(&apNew[i]->aData[8]);
75410 ptrmapPut(pBt, key, PTRMAP_BTREE5, apNew[i]->pgno, &rc);
75411 }
75412 }
75413
75414 assert( pParent->isInit )((void) (0));
75415 TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
75416 nOld, nNew, b.nCell));
75417
75418 /* Free any old pages that were not reused as new pages.
75419 */
75420 for(i=nNew; i<nOld; i++){
75421 freePage(apOld[i], &rc);
75422 }
75423
75424#if 0
75425 if( ISAUTOVACUUM(pBt->autoVacuum) && rc==SQLITE_OK0 && apNew[0]->isInit ){
75426 /* The ptrmapCheckPages() contains assert() statements that verify that
75427 ** all pointer map pages are set correctly. This is helpful while
75428 ** debugging. This is usually disabled because a corrupt database may
75429 ** cause an assert() statement to fail. */
75430 ptrmapCheckPages(apNew, nNew);
75431 ptrmapCheckPages(&pParent, 1);
75432 }
75433#endif
75434
75435 /*
75436 ** Cleanup before returning.
75437 */
75438balance_cleanup:
75439 sqlite3StackFree(0, b.apCell)sqlite3DbFree(0,b.apCell);
75440 for(i=0; i<nOld; i++){
75441 releasePage(apOld[i]);
75442 }
75443 for(i=0; i<nNew; i++){
75444 releasePage(apNew[i]);
75445 }
75446
75447 return rc;
75448}
75449
75450
75451/*
75452** This function is called when the root page of a b-tree structure is
75453** overfull (has one or more overflow pages).
75454**
75455** A new child page is allocated and the contents of the current root
75456** page, including overflow cells, are copied into the child. The root
75457** page is then overwritten to make it an empty page with the right-child
75458** pointer pointing to the new page.
75459**
75460** Before returning, all pointer-map entries corresponding to pages
75461** that the new child-page now contains pointers to are updated. The
75462** entry corresponding to the new right-child pointer of the root
75463** page is also updated.
75464**
75465** If successful, *ppChild is set to contain a reference to the child
75466** page and SQLITE_OK is returned. In this case the caller is required
75467** to call releasePage() on *ppChild exactly once. If an error occurs,
75468** an error code is returned and *ppChild is set to 0.
75469*/
75470static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
75471 int rc; /* Return value from subprocedures */
75472 MemPage *pChild = 0; /* Pointer to a new child page */
75473 Pgno pgnoChild = 0; /* Page number of the new child page */
75474 BtShared *pBt = pRoot->pBt; /* The BTree */
75475
75476 assert( pRoot->nOverflow>0 )((void) (0));
75477 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
75478
75479 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
75480 ** page that will become the new right-child of pPage. Copy the contents
75481 ** of the node stored on pRoot into the new child page.
75482 */
75483 rc = sqlite3PagerWrite(pRoot->pDbPage);
75484 if( rc==SQLITE_OK0 ){
75485 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
75486 copyNodeContent(pRoot, pChild, &rc);
75487 if( ISAUTOVACUUM(pBt->autoVacuum) ){
75488 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE5, pRoot->pgno, &rc);
75489 }
75490 }
75491 if( rc ){
75492 *ppChild = 0;
75493 releasePage(pChild);
75494 return rc;
75495 }
75496 assert( sqlite3PagerIswriteable(pChild->pDbPage) )((void) (0));
75497 assert( sqlite3PagerIswriteable(pRoot->pDbPage) )((void) (0));
75498 assert( pChild->nCell==pRoot->nCell || CORRUPT_DB )((void) (0));
75499
75500 TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
75501
75502 /* Copy the overflow cells from pRoot to pChild */
75503 memcpy(pChild->aiOvfl, pRoot->aiOvfl,
75504 pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
75505 memcpy(pChild->apOvfl, pRoot->apOvfl,
75506 pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
75507 pChild->nOverflow = pRoot->nOverflow;
75508
75509 /* Zero the contents of pRoot. Then install pChild as the right-child. */
75510 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF0x08);
75511 put4bytesqlite3Put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
75512
75513 *ppChild = pChild;
75514 return SQLITE_OK0;
75515}
75516
75517/*
75518** Return SQLITE_CORRUPT if any cursor other than pCur is currently valid
75519** on the same B-tree as pCur.
75520**
75521** This can occur if a database is corrupt with two or more SQL tables
75522** pointing to the same b-tree. If an insert occurs on one SQL table
75523** and causes a BEFORE TRIGGER to do a secondary insert on the other SQL
75524** table linked to the same b-tree. If the secondary insert causes a
75525** rebalance, that can change content out from under the cursor on the
75526** first SQL table, violating invariants on the first insert.
75527*/
75528static int anotherValidCursor(BtCursor *pCur){
75529 BtCursor *pOther;
75530 for(pOther=pCur->pBt->pCursor; pOther; pOther=pOther->pNext){
75531 if( pOther!=pCur
75532 && pOther->eState==CURSOR_VALID0
75533 && pOther->pPage==pCur->pPage
75534 ){
75535 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(75535);
75536 }
75537 }
75538 return SQLITE_OK0;
75539}
75540
75541/*
75542** The page that pCur currently points to has just been modified in
75543** some way. This function figures out if this modification means the
75544** tree needs to be balanced, and if so calls the appropriate balancing
75545** routine. Balancing routines are:
75546**
75547** balance_quick()
75548** balance_deeper()
75549** balance_nonroot()
75550*/
75551static int balance(BtCursor *pCur){
75552 int rc = SQLITE_OK0;
75553 u8 aBalanceQuickSpace[13];
75554 u8 *pFree = 0;
75555
75556 VVA_ONLY( int balance_quick_called = 0 );
75557 VVA_ONLY( int balance_deeper_called = 0 );
75558
75559 do {
75560 int iPage;
75561 MemPage *pPage = pCur->pPage;
75562
75563 if( NEVER(pPage->nFree<0)(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
75564 if( pPage->nOverflow==0 && pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
75565 /* No rebalance required as long as:
75566 ** (1) There are no overflow cells
75567 ** (2) The amount of free space on the page is less than 2/3rds of
75568 ** the total usable space on the page. */
75569 break;
75570 }else if( (iPage = pCur->iPage)==0 ){
75571 if( pPage->nOverflow && (rc = anotherValidCursor(pCur))==SQLITE_OK0 ){
75572 /* The root page of the b-tree is overfull. In this case call the
75573 ** balance_deeper() function to create a new child for the root-page
75574 ** and copy the current contents of the root-page to it. The
75575 ** next iteration of the do-loop will balance the child page.
75576 */
75577 assert( balance_deeper_called==0 )((void) (0));
75578 VVA_ONLY( balance_deeper_called++ );
75579 rc = balance_deeper(pPage, &pCur->apPage[1]);
75580 if( rc==SQLITE_OK0 ){
75581 pCur->iPage = 1;
75582 pCur->ix = 0;
75583 pCur->aiIdx[0] = 0;
75584 pCur->apPage[0] = pPage;
75585 pCur->pPage = pCur->apPage[1];
75586 assert( pCur->pPage->nOverflow )((void) (0));
75587 }
75588 }else{
75589 break;
75590 }
75591 }else{
75592 MemPage * const pParent = pCur->apPage[iPage-1];
75593 int const iIdx = pCur->aiIdx[iPage-1];
75594
75595 rc = sqlite3PagerWrite(pParent->pDbPage);
75596 if( rc==SQLITE_OK0 && pParent->nFree<0 ){
75597 rc = btreeComputeFreeSpace(pParent);
75598 }
75599 if( rc==SQLITE_OK0 ){
75600#ifndef SQLITE_OMIT_QUICKBALANCE
75601 if( pPage->intKeyLeaf
75602 && pPage->nOverflow==1
75603 && pPage->aiOvfl[0]==pPage->nCell
75604 && pParent->pgno!=1
75605 && pParent->nCell==iIdx
75606 ){
75607 /* Call balance_quick() to create a new sibling of pPage on which
75608 ** to store the overflow cell. balance_quick() inserts a new cell
75609 ** into pParent, which may cause pParent overflow. If this
75610 ** happens, the next iteration of the do-loop will balance pParent
75611 ** use either balance_nonroot() or balance_deeper(). Until this
75612 ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
75613 ** buffer.
75614 **
75615 ** The purpose of the following assert() is to check that only a
75616 ** single call to balance_quick() is made for each call to this
75617 ** function. If this were not verified, a subtle bug involving reuse
75618 ** of the aBalanceQuickSpace[] might sneak in.
75619 */
75620 assert( balance_quick_called==0 )((void) (0));
75621 VVA_ONLY( balance_quick_called++ );
75622 rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
75623 }else
75624#endif
75625 {
75626 /* In this case, call balance_nonroot() to redistribute cells
75627 ** between pPage and up to 2 of its sibling pages. This involves
75628 ** modifying the contents of pParent, which may cause pParent to
75629 ** become overfull or underfull. The next iteration of the do-loop
75630 ** will balance the parent page to correct this.
75631 **
75632 ** If the parent page becomes overfull, the overflow cell or cells
75633 ** are stored in the pSpace buffer allocated immediately below.
75634 ** A subsequent iteration of the do-loop will deal with this by
75635 ** calling balance_nonroot() (balance_deeper() may be called first,
75636 ** but it doesn't deal with overflow cells - just moves them to a
75637 ** different page). Once this subsequent call to balance_nonroot()
75638 ** has completed, it is safe to release the pSpace buffer used by
75639 ** the previous call, as the overflow cell data will have been
75640 ** copied either into the body of a database page or into the new
75641 ** pSpace buffer passed to the latter call to balance_nonroot().
75642 */
75643 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
75644 rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1,
75645 pCur->hints&BTREE_BULKLOAD0x00000001);
75646 if( pFree ){
75647 /* If pFree is not NULL, it points to the pSpace buffer used
75648 ** by a previous call to balance_nonroot(). Its contents are
75649 ** now stored either on real database pages or within the
75650 ** new pSpace buffer, so it may be safely freed here. */
75651 sqlite3PageFree(pFree);
75652 }
75653
75654 /* The pSpace buffer will be freed after the next call to
75655 ** balance_nonroot(), or just before this function returns, whichever
75656 ** comes first. */
75657 pFree = pSpace;
75658 }
75659 }
75660
75661 pPage->nOverflow = 0;
75662
75663 /* The next iteration of the do-loop balances the parent page. */
75664 releasePage(pPage);
75665 pCur->iPage--;
75666 assert( pCur->iPage>=0 )((void) (0));
75667 pCur->pPage = pCur->apPage[pCur->iPage];
75668 }
75669 }while( rc==SQLITE_OK0 );
75670
75671 if( pFree ){
75672 sqlite3PageFree(pFree);
75673 }
75674 return rc;
75675}
75676
75677/* Overwrite content from pX into pDest. Only do the write if the
75678** content is different from what is already there.
75679*/
75680static int btreeOverwriteContent(
75681 MemPage *pPage, /* MemPage on which writing will occur */
75682 u8 *pDest, /* Pointer to the place to start writing */
75683 const BtreePayload *pX, /* Source of data to write */
75684 int iOffset, /* Offset of first byte to write */
75685 int iAmt /* Number of bytes to be written */
75686){
75687 int nData = pX->nData - iOffset;
75688 if( nData<=0 ){
75689 /* Overwritting with zeros */
75690 int i;
75691 for(i=0; i<iAmt && pDest[i]==0; i++){}
75692 if( i<iAmt ){
75693 int rc = sqlite3PagerWrite(pPage->pDbPage);
75694 if( rc ) return rc;
75695 memset(pDest + i, 0, iAmt - i);
75696 }
75697 }else{
75698 if( nData<iAmt ){
75699 /* Mixed read data and zeros at the end. Make a recursive call
75700 ** to write the zeros then fall through to write the real data */
75701 int rc = btreeOverwriteContent(pPage, pDest+nData, pX, iOffset+nData,
75702 iAmt-nData);
75703 if( rc ) return rc;
75704 iAmt = nData;
75705 }
75706 if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){
75707 int rc = sqlite3PagerWrite(pPage->pDbPage);
75708 if( rc ) return rc;
75709 /* In a corrupt database, it is possible for the source and destination
75710 ** buffers to overlap. This is harmless since the database is already
75711 ** corrupt but it does cause valgrind and ASAN warnings. So use
75712 ** memmove(). */
75713 memmove(pDest, ((u8*)pX->pData) + iOffset, iAmt);
75714 }
75715 }
75716 return SQLITE_OK0;
75717}
75718
75719/*
75720** Overwrite the cell that cursor pCur is pointing to with fresh content
75721** contained in pX.
75722*/
75723static int btreeOverwriteCell(BtCursor *pCur, const BtreePayload *pX){
75724 int iOffset; /* Next byte of pX->pData to write */
75725 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
75726 int rc; /* Return code */
75727 MemPage *pPage = pCur->pPage; /* Page being written */
75728 BtShared *pBt; /* Btree */
75729 Pgno ovflPgno; /* Next overflow page to write */
75730 u32 ovflPageSize; /* Size to write on overflow page */
75731
75732 if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd
75733 || pCur->info.pPayload < pPage->aData + pPage->cellOffset
75734 ){
75735 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(75735);
75736 }
75737 /* Overwrite the local portion first */
75738 rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
75739 0, pCur->info.nLocal);
75740 if( rc ) return rc;
75741 if( pCur->info.nLocal==nTotal ) return SQLITE_OK0;
75742
75743 /* Now overwrite the overflow pages */
75744 iOffset = pCur->info.nLocal;
75745 assert( nTotal>=0 )((void) (0));
75746 assert( iOffset>=0 )((void) (0));
75747 ovflPgno = get4bytesqlite3Get4byte(pCur->info.pPayload + iOffset);
75748 pBt = pPage->pBt;
75749 ovflPageSize = pBt->usableSize - 4;
75750 do{
75751 rc = btreeGetPage(pBt, ovflPgno, &pPage, 0);
75752 if( rc ) return rc;
75753 if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 || pPage->isInit ){
75754 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(75754);
75755 }else{
75756 if( iOffset+ovflPageSize<(u32)nTotal ){
75757 ovflPgno = get4bytesqlite3Get4byte(pPage->aData);
75758 }else{
75759 ovflPageSize = nTotal - iOffset;
75760 }
75761 rc = btreeOverwriteContent(pPage, pPage->aData+4, pX,
75762 iOffset, ovflPageSize);
75763 }
75764 sqlite3PagerUnref(pPage->pDbPage);
75765 if( rc ) return rc;
75766 iOffset += ovflPageSize;
75767 }while( iOffset<nTotal );
75768 return SQLITE_OK0;
75769}
75770
75771
75772/*
75773** Insert a new record into the BTree. The content of the new record
75774** is described by the pX object. The pCur cursor is used only to
75775** define what table the record should be inserted into, and is left
75776** pointing at a random location.
75777**
75778** For a table btree (used for rowid tables), only the pX.nKey value of
75779** the key is used. The pX.pKey value must be NULL. The pX.nKey is the
75780** rowid or INTEGER PRIMARY KEY of the row. The pX.nData,pData,nZero fields
75781** hold the content of the row.
75782**
75783** For an index btree (used for indexes and WITHOUT ROWID tables), the
75784** key is an arbitrary byte sequence stored in pX.pKey,nKey. The
75785** pX.pData,nData,nZero fields must be zero.
75786**
75787** If the seekResult parameter is non-zero, then a successful call to
75788** sqlite3BtreeIndexMoveto() to seek cursor pCur to (pKey,nKey) has already
75789** been performed. In other words, if seekResult!=0 then the cursor
75790** is currently pointing to a cell that will be adjacent to the cell
75791** to be inserted. If seekResult<0 then pCur points to a cell that is
75792** smaller then (pKey,nKey). If seekResult>0 then pCur points to a cell
75793** that is larger than (pKey,nKey).
75794**
75795** If seekResult==0, that means pCur is pointing at some unknown location.
75796** In that case, this routine must seek the cursor to the correct insertion
75797** point for (pKey,nKey) before doing the insertion. For index btrees,
75798** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
75799** key values and pX->aMem can be used instead of pX->pKey to avoid having
75800** to decode the key.
75801*/
75802SQLITE_PRIVATEstatic int sqlite3BtreeInsert(
75803 BtCursor *pCur, /* Insert data into the table of this cursor */
75804 const BtreePayload *pX, /* Content of the row to be inserted */
75805 int flags, /* True if this is likely an append */
75806 int seekResult /* Result of prior IndexMoveto() call */
75807){
75808 int rc;
75809 int loc = seekResult; /* -1: before desired location +1: after */
75810 int szNew = 0;
75811 int idx;
75812 MemPage *pPage;
75813 Btree *p = pCur->pBtree;
75814 BtShared *pBt = p->pBt;
75815 unsigned char *oldCell;
75816 unsigned char *newCell = 0;
75817
75818 assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND|BTREE_PREFORMAT))==flags )((void) (0));
75819 assert( (flags & BTREE_PREFORMAT)==0 || seekResult || pCur->pKeyInfo==0 )((void) (0));
75820
75821 /* Save the positions of any other cursors open on this table.
75822 **
75823 ** In some cases, the call to btreeMoveto() below is a no-op. For
75824 ** example, when inserting data into a table with auto-generated integer
75825 ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the
75826 ** integer key to use. It then calls this function to actually insert the
75827 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
75828 ** that the cursor is already where it needs to be and returns without
75829 ** doing any work. To avoid thwarting these optimizations, it is important
75830 ** not to clear the cursor here.
75831 */
75832 if( pCur->curFlags & BTCF_Multiple0x20 ){
75833 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
75834 if( rc ) return rc;
75835 if( loc && pCur->iPage<0 ){
75836 /* This can only happen if the schema is corrupt such that there is more
75837 ** than one table or index with the same root page as used by the cursor.
75838 ** Which can only happen if the SQLITE_NoSchemaError flag was set when
75839 ** the schema was loaded. This cannot be asserted though, as a user might
75840 ** set the flag, load the schema, and then unset the flag. */
75841 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(75841);
75842 }
75843 }
75844
75845 /* Ensure that the cursor is not in the CURSOR_FAULT state and that it
75846 ** points to a valid cell.
75847 */
75848 if( pCur->eState>=CURSOR_REQUIRESEEK3 ){
75849 testcase( pCur->eState==CURSOR_REQUIRESEEK );
75850 testcase( pCur->eState==CURSOR_FAULT );
75851 rc = moveToRoot(pCur);
75852 if( rc && rc!=SQLITE_EMPTY16 ) return rc;
75853 }
75854
75855 assert( cursorOwnsBtShared(pCur) )((void) (0));
75856 assert( (pCur->curFlags & BTCF_WriteFlag)!=0((void) (0))
75857 && pBt->inTransaction==TRANS_WRITE((void) (0))
75858 && (pBt->btsFlags & BTS_READ_ONLY)==0 )((void) (0));
75859 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) )((void) (0));
75860
75861 /* Assert that the caller has been consistent. If this cursor was opened
75862 ** expecting an index b-tree, then the caller should be inserting blob
75863 ** keys with no associated data. If the cursor was opened expecting an
75864 ** intkey table, the caller should be inserting integer keys with a
75865 ** blob of associated data. */
75866 assert( (flags & BTREE_PREFORMAT) || (pX->pKey==0)==(pCur->pKeyInfo==0) )((void) (0));
75867
75868 if( pCur->pKeyInfo==0 ){
75869 assert( pX->pKey==0 )((void) (0));
75870 /* If this is an insert into a table b-tree, invalidate any incrblob
75871 ** cursors open on the row being replaced */
75872 if( p->hasIncrblobCur ){
75873 invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
75874 }
75875
75876 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
75877 ** to a row with the same key as the new entry being inserted.
75878 */
75879#ifdef SQLITE_DEBUG
75880 if( flags & BTREE_SAVEPOSITION0x02 ){
75881 assert( pCur->curFlags & BTCF_ValidNKey )((void) (0));
75882 assert( pX->nKey==pCur->info.nKey )((void) (0));
75883 assert( loc==0 )((void) (0));
75884 }
75885#endif
75886
75887 /* On the other hand, BTREE_SAVEPOSITION==0 does not imply
75888 ** that the cursor is not pointing to a row to be overwritten.
75889 ** So do a complete check.
75890 */
75891 if( (pCur->curFlags&BTCF_ValidNKey0x02)!=0 && pX->nKey==pCur->info.nKey ){
75892 /* The cursor is pointing to the entry that is to be
75893 ** overwritten */
75894 assert( pX->nData>=0 && pX->nZero>=0 )((void) (0));
75895 if( pCur->info.nSize!=0
75896 && pCur->info.nPayload==(u32)pX->nData+pX->nZero
75897 ){
75898 /* New entry is the same size as the old. Do an overwrite */
75899 return btreeOverwriteCell(pCur, pX);
75900 }
75901 assert( loc==0 )((void) (0));
75902 }else if( loc==0 ){
75903 /* The cursor is *not* pointing to the cell to be overwritten, nor
75904 ** to an adjacent cell. Move the cursor so that it is pointing either
75905 ** to the cell to be overwritten or an adjacent cell.
75906 */
75907 rc = sqlite3BtreeTableMoveto(pCur, pX->nKey,
75908 (flags & BTREE_APPEND0x08)!=0, &loc);
75909 if( rc ) return rc;
75910 }
75911 }else{
75912 /* This is an index or a WITHOUT ROWID table */
75913
75914 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
75915 ** to a row with the same key as the new entry being inserted.
75916 */
75917 assert( (flags & BTREE_SAVEPOSITION)==0 || loc==0 )((void) (0));
75918
75919 /* If the cursor is not already pointing either to the cell to be
75920 ** overwritten, or if a new cell is being inserted, if the cursor is
75921 ** not pointing to an immediately adjacent cell, then move the cursor
75922 ** so that it does.
75923 */
75924 if( loc==0 && (flags & BTREE_SAVEPOSITION0x02)==0 ){
75925 if( pX->nMem ){
75926 UnpackedRecord r;
75927 r.pKeyInfo = pCur->pKeyInfo;
75928 r.aMem = pX->aMem;
75929 r.nField = pX->nMem;
75930 r.default_rc = 0;
75931 r.eqSeen = 0;
75932 rc = sqlite3BtreeIndexMoveto(pCur, &r, &loc);
75933 }else{
75934 rc = btreeMoveto(pCur, pX->pKey, pX->nKey,
75935 (flags & BTREE_APPEND0x08)!=0, &loc);
75936 }
75937 if( rc ) return rc;
75938 }
75939
75940 /* If the cursor is currently pointing to an entry to be overwritten
75941 ** and the new content is the same as as the old, then use the
75942 ** overwrite optimization.
75943 */
75944 if( loc==0 ){
75945 getCellInfo(pCur);
75946 if( pCur->info.nKey==pX->nKey ){
75947 BtreePayload x2;
75948 x2.pData = pX->pKey;
75949 x2.nData = pX->nKey;
75950 x2.nZero = 0;
75951 return btreeOverwriteCell(pCur, &x2);
75952 }
75953 }
75954 }
75955 assert( pCur->eState==CURSOR_VALID((void) (0))
75956 || (pCur->eState==CURSOR_INVALID && loc) )((void) (0));
75957
75958 pPage = pCur->pPage;
75959 assert( pPage->intKey || pX->nKey>=0 || (flags & BTREE_PREFORMAT) )((void) (0));
75960 assert( pPage->leaf || !pPage->intKey )((void) (0));
75961 if( pPage->nFree<0 ){
75962 if( NEVER(pCur->eState>CURSOR_INVALID)(pCur->eState>1) ){
75963 /* ^^^^^--- due to the moveToRoot() call above */
75964 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(75964);
75965 }else{
75966 rc = btreeComputeFreeSpace(pPage);
75967 }
75968 if( rc ) return rc;
75969 }
75970
75971 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
75972 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
75973 loc==0 ? "overwrite" : "new entry"));
75974 assert( pPage->isInit || CORRUPT_DB )((void) (0));
75975 newCell = pBt->pTmpSpace;
75976 assert( newCell!=0 )((void) (0));
75977 if( flags & BTREE_PREFORMAT0x80 ){
75978 rc = SQLITE_OK0;
75979 szNew = pBt->nPreformatSize;
75980 if( szNew<4 ) szNew = 4;
75981 if( ISAUTOVACUUM(pBt->autoVacuum) && szNew>pPage->maxLocal ){
75982 CellInfo info;
75983 pPage->xParseCell(pPage, newCell, &info);
75984 if( info.nPayload!=info.nLocal ){
75985 Pgno ovfl = get4bytesqlite3Get4byte(&newCell[szNew-4]);
75986 ptrmapPut(pBt, ovfl, PTRMAP_OVERFLOW13, pPage->pgno, &rc);
75987 }
75988 }
75989 }else{
75990 rc = fillInCell(pPage, newCell, pX, &szNew);
75991 }
75992 if( rc ) goto end_insert;
75993 assert( szNew==pPage->xCellSize(pPage, newCell) )((void) (0));
75994 assert( szNew <= MX_CELL_SIZE(pBt) )((void) (0));
75995 idx = pCur->ix;
75996 if( loc==0 ){
75997 CellInfo info;
75998 assert( idx>=0 )((void) (0));
75999 if( idx>=pPage->nCell ){
76000 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(76000);
76001 }
76002 rc = sqlite3PagerWrite(pPage->pDbPage);
76003 if( rc ){
76004 goto end_insert;
76005 }
76006 oldCell = findCell(pPage, idx)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(idx)])[0]<<8 | (&(pPage)->aCellIdx
[2*(idx)])[1])))
;
76007 if( !pPage->leaf ){
76008 memcpy(newCell, oldCell, 4);
76009 }
76010 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; }
;
76011 testcase( pCur->curFlags & BTCF_ValidOvfl );
76012 invalidateOverflowCache(pCur)(pCur->curFlags &= ~0x04);
76013 if( info.nSize==szNew && info.nLocal==info.nPayload
76014 && (!ISAUTOVACUUM(pBt->autoVacuum) || szNew<pPage->minLocal)
76015 ){
76016 /* Overwrite the old cell with the new if they are the same size.
76017 ** We could also try to do this if the old cell is smaller, then add
76018 ** the leftover space to the free list. But experiments show that
76019 ** doing that is no faster then skipping this optimization and just
76020 ** calling dropCell() and insertCell().
76021 **
76022 ** This optimization cannot be used on an autovacuum database if the
76023 ** new entry uses overflow pages, as the insertCell() call below is
76024 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */
76025 assert( rc==SQLITE_OK )((void) (0)); /* clearCell never fails when nLocal==nPayload */
76026 if( oldCell < pPage->aData+pPage->hdrOffset+10 ){
76027 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(76027);
76028 }
76029 if( oldCell+szNew > pPage->aDataEnd ){
76030 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(76030);
76031 }
76032 memcpy(oldCell, newCell, szNew);
76033 return SQLITE_OK0;
76034 }
76035 dropCell(pPage, idx, info.nSize, &rc);
76036 if( rc ) goto end_insert;
76037 }else if( loc<0 && pPage->nCell>0 ){
76038 assert( pPage->leaf )((void) (0));
76039 idx = ++pCur->ix;
76040 pCur->curFlags &= ~BTCF_ValidNKey0x02;
76041 }else{
76042 assert( pPage->leaf )((void) (0));
76043 }
76044 insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
76045 assert( pPage->nOverflow==0 || rc==SQLITE_OK )((void) (0));
76046 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 )((void) (0));
76047
76048 /* If no error has occurred and pPage has an overflow cell, call balance()
76049 ** to redistribute the cells within the tree. Since balance() may move
76050 ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey
76051 ** variables.
76052 **
76053 ** Previous versions of SQLite called moveToRoot() to move the cursor
76054 ** back to the root page as balance() used to invalidate the contents
76055 ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
76056 ** set the cursor state to "invalid". This makes common insert operations
76057 ** slightly faster.
76058 **
76059 ** There is a subtle but important optimization here too. When inserting
76060 ** multiple records into an intkey b-tree using a single cursor (as can
76061 ** happen while processing an "INSERT INTO ... SELECT" statement), it
76062 ** is advantageous to leave the cursor pointing to the last entry in
76063 ** the b-tree if possible. If the cursor is left pointing to the last
76064 ** entry in the table, and the next row inserted has an integer key
76065 ** larger than the largest existing key, it is possible to insert the
76066 ** row without seeking the cursor. This can be a big performance boost.
76067 */
76068 pCur->info.nSize = 0;
76069 if( pPage->nOverflow ){
76070 assert( rc==SQLITE_OK )((void) (0));
76071 pCur->curFlags &= ~(BTCF_ValidNKey0x02);
76072 rc = balance(pCur);
76073
76074 /* Must make sure nOverflow is reset to zero even if the balance()
76075 ** fails. Internal data structure corruption will result otherwise.
76076 ** Also, set the cursor state to invalid. This stops saveCursorPosition()
76077 ** from trying to save the current position of the cursor. */
76078 pCur->pPage->nOverflow = 0;
76079 pCur->eState = CURSOR_INVALID1;
76080 if( (flags & BTREE_SAVEPOSITION0x02) && rc==SQLITE_OK0 ){
76081 btreeReleaseAllCursorPages(pCur);
76082 if( pCur->pKeyInfo ){
76083 assert( pCur->pKey==0 )((void) (0));
76084 pCur->pKey = sqlite3Malloc( pX->nKey );
76085 if( pCur->pKey==0 ){
76086 rc = SQLITE_NOMEM7;
76087 }else{
76088 memcpy(pCur->pKey, pX->pKey, pX->nKey);
76089 }
76090 }
76091 pCur->eState = CURSOR_REQUIRESEEK3;
76092 pCur->nKey = pX->nKey;
76093 }
76094 }
76095 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 )((void) (0));
76096
76097end_insert:
76098 return rc;
76099}
76100
76101/*
76102** This function is used as part of copying the current row from cursor
76103** pSrc into cursor pDest. If the cursors are open on intkey tables, then
76104** parameter iKey is used as the rowid value when the record is copied
76105** into pDest. Otherwise, the record is copied verbatim.
76106**
76107** This function does not actually write the new value to cursor pDest.
76108** Instead, it creates and populates any required overflow pages and
76109** writes the data for the new cell into the BtShared.pTmpSpace buffer
76110** for the destination database. The size of the cell, in bytes, is left
76111** in BtShared.nPreformatSize. The caller completes the insertion by
76112** calling sqlite3BtreeInsert() with the BTREE_PREFORMAT flag specified.
76113**
76114** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
76115*/
76116SQLITE_PRIVATEstatic int sqlite3BtreeTransferRow(BtCursor *pDest, BtCursor *pSrc, i64 iKey){
76117 int rc = SQLITE_OK0;
76118 BtShared *pBt = pDest->pBt;
76119 u8 *aOut = pBt->pTmpSpace; /* Pointer to next output buffer */
76120 const u8 *aIn; /* Pointer to next input buffer */
76121 u32 nIn; /* Size of input buffer aIn[] */
76122 u32 nRem; /* Bytes of data still to copy */
76123
76124 getCellInfo(pSrc);
76125 if( pSrc->info.nPayload<0x80 ){
76126 *(aOut++) = pSrc->info.nPayload;
76127 }else{
76128 aOut += sqlite3PutVarint(aOut, pSrc->info.nPayload);
76129 }
76130 if( pDest->pKeyInfo==0 ) aOut += putVarintsqlite3PutVarint(aOut, iKey);
76131 nIn = pSrc->info.nLocal;
76132 aIn = pSrc->info.pPayload;
76133 if( aIn+nIn>pSrc->pPage->aDataEnd ){
76134 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(76134);
76135 }
76136 nRem = pSrc->info.nPayload;
76137 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
76138 memcpy(aOut, aIn, nIn);
76139 pBt->nPreformatSize = nIn + (aOut - pBt->pTmpSpace);
76140 }else{
76141 Pager *pSrcPager = pSrc->pBt->pPager;
76142 u8 *pPgnoOut = 0;
76143 Pgno ovflIn = 0;
76144 DbPage *pPageIn = 0;
76145 MemPage *pPageOut = 0;
76146 u32 nOut; /* Size of output buffer aOut[] */
76147
76148 nOut = btreePayloadToLocal(pDest->pPage, pSrc->info.nPayload);
76149 pBt->nPreformatSize = nOut + (aOut - pBt->pTmpSpace);
76150 if( nOut<pSrc->info.nPayload ){
76151 pPgnoOut = &aOut[nOut];
76152 pBt->nPreformatSize += 4;
76153 }
76154
76155 if( nRem>nIn ){
76156 if( aIn+nIn+4>pSrc->pPage->aDataEnd ){
76157 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(76157);
76158 }
76159 ovflIn = get4bytesqlite3Get4byte(&pSrc->info.pPayload[nIn]);
76160 }
76161
76162 do {
76163 nRem -= nOut;
76164 do{
76165 assert( nOut>0 )((void) (0));
76166 if( nIn>0 ){
76167 int nCopy = MIN(nOut, nIn)((nOut)<(nIn)?(nOut):(nIn));
76168 memcpy(aOut, aIn, nCopy);
76169 nOut -= nCopy;
76170 nIn -= nCopy;
76171 aOut += nCopy;
76172 aIn += nCopy;
76173 }
76174 if( nOut>0 ){
76175 sqlite3PagerUnref(pPageIn);
76176 pPageIn = 0;
76177 rc = sqlite3PagerGet(pSrcPager, ovflIn, &pPageIn, PAGER_GET_READONLY0x02);
76178 if( rc==SQLITE_OK0 ){
76179 aIn = (const u8*)sqlite3PagerGetData(pPageIn);
76180 ovflIn = get4bytesqlite3Get4byte(aIn);
76181 aIn += 4;
76182 nIn = pSrc->pBt->usableSize - 4;
76183 }
76184 }
76185 }while( rc==SQLITE_OK0 && nOut>0 );
76186
76187 if( rc==SQLITE_OK0 && nRem>0 && ALWAYS(pPgnoOut)(pPgnoOut) ){
76188 Pgno pgnoNew;
76189 MemPage *pNew = 0;
76190 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
76191 put4bytesqlite3Put4byte(pPgnoOut, pgnoNew);
76192 if( ISAUTOVACUUM(pBt->autoVacuum) && pPageOut ){
76193 ptrmapPut(pBt, pgnoNew, PTRMAP_OVERFLOW24, pPageOut->pgno, &rc);
76194 }
76195 releasePage(pPageOut);
76196 pPageOut = pNew;
76197 if( pPageOut ){
76198 pPgnoOut = pPageOut->aData;
76199 put4bytesqlite3Put4byte(pPgnoOut, 0);
76200 aOut = &pPgnoOut[4];
76201 nOut = MIN(pBt->usableSize - 4, nRem)((pBt->usableSize - 4)<(nRem)?(pBt->usableSize - 4):
(nRem))
;
76202 }
76203 }
76204 }while( nRem>0 && rc==SQLITE_OK0 );
76205
76206 releasePage(pPageOut);
76207 sqlite3PagerUnref(pPageIn);
76208 }
76209
76210 return rc;
76211}
76212
76213/*
76214** Delete the entry that the cursor is pointing to.
76215**
76216** If the BTREE_SAVEPOSITION bit of the flags parameter is zero, then
76217** the cursor is left pointing at an arbitrary location after the delete.
76218** But if that bit is set, then the cursor is left in a state such that
76219** the next call to BtreeNext() or BtreePrev() moves it to the same row
76220** as it would have been on if the call to BtreeDelete() had been omitted.
76221**
76222** The BTREE_AUXDELETE bit of flags indicates that is one of several deletes
76223** associated with a single table entry and its indexes. Only one of those
76224** deletes is considered the "primary" delete. The primary delete occurs
76225** on a cursor that is not a BTREE_FORDELETE cursor. All but one delete
76226** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
76227** The BTREE_AUXDELETE bit is a hint that is not used by this implementation,
76228** but which might be used by alternative storage engines.
76229*/
76230SQLITE_PRIVATEstatic int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
76231 Btree *p = pCur->pBtree;
76232 BtShared *pBt = p->pBt;
76233 int rc; /* Return code */
76234 MemPage *pPage; /* Page to delete cell from */
76235 unsigned char *pCell; /* Pointer to cell to delete */
76236 int iCellIdx; /* Index of cell to delete */
76237 int iCellDepth; /* Depth of node containing pCell */
76238 CellInfo info; /* Size of the cell being deleted */
76239 u8 bPreserve; /* Keep cursor valid. 2 for CURSOR_SKIPNEXT */
76240
76241 assert( cursorOwnsBtShared(pCur) )((void) (0));
76242 assert( pBt->inTransaction==TRANS_WRITE )((void) (0));
76243 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 )((void) (0));
76244 assert( pCur->curFlags & BTCF_WriteFlag )((void) (0));
76245 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) )((void) (0));
76246 assert( !hasReadConflicts(p, pCur->pgnoRoot) )((void) (0));
76247 assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 )((void) (0));
76248 if( pCur->eState!=CURSOR_VALID0 ){
76249 if( pCur->eState>=CURSOR_REQUIRESEEK3 ){
76250 rc = btreeRestoreCursorPosition(pCur);
76251 assert( rc!=SQLITE_OK || CORRUPT_DB || pCur->eState==CURSOR_VALID )((void) (0));
76252 if( rc || pCur->eState!=CURSOR_VALID0 ) return rc;
76253 }else{
76254 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(76254);
76255 }
76256 }
76257 assert( pCur->eState==CURSOR_VALID )((void) (0));
76258
76259 iCellDepth = pCur->iPage;
76260 iCellIdx = pCur->ix;
76261 pPage = pCur->pPage;
76262 if( pPage->nCell<=iCellIdx ){
76263 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(76263);
76264 }
76265 pCell = findCell(pPage, iCellIdx)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(iCellIdx)])[0]<<8 | (&(pPage)->
aCellIdx[2*(iCellIdx)])[1])))
;
76266 if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ){
76267 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(76267);
76268 }
76269
76270 /* If the BTREE_SAVEPOSITION bit is on, then the cursor position must
76271 ** be preserved following this delete operation. If the current delete
76272 ** will cause a b-tree rebalance, then this is done by saving the cursor
76273 ** key and leaving the cursor in CURSOR_REQUIRESEEK state before
76274 ** returning.
76275 **
76276 ** If the current delete will not cause a rebalance, then the cursor
76277 ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately
76278 ** before or after the deleted entry.
76279 **
76280 ** The bPreserve value records which path is required:
76281 **
76282 ** bPreserve==0 Not necessary to save the cursor position
76283 ** bPreserve==1 Use CURSOR_REQUIRESEEK to save the cursor position
76284 ** bPreserve==2 Cursor won't move. Set CURSOR_SKIPNEXT.
76285 */
76286 bPreserve = (flags & BTREE_SAVEPOSITION0x02)!=0;
76287 if( bPreserve ){
76288 if( !pPage->leaf
76289 || (pPage->nFree+pPage->xCellSize(pPage,pCell)+2) >
76290 (int)(pBt->usableSize*2/3)
76291 || pPage->nCell==1 /* See dbfuzz001.test for a test case */
76292 ){
76293 /* A b-tree rebalance will be required after deleting this entry.
76294 ** Save the cursor key. */
76295 rc = saveCursorKey(pCur);
76296 if( rc ) return rc;
76297 }else{
76298 bPreserve = 2;
76299 }
76300 }
76301
76302 /* If the page containing the entry to delete is not a leaf page, move
76303 ** the cursor to the largest entry in the tree that is smaller than
76304 ** the entry being deleted. This cell will replace the cell being deleted
76305 ** from the internal node. The 'previous' entry is used for this instead
76306 ** of the 'next' entry, as the previous entry is always a part of the
76307 ** sub-tree headed by the child page of the cell being deleted. This makes
76308 ** balancing the tree following the delete operation easier. */
76309 if( !pPage->leaf ){
76310 rc = sqlite3BtreePrevious(pCur, 0);
76311 assert( rc!=SQLITE_DONE )((void) (0));
76312 if( rc ) return rc;
76313 }
76314
76315 /* Save the positions of any other cursors open on this table before
76316 ** making any modifications. */
76317 if( pCur->curFlags & BTCF_Multiple0x20 ){
76318 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
76319 if( rc ) return rc;
76320 }
76321
76322 /* If this is a delete operation to remove a row from a table b-tree,
76323 ** invalidate any incrblob cursors open on the row being deleted. */
76324 if( pCur->pKeyInfo==0 && p->hasIncrblobCur ){
76325 invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
76326 }
76327
76328 /* Make the page containing the entry to be deleted writable. Then free any
76329 ** overflow pages associated with the entry and finally remove the cell
76330 ** itself from within the page. */
76331 rc = sqlite3PagerWrite(pPage->pDbPage);
76332 if( rc ) return rc;
76333 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; }
;
76334 dropCell(pPage, iCellIdx, info.nSize, &rc);
76335 if( rc ) return rc;
76336
76337 /* If the cell deleted was not located on a leaf page, then the cursor
76338 ** is currently pointing to the largest entry in the sub-tree headed
76339 ** by the child-page of the cell that was just deleted from an internal
76340 ** node. The cell from the leaf node needs to be moved to the internal
76341 ** node to replace the deleted cell. */
76342 if( !pPage->leaf ){
76343 MemPage *pLeaf = pCur->pPage;
76344 int nCell;
76345 Pgno n;
76346 unsigned char *pTmp;
76347
76348 if( pLeaf->nFree<0 ){
76349 rc = btreeComputeFreeSpace(pLeaf);
76350 if( rc ) return rc;
76351 }
76352 if( iCellDepth<pCur->iPage-1 ){
76353 n = pCur->apPage[iCellDepth+1]->pgno;
76354 }else{
76355 n = pCur->pPage->pgno;
76356 }
76357 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])))
;
76358 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(76358);
76359 nCell = pLeaf->xCellSize(pLeaf, pCell);
76360 assert( MX_CELL_SIZE(pBt) >= nCell )((void) (0));
76361 pTmp = pBt->pTmpSpace;
76362 assert( pTmp!=0 )((void) (0));
76363 rc = sqlite3PagerWrite(pLeaf->pDbPage);
76364 if( rc==SQLITE_OK0 ){
76365 insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
76366 }
76367 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
76368 if( rc ) return rc;
76369 }
76370
76371 /* Balance the tree. If the entry deleted was located on a leaf page,
76372 ** then the cursor still points to that page. In this case the first
76373 ** call to balance() repairs the tree, and the if(...) condition is
76374 ** never true.
76375 **
76376 ** Otherwise, if the entry deleted was on an internal node page, then
76377 ** pCur is pointing to the leaf page from which a cell was removed to
76378 ** replace the cell deleted from the internal node. This is slightly
76379 ** tricky as the leaf node may be underfull, and the internal node may
76380 ** be either under or overfull. In this case run the balancing algorithm
76381 ** on the leaf node first. If the balance proceeds far enough up the
76382 ** tree that we can be sure that any problem in the internal node has
76383 ** been corrected, so be it. Otherwise, after balancing the leaf node,
76384 ** walk the cursor up the tree to the internal node and balance it as
76385 ** well. */
76386 assert( pCur->pPage->nOverflow==0 )((void) (0));
76387 assert( pCur->pPage->nFree>=0 )((void) (0));
76388 if( pCur->pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
76389 /* Optimization: If the free space is less than 2/3rds of the page,
76390 ** then balance() will always be a no-op. No need to invoke it. */
76391 rc = SQLITE_OK0;
76392 }else{
76393 rc = balance(pCur);
76394 }
76395 if( rc==SQLITE_OK0 && pCur->iPage>iCellDepth ){
76396 releasePageNotNull(pCur->pPage);
76397 pCur->iPage--;
76398 while( pCur->iPage>iCellDepth ){
76399 releasePage(pCur->apPage[pCur->iPage--]);
76400 }
76401 pCur->pPage = pCur->apPage[pCur->iPage];
76402 rc = balance(pCur);
76403 }
76404
76405 if( rc==SQLITE_OK0 ){
76406 if( bPreserve>1 ){
76407 assert( (pCur->iPage==iCellDepth || CORRUPT_DB) )((void) (0));
76408 assert( pPage==pCur->pPage || CORRUPT_DB )((void) (0));
76409 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell )((void) (0));
76410 pCur->eState = CURSOR_SKIPNEXT2;
76411 if( iCellIdx>=pPage->nCell ){
76412 pCur->skipNext = -1;
76413 pCur->ix = pPage->nCell-1;
76414 }else{
76415 pCur->skipNext = 1;
76416 }
76417 }else{
76418 rc = moveToRoot(pCur);
76419 if( bPreserve ){
76420 btreeReleaseAllCursorPages(pCur);
76421 pCur->eState = CURSOR_REQUIRESEEK3;
76422 }
76423 if( rc==SQLITE_EMPTY16 ) rc = SQLITE_OK0;
76424 }
76425 }
76426 return rc;
76427}
76428
76429/*
76430** Create a new BTree table. Write into *piTable the page
76431** number for the root page of the new table.
76432**
76433** The type of type is determined by the flags parameter. Only the
76434** following values of flags are currently in use. Other values for
76435** flags might not work:
76436**
76437** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys
76438** BTREE_ZERODATA Used for SQL indices
76439*/
76440static int btreeCreateTable(Btree *p, Pgno *piTable, int createTabFlags){
76441 BtShared *pBt = p->pBt;
76442 MemPage *pRoot;
76443 Pgno pgnoRoot;
76444 int rc;
76445 int ptfFlags; /* Page-type flage for the root page of new table */
76446
76447 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
76448 assert( pBt->inTransaction==TRANS_WRITE )((void) (0));
76449 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 )((void) (0));
76450
76451#ifdef SQLITE_OMIT_AUTOVACUUM
76452 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
76453 if( rc ){
76454 return rc;
76455 }
76456#else
76457 if( pBt->autoVacuum ){
76458 Pgno pgnoMove; /* Move a page here to make room for the root-page */
76459 MemPage *pPageMove; /* The page to move to. */
76460
76461 /* Creating a new table may probably require moving an existing database
76462 ** to make room for the new tables root page. In case this page turns
76463 ** out to be an overflow page, delete all overflow page-map caches
76464 ** held by open cursors.
76465 */
76466 invalidateAllOverflowCache(pBt);
76467
76468 /* Read the value of meta[3] from the database to determine where the
76469 ** root page of the new table should go. meta[3] is the largest root-page
76470 ** created so far, so the new root-page is (meta[3]+1).
76471 */
76472 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE4, &pgnoRoot);
76473 if( pgnoRoot>btreePagecount(pBt) ){
76474 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(76474);
76475 }
76476 pgnoRoot++;
76477
76478 /* The new root-page may not be allocated on a pointer-map page, or the
76479 ** PENDING_BYTE page.
76480 */
76481 while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot)ptrmapPageno(pBt, pgnoRoot) ||
76482 pgnoRoot==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
76483 pgnoRoot++;
76484 }
76485 assert( pgnoRoot>=3 )((void) (0));
76486
76487 /* Allocate a page. The page that currently resides at pgnoRoot will
76488 ** be moved to the allocated page (unless the allocated page happens
76489 ** to reside at pgnoRoot).
76490 */
76491 rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, BTALLOC_EXACT1);
76492 if( rc!=SQLITE_OK0 ){
76493 return rc;
76494 }
76495
76496 if( pgnoMove!=pgnoRoot ){
76497 /* pgnoRoot is the page that will be used for the root-page of
76498 ** the new table (assuming an error did not occur). But we were
76499 ** allocated pgnoMove. If required (i.e. if it was not allocated
76500 ** by extending the file), the current page at position pgnoMove
76501 ** is already journaled.
76502 */
76503 u8 eType = 0;
76504 Pgno iPtrPage = 0;
76505
76506 /* Save the positions of any open cursors. This is required in
76507 ** case they are holding a reference to an xFetch reference
76508 ** corresponding to page pgnoRoot. */
76509 rc = saveAllCursors(pBt, 0, 0);
76510 releasePage(pPageMove);
76511 if( rc!=SQLITE_OK0 ){
76512 return rc;
76513 }
76514
76515 /* Move the page currently at pgnoRoot to pgnoMove. */
76516 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
76517 if( rc!=SQLITE_OK0 ){
76518 return rc;
76519 }
76520 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
76521 if( eType==PTRMAP_ROOTPAGE1 || eType==PTRMAP_FREEPAGE2 ){
76522 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(76522);
76523 }
76524 if( rc!=SQLITE_OK0 ){
76525 releasePage(pRoot);
76526 return rc;
76527 }
76528 assert( eType!=PTRMAP_ROOTPAGE )((void) (0));
76529 assert( eType!=PTRMAP_FREEPAGE )((void) (0));
76530 rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
76531 releasePage(pRoot);
76532
76533 /* Obtain the page at pgnoRoot */
76534 if( rc!=SQLITE_OK0 ){
76535 return rc;
76536 }
76537 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
76538 if( rc!=SQLITE_OK0 ){
76539 return rc;
76540 }
76541 rc = sqlite3PagerWrite(pRoot->pDbPage);
76542 if( rc!=SQLITE_OK0 ){
76543 releasePage(pRoot);
76544 return rc;
76545 }
76546 }else{
76547 pRoot = pPageMove;
76548 }
76549
76550 /* Update the pointer-map and meta-data with the new root-page number. */
76551 ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE1, 0, &rc);
76552 if( rc ){
76553 releasePage(pRoot);
76554 return rc;
76555 }
76556
76557 /* When the new root page was allocated, page 1 was made writable in
76558 ** order either to increase the database filesize, or to decrement the
76559 ** freelist count. Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
76560 */
76561 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) )((void) (0));
76562 rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
76563 if( NEVER(rc)(rc) ){
76564 releasePage(pRoot);
76565 return rc;
76566 }
76567
76568 }else{
76569 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
76570 if( rc ) return rc;
76571 }
76572#endif
76573 assert( sqlite3PagerIswriteable(pRoot->pDbPage) )((void) (0));
76574 if( createTabFlags & BTREE_INTKEY1 ){
76575 ptfFlags = PTF_INTKEY0x01 | PTF_LEAFDATA0x04 | PTF_LEAF0x08;
76576 }else{
76577 ptfFlags = PTF_ZERODATA0x02 | PTF_LEAF0x08;
76578 }
76579 zeroPage(pRoot, ptfFlags);
76580 sqlite3PagerUnref(pRoot->pDbPage);
76581 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 )((void) (0));
76582 *piTable = pgnoRoot;
76583 return SQLITE_OK0;
76584}
76585SQLITE_PRIVATEstatic int sqlite3BtreeCreateTable(Btree *p, Pgno *piTable, int flags){
76586 int rc;
76587 sqlite3BtreeEnter(p);
76588 rc = btreeCreateTable(p, piTable, flags);
76589 sqlite3BtreeLeave(p);
76590 return rc;
76591}
76592
76593/*
76594** Erase the given database page and all its children. Return
76595** the page to the freelist.
76596*/
76597static int clearDatabasePage(
76598 BtShared *pBt, /* The BTree that contains the table */
76599 Pgno pgno, /* Page number to clear */
76600 int freePageFlag, /* Deallocate page if true */
76601 i64 *pnChange /* Add number of Cells freed to this counter */
76602){
76603 MemPage *pPage;
76604 int rc;
76605 unsigned char *pCell;
76606 int i;
76607 int hdr;
76608 CellInfo info;
76609
76610 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
76611 if( pgno>btreePagecount(pBt) ){
76612 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(76612);
76613 }
76614 rc = getAndInitPage(pBt, pgno, &pPage, 0, 0);
76615 if( rc ) return rc;
76616 if( (pBt->openFlags & BTREE_SINGLE4)==0
76617 && sqlite3PagerPageRefcount(pPage->pDbPage) != (1 + (pgno==1))
76618 ){
76619 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(76619);
76620 goto cleardatabasepage_out;
76621 }
76622 hdr = pPage->hdrOffset;
76623 for(i=0; i<pPage->nCell; i++){
76624 pCell = findCell(pPage, i)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(i)])[0]<<8 | (&(pPage)->aCellIdx
[2*(i)])[1])))
;
76625 if( !pPage->leaf ){
76626 rc = clearDatabasePage(pBt, get4bytesqlite3Get4byte(pCell), 1, pnChange);
76627 if( rc ) goto cleardatabasepage_out;
76628 }
76629 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; }
;
76630 if( rc ) goto cleardatabasepage_out;
76631 }
76632 if( !pPage->leaf ){
76633 rc = clearDatabasePage(pBt, get4bytesqlite3Get4byte(&pPage->aData[hdr+8]), 1, pnChange);
76634 if( rc ) goto cleardatabasepage_out;
76635 if( pPage->intKey ) pnChange = 0;
76636 }
76637 if( pnChange ){
76638 testcase( !pPage->intKey );
76639 *pnChange += pPage->nCell;
76640 }
76641 if( freePageFlag ){
76642 freePage(pPage, &rc);
76643 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
76644 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF0x08);
76645 }
76646
76647cleardatabasepage_out:
76648 releasePage(pPage);
76649 return rc;
76650}
76651
76652/*
76653** Delete all information from a single table in the database. iTable is
76654** the page number of the root of the table. After this routine returns,
76655** the root page is empty, but still exists.
76656**
76657** This routine will fail with SQLITE_LOCKED if there are any open
76658** read cursors on the table. Open write cursors are moved to the
76659** root of the table.
76660**
76661** If pnChange is not NULL, then the integer value pointed to by pnChange
76662** is incremented by the number of entries in the table.
76663*/
76664SQLITE_PRIVATEstatic int sqlite3BtreeClearTable(Btree *p, int iTable, i64 *pnChange){
76665 int rc;
76666 BtShared *pBt = p->pBt;
76667 sqlite3BtreeEnter(p);
76668 assert( p->inTrans==TRANS_WRITE )((void) (0));
76669
76670 rc = saveAllCursors(pBt, (Pgno)iTable, 0);
76671
76672 if( SQLITE_OK0==rc ){
76673 /* Invalidate all incrblob cursors open on table iTable (assuming iTable
76674 ** is the root of a table b-tree - if it is not, the following call is
76675 ** a no-op). */
76676 if( p->hasIncrblobCur ){
76677 invalidateIncrblobCursors(p, (Pgno)iTable, 0, 1);
76678 }
76679 rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
76680 }
76681 sqlite3BtreeLeave(p);
76682 return rc;
76683}
76684
76685/*
76686** Delete all information from the single table that pCur is open on.
76687**
76688** This routine only work for pCur on an ephemeral table.
76689*/
76690SQLITE_PRIVATEstatic int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){
76691 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
76692}
76693
76694/*
76695** Erase all information in a table and add the root of the table to
76696** the freelist. Except, the root of the principle table (the one on
76697** page 1) is never added to the freelist.
76698**
76699** This routine will fail with SQLITE_LOCKED if there are any open
76700** cursors on the table.
76701**
76702** If AUTOVACUUM is enabled and the page at iTable is not the last
76703** root page in the database file, then the last root page
76704** in the database file is moved into the slot formerly occupied by
76705** iTable and that last slot formerly occupied by the last root page
76706** is added to the freelist instead of iTable. In this say, all
76707** root pages are kept at the beginning of the database file, which
76708** is necessary for AUTOVACUUM to work right. *piMoved is set to the
76709** page number that used to be the last root page in the file before
76710** the move. If no page gets moved, *piMoved is set to 0.
76711** The last root page is recorded in meta[3] and the value of
76712** meta[3] is updated by this procedure.
76713*/
76714static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
76715 int rc;
76716 MemPage *pPage = 0;
76717 BtShared *pBt = p->pBt;
76718
76719 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
76720 assert( p->inTrans==TRANS_WRITE )((void) (0));
76721 assert( iTable>=2 )((void) (0));
76722 if( iTable>btreePagecount(pBt) ){
76723 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(76723);
76724 }
76725
76726 rc = sqlite3BtreeClearTable(p, iTable, 0);
76727 if( rc ) return rc;
76728 rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
76729 if( NEVER(rc)(rc) ){
76730 releasePage(pPage);
76731 return rc;
76732 }
76733
76734 *piMoved = 0;
76735
76736#ifdef SQLITE_OMIT_AUTOVACUUM
76737 freePage(pPage, &rc);
76738 releasePage(pPage);
76739#else
76740 if( pBt->autoVacuum ){
76741 Pgno maxRootPgno;
76742 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE4, &maxRootPgno);
76743
76744 if( iTable==maxRootPgno ){
76745 /* If the table being dropped is the table with the largest root-page
76746 ** number in the database, put the root page on the free list.
76747 */
76748 freePage(pPage, &rc);
76749 releasePage(pPage);
76750 if( rc!=SQLITE_OK0 ){
76751 return rc;
76752 }
76753 }else{
76754 /* The table being dropped does not have the largest root-page
76755 ** number in the database. So move the page that does into the
76756 ** gap left by the deleted root-page.
76757 */
76758 MemPage *pMove;
76759 releasePage(pPage);
76760 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
76761 if( rc!=SQLITE_OK0 ){
76762 return rc;
76763 }
76764 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE1, 0, iTable, 0);
76765 releasePage(pMove);
76766 if( rc!=SQLITE_OK0 ){
76767 return rc;
76768 }
76769 pMove = 0;
76770 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
76771 freePage(pMove, &rc);
76772 releasePage(pMove);
76773 if( rc!=SQLITE_OK0 ){
76774 return rc;
76775 }
76776 *piMoved = maxRootPgno;
76777 }
76778
76779 /* Set the new 'max-root-page' value in the database header. This
76780 ** is the old value less one, less one more if that happens to
76781 ** be a root-page number, less one again if that is the
76782 ** PENDING_BYTE_PAGE.
76783 */
76784 maxRootPgno--;
76785 while( maxRootPgno==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1))
76786 || PTRMAP_ISPAGE(pBt, maxRootPgno)(ptrmapPageno((pBt), (maxRootPgno))==(maxRootPgno)) ){
76787 maxRootPgno--;
76788 }
76789 assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) )((void) (0));
76790
76791 rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
76792 }else{
76793 freePage(pPage, &rc);
76794 releasePage(pPage);
76795 }
76796#endif
76797 return rc;
76798}
76799SQLITE_PRIVATEstatic int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
76800 int rc;
76801 sqlite3BtreeEnter(p);
76802 rc = btreeDropTable(p, iTable, piMoved);
76803 sqlite3BtreeLeave(p);
76804 return rc;
76805}
76806
76807
76808/*
76809** This function may only be called if the b-tree connection already
76810** has a read or write transaction open on the database.
76811**
76812** Read the meta-information out of a database file. Meta[0]
76813** is the number of free pages currently in the database. Meta[1]
76814** through meta[15] are available for use by higher layers. Meta[0]
76815** is read-only, the others are read/write.
76816**
76817** The schema layer numbers meta values differently. At the schema
76818** layer (and the SetCookie and ReadCookie opcodes) the number of
76819** free pages is not visible. So Cookie[0] is the same as Meta[1].
76820**
76821** This routine treats Meta[BTREE_DATA_VERSION] as a special case. Instead
76822** of reading the value out of the header, it instead loads the "DataVersion"
76823** from the pager. The BTREE_DATA_VERSION value is not actually stored in the
76824** database file. It is a number computed by the pager. But its access
76825** pattern is the same as header meta values, and so it is convenient to
76826** read it from this routine.
76827*/
76828SQLITE_PRIVATEstatic void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
76829 BtShared *pBt = p->pBt;
76830
76831 sqlite3BtreeEnter(p);
76832 assert( p->inTrans>TRANS_NONE )((void) (0));
76833 assert( SQLITE_OK==querySharedCacheTableLock(p, SCHEMA_ROOT, READ_LOCK) )((void) (0));
76834 assert( pBt->pPage1 )((void) (0));
76835 assert( idx>=0 && idx<=15 )((void) (0));
76836
76837 if( idx==BTREE_DATA_VERSION15 ){
76838 *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iBDataVersion;
76839 }else{
76840 *pMeta = get4bytesqlite3Get4byte(&pBt->pPage1->aData[36 + idx*4]);
76841 }
76842
76843 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
76844 ** database, mark the database as read-only. */
76845#ifdef SQLITE_OMIT_AUTOVACUUM
76846 if( idx==BTREE_LARGEST_ROOT_PAGE4 && *pMeta>0 ){
76847 pBt->btsFlags |= BTS_READ_ONLY0x0001;
76848 }
76849#endif
76850
76851 sqlite3BtreeLeave(p);
76852}
76853
76854/*
76855** Write meta-information back into the database. Meta[0] is
76856** read-only and may not be written.
76857*/
76858SQLITE_PRIVATEstatic int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
76859 BtShared *pBt = p->pBt;
76860 unsigned char *pP1;
76861 int rc;
76862 assert( idx>=1 && idx<=15 )((void) (0));
76863 sqlite3BtreeEnter(p);
76864 assert( p->inTrans==TRANS_WRITE )((void) (0));
76865 assert( pBt->pPage1!=0 )((void) (0));
76866 pP1 = pBt->pPage1->aData;
76867 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
76868 if( rc==SQLITE_OK0 ){
76869 put4bytesqlite3Put4byte(&pP1[36 + idx*4], iMeta);
76870#ifndef SQLITE_OMIT_AUTOVACUUM
76871 if( idx==BTREE_INCR_VACUUM7 ){
76872 assert( pBt->autoVacuum || iMeta==0 )((void) (0));
76873 assert( iMeta==0 || iMeta==1 )((void) (0));
76874 pBt->incrVacuum = (u8)iMeta;
76875 }
76876#endif
76877 }
76878 sqlite3BtreeLeave(p);
76879 return rc;
76880}
76881
76882/*
76883** The first argument, pCur, is a cursor opened on some b-tree. Count the
76884** number of entries in the b-tree and write the result to *pnEntry.
76885**
76886** SQLITE_OK is returned if the operation is successfully executed.
76887** Otherwise, if an error is encountered (i.e. an IO error or database
76888** corruption) an SQLite error code is returned.
76889*/
76890SQLITE_PRIVATEstatic int sqlite3BtreeCount(sqlite3 *db, BtCursor *pCur, i64 *pnEntry){
76891 i64 nEntry = 0; /* Value to return in *pnEntry */
76892 int rc; /* Return code */
76893
76894 rc = moveToRoot(pCur);
76895 if( rc==SQLITE_EMPTY16 ){
76896 *pnEntry = 0;
76897 return SQLITE_OK0;
76898 }
76899
76900 /* Unless an error occurs, the following loop runs one iteration for each
76901 ** page in the B-Tree structure (not including overflow pages).
76902 */
76903 while( rc==SQLITE_OK0 && !AtomicLoad(&db->u1.isInterrupted)__atomic_load_n((&db->u1.isInterrupted),0) ){
76904 int iIdx; /* Index of child node in parent */
76905 MemPage *pPage; /* Current page of the b-tree */
76906
76907 /* If this is a leaf page or the tree is not an int-key tree, then
76908 ** this page contains countable entries. Increment the entry counter
76909 ** accordingly.
76910 */
76911 pPage = pCur->pPage;
76912 if( pPage->leaf || !pPage->intKey ){
76913 nEntry += pPage->nCell;
76914 }
76915
76916 /* pPage is a leaf node. This loop navigates the cursor so that it
76917 ** points to the first interior cell that it points to the parent of
76918 ** the next page in the tree that has not yet been visited. The
76919 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
76920 ** of the page, or to the number of cells in the page if the next page
76921 ** to visit is the right-child of its parent.
76922 **
76923 ** If all pages in the tree have been visited, return SQLITE_OK to the
76924 ** caller.
76925 */
76926 if( pPage->leaf ){
76927 do {
76928 if( pCur->iPage==0 ){
76929 /* All pages of the b-tree have been visited. Return successfully. */
76930 *pnEntry = nEntry;
76931 return moveToRoot(pCur);
76932 }
76933 moveToParent(pCur);
76934 }while ( pCur->ix>=pCur->pPage->nCell );
76935
76936 pCur->ix++;
76937 pPage = pCur->pPage;
76938 }
76939
76940 /* Descend to the child node of the cell that the cursor currently
76941 ** points at. This is the right-child if (iIdx==pPage->nCell).
76942 */
76943 iIdx = pCur->ix;
76944 if( iIdx==pPage->nCell ){
76945 rc = moveToChild(pCur, get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]));
76946 }else{
76947 rc = moveToChild(pCur, get4bytesqlite3Get4byte(findCell(pPage, iIdx)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(iIdx)])[0]<<8 | (&(pPage)->aCellIdx
[2*(iIdx)])[1])))
));
76948 }
76949 }
76950
76951 /* An error has occurred. Return an error code. */
76952 return rc;
76953}
76954
76955/*
76956** Return the pager associated with a BTree. This routine is used for
76957** testing and debugging only.
76958*/
76959SQLITE_PRIVATEstatic Pager *sqlite3BtreePager(Btree *p){
76960 return p->pBt->pPager;
76961}
76962
76963#ifndef SQLITE_OMIT_INTEGRITY_CHECK
76964/*
76965** Append a message to the error message string.
76966*/
76967static void checkAppendMsg(
76968 IntegrityCk *pCheck,
76969 const char *zFormat,
76970 ...
76971){
76972 va_list ap;
76973 if( !pCheck->mxErr ) return;
76974 pCheck->mxErr--;
76975 pCheck->nErr++;
76976 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
76977 if( pCheck->errMsg.nChar ){
76978 sqlite3_str_append(&pCheck->errMsg, "\n", 1);
76979 }
76980 if( pCheck->zPfx ){
76981 sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2);
76982 }
76983 sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap);
76984 va_end(ap)__builtin_va_end(ap);
76985 if( pCheck->errMsg.accError==SQLITE_NOMEM7 ){
76986 pCheck->bOomFault = 1;
76987 }
76988}
76989#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
76990
76991#ifndef SQLITE_OMIT_INTEGRITY_CHECK
76992
76993/*
76994** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
76995** corresponds to page iPg is already set.
76996*/
76997static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){
76998 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 )((void) (0));
76999 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
77000}
77001
77002/*
77003** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg.
77004*/
77005static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){
77006 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 )((void) (0));
77007 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
77008}
77009
77010
77011/*
77012** Add 1 to the reference count for page iPage. If this is the second
77013** reference to the page, add an error message to pCheck->zErrMsg.
77014** Return 1 if there are 2 or more references to the page and 0 if
77015** if this is the first reference to the page.
77016**
77017** Also check that the page number is in bounds.
77018*/
77019static int checkRef(IntegrityCk *pCheck, Pgno iPage){
77020 if( iPage>pCheck->nPage || iPage==0 ){
77021 checkAppendMsg(pCheck, "invalid page number %d", iPage);
77022 return 1;
77023 }
77024 if( getPageReferenced(pCheck, iPage) ){
77025 checkAppendMsg(pCheck, "2nd reference to page %d", iPage);
77026 return 1;
77027 }
77028 if( AtomicLoad(&pCheck->db->u1.isInterrupted)__atomic_load_n((&pCheck->db->u1.isInterrupted),0) ) return 1;
77029 setPageReferenced(pCheck, iPage);
77030 return 0;
77031}
77032
77033#ifndef SQLITE_OMIT_AUTOVACUUM
77034/*
77035** Check that the entry in the pointer-map for page iChild maps to
77036** page iParent, pointer type ptrType. If not, append an error message
77037** to pCheck.
77038*/
77039static void checkPtrmap(
77040 IntegrityCk *pCheck, /* Integrity check context */
77041 Pgno iChild, /* Child page number */
77042 u8 eType, /* Expected pointer map type */
77043 Pgno iParent /* Expected pointer map parent page number */
77044){
77045 int rc;
77046 u8 ePtrmapType;
77047 Pgno iPtrmapParent;
77048
77049 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
77050 if( rc!=SQLITE_OK0 ){
77051 if( rc==SQLITE_NOMEM7 || rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ) pCheck->bOomFault = 1;
77052 checkAppendMsg(pCheck, "Failed to read ptrmap key=%d", iChild);
77053 return;
77054 }
77055
77056 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
77057 checkAppendMsg(pCheck,
77058 "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
77059 iChild, eType, iParent, ePtrmapType, iPtrmapParent);
77060 }
77061}
77062#endif
77063
77064/*
77065** Check the integrity of the freelist or of an overflow page list.
77066** Verify that the number of pages on the list is N.
77067*/
77068static void checkList(
77069 IntegrityCk *pCheck, /* Integrity checking context */
77070 int isFreeList, /* True for a freelist. False for overflow page list */
77071 Pgno iPage, /* Page number for first page in the list */
77072 u32 N /* Expected number of pages in the list */
77073){
77074 int i;
77075 u32 expected = N;
77076 int nErrAtStart = pCheck->nErr;
77077 while( iPage!=0 && pCheck->mxErr ){
77078 DbPage *pOvflPage;
77079 unsigned char *pOvflData;
77080 if( checkRef(pCheck, iPage) ) break;
77081 N--;
77082 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
77083 checkAppendMsg(pCheck, "failed to get page %d", iPage);
77084 break;
77085 }
77086 pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
77087 if( isFreeList ){
77088 u32 n = (u32)get4bytesqlite3Get4byte(&pOvflData[4]);
77089#ifndef SQLITE_OMIT_AUTOVACUUM
77090 if( pCheck->pBt->autoVacuum ){
77091 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE2, 0);
77092 }
77093#endif
77094 if( n>pCheck->pBt->usableSize/4-2 ){
77095 checkAppendMsg(pCheck,
77096 "freelist leaf count too big on page %d", iPage);
77097 N--;
77098 }else{
77099 for(i=0; i<(int)n; i++){
77100 Pgno iFreePage = get4bytesqlite3Get4byte(&pOvflData[8+i*4]);
77101#ifndef SQLITE_OMIT_AUTOVACUUM
77102 if( pCheck->pBt->autoVacuum ){
77103 checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE2, 0);
77104 }
77105#endif
77106 checkRef(pCheck, iFreePage);
77107 }
77108 N -= n;
77109 }
77110 }
77111#ifndef SQLITE_OMIT_AUTOVACUUM
77112 else{
77113 /* If this database supports auto-vacuum and iPage is not the last
77114 ** page in this overflow list, check that the pointer-map entry for
77115 ** the following page matches iPage.
77116 */
77117 if( pCheck->pBt->autoVacuum && N>0 ){
77118 i = get4bytesqlite3Get4byte(pOvflData);
77119 checkPtrmap(pCheck, i, PTRMAP_OVERFLOW24, iPage);
77120 }
77121 }
77122#endif
77123 iPage = get4bytesqlite3Get4byte(pOvflData);
77124 sqlite3PagerUnref(pOvflPage);
77125 }
77126 if( N && nErrAtStart==pCheck->nErr ){
77127 checkAppendMsg(pCheck,
77128 "%s is %d but should be %d",
77129 isFreeList ? "size" : "overflow list length",
77130 expected-N, expected);
77131 }
77132}
77133#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
77134
77135/*
77136** An implementation of a min-heap.
77137**
77138** aHeap[0] is the number of elements on the heap. aHeap[1] is the
77139** root element. The daughter nodes of aHeap[N] are aHeap[N*2]
77140** and aHeap[N*2+1].
77141**
77142** The heap property is this: Every node is less than or equal to both
77143** of its daughter nodes. A consequence of the heap property is that the
77144** root node aHeap[1] is always the minimum value currently in the heap.
77145**
77146** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
77147** the heap, preserving the heap property. The btreeHeapPull() routine
77148** removes the root element from the heap (the minimum value in the heap)
77149** and then moves other nodes around as necessary to preserve the heap
77150** property.
77151**
77152** This heap is used for cell overlap and coverage testing. Each u32
77153** entry represents the span of a cell or freeblock on a btree page.
77154** The upper 16 bits are the index of the first byte of a range and the
77155** lower 16 bits are the index of the last byte of that range.
77156*/
77157static void btreeHeapInsert(u32 *aHeap, u32 x){
77158 u32 j, i = ++aHeap[0];
77159 aHeap[i] = x;
77160 while( (j = i/2)>0 && aHeap[j]>aHeap[i] ){
77161 x = aHeap[j];
77162 aHeap[j] = aHeap[i];
77163 aHeap[i] = x;
77164 i = j;
77165 }
77166}
77167static int btreeHeapPull(u32 *aHeap, u32 *pOut){
77168 u32 j, i, x;
77169 if( (x = aHeap[0])==0 ) return 0;
77170 *pOut = aHeap[1];
77171 aHeap[1] = aHeap[x];
77172 aHeap[x] = 0xffffffff;
77173 aHeap[0]--;
77174 i = 1;
77175 while( (j = i*2)<=aHeap[0] ){
77176 if( aHeap[j]>aHeap[j+1] ) j++;
77177 if( aHeap[i]<aHeap[j] ) break;
77178 x = aHeap[i];
77179 aHeap[i] = aHeap[j];
77180 aHeap[j] = x;
77181 i = j;
77182 }
77183 return 1;
77184}
77185
77186#ifndef SQLITE_OMIT_INTEGRITY_CHECK
77187/*
77188** Do various sanity checks on a single page of a tree. Return
77189** the tree depth. Root pages return 0. Parents of root pages
77190** return 1, and so forth.
77191**
77192** These checks are done:
77193**
77194** 1. Make sure that cells and freeblocks do not overlap
77195** but combine to completely cover the page.
77196** 2. Make sure integer cell keys are in order.
77197** 3. Check the integrity of overflow pages.
77198** 4. Recursively call checkTreePage on all children.
77199** 5. Verify that the depth of all children is the same.
77200*/
77201static int checkTreePage(
77202 IntegrityCk *pCheck, /* Context for the sanity check */
77203 Pgno iPage, /* Page number of the page to check */
77204 i64 *piMinKey, /* Write minimum integer primary key here */
77205 i64 maxKey /* Error if integer primary key greater than this */
77206){
77207 MemPage *pPage = 0; /* The page being analyzed */
77208 int i; /* Loop counter */
77209 int rc; /* Result code from subroutine call */
77210 int depth = -1, d2; /* Depth of a subtree */
77211 int pgno; /* Page number */
77212 int nFrag; /* Number of fragmented bytes on the page */
77213 int hdr; /* Offset to the page header */
77214 int cellStart; /* Offset to the start of the cell pointer array */
77215 int nCell; /* Number of cells */
77216 int doCoverageCheck = 1; /* True if cell coverage checking should be done */
77217 int keyCanBeEqual = 1; /* True if IPK can be equal to maxKey
77218 ** False if IPK must be strictly less than maxKey */
77219 u8 *data; /* Page content */
77220 u8 *pCell; /* Cell content */
77221 u8 *pCellIdx; /* Next element of the cell pointer array */
77222 BtShared *pBt; /* The BtShared object that owns pPage */
77223 u32 pc; /* Address of a cell */
77224 u32 usableSize; /* Usable size of the page */
77225 u32 contentOffset; /* Offset to the start of the cell content area */
77226 u32 *heap = 0; /* Min-heap used for checking cell coverage */
77227 u32 x, prev = 0; /* Next and previous entry on the min-heap */
77228 const char *saved_zPfx = pCheck->zPfx;
77229 int saved_v1 = pCheck->v1;
77230 int saved_v2 = pCheck->v2;
77231 u8 savedIsInit = 0;
77232
77233 /* Check that the page exists
77234 */
77235 pBt = pCheck->pBt;
77236 usableSize = pBt->usableSize;
77237 if( iPage==0 ) return 0;
77238 if( checkRef(pCheck, iPage) ) return 0;
77239 pCheck->zPfx = "Page %u: ";
77240 pCheck->v1 = iPage;
77241 if( (rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0 ){
77242 checkAppendMsg(pCheck,
77243 "unable to get the page. error code=%d", rc);
77244 goto end_of_check;
77245 }
77246
77247 /* Clear MemPage.isInit to make sure the corruption detection code in
77248 ** btreeInitPage() is executed. */
77249 savedIsInit = pPage->isInit;
77250 pPage->isInit = 0;
77251 if( (rc = btreeInitPage(pPage))!=0 ){
77252 assert( rc==SQLITE_CORRUPT )((void) (0)); /* The only possible error from InitPage */
77253 checkAppendMsg(pCheck,
77254 "btreeInitPage() returns error code %d", rc);
77255 goto end_of_check;
77256 }
77257 if( (rc = btreeComputeFreeSpace(pPage))!=0 ){
77258 assert( rc==SQLITE_CORRUPT )((void) (0));
77259 checkAppendMsg(pCheck, "free space corruption", rc);
77260 goto end_of_check;
77261 }
77262 data = pPage->aData;
77263 hdr = pPage->hdrOffset;
77264
77265 /* Set up for cell analysis */
77266 pCheck->zPfx = "On tree page %u cell %d: ";
77267 contentOffset = get2byteNotZero(&data[hdr+5])(((((int)((&data[hdr+5])[0]<<8 | (&data[hdr+5])
[1]))-1)&0xffff)+1)
;
77268 assert( contentOffset<=usableSize )((void) (0)); /* Enforced by btreeInitPage() */
77269
77270 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
77271 ** number of cells on the page. */
77272 nCell = get2byte(&data[hdr+3])((&data[hdr+3])[0]<<8 | (&data[hdr+3])[1]);
77273 assert( pPage->nCell==nCell )((void) (0));
77274
77275 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
77276 ** immediately follows the b-tree page header. */
77277 cellStart = hdr + 12 - 4*pPage->leaf;
77278 assert( pPage->aCellIdx==&data[cellStart] )((void) (0));
77279 pCellIdx = &data[cellStart + 2*(nCell-1)];
77280
77281 if( !pPage->leaf ){
77282 /* Analyze the right-child page of internal pages */
77283 pgno = get4bytesqlite3Get4byte(&data[hdr+8]);
77284#ifndef SQLITE_OMIT_AUTOVACUUM
77285 if( pBt->autoVacuum ){
77286 pCheck->zPfx = "On page %u at right child: ";
77287 checkPtrmap(pCheck, pgno, PTRMAP_BTREE5, iPage);
77288 }
77289#endif
77290 depth = checkTreePage(pCheck, pgno, &maxKey, maxKey);
77291 keyCanBeEqual = 0;
77292 }else{
77293 /* For leaf pages, the coverage check will occur in the same loop
77294 ** as the other cell checks, so initialize the heap. */
77295 heap = pCheck->heap;
77296 heap[0] = 0;
77297 }
77298
77299 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
77300 ** integer offsets to the cell contents. */
77301 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
77302 CellInfo info;
77303
77304 /* Check cell size */
77305 pCheck->v2 = i;
77306 assert( pCellIdx==&data[cellStart + i*2] )((void) (0));
77307 pc = get2byteAligned(pCellIdx)((pCellIdx)[0]<<8 | (pCellIdx)[1]);
77308 pCellIdx -= 2;
77309 if( pc<contentOffset || pc>usableSize-4 ){
77310 checkAppendMsg(pCheck, "Offset %d out of range %d..%d",
77311 pc, contentOffset, usableSize-4);
77312 doCoverageCheck = 0;
77313 continue;
77314 }
77315 pCell = &data[pc];
77316 pPage->xParseCell(pPage, pCell, &info);
77317 if( pc+info.nSize>usableSize ){
77318 checkAppendMsg(pCheck, "Extends off end of page");
77319 doCoverageCheck = 0;
77320 continue;
77321 }
77322
77323 /* Check for integer primary key out of range */
77324 if( pPage->intKey ){
77325 if( keyCanBeEqual ? (info.nKey > maxKey) : (info.nKey >= maxKey) ){
77326 checkAppendMsg(pCheck, "Rowid %lld out of order", info.nKey);
77327 }
77328 maxKey = info.nKey;
77329 keyCanBeEqual = 0; /* Only the first key on the page may ==maxKey */
77330 }
77331
77332 /* Check the content overflow list */
77333 if( info.nPayload>info.nLocal ){
77334 u32 nPage; /* Number of pages on the overflow chain */
77335 Pgno pgnoOvfl; /* First page of the overflow chain */
77336 assert( pc + info.nSize - 4 <= usableSize )((void) (0));
77337 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
77338 pgnoOvfl = get4bytesqlite3Get4byte(&pCell[info.nSize - 4]);
77339#ifndef SQLITE_OMIT_AUTOVACUUM
77340 if( pBt->autoVacuum ){
77341 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW13, iPage);
77342 }
77343#endif
77344 checkList(pCheck, 0, pgnoOvfl, nPage);
77345 }
77346
77347 if( !pPage->leaf ){
77348 /* Check sanity of left child page for internal pages */
77349 pgno = get4bytesqlite3Get4byte(pCell);
77350#ifndef SQLITE_OMIT_AUTOVACUUM
77351 if( pBt->autoVacuum ){
77352 checkPtrmap(pCheck, pgno, PTRMAP_BTREE5, iPage);
77353 }
77354#endif
77355 d2 = checkTreePage(pCheck, pgno, &maxKey, maxKey);
77356 keyCanBeEqual = 0;
77357 if( d2!=depth ){
77358 checkAppendMsg(pCheck, "Child page depth differs");
77359 depth = d2;
77360 }
77361 }else{
77362 /* Populate the coverage-checking heap for leaf pages */
77363 btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
77364 }
77365 }
77366 *piMinKey = maxKey;
77367
77368 /* Check for complete coverage of the page
77369 */
77370 pCheck->zPfx = 0;
77371 if( doCoverageCheck && pCheck->mxErr>0 ){
77372 /* For leaf pages, the min-heap has already been initialized and the
77373 ** cells have already been inserted. But for internal pages, that has
77374 ** not yet been done, so do it now */
77375 if( !pPage->leaf ){
77376 heap = pCheck->heap;
77377 heap[0] = 0;
77378 for(i=nCell-1; i>=0; i--){
77379 u32 size;
77380 pc = get2byteAligned(&data[cellStart+i*2])((&data[cellStart+i*2])[0]<<8 | (&data[cellStart
+i*2])[1])
;
77381 size = pPage->xCellSize(pPage, &data[pc]);
77382 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
77383 }
77384 }
77385 /* Add the freeblocks to the min-heap
77386 **
77387 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
77388 ** is the offset of the first freeblock, or zero if there are no
77389 ** freeblocks on the page.
77390 */
77391 i = get2byte(&data[hdr+1])((&data[hdr+1])[0]<<8 | (&data[hdr+1])[1]);
77392 while( i>0 ){
77393 int size, j;
77394 assert( (u32)i<=usableSize-4 )((void) (0)); /* Enforced by btreeComputeFreeSpace() */
77395 size = get2byte(&data[i+2])((&data[i+2])[0]<<8 | (&data[i+2])[1]);
77396 assert( (u32)(i+size)<=usableSize )((void) (0)); /* due to btreeComputeFreeSpace() */
77397 btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
77398 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
77399 ** big-endian integer which is the offset in the b-tree page of the next
77400 ** freeblock in the chain, or zero if the freeblock is the last on the
77401 ** chain. */
77402 j = get2byte(&data[i])((&data[i])[0]<<8 | (&data[i])[1]);
77403 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
77404 ** increasing offset. */
77405 assert( j==0 || j>i+size )((void) (0)); /* Enforced by btreeComputeFreeSpace() */
77406 assert( (u32)j<=usableSize-4 )((void) (0)); /* Enforced by btreeComputeFreeSpace() */
77407 i = j;
77408 }
77409 /* Analyze the min-heap looking for overlap between cells and/or
77410 ** freeblocks, and counting the number of untracked bytes in nFrag.
77411 **
77412 ** Each min-heap entry is of the form: (start_address<<16)|end_address.
77413 ** There is an implied first entry the covers the page header, the cell
77414 ** pointer index, and the gap between the cell pointer index and the start
77415 ** of cell content.
77416 **
77417 ** The loop below pulls entries from the min-heap in order and compares
77418 ** the start_address against the previous end_address. If there is an
77419 ** overlap, that means bytes are used multiple times. If there is a gap,
77420 ** that gap is added to the fragmentation count.
77421 */
77422 nFrag = 0;
77423 prev = contentOffset - 1; /* Implied first min-heap entry */
77424 while( btreeHeapPull(heap,&x) ){
77425 if( (prev&0xffff)>=(x>>16) ){
77426 checkAppendMsg(pCheck,
77427 "Multiple uses for byte %u of page %u", x>>16, iPage);
77428 break;
77429 }else{
77430 nFrag += (x>>16) - (prev&0xffff) - 1;
77431 prev = x;
77432 }
77433 }
77434 nFrag += usableSize - (prev&0xffff) - 1;
77435 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
77436 ** is stored in the fifth field of the b-tree page header.
77437 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
77438 ** number of fragmented free bytes within the cell content area.
77439 */
77440 if( heap[0]==0 && nFrag!=data[hdr+7] ){
77441 checkAppendMsg(pCheck,
77442 "Fragmentation of %d bytes reported as %d on page %u",
77443 nFrag, data[hdr+7], iPage);
77444 }
77445 }
77446
77447end_of_check:
77448 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
77449 releasePage(pPage);
77450 pCheck->zPfx = saved_zPfx;
77451 pCheck->v1 = saved_v1;
77452 pCheck->v2 = saved_v2;
77453 return depth+1;
77454}
77455#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
77456
77457#ifndef SQLITE_OMIT_INTEGRITY_CHECK
77458/*
77459** This routine does a complete check of the given BTree file. aRoot[] is
77460** an array of pages numbers were each page number is the root page of
77461** a table. nRoot is the number of entries in aRoot.
77462**
77463** A read-only or read-write transaction must be opened before calling
77464** this function.
77465**
77466** Write the number of error seen in *pnErr. Except for some memory
77467** allocation errors, an error message held in memory obtained from
77468** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
77469** returned. If a memory allocation error occurs, NULL is returned.
77470**
77471** If the first entry in aRoot[] is 0, that indicates that the list of
77472** root pages is incomplete. This is a "partial integrity-check". This
77473** happens when performing an integrity check on a single table. The
77474** zero is skipped, of course. But in addition, the freelist checks
77475** and the checks to make sure every page is referenced are also skipped,
77476** since obviously it is not possible to know which pages are covered by
77477** the unverified btrees. Except, if aRoot[1] is 1, then the freelist
77478** checks are still performed.
77479*/
77480SQLITE_PRIVATEstatic char *sqlite3BtreeIntegrityCheck(
77481 sqlite3 *db, /* Database connection that is running the check */
77482 Btree *p, /* The btree to be checked */
77483 Pgno *aRoot, /* An array of root pages numbers for individual trees */
77484 int nRoot, /* Number of entries in aRoot[] */
77485 int mxErr, /* Stop reporting errors after this many */
77486 int *pnErr /* Write number of errors seen to this variable */
77487){
77488 Pgno i;
77489 IntegrityCk sCheck;
77490 BtShared *pBt = p->pBt;
77491 u64 savedDbFlags = pBt->db->flags;
77492 char zErr[100];
77493 int bPartial = 0; /* True if not checking all btrees */
77494 int bCkFreelist = 1; /* True to scan the freelist */
77495 VVA_ONLY( int nRef );
77496 assert( nRoot>0 )((void) (0));
77497
77498 /* aRoot[0]==0 means this is a partial check */
77499 if( aRoot[0]==0 ){
77500 assert( nRoot>1 )((void) (0));
77501 bPartial = 1;
77502 if( aRoot[1]!=1 ) bCkFreelist = 0;
77503 }
77504
77505 sqlite3BtreeEnter(p);
77506 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE )((void) (0));
77507 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
77508 assert( nRef>=0 )((void) (0));
77509 sCheck.db = db;
77510 sCheck.pBt = pBt;
77511 sCheck.pPager = pBt->pPager;
77512 sCheck.nPage = btreePagecount(sCheck.pBt);
77513 sCheck.mxErr = mxErr;
77514 sCheck.nErr = 0;
77515 sCheck.bOomFault = 0;
77516 sCheck.zPfx = 0;
77517 sCheck.v1 = 0;
77518 sCheck.v2 = 0;
77519 sCheck.aPgRef = 0;
77520 sCheck.heap = 0;
77521 sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH2147483645);
77522 sCheck.errMsg.printfFlags = SQLITE_PRINTF_INTERNAL0x01;
77523 if( sCheck.nPage==0 ){
77524 goto integrity_ck_cleanup;
77525 }
77526
77527 sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
77528 if( !sCheck.aPgRef ){
77529 sCheck.bOomFault = 1;
77530 goto integrity_ck_cleanup;
77531 }
77532 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
77533 if( sCheck.heap==0 ){
77534 sCheck.bOomFault = 1;
77535 goto integrity_ck_cleanup;
77536 }
77537
77538 i = PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1));
77539 if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
77540
77541 /* Check the integrity of the freelist
77542 */
77543 if( bCkFreelist ){
77544 sCheck.zPfx = "Main freelist: ";
77545 checkList(&sCheck, 1, get4bytesqlite3Get4byte(&pBt->pPage1->aData[32]),
77546 get4bytesqlite3Get4byte(&pBt->pPage1->aData[36]));
77547 sCheck.zPfx = 0;
77548 }
77549
77550 /* Check all the tables.
77551 */
77552#ifndef SQLITE_OMIT_AUTOVACUUM
77553 if( !bPartial ){
77554 if( pBt->autoVacuum ){
77555 Pgno mx = 0;
77556 Pgno mxInHdr;
77557 for(i=0; (int)i<nRoot; i++) if( mx<aRoot[i] ) mx = aRoot[i];
77558 mxInHdr = get4bytesqlite3Get4byte(&pBt->pPage1->aData[52]);
77559 if( mx!=mxInHdr ){
77560 checkAppendMsg(&sCheck,
77561 "max rootpage (%d) disagrees with header (%d)",
77562 mx, mxInHdr
77563 );
77564 }
77565 }else if( get4bytesqlite3Get4byte(&pBt->pPage1->aData[64])!=0 ){
77566 checkAppendMsg(&sCheck,
77567 "incremental_vacuum enabled with a max rootpage of zero"
77568 );
77569 }
77570 }
77571#endif
77572 testcase( pBt->db->flags & SQLITE_CellSizeCk );
77573 pBt->db->flags &= ~(u64)SQLITE_CellSizeCk0x00200000;
77574 for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
77575 i64 notUsed;
77576 if( aRoot[i]==0 ) continue;
77577#ifndef SQLITE_OMIT_AUTOVACUUM
77578 if( pBt->autoVacuum && aRoot[i]>1 && !bPartial ){
77579 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE1, 0);
77580 }
77581#endif
77582 checkTreePage(&sCheck, aRoot[i], &notUsed, LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)));
77583 }
77584 pBt->db->flags = savedDbFlags;
77585
77586 /* Make sure every page in the file is referenced
77587 */
77588 if( !bPartial ){
77589 for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
77590#ifdef SQLITE_OMIT_AUTOVACUUM
77591 if( getPageReferenced(&sCheck, i)==0 ){
77592 checkAppendMsg(&sCheck, "Page %d is never used", i);
77593 }
77594#else
77595 /* If the database supports auto-vacuum, make sure no tables contain
77596 ** references to pointer-map pages.
77597 */
77598 if( getPageReferenced(&sCheck, i)==0 &&
77599 (PTRMAP_PAGENO(pBt, i)ptrmapPageno(pBt, i)!=i || !pBt->autoVacuum) ){
77600 checkAppendMsg(&sCheck, "Page %d is never used", i);
77601 }
77602 if( getPageReferenced(&sCheck, i)!=0 &&
77603 (PTRMAP_PAGENO(pBt, i)ptrmapPageno(pBt, i)==i && pBt->autoVacuum) ){
77604 checkAppendMsg(&sCheck, "Pointer map page %d is referenced", i);
77605 }
77606#endif
77607 }
77608 }
77609
77610 /* Clean up and report errors.
77611 */
77612integrity_ck_cleanup:
77613 sqlite3PageFree(sCheck.heap);
77614 sqlite3_free(sCheck.aPgRef);
77615 if( sCheck.bOomFault ){
77616 sqlite3_str_reset(&sCheck.errMsg);
77617 sCheck.nErr++;
77618 }
77619 *pnErr = sCheck.nErr;
77620 if( sCheck.nErr==0 ) sqlite3_str_reset(&sCheck.errMsg);
77621 /* Make sure this analysis did not leave any unref() pages. */
77622 assert( nRef==sqlite3PagerRefcount(pBt->pPager) )((void) (0));
77623 sqlite3BtreeLeave(p);
77624 return sqlite3StrAccumFinish(&sCheck.errMsg);
77625}
77626#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
77627
77628/*
77629** Return the full pathname of the underlying database file. Return
77630** an empty string if the database is in-memory or a TEMP database.
77631**
77632** The pager filename is invariant as long as the pager is
77633** open so it is safe to access without the BtShared mutex.
77634*/
77635SQLITE_PRIVATEstatic const char *sqlite3BtreeGetFilename(Btree *p){
77636 assert( p->pBt->pPager!=0 )((void) (0));
77637 return sqlite3PagerFilename(p->pBt->pPager, 1);
77638}
77639
77640/*
77641** Return the pathname of the journal file for this database. The return
77642** value of this routine is the same regardless of whether the journal file
77643** has been created or not.
77644**
77645** The pager journal filename is invariant as long as the pager is
77646** open so it is safe to access without the BtShared mutex.
77647*/
77648SQLITE_PRIVATEstatic const char *sqlite3BtreeGetJournalname(Btree *p){
77649 assert( p->pBt->pPager!=0 )((void) (0));
77650 return sqlite3PagerJournalname(p->pBt->pPager);
77651}
77652
77653/*
77654** Return one of SQLITE_TXN_NONE, SQLITE_TXN_READ, or SQLITE_TXN_WRITE
77655** to describe the current transaction state of Btree p.
77656*/
77657SQLITE_PRIVATEstatic int sqlite3BtreeTxnState(Btree *p){
77658 assert( p==0 || sqlite3_mutex_held(p->db->mutex) )((void) (0));
77659 return p ? p->inTrans : 0;
77660}
77661
77662#ifndef SQLITE_OMIT_WAL
77663/*
77664** Run a checkpoint on the Btree passed as the first argument.
77665**
77666** Return SQLITE_LOCKED if this or any other connection has an open
77667** transaction on the shared-cache the argument Btree is connected to.
77668**
77669** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
77670*/
77671SQLITE_PRIVATEstatic int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
77672 int rc = SQLITE_OK0;
77673 if( p ){
77674 BtShared *pBt = p->pBt;
77675 sqlite3BtreeEnter(p);
77676 if( pBt->inTransaction!=TRANS_NONE0 ){
77677 rc = SQLITE_LOCKED6;
77678 }else{
77679 rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
77680 }
77681 sqlite3BtreeLeave(p);
77682 }
77683 return rc;
77684}
77685#endif
77686
77687/*
77688** Return true if there is currently a backup running on Btree p.
77689*/
77690SQLITE_PRIVATEstatic int sqlite3BtreeIsInBackup(Btree *p){
77691 assert( p )((void) (0));
77692 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
77693 return p->nBackup!=0;
77694}
77695
77696/*
77697** This function returns a pointer to a blob of memory associated with
77698** a single shared-btree. The memory is used by client code for its own
77699** purposes (for example, to store a high-level schema associated with
77700** the shared-btree). The btree layer manages reference counting issues.
77701**
77702** The first time this is called on a shared-btree, nBytes bytes of memory
77703** are allocated, zeroed, and returned to the caller. For each subsequent
77704** call the nBytes parameter is ignored and a pointer to the same blob
77705** of memory returned.
77706**
77707** If the nBytes parameter is 0 and the blob of memory has not yet been
77708** allocated, a null pointer is returned. If the blob has already been
77709** allocated, it is returned as normal.
77710**
77711** Just before the shared-btree is closed, the function passed as the
77712** xFree argument when the memory allocation was made is invoked on the
77713** blob of allocated memory. The xFree function should not call sqlite3_free()
77714** on the memory, the btree layer does that.
77715*/
77716SQLITE_PRIVATEstatic void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
77717 BtShared *pBt = p->pBt;
77718 sqlite3BtreeEnter(p);
77719 if( !pBt->pSchema && nBytes ){
77720 pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
77721 pBt->xFreeSchema = xFree;
77722 }
77723 sqlite3BtreeLeave(p);
77724 return pBt->pSchema;
77725}
77726
77727/*
77728** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared
77729** btree as the argument handle holds an exclusive lock on the
77730** sqlite_schema table. Otherwise SQLITE_OK.
77731*/
77732SQLITE_PRIVATEstatic int sqlite3BtreeSchemaLocked(Btree *p){
77733 int rc;
77734 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
77735 sqlite3BtreeEnter(p);
77736 rc = querySharedCacheTableLock(p, SCHEMA_ROOT1, READ_LOCK1);
77737 assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE )((void) (0));
77738 sqlite3BtreeLeave(p);
77739 return rc;
77740}
77741
77742
77743#ifndef SQLITE_OMIT_SHARED_CACHE
77744/*
77745** Obtain a lock on the table whose root page is iTab. The
77746** lock is a write lock if isWritelock is true or a read lock
77747** if it is false.
77748*/
77749SQLITE_PRIVATEstatic int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
77750 int rc = SQLITE_OK0;
77751 assert( p->inTrans!=TRANS_NONE )((void) (0));
77752 if( p->sharable ){
77753 u8 lockType = READ_LOCK1 + isWriteLock;
77754 assert( READ_LOCK+1==WRITE_LOCK )((void) (0));
77755 assert( isWriteLock==0 || isWriteLock==1 )((void) (0));
77756
77757 sqlite3BtreeEnter(p);
77758 rc = querySharedCacheTableLock(p, iTab, lockType);
77759 if( rc==SQLITE_OK0 ){
77760 rc = setSharedCacheTableLock(p, iTab, lockType);
77761 }
77762 sqlite3BtreeLeave(p);
77763 }
77764 return rc;
77765}
77766#endif
77767
77768#ifndef SQLITE_OMIT_INCRBLOB
77769/*
77770** Argument pCsr must be a cursor opened for writing on an
77771** INTKEY table currently pointing at a valid table entry.
77772** This function modifies the data stored as part of that entry.
77773**
77774** Only the data content may only be modified, it is not possible to
77775** change the length of the data stored. If this function is called with
77776** parameters that attempt to write past the end of the existing data,
77777** no modifications are made and SQLITE_CORRUPT is returned.
77778*/
77779SQLITE_PRIVATEstatic int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
77780 int rc;
77781 assert( cursorOwnsBtShared(pCsr) )((void) (0));
77782 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) )((void) (0));
77783 assert( pCsr->curFlags & BTCF_Incrblob )((void) (0));
77784
77785 rc = restoreCursorPosition(pCsr)(pCsr->eState>=3 ? btreeRestoreCursorPosition(pCsr) : 0
)
;
77786 if( rc!=SQLITE_OK0 ){
77787 return rc;
77788 }
77789 assert( pCsr->eState!=CURSOR_REQUIRESEEK )((void) (0));
77790 if( pCsr->eState!=CURSOR_VALID0 ){
77791 return SQLITE_ABORT4;
77792 }
77793
77794 /* Save the positions of all other cursors open on this table. This is
77795 ** required in case any of them are holding references to an xFetch
77796 ** version of the b-tree page modified by the accessPayload call below.
77797 **
77798 ** Note that pCsr must be open on a INTKEY table and saveCursorPosition()
77799 ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
77800 ** saveAllCursors can only return SQLITE_OK.
77801 */
77802 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
77803 assert( rc==SQLITE_OK )((void) (0));
77804
77805 /* Check some assumptions:
77806 ** (a) the cursor is open for writing,
77807 ** (b) there is a read/write transaction open,
77808 ** (c) the connection holds a write-lock on the table (if required),
77809 ** (d) there are no conflicting read-locks, and
77810 ** (e) the cursor points at a valid row of an intKey table.
77811 */
77812 if( (pCsr->curFlags & BTCF_WriteFlag0x01)==0 ){
77813 return SQLITE_READONLY8;
77814 }
77815 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0((void) (0))
77816 && pCsr->pBt->inTransaction==TRANS_WRITE )((void) (0));
77817 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) )((void) (0));
77818 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) )((void) (0));
77819 assert( pCsr->pPage->intKey )((void) (0));
77820
77821 return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
77822}
77823
77824/*
77825** Mark this cursor as an incremental blob cursor.
77826*/
77827SQLITE_PRIVATEstatic void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
77828 pCur->curFlags |= BTCF_Incrblob0x10;
77829 pCur->pBtree->hasIncrblobCur = 1;
77830}
77831#endif
77832
77833/*
77834** Set both the "read version" (single byte at byte offset 18) and
77835** "write version" (single byte at byte offset 19) fields in the database
77836** header to iVersion.
77837*/
77838SQLITE_PRIVATEstatic int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
77839 BtShared *pBt = pBtree->pBt;
77840 int rc; /* Return code */
77841
77842 assert( iVersion==1 || iVersion==2 )((void) (0));
77843
77844 /* If setting the version fields to 1, do not automatically open the
77845 ** WAL connection, even if the version fields are currently set to 2.
77846 */
77847 pBt->btsFlags &= ~BTS_NO_WAL0x0020;
77848 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL0x0020;
77849
77850 rc = sqlite3BtreeBeginTrans(pBtree, 0, 0);
77851 if( rc==SQLITE_OK0 ){
77852 u8 *aData = pBt->pPage1->aData;
77853 if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
77854 rc = sqlite3BtreeBeginTrans(pBtree, 2, 0);
77855 if( rc==SQLITE_OK0 ){
77856 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
77857 if( rc==SQLITE_OK0 ){
77858 aData[18] = (u8)iVersion;
77859 aData[19] = (u8)iVersion;
77860 }
77861 }
77862 }
77863 }
77864
77865 pBt->btsFlags &= ~BTS_NO_WAL0x0020;
77866 return rc;
77867}
77868
77869/*
77870** Return true if the cursor has a hint specified. This routine is
77871** only used from within assert() statements
77872*/
77873SQLITE_PRIVATEstatic int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){
77874 return (pCsr->hints & mask)!=0;
77875}
77876
77877/*
77878** Return true if the given Btree is read-only.
77879*/
77880SQLITE_PRIVATEstatic int sqlite3BtreeIsReadonly(Btree *p){
77881 return (p->pBt->btsFlags & BTS_READ_ONLY0x0001)!=0;
77882}
77883
77884/*
77885** Return the size of the header added to each page by this module.
77886*/
77887SQLITE_PRIVATEstatic int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage))(((sizeof(MemPage))+7)&~7); }
77888
77889#if !defined(SQLITE_OMIT_SHARED_CACHE)
77890/*
77891** Return true if the Btree passed as the only argument is sharable.
77892*/
77893SQLITE_PRIVATEstatic int sqlite3BtreeSharable(Btree *p){
77894 return p->sharable;
77895}
77896
77897/*
77898** Return the number of connections to the BtShared object accessed by
77899** the Btree handle passed as the only argument. For private caches
77900** this is always 1. For shared caches it may be 1 or greater.
77901*/
77902SQLITE_PRIVATEstatic int sqlite3BtreeConnectionCount(Btree *p){
77903 testcase( p->sharable );
77904 return p->pBt->nRef;
77905}
77906#endif
77907
77908/************** End of btree.c ***********************************************/
77909/************** Begin file backup.c ******************************************/
77910/*
77911** 2009 January 28
77912**
77913** The author disclaims copyright to this source code. In place of
77914** a legal notice, here is a blessing:
77915**
77916** May you do good and not evil.
77917** May you find forgiveness for yourself and forgive others.
77918** May you share freely, never taking more than you give.
77919**
77920*************************************************************************
77921** This file contains the implementation of the sqlite3_backup_XXX()
77922** API functions and the related features.
77923*/
77924/* #include "sqliteInt.h" */
77925/* #include "btreeInt.h" */
77926
77927/*
77928** Structure allocated for each backup operation.
77929*/
77930struct sqlite3_backup {
77931 sqlite3* pDestDb; /* Destination database handle */
77932 Btree *pDest; /* Destination b-tree file */
77933 u32 iDestSchema; /* Original schema cookie in destination */
77934 int bDestLocked; /* True once a write-transaction is open on pDest */
77935
77936 Pgno iNext; /* Page number of the next source page to copy */
77937 sqlite3* pSrcDb; /* Source database handle */
77938 Btree *pSrc; /* Source b-tree file */
77939
77940 int rc; /* Backup process error code */
77941
77942 /* These two variables are set by every call to backup_step(). They are
77943 ** read by calls to backup_remaining() and backup_pagecount().
77944 */
77945 Pgno nRemaining; /* Number of pages left to copy */
77946 Pgno nPagecount; /* Total number of pages to copy */
77947
77948 int isAttached; /* True once backup has been registered with pager */
77949 sqlite3_backup *pNext; /* Next backup associated with source pager */
77950};
77951
77952/*
77953** THREAD SAFETY NOTES:
77954**
77955** Once it has been created using backup_init(), a single sqlite3_backup
77956** structure may be accessed via two groups of thread-safe entry points:
77957**
77958** * Via the sqlite3_backup_XXX() API function backup_step() and
77959** backup_finish(). Both these functions obtain the source database
77960** handle mutex and the mutex associated with the source BtShared
77961** structure, in that order.
77962**
77963** * Via the BackupUpdate() and BackupRestart() functions, which are
77964** invoked by the pager layer to report various state changes in
77965** the page cache associated with the source database. The mutex
77966** associated with the source database BtShared structure will always
77967** be held when either of these functions are invoked.
77968**
77969** The other sqlite3_backup_XXX() API functions, backup_remaining() and
77970** backup_pagecount() are not thread-safe functions. If they are called
77971** while some other thread is calling backup_step() or backup_finish(),
77972** the values returned may be invalid. There is no way for a call to
77973** BackupUpdate() or BackupRestart() to interfere with backup_remaining()
77974** or backup_pagecount().
77975**
77976** Depending on the SQLite configuration, the database handles and/or
77977** the Btree objects may have their own mutexes that require locking.
77978** Non-sharable Btrees (in-memory databases for example), do not have
77979** associated mutexes.
77980*/
77981
77982/*
77983** Return a pointer corresponding to database zDb (i.e. "main", "temp")
77984** in connection handle pDb. If such a database cannot be found, return
77985** a NULL pointer and write an error message to pErrorDb.
77986**
77987** If the "temp" database is requested, it may need to be opened by this
77988** function. If an error occurs while doing so, return 0 and write an
77989** error message to pErrorDb.
77990*/
77991static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
77992 int i = sqlite3FindDbName(pDb, zDb);
77993
77994 if( i==1 ){
77995 Parse sParse;
77996 int rc = 0;
77997 sqlite3ParseObjectInit(&sParse,pDb);
77998 if( sqlite3OpenTempDatabase(&sParse) ){
77999 sqlite3ErrorWithMsg(pErrorDb, sParse.rc, "%s", sParse.zErrMsg);
78000 rc = SQLITE_ERROR1;
78001 }
78002 sqlite3DbFree(pErrorDb, sParse.zErrMsg);
78003 sqlite3ParseObjectReset(&sParse);
78004 if( rc ){
78005 return 0;
78006 }
78007 }
78008
78009 if( i<0 ){
78010 sqlite3ErrorWithMsg(pErrorDb, SQLITE_ERROR1, "unknown database %s", zDb);
78011 return 0;
78012 }
78013
78014 return pDb->aDb[i].pBt;
78015}
78016
78017/*
78018** Attempt to set the page size of the destination to match the page size
78019** of the source.
78020*/
78021static int setDestPgsz(sqlite3_backup *p){
78022 int rc;
78023 rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),0,0);
78024 return rc;
78025}
78026
78027/*
78028** Check that there is no open read-transaction on the b-tree passed as the
78029** second argument. If there is not, return SQLITE_OK. Otherwise, if there
78030** is an open read-transaction, return SQLITE_ERROR and leave an error
78031** message in database handle db.
78032*/
78033static int checkReadTransaction(sqlite3 *db, Btree *p){
78034 if( sqlite3BtreeTxnState(p)!=SQLITE_TXN_NONE0 ){
78035 sqlite3ErrorWithMsg(db, SQLITE_ERROR1, "destination database is in use");
78036 return SQLITE_ERROR1;
78037 }
78038 return SQLITE_OK0;
78039}
78040
78041/*
78042** Create an sqlite3_backup process to copy the contents of zSrcDb from
78043** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
78044** a pointer to the new sqlite3_backup object.
78045**
78046** If an error occurs, NULL is returned and an error code and error message
78047** stored in database handle pDestDb.
78048*/
78049SQLITE_API sqlite3_backup *sqlite3_backup_init(
78050 sqlite3* pDestDb, /* Database to write to */
78051 const char *zDestDb, /* Name of database within pDestDb */
78052 sqlite3* pSrcDb, /* Database connection to read from */
78053 const char *zSrcDb /* Name of database within pSrcDb */
78054){
78055 sqlite3_backup *p; /* Value to return */
78056
78057#ifdef SQLITE_ENABLE_API_ARMOR1
78058 if( !sqlite3SafetyCheckOk(pSrcDb)||!sqlite3SafetyCheckOk(pDestDb) ){
78059 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(78059);
78060 return 0;
78061 }
78062#endif
78063
78064 /* Lock the source database handle. The destination database
78065 ** handle is not locked in this routine, but it is locked in
78066 ** sqlite3_backup_step(). The user is required to ensure that no
78067 ** other thread accesses the destination handle for the duration
78068 ** of the backup operation. Any attempt to use the destination
78069 ** database connection while a backup is in progress may cause
78070 ** a malfunction or a deadlock.
78071 */
78072 sqlite3_mutex_enter(pSrcDb->mutex);
78073 sqlite3_mutex_enter(pDestDb->mutex);
78074
78075 if( pSrcDb==pDestDb ){
78076 sqlite3ErrorWithMsg(
78077 pDestDb, SQLITE_ERROR1, "source and destination must be distinct"
78078 );
78079 p = 0;
78080 }else {
78081 /* Allocate space for a new sqlite3_backup object...
78082 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
78083 ** call to sqlite3_backup_init() and is destroyed by a call to
78084 ** sqlite3_backup_finish(). */
78085 p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
78086 if( !p ){
78087 sqlite3Error(pDestDb, SQLITE_NOMEM_BKPT7);
78088 }
78089 }
78090
78091 /* If the allocation succeeded, populate the new object. */
78092 if( p ){
78093 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
78094 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
78095 p->pDestDb = pDestDb;
78096 p->pSrcDb = pSrcDb;
78097 p->iNext = 1;
78098 p->isAttached = 0;
78099
78100 if( 0==p->pSrc || 0==p->pDest
78101 || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK0
78102 ){
78103 /* One (or both) of the named databases did not exist or an OOM
78104 ** error was hit. Or there is a transaction open on the destination
78105 ** database. The error has already been written into the pDestDb
78106 ** handle. All that is left to do here is free the sqlite3_backup
78107 ** structure. */
78108 sqlite3_free(p);
78109 p = 0;
78110 }
78111 }
78112 if( p ){
78113 p->pSrc->nBackup++;
78114 }
78115
78116 sqlite3_mutex_leave(pDestDb->mutex);
78117 sqlite3_mutex_leave(pSrcDb->mutex);
78118 return p;
78119}
78120
78121/*
78122** Argument rc is an SQLite error code. Return true if this error is
78123** considered fatal if encountered during a backup operation. All errors
78124** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
78125*/
78126static int isFatalError(int rc){
78127 return (rc!=SQLITE_OK0 && rc!=SQLITE_BUSY5 && ALWAYS(rc!=SQLITE_LOCKED)(rc!=6));
78128}
78129
78130/*
78131** Parameter zSrcData points to a buffer containing the data for
78132** page iSrcPg from the source database. Copy this data into the
78133** destination database.
78134*/
78135static int backupOnePage(
78136 sqlite3_backup *p, /* Backup handle */
78137 Pgno iSrcPg, /* Source database page to backup */
78138 const u8 *zSrcData, /* Source database page data */
78139 int bUpdate /* True for an update, false otherwise */
78140){
78141 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
78142 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
78143 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
78144 const int nCopy = MIN(nSrcPgsz, nDestPgsz)((nSrcPgsz)<(nDestPgsz)?(nSrcPgsz):(nDestPgsz));
78145 const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
78146 int rc = SQLITE_OK0;
78147 i64 iOff;
78148
78149 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 )((void) (0));
78150 assert( p->bDestLocked )((void) (0));
78151 assert( !isFatalError(p->rc) )((void) (0));
78152 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) )((void) (0));
78153 assert( zSrcData )((void) (0));
78154
78155 /* Catch the case where the destination is an in-memory database and the
78156 ** page sizes of the source and destination differ.
78157 */
78158 if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
78159 rc = SQLITE_READONLY8;
78160 }
78161
78162 /* This loop runs once for each destination page spanned by the source
78163 ** page. For each iteration, variable iOff is set to the byte offset
78164 ** of the destination page.
78165 */
78166 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK0 && iOff<iEnd; iOff+=nDestPgsz){
78167 DbPage *pDestPg = 0;
78168 Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
78169 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt)((Pgno)((sqlite3PendingByte/((p->pDest->pBt)->pageSize
))+1))
) continue;
78170 if( SQLITE_OK0==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg, 0))
78171 && SQLITE_OK0==(rc = sqlite3PagerWrite(pDestPg))
78172 ){
78173 const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
78174 u8 *zDestData = sqlite3PagerGetData(pDestPg);
78175 u8 *zOut = &zDestData[iOff%nDestPgsz];
78176
78177 /* Copy the data from the source page into the destination page.
78178 ** Then clear the Btree layer MemPage.isInit flag. Both this module
78179 ** and the pager code use this trick (clearing the first byte
78180 ** of the page 'extra' space to invalidate the Btree layers
78181 ** cached parse of the page). MemPage.isInit is marked
78182 ** "MUST BE FIRST" for this purpose.
78183 */
78184 memcpy(zOut, zIn, nCopy);
78185 ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
78186 if( iOff==0 && bUpdate==0 ){
78187 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
78188 }
78189 }
78190 sqlite3PagerUnref(pDestPg);
78191 }
78192
78193 return rc;
78194}
78195
78196/*
78197** If pFile is currently larger than iSize bytes, then truncate it to
78198** exactly iSize bytes. If pFile is not larger than iSize bytes, then
78199** this function is a no-op.
78200**
78201** Return SQLITE_OK if everything is successful, or an SQLite error
78202** code if an error occurs.
78203*/
78204static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
78205 i64 iCurrent;
78206 int rc = sqlite3OsFileSize(pFile, &iCurrent);
78207 if( rc==SQLITE_OK0 && iCurrent>iSize ){
78208 rc = sqlite3OsTruncate(pFile, iSize);
78209 }
78210 return rc;
78211}
78212
78213/*
78214** Register this backup object with the associated source pager for
78215** callbacks when pages are changed or the cache invalidated.
78216*/
78217static void attachBackupObject(sqlite3_backup *p){
78218 sqlite3_backup **pp;
78219 assert( sqlite3BtreeHoldsMutex(p->pSrc) )((void) (0));
78220 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
78221 p->pNext = *pp;
78222 *pp = p;
78223 p->isAttached = 1;
78224}
78225
78226/*
78227** Copy nPage pages from the source b-tree to the destination.
78228*/
78229SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
78230 int rc;
78231 int destMode; /* Destination journal mode */
78232 int pgszSrc = 0; /* Source page size */
78233 int pgszDest = 0; /* Destination page size */
78234
78235#ifdef SQLITE_ENABLE_API_ARMOR1
78236 if( p==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(78236);
78237#endif
78238 sqlite3_mutex_enter(p->pSrcDb->mutex);
78239 sqlite3BtreeEnter(p->pSrc);
78240 if( p->pDestDb ){
78241 sqlite3_mutex_enter(p->pDestDb->mutex);
78242 }
78243
78244 rc = p->rc;
78245 if( !isFatalError(rc) ){
78246 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
78247 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
78248 int ii; /* Iterator variable */
78249 int nSrcPage = -1; /* Size of source db in pages */
78250 int bCloseTrans = 0; /* True if src db requires unlocking */
78251
78252 /* If the source pager is currently in a write-transaction, return
78253 ** SQLITE_BUSY immediately.
78254 */
78255 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE2 ){
78256 rc = SQLITE_BUSY5;
78257 }else{
78258 rc = SQLITE_OK0;
78259 }
78260
78261 /* If there is no open read-transaction on the source database, open
78262 ** one now. If a transaction is opened here, then it will be closed
78263 ** before this function exits.
78264 */
78265 if( rc==SQLITE_OK0 && SQLITE_TXN_NONE0==sqlite3BtreeTxnState(p->pSrc) ){
78266 rc = sqlite3BtreeBeginTrans(p->pSrc, 0, 0);
78267 bCloseTrans = 1;
78268 }
78269
78270 /* If the destination database has not yet been locked (i.e. if this
78271 ** is the first call to backup_step() for the current backup operation),
78272 ** try to set its page size to the same as the source database. This
78273 ** is especially important on ZipVFS systems, as in that case it is
78274 ** not possible to create a database file that uses one page size by
78275 ** writing to it with another. */
78276 if( p->bDestLocked==0 && rc==SQLITE_OK0 && setDestPgsz(p)==SQLITE_NOMEM7 ){
78277 rc = SQLITE_NOMEM7;
78278 }
78279
78280 /* Lock the destination database, if it is not locked already. */
78281 if( SQLITE_OK0==rc && p->bDestLocked==0
78282 && SQLITE_OK0==(rc = sqlite3BtreeBeginTrans(p->pDest, 2,
78283 (int*)&p->iDestSchema))
78284 ){
78285 p->bDestLocked = 1;
78286 }
78287
78288 /* Do not allow backup if the destination database is in WAL mode
78289 ** and the page sizes are different between source and destination */
78290 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
78291 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
78292 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
78293 if( SQLITE_OK0==rc && destMode==PAGER_JOURNALMODE_WAL5 && pgszSrc!=pgszDest ){
78294 rc = SQLITE_READONLY8;
78295 }
78296
78297 /* Now that there is a read-lock on the source database, query the
78298 ** source pager for the number of pages in the database.
78299 */
78300 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
78301 assert( nSrcPage>=0 )((void) (0));
78302 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
78303 const Pgno iSrcPg = p->iNext; /* Source page number */
78304 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt)((Pgno)((sqlite3PendingByte/((p->pSrc->pBt)->pageSize
))+1))
){
78305 DbPage *pSrcPg; /* Source page object */
78306 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg,PAGER_GET_READONLY0x02);
78307 if( rc==SQLITE_OK0 ){
78308 rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
78309 sqlite3PagerUnref(pSrcPg);
78310 }
78311 }
78312 p->iNext++;
78313 }
78314 if( rc==SQLITE_OK0 ){
78315 p->nPagecount = nSrcPage;
78316 p->nRemaining = nSrcPage+1-p->iNext;
78317 if( p->iNext>(Pgno)nSrcPage ){
78318 rc = SQLITE_DONE101;
78319 }else if( !p->isAttached ){
78320 attachBackupObject(p);
78321 }
78322 }
78323
78324 /* Update the schema version field in the destination database. This
78325 ** is to make sure that the schema-version really does change in
78326 ** the case where the source and destination databases have the
78327 ** same schema version.
78328 */
78329 if( rc==SQLITE_DONE101 ){
78330 if( nSrcPage==0 ){
78331 rc = sqlite3BtreeNewDb(p->pDest);
78332 nSrcPage = 1;
78333 }
78334 if( rc==SQLITE_OK0 || rc==SQLITE_DONE101 ){
78335 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
78336 }
78337 if( rc==SQLITE_OK0 ){
78338 if( p->pDestDb ){
78339 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
78340 }
78341 if( destMode==PAGER_JOURNALMODE_WAL5 ){
78342 rc = sqlite3BtreeSetVersion(p->pDest, 2);
78343 }
78344 }
78345 if( rc==SQLITE_OK0 ){
78346 int nDestTruncate;
78347 /* Set nDestTruncate to the final number of pages in the destination
78348 ** database. The complication here is that the destination page
78349 ** size may be different to the source page size.
78350 **
78351 ** If the source page size is smaller than the destination page size,
78352 ** round up. In this case the call to sqlite3OsTruncate() below will
78353 ** fix the size of the file. However it is important to call
78354 ** sqlite3PagerTruncateImage() here so that any pages in the
78355 ** destination file that lie beyond the nDestTruncate page mark are
78356 ** journalled by PagerCommitPhaseOne() before they are destroyed
78357 ** by the file truncation.
78358 */
78359 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) )((void) (0));
78360 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) )((void) (0));
78361 if( pgszSrc<pgszDest ){
78362 int ratio = pgszDest/pgszSrc;
78363 nDestTruncate = (nSrcPage+ratio-1)/ratio;
78364 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt)((Pgno)((sqlite3PendingByte/((p->pDest->pBt)->pageSize
))+1))
){
78365 nDestTruncate--;
78366 }
78367 }else{
78368 nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
78369 }
78370 assert( nDestTruncate>0 )((void) (0));
78371
78372 if( pgszSrc<pgszDest ){
78373 /* If the source page-size is smaller than the destination page-size,
78374 ** two extra things may need to happen:
78375 **
78376 ** * The destination may need to be truncated, and
78377 **
78378 ** * Data stored on the pages immediately following the
78379 ** pending-byte page in the source database may need to be
78380 ** copied into the destination database.
78381 */
78382 const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
78383 sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
78384 Pgno iPg;
78385 int nDstPage;
78386 i64 iOff;
78387 i64 iEnd;
78388
78389 assert( pFile )((void) (0));
78390 assert( nDestTruncate==0((void) (0))
78391 || (i64)nDestTruncate*(i64)pgszDest >= iSize || (((void) (0))
78392 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)((void) (0))
78393 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest((void) (0))
78394 ))((void) (0));
78395
78396 /* This block ensures that all data required to recreate the original
78397 ** database has been stored in the journal for pDestPager and the
78398 ** journal synced to disk. So at this point we may safely modify
78399 ** the database file in any way, knowing that if a power failure
78400 ** occurs, the original database will be reconstructed from the
78401 ** journal file. */
78402 sqlite3PagerPagecount(pDestPager, &nDstPage);
78403 for(iPg=nDestTruncate; rc==SQLITE_OK0 && iPg<=(Pgno)nDstPage; iPg++){
78404 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt)((Pgno)((sqlite3PendingByte/((p->pDest->pBt)->pageSize
))+1))
){
78405 DbPage *pPg;
78406 rc = sqlite3PagerGet(pDestPager, iPg, &pPg, 0);
78407 if( rc==SQLITE_OK0 ){
78408 rc = sqlite3PagerWrite(pPg);
78409 sqlite3PagerUnref(pPg);
78410 }
78411 }
78412 }
78413 if( rc==SQLITE_OK0 ){
78414 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
78415 }
78416
78417 /* Write the extra pages and truncate the database file as required */
78418 iEnd = MIN(PENDING_BYTE + pgszDest, iSize)((sqlite3PendingByte + pgszDest)<(iSize)?(sqlite3PendingByte
+ pgszDest):(iSize))
;
78419 for(
78420 iOff=PENDING_BYTEsqlite3PendingByte+pgszSrc;
78421 rc==SQLITE_OK0 && iOff<iEnd;
78422 iOff+=pgszSrc
78423 ){
78424 PgHdr *pSrcPg = 0;
78425 const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
78426 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg, 0);
78427 if( rc==SQLITE_OK0 ){
78428 u8 *zData = sqlite3PagerGetData(pSrcPg);
78429 rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
78430 }
78431 sqlite3PagerUnref(pSrcPg);
78432 }
78433 if( rc==SQLITE_OK0 ){
78434 rc = backupTruncateFile(pFile, iSize);
78435 }
78436
78437 /* Sync the database file to disk. */
78438 if( rc==SQLITE_OK0 ){
78439 rc = sqlite3PagerSync(pDestPager, 0);
78440 }
78441 }else{
78442 sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
78443 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
78444 }
78445
78446 /* Finish committing the transaction to the destination database. */
78447 if( SQLITE_OK0==rc
78448 && SQLITE_OK0==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
78449 ){
78450 rc = SQLITE_DONE101;
78451 }
78452 }
78453 }
78454
78455 /* If bCloseTrans is true, then this function opened a read transaction
78456 ** on the source database. Close the read transaction here. There is
78457 ** no need to check the return values of the btree methods here, as
78458 ** "committing" a read-only transaction cannot fail.
78459 */
78460 if( bCloseTrans ){
78461 TESTONLY( int rc2 );
78462 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
78463 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
78464 assert( rc2==SQLITE_OK )((void) (0));
78465 }
78466
78467 if( rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
78468 rc = SQLITE_NOMEM_BKPT7;
78469 }
78470 p->rc = rc;
78471 }
78472 if( p->pDestDb ){
78473 sqlite3_mutex_leave(p->pDestDb->mutex);
78474 }
78475 sqlite3BtreeLeave(p->pSrc);
78476 sqlite3_mutex_leave(p->pSrcDb->mutex);
78477 return rc;
78478}
78479
78480/*
78481** Release all resources associated with an sqlite3_backup* handle.
78482*/
78483SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
78484 sqlite3_backup **pp; /* Ptr to head of pagers backup list */
78485 sqlite3 *pSrcDb; /* Source database connection */
78486 int rc; /* Value to return */
78487
78488 /* Enter the mutexes */
78489 if( p==0 ) return SQLITE_OK0;
78490 pSrcDb = p->pSrcDb;
78491 sqlite3_mutex_enter(pSrcDb->mutex);
78492 sqlite3BtreeEnter(p->pSrc);
78493 if( p->pDestDb ){
78494 sqlite3_mutex_enter(p->pDestDb->mutex);
78495 }
78496
78497 /* Detach this backup from the source pager. */
78498 if( p->pDestDb ){
78499 p->pSrc->nBackup--;
78500 }
78501 if( p->isAttached ){
78502 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
78503 assert( pp!=0 )((void) (0));
78504 while( *pp!=p ){
78505 pp = &(*pp)->pNext;
78506 assert( pp!=0 )((void) (0));
78507 }
78508 *pp = p->pNext;
78509 }
78510
78511 /* If a transaction is still open on the Btree, roll it back. */
78512 sqlite3BtreeRollback(p->pDest, SQLITE_OK0, 0);
78513
78514 /* Set the error code of the destination database handle. */
78515 rc = (p->rc==SQLITE_DONE101) ? SQLITE_OK0 : p->rc;
78516 if( p->pDestDb ){
78517 sqlite3Error(p->pDestDb, rc);
78518
78519 /* Exit the mutexes and free the backup context structure. */
78520 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
78521 }
78522 sqlite3BtreeLeave(p->pSrc);
78523 if( p->pDestDb ){
78524 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
78525 ** call to sqlite3_backup_init() and is destroyed by a call to
78526 ** sqlite3_backup_finish(). */
78527 sqlite3_free(p);
78528 }
78529 sqlite3LeaveMutexAndCloseZombie(pSrcDb);
78530 return rc;
78531}
78532
78533/*
78534** Return the number of pages still to be backed up as of the most recent
78535** call to sqlite3_backup_step().
78536*/
78537SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
78538#ifdef SQLITE_ENABLE_API_ARMOR1
78539 if( p==0 ){
78540 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(78540);
78541 return 0;
78542 }
78543#endif
78544 return p->nRemaining;
78545}
78546
78547/*
78548** Return the total number of pages in the source database as of the most
78549** recent call to sqlite3_backup_step().
78550*/
78551SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
78552#ifdef SQLITE_ENABLE_API_ARMOR1
78553 if( p==0 ){
78554 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(78554);
78555 return 0;
78556 }
78557#endif
78558 return p->nPagecount;
78559}
78560
78561/*
78562** This function is called after the contents of page iPage of the
78563** source database have been modified. If page iPage has already been
78564** copied into the destination database, then the data written to the
78565** destination is now invalidated. The destination copy of iPage needs
78566** to be updated with the new data before the backup operation is
78567** complete.
78568**
78569** It is assumed that the mutex associated with the BtShared object
78570** corresponding to the source database is held when this function is
78571** called.
78572*/
78573static SQLITE_NOINLINE__attribute__((noinline)) void backupUpdate(
78574 sqlite3_backup *p,
78575 Pgno iPage,
78576 const u8 *aData
78577){
78578 assert( p!=0 )((void) (0));
78579 do{
78580 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) )((void) (0));
78581 if( !isFatalError(p->rc) && iPage<p->iNext ){
78582 /* The backup process p has already copied page iPage. But now it
78583 ** has been modified by a transaction on the source pager. Copy
78584 ** the new data into the backup.
78585 */
78586 int rc;
78587 assert( p->pDestDb )((void) (0));
78588 sqlite3_mutex_enter(p->pDestDb->mutex);
78589 rc = backupOnePage(p, iPage, aData, 1);
78590 sqlite3_mutex_leave(p->pDestDb->mutex);
78591 assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED )((void) (0));
78592 if( rc!=SQLITE_OK0 ){
78593 p->rc = rc;
78594 }
78595 }
78596 }while( (p = p->pNext)!=0 );
78597}
78598SQLITE_PRIVATEstatic void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
78599 if( pBackup ) backupUpdate(pBackup, iPage, aData);
78600}
78601
78602/*
78603** Restart the backup process. This is called when the pager layer
78604** detects that the database has been modified by an external database
78605** connection. In this case there is no way of knowing which of the
78606** pages that have been copied into the destination database are still
78607** valid and which are not, so the entire process needs to be restarted.
78608**
78609** It is assumed that the mutex associated with the BtShared object
78610** corresponding to the source database is held when this function is
78611** called.
78612*/
78613SQLITE_PRIVATEstatic void sqlite3BackupRestart(sqlite3_backup *pBackup){
78614 sqlite3_backup *p; /* Iterator variable */
78615 for(p=pBackup; p; p=p->pNext){
78616 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) )((void) (0));
78617 p->iNext = 1;
78618 }
78619}
78620
78621#ifndef SQLITE_OMIT_VACUUM
78622/*
78623** Copy the complete content of pBtFrom into pBtTo. A transaction
78624** must be active for both files.
78625**
78626** The size of file pTo may be reduced by this operation. If anything
78627** goes wrong, the transaction on pTo is rolled back. If successful, the
78628** transaction is committed before returning.
78629*/
78630SQLITE_PRIVATEstatic int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
78631 int rc;
78632 sqlite3_file *pFd; /* File descriptor for database pTo */
78633 sqlite3_backup b;
78634 sqlite3BtreeEnter(pTo);
78635 sqlite3BtreeEnter(pFrom);
78636
78637 assert( sqlite3BtreeTxnState(pTo)==SQLITE_TXN_WRITE )((void) (0));
78638 pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
78639 if( pFd->pMethods ){
78640 i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
78641 rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE11, &nByte);
78642 if( rc==SQLITE_NOTFOUND12 ) rc = SQLITE_OK0;
78643 if( rc ) goto copy_finished;
78644 }
78645
78646 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
78647 ** to 0. This is used by the implementations of sqlite3_backup_step()
78648 ** and sqlite3_backup_finish() to detect that they are being called
78649 ** from this function, not directly by the user.
78650 */
78651 memset(&b, 0, sizeof(b));
78652 b.pSrcDb = pFrom->db;
78653 b.pSrc = pFrom;
78654 b.pDest = pTo;
78655 b.iNext = 1;
78656
78657 /* 0x7FFFFFFF is the hard limit for the number of pages in a database
78658 ** file. By passing this as the number of pages to copy to
78659 ** sqlite3_backup_step(), we can guarantee that the copy finishes
78660 ** within a single call (unless an error occurs). The assert() statement
78661 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
78662 ** or an error code. */
78663 sqlite3_backup_step(&b, 0x7FFFFFFF);
78664 assert( b.rc!=SQLITE_OK )((void) (0));
78665
78666 rc = sqlite3_backup_finish(&b);
78667 if( rc==SQLITE_OK0 ){
78668 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED0x0002;
78669 }else{
78670 sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
78671 }
78672
78673 assert( sqlite3BtreeTxnState(pTo)!=SQLITE_TXN_WRITE )((void) (0));
78674copy_finished:
78675 sqlite3BtreeLeave(pFrom);
78676 sqlite3BtreeLeave(pTo);
78677 return rc;
78678}
78679#endif /* SQLITE_OMIT_VACUUM */
78680
78681/************** End of backup.c **********************************************/
78682/************** Begin file vdbemem.c *****************************************/
78683/*
78684** 2004 May 26
78685**
78686** The author disclaims copyright to this source code. In place of
78687** a legal notice, here is a blessing:
78688**
78689** May you do good and not evil.
78690** May you find forgiveness for yourself and forgive others.
78691** May you share freely, never taking more than you give.
78692**
78693*************************************************************************
78694**
78695** This file contains code use to manipulate "Mem" structure. A "Mem"
78696** stores a single value in the VDBE. Mem is an opaque structure visible
78697** only within the VDBE. Interface routines refer to a Mem using the
78698** name sqlite_value
78699*/
78700/* #include "sqliteInt.h" */
78701/* #include "vdbeInt.h" */
78702
78703/* True if X is a power of two. 0 is considered a power of two here.
78704** In other words, return true if X has at most one bit set.
78705*/
78706#define ISPOWEROF2(X)(((X)&((X)-1))==0) (((X)&((X)-1))==0)
78707
78708#ifdef SQLITE_DEBUG
78709/*
78710** Check invariants on a Mem object.
78711**
78712** This routine is intended for use inside of assert() statements, like
78713** this: assert( sqlite3VdbeCheckMemInvariants(pMem) );
78714*/
78715SQLITE_PRIVATEstatic int sqlite3VdbeCheckMemInvariants(Mem *p){
78716 /* If MEM_Dyn is set then Mem.xDel!=0.
78717 ** Mem.xDel might not be initialized if MEM_Dyn is clear.
78718 */
78719 assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 )((void) (0));
78720
78721 /* MEM_Dyn may only be set if Mem.szMalloc==0. In this way we
78722 ** ensure that if Mem.szMalloc>0 then it is safe to do
78723 ** Mem.z = Mem.zMalloc without having to check Mem.flags&MEM_Dyn.
78724 ** That saves a few cycles in inner loops. */
78725 assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 )((void) (0));
78726
78727 /* Cannot have more than one of MEM_Int, MEM_Real, or MEM_IntReal */
78728 assert( ISPOWEROF2(p->flags & (MEM_Int|MEM_Real|MEM_IntReal)) )((void) (0));
78729
78730 if( p->flags & MEM_Null0x0001 ){
78731 /* Cannot be both MEM_Null and some other type */
78732 assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob|MEM_Agg))==0 )((void) (0));
78733
78734 /* If MEM_Null is set, then either the value is a pure NULL (the usual
78735 ** case) or it is a pointer set using sqlite3_bind_pointer() or
78736 ** sqlite3_result_pointer(). If a pointer, then MEM_Term must also be
78737 ** set.
78738 */
78739 if( (p->flags & (MEM_Term0x0200|MEM_Subtype0x0800))==(MEM_Term0x0200|MEM_Subtype0x0800) ){
78740 /* This is a pointer type. There may be a flag to indicate what to
78741 ** do with the pointer. */
78742 assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +((void) (0))
78743 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +((void) (0))
78744 ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 )((void) (0));
78745
78746 /* No other bits set */
78747 assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype|MEM_FromBind((void) (0))
78748 |MEM_Dyn|MEM_Ephem|MEM_Static))==0 )((void) (0));
78749 }else{
78750 /* A pure NULL might have other flags, such as MEM_Static, MEM_Dyn,
78751 ** MEM_Ephem, MEM_Cleared, or MEM_Subtype */
78752 }
78753 }else{
78754 /* The MEM_Cleared bit is only allowed on NULLs */
78755 assert( (p->flags & MEM_Cleared)==0 )((void) (0));
78756 }
78757
78758 /* The szMalloc field holds the correct memory allocation size */
78759 assert( p->szMalloc==0((void) (0))
78760 || (p->flags==MEM_Undefined((void) (0))
78761 && p->szMalloc<=sqlite3DbMallocSize(p->db,p->zMalloc))((void) (0))
78762 || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc))((void) (0));
78763
78764 /* If p holds a string or blob, the Mem.z must point to exactly
78765 ** one of the following:
78766 **
78767 ** (1) Memory in Mem.zMalloc and managed by the Mem object
78768 ** (2) Memory to be freed using Mem.xDel
78769 ** (3) An ephemeral string or blob
78770 ** (4) A static string or blob
78771 */
78772 if( (p->flags & (MEM_Str0x0002|MEM_Blob0x0010)) && p->n>0 ){
78773 assert(((void) (0))
78774 ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +((void) (0))
78775 ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +((void) (0))
78776 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +((void) (0))
78777 ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1((void) (0))
78778 )((void) (0));
78779 }
78780 return 1;
78781}
78782#endif
78783
78784/*
78785** Render a Mem object which is one of MEM_Int, MEM_Real, or MEM_IntReal
78786** into a buffer.
78787*/
78788static void vdbeMemRenderNum(int sz, char *zBuf, Mem *p){
78789 StrAccum acc;
78790 assert( p->flags & (MEM_Int|MEM_Real|MEM_IntReal) )((void) (0));
78791 assert( sz>22 )((void) (0));
78792 if( p->flags & MEM_Int0x0004 ){
78793#if GCC_VERSION(4*1000000+2*1000+1)>=7000000
78794 /* Work-around for GCC bug
78795 ** https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96270 */
78796 i64 x;
78797 assert( (p->flags&MEM_Int)*2==sizeof(x) )((void) (0));
78798 memcpy(&x, (char*)&p->u, (p->flags&MEM_Int0x0004)*2);
78799 sqlite3Int64ToText(x, zBuf);
78800#else
78801 sqlite3Int64ToText(p->u.i, zBuf);
78802#endif
78803 }else{
78804 sqlite3StrAccumInit(&acc, 0, zBuf, sz, 0);
78805 sqlite3_str_appendf(&acc, "%!.15g",
78806 (p->flags & MEM_IntReal0x0020)!=0 ? (double)p->u.i : p->u.r);
78807 assert( acc.zText==zBuf && acc.mxAlloc<=0 )((void) (0));
78808 zBuf[acc.nChar] = 0; /* Fast version of sqlite3StrAccumFinish(&acc) */
78809 }
78810}
78811
78812#ifdef SQLITE_DEBUG
78813/*
78814** Validity checks on pMem. pMem holds a string.
78815**
78816** (1) Check that string value of pMem agrees with its integer or real value.
78817** (2) Check that the string is correctly zero terminated
78818**
78819** A single int or real value always converts to the same strings. But
78820** many different strings can be converted into the same int or real.
78821** If a table contains a numeric value and an index is based on the
78822** corresponding string value, then it is important that the string be
78823** derived from the numeric value, not the other way around, to ensure
78824** that the index and table are consistent. See ticket
78825** https://www.sqlite.org/src/info/343634942dd54ab (2018-01-31) for
78826** an example.
78827**
78828** This routine looks at pMem to verify that if it has both a numeric
78829** representation and a string representation then the string rep has
78830** been derived from the numeric and not the other way around. It returns
78831** true if everything is ok and false if there is a problem.
78832**
78833** This routine is for use inside of assert() statements only.
78834*/
78835SQLITE_PRIVATEstatic int sqlite3VdbeMemValidStrRep(Mem *p){
78836 char zBuf[100];
78837 char *z;
78838 int i, j, incr;
78839 if( (p->flags & MEM_Str0x0002)==0 ) return 1;
78840 if( p->flags & MEM_Term0x0200 ){
78841 /* Insure that the string is properly zero-terminated. Pay particular
78842 ** attention to the case where p->n is odd */
78843 if( p->szMalloc>0 && p->z==p->zMalloc ){
78844 assert( p->enc==SQLITE_UTF8 || p->szMalloc >= ((p->n+1)&~1)+2 )((void) (0));
78845 assert( p->enc!=SQLITE_UTF8 || p->szMalloc >= p->n+1 )((void) (0));
78846 }
78847 assert( p->z[p->n]==0 )((void) (0));
78848 assert( p->enc==SQLITE_UTF8 || p->z[(p->n+1)&~1]==0 )((void) (0));
78849 assert( p->enc==SQLITE_UTF8 || p->z[((p->n+1)&~1)+1]==0 )((void) (0));
78850 }
78851 if( (p->flags & (MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020))==0 ) return 1;
78852 vdbeMemRenderNum(sizeof(zBuf), zBuf, p);
78853 z = p->z;
78854 i = j = 0;
78855 incr = 1;
78856 if( p->enc!=SQLITE_UTF81 ){
78857 incr = 2;
78858 if( p->enc==SQLITE_UTF16BE3 ) z++;
78859 }
78860 while( zBuf[j] ){
78861 if( zBuf[j++]!=z[i] ) return 0;
78862 i += incr;
78863 }
78864 return 1;
78865}
78866#endif /* SQLITE_DEBUG */
78867
78868/*
78869** If pMem is an object with a valid string representation, this routine
78870** ensures the internal encoding for the string representation is
78871** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
78872**
78873** If pMem is not a string object, or the encoding of the string
78874** representation is already stored using the requested encoding, then this
78875** routine is a no-op.
78876**
78877** SQLITE_OK is returned if the conversion is successful (or not required).
78878** SQLITE_NOMEM may be returned if a malloc() fails during conversion
78879** between formats.
78880*/
78881SQLITE_PRIVATEstatic int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
78882#ifndef SQLITE_OMIT_UTF16
78883 int rc;
78884#endif
78885 assert( pMem!=0 )((void) (0));
78886 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
78887 assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE((void) (0))
78888 || desiredEnc==SQLITE_UTF16BE )((void) (0));
78889 if( !(pMem->flags&MEM_Str0x0002) ){
78890 pMem->enc = desiredEnc;
78891 return SQLITE_OK0;
78892 }
78893 if( pMem->enc==desiredEnc ){
78894 return SQLITE_OK0;
78895 }
78896 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
78897#ifdef SQLITE_OMIT_UTF16
78898 return SQLITE_ERROR1;
78899#else
78900
78901 /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
78902 ** then the encoding of the value may not have changed.
78903 */
78904 rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
78905 assert(rc==SQLITE_OK || rc==SQLITE_NOMEM)((void) (0));
78906 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc)((void) (0));
78907 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc)((void) (0));
78908 return rc;
78909#endif
78910}
78911
78912/*
78913** Make sure pMem->z points to a writable allocation of at least n bytes.
78914**
78915** If the bPreserve argument is true, then copy of the content of
78916** pMem->z into the new allocation. pMem must be either a string or
78917** blob if bPreserve is true. If bPreserve is false, any prior content
78918** in pMem->z is discarded.
78919*/
78920SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
78921 assert( sqlite3VdbeCheckMemInvariants(pMem) )((void) (0));
78922 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
78923 testcase( pMem->db==0 );
78924
78925 /* If the bPreserve flag is set to true, then the memory cell must already
78926 ** contain a valid string or blob value. */
78927 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) )((void) (0));
78928 testcase( bPreserve && pMem->z==0 );
78929
78930 assert( pMem->szMalloc==0((void) (0))
78931 || (pMem->flags==MEM_Undefined((void) (0))
78932 && pMem->szMalloc<=sqlite3DbMallocSize(pMem->db,pMem->zMalloc))((void) (0))
78933 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db,pMem->zMalloc))((void) (0));
78934 if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
78935 if( pMem->db ){
78936 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
78937 }else{
78938 pMem->zMalloc = sqlite3Realloc(pMem->z, n);
78939 if( pMem->zMalloc==0 ) sqlite3_free(pMem->z);
78940 pMem->z = pMem->zMalloc;
78941 }
78942 bPreserve = 0;
78943 }else{
78944 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
78945 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
78946 }
78947 if( pMem->zMalloc==0 ){
78948 sqlite3VdbeMemSetNull(pMem);
78949 pMem->z = 0;
78950 pMem->szMalloc = 0;
78951 return SQLITE_NOMEM_BKPT7;
78952 }else{
78953 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
78954 }
78955
78956 if( bPreserve && pMem->z ){
78957 assert( pMem->z!=pMem->zMalloc )((void) (0));
78958 memcpy(pMem->zMalloc, pMem->z, pMem->n);
78959 }
78960 if( (pMem->flags&MEM_Dyn0x1000)!=0 ){
78961 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC )((void) (0));
78962 pMem->xDel((void *)(pMem->z));
78963 }
78964
78965 pMem->z = pMem->zMalloc;
78966 pMem->flags &= ~(MEM_Dyn0x1000|MEM_Ephem0x4000|MEM_Static0x2000);
78967 return SQLITE_OK0;
78968}
78969
78970/*
78971** Change the pMem->zMalloc allocation to be at least szNew bytes.
78972** If pMem->zMalloc already meets or exceeds the requested size, this
78973** routine is a no-op.
78974**
78975** Any prior string or blob content in the pMem object may be discarded.
78976** The pMem->xDel destructor is called, if it exists. Though MEM_Str
78977** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, MEM_IntReal,
78978** and MEM_Null values are preserved.
78979**
78980** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM)
78981** if unable to complete the resizing.
78982*/
78983SQLITE_PRIVATEstatic int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){
78984 assert( CORRUPT_DB || szNew>0 )((void) (0));
78985 assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 )((void) (0));
78986 if( pMem->szMalloc<szNew ){
78987 return sqlite3VdbeMemGrow(pMem, szNew, 0);
78988 }
78989 assert( (pMem->flags & MEM_Dyn)==0 )((void) (0));
78990 pMem->z = pMem->zMalloc;
78991 pMem->flags &= (MEM_Null0x0001|MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020);
78992 return SQLITE_OK0;
78993}
78994
78995/*
78996** It is already known that pMem contains an unterminated string.
78997** Add the zero terminator.
78998**
78999** Three bytes of zero are added. In this way, there is guaranteed
79000** to be a double-zero byte at an even byte boundary in order to
79001** terminate a UTF16 string, even if the initial size of the buffer
79002** is an odd number of bytes.
79003*/
79004static SQLITE_NOINLINE__attribute__((noinline)) int vdbeMemAddTerminator(Mem *pMem){
79005 if( sqlite3VdbeMemGrow(pMem, pMem->n+3, 1) ){
79006 return SQLITE_NOMEM_BKPT7;
79007 }
79008 pMem->z[pMem->n] = 0;
79009 pMem->z[pMem->n+1] = 0;
79010 pMem->z[pMem->n+2] = 0;
79011 pMem->flags |= MEM_Term0x0200;
79012 return SQLITE_OK0;
79013}
79014
79015/*
79016** Change pMem so that its MEM_Str or MEM_Blob value is stored in
79017** MEM.zMalloc, where it can be safely written.
79018**
79019** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
79020*/
79021SQLITE_PRIVATEstatic int sqlite3VdbeMemMakeWriteable(Mem *pMem){
79022 assert( pMem!=0 )((void) (0));
79023 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
79024 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
79025 if( (pMem->flags & (MEM_Str0x0002|MEM_Blob0x0010))!=0 ){
79026 if( ExpandBlob(pMem)(((pMem)->flags&0x0400)?sqlite3VdbeMemExpandBlob(pMem)
:0)
) return SQLITE_NOMEM7;
79027 if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
79028 int rc = vdbeMemAddTerminator(pMem);
79029 if( rc ) return rc;
79030 }
79031 }
79032 pMem->flags &= ~MEM_Ephem0x4000;
79033#ifdef SQLITE_DEBUG
79034 pMem->pScopyFrom = 0;
79035#endif
79036
79037 return SQLITE_OK0;
79038}
79039
79040/*
79041** If the given Mem* has a zero-filled tail, turn it into an ordinary
79042** blob stored in dynamically allocated space.
79043*/
79044#ifndef SQLITE_OMIT_INCRBLOB
79045SQLITE_PRIVATEstatic int sqlite3VdbeMemExpandBlob(Mem *pMem){
79046 int nByte;
79047 assert( pMem!=0 )((void) (0));
79048 assert( pMem->flags & MEM_Zero )((void) (0));
79049 assert( (pMem->flags&MEM_Blob)!=0 || MemNullNochng(pMem) )((void) (0));
79050 testcase( sqlite3_value_nochange(pMem) );
79051 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
79052 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
79053
79054 /* Set nByte to the number of bytes required to store the expanded blob. */
79055 nByte = pMem->n + pMem->u.nZero;
79056 if( nByte<=0 ){
79057 if( (pMem->flags & MEM_Blob0x0010)==0 ) return SQLITE_OK0;
79058 nByte = 1;
79059 }
79060 if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
79061 return SQLITE_NOMEM_BKPT7;
79062 }
79063 assert( pMem->z!=0 )((void) (0));
79064 assert( sqlite3DbMallocSize(pMem->db,pMem->z) >= nByte )((void) (0));
79065
79066 memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
79067 pMem->n += pMem->u.nZero;
79068 pMem->flags &= ~(MEM_Zero0x0400|MEM_Term0x0200);
79069 return SQLITE_OK0;
79070}
79071#endif
79072
79073/*
79074** Make sure the given Mem is \u0000 terminated.
79075*/
79076SQLITE_PRIVATEstatic int sqlite3VdbeMemNulTerminate(Mem *pMem){
79077 assert( pMem!=0 )((void) (0));
79078 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
79079 testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
79080 testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
79081 if( (pMem->flags & (MEM_Term0x0200|MEM_Str0x0002))!=MEM_Str0x0002 ){
79082 return SQLITE_OK0; /* Nothing to do */
79083 }else{
79084 return vdbeMemAddTerminator(pMem);
79085 }
79086}
79087
79088/*
79089** Add MEM_Str to the set of representations for the given Mem. This
79090** routine is only called if pMem is a number of some kind, not a NULL
79091** or a BLOB.
79092**
79093** Existing representations MEM_Int, MEM_Real, or MEM_IntReal are invalidated
79094** if bForce is true but are retained if bForce is false.
79095**
79096** A MEM_Null value will never be passed to this function. This function is
79097** used for converting values to text for returning to the user (i.e. via
79098** sqlite3_value_text()), or for ensuring that values to be used as btree
79099** keys are strings. In the former case a NULL pointer is returned the
79100** user and the latter is an internal programming error.
79101*/
79102SQLITE_PRIVATEstatic int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){
79103 const int nByte = 32;
79104
79105 assert( pMem!=0 )((void) (0));
79106 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
79107 assert( !(pMem->flags&MEM_Zero) )((void) (0));
79108 assert( !(pMem->flags&(MEM_Str|MEM_Blob)) )((void) (0));
79109 assert( pMem->flags&(MEM_Int|MEM_Real|MEM_IntReal) )((void) (0));
79110 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
79111 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
79112
79113
79114 if( sqlite3VdbeMemClearAndResize(pMem, nByte) ){
79115 pMem->enc = 0;
79116 return SQLITE_NOMEM_BKPT7;
79117 }
79118
79119 vdbeMemRenderNum(nByte, pMem->z, pMem);
79120 assert( pMem->z!=0 )((void) (0));
79121 pMem->n = sqlite3Strlen30NN(pMem->z)(strlen(pMem->z)&0x3fffffff);
79122 pMem->enc = SQLITE_UTF81;
79123 pMem->flags |= MEM_Str0x0002|MEM_Term0x0200;
79124 if( bForce ) pMem->flags &= ~(MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020);
79125 sqlite3VdbeChangeEncoding(pMem, enc);
79126 return SQLITE_OK0;
79127}
79128
79129/*
79130** Memory cell pMem contains the context of an aggregate function.
79131** This routine calls the finalize method for that function. The
79132** result of the aggregate is stored back into pMem.
79133**
79134** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK
79135** otherwise.
79136*/
79137SQLITE_PRIVATEstatic int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
79138 sqlite3_context ctx;
79139 Mem t;
79140 assert( pFunc!=0 )((void) (0));
79141 assert( pMem!=0 )((void) (0));
79142 assert( pMem->db!=0 )((void) (0));
79143 assert( pFunc->xFinalize!=0 )((void) (0));
79144 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef )((void) (0));
79145 assert( sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
79146 memset(&ctx, 0, sizeof(ctx));
79147 memset(&t, 0, sizeof(t));
79148 t.flags = MEM_Null0x0001;
79149 t.db = pMem->db;
79150 ctx.pOut = &t;
79151 ctx.pMem = pMem;
79152 ctx.pFunc = pFunc;
79153 ctx.enc = ENC(t.db)((t.db)->enc);
79154 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
79155 assert( (pMem->flags & MEM_Dyn)==0 )((void) (0));
79156 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
79157 memcpy(pMem, &t, sizeof(t));
79158 return ctx.isError;
79159}
79160
79161/*
79162** Memory cell pAccum contains the context of an aggregate function.
79163** This routine calls the xValue method for that function and stores
79164** the results in memory cell pMem.
79165**
79166** SQLITE_ERROR is returned if xValue() reports an error. SQLITE_OK
79167** otherwise.
79168*/
79169#ifndef SQLITE_OMIT_WINDOWFUNC
79170SQLITE_PRIVATEstatic int sqlite3VdbeMemAggValue(Mem *pAccum, Mem *pOut, FuncDef *pFunc){
79171 sqlite3_context ctx;
79172 assert( pFunc!=0 )((void) (0));
79173 assert( pFunc->xValue!=0 )((void) (0));
79174 assert( (pAccum->flags & MEM_Null)!=0 || pFunc==pAccum->u.pDef )((void) (0));
79175 assert( pAccum->db!=0 )((void) (0));
79176 assert( sqlite3_mutex_held(pAccum->db->mutex) )((void) (0));
79177 memset(&ctx, 0, sizeof(ctx));
79178 sqlite3VdbeMemSetNull(pOut);
79179 ctx.pOut = pOut;
79180 ctx.pMem = pAccum;
79181 ctx.pFunc = pFunc;
79182 ctx.enc = ENC(pAccum->db)((pAccum->db)->enc);
79183 pFunc->xValue(&ctx);
79184 return ctx.isError;
79185}
79186#endif /* SQLITE_OMIT_WINDOWFUNC */
79187
79188/*
79189** If the memory cell contains a value that must be freed by
79190** invoking the external callback in Mem.xDel, then this routine
79191** will free that value. It also sets Mem.flags to MEM_Null.
79192**
79193** This is a helper routine for sqlite3VdbeMemSetNull() and
79194** for sqlite3VdbeMemRelease(). Use those other routines as the
79195** entry point for releasing Mem resources.
79196*/
79197static SQLITE_NOINLINE__attribute__((noinline)) void vdbeMemClearExternAndSetNull(Mem *p){
79198 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) )((void) (0));
79199 assert( VdbeMemDynamic(p) )((void) (0));
79200 if( p->flags&MEM_Agg0x8000 ){
79201 sqlite3VdbeMemFinalize(p, p->u.pDef);
79202 assert( (p->flags & MEM_Agg)==0 )((void) (0));
79203 testcase( p->flags & MEM_Dyn );
79204 }
79205 if( p->flags&MEM_Dyn0x1000 ){
79206 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 )((void) (0));
79207 p->xDel((void *)p->z);
79208 }
79209 p->flags = MEM_Null0x0001;
79210}
79211
79212/*
79213** Release memory held by the Mem p, both external memory cleared
79214** by p->xDel and memory in p->zMalloc.
79215**
79216** This is a helper routine invoked by sqlite3VdbeMemRelease() in
79217** the unusual case where there really is memory in p that needs
79218** to be freed.
79219*/
79220static SQLITE_NOINLINE__attribute__((noinline)) void vdbeMemClear(Mem *p){
79221 if( VdbeMemDynamic(p)(((p)->flags&(0x8000|0x1000))!=0) ){
79222 vdbeMemClearExternAndSetNull(p);
79223 }
79224 if( p->szMalloc ){
79225 sqlite3DbFreeNN(p->db, p->zMalloc);
79226 p->szMalloc = 0;
79227 }
79228 p->z = 0;
79229}
79230
79231/*
79232** Release any memory resources held by the Mem. Both the memory that is
79233** free by Mem.xDel and the Mem.zMalloc allocation are freed.
79234**
79235** Use this routine prior to clean up prior to abandoning a Mem, or to
79236** reset a Mem back to its minimum memory utilization.
79237**
79238** Use sqlite3VdbeMemSetNull() to release just the Mem.xDel space
79239** prior to inserting new content into the Mem.
79240*/
79241SQLITE_PRIVATEstatic void sqlite3VdbeMemRelease(Mem *p){
79242 assert( sqlite3VdbeCheckMemInvariants(p) )((void) (0));
79243 if( VdbeMemDynamic(p)(((p)->flags&(0x8000|0x1000))!=0) || p->szMalloc ){
79244 vdbeMemClear(p);
79245 }
79246}
79247
79248/* Like sqlite3VdbeMemRelease() but faster for cases where we
79249** know in advance that the Mem is not MEM_Dyn or MEM_Agg.
79250*/
79251SQLITE_PRIVATEstatic void sqlite3VdbeMemReleaseMalloc(Mem *p){
79252 assert( !VdbeMemDynamic(p) )((void) (0));
79253 if( p->szMalloc ) vdbeMemClear(p);
79254}
79255
79256/*
79257** Convert a 64-bit IEEE double into a 64-bit signed integer.
79258** If the double is out of range of a 64-bit signed integer then
79259** return the closest available 64-bit signed integer.
79260*/
79261static SQLITE_NOINLINE__attribute__((noinline)) i64 doubleToInt64(double r){
79262#ifdef SQLITE_OMIT_FLOATING_POINT
79263 /* When floating-point is omitted, double and int64 are the same thing */
79264 return r;
79265#else
79266 /*
79267 ** Many compilers we encounter do not define constants for the
79268 ** minimum and maximum 64-bit integers, or they define them
79269 ** inconsistently. And many do not understand the "LL" notation.
79270 ** So we define our own static constants here using nothing
79271 ** larger than a 32-bit integer constant.
79272 */
79273 static const i64 maxInt = LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
79274 static const i64 minInt = SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32)));
79275
79276 if( r<=(double)minInt ){
79277 return minInt;
79278 }else if( r>=(double)maxInt ){
79279 return maxInt;
79280 }else{
79281 return (i64)r;
79282 }
79283#endif
79284}
79285
79286/*
79287** Return some kind of integer value which is the best we can do
79288** at representing the value that *pMem describes as an integer.
79289** If pMem is an integer, then the value is exact. If pMem is
79290** a floating-point then the value returned is the integer part.
79291** If pMem is a string or blob, then we make an attempt to convert
79292** it into an integer and return that. If pMem represents an
79293** an SQL-NULL value, return 0.
79294**
79295** If pMem represents a string value, its encoding might be changed.
79296*/
79297static SQLITE_NOINLINE__attribute__((noinline)) i64 memIntValue(const Mem *pMem){
79298 i64 value = 0;
79299 sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
79300 return value;
79301}
79302SQLITE_PRIVATEstatic i64 sqlite3VdbeIntValue(const Mem *pMem){
79303 int flags;
79304 assert( pMem!=0 )((void) (0));
79305 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
79306 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
79307 flags = pMem->flags;
79308 if( flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
79309 testcase( flags & MEM_IntReal );
79310 return pMem->u.i;
79311 }else if( flags & MEM_Real0x0008 ){
79312 return doubleToInt64(pMem->u.r);
79313 }else if( (flags & (MEM_Str0x0002|MEM_Blob0x0010))!=0 && pMem->z!=0 ){
79314 return memIntValue(pMem);
79315 }else{
79316 return 0;
79317 }
79318}
79319
79320/*
79321** Return the best representation of pMem that we can get into a
79322** double. If pMem is already a double or an integer, return its
79323** value. If it is a string or blob, try to convert it to a double.
79324** If it is a NULL, return 0.0.
79325*/
79326static SQLITE_NOINLINE__attribute__((noinline)) double memRealValue(Mem *pMem){
79327 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
79328 double val = (double)0;
79329 sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
79330 return val;
79331}
79332SQLITE_PRIVATEstatic double sqlite3VdbeRealValue(Mem *pMem){
79333 assert( pMem!=0 )((void) (0));
79334 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
79335 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
79336 if( pMem->flags & MEM_Real0x0008 ){
79337 return pMem->u.r;
79338 }else if( pMem->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
79339 testcase( pMem->flags & MEM_IntReal );
79340 return (double)pMem->u.i;
79341 }else if( pMem->flags & (MEM_Str0x0002|MEM_Blob0x0010) ){
79342 return memRealValue(pMem);
79343 }else{
79344 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
79345 return (double)0;
79346 }
79347}
79348
79349/*
79350** Return 1 if pMem represents true, and return 0 if pMem represents false.
79351** Return the value ifNull if pMem is NULL.
79352*/
79353SQLITE_PRIVATEstatic int sqlite3VdbeBooleanValue(Mem *pMem, int ifNull){
79354 testcase( pMem->flags & MEM_IntReal );
79355 if( pMem->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ) return pMem->u.i!=0;
79356 if( pMem->flags & MEM_Null0x0001 ) return ifNull;
79357 return sqlite3VdbeRealValue(pMem)!=0.0;
79358}
79359
79360/*
79361** The MEM structure is already a MEM_Real. Try to also make it a
79362** MEM_Int if we can.
79363*/
79364SQLITE_PRIVATEstatic void sqlite3VdbeIntegerAffinity(Mem *pMem){
79365 i64 ix;
79366 assert( pMem!=0 )((void) (0));
79367 assert( pMem->flags & MEM_Real )((void) (0));
79368 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
79369 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
79370 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
79371
79372 ix = doubleToInt64(pMem->u.r);
79373
79374 /* Only mark the value as an integer if
79375 **
79376 ** (1) the round-trip conversion real->int->real is a no-op, and
79377 ** (2) The integer is neither the largest nor the smallest
79378 ** possible integer (ticket #3922)
79379 **
79380 ** The second and third terms in the following conditional enforces
79381 ** the second condition under the assumption that addition overflow causes
79382 ** values to wrap around.
79383 */
79384 if( pMem->u.r==ix && ix>SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) && ix<LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) ){
79385 pMem->u.i = ix;
79386 MemSetTypeFlag(pMem, MEM_Int)((pMem)->flags = ((pMem)->flags&~(0x0dbf|0x0400))|0x0004
)
;
79387 }
79388}
79389
79390/*
79391** Convert pMem to type integer. Invalidate any prior representations.
79392*/
79393SQLITE_PRIVATEstatic int sqlite3VdbeMemIntegerify(Mem *pMem){
79394 assert( pMem!=0 )((void) (0));
79395 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
79396 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
79397 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
79398
79399 pMem->u.i = sqlite3VdbeIntValue(pMem);
79400 MemSetTypeFlag(pMem, MEM_Int)((pMem)->flags = ((pMem)->flags&~(0x0dbf|0x0400))|0x0004
)
;
79401 return SQLITE_OK0;
79402}
79403
79404/*
79405** Convert pMem so that it is of type MEM_Real.
79406** Invalidate any prior representations.
79407*/
79408SQLITE_PRIVATEstatic int sqlite3VdbeMemRealify(Mem *pMem){
79409 assert( pMem!=0 )((void) (0));
79410 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
79411 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
79412
79413 pMem->u.r = sqlite3VdbeRealValue(pMem);
79414 MemSetTypeFlag(pMem, MEM_Real)((pMem)->flags = ((pMem)->flags&~(0x0dbf|0x0400))|0x0008
)
;
79415 return SQLITE_OK0;
79416}
79417
79418/* Compare a floating point value to an integer. Return true if the two
79419** values are the same within the precision of the floating point value.
79420**
79421** This function assumes that i was obtained by assignment from r1.
79422**
79423** For some versions of GCC on 32-bit machines, if you do the more obvious
79424** comparison of "r1==(double)i" you sometimes get an answer of false even
79425** though the r1 and (double)i values are bit-for-bit the same.
79426*/
79427SQLITE_PRIVATEstatic int sqlite3RealSameAsInt(double r1, sqlite3_int64 i){
79428 double r2 = (double)i;
79429 return r1==0.0
79430 || (memcmp(&r1, &r2, sizeof(r1))==0
79431 && i >= -2251799813685248LL && i < 2251799813685248LL);
79432}
79433
79434/*
79435** Convert pMem so that it has type MEM_Real or MEM_Int.
79436** Invalidate any prior representations.
79437**
79438** Every effort is made to force the conversion, even if the input
79439** is a string that does not look completely like a number. Convert
79440** as much of the string as we can and ignore the rest.
79441*/
79442SQLITE_PRIVATEstatic int sqlite3VdbeMemNumerify(Mem *pMem){
79443 assert( pMem!=0 )((void) (0));
79444 testcase( pMem->flags & MEM_Int );
79445 testcase( pMem->flags & MEM_Real );
79446 testcase( pMem->flags & MEM_IntReal );
79447 testcase( pMem->flags & MEM_Null );
79448 if( (pMem->flags & (MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020|MEM_Null0x0001))==0 ){
79449 int rc;
79450 sqlite3_int64 ix;
79451 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 )((void) (0));
79452 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
79453 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
79454 if( ((rc==0 || rc==1) && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1)
79455 || sqlite3RealSameAsInt(pMem->u.r, (ix = (i64)pMem->u.r))
79456 ){
79457 pMem->u.i = ix;
79458 MemSetTypeFlag(pMem, MEM_Int)((pMem)->flags = ((pMem)->flags&~(0x0dbf|0x0400))|0x0004
)
;
79459 }else{
79460 MemSetTypeFlag(pMem, MEM_Real)((pMem)->flags = ((pMem)->flags&~(0x0dbf|0x0400))|0x0008
)
;
79461 }
79462 }
79463 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))!=0 )((void) (0));
79464 pMem->flags &= ~(MEM_Str0x0002|MEM_Blob0x0010|MEM_Zero0x0400);
79465 return SQLITE_OK0;
79466}
79467
79468/*
79469** Cast the datatype of the value in pMem according to the affinity
79470** "aff". Casting is different from applying affinity in that a cast
79471** is forced. In other words, the value is converted into the desired
79472** affinity even if that results in loss of data. This routine is
79473** used (for example) to implement the SQL "cast()" operator.
79474*/
79475SQLITE_PRIVATEstatic int sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){
79476 if( pMem->flags & MEM_Null0x0001 ) return SQLITE_OK0;
79477 switch( aff ){
79478 case SQLITE_AFF_BLOB0x41: { /* Really a cast to BLOB */
79479 if( (pMem->flags & MEM_Blob0x0010)==0 ){
79480 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT0x42, encoding);
79481 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed )((void) (0));
79482 if( pMem->flags & MEM_Str0x0002 ) MemSetTypeFlag(pMem, MEM_Blob)((pMem)->flags = ((pMem)->flags&~(0x0dbf|0x0400))|0x0010
)
;
79483 }else{
79484 pMem->flags &= ~(MEM_TypeMask0x0dbf&~MEM_Blob0x0010);
79485 }
79486 break;
79487 }
79488 case SQLITE_AFF_NUMERIC0x43: {
79489 sqlite3VdbeMemNumerify(pMem);
79490 break;
79491 }
79492 case SQLITE_AFF_INTEGER0x44: {
79493 sqlite3VdbeMemIntegerify(pMem);
79494 break;
79495 }
79496 case SQLITE_AFF_REAL0x45: {
79497 sqlite3VdbeMemRealify(pMem);
79498 break;
79499 }
79500 default: {
79501 assert( aff==SQLITE_AFF_TEXT )((void) (0));
79502 assert( MEM_Str==(MEM_Blob>>3) )((void) (0));
79503 pMem->flags |= (pMem->flags&MEM_Blob0x0010)>>3;
79504 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT0x42, encoding);
79505 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed )((void) (0));
79506 pMem->flags &= ~(MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020|MEM_Blob0x0010|MEM_Zero0x0400);
79507 return sqlite3VdbeChangeEncoding(pMem, encoding);
79508 }
79509 }
79510 return SQLITE_OK0;
79511}
79512
79513/*
79514** Initialize bulk memory to be a consistent Mem object.
79515**
79516** The minimum amount of initialization feasible is performed.
79517*/
79518SQLITE_PRIVATEstatic void sqlite3VdbeMemInit(Mem *pMem, sqlite3 *db, u16 flags){
79519 assert( (flags & ~MEM_TypeMask)==0 )((void) (0));
79520 pMem->flags = flags;
79521 pMem->db = db;
79522 pMem->szMalloc = 0;
79523}
79524
79525
79526/*
79527** Delete any previous value and set the value stored in *pMem to NULL.
79528**
79529** This routine calls the Mem.xDel destructor to dispose of values that
79530** require the destructor. But it preserves the Mem.zMalloc memory allocation.
79531** To free all resources, use sqlite3VdbeMemRelease(), which both calls this
79532** routine to invoke the destructor and deallocates Mem.zMalloc.
79533**
79534** Use this routine to reset the Mem prior to insert a new value.
79535**
79536** Use sqlite3VdbeMemRelease() to complete erase the Mem prior to abandoning it.
79537*/
79538SQLITE_PRIVATEstatic void sqlite3VdbeMemSetNull(Mem *pMem){
79539 if( VdbeMemDynamic(pMem)(((pMem)->flags&(0x8000|0x1000))!=0) ){
79540 vdbeMemClearExternAndSetNull(pMem);
79541 }else{
79542 pMem->flags = MEM_Null0x0001;
79543 }
79544}
79545SQLITE_PRIVATEstatic void sqlite3ValueSetNull(sqlite3_value *p){
79546 sqlite3VdbeMemSetNull((Mem*)p);
79547}
79548
79549/*
79550** Delete any previous value and set the value to be a BLOB of length
79551** n containing all zeros.
79552*/
79553#ifndef SQLITE_OMIT_INCRBLOB
79554SQLITE_PRIVATEstatic void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
79555 sqlite3VdbeMemRelease(pMem);
79556 pMem->flags = MEM_Blob0x0010|MEM_Zero0x0400;
79557 pMem->n = 0;
79558 if( n<0 ) n = 0;
79559 pMem->u.nZero = n;
79560 pMem->enc = SQLITE_UTF81;
79561 pMem->z = 0;
79562}
79563#else
79564SQLITE_PRIVATEstatic int sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
79565 int nByte = n>0?n:1;
79566 if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){
79567 return SQLITE_NOMEM_BKPT7;
79568 }
79569 assert( pMem->z!=0 )((void) (0));
79570 assert( sqlite3DbMallocSize(pMem->db, pMem->z)>=nByte )((void) (0));
79571 memset(pMem->z, 0, nByte);
79572 pMem->n = n>0?n:0;
79573 pMem->flags = MEM_Blob0x0010;
79574 pMem->enc = SQLITE_UTF81;
79575 return SQLITE_OK0;
79576}
79577#endif
79578
79579/*
79580** The pMem is known to contain content that needs to be destroyed prior
79581** to a value change. So invoke the destructor, then set the value to
79582** a 64-bit integer.
79583*/
79584static SQLITE_NOINLINE__attribute__((noinline)) void vdbeReleaseAndSetInt64(Mem *pMem, i64 val){
79585 sqlite3VdbeMemSetNull(pMem);
79586 pMem->u.i = val;
79587 pMem->flags = MEM_Int0x0004;
79588}
79589
79590/*
79591** Delete any previous value and set the value stored in *pMem to val,
79592** manifest type INTEGER.
79593*/
79594SQLITE_PRIVATEstatic void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
79595 if( VdbeMemDynamic(pMem)(((pMem)->flags&(0x8000|0x1000))!=0) ){
79596 vdbeReleaseAndSetInt64(pMem, val);
79597 }else{
79598 pMem->u.i = val;
79599 pMem->flags = MEM_Int0x0004;
79600 }
79601}
79602
79603/* A no-op destructor */
79604SQLITE_PRIVATEstatic void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p)(void)(p); }
79605
79606/*
79607** Set the value stored in *pMem should already be a NULL.
79608** Also store a pointer to go with it.
79609*/
79610SQLITE_PRIVATEstatic void sqlite3VdbeMemSetPointer(
79611 Mem *pMem,
79612 void *pPtr,
79613 const char *zPType,
79614 void (*xDestructor)(void*)
79615){
79616 assert( pMem->flags==MEM_Null )((void) (0));
79617 vdbeMemClear(pMem);
79618 pMem->u.zPType = zPType ? zPType : "";
79619 pMem->z = pPtr;
79620 pMem->flags = MEM_Null0x0001|MEM_Dyn0x1000|MEM_Subtype0x0800|MEM_Term0x0200;
79621 pMem->eSubtype = 'p';
79622 pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor;
79623}
79624
79625#ifndef SQLITE_OMIT_FLOATING_POINT
79626/*
79627** Delete any previous value and set the value stored in *pMem to val,
79628** manifest type REAL.
79629*/
79630SQLITE_PRIVATEstatic void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
79631 sqlite3VdbeMemSetNull(pMem);
79632 if( !sqlite3IsNaN(val) ){
79633 pMem->u.r = val;
79634 pMem->flags = MEM_Real0x0008;
79635 }
79636}
79637#endif
79638
79639#ifdef SQLITE_DEBUG
79640/*
79641** Return true if the Mem holds a RowSet object. This routine is intended
79642** for use inside of assert() statements.
79643*/
79644SQLITE_PRIVATEstatic int sqlite3VdbeMemIsRowSet(const Mem *pMem){
79645 return (pMem->flags&(MEM_Blob0x0010|MEM_Dyn0x1000))==(MEM_Blob0x0010|MEM_Dyn0x1000)
79646 && pMem->xDel==sqlite3RowSetDelete;
79647}
79648#endif
79649
79650/*
79651** Delete any previous value and set the value of pMem to be an
79652** empty boolean index.
79653**
79654** Return SQLITE_OK on success and SQLITE_NOMEM if a memory allocation
79655** error occurs.
79656*/
79657SQLITE_PRIVATEstatic int sqlite3VdbeMemSetRowSet(Mem *pMem){
79658 sqlite3 *db = pMem->db;
79659 RowSet *p;
79660 assert( db!=0 )((void) (0));
79661 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
79662 sqlite3VdbeMemRelease(pMem);
79663 p = sqlite3RowSetInit(db);
79664 if( p==0 ) return SQLITE_NOMEM7;
79665 pMem->z = (char*)p;
79666 pMem->flags = MEM_Blob0x0010|MEM_Dyn0x1000;
79667 pMem->xDel = sqlite3RowSetDelete;
79668 return SQLITE_OK0;
79669}
79670
79671/*
79672** Return true if the Mem object contains a TEXT or BLOB that is
79673** too large - whose size exceeds SQLITE_MAX_LENGTH.
79674*/
79675SQLITE_PRIVATEstatic int sqlite3VdbeMemTooBig(Mem *p){
79676 assert( p->db!=0 )((void) (0));
79677 if( p->flags & (MEM_Str0x0002|MEM_Blob0x0010) ){
79678 int n = p->n;
79679 if( p->flags & MEM_Zero0x0400 ){
79680 n += p->u.nZero;
79681 }
79682 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH0];
79683 }
79684 return 0;
79685}
79686
79687#ifdef SQLITE_DEBUG
79688/*
79689** This routine prepares a memory cell for modification by breaking
79690** its link to a shallow copy and by marking any current shallow
79691** copies of this cell as invalid.
79692**
79693** This is used for testing and debugging only - to help ensure that shallow
79694** copies (created by OP_SCopy) are not misused.
79695*/
79696SQLITE_PRIVATEstatic void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
79697 int i;
79698 Mem *pX;
79699 for(i=1, pX=pVdbe->aMem+1; i<pVdbe->nMem; i++, pX++){
79700 if( pX->pScopyFrom==pMem ){
79701 u16 mFlags;
79702 if( pVdbe->db->flags & SQLITE_VdbeTrace ){
79703 sqlite3DebugPrintf("Invalidate R[%d] due to change in R[%d]\n",
79704 (int)(pX - pVdbe->aMem), (int)(pMem - pVdbe->aMem));
79705 }
79706 /* If pX is marked as a shallow copy of pMem, then try to verify that
79707 ** no significant changes have been made to pX since the OP_SCopy.
79708 ** A significant change would indicated a missed call to this
79709 ** function for pX. Minor changes, such as adding or removing a
79710 ** dual type, are allowed, as long as the underlying value is the
79711 ** same. */
79712 mFlags = pMem->flags & pX->flags & pX->mScopyFlags;
79713 assert( (mFlags&(MEM_Int|MEM_IntReal))==0 || pMem->u.i==pX->u.i )((void) (0));
79714
79715 /* pMem is the register that is changing. But also mark pX as
79716 ** undefined so that we can quickly detect the shallow-copy error */
79717 pX->flags = MEM_Undefined0x0000;
79718 pX->pScopyFrom = 0;
79719 }
79720 }
79721 pMem->pScopyFrom = 0;
79722}
79723#endif /* SQLITE_DEBUG */
79724
79725/*
79726** Make an shallow copy of pFrom into pTo. Prior contents of
79727** pTo are freed. The pFrom->z field is not duplicated. If
79728** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
79729** and flags gets srcType (either MEM_Ephem or MEM_Static).
79730*/
79731static SQLITE_NOINLINE__attribute__((noinline)) void vdbeClrCopy(Mem *pTo, const Mem *pFrom, int eType){
79732 vdbeMemClearExternAndSetNull(pTo);
79733 assert( !VdbeMemDynamic(pTo) )((void) (0));
79734 sqlite3VdbeMemShallowCopy(pTo, pFrom, eType);
79735}
79736SQLITE_PRIVATEstatic void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
79737 assert( !sqlite3VdbeMemIsRowSet(pFrom) )((void) (0));
79738 assert( pTo->db==pFrom->db )((void) (0));
79739 if( VdbeMemDynamic(pTo)(((pTo)->flags&(0x8000|0x1000))!=0) ){ vdbeClrCopy(pTo,pFrom,srcType); return; }
79740 memcpy(pTo, pFrom, MEMCELLSIZE__builtin_offsetof(Mem, db));
79741 if( (pFrom->flags&MEM_Static0x2000)==0 ){
79742 pTo->flags &= ~(MEM_Dyn0x1000|MEM_Static0x2000|MEM_Ephem0x4000);
79743 assert( srcType==MEM_Ephem || srcType==MEM_Static )((void) (0));
79744 pTo->flags |= srcType;
79745 }
79746}
79747
79748/*
79749** Make a full copy of pFrom into pTo. Prior contents of pTo are
79750** freed before the copy is made.
79751*/
79752SQLITE_PRIVATEstatic int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
79753 int rc = SQLITE_OK0;
79754
79755 assert( !sqlite3VdbeMemIsRowSet(pFrom) )((void) (0));
79756 if( VdbeMemDynamic(pTo)(((pTo)->flags&(0x8000|0x1000))!=0) ) vdbeMemClearExternAndSetNull(pTo);
79757 memcpy(pTo, pFrom, MEMCELLSIZE__builtin_offsetof(Mem, db));
79758 pTo->flags &= ~MEM_Dyn0x1000;
79759 if( pTo->flags&(MEM_Str0x0002|MEM_Blob0x0010) ){
79760 if( 0==(pFrom->flags&MEM_Static0x2000) ){
79761 pTo->flags |= MEM_Ephem0x4000;
79762 rc = sqlite3VdbeMemMakeWriteable(pTo);
79763 }
79764 }
79765
79766 return rc;
79767}
79768
79769/*
79770** Transfer the contents of pFrom to pTo. Any existing value in pTo is
79771** freed. If pFrom contains ephemeral data, a copy is made.
79772**
79773** pFrom contains an SQL NULL when this routine returns.
79774*/
79775SQLITE_PRIVATEstatic void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
79776 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) )((void) (0));
79777 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) )((void) (0));
79778 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db )((void) (0));
79779
79780 sqlite3VdbeMemRelease(pTo);
79781 memcpy(pTo, pFrom, sizeof(Mem));
79782 pFrom->flags = MEM_Null0x0001;
79783 pFrom->szMalloc = 0;
79784}
79785
79786/*
79787** Change the value of a Mem to be a string or a BLOB.
79788**
79789** The memory management strategy depends on the value of the xDel
79790** parameter. If the value passed is SQLITE_TRANSIENT, then the
79791** string is copied into a (possibly existing) buffer managed by the
79792** Mem structure. Otherwise, any existing buffer is freed and the
79793** pointer copied.
79794**
79795** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
79796** size limit) then no memory allocation occurs. If the string can be
79797** stored without allocating memory, then it is. If a memory allocation
79798** is required to store the string, then value of pMem is unchanged. In
79799** either case, SQLITE_TOOBIG is returned.
79800**
79801** The "enc" parameter is the text encoding for the string, or zero
79802** to store a blob.
79803**
79804** If n is negative, then the string consists of all bytes up to but
79805** excluding the first zero character. The n parameter must be
79806** non-negative for blobs.
79807*/
79808SQLITE_PRIVATEstatic int sqlite3VdbeMemSetStr(
79809 Mem *pMem, /* Memory cell to set to string value */
79810 const char *z, /* String pointer */
79811 i64 n, /* Bytes in string, or negative */
79812 u8 enc, /* Encoding of z. 0 for BLOBs */
79813 void (*xDel)(void*) /* Destructor function */
79814){
79815 i64 nByte = n; /* New value for pMem->n */
79816 int iLimit; /* Maximum allowed string or blob size */
79817 u16 flags; /* New value for pMem->flags */
79818
79819 assert( pMem!=0 )((void) (0));
79820 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
79821 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
79822 assert( enc!=0 || n>=0 )((void) (0));
79823
79824 /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
79825 if( !z ){
79826 sqlite3VdbeMemSetNull(pMem);
79827 return SQLITE_OK0;
79828 }
79829
79830 if( pMem->db ){
79831 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH0];
79832 }else{
79833 iLimit = SQLITE_MAX_LENGTH2147483645;
79834 }
79835 if( nByte<0 ){
79836 assert( enc!=0 )((void) (0));
79837 if( enc==SQLITE_UTF81 ){
79838 nByte = strlen(z);
79839 }else{
79840 for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
79841 }
79842 flags= MEM_Str0x0002|MEM_Term0x0200;
79843 }else if( enc==0 ){
79844 flags = MEM_Blob0x0010;
79845 enc = SQLITE_UTF81;
79846 }else{
79847 flags = MEM_Str0x0002;
79848 }
79849 if( nByte>iLimit ){
79850 if( xDel && xDel!=SQLITE_TRANSIENT((sqlite3_destructor_type)-1) ){
79851 if( xDel==SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear) ){
79852 sqlite3DbFree(pMem->db, (void*)z);
79853 }else{
79854 xDel((void*)z);
79855 }
79856 }
79857 sqlite3VdbeMemSetNull(pMem);
79858 return sqlite3ErrorToParser(pMem->db, SQLITE_TOOBIG18);
79859 }
79860
79861 /* The following block sets the new values of Mem.z and Mem.xDel. It
79862 ** also sets a flag in local variable "flags" to indicate the memory
79863 ** management (one of MEM_Dyn or MEM_Static).
79864 */
79865 if( xDel==SQLITE_TRANSIENT((sqlite3_destructor_type)-1) ){
79866 i64 nAlloc = nByte;
79867 if( flags&MEM_Term0x0200 ){
79868 nAlloc += (enc==SQLITE_UTF81?1:2);
79869 }
79870 testcase( nAlloc==0 );
79871 testcase( nAlloc==31 );
79872 testcase( nAlloc==32 );
79873 if( sqlite3VdbeMemClearAndResize(pMem, (int)MAX(nAlloc,32)((nAlloc)>(32)?(nAlloc):(32))) ){
79874 return SQLITE_NOMEM_BKPT7;
79875 }
79876 memcpy(pMem->z, z, nAlloc);
79877 }else{
79878 sqlite3VdbeMemRelease(pMem);
79879 pMem->z = (char *)z;
79880 if( xDel==SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear) ){
79881 pMem->zMalloc = pMem->z;
79882 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
79883 }else{
79884 pMem->xDel = xDel;
79885 flags |= ((xDel==SQLITE_STATIC((sqlite3_destructor_type)0))?MEM_Static0x2000:MEM_Dyn0x1000);
79886 }
79887 }
79888
79889 pMem->n = (int)(nByte & 0x7fffffff);
79890 pMem->flags = flags;
79891 pMem->enc = enc;
79892
79893#ifndef SQLITE_OMIT_UTF16
79894 if( enc>SQLITE_UTF81 && sqlite3VdbeMemHandleBom(pMem) ){
79895 return SQLITE_NOMEM_BKPT7;
79896 }
79897#endif
79898
79899
79900 return SQLITE_OK0;
79901}
79902
79903/*
79904** Move data out of a btree key or data field and into a Mem structure.
79905** The data is payload from the entry that pCur is currently pointing
79906** to. offset and amt determine what portion of the data or key to retrieve.
79907** The result is written into the pMem element.
79908**
79909** The pMem object must have been initialized. This routine will use
79910** pMem->zMalloc to hold the content from the btree, if possible. New
79911** pMem->zMalloc space will be allocated if necessary. The calling routine
79912** is responsible for making sure that the pMem object is eventually
79913** destroyed.
79914**
79915** If this routine fails for any reason (malloc returns NULL or unable
79916** to read from the disk) then the pMem is left in an inconsistent state.
79917*/
79918SQLITE_PRIVATEstatic int sqlite3VdbeMemFromBtree(
79919 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
79920 u32 offset, /* Offset from the start of data to return bytes from. */
79921 u32 amt, /* Number of bytes to return. */
79922 Mem *pMem /* OUT: Return data in this Mem structure. */
79923){
79924 int rc;
79925 pMem->flags = MEM_Null0x0001;
79926 if( sqlite3BtreeMaxRecordSize(pCur)<offset+amt ){
79927 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(79927);
79928 }
79929 if( SQLITE_OK0==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+1)) ){
79930 rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
79931 if( rc==SQLITE_OK0 ){
79932 pMem->z[amt] = 0; /* Overrun area used when reading malformed records */
79933 pMem->flags = MEM_Blob0x0010;
79934 pMem->n = (int)amt;
79935 }else{
79936 sqlite3VdbeMemRelease(pMem);
79937 }
79938 }
79939 return rc;
79940}
79941SQLITE_PRIVATEstatic int sqlite3VdbeMemFromBtreeZeroOffset(
79942 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
79943 u32 amt, /* Number of bytes to return. */
79944 Mem *pMem /* OUT: Return data in this Mem structure. */
79945){
79946 u32 available = 0; /* Number of bytes available on the local btree page */
79947 int rc = SQLITE_OK0; /* Return code */
79948
79949 assert( sqlite3BtreeCursorIsValid(pCur) )((void) (0));
79950 assert( !VdbeMemDynamic(pMem) )((void) (0));
79951
79952 /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert()
79953 ** that both the BtShared and database handle mutexes are held. */
79954 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
79955 pMem->z = (char *)sqlite3BtreePayloadFetch(pCur, &available);
79956 assert( pMem->z!=0 )((void) (0));
79957
79958 if( amt<=available ){
79959 pMem->flags = MEM_Blob0x0010|MEM_Ephem0x4000;
79960 pMem->n = (int)amt;
79961 }else{
79962 rc = sqlite3VdbeMemFromBtree(pCur, 0, amt, pMem);
79963 }
79964
79965 return rc;
79966}
79967
79968/*
79969** The pVal argument is known to be a value other than NULL.
79970** Convert it into a string with encoding enc and return a pointer
79971** to a zero-terminated version of that string.
79972*/
79973static SQLITE_NOINLINE__attribute__((noinline)) const void *valueToText(sqlite3_value* pVal, u8 enc){
79974 assert( pVal!=0 )((void) (0));
79975 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) )((void) (0));
79976 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) )((void) (0));
79977 assert( !sqlite3VdbeMemIsRowSet(pVal) )((void) (0));
79978 assert( (pVal->flags & (MEM_Null))==0 )((void) (0));
79979 if( pVal->flags & (MEM_Blob0x0010|MEM_Str0x0002) ){
79980 if( ExpandBlob(pVal)(((pVal)->flags&0x0400)?sqlite3VdbeMemExpandBlob(pVal)
:0)
) return 0;
79981 pVal->flags |= MEM_Str0x0002;
79982 if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED8) ){
79983 sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED8);
79984 }
79985 if( (enc & SQLITE_UTF16_ALIGNED8)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)((int)(long int)(pVal->z))) ){
79986 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 )((void) (0));
79987 if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK0 ){
79988 return 0;
79989 }
79990 }
79991 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
79992 }else{
79993 sqlite3VdbeMemStringify(pVal, enc, 0);
79994 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) )((void) (0));
79995 }
79996 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0((void) (0))
79997 || pVal->db->mallocFailed )((void) (0));
79998 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED8) ){
79999 assert( sqlite3VdbeMemValidStrRep(pVal) )((void) (0));
80000 return pVal->z;
80001 }else{
80002 return 0;
80003 }
80004}
80005
80006/* This function is only available internally, it is not part of the
80007** external API. It works in a similar way to sqlite3_value_text(),
80008** except the data returned is in the encoding specified by the second
80009** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
80010** SQLITE_UTF8.
80011**
80012** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
80013** If that is the case, then the result must be aligned on an even byte
80014** boundary.
80015*/
80016SQLITE_PRIVATEstatic const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
80017 if( !pVal ) return 0;
80018 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) )((void) (0));
80019 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) )((void) (0));
80020 assert( !sqlite3VdbeMemIsRowSet(pVal) )((void) (0));
80021 if( (pVal->flags&(MEM_Str0x0002|MEM_Term0x0200))==(MEM_Str0x0002|MEM_Term0x0200) && pVal->enc==enc ){
80022 assert( sqlite3VdbeMemValidStrRep(pVal) )((void) (0));
80023 return pVal->z;
80024 }
80025 if( pVal->flags&MEM_Null0x0001 ){
80026 return 0;
80027 }
80028 return valueToText(pVal, enc);
80029}
80030
80031/*
80032** Create a new sqlite3_value object.
80033*/
80034SQLITE_PRIVATEstatic sqlite3_value *sqlite3ValueNew(sqlite3 *db){
80035 Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
80036 if( p ){
80037 p->flags = MEM_Null0x0001;
80038 p->db = db;
80039 }
80040 return p;
80041}
80042
80043/*
80044** Context object passed by sqlite3Stat4ProbeSetValue() through to
80045** valueNew(). See comments above valueNew() for details.
80046*/
80047struct ValueNewStat4Ctx {
80048 Parse *pParse;
80049 Index *pIdx;
80050 UnpackedRecord **ppRec;
80051 int iVal;
80052};
80053
80054/*
80055** Allocate and return a pointer to a new sqlite3_value object. If
80056** the second argument to this function is NULL, the object is allocated
80057** by calling sqlite3ValueNew().
80058**
80059** Otherwise, if the second argument is non-zero, then this function is
80060** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
80061** already been allocated, allocate the UnpackedRecord structure that
80062** that function will return to its caller here. Then return a pointer to
80063** an sqlite3_value within the UnpackedRecord.a[] array.
80064*/
80065static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
80066#ifdef SQLITE_ENABLE_STAT4
80067 if( p ){
80068 UnpackedRecord *pRec = p->ppRec[0];
80069
80070 if( pRec==0 ){
80071 Index *pIdx = p->pIdx; /* Index being probed */
80072 int nByte; /* Bytes of space to allocate */
80073 int i; /* Counter variable */
80074 int nCol = pIdx->nColumn; /* Number of index columns including rowid */
80075
80076 nByte = sizeof(Mem) * nCol + ROUND8(sizeof(UnpackedRecord))(((sizeof(UnpackedRecord))+7)&~7);
80077 pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte);
80078 if( pRec ){
80079 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
80080 if( pRec->pKeyInfo ){
80081 assert( pRec->pKeyInfo->nAllField==nCol )((void) (0));
80082 assert( pRec->pKeyInfo->enc==ENC(db) )((void) (0));
80083 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord))(((sizeof(UnpackedRecord))+7)&~7));
80084 for(i=0; i<nCol; i++){
80085 pRec->aMem[i].flags = MEM_Null0x0001;
80086 pRec->aMem[i].db = db;
80087 }
80088 }else{
80089 sqlite3DbFreeNN(db, pRec);
80090 pRec = 0;
80091 }
80092 }
80093 if( pRec==0 ) return 0;
80094 p->ppRec[0] = pRec;
80095 }
80096
80097 pRec->nField = p->iVal+1;
80098 return &pRec->aMem[p->iVal];
80099 }
80100#else
80101 UNUSED_PARAMETER(p)(void)(p);
80102#endif /* defined(SQLITE_ENABLE_STAT4) */
80103 return sqlite3ValueNew(db);
80104}
80105
80106/*
80107** The expression object indicated by the second argument is guaranteed
80108** to be a scalar SQL function. If
80109**
80110** * all function arguments are SQL literals,
80111** * one of the SQLITE_FUNC_CONSTANT or _SLOCHNG function flags is set, and
80112** * the SQLITE_FUNC_NEEDCOLL function flag is not set,
80113**
80114** then this routine attempts to invoke the SQL function. Assuming no
80115** error occurs, output parameter (*ppVal) is set to point to a value
80116** object containing the result before returning SQLITE_OK.
80117**
80118** Affinity aff is applied to the result of the function before returning.
80119** If the result is a text value, the sqlite3_value object uses encoding
80120** enc.
80121**
80122** If the conditions above are not met, this function returns SQLITE_OK
80123** and sets (*ppVal) to NULL. Or, if an error occurs, (*ppVal) is set to
80124** NULL and an SQLite error code returned.
80125*/
80126#ifdef SQLITE_ENABLE_STAT4
80127static int valueFromFunction(0
80128 sqlite3 *db, /* The database connection */0
80129 const Expr *p, /* The expression to evaluate */0
80130 u8 enc, /* Encoding to use */0
80131 u8 aff, /* Affinity to use */0
80132 sqlite3_value **ppVal, /* Write the new value here */0
80133 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */0
80134)0{
80135 sqlite3_context ctx; /* Context object for function invocation */
80136 sqlite3_value **apVal = 0; /* Function arguments */
80137 int nVal = 0; /* Size of apVal[] array */
80138 FuncDef *pFunc = 0; /* Function definition */
80139 sqlite3_value *pVal = 0; /* New value */
80140 int rc = SQLITE_OK0; /* Return code */
80141 ExprList *pList = 0; /* Function arguments */
80142 int i; /* Iterator variable */
80143
80144 assert( pCtx!=0 )((void) (0));
80145 assert( (p->flags & EP_TokenOnly)==0 )((void) (0));
80146 assert( ExprUseXList(p) )((void) (0));
80147 pList = p->x.pList;
80148 if( pList ) nVal = pList->nExpr;
80149 assert( !ExprHasProperty(p, EP_IntValue) )((void) (0));
80150 pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0);
80151 assert( pFunc )((void) (0));
80152 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT0x0800|SQLITE_FUNC_SLOCHNG0x2000))==0
80153 || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL0x0020)
80154 ){
80155 return SQLITE_OK0;
80156 }
80157
80158 if( pList ){
80159 apVal = (sqlite3_value**)sqlite3DbMallocZero(db, sizeof(apVal[0]) * nVal);
80160 if( apVal==0 ){
80161 rc = SQLITE_NOMEM_BKPT7;
80162 goto value_from_function_out;
80163 }
80164 for(i=0; i<nVal; i++){
80165 rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]);
80166 if( apVal[i]==0 || rc!=SQLITE_OK0 ) goto value_from_function_out;
80167 }
80168 }
80169
80170 pVal = valueNew(db, pCtx);
80171 if( pVal==0 ){
80172 rc = SQLITE_NOMEM_BKPT7;
80173 goto value_from_function_out;
80174 }
80175
80176 testcase( pCtx->pParse->rc==SQLITE_ERROR );
80177 testcase( pCtx->pParse->rc==SQLITE_OK );
80178 memset(&ctx, 0, sizeof(ctx));
80179 ctx.pOut = pVal;
80180 ctx.pFunc = pFunc;
80181 ctx.enc = ENC(db)((db)->enc);
80182 pFunc->xSFunc(&ctx, nVal, apVal);
80183 if( ctx.isError ){
80184 rc = ctx.isError;
80185 sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
80186 }else{
80187 sqlite3ValueApplyAffinity(pVal, aff, SQLITE_UTF81);
80188 assert( rc==SQLITE_OK )((void) (0));
80189 rc = sqlite3VdbeChangeEncoding(pVal, enc);
80190 if( rc==SQLITE_OK0 && sqlite3VdbeMemTooBig(pVal) ){
80191 rc = SQLITE_TOOBIG18;
80192 pCtx->pParse->nErr++;
80193 }
80194 }
80195 pCtx->pParse->rc = rc;
80196
80197 value_from_function_out:
80198 if( rc!=SQLITE_OK0 ){
80199 pVal = 0;
80200 }
80201 if( apVal ){
80202 for(i=0; i<nVal; i++){
80203 sqlite3ValueFree(apVal[i]);
80204 }
80205 sqlite3DbFreeNN(db, apVal);
80206 }
80207
80208 *ppVal = pVal;
80209 return rc;
80210}
80211#else
80212# define valueFromFunction(a,b,c,d,e,f)0 SQLITE_OK0
80213#endif /* defined(SQLITE_ENABLE_STAT4) */
80214
80215/*
80216** Extract a value from the supplied expression in the manner described
80217** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
80218** using valueNew().
80219**
80220** If pCtx is NULL and an error occurs after the sqlite3_value object
80221** has been allocated, it is freed before returning. Or, if pCtx is not
80222** NULL, it is assumed that the caller will free any allocated object
80223** in all cases.
80224*/
80225static int valueFromExpr(
80226 sqlite3 *db, /* The database connection */
80227 const Expr *pExpr, /* The expression to evaluate */
80228 u8 enc, /* Encoding to use */
80229 u8 affinity, /* Affinity to use */
80230 sqlite3_value **ppVal, /* Write the new value here */
80231 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
80232){
80233 int op;
80234 char *zVal = 0;
80235 sqlite3_value *pVal = 0;
80236 int negInt = 1;
80237 const char *zNeg = "";
80238 int rc = SQLITE_OK0;
80239
80240 assert( pExpr!=0 )((void) (0));
80241 while( (op = pExpr->op)==TK_UPLUS174 || op==TK_SPAN181 ) pExpr = pExpr->pLeft;
80242 if( op==TK_REGISTER176 ) op = pExpr->op2;
80243
80244 /* Compressed expressions only appear when parsing the DEFAULT clause
80245 ** on a table column definition, and hence only when pCtx==0. This
80246 ** check ensures that an EP_TokenOnly expression is never passed down
80247 ** into valueFromFunction(). */
80248 assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 )((void) (0));
80249
80250 if( op==TK_CAST36 ){
80251 u8 aff;
80252 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
80253 aff = sqlite3AffinityType(pExpr->u.zToken,0);
80254 rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
80255 testcase( rc!=SQLITE_OK );
80256 if( *ppVal ){
80257 sqlite3VdbeMemCast(*ppVal, aff, enc);
80258 sqlite3ValueApplyAffinity(*ppVal, affinity, enc);
80259 }
80260 return rc;
80261 }
80262
80263 /* Handle negative integers in a single step. This is needed in the
80264 ** case when the value is -9223372036854775808.
80265 */
80266 if( op==TK_UMINUS173
80267 && (pExpr->pLeft->op==TK_INTEGER155 || pExpr->pLeft->op==TK_FLOAT153) ){
80268 pExpr = pExpr->pLeft;
80269 op = pExpr->op;
80270 negInt = -1;
80271 zNeg = "-";
80272 }
80273
80274 if( op==TK_STRING117 || op==TK_FLOAT153 || op==TK_INTEGER155 ){
80275 pVal = valueNew(db, pCtx);
80276 if( pVal==0 ) goto no_mem;
80277 if( ExprHasProperty(pExpr, EP_IntValue)(((pExpr)->flags&(0x000800))!=0) ){
80278 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
80279 }else{
80280 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
80281 if( zVal==0 ) goto no_mem;
80282 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF81, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
80283 }
80284 if( (op==TK_INTEGER155 || op==TK_FLOAT153 ) && affinity==SQLITE_AFF_BLOB0x41 ){
80285 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC0x43, SQLITE_UTF81);
80286 }else{
80287 sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF81);
80288 }
80289 assert( (pVal->flags & MEM_IntReal)==0 )((void) (0));
80290 if( pVal->flags & (MEM_Int0x0004|MEM_IntReal0x0020|MEM_Real0x0008) ){
80291 testcase( pVal->flags & MEM_Int );
80292 testcase( pVal->flags & MEM_Real );
80293 pVal->flags &= ~MEM_Str0x0002;
80294 }
80295 if( enc!=SQLITE_UTF81 ){
80296 rc = sqlite3VdbeChangeEncoding(pVal, enc);
80297 }
80298 }else if( op==TK_UMINUS173 ) {
80299 /* This branch happens for multiple negative signs. Ex: -(-5) */
80300 if( SQLITE_OK0==valueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal,pCtx)
80301 && pVal!=0
80302 ){
80303 sqlite3VdbeMemNumerify(pVal);
80304 if( pVal->flags & MEM_Real0x0008 ){
80305 pVal->u.r = -pVal->u.r;
80306 }else if( pVal->u.i==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ){
80307#ifndef SQLITE_OMIT_FLOATING_POINT
80308 pVal->u.r = -(double)SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32)));
80309#else
80310 pVal->u.r = LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
80311#endif
80312 MemSetTypeFlag(pVal, MEM_Real)((pVal)->flags = ((pVal)->flags&~(0x0dbf|0x0400))|0x0008
)
;
80313 }else{
80314 pVal->u.i = -pVal->u.i;
80315 }
80316 sqlite3ValueApplyAffinity(pVal, affinity, enc);
80317 }
80318 }else if( op==TK_NULL121 ){
80319 pVal = valueNew(db, pCtx);
80320 if( pVal==0 ) goto no_mem;
80321 sqlite3VdbeMemSetNull(pVal);
80322 }
80323#ifndef SQLITE_OMIT_BLOB_LITERAL
80324 else if( op==TK_BLOB154 ){
80325 int nVal;
80326 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
80327 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' )((void) (0));
80328 assert( pExpr->u.zToken[1]=='\'' )((void) (0));
80329 pVal = valueNew(db, pCtx);
80330 if( !pVal ) goto no_mem;
80331 zVal = &pExpr->u.zToken[2];
80332 nVal = sqlite3Strlen30(zVal)-1;
80333 assert( zVal[nVal]=='\'' )((void) (0));
80334 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
80335 0, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
80336 }
80337#endif
80338#ifdef SQLITE_ENABLE_STAT4
80339 else if( op==TK_FUNCTION172 && pCtx!=0 ){
80340 rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx)0;
80341 }
80342#endif
80343 else if( op==TK_TRUEFALSE170 ){
80344 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
80345 pVal = valueNew(db, pCtx);
80346 if( pVal ){
80347 pVal->flags = MEM_Int0x0004;
80348 pVal->u.i = pExpr->u.zToken[4]==0;
80349 }
80350 }
80351
80352 *ppVal = pVal;
80353 return rc;
80354
80355no_mem:
80356#ifdef SQLITE_ENABLE_STAT4
80357 if( pCtx==0 || NEVER(pCtx->pParse->nErr==0)(pCtx->pParse->nErr==0) )
80358#endif
80359 sqlite3OomFault(db);
80360 sqlite3DbFree(db, zVal);
80361 assert( *ppVal==0 )((void) (0));
80362#ifdef SQLITE_ENABLE_STAT4
80363 if( pCtx==0 ) sqlite3ValueFree(pVal);
80364#else
80365 assert( pCtx==0 )((void) (0)); sqlite3ValueFree(pVal);
80366#endif
80367 return SQLITE_NOMEM_BKPT7;
80368}
80369
80370/*
80371** Create a new sqlite3_value object, containing the value of pExpr.
80372**
80373** This only works for very simple expressions that consist of one constant
80374** token (i.e. "5", "5.1", "'a string'"). If the expression can
80375** be converted directly into a value, then the value is allocated and
80376** a pointer written to *ppVal. The caller is responsible for deallocating
80377** the value by passing it to sqlite3ValueFree() later on. If the expression
80378** cannot be converted to a value, then *ppVal is set to NULL.
80379*/
80380SQLITE_PRIVATEstatic int sqlite3ValueFromExpr(
80381 sqlite3 *db, /* The database connection */
80382 const Expr *pExpr, /* The expression to evaluate */
80383 u8 enc, /* Encoding to use */
80384 u8 affinity, /* Affinity to use */
80385 sqlite3_value **ppVal /* Write the new value here */
80386){
80387 return pExpr ? valueFromExpr(db, pExpr, enc, affinity, ppVal, 0) : 0;
80388}
80389
80390#ifdef SQLITE_ENABLE_STAT4
80391/*
80392** Attempt to extract a value from pExpr and use it to construct *ppVal.
80393**
80394** If pAlloc is not NULL, then an UnpackedRecord object is created for
80395** pAlloc if one does not exist and the new value is added to the
80396** UnpackedRecord object.
80397**
80398** A value is extracted in the following cases:
80399**
80400** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
80401**
80402** * The expression is a bound variable, and this is a reprepare, or
80403**
80404** * The expression is a literal value.
80405**
80406** On success, *ppVal is made to point to the extracted value. The caller
80407** is responsible for ensuring that the value is eventually freed.
80408*/
80409static int stat4ValueFromExpr(
80410 Parse *pParse, /* Parse context */
80411 Expr *pExpr, /* The expression to extract a value from */
80412 u8 affinity, /* Affinity to use */
80413 struct ValueNewStat4Ctx *pAlloc,/* How to allocate space. Or NULL */
80414 sqlite3_value **ppVal /* OUT: New value object (or NULL) */
80415){
80416 int rc = SQLITE_OK0;
80417 sqlite3_value *pVal = 0;
80418 sqlite3 *db = pParse->db;
80419
80420 /* Skip over any TK_COLLATE nodes */
80421 pExpr = sqlite3ExprSkipCollate(pExpr);
80422
80423 assert( pExpr==0 || pExpr->op!=TK_REGISTER || pExpr->op2!=TK_VARIABLE )((void) (0));
80424 if( !pExpr ){
80425 pVal = valueNew(db, pAlloc);
80426 if( pVal ){
80427 sqlite3VdbeMemSetNull((Mem*)pVal);
80428 }
80429 }else if( pExpr->op==TK_VARIABLE156 && (db->flags & SQLITE_EnableQPSG0x00800000)==0 ){
80430 Vdbe *v;
80431 int iBindVar = pExpr->iColumn;
80432 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
80433 if( (v = pParse->pReprepare)!=0 ){
80434 pVal = valueNew(db, pAlloc);
80435 if( pVal ){
80436 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
80437 sqlite3ValueApplyAffinity(pVal, affinity, ENC(db)((db)->enc));
80438 pVal->db = pParse->db;
80439 }
80440 }
80441 }else{
80442 rc = valueFromExpr(db, pExpr, ENC(db)((db)->enc), affinity, &pVal, pAlloc);
80443 }
80444
80445 assert( pVal==0 || pVal->db==db )((void) (0));
80446 *ppVal = pVal;
80447 return rc;
80448}
80449
80450/*
80451** This function is used to allocate and populate UnpackedRecord
80452** structures intended to be compared against sample index keys stored
80453** in the sqlite_stat4 table.
80454**
80455** A single call to this function populates zero or more fields of the
80456** record starting with field iVal (fields are numbered from left to
80457** right starting with 0). A single field is populated if:
80458**
80459** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
80460**
80461** * The expression is a bound variable, and this is a reprepare, or
80462**
80463** * The sqlite3ValueFromExpr() function is able to extract a value
80464** from the expression (i.e. the expression is a literal value).
80465**
80466** Or, if pExpr is a TK_VECTOR, one field is populated for each of the
80467** vector components that match either of the two latter criteria listed
80468** above.
80469**
80470** Before any value is appended to the record, the affinity of the
80471** corresponding column within index pIdx is applied to it. Before
80472** this function returns, output parameter *pnExtract is set to the
80473** number of values appended to the record.
80474**
80475** When this function is called, *ppRec must either point to an object
80476** allocated by an earlier call to this function, or must be NULL. If it
80477** is NULL and a value can be successfully extracted, a new UnpackedRecord
80478** is allocated (and *ppRec set to point to it) before returning.
80479**
80480** Unless an error is encountered, SQLITE_OK is returned. It is not an
80481** error if a value cannot be extracted from pExpr. If an error does
80482** occur, an SQLite error code is returned.
80483*/
80484SQLITE_PRIVATEstatic int sqlite3Stat4ProbeSetValue(
80485 Parse *pParse, /* Parse context */
80486 Index *pIdx, /* Index being probed */
80487 UnpackedRecord **ppRec, /* IN/OUT: Probe record */
80488 Expr *pExpr, /* The expression to extract a value from */
80489 int nElem, /* Maximum number of values to append */
80490 int iVal, /* Array element to populate */
80491 int *pnExtract /* OUT: Values appended to the record */
80492){
80493 int rc = SQLITE_OK0;
80494 int nExtract = 0;
80495
80496 if( pExpr==0 || pExpr->op!=TK_SELECT138 ){
80497 int i;
80498 struct ValueNewStat4Ctx alloc;
80499
80500 alloc.pParse = pParse;
80501 alloc.pIdx = pIdx;
80502 alloc.ppRec = ppRec;
80503
80504 for(i=0; i<nElem; i++){
80505 sqlite3_value *pVal = 0;
80506 Expr *pElem = (pExpr ? sqlite3VectorFieldSubexpr(pExpr, i) : 0);
80507 u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
80508 alloc.iVal = iVal+i;
80509 rc = stat4ValueFromExpr(pParse, pElem, aff, &alloc, &pVal);
80510 if( !pVal ) break;
80511 nExtract++;
80512 }
80513 }
80514
80515 *pnExtract = nExtract;
80516 return rc;
80517}
80518
80519/*
80520** Attempt to extract a value from expression pExpr using the methods
80521** as described for sqlite3Stat4ProbeSetValue() above.
80522**
80523** If successful, set *ppVal to point to a new value object and return
80524** SQLITE_OK. If no value can be extracted, but no other error occurs
80525** (e.g. OOM), return SQLITE_OK and set *ppVal to NULL. Or, if an error
80526** does occur, return an SQLite error code. The final value of *ppVal
80527** is undefined in this case.
80528*/
80529SQLITE_PRIVATEstatic int sqlite3Stat4ValueFromExpr(
80530 Parse *pParse, /* Parse context */
80531 Expr *pExpr, /* The expression to extract a value from */
80532 u8 affinity, /* Affinity to use */
80533 sqlite3_value **ppVal /* OUT: New value object (or NULL) */
80534){
80535 return stat4ValueFromExpr(pParse, pExpr, affinity, 0, ppVal);
80536}
80537
80538/*
80539** Extract the iCol-th column from the nRec-byte record in pRec. Write
80540** the column value into *ppVal. If *ppVal is initially NULL then a new
80541** sqlite3_value object is allocated.
80542**
80543** If *ppVal is initially NULL then the caller is responsible for
80544** ensuring that the value written into *ppVal is eventually freed.
80545*/
80546SQLITE_PRIVATEstatic int sqlite3Stat4Column(
80547 sqlite3 *db, /* Database handle */
80548 const void *pRec, /* Pointer to buffer containing record */
80549 int nRec, /* Size of buffer pRec in bytes */
80550 int iCol, /* Column to extract */
80551 sqlite3_value **ppVal /* OUT: Extracted value */
80552){
80553 u32 t = 0; /* a column type code */
80554 int nHdr; /* Size of the header in the record */
80555 int iHdr; /* Next unread header byte */
80556 int iField; /* Next unread data byte */
80557 int szField = 0; /* Size of the current data field */
80558 int i; /* Column index */
80559 u8 *a = (u8*)pRec; /* Typecast byte array */
80560 Mem *pMem = *ppVal; /* Write result into this Mem object */
80561
80562 assert( iCol>0 )((void) (0));
80563 iHdr = getVarint32(a, nHdr)(u8)((*(a)<(u8)0x80)?((nHdr)=(u32)*(a)),1:sqlite3GetVarint32
((a),(u32 *)&(nHdr)))
;
80564 if( nHdr>nRec || iHdr>=nHdr ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(80564);
80565 iField = nHdr;
80566 for(i=0; i<=iCol; i++){
80567 iHdr += getVarint32(&a[iHdr], t)(u8)((*(&a[iHdr])<(u8)0x80)?((t)=(u32)*(&a[iHdr]))
,1:sqlite3GetVarint32((&a[iHdr]),(u32 *)&(t)))
;
80568 testcase( iHdr==nHdr );
80569 testcase( iHdr==nHdr+1 );
80570 if( iHdr>nHdr ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(80570);
80571 szField = sqlite3VdbeSerialTypeLen(t);
80572 iField += szField;
80573 }
80574 testcase( iField==nRec );
80575 testcase( iField==nRec+1 );
80576 if( iField>nRec ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(80576);
80577 if( pMem==0 ){
80578 pMem = *ppVal = sqlite3ValueNew(db);
80579 if( pMem==0 ) return SQLITE_NOMEM_BKPT7;
80580 }
80581 sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
80582 pMem->enc = ENC(db)((db)->enc);
80583 return SQLITE_OK0;
80584}
80585
80586/*
80587** Unless it is NULL, the argument must be an UnpackedRecord object returned
80588** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
80589** the object.
80590*/
80591SQLITE_PRIVATEstatic void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
80592 if( pRec ){
80593 int i;
80594 int nCol = pRec->pKeyInfo->nAllField;
80595 Mem *aMem = pRec->aMem;
80596 sqlite3 *db = aMem[0].db;
80597 for(i=0; i<nCol; i++){
80598 sqlite3VdbeMemRelease(&aMem[i]);
80599 }
80600 sqlite3KeyInfoUnref(pRec->pKeyInfo);
80601 sqlite3DbFreeNN(db, pRec);
80602 }
80603}
80604#endif /* ifdef SQLITE_ENABLE_STAT4 */
80605
80606/*
80607** Change the string value of an sqlite3_value object
80608*/
80609SQLITE_PRIVATEstatic void sqlite3ValueSetStr(
80610 sqlite3_value *v, /* Value to be set */
80611 int n, /* Length of string z */
80612 const void *z, /* Text of the new string */
80613 u8 enc, /* Encoding to use */
80614 void (*xDel)(void*) /* Destructor for the string */
80615){
80616 if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
80617}
80618
80619/*
80620** Free an sqlite3_value object
80621*/
80622SQLITE_PRIVATEstatic void sqlite3ValueFree(sqlite3_value *v){
80623 if( !v ) return;
80624 sqlite3VdbeMemRelease((Mem *)v);
80625 sqlite3DbFreeNN(((Mem*)v)->db, v);
80626}
80627
80628/*
80629** The sqlite3ValueBytes() routine returns the number of bytes in the
80630** sqlite3_value object assuming that it uses the encoding "enc".
80631** The valueBytes() routine is a helper function.
80632*/
80633static SQLITE_NOINLINE__attribute__((noinline)) int valueBytes(sqlite3_value *pVal, u8 enc){
80634 return valueToText(pVal, enc)!=0 ? pVal->n : 0;
80635}
80636SQLITE_PRIVATEstatic int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
80637 Mem *p = (Mem*)pVal;
80638 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 )((void) (0));
80639 if( (p->flags & MEM_Str0x0002)!=0 && pVal->enc==enc ){
80640 return p->n;
80641 }
80642 if( (p->flags & MEM_Blob0x0010)!=0 ){
80643 if( p->flags & MEM_Zero0x0400 ){
80644 return p->n + p->u.nZero;
80645 }else{
80646 return p->n;
80647 }
80648 }
80649 if( p->flags & MEM_Null0x0001 ) return 0;
80650 return valueBytes(pVal, enc);
80651}
80652
80653/************** End of vdbemem.c *********************************************/
80654/************** Begin file vdbeaux.c *****************************************/
80655/*
80656** 2003 September 6
80657**
80658** The author disclaims copyright to this source code. In place of
80659** a legal notice, here is a blessing:
80660**
80661** May you do good and not evil.
80662** May you find forgiveness for yourself and forgive others.
80663** May you share freely, never taking more than you give.
80664**
80665*************************************************************************
80666** This file contains code used for creating, destroying, and populating
80667** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)
80668*/
80669/* #include "sqliteInt.h" */
80670/* #include "vdbeInt.h" */
80671
80672/* Forward references */
80673static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef);
80674static void vdbeFreeOpArray(sqlite3 *, Op *, int);
80675
80676/*
80677** Create a new virtual database engine.
80678*/
80679SQLITE_PRIVATEstatic Vdbe *sqlite3VdbeCreate(Parse *pParse){
80680 sqlite3 *db = pParse->db;
80681 Vdbe *p;
80682 p = sqlite3DbMallocRawNN(db, sizeof(Vdbe) );
80683 if( p==0 ) return 0;
80684 memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp)__builtin_offsetof(Vdbe, aOp));
80685 p->db = db;
80686 if( db->pVdbe ){
80687 db->pVdbe->pPrev = p;
80688 }
80689 p->pNext = db->pVdbe;
80690 p->pPrev = 0;
80691 db->pVdbe = p;
80692 assert( p->eVdbeState==VDBE_INIT_STATE )((void) (0));
80693 p->pParse = pParse;
80694 pParse->pVdbe = p;
80695 assert( pParse->aLabel==0 )((void) (0));
80696 assert( pParse->nLabel==0 )((void) (0));
80697 assert( p->nOpAlloc==0 )((void) (0));
80698 assert( pParse->szOpAlloc==0 )((void) (0));
80699 sqlite3VdbeAddOp2(p, OP_Init64, 0, 1);
80700 return p;
80701}
80702
80703/*
80704** Return the Parse object that owns a Vdbe object.
80705*/
80706SQLITE_PRIVATEstatic Parse *sqlite3VdbeParser(Vdbe *p){
80707 return p->pParse;
80708}
80709
80710/*
80711** Change the error string stored in Vdbe.zErrMsg
80712*/
80713SQLITE_PRIVATEstatic void sqlite3VdbeError(Vdbe *p, const char *zFormat, ...){
80714 va_list ap;
80715 sqlite3DbFree(p->db, p->zErrMsg);
80716 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
80717 p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
80718 va_end(ap)__builtin_va_end(ap);
80719}
80720
80721/*
80722** Remember the SQL string for a prepared statement.
80723*/
80724SQLITE_PRIVATEstatic void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, u8 prepFlags){
80725 if( p==0 ) return;
80726 p->prepFlags = prepFlags;
80727 if( (prepFlags & SQLITE_PREPARE_SAVESQL0x80)==0 ){
80728 p->expmask = 0;
80729 }
80730 assert( p->zSql==0 )((void) (0));
80731 p->zSql = sqlite3DbStrNDup(p->db, z, n);
80732}
80733
80734#ifdef SQLITE_ENABLE_NORMALIZE
80735/*
80736** Add a new element to the Vdbe->pDblStr list.
80737*/
80738SQLITE_PRIVATEstatic void sqlite3VdbeAddDblquoteStr(sqlite3 *db, Vdbe *p, const char *z){
80739 if( p ){
80740 int n = sqlite3Strlen30(z);
80741 DblquoteStr *pStr = sqlite3DbMallocRawNN(db,
80742 sizeof(*pStr)+n+1-sizeof(pStr->z));
80743 if( pStr ){
80744 pStr->pNextStr = p->pDblStr;
80745 p->pDblStr = pStr;
80746 memcpy(pStr->z, z, n+1);
80747 }
80748 }
80749}
80750#endif
80751
80752#ifdef SQLITE_ENABLE_NORMALIZE
80753/*
80754** zId of length nId is a double-quoted identifier. Check to see if
80755** that identifier is really used as a string literal.
80756*/
80757SQLITE_PRIVATEstatic int sqlite3VdbeUsesDoubleQuotedString(
80758 Vdbe *pVdbe, /* The prepared statement */
80759 const char *zId /* The double-quoted identifier, already dequoted */
80760){
80761 DblquoteStr *pStr;
80762 assert( zId!=0 )((void) (0));
80763 if( pVdbe->pDblStr==0 ) return 0;
80764 for(pStr=pVdbe->pDblStr; pStr; pStr=pStr->pNextStr){
80765 if( strcmp(zId, pStr->z)==0 ) return 1;
80766 }
80767 return 0;
80768}
80769#endif
80770
80771/*
80772** Swap all content between two VDBE structures.
80773*/
80774SQLITE_PRIVATEstatic void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
80775 Vdbe tmp, *pTmp;
80776 char *zTmp;
80777 assert( pA->db==pB->db )((void) (0));
80778 tmp = *pA;
80779 *pA = *pB;
80780 *pB = tmp;
80781 pTmp = pA->pNext;
80782 pA->pNext = pB->pNext;
80783 pB->pNext = pTmp;
80784 pTmp = pA->pPrev;
80785 pA->pPrev = pB->pPrev;
80786 pB->pPrev = pTmp;
80787 zTmp = pA->zSql;
80788 pA->zSql = pB->zSql;
80789 pB->zSql = zTmp;
80790#ifdef SQLITE_ENABLE_NORMALIZE
80791 zTmp = pA->zNormSql;
80792 pA->zNormSql = pB->zNormSql;
80793 pB->zNormSql = zTmp;
80794#endif
80795 pB->expmask = pA->expmask;
80796 pB->prepFlags = pA->prepFlags;
80797 memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter));
80798 pB->aCounter[SQLITE_STMTSTATUS_REPREPARE5]++;
80799}
80800
80801/*
80802** Resize the Vdbe.aOp array so that it is at least nOp elements larger
80803** than its current size. nOp is guaranteed to be less than or equal
80804** to 1024/sizeof(Op).
80805**
80806** If an out-of-memory error occurs while resizing the array, return
80807** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain
80808** unchanged (this is so that any opcodes already allocated can be
80809** correctly deallocated along with the rest of the Vdbe).
80810*/
80811static int growOpArray(Vdbe *v, int nOp){
80812 VdbeOp *pNew;
80813 Parse *p = v->pParse;
80814
80815 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
80816 ** more frequent reallocs and hence provide more opportunities for
80817 ** simulated OOM faults. SQLITE_TEST_REALLOC_STRESS is generally used
80818 ** during testing only. With SQLITE_TEST_REALLOC_STRESS grow the op array
80819 ** by the minimum* amount required until the size reaches 512. Normal
80820 ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current
80821 ** size of the op array or add 1KB of space, whichever is smaller. */
80822#ifdef SQLITE_TEST_REALLOC_STRESS
80823 sqlite3_int64 nNew = (v->nOpAlloc>=512 ? 2*(sqlite3_int64)v->nOpAlloc
80824 : (sqlite3_int64)v->nOpAlloc+nOp);
80825#else
80826 sqlite3_int64 nNew = (v->nOpAlloc ? 2*(sqlite3_int64)v->nOpAlloc
80827 : (sqlite3_int64)(1024/sizeof(Op)));
80828 UNUSED_PARAMETER(nOp)(void)(nOp);
80829#endif
80830
80831 /* Ensure that the size of a VDBE does not grow too large */
80832 if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP5] ){
80833 sqlite3OomFault(p->db);
80834 return SQLITE_NOMEM7;
80835 }
80836
80837 assert( nOp<=(int)(1024/sizeof(Op)) )((void) (0));
80838 assert( nNew>=(v->nOpAlloc+nOp) )((void) (0));
80839 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
80840 if( pNew ){
80841 p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
80842 v->nOpAlloc = p->szOpAlloc/sizeof(Op);
80843 v->aOp = pNew;
80844 }
80845 return (pNew ? SQLITE_OK0 : SQLITE_NOMEM_BKPT7);
80846}
80847
80848#ifdef SQLITE_DEBUG
80849/* This routine is just a convenient place to set a breakpoint that will
80850** fire after each opcode is inserted and displayed using
80851** "PRAGMA vdbe_addoptrace=on". Parameters "pc" (program counter) and
80852** pOp are available to make the breakpoint conditional.
80853**
80854** Other useful labels for breakpoints include:
80855** test_trace_breakpoint(pc,pOp)
80856** sqlite3CorruptError(lineno)
80857** sqlite3MisuseError(lineno)
80858** sqlite3CantopenError(lineno)
80859*/
80860static void test_addop_breakpoint(int pc, Op *pOp){
80861 static int n = 0;
80862 n++;
80863}
80864#endif
80865
80866/*
80867** Add a new instruction to the list of instructions current in the
80868** VDBE. Return the address of the new instruction.
80869**
80870** Parameters:
80871**
80872** p Pointer to the VDBE
80873**
80874** op The opcode for this instruction
80875**
80876** p1, p2, p3 Operands
80877**
80878** Use the sqlite3VdbeResolveLabel() function to fix an address and
80879** the sqlite3VdbeChangeP4() function to change the value of the P4
80880** operand.
80881*/
80882static SQLITE_NOINLINE__attribute__((noinline)) int growOp3(Vdbe *p, int op, int p1, int p2, int p3){
80883 assert( p->nOpAlloc<=p->nOp )((void) (0));
80884 if( growOpArray(p, 1) ) return 1;
80885 assert( p->nOpAlloc>p->nOp )((void) (0));
80886 return sqlite3VdbeAddOp3(p, op, p1, p2, p3);
80887}
80888SQLITE_PRIVATEstatic int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
80889 int i;
80890 VdbeOp *pOp;
80891
80892 i = p->nOp;
80893 assert( p->eVdbeState==VDBE_INIT_STATE )((void) (0));
80894 assert( op>=0 && op<0xff )((void) (0));
80895 if( p->nOpAlloc<=i ){
80896 return growOp3(p, op, p1, p2, p3);
80897 }
80898 assert( p->aOp!=0 )((void) (0));
80899 p->nOp++;
80900 pOp = &p->aOp[i];
80901 assert( pOp!=0 )((void) (0));
80902 pOp->opcode = (u8)op;
80903 pOp->p5 = 0;
80904 pOp->p1 = p1;
80905 pOp->p2 = p2;
80906 pOp->p3 = p3;
80907 pOp->p4.p = 0;
80908 pOp->p4type = P4_NOTUSED0;
80909#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
80910 pOp->zComment = 0;
80911#endif
80912#ifdef SQLITE_DEBUG
80913 if( p->db->flags & SQLITE_VdbeAddopTrace ){
80914 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
80915 test_addop_breakpoint(i, &p->aOp[i]);
80916 }
80917#endif
80918#ifdef VDBE_PROFILE
80919 pOp->cycles = 0;
80920 pOp->cnt = 0;
80921#endif
80922#ifdef SQLITE_VDBE_COVERAGE
80923 pOp->iSrcLine = 0;
80924#endif
80925 return i;
80926}
80927SQLITE_PRIVATEstatic int sqlite3VdbeAddOp0(Vdbe *p, int op){
80928 return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
80929}
80930SQLITE_PRIVATEstatic int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
80931 return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
80932}
80933SQLITE_PRIVATEstatic int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
80934 return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
80935}
80936
80937/* Generate code for an unconditional jump to instruction iDest
80938*/
80939SQLITE_PRIVATEstatic int sqlite3VdbeGoto(Vdbe *p, int iDest){
80940 return sqlite3VdbeAddOp3(p, OP_Goto8, 0, iDest, 0);
80941}
80942
80943/* Generate code to cause the string zStr to be loaded into
80944** register iDest
80945*/
80946SQLITE_PRIVATEstatic int sqlite3VdbeLoadString(Vdbe *p, int iDest, const char *zStr){
80947 return sqlite3VdbeAddOp4(p, OP_String8117, 0, iDest, 0, zStr, 0);
80948}
80949
80950/*
80951** Generate code that initializes multiple registers to string or integer
80952** constants. The registers begin with iDest and increase consecutively.
80953** One register is initialized for each characgter in zTypes[]. For each
80954** "s" character in zTypes[], the register is a string if the argument is
80955** not NULL, or OP_Null if the value is a null pointer. For each "i" character
80956** in zTypes[], the register is initialized to an integer.
80957**
80958** If the input string does not end with "X" then an OP_ResultRow instruction
80959** is generated for the values inserted.
80960*/
80961SQLITE_PRIVATEstatic void sqlite3VdbeMultiLoad(Vdbe *p, int iDest, const char *zTypes, ...){
80962 va_list ap;
80963 int i;
80964 char c;
80965 va_start(ap, zTypes)__builtin_va_start(ap, zTypes);
80966 for(i=0; (c = zTypes[i])!=0; i++){
80967 if( c=='s' ){
80968 const char *z = va_arg(ap, const char*)__builtin_va_arg(ap, const char*);
80969 sqlite3VdbeAddOp4(p, z==0 ? OP_Null75 : OP_String8117, 0, iDest+i, 0, z, 0);
80970 }else if( c=='i' ){
80971 sqlite3VdbeAddOp2(p, OP_Integer71, va_arg(ap, int)__builtin_va_arg(ap, int), iDest+i);
80972 }else{
80973 goto skip_op_resultrow;
80974 }
80975 }
80976 sqlite3VdbeAddOp2(p, OP_ResultRow84, iDest, i);
80977skip_op_resultrow:
80978 va_end(ap)__builtin_va_end(ap);
80979}
80980
80981/*
80982** Add an opcode that includes the p4 value as a pointer.
80983*/
80984SQLITE_PRIVATEstatic int sqlite3VdbeAddOp4(
80985 Vdbe *p, /* Add the opcode to this VM */
80986 int op, /* The new opcode */
80987 int p1, /* The P1 operand */
80988 int p2, /* The P2 operand */
80989 int p3, /* The P3 operand */
80990 const char *zP4, /* The P4 operand */
80991 int p4type /* P4 operand type */
80992){
80993 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
80994 sqlite3VdbeChangeP4(p, addr, zP4, p4type);
80995 return addr;
80996}
80997
80998/*
80999** Add an OP_Function or OP_PureFunc opcode.
81000**
81001** The eCallCtx argument is information (typically taken from Expr.op2)
81002** that describes the calling context of the function. 0 means a general
81003** function call. NC_IsCheck means called by a check constraint,
81004** NC_IdxExpr means called as part of an index expression. NC_PartIdx
81005** means in the WHERE clause of a partial index. NC_GenCol means called
81006** while computing a generated column value. 0 is the usual case.
81007*/
81008SQLITE_PRIVATEstatic int sqlite3VdbeAddFunctionCall(
81009 Parse *pParse, /* Parsing context */
81010 int p1, /* Constant argument mask */
81011 int p2, /* First argument register */
81012 int p3, /* Register into which results are written */
81013 int nArg, /* Number of argument */
81014 const FuncDef *pFunc, /* The function to be invoked */
81015 int eCallCtx /* Calling context */
81016){
81017 Vdbe *v = pParse->pVdbe;
81018 int nByte;
81019 int addr;
81020 sqlite3_context *pCtx;
81021 assert( v )((void) (0));
81022 nByte = sizeof(*pCtx) + (nArg-1)*sizeof(sqlite3_value*);
81023 pCtx = sqlite3DbMallocRawNN(pParse->db, nByte);
81024 if( pCtx==0 ){
81025 assert( pParse->db->mallocFailed )((void) (0));
81026 freeEphemeralFunction(pParse->db, (FuncDef*)pFunc);
81027 return 0;
81028 }
81029 pCtx->pOut = 0;
81030 pCtx->pFunc = (FuncDef*)pFunc;
81031 pCtx->pVdbe = 0;
81032 pCtx->isError = 0;
81033 pCtx->argc = nArg;
81034 pCtx->iOp = sqlite3VdbeCurrentAddr(v);
81035 addr = sqlite3VdbeAddOp4(v, eCallCtx ? OP_PureFunc65 : OP_Function66,
81036 p1, p2, p3, (char*)pCtx, P4_FUNCCTX(-15));
81037 sqlite3VdbeChangeP5(v, eCallCtx & NC_SelfRef0x00002e);
81038 return addr;
81039}
81040
81041/*
81042** Add an opcode that includes the p4 value with a P4_INT64 or
81043** P4_REAL type.
81044*/
81045SQLITE_PRIVATEstatic int sqlite3VdbeAddOp4Dup8(
81046 Vdbe *p, /* Add the opcode to this VM */
81047 int op, /* The new opcode */
81048 int p1, /* The P1 operand */
81049 int p2, /* The P2 operand */
81050 int p3, /* The P3 operand */
81051 const u8 *zP4, /* The P4 operand */
81052 int p4type /* P4 operand type */
81053){
81054 char *p4copy = sqlite3DbMallocRawNN(sqlite3VdbeDb(p), 8);
81055 if( p4copy ) memcpy(p4copy, zP4, 8);
81056 return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
81057}
81058
81059#ifndef SQLITE_OMIT_EXPLAIN
81060/*
81061** Return the address of the current EXPLAIN QUERY PLAN baseline.
81062** 0 means "none".
81063*/
81064SQLITE_PRIVATEstatic int sqlite3VdbeExplainParent(Parse *pParse){
81065 VdbeOp *pOp;
81066 if( pParse->addrExplain==0 ) return 0;
81067 pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain);
81068 return pOp->p2;
81069}
81070
81071/*
81072** Set a debugger breakpoint on the following routine in order to
81073** monitor the EXPLAIN QUERY PLAN code generation.
81074*/
81075#if defined(SQLITE_DEBUG)
81076SQLITE_PRIVATEstatic void sqlite3ExplainBreakpoint(const char *z1, const char *z2){
81077 (void)z1;
81078 (void)z2;
81079}
81080#endif
81081
81082/*
81083** Add a new OP_Explain opcode.
81084**
81085** If the bPush flag is true, then make this opcode the parent for
81086** subsequent Explains until sqlite3VdbeExplainPop() is called.
81087*/
81088SQLITE_PRIVATEstatic void sqlite3VdbeExplain(Parse *pParse, u8 bPush, const char *zFmt, ...){
81089#ifndef SQLITE_DEBUG
81090 /* Always include the OP_Explain opcodes if SQLITE_DEBUG is defined.
81091 ** But omit them (for performance) during production builds */
81092 if( pParse->explain==2 )
81093#endif
81094 {
81095 char *zMsg;
81096 Vdbe *v;
81097 va_list ap;
81098 int iThis;
81099 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
81100 zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap);
81101 va_end(ap)__builtin_va_end(ap);
81102 v = pParse->pVdbe;
81103 iThis = v->nOp;
81104 sqlite3VdbeAddOp4(v, OP_Explain185, iThis, pParse->addrExplain, 0,
81105 zMsg, P4_DYNAMIC(-6));
81106 sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetOp(v,-1)->p4.z);
81107 if( bPush){
81108 pParse->addrExplain = iThis;
81109 }
81110 }
81111}
81112
81113/*
81114** Pop the EXPLAIN QUERY PLAN stack one level.
81115*/
81116SQLITE_PRIVATEstatic void sqlite3VdbeExplainPop(Parse *pParse){
81117 sqlite3ExplainBreakpoint("POP", 0);
81118 pParse->addrExplain = sqlite3VdbeExplainParent(pParse);
81119}
81120#endif /* SQLITE_OMIT_EXPLAIN */
81121
81122/*
81123** Add an OP_ParseSchema opcode. This routine is broken out from
81124** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
81125** as having been used.
81126**
81127** The zWhere string must have been obtained from sqlite3_malloc().
81128** This routine will take ownership of the allocated memory.
81129*/
81130SQLITE_PRIVATEstatic void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere, u16 p5){
81131 int j;
81132 sqlite3VdbeAddOp4(p, OP_ParseSchema149, iDb, 0, 0, zWhere, P4_DYNAMIC(-6));
81133 sqlite3VdbeChangeP5(p, p5);
81134 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
81135 sqlite3MayAbort(p->pParse);
81136}
81137
81138/*
81139** Add an opcode that includes the p4 value as an integer.
81140*/
81141SQLITE_PRIVATEstatic int sqlite3VdbeAddOp4Int(
81142 Vdbe *p, /* Add the opcode to this VM */
81143 int op, /* The new opcode */
81144 int p1, /* The P1 operand */
81145 int p2, /* The P2 operand */
81146 int p3, /* The P3 operand */
81147 int p4 /* The P4 operand as an integer */
81148){
81149 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
81150 if( p->db->mallocFailed==0 ){
81151 VdbeOp *pOp = &p->aOp[addr];
81152 pOp->p4type = P4_INT32(-3);
81153 pOp->p4.i = p4;
81154 }
81155 return addr;
81156}
81157
81158/* Insert the end of a co-routine
81159*/
81160SQLITE_PRIVATEstatic void sqlite3VdbeEndCoroutine(Vdbe *v, int regYield){
81161 sqlite3VdbeAddOp1(v, OP_EndCoroutine68, regYield);
81162
81163 /* Clear the temporary register cache, thereby ensuring that each
81164 ** co-routine has its own independent set of registers, because co-routines
81165 ** might expect their registers to be preserved across an OP_Yield, and
81166 ** that could cause problems if two or more co-routines are using the same
81167 ** temporary register.
81168 */
81169 v->pParse->nTempReg = 0;
81170 v->pParse->nRangeReg = 0;
81171}
81172
81173/*
81174** Create a new symbolic label for an instruction that has yet to be
81175** coded. The symbolic label is really just a negative number. The
81176** label can be used as the P2 value of an operation. Later, when
81177** the label is resolved to a specific address, the VDBE will scan
81178** through its operation list and change all values of P2 which match
81179** the label into the resolved address.
81180**
81181** The VDBE knows that a P2 value is a label because labels are
81182** always negative and P2 values are suppose to be non-negative.
81183** Hence, a negative P2 value is a label that has yet to be resolved.
81184** (Later:) This is only true for opcodes that have the OPFLG_JUMP
81185** property.
81186**
81187** Variable usage notes:
81188**
81189** Parse.aLabel[x] Stores the address that the x-th label resolves
81190** into. For testing (SQLITE_DEBUG), unresolved
81191** labels stores -1, but that is not required.
81192** Parse.nLabelAlloc Number of slots allocated to Parse.aLabel[]
81193** Parse.nLabel The *negative* of the number of labels that have
81194** been issued. The negative is stored because
81195** that gives a performance improvement over storing
81196** the equivalent positive value.
81197*/
81198SQLITE_PRIVATEstatic int sqlite3VdbeMakeLabel(Parse *pParse){
81199 return --pParse->nLabel;
81200}
81201
81202/*
81203** Resolve label "x" to be the address of the next instruction to
81204** be inserted. The parameter "x" must have been obtained from
81205** a prior call to sqlite3VdbeMakeLabel().
81206*/
81207static SQLITE_NOINLINE__attribute__((noinline)) void resizeResolveLabel(Parse *p, Vdbe *v, int j){
81208 int nNewSize = 10 - p->nLabel;
81209 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
81210 nNewSize*sizeof(p->aLabel[0]));
81211 if( p->aLabel==0 ){
81212 p->nLabelAlloc = 0;
81213 }else{
81214#ifdef SQLITE_DEBUG
81215 int i;
81216 for(i=p->nLabelAlloc; i<nNewSize; i++) p->aLabel[i] = -1;
81217#endif
81218 p->nLabelAlloc = nNewSize;
81219 p->aLabel[j] = v->nOp;
81220 }
81221}
81222SQLITE_PRIVATEstatic void sqlite3VdbeResolveLabel(Vdbe *v, int x){
81223 Parse *p = v->pParse;
81224 int j = ADDR(x)(~(x));
81225 assert( v->eVdbeState==VDBE_INIT_STATE )((void) (0));
81226 assert( j<-p->nLabel )((void) (0));
81227 assert( j>=0 )((void) (0));
81228#ifdef SQLITE_DEBUG
81229 if( p->db->flags & SQLITE_VdbeAddopTrace ){
81230 printf("RESOLVE LABEL %d to %d\n", x, v->nOp);
81231 }
81232#endif
81233 if( p->nLabelAlloc + p->nLabel < 0 ){
81234 resizeResolveLabel(p,v,j);
81235 }else{
81236 assert( p->aLabel[j]==(-1) )((void) (0)); /* Labels may only be resolved once */
81237 p->aLabel[j] = v->nOp;
81238 }
81239}
81240
81241/*
81242** Mark the VDBE as one that can only be run one time.
81243*/
81244SQLITE_PRIVATEstatic void sqlite3VdbeRunOnlyOnce(Vdbe *p){
81245 sqlite3VdbeAddOp2(p, OP_Expire166, 1, 1);
81246}
81247
81248/*
81249** Mark the VDBE as one that can be run multiple times.
81250*/
81251SQLITE_PRIVATEstatic void sqlite3VdbeReusable(Vdbe *p){
81252 int i;
81253 for(i=1; ALWAYS(i<p->nOp)(i<p->nOp); i++){
81254 if( ALWAYS(p->aOp[i].opcode==OP_Expire)(p->aOp[i].opcode==166) ){
81255 p->aOp[1].opcode = OP_Noop184;
81256 break;
81257 }
81258 }
81259}
81260
81261#ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
81262
81263/*
81264** The following type and function are used to iterate through all opcodes
81265** in a Vdbe main program and each of the sub-programs (triggers) it may
81266** invoke directly or indirectly. It should be used as follows:
81267**
81268** Op *pOp;
81269** VdbeOpIter sIter;
81270**
81271** memset(&sIter, 0, sizeof(sIter));
81272** sIter.v = v; // v is of type Vdbe*
81273** while( (pOp = opIterNext(&sIter)) ){
81274** // Do something with pOp
81275** }
81276** sqlite3DbFree(v->db, sIter.apSub);
81277**
81278*/
81279typedef struct VdbeOpIter VdbeOpIter;
81280struct VdbeOpIter {
81281 Vdbe *v; /* Vdbe to iterate through the opcodes of */
81282 SubProgram **apSub; /* Array of subprograms */
81283 int nSub; /* Number of entries in apSub */
81284 int iAddr; /* Address of next instruction to return */
81285 int iSub; /* 0 = main program, 1 = first sub-program etc. */
81286};
81287static Op *opIterNext(VdbeOpIter *p){
81288 Vdbe *v = p->v;
81289 Op *pRet = 0;
81290 Op *aOp;
81291 int nOp;
81292
81293 if( p->iSub<=p->nSub ){
81294
81295 if( p->iSub==0 ){
81296 aOp = v->aOp;
81297 nOp = v->nOp;
81298 }else{
81299 aOp = p->apSub[p->iSub-1]->aOp;
81300 nOp = p->apSub[p->iSub-1]->nOp;
81301 }
81302 assert( p->iAddr<nOp )((void) (0));
81303
81304 pRet = &aOp[p->iAddr];
81305 p->iAddr++;
81306 if( p->iAddr==nOp ){
81307 p->iSub++;
81308 p->iAddr = 0;
81309 }
81310
81311 if( pRet->p4type==P4_SUBPROGRAM(-4) ){
81312 int nByte = (p->nSub+1)*sizeof(SubProgram*);
81313 int j;
81314 for(j=0; j<p->nSub; j++){
81315 if( p->apSub[j]==pRet->p4.pProgram ) break;
81316 }
81317 if( j==p->nSub ){
81318 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
81319 if( !p->apSub ){
81320 pRet = 0;
81321 }else{
81322 p->apSub[p->nSub++] = pRet->p4.pProgram;
81323 }
81324 }
81325 }
81326 }
81327
81328 return pRet;
81329}
81330
81331/*
81332** Check if the program stored in the VM associated with pParse may
81333** throw an ABORT exception (causing the statement, but not entire transaction
81334** to be rolled back). This condition is true if the main program or any
81335** sub-programs contains any of the following:
81336**
81337** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
81338** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
81339** * OP_Destroy
81340** * OP_VUpdate
81341** * OP_VCreate
81342** * OP_VRename
81343** * OP_FkCounter with P2==0 (immediate foreign key constraint)
81344** * OP_CreateBtree/BTREE_INTKEY and OP_InitCoroutine
81345** (for CREATE TABLE AS SELECT ...)
81346**
81347** Then check that the value of Parse.mayAbort is true if an
81348** ABORT may be thrown, or false otherwise. Return true if it does
81349** match, or false otherwise. This function is intended to be used as
81350** part of an assert statement in the compiler. Similar to:
81351**
81352** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
81353*/
81354SQLITE_PRIVATEstatic int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
81355 int hasAbort = 0;
81356 int hasFkCounter = 0;
81357 int hasCreateTable = 0;
81358 int hasCreateIndex = 0;
81359 int hasInitCoroutine = 0;
81360 Op *pOp;
81361 VdbeOpIter sIter;
81362
81363 if( v==0 ) return 0;
81364 memset(&sIter, 0, sizeof(sIter));
81365 sIter.v = v;
81366
81367 while( (pOp = opIterNext(&sIter))!=0 ){
81368 int opcode = pOp->opcode;
81369 if( opcode==OP_Destroy144 || opcode==OP_VUpdate7 || opcode==OP_VRename176
81370 || opcode==OP_VDestroy172
81371 || opcode==OP_VCreate171
81372 || opcode==OP_ParseSchema149
81373 || ((opcode==OP_Halt70 || opcode==OP_HaltIfNull69)
81374 && ((pOp->p1)!=SQLITE_OK0 && pOp->p2==OE_Abort2))
81375 ){
81376 hasAbort = 1;
81377 break;
81378 }
81379 if( opcode==OP_CreateBtree147 && pOp->p3==BTREE_INTKEY1 ) hasCreateTable = 1;
81380 if( mayAbort ){
81381 /* hasCreateIndex may also be set for some DELETE statements that use
81382 ** OP_Clear. So this routine may end up returning true in the case
81383 ** where a "DELETE FROM tbl" has a statement-journal but does not
81384 ** require one. This is not so bad - it is an inefficiency, not a bug. */
81385 if( opcode==OP_CreateBtree147 && pOp->p3==BTREE_BLOBKEY2 ) hasCreateIndex = 1;
81386 if( opcode==OP_Clear145 ) hasCreateIndex = 1;
81387 }
81388 if( opcode==OP_InitCoroutine10 ) hasInitCoroutine = 1;
81389#ifndef SQLITE_OMIT_FOREIGN_KEY
81390 if( opcode==OP_FkCounter158 && pOp->p1==0 && pOp->p2==1 ){
81391 hasFkCounter = 1;
81392 }
81393#endif
81394 }
81395 sqlite3DbFree(v->db, sIter.apSub);
81396
81397 /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
81398 ** If malloc failed, then the while() loop above may not have iterated
81399 ** through all opcodes and hasAbort may be set incorrectly. Return
81400 ** true for this case to prevent the assert() in the callers frame
81401 ** from failing. */
81402 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
81403 || (hasCreateTable && hasInitCoroutine) || hasCreateIndex
81404 );
81405}
81406#endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
81407
81408#ifdef SQLITE_DEBUG
81409/*
81410** Increment the nWrite counter in the VDBE if the cursor is not an
81411** ephemeral cursor, or if the cursor argument is NULL.
81412*/
81413SQLITE_PRIVATEstatic void sqlite3VdbeIncrWriteCounter(Vdbe *p, VdbeCursor *pC){
81414 if( pC==0
81415 || (pC->eCurType!=CURTYPE_SORTER1
81416 && pC->eCurType!=CURTYPE_PSEUDO3
81417 && !pC->isEphemeral)
81418 ){
81419 p->nWrite++;
81420 }
81421}
81422#endif
81423
81424#ifdef SQLITE_DEBUG
81425/*
81426** Assert if an Abort at this point in time might result in a corrupt
81427** database.
81428*/
81429SQLITE_PRIVATEstatic void sqlite3VdbeAssertAbortable(Vdbe *p){
81430 assert( p->nWrite==0 || p->usesStmtJournal )((void) (0));
81431}
81432#endif
81433
81434/*
81435** This routine is called after all opcodes have been inserted. It loops
81436** through all the opcodes and fixes up some details.
81437**
81438** (1) For each jump instruction with a negative P2 value (a label)
81439** resolve the P2 value to an actual address.
81440**
81441** (2) Compute the maximum number of arguments used by any SQL function
81442** and store that value in *pMaxFuncArgs.
81443**
81444** (3) Update the Vdbe.readOnly and Vdbe.bIsReader flags to accurately
81445** indicate what the prepared statement actually does.
81446**
81447** (4) (discontinued)
81448**
81449** (5) Reclaim the memory allocated for storing labels.
81450**
81451** This routine will only function correctly if the mkopcodeh.tcl generator
81452** script numbers the opcodes correctly. Changes to this routine must be
81453** coordinated with changes to mkopcodeh.tcl.
81454*/
81455static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
81456 int nMaxArgs = *pMaxFuncArgs;
81457 Op *pOp;
81458 Parse *pParse = p->pParse;
81459 int *aLabel = pParse->aLabel;
81460 p->readOnly = 1;
81461 p->bIsReader = 0;
81462 pOp = &p->aOp[p->nOp-1];
81463 while(1){
81464
81465 /* Only JUMP opcodes and the short list of special opcodes in the switch
81466 ** below need to be considered. The mkopcodeh.tcl generator script groups
81467 ** all these opcodes together near the front of the opcode list. Skip
81468 ** any opcode that does not need processing by virtual of the fact that
81469 ** it is larger than SQLITE_MX_JUMP_OPCODE, as a performance optimization.
81470 */
81471 if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE64 ){
81472 /* NOTE: Be sure to update mkopcodeh.tcl when adding or removing
81473 ** cases from this switch! */
81474 switch( pOp->opcode ){
81475 case OP_Transaction2: {
81476 if( pOp->p2!=0 ) p->readOnly = 0;
81477 /* no break */ deliberate_fall_through
81478 }
81479 case OP_AutoCommit1:
81480 case OP_Savepoint0: {
81481 p->bIsReader = 1;
81482 break;
81483 }
81484#ifndef SQLITE_OMIT_WAL
81485 case OP_Checkpoint3:
81486#endif
81487 case OP_Vacuum5:
81488 case OP_JournalMode4: {
81489 p->readOnly = 0;
81490 p->bIsReader = 1;
81491 break;
81492 }
81493#ifndef SQLITE_OMIT_VIRTUALTABLE
81494 case OP_VUpdate7: {
81495 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
81496 break;
81497 }
81498 case OP_VFilter6: {
81499 int n;
81500 assert( (pOp - p->aOp) >= 3 )((void) (0));
81501 assert( pOp[-1].opcode==OP_Integer )((void) (0));
81502 n = pOp[-1].p1;
81503 if( n>nMaxArgs ) nMaxArgs = n;
81504 /* Fall through into the default case */
81505 /* no break */ deliberate_fall_through
81506 }
81507#endif
81508 default: {
81509 if( pOp->p2<0 ){
81510 /* The mkopcodeh.tcl script has so arranged things that the only
81511 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
81512 ** have non-negative values for P2. */
81513 assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 )((void) (0));
81514 assert( ADDR(pOp->p2)<-pParse->nLabel )((void) (0));
81515 pOp->p2 = aLabel[ADDR(pOp->p2)(~(pOp->p2))];
81516 }
81517 break;
81518 }
81519 }
81520 /* The mkopcodeh.tcl script has so arranged things that the only
81521 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
81522 ** have non-negative values for P2. */
81523 assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0)((void) (0));
81524 }
81525 if( pOp==p->aOp ) break;
81526 pOp--;
81527 }
81528 if( aLabel ){
81529 sqlite3DbFreeNN(p->db, pParse->aLabel);
81530 pParse->aLabel = 0;
81531 }
81532 pParse->nLabel = 0;
81533 *pMaxFuncArgs = nMaxArgs;
81534 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) )((void) (0));
81535}
81536
81537#ifdef SQLITE_DEBUG
81538/*
81539** Check to see if a subroutine contains a jump to a location outside of
81540** the subroutine. If a jump outside the subroutine is detected, add code
81541** that will cause the program to halt with an error message.
81542**
81543** The subroutine consists of opcodes between iFirst and iLast. Jumps to
81544** locations within the subroutine are acceptable. iRetReg is a register
81545** that contains the return address. Jumps to outside the range of iFirst
81546** through iLast are also acceptable as long as the jump destination is
81547** an OP_Return to iReturnAddr.
81548**
81549** A jump to an unresolved label means that the jump destination will be
81550** beyond the current address. That is normally a jump to an early
81551** termination and is consider acceptable.
81552**
81553** This routine only runs during debug builds. The purpose is (of course)
81554** to detect invalid escapes out of a subroutine. The OP_Halt opcode
81555** is generated rather than an assert() or other error, so that ".eqp full"
81556** will still work to show the original bytecode, to aid in debugging.
81557*/
81558SQLITE_PRIVATEstatic void sqlite3VdbeNoJumpsOutsideSubrtn(
81559 Vdbe *v, /* The byte-code program under construction */
81560 int iFirst, /* First opcode of the subroutine */
81561 int iLast, /* Last opcode of the subroutine */
81562 int iRetReg /* Subroutine return address register */
81563){
81564 VdbeOp *pOp;
81565 Parse *pParse;
81566 int i;
81567 sqlite3_str *pErr = 0;
81568 assert( v!=0 )((void) (0));
81569 pParse = v->pParse;
81570 assert( pParse!=0 )((void) (0));
81571 if( pParse->nErr ) return;
81572 assert( iLast>=iFirst )((void) (0));
81573 assert( iLast<v->nOp )((void) (0));
81574 pOp = &v->aOp[iFirst];
81575 for(i=iFirst; i<=iLast; i++, pOp++){
81576 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP0x01)!=0 ){
81577 int iDest = pOp->p2; /* Jump destination */
81578 if( iDest==0 ) continue;
81579 if( pOp->opcode==OP_Gosub9 ) continue;
81580 if( iDest<0 ){
81581 int j = ADDR(iDest)(~(iDest));
81582 assert( j>=0 )((void) (0));
81583 if( j>=-pParse->nLabel || pParse->aLabel[j]<0 ){
81584 continue;
81585 }
81586 iDest = pParse->aLabel[j];
81587 }
81588 if( iDest<iFirst || iDest>iLast ){
81589 int j = iDest;
81590 for(; j<v->nOp; j++){
81591 VdbeOp *pX = &v->aOp[j];
81592 if( pX->opcode==OP_Return67 ){
81593 if( pX->p1==iRetReg ) break;
81594 continue;
81595 }
81596 if( pX->opcode==OP_Noop184 ) continue;
81597 if( pX->opcode==OP_Explain185 ) continue;
81598 if( pErr==0 ){
81599 pErr = sqlite3_str_new(0);
81600 }else{
81601 sqlite3_str_appendchar(pErr, 1, '\n');
81602 }
81603 sqlite3_str_appendf(pErr,
81604 "Opcode at %d jumps to %d which is outside the "
81605 "subroutine at %d..%d",
81606 i, iDest, iFirst, iLast);
81607 break;
81608 }
81609 }
81610 }
81611 }
81612 if( pErr ){
81613 char *zErr = sqlite3_str_finish(pErr);
81614 sqlite3VdbeAddOp4(v, OP_Halt70, SQLITE_INTERNAL2, OE_Abort2, 0, zErr, 0);
81615 sqlite3_free(zErr);
81616 sqlite3MayAbort(pParse);
81617 }
81618}
81619#endif /* SQLITE_DEBUG */
81620
81621/*
81622** Return the address of the next instruction to be inserted.
81623*/
81624SQLITE_PRIVATEstatic int sqlite3VdbeCurrentAddr(Vdbe *p){
81625 assert( p->eVdbeState==VDBE_INIT_STATE )((void) (0));
81626 return p->nOp;
81627}
81628
81629/*
81630** Verify that at least N opcode slots are available in p without
81631** having to malloc for more space (except when compiled using
81632** SQLITE_TEST_REALLOC_STRESS). This interface is used during testing
81633** to verify that certain calls to sqlite3VdbeAddOpList() can never
81634** fail due to a OOM fault and hence that the return value from
81635** sqlite3VdbeAddOpList() will always be non-NULL.
81636*/
81637#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
81638SQLITE_PRIVATEstatic void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N){
81639 assert( p->nOp + N <= p->nOpAlloc )((void) (0));
81640}
81641#endif
81642
81643/*
81644** Verify that the VM passed as the only argument does not contain
81645** an OP_ResultRow opcode. Fail an assert() if it does. This is used
81646** by code in pragma.c to ensure that the implementation of certain
81647** pragmas comports with the flags specified in the mkpragmatab.tcl
81648** script.
81649*/
81650#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
81651SQLITE_PRIVATEstatic void sqlite3VdbeVerifyNoResultRow(Vdbe *p){
81652 int i;
81653 for(i=0; i<p->nOp; i++){
81654 assert( p->aOp[i].opcode!=OP_ResultRow )((void) (0));
81655 }
81656}
81657#endif
81658
81659/*
81660** Generate code (a single OP_Abortable opcode) that will
81661** verify that the VDBE program can safely call Abort in the current
81662** context.
81663*/
81664#if defined(SQLITE_DEBUG)
81665SQLITE_PRIVATEstatic void sqlite3VdbeVerifyAbortable(Vdbe *p, int onError){
81666 if( onError==OE_Abort2 ) sqlite3VdbeAddOp0(p, OP_Abortable186);
81667}
81668#endif
81669
81670/*
81671** This function returns a pointer to the array of opcodes associated with
81672** the Vdbe passed as the first argument. It is the callers responsibility
81673** to arrange for the returned array to be eventually freed using the
81674** vdbeFreeOpArray() function.
81675**
81676** Before returning, *pnOp is set to the number of entries in the returned
81677** array. Also, *pnMaxArg is set to the larger of its current value and
81678** the number of entries in the Vdbe.apArg[] array required to execute the
81679** returned program.
81680*/
81681SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
81682 VdbeOp *aOp = p->aOp;
81683 assert( aOp && !p->db->mallocFailed )((void) (0));
81684
81685 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
81686 assert( DbMaskAllZero(p->btreeMask) )((void) (0));
81687
81688 resolveP2Values(p, pnMaxArg);
81689 *pnOp = p->nOp;
81690 p->aOp = 0;
81691 return aOp;
81692}
81693
81694/*
81695** Add a whole list of operations to the operation stack. Return a
81696** pointer to the first operation inserted.
81697**
81698** Non-zero P2 arguments to jump instructions are automatically adjusted
81699** so that the jump target is relative to the first operation inserted.
81700*/
81701SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeAddOpList(
81702 Vdbe *p, /* Add opcodes to the prepared statement */
81703 int nOp, /* Number of opcodes to add */
81704 VdbeOpList const *aOp, /* The opcodes to be added */
81705 int iLineno /* Source-file line number of first opcode */
81706){
81707 int i;
81708 VdbeOp *pOut, *pFirst;
81709 assert( nOp>0 )((void) (0));
81710 assert( p->eVdbeState==VDBE_INIT_STATE )((void) (0));
81711 if( p->nOp + nOp > p->nOpAlloc && growOpArray(p, nOp) ){
81712 return 0;
81713 }
81714 pFirst = pOut = &p->aOp[p->nOp];
81715 for(i=0; i<nOp; i++, aOp++, pOut++){
81716 pOut->opcode = aOp->opcode;
81717 pOut->p1 = aOp->p1;
81718 pOut->p2 = aOp->p2;
81719 assert( aOp->p2>=0 )((void) (0));
81720 if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP0x01)!=0 && aOp->p2>0 ){
81721 pOut->p2 += p->nOp;
81722 }
81723 pOut->p3 = aOp->p3;
81724 pOut->p4type = P4_NOTUSED0;
81725 pOut->p4.p = 0;
81726 pOut->p5 = 0;
81727#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
81728 pOut->zComment = 0;
81729#endif
81730#ifdef SQLITE_VDBE_COVERAGE
81731 pOut->iSrcLine = iLineno+i;
81732#else
81733 (void)iLineno;
81734#endif
81735#ifdef SQLITE_DEBUG
81736 if( p->db->flags & SQLITE_VdbeAddopTrace ){
81737 sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
81738 }
81739#endif
81740 }
81741 p->nOp += nOp;
81742 return pFirst;
81743}
81744
81745#if defined(SQLITE_ENABLE_STMT_SCANSTATUS1)
81746/*
81747** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus().
81748*/
81749SQLITE_PRIVATEstatic void sqlite3VdbeScanStatus(
81750 Vdbe *p, /* VM to add scanstatus() to */
81751 int addrExplain, /* Address of OP_Explain (or 0) */
81752 int addrLoop, /* Address of loop counter */
81753 int addrVisit, /* Address of rows visited counter */
81754 LogEst nEst, /* Estimated number of output rows */
81755 const char *zName /* Name of table or index being scanned */
81756){
81757 sqlite3_int64 nByte = (p->nScan+1) * sizeof(ScanStatus);
81758 ScanStatus *aNew;
81759 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
81760 if( aNew ){
81761 ScanStatus *pNew = &aNew[p->nScan++];
81762 pNew->addrExplain = addrExplain;
81763 pNew->addrLoop = addrLoop;
81764 pNew->addrVisit = addrVisit;
81765 pNew->nEst = nEst;
81766 pNew->zName = sqlite3DbStrDup(p->db, zName);
81767 p->aScan = aNew;
81768 }
81769}
81770#endif
81771
81772
81773/*
81774** Change the value of the opcode, or P1, P2, P3, or P5 operands
81775** for a specific instruction.
81776*/
81777SQLITE_PRIVATEstatic void sqlite3VdbeChangeOpcode(Vdbe *p, int addr, u8 iNewOpcode){
81778 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
81779}
81780SQLITE_PRIVATEstatic void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
81781 sqlite3VdbeGetOp(p,addr)->p1 = val;
81782}
81783SQLITE_PRIVATEstatic void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
81784 sqlite3VdbeGetOp(p,addr)->p2 = val;
81785}
81786SQLITE_PRIVATEstatic void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
81787 sqlite3VdbeGetOp(p,addr)->p3 = val;
81788}
81789SQLITE_PRIVATEstatic void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
81790 assert( p->nOp>0 || p->db->mallocFailed )((void) (0));
81791 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
81792}
81793
81794/*
81795** Change the P2 operand of instruction addr so that it points to
81796** the address of the next instruction to be coded.
81797*/
81798SQLITE_PRIVATEstatic void sqlite3VdbeJumpHere(Vdbe *p, int addr){
81799 sqlite3VdbeChangeP2(p, addr, p->nOp);
81800}
81801
81802/*
81803** Change the P2 operand of the jump instruction at addr so that
81804** the jump lands on the next opcode. Or if the jump instruction was
81805** the previous opcode (and is thus a no-op) then simply back up
81806** the next instruction counter by one slot so that the jump is
81807** overwritten by the next inserted opcode.
81808**
81809** This routine is an optimization of sqlite3VdbeJumpHere() that
81810** strives to omit useless byte-code like this:
81811**
81812** 7 Once 0 8 0
81813** 8 ...
81814*/
81815SQLITE_PRIVATEstatic void sqlite3VdbeJumpHereOrPopInst(Vdbe *p, int addr){
81816 if( addr==p->nOp-1 ){
81817 assert( p->aOp[addr].opcode==OP_Once((void) (0))
81818 || p->aOp[addr].opcode==OP_If((void) (0))
81819 || p->aOp[addr].opcode==OP_FkIfZero )((void) (0));
81820 assert( p->aOp[addr].p4type==0 )((void) (0));
81821#ifdef SQLITE_VDBE_COVERAGE
81822 sqlite3VdbeGetOp(p,-1)->iSrcLine = 0; /* Erase VdbeCoverage() macros */
81823#endif
81824 p->nOp--;
81825 }else{
81826 sqlite3VdbeChangeP2(p, addr, p->nOp);
81827 }
81828}
81829
81830
81831/*
81832** If the input FuncDef structure is ephemeral, then free it. If
81833** the FuncDef is not ephermal, then do nothing.
81834*/
81835static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
81836 if( (pDef->funcFlags & SQLITE_FUNC_EPHEM0x0010)!=0 ){
81837 sqlite3DbFreeNN(db, pDef);
81838 }
81839}
81840
81841/*
81842** Delete a P4 value if necessary.
81843*/
81844static SQLITE_NOINLINE__attribute__((noinline)) void freeP4Mem(sqlite3 *db, Mem *p){
81845 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
81846 sqlite3DbFreeNN(db, p);
81847}
81848static SQLITE_NOINLINE__attribute__((noinline)) void freeP4FuncCtx(sqlite3 *db, sqlite3_context *p){
81849 freeEphemeralFunction(db, p->pFunc);
81850 sqlite3DbFreeNN(db, p);
81851}
81852static void freeP4(sqlite3 *db, int p4type, void *p4){
81853 assert( db )((void) (0));
81854 switch( p4type ){
81855 case P4_FUNCCTX(-15): {
81856 freeP4FuncCtx(db, (sqlite3_context*)p4);
81857 break;
81858 }
81859 case P4_REAL(-12):
81860 case P4_INT64(-13):
81861 case P4_DYNAMIC(-6):
81862 case P4_INTARRAY(-14): {
81863 sqlite3DbFree(db, p4);
81864 break;
81865 }
81866 case P4_KEYINFO(-8): {
81867 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
81868 break;
81869 }
81870#ifdef SQLITE_ENABLE_CURSOR_HINTS
81871 case P4_EXPR(-9): {
81872 sqlite3ExprDelete(db, (Expr*)p4);
81873 break;
81874 }
81875#endif
81876 case P4_FUNCDEF(-7): {
81877 freeEphemeralFunction(db, (FuncDef*)p4);
81878 break;
81879 }
81880 case P4_MEM(-10): {
81881 if( db->pnBytesFreed==0 ){
81882 sqlite3ValueFree((sqlite3_value*)p4);
81883 }else{
81884 freeP4Mem(db, (Mem*)p4);
81885 }
81886 break;
81887 }
81888 case P4_VTAB(-11) : {
81889 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
81890 break;
81891 }
81892 }
81893}
81894
81895/*
81896** Free the space allocated for aOp and any p4 values allocated for the
81897** opcodes contained within. If aOp is not NULL it is assumed to contain
81898** nOp entries.
81899*/
81900static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
81901 assert( nOp>=0 )((void) (0));
81902 if( aOp ){
81903 Op *pOp = &aOp[nOp-1];
81904 while(1){ /* Exit via break */
81905 if( pOp->p4type <= P4_FREE_IF_LE(-6) ) freeP4(db, pOp->p4type, pOp->p4.p);
81906#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
81907 sqlite3DbFree(db, pOp->zComment);
81908#endif
81909 if( pOp==aOp ) break;
81910 pOp--;
81911 }
81912 sqlite3DbFreeNN(db, aOp);
81913 }
81914}
81915
81916/*
81917** Link the SubProgram object passed as the second argument into the linked
81918** list at Vdbe.pSubProgram. This list is used to delete all sub-program
81919** objects when the VM is no longer required.
81920*/
81921SQLITE_PRIVATEstatic void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
81922 p->pNext = pVdbe->pProgram;
81923 pVdbe->pProgram = p;
81924}
81925
81926/*
81927** Return true if the given Vdbe has any SubPrograms.
81928*/
81929SQLITE_PRIVATEstatic int sqlite3VdbeHasSubProgram(Vdbe *pVdbe){
81930 return pVdbe->pProgram!=0;
81931}
81932
81933/*
81934** Change the opcode at addr into OP_Noop
81935*/
81936SQLITE_PRIVATEstatic int sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
81937 VdbeOp *pOp;
81938 if( p->db->mallocFailed ) return 0;
81939 assert( addr>=0 && addr<p->nOp )((void) (0));
81940 pOp = &p->aOp[addr];
81941 freeP4(p->db, pOp->p4type, pOp->p4.p);
81942 pOp->p4type = P4_NOTUSED0;
81943 pOp->p4.z = 0;
81944 pOp->opcode = OP_Noop184;
81945 return 1;
81946}
81947
81948/*
81949** If the last opcode is "op" and it is not a jump destination,
81950** then remove it. Return true if and only if an opcode was removed.
81951*/
81952SQLITE_PRIVATEstatic int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
81953 if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
81954 return sqlite3VdbeChangeToNoop(p, p->nOp-1);
81955 }else{
81956 return 0;
81957 }
81958}
81959
81960#ifdef SQLITE_DEBUG
81961/*
81962** Generate an OP_ReleaseReg opcode to indicate that a range of
81963** registers, except any identified by mask, are no longer in use.
81964*/
81965SQLITE_PRIVATEstatic void sqlite3VdbeReleaseRegisters(
81966 Parse *pParse, /* Parsing context */
81967 int iFirst, /* Index of first register to be released */
81968 int N, /* Number of registers to release */
81969 u32 mask, /* Mask of registers to NOT release */
81970 int bUndefine /* If true, mark registers as undefined */
81971){
81972 if( N==0 || OptimizationDisabled(pParse->db, SQLITE_ReleaseReg)(((pParse->db)->dbOptFlags&(0x00400000))!=0) ) return;
81973 assert( pParse->pVdbe )((void) (0));
81974 assert( iFirst>=1 )((void) (0));
81975 assert( iFirst+N-1<=pParse->nMem )((void) (0));
81976 if( N<=31 && mask!=0 ){
81977 while( N>0 && (mask&1)!=0 ){
81978 mask >>= 1;
81979 iFirst++;
81980 N--;
81981 }
81982 while( N>0 && N<=32 && (mask & MASKBIT32(N-1)(((unsigned int)1)<<(N-1)))!=0 ){
81983 mask &= ~MASKBIT32(N-1)(((unsigned int)1)<<(N-1));
81984 N--;
81985 }
81986 }
81987 if( N>0 ){
81988 sqlite3VdbeAddOp3(pParse->pVdbe, OP_ReleaseReg183, iFirst, N, *(int*)&mask);
81989 if( bUndefine ) sqlite3VdbeChangeP5(pParse->pVdbe, 1);
81990 }
81991}
81992#endif /* SQLITE_DEBUG */
81993
81994
81995/*
81996** Change the value of the P4 operand for a specific instruction.
81997** This routine is useful when a large program is loaded from a
81998** static array using sqlite3VdbeAddOpList but we want to make a
81999** few minor changes to the program.
82000**
82001** If n>=0 then the P4 operand is dynamic, meaning that a copy of
82002** the string is made into memory obtained from sqlite3_malloc().
82003** A value of n==0 means copy bytes of zP4 up to and including the
82004** first null byte. If n>0 then copy n+1 bytes of zP4.
82005**
82006** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
82007** to a string or structure that is guaranteed to exist for the lifetime of
82008** the Vdbe. In these cases we can just copy the pointer.
82009**
82010** If addr<0 then change P4 on the most recently inserted instruction.
82011*/
82012static void SQLITE_NOINLINE__attribute__((noinline)) vdbeChangeP4Full(
82013 Vdbe *p,
82014 Op *pOp,
82015 const char *zP4,
82016 int n
82017){
82018 if( pOp->p4type ){
82019 freeP4(p->db, pOp->p4type, pOp->p4.p);
82020 pOp->p4type = 0;
82021 pOp->p4.p = 0;
82022 }
82023 if( n<0 ){
82024 sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n);
82025 }else{
82026 if( n==0 ) n = sqlite3Strlen30(zP4);
82027 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
82028 pOp->p4type = P4_DYNAMIC(-6);
82029 }
82030}
82031SQLITE_PRIVATEstatic void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
82032 Op *pOp;
82033 sqlite3 *db;
82034 assert( p!=0 )((void) (0));
82035 db = p->db;
82036 assert( p->eVdbeState==VDBE_INIT_STATE )((void) (0));
82037 assert( p->aOp!=0 || db->mallocFailed )((void) (0));
82038 if( db->mallocFailed ){
82039 if( n!=P4_VTAB(-11) ) freeP4(db, n, (void*)*(char**)&zP4);
82040 return;
82041 }
82042 assert( p->nOp>0 )((void) (0));
82043 assert( addr<p->nOp )((void) (0));
82044 if( addr<0 ){
82045 addr = p->nOp - 1;
82046 }
82047 pOp = &p->aOp[addr];
82048 if( n>=0 || pOp->p4type ){
82049 vdbeChangeP4Full(p, pOp, zP4, n);
82050 return;
82051 }
82052 if( n==P4_INT32(-3) ){
82053 /* Note: this cast is safe, because the origin data point was an int
82054 ** that was cast to a (const char *). */
82055 pOp->p4.i = SQLITE_PTR_TO_INT(zP4)((int)(long int)(zP4));
82056 pOp->p4type = P4_INT32(-3);
82057 }else if( zP4!=0 ){
82058 assert( n<0 )((void) (0));
82059 pOp->p4.p = (void*)zP4;
82060 pOp->p4type = (signed char)n;
82061 if( n==P4_VTAB(-11) ) sqlite3VtabLock((VTable*)zP4);
82062 }
82063}
82064
82065/*
82066** Change the P4 operand of the most recently coded instruction
82067** to the value defined by the arguments. This is a high-speed
82068** version of sqlite3VdbeChangeP4().
82069**
82070** The P4 operand must not have been previously defined. And the new
82071** P4 must not be P4_INT32. Use sqlite3VdbeChangeP4() in either of
82072** those cases.
82073*/
82074SQLITE_PRIVATEstatic void sqlite3VdbeAppendP4(Vdbe *p, void *pP4, int n){
82075 VdbeOp *pOp;
82076 assert( n!=P4_INT32 && n!=P4_VTAB )((void) (0));
82077 assert( n<=0 )((void) (0));
82078 if( p->db->mallocFailed ){
82079 freeP4(p->db, n, pP4);
82080 }else{
82081 assert( pP4!=0 )((void) (0));
82082 assert( p->nOp>0 )((void) (0));
82083 pOp = &p->aOp[p->nOp-1];
82084 assert( pOp->p4type==P4_NOTUSED )((void) (0));
82085 pOp->p4type = n;
82086 pOp->p4.p = pP4;
82087 }
82088}
82089
82090/*
82091** Set the P4 on the most recently added opcode to the KeyInfo for the
82092** index given.
82093*/
82094SQLITE_PRIVATEstatic void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
82095 Vdbe *v = pParse->pVdbe;
82096 KeyInfo *pKeyInfo;
82097 assert( v!=0 )((void) (0));
82098 assert( pIdx!=0 )((void) (0));
82099 pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pIdx);
82100 if( pKeyInfo ) sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO(-8));
82101}
82102
82103#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
82104/*
82105** Change the comment on the most recently coded instruction. Or
82106** insert a No-op and add the comment to that new instruction. This
82107** makes the code easier to read during debugging. None of this happens
82108** in a production build.
82109*/
82110static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
82111 assert( p->nOp>0 || p->aOp==0 )((void) (0));
82112 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->pParse->nErr>0 )((void) (0));
82113 if( p->nOp ){
82114 assert( p->aOp )((void) (0));
82115 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
82116 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
82117 }
82118}
82119SQLITE_PRIVATEstatic void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
82120 va_list ap;
82121 if( p ){
82122 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
82123 vdbeVComment(p, zFormat, ap);
82124 va_end(ap)__builtin_va_end(ap);
82125 }
82126}
82127SQLITE_PRIVATEstatic void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
82128 va_list ap;
82129 if( p ){
82130 sqlite3VdbeAddOp0(p, OP_Noop184);
82131 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
82132 vdbeVComment(p, zFormat, ap);
82133 va_end(ap)__builtin_va_end(ap);
82134 }
82135}
82136#endif /* NDEBUG */
82137
82138#ifdef SQLITE_VDBE_COVERAGE
82139/*
82140** Set the value if the iSrcLine field for the previously coded instruction.
82141*/
82142SQLITE_PRIVATEstatic void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
82143 sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
82144}
82145#endif /* SQLITE_VDBE_COVERAGE */
82146
82147/*
82148** Return the opcode for a given address. If the address is -1, then
82149** return the most recently inserted opcode.
82150**
82151** If a memory allocation error has occurred prior to the calling of this
82152** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
82153** is readable but not writable, though it is cast to a writable value.
82154** The return of a dummy opcode allows the call to continue functioning
82155** after an OOM fault without having to check to see if the return from
82156** this routine is a valid pointer. But because the dummy.opcode is 0,
82157** dummy will never be written to. This is verified by code inspection and
82158** by running with Valgrind.
82159*/
82160SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
82161 /* C89 specifies that the constant "dummy" will be initialized to all
82162 ** zeros, which is correct. MSVC generates a warning, nevertheless. */
82163 static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */
82164 assert( p->eVdbeState==VDBE_INIT_STATE )((void) (0));
82165 if( addr<0 ){
82166 addr = p->nOp - 1;
82167 }
82168 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed )((void) (0));
82169 if( p->db->mallocFailed ){
82170 return (VdbeOp*)&dummy;
82171 }else{
82172 return &p->aOp[addr];
82173 }
82174}
82175
82176#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
82177/*
82178** Return an integer value for one of the parameters to the opcode pOp
82179** determined by character c.
82180*/
82181static int translateP(char c, const Op *pOp){
82182 if( c=='1' ) return pOp->p1;
82183 if( c=='2' ) return pOp->p2;
82184 if( c=='3' ) return pOp->p3;
82185 if( c=='4' ) return pOp->p4.i;
82186 return pOp->p5;
82187}
82188
82189/*
82190** Compute a string for the "comment" field of a VDBE opcode listing.
82191**
82192** The Synopsis: field in comments in the vdbe.c source file gets converted
82193** to an extra string that is appended to the sqlite3OpcodeName(). In the
82194** absence of other comments, this synopsis becomes the comment on the opcode.
82195** Some translation occurs:
82196**
82197** "PX" -> "r[X]"
82198** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
82199** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
82200** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
82201*/
82202SQLITE_PRIVATEstatic char *sqlite3VdbeDisplayComment(
82203 sqlite3 *db, /* Optional - Oom error reporting only */
82204 const Op *pOp, /* The opcode to be commented */
82205 const char *zP4 /* Previously obtained value for P4 */
82206){
82207 const char *zOpName;
82208 const char *zSynopsis;
82209 int nOpName;
82210 int ii;
82211 char zAlt[50];
82212 StrAccum x;
82213
82214 sqlite3StrAccumInit(&x, 0, 0, 0, SQLITE_MAX_LENGTH2147483645);
82215 zOpName = sqlite3OpcodeName(pOp->opcode);
82216 nOpName = sqlite3Strlen30(zOpName);
82217 if( zOpName[nOpName+1] ){
82218 int seenCom = 0;
82219 char c;
82220 zSynopsis = zOpName + nOpName + 1;
82221 if( strncmp(zSynopsis,"IF ",3)==0 ){
82222 sqlite3_snprintf(sizeof(zAlt), zAlt, "if %s goto P2", zSynopsis+3);
82223 zSynopsis = zAlt;
82224 }
82225 for(ii=0; (c = zSynopsis[ii])!=0; ii++){
82226 if( c=='P' ){
82227 c = zSynopsis[++ii];
82228 if( c=='4' ){
82229 sqlite3_str_appendall(&x, zP4);
82230 }else if( c=='X' ){
82231 if( pOp->zComment && pOp->zComment[0] ){
82232 sqlite3_str_appendall(&x, pOp->zComment);
82233 seenCom = 1;
82234 break;
82235 }
82236 }else{
82237 int v1 = translateP(c, pOp);
82238 int v2;
82239 if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){
82240 ii += 3;
82241 v2 = translateP(zSynopsis[ii], pOp);
82242 if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){
82243 ii += 2;
82244 v2++;
82245 }
82246 if( v2<2 ){
82247 sqlite3_str_appendf(&x, "%d", v1);
82248 }else{
82249 sqlite3_str_appendf(&x, "%d..%d", v1, v1+v2-1);
82250 }
82251 }else if( strncmp(zSynopsis+ii+1, "@NP", 3)==0 ){
82252 sqlite3_context *pCtx = pOp->p4.pCtx;
82253 if( pOp->p4type!=P4_FUNCCTX(-15) || pCtx->argc==1 ){
82254 sqlite3_str_appendf(&x, "%d", v1);
82255 }else if( pCtx->argc>1 ){
82256 sqlite3_str_appendf(&x, "%d..%d", v1, v1+pCtx->argc-1);
82257 }else if( x.accError==0 ){
82258 assert( x.nChar>2 )((void) (0));
82259 x.nChar -= 2;
82260 ii++;
82261 }
82262 ii += 3;
82263 }else{
82264 sqlite3_str_appendf(&x, "%d", v1);
82265 if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
82266 ii += 4;
82267 }
82268 }
82269 }
82270 }else{
82271 sqlite3_str_appendchar(&x, 1, c);
82272 }
82273 }
82274 if( !seenCom && pOp->zComment ){
82275 sqlite3_str_appendf(&x, "; %s", pOp->zComment);
82276 }
82277 }else if( pOp->zComment ){
82278 sqlite3_str_appendall(&x, pOp->zComment);
82279 }
82280 if( (x.accError & SQLITE_NOMEM7)!=0 && db!=0 ){
82281 sqlite3OomFault(db);
82282 }
82283 return sqlite3StrAccumFinish(&x);
82284}
82285#endif /* SQLITE_ENABLE_EXPLAIN_COMMENTS */
82286
82287#if VDBE_DISPLAY_P41 && defined(SQLITE_ENABLE_CURSOR_HINTS)
82288/*
82289** Translate the P4.pExpr value for an OP_CursorHint opcode into text
82290** that can be displayed in the P4 column of EXPLAIN output.
82291*/
82292static void displayP4Expr(StrAccum *p, Expr *pExpr){
82293 const char *zOp = 0;
82294 switch( pExpr->op ){
82295 case TK_STRING117:
82296 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
82297 sqlite3_str_appendf(p, "%Q", pExpr->u.zToken);
82298 break;
82299 case TK_INTEGER155:
82300 sqlite3_str_appendf(p, "%d", pExpr->u.iValue);
82301 break;
82302 case TK_NULL121:
82303 sqlite3_str_appendf(p, "NULL");
82304 break;
82305 case TK_REGISTER176: {
82306 sqlite3_str_appendf(p, "r[%d]", pExpr->iTable);
82307 break;
82308 }
82309 case TK_COLUMN167: {
82310 if( pExpr->iColumn<0 ){
82311 sqlite3_str_appendf(p, "rowid");
82312 }else{
82313 sqlite3_str_appendf(p, "c%d", (int)pExpr->iColumn);
82314 }
82315 break;
82316 }
82317 case TK_LT56: zOp = "LT"; break;
82318 case TK_LE55: zOp = "LE"; break;
82319 case TK_GT54: zOp = "GT"; break;
82320 case TK_GE57: zOp = "GE"; break;
82321 case TK_NE52: zOp = "NE"; break;
82322 case TK_EQ53: zOp = "EQ"; break;
82323 case TK_IS45: zOp = "IS"; break;
82324 case TK_ISNOT171: zOp = "ISNOT"; break;
82325 case TK_AND44: zOp = "AND"; break;
82326 case TK_OR43: zOp = "OR"; break;
82327 case TK_PLUS106: zOp = "ADD"; break;
82328 case TK_STAR108: zOp = "MUL"; break;
82329 case TK_MINUS107: zOp = "SUB"; break;
82330 case TK_REM110: zOp = "REM"; break;
82331 case TK_BITAND102: zOp = "BITAND"; break;
82332 case TK_BITOR103: zOp = "BITOR"; break;
82333 case TK_SLASH109: zOp = "DIV"; break;
82334 case TK_LSHIFT104: zOp = "LSHIFT"; break;
82335 case TK_RSHIFT105: zOp = "RSHIFT"; break;
82336 case TK_CONCAT111: zOp = "CONCAT"; break;
82337 case TK_UMINUS173: zOp = "MINUS"; break;
82338 case TK_UPLUS174: zOp = "PLUS"; break;
82339 case TK_BITNOT114: zOp = "BITNOT"; break;
82340 case TK_NOT19: zOp = "NOT"; break;
82341 case TK_ISNULL50: zOp = "ISNULL"; break;
82342 case TK_NOTNULL51: zOp = "NOTNULL"; break;
82343
82344 default:
82345 sqlite3_str_appendf(p, "%s", "expr");
82346 break;
82347 }
82348
82349 if( zOp ){
82350 sqlite3_str_appendf(p, "%s(", zOp);
82351 displayP4Expr(p, pExpr->pLeft);
82352 if( pExpr->pRight ){
82353 sqlite3_str_append(p, ",", 1);
82354 displayP4Expr(p, pExpr->pRight);
82355 }
82356 sqlite3_str_append(p, ")", 1);
82357 }
82358}
82359#endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */
82360
82361
82362#if VDBE_DISPLAY_P41
82363/*
82364** Compute a string that describes the P4 parameter for an opcode.
82365** Use zTemp for any required temporary buffer space.
82366*/
82367SQLITE_PRIVATEstatic char *sqlite3VdbeDisplayP4(sqlite3 *db, Op *pOp){
82368 char *zP4 = 0;
82369 StrAccum x;
82370
82371 sqlite3StrAccumInit(&x, 0, 0, 0, SQLITE_MAX_LENGTH2147483645);
82372 switch( pOp->p4type ){
82373 case P4_KEYINFO(-8): {
82374 int j;
82375 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
82376 assert( pKeyInfo->aSortFlags!=0 )((void) (0));
82377 sqlite3_str_appendf(&x, "k(%d", pKeyInfo->nKeyField);
82378 for(j=0; j<pKeyInfo->nKeyField; j++){
82379 CollSeq *pColl = pKeyInfo->aColl[j];
82380 const char *zColl = pColl ? pColl->zName : "";
82381 if( strcmp(zColl, "BINARY")==0 ) zColl = "B";
82382 sqlite3_str_appendf(&x, ",%s%s%s",
82383 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_DESC0x01) ? "-" : "",
82384 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_BIGNULL0x02)? "N." : "",
82385 zColl);
82386 }
82387 sqlite3_str_append(&x, ")", 1);
82388 break;
82389 }
82390#ifdef SQLITE_ENABLE_CURSOR_HINTS
82391 case P4_EXPR(-9): {
82392 displayP4Expr(&x, pOp->p4.pExpr);
82393 break;
82394 }
82395#endif
82396 case P4_COLLSEQ(-2): {
82397 static const char *const encnames[] = {"?", "8", "16LE", "16BE"};
82398 CollSeq *pColl = pOp->p4.pColl;
82399 assert( pColl->enc<4 )((void) (0));
82400 sqlite3_str_appendf(&x, "%.18s-%s", pColl->zName,
82401 encnames[pColl->enc]);
82402 break;
82403 }
82404 case P4_FUNCDEF(-7): {
82405 FuncDef *pDef = pOp->p4.pFunc;
82406 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
82407 break;
82408 }
82409 case P4_FUNCCTX(-15): {
82410 FuncDef *pDef = pOp->p4.pCtx->pFunc;
82411 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
82412 break;
82413 }
82414 case P4_INT64(-13): {
82415 sqlite3_str_appendf(&x, "%lld", *pOp->p4.pI64);
82416 break;
82417 }
82418 case P4_INT32(-3): {
82419 sqlite3_str_appendf(&x, "%d", pOp->p4.i);
82420 break;
82421 }
82422 case P4_REAL(-12): {
82423 sqlite3_str_appendf(&x, "%.16g", *pOp->p4.pReal);
82424 break;
82425 }
82426 case P4_MEM(-10): {
82427 Mem *pMem = pOp->p4.pMem;
82428 if( pMem->flags & MEM_Str0x0002 ){
82429 zP4 = pMem->z;
82430 }else if( pMem->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
82431 sqlite3_str_appendf(&x, "%lld", pMem->u.i);
82432 }else if( pMem->flags & MEM_Real0x0008 ){
82433 sqlite3_str_appendf(&x, "%.16g", pMem->u.r);
82434 }else if( pMem->flags & MEM_Null0x0001 ){
82435 zP4 = "NULL";
82436 }else{
82437 assert( pMem->flags & MEM_Blob )((void) (0));
82438 zP4 = "(blob)";
82439 }
82440 break;
82441 }
82442#ifndef SQLITE_OMIT_VIRTUALTABLE
82443 case P4_VTAB(-11): {
82444 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
82445 sqlite3_str_appendf(&x, "vtab:%p", pVtab);
82446 break;
82447 }
82448#endif
82449 case P4_INTARRAY(-14): {
82450 u32 i;
82451 u32 *ai = pOp->p4.ai;
82452 u32 n = ai[0]; /* The first element of an INTARRAY is always the
82453 ** count of the number of elements to follow */
82454 for(i=1; i<=n; i++){
82455 sqlite3_str_appendf(&x, "%c%u", (i==1 ? '[' : ','), ai[i]);
82456 }
82457 sqlite3_str_append(&x, "]", 1);
82458 break;
82459 }
82460 case P4_SUBPROGRAM(-4): {
82461 zP4 = "program";
82462 break;
82463 }
82464 case P4_TABLE(-5): {
82465 zP4 = pOp->p4.pTab->zName;
82466 break;
82467 }
82468 default: {
82469 zP4 = pOp->p4.z;
82470 }
82471 }
82472 if( zP4 ) sqlite3_str_appendall(&x, zP4);
82473 if( (x.accError & SQLITE_NOMEM7)!=0 ){
82474 sqlite3OomFault(db);
82475 }
82476 return sqlite3StrAccumFinish(&x);
82477}
82478#endif /* VDBE_DISPLAY_P4 */
82479
82480/*
82481** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
82482**
82483** The prepared statements need to know in advance the complete set of
82484** attached databases that will be use. A mask of these databases
82485** is maintained in p->btreeMask. The p->lockMask value is the subset of
82486** p->btreeMask of databases that will require a lock.
82487*/
82488SQLITE_PRIVATEstatic void sqlite3VdbeUsesBtree(Vdbe *p, int i){
82489 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 )((void) (0));
82490 assert( i<(int)sizeof(p->btreeMask)*8 )((void) (0));
82491 DbMaskSet(p->btreeMask, i)(p->btreeMask)|=(((yDbMask)1)<<(i));
82492 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
82493 DbMaskSet(p->lockMask, i)(p->lockMask)|=(((yDbMask)1)<<(i));
82494 }
82495}
82496
82497#if !defined(SQLITE_OMIT_SHARED_CACHE)
82498/*
82499** If SQLite is compiled to support shared-cache mode and to be threadsafe,
82500** this routine obtains the mutex associated with each BtShared structure
82501** that may be accessed by the VM passed as an argument. In doing so it also
82502** sets the BtShared.db member of each of the BtShared structures, ensuring
82503** that the correct busy-handler callback is invoked if required.
82504**
82505** If SQLite is not threadsafe but does support shared-cache mode, then
82506** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
82507** of all of BtShared structures accessible via the database handle
82508** associated with the VM.
82509**
82510** If SQLite is not threadsafe and does not support shared-cache mode, this
82511** function is a no-op.
82512**
82513** The p->btreeMask field is a bitmask of all btrees that the prepared
82514** statement p will ever use. Let N be the number of bits in p->btreeMask
82515** corresponding to btrees that use shared cache. Then the runtime of
82516** this routine is N*N. But as N is rarely more than 1, this should not
82517** be a problem.
82518*/
82519SQLITE_PRIVATEstatic void sqlite3VdbeEnter(Vdbe *p){
82520 int i;
82521 sqlite3 *db;
82522 Db *aDb;
82523 int nDb;
82524 if( DbMaskAllZero(p->lockMask)(p->lockMask)==0 ) return; /* The common case */
82525 db = p->db;
82526 aDb = db->aDb;
82527 nDb = db->nDb;
82528 for(i=0; i<nDb; i++){
82529 if( i!=1 && DbMaskTest(p->lockMask,i)(((p->lockMask)&(((yDbMask)1)<<(i)))!=0) && ALWAYS(aDb[i].pBt!=0)(aDb[i].pBt!=0) ){
82530 sqlite3BtreeEnter(aDb[i].pBt);
82531 }
82532 }
82533}
82534#endif
82535
82536#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE2>0
82537/*
82538** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
82539*/
82540static SQLITE_NOINLINE__attribute__((noinline)) void vdbeLeave(Vdbe *p){
82541 int i;
82542 sqlite3 *db;
82543 Db *aDb;
82544 int nDb;
82545 db = p->db;
82546 aDb = db->aDb;
82547 nDb = db->nDb;
82548 for(i=0; i<nDb; i++){
82549 if( i!=1 && DbMaskTest(p->lockMask,i)(((p->lockMask)&(((yDbMask)1)<<(i)))!=0) && ALWAYS(aDb[i].pBt!=0)(aDb[i].pBt!=0) ){
82550 sqlite3BtreeLeave(aDb[i].pBt);
82551 }
82552 }
82553}
82554SQLITE_PRIVATEstatic void sqlite3VdbeLeave(Vdbe *p){
82555 if( DbMaskAllZero(p->lockMask)(p->lockMask)==0 ) return; /* The common case */
82556 vdbeLeave(p);
82557}
82558#endif
82559
82560#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
82561/*
82562** Print a single opcode. This routine is used for debugging only.
82563*/
82564SQLITE_PRIVATEstatic void sqlite3VdbePrintOp(FILE *pOut, int pc, VdbeOp *pOp){
82565 char *zP4;
82566 char *zCom;
82567 sqlite3 dummyDb;
82568 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
82569 if( pOut==0 ) pOut = stdoutstdout;
82570 sqlite3BeginBenignMalloc();
82571 dummyDb.mallocFailed = 1;
82572 zP4 = sqlite3VdbeDisplayP4(&dummyDb, pOp);
82573#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
82574 zCom = sqlite3VdbeDisplayComment(0, pOp, zP4);
82575#else
82576 zCom = 0;
82577#endif
82578 /* NB: The sqlite3OpcodeName() function is implemented by code created
82579 ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the
82580 ** information from the vdbe.c source text */
82581 fprintf(pOut, zFormat1, pc,
82582 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3,
82583 zP4 ? zP4 : "", pOp->p5,
82584 zCom ? zCom : ""
82585 );
82586 fflush(pOut);
82587 sqlite3_free(zP4);
82588 sqlite3_free(zCom);
82589 sqlite3EndBenignMalloc();
82590}
82591#endif
82592
82593/*
82594** Initialize an array of N Mem element.
82595**
82596** This is a high-runner, so only those fields that really do need to
82597** be initialized are set. The Mem structure is organized so that
82598** the fields that get initialized are nearby and hopefully on the same
82599** cache line.
82600**
82601** Mem.flags = flags
82602** Mem.db = db
82603** Mem.szMalloc = 0
82604**
82605** All other fields of Mem can safely remain uninitialized for now. They
82606** will be initialized before use.
82607*/
82608static void initMemArray(Mem *p, int N, sqlite3 *db, u16 flags){
82609 if( N>0 ){
82610 do{
82611 p->flags = flags;
82612 p->db = db;
82613 p->szMalloc = 0;
82614#ifdef SQLITE_DEBUG
82615 p->pScopyFrom = 0;
82616#endif
82617 p++;
82618 }while( (--N)>0 );
82619 }
82620}
82621
82622/*
82623** Release auxiliary memory held in an array of N Mem elements.
82624**
82625** After this routine returns, all Mem elements in the array will still
82626** be valid. Those Mem elements that were not holding auxiliary resources
82627** will be unchanged. Mem elements which had something freed will be
82628** set to MEM_Undefined.
82629*/
82630static void releaseMemArray(Mem *p, int N){
82631 if( p && N ){
82632 Mem *pEnd = &p[N];
82633 sqlite3 *db = p->db;
82634 if( db->pnBytesFreed ){
82635 do{
82636 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
82637 }while( (++p)<pEnd );
82638 return;
82639 }
82640 do{
82641 assert( (&p[1])==pEnd || p[0].db==p[1].db )((void) (0));
82642 assert( sqlite3VdbeCheckMemInvariants(p) )((void) (0));
82643
82644 /* This block is really an inlined version of sqlite3VdbeMemRelease()
82645 ** that takes advantage of the fact that the memory cell value is
82646 ** being set to NULL after releasing any dynamic resources.
82647 **
82648 ** The justification for duplicating code is that according to
82649 ** callgrind, this causes a certain test case to hit the CPU 4.7
82650 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
82651 ** sqlite3MemRelease() were called from here. With -O2, this jumps
82652 ** to 6.6 percent. The test case is inserting 1000 rows into a table
82653 ** with no indexes using a single prepared INSERT statement, bind()
82654 ** and reset(). Inserts are grouped into a transaction.
82655 */
82656 testcase( p->flags & MEM_Agg );
82657 testcase( p->flags & MEM_Dyn );
82658 if( p->flags&(MEM_Agg0x8000|MEM_Dyn0x1000) ){
82659 testcase( (p->flags & MEM_Dyn)!=0 && p->xDel==sqlite3VdbeFrameMemDel );
82660 sqlite3VdbeMemRelease(p);
82661 p->flags = MEM_Undefined0x0000;
82662 }else if( p->szMalloc ){
82663 sqlite3DbFreeNN(db, p->zMalloc);
82664 p->szMalloc = 0;
82665 p->flags = MEM_Undefined0x0000;
82666 }
82667#ifdef SQLITE_DEBUG
82668 else{
82669 p->flags = MEM_Undefined0x0000;
82670 }
82671#endif
82672 }while( (++p)<pEnd );
82673 }
82674}
82675
82676#ifdef SQLITE_DEBUG
82677/*
82678** Verify that pFrame is a valid VdbeFrame pointer. Return true if it is
82679** and false if something is wrong.
82680**
82681** This routine is intended for use inside of assert() statements only.
82682*/
82683SQLITE_PRIVATEstatic int sqlite3VdbeFrameIsValid(VdbeFrame *pFrame){
82684 if( pFrame->iFrameMagic!=SQLITE_FRAME_MAGIC0x879fb71e ) return 0;
82685 return 1;
82686}
82687#endif
82688
82689
82690/*
82691** This is a destructor on a Mem object (which is really an sqlite3_value)
82692** that deletes the Frame object that is attached to it as a blob.
82693**
82694** This routine does not delete the Frame right away. It merely adds the
82695** frame to a list of frames to be deleted when the Vdbe halts.
82696*/
82697SQLITE_PRIVATEstatic void sqlite3VdbeFrameMemDel(void *pArg){
82698 VdbeFrame *pFrame = (VdbeFrame*)pArg;
82699 assert( sqlite3VdbeFrameIsValid(pFrame) )((void) (0));
82700 pFrame->pParent = pFrame->v->pDelFrame;
82701 pFrame->v->pDelFrame = pFrame;
82702}
82703
82704#if defined(SQLITE_ENABLE_BYTECODE_VTAB) || !defined(SQLITE_OMIT_EXPLAIN)
82705/*
82706** Locate the next opcode to be displayed in EXPLAIN or EXPLAIN
82707** QUERY PLAN output.
82708**
82709** Return SQLITE_ROW on success. Return SQLITE_DONE if there are no
82710** more opcodes to be displayed.
82711*/
82712SQLITE_PRIVATEstatic int sqlite3VdbeNextOpcode(
82713 Vdbe *p, /* The statement being explained */
82714 Mem *pSub, /* Storage for keeping track of subprogram nesting */
82715 int eMode, /* 0: normal. 1: EQP. 2: TablesUsed */
82716 int *piPc, /* IN/OUT: Current rowid. Overwritten with next rowid */
82717 int *piAddr, /* OUT: Write index into (*paOp)[] here */
82718 Op **paOp /* OUT: Write the opcode array here */
82719){
82720 int nRow; /* Stop when row count reaches this */
82721 int nSub = 0; /* Number of sub-vdbes seen so far */
82722 SubProgram **apSub = 0; /* Array of sub-vdbes */
82723 int i; /* Next instruction address */
82724 int rc = SQLITE_OK0; /* Result code */
82725 Op *aOp = 0; /* Opcode array */
82726 int iPc; /* Rowid. Copy of value in *piPc */
82727
82728 /* When the number of output rows reaches nRow, that means the
82729 ** listing has finished and sqlite3_step() should return SQLITE_DONE.
82730 ** nRow is the sum of the number of rows in the main program, plus
82731 ** the sum of the number of rows in all trigger subprograms encountered
82732 ** so far. The nRow value will increase as new trigger subprograms are
82733 ** encountered, but p->pc will eventually catch up to nRow.
82734 */
82735 nRow = p->nOp;
82736 if( pSub!=0 ){
82737 if( pSub->flags&MEM_Blob0x0010 ){
82738 /* pSub is initiallly NULL. It is initialized to a BLOB by
82739 ** the P4_SUBPROGRAM processing logic below */
82740 nSub = pSub->n/sizeof(Vdbe*);
82741 apSub = (SubProgram **)pSub->z;
82742 }
82743 for(i=0; i<nSub; i++){
82744 nRow += apSub[i]->nOp;
82745 }
82746 }
82747 iPc = *piPc;
82748 while(1){ /* Loop exits via break */
82749 i = iPc++;
82750 if( i>=nRow ){
82751 p->rc = SQLITE_OK0;
82752 rc = SQLITE_DONE101;
82753 break;
82754 }
82755 if( i<p->nOp ){
82756 /* The rowid is small enough that we are still in the
82757 ** main program. */
82758 aOp = p->aOp;
82759 }else{
82760 /* We are currently listing subprograms. Figure out which one and
82761 ** pick up the appropriate opcode. */
82762 int j;
82763 i -= p->nOp;
82764 assert( apSub!=0 )((void) (0));
82765 assert( nSub>0 )((void) (0));
82766 for(j=0; i>=apSub[j]->nOp; j++){
82767 i -= apSub[j]->nOp;
82768 assert( i<apSub[j]->nOp || j+1<nSub )((void) (0));
82769 }
82770 aOp = apSub[j]->aOp;
82771 }
82772
82773 /* When an OP_Program opcode is encounter (the only opcode that has
82774 ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
82775 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
82776 ** has not already been seen.
82777 */
82778 if( pSub!=0 && aOp[i].p4type==P4_SUBPROGRAM(-4) ){
82779 int nByte = (nSub+1)*sizeof(SubProgram*);
82780 int j;
82781 for(j=0; j<nSub; j++){
82782 if( apSub[j]==aOp[i].p4.pProgram ) break;
82783 }
82784 if( j==nSub ){
82785 p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0);
82786 if( p->rc!=SQLITE_OK0 ){
82787 rc = SQLITE_ERROR1;
82788 break;
82789 }
82790 apSub = (SubProgram **)pSub->z;
82791 apSub[nSub++] = aOp[i].p4.pProgram;
82792 MemSetTypeFlag(pSub, MEM_Blob)((pSub)->flags = ((pSub)->flags&~(0x0dbf|0x0400))|0x0010
)
;
82793 pSub->n = nSub*sizeof(SubProgram*);
82794 nRow += aOp[i].p4.pProgram->nOp;
82795 }
82796 }
82797 if( eMode==0 ) break;
82798#ifdef SQLITE_ENABLE_BYTECODE_VTAB
82799 if( eMode==2 ){
82800 Op *pOp = aOp + i;
82801 if( pOp->opcode==OP_OpenRead112 ) break;
82802 if( pOp->opcode==OP_OpenWrite113 && (pOp->p5 & OPFLAG_P2ISREG0x10)==0 ) break;
82803 if( pOp->opcode==OP_ReopenIdx101 ) break;
82804 }else
82805#endif
82806 {
82807 assert( eMode==1 )((void) (0));
82808 if( aOp[i].opcode==OP_Explain185 ) break;
82809 if( aOp[i].opcode==OP_Init64 && iPc>1 ) break;
82810 }
82811 }
82812 *piPc = iPc;
82813 *piAddr = i;
82814 *paOp = aOp;
82815 return rc;
82816}
82817#endif /* SQLITE_ENABLE_BYTECODE_VTAB || !SQLITE_OMIT_EXPLAIN */
82818
82819
82820/*
82821** Delete a VdbeFrame object and its contents. VdbeFrame objects are
82822** allocated by the OP_Program opcode in sqlite3VdbeExec().
82823*/
82824SQLITE_PRIVATEstatic void sqlite3VdbeFrameDelete(VdbeFrame *p){
82825 int i;
82826 Mem *aMem = VdbeFrameMem(p)((Mem *)&((u8 *)p)[(((sizeof(VdbeFrame))+7)&~7)]);
82827 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
82828 assert( sqlite3VdbeFrameIsValid(p) )((void) (0));
82829 for(i=0; i<p->nChildCsr; i++){
82830 if( apCsr[i] ) sqlite3VdbeFreeCursorNN(p->v, apCsr[i]);
82831 }
82832 releaseMemArray(aMem, p->nChildMem);
82833 sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0);
82834 sqlite3DbFree(p->v->db, p);
82835}
82836
82837#ifndef SQLITE_OMIT_EXPLAIN
82838/*
82839** Give a listing of the program in the virtual machine.
82840**
82841** The interface is the same as sqlite3VdbeExec(). But instead of
82842** running the code, it invokes the callback once for each instruction.
82843** This feature is used to implement "EXPLAIN".
82844**
82845** When p->explain==1, each instruction is listed. When
82846** p->explain==2, only OP_Explain instructions are listed and these
82847** are shown in a different format. p->explain==2 is used to implement
82848** EXPLAIN QUERY PLAN.
82849** 2018-04-24: In p->explain==2 mode, the OP_Init opcodes of triggers
82850** are also shown, so that the boundaries between the main program and
82851** each trigger are clear.
82852**
82853** When p->explain==1, first the main program is listed, then each of
82854** the trigger subprograms are listed one by one.
82855*/
82856SQLITE_PRIVATEstatic int sqlite3VdbeList(
82857 Vdbe *p /* The VDBE */
82858){
82859 Mem *pSub = 0; /* Memory cell hold array of subprogs */
82860 sqlite3 *db = p->db; /* The database connection */
82861 int i; /* Loop counter */
82862 int rc = SQLITE_OK0; /* Return code */
82863 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
82864 int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP0x01000000)!=0);
82865 Op *aOp; /* Array of opcodes */
82866 Op *pOp; /* Current opcode */
82867
82868 assert( p->explain )((void) (0));
82869 assert( p->eVdbeState==VDBE_RUN_STATE )((void) (0));
82870 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM )((void) (0));
82871
82872 /* Even though this opcode does not use dynamic strings for
82873 ** the result, result columns may become dynamic if the user calls
82874 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
82875 */
82876 releaseMemArray(pMem, 8);
82877 p->pResultSet = 0;
82878
82879 if( p->rc==SQLITE_NOMEM7 ){
82880 /* This happens if a malloc() inside a call to sqlite3_column_text() or
82881 ** sqlite3_column_text16() failed. */
82882 sqlite3OomFault(db);
82883 return SQLITE_ERROR1;
82884 }
82885
82886 if( bListSubprogs ){
82887 /* The first 8 memory cells are used for the result set. So we will
82888 ** commandeer the 9th cell to use as storage for an array of pointers
82889 ** to trigger subprograms. The VDBE is guaranteed to have at least 9
82890 ** cells. */
82891 assert( p->nMem>9 )((void) (0));
82892 pSub = &p->aMem[9];
82893 }else{
82894 pSub = 0;
82895 }
82896
82897 /* Figure out which opcode is next to display */
82898 rc = sqlite3VdbeNextOpcode(p, pSub, p->explain==2, &p->pc, &i, &aOp);
82899
82900 if( rc==SQLITE_OK0 ){
82901 pOp = aOp + i;
82902 if( AtomicLoad(&db->u1.isInterrupted)__atomic_load_n((&db->u1.isInterrupted),0) ){
82903 p->rc = SQLITE_INTERRUPT9;
82904 rc = SQLITE_ERROR1;
82905 sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
82906 }else{
82907 char *zP4 = sqlite3VdbeDisplayP4(db, pOp);
82908 if( p->explain==2 ){
82909 sqlite3VdbeMemSetInt64(pMem, pOp->p1);
82910 sqlite3VdbeMemSetInt64(pMem+1, pOp->p2);
82911 sqlite3VdbeMemSetInt64(pMem+2, pOp->p3);
82912 sqlite3VdbeMemSetStr(pMem+3, zP4, -1, SQLITE_UTF81, sqlite3_free);
82913 p->nResColumn = 4;
82914 }else{
82915 sqlite3VdbeMemSetInt64(pMem+0, i);
82916 sqlite3VdbeMemSetStr(pMem+1, (char*)sqlite3OpcodeName(pOp->opcode),
82917 -1, SQLITE_UTF81, SQLITE_STATIC((sqlite3_destructor_type)0));
82918 sqlite3VdbeMemSetInt64(pMem+2, pOp->p1);
82919 sqlite3VdbeMemSetInt64(pMem+3, pOp->p2);
82920 sqlite3VdbeMemSetInt64(pMem+4, pOp->p3);
82921 /* pMem+5 for p4 is done last */
82922 sqlite3VdbeMemSetInt64(pMem+6, pOp->p5);
82923#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
82924 {
82925 char *zCom = sqlite3VdbeDisplayComment(db, pOp, zP4);
82926 sqlite3VdbeMemSetStr(pMem+7, zCom, -1, SQLITE_UTF81, sqlite3_free);
82927 }
82928#else
82929 sqlite3VdbeMemSetNull(pMem+7);
82930#endif
82931 sqlite3VdbeMemSetStr(pMem+5, zP4, -1, SQLITE_UTF81, sqlite3_free);
82932 p->nResColumn = 8;
82933 }
82934 p->pResultSet = pMem;
82935 if( db->mallocFailed ){
82936 p->rc = SQLITE_NOMEM7;
82937 rc = SQLITE_ERROR1;
82938 }else{
82939 p->rc = SQLITE_OK0;
82940 rc = SQLITE_ROW100;
82941 }
82942 }
82943 }
82944 return rc;
82945}
82946#endif /* SQLITE_OMIT_EXPLAIN */
82947
82948#ifdef SQLITE_DEBUG
82949/*
82950** Print the SQL that was used to generate a VDBE program.
82951*/
82952SQLITE_PRIVATEstatic void sqlite3VdbePrintSql(Vdbe *p){
82953 const char *z = 0;
82954 if( p->zSql ){
82955 z = p->zSql;
82956 }else if( p->nOp>=1 ){
82957 const VdbeOp *pOp = &p->aOp[0];
82958 if( pOp->opcode==OP_Init64 && pOp->p4.z!=0 ){
82959 z = pOp->p4.z;
82960 while( sqlite3Isspace(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x01) ) z++;
82961 }
82962 }
82963 if( z ) printf("SQL: [%s]\n", z);
82964}
82965#endif
82966
82967#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
82968/*
82969** Print an IOTRACE message showing SQL content.
82970*/
82971SQLITE_PRIVATEstatic void sqlite3VdbeIOTraceSql(Vdbe *p){
82972 int nOp = p->nOp;
82973 VdbeOp *pOp;
82974 if( sqlite3IoTrace==0 ) return;
82975 if( nOp<1 ) return;
82976 pOp = &p->aOp[0];
82977 if( pOp->opcode==OP_Init64 && pOp->p4.z!=0 ){
82978 int i, j;
82979 char z[1000];
82980 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
82981 for(i=0; sqlite3Isspace(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x01); i++){}
82982 for(j=0; z[i]; i++){
82983 if( sqlite3Isspace(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x01) ){
82984 if( z[i-1]!=' ' ){
82985 z[j++] = ' ';
82986 }
82987 }else{
82988 z[j++] = z[i];
82989 }
82990 }
82991 z[j] = 0;
82992 sqlite3IoTrace("SQL %s\n", z);
82993 }
82994}
82995#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
82996
82997/* An instance of this object describes bulk memory available for use
82998** by subcomponents of a prepared statement. Space is allocated out
82999** of a ReusableSpace object by the allocSpace() routine below.
83000*/
83001struct ReusableSpace {
83002 u8 *pSpace; /* Available memory */
83003 sqlite3_int64 nFree; /* Bytes of available memory */
83004 sqlite3_int64 nNeeded; /* Total bytes that could not be allocated */
83005};
83006
83007/* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
83008** from the ReusableSpace object. Return a pointer to the allocated
83009** memory on success. If insufficient memory is available in the
83010** ReusableSpace object, increase the ReusableSpace.nNeeded
83011** value by the amount needed and return NULL.
83012**
83013** If pBuf is not initially NULL, that means that the memory has already
83014** been allocated by a prior call to this routine, so just return a copy
83015** of pBuf and leave ReusableSpace unchanged.
83016**
83017** This allocator is employed to repurpose unused slots at the end of the
83018** opcode array of prepared state for other memory needs of the prepared
83019** statement.
83020*/
83021static void *allocSpace(
83022 struct ReusableSpace *p, /* Bulk memory available for allocation */
83023 void *pBuf, /* Pointer to a prior allocation */
83024 sqlite3_int64 nByte /* Bytes of memory needed. */
83025){
83026 assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) )((void) (0));
83027 if( pBuf==0 ){
83028 nByte = ROUND8P(nByte)(nByte);
83029 if( nByte <= p->nFree ){
83030 p->nFree -= nByte;
83031 pBuf = &p->pSpace[p->nFree];
83032 }else{
83033 p->nNeeded += nByte;
83034 }
83035 }
83036 assert( EIGHT_BYTE_ALIGNMENT(pBuf) )((void) (0));
83037 return pBuf;
83038}
83039
83040/*
83041** Rewind the VDBE back to the beginning in preparation for
83042** running it.
83043*/
83044SQLITE_PRIVATEstatic void sqlite3VdbeRewind(Vdbe *p){
83045#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
83046 int i;
83047#endif
83048 assert( p!=0 )((void) (0));
83049 assert( p->eVdbeState==VDBE_INIT_STATE((void) (0))
83050 || p->eVdbeState==VDBE_READY_STATE((void) (0))
83051 || p->eVdbeState==VDBE_HALT_STATE )((void) (0));
83052
83053 /* There should be at least one opcode.
83054 */
83055 assert( p->nOp>0 )((void) (0));
83056
83057 p->eVdbeState = VDBE_READY_STATE1;
83058
83059#ifdef SQLITE_DEBUG
83060 for(i=0; i<p->nMem; i++){
83061 assert( p->aMem[i].db==p->db )((void) (0));
83062 }
83063#endif
83064 p->pc = -1;
83065 p->rc = SQLITE_OK0;
83066 p->errorAction = OE_Abort2;
83067 p->nChange = 0;
83068 p->cacheCtr = 1;
83069 p->minWriteFileFormat = 255;
83070 p->iStatement = 0;
83071 p->nFkConstraint = 0;
83072#ifdef VDBE_PROFILE
83073 for(i=0; i<p->nOp; i++){
83074 p->aOp[i].cnt = 0;
83075 p->aOp[i].cycles = 0;
83076 }
83077#endif
83078}
83079
83080/*
83081** Prepare a virtual machine for execution for the first time after
83082** creating the virtual machine. This involves things such
83083** as allocating registers and initializing the program counter.
83084** After the VDBE has be prepped, it can be executed by one or more
83085** calls to sqlite3VdbeExec().
83086**
83087** This function may be called exactly once on each virtual machine.
83088** After this routine is called the VM has been "packaged" and is ready
83089** to run. After this routine is called, further calls to
83090** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects
83091** the Vdbe from the Parse object that helped generate it so that the
83092** the Vdbe becomes an independent entity and the Parse object can be
83093** destroyed.
83094**
83095** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
83096** to its initial state after it has been run.
83097*/
83098SQLITE_PRIVATEstatic void sqlite3VdbeMakeReady(
83099 Vdbe *p, /* The VDBE */
83100 Parse *pParse /* Parsing context */
83101){
83102 sqlite3 *db; /* The database connection */
83103 int nVar; /* Number of parameters */
83104 int nMem; /* Number of VM memory registers */
83105 int nCursor; /* Number of cursors required */
83106 int nArg; /* Number of arguments in subprograms */
83107 int n; /* Loop counter */
83108 struct ReusableSpace x; /* Reusable bulk memory */
83109
83110 assert( p!=0 )((void) (0));
83111 assert( p->nOp>0 )((void) (0));
83112 assert( pParse!=0 )((void) (0));
83113 assert( p->eVdbeState==VDBE_INIT_STATE )((void) (0));
83114 assert( pParse==p->pParse )((void) (0));
83115 p->pVList = pParse->pVList;
83116 pParse->pVList = 0;
83117 db = p->db;
83118 assert( db->mallocFailed==0 )((void) (0));
83119 nVar = pParse->nVar;
83120 nMem = pParse->nMem;
83121 nCursor = pParse->nTab;
83122 nArg = pParse->nMaxArg;
83123
83124 /* Each cursor uses a memory cell. The first cursor (cursor 0) can
83125 ** use aMem[0] which is not otherwise used by the VDBE program. Allocate
83126 ** space at the end of aMem[] for cursors 1 and greater.
83127 ** See also: allocateCursor().
83128 */
83129 nMem += nCursor;
83130 if( nCursor==0 && nMem>0 ) nMem++; /* Space for aMem[0] even if not used */
83131
83132 /* Figure out how much reusable memory is available at the end of the
83133 ** opcode array. This extra memory will be reallocated for other elements
83134 ** of the prepared statement.
83135 */
83136 n = ROUND8P(sizeof(Op)*p->nOp)(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */
83137 x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */
83138 assert( EIGHT_BYTE_ALIGNMENT(x.pSpace) )((void) (0));
83139 x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n)((pParse->szOpAlloc - n)&~7); /* Bytes of unused memory */
83140 assert( x.nFree>=0 )((void) (0));
83141 assert( EIGHT_BYTE_ALIGNMENT(&x.pSpace[x.nFree]) )((void) (0));
83142
83143 resolveP2Values(p, &nArg);
83144 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
83145 if( pParse->explain ){
83146 static const char * const azColName[] = {
83147 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
83148 "id", "parent", "notused", "detail"
83149 };
83150 int iFirst, mx, i;
83151 if( nMem<10 ) nMem = 10;
83152 p->explain = pParse->explain;
83153 if( pParse->explain==2 ){
83154 sqlite3VdbeSetNumCols(p, 4);
83155 iFirst = 8;
83156 mx = 12;
83157 }else{
83158 sqlite3VdbeSetNumCols(p, 8);
83159 iFirst = 0;
83160 mx = 8;
83161 }
83162 for(i=iFirst; i<mx; i++){
83163 sqlite3VdbeSetColName(p, i-iFirst, COLNAME_NAME0,
83164 azColName[i], SQLITE_STATIC((sqlite3_destructor_type)0));
83165 }
83166 }
83167 p->expired = 0;
83168
83169 /* Memory for registers, parameters, cursor, etc, is allocated in one or two
83170 ** passes. On the first pass, we try to reuse unused memory at the
83171 ** end of the opcode array. If we are unable to satisfy all memory
83172 ** requirements by reusing the opcode array tail, then the second
83173 ** pass will fill in the remainder using a fresh memory allocation.
83174 **
83175 ** This two-pass approach that reuses as much memory as possible from
83176 ** the leftover memory at the end of the opcode array. This can significantly
83177 ** reduce the amount of memory held by a prepared statement.
83178 */
83179 x.nNeeded = 0;
83180 p->aMem = allocSpace(&x, 0, nMem*sizeof(Mem));
83181 p->aVar = allocSpace(&x, 0, nVar*sizeof(Mem));
83182 p->apArg = allocSpace(&x, 0, nArg*sizeof(Mem*));
83183 p->apCsr = allocSpace(&x, 0, nCursor*sizeof(VdbeCursor*));
83184#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
83185 p->anExec = allocSpace(&x, 0, p->nOp*sizeof(i64));
83186#endif
83187 if( x.nNeeded ){
83188 x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded);
83189 x.nFree = x.nNeeded;
83190 if( !db->mallocFailed ){
83191 p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
83192 p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
83193 p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
83194 p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
83195#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
83196 p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64));
83197#endif
83198 }
83199 }
83200
83201 if( db->mallocFailed ){
83202 p->nVar = 0;
83203 p->nCursor = 0;
83204 p->nMem = 0;
83205 }else{
83206 p->nCursor = nCursor;
83207 p->nVar = (ynVar)nVar;
83208 initMemArray(p->aVar, nVar, db, MEM_Null0x0001);
83209 p->nMem = nMem;
83210 initMemArray(p->aMem, nMem, db, MEM_Undefined0x0000);
83211 memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));
83212#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
83213 memset(p->anExec, 0, p->nOp*sizeof(i64));
83214#endif
83215 }
83216 sqlite3VdbeRewind(p);
83217}
83218
83219/*
83220** Close a VDBE cursor and release all the resources that cursor
83221** happens to hold.
83222*/
83223SQLITE_PRIVATEstatic void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
83224 if( pCx ) sqlite3VdbeFreeCursorNN(p,pCx);
83225}
83226SQLITE_PRIVATEstatic void sqlite3VdbeFreeCursorNN(Vdbe *p, VdbeCursor *pCx){
83227 switch( pCx->eCurType ){
83228 case CURTYPE_SORTER1: {
83229 sqlite3VdbeSorterClose(p->db, pCx);
83230 break;
83231 }
83232 case CURTYPE_BTREE0: {
83233 assert( pCx->uc.pCursor!=0 )((void) (0));
83234 sqlite3BtreeCloseCursor(pCx->uc.pCursor);
83235 break;
83236 }
83237#ifndef SQLITE_OMIT_VIRTUALTABLE
83238 case CURTYPE_VTAB2: {
83239 sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
83240 const sqlite3_module *pModule = pVCur->pVtab->pModule;
83241 assert( pVCur->pVtab->nRef>0 )((void) (0));
83242 pVCur->pVtab->nRef--;
83243 pModule->xClose(pVCur);
83244 break;
83245 }
83246#endif
83247 }
83248}
83249
83250/*
83251** Close all cursors in the current frame.
83252*/
83253static void closeCursorsInFrame(Vdbe *p){
83254 int i;
83255 for(i=0; i<p->nCursor; i++){
83256 VdbeCursor *pC = p->apCsr[i];
83257 if( pC ){
83258 sqlite3VdbeFreeCursorNN(p, pC);
83259 p->apCsr[i] = 0;
83260 }
83261 }
83262}
83263
83264/*
83265** Copy the values stored in the VdbeFrame structure to its Vdbe. This
83266** is used, for example, when a trigger sub-program is halted to restore
83267** control to the main program.
83268*/
83269SQLITE_PRIVATEstatic int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
83270 Vdbe *v = pFrame->v;
83271 closeCursorsInFrame(v);
83272#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
83273 v->anExec = pFrame->anExec;
83274#endif
83275 v->aOp = pFrame->aOp;
83276 v->nOp = pFrame->nOp;
83277 v->aMem = pFrame->aMem;
83278 v->nMem = pFrame->nMem;
83279 v->apCsr = pFrame->apCsr;
83280 v->nCursor = pFrame->nCursor;
83281 v->db->lastRowid = pFrame->lastRowid;
83282 v->nChange = pFrame->nChange;
83283 v->db->nChange = pFrame->nDbChange;
83284 sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0);
83285 v->pAuxData = pFrame->pAuxData;
83286 pFrame->pAuxData = 0;
83287 return pFrame->pc;
83288}
83289
83290/*
83291** Close all cursors.
83292**
83293** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
83294** cell array. This is necessary as the memory cell array may contain
83295** pointers to VdbeFrame objects, which may in turn contain pointers to
83296** open cursors.
83297*/
83298static void closeAllCursors(Vdbe *p){
83299 if( p->pFrame ){
83300 VdbeFrame *pFrame;
83301 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
83302 sqlite3VdbeFrameRestore(pFrame);
83303 p->pFrame = 0;
83304 p->nFrame = 0;
83305 }
83306 assert( p->nFrame==0 )((void) (0));
83307 closeCursorsInFrame(p);
83308 releaseMemArray(p->aMem, p->nMem);
83309 while( p->pDelFrame ){
83310 VdbeFrame *pDel = p->pDelFrame;
83311 p->pDelFrame = pDel->pParent;
83312 sqlite3VdbeFrameDelete(pDel);
83313 }
83314
83315 /* Delete any auxdata allocations made by the VM */
83316 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
83317 assert( p->pAuxData==0 )((void) (0));
83318}
83319
83320/*
83321** Set the number of result columns that will be returned by this SQL
83322** statement. This is now set at compile time, rather than during
83323** execution of the vdbe program so that sqlite3_column_count() can
83324** be called on an SQL statement before sqlite3_step().
83325*/
83326SQLITE_PRIVATEstatic void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
83327 int n;
83328 sqlite3 *db = p->db;
83329
83330 if( p->nResColumn ){
83331 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N5);
83332 sqlite3DbFree(db, p->aColName);
83333 }
83334 n = nResColumn*COLNAME_N5;
83335 p->nResColumn = (u16)nResColumn;
83336 p->aColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n );
83337 if( p->aColName==0 ) return;
83338 initMemArray(p->aColName, n, db, MEM_Null0x0001);
83339}
83340
83341/*
83342** Set the name of the idx'th column to be returned by the SQL statement.
83343** zName must be a pointer to a nul terminated string.
83344**
83345** This call must be made after a call to sqlite3VdbeSetNumCols().
83346**
83347** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
83348** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
83349** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
83350*/
83351SQLITE_PRIVATEstatic int sqlite3VdbeSetColName(
83352 Vdbe *p, /* Vdbe being configured */
83353 int idx, /* Index of column zName applies to */
83354 int var, /* One of the COLNAME_* constants */
83355 const char *zName, /* Pointer to buffer containing name */
83356 void (*xDel)(void*) /* Memory management strategy for zName */
83357){
83358 int rc;
83359 Mem *pColName;
83360 assert( idx<p->nResColumn )((void) (0));
83361 assert( var<COLNAME_N )((void) (0));
83362 if( p->db->mallocFailed ){
83363 assert( !zName || xDel!=SQLITE_DYNAMIC )((void) (0));
83364 return SQLITE_NOMEM_BKPT7;
83365 }
83366 assert( p->aColName!=0 )((void) (0));
83367 pColName = &(p->aColName[idx+var*p->nResColumn]);
83368 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF81, xDel);
83369 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 )((void) (0));
83370 return rc;
83371}
83372
83373/*
83374** A read or write transaction may or may not be active on database handle
83375** db. If a transaction is active, commit it. If there is a
83376** write-transaction spanning more than one database file, this routine
83377** takes care of the super-journal trickery.
83378*/
83379static int vdbeCommit(sqlite3 *db, Vdbe *p){
83380 int i;
83381 int nTrans = 0; /* Number of databases with an active write-transaction
83382 ** that are candidates for a two-phase commit using a
83383 ** super-journal */
83384 int rc = SQLITE_OK0;
83385 int needXcommit = 0;
83386
83387#ifdef SQLITE_OMIT_VIRTUALTABLE
83388 /* With this option, sqlite3VtabSync() is defined to be simply
83389 ** SQLITE_OK so p is not used.
83390 */
83391 UNUSED_PARAMETER(p)(void)(p);
83392#endif
83393
83394 /* Before doing anything else, call the xSync() callback for any
83395 ** virtual module tables written in this transaction. This has to
83396 ** be done before determining whether a super-journal file is
83397 ** required, as an xSync() callback may add an attached database
83398 ** to the transaction.
83399 */
83400 rc = sqlite3VtabSync(db, p);
83401
83402 /* This loop determines (a) if the commit hook should be invoked and
83403 ** (b) how many database files have open write transactions, not
83404 ** including the temp database. (b) is important because if more than
83405 ** one database file has an open write transaction, a super-journal
83406 ** file is required for an atomic commit.
83407 */
83408 for(i=0; rc==SQLITE_OK0 && i<db->nDb; i++){
83409 Btree *pBt = db->aDb[i].pBt;
83410 if( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE2 ){
83411 /* Whether or not a database might need a super-journal depends upon
83412 ** its journal mode (among other things). This matrix determines which
83413 ** journal modes use a super-journal and which do not */
83414 static const u8 aMJNeeded[] = {
83415 /* DELETE */ 1,
83416 /* PERSIST */ 1,
83417 /* OFF */ 0,
83418 /* TRUNCATE */ 1,
83419 /* MEMORY */ 0,
83420 /* WAL */ 0
83421 };
83422 Pager *pPager; /* Pager associated with pBt */
83423 needXcommit = 1;
83424 sqlite3BtreeEnter(pBt);
83425 pPager = sqlite3BtreePager(pBt);
83426 if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF0x01
83427 && aMJNeeded[sqlite3PagerGetJournalMode(pPager)]
83428 && sqlite3PagerIsMemdb(pPager)==0
83429 ){
83430 assert( i!=1 )((void) (0));
83431 nTrans++;
83432 }
83433 rc = sqlite3PagerExclusiveLock(pPager);
83434 sqlite3BtreeLeave(pBt);
83435 }
83436 }
83437 if( rc!=SQLITE_OK0 ){
83438 return rc;
83439 }
83440
83441 /* If there are any write-transactions at all, invoke the commit hook */
83442 if( needXcommit && db->xCommitCallback ){
83443 rc = db->xCommitCallback(db->pCommitArg);
83444 if( rc ){
83445 return SQLITE_CONSTRAINT_COMMITHOOK(19 | (2<<8));
83446 }
83447 }
83448
83449 /* The simple case - no more than one database file (not counting the
83450 ** TEMP database) has a transaction active. There is no need for the
83451 ** super-journal.
83452 **
83453 ** If the return value of sqlite3BtreeGetFilename() is a zero length
83454 ** string, it means the main database is :memory: or a temp file. In
83455 ** that case we do not support atomic multi-file commits, so use the
83456 ** simple case then too.
83457 */
83458 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
83459 || nTrans<=1
83460 ){
83461 for(i=0; rc==SQLITE_OK0 && i<db->nDb; i++){
83462 Btree *pBt = db->aDb[i].pBt;
83463 if( pBt ){
83464 rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
83465 }
83466 }
83467
83468 /* Do the commit only if all databases successfully complete phase 1.
83469 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
83470 ** IO error while deleting or truncating a journal file. It is unlikely,
83471 ** but could happen. In this case abandon processing and return the error.
83472 */
83473 for(i=0; rc==SQLITE_OK0 && i<db->nDb; i++){
83474 Btree *pBt = db->aDb[i].pBt;
83475 if( pBt ){
83476 rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
83477 }
83478 }
83479 if( rc==SQLITE_OK0 ){
83480 sqlite3VtabCommit(db);
83481 }
83482 }
83483
83484 /* The complex case - There is a multi-file write-transaction active.
83485 ** This requires a super-journal file to ensure the transaction is
83486 ** committed atomically.
83487 */
83488#ifndef SQLITE_OMIT_DISKIO
83489 else{
83490 sqlite3_vfs *pVfs = db->pVfs;
83491 char *zSuper = 0; /* File-name for the super-journal */
83492 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
83493 sqlite3_file *pSuperJrnl = 0;
83494 i64 offset = 0;
83495 int res;
83496 int retryCount = 0;
83497 int nMainFile;
83498
83499 /* Select a super-journal file name */
83500 nMainFile = sqlite3Strlen30(zMainFile);
83501 zSuper = sqlite3MPrintf(db, "%.4c%s%.16c", 0,zMainFile,0);
83502 if( zSuper==0 ) return SQLITE_NOMEM_BKPT7;
83503 zSuper += 4;
83504 do {
83505 u32 iRandom;
83506 if( retryCount ){
83507 if( retryCount>100 ){
83508 sqlite3_log(SQLITE_FULL13, "MJ delete: %s", zSuper);
83509 sqlite3OsDelete(pVfs, zSuper, 0);
83510 break;
83511 }else if( retryCount==1 ){
83512 sqlite3_log(SQLITE_FULL13, "MJ collide: %s", zSuper);
83513 }
83514 }
83515 retryCount++;
83516 sqlite3_randomness(sizeof(iRandom), &iRandom);
83517 sqlite3_snprintf(13, &zSuper[nMainFile], "-mj%06X9%02X",
83518 (iRandom>>8)&0xffffff, iRandom&0xff);
83519 /* The antipenultimate character of the super-journal name must
83520 ** be "9" to avoid name collisions when using 8+3 filenames. */
83521 assert( zSuper[sqlite3Strlen30(zSuper)-3]=='9' )((void) (0));
83522 sqlite3FileSuffix3(zMainFile, zSuper);
83523 rc = sqlite3OsAccess(pVfs, zSuper, SQLITE_ACCESS_EXISTS0, &res);
83524 }while( rc==SQLITE_OK0 && res );
83525 if( rc==SQLITE_OK0 ){
83526 /* Open the super-journal. */
83527 rc = sqlite3OsOpenMalloc(pVfs, zSuper, &pSuperJrnl,
83528 SQLITE_OPEN_READWRITE0x00000002|SQLITE_OPEN_CREATE0x00000004|
83529 SQLITE_OPEN_EXCLUSIVE0x00000010|SQLITE_OPEN_SUPER_JOURNAL0x00004000, 0
83530 );
83531 }
83532 if( rc!=SQLITE_OK0 ){
83533 sqlite3DbFree(db, zSuper-4);
83534 return rc;
83535 }
83536
83537 /* Write the name of each database file in the transaction into the new
83538 ** super-journal file. If an error occurs at this point close
83539 ** and delete the super-journal file. All the individual journal files
83540 ** still have 'null' as the super-journal pointer, so they will roll
83541 ** back independently if a failure occurs.
83542 */
83543 for(i=0; i<db->nDb; i++){
83544 Btree *pBt = db->aDb[i].pBt;
83545 if( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE2 ){
83546 char const *zFile = sqlite3BtreeGetJournalname(pBt);
83547 if( zFile==0 ){
83548 continue; /* Ignore TEMP and :memory: databases */
83549 }
83550 assert( zFile[0]!=0 )((void) (0));
83551 rc = sqlite3OsWrite(pSuperJrnl, zFile, sqlite3Strlen30(zFile)+1,offset);
83552 offset += sqlite3Strlen30(zFile)+1;
83553 if( rc!=SQLITE_OK0 ){
83554 sqlite3OsCloseFree(pSuperJrnl);
83555 sqlite3OsDelete(pVfs, zSuper, 0);
83556 sqlite3DbFree(db, zSuper-4);
83557 return rc;
83558 }
83559 }
83560 }
83561
83562 /* Sync the super-journal file. If the IOCAP_SEQUENTIAL device
83563 ** flag is set this is not required.
83564 */
83565 if( 0==(sqlite3OsDeviceCharacteristics(pSuperJrnl)&SQLITE_IOCAP_SEQUENTIAL0x00000400)
83566 && SQLITE_OK0!=(rc = sqlite3OsSync(pSuperJrnl, SQLITE_SYNC_NORMAL0x00002))
83567 ){
83568 sqlite3OsCloseFree(pSuperJrnl);
83569 sqlite3OsDelete(pVfs, zSuper, 0);
83570 sqlite3DbFree(db, zSuper-4);
83571 return rc;
83572 }
83573
83574 /* Sync all the db files involved in the transaction. The same call
83575 ** sets the super-journal pointer in each individual journal. If
83576 ** an error occurs here, do not delete the super-journal file.
83577 **
83578 ** If the error occurs during the first call to
83579 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
83580 ** super-journal file will be orphaned. But we cannot delete it,
83581 ** in case the super-journal file name was written into the journal
83582 ** file before the failure occurred.
83583 */
83584 for(i=0; rc==SQLITE_OK0 && i<db->nDb; i++){
83585 Btree *pBt = db->aDb[i].pBt;
83586 if( pBt ){
83587 rc = sqlite3BtreeCommitPhaseOne(pBt, zSuper);
83588 }
83589 }
83590 sqlite3OsCloseFree(pSuperJrnl);
83591 assert( rc!=SQLITE_BUSY )((void) (0));
83592 if( rc!=SQLITE_OK0 ){
83593 sqlite3DbFree(db, zSuper-4);
83594 return rc;
83595 }
83596
83597 /* Delete the super-journal file. This commits the transaction. After
83598 ** doing this the directory is synced again before any individual
83599 ** transaction files are deleted.
83600 */
83601 rc = sqlite3OsDelete(pVfs, zSuper, 1);
83602 sqlite3DbFree(db, zSuper-4);
83603 zSuper = 0;
83604 if( rc ){
83605 return rc;
83606 }
83607
83608 /* All files and directories have already been synced, so the following
83609 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
83610 ** deleting or truncating journals. If something goes wrong while
83611 ** this is happening we don't really care. The integrity of the
83612 ** transaction is already guaranteed, but some stray 'cold' journals
83613 ** may be lying around. Returning an error code won't help matters.
83614 */
83615 disable_simulated_io_errors();
83616 sqlite3BeginBenignMalloc();
83617 for(i=0; i<db->nDb; i++){
83618 Btree *pBt = db->aDb[i].pBt;
83619 if( pBt ){
83620 sqlite3BtreeCommitPhaseTwo(pBt, 1);
83621 }
83622 }
83623 sqlite3EndBenignMalloc();
83624 enable_simulated_io_errors();
83625
83626 sqlite3VtabCommit(db);
83627 }
83628#endif
83629
83630 return rc;
83631}
83632
83633/*
83634** This routine checks that the sqlite3.nVdbeActive count variable
83635** matches the number of vdbe's in the list sqlite3.pVdbe that are
83636** currently active. An assertion fails if the two counts do not match.
83637** This is an internal self-check only - it is not an essential processing
83638** step.
83639**
83640** This is a no-op if NDEBUG is defined.
83641*/
83642#ifndef NDEBUG1
83643static void checkActiveVdbeCnt(sqlite3 *db){
83644 Vdbe *p;
83645 int cnt = 0;
83646 int nWrite = 0;
83647 int nRead = 0;
83648 p = db->pVdbe;
83649 while( p ){
83650 if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){
83651 cnt++;
83652 if( p->readOnly==0 ) nWrite++;
83653 if( p->bIsReader ) nRead++;
83654 }
83655 p = p->pNext;
83656 }
83657 assert( cnt==db->nVdbeActive )((void) (0));
83658 assert( nWrite==db->nVdbeWrite )((void) (0));
83659 assert( nRead==db->nVdbeRead )((void) (0));
83660}
83661#else
83662#define checkActiveVdbeCnt(x)
83663#endif
83664
83665/*
83666** If the Vdbe passed as the first argument opened a statement-transaction,
83667** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
83668** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
83669** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
83670** statement transaction is committed.
83671**
83672** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
83673** Otherwise SQLITE_OK.
83674*/
83675static SQLITE_NOINLINE__attribute__((noinline)) int vdbeCloseStatement(Vdbe *p, int eOp){
83676 sqlite3 *const db = p->db;
83677 int rc = SQLITE_OK0;
83678 int i;
83679 const int iSavepoint = p->iStatement-1;
83680
83681 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE)((void) (0));
83682 assert( db->nStatement>0 )((void) (0));
83683 assert( p->iStatement==(db->nStatement+db->nSavepoint) )((void) (0));
83684
83685 for(i=0; i<db->nDb; i++){
83686 int rc2 = SQLITE_OK0;
83687 Btree *pBt = db->aDb[i].pBt;
83688 if( pBt ){
83689 if( eOp==SAVEPOINT_ROLLBACK2 ){
83690 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK2, iSavepoint);
83691 }
83692 if( rc2==SQLITE_OK0 ){
83693 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE1, iSavepoint);
83694 }
83695 if( rc==SQLITE_OK0 ){
83696 rc = rc2;
83697 }
83698 }
83699 }
83700 db->nStatement--;
83701 p->iStatement = 0;
83702
83703 if( rc==SQLITE_OK0 ){
83704 if( eOp==SAVEPOINT_ROLLBACK2 ){
83705 rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK2, iSavepoint);
83706 }
83707 if( rc==SQLITE_OK0 ){
83708 rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE1, iSavepoint);
83709 }
83710 }
83711
83712 /* If the statement transaction is being rolled back, also restore the
83713 ** database handles deferred constraint counter to the value it had when
83714 ** the statement transaction was opened. */
83715 if( eOp==SAVEPOINT_ROLLBACK2 ){
83716 db->nDeferredCons = p->nStmtDefCons;
83717 db->nDeferredImmCons = p->nStmtDefImmCons;
83718 }
83719 return rc;
83720}
83721SQLITE_PRIVATEstatic int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
83722 if( p->db->nStatement && p->iStatement ){
83723 return vdbeCloseStatement(p, eOp);
83724 }
83725 return SQLITE_OK0;
83726}
83727
83728
83729/*
83730** This function is called when a transaction opened by the database
83731** handle associated with the VM passed as an argument is about to be
83732** committed. If there are outstanding deferred foreign key constraint
83733** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
83734**
83735** If there are outstanding FK violations and this function returns
83736** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
83737** and write an error message to it. Then return SQLITE_ERROR.
83738*/
83739#ifndef SQLITE_OMIT_FOREIGN_KEY
83740SQLITE_PRIVATEstatic int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
83741 sqlite3 *db = p->db;
83742 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
83743 || (!deferred && p->nFkConstraint>0)
83744 ){
83745 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8));
83746 p->errorAction = OE_Abort2;
83747 sqlite3VdbeError(p, "FOREIGN KEY constraint failed");
83748 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL0x80)==0 ) return SQLITE_ERROR1;
83749 return SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8));
83750 }
83751 return SQLITE_OK0;
83752}
83753#endif
83754
83755/*
83756** This routine is called the when a VDBE tries to halt. If the VDBE
83757** has made changes and is in autocommit mode, then commit those
83758** changes. If a rollback is needed, then do the rollback.
83759**
83760** This routine is the only way to move the sqlite3eOpenState of a VM from
83761** SQLITE_STATE_RUN to SQLITE_STATE_HALT. It is harmless to
83762** call this on a VM that is in the SQLITE_STATE_HALT state.
83763**
83764** Return an error code. If the commit could not complete because of
83765** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
83766** means the close did not happen and needs to be repeated.
83767*/
83768SQLITE_PRIVATEstatic int sqlite3VdbeHalt(Vdbe *p){
83769 int rc; /* Used to store transient return codes */
83770 sqlite3 *db = p->db;
83771
83772 /* This function contains the logic that determines if a statement or
83773 ** transaction will be committed or rolled back as a result of the
83774 ** execution of this virtual machine.
83775 **
83776 ** If any of the following errors occur:
83777 **
83778 ** SQLITE_NOMEM
83779 ** SQLITE_IOERR
83780 ** SQLITE_FULL
83781 ** SQLITE_INTERRUPT
83782 **
83783 ** Then the internal cache might have been left in an inconsistent
83784 ** state. We need to rollback the statement transaction, if there is
83785 ** one, or the complete transaction if there is no statement transaction.
83786 */
83787
83788 assert( p->eVdbeState==VDBE_RUN_STATE )((void) (0));
83789 if( db->mallocFailed ){
83790 p->rc = SQLITE_NOMEM_BKPT7;
83791 }
83792 closeAllCursors(p);
83793 checkActiveVdbeCnt(db);
83794
83795 /* No commit or rollback needed if the program never started or if the
83796 ** SQL statement does not read or write a database file. */
83797 if( p->bIsReader ){
83798 int mrc; /* Primary error code from p->rc */
83799 int eStatementOp = 0;
83800 int isSpecialError; /* Set to true if a 'special' error */
83801
83802 /* Lock all btrees used by the statement */
83803 sqlite3VdbeEnter(p);
83804
83805 /* Check for one of the special errors */
83806 if( p->rc ){
83807 mrc = p->rc & 0xff;
83808 isSpecialError = mrc==SQLITE_NOMEM7
83809 || mrc==SQLITE_IOERR10
83810 || mrc==SQLITE_INTERRUPT9
83811 || mrc==SQLITE_FULL13;
83812 }else{
83813 mrc = isSpecialError = 0;
83814 }
83815 if( isSpecialError ){
83816 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
83817 ** no rollback is necessary. Otherwise, at least a savepoint
83818 ** transaction must be rolled back to restore the database to a
83819 ** consistent state.
83820 **
83821 ** Even if the statement is read-only, it is important to perform
83822 ** a statement or transaction rollback operation. If the error
83823 ** occurred while writing to the journal, sub-journal or database
83824 ** file as part of an effort to free up cache space (see function
83825 ** pagerStress() in pager.c), the rollback is required to restore
83826 ** the pager to a consistent state.
83827 */
83828 if( !p->readOnly || mrc!=SQLITE_INTERRUPT9 ){
83829 if( (mrc==SQLITE_NOMEM7 || mrc==SQLITE_FULL13) && p->usesStmtJournal ){
83830 eStatementOp = SAVEPOINT_ROLLBACK2;
83831 }else{
83832 /* We are forced to roll back the active transaction. Before doing
83833 ** so, abort any other statements this handle currently has active.
83834 */
83835 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK(4 | (2<<8)));
83836 sqlite3CloseSavepoints(db);
83837 db->autoCommit = 1;
83838 p->nChange = 0;
83839 }
83840 }
83841 }
83842
83843 /* Check for immediate foreign key violations. */
83844 if( p->rc==SQLITE_OK0 || (p->errorAction==OE_Fail3 && !isSpecialError) ){
83845 sqlite3VdbeCheckFk(p, 0);
83846 }
83847
83848 /* If the auto-commit flag is set and this is the only active writer
83849 ** VM, then we do either a commit or rollback of the current transaction.
83850 **
83851 ** Note: This block also runs if one of the special errors handled
83852 ** above has occurred.
83853 */
83854 if( !sqlite3VtabInSync(db)((db)->nVTrans>0 && (db)->aVTrans==0)
83855 && db->autoCommit
83856 && db->nVdbeWrite==(p->readOnly==0)
83857 ){
83858 if( p->rc==SQLITE_OK0 || (p->errorAction==OE_Fail3 && !isSpecialError) ){
83859 rc = sqlite3VdbeCheckFk(p, 1);
83860 if( rc!=SQLITE_OK0 ){
83861 if( NEVER(p->readOnly)(p->readOnly) ){
83862 sqlite3VdbeLeave(p);
83863 return SQLITE_ERROR1;
83864 }
83865 rc = SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8));
83866 }else if( db->flags & SQLITE_CorruptRdOnly((u64)(0x00002)<<32) ){
83867 rc = SQLITE_CORRUPT11;
83868 db->flags &= ~SQLITE_CorruptRdOnly((u64)(0x00002)<<32);
83869 }else{
83870 /* The auto-commit flag is true, the vdbe program was successful
83871 ** or hit an 'OR FAIL' constraint and there are no deferred foreign
83872 ** key constraints to hold up the transaction. This means a commit
83873 ** is required. */
83874 rc = vdbeCommit(db, p);
83875 }
83876 if( rc==SQLITE_BUSY5 && p->readOnly ){
83877 sqlite3VdbeLeave(p);
83878 return SQLITE_BUSY5;
83879 }else if( rc!=SQLITE_OK0 ){
83880 p->rc = rc;
83881 sqlite3RollbackAll(db, SQLITE_OK0);
83882 p->nChange = 0;
83883 }else{
83884 db->nDeferredCons = 0;
83885 db->nDeferredImmCons = 0;
83886 db->flags &= ~(u64)SQLITE_DeferFKs0x00080000;
83887 sqlite3CommitInternalChanges(db);
83888 }
83889 }else{
83890 sqlite3RollbackAll(db, SQLITE_OK0);
83891 p->nChange = 0;
83892 }
83893 db->nStatement = 0;
83894 }else if( eStatementOp==0 ){
83895 if( p->rc==SQLITE_OK0 || p->errorAction==OE_Fail3 ){
83896 eStatementOp = SAVEPOINT_RELEASE1;
83897 }else if( p->errorAction==OE_Abort2 ){
83898 eStatementOp = SAVEPOINT_ROLLBACK2;
83899 }else{
83900 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK(4 | (2<<8)));
83901 sqlite3CloseSavepoints(db);
83902 db->autoCommit = 1;
83903 p->nChange = 0;
83904 }
83905 }
83906
83907 /* If eStatementOp is non-zero, then a statement transaction needs to
83908 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
83909 ** do so. If this operation returns an error, and the current statement
83910 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
83911 ** current statement error code.
83912 */
83913 if( eStatementOp ){
83914 rc = sqlite3VdbeCloseStatement(p, eStatementOp);
83915 if( rc ){
83916 if( p->rc==SQLITE_OK0 || (p->rc&0xff)==SQLITE_CONSTRAINT19 ){
83917 p->rc = rc;
83918 sqlite3DbFree(db, p->zErrMsg);
83919 p->zErrMsg = 0;
83920 }
83921 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK(4 | (2<<8)));
83922 sqlite3CloseSavepoints(db);
83923 db->autoCommit = 1;
83924 p->nChange = 0;
83925 }
83926 }
83927
83928 /* If this was an INSERT, UPDATE or DELETE and no statement transaction
83929 ** has been rolled back, update the database connection change-counter.
83930 */
83931 if( p->changeCntOn ){
83932 if( eStatementOp!=SAVEPOINT_ROLLBACK2 ){
83933 sqlite3VdbeSetChanges(db, p->nChange);
83934 }else{
83935 sqlite3VdbeSetChanges(db, 0);
83936 }
83937 p->nChange = 0;
83938 }
83939
83940 /* Release the locks */
83941 sqlite3VdbeLeave(p);
83942 }
83943
83944 /* We have successfully halted and closed the VM. Record this fact. */
83945 db->nVdbeActive--;
83946 if( !p->readOnly ) db->nVdbeWrite--;
83947 if( p->bIsReader ) db->nVdbeRead--;
83948 assert( db->nVdbeActive>=db->nVdbeRead )((void) (0));
83949 assert( db->nVdbeRead>=db->nVdbeWrite )((void) (0));
83950 assert( db->nVdbeWrite>=0 )((void) (0));
83951 p->eVdbeState = VDBE_HALT_STATE3;
83952 checkActiveVdbeCnt(db);
83953 if( db->mallocFailed ){
83954 p->rc = SQLITE_NOMEM_BKPT7;
83955 }
83956
83957 /* If the auto-commit flag is set to true, then any locks that were held
83958 ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
83959 ** to invoke any required unlock-notify callbacks.
83960 */
83961 if( db->autoCommit ){
83962 sqlite3ConnectionUnlocked(db);
83963 }
83964
83965 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 )((void) (0));
83966 return (p->rc==SQLITE_BUSY5 ? SQLITE_BUSY5 : SQLITE_OK0);
83967}
83968
83969
83970/*
83971** Each VDBE holds the result of the most recent sqlite3_step() call
83972** in p->rc. This routine sets that result back to SQLITE_OK.
83973*/
83974SQLITE_PRIVATEstatic void sqlite3VdbeResetStepResult(Vdbe *p){
83975 p->rc = SQLITE_OK0;
83976}
83977
83978/*
83979** Copy the error code and error message belonging to the VDBE passed
83980** as the first argument to its database handle (so that they will be
83981** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
83982**
83983** This function does not clear the VDBE error code or message, just
83984** copies them to the database handle.
83985*/
83986SQLITE_PRIVATEstatic int sqlite3VdbeTransferError(Vdbe *p){
83987 sqlite3 *db = p->db;
83988 int rc = p->rc;
83989 if( p->zErrMsg ){
83990 db->bBenignMalloc++;
83991 sqlite3BeginBenignMalloc();
83992 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
83993 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF81, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
83994 sqlite3EndBenignMalloc();
83995 db->bBenignMalloc--;
83996 }else if( db->pErr ){
83997 sqlite3ValueSetNull(db->pErr);
83998 }
83999 db->errCode = rc;
84000 db->errByteOffset = -1;
84001 return rc;
84002}
84003
84004#ifdef SQLITE_ENABLE_SQLLOG
84005/*
84006** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run,
84007** invoke it.
84008*/
84009static void vdbeInvokeSqllog(Vdbe *v){
84010 if( sqlite3GlobalConfigsqlite3Config.xSqllog && v->rc==SQLITE_OK0 && v->zSql && v->pc>=0 ){
84011 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
84012 assert( v->db->init.busy==0 )((void) (0));
84013 if( zExpanded ){
84014 sqlite3GlobalConfigsqlite3Config.xSqllog(
84015 sqlite3GlobalConfigsqlite3Config.pSqllogArg, v->db, zExpanded, 1
84016 );
84017 sqlite3DbFree(v->db, zExpanded);
84018 }
84019 }
84020}
84021#else
84022# define vdbeInvokeSqllog(x)
84023#endif
84024
84025/*
84026** Clean up a VDBE after execution but do not delete the VDBE just yet.
84027** Write any error messages into *pzErrMsg. Return the result code.
84028**
84029** After this routine is run, the VDBE should be ready to be executed
84030** again.
84031**
84032** To look at it another way, this routine resets the state of the
84033** virtual machine from VDBE_RUN_STATE or VDBE_HALT_STATE back to
84034** VDBE_READY_STATE.
84035*/
84036SQLITE_PRIVATEstatic int sqlite3VdbeReset(Vdbe *p){
84037#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
84038 int i;
84039#endif
84040
84041 sqlite3 *db;
84042 db = p->db;
84043
84044 /* If the VM did not run to completion or if it encountered an
84045 ** error, then it might not have been halted properly. So halt
84046 ** it now.
84047 */
84048 if( p->eVdbeState==VDBE_RUN_STATE2 ) sqlite3VdbeHalt(p);
84049
84050 /* If the VDBE has been run even partially, then transfer the error code
84051 ** and error message from the VDBE into the main database structure. But
84052 ** if the VDBE has just been set to run but has not actually executed any
84053 ** instructions yet, leave the main database error information unchanged.
84054 */
84055 if( p->pc>=0 ){
84056 vdbeInvokeSqllog(p);
84057 if( db->pErr || p->zErrMsg ){
84058 sqlite3VdbeTransferError(p);
84059 }else{
84060 db->errCode = p->rc;
84061 }
84062 }
84063
84064 /* Reset register contents and reclaim error message memory.
84065 */
84066#ifdef SQLITE_DEBUG
84067 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
84068 ** Vdbe.aMem[] arrays have already been cleaned up. */
84069 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 )((void) (0));
84070 if( p->aMem ){
84071 for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined )((void) (0));
84072 }
84073#endif
84074 if( p->zErrMsg ){
84075 sqlite3DbFree(db, p->zErrMsg);
84076 p->zErrMsg = 0;
84077 }
84078 p->pResultSet = 0;
84079#ifdef SQLITE_DEBUG
84080 p->nWrite = 0;
84081#endif
84082
84083 /* Save profiling information from this VDBE run.
84084 */
84085#ifdef VDBE_PROFILE
84086 {
84087 FILE *out = fopen("vdbe_profile.out", "a");
84088 if( out ){
84089 fprintf(out, "---- ");
84090 for(i=0; i<p->nOp; i++){
84091 fprintf(out, "%02x", p->aOp[i].opcode);
84092 }
84093 fprintf(out, "\n");
84094 if( p->zSql ){
84095 char c, pc = 0;
84096 fprintf(out, "-- ");
84097 for(i=0; (c = p->zSql[i])!=0; i++){
84098 if( pc=='\n' ) fprintf(out, "-- ");
84099 putc(c, out);
84100 pc = c;
84101 }
84102 if( pc!='\n' ) fprintf(out, "\n");
84103 }
84104 for(i=0; i<p->nOp; i++){
84105 char zHdr[100];
84106 sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ",
84107 p->aOp[i].cnt,
84108 p->aOp[i].cycles,
84109 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
84110 );
84111 fprintf(out, "%s", zHdr);
84112 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
84113 }
84114 fclose(out);
84115 }
84116 }
84117#endif
84118 return p->rc & db->errMask;
84119}
84120
84121/*
84122** Clean up and delete a VDBE after execution. Return an integer which is
84123** the result code. Write any error message text into *pzErrMsg.
84124*/
84125SQLITE_PRIVATEstatic int sqlite3VdbeFinalize(Vdbe *p){
84126 int rc = SQLITE_OK0;
84127 assert( VDBE_RUN_STATE>VDBE_READY_STATE )((void) (0));
84128 assert( VDBE_HALT_STATE>VDBE_READY_STATE )((void) (0));
84129 assert( VDBE_INIT_STATE<VDBE_READY_STATE )((void) (0));
84130 if( p->eVdbeState>=VDBE_READY_STATE1 ){
84131 rc = sqlite3VdbeReset(p);
84132 assert( (rc & p->db->errMask)==rc )((void) (0));
84133 }
84134 sqlite3VdbeDelete(p);
84135 return rc;
84136}
84137
84138/*
84139** If parameter iOp is less than zero, then invoke the destructor for
84140** all auxiliary data pointers currently cached by the VM passed as
84141** the first argument.
84142**
84143** Or, if iOp is greater than or equal to zero, then the destructor is
84144** only invoked for those auxiliary data pointers created by the user
84145** function invoked by the OP_Function opcode at instruction iOp of
84146** VM pVdbe, and only then if:
84147**
84148** * the associated function parameter is the 32nd or later (counting
84149** from left to right), or
84150**
84151** * the corresponding bit in argument mask is clear (where the first
84152** function parameter corresponds to bit 0 etc.).
84153*/
84154SQLITE_PRIVATEstatic void sqlite3VdbeDeleteAuxData(sqlite3 *db, AuxData **pp, int iOp, int mask){
84155 while( *pp ){
84156 AuxData *pAux = *pp;
84157 if( (iOp<0)
84158 || (pAux->iAuxOp==iOp
84159 && pAux->iAuxArg>=0
84160 && (pAux->iAuxArg>31 || !(mask & MASKBIT32(pAux->iAuxArg)(((unsigned int)1)<<(pAux->iAuxArg)))))
84161 ){
84162 testcase( pAux->iAuxArg==31 );
84163 if( pAux->xDeleteAux ){
84164 pAux->xDeleteAux(pAux->pAux);
84165 }
84166 *pp = pAux->pNextAux;
84167 sqlite3DbFree(db, pAux);
84168 }else{
84169 pp= &pAux->pNextAux;
84170 }
84171 }
84172}
84173
84174/*
84175** Free all memory associated with the Vdbe passed as the second argument,
84176** except for object itself, which is preserved.
84177**
84178** The difference between this function and sqlite3VdbeDelete() is that
84179** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
84180** the database connection and frees the object itself.
84181*/
84182static void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
84183 SubProgram *pSub, *pNext;
84184 assert( p->db==0 || p->db==db )((void) (0));
84185 if( p->aColName ){
84186 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N5);
84187 sqlite3DbFreeNN(db, p->aColName);
84188 }
84189 for(pSub=p->pProgram; pSub; pSub=pNext){
84190 pNext = pSub->pNext;
84191 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
84192 sqlite3DbFree(db, pSub);
84193 }
84194 if( p->eVdbeState!=VDBE_INIT_STATE0 ){
84195 releaseMemArray(p->aVar, p->nVar);
84196 if( p->pVList ) sqlite3DbFreeNN(db, p->pVList);
84197 if( p->pFree ) sqlite3DbFreeNN(db, p->pFree);
84198 }
84199 vdbeFreeOpArray(db, p->aOp, p->nOp);
84200 sqlite3DbFree(db, p->zSql);
84201#ifdef SQLITE_ENABLE_NORMALIZE
84202 sqlite3DbFree(db, p->zNormSql);
84203 {
84204 DblquoteStr *pThis, *pNext;
84205 for(pThis=p->pDblStr; pThis; pThis=pNext){
84206 pNext = pThis->pNextStr;
84207 sqlite3DbFree(db, pThis);
84208 }
84209 }
84210#endif
84211#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
84212 {
84213 int i;
84214 for(i=0; i<p->nScan; i++){
84215 sqlite3DbFree(db, p->aScan[i].zName);
84216 }
84217 sqlite3DbFree(db, p->aScan);
84218 }
84219#endif
84220}
84221
84222/*
84223** Delete an entire VDBE.
84224*/
84225SQLITE_PRIVATEstatic void sqlite3VdbeDelete(Vdbe *p){
84226 sqlite3 *db;
84227
84228 assert( p!=0 )((void) (0));
84229 db = p->db;
84230 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
84231 sqlite3VdbeClearObject(db, p);
84232 if( db->pnBytesFreed==0 ){
84233 if( p->pPrev ){
84234 p->pPrev->pNext = p->pNext;
84235 }else{
84236 assert( db->pVdbe==p )((void) (0));
84237 db->pVdbe = p->pNext;
84238 }
84239 if( p->pNext ){
84240 p->pNext->pPrev = p->pPrev;
84241 }
84242 }
84243 sqlite3DbFreeNN(db, p);
84244}
84245
84246/*
84247** The cursor "p" has a pending seek operation that has not yet been
84248** carried out. Seek the cursor now. If an error occurs, return
84249** the appropriate error code.
84250*/
84251SQLITE_PRIVATEstatic int SQLITE_NOINLINE__attribute__((noinline)) sqlite3VdbeFinishMoveto(VdbeCursor *p){
84252 int res, rc;
84253#ifdef SQLITE_TEST
84254 extern int sqlite3_search_count;
84255#endif
84256 assert( p->deferredMoveto )((void) (0));
84257 assert( p->isTable )((void) (0));
84258 assert( p->eCurType==CURTYPE_BTREE )((void) (0));
84259 rc = sqlite3BtreeTableMoveto(p->uc.pCursor, p->movetoTarget, 0, &res);
84260 if( rc ) return rc;
84261 if( res!=0 ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(84261);
84262#ifdef SQLITE_TEST
84263 sqlite3_search_count++;
84264#endif
84265 p->deferredMoveto = 0;
84266 p->cacheStatus = CACHE_STALE0;
84267 return SQLITE_OK0;
84268}
84269
84270/*
84271** Something has moved cursor "p" out of place. Maybe the row it was
84272** pointed to was deleted out from under it. Or maybe the btree was
84273** rebalanced. Whatever the cause, try to restore "p" to the place it
84274** is supposed to be pointing. If the row was deleted out from under the
84275** cursor, set the cursor to point to a NULL row.
84276*/
84277SQLITE_PRIVATEstatic int SQLITE_NOINLINE__attribute__((noinline)) sqlite3VdbeHandleMovedCursor(VdbeCursor *p){
84278 int isDifferentRow, rc;
84279 assert( p->eCurType==CURTYPE_BTREE )((void) (0));
84280 assert( p->uc.pCursor!=0 )((void) (0));
84281 assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) )((void) (0));
84282 rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
84283 p->cacheStatus = CACHE_STALE0;
84284 if( isDifferentRow ) p->nullRow = 1;
84285 return rc;
84286}
84287
84288/*
84289** Check to ensure that the cursor is valid. Restore the cursor
84290** if need be. Return any I/O error from the restore operation.
84291*/
84292SQLITE_PRIVATEstatic int sqlite3VdbeCursorRestore(VdbeCursor *p){
84293 assert( p->eCurType==CURTYPE_BTREE || IsNullCursor(p) )((void) (0));
84294 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
84295 return sqlite3VdbeHandleMovedCursor(p);
84296 }
84297 return SQLITE_OK0;
84298}
84299
84300/*
84301** The following functions:
84302**
84303** sqlite3VdbeSerialType()
84304** sqlite3VdbeSerialTypeLen()
84305** sqlite3VdbeSerialLen()
84306** sqlite3VdbeSerialPut() <--- in-lined into OP_MakeRecord as of 2022-04-02
84307** sqlite3VdbeSerialGet()
84308**
84309** encapsulate the code that serializes values for storage in SQLite
84310** data and index records. Each serialized value consists of a
84311** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
84312** integer, stored as a varint.
84313**
84314** In an SQLite index record, the serial type is stored directly before
84315** the blob of data that it corresponds to. In a table record, all serial
84316** types are stored at the start of the record, and the blobs of data at
84317** the end. Hence these functions allow the caller to handle the
84318** serial-type and data blob separately.
84319**
84320** The following table describes the various storage classes for data:
84321**
84322** serial type bytes of data type
84323** -------------- --------------- ---------------
84324** 0 0 NULL
84325** 1 1 signed integer
84326** 2 2 signed integer
84327** 3 3 signed integer
84328** 4 4 signed integer
84329** 5 6 signed integer
84330** 6 8 signed integer
84331** 7 8 IEEE float
84332** 8 0 Integer constant 0
84333** 9 0 Integer constant 1
84334** 10,11 reserved for expansion
84335** N>=12 and even (N-12)/2 BLOB
84336** N>=13 and odd (N-13)/2 text
84337**
84338** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
84339** of SQLite will not understand those serial types.
84340*/
84341
84342#if 0 /* Inlined into the OP_MakeRecord opcode */
84343/*
84344** Return the serial-type for the value stored in pMem.
84345**
84346** This routine might convert a large MEM_IntReal value into MEM_Real.
84347**
84348** 2019-07-11: The primary user of this subroutine was the OP_MakeRecord
84349** opcode in the byte-code engine. But by moving this routine in-line, we
84350** can omit some redundant tests and make that opcode a lot faster. So
84351** this routine is now only used by the STAT3 logic and STAT3 support has
84352** ended. The code is kept here for historical reference only.
84353*/
84354SQLITE_PRIVATEstatic u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){
84355 int flags = pMem->flags;
84356 u32 n;
84357
84358 assert( pLen!=0 )((void) (0));
84359 if( flags&MEM_Null0x0001 ){
84360 *pLen = 0;
84361 return 0;
84362 }
84363 if( flags&(MEM_Int0x0004|MEM_IntReal0x0020) ){
84364 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
84365# define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
84366 i64 i = pMem->u.i;
84367 u64 u;
84368 testcase( flags & MEM_Int );
84369 testcase( flags & MEM_IntReal );
84370 if( i<0 ){
84371 u = ~i;
84372 }else{
84373 u = i;
84374 }
84375 if( u<=127 ){
84376 if( (i&1)==i && file_format>=4 ){
84377 *pLen = 0;
84378 return 8+(u32)u;
84379 }else{
84380 *pLen = 1;
84381 return 1;
84382 }
84383 }
84384 if( u<=32767 ){ *pLen = 2; return 2; }
84385 if( u<=8388607 ){ *pLen = 3; return 3; }
84386 if( u<=2147483647 ){ *pLen = 4; return 4; }
84387 if( u<=MAX_6BYTE ){ *pLen = 6; return 5; }
84388 *pLen = 8;
84389 if( flags&MEM_IntReal0x0020 ){
84390 /* If the value is IntReal and is going to take up 8 bytes to store
84391 ** as an integer, then we might as well make it an 8-byte floating
84392 ** point value */
84393 pMem->u.r = (double)pMem->u.i;
84394 pMem->flags &= ~MEM_IntReal0x0020;
84395 pMem->flags |= MEM_Real0x0008;
84396 return 7;
84397 }
84398 return 6;
84399 }
84400 if( flags&MEM_Real0x0008 ){
84401 *pLen = 8;
84402 return 7;
84403 }
84404 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) )((void) (0));
84405 assert( pMem->n>=0 )((void) (0));
84406 n = (u32)pMem->n;
84407 if( flags & MEM_Zero0x0400 ){
84408 n += pMem->u.nZero;
84409 }
84410 *pLen = n;
84411 return ((n*2) + 12 + ((flags&MEM_Str0x0002)!=0));
84412}
84413#endif /* inlined into OP_MakeRecord */
84414
84415/*
84416** The sizes for serial types less than 128
84417*/
84418SQLITE_PRIVATEstatic const u8 sqlite3SmallTypeSizes[128] = {
84419 /* 0 1 2 3 4 5 6 7 8 9 */
84420/* 0 */ 0, 1, 2, 3, 4, 6, 8, 8, 0, 0,
84421/* 10 */ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
84422/* 20 */ 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
84423/* 30 */ 9, 9, 10, 10, 11, 11, 12, 12, 13, 13,
84424/* 40 */ 14, 14, 15, 15, 16, 16, 17, 17, 18, 18,
84425/* 50 */ 19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
84426/* 60 */ 24, 24, 25, 25, 26, 26, 27, 27, 28, 28,
84427/* 70 */ 29, 29, 30, 30, 31, 31, 32, 32, 33, 33,
84428/* 80 */ 34, 34, 35, 35, 36, 36, 37, 37, 38, 38,
84429/* 90 */ 39, 39, 40, 40, 41, 41, 42, 42, 43, 43,
84430/* 100 */ 44, 44, 45, 45, 46, 46, 47, 47, 48, 48,
84431/* 110 */ 49, 49, 50, 50, 51, 51, 52, 52, 53, 53,
84432/* 120 */ 54, 54, 55, 55, 56, 56, 57, 57
84433};
84434
84435/*
84436** Return the length of the data corresponding to the supplied serial-type.
84437*/
84438SQLITE_PRIVATEstatic u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
84439 if( serial_type>=128 ){
84440 return (serial_type-12)/2;
84441 }else{
84442 assert( serial_type<12((void) (0))
84443 || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 )((void) (0));
84444 return sqlite3SmallTypeSizes[serial_type];
84445 }
84446}
84447SQLITE_PRIVATEstatic u8 sqlite3VdbeOneByteSerialTypeLen(u8 serial_type){
84448 assert( serial_type<128 )((void) (0));
84449 return sqlite3SmallTypeSizes[serial_type];
84450}
84451
84452/*
84453** If we are on an architecture with mixed-endian floating
84454** points (ex: ARM7) then swap the lower 4 bytes with the
84455** upper 4 bytes. Return the result.
84456**
84457** For most architectures, this is a no-op.
84458**
84459** (later): It is reported to me that the mixed-endian problem
84460** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
84461** that early versions of GCC stored the two words of a 64-bit
84462** float in the wrong order. And that error has been propagated
84463** ever since. The blame is not necessarily with GCC, though.
84464** GCC might have just copying the problem from a prior compiler.
84465** I am also told that newer versions of GCC that follow a different
84466** ABI get the byte order right.
84467**
84468** Developers using SQLite on an ARM7 should compile and run their
84469** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
84470** enabled, some asserts below will ensure that the byte order of
84471** floating point values is correct.
84472**
84473** (2007-08-30) Frank van Vugt has studied this problem closely
84474** and has send his findings to the SQLite developers. Frank
84475** writes that some Linux kernels offer floating point hardware
84476** emulation that uses only 32-bit mantissas instead of a full
84477** 48-bits as required by the IEEE standard. (This is the
84478** CONFIG_FPE_FASTFPE option.) On such systems, floating point
84479** byte swapping becomes very complicated. To avoid problems,
84480** the necessary byte swapping is carried out using a 64-bit integer
84481** rather than a 64-bit float. Frank assures us that the code here
84482** works for him. We, the developers, have no way to independently
84483** verify this, but Frank seems to know what he is talking about
84484** so we trust him.
84485*/
84486#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
84487SQLITE_PRIVATEstatic u64 sqlite3FloatSwap(u64 in){
84488 union {
84489 u64 r;
84490 u32 i[2];
84491 } u;
84492 u32 t;
84493
84494 u.r = in;
84495 t = u.i[0];
84496 u.i[0] = u.i[1];
84497 u.i[1] = t;
84498 return u.r;
84499}
84500#endif /* SQLITE_MIXED_ENDIAN_64BIT_FLOAT */
84501
84502
84503/* Input "x" is a sequence of unsigned characters that represent a
84504** big-endian integer. Return the equivalent native integer
84505*/
84506#define ONE_BYTE_INT(x)((i8)(x)[0]) ((i8)(x)[0])
84507#define TWO_BYTE_INT(x)(256*(i8)((x)[0])|(x)[1]) (256*(i8)((x)[0])|(x)[1])
84508#define THREE_BYTE_INT(x)(65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2]) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
84509#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])
84510#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])
84511
84512/*
84513** Deserialize the data blob pointed to by buf as serial type serial_type
84514** and store the result in pMem.
84515**
84516** This function is implemented as two separate routines for performance.
84517** The few cases that require local variables are broken out into a separate
84518** routine so that in most cases the overhead of moving the stack pointer
84519** is avoided.
84520*/
84521static void serialGet(
84522 const unsigned char *buf, /* Buffer to deserialize from */
84523 u32 serial_type, /* Serial type to deserialize */
84524 Mem *pMem /* Memory cell to write value into */
84525){
84526 u64 x = FOUR_BYTE_UINT(buf)(((u32)(buf)[0]<<24)|((buf)[1]<<16)|((buf)[2]<<
8)|(buf)[3])
;
84527 u32 y = FOUR_BYTE_UINT(buf+4)(((u32)(buf+4)[0]<<24)|((buf+4)[1]<<16)|((buf+4)[
2]<<8)|(buf+4)[3])
;
84528 x = (x<<32) + y;
84529 if( serial_type==6 ){
84530 /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
84531 ** twos-complement integer. */
84532 pMem->u.i = *(i64*)&x;
84533 pMem->flags = MEM_Int0x0004;
84534 testcase( pMem->u.i<0 );
84535 }else{
84536 /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
84537 ** floating point number. */
84538#if !defined(NDEBUG1) && !defined(SQLITE_OMIT_FLOATING_POINT)
84539 /* Verify that integers and floating point values use the same
84540 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
84541 ** defined that 64-bit floating point values really are mixed
84542 ** endian.
84543 */
84544 static const u64 t1 = ((u64)0x3ff00000)<<32;
84545 static const double r1 = 1.0;
84546 u64 t2 = t1;
84547 swapMixedEndianFloat(t2);
84548 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 )((void) (0));
84549#endif
84550 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 )((void) (0));
84551 swapMixedEndianFloat(x);
84552 memcpy(&pMem->u.r, &x, sizeof(x));
84553 pMem->flags = IsNaN(x)(((x)&(((u64)0x7ff)<<52))==(((u64)0x7ff)<<52)
&& ((x)&((((u64)1)<<52)-1))!=0)
? MEM_Null0x0001 : MEM_Real0x0008;
84554 }
84555}
84556SQLITE_PRIVATEstatic void sqlite3VdbeSerialGet(
84557 const unsigned char *buf, /* Buffer to deserialize from */
84558 u32 serial_type, /* Serial type to deserialize */
84559 Mem *pMem /* Memory cell to write value into */
84560){
84561 switch( serial_type ){
84562 case 10: { /* Internal use only: NULL with virtual table
84563 ** UPDATE no-change flag set */
84564 pMem->flags = MEM_Null0x0001|MEM_Zero0x0400;
84565 pMem->n = 0;
84566 pMem->u.nZero = 0;
84567 return;
84568 }
84569 case 11: /* Reserved for future use */
84570 case 0: { /* Null */
84571 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
84572 pMem->flags = MEM_Null0x0001;
84573 return;
84574 }
84575 case 1: {
84576 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
84577 ** integer. */
84578 pMem->u.i = ONE_BYTE_INT(buf)((i8)(buf)[0]);
84579 pMem->flags = MEM_Int0x0004;
84580 testcase( pMem->u.i<0 );
84581 return;
84582 }
84583 case 2: { /* 2-byte signed integer */
84584 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
84585 ** twos-complement integer. */
84586 pMem->u.i = TWO_BYTE_INT(buf)(256*(i8)((buf)[0])|(buf)[1]);
84587 pMem->flags = MEM_Int0x0004;
84588 testcase( pMem->u.i<0 );
84589 return;
84590 }
84591 case 3: { /* 3-byte signed integer */
84592 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
84593 ** twos-complement integer. */
84594 pMem->u.i = THREE_BYTE_INT(buf)(65536*(i8)((buf)[0])|((buf)[1]<<8)|(buf)[2]);
84595 pMem->flags = MEM_Int0x0004;
84596 testcase( pMem->u.i<0 );
84597 return;
84598 }
84599 case 4: { /* 4-byte signed integer */
84600 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
84601 ** twos-complement integer. */
84602 pMem->u.i = FOUR_BYTE_INT(buf)(16777216*(i8)((buf)[0])|((buf)[1]<<16)|((buf)[2]<<
8)|(buf)[3])
;
84603#ifdef __HP_cc
84604 /* Work around a sign-extension bug in the HP compiler for HP/UX */
84605 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
84606#endif
84607 pMem->flags = MEM_Int0x0004;
84608 testcase( pMem->u.i<0 );
84609 return;
84610 }
84611 case 5: { /* 6-byte signed integer */
84612 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
84613 ** twos-complement integer. */
84614 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]);
84615 pMem->flags = MEM_Int0x0004;
84616 testcase( pMem->u.i<0 );
84617 return;
84618 }
84619 case 6: /* 8-byte signed integer */
84620 case 7: { /* IEEE floating point */
84621 /* These use local variables, so do them in a separate routine
84622 ** to avoid having to move the frame pointer in the common case */
84623 serialGet(buf,serial_type,pMem);
84624 return;
84625 }
84626 case 8: /* Integer 0 */
84627 case 9: { /* Integer 1 */
84628 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
84629 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
84630 pMem->u.i = serial_type-8;
84631 pMem->flags = MEM_Int0x0004;
84632 return;
84633 }
84634 default: {
84635 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
84636 ** length.
84637 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
84638 ** (N-13)/2 bytes in length. */
84639 static const u16 aFlag[] = { MEM_Blob0x0010|MEM_Ephem0x4000, MEM_Str0x0002|MEM_Ephem0x4000 };
84640 pMem->z = (char *)buf;
84641 pMem->n = (serial_type-12)/2;
84642 pMem->flags = aFlag[serial_type&1];
84643 return;
84644 }
84645 }
84646 return;
84647}
84648/*
84649** This routine is used to allocate sufficient space for an UnpackedRecord
84650** structure large enough to be used with sqlite3VdbeRecordUnpack() if
84651** the first argument is a pointer to KeyInfo structure pKeyInfo.
84652**
84653** The space is either allocated using sqlite3DbMallocRaw() or from within
84654** the unaligned buffer passed via the second and third arguments (presumably
84655** stack space). If the former, then *ppFree is set to a pointer that should
84656** be eventually freed by the caller using sqlite3DbFree(). Or, if the
84657** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
84658** before returning.
84659**
84660** If an OOM error occurs, NULL is returned.
84661*/
84662SQLITE_PRIVATEstatic UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
84663 KeyInfo *pKeyInfo /* Description of the record */
84664){
84665 UnpackedRecord *p; /* Unpacked record to return */
84666 int nByte; /* Number of bytes required for *p */
84667 nByte = ROUND8P(sizeof(UnpackedRecord))(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1);
84668 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
84669 if( !p ) return 0;
84670 p->aMem = (Mem*)&((char*)p)[ROUND8P(sizeof(UnpackedRecord))(sizeof(UnpackedRecord))];
84671 assert( pKeyInfo->aSortFlags!=0 )((void) (0));
84672 p->pKeyInfo = pKeyInfo;
84673 p->nField = pKeyInfo->nKeyField + 1;
84674 return p;
84675}
84676
84677/*
84678** Given the nKey-byte encoding of a record in pKey[], populate the
84679** UnpackedRecord structure indicated by the fourth argument with the
84680** contents of the decoded record.
84681*/
84682SQLITE_PRIVATEstatic void sqlite3VdbeRecordUnpack(
84683 KeyInfo *pKeyInfo, /* Information about the record format */
84684 int nKey, /* Size of the binary record */
84685 const void *pKey, /* The binary record */
84686 UnpackedRecord *p /* Populate this structure before returning. */
84687){
84688 const unsigned char *aKey = (const unsigned char *)pKey;
84689 u32 d;
84690 u32 idx; /* Offset in aKey[] to read from */
84691 u16 u; /* Unsigned loop counter */
84692 u32 szHdr;
84693 Mem *pMem = p->aMem;
84694
84695 p->default_rc = 0;
84696 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
84697 idx = getVarint32(aKey, szHdr)(u8)((*(aKey)<(u8)0x80)?((szHdr)=(u32)*(aKey)),1:sqlite3GetVarint32
((aKey),(u32 *)&(szHdr)))
;
84698 d = szHdr;
84699 u = 0;
84700 while( idx<szHdr && d<=(u32)nKey ){
84701 u32 serial_type;
84702
84703 idx += getVarint32(&aKey[idx], serial_type)(u8)((*(&aKey[idx])<(u8)0x80)?((serial_type)=(u32)*(&
aKey[idx])),1:sqlite3GetVarint32((&aKey[idx]),(u32 *)&
(serial_type)))
;
84704 pMem->enc = pKeyInfo->enc;
84705 pMem->db = pKeyInfo->db;
84706 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
84707 pMem->szMalloc = 0;
84708 pMem->z = 0;
84709 sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
84710 d += sqlite3VdbeSerialTypeLen(serial_type);
84711 pMem++;
84712 if( (++u)>=p->nField ) break;
84713 }
84714 if( d>(u32)nKey && u ){
84715 assert( CORRUPT_DB )((void) (0));
84716 /* In a corrupt record entry, the last pMem might have been set up using
84717 ** uninitialized memory. Overwrite its value with NULL, to prevent
84718 ** warnings from MSAN. */
84719 sqlite3VdbeMemSetNull(pMem-1);
84720 }
84721 assert( u<=pKeyInfo->nKeyField + 1 )((void) (0));
84722 p->nField = u;
84723}
84724
84725#ifdef SQLITE_DEBUG
84726/*
84727** This function compares two index or table record keys in the same way
84728** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
84729** this function deserializes and compares values using the
84730** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
84731** in assert() statements to ensure that the optimized code in
84732** sqlite3VdbeRecordCompare() returns results with these two primitives.
84733**
84734** Return true if the result of comparison is equivalent to desiredResult.
84735** Return false if there is a disagreement.
84736*/
84737static int vdbeRecordCompareDebug(
84738 int nKey1, const void *pKey1, /* Left key */
84739 const UnpackedRecord *pPKey2, /* Right key */
84740 int desiredResult /* Correct answer */
84741){
84742 u32 d1; /* Offset into aKey[] of next data element */
84743 u32 idx1; /* Offset into aKey[] of next header element */
84744 u32 szHdr1; /* Number of bytes in header */
84745 int i = 0;
84746 int rc = 0;
84747 const unsigned char *aKey1 = (const unsigned char *)pKey1;
84748 KeyInfo *pKeyInfo;
84749 Mem mem1;
84750
84751 pKeyInfo = pPKey2->pKeyInfo;
84752 if( pKeyInfo->db==0 ) return 1;
84753 mem1.enc = pKeyInfo->enc;
84754 mem1.db = pKeyInfo->db;
84755 /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */
84756 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
84757
84758 /* Compilers may complain that mem1.u.i is potentially uninitialized.
84759 ** We could initialize it, as shown here, to silence those complaints.
84760 ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
84761 ** the unnecessary initialization has a measurable negative performance
84762 ** impact, since this routine is a very high runner. And so, we choose
84763 ** to ignore the compiler warnings and leave this variable uninitialized.
84764 */
84765 /* mem1.u.i = 0; // not needed, here to silence compiler warning */
84766
84767 idx1 = getVarint32(aKey1, szHdr1)(u8)((*(aKey1)<(u8)0x80)?((szHdr1)=(u32)*(aKey1)),1:sqlite3GetVarint32
((aKey1),(u32 *)&(szHdr1)))
;
84768 if( szHdr1>98307 ) return SQLITE_CORRUPT11;
84769 d1 = szHdr1;
84770 assert( pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB )((void) (0));
84771 assert( pKeyInfo->aSortFlags!=0 )((void) (0));
84772 assert( pKeyInfo->nKeyField>0 )((void) (0));
84773 assert( idx1<=szHdr1 || CORRUPT_DB )((void) (0));
84774 do{
84775 u32 serial_type1;
84776
84777 /* Read the serial types for the next element in each key. */
84778 idx1 += getVarint32( aKey1+idx1, serial_type1 )(u8)((*(aKey1+idx1)<(u8)0x80)?((serial_type1)=(u32)*(aKey1
+idx1)),1:sqlite3GetVarint32((aKey1+idx1),(u32 *)&(serial_type1
)))
;
84779
84780 /* Verify that there is enough key space remaining to avoid
84781 ** a buffer overread. The "d1+serial_type1+2" subexpression will
84782 ** always be greater than or equal to the amount of required key space.
84783 ** Use that approximation to avoid the more expensive call to
84784 ** sqlite3VdbeSerialTypeLen() in the common case.
84785 */
84786 if( d1+(u64)serial_type1+2>(u64)nKey1
84787 && d1+(u64)sqlite3VdbeSerialTypeLen(serial_type1)>(u64)nKey1
84788 ){
84789 break;
84790 }
84791
84792 /* Extract the values to be compared.
84793 */
84794 sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
84795 d1 += sqlite3VdbeSerialTypeLen(serial_type1);
84796
84797 /* Do the comparison
84798 */
84799 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
84800 pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0);
84801 if( rc!=0 ){
84802 assert( mem1.szMalloc==0 )((void) (0)); /* See comment below */
84803 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL0x02)
84804 && ((mem1.flags & MEM_Null0x0001) || (pPKey2->aMem[i].flags & MEM_Null0x0001))
84805 ){
84806 rc = -rc;
84807 }
84808 if( pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC0x01 ){
84809 rc = -rc; /* Invert the result for DESC sort order. */
84810 }
84811 goto debugCompareEnd;
84812 }
84813 i++;
84814 }while( idx1<szHdr1 && i<pPKey2->nField );
84815
84816 /* No memory allocation is ever used on mem1. Prove this using
84817 ** the following assert(). If the assert() fails, it indicates a
84818 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
84819 */
84820 assert( mem1.szMalloc==0 )((void) (0));
84821
84822 /* rc==0 here means that one of the keys ran out of fields and
84823 ** all the fields up to that point were equal. Return the default_rc
84824 ** value. */
84825 rc = pPKey2->default_rc;
84826
84827debugCompareEnd:
84828 if( desiredResult==0 && rc==0 ) return 1;
84829 if( desiredResult<0 && rc<0 ) return 1;
84830 if( desiredResult>0 && rc>0 ) return 1;
84831 if( CORRUPT_DB(sqlite3Config.neverCorrupt==0) ) return 1;
84832 if( pKeyInfo->db->mallocFailed ) return 1;
84833 return 0;
84834}
84835#endif
84836
84837#ifdef SQLITE_DEBUG
84838/*
84839** Count the number of fields (a.k.a. columns) in the record given by
84840** pKey,nKey. The verify that this count is less than or equal to the
84841** limit given by pKeyInfo->nAllField.
84842**
84843** If this constraint is not satisfied, it means that the high-speed
84844** vdbeRecordCompareInt() and vdbeRecordCompareString() routines will
84845** not work correctly. If this assert() ever fires, it probably means
84846** that the KeyInfo.nKeyField or KeyInfo.nAllField values were computed
84847** incorrectly.
84848*/
84849static void vdbeAssertFieldCountWithinLimits(
84850 int nKey, const void *pKey, /* The record to verify */
84851 const KeyInfo *pKeyInfo /* Compare size with this KeyInfo */
84852){
84853 int nField = 0;
84854 u32 szHdr;
84855 u32 idx;
84856 u32 notUsed;
84857 const unsigned char *aKey = (const unsigned char*)pKey;
84858
84859 if( CORRUPT_DB(sqlite3Config.neverCorrupt==0) ) return;
84860 idx = getVarint32(aKey, szHdr)(u8)((*(aKey)<(u8)0x80)?((szHdr)=(u32)*(aKey)),1:sqlite3GetVarint32
((aKey),(u32 *)&(szHdr)))
;
84861 assert( nKey>=0 )((void) (0));
84862 assert( szHdr<=(u32)nKey )((void) (0));
84863 while( idx<szHdr ){
84864 idx += getVarint32(aKey+idx, notUsed)(u8)((*(aKey+idx)<(u8)0x80)?((notUsed)=(u32)*(aKey+idx)),1
:sqlite3GetVarint32((aKey+idx),(u32 *)&(notUsed)))
;
84865 nField++;
84866 }
84867 assert( nField <= pKeyInfo->nAllField )((void) (0));
84868}
84869#else
84870# define vdbeAssertFieldCountWithinLimits(A,B,C)
84871#endif
84872
84873/*
84874** Both *pMem1 and *pMem2 contain string values. Compare the two values
84875** using the collation sequence pColl. As usual, return a negative , zero
84876** or positive value if *pMem1 is less than, equal to or greater than
84877** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
84878*/
84879static int vdbeCompareMemString(
84880 const Mem *pMem1,
84881 const Mem *pMem2,
84882 const CollSeq *pColl,
84883 u8 *prcErr /* If an OOM occurs, set to SQLITE_NOMEM */
84884){
84885 if( pMem1->enc==pColl->enc ){
84886 /* The strings are already in the correct encoding. Call the
84887 ** comparison function directly */
84888 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
84889 }else{
84890 int rc;
84891 const void *v1, *v2;
84892 Mem c1;
84893 Mem c2;
84894 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null0x0001);
84895 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null0x0001);
84896 sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem0x4000);
84897 sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem0x4000);
84898 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
84899 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
84900 if( (v1==0 || v2==0) ){
84901 if( prcErr ) *prcErr = SQLITE_NOMEM_BKPT7;
84902 rc = 0;
84903 }else{
84904 rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2);
84905 }
84906 sqlite3VdbeMemReleaseMalloc(&c1);
84907 sqlite3VdbeMemReleaseMalloc(&c2);
84908 return rc;
84909 }
84910}
84911
84912/*
84913** The input pBlob is guaranteed to be a Blob that is not marked
84914** with MEM_Zero. Return true if it could be a zero-blob.
84915*/
84916static int isAllZero(const char *z, int n){
84917 int i;
84918 for(i=0; i<n; i++){
84919 if( z[i] ) return 0;
84920 }
84921 return 1;
84922}
84923
84924/*
84925** Compare two blobs. Return negative, zero, or positive if the first
84926** is less than, equal to, or greater than the second, respectively.
84927** If one blob is a prefix of the other, then the shorter is the lessor.
84928*/
84929SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){
84930 int c;
84931 int n1 = pB1->n;
84932 int n2 = pB2->n;
84933
84934 /* It is possible to have a Blob value that has some non-zero content
84935 ** followed by zero content. But that only comes up for Blobs formed
84936 ** by the OP_MakeRecord opcode, and such Blobs never get passed into
84937 ** sqlite3MemCompare(). */
84938 assert( (pB1->flags & MEM_Zero)==0 || n1==0 )((void) (0));
84939 assert( (pB2->flags & MEM_Zero)==0 || n2==0 )((void) (0));
84940
84941 if( (pB1->flags|pB2->flags) & MEM_Zero0x0400 ){
84942 if( pB1->flags & pB2->flags & MEM_Zero0x0400 ){
84943 return pB1->u.nZero - pB2->u.nZero;
84944 }else if( pB1->flags & MEM_Zero0x0400 ){
84945 if( !isAllZero(pB2->z, pB2->n) ) return -1;
84946 return pB1->u.nZero - n2;
84947 }else{
84948 if( !isAllZero(pB1->z, pB1->n) ) return +1;
84949 return n1 - pB2->u.nZero;
84950 }
84951 }
84952 c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1);
84953 if( c ) return c;
84954 return n1 - n2;
84955}
84956
84957/*
84958** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
84959** number. Return negative, zero, or positive if the first (i64) is less than,
84960** equal to, or greater than the second (double).
84961*/
84962SQLITE_PRIVATEstatic int sqlite3IntFloatCompare(i64 i, double r){
84963 if( sizeof(LONGDOUBLE_TYPElong double)>8 ){
84964 LONGDOUBLE_TYPElong double x = (LONGDOUBLE_TYPElong double)i;
84965 testcase( x<r );
84966 testcase( x>r );
84967 testcase( x==r );
84968 if( x<r ) return -1;
84969 if( x>r ) return +1; /*NO_TEST*/ /* work around bugs in gcov */
84970 return 0; /*NO_TEST*/ /* work around bugs in gcov */
84971 }else{
84972 i64 y;
84973 double s;
84974 if( r<-9223372036854775808.0 ) return +1;
84975 if( r>=9223372036854775808.0 ) return -1;
84976 y = (i64)r;
84977 if( i<y ) return -1;
84978 if( i>y ) return +1;
84979 s = (double)i;
84980 if( s<r ) return -1;
84981 if( s>r ) return +1;
84982 return 0;
84983 }
84984}
84985
84986/*
84987** Compare the values contained by the two memory cells, returning
84988** negative, zero or positive if pMem1 is less than, equal to, or greater
84989** than pMem2. Sorting order is NULL's first, followed by numbers (integers
84990** and reals) sorted numerically, followed by text ordered by the collating
84991** sequence pColl and finally blob's ordered by memcmp().
84992**
84993** Two NULL values are considered equal by this function.
84994*/
84995SQLITE_PRIVATEstatic int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
84996 int f1, f2;
84997 int combined_flags;
84998
84999 f1 = pMem1->flags;
85000 f2 = pMem2->flags;
85001 combined_flags = f1|f2;
85002 assert( !sqlite3VdbeMemIsRowSet(pMem1) && !sqlite3VdbeMemIsRowSet(pMem2) )((void) (0));
85003
85004 /* If one value is NULL, it is less than the other. If both values
85005 ** are NULL, return 0.
85006 */
85007 if( combined_flags&MEM_Null0x0001 ){
85008 return (f2&MEM_Null0x0001) - (f1&MEM_Null0x0001);
85009 }
85010
85011 /* At least one of the two values is a number
85012 */
85013 if( combined_flags&(MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020) ){
85014 testcase( combined_flags & MEM_Int );
85015 testcase( combined_flags & MEM_Real );
85016 testcase( combined_flags & MEM_IntReal );
85017 if( (f1 & f2 & (MEM_Int0x0004|MEM_IntReal0x0020))!=0 ){
85018 testcase( f1 & f2 & MEM_Int );
85019 testcase( f1 & f2 & MEM_IntReal );
85020 if( pMem1->u.i < pMem2->u.i ) return -1;
85021 if( pMem1->u.i > pMem2->u.i ) return +1;
85022 return 0;
85023 }
85024 if( (f1 & f2 & MEM_Real0x0008)!=0 ){
85025 if( pMem1->u.r < pMem2->u.r ) return -1;
85026 if( pMem1->u.r > pMem2->u.r ) return +1;
85027 return 0;
85028 }
85029 if( (f1&(MEM_Int0x0004|MEM_IntReal0x0020))!=0 ){
85030 testcase( f1 & MEM_Int );
85031 testcase( f1 & MEM_IntReal );
85032 if( (f2&MEM_Real0x0008)!=0 ){
85033 return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
85034 }else if( (f2&(MEM_Int0x0004|MEM_IntReal0x0020))!=0 ){
85035 if( pMem1->u.i < pMem2->u.i ) return -1;
85036 if( pMem1->u.i > pMem2->u.i ) return +1;
85037 return 0;
85038 }else{
85039 return -1;
85040 }
85041 }
85042 if( (f1&MEM_Real0x0008)!=0 ){
85043 if( (f2&(MEM_Int0x0004|MEM_IntReal0x0020))!=0 ){
85044 testcase( f2 & MEM_Int );
85045 testcase( f2 & MEM_IntReal );
85046 return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
85047 }else{
85048 return -1;
85049 }
85050 }
85051 return +1;
85052 }
85053
85054 /* If one value is a string and the other is a blob, the string is less.
85055 ** If both are strings, compare using the collating functions.
85056 */
85057 if( combined_flags&MEM_Str0x0002 ){
85058 if( (f1 & MEM_Str0x0002)==0 ){
85059 return 1;
85060 }
85061 if( (f2 & MEM_Str0x0002)==0 ){
85062 return -1;
85063 }
85064
85065 assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed )((void) (0));
85066 assert( pMem1->enc==SQLITE_UTF8 ||((void) (0))
85067 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE )((void) (0));
85068
85069 /* The collation sequence must be defined at this point, even if
85070 ** the user deletes the collation sequence after the vdbe program is
85071 ** compiled (this was not always the case).
85072 */
85073 assert( !pColl || pColl->xCmp )((void) (0));
85074
85075 if( pColl ){
85076 return vdbeCompareMemString(pMem1, pMem2, pColl, 0);
85077 }
85078 /* If a NULL pointer was passed as the collate function, fall through
85079 ** to the blob case and use memcmp(). */
85080 }
85081
85082 /* Both values must be blobs. Compare using memcmp(). */
85083 return sqlite3BlobCompare(pMem1, pMem2);
85084}
85085
85086
85087/*
85088** The first argument passed to this function is a serial-type that
85089** corresponds to an integer - all values between 1 and 9 inclusive
85090** except 7. The second points to a buffer containing an integer value
85091** serialized according to serial_type. This function deserializes
85092** and returns the value.
85093*/
85094static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
85095 u32 y;
85096 assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) )((void) (0));
85097 switch( serial_type ){
85098 case 0:
85099 case 1:
85100 testcase( aKey[0]&0x80 );
85101 return ONE_BYTE_INT(aKey)((i8)(aKey)[0]);
85102 case 2:
85103 testcase( aKey[0]&0x80 );
85104 return TWO_BYTE_INT(aKey)(256*(i8)((aKey)[0])|(aKey)[1]);
85105 case 3:
85106 testcase( aKey[0]&0x80 );
85107 return THREE_BYTE_INT(aKey)(65536*(i8)((aKey)[0])|((aKey)[1]<<8)|(aKey)[2]);
85108 case 4: {
85109 testcase( aKey[0]&0x80 );
85110 y = FOUR_BYTE_UINT(aKey)(((u32)(aKey)[0]<<24)|((aKey)[1]<<16)|((aKey)[2]<<
8)|(aKey)[3])
;
85111 return (i64)*(int*)&y;
85112 }
85113 case 5: {
85114 testcase( aKey[0]&0x80 );
85115 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]);
85116 }
85117 case 6: {
85118 u64 x = FOUR_BYTE_UINT(aKey)(((u32)(aKey)[0]<<24)|((aKey)[1]<<16)|((aKey)[2]<<
8)|(aKey)[3])
;
85119 testcase( aKey[0]&0x80 );
85120 x = (x<<32) | FOUR_BYTE_UINT(aKey+4)(((u32)(aKey+4)[0]<<24)|((aKey+4)[1]<<16)|((aKey+
4)[2]<<8)|(aKey+4)[3])
;
85121 return (i64)*(i64*)&x;
85122 }
85123 }
85124
85125 return (serial_type - 8);
85126}
85127
85128/*
85129** This function compares the two table rows or index records
85130** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
85131** or positive integer if key1 is less than, equal to or
85132** greater than key2. The {nKey1, pKey1} key must be a blob
85133** created by the OP_MakeRecord opcode of the VDBE. The pPKey2
85134** key must be a parsed key such as obtained from
85135** sqlite3VdbeParseRecord.
85136**
85137** If argument bSkip is non-zero, it is assumed that the caller has already
85138** determined that the first fields of the keys are equal.
85139**
85140** Key1 and Key2 do not have to contain the same number of fields. If all
85141** fields that appear in both keys are equal, then pPKey2->default_rc is
85142** returned.
85143**
85144** If database corruption is discovered, set pPKey2->errCode to
85145** SQLITE_CORRUPT and return 0. If an OOM error is encountered,
85146** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
85147** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
85148*/
85149SQLITE_PRIVATEstatic int sqlite3VdbeRecordCompareWithSkip(
85150 int nKey1, const void *pKey1, /* Left key */
85151 UnpackedRecord *pPKey2, /* Right key */
85152 int bSkip /* If true, skip the first field */
85153){
85154 u32 d1; /* Offset into aKey[] of next data element */
85155 int i; /* Index of next field to compare */
85156 u32 szHdr1; /* Size of record header in bytes */
85157 u32 idx1; /* Offset of first type in header */
85158 int rc = 0; /* Return value */
85159 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
85160 KeyInfo *pKeyInfo;
85161 const unsigned char *aKey1 = (const unsigned char *)pKey1;
85162 Mem mem1;
85163
85164 /* If bSkip is true, then the caller has already determined that the first
85165 ** two elements in the keys are equal. Fix the various stack variables so
85166 ** that this routine begins comparing at the second field. */
85167 if( bSkip ){
85168 u32 s1 = aKey1[1];
85169 if( s1<0x80 ){
85170 idx1 = 2;
85171 }else{
85172 idx1 = 1 + sqlite3GetVarint32(&aKey1[1], &s1);
85173 }
85174 szHdr1 = aKey1[0];
85175 d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
85176 i = 1;
85177 pRhs++;
85178 }else{
85179 if( (szHdr1 = aKey1[0])<0x80 ){
85180 idx1 = 1;
85181 }else{
85182 idx1 = sqlite3GetVarint32(aKey1, &szHdr1);
85183 }
85184 d1 = szHdr1;
85185 i = 0;
85186 }
85187 if( d1>(unsigned)nKey1 ){
85188 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPTsqlite3CorruptError(85188);
85189 return 0; /* Corruption */
85190 }
85191
85192 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
85193 assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField((void) (0))
85194 || CORRUPT_DB )((void) (0));
85195 assert( pPKey2->pKeyInfo->aSortFlags!=0 )((void) (0));
85196 assert( pPKey2->pKeyInfo->nKeyField>0 )((void) (0));
85197 assert( idx1<=szHdr1 || CORRUPT_DB )((void) (0));
85198 do{
85199 u32 serial_type;
85200
85201 /* RHS is an integer */
85202 if( pRhs->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
85203 testcase( pRhs->flags & MEM_Int );
85204 testcase( pRhs->flags & MEM_IntReal );
85205 serial_type = aKey1[idx1];
85206 testcase( serial_type==12 );
85207 if( serial_type>=10 ){
85208 rc = +1;
85209 }else if( serial_type==0 ){
85210 rc = -1;
85211 }else if( serial_type==7 ){
85212 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
85213 rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
85214 }else{
85215 i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
85216 i64 rhs = pRhs->u.i;
85217 if( lhs<rhs ){
85218 rc = -1;
85219 }else if( lhs>rhs ){
85220 rc = +1;
85221 }
85222 }
85223 }
85224
85225 /* RHS is real */
85226 else if( pRhs->flags & MEM_Real0x0008 ){
85227 serial_type = aKey1[idx1];
85228 if( serial_type>=10 ){
85229 /* Serial types 12 or greater are strings and blobs (greater than
85230 ** numbers). Types 10 and 11 are currently "reserved for future
85231 ** use", so it doesn't really matter what the results of comparing
85232 ** them to numberic values are. */
85233 rc = +1;
85234 }else if( serial_type==0 ){
85235 rc = -1;
85236 }else{
85237 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
85238 if( serial_type==7 ){
85239 if( mem1.u.r<pRhs->u.r ){
85240 rc = -1;
85241 }else if( mem1.u.r>pRhs->u.r ){
85242 rc = +1;
85243 }
85244 }else{
85245 rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r);
85246 }
85247 }
85248 }
85249
85250 /* RHS is a string */
85251 else if( pRhs->flags & MEM_Str0x0002 ){
85252 getVarint32NR(&aKey1[idx1], serial_type)serial_type=(u32)*(&aKey1[idx1]);if(serial_type>=0x80)
sqlite3GetVarint32((&aKey1[idx1]),(u32*)&(serial_type
))
;
85253 testcase( serial_type==12 );
85254 if( serial_type<12 ){
85255 rc = -1;
85256 }else if( !(serial_type & 0x01) ){
85257 rc = +1;
85258 }else{
85259 mem1.n = (serial_type - 12) / 2;
85260 testcase( (d1+mem1.n)==(unsigned)nKey1 );
85261 testcase( (d1+mem1.n+1)==(unsigned)nKey1 );
85262 if( (d1+mem1.n) > (unsigned)nKey1
85263 || (pKeyInfo = pPKey2->pKeyInfo)->nAllField<=i
85264 ){
85265 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPTsqlite3CorruptError(85265);
85266 return 0; /* Corruption */
85267 }else if( pKeyInfo->aColl[i] ){
85268 mem1.enc = pKeyInfo->enc;
85269 mem1.db = pKeyInfo->db;
85270 mem1.flags = MEM_Str0x0002;
85271 mem1.z = (char*)&aKey1[d1];
85272 rc = vdbeCompareMemString(
85273 &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
85274 );
85275 }else{
85276 int nCmp = MIN(mem1.n, pRhs->n)((mem1.n)<(pRhs->n)?(mem1.n):(pRhs->n));
85277 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
85278 if( rc==0 ) rc = mem1.n - pRhs->n;
85279 }
85280 }
85281 }
85282
85283 /* RHS is a blob */
85284 else if( pRhs->flags & MEM_Blob0x0010 ){
85285 assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 )((void) (0));
85286 getVarint32NR(&aKey1[idx1], serial_type)serial_type=(u32)*(&aKey1[idx1]);if(serial_type>=0x80)
sqlite3GetVarint32((&aKey1[idx1]),(u32*)&(serial_type
))
;
85287 testcase( serial_type==12 );
85288 if( serial_type<12 || (serial_type & 0x01) ){
85289 rc = -1;
85290 }else{
85291 int nStr = (serial_type - 12) / 2;
85292 testcase( (d1+nStr)==(unsigned)nKey1 );
85293 testcase( (d1+nStr+1)==(unsigned)nKey1 );
85294 if( (d1+nStr) > (unsigned)nKey1 ){
85295 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPTsqlite3CorruptError(85295);
85296 return 0; /* Corruption */
85297 }else if( pRhs->flags & MEM_Zero0x0400 ){
85298 if( !isAllZero((const char*)&aKey1[d1],nStr) ){
85299 rc = 1;
85300 }else{
85301 rc = nStr - pRhs->u.nZero;
85302 }
85303 }else{
85304 int nCmp = MIN(nStr, pRhs->n)((nStr)<(pRhs->n)?(nStr):(pRhs->n));
85305 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
85306 if( rc==0 ) rc = nStr - pRhs->n;
85307 }
85308 }
85309 }
85310
85311 /* RHS is null */
85312 else{
85313 serial_type = aKey1[idx1];
85314 rc = (serial_type!=0);
85315 }
85316
85317 if( rc!=0 ){
85318 int sortFlags = pPKey2->pKeyInfo->aSortFlags[i];
85319 if( sortFlags ){
85320 if( (sortFlags & KEYINFO_ORDER_BIGNULL0x02)==0
85321 || ((sortFlags & KEYINFO_ORDER_DESC0x01)
85322 !=(serial_type==0 || (pRhs->flags&MEM_Null0x0001)))
85323 ){
85324 rc = -rc;
85325 }
85326 }
85327 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) )((void) (0));
85328 assert( mem1.szMalloc==0 )((void) (0)); /* See comment below */
85329 return rc;
85330 }
85331
85332 i++;
85333 if( i==pPKey2->nField ) break;
85334 pRhs++;
85335 d1 += sqlite3VdbeSerialTypeLen(serial_type);
85336 idx1 += sqlite3VarintLen(serial_type);
85337 }while( idx1<(unsigned)szHdr1 && d1<=(unsigned)nKey1 );
85338
85339 /* No memory allocation is ever used on mem1. Prove this using
85340 ** the following assert(). If the assert() fails, it indicates a
85341 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */
85342 assert( mem1.szMalloc==0 )((void) (0));
85343
85344 /* rc==0 here means that one or both of the keys ran out of fields and
85345 ** all the fields up to that point were equal. Return the default_rc
85346 ** value. */
85347 assert( CORRUPT_DB((void) (0))
85348 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)((void) (0))
85349 || pPKey2->pKeyInfo->db->mallocFailed((void) (0))
85350 )((void) (0));
85351 pPKey2->eqSeen = 1;
85352 return pPKey2->default_rc;
85353}
85354SQLITE_PRIVATEstatic int sqlite3VdbeRecordCompare(
85355 int nKey1, const void *pKey1, /* Left key */
85356 UnpackedRecord *pPKey2 /* Right key */
85357){
85358 return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0);
85359}
85360
85361
85362/*
85363** This function is an optimized version of sqlite3VdbeRecordCompare()
85364** that (a) the first field of pPKey2 is an integer, and (b) the
85365** size-of-header varint at the start of (pKey1/nKey1) fits in a single
85366** byte (i.e. is less than 128).
85367**
85368** To avoid concerns about buffer overreads, this routine is only used
85369** on schemas where the maximum valid header size is 63 bytes or less.
85370*/
85371static int vdbeRecordCompareInt(
85372 int nKey1, const void *pKey1, /* Left key */
85373 UnpackedRecord *pPKey2 /* Right key */
85374){
85375 const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
85376 int serial_type = ((const u8*)pKey1)[1];
85377 int res;
85378 u32 y;
85379 u64 x;
85380 i64 v;
85381 i64 lhs;
85382
85383 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
85384 assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB )((void) (0));
85385 switch( serial_type ){
85386 case 1: { /* 1-byte signed integer */
85387 lhs = ONE_BYTE_INT(aKey)((i8)(aKey)[0]);
85388 testcase( lhs<0 );
85389 break;
85390 }
85391 case 2: { /* 2-byte signed integer */
85392 lhs = TWO_BYTE_INT(aKey)(256*(i8)((aKey)[0])|(aKey)[1]);
85393 testcase( lhs<0 );
85394 break;
85395 }
85396 case 3: { /* 3-byte signed integer */
85397 lhs = THREE_BYTE_INT(aKey)(65536*(i8)((aKey)[0])|((aKey)[1]<<8)|(aKey)[2]);
85398 testcase( lhs<0 );
85399 break;
85400 }
85401 case 4: { /* 4-byte signed integer */
85402 y = FOUR_BYTE_UINT(aKey)(((u32)(aKey)[0]<<24)|((aKey)[1]<<16)|((aKey)[2]<<
8)|(aKey)[3])
;
85403 lhs = (i64)*(int*)&y;
85404 testcase( lhs<0 );
85405 break;
85406 }
85407 case 5: { /* 6-byte signed integer */
85408 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]);
85409 testcase( lhs<0 );
85410 break;
85411 }
85412 case 6: { /* 8-byte signed integer */
85413 x = FOUR_BYTE_UINT(aKey)(((u32)(aKey)[0]<<24)|((aKey)[1]<<16)|((aKey)[2]<<
8)|(aKey)[3])
;
85414 x = (x<<32) | FOUR_BYTE_UINT(aKey+4)(((u32)(aKey+4)[0]<<24)|((aKey+4)[1]<<16)|((aKey+
4)[2]<<8)|(aKey+4)[3])
;
85415 lhs = *(i64*)&x;
85416 testcase( lhs<0 );
85417 break;
85418 }
85419 case 8:
85420 lhs = 0;
85421 break;
85422 case 9:
85423 lhs = 1;
85424 break;
85425
85426 /* This case could be removed without changing the results of running
85427 ** this code. Including it causes gcc to generate a faster switch
85428 ** statement (since the range of switch targets now starts at zero and
85429 ** is contiguous) but does not cause any duplicate code to be generated
85430 ** (as gcc is clever enough to combine the two like cases). Other
85431 ** compilers might be similar. */
85432 case 0: case 7:
85433 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
85434
85435 default:
85436 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
85437 }
85438
85439 assert( pPKey2->u.i == pPKey2->aMem[0].u.i )((void) (0));
85440 v = pPKey2->u.i;
85441 if( v>lhs ){
85442 res = pPKey2->r1;
85443 }else if( v<lhs ){
85444 res = pPKey2->r2;
85445 }else if( pPKey2->nField>1 ){
85446 /* The first fields of the two keys are equal. Compare the trailing
85447 ** fields. */
85448 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
85449 }else{
85450 /* The first fields of the two keys are equal and there are no trailing
85451 ** fields. Return pPKey2->default_rc in this case. */
85452 res = pPKey2->default_rc;
85453 pPKey2->eqSeen = 1;
85454 }
85455
85456 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) )((void) (0));
85457 return res;
85458}
85459
85460/*
85461** This function is an optimized version of sqlite3VdbeRecordCompare()
85462** that (a) the first field of pPKey2 is a string, that (b) the first field
85463** uses the collation sequence BINARY and (c) that the size-of-header varint
85464** at the start of (pKey1/nKey1) fits in a single byte.
85465*/
85466static int vdbeRecordCompareString(
85467 int nKey1, const void *pKey1, /* Left key */
85468 UnpackedRecord *pPKey2 /* Right key */
85469){
85470 const u8 *aKey1 = (const u8*)pKey1;
85471 int serial_type;
85472 int res;
85473
85474 assert( pPKey2->aMem[0].flags & MEM_Str )((void) (0));
85475 assert( pPKey2->aMem[0].n == pPKey2->n )((void) (0));
85476 assert( pPKey2->aMem[0].z == pPKey2->u.z )((void) (0));
85477 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
85478 serial_type = (signed char)(aKey1[1]);
85479
85480vrcs_restart:
85481 if( serial_type<12 ){
85482 if( serial_type<0 ){
85483 sqlite3GetVarint32(&aKey1[1], (u32*)&serial_type);
85484 if( serial_type>=12 ) goto vrcs_restart;
85485 assert( CORRUPT_DB )((void) (0));
85486 }
85487 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
85488 }else if( !(serial_type & 0x01) ){
85489 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
85490 }else{
85491 int nCmp;
85492 int nStr;
85493 int szHdr = aKey1[0];
85494
85495 nStr = (serial_type-12) / 2;
85496 if( (szHdr + nStr) > nKey1 ){
85497 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPTsqlite3CorruptError(85497);
85498 return 0; /* Corruption */
85499 }
85500 nCmp = MIN( pPKey2->n, nStr )((pPKey2->n)<(nStr)?(pPKey2->n):(nStr));
85501 res = memcmp(&aKey1[szHdr], pPKey2->u.z, nCmp);
85502
85503 if( res>0 ){
85504 res = pPKey2->r2;
85505 }else if( res<0 ){
85506 res = pPKey2->r1;
85507 }else{
85508 res = nStr - pPKey2->n;
85509 if( res==0 ){
85510 if( pPKey2->nField>1 ){
85511 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
85512 }else{
85513 res = pPKey2->default_rc;
85514 pPKey2->eqSeen = 1;
85515 }
85516 }else if( res>0 ){
85517 res = pPKey2->r2;
85518 }else{
85519 res = pPKey2->r1;
85520 }
85521 }
85522 }
85523
85524 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res)((void) (0))
85525 || CORRUPT_DB((void) (0))
85526 || pPKey2->pKeyInfo->db->mallocFailed((void) (0))
85527 )((void) (0));
85528 return res;
85529}
85530
85531/*
85532** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
85533** suitable for comparing serialized records to the unpacked record passed
85534** as the only argument.
85535*/
85536SQLITE_PRIVATEstatic RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
85537 /* varintRecordCompareInt() and varintRecordCompareString() both assume
85538 ** that the size-of-header varint that occurs at the start of each record
85539 ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
85540 ** also assumes that it is safe to overread a buffer by at least the
85541 ** maximum possible legal header size plus 8 bytes. Because there is
85542 ** guaranteed to be at least 74 (but not 136) bytes of padding following each
85543 ** buffer passed to varintRecordCompareInt() this makes it convenient to
85544 ** limit the size of the header to 64 bytes in cases where the first field
85545 ** is an integer.
85546 **
85547 ** The easiest way to enforce this limit is to consider only records with
85548 ** 13 fields or less. If the first field is an integer, the maximum legal
85549 ** header size is (12*5 + 1 + 1) bytes. */
85550 if( p->pKeyInfo->nAllField<=13 ){
85551 int flags = p->aMem[0].flags;
85552 if( p->pKeyInfo->aSortFlags[0] ){
85553 if( p->pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL0x02 ){
85554 return sqlite3VdbeRecordCompare;
85555 }
85556 p->r1 = 1;
85557 p->r2 = -1;
85558 }else{
85559 p->r1 = -1;
85560 p->r2 = 1;
85561 }
85562 if( (flags & MEM_Int0x0004) ){
85563 p->u.i = p->aMem[0].u.i;
85564 return vdbeRecordCompareInt;
85565 }
85566 testcase( flags & MEM_Real );
85567 testcase( flags & MEM_Null );
85568 testcase( flags & MEM_Blob );
85569 if( (flags & (MEM_Real0x0008|MEM_IntReal0x0020|MEM_Null0x0001|MEM_Blob0x0010))==0
85570 && p->pKeyInfo->aColl[0]==0
85571 ){
85572 assert( flags & MEM_Str )((void) (0));
85573 p->u.z = p->aMem[0].z;
85574 p->n = p->aMem[0].n;
85575 return vdbeRecordCompareString;
85576 }
85577 }
85578
85579 return sqlite3VdbeRecordCompare;
85580}
85581
85582/*
85583** pCur points at an index entry created using the OP_MakeRecord opcode.
85584** Read the rowid (the last field in the record) and store it in *rowid.
85585** Return SQLITE_OK if everything works, or an error code otherwise.
85586**
85587** pCur might be pointing to text obtained from a corrupt database file.
85588** So the content cannot be trusted. Do appropriate checks on the content.
85589*/
85590SQLITE_PRIVATEstatic int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
85591 i64 nCellKey = 0;
85592 int rc;
85593 u32 szHdr; /* Size of the header */
85594 u32 typeRowid; /* Serial type of the rowid */
85595 u32 lenRowid; /* Size of the rowid */
85596 Mem m, v;
85597
85598 /* Get the size of the index entry. Only indices entries of less
85599 ** than 2GiB are support - anything large must be database corruption.
85600 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
85601 ** this code can safely assume that nCellKey is 32-bits
85602 */
85603 assert( sqlite3BtreeCursorIsValid(pCur) )((void) (0));
85604 nCellKey = sqlite3BtreePayloadSize(pCur);
85605 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey )((void) (0));
85606
85607 /* Read in the complete content of the index entry */
85608 sqlite3VdbeMemInit(&m, db, 0);
85609 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m);
85610 if( rc ){
85611 return rc;
85612 }
85613
85614 /* The index entry must begin with a header size */
85615 getVarint32NR((u8*)m.z, szHdr)szHdr=(u32)*((u8*)m.z);if(szHdr>=0x80)sqlite3GetVarint32((
(u8*)m.z),(u32*)&(szHdr))
;
85616 testcase( szHdr==3 );
85617 testcase( szHdr==(u32)m.n );
85618 testcase( szHdr>0x7fffffff );
85619 assert( m.n>=0 )((void) (0));
85620 if( unlikely(szHdr<3 || szHdr>(unsigned)m.n)(szHdr<3 || szHdr>(unsigned)m.n) ){
85621 goto idx_rowid_corruption;
85622 }
85623
85624 /* The last field of the index should be an integer - the ROWID.
85625 ** Verify that the last entry really is an integer. */
85626 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
))
;
85627 testcase( typeRowid==1 );
85628 testcase( typeRowid==2 );
85629 testcase( typeRowid==3 );
85630 testcase( typeRowid==4 );
85631 testcase( typeRowid==5 );
85632 testcase( typeRowid==6 );
85633 testcase( typeRowid==8 );
85634 testcase( typeRowid==9 );
85635 if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7)(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
85636 goto idx_rowid_corruption;
85637 }
85638 lenRowid = sqlite3SmallTypeSizes[typeRowid];
85639 testcase( (u32)m.n==szHdr+lenRowid );
85640 if( unlikely((u32)m.n<szHdr+lenRowid)((u32)m.n<szHdr+lenRowid) ){
85641 goto idx_rowid_corruption;
85642 }
85643
85644 /* Fetch the integer off the end of the index record */
85645 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
85646 *rowid = v.u.i;
85647 sqlite3VdbeMemReleaseMalloc(&m);
85648 return SQLITE_OK0;
85649
85650 /* Jump here if database corruption is detected after m has been
85651 ** allocated. Free the m object and return SQLITE_CORRUPT. */
85652idx_rowid_corruption:
85653 testcase( m.szMalloc!=0 );
85654 sqlite3VdbeMemReleaseMalloc(&m);
85655 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(85655);
85656}
85657
85658/*
85659** Compare the key of the index entry that cursor pC is pointing to against
85660** the key string in pUnpacked. Write into *pRes a number
85661** that is negative, zero, or positive if pC is less than, equal to,
85662** or greater than pUnpacked. Return SQLITE_OK on success.
85663**
85664** pUnpacked is either created without a rowid or is truncated so that it
85665** omits the rowid at the end. The rowid at the end of the index entry
85666** is ignored as well. Hence, this routine only compares the prefixes
85667** of the keys prior to the final rowid, not the entire key.
85668*/
85669SQLITE_PRIVATEstatic int sqlite3VdbeIdxKeyCompare(
85670 sqlite3 *db, /* Database connection */
85671 VdbeCursor *pC, /* The cursor to compare against */
85672 UnpackedRecord *pUnpacked, /* Unpacked version of key */
85673 int *res /* Write the comparison result here */
85674){
85675 i64 nCellKey = 0;
85676 int rc;
85677 BtCursor *pCur;
85678 Mem m;
85679
85680 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
85681 pCur = pC->uc.pCursor;
85682 assert( sqlite3BtreeCursorIsValid(pCur) )((void) (0));
85683 nCellKey = sqlite3BtreePayloadSize(pCur);
85684 /* nCellKey will always be between 0 and 0xffffffff because of the way
85685 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
85686 if( nCellKey<=0 || nCellKey>0x7fffffff ){
85687 *res = 0;
85688 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(85688);
85689 }
85690 sqlite3VdbeMemInit(&m, db, 0);
85691 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m);
85692 if( rc ){
85693 return rc;
85694 }
85695 *res = sqlite3VdbeRecordCompareWithSkip(m.n, m.z, pUnpacked, 0);
85696 sqlite3VdbeMemReleaseMalloc(&m);
85697 return SQLITE_OK0;
85698}
85699
85700/*
85701** This routine sets the value to be returned by subsequent calls to
85702** sqlite3_changes() on the database handle 'db'.
85703*/
85704SQLITE_PRIVATEstatic void sqlite3VdbeSetChanges(sqlite3 *db, i64 nChange){
85705 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
85706 db->nChange = nChange;
85707 db->nTotalChange += nChange;
85708}
85709
85710/*
85711** Set a flag in the vdbe to update the change counter when it is finalised
85712** or reset.
85713*/
85714SQLITE_PRIVATEstatic void sqlite3VdbeCountChanges(Vdbe *v){
85715 v->changeCntOn = 1;
85716}
85717
85718/*
85719** Mark every prepared statement associated with a database connection
85720** as expired.
85721**
85722** An expired statement means that recompilation of the statement is
85723** recommend. Statements expire when things happen that make their
85724** programs obsolete. Removing user-defined functions or collating
85725** sequences, or changing an authorization function are the types of
85726** things that make prepared statements obsolete.
85727**
85728** If iCode is 1, then expiration is advisory. The statement should
85729** be reprepared before being restarted, but if it is already running
85730** it is allowed to run to completion.
85731**
85732** Internally, this function just sets the Vdbe.expired flag on all
85733** prepared statements. The flag is set to 1 for an immediate expiration
85734** and set to 2 for an advisory expiration.
85735*/
85736SQLITE_PRIVATEstatic void sqlite3ExpirePreparedStatements(sqlite3 *db, int iCode){
85737 Vdbe *p;
85738 for(p = db->pVdbe; p; p=p->pNext){
85739 p->expired = iCode+1;
85740 }
85741}
85742
85743/*
85744** Return the database associated with the Vdbe.
85745*/
85746SQLITE_PRIVATEstatic sqlite3 *sqlite3VdbeDb(Vdbe *v){
85747 return v->db;
85748}
85749
85750/*
85751** Return the SQLITE_PREPARE flags for a Vdbe.
85752*/
85753SQLITE_PRIVATEstatic u8 sqlite3VdbePrepareFlags(Vdbe *v){
85754 return v->prepFlags;
85755}
85756
85757/*
85758** Return a pointer to an sqlite3_value structure containing the value bound
85759** parameter iVar of VM v. Except, if the value is an SQL NULL, return
85760** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
85761** constants) to the value before returning it.
85762**
85763** The returned value must be freed by the caller using sqlite3ValueFree().
85764*/
85765SQLITE_PRIVATEstatic sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){
85766 assert( iVar>0 )((void) (0));
85767 if( v ){
85768 Mem *pMem = &v->aVar[iVar-1];
85769 assert( (v->db->flags & SQLITE_EnableQPSG)==0 )((void) (0));
85770 if( 0==(pMem->flags & MEM_Null0x0001) ){
85771 sqlite3_value *pRet = sqlite3ValueNew(v->db);
85772 if( pRet ){
85773 sqlite3VdbeMemCopy((Mem *)pRet, pMem);
85774 sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF81);
85775 }
85776 return pRet;
85777 }
85778 }
85779 return 0;
85780}
85781
85782/*
85783** Configure SQL variable iVar so that binding a new value to it signals
85784** to sqlite3_reoptimize() that re-preparing the statement may result
85785** in a better query plan.
85786*/
85787SQLITE_PRIVATEstatic void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
85788 assert( iVar>0 )((void) (0));
85789 assert( (v->db->flags & SQLITE_EnableQPSG)==0 )((void) (0));
85790 if( iVar>=32 ){
85791 v->expmask |= 0x80000000;
85792 }else{
85793 v->expmask |= ((u32)1 << (iVar-1));
85794 }
85795}
85796
85797/*
85798** Cause a function to throw an error if it was call from OP_PureFunc
85799** rather than OP_Function.
85800**
85801** OP_PureFunc means that the function must be deterministic, and should
85802** throw an error if it is given inputs that would make it non-deterministic.
85803** This routine is invoked by date/time functions that use non-deterministic
85804** features such as 'now'.
85805*/
85806SQLITE_PRIVATEstatic int sqlite3NotPureFunc(sqlite3_context *pCtx){
85807 const VdbeOp *pOp;
85808#ifdef SQLITE_ENABLE_STAT4
85809 if( pCtx->pVdbe==0 ) return 1;
85810#endif
85811 pOp = pCtx->pVdbe->aOp + pCtx->iOp;
85812 if( pOp->opcode==OP_PureFunc65 ){
85813 const char *zContext;
85814 char *zMsg;
85815 if( pOp->p5 & NC_IsCheck0x000004 ){
85816 zContext = "a CHECK constraint";
85817 }else if( pOp->p5 & NC_GenCol0x000008 ){
85818 zContext = "a generated column";
85819 }else{
85820 zContext = "an index";
85821 }
85822 zMsg = sqlite3_mprintf("non-deterministic use of %s() in %s",
85823 pCtx->pFunc->zName, zContext);
85824 sqlite3_result_error(pCtx, zMsg, -1);
85825 sqlite3_free(zMsg);
85826 return 0;
85827 }
85828 return 1;
85829}
85830
85831#ifndef SQLITE_OMIT_VIRTUALTABLE
85832/*
85833** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
85834** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
85835** in memory obtained from sqlite3DbMalloc).
85836*/
85837SQLITE_PRIVATEstatic void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
85838 if( pVtab->zErrMsg ){
85839 sqlite3 *db = p->db;
85840 sqlite3DbFree(db, p->zErrMsg);
85841 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
85842 sqlite3_free(pVtab->zErrMsg);
85843 pVtab->zErrMsg = 0;
85844 }
85845}
85846#endif /* SQLITE_OMIT_VIRTUALTABLE */
85847
85848#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
85849
85850/*
85851** If the second argument is not NULL, release any allocations associated
85852** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
85853** structure itself, using sqlite3DbFree().
85854**
85855** This function is used to free UnpackedRecord structures allocated by
85856** the vdbeUnpackRecord() function found in vdbeapi.c.
85857*/
85858static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){
85859 if( p ){
85860 int i;
85861 for(i=0; i<nField; i++){
85862 Mem *pMem = &p->aMem[i];
85863 if( pMem->zMalloc ) sqlite3VdbeMemReleaseMalloc(pMem);
85864 }
85865 sqlite3DbFreeNN(db, p);
85866 }
85867}
85868#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
85869
85870#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
85871/*
85872** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
85873** then cursor passed as the second argument should point to the row about
85874** to be update or deleted. If the application calls sqlite3_preupdate_old(),
85875** the required value will be read from the row the cursor points to.
85876*/
85877SQLITE_PRIVATEstatic void sqlite3VdbePreUpdateHook(
85878 Vdbe *v, /* Vdbe pre-update hook is invoked by */
85879 VdbeCursor *pCsr, /* Cursor to grab old.* values from */
85880 int op, /* SQLITE_INSERT, UPDATE or DELETE */
85881 const char *zDb, /* Database name */
85882 Table *pTab, /* Modified table */
85883 i64 iKey1, /* Initial key value */
85884 int iReg, /* Register for new.* record */
85885 int iBlobWrite
85886){
85887 sqlite3 *db = v->db;
85888 i64 iKey2;
85889 PreUpdate preupdate;
85890 const char *zTbl = pTab->zName;
85891 static const u8 fakeSortOrder = 0;
85892
85893 assert( db->pPreUpdate==0 )((void) (0));
85894 memset(&preupdate, 0, sizeof(PreUpdate));
85895 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0)==0 ){
85896 iKey1 = iKey2 = 0;
85897 preupdate.pPk = sqlite3PrimaryKeyIndex(pTab);
85898 }else{
85899 if( op==SQLITE_UPDATE23 ){
85900 iKey2 = v->aMem[iReg].u.i;
85901 }else{
85902 iKey2 = iKey1;
85903 }
85904 }
85905
85906 assert( pCsr!=0 )((void) (0));
85907 assert( pCsr->eCurType==CURTYPE_BTREE )((void) (0));
85908 assert( pCsr->nField==pTab->nCol((void) (0))
85909 || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1)((void) (0))
85910 )((void) (0));
85911
85912 preupdate.v = v;
85913 preupdate.pCsr = pCsr;
85914 preupdate.op = op;
85915 preupdate.iNewReg = iReg;
85916 preupdate.keyinfo.db = db;
85917 preupdate.keyinfo.enc = ENC(db)((db)->enc);
85918 preupdate.keyinfo.nKeyField = pTab->nCol;
85919 preupdate.keyinfo.aSortFlags = (u8*)&fakeSortOrder;
85920 preupdate.iKey1 = iKey1;
85921 preupdate.iKey2 = iKey2;
85922 preupdate.pTab = pTab;
85923 preupdate.iBlobWrite = iBlobWrite;
85924
85925 db->pPreUpdate = &preupdate;
85926 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
85927 db->pPreUpdate = 0;
85928 sqlite3DbFree(db, preupdate.aRecord);
85929 vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pUnpacked);
85930 vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pNewUnpacked);
85931 if( preupdate.aNew ){
85932 int i;
85933 for(i=0; i<pCsr->nField; i++){
85934 sqlite3VdbeMemRelease(&preupdate.aNew[i]);
85935 }
85936 sqlite3DbFreeNN(db, preupdate.aNew);
85937 }
85938}
85939#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
85940
85941/************** End of vdbeaux.c *********************************************/
85942/************** Begin file vdbeapi.c *****************************************/
85943/*
85944** 2004 May 26
85945**
85946** The author disclaims copyright to this source code. In place of
85947** a legal notice, here is a blessing:
85948**
85949** May you do good and not evil.
85950** May you find forgiveness for yourself and forgive others.
85951** May you share freely, never taking more than you give.
85952**
85953*************************************************************************
85954**
85955** This file contains code use to implement APIs that are part of the
85956** VDBE.
85957*/
85958/* #include "sqliteInt.h" */
85959/* #include "vdbeInt.h" */
85960
85961#ifndef SQLITE_OMIT_DEPRECATED1
85962/*
85963** Return TRUE (non-zero) of the statement supplied as an argument needs
85964** to be recompiled. A statement needs to be recompiled whenever the
85965** execution environment changes in a way that would alter the program
85966** that sqlite3_prepare() generates. For example, if new functions or
85967** collating sequences are registered or if an authorizer function is
85968** added or changed.
85969*/
85970SQLITE_API int sqlite3_expired(sqlite3_stmt *pStmt){
85971 Vdbe *p = (Vdbe*)pStmt;
85972 return p==0 || p->expired;
85973}
85974#endif
85975
85976/*
85977** Check on a Vdbe to make sure it has not been finalized. Log
85978** an error and return true if it has been finalized (or is otherwise
85979** invalid). Return false if it is ok.
85980*/
85981static int vdbeSafety(Vdbe *p){
85982 if( p->db==0 ){
85983 sqlite3_log(SQLITE_MISUSE21, "API called with finalized prepared statement");
85984 return 1;
85985 }else{
85986 return 0;
85987 }
85988}
85989static int vdbeSafetyNotNull(Vdbe *p){
85990 if( p==0 ){
85991 sqlite3_log(SQLITE_MISUSE21, "API called with NULL prepared statement");
85992 return 1;
85993 }else{
85994 return vdbeSafety(p);
85995 }
85996}
85997
85998#ifndef SQLITE_OMIT_TRACE
85999/*
86000** Invoke the profile callback. This routine is only called if we already
86001** know that the profile callback is defined and needs to be invoked.
86002*/
86003static SQLITE_NOINLINE__attribute__((noinline)) void invokeProfileCallback(sqlite3 *db, Vdbe *p){
86004 sqlite3_int64 iNow;
86005 sqlite3_int64 iElapse;
86006 assert( p->startTime>0 )((void) (0));
86007 assert( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 )((void) (0));
86008 assert( db->init.busy==0 )((void) (0));
86009 assert( p->zSql!=0 )((void) (0));
86010 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
86011 iElapse = (iNow - p->startTime)*1000000;
86012#ifndef SQLITE_OMIT_DEPRECATED1
86013 if( db->xProfile ){
86014 db->xProfile(db->pProfileArg, p->zSql, iElapse);
86015 }
86016#endif
86017 if( db->mTrace & SQLITE_TRACE_PROFILE0x02 ){
86018 db->trace.xV2(SQLITE_TRACE_PROFILE0x02, db->pTraceArg, p, (void*)&iElapse);
86019 }
86020 p->startTime = 0;
86021}
86022/*
86023** The checkProfileCallback(DB,P) macro checks to see if a profile callback
86024** is needed, and it invokes the callback if it is needed.
86025*/
86026# define checkProfileCallback(DB,P)if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); } \
86027 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
86028#else
86029# define checkProfileCallback(DB,P)if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); } /*no-op*/
86030#endif
86031
86032/*
86033** The following routine destroys a virtual machine that is created by
86034** the sqlite3_compile() routine. The integer returned is an SQLITE_
86035** success/failure code that describes the result of executing the virtual
86036** machine.
86037**
86038** This routine sets the error code and string returned by
86039** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
86040*/
86041SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){
86042 int rc;
86043 if( pStmt==0 ){
86044 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
86045 ** pointer is a harmless no-op. */
86046 rc = SQLITE_OK0;
86047 }else{
86048 Vdbe *v = (Vdbe*)pStmt;
86049 sqlite3 *db = v->db;
86050 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(86050);
86051 sqlite3_mutex_enter(db->mutex);
86052 checkProfileCallback(db, v)if( ((v)->startTime)>0 ){ invokeProfileCallback(db,v); };
86053 rc = sqlite3VdbeFinalize(v);
86054 rc = sqlite3ApiExit(db, rc);
86055 sqlite3LeaveMutexAndCloseZombie(db);
86056 }
86057 return rc;
86058}
86059
86060/*
86061** Terminate the current execution of an SQL statement and reset it
86062** back to its starting state so that it can be reused. A success code from
86063** the prior execution is returned.
86064**
86065** This routine sets the error code and string returned by
86066** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
86067*/
86068SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){
86069 int rc;
86070 if( pStmt==0 ){
86071 rc = SQLITE_OK0;
86072 }else{
86073 Vdbe *v = (Vdbe*)pStmt;
86074 sqlite3 *db = v->db;
86075 sqlite3_mutex_enter(db->mutex);
86076 checkProfileCallback(db, v)if( ((v)->startTime)>0 ){ invokeProfileCallback(db,v); };
86077 rc = sqlite3VdbeReset(v);
86078 sqlite3VdbeRewind(v);
86079 assert( (rc & (db->errMask))==rc )((void) (0));
86080 rc = sqlite3ApiExit(db, rc);
86081 sqlite3_mutex_leave(db->mutex);
86082 }
86083 return rc;
86084}
86085
86086/*
86087** Set all the parameters in the compiled SQL statement to NULL.
86088*/
86089SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
86090 int i;
86091 int rc = SQLITE_OK0;
86092 Vdbe *p = (Vdbe*)pStmt;
86093#if SQLITE_THREADSAFE2
86094 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
86095#endif
86096 sqlite3_mutex_enter(mutex);
86097 for(i=0; i<p->nVar; i++){
86098 sqlite3VdbeMemRelease(&p->aVar[i]);
86099 p->aVar[i].flags = MEM_Null0x0001;
86100 }
86101 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 )((void) (0));
86102 if( p->expmask ){
86103 p->expired = 1;
86104 }
86105 sqlite3_mutex_leave(mutex);
86106 return rc;
86107}
86108
86109
86110/**************************** sqlite3_value_ *******************************
86111** The following routines extract information from a Mem or sqlite3_value
86112** structure.
86113*/
86114SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){
86115 Mem *p = (Mem*)pVal;
86116 if( p->flags & (MEM_Blob0x0010|MEM_Str0x0002) ){
86117 if( ExpandBlob(p)(((p)->flags&0x0400)?sqlite3VdbeMemExpandBlob(p):0)!=SQLITE_OK0 ){
86118 assert( p->flags==MEM_Null && p->z==0 )((void) (0));
86119 return 0;
86120 }
86121 p->flags |= MEM_Blob0x0010;
86122 return p->n ? p->z : 0;
86123 }else{
86124 return sqlite3_value_text(pVal);
86125 }
86126}
86127SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){
86128 return sqlite3ValueBytes(pVal, SQLITE_UTF81);
86129}
86130SQLITE_API int sqlite3_value_bytes16(sqlite3_value *pVal){
86131 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE2);
86132}
86133SQLITE_API double sqlite3_value_double(sqlite3_value *pVal){
86134 return sqlite3VdbeRealValue((Mem*)pVal);
86135}
86136SQLITE_API int sqlite3_value_int(sqlite3_value *pVal){
86137 return (int)sqlite3VdbeIntValue((Mem*)pVal);
86138}
86139SQLITE_API sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
86140 return sqlite3VdbeIntValue((Mem*)pVal);
86141}
86142SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value *pVal){
86143 Mem *pMem = (Mem*)pVal;
86144 return ((pMem->flags & MEM_Subtype0x0800) ? pMem->eSubtype : 0);
86145}
86146SQLITE_API void *sqlite3_value_pointer(sqlite3_value *pVal, const char *zPType){
86147 Mem *p = (Mem*)pVal;
86148 if( (p->flags&(MEM_TypeMask0x0dbf|MEM_Term0x0200|MEM_Subtype0x0800)) ==
86149 (MEM_Null0x0001|MEM_Term0x0200|MEM_Subtype0x0800)
86150 && zPType!=0
86151 && p->eSubtype=='p'
86152 && strcmp(p->u.zPType, zPType)==0
86153 ){
86154 return (void*)p->z;
86155 }else{
86156 return 0;
86157 }
86158}
86159SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
86160 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF81);
86161}
86162#ifndef SQLITE_OMIT_UTF16
86163SQLITE_API const void *sqlite3_value_text16(sqlite3_value* pVal){
86164 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE2);
86165}
86166SQLITE_API const void *sqlite3_value_text16be(sqlite3_value *pVal){
86167 return sqlite3ValueText(pVal, SQLITE_UTF16BE3);
86168}
86169SQLITE_API const void *sqlite3_value_text16le(sqlite3_value *pVal){
86170 return sqlite3ValueText(pVal, SQLITE_UTF16LE2);
86171}
86172#endif /* SQLITE_OMIT_UTF16 */
86173/* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
86174** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
86175** point number string BLOB NULL
86176*/
86177SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){
86178 static const u8 aType[] = {
86179 SQLITE_BLOB4, /* 0x00 (not possible) */
86180 SQLITE_NULL5, /* 0x01 NULL */
86181 SQLITE_TEXT3, /* 0x02 TEXT */
86182 SQLITE_NULL5, /* 0x03 (not possible) */
86183 SQLITE_INTEGER1, /* 0x04 INTEGER */
86184 SQLITE_NULL5, /* 0x05 (not possible) */
86185 SQLITE_INTEGER1, /* 0x06 INTEGER + TEXT */
86186 SQLITE_NULL5, /* 0x07 (not possible) */
86187 SQLITE_FLOAT2, /* 0x08 FLOAT */
86188 SQLITE_NULL5, /* 0x09 (not possible) */
86189 SQLITE_FLOAT2, /* 0x0a FLOAT + TEXT */
86190 SQLITE_NULL5, /* 0x0b (not possible) */
86191 SQLITE_INTEGER1, /* 0x0c (not possible) */
86192 SQLITE_NULL5, /* 0x0d (not possible) */
86193 SQLITE_INTEGER1, /* 0x0e (not possible) */
86194 SQLITE_NULL5, /* 0x0f (not possible) */
86195 SQLITE_BLOB4, /* 0x10 BLOB */
86196 SQLITE_NULL5, /* 0x11 (not possible) */
86197 SQLITE_TEXT3, /* 0x12 (not possible) */
86198 SQLITE_NULL5, /* 0x13 (not possible) */
86199 SQLITE_INTEGER1, /* 0x14 INTEGER + BLOB */
86200 SQLITE_NULL5, /* 0x15 (not possible) */
86201 SQLITE_INTEGER1, /* 0x16 (not possible) */
86202 SQLITE_NULL5, /* 0x17 (not possible) */
86203 SQLITE_FLOAT2, /* 0x18 FLOAT + BLOB */
86204 SQLITE_NULL5, /* 0x19 (not possible) */
86205 SQLITE_FLOAT2, /* 0x1a (not possible) */
86206 SQLITE_NULL5, /* 0x1b (not possible) */
86207 SQLITE_INTEGER1, /* 0x1c (not possible) */
86208 SQLITE_NULL5, /* 0x1d (not possible) */
86209 SQLITE_INTEGER1, /* 0x1e (not possible) */
86210 SQLITE_NULL5, /* 0x1f (not possible) */
86211 SQLITE_FLOAT2, /* 0x20 INTREAL */
86212 SQLITE_NULL5, /* 0x21 (not possible) */
86213 SQLITE_TEXT3, /* 0x22 INTREAL + TEXT */
86214 SQLITE_NULL5, /* 0x23 (not possible) */
86215 SQLITE_FLOAT2, /* 0x24 (not possible) */
86216 SQLITE_NULL5, /* 0x25 (not possible) */
86217 SQLITE_FLOAT2, /* 0x26 (not possible) */
86218 SQLITE_NULL5, /* 0x27 (not possible) */
86219 SQLITE_FLOAT2, /* 0x28 (not possible) */
86220 SQLITE_NULL5, /* 0x29 (not possible) */
86221 SQLITE_FLOAT2, /* 0x2a (not possible) */
86222 SQLITE_NULL5, /* 0x2b (not possible) */
86223 SQLITE_FLOAT2, /* 0x2c (not possible) */
86224 SQLITE_NULL5, /* 0x2d (not possible) */
86225 SQLITE_FLOAT2, /* 0x2e (not possible) */
86226 SQLITE_NULL5, /* 0x2f (not possible) */
86227 SQLITE_BLOB4, /* 0x30 (not possible) */
86228 SQLITE_NULL5, /* 0x31 (not possible) */
86229 SQLITE_TEXT3, /* 0x32 (not possible) */
86230 SQLITE_NULL5, /* 0x33 (not possible) */
86231 SQLITE_FLOAT2, /* 0x34 (not possible) */
86232 SQLITE_NULL5, /* 0x35 (not possible) */
86233 SQLITE_FLOAT2, /* 0x36 (not possible) */
86234 SQLITE_NULL5, /* 0x37 (not possible) */
86235 SQLITE_FLOAT2, /* 0x38 (not possible) */
86236 SQLITE_NULL5, /* 0x39 (not possible) */
86237 SQLITE_FLOAT2, /* 0x3a (not possible) */
86238 SQLITE_NULL5, /* 0x3b (not possible) */
86239 SQLITE_FLOAT2, /* 0x3c (not possible) */
86240 SQLITE_NULL5, /* 0x3d (not possible) */
86241 SQLITE_FLOAT2, /* 0x3e (not possible) */
86242 SQLITE_NULL5, /* 0x3f (not possible) */
86243 };
86244#ifdef SQLITE_DEBUG
86245 {
86246 int eType = SQLITE_BLOB4;
86247 if( pVal->flags & MEM_Null0x0001 ){
86248 eType = SQLITE_NULL5;
86249 }else if( pVal->flags & (MEM_Real0x0008|MEM_IntReal0x0020) ){
86250 eType = SQLITE_FLOAT2;
86251 }else if( pVal->flags & MEM_Int0x0004 ){
86252 eType = SQLITE_INTEGER1;
86253 }else if( pVal->flags & MEM_Str0x0002 ){
86254 eType = SQLITE_TEXT3;
86255 }
86256 assert( eType == aType[pVal->flags&MEM_AffMask] )((void) (0));
86257 }
86258#endif
86259 return aType[pVal->flags&MEM_AffMask0x003f];
86260}
86261
86262/* Return true if a parameter to xUpdate represents an unchanged column */
86263SQLITE_API int sqlite3_value_nochange(sqlite3_value *pVal){
86264 return (pVal->flags&(MEM_Null0x0001|MEM_Zero0x0400))==(MEM_Null0x0001|MEM_Zero0x0400);
86265}
86266
86267/* Return true if a parameter value originated from an sqlite3_bind() */
86268SQLITE_API int sqlite3_value_frombind(sqlite3_value *pVal){
86269 return (pVal->flags&MEM_FromBind0x0040)!=0;
86270}
86271
86272/* Make a copy of an sqlite3_value object
86273*/
86274SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){
86275 sqlite3_value *pNew;
86276 if( pOrig==0 ) return 0;
86277 pNew = sqlite3_malloc( sizeof(*pNew) );
86278 if( pNew==0 ) return 0;
86279 memset(pNew, 0, sizeof(*pNew));
86280 memcpy(pNew, pOrig, MEMCELLSIZE__builtin_offsetof(Mem, db));
86281 pNew->flags &= ~MEM_Dyn0x1000;
86282 pNew->db = 0;
86283 if( pNew->flags&(MEM_Str0x0002|MEM_Blob0x0010) ){
86284 pNew->flags &= ~(MEM_Static0x2000|MEM_Dyn0x1000);
86285 pNew->flags |= MEM_Ephem0x4000;
86286 if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK0 ){
86287 sqlite3ValueFree(pNew);
86288 pNew = 0;
86289 }
86290 }else if( pNew->flags & MEM_Null0x0001 ){
86291 /* Do not duplicate pointer values */
86292 pNew->flags &= ~(MEM_Term0x0200|MEM_Subtype0x0800);
86293 }
86294 return pNew;
86295}
86296
86297/* Destroy an sqlite3_value object previously obtained from
86298** sqlite3_value_dup().
86299*/
86300SQLITE_API void sqlite3_value_free(sqlite3_value *pOld){
86301 sqlite3ValueFree(pOld);
86302}
86303
86304
86305/**************************** sqlite3_result_ *******************************
86306** The following routines are used by user-defined functions to specify
86307** the function result.
86308**
86309** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
86310** result as a string or blob. Appropriate errors are set if the string/blob
86311** is too big or if an OOM occurs.
86312**
86313** The invokeValueDestructor(P,X) routine invokes destructor function X()
86314** on value P is not going to be used and need to be destroyed.
86315*/
86316static void setResultStrOrError(
86317 sqlite3_context *pCtx, /* Function context */
86318 const char *z, /* String pointer */
86319 int n, /* Bytes in string, or negative */
86320 u8 enc, /* Encoding of z. 0 for BLOBs */
86321 void (*xDel)(void*) /* Destructor function */
86322){
86323 Mem *pOut = pCtx->pOut;
86324 int rc = sqlite3VdbeMemSetStr(pOut, z, n, enc, xDel);
86325 if( rc ){
86326 if( rc==SQLITE_TOOBIG18 ){
86327 sqlite3_result_error_toobig(pCtx);
86328 }else{
86329 /* The only errors possible from sqlite3VdbeMemSetStr are
86330 ** SQLITE_TOOBIG and SQLITE_NOMEM */
86331 assert( rc==SQLITE_NOMEM )((void) (0));
86332 sqlite3_result_error_nomem(pCtx);
86333 }
86334 return;
86335 }
86336 sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
86337 if( sqlite3VdbeMemTooBig(pOut) ){
86338 sqlite3_result_error_toobig(pCtx);
86339 }
86340}
86341static int invokeValueDestructor(
86342 const void *p, /* Value to destroy */
86343 void (*xDel)(void*), /* The destructor */
86344 sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if no NULL */
86345){
86346 assert( xDel!=SQLITE_DYNAMIC )((void) (0));
86347 if( xDel==0 ){
86348 /* noop */
86349 }else if( xDel==SQLITE_TRANSIENT((sqlite3_destructor_type)-1) ){
86350 /* noop */
86351 }else{
86352 xDel((void*)p);
86353 }
86354 sqlite3_result_error_toobig(pCtx);
86355 return SQLITE_TOOBIG18;
86356}
86357SQLITE_API void sqlite3_result_blob(
86358 sqlite3_context *pCtx,
86359 const void *z,
86360 int n,
86361 void (*xDel)(void *)
86362){
86363 assert( n>=0 )((void) (0));
86364 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
86365 setResultStrOrError(pCtx, z, n, 0, xDel);
86366}
86367SQLITE_API void sqlite3_result_blob64(
86368 sqlite3_context *pCtx,
86369 const void *z,
86370 sqlite3_uint64 n,
86371 void (*xDel)(void *)
86372){
86373 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
86374 assert( xDel!=SQLITE_DYNAMIC )((void) (0));
86375 if( n>0x7fffffff ){
86376 (void)invokeValueDestructor(z, xDel, pCtx);
86377 }else{
86378 setResultStrOrError(pCtx, z, (int)n, 0, xDel);
86379 }
86380}
86381SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
86382 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
86383 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
86384}
86385SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
86386 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
86387 pCtx->isError = SQLITE_ERROR1;
86388 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF81, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
86389}
86390#ifndef SQLITE_OMIT_UTF16
86391SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
86392 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
86393 pCtx->isError = SQLITE_ERROR1;
86394 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE2, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
86395}
86396#endif
86397SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
86398 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
86399 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
86400}
86401SQLITE_API void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
86402 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
86403 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
86404}
86405SQLITE_API void sqlite3_result_null(sqlite3_context *pCtx){
86406 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
86407 sqlite3VdbeMemSetNull(pCtx->pOut);
86408}
86409SQLITE_API void sqlite3_result_pointer(
86410 sqlite3_context *pCtx,
86411 void *pPtr,
86412 const char *zPType,
86413 void (*xDestructor)(void*)
86414){
86415 Mem *pOut = pCtx->pOut;
86416 assert( sqlite3_mutex_held(pOut->db->mutex) )((void) (0));
86417 sqlite3VdbeMemRelease(pOut);
86418 pOut->flags = MEM_Null0x0001;
86419 sqlite3VdbeMemSetPointer(pOut, pPtr, zPType, xDestructor);
86420}
86421SQLITE_API void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
86422 Mem *pOut = pCtx->pOut;
86423 assert( sqlite3_mutex_held(pOut->db->mutex) )((void) (0));
86424 pOut->eSubtype = eSubtype & 0xff;
86425 pOut->flags |= MEM_Subtype0x0800;
86426}
86427SQLITE_API void sqlite3_result_text(
86428 sqlite3_context *pCtx,
86429 const char *z,
86430 int n,
86431 void (*xDel)(void *)
86432){
86433 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
86434 setResultStrOrError(pCtx, z, n, SQLITE_UTF81, xDel);
86435}
86436SQLITE_API void sqlite3_result_text64(
86437 sqlite3_context *pCtx,
86438 const char *z,
86439 sqlite3_uint64 n,
86440 void (*xDel)(void *),
86441 unsigned char enc
86442){
86443 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
86444 assert( xDel!=SQLITE_DYNAMIC )((void) (0));
86445 if( enc==SQLITE_UTF164 ) enc = SQLITE_UTF16NATIVE2;
86446 if( n>0x7fffffff ){
86447 (void)invokeValueDestructor(z, xDel, pCtx);
86448 }else{
86449 setResultStrOrError(pCtx, z, (int)n, enc, xDel);
86450 }
86451}
86452#ifndef SQLITE_OMIT_UTF16
86453SQLITE_API void sqlite3_result_text16(
86454 sqlite3_context *pCtx,
86455 const void *z,
86456 int n,
86457 void (*xDel)(void *)
86458){
86459 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
86460 setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE2, xDel);
86461}
86462SQLITE_API void sqlite3_result_text16be(
86463 sqlite3_context *pCtx,
86464 const void *z,
86465 int n,
86466 void (*xDel)(void *)
86467){
86468 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
86469 setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE3, xDel);
86470}
86471SQLITE_API void sqlite3_result_text16le(
86472 sqlite3_context *pCtx,
86473 const void *z,
86474 int n,
86475 void (*xDel)(void *)
86476){
86477 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
86478 setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE2, xDel);
86479}
86480#endif /* SQLITE_OMIT_UTF16 */
86481SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
86482 Mem *pOut = pCtx->pOut;
86483 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
86484 sqlite3VdbeMemCopy(pOut, pValue);
86485 sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
86486 if( sqlite3VdbeMemTooBig(pOut) ){
86487 sqlite3_result_error_toobig(pCtx);
86488 }
86489}
86490SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
86491 sqlite3_result_zeroblob64(pCtx, n>0 ? n : 0);
86492}
86493SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){
86494 Mem *pOut = pCtx->pOut;
86495 assert( sqlite3_mutex_held(pOut->db->mutex) )((void) (0));
86496 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH0] ){
86497 sqlite3_result_error_toobig(pCtx);
86498 return SQLITE_TOOBIG18;
86499 }
86500#ifndef SQLITE_OMIT_INCRBLOB
86501 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
86502 return SQLITE_OK0;
86503#else
86504 return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
86505#endif
86506}
86507SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
86508 pCtx->isError = errCode ? errCode : -1;
86509#ifdef SQLITE_DEBUG
86510 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
86511#endif
86512 if( pCtx->pOut->flags & MEM_Null0x0001 ){
86513 setResultStrOrError(pCtx, sqlite3ErrStr(errCode), -1, SQLITE_UTF81,
86514 SQLITE_STATIC((sqlite3_destructor_type)0));
86515 }
86516}
86517
86518/* Force an SQLITE_TOOBIG error. */
86519SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
86520 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
86521 pCtx->isError = SQLITE_TOOBIG18;
86522 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
86523 SQLITE_UTF81, SQLITE_STATIC((sqlite3_destructor_type)0));
86524}
86525
86526/* An SQLITE_NOMEM error. */
86527SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
86528 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
86529 sqlite3VdbeMemSetNull(pCtx->pOut);
86530 pCtx->isError = SQLITE_NOMEM_BKPT7;
86531 sqlite3OomFault(pCtx->pOut->db);
86532}
86533
86534#ifndef SQLITE_UNTESTABLE
86535/* Force the INT64 value currently stored as the result to be
86536** a MEM_IntReal value. See the SQLITE_TESTCTRL_RESULT_INTREAL
86537** test-control.
86538*/
86539SQLITE_PRIVATEstatic void sqlite3ResultIntReal(sqlite3_context *pCtx){
86540 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
86541 if( pCtx->pOut->flags & MEM_Int0x0004 ){
86542 pCtx->pOut->flags &= ~MEM_Int0x0004;
86543 pCtx->pOut->flags |= MEM_IntReal0x0020;
86544 }
86545}
86546#endif
86547
86548
86549/*
86550** This function is called after a transaction has been committed. It
86551** invokes callbacks registered with sqlite3_wal_hook() as required.
86552*/
86553static int doWalCallbacks(sqlite3 *db){
86554 int rc = SQLITE_OK0;
86555#ifndef SQLITE_OMIT_WAL
86556 int i;
86557 for(i=0; i<db->nDb; i++){
86558 Btree *pBt = db->aDb[i].pBt;
86559 if( pBt ){
86560 int nEntry;
86561 sqlite3BtreeEnter(pBt);
86562 nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
86563 sqlite3BtreeLeave(pBt);
86564 if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK0 ){
86565 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
86566 }
86567 }
86568 }
86569#endif
86570 return rc;
86571}
86572
86573
86574/*
86575** Execute the statement pStmt, either until a row of data is ready, the
86576** statement is completely executed or an error occurs.
86577**
86578** This routine implements the bulk of the logic behind the sqlite_step()
86579** API. The only thing omitted is the automatic recompile if a
86580** schema change has occurred. That detail is handled by the
86581** outer sqlite3_step() wrapper procedure.
86582*/
86583static int sqlite3Step(Vdbe *p){
86584 sqlite3 *db;
86585 int rc;
86586
86587 assert(p)((void) (0));
86588 db = p->db;
86589 if( p->eVdbeState!=VDBE_RUN_STATE2 ){
86590 restart_step:
86591 if( p->eVdbeState==VDBE_READY_STATE1 ){
86592 if( p->expired ){
86593 p->rc = SQLITE_SCHEMA17;
86594 rc = SQLITE_ERROR1;
86595 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL0x80)!=0 ){
86596 /* If this statement was prepared using saved SQL and an
86597 ** error has occurred, then return the error code in p->rc to the
86598 ** caller. Set the error code in the database handle to the same
86599 ** value.
86600 */
86601 rc = sqlite3VdbeTransferError(p);
86602 }
86603 goto end_of_step;
86604 }
86605
86606 /* If there are no other statements currently running, then
86607 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
86608 ** from interrupting a statement that has not yet started.
86609 */
86610 if( db->nVdbeActive==0 ){
86611 AtomicStore(&db->u1.isInterrupted, 0)__atomic_store_n((&db->u1.isInterrupted),(0),0);
86612 }
86613
86614 assert( db->nVdbeWrite>0 || db->autoCommit==0((void) (0))
86615 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)((void) (0))
86616 )((void) (0));
86617
86618#ifndef SQLITE_OMIT_TRACE
86619 if( (db->mTrace & (SQLITE_TRACE_PROFILE0x02|SQLITE_TRACE_XPROFILE0))!=0
86620 && !db->init.busy && p->zSql ){
86621 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
86622 }else{
86623 assert( p->startTime==0 )((void) (0));
86624 }
86625#endif
86626
86627 db->nVdbeActive++;
86628 if( p->readOnly==0 ) db->nVdbeWrite++;
86629 if( p->bIsReader ) db->nVdbeRead++;
86630 p->pc = 0;
86631 p->eVdbeState = VDBE_RUN_STATE2;
86632 }else
86633
86634 if( ALWAYS(p->eVdbeState==VDBE_HALT_STATE)(p->eVdbeState==3) ){
86635 /* We used to require that sqlite3_reset() be called before retrying
86636 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning
86637 ** with version 3.7.0, we changed this so that sqlite3_reset() would
86638 ** be called automatically instead of throwing the SQLITE_MISUSE error.
86639 ** This "automatic-reset" change is not technically an incompatibility,
86640 ** since any application that receives an SQLITE_MISUSE is broken by
86641 ** definition.
86642 **
86643 ** Nevertheless, some published applications that were originally written
86644 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
86645 ** returns, and those were broken by the automatic-reset change. As a
86646 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
86647 ** legacy behavior of returning SQLITE_MISUSE for cases where the
86648 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
86649 ** or SQLITE_BUSY error.
86650 */
86651#ifdef SQLITE_OMIT_AUTORESET1
86652 if( (rc = p->rc&0xff)==SQLITE_BUSY5 || rc==SQLITE_LOCKED6 ){
86653 sqlite3_reset((sqlite3_stmt*)p);
86654 }else{
86655 return SQLITE_MISUSE_BKPTsqlite3MisuseError(86655);
86656 }
86657#else
86658 sqlite3_reset((sqlite3_stmt*)p);
86659#endif
86660 assert( p->eVdbeState==VDBE_READY_STATE )((void) (0));
86661 goto restart_step;
86662 }
86663 }
86664
86665#ifdef SQLITE_DEBUG
86666 p->rcApp = SQLITE_OK0;
86667#endif
86668#ifndef SQLITE_OMIT_EXPLAIN
86669 if( p->explain ){
86670 rc = sqlite3VdbeList(p);
86671 }else
86672#endif /* SQLITE_OMIT_EXPLAIN */
86673 {
86674 db->nVdbeExec++;
86675 rc = sqlite3VdbeExec(p);
86676 db->nVdbeExec--;
86677 }
86678
86679 if( rc==SQLITE_ROW100 ){
86680 assert( p->rc==SQLITE_OK )((void) (0));
86681 assert( db->mallocFailed==0 )((void) (0));
86682 db->errCode = SQLITE_ROW100;
86683 return SQLITE_ROW100;
86684 }else{
86685#ifndef SQLITE_OMIT_TRACE
86686 /* If the statement completed successfully, invoke the profile callback */
86687 checkProfileCallback(db, p)if( ((p)->startTime)>0 ){ invokeProfileCallback(db,p); };
86688#endif
86689
86690 if( rc==SQLITE_DONE101 && db->autoCommit ){
86691 assert( p->rc==SQLITE_OK )((void) (0));
86692 p->rc = doWalCallbacks(db);
86693 if( p->rc!=SQLITE_OK0 ){
86694 rc = SQLITE_ERROR1;
86695 }
86696 }else if( rc!=SQLITE_DONE101 && (p->prepFlags & SQLITE_PREPARE_SAVESQL0x80)!=0 ){
86697 /* If this statement was prepared using saved SQL and an
86698 ** error has occurred, then return the error code in p->rc to the
86699 ** caller. Set the error code in the database handle to the same value.
86700 */
86701 rc = sqlite3VdbeTransferError(p);
86702 }
86703 }
86704
86705 db->errCode = rc;
86706 if( SQLITE_NOMEM7==sqlite3ApiExit(p->db, p->rc) ){
86707 p->rc = SQLITE_NOMEM_BKPT7;
86708 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL0x80)!=0 ) rc = p->rc;
86709 }
86710end_of_step:
86711 /* There are only a limited number of result codes allowed from the
86712 ** statements prepared using the legacy sqlite3_prepare() interface */
86713 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0((void) (0))
86714 || rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR((void) (0))
86715 || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE((void) (0))
86716 )((void) (0));
86717 return (rc&db->errMask);
86718}
86719
86720/*
86721** This is the top-level implementation of sqlite3_step(). Call
86722** sqlite3Step() to do most of the work. If a schema error occurs,
86723** call sqlite3Reprepare() and try again.
86724*/
86725SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
86726 int rc = SQLITE_OK0; /* Result from sqlite3Step() */
86727 Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */
86728 int cnt = 0; /* Counter to prevent infinite loop of reprepares */
86729 sqlite3 *db; /* The database connection */
86730
86731 if( vdbeSafetyNotNull(v) ){
86732 return SQLITE_MISUSE_BKPTsqlite3MisuseError(86732);
86733 }
86734 db = v->db;
86735 sqlite3_mutex_enter(db->mutex);
86736 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA17
86737 && cnt++ < SQLITE_MAX_SCHEMA_RETRY50 ){
86738 int savedPc = v->pc;
86739 rc = sqlite3Reprepare(v);
86740 if( rc!=SQLITE_OK0 ){
86741 /* This case occurs after failing to recompile an sql statement.
86742 ** The error message from the SQL compiler has already been loaded
86743 ** into the database handle. This block copies the error message
86744 ** from the database handle into the statement and sets the statement
86745 ** program counter to 0 to ensure that when the statement is
86746 ** finalized or reset the parser error message is available via
86747 ** sqlite3_errmsg() and sqlite3_errcode().
86748 */
86749 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
86750 sqlite3DbFree(db, v->zErrMsg);
86751 if( !db->mallocFailed ){
86752 v->zErrMsg = sqlite3DbStrDup(db, zErr);
86753 v->rc = rc = sqlite3ApiExit(db, rc);
86754 } else {
86755 v->zErrMsg = 0;
86756 v->rc = rc = SQLITE_NOMEM_BKPT7;
86757 }
86758 break;
86759 }
86760 sqlite3_reset(pStmt);
86761 if( savedPc>=0 ){
86762 /* Setting minWriteFileFormat to 254 is a signal to the OP_Init and
86763 ** OP_Trace opcodes to *not* perform SQLITE_TRACE_STMT because it has
86764 ** already been done once on a prior invocation that failed due to
86765 ** SQLITE_SCHEMA. tag-20220401a */
86766 v->minWriteFileFormat = 254;
86767 }
86768 assert( v->expired==0 )((void) (0));
86769 }
86770 sqlite3_mutex_leave(db->mutex);
86771 return rc;
86772}
86773
86774
86775/*
86776** Extract the user data from a sqlite3_context structure and return a
86777** pointer to it.
86778*/
86779SQLITE_API void *sqlite3_user_data(sqlite3_context *p){
86780 assert( p && p->pFunc )((void) (0));
86781 return p->pFunc->pUserData;
86782}
86783
86784/*
86785** Extract the user data from a sqlite3_context structure and return a
86786** pointer to it.
86787**
86788** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
86789** returns a copy of the pointer to the database connection (the 1st
86790** parameter) of the sqlite3_create_function() and
86791** sqlite3_create_function16() routines that originally registered the
86792** application defined function.
86793*/
86794SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
86795 assert( p && p->pOut )((void) (0));
86796 return p->pOut->db;
86797}
86798
86799/*
86800** If this routine is invoked from within an xColumn method of a virtual
86801** table, then it returns true if and only if the the call is during an
86802** UPDATE operation and the value of the column will not be modified
86803** by the UPDATE.
86804**
86805** If this routine is called from any context other than within the
86806** xColumn method of a virtual table, then the return value is meaningless
86807** and arbitrary.
86808**
86809** Virtual table implements might use this routine to optimize their
86810** performance by substituting a NULL result, or some other light-weight
86811** value, as a signal to the xUpdate routine that the column is unchanged.
86812*/
86813SQLITE_API int sqlite3_vtab_nochange(sqlite3_context *p){
86814 assert( p )((void) (0));
86815 return sqlite3_value_nochange(p->pOut);
86816}
86817
86818/*
86819** Implementation of sqlite3_vtab_in_first() (if bNext==0) and
86820** sqlite3_vtab_in_next() (if bNext!=0).
86821*/
86822static int valueFromValueList(
86823 sqlite3_value *pVal, /* Pointer to the ValueList object */
86824 sqlite3_value **ppOut, /* Store the next value from the list here */
86825 int bNext /* 1 for _next(). 0 for _first() */
86826){
86827 int rc;
86828 ValueList *pRhs;
86829
86830 *ppOut = 0;
86831 if( pVal==0 ) return SQLITE_MISUSE21;
86832 pRhs = (ValueList*)sqlite3_value_pointer(pVal, "ValueList");
86833 if( pRhs==0 ) return SQLITE_MISUSE21;
86834 if( bNext ){
86835 rc = sqlite3BtreeNext(pRhs->pCsr, 0);
86836 }else{
86837 int dummy = 0;
86838 rc = sqlite3BtreeFirst(pRhs->pCsr, &dummy);
86839 assert( rc==SQLITE_OK || sqlite3BtreeEof(pRhs->pCsr) )((void) (0));
86840 if( sqlite3BtreeEof(pRhs->pCsr) ) rc = SQLITE_DONE101;
86841 }
86842 if( rc==SQLITE_OK0 ){
86843 u32 sz; /* Size of current row in bytes */
86844 Mem sMem; /* Raw content of current row */
86845 memset(&sMem, 0, sizeof(sMem));
86846 sz = sqlite3BtreePayloadSize(pRhs->pCsr);
86847 rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,(int)sz,&sMem);
86848 if( rc==SQLITE_OK0 ){
86849 u8 *zBuf = (u8*)sMem.z;
86850 u32 iSerial;
86851 sqlite3_value *pOut = pRhs->pOut;
86852 int iOff = 1 + getVarint32(&zBuf[1], iSerial)(u8)((*(&zBuf[1])<(u8)0x80)?((iSerial)=(u32)*(&zBuf
[1])),1:sqlite3GetVarint32((&zBuf[1]),(u32 *)&(iSerial
)))
;
86853 sqlite3VdbeSerialGet(&zBuf[iOff], iSerial, pOut);
86854 pOut->enc = ENC(pOut->db)((pOut->db)->enc);
86855 if( (pOut->flags & MEM_Ephem0x4000)!=0 && sqlite3VdbeMemMakeWriteable(pOut) ){
86856 rc = SQLITE_NOMEM7;
86857 }else{
86858 *ppOut = pOut;
86859 }
86860 }
86861 sqlite3VdbeMemRelease(&sMem);
86862 }
86863 return rc;
86864}
86865
86866/*
86867** Set the iterator value pVal to point to the first value in the set.
86868** Set (*ppOut) to point to this value before returning.
86869*/
86870SQLITE_API int sqlite3_vtab_in_first(sqlite3_value *pVal, sqlite3_value **ppOut){
86871 return valueFromValueList(pVal, ppOut, 0);
86872}
86873
86874/*
86875** Set the iterator value pVal to point to the next value in the set.
86876** Set (*ppOut) to point to this value before returning.
86877*/
86878SQLITE_API int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut){
86879 return valueFromValueList(pVal, ppOut, 1);
86880}
86881
86882/*
86883** Return the current time for a statement. If the current time
86884** is requested more than once within the same run of a single prepared
86885** statement, the exact same time is returned for each invocation regardless
86886** of the amount of time that elapses between invocations. In other words,
86887** the time returned is always the time of the first call.
86888*/
86889SQLITE_PRIVATEstatic sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
86890 int rc;
86891#ifndef SQLITE_ENABLE_STAT4
86892 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
86893 assert( p->pVdbe!=0 )((void) (0));
86894#else
86895 sqlite3_int64 iTime = 0;
86896 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
86897#endif
86898 if( *piTime==0 ){
86899 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
86900 if( rc ) *piTime = 0;
86901 }
86902 return *piTime;
86903}
86904
86905/*
86906** Create a new aggregate context for p and return a pointer to
86907** its pMem->z element.
86908*/
86909static SQLITE_NOINLINE__attribute__((noinline)) void *createAggContext(sqlite3_context *p, int nByte){
86910 Mem *pMem = p->pMem;
86911 assert( (pMem->flags & MEM_Agg)==0 )((void) (0));
86912 if( nByte<=0 ){
86913 sqlite3VdbeMemSetNull(pMem);
86914 pMem->z = 0;
86915 }else{
86916 sqlite3VdbeMemClearAndResize(pMem, nByte);
86917 pMem->flags = MEM_Agg0x8000;
86918 pMem->u.pDef = p->pFunc;
86919 if( pMem->z ){
86920 memset(pMem->z, 0, nByte);
86921 }
86922 }
86923 return (void*)pMem->z;
86924}
86925
86926/*
86927** Allocate or return the aggregate context for a user function. A new
86928** context is allocated on the first call. Subsequent calls return the
86929** same context that was returned on prior calls.
86930*/
86931SQLITE_API void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
86932 assert( p && p->pFunc && p->pFunc->xFinalize )((void) (0));
86933 assert( sqlite3_mutex_held(p->pOut->db->mutex) )((void) (0));
86934 testcase( nByte<0 );
86935 if( (p->pMem->flags & MEM_Agg0x8000)==0 ){
86936 return createAggContext(p, nByte);
86937 }else{
86938 return (void*)p->pMem->z;
86939 }
86940}
86941
86942/*
86943** Return the auxiliary data pointer, if any, for the iArg'th argument to
86944** the user-function defined by pCtx.
86945**
86946** The left-most argument is 0.
86947**
86948** Undocumented behavior: If iArg is negative then access a cache of
86949** auxiliary data pointers that is available to all functions within a
86950** single prepared statement. The iArg values must match.
86951*/
86952SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
86953 AuxData *pAuxData;
86954
86955 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
86956#if SQLITE_ENABLE_STAT4
86957 if( pCtx->pVdbe==0 ) return 0;
86958#else
86959 assert( pCtx->pVdbe!=0 )((void) (0));
86960#endif
86961 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
86962 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
86963 return pAuxData->pAux;
86964 }
86965 }
86966 return 0;
86967}
86968
86969/*
86970** Set the auxiliary data pointer and delete function, for the iArg'th
86971** argument to the user-function defined by pCtx. Any previous value is
86972** deleted by calling the delete function specified when it was set.
86973**
86974** The left-most argument is 0.
86975**
86976** Undocumented behavior: If iArg is negative then make the data available
86977** to all functions within the current prepared statement using iArg as an
86978** access code.
86979*/
86980SQLITE_API void sqlite3_set_auxdata(
86981 sqlite3_context *pCtx,
86982 int iArg,
86983 void *pAux,
86984 void (*xDelete)(void*)
86985){
86986 AuxData *pAuxData;
86987 Vdbe *pVdbe = pCtx->pVdbe;
86988
86989 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
86990#ifdef SQLITE_ENABLE_STAT4
86991 if( pVdbe==0 ) goto failed;
86992#else
86993 assert( pVdbe!=0 )((void) (0));
86994#endif
86995
86996 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
86997 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
86998 break;
86999 }
87000 }
87001 if( pAuxData==0 ){
87002 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
87003 if( !pAuxData ) goto failed;
87004 pAuxData->iAuxOp = pCtx->iOp;
87005 pAuxData->iAuxArg = iArg;
87006 pAuxData->pNextAux = pVdbe->pAuxData;
87007 pVdbe->pAuxData = pAuxData;
87008 if( pCtx->isError==0 ) pCtx->isError = -1;
87009 }else if( pAuxData->xDeleteAux ){
87010 pAuxData->xDeleteAux(pAuxData->pAux);
87011 }
87012
87013 pAuxData->pAux = pAux;
87014 pAuxData->xDeleteAux = xDelete;
87015 return;
87016
87017failed:
87018 if( xDelete ){
87019 xDelete(pAux);
87020 }
87021}
87022
87023#ifndef SQLITE_OMIT_DEPRECATED1
87024/*
87025** Return the number of times the Step function of an aggregate has been
87026** called.
87027**
87028** This function is deprecated. Do not use it for new code. It is
87029** provide only to avoid breaking legacy code. New aggregate function
87030** implementations should keep their own counts within their aggregate
87031** context.
87032*/
87033SQLITE_API int sqlite3_aggregate_count(sqlite3_context *p){
87034 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize )((void) (0));
87035 return p->pMem->n;
87036}
87037#endif
87038
87039/*
87040** Return the number of columns in the result set for the statement pStmt.
87041*/
87042SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){
87043 Vdbe *pVm = (Vdbe *)pStmt;
87044 return pVm ? pVm->nResColumn : 0;
87045}
87046
87047/*
87048** Return the number of values available from the current row of the
87049** currently executing statement pStmt.
87050*/
87051SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){
87052 Vdbe *pVm = (Vdbe *)pStmt;
87053 if( pVm==0 || pVm->pResultSet==0 ) return 0;
87054 return pVm->nResColumn;
87055}
87056
87057/*
87058** Return a pointer to static memory containing an SQL NULL value.
87059*/
87060static const Mem *columnNullValue(void){
87061 /* Even though the Mem structure contains an element
87062 ** of type i64, on certain architectures (x86) with certain compiler
87063 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
87064 ** instead of an 8-byte one. This all works fine, except that when
87065 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
87066 ** that a Mem structure is located on an 8-byte boundary. To prevent
87067 ** these assert()s from failing, when building with SQLITE_DEBUG defined
87068 ** using gcc, we force nullMem to be 8-byte aligned using the magical
87069 ** __attribute__((aligned(8))) macro. */
87070 static const Mem nullMem
87071#if defined(SQLITE_DEBUG) && defined(__GNUC__4)
87072 __attribute__((aligned(8)))
87073#endif
87074 = {
87075 /* .u = */ {0},
87076 /* .z = */ (char*)0,
87077 /* .n = */ (int)0,
87078 /* .flags = */ (u16)MEM_Null0x0001,
87079 /* .enc = */ (u8)0,
87080 /* .eSubtype = */ (u8)0,
87081 /* .db = */ (sqlite3*)0,
87082 /* .szMalloc = */ (int)0,
87083 /* .uTemp = */ (u32)0,
87084 /* .zMalloc = */ (char*)0,
87085 /* .xDel = */ (void(*)(void*))0,
87086#ifdef SQLITE_DEBUG
87087 /* .pScopyFrom = */ (Mem*)0,
87088 /* .mScopyFlags= */ 0,
87089#endif
87090 };
87091 return &nullMem;
87092}
87093
87094/*
87095** Check to see if column iCol of the given statement is valid. If
87096** it is, return a pointer to the Mem for the value of that column.
87097** If iCol is not valid, return a pointer to a Mem which has a value
87098** of NULL.
87099*/
87100static Mem *columnMem(sqlite3_stmt *pStmt, int i){
87101 Vdbe *pVm;
87102 Mem *pOut;
87103
87104 pVm = (Vdbe *)pStmt;
87105 if( pVm==0 ) return (Mem*)columnNullValue();
87106 assert( pVm->db )((void) (0));
87107 sqlite3_mutex_enter(pVm->db->mutex);
87108 if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
87109 pOut = &pVm->pResultSet[i];
87110 }else{
87111 sqlite3Error(pVm->db, SQLITE_RANGE25);
87112 pOut = (Mem*)columnNullValue();
87113 }
87114 return pOut;
87115}
87116
87117/*
87118** This function is called after invoking an sqlite3_value_XXX function on a
87119** column value (i.e. a value returned by evaluating an SQL expression in the
87120** select list of a SELECT statement) that may cause a malloc() failure. If
87121** malloc() has failed, the threads mallocFailed flag is cleared and the result
87122** code of statement pStmt set to SQLITE_NOMEM.
87123**
87124** Specifically, this is called from within:
87125**
87126** sqlite3_column_int()
87127** sqlite3_column_int64()
87128** sqlite3_column_text()
87129** sqlite3_column_text16()
87130** sqlite3_column_real()
87131** sqlite3_column_bytes()
87132** sqlite3_column_bytes16()
87133** sqiite3_column_blob()
87134*/
87135static void columnMallocFailure(sqlite3_stmt *pStmt)
87136{
87137 /* If malloc() failed during an encoding conversion within an
87138 ** sqlite3_column_XXX API, then set the return code of the statement to
87139 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
87140 ** and _finalize() will return NOMEM.
87141 */
87142 Vdbe *p = (Vdbe *)pStmt;
87143 if( p ){
87144 assert( p->db!=0 )((void) (0));
87145 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
87146 p->rc = sqlite3ApiExit(p->db, p->rc);
87147 sqlite3_mutex_leave(p->db->mutex);
87148 }
87149}
87150
87151/**************************** sqlite3_column_ *******************************
87152** The following routines are used to access elements of the current row
87153** in the result set.
87154*/
87155SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
87156 const void *val;
87157 val = sqlite3_value_blob( columnMem(pStmt,i) );
87158 /* Even though there is no encoding conversion, value_blob() might
87159 ** need to call malloc() to expand the result of a zeroblob()
87160 ** expression.
87161 */
87162 columnMallocFailure(pStmt);
87163 return val;
87164}
87165SQLITE_API int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
87166 int val = sqlite3_value_bytes( columnMem(pStmt,i) );
87167 columnMallocFailure(pStmt);
87168 return val;
87169}
87170SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
87171 int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
87172 columnMallocFailure(pStmt);
87173 return val;
87174}
87175SQLITE_API double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
87176 double val = sqlite3_value_double( columnMem(pStmt,i) );
87177 columnMallocFailure(pStmt);
87178 return val;
87179}
87180SQLITE_API int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
87181 int val = sqlite3_value_int( columnMem(pStmt,i) );
87182 columnMallocFailure(pStmt);
87183 return val;
87184}
87185SQLITE_API sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
87186 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
87187 columnMallocFailure(pStmt);
87188 return val;
87189}
87190SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
87191 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
87192 columnMallocFailure(pStmt);
87193 return val;
87194}
87195SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
87196 Mem *pOut = columnMem(pStmt, i);
87197 if( pOut->flags&MEM_Static0x2000 ){
87198 pOut->flags &= ~MEM_Static0x2000;
87199 pOut->flags |= MEM_Ephem0x4000;
87200 }
87201 columnMallocFailure(pStmt);
87202 return (sqlite3_value *)pOut;
87203}
87204#ifndef SQLITE_OMIT_UTF16
87205SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
87206 const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
87207 columnMallocFailure(pStmt);
87208 return val;
87209}
87210#endif /* SQLITE_OMIT_UTF16 */
87211SQLITE_API int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
87212 int iType = sqlite3_value_type( columnMem(pStmt,i) );
87213 columnMallocFailure(pStmt);
87214 return iType;
87215}
87216
87217/*
87218** Convert the N-th element of pStmt->pColName[] into a string using
87219** xFunc() then return that string. If N is out of range, return 0.
87220**
87221** There are up to 5 names for each column. useType determines which
87222** name is returned. Here are the names:
87223**
87224** 0 The column name as it should be displayed for output
87225** 1 The datatype name for the column
87226** 2 The name of the database that the column derives from
87227** 3 The name of the table that the column derives from
87228** 4 The name of the table column that the result column derives from
87229**
87230** If the result is not a simple column reference (if it is an expression
87231** or a constant) then useTypes 2, 3, and 4 return NULL.
87232*/
87233static const void *columnName(
87234 sqlite3_stmt *pStmt, /* The statement */
87235 int N, /* Which column to get the name for */
87236 int useUtf16, /* True to return the name as UTF16 */
87237 int useType /* What type of name */
87238){
87239 const void *ret;
87240 Vdbe *p;
87241 int n;
87242 sqlite3 *db;
87243#ifdef SQLITE_ENABLE_API_ARMOR1
87244 if( pStmt==0 ){
87245 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(87245);
87246 return 0;
87247 }
87248#endif
87249 ret = 0;
87250 p = (Vdbe *)pStmt;
87251 db = p->db;
87252 assert( db!=0 )((void) (0));
87253 n = sqlite3_column_count(pStmt);
87254 if( N<n && N>=0 ){
87255 N += useType*n;
87256 sqlite3_mutex_enter(db->mutex);
87257 assert( db->mallocFailed==0 )((void) (0));
87258#ifndef SQLITE_OMIT_UTF16
87259 if( useUtf16 ){
87260 ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]);
87261 }else
87262#endif
87263 {
87264 ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]);
87265 }
87266 /* A malloc may have failed inside of the _text() call. If this
87267 ** is the case, clear the mallocFailed flag and return NULL.
87268 */
87269 if( db->mallocFailed ){
87270 sqlite3OomClear(db);
87271 ret = 0;
87272 }
87273 sqlite3_mutex_leave(db->mutex);
87274 }
87275 return ret;
87276}
87277
87278/*
87279** Return the name of the Nth column of the result set returned by SQL
87280** statement pStmt.
87281*/
87282SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
87283 return columnName(pStmt, N, 0, COLNAME_NAME0);
87284}
87285#ifndef SQLITE_OMIT_UTF16
87286SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
87287 return columnName(pStmt, N, 1, COLNAME_NAME0);
87288}
87289#endif
87290
87291/*
87292** Constraint: If you have ENABLE_COLUMN_METADATA then you must
87293** not define OMIT_DECLTYPE.
87294*/
87295#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA1)
87296# error "Must not define both SQLITE_OMIT_DECLTYPE \
87297 and SQLITE_ENABLE_COLUMN_METADATA"
87298#endif
87299
87300#ifndef SQLITE_OMIT_DECLTYPE
87301/*
87302** Return the column declaration type (if applicable) of the 'i'th column
87303** of the result set of SQL statement pStmt.
87304*/
87305SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
87306 return columnName(pStmt, N, 0, COLNAME_DECLTYPE1);
87307}
87308#ifndef SQLITE_OMIT_UTF16
87309SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
87310 return columnName(pStmt, N, 1, COLNAME_DECLTYPE1);
87311}
87312#endif /* SQLITE_OMIT_UTF16 */
87313#endif /* SQLITE_OMIT_DECLTYPE */
87314
87315#ifdef SQLITE_ENABLE_COLUMN_METADATA1
87316/*
87317** Return the name of the database from which a result column derives.
87318** NULL is returned if the result column is an expression or constant or
87319** anything else which is not an unambiguous reference to a database column.
87320*/
87321SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
87322 return columnName(pStmt, N, 0, COLNAME_DATABASE2);
87323}
87324#ifndef SQLITE_OMIT_UTF16
87325SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
87326 return columnName(pStmt, N, 1, COLNAME_DATABASE2);
87327}
87328#endif /* SQLITE_OMIT_UTF16 */
87329
87330/*
87331** Return the name of the table from which a result column derives.
87332** NULL is returned if the result column is an expression or constant or
87333** anything else which is not an unambiguous reference to a database column.
87334*/
87335SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
87336 return columnName(pStmt, N, 0, COLNAME_TABLE3);
87337}
87338#ifndef SQLITE_OMIT_UTF16
87339SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
87340 return columnName(pStmt, N, 1, COLNAME_TABLE3);
87341}
87342#endif /* SQLITE_OMIT_UTF16 */
87343
87344/*
87345** Return the name of the table column from which a result column derives.
87346** NULL is returned if the result column is an expression or constant or
87347** anything else which is not an unambiguous reference to a database column.
87348*/
87349SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
87350 return columnName(pStmt, N, 0, COLNAME_COLUMN4);
87351}
87352#ifndef SQLITE_OMIT_UTF16
87353SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
87354 return columnName(pStmt, N, 1, COLNAME_COLUMN4);
87355}
87356#endif /* SQLITE_OMIT_UTF16 */
87357#endif /* SQLITE_ENABLE_COLUMN_METADATA */
87358
87359
87360/******************************* sqlite3_bind_ ***************************
87361**
87362** Routines used to attach values to wildcards in a compiled SQL statement.
87363*/
87364/*
87365** Unbind the value bound to variable i in virtual machine p. This is the
87366** the same as binding a NULL value to the column. If the "i" parameter is
87367** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
87368**
87369** A successful evaluation of this routine acquires the mutex on p.
87370** the mutex is released if any kind of error occurs.
87371**
87372** The error code stored in database p->db is overwritten with the return
87373** value in any case.
87374*/
87375static int vdbeUnbind(Vdbe *p, int i){
87376 Mem *pVar;
87377 if( vdbeSafetyNotNull(p) ){
87378 return SQLITE_MISUSE_BKPTsqlite3MisuseError(87378);
87379 }
87380 sqlite3_mutex_enter(p->db->mutex);
87381 if( p->eVdbeState!=VDBE_READY_STATE1 ){
87382 sqlite3Error(p->db, SQLITE_MISUSE21);
87383 sqlite3_mutex_leave(p->db->mutex);
87384 sqlite3_log(SQLITE_MISUSE21,
87385 "bind on a busy prepared statement: [%s]", p->zSql);
87386 return SQLITE_MISUSE_BKPTsqlite3MisuseError(87386);
87387 }
87388 if( i<1 || i>p->nVar ){
87389 sqlite3Error(p->db, SQLITE_RANGE25);
87390 sqlite3_mutex_leave(p->db->mutex);
87391 return SQLITE_RANGE25;
87392 }
87393 i--;
87394 pVar = &p->aVar[i];
87395 sqlite3VdbeMemRelease(pVar);
87396 pVar->flags = MEM_Null0x0001;
87397 p->db->errCode = SQLITE_OK0;
87398
87399 /* If the bit corresponding to this variable in Vdbe.expmask is set, then
87400 ** binding a new value to this variable invalidates the current query plan.
87401 **
87402 ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host
87403 ** parameter in the WHERE clause might influence the choice of query plan
87404 ** for a statement, then the statement will be automatically recompiled,
87405 ** as if there had been a schema change, on the first sqlite3_step() call
87406 ** following any change to the bindings of that parameter.
87407 */
87408 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 )((void) (0));
87409 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
87410 p->expired = 1;
87411 }
87412 return SQLITE_OK0;
87413}
87414
87415/*
87416** Bind a text or BLOB value.
87417*/
87418static int bindText(
87419 sqlite3_stmt *pStmt, /* The statement to bind against */
87420 int i, /* Index of the parameter to bind */
87421 const void *zData, /* Pointer to the data to be bound */
87422 i64 nData, /* Number of bytes of data to be bound */
87423 void (*xDel)(void*), /* Destructor for the data */
87424 u8 encoding /* Encoding for the data */
87425){
87426 Vdbe *p = (Vdbe *)pStmt;
87427 Mem *pVar;
87428 int rc;
87429
87430 rc = vdbeUnbind(p, i);
87431 if( rc==SQLITE_OK0 ){
87432 if( zData!=0 ){
87433 pVar = &p->aVar[i-1];
87434 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
87435 if( rc==SQLITE_OK0 && encoding!=0 ){
87436 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)((p->db)->enc));
87437 }
87438 if( rc ){
87439 sqlite3Error(p->db, rc);
87440 rc = sqlite3ApiExit(p->db, rc);
87441 }
87442 }
87443 sqlite3_mutex_leave(p->db->mutex);
87444 }else if( xDel!=SQLITE_STATIC((sqlite3_destructor_type)0) && xDel!=SQLITE_TRANSIENT((sqlite3_destructor_type)-1) ){
87445 xDel((void*)zData);
87446 }
87447 return rc;
87448}
87449
87450
87451/*
87452** Bind a blob value to an SQL statement variable.
87453*/
87454SQLITE_API int sqlite3_bind_blob(
87455 sqlite3_stmt *pStmt,
87456 int i,
87457 const void *zData,
87458 int nData,
87459 void (*xDel)(void*)
87460){
87461#ifdef SQLITE_ENABLE_API_ARMOR1
87462 if( nData<0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(87462);
87463#endif
87464 return bindText(pStmt, i, zData, nData, xDel, 0);
87465}
87466SQLITE_API int sqlite3_bind_blob64(
87467 sqlite3_stmt *pStmt,
87468 int i,
87469 const void *zData,
87470 sqlite3_uint64 nData,
87471 void (*xDel)(void*)
87472){
87473 assert( xDel!=SQLITE_DYNAMIC )((void) (0));
87474 return bindText(pStmt, i, zData, nData, xDel, 0);
87475}
87476SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
87477 int rc;
87478 Vdbe *p = (Vdbe *)pStmt;
87479 rc = vdbeUnbind(p, i);
87480 if( rc==SQLITE_OK0 ){
87481 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
87482 sqlite3_mutex_leave(p->db->mutex);
87483 }
87484 return rc;
87485}
87486SQLITE_API int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
87487 return sqlite3_bind_int64(p, i, (i64)iValue);
87488}
87489SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
87490 int rc;
87491 Vdbe *p = (Vdbe *)pStmt;
87492 rc = vdbeUnbind(p, i);
87493 if( rc==SQLITE_OK0 ){
87494 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
87495 sqlite3_mutex_leave(p->db->mutex);
87496 }
87497 return rc;
87498}
87499SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
87500 int rc;
87501 Vdbe *p = (Vdbe*)pStmt;
87502 rc = vdbeUnbind(p, i);
87503 if( rc==SQLITE_OK0 ){
87504 sqlite3_mutex_leave(p->db->mutex);
87505 }
87506 return rc;
87507}
87508SQLITE_API int sqlite3_bind_pointer(
87509 sqlite3_stmt *pStmt,
87510 int i,
87511 void *pPtr,
87512 const char *zPTtype,
87513 void (*xDestructor)(void*)
87514){
87515 int rc;
87516 Vdbe *p = (Vdbe*)pStmt;
87517 rc = vdbeUnbind(p, i);
87518 if( rc==SQLITE_OK0 ){
87519 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
87520 sqlite3_mutex_leave(p->db->mutex);
87521 }else if( xDestructor ){
87522 xDestructor(pPtr);
87523 }
87524 return rc;
87525}
87526SQLITE_API int sqlite3_bind_text(
87527 sqlite3_stmt *pStmt,
87528 int i,
87529 const char *zData,
87530 int nData,
87531 void (*xDel)(void*)
87532){
87533 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF81);
87534}
87535SQLITE_API int sqlite3_bind_text64(
87536 sqlite3_stmt *pStmt,
87537 int i,
87538 const char *zData,
87539 sqlite3_uint64 nData,
87540 void (*xDel)(void*),
87541 unsigned char enc
87542){
87543 assert( xDel!=SQLITE_DYNAMIC )((void) (0));
87544 if( enc==SQLITE_UTF164 ) enc = SQLITE_UTF16NATIVE2;
87545 return bindText(pStmt, i, zData, nData, xDel, enc);
87546}
87547#ifndef SQLITE_OMIT_UTF16
87548SQLITE_API int sqlite3_bind_text16(
87549 sqlite3_stmt *pStmt,
87550 int i,
87551 const void *zData,
87552 int nData,
87553 void (*xDel)(void*)
87554){
87555 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE2);
87556}
87557#endif /* SQLITE_OMIT_UTF16 */
87558SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
87559 int rc;
87560 switch( sqlite3_value_type((sqlite3_value*)pValue) ){
87561 case SQLITE_INTEGER1: {
87562 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
87563 break;
87564 }
87565 case SQLITE_FLOAT2: {
87566 assert( pValue->flags & (MEM_Real|MEM_IntReal) )((void) (0));
87567 rc = sqlite3_bind_double(pStmt, i,
87568 (pValue->flags & MEM_Real0x0008) ? pValue->u.r : (double)pValue->u.i
87569 );
87570 break;
87571 }
87572 case SQLITE_BLOB4: {
87573 if( pValue->flags & MEM_Zero0x0400 ){
87574 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
87575 }else{
87576 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
87577 }
87578 break;
87579 }
87580 case SQLITE_TEXT3: {
87581 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1),
87582 pValue->enc);
87583 break;
87584 }
87585 default: {
87586 rc = sqlite3_bind_null(pStmt, i);
87587 break;
87588 }
87589 }
87590 return rc;
87591}
87592SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
87593 int rc;
87594 Vdbe *p = (Vdbe *)pStmt;
87595 rc = vdbeUnbind(p, i);
87596 if( rc==SQLITE_OK0 ){
87597#ifndef SQLITE_OMIT_INCRBLOB
87598 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
87599#else
87600 rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
87601#endif
87602 sqlite3_mutex_leave(p->db->mutex);
87603 }
87604 return rc;
87605}
87606SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){
87607 int rc;
87608 Vdbe *p = (Vdbe *)pStmt;
87609 sqlite3_mutex_enter(p->db->mutex);
87610 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH0] ){
87611 rc = SQLITE_TOOBIG18;
87612 }else{
87613 assert( (n & 0x7FFFFFFF)==n )((void) (0));
87614 rc = sqlite3_bind_zeroblob(pStmt, i, n);
87615 }
87616 rc = sqlite3ApiExit(p->db, rc);
87617 sqlite3_mutex_leave(p->db->mutex);
87618 return rc;
87619}
87620
87621/*
87622** Return the number of wildcards that can be potentially bound to.
87623** This routine is added to support DBD::SQLite.
87624*/
87625SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
87626 Vdbe *p = (Vdbe*)pStmt;
87627 return p ? p->nVar : 0;
87628}
87629
87630/*
87631** Return the name of a wildcard parameter. Return NULL if the index
87632** is out of range or if the wildcard is unnamed.
87633**
87634** The result is always UTF-8.
87635*/
87636SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
87637 Vdbe *p = (Vdbe*)pStmt;
87638 if( p==0 ) return 0;
87639 return sqlite3VListNumToName(p->pVList, i);
87640}
87641
87642/*
87643** Given a wildcard parameter name, return the index of the variable
87644** with that name. If there is no variable with the given name,
87645** return 0.
87646*/
87647SQLITE_PRIVATEstatic int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
87648 if( p==0 || zName==0 ) return 0;
87649 return sqlite3VListNameToNum(p->pVList, zName, nName);
87650}
87651SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
87652 return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
87653}
87654
87655/*
87656** Transfer all bindings from the first statement over to the second.
87657*/
87658SQLITE_PRIVATEstatic int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
87659 Vdbe *pFrom = (Vdbe*)pFromStmt;
87660 Vdbe *pTo = (Vdbe*)pToStmt;
87661 int i;
87662 assert( pTo->db==pFrom->db )((void) (0));
87663 assert( pTo->nVar==pFrom->nVar )((void) (0));
87664 sqlite3_mutex_enter(pTo->db->mutex);
87665 for(i=0; i<pFrom->nVar; i++){
87666 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
87667 }
87668 sqlite3_mutex_leave(pTo->db->mutex);
87669 return SQLITE_OK0;
87670}
87671
87672#ifndef SQLITE_OMIT_DEPRECATED1
87673/*
87674** Deprecated external interface. Internal/core SQLite code
87675** should call sqlite3TransferBindings.
87676**
87677** It is misuse to call this routine with statements from different
87678** database connections. But as this is a deprecated interface, we
87679** will not bother to check for that condition.
87680**
87681** If the two statements contain a different number of bindings, then
87682** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise
87683** SQLITE_OK is returned.
87684*/
87685SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
87686 Vdbe *pFrom = (Vdbe*)pFromStmt;
87687 Vdbe *pTo = (Vdbe*)pToStmt;
87688 if( pFrom->nVar!=pTo->nVar ){
87689 return SQLITE_ERROR1;
87690 }
87691 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 )((void) (0));
87692 if( pTo->expmask ){
87693 pTo->expired = 1;
87694 }
87695 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 )((void) (0));
87696 if( pFrom->expmask ){
87697 pFrom->expired = 1;
87698 }
87699 return sqlite3TransferBindings(pFromStmt, pToStmt);
87700}
87701#endif
87702
87703/*
87704** Return the sqlite3* database handle to which the prepared statement given
87705** in the argument belongs. This is the same database handle that was
87706** the first argument to the sqlite3_prepare() that was used to create
87707** the statement in the first place.
87708*/
87709SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
87710 return pStmt ? ((Vdbe*)pStmt)->db : 0;
87711}
87712
87713/*
87714** Return true if the prepared statement is guaranteed to not modify the
87715** database.
87716*/
87717SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
87718 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
87719}
87720
87721/*
87722** Return 1 if the statement is an EXPLAIN and return 2 if the
87723** statement is an EXPLAIN QUERY PLAN
87724*/
87725SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt){
87726 return pStmt ? ((Vdbe*)pStmt)->explain : 0;
87727}
87728
87729/*
87730** Return true if the prepared statement is in need of being reset.
87731*/
87732SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
87733 Vdbe *v = (Vdbe*)pStmt;
87734 return v!=0 && v->eVdbeState==VDBE_RUN_STATE2;
87735}
87736
87737/*
87738** Return a pointer to the next prepared statement after pStmt associated
87739** with database connection pDb. If pStmt is NULL, return the first
87740** prepared statement for the database connection. Return NULL if there
87741** are no more.
87742*/
87743SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
87744 sqlite3_stmt *pNext;
87745#ifdef SQLITE_ENABLE_API_ARMOR1
87746 if( !sqlite3SafetyCheckOk(pDb) ){
87747 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(87747);
87748 return 0;
87749 }
87750#endif
87751 sqlite3_mutex_enter(pDb->mutex);
87752 if( pStmt==0 ){
87753 pNext = (sqlite3_stmt*)pDb->pVdbe;
87754 }else{
87755 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
87756 }
87757 sqlite3_mutex_leave(pDb->mutex);
87758 return pNext;
87759}
87760
87761/*
87762** Return the value of a status counter for a prepared statement
87763*/
87764SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
87765 Vdbe *pVdbe = (Vdbe*)pStmt;
87766 u32 v;
87767#ifdef SQLITE_ENABLE_API_ARMOR1
87768 if( !pStmt
87769 || (op!=SQLITE_STMTSTATUS_MEMUSED99 && (op<0||op>=ArraySize(pVdbe->aCounter)((int)(sizeof(pVdbe->aCounter)/sizeof(pVdbe->aCounter[0
])))
))
87770 ){
87771 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(87771);
87772 return 0;
87773 }
87774#endif
87775 if( op==SQLITE_STMTSTATUS_MEMUSED99 ){
87776 sqlite3 *db = pVdbe->db;
87777 sqlite3_mutex_enter(db->mutex);
87778 v = 0;
87779 db->pnBytesFreed = (int*)&v;
87780 sqlite3VdbeDelete(pVdbe);
87781 db->pnBytesFreed = 0;
87782 sqlite3_mutex_leave(db->mutex);
87783 }else{
87784 v = pVdbe->aCounter[op];
87785 if( resetFlag ) pVdbe->aCounter[op] = 0;
87786 }
87787 return (int)v;
87788}
87789
87790/*
87791** Return the SQL associated with a prepared statement
87792*/
87793SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt){
87794 Vdbe *p = (Vdbe *)pStmt;
87795 return p ? p->zSql : 0;
87796}
87797
87798/*
87799** Return the SQL associated with a prepared statement with
87800** bound parameters expanded. Space to hold the returned string is
87801** obtained from sqlite3_malloc(). The caller is responsible for
87802** freeing the returned string by passing it to sqlite3_free().
87803**
87804** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of
87805** expanded bound parameters.
87806*/
87807SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){
87808#ifdef SQLITE_OMIT_TRACE
87809 return 0;
87810#else
87811 char *z = 0;
87812 const char *zSql = sqlite3_sql(pStmt);
87813 if( zSql ){
87814 Vdbe *p = (Vdbe *)pStmt;
87815 sqlite3_mutex_enter(p->db->mutex);
87816 z = sqlite3VdbeExpandSql(p, zSql);
87817 sqlite3_mutex_leave(p->db->mutex);
87818 }
87819 return z;
87820#endif
87821}
87822
87823#ifdef SQLITE_ENABLE_NORMALIZE
87824/*
87825** Return the normalized SQL associated with a prepared statement.
87826*/
87827SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt){
87828 Vdbe *p = (Vdbe *)pStmt;
87829 if( p==0 ) return 0;
87830 if( p->zNormSql==0 && ALWAYS(p->zSql!=0)(p->zSql!=0) ){
87831 sqlite3_mutex_enter(p->db->mutex);
87832 p->zNormSql = sqlite3Normalize(p, p->zSql);
87833 sqlite3_mutex_leave(p->db->mutex);
87834 }
87835 return p->zNormSql;
87836}
87837#endif /* SQLITE_ENABLE_NORMALIZE */
87838
87839#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
87840/*
87841** Allocate and populate an UnpackedRecord structure based on the serialized
87842** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
87843** if successful, or a NULL pointer if an OOM error is encountered.
87844*/
87845static UnpackedRecord *vdbeUnpackRecord(
87846 KeyInfo *pKeyInfo,
87847 int nKey,
87848 const void *pKey
87849){
87850 UnpackedRecord *pRet; /* Return value */
87851
87852 pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
87853 if( pRet ){
87854 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1));
87855 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet);
87856 }
87857 return pRet;
87858}
87859
87860/*
87861** This function is called from within a pre-update callback to retrieve
87862** a field of the row currently being updated or deleted.
87863*/
87864SQLITE_API int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
87865 PreUpdate *p = db->pPreUpdate;
87866 Mem *pMem;
87867 int rc = SQLITE_OK0;
87868
87869 /* Test that this call is being made from within an SQLITE_DELETE or
87870 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
87871 if( !p || p->op==SQLITE_INSERT18 ){
87872 rc = SQLITE_MISUSE_BKPTsqlite3MisuseError(87872);
87873 goto preupdate_old_out;
87874 }
87875 if( p->pPk ){
87876 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
87877 }
87878 if( iIdx>=p->pCsr->nField || iIdx<0 ){
87879 rc = SQLITE_RANGE25;
87880 goto preupdate_old_out;
87881 }
87882
87883 /* If the old.* record has not yet been loaded into memory, do so now. */
87884 if( p->pUnpacked==0 ){
87885 u32 nRec;
87886 u8 *aRec;
87887
87888 assert( p->pCsr->eCurType==CURTYPE_BTREE )((void) (0));
87889 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
87890 aRec = sqlite3DbMallocRaw(db, nRec);
87891 if( !aRec ) goto preupdate_old_out;
87892 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
87893 if( rc==SQLITE_OK0 ){
87894 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
87895 if( !p->pUnpacked ) rc = SQLITE_NOMEM7;
87896 }
87897 if( rc!=SQLITE_OK0 ){
87898 sqlite3DbFree(db, aRec);
87899 goto preupdate_old_out;
87900 }
87901 p->aRecord = aRec;
87902 }
87903
87904 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
87905 if( iIdx==p->pTab->iPKey ){
87906 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
87907 }else if( iIdx>=p->pUnpacked->nField ){
87908 *ppValue = (sqlite3_value *)columnNullValue();
87909 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL0x45 ){
87910 if( pMem->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
87911 testcase( pMem->flags & MEM_Int );
87912 testcase( pMem->flags & MEM_IntReal );
87913 sqlite3VdbeMemRealify(pMem);
87914 }
87915 }
87916
87917 preupdate_old_out:
87918 sqlite3Error(db, rc);
87919 return sqlite3ApiExit(db, rc);
87920}
87921#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
87922
87923#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
87924/*
87925** This function is called from within a pre-update callback to retrieve
87926** the number of columns in the row being updated, deleted or inserted.
87927*/
87928SQLITE_API int sqlite3_preupdate_count(sqlite3 *db){
87929 PreUpdate *p = db->pPreUpdate;
87930 return (p ? p->keyinfo.nKeyField : 0);
87931}
87932#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
87933
87934#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
87935/*
87936** This function is designed to be called from within a pre-update callback
87937** only. It returns zero if the change that caused the callback was made
87938** immediately by a user SQL statement. Or, if the change was made by a
87939** trigger program, it returns the number of trigger programs currently
87940** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
87941** top-level trigger etc.).
87942**
87943** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL
87944** or SET DEFAULT action is considered a trigger.
87945*/
87946SQLITE_API int sqlite3_preupdate_depth(sqlite3 *db){
87947 PreUpdate *p = db->pPreUpdate;
87948 return (p ? p->v->nFrame : 0);
87949}
87950#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
87951
87952#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
87953/*
87954** This function is designed to be called from within a pre-update callback
87955** only.
87956*/
87957SQLITE_API int sqlite3_preupdate_blobwrite(sqlite3 *db){
87958 PreUpdate *p = db->pPreUpdate;
87959 return (p ? p->iBlobWrite : -1);
87960}
87961#endif
87962
87963#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
87964/*
87965** This function is called from within a pre-update callback to retrieve
87966** a field of the row currently being updated or inserted.
87967*/
87968SQLITE_API int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
87969 PreUpdate *p = db->pPreUpdate;
87970 int rc = SQLITE_OK0;
87971 Mem *pMem;
87972
87973 if( !p || p->op==SQLITE_DELETE9 ){
87974 rc = SQLITE_MISUSE_BKPTsqlite3MisuseError(87974);
87975 goto preupdate_new_out;
87976 }
87977 if( p->pPk && p->op!=SQLITE_UPDATE23 ){
87978 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
87979 }
87980 if( iIdx>=p->pCsr->nField || iIdx<0 ){
87981 rc = SQLITE_RANGE25;
87982 goto preupdate_new_out;
87983 }
87984
87985 if( p->op==SQLITE_INSERT18 ){
87986 /* For an INSERT, memory cell p->iNewReg contains the serialized record
87987 ** that is being inserted. Deserialize it. */
87988 UnpackedRecord *pUnpack = p->pNewUnpacked;
87989 if( !pUnpack ){
87990 Mem *pData = &p->v->aMem[p->iNewReg];
87991 rc = ExpandBlob(pData)(((pData)->flags&0x0400)?sqlite3VdbeMemExpandBlob(pData
):0)
;
87992 if( rc!=SQLITE_OK0 ) goto preupdate_new_out;
87993 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
87994 if( !pUnpack ){
87995 rc = SQLITE_NOMEM7;
87996 goto preupdate_new_out;
87997 }
87998 p->pNewUnpacked = pUnpack;
87999 }
88000 pMem = &pUnpack->aMem[iIdx];
88001 if( iIdx==p->pTab->iPKey ){
88002 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
88003 }else if( iIdx>=pUnpack->nField ){
88004 pMem = (sqlite3_value *)columnNullValue();
88005 }
88006 }else{
88007 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
88008 ** value. Make a copy of the cell contents and return a pointer to it.
88009 ** It is not safe to return a pointer to the memory cell itself as the
88010 ** caller may modify the value text encoding.
88011 */
88012 assert( p->op==SQLITE_UPDATE )((void) (0));
88013 if( !p->aNew ){
88014 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
88015 if( !p->aNew ){
88016 rc = SQLITE_NOMEM7;
88017 goto preupdate_new_out;
88018 }
88019 }
88020 assert( iIdx>=0 && iIdx<p->pCsr->nField )((void) (0));
88021 pMem = &p->aNew[iIdx];
88022 if( pMem->flags==0 ){
88023 if( iIdx==p->pTab->iPKey ){
88024 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
88025 }else{
88026 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
88027 if( rc!=SQLITE_OK0 ) goto preupdate_new_out;
88028 }
88029 }
88030 }
88031 *ppValue = pMem;
88032
88033 preupdate_new_out:
88034 sqlite3Error(db, rc);
88035 return sqlite3ApiExit(db, rc);
88036}
88037#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
88038
88039#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
88040/*
88041** Return status data for a single loop within query pStmt.
88042*/
88043SQLITE_API int sqlite3_stmt_scanstatus(
88044 sqlite3_stmt *pStmt, /* Prepared statement being queried */
88045 int idx, /* Index of loop to report on */
88046 int iScanStatusOp, /* Which metric to return */
88047 void *pOut /* OUT: Write the answer here */
88048){
88049 Vdbe *p = (Vdbe*)pStmt;
88050 ScanStatus *pScan;
88051 if( idx<0 || idx>=p->nScan ) return 1;
88052 pScan = &p->aScan[idx];
88053 switch( iScanStatusOp ){
88054 case SQLITE_SCANSTAT_NLOOP0: {
88055 *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop];
88056 break;
88057 }
88058 case SQLITE_SCANSTAT_NVISIT1: {
88059 *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit];
88060 break;
88061 }
88062 case SQLITE_SCANSTAT_EST2: {
88063 double r = 1.0;
88064 LogEst x = pScan->nEst;
88065 while( x<100 ){
88066 x += 10;
88067 r *= 0.5;
88068 }
88069 *(double*)pOut = r*sqlite3LogEstToInt(x);
88070 break;
88071 }
88072 case SQLITE_SCANSTAT_NAME3: {
88073 *(const char**)pOut = pScan->zName;
88074 break;
88075 }
88076 case SQLITE_SCANSTAT_EXPLAIN4: {
88077 if( pScan->addrExplain ){
88078 *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z;
88079 }else{
88080 *(const char**)pOut = 0;
88081 }
88082 break;
88083 }
88084 case SQLITE_SCANSTAT_SELECTID5: {
88085 if( pScan->addrExplain ){
88086 *(int*)pOut = p->aOp[ pScan->addrExplain ].p1;
88087 }else{
88088 *(int*)pOut = -1;
88089 }
88090 break;
88091 }
88092 default: {
88093 return 1;
88094 }
88095 }
88096 return 0;
88097}
88098
88099/*
88100** Zero all counters associated with the sqlite3_stmt_scanstatus() data.
88101*/
88102SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){
88103 Vdbe *p = (Vdbe*)pStmt;
88104 memset(p->anExec, 0, p->nOp * sizeof(i64));
88105}
88106#endif /* SQLITE_ENABLE_STMT_SCANSTATUS */
88107
88108/************** End of vdbeapi.c *********************************************/
88109/************** Begin file vdbetrace.c ***************************************/
88110/*
88111** 2009 November 25
88112**
88113** The author disclaims copyright to this source code. In place of
88114** a legal notice, here is a blessing:
88115**
88116** May you do good and not evil.
88117** May you find forgiveness for yourself and forgive others.
88118** May you share freely, never taking more than you give.
88119**
88120*************************************************************************
88121**
88122** This file contains code used to insert the values of host parameters
88123** (aka "wildcards") into the SQL text output by sqlite3_trace().
88124**
88125** The Vdbe parse-tree explainer is also found here.
88126*/
88127/* #include "sqliteInt.h" */
88128/* #include "vdbeInt.h" */
88129
88130#ifndef SQLITE_OMIT_TRACE
88131
88132/*
88133** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
88134** bytes in this text up to but excluding the first character in
88135** a host parameter. If the text contains no host parameters, return
88136** the total number of bytes in the text.
88137*/
88138static int findNextHostParameter(const char *zSql, int *pnToken){
88139 int tokenType;
88140 int nTotal = 0;
88141 int n;
88142
88143 *pnToken = 0;
88144 while( zSql[0] ){
88145 n = sqlite3GetToken((u8*)zSql, &tokenType);
88146 assert( n>0 && tokenType!=TK_ILLEGAL )((void) (0));
88147 if( tokenType==TK_VARIABLE156 ){
88148 *pnToken = n;
88149 break;
88150 }
88151 nTotal += n;
88152 zSql += n;
88153 }
88154 return nTotal;
88155}
88156
88157/*
88158** This function returns a pointer to a nul-terminated string in memory
88159** obtained from sqlite3DbMalloc(). If sqlite3.nVdbeExec is 1, then the
88160** string contains a copy of zRawSql but with host parameters expanded to
88161** their current bindings. Or, if sqlite3.nVdbeExec is greater than 1,
88162** then the returned string holds a copy of zRawSql with "-- " prepended
88163** to each line of text.
88164**
88165** If the SQLITE_TRACE_SIZE_LIMIT macro is defined to an integer, then
88166** then long strings and blobs are truncated to that many bytes. This
88167** can be used to prevent unreasonably large trace strings when dealing
88168** with large (multi-megabyte) strings and blobs.
88169**
88170** The calling function is responsible for making sure the memory returned
88171** is eventually freed.
88172**
88173** ALGORITHM: Scan the input string looking for host parameters in any of
88174** these forms: ?, ?N, $A, @A, :A. Take care to avoid text within
88175** string literals, quoted identifier names, and comments. For text forms,
88176** the host parameter index is found by scanning the prepared
88177** statement for the corresponding OP_Variable opcode. Once the host
88178** parameter index is known, locate the value in p->aVar[]. Then render
88179** the value as a literal in place of the host parameter name.
88180*/
88181SQLITE_PRIVATEstatic char *sqlite3VdbeExpandSql(
88182 Vdbe *p, /* The prepared statement being evaluated */
88183 const char *zRawSql /* Raw text of the SQL statement */
88184){
88185 sqlite3 *db; /* The database connection */
88186 int idx = 0; /* Index of a host parameter */
88187 int nextIndex = 1; /* Index of next ? host parameter */
88188 int n; /* Length of a token prefix */
88189 int nToken; /* Length of the parameter token */
88190 int i; /* Loop counter */
88191 Mem *pVar; /* Value of a host parameter */
88192 StrAccum out; /* Accumulate the output here */
88193#ifndef SQLITE_OMIT_UTF16
88194 Mem utf8; /* Used to convert UTF16 into UTF8 for display */
88195#endif
88196
88197 db = p->db;
88198 sqlite3StrAccumInit(&out, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH0]);
88199 if( db->nVdbeExec>1 ){
88200 while( *zRawSql ){
88201 const char *zStart = zRawSql;
88202 while( *(zRawSql++)!='\n' && *zRawSql );
88203 sqlite3_str_append(&out, "-- ", 3);
88204 assert( (zRawSql - zStart) > 0 )((void) (0));
88205 sqlite3_str_append(&out, zStart, (int)(zRawSql-zStart));
88206 }
88207 }else if( p->nVar==0 ){
88208 sqlite3_str_append(&out, zRawSql, sqlite3Strlen30(zRawSql));
88209 }else{
88210 while( zRawSql[0] ){
88211 n = findNextHostParameter(zRawSql, &nToken);
88212 assert( n>0 )((void) (0));
88213 sqlite3_str_append(&out, zRawSql, n);
88214 zRawSql += n;
88215 assert( zRawSql[0] || nToken==0 )((void) (0));
88216 if( nToken==0 ) break;
88217 if( zRawSql[0]=='?' ){
88218 if( nToken>1 ){
88219 assert( sqlite3Isdigit(zRawSql[1]) )((void) (0));
88220 sqlite3GetInt32(&zRawSql[1], &idx);
88221 }else{
88222 idx = nextIndex;
88223 }
88224 }else{
88225 assert( zRawSql[0]==':' || zRawSql[0]=='$' ||((void) (0))
88226 zRawSql[0]=='@' || zRawSql[0]=='#' )((void) (0));
88227 testcase( zRawSql[0]==':' );
88228 testcase( zRawSql[0]=='$' );
88229 testcase( zRawSql[0]=='@' );
88230 testcase( zRawSql[0]=='#' );
88231 idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
88232 assert( idx>0 )((void) (0));
88233 }
88234 zRawSql += nToken;
88235 nextIndex = MAX(idx + 1, nextIndex)((idx + 1)>(nextIndex)?(idx + 1):(nextIndex));
88236 assert( idx>0 && idx<=p->nVar )((void) (0));
88237 pVar = &p->aVar[idx-1];
88238 if( pVar->flags & MEM_Null0x0001 ){
88239 sqlite3_str_append(&out, "NULL", 4);
88240 }else if( pVar->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
88241 sqlite3_str_appendf(&out, "%lld", pVar->u.i);
88242 }else if( pVar->flags & MEM_Real0x0008 ){
88243 sqlite3_str_appendf(&out, "%!.15g", pVar->u.r);
88244 }else if( pVar->flags & MEM_Str0x0002 ){
88245 int nOut; /* Number of bytes of the string text to include in output */
88246#ifndef SQLITE_OMIT_UTF16
88247 u8 enc = ENC(db)((db)->enc);
88248 if( enc!=SQLITE_UTF81 ){
88249 memset(&utf8, 0, sizeof(utf8));
88250 utf8.db = db;
88251 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC((sqlite3_destructor_type)0));
88252 if( SQLITE_NOMEM7==sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF81) ){
88253 out.accError = SQLITE_NOMEM7;
88254 out.nAlloc = 0;
88255 }
88256 pVar = &utf8;
88257 }
88258#endif
88259 nOut = pVar->n;
88260#ifdef SQLITE_TRACE_SIZE_LIMIT
88261 if( nOut>SQLITE_TRACE_SIZE_LIMIT ){
88262 nOut = SQLITE_TRACE_SIZE_LIMIT;
88263 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
88264 }
88265#endif
88266 sqlite3_str_appendf(&out, "'%.*q'", nOut, pVar->z);
88267#ifdef SQLITE_TRACE_SIZE_LIMIT
88268 if( nOut<pVar->n ){
88269 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
88270 }
88271#endif
88272#ifndef SQLITE_OMIT_UTF16
88273 if( enc!=SQLITE_UTF81 ) sqlite3VdbeMemRelease(&utf8);
88274#endif
88275 }else if( pVar->flags & MEM_Zero0x0400 ){
88276 sqlite3_str_appendf(&out, "zeroblob(%d)", pVar->u.nZero);
88277 }else{
88278 int nOut; /* Number of bytes of the blob to include in output */
88279 assert( pVar->flags & MEM_Blob )((void) (0));
88280 sqlite3_str_append(&out, "x'", 2);
88281 nOut = pVar->n;
88282#ifdef SQLITE_TRACE_SIZE_LIMIT
88283 if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
88284#endif
88285 for(i=0; i<nOut; i++){
88286 sqlite3_str_appendf(&out, "%02x", pVar->z[i]&0xff);
88287 }
88288 sqlite3_str_append(&out, "'", 1);
88289#ifdef SQLITE_TRACE_SIZE_LIMIT
88290 if( nOut<pVar->n ){
88291 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
88292 }
88293#endif
88294 }
88295 }
88296 }
88297 if( out.accError ) sqlite3_str_reset(&out);
88298 return sqlite3StrAccumFinish(&out);
88299}
88300
88301#endif /* #ifndef SQLITE_OMIT_TRACE */
88302
88303/************** End of vdbetrace.c *******************************************/
88304/************** Begin file vdbe.c ********************************************/
88305/*
88306** 2001 September 15
88307**
88308** The author disclaims copyright to this source code. In place of
88309** a legal notice, here is a blessing:
88310**
88311** May you do good and not evil.
88312** May you find forgiveness for yourself and forgive others.
88313** May you share freely, never taking more than you give.
88314**
88315*************************************************************************
88316** The code in this file implements the function that runs the
88317** bytecode of a prepared statement.
88318**
88319** Various scripts scan this source file in order to generate HTML
88320** documentation, headers files, or other derived files. The formatting
88321** of the code in this file is, therefore, important. See other comments
88322** in this file for details. If in doubt, do not deviate from existing
88323** commenting and indentation practices when changing or adding code.
88324*/
88325/* #include "sqliteInt.h" */
88326/* #include "vdbeInt.h" */
88327
88328/*
88329** Invoke this macro on memory cells just prior to changing the
88330** value of the cell. This macro verifies that shallow copies are
88331** not misused. A shallow copy of a string or blob just copies a
88332** pointer to the string or blob, not the content. If the original
88333** is changed while the copy is still in use, the string or blob might
88334** be changed out from under the copy. This macro verifies that nothing
88335** like that ever happens.
88336*/
88337#ifdef SQLITE_DEBUG
88338# define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
88339#else
88340# define memAboutToChange(P,M)
88341#endif
88342
88343/*
88344** The following global variable is incremented every time a cursor
88345** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
88346** procedures use this information to make sure that indices are
88347** working correctly. This variable has no function other than to
88348** help verify the correct operation of the library.
88349*/
88350#ifdef SQLITE_TEST
88351SQLITE_API int sqlite3_search_count = 0;
88352#endif
88353
88354/*
88355** When this global variable is positive, it gets decremented once before
88356** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted
88357** field of the sqlite3 structure is set in order to simulate an interrupt.
88358**
88359** This facility is used for testing purposes only. It does not function
88360** in an ordinary build.
88361*/
88362#ifdef SQLITE_TEST
88363SQLITE_API int sqlite3_interrupt_count = 0;
88364#endif
88365
88366/*
88367** The next global variable is incremented each type the OP_Sort opcode
88368** is executed. The test procedures use this information to make sure that
88369** sorting is occurring or not occurring at appropriate times. This variable
88370** has no function other than to help verify the correct operation of the
88371** library.
88372*/
88373#ifdef SQLITE_TEST
88374SQLITE_API int sqlite3_sort_count = 0;
88375#endif
88376
88377/*
88378** The next global variable records the size of the largest MEM_Blob
88379** or MEM_Str that has been used by a VDBE opcode. The test procedures
88380** use this information to make sure that the zero-blob functionality
88381** is working correctly. This variable has no function other than to
88382** help verify the correct operation of the library.
88383*/
88384#ifdef SQLITE_TEST
88385SQLITE_API int sqlite3_max_blobsize = 0;
88386static void updateMaxBlobsize(Mem *p){
88387 if( (p->flags & (MEM_Str0x0002|MEM_Blob0x0010))!=0 && p->n>sqlite3_max_blobsize ){
88388 sqlite3_max_blobsize = p->n;
88389 }
88390}
88391#endif
88392
88393/*
88394** This macro evaluates to true if either the update hook or the preupdate
88395** hook are enabled for database connect DB.
88396*/
88397#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
88398# define HAS_UPDATE_HOOK(DB)((DB)->xPreUpdateCallback||(DB)->xUpdateCallback) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
88399#else
88400# define HAS_UPDATE_HOOK(DB)((DB)->xPreUpdateCallback||(DB)->xUpdateCallback) ((DB)->xUpdateCallback)
88401#endif
88402
88403/*
88404** The next global variable is incremented each time the OP_Found opcode
88405** is executed. This is used to test whether or not the foreign key
88406** operation implemented using OP_FkIsZero is working. This variable
88407** has no function other than to help verify the correct operation of the
88408** library.
88409*/
88410#ifdef SQLITE_TEST
88411SQLITE_API int sqlite3_found_count = 0;
88412#endif
88413
88414/*
88415** Test a register to see if it exceeds the current maximum blob size.
88416** If it does, record the new maximum blob size.
88417*/
88418#if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE)
88419# define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
88420#else
88421# define UPDATE_MAX_BLOBSIZE(P)
88422#endif
88423
88424#ifdef SQLITE_DEBUG
88425/* This routine provides a convenient place to set a breakpoint during
88426** tracing with PRAGMA vdbe_trace=on. The breakpoint fires right after
88427** each opcode is printed. Variables "pc" (program counter) and pOp are
88428** available to add conditionals to the breakpoint. GDB example:
88429**
88430** break test_trace_breakpoint if pc=22
88431**
88432** Other useful labels for breakpoints include:
88433** test_addop_breakpoint(pc,pOp)
88434** sqlite3CorruptError(lineno)
88435** sqlite3MisuseError(lineno)
88436** sqlite3CantopenError(lineno)
88437*/
88438static void test_trace_breakpoint(int pc, Op *pOp, Vdbe *v){
88439 static int n = 0;
88440 n++;
88441}
88442#endif
88443
88444/*
88445** Invoke the VDBE coverage callback, if that callback is defined. This
88446** feature is used for test suite validation only and does not appear an
88447** production builds.
88448**
88449** M is the type of branch. I is the direction taken for this instance of
88450** the branch.
88451**
88452** M: 2 - two-way branch (I=0: fall-thru 1: jump )
88453** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL )
88454** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3)
88455**
88456** In other words, if M is 2, then I is either 0 (for fall-through) or
88457** 1 (for when the branch is taken). If M is 3, the I is 0 for an
88458** ordinary fall-through, I is 1 if the branch was taken, and I is 2
88459** if the result of comparison is NULL. For M=3, I=2 the jump may or
88460** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5.
88461** When M is 4, that means that an OP_Jump is being run. I is 0, 1, or 2
88462** depending on if the operands are less than, equal, or greater than.
88463**
88464** iSrcLine is the source code line (from the __LINE__ macro) that
88465** generated the VDBE instruction combined with flag bits. The source
88466** code line number is in the lower 24 bits of iSrcLine and the upper
88467** 8 bytes are flags. The lower three bits of the flags indicate
88468** values for I that should never occur. For example, if the branch is
88469** always taken, the flags should be 0x05 since the fall-through and
88470** alternate branch are never taken. If a branch is never taken then
88471** flags should be 0x06 since only the fall-through approach is allowed.
88472**
88473** Bit 0x08 of the flags indicates an OP_Jump opcode that is only
88474** interested in equal or not-equal. In other words, I==0 and I==2
88475** should be treated as equivalent
88476**
88477** Since only a line number is retained, not the filename, this macro
88478** only works for amalgamation builds. But that is ok, since these macros
88479** should be no-ops except for special builds used to measure test coverage.
88480*/
88481#if !defined(SQLITE_VDBE_COVERAGE)
88482# define VdbeBranchTaken(I,M)
88483#else
88484# define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
88485 static void vdbeTakeBranch(u32 iSrcLine, u8 I, u8 M){
88486 u8 mNever;
88487 assert( I<=2 )((void) (0)); /* 0: fall through, 1: taken, 2: alternate taken */
88488 assert( M<=4 )((void) (0)); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
88489 assert( I<M )((void) (0)); /* I can only be 2 if M is 3 or 4 */
88490 /* Transform I from a integer [0,1,2] into a bitmask of [1,2,4] */
88491 I = 1<<I;
88492 /* The upper 8 bits of iSrcLine are flags. The lower three bits of
88493 ** the flags indicate directions that the branch can never go. If
88494 ** a branch really does go in one of those directions, assert right
88495 ** away. */
88496 mNever = iSrcLine >> 24;
88497 assert( (I & mNever)==0 )((void) (0));
88498 if( sqlite3GlobalConfigsqlite3Config.xVdbeBranch==0 ) return; /*NO_TEST*/
88499 /* Invoke the branch coverage callback with three arguments:
88500 ** iSrcLine - the line number of the VdbeCoverage() macro, with
88501 ** flags removed.
88502 ** I - Mask of bits 0x07 indicating which cases are are
88503 ** fulfilled by this instance of the jump. 0x01 means
88504 ** fall-thru, 0x02 means taken, 0x04 means NULL. Any
88505 ** impossible cases (ex: if the comparison is never NULL)
88506 ** are filled in automatically so that the coverage
88507 ** measurement logic does not flag those impossible cases
88508 ** as missed coverage.
88509 ** M - Type of jump. Same as M argument above
88510 */
88511 I |= mNever;
88512 if( M==2 ) I |= 0x04;
88513 if( M==4 ){
88514 I |= 0x08;
88515 if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/
88516 }
88517 sqlite3GlobalConfigsqlite3Config.xVdbeBranch(sqlite3GlobalConfigsqlite3Config.pVdbeBranchArg,
88518 iSrcLine&0xffffff, I, M);
88519 }
88520#endif
88521
88522/*
88523** An ephemeral string value (signified by the MEM_Ephem flag) contains
88524** a pointer to a dynamically allocated string where some other entity
88525** is responsible for deallocating that string. Because the register
88526** does not control the string, it might be deleted without the register
88527** knowing it.
88528**
88529** This routine converts an ephemeral string into a dynamically allocated
88530** string that the register itself controls. In other words, it
88531** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
88532*/
88533#define Deephemeralize(P)if( ((P)->flags&0x4000)!=0 && sqlite3VdbeMemMakeWriteable
(P) ){ goto no_mem;}
\
88534 if( ((P)->flags&MEM_Ephem0x4000)!=0 \
88535 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
88536
88537/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
88538#define isSorter(x)((x)->eCurType==1) ((x)->eCurType==CURTYPE_SORTER1)
88539
88540/*
88541** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
88542** if we run out of memory.
88543*/
88544static VdbeCursor *allocateCursor(
88545 Vdbe *p, /* The virtual machine */
88546 int iCur, /* Index of the new VdbeCursor */
88547 int nField, /* Number of fields in the table or index */
88548 u8 eCurType /* Type of the new cursor */
88549){
88550 /* Find the memory cell that will be used to store the blob of memory
88551 ** required for this VdbeCursor structure. It is convenient to use a
88552 ** vdbe memory cell to manage the memory allocation required for a
88553 ** VdbeCursor structure for the following reasons:
88554 **
88555 ** * Sometimes cursor numbers are used for a couple of different
88556 ** purposes in a vdbe program. The different uses might require
88557 ** different sized allocations. Memory cells provide growable
88558 ** allocations.
88559 **
88560 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
88561 ** be freed lazily via the sqlite3_release_memory() API. This
88562 ** minimizes the number of malloc calls made by the system.
88563 **
88564 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from
88565 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
88566 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
88567 */
88568 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
88569
88570 int nByte;
88571 VdbeCursor *pCx = 0;
88572 nByte =
88573 ROUND8P(sizeof(VdbeCursor))(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
88574 (eCurType==CURTYPE_BTREE0?sqlite3BtreeCursorSize():0);
88575
88576 assert( iCur>=0 && iCur<p->nCursor )((void) (0));
88577 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
88578 sqlite3VdbeFreeCursorNN(p, p->apCsr[iCur]);
88579 p->apCsr[iCur] = 0;
88580 }
88581
88582 /* There used to be a call to sqlite3VdbeMemClearAndResize() to make sure
88583 ** the pMem used to hold space for the cursor has enough storage available
88584 ** in pMem->zMalloc. But for the special case of the aMem[] entries used
88585 ** to hold cursors, it is faster to in-line the logic. */
88586 assert( pMem->flags==MEM_Undefined )((void) (0));
88587 assert( (pMem->flags & MEM_Dyn)==0 )((void) (0));
88588 assert( pMem->szMalloc==0 || pMem->z==pMem->zMalloc )((void) (0));
88589 if( pMem->szMalloc<nByte ){
88590 if( pMem->szMalloc>0 ){
88591 sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
88592 }
88593 pMem->z = pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, nByte);
88594 if( pMem->zMalloc==0 ){
88595 pMem->szMalloc = 0;
88596 return 0;
88597 }
88598 pMem->szMalloc = nByte;
88599 }
88600
88601 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->zMalloc;
88602 memset(pCx, 0, offsetof(VdbeCursor,pAltCursor)__builtin_offsetof(VdbeCursor, pAltCursor));
88603 pCx->eCurType = eCurType;
88604 pCx->nField = nField;
88605 pCx->aOffset = &pCx->aType[nField];
88606 if( eCurType==CURTYPE_BTREE0 ){
88607 pCx->uc.pCursor = (BtCursor*)
88608 &pMem->z[ROUND8P(sizeof(VdbeCursor))(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
88609 sqlite3BtreeCursorZero(pCx->uc.pCursor);
88610 }
88611 return pCx;
88612}
88613
88614/*
88615** The string in pRec is known to look like an integer and to have a
88616** floating point value of rValue. Return true and set *piValue to the
88617** integer value if the string is in range to be an integer. Otherwise,
88618** return false.
88619*/
88620static int alsoAnInt(Mem *pRec, double rValue, i64 *piValue){
88621 i64 iValue = (double)rValue;
88622 if( sqlite3RealSameAsInt(rValue,iValue) ){
88623 *piValue = iValue;
88624 return 1;
88625 }
88626 return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc);
88627}
88628
88629/*
88630** Try to convert a value into a numeric representation if we can
88631** do so without loss of information. In other words, if the string
88632** looks like a number, convert it into a number. If it does not
88633** look like a number, leave it alone.
88634**
88635** If the bTryForInt flag is true, then extra effort is made to give
88636** an integer representation. Strings that look like floating point
88637** values but which have no fractional component (example: '48.00')
88638** will have a MEM_Int representation when bTryForInt is true.
88639**
88640** If bTryForInt is false, then if the input string contains a decimal
88641** point or exponential notation, the result is only MEM_Real, even
88642** if there is an exact integer representation of the quantity.
88643*/
88644static void applyNumericAffinity(Mem *pRec, int bTryForInt){
88645 double rValue;
88646 u8 enc = pRec->enc;
88647 int rc;
88648 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str )((void) (0));
88649 rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc);
88650 if( rc<=0 ) return;
88651 if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){
88652 pRec->flags |= MEM_Int0x0004;
88653 }else{
88654 pRec->u.r = rValue;
88655 pRec->flags |= MEM_Real0x0008;
88656 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
88657 }
88658 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
88659 ** string representation after computing a numeric equivalent, because the
88660 ** string representation might not be the canonical representation for the
88661 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
88662 pRec->flags &= ~MEM_Str0x0002;
88663}
88664
88665/*
88666** Processing is determine by the affinity parameter:
88667**
88668** SQLITE_AFF_INTEGER:
88669** SQLITE_AFF_REAL:
88670** SQLITE_AFF_NUMERIC:
88671** Try to convert pRec to an integer representation or a
88672** floating-point representation if an integer representation
88673** is not possible. Note that the integer representation is
88674** always preferred, even if the affinity is REAL, because
88675** an integer representation is more space efficient on disk.
88676**
88677** SQLITE_AFF_TEXT:
88678** Convert pRec to a text representation.
88679**
88680** SQLITE_AFF_BLOB:
88681** SQLITE_AFF_NONE:
88682** No-op. pRec is unchanged.
88683*/
88684static void applyAffinity(
88685 Mem *pRec, /* The value to apply affinity to */
88686 char affinity, /* The affinity to be applied */
88687 u8 enc /* Use this text encoding */
88688){
88689 if( affinity>=SQLITE_AFF_NUMERIC0x43 ){
88690 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL((void) (0))
88691 || affinity==SQLITE_AFF_NUMERIC )((void) (0));
88692 if( (pRec->flags & MEM_Int0x0004)==0 ){ /*OPTIMIZATION-IF-FALSE*/
88693 if( (pRec->flags & MEM_Real0x0008)==0 ){
88694 if( pRec->flags & MEM_Str0x0002 ) applyNumericAffinity(pRec,1);
88695 }else{
88696 sqlite3VdbeIntegerAffinity(pRec);
88697 }
88698 }
88699 }else if( affinity==SQLITE_AFF_TEXT0x42 ){
88700 /* Only attempt the conversion to TEXT if there is an integer or real
88701 ** representation (blob and NULL do not get converted) but no string
88702 ** representation. It would be harmless to repeat the conversion if
88703 ** there is already a string rep, but it is pointless to waste those
88704 ** CPU cycles. */
88705 if( 0==(pRec->flags&MEM_Str0x0002) ){ /*OPTIMIZATION-IF-FALSE*/
88706 if( (pRec->flags&(MEM_Real0x0008|MEM_Int0x0004|MEM_IntReal0x0020)) ){
88707 testcase( pRec->flags & MEM_Int );
88708 testcase( pRec->flags & MEM_Real );
88709 testcase( pRec->flags & MEM_IntReal );
88710 sqlite3VdbeMemStringify(pRec, enc, 1);
88711 }
88712 }
88713 pRec->flags &= ~(MEM_Real0x0008|MEM_Int0x0004|MEM_IntReal0x0020);
88714 }
88715}
88716
88717/*
88718** Try to convert the type of a function argument or a result column
88719** into a numeric representation. Use either INTEGER or REAL whichever
88720** is appropriate. But only do the conversion if it is possible without
88721** loss of information and return the revised type of the argument.
88722*/
88723SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){
88724 int eType = sqlite3_value_type(pVal);
88725 if( eType==SQLITE_TEXT3 ){
88726 Mem *pMem = (Mem*)pVal;
88727 applyNumericAffinity(pMem, 0);
88728 eType = sqlite3_value_type(pVal);
88729 }
88730 return eType;
88731}
88732
88733/*
88734** Exported version of applyAffinity(). This one works on sqlite3_value*,
88735** not the internal Mem* type.
88736*/
88737SQLITE_PRIVATEstatic void sqlite3ValueApplyAffinity(
88738 sqlite3_value *pVal,
88739 u8 affinity,
88740 u8 enc
88741){
88742 applyAffinity((Mem *)pVal, affinity, enc);
88743}
88744
88745/*
88746** pMem currently only holds a string type (or maybe a BLOB that we can
88747** interpret as a string if we want to). Compute its corresponding
88748** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
88749** accordingly.
88750*/
88751static u16 SQLITE_NOINLINE__attribute__((noinline)) computeNumericType(Mem *pMem){
88752 int rc;
88753 sqlite3_int64 ix;
88754 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 )((void) (0));
88755 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 )((void) (0));
88756 if( ExpandBlob(pMem)(((pMem)->flags&0x0400)?sqlite3VdbeMemExpandBlob(pMem)
:0)
){
88757 pMem->u.i = 0;
88758 return MEM_Int0x0004;
88759 }
88760 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
88761 if( rc<=0 ){
88762 if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){
88763 pMem->u.i = ix;
88764 return MEM_Int0x0004;
88765 }else{
88766 return MEM_Real0x0008;
88767 }
88768 }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){
88769 pMem->u.i = ix;
88770 return MEM_Int0x0004;
88771 }
88772 return MEM_Real0x0008;
88773}
88774
88775/*
88776** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
88777** none.
88778**
88779** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
88780** But it does set pMem->u.r and pMem->u.i appropriately.
88781*/
88782static u16 numericType(Mem *pMem){
88783 if( pMem->flags & (MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020) ){
88784 testcase( pMem->flags & MEM_Int );
88785 testcase( pMem->flags & MEM_Real );
88786 testcase( pMem->flags & MEM_IntReal );
88787 return pMem->flags & (MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020);
88788 }
88789 if( pMem->flags & (MEM_Str0x0002|MEM_Blob0x0010) ){
88790 testcase( pMem->flags & MEM_Str );
88791 testcase( pMem->flags & MEM_Blob );
88792 return computeNumericType(pMem);
88793 }
88794 return 0;
88795}
88796
88797#ifdef SQLITE_DEBUG
88798/*
88799** Write a nice string representation of the contents of cell pMem
88800** into buffer zBuf, length nBuf.
88801*/
88802SQLITE_PRIVATEstatic void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr){
88803 int f = pMem->flags;
88804 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
88805 if( f&MEM_Blob0x0010 ){
88806 int i;
88807 char c;
88808 if( f & MEM_Dyn0x1000 ){
88809 c = 'z';
88810 assert( (f & (MEM_Static|MEM_Ephem))==0 )((void) (0));
88811 }else if( f & MEM_Static0x2000 ){
88812 c = 't';
88813 assert( (f & (MEM_Dyn|MEM_Ephem))==0 )((void) (0));
88814 }else if( f & MEM_Ephem0x4000 ){
88815 c = 'e';
88816 assert( (f & (MEM_Static|MEM_Dyn))==0 )((void) (0));
88817 }else{
88818 c = 's';
88819 }
88820 sqlite3_str_appendf(pStr, "%cx[", c);
88821 for(i=0; i<25 && i<pMem->n; i++){
88822 sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF));
88823 }
88824 sqlite3_str_appendf(pStr, "|");
88825 for(i=0; i<25 && i<pMem->n; i++){
88826 char z = pMem->z[i];
88827 sqlite3_str_appendchar(pStr, 1, (z<32||z>126)?'.':z);
88828 }
88829 sqlite3_str_appendf(pStr,"]");
88830 if( f & MEM_Zero0x0400 ){
88831 sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero);
88832 }
88833 }else if( f & MEM_Str0x0002 ){
88834 int j;
88835 u8 c;
88836 if( f & MEM_Dyn0x1000 ){
88837 c = 'z';
88838 assert( (f & (MEM_Static|MEM_Ephem))==0 )((void) (0));
88839 }else if( f & MEM_Static0x2000 ){
88840 c = 't';
88841 assert( (f & (MEM_Dyn|MEM_Ephem))==0 )((void) (0));
88842 }else if( f & MEM_Ephem0x4000 ){
88843 c = 'e';
88844 assert( (f & (MEM_Static|MEM_Dyn))==0 )((void) (0));
88845 }else{
88846 c = 's';
88847 }
88848 sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n);
88849 for(j=0; j<25 && j<pMem->n; j++){
88850 c = pMem->z[j];
88851 sqlite3_str_appendchar(pStr, 1, (c>=0x20&&c<=0x7f) ? c : '.');
88852 }
88853 sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]);
88854 }
88855}
88856#endif
88857
88858#ifdef SQLITE_DEBUG
88859/*
88860** Print the value of a register for tracing purposes:
88861*/
88862static void memTracePrint(Mem *p){
88863 if( p->flags & MEM_Undefined0x0000 ){
88864 printf(" undefined");
88865 }else if( p->flags & MEM_Null0x0001 ){
88866 printf(p->flags & MEM_Zero0x0400 ? " NULL-nochng" : " NULL");
88867 }else if( (p->flags & (MEM_Int0x0004|MEM_Str0x0002))==(MEM_Int0x0004|MEM_Str0x0002) ){
88868 printf(" si:%lld", p->u.i);
88869 }else if( (p->flags & (MEM_IntReal0x0020))!=0 ){
88870 printf(" ir:%lld", p->u.i);
88871 }else if( p->flags & MEM_Int0x0004 ){
88872 printf(" i:%lld", p->u.i);
88873#ifndef SQLITE_OMIT_FLOATING_POINT
88874 }else if( p->flags & MEM_Real0x0008 ){
88875 printf(" r:%.17g", p->u.r);
88876#endif
88877 }else if( sqlite3VdbeMemIsRowSet(p) ){
88878 printf(" (rowset)");
88879 }else{
88880 StrAccum acc;
88881 char zBuf[1000];
88882 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
88883 sqlite3VdbeMemPrettyPrint(p, &acc);
88884 printf(" %s", sqlite3StrAccumFinish(&acc));
88885 }
88886 if( p->flags & MEM_Subtype0x0800 ) printf(" subtype=0x%02x", p->eSubtype);
88887}
88888static void registerTrace(int iReg, Mem *p){
88889 printf("R[%d] = ", iReg);
88890 memTracePrint(p);
88891 if( p->pScopyFrom ){
88892 printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg]));
88893 }
88894 printf("\n");
88895 sqlite3VdbeCheckMemInvariants(p);
88896}
88897/**/ void sqlite3PrintMem(Mem *pMem){
88898 memTracePrint(pMem);
88899 printf("\n");
88900 fflush(stdoutstdout);
88901}
88902#endif
88903
88904#ifdef SQLITE_DEBUG
88905/*
88906** Show the values of all registers in the virtual machine. Used for
88907** interactive debugging.
88908*/
88909SQLITE_PRIVATEstatic void sqlite3VdbeRegisterDump(Vdbe *v){
88910 int i;
88911 for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i);
88912}
88913#endif /* SQLITE_DEBUG */
88914
88915
88916#ifdef SQLITE_DEBUG
88917# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
88918#else
88919# define REGISTER_TRACE(R,M)
88920#endif
88921
88922
88923#ifdef VDBE_PROFILE
88924
88925/*
88926** hwtime.h contains inline assembler code for implementing
88927** high-performance timing routines.
88928*/
88929/* #include "hwtime.h" */
88930
88931#endif
88932
88933#ifndef NDEBUG1
88934/*
88935** This function is only called from within an assert() expression. It
88936** checks that the sqlite3.nTransaction variable is correctly set to
88937** the number of non-transaction savepoints currently in the
88938** linked list starting at sqlite3.pSavepoint.
88939**
88940** Usage:
88941**
88942** assert( checkSavepointCount(db) );
88943*/
88944static int checkSavepointCount(sqlite3 *db){
88945 int n = 0;
88946 Savepoint *p;
88947 for(p=db->pSavepoint; p; p=p->pNext) n++;
88948 assert( n==(db->nSavepoint + db->isTransactionSavepoint) )((void) (0));
88949 return 1;
88950}
88951#endif
88952
88953/*
88954** Return the register of pOp->p2 after first preparing it to be
88955** overwritten with an integer value.
88956*/
88957static SQLITE_NOINLINE__attribute__((noinline)) Mem *out2PrereleaseWithClear(Mem *pOut){
88958 sqlite3VdbeMemSetNull(pOut);
88959 pOut->flags = MEM_Int0x0004;
88960 return pOut;
88961}
88962static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
88963 Mem *pOut;
88964 assert( pOp->p2>0 )((void) (0));
88965 assert( pOp->p2<=(p->nMem+1 - p->nCursor) )((void) (0));
88966 pOut = &p->aMem[pOp->p2];
88967 memAboutToChange(p, pOut);
88968 if( VdbeMemDynamic(pOut)(((pOut)->flags&(0x8000|0x1000))!=0) ){ /*OPTIMIZATION-IF-FALSE*/
88969 return out2PrereleaseWithClear(pOut);
88970 }else{
88971 pOut->flags = MEM_Int0x0004;
88972 return pOut;
88973 }
88974}
88975
88976/*
88977** Compute a bloom filter hash using pOp->p4.i registers from aMem[] beginning
88978** with pOp->p3. Return the hash.
88979*/
88980static u64 filterHash(const Mem *aMem, const Op *pOp){
88981 int i, mx;
88982 u64 h = 0;
88983
88984 assert( pOp->p4type==P4_INT32 )((void) (0));
88985 for(i=pOp->p3, mx=i+pOp->p4.i; i<mx; i++){
88986 const Mem *p = &aMem[i];
88987 if( p->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
88988 h += p->u.i;
88989 }else if( p->flags & MEM_Real0x0008 ){
88990 h += sqlite3VdbeIntValue(p);
88991 }else if( p->flags & (MEM_Str0x0002|MEM_Blob0x0010) ){
88992 h += p->n;
88993 if( p->flags & MEM_Zero0x0400 ) h += p->u.nZero;
88994 }
88995 }
88996 return h;
88997}
88998
88999/*
89000** Return the symbolic name for the data type of a pMem
89001*/
89002static const char *vdbeMemTypeName(Mem *pMem){
89003 static const char *azTypes[] = {
89004 /* SQLITE_INTEGER */ "INT",
89005 /* SQLITE_FLOAT */ "REAL",
89006 /* SQLITE_TEXT */ "TEXT",
89007 /* SQLITE_BLOB */ "BLOB",
89008 /* SQLITE_NULL */ "NULL"
89009 };
89010 return azTypes[sqlite3_value_type(pMem)-1];
89011}
89012
89013/*
89014** Execute as much of a VDBE program as we can.
89015** This is the core of sqlite3_step().
89016*/
89017SQLITE_PRIVATEstatic int sqlite3VdbeExec(
89018 Vdbe *p /* The VDBE */
89019){
89020 Op *aOp = p->aOp; /* Copy of p->aOp */
89021 Op *pOp = aOp; /* Current operation */
89022#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
89023 Op *pOrigOp; /* Value of pOp at the top of the loop */
89024#endif
89025#ifdef SQLITE_DEBUG
89026 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
89027#endif
89028 int rc = SQLITE_OK0; /* Value to return */
89029 sqlite3 *db = p->db; /* The database */
89030 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
89031 u8 encoding = ENC(db)((db)->enc); /* The database encoding */
89032 int iCompare = 0; /* Result of last comparison */
89033 u64 nVmStep = 0; /* Number of virtual machine steps */
89034#ifndef SQLITE_OMIT_PROGRESS_CALLBACK1
89035 u64 nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */
89036#endif
89037 Mem *aMem = p->aMem; /* Copy of p->aMem */
89038 Mem *pIn1 = 0; /* 1st input operand */
89039 Mem *pIn2 = 0; /* 2nd input operand */
89040 Mem *pIn3 = 0; /* 3rd input operand */
89041 Mem *pOut = 0; /* Output operand */
89042#ifdef VDBE_PROFILE
89043 u64 start; /* CPU clock count at start of opcode */
89044#endif
89045 /*** INSERT STACK UNION HERE ***/
89046
89047 assert( p->eVdbeState==VDBE_RUN_STATE )((void) (0)); /* sqlite3_step() verifies this */
89048 sqlite3VdbeEnter(p);
89049#ifndef SQLITE_OMIT_PROGRESS_CALLBACK1
89050 if( db->xProgress ){
89051 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP4];
89052 assert( 0 < db->nProgressOps )((void) (0));
89053 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
89054 }else{
89055 nProgressLimit = LARGEST_UINT64(0xffffffff|(((u64)0xffffffff)<<32));
89056 }
89057#endif
89058 if( p->rc==SQLITE_NOMEM7 ){
89059 /* This happens if a malloc() inside a call to sqlite3_column_text() or
89060 ** sqlite3_column_text16() failed. */
89061 goto no_mem;
89062 }
89063 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY )((void) (0));
89064 testcase( p->rc!=SQLITE_OK );
89065 p->rc = SQLITE_OK0;
89066 assert( p->bIsReader || p->readOnly!=0 )((void) (0));
89067 p->iCurrentTime = 0;
89068 assert( p->explain==0 )((void) (0));
89069 p->pResultSet = 0;
89070 db->busyHandler.nBusy = 0;
89071 if( AtomicLoad(&db->u1.isInterrupted)__atomic_load_n((&db->u1.isInterrupted),0) ) goto abort_due_to_interrupt;
89072 sqlite3VdbeIOTraceSql(p);
89073#ifdef SQLITE_DEBUG
89074 sqlite3BeginBenignMalloc();
89075 if( p->pc==0
89076 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
89077 ){
89078 int i;
89079 int once = 1;
89080 sqlite3VdbePrintSql(p);
89081 if( p->db->flags & SQLITE_VdbeListing ){
89082 printf("VDBE Program Listing:\n");
89083 for(i=0; i<p->nOp; i++){
89084 sqlite3VdbePrintOp(stdoutstdout, i, &aOp[i]);
89085 }
89086 }
89087 if( p->db->flags & SQLITE_VdbeEQP ){
89088 for(i=0; i<p->nOp; i++){
89089 if( aOp[i].opcode==OP_Explain185 ){
89090 if( once ) printf("VDBE Query Plan:\n");
89091 printf("%s\n", aOp[i].p4.z);
89092 once = 0;
89093 }
89094 }
89095 }
89096 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
89097 }
89098 sqlite3EndBenignMalloc();
89099#endif
89100 for(pOp=&aOp[p->pc]; 1; pOp++){
89101 /* Errors are detected by individual opcodes, with an immediate
89102 ** jumps to abort_due_to_error. */
89103 assert( rc==SQLITE_OK )((void) (0));
89104
89105 assert( pOp>=aOp && pOp<&aOp[p->nOp])((void) (0));
89106#ifdef VDBE_PROFILE
89107 start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
89108#endif
89109 nVmStep++;
89110#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
89111 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
89112#endif
89113
89114 /* Only allow tracing if SQLITE_DEBUG is defined.
89115 */
89116#ifdef SQLITE_DEBUG
89117 if( db->flags & SQLITE_VdbeTrace ){
89118 sqlite3VdbePrintOp(stdoutstdout, (int)(pOp - aOp), pOp);
89119 test_trace_breakpoint((int)(pOp - aOp),pOp,p);
89120 }
89121#endif
89122
89123
89124 /* Check to see if we need to simulate an interrupt. This only happens
89125 ** if we have a special test build.
89126 */
89127#ifdef SQLITE_TEST
89128 if( sqlite3_interrupt_count>0 ){
89129 sqlite3_interrupt_count--;
89130 if( sqlite3_interrupt_count==0 ){
89131 sqlite3_interrupt(db);
89132 }
89133 }
89134#endif
89135
89136 /* Sanity checking on other operands */
89137#ifdef SQLITE_DEBUG
89138 {
89139 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
89140 if( (opProperty & OPFLG_IN10x02)!=0 ){
89141 assert( pOp->p1>0 )((void) (0));
89142 assert( pOp->p1<=(p->nMem+1 - p->nCursor) )((void) (0));
89143 assert( memIsValid(&aMem[pOp->p1]) )((void) (0));
89144 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) )((void) (0));
89145 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
89146 }
89147 if( (opProperty & OPFLG_IN20x04)!=0 ){
89148 assert( pOp->p2>0 )((void) (0));
89149 assert( pOp->p2<=(p->nMem+1 - p->nCursor) )((void) (0));
89150 assert( memIsValid(&aMem[pOp->p2]) )((void) (0));
89151 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) )((void) (0));
89152 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
89153 }
89154 if( (opProperty & OPFLG_IN30x08)!=0 ){
89155 assert( pOp->p3>0 )((void) (0));
89156 assert( pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
89157 assert( memIsValid(&aMem[pOp->p3]) )((void) (0));
89158 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) )((void) (0));
89159 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
89160 }
89161 if( (opProperty & OPFLG_OUT20x10)!=0 ){
89162 assert( pOp->p2>0 )((void) (0));
89163 assert( pOp->p2<=(p->nMem+1 - p->nCursor) )((void) (0));
89164 memAboutToChange(p, &aMem[pOp->p2]);
89165 }
89166 if( (opProperty & OPFLG_OUT30x20)!=0 ){
89167 assert( pOp->p3>0 )((void) (0));
89168 assert( pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
89169 memAboutToChange(p, &aMem[pOp->p3]);
89170 }
89171 }
89172#endif
89173#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
89174 pOrigOp = pOp;
89175#endif
89176
89177 switch( pOp->opcode ){
89178
89179/*****************************************************************************
89180** What follows is a massive switch statement where each case implements a
89181** separate instruction in the virtual machine. If we follow the usual
89182** indentation conventions, each case should be indented by 6 spaces. But
89183** that is a lot of wasted space on the left margin. So the code within
89184** the switch statement will break with convention and be flush-left. Another
89185** big comment (similar to this one) will mark the point in the code where
89186** we transition back to normal indentation.
89187**
89188** The formatting of each case is important. The makefile for SQLite
89189** generates two C files "opcodes.h" and "opcodes.c" by scanning this
89190** file looking for lines that begin with "case OP_". The opcodes.h files
89191** will be filled with #defines that give unique integer values to each
89192** opcode and the opcodes.c file is filled with an array of strings where
89193** each string is the symbolic name for the corresponding opcode. If the
89194** case statement is followed by a comment of the form "/# same as ... #/"
89195** that comment is used to determine the particular value of the opcode.
89196**
89197** Other keywords in the comment that follows each case are used to
89198** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
89199** Keywords include: in1, in2, in3, out2, out3. See
89200** the mkopcodeh.awk script for additional information.
89201**
89202** Documentation about VDBE opcodes is generated by scanning this file
89203** for lines of that contain "Opcode:". That line and all subsequent
89204** comment lines are used in the generation of the opcode.html documentation
89205** file.
89206**
89207** SUMMARY:
89208**
89209** Formatting is important to scripts that scan this file.
89210** Do not deviate from the formatting style currently in use.
89211**
89212*****************************************************************************/
89213
89214/* Opcode: Goto * P2 * * *
89215**
89216** An unconditional jump to address P2.
89217** The next instruction executed will be
89218** the one at index P2 from the beginning of
89219** the program.
89220**
89221** The P1 parameter is not actually used by this opcode. However, it
89222** is sometimes set to 1 instead of 0 as a hint to the command-line shell
89223** that this Goto is the bottom of a loop and that the lines from P2 down
89224** to the current line should be indented for EXPLAIN output.
89225*/
89226case OP_Goto8: { /* jump */
89227
89228#ifdef SQLITE_DEBUG
89229 /* In debuggging mode, when the p5 flags is set on an OP_Goto, that
89230 ** means we should really jump back to the preceeding OP_ReleaseReg
89231 ** instruction. */
89232 if( pOp->p5 ){
89233 assert( pOp->p2 < (int)(pOp - aOp) )((void) (0));
89234 assert( pOp->p2 > 1 )((void) (0));
89235 pOp = &aOp[pOp->p2 - 2];
89236 assert( pOp[1].opcode==OP_ReleaseReg )((void) (0));
89237 goto check_for_interrupt;
89238 }
89239#endif
89240
89241jump_to_p2_and_check_for_interrupt:
89242 pOp = &aOp[pOp->p2 - 1];
89243
89244 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
89245 ** OP_VNext, or OP_SorterNext) all jump here upon
89246 ** completion. Check to see if sqlite3_interrupt() has been called
89247 ** or if the progress callback needs to be invoked.
89248 **
89249 ** This code uses unstructured "goto" statements and does not look clean.
89250 ** But that is not due to sloppy coding habits. The code is written this
89251 ** way for performance, to avoid having to run the interrupt and progress
89252 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
89253 ** faster according to "valgrind --tool=cachegrind" */
89254check_for_interrupt:
89255 if( AtomicLoad(&db->u1.isInterrupted)__atomic_load_n((&db->u1.isInterrupted),0) ) goto abort_due_to_interrupt;
89256#ifndef SQLITE_OMIT_PROGRESS_CALLBACK1
89257 /* Call the progress callback if it is configured and the required number
89258 ** of VDBE ops have been executed (either since this invocation of
89259 ** sqlite3VdbeExec() or since last time the progress callback was called).
89260 ** If the progress callback returns non-zero, exit the virtual machine with
89261 ** a return code SQLITE_ABORT.
89262 */
89263 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
89264 assert( db->nProgressOps!=0 )((void) (0));
89265 nProgressLimit += db->nProgressOps;
89266 if( db->xProgress(db->pProgressArg) ){
89267 nProgressLimit = LARGEST_UINT64(0xffffffff|(((u64)0xffffffff)<<32));
89268 rc = SQLITE_INTERRUPT9;
89269 goto abort_due_to_error;
89270 }
89271 }
89272#endif
89273
89274 break;
89275}
89276
89277/* Opcode: Gosub P1 P2 * * *
89278**
89279** Write the current address onto register P1
89280** and then jump to address P2.
89281*/
89282case OP_Gosub9: { /* jump */
89283 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) )((void) (0));
89284 pIn1 = &aMem[pOp->p1];
89285 assert( VdbeMemDynamic(pIn1)==0 )((void) (0));
89286 memAboutToChange(p, pIn1);
89287 pIn1->flags = MEM_Int0x0004;
89288 pIn1->u.i = (int)(pOp-aOp);
89289 REGISTER_TRACE(pOp->p1, pIn1);
89290 goto jump_to_p2_and_check_for_interrupt;
89291}
89292
89293/* Opcode: Return P1 P2 P3 * *
89294**
89295** Jump to the address stored in register P1. If P1 is a return address
89296** register, then this accomplishes a return from a subroutine.
89297**
89298** If P3 is 1, then the jump is only taken if register P1 holds an integer
89299** values, otherwise execution falls through to the next opcode, and the
89300** OP_Return becomes a no-op. If P3 is 0, then register P1 must hold an
89301** integer or else an assert() is raised. P3 should be set to 1 when
89302** this opcode is used in combination with OP_BeginSubrtn, and set to 0
89303** otherwise.
89304**
89305** The value in register P1 is unchanged by this opcode.
89306**
89307** P2 is not used by the byte-code engine. However, if P2 is positive
89308** and also less than the current address, then the "EXPLAIN" output
89309** formatter in the CLI will indent all opcodes from the P2 opcode up
89310** to be not including the current Return. P2 should be the first opcode
89311** in the subroutine from which this opcode is returning. Thus the P2
89312** value is a byte-code indentation hint. See tag-20220407a in
89313** wherecode.c and shell.c.
89314*/
89315case OP_Return67: { /* in1 */
89316 pIn1 = &aMem[pOp->p1];
89317 if( pIn1->flags & MEM_Int0x0004 ){
89318 if( pOp->p3 ){ VdbeBranchTaken(1, 2); }
89319 pOp = &aOp[pIn1->u.i];
89320 }else if( ALWAYS(pOp->p3)(pOp->p3) ){
89321 VdbeBranchTaken(0, 2);
89322 }
89323 break;
89324}
89325
89326/* Opcode: InitCoroutine P1 P2 P3 * *
89327**
89328** Set up register P1 so that it will Yield to the coroutine
89329** located at address P3.
89330**
89331** If P2!=0 then the coroutine implementation immediately follows
89332** this opcode. So jump over the coroutine implementation to
89333** address P2.
89334**
89335** See also: EndCoroutine
89336*/
89337case OP_InitCoroutine10: { /* jump */
89338 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) )((void) (0));
89339 assert( pOp->p2>=0 && pOp->p2<p->nOp )((void) (0));
89340 assert( pOp->p3>=0 && pOp->p3<p->nOp )((void) (0));
89341 pOut = &aMem[pOp->p1];
89342 assert( !VdbeMemDynamic(pOut) )((void) (0));
89343 pOut->u.i = pOp->p3 - 1;
89344 pOut->flags = MEM_Int0x0004;
89345 if( pOp->p2==0 ) break;
89346
89347 /* Most jump operations do a goto to this spot in order to update
89348 ** the pOp pointer. */
89349jump_to_p2:
89350 assert( pOp->p2>0 )((void) (0)); /* There are never any jumps to instruction 0 */
89351 assert( pOp->p2<p->nOp )((void) (0)); /* Jumps must be in range */
89352 pOp = &aOp[pOp->p2 - 1];
89353 break;
89354}
89355
89356/* Opcode: EndCoroutine P1 * * * *
89357**
89358** The instruction at the address in register P1 is a Yield.
89359** Jump to the P2 parameter of that Yield.
89360** After the jump, register P1 becomes undefined.
89361**
89362** See also: InitCoroutine
89363*/
89364case OP_EndCoroutine68: { /* in1 */
89365 VdbeOp *pCaller;
89366 pIn1 = &aMem[pOp->p1];
89367 assert( pIn1->flags==MEM_Int )((void) (0));
89368 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp )((void) (0));
89369 pCaller = &aOp[pIn1->u.i];
89370 assert( pCaller->opcode==OP_Yield )((void) (0));
89371 assert( pCaller->p2>=0 && pCaller->p2<p->nOp )((void) (0));
89372 pOp = &aOp[pCaller->p2 - 1];
89373 pIn1->flags = MEM_Undefined0x0000;
89374 break;
89375}
89376
89377/* Opcode: Yield P1 P2 * * *
89378**
89379** Swap the program counter with the value in register P1. This
89380** has the effect of yielding to a coroutine.
89381**
89382** If the coroutine that is launched by this instruction ends with
89383** Yield or Return then continue to the next instruction. But if
89384** the coroutine launched by this instruction ends with
89385** EndCoroutine, then jump to P2 rather than continuing with the
89386** next instruction.
89387**
89388** See also: InitCoroutine
89389*/
89390case OP_Yield11: { /* in1, jump */
89391 int pcDest;
89392 pIn1 = &aMem[pOp->p1];
89393 assert( VdbeMemDynamic(pIn1)==0 )((void) (0));
89394 pIn1->flags = MEM_Int0x0004;
89395 pcDest = (int)pIn1->u.i;
89396 pIn1->u.i = (int)(pOp - aOp);
89397 REGISTER_TRACE(pOp->p1, pIn1);
89398 pOp = &aOp[pcDest];
89399 break;
89400}
89401
89402/* Opcode: HaltIfNull P1 P2 P3 P4 P5
89403** Synopsis: if r[P3]=null halt
89404**
89405** Check the value in register P3. If it is NULL then Halt using
89406** parameter P1, P2, and P4 as if this were a Halt instruction. If the
89407** value in register P3 is not NULL, then this routine is a no-op.
89408** The P5 parameter should be 1.
89409*/
89410case OP_HaltIfNull69: { /* in3 */
89411 pIn3 = &aMem[pOp->p3];
89412#ifdef SQLITE_DEBUG
89413 if( pOp->p2==OE_Abort2 ){ sqlite3VdbeAssertAbortable(p); }
89414#endif
89415 if( (pIn3->flags & MEM_Null0x0001)==0 ) break;
89416 /* Fall through into OP_Halt */
89417 /* no break */ deliberate_fall_through
89418}
89419
89420/* Opcode: Halt P1 P2 * P4 P5
89421**
89422** Exit immediately. All open cursors, etc are closed
89423** automatically.
89424**
89425** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
89426** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
89427** For errors, it can be some other value. If P1!=0 then P2 will determine
89428** whether or not to rollback the current transaction. Do not rollback
89429** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
89430** then back out all changes that have occurred during this execution of the
89431** VDBE, but do not rollback the transaction.
89432**
89433** If P4 is not null then it is an error message string.
89434**
89435** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
89436**
89437** 0: (no change)
89438** 1: NOT NULL contraint failed: P4
89439** 2: UNIQUE constraint failed: P4
89440** 3: CHECK constraint failed: P4
89441** 4: FOREIGN KEY constraint failed: P4
89442**
89443** If P5 is not zero and P4 is NULL, then everything after the ":" is
89444** omitted.
89445**
89446** There is an implied "Halt 0 0 0" instruction inserted at the very end of
89447** every program. So a jump past the last instruction of the program
89448** is the same as executing Halt.
89449*/
89450case OP_Halt70: {
89451 VdbeFrame *pFrame;
89452 int pcx;
89453
89454#ifdef SQLITE_DEBUG
89455 if( pOp->p2==OE_Abort2 ){ sqlite3VdbeAssertAbortable(p); }
89456#endif
89457 if( p->pFrame && pOp->p1==SQLITE_OK0 ){
89458 /* Halt the sub-program. Return control to the parent frame. */
89459 pFrame = p->pFrame;
89460 p->pFrame = pFrame->pParent;
89461 p->nFrame--;
89462 sqlite3VdbeSetChanges(db, p->nChange);
89463 pcx = sqlite3VdbeFrameRestore(pFrame);
89464 if( pOp->p2==OE_Ignore4 ){
89465 /* Instruction pcx is the OP_Program that invoked the sub-program
89466 ** currently being halted. If the p2 instruction of this OP_Halt
89467 ** instruction is set to OE_Ignore, then the sub-program is throwing
89468 ** an IGNORE exception. In this case jump to the address specified
89469 ** as the p2 of the calling OP_Program. */
89470 pcx = p->aOp[pcx].p2-1;
89471 }
89472 aOp = p->aOp;
89473 aMem = p->aMem;
89474 pOp = &aOp[pcx];
89475 break;
89476 }
89477 p->rc = pOp->p1;
89478 p->errorAction = (u8)pOp->p2;
89479 assert( pOp->p5<=4 )((void) (0));
89480 if( p->rc ){
89481 if( pOp->p5 ){
89482 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
89483 "FOREIGN KEY" };
89484 testcase( pOp->p5==1 );
89485 testcase( pOp->p5==2 );
89486 testcase( pOp->p5==3 );
89487 testcase( pOp->p5==4 );
89488 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
89489 if( pOp->p4.z ){
89490 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
89491 }
89492 }else{
89493 sqlite3VdbeError(p, "%s", pOp->p4.z);
89494 }
89495 pcx = (int)(pOp - aOp);
89496 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
89497 }
89498 rc = sqlite3VdbeHalt(p);
89499 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR )((void) (0));
89500 if( rc==SQLITE_BUSY5 ){
89501 p->rc = SQLITE_BUSY5;
89502 }else{
89503 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT )((void) (0));
89504 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 )((void) (0));
89505 rc = p->rc ? SQLITE_ERROR1 : SQLITE_DONE101;
89506 }
89507 goto vdbe_return;
89508}
89509
89510/* Opcode: Integer P1 P2 * * *
89511** Synopsis: r[P2]=P1
89512**
89513** The 32-bit integer value P1 is written into register P2.
89514*/
89515case OP_Integer71: { /* out2 */
89516 pOut = out2Prerelease(p, pOp);
89517 pOut->u.i = pOp->p1;
89518 break;
89519}
89520
89521/* Opcode: Int64 * P2 * P4 *
89522** Synopsis: r[P2]=P4
89523**
89524** P4 is a pointer to a 64-bit integer value.
89525** Write that value into register P2.
89526*/
89527case OP_Int6472: { /* out2 */
89528 pOut = out2Prerelease(p, pOp);
89529 assert( pOp->p4.pI64!=0 )((void) (0));
89530 pOut->u.i = *pOp->p4.pI64;
89531 break;
89532}
89533
89534#ifndef SQLITE_OMIT_FLOATING_POINT
89535/* Opcode: Real * P2 * P4 *
89536** Synopsis: r[P2]=P4
89537**
89538** P4 is a pointer to a 64-bit floating point value.
89539** Write that value into register P2.
89540*/
89541case OP_Real153: { /* same as TK_FLOAT, out2 */
89542 pOut = out2Prerelease(p, pOp);
89543 pOut->flags = MEM_Real0x0008;
89544 assert( !sqlite3IsNaN(*pOp->p4.pReal) )((void) (0));
89545 pOut->u.r = *pOp->p4.pReal;
89546 break;
89547}
89548#endif
89549
89550/* Opcode: String8 * P2 * P4 *
89551** Synopsis: r[P2]='P4'
89552**
89553** P4 points to a nul terminated UTF-8 string. This opcode is transformed
89554** into a String opcode before it is executed for the first time. During
89555** this transformation, the length of string P4 is computed and stored
89556** as the P1 parameter.
89557*/
89558case OP_String8117: { /* same as TK_STRING, out2 */
89559 assert( pOp->p4.z!=0 )((void) (0));
89560 pOut = out2Prerelease(p, pOp);
89561 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
89562
89563#ifndef SQLITE_OMIT_UTF16
89564 if( encoding!=SQLITE_UTF81 ){
89565 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF81, SQLITE_STATIC((sqlite3_destructor_type)0));
89566 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG )((void) (0));
89567 if( rc ) goto too_big;
89568 if( SQLITE_OK0!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
89569 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z )((void) (0));
89570 assert( VdbeMemDynamic(pOut)==0 )((void) (0));
89571 pOut->szMalloc = 0;
89572 pOut->flags |= MEM_Static0x2000;
89573 if( pOp->p4type==P4_DYNAMIC(-6) ){
89574 sqlite3DbFree(db, pOp->p4.z);
89575 }
89576 pOp->p4type = P4_DYNAMIC(-6);
89577 pOp->p4.z = pOut->z;
89578 pOp->p1 = pOut->n;
89579 }
89580#endif
89581 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH0] ){
89582 goto too_big;
89583 }
89584 pOp->opcode = OP_String73;
89585 assert( rc==SQLITE_OK )((void) (0));
89586 /* Fall through to the next case, OP_String */
89587 /* no break */ deliberate_fall_through
89588}
89589
89590/* Opcode: String P1 P2 P3 P4 P5
89591** Synopsis: r[P2]='P4' (len=P1)
89592**
89593** The string value P4 of length P1 (bytes) is stored in register P2.
89594**
89595** If P3 is not zero and the content of register P3 is equal to P5, then
89596** the datatype of the register P2 is converted to BLOB. The content is
89597** the same sequence of bytes, it is merely interpreted as a BLOB instead
89598** of a string, as if it had been CAST. In other words:
89599**
89600** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
89601*/
89602case OP_String73: { /* out2 */
89603 assert( pOp->p4.z!=0 )((void) (0));
89604 pOut = out2Prerelease(p, pOp);
89605 pOut->flags = MEM_Str0x0002|MEM_Static0x2000|MEM_Term0x0200;
89606 pOut->z = pOp->p4.z;
89607 pOut->n = pOp->p1;
89608 pOut->enc = encoding;
89609 UPDATE_MAX_BLOBSIZE(pOut);
89610#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
89611 if( pOp->p3>0 ){
89612 assert( pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
89613 pIn3 = &aMem[pOp->p3];
89614 assert( pIn3->flags & MEM_Int )((void) (0));
89615 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob0x0010|MEM_Static0x2000|MEM_Term0x0200;
89616 }
89617#endif
89618 break;
89619}
89620
89621/* Opcode: BeginSubrtn * P2 * * *
89622** Synopsis: r[P2]=NULL
89623**
89624** Mark the beginning of a subroutine that can be entered in-line
89625** or that can be called using OP_Gosub. The subroutine should
89626** be terminated by an OP_Return instruction that has a P1 operand that
89627** is the same as the P2 operand to this opcode and that has P3 set to 1.
89628** If the subroutine is entered in-line, then the OP_Return will simply
89629** fall through. But if the subroutine is entered using OP_Gosub, then
89630** the OP_Return will jump back to the first instruction after the OP_Gosub.
89631**
89632** This routine works by loading a NULL into the P2 register. When the
89633** return address register contains a NULL, the OP_Return instruction is
89634** a no-op that simply falls through to the next instruction (assuming that
89635** the OP_Return opcode has a P3 value of 1). Thus if the subroutine is
89636** entered in-line, then the OP_Return will cause in-line execution to
89637** continue. But if the subroutine is entered via OP_Gosub, then the
89638** OP_Return will cause a return to the address following the OP_Gosub.
89639**
89640** This opcode is identical to OP_Null. It has a different name
89641** only to make the byte code easier to read and verify.
89642*/
89643/* Opcode: Null P1 P2 P3 * *
89644** Synopsis: r[P2..P3]=NULL
89645**
89646** Write a NULL into registers P2. If P3 greater than P2, then also write
89647** NULL into register P3 and every register in between P2 and P3. If P3
89648** is less than P2 (typically P3 is zero) then only register P2 is
89649** set to NULL.
89650**
89651** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
89652** NULL values will not compare equal even if SQLITE_NULLEQ is set on
89653** OP_Ne or OP_Eq.
89654*/
89655case OP_BeginSubrtn74:
89656case OP_Null75: { /* out2 */
89657 int cnt;
89658 u16 nullFlag;
89659 pOut = out2Prerelease(p, pOp);
89660 cnt = pOp->p3-pOp->p2;
89661 assert( pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
89662 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null0x0001|MEM_Cleared0x0100) : MEM_Null0x0001;
89663 pOut->n = 0;
89664#ifdef SQLITE_DEBUG
89665 pOut->uTemp = 0;
89666#endif
89667 while( cnt>0 ){
89668 pOut++;
89669 memAboutToChange(p, pOut);
89670 sqlite3VdbeMemSetNull(pOut);
89671 pOut->flags = nullFlag;
89672 pOut->n = 0;
89673 cnt--;
89674 }
89675 break;
89676}
89677
89678/* Opcode: SoftNull P1 * * * *
89679** Synopsis: r[P1]=NULL
89680**
89681** Set register P1 to have the value NULL as seen by the OP_MakeRecord
89682** instruction, but do not free any string or blob memory associated with
89683** the register, so that if the value was a string or blob that was
89684** previously copied using OP_SCopy, the copies will continue to be valid.
89685*/
89686case OP_SoftNull76: {
89687 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) )((void) (0));
89688 pOut = &aMem[pOp->p1];
89689 pOut->flags = (pOut->flags&~(MEM_Undefined0x0000|MEM_AffMask0x003f))|MEM_Null0x0001;
89690 break;
89691}
89692
89693/* Opcode: Blob P1 P2 * P4 *
89694** Synopsis: r[P2]=P4 (len=P1)
89695**
89696** P4 points to a blob of data P1 bytes long. Store this
89697** blob in register P2. If P4 is a NULL pointer, then construct
89698** a zero-filled blob that is P1 bytes long in P2.
89699*/
89700case OP_Blob77: { /* out2 */
89701 assert( pOp->p1 <= SQLITE_MAX_LENGTH )((void) (0));
89702 pOut = out2Prerelease(p, pOp);
89703 if( pOp->p4.z==0 ){
89704 sqlite3VdbeMemSetZeroBlob(pOut, pOp->p1);
89705 if( sqlite3VdbeMemExpandBlob(pOut) ) goto no_mem;
89706 }else{
89707 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
89708 }
89709 pOut->enc = encoding;
89710 UPDATE_MAX_BLOBSIZE(pOut);
89711 break;
89712}
89713
89714/* Opcode: Variable P1 P2 * P4 *
89715** Synopsis: r[P2]=parameter(P1,P4)
89716**
89717** Transfer the values of bound parameter P1 into register P2
89718**
89719** If the parameter is named, then its name appears in P4.
89720** The P4 value is used by sqlite3_bind_parameter_name().
89721*/
89722case OP_Variable78: { /* out2 */
89723 Mem *pVar; /* Value being transferred */
89724
89725 assert( pOp->p1>0 && pOp->p1<=p->nVar )((void) (0));
89726 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) )((void) (0));
89727 pVar = &p->aVar[pOp->p1 - 1];
89728 if( sqlite3VdbeMemTooBig(pVar) ){
89729 goto too_big;
89730 }
89731 pOut = &aMem[pOp->p2];
89732 if( VdbeMemDynamic(pOut)(((pOut)->flags&(0x8000|0x1000))!=0) ) sqlite3VdbeMemSetNull(pOut);
89733 memcpy(pOut, pVar, MEMCELLSIZE__builtin_offsetof(Mem, db));
89734 pOut->flags &= ~(MEM_Dyn0x1000|MEM_Ephem0x4000);
89735 pOut->flags |= MEM_Static0x2000|MEM_FromBind0x0040;
89736 UPDATE_MAX_BLOBSIZE(pOut);
89737 break;
89738}
89739
89740/* Opcode: Move P1 P2 P3 * *
89741** Synopsis: r[P2@P3]=r[P1@P3]
89742**
89743** Move the P3 values in register P1..P1+P3-1 over into
89744** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
89745** left holding a NULL. It is an error for register ranges
89746** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
89747** for P3 to be less than 1.
89748*/
89749case OP_Move79: {
89750 int n; /* Number of registers left to copy */
89751 int p1; /* Register to copy from */
89752 int p2; /* Register to copy to */
89753
89754 n = pOp->p3;
89755 p1 = pOp->p1;
89756 p2 = pOp->p2;
89757 assert( n>0 && p1>0 && p2>0 )((void) (0));
89758 assert( p1+n<=p2 || p2+n<=p1 )((void) (0));
89759
89760 pIn1 = &aMem[p1];
89761 pOut = &aMem[p2];
89762 do{
89763 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] )((void) (0));
89764 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] )((void) (0));
89765 assert( memIsValid(pIn1) )((void) (0));
89766 memAboutToChange(p, pOut);
89767 sqlite3VdbeMemMove(pOut, pIn1);
89768#ifdef SQLITE_DEBUG
89769 pIn1->pScopyFrom = 0;
89770 { int i;
89771 for(i=1; i<p->nMem; i++){
89772 if( aMem[i].pScopyFrom==pIn1 ){
89773 aMem[i].pScopyFrom = pOut;
89774 }
89775 }
89776 }
89777#endif
89778 Deephemeralize(pOut)if( ((pOut)->flags&0x4000)!=0 && sqlite3VdbeMemMakeWriteable
(pOut) ){ goto no_mem;}
;
89779 REGISTER_TRACE(p2++, pOut);
89780 pIn1++;
89781 pOut++;
89782 }while( --n );
89783 break;
89784}
89785
89786/* Opcode: Copy P1 P2 P3 * P5
89787** Synopsis: r[P2@P3+1]=r[P1@P3+1]
89788**
89789** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
89790**
89791** If the 0x0002 bit of P5 is set then also clear the MEM_Subtype flag in the
89792** destination. The 0x0001 bit of P5 indicates that this Copy opcode cannot
89793** be merged. The 0x0001 bit is used by the query planner and does not
89794** come into play during query execution.
89795**
89796** This instruction makes a deep copy of the value. A duplicate
89797** is made of any string or blob constant. See also OP_SCopy.
89798*/
89799case OP_Copy80: {
89800 int n;
89801
89802 n = pOp->p3;
89803 pIn1 = &aMem[pOp->p1];
89804 pOut = &aMem[pOp->p2];
89805 assert( pOut!=pIn1 )((void) (0));
89806 while( 1 ){
89807 memAboutToChange(p, pOut);
89808 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem0x4000);
89809 Deephemeralize(pOut)if( ((pOut)->flags&0x4000)!=0 && sqlite3VdbeMemMakeWriteable
(pOut) ){ goto no_mem;}
;
89810 if( (pOut->flags & MEM_Subtype0x0800)!=0 && (pOp->p5 & 0x0002)!=0 ){
89811 pOut->flags &= ~MEM_Subtype0x0800;
89812 }
89813#ifdef SQLITE_DEBUG
89814 pOut->pScopyFrom = 0;
89815#endif
89816 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
89817 if( (n--)==0 ) break;
89818 pOut++;
89819 pIn1++;
89820 }
89821 break;
89822}
89823
89824/* Opcode: SCopy P1 P2 * * *
89825** Synopsis: r[P2]=r[P1]
89826**
89827** Make a shallow copy of register P1 into register P2.
89828**
89829** This instruction makes a shallow copy of the value. If the value
89830** is a string or blob, then the copy is only a pointer to the
89831** original and hence if the original changes so will the copy.
89832** Worse, if the original is deallocated, the copy becomes invalid.
89833** Thus the program must guarantee that the original will not change
89834** during the lifetime of the copy. Use OP_Copy to make a complete
89835** copy.
89836*/
89837case OP_SCopy81: { /* out2 */
89838 pIn1 = &aMem[pOp->p1];
89839 pOut = &aMem[pOp->p2];
89840 assert( pOut!=pIn1 )((void) (0));
89841 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem0x4000);
89842#ifdef SQLITE_DEBUG
89843 pOut->pScopyFrom = pIn1;
89844 pOut->mScopyFlags = pIn1->flags;
89845#endif
89846 break;
89847}
89848
89849/* Opcode: IntCopy P1 P2 * * *
89850** Synopsis: r[P2]=r[P1]
89851**
89852** Transfer the integer value held in register P1 into register P2.
89853**
89854** This is an optimized version of SCopy that works only for integer
89855** values.
89856*/
89857case OP_IntCopy82: { /* out2 */
89858 pIn1 = &aMem[pOp->p1];
89859 assert( (pIn1->flags & MEM_Int)!=0 )((void) (0));
89860 pOut = &aMem[pOp->p2];
89861 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
89862 break;
89863}
89864
89865/* Opcode: FkCheck * * * * *
89866**
89867** Halt with an SQLITE_CONSTRAINT error if there are any unresolved
89868** foreign key constraint violations. If there are no foreign key
89869** constraint violations, this is a no-op.
89870**
89871** FK constraint violations are also checked when the prepared statement
89872** exits. This opcode is used to raise foreign key constraint errors prior
89873** to returning results such as a row change count or the result of a
89874** RETURNING clause.
89875*/
89876case OP_FkCheck83: {
89877 if( (rc = sqlite3VdbeCheckFk(p,0))!=SQLITE_OK0 ){
89878 goto abort_due_to_error;
89879 }
89880 break;
89881}
89882
89883/* Opcode: ResultRow P1 P2 * * *
89884** Synopsis: output=r[P1@P2]
89885**
89886** The registers P1 through P1+P2-1 contain a single row of
89887** results. This opcode causes the sqlite3_step() call to terminate
89888** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
89889** structure to provide access to the r(P1)..r(P1+P2-1) values as
89890** the result row.
89891*/
89892case OP_ResultRow84: {
89893 assert( p->nResColumn==pOp->p2 )((void) (0));
89894 assert( pOp->p1>0 || CORRUPT_DB )((void) (0));
89895 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
89896
89897 p->cacheCtr = (p->cacheCtr + 2)|1;
89898 p->pResultSet = &aMem[pOp->p1];
89899#ifdef SQLITE_DEBUG
89900 {
89901 Mem *pMem = p->pResultSet;
89902 int i;
89903 for(i=0; i<pOp->p2; i++){
89904 assert( memIsValid(&pMem[i]) )((void) (0));
89905 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
89906 /* The registers in the result will not be used again when the
89907 ** prepared statement restarts. This is because sqlite3_column()
89908 ** APIs might have caused type conversions of made other changes to
89909 ** the register values. Therefore, we can go ahead and break any
89910 ** OP_SCopy dependencies. */
89911 pMem[i].pScopyFrom = 0;
89912 }
89913 }
89914#endif
89915 if( db->mallocFailed ) goto no_mem;
89916 if( db->mTrace & SQLITE_TRACE_ROW0x04 ){
89917 db->trace.xV2(SQLITE_TRACE_ROW0x04, db->pTraceArg, p, 0);
89918 }
89919 p->pc = (int)(pOp - aOp) + 1;
89920 rc = SQLITE_ROW100;
89921 goto vdbe_return;
89922}
89923
89924/* Opcode: Concat P1 P2 P3 * *
89925** Synopsis: r[P3]=r[P2]+r[P1]
89926**
89927** Add the text in register P1 onto the end of the text in
89928** register P2 and store the result in register P3.
89929** If either the P1 or P2 text are NULL then store NULL in P3.
89930**
89931** P3 = P2 || P1
89932**
89933** It is illegal for P1 and P3 to be the same register. Sometimes,
89934** if P3 is the same register as P2, the implementation is able
89935** to avoid a memcpy().
89936*/
89937case OP_Concat111: { /* same as TK_CONCAT, in1, in2, out3 */
89938 i64 nByte; /* Total size of the output string or blob */
89939 u16 flags1; /* Initial flags for P1 */
89940 u16 flags2; /* Initial flags for P2 */
89941
89942 pIn1 = &aMem[pOp->p1];
89943 pIn2 = &aMem[pOp->p2];
89944 pOut = &aMem[pOp->p3];
89945 testcase( pOut==pIn2 );
89946 assert( pIn1!=pOut )((void) (0));
89947 flags1 = pIn1->flags;
89948 testcase( flags1 & MEM_Null );
89949 testcase( pIn2->flags & MEM_Null );
89950 if( (flags1 | pIn2->flags) & MEM_Null0x0001 ){
89951 sqlite3VdbeMemSetNull(pOut);
89952 break;
89953 }
89954 if( (flags1 & (MEM_Str0x0002|MEM_Blob0x0010))==0 ){
89955 if( sqlite3VdbeMemStringify(pIn1,encoding,0) ) goto no_mem;
89956 flags1 = pIn1->flags & ~MEM_Str0x0002;
89957 }else if( (flags1 & MEM_Zero0x0400)!=0 ){
89958 if( sqlite3VdbeMemExpandBlob(pIn1) ) goto no_mem;
89959 flags1 = pIn1->flags & ~MEM_Str0x0002;
89960 }
89961 flags2 = pIn2->flags;
89962 if( (flags2 & (MEM_Str0x0002|MEM_Blob0x0010))==0 ){
89963 if( sqlite3VdbeMemStringify(pIn2,encoding,0) ) goto no_mem;
89964 flags2 = pIn2->flags & ~MEM_Str0x0002;
89965 }else if( (flags2 & MEM_Zero0x0400)!=0 ){
89966 if( sqlite3VdbeMemExpandBlob(pIn2) ) goto no_mem;
89967 flags2 = pIn2->flags & ~MEM_Str0x0002;
89968 }
89969 nByte = pIn1->n + pIn2->n;
89970 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH0] ){
89971 goto too_big;
89972 }
89973 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
89974 goto no_mem;
89975 }
89976 MemSetTypeFlag(pOut, MEM_Str)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0002
)
;
89977 if( pOut!=pIn2 ){
89978 memcpy(pOut->z, pIn2->z, pIn2->n);
89979 assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) )((void) (0));
89980 pIn2->flags = flags2;
89981 }
89982 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
89983 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) )((void) (0));
89984 pIn1->flags = flags1;
89985 if( encoding>SQLITE_UTF81 ) nByte &= ~1;
89986 pOut->z[nByte]=0;
89987 pOut->z[nByte+1] = 0;
89988 pOut->flags |= MEM_Term0x0200;
89989 pOut->n = (int)nByte;
89990 pOut->enc = encoding;
89991 UPDATE_MAX_BLOBSIZE(pOut);
89992 break;
89993}
89994
89995/* Opcode: Add P1 P2 P3 * *
89996** Synopsis: r[P3]=r[P1]+r[P2]
89997**
89998** Add the value in register P1 to the value in register P2
89999** and store the result in register P3.
90000** If either input is NULL, the result is NULL.
90001*/
90002/* Opcode: Multiply P1 P2 P3 * *
90003** Synopsis: r[P3]=r[P1]*r[P2]
90004**
90005**
90006** Multiply the value in register P1 by the value in register P2
90007** and store the result in register P3.
90008** If either input is NULL, the result is NULL.
90009*/
90010/* Opcode: Subtract P1 P2 P3 * *
90011** Synopsis: r[P3]=r[P2]-r[P1]
90012**
90013** Subtract the value in register P1 from the value in register P2
90014** and store the result in register P3.
90015** If either input is NULL, the result is NULL.
90016*/
90017/* Opcode: Divide P1 P2 P3 * *
90018** Synopsis: r[P3]=r[P2]/r[P1]
90019**
90020** Divide the value in register P1 by the value in register P2
90021** and store the result in register P3 (P3=P2/P1). If the value in
90022** register P1 is zero, then the result is NULL. If either input is
90023** NULL, the result is NULL.
90024*/
90025/* Opcode: Remainder P1 P2 P3 * *
90026** Synopsis: r[P3]=r[P2]%r[P1]
90027**
90028** Compute the remainder after integer register P2 is divided by
90029** register P1 and store the result in register P3.
90030** If the value in register P1 is zero the result is NULL.
90031** If either operand is NULL, the result is NULL.
90032*/
90033case OP_Add106: /* same as TK_PLUS, in1, in2, out3 */
90034case OP_Subtract107: /* same as TK_MINUS, in1, in2, out3 */
90035case OP_Multiply108: /* same as TK_STAR, in1, in2, out3 */
90036case OP_Divide109: /* same as TK_SLASH, in1, in2, out3 */
90037case OP_Remainder110: { /* same as TK_REM, in1, in2, out3 */
90038 u16 flags; /* Combined MEM_* flags from both inputs */
90039 u16 type1; /* Numeric type of left operand */
90040 u16 type2; /* Numeric type of right operand */
90041 i64 iA; /* Integer value of left operand */
90042 i64 iB; /* Integer value of right operand */
90043 double rA; /* Real value of left operand */
90044 double rB; /* Real value of right operand */
90045
90046 pIn1 = &aMem[pOp->p1];
90047 type1 = numericType(pIn1);
90048 pIn2 = &aMem[pOp->p2];
90049 type2 = numericType(pIn2);
90050 pOut = &aMem[pOp->p3];
90051 flags = pIn1->flags | pIn2->flags;
90052 if( (type1 & type2 & MEM_Int0x0004)!=0 ){
90053 iA = pIn1->u.i;
90054 iB = pIn2->u.i;
90055 switch( pOp->opcode ){
90056 case OP_Add106: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
90057 case OP_Subtract107: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
90058 case OP_Multiply108: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
90059 case OP_Divide109: {
90060 if( iA==0 ) goto arithmetic_result_is_null;
90061 if( iA==-1 && iB==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ) goto fp_math;
90062 iB /= iA;
90063 break;
90064 }
90065 default: {
90066 if( iA==0 ) goto arithmetic_result_is_null;
90067 if( iA==-1 ) iA = 1;
90068 iB %= iA;
90069 break;
90070 }
90071 }
90072 pOut->u.i = iB;
90073 MemSetTypeFlag(pOut, MEM_Int)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0004
)
;
90074 }else if( (flags & MEM_Null0x0001)!=0 ){
90075 goto arithmetic_result_is_null;
90076 }else{
90077fp_math:
90078 rA = sqlite3VdbeRealValue(pIn1);
90079 rB = sqlite3VdbeRealValue(pIn2);
90080 switch( pOp->opcode ){
90081 case OP_Add106: rB += rA; break;
90082 case OP_Subtract107: rB -= rA; break;
90083 case OP_Multiply108: rB *= rA; break;
90084 case OP_Divide109: {
90085 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
90086 if( rA==(double)0 ) goto arithmetic_result_is_null;
90087 rB /= rA;
90088 break;
90089 }
90090 default: {
90091 iA = sqlite3VdbeIntValue(pIn1);
90092 iB = sqlite3VdbeIntValue(pIn2);
90093 if( iA==0 ) goto arithmetic_result_is_null;
90094 if( iA==-1 ) iA = 1;
90095 rB = (double)(iB % iA);
90096 break;
90097 }
90098 }
90099#ifdef SQLITE_OMIT_FLOATING_POINT
90100 pOut->u.i = rB;
90101 MemSetTypeFlag(pOut, MEM_Int)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0004
)
;
90102#else
90103 if( sqlite3IsNaN(rB) ){
90104 goto arithmetic_result_is_null;
90105 }
90106 pOut->u.r = rB;
90107 MemSetTypeFlag(pOut, MEM_Real)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0008
)
;
90108#endif
90109 }
90110 break;
90111
90112arithmetic_result_is_null:
90113 sqlite3VdbeMemSetNull(pOut);
90114 break;
90115}
90116
90117/* Opcode: CollSeq P1 * * P4
90118**
90119** P4 is a pointer to a CollSeq object. If the next call to a user function
90120** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
90121** be returned. This is used by the built-in min(), max() and nullif()
90122** functions.
90123**
90124** If P1 is not zero, then it is a register that a subsequent min() or
90125** max() aggregate will set to 1 if the current row is not the minimum or
90126** maximum. The P1 register is initialized to 0 by this instruction.
90127**
90128** The interface used by the implementation of the aforementioned functions
90129** to retrieve the collation sequence set by this opcode is not available
90130** publicly. Only built-in functions have access to this feature.
90131*/
90132case OP_CollSeq85: {
90133 assert( pOp->p4type==P4_COLLSEQ )((void) (0));
90134 if( pOp->p1 ){
90135 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
90136 }
90137 break;
90138}
90139
90140/* Opcode: BitAnd P1 P2 P3 * *
90141** Synopsis: r[P3]=r[P1]&r[P2]
90142**
90143** Take the bit-wise AND of the values in register P1 and P2 and
90144** store the result in register P3.
90145** If either input is NULL, the result is NULL.
90146*/
90147/* Opcode: BitOr P1 P2 P3 * *
90148** Synopsis: r[P3]=r[P1]|r[P2]
90149**
90150** Take the bit-wise OR of the values in register P1 and P2 and
90151** store the result in register P3.
90152** If either input is NULL, the result is NULL.
90153*/
90154/* Opcode: ShiftLeft P1 P2 P3 * *
90155** Synopsis: r[P3]=r[P2]<<r[P1]
90156**
90157** Shift the integer value in register P2 to the left by the
90158** number of bits specified by the integer in register P1.
90159** Store the result in register P3.
90160** If either input is NULL, the result is NULL.
90161*/
90162/* Opcode: ShiftRight P1 P2 P3 * *
90163** Synopsis: r[P3]=r[P2]>>r[P1]
90164**
90165** Shift the integer value in register P2 to the right by the
90166** number of bits specified by the integer in register P1.
90167** Store the result in register P3.
90168** If either input is NULL, the result is NULL.
90169*/
90170case OP_BitAnd102: /* same as TK_BITAND, in1, in2, out3 */
90171case OP_BitOr103: /* same as TK_BITOR, in1, in2, out3 */
90172case OP_ShiftLeft104: /* same as TK_LSHIFT, in1, in2, out3 */
90173case OP_ShiftRight105: { /* same as TK_RSHIFT, in1, in2, out3 */
90174 i64 iA;
90175 u64 uA;
90176 i64 iB;
90177 u8 op;
90178
90179 pIn1 = &aMem[pOp->p1];
90180 pIn2 = &aMem[pOp->p2];
90181 pOut = &aMem[pOp->p3];
90182 if( (pIn1->flags | pIn2->flags) & MEM_Null0x0001 ){
90183 sqlite3VdbeMemSetNull(pOut);
90184 break;
90185 }
90186 iA = sqlite3VdbeIntValue(pIn2);
90187 iB = sqlite3VdbeIntValue(pIn1);
90188 op = pOp->opcode;
90189 if( op==OP_BitAnd102 ){
90190 iA &= iB;
90191 }else if( op==OP_BitOr103 ){
90192 iA |= iB;
90193 }else if( iB!=0 ){
90194 assert( op==OP_ShiftRight || op==OP_ShiftLeft )((void) (0));
90195
90196 /* If shifting by a negative amount, shift in the other direction */
90197 if( iB<0 ){
90198 assert( OP_ShiftRight==OP_ShiftLeft+1 )((void) (0));
90199 op = 2*OP_ShiftLeft104 + 1 - op;
90200 iB = iB>(-64) ? -iB : 64;
90201 }
90202
90203 if( iB>=64 ){
90204 iA = (iA>=0 || op==OP_ShiftLeft104) ? 0 : -1;
90205 }else{
90206 memcpy(&uA, &iA, sizeof(uA));
90207 if( op==OP_ShiftLeft104 ){
90208 uA <<= iB;
90209 }else{
90210 uA >>= iB;
90211 /* Sign-extend on a right shift of a negative number */
90212 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
90213 }
90214 memcpy(&iA, &uA, sizeof(iA));
90215 }
90216 }
90217 pOut->u.i = iA;
90218 MemSetTypeFlag(pOut, MEM_Int)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0004
)
;
90219 break;
90220}
90221
90222/* Opcode: AddImm P1 P2 * * *
90223** Synopsis: r[P1]=r[P1]+P2
90224**
90225** Add the constant P2 to the value in register P1.
90226** The result is always an integer.
90227**
90228** To force any register to be an integer, just add 0.
90229*/
90230case OP_AddImm86: { /* in1 */
90231 pIn1 = &aMem[pOp->p1];
90232 memAboutToChange(p, pIn1);
90233 sqlite3VdbeMemIntegerify(pIn1);
90234 pIn1->u.i += pOp->p2;
90235 break;
90236}
90237
90238/* Opcode: MustBeInt P1 P2 * * *
90239**
90240** Force the value in register P1 to be an integer. If the value
90241** in P1 is not an integer and cannot be converted into an integer
90242** without data loss, then jump immediately to P2, or if P2==0
90243** raise an SQLITE_MISMATCH exception.
90244*/
90245case OP_MustBeInt12: { /* jump, in1 */
90246 pIn1 = &aMem[pOp->p1];
90247 if( (pIn1->flags & MEM_Int0x0004)==0 ){
90248 applyAffinity(pIn1, SQLITE_AFF_NUMERIC0x43, encoding);
90249 if( (pIn1->flags & MEM_Int0x0004)==0 ){
90250 VdbeBranchTaken(1, 2);
90251 if( pOp->p2==0 ){
90252 rc = SQLITE_MISMATCH20;
90253 goto abort_due_to_error;
90254 }else{
90255 goto jump_to_p2;
90256 }
90257 }
90258 }
90259 VdbeBranchTaken(0, 2);
90260 MemSetTypeFlag(pIn1, MEM_Int)((pIn1)->flags = ((pIn1)->flags&~(0x0dbf|0x0400))|0x0004
)
;
90261 break;
90262}
90263
90264#ifndef SQLITE_OMIT_FLOATING_POINT
90265/* Opcode: RealAffinity P1 * * * *
90266**
90267** If register P1 holds an integer convert it to a real value.
90268**
90269** This opcode is used when extracting information from a column that
90270** has REAL affinity. Such column values may still be stored as
90271** integers, for space efficiency, but after extraction we want them
90272** to have only a real value.
90273*/
90274case OP_RealAffinity87: { /* in1 */
90275 pIn1 = &aMem[pOp->p1];
90276 if( pIn1->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
90277 testcase( pIn1->flags & MEM_Int );
90278 testcase( pIn1->flags & MEM_IntReal );
90279 sqlite3VdbeMemRealify(pIn1);
90280 REGISTER_TRACE(pOp->p1, pIn1);
90281 }
90282 break;
90283}
90284#endif
90285
90286#ifndef SQLITE_OMIT_CAST
90287/* Opcode: Cast P1 P2 * * *
90288** Synopsis: affinity(r[P1])
90289**
90290** Force the value in register P1 to be the type defined by P2.
90291**
90292** <ul>
90293** <li> P2=='A' &rarr; BLOB
90294** <li> P2=='B' &rarr; TEXT
90295** <li> P2=='C' &rarr; NUMERIC
90296** <li> P2=='D' &rarr; INTEGER
90297** <li> P2=='E' &rarr; REAL
90298** </ul>
90299**
90300** A NULL value is not changed by this routine. It remains NULL.
90301*/
90302case OP_Cast88: { /* in1 */
90303 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL )((void) (0));
90304 testcase( pOp->p2==SQLITE_AFF_TEXT );
90305 testcase( pOp->p2==SQLITE_AFF_BLOB );
90306 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
90307 testcase( pOp->p2==SQLITE_AFF_INTEGER );
90308 testcase( pOp->p2==SQLITE_AFF_REAL );
90309 pIn1 = &aMem[pOp->p1];
90310 memAboutToChange(p, pIn1);
90311 rc = ExpandBlob(pIn1)(((pIn1)->flags&0x0400)?sqlite3VdbeMemExpandBlob(pIn1)
:0)
;
90312 if( rc ) goto abort_due_to_error;
90313 rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
90314 if( rc ) goto abort_due_to_error;
90315 UPDATE_MAX_BLOBSIZE(pIn1);
90316 REGISTER_TRACE(pOp->p1, pIn1);
90317 break;
90318}
90319#endif /* SQLITE_OMIT_CAST */
90320
90321/* Opcode: Eq P1 P2 P3 P4 P5
90322** Synopsis: IF r[P3]==r[P1]
90323**
90324** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
90325** jump to address P2.
90326**
90327** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
90328** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
90329** to coerce both inputs according to this affinity before the
90330** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
90331** affinity is used. Note that the affinity conversions are stored
90332** back into the input registers P1 and P3. So this opcode can cause
90333** persistent changes to registers P1 and P3.
90334**
90335** Once any conversions have taken place, and neither value is NULL,
90336** the values are compared. If both values are blobs then memcmp() is
90337** used to determine the results of the comparison. If both values
90338** are text, then the appropriate collating function specified in
90339** P4 is used to do the comparison. If P4 is not specified then
90340** memcmp() is used to compare text string. If both values are
90341** numeric, then a numeric comparison is used. If the two values
90342** are of different types, then numbers are considered less than
90343** strings and strings are considered less than blobs.
90344**
90345** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
90346** true or false and is never NULL. If both operands are NULL then the result
90347** of comparison is true. If either operand is NULL then the result is false.
90348** If neither operand is NULL the result is the same as it would be if
90349** the SQLITE_NULLEQ flag were omitted from P5.
90350**
90351** This opcode saves the result of comparison for use by the new
90352** OP_Jump opcode.
90353*/
90354/* Opcode: Ne P1 P2 P3 P4 P5
90355** Synopsis: IF r[P3]!=r[P1]
90356**
90357** This works just like the Eq opcode except that the jump is taken if
90358** the operands in registers P1 and P3 are not equal. See the Eq opcode for
90359** additional information.
90360*/
90361/* Opcode: Lt P1 P2 P3 P4 P5
90362** Synopsis: IF r[P3]<r[P1]
90363**
90364** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
90365** jump to address P2.
90366**
90367** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
90368** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
90369** bit is clear then fall through if either operand is NULL.
90370**
90371** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
90372** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
90373** to coerce both inputs according to this affinity before the
90374** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
90375** affinity is used. Note that the affinity conversions are stored
90376** back into the input registers P1 and P3. So this opcode can cause
90377** persistent changes to registers P1 and P3.
90378**
90379** Once any conversions have taken place, and neither value is NULL,
90380** the values are compared. If both values are blobs then memcmp() is
90381** used to determine the results of the comparison. If both values
90382** are text, then the appropriate collating function specified in
90383** P4 is used to do the comparison. If P4 is not specified then
90384** memcmp() is used to compare text string. If both values are
90385** numeric, then a numeric comparison is used. If the two values
90386** are of different types, then numbers are considered less than
90387** strings and strings are considered less than blobs.
90388**
90389** This opcode saves the result of comparison for use by the new
90390** OP_Jump opcode.
90391*/
90392/* Opcode: Le P1 P2 P3 P4 P5
90393** Synopsis: IF r[P3]<=r[P1]
90394**
90395** This works just like the Lt opcode except that the jump is taken if
90396** the content of register P3 is less than or equal to the content of
90397** register P1. See the Lt opcode for additional information.
90398*/
90399/* Opcode: Gt P1 P2 P3 P4 P5
90400** Synopsis: IF r[P3]>r[P1]
90401**
90402** This works just like the Lt opcode except that the jump is taken if
90403** the content of register P3 is greater than the content of
90404** register P1. See the Lt opcode for additional information.
90405*/
90406/* Opcode: Ge P1 P2 P3 P4 P5
90407** Synopsis: IF r[P3]>=r[P1]
90408**
90409** This works just like the Lt opcode except that the jump is taken if
90410** the content of register P3 is greater than or equal to the content of
90411** register P1. See the Lt opcode for additional information.
90412*/
90413case OP_Eq53: /* same as TK_EQ, jump, in1, in3 */
90414case OP_Ne52: /* same as TK_NE, jump, in1, in3 */
90415case OP_Lt56: /* same as TK_LT, jump, in1, in3 */
90416case OP_Le55: /* same as TK_LE, jump, in1, in3 */
90417case OP_Gt54: /* same as TK_GT, jump, in1, in3 */
90418case OP_Ge57: { /* same as TK_GE, jump, in1, in3 */
90419 int res, res2; /* Result of the comparison of pIn1 against pIn3 */
90420 char affinity; /* Affinity to use for comparison */
90421 u16 flags1; /* Copy of initial value of pIn1->flags */
90422 u16 flags3; /* Copy of initial value of pIn3->flags */
90423
90424 pIn1 = &aMem[pOp->p1];
90425 pIn3 = &aMem[pOp->p3];
90426 flags1 = pIn1->flags;
90427 flags3 = pIn3->flags;
90428 if( (flags1 & flags3 & MEM_Int0x0004)!=0 ){
90429 assert( (pOp->p5 & SQLITE_AFF_MASK)!=SQLITE_AFF_TEXT || CORRUPT_DB )((void) (0));
90430 /* Common case of comparison of two integers */
90431 if( pIn3->u.i > pIn1->u.i ){
90432 if( sqlite3aGTb[pOp->opcode] ){
90433 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
90434 goto jump_to_p2;
90435 }
90436 iCompare = +1;
90437 }else if( pIn3->u.i < pIn1->u.i ){
90438 if( sqlite3aLTb[pOp->opcode] ){
90439 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
90440 goto jump_to_p2;
90441 }
90442 iCompare = -1;
90443 }else{
90444 if( sqlite3aEQb[pOp->opcode] ){
90445 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
90446 goto jump_to_p2;
90447 }
90448 iCompare = 0;
90449 }
90450 VdbeBranchTaken(0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
90451 break;
90452 }
90453 if( (flags1 | flags3)&MEM_Null0x0001 ){
90454 /* One or both operands are NULL */
90455 if( pOp->p5 & SQLITE_NULLEQ0x80 ){
90456 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
90457 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
90458 ** or not both operands are null.
90459 */
90460 assert( (flags1 & MEM_Cleared)==0 )((void) (0));
90461 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB )((void) (0));
90462 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
90463 if( (flags1&flags3&MEM_Null0x0001)!=0
90464 && (flags3&MEM_Cleared0x0100)==0
90465 ){
90466 res = 0; /* Operands are equal */
90467 }else{
90468 res = ((flags3 & MEM_Null0x0001) ? -1 : +1); /* Operands are not equal */
90469 }
90470 }else{
90471 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
90472 ** then the result is always NULL.
90473 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
90474 */
90475 VdbeBranchTaken(2,3);
90476 if( pOp->p5 & SQLITE_JUMPIFNULL0x10 ){
90477 goto jump_to_p2;
90478 }
90479 iCompare = 1; /* Operands are not equal */
90480 break;
90481 }
90482 }else{
90483 /* Neither operand is NULL and we couldn't do the special high-speed
90484 ** integer comparison case. So do a general-case comparison. */
90485 affinity = pOp->p5 & SQLITE_AFF_MASK0x47;
90486 if( affinity>=SQLITE_AFF_NUMERIC0x43 ){
90487 if( (flags1 | flags3)&MEM_Str0x0002 ){
90488 if( (flags1 & (MEM_Int0x0004|MEM_IntReal0x0020|MEM_Real0x0008|MEM_Str0x0002))==MEM_Str0x0002 ){
90489 applyNumericAffinity(pIn1,0);
90490 testcase( flags3==pIn3->flags );
90491 flags3 = pIn3->flags;
90492 }
90493 if( (flags3 & (MEM_Int0x0004|MEM_IntReal0x0020|MEM_Real0x0008|MEM_Str0x0002))==MEM_Str0x0002 ){
90494 applyNumericAffinity(pIn3,0);
90495 }
90496 }
90497 }else if( affinity==SQLITE_AFF_TEXT0x42 ){
90498 if( (flags1 & MEM_Str0x0002)==0 && (flags1&(MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020))!=0 ){
90499 testcase( pIn1->flags & MEM_Int );
90500 testcase( pIn1->flags & MEM_Real );
90501 testcase( pIn1->flags & MEM_IntReal );
90502 sqlite3VdbeMemStringify(pIn1, encoding, 1);
90503 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
90504 flags1 = (pIn1->flags & ~MEM_TypeMask0x0dbf) | (flags1 & MEM_TypeMask0x0dbf);
90505 if( pIn1==pIn3 ) flags3 = flags1 | MEM_Str0x0002;
90506 }
90507 if( (flags3 & MEM_Str0x0002)==0 && (flags3&(MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020))!=0 ){
90508 testcase( pIn3->flags & MEM_Int );
90509 testcase( pIn3->flags & MEM_Real );
90510 testcase( pIn3->flags & MEM_IntReal );
90511 sqlite3VdbeMemStringify(pIn3, encoding, 1);
90512 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
90513 flags3 = (pIn3->flags & ~MEM_TypeMask0x0dbf) | (flags3 & MEM_TypeMask0x0dbf);
90514 }
90515 }
90516 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 )((void) (0));
90517 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
90518 }
90519
90520 /* At this point, res is negative, zero, or positive if reg[P1] is
90521 ** less than, equal to, or greater than reg[P3], respectively. Compute
90522 ** the answer to this operator in res2, depending on what the comparison
90523 ** operator actually is. The next block of code depends on the fact
90524 ** that the 6 comparison operators are consecutive integers in this
90525 ** order: NE, EQ, GT, LE, LT, GE */
90526 assert( OP_Eq==OP_Ne+1 )((void) (0)); assert( OP_Gt==OP_Ne+2 )((void) (0)); assert( OP_Le==OP_Ne+3 )((void) (0));
90527 assert( OP_Lt==OP_Ne+4 )((void) (0)); assert( OP_Ge==OP_Ne+5 )((void) (0));
90528 if( res<0 ){
90529 res2 = sqlite3aLTb[pOp->opcode];
90530 }else if( res==0 ){
90531 res2 = sqlite3aEQb[pOp->opcode];
90532 }else{
90533 res2 = sqlite3aGTb[pOp->opcode];
90534 }
90535 iCompare = res;
90536
90537 /* Undo any changes made by applyAffinity() to the input registers. */
90538 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) )((void) (0));
90539 pIn3->flags = flags3;
90540 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) )((void) (0));
90541 pIn1->flags = flags1;
90542
90543 VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
90544 if( res2 ){
90545 goto jump_to_p2;
90546 }
90547 break;
90548}
90549
90550/* Opcode: ElseEq * P2 * * *
90551**
90552** This opcode must follow an OP_Lt or OP_Gt comparison operator. There
90553** can be zero or more OP_ReleaseReg opcodes intervening, but no other
90554** opcodes are allowed to occur between this instruction and the previous
90555** OP_Lt or OP_Gt.
90556**
90557** If result of an OP_Eq comparison on the same two operands as the
90558** prior OP_Lt or OP_Gt would have been true, then jump to P2.
90559** If the result of an OP_Eq comparison on the two previous
90560** operands would have been false or NULL, then fall through.
90561*/
90562case OP_ElseEq58: { /* same as TK_ESCAPE, jump */
90563
90564#ifdef SQLITE_DEBUG
90565 /* Verify the preconditions of this opcode - that it follows an OP_Lt or
90566 ** OP_Gt with zero or more intervening OP_ReleaseReg opcodes */
90567 int iAddr;
90568 for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0)(iAddr>=0); iAddr--){
90569 if( aOp[iAddr].opcode==OP_ReleaseReg183 ) continue;
90570 assert( aOp[iAddr].opcode==OP_Lt || aOp[iAddr].opcode==OP_Gt )((void) (0));
90571 break;
90572 }
90573#endif /* SQLITE_DEBUG */
90574 VdbeBranchTaken(iCompare==0, 2);
90575 if( iCompare==0 ) goto jump_to_p2;
90576 break;
90577}
90578
90579
90580/* Opcode: Permutation * * * P4 *
90581**
90582** Set the permutation used by the OP_Compare operator in the next
90583** instruction. The permutation is stored in the P4 operand.
90584**
90585** The permutation is only valid for the next opcode which must be
90586** an OP_Compare that has the OPFLAG_PERMUTE bit set in P5.
90587**
90588** The first integer in the P4 integer array is the length of the array
90589** and does not become part of the permutation.
90590*/
90591case OP_Permutation89: {
90592 assert( pOp->p4type==P4_INTARRAY )((void) (0));
90593 assert( pOp->p4.ai )((void) (0));
90594 assert( pOp[1].opcode==OP_Compare )((void) (0));
90595 assert( pOp[1].p5 & OPFLAG_PERMUTE )((void) (0));
90596 break;
90597}
90598
90599/* Opcode: Compare P1 P2 P3 P4 P5
90600** Synopsis: r[P1@P3] <-> r[P2@P3]
90601**
90602** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
90603** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
90604** the comparison for use by the next OP_Jump instruct.
90605**
90606** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
90607** determined by the most recent OP_Permutation operator. If the
90608** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
90609** order.
90610**
90611** P4 is a KeyInfo structure that defines collating sequences and sort
90612** orders for the comparison. The permutation applies to registers
90613** only. The KeyInfo elements are used sequentially.
90614**
90615** The comparison is a sort comparison, so NULLs compare equal,
90616** NULLs are less than numbers, numbers are less than strings,
90617** and strings are less than blobs.
90618**
90619** This opcode must be immediately followed by an OP_Jump opcode.
90620*/
90621case OP_Compare90: {
90622 int n;
90623 int i;
90624 int p1;
90625 int p2;
90626 const KeyInfo *pKeyInfo;
90627 u32 idx;
90628 CollSeq *pColl; /* Collating sequence to use on this term */
90629 int bRev; /* True for DESCENDING sort order */
90630 u32 *aPermute; /* The permutation */
90631
90632 if( (pOp->p5 & OPFLAG_PERMUTE0x01)==0 ){
90633 aPermute = 0;
90634 }else{
90635 assert( pOp>aOp )((void) (0));
90636 assert( pOp[-1].opcode==OP_Permutation )((void) (0));
90637 assert( pOp[-1].p4type==P4_INTARRAY )((void) (0));
90638 aPermute = pOp[-1].p4.ai + 1;
90639 assert( aPermute!=0 )((void) (0));
90640 }
90641 n = pOp->p3;
90642 pKeyInfo = pOp->p4.pKeyInfo;
90643 assert( n>0 )((void) (0));
90644 assert( pKeyInfo!=0 )((void) (0));
90645 p1 = pOp->p1;
90646 p2 = pOp->p2;
90647#ifdef SQLITE_DEBUG
90648 if( aPermute ){
90649 int k, mx = 0;
90650 for(k=0; k<n; k++) if( aPermute[k]>(u32)mx ) mx = aPermute[k];
90651 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
90652 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
90653 }else{
90654 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
90655 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
90656 }
90657#endif /* SQLITE_DEBUG */
90658 for(i=0; i<n; i++){
90659 idx = aPermute ? aPermute[i] : (u32)i;
90660 assert( memIsValid(&aMem[p1+idx]) )((void) (0));
90661 assert( memIsValid(&aMem[p2+idx]) )((void) (0));
90662 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
90663 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
90664 assert( i<pKeyInfo->nKeyField )((void) (0));
90665 pColl = pKeyInfo->aColl[i];
90666 bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC0x01);
90667 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
90668 if( iCompare ){
90669 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL0x02)
90670 && ((aMem[p1+idx].flags & MEM_Null0x0001) || (aMem[p2+idx].flags & MEM_Null0x0001))
90671 ){
90672 iCompare = -iCompare;
90673 }
90674 if( bRev ) iCompare = -iCompare;
90675 break;
90676 }
90677 }
90678 assert( pOp[1].opcode==OP_Jump )((void) (0));
90679 break;
90680}
90681
90682/* Opcode: Jump P1 P2 P3 * *
90683**
90684** Jump to the instruction at address P1, P2, or P3 depending on whether
90685** in the most recent OP_Compare instruction the P1 vector was less than
90686** equal to, or greater than the P2 vector, respectively.
90687**
90688** This opcode must immediately follow an OP_Compare opcode.
90689*/
90690case OP_Jump13: { /* jump */
90691 assert( pOp>aOp && pOp[-1].opcode==OP_Compare )((void) (0));
90692 if( iCompare<0 ){
90693 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
90694 }else if( iCompare==0 ){
90695 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
90696 }else{
90697 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
90698 }
90699 break;
90700}
90701
90702/* Opcode: And P1 P2 P3 * *
90703** Synopsis: r[P3]=(r[P1] && r[P2])
90704**
90705** Take the logical AND of the values in registers P1 and P2 and
90706** write the result into register P3.
90707**
90708** If either P1 or P2 is 0 (false) then the result is 0 even if
90709** the other input is NULL. A NULL and true or two NULLs give
90710** a NULL output.
90711*/
90712/* Opcode: Or P1 P2 P3 * *
90713** Synopsis: r[P3]=(r[P1] || r[P2])
90714**
90715** Take the logical OR of the values in register P1 and P2 and
90716** store the answer in register P3.
90717**
90718** If either P1 or P2 is nonzero (true) then the result is 1 (true)
90719** even if the other input is NULL. A NULL and false or two NULLs
90720** give a NULL output.
90721*/
90722case OP_And44: /* same as TK_AND, in1, in2, out3 */
90723case OP_Or43: { /* same as TK_OR, in1, in2, out3 */
90724 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
90725 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
90726
90727 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
90728 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
90729 if( pOp->opcode==OP_And44 ){
90730 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
90731 v1 = and_logic[v1*3+v2];
90732 }else{
90733 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
90734 v1 = or_logic[v1*3+v2];
90735 }
90736 pOut = &aMem[pOp->p3];
90737 if( v1==2 ){
90738 MemSetTypeFlag(pOut, MEM_Null)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0001
)
;
90739 }else{
90740 pOut->u.i = v1;
90741 MemSetTypeFlag(pOut, MEM_Int)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0004
)
;
90742 }
90743 break;
90744}
90745
90746/* Opcode: IsTrue P1 P2 P3 P4 *
90747** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
90748**
90749** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
90750** IS NOT FALSE operators.
90751**
90752** Interpret the value in register P1 as a boolean value. Store that
90753** boolean (a 0 or 1) in register P2. Or if the value in register P1 is
90754** NULL, then the P3 is stored in register P2. Invert the answer if P4
90755** is 1.
90756**
90757** The logic is summarized like this:
90758**
90759** <ul>
90760** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE
90761** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE
90762** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE
90763** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE
90764** </ul>
90765*/
90766case OP_IsTrue91: { /* in1, out2 */
90767 assert( pOp->p4type==P4_INT32 )((void) (0));
90768 assert( pOp->p4.i==0 || pOp->p4.i==1 )((void) (0));
90769 assert( pOp->p3==0 || pOp->p3==1 )((void) (0));
90770 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
90771 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
90772 break;
90773}
90774
90775/* Opcode: Not P1 P2 * * *
90776** Synopsis: r[P2]= !r[P1]
90777**
90778** Interpret the value in register P1 as a boolean value. Store the
90779** boolean complement in register P2. If the value in register P1 is
90780** NULL, then a NULL is stored in P2.
90781*/
90782case OP_Not19: { /* same as TK_NOT, in1, out2 */
90783 pIn1 = &aMem[pOp->p1];
90784 pOut = &aMem[pOp->p2];
90785 if( (pIn1->flags & MEM_Null0x0001)==0 ){
90786 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0));
90787 }else{
90788 sqlite3VdbeMemSetNull(pOut);
90789 }
90790 break;
90791}
90792
90793/* Opcode: BitNot P1 P2 * * *
90794** Synopsis: r[P2]= ~r[P1]
90795**
90796** Interpret the content of register P1 as an integer. Store the
90797** ones-complement of the P1 value into register P2. If P1 holds
90798** a NULL then store a NULL in P2.
90799*/
90800case OP_BitNot114: { /* same as TK_BITNOT, in1, out2 */
90801 pIn1 = &aMem[pOp->p1];
90802 pOut = &aMem[pOp->p2];
90803 sqlite3VdbeMemSetNull(pOut);
90804 if( (pIn1->flags & MEM_Null0x0001)==0 ){
90805 pOut->flags = MEM_Int0x0004;
90806 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
90807 }
90808 break;
90809}
90810
90811/* Opcode: Once P1 P2 * * *
90812**
90813** Fall through to the next instruction the first time this opcode is
90814** encountered on each invocation of the byte-code program. Jump to P2
90815** on the second and all subsequent encounters during the same invocation.
90816**
90817** Top-level programs determine first invocation by comparing the P1
90818** operand against the P1 operand on the OP_Init opcode at the beginning
90819** of the program. If the P1 values differ, then fall through and make
90820** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are
90821** the same then take the jump.
90822**
90823** For subprograms, there is a bitmask in the VdbeFrame that determines
90824** whether or not the jump should be taken. The bitmask is necessary
90825** because the self-altering code trick does not work for recursive
90826** triggers.
90827*/
90828case OP_Once14: { /* jump */
90829 u32 iAddr; /* Address of this instruction */
90830 assert( p->aOp[0].opcode==OP_Init )((void) (0));
90831 if( p->pFrame ){
90832 iAddr = (int)(pOp - p->aOp);
90833 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
90834 VdbeBranchTaken(1, 2);
90835 goto jump_to_p2;
90836 }
90837 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
90838 }else{
90839 if( p->aOp[0].p1==pOp->p1 ){
90840 VdbeBranchTaken(1, 2);
90841 goto jump_to_p2;
90842 }
90843 }
90844 VdbeBranchTaken(0, 2);
90845 pOp->p1 = p->aOp[0].p1;
90846 break;
90847}
90848
90849/* Opcode: If P1 P2 P3 * *
90850**
90851** Jump to P2 if the value in register P1 is true. The value
90852** is considered true if it is numeric and non-zero. If the value
90853** in P1 is NULL then take the jump if and only if P3 is non-zero.
90854*/
90855case OP_If15: { /* jump, in1 */
90856 int c;
90857 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
90858 VdbeBranchTaken(c!=0, 2);
90859 if( c ) goto jump_to_p2;
90860 break;
90861}
90862
90863/* Opcode: IfNot P1 P2 P3 * *
90864**
90865** Jump to P2 if the value in register P1 is False. The value
90866** is considered false if it has a numeric value of zero. If the value
90867** in P1 is NULL then take the jump if and only if P3 is non-zero.
90868*/
90869case OP_IfNot16: { /* jump, in1 */
90870 int c;
90871 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
90872 VdbeBranchTaken(c!=0, 2);
90873 if( c ) goto jump_to_p2;
90874 break;
90875}
90876
90877/* Opcode: IsNull P1 P2 * * *
90878** Synopsis: if r[P1]==NULL goto P2
90879**
90880** Jump to P2 if the value in register P1 is NULL.
90881*/
90882case OP_IsNull50: { /* same as TK_ISNULL, jump, in1 */
90883 pIn1 = &aMem[pOp->p1];
90884 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
90885 if( (pIn1->flags & MEM_Null0x0001)!=0 ){
90886 goto jump_to_p2;
90887 }
90888 break;
90889}
90890
90891/* Opcode: IsNullOrType P1 P2 P3 * *
90892** Synopsis: if typeof(r[P1]) IN (P3,5) goto P2
90893**
90894** Jump to P2 if the value in register P1 is NULL or has a datatype P3.
90895** P3 is an integer which should be one of SQLITE_INTEGER, SQLITE_FLOAT,
90896** SQLITE_BLOB, SQLITE_NULL, or SQLITE_TEXT.
90897*/
90898case OP_IsNullOrType17: { /* jump, in1 */
90899 int doTheJump;
90900 pIn1 = &aMem[pOp->p1];
90901 doTheJump = (pIn1->flags & MEM_Null0x0001)!=0 || sqlite3_value_type(pIn1)==pOp->p3;
90902 VdbeBranchTaken( doTheJump, 2);
90903 if( doTheJump ) goto jump_to_p2;
90904 break;
90905}
90906
90907/* Opcode: ZeroOrNull P1 P2 P3 * *
90908** Synopsis: r[P2] = 0 OR NULL
90909**
90910** If all both registers P1 and P3 are NOT NULL, then store a zero in
90911** register P2. If either registers P1 or P3 are NULL then put
90912** a NULL in register P2.
90913*/
90914case OP_ZeroOrNull92: { /* in1, in2, out2, in3 */
90915 if( (aMem[pOp->p1].flags & MEM_Null0x0001)!=0
90916 || (aMem[pOp->p3].flags & MEM_Null0x0001)!=0
90917 ){
90918 sqlite3VdbeMemSetNull(aMem + pOp->p2);
90919 }else{
90920 sqlite3VdbeMemSetInt64(aMem + pOp->p2, 0);
90921 }
90922 break;
90923}
90924
90925/* Opcode: NotNull P1 P2 * * *
90926** Synopsis: if r[P1]!=NULL goto P2
90927**
90928** Jump to P2 if the value in register P1 is not NULL.
90929*/
90930case OP_NotNull51: { /* same as TK_NOTNULL, jump, in1 */
90931 pIn1 = &aMem[pOp->p1];
90932 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
90933 if( (pIn1->flags & MEM_Null0x0001)==0 ){
90934 goto jump_to_p2;
90935 }
90936 break;
90937}
90938
90939/* Opcode: IfNullRow P1 P2 P3 * *
90940** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
90941**
90942** Check the cursor P1 to see if it is currently pointing at a NULL row.
90943** If it is, then set register P3 to NULL and jump immediately to P2.
90944** If P1 is not on a NULL row, then fall through without making any
90945** changes.
90946*/
90947case OP_IfNullRow18: { /* jump */
90948 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
90949 assert( p->apCsr[pOp->p1]!=0 )((void) (0));
90950 if( p->apCsr[pOp->p1]->nullRow ){
90951 sqlite3VdbeMemSetNull(aMem + pOp->p3);
90952 goto jump_to_p2;
90953 }
90954 break;
90955}
90956
90957#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
90958/* Opcode: Offset P1 P2 P3 * *
90959** Synopsis: r[P3] = sqlite_offset(P1)
90960**
90961** Store in register r[P3] the byte offset into the database file that is the
90962** start of the payload for the record at which that cursor P1 is currently
90963** pointing.
90964**
90965** P2 is the column number for the argument to the sqlite_offset() function.
90966** This opcode does not use P2 itself, but the P2 value is used by the
90967** code generator. The P1, P2, and P3 operands to this opcode are the
90968** same as for OP_Column.
90969**
90970** This opcode is only available if SQLite is compiled with the
90971** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
90972*/
90973case OP_Offset93: { /* out3 */
90974 VdbeCursor *pC; /* The VDBE cursor */
90975 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
90976 pC = p->apCsr[pOp->p1];
90977 pOut = &p->aMem[pOp->p3];
90978 if( pC==0 || pC->eCurType!=CURTYPE_BTREE0 ){
90979 sqlite3VdbeMemSetNull(pOut);
90980 }else{
90981 if( pC->deferredMoveto ){
90982 rc = sqlite3VdbeFinishMoveto(pC);
90983 if( rc ) goto abort_due_to_error;
90984 }
90985 if( sqlite3BtreeEof(pC->uc.pCursor) ){
90986 sqlite3VdbeMemSetNull(pOut);
90987 }else{
90988 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
90989 }
90990 }
90991 break;
90992}
90993#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
90994
90995/* Opcode: Column P1 P2 P3 P4 P5
90996** Synopsis: r[P3]=PX cursor P1 column P2
90997**
90998** Interpret the data that cursor P1 points to as a structure built using
90999** the MakeRecord instruction. (See the MakeRecord opcode for additional
91000** information about the format of the data.) Extract the P2-th column
91001** from this record. If there are less that (P2+1)
91002** values in the record, extract a NULL.
91003**
91004** The value extracted is stored in register P3.
91005**
91006** If the record contains fewer than P2 fields, then extract a NULL. Or,
91007** if the P4 argument is a P4_MEM use the value of the P4 argument as
91008** the result.
91009**
91010** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then
91011** the result is guaranteed to only be used as the argument of a length()
91012** or typeof() function, respectively. The loading of large blobs can be
91013** skipped for length() and all content loading can be skipped for typeof().
91014*/
91015case OP_Column94: {
91016 u32 p2; /* column number to retrieve */
91017 VdbeCursor *pC; /* The VDBE cursor */
91018 BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */
91019 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
91020 int len; /* The length of the serialized data for the column */
91021 int i; /* Loop counter */
91022 Mem *pDest; /* Where to write the extracted value */
91023 Mem sMem; /* For storing the record being decoded */
91024 const u8 *zData; /* Part of the record being decoded */
91025 const u8 *zHdr; /* Next unparsed byte of the header */
91026 const u8 *zEndHdr; /* Pointer to first byte after the header */
91027 u64 offset64; /* 64-bit offset */
91028 u32 t; /* A type code from the record header */
91029 Mem *pReg; /* PseudoTable input register */
91030
91031 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
91032 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
91033 pC = p->apCsr[pOp->p1];
91034 p2 = (u32)pOp->p2;
91035
91036op_column_restart:
91037 assert( pC!=0 )((void) (0));
91038 assert( p2<(u32)pC->nField((void) (0))
91039 || (pC->eCurType==CURTYPE_PSEUDO && pC->seekResult==0) )((void) (0));
91040 aOffset = pC->aOffset;
91041 assert( aOffset==pC->aType+pC->nField )((void) (0));
91042 assert( pC->eCurType!=CURTYPE_VTAB )((void) (0));
91043 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow )((void) (0));
91044 assert( pC->eCurType!=CURTYPE_SORTER )((void) (0));
91045
91046 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
91047 if( pC->nullRow ){
91048 if( pC->eCurType==CURTYPE_PSEUDO3 && pC->seekResult>0 ){
91049 /* For the special case of as pseudo-cursor, the seekResult field
91050 ** identifies the register that holds the record */
91051 pReg = &aMem[pC->seekResult];
91052 assert( pReg->flags & MEM_Blob )((void) (0));
91053 assert( memIsValid(pReg) )((void) (0));
91054 pC->payloadSize = pC->szRow = pReg->n;
91055 pC->aRow = (u8*)pReg->z;
91056 }else{
91057 pDest = &aMem[pOp->p3];
91058 memAboutToChange(p, pDest);
91059 sqlite3VdbeMemSetNull(pDest);
91060 goto op_column_out;
91061 }
91062 }else{
91063 pCrsr = pC->uc.pCursor;
91064 if( pC->deferredMoveto ){
91065 u32 iMap;
91066 assert( !pC->isEphemeral )((void) (0));
91067 if( pC->ub.aAltMap && (iMap = pC->ub.aAltMap[1+p2])>0 ){
91068 pC = pC->pAltCursor;
91069 p2 = iMap - 1;
91070 goto op_column_restart;
91071 }
91072 rc = sqlite3VdbeFinishMoveto(pC);
91073 if( rc ) goto abort_due_to_error;
91074 }else if( sqlite3BtreeCursorHasMoved(pCrsr) ){
91075 rc = sqlite3VdbeHandleMovedCursor(pC);
91076 if( rc ) goto abort_due_to_error;
91077 goto op_column_restart;
91078 }
91079 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
91080 assert( pCrsr )((void) (0));
91081 assert( sqlite3BtreeCursorIsValid(pCrsr) )((void) (0));
91082 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
91083 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
91084 assert( pC->szRow<=pC->payloadSize )((void) (0));
91085 assert( pC->szRow<=65536 )((void) (0)); /* Maximum page size is 64KiB */
91086 }
91087 pC->cacheStatus = p->cacheCtr;
91088 if( (aOffset[0] = pC->aRow[0])<0x80 ){
91089 pC->iHdrOffset = 1;
91090 }else{
91091 pC->iHdrOffset = sqlite3GetVarint32(pC->aRow, aOffset);
91092 }
91093 pC->nHdrParsed = 0;
91094
91095 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
91096 /* pC->aRow does not have to hold the entire row, but it does at least
91097 ** need to cover the header of the record. If pC->aRow does not contain
91098 ** the complete header, then set it to zero, forcing the header to be
91099 ** dynamically allocated. */
91100 pC->aRow = 0;
91101 pC->szRow = 0;
91102
91103 /* Make sure a corrupt database has not given us an oversize header.
91104 ** Do this now to avoid an oversize memory allocation.
91105 **
91106 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
91107 ** types use so much data space that there can only be 4096 and 32 of
91108 ** them, respectively. So the maximum header length results from a
91109 ** 3-byte type for each of the maximum of 32768 columns plus three
91110 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
91111 */
91112 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
91113 goto op_column_corrupt;
91114 }
91115 }else{
91116 /* This is an optimization. By skipping over the first few tests
91117 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
91118 ** measurable performance gain.
91119 **
91120 ** This branch is taken even if aOffset[0]==0. Such a record is never
91121 ** generated by SQLite, and could be considered corruption, but we
91122 ** accept it for historical reasons. When aOffset[0]==0, the code this
91123 ** branch jumps to reads past the end of the record, but never more
91124 ** than a few bytes. Even if the record occurs at the end of the page
91125 ** content area, the "page header" comes after the page content and so
91126 ** this overread is harmless. Similar overreads can occur for a corrupt
91127 ** database file.
91128 */
91129 zData = pC->aRow;
91130 assert( pC->nHdrParsed<=p2 )((void) (0)); /* Conditional skipped */
91131 testcase( aOffset[0]==0 );
91132 goto op_column_read_header;
91133 }
91134 }else if( sqlite3BtreeCursorHasMoved(pC->uc.pCursor) ){
91135 rc = sqlite3VdbeHandleMovedCursor(pC);
91136 if( rc ) goto abort_due_to_error;
91137 goto op_column_restart;
91138 }
91139
91140 /* Make sure at least the first p2+1 entries of the header have been
91141 ** parsed and valid information is in aOffset[] and pC->aType[].
91142 */
91143 if( pC->nHdrParsed<=p2 ){
91144 /* If there is more header available for parsing in the record, try
91145 ** to extract additional fields up through the p2+1-th field
91146 */
91147 if( pC->iHdrOffset<aOffset[0] ){
91148 /* Make sure zData points to enough of the record to cover the header. */
91149 if( pC->aRow==0 ){
91150 memset(&sMem, 0, sizeof(sMem));
91151 rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem);
91152 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
91153 zData = (u8*)sMem.z;
91154 }else{
91155 zData = pC->aRow;
91156 }
91157
91158 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
91159 op_column_read_header:
91160 i = pC->nHdrParsed;
91161 offset64 = aOffset[i];
91162 zHdr = zData + pC->iHdrOffset;
91163 zEndHdr = zData + aOffset[0];
91164 testcase( zHdr>=zEndHdr );
91165 do{
91166 if( (pC->aType[i] = t = zHdr[0])<0x80 ){
91167 zHdr++;
91168 offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
91169 }else{
91170 zHdr += sqlite3GetVarint32(zHdr, &t);
91171 pC->aType[i] = t;
91172 offset64 += sqlite3VdbeSerialTypeLen(t);
91173 }
91174 aOffset[++i] = (u32)(offset64 & 0xffffffff);
91175 }while( (u32)i<=p2 && zHdr<zEndHdr );
91176
91177 /* The record is corrupt if any of the following are true:
91178 ** (1) the bytes of the header extend past the declared header size
91179 ** (2) the entire header was used but not all data was used
91180 ** (3) the end of the data extends beyond the end of the record.
91181 */
91182 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
91183 || (offset64 > pC->payloadSize)
91184 ){
91185 if( aOffset[0]==0 ){
91186 i = 0;
91187 zHdr = zEndHdr;
91188 }else{
91189 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
91190 goto op_column_corrupt;
91191 }
91192 }
91193
91194 pC->nHdrParsed = i;
91195 pC->iHdrOffset = (u32)(zHdr - zData);
91196 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
91197 }else{
91198 t = 0;
91199 }
91200
91201 /* If after trying to extract new entries from the header, nHdrParsed is
91202 ** still not up to p2, that means that the record has fewer than p2
91203 ** columns. So the result will be either the default value or a NULL.
91204 */
91205 if( pC->nHdrParsed<=p2 ){
91206 pDest = &aMem[pOp->p3];
91207 memAboutToChange(p, pDest);
91208 if( pOp->p4type==P4_MEM(-10) ){
91209 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static0x2000);
91210 }else{
91211 sqlite3VdbeMemSetNull(pDest);
91212 }
91213 goto op_column_out;
91214 }
91215 }else{
91216 t = pC->aType[p2];
91217 }
91218
91219 /* Extract the content for the p2+1-th column. Control can only
91220 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
91221 ** all valid.
91222 */
91223 assert( p2<pC->nHdrParsed )((void) (0));
91224 assert( rc==SQLITE_OK )((void) (0));
91225 pDest = &aMem[pOp->p3];
91226 memAboutToChange(p, pDest);
91227 assert( sqlite3VdbeCheckMemInvariants(pDest) )((void) (0));
91228 if( VdbeMemDynamic(pDest)(((pDest)->flags&(0x8000|0x1000))!=0) ){
91229 sqlite3VdbeMemSetNull(pDest);
91230 }
91231 assert( t==pC->aType[p2] )((void) (0));
91232 if( pC->szRow>=aOffset[p2+1] ){
91233 /* This is the common case where the desired content fits on the original
91234 ** page - where the content is not on an overflow page */
91235 zData = pC->aRow + aOffset[p2];
91236 if( t<12 ){
91237 sqlite3VdbeSerialGet(zData, t, pDest);
91238 }else{
91239 /* If the column value is a string, we need a persistent value, not
91240 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
91241 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
91242 */
91243 static const u16 aFlag[] = { MEM_Blob0x0010, MEM_Str0x0002|MEM_Term0x0200 };
91244 pDest->n = len = (t-12)/2;
91245 pDest->enc = encoding;
91246 if( pDest->szMalloc < len+2 ){
91247 if( len>db->aLimit[SQLITE_LIMIT_LENGTH0] ) goto too_big;
91248 pDest->flags = MEM_Null0x0001;
91249 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem;
91250 }else{
91251 pDest->z = pDest->zMalloc;
91252 }
91253 memcpy(pDest->z, zData, len);
91254 pDest->z[len] = 0;
91255 pDest->z[len+1] = 0;
91256 pDest->flags = aFlag[t&1];
91257 }
91258 }else{
91259 pDest->enc = encoding;
91260 /* This branch happens only when content is on overflow pages */
91261 if( ((pOp->p5 & (OPFLAG_LENGTHARG0x40|OPFLAG_TYPEOFARG0x80))!=0
91262 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG0x80)!=0))
91263 || (len = sqlite3VdbeSerialTypeLen(t))==0
91264 ){
91265 /* Content is irrelevant for
91266 ** 1. the typeof() function,
91267 ** 2. the length(X) function if X is a blob, and
91268 ** 3. if the content length is zero.
91269 ** So we might as well use bogus content rather than reading
91270 ** content from disk.
91271 **
91272 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
91273 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
91274 ** read more. Use the global constant sqlite3CtypeMap[] as the array,
91275 ** as that array is 256 bytes long (plenty for VdbeMemPrettyPrint())
91276 ** and it begins with a bunch of zeros.
91277 */
91278 sqlite3VdbeSerialGet((u8*)sqlite3CtypeMap, t, pDest);
91279 }else{
91280 if( len>db->aLimit[SQLITE_LIMIT_LENGTH0] ) goto too_big;
91281 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
91282 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
91283 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
91284 pDest->flags &= ~MEM_Ephem0x4000;
91285 }
91286 }
91287
91288op_column_out:
91289 UPDATE_MAX_BLOBSIZE(pDest);
91290 REGISTER_TRACE(pOp->p3, pDest);
91291 break;
91292
91293op_column_corrupt:
91294 if( aOp[0].p3>0 ){
91295 pOp = &aOp[aOp[0].p3-1];
91296 break;
91297 }else{
91298 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(91298);
91299 goto abort_due_to_error;
91300 }
91301}
91302
91303/* Opcode: TypeCheck P1 P2 P3 P4 *
91304** Synopsis: typecheck(r[P1@P2])
91305**
91306** Apply affinities to the range of P2 registers beginning with P1.
91307** Take the affinities from the Table object in P4. If any value
91308** cannot be coerced into the correct type, then raise an error.
91309**
91310** This opcode is similar to OP_Affinity except that this opcode
91311** forces the register type to the Table column type. This is used
91312** to implement "strict affinity".
91313**
91314** GENERATED ALWAYS AS ... STATIC columns are only checked if P3
91315** is zero. When P3 is non-zero, no type checking occurs for
91316** static generated columns. Virtual columns are computed at query time
91317** and so they are never checked.
91318**
91319** Preconditions:
91320**
91321** <ul>
91322** <li> P2 should be the number of non-virtual columns in the
91323** table of P4.
91324** <li> Table P4 should be a STRICT table.
91325** </ul>
91326**
91327** If any precondition is false, an assertion fault occurs.
91328*/
91329case OP_TypeCheck95: {
91330 Table *pTab;
91331 Column *aCol;
91332 int i;
91333
91334 assert( pOp->p4type==P4_TABLE )((void) (0));
91335 pTab = pOp->p4.pTab;
91336 assert( pTab->tabFlags & TF_Strict )((void) (0));
91337 assert( pTab->nNVCol==pOp->p2 )((void) (0));
91338 aCol = pTab->aCol;
91339 pIn1 = &aMem[pOp->p1];
91340 for(i=0; i<pTab->nCol; i++){
91341 if( aCol[i].colFlags & COLFLAG_GENERATED0x0060 ){
91342 if( aCol[i].colFlags & COLFLAG_VIRTUAL0x0020 ) continue;
91343 if( pOp->p3 ){ pIn1++; continue; }
91344 }
91345 assert( pIn1 < &aMem[pOp->p1+pOp->p2] )((void) (0));
91346 applyAffinity(pIn1, aCol[i].affinity, encoding);
91347 if( (pIn1->flags & MEM_Null0x0001)==0 ){
91348 switch( aCol[i].eCType ){
91349 case COLTYPE_BLOB2: {
91350 if( (pIn1->flags & MEM_Blob0x0010)==0 ) goto vdbe_type_error;
91351 break;
91352 }
91353 case COLTYPE_INTEGER4:
91354 case COLTYPE_INT3: {
91355 if( (pIn1->flags & MEM_Int0x0004)==0 ) goto vdbe_type_error;
91356 break;
91357 }
91358 case COLTYPE_TEXT6: {
91359 if( (pIn1->flags & MEM_Str0x0002)==0 ) goto vdbe_type_error;
91360 break;
91361 }
91362 case COLTYPE_REAL5: {
91363 testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_Real );
91364 testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_IntReal );
91365 if( pIn1->flags & MEM_Int0x0004 ){
91366 /* When applying REAL affinity, if the result is still an MEM_Int
91367 ** that will fit in 6 bytes, then change the type to MEM_IntReal
91368 ** so that we keep the high-resolution integer value but know that
91369 ** the type really wants to be REAL. */
91370 testcase( pIn1->u.i==140737488355328LL );
91371 testcase( pIn1->u.i==140737488355327LL );
91372 testcase( pIn1->u.i==-140737488355328LL );
91373 testcase( pIn1->u.i==-140737488355329LL );
91374 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL){
91375 pIn1->flags |= MEM_IntReal0x0020;
91376 pIn1->flags &= ~MEM_Int0x0004;
91377 }else{
91378 pIn1->u.r = (double)pIn1->u.i;
91379 pIn1->flags |= MEM_Real0x0008;
91380 pIn1->flags &= ~MEM_Int0x0004;
91381 }
91382 }else if( (pIn1->flags & (MEM_Real0x0008|MEM_IntReal0x0020))==0 ){
91383 goto vdbe_type_error;
91384 }
91385 break;
91386 }
91387 default: {
91388 /* COLTYPE_ANY. Accept anything. */
91389 break;
91390 }
91391 }
91392 }
91393 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
91394 pIn1++;
91395 }
91396 assert( pIn1 == &aMem[pOp->p1+pOp->p2] )((void) (0));
91397 break;
91398
91399vdbe_type_error:
91400 sqlite3VdbeError(p, "cannot store %s value in %s column %s.%s",
91401 vdbeMemTypeName(pIn1), sqlite3StdType[aCol[i].eCType-1],
91402 pTab->zName, aCol[i].zCnName);
91403 rc = SQLITE_CONSTRAINT_DATATYPE(19 |(12<<8));
91404 goto abort_due_to_error;
91405}
91406
91407/* Opcode: Affinity P1 P2 * P4 *
91408** Synopsis: affinity(r[P1@P2])
91409**
91410** Apply affinities to a range of P2 registers starting with P1.
91411**
91412** P4 is a string that is P2 characters long. The N-th character of the
91413** string indicates the column affinity that should be used for the N-th
91414** memory cell in the range.
91415*/
91416case OP_Affinity96: {
91417 const char *zAffinity; /* The affinity to be applied */
91418
91419 zAffinity = pOp->p4.z;
91420 assert( zAffinity!=0 )((void) (0));
91421 assert( pOp->p2>0 )((void) (0));
91422 assert( zAffinity[pOp->p2]==0 )((void) (0));
91423 pIn1 = &aMem[pOp->p1];
91424 while( 1 /*exit-by-break*/ ){
91425 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] )((void) (0));
91426 assert( zAffinity[0]==SQLITE_AFF_NONE || memIsValid(pIn1) )((void) (0));
91427 applyAffinity(pIn1, zAffinity[0], encoding);
91428 if( zAffinity[0]==SQLITE_AFF_REAL0x45 && (pIn1->flags & MEM_Int0x0004)!=0 ){
91429 /* When applying REAL affinity, if the result is still an MEM_Int
91430 ** that will fit in 6 bytes, then change the type to MEM_IntReal
91431 ** so that we keep the high-resolution integer value but know that
91432 ** the type really wants to be REAL. */
91433 testcase( pIn1->u.i==140737488355328LL );
91434 testcase( pIn1->u.i==140737488355327LL );
91435 testcase( pIn1->u.i==-140737488355328LL );
91436 testcase( pIn1->u.i==-140737488355329LL );
91437 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){
91438 pIn1->flags |= MEM_IntReal0x0020;
91439 pIn1->flags &= ~MEM_Int0x0004;
91440 }else{
91441 pIn1->u.r = (double)pIn1->u.i;
91442 pIn1->flags |= MEM_Real0x0008;
91443 pIn1->flags &= ~MEM_Int0x0004;
91444 }
91445 }
91446 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
91447 zAffinity++;
91448 if( zAffinity[0]==0 ) break;
91449 pIn1++;
91450 }
91451 break;
91452}
91453
91454/* Opcode: MakeRecord P1 P2 P3 P4 *
91455** Synopsis: r[P3]=mkrec(r[P1@P2])
91456**
91457** Convert P2 registers beginning with P1 into the [record format]
91458** use as a data record in a database table or as a key
91459** in an index. The OP_Column opcode can decode the record later.
91460**
91461** P4 may be a string that is P2 characters long. The N-th character of the
91462** string indicates the column affinity that should be used for the N-th
91463** field of the index key.
91464**
91465** The mapping from character to affinity is given by the SQLITE_AFF_
91466** macros defined in sqliteInt.h.
91467**
91468** If P4 is NULL then all index fields have the affinity BLOB.
91469**
91470** The meaning of P5 depends on whether or not the SQLITE_ENABLE_NULL_TRIM
91471** compile-time option is enabled:
91472**
91473** * If SQLITE_ENABLE_NULL_TRIM is enabled, then the P5 is the index
91474** of the right-most table that can be null-trimmed.
91475**
91476** * If SQLITE_ENABLE_NULL_TRIM is omitted, then P5 has the value
91477** OPFLAG_NOCHNG_MAGIC if the OP_MakeRecord opcode is allowed to
91478** accept no-change records with serial_type 10. This value is
91479** only used inside an assert() and does not affect the end result.
91480*/
91481case OP_MakeRecord97: {
91482 Mem *pRec; /* The new record */
91483 u64 nData; /* Number of bytes of data space */
91484 int nHdr; /* Number of bytes of header space */
91485 i64 nByte; /* Data space required for this record */
91486 i64 nZero; /* Number of zero bytes at the end of the record */
91487 int nVarint; /* Number of bytes in a varint */
91488 u32 serial_type; /* Type field */
91489 Mem *pData0; /* First field to be combined into the record */
91490 Mem *pLast; /* Last field of the record */
91491 int nField; /* Number of fields in the record */
91492 char *zAffinity; /* The affinity string for the record */
91493 u32 len; /* Length of a field */
91494 u8 *zHdr; /* Where to write next byte of the header */
91495 u8 *zPayload; /* Where to write next byte of the payload */
91496
91497 /* Assuming the record contains N fields, the record format looks
91498 ** like this:
91499 **
91500 ** ------------------------------------------------------------------------
91501 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
91502 ** ------------------------------------------------------------------------
91503 **
91504 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
91505 ** and so forth.
91506 **
91507 ** Each type field is a varint representing the serial type of the
91508 ** corresponding data element (see sqlite3VdbeSerialType()). The
91509 ** hdr-size field is also a varint which is the offset from the beginning
91510 ** of the record to data0.
91511 */
91512 nData = 0; /* Number of bytes of data space */
91513 nHdr = 0; /* Number of bytes of header space */
91514 nZero = 0; /* Number of zero bytes at the end of the record */
91515 nField = pOp->p1;
91516 zAffinity = pOp->p4.z;
91517 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
91518 pData0 = &aMem[nField];
91519 nField = pOp->p2;
91520 pLast = &pData0[nField-1];
91521
91522 /* Identify the output register */
91523 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 )((void) (0));
91524 pOut = &aMem[pOp->p3];
91525 memAboutToChange(p, pOut);
91526
91527 /* Apply the requested affinity to all inputs
91528 */
91529 assert( pData0<=pLast )((void) (0));
91530 if( zAffinity ){
91531 pRec = pData0;
91532 do{
91533 applyAffinity(pRec, zAffinity[0], encoding);
91534 if( zAffinity[0]==SQLITE_AFF_REAL0x45 && (pRec->flags & MEM_Int0x0004) ){
91535 pRec->flags |= MEM_IntReal0x0020;
91536 pRec->flags &= ~(MEM_Int0x0004);
91537 }
91538 REGISTER_TRACE((int)(pRec-aMem), pRec);
91539 zAffinity++;
91540 pRec++;
91541 assert( zAffinity[0]==0 || pRec<=pLast )((void) (0));
91542 }while( zAffinity[0] );
91543 }
91544
91545#ifdef SQLITE_ENABLE_NULL_TRIM
91546 /* NULLs can be safely trimmed from the end of the record, as long as
91547 ** as the schema format is 2 or more and none of the omitted columns
91548 ** have a non-NULL default value. Also, the record must be left with
91549 ** at least one field. If P5>0 then it will be one more than the
91550 ** index of the right-most column with a non-NULL default value */
91551 if( pOp->p5 ){
91552 while( (pLast->flags & MEM_Null0x0001)!=0 && nField>pOp->p5 ){
91553 pLast--;
91554 nField--;
91555 }
91556 }
91557#endif
91558
91559 /* Loop through the elements that will make up the record to figure
91560 ** out how much space is required for the new record. After this loop,
91561 ** the Mem.uTemp field of each term should hold the serial-type that will
91562 ** be used for that term in the generated record:
91563 **
91564 ** Mem.uTemp value type
91565 ** --------------- ---------------
91566 ** 0 NULL
91567 ** 1 1-byte signed integer
91568 ** 2 2-byte signed integer
91569 ** 3 3-byte signed integer
91570 ** 4 4-byte signed integer
91571 ** 5 6-byte signed integer
91572 ** 6 8-byte signed integer
91573 ** 7 IEEE float
91574 ** 8 Integer constant 0
91575 ** 9 Integer constant 1
91576 ** 10,11 reserved for expansion
91577 ** N>=12 and even BLOB
91578 ** N>=13 and odd text
91579 **
91580 ** The following additional values are computed:
91581 ** nHdr Number of bytes needed for the record header
91582 ** nData Number of bytes of data space needed for the record
91583 ** nZero Zero bytes at the end of the record
91584 */
91585 pRec = pLast;
91586 do{
91587 assert( memIsValid(pRec) )((void) (0));
91588 if( pRec->flags & MEM_Null0x0001 ){
91589 if( pRec->flags & MEM_Zero0x0400 ){
91590 /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
91591 ** table methods that never invoke sqlite3_result_xxxxx() while
91592 ** computing an unchanging column value in an UPDATE statement.
91593 ** Give such values a special internal-use-only serial-type of 10
91594 ** so that they can be passed through to xUpdate and have
91595 ** a true sqlite3_value_nochange(). */
91596#ifndef SQLITE_ENABLE_NULL_TRIM
91597 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB )((void) (0));
91598#endif
91599 pRec->uTemp = 10;
91600 }else{
91601 pRec->uTemp = 0;
91602 }
91603 nHdr++;
91604 }else if( pRec->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
91605 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
91606 i64 i = pRec->u.i;
91607 u64 uu;
91608 testcase( pRec->flags & MEM_Int );
91609 testcase( pRec->flags & MEM_IntReal );
91610 if( i<0 ){
91611 uu = ~i;
91612 }else{
91613 uu = i;
91614 }
91615 nHdr++;
91616 testcase( uu==127 ); testcase( uu==128 );
91617 testcase( uu==32767 ); testcase( uu==32768 );
91618 testcase( uu==8388607 ); testcase( uu==8388608 );
91619 testcase( uu==2147483647 ); testcase( uu==2147483648LL );
91620 testcase( uu==140737488355327LL ); testcase( uu==140737488355328LL );
91621 if( uu<=127 ){
91622 if( (i&1)==i && p->minWriteFileFormat>=4 ){
91623 pRec->uTemp = 8+(u32)uu;
91624 }else{
91625 nData++;
91626 pRec->uTemp = 1;
91627 }
91628 }else if( uu<=32767 ){
91629 nData += 2;
91630 pRec->uTemp = 2;
91631 }else if( uu<=8388607 ){
91632 nData += 3;
91633 pRec->uTemp = 3;
91634 }else if( uu<=2147483647 ){
91635 nData += 4;
91636 pRec->uTemp = 4;
91637 }else if( uu<=140737488355327LL ){
91638 nData += 6;
91639 pRec->uTemp = 5;
91640 }else{
91641 nData += 8;
91642 if( pRec->flags & MEM_IntReal0x0020 ){
91643 /* If the value is IntReal and is going to take up 8 bytes to store
91644 ** as an integer, then we might as well make it an 8-byte floating
91645 ** point value */
91646 pRec->u.r = (double)pRec->u.i;
91647 pRec->flags &= ~MEM_IntReal0x0020;
91648 pRec->flags |= MEM_Real0x0008;
91649 pRec->uTemp = 7;
91650 }else{
91651 pRec->uTemp = 6;
91652 }
91653 }
91654 }else if( pRec->flags & MEM_Real0x0008 ){
91655 nHdr++;
91656 nData += 8;
91657 pRec->uTemp = 7;
91658 }else{
91659 assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) )((void) (0));
91660 assert( pRec->n>=0 )((void) (0));
91661 len = (u32)pRec->n;
91662 serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str0x0002)!=0);
91663 if( pRec->flags & MEM_Zero0x0400 ){
91664 serial_type += pRec->u.nZero*2;
91665 if( nData ){
91666 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
91667 len += pRec->u.nZero;
91668 }else{
91669 nZero += pRec->u.nZero;
91670 }
91671 }
91672 nData += len;
91673 nHdr += sqlite3VarintLen(serial_type);
91674 pRec->uTemp = serial_type;
91675 }
91676 if( pRec==pData0 ) break;
91677 pRec--;
91678 }while(1);
91679
91680 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
91681 ** which determines the total number of bytes in the header. The varint
91682 ** value is the size of the header in bytes including the size varint
91683 ** itself. */
91684 testcase( nHdr==126 );
91685 testcase( nHdr==127 );
91686 if( nHdr<=126 ){
91687 /* The common case */
91688 nHdr += 1;
91689 }else{
91690 /* Rare case of a really large header */
91691 nVarint = sqlite3VarintLen(nHdr);
91692 nHdr += nVarint;
91693 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
91694 }
91695 nByte = nHdr+nData;
91696
91697 /* Make sure the output register has a buffer large enough to store
91698 ** the new record. The output register (pOp->p3) is not allowed to
91699 ** be one of the input registers (because the following call to
91700 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
91701 */
91702 if( nByte+nZero<=pOut->szMalloc ){
91703 /* The output register is already large enough to hold the record.
91704 ** No error checks or buffer enlargement is required */
91705 pOut->z = pOut->zMalloc;
91706 }else{
91707 /* Need to make sure that the output is not too big and then enlarge
91708 ** the output register to hold the full result */
91709 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH0] ){
91710 goto too_big;
91711 }
91712 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
91713 goto no_mem;
91714 }
91715 }
91716 pOut->n = (int)nByte;
91717 pOut->flags = MEM_Blob0x0010;
91718 if( nZero ){
91719 pOut->u.nZero = nZero;
91720 pOut->flags |= MEM_Zero0x0400;
91721 }
91722 UPDATE_MAX_BLOBSIZE(pOut);
91723 zHdr = (u8 *)pOut->z;
91724 zPayload = zHdr + nHdr;
91725
91726 /* Write the record */
91727 if( nHdr<0x80 ){
91728 *(zHdr++) = nHdr;
91729 }else{
91730 zHdr += sqlite3PutVarint(zHdr,nHdr);
91731 }
91732 assert( pData0<=pLast )((void) (0));
91733 pRec = pData0;
91734 while( 1 /*exit-by-break*/ ){
91735 serial_type = pRec->uTemp;
91736 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
91737 ** additional varints, one per column.
91738 ** EVIDENCE-OF: R-64536-51728 The values for each column in the record
91739 ** immediately follow the header. */
91740 if( serial_type<=7 ){
91741 *(zHdr++) = serial_type;
91742 if( serial_type==0 ){
91743 /* NULL value. No change in zPayload */
91744 }else{
91745 u64 v;
91746 u32 i;
91747 if( serial_type==7 ){
91748 assert( sizeof(v)==sizeof(pRec->u.r) )((void) (0));
91749 memcpy(&v, &pRec->u.r, sizeof(v));
91750 swapMixedEndianFloat(v);
91751 }else{
91752 v = pRec->u.i;
91753 }
91754 len = i = sqlite3SmallTypeSizes[serial_type];
91755 assert( i>0 )((void) (0));
91756 while( 1 /*exit-by-break*/ ){
91757 zPayload[--i] = (u8)(v&0xFF);
91758 if( i==0 ) break;
91759 v >>= 8;
91760 }
91761 zPayload += len;
91762 }
91763 }else if( serial_type<0x80 ){
91764 *(zHdr++) = serial_type;
91765 if( serial_type>=14 && pRec->n>0 ){
91766 assert( pRec->z!=0 )((void) (0));
91767 memcpy(zPayload, pRec->z, pRec->n);
91768 zPayload += pRec->n;
91769 }
91770 }else{
91771 zHdr += sqlite3PutVarint(zHdr, serial_type);
91772 if( pRec->n ){
91773 assert( pRec->z!=0 )((void) (0));
91774 memcpy(zPayload, pRec->z, pRec->n);
91775 zPayload += pRec->n;
91776 }
91777 }
91778 if( pRec==pLast ) break;
91779 pRec++;
91780 }
91781 assert( nHdr==(int)(zHdr - (u8*)pOut->z) )((void) (0));
91782 assert( nByte==(int)(zPayload - (u8*)pOut->z) )((void) (0));
91783
91784 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
91785 REGISTER_TRACE(pOp->p3, pOut);
91786 break;
91787}
91788
91789/* Opcode: Count P1 P2 P3 * *
91790** Synopsis: r[P2]=count()
91791**
91792** Store the number of entries (an integer value) in the table or index
91793** opened by cursor P1 in register P2.
91794**
91795** If P3==0, then an exact count is obtained, which involves visiting
91796** every btree page of the table. But if P3 is non-zero, an estimate
91797** is returned based on the current cursor position.
91798*/
91799case OP_Count98: { /* out2 */
91800 i64 nEntry;
91801 BtCursor *pCrsr;
91802
91803 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE )((void) (0));
91804 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
91805 assert( pCrsr )((void) (0));
91806 if( pOp->p3 ){
91807 nEntry = sqlite3BtreeRowCountEst(pCrsr);
91808 }else{
91809 nEntry = 0; /* Not needed. Only used to silence a warning. */
91810 rc = sqlite3BtreeCount(db, pCrsr, &nEntry);
91811 if( rc ) goto abort_due_to_error;
91812 }
91813 pOut = out2Prerelease(p, pOp);
91814 pOut->u.i = nEntry;
91815 goto check_for_interrupt;
91816}
91817
91818/* Opcode: Savepoint P1 * * P4 *
91819**
91820** Open, release or rollback the savepoint named by parameter P4, depending
91821** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN).
91822** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE).
91823** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK).
91824*/
91825case OP_Savepoint0: {
91826 int p1; /* Value of P1 operand */
91827 char *zName; /* Name of savepoint */
91828 int nName;
91829 Savepoint *pNew;
91830 Savepoint *pSavepoint;
91831 Savepoint *pTmp;
91832 int iSavepoint;
91833 int ii;
91834
91835 p1 = pOp->p1;
91836 zName = pOp->p4.z;
91837
91838 /* Assert that the p1 parameter is valid. Also that if there is no open
91839 ** transaction, then there cannot be any savepoints.
91840 */
91841 assert( db->pSavepoint==0 || db->autoCommit==0 )((void) (0));
91842 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK )((void) (0));
91843 assert( db->pSavepoint || db->isTransactionSavepoint==0 )((void) (0));
91844 assert( checkSavepointCount(db) )((void) (0));
91845 assert( p->bIsReader )((void) (0));
91846
91847 if( p1==SAVEPOINT_BEGIN0 ){
91848 if( db->nVdbeWrite>0 ){
91849 /* A new savepoint cannot be created if there are active write
91850 ** statements (i.e. open read/write incremental blob handles).
91851 */
91852 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
91853 rc = SQLITE_BUSY5;
91854 }else{
91855 nName = sqlite3Strlen30(zName);
91856
91857#ifndef SQLITE_OMIT_VIRTUALTABLE
91858 /* This call is Ok even if this savepoint is actually a transaction
91859 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
91860 ** If this is a transaction savepoint being opened, it is guaranteed
91861 ** that the db->aVTrans[] array is empty. */
91862 assert( db->autoCommit==0 || db->nVTrans==0 )((void) (0));
91863 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN0,
91864 db->nStatement+db->nSavepoint);
91865 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
91866#endif
91867
91868 /* Create a new savepoint structure. */
91869 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
91870 if( pNew ){
91871 pNew->zName = (char *)&pNew[1];
91872 memcpy(pNew->zName, zName, nName+1);
91873
91874 /* If there is no open transaction, then mark this as a special
91875 ** "transaction savepoint". */
91876 if( db->autoCommit ){
91877 db->autoCommit = 0;
91878 db->isTransactionSavepoint = 1;
91879 }else{
91880 db->nSavepoint++;
91881 }
91882
91883 /* Link the new savepoint into the database handle's list. */
91884 pNew->pNext = db->pSavepoint;
91885 db->pSavepoint = pNew;
91886 pNew->nDeferredCons = db->nDeferredCons;
91887 pNew->nDeferredImmCons = db->nDeferredImmCons;
91888 }
91889 }
91890 }else{
91891 assert( p1==SAVEPOINT_RELEASE || p1==SAVEPOINT_ROLLBACK )((void) (0));
91892 iSavepoint = 0;
91893
91894 /* Find the named savepoint. If there is no such savepoint, then an
91895 ** an error is returned to the user. */
91896 for(
91897 pSavepoint = db->pSavepoint;
91898 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
91899 pSavepoint = pSavepoint->pNext
91900 ){
91901 iSavepoint++;
91902 }
91903 if( !pSavepoint ){
91904 sqlite3VdbeError(p, "no such savepoint: %s", zName);
91905 rc = SQLITE_ERROR1;
91906 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE1 ){
91907 /* It is not possible to release (commit) a savepoint if there are
91908 ** active write statements.
91909 */
91910 sqlite3VdbeError(p, "cannot release savepoint - "
91911 "SQL statements in progress");
91912 rc = SQLITE_BUSY5;
91913 }else{
91914
91915 /* Determine whether or not this is a transaction savepoint. If so,
91916 ** and this is a RELEASE command, then the current transaction
91917 ** is committed.
91918 */
91919 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
91920 if( isTransaction && p1==SAVEPOINT_RELEASE1 ){
91921 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK0 ){
91922 goto vdbe_return;
91923 }
91924 db->autoCommit = 1;
91925 if( sqlite3VdbeHalt(p)==SQLITE_BUSY5 ){
91926 p->pc = (int)(pOp - aOp);
91927 db->autoCommit = 0;
91928 p->rc = rc = SQLITE_BUSY5;
91929 goto vdbe_return;
91930 }
91931 rc = p->rc;
91932 if( rc ){
91933 db->autoCommit = 0;
91934 }else{
91935 db->isTransactionSavepoint = 0;
91936 }
91937 }else{
91938 int isSchemaChange;
91939 iSavepoint = db->nSavepoint - iSavepoint - 1;
91940 if( p1==SAVEPOINT_ROLLBACK2 ){
91941 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange0x0001)!=0;
91942 for(ii=0; ii<db->nDb; ii++){
91943 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
91944 SQLITE_ABORT_ROLLBACK(4 | (2<<8)),
91945 isSchemaChange==0);
91946 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
91947 }
91948 }else{
91949 assert( p1==SAVEPOINT_RELEASE )((void) (0));
91950 isSchemaChange = 0;
91951 }
91952 for(ii=0; ii<db->nDb; ii++){
91953 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
91954 if( rc!=SQLITE_OK0 ){
91955 goto abort_due_to_error;
91956 }
91957 }
91958 if( isSchemaChange ){
91959 sqlite3ExpirePreparedStatements(db, 0);
91960 sqlite3ResetAllSchemasOfConnection(db);
91961 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
91962 }
91963 }
91964 if( rc ) goto abort_due_to_error;
91965
91966 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
91967 ** savepoints nested inside of the savepoint being operated on. */
91968 while( db->pSavepoint!=pSavepoint ){
91969 pTmp = db->pSavepoint;
91970 db->pSavepoint = pTmp->pNext;
91971 sqlite3DbFree(db, pTmp);
91972 db->nSavepoint--;
91973 }
91974
91975 /* If it is a RELEASE, then destroy the savepoint being operated on
91976 ** too. If it is a ROLLBACK TO, then set the number of deferred
91977 ** constraint violations present in the database to the value stored
91978 ** when the savepoint was created. */
91979 if( p1==SAVEPOINT_RELEASE1 ){
91980 assert( pSavepoint==db->pSavepoint )((void) (0));
91981 db->pSavepoint = pSavepoint->pNext;
91982 sqlite3DbFree(db, pSavepoint);
91983 if( !isTransaction ){
91984 db->nSavepoint--;
91985 }
91986 }else{
91987 assert( p1==SAVEPOINT_ROLLBACK )((void) (0));
91988 db->nDeferredCons = pSavepoint->nDeferredCons;
91989 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
91990 }
91991
91992 if( !isTransaction || p1==SAVEPOINT_ROLLBACK2 ){
91993 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
91994 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
91995 }
91996 }
91997 }
91998 if( rc ) goto abort_due_to_error;
91999 if( p->eVdbeState==VDBE_HALT_STATE3 ){
92000 rc = SQLITE_DONE101;
92001 goto vdbe_return;
92002 }
92003 break;
92004}
92005
92006/* Opcode: AutoCommit P1 P2 * * *
92007**
92008** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
92009** back any currently active btree transactions. If there are any active
92010** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
92011** there are active writing VMs or active VMs that use shared cache.
92012**
92013** This instruction causes the VM to halt.
92014*/
92015case OP_AutoCommit1: {
92016 int desiredAutoCommit;
92017 int iRollback;
92018
92019 desiredAutoCommit = pOp->p1;
92020 iRollback = pOp->p2;
92021 assert( desiredAutoCommit==1 || desiredAutoCommit==0 )((void) (0));
92022 assert( desiredAutoCommit==1 || iRollback==0 )((void) (0));
92023 assert( db->nVdbeActive>0 )((void) (0)); /* At least this one VM is active */
92024 assert( p->bIsReader )((void) (0));
92025
92026 if( desiredAutoCommit!=db->autoCommit ){
92027 if( iRollback ){
92028 assert( desiredAutoCommit==1 )((void) (0));
92029 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK(4 | (2<<8)));
92030 db->autoCommit = 1;
92031 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
92032 /* If this instruction implements a COMMIT and other VMs are writing
92033 ** return an error indicating that the other VMs must complete first.
92034 */
92035 sqlite3VdbeError(p, "cannot commit transaction - "
92036 "SQL statements in progress");
92037 rc = SQLITE_BUSY5;
92038 goto abort_due_to_error;
92039 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK0 ){
92040 goto vdbe_return;
92041 }else{
92042 db->autoCommit = (u8)desiredAutoCommit;
92043 }
92044 if( sqlite3VdbeHalt(p)==SQLITE_BUSY5 ){
92045 p->pc = (int)(pOp - aOp);
92046 db->autoCommit = (u8)(1-desiredAutoCommit);
92047 p->rc = rc = SQLITE_BUSY5;
92048 goto vdbe_return;
92049 }
92050 sqlite3CloseSavepoints(db);
92051 if( p->rc==SQLITE_OK0 ){
92052 rc = SQLITE_DONE101;
92053 }else{
92054 rc = SQLITE_ERROR1;
92055 }
92056 goto vdbe_return;
92057 }else{
92058 sqlite3VdbeError(p,
92059 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
92060 (iRollback)?"cannot rollback - no transaction is active":
92061 "cannot commit - no transaction is active"));
92062
92063 rc = SQLITE_ERROR1;
92064 goto abort_due_to_error;
92065 }
92066 /*NOTREACHED*/ assert(0)((void) (0));
92067}
92068
92069/* Opcode: Transaction P1 P2 P3 P4 P5
92070**
92071** Begin a transaction on database P1 if a transaction is not already
92072** active.
92073** If P2 is non-zero, then a write-transaction is started, or if a
92074** read-transaction is already active, it is upgraded to a write-transaction.
92075** If P2 is zero, then a read-transaction is started. If P2 is 2 or more
92076** then an exclusive transaction is started.
92077**
92078** P1 is the index of the database file on which the transaction is
92079** started. Index 0 is the main database file and index 1 is the
92080** file used for temporary tables. Indices of 2 or more are used for
92081** attached databases.
92082**
92083** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
92084** true (this flag is set if the Vdbe may modify more than one row and may
92085** throw an ABORT exception), a statement transaction may also be opened.
92086** More specifically, a statement transaction is opened iff the database
92087** connection is currently not in autocommit mode, or if there are other
92088** active statements. A statement transaction allows the changes made by this
92089** VDBE to be rolled back after an error without having to roll back the
92090** entire transaction. If no error is encountered, the statement transaction
92091** will automatically commit when the VDBE halts.
92092**
92093** If P5!=0 then this opcode also checks the schema cookie against P3
92094** and the schema generation counter against P4.
92095** The cookie changes its value whenever the database schema changes.
92096** This operation is used to detect when that the cookie has changed
92097** and that the current process needs to reread the schema. If the schema
92098** cookie in P3 differs from the schema cookie in the database header or
92099** if the schema generation counter in P4 differs from the current
92100** generation counter, then an SQLITE_SCHEMA error is raised and execution
92101** halts. The sqlite3_step() wrapper function might then reprepare the
92102** statement and rerun it from the beginning.
92103*/
92104case OP_Transaction2: {
92105 Btree *pBt;
92106 Db *pDb;
92107 int iMeta = 0;
92108
92109 assert( p->bIsReader )((void) (0));
92110 assert( p->readOnly==0 || pOp->p2==0 )((void) (0));
92111 assert( pOp->p2>=0 && pOp->p2<=2 )((void) (0));
92112 assert( pOp->p1>=0 && pOp->p1<db->nDb )((void) (0));
92113 assert( DbMaskTest(p->btreeMask, pOp->p1) )((void) (0));
92114 assert( rc==SQLITE_OK )((void) (0));
92115 if( pOp->p2 && (db->flags & (SQLITE_QueryOnly0x00100000|SQLITE_CorruptRdOnly((u64)(0x00002)<<32)))!=0 ){
92116 if( db->flags & SQLITE_QueryOnly0x00100000 ){
92117 /* Writes prohibited by the "PRAGMA query_only=TRUE" statement */
92118 rc = SQLITE_READONLY8;
92119 }else{
92120 /* Writes prohibited due to a prior SQLITE_CORRUPT in the current
92121 ** transaction */
92122 rc = SQLITE_CORRUPT11;
92123 }
92124 goto abort_due_to_error;
92125 }
92126 pDb = &db->aDb[pOp->p1];
92127 pBt = pDb->pBt;
92128
92129 if( pBt ){
92130 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
92131 testcase( rc==SQLITE_BUSY_SNAPSHOT );
92132 testcase( rc==SQLITE_BUSY_RECOVERY );
92133 if( rc!=SQLITE_OK0 ){
92134 if( (rc&0xff)==SQLITE_BUSY5 ){
92135 p->pc = (int)(pOp - aOp);
92136 p->rc = rc;
92137 goto vdbe_return;
92138 }
92139 goto abort_due_to_error;
92140 }
92141
92142 if( p->usesStmtJournal
92143 && pOp->p2
92144 && (db->autoCommit==0 || db->nVdbeRead>1)
92145 ){
92146 assert( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE )((void) (0));
92147 if( p->iStatement==0 ){
92148 assert( db->nStatement>=0 && db->nSavepoint>=0 )((void) (0));
92149 db->nStatement++;
92150 p->iStatement = db->nSavepoint + db->nStatement;
92151 }
92152
92153 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN0, p->iStatement-1);
92154 if( rc==SQLITE_OK0 ){
92155 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
92156 }
92157
92158 /* Store the current value of the database handles deferred constraint
92159 ** counter. If the statement transaction needs to be rolled back,
92160 ** the value of this counter needs to be restored too. */
92161 p->nStmtDefCons = db->nDeferredCons;
92162 p->nStmtDefImmCons = db->nDeferredImmCons;
92163 }
92164 }
92165 assert( pOp->p5==0 || pOp->p4type==P4_INT32 )((void) (0));
92166 if( rc==SQLITE_OK0
92167 && pOp->p5
92168 && (iMeta!=pOp->p3 || pDb->pSchema->iGeneration!=pOp->p4.i)
92169 ){
92170 /*
92171 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
92172 ** version is checked to ensure that the schema has not changed since the
92173 ** SQL statement was prepared.
92174 */
92175 sqlite3DbFree(db, p->zErrMsg);
92176 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
92177 /* If the schema-cookie from the database file matches the cookie
92178 ** stored with the in-memory representation of the schema, do
92179 ** not reload the schema from the database file.
92180 **
92181 ** If virtual-tables are in use, this is not just an optimization.
92182 ** Often, v-tables store their data in other SQLite tables, which
92183 ** are queried from within xNext() and other v-table methods using
92184 ** prepared queries. If such a query is out-of-date, we do not want to
92185 ** discard the database schema, as the user code implementing the
92186 ** v-table would have to be ready for the sqlite3_vtab structure itself
92187 ** to be invalidated whenever sqlite3_step() is called from within
92188 ** a v-table method.
92189 */
92190 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
92191 sqlite3ResetOneSchema(db, pOp->p1);
92192 }
92193 p->expired = 1;
92194 rc = SQLITE_SCHEMA17;
92195
92196 /* Set changeCntOn to 0 to prevent the value returned by sqlite3_changes()
92197 ** from being modified in sqlite3VdbeHalt(). If this statement is
92198 ** reprepared, changeCntOn will be set again. */
92199 p->changeCntOn = 0;
92200 }
92201 if( rc ) goto abort_due_to_error;
92202 break;
92203}
92204
92205/* Opcode: ReadCookie P1 P2 P3 * *
92206**
92207** Read cookie number P3 from database P1 and write it into register P2.
92208** P3==1 is the schema version. P3==2 is the database format.
92209** P3==3 is the recommended pager cache size, and so forth. P1==0 is
92210** the main database file and P1==1 is the database file used to store
92211** temporary tables.
92212**
92213** There must be a read-lock on the database (either a transaction
92214** must be started or there must be an open cursor) before
92215** executing this instruction.
92216*/
92217case OP_ReadCookie99: { /* out2 */
92218 int iMeta;
92219 int iDb;
92220 int iCookie;
92221
92222 assert( p->bIsReader )((void) (0));
92223 iDb = pOp->p1;
92224 iCookie = pOp->p3;
92225 assert( pOp->p3<SQLITE_N_BTREE_META )((void) (0));
92226 assert( iDb>=0 && iDb<db->nDb )((void) (0));
92227 assert( db->aDb[iDb].pBt!=0 )((void) (0));
92228 assert( DbMaskTest(p->btreeMask, iDb) )((void) (0));
92229
92230 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
92231 pOut = out2Prerelease(p, pOp);
92232 pOut->u.i = iMeta;
92233 break;
92234}
92235
92236/* Opcode: SetCookie P1 P2 P3 * P5
92237**
92238** Write the integer value P3 into cookie number P2 of database P1.
92239** P2==1 is the schema version. P2==2 is the database format.
92240** P2==3 is the recommended pager cache
92241** size, and so forth. P1==0 is the main database file and P1==1 is the
92242** database file used to store temporary tables.
92243**
92244** A transaction must be started before executing this opcode.
92245**
92246** If P2 is the SCHEMA_VERSION cookie (cookie number 1) then the internal
92247** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement
92248** has P5 set to 1, so that the internal schema version will be different
92249** from the database schema version, resulting in a schema reset.
92250*/
92251case OP_SetCookie100: {
92252 Db *pDb;
92253
92254 sqlite3VdbeIncrWriteCounter(p, 0);
92255 assert( pOp->p2<SQLITE_N_BTREE_META )((void) (0));
92256 assert( pOp->p1>=0 && pOp->p1<db->nDb )((void) (0));
92257 assert( DbMaskTest(p->btreeMask, pOp->p1) )((void) (0));
92258 assert( p->readOnly==0 )((void) (0));
92259 pDb = &db->aDb[pOp->p1];
92260 assert( pDb->pBt!=0 )((void) (0));
92261 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) )((void) (0));
92262 /* See note about index shifting on OP_ReadCookie */
92263 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
92264 if( pOp->p2==BTREE_SCHEMA_VERSION1 ){
92265 /* When the schema cookie changes, record the new cookie internally */
92266 *(u32*)&pDb->pSchema->schema_cookie = *(u32*)&pOp->p3 - pOp->p5;
92267 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
92268 sqlite3FkClearTriggerCache(db, pOp->p1);
92269 }else if( pOp->p2==BTREE_FILE_FORMAT2 ){
92270 /* Record changes in the file format */
92271 pDb->pSchema->file_format = pOp->p3;
92272 }
92273 if( pOp->p1==1 ){
92274 /* Invalidate all prepared statements whenever the TEMP database
92275 ** schema is changed. Ticket #1644 */
92276 sqlite3ExpirePreparedStatements(db, 0);
92277 p->expired = 0;
92278 }
92279 if( rc ) goto abort_due_to_error;
92280 break;
92281}
92282
92283/* Opcode: OpenRead P1 P2 P3 P4 P5
92284** Synopsis: root=P2 iDb=P3
92285**
92286** Open a read-only cursor for the database table whose root page is
92287** P2 in a database file. The database file is determined by P3.
92288** P3==0 means the main database, P3==1 means the database used for
92289** temporary tables, and P3>1 means used the corresponding attached
92290** database. Give the new cursor an identifier of P1. The P1
92291** values need not be contiguous but all P1 values should be small integers.
92292** It is an error for P1 to be negative.
92293**
92294** Allowed P5 bits:
92295** <ul>
92296** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
92297** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
92298** of OP_SeekLE/OP_IdxLT)
92299** </ul>
92300**
92301** The P4 value may be either an integer (P4_INT32) or a pointer to
92302** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
92303** object, then table being opened must be an [index b-tree] where the
92304** KeyInfo object defines the content and collating
92305** sequence of that index b-tree. Otherwise, if P4 is an integer
92306** value, then the table being opened must be a [table b-tree] with a
92307** number of columns no less than the value of P4.
92308**
92309** See also: OpenWrite, ReopenIdx
92310*/
92311/* Opcode: ReopenIdx P1 P2 P3 P4 P5
92312** Synopsis: root=P2 iDb=P3
92313**
92314** The ReopenIdx opcode works like OP_OpenRead except that it first
92315** checks to see if the cursor on P1 is already open on the same
92316** b-tree and if it is this opcode becomes a no-op. In other words,
92317** if the cursor is already open, do not reopen it.
92318**
92319** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ
92320** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must
92321** be the same as every other ReopenIdx or OpenRead for the same cursor
92322** number.
92323**
92324** Allowed P5 bits:
92325** <ul>
92326** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
92327** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
92328** of OP_SeekLE/OP_IdxLT)
92329** </ul>
92330**
92331** See also: OP_OpenRead, OP_OpenWrite
92332*/
92333/* Opcode: OpenWrite P1 P2 P3 P4 P5
92334** Synopsis: root=P2 iDb=P3
92335**
92336** Open a read/write cursor named P1 on the table or index whose root
92337** page is P2 (or whose root page is held in register P2 if the
92338** OPFLAG_P2ISREG bit is set in P5 - see below).
92339**
92340** The P4 value may be either an integer (P4_INT32) or a pointer to
92341** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
92342** object, then table being opened must be an [index b-tree] where the
92343** KeyInfo object defines the content and collating
92344** sequence of that index b-tree. Otherwise, if P4 is an integer
92345** value, then the table being opened must be a [table b-tree] with a
92346** number of columns no less than the value of P4.
92347**
92348** Allowed P5 bits:
92349** <ul>
92350** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
92351** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
92352** of OP_SeekLE/OP_IdxLT)
92353** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
92354** and subsequently delete entries in an index btree. This is a
92355** hint to the storage engine that the storage engine is allowed to
92356** ignore. The hint is not used by the official SQLite b*tree storage
92357** engine, but is used by COMDB2.
92358** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2
92359** as the root page, not the value of P2 itself.
92360** </ul>
92361**
92362** This instruction works like OpenRead except that it opens the cursor
92363** in read/write mode.
92364**
92365** See also: OP_OpenRead, OP_ReopenIdx
92366*/
92367case OP_ReopenIdx101: {
92368 int nField;
92369 KeyInfo *pKeyInfo;
92370 u32 p2;
92371 int iDb;
92372 int wrFlag;
92373 Btree *pX;
92374 VdbeCursor *pCur;
92375 Db *pDb;
92376
92377 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ )((void) (0));
92378 assert( pOp->p4type==P4_KEYINFO )((void) (0));
92379 pCur = p->apCsr[pOp->p1];
92380 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
92381 assert( pCur->iDb==pOp->p3 )((void) (0)); /* Guaranteed by the code generator */
92382 assert( pCur->eCurType==CURTYPE_BTREE )((void) (0));
92383 sqlite3BtreeClearCursor(pCur->uc.pCursor);
92384 goto open_cursor_set_hints;
92385 }
92386 /* If the cursor is not currently open or is open on a different
92387 ** index, then fall through into OP_OpenRead to force a reopen */
92388case OP_OpenRead112:
92389case OP_OpenWrite113:
92390
92391 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ )((void) (0));
92392 assert( p->bIsReader )((void) (0));
92393 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx((void) (0))
92394 || p->readOnly==0 )((void) (0));
92395
92396 if( p->expired==1 ){
92397 rc = SQLITE_ABORT_ROLLBACK(4 | (2<<8));
92398 goto abort_due_to_error;
92399 }
92400
92401 nField = 0;
92402 pKeyInfo = 0;
92403 p2 = (u32)pOp->p2;
92404 iDb = pOp->p3;
92405 assert( iDb>=0 && iDb<db->nDb )((void) (0));
92406 assert( DbMaskTest(p->btreeMask, iDb) )((void) (0));
92407 pDb = &db->aDb[iDb];
92408 pX = pDb->pBt;
92409 assert( pX!=0 )((void) (0));
92410 if( pOp->opcode==OP_OpenWrite113 ){
92411 assert( OPFLAG_FORDELETE==BTREE_FORDELETE )((void) (0));
92412 wrFlag = BTREE_WRCSR0x00000004 | (pOp->p5 & OPFLAG_FORDELETE0x08);
92413 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
92414 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
92415 p->minWriteFileFormat = pDb->pSchema->file_format;
92416 }
92417 }else{
92418 wrFlag = 0;
92419 }
92420 if( pOp->p5 & OPFLAG_P2ISREG0x10 ){
92421 assert( p2>0 )((void) (0));
92422 assert( p2<=(u32)(p->nMem+1 - p->nCursor) )((void) (0));
92423 assert( pOp->opcode==OP_OpenWrite )((void) (0));
92424 pIn2 = &aMem[p2];
92425 assert( memIsValid(pIn2) )((void) (0));
92426 assert( (pIn2->flags & MEM_Int)!=0 )((void) (0));
92427 sqlite3VdbeMemIntegerify(pIn2);
92428 p2 = (int)pIn2->u.i;
92429 /* The p2 value always comes from a prior OP_CreateBtree opcode and
92430 ** that opcode will always set the p2 value to 2 or more or else fail.
92431 ** If there were a failure, the prepared statement would have halted
92432 ** before reaching this instruction. */
92433 assert( p2>=2 )((void) (0));
92434 }
92435 if( pOp->p4type==P4_KEYINFO(-8) ){
92436 pKeyInfo = pOp->p4.pKeyInfo;
92437 assert( pKeyInfo->enc==ENC(db) )((void) (0));
92438 assert( pKeyInfo->db==db )((void) (0));
92439 nField = pKeyInfo->nAllField;
92440 }else if( pOp->p4type==P4_INT32(-3) ){
92441 nField = pOp->p4.i;
92442 }
92443 assert( pOp->p1>=0 )((void) (0));
92444 assert( nField>=0 )((void) (0));
92445 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
92446 pCur = allocateCursor(p, pOp->p1, nField, CURTYPE_BTREE0);
92447 if( pCur==0 ) goto no_mem;
92448 pCur->iDb = iDb;
92449 pCur->nullRow = 1;
92450 pCur->isOrdered = 1;
92451 pCur->pgnoRoot = p2;
92452#ifdef SQLITE_DEBUG
92453 pCur->wrFlag = wrFlag;
92454#endif
92455 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
92456 pCur->pKeyInfo = pKeyInfo;
92457 /* Set the VdbeCursor.isTable variable. Previous versions of
92458 ** SQLite used to check if the root-page flags were sane at this point
92459 ** and report database corruption if they were not, but this check has
92460 ** since moved into the btree layer. */
92461 pCur->isTable = pOp->p4type!=P4_KEYINFO(-8);
92462
92463open_cursor_set_hints:
92464 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD )((void) (0));
92465 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ )((void) (0));
92466 testcase( pOp->p5 & OPFLAG_BULKCSR );
92467 testcase( pOp->p2 & OPFLAG_SEEKEQ );
92468 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
92469 (pOp->p5 & (OPFLAG_BULKCSR0x01|OPFLAG_SEEKEQ0x02)));
92470 if( rc ) goto abort_due_to_error;
92471 break;
92472}
92473
92474/* Opcode: OpenDup P1 P2 * * *
92475**
92476** Open a new cursor P1 that points to the same ephemeral table as
92477** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral
92478** opcode. Only ephemeral cursors may be duplicated.
92479**
92480** Duplicate ephemeral cursors are used for self-joins of materialized views.
92481*/
92482case OP_OpenDup115: {
92483 VdbeCursor *pOrig; /* The original cursor to be duplicated */
92484 VdbeCursor *pCx; /* The new cursor */
92485
92486 pOrig = p->apCsr[pOp->p2];
92487 assert( pOrig )((void) (0));
92488 assert( pOrig->isEphemeral )((void) (0)); /* Only ephemeral cursors can be duplicated */
92489
92490 pCx = allocateCursor(p, pOp->p1, pOrig->nField, CURTYPE_BTREE0);
92491 if( pCx==0 ) goto no_mem;
92492 pCx->nullRow = 1;
92493 pCx->isEphemeral = 1;
92494 pCx->pKeyInfo = pOrig->pKeyInfo;
92495 pCx->isTable = pOrig->isTable;
92496 pCx->pgnoRoot = pOrig->pgnoRoot;
92497 pCx->isOrdered = pOrig->isOrdered;
92498 pCx->ub.pBtx = pOrig->ub.pBtx;
92499 pCx->noReuse = 1;
92500 pOrig->noReuse = 1;
92501 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR0x00000004,
92502 pCx->pKeyInfo, pCx->uc.pCursor);
92503 /* The sqlite3BtreeCursor() routine can only fail for the first cursor
92504 ** opened for a database. Since there is already an open cursor when this
92505 ** opcode is run, the sqlite3BtreeCursor() cannot fail */
92506 assert( rc==SQLITE_OK )((void) (0));
92507 break;
92508}
92509
92510
92511/* Opcode: OpenEphemeral P1 P2 P3 P4 P5
92512** Synopsis: nColumn=P2
92513**
92514** Open a new cursor P1 to a transient table.
92515** The cursor is always opened read/write even if
92516** the main database is read-only. The ephemeral
92517** table is deleted automatically when the cursor is closed.
92518**
92519** If the cursor P1 is already opened on an ephemeral table, the table
92520** is cleared (all content is erased).
92521**
92522** P2 is the number of columns in the ephemeral table.
92523** The cursor points to a BTree table if P4==0 and to a BTree index
92524** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
92525** that defines the format of keys in the index.
92526**
92527** The P5 parameter can be a mask of the BTREE_* flags defined
92528** in btree.h. These flags control aspects of the operation of
92529** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
92530** added automatically.
92531**
92532** If P3 is positive, then reg[P3] is modified slightly so that it
92533** can be used as zero-length data for OP_Insert. This is an optimization
92534** that avoids an extra OP_Blob opcode to initialize that register.
92535*/
92536/* Opcode: OpenAutoindex P1 P2 * P4 *
92537** Synopsis: nColumn=P2
92538**
92539** This opcode works the same as OP_OpenEphemeral. It has a
92540** different name to distinguish its use. Tables created using
92541** by this opcode will be used for automatically created transient
92542** indices in joins.
92543*/
92544case OP_OpenAutoindex116:
92545case OP_OpenEphemeral118: {
92546 VdbeCursor *pCx;
92547 KeyInfo *pKeyInfo;
92548
92549 static const int vfsFlags =
92550 SQLITE_OPEN_READWRITE0x00000002 |
92551 SQLITE_OPEN_CREATE0x00000004 |
92552 SQLITE_OPEN_EXCLUSIVE0x00000010 |
92553 SQLITE_OPEN_DELETEONCLOSE0x00000008 |
92554 SQLITE_OPEN_TRANSIENT_DB0x00000400;
92555 assert( pOp->p1>=0 )((void) (0));
92556 assert( pOp->p2>=0 )((void) (0));
92557 if( pOp->p3>0 ){
92558 /* Make register reg[P3] into a value that can be used as the data
92559 ** form sqlite3BtreeInsert() where the length of the data is zero. */
92560 assert( pOp->p2==0 )((void) (0)); /* Only used when number of columns is zero */
92561 assert( pOp->opcode==OP_OpenEphemeral )((void) (0));
92562 assert( aMem[pOp->p3].flags & MEM_Null )((void) (0));
92563 aMem[pOp->p3].n = 0;
92564 aMem[pOp->p3].z = "";
92565 }
92566 pCx = p->apCsr[pOp->p1];
92567 if( pCx && !pCx->noReuse && ALWAYS(pOp->p2<=pCx->nField)(pOp->p2<=pCx->nField) ){
92568 /* If the ephermeral table is already open and has no duplicates from
92569 ** OP_OpenDup, then erase all existing content so that the table is
92570 ** empty again, rather than creating a new table. */
92571 assert( pCx->isEphemeral )((void) (0));
92572 pCx->seqCount = 0;
92573 pCx->cacheStatus = CACHE_STALE0;
92574 rc = sqlite3BtreeClearTable(pCx->ub.pBtx, pCx->pgnoRoot, 0);
92575 }else{
92576 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_BTREE0);
92577 if( pCx==0 ) goto no_mem;
92578 pCx->isEphemeral = 1;
92579 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->ub.pBtx,
92580 BTREE_OMIT_JOURNAL1 | BTREE_SINGLE4 | pOp->p5,
92581 vfsFlags);
92582 if( rc==SQLITE_OK0 ){
92583 rc = sqlite3BtreeBeginTrans(pCx->ub.pBtx, 1, 0);
92584 if( rc==SQLITE_OK0 ){
92585 /* If a transient index is required, create it by calling
92586 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
92587 ** opening it. If a transient table is required, just use the
92588 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
92589 */
92590 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
92591 assert( pOp->p4type==P4_KEYINFO )((void) (0));
92592 rc = sqlite3BtreeCreateTable(pCx->ub.pBtx, &pCx->pgnoRoot,
92593 BTREE_BLOBKEY2 | pOp->p5);
92594 if( rc==SQLITE_OK0 ){
92595 assert( pCx->pgnoRoot==SCHEMA_ROOT+1 )((void) (0));
92596 assert( pKeyInfo->db==db )((void) (0));
92597 assert( pKeyInfo->enc==ENC(db) )((void) (0));
92598 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR0x00000004,
92599 pKeyInfo, pCx->uc.pCursor);
92600 }
92601 pCx->isTable = 0;
92602 }else{
92603 pCx->pgnoRoot = SCHEMA_ROOT1;
92604 rc = sqlite3BtreeCursor(pCx->ub.pBtx, SCHEMA_ROOT1, BTREE_WRCSR0x00000004,
92605 0, pCx->uc.pCursor);
92606 pCx->isTable = 1;
92607 }
92608 }
92609 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED8);
92610 if( rc ){
92611 sqlite3BtreeClose(pCx->ub.pBtx);
92612 }
92613 }
92614 }
92615 if( rc ) goto abort_due_to_error;
92616 pCx->nullRow = 1;
92617 break;
92618}
92619
92620/* Opcode: SorterOpen P1 P2 P3 P4 *
92621**
92622** This opcode works like OP_OpenEphemeral except that it opens
92623** a transient index that is specifically designed to sort large
92624** tables using an external merge-sort algorithm.
92625**
92626** If argument P3 is non-zero, then it indicates that the sorter may
92627** assume that a stable sort considering the first P3 fields of each
92628** key is sufficient to produce the required results.
92629*/
92630case OP_SorterOpen119: {
92631 VdbeCursor *pCx;
92632
92633 assert( pOp->p1>=0 )((void) (0));
92634 assert( pOp->p2>=0 )((void) (0));
92635 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_SORTER1);
92636 if( pCx==0 ) goto no_mem;
92637 pCx->pKeyInfo = pOp->p4.pKeyInfo;
92638 assert( pCx->pKeyInfo->db==db )((void) (0));
92639 assert( pCx->pKeyInfo->enc==ENC(db) )((void) (0));
92640 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
92641 if( rc ) goto abort_due_to_error;
92642 break;
92643}
92644
92645/* Opcode: SequenceTest P1 P2 * * *
92646** Synopsis: if( cursor[P1].ctr++ ) pc = P2
92647**
92648** P1 is a sorter cursor. If the sequence counter is currently zero, jump
92649** to P2. Regardless of whether or not the jump is taken, increment the
92650** the sequence value.
92651*/
92652case OP_SequenceTest120: {
92653 VdbeCursor *pC;
92654 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
92655 pC = p->apCsr[pOp->p1];
92656 assert( isSorter(pC) )((void) (0));
92657 if( (pC->seqCount++)==0 ){
92658 goto jump_to_p2;
92659 }
92660 break;
92661}
92662
92663/* Opcode: OpenPseudo P1 P2 P3 * *
92664** Synopsis: P3 columns in r[P2]
92665**
92666** Open a new cursor that points to a fake table that contains a single
92667** row of data. The content of that one row is the content of memory
92668** register P2. In other words, cursor P1 becomes an alias for the
92669** MEM_Blob content contained in register P2.
92670**
92671** A pseudo-table created by this opcode is used to hold a single
92672** row output from the sorter so that the row can be decomposed into
92673** individual columns using the OP_Column opcode. The OP_Column opcode
92674** is the only cursor opcode that works with a pseudo-table.
92675**
92676** P3 is the number of fields in the records that will be stored by
92677** the pseudo-table.
92678*/
92679case OP_OpenPseudo121: {
92680 VdbeCursor *pCx;
92681
92682 assert( pOp->p1>=0 )((void) (0));
92683 assert( pOp->p3>=0 )((void) (0));
92684 pCx = allocateCursor(p, pOp->p1, pOp->p3, CURTYPE_PSEUDO3);
92685 if( pCx==0 ) goto no_mem;
92686 pCx->nullRow = 1;
92687 pCx->seekResult = pOp->p2;
92688 pCx->isTable = 1;
92689 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
92690 ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test
92691 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
92692 ** which is a performance optimization */
92693 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
92694 assert( pOp->p5==0 )((void) (0));
92695 break;
92696}
92697
92698/* Opcode: Close P1 * * * *
92699**
92700** Close a cursor previously opened as P1. If P1 is not
92701** currently open, this instruction is a no-op.
92702*/
92703case OP_Close122: {
92704 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
92705 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
92706 p->apCsr[pOp->p1] = 0;
92707 break;
92708}
92709
92710#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
92711/* Opcode: ColumnsUsed P1 * * P4 *
92712**
92713** This opcode (which only exists if SQLite was compiled with
92714** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
92715** table or index for cursor P1 are used. P4 is a 64-bit integer
92716** (P4_INT64) in which the first 63 bits are one for each of the
92717** first 63 columns of the table or index that are actually used
92718** by the cursor. The high-order bit is set if any column after
92719** the 64th is used.
92720*/
92721case OP_ColumnsUsed123: {
92722 VdbeCursor *pC;
92723 pC = p->apCsr[pOp->p1];
92724 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
92725 pC->maskUsed = *(u64*)pOp->p4.pI64;
92726 break;
92727}
92728#endif
92729
92730/* Opcode: SeekGE P1 P2 P3 P4 *
92731** Synopsis: key=r[P3@P4]
92732**
92733** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
92734** use the value in register P3 as the key. If cursor P1 refers
92735** to an SQL index, then P3 is the first in an array of P4 registers
92736** that are used as an unpacked index key.
92737**
92738** Reposition cursor P1 so that it points to the smallest entry that
92739** is greater than or equal to the key value. If there are no records
92740** greater than or equal to the key and P2 is not zero, then jump to P2.
92741**
92742** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
92743** opcode will either land on a record that exactly matches the key, or
92744** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
92745** this opcode must be followed by an IdxLE opcode with the same arguments.
92746** The IdxGT opcode will be skipped if this opcode succeeds, but the
92747** IdxGT opcode will be used on subsequent loop iterations. The
92748** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
92749** is an equality search.
92750**
92751** This opcode leaves the cursor configured to move in forward order,
92752** from the beginning toward the end. In other words, the cursor is
92753** configured to use Next, not Prev.
92754**
92755** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
92756*/
92757/* Opcode: SeekGT P1 P2 P3 P4 *
92758** Synopsis: key=r[P3@P4]
92759**
92760** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
92761** use the value in register P3 as a key. If cursor P1 refers
92762** to an SQL index, then P3 is the first in an array of P4 registers
92763** that are used as an unpacked index key.
92764**
92765** Reposition cursor P1 so that it points to the smallest entry that
92766** is greater than the key value. If there are no records greater than
92767** the key and P2 is not zero, then jump to P2.
92768**
92769** This opcode leaves the cursor configured to move in forward order,
92770** from the beginning toward the end. In other words, the cursor is
92771** configured to use Next, not Prev.
92772**
92773** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
92774*/
92775/* Opcode: SeekLT P1 P2 P3 P4 *
92776** Synopsis: key=r[P3@P4]
92777**
92778** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
92779** use the value in register P3 as a key. If cursor P1 refers
92780** to an SQL index, then P3 is the first in an array of P4 registers
92781** that are used as an unpacked index key.
92782**
92783** Reposition cursor P1 so that it points to the largest entry that
92784** is less than the key value. If there are no records less than
92785** the key and P2 is not zero, then jump to P2.
92786**
92787** This opcode leaves the cursor configured to move in reverse order,
92788** from the end toward the beginning. In other words, the cursor is
92789** configured to use Prev, not Next.
92790**
92791** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
92792*/
92793/* Opcode: SeekLE P1 P2 P3 P4 *
92794** Synopsis: key=r[P3@P4]
92795**
92796** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
92797** use the value in register P3 as a key. If cursor P1 refers
92798** to an SQL index, then P3 is the first in an array of P4 registers
92799** that are used as an unpacked index key.
92800**
92801** Reposition cursor P1 so that it points to the largest entry that
92802** is less than or equal to the key value. If there are no records
92803** less than or equal to the key and P2 is not zero, then jump to P2.
92804**
92805** This opcode leaves the cursor configured to move in reverse order,
92806** from the end toward the beginning. In other words, the cursor is
92807** configured to use Prev, not Next.
92808**
92809** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
92810** opcode will either land on a record that exactly matches the key, or
92811** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
92812** this opcode must be followed by an IdxLE opcode with the same arguments.
92813** The IdxGE opcode will be skipped if this opcode succeeds, but the
92814** IdxGE opcode will be used on subsequent loop iterations. The
92815** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
92816** is an equality search.
92817**
92818** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
92819*/
92820case OP_SeekLT20: /* jump, in3, group */
92821case OP_SeekLE21: /* jump, in3, group */
92822case OP_SeekGE22: /* jump, in3, group */
92823case OP_SeekGT23: { /* jump, in3, group */
92824 int res; /* Comparison result */
92825 int oc; /* Opcode */
92826 VdbeCursor *pC; /* The cursor to seek */
92827 UnpackedRecord r; /* The key to seek for */
92828 int nField; /* Number of columns or fields in the key */
92829 i64 iKey; /* The rowid we are to seek to */
92830 int eqOnly; /* Only interested in == results */
92831
92832 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
92833 assert( pOp->p2!=0 )((void) (0));
92834 pC = p->apCsr[pOp->p1];
92835 assert( pC!=0 )((void) (0));
92836 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
92837 assert( OP_SeekLE == OP_SeekLT+1 )((void) (0));
92838 assert( OP_SeekGE == OP_SeekLT+2 )((void) (0));
92839 assert( OP_SeekGT == OP_SeekLT+3 )((void) (0));
92840 assert( pC->isOrdered )((void) (0));
92841 assert( pC->uc.pCursor!=0 )((void) (0));
92842 oc = pOp->opcode;
92843 eqOnly = 0;
92844 pC->nullRow = 0;
92845#ifdef SQLITE_DEBUG
92846 pC->seekOp = pOp->opcode;
92847#endif
92848
92849 pC->deferredMoveto = 0;
92850 pC->cacheStatus = CACHE_STALE0;
92851 if( pC->isTable ){
92852 u16 flags3, newType;
92853 /* The OPFLAG_SEEKEQ/BTREE_SEEK_EQ flag is only set on index cursors */
92854 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0((void) (0))
92855 || CORRUPT_DB )((void) (0));
92856
92857 /* The input value in P3 might be of any type: integer, real, string,
92858 ** blob, or NULL. But it needs to be an integer before we can do
92859 ** the seek, so convert it. */
92860 pIn3 = &aMem[pOp->p3];
92861 flags3 = pIn3->flags;
92862 if( (flags3 & (MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020|MEM_Str0x0002))==MEM_Str0x0002 ){
92863 applyNumericAffinity(pIn3, 0);
92864 }
92865 iKey = sqlite3VdbeIntValue(pIn3); /* Get the integer key value */
92866 newType = pIn3->flags; /* Record the type after applying numeric affinity */
92867 pIn3->flags = flags3; /* But convert the type back to its original */
92868
92869 /* If the P3 value could not be converted into an integer without
92870 ** loss of information, then special processing is required... */
92871 if( (newType & (MEM_Int0x0004|MEM_IntReal0x0020))==0 ){
92872 int c;
92873 if( (newType & MEM_Real0x0008)==0 ){
92874 if( (newType & MEM_Null0x0001) || oc>=OP_SeekGE22 ){
92875 VdbeBranchTaken(1,2);
92876 goto jump_to_p2;
92877 }else{
92878 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
92879 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
92880 goto seek_not_found;
92881 }
92882 }
92883 c = sqlite3IntFloatCompare(iKey, pIn3->u.r);
92884
92885 /* If the approximation iKey is larger than the actual real search
92886 ** term, substitute >= for > and < for <=. e.g. if the search term
92887 ** is 4.9 and the integer approximation 5:
92888 **
92889 ** (x > 4.9) -> (x >= 5)
92890 ** (x <= 4.9) -> (x < 5)
92891 */
92892 if( c>0 ){
92893 assert( OP_SeekGE==(OP_SeekGT-1) )((void) (0));
92894 assert( OP_SeekLT==(OP_SeekLE-1) )((void) (0));
92895 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) )((void) (0));
92896 if( (oc & 0x0001)==(OP_SeekGT23 & 0x0001) ) oc--;
92897 }
92898
92899 /* If the approximation iKey is smaller than the actual real search
92900 ** term, substitute <= for < and > for >=. */
92901 else if( c<0 ){
92902 assert( OP_SeekLE==(OP_SeekLT+1) )((void) (0));
92903 assert( OP_SeekGT==(OP_SeekGE+1) )((void) (0));
92904 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) )((void) (0));
92905 if( (oc & 0x0001)==(OP_SeekLT20 & 0x0001) ) oc++;
92906 }
92907 }
92908 rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)iKey, 0, &res);
92909 pC->movetoTarget = iKey; /* Used by OP_Delete */
92910 if( rc!=SQLITE_OK0 ){
92911 goto abort_due_to_error;
92912 }
92913 }else{
92914 /* For a cursor with the OPFLAG_SEEKEQ/BTREE_SEEK_EQ hint, only the
92915 ** OP_SeekGE and OP_SeekLE opcodes are allowed, and these must be
92916 ** immediately followed by an OP_IdxGT or OP_IdxLT opcode, respectively,
92917 ** with the same key.
92918 */
92919 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ0x00000002) ){
92920 eqOnly = 1;
92921 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE )((void) (0));
92922 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT )((void) (0));
92923 assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT )((void) (0));
92924 assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT )((void) (0));
92925 assert( pOp[1].p1==pOp[0].p1 )((void) (0));
92926 assert( pOp[1].p2==pOp[0].p2 )((void) (0));
92927 assert( pOp[1].p3==pOp[0].p3 )((void) (0));
92928 assert( pOp[1].p4.i==pOp[0].p4.i )((void) (0));
92929 }
92930
92931 nField = pOp->p4.i;
92932 assert( pOp->p4type==P4_INT32 )((void) (0));
92933 assert( nField>0 )((void) (0));
92934 r.pKeyInfo = pC->pKeyInfo;
92935 r.nField = (u16)nField;
92936
92937 /* The next line of code computes as follows, only faster:
92938 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
92939 ** r.default_rc = -1;
92940 ** }else{
92941 ** r.default_rc = +1;
92942 ** }
92943 */
92944 r.default_rc = ((1 & (oc - OP_SeekLT20)) ? -1 : +1);
92945 assert( oc!=OP_SeekGT || r.default_rc==-1 )((void) (0));
92946 assert( oc!=OP_SeekLE || r.default_rc==-1 )((void) (0));
92947 assert( oc!=OP_SeekGE || r.default_rc==+1 )((void) (0));
92948 assert( oc!=OP_SeekLT || r.default_rc==+1 )((void) (0));
92949
92950 r.aMem = &aMem[pOp->p3];
92951#ifdef SQLITE_DEBUG
92952 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) )((void) (0)); }
92953#endif
92954 r.eqSeen = 0;
92955 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &res);
92956 if( rc!=SQLITE_OK0 ){
92957 goto abort_due_to_error;
92958 }
92959 if( eqOnly && r.eqSeen==0 ){
92960 assert( res!=0 )((void) (0));
92961 goto seek_not_found;
92962 }
92963 }
92964#ifdef SQLITE_TEST
92965 sqlite3_search_count++;
92966#endif
92967 if( oc>=OP_SeekGE22 ){ assert( oc==OP_SeekGE || oc==OP_SeekGT )((void) (0));
92968 if( res<0 || (res==0 && oc==OP_SeekGT23) ){
92969 res = 0;
92970 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
92971 if( rc!=SQLITE_OK0 ){
92972 if( rc==SQLITE_DONE101 ){
92973 rc = SQLITE_OK0;
92974 res = 1;
92975 }else{
92976 goto abort_due_to_error;
92977 }
92978 }
92979 }else{
92980 res = 0;
92981 }
92982 }else{
92983 assert( oc==OP_SeekLT || oc==OP_SeekLE )((void) (0));
92984 if( res>0 || (res==0 && oc==OP_SeekLT20) ){
92985 res = 0;
92986 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
92987 if( rc!=SQLITE_OK0 ){
92988 if( rc==SQLITE_DONE101 ){
92989 rc = SQLITE_OK0;
92990 res = 1;
92991 }else{
92992 goto abort_due_to_error;
92993 }
92994 }
92995 }else{
92996 /* res might be negative because the table is empty. Check to
92997 ** see if this is the case.
92998 */
92999 res = sqlite3BtreeEof(pC->uc.pCursor);
93000 }
93001 }
93002seek_not_found:
93003 assert( pOp->p2>0 )((void) (0));
93004 VdbeBranchTaken(res!=0,2);
93005 if( res ){
93006 goto jump_to_p2;
93007 }else if( eqOnly ){
93008 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT )((void) (0));
93009 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
93010 }
93011 break;
93012}
93013
93014
93015/* Opcode: SeekScan P1 P2 * * *
93016** Synopsis: Scan-ahead up to P1 rows
93017**
93018** This opcode is a prefix opcode to OP_SeekGE. In other words, this
93019** opcode must be immediately followed by OP_SeekGE. This constraint is
93020** checked by assert() statements.
93021**
93022** This opcode uses the P1 through P4 operands of the subsequent
93023** OP_SeekGE. In the text that follows, the operands of the subsequent
93024** OP_SeekGE opcode are denoted as SeekOP.P1 through SeekOP.P4. Only
93025** the P1 and P2 operands of this opcode are also used, and are called
93026** This.P1 and This.P2.
93027**
93028** This opcode helps to optimize IN operators on a multi-column index
93029** where the IN operator is on the later terms of the index by avoiding
93030** unnecessary seeks on the btree, substituting steps to the next row
93031** of the b-tree instead. A correct answer is obtained if this opcode
93032** is omitted or is a no-op.
93033**
93034** The SeekGE.P3 and SeekGE.P4 operands identify an unpacked key which
93035** is the desired entry that we want the cursor SeekGE.P1 to be pointing
93036** to. Call this SeekGE.P4/P5 row the "target".
93037**
93038** If the SeekGE.P1 cursor is not currently pointing to a valid row,
93039** then this opcode is a no-op and control passes through into the OP_SeekGE.
93040**
93041** If the SeekGE.P1 cursor is pointing to a valid row, then that row
93042** might be the target row, or it might be near and slightly before the
93043** target row. This opcode attempts to position the cursor on the target
93044** row by, perhaps by invoking sqlite3BtreeStep() on the cursor
93045** between 0 and This.P1 times.
93046**
93047** There are three possible outcomes from this opcode:<ol>
93048**
93049** <li> If after This.P1 steps, the cursor is still pointing to a place that
93050** is earlier in the btree than the target row, then fall through
93051** into the subsquence OP_SeekGE opcode.
93052**
93053** <li> If the cursor is successfully moved to the target row by 0 or more
93054** sqlite3BtreeNext() calls, then jump to This.P2, which will land just
93055** past the OP_IdxGT or OP_IdxGE opcode that follows the OP_SeekGE.
93056**
93057** <li> If the cursor ends up past the target row (indicating the the target
93058** row does not exist in the btree) then jump to SeekOP.P2.
93059** </ol>
93060*/
93061case OP_SeekScan124: {
93062 VdbeCursor *pC;
93063 int res;
93064 int nStep;
93065 UnpackedRecord r;
93066
93067 assert( pOp[1].opcode==OP_SeekGE )((void) (0));
93068
93069 /* pOp->p2 points to the first instruction past the OP_IdxGT that
93070 ** follows the OP_SeekGE. */
93071 assert( pOp->p2>=(int)(pOp-aOp)+2 )((void) (0));
93072 assert( aOp[pOp->p2-1].opcode==OP_IdxGT || aOp[pOp->p2-1].opcode==OP_IdxGE )((void) (0));
93073 testcase( aOp[pOp->p2-1].opcode==OP_IdxGE );
93074 assert( pOp[1].p1==aOp[pOp->p2-1].p1 )((void) (0));
93075 assert( pOp[1].p2==aOp[pOp->p2-1].p2 )((void) (0));
93076 assert( pOp[1].p3==aOp[pOp->p2-1].p3 )((void) (0));
93077
93078 assert( pOp->p1>0 )((void) (0));
93079 pC = p->apCsr[pOp[1].p1];
93080 assert( pC!=0 )((void) (0));
93081 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
93082 assert( !pC->isTable )((void) (0));
93083 if( !sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) ){
93084#ifdef SQLITE_DEBUG
93085 if( db->flags&SQLITE_VdbeTrace ){
93086 printf("... cursor not valid - fall through\n");
93087 }
93088#endif
93089 break;
93090 }
93091 nStep = pOp->p1;
93092 assert( nStep>=1 )((void) (0));
93093 r.pKeyInfo = pC->pKeyInfo;
93094 r.nField = (u16)pOp[1].p4.i;
93095 r.default_rc = 0;
93096 r.aMem = &aMem[pOp[1].p3];
93097#ifdef SQLITE_DEBUG
93098 {
93099 int i;
93100 for(i=0; i<r.nField; i++){
93101 assert( memIsValid(&r.aMem[i]) )((void) (0));
93102 REGISTER_TRACE(pOp[1].p3+i, &aMem[pOp[1].p3+i]);
93103 }
93104 }
93105#endif
93106 res = 0; /* Not needed. Only used to silence a warning. */
93107 while(1){
93108 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
93109 if( rc ) goto abort_due_to_error;
93110 if( res>0 ){
93111 seekscan_search_fail:
93112#ifdef SQLITE_DEBUG
93113 if( db->flags&SQLITE_VdbeTrace ){
93114 printf("... %d steps and then skip\n", pOp->p1 - nStep);
93115 }
93116#endif
93117 VdbeBranchTaken(1,3);
93118 pOp++;
93119 goto jump_to_p2;
93120 }
93121 if( res==0 ){
93122#ifdef SQLITE_DEBUG
93123 if( db->flags&SQLITE_VdbeTrace ){
93124 printf("... %d steps and then success\n", pOp->p1 - nStep);
93125 }
93126#endif
93127 VdbeBranchTaken(2,3);
93128 goto jump_to_p2;
93129 break;
93130 }
93131 if( nStep<=0 ){
93132#ifdef SQLITE_DEBUG
93133 if( db->flags&SQLITE_VdbeTrace ){
93134 printf("... fall through after %d steps\n", pOp->p1);
93135 }
93136#endif
93137 VdbeBranchTaken(0,3);
93138 break;
93139 }
93140 nStep--;
93141 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
93142 if( rc ){
93143 if( rc==SQLITE_DONE101 ){
93144 rc = SQLITE_OK0;
93145 goto seekscan_search_fail;
93146 }else{
93147 goto abort_due_to_error;
93148 }
93149 }
93150 }
93151
93152 break;
93153}
93154
93155
93156/* Opcode: SeekHit P1 P2 P3 * *
93157** Synopsis: set P2<=seekHit<=P3
93158**
93159** Increase or decrease the seekHit value for cursor P1, if necessary,
93160** so that it is no less than P2 and no greater than P3.
93161**
93162** The seekHit integer represents the maximum of terms in an index for which
93163** there is known to be at least one match. If the seekHit value is smaller
93164** than the total number of equality terms in an index lookup, then the
93165** OP_IfNoHope opcode might run to see if the IN loop can be abandoned
93166** early, thus saving work. This is part of the IN-early-out optimization.
93167**
93168** P1 must be a valid b-tree cursor.
93169*/
93170case OP_SeekHit125: {
93171 VdbeCursor *pC;
93172 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
93173 pC = p->apCsr[pOp->p1];
93174 assert( pC!=0 )((void) (0));
93175 assert( pOp->p3>=pOp->p2 )((void) (0));
93176 if( pC->seekHit<pOp->p2 ){
93177#ifdef SQLITE_DEBUG
93178 if( db->flags&SQLITE_VdbeTrace ){
93179 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p2);
93180 }
93181#endif
93182 pC->seekHit = pOp->p2;
93183 }else if( pC->seekHit>pOp->p3 ){
93184#ifdef SQLITE_DEBUG
93185 if( db->flags&SQLITE_VdbeTrace ){
93186 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p3);
93187 }
93188#endif
93189 pC->seekHit = pOp->p3;
93190 }
93191 break;
93192}
93193
93194/* Opcode: IfNotOpen P1 P2 * * *
93195** Synopsis: if( !csr[P1] ) goto P2
93196**
93197** If cursor P1 is not open, jump to instruction P2. Otherwise, fall through.
93198*/
93199case OP_IfNotOpen24: { /* jump */
93200 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
93201 VdbeBranchTaken(p->apCsr[pOp->p1]==0, 2);
93202 if( !p->apCsr[pOp->p1] ){
93203 goto jump_to_p2_and_check_for_interrupt;
93204 }
93205 break;
93206}
93207
93208/* Opcode: Found P1 P2 P3 P4 *
93209** Synopsis: key=r[P3@P4]
93210**
93211** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
93212** P4>0 then register P3 is the first of P4 registers that form an unpacked
93213** record.
93214**
93215** Cursor P1 is on an index btree. If the record identified by P3 and P4
93216** is a prefix of any entry in P1 then a jump is made to P2 and
93217** P1 is left pointing at the matching entry.
93218**
93219** This operation leaves the cursor in a state where it can be
93220** advanced in the forward direction. The Next instruction will work,
93221** but not the Prev instruction.
93222**
93223** See also: NotFound, NoConflict, NotExists. SeekGe
93224*/
93225/* Opcode: NotFound P1 P2 P3 P4 *
93226** Synopsis: key=r[P3@P4]
93227**
93228** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
93229** P4>0 then register P3 is the first of P4 registers that form an unpacked
93230** record.
93231**
93232** Cursor P1 is on an index btree. If the record identified by P3 and P4
93233** is not the prefix of any entry in P1 then a jump is made to P2. If P1
93234** does contain an entry whose prefix matches the P3/P4 record then control
93235** falls through to the next instruction and P1 is left pointing at the
93236** matching entry.
93237**
93238** This operation leaves the cursor in a state where it cannot be
93239** advanced in either direction. In other words, the Next and Prev
93240** opcodes do not work after this operation.
93241**
93242** See also: Found, NotExists, NoConflict, IfNoHope
93243*/
93244/* Opcode: IfNoHope P1 P2 P3 P4 *
93245** Synopsis: key=r[P3@P4]
93246**
93247** Register P3 is the first of P4 registers that form an unpacked
93248** record. Cursor P1 is an index btree. P2 is a jump destination.
93249** In other words, the operands to this opcode are the same as the
93250** operands to OP_NotFound and OP_IdxGT.
93251**
93252** This opcode is an optimization attempt only. If this opcode always
93253** falls through, the correct answer is still obtained, but extra works
93254** is performed.
93255**
93256** A value of N in the seekHit flag of cursor P1 means that there exists
93257** a key P3:N that will match some record in the index. We want to know
93258** if it is possible for a record P3:P4 to match some record in the
93259** index. If it is not possible, we can skips some work. So if seekHit
93260** is less than P4, attempt to find out if a match is possible by running
93261** OP_NotFound.
93262**
93263** This opcode is used in IN clause processing for a multi-column key.
93264** If an IN clause is attached to an element of the key other than the
93265** left-most element, and if there are no matches on the most recent
93266** seek over the whole key, then it might be that one of the key element
93267** to the left is prohibiting a match, and hence there is "no hope" of
93268** any match regardless of how many IN clause elements are checked.
93269** In such a case, we abandon the IN clause search early, using this
93270** opcode. The opcode name comes from the fact that the
93271** jump is taken if there is "no hope" of achieving a match.
93272**
93273** See also: NotFound, SeekHit
93274*/
93275/* Opcode: NoConflict P1 P2 P3 P4 *
93276** Synopsis: key=r[P3@P4]
93277**
93278** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
93279** P4>0 then register P3 is the first of P4 registers that form an unpacked
93280** record.
93281**
93282** Cursor P1 is on an index btree. If the record identified by P3 and P4
93283** contains any NULL value, jump immediately to P2. If all terms of the
93284** record are not-NULL then a check is done to determine if any row in the
93285** P1 index btree has a matching key prefix. If there are no matches, jump
93286** immediately to P2. If there is a match, fall through and leave the P1
93287** cursor pointing to the matching row.
93288**
93289** This opcode is similar to OP_NotFound with the exceptions that the
93290** branch is always taken if any part of the search key input is NULL.
93291**
93292** This operation leaves the cursor in a state where it cannot be
93293** advanced in either direction. In other words, the Next and Prev
93294** opcodes do not work after this operation.
93295**
93296** See also: NotFound, Found, NotExists
93297*/
93298case OP_IfNoHope25: { /* jump, in3 */
93299 VdbeCursor *pC;
93300 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
93301 pC = p->apCsr[pOp->p1];
93302 assert( pC!=0 )((void) (0));
93303#ifdef SQLITE_DEBUG
93304 if( db->flags&SQLITE_VdbeTrace ){
93305 printf("seekHit is %d\n", pC->seekHit);
93306 }
93307#endif
93308 if( pC->seekHit>=pOp->p4.i ) break;
93309 /* Fall through into OP_NotFound */
93310 /* no break */ deliberate_fall_through
93311}
93312case OP_NoConflict26: /* jump, in3 */
93313case OP_NotFound27: /* jump, in3 */
93314case OP_Found28: { /* jump, in3 */
93315 int alreadyExists;
93316 int ii;
93317 VdbeCursor *pC;
93318 UnpackedRecord *pIdxKey;
93319 UnpackedRecord r;
93320
93321#ifdef SQLITE_TEST
93322 if( pOp->opcode!=OP_NoConflict26 ) sqlite3_found_count++;
93323#endif
93324
93325 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
93326 assert( pOp->p4type==P4_INT32 )((void) (0));
93327 pC = p->apCsr[pOp->p1];
93328 assert( pC!=0 )((void) (0));
93329#ifdef SQLITE_DEBUG
93330 pC->seekOp = pOp->opcode;
93331#endif
93332 r.aMem = &aMem[pOp->p3];
93333 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
93334 assert( pC->uc.pCursor!=0 )((void) (0));
93335 assert( pC->isTable==0 )((void) (0));
93336 r.nField = (u16)pOp->p4.i;
93337 if( r.nField>0 ){
93338 /* Key values in an array of registers */
93339 r.pKeyInfo = pC->pKeyInfo;
93340 r.default_rc = 0;
93341#ifdef SQLITE_DEBUG
93342 for(ii=0; ii<r.nField; ii++){
93343 assert( memIsValid(&r.aMem[ii]) )((void) (0));
93344 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 )((void) (0));
93345 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
93346 }
93347#endif
93348 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &pC->seekResult);
93349 }else{
93350 /* Composite key generated by OP_MakeRecord */
93351 assert( r.aMem->flags & MEM_Blob )((void) (0));
93352 assert( pOp->opcode!=OP_NoConflict )((void) (0));
93353 rc = ExpandBlob(r.aMem)(((r.aMem)->flags&0x0400)?sqlite3VdbeMemExpandBlob(r.aMem
):0)
;
93354 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM )((void) (0));
93355 if( rc ) goto no_mem;
93356 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
93357 if( pIdxKey==0 ) goto no_mem;
93358 sqlite3VdbeRecordUnpack(pC->pKeyInfo, r.aMem->n, r.aMem->z, pIdxKey);
93359 pIdxKey->default_rc = 0;
93360 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, pIdxKey, &pC->seekResult);
93361 sqlite3DbFreeNN(db, pIdxKey);
93362 }
93363 if( rc!=SQLITE_OK0 ){
93364 goto abort_due_to_error;
93365 }
93366 alreadyExists = (pC->seekResult==0);
93367 pC->nullRow = 1-alreadyExists;
93368 pC->deferredMoveto = 0;
93369 pC->cacheStatus = CACHE_STALE0;
93370 if( pOp->opcode==OP_Found28 ){
93371 VdbeBranchTaken(alreadyExists!=0,2);
93372 if( alreadyExists ) goto jump_to_p2;
93373 }else{
93374 if( !alreadyExists ){
93375 VdbeBranchTaken(1,2);
93376 goto jump_to_p2;
93377 }
93378 if( pOp->opcode==OP_NoConflict26 ){
93379 /* For the OP_NoConflict opcode, take the jump if any of the
93380 ** input fields are NULL, since any key with a NULL will not
93381 ** conflict */
93382 for(ii=0; ii<r.nField; ii++){
93383 if( r.aMem[ii].flags & MEM_Null0x0001 ){
93384 VdbeBranchTaken(1,2);
93385 goto jump_to_p2;
93386 }
93387 }
93388 }
93389 VdbeBranchTaken(0,2);
93390 if( pOp->opcode==OP_IfNoHope25 ){
93391 pC->seekHit = pOp->p4.i;
93392 }
93393 }
93394 break;
93395}
93396
93397/* Opcode: SeekRowid P1 P2 P3 * *
93398** Synopsis: intkey=r[P3]
93399**
93400** P1 is the index of a cursor open on an SQL table btree (with integer
93401** keys). If register P3 does not contain an integer or if P1 does not
93402** contain a record with rowid P3 then jump immediately to P2.
93403** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
93404** a record with rowid P3 then
93405** leave the cursor pointing at that record and fall through to the next
93406** instruction.
93407**
93408** The OP_NotExists opcode performs the same operation, but with OP_NotExists
93409** the P3 register must be guaranteed to contain an integer value. With this
93410** opcode, register P3 might not contain an integer.
93411**
93412** The OP_NotFound opcode performs the same operation on index btrees
93413** (with arbitrary multi-value keys).
93414**
93415** This opcode leaves the cursor in a state where it cannot be advanced
93416** in either direction. In other words, the Next and Prev opcodes will
93417** not work following this opcode.
93418**
93419** See also: Found, NotFound, NoConflict, SeekRowid
93420*/
93421/* Opcode: NotExists P1 P2 P3 * *
93422** Synopsis: intkey=r[P3]
93423**
93424** P1 is the index of a cursor open on an SQL table btree (with integer
93425** keys). P3 is an integer rowid. If P1 does not contain a record with
93426** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
93427** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
93428** leave the cursor pointing at that record and fall through to the next
93429** instruction.
93430**
93431** The OP_SeekRowid opcode performs the same operation but also allows the
93432** P3 register to contain a non-integer value, in which case the jump is
93433** always taken. This opcode requires that P3 always contain an integer.
93434**
93435** The OP_NotFound opcode performs the same operation on index btrees
93436** (with arbitrary multi-value keys).
93437**
93438** This opcode leaves the cursor in a state where it cannot be advanced
93439** in either direction. In other words, the Next and Prev opcodes will
93440** not work following this opcode.
93441**
93442** See also: Found, NotFound, NoConflict, SeekRowid
93443*/
93444case OP_SeekRowid29: { /* jump, in3 */
93445 VdbeCursor *pC;
93446 BtCursor *pCrsr;
93447 int res;
93448 u64 iKey;
93449
93450 pIn3 = &aMem[pOp->p3];
93451 testcase( pIn3->flags & MEM_Int );
93452 testcase( pIn3->flags & MEM_IntReal );
93453 testcase( pIn3->flags & MEM_Real );
93454 testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str );
93455 if( (pIn3->flags & (MEM_Int0x0004|MEM_IntReal0x0020))==0 ){
93456 /* If pIn3->u.i does not contain an integer, compute iKey as the
93457 ** integer value of pIn3. Jump to P2 if pIn3 cannot be converted
93458 ** into an integer without loss of information. Take care to avoid
93459 ** changing the datatype of pIn3, however, as it is used by other
93460 ** parts of the prepared statement. */
93461 Mem x = pIn3[0];
93462 applyAffinity(&x, SQLITE_AFF_NUMERIC0x43, encoding);
93463 if( (x.flags & MEM_Int0x0004)==0 ) goto jump_to_p2;
93464 iKey = x.u.i;
93465 goto notExistsWithKey;
93466 }
93467 /* Fall through into OP_NotExists */
93468 /* no break */ deliberate_fall_through
93469case OP_NotExists30: /* jump, in3 */
93470 pIn3 = &aMem[pOp->p3];
93471 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid )((void) (0));
93472 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
93473 iKey = pIn3->u.i;
93474notExistsWithKey:
93475 pC = p->apCsr[pOp->p1];
93476 assert( pC!=0 )((void) (0));
93477#ifdef SQLITE_DEBUG
93478 if( pOp->opcode==OP_SeekRowid29 ) pC->seekOp = OP_SeekRowid29;
93479#endif
93480 assert( pC->isTable )((void) (0));
93481 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
93482 pCrsr = pC->uc.pCursor;
93483 assert( pCrsr!=0 )((void) (0));
93484 res = 0;
93485 rc = sqlite3BtreeTableMoveto(pCrsr, iKey, 0, &res);
93486 assert( rc==SQLITE_OK || res==0 )((void) (0));
93487 pC->movetoTarget = iKey; /* Used by OP_Delete */
93488 pC->nullRow = 0;
93489 pC->cacheStatus = CACHE_STALE0;
93490 pC->deferredMoveto = 0;
93491 VdbeBranchTaken(res!=0,2);
93492 pC->seekResult = res;
93493 if( res!=0 ){
93494 assert( rc==SQLITE_OK )((void) (0));
93495 if( pOp->p2==0 ){
93496 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(93496);
93497 }else{
93498 goto jump_to_p2;
93499 }
93500 }
93501 if( rc ) goto abort_due_to_error;
93502 break;
93503}
93504
93505/* Opcode: Sequence P1 P2 * * *
93506** Synopsis: r[P2]=cursor[P1].ctr++
93507**
93508** Find the next available sequence number for cursor P1.
93509** Write the sequence number into register P2.
93510** The sequence number on the cursor is incremented after this
93511** instruction.
93512*/
93513case OP_Sequence126: { /* out2 */
93514 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
93515 assert( p->apCsr[pOp->p1]!=0 )((void) (0));
93516 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB )((void) (0));
93517 pOut = out2Prerelease(p, pOp);
93518 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
93519 break;
93520}
93521
93522
93523/* Opcode: NewRowid P1 P2 P3 * *
93524** Synopsis: r[P2]=rowid
93525**
93526** Get a new integer record number (a.k.a "rowid") used as the key to a table.
93527** The record number is not previously used as a key in the database
93528** table that cursor P1 points to. The new record number is written
93529** written to register P2.
93530**
93531** If P3>0 then P3 is a register in the root frame of this VDBE that holds
93532** the largest previously generated record number. No new record numbers are
93533** allowed to be less than this value. When this value reaches its maximum,
93534** an SQLITE_FULL error is generated. The P3 register is updated with the '
93535** generated record number. This P3 mechanism is used to help implement the
93536** AUTOINCREMENT feature.
93537*/
93538case OP_NewRowid127: { /* out2 */
93539 i64 v; /* The new rowid */
93540 VdbeCursor *pC; /* Cursor of table to get the new rowid */
93541 int res; /* Result of an sqlite3BtreeLast() */
93542 int cnt; /* Counter to limit the number of searches */
93543#ifndef SQLITE_OMIT_AUTOINCREMENT
93544 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
93545 VdbeFrame *pFrame; /* Root frame of VDBE */
93546#endif
93547
93548 v = 0;
93549 res = 0;
93550 pOut = out2Prerelease(p, pOp);
93551 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
93552 pC = p->apCsr[pOp->p1];
93553 assert( pC!=0 )((void) (0));
93554 assert( pC->isTable )((void) (0));
93555 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
93556 assert( pC->uc.pCursor!=0 )((void) (0));
93557 {
93558 /* The next rowid or record number (different terms for the same
93559 ** thing) is obtained in a two-step algorithm.
93560 **
93561 ** First we attempt to find the largest existing rowid and add one
93562 ** to that. But if the largest existing rowid is already the maximum
93563 ** positive integer, we have to fall through to the second
93564 ** probabilistic algorithm
93565 **
93566 ** The second algorithm is to select a rowid at random and see if
93567 ** it already exists in the table. If it does not exist, we have
93568 ** succeeded. If the random rowid does exist, we select a new one
93569 ** and try again, up to 100 times.
93570 */
93571 assert( pC->isTable )((void) (0));
93572
93573#ifdef SQLITE_32BIT_ROWID
93574# define MAX_ROWID(i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) 0x7fffffff
93575#else
93576 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
93577 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
93578 ** to provide the constant while making all compilers happy.
93579 */
93580# define MAX_ROWID(i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
93581#endif
93582
93583 if( !pC->useRandomRowid ){
93584 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
93585 if( rc!=SQLITE_OK0 ){
93586 goto abort_due_to_error;
93587 }
93588 if( res ){
93589 v = 1; /* IMP: R-61914-48074 */
93590 }else{
93591 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) )((void) (0));
93592 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
93593 if( v>=MAX_ROWID(i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) ){
93594 pC->useRandomRowid = 1;
93595 }else{
93596 v++; /* IMP: R-29538-34987 */
93597 }
93598 }
93599 }
93600
93601#ifndef SQLITE_OMIT_AUTOINCREMENT
93602 if( pOp->p3 ){
93603 /* Assert that P3 is a valid memory cell. */
93604 assert( pOp->p3>0 )((void) (0));
93605 if( p->pFrame ){
93606 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
93607 /* Assert that P3 is a valid memory cell. */
93608 assert( pOp->p3<=pFrame->nMem )((void) (0));
93609 pMem = &pFrame->aMem[pOp->p3];
93610 }else{
93611 /* Assert that P3 is a valid memory cell. */
93612 assert( pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
93613 pMem = &aMem[pOp->p3];
93614 memAboutToChange(p, pMem);
93615 }
93616 assert( memIsValid(pMem) )((void) (0));
93617
93618 REGISTER_TRACE(pOp->p3, pMem);
93619 sqlite3VdbeMemIntegerify(pMem);
93620 assert( (pMem->flags & MEM_Int)!=0 )((void) (0)); /* mem(P3) holds an integer */
93621 if( pMem->u.i==MAX_ROWID(i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) || pC->useRandomRowid ){
93622 rc = SQLITE_FULL13; /* IMP: R-17817-00630 */
93623 goto abort_due_to_error;
93624 }
93625 if( v<pMem->u.i+1 ){
93626 v = pMem->u.i + 1;
93627 }
93628 pMem->u.i = v;
93629 }
93630#endif
93631 if( pC->useRandomRowid ){
93632 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
93633 ** largest possible integer (9223372036854775807) then the database
93634 ** engine starts picking positive candidate ROWIDs at random until
93635 ** it finds one that is not previously used. */
93636 assert( pOp->p3==0 )((void) (0)); /* We cannot be in random rowid mode if this is
93637 ** an AUTOINCREMENT table. */
93638 cnt = 0;
93639 do{
93640 sqlite3_randomness(sizeof(v), &v);
93641 v &= (MAX_ROWID(i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )>>1); v++; /* Ensure that v is greater than zero */
93642 }while( ((rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)v,
93643 0, &res))==SQLITE_OK0)
93644 && (res==0)
93645 && (++cnt<100));
93646 if( rc ) goto abort_due_to_error;
93647 if( res==0 ){
93648 rc = SQLITE_FULL13; /* IMP: R-38219-53002 */
93649 goto abort_due_to_error;
93650 }
93651 assert( v>0 )((void) (0)); /* EV: R-40812-03570 */
93652 }
93653 pC->deferredMoveto = 0;
93654 pC->cacheStatus = CACHE_STALE0;
93655 }
93656 pOut->u.i = v;
93657 break;
93658}
93659
93660/* Opcode: Insert P1 P2 P3 P4 P5
93661** Synopsis: intkey=r[P3] data=r[P2]
93662**
93663** Write an entry into the table of cursor P1. A new entry is
93664** created if it doesn't already exist or the data for an existing
93665** entry is overwritten. The data is the value MEM_Blob stored in register
93666** number P2. The key is stored in register P3. The key must
93667** be a MEM_Int.
93668**
93669** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
93670** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
93671** then rowid is stored for subsequent return by the
93672** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
93673**
93674** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
93675** run faster by avoiding an unnecessary seek on cursor P1. However,
93676** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
93677** seeks on the cursor or if the most recent seek used a key equal to P3.
93678**
93679** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
93680** UPDATE operation. Otherwise (if the flag is clear) then this opcode
93681** is part of an INSERT operation. The difference is only important to
93682** the update hook.
93683**
93684** Parameter P4 may point to a Table structure, or may be NULL. If it is
93685** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
93686** following a successful insert.
93687**
93688** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
93689** allocated, then ownership of P2 is transferred to the pseudo-cursor
93690** and register P2 becomes ephemeral. If the cursor is changed, the
93691** value of register P2 will then change. Make sure this does not
93692** cause any problems.)
93693**
93694** This instruction only works on tables. The equivalent instruction
93695** for indices is OP_IdxInsert.
93696*/
93697case OP_Insert128: {
93698 Mem *pData; /* MEM cell holding data for the record to be inserted */
93699 Mem *pKey; /* MEM cell holding key for the record */
93700 VdbeCursor *pC; /* Cursor to table into which insert is written */
93701 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
93702 const char *zDb; /* database name - used by the update hook */
93703 Table *pTab; /* Table structure - used by update and pre-update hooks */
93704 BtreePayload x; /* Payload to be inserted */
93705
93706 pData = &aMem[pOp->p2];
93707 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
93708 assert( memIsValid(pData) )((void) (0));
93709 pC = p->apCsr[pOp->p1];
93710 assert( pC!=0 )((void) (0));
93711 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
93712 assert( pC->deferredMoveto==0 )((void) (0));
93713 assert( pC->uc.pCursor!=0 )((void) (0));
93714 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable )((void) (0));
93715 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC )((void) (0));
93716 REGISTER_TRACE(pOp->p2, pData);
93717 sqlite3VdbeIncrWriteCounter(p, pC);
93718
93719 pKey = &aMem[pOp->p3];
93720 assert( pKey->flags & MEM_Int )((void) (0));
93721 assert( memIsValid(pKey) )((void) (0));
93722 REGISTER_TRACE(pOp->p3, pKey);
93723 x.nKey = pKey->u.i;
93724
93725 if( pOp->p4type==P4_TABLE(-5) && HAS_UPDATE_HOOK(db)((db)->xPreUpdateCallback||(db)->xUpdateCallback) ){
93726 assert( pC->iDb>=0 )((void) (0));
93727 zDb = db->aDb[pC->iDb].zDbSName;
93728 pTab = pOp->p4.pTab;
93729 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) )((void) (0));
93730 }else{
93731 pTab = 0;
93732 zDb = 0;
93733 }
93734
93735#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
93736 /* Invoke the pre-update hook, if any */
93737 if( pTab ){
93738 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE0x04) ){
93739 sqlite3VdbePreUpdateHook(p,pC,SQLITE_INSERT18,zDb,pTab,x.nKey,pOp->p2,-1);
93740 }
93741 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
93742 /* Prevent post-update hook from running in cases when it should not */
93743 pTab = 0;
93744 }
93745 }
93746 if( pOp->p5 & OPFLAG_ISNOOP0x40 ) break;
93747#endif
93748
93749 if( pOp->p5 & OPFLAG_NCHANGE0x01 ) p->nChange++;
93750 if( pOp->p5 & OPFLAG_LASTROWID0x20 ) db->lastRowid = x.nKey;
93751 assert( (pData->flags & (MEM_Blob|MEM_Str))!=0 || pData->n==0 )((void) (0));
93752 x.pData = pData->z;
93753 x.nData = pData->n;
93754 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT0x10) ? pC->seekResult : 0);
93755 if( pData->flags & MEM_Zero0x0400 ){
93756 x.nZero = pData->u.nZero;
93757 }else{
93758 x.nZero = 0;
93759 }
93760 x.pKey = 0;
93761 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
93762 (pOp->p5 & (OPFLAG_APPEND0x08|OPFLAG_SAVEPOSITION0x02|OPFLAG_PREFORMAT0x80)),
93763 seekResult
93764 );
93765 pC->deferredMoveto = 0;
93766 pC->cacheStatus = CACHE_STALE0;
93767
93768 /* Invoke the update-hook if required. */
93769 if( rc ) goto abort_due_to_error;
93770 if( pTab ){
93771 assert( db->xUpdateCallback!=0 )((void) (0));
93772 assert( pTab->aCol!=0 )((void) (0));
93773 db->xUpdateCallback(db->pUpdateArg,
93774 (pOp->p5 & OPFLAG_ISUPDATE0x04) ? SQLITE_UPDATE23 : SQLITE_INSERT18,
93775 zDb, pTab->zName, x.nKey);
93776 }
93777 break;
93778}
93779
93780/* Opcode: RowCell P1 P2 P3 * *
93781**
93782** P1 and P2 are both open cursors. Both must be opened on the same type
93783** of table - intkey or index. This opcode is used as part of copying
93784** the current row from P2 into P1. If the cursors are opened on intkey
93785** tables, register P3 contains the rowid to use with the new record in
93786** P1. If they are opened on index tables, P3 is not used.
93787**
93788** This opcode must be followed by either an Insert or InsertIdx opcode
93789** with the OPFLAG_PREFORMAT flag set to complete the insert operation.
93790*/
93791case OP_RowCell129: {
93792 VdbeCursor *pDest; /* Cursor to write to */
93793 VdbeCursor *pSrc; /* Cursor to read from */
93794 i64 iKey; /* Rowid value to insert with */
93795 assert( pOp[1].opcode==OP_Insert || pOp[1].opcode==OP_IdxInsert )((void) (0));
93796 assert( pOp[1].opcode==OP_Insert || pOp->p3==0 )((void) (0));
93797 assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 )((void) (0));
93798 assert( pOp[1].p5 & OPFLAG_PREFORMAT )((void) (0));
93799 pDest = p->apCsr[pOp->p1];
93800 pSrc = p->apCsr[pOp->p2];
93801 iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0;
93802 rc = sqlite3BtreeTransferRow(pDest->uc.pCursor, pSrc->uc.pCursor, iKey);
93803 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
93804 break;
93805};
93806
93807/* Opcode: Delete P1 P2 P3 P4 P5
93808**
93809** Delete the record at which the P1 cursor is currently pointing.
93810**
93811** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
93812** the cursor will be left pointing at either the next or the previous
93813** record in the table. If it is left pointing at the next record, then
93814** the next Next instruction will be a no-op. As a result, in this case
93815** it is ok to delete a record from within a Next loop. If
93816** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
93817** left in an undefined state.
93818**
93819** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
93820** delete one of several associated with deleting a table row and all its
93821** associated index entries. Exactly one of those deletes is the "primary"
93822** delete. The others are all on OPFLAG_FORDELETE cursors or else are
93823** marked with the AUXDELETE flag.
93824**
93825** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
93826** change count is incremented (otherwise not).
93827**
93828** P1 must not be pseudo-table. It has to be a real table with
93829** multiple rows.
93830**
93831** If P4 is not NULL then it points to a Table object. In this case either
93832** the update or pre-update hook, or both, may be invoked. The P1 cursor must
93833** have been positioned using OP_NotFound prior to invoking this opcode in
93834** this case. Specifically, if one is configured, the pre-update hook is
93835** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
93836** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
93837**
93838** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
93839** of the memory cell that contains the value that the rowid of the row will
93840** be set to by the update.
93841*/
93842case OP_Delete130: {
93843 VdbeCursor *pC;
93844 const char *zDb;
93845 Table *pTab;
93846 int opflags;
93847
93848 opflags = pOp->p2;
93849 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
93850 pC = p->apCsr[pOp->p1];
93851 assert( pC!=0 )((void) (0));
93852 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
93853 assert( pC->uc.pCursor!=0 )((void) (0));
93854 assert( pC->deferredMoveto==0 )((void) (0));
93855 sqlite3VdbeIncrWriteCounter(p, pC);
93856
93857#ifdef SQLITE_DEBUG
93858 if( pOp->p4type==P4_TABLE(-5)
93859 && HasRowid(pOp->p4.pTab)(((pOp->p4.pTab)->tabFlags & 0x00000080)==0)
93860 && pOp->p5==0
93861 && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor)
93862 ){
93863 /* If p5 is zero, the seek operation that positioned the cursor prior to
93864 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
93865 ** the row that is being deleted */
93866 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
93867 assert( CORRUPT_DB || pC->movetoTarget==iKey )((void) (0));
93868 }
93869#endif
93870
93871 /* If the update-hook or pre-update-hook will be invoked, set zDb to
93872 ** the name of the db to pass as to it. Also set local pTab to a copy
93873 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
93874 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
93875 ** VdbeCursor.movetoTarget to the current rowid. */
93876 if( pOp->p4type==P4_TABLE(-5) && HAS_UPDATE_HOOK(db)((db)->xPreUpdateCallback||(db)->xUpdateCallback) ){
93877 assert( pC->iDb>=0 )((void) (0));
93878 assert( pOp->p4.pTab!=0 )((void) (0));
93879 zDb = db->aDb[pC->iDb].zDbSName;
93880 pTab = pOp->p4.pTab;
93881 if( (pOp->p5 & OPFLAG_SAVEPOSITION0x02)!=0 && pC->isTable ){
93882 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
93883 }
93884 }else{
93885 zDb = 0;
93886 pTab = 0;
93887 }
93888
93889#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
93890 /* Invoke the pre-update-hook if required. */
93891 assert( db->xPreUpdateCallback==0 || pTab==pOp->p4.pTab )((void) (0));
93892 if( db->xPreUpdateCallback && pTab ){
93893 assert( !(opflags & OPFLAG_ISUPDATE)((void) (0))
93894 || HasRowid(pTab)==0((void) (0))
93895 || (aMem[pOp->p3].flags & MEM_Int)((void) (0))
93896 )((void) (0));
93897 sqlite3VdbePreUpdateHook(p, pC,
93898 (opflags & OPFLAG_ISUPDATE0x04) ? SQLITE_UPDATE23 : SQLITE_DELETE9,
93899 zDb, pTab, pC->movetoTarget,
93900 pOp->p3, -1
93901 );
93902 }
93903 if( opflags & OPFLAG_ISNOOP0x40 ) break;
93904#endif
93905
93906 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
93907 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 )((void) (0));
93908 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION )((void) (0));
93909 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE )((void) (0));
93910
93911#ifdef SQLITE_DEBUG
93912 if( p->pFrame==0 ){
93913 if( pC->isEphemeral==0
93914 && (pOp->p5 & OPFLAG_AUXDELETE0x04)==0
93915 && (pC->wrFlag & OPFLAG_FORDELETE0x08)==0
93916 ){
93917 nExtraDelete++;
93918 }
93919 if( pOp->p2 & OPFLAG_NCHANGE0x01 ){
93920 nExtraDelete--;
93921 }
93922 }
93923#endif
93924
93925 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
93926 pC->cacheStatus = CACHE_STALE0;
93927 pC->seekResult = 0;
93928 if( rc ) goto abort_due_to_error;
93929
93930 /* Invoke the update-hook if required. */
93931 if( opflags & OPFLAG_NCHANGE0x01 ){
93932 p->nChange++;
93933 if( db->xUpdateCallback && ALWAYS(pTab!=0)(pTab!=0) && HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
93934 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE9, zDb, pTab->zName,
93935 pC->movetoTarget);
93936 assert( pC->iDb>=0 )((void) (0));
93937 }
93938 }
93939
93940 break;
93941}
93942/* Opcode: ResetCount * * * * *
93943**
93944** The value of the change counter is copied to the database handle
93945** change counter (returned by subsequent calls to sqlite3_changes()).
93946** Then the VMs internal change counter resets to 0.
93947** This is used by trigger programs.
93948*/
93949case OP_ResetCount131: {
93950 sqlite3VdbeSetChanges(db, p->nChange);
93951 p->nChange = 0;
93952 break;
93953}
93954
93955/* Opcode: SorterCompare P1 P2 P3 P4
93956** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
93957**
93958** P1 is a sorter cursor. This instruction compares a prefix of the
93959** record blob in register P3 against a prefix of the entry that
93960** the sorter cursor currently points to. Only the first P4 fields
93961** of r[P3] and the sorter record are compared.
93962**
93963** If either P3 or the sorter contains a NULL in one of their significant
93964** fields (not counting the P4 fields at the end which are ignored) then
93965** the comparison is assumed to be equal.
93966**
93967** Fall through to next instruction if the two records compare equal to
93968** each other. Jump to P2 if they are different.
93969*/
93970case OP_SorterCompare132: {
93971 VdbeCursor *pC;
93972 int res;
93973 int nKeyCol;
93974
93975 pC = p->apCsr[pOp->p1];
93976 assert( isSorter(pC) )((void) (0));
93977 assert( pOp->p4type==P4_INT32 )((void) (0));
93978 pIn3 = &aMem[pOp->p3];
93979 nKeyCol = pOp->p4.i;
93980 res = 0;
93981 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
93982 VdbeBranchTaken(res!=0,2);
93983 if( rc ) goto abort_due_to_error;
93984 if( res ) goto jump_to_p2;
93985 break;
93986};
93987
93988/* Opcode: SorterData P1 P2 P3 * *
93989** Synopsis: r[P2]=data
93990**
93991** Write into register P2 the current sorter data for sorter cursor P1.
93992** Then clear the column header cache on cursor P3.
93993**
93994** This opcode is normally use to move a record out of the sorter and into
93995** a register that is the source for a pseudo-table cursor created using
93996** OpenPseudo. That pseudo-table cursor is the one that is identified by
93997** parameter P3. Clearing the P3 column cache as part of this opcode saves
93998** us from having to issue a separate NullRow instruction to clear that cache.
93999*/
94000case OP_SorterData133: {
94001 VdbeCursor *pC;
94002
94003 pOut = &aMem[pOp->p2];
94004 pC = p->apCsr[pOp->p1];
94005 assert( isSorter(pC) )((void) (0));
94006 rc = sqlite3VdbeSorterRowkey(pC, pOut);
94007 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) )((void) (0));
94008 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
94009 if( rc ) goto abort_due_to_error;
94010 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE0;
94011 break;
94012}
94013
94014/* Opcode: RowData P1 P2 P3 * *
94015** Synopsis: r[P2]=data
94016**
94017** Write into register P2 the complete row content for the row at
94018** which cursor P1 is currently pointing.
94019** There is no interpretation of the data.
94020** It is just copied onto the P2 register exactly as
94021** it is found in the database file.
94022**
94023** If cursor P1 is an index, then the content is the key of the row.
94024** If cursor P2 is a table, then the content extracted is the data.
94025**
94026** If the P1 cursor must be pointing to a valid row (not a NULL row)
94027** of a real table, not a pseudo-table.
94028**
94029** If P3!=0 then this opcode is allowed to make an ephemeral pointer
94030** into the database page. That means that the content of the output
94031** register will be invalidated as soon as the cursor moves - including
94032** moves caused by other cursors that "save" the current cursors
94033** position in order that they can write to the same table. If P3==0
94034** then a copy of the data is made into memory. P3!=0 is faster, but
94035** P3==0 is safer.
94036**
94037** If P3!=0 then the content of the P2 register is unsuitable for use
94038** in OP_Result and any OP_Result will invalidate the P2 register content.
94039** The P2 register content is invalidated by opcodes like OP_Function or
94040** by any use of another cursor pointing to the same table.
94041*/
94042case OP_RowData134: {
94043 VdbeCursor *pC;
94044 BtCursor *pCrsr;
94045 u32 n;
94046
94047 pOut = out2Prerelease(p, pOp);
94048
94049 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
94050 pC = p->apCsr[pOp->p1];
94051 assert( pC!=0 )((void) (0));
94052 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
94053 assert( isSorter(pC)==0 )((void) (0));
94054 assert( pC->nullRow==0 )((void) (0));
94055 assert( pC->uc.pCursor!=0 )((void) (0));
94056 pCrsr = pC->uc.pCursor;
94057
94058 /* The OP_RowData opcodes always follow OP_NotExists or
94059 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
94060 ** that might invalidate the cursor.
94061 ** If this where not the case, on of the following assert()s
94062 ** would fail. Should this ever change (because of changes in the code
94063 ** generator) then the fix would be to insert a call to
94064 ** sqlite3VdbeCursorMoveto().
94065 */
94066 assert( pC->deferredMoveto==0 )((void) (0));
94067 assert( sqlite3BtreeCursorIsValid(pCrsr) )((void) (0));
94068
94069 n = sqlite3BtreePayloadSize(pCrsr);
94070 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH0] ){
94071 goto too_big;
94072 }
94073 testcase( n==0 );
94074 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCrsr, n, pOut);
94075 if( rc ) goto abort_due_to_error;
94076 if( !pOp->p3 ) Deephemeralize(pOut)if( ((pOut)->flags&0x4000)!=0 && sqlite3VdbeMemMakeWriteable
(pOut) ){ goto no_mem;}
;
94077 UPDATE_MAX_BLOBSIZE(pOut);
94078 REGISTER_TRACE(pOp->p2, pOut);
94079 break;
94080}
94081
94082/* Opcode: Rowid P1 P2 * * *
94083** Synopsis: r[P2]=PX rowid of P1
94084**
94085** Store in register P2 an integer which is the key of the table entry that
94086** P1 is currently point to.
94087**
94088** P1 can be either an ordinary table or a virtual table. There used to
94089** be a separate OP_VRowid opcode for use with virtual tables, but this
94090** one opcode now works for both table types.
94091*/
94092case OP_Rowid135: { /* out2 */
94093 VdbeCursor *pC;
94094 i64 v;
94095 sqlite3_vtab *pVtab;
94096 const sqlite3_module *pModule;
94097
94098 pOut = out2Prerelease(p, pOp);
94099 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
94100 pC = p->apCsr[pOp->p1];
94101 assert( pC!=0 )((void) (0));
94102 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow )((void) (0));
94103 if( pC->nullRow ){
94104 pOut->flags = MEM_Null0x0001;
94105 break;
94106 }else if( pC->deferredMoveto ){
94107 v = pC->movetoTarget;
94108#ifndef SQLITE_OMIT_VIRTUALTABLE
94109 }else if( pC->eCurType==CURTYPE_VTAB2 ){
94110 assert( pC->uc.pVCur!=0 )((void) (0));
94111 pVtab = pC->uc.pVCur->pVtab;
94112 pModule = pVtab->pModule;
94113 assert( pModule->xRowid )((void) (0));
94114 rc = pModule->xRowid(pC->uc.pVCur, &v);
94115 sqlite3VtabImportErrmsg(p, pVtab);
94116 if( rc ) goto abort_due_to_error;
94117#endif /* SQLITE_OMIT_VIRTUALTABLE */
94118 }else{
94119 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
94120 assert( pC->uc.pCursor!=0 )((void) (0));
94121 rc = sqlite3VdbeCursorRestore(pC);
94122 if( rc ) goto abort_due_to_error;
94123 if( pC->nullRow ){
94124 pOut->flags = MEM_Null0x0001;
94125 break;
94126 }
94127 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
94128 }
94129 pOut->u.i = v;
94130 break;
94131}
94132
94133/* Opcode: NullRow P1 * * * *
94134**
94135** Move the cursor P1 to a null row. Any OP_Column operations
94136** that occur while the cursor is on the null row will always
94137** write a NULL.
94138**
94139** If cursor P1 is not previously opened, open it now to a special
94140** pseudo-cursor that always returns NULL for every column.
94141*/
94142case OP_NullRow136: {
94143 VdbeCursor *pC;
94144
94145 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
94146 pC = p->apCsr[pOp->p1];
94147 if( pC==0 ){
94148 /* If the cursor is not already open, create a special kind of
94149 ** pseudo-cursor that always gives null rows. */
94150 pC = allocateCursor(p, pOp->p1, 1, CURTYPE_PSEUDO3);
94151 if( pC==0 ) goto no_mem;
94152 pC->seekResult = 0;
94153 pC->isTable = 1;
94154 pC->noReuse = 1;
94155 pC->uc.pCursor = sqlite3BtreeFakeValidCursor();
94156 }
94157 pC->nullRow = 1;
94158 pC->cacheStatus = CACHE_STALE0;
94159 if( pC->eCurType==CURTYPE_BTREE0 ){
94160 assert( pC->uc.pCursor!=0 )((void) (0));
94161 sqlite3BtreeClearCursor(pC->uc.pCursor);
94162 }
94163#ifdef SQLITE_DEBUG
94164 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow136;
94165#endif
94166 break;
94167}
94168
94169/* Opcode: SeekEnd P1 * * * *
94170**
94171** Position cursor P1 at the end of the btree for the purpose of
94172** appending a new entry onto the btree.
94173**
94174** It is assumed that the cursor is used only for appending and so
94175** if the cursor is valid, then the cursor must already be pointing
94176** at the end of the btree and so no changes are made to
94177** the cursor.
94178*/
94179/* Opcode: Last P1 P2 * * *
94180**
94181** The next use of the Rowid or Column or Prev instruction for P1
94182** will refer to the last entry in the database table or index.
94183** If the table or index is empty and P2>0, then jump immediately to P2.
94184** If P2 is 0 or if the table or index is not empty, fall through
94185** to the following instruction.
94186**
94187** This opcode leaves the cursor configured to move in reverse order,
94188** from the end toward the beginning. In other words, the cursor is
94189** configured to use Prev, not Next.
94190*/
94191case OP_SeekEnd137:
94192case OP_Last31: { /* jump */
94193 VdbeCursor *pC;
94194 BtCursor *pCrsr;
94195 int res;
94196
94197 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
94198 pC = p->apCsr[pOp->p1];
94199 assert( pC!=0 )((void) (0));
94200 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
94201 pCrsr = pC->uc.pCursor;
94202 res = 0;
94203 assert( pCrsr!=0 )((void) (0));
94204#ifdef SQLITE_DEBUG
94205 pC->seekOp = pOp->opcode;
94206#endif
94207 if( pOp->opcode==OP_SeekEnd137 ){
94208 assert( pOp->p2==0 )((void) (0));
94209 pC->seekResult = -1;
94210 if( sqlite3BtreeCursorIsValidNN(pCrsr) ){
94211 break;
94212 }
94213 }
94214 rc = sqlite3BtreeLast(pCrsr, &res);
94215 pC->nullRow = (u8)res;
94216 pC->deferredMoveto = 0;
94217 pC->cacheStatus = CACHE_STALE0;
94218 if( rc ) goto abort_due_to_error;
94219 if( pOp->p2>0 ){
94220 VdbeBranchTaken(res!=0,2);
94221 if( res ) goto jump_to_p2;
94222 }
94223 break;
94224}
94225
94226/* Opcode: IfSmaller P1 P2 P3 * *
94227**
94228** Estimate the number of rows in the table P1. Jump to P2 if that
94229** estimate is less than approximately 2**(0.1*P3).
94230*/
94231case OP_IfSmaller32: { /* jump */
94232 VdbeCursor *pC;
94233 BtCursor *pCrsr;
94234 int res;
94235 i64 sz;
94236
94237 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
94238 pC = p->apCsr[pOp->p1];
94239 assert( pC!=0 )((void) (0));
94240 pCrsr = pC->uc.pCursor;
94241 assert( pCrsr )((void) (0));
94242 rc = sqlite3BtreeFirst(pCrsr, &res);
94243 if( rc ) goto abort_due_to_error;
94244 if( res==0 ){
94245 sz = sqlite3BtreeRowCountEst(pCrsr);
94246 if( ALWAYS(sz>=0)(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
94247 }
94248 VdbeBranchTaken(res!=0,2);
94249 if( res ) goto jump_to_p2;
94250 break;
94251}
94252
94253
94254/* Opcode: SorterSort P1 P2 * * *
94255**
94256** After all records have been inserted into the Sorter object
94257** identified by P1, invoke this opcode to actually do the sorting.
94258** Jump to P2 if there are no records to be sorted.
94259**
94260** This opcode is an alias for OP_Sort and OP_Rewind that is used
94261** for Sorter objects.
94262*/
94263/* Opcode: Sort P1 P2 * * *
94264**
94265** This opcode does exactly the same thing as OP_Rewind except that
94266** it increments an undocumented global variable used for testing.
94267**
94268** Sorting is accomplished by writing records into a sorting index,
94269** then rewinding that index and playing it back from beginning to
94270** end. We use the OP_Sort opcode instead of OP_Rewind to do the
94271** rewinding so that the global variable will be incremented and
94272** regression tests can determine whether or not the optimizer is
94273** correctly optimizing out sorts.
94274*/
94275case OP_SorterSort33: /* jump */
94276case OP_Sort34: { /* jump */
94277#ifdef SQLITE_TEST
94278 sqlite3_sort_count++;
94279 sqlite3_search_count--;
94280#endif
94281 p->aCounter[SQLITE_STMTSTATUS_SORT2]++;
94282 /* Fall through into OP_Rewind */
94283 /* no break */ deliberate_fall_through
94284}
94285/* Opcode: Rewind P1 P2 * * *
94286**
94287** The next use of the Rowid or Column or Next instruction for P1
94288** will refer to the first entry in the database table or index.
94289** If the table or index is empty, jump immediately to P2.
94290** If the table or index is not empty, fall through to the following
94291** instruction.
94292**
94293** This opcode leaves the cursor configured to move in forward order,
94294** from the beginning toward the end. In other words, the cursor is
94295** configured to use Next, not Prev.
94296*/
94297case OP_Rewind35: { /* jump */
94298 VdbeCursor *pC;
94299 BtCursor *pCrsr;
94300 int res;
94301
94302 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
94303 assert( pOp->p5==0 )((void) (0));
94304 pC = p->apCsr[pOp->p1];
94305 assert( pC!=0 )((void) (0));
94306 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) )((void) (0));
94307 res = 1;
94308#ifdef SQLITE_DEBUG
94309 pC->seekOp = OP_Rewind35;
94310#endif
94311 if( isSorter(pC)((pC)->eCurType==1) ){
94312 rc = sqlite3VdbeSorterRewind(pC, &res);
94313 }else{
94314 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
94315 pCrsr = pC->uc.pCursor;
94316 assert( pCrsr )((void) (0));
94317 rc = sqlite3BtreeFirst(pCrsr, &res);
94318 pC->deferredMoveto = 0;
94319 pC->cacheStatus = CACHE_STALE0;
94320 }
94321 if( rc ) goto abort_due_to_error;
94322 pC->nullRow = (u8)res;
94323 assert( pOp->p2>0 && pOp->p2<p->nOp )((void) (0));
94324 VdbeBranchTaken(res!=0,2);
94325 if( res ) goto jump_to_p2;
94326 break;
94327}
94328
94329/* Opcode: Next P1 P2 P3 * P5
94330**
94331** Advance cursor P1 so that it points to the next key/data pair in its
94332** table or index. If there are no more key/value pairs then fall through
94333** to the following instruction. But if the cursor advance was successful,
94334** jump immediately to P2.
94335**
94336** The Next opcode is only valid following an SeekGT, SeekGE, or
94337** OP_Rewind opcode used to position the cursor. Next is not allowed
94338** to follow SeekLT, SeekLE, or OP_Last.
94339**
94340** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
94341** been opened prior to this opcode or the program will segfault.
94342**
94343** The P3 value is a hint to the btree implementation. If P3==1, that
94344** means P1 is an SQL index and that this instruction could have been
94345** omitted if that index had been unique. P3 is usually 0. P3 is
94346** always either 0 or 1.
94347**
94348** If P5 is positive and the jump is taken, then event counter
94349** number P5-1 in the prepared statement is incremented.
94350**
94351** See also: Prev
94352*/
94353/* Opcode: Prev P1 P2 P3 * P5
94354**
94355** Back up cursor P1 so that it points to the previous key/data pair in its
94356** table or index. If there is no previous key/value pairs then fall through
94357** to the following instruction. But if the cursor backup was successful,
94358** jump immediately to P2.
94359**
94360**
94361** The Prev opcode is only valid following an SeekLT, SeekLE, or
94362** OP_Last opcode used to position the cursor. Prev is not allowed
94363** to follow SeekGT, SeekGE, or OP_Rewind.
94364**
94365** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
94366** not open then the behavior is undefined.
94367**
94368** The P3 value is a hint to the btree implementation. If P3==1, that
94369** means P1 is an SQL index and that this instruction could have been
94370** omitted if that index had been unique. P3 is usually 0. P3 is
94371** always either 0 or 1.
94372**
94373** If P5 is positive and the jump is taken, then event counter
94374** number P5-1 in the prepared statement is incremented.
94375*/
94376/* Opcode: SorterNext P1 P2 * * P5
94377**
94378** This opcode works just like OP_Next except that P1 must be a
94379** sorter object for which the OP_SorterSort opcode has been
94380** invoked. This opcode advances the cursor to the next sorted
94381** record, or jumps to P2 if there are no more sorted records.
94382*/
94383case OP_SorterNext36: { /* jump */
94384 VdbeCursor *pC;
94385
94386 pC = p->apCsr[pOp->p1];
94387 assert( isSorter(pC) )((void) (0));
94388 rc = sqlite3VdbeSorterNext(db, pC);
94389 goto next_tail;
94390
94391case OP_Prev37: /* jump */
94392 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
94393 assert( pOp->p5<ArraySize(p->aCounter) )((void) (0));
94394 pC = p->apCsr[pOp->p1];
94395 assert( pC!=0 )((void) (0));
94396 assert( pC->deferredMoveto==0 )((void) (0));
94397 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
94398 assert( pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE((void) (0))
94399 || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope((void) (0))
94400 || pC->seekOp==OP_NullRow)((void) (0));
94401 rc = sqlite3BtreePrevious(pC->uc.pCursor, pOp->p3);
94402 goto next_tail;
94403
94404case OP_Next38: /* jump */
94405 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
94406 assert( pOp->p5<ArraySize(p->aCounter) )((void) (0));
94407 pC = p->apCsr[pOp->p1];
94408 assert( pC!=0 )((void) (0));
94409 assert( pC->deferredMoveto==0 )((void) (0));
94410 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
94411 assert( pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE((void) (0))
94412 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found((void) (0))
94413 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid((void) (0))
94414 || pC->seekOp==OP_IfNoHope)((void) (0));
94415 rc = sqlite3BtreeNext(pC->uc.pCursor, pOp->p3);
94416
94417next_tail:
94418 pC->cacheStatus = CACHE_STALE0;
94419 VdbeBranchTaken(rc==SQLITE_OK,2);
94420 if( rc==SQLITE_OK0 ){
94421 pC->nullRow = 0;
94422 p->aCounter[pOp->p5]++;
94423#ifdef SQLITE_TEST
94424 sqlite3_search_count++;
94425#endif
94426 goto jump_to_p2_and_check_for_interrupt;
94427 }
94428 if( rc!=SQLITE_DONE101 ) goto abort_due_to_error;
94429 rc = SQLITE_OK0;
94430 pC->nullRow = 1;
94431 goto check_for_interrupt;
94432}
94433
94434/* Opcode: IdxInsert P1 P2 P3 P4 P5
94435** Synopsis: key=r[P2]
94436**
94437** Register P2 holds an SQL index key made using the
94438** MakeRecord instructions. This opcode writes that key
94439** into the index P1. Data for the entry is nil.
94440**
94441** If P4 is not zero, then it is the number of values in the unpacked
94442** key of reg(P2). In that case, P3 is the index of the first register
94443** for the unpacked key. The availability of the unpacked key can sometimes
94444** be an optimization.
94445**
94446** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
94447** that this insert is likely to be an append.
94448**
94449** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
94450** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
94451** then the change counter is unchanged.
94452**
94453** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
94454** run faster by avoiding an unnecessary seek on cursor P1. However,
94455** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
94456** seeks on the cursor or if the most recent seek used a key equivalent
94457** to P2.
94458**
94459** This instruction only works for indices. The equivalent instruction
94460** for tables is OP_Insert.
94461*/
94462case OP_IdxInsert138: { /* in2 */
94463 VdbeCursor *pC;
94464 BtreePayload x;
94465
94466 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
94467 pC = p->apCsr[pOp->p1];
94468 sqlite3VdbeIncrWriteCounter(p, pC);
94469 assert( pC!=0 )((void) (0));
94470 assert( !isSorter(pC) )((void) (0));
94471 pIn2 = &aMem[pOp->p2];
94472 assert( (pIn2->flags & MEM_Blob) || (pOp->p5 & OPFLAG_PREFORMAT) )((void) (0));
94473 if( pOp->p5 & OPFLAG_NCHANGE0x01 ) p->nChange++;
94474 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
94475 assert( pC->isTable==0 )((void) (0));
94476 rc = ExpandBlob(pIn2)(((pIn2)->flags&0x0400)?sqlite3VdbeMemExpandBlob(pIn2)
:0)
;
94477 if( rc ) goto abort_due_to_error;
94478 x.nKey = pIn2->n;
94479 x.pKey = pIn2->z;
94480 x.aMem = aMem + pOp->p3;
94481 x.nMem = (u16)pOp->p4.i;
94482 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
94483 (pOp->p5 & (OPFLAG_APPEND0x08|OPFLAG_SAVEPOSITION0x02|OPFLAG_PREFORMAT0x80)),
94484 ((pOp->p5 & OPFLAG_USESEEKRESULT0x10) ? pC->seekResult : 0)
94485 );
94486 assert( pC->deferredMoveto==0 )((void) (0));
94487 pC->cacheStatus = CACHE_STALE0;
94488 if( rc) goto abort_due_to_error;
94489 break;
94490}
94491
94492/* Opcode: SorterInsert P1 P2 * * *
94493** Synopsis: key=r[P2]
94494**
94495** Register P2 holds an SQL index key made using the
94496** MakeRecord instructions. This opcode writes that key
94497** into the sorter P1. Data for the entry is nil.
94498*/
94499case OP_SorterInsert139: { /* in2 */
94500 VdbeCursor *pC;
94501
94502 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
94503 pC = p->apCsr[pOp->p1];
94504 sqlite3VdbeIncrWriteCounter(p, pC);
94505 assert( pC!=0 )((void) (0));
94506 assert( isSorter(pC) )((void) (0));
94507 pIn2 = &aMem[pOp->p2];
94508 assert( pIn2->flags & MEM_Blob )((void) (0));
94509 assert( pC->isTable==0 )((void) (0));
94510 rc = ExpandBlob(pIn2)(((pIn2)->flags&0x0400)?sqlite3VdbeMemExpandBlob(pIn2)
:0)
;
94511 if( rc ) goto abort_due_to_error;
94512 rc = sqlite3VdbeSorterWrite(pC, pIn2);
94513 if( rc) goto abort_due_to_error;
94514 break;
94515}
94516
94517/* Opcode: IdxDelete P1 P2 P3 * P5
94518** Synopsis: key=r[P2@P3]
94519**
94520** The content of P3 registers starting at register P2 form
94521** an unpacked index key. This opcode removes that entry from the
94522** index opened by cursor P1.
94523**
94524** If P5 is not zero, then raise an SQLITE_CORRUPT_INDEX error
94525** if no matching index entry is found. This happens when running
94526** an UPDATE or DELETE statement and the index entry to be updated
94527** or deleted is not found. For some uses of IdxDelete
94528** (example: the EXCEPT operator) it does not matter that no matching
94529** entry is found. For those cases, P5 is zero. Also, do not raise
94530** this (self-correcting and non-critical) error if in writable_schema mode.
94531*/
94532case OP_IdxDelete140: {
94533 VdbeCursor *pC;
94534 BtCursor *pCrsr;
94535 int res;
94536 UnpackedRecord r;
94537
94538 assert( pOp->p3>0 )((void) (0));
94539 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
94540 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
94541 pC = p->apCsr[pOp->p1];
94542 assert( pC!=0 )((void) (0));
94543 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
94544 sqlite3VdbeIncrWriteCounter(p, pC);
94545 pCrsr = pC->uc.pCursor;
94546 assert( pCrsr!=0 )((void) (0));
94547 r.pKeyInfo = pC->pKeyInfo;
94548 r.nField = (u16)pOp->p3;
94549 r.default_rc = 0;
94550 r.aMem = &aMem[pOp->p2];
94551 rc = sqlite3BtreeIndexMoveto(pCrsr, &r, &res);
94552 if( rc ) goto abort_due_to_error;
94553 if( res==0 ){
94554 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE0x04);
94555 if( rc ) goto abort_due_to_error;
94556 }else if( pOp->p5 && !sqlite3WritableSchema(db) ){
94557 rc = sqlite3ReportError(SQLITE_CORRUPT_INDEX(11 | (3<<8)), __LINE__94557, "index corruption");
94558 goto abort_due_to_error;
94559 }
94560 assert( pC->deferredMoveto==0 )((void) (0));
94561 pC->cacheStatus = CACHE_STALE0;
94562 pC->seekResult = 0;
94563 break;
94564}
94565
94566/* Opcode: DeferredSeek P1 * P3 P4 *
94567** Synopsis: Move P3 to P1.rowid if needed
94568**
94569** P1 is an open index cursor and P3 is a cursor on the corresponding
94570** table. This opcode does a deferred seek of the P3 table cursor
94571** to the row that corresponds to the current row of P1.
94572**
94573** This is a deferred seek. Nothing actually happens until
94574** the cursor is used to read a record. That way, if no reads
94575** occur, no unnecessary I/O happens.
94576**
94577** P4 may be an array of integers (type P4_INTARRAY) containing
94578** one entry for each column in the P3 table. If array entry a(i)
94579** is non-zero, then reading column a(i)-1 from cursor P3 is
94580** equivalent to performing the deferred seek and then reading column i
94581** from P1. This information is stored in P3 and used to redirect
94582** reads against P3 over to P1, thus possibly avoiding the need to
94583** seek and read cursor P3.
94584*/
94585/* Opcode: IdxRowid P1 P2 * * *
94586** Synopsis: r[P2]=rowid
94587**
94588** Write into register P2 an integer which is the last entry in the record at
94589** the end of the index key pointed to by cursor P1. This integer should be
94590** the rowid of the table entry to which this index entry points.
94591**
94592** See also: Rowid, MakeRecord.
94593*/
94594case OP_DeferredSeek141:
94595case OP_IdxRowid142: { /* out2 */
94596 VdbeCursor *pC; /* The P1 index cursor */
94597 VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */
94598 i64 rowid; /* Rowid that P1 current points to */
94599
94600 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
94601 pC = p->apCsr[pOp->p1];
94602 assert( pC!=0 )((void) (0));
94603 assert( pC->eCurType==CURTYPE_BTREE || IsNullCursor(pC) )((void) (0));
94604 assert( pC->uc.pCursor!=0 )((void) (0));
94605 assert( pC->isTable==0 || IsNullCursor(pC) )((void) (0));
94606 assert( pC->deferredMoveto==0 )((void) (0));
94607 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid )((void) (0));
94608
94609 /* The IdxRowid and Seek opcodes are combined because of the commonality
94610 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
94611 rc = sqlite3VdbeCursorRestore(pC);
94612
94613 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
94614 ** out from under the cursor. That will never happens for an IdxRowid
94615 ** or Seek opcode */
94616 if( NEVER(rc!=SQLITE_OK)(rc!=0) ) goto abort_due_to_error;
94617
94618 if( !pC->nullRow ){
94619 rowid = 0; /* Not needed. Only used to silence a warning. */
94620 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
94621 if( rc!=SQLITE_OK0 ){
94622 goto abort_due_to_error;
94623 }
94624 if( pOp->opcode==OP_DeferredSeek141 ){
94625 assert( pOp->p3>=0 && pOp->p3<p->nCursor )((void) (0));
94626 pTabCur = p->apCsr[pOp->p3];
94627 assert( pTabCur!=0 )((void) (0));
94628 assert( pTabCur->eCurType==CURTYPE_BTREE )((void) (0));
94629 assert( pTabCur->uc.pCursor!=0 )((void) (0));
94630 assert( pTabCur->isTable )((void) (0));
94631 pTabCur->nullRow = 0;
94632 pTabCur->movetoTarget = rowid;
94633 pTabCur->deferredMoveto = 1;
94634 pTabCur->cacheStatus = CACHE_STALE0;
94635 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 )((void) (0));
94636 assert( !pTabCur->isEphemeral )((void) (0));
94637 pTabCur->ub.aAltMap = pOp->p4.ai;
94638 assert( !pC->isEphemeral )((void) (0));
94639 pTabCur->pAltCursor = pC;
94640 }else{
94641 pOut = out2Prerelease(p, pOp);
94642 pOut->u.i = rowid;
94643 }
94644 }else{
94645 assert( pOp->opcode==OP_IdxRowid )((void) (0));
94646 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
94647 }
94648 break;
94649}
94650
94651/* Opcode: FinishSeek P1 * * * *
94652**
94653** If cursor P1 was previously moved via OP_DeferredSeek, complete that
94654** seek operation now, without further delay. If the cursor seek has
94655** already occurred, this instruction is a no-op.
94656*/
94657case OP_FinishSeek143: {
94658 VdbeCursor *pC; /* The P1 index cursor */
94659
94660 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
94661 pC = p->apCsr[pOp->p1];
94662 if( pC->deferredMoveto ){
94663 rc = sqlite3VdbeFinishMoveto(pC);
94664 if( rc ) goto abort_due_to_error;
94665 }
94666 break;
94667}
94668
94669/* Opcode: IdxGE P1 P2 P3 P4 *
94670** Synopsis: key=r[P3@P4]
94671**
94672** The P4 register values beginning with P3 form an unpacked index
94673** key that omits the PRIMARY KEY. Compare this key value against the index
94674** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
94675** fields at the end.
94676**
94677** If the P1 index entry is greater than or equal to the key value
94678** then jump to P2. Otherwise fall through to the next instruction.
94679*/
94680/* Opcode: IdxGT P1 P2 P3 P4 *
94681** Synopsis: key=r[P3@P4]
94682**
94683** The P4 register values beginning with P3 form an unpacked index
94684** key that omits the PRIMARY KEY. Compare this key value against the index
94685** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
94686** fields at the end.
94687**
94688** If the P1 index entry is greater than the key value
94689** then jump to P2. Otherwise fall through to the next instruction.
94690*/
94691/* Opcode: IdxLT P1 P2 P3 P4 *
94692** Synopsis: key=r[P3@P4]
94693**
94694** The P4 register values beginning with P3 form an unpacked index
94695** key that omits the PRIMARY KEY or ROWID. Compare this key value against
94696** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
94697** ROWID on the P1 index.
94698**
94699** If the P1 index entry is less than the key value then jump to P2.
94700** Otherwise fall through to the next instruction.
94701*/
94702/* Opcode: IdxLE P1 P2 P3 P4 *
94703** Synopsis: key=r[P3@P4]
94704**
94705** The P4 register values beginning with P3 form an unpacked index
94706** key that omits the PRIMARY KEY or ROWID. Compare this key value against
94707** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
94708** ROWID on the P1 index.
94709**
94710** If the P1 index entry is less than or equal to the key value then jump
94711** to P2. Otherwise fall through to the next instruction.
94712*/
94713case OP_IdxLE39: /* jump */
94714case OP_IdxGT40: /* jump */
94715case OP_IdxLT41: /* jump */
94716case OP_IdxGE42: { /* jump */
94717 VdbeCursor *pC;
94718 int res;
94719 UnpackedRecord r;
94720
94721 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
94722 pC = p->apCsr[pOp->p1];
94723 assert( pC!=0 )((void) (0));
94724 assert( pC->isOrdered )((void) (0));
94725 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
94726 assert( pC->uc.pCursor!=0)((void) (0));
94727 assert( pC->deferredMoveto==0 )((void) (0));
94728 assert( pOp->p4type==P4_INT32 )((void) (0));
94729 r.pKeyInfo = pC->pKeyInfo;
94730 r.nField = (u16)pOp->p4.i;
94731 if( pOp->opcode<OP_IdxLT41 ){
94732 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT )((void) (0));
94733 r.default_rc = -1;
94734 }else{
94735 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT )((void) (0));
94736 r.default_rc = 0;
94737 }
94738 r.aMem = &aMem[pOp->p3];
94739#ifdef SQLITE_DEBUG
94740 {
94741 int i;
94742 for(i=0; i<r.nField; i++){
94743 assert( memIsValid(&r.aMem[i]) )((void) (0));
94744 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
94745 }
94746 }
94747#endif
94748
94749 /* Inlined version of sqlite3VdbeIdxKeyCompare() */
94750 {
94751 i64 nCellKey = 0;
94752 BtCursor *pCur;
94753 Mem m;
94754
94755 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
94756 pCur = pC->uc.pCursor;
94757 assert( sqlite3BtreeCursorIsValid(pCur) )((void) (0));
94758 nCellKey = sqlite3BtreePayloadSize(pCur);
94759 /* nCellKey will always be between 0 and 0xffffffff because of the way
94760 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
94761 if( nCellKey<=0 || nCellKey>0x7fffffff ){
94762 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(94762);
94763 goto abort_due_to_error;
94764 }
94765 sqlite3VdbeMemInit(&m, db, 0);
94766 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m);
94767 if( rc ) goto abort_due_to_error;
94768 res = sqlite3VdbeRecordCompareWithSkip(m.n, m.z, &r, 0);
94769 sqlite3VdbeMemReleaseMalloc(&m);
94770 }
94771 /* End of inlined sqlite3VdbeIdxKeyCompare() */
94772
94773 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) )((void) (0));
94774 if( (pOp->opcode&1)==(OP_IdxLT41&1) ){
94775 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT )((void) (0));
94776 res = -res;
94777 }else{
94778 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT )((void) (0));
94779 res++;
94780 }
94781 VdbeBranchTaken(res>0,2);
94782 assert( rc==SQLITE_OK )((void) (0));
94783 if( res>0 ) goto jump_to_p2;
94784 break;
94785}
94786
94787/* Opcode: Destroy P1 P2 P3 * *
94788**
94789** Delete an entire database table or index whose root page in the database
94790** file is given by P1.
94791**
94792** The table being destroyed is in the main database file if P3==0. If
94793** P3==1 then the table to be clear is in the auxiliary database file
94794** that is used to store tables create using CREATE TEMPORARY TABLE.
94795**
94796** If AUTOVACUUM is enabled then it is possible that another root page
94797** might be moved into the newly deleted root page in order to keep all
94798** root pages contiguous at the beginning of the database. The former
94799** value of the root page that moved - its value before the move occurred -
94800** is stored in register P2. If no page movement was required (because the
94801** table being dropped was already the last one in the database) then a
94802** zero is stored in register P2. If AUTOVACUUM is disabled then a zero
94803** is stored in register P2.
94804**
94805** This opcode throws an error if there are any active reader VMs when
94806** it is invoked. This is done to avoid the difficulty associated with
94807** updating existing cursors when a root page is moved in an AUTOVACUUM
94808** database. This error is thrown even if the database is not an AUTOVACUUM
94809** db in order to avoid introducing an incompatibility between autovacuum
94810** and non-autovacuum modes.
94811**
94812** See also: Clear
94813*/
94814case OP_Destroy144: { /* out2 */
94815 int iMoved;
94816 int iDb;
94817
94818 sqlite3VdbeIncrWriteCounter(p, 0);
94819 assert( p->readOnly==0 )((void) (0));
94820 assert( pOp->p1>1 )((void) (0));
94821 pOut = out2Prerelease(p, pOp);
94822 pOut->flags = MEM_Null0x0001;
94823 if( db->nVdbeRead > db->nVDestroy+1 ){
94824 rc = SQLITE_LOCKED6;
94825 p->errorAction = OE_Abort2;
94826 goto abort_due_to_error;
94827 }else{
94828 iDb = pOp->p3;
94829 assert( DbMaskTest(p->btreeMask, iDb) )((void) (0));
94830 iMoved = 0; /* Not needed. Only to silence a warning. */
94831 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
94832 pOut->flags = MEM_Int0x0004;
94833 pOut->u.i = iMoved;
94834 if( rc ) goto abort_due_to_error;
94835#ifndef SQLITE_OMIT_AUTOVACUUM
94836 if( iMoved!=0 ){
94837 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
94838 /* All OP_Destroy operations occur on the same btree */
94839 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 )((void) (0));
94840 resetSchemaOnFault = iDb+1;
94841 }
94842#endif
94843 }
94844 break;
94845}
94846
94847/* Opcode: Clear P1 P2 P3
94848**
94849** Delete all contents of the database table or index whose root page
94850** in the database file is given by P1. But, unlike Destroy, do not
94851** remove the table or index from the database file.
94852**
94853** The table being clear is in the main database file if P2==0. If
94854** P2==1 then the table to be clear is in the auxiliary database file
94855** that is used to store tables create using CREATE TEMPORARY TABLE.
94856**
94857** If the P3 value is non-zero, then the row change count is incremented
94858** by the number of rows in the table being cleared. If P3 is greater
94859** than zero, then the value stored in register P3 is also incremented
94860** by the number of rows in the table being cleared.
94861**
94862** See also: Destroy
94863*/
94864case OP_Clear145: {
94865 i64 nChange;
94866
94867 sqlite3VdbeIncrWriteCounter(p, 0);
94868 nChange = 0;
94869 assert( p->readOnly==0 )((void) (0));
94870 assert( DbMaskTest(p->btreeMask, pOp->p2) )((void) (0));
94871 rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, (u32)pOp->p1, &nChange);
94872 if( pOp->p3 ){
94873 p->nChange += nChange;
94874 if( pOp->p3>0 ){
94875 assert( memIsValid(&aMem[pOp->p3]) )((void) (0));
94876 memAboutToChange(p, &aMem[pOp->p3]);
94877 aMem[pOp->p3].u.i += nChange;
94878 }
94879 }
94880 if( rc ) goto abort_due_to_error;
94881 break;
94882}
94883
94884/* Opcode: ResetSorter P1 * * * *
94885**
94886** Delete all contents from the ephemeral table or sorter
94887** that is open on cursor P1.
94888**
94889** This opcode only works for cursors used for sorting and
94890** opened with OP_OpenEphemeral or OP_SorterOpen.
94891*/
94892case OP_ResetSorter146: {
94893 VdbeCursor *pC;
94894
94895 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
94896 pC = p->apCsr[pOp->p1];
94897 assert( pC!=0 )((void) (0));
94898 if( isSorter(pC)((pC)->eCurType==1) ){
94899 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
94900 }else{
94901 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
94902 assert( pC->isEphemeral )((void) (0));
94903 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
94904 if( rc ) goto abort_due_to_error;
94905 }
94906 break;
94907}
94908
94909/* Opcode: CreateBtree P1 P2 P3 * *
94910** Synopsis: r[P2]=root iDb=P1 flags=P3
94911**
94912** Allocate a new b-tree in the main database file if P1==0 or in the
94913** TEMP database file if P1==1 or in an attached database if
94914** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
94915** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
94916** The root page number of the new b-tree is stored in register P2.
94917*/
94918case OP_CreateBtree147: { /* out2 */
94919 Pgno pgno;
94920 Db *pDb;
94921
94922 sqlite3VdbeIncrWriteCounter(p, 0);
94923 pOut = out2Prerelease(p, pOp);
94924 pgno = 0;
94925 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY )((void) (0));
94926 assert( pOp->p1>=0 && pOp->p1<db->nDb )((void) (0));
94927 assert( DbMaskTest(p->btreeMask, pOp->p1) )((void) (0));
94928 assert( p->readOnly==0 )((void) (0));
94929 pDb = &db->aDb[pOp->p1];
94930 assert( pDb->pBt!=0 )((void) (0));
94931 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
94932 if( rc ) goto abort_due_to_error;
94933 pOut->u.i = pgno;
94934 break;
94935}
94936
94937/* Opcode: SqlExec * * * P4 *
94938**
94939** Run the SQL statement or statements specified in the P4 string.
94940*/
94941case OP_SqlExec148: {
94942 sqlite3VdbeIncrWriteCounter(p, 0);
94943 db->nSqlExec++;
94944 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
94945 db->nSqlExec--;
94946 if( rc ) goto abort_due_to_error;
94947 break;
94948}
94949
94950/* Opcode: ParseSchema P1 * * P4 *
94951**
94952** Read and parse all entries from the schema table of database P1
94953** that match the WHERE clause P4. If P4 is a NULL pointer, then the
94954** entire schema for P1 is reparsed.
94955**
94956** This opcode invokes the parser to create a new virtual machine,
94957** then runs the new virtual machine. It is thus a re-entrant opcode.
94958*/
94959case OP_ParseSchema149: {
94960 int iDb;
94961 const char *zSchema;
94962 char *zSql;
94963 InitData initData;
94964
94965 /* Any prepared statement that invokes this opcode will hold mutexes
94966 ** on every btree. This is a prerequisite for invoking
94967 ** sqlite3InitCallback().
94968 */
94969#ifdef SQLITE_DEBUG
94970 for(iDb=0; iDb<db->nDb; iDb++){
94971 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) )((void) (0));
94972 }
94973#endif
94974
94975 iDb = pOp->p1;
94976 assert( iDb>=0 && iDb<db->nDb )((void) (0));
94977 assert( DbHasProperty(db, iDb, DB_SchemaLoaded)((void) (0))
94978 || db->mallocFailed((void) (0))
94979 || (CORRUPT_DB && (db->flags & SQLITE_NoSchemaError)!=0) )((void) (0));
94980
94981#ifndef SQLITE_OMIT_ALTERTABLE
94982 if( pOp->p4.z==0 ){
94983 sqlite3SchemaClear(db->aDb[iDb].pSchema);
94984 db->mDbFlags &= ~DBFLAG_SchemaKnownOk0x0010;
94985 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, pOp->p5);
94986 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
94987 p->expired = 0;
94988 }else
94989#endif
94990 {
94991 zSchema = LEGACY_SCHEMA_TABLE"sqlite_master";
94992 initData.db = db;
94993 initData.iDb = iDb;
94994 initData.pzErrMsg = &p->zErrMsg;
94995 initData.mInitFlags = 0;
94996 initData.mxPage = sqlite3BtreeLastPage(db->aDb[iDb].pBt);
94997 zSql = sqlite3MPrintf(db,
94998 "SELECT*FROM\"%w\".%s WHERE %s ORDER BY rowid",
94999 db->aDb[iDb].zDbSName, zSchema, pOp->p4.z);
95000 if( zSql==0 ){
95001 rc = SQLITE_NOMEM_BKPT7;
95002 }else{
95003 assert( db->init.busy==0 )((void) (0));
95004 db->init.busy = 1;
95005 initData.rc = SQLITE_OK0;
95006 initData.nInitRow = 0;
95007 assert( !db->mallocFailed )((void) (0));
95008 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
95009 if( rc==SQLITE_OK0 ) rc = initData.rc;
95010 if( rc==SQLITE_OK0 && initData.nInitRow==0 ){
95011 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
95012 ** at least one SQL statement. Any less than that indicates that
95013 ** the sqlite_schema table is corrupt. */
95014 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(95014);
95015 }
95016 sqlite3DbFreeNN(db, zSql);
95017 db->init.busy = 0;
95018 }
95019 }
95020 if( rc ){
95021 sqlite3ResetAllSchemasOfConnection(db);
95022 if( rc==SQLITE_NOMEM7 ){
95023 goto no_mem;
95024 }
95025 goto abort_due_to_error;
95026 }
95027 break;
95028}
95029
95030#if !defined(SQLITE_OMIT_ANALYZE)
95031/* Opcode: LoadAnalysis P1 * * * *
95032**
95033** Read the sqlite_stat1 table for database P1 and load the content
95034** of that table into the internal index hash table. This will cause
95035** the analysis to be used when preparing all subsequent queries.
95036*/
95037case OP_LoadAnalysis150: {
95038 assert( pOp->p1>=0 && pOp->p1<db->nDb )((void) (0));
95039 rc = sqlite3AnalysisLoad(db, pOp->p1);
95040 if( rc ) goto abort_due_to_error;
95041 break;
95042}
95043#endif /* !defined(SQLITE_OMIT_ANALYZE) */
95044
95045/* Opcode: DropTable P1 * * P4 *
95046**
95047** Remove the internal (in-memory) data structures that describe
95048** the table named P4 in database P1. This is called after a table
95049** is dropped from disk (using the Destroy opcode) in order to keep
95050** the internal representation of the
95051** schema consistent with what is on disk.
95052*/
95053case OP_DropTable151: {
95054 sqlite3VdbeIncrWriteCounter(p, 0);
95055 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
95056 break;
95057}
95058
95059/* Opcode: DropIndex P1 * * P4 *
95060**
95061** Remove the internal (in-memory) data structures that describe
95062** the index named P4 in database P1. This is called after an index
95063** is dropped from disk (using the Destroy opcode)
95064** in order to keep the internal representation of the
95065** schema consistent with what is on disk.
95066*/
95067case OP_DropIndex152: {
95068 sqlite3VdbeIncrWriteCounter(p, 0);
95069 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
95070 break;
95071}
95072
95073/* Opcode: DropTrigger P1 * * P4 *
95074**
95075** Remove the internal (in-memory) data structures that describe
95076** the trigger named P4 in database P1. This is called after a trigger
95077** is dropped from disk (using the Destroy opcode) in order to keep
95078** the internal representation of the
95079** schema consistent with what is on disk.
95080*/
95081case OP_DropTrigger154: {
95082 sqlite3VdbeIncrWriteCounter(p, 0);
95083 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
95084 break;
95085}
95086
95087
95088#ifndef SQLITE_OMIT_INTEGRITY_CHECK
95089/* Opcode: IntegrityCk P1 P2 P3 P4 P5
95090**
95091** Do an analysis of the currently open database. Store in
95092** register P1 the text of an error message describing any problems.
95093** If no problems are found, store a NULL in register P1.
95094**
95095** The register P3 contains one less than the maximum number of allowed errors.
95096** At most reg(P3) errors will be reported.
95097** In other words, the analysis stops as soon as reg(P1) errors are
95098** seen. Reg(P1) is updated with the number of errors remaining.
95099**
95100** The root page numbers of all tables in the database are integers
95101** stored in P4_INTARRAY argument.
95102**
95103** If P5 is not zero, the check is done on the auxiliary database
95104** file, not the main database file.
95105**
95106** This opcode is used to implement the integrity_check pragma.
95107*/
95108case OP_IntegrityCk155: {
95109 int nRoot; /* Number of tables to check. (Number of root pages.) */
95110 Pgno *aRoot; /* Array of rootpage numbers for tables to be checked */
95111 int nErr; /* Number of errors reported */
95112 char *z; /* Text of the error report */
95113 Mem *pnErr; /* Register keeping track of errors remaining */
95114
95115 assert( p->bIsReader )((void) (0));
95116 nRoot = pOp->p2;
95117 aRoot = pOp->p4.ai;
95118 assert( nRoot>0 )((void) (0));
95119 assert( aRoot[0]==(Pgno)nRoot )((void) (0));
95120 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
95121 pnErr = &aMem[pOp->p3];
95122 assert( (pnErr->flags & MEM_Int)!=0 )((void) (0));
95123 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 )((void) (0));
95124 pIn1 = &aMem[pOp->p1];
95125 assert( pOp->p5<db->nDb )((void) (0));
95126 assert( DbMaskTest(p->btreeMask, pOp->p5) )((void) (0));
95127 z = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
95128 (int)pnErr->u.i+1, &nErr);
95129 sqlite3VdbeMemSetNull(pIn1);
95130 if( nErr==0 ){
95131 assert( z==0 )((void) (0));
95132 }else if( z==0 ){
95133 goto no_mem;
95134 }else{
95135 pnErr->u.i -= nErr-1;
95136 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF81, sqlite3_free);
95137 }
95138 UPDATE_MAX_BLOBSIZE(pIn1);
95139 sqlite3VdbeChangeEncoding(pIn1, encoding);
95140 goto check_for_interrupt;
95141}
95142#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
95143
95144/* Opcode: RowSetAdd P1 P2 * * *
95145** Synopsis: rowset(P1)=r[P2]
95146**
95147** Insert the integer value held by register P2 into a RowSet object
95148** held in register P1.
95149**
95150** An assertion fails if P2 is not an integer.
95151*/
95152case OP_RowSetAdd156: { /* in1, in2 */
95153 pIn1 = &aMem[pOp->p1];
95154 pIn2 = &aMem[pOp->p2];
95155 assert( (pIn2->flags & MEM_Int)!=0 )((void) (0));
95156 if( (pIn1->flags & MEM_Blob0x0010)==0 ){
95157 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
95158 }
95159 assert( sqlite3VdbeMemIsRowSet(pIn1) )((void) (0));
95160 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
95161 break;
95162}
95163
95164/* Opcode: RowSetRead P1 P2 P3 * *
95165** Synopsis: r[P3]=rowset(P1)
95166**
95167** Extract the smallest value from the RowSet object in P1
95168** and put that value into register P3.
95169** Or, if RowSet object P1 is initially empty, leave P3
95170** unchanged and jump to instruction P2.
95171*/
95172case OP_RowSetRead45: { /* jump, in1, out3 */
95173 i64 val;
95174
95175 pIn1 = &aMem[pOp->p1];
95176 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) )((void) (0));
95177 if( (pIn1->flags & MEM_Blob0x0010)==0
95178 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
95179 ){
95180 /* The boolean index is empty */
95181 sqlite3VdbeMemSetNull(pIn1);
95182 VdbeBranchTaken(1,2);
95183 goto jump_to_p2_and_check_for_interrupt;
95184 }else{
95185 /* A value was pulled from the index */
95186 VdbeBranchTaken(0,2);
95187 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
95188 }
95189 goto check_for_interrupt;
95190}
95191
95192/* Opcode: RowSetTest P1 P2 P3 P4
95193** Synopsis: if r[P3] in rowset(P1) goto P2
95194**
95195** Register P3 is assumed to hold a 64-bit integer value. If register P1
95196** contains a RowSet object and that RowSet object contains
95197** the value held in P3, jump to register P2. Otherwise, insert the
95198** integer in P3 into the RowSet and continue on to the
95199** next opcode.
95200**
95201** The RowSet object is optimized for the case where sets of integers
95202** are inserted in distinct phases, which each set contains no duplicates.
95203** Each set is identified by a unique P4 value. The first set
95204** must have P4==0, the final set must have P4==-1, and for all other sets
95205** must have P4>0.
95206**
95207** This allows optimizations: (a) when P4==0 there is no need to test
95208** the RowSet object for P3, as it is guaranteed not to contain it,
95209** (b) when P4==-1 there is no need to insert the value, as it will
95210** never be tested for, and (c) when a value that is part of set X is
95211** inserted, there is no need to search to see if the same value was
95212** previously inserted as part of set X (only if it was previously
95213** inserted as part of some other set).
95214*/
95215case OP_RowSetTest46: { /* jump, in1, in3 */
95216 int iSet;
95217 int exists;
95218
95219 pIn1 = &aMem[pOp->p1];
95220 pIn3 = &aMem[pOp->p3];
95221 iSet = pOp->p4.i;
95222 assert( pIn3->flags&MEM_Int )((void) (0));
95223
95224 /* If there is anything other than a rowset object in memory cell P1,
95225 ** delete it now and initialize P1 with an empty rowset
95226 */
95227 if( (pIn1->flags & MEM_Blob0x0010)==0 ){
95228 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
95229 }
95230 assert( sqlite3VdbeMemIsRowSet(pIn1) )((void) (0));
95231 assert( pOp->p4type==P4_INT32 )((void) (0));
95232 assert( iSet==-1 || iSet>=0 )((void) (0));
95233 if( iSet ){
95234 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
95235 VdbeBranchTaken(exists!=0,2);
95236 if( exists ) goto jump_to_p2;
95237 }
95238 if( iSet>=0 ){
95239 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
95240 }
95241 break;
95242}
95243
95244
95245#ifndef SQLITE_OMIT_TRIGGER
95246
95247/* Opcode: Program P1 P2 P3 P4 P5
95248**
95249** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
95250**
95251** P1 contains the address of the memory cell that contains the first memory
95252** cell in an array of values used as arguments to the sub-program. P2
95253** contains the address to jump to if the sub-program throws an IGNORE
95254** exception using the RAISE() function. Register P3 contains the address
95255** of a memory cell in this (the parent) VM that is used to allocate the
95256** memory required by the sub-vdbe at runtime.
95257**
95258** P4 is a pointer to the VM containing the trigger program.
95259**
95260** If P5 is non-zero, then recursive program invocation is enabled.
95261*/
95262case OP_Program47: { /* jump */
95263 int nMem; /* Number of memory registers for sub-program */
95264 int nByte; /* Bytes of runtime space required for sub-program */
95265 Mem *pRt; /* Register to allocate runtime space */
95266 Mem *pMem; /* Used to iterate through memory cells */
95267 Mem *pEnd; /* Last memory cell in new array */
95268 VdbeFrame *pFrame; /* New vdbe frame to execute in */
95269 SubProgram *pProgram; /* Sub-program to execute */
95270 void *t; /* Token identifying trigger */
95271
95272 pProgram = pOp->p4.pProgram;
95273 pRt = &aMem[pOp->p3];
95274 assert( pProgram->nOp>0 )((void) (0));
95275
95276 /* If the p5 flag is clear, then recursive invocation of triggers is
95277 ** disabled for backwards compatibility (p5 is set if this sub-program
95278 ** is really a trigger, not a foreign key action, and the flag set
95279 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
95280 **
95281 ** It is recursive invocation of triggers, at the SQL level, that is
95282 ** disabled. In some cases a single trigger may generate more than one
95283 ** SubProgram (if the trigger may be executed with more than one different
95284 ** ON CONFLICT algorithm). SubProgram structures associated with a
95285 ** single trigger all have the same value for the SubProgram.token
95286 ** variable. */
95287 if( pOp->p5 ){
95288 t = pProgram->token;
95289 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
95290 if( pFrame ) break;
95291 }
95292
95293 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH10] ){
95294 rc = SQLITE_ERROR1;
95295 sqlite3VdbeError(p, "too many levels of trigger recursion");
95296 goto abort_due_to_error;
95297 }
95298
95299 /* Register pRt is used to store the memory required to save the state
95300 ** of the current program, and the memory required at runtime to execute
95301 ** the trigger program. If this trigger has been fired before, then pRt
95302 ** is already allocated. Otherwise, it must be initialized. */
95303 if( (pRt->flags&MEM_Blob0x0010)==0 ){
95304 /* SubProgram.nMem is set to the number of memory cells used by the
95305 ** program stored in SubProgram.aOp. As well as these, one memory
95306 ** cell is required for each cursor used by the program. Set local
95307 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
95308 */
95309 nMem = pProgram->nMem + pProgram->nCsr;
95310 assert( nMem>0 )((void) (0));
95311 if( pProgram->nCsr==0 ) nMem++;
95312 nByte = ROUND8(sizeof(VdbeFrame))(((sizeof(VdbeFrame))+7)&~7)
95313 + nMem * sizeof(Mem)
95314 + pProgram->nCsr * sizeof(VdbeCursor*)
95315 + (pProgram->nOp + 7)/8;
95316 pFrame = sqlite3DbMallocZero(db, nByte);
95317 if( !pFrame ){
95318 goto no_mem;
95319 }
95320 sqlite3VdbeMemRelease(pRt);
95321 pRt->flags = MEM_Blob0x0010|MEM_Dyn0x1000;
95322 pRt->z = (char*)pFrame;
95323 pRt->n = nByte;
95324 pRt->xDel = sqlite3VdbeFrameMemDel;
95325
95326 pFrame->v = p;
95327 pFrame->nChildMem = nMem;
95328 pFrame->nChildCsr = pProgram->nCsr;
95329 pFrame->pc = (int)(pOp - aOp);
95330 pFrame->aMem = p->aMem;
95331 pFrame->nMem = p->nMem;
95332 pFrame->apCsr = p->apCsr;
95333 pFrame->nCursor = p->nCursor;
95334 pFrame->aOp = p->aOp;
95335 pFrame->nOp = p->nOp;
95336 pFrame->token = pProgram->token;
95337#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
95338 pFrame->anExec = p->anExec;
95339#endif
95340#ifdef SQLITE_DEBUG
95341 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC0x879fb71e;
95342#endif
95343
95344 pEnd = &VdbeFrameMem(pFrame)((Mem *)&((u8 *)pFrame)[(((sizeof(VdbeFrame))+7)&~7)]
)
[pFrame->nChildMem];
95345 for(pMem=VdbeFrameMem(pFrame)((Mem *)&((u8 *)pFrame)[(((sizeof(VdbeFrame))+7)&~7)]
)
; pMem!=pEnd; pMem++){
95346 pMem->flags = MEM_Undefined0x0000;
95347 pMem->db = db;
95348 }
95349 }else{
95350 pFrame = (VdbeFrame*)pRt->z;
95351 assert( pRt->xDel==sqlite3VdbeFrameMemDel )((void) (0));
95352 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem((void) (0))
95353 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) )((void) (0));
95354 assert( pProgram->nCsr==pFrame->nChildCsr )((void) (0));
95355 assert( (int)(pOp - aOp)==pFrame->pc )((void) (0));
95356 }
95357
95358 p->nFrame++;
95359 pFrame->pParent = p->pFrame;
95360 pFrame->lastRowid = db->lastRowid;
95361 pFrame->nChange = p->nChange;
95362 pFrame->nDbChange = p->db->nChange;
95363 assert( pFrame->pAuxData==0 )((void) (0));
95364 pFrame->pAuxData = p->pAuxData;
95365 p->pAuxData = 0;
95366 p->nChange = 0;
95367 p->pFrame = pFrame;
95368 p->aMem = aMem = VdbeFrameMem(pFrame)((Mem *)&((u8 *)pFrame)[(((sizeof(VdbeFrame))+7)&~7)]
)
;
95369 p->nMem = pFrame->nChildMem;
95370 p->nCursor = (u16)pFrame->nChildCsr;
95371 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
95372 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
95373 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
95374 p->aOp = aOp = pProgram->aOp;
95375 p->nOp = pProgram->nOp;
95376#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
95377 p->anExec = 0;
95378#endif
95379#ifdef SQLITE_DEBUG
95380 /* Verify that second and subsequent executions of the same trigger do not
95381 ** try to reuse register values from the first use. */
95382 {
95383 int i;
95384 for(i=0; i<p->nMem; i++){
95385 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
95386 MemSetTypeFlag(&aMem[i], MEM_Undefined)((&aMem[i])->flags = ((&aMem[i])->flags&~(0x0dbf
|0x0400))|0x0000)
; /* Fault if this reg is reused */
95387 }
95388 }
95389#endif
95390 pOp = &aOp[-1];
95391 goto check_for_interrupt;
95392}
95393
95394/* Opcode: Param P1 P2 * * *
95395**
95396** This opcode is only ever present in sub-programs called via the
95397** OP_Program instruction. Copy a value currently stored in a memory
95398** cell of the calling (parent) frame to cell P2 in the current frames
95399** address space. This is used by trigger programs to access the new.*
95400** and old.* values.
95401**
95402** The address of the cell in the parent frame is determined by adding
95403** the value of the P1 argument to the value of the P1 argument to the
95404** calling OP_Program instruction.
95405*/
95406case OP_Param157: { /* out2 */
95407 VdbeFrame *pFrame;
95408 Mem *pIn;
95409 pOut = out2Prerelease(p, pOp);
95410 pFrame = p->pFrame;
95411 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
95412 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem0x4000);
95413 break;
95414}
95415
95416#endif /* #ifndef SQLITE_OMIT_TRIGGER */
95417
95418#ifndef SQLITE_OMIT_FOREIGN_KEY
95419/* Opcode: FkCounter P1 P2 * * *
95420** Synopsis: fkctr[P1]+=P2
95421**
95422** Increment a "constraint counter" by P2 (P2 may be negative or positive).
95423** If P1 is non-zero, the database constraint counter is incremented
95424** (deferred foreign key constraints). Otherwise, if P1 is zero, the
95425** statement counter is incremented (immediate foreign key constraints).
95426*/
95427case OP_FkCounter158: {
95428 if( db->flags & SQLITE_DeferFKs0x00080000 ){
95429 db->nDeferredImmCons += pOp->p2;
95430 }else if( pOp->p1 ){
95431 db->nDeferredCons += pOp->p2;
95432 }else{
95433 p->nFkConstraint += pOp->p2;
95434 }
95435 break;
95436}
95437
95438/* Opcode: FkIfZero P1 P2 * * *
95439** Synopsis: if fkctr[P1]==0 goto P2
95440**
95441** This opcode tests if a foreign key constraint-counter is currently zero.
95442** If so, jump to instruction P2. Otherwise, fall through to the next
95443** instruction.
95444**
95445** If P1 is non-zero, then the jump is taken if the database constraint-counter
95446** is zero (the one that counts deferred constraint violations). If P1 is
95447** zero, the jump is taken if the statement constraint-counter is zero
95448** (immediate foreign key constraint violations).
95449*/
95450case OP_FkIfZero48: { /* jump */
95451 if( pOp->p1 ){
95452 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
95453 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
95454 }else{
95455 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
95456 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
95457 }
95458 break;
95459}
95460#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
95461
95462#ifndef SQLITE_OMIT_AUTOINCREMENT
95463/* Opcode: MemMax P1 P2 * * *
95464** Synopsis: r[P1]=max(r[P1],r[P2])
95465**
95466** P1 is a register in the root frame of this VM (the root frame is
95467** different from the current frame if this instruction is being executed
95468** within a sub-program). Set the value of register P1 to the maximum of
95469** its current value and the value in register P2.
95470**
95471** This instruction throws an error if the memory cell is not initially
95472** an integer.
95473*/
95474case OP_MemMax159: { /* in2 */
95475 VdbeFrame *pFrame;
95476 if( p->pFrame ){
95477 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
95478 pIn1 = &pFrame->aMem[pOp->p1];
95479 }else{
95480 pIn1 = &aMem[pOp->p1];
95481 }
95482 assert( memIsValid(pIn1) )((void) (0));
95483 sqlite3VdbeMemIntegerify(pIn1);
95484 pIn2 = &aMem[pOp->p2];
95485 sqlite3VdbeMemIntegerify(pIn2);
95486 if( pIn1->u.i<pIn2->u.i){
95487 pIn1->u.i = pIn2->u.i;
95488 }
95489 break;
95490}
95491#endif /* SQLITE_OMIT_AUTOINCREMENT */
95492
95493/* Opcode: IfPos P1 P2 P3 * *
95494** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
95495**
95496** Register P1 must contain an integer.
95497** If the value of register P1 is 1 or greater, subtract P3 from the
95498** value in P1 and jump to P2.
95499**
95500** If the initial value of register P1 is less than 1, then the
95501** value is unchanged and control passes through to the next instruction.
95502*/
95503case OP_IfPos49: { /* jump, in1 */
95504 pIn1 = &aMem[pOp->p1];
95505 assert( pIn1->flags&MEM_Int )((void) (0));
95506 VdbeBranchTaken( pIn1->u.i>0, 2);
95507 if( pIn1->u.i>0 ){
95508 pIn1->u.i -= pOp->p3;
95509 goto jump_to_p2;
95510 }
95511 break;
95512}
95513
95514/* Opcode: OffsetLimit P1 P2 P3 * *
95515** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
95516**
95517** This opcode performs a commonly used computation associated with
95518** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3]
95519** holds the offset counter. The opcode computes the combined value
95520** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
95521** value computed is the total number of rows that will need to be
95522** visited in order to complete the query.
95523**
95524** If r[P3] is zero or negative, that means there is no OFFSET
95525** and r[P2] is set to be the value of the LIMIT, r[P1].
95526**
95527** if r[P1] is zero or negative, that means there is no LIMIT
95528** and r[P2] is set to -1.
95529**
95530** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
95531*/
95532case OP_OffsetLimit160: { /* in1, out2, in3 */
95533 i64 x;
95534 pIn1 = &aMem[pOp->p1];
95535 pIn3 = &aMem[pOp->p3];
95536 pOut = out2Prerelease(p, pOp);
95537 assert( pIn1->flags & MEM_Int )((void) (0));
95538 assert( pIn3->flags & MEM_Int )((void) (0));
95539 x = pIn1->u.i;
95540 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
95541 /* If the LIMIT is less than or equal to zero, loop forever. This
95542 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
95543 ** also loop forever. This is undocumented. In fact, one could argue
95544 ** that the loop should terminate. But assuming 1 billion iterations
95545 ** per second (far exceeding the capabilities of any current hardware)
95546 ** it would take nearly 300 years to actually reach the limit. So
95547 ** looping forever is a reasonable approximation. */
95548 pOut->u.i = -1;
95549 }else{
95550 pOut->u.i = x;
95551 }
95552 break;
95553}
95554
95555/* Opcode: IfNotZero P1 P2 * * *
95556** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
95557**
95558** Register P1 must contain an integer. If the content of register P1 is
95559** initially greater than zero, then decrement the value in register P1.
95560** If it is non-zero (negative or positive) and then also jump to P2.
95561** If register P1 is initially zero, leave it unchanged and fall through.
95562*/
95563case OP_IfNotZero59: { /* jump, in1 */
95564 pIn1 = &aMem[pOp->p1];
95565 assert( pIn1->flags&MEM_Int )((void) (0));
95566 VdbeBranchTaken(pIn1->u.i<0, 2);
95567 if( pIn1->u.i ){
95568 if( pIn1->u.i>0 ) pIn1->u.i--;
95569 goto jump_to_p2;
95570 }
95571 break;
95572}
95573
95574/* Opcode: DecrJumpZero P1 P2 * * *
95575** Synopsis: if (--r[P1])==0 goto P2
95576**
95577** Register P1 must hold an integer. Decrement the value in P1
95578** and jump to P2 if the new value is exactly zero.
95579*/
95580case OP_DecrJumpZero60: { /* jump, in1 */
95581 pIn1 = &aMem[pOp->p1];
95582 assert( pIn1->flags&MEM_Int )((void) (0));
95583 if( pIn1->u.i>SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ) pIn1->u.i--;
95584 VdbeBranchTaken(pIn1->u.i==0, 2);
95585 if( pIn1->u.i==0 ) goto jump_to_p2;
95586 break;
95587}
95588
95589
95590/* Opcode: AggStep * P2 P3 P4 P5
95591** Synopsis: accum=r[P3] step(r[P2@P5])
95592**
95593** Execute the xStep function for an aggregate.
95594** The function has P5 arguments. P4 is a pointer to the
95595** FuncDef structure that specifies the function. Register P3 is the
95596** accumulator.
95597**
95598** The P5 arguments are taken from register P2 and its
95599** successors.
95600*/
95601/* Opcode: AggInverse * P2 P3 P4 P5
95602** Synopsis: accum=r[P3] inverse(r[P2@P5])
95603**
95604** Execute the xInverse function for an aggregate.
95605** The function has P5 arguments. P4 is a pointer to the
95606** FuncDef structure that specifies the function. Register P3 is the
95607** accumulator.
95608**
95609** The P5 arguments are taken from register P2 and its
95610** successors.
95611*/
95612/* Opcode: AggStep1 P1 P2 P3 P4 P5
95613** Synopsis: accum=r[P3] step(r[P2@P5])
95614**
95615** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
95616** aggregate. The function has P5 arguments. P4 is a pointer to the
95617** FuncDef structure that specifies the function. Register P3 is the
95618** accumulator.
95619**
95620** The P5 arguments are taken from register P2 and its
95621** successors.
95622**
95623** This opcode is initially coded as OP_AggStep0. On first evaluation,
95624** the FuncDef stored in P4 is converted into an sqlite3_context and
95625** the opcode is changed. In this way, the initialization of the
95626** sqlite3_context only happens once, instead of on each call to the
95627** step function.
95628*/
95629case OP_AggInverse161:
95630case OP_AggStep162: {
95631 int n;
95632 sqlite3_context *pCtx;
95633
95634 assert( pOp->p4type==P4_FUNCDEF )((void) (0));
95635 n = pOp->p5;
95636 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
95637 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) )((void) (0));
95638 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n )((void) (0));
95639 pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) +
95640 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
95641 if( pCtx==0 ) goto no_mem;
95642 pCtx->pMem = 0;
95643 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
95644 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null0x0001);
95645 pCtx->pFunc = pOp->p4.pFunc;
95646 pCtx->iOp = (int)(pOp - aOp);
95647 pCtx->pVdbe = p;
95648 pCtx->skipFlag = 0;
95649 pCtx->isError = 0;
95650 pCtx->enc = encoding;
95651 pCtx->argc = n;
95652 pOp->p4type = P4_FUNCCTX(-15);
95653 pOp->p4.pCtx = pCtx;
95654
95655 /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */
95656 assert( pOp->p1==(pOp->opcode==OP_AggInverse) )((void) (0));
95657
95658 pOp->opcode = OP_AggStep1163;
95659 /* Fall through into OP_AggStep */
95660 /* no break */ deliberate_fall_through
95661}
95662case OP_AggStep1163: {
95663 int i;
95664 sqlite3_context *pCtx;
95665 Mem *pMem;
95666
95667 assert( pOp->p4type==P4_FUNCCTX )((void) (0));
95668 pCtx = pOp->p4.pCtx;
95669 pMem = &aMem[pOp->p3];
95670
95671#ifdef SQLITE_DEBUG
95672 if( pOp->p1 ){
95673 /* This is an OP_AggInverse call. Verify that xStep has always
95674 ** been called at least once prior to any xInverse call. */
95675 assert( pMem->uTemp==0x1122e0e3 )((void) (0));
95676 }else{
95677 /* This is an OP_AggStep call. Mark it as such. */
95678 pMem->uTemp = 0x1122e0e3;
95679 }
95680#endif
95681
95682 /* If this function is inside of a trigger, the register array in aMem[]
95683 ** might change from one evaluation to the next. The next block of code
95684 ** checks to see if the register array has changed, and if so it
95685 ** reinitializes the relavant parts of the sqlite3_context object */
95686 if( pCtx->pMem != pMem ){
95687 pCtx->pMem = pMem;
95688 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
95689 }
95690
95691#ifdef SQLITE_DEBUG
95692 for(i=0; i<pCtx->argc; i++){
95693 assert( memIsValid(pCtx->argv[i]) )((void) (0));
95694 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
95695 }
95696#endif
95697
95698 pMem->n++;
95699 assert( pCtx->pOut->flags==MEM_Null )((void) (0));
95700 assert( pCtx->isError==0 )((void) (0));
95701 assert( pCtx->skipFlag==0 )((void) (0));
95702#ifndef SQLITE_OMIT_WINDOWFUNC
95703 if( pOp->p1 ){
95704 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
95705 }else
95706#endif
95707 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
95708
95709 if( pCtx->isError ){
95710 if( pCtx->isError>0 ){
95711 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
95712 rc = pCtx->isError;
95713 }
95714 if( pCtx->skipFlag ){
95715 assert( pOp[-1].opcode==OP_CollSeq )((void) (0));
95716 i = pOp[-1].p1;
95717 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
95718 pCtx->skipFlag = 0;
95719 }
95720 sqlite3VdbeMemRelease(pCtx->pOut);
95721 pCtx->pOut->flags = MEM_Null0x0001;
95722 pCtx->isError = 0;
95723 if( rc ) goto abort_due_to_error;
95724 }
95725 assert( pCtx->pOut->flags==MEM_Null )((void) (0));
95726 assert( pCtx->skipFlag==0 )((void) (0));
95727 break;
95728}
95729
95730/* Opcode: AggFinal P1 P2 * P4 *
95731** Synopsis: accum=r[P1] N=P2
95732**
95733** P1 is the memory location that is the accumulator for an aggregate
95734** or window function. Execute the finalizer function
95735** for an aggregate and store the result in P1.
95736**
95737** P2 is the number of arguments that the step function takes and
95738** P4 is a pointer to the FuncDef for this function. The P2
95739** argument is not used by this opcode. It is only there to disambiguate
95740** functions that can take varying numbers of arguments. The
95741** P4 argument is only needed for the case where
95742** the step function was not previously called.
95743*/
95744/* Opcode: AggValue * P2 P3 P4 *
95745** Synopsis: r[P3]=value N=P2
95746**
95747** Invoke the xValue() function and store the result in register P3.
95748**
95749** P2 is the number of arguments that the step function takes and
95750** P4 is a pointer to the FuncDef for this function. The P2
95751** argument is not used by this opcode. It is only there to disambiguate
95752** functions that can take varying numbers of arguments. The
95753** P4 argument is only needed for the case where
95754** the step function was not previously called.
95755*/
95756case OP_AggValue164:
95757case OP_AggFinal165: {
95758 Mem *pMem;
95759 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) )((void) (0));
95760 assert( pOp->p3==0 || pOp->opcode==OP_AggValue )((void) (0));
95761 pMem = &aMem[pOp->p1];
95762 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 )((void) (0));
95763#ifndef SQLITE_OMIT_WINDOWFUNC
95764 if( pOp->p3 ){
95765 memAboutToChange(p, &aMem[pOp->p3]);
95766 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
95767 pMem = &aMem[pOp->p3];
95768 }else
95769#endif
95770 {
95771 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
95772 }
95773
95774 if( rc ){
95775 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
95776 goto abort_due_to_error;
95777 }
95778 sqlite3VdbeChangeEncoding(pMem, encoding);
95779 UPDATE_MAX_BLOBSIZE(pMem);
95780 break;
95781}
95782
95783#ifndef SQLITE_OMIT_WAL
95784/* Opcode: Checkpoint P1 P2 P3 * *
95785**
95786** Checkpoint database P1. This is a no-op if P1 is not currently in
95787** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
95788** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
95789** SQLITE_BUSY or not, respectively. Write the number of pages in the
95790** WAL after the checkpoint into mem[P3+1] and the number of pages
95791** in the WAL that have been checkpointed after the checkpoint
95792** completes into mem[P3+2]. However on an error, mem[P3+1] and
95793** mem[P3+2] are initialized to -1.
95794*/
95795case OP_Checkpoint3: {
95796 int i; /* Loop counter */
95797 int aRes[3]; /* Results */
95798 Mem *pMem; /* Write results here */
95799
95800 assert( p->readOnly==0 )((void) (0));
95801 aRes[0] = 0;
95802 aRes[1] = aRes[2] = -1;
95803 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE((void) (0))
95804 || pOp->p2==SQLITE_CHECKPOINT_FULL((void) (0))
95805 || pOp->p2==SQLITE_CHECKPOINT_RESTART((void) (0))
95806 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE((void) (0))
95807 )((void) (0));
95808 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
95809 if( rc ){
95810 if( rc!=SQLITE_BUSY5 ) goto abort_due_to_error;
95811 rc = SQLITE_OK0;
95812 aRes[0] = 1;
95813 }
95814 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
95815 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
95816 }
95817 break;
95818};
95819#endif
95820
95821#ifndef SQLITE_OMIT_PRAGMA
95822/* Opcode: JournalMode P1 P2 P3 * *
95823**
95824** Change the journal mode of database P1 to P3. P3 must be one of the
95825** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
95826** modes (delete, truncate, persist, off and memory), this is a simple
95827** operation. No IO is required.
95828**
95829** If changing into or out of WAL mode the procedure is more complicated.
95830**
95831** Write a string containing the final journal-mode to register P2.
95832*/
95833case OP_JournalMode4: { /* out2 */
95834 Btree *pBt; /* Btree to change journal mode of */
95835 Pager *pPager; /* Pager associated with pBt */
95836 int eNew; /* New journal mode */
95837 int eOld; /* The old journal mode */
95838#ifndef SQLITE_OMIT_WAL
95839 const char *zFilename; /* Name of database file for pPager */
95840#endif
95841
95842 pOut = out2Prerelease(p, pOp);
95843 eNew = pOp->p3;
95844 assert( eNew==PAGER_JOURNALMODE_DELETE((void) (0))
95845 || eNew==PAGER_JOURNALMODE_TRUNCATE((void) (0))
95846 || eNew==PAGER_JOURNALMODE_PERSIST((void) (0))
95847 || eNew==PAGER_JOURNALMODE_OFF((void) (0))
95848 || eNew==PAGER_JOURNALMODE_MEMORY((void) (0))
95849 || eNew==PAGER_JOURNALMODE_WAL((void) (0))
95850 || eNew==PAGER_JOURNALMODE_QUERY((void) (0))
95851 )((void) (0));
95852 assert( pOp->p1>=0 && pOp->p1<db->nDb )((void) (0));
95853 assert( p->readOnly==0 )((void) (0));
95854
95855 pBt = db->aDb[pOp->p1].pBt;
95856 pPager = sqlite3BtreePager(pBt);
95857 eOld = sqlite3PagerGetJournalMode(pPager);
95858 if( eNew==PAGER_JOURNALMODE_QUERY(-1) ) eNew = eOld;
95859 assert( sqlite3BtreeHoldsMutex(pBt) )((void) (0));
95860 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
95861
95862#ifndef SQLITE_OMIT_WAL
95863 zFilename = sqlite3PagerFilename(pPager, 1);
95864
95865 /* Do not allow a transition to journal_mode=WAL for a database
95866 ** in temporary storage or if the VFS does not support shared memory
95867 */
95868 if( eNew==PAGER_JOURNALMODE_WAL5
95869 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
95870 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
95871 ){
95872 eNew = eOld;
95873 }
95874
95875 if( (eNew!=eOld)
95876 && (eOld==PAGER_JOURNALMODE_WAL5 || eNew==PAGER_JOURNALMODE_WAL5)
95877 ){
95878 if( !db->autoCommit || db->nVdbeRead>1 ){
95879 rc = SQLITE_ERROR1;
95880 sqlite3VdbeError(p,
95881 "cannot change %s wal mode from within a transaction",
95882 (eNew==PAGER_JOURNALMODE_WAL5 ? "into" : "out of")
95883 );
95884 goto abort_due_to_error;
95885 }else{
95886
95887 if( eOld==PAGER_JOURNALMODE_WAL5 ){
95888 /* If leaving WAL mode, close the log file. If successful, the call
95889 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
95890 ** file. An EXCLUSIVE lock may still be held on the database file
95891 ** after a successful return.
95892 */
95893 rc = sqlite3PagerCloseWal(pPager, db);
95894 if( rc==SQLITE_OK0 ){
95895 sqlite3PagerSetJournalMode(pPager, eNew);
95896 }
95897 }else if( eOld==PAGER_JOURNALMODE_MEMORY4 ){
95898 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
95899 ** as an intermediate */
95900 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF2);
95901 }
95902
95903 /* Open a transaction on the database file. Regardless of the journal
95904 ** mode, this transaction always uses a rollback journal.
95905 */
95906 assert( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_WRITE )((void) (0));
95907 if( rc==SQLITE_OK0 ){
95908 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL5 ? 2 : 1));
95909 }
95910 }
95911 }
95912#endif /* ifndef SQLITE_OMIT_WAL */
95913
95914 if( rc ) eNew = eOld;
95915 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
95916
95917 pOut->flags = MEM_Str0x0002|MEM_Static0x2000|MEM_Term0x0200;
95918 pOut->z = (char *)sqlite3JournalModename(eNew);
95919 pOut->n = sqlite3Strlen30(pOut->z);
95920 pOut->enc = SQLITE_UTF81;
95921 sqlite3VdbeChangeEncoding(pOut, encoding);
95922 if( rc ) goto abort_due_to_error;
95923 break;
95924};
95925#endif /* SQLITE_OMIT_PRAGMA */
95926
95927#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
95928/* Opcode: Vacuum P1 P2 * * *
95929**
95930** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
95931** for an attached database. The "temp" database may not be vacuumed.
95932**
95933** If P2 is not zero, then it is a register holding a string which is
95934** the file into which the result of vacuum should be written. When
95935** P2 is zero, the vacuum overwrites the original database.
95936*/
95937case OP_Vacuum5: {
95938 assert( p->readOnly==0 )((void) (0));
95939 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
95940 pOp->p2 ? &aMem[pOp->p2] : 0);
95941 if( rc ) goto abort_due_to_error;
95942 break;
95943}
95944#endif
95945
95946#if !defined(SQLITE_OMIT_AUTOVACUUM)
95947/* Opcode: IncrVacuum P1 P2 * * *
95948**
95949** Perform a single step of the incremental vacuum procedure on
95950** the P1 database. If the vacuum has finished, jump to instruction
95951** P2. Otherwise, fall through to the next instruction.
95952*/
95953case OP_IncrVacuum61: { /* jump */
95954 Btree *pBt;
95955
95956 assert( pOp->p1>=0 && pOp->p1<db->nDb )((void) (0));
95957 assert( DbMaskTest(p->btreeMask, pOp->p1) )((void) (0));
95958 assert( p->readOnly==0 )((void) (0));
95959 pBt = db->aDb[pOp->p1].pBt;
95960 rc = sqlite3BtreeIncrVacuum(pBt);
95961 VdbeBranchTaken(rc==SQLITE_DONE,2);
95962 if( rc ){
95963 if( rc!=SQLITE_DONE101 ) goto abort_due_to_error;
95964 rc = SQLITE_OK0;
95965 goto jump_to_p2;
95966 }
95967 break;
95968}
95969#endif
95970
95971/* Opcode: Expire P1 P2 * * *
95972**
95973** Cause precompiled statements to expire. When an expired statement
95974** is executed using sqlite3_step() it will either automatically
95975** reprepare itself (if it was originally created using sqlite3_prepare_v2())
95976** or it will fail with SQLITE_SCHEMA.
95977**
95978** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
95979** then only the currently executing statement is expired.
95980**
95981** If P2 is 0, then SQL statements are expired immediately. If P2 is 1,
95982** then running SQL statements are allowed to continue to run to completion.
95983** The P2==1 case occurs when a CREATE INDEX or similar schema change happens
95984** that might help the statement run faster but which does not affect the
95985** correctness of operation.
95986*/
95987case OP_Expire166: {
95988 assert( pOp->p2==0 || pOp->p2==1 )((void) (0));
95989 if( !pOp->p1 ){
95990 sqlite3ExpirePreparedStatements(db, pOp->p2);
95991 }else{
95992 p->expired = pOp->p2+1;
95993 }
95994 break;
95995}
95996
95997/* Opcode: CursorLock P1 * * * *
95998**
95999** Lock the btree to which cursor P1 is pointing so that the btree cannot be
96000** written by an other cursor.
96001*/
96002case OP_CursorLock167: {
96003 VdbeCursor *pC;
96004 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
96005 pC = p->apCsr[pOp->p1];
96006 assert( pC!=0 )((void) (0));
96007 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
96008 sqlite3BtreeCursorPin(pC->uc.pCursor);
96009 break;
96010}
96011
96012/* Opcode: CursorUnlock P1 * * * *
96013**
96014** Unlock the btree to which cursor P1 is pointing so that it can be
96015** written by other cursors.
96016*/
96017case OP_CursorUnlock168: {
96018 VdbeCursor *pC;
96019 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
96020 pC = p->apCsr[pOp->p1];
96021 assert( pC!=0 )((void) (0));
96022 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
96023 sqlite3BtreeCursorUnpin(pC->uc.pCursor);
96024 break;
96025}
96026
96027#ifndef SQLITE_OMIT_SHARED_CACHE
96028/* Opcode: TableLock P1 P2 P3 P4 *
96029** Synopsis: iDb=P1 root=P2 write=P3
96030**
96031** Obtain a lock on a particular table. This instruction is only used when
96032** the shared-cache feature is enabled.
96033**
96034** P1 is the index of the database in sqlite3.aDb[] of the database
96035** on which the lock is acquired. A readlock is obtained if P3==0 or
96036** a write lock if P3==1.
96037**
96038** P2 contains the root-page of the table to lock.
96039**
96040** P4 contains a pointer to the name of the table being locked. This is only
96041** used to generate an error message if the lock cannot be obtained.
96042*/
96043case OP_TableLock169: {
96044 u8 isWriteLock = (u8)pOp->p3;
96045 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit0x00000400) ){
96046 int p1 = pOp->p1;
96047 assert( p1>=0 && p1<db->nDb )((void) (0));
96048 assert( DbMaskTest(p->btreeMask, p1) )((void) (0));
96049 assert( isWriteLock==0 || isWriteLock==1 )((void) (0));
96050 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
96051 if( rc ){
96052 if( (rc&0xFF)==SQLITE_LOCKED6 ){
96053 const char *z = pOp->p4.z;
96054 sqlite3VdbeError(p, "database table is locked: %s", z);
96055 }
96056 goto abort_due_to_error;
96057 }
96058 }
96059 break;
96060}
96061#endif /* SQLITE_OMIT_SHARED_CACHE */
96062
96063#ifndef SQLITE_OMIT_VIRTUALTABLE
96064/* Opcode: VBegin * * * P4 *
96065**
96066** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
96067** xBegin method for that table.
96068**
96069** Also, whether or not P4 is set, check that this is not being called from
96070** within a callback to a virtual table xSync() method. If it is, the error
96071** code will be set to SQLITE_LOCKED.
96072*/
96073case OP_VBegin170: {
96074 VTable *pVTab;
96075 pVTab = pOp->p4.pVtab;
96076 rc = sqlite3VtabBegin(db, pVTab);
96077 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
96078 if( rc ) goto abort_due_to_error;
96079 break;
96080}
96081#endif /* SQLITE_OMIT_VIRTUALTABLE */
96082
96083#ifndef SQLITE_OMIT_VIRTUALTABLE
96084/* Opcode: VCreate P1 P2 * * *
96085**
96086** P2 is a register that holds the name of a virtual table in database
96087** P1. Call the xCreate method for that table.
96088*/
96089case OP_VCreate171: {
96090 Mem sMem; /* For storing the record being decoded */
96091 const char *zTab; /* Name of the virtual table */
96092
96093 memset(&sMem, 0, sizeof(sMem));
96094 sMem.db = db;
96095 /* Because P2 is always a static string, it is impossible for the
96096 ** sqlite3VdbeMemCopy() to fail */
96097 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 )((void) (0));
96098 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 )((void) (0));
96099 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
96100 assert( rc==SQLITE_OK )((void) (0));
96101 zTab = (const char*)sqlite3_value_text(&sMem);
96102 assert( zTab || db->mallocFailed )((void) (0));
96103 if( zTab ){
96104 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
96105 }
96106 sqlite3VdbeMemRelease(&sMem);
96107 if( rc ) goto abort_due_to_error;
96108 break;
96109}
96110#endif /* SQLITE_OMIT_VIRTUALTABLE */
96111
96112#ifndef SQLITE_OMIT_VIRTUALTABLE
96113/* Opcode: VDestroy P1 * * P4 *
96114**
96115** P4 is the name of a virtual table in database P1. Call the xDestroy method
96116** of that table.
96117*/
96118case OP_VDestroy172: {
96119 db->nVDestroy++;
96120 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
96121 db->nVDestroy--;
96122 assert( p->errorAction==OE_Abort && p->usesStmtJournal )((void) (0));
96123 if( rc ) goto abort_due_to_error;
96124 break;
96125}
96126#endif /* SQLITE_OMIT_VIRTUALTABLE */
96127
96128#ifndef SQLITE_OMIT_VIRTUALTABLE
96129/* Opcode: VOpen P1 * * P4 *
96130**
96131** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
96132** P1 is a cursor number. This opcode opens a cursor to the virtual
96133** table and stores that cursor in P1.
96134*/
96135case OP_VOpen173: {
96136 VdbeCursor *pCur;
96137 sqlite3_vtab_cursor *pVCur;
96138 sqlite3_vtab *pVtab;
96139 const sqlite3_module *pModule;
96140
96141 assert( p->bIsReader )((void) (0));
96142 pCur = 0;
96143 pVCur = 0;
96144 pVtab = pOp->p4.pVtab->pVtab;
96145 if( pVtab==0 || NEVER(pVtab->pModule==0)(pVtab->pModule==0) ){
96146 rc = SQLITE_LOCKED6;
96147 goto abort_due_to_error;
96148 }
96149 pModule = pVtab->pModule;
96150 rc = pModule->xOpen(pVtab, &pVCur);
96151 sqlite3VtabImportErrmsg(p, pVtab);
96152 if( rc ) goto abort_due_to_error;
96153
96154 /* Initialize sqlite3_vtab_cursor base class */
96155 pVCur->pVtab = pVtab;
96156
96157 /* Initialize vdbe cursor object */
96158 pCur = allocateCursor(p, pOp->p1, 0, CURTYPE_VTAB2);
96159 if( pCur ){
96160 pCur->uc.pVCur = pVCur;
96161 pVtab->nRef++;
96162 }else{
96163 assert( db->mallocFailed )((void) (0));
96164 pModule->xClose(pVCur);
96165 goto no_mem;
96166 }
96167 break;
96168}
96169#endif /* SQLITE_OMIT_VIRTUALTABLE */
96170
96171#ifndef SQLITE_OMIT_VIRTUALTABLE
96172/* Opcode: VInitIn P1 P2 P3 * *
96173** Synopsis: r[P2]=ValueList(P1,P3)
96174**
96175** Set register P2 to be a pointer to a ValueList object for cursor P1
96176** with cache register P3 and output register P3+1. This ValueList object
96177** can be used as the first argument to sqlite3_vtab_in_first() and
96178** sqlite3_vtab_in_next() to extract all of the values stored in the P1
96179** cursor. Register P3 is used to hold the values returned by
96180** sqlite3_vtab_in_first() and sqlite3_vtab_in_next().
96181*/
96182case OP_VInitIn174: { /* out2 */
96183 VdbeCursor *pC; /* The cursor containing the RHS values */
96184 ValueList *pRhs; /* New ValueList object to put in reg[P2] */
96185
96186 pC = p->apCsr[pOp->p1];
96187 pRhs = sqlite3_malloc64( sizeof(*pRhs) );
96188 if( pRhs==0 ) goto no_mem;
96189 pRhs->pCsr = pC->uc.pCursor;
96190 pRhs->pOut = &aMem[pOp->p3];
96191 pOut = out2Prerelease(p, pOp);
96192 pOut->flags = MEM_Null0x0001;
96193 sqlite3VdbeMemSetPointer(pOut, pRhs, "ValueList", sqlite3_free);
96194 break;
96195}
96196#endif /* SQLITE_OMIT_VIRTUALTABLE */
96197
96198
96199#ifndef SQLITE_OMIT_VIRTUALTABLE
96200/* Opcode: VFilter P1 P2 P3 P4 *
96201** Synopsis: iplan=r[P3] zplan='P4'
96202**
96203** P1 is a cursor opened using VOpen. P2 is an address to jump to if
96204** the filtered result set is empty.
96205**
96206** P4 is either NULL or a string that was generated by the xBestIndex
96207** method of the module. The interpretation of the P4 string is left
96208** to the module implementation.
96209**
96210** This opcode invokes the xFilter method on the virtual table specified
96211** by P1. The integer query plan parameter to xFilter is stored in register
96212** P3. Register P3+1 stores the argc parameter to be passed to the
96213** xFilter method. Registers P3+2..P3+1+argc are the argc
96214** additional parameters which are passed to
96215** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
96216**
96217** A jump is made to P2 if the result set after filtering would be empty.
96218*/
96219case OP_VFilter6: { /* jump */
96220 int nArg;
96221 int iQuery;
96222 const sqlite3_module *pModule;
96223 Mem *pQuery;
96224 Mem *pArgc;
96225 sqlite3_vtab_cursor *pVCur;
96226 sqlite3_vtab *pVtab;
96227 VdbeCursor *pCur;
96228 int res;
96229 int i;
96230 Mem **apArg;
96231
96232 pQuery = &aMem[pOp->p3];
96233 pArgc = &pQuery[1];
96234 pCur = p->apCsr[pOp->p1];
96235 assert( memIsValid(pQuery) )((void) (0));
96236 REGISTER_TRACE(pOp->p3, pQuery);
96237 assert( pCur!=0 )((void) (0));
96238 assert( pCur->eCurType==CURTYPE_VTAB )((void) (0));
96239 pVCur = pCur->uc.pVCur;
96240 pVtab = pVCur->pVtab;
96241 pModule = pVtab->pModule;
96242
96243 /* Grab the index number and argc parameters */
96244 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int )((void) (0));
96245 nArg = (int)pArgc->u.i;
96246 iQuery = (int)pQuery->u.i;
96247
96248 /* Invoke the xFilter method */
96249 apArg = p->apArg;
96250 for(i = 0; i<nArg; i++){
96251 apArg[i] = &pArgc[i+1];
96252 }
96253 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
96254 sqlite3VtabImportErrmsg(p, pVtab);
96255 if( rc ) goto abort_due_to_error;
96256 res = pModule->xEof(pVCur);
96257 pCur->nullRow = 0;
96258 VdbeBranchTaken(res!=0,2);
96259 if( res ) goto jump_to_p2;
96260 break;
96261}
96262#endif /* SQLITE_OMIT_VIRTUALTABLE */
96263
96264#ifndef SQLITE_OMIT_VIRTUALTABLE
96265/* Opcode: VColumn P1 P2 P3 * P5
96266** Synopsis: r[P3]=vcolumn(P2)
96267**
96268** Store in register P3 the value of the P2-th column of
96269** the current row of the virtual-table of cursor P1.
96270**
96271** If the VColumn opcode is being used to fetch the value of
96272** an unchanging column during an UPDATE operation, then the P5
96273** value is OPFLAG_NOCHNG. This will cause the sqlite3_vtab_nochange()
96274** function to return true inside the xColumn method of the virtual
96275** table implementation. The P5 column might also contain other
96276** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are
96277** unused by OP_VColumn.
96278*/
96279case OP_VColumn175: {
96280 sqlite3_vtab *pVtab;
96281 const sqlite3_module *pModule;
96282 Mem *pDest;
96283 sqlite3_context sContext;
96284
96285 VdbeCursor *pCur = p->apCsr[pOp->p1];
96286 assert( pCur!=0 )((void) (0));
96287 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
96288 pDest = &aMem[pOp->p3];
96289 memAboutToChange(p, pDest);
96290 if( pCur->nullRow ){
96291 sqlite3VdbeMemSetNull(pDest);
96292 break;
96293 }
96294 assert( pCur->eCurType==CURTYPE_VTAB )((void) (0));
96295 pVtab = pCur->uc.pVCur->pVtab;
96296 pModule = pVtab->pModule;
96297 assert( pModule->xColumn )((void) (0));
96298 memset(&sContext, 0, sizeof(sContext));
96299 sContext.pOut = pDest;
96300 sContext.enc = encoding;
96301 assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 )((void) (0));
96302 if( pOp->p5 & OPFLAG_NOCHNG0x01 ){
96303 sqlite3VdbeMemSetNull(pDest);
96304 pDest->flags = MEM_Null0x0001|MEM_Zero0x0400;
96305 pDest->u.nZero = 0;
96306 }else{
96307 MemSetTypeFlag(pDest, MEM_Null)((pDest)->flags = ((pDest)->flags&~(0x0dbf|0x0400))
|0x0001)
;
96308 }
96309 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
96310 sqlite3VtabImportErrmsg(p, pVtab);
96311 if( sContext.isError>0 ){
96312 sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest));
96313 rc = sContext.isError;
96314 }
96315 sqlite3VdbeChangeEncoding(pDest, encoding);
96316 REGISTER_TRACE(pOp->p3, pDest);
96317 UPDATE_MAX_BLOBSIZE(pDest);
96318
96319 if( rc ) goto abort_due_to_error;
96320 break;
96321}
96322#endif /* SQLITE_OMIT_VIRTUALTABLE */
96323
96324#ifndef SQLITE_OMIT_VIRTUALTABLE
96325/* Opcode: VNext P1 P2 * * *
96326**
96327** Advance virtual table P1 to the next row in its result set and
96328** jump to instruction P2. Or, if the virtual table has reached
96329** the end of its result set, then fall through to the next instruction.
96330*/
96331case OP_VNext62: { /* jump */
96332 sqlite3_vtab *pVtab;
96333 const sqlite3_module *pModule;
96334 int res;
96335 VdbeCursor *pCur;
96336
96337 pCur = p->apCsr[pOp->p1];
96338 assert( pCur!=0 )((void) (0));
96339 assert( pCur->eCurType==CURTYPE_VTAB )((void) (0));
96340 if( pCur->nullRow ){
96341 break;
96342 }
96343 pVtab = pCur->uc.pVCur->pVtab;
96344 pModule = pVtab->pModule;
96345 assert( pModule->xNext )((void) (0));
96346
96347 /* Invoke the xNext() method of the module. There is no way for the
96348 ** underlying implementation to return an error if one occurs during
96349 ** xNext(). Instead, if an error occurs, true is returned (indicating that
96350 ** data is available) and the error code returned when xColumn or
96351 ** some other method is next invoked on the save virtual table cursor.
96352 */
96353 rc = pModule->xNext(pCur->uc.pVCur);
96354 sqlite3VtabImportErrmsg(p, pVtab);
96355 if( rc ) goto abort_due_to_error;
96356 res = pModule->xEof(pCur->uc.pVCur);
96357 VdbeBranchTaken(!res,2);
96358 if( !res ){
96359 /* If there is data, jump to P2 */
96360 goto jump_to_p2_and_check_for_interrupt;
96361 }
96362 goto check_for_interrupt;
96363}
96364#endif /* SQLITE_OMIT_VIRTUALTABLE */
96365
96366#ifndef SQLITE_OMIT_VIRTUALTABLE
96367/* Opcode: VRename P1 * * P4 *
96368**
96369** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
96370** This opcode invokes the corresponding xRename method. The value
96371** in register P1 is passed as the zName argument to the xRename method.
96372*/
96373case OP_VRename176: {
96374 sqlite3_vtab *pVtab;
96375 Mem *pName;
96376 int isLegacy;
96377
96378 isLegacy = (db->flags & SQLITE_LegacyAlter0x04000000);
96379 db->flags |= SQLITE_LegacyAlter0x04000000;
96380 pVtab = pOp->p4.pVtab->pVtab;
96381 pName = &aMem[pOp->p1];
96382 assert( pVtab->pModule->xRename )((void) (0));
96383 assert( memIsValid(pName) )((void) (0));
96384 assert( p->readOnly==0 )((void) (0));
96385 REGISTER_TRACE(pOp->p1, pName);
96386 assert( pName->flags & MEM_Str )((void) (0));
96387 testcase( pName->enc==SQLITE_UTF8 );
96388 testcase( pName->enc==SQLITE_UTF16BE );
96389 testcase( pName->enc==SQLITE_UTF16LE );
96390 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF81);
96391 if( rc ) goto abort_due_to_error;
96392 rc = pVtab->pModule->xRename(pVtab, pName->z);
96393 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter0x04000000;
96394 sqlite3VtabImportErrmsg(p, pVtab);
96395 p->expired = 0;
96396 if( rc ) goto abort_due_to_error;
96397 break;
96398}
96399#endif
96400
96401#ifndef SQLITE_OMIT_VIRTUALTABLE
96402/* Opcode: VUpdate P1 P2 P3 P4 P5
96403** Synopsis: data=r[P3@P2]
96404**
96405** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
96406** This opcode invokes the corresponding xUpdate method. P2 values
96407** are contiguous memory cells starting at P3 to pass to the xUpdate
96408** invocation. The value in register (P3+P2-1) corresponds to the
96409** p2th element of the argv array passed to xUpdate.
96410**
96411** The xUpdate method will do a DELETE or an INSERT or both.
96412** The argv[0] element (which corresponds to memory cell P3)
96413** is the rowid of a row to delete. If argv[0] is NULL then no
96414** deletion occurs. The argv[1] element is the rowid of the new
96415** row. This can be NULL to have the virtual table select the new
96416** rowid for itself. The subsequent elements in the array are
96417** the values of columns in the new row.
96418**
96419** If P2==1 then no insert is performed. argv[0] is the rowid of
96420** a row to delete.
96421**
96422** P1 is a boolean flag. If it is set to true and the xUpdate call
96423** is successful, then the value returned by sqlite3_last_insert_rowid()
96424** is set to the value of the rowid for the row just inserted.
96425**
96426** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
96427** apply in the case of a constraint failure on an insert or update.
96428*/
96429case OP_VUpdate7: {
96430 sqlite3_vtab *pVtab;
96431 const sqlite3_module *pModule;
96432 int nArg;
96433 int i;
96434 sqlite_int64 rowid = 0;
96435 Mem **apArg;
96436 Mem *pX;
96437
96438 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback((void) (0))
96439 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace((void) (0))
96440 )((void) (0));
96441 assert( p->readOnly==0 )((void) (0));
96442 if( db->mallocFailed ) goto no_mem;
96443 sqlite3VdbeIncrWriteCounter(p, 0);
96444 pVtab = pOp->p4.pVtab->pVtab;
96445 if( pVtab==0 || NEVER(pVtab->pModule==0)(pVtab->pModule==0) ){
96446 rc = SQLITE_LOCKED6;
96447 goto abort_due_to_error;
96448 }
96449 pModule = pVtab->pModule;
96450 nArg = pOp->p2;
96451 assert( pOp->p4type==P4_VTAB )((void) (0));
96452 if( ALWAYS(pModule->xUpdate)(pModule->xUpdate) ){
96453 u8 vtabOnConflict = db->vtabOnConflict;
96454 apArg = p->apArg;
96455 pX = &aMem[pOp->p3];
96456 for(i=0; i<nArg; i++){
96457 assert( memIsValid(pX) )((void) (0));
96458 memAboutToChange(p, pX);
96459 apArg[i] = pX;
96460 pX++;
96461 }
96462 db->vtabOnConflict = pOp->p5;
96463 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
96464 db->vtabOnConflict = vtabOnConflict;
96465 sqlite3VtabImportErrmsg(p, pVtab);
96466 if( rc==SQLITE_OK0 && pOp->p1 ){
96467 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) )((void) (0));
96468 db->lastRowid = rowid;
96469 }
96470 if( (rc&0xff)==SQLITE_CONSTRAINT19 && pOp->p4.pVtab->bConstraint ){
96471 if( pOp->p5==OE_Ignore4 ){
96472 rc = SQLITE_OK0;
96473 }else{
96474 p->errorAction = ((pOp->p5==OE_Replace5) ? OE_Abort2 : pOp->p5);
96475 }
96476 }else{
96477 p->nChange++;
96478 }
96479 if( rc ) goto abort_due_to_error;
96480 }
96481 break;
96482}
96483#endif /* SQLITE_OMIT_VIRTUALTABLE */
96484
96485#ifndef SQLITE_OMIT_PAGER_PRAGMAS
96486/* Opcode: Pagecount P1 P2 * * *
96487**
96488** Write the current number of pages in database P1 to memory cell P2.
96489*/
96490case OP_Pagecount177: { /* out2 */
96491 pOut = out2Prerelease(p, pOp);
96492 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
96493 break;
96494}
96495#endif
96496
96497
96498#ifndef SQLITE_OMIT_PAGER_PRAGMAS
96499/* Opcode: MaxPgcnt P1 P2 P3 * *
96500**
96501** Try to set the maximum page count for database P1 to the value in P3.
96502** Do not let the maximum page count fall below the current page count and
96503** do not change the maximum page count value if P3==0.
96504**
96505** Store the maximum page count after the change in register P2.
96506*/
96507case OP_MaxPgcnt178: { /* out2 */
96508 unsigned int newMax;
96509 Btree *pBt;
96510
96511 pOut = out2Prerelease(p, pOp);
96512 pBt = db->aDb[pOp->p1].pBt;
96513 newMax = 0;
96514 if( pOp->p3 ){
96515 newMax = sqlite3BtreeLastPage(pBt);
96516 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
96517 }
96518 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
96519 break;
96520}
96521#endif
96522
96523/* Opcode: Function P1 P2 P3 P4 *
96524** Synopsis: r[P3]=func(r[P2@NP])
96525**
96526** Invoke a user function (P4 is a pointer to an sqlite3_context object that
96527** contains a pointer to the function to be run) with arguments taken
96528** from register P2 and successors. The number of arguments is in
96529** the sqlite3_context object that P4 points to.
96530** The result of the function is stored
96531** in register P3. Register P3 must not be one of the function inputs.
96532**
96533** P1 is a 32-bit bitmask indicating whether or not each argument to the
96534** function was determined to be constant at compile time. If the first
96535** argument was constant then bit 0 of P1 is set. This is used to determine
96536** whether meta data associated with a user function argument using the
96537** sqlite3_set_auxdata() API may be safely retained until the next
96538** invocation of this opcode.
96539**
96540** See also: AggStep, AggFinal, PureFunc
96541*/
96542/* Opcode: PureFunc P1 P2 P3 P4 *
96543** Synopsis: r[P3]=func(r[P2@NP])
96544**
96545** Invoke a user function (P4 is a pointer to an sqlite3_context object that
96546** contains a pointer to the function to be run) with arguments taken
96547** from register P2 and successors. The number of arguments is in
96548** the sqlite3_context object that P4 points to.
96549** The result of the function is stored
96550** in register P3. Register P3 must not be one of the function inputs.
96551**
96552** P1 is a 32-bit bitmask indicating whether or not each argument to the
96553** function was determined to be constant at compile time. If the first
96554** argument was constant then bit 0 of P1 is set. This is used to determine
96555** whether meta data associated with a user function argument using the
96556** sqlite3_set_auxdata() API may be safely retained until the next
96557** invocation of this opcode.
96558**
96559** This opcode works exactly like OP_Function. The only difference is in
96560** its name. This opcode is used in places where the function must be
96561** purely non-deterministic. Some built-in date/time functions can be
96562** either determinitic of non-deterministic, depending on their arguments.
96563** When those function are used in a non-deterministic way, they will check
96564** to see if they were called using OP_PureFunc instead of OP_Function, and
96565** if they were, they throw an error.
96566**
96567** See also: AggStep, AggFinal, Function
96568*/
96569case OP_PureFunc65: /* group */
96570case OP_Function66: { /* group */
96571 int i;
96572 sqlite3_context *pCtx;
96573
96574 assert( pOp->p4type==P4_FUNCCTX )((void) (0));
96575 pCtx = pOp->p4.pCtx;
96576
96577 /* If this function is inside of a trigger, the register array in aMem[]
96578 ** might change from one evaluation to the next. The next block of code
96579 ** checks to see if the register array has changed, and if so it
96580 ** reinitializes the relavant parts of the sqlite3_context object */
96581 pOut = &aMem[pOp->p3];
96582 if( pCtx->pOut != pOut ){
96583 pCtx->pVdbe = p;
96584 pCtx->pOut = pOut;
96585 pCtx->enc = encoding;
96586 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
96587 }
96588 assert( pCtx->pVdbe==p )((void) (0));
96589
96590 memAboutToChange(p, pOut);
96591#ifdef SQLITE_DEBUG
96592 for(i=0; i<pCtx->argc; i++){
96593 assert( memIsValid(pCtx->argv[i]) )((void) (0));
96594 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
96595 }
96596#endif
96597 MemSetTypeFlag(pOut, MEM_Null)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0001
)
;
96598 assert( pCtx->isError==0 )((void) (0));
96599 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
96600
96601 /* If the function returned an error, throw an exception */
96602 if( pCtx->isError ){
96603 if( pCtx->isError>0 ){
96604 sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
96605 rc = pCtx->isError;
96606 }
96607 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
96608 pCtx->isError = 0;
96609 if( rc ) goto abort_due_to_error;
96610 }
96611
96612 assert( (pOut->flags&MEM_Str)==0((void) (0))
96613 || pOut->enc==encoding((void) (0))
96614 || db->mallocFailed )((void) (0));
96615 assert( !sqlite3VdbeMemTooBig(pOut) )((void) (0));
96616
96617 REGISTER_TRACE(pOp->p3, pOut);
96618 UPDATE_MAX_BLOBSIZE(pOut);
96619 break;
96620}
96621
96622/* Opcode: ClrSubtype P1 * * * *
96623** Synopsis: r[P1].subtype = 0
96624**
96625** Clear the subtype from register P1.
96626*/
96627case OP_ClrSubtype179: { /* in1 */
96628 pIn1 = &aMem[pOp->p1];
96629 pIn1->flags &= ~MEM_Subtype0x0800;
96630 break;
96631}
96632
96633/* Opcode: FilterAdd P1 * P3 P4 *
96634** Synopsis: filter(P1) += key(P3@P4)
96635**
96636** Compute a hash on the P4 registers starting with r[P3] and
96637** add that hash to the bloom filter contained in r[P1].
96638*/
96639case OP_FilterAdd180: {
96640 u64 h;
96641
96642 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) )((void) (0));
96643 pIn1 = &aMem[pOp->p1];
96644 assert( pIn1->flags & MEM_Blob )((void) (0));
96645 assert( pIn1->n>0 )((void) (0));
96646 h = filterHash(aMem, pOp);
96647#ifdef SQLITE_DEBUG
96648 if( db->flags&SQLITE_VdbeTrace ){
96649 int ii;
96650 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
96651 registerTrace(ii, &aMem[ii]);
96652 }
96653 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
96654 }
96655#endif
96656 h %= pIn1->n;
96657 pIn1->z[h/8] |= 1<<(h&7);
96658 break;
96659}
96660
96661/* Opcode: Filter P1 P2 P3 P4 *
96662** Synopsis: if key(P3@P4) not in filter(P1) goto P2
96663**
96664** Compute a hash on the key contained in the P4 registers starting
96665** with r[P3]. Check to see if that hash is found in the
96666** bloom filter hosted by register P1. If it is not present then
96667** maybe jump to P2. Otherwise fall through.
96668**
96669** False negatives are harmless. It is always safe to fall through,
96670** even if the value is in the bloom filter. A false negative causes
96671** more CPU cycles to be used, but it should still yield the correct
96672** answer. However, an incorrect answer may well arise from a
96673** false positive - if the jump is taken when it should fall through.
96674*/
96675case OP_Filter63: { /* jump */
96676 u64 h;
96677
96678 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) )((void) (0));
96679 pIn1 = &aMem[pOp->p1];
96680 assert( (pIn1->flags & MEM_Blob)!=0 )((void) (0));
96681 assert( pIn1->n >= 1 )((void) (0));
96682 h = filterHash(aMem, pOp);
96683#ifdef SQLITE_DEBUG
96684 if( db->flags&SQLITE_VdbeTrace ){
96685 int ii;
96686 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
96687 registerTrace(ii, &aMem[ii]);
96688 }
96689 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
96690 }
96691#endif
96692 h %= pIn1->n;
96693 if( (pIn1->z[h/8] & (1<<(h&7)))==0 ){
96694 VdbeBranchTaken(1, 2);
96695 p->aCounter[SQLITE_STMTSTATUS_FILTER_HIT8]++;
96696 goto jump_to_p2;
96697 }else{
96698 p->aCounter[SQLITE_STMTSTATUS_FILTER_MISS7]++;
96699 VdbeBranchTaken(0, 2);
96700 }
96701 break;
96702}
96703
96704/* Opcode: Trace P1 P2 * P4 *
96705**
96706** Write P4 on the statement trace output if statement tracing is
96707** enabled.
96708**
96709** Operand P1 must be 0x7fffffff and P2 must positive.
96710*/
96711/* Opcode: Init P1 P2 P3 P4 *
96712** Synopsis: Start at P2
96713**
96714** Programs contain a single instance of this opcode as the very first
96715** opcode.
96716**
96717** If tracing is enabled (by the sqlite3_trace()) interface, then
96718** the UTF-8 string contained in P4 is emitted on the trace callback.
96719** Or if P4 is blank, use the string returned by sqlite3_sql().
96720**
96721** If P2 is not zero, jump to instruction P2.
96722**
96723** Increment the value of P1 so that OP_Once opcodes will jump the
96724** first time they are evaluated for this run.
96725**
96726** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
96727** error is encountered.
96728*/
96729case OP_Trace181:
96730case OP_Init64: { /* jump */
96731 int i;
96732#ifndef SQLITE_OMIT_TRACE
96733 char *zTrace;
96734#endif
96735
96736 /* If the P4 argument is not NULL, then it must be an SQL comment string.
96737 ** The "--" string is broken up to prevent false-positives with srcck1.c.
96738 **
96739 ** This assert() provides evidence for:
96740 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
96741 ** would have been returned by the legacy sqlite3_trace() interface by
96742 ** using the X argument when X begins with "--" and invoking
96743 ** sqlite3_expanded_sql(P) otherwise.
96744 */
96745 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 )((void) (0));
96746
96747 /* OP_Init is always instruction 0 */
96748 assert( pOp==p->aOp || pOp->opcode==OP_Trace )((void) (0));
96749
96750#ifndef SQLITE_OMIT_TRACE
96751 if( (db->mTrace & (SQLITE_TRACE_STMT0x01|SQLITE_TRACE_LEGACY0))!=0
96752 && p->minWriteFileFormat!=254 /* tag-20220401a */
96753 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
96754 ){
96755#ifndef SQLITE_OMIT_DEPRECATED1
96756 if( db->mTrace & SQLITE_TRACE_LEGACY0 ){
96757 char *z = sqlite3VdbeExpandSql(p, zTrace);
96758 db->trace.xLegacy(db->pTraceArg, z);
96759 sqlite3_free(z);
96760 }else
96761#endif
96762 if( db->nVdbeExec>1 ){
96763 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
96764 (void)db->trace.xV2(SQLITE_TRACE_STMT0x01, db->pTraceArg, p, z);
96765 sqlite3DbFree(db, z);
96766 }else{
96767 (void)db->trace.xV2(SQLITE_TRACE_STMT0x01, db->pTraceArg, p, zTrace);
96768 }
96769 }
96770#ifdef SQLITE_USE_FCNTL_TRACE
96771 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
96772 if( zTrace ){
96773 int j;
96774 for(j=0; j<db->nDb; j++){
96775 if( DbMaskTest(p->btreeMask, j)(((p->btreeMask)&(((yDbMask)1)<<(j)))!=0)==0 ) continue;
96776 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE19, zTrace);
96777 }
96778 }
96779#endif /* SQLITE_USE_FCNTL_TRACE */
96780#ifdef SQLITE_DEBUG
96781 if( (db->flags & SQLITE_SqlTrace)!=0
96782 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
96783 ){
96784 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
96785 }
96786#endif /* SQLITE_DEBUG */
96787#endif /* SQLITE_OMIT_TRACE */
96788 assert( pOp->p2>0 )((void) (0));
96789 if( pOp->p1>=sqlite3GlobalConfigsqlite3Config.iOnceResetThreshold ){
96790 if( pOp->opcode==OP_Trace181 ) break;
96791 for(i=1; i<p->nOp; i++){
96792 if( p->aOp[i].opcode==OP_Once14 ) p->aOp[i].p1 = 0;
96793 }
96794 pOp->p1 = 0;
96795 }
96796 pOp->p1++;
96797 p->aCounter[SQLITE_STMTSTATUS_RUN6]++;
96798 goto jump_to_p2;
96799}
96800
96801#ifdef SQLITE_ENABLE_CURSOR_HINTS
96802/* Opcode: CursorHint P1 * * P4 *
96803**
96804** Provide a hint to cursor P1 that it only needs to return rows that
96805** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
96806** to values currently held in registers. TK_COLUMN terms in the P4
96807** expression refer to columns in the b-tree to which cursor P1 is pointing.
96808*/
96809case OP_CursorHint182: {
96810 VdbeCursor *pC;
96811
96812 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
96813 assert( pOp->p4type==P4_EXPR )((void) (0));
96814 pC = p->apCsr[pOp->p1];
96815 if( pC ){
96816 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
96817 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE0,
96818 pOp->p4.pExpr, aMem);
96819 }
96820 break;
96821}
96822#endif /* SQLITE_ENABLE_CURSOR_HINTS */
96823
96824#ifdef SQLITE_DEBUG
96825/* Opcode: Abortable * * * * *
96826**
96827** Verify that an Abort can happen. Assert if an Abort at this point
96828** might cause database corruption. This opcode only appears in debugging
96829** builds.
96830**
96831** An Abort is safe if either there have been no writes, or if there is
96832** an active statement journal.
96833*/
96834case OP_Abortable186: {
96835 sqlite3VdbeAssertAbortable(p);
96836 break;
96837}
96838#endif
96839
96840#ifdef SQLITE_DEBUG
96841/* Opcode: ReleaseReg P1 P2 P3 * P5
96842** Synopsis: release r[P1@P2] mask P3
96843**
96844** Release registers from service. Any content that was in the
96845** the registers is unreliable after this opcode completes.
96846**
96847** The registers released will be the P2 registers starting at P1,
96848** except if bit ii of P3 set, then do not release register P1+ii.
96849** In other words, P3 is a mask of registers to preserve.
96850**
96851** Releasing a register clears the Mem.pScopyFrom pointer. That means
96852** that if the content of the released register was set using OP_SCopy,
96853** a change to the value of the source register for the OP_SCopy will no longer
96854** generate an assertion fault in sqlite3VdbeMemAboutToChange().
96855**
96856** If P5 is set, then all released registers have their type set
96857** to MEM_Undefined so that any subsequent attempt to read the released
96858** register (before it is reinitialized) will generate an assertion fault.
96859**
96860** P5 ought to be set on every call to this opcode.
96861** However, there are places in the code generator will release registers
96862** before their are used, under the (valid) assumption that the registers
96863** will not be reallocated for some other purpose before they are used and
96864** hence are safe to release.
96865**
96866** This opcode is only available in testing and debugging builds. It is
96867** not generated for release builds. The purpose of this opcode is to help
96868** validate the generated bytecode. This opcode does not actually contribute
96869** to computing an answer.
96870*/
96871case OP_ReleaseReg183: {
96872 Mem *pMem;
96873 int i;
96874 u32 constMask;
96875 assert( pOp->p1>0 )((void) (0));
96876 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
96877 pMem = &aMem[pOp->p1];
96878 constMask = pOp->p3;
96879 for(i=0; i<pOp->p2; i++, pMem++){
96880 if( i>=32 || (constMask & MASKBIT32(i)(((unsigned int)1)<<(i)))==0 ){
96881 pMem->pScopyFrom = 0;
96882 if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined)((pMem)->flags = ((pMem)->flags&~(0x0dbf|0x0400))|0x0000
)
;
96883 }
96884 }
96885 break;
96886}
96887#endif
96888
96889/* Opcode: Noop * * * * *
96890**
96891** Do nothing. This instruction is often useful as a jump
96892** destination.
96893*/
96894/*
96895** The magic Explain opcode are only inserted when explain==2 (which
96896** is to say when the EXPLAIN QUERY PLAN syntax is used.)
96897** This opcode records information from the optimizer. It is the
96898** the same as a no-op. This opcodesnever appears in a real VM program.
96899*/
96900default: { /* This is really OP_Noop, OP_Explain */
96901 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain )((void) (0));
96902
96903 break;
96904}
96905
96906/*****************************************************************************
96907** The cases of the switch statement above this line should all be indented
96908** by 6 spaces. But the left-most 6 spaces have been removed to improve the
96909** readability. From this point on down, the normal indentation rules are
96910** restored.
96911*****************************************************************************/
96912 }
96913
96914#ifdef VDBE_PROFILE
96915 {
96916 u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
96917 if( endTime>start ) pOrigOp->cycles += endTime - start;
96918 pOrigOp->cnt++;
96919 }
96920#endif
96921
96922 /* The following code adds nothing to the actual functionality
96923 ** of the program. It is only here for testing and debugging.
96924 ** On the other hand, it does burn CPU cycles every time through
96925 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
96926 */
96927#ifndef NDEBUG1
96928 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] )((void) (0));
96929
96930#ifdef SQLITE_DEBUG
96931 if( db->flags & SQLITE_VdbeTrace ){
96932 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
96933 if( rc!=0 ) printf("rc=%d\n",rc);
96934 if( opProperty & (OPFLG_OUT20x10) ){
96935 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
96936 }
96937 if( opProperty & OPFLG_OUT30x20 ){
96938 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
96939 }
96940 if( opProperty==0xff ){
96941 /* Never happens. This code exists to avoid a harmless linkage
96942 ** warning aboud sqlite3VdbeRegisterDump() being defined but not
96943 ** used. */
96944 sqlite3VdbeRegisterDump(p);
96945 }
96946 }
96947#endif /* SQLITE_DEBUG */
96948#endif /* NDEBUG */
96949 } /* The end of the for(;;) loop the loops through opcodes */
96950
96951 /* If we reach this point, it means that execution is finished with
96952 ** an error of some kind.
96953 */
96954abort_due_to_error:
96955 if( db->mallocFailed ){
96956 rc = SQLITE_NOMEM_BKPT7;
96957 }else if( rc==SQLITE_IOERR_CORRUPTFS(10 | (33<<8)) ){
96958 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(96958);
96959 }
96960 assert( rc )((void) (0));
96961#ifdef SQLITE_DEBUG
96962 if( db->flags & SQLITE_VdbeTrace ){
96963 const char *zTrace = p->zSql;
96964 if( zTrace==0 ){
96965 if( aOp[0].opcode==OP_Trace181 ){
96966 zTrace = aOp[0].p4.z;
96967 }
96968 if( zTrace==0 ) zTrace = "???";
96969 }
96970 printf("ABORT-due-to-error (rc=%d): %s\n", rc, zTrace);
96971 }
96972#endif
96973 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
96974 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
96975 }
96976 p->rc = rc;
96977 sqlite3SystemError(db, rc);
96978 testcase( sqlite3GlobalConfig.xLog!=0 );
96979 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
96980 (int)(pOp - aOp), p->zSql, p->zErrMsg);
96981 if( p->eVdbeState==VDBE_RUN_STATE2 ) sqlite3VdbeHalt(p);
96982 if( rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ) sqlite3OomFault(db);
96983 if( rc==SQLITE_CORRUPT11 && db->autoCommit==0 ){
96984 db->flags |= SQLITE_CorruptRdOnly((u64)(0x00002)<<32);
96985 }
96986 rc = SQLITE_ERROR1;
96987 if( resetSchemaOnFault>0 ){
96988 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
96989 }
96990
96991 /* This is the only way out of this procedure. We have to
96992 ** release the mutexes on btrees that were acquired at the
96993 ** top. */
96994vdbe_return:
96995#ifndef SQLITE_OMIT_PROGRESS_CALLBACK1
96996 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
96997 nProgressLimit += db->nProgressOps;
96998 if( db->xProgress(db->pProgressArg) ){
96999 nProgressLimit = LARGEST_UINT64(0xffffffff|(((u64)0xffffffff)<<32));
97000 rc = SQLITE_INTERRUPT9;
97001 goto abort_due_to_error;
97002 }
97003 }
97004#endif
97005 p->aCounter[SQLITE_STMTSTATUS_VM_STEP4] += (int)nVmStep;
97006 sqlite3VdbeLeave(p);
97007 assert( rc!=SQLITE_OK || nExtraDelete==0((void) (0))
97008 || sqlite3_strlike("DELETE%",p->zSql,0)!=0((void) (0))
97009 )((void) (0));
97010 return rc;
97011
97012 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
97013 ** is encountered.
97014 */
97015too_big:
97016 sqlite3VdbeError(p, "string or blob too big");
97017 rc = SQLITE_TOOBIG18;
97018 goto abort_due_to_error;
97019
97020 /* Jump to here if a malloc() fails.
97021 */
97022no_mem:
97023 sqlite3OomFault(db);
97024 sqlite3VdbeError(p, "out of memory");
97025 rc = SQLITE_NOMEM_BKPT7;
97026 goto abort_due_to_error;
97027
97028 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
97029 ** flag.
97030 */
97031abort_due_to_interrupt:
97032 assert( AtomicLoad(&db->u1.isInterrupted) )((void) (0));
97033 rc = SQLITE_INTERRUPT9;
97034 goto abort_due_to_error;
97035}
97036
97037
97038/************** End of vdbe.c ************************************************/
97039/************** Begin file vdbeblob.c ****************************************/
97040/*
97041** 2007 May 1
97042**
97043** The author disclaims copyright to this source code. In place of
97044** a legal notice, here is a blessing:
97045**
97046** May you do good and not evil.
97047** May you find forgiveness for yourself and forgive others.
97048** May you share freely, never taking more than you give.
97049**
97050*************************************************************************
97051**
97052** This file contains code used to implement incremental BLOB I/O.
97053*/
97054
97055/* #include "sqliteInt.h" */
97056/* #include "vdbeInt.h" */
97057
97058#ifndef SQLITE_OMIT_INCRBLOB
97059
97060/*
97061** Valid sqlite3_blob* handles point to Incrblob structures.
97062*/
97063typedef struct Incrblob Incrblob;
97064struct Incrblob {
97065 int nByte; /* Size of open blob, in bytes */
97066 int iOffset; /* Byte offset of blob in cursor data */
97067 u16 iCol; /* Table column this handle is open on */
97068 BtCursor *pCsr; /* Cursor pointing at blob row */
97069 sqlite3_stmt *pStmt; /* Statement holding cursor open */
97070 sqlite3 *db; /* The associated database */
97071 char *zDb; /* Database name */
97072 Table *pTab; /* Table object */
97073};
97074
97075
97076/*
97077** This function is used by both blob_open() and blob_reopen(). It seeks
97078** the b-tree cursor associated with blob handle p to point to row iRow.
97079** If successful, SQLITE_OK is returned and subsequent calls to
97080** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
97081**
97082** If an error occurs, or if the specified row does not exist or does not
97083** contain a value of type TEXT or BLOB in the column nominated when the
97084** blob handle was opened, then an error code is returned and *pzErr may
97085** be set to point to a buffer containing an error message. It is the
97086** responsibility of the caller to free the error message buffer using
97087** sqlite3DbFree().
97088**
97089** If an error does occur, then the b-tree cursor is closed. All subsequent
97090** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will
97091** immediately return SQLITE_ABORT.
97092*/
97093static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
97094 int rc; /* Error code */
97095 char *zErr = 0; /* Error message */
97096 Vdbe *v = (Vdbe *)p->pStmt;
97097
97098 /* Set the value of register r[1] in the SQL statement to integer iRow.
97099 ** This is done directly as a performance optimization
97100 */
97101 v->aMem[1].flags = MEM_Int0x0004;
97102 v->aMem[1].u.i = iRow;
97103
97104 /* If the statement has been run before (and is paused at the OP_ResultRow)
97105 ** then back it up to the point where it does the OP_NotExists. This could
97106 ** have been down with an extra OP_Goto, but simply setting the program
97107 ** counter is faster. */
97108 if( v->pc>4 ){
97109 v->pc = 4;
97110 assert( v->aOp[v->pc].opcode==OP_NotExists )((void) (0));
97111 rc = sqlite3VdbeExec(v);
97112 }else{
97113 rc = sqlite3_step(p->pStmt);
97114 }
97115 if( rc==SQLITE_ROW100 ){
97116 VdbeCursor *pC = v->apCsr[0];
97117 u32 type;
97118 assert( pC!=0 )((void) (0));
97119 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
97120 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
97121 testcase( pC->nHdrParsed==p->iCol );
97122 testcase( pC->nHdrParsed==p->iCol+1 );
97123 if( type<12 ){
97124 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
97125 type==0?"null": type==7?"real": "integer"
97126 );
97127 rc = SQLITE_ERROR1;
97128 sqlite3_finalize(p->pStmt);
97129 p->pStmt = 0;
97130 }else{
97131 p->iOffset = pC->aType[p->iCol + pC->nField];
97132 p->nByte = sqlite3VdbeSerialTypeLen(type);
97133 p->pCsr = pC->uc.pCursor;
97134 sqlite3BtreeIncrblobCursor(p->pCsr);
97135 }
97136 }
97137
97138 if( rc==SQLITE_ROW100 ){
97139 rc = SQLITE_OK0;
97140 }else if( p->pStmt ){
97141 rc = sqlite3_finalize(p->pStmt);
97142 p->pStmt = 0;
97143 if( rc==SQLITE_OK0 ){
97144 zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
97145 rc = SQLITE_ERROR1;
97146 }else{
97147 zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
97148 }
97149 }
97150
97151 assert( rc!=SQLITE_OK || zErr==0 )((void) (0));
97152 assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE )((void) (0));
97153
97154 *pzErr = zErr;
97155 return rc;
97156}
97157
97158/*
97159** Open a blob handle.
97160*/
97161SQLITE_API int sqlite3_blob_open(
97162 sqlite3* db, /* The database connection */
97163 const char *zDb, /* The attached database containing the blob */
97164 const char *zTable, /* The table containing the blob */
97165 const char *zColumn, /* The column containing the blob */
97166 sqlite_int64 iRow, /* The row containing the glob */
97167 int wrFlag, /* True -> read/write access, false -> read-only */
97168 sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */
97169){
97170 int nAttempt = 0;
97171 int iCol; /* Index of zColumn in row-record */
97172 int rc = SQLITE_OK0;
97173 char *zErr = 0;
97174 Table *pTab;
97175 Incrblob *pBlob = 0;
97176 Parse sParse;
97177
97178#ifdef SQLITE_ENABLE_API_ARMOR1
97179 if( ppBlob==0 ){
97180 return SQLITE_MISUSE_BKPTsqlite3MisuseError(97180);
97181 }
97182#endif
97183 *ppBlob = 0;
97184#ifdef SQLITE_ENABLE_API_ARMOR1
97185 if( !sqlite3SafetyCheckOk(db) || zTable==0 ){
97186 return SQLITE_MISUSE_BKPTsqlite3MisuseError(97186);
97187 }
97188#endif
97189 wrFlag = !!wrFlag; /* wrFlag = (wrFlag ? 1 : 0); */
97190
97191 sqlite3_mutex_enter(db->mutex);
97192
97193 pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
97194 while(1){
97195 sqlite3ParseObjectInit(&sParse,db);
97196 if( !pBlob ) goto blob_open_out;
97197 sqlite3DbFree(db, zErr);
97198 zErr = 0;
97199
97200 sqlite3BtreeEnterAll(db);
97201 pTab = sqlite3LocateTable(&sParse, 0, zTable, zDb);
97202 if( pTab && IsVirtual(pTab)((pTab)->eTabType==1) ){
97203 pTab = 0;
97204 sqlite3ErrorMsg(&sParse, "cannot open virtual table: %s", zTable);
97205 }
97206 if( pTab && !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
97207 pTab = 0;
97208 sqlite3ErrorMsg(&sParse, "cannot open table without rowid: %s", zTable);
97209 }
97210#ifndef SQLITE_OMIT_VIEW
97211 if( pTab && IsView(pTab)((pTab)->eTabType==2) ){
97212 pTab = 0;
97213 sqlite3ErrorMsg(&sParse, "cannot open view: %s", zTable);
97214 }
97215#endif
97216 if( !pTab ){
97217 if( sParse.zErrMsg ){
97218 sqlite3DbFree(db, zErr);
97219 zErr = sParse.zErrMsg;
97220 sParse.zErrMsg = 0;
97221 }
97222 rc = SQLITE_ERROR1;
97223 sqlite3BtreeLeaveAll(db);
97224 goto blob_open_out;
97225 }
97226 pBlob->pTab = pTab;
97227 pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
97228
97229 /* Now search pTab for the exact column. */
97230 for(iCol=0; iCol<pTab->nCol; iCol++) {
97231 if( sqlite3StrICmp(pTab->aCol[iCol].zCnName, zColumn)==0 ){
97232 break;
97233 }
97234 }
97235 if( iCol==pTab->nCol ){
97236 sqlite3DbFree(db, zErr);
97237 zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
97238 rc = SQLITE_ERROR1;
97239 sqlite3BtreeLeaveAll(db);
97240 goto blob_open_out;
97241 }
97242
97243 /* If the value is being opened for writing, check that the
97244 ** column is not indexed, and that it is not part of a foreign key.
97245 */
97246 if( wrFlag ){
97247 const char *zFault = 0;
97248 Index *pIdx;
97249#ifndef SQLITE_OMIT_FOREIGN_KEY
97250 if( db->flags&SQLITE_ForeignKeys0x00004000 ){
97251 /* Check that the column is not part of an FK child key definition. It
97252 ** is not necessary to check if it is part of a parent key, as parent
97253 ** key columns must be indexed. The check below will pick up this
97254 ** case. */
97255 FKey *pFKey;
97256 assert( IsOrdinaryTable(pTab) )((void) (0));
97257 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
97258 int j;
97259 for(j=0; j<pFKey->nCol; j++){
97260 if( pFKey->aCol[j].iFrom==iCol ){
97261 zFault = "foreign key";
97262 }
97263 }
97264 }
97265 }
97266#endif
97267 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
97268 int j;
97269 for(j=0; j<pIdx->nKeyCol; j++){
97270 /* FIXME: Be smarter about indexes that use expressions */
97271 if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR(-2) ){
97272 zFault = "indexed";
97273 }
97274 }
97275 }
97276 if( zFault ){
97277 sqlite3DbFree(db, zErr);
97278 zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
97279 rc = SQLITE_ERROR1;
97280 sqlite3BtreeLeaveAll(db);
97281 goto blob_open_out;
97282 }
97283 }
97284
97285 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(&sParse);
97286 assert( pBlob->pStmt || db->mallocFailed )((void) (0));
97287 if( pBlob->pStmt ){
97288
97289 /* This VDBE program seeks a btree cursor to the identified
97290 ** db/table/row entry. The reason for using a vdbe program instead
97291 ** of writing code to use the b-tree layer directly is that the
97292 ** vdbe program will take advantage of the various transaction,
97293 ** locking and error handling infrastructure built into the vdbe.
97294 **
97295 ** After seeking the cursor, the vdbe executes an OP_ResultRow.
97296 ** Code external to the Vdbe then "borrows" the b-tree cursor and
97297 ** uses it to implement the blob_read(), blob_write() and
97298 ** blob_bytes() functions.
97299 **
97300 ** The sqlite3_blob_close() function finalizes the vdbe program,
97301 ** which closes the b-tree cursor and (possibly) commits the
97302 ** transaction.
97303 */
97304 static const int iLn = VDBE_OFFSET_LINENO(2)0;
97305 static const VdbeOpList openBlob[] = {
97306 {OP_TableLock169, 0, 0, 0}, /* 0: Acquire a read or write lock */
97307 {OP_OpenRead112, 0, 0, 0}, /* 1: Open a cursor */
97308 /* blobSeekToRow() will initialize r[1] to the desired rowid */
97309 {OP_NotExists30, 0, 5, 1}, /* 2: Seek the cursor to rowid=r[1] */
97310 {OP_Column94, 0, 0, 1}, /* 3 */
97311 {OP_ResultRow84, 1, 0, 0}, /* 4 */
97312 {OP_Halt70, 0, 0, 0}, /* 5 */
97313 };
97314 Vdbe *v = (Vdbe *)pBlob->pStmt;
97315 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
97316 VdbeOp *aOp;
97317
97318 sqlite3VdbeAddOp4Int(v, OP_Transaction2, iDb, wrFlag,
97319 pTab->pSchema->schema_cookie,
97320 pTab->pSchema->iGeneration);
97321 sqlite3VdbeChangeP5(v, 1);
97322 assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed )((void) (0));
97323 aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob)((int)(sizeof(openBlob)/sizeof(openBlob[0]))), openBlob, iLn);
97324
97325 /* Make sure a mutex is held on the table to be accessed */
97326 sqlite3VdbeUsesBtree(v, iDb);
97327
97328 if( db->mallocFailed==0 ){
97329 assert( aOp!=0 )((void) (0));
97330 /* Configure the OP_TableLock instruction */
97331#ifdef SQLITE_OMIT_SHARED_CACHE
97332 aOp[0].opcode = OP_Noop184;
97333#else
97334 aOp[0].p1 = iDb;
97335 aOp[0].p2 = pTab->tnum;
97336 aOp[0].p3 = wrFlag;
97337 sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT0);
97338 }
97339 if( db->mallocFailed==0 ){
97340#endif
97341
97342 /* Remove either the OP_OpenWrite or OpenRead. Set the P2
97343 ** parameter of the other to pTab->tnum. */
97344 if( wrFlag ) aOp[1].opcode = OP_OpenWrite113;
97345 aOp[1].p2 = pTab->tnum;
97346 aOp[1].p3 = iDb;
97347
97348 /* Configure the number of columns. Configure the cursor to
97349 ** think that the table has one more column than it really
97350 ** does. An OP_Column to retrieve this imaginary column will
97351 ** always return an SQL NULL. This is useful because it means
97352 ** we can invoke OP_Column to fill in the vdbe cursors type
97353 ** and offset cache without causing any IO.
97354 */
97355 aOp[1].p4type = P4_INT32(-3);
97356 aOp[1].p4.i = pTab->nCol+1;
97357 aOp[3].p2 = pTab->nCol;
97358
97359 sParse.nVar = 0;
97360 sParse.nMem = 1;
97361 sParse.nTab = 1;
97362 sqlite3VdbeMakeReady(v, &sParse);
97363 }
97364 }
97365
97366 pBlob->iCol = iCol;
97367 pBlob->db = db;
97368 sqlite3BtreeLeaveAll(db);
97369 if( db->mallocFailed ){
97370 goto blob_open_out;
97371 }
97372 rc = blobSeekToRow(pBlob, iRow, &zErr);
97373 if( (++nAttempt)>=SQLITE_MAX_SCHEMA_RETRY50 || rc!=SQLITE_SCHEMA17 ) break;
97374 sqlite3ParseObjectReset(&sParse);
97375 }
97376
97377blob_open_out:
97378 if( rc==SQLITE_OK0 && db->mallocFailed==0 ){
97379 *ppBlob = (sqlite3_blob *)pBlob;
97380 }else{
97381 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
97382 sqlite3DbFree(db, pBlob);
97383 }
97384 sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
97385 sqlite3DbFree(db, zErr);
97386 sqlite3ParseObjectReset(&sParse);
97387 rc = sqlite3ApiExit(db, rc);
97388 sqlite3_mutex_leave(db->mutex);
97389 return rc;
97390}
97391
97392/*
97393** Close a blob handle that was previously created using
97394** sqlite3_blob_open().
97395*/
97396SQLITE_API int sqlite3_blob_close(sqlite3_blob *pBlob){
97397 Incrblob *p = (Incrblob *)pBlob;
97398 int rc;
97399 sqlite3 *db;
97400
97401 if( p ){
97402 sqlite3_stmt *pStmt = p->pStmt;
97403 db = p->db;
97404 sqlite3_mutex_enter(db->mutex);
97405 sqlite3DbFree(db, p);
97406 sqlite3_mutex_leave(db->mutex);
97407 rc = sqlite3_finalize(pStmt);
97408 }else{
97409 rc = SQLITE_OK0;
97410 }
97411 return rc;
97412}
97413
97414/*
97415** Perform a read or write operation on a blob
97416*/
97417static int blobReadWrite(
97418 sqlite3_blob *pBlob,
97419 void *z,
97420 int n,
97421 int iOffset,
97422 int (*xCall)(BtCursor*, u32, u32, void*)
97423){
97424 int rc;
97425 Incrblob *p = (Incrblob *)pBlob;
97426 Vdbe *v;
97427 sqlite3 *db;
97428
97429 if( p==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(97429);
97430 db = p->db;
97431 sqlite3_mutex_enter(db->mutex);
97432 v = (Vdbe*)p->pStmt;
97433
97434 if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
97435 /* Request is out of range. Return a transient error. */
97436 rc = SQLITE_ERROR1;
97437 }else if( v==0 ){
97438 /* If there is no statement handle, then the blob-handle has
97439 ** already been invalidated. Return SQLITE_ABORT in this case.
97440 */
97441 rc = SQLITE_ABORT4;
97442 }else{
97443 /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
97444 ** returned, clean-up the statement handle.
97445 */
97446 assert( db == v->db )((void) (0));
97447 sqlite3BtreeEnterCursor(p->pCsr);
97448
97449#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
97450 if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
97451 /* If a pre-update hook is registered and this is a write cursor,
97452 ** invoke it here.
97453 **
97454 ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
97455 ** operation should really be an SQLITE_UPDATE. This is probably
97456 ** incorrect, but is convenient because at this point the new.* values
97457 ** are not easily obtainable. And for the sessions module, an
97458 ** SQLITE_UPDATE where the PK columns do not change is handled in the
97459 ** same way as an SQLITE_DELETE (the SQLITE_DELETE code is actually
97460 ** slightly more efficient). Since you cannot write to a PK column
97461 ** using the incremental-blob API, this works. For the sessions module
97462 ** anyhow.
97463 */
97464 sqlite3_int64 iKey;
97465 iKey = sqlite3BtreeIntegerKey(p->pCsr);
97466 assert( v->apCsr[0]!=0 )((void) (0));
97467 assert( v->apCsr[0]->eCurType==CURTYPE_BTREE )((void) (0));
97468 sqlite3VdbePreUpdateHook(
97469 v, v->apCsr[0], SQLITE_DELETE9, p->zDb, p->pTab, iKey, -1, p->iCol
97470 );
97471 }
97472#endif
97473
97474 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
97475 sqlite3BtreeLeaveCursor(p->pCsr);
97476 if( rc==SQLITE_ABORT4 ){
97477 sqlite3VdbeFinalize(v);
97478 p->pStmt = 0;
97479 }else{
97480 v->rc = rc;
97481 }
97482 }
97483 sqlite3Error(db, rc);
97484 rc = sqlite3ApiExit(db, rc);
97485 sqlite3_mutex_leave(db->mutex);
97486 return rc;
97487}
97488
97489/*
97490** Read data from a blob handle.
97491*/
97492SQLITE_API int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
97493 return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreePayloadChecked);
97494}
97495
97496/*
97497** Write data to a blob handle.
97498*/
97499SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
97500 return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
97501}
97502
97503/*
97504** Query a blob handle for the size of the data.
97505**
97506** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
97507** so no mutex is required for access.
97508*/
97509SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *pBlob){
97510 Incrblob *p = (Incrblob *)pBlob;
97511 return (p && p->pStmt) ? p->nByte : 0;
97512}
97513
97514/*
97515** Move an existing blob handle to point to a different row of the same
97516** database table.
97517**
97518** If an error occurs, or if the specified row does not exist or does not
97519** contain a blob or text value, then an error code is returned and the
97520** database handle error code and message set. If this happens, then all
97521** subsequent calls to sqlite3_blob_xxx() functions (except blob_close())
97522** immediately return SQLITE_ABORT.
97523*/
97524SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
97525 int rc;
97526 Incrblob *p = (Incrblob *)pBlob;
97527 sqlite3 *db;
97528
97529 if( p==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(97529);
97530 db = p->db;
97531 sqlite3_mutex_enter(db->mutex);
97532
97533 if( p->pStmt==0 ){
97534 /* If there is no statement handle, then the blob-handle has
97535 ** already been invalidated. Return SQLITE_ABORT in this case.
97536 */
97537 rc = SQLITE_ABORT4;
97538 }else{
97539 char *zErr;
97540 ((Vdbe*)p->pStmt)->rc = SQLITE_OK0;
97541 rc = blobSeekToRow(p, iRow, &zErr);
97542 if( rc!=SQLITE_OK0 ){
97543 sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
97544 sqlite3DbFree(db, zErr);
97545 }
97546 assert( rc!=SQLITE_SCHEMA )((void) (0));
97547 }
97548
97549 rc = sqlite3ApiExit(db, rc);
97550 assert( rc==SQLITE_OK || p->pStmt==0 )((void) (0));
97551 sqlite3_mutex_leave(db->mutex);
97552 return rc;
97553}
97554
97555#endif /* #ifndef SQLITE_OMIT_INCRBLOB */
97556
97557/************** End of vdbeblob.c ********************************************/
97558/************** Begin file vdbesort.c ****************************************/
97559/*
97560** 2011-07-09
97561**
97562** The author disclaims copyright to this source code. In place of
97563** a legal notice, here is a blessing:
97564**
97565** May you do good and not evil.
97566** May you find forgiveness for yourself and forgive others.
97567** May you share freely, never taking more than you give.
97568**
97569*************************************************************************
97570** This file contains code for the VdbeSorter object, used in concert with
97571** a VdbeCursor to sort large numbers of keys for CREATE INDEX statements
97572** or by SELECT statements with ORDER BY clauses that cannot be satisfied
97573** using indexes and without LIMIT clauses.
97574**
97575** The VdbeSorter object implements a multi-threaded external merge sort
97576** algorithm that is efficient even if the number of elements being sorted
97577** exceeds the available memory.
97578**
97579** Here is the (internal, non-API) interface between this module and the
97580** rest of the SQLite system:
97581**
97582** sqlite3VdbeSorterInit() Create a new VdbeSorter object.
97583**
97584** sqlite3VdbeSorterWrite() Add a single new row to the VdbeSorter
97585** object. The row is a binary blob in the
97586** OP_MakeRecord format that contains both
97587** the ORDER BY key columns and result columns
97588** in the case of a SELECT w/ ORDER BY, or
97589** the complete record for an index entry
97590** in the case of a CREATE INDEX.
97591**
97592** sqlite3VdbeSorterRewind() Sort all content previously added.
97593** Position the read cursor on the
97594** first sorted element.
97595**
97596** sqlite3VdbeSorterNext() Advance the read cursor to the next sorted
97597** element.
97598**
97599** sqlite3VdbeSorterRowkey() Return the complete binary blob for the
97600** row currently under the read cursor.
97601**
97602** sqlite3VdbeSorterCompare() Compare the binary blob for the row
97603** currently under the read cursor against
97604** another binary blob X and report if
97605** X is strictly less than the read cursor.
97606** Used to enforce uniqueness in a
97607** CREATE UNIQUE INDEX statement.
97608**
97609** sqlite3VdbeSorterClose() Close the VdbeSorter object and reclaim
97610** all resources.
97611**
97612** sqlite3VdbeSorterReset() Refurbish the VdbeSorter for reuse. This
97613** is like Close() followed by Init() only
97614** much faster.
97615**
97616** The interfaces above must be called in a particular order. Write() can
97617** only occur in between Init()/Reset() and Rewind(). Next(), Rowkey(), and
97618** Compare() can only occur in between Rewind() and Close()/Reset(). i.e.
97619**
97620** Init()
97621** for each record: Write()
97622** Rewind()
97623** Rowkey()/Compare()
97624** Next()
97625** Close()
97626**
97627** Algorithm:
97628**
97629** Records passed to the sorter via calls to Write() are initially held
97630** unsorted in main memory. Assuming the amount of memory used never exceeds
97631** a threshold, when Rewind() is called the set of records is sorted using
97632** an in-memory merge sort. In this case, no temporary files are required
97633** and subsequent calls to Rowkey(), Next() and Compare() read records
97634** directly from main memory.
97635**
97636** If the amount of space used to store records in main memory exceeds the
97637** threshold, then the set of records currently in memory are sorted and
97638** written to a temporary file in "Packed Memory Array" (PMA) format.
97639** A PMA created at this point is known as a "level-0 PMA". Higher levels
97640** of PMAs may be created by merging existing PMAs together - for example
97641** merging two or more level-0 PMAs together creates a level-1 PMA.
97642**
97643** The threshold for the amount of main memory to use before flushing
97644** records to a PMA is roughly the same as the limit configured for the
97645** page-cache of the main database. Specifically, the threshold is set to
97646** the value returned by "PRAGMA main.page_size" multipled by
97647** that returned by "PRAGMA main.cache_size", in bytes.
97648**
97649** If the sorter is running in single-threaded mode, then all PMAs generated
97650** are appended to a single temporary file. Or, if the sorter is running in
97651** multi-threaded mode then up to (N+1) temporary files may be opened, where
97652** N is the configured number of worker threads. In this case, instead of
97653** sorting the records and writing the PMA to a temporary file itself, the
97654** calling thread usually launches a worker thread to do so. Except, if
97655** there are already N worker threads running, the main thread does the work
97656** itself.
97657**
97658** The sorter is running in multi-threaded mode if (a) the library was built
97659** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
97660** than zero, and (b) worker threads have been enabled at runtime by calling
97661** "PRAGMA threads=N" with some value of N greater than 0.
97662**
97663** When Rewind() is called, any data remaining in memory is flushed to a
97664** final PMA. So at this point the data is stored in some number of sorted
97665** PMAs within temporary files on disk.
97666**
97667** If there are fewer than SORTER_MAX_MERGE_COUNT PMAs in total and the
97668** sorter is running in single-threaded mode, then these PMAs are merged
97669** incrementally as keys are retreived from the sorter by the VDBE. The
97670** MergeEngine object, described in further detail below, performs this
97671** merge.
97672**
97673** Or, if running in multi-threaded mode, then a background thread is
97674** launched to merge the existing PMAs. Once the background thread has
97675** merged T bytes of data into a single sorted PMA, the main thread
97676** begins reading keys from that PMA while the background thread proceeds
97677** with merging the next T bytes of data. And so on.
97678**
97679** Parameter T is set to half the value of the memory threshold used
97680** by Write() above to determine when to create a new PMA.
97681**
97682** If there are more than SORTER_MAX_MERGE_COUNT PMAs in total when
97683** Rewind() is called, then a hierarchy of incremental-merges is used.
97684** First, T bytes of data from the first SORTER_MAX_MERGE_COUNT PMAs on
97685** disk are merged together. Then T bytes of data from the second set, and
97686** so on, such that no operation ever merges more than SORTER_MAX_MERGE_COUNT
97687** PMAs at a time. This done is to improve locality.
97688**
97689** If running in multi-threaded mode and there are more than
97690** SORTER_MAX_MERGE_COUNT PMAs on disk when Rewind() is called, then more
97691** than one background thread may be created. Specifically, there may be
97692** one background thread for each temporary file on disk, and one background
97693** thread to merge the output of each of the others to a single PMA for
97694** the main thread to read from.
97695*/
97696/* #include "sqliteInt.h" */
97697/* #include "vdbeInt.h" */
97698
97699/*
97700** If SQLITE_DEBUG_SORTER_THREADS is defined, this module outputs various
97701** messages to stderr that may be helpful in understanding the performance
97702** characteristics of the sorter in multi-threaded mode.
97703*/
97704#if 0
97705# define SQLITE_DEBUG_SORTER_THREADS 1
97706#endif
97707
97708/*
97709** Hard-coded maximum amount of data to accumulate in memory before flushing
97710** to a level 0 PMA. The purpose of this limit is to prevent various integer
97711** overflows. 512MiB.
97712*/
97713#define SQLITE_MAX_PMASZ(1<<29) (1<<29)
97714
97715/*
97716** Private objects used by the sorter
97717*/
97718typedef struct MergeEngine MergeEngine; /* Merge PMAs together */
97719typedef struct PmaReader PmaReader; /* Incrementally read one PMA */
97720typedef struct PmaWriter PmaWriter; /* Incrementally write one PMA */
97721typedef struct SorterRecord SorterRecord; /* A record being sorted */
97722typedef struct SortSubtask SortSubtask; /* A sub-task in the sort process */
97723typedef struct SorterFile SorterFile; /* Temporary file object wrapper */
97724typedef struct SorterList SorterList; /* In-memory list of records */
97725typedef struct IncrMerger IncrMerger; /* Read & merge multiple PMAs */
97726
97727/*
97728** A container for a temp file handle and the current amount of data
97729** stored in the file.
97730*/
97731struct SorterFile {
97732 sqlite3_file *pFd; /* File handle */
97733 i64 iEof; /* Bytes of data stored in pFd */
97734};
97735
97736/*
97737** An in-memory list of objects to be sorted.
97738**
97739** If aMemory==0 then each object is allocated separately and the objects
97740** are connected using SorterRecord.u.pNext. If aMemory!=0 then all objects
97741** are stored in the aMemory[] bulk memory, one right after the other, and
97742** are connected using SorterRecord.u.iNext.
97743*/
97744struct SorterList {
97745 SorterRecord *pList; /* Linked list of records */
97746 u8 *aMemory; /* If non-NULL, bulk memory to hold pList */
97747 int szPMA; /* Size of pList as PMA in bytes */
97748};
97749
97750/*
97751** The MergeEngine object is used to combine two or more smaller PMAs into
97752** one big PMA using a merge operation. Separate PMAs all need to be
97753** combined into one big PMA in order to be able to step through the sorted
97754** records in order.
97755**
97756** The aReadr[] array contains a PmaReader object for each of the PMAs being
97757** merged. An aReadr[] object either points to a valid key or else is at EOF.
97758** ("EOF" means "End Of File". When aReadr[] is at EOF there is no more data.)
97759** For the purposes of the paragraphs below, we assume that the array is
97760** actually N elements in size, where N is the smallest power of 2 greater
97761** to or equal to the number of PMAs being merged. The extra aReadr[] elements
97762** are treated as if they are empty (always at EOF).
97763**
97764** The aTree[] array is also N elements in size. The value of N is stored in
97765** the MergeEngine.nTree variable.
97766**
97767** The final (N/2) elements of aTree[] contain the results of comparing
97768** pairs of PMA keys together. Element i contains the result of
97769** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the
97770** aTree element is set to the index of it.
97771**
97772** For the purposes of this comparison, EOF is considered greater than any
97773** other key value. If the keys are equal (only possible with two EOF
97774** values), it doesn't matter which index is stored.
97775**
97776** The (N/4) elements of aTree[] that precede the final (N/2) described
97777** above contains the index of the smallest of each block of 4 PmaReaders
97778** And so on. So that aTree[1] contains the index of the PmaReader that
97779** currently points to the smallest key value. aTree[0] is unused.
97780**
97781** Example:
97782**
97783** aReadr[0] -> Banana
97784** aReadr[1] -> Feijoa
97785** aReadr[2] -> Elderberry
97786** aReadr[3] -> Currant
97787** aReadr[4] -> Grapefruit
97788** aReadr[5] -> Apple
97789** aReadr[6] -> Durian
97790** aReadr[7] -> EOF
97791**
97792** aTree[] = { X, 5 0, 5 0, 3, 5, 6 }
97793**
97794** The current element is "Apple" (the value of the key indicated by
97795** PmaReader 5). When the Next() operation is invoked, PmaReader 5 will
97796** be advanced to the next key in its segment. Say the next key is
97797** "Eggplant":
97798**
97799** aReadr[5] -> Eggplant
97800**
97801** The contents of aTree[] are updated first by comparing the new PmaReader
97802** 5 key to the current key of PmaReader 4 (still "Grapefruit"). The PmaReader
97803** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
97804** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader
97805** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
97806** so the value written into element 1 of the array is 0. As follows:
97807**
97808** aTree[] = { X, 0 0, 6 0, 3, 5, 6 }
97809**
97810** In other words, each time we advance to the next sorter element, log2(N)
97811** key comparison operations are required, where N is the number of segments
97812** being merged (rounded up to the next power of 2).
97813*/
97814struct MergeEngine {
97815 int nTree; /* Used size of aTree/aReadr (power of 2) */
97816 SortSubtask *pTask; /* Used by this thread only */
97817 int *aTree; /* Current state of incremental merge */
97818 PmaReader *aReadr; /* Array of PmaReaders to merge data from */
97819};
97820
97821/*
97822** This object represents a single thread of control in a sort operation.
97823** Exactly VdbeSorter.nTask instances of this object are allocated
97824** as part of each VdbeSorter object. Instances are never allocated any
97825** other way. VdbeSorter.nTask is set to the number of worker threads allowed
97826** (see SQLITE_CONFIG_WORKER_THREADS) plus one (the main thread). Thus for
97827** single-threaded operation, there is exactly one instance of this object
97828** and for multi-threaded operation there are two or more instances.
97829**
97830** Essentially, this structure contains all those fields of the VdbeSorter
97831** structure for which each thread requires a separate instance. For example,
97832** each thread requries its own UnpackedRecord object to unpack records in
97833** as part of comparison operations.
97834**
97835** Before a background thread is launched, variable bDone is set to 0. Then,
97836** right before it exits, the thread itself sets bDone to 1. This is used for
97837** two purposes:
97838**
97839** 1. When flushing the contents of memory to a level-0 PMA on disk, to
97840** attempt to select a SortSubtask for which there is not already an
97841** active background thread (since doing so causes the main thread
97842** to block until it finishes).
97843**
97844** 2. If SQLITE_DEBUG_SORTER_THREADS is defined, to determine if a call
97845** to sqlite3ThreadJoin() is likely to block. Cases that are likely to
97846** block provoke debugging output.
97847**
97848** In both cases, the effects of the main thread seeing (bDone==0) even
97849** after the thread has finished are not dire. So we don't worry about
97850** memory barriers and such here.
97851*/
97852typedef int (*SorterCompare)(SortSubtask*,int*,const void*,int,const void*,int);
97853struct SortSubtask {
97854 SQLiteThread *pThread; /* Background thread, if any */
97855 int bDone; /* Set if thread is finished but not joined */
97856 VdbeSorter *pSorter; /* Sorter that owns this sub-task */
97857 UnpackedRecord *pUnpacked; /* Space to unpack a record */
97858 SorterList list; /* List for thread to write to a PMA */
97859 int nPMA; /* Number of PMAs currently in file */
97860 SorterCompare xCompare; /* Compare function to use */
97861 SorterFile file; /* Temp file for level-0 PMAs */
97862 SorterFile file2; /* Space for other PMAs */
97863};
97864
97865
97866/*
97867** Main sorter structure. A single instance of this is allocated for each
97868** sorter cursor created by the VDBE.
97869**
97870** mxKeysize:
97871** As records are added to the sorter by calls to sqlite3VdbeSorterWrite(),
97872** this variable is updated so as to be set to the size on disk of the
97873** largest record in the sorter.
97874*/
97875struct VdbeSorter {
97876 int mnPmaSize; /* Minimum PMA size, in bytes */
97877 int mxPmaSize; /* Maximum PMA size, in bytes. 0==no limit */
97878 int mxKeysize; /* Largest serialized key seen so far */
97879 int pgsz; /* Main database page size */
97880 PmaReader *pReader; /* Readr data from here after Rewind() */
97881 MergeEngine *pMerger; /* Or here, if bUseThreads==0 */
97882 sqlite3 *db; /* Database connection */
97883 KeyInfo *pKeyInfo; /* How to compare records */
97884 UnpackedRecord *pUnpacked; /* Used by VdbeSorterCompare() */
97885 SorterList list; /* List of in-memory records */
97886 int iMemory; /* Offset of free space in list.aMemory */
97887 int nMemory; /* Size of list.aMemory allocation in bytes */
97888 u8 bUsePMA; /* True if one or more PMAs created */
97889 u8 bUseThreads; /* True to use background threads */
97890 u8 iPrev; /* Previous thread used to flush PMA */
97891 u8 nTask; /* Size of aTask[] array */
97892 u8 typeMask;
97893 SortSubtask aTask[1]; /* One or more subtasks */
97894};
97895
97896#define SORTER_TYPE_INTEGER0x01 0x01
97897#define SORTER_TYPE_TEXT0x02 0x02
97898
97899/*
97900** An instance of the following object is used to read records out of a
97901** PMA, in sorted order. The next key to be read is cached in nKey/aKey.
97902** aKey might point into aMap or into aBuffer. If neither of those locations
97903** contain a contiguous representation of the key, then aAlloc is allocated
97904** and the key is copied into aAlloc and aKey is made to poitn to aAlloc.
97905**
97906** pFd==0 at EOF.
97907*/
97908struct PmaReader {
97909 i64 iReadOff; /* Current read offset */
97910 i64 iEof; /* 1 byte past EOF for this PmaReader */
97911 int nAlloc; /* Bytes of space at aAlloc */
97912 int nKey; /* Number of bytes in key */
97913 sqlite3_file *pFd; /* File handle we are reading from */
97914 u8 *aAlloc; /* Space for aKey if aBuffer and pMap wont work */
97915 u8 *aKey; /* Pointer to current key */
97916 u8 *aBuffer; /* Current read buffer */
97917 int nBuffer; /* Size of read buffer in bytes */
97918 u8 *aMap; /* Pointer to mapping of entire file */
97919 IncrMerger *pIncr; /* Incremental merger */
97920};
97921
97922/*
97923** Normally, a PmaReader object iterates through an existing PMA stored
97924** within a temp file. However, if the PmaReader.pIncr variable points to
97925** an object of the following type, it may be used to iterate/merge through
97926** multiple PMAs simultaneously.
97927**
97928** There are two types of IncrMerger object - single (bUseThread==0) and
97929** multi-threaded (bUseThread==1).
97930**
97931** A multi-threaded IncrMerger object uses two temporary files - aFile[0]
97932** and aFile[1]. Neither file is allowed to grow to more than mxSz bytes in
97933** size. When the IncrMerger is initialized, it reads enough data from
97934** pMerger to populate aFile[0]. It then sets variables within the
97935** corresponding PmaReader object to read from that file and kicks off
97936** a background thread to populate aFile[1] with the next mxSz bytes of
97937** sorted record data from pMerger.
97938**
97939** When the PmaReader reaches the end of aFile[0], it blocks until the
97940** background thread has finished populating aFile[1]. It then exchanges
97941** the contents of the aFile[0] and aFile[1] variables within this structure,
97942** sets the PmaReader fields to read from the new aFile[0] and kicks off
97943** another background thread to populate the new aFile[1]. And so on, until
97944** the contents of pMerger are exhausted.
97945**
97946** A single-threaded IncrMerger does not open any temporary files of its
97947** own. Instead, it has exclusive access to mxSz bytes of space beginning
97948** at offset iStartOff of file pTask->file2. And instead of using a
97949** background thread to prepare data for the PmaReader, with a single
97950** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with
97951** keys from pMerger by the calling thread whenever the PmaReader runs out
97952** of data.
97953*/
97954struct IncrMerger {
97955 SortSubtask *pTask; /* Task that owns this merger */
97956 MergeEngine *pMerger; /* Merge engine thread reads data from */
97957 i64 iStartOff; /* Offset to start writing file at */
97958 int mxSz; /* Maximum bytes of data to store */
97959 int bEof; /* Set to true when merge is finished */
97960 int bUseThread; /* True to use a bg thread for this object */
97961 SorterFile aFile[2]; /* aFile[0] for reading, [1] for writing */
97962};
97963
97964/*
97965** An instance of this object is used for writing a PMA.
97966**
97967** The PMA is written one record at a time. Each record is of an arbitrary
97968** size. But I/O is more efficient if it occurs in page-sized blocks where
97969** each block is aligned on a page boundary. This object caches writes to
97970** the PMA so that aligned, page-size blocks are written.
97971*/
97972struct PmaWriter {
97973 int eFWErr; /* Non-zero if in an error state */
97974 u8 *aBuffer; /* Pointer to write buffer */
97975 int nBuffer; /* Size of write buffer in bytes */
97976 int iBufStart; /* First byte of buffer to write */
97977 int iBufEnd; /* Last byte of buffer to write */
97978 i64 iWriteOff; /* Offset of start of buffer in file */
97979 sqlite3_file *pFd; /* File handle to write to */
97980};
97981
97982/*
97983** This object is the header on a single record while that record is being
97984** held in memory and prior to being written out as part of a PMA.
97985**
97986** How the linked list is connected depends on how memory is being managed
97987** by this module. If using a separate allocation for each in-memory record
97988** (VdbeSorter.list.aMemory==0), then the list is always connected using the
97989** SorterRecord.u.pNext pointers.
97990**
97991** Or, if using the single large allocation method (VdbeSorter.list.aMemory!=0),
97992** then while records are being accumulated the list is linked using the
97993** SorterRecord.u.iNext offset. This is because the aMemory[] array may
97994** be sqlite3Realloc()ed while records are being accumulated. Once the VM
97995** has finished passing records to the sorter, or when the in-memory buffer
97996** is full, the list is sorted. As part of the sorting process, it is
97997** converted to use the SorterRecord.u.pNext pointers. See function
97998** vdbeSorterSort() for details.
97999*/
98000struct SorterRecord {
98001 int nVal; /* Size of the record in bytes */
98002 union {
98003 SorterRecord *pNext; /* Pointer to next record in list */
98004 int iNext; /* Offset within aMemory of next record */
98005 } u;
98006 /* The data for the record immediately follows this header */
98007};
98008
98009/* Return a pointer to the buffer containing the record data for SorterRecord
98010** object p. Should be used as if:
98011**
98012** void *SRVAL(SorterRecord *p) { return (void*)&p[1]; }
98013*/
98014#define SRVAL(p)((void*)((SorterRecord*)(p) + 1)) ((void*)((SorterRecord*)(p) + 1))
98015
98016
98017/* Maximum number of PMAs that a single MergeEngine can merge */
98018#define SORTER_MAX_MERGE_COUNT16 16
98019
98020static int vdbeIncrSwap(IncrMerger*);
98021static void vdbeIncrFree(IncrMerger *);
98022
98023/*
98024** Free all memory belonging to the PmaReader object passed as the
98025** argument. All structure fields are set to zero before returning.
98026*/
98027static void vdbePmaReaderClear(PmaReader *pReadr){
98028 sqlite3_free(pReadr->aAlloc);
98029 sqlite3_free(pReadr->aBuffer);
98030 if( pReadr->aMap ) sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
98031 vdbeIncrFree(pReadr->pIncr);
98032 memset(pReadr, 0, sizeof(PmaReader));
98033}
98034
98035/*
98036** Read the next nByte bytes of data from the PMA p.
98037** If successful, set *ppOut to point to a buffer containing the data
98038** and return SQLITE_OK. Otherwise, if an error occurs, return an SQLite
98039** error code.
98040**
98041** The buffer returned in *ppOut is only valid until the
98042** next call to this function.
98043*/
98044static int vdbePmaReadBlob(
98045 PmaReader *p, /* PmaReader from which to take the blob */
98046 int nByte, /* Bytes of data to read */
98047 u8 **ppOut /* OUT: Pointer to buffer containing data */
98048){
98049 int iBuf; /* Offset within buffer to read from */
98050 int nAvail; /* Bytes of data available in buffer */
98051
98052 if( p->aMap ){
98053 *ppOut = &p->aMap[p->iReadOff];
98054 p->iReadOff += nByte;
98055 return SQLITE_OK0;
98056 }
98057
98058 assert( p->aBuffer )((void) (0));
98059
98060 /* If there is no more data to be read from the buffer, read the next
98061 ** p->nBuffer bytes of data from the file into it. Or, if there are less
98062 ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */
98063 iBuf = p->iReadOff % p->nBuffer;
98064 if( iBuf==0 ){
98065 int nRead; /* Bytes to read from disk */
98066 int rc; /* sqlite3OsRead() return code */
98067
98068 /* Determine how many bytes of data to read. */
98069 if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
98070 nRead = p->nBuffer;
98071 }else{
98072 nRead = (int)(p->iEof - p->iReadOff);
98073 }
98074 assert( nRead>0 )((void) (0));
98075
98076 /* Readr data from the file. Return early if an error occurs. */
98077 rc = sqlite3OsRead(p->pFd, p->aBuffer, nRead, p->iReadOff);
98078 assert( rc!=SQLITE_IOERR_SHORT_READ )((void) (0));
98079 if( rc!=SQLITE_OK0 ) return rc;
98080 }
98081 nAvail = p->nBuffer - iBuf;
98082
98083 if( nByte<=nAvail ){
98084 /* The requested data is available in the in-memory buffer. In this
98085 ** case there is no need to make a copy of the data, just return a
98086 ** pointer into the buffer to the caller. */
98087 *ppOut = &p->aBuffer[iBuf];
98088 p->iReadOff += nByte;
98089 }else{
98090 /* The requested data is not all available in the in-memory buffer.
98091 ** In this case, allocate space at p->aAlloc[] to copy the requested
98092 ** range into. Then return a copy of pointer p->aAlloc to the caller. */
98093 int nRem; /* Bytes remaining to copy */
98094
98095 /* Extend the p->aAlloc[] allocation if required. */
98096 if( p->nAlloc<nByte ){
98097 u8 *aNew;
98098 sqlite3_int64 nNew = MAX(128, 2*(sqlite3_int64)p->nAlloc)((128)>(2*(sqlite3_int64)p->nAlloc)?(128):(2*(sqlite3_int64
)p->nAlloc))
;
98099 while( nByte>nNew ) nNew = nNew*2;
98100 aNew = sqlite3Realloc(p->aAlloc, nNew);
98101 if( !aNew ) return SQLITE_NOMEM_BKPT7;
98102 p->nAlloc = nNew;
98103 p->aAlloc = aNew;
98104 }
98105
98106 /* Copy as much data as is available in the buffer into the start of
98107 ** p->aAlloc[]. */
98108 memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
98109 p->iReadOff += nAvail;
98110 nRem = nByte - nAvail;
98111
98112 /* The following loop copies up to p->nBuffer bytes per iteration into
98113 ** the p->aAlloc[] buffer. */
98114 while( nRem>0 ){
98115 int rc; /* vdbePmaReadBlob() return code */
98116 int nCopy; /* Number of bytes to copy */
98117 u8 *aNext; /* Pointer to buffer to copy data from */
98118
98119 nCopy = nRem;
98120 if( nRem>p->nBuffer ) nCopy = p->nBuffer;
98121 rc = vdbePmaReadBlob(p, nCopy, &aNext);
98122 if( rc!=SQLITE_OK0 ) return rc;
98123 assert( aNext!=p->aAlloc )((void) (0));
98124 memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
98125 nRem -= nCopy;
98126 }
98127
98128 *ppOut = p->aAlloc;
98129 }
98130
98131 return SQLITE_OK0;
98132}
98133
98134/*
98135** Read a varint from the stream of data accessed by p. Set *pnOut to
98136** the value read.
98137*/
98138static int vdbePmaReadVarint(PmaReader *p, u64 *pnOut){
98139 int iBuf;
98140
98141 if( p->aMap ){
98142 p->iReadOff += sqlite3GetVarint(&p->aMap[p->iReadOff], pnOut);
98143 }else{
98144 iBuf = p->iReadOff % p->nBuffer;
98145 if( iBuf && (p->nBuffer-iBuf)>=9 ){
98146 p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
98147 }else{
98148 u8 aVarint[16], *a;
98149 int i = 0, rc;
98150 do{
98151 rc = vdbePmaReadBlob(p, 1, &a);
98152 if( rc ) return rc;
98153 aVarint[(i++)&0xf] = a[0];
98154 }while( (a[0]&0x80)!=0 );
98155 sqlite3GetVarint(aVarint, pnOut);
98156 }
98157 }
98158
98159 return SQLITE_OK0;
98160}
98161
98162/*
98163** Attempt to memory map file pFile. If successful, set *pp to point to the
98164** new mapping and return SQLITE_OK. If the mapping is not attempted
98165** (because the file is too large or the VFS layer is configured not to use
98166** mmap), return SQLITE_OK and set *pp to NULL.
98167**
98168** Or, if an error occurs, return an SQLite error code. The final value of
98169** *pp is undefined in this case.
98170*/
98171static int vdbeSorterMapFile(SortSubtask *pTask, SorterFile *pFile, u8 **pp){
98172 int rc = SQLITE_OK0;
98173 if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){
98174 sqlite3_file *pFd = pFile->pFd;
98175 if( pFd->pMethods->iVersion>=3 ){
98176 rc = sqlite3OsFetch(pFd, 0, (int)pFile->iEof, (void**)pp);
98177 testcase( rc!=SQLITE_OK );
98178 }
98179 }
98180 return rc;
98181}
98182
98183/*
98184** Attach PmaReader pReadr to file pFile (if it is not already attached to
98185** that file) and seek it to offset iOff within the file. Return SQLITE_OK
98186** if successful, or an SQLite error code if an error occurs.
98187*/
98188static int vdbePmaReaderSeek(
98189 SortSubtask *pTask, /* Task context */
98190 PmaReader *pReadr, /* Reader whose cursor is to be moved */
98191 SorterFile *pFile, /* Sorter file to read from */
98192 i64 iOff /* Offset in pFile */
98193){
98194 int rc = SQLITE_OK0;
98195
98196 assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 )((void) (0));
98197
98198 if( sqlite3FaultSim(201) ) return SQLITE_IOERR_READ(10 | (1<<8));
98199 if( pReadr->aMap ){
98200 sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
98201 pReadr->aMap = 0;
98202 }
98203 pReadr->iReadOff = iOff;
98204 pReadr->iEof = pFile->iEof;
98205 pReadr->pFd = pFile->pFd;
98206
98207 rc = vdbeSorterMapFile(pTask, pFile, &pReadr->aMap);
98208 if( rc==SQLITE_OK0 && pReadr->aMap==0 ){
98209 int pgsz = pTask->pSorter->pgsz;
98210 int iBuf = pReadr->iReadOff % pgsz;
98211 if( pReadr->aBuffer==0 ){
98212 pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz);
98213 if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT7;
98214 pReadr->nBuffer = pgsz;
98215 }
98216 if( rc==SQLITE_OK0 && iBuf ){
98217 int nRead = pgsz - iBuf;
98218 if( (pReadr->iReadOff + nRead) > pReadr->iEof ){
98219 nRead = (int)(pReadr->iEof - pReadr->iReadOff);
98220 }
98221 rc = sqlite3OsRead(
98222 pReadr->pFd, &pReadr->aBuffer[iBuf], nRead, pReadr->iReadOff
98223 );
98224 testcase( rc!=SQLITE_OK );
98225 }
98226 }
98227
98228 return rc;
98229}
98230
98231/*
98232** Advance PmaReader pReadr to the next key in its PMA. Return SQLITE_OK if
98233** no error occurs, or an SQLite error code if one does.
98234*/
98235static int vdbePmaReaderNext(PmaReader *pReadr){
98236 int rc = SQLITE_OK0; /* Return Code */
98237 u64 nRec = 0; /* Size of record in bytes */
98238
98239
98240 if( pReadr->iReadOff>=pReadr->iEof ){
98241 IncrMerger *pIncr = pReadr->pIncr;
98242 int bEof = 1;
98243 if( pIncr ){
98244 rc = vdbeIncrSwap(pIncr);
98245 if( rc==SQLITE_OK0 && pIncr->bEof==0 ){
98246 rc = vdbePmaReaderSeek(
98247 pIncr->pTask, pReadr, &pIncr->aFile[0], pIncr->iStartOff
98248 );
98249 bEof = 0;
98250 }
98251 }
98252
98253 if( bEof ){
98254 /* This is an EOF condition */
98255 vdbePmaReaderClear(pReadr);
98256 testcase( rc!=SQLITE_OK );
98257 return rc;
98258 }
98259 }
98260
98261 if( rc==SQLITE_OK0 ){
98262 rc = vdbePmaReadVarint(pReadr, &nRec);
98263 }
98264 if( rc==SQLITE_OK0 ){
98265 pReadr->nKey = (int)nRec;
98266 rc = vdbePmaReadBlob(pReadr, (int)nRec, &pReadr->aKey);
98267 testcase( rc!=SQLITE_OK );
98268 }
98269
98270 return rc;
98271}
98272
98273/*
98274** Initialize PmaReader pReadr to scan through the PMA stored in file pFile
98275** starting at offset iStart and ending at offset iEof-1. This function
98276** leaves the PmaReader pointing to the first key in the PMA (or EOF if the
98277** PMA is empty).
98278**
98279** If the pnByte parameter is NULL, then it is assumed that the file
98280** contains a single PMA, and that that PMA omits the initial length varint.
98281*/
98282static int vdbePmaReaderInit(
98283 SortSubtask *pTask, /* Task context */
98284 SorterFile *pFile, /* Sorter file to read from */
98285 i64 iStart, /* Start offset in pFile */
98286 PmaReader *pReadr, /* PmaReader to populate */
98287 i64 *pnByte /* IN/OUT: Increment this value by PMA size */
98288){
98289 int rc;
98290
98291 assert( pFile->iEof>iStart )((void) (0));
98292 assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 )((void) (0));
98293 assert( pReadr->aBuffer==0 )((void) (0));
98294 assert( pReadr->aMap==0 )((void) (0));
98295
98296 rc = vdbePmaReaderSeek(pTask, pReadr, pFile, iStart);
98297 if( rc==SQLITE_OK0 ){
98298 u64 nByte = 0; /* Size of PMA in bytes */
98299 rc = vdbePmaReadVarint(pReadr, &nByte);
98300 pReadr->iEof = pReadr->iReadOff + nByte;
98301 *pnByte += nByte;
98302 }
98303
98304 if( rc==SQLITE_OK0 ){
98305 rc = vdbePmaReaderNext(pReadr);
98306 }
98307 return rc;
98308}
98309
98310/*
98311** A version of vdbeSorterCompare() that assumes that it has already been
98312** determined that the first field of key1 is equal to the first field of
98313** key2.
98314*/
98315static int vdbeSorterCompareTail(
98316 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
98317 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
98318 const void *pKey1, int nKey1, /* Left side of comparison */
98319 const void *pKey2, int nKey2 /* Right side of comparison */
98320){
98321 UnpackedRecord *r2 = pTask->pUnpacked;
98322 if( *pbKey2Cached==0 ){
98323 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
98324 *pbKey2Cached = 1;
98325 }
98326 return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, r2, 1);
98327}
98328
98329/*
98330** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2,
98331** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
98332** used by the comparison. Return the result of the comparison.
98333**
98334** If IN/OUT parameter *pbKey2Cached is true when this function is called,
98335** it is assumed that (pTask->pUnpacked) contains the unpacked version
98336** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
98337** version of key2 and *pbKey2Cached set to true before returning.
98338**
98339** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
98340** to SQLITE_NOMEM.
98341*/
98342static int vdbeSorterCompare(
98343 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
98344 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
98345 const void *pKey1, int nKey1, /* Left side of comparison */
98346 const void *pKey2, int nKey2 /* Right side of comparison */
98347){
98348 UnpackedRecord *r2 = pTask->pUnpacked;
98349 if( !*pbKey2Cached ){
98350 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
98351 *pbKey2Cached = 1;
98352 }
98353 return sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
98354}
98355
98356/*
98357** A specially optimized version of vdbeSorterCompare() that assumes that
98358** the first field of each key is a TEXT value and that the collation
98359** sequence to compare them with is BINARY.
98360*/
98361static int vdbeSorterCompareText(
98362 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
98363 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
98364 const void *pKey1, int nKey1, /* Left side of comparison */
98365 const void *pKey2, int nKey2 /* Right side of comparison */
98366){
98367 const u8 * const p1 = (const u8 * const)pKey1;
98368 const u8 * const p2 = (const u8 * const)pKey2;
98369 const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */
98370 const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */
98371
98372 int n1;
98373 int n2;
98374 int res;
98375
98376 getVarint32NR(&p1[1], n1)n1=(u32)*(&p1[1]);if(n1>=0x80)sqlite3GetVarint32((&
p1[1]),(u32*)&(n1))
;
98377 getVarint32NR(&p2[1], n2)n2=(u32)*(&p2[1]);if(n2>=0x80)sqlite3GetVarint32((&
p2[1]),(u32*)&(n2))
;
98378 res = memcmp(v1, v2, (MIN(n1, n2)((n1)<(n2)?(n1):(n2)) - 13)/2);
98379 if( res==0 ){
98380 res = n1 - n2;
98381 }
98382
98383 if( res==0 ){
98384 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
98385 res = vdbeSorterCompareTail(
98386 pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
98387 );
98388 }
98389 }else{
98390 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) )((void) (0));
98391 if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
98392 res = res * -1;
98393 }
98394 }
98395
98396 return res;
98397}
98398
98399/*
98400** A specially optimized version of vdbeSorterCompare() that assumes that
98401** the first field of each key is an INTEGER value.
98402*/
98403static int vdbeSorterCompareInt(
98404 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
98405 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
98406 const void *pKey1, int nKey1, /* Left side of comparison */
98407 const void *pKey2, int nKey2 /* Right side of comparison */
98408){
98409 const u8 * const p1 = (const u8 * const)pKey1;
98410 const u8 * const p2 = (const u8 * const)pKey2;
98411 const int s1 = p1[1]; /* Left hand serial type */
98412 const int s2 = p2[1]; /* Right hand serial type */
98413 const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */
98414 const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */
98415 int res; /* Return value */
98416
98417 assert( (s1>0 && s1<7) || s1==8 || s1==9 )((void) (0));
98418 assert( (s2>0 && s2<7) || s2==8 || s2==9 )((void) (0));
98419
98420 if( s1==s2 ){
98421 /* The two values have the same sign. Compare using memcmp(). */
98422 static const u8 aLen[] = {0, 1, 2, 3, 4, 6, 8, 0, 0, 0 };
98423 const u8 n = aLen[s1];
98424 int i;
98425 res = 0;
98426 for(i=0; i<n; i++){
98427 if( (res = v1[i] - v2[i])!=0 ){
98428 if( ((v1[0] ^ v2[0]) & 0x80)!=0 ){
98429 res = v1[0] & 0x80 ? -1 : +1;
98430 }
98431 break;
98432 }
98433 }
98434 }else if( s1>7 && s2>7 ){
98435 res = s1 - s2;
98436 }else{
98437 if( s2>7 ){
98438 res = +1;
98439 }else if( s1>7 ){
98440 res = -1;
98441 }else{
98442 res = s1 - s2;
98443 }
98444 assert( res!=0 )((void) (0));
98445
98446 if( res>0 ){
98447 if( *v1 & 0x80 ) res = -1;
98448 }else{
98449 if( *v2 & 0x80 ) res = +1;
98450 }
98451 }
98452
98453 if( res==0 ){
98454 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
98455 res = vdbeSorterCompareTail(
98456 pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
98457 );
98458 }
98459 }else if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
98460 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) )((void) (0));
98461 res = res * -1;
98462 }
98463
98464 return res;
98465}
98466
98467/*
98468** Initialize the temporary index cursor just opened as a sorter cursor.
98469**
98470** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nKeyField)
98471** to determine the number of fields that should be compared from the
98472** records being sorted. However, if the value passed as argument nField
98473** is non-zero and the sorter is able to guarantee a stable sort, nField
98474** is used instead. This is used when sorting records for a CREATE INDEX
98475** statement. In this case, keys are always delivered to the sorter in
98476** order of the primary key, which happens to be make up the final part
98477** of the records being sorted. So if the sort is stable, there is never
98478** any reason to compare PK fields and they can be ignored for a small
98479** performance boost.
98480**
98481** The sorter can guarantee a stable sort when running in single-threaded
98482** mode, but not in multi-threaded mode.
98483**
98484** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
98485*/
98486SQLITE_PRIVATEstatic int sqlite3VdbeSorterInit(
98487 sqlite3 *db, /* Database connection (for malloc()) */
98488 int nField, /* Number of key fields in each record */
98489 VdbeCursor *pCsr /* Cursor that holds the new sorter */
98490){
98491 int pgsz; /* Page size of main database */
98492 int i; /* Used to iterate through aTask[] */
98493 VdbeSorter *pSorter; /* The new sorter */
98494 KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */
98495 int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */
98496 int sz; /* Size of pSorter in bytes */
98497 int rc = SQLITE_OK0;
98498#if SQLITE_MAX_WORKER_THREADS8==0
98499# define nWorker 0
98500#else
98501 int nWorker;
98502#endif
98503
98504 /* Initialize the upper limit on the number of worker threads */
98505#if SQLITE_MAX_WORKER_THREADS8>0
98506 if( sqlite3TempInMemory(db) || sqlite3GlobalConfigsqlite3Config.bCoreMutex==0 ){
98507 nWorker = 0;
98508 }else{
98509 nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS11];
98510 }
98511#endif
98512
98513 /* Do not allow the total number of threads (main thread + all workers)
98514 ** to exceed the maximum merge count */
98515#if SQLITE_MAX_WORKER_THREADS8>=SORTER_MAX_MERGE_COUNT16
98516 if( nWorker>=SORTER_MAX_MERGE_COUNT16 ){
98517 nWorker = SORTER_MAX_MERGE_COUNT16-1;
98518 }
98519#endif
98520
98521 assert( pCsr->pKeyInfo )((void) (0));
98522 assert( !pCsr->isEphemeral )((void) (0));
98523 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
98524 szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nKeyField-1)*sizeof(CollSeq*);
98525 sz = sizeof(VdbeSorter) + nWorker * sizeof(SortSubtask);
98526
98527 pSorter = (VdbeSorter*)sqlite3DbMallocZero(db, sz + szKeyInfo);
98528 pCsr->uc.pSorter = pSorter;
98529 if( pSorter==0 ){
98530 rc = SQLITE_NOMEM_BKPT7;
98531 }else{
98532 Btree *pBt = db->aDb[0].pBt;
98533 pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
98534 memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
98535 pKeyInfo->db = 0;
98536 if( nField && nWorker==0 ){
98537 pKeyInfo->nKeyField = nField;
98538 }
98539 sqlite3BtreeEnter(pBt);
98540 pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(pBt);
98541 sqlite3BtreeLeave(pBt);
98542 pSorter->nTask = nWorker + 1;
98543 pSorter->iPrev = (u8)(nWorker - 1);
98544 pSorter->bUseThreads = (pSorter->nTask>1);
98545 pSorter->db = db;
98546 for(i=0; i<pSorter->nTask; i++){
98547 SortSubtask *pTask = &pSorter->aTask[i];
98548 pTask->pSorter = pSorter;
98549 }
98550
98551 if( !sqlite3TempInMemory(db) ){
98552 i64 mxCache; /* Cache size in bytes*/
98553 u32 szPma = sqlite3GlobalConfigsqlite3Config.szPma;
98554 pSorter->mnPmaSize = szPma * pgsz;
98555
98556 mxCache = db->aDb[0].pSchema->cache_size;
98557 if( mxCache<0 ){
98558 /* A negative cache-size value C indicates that the cache is abs(C)
98559 ** KiB in size. */
98560 mxCache = mxCache * -1024;
98561 }else{
98562 mxCache = mxCache * pgsz;
98563 }
98564 mxCache = MIN(mxCache, SQLITE_MAX_PMASZ)((mxCache)<((1<<29))?(mxCache):((1<<29)));
98565 pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache)((pSorter->mnPmaSize)>((int)mxCache)?(pSorter->mnPmaSize
):((int)mxCache))
;
98566
98567 /* Avoid large memory allocations if the application has requested
98568 ** SQLITE_CONFIG_SMALL_MALLOC. */
98569 if( sqlite3GlobalConfigsqlite3Config.bSmallMalloc==0 ){
98570 assert( pSorter->iMemory==0 )((void) (0));
98571 pSorter->nMemory = pgsz;
98572 pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
98573 if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT7;
98574 }
98575 }
98576
98577 if( pKeyInfo->nAllField<13
98578 && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
98579 && (pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL0x02)==0
98580 ){
98581 pSorter->typeMask = SORTER_TYPE_INTEGER0x01 | SORTER_TYPE_TEXT0x02;
98582 }
98583 }
98584
98585 return rc;
98586}
98587#undef nWorker /* Defined at the top of this function */
98588
98589/*
98590** Free the list of sorted records starting at pRecord.
98591*/
98592static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){
98593 SorterRecord *p;
98594 SorterRecord *pNext;
98595 for(p=pRecord; p; p=pNext){
98596 pNext = p->u.pNext;
98597 sqlite3DbFree(db, p);
98598 }
98599}
98600
98601/*
98602** Free all resources owned by the object indicated by argument pTask. All
98603** fields of *pTask are zeroed before returning.
98604*/
98605static void vdbeSortSubtaskCleanup(sqlite3 *db, SortSubtask *pTask){
98606 sqlite3DbFree(db, pTask->pUnpacked);
98607#if SQLITE_MAX_WORKER_THREADS8>0
98608 /* pTask->list.aMemory can only be non-zero if it was handed memory
98609 ** from the main thread. That only occurs SQLITE_MAX_WORKER_THREADS>0 */
98610 if( pTask->list.aMemory ){
98611 sqlite3_free(pTask->list.aMemory);
98612 }else
98613#endif
98614 {
98615 assert( pTask->list.aMemory==0 )((void) (0));
98616 vdbeSorterRecordFree(0, pTask->list.pList);
98617 }
98618 if( pTask->file.pFd ){
98619 sqlite3OsCloseFree(pTask->file.pFd);
98620 }
98621 if( pTask->file2.pFd ){
98622 sqlite3OsCloseFree(pTask->file2.pFd);
98623 }
98624 memset(pTask, 0, sizeof(SortSubtask));
98625}
98626
98627#ifdef SQLITE_DEBUG_SORTER_THREADS
98628static void vdbeSorterWorkDebug(SortSubtask *pTask, const char *zEvent){
98629 i64 t;
98630 int iTask = (pTask - pTask->pSorter->aTask);
98631 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
98632 fprintf(stderrstderr, "%lld:%d %s\n", t, iTask, zEvent);
98633}
98634static void vdbeSorterRewindDebug(const char *zEvent){
98635 i64 t = 0;
98636 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
98637 if( ALWAYS(pVfs)(pVfs) ) sqlite3OsCurrentTimeInt64(pVfs, &t);
98638 fprintf(stderrstderr, "%lld:X %s\n", t, zEvent);
98639}
98640static void vdbeSorterPopulateDebug(
98641 SortSubtask *pTask,
98642 const char *zEvent
98643){
98644 i64 t;
98645 int iTask = (pTask - pTask->pSorter->aTask);
98646 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
98647 fprintf(stderrstderr, "%lld:bg%d %s\n", t, iTask, zEvent);
98648}
98649static void vdbeSorterBlockDebug(
98650 SortSubtask *pTask,
98651 int bBlocked,
98652 const char *zEvent
98653){
98654 if( bBlocked ){
98655 i64 t;
98656 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
98657 fprintf(stderrstderr, "%lld:main %s\n", t, zEvent);
98658 }
98659}
98660#else
98661# define vdbeSorterWorkDebug(x,y)
98662# define vdbeSorterRewindDebug(y)
98663# define vdbeSorterPopulateDebug(x,y)
98664# define vdbeSorterBlockDebug(x,y,z)
98665#endif
98666
98667#if SQLITE_MAX_WORKER_THREADS8>0
98668/*
98669** Join thread pTask->thread.
98670*/
98671static int vdbeSorterJoinThread(SortSubtask *pTask){
98672 int rc = SQLITE_OK0;
98673 if( pTask->pThread ){
98674#ifdef SQLITE_DEBUG_SORTER_THREADS
98675 int bDone = pTask->bDone;
98676#endif
98677 void *pRet = SQLITE_INT_TO_PTR(SQLITE_ERROR)((void*)(long int)(1));
98678 vdbeSorterBlockDebug(pTask, !bDone, "enter");
98679 (void)sqlite3ThreadJoin(pTask->pThread, &pRet);
98680 vdbeSorterBlockDebug(pTask, !bDone, "exit");
98681 rc = SQLITE_PTR_TO_INT(pRet)((int)(long int)(pRet));
98682 assert( pTask->bDone==1 )((void) (0));
98683 pTask->bDone = 0;
98684 pTask->pThread = 0;
98685 }
98686 return rc;
98687}
98688
98689/*
98690** Launch a background thread to run xTask(pIn).
98691*/
98692static int vdbeSorterCreateThread(
98693 SortSubtask *pTask, /* Thread will use this task object */
98694 void *(*xTask)(void*), /* Routine to run in a separate thread */
98695 void *pIn /* Argument passed into xTask() */
98696){
98697 assert( pTask->pThread==0 && pTask->bDone==0 )((void) (0));
98698 return sqlite3ThreadCreate(&pTask->pThread, xTask, pIn);
98699}
98700
98701/*
98702** Join all outstanding threads launched by SorterWrite() to create
98703** level-0 PMAs.
98704*/
98705static int vdbeSorterJoinAll(VdbeSorter *pSorter, int rcin){
98706 int rc = rcin;
98707 int i;
98708
98709 /* This function is always called by the main user thread.
98710 **
98711 ** If this function is being called after SorterRewind() has been called,
98712 ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread
98713 ** is currently attempt to join one of the other threads. To avoid a race
98714 ** condition where this thread also attempts to join the same object, join
98715 ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
98716 for(i=pSorter->nTask-1; i>=0; i--){
98717 SortSubtask *pTask = &pSorter->aTask[i];
98718 int rc2 = vdbeSorterJoinThread(pTask);
98719 if( rc==SQLITE_OK0 ) rc = rc2;
98720 }
98721 return rc;
98722}
98723#else
98724# define vdbeSorterJoinAll(x,rcin) (rcin)
98725# define vdbeSorterJoinThread(pTask) SQLITE_OK0
98726#endif
98727
98728/*
98729** Allocate a new MergeEngine object capable of handling up to
98730** nReader PmaReader inputs.
98731**
98732** nReader is automatically rounded up to the next power of two.
98733** nReader may not exceed SORTER_MAX_MERGE_COUNT even after rounding up.
98734*/
98735static MergeEngine *vdbeMergeEngineNew(int nReader){
98736 int N = 2; /* Smallest power of two >= nReader */
98737 int nByte; /* Total bytes of space to allocate */
98738 MergeEngine *pNew; /* Pointer to allocated object to return */
98739
98740 assert( nReader<=SORTER_MAX_MERGE_COUNT )((void) (0));
98741
98742 while( N<nReader ) N += N;
98743 nByte = sizeof(MergeEngine) + N * (sizeof(int) + sizeof(PmaReader));
98744
98745 pNew = sqlite3FaultSim(100) ? 0 : (MergeEngine*)sqlite3MallocZero(nByte);
98746 if( pNew ){
98747 pNew->nTree = N;
98748 pNew->pTask = 0;
98749 pNew->aReadr = (PmaReader*)&pNew[1];
98750 pNew->aTree = (int*)&pNew->aReadr[N];
98751 }
98752 return pNew;
98753}
98754
98755/*
98756** Free the MergeEngine object passed as the only argument.
98757*/
98758static void vdbeMergeEngineFree(MergeEngine *pMerger){
98759 int i;
98760 if( pMerger ){
98761 for(i=0; i<pMerger->nTree; i++){
98762 vdbePmaReaderClear(&pMerger->aReadr[i]);
98763 }
98764 }
98765 sqlite3_free(pMerger);
98766}
98767
98768/*
98769** Free all resources associated with the IncrMerger object indicated by
98770** the first argument.
98771*/
98772static void vdbeIncrFree(IncrMerger *pIncr){
98773 if( pIncr ){
98774#if SQLITE_MAX_WORKER_THREADS8>0
98775 if( pIncr->bUseThread ){
98776 vdbeSorterJoinThread(pIncr->pTask);
98777 if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pIncr->aFile[0].pFd);
98778 if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pIncr->aFile[1].pFd);
98779 }
98780#endif
98781 vdbeMergeEngineFree(pIncr->pMerger);
98782 sqlite3_free(pIncr);
98783 }
98784}
98785
98786/*
98787** Reset a sorting cursor back to its original empty state.
98788*/
98789SQLITE_PRIVATEstatic void sqlite3VdbeSorterReset(sqlite3 *db, VdbeSorter *pSorter){
98790 int i;
98791 (void)vdbeSorterJoinAll(pSorter, SQLITE_OK0);
98792 assert( pSorter->bUseThreads || pSorter->pReader==0 )((void) (0));
98793#if SQLITE_MAX_WORKER_THREADS8>0
98794 if( pSorter->pReader ){
98795 vdbePmaReaderClear(pSorter->pReader);
98796 sqlite3DbFree(db, pSorter->pReader);
98797 pSorter->pReader = 0;
98798 }
98799#endif
98800 vdbeMergeEngineFree(pSorter->pMerger);
98801 pSorter->pMerger = 0;
98802 for(i=0; i<pSorter->nTask; i++){
98803 SortSubtask *pTask = &pSorter->aTask[i];
98804 vdbeSortSubtaskCleanup(db, pTask);
98805 pTask->pSorter = pSorter;
98806 }
98807 if( pSorter->list.aMemory==0 ){
98808 vdbeSorterRecordFree(0, pSorter->list.pList);
98809 }
98810 pSorter->list.pList = 0;
98811 pSorter->list.szPMA = 0;
98812 pSorter->bUsePMA = 0;
98813 pSorter->iMemory = 0;
98814 pSorter->mxKeysize = 0;
98815 sqlite3DbFree(db, pSorter->pUnpacked);
98816 pSorter->pUnpacked = 0;
98817}
98818
98819/*
98820** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
98821*/
98822SQLITE_PRIVATEstatic void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
98823 VdbeSorter *pSorter;
98824 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
98825 pSorter = pCsr->uc.pSorter;
98826 if( pSorter ){
98827 sqlite3VdbeSorterReset(db, pSorter);
98828 sqlite3_free(pSorter->list.aMemory);
98829 sqlite3DbFree(db, pSorter);
98830 pCsr->uc.pSorter = 0;
98831 }
98832}
98833
98834#if SQLITE_MAX_MMAP_SIZE20971520>0
98835/*
98836** The first argument is a file-handle open on a temporary file. The file
98837** is guaranteed to be nByte bytes or smaller in size. This function
98838** attempts to extend the file to nByte bytes in size and to ensure that
98839** the VFS has memory mapped it.
98840**
98841** Whether or not the file does end up memory mapped of course depends on
98842** the specific VFS implementation.
98843*/
98844static void vdbeSorterExtendFile(sqlite3 *db, sqlite3_file *pFd, i64 nByte){
98845 if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){
98846 void *p = 0;
98847 int chunksize = 4*1024;
98848 sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_CHUNK_SIZE6, &chunksize);
98849 sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_SIZE_HINT5, &nByte);
98850 sqlite3OsFetch(pFd, 0, (int)nByte, &p);
98851 if( p ) sqlite3OsUnfetch(pFd, 0, p);
98852 }
98853}
98854#else
98855# define vdbeSorterExtendFile(x,y,z)
98856#endif
98857
98858/*
98859** Allocate space for a file-handle and open a temporary file. If successful,
98860** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK.
98861** Otherwise, set *ppFd to 0 and return an SQLite error code.
98862*/
98863static int vdbeSorterOpenTempFile(
98864 sqlite3 *db, /* Database handle doing sort */
98865 i64 nExtend, /* Attempt to extend file to this size */
98866 sqlite3_file **ppFd
98867){
98868 int rc;
98869 if( sqlite3FaultSim(202) ) return SQLITE_IOERR_ACCESS(10 | (13<<8));
98870 rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd,
98871 SQLITE_OPEN_TEMP_JOURNAL0x00001000 |
98872 SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004 |
98873 SQLITE_OPEN_EXCLUSIVE0x00000010 | SQLITE_OPEN_DELETEONCLOSE0x00000008, &rc
98874 );
98875 if( rc==SQLITE_OK0 ){
98876 i64 max = SQLITE_MAX_MMAP_SIZE20971520;
98877 sqlite3OsFileControlHint(*ppFd, SQLITE_FCNTL_MMAP_SIZE18, (void*)&max);
98878 if( nExtend>0 ){
98879 vdbeSorterExtendFile(db, *ppFd, nExtend);
98880 }
98881 }
98882 return rc;
98883}
98884
98885/*
98886** If it has not already been allocated, allocate the UnpackedRecord
98887** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or
98888** if no allocation was required), or SQLITE_NOMEM otherwise.
98889*/
98890static int vdbeSortAllocUnpacked(SortSubtask *pTask){
98891 if( pTask->pUnpacked==0 ){
98892 pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pTask->pSorter->pKeyInfo);
98893 if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT7;
98894 pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nKeyField;
98895 pTask->pUnpacked->errCode = 0;
98896 }
98897 return SQLITE_OK0;
98898}
98899
98900
98901/*
98902** Merge the two sorted lists p1 and p2 into a single list.
98903*/
98904static SorterRecord *vdbeSorterMerge(
98905 SortSubtask *pTask, /* Calling thread context */
98906 SorterRecord *p1, /* First list to merge */
98907 SorterRecord *p2 /* Second list to merge */
98908){
98909 SorterRecord *pFinal = 0;
98910 SorterRecord **pp = &pFinal;
98911 int bCached = 0;
98912
98913 assert( p1!=0 && p2!=0 )((void) (0));
98914 for(;;){
98915 int res;
98916 res = pTask->xCompare(
98917 pTask, &bCached, SRVAL(p1)((void*)((SorterRecord*)(p1) + 1)), p1->nVal, SRVAL(p2)((void*)((SorterRecord*)(p2) + 1)), p2->nVal
98918 );
98919
98920 if( res<=0 ){
98921 *pp = p1;
98922 pp = &p1->u.pNext;
98923 p1 = p1->u.pNext;
98924 if( p1==0 ){
98925 *pp = p2;
98926 break;
98927 }
98928 }else{
98929 *pp = p2;
98930 pp = &p2->u.pNext;
98931 p2 = p2->u.pNext;
98932 bCached = 0;
98933 if( p2==0 ){
98934 *pp = p1;
98935 break;
98936 }
98937 }
98938 }
98939 return pFinal;
98940}
98941
98942/*
98943** Return the SorterCompare function to compare values collected by the
98944** sorter object passed as the only argument.
98945*/
98946static SorterCompare vdbeSorterGetCompare(VdbeSorter *p){
98947 if( p->typeMask==SORTER_TYPE_INTEGER0x01 ){
98948 return vdbeSorterCompareInt;
98949 }else if( p->typeMask==SORTER_TYPE_TEXT0x02 ){
98950 return vdbeSorterCompareText;
98951 }
98952 return vdbeSorterCompare;
98953}
98954
98955/*
98956** Sort the linked list of records headed at pTask->pList. Return
98957** SQLITE_OK if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if
98958** an error occurs.
98959*/
98960static int vdbeSorterSort(SortSubtask *pTask, SorterList *pList){
98961 int i;
98962 SorterRecord *p;
98963 int rc;
98964 SorterRecord *aSlot[64];
98965
98966 rc = vdbeSortAllocUnpacked(pTask);
98967 if( rc!=SQLITE_OK0 ) return rc;
98968
98969 p = pList->pList;
98970 pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter);
98971 memset(aSlot, 0, sizeof(aSlot));
98972
98973 while( p ){
98974 SorterRecord *pNext;
98975 if( pList->aMemory ){
98976 if( (u8*)p==pList->aMemory ){
98977 pNext = 0;
98978 }else{
98979 assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) )((void) (0));
98980 pNext = (SorterRecord*)&pList->aMemory[p->u.iNext];
98981 }
98982 }else{
98983 pNext = p->u.pNext;
98984 }
98985
98986 p->u.pNext = 0;
98987 for(i=0; aSlot[i]; i++){
98988 p = vdbeSorterMerge(pTask, p, aSlot[i]);
98989 aSlot[i] = 0;
98990 }
98991 aSlot[i] = p;
98992 p = pNext;
98993 }
98994
98995 p = 0;
98996 for(i=0; i<ArraySize(aSlot)((int)(sizeof(aSlot)/sizeof(aSlot[0]))); i++){
98997 if( aSlot[i]==0 ) continue;
98998 p = p ? vdbeSorterMerge(pTask, p, aSlot[i]) : aSlot[i];
98999 }
99000 pList->pList = p;
99001
99002 assert( pTask->pUnpacked->errCode==SQLITE_OK((void) (0))
99003 || pTask->pUnpacked->errCode==SQLITE_NOMEM((void) (0))
99004 )((void) (0));
99005 return pTask->pUnpacked->errCode;
99006}
99007
99008/*
99009** Initialize a PMA-writer object.
99010*/
99011static void vdbePmaWriterInit(
99012 sqlite3_file *pFd, /* File handle to write to */
99013 PmaWriter *p, /* Object to populate */
99014 int nBuf, /* Buffer size */
99015 i64 iStart /* Offset of pFd to begin writing at */
99016){
99017 memset(p, 0, sizeof(PmaWriter));
99018 p->aBuffer = (u8*)sqlite3Malloc(nBuf);
99019 if( !p->aBuffer ){
99020 p->eFWErr = SQLITE_NOMEM_BKPT7;
99021 }else{
99022 p->iBufEnd = p->iBufStart = (iStart % nBuf);
99023 p->iWriteOff = iStart - p->iBufStart;
99024 p->nBuffer = nBuf;
99025 p->pFd = pFd;
99026 }
99027}
99028
99029/*
99030** Write nData bytes of data to the PMA. Return SQLITE_OK
99031** if successful, or an SQLite error code if an error occurs.
99032*/
99033static void vdbePmaWriteBlob(PmaWriter *p, u8 *pData, int nData){
99034 int nRem = nData;
99035 while( nRem>0 && p->eFWErr==0 ){
99036 int nCopy = nRem;
99037 if( nCopy>(p->nBuffer - p->iBufEnd) ){
99038 nCopy = p->nBuffer - p->iBufEnd;
99039 }
99040
99041 memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
99042 p->iBufEnd += nCopy;
99043 if( p->iBufEnd==p->nBuffer ){
99044 p->eFWErr = sqlite3OsWrite(p->pFd,
99045 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
99046 p->iWriteOff + p->iBufStart
99047 );
99048 p->iBufStart = p->iBufEnd = 0;
99049 p->iWriteOff += p->nBuffer;
99050 }
99051 assert( p->iBufEnd<p->nBuffer )((void) (0));
99052
99053 nRem -= nCopy;
99054 }
99055}
99056
99057/*
99058** Flush any buffered data to disk and clean up the PMA-writer object.
99059** The results of using the PMA-writer after this call are undefined.
99060** Return SQLITE_OK if flushing the buffered data succeeds or is not
99061** required. Otherwise, return an SQLite error code.
99062**
99063** Before returning, set *piEof to the offset immediately following the
99064** last byte written to the file.
99065*/
99066static int vdbePmaWriterFinish(PmaWriter *p, i64 *piEof){
99067 int rc;
99068 if( p->eFWErr==0 && ALWAYS(p->aBuffer)(p->aBuffer) && p->iBufEnd>p->iBufStart ){
99069 p->eFWErr = sqlite3OsWrite(p->pFd,
99070 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
99071 p->iWriteOff + p->iBufStart
99072 );
99073 }
99074 *piEof = (p->iWriteOff + p->iBufEnd);
99075 sqlite3_free(p->aBuffer);
99076 rc = p->eFWErr;
99077 memset(p, 0, sizeof(PmaWriter));
99078 return rc;
99079}
99080
99081/*
99082** Write value iVal encoded as a varint to the PMA. Return
99083** SQLITE_OK if successful, or an SQLite error code if an error occurs.
99084*/
99085static void vdbePmaWriteVarint(PmaWriter *p, u64 iVal){
99086 int nByte;
99087 u8 aByte[10];
99088 nByte = sqlite3PutVarint(aByte, iVal);
99089 vdbePmaWriteBlob(p, aByte, nByte);
99090}
99091
99092/*
99093** Write the current contents of in-memory linked-list pList to a level-0
99094** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if
99095** successful, or an SQLite error code otherwise.
99096**
99097** The format of a PMA is:
99098**
99099** * A varint. This varint contains the total number of bytes of content
99100** in the PMA (not including the varint itself).
99101**
99102** * One or more records packed end-to-end in order of ascending keys.
99103** Each record consists of a varint followed by a blob of data (the
99104** key). The varint is the number of bytes in the blob of data.
99105*/
99106static int vdbeSorterListToPMA(SortSubtask *pTask, SorterList *pList){
99107 sqlite3 *db = pTask->pSorter->db;
99108 int rc = SQLITE_OK0; /* Return code */
99109 PmaWriter writer; /* Object used to write to the file */
99110
99111#ifdef SQLITE_DEBUG
99112 /* Set iSz to the expected size of file pTask->file after writing the PMA.
99113 ** This is used by an assert() statement at the end of this function. */
99114 i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof;
99115#endif
99116
99117 vdbeSorterWorkDebug(pTask, "enter");
99118 memset(&writer, 0, sizeof(PmaWriter));
99119 assert( pList->szPMA>0 )((void) (0));
99120
99121 /* If the first temporary PMA file has not been opened, open it now. */
99122 if( pTask->file.pFd==0 ){
99123 rc = vdbeSorterOpenTempFile(db, 0, &pTask->file.pFd);
99124 assert( rc!=SQLITE_OK || pTask->file.pFd )((void) (0));
99125 assert( pTask->file.iEof==0 )((void) (0));
99126 assert( pTask->nPMA==0 )((void) (0));
99127 }
99128
99129 /* Try to get the file to memory map */
99130 if( rc==SQLITE_OK0 ){
99131 vdbeSorterExtendFile(db, pTask->file.pFd, pTask->file.iEof+pList->szPMA+9);
99132 }
99133
99134 /* Sort the list */
99135 if( rc==SQLITE_OK0 ){
99136 rc = vdbeSorterSort(pTask, pList);
99137 }
99138
99139 if( rc==SQLITE_OK0 ){
99140 SorterRecord *p;
99141 SorterRecord *pNext = 0;
99142
99143 vdbePmaWriterInit(pTask->file.pFd, &writer, pTask->pSorter->pgsz,
99144 pTask->file.iEof);
99145 pTask->nPMA++;
99146 vdbePmaWriteVarint(&writer, pList->szPMA);
99147 for(p=pList->pList; p; p=pNext){
99148 pNext = p->u.pNext;
99149 vdbePmaWriteVarint(&writer, p->nVal);
99150 vdbePmaWriteBlob(&writer, SRVAL(p)((void*)((SorterRecord*)(p) + 1)), p->nVal);
99151 if( pList->aMemory==0 ) sqlite3_free(p);
99152 }
99153 pList->pList = p;
99154 rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof);
99155 }
99156
99157 vdbeSorterWorkDebug(pTask, "exit");
99158 assert( rc!=SQLITE_OK || pList->pList==0 )((void) (0));
99159 assert( rc!=SQLITE_OK || pTask->file.iEof==iSz )((void) (0));
99160 return rc;
99161}
99162
99163/*
99164** Advance the MergeEngine to its next entry.
99165** Set *pbEof to true there is no next entry because
99166** the MergeEngine has reached the end of all its inputs.
99167**
99168** Return SQLITE_OK if successful or an error code if an error occurs.
99169*/
99170static int vdbeMergeEngineStep(
99171 MergeEngine *pMerger, /* The merge engine to advance to the next row */
99172 int *pbEof /* Set TRUE at EOF. Set false for more content */
99173){
99174 int rc;
99175 int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
99176 SortSubtask *pTask = pMerger->pTask;
99177
99178 /* Advance the current PmaReader */
99179 rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);
99180
99181 /* Update contents of aTree[] */
99182 if( rc==SQLITE_OK0 ){
99183 int i; /* Index of aTree[] to recalculate */
99184 PmaReader *pReadr1; /* First PmaReader to compare */
99185 PmaReader *pReadr2; /* Second PmaReader to compare */
99186 int bCached = 0;
99187
99188 /* Find the first two PmaReaders to compare. The one that was just
99189 ** advanced (iPrev) and the one next to it in the array. */
99190 pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
99191 pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
99192
99193 for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
99194 /* Compare pReadr1 and pReadr2. Store the result in variable iRes. */
99195 int iRes;
99196 if( pReadr1->pFd==0 ){
99197 iRes = +1;
99198 }else if( pReadr2->pFd==0 ){
99199 iRes = -1;
99200 }else{
99201 iRes = pTask->xCompare(pTask, &bCached,
99202 pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
99203 );
99204 }
99205
99206 /* If pReadr1 contained the smaller value, set aTree[i] to its index.
99207 ** Then set pReadr2 to the next PmaReader to compare to pReadr1. In this
99208 ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
99209 ** pKey2 to point to the record belonging to pReadr2.
99210 **
99211 ** Alternatively, if pReadr2 contains the smaller of the two values,
99212 ** set aTree[i] to its index and update pReadr1. If vdbeSorterCompare()
99213 ** was actually called above, then pTask->pUnpacked now contains
99214 ** a value equivalent to pReadr2. So set pKey2 to NULL to prevent
99215 ** vdbeSorterCompare() from decoding pReadr2 again.
99216 **
99217 ** If the two values were equal, then the value from the oldest
99218 ** PMA should be considered smaller. The VdbeSorter.aReadr[] array
99219 ** is sorted from oldest to newest, so pReadr1 contains older values
99220 ** than pReadr2 iff (pReadr1<pReadr2). */
99221 if( iRes<0 || (iRes==0 && pReadr1<pReadr2) ){
99222 pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
99223 pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
99224 bCached = 0;
99225 }else{
99226 if( pReadr1->pFd ) bCached = 0;
99227 pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
99228 pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
99229 }
99230 }
99231 *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
99232 }
99233
99234 return (rc==SQLITE_OK0 ? pTask->pUnpacked->errCode : rc);
99235}
99236
99237#if SQLITE_MAX_WORKER_THREADS8>0
99238/*
99239** The main routine for background threads that write level-0 PMAs.
99240*/
99241static void *vdbeSorterFlushThread(void *pCtx){
99242 SortSubtask *pTask = (SortSubtask*)pCtx;
99243 int rc; /* Return code */
99244 assert( pTask->bDone==0 )((void) (0));
99245 rc = vdbeSorterListToPMA(pTask, &pTask->list);
99246 pTask->bDone = 1;
99247 return SQLITE_INT_TO_PTR(rc)((void*)(long int)(rc));
99248}
99249#endif /* SQLITE_MAX_WORKER_THREADS>0 */
99250
99251/*
99252** Flush the current contents of VdbeSorter.list to a new PMA, possibly
99253** using a background thread.
99254*/
99255static int vdbeSorterFlushPMA(VdbeSorter *pSorter){
99256#if SQLITE_MAX_WORKER_THREADS8==0
99257 pSorter->bUsePMA = 1;
99258 return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list);
99259#else
99260 int rc = SQLITE_OK0;
99261 int i;
99262 SortSubtask *pTask = 0; /* Thread context used to create new PMA */
99263 int nWorker = (pSorter->nTask-1);
99264
99265 /* Set the flag to indicate that at least one PMA has been written.
99266 ** Or will be, anyhow. */
99267 pSorter->bUsePMA = 1;
99268
99269 /* Select a sub-task to sort and flush the current list of in-memory
99270 ** records to disk. If the sorter is running in multi-threaded mode,
99271 ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
99272 ** the background thread from a sub-tasks previous turn is still running,
99273 ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
99274 ** fall back to using the final sub-task. The first (pSorter->nTask-1)
99275 ** sub-tasks are prefered as they use background threads - the final
99276 ** sub-task uses the main thread. */
99277 for(i=0; i<nWorker; i++){
99278 int iTest = (pSorter->iPrev + i + 1) % nWorker;
99279 pTask = &pSorter->aTask[iTest];
99280 if( pTask->bDone ){
99281 rc = vdbeSorterJoinThread(pTask);
99282 }
99283 if( rc!=SQLITE_OK0 || pTask->pThread==0 ) break;
99284 }
99285
99286 if( rc==SQLITE_OK0 ){
99287 if( i==nWorker ){
99288 /* Use the foreground thread for this operation */
99289 rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list);
99290 }else{
99291 /* Launch a background thread for this operation */
99292 u8 *aMem;
99293 void *pCtx;
99294
99295 assert( pTask!=0 )((void) (0));
99296 assert( pTask->pThread==0 && pTask->bDone==0 )((void) (0));
99297 assert( pTask->list.pList==0 )((void) (0));
99298 assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 )((void) (0));
99299
99300 aMem = pTask->list.aMemory;
99301 pCtx = (void*)pTask;
99302 pSorter->iPrev = (u8)(pTask - pSorter->aTask);
99303 pTask->list = pSorter->list;
99304 pSorter->list.pList = 0;
99305 pSorter->list.szPMA = 0;
99306 if( aMem ){
99307 pSorter->list.aMemory = aMem;
99308 pSorter->nMemory = sqlite3MallocSize(aMem);
99309 }else if( pSorter->list.aMemory ){
99310 pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory);
99311 if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT7;
99312 }
99313
99314 rc = vdbeSorterCreateThread(pTask, vdbeSorterFlushThread, pCtx);
99315 }
99316 }
99317
99318 return rc;
99319#endif /* SQLITE_MAX_WORKER_THREADS!=0 */
99320}
99321
99322/*
99323** Add a record to the sorter.
99324*/
99325SQLITE_PRIVATEstatic int sqlite3VdbeSorterWrite(
99326 const VdbeCursor *pCsr, /* Sorter cursor */
99327 Mem *pVal /* Memory cell containing record */
99328){
99329 VdbeSorter *pSorter;
99330 int rc = SQLITE_OK0; /* Return Code */
99331 SorterRecord *pNew; /* New list element */
99332 int bFlush; /* True to flush contents of memory to PMA */
99333 int nReq; /* Bytes of memory required */
99334 int nPMA; /* Bytes of PMA space required */
99335 int t; /* serial type of first record field */
99336
99337 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
99338 pSorter = pCsr->uc.pSorter;
99339 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))
;
99340 if( t>0 && t<10 && t!=7 ){
99341 pSorter->typeMask &= SORTER_TYPE_INTEGER0x01;
99342 }else if( t>10 && (t & 0x01) ){
99343 pSorter->typeMask &= SORTER_TYPE_TEXT0x02;
99344 }else{
99345 pSorter->typeMask = 0;
99346 }
99347
99348 assert( pSorter )((void) (0));
99349
99350 /* Figure out whether or not the current contents of memory should be
99351 ** flushed to a PMA before continuing. If so, do so.
99352 **
99353 ** If using the single large allocation mode (pSorter->aMemory!=0), then
99354 ** flush the contents of memory to a new PMA if (a) at least one value is
99355 ** already in memory and (b) the new value will not fit in memory.
99356 **
99357 ** Or, if using separate allocations for each record, flush the contents
99358 ** of memory to a PMA if either of the following are true:
99359 **
99360 ** * The total memory allocated for the in-memory list is greater
99361 ** than (page-size * cache-size), or
99362 **
99363 ** * The total memory allocated for the in-memory list is greater
99364 ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
99365 */
99366 nReq = pVal->n + sizeof(SorterRecord);
99367 nPMA = pVal->n + sqlite3VarintLen(pVal->n);
99368 if( pSorter->mxPmaSize ){
99369 if( pSorter->list.aMemory ){
99370 bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize;
99371 }else{
99372 bFlush = (
99373 (pSorter->list.szPMA > pSorter->mxPmaSize)
99374 || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull())
99375 );
99376 }
99377 if( bFlush ){
99378 rc = vdbeSorterFlushPMA(pSorter);
99379 pSorter->list.szPMA = 0;
99380 pSorter->iMemory = 0;
99381 assert( rc!=SQLITE_OK || pSorter->list.pList==0 )((void) (0));
99382 }
99383 }
99384
99385 pSorter->list.szPMA += nPMA;
99386 if( nPMA>pSorter->mxKeysize ){
99387 pSorter->mxKeysize = nPMA;
99388 }
99389
99390 if( pSorter->list.aMemory ){
99391 int nMin = pSorter->iMemory + nReq;
99392
99393 if( nMin>pSorter->nMemory ){
99394 u8 *aNew;
99395 sqlite3_int64 nNew = 2 * (sqlite3_int64)pSorter->nMemory;
99396 int iListOff = -1;
99397 if( pSorter->list.pList ){
99398 iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
99399 }
99400 while( nNew < nMin ) nNew = nNew*2;
99401 if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
99402 if( nNew < nMin ) nNew = nMin;
99403 aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
99404 if( !aNew ) return SQLITE_NOMEM_BKPT7;
99405 if( iListOff>=0 ){
99406 pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
99407 }
99408 pSorter->list.aMemory = aNew;
99409 pSorter->nMemory = nNew;
99410 }
99411
99412 pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
99413 pSorter->iMemory += ROUND8(nReq)(((nReq)+7)&~7);
99414 if( pSorter->list.pList ){
99415 pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
99416 }
99417 }else{
99418 pNew = (SorterRecord *)sqlite3Malloc(nReq);
99419 if( pNew==0 ){
99420 return SQLITE_NOMEM_BKPT7;
99421 }
99422 pNew->u.pNext = pSorter->list.pList;
99423 }
99424
99425 memcpy(SRVAL(pNew)((void*)((SorterRecord*)(pNew) + 1)), pVal->z, pVal->n);
99426 pNew->nVal = pVal->n;
99427 pSorter->list.pList = pNew;
99428
99429 return rc;
99430}
99431
99432/*
99433** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format
99434** of the data stored in aFile[1] is the same as that used by regular PMAs,
99435** except that the number-of-bytes varint is omitted from the start.
99436*/
99437static int vdbeIncrPopulate(IncrMerger *pIncr){
99438 int rc = SQLITE_OK0;
99439 int rc2;
99440 i64 iStart = pIncr->iStartOff;
99441 SorterFile *pOut = &pIncr->aFile[1];
99442 SortSubtask *pTask = pIncr->pTask;
99443 MergeEngine *pMerger = pIncr->pMerger;
99444 PmaWriter writer;
99445 assert( pIncr->bEof==0 )((void) (0));
99446
99447 vdbeSorterPopulateDebug(pTask, "enter");
99448
99449 vdbePmaWriterInit(pOut->pFd, &writer, pTask->pSorter->pgsz, iStart);
99450 while( rc==SQLITE_OK0 ){
99451 int dummy;
99452 PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ];
99453 int nKey = pReader->nKey;
99454 i64 iEof = writer.iWriteOff + writer.iBufEnd;
99455
99456 /* Check if the output file is full or if the input has been exhausted.
99457 ** In either case exit the loop. */
99458 if( pReader->pFd==0 ) break;
99459 if( (iEof + nKey + sqlite3VarintLen(nKey))>(iStart + pIncr->mxSz) ) break;
99460
99461 /* Write the next key to the output. */
99462 vdbePmaWriteVarint(&writer, nKey);
99463 vdbePmaWriteBlob(&writer, pReader->aKey, nKey);
99464 assert( pIncr->pMerger->pTask==pTask )((void) (0));
99465 rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy);
99466 }
99467
99468 rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof);
99469 if( rc==SQLITE_OK0 ) rc = rc2;
99470 vdbeSorterPopulateDebug(pTask, "exit");
99471 return rc;
99472}
99473
99474#if SQLITE_MAX_WORKER_THREADS8>0
99475/*
99476** The main routine for background threads that populate aFile[1] of
99477** multi-threaded IncrMerger objects.
99478*/
99479static void *vdbeIncrPopulateThread(void *pCtx){
99480 IncrMerger *pIncr = (IncrMerger*)pCtx;
99481 void *pRet = SQLITE_INT_TO_PTR( vdbeIncrPopulate(pIncr) )((void*)(long int)(vdbeIncrPopulate(pIncr)));
99482 pIncr->pTask->bDone = 1;
99483 return pRet;
99484}
99485
99486/*
99487** Launch a background thread to populate aFile[1] of pIncr.
99488*/
99489static int vdbeIncrBgPopulate(IncrMerger *pIncr){
99490 void *p = (void*)pIncr;
99491 assert( pIncr->bUseThread )((void) (0));
99492 return vdbeSorterCreateThread(pIncr->pTask, vdbeIncrPopulateThread, p);
99493}
99494#endif
99495
99496/*
99497** This function is called when the PmaReader corresponding to pIncr has
99498** finished reading the contents of aFile[0]. Its purpose is to "refill"
99499** aFile[0] such that the PmaReader should start rereading it from the
99500** beginning.
99501**
99502** For single-threaded objects, this is accomplished by literally reading
99503** keys from pIncr->pMerger and repopulating aFile[0].
99504**
99505** For multi-threaded objects, all that is required is to wait until the
99506** background thread is finished (if it is not already) and then swap
99507** aFile[0] and aFile[1] in place. If the contents of pMerger have not
99508** been exhausted, this function also launches a new background thread
99509** to populate the new aFile[1].
99510**
99511** SQLITE_OK is returned on success, or an SQLite error code otherwise.
99512*/
99513static int vdbeIncrSwap(IncrMerger *pIncr){
99514 int rc = SQLITE_OK0;
99515
99516#if SQLITE_MAX_WORKER_THREADS8>0
99517 if( pIncr->bUseThread ){
99518 rc = vdbeSorterJoinThread(pIncr->pTask);
99519
99520 if( rc==SQLITE_OK0 ){
99521 SorterFile f0 = pIncr->aFile[0];
99522 pIncr->aFile[0] = pIncr->aFile[1];
99523 pIncr->aFile[1] = f0;
99524 }
99525
99526 if( rc==SQLITE_OK0 ){
99527 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
99528 pIncr->bEof = 1;
99529 }else{
99530 rc = vdbeIncrBgPopulate(pIncr);
99531 }
99532 }
99533 }else
99534#endif
99535 {
99536 rc = vdbeIncrPopulate(pIncr);
99537 pIncr->aFile[0] = pIncr->aFile[1];
99538 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
99539 pIncr->bEof = 1;
99540 }
99541 }
99542
99543 return rc;
99544}
99545
99546/*
99547** Allocate and return a new IncrMerger object to read data from pMerger.
99548**
99549** If an OOM condition is encountered, return NULL. In this case free the
99550** pMerger argument before returning.
99551*/
99552static int vdbeIncrMergerNew(
99553 SortSubtask *pTask, /* The thread that will be using the new IncrMerger */
99554 MergeEngine *pMerger, /* The MergeEngine that the IncrMerger will control */
99555 IncrMerger **ppOut /* Write the new IncrMerger here */
99556){
99557 int rc = SQLITE_OK0;
99558 IncrMerger *pIncr = *ppOut = (IncrMerger*)
99559 (sqlite3FaultSim(100) ? 0 : sqlite3MallocZero(sizeof(*pIncr)));
99560 if( pIncr ){
99561 pIncr->pMerger = pMerger;
99562 pIncr->pTask = pTask;
99563 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))
;
99564 pTask->file2.iEof += pIncr->mxSz;
99565 }else{
99566 vdbeMergeEngineFree(pMerger);
99567 rc = SQLITE_NOMEM_BKPT7;
99568 }
99569 assert( *ppOut!=0 || rc!=SQLITE_OK )((void) (0));
99570 return rc;
99571}
99572
99573#if SQLITE_MAX_WORKER_THREADS8>0
99574/*
99575** Set the "use-threads" flag on object pIncr.
99576*/
99577static void vdbeIncrMergerSetThreads(IncrMerger *pIncr){
99578 pIncr->bUseThread = 1;
99579 pIncr->pTask->file2.iEof -= pIncr->mxSz;
99580}
99581#endif /* SQLITE_MAX_WORKER_THREADS>0 */
99582
99583
99584
99585/*
99586** Recompute pMerger->aTree[iOut] by comparing the next keys on the
99587** two PmaReaders that feed that entry. Neither of the PmaReaders
99588** are advanced. This routine merely does the comparison.
99589*/
99590static void vdbeMergeEngineCompare(
99591 MergeEngine *pMerger, /* Merge engine containing PmaReaders to compare */
99592 int iOut /* Store the result in pMerger->aTree[iOut] */
99593){
99594 int i1;
99595 int i2;
99596 int iRes;
99597 PmaReader *p1;
99598 PmaReader *p2;
99599
99600 assert( iOut<pMerger->nTree && iOut>0 )((void) (0));
99601
99602 if( iOut>=(pMerger->nTree/2) ){
99603 i1 = (iOut - pMerger->nTree/2) * 2;
99604 i2 = i1 + 1;
99605 }else{
99606 i1 = pMerger->aTree[iOut*2];
99607 i2 = pMerger->aTree[iOut*2+1];
99608 }
99609
99610 p1 = &pMerger->aReadr[i1];
99611 p2 = &pMerger->aReadr[i2];
99612
99613 if( p1->pFd==0 ){
99614 iRes = i2;
99615 }else if( p2->pFd==0 ){
99616 iRes = i1;
99617 }else{
99618 SortSubtask *pTask = pMerger->pTask;
99619 int bCached = 0;
99620 int res;
99621 assert( pTask->pUnpacked!=0 )((void) (0)); /* from vdbeSortSubtaskMain() */
99622 res = pTask->xCompare(
99623 pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
99624 );
99625 if( res<=0 ){
99626 iRes = i1;
99627 }else{
99628 iRes = i2;
99629 }
99630 }
99631
99632 pMerger->aTree[iOut] = iRes;
99633}
99634
99635/*
99636** Allowed values for the eMode parameter to vdbeMergeEngineInit()
99637** and vdbePmaReaderIncrMergeInit().
99638**
99639** Only INCRINIT_NORMAL is valid in single-threaded builds (when
99640** SQLITE_MAX_WORKER_THREADS==0). The other values are only used
99641** when there exists one or more separate worker threads.
99642*/
99643#define INCRINIT_NORMAL0 0
99644#define INCRINIT_TASK1 1
99645#define INCRINIT_ROOT2 2
99646
99647/*
99648** Forward reference required as the vdbeIncrMergeInit() and
99649** vdbePmaReaderIncrInit() routines are called mutually recursively when
99650** building a merge tree.
99651*/
99652static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode);
99653
99654/*
99655** Initialize the MergeEngine object passed as the second argument. Once this
99656** function returns, the first key of merged data may be read from the
99657** MergeEngine object in the usual fashion.
99658**
99659** If argument eMode is INCRINIT_ROOT, then it is assumed that any IncrMerge
99660** objects attached to the PmaReader objects that the merger reads from have
99661** already been populated, but that they have not yet populated aFile[0] and
99662** set the PmaReader objects up to read from it. In this case all that is
99663** required is to call vdbePmaReaderNext() on each PmaReader to point it at
99664** its first key.
99665**
99666** Otherwise, if eMode is any value other than INCRINIT_ROOT, then use
99667** vdbePmaReaderIncrMergeInit() to initialize each PmaReader that feeds data
99668** to pMerger.
99669**
99670** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
99671*/
99672static int vdbeMergeEngineInit(
99673 SortSubtask *pTask, /* Thread that will run pMerger */
99674 MergeEngine *pMerger, /* MergeEngine to initialize */
99675 int eMode /* One of the INCRINIT_XXX constants */
99676){
99677 int rc = SQLITE_OK0; /* Return code */
99678 int i; /* For looping over PmaReader objects */
99679 int nTree; /* Number of subtrees to merge */
99680
99681 /* Failure to allocate the merge would have been detected prior to
99682 ** invoking this routine */
99683 assert( pMerger!=0 )((void) (0));
99684
99685 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
99686 assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL )((void) (0));
99687
99688 /* Verify that the MergeEngine is assigned to a single thread */
99689 assert( pMerger->pTask==0 )((void) (0));
99690 pMerger->pTask = pTask;
99691
99692 nTree = pMerger->nTree;
99693 for(i=0; i<nTree; i++){
99694 if( SQLITE_MAX_WORKER_THREADS8>0 && eMode==INCRINIT_ROOT2 ){
99695 /* PmaReaders should be normally initialized in order, as if they are
99696 ** reading from the same temp file this makes for more linear file IO.
99697 ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
99698 ** in use it will block the vdbePmaReaderNext() call while it uses
99699 ** the main thread to fill its buffer. So calling PmaReaderNext()
99700 ** on this PmaReader before any of the multi-threaded PmaReaders takes
99701 ** better advantage of multi-processor hardware. */
99702 rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]);
99703 }else{
99704 rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL0);
99705 }
99706 if( rc!=SQLITE_OK0 ) return rc;
99707 }
99708
99709 for(i=pMerger->nTree-1; i>0; i--){
99710 vdbeMergeEngineCompare(pMerger, i);
99711 }
99712 return pTask->pUnpacked->errCode;
99713}
99714
99715/*
99716** The PmaReader passed as the first argument is guaranteed to be an
99717** incremental-reader (pReadr->pIncr!=0). This function serves to open
99718** and/or initialize the temp file related fields of the IncrMerge
99719** object at (pReadr->pIncr).
99720**
99721** If argument eMode is set to INCRINIT_NORMAL, then all PmaReaders
99722** in the sub-tree headed by pReadr are also initialized. Data is then
99723** loaded into the buffers belonging to pReadr and it is set to point to
99724** the first key in its range.
99725**
99726** If argument eMode is set to INCRINIT_TASK, then pReadr is guaranteed
99727** to be a multi-threaded PmaReader and this function is being called in a
99728** background thread. In this case all PmaReaders in the sub-tree are
99729** initialized as for INCRINIT_NORMAL and the aFile[1] buffer belonging to
99730** pReadr is populated. However, pReadr itself is not set up to point
99731** to its first key. A call to vdbePmaReaderNext() is still required to do
99732** that.
99733**
99734** The reason this function does not call vdbePmaReaderNext() immediately
99735** in the INCRINIT_TASK case is that vdbePmaReaderNext() assumes that it has
99736** to block on thread (pTask->thread) before accessing aFile[1]. But, since
99737** this entire function is being run by thread (pTask->thread), that will
99738** lead to the current background thread attempting to join itself.
99739**
99740** Finally, if argument eMode is set to INCRINIT_ROOT, it may be assumed
99741** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all
99742** child-trees have already been initialized using IncrInit(INCRINIT_TASK).
99743** In this case vdbePmaReaderNext() is called on all child PmaReaders and
99744** the current PmaReader set to point to the first key in its range.
99745**
99746** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
99747*/
99748static int vdbePmaReaderIncrMergeInit(PmaReader *pReadr, int eMode){
99749 int rc = SQLITE_OK0;
99750 IncrMerger *pIncr = pReadr->pIncr;
99751 SortSubtask *pTask = pIncr->pTask;
99752 sqlite3 *db = pTask->pSorter->db;
99753
99754 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
99755 assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL )((void) (0));
99756
99757 rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
99758
99759 /* Set up the required files for pIncr. A multi-theaded IncrMerge object
99760 ** requires two temp files to itself, whereas a single-threaded object
99761 ** only requires a region of pTask->file2. */
99762 if( rc==SQLITE_OK0 ){
99763 int mxSz = pIncr->mxSz;
99764#if SQLITE_MAX_WORKER_THREADS8>0
99765 if( pIncr->bUseThread ){
99766 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
99767 if( rc==SQLITE_OK0 ){
99768 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
99769 }
99770 }else
99771#endif
99772 /*if( !pIncr->bUseThread )*/{
99773 if( pTask->file2.pFd==0 ){
99774 assert( pTask->file2.iEof>0 )((void) (0));
99775 rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
99776 pTask->file2.iEof = 0;
99777 }
99778 if( rc==SQLITE_OK0 ){
99779 pIncr->aFile[1].pFd = pTask->file2.pFd;
99780 pIncr->iStartOff = pTask->file2.iEof;
99781 pTask->file2.iEof += mxSz;
99782 }
99783 }
99784 }
99785
99786#if SQLITE_MAX_WORKER_THREADS8>0
99787 if( rc==SQLITE_OK0 && pIncr->bUseThread ){
99788 /* Use the current thread to populate aFile[1], even though this
99789 ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
99790 ** then this function is already running in background thread
99791 ** pIncr->pTask->thread.
99792 **
99793 ** If this is the INCRINIT_ROOT object, then it is running in the
99794 ** main VDBE thread. But that is Ok, as that thread cannot return
99795 ** control to the VDBE or proceed with anything useful until the
99796 ** first results are ready from this merger object anyway.
99797 */
99798 assert( eMode==INCRINIT_ROOT || eMode==INCRINIT_TASK )((void) (0));
99799 rc = vdbeIncrPopulate(pIncr);
99800 }
99801#endif
99802
99803 if( rc==SQLITE_OK0 && (SQLITE_MAX_WORKER_THREADS8==0 || eMode!=INCRINIT_TASK1) ){
99804 rc = vdbePmaReaderNext(pReadr);
99805 }
99806
99807 return rc;
99808}
99809
99810#if SQLITE_MAX_WORKER_THREADS8>0
99811/*
99812** The main routine for vdbePmaReaderIncrMergeInit() operations run in
99813** background threads.
99814*/
99815static void *vdbePmaReaderBgIncrInit(void *pCtx){
99816 PmaReader *pReader = (PmaReader*)pCtx;
99817 void *pRet = SQLITE_INT_TO_PTR(((void*)(long int)(vdbePmaReaderIncrMergeInit(pReader,1)))
99818 vdbePmaReaderIncrMergeInit(pReader,INCRINIT_TASK)((void*)(long int)(vdbePmaReaderIncrMergeInit(pReader,1)))
99819 )((void*)(long int)(vdbePmaReaderIncrMergeInit(pReader,1)));
99820 pReader->pIncr->pTask->bDone = 1;
99821 return pRet;
99822}
99823#endif
99824
99825/*
99826** If the PmaReader passed as the first argument is not an incremental-reader
99827** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
99828** the vdbePmaReaderIncrMergeInit() function with the parameters passed to
99829** this routine to initialize the incremental merge.
99830**
99831** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1),
99832** then a background thread is launched to call vdbePmaReaderIncrMergeInit().
99833** Or, if the IncrMerger is single threaded, the same function is called
99834** using the current thread.
99835*/
99836static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode){
99837 IncrMerger *pIncr = pReadr->pIncr; /* Incremental merger */
99838 int rc = SQLITE_OK0; /* Return code */
99839 if( pIncr ){
99840#if SQLITE_MAX_WORKER_THREADS8>0
99841 assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK )((void) (0));
99842 if( pIncr->bUseThread ){
99843 void *pCtx = (void*)pReadr;
99844 rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx);
99845 }else
99846#endif
99847 {
99848 rc = vdbePmaReaderIncrMergeInit(pReadr, eMode);
99849 }
99850 }
99851 return rc;
99852}
99853
99854/*
99855** Allocate a new MergeEngine object to merge the contents of nPMA level-0
99856** PMAs from pTask->file. If no error occurs, set *ppOut to point to
99857** the new object and return SQLITE_OK. Or, if an error does occur, set *ppOut
99858** to NULL and return an SQLite error code.
99859**
99860** When this function is called, *piOffset is set to the offset of the
99861** first PMA to read from pTask->file. Assuming no error occurs, it is
99862** set to the offset immediately following the last byte of the last
99863** PMA before returning. If an error does occur, then the final value of
99864** *piOffset is undefined.
99865*/
99866static int vdbeMergeEngineLevel0(
99867 SortSubtask *pTask, /* Sorter task to read from */
99868 int nPMA, /* Number of PMAs to read */
99869 i64 *piOffset, /* IN/OUT: Readr offset in pTask->file */
99870 MergeEngine **ppOut /* OUT: New merge-engine */
99871){
99872 MergeEngine *pNew; /* Merge engine to return */
99873 i64 iOff = *piOffset;
99874 int i;
99875 int rc = SQLITE_OK0;
99876
99877 *ppOut = pNew = vdbeMergeEngineNew(nPMA);
99878 if( pNew==0 ) rc = SQLITE_NOMEM_BKPT7;
99879
99880 for(i=0; i<nPMA && rc==SQLITE_OK0; i++){
99881 i64 nDummy = 0;
99882 PmaReader *pReadr = &pNew->aReadr[i];
99883 rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy);
99884 iOff = pReadr->iEof;
99885 }
99886
99887 if( rc!=SQLITE_OK0 ){
99888 vdbeMergeEngineFree(pNew);
99889 *ppOut = 0;
99890 }
99891 *piOffset = iOff;
99892 return rc;
99893}
99894
99895/*
99896** Return the depth of a tree comprising nPMA PMAs, assuming a fanout of
99897** SORTER_MAX_MERGE_COUNT. The returned value does not include leaf nodes.
99898**
99899** i.e.
99900**
99901** nPMA<=16 -> TreeDepth() == 0
99902** nPMA<=256 -> TreeDepth() == 1
99903** nPMA<=65536 -> TreeDepth() == 2
99904*/
99905static int vdbeSorterTreeDepth(int nPMA){
99906 int nDepth = 0;
99907 i64 nDiv = SORTER_MAX_MERGE_COUNT16;
99908 while( nDiv < (i64)nPMA ){
99909 nDiv = nDiv * SORTER_MAX_MERGE_COUNT16;
99910 nDepth++;
99911 }
99912 return nDepth;
99913}
99914
99915/*
99916** pRoot is the root of an incremental merge-tree with depth nDepth (according
99917** to vdbeSorterTreeDepth()). pLeaf is the iSeq'th leaf to be added to the
99918** tree, counting from zero. This function adds pLeaf to the tree.
99919**
99920** If successful, SQLITE_OK is returned. If an error occurs, an SQLite error
99921** code is returned and pLeaf is freed.
99922*/
99923static int vdbeSorterAddToTree(
99924 SortSubtask *pTask, /* Task context */
99925 int nDepth, /* Depth of tree according to TreeDepth() */
99926 int iSeq, /* Sequence number of leaf within tree */
99927 MergeEngine *pRoot, /* Root of tree */
99928 MergeEngine *pLeaf /* Leaf to add to tree */
99929){
99930 int rc = SQLITE_OK0;
99931 int nDiv = 1;
99932 int i;
99933 MergeEngine *p = pRoot;
99934 IncrMerger *pIncr;
99935
99936 rc = vdbeIncrMergerNew(pTask, pLeaf, &pIncr);
99937
99938 for(i=1; i<nDepth; i++){
99939 nDiv = nDiv * SORTER_MAX_MERGE_COUNT16;
99940 }
99941
99942 for(i=1; i<nDepth && rc==SQLITE_OK0; i++){
99943 int iIter = (iSeq / nDiv) % SORTER_MAX_MERGE_COUNT16;
99944 PmaReader *pReadr = &p->aReadr[iIter];
99945
99946 if( pReadr->pIncr==0 ){
99947 MergeEngine *pNew = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT16);
99948 if( pNew==0 ){
99949 rc = SQLITE_NOMEM_BKPT7;
99950 }else{
99951 rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr);
99952 }
99953 }
99954 if( rc==SQLITE_OK0 ){
99955 p = pReadr->pIncr->pMerger;
99956 nDiv = nDiv / SORTER_MAX_MERGE_COUNT16;
99957 }
99958 }
99959
99960 if( rc==SQLITE_OK0 ){
99961 p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT16].pIncr = pIncr;
99962 }else{
99963 vdbeIncrFree(pIncr);
99964 }
99965 return rc;
99966}
99967
99968/*
99969** This function is called as part of a SorterRewind() operation on a sorter
99970** that has already written two or more level-0 PMAs to one or more temp
99971** files. It builds a tree of MergeEngine/IncrMerger/PmaReader objects that
99972** can be used to incrementally merge all PMAs on disk.
99973**
99974** If successful, SQLITE_OK is returned and *ppOut set to point to the
99975** MergeEngine object at the root of the tree before returning. Or, if an
99976** error occurs, an SQLite error code is returned and the final value
99977** of *ppOut is undefined.
99978*/
99979static int vdbeSorterMergeTreeBuild(
99980 VdbeSorter *pSorter, /* The VDBE cursor that implements the sort */
99981 MergeEngine **ppOut /* Write the MergeEngine here */
99982){
99983 MergeEngine *pMain = 0;
99984 int rc = SQLITE_OK0;
99985 int iTask;
99986
99987#if SQLITE_MAX_WORKER_THREADS8>0
99988 /* If the sorter uses more than one task, then create the top-level
99989 ** MergeEngine here. This MergeEngine will read data from exactly
99990 ** one PmaReader per sub-task. */
99991 assert( pSorter->bUseThreads || pSorter->nTask==1 )((void) (0));
99992 if( pSorter->nTask>1 ){
99993 pMain = vdbeMergeEngineNew(pSorter->nTask);
99994 if( pMain==0 ) rc = SQLITE_NOMEM_BKPT7;
99995 }
99996#endif
99997
99998 for(iTask=0; rc==SQLITE_OK0 && iTask<pSorter->nTask; iTask++){
99999 SortSubtask *pTask = &pSorter->aTask[iTask];
100000 assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 )((void) (0));
100001 if( SQLITE_MAX_WORKER_THREADS8==0 || pTask->nPMA ){
100002 MergeEngine *pRoot = 0; /* Root node of tree for this task */
100003 int nDepth = vdbeSorterTreeDepth(pTask->nPMA);
100004 i64 iReadOff = 0;
100005
100006 if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT16 ){
100007 rc = vdbeMergeEngineLevel0(pTask, pTask->nPMA, &iReadOff, &pRoot);
100008 }else{
100009 int i;
100010 int iSeq = 0;
100011 pRoot = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT16);
100012 if( pRoot==0 ) rc = SQLITE_NOMEM_BKPT7;
100013 for(i=0; i<pTask->nPMA && rc==SQLITE_OK0; i += SORTER_MAX_MERGE_COUNT16){
100014 MergeEngine *pMerger = 0; /* New level-0 PMA merger */
100015 int nReader; /* Number of level-0 PMAs to merge */
100016
100017 nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT)((pTask->nPMA - i)<(16)?(pTask->nPMA - i):(16));
100018 rc = vdbeMergeEngineLevel0(pTask, nReader, &iReadOff, &pMerger);
100019 if( rc==SQLITE_OK0 ){
100020 rc = vdbeSorterAddToTree(pTask, nDepth, iSeq++, pRoot, pMerger);
100021 }
100022 }
100023 }
100024
100025 if( rc==SQLITE_OK0 ){
100026#if SQLITE_MAX_WORKER_THREADS8>0
100027 if( pMain!=0 ){
100028 rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr);
100029 }else
100030#endif
100031 {
100032 assert( pMain==0 )((void) (0));
100033 pMain = pRoot;
100034 }
100035 }else{
100036 vdbeMergeEngineFree(pRoot);
100037 }
100038 }
100039 }
100040
100041 if( rc!=SQLITE_OK0 ){
100042 vdbeMergeEngineFree(pMain);
100043 pMain = 0;
100044 }
100045 *ppOut = pMain;
100046 return rc;
100047}
100048
100049/*
100050** This function is called as part of an sqlite3VdbeSorterRewind() operation
100051** on a sorter that has written two or more PMAs to temporary files. It sets
100052** up either VdbeSorter.pMerger (for single threaded sorters) or pReader
100053** (for multi-threaded sorters) so that it can be used to iterate through
100054** all records stored in the sorter.
100055**
100056** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
100057*/
100058static int vdbeSorterSetupMerge(VdbeSorter *pSorter){
100059 int rc; /* Return code */
100060 SortSubtask *pTask0 = &pSorter->aTask[0];
100061 MergeEngine *pMain = 0;
100062#if SQLITE_MAX_WORKER_THREADS8
100063 sqlite3 *db = pTask0->pSorter->db;
100064 int i;
100065 SorterCompare xCompare = vdbeSorterGetCompare(pSorter);
100066 for(i=0; i<pSorter->nTask; i++){
100067 pSorter->aTask[i].xCompare = xCompare;
100068 }
100069#endif
100070
100071 rc = vdbeSorterMergeTreeBuild(pSorter, &pMain);
100072 if( rc==SQLITE_OK0 ){
100073#if SQLITE_MAX_WORKER_THREADS8
100074 assert( pSorter->bUseThreads==0 || pSorter->nTask>1 )((void) (0));
100075 if( pSorter->bUseThreads ){
100076 int iTask;
100077 PmaReader *pReadr = 0;
100078 SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1];
100079 rc = vdbeSortAllocUnpacked(pLast);
100080 if( rc==SQLITE_OK0 ){
100081 pReadr = (PmaReader*)sqlite3DbMallocZero(db, sizeof(PmaReader));
100082 pSorter->pReader = pReadr;
100083 if( pReadr==0 ) rc = SQLITE_NOMEM_BKPT7;
100084 }
100085 if( rc==SQLITE_OK0 ){
100086 rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr);
100087 if( rc==SQLITE_OK0 ){
100088 vdbeIncrMergerSetThreads(pReadr->pIncr);
100089 for(iTask=0; iTask<(pSorter->nTask-1); iTask++){
100090 IncrMerger *pIncr;
100091 if( (pIncr = pMain->aReadr[iTask].pIncr) ){
100092 vdbeIncrMergerSetThreads(pIncr);
100093 assert( pIncr->pTask!=pLast )((void) (0));
100094 }
100095 }
100096 for(iTask=0; rc==SQLITE_OK0 && iTask<pSorter->nTask; iTask++){
100097 /* Check that:
100098 **
100099 ** a) The incremental merge object is configured to use the
100100 ** right task, and
100101 ** b) If it is using task (nTask-1), it is configured to run
100102 ** in single-threaded mode. This is important, as the
100103 ** root merge (INCRINIT_ROOT) will be using the same task
100104 ** object.
100105 */
100106 PmaReader *p = &pMain->aReadr[iTask];
100107 assert( p->pIncr==0 || (((void) (0))
100108 (p->pIncr->pTask==&pSorter->aTask[iTask]) /* a */((void) (0))
100109 && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0) /* b */((void) (0))
100110 ))((void) (0));
100111 rc = vdbePmaReaderIncrInit(p, INCRINIT_TASK1);
100112 }
100113 }
100114 pMain = 0;
100115 }
100116 if( rc==SQLITE_OK0 ){
100117 rc = vdbePmaReaderIncrMergeInit(pReadr, INCRINIT_ROOT2);
100118 }
100119 }else
100120#endif
100121 {
100122 rc = vdbeMergeEngineInit(pTask0, pMain, INCRINIT_NORMAL0);
100123 pSorter->pMerger = pMain;
100124 pMain = 0;
100125 }
100126 }
100127
100128 if( rc!=SQLITE_OK0 ){
100129 vdbeMergeEngineFree(pMain);
100130 }
100131 return rc;
100132}
100133
100134
100135/*
100136** Once the sorter has been populated by calls to sqlite3VdbeSorterWrite,
100137** this function is called to prepare for iterating through the records
100138** in sorted order.
100139*/
100140SQLITE_PRIVATEstatic int sqlite3VdbeSorterRewind(const VdbeCursor *pCsr, int *pbEof){
100141 VdbeSorter *pSorter;
100142 int rc = SQLITE_OK0; /* Return code */
100143
100144 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
100145 pSorter = pCsr->uc.pSorter;
100146 assert( pSorter )((void) (0));
100147
100148 /* If no data has been written to disk, then do not do so now. Instead,
100149 ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
100150 ** from the in-memory list. */
100151 if( pSorter->bUsePMA==0 ){
100152 if( pSorter->list.pList ){
100153 *pbEof = 0;
100154 rc = vdbeSorterSort(&pSorter->aTask[0], &pSorter->list);
100155 }else{
100156 *pbEof = 1;
100157 }
100158 return rc;
100159 }
100160
100161 /* Write the current in-memory list to a PMA. When the VdbeSorterWrite()
100162 ** function flushes the contents of memory to disk, it immediately always
100163 ** creates a new list consisting of a single key immediately afterwards.
100164 ** So the list is never empty at this point. */
100165 assert( pSorter->list.pList )((void) (0));
100166 rc = vdbeSorterFlushPMA(pSorter);
100167
100168 /* Join all threads */
100169 rc = vdbeSorterJoinAll(pSorter, rc);
100170
100171 vdbeSorterRewindDebug("rewind");
100172
100173 /* Assuming no errors have occurred, set up a merger structure to
100174 ** incrementally read and merge all remaining PMAs. */
100175 assert( pSorter->pReader==0 )((void) (0));
100176 if( rc==SQLITE_OK0 ){
100177 rc = vdbeSorterSetupMerge(pSorter);
100178 *pbEof = 0;
100179 }
100180
100181 vdbeSorterRewindDebug("rewinddone");
100182 return rc;
100183}
100184
100185/*
100186** Advance to the next element in the sorter. Return value:
100187**
100188** SQLITE_OK success
100189** SQLITE_DONE end of data
100190** otherwise some kind of error.
100191*/
100192SQLITE_PRIVATEstatic int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr){
100193 VdbeSorter *pSorter;
100194 int rc; /* Return code */
100195
100196 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
100197 pSorter = pCsr->uc.pSorter;
100198 assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) )((void) (0));
100199 if( pSorter->bUsePMA ){
100200 assert( pSorter->pReader==0 || pSorter->pMerger==0 )((void) (0));
100201 assert( pSorter->bUseThreads==0 || pSorter->pReader )((void) (0));
100202 assert( pSorter->bUseThreads==1 || pSorter->pMerger )((void) (0));
100203#if SQLITE_MAX_WORKER_THREADS8>0
100204 if( pSorter->bUseThreads ){
100205 rc = vdbePmaReaderNext(pSorter->pReader);
100206 if( rc==SQLITE_OK0 && pSorter->pReader->pFd==0 ) rc = SQLITE_DONE101;
100207 }else
100208#endif
100209 /*if( !pSorter->bUseThreads )*/ {
100210 int res = 0;
100211 assert( pSorter->pMerger!=0 )((void) (0));
100212 assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) )((void) (0));
100213 rc = vdbeMergeEngineStep(pSorter->pMerger, &res);
100214 if( rc==SQLITE_OK0 && res ) rc = SQLITE_DONE101;
100215 }
100216 }else{
100217 SorterRecord *pFree = pSorter->list.pList;
100218 pSorter->list.pList = pFree->u.pNext;
100219 pFree->u.pNext = 0;
100220 if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree);
100221 rc = pSorter->list.pList ? SQLITE_OK0 : SQLITE_DONE101;
100222 }
100223 return rc;
100224}
100225
100226/*
100227** Return a pointer to a buffer owned by the sorter that contains the
100228** current key.
100229*/
100230static void *vdbeSorterRowkey(
100231 const VdbeSorter *pSorter, /* Sorter object */
100232 int *pnKey /* OUT: Size of current key in bytes */
100233){
100234 void *pKey;
100235 if( pSorter->bUsePMA ){
100236 PmaReader *pReader;
100237#if SQLITE_MAX_WORKER_THREADS8>0
100238 if( pSorter->bUseThreads ){
100239 pReader = pSorter->pReader;
100240 }else
100241#endif
100242 /*if( !pSorter->bUseThreads )*/{
100243 pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]];
100244 }
100245 *pnKey = pReader->nKey;
100246 pKey = pReader->aKey;
100247 }else{
100248 *pnKey = pSorter->list.pList->nVal;
100249 pKey = SRVAL(pSorter->list.pList)((void*)((SorterRecord*)(pSorter->list.pList) + 1));
100250 }
100251 return pKey;
100252}
100253
100254/*
100255** Copy the current sorter key into the memory cell pOut.
100256*/
100257SQLITE_PRIVATEstatic int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){
100258 VdbeSorter *pSorter;
100259 void *pKey; int nKey; /* Sorter key to copy into pOut */
100260
100261 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
100262 pSorter = pCsr->uc.pSorter;
100263 pKey = vdbeSorterRowkey(pSorter, &nKey);
100264 if( sqlite3VdbeMemClearAndResize(pOut, nKey) ){
100265 return SQLITE_NOMEM_BKPT7;
100266 }
100267 pOut->n = nKey;
100268 MemSetTypeFlag(pOut, MEM_Blob)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0010
)
;
100269 memcpy(pOut->z, pKey, nKey);
100270
100271 return SQLITE_OK0;
100272}
100273
100274/*
100275** Compare the key in memory cell pVal with the key that the sorter cursor
100276** passed as the first argument currently points to. For the purposes of
100277** the comparison, ignore the rowid field at the end of each record.
100278**
100279** If the sorter cursor key contains any NULL values, consider it to be
100280** less than pVal. Even if pVal also contains NULL values.
100281**
100282** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
100283** Otherwise, set *pRes to a negative, zero or positive value if the
100284** key in pVal is smaller than, equal to or larger than the current sorter
100285** key.
100286**
100287** This routine forms the core of the OP_SorterCompare opcode, which in
100288** turn is used to verify uniqueness when constructing a UNIQUE INDEX.
100289*/
100290SQLITE_PRIVATEstatic int sqlite3VdbeSorterCompare(
100291 const VdbeCursor *pCsr, /* Sorter cursor */
100292 Mem *pVal, /* Value to compare to current sorter key */
100293 int nKeyCol, /* Compare this many columns */
100294 int *pRes /* OUT: Result of comparison */
100295){
100296 VdbeSorter *pSorter;
100297 UnpackedRecord *r2;
100298 KeyInfo *pKeyInfo;
100299 int i;
100300 void *pKey; int nKey; /* Sorter key to compare pVal with */
100301
100302 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
100303 pSorter = pCsr->uc.pSorter;
100304 r2 = pSorter->pUnpacked;
100305 pKeyInfo = pCsr->pKeyInfo;
100306 if( r2==0 ){
100307 r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
100308 if( r2==0 ) return SQLITE_NOMEM_BKPT7;
100309 r2->nField = nKeyCol;
100310 }
100311 assert( r2->nField==nKeyCol )((void) (0));
100312
100313 pKey = vdbeSorterRowkey(pSorter, &nKey);
100314 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, r2);
100315 for(i=0; i<nKeyCol; i++){
100316 if( r2->aMem[i].flags & MEM_Null0x0001 ){
100317 *pRes = -1;
100318 return SQLITE_OK0;
100319 }
100320 }
100321
100322 *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2);
100323 return SQLITE_OK0;
100324}
100325
100326/************** End of vdbesort.c ********************************************/
100327/************** Begin file vdbevtab.c ****************************************/
100328/*
100329** 2020-03-23
100330**
100331** The author disclaims copyright to this source code. In place of
100332** a legal notice, here is a blessing:
100333**
100334** May you do good and not evil.
100335** May you find forgiveness for yourself and forgive others.
100336** May you share freely, never taking more than you give.
100337**
100338*************************************************************************
100339**
100340** This file implements virtual-tables for examining the bytecode content
100341** of a prepared statement.
100342*/
100343/* #include "sqliteInt.h" */
100344#if defined(SQLITE_ENABLE_BYTECODE_VTAB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
100345/* #include "vdbeInt.h" */
100346
100347/* An instance of the bytecode() table-valued function.
100348*/
100349typedef struct bytecodevtab bytecodevtab;
100350struct bytecodevtab {
100351 sqlite3_vtab base; /* Base class - must be first */
100352 sqlite3 *db; /* Database connection */
100353 int bTablesUsed; /* 2 for tables_used(). 0 for bytecode(). */
100354};
100355
100356/* A cursor for scanning through the bytecode
100357*/
100358typedef struct bytecodevtab_cursor bytecodevtab_cursor;
100359struct bytecodevtab_cursor {
100360 sqlite3_vtab_cursor base; /* Base class - must be first */
100361 sqlite3_stmt *pStmt; /* The statement whose bytecode is displayed */
100362 int iRowid; /* The rowid of the output table */
100363 int iAddr; /* Address */
100364 int needFinalize; /* Cursors owns pStmt and must finalize it */
100365 int showSubprograms; /* Provide a listing of subprograms */
100366 Op *aOp; /* Operand array */
100367 char *zP4; /* Rendered P4 value */
100368 const char *zType; /* tables_used.type */
100369 const char *zSchema; /* tables_used.schema */
100370 const char *zName; /* tables_used.name */
100371 Mem sub; /* Subprograms */
100372};
100373
100374/*
100375** Create a new bytecode() table-valued function.
100376*/
100377static int bytecodevtabConnect(
100378 sqlite3 *db,
100379 void *pAux,
100380 int argc, const char *const*argv,
100381 sqlite3_vtab **ppVtab,
100382 char **pzErr
100383){
100384 bytecodevtab *pNew;
100385 int rc;
100386 int isTabUsed = pAux!=0;
100387 const char *azSchema[2] = {
100388 /* bytecode() schema */
100389 "CREATE TABLE x("
100390 "addr INT,"
100391 "opcode TEXT,"
100392 "p1 INT,"
100393 "p2 INT,"
100394 "p3 INT,"
100395 "p4 TEXT,"
100396 "p5 INT,"
100397 "comment TEXT,"
100398 "subprog TEXT,"
100399 "stmt HIDDEN"
100400 ");",
100401
100402 /* Tables_used() schema */
100403 "CREATE TABLE x("
100404 "type TEXT,"
100405 "schema TEXT,"
100406 "name TEXT,"
100407 "wr INT,"
100408 "subprog TEXT,"
100409 "stmt HIDDEN"
100410 ");"
100411 };
100412
100413 rc = sqlite3_declare_vtab(db, azSchema[isTabUsed]);
100414 if( rc==SQLITE_OK0 ){
100415 pNew = sqlite3_malloc( sizeof(*pNew) );
100416 *ppVtab = (sqlite3_vtab*)pNew;
100417 if( pNew==0 ) return SQLITE_NOMEM7;
100418 memset(pNew, 0, sizeof(*pNew));
100419 pNew->db = db;
100420 pNew->bTablesUsed = isTabUsed*2;
100421 }
100422 return rc;
100423}
100424
100425/*
100426** This method is the destructor for bytecodevtab objects.
100427*/
100428static int bytecodevtabDisconnect(sqlite3_vtab *pVtab){
100429 bytecodevtab *p = (bytecodevtab*)pVtab;
100430 sqlite3_free(p);
100431 return SQLITE_OK0;
100432}
100433
100434/*
100435** Constructor for a new bytecodevtab_cursor object.
100436*/
100437static int bytecodevtabOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
100438 bytecodevtab *pVTab = (bytecodevtab*)p;
100439 bytecodevtab_cursor *pCur;
100440 pCur = sqlite3_malloc( sizeof(*pCur) );
100441 if( pCur==0 ) return SQLITE_NOMEM7;
100442 memset(pCur, 0, sizeof(*pCur));
100443 sqlite3VdbeMemInit(&pCur->sub, pVTab->db, 1);
100444 *ppCursor = &pCur->base;
100445 return SQLITE_OK0;
100446}
100447
100448/*
100449** Clear all internal content from a bytecodevtab cursor.
100450*/
100451static void bytecodevtabCursorClear(bytecodevtab_cursor *pCur){
100452 sqlite3_free(pCur->zP4);
100453 pCur->zP4 = 0;
100454 sqlite3VdbeMemRelease(&pCur->sub);
100455 sqlite3VdbeMemSetNull(&pCur->sub);
100456 if( pCur->needFinalize ){
100457 sqlite3_finalize(pCur->pStmt);
100458 }
100459 pCur->pStmt = 0;
100460 pCur->needFinalize = 0;
100461 pCur->zType = 0;
100462 pCur->zSchema = 0;
100463 pCur->zName = 0;
100464}
100465
100466/*
100467** Destructor for a bytecodevtab_cursor.
100468*/
100469static int bytecodevtabClose(sqlite3_vtab_cursor *cur){
100470 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
100471 bytecodevtabCursorClear(pCur);
100472 sqlite3_free(pCur);
100473 return SQLITE_OK0;
100474}
100475
100476
100477/*
100478** Advance a bytecodevtab_cursor to its next row of output.
100479*/
100480static int bytecodevtabNext(sqlite3_vtab_cursor *cur){
100481 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
100482 bytecodevtab *pTab = (bytecodevtab*)cur->pVtab;
100483 int rc;
100484 if( pCur->zP4 ){
100485 sqlite3_free(pCur->zP4);
100486 pCur->zP4 = 0;
100487 }
100488 if( pCur->zName ){
100489 pCur->zName = 0;
100490 pCur->zType = 0;
100491 pCur->zSchema = 0;
100492 }
100493 rc = sqlite3VdbeNextOpcode(
100494 (Vdbe*)pCur->pStmt,
100495 pCur->showSubprograms ? &pCur->sub : 0,
100496 pTab->bTablesUsed,
100497 &pCur->iRowid,
100498 &pCur->iAddr,
100499 &pCur->aOp);
100500 if( rc!=SQLITE_OK0 ){
100501 sqlite3VdbeMemSetNull(&pCur->sub);
100502 pCur->aOp = 0;
100503 }
100504 return SQLITE_OK0;
100505}
100506
100507/*
100508** Return TRUE if the cursor has been moved off of the last
100509** row of output.
100510*/
100511static int bytecodevtabEof(sqlite3_vtab_cursor *cur){
100512 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
100513 return pCur->aOp==0;
100514}
100515
100516/*
100517** Return values of columns for the row at which the bytecodevtab_cursor
100518** is currently pointing.
100519*/
100520static int bytecodevtabColumn(
100521 sqlite3_vtab_cursor *cur, /* The cursor */
100522 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
100523 int i /* Which column to return */
100524){
100525 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
100526 bytecodevtab *pVTab = (bytecodevtab*)cur->pVtab;
100527 Op *pOp = pCur->aOp + pCur->iAddr;
100528 if( pVTab->bTablesUsed ){
100529 if( i==4 ){
100530 i = 8;
100531 }else{
100532 if( i<=2 && pCur->zType==0 ){
100533 Schema *pSchema;
100534 HashElem *k;
100535 int iDb = pOp->p3;
100536 Pgno iRoot = (Pgno)pOp->p2;
100537 sqlite3 *db = pVTab->db;
100538 pSchema = db->aDb[iDb].pSchema;
100539 pCur->zSchema = db->aDb[iDb].zDbSName;
100540 for(k=sqliteHashFirst(&pSchema->tblHash)((&pSchema->tblHash)->first); k; k=sqliteHashNext(k)((k)->next)){
100541 Table *pTab = (Table*)sqliteHashData(k)((k)->data);
100542 if( !IsVirtual(pTab)((pTab)->eTabType==1) && pTab->tnum==iRoot ){
100543 pCur->zName = pTab->zName;
100544 pCur->zType = "table";
100545 break;
100546 }
100547 }
100548 if( pCur->zName==0 ){
100549 for(k=sqliteHashFirst(&pSchema->idxHash)((&pSchema->idxHash)->first); k; k=sqliteHashNext(k)((k)->next)){
100550 Index *pIdx = (Index*)sqliteHashData(k)((k)->data);
100551 if( pIdx->tnum==iRoot ){
100552 pCur->zName = pIdx->zName;
100553 pCur->zType = "index";
100554 }
100555 }
100556 }
100557 }
100558 i += 10;
100559 }
100560 }
100561 switch( i ){
100562 case 0: /* addr */
100563 sqlite3_result_int(ctx, pCur->iAddr);
100564 break;
100565 case 1: /* opcode */
100566 sqlite3_result_text(ctx, (char*)sqlite3OpcodeName(pOp->opcode),
100567 -1, SQLITE_STATIC((sqlite3_destructor_type)0));
100568 break;
100569 case 2: /* p1 */
100570 sqlite3_result_int(ctx, pOp->p1);
100571 break;
100572 case 3: /* p2 */
100573 sqlite3_result_int(ctx, pOp->p2);
100574 break;
100575 case 4: /* p3 */
100576 sqlite3_result_int(ctx, pOp->p3);
100577 break;
100578 case 5: /* p4 */
100579 case 7: /* comment */
100580 if( pCur->zP4==0 ){
100581 pCur->zP4 = sqlite3VdbeDisplayP4(pVTab->db, pOp);
100582 }
100583 if( i==5 ){
100584 sqlite3_result_text(ctx, pCur->zP4, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
100585 }else{
100586#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
100587 char *zCom = sqlite3VdbeDisplayComment(pVTab->db, pOp, pCur->zP4);
100588 sqlite3_result_text(ctx, zCom, -1, sqlite3_free);
100589#endif
100590 }
100591 break;
100592 case 6: /* p5 */
100593 sqlite3_result_int(ctx, pOp->p5);
100594 break;
100595 case 8: { /* subprog */
100596 Op *aOp = pCur->aOp;
100597 assert( aOp[0].opcode==OP_Init )((void) (0));
100598 assert( aOp[0].p4.z==0 || strncmp(aOp[0].p4.z,"-" "- ",3)==0 )((void) (0));
100599 if( pCur->iRowid==pCur->iAddr+1 ){
100600 break; /* Result is NULL for the main program */
100601 }else if( aOp[0].p4.z!=0 ){
100602 sqlite3_result_text(ctx, aOp[0].p4.z+3, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
100603 }else{
100604 sqlite3_result_text(ctx, "(FK)", 4, SQLITE_STATIC((sqlite3_destructor_type)0));
100605 }
100606 break;
100607 }
100608 case 10: /* tables_used.type */
100609 sqlite3_result_text(ctx, pCur->zType, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
100610 break;
100611 case 11: /* tables_used.schema */
100612 sqlite3_result_text(ctx, pCur->zSchema, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
100613 break;
100614 case 12: /* tables_used.name */
100615 sqlite3_result_text(ctx, pCur->zName, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
100616 break;
100617 case 13: /* tables_used.wr */
100618 sqlite3_result_int(ctx, pOp->opcode==OP_OpenWrite113);
100619 break;
100620 }
100621 return SQLITE_OK0;
100622}
100623
100624/*
100625** Return the rowid for the current row. In this implementation, the
100626** rowid is the same as the output value.
100627*/
100628static int bytecodevtabRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
100629 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
100630 *pRowid = pCur->iRowid;
100631 return SQLITE_OK0;
100632}
100633
100634/*
100635** Initialize a cursor.
100636**
100637** idxNum==0 means show all subprograms
100638** idxNum==1 means show only the main bytecode and omit subprograms.
100639*/
100640static int bytecodevtabFilter(
100641 sqlite3_vtab_cursor *pVtabCursor,
100642 int idxNum, const char *idxStr,
100643 int argc, sqlite3_value **argv
100644){
100645 bytecodevtab_cursor *pCur = (bytecodevtab_cursor *)pVtabCursor;
100646 bytecodevtab *pVTab = (bytecodevtab *)pVtabCursor->pVtab;
100647 int rc = SQLITE_OK0;
100648
100649 bytecodevtabCursorClear(pCur);
100650 pCur->iRowid = 0;
100651 pCur->iAddr = 0;
100652 pCur->showSubprograms = idxNum==0;
100653 assert( argc==1 )((void) (0));
100654 if( sqlite3_value_type(argv[0])==SQLITE_TEXT3 ){
100655 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
100656 if( zSql==0 ){
100657 rc = SQLITE_NOMEM7;
100658 }else{
100659 rc = sqlite3_prepare_v2(pVTab->db, zSql, -1, &pCur->pStmt, 0);
100660 pCur->needFinalize = 1;
100661 }
100662 }else{
100663 pCur->pStmt = (sqlite3_stmt*)sqlite3_value_pointer(argv[0],"stmt-pointer");
100664 }
100665 if( pCur->pStmt==0 ){
100666 pVTab->base.zErrMsg = sqlite3_mprintf(
100667 "argument to %s() is not a valid SQL statement",
100668 pVTab->bTablesUsed ? "tables_used" : "bytecode"
100669 );
100670 rc = SQLITE_ERROR1;
100671 }else{
100672 bytecodevtabNext(pVtabCursor);
100673 }
100674 return rc;
100675}
100676
100677/*
100678** We must have a single stmt=? constraint that will be passed through
100679** into the xFilter method. If there is no valid stmt=? constraint,
100680** then return an SQLITE_CONSTRAINT error.
100681*/
100682static int bytecodevtabBestIndex(
100683 sqlite3_vtab *tab,
100684 sqlite3_index_info *pIdxInfo
100685){
100686 int i;
100687 int rc = SQLITE_CONSTRAINT19;
100688 struct sqlite3_index_constraint *p;
100689 bytecodevtab *pVTab = (bytecodevtab*)tab;
100690 int iBaseCol = pVTab->bTablesUsed ? 4 : 8;
100691 pIdxInfo->estimatedCost = (double)100;
100692 pIdxInfo->estimatedRows = 100;
100693 pIdxInfo->idxNum = 0;
100694 for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){
100695 if( p->usable==0 ) continue;
100696 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ2 && p->iColumn==iBaseCol+1 ){
100697 rc = SQLITE_OK0;
100698 pIdxInfo->aConstraintUsage[i].omit = 1;
100699 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
100700 }
100701 if( p->op==SQLITE_INDEX_CONSTRAINT_ISNULL71 && p->iColumn==iBaseCol ){
100702 pIdxInfo->aConstraintUsage[i].omit = 1;
100703 pIdxInfo->idxNum = 1;
100704 }
100705 }
100706 return rc;
100707}
100708
100709/*
100710** This following structure defines all the methods for the
100711** virtual table.
100712*/
100713static sqlite3_module bytecodevtabModule = {
100714 /* iVersion */ 0,
100715 /* xCreate */ 0,
100716 /* xConnect */ bytecodevtabConnect,
100717 /* xBestIndex */ bytecodevtabBestIndex,
100718 /* xDisconnect */ bytecodevtabDisconnect,
100719 /* xDestroy */ 0,
100720 /* xOpen */ bytecodevtabOpen,
100721 /* xClose */ bytecodevtabClose,
100722 /* xFilter */ bytecodevtabFilter,
100723 /* xNext */ bytecodevtabNext,
100724 /* xEof */ bytecodevtabEof,
100725 /* xColumn */ bytecodevtabColumn,
100726 /* xRowid */ bytecodevtabRowid,
100727 /* xUpdate */ 0,
100728 /* xBegin */ 0,
100729 /* xSync */ 0,
100730 /* xCommit */ 0,
100731 /* xRollback */ 0,
100732 /* xFindMethod */ 0,
100733 /* xRename */ 0,
100734 /* xSavepoint */ 0,
100735 /* xRelease */ 0,
100736 /* xRollbackTo */ 0,
100737 /* xShadowName */ 0
100738};
100739
100740
100741SQLITE_PRIVATEstatic int sqlite3VdbeBytecodeVtabInit(sqlite3 *db){
100742 int rc;
100743 rc = sqlite3_create_module(db, "bytecode", &bytecodevtabModule, 0);
100744 if( rc==SQLITE_OK0 ){
100745 rc = sqlite3_create_module(db, "tables_used", &bytecodevtabModule, &db);
100746 }
100747 return rc;
100748}
100749#elif defined(SQLITE_ENABLE_BYTECODE_VTAB)
100750SQLITE_PRIVATEstatic int sqlite3VdbeBytecodeVtabInit(sqlite3 *db){ return SQLITE_OK0; }
100751#endif /* SQLITE_ENABLE_BYTECODE_VTAB */
100752
100753/************** End of vdbevtab.c ********************************************/
100754/************** Begin file memjournal.c **************************************/
100755/*
100756** 2008 October 7
100757**
100758** The author disclaims copyright to this source code. In place of
100759** a legal notice, here is a blessing:
100760**
100761** May you do good and not evil.
100762** May you find forgiveness for yourself and forgive others.
100763** May you share freely, never taking more than you give.
100764**
100765*************************************************************************
100766**
100767** This file contains code use to implement an in-memory rollback journal.
100768** The in-memory rollback journal is used to journal transactions for
100769** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
100770**
100771** Update: The in-memory journal is also used to temporarily cache
100772** smaller journals that are not critical for power-loss recovery.
100773** For example, statement journals that are not too big will be held
100774** entirely in memory, thus reducing the number of file I/O calls, and
100775** more importantly, reducing temporary file creation events. If these
100776** journals become too large for memory, they are spilled to disk. But
100777** in the common case, they are usually small and no file I/O needs to
100778** occur.
100779*/
100780/* #include "sqliteInt.h" */
100781
100782/* Forward references to internal structures */
100783typedef struct MemJournal MemJournal;
100784typedef struct FilePoint FilePoint;
100785typedef struct FileChunk FileChunk;
100786
100787/*
100788** The rollback journal is composed of a linked list of these structures.
100789**
100790** The zChunk array is always at least 8 bytes in size - usually much more.
100791** Its actual size is stored in the MemJournal.nChunkSize variable.
100792*/
100793struct FileChunk {
100794 FileChunk *pNext; /* Next chunk in the journal */
100795 u8 zChunk[8]; /* Content of this chunk */
100796};
100797
100798/*
100799** By default, allocate this many bytes of memory for each FileChunk object.
100800*/
100801#define MEMJOURNAL_DFLT_FILECHUNKSIZE1024 1024
100802
100803/*
100804** For chunk size nChunkSize, return the number of bytes that should
100805** be allocated for each FileChunk structure.
100806*/
100807#define fileChunkSize(nChunkSize)(sizeof(FileChunk) + ((nChunkSize)-8)) (sizeof(FileChunk) + ((nChunkSize)-8))
100808
100809/*
100810** An instance of this object serves as a cursor into the rollback journal.
100811** The cursor can be either for reading or writing.
100812*/
100813struct FilePoint {
100814 sqlite3_int64 iOffset; /* Offset from the beginning of the file */
100815 FileChunk *pChunk; /* Specific chunk into which cursor points */
100816};
100817
100818/*
100819** This structure is a subclass of sqlite3_file. Each open memory-journal
100820** is an instance of this class.
100821*/
100822struct MemJournal {
100823 const sqlite3_io_methods *pMethod; /* Parent class. MUST BE FIRST */
100824 int nChunkSize; /* In-memory chunk-size */
100825
100826 int nSpill; /* Bytes of data before flushing */
100827 FileChunk *pFirst; /* Head of in-memory chunk-list */
100828 FilePoint endpoint; /* Pointer to the end of the file */
100829 FilePoint readpoint; /* Pointer to the end of the last xRead() */
100830
100831 int flags; /* xOpen flags */
100832 sqlite3_vfs *pVfs; /* The "real" underlying VFS */
100833 const char *zJournal; /* Name of the journal file */
100834};
100835
100836/*
100837** Read data from the in-memory journal file. This is the implementation
100838** of the sqlite3_vfs.xRead method.
100839*/
100840static int memjrnlRead(
100841 sqlite3_file *pJfd, /* The journal file from which to read */
100842 void *zBuf, /* Put the results here */
100843 int iAmt, /* Number of bytes to read */
100844 sqlite_int64 iOfst /* Begin reading at this offset */
100845){
100846 MemJournal *p = (MemJournal *)pJfd;
100847 u8 *zOut = zBuf;
100848 int nRead = iAmt;
100849 int iChunkOffset;
100850 FileChunk *pChunk;
100851
100852 if( (iAmt+iOfst)>p->endpoint.iOffset ){
100853 return SQLITE_IOERR_SHORT_READ(10 | (2<<8));
100854 }
100855 assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 )((void) (0));
100856 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
100857 sqlite3_int64 iOff = 0;
100858 for(pChunk=p->pFirst;
100859 ALWAYS(pChunk)(pChunk) && (iOff+p->nChunkSize)<=iOfst;
100860 pChunk=pChunk->pNext
100861 ){
100862 iOff += p->nChunkSize;
100863 }
100864 }else{
100865 pChunk = p->readpoint.pChunk;
100866 assert( pChunk!=0 )((void) (0));
100867 }
100868
100869 iChunkOffset = (int)(iOfst%p->nChunkSize);
100870 do {
100871 int iSpace = p->nChunkSize - iChunkOffset;
100872 int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset))((nRead)<((p->nChunkSize - iChunkOffset))?(nRead):((p->
nChunkSize - iChunkOffset)))
;
100873 memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
100874 zOut += nCopy;
100875 nRead -= iSpace;
100876 iChunkOffset = 0;
100877 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
100878 p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
100879 p->readpoint.pChunk = pChunk;
100880
100881 return SQLITE_OK0;
100882}
100883
100884/*
100885** Free the list of FileChunk structures headed at MemJournal.pFirst.
100886*/
100887static void memjrnlFreeChunks(FileChunk *pFirst){
100888 FileChunk *pIter;
100889 FileChunk *pNext;
100890 for(pIter=pFirst; pIter; pIter=pNext){
100891 pNext = pIter->pNext;
100892 sqlite3_free(pIter);
100893 }
100894}
100895
100896/*
100897** Flush the contents of memory to a real file on disk.
100898*/
100899static int memjrnlCreateFile(MemJournal *p){
100900 int rc;
100901 sqlite3_file *pReal = (sqlite3_file*)p;
100902 MemJournal copy = *p;
100903
100904 memset(p, 0, sizeof(MemJournal));
100905 rc = sqlite3OsOpen(copy.pVfs, copy.zJournal, pReal, copy.flags, 0);
100906 if( rc==SQLITE_OK0 ){
100907 int nChunk = copy.nChunkSize;
100908 i64 iOff = 0;
100909 FileChunk *pIter;
100910 for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
100911 if( iOff + nChunk > copy.endpoint.iOffset ){
100912 nChunk = copy.endpoint.iOffset - iOff;
100913 }
100914 rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff);
100915 if( rc ) break;
100916 iOff += nChunk;
100917 }
100918 if( rc==SQLITE_OK0 ){
100919 /* No error has occurred. Free the in-memory buffers. */
100920 memjrnlFreeChunks(copy.pFirst);
100921 }
100922 }
100923 if( rc!=SQLITE_OK0 ){
100924 /* If an error occurred while creating or writing to the file, restore
100925 ** the original before returning. This way, SQLite uses the in-memory
100926 ** journal data to roll back changes made to the internal page-cache
100927 ** before this function was called. */
100928 sqlite3OsClose(pReal);
100929 *p = copy;
100930 }
100931 return rc;
100932}
100933
100934
100935/* Forward reference */
100936static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size);
100937
100938/*
100939** Write data to the file.
100940*/
100941static int memjrnlWrite(
100942 sqlite3_file *pJfd, /* The journal file into which to write */
100943 const void *zBuf, /* Take data to be written from here */
100944 int iAmt, /* Number of bytes to write */
100945 sqlite_int64 iOfst /* Begin writing at this offset into the file */
100946){
100947 MemJournal *p = (MemJournal *)pJfd;
100948 int nWrite = iAmt;
100949 u8 *zWrite = (u8 *)zBuf;
100950
100951 /* If the file should be created now, create it and write the new data
100952 ** into the file on disk. */
100953 if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
100954 int rc = memjrnlCreateFile(p);
100955 if( rc==SQLITE_OK0 ){
100956 rc = sqlite3OsWrite(pJfd, zBuf, iAmt, iOfst);
100957 }
100958 return rc;
100959 }
100960
100961 /* If the contents of this write should be stored in memory */
100962 else{
100963 /* An in-memory journal file should only ever be appended to. Random
100964 ** access writes are not required. The only exception to this is when
100965 ** the in-memory journal is being used by a connection using the
100966 ** atomic-write optimization. In this case the first 28 bytes of the
100967 ** journal file may be written as part of committing the transaction. */
100968 assert( iOfst<=p->endpoint.iOffset )((void) (0));
100969 if( iOfst>0 && iOfst!=p->endpoint.iOffset ){
100970 memjrnlTruncate(pJfd, iOfst);
100971 }
100972 if( iOfst==0 && p->pFirst ){
100973 assert( p->nChunkSize>iAmt )((void) (0));
100974 memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
100975 }else{
100976 while( nWrite>0 ){
100977 FileChunk *pChunk = p->endpoint.pChunk;
100978 int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
100979 int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset)((nWrite)<(p->nChunkSize - iChunkOffset)?(nWrite):(p->
nChunkSize - iChunkOffset))
;
100980
100981 assert( pChunk!=0 || iChunkOffset==0 )((void) (0));
100982 if( iChunkOffset==0 ){
100983 /* New chunk is required to extend the file. */
100984 FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize)(sizeof(FileChunk) + ((p->nChunkSize)-8)));
100985 if( !pNew ){
100986 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
100987 }
100988 pNew->pNext = 0;
100989 if( pChunk ){
100990 assert( p->pFirst )((void) (0));
100991 pChunk->pNext = pNew;
100992 }else{
100993 assert( !p->pFirst )((void) (0));
100994 p->pFirst = pNew;
100995 }
100996 pChunk = p->endpoint.pChunk = pNew;
100997 }
100998
100999 assert( pChunk!=0 )((void) (0));
101000 memcpy((u8*)pChunk->zChunk + iChunkOffset, zWrite, iSpace);
101001 zWrite += iSpace;
101002 nWrite -= iSpace;
101003 p->endpoint.iOffset += iSpace;
101004 }
101005 }
101006 }
101007
101008 return SQLITE_OK0;
101009}
101010
101011/*
101012** Truncate the in-memory file.
101013*/
101014static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
101015 MemJournal *p = (MemJournal *)pJfd;
101016 assert( p->endpoint.pChunk==0 || p->endpoint.pChunk->pNext==0 )((void) (0));
101017 if( size<p->endpoint.iOffset ){
101018 FileChunk *pIter = 0;
101019 if( size==0 ){
101020 memjrnlFreeChunks(p->pFirst);
101021 p->pFirst = 0;
101022 }else{
101023 i64 iOff = p->nChunkSize;
101024 for(pIter=p->pFirst; ALWAYS(pIter)(pIter) && iOff<size; pIter=pIter->pNext){
101025 iOff += p->nChunkSize;
101026 }
101027 if( ALWAYS(pIter)(pIter) ){
101028 memjrnlFreeChunks(pIter->pNext);
101029 pIter->pNext = 0;
101030 }
101031 }
101032
101033 p->endpoint.pChunk = pIter;
101034 p->endpoint.iOffset = size;
101035 p->readpoint.pChunk = 0;
101036 p->readpoint.iOffset = 0;
101037 }
101038 return SQLITE_OK0;
101039}
101040
101041/*
101042** Close the file.
101043*/
101044static int memjrnlClose(sqlite3_file *pJfd){
101045 MemJournal *p = (MemJournal *)pJfd;
101046 memjrnlFreeChunks(p->pFirst);
101047 return SQLITE_OK0;
101048}
101049
101050/*
101051** Sync the file.
101052**
101053** If the real file has been created, call its xSync method. Otherwise,
101054** syncing an in-memory journal is a no-op.
101055*/
101056static int memjrnlSync(sqlite3_file *pJfd, int flags){
101057 UNUSED_PARAMETER2(pJfd, flags)(void)(pJfd),(void)(flags);
101058 return SQLITE_OK0;
101059}
101060
101061/*
101062** Query the size of the file in bytes.
101063*/
101064static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
101065 MemJournal *p = (MemJournal *)pJfd;
101066 *pSize = (sqlite_int64) p->endpoint.iOffset;
101067 return SQLITE_OK0;
101068}
101069
101070/*
101071** Table of methods for MemJournal sqlite3_file object.
101072*/
101073static const struct sqlite3_io_methods MemJournalMethods = {
101074 1, /* iVersion */
101075 memjrnlClose, /* xClose */
101076 memjrnlRead, /* xRead */
101077 memjrnlWrite, /* xWrite */
101078 memjrnlTruncate, /* xTruncate */
101079 memjrnlSync, /* xSync */
101080 memjrnlFileSize, /* xFileSize */
101081 0, /* xLock */
101082 0, /* xUnlock */
101083 0, /* xCheckReservedLock */
101084 0, /* xFileControl */
101085 0, /* xSectorSize */
101086 0, /* xDeviceCharacteristics */
101087 0, /* xShmMap */
101088 0, /* xShmLock */
101089 0, /* xShmBarrier */
101090 0, /* xShmUnmap */
101091 0, /* xFetch */
101092 0 /* xUnfetch */
101093};
101094
101095/*
101096** Open a journal file.
101097**
101098** The behaviour of the journal file depends on the value of parameter
101099** nSpill. If nSpill is 0, then the journal file is always create and
101100** accessed using the underlying VFS. If nSpill is less than zero, then
101101** all content is always stored in main-memory. Finally, if nSpill is a
101102** positive value, then the journal file is initially created in-memory
101103** but may be flushed to disk later on. In this case the journal file is
101104** flushed to disk either when it grows larger than nSpill bytes in size,
101105** or when sqlite3JournalCreate() is called.
101106*/
101107SQLITE_PRIVATEstatic int sqlite3JournalOpen(
101108 sqlite3_vfs *pVfs, /* The VFS to use for actual file I/O */
101109 const char *zName, /* Name of the journal file */
101110 sqlite3_file *pJfd, /* Preallocated, blank file handle */
101111 int flags, /* Opening flags */
101112 int nSpill /* Bytes buffered before opening the file */
101113){
101114 MemJournal *p = (MemJournal*)pJfd;
101115
101116 /* Zero the file-handle object. If nSpill was passed zero, initialize
101117 ** it using the sqlite3OsOpen() function of the underlying VFS. In this
101118 ** case none of the code in this module is executed as a result of calls
101119 ** made on the journal file-handle. */
101120 memset(p, 0, sizeof(MemJournal));
101121 if( nSpill==0 ){
101122 return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
101123 }
101124
101125 if( nSpill>0 ){
101126 p->nChunkSize = nSpill;
101127 }else{
101128 p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE1024 - sizeof(FileChunk);
101129 assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) )((void) (0));
101130 }
101131
101132 pJfd->pMethods = (const sqlite3_io_methods*)&MemJournalMethods;
101133 p->nSpill = nSpill;
101134 p->flags = flags;
101135 p->zJournal = zName;
101136 p->pVfs = pVfs;
101137 return SQLITE_OK0;
101138}
101139
101140/*
101141** Open an in-memory journal file.
101142*/
101143SQLITE_PRIVATEstatic void sqlite3MemJournalOpen(sqlite3_file *pJfd){
101144 sqlite3JournalOpen(0, 0, pJfd, 0, -1);
101145}
101146
101147#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
101148 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
101149/*
101150** If the argument p points to a MemJournal structure that is not an
101151** in-memory-only journal file (i.e. is one that was opened with a +ve
101152** nSpill parameter or as SQLITE_OPEN_MAIN_JOURNAL), and the underlying
101153** file has not yet been created, create it now.
101154*/
101155SQLITE_PRIVATEstatic int sqlite3JournalCreate(sqlite3_file *pJfd){
101156 int rc = SQLITE_OK0;
101157 MemJournal *p = (MemJournal*)pJfd;
101158 if( pJfd->pMethods==&MemJournalMethods && (
101159#ifdef SQLITE_ENABLE_ATOMIC_WRITE
101160 p->nSpill>0
101161#else
101162 /* While this appears to not be possible without ATOMIC_WRITE, the
101163 ** paths are complex, so it seems prudent to leave the test in as
101164 ** a NEVER(), in case our analysis is subtly flawed. */
101165 NEVER(p->nSpill>0)(p->nSpill>0)
101166#endif
101167#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
101168 || (p->flags & SQLITE_OPEN_MAIN_JOURNAL0x00000800)
101169#endif
101170 )){
101171 rc = memjrnlCreateFile(p);
101172 }
101173 return rc;
101174}
101175#endif
101176
101177/*
101178** The file-handle passed as the only argument is open on a journal file.
101179** Return true if this "journal file" is currently stored in heap memory,
101180** or false otherwise.
101181*/
101182SQLITE_PRIVATEstatic int sqlite3JournalIsInMemory(sqlite3_file *p){
101183 return p->pMethods==&MemJournalMethods;
101184}
101185
101186/*
101187** Return the number of bytes required to store a JournalFile that uses vfs
101188** pVfs to create the underlying on-disk files.
101189*/
101190SQLITE_PRIVATEstatic int sqlite3JournalSize(sqlite3_vfs *pVfs){
101191 return MAX(pVfs->szOsFile, (int)sizeof(MemJournal))((pVfs->szOsFile)>((int)sizeof(MemJournal))?(pVfs->szOsFile
):((int)sizeof(MemJournal)))
;
101192}
101193
101194/************** End of memjournal.c ******************************************/
101195/************** Begin file walker.c ******************************************/
101196/*
101197** 2008 August 16
101198**
101199** The author disclaims copyright to this source code. In place of
101200** a legal notice, here is a blessing:
101201**
101202** May you do good and not evil.
101203** May you find forgiveness for yourself and forgive others.
101204** May you share freely, never taking more than you give.
101205**
101206*************************************************************************
101207** This file contains routines used for walking the parser tree for
101208** an SQL statement.
101209*/
101210/* #include "sqliteInt.h" */
101211/* #include <stdlib.h> */
101212/* #include <string.h> */
101213
101214
101215#if !defined(SQLITE_OMIT_WINDOWFUNC)
101216/*
101217** Walk all expressions linked into the list of Window objects passed
101218** as the second argument.
101219*/
101220static int walkWindowList(Walker *pWalker, Window *pList, int bOneOnly){
101221 Window *pWin;
101222 for(pWin=pList; pWin; pWin=pWin->pNextWin){
101223 int rc;
101224 rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy);
101225 if( rc ) return WRC_Abort2;
101226 rc = sqlite3WalkExprList(pWalker, pWin->pPartition);
101227 if( rc ) return WRC_Abort2;
101228 rc = sqlite3WalkExpr(pWalker, pWin->pFilter);
101229 if( rc ) return WRC_Abort2;
101230 rc = sqlite3WalkExpr(pWalker, pWin->pStart);
101231 if( rc ) return WRC_Abort2;
101232 rc = sqlite3WalkExpr(pWalker, pWin->pEnd);
101233 if( rc ) return WRC_Abort2;
101234 if( bOneOnly ) break;
101235 }
101236 return WRC_Continue0;
101237}
101238#endif
101239
101240/*
101241** Walk an expression tree. Invoke the callback once for each node
101242** of the expression, while descending. (In other words, the callback
101243** is invoked before visiting children.)
101244**
101245** The return value from the callback should be one of the WRC_*
101246** constants to specify how to proceed with the walk.
101247**
101248** WRC_Continue Continue descending down the tree.
101249**
101250** WRC_Prune Do not descend into child nodes, but allow
101251** the walk to continue with sibling nodes.
101252**
101253** WRC_Abort Do no more callbacks. Unwind the stack and
101254** return from the top-level walk call.
101255**
101256** The return value from this routine is WRC_Abort to abandon the tree walk
101257** and WRC_Continue to continue.
101258*/
101259static SQLITE_NOINLINE__attribute__((noinline)) int walkExpr(Walker *pWalker, Expr *pExpr){
101260 int rc;
101261 testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
101262 testcase( ExprHasProperty(pExpr, EP_Reduced) );
101263 while(1){
101264 rc = pWalker->xExprCallback(pWalker, pExpr);
101265 if( rc ) return rc & WRC_Abort2;
101266 if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf))(((pExpr)->flags&((0x010000|0x800000)))!=0) ){
101267 assert( pExpr->x.pList==0 || pExpr->pRight==0 )((void) (0));
101268 if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort2;
101269 if( pExpr->pRight ){
101270 assert( !ExprHasProperty(pExpr, EP_WinFunc) )((void) (0));
101271 pExpr = pExpr->pRight;
101272 continue;
101273 }else if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
101274 assert( !ExprHasProperty(pExpr, EP_WinFunc) )((void) (0));
101275 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort2;
101276 }else{
101277 if( pExpr->x.pList ){
101278 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort2;
101279 }
101280#ifndef SQLITE_OMIT_WINDOWFUNC
101281 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
101282 if( walkWindowList(pWalker, pExpr->y.pWin, 1) ) return WRC_Abort2;
101283 }
101284#endif
101285 }
101286 }
101287 break;
101288 }
101289 return WRC_Continue0;
101290}
101291SQLITE_PRIVATEstatic int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
101292 return pExpr ? walkExpr(pWalker,pExpr) : WRC_Continue0;
101293}
101294
101295/*
101296** Call sqlite3WalkExpr() for every expression in list p or until
101297** an abort request is seen.
101298*/
101299SQLITE_PRIVATEstatic int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
101300 int i;
101301 struct ExprList_item *pItem;
101302 if( p ){
101303 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
101304 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort2;
101305 }
101306 }
101307 return WRC_Continue0;
101308}
101309
101310/*
101311** This is a no-op callback for Walker->xSelectCallback2. If this
101312** callback is set, then the Select->pWinDefn list is traversed.
101313*/
101314SQLITE_PRIVATEstatic void sqlite3WalkWinDefnDummyCallback(Walker *pWalker, Select *p){
101315 UNUSED_PARAMETER(pWalker)(void)(pWalker);
101316 UNUSED_PARAMETER(p)(void)(p);
101317 /* No-op */
101318}
101319
101320/*
101321** Walk all expressions associated with SELECT statement p. Do
101322** not invoke the SELECT callback on p, but do (of course) invoke
101323** any expr callbacks and SELECT callbacks that come from subqueries.
101324** Return WRC_Abort or WRC_Continue.
101325*/
101326SQLITE_PRIVATEstatic int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
101327 if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort2;
101328 if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort2;
101329 if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort2;
101330 if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort2;
101331 if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort2;
101332 if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort2;
101333#if !defined(SQLITE_OMIT_WINDOWFUNC)
101334 if( p->pWinDefn ){
101335 Parse *pParse;
101336 if( pWalker->xSelectCallback2==sqlite3WalkWinDefnDummyCallback
101337 || ((pParse = pWalker->pParse)!=0 && IN_RENAME_OBJECT(pParse->eParseMode>=2))
101338#ifndef SQLITE_OMIT_CTE
101339 || pWalker->xSelectCallback2==sqlite3SelectPopWith
101340#endif
101341 ){
101342 /* The following may return WRC_Abort if there are unresolvable
101343 ** symbols (e.g. a table that does not exist) in a window definition. */
101344 int rc = walkWindowList(pWalker, p->pWinDefn, 0);
101345 return rc;
101346 }
101347 }
101348#endif
101349 return WRC_Continue0;
101350}
101351
101352/*
101353** Walk the parse trees associated with all subqueries in the
101354** FROM clause of SELECT statement p. Do not invoke the select
101355** callback on p, but do invoke it on each FROM clause subquery
101356** and on any subqueries further down in the tree. Return
101357** WRC_Abort or WRC_Continue;
101358*/
101359SQLITE_PRIVATEstatic int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
101360 SrcList *pSrc;
101361 int i;
101362 SrcItem *pItem;
101363
101364 pSrc = p->pSrc;
101365 if( ALWAYS(pSrc)(pSrc) ){
101366 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
101367 if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
101368 return WRC_Abort2;
101369 }
101370 if( pItem->fg.isTabFunc
101371 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
101372 ){
101373 return WRC_Abort2;
101374 }
101375 }
101376 }
101377 return WRC_Continue0;
101378}
101379
101380/*
101381** Call sqlite3WalkExpr() for every expression in Select statement p.
101382** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
101383** on the compound select chain, p->pPrior.
101384**
101385** If it is not NULL, the xSelectCallback() callback is invoked before
101386** the walk of the expressions and FROM clause. The xSelectCallback2()
101387** method is invoked following the walk of the expressions and FROM clause,
101388** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
101389** and if the expressions and FROM clause both return WRC_Continue;
101390**
101391** Return WRC_Continue under normal conditions. Return WRC_Abort if
101392** there is an abort request.
101393**
101394** If the Walker does not have an xSelectCallback() then this routine
101395** is a no-op returning WRC_Continue.
101396*/
101397SQLITE_PRIVATEstatic int sqlite3WalkSelect(Walker *pWalker, Select *p){
101398 int rc;
101399 if( p==0 ) return WRC_Continue0;
101400 if( pWalker->xSelectCallback==0 ) return WRC_Continue0;
101401 do{
101402 rc = pWalker->xSelectCallback(pWalker, p);
101403 if( rc ) return rc & WRC_Abort2;
101404 if( sqlite3WalkSelectExpr(pWalker, p)
101405 || sqlite3WalkSelectFrom(pWalker, p)
101406 ){
101407 return WRC_Abort2;
101408 }
101409 if( pWalker->xSelectCallback2 ){
101410 pWalker->xSelectCallback2(pWalker, p);
101411 }
101412 p = p->pPrior;
101413 }while( p!=0 );
101414 return WRC_Continue0;
101415}
101416
101417/* Increase the walkerDepth when entering a subquery, and
101418** descrease when leaving the subquery.
101419*/
101420SQLITE_PRIVATEstatic int sqlite3WalkerDepthIncrease(Walker *pWalker, Select *pSelect){
101421 UNUSED_PARAMETER(pSelect)(void)(pSelect);
101422 pWalker->walkerDepth++;
101423 return WRC_Continue0;
101424}
101425SQLITE_PRIVATEstatic void sqlite3WalkerDepthDecrease(Walker *pWalker, Select *pSelect){
101426 UNUSED_PARAMETER(pSelect)(void)(pSelect);
101427 pWalker->walkerDepth--;
101428}
101429
101430
101431/*
101432** No-op routine for the parse-tree walker.
101433**
101434** When this routine is the Walker.xExprCallback then expression trees
101435** are walked without any actions being taken at each node. Presumably,
101436** when this routine is used for Walker.xExprCallback then
101437** Walker.xSelectCallback is set to do something useful for every
101438** subquery in the parser tree.
101439*/
101440SQLITE_PRIVATEstatic int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
101441 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
101442 return WRC_Continue0;
101443}
101444
101445/*
101446** No-op routine for the parse-tree walker for SELECT statements.
101447** subquery in the parser tree.
101448*/
101449SQLITE_PRIVATEstatic int sqlite3SelectWalkNoop(Walker *NotUsed, Select *NotUsed2){
101450 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
101451 return WRC_Continue0;
101452}
101453
101454/************** End of walker.c **********************************************/
101455/************** Begin file resolve.c *****************************************/
101456/*
101457** 2008 August 18
101458**
101459** The author disclaims copyright to this source code. In place of
101460** a legal notice, here is a blessing:
101461**
101462** May you do good and not evil.
101463** May you find forgiveness for yourself and forgive others.
101464** May you share freely, never taking more than you give.
101465**
101466*************************************************************************
101467**
101468** This file contains routines used for walking the parser tree and
101469** resolve all identifiers by associating them with a particular
101470** table and column.
101471*/
101472/* #include "sqliteInt.h" */
101473
101474/*
101475** Magic table number to mean the EXCLUDED table in an UPSERT statement.
101476*/
101477#define EXCLUDED_TABLE_NUMBER2 2
101478
101479/*
101480** Walk the expression tree pExpr and increase the aggregate function
101481** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
101482** This needs to occur when copying a TK_AGG_FUNCTION node from an
101483** outer query into an inner subquery.
101484**
101485** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..)
101486** is a helper function - a callback for the tree walker.
101487**
101488** See also the sqlite3WindowExtraAggFuncDepth() routine in window.c
101489*/
101490static int incrAggDepth(Walker *pWalker, Expr *pExpr){
101491 if( pExpr->op==TK_AGG_FUNCTION168 ) pExpr->op2 += pWalker->u.n;
101492 return WRC_Continue0;
101493}
101494static void incrAggFunctionDepth(Expr *pExpr, int N){
101495 if( N>0 ){
101496 Walker w;
101497 memset(&w, 0, sizeof(w));
101498 w.xExprCallback = incrAggDepth;
101499 w.u.n = N;
101500 sqlite3WalkExpr(&w, pExpr);
101501 }
101502}
101503
101504/*
101505** Turn the pExpr expression into an alias for the iCol-th column of the
101506** result set in pEList.
101507**
101508** If the reference is followed by a COLLATE operator, then make sure
101509** the COLLATE operator is preserved. For example:
101510**
101511** SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
101512**
101513** Should be transformed into:
101514**
101515** SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
101516**
101517** The nSubquery parameter specifies how many levels of subquery the
101518** alias is removed from the original expression. The usual value is
101519** zero but it might be more if the alias is contained within a subquery
101520** of the original expression. The Expr.op2 field of TK_AGG_FUNCTION
101521** structures must be increased by the nSubquery amount.
101522*/
101523static void resolveAlias(
101524 Parse *pParse, /* Parsing context */
101525 ExprList *pEList, /* A result set */
101526 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
101527 Expr *pExpr, /* Transform this into an alias to the result set */
101528 int nSubquery /* Number of subqueries that the label is moving */
101529){
101530 Expr *pOrig; /* The iCol-th column of the result set */
101531 Expr *pDup; /* Copy of pOrig */
101532 sqlite3 *db; /* The database connection */
101533
101534 assert( iCol>=0 && iCol<pEList->nExpr )((void) (0));
101535 pOrig = pEList->a[iCol].pExpr;
101536 assert( pOrig!=0 )((void) (0));
101537 db = pParse->db;
101538 pDup = sqlite3ExprDup(db, pOrig, 0);
101539 if( db->mallocFailed ){
101540 sqlite3ExprDelete(db, pDup);
101541 pDup = 0;
101542 }else{
101543 Expr temp;
101544 incrAggFunctionDepth(pDup, nSubquery);
101545 if( pExpr->op==TK_COLLATE113 ){
101546 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
101547 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
101548 }
101549 memcpy(&temp, pDup, sizeof(Expr));
101550 memcpy(pDup, pExpr, sizeof(Expr));
101551 memcpy(pExpr, &temp, sizeof(Expr));
101552 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
101553 if( ALWAYS(pExpr->y.pWin!=0)(pExpr->y.pWin!=0) ){
101554 pExpr->y.pWin->pOwner = pExpr;
101555 }
101556 }
101557 sqlite3ParserAddCleanup(pParse,
101558 (void(*)(sqlite3*,void*))sqlite3ExprDelete,
101559 pDup);
101560 }
101561}
101562
101563/*
101564** Subqueries stores the original database, table and column names for their
101565** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN".
101566** Check to see if the zSpan given to this routine matches the zDb, zTab,
101567** and zCol. If any of zDb, zTab, and zCol are NULL then those fields will
101568** match anything.
101569*/
101570SQLITE_PRIVATEstatic int sqlite3MatchEName(
101571 const struct ExprList_item *pItem,
101572 const char *zCol,
101573 const char *zTab,
101574 const char *zDb
101575){
101576 int n;
101577 const char *zSpan;
101578 if( pItem->fg.eEName!=ENAME_TAB2 ) return 0;
101579 zSpan = pItem->zEName;
101580 for(n=0; ALWAYS(zSpan[n])(zSpan[n]) && zSpan[n]!='.'; n++){}
101581 if( zDb && (sqlite3StrNICmpsqlite3_strnicmp(zSpan, zDb, n)!=0 || zDb[n]!=0) ){
101582 return 0;
101583 }
101584 zSpan += n+1;
101585 for(n=0; ALWAYS(zSpan[n])(zSpan[n]) && zSpan[n]!='.'; n++){}
101586 if( zTab && (sqlite3StrNICmpsqlite3_strnicmp(zSpan, zTab, n)!=0 || zTab[n]!=0) ){
101587 return 0;
101588 }
101589 zSpan += n+1;
101590 if( zCol && sqlite3StrICmp(zSpan, zCol)!=0 ){
101591 return 0;
101592 }
101593 return 1;
101594}
101595
101596/*
101597** Return TRUE if the double-quoted string mis-feature should be supported.
101598*/
101599static int areDoubleQuotedStringsEnabled(sqlite3 *db, NameContext *pTopNC){
101600 if( db->init.busy ) return 1; /* Always support for legacy schemas */
101601 if( pTopNC->ncFlags & NC_IsDDL0x010000 ){
101602 /* Currently parsing a DDL statement */
101603 if( sqlite3WritableSchema(db) && (db->flags & SQLITE_DqsDML0x40000000)!=0 ){
101604 return 1;
101605 }
101606 return (db->flags & SQLITE_DqsDDL0x20000000)!=0;
101607 }else{
101608 /* Currently parsing a DML statement */
101609 return (db->flags & SQLITE_DqsDML0x40000000)!=0;
101610 }
101611}
101612
101613/*
101614** The argument is guaranteed to be a non-NULL Expr node of type TK_COLUMN.
101615** return the appropriate colUsed mask.
101616*/
101617SQLITE_PRIVATEstatic Bitmask sqlite3ExprColUsed(Expr *pExpr){
101618 int n;
101619 Table *pExTab;
101620
101621 n = pExpr->iColumn;
101622 assert( ExprUseYTab(pExpr) )((void) (0));
101623 pExTab = pExpr->y.pTab;
101624 assert( pExTab!=0 )((void) (0));
101625 if( (pExTab->tabFlags & TF_HasGenerated0x00000060)!=0
101626 && (pExTab->aCol[n].colFlags & COLFLAG_GENERATED0x0060)!=0
101627 ){
101628 testcase( pExTab->nCol==BMS-1 );
101629 testcase( pExTab->nCol==BMS );
101630 return pExTab->nCol>=BMS((int)(sizeof(Bitmask)*8)) ? ALLBITS((Bitmask)-1) : MASKBIT(pExTab->nCol)(((Bitmask)1)<<(pExTab->nCol))-1;
101631 }else{
101632 testcase( n==BMS-1 );
101633 testcase( n==BMS );
101634 if( n>=BMS((int)(sizeof(Bitmask)*8)) ) n = BMS((int)(sizeof(Bitmask)*8))-1;
101635 return ((Bitmask)1)<<n;
101636 }
101637}
101638
101639/*
101640** Create a new expression term for the column specified by pMatch and
101641** iColumn. Append this new expression term to the FULL JOIN Match set
101642** in *ppList. Create a new *ppList if this is the first term in the
101643** set.
101644*/
101645static void extendFJMatch(
101646 Parse *pParse, /* Parsing context */
101647 ExprList **ppList, /* ExprList to extend */
101648 SrcItem *pMatch, /* Source table containing the column */
101649 i16 iColumn /* The column number */
101650){
101651 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLUMN167, 0, 0);
101652 if( pNew ){
101653 pNew->iTable = pMatch->iCursor;
101654 pNew->iColumn = iColumn;
101655 pNew->y.pTab = pMatch->pTab;
101656 assert( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 )((void) (0));
101657 ExprSetProperty(pNew, EP_CanBeNull)(pNew)->flags|=(0x200000);
101658 *ppList = sqlite3ExprListAppend(pParse, *ppList, pNew);
101659 }
101660}
101661
101662/*
101663** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
101664** that name in the set of source tables in pSrcList and make the pExpr
101665** expression node refer back to that source column. The following changes
101666** are made to pExpr:
101667**
101668** pExpr->iDb Set the index in db->aDb[] of the database X
101669** (even if X is implied).
101670** pExpr->iTable Set to the cursor number for the table obtained
101671** from pSrcList.
101672** pExpr->y.pTab Points to the Table structure of X.Y (even if
101673** X and/or Y are implied.)
101674** pExpr->iColumn Set to the column number within the table.
101675** pExpr->op Set to TK_COLUMN.
101676** pExpr->pLeft Any expression this points to is deleted
101677** pExpr->pRight Any expression this points to is deleted.
101678**
101679** The zDb variable is the name of the database (the "X"). This value may be
101680** NULL meaning that name is of the form Y.Z or Z. Any available database
101681** can be used. The zTable variable is the name of the table (the "Y"). This
101682** value can be NULL if zDb is also NULL. If zTable is NULL it
101683** means that the form of the name is Z and that columns from any table
101684** can be used.
101685**
101686** If the name cannot be resolved unambiguously, leave an error message
101687** in pParse and return WRC_Abort. Return WRC_Prune on success.
101688*/
101689static int lookupName(
101690 Parse *pParse, /* The parsing context */
101691 const char *zDb, /* Name of the database containing table, or NULL */
101692 const char *zTab, /* Name of table containing column, or NULL */
101693 const char *zCol, /* Name of the column. */
101694 NameContext *pNC, /* The name context used to resolve the name */
101695 Expr *pExpr /* Make this EXPR node point to the selected column */
101696){
101697 int i, j; /* Loop counters */
101698 int cnt = 0; /* Number of matching column names */
101699 int cntTab = 0; /* Number of matching table names */
101700 int nSubquery = 0; /* How many levels of subquery */
101701 sqlite3 *db = pParse->db; /* The database connection */
101702 SrcItem *pItem; /* Use for looping over pSrcList items */
101703 SrcItem *pMatch = 0; /* The matching pSrcList item */
101704 NameContext *pTopNC = pNC; /* First namecontext in the list */
101705 Schema *pSchema = 0; /* Schema of the expression */
101706 int eNewExprOp = TK_COLUMN167; /* New value for pExpr->op on success */
101707 Table *pTab = 0; /* Table holding the row */
101708 Column *pCol; /* A column of pTab */
101709 ExprList *pFJMatch = 0; /* Matches for FULL JOIN .. USING */
101710
101711 assert( pNC )((void) (0)); /* the name context cannot be NULL. */
101712 assert( zCol )((void) (0)); /* The Z in X.Y.Z cannot be NULL */
101713 assert( zDb==0 || zTab!=0 )((void) (0));
101714 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) )((void) (0));
101715
101716 /* Initialize the node to no-match */
101717 pExpr->iTable = -1;
101718 ExprSetVVAProperty(pExpr, EP_NoReduce);
101719
101720 /* Translate the schema name in zDb into a pointer to the corresponding
101721 ** schema. If not found, pSchema will remain NULL and nothing will match
101722 ** resulting in an appropriate error message toward the end of this routine
101723 */
101724 if( zDb ){
101725 testcase( pNC->ncFlags & NC_PartIdx );
101726 testcase( pNC->ncFlags & NC_IsCheck );
101727 if( (pNC->ncFlags & (NC_PartIdx0x000002|NC_IsCheck0x000004))!=0 ){
101728 /* Silently ignore database qualifiers inside CHECK constraints and
101729 ** partial indices. Do not raise errors because that might break
101730 ** legacy and because it does not hurt anything to just ignore the
101731 ** database name. */
101732 zDb = 0;
101733 }else{
101734 for(i=0; i<db->nDb; i++){
101735 assert( db->aDb[i].zDbSName )((void) (0));
101736 if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
101737 pSchema = db->aDb[i].pSchema;
101738 break;
101739 }
101740 }
101741 if( i==db->nDb && sqlite3StrICmp("main", zDb)==0 ){
101742 /* This branch is taken when the main database has been renamed
101743 ** using SQLITE_DBCONFIG_MAINDBNAME. */
101744 pSchema = db->aDb[0].pSchema;
101745 zDb = db->aDb[0].zDbSName;
101746 }
101747 }
101748 }
101749
101750 /* Start at the inner-most context and move outward until a match is found */
101751 assert( pNC && cnt==0 )((void) (0));
101752 do{
101753 ExprList *pEList;
101754 SrcList *pSrcList = pNC->pSrcList;
101755
101756 if( pSrcList ){
101757 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
101758 u8 hCol;
101759 pTab = pItem->pTab;
101760 assert( pTab!=0 && pTab->zName!=0 )((void) (0));
101761 assert( pTab->nCol>0 || pParse->nErr )((void) (0));
101762 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) )((void) (0));
101763 if( pItem->fg.isNestedFrom ){
101764 /* In this case, pItem is a subquery that has been formed from a
101765 ** parenthesized subset of the FROM clause terms. Example:
101766 ** .... FROM t1 LEFT JOIN (t2 RIGHT JOIN t3 USING(x)) USING(y) ...
101767 ** \_________________________/
101768 ** This pItem -------------^
101769 */
101770 int hit = 0;
101771 assert( pItem->pSelect!=0 )((void) (0));
101772 pEList = pItem->pSelect->pEList;
101773 assert( pEList!=0 )((void) (0));
101774 assert( pEList->nExpr==pTab->nCol )((void) (0));
101775 for(j=0; j<pEList->nExpr; j++){
101776 if( !sqlite3MatchEName(&pEList->a[j], zCol, zTab, zDb) ){
101777 continue;
101778 }
101779 if( cnt>0 ){
101780 if( pItem->fg.isUsing==0
101781 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
101782 ){
101783 /* Two or more tables have the same column name which is
101784 ** not joined by USING. This is an error. Signal as much
101785 ** by clearing pFJMatch and letting cnt go above 1. */
101786 sqlite3ExprListDelete(db, pFJMatch);
101787 pFJMatch = 0;
101788 }else
101789 if( (pItem->fg.jointype & JT_RIGHT0x10)==0 ){
101790 /* An INNER or LEFT JOIN. Use the left-most table */
101791 continue;
101792 }else
101793 if( (pItem->fg.jointype & JT_LEFT0x08)==0 ){
101794 /* A RIGHT JOIN. Use the right-most table */
101795 cnt = 0;
101796 sqlite3ExprListDelete(db, pFJMatch);
101797 pFJMatch = 0;
101798 }else{
101799 /* For a FULL JOIN, we must construct a coalesce() func */
101800 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
101801 }
101802 }
101803 cnt++;
101804 cntTab = 2;
101805 pMatch = pItem;
101806 pExpr->iColumn = j;
101807 pEList->a[j].fg.bUsed = 1;
101808 hit = 1;
101809 if( pEList->a[j].fg.bUsingTerm ) break;
101810 }
101811 if( hit || zTab==0 ) continue;
101812 }
101813 assert( zDb==0 || zTab!=0 )((void) (0));
101814 if( zTab ){
101815 const char *zTabName;
101816 if( zDb ){
101817 if( pTab->pSchema!=pSchema ) continue;
101818 if( pSchema==0 && strcmp(zDb,"*")!=0 ) continue;
101819 }
101820 zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
101821 assert( zTabName!=0 )((void) (0));
101822 if( sqlite3StrICmp(zTabName, zTab)!=0 ){
101823 continue;
101824 }
101825 assert( ExprUseYTab(pExpr) )((void) (0));
101826 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && pItem->zAlias ){
101827 sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab);
101828 }
101829 }
101830 hCol = sqlite3StrIHash(zCol);
101831 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
101832 if( pCol->hName==hCol
101833 && sqlite3StrICmp(pCol->zCnName, zCol)==0
101834 ){
101835 if( cnt>0 ){
101836 if( pItem->fg.isUsing==0
101837 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
101838 ){
101839 /* Two or more tables have the same column name which is
101840 ** not joined by USING. This is an error. Signal as much
101841 ** by clearing pFJMatch and letting cnt go above 1. */
101842 sqlite3ExprListDelete(db, pFJMatch);
101843 pFJMatch = 0;
101844 }else
101845 if( (pItem->fg.jointype & JT_RIGHT0x10)==0 ){
101846 /* An INNER or LEFT JOIN. Use the left-most table */
101847 continue;
101848 }else
101849 if( (pItem->fg.jointype & JT_LEFT0x08)==0 ){
101850 /* A RIGHT JOIN. Use the right-most table */
101851 cnt = 0;
101852 sqlite3ExprListDelete(db, pFJMatch);
101853 pFJMatch = 0;
101854 }else{
101855 /* For a FULL JOIN, we must construct a coalesce() func */
101856 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
101857 }
101858 }
101859 cnt++;
101860 pMatch = pItem;
101861 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
101862 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
101863 if( pItem->fg.isNestedFrom ){
101864 sqlite3SrcItemColumnUsed(pItem, j);
101865 }
101866 break;
101867 }
101868 }
101869 if( 0==cnt && VisibleRowid(pTab)(((pTab)->tabFlags & 0x00000200)==0) ){
101870 cntTab++;
101871 pMatch = pItem;
101872 }
101873 }
101874 if( pMatch ){
101875 pExpr->iTable = pMatch->iCursor;
101876 assert( ExprUseYTab(pExpr) )((void) (0));
101877 pExpr->y.pTab = pMatch->pTab;
101878 if( (pMatch->fg.jointype & (JT_LEFT0x08|JT_LTORJ0x40))!=0 ){
101879 ExprSetProperty(pExpr, EP_CanBeNull)(pExpr)->flags|=(0x200000);
101880 }
101881 pSchema = pExpr->y.pTab->pSchema;
101882 }
101883 } /* if( pSrcList ) */
101884
101885#if !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT)
101886 /* If we have not already resolved the name, then maybe
101887 ** it is a new.* or old.* trigger argument reference. Or
101888 ** maybe it is an excluded.* from an upsert. Or maybe it is
101889 ** a reference in the RETURNING clause to a table being modified.
101890 */
101891 if( cnt==0 && zDb==0 ){
101892 pTab = 0;
101893#ifndef SQLITE_OMIT_TRIGGER
101894 if( pParse->pTriggerTab!=0 ){
101895 int op = pParse->eTriggerOp;
101896 assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT )((void) (0));
101897 if( pParse->bReturning ){
101898 if( (pNC->ncFlags & NC_UBaseReg0x000400)!=0
101899 && (zTab==0 || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName)==0)
101900 ){
101901 pExpr->iTable = op!=TK_DELETE128;
101902 pTab = pParse->pTriggerTab;
101903 }
101904 }else if( op!=TK_DELETE128 && zTab && sqlite3StrICmp("new",zTab) == 0 ){
101905 pExpr->iTable = 1;
101906 pTab = pParse->pTriggerTab;
101907 }else if( op!=TK_INSERT127 && zTab && sqlite3StrICmp("old",zTab)==0 ){
101908 pExpr->iTable = 0;
101909 pTab = pParse->pTriggerTab;
101910 }
101911 }
101912#endif /* SQLITE_OMIT_TRIGGER */
101913#ifndef SQLITE_OMIT_UPSERT
101914 if( (pNC->ncFlags & NC_UUpsert0x000200)!=0 && zTab!=0 ){
101915 Upsert *pUpsert = pNC->uNC.pUpsert;
101916 if( pUpsert && sqlite3StrICmp("excluded",zTab)==0 ){
101917 pTab = pUpsert->pUpsertSrc->a[0].pTab;
101918 pExpr->iTable = EXCLUDED_TABLE_NUMBER2;
101919 }
101920 }
101921#endif /* SQLITE_OMIT_UPSERT */
101922
101923 if( pTab ){
101924 int iCol;
101925 u8 hCol = sqlite3StrIHash(zCol);
101926 pSchema = pTab->pSchema;
101927 cntTab++;
101928 for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
101929 if( pCol->hName==hCol
101930 && sqlite3StrICmp(pCol->zCnName, zCol)==0
101931 ){
101932 if( iCol==pTab->iPKey ){
101933 iCol = -1;
101934 }
101935 break;
101936 }
101937 }
101938 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab)(((pTab)->tabFlags & 0x00000200)==0) ){
101939 /* IMP: R-51414-32910 */
101940 iCol = -1;
101941 }
101942 if( iCol<pTab->nCol ){
101943 cnt++;
101944 pMatch = 0;
101945#ifndef SQLITE_OMIT_UPSERT
101946 if( pExpr->iTable==EXCLUDED_TABLE_NUMBER2 ){
101947 testcase( iCol==(-1) );
101948 assert( ExprUseYTab(pExpr) )((void) (0));
101949 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
101950 pExpr->iColumn = iCol;
101951 pExpr->y.pTab = pTab;
101952 eNewExprOp = TK_COLUMN167;
101953 }else{
101954 pExpr->iTable = pNC->uNC.pUpsert->regData +
101955 sqlite3TableColumnToStorage(pTab, iCol);
101956 eNewExprOp = TK_REGISTER176;
101957 }
101958 }else
101959#endif /* SQLITE_OMIT_UPSERT */
101960 {
101961 assert( ExprUseYTab(pExpr) )((void) (0));
101962 pExpr->y.pTab = pTab;
101963 if( pParse->bReturning ){
101964 eNewExprOp = TK_REGISTER176;
101965 pExpr->op2 = TK_COLUMN167;
101966 pExpr->iTable = pNC->uNC.iBaseReg + (pTab->nCol+1)*pExpr->iTable +
101967 sqlite3TableColumnToStorage(pTab, iCol) + 1;
101968 }else{
101969 pExpr->iColumn = (i16)iCol;
101970 eNewExprOp = TK_TRIGGER77;
101971#ifndef SQLITE_OMIT_TRIGGER
101972 if( iCol<0 ){
101973 pExpr->affExpr = SQLITE_AFF_INTEGER0x44;
101974 }else if( pExpr->iTable==0 ){
101975 testcase( iCol==31 );
101976 testcase( iCol==32 );
101977 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
101978 }else{
101979 testcase( iCol==31 );
101980 testcase( iCol==32 );
101981 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
101982 }
101983#endif /* SQLITE_OMIT_TRIGGER */
101984 }
101985 }
101986 }
101987 }
101988 }
101989#endif /* !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT) */
101990
101991 /*
101992 ** Perhaps the name is a reference to the ROWID
101993 */
101994 if( cnt==0
101995 && cntTab==1
101996 && pMatch
101997 && (pNC->ncFlags & (NC_IdxExpr0x000020|NC_GenCol0x000008))==0
101998 && sqlite3IsRowid(zCol)
101999 && ALWAYS(VisibleRowid(pMatch->pTab))((((pMatch->pTab)->tabFlags & 0x00000200)==0))
102000 ){
102001 cnt = 1;
102002 pExpr->iColumn = -1;
102003 pExpr->affExpr = SQLITE_AFF_INTEGER0x44;
102004 }
102005
102006 /*
102007 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
102008 ** might refer to an result-set alias. This happens, for example, when
102009 ** we are resolving names in the WHERE clause of the following command:
102010 **
102011 ** SELECT a+b AS x FROM table WHERE x<10;
102012 **
102013 ** In cases like this, replace pExpr with a copy of the expression that
102014 ** forms the result set entry ("a+b" in the example) and return immediately.
102015 ** Note that the expression in the result set should have already been
102016 ** resolved by the time the WHERE clause is resolved.
102017 **
102018 ** The ability to use an output result-set column in the WHERE, GROUP BY,
102019 ** or HAVING clauses, or as part of a larger expression in the ORDER BY
102020 ** clause is not standard SQL. This is a (goofy) SQLite extension, that
102021 ** is supported for backwards compatibility only. Hence, we issue a warning
102022 ** on sqlite3_log() whenever the capability is used.
102023 */
102024 if( cnt==0
102025 && (pNC->ncFlags & NC_UEList0x000080)!=0
102026 && zTab==0
102027 ){
102028 pEList = pNC->uNC.pEList;
102029 assert( pEList!=0 )((void) (0));
102030 for(j=0; j<pEList->nExpr; j++){
102031 char *zAs = pEList->a[j].zEName;
102032 if( pEList->a[j].fg.eEName==ENAME_NAME0
102033 && sqlite3_stricmp(zAs, zCol)==0
102034 ){
102035 Expr *pOrig;
102036 assert( pExpr->pLeft==0 && pExpr->pRight==0 )((void) (0));
102037 assert( ExprUseXList(pExpr)==0 || pExpr->x.pList==0 )((void) (0));
102038 assert( ExprUseXSelect(pExpr)==0 || pExpr->x.pSelect==0 )((void) (0));
102039 pOrig = pEList->a[j].pExpr;
102040 if( (pNC->ncFlags&NC_AllowAgg0x000001)==0 && ExprHasProperty(pOrig, EP_Agg)(((pOrig)->flags&(0x000010))!=0) ){
102041 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
102042 return WRC_Abort2;
102043 }
102044 if( ExprHasProperty(pOrig, EP_Win)(((pOrig)->flags&(0x008000))!=0)
102045 && ((pNC->ncFlags&NC_AllowWin0x004000)==0 || pNC!=pTopNC )
102046 ){
102047 sqlite3ErrorMsg(pParse, "misuse of aliased window function %s",zAs);
102048 return WRC_Abort2;
102049 }
102050 if( sqlite3ExprVectorSize(pOrig)!=1 ){
102051 sqlite3ErrorMsg(pParse, "row value misused");
102052 return WRC_Abort2;
102053 }
102054 resolveAlias(pParse, pEList, j, pExpr, nSubquery);
102055 cnt = 1;
102056 pMatch = 0;
102057 assert( zTab==0 && zDb==0 )((void) (0));
102058 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
102059 sqlite3RenameTokenRemap(pParse, 0, (void*)pExpr);
102060 }
102061 goto lookupname_end;
102062 }
102063 }
102064 }
102065
102066 /* Advance to the next name context. The loop will exit when either
102067 ** we have a match (cnt>0) or when we run out of name contexts.
102068 */
102069 if( cnt ) break;
102070 pNC = pNC->pNext;
102071 nSubquery++;
102072 }while( pNC );
102073
102074
102075 /*
102076 ** If X and Y are NULL (in other words if only the column name Z is
102077 ** supplied) and the value of Z is enclosed in double-quotes, then
102078 ** Z is a string literal if it doesn't match any column names. In that
102079 ** case, we need to return right away and not make any changes to
102080 ** pExpr.
102081 **
102082 ** Because no reference was made to outer contexts, the pNC->nRef
102083 ** fields are not changed in any context.
102084 */
102085 if( cnt==0 && zTab==0 ){
102086 assert( pExpr->op==TK_ID )((void) (0));
102087 if( ExprHasProperty(pExpr,EP_DblQuoted)(((pExpr)->flags&(0x000080))!=0)
102088 && areDoubleQuotedStringsEnabled(db, pTopNC)
102089 ){
102090 /* If a double-quoted identifier does not match any known column name,
102091 ** then treat it as a string.
102092 **
102093 ** This hack was added in the early days of SQLite in a misguided attempt
102094 ** to be compatible with MySQL 3.x, which used double-quotes for strings.
102095 ** I now sorely regret putting in this hack. The effect of this hack is
102096 ** that misspelled identifier names are silently converted into strings
102097 ** rather than causing an error, to the frustration of countless
102098 ** programmers. To all those frustrated programmers, my apologies.
102099 **
102100 ** Someday, I hope to get rid of this hack. Unfortunately there is
102101 ** a huge amount of legacy SQL that uses it. So for now, we just
102102 ** issue a warning.
102103 */
102104 sqlite3_log(SQLITE_WARNING28,
102105 "double-quoted string literal: \"%w\"", zCol);
102106#ifdef SQLITE_ENABLE_NORMALIZE
102107 sqlite3VdbeAddDblquoteStr(db, pParse->pVdbe, zCol);
102108#endif
102109 pExpr->op = TK_STRING117;
102110 memset(&pExpr->y, 0, sizeof(pExpr->y));
102111 return WRC_Prune1;
102112 }
102113 if( sqlite3ExprIdToTrueFalse(pExpr) ){
102114 return WRC_Prune1;
102115 }
102116 }
102117
102118 /*
102119 ** cnt==0 means there was not match.
102120 ** cnt>1 means there were two or more matches.
102121 **
102122 ** cnt==0 is always an error. cnt>1 is often an error, but might
102123 ** be multiple matches for a NATURAL LEFT JOIN or a LEFT JOIN USING.
102124 */
102125 assert( pFJMatch==0 || cnt>0 )((void) (0));
102126 assert( !ExprHasProperty(pExpr, EP_xIsSelect|EP_IntValue) )((void) (0));
102127 if( cnt!=1 ){
102128 const char *zErr;
102129 if( pFJMatch ){
102130 if( pFJMatch->nExpr==cnt-1 ){
102131 if( ExprHasProperty(pExpr,EP_Leaf)(((pExpr)->flags&(0x800000))!=0) ){
102132 ExprClearProperty(pExpr,EP_Leaf)(pExpr)->flags&=~(0x800000);
102133 }else{
102134 sqlite3ExprDelete(db, pExpr->pLeft);
102135 pExpr->pLeft = 0;
102136 sqlite3ExprDelete(db, pExpr->pRight);
102137 pExpr->pRight = 0;
102138 }
102139 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
102140 pExpr->op = TK_FUNCTION172;
102141 pExpr->u.zToken = "coalesce";
102142 pExpr->x.pList = pFJMatch;
102143 cnt = 1;
102144 goto lookupname_end;
102145 }else{
102146 sqlite3ExprListDelete(db, pFJMatch);
102147 pFJMatch = 0;
102148 }
102149 }
102150 zErr = cnt==0 ? "no such column" : "ambiguous column name";
102151 if( zDb ){
102152 sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
102153 }else if( zTab ){
102154 sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
102155 }else{
102156 sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
102157 }
102158 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
102159 pParse->checkSchema = 1;
102160 pTopNC->nNcErr++;
102161 }
102162 assert( pFJMatch==0 )((void) (0));
102163
102164 /* Remove all substructure from pExpr */
102165 if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf))(((pExpr)->flags&((0x010000|0x800000)))!=0) ){
102166 sqlite3ExprDelete(db, pExpr->pLeft);
102167 pExpr->pLeft = 0;
102168 sqlite3ExprDelete(db, pExpr->pRight);
102169 pExpr->pRight = 0;
102170 ExprSetProperty(pExpr, EP_Leaf)(pExpr)->flags|=(0x800000);
102171 }
102172
102173 /* If a column from a table in pSrcList is referenced, then record
102174 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
102175 ** bit 0 to be set. Column 1 sets bit 1. And so forth. Bit 63 is
102176 ** set if the 63rd or any subsequent column is used.
102177 **
102178 ** The colUsed mask is an optimization used to help determine if an
102179 ** index is a covering index. The correct answer is still obtained
102180 ** if the mask contains extra set bits. However, it is important to
102181 ** avoid setting bits beyond the maximum column number of the table.
102182 ** (See ticket [b92e5e8ec2cdbaa1]).
102183 **
102184 ** If a generated column is referenced, set bits for every column
102185 ** of the table.
102186 */
102187 if( pExpr->iColumn>=0 && pMatch!=0 ){
102188 pMatch->colUsed |= sqlite3ExprColUsed(pExpr);
102189 }
102190
102191 pExpr->op = eNewExprOp;
102192lookupname_end:
102193 if( cnt==1 ){
102194 assert( pNC!=0 )((void) (0));
102195#ifndef SQLITE_OMIT_AUTHORIZATION
102196 if( pParse->db->xAuth
102197 && (pExpr->op==TK_COLUMN167 || pExpr->op==TK_TRIGGER77)
102198 ){
102199 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
102200 }
102201#endif
102202 /* Increment the nRef value on all name contexts from TopNC up to
102203 ** the point where the name matched. */
102204 for(;;){
102205 assert( pTopNC!=0 )((void) (0));
102206 pTopNC->nRef++;
102207 if( pTopNC==pNC ) break;
102208 pTopNC = pTopNC->pNext;
102209 }
102210 return WRC_Prune1;
102211 } else {
102212 return WRC_Abort2;
102213 }
102214}
102215
102216/*
102217** Allocate and return a pointer to an expression to load the column iCol
102218** from datasource iSrc in SrcList pSrc.
102219*/
102220SQLITE_PRIVATEstatic Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
102221 Expr *p = sqlite3ExprAlloc(db, TK_COLUMN167, 0, 0);
102222 if( p ){
102223 SrcItem *pItem = &pSrc->a[iSrc];
102224 Table *pTab;
102225 assert( ExprUseYTab(p) )((void) (0));
102226 pTab = p->y.pTab = pItem->pTab;
102227 p->iTable = pItem->iCursor;
102228 if( p->y.pTab->iPKey==iCol ){
102229 p->iColumn = -1;
102230 }else{
102231 p->iColumn = (ynVar)iCol;
102232 if( (pTab->tabFlags & TF_HasGenerated0x00000060)!=0
102233 && (pTab->aCol[iCol].colFlags & COLFLAG_GENERATED0x0060)!=0
102234 ){
102235 testcase( pTab->nCol==63 );
102236 testcase( pTab->nCol==64 );
102237 pItem->colUsed = pTab->nCol>=64 ? ALLBITS((Bitmask)-1) : MASKBIT(pTab->nCol)(((Bitmask)1)<<(pTab->nCol))-1;
102238 }else{
102239 testcase( iCol==BMS );
102240 testcase( iCol==BMS-1 );
102241 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS((int)(sizeof(Bitmask)*8)) ? BMS((int)(sizeof(Bitmask)*8))-1 : iCol);
102242 }
102243 }
102244 }
102245 return p;
102246}
102247
102248/*
102249** Report an error that an expression is not valid for some set of
102250** pNC->ncFlags values determined by validMask.
102251**
102252** static void notValid(
102253** Parse *pParse, // Leave error message here
102254** NameContext *pNC, // The name context
102255** const char *zMsg, // Type of error
102256** int validMask, // Set of contexts for which prohibited
102257** Expr *pExpr // Invalidate this expression on error
102258** ){...}
102259**
102260** As an optimization, since the conditional is almost always false
102261** (because errors are rare), the conditional is moved outside of the
102262** function call using a macro.
102263*/
102264static void notValidImpl(
102265 Parse *pParse, /* Leave error message here */
102266 NameContext *pNC, /* The name context */
102267 const char *zMsg, /* Type of error */
102268 Expr *pExpr, /* Invalidate this expression on error */
102269 Expr *pError /* Associate error with this expression */
102270){
102271 const char *zIn = "partial index WHERE clauses";
102272 if( pNC->ncFlags & NC_IdxExpr0x000020 ) zIn = "index expressions";
102273#ifndef SQLITE_OMIT_CHECK
102274 else if( pNC->ncFlags & NC_IsCheck0x000004 ) zIn = "CHECK constraints";
102275#endif
102276#ifndef SQLITE_OMIT_GENERATED_COLUMNS
102277 else if( pNC->ncFlags & NC_GenCol0x000008 ) zIn = "generated columns";
102278#endif
102279 sqlite3ErrorMsg(pParse, "%s prohibited in %s", zMsg, zIn);
102280 if( pExpr ) pExpr->op = TK_NULL121;
102281 sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);
102282}
102283#define sqlite3ResolveNotValid(P,N,M,X,E,R)((void) (0)); if( ((N)->ncFlags & (X))!=0 ) notValidImpl
(P,N,M,E,R);
\
102284 assert( ((X)&~(NC_IsCheck|NC_PartIdx|NC_IdxExpr|NC_GenCol))==0 )((void) (0)); \
102285 if( ((N)->ncFlags & (X))!=0 ) notValidImpl(P,N,M,E,R);
102286
102287/*
102288** Expression p should encode a floating point value between 1.0 and 0.0.
102289** Return 1024 times this value. Or return -1 if p is not a floating point
102290** value between 1.0 and 0.0.
102291*/
102292static int exprProbability(Expr *p){
102293 double r = -1.0;
102294 if( p->op!=TK_FLOAT153 ) return -1;
102295 assert( !ExprHasProperty(p, EP_IntValue) )((void) (0));
102296 sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF81);
102297 assert( r>=0.0 )((void) (0));
102298 if( r>1.0 ) return -1;
102299 return (int)(r*134217728.0);
102300}
102301
102302/*
102303** This routine is callback for sqlite3WalkExpr().
102304**
102305** Resolve symbolic names into TK_COLUMN operators for the current
102306** node in the expression tree. Return 0 to continue the search down
102307** the tree or 2 to abort the tree walk.
102308**
102309** This routine also does error checking and name resolution for
102310** function names. The operator for aggregate functions is changed
102311** to TK_AGG_FUNCTION.
102312*/
102313static int resolveExprStep(Walker *pWalker, Expr *pExpr){
102314 NameContext *pNC;
102315 Parse *pParse;
102316
102317 pNC = pWalker->u.pNC;
102318 assert( pNC!=0 )((void) (0));
102319 pParse = pNC->pParse;
102320 assert( pParse==pWalker->pParse )((void) (0));
102321
102322#ifndef NDEBUG1
102323 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
102324 SrcList *pSrcList = pNC->pSrcList;
102325 int i;
102326 for(i=0; i<pNC->pSrcList->nSrc; i++){
102327 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab)((void) (0));
102328 }
102329 }
102330#endif
102331 switch( pExpr->op ){
102332
102333 /* The special operator TK_ROW means use the rowid for the first
102334 ** column in the FROM clause. This is used by the LIMIT and ORDER BY
102335 ** clause processing on UPDATE and DELETE statements, and by
102336 ** UPDATE ... FROM statement processing.
102337 */
102338 case TK_ROW75: {
102339 SrcList *pSrcList = pNC->pSrcList;
102340 SrcItem *pItem;
102341 assert( pSrcList && pSrcList->nSrc>=1 )((void) (0));
102342 pItem = pSrcList->a;
102343 pExpr->op = TK_COLUMN167;
102344 assert( ExprUseYTab(pExpr) )((void) (0));
102345 pExpr->y.pTab = pItem->pTab;
102346 pExpr->iTable = pItem->iCursor;
102347 pExpr->iColumn--;
102348 pExpr->affExpr = SQLITE_AFF_INTEGER0x44;
102349 break;
102350 }
102351
102352 /* An optimization: Attempt to convert
102353 **
102354 ** "expr IS NOT NULL" --> "TRUE"
102355 ** "expr IS NULL" --> "FALSE"
102356 **
102357 ** if we can prove that "expr" is never NULL. Call this the
102358 ** "NOT NULL strength reduction optimization".
102359 **
102360 ** If this optimization occurs, also restore the NameContext ref-counts
102361 ** to the state they where in before the "column" LHS expression was
102362 ** resolved. This prevents "column" from being counted as having been
102363 ** referenced, which might prevent a SELECT from being erroneously
102364 ** marked as correlated.
102365 */
102366 case TK_NOTNULL51:
102367 case TK_ISNULL50: {
102368 int anRef[8];
102369 NameContext *p;
102370 int i;
102371 for(i=0, p=pNC; p && i<ArraySize(anRef)((int)(sizeof(anRef)/sizeof(anRef[0]))); p=p->pNext, i++){
102372 anRef[i] = p->nRef;
102373 }
102374 sqlite3WalkExpr(pWalker, pExpr->pLeft);
102375 if( 0==sqlite3ExprCanBeNull(pExpr->pLeft) && !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
102376 testcase( ExprHasProperty(pExpr, EP_OuterON) );
102377 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
102378 if( pExpr->op==TK_NOTNULL51 ){
102379 pExpr->u.zToken = "true";
102380 ExprSetProperty(pExpr, EP_IsTrue)(pExpr)->flags|=(0x10000000);
102381 }else{
102382 pExpr->u.zToken = "false";
102383 ExprSetProperty(pExpr, EP_IsFalse)(pExpr)->flags|=(0x20000000);
102384 }
102385 pExpr->op = TK_TRUEFALSE170;
102386 for(i=0, p=pNC; p && i<ArraySize(anRef)((int)(sizeof(anRef)/sizeof(anRef[0]))); p=p->pNext, i++){
102387 p->nRef = anRef[i];
102388 }
102389 sqlite3ExprDelete(pParse->db, pExpr->pLeft);
102390 pExpr->pLeft = 0;
102391 }
102392 return WRC_Prune1;
102393 }
102394
102395 /* A column name: ID
102396 ** Or table name and column name: ID.ID
102397 ** Or a database, table and column: ID.ID.ID
102398 **
102399 ** The TK_ID and TK_OUT cases are combined so that there will only
102400 ** be one call to lookupName(). Then the compiler will in-line
102401 ** lookupName() for a size reduction and performance increase.
102402 */
102403 case TK_ID59:
102404 case TK_DOT141: {
102405 const char *zColumn;
102406 const char *zTable;
102407 const char *zDb;
102408 Expr *pRight;
102409
102410 if( pExpr->op==TK_ID59 ){
102411 zDb = 0;
102412 zTable = 0;
102413 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
102414 zColumn = pExpr->u.zToken;
102415 }else{
102416 Expr *pLeft = pExpr->pLeft;
102417 testcase( pNC->ncFlags & NC_IdxExpr );
102418 testcase( pNC->ncFlags & NC_GenCol );
102419 sqlite3ResolveNotValid(pParse, pNC, "the \".\" operator",((void) (0)); if( ((pNC)->ncFlags & (0x000020|0x000008
))!=0 ) notValidImpl(pParse,pNC,"the \".\" operator",0,pExpr)
;
102420 NC_IdxExpr|NC_GenCol, 0, pExpr)((void) (0)); if( ((pNC)->ncFlags & (0x000020|0x000008
))!=0 ) notValidImpl(pParse,pNC,"the \".\" operator",0,pExpr)
;
;
102421 pRight = pExpr->pRight;
102422 if( pRight->op==TK_ID59 ){
102423 zDb = 0;
102424 }else{
102425 assert( pRight->op==TK_DOT )((void) (0));
102426 assert( !ExprHasProperty(pRight, EP_IntValue) )((void) (0));
102427 zDb = pLeft->u.zToken;
102428 pLeft = pRight->pLeft;
102429 pRight = pRight->pRight;
102430 }
102431 assert( ExprUseUToken(pLeft) && ExprUseUToken(pRight) )((void) (0));
102432 zTable = pLeft->u.zToken;
102433 zColumn = pRight->u.zToken;
102434 assert( ExprUseYTab(pExpr) )((void) (0));
102435 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
102436 sqlite3RenameTokenRemap(pParse, (void*)pExpr, (void*)pRight);
102437 sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft);
102438 }
102439 }
102440 return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
102441 }
102442
102443 /* Resolve function names
102444 */
102445 case TK_FUNCTION172: {
102446 ExprList *pList = pExpr->x.pList; /* The argument list */
102447 int n = pList ? pList->nExpr : 0; /* Number of arguments */
102448 int no_such_func = 0; /* True if no such function exists */
102449 int wrong_num_args = 0; /* True if wrong number of arguments */
102450 int is_agg = 0; /* True if is an aggregate function */
102451 const char *zId; /* The function name. */
102452 FuncDef *pDef; /* Information about the function */
102453 u8 enc = ENC(pParse->db)((pParse->db)->enc); /* The database encoding */
102454 int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg0x000001 | NC_AllowWin0x004000));
102455#ifndef SQLITE_OMIT_WINDOWFUNC
102456 Window *pWin = (IsWindowFunc(pExpr)( ((((pExpr))->flags&(0x1000000))!=0) && pExpr
->y.pWin->eFrmType!=166 )
? pExpr->y.pWin : 0);
102457#endif
102458 assert( !ExprHasProperty(pExpr, EP_xIsSelect|EP_IntValue) )((void) (0));
102459 zId = pExpr->u.zToken;
102460 pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
102461 if( pDef==0 ){
102462 pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
102463 if( pDef==0 ){
102464 no_such_func = 1;
102465 }else{
102466 wrong_num_args = 1;
102467 }
102468 }else{
102469 is_agg = pDef->xFinalize!=0;
102470 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY0x0400 ){
102471 ExprSetProperty(pExpr, EP_Unlikely)(pExpr)->flags|=(0x080000);
102472 if( n==2 ){
102473 pExpr->iTable = exprProbability(pList->a[1].pExpr);
102474 if( pExpr->iTable<0 ){
102475 sqlite3ErrorMsg(pParse,
102476 "second argument to %#T() must be a "
102477 "constant between 0.0 and 1.0", pExpr);
102478 pNC->nNcErr++;
102479 }
102480 }else{
102481 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
102482 ** equivalent to likelihood(X, 0.0625).
102483 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
102484 ** short-hand for likelihood(X,0.0625).
102485 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
102486 ** for likelihood(X,0.9375).
102487 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
102488 ** to likelihood(X,0.9375). */
102489 /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */
102490 pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
102491 }
102492 }
102493#ifndef SQLITE_OMIT_AUTHORIZATION
102494 {
102495 int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION31, 0,pDef->zName,0);
102496 if( auth!=SQLITE_OK0 ){
102497 if( auth==SQLITE_DENY1 ){
102498 sqlite3ErrorMsg(pParse, "not authorized to use function: %#T",
102499 pExpr);
102500 pNC->nNcErr++;
102501 }
102502 pExpr->op = TK_NULL121;
102503 return WRC_Prune1;
102504 }
102505 }
102506#endif
102507 if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT0x0800|SQLITE_FUNC_SLOCHNG0x2000) ){
102508 /* For the purposes of the EP_ConstFunc flag, date and time
102509 ** functions and other functions that change slowly are considered
102510 ** constant because they are constant for the duration of one query.
102511 ** This allows them to be factored out of inner loops. */
102512 ExprSetProperty(pExpr,EP_ConstFunc)(pExpr)->flags|=(0x100000);
102513 }
102514 if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT0x0800)==0 ){
102515 /* Clearly non-deterministic functions like random(), but also
102516 ** date/time functions that use 'now', and other functions like
102517 ** sqlite_version() that might change over time cannot be used
102518 ** in an index or generated column. Curiously, they can be used
102519 ** in a CHECK constraint. SQLServer, MySQL, and PostgreSQL all
102520 ** all this. */
102521 sqlite3ResolveNotValid(pParse, pNC, "non-deterministic functions",((void) (0)); if( ((pNC)->ncFlags & (0x000020|0x000002
|0x000008))!=0 ) notValidImpl(pParse,pNC,"non-deterministic functions"
,0,pExpr);
102522 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);
;
102523 }else{
102524 assert( (NC_SelfRef & 0xff)==NC_SelfRef )((void) (0)); /* Must fit in 8 bits */
102525 pExpr->op2 = pNC->ncFlags & NC_SelfRef0x00002e;
102526 if( pNC->ncFlags & NC_FromDDL0x040000 ) ExprSetProperty(pExpr, EP_FromDDL)(pExpr)->flags|=(0x40000000);
102527 }
102528 if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL0x00040000)!=0
102529 && pParse->nested==0
102530 && (pParse->db->mDbFlags & DBFLAG_InternalFunc0x0020)==0
102531 ){
102532 /* Internal-use-only functions are disallowed unless the
102533 ** SQL is being compiled using sqlite3NestedParse() or
102534 ** the SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test-control has be
102535 ** used to activate internal functions for testing purposes */
102536 no_such_func = 1;
102537 pDef = 0;
102538 }else
102539 if( (pDef->funcFlags & (SQLITE_FUNC_DIRECT0x00080000|SQLITE_FUNC_UNSAFE0x00200000))!=0
102540 && !IN_RENAME_OBJECT(pParse->eParseMode>=2)
102541 ){
102542 sqlite3ExprFunctionUsable(pParse, pExpr, pDef);
102543 }
102544 }
102545
102546 if( 0==IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
102547#ifndef SQLITE_OMIT_WINDOWFUNC
102548 assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)((void) (0))
102549 || (pDef->xValue==0 && pDef->xInverse==0)((void) (0))
102550 || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)((void) (0))
102551 )((void) (0));
102552 if( pDef && pDef->xValue==0 && pWin ){
102553 sqlite3ErrorMsg(pParse,
102554 "%#T() may not be used as a window function", pExpr
102555 );
102556 pNC->nNcErr++;
102557 }else if(
102558 (is_agg && (pNC->ncFlags & NC_AllowAgg0x000001)==0)
102559 || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW0x00010000) && !pWin)
102560 || (is_agg && pWin && (pNC->ncFlags & NC_AllowWin0x004000)==0)
102561 ){
102562 const char *zType;
102563 if( (pDef->funcFlags & SQLITE_FUNC_WINDOW0x00010000) || pWin ){
102564 zType = "window";
102565 }else{
102566 zType = "aggregate";
102567 }
102568 sqlite3ErrorMsg(pParse, "misuse of %s function %#T()",zType,pExpr);
102569 pNC->nNcErr++;
102570 is_agg = 0;
102571 }
102572#else
102573 if( (is_agg && (pNC->ncFlags & NC_AllowAgg0x000001)==0) ){
102574 sqlite3ErrorMsg(pParse,"misuse of aggregate function %#T()",pExpr);
102575 pNC->nNcErr++;
102576 is_agg = 0;
102577 }
102578#endif
102579 else if( no_such_func && pParse->db->init.busy==0
102580#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION1
102581 && pParse->explain==0
102582#endif
102583 ){
102584 sqlite3ErrorMsg(pParse, "no such function: %#T", pExpr);
102585 pNC->nNcErr++;
102586 }else if( wrong_num_args ){
102587 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %#T()",
102588 pExpr);
102589 pNC->nNcErr++;
102590 }
102591#ifndef SQLITE_OMIT_WINDOWFUNC
102592 else if( is_agg==0 && ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
102593 sqlite3ErrorMsg(pParse,
102594 "FILTER may not be used with non-aggregate %#T()",
102595 pExpr
102596 );
102597 pNC->nNcErr++;
102598 }
102599#endif
102600 if( is_agg ){
102601 /* Window functions may not be arguments of aggregate functions.
102602 ** Or arguments of other window functions. But aggregate functions
102603 ** may be arguments for window functions. */
102604#ifndef SQLITE_OMIT_WINDOWFUNC
102605 pNC->ncFlags &= ~(NC_AllowWin0x004000 | (!pWin ? NC_AllowAgg0x000001 : 0));
102606#else
102607 pNC->ncFlags &= ~NC_AllowAgg0x000001;
102608#endif
102609 }
102610 }
102611#ifndef SQLITE_OMIT_WINDOWFUNC
102612 else if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
102613 is_agg = 1;
102614 }
102615#endif
102616 sqlite3WalkExprList(pWalker, pList);
102617 if( is_agg ){
102618#ifndef SQLITE_OMIT_WINDOWFUNC
102619 if( pWin ){
102620 Select *pSel = pNC->pWinSelect;
102621 assert( pWin==0 || (ExprUseYWin(pExpr) && pWin==pExpr->y.pWin) )((void) (0));
102622 if( IN_RENAME_OBJECT(pParse->eParseMode>=2)==0 ){
102623 sqlite3WindowUpdate(pParse, pSel ? pSel->pWinDefn : 0, pWin, pDef);
102624 if( pParse->db->mallocFailed ) break;
102625 }
102626 sqlite3WalkExprList(pWalker, pWin->pPartition);
102627 sqlite3WalkExprList(pWalker, pWin->pOrderBy);
102628 sqlite3WalkExpr(pWalker, pWin->pFilter);
102629 sqlite3WindowLink(pSel, pWin);
102630 pNC->ncFlags |= NC_HasWin0x008000;
102631 }else
102632#endif /* SQLITE_OMIT_WINDOWFUNC */
102633 {
102634 NameContext *pNC2; /* For looping up thru outer contexts */
102635 pExpr->op = TK_AGG_FUNCTION168;
102636 pExpr->op2 = 0;
102637#ifndef SQLITE_OMIT_WINDOWFUNC
102638 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
102639 sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
102640 }
102641#endif
102642 pNC2 = pNC;
102643 while( pNC2
102644 && sqlite3ReferencesSrcList(pParse, pExpr, pNC2->pSrcList)==0
102645 ){
102646 pExpr->op2++;
102647 pNC2 = pNC2->pNext;
102648 }
102649 assert( pDef!=0 || IN_RENAME_OBJECT )((void) (0));
102650 if( pNC2 && pDef ){
102651 assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg )((void) (0));
102652 assert( SQLITE_FUNC_ANYORDER==NC_OrderAgg )((void) (0));
102653 testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
102654 testcase( (pDef->funcFlags & SQLITE_FUNC_ANYORDER)!=0 );
102655 pNC2->ncFlags |= NC_HasAgg0x000010
102656 | ((pDef->funcFlags^SQLITE_FUNC_ANYORDER0x08000000)
102657 & (SQLITE_FUNC_MINMAX0x1000|SQLITE_FUNC_ANYORDER0x08000000));
102658 }
102659 }
102660 pNC->ncFlags |= savedAllowFlags;
102661 }
102662 /* FIX ME: Compute pExpr->affinity based on the expected return
102663 ** type of the function
102664 */
102665 return WRC_Prune1;
102666 }
102667#ifndef SQLITE_OMIT_SUBQUERY
102668 case TK_SELECT138:
102669 case TK_EXISTS20: testcase( pExpr->op==TK_EXISTS );
102670#endif
102671 case TK_IN49: {
102672 testcase( pExpr->op==TK_IN );
102673 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
102674 int nRef = pNC->nRef;
102675 testcase( pNC->ncFlags & NC_IsCheck );
102676 testcase( pNC->ncFlags & NC_PartIdx );
102677 testcase( pNC->ncFlags & NC_IdxExpr );
102678 testcase( pNC->ncFlags & NC_GenCol );
102679 if( pNC->ncFlags & NC_SelfRef0x00002e ){
102680 notValidImpl(pParse, pNC, "subqueries", pExpr, pExpr);
102681 }else{
102682 sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
102683 }
102684 assert( pNC->nRef>=nRef )((void) (0));
102685 if( nRef!=pNC->nRef ){
102686 ExprSetProperty(pExpr, EP_VarSelect)(pExpr)->flags|=(0x000040);
102687 pNC->ncFlags |= NC_VarSelect0x000040;
102688 }
102689 }
102690 break;
102691 }
102692 case TK_VARIABLE156: {
102693 testcase( pNC->ncFlags & NC_IsCheck );
102694 testcase( pNC->ncFlags & NC_PartIdx );
102695 testcase( pNC->ncFlags & NC_IdxExpr );
102696 testcase( pNC->ncFlags & NC_GenCol );
102697 sqlite3ResolveNotValid(pParse, pNC, "parameters",((void) (0)); if( ((pNC)->ncFlags & (0x000004|0x000002
|0x000020|0x000008))!=0 ) notValidImpl(pParse,pNC,"parameters"
,pExpr,pExpr);
102698 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);
;
102699 break;
102700 }
102701 case TK_IS45:
102702 case TK_ISNOT171: {
102703 Expr *pRight = sqlite3ExprSkipCollateAndLikely(pExpr->pRight);
102704 assert( !ExprHasProperty(pExpr, EP_Reduced) )((void) (0));
102705 /* Handle special cases of "x IS TRUE", "x IS FALSE", "x IS NOT TRUE",
102706 ** and "x IS NOT FALSE". */
102707 if( ALWAYS(pRight)(pRight) && (pRight->op==TK_ID59 || pRight->op==TK_TRUEFALSE170) ){
102708 int rc = resolveExprStep(pWalker, pRight);
102709 if( rc==WRC_Abort2 ) return WRC_Abort2;
102710 if( pRight->op==TK_TRUEFALSE170 ){
102711 pExpr->op2 = pExpr->op;
102712 pExpr->op = TK_TRUTH175;
102713 return WRC_Continue0;
102714 }
102715 }
102716 /* no break */ deliberate_fall_through
102717 }
102718 case TK_BETWEEN48:
102719 case TK_EQ53:
102720 case TK_NE52:
102721 case TK_LT56:
102722 case TK_LE55:
102723 case TK_GT54:
102724 case TK_GE57: {
102725 int nLeft, nRight;
102726 if( pParse->db->mallocFailed ) break;
102727 assert( pExpr->pLeft!=0 )((void) (0));
102728 nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
102729 if( pExpr->op==TK_BETWEEN48 ){
102730 assert( ExprUseXList(pExpr) )((void) (0));
102731 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
102732 if( nRight==nLeft ){
102733 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr);
102734 }
102735 }else{
102736 assert( pExpr->pRight!=0 )((void) (0));
102737 nRight = sqlite3ExprVectorSize(pExpr->pRight);
102738 }
102739 if( nLeft!=nRight ){
102740 testcase( pExpr->op==TK_EQ );
102741 testcase( pExpr->op==TK_NE );
102742 testcase( pExpr->op==TK_LT );
102743 testcase( pExpr->op==TK_LE );
102744 testcase( pExpr->op==TK_GT );
102745 testcase( pExpr->op==TK_GE );
102746 testcase( pExpr->op==TK_IS );
102747 testcase( pExpr->op==TK_ISNOT );
102748 testcase( pExpr->op==TK_BETWEEN );
102749 sqlite3ErrorMsg(pParse, "row value misused");
102750 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
102751 }
102752 break;
102753 }
102754 }
102755 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 )((void) (0));
102756 return pParse->nErr ? WRC_Abort2 : WRC_Continue0;
102757}
102758
102759/*
102760** pEList is a list of expressions which are really the result set of the
102761** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause.
102762** This routine checks to see if pE is a simple identifier which corresponds
102763** to the AS-name of one of the terms of the expression list. If it is,
102764** this routine return an integer between 1 and N where N is the number of
102765** elements in pEList, corresponding to the matching entry. If there is
102766** no match, or if pE is not a simple identifier, then this routine
102767** return 0.
102768**
102769** pEList has been resolved. pE has not.
102770*/
102771static int resolveAsName(
102772 Parse *pParse, /* Parsing context for error messages */
102773 ExprList *pEList, /* List of expressions to scan */
102774 Expr *pE /* Expression we are trying to match */
102775){
102776 int i; /* Loop counter */
102777
102778 UNUSED_PARAMETER(pParse)(void)(pParse);
102779
102780 if( pE->op==TK_ID59 ){
102781 const char *zCol;
102782 assert( !ExprHasProperty(pE, EP_IntValue) )((void) (0));
102783 zCol = pE->u.zToken;
102784 for(i=0; i<pEList->nExpr; i++){
102785 if( pEList->a[i].fg.eEName==ENAME_NAME0
102786 && sqlite3_stricmp(pEList->a[i].zEName, zCol)==0
102787 ){
102788 return i+1;
102789 }
102790 }
102791 }
102792 return 0;
102793}
102794
102795/*
102796** pE is a pointer to an expression which is a single term in the
102797** ORDER BY of a compound SELECT. The expression has not been
102798** name resolved.
102799**
102800** At the point this routine is called, we already know that the
102801** ORDER BY term is not an integer index into the result set. That
102802** case is handled by the calling routine.
102803**
102804** Attempt to match pE against result set columns in the left-most
102805** SELECT statement. Return the index i of the matching column,
102806** as an indication to the caller that it should sort by the i-th column.
102807** The left-most column is 1. In other words, the value returned is the
102808** same integer value that would be used in the SQL statement to indicate
102809** the column.
102810**
102811** If there is no match, return 0. Return -1 if an error occurs.
102812*/
102813static int resolveOrderByTermToExprList(
102814 Parse *pParse, /* Parsing context for error messages */
102815 Select *pSelect, /* The SELECT statement with the ORDER BY clause */
102816 Expr *pE /* The specific ORDER BY term */
102817){
102818 int i; /* Loop counter */
102819 ExprList *pEList; /* The columns of the result set */
102820 NameContext nc; /* Name context for resolving pE */
102821 sqlite3 *db; /* Database connection */
102822 int rc; /* Return code from subprocedures */
102823 u8 savedSuppErr; /* Saved value of db->suppressErr */
102824
102825 assert( sqlite3ExprIsInteger(pE, &i)==0 )((void) (0));
102826 pEList = pSelect->pEList;
102827
102828 /* Resolve all names in the ORDER BY term expression
102829 */
102830 memset(&nc, 0, sizeof(nc));
102831 nc.pParse = pParse;
102832 nc.pSrcList = pSelect->pSrc;
102833 nc.uNC.pEList = pEList;
102834 nc.ncFlags = NC_AllowAgg0x000001|NC_UEList0x000080|NC_NoSelect0x080000;
102835 nc.nNcErr = 0;
102836 db = pParse->db;
102837 savedSuppErr = db->suppressErr;
102838 db->suppressErr = 1;
102839 rc = sqlite3ResolveExprNames(&nc, pE);
102840 db->suppressErr = savedSuppErr;
102841 if( rc ) return 0;
102842
102843 /* Try to match the ORDER BY expression against an expression
102844 ** in the result set. Return an 1-based index of the matching
102845 ** result-set entry.
102846 */
102847 for(i=0; i<pEList->nExpr; i++){
102848 if( sqlite3ExprCompare(0, pEList->a[i].pExpr, pE, -1)<2 ){
102849 return i+1;
102850 }
102851 }
102852
102853 /* If no match, return 0. */
102854 return 0;
102855}
102856
102857/*
102858** Generate an ORDER BY or GROUP BY term out-of-range error.
102859*/
102860static void resolveOutOfRangeError(
102861 Parse *pParse, /* The error context into which to write the error */
102862 const char *zType, /* "ORDER" or "GROUP" */
102863 int i, /* The index (1-based) of the term out of range */
102864 int mx, /* Largest permissible value of i */
102865 Expr *pError /* Associate the error with the expression */
102866){
102867 sqlite3ErrorMsg(pParse,
102868 "%r %s BY term out of range - should be "
102869 "between 1 and %d", i, zType, mx);
102870 sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);
102871}
102872
102873/*
102874** Analyze the ORDER BY clause in a compound SELECT statement. Modify
102875** each term of the ORDER BY clause is a constant integer between 1
102876** and N where N is the number of columns in the compound SELECT.
102877**
102878** ORDER BY terms that are already an integer between 1 and N are
102879** unmodified. ORDER BY terms that are integers outside the range of
102880** 1 through N generate an error. ORDER BY terms that are expressions
102881** are matched against result set expressions of compound SELECT
102882** beginning with the left-most SELECT and working toward the right.
102883** At the first match, the ORDER BY expression is transformed into
102884** the integer column number.
102885**
102886** Return the number of errors seen.
102887*/
102888static int resolveCompoundOrderBy(
102889 Parse *pParse, /* Parsing context. Leave error messages here */
102890 Select *pSelect /* The SELECT statement containing the ORDER BY */
102891){
102892 int i;
102893 ExprList *pOrderBy;
102894 ExprList *pEList;
102895 sqlite3 *db;
102896 int moreToDo = 1;
102897
102898 pOrderBy = pSelect->pOrderBy;
102899 if( pOrderBy==0 ) return 0;
102900 db = pParse->db;
102901 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN2] ){
102902 sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
102903 return 1;
102904 }
102905 for(i=0; i<pOrderBy->nExpr; i++){
102906 pOrderBy->a[i].fg.done = 0;
102907 }
102908 pSelect->pNext = 0;
102909 while( pSelect->pPrior ){
102910 pSelect->pPrior->pNext = pSelect;
102911 pSelect = pSelect->pPrior;
102912 }
102913 while( pSelect && moreToDo ){
102914 struct ExprList_item *pItem;
102915 moreToDo = 0;
102916 pEList = pSelect->pEList;
102917 assert( pEList!=0 )((void) (0));
102918 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
102919 int iCol = -1;
102920 Expr *pE, *pDup;
102921 if( pItem->fg.done ) continue;
102922 pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr);
102923 if( NEVER(pE==0)(pE==0) ) continue;
102924 if( sqlite3ExprIsInteger(pE, &iCol) ){
102925 if( iCol<=0 || iCol>pEList->nExpr ){
102926 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr, pE);
102927 return 1;
102928 }
102929 }else{
102930 iCol = resolveAsName(pParse, pEList, pE);
102931 if( iCol==0 ){
102932 /* Now test if expression pE matches one of the values returned
102933 ** by pSelect. In the usual case this is done by duplicating the
102934 ** expression, resolving any symbols in it, and then comparing
102935 ** it against each expression returned by the SELECT statement.
102936 ** Once the comparisons are finished, the duplicate expression
102937 ** is deleted.
102938 **
102939 ** If this is running as part of an ALTER TABLE operation and
102940 ** the symbols resolve successfully, also resolve the symbols in the
102941 ** actual expression. This allows the code in alter.c to modify
102942 ** column references within the ORDER BY expression as required. */
102943 pDup = sqlite3ExprDup(db, pE, 0);
102944 if( !db->mallocFailed ){
102945 assert(pDup)((void) (0));
102946 iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
102947 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && iCol>0 ){
102948 resolveOrderByTermToExprList(pParse, pSelect, pE);
102949 }
102950 }
102951 sqlite3ExprDelete(db, pDup);
102952 }
102953 }
102954 if( iCol>0 ){
102955 /* Convert the ORDER BY term into an integer column number iCol,
102956 ** taking care to preserve the COLLATE clause if it exists. */
102957 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
102958 Expr *pNew = sqlite3Expr(db, TK_INTEGER155, 0);
102959 if( pNew==0 ) return 1;
102960 pNew->flags |= EP_IntValue0x000800;
102961 pNew->u.iValue = iCol;
102962 if( pItem->pExpr==pE ){
102963 pItem->pExpr = pNew;
102964 }else{
102965 Expr *pParent = pItem->pExpr;
102966 assert( pParent->op==TK_COLLATE )((void) (0));
102967 while( pParent->pLeft->op==TK_COLLATE113 ) pParent = pParent->pLeft;
102968 assert( pParent->pLeft==pE )((void) (0));
102969 pParent->pLeft = pNew;
102970 }
102971 sqlite3ExprDelete(db, pE);
102972 pItem->u.x.iOrderByCol = (u16)iCol;
102973 }
102974 pItem->fg.done = 1;
102975 }else{
102976 moreToDo = 1;
102977 }
102978 }
102979 pSelect = pSelect->pNext;
102980 }
102981 for(i=0; i<pOrderBy->nExpr; i++){
102982 if( pOrderBy->a[i].fg.done==0 ){
102983 sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
102984 "column in the result set", i+1);
102985 return 1;
102986 }
102987 }
102988 return 0;
102989}
102990
102991/*
102992** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
102993** the SELECT statement pSelect. If any term is reference to a
102994** result set expression (as determined by the ExprList.a.u.x.iOrderByCol
102995** field) then convert that term into a copy of the corresponding result set
102996** column.
102997**
102998** If any errors are detected, add an error message to pParse and
102999** return non-zero. Return zero if no errors are seen.
103000*/
103001SQLITE_PRIVATEstatic int sqlite3ResolveOrderGroupBy(
103002 Parse *pParse, /* Parsing context. Leave error messages here */
103003 Select *pSelect, /* The SELECT statement containing the clause */
103004 ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */
103005 const char *zType /* "ORDER" or "GROUP" */
103006){
103007 int i;
103008 sqlite3 *db = pParse->db;
103009 ExprList *pEList;
103010 struct ExprList_item *pItem;
103011
103012 if( pOrderBy==0 || pParse->db->mallocFailed || IN_RENAME_OBJECT(pParse->eParseMode>=2) ) return 0;
103013 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN2] ){
103014 sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
103015 return 1;
103016 }
103017 pEList = pSelect->pEList;
103018 assert( pEList!=0 )((void) (0)); /* sqlite3SelectNew() guarantees this */
103019 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
103020 if( pItem->u.x.iOrderByCol ){
103021 if( pItem->u.x.iOrderByCol>pEList->nExpr ){
103022 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr, 0);
103023 return 1;
103024 }
103025 resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,0);
103026 }
103027 }
103028 return 0;
103029}
103030
103031#ifndef SQLITE_OMIT_WINDOWFUNC
103032/*
103033** Walker callback for windowRemoveExprFromSelect().
103034*/
103035static int resolveRemoveWindowsCb(Walker *pWalker, Expr *pExpr){
103036 UNUSED_PARAMETER(pWalker)(void)(pWalker);
103037 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
103038 Window *pWin = pExpr->y.pWin;
103039 sqlite3WindowUnlinkFromSelect(pWin);
103040 }
103041 return WRC_Continue0;
103042}
103043
103044/*
103045** Remove any Window objects owned by the expression pExpr from the
103046** Select.pWin list of Select object pSelect.
103047*/
103048static void windowRemoveExprFromSelect(Select *pSelect, Expr *pExpr){
103049 if( pSelect->pWin ){
103050 Walker sWalker;
103051 memset(&sWalker, 0, sizeof(Walker));
103052 sWalker.xExprCallback = resolveRemoveWindowsCb;
103053 sWalker.u.pSelect = pSelect;
103054 sqlite3WalkExpr(&sWalker, pExpr);
103055 }
103056}
103057#else
103058# define windowRemoveExprFromSelect(a, b)
103059#endif /* SQLITE_OMIT_WINDOWFUNC */
103060
103061/*
103062** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
103063** The Name context of the SELECT statement is pNC. zType is either
103064** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
103065**
103066** This routine resolves each term of the clause into an expression.
103067** If the order-by term is an integer I between 1 and N (where N is the
103068** number of columns in the result set of the SELECT) then the expression
103069** in the resolution is a copy of the I-th result-set expression. If
103070** the order-by term is an identifier that corresponds to the AS-name of
103071** a result-set expression, then the term resolves to a copy of the
103072** result-set expression. Otherwise, the expression is resolved in
103073** the usual way - using sqlite3ResolveExprNames().
103074**
103075** This routine returns the number of errors. If errors occur, then
103076** an appropriate error message might be left in pParse. (OOM errors
103077** excepted.)
103078*/
103079static int resolveOrderGroupBy(
103080 NameContext *pNC, /* The name context of the SELECT statement */
103081 Select *pSelect, /* The SELECT statement holding pOrderBy */
103082 ExprList *pOrderBy, /* An ORDER BY or GROUP BY clause to resolve */
103083 const char *zType /* Either "ORDER" or "GROUP", as appropriate */
103084){
103085 int i, j; /* Loop counters */
103086 int iCol; /* Column number */
103087 struct ExprList_item *pItem; /* A term of the ORDER BY clause */
103088 Parse *pParse; /* Parsing context */
103089 int nResult; /* Number of terms in the result set */
103090
103091 assert( pOrderBy!=0 )((void) (0));
103092 nResult = pSelect->pEList->nExpr;
103093 pParse = pNC->pParse;
103094 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
103095 Expr *pE = pItem->pExpr;
103096 Expr *pE2 = sqlite3ExprSkipCollateAndLikely(pE);
103097 if( NEVER(pE2==0)(pE2==0) ) continue;
103098 if( zType[0]!='G' ){
103099 iCol = resolveAsName(pParse, pSelect->pEList, pE2);
103100 if( iCol>0 ){
103101 /* If an AS-name match is found, mark this ORDER BY column as being
103102 ** a copy of the iCol-th result-set column. The subsequent call to
103103 ** sqlite3ResolveOrderGroupBy() will convert the expression to a
103104 ** copy of the iCol-th result-set expression. */
103105 pItem->u.x.iOrderByCol = (u16)iCol;
103106 continue;
103107 }
103108 }
103109 if( sqlite3ExprIsInteger(pE2, &iCol) ){
103110 /* The ORDER BY term is an integer constant. Again, set the column
103111 ** number so that sqlite3ResolveOrderGroupBy() will convert the
103112 ** order-by term to a copy of the result-set expression */
103113 if( iCol<1 || iCol>0xffff ){
103114 resolveOutOfRangeError(pParse, zType, i+1, nResult, pE2);
103115 return 1;
103116 }
103117 pItem->u.x.iOrderByCol = (u16)iCol;
103118 continue;
103119 }
103120
103121 /* Otherwise, treat the ORDER BY term as an ordinary expression */
103122 pItem->u.x.iOrderByCol = 0;
103123 if( sqlite3ResolveExprNames(pNC, pE) ){
103124 return 1;
103125 }
103126 for(j=0; j<pSelect->pEList->nExpr; j++){
103127 if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
103128 /* Since this expresion is being changed into a reference
103129 ** to an identical expression in the result set, remove all Window
103130 ** objects belonging to the expression from the Select.pWin list. */
103131 windowRemoveExprFromSelect(pSelect, pE);
103132 pItem->u.x.iOrderByCol = j+1;
103133 }
103134 }
103135 }
103136 return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
103137}
103138
103139/*
103140** Resolve names in the SELECT statement p and all of its descendants.
103141*/
103142static int resolveSelectStep(Walker *pWalker, Select *p){
103143 NameContext *pOuterNC; /* Context that contains this SELECT */
103144 NameContext sNC; /* Name context of this SELECT */
103145 int isCompound; /* True if p is a compound select */
103146 int nCompound; /* Number of compound terms processed so far */
103147 Parse *pParse; /* Parsing context */
103148 int i; /* Loop counter */
103149 ExprList *pGroupBy; /* The GROUP BY clause */
103150 Select *pLeftmost; /* Left-most of SELECT of a compound */
103151 sqlite3 *db; /* Database connection */
103152
103153
103154 assert( p!=0 )((void) (0));
103155 if( p->selFlags & SF_Resolved0x0000004 ){
103156 return WRC_Prune1;
103157 }
103158 pOuterNC = pWalker->u.pNC;
103159 pParse = pWalker->pParse;
103160 db = pParse->db;
103161
103162 /* Normally sqlite3SelectExpand() will be called first and will have
103163 ** already expanded this SELECT. However, if this is a subquery within
103164 ** an expression, sqlite3ResolveExprNames() will be called without a
103165 ** prior call to sqlite3SelectExpand(). When that happens, let
103166 ** sqlite3SelectPrep() do all of the processing for this SELECT.
103167 ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
103168 ** this routine in the correct order.
103169 */
103170 if( (p->selFlags & SF_Expanded0x0000040)==0 ){
103171 sqlite3SelectPrep(pParse, p, pOuterNC);
103172 return pParse->nErr ? WRC_Abort2 : WRC_Prune1;
103173 }
103174
103175 isCompound = p->pPrior!=0;
103176 nCompound = 0;
103177 pLeftmost = p;
103178 while( p ){
103179 assert( (p->selFlags & SF_Expanded)!=0 )((void) (0));
103180 assert( (p->selFlags & SF_Resolved)==0 )((void) (0));
103181 assert( db->suppressErr==0 )((void) (0)); /* SF_Resolved not set if errors suppressed */
103182 p->selFlags |= SF_Resolved0x0000004;
103183
103184
103185 /* Resolve the expressions in the LIMIT and OFFSET clauses. These
103186 ** are not allowed to refer to any names, so pass an empty NameContext.
103187 */
103188 memset(&sNC, 0, sizeof(sNC));
103189 sNC.pParse = pParse;
103190 sNC.pWinSelect = p;
103191 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){
103192 return WRC_Abort2;
103193 }
103194
103195 /* If the SF_Converted flags is set, then this Select object was
103196 ** was created by the convertCompoundSelectToSubquery() function.
103197 ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
103198 ** as if it were part of the sub-query, not the parent. This block
103199 ** moves the pOrderBy down to the sub-query. It will be moved back
103200 ** after the names have been resolved. */
103201 if( p->selFlags & SF_Converted0x0010000 ){
103202 Select *pSub = p->pSrc->a[0].pSelect;
103203 assert( p->pSrc->nSrc==1 && p->pOrderBy )((void) (0));
103204 assert( pSub->pPrior && pSub->pOrderBy==0 )((void) (0));
103205 pSub->pOrderBy = p->pOrderBy;
103206 p->pOrderBy = 0;
103207 }
103208
103209 /* Recursively resolve names in all subqueries in the FROM clause
103210 */
103211 for(i=0; i<p->pSrc->nSrc; i++){
103212 SrcItem *pItem = &p->pSrc->a[i];
103213 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved0x0000004)==0 ){
103214 int nRef = pOuterNC ? pOuterNC->nRef : 0;
103215 const char *zSavedContext = pParse->zAuthContext;
103216
103217 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
103218 sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
103219 pParse->zAuthContext = zSavedContext;
103220 if( pParse->nErr ) return WRC_Abort2;
103221 assert( db->mallocFailed==0 )((void) (0));
103222
103223 /* If the number of references to the outer context changed when
103224 ** expressions in the sub-select were resolved, the sub-select
103225 ** is correlated. It is not required to check the refcount on any
103226 ** but the innermost outer context object, as lookupName() increments
103227 ** the refcount on all contexts between the current one and the
103228 ** context containing the column when it resolves a name. */
103229 if( pOuterNC ){
103230 assert( pItem->fg.isCorrelated==0 && pOuterNC->nRef>=nRef )((void) (0));
103231 pItem->fg.isCorrelated = (pOuterNC->nRef>nRef);
103232 }
103233 }
103234 }
103235
103236 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
103237 ** resolve the result-set expression list.
103238 */
103239 sNC.ncFlags = NC_AllowAgg0x000001|NC_AllowWin0x004000;
103240 sNC.pSrcList = p->pSrc;
103241 sNC.pNext = pOuterNC;
103242
103243 /* Resolve names in the result set. */
103244 if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort2;
103245 sNC.ncFlags &= ~NC_AllowWin0x004000;
103246
103247 /* If there are no aggregate functions in the result-set, and no GROUP BY
103248 ** expression, do not allow aggregates in any of the other expressions.
103249 */
103250 assert( (p->selFlags & SF_Aggregate)==0 )((void) (0));
103251 pGroupBy = p->pGroupBy;
103252 if( pGroupBy || (sNC.ncFlags & NC_HasAgg0x000010)!=0 ){
103253 assert( NC_MinMaxAgg==SF_MinMaxAgg )((void) (0));
103254 assert( NC_OrderAgg==SF_OrderByReqd )((void) (0));
103255 p->selFlags |= SF_Aggregate0x0000008 | (sNC.ncFlags&(NC_MinMaxAgg0x001000|NC_OrderAgg0x8000000));
103256 }else{
103257 sNC.ncFlags &= ~NC_AllowAgg0x000001;
103258 }
103259
103260 /* Add the output column list to the name-context before parsing the
103261 ** other expressions in the SELECT statement. This is so that
103262 ** expressions in the WHERE clause (etc.) can refer to expressions by
103263 ** aliases in the result set.
103264 **
103265 ** Minor point: If this is the case, then the expression will be
103266 ** re-evaluated for each reference to it.
103267 */
103268 assert( (sNC.ncFlags & (NC_UAggInfo|NC_UUpsert|NC_UBaseReg))==0 )((void) (0));
103269 sNC.uNC.pEList = p->pEList;
103270 sNC.ncFlags |= NC_UEList0x000080;
103271 if( p->pHaving ){
103272 if( (p->selFlags & SF_Aggregate0x0000008)==0 ){
103273 sqlite3ErrorMsg(pParse, "HAVING clause on a non-aggregate query");
103274 return WRC_Abort2;
103275 }
103276 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort2;
103277 }
103278 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort2;
103279
103280 /* Resolve names in table-valued-function arguments */
103281 for(i=0; i<p->pSrc->nSrc; i++){
103282 SrcItem *pItem = &p->pSrc->a[i];
103283 if( pItem->fg.isTabFunc
103284 && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
103285 ){
103286 return WRC_Abort2;
103287 }
103288 }
103289
103290#ifndef SQLITE_OMIT_WINDOWFUNC
103291 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
103292 Window *pWin;
103293 for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){
103294 if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy)
103295 || sqlite3ResolveExprListNames(&sNC, pWin->pPartition)
103296 ){
103297 return WRC_Abort2;
103298 }
103299 }
103300 }
103301#endif
103302
103303 /* The ORDER BY and GROUP BY clauses may not refer to terms in
103304 ** outer queries
103305 */
103306 sNC.pNext = 0;
103307 sNC.ncFlags |= NC_AllowAgg0x000001|NC_AllowWin0x004000;
103308
103309 /* If this is a converted compound query, move the ORDER BY clause from
103310 ** the sub-query back to the parent query. At this point each term
103311 ** within the ORDER BY clause has been transformed to an integer value.
103312 ** These integers will be replaced by copies of the corresponding result
103313 ** set expressions by the call to resolveOrderGroupBy() below. */
103314 if( p->selFlags & SF_Converted0x0010000 ){
103315 Select *pSub = p->pSrc->a[0].pSelect;
103316 p->pOrderBy = pSub->pOrderBy;
103317 pSub->pOrderBy = 0;
103318 }
103319
103320 /* Process the ORDER BY clause for singleton SELECT statements.
103321 ** The ORDER BY clause for compounds SELECT statements is handled
103322 ** below, after all of the result-sets for all of the elements of
103323 ** the compound have been resolved.
103324 **
103325 ** If there is an ORDER BY clause on a term of a compound-select other
103326 ** than the right-most term, then that is a syntax error. But the error
103327 ** is not detected until much later, and so we need to go ahead and
103328 ** resolve those symbols on the incorrect ORDER BY for consistency.
103329 */
103330 if( p->pOrderBy!=0
103331 && isCompound<=nCompound /* Defer right-most ORDER BY of a compound */
103332 && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
103333 ){
103334 return WRC_Abort2;
103335 }
103336 if( db->mallocFailed ){
103337 return WRC_Abort2;
103338 }
103339 sNC.ncFlags &= ~NC_AllowWin0x004000;
103340
103341 /* Resolve the GROUP BY clause. At the same time, make sure
103342 ** the GROUP BY clause does not contain aggregate functions.
103343 */
103344 if( pGroupBy ){
103345 struct ExprList_item *pItem;
103346
103347 if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
103348 return WRC_Abort2;
103349 }
103350 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
103351 if( ExprHasProperty(pItem->pExpr, EP_Agg)(((pItem->pExpr)->flags&(0x000010))!=0) ){
103352 sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
103353 "the GROUP BY clause");
103354 return WRC_Abort2;
103355 }
103356 }
103357 }
103358
103359 /* If this is part of a compound SELECT, check that it has the right
103360 ** number of expressions in the select list. */
103361 if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
103362 sqlite3SelectWrongNumTermsError(pParse, p->pNext);
103363 return WRC_Abort2;
103364 }
103365
103366 /* Advance to the next term of the compound
103367 */
103368 p = p->pPrior;
103369 nCompound++;
103370 }
103371
103372 /* Resolve the ORDER BY on a compound SELECT after all terms of
103373 ** the compound have been resolved.
103374 */
103375 if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
103376 return WRC_Abort2;
103377 }
103378
103379 return WRC_Prune1;
103380}
103381
103382/*
103383** This routine walks an expression tree and resolves references to
103384** table columns and result-set columns. At the same time, do error
103385** checking on function usage and set a flag if any aggregate functions
103386** are seen.
103387**
103388** To resolve table columns references we look for nodes (or subtrees) of the
103389** form X.Y.Z or Y.Z or just Z where
103390**
103391** X: The name of a database. Ex: "main" or "temp" or
103392** the symbolic name assigned to an ATTACH-ed database.
103393**
103394** Y: The name of a table in a FROM clause. Or in a trigger
103395** one of the special names "old" or "new".
103396**
103397** Z: The name of a column in table Y.
103398**
103399** The node at the root of the subtree is modified as follows:
103400**
103401** Expr.op Changed to TK_COLUMN
103402** Expr.pTab Points to the Table object for X.Y
103403** Expr.iColumn The column index in X.Y. -1 for the rowid.
103404** Expr.iTable The VDBE cursor number for X.Y
103405**
103406**
103407** To resolve result-set references, look for expression nodes of the
103408** form Z (with no X and Y prefix) where the Z matches the right-hand
103409** size of an AS clause in the result-set of a SELECT. The Z expression
103410** is replaced by a copy of the left-hand side of the result-set expression.
103411** Table-name and function resolution occurs on the substituted expression
103412** tree. For example, in:
103413**
103414** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
103415**
103416** The "x" term of the order by is replaced by "a+b" to render:
103417**
103418** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
103419**
103420** Function calls are checked to make sure that the function is
103421** defined and that the correct number of arguments are specified.
103422** If the function is an aggregate function, then the NC_HasAgg flag is
103423** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
103424** If an expression contains aggregate functions then the EP_Agg
103425** property on the expression is set.
103426**
103427** An error message is left in pParse if anything is amiss. The number
103428** if errors is returned.
103429*/
103430SQLITE_PRIVATEstatic int sqlite3ResolveExprNames(
103431 NameContext *pNC, /* Namespace to resolve expressions in. */
103432 Expr *pExpr /* The expression to be analyzed. */
103433){
103434 int savedHasAgg;
103435 Walker w;
103436
103437 if( pExpr==0 ) return SQLITE_OK0;
103438 savedHasAgg = pNC->ncFlags & (NC_HasAgg0x000010|NC_MinMaxAgg0x001000|NC_HasWin0x008000|NC_OrderAgg0x8000000);
103439 pNC->ncFlags &= ~(NC_HasAgg0x000010|NC_MinMaxAgg0x001000|NC_HasWin0x008000|NC_OrderAgg0x8000000);
103440 w.pParse = pNC->pParse;
103441 w.xExprCallback = resolveExprStep;
103442 w.xSelectCallback = (pNC->ncFlags & NC_NoSelect0x080000) ? 0 : resolveSelectStep;
103443 w.xSelectCallback2 = 0;
103444 w.u.pNC = pNC;
103445#if SQLITE_MAX_EXPR_DEPTH1000>0
103446 w.pParse->nHeight += pExpr->nHeight;
103447 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
103448 return SQLITE_ERROR1;
103449 }
103450#endif
103451 sqlite3WalkExpr(&w, pExpr);
103452#if SQLITE_MAX_EXPR_DEPTH1000>0
103453 w.pParse->nHeight -= pExpr->nHeight;
103454#endif
103455 assert( EP_Agg==NC_HasAgg )((void) (0));
103456 assert( EP_Win==NC_HasWin )((void) (0));
103457 testcase( pNC->ncFlags & NC_HasAgg );
103458 testcase( pNC->ncFlags & NC_HasWin );
103459 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) )(pExpr)->flags|=(pNC->ncFlags & (0x000010|0x008000)
)
;
103460 pNC->ncFlags |= savedHasAgg;
103461 return pNC->nNcErr>0 || w.pParse->nErr>0;
103462}
103463
103464/*
103465** Resolve all names for all expression in an expression list. This is
103466** just like sqlite3ResolveExprNames() except that it works for an expression
103467** list rather than a single expression.
103468*/
103469SQLITE_PRIVATEstatic int sqlite3ResolveExprListNames(
103470 NameContext *pNC, /* Namespace to resolve expressions in. */
103471 ExprList *pList /* The expression list to be analyzed. */
103472){
103473 int i;
103474 int savedHasAgg = 0;
103475 Walker w;
103476 if( pList==0 ) return WRC_Continue0;
103477 w.pParse = pNC->pParse;
103478 w.xExprCallback = resolveExprStep;
103479 w.xSelectCallback = resolveSelectStep;
103480 w.xSelectCallback2 = 0;
103481 w.u.pNC = pNC;
103482 savedHasAgg = pNC->ncFlags & (NC_HasAgg0x000010|NC_MinMaxAgg0x001000|NC_HasWin0x008000|NC_OrderAgg0x8000000);
103483 pNC->ncFlags &= ~(NC_HasAgg0x000010|NC_MinMaxAgg0x001000|NC_HasWin0x008000|NC_OrderAgg0x8000000);
103484 for(i=0; i<pList->nExpr; i++){
103485 Expr *pExpr = pList->a[i].pExpr;
103486 if( pExpr==0 ) continue;
103487#if SQLITE_MAX_EXPR_DEPTH1000>0
103488 w.pParse->nHeight += pExpr->nHeight;
103489 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
103490 return WRC_Abort2;
103491 }
103492#endif
103493 sqlite3WalkExpr(&w, pExpr);
103494#if SQLITE_MAX_EXPR_DEPTH1000>0
103495 w.pParse->nHeight -= pExpr->nHeight;
103496#endif
103497 assert( EP_Agg==NC_HasAgg )((void) (0));
103498 assert( EP_Win==NC_HasWin )((void) (0));
103499 testcase( pNC->ncFlags & NC_HasAgg );
103500 testcase( pNC->ncFlags & NC_HasWin );
103501 if( pNC->ncFlags & (NC_HasAgg0x000010|NC_MinMaxAgg0x001000|NC_HasWin0x008000|NC_OrderAgg0x8000000) ){
103502 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) )(pExpr)->flags|=(pNC->ncFlags & (0x000010|0x008000)
)
;
103503 savedHasAgg |= pNC->ncFlags &
103504 (NC_HasAgg0x000010|NC_MinMaxAgg0x001000|NC_HasWin0x008000|NC_OrderAgg0x8000000);
103505 pNC->ncFlags &= ~(NC_HasAgg0x000010|NC_MinMaxAgg0x001000|NC_HasWin0x008000|NC_OrderAgg0x8000000);
103506 }
103507 if( w.pParse->nErr>0 ) return WRC_Abort2;
103508 }
103509 pNC->ncFlags |= savedHasAgg;
103510 return WRC_Continue0;
103511}
103512
103513/*
103514** Resolve all names in all expressions of a SELECT and in all
103515** decendents of the SELECT, including compounds off of p->pPrior,
103516** subqueries in expressions, and subqueries used as FROM clause
103517** terms.
103518**
103519** See sqlite3ResolveExprNames() for a description of the kinds of
103520** transformations that occur.
103521**
103522** All SELECT statements should have been expanded using
103523** sqlite3SelectExpand() prior to invoking this routine.
103524*/
103525SQLITE_PRIVATEstatic void sqlite3ResolveSelectNames(
103526 Parse *pParse, /* The parser context */
103527 Select *p, /* The SELECT statement being coded. */
103528 NameContext *pOuterNC /* Name context for parent SELECT statement */
103529){
103530 Walker w;
103531
103532 assert( p!=0 )((void) (0));
103533 w.xExprCallback = resolveExprStep;
103534 w.xSelectCallback = resolveSelectStep;
103535 w.xSelectCallback2 = 0;
103536 w.pParse = pParse;
103537 w.u.pNC = pOuterNC;
103538 sqlite3WalkSelect(&w, p);
103539}
103540
103541/*
103542** Resolve names in expressions that can only reference a single table
103543** or which cannot reference any tables at all. Examples:
103544**
103545** "type" flag
103546** ------------
103547** (1) CHECK constraints NC_IsCheck
103548** (2) WHERE clauses on partial indices NC_PartIdx
103549** (3) Expressions in indexes on expressions NC_IdxExpr
103550** (4) Expression arguments to VACUUM INTO. 0
103551** (5) GENERATED ALWAYS as expressions NC_GenCol
103552**
103553** In all cases except (4), the Expr.iTable value for Expr.op==TK_COLUMN
103554** nodes of the expression is set to -1 and the Expr.iColumn value is
103555** set to the column number. In case (4), TK_COLUMN nodes cause an error.
103556**
103557** Any errors cause an error message to be set in pParse.
103558*/
103559SQLITE_PRIVATEstatic int sqlite3ResolveSelfReference(
103560 Parse *pParse, /* Parsing context */
103561 Table *pTab, /* The table being referenced, or NULL */
103562 int type, /* NC_IsCheck, NC_PartIdx, NC_IdxExpr, NC_GenCol, or 0 */
103563 Expr *pExpr, /* Expression to resolve. May be NULL. */
103564 ExprList *pList /* Expression list to resolve. May be NULL. */
103565){
103566 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
103567 NameContext sNC; /* Name context for pParse->pNewTable */
103568 int rc;
103569
103570 assert( type==0 || pTab!=0 )((void) (0));
103571 assert( type==NC_IsCheck || type==NC_PartIdx || type==NC_IdxExpr((void) (0))
103572 || type==NC_GenCol || pTab==0 )((void) (0));
103573 memset(&sNC, 0, sizeof(sNC));
103574 memset(&sSrc, 0, sizeof(sSrc));
103575 if( pTab ){
103576 sSrc.nSrc = 1;
103577 sSrc.a[0].zName = pTab->zName;
103578 sSrc.a[0].pTab = pTab;
103579 sSrc.a[0].iCursor = -1;
103580 if( pTab->pSchema!=pParse->db->aDb[1].pSchema ){
103581 /* Cause EP_FromDDL to be set on TK_FUNCTION nodes of non-TEMP
103582 ** schema elements */
103583 type |= NC_FromDDL0x040000;
103584 }
103585 }
103586 sNC.pParse = pParse;
103587 sNC.pSrcList = &sSrc;
103588 sNC.ncFlags = type | NC_IsDDL0x010000;
103589 if( (rc = sqlite3ResolveExprNames(&sNC, pExpr))!=SQLITE_OK0 ) return rc;
103590 if( pList ) rc = sqlite3ResolveExprListNames(&sNC, pList);
103591 return rc;
103592}
103593
103594/************** End of resolve.c *********************************************/
103595/************** Begin file expr.c ********************************************/
103596/*
103597** 2001 September 15
103598**
103599** The author disclaims copyright to this source code. In place of
103600** a legal notice, here is a blessing:
103601**
103602** May you do good and not evil.
103603** May you find forgiveness for yourself and forgive others.
103604** May you share freely, never taking more than you give.
103605**
103606*************************************************************************
103607** This file contains routines used for analyzing expressions and
103608** for generating VDBE code that evaluates expressions in SQLite.
103609*/
103610/* #include "sqliteInt.h" */
103611
103612/* Forward declarations */
103613static void exprCodeBetween(Parse*,Expr*,int,void(*)(Parse*,Expr*,int,int),int);
103614static int exprCodeVector(Parse *pParse, Expr *p, int *piToFree);
103615
103616/*
103617** Return the affinity character for a single column of a table.
103618*/
103619SQLITE_PRIVATEstatic char sqlite3TableColumnAffinity(const Table *pTab, int iCol){
103620 if( iCol<0 || NEVER(iCol>=pTab->nCol)(iCol>=pTab->nCol) ) return SQLITE_AFF_INTEGER0x44;
103621 return pTab->aCol[iCol].affinity;
103622}
103623
103624/*
103625** Return the 'affinity' of the expression pExpr if any.
103626**
103627** If pExpr is a column, a reference to a column via an 'AS' alias,
103628** or a sub-select with a column as the return value, then the
103629** affinity of that column is returned. Otherwise, 0x00 is returned,
103630** indicating no affinity for the expression.
103631**
103632** i.e. the WHERE clause expressions in the following statements all
103633** have an affinity:
103634**
103635** CREATE TABLE t1(a);
103636** SELECT * FROM t1 WHERE a;
103637** SELECT a AS b FROM t1 WHERE b;
103638** SELECT * FROM t1 WHERE (select a from t1);
103639*/
103640SQLITE_PRIVATEstatic char sqlite3ExprAffinity(const Expr *pExpr){
103641 int op;
103642 while( ExprHasProperty(pExpr, EP_Skip|EP_IfNullRow)(((pExpr)->flags&(0x002000|0x040000))!=0) ){
103643 assert( pExpr->op==TK_COLLATE((void) (0))
103644 || pExpr->op==TK_IF_NULL_ROW((void) (0))
103645 || (pExpr->op==TK_REGISTER && pExpr->op2==TK_IF_NULL_ROW) )((void) (0));
103646 pExpr = pExpr->pLeft;
103647 assert( pExpr!=0 )((void) (0));
103648 }
103649 op = pExpr->op;
103650 if( op==TK_REGISTER176 ) op = pExpr->op2;
103651 if( op==TK_COLUMN167 || op==TK_AGG_COLUMN169 ){
103652 assert( ExprUseYTab(pExpr) )((void) (0));
103653 if( pExpr->y.pTab ){
103654 return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
103655 }
103656 }
103657 if( op==TK_SELECT138 ){
103658 assert( ExprUseXSelect(pExpr) )((void) (0));
103659 assert( pExpr->x.pSelect!=0 )((void) (0));
103660 assert( pExpr->x.pSelect->pEList!=0 )((void) (0));
103661 assert( pExpr->x.pSelect->pEList->a[0].pExpr!=0 )((void) (0));
103662 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
103663 }
103664#ifndef SQLITE_OMIT_CAST
103665 if( op==TK_CAST36 ){
103666 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
103667 return sqlite3AffinityType(pExpr->u.zToken, 0);
103668 }
103669#endif
103670 if( op==TK_SELECT_COLUMN178 ){
103671 assert( pExpr->pLeft!=0 && ExprUseXSelect(pExpr->pLeft) )((void) (0));
103672 assert( pExpr->iColumn < pExpr->iTable )((void) (0));
103673 assert( pExpr->iTable==pExpr->pLeft->x.pSelect->pEList->nExpr )((void) (0));
103674 return sqlite3ExprAffinity(
103675 pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
103676 );
103677 }
103678 if( op==TK_VECTOR177 ){
103679 assert( ExprUseXList(pExpr) )((void) (0));
103680 return sqlite3ExprAffinity(pExpr->x.pList->a[0].pExpr);
103681 }
103682 return pExpr->affExpr;
103683}
103684
103685/*
103686** Set the collating sequence for expression pExpr to be the collating
103687** sequence named by pToken. Return a pointer to a new Expr node that
103688** implements the COLLATE operator.
103689**
103690** If a memory allocation error occurs, that fact is recorded in pParse->db
103691** and the pExpr parameter is returned unchanged.
103692*/
103693SQLITE_PRIVATEstatic Expr *sqlite3ExprAddCollateToken(
103694 const Parse *pParse, /* Parsing context */
103695 Expr *pExpr, /* Add the "COLLATE" clause to this expression */
103696 const Token *pCollName, /* Name of collating sequence */
103697 int dequote /* True to dequote pCollName */
103698){
103699 if( pCollName->n>0 ){
103700 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE113, pCollName, dequote);
103701 if( pNew ){
103702 pNew->pLeft = pExpr;
103703 pNew->flags |= EP_Collate0x000200|EP_Skip0x002000;
103704 pExpr = pNew;
103705 }
103706 }
103707 return pExpr;
103708}
103709SQLITE_PRIVATEstatic Expr *sqlite3ExprAddCollateString(
103710 const Parse *pParse, /* Parsing context */
103711 Expr *pExpr, /* Add the "COLLATE" clause to this expression */
103712 const char *zC /* The collating sequence name */
103713){
103714 Token s;
103715 assert( zC!=0 )((void) (0));
103716 sqlite3TokenInit(&s, (char*)zC);
103717 return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0);
103718}
103719
103720/*
103721** Skip over any TK_COLLATE operators.
103722*/
103723SQLITE_PRIVATEstatic Expr *sqlite3ExprSkipCollate(Expr *pExpr){
103724 while( pExpr && ExprHasProperty(pExpr, EP_Skip)(((pExpr)->flags&(0x002000))!=0) ){
103725 assert( pExpr->op==TK_COLLATE )((void) (0));
103726 pExpr = pExpr->pLeft;
103727 }
103728 return pExpr;
103729}
103730
103731/*
103732** Skip over any TK_COLLATE operators and/or any unlikely()
103733** or likelihood() or likely() functions at the root of an
103734** expression.
103735*/
103736SQLITE_PRIVATEstatic Expr *sqlite3ExprSkipCollateAndLikely(Expr *pExpr){
103737 while( pExpr && ExprHasProperty(pExpr, EP_Skip|EP_Unlikely)(((pExpr)->flags&(0x002000|0x080000))!=0) ){
103738 if( ExprHasProperty(pExpr, EP_Unlikely)(((pExpr)->flags&(0x080000))!=0) ){
103739 assert( ExprUseXList(pExpr) )((void) (0));
103740 assert( pExpr->x.pList->nExpr>0 )((void) (0));
103741 assert( pExpr->op==TK_FUNCTION )((void) (0));
103742 pExpr = pExpr->x.pList->a[0].pExpr;
103743 }else{
103744 assert( pExpr->op==TK_COLLATE )((void) (0));
103745 pExpr = pExpr->pLeft;
103746 }
103747 }
103748 return pExpr;
103749}
103750
103751/*
103752** Return the collation sequence for the expression pExpr. If
103753** there is no defined collating sequence, return NULL.
103754**
103755** See also: sqlite3ExprNNCollSeq()
103756**
103757** The sqlite3ExprNNCollSeq() works the same exact that it returns the
103758** default collation if pExpr has no defined collation.
103759**
103760** The collating sequence might be determined by a COLLATE operator
103761** or by the presence of a column with a defined collating sequence.
103762** COLLATE operators take first precedence. Left operands take
103763** precedence over right operands.
103764*/
103765SQLITE_PRIVATEstatic CollSeq *sqlite3ExprCollSeq(Parse *pParse, const Expr *pExpr){
103766 sqlite3 *db = pParse->db;
103767 CollSeq *pColl = 0;
103768 const Expr *p = pExpr;
103769 while( p ){
103770 int op = p->op;
103771 if( op==TK_REGISTER176 ) op = p->op2;
103772 if( op==TK_AGG_COLUMN169 || op==TK_COLUMN167 || op==TK_TRIGGER77 ){
103773 assert( ExprUseYTab(p) )((void) (0));
103774 if( p->y.pTab!=0 ){
103775 /* op==TK_REGISTER && p->y.pTab!=0 happens when pExpr was originally
103776 ** a TK_COLUMN but was previously evaluated and cached in a register */
103777 int j = p->iColumn;
103778 if( j>=0 ){
103779 const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]);
103780 pColl = sqlite3FindCollSeq(db, ENC(db)((db)->enc), zColl, 0);
103781 }
103782 break;
103783 }
103784 }
103785 if( op==TK_CAST36 || op==TK_UPLUS174 ){
103786 p = p->pLeft;
103787 continue;
103788 }
103789 if( op==TK_VECTOR177 ){
103790 assert( ExprUseXList(p) )((void) (0));
103791 p = p->x.pList->a[0].pExpr;
103792 continue;
103793 }
103794 if( op==TK_COLLATE113 ){
103795 assert( !ExprHasProperty(p, EP_IntValue) )((void) (0));
103796 pColl = sqlite3GetCollSeq(pParse, ENC(db)((db)->enc), 0, p->u.zToken);
103797 break;
103798 }
103799 if( p->flags & EP_Collate0x000200 ){
103800 if( p->pLeft && (p->pLeft->flags & EP_Collate0x000200)!=0 ){
103801 p = p->pLeft;
103802 }else{
103803 Expr *pNext = p->pRight;
103804 /* The Expr.x union is never used at the same time as Expr.pRight */
103805 assert( ExprUseXList(p) )((void) (0));
103806 assert( p->x.pList==0 || p->pRight==0 )((void) (0));
103807 if( p->x.pList!=0 && !db->mallocFailed ){
103808 int i;
103809 for(i=0; ALWAYS(i<p->x.pList->nExpr)(i<p->x.pList->nExpr); i++){
103810 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate)(((p->x.pList->a[i].pExpr)->flags&(0x000200))!=0
)
){
103811 pNext = p->x.pList->a[i].pExpr;
103812 break;
103813 }
103814 }
103815 }
103816 p = pNext;
103817 }
103818 }else{
103819 break;
103820 }
103821 }
103822 if( sqlite3CheckCollSeq(pParse, pColl) ){
103823 pColl = 0;
103824 }
103825 return pColl;
103826}
103827
103828/*
103829** Return the collation sequence for the expression pExpr. If
103830** there is no defined collating sequence, return a pointer to the
103831** defautl collation sequence.
103832**
103833** See also: sqlite3ExprCollSeq()
103834**
103835** The sqlite3ExprCollSeq() routine works the same except that it
103836** returns NULL if there is no defined collation.
103837*/
103838SQLITE_PRIVATEstatic CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, const Expr *pExpr){
103839 CollSeq *p = sqlite3ExprCollSeq(pParse, pExpr);
103840 if( p==0 ) p = pParse->db->pDfltColl;
103841 assert( p!=0 )((void) (0));
103842 return p;
103843}
103844
103845/*
103846** Return TRUE if the two expressions have equivalent collating sequences.
103847*/
103848SQLITE_PRIVATEstatic int sqlite3ExprCollSeqMatch(Parse *pParse, const Expr *pE1, const Expr *pE2){
103849 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pE1);
103850 CollSeq *pColl2 = sqlite3ExprNNCollSeq(pParse, pE2);
103851 return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0;
103852}
103853
103854/*
103855** pExpr is an operand of a comparison operator. aff2 is the
103856** type affinity of the other operand. This routine returns the
103857** type affinity that should be used for the comparison operator.
103858*/
103859SQLITE_PRIVATEstatic char sqlite3CompareAffinity(const Expr *pExpr, char aff2){
103860 char aff1 = sqlite3ExprAffinity(pExpr);
103861 if( aff1>SQLITE_AFF_NONE0x40 && aff2>SQLITE_AFF_NONE0x40 ){
103862 /* Both sides of the comparison are columns. If one has numeric
103863 ** affinity, use that. Otherwise use no affinity.
103864 */
103865 if( sqlite3IsNumericAffinity(aff1)((aff1)>=0x43) || sqlite3IsNumericAffinity(aff2)((aff2)>=0x43) ){
103866 return SQLITE_AFF_NUMERIC0x43;
103867 }else{
103868 return SQLITE_AFF_BLOB0x41;
103869 }
103870 }else{
103871 /* One side is a column, the other is not. Use the columns affinity. */
103872 assert( aff1<=SQLITE_AFF_NONE || aff2<=SQLITE_AFF_NONE )((void) (0));
103873 return (aff1<=SQLITE_AFF_NONE0x40 ? aff2 : aff1) | SQLITE_AFF_NONE0x40;
103874 }
103875}
103876
103877/*
103878** pExpr is a comparison operator. Return the type affinity that should
103879** be applied to both operands prior to doing the comparison.
103880*/
103881static char comparisonAffinity(const Expr *pExpr){
103882 char aff;
103883 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||((void) (0))
103884 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||((void) (0))
103885 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT )((void) (0));
103886 assert( pExpr->pLeft )((void) (0));
103887 aff = sqlite3ExprAffinity(pExpr->pLeft);
103888 if( pExpr->pRight ){
103889 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
103890 }else if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
103891 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
103892 }else if( aff==0 ){
103893 aff = SQLITE_AFF_BLOB0x41;
103894 }
103895 return aff;
103896}
103897
103898/*
103899** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
103900** idx_affinity is the affinity of an indexed column. Return true
103901** if the index with affinity idx_affinity may be used to implement
103902** the comparison in pExpr.
103903*/
103904SQLITE_PRIVATEstatic int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity){
103905 char aff = comparisonAffinity(pExpr);
103906 if( aff<SQLITE_AFF_TEXT0x42 ){
103907 return 1;
103908 }
103909 if( aff==SQLITE_AFF_TEXT0x42 ){
103910 return idx_affinity==SQLITE_AFF_TEXT0x42;
103911 }
103912 return sqlite3IsNumericAffinity(idx_affinity)((idx_affinity)>=0x43);
103913}
103914
103915/*
103916** Return the P5 value that should be used for a binary comparison
103917** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
103918*/
103919static u8 binaryCompareP5(
103920 const Expr *pExpr1, /* Left operand */
103921 const Expr *pExpr2, /* Right operand */
103922 int jumpIfNull /* Extra flags added to P5 */
103923){
103924 u8 aff = (char)sqlite3ExprAffinity(pExpr2);
103925 aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
103926 return aff;
103927}
103928
103929/*
103930** Return a pointer to the collation sequence that should be used by
103931** a binary comparison operator comparing pLeft and pRight.
103932**
103933** If the left hand expression has a collating sequence type, then it is
103934** used. Otherwise the collation sequence for the right hand expression
103935** is used, or the default (BINARY) if neither expression has a collating
103936** type.
103937**
103938** Argument pRight (but not pLeft) may be a null pointer. In this case,
103939** it is not considered.
103940*/
103941SQLITE_PRIVATEstatic CollSeq *sqlite3BinaryCompareCollSeq(
103942 Parse *pParse,
103943 const Expr *pLeft,
103944 const Expr *pRight
103945){
103946 CollSeq *pColl;
103947 assert( pLeft )((void) (0));
103948 if( pLeft->flags & EP_Collate0x000200 ){
103949 pColl = sqlite3ExprCollSeq(pParse, pLeft);
103950 }else if( pRight && (pRight->flags & EP_Collate0x000200)!=0 ){
103951 pColl = sqlite3ExprCollSeq(pParse, pRight);
103952 }else{
103953 pColl = sqlite3ExprCollSeq(pParse, pLeft);
103954 if( !pColl ){
103955 pColl = sqlite3ExprCollSeq(pParse, pRight);
103956 }
103957 }
103958 return pColl;
103959}
103960
103961/* Expresssion p is a comparison operator. Return a collation sequence
103962** appropriate for the comparison operator.
103963**
103964** This is normally just a wrapper around sqlite3BinaryCompareCollSeq().
103965** However, if the OP_Commuted flag is set, then the order of the operands
103966** is reversed in the sqlite3BinaryCompareCollSeq() call so that the
103967** correct collating sequence is found.
103968*/
103969SQLITE_PRIVATEstatic CollSeq *sqlite3ExprCompareCollSeq(Parse *pParse, const Expr *p){
103970 if( ExprHasProperty(p, EP_Commuted)(((p)->flags&(0x000400))!=0) ){
103971 return sqlite3BinaryCompareCollSeq(pParse, p->pRight, p->pLeft);
103972 }else{
103973 return sqlite3BinaryCompareCollSeq(pParse, p->pLeft, p->pRight);
103974 }
103975}
103976
103977/*
103978** Generate code for a comparison operator.
103979*/
103980static int codeCompare(
103981 Parse *pParse, /* The parsing (and code generating) context */
103982 Expr *pLeft, /* The left operand */
103983 Expr *pRight, /* The right operand */
103984 int opcode, /* The comparison opcode */
103985 int in1, int in2, /* Register holding operands */
103986 int dest, /* Jump here if true. */
103987 int jumpIfNull, /* If true, jump if either operand is NULL */
103988 int isCommuted /* The comparison has been commuted */
103989){
103990 int p5;
103991 int addr;
103992 CollSeq *p4;
103993
103994 if( pParse->nErr ) return 0;
103995 if( isCommuted ){
103996 p4 = sqlite3BinaryCompareCollSeq(pParse, pRight, pLeft);
103997 }else{
103998 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
103999 }
104000 p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
104001 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
104002 (void*)p4, P4_COLLSEQ(-2));
104003 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
104004 return addr;
104005}
104006
104007/*
104008** Return true if expression pExpr is a vector, or false otherwise.
104009**
104010** A vector is defined as any expression that results in two or more
104011** columns of result. Every TK_VECTOR node is an vector because the
104012** parser will not generate a TK_VECTOR with fewer than two entries.
104013** But a TK_SELECT might be either a vector or a scalar. It is only
104014** considered a vector if it has two or more result columns.
104015*/
104016SQLITE_PRIVATEstatic int sqlite3ExprIsVector(const Expr *pExpr){
104017 return sqlite3ExprVectorSize(pExpr)>1;
104018}
104019
104020/*
104021** If the expression passed as the only argument is of type TK_VECTOR
104022** return the number of expressions in the vector. Or, if the expression
104023** is a sub-select, return the number of columns in the sub-select. For
104024** any other type of expression, return 1.
104025*/
104026SQLITE_PRIVATEstatic int sqlite3ExprVectorSize(const Expr *pExpr){
104027 u8 op = pExpr->op;
104028 if( op==TK_REGISTER176 ) op = pExpr->op2;
104029 if( op==TK_VECTOR177 ){
104030 assert( ExprUseXList(pExpr) )((void) (0));
104031 return pExpr->x.pList->nExpr;
104032 }else if( op==TK_SELECT138 ){
104033 assert( ExprUseXSelect(pExpr) )((void) (0));
104034 return pExpr->x.pSelect->pEList->nExpr;
104035 }else{
104036 return 1;
104037 }
104038}
104039
104040/*
104041** Return a pointer to a subexpression of pVector that is the i-th
104042** column of the vector (numbered starting with 0). The caller must
104043** ensure that i is within range.
104044**
104045** If pVector is really a scalar (and "scalar" here includes subqueries
104046** that return a single column!) then return pVector unmodified.
104047**
104048** pVector retains ownership of the returned subexpression.
104049**
104050** If the vector is a (SELECT ...) then the expression returned is
104051** just the expression for the i-th term of the result set, and may
104052** not be ready for evaluation because the table cursor has not yet
104053** been positioned.
104054*/
104055SQLITE_PRIVATEstatic Expr *sqlite3VectorFieldSubexpr(Expr *pVector, int i){
104056 assert( i<sqlite3ExprVectorSize(pVector) || pVector->op==TK_ERROR )((void) (0));
104057 if( sqlite3ExprIsVector(pVector) ){
104058 assert( pVector->op2==0 || pVector->op==TK_REGISTER )((void) (0));
104059 if( pVector->op==TK_SELECT138 || pVector->op2==TK_SELECT138 ){
104060 assert( ExprUseXSelect(pVector) )((void) (0));
104061 return pVector->x.pSelect->pEList->a[i].pExpr;
104062 }else{
104063 assert( ExprUseXList(pVector) )((void) (0));
104064 return pVector->x.pList->a[i].pExpr;
104065 }
104066 }
104067 return pVector;
104068}
104069
104070/*
104071** Compute and return a new Expr object which when passed to
104072** sqlite3ExprCode() will generate all necessary code to compute
104073** the iField-th column of the vector expression pVector.
104074**
104075** It is ok for pVector to be a scalar (as long as iField==0).
104076** In that case, this routine works like sqlite3ExprDup().
104077**
104078** The caller owns the returned Expr object and is responsible for
104079** ensuring that the returned value eventually gets freed.
104080**
104081** The caller retains ownership of pVector. If pVector is a TK_SELECT,
104082** then the returned object will reference pVector and so pVector must remain
104083** valid for the life of the returned object. If pVector is a TK_VECTOR
104084** or a scalar expression, then it can be deleted as soon as this routine
104085** returns.
104086**
104087** A trick to cause a TK_SELECT pVector to be deleted together with
104088** the returned Expr object is to attach the pVector to the pRight field
104089** of the returned TK_SELECT_COLUMN Expr object.
104090*/
104091SQLITE_PRIVATEstatic Expr *sqlite3ExprForVectorField(
104092 Parse *pParse, /* Parsing context */
104093 Expr *pVector, /* The vector. List of expressions or a sub-SELECT */
104094 int iField, /* Which column of the vector to return */
104095 int nField /* Total number of columns in the vector */
104096){
104097 Expr *pRet;
104098 if( pVector->op==TK_SELECT138 ){
104099 assert( ExprUseXSelect(pVector) )((void) (0));
104100 /* The TK_SELECT_COLUMN Expr node:
104101 **
104102 ** pLeft: pVector containing TK_SELECT. Not deleted.
104103 ** pRight: not used. But recursively deleted.
104104 ** iColumn: Index of a column in pVector
104105 ** iTable: 0 or the number of columns on the LHS of an assignment
104106 ** pLeft->iTable: First in an array of register holding result, or 0
104107 ** if the result is not yet computed.
104108 **
104109 ** sqlite3ExprDelete() specifically skips the recursive delete of
104110 ** pLeft on TK_SELECT_COLUMN nodes. But pRight is followed, so pVector
104111 ** can be attached to pRight to cause this node to take ownership of
104112 ** pVector. Typically there will be multiple TK_SELECT_COLUMN nodes
104113 ** with the same pLeft pointer to the pVector, but only one of them
104114 ** will own the pVector.
104115 */
104116 pRet = sqlite3PExpr(pParse, TK_SELECT_COLUMN178, 0, 0);
104117 if( pRet ){
104118 pRet->iTable = nField;
104119 pRet->iColumn = iField;
104120 pRet->pLeft = pVector;
104121 }
104122 }else{
104123 if( pVector->op==TK_VECTOR177 ){
104124 Expr **ppVector;
104125 assert( ExprUseXList(pVector) )((void) (0));
104126 ppVector = &pVector->x.pList->a[iField].pExpr;
104127 pVector = *ppVector;
104128 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
104129 /* This must be a vector UPDATE inside a trigger */
104130 *ppVector = 0;
104131 return pVector;
104132 }
104133 }
104134 pRet = sqlite3ExprDup(pParse->db, pVector, 0);
104135 }
104136 return pRet;
104137}
104138
104139/*
104140** If expression pExpr is of type TK_SELECT, generate code to evaluate
104141** it. Return the register in which the result is stored (or, if the
104142** sub-select returns more than one column, the first in an array
104143** of registers in which the result is stored).
104144**
104145** If pExpr is not a TK_SELECT expression, return 0.
104146*/
104147static int exprCodeSubselect(Parse *pParse, Expr *pExpr){
104148 int reg = 0;
104149#ifndef SQLITE_OMIT_SUBQUERY
104150 if( pExpr->op==TK_SELECT138 ){
104151 reg = sqlite3CodeSubselect(pParse, pExpr);
104152 }
104153#endif
104154 return reg;
104155}
104156
104157/*
104158** Argument pVector points to a vector expression - either a TK_VECTOR
104159** or TK_SELECT that returns more than one column. This function returns
104160** the register number of a register that contains the value of
104161** element iField of the vector.
104162**
104163** If pVector is a TK_SELECT expression, then code for it must have
104164** already been generated using the exprCodeSubselect() routine. In this
104165** case parameter regSelect should be the first in an array of registers
104166** containing the results of the sub-select.
104167**
104168** If pVector is of type TK_VECTOR, then code for the requested field
104169** is generated. In this case (*pRegFree) may be set to the number of
104170** a temporary register to be freed by the caller before returning.
104171**
104172** Before returning, output parameter (*ppExpr) is set to point to the
104173** Expr object corresponding to element iElem of the vector.
104174*/
104175static int exprVectorRegister(
104176 Parse *pParse, /* Parse context */
104177 Expr *pVector, /* Vector to extract element from */
104178 int iField, /* Field to extract from pVector */
104179 int regSelect, /* First in array of registers */
104180 Expr **ppExpr, /* OUT: Expression element */
104181 int *pRegFree /* OUT: Temp register to free */
104182){
104183 u8 op = pVector->op;
104184 assert( op==TK_VECTOR || op==TK_REGISTER || op==TK_SELECT || op==TK_ERROR )((void) (0));
104185 if( op==TK_REGISTER176 ){
104186 *ppExpr = sqlite3VectorFieldSubexpr(pVector, iField);
104187 return pVector->iTable+iField;
104188 }
104189 if( op==TK_SELECT138 ){
104190 assert( ExprUseXSelect(pVector) )((void) (0));
104191 *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
104192 return regSelect+iField;
104193 }
104194 if( op==TK_VECTOR177 ){
104195 assert( ExprUseXList(pVector) )((void) (0));
104196 *ppExpr = pVector->x.pList->a[iField].pExpr;
104197 return sqlite3ExprCodeTemp(pParse, *ppExpr, pRegFree);
104198 }
104199 return 0;
104200}
104201
104202/*
104203** Expression pExpr is a comparison between two vector values. Compute
104204** the result of the comparison (1, 0, or NULL) and write that
104205** result into register dest.
104206**
104207** The caller must satisfy the following preconditions:
104208**
104209** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ
104210** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ
104211** otherwise: op==pExpr->op and p5==0
104212*/
104213static void codeVectorCompare(
104214 Parse *pParse, /* Code generator context */
104215 Expr *pExpr, /* The comparison operation */
104216 int dest, /* Write results into this register */
104217 u8 op, /* Comparison operator */
104218 u8 p5 /* SQLITE_NULLEQ or zero */
104219){
104220 Vdbe *v = pParse->pVdbe;
104221 Expr *pLeft = pExpr->pLeft;
104222 Expr *pRight = pExpr->pRight;
104223 int nLeft = sqlite3ExprVectorSize(pLeft);
104224 int i;
104225 int regLeft = 0;
104226 int regRight = 0;
104227 u8 opx = op;
104228 int addrCmp = 0;
104229 int addrDone = sqlite3VdbeMakeLabel(pParse);
104230 int isCommuted = ExprHasProperty(pExpr,EP_Commuted)(((pExpr)->flags&(0x000400))!=0);
104231
104232 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) )((void) (0));
104233 if( pParse->nErr ) return;
104234 if( nLeft!=sqlite3ExprVectorSize(pRight) ){
104235 sqlite3ErrorMsg(pParse, "row value misused");
104236 return;
104237 }
104238 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE((void) (0))
104239 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT((void) (0))
104240 || pExpr->op==TK_LT || pExpr->op==TK_GT((void) (0))
104241 || pExpr->op==TK_LE || pExpr->op==TK_GE((void) (0))
104242 )((void) (0));
104243 assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)((void) (0))
104244 || (pExpr->op==TK_ISNOT && op==TK_NE) )((void) (0));
104245 assert( p5==0 || pExpr->op!=op )((void) (0));
104246 assert( p5==SQLITE_NULLEQ || pExpr->op==op )((void) (0));
104247
104248 if( op==TK_LE55 ) opx = TK_LT56;
104249 if( op==TK_GE57 ) opx = TK_GT54;
104250 if( op==TK_NE52 ) opx = TK_EQ53;
104251
104252 regLeft = exprCodeSubselect(pParse, pLeft);
104253 regRight = exprCodeSubselect(pParse, pRight);
104254
104255 sqlite3VdbeAddOp2(v, OP_Integer71, 1, dest);
104256 for(i=0; 1 /*Loop exits by "break"*/; i++){
104257 int regFree1 = 0, regFree2 = 0;
104258 Expr *pL = 0, *pR = 0;
104259 int r1, r2;
104260 assert( i>=0 && i<nLeft )((void) (0));
104261 if( addrCmp ) sqlite3VdbeJumpHere(v, addrCmp);
104262 r1 = exprVectorRegister(pParse, pLeft, i, regLeft, &pL, &regFree1);
104263 r2 = exprVectorRegister(pParse, pRight, i, regRight, &pR, &regFree2);
104264 addrCmp = sqlite3VdbeCurrentAddr(v);
104265 codeCompare(pParse, pL, pR, opx, r1, r2, addrDone, p5, isCommuted);
104266 testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
104267 testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
104268 testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
104269 testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
104270 testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
104271 testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
104272 sqlite3ReleaseTempReg(pParse, regFree1);
104273 sqlite3ReleaseTempReg(pParse, regFree2);
104274 if( (opx==TK_LT56 || opx==TK_GT54) && i<nLeft-1 ){
104275 addrCmp = sqlite3VdbeAddOp0(v, OP_ElseEq58);
104276 testcase(opx==TK_LT); VdbeCoverageIf(v,opx==TK_LT);
104277 testcase(opx==TK_GT); VdbeCoverageIf(v,opx==TK_GT);
104278 }
104279 if( p5==SQLITE_NULLEQ0x80 ){
104280 sqlite3VdbeAddOp2(v, OP_Integer71, 0, dest);
104281 }else{
104282 sqlite3VdbeAddOp3(v, OP_ZeroOrNull92, r1, dest, r2);
104283 }
104284 if( i==nLeft-1 ){
104285 break;
104286 }
104287 if( opx==TK_EQ53 ){
104288 sqlite3VdbeAddOp2(v, OP_NotNull51, dest, addrDone); VdbeCoverage(v);
104289 }else{
104290 assert( op==TK_LT || op==TK_GT || op==TK_LE || op==TK_GE )((void) (0));
104291 sqlite3VdbeAddOp2(v, OP_Goto8, 0, addrDone);
104292 if( i==nLeft-2 ) opx = op;
104293 }
104294 }
104295 sqlite3VdbeJumpHere(v, addrCmp);
104296 sqlite3VdbeResolveLabel(v, addrDone);
104297 if( op==TK_NE52 ){
104298 sqlite3VdbeAddOp2(v, OP_Not19, dest, dest);
104299 }
104300}
104301
104302#if SQLITE_MAX_EXPR_DEPTH1000>0
104303/*
104304** Check that argument nHeight is less than or equal to the maximum
104305** expression depth allowed. If it is not, leave an error message in
104306** pParse.
104307*/
104308SQLITE_PRIVATEstatic int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
104309 int rc = SQLITE_OK0;
104310 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH3];
104311 if( nHeight>mxHeight ){
104312 sqlite3ErrorMsg(pParse,
104313 "Expression tree is too large (maximum depth %d)", mxHeight
104314 );
104315 rc = SQLITE_ERROR1;
104316 }
104317 return rc;
104318}
104319
104320/* The following three functions, heightOfExpr(), heightOfExprList()
104321** and heightOfSelect(), are used to determine the maximum height
104322** of any expression tree referenced by the structure passed as the
104323** first argument.
104324**
104325** If this maximum height is greater than the current value pointed
104326** to by pnHeight, the second parameter, then set *pnHeight to that
104327** value.
104328*/
104329static void heightOfExpr(const Expr *p, int *pnHeight){
104330 if( p ){
104331 if( p->nHeight>*pnHeight ){
104332 *pnHeight = p->nHeight;
104333 }
104334 }
104335}
104336static void heightOfExprList(const ExprList *p, int *pnHeight){
104337 if( p ){
104338 int i;
104339 for(i=0; i<p->nExpr; i++){
104340 heightOfExpr(p->a[i].pExpr, pnHeight);
104341 }
104342 }
104343}
104344static void heightOfSelect(const Select *pSelect, int *pnHeight){
104345 const Select *p;
104346 for(p=pSelect; p; p=p->pPrior){
104347 heightOfExpr(p->pWhere, pnHeight);
104348 heightOfExpr(p->pHaving, pnHeight);
104349 heightOfExpr(p->pLimit, pnHeight);
104350 heightOfExprList(p->pEList, pnHeight);
104351 heightOfExprList(p->pGroupBy, pnHeight);
104352 heightOfExprList(p->pOrderBy, pnHeight);
104353 }
104354}
104355
104356/*
104357** Set the Expr.nHeight variable in the structure passed as an
104358** argument. An expression with no children, Expr.pList or
104359** Expr.pSelect member has a height of 1. Any other expression
104360** has a height equal to the maximum height of any other
104361** referenced Expr plus one.
104362**
104363** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags,
104364** if appropriate.
104365*/
104366static void exprSetHeight(Expr *p){
104367 int nHeight = p->pLeft ? p->pLeft->nHeight : 0;
104368 if( p->pRight && p->pRight->nHeight>nHeight ) nHeight = p->pRight->nHeight;
104369 if( ExprUseXSelect(p)(((p)->flags&0x001000)!=0) ){
104370 heightOfSelect(p->x.pSelect, &nHeight);
104371 }else if( p->x.pList ){
104372 heightOfExprList(p->x.pList, &nHeight);
104373 p->flags |= EP_Propagate(0x000200|0x400000|0x000008) & sqlite3ExprListFlags(p->x.pList);
104374 }
104375 p->nHeight = nHeight + 1;
104376}
104377
104378/*
104379** Set the Expr.nHeight variable using the exprSetHeight() function. If
104380** the height is greater than the maximum allowed expression depth,
104381** leave an error in pParse.
104382**
104383** Also propagate all EP_Propagate flags from the Expr.x.pList into
104384** Expr.flags.
104385*/
104386SQLITE_PRIVATEstatic void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
104387 if( pParse->nErr ) return;
104388 exprSetHeight(p);
104389 sqlite3ExprCheckHeight(pParse, p->nHeight);
104390}
104391
104392/*
104393** Return the maximum height of any expression tree referenced
104394** by the select statement passed as an argument.
104395*/
104396SQLITE_PRIVATEstatic int sqlite3SelectExprHeight(const Select *p){
104397 int nHeight = 0;
104398 heightOfSelect(p, &nHeight);
104399 return nHeight;
104400}
104401#else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */
104402/*
104403** Propagate all EP_Propagate flags from the Expr.x.pList into
104404** Expr.flags.
104405*/
104406SQLITE_PRIVATEstatic void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
104407 if( pParse->nErr ) return;
104408 if( p && ExprUseXList(p)(((p)->flags&0x001000)==0) && p->x.pList ){
104409 p->flags |= EP_Propagate(0x000200|0x400000|0x000008) & sqlite3ExprListFlags(p->x.pList);
104410 }
104411}
104412#define exprSetHeight(y)
104413#endif /* SQLITE_MAX_EXPR_DEPTH>0 */
104414
104415/*
104416** This routine is the core allocator for Expr nodes.
104417**
104418** Construct a new expression node and return a pointer to it. Memory
104419** for this node and for the pToken argument is a single allocation
104420** obtained from sqlite3DbMalloc(). The calling function
104421** is responsible for making sure the node eventually gets freed.
104422**
104423** If dequote is true, then the token (if it exists) is dequoted.
104424** If dequote is false, no dequoting is performed. The deQuote
104425** parameter is ignored if pToken is NULL or if the token does not
104426** appear to be quoted. If the quotes were of the form "..." (double-quotes)
104427** then the EP_DblQuoted flag is set on the expression node.
104428**
104429** Special case: If op==TK_INTEGER and pToken points to a string that
104430** can be translated into a 32-bit integer, then the token is not
104431** stored in u.zToken. Instead, the integer values is written
104432** into u.iValue and the EP_IntValue flag is set. No extra storage
104433** is allocated to hold the integer text and the dequote flag is ignored.
104434*/
104435SQLITE_PRIVATEstatic Expr *sqlite3ExprAlloc(
104436 sqlite3 *db, /* Handle for sqlite3DbMallocRawNN() */
104437 int op, /* Expression opcode */
104438 const Token *pToken, /* Token argument. Might be NULL */
104439 int dequote /* True to dequote */
104440){
104441 Expr *pNew;
104442 int nExtra = 0;
104443 int iValue = 0;
104444
104445 assert( db!=0 )((void) (0));
104446 if( pToken ){
104447 if( op!=TK_INTEGER155 || pToken->z==0
104448 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
104449 nExtra = pToken->n+1;
104450 assert( iValue>=0 )((void) (0));
104451 }
104452 }
104453 pNew = sqlite3DbMallocRawNN(db, sizeof(Expr)+nExtra);
104454 if( pNew ){
104455 memset(pNew, 0, sizeof(Expr));
104456 pNew->op = (u8)op;
104457 pNew->iAgg = -1;
104458 if( pToken ){
104459 if( nExtra==0 ){
104460 pNew->flags |= EP_IntValue0x000800|EP_Leaf0x800000|(iValue?EP_IsTrue0x10000000:EP_IsFalse0x20000000);
104461 pNew->u.iValue = iValue;
104462 }else{
104463 pNew->u.zToken = (char*)&pNew[1];
104464 assert( pToken->z!=0 || pToken->n==0 )((void) (0));
104465 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
104466 pNew->u.zToken[pToken->n] = 0;
104467 if( dequote && sqlite3Isquote(pNew->u.zToken[0])(sqlite3CtypeMap[(unsigned char)(pNew->u.zToken[0])]&0x80
)
){
104468 sqlite3DequoteExpr(pNew);
104469 }
104470 }
104471 }
104472#if SQLITE_MAX_EXPR_DEPTH1000>0
104473 pNew->nHeight = 1;
104474#endif
104475 }
104476 return pNew;
104477}
104478
104479/*
104480** Allocate a new expression node from a zero-terminated token that has
104481** already been dequoted.
104482*/
104483SQLITE_PRIVATEstatic Expr *sqlite3Expr(
104484 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
104485 int op, /* Expression opcode */
104486 const char *zToken /* Token argument. Might be NULL */
104487){
104488 Token x;
104489 x.z = zToken;
104490 x.n = sqlite3Strlen30(zToken);
104491 return sqlite3ExprAlloc(db, op, &x, 0);
104492}
104493
104494/*
104495** Attach subtrees pLeft and pRight to the Expr node pRoot.
104496**
104497** If pRoot==NULL that means that a memory allocation error has occurred.
104498** In that case, delete the subtrees pLeft and pRight.
104499*/
104500SQLITE_PRIVATEstatic void sqlite3ExprAttachSubtrees(
104501 sqlite3 *db,
104502 Expr *pRoot,
104503 Expr *pLeft,
104504 Expr *pRight
104505){
104506 if( pRoot==0 ){
104507 assert( db->mallocFailed )((void) (0));
104508 sqlite3ExprDelete(db, pLeft);
104509 sqlite3ExprDelete(db, pRight);
104510 }else{
104511 if( pRight ){
104512 pRoot->pRight = pRight;
104513 pRoot->flags |= EP_Propagate(0x000200|0x400000|0x000008) & pRight->flags;
104514 }
104515 if( pLeft ){
104516 pRoot->pLeft = pLeft;
104517 pRoot->flags |= EP_Propagate(0x000200|0x400000|0x000008) & pLeft->flags;
104518 }
104519 exprSetHeight(pRoot);
104520 }
104521}
104522
104523/*
104524** Allocate an Expr node which joins as many as two subtrees.
104525**
104526** One or both of the subtrees can be NULL. Return a pointer to the new
104527** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
104528** free the subtrees and return NULL.
104529*/
104530SQLITE_PRIVATEstatic Expr *sqlite3PExpr(
104531 Parse *pParse, /* Parsing context */
104532 int op, /* Expression opcode */
104533 Expr *pLeft, /* Left operand */
104534 Expr *pRight /* Right operand */
104535){
104536 Expr *p;
104537 p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
104538 if( p ){
104539 memset(p, 0, sizeof(Expr));
104540 p->op = op & 0xff;
104541 p->iAgg = -1;
104542 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
104543 sqlite3ExprCheckHeight(pParse, p->nHeight);
104544 }else{
104545 sqlite3ExprDelete(pParse->db, pLeft);
104546 sqlite3ExprDelete(pParse->db, pRight);
104547 }
104548 return p;
104549}
104550
104551/*
104552** Add pSelect to the Expr.x.pSelect field. Or, if pExpr is NULL (due
104553** do a memory allocation failure) then delete the pSelect object.
104554*/
104555SQLITE_PRIVATEstatic void sqlite3PExprAddSelect(Parse *pParse, Expr *pExpr, Select *pSelect){
104556 if( pExpr ){
104557 pExpr->x.pSelect = pSelect;
104558 ExprSetProperty(pExpr, EP_xIsSelect|EP_Subquery)(pExpr)->flags|=(0x001000|0x400000);
104559 sqlite3ExprSetHeightAndFlags(pParse, pExpr);
104560 }else{
104561 assert( pParse->db->mallocFailed )((void) (0));
104562 sqlite3SelectDelete(pParse->db, pSelect);
104563 }
104564}
104565
104566/*
104567** Expression list pEList is a list of vector values. This function
104568** converts the contents of pEList to a VALUES(...) Select statement
104569** returning 1 row for each element of the list. For example, the
104570** expression list:
104571**
104572** ( (1,2), (3,4) (5,6) )
104573**
104574** is translated to the equivalent of:
104575**
104576** VALUES(1,2), (3,4), (5,6)
104577**
104578** Each of the vector values in pEList must contain exactly nElem terms.
104579** If a list element that is not a vector or does not contain nElem terms,
104580** an error message is left in pParse.
104581**
104582** This is used as part of processing IN(...) expressions with a list
104583** of vectors on the RHS. e.g. "... IN ((1,2), (3,4), (5,6))".
104584*/
104585SQLITE_PRIVATEstatic Select *sqlite3ExprListToValues(Parse *pParse, int nElem, ExprList *pEList){
104586 int ii;
104587 Select *pRet = 0;
104588 assert( nElem>1 )((void) (0));
104589 for(ii=0; ii<pEList->nExpr; ii++){
104590 Select *pSel;
104591 Expr *pExpr = pEList->a[ii].pExpr;
104592 int nExprElem;
104593 if( pExpr->op==TK_VECTOR177 ){
104594 assert( ExprUseXList(pExpr) )((void) (0));
104595 nExprElem = pExpr->x.pList->nExpr;
104596 }else{
104597 nExprElem = 1;
104598 }
104599 if( nExprElem!=nElem ){
104600 sqlite3ErrorMsg(pParse, "IN(...) element has %d term%s - expected %d",
104601 nExprElem, nExprElem>1?"s":"", nElem
104602 );
104603 break;
104604 }
104605 assert( ExprUseXList(pExpr) )((void) (0));
104606 pSel = sqlite3SelectNew(pParse, pExpr->x.pList, 0, 0, 0, 0, 0, SF_Values0x0000200,0);
104607 pExpr->x.pList = 0;
104608 if( pSel ){
104609 if( pRet ){
104610 pSel->op = TK_ALL135;
104611 pSel->pPrior = pRet;
104612 }
104613 pRet = pSel;
104614 }
104615 }
104616
104617 if( pRet && pRet->pPrior ){
104618 pRet->selFlags |= SF_MultiValue0x0000400;
104619 }
104620 sqlite3ExprListDelete(pParse->db, pEList);
104621 return pRet;
104622}
104623
104624/*
104625** Join two expressions using an AND operator. If either expression is
104626** NULL, then just return the other expression.
104627**
104628** If one side or the other of the AND is known to be false, then instead
104629** of returning an AND expression, just return a constant expression with
104630** a value of false.
104631*/
104632SQLITE_PRIVATEstatic Expr *sqlite3ExprAnd(Parse *pParse, Expr *pLeft, Expr *pRight){
104633 sqlite3 *db = pParse->db;
104634 if( pLeft==0 ){
104635 return pRight;
104636 }else if( pRight==0 ){
104637 return pLeft;
104638 }else if( (ExprAlwaysFalse(pLeft)(((pLeft)->flags&(0x000001|0x20000000))==0x20000000) || ExprAlwaysFalse(pRight)(((pRight)->flags&(0x000001|0x20000000))==0x20000000))
104639 && !IN_RENAME_OBJECT(pParse->eParseMode>=2)
104640 ){
104641 sqlite3ExprDeferredDelete(pParse, pLeft);
104642 sqlite3ExprDeferredDelete(pParse, pRight);
104643 return sqlite3Expr(db, TK_INTEGER155, "0");
104644 }else{
104645 return sqlite3PExpr(pParse, TK_AND44, pLeft, pRight);
104646 }
104647}
104648
104649/*
104650** Construct a new expression node for a function with multiple
104651** arguments.
104652*/
104653SQLITE_PRIVATEstatic Expr *sqlite3ExprFunction(
104654 Parse *pParse, /* Parsing context */
104655 ExprList *pList, /* Argument list */
104656 const Token *pToken, /* Name of the function */
104657 int eDistinct /* SF_Distinct or SF_ALL or 0 */
104658){
104659 Expr *pNew;
104660 sqlite3 *db = pParse->db;
104661 assert( pToken )((void) (0));
104662 pNew = sqlite3ExprAlloc(db, TK_FUNCTION172, pToken, 1);
104663 if( pNew==0 ){
104664 sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
104665 return 0;
104666 }
104667 assert( !ExprHasProperty(pNew, EP_InnerON|EP_OuterON) )((void) (0));
104668 pNew->w.iOfst = (int)(pToken->z - pParse->zTail);
104669 if( pList
104670 && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG6]
104671 && !pParse->nested
104672 ){
104673 sqlite3ErrorMsg(pParse, "too many arguments on function %T", pToken);
104674 }
104675 pNew->x.pList = pList;
104676 ExprSetProperty(pNew, EP_HasFunc)(pNew)->flags|=(0x000008);
104677 assert( ExprUseXList(pNew) )((void) (0));
104678 sqlite3ExprSetHeightAndFlags(pParse, pNew);
104679 if( eDistinct==SF_Distinct0x0000001 ) ExprSetProperty(pNew, EP_Distinct)(pNew)->flags|=(0x000004);
104680 return pNew;
104681}
104682
104683/*
104684** Check to see if a function is usable according to current access
104685** rules:
104686**
104687** SQLITE_FUNC_DIRECT - Only usable from top-level SQL
104688**
104689** SQLITE_FUNC_UNSAFE - Usable if TRUSTED_SCHEMA or from
104690** top-level SQL
104691**
104692** If the function is not usable, create an error.
104693*/
104694SQLITE_PRIVATEstatic void sqlite3ExprFunctionUsable(
104695 Parse *pParse, /* Parsing and code generating context */
104696 const Expr *pExpr, /* The function invocation */
104697 const FuncDef *pDef /* The function being invoked */
104698){
104699 assert( !IN_RENAME_OBJECT )((void) (0));
104700 assert( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0 )((void) (0));
104701 if( ExprHasProperty(pExpr, EP_FromDDL)(((pExpr)->flags&(0x40000000))!=0) ){
104702 if( (pDef->funcFlags & SQLITE_FUNC_DIRECT0x00080000)!=0
104703 || (pParse->db->flags & SQLITE_TrustedSchema0x00000080)==0
104704 ){
104705 /* Functions prohibited in triggers and views if:
104706 ** (1) tagged with SQLITE_DIRECTONLY
104707 ** (2) not tagged with SQLITE_INNOCUOUS (which means it
104708 ** is tagged with SQLITE_FUNC_UNSAFE) and
104709 ** SQLITE_DBCONFIG_TRUSTED_SCHEMA is off (meaning
104710 ** that the schema is possibly tainted).
104711 */
104712 sqlite3ErrorMsg(pParse, "unsafe use of %#T()", pExpr);
104713 }
104714 }
104715}
104716
104717/*
104718** Assign a variable number to an expression that encodes a wildcard
104719** in the original SQL statement.
104720**
104721** Wildcards consisting of a single "?" are assigned the next sequential
104722** variable number.
104723**
104724** Wildcards of the form "?nnn" are assigned the number "nnn". We make
104725** sure "nnn" is not too big to avoid a denial of service attack when
104726** the SQL statement comes from an external source.
104727**
104728** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
104729** as the previous instance of the same wildcard. Or if this is the first
104730** instance of the wildcard, the next sequential variable number is
104731** assigned.
104732*/
104733SQLITE_PRIVATEstatic void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr, u32 n){
104734 sqlite3 *db = pParse->db;
104735 const char *z;
104736 ynVar x;
104737
104738 if( pExpr==0 ) return;
104739 assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) )((void) (0));
104740 z = pExpr->u.zToken;
104741 assert( z!=0 )((void) (0));
104742 assert( z[0]!=0 )((void) (0));
104743 assert( n==(u32)sqlite3Strlen30(z) )((void) (0));
104744 if( z[1]==0 ){
104745 /* Wildcard of the form "?". Assign the next variable number */
104746 assert( z[0]=='?' )((void) (0));
104747 x = (ynVar)(++pParse->nVar);
104748 }else{
104749 int doAdd = 0;
104750 if( z[0]=='?' ){
104751 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
104752 ** use it as the variable number */
104753 i64 i;
104754 int bOk;
104755 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
104756 i = z[1]-'0'; /* The common case of ?N for a single digit N */
104757 bOk = 1;
104758 }else{
104759 bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF81);
104760 }
104761 testcase( i==0 );
104762 testcase( i==1 );
104763 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
104764 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
104765 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER9] ){
104766 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
104767 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER9]);
104768 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
104769 return;
104770 }
104771 x = (ynVar)i;
104772 if( x>pParse->nVar ){
104773 pParse->nVar = (int)x;
104774 doAdd = 1;
104775 }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
104776 doAdd = 1;
104777 }
104778 }else{
104779 /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable
104780 ** number as the prior appearance of the same name, or if the name
104781 ** has never appeared before, reuse the same variable number
104782 */
104783 x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n);
104784 if( x==0 ){
104785 x = (ynVar)(++pParse->nVar);
104786 doAdd = 1;
104787 }
104788 }
104789 if( doAdd ){
104790 pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x);
104791 }
104792 }
104793 pExpr->iColumn = x;
104794 if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER9] ){
104795 sqlite3ErrorMsg(pParse, "too many SQL variables");
104796 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
104797 }
104798}
104799
104800/*
104801** Recursively delete an expression tree.
104802*/
104803static SQLITE_NOINLINE__attribute__((noinline)) void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
104804 assert( p!=0 )((void) (0));
104805 assert( !ExprUseUValue(p) || p->u.iValue>=0 )((void) (0));
104806 assert( !ExprUseYWin(p) || !ExprUseYSub(p) )((void) (0));
104807 assert( !ExprUseYWin(p) || p->y.pWin!=0 || db->mallocFailed )((void) (0));
104808 assert( p->op!=TK_FUNCTION || !ExprUseYSub(p) )((void) (0));
104809#ifdef SQLITE_DEBUG
104810 if( ExprHasProperty(p, EP_Leaf)(((p)->flags&(0x800000))!=0) && !ExprHasProperty(p, EP_TokenOnly)(((p)->flags&(0x010000))!=0) ){
104811 assert( p->pLeft==0 )((void) (0));
104812 assert( p->pRight==0 )((void) (0));
104813 assert( !ExprUseXSelect(p) || p->x.pSelect==0 )((void) (0));
104814 assert( !ExprUseXList(p) || p->x.pList==0 )((void) (0));
104815 }
104816#endif
104817 if( !ExprHasProperty(p, (EP_TokenOnly|EP_Leaf))(((p)->flags&((0x010000|0x800000)))!=0) ){
104818 /* The Expr.x union is never used at the same time as Expr.pRight */
104819 assert( (ExprUseXList(p) && p->x.pList==0) || p->pRight==0 )((void) (0));
104820 if( p->pLeft && p->op!=TK_SELECT_COLUMN178 ) sqlite3ExprDeleteNN(db, p->pLeft);
104821 if( p->pRight ){
104822 assert( !ExprHasProperty(p, EP_WinFunc) )((void) (0));
104823 sqlite3ExprDeleteNN(db, p->pRight);
104824 }else if( ExprUseXSelect(p)(((p)->flags&0x001000)!=0) ){
104825 assert( !ExprHasProperty(p, EP_WinFunc) )((void) (0));
104826 sqlite3SelectDelete(db, p->x.pSelect);
104827 }else{
104828 sqlite3ExprListDelete(db, p->x.pList);
104829#ifndef SQLITE_OMIT_WINDOWFUNC
104830 if( ExprHasProperty(p, EP_WinFunc)(((p)->flags&(0x1000000))!=0) ){
104831 sqlite3WindowDelete(db, p->y.pWin);
104832 }
104833#endif
104834 }
104835 }
104836 if( ExprHasProperty(p, EP_MemToken)(((p)->flags&(0x020000))!=0) ){
104837 assert( !ExprHasProperty(p, EP_IntValue) )((void) (0));
104838 sqlite3DbFree(db, p->u.zToken);
104839 }
104840 if( !ExprHasProperty(p, EP_Static)(((p)->flags&(0x8000000))!=0) ){
104841 sqlite3DbFreeNN(db, p);
104842 }
104843}
104844SQLITE_PRIVATEstatic void sqlite3ExprDelete(sqlite3 *db, Expr *p){
104845 if( p ) sqlite3ExprDeleteNN(db, p);
104846}
104847
104848/*
104849** Clear both elements of an OnOrUsing object
104850*/
104851SQLITE_PRIVATEstatic void sqlite3ClearOnOrUsing(sqlite3 *db, OnOrUsing *p){
104852 if( p==0 ){
104853 /* Nothing to clear */
104854 }else if( p->pOn ){
104855 sqlite3ExprDeleteNN(db, p->pOn);
104856 }else if( p->pUsing ){
104857 sqlite3IdListDelete(db, p->pUsing);
104858 }
104859}
104860
104861/*
104862** Arrange to cause pExpr to be deleted when the pParse is deleted.
104863** This is similar to sqlite3ExprDelete() except that the delete is
104864** deferred untilthe pParse is deleted.
104865**
104866** The pExpr might be deleted immediately on an OOM error.
104867**
104868** The deferred delete is (currently) implemented by adding the
104869** pExpr to the pParse->pConstExpr list with a register number of 0.
104870*/
104871SQLITE_PRIVATEstatic void sqlite3ExprDeferredDelete(Parse *pParse, Expr *pExpr){
104872 pParse->pConstExpr =
104873 sqlite3ExprListAppend(pParse, pParse->pConstExpr, pExpr);
104874}
104875
104876/* Invoke sqlite3RenameExprUnmap() and sqlite3ExprDelete() on the
104877** expression.
104878*/
104879SQLITE_PRIVATEstatic void sqlite3ExprUnmapAndDelete(Parse *pParse, Expr *p){
104880 if( p ){
104881 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
104882 sqlite3RenameExprUnmap(pParse, p);
104883 }
104884 sqlite3ExprDeleteNN(pParse->db, p);
104885 }
104886}
104887
104888/*
104889** Return the number of bytes allocated for the expression structure
104890** passed as the first argument. This is always one of EXPR_FULLSIZE,
104891** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
104892*/
104893static int exprStructSize(const Expr *p){
104894 if( ExprHasProperty(p, EP_TokenOnly)(((p)->flags&(0x010000))!=0) ) return EXPR_TOKENONLYSIZE__builtin_offsetof(Expr, pLeft);
104895 if( ExprHasProperty(p, EP_Reduced)(((p)->flags&(0x004000))!=0) ) return EXPR_REDUCEDSIZE__builtin_offsetof(Expr, iTable);
104896 return EXPR_FULLSIZEsizeof(Expr);
104897}
104898
104899/*
104900** The dupedExpr*Size() routines each return the number of bytes required
104901** to store a copy of an expression or expression tree. They differ in
104902** how much of the tree is measured.
104903**
104904** dupedExprStructSize() Size of only the Expr structure
104905** dupedExprNodeSize() Size of Expr + space for token
104906** dupedExprSize() Expr + token + subtree components
104907**
104908***************************************************************************
104909**
104910** The dupedExprStructSize() function returns two values OR-ed together:
104911** (1) the space required for a copy of the Expr structure only and
104912** (2) the EP_xxx flags that indicate what the structure size should be.
104913** The return values is always one of:
104914**
104915** EXPR_FULLSIZE
104916** EXPR_REDUCEDSIZE | EP_Reduced
104917** EXPR_TOKENONLYSIZE | EP_TokenOnly
104918**
104919** The size of the structure can be found by masking the return value
104920** of this routine with 0xfff. The flags can be found by masking the
104921** return value with EP_Reduced|EP_TokenOnly.
104922**
104923** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
104924** (unreduced) Expr objects as they or originally constructed by the parser.
104925** During expression analysis, extra information is computed and moved into
104926** later parts of the Expr object and that extra information might get chopped
104927** off if the expression is reduced. Note also that it does not work to
104928** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal
104929** to reduce a pristine expression tree from the parser. The implementation
104930** of dupedExprStructSize() contain multiple assert() statements that attempt
104931** to enforce this constraint.
104932*/
104933static int dupedExprStructSize(const Expr *p, int flags){
104934 int nSize;
104935 assert( flags==EXPRDUP_REDUCE || flags==0 )((void) (0)); /* Only one flag value allowed */
104936 assert( EXPR_FULLSIZE<=0xfff )((void) (0));
104937 assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 )((void) (0));
104938 if( 0==flags || p->op==TK_SELECT_COLUMN178
104939#ifndef SQLITE_OMIT_WINDOWFUNC
104940 || ExprHasProperty(p, EP_WinFunc)(((p)->flags&(0x1000000))!=0)
104941#endif
104942 ){
104943 nSize = EXPR_FULLSIZEsizeof(Expr);
104944 }else{
104945 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) )((void) (0));
104946 assert( !ExprHasProperty(p, EP_OuterON) )((void) (0));
104947 assert( !ExprHasProperty(p, EP_MemToken) )((void) (0));
104948 assert( !ExprHasVVAProperty(p, EP_NoReduce) )((void) (0));
104949 if( p->pLeft || p->x.pList ){
104950 nSize = EXPR_REDUCEDSIZE__builtin_offsetof(Expr, iTable) | EP_Reduced0x004000;
104951 }else{
104952 assert( p->pRight==0 )((void) (0));
104953 nSize = EXPR_TOKENONLYSIZE__builtin_offsetof(Expr, pLeft) | EP_TokenOnly0x010000;
104954 }
104955 }
104956 return nSize;
104957}
104958
104959/*
104960** This function returns the space in bytes required to store the copy
104961** of the Expr structure and a copy of the Expr.u.zToken string (if that
104962** string is defined.)
104963*/
104964static int dupedExprNodeSize(const Expr *p, int flags){
104965 int nByte = dupedExprStructSize(p, flags) & 0xfff;
104966 if( !ExprHasProperty(p, EP_IntValue)(((p)->flags&(0x000800))!=0) && p->u.zToken ){
104967 nByte += sqlite3Strlen30NN(p->u.zToken)(strlen(p->u.zToken)&0x3fffffff)+1;
104968 }
104969 return ROUND8(nByte)(((nByte)+7)&~7);
104970}
104971
104972/*
104973** Return the number of bytes required to create a duplicate of the
104974** expression passed as the first argument. The second argument is a
104975** mask containing EXPRDUP_XXX flags.
104976**
104977** The value returned includes space to create a copy of the Expr struct
104978** itself and the buffer referred to by Expr.u.zToken, if any.
104979**
104980** If the EXPRDUP_REDUCE flag is set, then the return value includes
104981** space to duplicate all Expr nodes in the tree formed by Expr.pLeft
104982** and Expr.pRight variables (but not for any structures pointed to or
104983** descended from the Expr.x.pList or Expr.x.pSelect variables).
104984*/
104985static int dupedExprSize(const Expr *p, int flags){
104986 int nByte = 0;
104987 if( p ){
104988 nByte = dupedExprNodeSize(p, flags);
104989 if( flags&EXPRDUP_REDUCE0x0001 ){
104990 nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
104991 }
104992 }
104993 return nByte;
104994}
104995
104996/*
104997** This function is similar to sqlite3ExprDup(), except that if pzBuffer
104998** is not NULL then *pzBuffer is assumed to point to a buffer large enough
104999** to store the copy of expression p, the copies of p->u.zToken
105000** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
105001** if any. Before returning, *pzBuffer is set to the first byte past the
105002** portion of the buffer copied into by this function.
105003*/
105004static Expr *exprDup(sqlite3 *db, const Expr *p, int dupFlags, u8 **pzBuffer){
105005 Expr *pNew; /* Value to return */
105006 u8 *zAlloc; /* Memory space from which to build Expr object */
105007 u32 staticFlag; /* EP_Static if space not obtained from malloc */
105008
105009 assert( db!=0 )((void) (0));
105010 assert( p )((void) (0));
105011 assert( dupFlags==0 || dupFlags==EXPRDUP_REDUCE )((void) (0));
105012 assert( pzBuffer==0 || dupFlags==EXPRDUP_REDUCE )((void) (0));
105013
105014 /* Figure out where to write the new Expr structure. */
105015 if( pzBuffer ){
105016 zAlloc = *pzBuffer;
105017 staticFlag = EP_Static0x8000000;
105018 assert( zAlloc!=0 )((void) (0));
105019 }else{
105020 zAlloc = sqlite3DbMallocRawNN(db, dupedExprSize(p, dupFlags));
105021 staticFlag = 0;
105022 }
105023 pNew = (Expr *)zAlloc;
105024
105025 if( pNew ){
105026 /* Set nNewSize to the size allocated for the structure pointed to
105027 ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
105028 ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
105029 ** by the copy of the p->u.zToken string (if any).
105030 */
105031 const unsigned nStructSize = dupedExprStructSize(p, dupFlags);
105032 const int nNewSize = nStructSize & 0xfff;
105033 int nToken;
105034 if( !ExprHasProperty(p, EP_IntValue)(((p)->flags&(0x000800))!=0) && p->u.zToken ){
105035 nToken = sqlite3Strlen30(p->u.zToken) + 1;
105036 }else{
105037 nToken = 0;
105038 }
105039 if( dupFlags ){
105040 assert( ExprHasProperty(p, EP_Reduced)==0 )((void) (0));
105041 memcpy(zAlloc, p, nNewSize);
105042 }else{
105043 u32 nSize = (u32)exprStructSize(p);
105044 memcpy(zAlloc, p, nSize);
105045 if( nSize<EXPR_FULLSIZEsizeof(Expr) ){
105046 memset(&zAlloc[nSize], 0, EXPR_FULLSIZEsizeof(Expr)-nSize);
105047 }
105048 }
105049
105050 /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
105051 pNew->flags &= ~(EP_Reduced0x004000|EP_TokenOnly0x010000|EP_Static0x8000000|EP_MemToken0x020000);
105052 pNew->flags |= nStructSize & (EP_Reduced0x004000|EP_TokenOnly0x010000);
105053 pNew->flags |= staticFlag;
105054 ExprClearVVAProperties(pNew);
105055 if( dupFlags ){
105056 ExprSetVVAProperty(pNew, EP_Immutable);
105057 }
105058
105059 /* Copy the p->u.zToken string, if any. */
105060 if( nToken ){
105061 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
105062 memcpy(zToken, p->u.zToken, nToken);
105063 }
105064
105065 if( 0==((p->flags|pNew->flags) & (EP_TokenOnly0x010000|EP_Leaf0x800000)) ){
105066 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
105067 if( ExprUseXSelect(p)(((p)->flags&0x001000)!=0) ){
105068 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
105069 }else{
105070 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
105071 }
105072 }
105073
105074 /* Fill in pNew->pLeft and pNew->pRight. */
105075 if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly|EP_WinFunc)(((pNew)->flags&(0x004000|0x010000|0x1000000))!=0) ){
105076 zAlloc += dupedExprNodeSize(p, dupFlags);
105077 if( !ExprHasProperty(pNew, EP_TokenOnly|EP_Leaf)(((pNew)->flags&(0x010000|0x800000))!=0) ){
105078 pNew->pLeft = p->pLeft ?
105079 exprDup(db, p->pLeft, EXPRDUP_REDUCE0x0001, &zAlloc) : 0;
105080 pNew->pRight = p->pRight ?
105081 exprDup(db, p->pRight, EXPRDUP_REDUCE0x0001, &zAlloc) : 0;
105082 }
105083#ifndef SQLITE_OMIT_WINDOWFUNC
105084 if( ExprHasProperty(p, EP_WinFunc)(((p)->flags&(0x1000000))!=0) ){
105085 pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin);
105086 assert( ExprHasProperty(pNew, EP_WinFunc) )((void) (0));
105087 }
105088#endif /* SQLITE_OMIT_WINDOWFUNC */
105089 if( pzBuffer ){
105090 *pzBuffer = zAlloc;
105091 }
105092 }else{
105093 if( !ExprHasProperty(p, EP_TokenOnly|EP_Leaf)(((p)->flags&(0x010000|0x800000))!=0) ){
105094 if( pNew->op==TK_SELECT_COLUMN178 ){
105095 pNew->pLeft = p->pLeft;
105096 assert( p->pRight==0 || p->pRight==p->pLeft((void) (0))
105097 || ExprHasProperty(p->pLeft, EP_Subquery) )((void) (0));
105098 }else{
105099 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
105100 }
105101 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
105102 }
105103 }
105104 }
105105 return pNew;
105106}
105107
105108/*
105109** Create and return a deep copy of the object passed as the second
105110** argument. If an OOM condition is encountered, NULL is returned
105111** and the db->mallocFailed flag set.
105112*/
105113#ifndef SQLITE_OMIT_CTE
105114SQLITE_PRIVATEstatic With *sqlite3WithDup(sqlite3 *db, With *p){
105115 With *pRet = 0;
105116 if( p ){
105117 sqlite3_int64 nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
105118 pRet = sqlite3DbMallocZero(db, nByte);
105119 if( pRet ){
105120 int i;
105121 pRet->nCte = p->nCte;
105122 for(i=0; i<p->nCte; i++){
105123 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
105124 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
105125 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
105126 pRet->a[i].eM10d = p->a[i].eM10d;
105127 }
105128 }
105129 }
105130 return pRet;
105131}
105132#else
105133# define sqlite3WithDup(x,y) 0
105134#endif
105135
105136#ifndef SQLITE_OMIT_WINDOWFUNC
105137/*
105138** The gatherSelectWindows() procedure and its helper routine
105139** gatherSelectWindowsCallback() are used to scan all the expressions
105140** an a newly duplicated SELECT statement and gather all of the Window
105141** objects found there, assembling them onto the linked list at Select->pWin.
105142*/
105143static int gatherSelectWindowsCallback(Walker *pWalker, Expr *pExpr){
105144 if( pExpr->op==TK_FUNCTION172 && ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
105145 Select *pSelect = pWalker->u.pSelect;
105146 Window *pWin = pExpr->y.pWin;
105147 assert( pWin )((void) (0));
105148 assert( IsWindowFunc(pExpr) )((void) (0));
105149 assert( pWin->ppThis==0 )((void) (0));
105150 sqlite3WindowLink(pSelect, pWin);
105151 }
105152 return WRC_Continue0;
105153}
105154static int gatherSelectWindowsSelectCallback(Walker *pWalker, Select *p){
105155 return p==pWalker->u.pSelect ? WRC_Continue0 : WRC_Prune1;
105156}
105157static void gatherSelectWindows(Select *p){
105158 Walker w;
105159 w.xExprCallback = gatherSelectWindowsCallback;
105160 w.xSelectCallback = gatherSelectWindowsSelectCallback;
105161 w.xSelectCallback2 = 0;
105162 w.pParse = 0;
105163 w.u.pSelect = p;
105164 sqlite3WalkSelect(&w, p);
105165}
105166#endif
105167
105168
105169/*
105170** The following group of routines make deep copies of expressions,
105171** expression lists, ID lists, and select statements. The copies can
105172** be deleted (by being passed to their respective ...Delete() routines)
105173** without effecting the originals.
105174**
105175** The expression list, ID, and source lists return by sqlite3ExprListDup(),
105176** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
105177** by subsequent calls to sqlite*ListAppend() routines.
105178**
105179** Any tables that the SrcList might point to are not duplicated.
105180**
105181** The flags parameter contains a combination of the EXPRDUP_XXX flags.
105182** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
105183** truncated version of the usual Expr structure that will be stored as
105184** part of the in-memory representation of the database schema.
105185*/
105186SQLITE_PRIVATEstatic Expr *sqlite3ExprDup(sqlite3 *db, const Expr *p, int flags){
105187 assert( flags==0 || flags==EXPRDUP_REDUCE )((void) (0));
105188 return p ? exprDup(db, p, flags, 0) : 0;
105189}
105190SQLITE_PRIVATEstatic ExprList *sqlite3ExprListDup(sqlite3 *db, const ExprList *p, int flags){
105191 ExprList *pNew;
105192 struct ExprList_item *pItem;
105193 const struct ExprList_item *pOldItem;
105194 int i;
105195 Expr *pPriorSelectColOld = 0;
105196 Expr *pPriorSelectColNew = 0;
105197 assert( db!=0 )((void) (0));
105198 if( p==0 ) return 0;
105199 pNew = sqlite3DbMallocRawNN(db, sqlite3DbMallocSize(db, p));
105200 if( pNew==0 ) return 0;
105201 pNew->nExpr = p->nExpr;
105202 pNew->nAlloc = p->nAlloc;
105203 pItem = pNew->a;
105204 pOldItem = p->a;
105205 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
105206 Expr *pOldExpr = pOldItem->pExpr;
105207 Expr *pNewExpr;
105208 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
105209 if( pOldExpr
105210 && pOldExpr->op==TK_SELECT_COLUMN178
105211 && (pNewExpr = pItem->pExpr)!=0
105212 ){
105213 if( pNewExpr->pRight ){
105214 pPriorSelectColOld = pOldExpr->pRight;
105215 pPriorSelectColNew = pNewExpr->pRight;
105216 pNewExpr->pLeft = pNewExpr->pRight;
105217 }else{
105218 if( pOldExpr->pLeft!=pPriorSelectColOld ){
105219 pPriorSelectColOld = pOldExpr->pLeft;
105220 pPriorSelectColNew = sqlite3ExprDup(db, pPriorSelectColOld, flags);
105221 pNewExpr->pRight = pPriorSelectColNew;
105222 }
105223 pNewExpr->pLeft = pPriorSelectColNew;
105224 }
105225 }
105226 pItem->zEName = sqlite3DbStrDup(db, pOldItem->zEName);
105227 pItem->fg = pOldItem->fg;
105228 pItem->fg.done = 0;
105229 pItem->u = pOldItem->u;
105230 }
105231 return pNew;
105232}
105233
105234/*
105235** If cursors, triggers, views and subqueries are all omitted from
105236** the build, then none of the following routines, except for
105237** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
105238** called with a NULL argument.
105239*/
105240#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
105241 || !defined(SQLITE_OMIT_SUBQUERY)
105242SQLITE_PRIVATEstatic SrcList *sqlite3SrcListDup(sqlite3 *db, const SrcList *p, int flags){
105243 SrcList *pNew;
105244 int i;
105245 int nByte;
105246 assert( db!=0 )((void) (0));
105247 if( p==0 ) return 0;
105248 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
105249 pNew = sqlite3DbMallocRawNN(db, nByte );
105250 if( pNew==0 ) return 0;
105251 pNew->nSrc = pNew->nAlloc = p->nSrc;
105252 for(i=0; i<p->nSrc; i++){
105253 SrcItem *pNewItem = &pNew->a[i];
105254 const SrcItem *pOldItem = &p->a[i];
105255 Table *pTab;
105256 pNewItem->pSchema = pOldItem->pSchema;
105257 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
105258 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
105259 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
105260 pNewItem->fg = pOldItem->fg;
105261 pNewItem->iCursor = pOldItem->iCursor;
105262 pNewItem->addrFillSub = pOldItem->addrFillSub;
105263 pNewItem->regReturn = pOldItem->regReturn;
105264 if( pNewItem->fg.isIndexedBy ){
105265 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
105266 }
105267 pNewItem->u2 = pOldItem->u2;
105268 if( pNewItem->fg.isCte ){
105269 pNewItem->u2.pCteUse->nUse++;
105270 }
105271 if( pNewItem->fg.isTabFunc ){
105272 pNewItem->u1.pFuncArg =
105273 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
105274 }
105275 pTab = pNewItem->pTab = pOldItem->pTab;
105276 if( pTab ){
105277 pTab->nTabRef++;
105278 }
105279 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
105280 if( pOldItem->fg.isUsing ){
105281 assert( pNewItem->fg.isUsing )((void) (0));
105282 pNewItem->u3.pUsing = sqlite3IdListDup(db, pOldItem->u3.pUsing);
105283 }else{
105284 pNewItem->u3.pOn = sqlite3ExprDup(db, pOldItem->u3.pOn, flags);
105285 }
105286 pNewItem->colUsed = pOldItem->colUsed;
105287 }
105288 return pNew;
105289}
105290SQLITE_PRIVATEstatic IdList *sqlite3IdListDup(sqlite3 *db, const IdList *p){
105291 IdList *pNew;
105292 int i;
105293 assert( db!=0 )((void) (0));
105294 if( p==0 ) return 0;
105295 assert( p->eU4!=EU4_EXPR )((void) (0));
105296 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew)+(p->nId-1)*sizeof(p->a[0]) );
105297 if( pNew==0 ) return 0;
105298 pNew->nId = p->nId;
105299 pNew->eU4 = p->eU4;
105300 for(i=0; i<p->nId; i++){
105301 struct IdList_item *pNewItem = &pNew->a[i];
105302 const struct IdList_item *pOldItem = &p->a[i];
105303 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
105304 pNewItem->u4 = pOldItem->u4;
105305 }
105306 return pNew;
105307}
105308SQLITE_PRIVATEstatic Select *sqlite3SelectDup(sqlite3 *db, const Select *pDup, int flags){
105309 Select *pRet = 0;
105310 Select *pNext = 0;
105311 Select **pp = &pRet;
105312 const Select *p;
105313
105314 assert( db!=0 )((void) (0));
105315 for(p=pDup; p; p=p->pPrior){
105316 Select *pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
105317 if( pNew==0 ) break;
105318 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
105319 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
105320 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
105321 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
105322 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
105323 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
105324 pNew->op = p->op;
105325 pNew->pNext = pNext;
105326 pNew->pPrior = 0;
105327 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
105328 pNew->iLimit = 0;
105329 pNew->iOffset = 0;
105330 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral0x0000020;
105331 pNew->addrOpenEphm[0] = -1;
105332 pNew->addrOpenEphm[1] = -1;
105333 pNew->nSelectRow = p->nSelectRow;
105334 pNew->pWith = sqlite3WithDup(db, p->pWith);
105335#ifndef SQLITE_OMIT_WINDOWFUNC
105336 pNew->pWin = 0;
105337 pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn);
105338 if( p->pWin && db->mallocFailed==0 ) gatherSelectWindows(pNew);
105339#endif
105340 pNew->selId = p->selId;
105341 if( db->mallocFailed ){
105342 /* Any prior OOM might have left the Select object incomplete.
105343 ** Delete the whole thing rather than allow an incomplete Select
105344 ** to be used by the code generator. */
105345 pNew->pNext = 0;
105346 sqlite3SelectDelete(db, pNew);
105347 break;
105348 }
105349 *pp = pNew;
105350 pp = &pNew->pPrior;
105351 pNext = pNew;
105352 }
105353
105354 return pRet;
105355}
105356#else
105357SQLITE_PRIVATEstatic Select *sqlite3SelectDup(sqlite3 *db, const Select *p, int flags){
105358 assert( p==0 )((void) (0));
105359 return 0;
105360}
105361#endif
105362
105363
105364/*
105365** Add a new element to the end of an expression list. If pList is
105366** initially NULL, then create a new expression list.
105367**
105368** The pList argument must be either NULL or a pointer to an ExprList
105369** obtained from a prior call to sqlite3ExprListAppend(). This routine
105370** may not be used with an ExprList obtained from sqlite3ExprListDup().
105371** Reason: This routine assumes that the number of slots in pList->a[]
105372** is a power of two. That is true for sqlite3ExprListAppend() returns
105373** but is not necessarily true from the return value of sqlite3ExprListDup().
105374**
105375** If a memory allocation error occurs, the entire list is freed and
105376** NULL is returned. If non-NULL is returned, then it is guaranteed
105377** that the new entry was successfully appended.
105378*/
105379static const struct ExprList_item zeroItem = {0};
105380SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) ExprList *sqlite3ExprListAppendNew(
105381 sqlite3 *db, /* Database handle. Used for memory allocation */
105382 Expr *pExpr /* Expression to be appended. Might be NULL */
105383){
105384 struct ExprList_item *pItem;
105385 ExprList *pList;
105386
105387 pList = sqlite3DbMallocRawNN(db, sizeof(ExprList)+sizeof(pList->a[0])*4 );
105388 if( pList==0 ){
105389 sqlite3ExprDelete(db, pExpr);
105390 return 0;
105391 }
105392 pList->nAlloc = 4;
105393 pList->nExpr = 1;
105394 pItem = &pList->a[0];
105395 *pItem = zeroItem;
105396 pItem->pExpr = pExpr;
105397 return pList;
105398}
105399SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) ExprList *sqlite3ExprListAppendGrow(
105400 sqlite3 *db, /* Database handle. Used for memory allocation */
105401 ExprList *pList, /* List to which to append. Might be NULL */
105402 Expr *pExpr /* Expression to be appended. Might be NULL */
105403){
105404 struct ExprList_item *pItem;
105405 ExprList *pNew;
105406 pList->nAlloc *= 2;
105407 pNew = sqlite3DbRealloc(db, pList,
105408 sizeof(*pList)+(pList->nAlloc-1)*sizeof(pList->a[0]));
105409 if( pNew==0 ){
105410 sqlite3ExprListDelete(db, pList);
105411 sqlite3ExprDelete(db, pExpr);
105412 return 0;
105413 }else{
105414 pList = pNew;
105415 }
105416 pItem = &pList->a[pList->nExpr++];
105417 *pItem = zeroItem;
105418 pItem->pExpr = pExpr;
105419 return pList;
105420}
105421SQLITE_PRIVATEstatic ExprList *sqlite3ExprListAppend(
105422 Parse *pParse, /* Parsing context */
105423 ExprList *pList, /* List to which to append. Might be NULL */
105424 Expr *pExpr /* Expression to be appended. Might be NULL */
105425){
105426 struct ExprList_item *pItem;
105427 if( pList==0 ){
105428 return sqlite3ExprListAppendNew(pParse->db,pExpr);
105429 }
105430 if( pList->nAlloc<pList->nExpr+1 ){
105431 return sqlite3ExprListAppendGrow(pParse->db,pList,pExpr);
105432 }
105433 pItem = &pList->a[pList->nExpr++];
105434 *pItem = zeroItem;
105435 pItem->pExpr = pExpr;
105436 return pList;
105437}
105438
105439/*
105440** pColumns and pExpr form a vector assignment which is part of the SET
105441** clause of an UPDATE statement. Like this:
105442**
105443** (a,b,c) = (expr1,expr2,expr3)
105444** Or: (a,b,c) = (SELECT x,y,z FROM ....)
105445**
105446** For each term of the vector assignment, append new entries to the
105447** expression list pList. In the case of a subquery on the RHS, append
105448** TK_SELECT_COLUMN expressions.
105449*/
105450SQLITE_PRIVATEstatic ExprList *sqlite3ExprListAppendVector(
105451 Parse *pParse, /* Parsing context */
105452 ExprList *pList, /* List to which to append. Might be NULL */
105453 IdList *pColumns, /* List of names of LHS of the assignment */
105454 Expr *pExpr /* Vector expression to be appended. Might be NULL */
105455){
105456 sqlite3 *db = pParse->db;
105457 int n;
105458 int i;
105459 int iFirst = pList ? pList->nExpr : 0;
105460 /* pColumns can only be NULL due to an OOM but an OOM will cause an
105461 ** exit prior to this routine being invoked */
105462 if( NEVER(pColumns==0)(pColumns==0) ) goto vector_append_error;
105463 if( pExpr==0 ) goto vector_append_error;
105464
105465 /* If the RHS is a vector, then we can immediately check to see that
105466 ** the size of the RHS and LHS match. But if the RHS is a SELECT,
105467 ** wildcards ("*") in the result set of the SELECT must be expanded before
105468 ** we can do the size check, so defer the size check until code generation.
105469 */
105470 if( pExpr->op!=TK_SELECT138 && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){
105471 sqlite3ErrorMsg(pParse, "%d columns assigned %d values",
105472 pColumns->nId, n);
105473 goto vector_append_error;
105474 }
105475
105476 for(i=0; i<pColumns->nId; i++){
105477 Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i, pColumns->nId);
105478 assert( pSubExpr!=0 || db->mallocFailed )((void) (0));
105479 if( pSubExpr==0 ) continue;
105480 pList = sqlite3ExprListAppend(pParse, pList, pSubExpr);
105481 if( pList ){
105482 assert( pList->nExpr==iFirst+i+1 )((void) (0));
105483 pList->a[pList->nExpr-1].zEName = pColumns->a[i].zName;
105484 pColumns->a[i].zName = 0;
105485 }
105486 }
105487
105488 if( !db->mallocFailed && pExpr->op==TK_SELECT138 && ALWAYS(pList!=0)(pList!=0) ){
105489 Expr *pFirst = pList->a[iFirst].pExpr;
105490 assert( pFirst!=0 )((void) (0));
105491 assert( pFirst->op==TK_SELECT_COLUMN )((void) (0));
105492
105493 /* Store the SELECT statement in pRight so it will be deleted when
105494 ** sqlite3ExprListDelete() is called */
105495 pFirst->pRight = pExpr;
105496 pExpr = 0;
105497
105498 /* Remember the size of the LHS in iTable so that we can check that
105499 ** the RHS and LHS sizes match during code generation. */
105500 pFirst->iTable = pColumns->nId;
105501 }
105502
105503vector_append_error:
105504 sqlite3ExprUnmapAndDelete(pParse, pExpr);
105505 sqlite3IdListDelete(db, pColumns);
105506 return pList;
105507}
105508
105509/*
105510** Set the sort order for the last element on the given ExprList.
105511*/
105512SQLITE_PRIVATEstatic void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder, int eNulls){
105513 struct ExprList_item *pItem;
105514 if( p==0 ) return;
105515 assert( p->nExpr>0 )((void) (0));
105516
105517 assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC==0 && SQLITE_SO_DESC>0 )((void) (0));
105518 assert( iSortOrder==SQLITE_SO_UNDEFINED((void) (0))
105519 || iSortOrder==SQLITE_SO_ASC((void) (0))
105520 || iSortOrder==SQLITE_SO_DESC((void) (0))
105521 )((void) (0));
105522 assert( eNulls==SQLITE_SO_UNDEFINED((void) (0))
105523 || eNulls==SQLITE_SO_ASC((void) (0))
105524 || eNulls==SQLITE_SO_DESC((void) (0))
105525 )((void) (0));
105526
105527 pItem = &p->a[p->nExpr-1];
105528 assert( pItem->fg.bNulls==0 )((void) (0));
105529 if( iSortOrder==SQLITE_SO_UNDEFINED-1 ){
105530 iSortOrder = SQLITE_SO_ASC0;
105531 }
105532 pItem->fg.sortFlags = (u8)iSortOrder;
105533
105534 if( eNulls!=SQLITE_SO_UNDEFINED-1 ){
105535 pItem->fg.bNulls = 1;
105536 if( iSortOrder!=eNulls ){
105537 pItem->fg.sortFlags |= KEYINFO_ORDER_BIGNULL0x02;
105538 }
105539 }
105540}
105541
105542/*
105543** Set the ExprList.a[].zEName element of the most recently added item
105544** on the expression list.
105545**
105546** pList might be NULL following an OOM error. But pName should never be
105547** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
105548** is set.
105549*/
105550SQLITE_PRIVATEstatic void sqlite3ExprListSetName(
105551 Parse *pParse, /* Parsing context */
105552 ExprList *pList, /* List to which to add the span. */
105553 const Token *pName, /* Name to be added */
105554 int dequote /* True to cause the name to be dequoted */
105555){
105556 assert( pList!=0 || pParse->db->mallocFailed!=0 )((void) (0));
105557 assert( pParse->eParseMode!=PARSE_MODE_UNMAP || dequote==0 )((void) (0));
105558 if( pList ){
105559 struct ExprList_item *pItem;
105560 assert( pList->nExpr>0 )((void) (0));
105561 pItem = &pList->a[pList->nExpr-1];
105562 assert( pItem->zEName==0 )((void) (0));
105563 assert( pItem->fg.eEName==ENAME_NAME )((void) (0));
105564 pItem->zEName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
105565 if( dequote ){
105566 /* If dequote==0, then pName->z does not point to part of a DDL
105567 ** statement handled by the parser. And so no token need be added
105568 ** to the token-map. */
105569 sqlite3Dequote(pItem->zEName);
105570 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
105571 sqlite3RenameTokenMap(pParse, (const void*)pItem->zEName, pName);
105572 }
105573 }
105574 }
105575}
105576
105577/*
105578** Set the ExprList.a[].zSpan element of the most recently added item
105579** on the expression list.
105580**
105581** pList might be NULL following an OOM error. But pSpan should never be
105582** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
105583** is set.
105584*/
105585SQLITE_PRIVATEstatic void sqlite3ExprListSetSpan(
105586 Parse *pParse, /* Parsing context */
105587 ExprList *pList, /* List to which to add the span. */
105588 const char *zStart, /* Start of the span */
105589 const char *zEnd /* End of the span */
105590){
105591 sqlite3 *db = pParse->db;
105592 assert( pList!=0 || db->mallocFailed!=0 )((void) (0));
105593 if( pList ){
105594 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
105595 assert( pList->nExpr>0 )((void) (0));
105596 if( pItem->zEName==0 ){
105597 pItem->zEName = sqlite3DbSpanDup(db, zStart, zEnd);
105598 pItem->fg.eEName = ENAME_SPAN1;
105599 }
105600 }
105601}
105602
105603/*
105604** If the expression list pEList contains more than iLimit elements,
105605** leave an error message in pParse.
105606*/
105607SQLITE_PRIVATEstatic void sqlite3ExprListCheckLength(
105608 Parse *pParse,
105609 ExprList *pEList,
105610 const char *zObject
105611){
105612 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN2];
105613 testcase( pEList && pEList->nExpr==mx );
105614 testcase( pEList && pEList->nExpr==mx+1 );
105615 if( pEList && pEList->nExpr>mx ){
105616 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
105617 }
105618}
105619
105620/*
105621** Delete an entire expression list.
105622*/
105623static SQLITE_NOINLINE__attribute__((noinline)) void exprListDeleteNN(sqlite3 *db, ExprList *pList){
105624 int i = pList->nExpr;
105625 struct ExprList_item *pItem = pList->a;
105626 assert( pList->nExpr>0 )((void) (0));
105627 do{
105628 sqlite3ExprDelete(db, pItem->pExpr);
105629 sqlite3DbFree(db, pItem->zEName);
105630 pItem++;
105631 }while( --i>0 );
105632 sqlite3DbFreeNN(db, pList);
105633}
105634SQLITE_PRIVATEstatic void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
105635 if( pList ) exprListDeleteNN(db, pList);
105636}
105637
105638/*
105639** Return the bitwise-OR of all Expr.flags fields in the given
105640** ExprList.
105641*/
105642SQLITE_PRIVATEstatic u32 sqlite3ExprListFlags(const ExprList *pList){
105643 int i;
105644 u32 m = 0;
105645 assert( pList!=0 )((void) (0));
105646 for(i=0; i<pList->nExpr; i++){
105647 Expr *pExpr = pList->a[i].pExpr;
105648 assert( pExpr!=0 )((void) (0));
105649 m |= pExpr->flags;
105650 }
105651 return m;
105652}
105653
105654/*
105655** This is a SELECT-node callback for the expression walker that
105656** always "fails". By "fail" in this case, we mean set
105657** pWalker->eCode to zero and abort.
105658**
105659** This callback is used by multiple expression walkers.
105660*/
105661SQLITE_PRIVATEstatic int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){
105662 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
105663 pWalker->eCode = 0;
105664 return WRC_Abort2;
105665}
105666
105667/*
105668** Check the input string to see if it is "true" or "false" (in any case).
105669**
105670** If the string is.... Return
105671** "true" EP_IsTrue
105672** "false" EP_IsFalse
105673** anything else 0
105674*/
105675SQLITE_PRIVATEstatic u32 sqlite3IsTrueOrFalse(const char *zIn){
105676 if( sqlite3StrICmp(zIn, "true")==0 ) return EP_IsTrue0x10000000;
105677 if( sqlite3StrICmp(zIn, "false")==0 ) return EP_IsFalse0x20000000;
105678 return 0;
105679}
105680
105681
105682/*
105683** If the input expression is an ID with the name "true" or "false"
105684** then convert it into an TK_TRUEFALSE term. Return non-zero if
105685** the conversion happened, and zero if the expression is unaltered.
105686*/
105687SQLITE_PRIVATEstatic int sqlite3ExprIdToTrueFalse(Expr *pExpr){
105688 u32 v;
105689 assert( pExpr->op==TK_ID || pExpr->op==TK_STRING )((void) (0));
105690 if( !ExprHasProperty(pExpr, EP_Quoted|EP_IntValue)(((pExpr)->flags&(0x4000000|0x000800))!=0)
105691 && (v = sqlite3IsTrueOrFalse(pExpr->u.zToken))!=0
105692 ){
105693 pExpr->op = TK_TRUEFALSE170;
105694 ExprSetProperty(pExpr, v)(pExpr)->flags|=(v);
105695 return 1;
105696 }
105697 return 0;
105698}
105699
105700/*
105701** The argument must be a TK_TRUEFALSE Expr node. Return 1 if it is TRUE
105702** and 0 if it is FALSE.
105703*/
105704SQLITE_PRIVATEstatic int sqlite3ExprTruthValue(const Expr *pExpr){
105705 pExpr = sqlite3ExprSkipCollate((Expr*)pExpr);
105706 assert( pExpr->op==TK_TRUEFALSE )((void) (0));
105707 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
105708 assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0((void) (0))
105709 || sqlite3StrICmp(pExpr->u.zToken,"false")==0 )((void) (0));
105710 return pExpr->u.zToken[4]==0;
105711}
105712
105713/*
105714** If pExpr is an AND or OR expression, try to simplify it by eliminating
105715** terms that are always true or false. Return the simplified expression.
105716** Or return the original expression if no simplification is possible.
105717**
105718** Examples:
105719**
105720** (x<10) AND true => (x<10)
105721** (x<10) AND false => false
105722** (x<10) AND (y=22 OR false) => (x<10) AND (y=22)
105723** (x<10) AND (y=22 OR true) => (x<10)
105724** (y=22) OR true => true
105725*/
105726SQLITE_PRIVATEstatic Expr *sqlite3ExprSimplifiedAndOr(Expr *pExpr){
105727 assert( pExpr!=0 )((void) (0));
105728 if( pExpr->op==TK_AND44 || pExpr->op==TK_OR43 ){
105729 Expr *pRight = sqlite3ExprSimplifiedAndOr(pExpr->pRight);
105730 Expr *pLeft = sqlite3ExprSimplifiedAndOr(pExpr->pLeft);
105731 if( ExprAlwaysTrue(pLeft)(((pLeft)->flags&(0x000001|0x10000000))==0x10000000) || ExprAlwaysFalse(pRight)(((pRight)->flags&(0x000001|0x20000000))==0x20000000) ){
105732 pExpr = pExpr->op==TK_AND44 ? pRight : pLeft;
105733 }else if( ExprAlwaysTrue(pRight)(((pRight)->flags&(0x000001|0x10000000))==0x10000000) || ExprAlwaysFalse(pLeft)(((pLeft)->flags&(0x000001|0x20000000))==0x20000000) ){
105734 pExpr = pExpr->op==TK_AND44 ? pLeft : pRight;
105735 }
105736 }
105737 return pExpr;
105738}
105739
105740
105741/*
105742** These routines are Walker callbacks used to check expressions to
105743** see if they are "constant" for some definition of constant. The
105744** Walker.eCode value determines the type of "constant" we are looking
105745** for.
105746**
105747** These callback routines are used to implement the following:
105748**
105749** sqlite3ExprIsConstant() pWalker->eCode==1
105750** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
105751** sqlite3ExprIsTableConstant() pWalker->eCode==3
105752** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
105753**
105754** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
105755** is found to not be a constant.
105756**
105757** The sqlite3ExprIsConstantOrFunction() is used for evaluating DEFAULT
105758** expressions in a CREATE TABLE statement. The Walker.eCode value is 5
105759** when parsing an existing schema out of the sqlite_schema table and 4
105760** when processing a new CREATE TABLE statement. A bound parameter raises
105761** an error for new statements, but is silently converted
105762** to NULL for existing schemas. This allows sqlite_schema tables that
105763** contain a bound parameter because they were generated by older versions
105764** of SQLite to be parsed by newer versions of SQLite without raising a
105765** malformed schema error.
105766*/
105767static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
105768
105769 /* If pWalker->eCode is 2 then any term of the expression that comes from
105770 ** the ON or USING clauses of an outer join disqualifies the expression
105771 ** from being considered constant. */
105772 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(0x000001))!=0) ){
105773 pWalker->eCode = 0;
105774 return WRC_Abort2;
105775 }
105776
105777 switch( pExpr->op ){
105778 /* Consider functions to be constant if all their arguments are constant
105779 ** and either pWalker->eCode==4 or 5 or the function has the
105780 ** SQLITE_FUNC_CONST flag. */
105781 case TK_FUNCTION172:
105782 if( (pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc)(((pExpr)->flags&(0x100000))!=0))
105783 && !ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0)
105784 ){
105785 if( pWalker->eCode==5 ) ExprSetProperty(pExpr, EP_FromDDL)(pExpr)->flags|=(0x40000000);
105786 return WRC_Continue0;
105787 }else{
105788 pWalker->eCode = 0;
105789 return WRC_Abort2;
105790 }
105791 case TK_ID59:
105792 /* Convert "true" or "false" in a DEFAULT clause into the
105793 ** appropriate TK_TRUEFALSE operator */
105794 if( sqlite3ExprIdToTrueFalse(pExpr) ){
105795 return WRC_Prune1;
105796 }
105797 /* no break */ deliberate_fall_through
105798 case TK_COLUMN167:
105799 case TK_AGG_FUNCTION168:
105800 case TK_AGG_COLUMN169:
105801 testcase( pExpr->op==TK_ID );
105802 testcase( pExpr->op==TK_COLUMN );
105803 testcase( pExpr->op==TK_AGG_FUNCTION );
105804 testcase( pExpr->op==TK_AGG_COLUMN );
105805 if( ExprHasProperty(pExpr, EP_FixedCol)(((pExpr)->flags&(0x000020))!=0) && pWalker->eCode!=2 ){
105806 return WRC_Continue0;
105807 }
105808 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
105809 return WRC_Continue0;
105810 }
105811 /* no break */ deliberate_fall_through
105812 case TK_IF_NULL_ROW179:
105813 case TK_REGISTER176:
105814 case TK_DOT141:
105815 testcase( pExpr->op==TK_REGISTER );
105816 testcase( pExpr->op==TK_IF_NULL_ROW );
105817 testcase( pExpr->op==TK_DOT );
105818 pWalker->eCode = 0;
105819 return WRC_Abort2;
105820 case TK_VARIABLE156:
105821 if( pWalker->eCode==5 ){
105822 /* Silently convert bound parameters that appear inside of CREATE
105823 ** statements into a NULL when parsing the CREATE statement text out
105824 ** of the sqlite_schema table */
105825 pExpr->op = TK_NULL121;
105826 }else if( pWalker->eCode==4 ){
105827 /* A bound parameter in a CREATE statement that originates from
105828 ** sqlite3_prepare() causes an error */
105829 pWalker->eCode = 0;
105830 return WRC_Abort2;
105831 }
105832 /* no break */ deliberate_fall_through
105833 default:
105834 testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */
105835 testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */
105836 return WRC_Continue0;
105837 }
105838}
105839static int exprIsConst(Expr *p, int initFlag, int iCur){
105840 Walker w;
105841 w.eCode = initFlag;
105842 w.xExprCallback = exprNodeIsConstant;
105843 w.xSelectCallback = sqlite3SelectWalkFail;
105844#ifdef SQLITE_DEBUG
105845 w.xSelectCallback2 = sqlite3SelectWalkAssert2;
105846#endif
105847 w.u.iCur = iCur;
105848 sqlite3WalkExpr(&w, p);
105849 return w.eCode;
105850}
105851
105852/*
105853** Walk an expression tree. Return non-zero if the expression is constant
105854** and 0 if it involves variables or function calls.
105855**
105856** For the purposes of this function, a double-quoted string (ex: "abc")
105857** is considered a variable but a single-quoted string (ex: 'abc') is
105858** a constant.
105859*/
105860SQLITE_PRIVATEstatic int sqlite3ExprIsConstant(Expr *p){
105861 return exprIsConst(p, 1, 0);
105862}
105863
105864/*
105865** Walk an expression tree. Return non-zero if
105866**
105867** (1) the expression is constant, and
105868** (2) the expression does originate in the ON or USING clause
105869** of a LEFT JOIN, and
105870** (3) the expression does not contain any EP_FixedCol TK_COLUMN
105871** operands created by the constant propagation optimization.
105872**
105873** When this routine returns true, it indicates that the expression
105874** can be added to the pParse->pConstExpr list and evaluated once when
105875** the prepared statement starts up. See sqlite3ExprCodeRunJustOnce().
105876*/
105877SQLITE_PRIVATEstatic int sqlite3ExprIsConstantNotJoin(Expr *p){
105878 return exprIsConst(p, 2, 0);
105879}
105880
105881/*
105882** Walk an expression tree. Return non-zero if the expression is constant
105883** for any single row of the table with cursor iCur. In other words, the
105884** expression must not refer to any non-deterministic function nor any
105885** table other than iCur.
105886*/
105887SQLITE_PRIVATEstatic int sqlite3ExprIsTableConstant(Expr *p, int iCur){
105888 return exprIsConst(p, 3, iCur);
105889}
105890
105891/*
105892** Check pExpr to see if it is an invariant constraint on data source pSrc.
105893** This is an optimization. False negatives will perhaps cause slower
105894** queries, but false positives will yield incorrect answers. So when in
105895** doubt, return 0.
105896**
105897** To be an invariant constraint, the following must be true:
105898**
105899** (1) pExpr cannot refer to any table other than pSrc->iCursor.
105900**
105901** (2) pExpr cannot use subqueries or non-deterministic functions.
105902**
105903** (3) pSrc cannot be part of the left operand for a RIGHT JOIN.
105904** (Is there some way to relax this constraint?)
105905**
105906** (4) If pSrc is the right operand of a LEFT JOIN, then...
105907** (4a) pExpr must come from an ON clause..
105908 (4b) and specifically the ON clause associated with the LEFT JOIN.
105909**
105910** (5) If pSrc is not the right operand of a LEFT JOIN or the left
105911** operand of a RIGHT JOIN, then pExpr must be from the WHERE
105912** clause, not an ON clause.
105913*/
105914SQLITE_PRIVATEstatic int sqlite3ExprIsTableConstraint(Expr *pExpr, const SrcItem *pSrc){
105915 if( pSrc->fg.jointype & JT_LTORJ0x40 ){
105916 return 0; /* rule (3) */
105917 }
105918 if( pSrc->fg.jointype & JT_LEFT0x08 ){
105919 if( !ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(0x000001))!=0) ) return 0; /* rule (4a) */
105920 if( pExpr->w.iJoin!=pSrc->iCursor ) return 0; /* rule (4b) */
105921 }else{
105922 if( ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(0x000001))!=0) ) return 0; /* rule (5) */
105923 }
105924 return sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor); /* rules (1), (2) */
105925}
105926
105927
105928/*
105929** sqlite3WalkExpr() callback used by sqlite3ExprIsConstantOrGroupBy().
105930*/
105931static int exprNodeIsConstantOrGroupBy(Walker *pWalker, Expr *pExpr){
105932 ExprList *pGroupBy = pWalker->u.pGroupBy;
105933 int i;
105934
105935 /* Check if pExpr is identical to any GROUP BY term. If so, consider
105936 ** it constant. */
105937 for(i=0; i<pGroupBy->nExpr; i++){
105938 Expr *p = pGroupBy->a[i].pExpr;
105939 if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
105940 CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p);
105941 if( sqlite3IsBinary(pColl) ){
105942 return WRC_Prune1;
105943 }
105944 }
105945 }
105946
105947 /* Check if pExpr is a sub-select. If so, consider it variable. */
105948 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
105949 pWalker->eCode = 0;
105950 return WRC_Abort2;
105951 }
105952
105953 return exprNodeIsConstant(pWalker, pExpr);
105954}
105955
105956/*
105957** Walk the expression tree passed as the first argument. Return non-zero
105958** if the expression consists entirely of constants or copies of terms
105959** in pGroupBy that sort with the BINARY collation sequence.
105960**
105961** This routine is used to determine if a term of the HAVING clause can
105962** be promoted into the WHERE clause. In order for such a promotion to work,
105963** the value of the HAVING clause term must be the same for all members of
105964** a "group". The requirement that the GROUP BY term must be BINARY
105965** assumes that no other collating sequence will have a finer-grained
105966** grouping than binary. In other words (A=B COLLATE binary) implies
105967** A=B in every other collating sequence. The requirement that the
105968** GROUP BY be BINARY is stricter than necessary. It would also work
105969** to promote HAVING clauses that use the same alternative collating
105970** sequence as the GROUP BY term, but that is much harder to check,
105971** alternative collating sequences are uncommon, and this is only an
105972** optimization, so we take the easy way out and simply require the
105973** GROUP BY to use the BINARY collating sequence.
105974*/
105975SQLITE_PRIVATEstatic int sqlite3ExprIsConstantOrGroupBy(Parse *pParse, Expr *p, ExprList *pGroupBy){
105976 Walker w;
105977 w.eCode = 1;
105978 w.xExprCallback = exprNodeIsConstantOrGroupBy;
105979 w.xSelectCallback = 0;
105980 w.u.pGroupBy = pGroupBy;
105981 w.pParse = pParse;
105982 sqlite3WalkExpr(&w, p);
105983 return w.eCode;
105984}
105985
105986/*
105987** Walk an expression tree for the DEFAULT field of a column definition
105988** in a CREATE TABLE statement. Return non-zero if the expression is
105989** acceptable for use as a DEFAULT. That is to say, return non-zero if
105990** the expression is constant or a function call with constant arguments.
105991** Return and 0 if there are any variables.
105992**
105993** isInit is true when parsing from sqlite_schema. isInit is false when
105994** processing a new CREATE TABLE statement. When isInit is true, parameters
105995** (such as ? or $abc) in the expression are converted into NULL. When
105996** isInit is false, parameters raise an error. Parameters should not be
105997** allowed in a CREATE TABLE statement, but some legacy versions of SQLite
105998** allowed it, so we need to support it when reading sqlite_schema for
105999** backwards compatibility.
106000**
106001** If isInit is true, set EP_FromDDL on every TK_FUNCTION node.
106002**
106003** For the purposes of this function, a double-quoted string (ex: "abc")
106004** is considered a variable but a single-quoted string (ex: 'abc') is
106005** a constant.
106006*/
106007SQLITE_PRIVATEstatic int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
106008 assert( isInit==0 || isInit==1 )((void) (0));
106009 return exprIsConst(p, 4+isInit, 0);
106010}
106011
106012#ifdef SQLITE_ENABLE_CURSOR_HINTS
106013/*
106014** Walk an expression tree. Return 1 if the expression contains a
106015** subquery of some kind. Return 0 if there are no subqueries.
106016*/
106017SQLITE_PRIVATEstatic int sqlite3ExprContainsSubquery(Expr *p){
106018 Walker w;
106019 w.eCode = 1;
106020 w.xExprCallback = sqlite3ExprWalkNoop;
106021 w.xSelectCallback = sqlite3SelectWalkFail;
106022#ifdef SQLITE_DEBUG
106023 w.xSelectCallback2 = sqlite3SelectWalkAssert2;
106024#endif
106025 sqlite3WalkExpr(&w, p);
106026 return w.eCode==0;
106027}
106028#endif
106029
106030/*
106031** If the expression p codes a constant integer that is small enough
106032** to fit in a 32-bit integer, return 1 and put the value of the integer
106033** in *pValue. If the expression is not an integer or if it is too big
106034** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
106035*/
106036SQLITE_PRIVATEstatic int sqlite3ExprIsInteger(const Expr *p, int *pValue){
106037 int rc = 0;
106038 if( NEVER(p==0)(p==0) ) return 0; /* Used to only happen following on OOM */
106039
106040 /* If an expression is an integer literal that fits in a signed 32-bit
106041 ** integer, then the EP_IntValue flag will have already been set */
106042 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0((void) (0))
106043 || sqlite3GetInt32(p->u.zToken, &rc)==0 )((void) (0));
106044
106045 if( p->flags & EP_IntValue0x000800 ){
106046 *pValue = p->u.iValue;
106047 return 1;
106048 }
106049 switch( p->op ){
106050 case TK_UPLUS174: {
106051 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
106052 break;
106053 }
106054 case TK_UMINUS173: {
106055 int v = 0;
106056 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
106057 assert( ((unsigned int)v)!=0x80000000 )((void) (0));
106058 *pValue = -v;
106059 rc = 1;
106060 }
106061 break;
106062 }
106063 default: break;
106064 }
106065 return rc;
106066}
106067
106068/*
106069** Return FALSE if there is no chance that the expression can be NULL.
106070**
106071** If the expression might be NULL or if the expression is too complex
106072** to tell return TRUE.
106073**
106074** This routine is used as an optimization, to skip OP_IsNull opcodes
106075** when we know that a value cannot be NULL. Hence, a false positive
106076** (returning TRUE when in fact the expression can never be NULL) might
106077** be a small performance hit but is otherwise harmless. On the other
106078** hand, a false negative (returning FALSE when the result could be NULL)
106079** will likely result in an incorrect answer. So when in doubt, return
106080** TRUE.
106081*/
106082SQLITE_PRIVATEstatic int sqlite3ExprCanBeNull(const Expr *p){
106083 u8 op;
106084 assert( p!=0 )((void) (0));
106085 while( p->op==TK_UPLUS174 || p->op==TK_UMINUS173 ){
106086 p = p->pLeft;
106087 assert( p!=0 )((void) (0));
106088 }
106089 op = p->op;
106090 if( op==TK_REGISTER176 ) op = p->op2;
106091 switch( op ){
106092 case TK_INTEGER155:
106093 case TK_STRING117:
106094 case TK_FLOAT153:
106095 case TK_BLOB154:
106096 return 0;
106097 case TK_COLUMN167:
106098 assert( ExprUseYTab(p) )((void) (0));
106099 return ExprHasProperty(p, EP_CanBeNull)(((p)->flags&(0x200000))!=0) ||
106100 p->y.pTab==0 || /* Reference to column of index on expression */
106101 (p->iColumn>=0
106102 && p->y.pTab->aCol!=0 /* Possible due to prior error */
106103 && p->y.pTab->aCol[p->iColumn].notNull==0);
106104 default:
106105 return 1;
106106 }
106107}
106108
106109/*
106110** Return TRUE if the given expression is a constant which would be
106111** unchanged by OP_Affinity with the affinity given in the second
106112** argument.
106113**
106114** This routine is used to determine if the OP_Affinity operation
106115** can be omitted. When in doubt return FALSE. A false negative
106116** is harmless. A false positive, however, can result in the wrong
106117** answer.
106118*/
106119SQLITE_PRIVATEstatic int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
106120 u8 op;
106121 int unaryMinus = 0;
106122 if( aff==SQLITE_AFF_BLOB0x41 ) return 1;
106123 while( p->op==TK_UPLUS174 || p->op==TK_UMINUS173 ){
106124 if( p->op==TK_UMINUS173 ) unaryMinus = 1;
106125 p = p->pLeft;
106126 }
106127 op = p->op;
106128 if( op==TK_REGISTER176 ) op = p->op2;
106129 switch( op ){
106130 case TK_INTEGER155: {
106131 return aff>=SQLITE_AFF_NUMERIC0x43;
106132 }
106133 case TK_FLOAT153: {
106134 return aff>=SQLITE_AFF_NUMERIC0x43;
106135 }
106136 case TK_STRING117: {
106137 return !unaryMinus && aff==SQLITE_AFF_TEXT0x42;
106138 }
106139 case TK_BLOB154: {
106140 return !unaryMinus;
106141 }
106142 case TK_COLUMN167: {
106143 assert( p->iTable>=0 )((void) (0)); /* p cannot be part of a CHECK constraint */
106144 return aff>=SQLITE_AFF_NUMERIC0x43 && p->iColumn<0;
106145 }
106146 default: {
106147 return 0;
106148 }
106149 }
106150}
106151
106152/*
106153** Return TRUE if the given string is a row-id column name.
106154*/
106155SQLITE_PRIVATEstatic int sqlite3IsRowid(const char *z){
106156 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
106157 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
106158 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
106159 return 0;
106160}
106161
106162/*
106163** pX is the RHS of an IN operator. If pX is a SELECT statement
106164** that can be simplified to a direct table access, then return
106165** a pointer to the SELECT statement. If pX is not a SELECT statement,
106166** or if the SELECT statement needs to be manifested into a transient
106167** table, then return NULL.
106168*/
106169#ifndef SQLITE_OMIT_SUBQUERY
106170static Select *isCandidateForInOpt(const Expr *pX){
106171 Select *p;
106172 SrcList *pSrc;
106173 ExprList *pEList;
106174 Table *pTab;
106175 int i;
106176 if( !ExprUseXSelect(pX)(((pX)->flags&0x001000)!=0) ) return 0; /* Not a subquery */
106177 if( ExprHasProperty(pX, EP_VarSelect)(((pX)->flags&(0x000040))!=0) ) return 0; /* Correlated subq */
106178 p = pX->x.pSelect;
106179 if( p->pPrior ) return 0; /* Not a compound SELECT */
106180 if( p->selFlags & (SF_Distinct0x0000001|SF_Aggregate0x0000008) ){
106181 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
106182 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
106183 return 0; /* No DISTINCT keyword and no aggregate functions */
106184 }
106185 assert( p->pGroupBy==0 )((void) (0)); /* Has no GROUP BY clause */
106186 if( p->pLimit ) return 0; /* Has no LIMIT clause */
106187 if( p->pWhere ) return 0; /* Has no WHERE clause */
106188 pSrc = p->pSrc;
106189 assert( pSrc!=0 )((void) (0));
106190 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
106191 if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */
106192 pTab = pSrc->a[0].pTab;
106193 assert( pTab!=0 )((void) (0));
106194 assert( !IsView(pTab) )((void) (0)); /* FROM clause is not a view */
106195 if( IsVirtual(pTab)((pTab)->eTabType==1) ) return 0; /* FROM clause not a virtual table */
106196 pEList = p->pEList;
106197 assert( pEList!=0 )((void) (0));
106198 /* All SELECT results must be columns. */
106199 for(i=0; i<pEList->nExpr; i++){
106200 Expr *pRes = pEList->a[i].pExpr;
106201 if( pRes->op!=TK_COLUMN167 ) return 0;
106202 assert( pRes->iTable==pSrc->a[0].iCursor )((void) (0)); /* Not a correlated subquery */
106203 }
106204 return p;
106205}
106206#endif /* SQLITE_OMIT_SUBQUERY */
106207
106208#ifndef SQLITE_OMIT_SUBQUERY
106209/*
106210** Generate code that checks the left-most column of index table iCur to see if
106211** it contains any NULL entries. Cause the register at regHasNull to be set
106212** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
106213** to be set to NULL if iCur contains one or more NULL values.
106214*/
106215static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
106216 int addr1;
106217 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regHasNull);
106218 addr1 = sqlite3VdbeAddOp1(v, OP_Rewind35, iCur); VdbeCoverage(v);
106219 sqlite3VdbeAddOp3(v, OP_Column94, iCur, 0, regHasNull);
106220 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG0x80);
106221 VdbeComment((v, "first_entry_in(%d)", iCur));
106222 sqlite3VdbeJumpHere(v, addr1);
106223}
106224#endif
106225
106226
106227#ifndef SQLITE_OMIT_SUBQUERY
106228/*
106229** The argument is an IN operator with a list (not a subquery) on the
106230** right-hand side. Return TRUE if that list is constant.
106231*/
106232static int sqlite3InRhsIsConstant(Expr *pIn){
106233 Expr *pLHS;
106234 int res;
106235 assert( !ExprHasProperty(pIn, EP_xIsSelect) )((void) (0));
106236 pLHS = pIn->pLeft;
106237 pIn->pLeft = 0;
106238 res = sqlite3ExprIsConstant(pIn);
106239 pIn->pLeft = pLHS;
106240 return res;
106241}
106242#endif
106243
106244/*
106245** This function is used by the implementation of the IN (...) operator.
106246** The pX parameter is the expression on the RHS of the IN operator, which
106247** might be either a list of expressions or a subquery.
106248**
106249** The job of this routine is to find or create a b-tree object that can
106250** be used either to test for membership in the RHS set or to iterate through
106251** all members of the RHS set, skipping duplicates.
106252**
106253** A cursor is opened on the b-tree object that is the RHS of the IN operator
106254** and the *piTab parameter is set to the index of that cursor.
106255**
106256** The returned value of this function indicates the b-tree type, as follows:
106257**
106258** IN_INDEX_ROWID - The cursor was opened on a database table.
106259** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
106260** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
106261** IN_INDEX_EPH - The cursor was opened on a specially created and
106262** populated epheremal table.
106263** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
106264** implemented as a sequence of comparisons.
106265**
106266** An existing b-tree might be used if the RHS expression pX is a simple
106267** subquery such as:
106268**
106269** SELECT <column1>, <column2>... FROM <table>
106270**
106271** If the RHS of the IN operator is a list or a more complex subquery, then
106272** an ephemeral table might need to be generated from the RHS and then
106273** pX->iTable made to point to the ephemeral table instead of an
106274** existing table. In this case, the creation and initialization of the
106275** ephmeral table might be put inside of a subroutine, the EP_Subrtn flag
106276** will be set on pX and the pX->y.sub fields will be set to show where
106277** the subroutine is coded.
106278**
106279** The inFlags parameter must contain, at a minimum, one of the bits
106280** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP but not both. If inFlags contains
106281** IN_INDEX_MEMBERSHIP, then the generated table will be used for a fast
106282** membership test. When the IN_INDEX_LOOP bit is set, the IN index will
106283** be used to loop over all values of the RHS of the IN operator.
106284**
106285** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
106286** through the set members) then the b-tree must not contain duplicates.
106287** An epheremal table will be created unless the selected columns are guaranteed
106288** to be unique - either because it is an INTEGER PRIMARY KEY or due to
106289** a UNIQUE constraint or index.
106290**
106291** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
106292** for fast set membership tests) then an epheremal table must
106293** be used unless <columns> is a single INTEGER PRIMARY KEY column or an
106294** index can be found with the specified <columns> as its left-most.
106295**
106296** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and
106297** if the RHS of the IN operator is a list (not a subquery) then this
106298** routine might decide that creating an ephemeral b-tree for membership
106299** testing is too expensive and return IN_INDEX_NOOP. In that case, the
106300** calling routine should implement the IN operator using a sequence
106301** of Eq or Ne comparison operations.
106302**
106303** When the b-tree is being used for membership tests, the calling function
106304** might need to know whether or not the RHS side of the IN operator
106305** contains a NULL. If prRhsHasNull is not a NULL pointer and
106306** if there is any chance that the (...) might contain a NULL value at
106307** runtime, then a register is allocated and the register number written
106308** to *prRhsHasNull. If there is no chance that the (...) contains a
106309** NULL value, then *prRhsHasNull is left unchanged.
106310**
106311** If a register is allocated and its location stored in *prRhsHasNull, then
106312** the value in that register will be NULL if the b-tree contains one or more
106313** NULL values, and it will be some non-NULL value if the b-tree contains no
106314** NULL values.
106315**
106316** If the aiMap parameter is not NULL, it must point to an array containing
106317** one element for each column returned by the SELECT statement on the RHS
106318** of the IN(...) operator. The i'th entry of the array is populated with the
106319** offset of the index column that matches the i'th column returned by the
106320** SELECT. For example, if the expression and selected index are:
106321**
106322** (?,?,?) IN (SELECT a, b, c FROM t1)
106323** CREATE INDEX i1 ON t1(b, c, a);
106324**
106325** then aiMap[] is populated with {2, 0, 1}.
106326*/
106327#ifndef SQLITE_OMIT_SUBQUERY
106328SQLITE_PRIVATEstatic int sqlite3FindInIndex(
106329 Parse *pParse, /* Parsing context */
106330 Expr *pX, /* The IN expression */
106331 u32 inFlags, /* IN_INDEX_LOOP, _MEMBERSHIP, and/or _NOOP_OK */
106332 int *prRhsHasNull, /* Register holding NULL status. See notes */
106333 int *aiMap, /* Mapping from Index fields to RHS fields */
106334 int *piTab /* OUT: index to use */
106335){
106336 Select *p; /* SELECT to the right of IN operator */
106337 int eType = 0; /* Type of RHS table. IN_INDEX_* */
106338 int iTab; /* Cursor of the RHS table */
106339 int mustBeUnique; /* True if RHS must be unique */
106340 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
106341
106342 assert( pX->op==TK_IN )((void) (0));
106343 mustBeUnique = (inFlags & IN_INDEX_LOOP0x0004)!=0;
106344 iTab = pParse->nTab++;
106345
106346 /* If the RHS of this IN(...) operator is a SELECT, and if it matters
106347 ** whether or not the SELECT result contains NULL values, check whether
106348 ** or not NULL is actually possible (it may not be, for example, due
106349 ** to NOT NULL constraints in the schema). If no NULL values are possible,
106350 ** set prRhsHasNull to 0 before continuing. */
106351 if( prRhsHasNull && ExprUseXSelect(pX)(((pX)->flags&0x001000)!=0) ){
106352 int i;
106353 ExprList *pEList = pX->x.pSelect->pEList;
106354 for(i=0; i<pEList->nExpr; i++){
106355 if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break;
106356 }
106357 if( i==pEList->nExpr ){
106358 prRhsHasNull = 0;
106359 }
106360 }
106361
106362 /* Check to see if an existing table or index can be used to
106363 ** satisfy the query. This is preferable to generating a new
106364 ** ephemeral table. */
106365 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
106366 sqlite3 *db = pParse->db; /* Database connection */
106367 Table *pTab; /* Table <table>. */
106368 int iDb; /* Database idx for pTab */
106369 ExprList *pEList = p->pEList;
106370 int nExpr = pEList->nExpr;
106371
106372 assert( p->pEList!=0 )((void) (0)); /* Because of isCandidateForInOpt(p) */
106373 assert( p->pEList->a[0].pExpr!=0 )((void) (0)); /* Because of isCandidateForInOpt(p) */
106374 assert( p->pSrc!=0 )((void) (0)); /* Because of isCandidateForInOpt(p) */
106375 pTab = p->pSrc->a[0].pTab;
106376
106377 /* Code an OP_Transaction and OP_TableLock for <table>. */
106378 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
106379 assert( iDb>=0 && iDb<SQLITE_MAX_DB )((void) (0));
106380 sqlite3CodeVerifySchema(pParse, iDb);
106381 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
106382
106383 assert(v)((void) (0)); /* sqlite3GetVdbe() has always been previously called */
106384 if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
106385 /* The "x IN (SELECT rowid FROM table)" case */
106386 int iAddr = sqlite3VdbeAddOp0(v, OP_Once14);
106387 VdbeCoverage(v);
106388
106389 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead112);
106390 eType = IN_INDEX_ROWID1;
106391 ExplainQueryPlan((pParse, 0,sqlite3VdbeExplain (pParse, 0, "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR"
,pTab->zName)
106392 "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)
;
106393 sqlite3VdbeJumpHere(v, iAddr);
106394 }else{
106395 Index *pIdx; /* Iterator variable */
106396 int affinity_ok = 1;
106397 int i;
106398
106399 /* Check that the affinity that will be used to perform each
106400 ** comparison is the same as the affinity of each column in table
106401 ** on the RHS of the IN operator. If it not, it is not possible to
106402 ** use any index of the RHS table. */
106403 for(i=0; i<nExpr && affinity_ok; i++){
106404 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
106405 int iCol = pEList->a[i].pExpr->iColumn;
106406 char idxaff = sqlite3TableColumnAffinity(pTab,iCol); /* RHS table */
106407 char cmpaff = sqlite3CompareAffinity(pLhs, idxaff);
106408 testcase( cmpaff==SQLITE_AFF_BLOB );
106409 testcase( cmpaff==SQLITE_AFF_TEXT );
106410 switch( cmpaff ){
106411 case SQLITE_AFF_BLOB0x41:
106412 break;
106413 case SQLITE_AFF_TEXT0x42:
106414 /* sqlite3CompareAffinity() only returns TEXT if one side or the
106415 ** other has no affinity and the other side is TEXT. Hence,
106416 ** the only way for cmpaff to be TEXT is for idxaff to be TEXT
106417 ** and for the term on the LHS of the IN to have no affinity. */
106418 assert( idxaff==SQLITE_AFF_TEXT )((void) (0));
106419 break;
106420 default:
106421 affinity_ok = sqlite3IsNumericAffinity(idxaff)((idxaff)>=0x43);
106422 }
106423 }
106424
106425 if( affinity_ok ){
106426 /* Search for an existing index that will work for this IN operator */
106427 for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
106428 Bitmask colUsed; /* Columns of the index used */
106429 Bitmask mCol; /* Mask for the current column */
106430 if( pIdx->nColumn<nExpr ) continue;
106431 if( pIdx->pPartIdxWhere!=0 ) continue;
106432 /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
106433 ** BITMASK(nExpr) without overflowing */
106434 testcase( pIdx->nColumn==BMS-2 );
106435 testcase( pIdx->nColumn==BMS-1 );
106436 if( pIdx->nColumn>=BMS((int)(sizeof(Bitmask)*8))-1 ) continue;
106437 if( mustBeUnique ){
106438 if( pIdx->nKeyCol>nExpr
106439 ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx)((pIdx)->onError!=0))
106440 ){
106441 continue; /* This index is not unique over the IN RHS columns */
106442 }
106443 }
106444
106445 colUsed = 0; /* Columns of index used so far */
106446 for(i=0; i<nExpr; i++){
106447 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
106448 Expr *pRhs = pEList->a[i].pExpr;
106449 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs);
106450 int j;
106451
106452 assert( pReq!=0 || pRhs->iColumn==XN_ROWID || pParse->nErr )((void) (0));
106453 for(j=0; j<nExpr; j++){
106454 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
106455 assert( pIdx->azColl[j] )((void) (0));
106456 if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){
106457 continue;
106458 }
106459 break;
106460 }
106461 if( j==nExpr ) break;
106462 mCol = MASKBIT(j)(((Bitmask)1)<<(j));
106463 if( mCol & colUsed ) break; /* Each column used only once */
106464 colUsed |= mCol;
106465 if( aiMap ) aiMap[i] = j;
106466 }
106467
106468 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) )((void) (0));
106469 if( colUsed==(MASKBIT(nExpr)(((Bitmask)1)<<(nExpr))-1) ){
106470 /* If we reach this point, that means the index pIdx is usable */
106471 int iAddr = sqlite3VdbeAddOp0(v, OP_Once14); VdbeCoverage(v);
106472 ExplainQueryPlan((pParse, 0,sqlite3VdbeExplain (pParse, 0, "USING INDEX %s FOR IN-OPERATOR"
,pIdx->zName)
106473 "USING INDEX %s FOR IN-OPERATOR",pIdx->zName))sqlite3VdbeExplain (pParse, 0, "USING INDEX %s FOR IN-OPERATOR"
,pIdx->zName)
;
106474 sqlite3VdbeAddOp3(v, OP_OpenRead112, iTab, pIdx->tnum, iDb);
106475 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
106476 VdbeComment((v, "%s", pIdx->zName));
106477 assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 )((void) (0));
106478 eType = IN_INDEX_INDEX_ASC3 + pIdx->aSortOrder[0];
106479
106480 if( prRhsHasNull ){
106481#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
106482 i64 mask = (1<<nExpr)-1;
106483 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed123,
106484 iTab, 0, 0, (u8*)&mask, P4_INT64(-13));
106485#endif
106486 *prRhsHasNull = ++pParse->nMem;
106487 if( nExpr==1 ){
106488 sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull);
106489 }
106490 }
106491 sqlite3VdbeJumpHere(v, iAddr);
106492 }
106493 } /* End loop over indexes */
106494 } /* End if( affinity_ok ) */
106495 } /* End if not an rowid index */
106496 } /* End attempt to optimize using an index */
106497
106498 /* If no preexisting index is available for the IN clause
106499 ** and IN_INDEX_NOOP is an allowed reply
106500 ** and the RHS of the IN operator is a list, not a subquery
106501 ** and the RHS is not constant or has two or fewer terms,
106502 ** then it is not worth creating an ephemeral table to evaluate
106503 ** the IN operator so return IN_INDEX_NOOP.
106504 */
106505 if( eType==0
106506 && (inFlags & IN_INDEX_NOOP_OK0x0001)
106507 && ExprUseXList(pX)(((pX)->flags&0x001000)==0)
106508 && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
106509 ){
106510 pParse->nTab--; /* Back out the allocation of the unused cursor */
106511 iTab = -1; /* Cursor is not allocated */
106512 eType = IN_INDEX_NOOP5;
106513 }
106514
106515 if( eType==0 ){
106516 /* Could not find an existing table or index to use as the RHS b-tree.
106517 ** We will have to generate an ephemeral table to do the job.
106518 */
106519 u32 savedNQueryLoop = pParse->nQueryLoop;
106520 int rMayHaveNull = 0;
106521 eType = IN_INDEX_EPH2;
106522 if( inFlags & IN_INDEX_LOOP0x0004 ){
106523 pParse->nQueryLoop = 0;
106524 }else if( prRhsHasNull ){
106525 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
106526 }
106527 assert( pX->op==TK_IN )((void) (0));
106528 sqlite3CodeRhsOfIN(pParse, pX, iTab);
106529 if( rMayHaveNull ){
106530 sqlite3SetHasNullFlag(v, iTab, rMayHaveNull);
106531 }
106532 pParse->nQueryLoop = savedNQueryLoop;
106533 }
106534
106535 if( aiMap && eType!=IN_INDEX_INDEX_ASC3 && eType!=IN_INDEX_INDEX_DESC4 ){
106536 int i, n;
106537 n = sqlite3ExprVectorSize(pX->pLeft);
106538 for(i=0; i<n; i++) aiMap[i] = i;
106539 }
106540 *piTab = iTab;
106541 return eType;
106542}
106543#endif
106544
106545#ifndef SQLITE_OMIT_SUBQUERY
106546/*
106547** Argument pExpr is an (?, ?...) IN(...) expression. This
106548** function allocates and returns a nul-terminated string containing
106549** the affinities to be used for each column of the comparison.
106550**
106551** It is the responsibility of the caller to ensure that the returned
106552** string is eventually freed using sqlite3DbFree().
106553*/
106554static char *exprINAffinity(Parse *pParse, const Expr *pExpr){
106555 Expr *pLeft = pExpr->pLeft;
106556 int nVal = sqlite3ExprVectorSize(pLeft);
106557 Select *pSelect = ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ? pExpr->x.pSelect : 0;
106558 char *zRet;
106559
106560 assert( pExpr->op==TK_IN )((void) (0));
106561 zRet = sqlite3DbMallocRaw(pParse->db, nVal+1);
106562 if( zRet ){
106563 int i;
106564 for(i=0; i<nVal; i++){
106565 Expr *pA = sqlite3VectorFieldSubexpr(pLeft, i);
106566 char a = sqlite3ExprAffinity(pA);
106567 if( pSelect ){
106568 zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
106569 }else{
106570 zRet[i] = a;
106571 }
106572 }
106573 zRet[nVal] = '\0';
106574 }
106575 return zRet;
106576}
106577#endif
106578
106579#ifndef SQLITE_OMIT_SUBQUERY
106580/*
106581** Load the Parse object passed as the first argument with an error
106582** message of the form:
106583**
106584** "sub-select returns N columns - expected M"
106585*/
106586SQLITE_PRIVATEstatic void sqlite3SubselectError(Parse *pParse, int nActual, int nExpect){
106587 if( pParse->nErr==0 ){
106588 const char *zFmt = "sub-select returns %d columns - expected %d";
106589 sqlite3ErrorMsg(pParse, zFmt, nActual, nExpect);
106590 }
106591}
106592#endif
106593
106594/*
106595** Expression pExpr is a vector that has been used in a context where
106596** it is not permitted. If pExpr is a sub-select vector, this routine
106597** loads the Parse object with a message of the form:
106598**
106599** "sub-select returns N columns - expected 1"
106600**
106601** Or, if it is a regular scalar vector:
106602**
106603** "row value misused"
106604*/
106605SQLITE_PRIVATEstatic void sqlite3VectorErrorMsg(Parse *pParse, Expr *pExpr){
106606#ifndef SQLITE_OMIT_SUBQUERY
106607 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
106608 sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1);
106609 }else
106610#endif
106611 {
106612 sqlite3ErrorMsg(pParse, "row value misused");
106613 }
106614}
106615
106616#ifndef SQLITE_OMIT_SUBQUERY
106617/*
106618** Generate code that will construct an ephemeral table containing all terms
106619** in the RHS of an IN operator. The IN operator can be in either of two
106620** forms:
106621**
106622** x IN (4,5,11) -- IN operator with list on right-hand side
106623** x IN (SELECT a FROM b) -- IN operator with subquery on the right
106624**
106625** The pExpr parameter is the IN operator. The cursor number for the
106626** constructed ephermeral table is returned. The first time the ephemeral
106627** table is computed, the cursor number is also stored in pExpr->iTable,
106628** however the cursor number returned might not be the same, as it might
106629** have been duplicated using OP_OpenDup.
106630**
106631** If the LHS expression ("x" in the examples) is a column value, or
106632** the SELECT statement returns a column value, then the affinity of that
106633** column is used to build the index keys. If both 'x' and the
106634** SELECT... statement are columns, then numeric affinity is used
106635** if either column has NUMERIC or INTEGER affinity. If neither
106636** 'x' nor the SELECT... statement are columns, then numeric affinity
106637** is used.
106638*/
106639SQLITE_PRIVATEstatic void sqlite3CodeRhsOfIN(
106640 Parse *pParse, /* Parsing context */
106641 Expr *pExpr, /* The IN operator */
106642 int iTab /* Use this cursor number */
106643){
106644 int addrOnce = 0; /* Address of the OP_Once instruction at top */
106645 int addr; /* Address of OP_OpenEphemeral instruction */
106646 Expr *pLeft; /* the LHS of the IN operator */
106647 KeyInfo *pKeyInfo = 0; /* Key information */
106648 int nVal; /* Size of vector pLeft */
106649 Vdbe *v; /* The prepared statement under construction */
106650
106651 v = pParse->pVdbe;
106652 assert( v!=0 )((void) (0));
106653
106654 /* The evaluation of the IN must be repeated every time it
106655 ** is encountered if any of the following is true:
106656 **
106657 ** * The right-hand side is a correlated subquery
106658 ** * The right-hand side is an expression list containing variables
106659 ** * We are inside a trigger
106660 **
106661 ** If all of the above are false, then we can compute the RHS just once
106662 ** and reuse it many names.
106663 */
106664 if( !ExprHasProperty(pExpr, EP_VarSelect)(((pExpr)->flags&(0x000040))!=0) && pParse->iSelfTab==0 ){
106665 /* Reuse of the RHS is allowed */
106666 /* If this routine has already been coded, but the previous code
106667 ** might not have been invoked yet, so invoke it now as a subroutine.
106668 */
106669 if( ExprHasProperty(pExpr, EP_Subrtn)(((pExpr)->flags&(0x2000000))!=0) ){
106670 addrOnce = sqlite3VdbeAddOp0(v, OP_Once14); VdbeCoverage(v);
106671 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
106672 ExplainQueryPlan((pParse, 0, "REUSE LIST SUBQUERY %d",sqlite3VdbeExplain (pParse, 0, "REUSE LIST SUBQUERY %d", pExpr
->x.pSelect->selId)
106673 pExpr->x.pSelect->selId))sqlite3VdbeExplain (pParse, 0, "REUSE LIST SUBQUERY %d", pExpr
->x.pSelect->selId)
;
106674 }
106675 assert( ExprUseYSub(pExpr) )((void) (0));
106676 sqlite3VdbeAddOp2(v, OP_Gosub9, pExpr->y.sub.regReturn,
106677 pExpr->y.sub.iAddr);
106678 assert( iTab!=pExpr->iTable )((void) (0));
106679 sqlite3VdbeAddOp2(v, OP_OpenDup115, iTab, pExpr->iTable);
106680 sqlite3VdbeJumpHere(v, addrOnce);
106681 return;
106682 }
106683
106684 /* Begin coding the subroutine */
106685 assert( !ExprUseYWin(pExpr) )((void) (0));
106686 ExprSetProperty(pExpr, EP_Subrtn)(pExpr)->flags|=(0x2000000);
106687 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) )((void) (0));
106688 pExpr->y.sub.regReturn = ++pParse->nMem;
106689 pExpr->y.sub.iAddr =
106690 sqlite3VdbeAddOp2(v, OP_BeginSubrtn74, 0, pExpr->y.sub.regReturn) + 1;
106691
106692 addrOnce = sqlite3VdbeAddOp0(v, OP_Once14); VdbeCoverage(v);
106693 }
106694
106695 /* Check to see if this is a vector IN operator */
106696 pLeft = pExpr->pLeft;
106697 nVal = sqlite3ExprVectorSize(pLeft);
106698
106699 /* Construct the ephemeral table that will contain the content of
106700 ** RHS of the IN operator.
106701 */
106702 pExpr->iTable = iTab;
106703 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral118, pExpr->iTable, nVal);
106704#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
106705 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
106706 VdbeComment((v, "Result of SELECT %u", pExpr->x.pSelect->selId));
106707 }else{
106708 VdbeComment((v, "RHS of IN operator"));
106709 }
106710#endif
106711 pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
106712
106713 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
106714 /* Case 1: expr IN (SELECT ...)
106715 **
106716 ** Generate code to write the results of the select into the temporary
106717 ** table allocated and opened above.
106718 */
106719 Select *pSelect = pExpr->x.pSelect;
106720 ExprList *pEList = pSelect->pEList;
106721
106722 ExplainQueryPlan((pParse, 1, "%sLIST SUBQUERY %d",sqlite3VdbeExplain (pParse, 1, "%sLIST SUBQUERY %d", addrOnce
?"":"CORRELATED ", pSelect->selId )
106723 addrOnce?"":"CORRELATED ", pSelect->selIdsqlite3VdbeExplain (pParse, 1, "%sLIST SUBQUERY %d", addrOnce
?"":"CORRELATED ", pSelect->selId )
106724 ))sqlite3VdbeExplain (pParse, 1, "%sLIST SUBQUERY %d", addrOnce
?"":"CORRELATED ", pSelect->selId )
;
106725 /* If the LHS and RHS of the IN operator do not match, that
106726 ** error will have been caught long before we reach this point. */
106727 if( ALWAYS(pEList->nExpr==nVal)(pEList->nExpr==nVal) ){
106728 Select *pCopy;
106729 SelectDest dest;
106730 int i;
106731 int rc;
106732 sqlite3SelectDestInit(&dest, SRT_Set11, iTab);
106733 dest.zAffSdst = exprINAffinity(pParse, pExpr);
106734 pSelect->iLimit = 0;
106735 testcase( pSelect->selFlags & SF_Distinct );
106736 testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
106737 pCopy = sqlite3SelectDup(pParse->db, pSelect, 0);
106738 rc = pParse->db->mallocFailed ? 1 :sqlite3Select(pParse, pCopy, &dest);
106739 sqlite3SelectDelete(pParse->db, pCopy);
106740 sqlite3DbFree(pParse->db, dest.zAffSdst);
106741 if( rc ){
106742 sqlite3KeyInfoUnref(pKeyInfo);
106743 return;
106744 }
106745 assert( pKeyInfo!=0 )((void) (0)); /* OOM will cause exit after sqlite3Select() */
106746 assert( pEList!=0 )((void) (0));
106747 assert( pEList->nExpr>0 )((void) (0));
106748 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) )((void) (0));
106749 for(i=0; i<nVal; i++){
106750 Expr *p = sqlite3VectorFieldSubexpr(pLeft, i);
106751 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
106752 pParse, p, pEList->a[i].pExpr
106753 );
106754 }
106755 }
106756 }else if( ALWAYS(pExpr->x.pList!=0)(pExpr->x.pList!=0) ){
106757 /* Case 2: expr IN (exprlist)
106758 **
106759 ** For each expression, build an index key from the evaluation and
106760 ** store it in the temporary table. If <expr> is a column, then use
106761 ** that columns affinity when building index keys. If <expr> is not
106762 ** a column, use numeric affinity.
106763 */
106764 char affinity; /* Affinity of the LHS of the IN */
106765 int i;
106766 ExprList *pList = pExpr->x.pList;
106767 struct ExprList_item *pItem;
106768 int r1, r2;
106769 affinity = sqlite3ExprAffinity(pLeft);
106770 if( affinity<=SQLITE_AFF_NONE0x40 ){
106771 affinity = SQLITE_AFF_BLOB0x41;
106772 }else if( affinity==SQLITE_AFF_REAL0x45 ){
106773 affinity = SQLITE_AFF_NUMERIC0x43;
106774 }
106775 if( pKeyInfo ){
106776 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) )((void) (0));
106777 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
106778 }
106779
106780 /* Loop through each expression in <exprlist>. */
106781 r1 = sqlite3GetTempReg(pParse);
106782 r2 = sqlite3GetTempReg(pParse);
106783 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
106784 Expr *pE2 = pItem->pExpr;
106785
106786 /* If the expression is not constant then we will need to
106787 ** disable the test that was generated above that makes sure
106788 ** this code only executes once. Because for a non-constant
106789 ** expression we need to rerun this code each time.
106790 */
106791 if( addrOnce && !sqlite3ExprIsConstant(pE2) ){
106792 sqlite3VdbeChangeToNoop(v, addrOnce-1);
106793 sqlite3VdbeChangeToNoop(v, addrOnce);
106794 ExprClearProperty(pExpr, EP_Subrtn)(pExpr)->flags&=~(0x2000000);
106795 addrOnce = 0;
106796 }
106797
106798 /* Evaluate the expression and insert it into the temp table */
106799 sqlite3ExprCode(pParse, pE2, r1);
106800 sqlite3VdbeAddOp4(v, OP_MakeRecord97, r1, 1, r2, &affinity, 1);
106801 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iTab, r2, r1, 1);
106802 }
106803 sqlite3ReleaseTempReg(pParse, r1);
106804 sqlite3ReleaseTempReg(pParse, r2);
106805 }
106806 if( pKeyInfo ){
106807 sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO(-8));
106808 }
106809 if( addrOnce ){
106810 sqlite3VdbeJumpHere(v, addrOnce);
106811 /* Subroutine return */
106812 assert( ExprUseYSub(pExpr) )((void) (0));
106813 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn((void) (0))
106814 || pParse->nErr )((void) (0));
106815 sqlite3VdbeAddOp3(v, OP_Return67, pExpr->y.sub.regReturn,
106816 pExpr->y.sub.iAddr, 1);
106817 VdbeCoverage(v);
106818 sqlite3ClearTempRegCache(pParse);
106819 }
106820}
106821#endif /* SQLITE_OMIT_SUBQUERY */
106822
106823/*
106824** Generate code for scalar subqueries used as a subquery expression
106825** or EXISTS operator:
106826**
106827** (SELECT a FROM b) -- subquery
106828** EXISTS (SELECT a FROM b) -- EXISTS subquery
106829**
106830** The pExpr parameter is the SELECT or EXISTS operator to be coded.
106831**
106832** Return the register that holds the result. For a multi-column SELECT,
106833** the result is stored in a contiguous array of registers and the
106834** return value is the register of the left-most result column.
106835** Return 0 if an error occurs.
106836*/
106837#ifndef SQLITE_OMIT_SUBQUERY
106838SQLITE_PRIVATEstatic int sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
106839 int addrOnce = 0; /* Address of OP_Once at top of subroutine */
106840 int rReg = 0; /* Register storing resulting */
106841 Select *pSel; /* SELECT statement to encode */
106842 SelectDest dest; /* How to deal with SELECT result */
106843 int nReg; /* Registers to allocate */
106844 Expr *pLimit; /* New limit expression */
106845
106846 Vdbe *v = pParse->pVdbe;
106847 assert( v!=0 )((void) (0));
106848 if( pParse->nErr ) return 0;
106849 testcase( pExpr->op==TK_EXISTS );
106850 testcase( pExpr->op==TK_SELECT );
106851 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT )((void) (0));
106852 assert( ExprUseXSelect(pExpr) )((void) (0));
106853 pSel = pExpr->x.pSelect;
106854
106855 /* If this routine has already been coded, then invoke it as a
106856 ** subroutine. */
106857 if( ExprHasProperty(pExpr, EP_Subrtn)(((pExpr)->flags&(0x2000000))!=0) ){
106858 ExplainQueryPlan((pParse, 0, "REUSE SUBQUERY %d", pSel->selId))sqlite3VdbeExplain (pParse, 0, "REUSE SUBQUERY %d", pSel->
selId)
;
106859 assert( ExprUseYSub(pExpr) )((void) (0));
106860 sqlite3VdbeAddOp2(v, OP_Gosub9, pExpr->y.sub.regReturn,
106861 pExpr->y.sub.iAddr);
106862 return pExpr->iTable;
106863 }
106864
106865 /* Begin coding the subroutine */
106866 assert( !ExprUseYWin(pExpr) )((void) (0));
106867 assert( !ExprHasProperty(pExpr, EP_Reduced|EP_TokenOnly) )((void) (0));
106868 ExprSetProperty(pExpr, EP_Subrtn)(pExpr)->flags|=(0x2000000);
106869 pExpr->y.sub.regReturn = ++pParse->nMem;
106870 pExpr->y.sub.iAddr =
106871 sqlite3VdbeAddOp2(v, OP_BeginSubrtn74, 0, pExpr->y.sub.regReturn) + 1;
106872
106873 /* The evaluation of the EXISTS/SELECT must be repeated every time it
106874 ** is encountered if any of the following is true:
106875 **
106876 ** * The right-hand side is a correlated subquery
106877 ** * The right-hand side is an expression list containing variables
106878 ** * We are inside a trigger
106879 **
106880 ** If all of the above are false, then we can run this code just once
106881 ** save the results, and reuse the same result on subsequent invocations.
106882 */
106883 if( !ExprHasProperty(pExpr, EP_VarSelect)(((pExpr)->flags&(0x000040))!=0) ){
106884 addrOnce = sqlite3VdbeAddOp0(v, OP_Once14); VdbeCoverage(v);
106885 }
106886
106887 /* For a SELECT, generate code to put the values for all columns of
106888 ** the first row into an array of registers and return the index of
106889 ** the first register.
106890 **
106891 ** If this is an EXISTS, write an integer 0 (not exists) or 1 (exists)
106892 ** into a register and return that register number.
106893 **
106894 ** In both cases, the query is augmented with "LIMIT 1". Any
106895 ** preexisting limit is discarded in place of the new LIMIT 1.
106896 */
106897 ExplainQueryPlan((pParse, 1, "%sSCALAR SUBQUERY %d",sqlite3VdbeExplain (pParse, 1, "%sSCALAR SUBQUERY %d", addrOnce
?"":"CORRELATED ", pSel->selId)
106898 addrOnce?"":"CORRELATED ", pSel->selId))sqlite3VdbeExplain (pParse, 1, "%sSCALAR SUBQUERY %d", addrOnce
?"":"CORRELATED ", pSel->selId)
;
106899 nReg = pExpr->op==TK_SELECT138 ? pSel->pEList->nExpr : 1;
106900 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
106901 pParse->nMem += nReg;
106902 if( pExpr->op==TK_SELECT138 ){
106903 dest.eDest = SRT_Mem10;
106904 dest.iSdst = dest.iSDParm;
106905 dest.nSdst = nReg;
106906 sqlite3VdbeAddOp3(v, OP_Null75, 0, dest.iSDParm, dest.iSDParm+nReg-1);
106907 VdbeComment((v, "Init subquery result"));
106908 }else{
106909 dest.eDest = SRT_Exists3;
106910 sqlite3VdbeAddOp2(v, OP_Integer71, 0, dest.iSDParm);
106911 VdbeComment((v, "Init EXISTS result"));
106912 }
106913 if( pSel->pLimit ){
106914 /* The subquery already has a limit. If the pre-existing limit is X
106915 ** then make the new limit X<>0 so that the new limit is either 1 or 0 */
106916 sqlite3 *db = pParse->db;
106917 pLimit = sqlite3Expr(db, TK_INTEGER155, "0");
106918 if( pLimit ){
106919 pLimit->affExpr = SQLITE_AFF_NUMERIC0x43;
106920 pLimit = sqlite3PExpr(pParse, TK_NE52,
106921 sqlite3ExprDup(db, pSel->pLimit->pLeft, 0), pLimit);
106922 }
106923 sqlite3ExprDelete(db, pSel->pLimit->pLeft);
106924 pSel->pLimit->pLeft = pLimit;
106925 }else{
106926 /* If there is no pre-existing limit add a limit of 1 */
106927 pLimit = sqlite3Expr(pParse->db, TK_INTEGER155, "1");
106928 pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT148, pLimit, 0);
106929 }
106930 pSel->iLimit = 0;
106931 if( sqlite3Select(pParse, pSel, &dest) ){
106932 pExpr->op2 = pExpr->op;
106933 pExpr->op = TK_ERROR182;
106934 return 0;
106935 }
106936 pExpr->iTable = rReg = dest.iSDParm;
106937 ExprSetVVAProperty(pExpr, EP_NoReduce);
106938 if( addrOnce ){
106939 sqlite3VdbeJumpHere(v, addrOnce);
106940 }
106941
106942 /* Subroutine return */
106943 assert( ExprUseYSub(pExpr) )((void) (0));
106944 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn((void) (0))
106945 || pParse->nErr )((void) (0));
106946 sqlite3VdbeAddOp3(v, OP_Return67, pExpr->y.sub.regReturn,
106947 pExpr->y.sub.iAddr, 1);
106948 VdbeCoverage(v);
106949 sqlite3ClearTempRegCache(pParse);
106950 return rReg;
106951}
106952#endif /* SQLITE_OMIT_SUBQUERY */
106953
106954#ifndef SQLITE_OMIT_SUBQUERY
106955/*
106956** Expr pIn is an IN(...) expression. This function checks that the
106957** sub-select on the RHS of the IN() operator has the same number of
106958** columns as the vector on the LHS. Or, if the RHS of the IN() is not
106959** a sub-query, that the LHS is a vector of size 1.
106960*/
106961SQLITE_PRIVATEstatic int sqlite3ExprCheckIN(Parse *pParse, Expr *pIn){
106962 int nVector = sqlite3ExprVectorSize(pIn->pLeft);
106963 if( ExprUseXSelect(pIn)(((pIn)->flags&0x001000)!=0) && !pParse->db->mallocFailed ){
106964 if( nVector!=pIn->x.pSelect->pEList->nExpr ){
106965 sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
106966 return 1;
106967 }
106968 }else if( nVector!=1 ){
106969 sqlite3VectorErrorMsg(pParse, pIn->pLeft);
106970 return 1;
106971 }
106972 return 0;
106973}
106974#endif
106975
106976#ifndef SQLITE_OMIT_SUBQUERY
106977/*
106978** Generate code for an IN expression.
106979**
106980** x IN (SELECT ...)
106981** x IN (value, value, ...)
106982**
106983** The left-hand side (LHS) is a scalar or vector expression. The
106984** right-hand side (RHS) is an array of zero or more scalar values, or a
106985** subquery. If the RHS is a subquery, the number of result columns must
106986** match the number of columns in the vector on the LHS. If the RHS is
106987** a list of values, the LHS must be a scalar.
106988**
106989** The IN operator is true if the LHS value is contained within the RHS.
106990** The result is false if the LHS is definitely not in the RHS. The
106991** result is NULL if the presence of the LHS in the RHS cannot be
106992** determined due to NULLs.
106993**
106994** This routine generates code that jumps to destIfFalse if the LHS is not
106995** contained within the RHS. If due to NULLs we cannot determine if the LHS
106996** is contained in the RHS then jump to destIfNull. If the LHS is contained
106997** within the RHS then fall through.
106998**
106999** See the separate in-operator.md documentation file in the canonical
107000** SQLite source tree for additional information.
107001*/
107002static void sqlite3ExprCodeIN(
107003 Parse *pParse, /* Parsing and code generating context */
107004 Expr *pExpr, /* The IN expression */
107005 int destIfFalse, /* Jump here if LHS is not contained in the RHS */
107006 int destIfNull /* Jump here if the results are unknown due to NULLs */
107007){
107008 int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */
107009 int eType; /* Type of the RHS */
107010 int rLhs; /* Register(s) holding the LHS values */
107011 int rLhsOrig; /* LHS values prior to reordering by aiMap[] */
107012 Vdbe *v; /* Statement under construction */
107013 int *aiMap = 0; /* Map from vector field to index column */
107014 char *zAff = 0; /* Affinity string for comparisons */
107015 int nVector; /* Size of vectors for this IN operator */
107016 int iDummy; /* Dummy parameter to exprCodeVector() */
107017 Expr *pLeft; /* The LHS of the IN operator */
107018 int i; /* loop counter */
107019 int destStep2; /* Where to jump when NULLs seen in step 2 */
107020 int destStep6 = 0; /* Start of code for Step 6 */
107021 int addrTruthOp; /* Address of opcode that determines the IN is true */
107022 int destNotNull; /* Jump here if a comparison is not true in step 6 */
107023 int addrTop; /* Top of the step-6 loop */
107024 int iTab = 0; /* Index to use */
107025 u8 okConstFactor = pParse->okConstFactor;
107026
107027 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) )((void) (0));
107028 pLeft = pExpr->pLeft;
107029 if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
107030 zAff = exprINAffinity(pParse, pExpr);
107031 nVector = sqlite3ExprVectorSize(pExpr->pLeft);
107032 aiMap = (int*)sqlite3DbMallocZero(
107033 pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1
107034 );
107035 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
107036
107037 /* Attempt to compute the RHS. After this step, if anything other than
107038 ** IN_INDEX_NOOP is returned, the table opened with cursor iTab
107039 ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned,
107040 ** the RHS has not yet been coded. */
107041 v = pParse->pVdbe;
107042 assert( v!=0 )((void) (0)); /* OOM detected prior to this routine */
107043 VdbeNoopComment((v, "begin IN expr"));
107044 eType = sqlite3FindInIndex(pParse, pExpr,
107045 IN_INDEX_MEMBERSHIP0x0002 | IN_INDEX_NOOP_OK0x0001,
107046 destIfFalse==destIfNull ? 0 : &rRhsHasNull,
107047 aiMap, &iTab);
107048
107049 assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH((void) (0))
107050 || eType==IN_INDEX_INDEX_ASC || eType==IN_INDEX_INDEX_DESC((void) (0))
107051 )((void) (0));
107052#ifdef SQLITE_DEBUG
107053 /* Confirm that aiMap[] contains nVector integer values between 0 and
107054 ** nVector-1. */
107055 for(i=0; i<nVector; i++){
107056 int j, cnt;
107057 for(cnt=j=0; j<nVector; j++) if( aiMap[j]==i ) cnt++;
107058 assert( cnt==1 )((void) (0));
107059 }
107060#endif
107061
107062 /* Code the LHS, the <expr> from "<expr> IN (...)". If the LHS is a
107063 ** vector, then it is stored in an array of nVector registers starting
107064 ** at r1.
107065 **
107066 ** sqlite3FindInIndex() might have reordered the fields of the LHS vector
107067 ** so that the fields are in the same order as an existing index. The
107068 ** aiMap[] array contains a mapping from the original LHS field order to
107069 ** the field order that matches the RHS index.
107070 **
107071 ** Avoid factoring the LHS of the IN(...) expression out of the loop,
107072 ** even if it is constant, as OP_Affinity may be used on the register
107073 ** by code generated below. */
107074 assert( pParse->okConstFactor==okConstFactor )((void) (0));
107075 pParse->okConstFactor = 0;
107076 rLhsOrig = exprCodeVector(pParse, pLeft, &iDummy);
107077 pParse->okConstFactor = okConstFactor;
107078 for(i=0; i<nVector && aiMap[i]==i; i++){} /* Are LHS fields reordered? */
107079 if( i==nVector ){
107080 /* LHS fields are not reordered */
107081 rLhs = rLhsOrig;
107082 }else{
107083 /* Need to reorder the LHS fields according to aiMap */
107084 rLhs = sqlite3GetTempRange(pParse, nVector);
107085 for(i=0; i<nVector; i++){
107086 sqlite3VdbeAddOp3(v, OP_Copy80, rLhsOrig+i, rLhs+aiMap[i], 0);
107087 }
107088 }
107089
107090 /* If sqlite3FindInIndex() did not find or create an index that is
107091 ** suitable for evaluating the IN operator, then evaluate using a
107092 ** sequence of comparisons.
107093 **
107094 ** This is step (1) in the in-operator.md optimized algorithm.
107095 */
107096 if( eType==IN_INDEX_NOOP5 ){
107097 ExprList *pList;
107098 CollSeq *pColl;
107099 int labelOk = sqlite3VdbeMakeLabel(pParse);
107100 int r2, regToFree;
107101 int regCkNull = 0;
107102 int ii;
107103 assert( ExprUseXList(pExpr) )((void) (0));
107104 pList = pExpr->x.pList;
107105 pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
107106 if( destIfNull!=destIfFalse ){
107107 regCkNull = sqlite3GetTempReg(pParse);
107108 sqlite3VdbeAddOp3(v, OP_BitAnd102, rLhs, rLhs, regCkNull);
107109 }
107110 for(ii=0; ii<pList->nExpr; ii++){
107111 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
107112 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
107113 sqlite3VdbeAddOp3(v, OP_BitAnd102, regCkNull, r2, regCkNull);
107114 }
107115 sqlite3ReleaseTempReg(pParse, regToFree);
107116 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
107117 int op = rLhs!=r2 ? OP_Eq53 : OP_NotNull51;
107118 sqlite3VdbeAddOp4(v, op, rLhs, labelOk, r2,
107119 (void*)pColl, P4_COLLSEQ(-2));
107120 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_Eq);
107121 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_Eq);
107122 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_NotNull);
107123 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_NotNull);
107124 sqlite3VdbeChangeP5(v, zAff[0]);
107125 }else{
107126 int op = rLhs!=r2 ? OP_Ne52 : OP_IsNull50;
107127 assert( destIfNull==destIfFalse )((void) (0));
107128 sqlite3VdbeAddOp4(v, op, rLhs, destIfFalse, r2,
107129 (void*)pColl, P4_COLLSEQ(-2));
107130 VdbeCoverageIf(v, op==OP_Ne);
107131 VdbeCoverageIf(v, op==OP_IsNull);
107132 sqlite3VdbeChangeP5(v, zAff[0] | SQLITE_JUMPIFNULL0x10);
107133 }
107134 }
107135 if( regCkNull ){
107136 sqlite3VdbeAddOp2(v, OP_IsNull50, regCkNull, destIfNull); VdbeCoverage(v);
107137 sqlite3VdbeGoto(v, destIfFalse);
107138 }
107139 sqlite3VdbeResolveLabel(v, labelOk);
107140 sqlite3ReleaseTempReg(pParse, regCkNull);
107141 goto sqlite3ExprCodeIN_finished;
107142 }
107143
107144 /* Step 2: Check to see if the LHS contains any NULL columns. If the
107145 ** LHS does contain NULLs then the result must be either FALSE or NULL.
107146 ** We will then skip the binary search of the RHS.
107147 */
107148 if( destIfNull==destIfFalse ){
107149 destStep2 = destIfFalse;
107150 }else{
107151 destStep2 = destStep6 = sqlite3VdbeMakeLabel(pParse);
107152 }
107153 for(i=0; i<nVector; i++){
107154 Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i);
107155 if( pParse->nErr ) goto sqlite3ExprCodeIN_oom_error;
107156 if( sqlite3ExprCanBeNull(p) ){
107157 sqlite3VdbeAddOp2(v, OP_IsNull50, rLhs+i, destStep2);
107158 VdbeCoverage(v);
107159 }
107160 }
107161
107162 /* Step 3. The LHS is now known to be non-NULL. Do the binary search
107163 ** of the RHS using the LHS as a probe. If found, the result is
107164 ** true.
107165 */
107166 if( eType==IN_INDEX_ROWID1 ){
107167 /* In this case, the RHS is the ROWID of table b-tree and so we also
107168 ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4
107169 ** into a single opcode. */
107170 sqlite3VdbeAddOp3(v, OP_SeekRowid29, iTab, destIfFalse, rLhs);
107171 VdbeCoverage(v);
107172 addrTruthOp = sqlite3VdbeAddOp0(v, OP_Goto8); /* Return True */
107173 }else{
107174 sqlite3VdbeAddOp4(v, OP_Affinity96, rLhs, nVector, 0, zAff, nVector);
107175 if( destIfFalse==destIfNull ){
107176 /* Combine Step 3 and Step 5 into a single opcode */
107177 sqlite3VdbeAddOp4Int(v, OP_NotFound27, iTab, destIfFalse,
107178 rLhs, nVector); VdbeCoverage(v);
107179 goto sqlite3ExprCodeIN_finished;
107180 }
107181 /* Ordinary Step 3, for the case where FALSE and NULL are distinct */
107182 addrTruthOp = sqlite3VdbeAddOp4Int(v, OP_Found28, iTab, 0,
107183 rLhs, nVector); VdbeCoverage(v);
107184 }
107185
107186 /* Step 4. If the RHS is known to be non-NULL and we did not find
107187 ** an match on the search above, then the result must be FALSE.
107188 */
107189 if( rRhsHasNull && nVector==1 ){
107190 sqlite3VdbeAddOp2(v, OP_NotNull51, rRhsHasNull, destIfFalse);
107191 VdbeCoverage(v);
107192 }
107193
107194 /* Step 5. If we do not care about the difference between NULL and
107195 ** FALSE, then just return false.
107196 */
107197 if( destIfFalse==destIfNull ) sqlite3VdbeGoto(v, destIfFalse);
107198
107199 /* Step 6: Loop through rows of the RHS. Compare each row to the LHS.
107200 ** If any comparison is NULL, then the result is NULL. If all
107201 ** comparisons are FALSE then the final result is FALSE.
107202 **
107203 ** For a scalar LHS, it is sufficient to check just the first row
107204 ** of the RHS.
107205 */
107206 if( destStep6 ) sqlite3VdbeResolveLabel(v, destStep6);
107207 addrTop = sqlite3VdbeAddOp2(v, OP_Rewind35, iTab, destIfFalse);
107208 VdbeCoverage(v);
107209 if( nVector>1 ){
107210 destNotNull = sqlite3VdbeMakeLabel(pParse);
107211 }else{
107212 /* For nVector==1, combine steps 6 and 7 by immediately returning
107213 ** FALSE if the first comparison is not NULL */
107214 destNotNull = destIfFalse;
107215 }
107216 for(i=0; i<nVector; i++){
107217 Expr *p;
107218 CollSeq *pColl;
107219 int r3 = sqlite3GetTempReg(pParse);
107220 p = sqlite3VectorFieldSubexpr(pLeft, i);
107221 pColl = sqlite3ExprCollSeq(pParse, p);
107222 sqlite3VdbeAddOp3(v, OP_Column94, iTab, i, r3);
107223 sqlite3VdbeAddOp4(v, OP_Ne52, rLhs+i, destNotNull, r3,
107224 (void*)pColl, P4_COLLSEQ(-2));
107225 VdbeCoverage(v);
107226 sqlite3ReleaseTempReg(pParse, r3);
107227 }
107228 sqlite3VdbeAddOp2(v, OP_Goto8, 0, destIfNull);
107229 if( nVector>1 ){
107230 sqlite3VdbeResolveLabel(v, destNotNull);
107231 sqlite3VdbeAddOp2(v, OP_Next38, iTab, addrTop+1);
107232 VdbeCoverage(v);
107233
107234 /* Step 7: If we reach this point, we know that the result must
107235 ** be false. */
107236 sqlite3VdbeAddOp2(v, OP_Goto8, 0, destIfFalse);
107237 }
107238
107239 /* Jumps here in order to return true. */
107240 sqlite3VdbeJumpHere(v, addrTruthOp);
107241
107242sqlite3ExprCodeIN_finished:
107243 if( rLhs!=rLhsOrig ) sqlite3ReleaseTempReg(pParse, rLhs);
107244 VdbeComment((v, "end IN expr"));
107245sqlite3ExprCodeIN_oom_error:
107246 sqlite3DbFree(pParse->db, aiMap);
107247 sqlite3DbFree(pParse->db, zAff);
107248}
107249#endif /* SQLITE_OMIT_SUBQUERY */
107250
107251#ifndef SQLITE_OMIT_FLOATING_POINT
107252/*
107253** Generate an instruction that will put the floating point
107254** value described by z[0..n-1] into register iMem.
107255**
107256** The z[] string will probably not be zero-terminated. But the
107257** z[n] character is guaranteed to be something that does not look
107258** like the continuation of the number.
107259*/
107260static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
107261 if( ALWAYS(z!=0)(z!=0) ){
107262 double value;
107263 sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF81);
107264 assert( !sqlite3IsNaN(value) )((void) (0)); /* The new AtoF never returns NaN */
107265 if( negateFlag ) value = -value;
107266 sqlite3VdbeAddOp4Dup8(v, OP_Real153, 0, iMem, 0, (u8*)&value, P4_REAL(-12));
107267 }
107268}
107269#endif
107270
107271
107272/*
107273** Generate an instruction that will put the integer describe by
107274** text z[0..n-1] into register iMem.
107275**
107276** Expr.u.zToken is always UTF8 and zero-terminated.
107277*/
107278static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
107279 Vdbe *v = pParse->pVdbe;
107280 if( pExpr->flags & EP_IntValue0x000800 ){
107281 int i = pExpr->u.iValue;
107282 assert( i>=0 )((void) (0));
107283 if( negFlag ) i = -i;
107284 sqlite3VdbeAddOp2(v, OP_Integer71, i, iMem);
107285 }else{
107286 int c;
107287 i64 value;
107288 const char *z = pExpr->u.zToken;
107289 assert( z!=0 )((void) (0));
107290 c = sqlite3DecOrHexToI64(z, &value);
107291 if( (c==3 && !negFlag) || (c==2) || (negFlag && value==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))))){
107292#ifdef SQLITE_OMIT_FLOATING_POINT
107293 sqlite3ErrorMsg(pParse, "oversized integer: %s%#T", negFlag?"-":"",pExpr);
107294#else
107295#ifndef SQLITE_OMIT_HEX_INTEGER
107296 if( sqlite3_strnicmp(z,"0x",2)==0 ){
107297 sqlite3ErrorMsg(pParse, "hex literal too big: %s%#T",
107298 negFlag?"-":"",pExpr);
107299 }else
107300#endif
107301 {
107302 codeReal(v, z, negFlag, iMem);
107303 }
107304#endif
107305 }else{
107306 if( negFlag ){ value = c==3 ? SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) : -value; }
107307 sqlite3VdbeAddOp4Dup8(v, OP_Int6472, 0, iMem, 0, (u8*)&value, P4_INT64(-13));
107308 }
107309 }
107310}
107311
107312
107313/* Generate code that will load into register regOut a value that is
107314** appropriate for the iIdxCol-th column of index pIdx.
107315*/
107316SQLITE_PRIVATEstatic void sqlite3ExprCodeLoadIndexColumn(
107317 Parse *pParse, /* The parsing context */
107318 Index *pIdx, /* The index whose column is to be loaded */
107319 int iTabCur, /* Cursor pointing to a table row */
107320 int iIdxCol, /* The column of the index to be loaded */
107321 int regOut /* Store the index column value in this register */
107322){
107323 i16 iTabCol = pIdx->aiColumn[iIdxCol];
107324 if( iTabCol==XN_EXPR(-2) ){
107325 assert( pIdx->aColExpr )((void) (0));
107326 assert( pIdx->aColExpr->nExpr>iIdxCol )((void) (0));
107327 pParse->iSelfTab = iTabCur + 1;
107328 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
107329 pParse->iSelfTab = 0;
107330 }else{
107331 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
107332 iTabCol, regOut);
107333 }
107334}
107335
107336#ifndef SQLITE_OMIT_GENERATED_COLUMNS
107337/*
107338** Generate code that will compute the value of generated column pCol
107339** and store the result in register regOut
107340*/
107341SQLITE_PRIVATEstatic void sqlite3ExprCodeGeneratedColumn(
107342 Parse *pParse, /* Parsing context */
107343 Table *pTab, /* Table containing the generated column */
107344 Column *pCol, /* The generated column */
107345 int regOut /* Put the result in this register */
107346){
107347 int iAddr;
107348 Vdbe *v = pParse->pVdbe;
107349 assert( v!=0 )((void) (0));
107350 assert( pParse->iSelfTab!=0 )((void) (0));
107351 if( pParse->iSelfTab>0 ){
107352 iAddr = sqlite3VdbeAddOp3(v, OP_IfNullRow18, pParse->iSelfTab-1, 0, regOut);
107353 }else{
107354 iAddr = 0;
107355 }
107356 sqlite3ExprCodeCopy(pParse, sqlite3ColumnExpr(pTab,pCol), regOut);
107357 if( pCol->affinity>=SQLITE_AFF_TEXT0x42 ){
107358 sqlite3VdbeAddOp4(v, OP_Affinity96, regOut, 1, 0, &pCol->affinity, 1);
107359 }
107360 if( iAddr ) sqlite3VdbeJumpHere(v, iAddr);
107361}
107362#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
107363
107364/*
107365** Generate code to extract the value of the iCol-th column of a table.
107366*/
107367SQLITE_PRIVATEstatic void sqlite3ExprCodeGetColumnOfTable(
107368 Vdbe *v, /* Parsing context */
107369 Table *pTab, /* The table containing the value */
107370 int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */
107371 int iCol, /* Index of the column to extract */
107372 int regOut /* Extract the value into this register */
107373){
107374 Column *pCol;
107375 assert( v!=0 )((void) (0));
107376 if( pTab==0 ){
107377 sqlite3VdbeAddOp3(v, OP_Column94, iTabCur, iCol, regOut);
107378 return;
107379 }
107380 if( iCol<0 || iCol==pTab->iPKey ){
107381 sqlite3VdbeAddOp2(v, OP_Rowid135, iTabCur, regOut);
107382 VdbeComment((v, "%s.rowid", pTab->zName));
107383 }else{
107384 int op;
107385 int x;
107386 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
107387 op = OP_VColumn175;
107388 x = iCol;
107389#ifndef SQLITE_OMIT_GENERATED_COLUMNS
107390 }else if( (pCol = &pTab->aCol[iCol])->colFlags & COLFLAG_VIRTUAL0x0020 ){
107391 Parse *pParse = sqlite3VdbeParser(v);
107392 if( pCol->colFlags & COLFLAG_BUSY0x0100 ){
107393 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"",
107394 pCol->zCnName);
107395 }else{
107396 int savedSelfTab = pParse->iSelfTab;
107397 pCol->colFlags |= COLFLAG_BUSY0x0100;
107398 pParse->iSelfTab = iTabCur+1;
107399 sqlite3ExprCodeGeneratedColumn(pParse, pTab, pCol, regOut);
107400 pParse->iSelfTab = savedSelfTab;
107401 pCol->colFlags &= ~COLFLAG_BUSY0x0100;
107402 }
107403 return;
107404#endif
107405 }else if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
107406 testcase( iCol!=sqlite3TableColumnToStorage(pTab, iCol) );
107407 x = sqlite3TableColumnToIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
107408 op = OP_Column94;
107409 }else{
107410 x = sqlite3TableColumnToStorage(pTab,iCol);
107411 testcase( x!=iCol );
107412 op = OP_Column94;
107413 }
107414 sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut);
107415 sqlite3ColumnDefault(v, pTab, iCol, regOut);
107416 }
107417}
107418
107419/*
107420** Generate code that will extract the iColumn-th column from
107421** table pTab and store the column value in register iReg.
107422**
107423** There must be an open cursor to pTab in iTable when this routine
107424** is called. If iColumn<0 then code is generated that extracts the rowid.
107425*/
107426SQLITE_PRIVATEstatic int sqlite3ExprCodeGetColumn(
107427 Parse *pParse, /* Parsing and code generating context */
107428 Table *pTab, /* Description of the table we are reading from */
107429 int iColumn, /* Index of the table column */
107430 int iTable, /* The cursor pointing to the table */
107431 int iReg, /* Store results here */
107432 u8 p5 /* P5 value for OP_Column + FLAGS */
107433){
107434 assert( pParse->pVdbe!=0 )((void) (0));
107435 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pTab, iTable, iColumn, iReg);
107436 if( p5 ){
107437 VdbeOp *pOp = sqlite3VdbeGetOp(pParse->pVdbe,-1);
107438 if( pOp->opcode==OP_Column94 ) pOp->p5 = p5;
107439 }
107440 return iReg;
107441}
107442
107443/*
107444** Generate code to move content from registers iFrom...iFrom+nReg-1
107445** over to iTo..iTo+nReg-1.
107446*/
107447SQLITE_PRIVATEstatic void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
107448 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move79, iFrom, iTo, nReg);
107449}
107450
107451/*
107452** Convert a scalar expression node to a TK_REGISTER referencing
107453** register iReg. The caller must ensure that iReg already contains
107454** the correct value for the expression.
107455*/
107456static void exprToRegister(Expr *pExpr, int iReg){
107457 Expr *p = sqlite3ExprSkipCollateAndLikely(pExpr);
107458 if( NEVER(p==0)(p==0) ) return;
107459 p->op2 = p->op;
107460 p->op = TK_REGISTER176;
107461 p->iTable = iReg;
107462 ExprClearProperty(p, EP_Skip)(p)->flags&=~(0x002000);
107463}
107464
107465/*
107466** Evaluate an expression (either a vector or a scalar expression) and store
107467** the result in continguous temporary registers. Return the index of
107468** the first register used to store the result.
107469**
107470** If the returned result register is a temporary scalar, then also write
107471** that register number into *piFreeable. If the returned result register
107472** is not a temporary or if the expression is a vector set *piFreeable
107473** to 0.
107474*/
107475static int exprCodeVector(Parse *pParse, Expr *p, int *piFreeable){
107476 int iResult;
107477 int nResult = sqlite3ExprVectorSize(p);
107478 if( nResult==1 ){
107479 iResult = sqlite3ExprCodeTemp(pParse, p, piFreeable);
107480 }else{
107481 *piFreeable = 0;
107482 if( p->op==TK_SELECT138 ){
107483#if SQLITE_OMIT_SUBQUERY
107484 iResult = 0;
107485#else
107486 iResult = sqlite3CodeSubselect(pParse, p);
107487#endif
107488 }else{
107489 int i;
107490 iResult = pParse->nMem+1;
107491 pParse->nMem += nResult;
107492 assert( ExprUseXList(p) )((void) (0));
107493 for(i=0; i<nResult; i++){
107494 sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult);
107495 }
107496 }
107497 }
107498 return iResult;
107499}
107500
107501/*
107502** If the last opcode is a OP_Copy, then set the do-not-merge flag (p5)
107503** so that a subsequent copy will not be merged into this one.
107504*/
107505static void setDoNotMergeFlagOnCopy(Vdbe *v){
107506 if( sqlite3VdbeGetOp(v, -1)->opcode==OP_Copy80 ){
107507 sqlite3VdbeChangeP5(v, 1); /* Tag trailing OP_Copy as not mergable */
107508 }
107509}
107510
107511/*
107512** Generate code to implement special SQL functions that are implemented
107513** in-line rather than by using the usual callbacks.
107514*/
107515static int exprCodeInlineFunction(
107516 Parse *pParse, /* Parsing context */
107517 ExprList *pFarg, /* List of function arguments */
107518 int iFuncId, /* Function ID. One of the INTFUNC_... values */
107519 int target /* Store function result in this register */
107520){
107521 int nFarg;
107522 Vdbe *v = pParse->pVdbe;
107523 assert( v!=0 )((void) (0));
107524 assert( pFarg!=0 )((void) (0));
107525 nFarg = pFarg->nExpr;
107526 assert( nFarg>0 )((void) (0)); /* All in-line functions have at least one argument */
107527 switch( iFuncId ){
107528 case INLINEFUNC_coalesce0: {
107529 /* Attempt a direct implementation of the built-in COALESCE() and
107530 ** IFNULL() functions. This avoids unnecessary evaluation of
107531 ** arguments past the first non-NULL argument.
107532 */
107533 int endCoalesce = sqlite3VdbeMakeLabel(pParse);
107534 int i;
107535 assert( nFarg>=2 )((void) (0));
107536 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
107537 for(i=1; i<nFarg; i++){
107538 sqlite3VdbeAddOp2(v, OP_NotNull51, target, endCoalesce);
107539 VdbeCoverage(v);
107540 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
107541 }
107542 setDoNotMergeFlagOnCopy(v);
107543 sqlite3VdbeResolveLabel(v, endCoalesce);
107544 break;
107545 }
107546 case INLINEFUNC_iif5: {
107547 Expr caseExpr;
107548 memset(&caseExpr, 0, sizeof(caseExpr));
107549 caseExpr.op = TK_CASE157;
107550 caseExpr.x.pList = pFarg;
107551 return sqlite3ExprCodeTarget(pParse, &caseExpr, target);
107552 }
107553#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
107554 case INLINEFUNC_sqlite_offset6: {
107555 Expr *pArg = pFarg->a[0].pExpr;
107556 if( pArg->op==TK_COLUMN167 && pArg->iTable>=0 ){
107557 sqlite3VdbeAddOp3(v, OP_Offset93, pArg->iTable, pArg->iColumn, target);
107558 }else{
107559 sqlite3VdbeAddOp2(v, OP_Null75, 0, target);
107560 }
107561 break;
107562 }
107563#endif
107564 default: {
107565 /* The UNLIKELY() function is a no-op. The result is the value
107566 ** of the first argument.
107567 */
107568 assert( nFarg==1 || nFarg==2 )((void) (0));
107569 target = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
107570 break;
107571 }
107572
107573 /***********************************************************************
107574 ** Test-only SQL functions that are only usable if enabled
107575 ** via SQLITE_TESTCTRL_INTERNAL_FUNCTIONS
107576 */
107577#if !defined(SQLITE_UNTESTABLE)
107578 case INLINEFUNC_expr_compare3: {
107579 /* Compare two expressions using sqlite3ExprCompare() */
107580 assert( nFarg==2 )((void) (0));
107581 sqlite3VdbeAddOp2(v, OP_Integer71,
107582 sqlite3ExprCompare(0,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
107583 target);
107584 break;
107585 }
107586
107587 case INLINEFUNC_expr_implies_expr2: {
107588 /* Compare two expressions using sqlite3ExprImpliesExpr() */
107589 assert( nFarg==2 )((void) (0));
107590 sqlite3VdbeAddOp2(v, OP_Integer71,
107591 sqlite3ExprImpliesExpr(pParse,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
107592 target);
107593 break;
107594 }
107595
107596 case INLINEFUNC_implies_nonnull_row1: {
107597 /* REsult of sqlite3ExprImpliesNonNullRow() */
107598 Expr *pA1;
107599 assert( nFarg==2 )((void) (0));
107600 pA1 = pFarg->a[1].pExpr;
107601 if( pA1->op==TK_COLUMN167 ){
107602 sqlite3VdbeAddOp2(v, OP_Integer71,
107603 sqlite3ExprImpliesNonNullRow(pFarg->a[0].pExpr,pA1->iTable),
107604 target);
107605 }else{
107606 sqlite3VdbeAddOp2(v, OP_Null75, 0, target);
107607 }
107608 break;
107609 }
107610
107611 case INLINEFUNC_affinity4: {
107612 /* The AFFINITY() function evaluates to a string that describes
107613 ** the type affinity of the argument. This is used for testing of
107614 ** the SQLite type logic.
107615 */
107616 const char *azAff[] = { "blob", "text", "numeric", "integer", "real" };
107617 char aff;
107618 assert( nFarg==1 )((void) (0));
107619 aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
107620 sqlite3VdbeLoadString(v, target,
107621 (aff<=SQLITE_AFF_NONE0x40) ? "none" : azAff[aff-SQLITE_AFF_BLOB0x41]);
107622 break;
107623 }
107624#endif /* !defined(SQLITE_UNTESTABLE) */
107625 }
107626 return target;
107627}
107628
107629
107630/*
107631** Generate code into the current Vdbe to evaluate the given
107632** expression. Attempt to store the results in register "target".
107633** Return the register where results are stored.
107634**
107635** With this routine, there is no guarantee that results will
107636** be stored in target. The result might be stored in some other
107637** register if it is convenient to do so. The calling function
107638** must check the return code and move the results to the desired
107639** register.
107640*/
107641SQLITE_PRIVATEstatic int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
107642 Vdbe *v = pParse->pVdbe; /* The VM under construction */
107643 int op; /* The opcode being coded */
107644 int inReg = target; /* Results stored in register inReg */
107645 int regFree1 = 0; /* If non-zero free this temporary register */
107646 int regFree2 = 0; /* If non-zero free this temporary register */
107647 int r1, r2; /* Various register numbers */
107648 Expr tempX; /* Temporary expression node */
107649 int p5 = 0;
107650
107651 assert( target>0 && target<=pParse->nMem )((void) (0));
107652 assert( v!=0 )((void) (0));
107653
107654expr_code_doover:
107655 if( pExpr==0 ){
107656 op = TK_NULL121;
107657 }else{
107658 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) )((void) (0));
107659 op = pExpr->op;
107660 }
107661 switch( op ){
107662 case TK_AGG_COLUMN169: {
107663 AggInfo *pAggInfo = pExpr->pAggInfo;
107664 struct AggInfo_col *pCol;
107665 assert( pAggInfo!=0 )((void) (0));
107666 assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn )((void) (0));
107667 pCol = &pAggInfo->aCol[pExpr->iAgg];
107668 if( !pAggInfo->directMode ){
107669 assert( pCol->iMem>0 )((void) (0));
107670 return pCol->iMem;
107671 }else if( pAggInfo->useSortingIdx ){
107672 Table *pTab = pCol->pTab;
107673 sqlite3VdbeAddOp3(v, OP_Column94, pAggInfo->sortingIdxPTab,
107674 pCol->iSorterColumn, target);
107675 if( pCol->iColumn<0 ){
107676 VdbeComment((v,"%s.rowid",pTab->zName));
107677 }else{
107678 VdbeComment((v,"%s.%s",
107679 pTab->zName, pTab->aCol[pCol->iColumn].zCnName));
107680 if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL0x45 ){
107681 sqlite3VdbeAddOp1(v, OP_RealAffinity87, target);
107682 }
107683 }
107684 return target;
107685 }
107686 /* Otherwise, fall thru into the TK_COLUMN case */
107687 /* no break */ deliberate_fall_through
107688 }
107689 case TK_COLUMN167: {
107690 int iTab = pExpr->iTable;
107691 int iReg;
107692 if( ExprHasProperty(pExpr, EP_FixedCol)(((pExpr)->flags&(0x000020))!=0) ){
107693 /* This COLUMN expression is really a constant due to WHERE clause
107694 ** constraints, and that constant is coded by the pExpr->pLeft
107695 ** expresssion. However, make sure the constant has the correct
107696 ** datatype by applying the Affinity of the table column to the
107697 ** constant.
107698 */
107699 int aff;
107700 iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
107701 assert( ExprUseYTab(pExpr) )((void) (0));
107702 if( pExpr->y.pTab ){
107703 aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
107704 }else{
107705 aff = pExpr->affExpr;
107706 }
107707 if( aff>SQLITE_AFF_BLOB0x41 ){
107708 static const char zAff[] = "B\000C\000D\000E";
107709 assert( SQLITE_AFF_BLOB=='A' )((void) (0));
107710 assert( SQLITE_AFF_TEXT=='B' )((void) (0));
107711 sqlite3VdbeAddOp4(v, OP_Affinity96, iReg, 1, 0,
107712 &zAff[(aff-'B')*2], P4_STATIC(-1));
107713 }
107714 return iReg;
107715 }
107716 if( iTab<0 ){
107717 if( pParse->iSelfTab<0 ){
107718 /* Other columns in the same row for CHECK constraints or
107719 ** generated columns or for inserting into partial index.
107720 ** The row is unpacked into registers beginning at
107721 ** 0-(pParse->iSelfTab). The rowid (if any) is in a register
107722 ** immediately prior to the first column.
107723 */
107724 Column *pCol;
107725 Table *pTab;
107726 int iSrc;
107727 int iCol = pExpr->iColumn;
107728 assert( ExprUseYTab(pExpr) )((void) (0));
107729 pTab = pExpr->y.pTab;
107730 assert( pTab!=0 )((void) (0));
107731 assert( iCol>=XN_ROWID )((void) (0));
107732 assert( iCol<pTab->nCol )((void) (0));
107733 if( iCol<0 ){
107734 return -1-pParse->iSelfTab;
107735 }
107736 pCol = pTab->aCol + iCol;
107737 testcase( iCol!=sqlite3TableColumnToStorage(pTab,iCol) );
107738 iSrc = sqlite3TableColumnToStorage(pTab, iCol) - pParse->iSelfTab;
107739#ifndef SQLITE_OMIT_GENERATED_COLUMNS
107740 if( pCol->colFlags & COLFLAG_GENERATED0x0060 ){
107741 if( pCol->colFlags & COLFLAG_BUSY0x0100 ){
107742 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"",
107743 pCol->zCnName);
107744 return 0;
107745 }
107746 pCol->colFlags |= COLFLAG_BUSY0x0100;
107747 if( pCol->colFlags & COLFLAG_NOTAVAIL0x0080 ){
107748 sqlite3ExprCodeGeneratedColumn(pParse, pTab, pCol, iSrc);
107749 }
107750 pCol->colFlags &= ~(COLFLAG_BUSY0x0100|COLFLAG_NOTAVAIL0x0080);
107751 return iSrc;
107752 }else
107753#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
107754 if( pCol->affinity==SQLITE_AFF_REAL0x45 ){
107755 sqlite3VdbeAddOp2(v, OP_SCopy81, iSrc, target);
107756 sqlite3VdbeAddOp1(v, OP_RealAffinity87, target);
107757 return target;
107758 }else{
107759 return iSrc;
107760 }
107761 }else{
107762 /* Coding an expression that is part of an index where column names
107763 ** in the index refer to the table to which the index belongs */
107764 iTab = pParse->iSelfTab - 1;
107765 }
107766 }
107767 assert( ExprUseYTab(pExpr) )((void) (0));
107768 iReg = sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
107769 pExpr->iColumn, iTab, target,
107770 pExpr->op2);
107771 if( pExpr->y.pTab==0 && pExpr->affExpr==SQLITE_AFF_REAL0x45 ){
107772 sqlite3VdbeAddOp1(v, OP_RealAffinity87, iReg);
107773 }
107774 return iReg;
107775 }
107776 case TK_INTEGER155: {
107777 codeInteger(pParse, pExpr, 0, target);
107778 return target;
107779 }
107780 case TK_TRUEFALSE170: {
107781 sqlite3VdbeAddOp2(v, OP_Integer71, sqlite3ExprTruthValue(pExpr), target);
107782 return target;
107783 }
107784#ifndef SQLITE_OMIT_FLOATING_POINT
107785 case TK_FLOAT153: {
107786 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
107787 codeReal(v, pExpr->u.zToken, 0, target);
107788 return target;
107789 }
107790#endif
107791 case TK_STRING117: {
107792 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
107793 sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
107794 return target;
107795 }
107796 default: {
107797 /* Make NULL the default case so that if a bug causes an illegal
107798 ** Expr node to be passed into this function, it will be handled
107799 ** sanely and not crash. But keep the assert() to bring the problem
107800 ** to the attention of the developers. */
107801 assert( op==TK_NULL || op==TK_ERROR || pParse->db->mallocFailed )((void) (0));
107802 sqlite3VdbeAddOp2(v, OP_Null75, 0, target);
107803 return target;
107804 }
107805#ifndef SQLITE_OMIT_BLOB_LITERAL
107806 case TK_BLOB154: {
107807 int n;
107808 const char *z;
107809 char *zBlob;
107810 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
107811 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' )((void) (0));
107812 assert( pExpr->u.zToken[1]=='\'' )((void) (0));
107813 z = &pExpr->u.zToken[2];
107814 n = sqlite3Strlen30(z) - 1;
107815 assert( z[n]=='\'' )((void) (0));
107816 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
107817 sqlite3VdbeAddOp4(v, OP_Blob77, n/2, target, 0, zBlob, P4_DYNAMIC(-6));
107818 return target;
107819 }
107820#endif
107821 case TK_VARIABLE156: {
107822 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
107823 assert( pExpr->u.zToken!=0 )((void) (0));
107824 assert( pExpr->u.zToken[0]!=0 )((void) (0));
107825 sqlite3VdbeAddOp2(v, OP_Variable78, pExpr->iColumn, target);
107826 if( pExpr->u.zToken[1]!=0 ){
107827 const char *z = sqlite3VListNumToName(pParse->pVList, pExpr->iColumn);
107828 assert( pExpr->u.zToken[0]=='?' || (z && !strcmp(pExpr->u.zToken, z)) )((void) (0));
107829 pParse->pVList[0] = 0; /* Indicate VList may no longer be enlarged */
107830 sqlite3VdbeAppendP4(v, (char*)z, P4_STATIC(-1));
107831 }
107832 return target;
107833 }
107834 case TK_REGISTER176: {
107835 return pExpr->iTable;
107836 }
107837#ifndef SQLITE_OMIT_CAST
107838 case TK_CAST36: {
107839 /* Expressions of the form: CAST(pLeft AS token) */
107840 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
107841 if( inReg!=target ){
107842 sqlite3VdbeAddOp2(v, OP_SCopy81, inReg, target);
107843 inReg = target;
107844 }
107845 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
107846 sqlite3VdbeAddOp2(v, OP_Cast88, target,
107847 sqlite3AffinityType(pExpr->u.zToken, 0));
107848 return inReg;
107849 }
107850#endif /* SQLITE_OMIT_CAST */
107851 case TK_IS45:
107852 case TK_ISNOT171:
107853 op = (op==TK_IS45) ? TK_EQ53 : TK_NE52;
107854 p5 = SQLITE_NULLEQ0x80;
107855 /* fall-through */
107856 case TK_LT56:
107857 case TK_LE55:
107858 case TK_GT54:
107859 case TK_GE57:
107860 case TK_NE52:
107861 case TK_EQ53: {
107862 Expr *pLeft = pExpr->pLeft;
107863 if( sqlite3ExprIsVector(pLeft) ){
107864 codeVectorCompare(pParse, pExpr, target, op, p5);
107865 }else{
107866 r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
107867 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
107868 sqlite3VdbeAddOp2(v, OP_Integer71, 1, inReg);
107869 codeCompare(pParse, pLeft, pExpr->pRight, op, r1, r2,
107870 sqlite3VdbeCurrentAddr(v)+2, p5,
107871 ExprHasProperty(pExpr,EP_Commuted)(((pExpr)->flags&(0x000400))!=0));
107872 assert(TK_LT==OP_Lt)((void) (0)); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
107873 assert(TK_LE==OP_Le)((void) (0)); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
107874 assert(TK_GT==OP_Gt)((void) (0)); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
107875 assert(TK_GE==OP_Ge)((void) (0)); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
107876 assert(TK_EQ==OP_Eq)((void) (0)); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
107877 assert(TK_NE==OP_Ne)((void) (0)); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
107878 if( p5==SQLITE_NULLEQ0x80 ){
107879 sqlite3VdbeAddOp2(v, OP_Integer71, 0, inReg);
107880 }else{
107881 sqlite3VdbeAddOp3(v, OP_ZeroOrNull92, r1, inReg, r2);
107882 }
107883 testcase( regFree1==0 );
107884 testcase( regFree2==0 );
107885 }
107886 break;
107887 }
107888 case TK_AND44:
107889 case TK_OR43:
107890 case TK_PLUS106:
107891 case TK_STAR108:
107892 case TK_MINUS107:
107893 case TK_REM110:
107894 case TK_BITAND102:
107895 case TK_BITOR103:
107896 case TK_SLASH109:
107897 case TK_LSHIFT104:
107898 case TK_RSHIFT105:
107899 case TK_CONCAT111: {
107900 assert( TK_AND==OP_And )((void) (0)); testcase( op==TK_AND );
107901 assert( TK_OR==OP_Or )((void) (0)); testcase( op==TK_OR );
107902 assert( TK_PLUS==OP_Add )((void) (0)); testcase( op==TK_PLUS );
107903 assert( TK_MINUS==OP_Subtract )((void) (0)); testcase( op==TK_MINUS );
107904 assert( TK_REM==OP_Remainder )((void) (0)); testcase( op==TK_REM );
107905 assert( TK_BITAND==OP_BitAnd )((void) (0)); testcase( op==TK_BITAND );
107906 assert( TK_BITOR==OP_BitOr )((void) (0)); testcase( op==TK_BITOR );
107907 assert( TK_SLASH==OP_Divide )((void) (0)); testcase( op==TK_SLASH );
107908 assert( TK_LSHIFT==OP_ShiftLeft )((void) (0)); testcase( op==TK_LSHIFT );
107909 assert( TK_RSHIFT==OP_ShiftRight )((void) (0)); testcase( op==TK_RSHIFT );
107910 assert( TK_CONCAT==OP_Concat )((void) (0)); testcase( op==TK_CONCAT );
107911 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
107912 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
107913 sqlite3VdbeAddOp3(v, op, r2, r1, target);
107914 testcase( regFree1==0 );
107915 testcase( regFree2==0 );
107916 break;
107917 }
107918 case TK_UMINUS173: {
107919 Expr *pLeft = pExpr->pLeft;
107920 assert( pLeft )((void) (0));
107921 if( pLeft->op==TK_INTEGER155 ){
107922 codeInteger(pParse, pLeft, 1, target);
107923 return target;
107924#ifndef SQLITE_OMIT_FLOATING_POINT
107925 }else if( pLeft->op==TK_FLOAT153 ){
107926 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
107927 codeReal(v, pLeft->u.zToken, 1, target);
107928 return target;
107929#endif
107930 }else{
107931 tempX.op = TK_INTEGER155;
107932 tempX.flags = EP_IntValue0x000800|EP_TokenOnly0x010000;
107933 tempX.u.iValue = 0;
107934 ExprClearVVAProperties(&tempX);
107935 r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
107936 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
107937 sqlite3VdbeAddOp3(v, OP_Subtract107, r2, r1, target);
107938 testcase( regFree2==0 );
107939 }
107940 break;
107941 }
107942 case TK_BITNOT114:
107943 case TK_NOT19: {
107944 assert( TK_BITNOT==OP_BitNot )((void) (0)); testcase( op==TK_BITNOT );
107945 assert( TK_NOT==OP_Not )((void) (0)); testcase( op==TK_NOT );
107946 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
107947 testcase( regFree1==0 );
107948 sqlite3VdbeAddOp2(v, op, r1, inReg);
107949 break;
107950 }
107951 case TK_TRUTH175: {
107952 int isTrue; /* IS TRUE or IS NOT TRUE */
107953 int bNormal; /* IS TRUE or IS FALSE */
107954 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
107955 testcase( regFree1==0 );
107956 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
107957 bNormal = pExpr->op2==TK_IS45;
107958 testcase( isTrue && bNormal);
107959 testcase( !isTrue && bNormal);
107960 sqlite3VdbeAddOp4Int(v, OP_IsTrue91, r1, inReg, !isTrue, isTrue ^ bNormal);
107961 break;
107962 }
107963 case TK_ISNULL50:
107964 case TK_NOTNULL51: {
107965 int addr;
107966 assert( TK_ISNULL==OP_IsNull )((void) (0)); testcase( op==TK_ISNULL );
107967 assert( TK_NOTNULL==OP_NotNull )((void) (0)); testcase( op==TK_NOTNULL );
107968 sqlite3VdbeAddOp2(v, OP_Integer71, 1, target);
107969 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
107970 testcase( regFree1==0 );
107971 addr = sqlite3VdbeAddOp1(v, op, r1);
107972 VdbeCoverageIf(v, op==TK_ISNULL);
107973 VdbeCoverageIf(v, op==TK_NOTNULL);
107974 sqlite3VdbeAddOp2(v, OP_Integer71, 0, target);
107975 sqlite3VdbeJumpHere(v, addr);
107976 break;
107977 }
107978 case TK_AGG_FUNCTION168: {
107979 AggInfo *pInfo = pExpr->pAggInfo;
107980 if( pInfo==0
107981 || NEVER(pExpr->iAgg<0)(pExpr->iAgg<0)
107982 || NEVER(pExpr->iAgg>=pInfo->nFunc)(pExpr->iAgg>=pInfo->nFunc)
107983 ){
107984 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
107985 sqlite3ErrorMsg(pParse, "misuse of aggregate: %#T()", pExpr);
107986 }else{
107987 return pInfo->aFunc[pExpr->iAgg].iMem;
107988 }
107989 break;
107990 }
107991 case TK_FUNCTION172: {
107992 ExprList *pFarg; /* List of function arguments */
107993 int nFarg; /* Number of function arguments */
107994 FuncDef *pDef; /* The function definition object */
107995 const char *zId; /* The function name */
107996 u32 constMask = 0; /* Mask of function arguments that are constant */
107997 int i; /* Loop counter */
107998 sqlite3 *db = pParse->db; /* The database connection */
107999 u8 enc = ENC(db)((db)->enc); /* The text encoding used by this database */
108000 CollSeq *pColl = 0; /* A collating sequence */
108001
108002#ifndef SQLITE_OMIT_WINDOWFUNC
108003 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
108004 return pExpr->y.pWin->regResult;
108005 }
108006#endif
108007
108008 if( ConstFactorOk(pParse)((pParse)->okConstFactor) && sqlite3ExprIsConstantNotJoin(pExpr) ){
108009 /* SQL functions can be expensive. So try to avoid running them
108010 ** multiple times if we know they always give the same result */
108011 return sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
108012 }
108013 assert( !ExprHasProperty(pExpr, EP_TokenOnly) )((void) (0));
108014 assert( ExprUseXList(pExpr) )((void) (0));
108015 pFarg = pExpr->x.pList;
108016 nFarg = pFarg ? pFarg->nExpr : 0;
108017 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
108018 zId = pExpr->u.zToken;
108019 pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0);
108020#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION1
108021 if( pDef==0 && pParse->explain ){
108022 pDef = sqlite3FindFunction(db, "unknown", nFarg, enc, 0);
108023 }
108024#endif
108025 if( pDef==0 || pDef->xFinalize!=0 ){
108026 sqlite3ErrorMsg(pParse, "unknown function: %#T()", pExpr);
108027 break;
108028 }
108029 if( pDef->funcFlags & SQLITE_FUNC_INLINE0x00400000 ){
108030 assert( (pDef->funcFlags & SQLITE_FUNC_UNSAFE)==0 )((void) (0));
108031 assert( (pDef->funcFlags & SQLITE_FUNC_DIRECT)==0 )((void) (0));
108032 return exprCodeInlineFunction(pParse, pFarg,
108033 SQLITE_PTR_TO_INT(pDef->pUserData)((int)(long int)(pDef->pUserData)), target);
108034 }else if( pDef->funcFlags & (SQLITE_FUNC_DIRECT0x00080000|SQLITE_FUNC_UNSAFE0x00200000) ){
108035 sqlite3ExprFunctionUsable(pParse, pExpr, pDef);
108036 }
108037
108038 for(i=0; i<nFarg; i++){
108039 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
108040 testcase( i==31 );
108041 constMask |= MASKBIT32(i)(((unsigned int)1)<<(i));
108042 }
108043 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL0x0020)!=0 && !pColl ){
108044 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
108045 }
108046 }
108047 if( pFarg ){
108048 if( constMask ){
108049 r1 = pParse->nMem+1;
108050 pParse->nMem += nFarg;
108051 }else{
108052 r1 = sqlite3GetTempRange(pParse, nFarg);
108053 }
108054
108055 /* For length() and typeof() functions with a column argument,
108056 ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG
108057 ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data
108058 ** loading.
108059 */
108060 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH0x0040|SQLITE_FUNC_TYPEOF0x0080))!=0 ){
108061 u8 exprOp;
108062 assert( nFarg==1 )((void) (0));
108063 assert( pFarg->a[0].pExpr!=0 )((void) (0));
108064 exprOp = pFarg->a[0].pExpr->op;
108065 if( exprOp==TK_COLUMN167 || exprOp==TK_AGG_COLUMN169 ){
108066 assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG )((void) (0));
108067 assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG )((void) (0));
108068 testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
108069 pFarg->a[0].pExpr->op2 =
108070 pDef->funcFlags & (OPFLAG_LENGTHARG0x40|OPFLAG_TYPEOFARG0x80);
108071 }
108072 }
108073
108074 sqlite3ExprCodeExprList(pParse, pFarg, r1, 0,
108075 SQLITE_ECEL_DUP0x01|SQLITE_ECEL_FACTOR0x02);
108076 }else{
108077 r1 = 0;
108078 }
108079#ifndef SQLITE_OMIT_VIRTUALTABLE
108080 /* Possibly overload the function if the first argument is
108081 ** a virtual table column.
108082 **
108083 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
108084 ** second argument, not the first, as the argument to test to
108085 ** see if it is a column in a virtual table. This is done because
108086 ** the left operand of infix functions (the operand we want to
108087 ** control overloading) ends up as the second argument to the
108088 ** function. The expression "A glob B" is equivalent to
108089 ** "glob(B,A). We want to use the A in "A glob B" to test
108090 ** for function overloading. But we use the B term in "glob(B,A)".
108091 */
108092 if( nFarg>=2 && ExprHasProperty(pExpr, EP_InfixFunc)(((pExpr)->flags&(0x000100))!=0) ){
108093 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
108094 }else if( nFarg>0 ){
108095 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
108096 }
108097#endif
108098 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL0x0020 ){
108099 if( !pColl ) pColl = db->pDfltColl;
108100 sqlite3VdbeAddOp4(v, OP_CollSeq85, 0, 0, 0, (char *)pColl, P4_COLLSEQ(-2));
108101 }
108102 sqlite3VdbeAddFunctionCall(pParse, constMask, r1, target, nFarg,
108103 pDef, pExpr->op2);
108104 if( nFarg ){
108105 if( constMask==0 ){
108106 sqlite3ReleaseTempRange(pParse, r1, nFarg);
108107 }else{
108108 sqlite3VdbeReleaseRegisters(pParse, r1, nFarg, constMask, 1);
108109 }
108110 }
108111 return target;
108112 }
108113#ifndef SQLITE_OMIT_SUBQUERY
108114 case TK_EXISTS20:
108115 case TK_SELECT138: {
108116 int nCol;
108117 testcase( op==TK_EXISTS );
108118 testcase( op==TK_SELECT );
108119 if( pParse->db->mallocFailed ){
108120 return 0;
108121 }else if( op==TK_SELECT138
108122 && ALWAYS( ExprUseXSelect(pExpr) )((((pExpr)->flags&0x001000)!=0))
108123 && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1
108124 ){
108125 sqlite3SubselectError(pParse, nCol, 1);
108126 }else{
108127 return sqlite3CodeSubselect(pParse, pExpr);
108128 }
108129 break;
108130 }
108131 case TK_SELECT_COLUMN178: {
108132 int n;
108133 Expr *pLeft = pExpr->pLeft;
108134 if( pLeft->iTable==0 || pParse->withinRJSubrtn > pLeft->op2 ){
108135 pLeft->iTable = sqlite3CodeSubselect(pParse, pLeft);
108136 pLeft->op2 = pParse->withinRJSubrtn;
108137 }
108138 assert( pLeft->op==TK_SELECT || pLeft->op==TK_ERROR )((void) (0));
108139 n = sqlite3ExprVectorSize(pLeft);
108140 if( pExpr->iTable!=n ){
108141 sqlite3ErrorMsg(pParse, "%d columns assigned %d values",
108142 pExpr->iTable, n);
108143 }
108144 return pLeft->iTable + pExpr->iColumn;
108145 }
108146 case TK_IN49: {
108147 int destIfFalse = sqlite3VdbeMakeLabel(pParse);
108148 int destIfNull = sqlite3VdbeMakeLabel(pParse);
108149 sqlite3VdbeAddOp2(v, OP_Null75, 0, target);
108150 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
108151 sqlite3VdbeAddOp2(v, OP_Integer71, 1, target);
108152 sqlite3VdbeResolveLabel(v, destIfFalse);
108153 sqlite3VdbeAddOp2(v, OP_AddImm86, target, 0);
108154 sqlite3VdbeResolveLabel(v, destIfNull);
108155 return target;
108156 }
108157#endif /* SQLITE_OMIT_SUBQUERY */
108158
108159
108160 /*
108161 ** x BETWEEN y AND z
108162 **
108163 ** This is equivalent to
108164 **
108165 ** x>=y AND x<=z
108166 **
108167 ** X is stored in pExpr->pLeft.
108168 ** Y is stored in pExpr->pList->a[0].pExpr.
108169 ** Z is stored in pExpr->pList->a[1].pExpr.
108170 */
108171 case TK_BETWEEN48: {
108172 exprCodeBetween(pParse, pExpr, target, 0, 0);
108173 return target;
108174 }
108175 case TK_COLLATE113: {
108176 if( !ExprHasProperty(pExpr, EP_Collate)(((pExpr)->flags&(0x000200))!=0)
108177 && ALWAYS(pExpr->pLeft)(pExpr->pLeft)
108178 && pExpr->pLeft->op==TK_FUNCTION172
108179 ){
108180 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
108181 if( inReg!=target ){
108182 sqlite3VdbeAddOp2(v, OP_SCopy81, inReg, target);
108183 inReg = target;
108184 }
108185 sqlite3VdbeAddOp1(v, OP_ClrSubtype179, inReg);
108186 return inReg;
108187 }else{
108188 pExpr = pExpr->pLeft;
108189 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. */
108190 }
108191 }
108192 case TK_SPAN181:
108193 case TK_UPLUS174: {
108194 pExpr = pExpr->pLeft;
108195 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
108196 }
108197
108198 case TK_TRIGGER77: {
108199 /* If the opcode is TK_TRIGGER, then the expression is a reference
108200 ** to a column in the new.* or old.* pseudo-tables available to
108201 ** trigger programs. In this case Expr.iTable is set to 1 for the
108202 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
108203 ** is set to the column of the pseudo-table to read, or to -1 to
108204 ** read the rowid field.
108205 **
108206 ** The expression is implemented using an OP_Param opcode. The p1
108207 ** parameter is set to 0 for an old.rowid reference, or to (i+1)
108208 ** to reference another column of the old.* pseudo-table, where
108209 ** i is the index of the column. For a new.rowid reference, p1 is
108210 ** set to (n+1), where n is the number of columns in each pseudo-table.
108211 ** For a reference to any other column in the new.* pseudo-table, p1
108212 ** is set to (n+2+i), where n and i are as defined previously. For
108213 ** example, if the table on which triggers are being fired is
108214 ** declared as:
108215 **
108216 ** CREATE TABLE t1(a, b);
108217 **
108218 ** Then p1 is interpreted as follows:
108219 **
108220 ** p1==0 -> old.rowid p1==3 -> new.rowid
108221 ** p1==1 -> old.a p1==4 -> new.a
108222 ** p1==2 -> old.b p1==5 -> new.b
108223 */
108224 Table *pTab;
108225 int iCol;
108226 int p1;
108227
108228 assert( ExprUseYTab(pExpr) )((void) (0));
108229 pTab = pExpr->y.pTab;
108230 iCol = pExpr->iColumn;
108231 p1 = pExpr->iTable * (pTab->nCol+1) + 1
108232 + sqlite3TableColumnToStorage(pTab, iCol);
108233
108234 assert( pExpr->iTable==0 || pExpr->iTable==1 )((void) (0));
108235 assert( iCol>=-1 && iCol<pTab->nCol )((void) (0));
108236 assert( pTab->iPKey<0 || iCol!=pTab->iPKey )((void) (0));
108237 assert( p1>=0 && p1<(pTab->nCol*2+2) )((void) (0));
108238
108239 sqlite3VdbeAddOp2(v, OP_Param157, p1, target);
108240 VdbeComment((v, "r[%d]=%s.%s", target,
108241 (pExpr->iTable ? "new" : "old"),
108242 (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[iCol].zCnName)
108243 ));
108244
108245#ifndef SQLITE_OMIT_FLOATING_POINT
108246 /* If the column has REAL affinity, it may currently be stored as an
108247 ** integer. Use OP_RealAffinity to make sure it is really real.
108248 **
108249 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
108250 ** floating point when extracting it from the record. */
108251 if( iCol>=0 && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL0x45 ){
108252 sqlite3VdbeAddOp1(v, OP_RealAffinity87, target);
108253 }
108254#endif
108255 break;
108256 }
108257
108258 case TK_VECTOR177: {
108259 sqlite3ErrorMsg(pParse, "row value misused");
108260 break;
108261 }
108262
108263 /* TK_IF_NULL_ROW Expr nodes are inserted ahead of expressions
108264 ** that derive from the right-hand table of a LEFT JOIN. The
108265 ** Expr.iTable value is the table number for the right-hand table.
108266 ** The expression is only evaluated if that table is not currently
108267 ** on a LEFT JOIN NULL row.
108268 */
108269 case TK_IF_NULL_ROW179: {
108270 int addrINR;
108271 u8 okConstFactor = pParse->okConstFactor;
108272 addrINR = sqlite3VdbeAddOp1(v, OP_IfNullRow18, pExpr->iTable);
108273 /* Temporarily disable factoring of constant expressions, since
108274 ** even though expressions may appear to be constant, they are not
108275 ** really constant because they originate from the right-hand side
108276 ** of a LEFT JOIN. */
108277 pParse->okConstFactor = 0;
108278 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
108279 pParse->okConstFactor = okConstFactor;
108280 sqlite3VdbeJumpHere(v, addrINR);
108281 sqlite3VdbeChangeP3(v, addrINR, inReg);
108282 break;
108283 }
108284
108285 /*
108286 ** Form A:
108287 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
108288 **
108289 ** Form B:
108290 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
108291 **
108292 ** Form A is can be transformed into the equivalent form B as follows:
108293 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
108294 ** WHEN x=eN THEN rN ELSE y END
108295 **
108296 ** X (if it exists) is in pExpr->pLeft.
108297 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
108298 ** odd. The Y is also optional. If the number of elements in x.pList
108299 ** is even, then Y is omitted and the "otherwise" result is NULL.
108300 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
108301 **
108302 ** The result of the expression is the Ri for the first matching Ei,
108303 ** or if there is no matching Ei, the ELSE term Y, or if there is
108304 ** no ELSE term, NULL.
108305 */
108306 case TK_CASE157: {
108307 int endLabel; /* GOTO label for end of CASE stmt */
108308 int nextCase; /* GOTO label for next WHEN clause */
108309 int nExpr; /* 2x number of WHEN terms */
108310 int i; /* Loop counter */
108311 ExprList *pEList; /* List of WHEN terms */
108312 struct ExprList_item *aListelem; /* Array of WHEN terms */
108313 Expr opCompare; /* The X==Ei expression */
108314 Expr *pX; /* The X expression */
108315 Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */
108316 Expr *pDel = 0;
108317 sqlite3 *db = pParse->db;
108318
108319 assert( ExprUseXList(pExpr) && pExpr->x.pList!=0 )((void) (0));
108320 assert(pExpr->x.pList->nExpr > 0)((void) (0));
108321 pEList = pExpr->x.pList;
108322 aListelem = pEList->a;
108323 nExpr = pEList->nExpr;
108324 endLabel = sqlite3VdbeMakeLabel(pParse);
108325 if( (pX = pExpr->pLeft)!=0 ){
108326 pDel = sqlite3ExprDup(db, pX, 0);
108327 if( db->mallocFailed ){
108328 sqlite3ExprDelete(db, pDel);
108329 break;
108330 }
108331 testcase( pX->op==TK_COLUMN );
108332 exprToRegister(pDel, exprCodeVector(pParse, pDel, &regFree1));
108333 testcase( regFree1==0 );
108334 memset(&opCompare, 0, sizeof(opCompare));
108335 opCompare.op = TK_EQ53;
108336 opCompare.pLeft = pDel;
108337 pTest = &opCompare;
108338 /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
108339 ** The value in regFree1 might get SCopy-ed into the file result.
108340 ** So make sure that the regFree1 register is not reused for other
108341 ** purposes and possibly overwritten. */
108342 regFree1 = 0;
108343 }
108344 for(i=0; i<nExpr-1; i=i+2){
108345 if( pX ){
108346 assert( pTest!=0 )((void) (0));
108347 opCompare.pRight = aListelem[i].pExpr;
108348 }else{
108349 pTest = aListelem[i].pExpr;
108350 }
108351 nextCase = sqlite3VdbeMakeLabel(pParse);
108352 testcase( pTest->op==TK_COLUMN );
108353 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL0x10);
108354 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
108355 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
108356 sqlite3VdbeGoto(v, endLabel);
108357 sqlite3VdbeResolveLabel(v, nextCase);
108358 }
108359 if( (nExpr&1)!=0 ){
108360 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
108361 }else{
108362 sqlite3VdbeAddOp2(v, OP_Null75, 0, target);
108363 }
108364 sqlite3ExprDelete(db, pDel);
108365 setDoNotMergeFlagOnCopy(v);
108366 sqlite3VdbeResolveLabel(v, endLabel);
108367 break;
108368 }
108369#ifndef SQLITE_OMIT_TRIGGER
108370 case TK_RAISE71: {
108371 assert( pExpr->affExpr==OE_Rollback((void) (0))
108372 || pExpr->affExpr==OE_Abort((void) (0))
108373 || pExpr->affExpr==OE_Fail((void) (0))
108374 || pExpr->affExpr==OE_Ignore((void) (0))
108375 )((void) (0));
108376 if( !pParse->pTriggerTab && !pParse->nested ){
108377 sqlite3ErrorMsg(pParse,
108378 "RAISE() may only be used within a trigger-program");
108379 return 0;
108380 }
108381 if( pExpr->affExpr==OE_Abort2 ){
108382 sqlite3MayAbort(pParse);
108383 }
108384 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
108385 if( pExpr->affExpr==OE_Ignore4 ){
108386 sqlite3VdbeAddOp4(
108387 v, OP_Halt70, SQLITE_OK0, OE_Ignore4, 0, pExpr->u.zToken,0);
108388 VdbeCoverage(v);
108389 }else{
108390 sqlite3HaltConstraint(pParse,
108391 pParse->pTriggerTab ? SQLITE_CONSTRAINT_TRIGGER(19 | (7<<8)) : SQLITE_ERROR1,
108392 pExpr->affExpr, pExpr->u.zToken, 0, 0);
108393 }
108394
108395 break;
108396 }
108397#endif
108398 }
108399 sqlite3ReleaseTempReg(pParse, regFree1);
108400 sqlite3ReleaseTempReg(pParse, regFree2);
108401 return inReg;
108402}
108403
108404/*
108405** Generate code that will evaluate expression pExpr just one time
108406** per prepared statement execution.
108407**
108408** If the expression uses functions (that might throw an exception) then
108409** guard them with an OP_Once opcode to ensure that the code is only executed
108410** once. If no functions are involved, then factor the code out and put it at
108411** the end of the prepared statement in the initialization section.
108412**
108413** If regDest>=0 then the result is always stored in that register and the
108414** result is not reusable. If regDest<0 then this routine is free to
108415** store the value whereever it wants. The register where the expression
108416** is stored is returned. When regDest<0, two identical expressions might
108417** code to the same register, if they do not contain function calls and hence
108418** are factored out into the initialization section at the end of the
108419** prepared statement.
108420*/
108421SQLITE_PRIVATEstatic int sqlite3ExprCodeRunJustOnce(
108422 Parse *pParse, /* Parsing context */
108423 Expr *pExpr, /* The expression to code when the VDBE initializes */
108424 int regDest /* Store the value in this register */
108425){
108426 ExprList *p;
108427 assert( ConstFactorOk(pParse) )((void) (0));
108428 p = pParse->pConstExpr;
108429 if( regDest<0 && p ){
108430 struct ExprList_item *pItem;
108431 int i;
108432 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
108433 if( pItem->fg.reusable
108434 && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0
108435 ){
108436 return pItem->u.iConstExprReg;
108437 }
108438 }
108439 }
108440 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
108441 if( pExpr!=0 && ExprHasProperty(pExpr, EP_HasFunc)(((pExpr)->flags&(0x000008))!=0) ){
108442 Vdbe *v = pParse->pVdbe;
108443 int addr;
108444 assert( v )((void) (0));
108445 addr = sqlite3VdbeAddOp0(v, OP_Once14); VdbeCoverage(v);
108446 pParse->okConstFactor = 0;
108447 if( !pParse->db->mallocFailed ){
108448 if( regDest<0 ) regDest = ++pParse->nMem;
108449 sqlite3ExprCode(pParse, pExpr, regDest);
108450 }
108451 pParse->okConstFactor = 1;
108452 sqlite3ExprDelete(pParse->db, pExpr);
108453 sqlite3VdbeJumpHere(v, addr);
108454 }else{
108455 p = sqlite3ExprListAppend(pParse, p, pExpr);
108456 if( p ){
108457 struct ExprList_item *pItem = &p->a[p->nExpr-1];
108458 pItem->fg.reusable = regDest<0;
108459 if( regDest<0 ) regDest = ++pParse->nMem;
108460 pItem->u.iConstExprReg = regDest;
108461 }
108462 pParse->pConstExpr = p;
108463 }
108464 return regDest;
108465}
108466
108467/*
108468** Generate code to evaluate an expression and store the results
108469** into a register. Return the register number where the results
108470** are stored.
108471**
108472** If the register is a temporary register that can be deallocated,
108473** then write its number into *pReg. If the result register is not
108474** a temporary, then set *pReg to zero.
108475**
108476** If pExpr is a constant, then this routine might generate this
108477** code to fill the register in the initialization section of the
108478** VDBE program, in order to factor it out of the evaluation loop.
108479*/
108480SQLITE_PRIVATEstatic int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
108481 int r2;
108482 pExpr = sqlite3ExprSkipCollateAndLikely(pExpr);
108483 if( ConstFactorOk(pParse)((pParse)->okConstFactor)
108484 && ALWAYS(pExpr!=0)(pExpr!=0)
108485 && pExpr->op!=TK_REGISTER176
108486 && sqlite3ExprIsConstantNotJoin(pExpr)
108487 ){
108488 *pReg = 0;
108489 r2 = sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
108490 }else{
108491 int r1 = sqlite3GetTempReg(pParse);
108492 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
108493 if( r2==r1 ){
108494 *pReg = r1;
108495 }else{
108496 sqlite3ReleaseTempReg(pParse, r1);
108497 *pReg = 0;
108498 }
108499 }
108500 return r2;
108501}
108502
108503/*
108504** Generate code that will evaluate expression pExpr and store the
108505** results in register target. The results are guaranteed to appear
108506** in register target.
108507*/
108508SQLITE_PRIVATEstatic void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
108509 int inReg;
108510
108511 assert( pExpr==0 || !ExprHasVVAProperty(pExpr,EP_Immutable) )((void) (0));
108512 assert( target>0 && target<=pParse->nMem )((void) (0));
108513 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed )((void) (0));
108514 if( pParse->pVdbe==0 ) return;
108515 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
108516 if( inReg!=target ){
108517 u8 op;
108518 if( ALWAYS(pExpr)(pExpr) && ExprHasProperty(pExpr,EP_Subquery)(((pExpr)->flags&(0x400000))!=0) ){
108519 op = OP_Copy80;
108520 }else{
108521 op = OP_SCopy81;
108522 }
108523 sqlite3VdbeAddOp2(pParse->pVdbe, op, inReg, target);
108524 }
108525}
108526
108527/*
108528** Make a transient copy of expression pExpr and then code it using
108529** sqlite3ExprCode(). This routine works just like sqlite3ExprCode()
108530** except that the input expression is guaranteed to be unchanged.
108531*/
108532SQLITE_PRIVATEstatic void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){
108533 sqlite3 *db = pParse->db;
108534 pExpr = sqlite3ExprDup(db, pExpr, 0);
108535 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
108536 sqlite3ExprDelete(db, pExpr);
108537}
108538
108539/*
108540** Generate code that will evaluate expression pExpr and store the
108541** results in register target. The results are guaranteed to appear
108542** in register target. If the expression is constant, then this routine
108543** might choose to code the expression at initialization time.
108544*/
108545SQLITE_PRIVATEstatic void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
108546 if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pExpr) ){
108547 sqlite3ExprCodeRunJustOnce(pParse, pExpr, target);
108548 }else{
108549 sqlite3ExprCodeCopy(pParse, pExpr, target);
108550 }
108551}
108552
108553/*
108554** Generate code that pushes the value of every element of the given
108555** expression list into a sequence of registers beginning at target.
108556**
108557** Return the number of elements evaluated. The number returned will
108558** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF
108559** is defined.
108560**
108561** The SQLITE_ECEL_DUP flag prevents the arguments from being
108562** filled using OP_SCopy. OP_Copy must be used instead.
108563**
108564** The SQLITE_ECEL_FACTOR argument allows constant arguments to be
108565** factored out into initialization code.
108566**
108567** The SQLITE_ECEL_REF flag means that expressions in the list with
108568** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored
108569** in registers at srcReg, and so the value can be copied from there.
108570** If SQLITE_ECEL_OMITREF is also set, then the values with u.x.iOrderByCol>0
108571** are simply omitted rather than being copied from srcReg.
108572*/
108573SQLITE_PRIVATEstatic int sqlite3ExprCodeExprList(
108574 Parse *pParse, /* Parsing context */
108575 ExprList *pList, /* The expression list to be coded */
108576 int target, /* Where to write results */
108577 int srcReg, /* Source registers if SQLITE_ECEL_REF */
108578 u8 flags /* SQLITE_ECEL_* flags */
108579){
108580 struct ExprList_item *pItem;
108581 int i, j, n;
108582 u8 copyOp = (flags & SQLITE_ECEL_DUP0x01) ? OP_Copy80 : OP_SCopy81;
108583 Vdbe *v = pParse->pVdbe;
108584 assert( pList!=0 )((void) (0));
108585 assert( target>0 )((void) (0));
108586 assert( pParse->pVdbe!=0 )((void) (0)); /* Never gets this far otherwise */
108587 n = pList->nExpr;
108588 if( !ConstFactorOk(pParse)((pParse)->okConstFactor) ) flags &= ~SQLITE_ECEL_FACTOR0x02;
108589 for(pItem=pList->a, i=0; i<n; i++, pItem++){
108590 Expr *pExpr = pItem->pExpr;
108591#ifdef SQLITE_ENABLE_SORTER_REFERENCES
108592 if( pItem->fg.bSorterRef ){
108593 i--;
108594 n--;
108595 }else
108596#endif
108597 if( (flags & SQLITE_ECEL_REF0x04)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
108598 if( flags & SQLITE_ECEL_OMITREF0x08 ){
108599 i--;
108600 n--;
108601 }else{
108602 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
108603 }
108604 }else if( (flags & SQLITE_ECEL_FACTOR0x02)!=0
108605 && sqlite3ExprIsConstantNotJoin(pExpr)
108606 ){
108607 sqlite3ExprCodeRunJustOnce(pParse, pExpr, target+i);
108608 }else{
108609 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
108610 if( inReg!=target+i ){
108611 VdbeOp *pOp;
108612 if( copyOp==OP_Copy80
108613 && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy80
108614 && pOp->p1+pOp->p3+1==inReg
108615 && pOp->p2+pOp->p3+1==target+i
108616 && pOp->p5==0 /* The do-not-merge flag must be clear */
108617 ){
108618 pOp->p3++;
108619 }else{
108620 sqlite3VdbeAddOp2(v, copyOp, inReg, target+i);
108621 }
108622 }
108623 }
108624 }
108625 return n;
108626}
108627
108628/*
108629** Generate code for a BETWEEN operator.
108630**
108631** x BETWEEN y AND z
108632**
108633** The above is equivalent to
108634**
108635** x>=y AND x<=z
108636**
108637** Code it as such, taking care to do the common subexpression
108638** elimination of x.
108639**
108640** The xJumpIf parameter determines details:
108641**
108642** NULL: Store the boolean result in reg[dest]
108643** sqlite3ExprIfTrue: Jump to dest if true
108644** sqlite3ExprIfFalse: Jump to dest if false
108645**
108646** The jumpIfNull parameter is ignored if xJumpIf is NULL.
108647*/
108648static void exprCodeBetween(
108649 Parse *pParse, /* Parsing and code generating context */
108650 Expr *pExpr, /* The BETWEEN expression */
108651 int dest, /* Jump destination or storage location */
108652 void (*xJump)(Parse*,Expr*,int,int), /* Action to take */
108653 int jumpIfNull /* Take the jump if the BETWEEN is NULL */
108654){
108655 Expr exprAnd; /* The AND operator in x>=y AND x<=z */
108656 Expr compLeft; /* The x>=y term */
108657 Expr compRight; /* The x<=z term */
108658 int regFree1 = 0; /* Temporary use register */
108659 Expr *pDel = 0;
108660 sqlite3 *db = pParse->db;
108661
108662 memset(&compLeft, 0, sizeof(Expr));
108663 memset(&compRight, 0, sizeof(Expr));
108664 memset(&exprAnd, 0, sizeof(Expr));
108665
108666 assert( ExprUseXList(pExpr) )((void) (0));
108667 pDel = sqlite3ExprDup(db, pExpr->pLeft, 0);
108668 if( db->mallocFailed==0 ){
108669 exprAnd.op = TK_AND44;
108670 exprAnd.pLeft = &compLeft;
108671 exprAnd.pRight = &compRight;
108672 compLeft.op = TK_GE57;
108673 compLeft.pLeft = pDel;
108674 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
108675 compRight.op = TK_LE55;
108676 compRight.pLeft = pDel;
108677 compRight.pRight = pExpr->x.pList->a[1].pExpr;
108678 exprToRegister(pDel, exprCodeVector(pParse, pDel, &regFree1));
108679 if( xJump ){
108680 xJump(pParse, &exprAnd, dest, jumpIfNull);
108681 }else{
108682 /* Mark the expression is being from the ON or USING clause of a join
108683 ** so that the sqlite3ExprCodeTarget() routine will not attempt to move
108684 ** it into the Parse.pConstExpr list. We should use a new bit for this,
108685 ** for clarity, but we are out of bits in the Expr.flags field so we
108686 ** have to reuse the EP_OuterON bit. Bummer. */
108687 pDel->flags |= EP_OuterON0x000001;
108688 sqlite3ExprCodeTarget(pParse, &exprAnd, dest);
108689 }
108690 sqlite3ReleaseTempReg(pParse, regFree1);
108691 }
108692 sqlite3ExprDelete(db, pDel);
108693
108694 /* Ensure adequate test coverage */
108695 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1==0 );
108696 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1!=0 );
108697 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1==0 );
108698 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1!=0 );
108699 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1==0 );
108700 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1!=0 );
108701 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1==0 );
108702 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1!=0 );
108703 testcase( xJump==0 );
108704}
108705
108706/*
108707** Generate code for a boolean expression such that a jump is made
108708** to the label "dest" if the expression is true but execution
108709** continues straight thru if the expression is false.
108710**
108711** If the expression evaluates to NULL (neither true nor false), then
108712** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
108713**
108714** This code depends on the fact that certain token values (ex: TK_EQ)
108715** are the same as opcode values (ex: OP_Eq) that implement the corresponding
108716** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
108717** the make process cause these values to align. Assert()s in the code
108718** below verify that the numbers are aligned correctly.
108719*/
108720SQLITE_PRIVATEstatic void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
108721 Vdbe *v = pParse->pVdbe;
108722 int op = 0;
108723 int regFree1 = 0;
108724 int regFree2 = 0;
108725 int r1, r2;
108726
108727 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 )((void) (0));
108728 if( NEVER(v==0)(v==0) ) return; /* Existence of VDBE checked by caller */
108729 if( NEVER(pExpr==0)(pExpr==0) ) return; /* No way this can happen */
108730 assert( !ExprHasVVAProperty(pExpr, EP_Immutable) )((void) (0));
108731 op = pExpr->op;
108732 switch( op ){
108733 case TK_AND44:
108734 case TK_OR43: {
108735 Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr);
108736 if( pAlt!=pExpr ){
108737 sqlite3ExprIfTrue(pParse, pAlt, dest, jumpIfNull);
108738 }else if( op==TK_AND44 ){
108739 int d2 = sqlite3VdbeMakeLabel(pParse);
108740 testcase( jumpIfNull==0 );
108741 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,
108742 jumpIfNull^SQLITE_JUMPIFNULL0x10);
108743 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
108744 sqlite3VdbeResolveLabel(v, d2);
108745 }else{
108746 testcase( jumpIfNull==0 );
108747 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
108748 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
108749 }
108750 break;
108751 }
108752 case TK_NOT19: {
108753 testcase( jumpIfNull==0 );
108754 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
108755 break;
108756 }
108757 case TK_TRUTH175: {
108758 int isNot; /* IS NOT TRUE or IS NOT FALSE */
108759 int isTrue; /* IS TRUE or IS NOT TRUE */
108760 testcase( jumpIfNull==0 );
108761 isNot = pExpr->op2==TK_ISNOT171;
108762 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
108763 testcase( isTrue && isNot );
108764 testcase( !isTrue && isNot );
108765 if( isTrue ^ isNot ){
108766 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
108767 isNot ? SQLITE_JUMPIFNULL0x10 : 0);
108768 }else{
108769 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
108770 isNot ? SQLITE_JUMPIFNULL0x10 : 0);
108771 }
108772 break;
108773 }
108774 case TK_IS45:
108775 case TK_ISNOT171:
108776 testcase( op==TK_IS );
108777 testcase( op==TK_ISNOT );
108778 op = (op==TK_IS45) ? TK_EQ53 : TK_NE52;
108779 jumpIfNull = SQLITE_NULLEQ0x80;
108780 /* no break */ deliberate_fall_through
108781 case TK_LT56:
108782 case TK_LE55:
108783 case TK_GT54:
108784 case TK_GE57:
108785 case TK_NE52:
108786 case TK_EQ53: {
108787 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
108788 testcase( jumpIfNull==0 );
108789 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
108790 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
108791 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
108792 r1, r2, dest, jumpIfNull, ExprHasProperty(pExpr,EP_Commuted)(((pExpr)->flags&(0x000400))!=0));
108793 assert(TK_LT==OP_Lt)((void) (0)); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
108794 assert(TK_LE==OP_Le)((void) (0)); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
108795 assert(TK_GT==OP_Gt)((void) (0)); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
108796 assert(TK_GE==OP_Ge)((void) (0)); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
108797 assert(TK_EQ==OP_Eq)((void) (0)); testcase(op==OP_Eq);
108798 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
108799 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
108800 assert(TK_NE==OP_Ne)((void) (0)); testcase(op==OP_Ne);
108801 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
108802 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
108803 testcase( regFree1==0 );
108804 testcase( regFree2==0 );
108805 break;
108806 }
108807 case TK_ISNULL50:
108808 case TK_NOTNULL51: {
108809 assert( TK_ISNULL==OP_IsNull )((void) (0)); testcase( op==TK_ISNULL );
108810 assert( TK_NOTNULL==OP_NotNull )((void) (0)); testcase( op==TK_NOTNULL );
108811 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
108812 sqlite3VdbeAddOp2(v, op, r1, dest);
108813 VdbeCoverageIf(v, op==TK_ISNULL);
108814 VdbeCoverageIf(v, op==TK_NOTNULL);
108815 testcase( regFree1==0 );
108816 break;
108817 }
108818 case TK_BETWEEN48: {
108819 testcase( jumpIfNull==0 );
108820 exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfTrue, jumpIfNull);
108821 break;
108822 }
108823#ifndef SQLITE_OMIT_SUBQUERY
108824 case TK_IN49: {
108825 int destIfFalse = sqlite3VdbeMakeLabel(pParse);
108826 int destIfNull = jumpIfNull ? dest : destIfFalse;
108827 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
108828 sqlite3VdbeGoto(v, dest);
108829 sqlite3VdbeResolveLabel(v, destIfFalse);
108830 break;
108831 }
108832#endif
108833 default: {
108834 default_expr:
108835 if( ExprAlwaysTrue(pExpr)(((pExpr)->flags&(0x000001|0x10000000))==0x10000000) ){
108836 sqlite3VdbeGoto(v, dest);
108837 }else if( ExprAlwaysFalse(pExpr)(((pExpr)->flags&(0x000001|0x20000000))==0x20000000) ){
108838 /* No-op */
108839 }else{
108840 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
108841 sqlite3VdbeAddOp3(v, OP_If15, r1, dest, jumpIfNull!=0);
108842 VdbeCoverage(v);
108843 testcase( regFree1==0 );
108844 testcase( jumpIfNull==0 );
108845 }
108846 break;
108847 }
108848 }
108849 sqlite3ReleaseTempReg(pParse, regFree1);
108850 sqlite3ReleaseTempReg(pParse, regFree2);
108851}
108852
108853/*
108854** Generate code for a boolean expression such that a jump is made
108855** to the label "dest" if the expression is false but execution
108856** continues straight thru if the expression is true.
108857**
108858** If the expression evaluates to NULL (neither true nor false) then
108859** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
108860** is 0.
108861*/
108862SQLITE_PRIVATEstatic void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
108863 Vdbe *v = pParse->pVdbe;
108864 int op = 0;
108865 int regFree1 = 0;
108866 int regFree2 = 0;
108867 int r1, r2;
108868
108869 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 )((void) (0));
108870 if( NEVER(v==0)(v==0) ) return; /* Existence of VDBE checked by caller */
108871 if( pExpr==0 ) return;
108872 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) )((void) (0));
108873
108874 /* The value of pExpr->op and op are related as follows:
108875 **
108876 ** pExpr->op op
108877 ** --------- ----------
108878 ** TK_ISNULL OP_NotNull
108879 ** TK_NOTNULL OP_IsNull
108880 ** TK_NE OP_Eq
108881 ** TK_EQ OP_Ne
108882 ** TK_GT OP_Le
108883 ** TK_LE OP_Gt
108884 ** TK_GE OP_Lt
108885 ** TK_LT OP_Ge
108886 **
108887 ** For other values of pExpr->op, op is undefined and unused.
108888 ** The value of TK_ and OP_ constants are arranged such that we
108889 ** can compute the mapping above using the following expression.
108890 ** Assert()s verify that the computation is correct.
108891 */
108892 op = ((pExpr->op+(TK_ISNULL50&1))^1)-(TK_ISNULL50&1);
108893
108894 /* Verify correct alignment of TK_ and OP_ constants
108895 */
108896 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull )((void) (0));
108897 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull )((void) (0));
108898 assert( pExpr->op!=TK_NE || op==OP_Eq )((void) (0));
108899 assert( pExpr->op!=TK_EQ || op==OP_Ne )((void) (0));
108900 assert( pExpr->op!=TK_LT || op==OP_Ge )((void) (0));
108901 assert( pExpr->op!=TK_LE || op==OP_Gt )((void) (0));
108902 assert( pExpr->op!=TK_GT || op==OP_Le )((void) (0));
108903 assert( pExpr->op!=TK_GE || op==OP_Lt )((void) (0));
108904
108905 switch( pExpr->op ){
108906 case TK_AND44:
108907 case TK_OR43: {
108908 Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr);
108909 if( pAlt!=pExpr ){
108910 sqlite3ExprIfFalse(pParse, pAlt, dest, jumpIfNull);
108911 }else if( pExpr->op==TK_AND44 ){
108912 testcase( jumpIfNull==0 );
108913 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
108914 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
108915 }else{
108916 int d2 = sqlite3VdbeMakeLabel(pParse);
108917 testcase( jumpIfNull==0 );
108918 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2,
108919 jumpIfNull^SQLITE_JUMPIFNULL0x10);
108920 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
108921 sqlite3VdbeResolveLabel(v, d2);
108922 }
108923 break;
108924 }
108925 case TK_NOT19: {
108926 testcase( jumpIfNull==0 );
108927 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
108928 break;
108929 }
108930 case TK_TRUTH175: {
108931 int isNot; /* IS NOT TRUE or IS NOT FALSE */
108932 int isTrue; /* IS TRUE or IS NOT TRUE */
108933 testcase( jumpIfNull==0 );
108934 isNot = pExpr->op2==TK_ISNOT171;
108935 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
108936 testcase( isTrue && isNot );
108937 testcase( !isTrue && isNot );
108938 if( isTrue ^ isNot ){
108939 /* IS TRUE and IS NOT FALSE */
108940 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
108941 isNot ? 0 : SQLITE_JUMPIFNULL0x10);
108942
108943 }else{
108944 /* IS FALSE and IS NOT TRUE */
108945 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
108946 isNot ? 0 : SQLITE_JUMPIFNULL0x10);
108947 }
108948 break;
108949 }
108950 case TK_IS45:
108951 case TK_ISNOT171:
108952 testcase( pExpr->op==TK_IS );
108953 testcase( pExpr->op==TK_ISNOT );
108954 op = (pExpr->op==TK_IS45) ? TK_NE52 : TK_EQ53;
108955 jumpIfNull = SQLITE_NULLEQ0x80;
108956 /* no break */ deliberate_fall_through
108957 case TK_LT56:
108958 case TK_LE55:
108959 case TK_GT54:
108960 case TK_GE57:
108961 case TK_NE52:
108962 case TK_EQ53: {
108963 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
108964 testcase( jumpIfNull==0 );
108965 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
108966 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
108967 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
108968 r1, r2, dest, jumpIfNull,ExprHasProperty(pExpr,EP_Commuted)(((pExpr)->flags&(0x000400))!=0));
108969 assert(TK_LT==OP_Lt)((void) (0)); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
108970 assert(TK_LE==OP_Le)((void) (0)); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
108971 assert(TK_GT==OP_Gt)((void) (0)); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
108972 assert(TK_GE==OP_Ge)((void) (0)); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
108973 assert(TK_EQ==OP_Eq)((void) (0)); testcase(op==OP_Eq);
108974 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
108975 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
108976 assert(TK_NE==OP_Ne)((void) (0)); testcase(op==OP_Ne);
108977 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
108978 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
108979 testcase( regFree1==0 );
108980 testcase( regFree2==0 );
108981 break;
108982 }
108983 case TK_ISNULL50:
108984 case TK_NOTNULL51: {
108985 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
108986 sqlite3VdbeAddOp2(v, op, r1, dest);
108987 testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL);
108988 testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL);
108989 testcase( regFree1==0 );
108990 break;
108991 }
108992 case TK_BETWEEN48: {
108993 testcase( jumpIfNull==0 );
108994 exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfFalse, jumpIfNull);
108995 break;
108996 }
108997#ifndef SQLITE_OMIT_SUBQUERY
108998 case TK_IN49: {
108999 if( jumpIfNull ){
109000 sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
109001 }else{
109002 int destIfNull = sqlite3VdbeMakeLabel(pParse);
109003 sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
109004 sqlite3VdbeResolveLabel(v, destIfNull);
109005 }
109006 break;
109007 }
109008#endif
109009 default: {
109010 default_expr:
109011 if( ExprAlwaysFalse(pExpr)(((pExpr)->flags&(0x000001|0x20000000))==0x20000000) ){
109012 sqlite3VdbeGoto(v, dest);
109013 }else if( ExprAlwaysTrue(pExpr)(((pExpr)->flags&(0x000001|0x10000000))==0x10000000) ){
109014 /* no-op */
109015 }else{
109016 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
109017 sqlite3VdbeAddOp3(v, OP_IfNot16, r1, dest, jumpIfNull!=0);
109018 VdbeCoverage(v);
109019 testcase( regFree1==0 );
109020 testcase( jumpIfNull==0 );
109021 }
109022 break;
109023 }
109024 }
109025 sqlite3ReleaseTempReg(pParse, regFree1);
109026 sqlite3ReleaseTempReg(pParse, regFree2);
109027}
109028
109029/*
109030** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before
109031** code generation, and that copy is deleted after code generation. This
109032** ensures that the original pExpr is unchanged.
109033*/
109034SQLITE_PRIVATEstatic void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){
109035 sqlite3 *db = pParse->db;
109036 Expr *pCopy = sqlite3ExprDup(db, pExpr, 0);
109037 if( db->mallocFailed==0 ){
109038 sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull);
109039 }
109040 sqlite3ExprDelete(db, pCopy);
109041}
109042
109043/*
109044** Expression pVar is guaranteed to be an SQL variable. pExpr may be any
109045** type of expression.
109046**
109047** If pExpr is a simple SQL value - an integer, real, string, blob
109048** or NULL value - then the VDBE currently being prepared is configured
109049** to re-prepare each time a new value is bound to variable pVar.
109050**
109051** Additionally, if pExpr is a simple SQL value and the value is the
109052** same as that currently bound to variable pVar, non-zero is returned.
109053** Otherwise, if the values are not the same or if pExpr is not a simple
109054** SQL value, zero is returned.
109055*/
109056static int exprCompareVariable(
109057 const Parse *pParse,
109058 const Expr *pVar,
109059 const Expr *pExpr
109060){
109061 int res = 0;
109062 int iVar;
109063 sqlite3_value *pL, *pR = 0;
109064
109065 sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF81, SQLITE_AFF_BLOB0x41, &pR);
109066 if( pR ){
109067 iVar = pVar->iColumn;
109068 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
109069 pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB0x41);
109070 if( pL ){
109071 if( sqlite3_value_type(pL)==SQLITE_TEXT3 ){
109072 sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */
109073 }
109074 res = 0==sqlite3MemCompare(pL, pR, 0);
109075 }
109076 sqlite3ValueFree(pR);
109077 sqlite3ValueFree(pL);
109078 }
109079
109080 return res;
109081}
109082
109083/*
109084** Do a deep comparison of two expression trees. Return 0 if the two
109085** expressions are completely identical. Return 1 if they differ only
109086** by a COLLATE operator at the top level. Return 2 if there are differences
109087** other than the top-level COLLATE operator.
109088**
109089** If any subelement of pB has Expr.iTable==(-1) then it is allowed
109090** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
109091**
109092** The pA side might be using TK_REGISTER. If that is the case and pB is
109093** not using TK_REGISTER but is otherwise equivalent, then still return 0.
109094**
109095** Sometimes this routine will return 2 even if the two expressions
109096** really are equivalent. If we cannot prove that the expressions are
109097** identical, we return 2 just to be safe. So if this routine
109098** returns 2, then you do not really know for certain if the two
109099** expressions are the same. But if you get a 0 or 1 return, then you
109100** can be sure the expressions are the same. In the places where
109101** this routine is used, it does not hurt to get an extra 2 - that
109102** just might result in some slightly slower code. But returning
109103** an incorrect 0 or 1 could lead to a malfunction.
109104**
109105** If pParse is not NULL then TK_VARIABLE terms in pA with bindings in
109106** pParse->pReprepare can be matched against literals in pB. The
109107** pParse->pVdbe->expmask bitmask is updated for each variable referenced.
109108** If pParse is NULL (the normal case) then any TK_VARIABLE term in
109109** Argument pParse should normally be NULL. If it is not NULL and pA or
109110** pB causes a return value of 2.
109111*/
109112SQLITE_PRIVATEstatic int sqlite3ExprCompare(
109113 const Parse *pParse,
109114 const Expr *pA,
109115 const Expr *pB,
109116 int iTab
109117){
109118 u32 combinedFlags;
109119 if( pA==0 || pB==0 ){
109120 return pB==pA ? 0 : 2;
109121 }
109122 if( pParse && pA->op==TK_VARIABLE156 && exprCompareVariable(pParse, pA, pB) ){
109123 return 0;
109124 }
109125 combinedFlags = pA->flags | pB->flags;
109126 if( combinedFlags & EP_IntValue0x000800 ){
109127 if( (pA->flags&pB->flags&EP_IntValue0x000800)!=0 && pA->u.iValue==pB->u.iValue ){
109128 return 0;
109129 }
109130 return 2;
109131 }
109132 if( pA->op!=pB->op || pA->op==TK_RAISE71 ){
109133 if( pA->op==TK_COLLATE113 && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){
109134 return 1;
109135 }
109136 if( pB->op==TK_COLLATE113 && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
109137 return 1;
109138 }
109139 return 2;
109140 }
109141 assert( !ExprHasProperty(pA, EP_IntValue) )((void) (0));
109142 assert( !ExprHasProperty(pB, EP_IntValue) )((void) (0));
109143 if( pA->u.zToken ){
109144 if( pA->op==TK_FUNCTION172 || pA->op==TK_AGG_FUNCTION168 ){
109145 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
109146#ifndef SQLITE_OMIT_WINDOWFUNC
109147 assert( pA->op==pB->op )((void) (0));
109148 if( ExprHasProperty(pA,EP_WinFunc)(((pA)->flags&(0x1000000))!=0)!=ExprHasProperty(pB,EP_WinFunc)(((pB)->flags&(0x1000000))!=0) ){
109149 return 2;
109150 }
109151 if( ExprHasProperty(pA,EP_WinFunc)(((pA)->flags&(0x1000000))!=0) ){
109152 if( sqlite3WindowCompare(pParse, pA->y.pWin, pB->y.pWin, 1)!=0 ){
109153 return 2;
109154 }
109155 }
109156#endif
109157 }else if( pA->op==TK_NULL121 ){
109158 return 0;
109159 }else if( pA->op==TK_COLLATE113 ){
109160 if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
109161 }else
109162 if( pB->u.zToken!=0
109163 && pA->op!=TK_COLUMN167
109164 && pA->op!=TK_AGG_COLUMN169
109165 && strcmp(pA->u.zToken,pB->u.zToken)!=0
109166 ){
109167 return 2;
109168 }
109169 }
109170 if( (pA->flags & (EP_Distinct0x000004|EP_Commuted0x000400))
109171 != (pB->flags & (EP_Distinct0x000004|EP_Commuted0x000400)) ) return 2;
109172 if( ALWAYS((combinedFlags & EP_TokenOnly)==0)((combinedFlags & 0x010000)==0) ){
109173 if( combinedFlags & EP_xIsSelect0x001000 ) return 2;
109174 if( (combinedFlags & EP_FixedCol0x000020)==0
109175 && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
109176 if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
109177 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
109178 if( pA->op!=TK_STRING117
109179 && pA->op!=TK_TRUEFALSE170
109180 && ALWAYS((combinedFlags & EP_Reduced)==0)((combinedFlags & 0x004000)==0)
109181 ){
109182 if( pA->iColumn!=pB->iColumn ) return 2;
109183 if( pA->op2!=pB->op2 && pA->op==TK_TRUTH175 ) return 2;
109184 if( pA->op!=TK_IN49 && pA->iTable!=pB->iTable && pA->iTable!=iTab ){
109185 return 2;
109186 }
109187 }
109188 }
109189 return 0;
109190}
109191
109192/*
109193** Compare two ExprList objects. Return 0 if they are identical, 1
109194** if they are certainly different, or 2 if it is not possible to
109195** determine if they are identical or not.
109196**
109197** If any subelement of pB has Expr.iTable==(-1) then it is allowed
109198** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
109199**
109200** This routine might return non-zero for equivalent ExprLists. The
109201** only consequence will be disabled optimizations. But this routine
109202** must never return 0 if the two ExprList objects are different, or
109203** a malfunction will result.
109204**
109205** Two NULL pointers are considered to be the same. But a NULL pointer
109206** always differs from a non-NULL pointer.
109207*/
109208SQLITE_PRIVATEstatic int sqlite3ExprListCompare(const ExprList *pA, const ExprList *pB, int iTab){
109209 int i;
109210 if( pA==0 && pB==0 ) return 0;
109211 if( pA==0 || pB==0 ) return 1;
109212 if( pA->nExpr!=pB->nExpr ) return 1;
109213 for(i=0; i<pA->nExpr; i++){
109214 int res;
109215 Expr *pExprA = pA->a[i].pExpr;
109216 Expr *pExprB = pB->a[i].pExpr;
109217 if( pA->a[i].fg.sortFlags!=pB->a[i].fg.sortFlags ) return 1;
109218 if( (res = sqlite3ExprCompare(0, pExprA, pExprB, iTab)) ) return res;
109219 }
109220 return 0;
109221}
109222
109223/*
109224** Like sqlite3ExprCompare() except COLLATE operators at the top-level
109225** are ignored.
109226*/
109227SQLITE_PRIVATEstatic int sqlite3ExprCompareSkip(Expr *pA,Expr *pB, int iTab){
109228 return sqlite3ExprCompare(0,
109229 sqlite3ExprSkipCollateAndLikely(pA),
109230 sqlite3ExprSkipCollateAndLikely(pB),
109231 iTab);
109232}
109233
109234/*
109235** Return non-zero if Expr p can only be true if pNN is not NULL.
109236**
109237** Or if seenNot is true, return non-zero if Expr p can only be
109238** non-NULL if pNN is not NULL
109239*/
109240static int exprImpliesNotNull(
109241 const Parse *pParse,/* Parsing context */
109242 const Expr *p, /* The expression to be checked */
109243 const Expr *pNN, /* The expression that is NOT NULL */
109244 int iTab, /* Table being evaluated */
109245 int seenNot /* Return true only if p can be any non-NULL value */
109246){
109247 assert( p )((void) (0));
109248 assert( pNN )((void) (0));
109249 if( sqlite3ExprCompare(pParse, p, pNN, iTab)==0 ){
109250 return pNN->op!=TK_NULL121;
109251 }
109252 switch( p->op ){
109253 case TK_IN49: {
109254 if( seenNot && ExprHasProperty(p, EP_xIsSelect)(((p)->flags&(0x001000))!=0) ) return 0;
109255 assert( ExprUseXSelect(p) || (p->x.pList!=0 && p->x.pList->nExpr>0) )((void) (0));
109256 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
109257 }
109258 case TK_BETWEEN48: {
109259 ExprList *pList;
109260 assert( ExprUseXList(p) )((void) (0));
109261 pList = p->x.pList;
109262 assert( pList!=0 )((void) (0));
109263 assert( pList->nExpr==2 )((void) (0));
109264 if( seenNot ) return 0;
109265 if( exprImpliesNotNull(pParse, pList->a[0].pExpr, pNN, iTab, 1)
109266 || exprImpliesNotNull(pParse, pList->a[1].pExpr, pNN, iTab, 1)
109267 ){
109268 return 1;
109269 }
109270 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
109271 }
109272 case TK_EQ53:
109273 case TK_NE52:
109274 case TK_LT56:
109275 case TK_LE55:
109276 case TK_GT54:
109277 case TK_GE57:
109278 case TK_PLUS106:
109279 case TK_MINUS107:
109280 case TK_BITOR103:
109281 case TK_LSHIFT104:
109282 case TK_RSHIFT105:
109283 case TK_CONCAT111:
109284 seenNot = 1;
109285 /* no break */ deliberate_fall_through
109286 case TK_STAR108:
109287 case TK_REM110:
109288 case TK_BITAND102:
109289 case TK_SLASH109: {
109290 if( exprImpliesNotNull(pParse, p->pRight, pNN, iTab, seenNot) ) return 1;
109291 /* no break */ deliberate_fall_through
109292 }
109293 case TK_SPAN181:
109294 case TK_COLLATE113:
109295 case TK_UPLUS174:
109296 case TK_UMINUS173: {
109297 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
109298 }
109299 case TK_TRUTH175: {
109300 if( seenNot ) return 0;
109301 if( p->op2!=TK_IS45 ) return 0;
109302 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
109303 }
109304 case TK_BITNOT114:
109305 case TK_NOT19: {
109306 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
109307 }
109308 }
109309 return 0;
109310}
109311
109312/*
109313** Return true if we can prove the pE2 will always be true if pE1 is
109314** true. Return false if we cannot complete the proof or if pE2 might
109315** be false. Examples:
109316**
109317** pE1: x==5 pE2: x==5 Result: true
109318** pE1: x>0 pE2: x==5 Result: false
109319** pE1: x=21 pE2: x=21 OR y=43 Result: true
109320** pE1: x!=123 pE2: x IS NOT NULL Result: true
109321** pE1: x!=?1 pE2: x IS NOT NULL Result: true
109322** pE1: x IS NULL pE2: x IS NOT NULL Result: false
109323** pE1: x IS ?2 pE2: x IS NOT NULL Reuslt: false
109324**
109325** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has
109326** Expr.iTable<0 then assume a table number given by iTab.
109327**
109328** If pParse is not NULL, then the values of bound variables in pE1 are
109329** compared against literal values in pE2 and pParse->pVdbe->expmask is
109330** modified to record which bound variables are referenced. If pParse
109331** is NULL, then false will be returned if pE1 contains any bound variables.
109332**
109333** When in doubt, return false. Returning true might give a performance
109334** improvement. Returning false might cause a performance reduction, but
109335** it will always give the correct answer and is hence always safe.
109336*/
109337SQLITE_PRIVATEstatic int sqlite3ExprImpliesExpr(
109338 const Parse *pParse,
109339 const Expr *pE1,
109340 const Expr *pE2,
109341 int iTab
109342){
109343 if( sqlite3ExprCompare(pParse, pE1, pE2, iTab)==0 ){
109344 return 1;
109345 }
109346 if( pE2->op==TK_OR43
109347 && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab)
109348 || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) )
109349 ){
109350 return 1;
109351 }
109352 if( pE2->op==TK_NOTNULL51
109353 && exprImpliesNotNull(pParse, pE1, pE2->pLeft, iTab, 0)
109354 ){
109355 return 1;
109356 }
109357 return 0;
109358}
109359
109360/*
109361** This is the Expr node callback for sqlite3ExprImpliesNonNullRow().
109362** If the expression node requires that the table at pWalker->iCur
109363** have one or more non-NULL column, then set pWalker->eCode to 1 and abort.
109364**
109365** This routine controls an optimization. False positives (setting
109366** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives
109367** (never setting pWalker->eCode) is a harmless missed optimization.
109368*/
109369static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){
109370 testcase( pExpr->op==TK_AGG_COLUMN );
109371 testcase( pExpr->op==TK_AGG_FUNCTION );
109372 if( ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(0x000001))!=0) ) return WRC_Prune1;
109373 switch( pExpr->op ){
109374 case TK_ISNOT171:
109375 case TK_ISNULL50:
109376 case TK_NOTNULL51:
109377 case TK_IS45:
109378 case TK_OR43:
109379 case TK_VECTOR177:
109380 case TK_CASE157:
109381 case TK_IN49:
109382 case TK_FUNCTION172:
109383 case TK_TRUTH175:
109384 testcase( pExpr->op==TK_ISNOT );
109385 testcase( pExpr->op==TK_ISNULL );
109386 testcase( pExpr->op==TK_NOTNULL );
109387 testcase( pExpr->op==TK_IS );
109388 testcase( pExpr->op==TK_OR );
109389 testcase( pExpr->op==TK_VECTOR );
109390 testcase( pExpr->op==TK_CASE );
109391 testcase( pExpr->op==TK_IN );
109392 testcase( pExpr->op==TK_FUNCTION );
109393 testcase( pExpr->op==TK_TRUTH );
109394 return WRC_Prune1;
109395 case TK_COLUMN167:
109396 if( pWalker->u.iCur==pExpr->iTable ){
109397 pWalker->eCode = 1;
109398 return WRC_Abort2;
109399 }
109400 return WRC_Prune1;
109401
109402 case TK_AND44:
109403 if( pWalker->eCode==0 ){
109404 sqlite3WalkExpr(pWalker, pExpr->pLeft);
109405 if( pWalker->eCode ){
109406 pWalker->eCode = 0;
109407 sqlite3WalkExpr(pWalker, pExpr->pRight);
109408 }
109409 }
109410 return WRC_Prune1;
109411
109412 case TK_BETWEEN48:
109413 if( sqlite3WalkExpr(pWalker, pExpr->pLeft)==WRC_Abort2 ){
109414 assert( pWalker->eCode )((void) (0));
109415 return WRC_Abort2;
109416 }
109417 return WRC_Prune1;
109418
109419 /* Virtual tables are allowed to use constraints like x=NULL. So
109420 ** a term of the form x=y does not prove that y is not null if x
109421 ** is the column of a virtual table */
109422 case TK_EQ53:
109423 case TK_NE52:
109424 case TK_LT56:
109425 case TK_LE55:
109426 case TK_GT54:
109427 case TK_GE57: {
109428 Expr *pLeft = pExpr->pLeft;
109429 Expr *pRight = pExpr->pRight;
109430 testcase( pExpr->op==TK_EQ );
109431 testcase( pExpr->op==TK_NE );
109432 testcase( pExpr->op==TK_LT );
109433 testcase( pExpr->op==TK_LE );
109434 testcase( pExpr->op==TK_GT );
109435 testcase( pExpr->op==TK_GE );
109436 /* The y.pTab=0 assignment in wherecode.c always happens after the
109437 ** impliesNotNullRow() test */
109438 assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) )((void) (0));
109439 assert( pRight->op!=TK_COLUMN || ExprUseYTab(pRight) )((void) (0));
109440 if( (pLeft->op==TK_COLUMN167
109441 && pLeft->y.pTab!=0
109442 && IsVirtual(pLeft->y.pTab)((pLeft->y.pTab)->eTabType==1))
109443 || (pRight->op==TK_COLUMN167
109444 && pRight->y.pTab!=0
109445 && IsVirtual(pRight->y.pTab)((pRight->y.pTab)->eTabType==1))
109446 ){
109447 return WRC_Prune1;
109448 }
109449 /* no break */ deliberate_fall_through
109450 }
109451 default:
109452 return WRC_Continue0;
109453 }
109454}
109455
109456/*
109457** Return true (non-zero) if expression p can only be true if at least
109458** one column of table iTab is non-null. In other words, return true
109459** if expression p will always be NULL or false if every column of iTab
109460** is NULL.
109461**
109462** False negatives are acceptable. In other words, it is ok to return
109463** zero even if expression p will never be true of every column of iTab
109464** is NULL. A false negative is merely a missed optimization opportunity.
109465**
109466** False positives are not allowed, however. A false positive may result
109467** in an incorrect answer.
109468**
109469** Terms of p that are marked with EP_OuterON (and hence that come from
109470** the ON or USING clauses of OUTER JOINS) are excluded from the analysis.
109471**
109472** This routine is used to check if a LEFT JOIN can be converted into
109473** an ordinary JOIN. The p argument is the WHERE clause. If the WHERE
109474** clause requires that some column of the right table of the LEFT JOIN
109475** be non-NULL, then the LEFT JOIN can be safely converted into an
109476** ordinary join.
109477*/
109478SQLITE_PRIVATEstatic int sqlite3ExprImpliesNonNullRow(Expr *p, int iTab){
109479 Walker w;
109480 p = sqlite3ExprSkipCollateAndLikely(p);
109481 if( p==0 ) return 0;
109482 if( p->op==TK_NOTNULL51 ){
109483 p = p->pLeft;
109484 }else{
109485 while( p->op==TK_AND44 ){
109486 if( sqlite3ExprImpliesNonNullRow(p->pLeft, iTab) ) return 1;
109487 p = p->pRight;
109488 }
109489 }
109490 w.xExprCallback = impliesNotNullRow;
109491 w.xSelectCallback = 0;
109492 w.xSelectCallback2 = 0;
109493 w.eCode = 0;
109494 w.u.iCur = iTab;
109495 sqlite3WalkExpr(&w, p);
109496 return w.eCode;
109497}
109498
109499/*
109500** An instance of the following structure is used by the tree walker
109501** to determine if an expression can be evaluated by reference to the
109502** index only, without having to do a search for the corresponding
109503** table entry. The IdxCover.pIdx field is the index. IdxCover.iCur
109504** is the cursor for the table.
109505*/
109506struct IdxCover {
109507 Index *pIdx; /* The index to be tested for coverage */
109508 int iCur; /* Cursor number for the table corresponding to the index */
109509};
109510
109511/*
109512** Check to see if there are references to columns in table
109513** pWalker->u.pIdxCover->iCur can be satisfied using the index
109514** pWalker->u.pIdxCover->pIdx.
109515*/
109516static int exprIdxCover(Walker *pWalker, Expr *pExpr){
109517 if( pExpr->op==TK_COLUMN167
109518 && pExpr->iTable==pWalker->u.pIdxCover->iCur
109519 && sqlite3TableColumnToIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
109520 ){
109521 pWalker->eCode = 1;
109522 return WRC_Abort2;
109523 }
109524 return WRC_Continue0;
109525}
109526
109527/*
109528** Determine if an index pIdx on table with cursor iCur contains will
109529** the expression pExpr. Return true if the index does cover the
109530** expression and false if the pExpr expression references table columns
109531** that are not found in the index pIdx.
109532**
109533** An index covering an expression means that the expression can be
109534** evaluated using only the index and without having to lookup the
109535** corresponding table entry.
109536*/
109537SQLITE_PRIVATEstatic int sqlite3ExprCoveredByIndex(
109538 Expr *pExpr, /* The index to be tested */
109539 int iCur, /* The cursor number for the corresponding table */
109540 Index *pIdx /* The index that might be used for coverage */
109541){
109542 Walker w;
109543 struct IdxCover xcov;
109544 memset(&w, 0, sizeof(w));
109545 xcov.iCur = iCur;
109546 xcov.pIdx = pIdx;
109547 w.xExprCallback = exprIdxCover;
109548 w.u.pIdxCover = &xcov;
109549 sqlite3WalkExpr(&w, pExpr);
109550 return !w.eCode;
109551}
109552
109553
109554/* Structure used to pass information throught the Walker in order to
109555** implement sqlite3ReferencesSrcList().
109556*/
109557struct RefSrcList {
109558 sqlite3 *db; /* Database connection used for sqlite3DbRealloc() */
109559 SrcList *pRef; /* Looking for references to these tables */
109560 i64 nExclude; /* Number of tables to exclude from the search */
109561 int *aiExclude; /* Cursor IDs for tables to exclude from the search */
109562};
109563
109564/*
109565** Walker SELECT callbacks for sqlite3ReferencesSrcList().
109566**
109567** When entering a new subquery on the pExpr argument, add all FROM clause
109568** entries for that subquery to the exclude list.
109569**
109570** When leaving the subquery, remove those entries from the exclude list.
109571*/
109572static int selectRefEnter(Walker *pWalker, Select *pSelect){
109573 struct RefSrcList *p = pWalker->u.pRefSrcList;
109574 SrcList *pSrc = pSelect->pSrc;
109575 i64 i, j;
109576 int *piNew;
109577 if( pSrc->nSrc==0 ) return WRC_Continue0;
109578 j = p->nExclude;
109579 p->nExclude += pSrc->nSrc;
109580 piNew = sqlite3DbRealloc(p->db, p->aiExclude, p->nExclude*sizeof(int));
109581 if( piNew==0 ){
109582 p->nExclude = 0;
109583 return WRC_Abort2;
109584 }else{
109585 p->aiExclude = piNew;
109586 }
109587 for(i=0; i<pSrc->nSrc; i++, j++){
109588 p->aiExclude[j] = pSrc->a[i].iCursor;
109589 }
109590 return WRC_Continue0;
109591}
109592static void selectRefLeave(Walker *pWalker, Select *pSelect){
109593 struct RefSrcList *p = pWalker->u.pRefSrcList;
109594 SrcList *pSrc = pSelect->pSrc;
109595 if( p->nExclude ){
109596 assert( p->nExclude>=pSrc->nSrc )((void) (0));
109597 p->nExclude -= pSrc->nSrc;
109598 }
109599}
109600
109601/* This is the Walker EXPR callback for sqlite3ReferencesSrcList().
109602**
109603** Set the 0x01 bit of pWalker->eCode if there is a reference to any
109604** of the tables shown in RefSrcList.pRef.
109605**
109606** Set the 0x02 bit of pWalker->eCode if there is a reference to a
109607** table is in neither RefSrcList.pRef nor RefSrcList.aiExclude.
109608*/
109609static int exprRefToSrcList(Walker *pWalker, Expr *pExpr){
109610 if( pExpr->op==TK_COLUMN167
109611 || pExpr->op==TK_AGG_COLUMN169
109612 ){
109613 int i;
109614 struct RefSrcList *p = pWalker->u.pRefSrcList;
109615 SrcList *pSrc = p->pRef;
109616 int nSrc = pSrc ? pSrc->nSrc : 0;
109617 for(i=0; i<nSrc; i++){
109618 if( pExpr->iTable==pSrc->a[i].iCursor ){
109619 pWalker->eCode |= 1;
109620 return WRC_Continue0;
109621 }
109622 }
109623 for(i=0; i<p->nExclude && p->aiExclude[i]!=pExpr->iTable; i++){}
109624 if( i>=p->nExclude ){
109625 pWalker->eCode |= 2;
109626 }
109627 }
109628 return WRC_Continue0;
109629}
109630
109631/*
109632** Check to see if pExpr references any tables in pSrcList.
109633** Possible return values:
109634**
109635** 1 pExpr does references a table in pSrcList.
109636**
109637** 0 pExpr references some table that is not defined in either
109638** pSrcList or in subqueries of pExpr itself.
109639**
109640** -1 pExpr only references no tables at all, or it only
109641** references tables defined in subqueries of pExpr itself.
109642**
109643** As currently used, pExpr is always an aggregate function call. That
109644** fact is exploited for efficiency.
109645*/
109646SQLITE_PRIVATEstatic int sqlite3ReferencesSrcList(Parse *pParse, Expr *pExpr, SrcList *pSrcList){
109647 Walker w;
109648 struct RefSrcList x;
109649 memset(&w, 0, sizeof(w));
109650 memset(&x, 0, sizeof(x));
109651 w.xExprCallback = exprRefToSrcList;
109652 w.xSelectCallback = selectRefEnter;
109653 w.xSelectCallback2 = selectRefLeave;
109654 w.u.pRefSrcList = &x;
109655 x.db = pParse->db;
109656 x.pRef = pSrcList;
109657 assert( pExpr->op==TK_AGG_FUNCTION )((void) (0));
109658 assert( ExprUseXList(pExpr) )((void) (0));
109659 sqlite3WalkExprList(&w, pExpr->x.pList);
109660#ifndef SQLITE_OMIT_WINDOWFUNC
109661 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
109662 sqlite3WalkExpr(&w, pExpr->y.pWin->pFilter);
109663 }
109664#endif
109665 sqlite3DbFree(pParse->db, x.aiExclude);
109666 if( w.eCode & 0x01 ){
109667 return 1;
109668 }else if( w.eCode ){
109669 return 0;
109670 }else{
109671 return -1;
109672 }
109673}
109674
109675/*
109676** This is a Walker expression node callback.
109677**
109678** For Expr nodes that contain pAggInfo pointers, make sure the AggInfo
109679** object that is referenced does not refer directly to the Expr. If
109680** it does, make a copy. This is done because the pExpr argument is
109681** subject to change.
109682**
109683** The copy is stored on pParse->pConstExpr with a register number of 0.
109684** This will cause the expression to be deleted automatically when the
109685** Parse object is destroyed, but the zero register number means that it
109686** will not generate any code in the preamble.
109687*/
109688static int agginfoPersistExprCb(Walker *pWalker, Expr *pExpr){
109689 if( ALWAYS(!ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced))(!(((pExpr)->flags&(0x010000|0x004000))!=0))
109690 && pExpr->pAggInfo!=0
109691 ){
109692 AggInfo *pAggInfo = pExpr->pAggInfo;
109693 int iAgg = pExpr->iAgg;
109694 Parse *pParse = pWalker->pParse;
109695 sqlite3 *db = pParse->db;
109696 assert( pExpr->op==TK_AGG_COLUMN || pExpr->op==TK_AGG_FUNCTION )((void) (0));
109697 if( pExpr->op==TK_AGG_COLUMN169 ){
109698 assert( iAgg>=0 && iAgg<pAggInfo->nColumn )((void) (0));
109699 if( pAggInfo->aCol[iAgg].pCExpr==pExpr ){
109700 pExpr = sqlite3ExprDup(db, pExpr, 0);
109701 if( pExpr ){
109702 pAggInfo->aCol[iAgg].pCExpr = pExpr;
109703 sqlite3ExprDeferredDelete(pParse, pExpr);
109704 }
109705 }
109706 }else{
109707 assert( iAgg>=0 && iAgg<pAggInfo->nFunc )((void) (0));
109708 if( pAggInfo->aFunc[iAgg].pFExpr==pExpr ){
109709 pExpr = sqlite3ExprDup(db, pExpr, 0);
109710 if( pExpr ){
109711 pAggInfo->aFunc[iAgg].pFExpr = pExpr;
109712 sqlite3ExprDeferredDelete(pParse, pExpr);
109713 }
109714 }
109715 }
109716 }
109717 return WRC_Continue0;
109718}
109719
109720/*
109721** Initialize a Walker object so that will persist AggInfo entries referenced
109722** by the tree that is walked.
109723*/
109724SQLITE_PRIVATEstatic void sqlite3AggInfoPersistWalkerInit(Walker *pWalker, Parse *pParse){
109725 memset(pWalker, 0, sizeof(*pWalker));
109726 pWalker->pParse = pParse;
109727 pWalker->xExprCallback = agginfoPersistExprCb;
109728 pWalker->xSelectCallback = sqlite3SelectWalkNoop;
109729}
109730
109731/*
109732** Add a new element to the pAggInfo->aCol[] array. Return the index of
109733** the new element. Return a negative number if malloc fails.
109734*/
109735static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
109736 int i;
109737 pInfo->aCol = sqlite3ArrayAllocate(
109738 db,
109739 pInfo->aCol,
109740 sizeof(pInfo->aCol[0]),
109741 &pInfo->nColumn,
109742 &i
109743 );
109744 return i;
109745}
109746
109747/*
109748** Add a new element to the pAggInfo->aFunc[] array. Return the index of
109749** the new element. Return a negative number if malloc fails.
109750*/
109751static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
109752 int i;
109753 pInfo->aFunc = sqlite3ArrayAllocate(
109754 db,
109755 pInfo->aFunc,
109756 sizeof(pInfo->aFunc[0]),
109757 &pInfo->nFunc,
109758 &i
109759 );
109760 return i;
109761}
109762
109763/*
109764** This is the xExprCallback for a tree walker. It is used to
109765** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
109766** for additional information.
109767*/
109768static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
109769 int i;
109770 NameContext *pNC = pWalker->u.pNC;
109771 Parse *pParse = pNC->pParse;
109772 SrcList *pSrcList = pNC->pSrcList;
109773 AggInfo *pAggInfo = pNC->uNC.pAggInfo;
109774
109775 assert( pNC->ncFlags & NC_UAggInfo )((void) (0));
109776 switch( pExpr->op ){
109777 case TK_AGG_COLUMN169:
109778 case TK_COLUMN167: {
109779 testcase( pExpr->op==TK_AGG_COLUMN );
109780 testcase( pExpr->op==TK_COLUMN );
109781 /* Check to see if the column is in one of the tables in the FROM
109782 ** clause of the aggregate query */
109783 if( ALWAYS(pSrcList!=0)(pSrcList!=0) ){
109784 SrcItem *pItem = pSrcList->a;
109785 for(i=0; i<pSrcList->nSrc; i++, pItem++){
109786 struct AggInfo_col *pCol;
109787 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) )((void) (0));
109788 if( pExpr->iTable==pItem->iCursor ){
109789 /* If we reach this point, it means that pExpr refers to a table
109790 ** that is in the FROM clause of the aggregate query.
109791 **
109792 ** Make an entry for the column in pAggInfo->aCol[] if there
109793 ** is not an entry there already.
109794 */
109795 int k;
109796 pCol = pAggInfo->aCol;
109797 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
109798 if( pCol->iTable==pExpr->iTable &&
109799 pCol->iColumn==pExpr->iColumn ){
109800 break;
109801 }
109802 }
109803 if( (k>=pAggInfo->nColumn)
109804 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
109805 ){
109806 pCol = &pAggInfo->aCol[k];
109807 assert( ExprUseYTab(pExpr) )((void) (0));
109808 pCol->pTab = pExpr->y.pTab;
109809 pCol->iTable = pExpr->iTable;
109810 pCol->iColumn = pExpr->iColumn;
109811 pCol->iMem = ++pParse->nMem;
109812 pCol->iSorterColumn = -1;
109813 pCol->pCExpr = pExpr;
109814 if( pAggInfo->pGroupBy ){
109815 int j, n;
109816 ExprList *pGB = pAggInfo->pGroupBy;
109817 struct ExprList_item *pTerm = pGB->a;
109818 n = pGB->nExpr;
109819 for(j=0; j<n; j++, pTerm++){
109820 Expr *pE = pTerm->pExpr;
109821 if( pE->op==TK_COLUMN167 && pE->iTable==pExpr->iTable &&
109822 pE->iColumn==pExpr->iColumn ){
109823 pCol->iSorterColumn = j;
109824 break;
109825 }
109826 }
109827 }
109828 if( pCol->iSorterColumn<0 ){
109829 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
109830 }
109831 }
109832 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
109833 ** because it was there before or because we just created it).
109834 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
109835 ** pAggInfo->aCol[] entry.
109836 */
109837 ExprSetVVAProperty(pExpr, EP_NoReduce);
109838 pExpr->pAggInfo = pAggInfo;
109839 pExpr->op = TK_AGG_COLUMN169;
109840 pExpr->iAgg = (i16)k;
109841 break;
109842 } /* endif pExpr->iTable==pItem->iCursor */
109843 } /* end loop over pSrcList */
109844 }
109845 return WRC_Prune1;
109846 }
109847 case TK_AGG_FUNCTION168: {
109848 if( (pNC->ncFlags & NC_InAggFunc0x020000)==0
109849 && pWalker->walkerDepth==pExpr->op2
109850 ){
109851 /* Check to see if pExpr is a duplicate of another aggregate
109852 ** function that is already in the pAggInfo structure
109853 */
109854 struct AggInfo_func *pItem = pAggInfo->aFunc;
109855 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
109856 if( pItem->pFExpr==pExpr ) break;
109857 if( sqlite3ExprCompare(0, pItem->pFExpr, pExpr, -1)==0 ){
109858 break;
109859 }
109860 }
109861 if( i>=pAggInfo->nFunc ){
109862 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
109863 */
109864 u8 enc = ENC(pParse->db)((pParse->db)->enc);
109865 i = addAggInfoFunc(pParse->db, pAggInfo);
109866 if( i>=0 ){
109867 assert( !ExprHasProperty(pExpr, EP_xIsSelect) )((void) (0));
109868 pItem = &pAggInfo->aFunc[i];
109869 pItem->pFExpr = pExpr;
109870 pItem->iMem = ++pParse->nMem;
109871 assert( ExprUseUToken(pExpr) )((void) (0));
109872 pItem->pFunc = sqlite3FindFunction(pParse->db,
109873 pExpr->u.zToken,
109874 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
109875 if( pExpr->flags & EP_Distinct0x000004 ){
109876 pItem->iDistinct = pParse->nTab++;
109877 }else{
109878 pItem->iDistinct = -1;
109879 }
109880 }
109881 }
109882 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
109883 */
109884 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) )((void) (0));
109885 ExprSetVVAProperty(pExpr, EP_NoReduce);
109886 pExpr->iAgg = (i16)i;
109887 pExpr->pAggInfo = pAggInfo;
109888 return WRC_Prune1;
109889 }else{
109890 return WRC_Continue0;
109891 }
109892 }
109893 }
109894 return WRC_Continue0;
109895}
109896
109897/*
109898** Analyze the pExpr expression looking for aggregate functions and
109899** for variables that need to be added to AggInfo object that pNC->pAggInfo
109900** points to. Additional entries are made on the AggInfo object as
109901** necessary.
109902**
109903** This routine should only be called after the expression has been
109904** analyzed by sqlite3ResolveExprNames().
109905*/
109906SQLITE_PRIVATEstatic void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
109907 Walker w;
109908 w.xExprCallback = analyzeAggregate;
109909 w.xSelectCallback = sqlite3WalkerDepthIncrease;
109910 w.xSelectCallback2 = sqlite3WalkerDepthDecrease;
109911 w.walkerDepth = 0;
109912 w.u.pNC = pNC;
109913 w.pParse = 0;
109914 assert( pNC->pSrcList!=0 )((void) (0));
109915 sqlite3WalkExpr(&w, pExpr);
109916}
109917
109918/*
109919** Call sqlite3ExprAnalyzeAggregates() for every expression in an
109920** expression list. Return the number of errors.
109921**
109922** If an error is found, the analysis is cut short.
109923*/
109924SQLITE_PRIVATEstatic void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
109925 struct ExprList_item *pItem;
109926 int i;
109927 if( pList ){
109928 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
109929 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
109930 }
109931 }
109932}
109933
109934/*
109935** Allocate a single new register for use to hold some intermediate result.
109936*/
109937SQLITE_PRIVATEstatic int sqlite3GetTempReg(Parse *pParse){
109938 if( pParse->nTempReg==0 ){
109939 return ++pParse->nMem;
109940 }
109941 return pParse->aTempReg[--pParse->nTempReg];
109942}
109943
109944/*
109945** Deallocate a register, making available for reuse for some other
109946** purpose.
109947*/
109948SQLITE_PRIVATEstatic void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
109949 if( iReg ){
109950 sqlite3VdbeReleaseRegisters(pParse, iReg, 1, 0, 0);
109951 if( pParse->nTempReg<ArraySize(pParse->aTempReg)((int)(sizeof(pParse->aTempReg)/sizeof(pParse->aTempReg
[0])))
){
109952 pParse->aTempReg[pParse->nTempReg++] = iReg;
109953 }
109954 }
109955}
109956
109957/*
109958** Allocate or deallocate a block of nReg consecutive registers.
109959*/
109960SQLITE_PRIVATEstatic int sqlite3GetTempRange(Parse *pParse, int nReg){
109961 int i, n;
109962 if( nReg==1 ) return sqlite3GetTempReg(pParse);
109963 i = pParse->iRangeReg;
109964 n = pParse->nRangeReg;
109965 if( nReg<=n ){
109966 pParse->iRangeReg += nReg;
109967 pParse->nRangeReg -= nReg;
109968 }else{
109969 i = pParse->nMem+1;
109970 pParse->nMem += nReg;
109971 }
109972 return i;
109973}
109974SQLITE_PRIVATEstatic void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
109975 if( nReg==1 ){
109976 sqlite3ReleaseTempReg(pParse, iReg);
109977 return;
109978 }
109979 sqlite3VdbeReleaseRegisters(pParse, iReg, nReg, 0, 0);
109980 if( nReg>pParse->nRangeReg ){
109981 pParse->nRangeReg = nReg;
109982 pParse->iRangeReg = iReg;
109983 }
109984}
109985
109986/*
109987** Mark all temporary registers as being unavailable for reuse.
109988**
109989** Always invoke this procedure after coding a subroutine or co-routine
109990** that might be invoked from other parts of the code, to ensure that
109991** the sub/co-routine does not use registers in common with the code that
109992** invokes the sub/co-routine.
109993*/
109994SQLITE_PRIVATEstatic void sqlite3ClearTempRegCache(Parse *pParse){
109995 pParse->nTempReg = 0;
109996 pParse->nRangeReg = 0;
109997}
109998
109999/*
110000** Validate that no temporary register falls within the range of
110001** iFirst..iLast, inclusive. This routine is only call from within assert()
110002** statements.
110003*/
110004#ifdef SQLITE_DEBUG
110005SQLITE_PRIVATEstatic int sqlite3NoTempsInRange(Parse *pParse, int iFirst, int iLast){
110006 int i;
110007 if( pParse->nRangeReg>0
110008 && pParse->iRangeReg+pParse->nRangeReg > iFirst
110009 && pParse->iRangeReg <= iLast
110010 ){
110011 return 0;
110012 }
110013 for(i=0; i<pParse->nTempReg; i++){
110014 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
110015 return 0;
110016 }
110017 }
110018 return 1;
110019}
110020#endif /* SQLITE_DEBUG */
110021
110022/************** End of expr.c ************************************************/
110023/************** Begin file alter.c *******************************************/
110024/*
110025** 2005 February 15
110026**
110027** The author disclaims copyright to this source code. In place of
110028** a legal notice, here is a blessing:
110029**
110030** May you do good and not evil.
110031** May you find forgiveness for yourself and forgive others.
110032** May you share freely, never taking more than you give.
110033**
110034*************************************************************************
110035** This file contains C code routines that used to generate VDBE code
110036** that implements the ALTER TABLE command.
110037*/
110038/* #include "sqliteInt.h" */
110039
110040/*
110041** The code in this file only exists if we are not omitting the
110042** ALTER TABLE logic from the build.
110043*/
110044#ifndef SQLITE_OMIT_ALTERTABLE
110045
110046/*
110047** Parameter zName is the name of a table that is about to be altered
110048** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
110049** If the table is a system table, this function leaves an error message
110050** in pParse->zErr (system tables may not be altered) and returns non-zero.
110051**
110052** Or, if zName is not a system table, zero is returned.
110053*/
110054static int isAlterableTable(Parse *pParse, Table *pTab){
110055 if( 0==sqlite3StrNICmpsqlite3_strnicmp(pTab->zName, "sqlite_", 7)
110056#ifndef SQLITE_OMIT_VIRTUALTABLE
110057 || (pTab->tabFlags & TF_Eponymous0x00008000)!=0
110058 || ( (pTab->tabFlags & TF_Shadow0x00001000)!=0
110059 && sqlite3ReadOnlyShadowTables(pParse->db)
110060 )
110061#endif
110062 ){
110063 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
110064 return 1;
110065 }
110066 return 0;
110067}
110068
110069/*
110070** Generate code to verify that the schemas of database zDb and, if
110071** bTemp is not true, database "temp", can still be parsed. This is
110072** called at the end of the generation of an ALTER TABLE ... RENAME ...
110073** statement to ensure that the operation has not rendered any schema
110074** objects unusable.
110075*/
110076static void renameTestSchema(
110077 Parse *pParse, /* Parse context */
110078 const char *zDb, /* Name of db to verify schema of */
110079 int bTemp, /* True if this is the temp db */
110080 const char *zWhen, /* "when" part of error message */
110081 int bNoDQS /* Do not allow DQS in the schema */
110082){
110083 pParse->colNamesSet = 1;
110084 sqlite3NestedParse(pParse,
110085 "SELECT 1 "
110086 "FROM \"%w\"." LEGACY_SCHEMA_TABLE"sqlite_master" " "
110087 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
110088 " AND sql NOT LIKE 'create virtual%%'"
110089 " AND sqlite_rename_test(%Q, sql, type, name, %d, %Q, %d)=NULL ",
110090 zDb,
110091 zDb, bTemp, zWhen, bNoDQS
110092 );
110093
110094 if( bTemp==0 ){
110095 sqlite3NestedParse(pParse,
110096 "SELECT 1 "
110097 "FROM temp." LEGACY_SCHEMA_TABLE"sqlite_master" " "
110098 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
110099 " AND sql NOT LIKE 'create virtual%%'"
110100 " AND sqlite_rename_test(%Q, sql, type, name, 1, %Q, %d)=NULL ",
110101 zDb, zWhen, bNoDQS
110102 );
110103 }
110104}
110105
110106/*
110107** Generate VM code to replace any double-quoted strings (but not double-quoted
110108** identifiers) within the "sql" column of the sqlite_schema table in
110109** database zDb with their single-quoted equivalents. If argument bTemp is
110110** not true, similarly update all SQL statements in the sqlite_schema table
110111** of the temp db.
110112*/
110113static void renameFixQuotes(Parse *pParse, const char *zDb, int bTemp){
110114 sqlite3NestedParse(pParse,
110115 "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE"sqlite_master"
110116 " SET sql = sqlite_rename_quotefix(%Q, sql)"
110117 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
110118 " AND sql NOT LIKE 'create virtual%%'" , zDb, zDb
110119 );
110120 if( bTemp==0 ){
110121 sqlite3NestedParse(pParse,
110122 "UPDATE temp." LEGACY_SCHEMA_TABLE"sqlite_master"
110123 " SET sql = sqlite_rename_quotefix('temp', sql)"
110124 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
110125 " AND sql NOT LIKE 'create virtual%%'"
110126 );
110127 }
110128}
110129
110130/*
110131** Generate code to reload the schema for database iDb. And, if iDb!=1, for
110132** the temp database as well.
110133*/
110134static void renameReloadSchema(Parse *pParse, int iDb, u16 p5){
110135 Vdbe *v = pParse->pVdbe;
110136 if( v ){
110137 sqlite3ChangeCookie(pParse, iDb);
110138 sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iDb, 0, p5);
110139 if( iDb!=1 ) sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, 1, 0, p5);
110140 }
110141}
110142
110143/*
110144** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
110145** command.
110146*/
110147SQLITE_PRIVATEstatic void sqlite3AlterRenameTable(
110148 Parse *pParse, /* Parser context. */
110149 SrcList *pSrc, /* The table to rename. */
110150 Token *pName /* The new table name. */
110151){
110152 int iDb; /* Database that contains the table */
110153 char *zDb; /* Name of database iDb */
110154 Table *pTab; /* Table being renamed */
110155 char *zName = 0; /* NULL-terminated version of pName */
110156 sqlite3 *db = pParse->db; /* Database connection */
110157 int nTabName; /* Number of UTF-8 characters in zTabName */
110158 const char *zTabName; /* Original name of the table */
110159 Vdbe *v;
110160 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
110161
110162 if( NEVER(db->mallocFailed)(db->mallocFailed) ) goto exit_rename_table;
110163 assert( pSrc->nSrc==1 )((void) (0));
110164 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) )((void) (0));
110165
110166 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
110167 if( !pTab ) goto exit_rename_table;
110168 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
110169 zDb = db->aDb[iDb].zDbSName;
110170
110171 /* Get a NULL terminated version of the new table name. */
110172 zName = sqlite3NameFromToken(db, pName);
110173 if( !zName ) goto exit_rename_table;
110174
110175 /* Check that a table or index named 'zName' does not already exist
110176 ** in database iDb. If so, this is an error.
110177 */
110178 if( sqlite3FindTable(db, zName, zDb)
110179 || sqlite3FindIndex(db, zName, zDb)
110180 || sqlite3IsShadowTableOf(db, pTab, zName)
110181 ){
110182 sqlite3ErrorMsg(pParse,
110183 "there is already another table or index with this name: %s", zName);
110184 goto exit_rename_table;
110185 }
110186
110187 /* Make sure it is not a system table being altered, or a reserved name
110188 ** that the table is being renamed to.
110189 */
110190 if( SQLITE_OK0!=isAlterableTable(pParse, pTab) ){
110191 goto exit_rename_table;
110192 }
110193 if( SQLITE_OK0!=sqlite3CheckObjectName(pParse,zName,"table",zName) ){
110194 goto exit_rename_table;
110195 }
110196
110197#ifndef SQLITE_OMIT_VIEW
110198 if( IsView(pTab)((pTab)->eTabType==2) ){
110199 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
110200 goto exit_rename_table;
110201 }
110202#endif
110203
110204#ifndef SQLITE_OMIT_AUTHORIZATION
110205 /* Invoke the authorization callback. */
110206 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE26, zDb, pTab->zName, 0) ){
110207 goto exit_rename_table;
110208 }
110209#endif
110210
110211#ifndef SQLITE_OMIT_VIRTUALTABLE
110212 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
110213 goto exit_rename_table;
110214 }
110215 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
110216 pVTab = sqlite3GetVTable(db, pTab);
110217 if( pVTab->pVtab->pModule->xRename==0 ){
110218 pVTab = 0;
110219 }
110220 }
110221#endif
110222
110223 /* Begin a transaction for database iDb. Then modify the schema cookie
110224 ** (since the ALTER TABLE modifies the schema). Call sqlite3MayAbort(),
110225 ** as the scalar functions (e.g. sqlite_rename_table()) invoked by the
110226 ** nested SQL may raise an exception. */
110227 v = sqlite3GetVdbe(pParse);
110228 if( v==0 ){
110229 goto exit_rename_table;
110230 }
110231 sqlite3MayAbort(pParse);
110232
110233 /* figure out how many UTF-8 characters are in zName */
110234 zTabName = pTab->zName;
110235 nTabName = sqlite3Utf8CharLen(zTabName, -1);
110236
110237 /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in
110238 ** the schema to use the new table name. */
110239 sqlite3NestedParse(pParse,
110240 "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE"sqlite_master" " SET "
110241 "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, %d) "
110242 "WHERE (type!='index' OR tbl_name=%Q COLLATE nocase)"
110243 "AND name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
110244 , zDb, zDb, zTabName, zName, (iDb==1), zTabName
110245 );
110246
110247 /* Update the tbl_name and name columns of the sqlite_schema table
110248 ** as required. */
110249 sqlite3NestedParse(pParse,
110250 "UPDATE %Q." LEGACY_SCHEMA_TABLE"sqlite_master" " SET "
110251 "tbl_name = %Q, "
110252 "name = CASE "
110253 "WHEN type='table' THEN %Q "
110254 "WHEN name LIKE 'sqliteX_autoindex%%' ESCAPE 'X' "
110255 " AND type='index' THEN "
110256 "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
110257 "ELSE name END "
110258 "WHERE tbl_name=%Q COLLATE nocase AND "
110259 "(type='table' OR type='index' OR type='trigger');",
110260 zDb,
110261 zName, zName, zName,
110262 nTabName, zTabName
110263 );
110264
110265#ifndef SQLITE_OMIT_AUTOINCREMENT
110266 /* If the sqlite_sequence table exists in this database, then update
110267 ** it with the new table name.
110268 */
110269 if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
110270 sqlite3NestedParse(pParse,
110271 "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
110272 zDb, zName, pTab->zName);
110273 }
110274#endif
110275
110276 /* If the table being renamed is not itself part of the temp database,
110277 ** edit view and trigger definitions within the temp database
110278 ** as required. */
110279 if( iDb!=1 ){
110280 sqlite3NestedParse(pParse,
110281 "UPDATE sqlite_temp_schema SET "
110282 "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, 1), "
110283 "tbl_name = "
110284 "CASE WHEN tbl_name=%Q COLLATE nocase AND "
110285 " sqlite_rename_test(%Q, sql, type, name, 1, 'after rename', 0) "
110286 "THEN %Q ELSE tbl_name END "
110287 "WHERE type IN ('view', 'trigger')"
110288 , zDb, zTabName, zName, zTabName, zDb, zName);
110289 }
110290
110291 /* If this is a virtual table, invoke the xRename() function if
110292 ** one is defined. The xRename() callback will modify the names
110293 ** of any resources used by the v-table implementation (including other
110294 ** SQLite tables) that are identified by the name of the virtual table.
110295 */
110296#ifndef SQLITE_OMIT_VIRTUALTABLE
110297 if( pVTab ){
110298 int i = ++pParse->nMem;
110299 sqlite3VdbeLoadString(v, i, zName);
110300 sqlite3VdbeAddOp4(v, OP_VRename176, i, 0, 0,(const char*)pVTab, P4_VTAB(-11));
110301 }
110302#endif
110303
110304 renameReloadSchema(pParse, iDb, INITFLAG_AlterRename0x0001);
110305 renameTestSchema(pParse, zDb, iDb==1, "after rename", 0);
110306
110307exit_rename_table:
110308 sqlite3SrcListDelete(db, pSrc);
110309 sqlite3DbFree(db, zName);
110310}
110311
110312/*
110313** Write code that will raise an error if the table described by
110314** zDb and zTab is not empty.
110315*/
110316static void sqlite3ErrorIfNotEmpty(
110317 Parse *pParse, /* Parsing context */
110318 const char *zDb, /* Schema holding the table */
110319 const char *zTab, /* Table to check for empty */
110320 const char *zErr /* Error message text */
110321){
110322 sqlite3NestedParse(pParse,
110323 "SELECT raise(ABORT,%Q) FROM \"%w\".\"%w\"",
110324 zErr, zDb, zTab
110325 );
110326}
110327
110328/*
110329** This function is called after an "ALTER TABLE ... ADD" statement
110330** has been parsed. Argument pColDef contains the text of the new
110331** column definition.
110332**
110333** The Table structure pParse->pNewTable was extended to include
110334** the new column during parsing.
110335*/
110336SQLITE_PRIVATEstatic void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
110337 Table *pNew; /* Copy of pParse->pNewTable */
110338 Table *pTab; /* Table being altered */
110339 int iDb; /* Database number */
110340 const char *zDb; /* Database name */
110341 const char *zTab; /* Table name */
110342 char *zCol; /* Null-terminated column definition */
110343 Column *pCol; /* The new column */
110344 Expr *pDflt; /* Default value for the new column */
110345 sqlite3 *db; /* The database connection; */
110346 Vdbe *v; /* The prepared statement under construction */
110347 int r1; /* Temporary registers */
110348
110349 db = pParse->db;
110350 assert( db->pParse==pParse )((void) (0));
110351 if( pParse->nErr ) return;
110352 assert( db->mallocFailed==0 )((void) (0));
110353 pNew = pParse->pNewTable;
110354 assert( pNew )((void) (0));
110355
110356 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
110357 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
110358 zDb = db->aDb[iDb].zDbSName;
110359 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
110360 pCol = &pNew->aCol[pNew->nCol-1];
110361 pDflt = sqlite3ColumnExpr(pNew, pCol);
110362 pTab = sqlite3FindTable(db, zTab, zDb);
110363 assert( pTab )((void) (0));
110364
110365#ifndef SQLITE_OMIT_AUTHORIZATION
110366 /* Invoke the authorization callback. */
110367 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE26, zDb, pTab->zName, 0) ){
110368 return;
110369 }
110370#endif
110371
110372
110373 /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
110374 ** If there is a NOT NULL constraint, then the default value for the
110375 ** column must not be NULL.
110376 */
110377 if( pCol->colFlags & COLFLAG_PRIMKEY0x0001 ){
110378 sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
110379 return;
110380 }
110381 if( pNew->pIndex ){
110382 sqlite3ErrorMsg(pParse,
110383 "Cannot add a UNIQUE column");
110384 return;
110385 }
110386 if( (pCol->colFlags & COLFLAG_GENERATED0x0060)==0 ){
110387 /* If the default value for the new column was specified with a
110388 ** literal NULL, then set pDflt to 0. This simplifies checking
110389 ** for an SQL NULL default below.
110390 */
110391 assert( pDflt==0 || pDflt->op==TK_SPAN )((void) (0));
110392 if( pDflt && pDflt->pLeft->op==TK_NULL121 ){
110393 pDflt = 0;
110394 }
110395 assert( IsOrdinaryTable(pNew) )((void) (0));
110396 if( (db->flags&SQLITE_ForeignKeys0x00004000) && pNew->u.tab.pFKey && pDflt ){
110397 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
110398 "Cannot add a REFERENCES column with non-NULL default value");
110399 }
110400 if( pCol->notNull && !pDflt ){
110401 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
110402 "Cannot add a NOT NULL column with default value NULL");
110403 }
110404
110405
110406 /* Ensure the default expression is something that sqlite3ValueFromExpr()
110407 ** can handle (i.e. not CURRENT_TIME etc.)
110408 */
110409 if( pDflt ){
110410 sqlite3_value *pVal = 0;
110411 int rc;
110412 rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF81, SQLITE_AFF_BLOB0x41, &pVal);
110413 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM )((void) (0));
110414 if( rc!=SQLITE_OK0 ){
110415 assert( db->mallocFailed == 1 )((void) (0));
110416 return;
110417 }
110418 if( !pVal ){
110419 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
110420 "Cannot add a column with non-constant default");
110421 }
110422 sqlite3ValueFree(pVal);
110423 }
110424 }else if( pCol->colFlags & COLFLAG_STORED0x0040 ){
110425 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, "cannot add a STORED column");
110426 }
110427
110428
110429 /* Modify the CREATE TABLE statement. */
110430 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
110431 if( zCol ){
110432 char *zEnd = &zCol[pColDef->n-1];
110433 while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)(sqlite3CtypeMap[(unsigned char)(*zEnd)]&0x01)) ){
110434 *zEnd-- = '\0';
110435 }
110436 /* substr() operations on characters, but addColOffset is in bytes. So we
110437 ** have to use printf() to translate between these units: */
110438 assert( IsOrdinaryTable(pTab) )((void) (0));
110439 assert( IsOrdinaryTable(pNew) )((void) (0));
110440 sqlite3NestedParse(pParse,
110441 "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE"sqlite_master" " SET "
110442 "sql = printf('%%.%ds, ',sql) || %Q"
110443 " || substr(sql,1+length(printf('%%.%ds',sql))) "
110444 "WHERE type = 'table' AND name = %Q",
110445 zDb, pNew->u.tab.addColOffset, zCol, pNew->u.tab.addColOffset,
110446 zTab
110447 );
110448 sqlite3DbFree(db, zCol);
110449 }
110450
110451 v = sqlite3GetVdbe(pParse);
110452 if( v ){
110453 /* Make sure the schema version is at least 3. But do not upgrade
110454 ** from less than 3 to 4, as that will corrupt any preexisting DESC
110455 ** index.
110456 */
110457 r1 = sqlite3GetTempReg(pParse);
110458 sqlite3VdbeAddOp3(v, OP_ReadCookie99, iDb, r1, BTREE_FILE_FORMAT2);
110459 sqlite3VdbeUsesBtree(v, iDb);
110460 sqlite3VdbeAddOp2(v, OP_AddImm86, r1, -2);
110461 sqlite3VdbeAddOp2(v, OP_IfPos49, r1, sqlite3VdbeCurrentAddr(v)+2);
110462 VdbeCoverage(v);
110463 sqlite3VdbeAddOp3(v, OP_SetCookie100, iDb, BTREE_FILE_FORMAT2, 3);
110464 sqlite3ReleaseTempReg(pParse, r1);
110465
110466 /* Reload the table definition */
110467 renameReloadSchema(pParse, iDb, INITFLAG_AlterAdd0x0003);
110468
110469 /* Verify that constraints are still satisfied */
110470 if( pNew->pCheck!=0
110471 || (pCol->notNull && (pCol->colFlags & COLFLAG_GENERATED0x0060)!=0)
110472 ){
110473 sqlite3NestedParse(pParse,
110474 "SELECT CASE WHEN quick_check GLOB 'CHECK*'"
110475 " THEN raise(ABORT,'CHECK constraint failed')"
110476 " ELSE raise(ABORT,'NOT NULL constraint failed')"
110477 " END"
110478 " FROM pragma_quick_check(%Q,%Q)"
110479 " WHERE quick_check GLOB 'CHECK*' OR quick_check GLOB 'NULL*'",
110480 zTab, zDb
110481 );
110482 }
110483 }
110484}
110485
110486/*
110487** This function is called by the parser after the table-name in
110488** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
110489** pSrc is the full-name of the table being altered.
110490**
110491** This routine makes a (partial) copy of the Table structure
110492** for the table being altered and sets Parse.pNewTable to point
110493** to it. Routines called by the parser as the column definition
110494** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
110495** the copy. The copy of the Table structure is deleted by tokenize.c
110496** after parsing is finished.
110497**
110498** Routine sqlite3AlterFinishAddColumn() will be called to complete
110499** coding the "ALTER TABLE ... ADD" statement.
110500*/
110501SQLITE_PRIVATEstatic void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
110502 Table *pNew;
110503 Table *pTab;
110504 int iDb;
110505 int i;
110506 int nAlloc;
110507 sqlite3 *db = pParse->db;
110508
110509 /* Look up the table being altered. */
110510 assert( pParse->pNewTable==0 )((void) (0));
110511 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
110512 if( db->mallocFailed ) goto exit_begin_add_column;
110513 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
110514 if( !pTab ) goto exit_begin_add_column;
110515
110516#ifndef SQLITE_OMIT_VIRTUALTABLE
110517 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
110518 sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
110519 goto exit_begin_add_column;
110520 }
110521#endif
110522
110523 /* Make sure this is not an attempt to ALTER a view. */
110524 if( IsView(pTab)((pTab)->eTabType==2) ){
110525 sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
110526 goto exit_begin_add_column;
110527 }
110528 if( SQLITE_OK0!=isAlterableTable(pParse, pTab) ){
110529 goto exit_begin_add_column;
110530 }
110531
110532 sqlite3MayAbort(pParse);
110533 assert( IsOrdinaryTable(pTab) )((void) (0));
110534 assert( pTab->u.tab.addColOffset>0 )((void) (0));
110535 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
110536
110537 /* Put a copy of the Table struct in Parse.pNewTable for the
110538 ** sqlite3AddColumn() function and friends to modify. But modify
110539 ** the name by adding an "sqlite_altertab_" prefix. By adding this
110540 ** prefix, we insure that the name will not collide with an existing
110541 ** table because user table are not allowed to have the "sqlite_"
110542 ** prefix on their name.
110543 */
110544 pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
110545 if( !pNew ) goto exit_begin_add_column;
110546 pParse->pNewTable = pNew;
110547 pNew->nTabRef = 1;
110548 pNew->nCol = pTab->nCol;
110549 assert( pNew->nCol>0 )((void) (0));
110550 nAlloc = (((pNew->nCol-1)/8)*8)+8;
110551 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 )((void) (0));
110552 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
110553 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
110554 if( !pNew->aCol || !pNew->zName ){
110555 assert( db->mallocFailed )((void) (0));
110556 goto exit_begin_add_column;
110557 }
110558 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
110559 for(i=0; i<pNew->nCol; i++){
110560 Column *pCol = &pNew->aCol[i];
110561 pCol->zCnName = sqlite3DbStrDup(db, pCol->zCnName);
110562 pCol->hName = sqlite3StrIHash(pCol->zCnName);
110563 }
110564 assert( IsOrdinaryTable(pNew) )((void) (0));
110565 pNew->u.tab.pDfltList = sqlite3ExprListDup(db, pTab->u.tab.pDfltList, 0);
110566 pNew->pSchema = db->aDb[iDb].pSchema;
110567 pNew->u.tab.addColOffset = pTab->u.tab.addColOffset;
110568 pNew->nTabRef = 1;
110569
110570exit_begin_add_column:
110571 sqlite3SrcListDelete(db, pSrc);
110572 return;
110573}
110574
110575/*
110576** Parameter pTab is the subject of an ALTER TABLE ... RENAME COLUMN
110577** command. This function checks if the table is a view or virtual
110578** table (columns of views or virtual tables may not be renamed). If so,
110579** it loads an error message into pParse and returns non-zero.
110580**
110581** Or, if pTab is not a view or virtual table, zero is returned.
110582*/
110583#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
110584static int isRealTable(Parse *pParse, Table *pTab, int bDrop){
110585 const char *zType = 0;
110586#ifndef SQLITE_OMIT_VIEW
110587 if( IsView(pTab)((pTab)->eTabType==2) ){
110588 zType = "view";
110589 }
110590#endif
110591#ifndef SQLITE_OMIT_VIRTUALTABLE
110592 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
110593 zType = "virtual table";
110594 }
110595#endif
110596 if( zType ){
110597 sqlite3ErrorMsg(pParse, "cannot %s %s \"%s\"",
110598 (bDrop ? "drop column from" : "rename columns of"),
110599 zType, pTab->zName
110600 );
110601 return 1;
110602 }
110603 return 0;
110604}
110605#else /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
110606# define isRealTable(x,y,z) (0)
110607#endif
110608
110609/*
110610** Handles the following parser reduction:
110611**
110612** cmd ::= ALTER TABLE pSrc RENAME COLUMN pOld TO pNew
110613*/
110614SQLITE_PRIVATEstatic void sqlite3AlterRenameColumn(
110615 Parse *pParse, /* Parsing context */
110616 SrcList *pSrc, /* Table being altered. pSrc->nSrc==1 */
110617 Token *pOld, /* Name of column being changed */
110618 Token *pNew /* New column name */
110619){
110620 sqlite3 *db = pParse->db; /* Database connection */
110621 Table *pTab; /* Table being updated */
110622 int iCol; /* Index of column being renamed */
110623 char *zOld = 0; /* Old column name */
110624 char *zNew = 0; /* New column name */
110625 const char *zDb; /* Name of schema containing the table */
110626 int iSchema; /* Index of the schema */
110627 int bQuote; /* True to quote the new name */
110628
110629 /* Locate the table to be altered */
110630 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
110631 if( !pTab ) goto exit_rename_column;
110632
110633 /* Cannot alter a system table */
110634 if( SQLITE_OK0!=isAlterableTable(pParse, pTab) ) goto exit_rename_column;
110635 if( SQLITE_OK0!=isRealTable(pParse, pTab, 0) ) goto exit_rename_column;
110636
110637 /* Which schema holds the table to be altered */
110638 iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
110639 assert( iSchema>=0 )((void) (0));
110640 zDb = db->aDb[iSchema].zDbSName;
110641
110642#ifndef SQLITE_OMIT_AUTHORIZATION
110643 /* Invoke the authorization callback. */
110644 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE26, zDb, pTab->zName, 0) ){
110645 goto exit_rename_column;
110646 }
110647#endif
110648
110649 /* Make sure the old name really is a column name in the table to be
110650 ** altered. Set iCol to be the index of the column being renamed */
110651 zOld = sqlite3NameFromToken(db, pOld);
110652 if( !zOld ) goto exit_rename_column;
110653 for(iCol=0; iCol<pTab->nCol; iCol++){
110654 if( 0==sqlite3StrICmp(pTab->aCol[iCol].zCnName, zOld) ) break;
110655 }
110656 if( iCol==pTab->nCol ){
110657 sqlite3ErrorMsg(pParse, "no such column: \"%T\"", pOld);
110658 goto exit_rename_column;
110659 }
110660
110661 /* Ensure the schema contains no double-quoted strings */
110662 renameTestSchema(pParse, zDb, iSchema==1, "", 0);
110663 renameFixQuotes(pParse, zDb, iSchema==1);
110664
110665 /* Do the rename operation using a recursive UPDATE statement that
110666 ** uses the sqlite_rename_column() SQL function to compute the new
110667 ** CREATE statement text for the sqlite_schema table.
110668 */
110669 sqlite3MayAbort(pParse);
110670 zNew = sqlite3NameFromToken(db, pNew);
110671 if( !zNew ) goto exit_rename_column;
110672 assert( pNew->n>0 )((void) (0));
110673 bQuote = sqlite3Isquote(pNew->z[0])(sqlite3CtypeMap[(unsigned char)(pNew->z[0])]&0x80);
110674 sqlite3NestedParse(pParse,
110675 "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE"sqlite_master" " SET "
110676 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, %d) "
110677 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X' "
110678 " AND (type != 'index' OR tbl_name = %Q)",
110679 zDb,
110680 zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
110681 pTab->zName
110682 );
110683
110684 sqlite3NestedParse(pParse,
110685 "UPDATE temp." LEGACY_SCHEMA_TABLE"sqlite_master" " SET "
110686 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, 1) "
110687 "WHERE type IN ('trigger', 'view')",
110688 zDb, pTab->zName, iCol, zNew, bQuote
110689 );
110690
110691 /* Drop and reload the database schema. */
110692 renameReloadSchema(pParse, iSchema, INITFLAG_AlterRename0x0001);
110693 renameTestSchema(pParse, zDb, iSchema==1, "after rename", 1);
110694
110695 exit_rename_column:
110696 sqlite3SrcListDelete(db, pSrc);
110697 sqlite3DbFree(db, zOld);
110698 sqlite3DbFree(db, zNew);
110699 return;
110700}
110701
110702/*
110703** Each RenameToken object maps an element of the parse tree into
110704** the token that generated that element. The parse tree element
110705** might be one of:
110706**
110707** * A pointer to an Expr that represents an ID
110708** * The name of a table column in Column.zName
110709**
110710** A list of RenameToken objects can be constructed during parsing.
110711** Each new object is created by sqlite3RenameTokenMap().
110712** As the parse tree is transformed, the sqlite3RenameTokenRemap()
110713** routine is used to keep the mapping current.
110714**
110715** After the parse finishes, renameTokenFind() routine can be used
110716** to look up the actual token value that created some element in
110717** the parse tree.
110718*/
110719struct RenameToken {
110720 const void *p; /* Parse tree element created by token t */
110721 Token t; /* The token that created parse tree element p */
110722 RenameToken *pNext; /* Next is a list of all RenameToken objects */
110723};
110724
110725/*
110726** The context of an ALTER TABLE RENAME COLUMN operation that gets passed
110727** down into the Walker.
110728*/
110729typedef struct RenameCtx RenameCtx;
110730struct RenameCtx {
110731 RenameToken *pList; /* List of tokens to overwrite */
110732 int nList; /* Number of tokens in pList */
110733 int iCol; /* Index of column being renamed */
110734 Table *pTab; /* Table being ALTERed */
110735 const char *zOld; /* Old column name */
110736};
110737
110738#ifdef SQLITE_DEBUG
110739/*
110740** This function is only for debugging. It performs two tasks:
110741**
110742** 1. Checks that pointer pPtr does not already appear in the
110743** rename-token list.
110744**
110745** 2. Dereferences each pointer in the rename-token list.
110746**
110747** The second is most effective when debugging under valgrind or
110748** address-sanitizer or similar. If any of these pointers no longer
110749** point to valid objects, an exception is raised by the memory-checking
110750** tool.
110751**
110752** The point of this is to prevent comparisons of invalid pointer values.
110753** Even though this always seems to work, it is undefined according to the
110754** C standard. Example of undefined comparison:
110755**
110756** sqlite3_free(x);
110757** if( x==y ) ...
110758**
110759** Technically, as x no longer points into a valid object or to the byte
110760** following a valid object, it may not be used in comparison operations.
110761*/
110762static void renameTokenCheckAll(Parse *pParse, const void *pPtr){
110763 assert( pParse==pParse->db->pParse )((void) (0));
110764 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 )((void) (0));
110765 if( pParse->nErr==0 ){
110766 const RenameToken *p;
110767 u8 i = 0;
110768 for(p=pParse->pRename; p; p=p->pNext){
110769 if( p->p ){
110770 assert( p->p!=pPtr )((void) (0));
110771 i += *(u8*)(p->p);
110772 }
110773 }
110774 }
110775}
110776#else
110777# define renameTokenCheckAll(x,y)
110778#endif
110779
110780/*
110781** Remember that the parser tree element pPtr was created using
110782** the token pToken.
110783**
110784** In other words, construct a new RenameToken object and add it
110785** to the list of RenameToken objects currently being built up
110786** in pParse->pRename.
110787**
110788** The pPtr argument is returned so that this routine can be used
110789** with tail recursion in tokenExpr() routine, for a small performance
110790** improvement.
110791*/
110792SQLITE_PRIVATEstatic const void *sqlite3RenameTokenMap(
110793 Parse *pParse,
110794 const void *pPtr,
110795 const Token *pToken
110796){
110797 RenameToken *pNew;
110798 assert( pPtr || pParse->db->mallocFailed )((void) (0));
110799 renameTokenCheckAll(pParse, pPtr);
110800 if( ALWAYS(pParse->eParseMode!=PARSE_MODE_UNMAP)(pParse->eParseMode!=3) ){
110801 pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
110802 if( pNew ){
110803 pNew->p = pPtr;
110804 pNew->t = *pToken;
110805 pNew->pNext = pParse->pRename;
110806 pParse->pRename = pNew;
110807 }
110808 }
110809
110810 return pPtr;
110811}
110812
110813/*
110814** It is assumed that there is already a RenameToken object associated
110815** with parse tree element pFrom. This function remaps the associated token
110816** to parse tree element pTo.
110817*/
110818SQLITE_PRIVATEstatic void sqlite3RenameTokenRemap(Parse *pParse, const void *pTo, const void *pFrom){
110819 RenameToken *p;
110820 renameTokenCheckAll(pParse, pTo);
110821 for(p=pParse->pRename; p; p=p->pNext){
110822 if( p->p==pFrom ){
110823 p->p = pTo;
110824 break;
110825 }
110826 }
110827}
110828
110829/*
110830** Walker callback used by sqlite3RenameExprUnmap().
110831*/
110832static int renameUnmapExprCb(Walker *pWalker, Expr *pExpr){
110833 Parse *pParse = pWalker->pParse;
110834 sqlite3RenameTokenRemap(pParse, 0, (const void*)pExpr);
110835 if( ExprUseYTab(pExpr)(((pExpr)->flags&(0x1000000|0x2000000))==0) ){
110836 sqlite3RenameTokenRemap(pParse, 0, (const void*)&pExpr->y.pTab);
110837 }
110838 return WRC_Continue0;
110839}
110840
110841/*
110842** Iterate through the Select objects that are part of WITH clauses attached
110843** to select statement pSelect.
110844*/
110845static void renameWalkWith(Walker *pWalker, Select *pSelect){
110846 With *pWith = pSelect->pWith;
110847 if( pWith ){
110848 Parse *pParse = pWalker->pParse;
110849 int i;
110850 With *pCopy = 0;
110851 assert( pWith->nCte>0 )((void) (0));
110852 if( (pWith->a[0].pSelect->selFlags & SF_Expanded0x0000040)==0 ){
110853 /* Push a copy of the With object onto the with-stack. We use a copy
110854 ** here as the original will be expanded and resolved (flags SF_Expanded
110855 ** and SF_Resolved) below. And the parser code that uses the with-stack
110856 ** fails if the Select objects on it have already been expanded and
110857 ** resolved. */
110858 pCopy = sqlite3WithDup(pParse->db, pWith);
110859 pCopy = sqlite3WithPush(pParse, pCopy, 1);
110860 }
110861 for(i=0; i<pWith->nCte; i++){
110862 Select *p = pWith->a[i].pSelect;
110863 NameContext sNC;
110864 memset(&sNC, 0, sizeof(sNC));
110865 sNC.pParse = pParse;
110866 if( pCopy ) sqlite3SelectPrep(sNC.pParse, p, &sNC);
110867 if( sNC.pParse->db->mallocFailed ) return;
110868 sqlite3WalkSelect(pWalker, p);
110869 sqlite3RenameExprlistUnmap(pParse, pWith->a[i].pCols);
110870 }
110871 if( pCopy && pParse->pWith==pCopy ){
110872 pParse->pWith = pCopy->pOuter;
110873 }
110874 }
110875}
110876
110877/*
110878** Unmap all tokens in the IdList object passed as the second argument.
110879*/
110880static void unmapColumnIdlistNames(
110881 Parse *pParse,
110882 const IdList *pIdList
110883){
110884 int ii;
110885 assert( pIdList!=0 )((void) (0));
110886 for(ii=0; ii<pIdList->nId; ii++){
110887 sqlite3RenameTokenRemap(pParse, 0, (const void*)pIdList->a[ii].zName);
110888 }
110889}
110890
110891/*
110892** Walker callback used by sqlite3RenameExprUnmap().
110893*/
110894static int renameUnmapSelectCb(Walker *pWalker, Select *p){
110895 Parse *pParse = pWalker->pParse;
110896 int i;
110897 if( pParse->nErr ) return WRC_Abort2;
110898 testcase( p->selFlags & SF_View );
110899 testcase( p->selFlags & SF_CopyCte );
110900 if( p->selFlags & (SF_View0x0200000|SF_CopyCte0x4000000) ){
110901 return WRC_Prune1;
110902 }
110903 if( ALWAYS(p->pEList)(p->pEList) ){
110904 ExprList *pList = p->pEList;
110905 for(i=0; i<pList->nExpr; i++){
110906 if( pList->a[i].zEName && pList->a[i].fg.eEName==ENAME_NAME0 ){
110907 sqlite3RenameTokenRemap(pParse, 0, (void*)pList->a[i].zEName);
110908 }
110909 }
110910 }
110911 if( ALWAYS(p->pSrc)(p->pSrc) ){ /* Every Select as a SrcList, even if it is empty */
110912 SrcList *pSrc = p->pSrc;
110913 for(i=0; i<pSrc->nSrc; i++){
110914 sqlite3RenameTokenRemap(pParse, 0, (void*)pSrc->a[i].zName);
110915 if( pSrc->a[i].fg.isUsing==0 ){
110916 sqlite3WalkExpr(pWalker, pSrc->a[i].u3.pOn);
110917 }else{
110918 unmapColumnIdlistNames(pParse, pSrc->a[i].u3.pUsing);
110919 }
110920 }
110921 }
110922
110923 renameWalkWith(pWalker, p);
110924 return WRC_Continue0;
110925}
110926
110927/*
110928** Remove all nodes that are part of expression pExpr from the rename list.
110929*/
110930SQLITE_PRIVATEstatic void sqlite3RenameExprUnmap(Parse *pParse, Expr *pExpr){
110931 u8 eMode = pParse->eParseMode;
110932 Walker sWalker;
110933 memset(&sWalker, 0, sizeof(Walker));
110934 sWalker.pParse = pParse;
110935 sWalker.xExprCallback = renameUnmapExprCb;
110936 sWalker.xSelectCallback = renameUnmapSelectCb;
110937 pParse->eParseMode = PARSE_MODE_UNMAP3;
110938 sqlite3WalkExpr(&sWalker, pExpr);
110939 pParse->eParseMode = eMode;
110940}
110941
110942/*
110943** Remove all nodes that are part of expression-list pEList from the
110944** rename list.
110945*/
110946SQLITE_PRIVATEstatic void sqlite3RenameExprlistUnmap(Parse *pParse, ExprList *pEList){
110947 if( pEList ){
110948 int i;
110949 Walker sWalker;
110950 memset(&sWalker, 0, sizeof(Walker));
110951 sWalker.pParse = pParse;
110952 sWalker.xExprCallback = renameUnmapExprCb;
110953 sqlite3WalkExprList(&sWalker, pEList);
110954 for(i=0; i<pEList->nExpr; i++){
110955 if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME)(pEList->a[i].fg.eEName==0) ){
110956 sqlite3RenameTokenRemap(pParse, 0, (void*)pEList->a[i].zEName);
110957 }
110958 }
110959 }
110960}
110961
110962/*
110963** Free the list of RenameToken objects given in the second argument
110964*/
110965static void renameTokenFree(sqlite3 *db, RenameToken *pToken){
110966 RenameToken *pNext;
110967 RenameToken *p;
110968 for(p=pToken; p; p=pNext){
110969 pNext = p->pNext;
110970 sqlite3DbFree(db, p);
110971 }
110972}
110973
110974/*
110975** Search the Parse object passed as the first argument for a RenameToken
110976** object associated with parse tree element pPtr. If found, return a pointer
110977** to it. Otherwise, return NULL.
110978**
110979** If the second argument passed to this function is not NULL and a matching
110980** RenameToken object is found, remove it from the Parse object and add it to
110981** the list maintained by the RenameCtx object.
110982*/
110983static RenameToken *renameTokenFind(
110984 Parse *pParse,
110985 struct RenameCtx *pCtx,
110986 const void *pPtr
110987){
110988 RenameToken **pp;
110989 if( NEVER(pPtr==0)(pPtr==0) ){
110990 return 0;
110991 }
110992 for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
110993 if( (*pp)->p==pPtr ){
110994 RenameToken *pToken = *pp;
110995 if( pCtx ){
110996 *pp = pToken->pNext;
110997 pToken->pNext = pCtx->pList;
110998 pCtx->pList = pToken;
110999 pCtx->nList++;
111000 }
111001 return pToken;
111002 }
111003 }
111004 return 0;
111005}
111006
111007/*
111008** This is a Walker select callback. It does nothing. It is only required
111009** because without a dummy callback, sqlite3WalkExpr() and similar do not
111010** descend into sub-select statements.
111011*/
111012static int renameColumnSelectCb(Walker *pWalker, Select *p){
111013 if( p->selFlags & (SF_View0x0200000|SF_CopyCte0x4000000) ){
111014 testcase( p->selFlags & SF_View );
111015 testcase( p->selFlags & SF_CopyCte );
111016 return WRC_Prune1;
111017 }
111018 renameWalkWith(pWalker, p);
111019 return WRC_Continue0;
111020}
111021
111022/*
111023** This is a Walker expression callback.
111024**
111025** For every TK_COLUMN node in the expression tree, search to see
111026** if the column being references is the column being renamed by an
111027** ALTER TABLE statement. If it is, then attach its associated
111028** RenameToken object to the list of RenameToken objects being
111029** constructed in RenameCtx object at pWalker->u.pRename.
111030*/
111031static int renameColumnExprCb(Walker *pWalker, Expr *pExpr){
111032 RenameCtx *p = pWalker->u.pRename;
111033 if( pExpr->op==TK_TRIGGER77
111034 && pExpr->iColumn==p->iCol
111035 && pWalker->pParse->pTriggerTab==p->pTab
111036 ){
111037 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
111038 }else if( pExpr->op==TK_COLUMN167
111039 && pExpr->iColumn==p->iCol
111040 && ALWAYS(ExprUseYTab(pExpr))((((pExpr)->flags&(0x1000000|0x2000000))==0))
111041 && p->pTab==pExpr->y.pTab
111042 ){
111043 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
111044 }
111045 return WRC_Continue0;
111046}
111047
111048/*
111049** The RenameCtx contains a list of tokens that reference a column that
111050** is being renamed by an ALTER TABLE statement. Return the "last"
111051** RenameToken in the RenameCtx and remove that RenameToken from the
111052** RenameContext. "Last" means the last RenameToken encountered when
111053** the input SQL is parsed from left to right. Repeated calls to this routine
111054** return all column name tokens in the order that they are encountered
111055** in the SQL statement.
111056*/
111057static RenameToken *renameColumnTokenNext(RenameCtx *pCtx){
111058 RenameToken *pBest = pCtx->pList;
111059 RenameToken *pToken;
111060 RenameToken **pp;
111061
111062 for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){
111063 if( pToken->t.z>pBest->t.z ) pBest = pToken;
111064 }
111065 for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext);
111066 *pp = pBest->pNext;
111067
111068 return pBest;
111069}
111070
111071/*
111072** An error occured while parsing or otherwise processing a database
111073** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an
111074** ALTER TABLE RENAME COLUMN program. The error message emitted by the
111075** sub-routine is currently stored in pParse->zErrMsg. This function
111076** adds context to the error message and then stores it in pCtx.
111077*/
111078static void renameColumnParseError(
111079 sqlite3_context *pCtx,
111080 const char *zWhen,
111081 sqlite3_value *pType,
111082 sqlite3_value *pObject,
111083 Parse *pParse
111084){
111085 const char *zT = (const char*)sqlite3_value_text(pType);
111086 const char *zN = (const char*)sqlite3_value_text(pObject);
111087 char *zErr;
111088
111089 zErr = sqlite3MPrintf(pParse->db, "error in %s %s%s%s: %s",
111090 zT, zN, (zWhen[0] ? " " : ""), zWhen,
111091 pParse->zErrMsg
111092 );
111093 sqlite3_result_error(pCtx, zErr, -1);
111094 sqlite3DbFree(pParse->db, zErr);
111095}
111096
111097/*
111098** For each name in the the expression-list pEList (i.e. each
111099** pEList->a[i].zName) that matches the string in zOld, extract the
111100** corresponding rename-token from Parse object pParse and add it
111101** to the RenameCtx pCtx.
111102*/
111103static void renameColumnElistNames(
111104 Parse *pParse,
111105 RenameCtx *pCtx,
111106 const ExprList *pEList,
111107 const char *zOld
111108){
111109 if( pEList ){
111110 int i;
111111 for(i=0; i<pEList->nExpr; i++){
111112 const char *zName = pEList->a[i].zEName;
111113 if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME)(pEList->a[i].fg.eEName==0)
111114 && ALWAYS(zName!=0)(zName!=0)
111115 && 0==sqlite3_stricmp(zName, zOld)
111116 ){
111117 renameTokenFind(pParse, pCtx, (const void*)zName);
111118 }
111119 }
111120 }
111121}
111122
111123/*
111124** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName)
111125** that matches the string in zOld, extract the corresponding rename-token
111126** from Parse object pParse and add it to the RenameCtx pCtx.
111127*/
111128static void renameColumnIdlistNames(
111129 Parse *pParse,
111130 RenameCtx *pCtx,
111131 const IdList *pIdList,
111132 const char *zOld
111133){
111134 if( pIdList ){
111135 int i;
111136 for(i=0; i<pIdList->nId; i++){
111137 const char *zName = pIdList->a[i].zName;
111138 if( 0==sqlite3_stricmp(zName, zOld) ){
111139 renameTokenFind(pParse, pCtx, (const void*)zName);
111140 }
111141 }
111142 }
111143}
111144
111145
111146/*
111147** Parse the SQL statement zSql using Parse object (*p). The Parse object
111148** is initialized by this function before it is used.
111149*/
111150static int renameParseSql(
111151 Parse *p, /* Memory to use for Parse object */
111152 const char *zDb, /* Name of schema SQL belongs to */
111153 sqlite3 *db, /* Database handle */
111154 const char *zSql, /* SQL to parse */
111155 int bTemp /* True if SQL is from temp schema */
111156){
111157 int rc;
111158
111159 sqlite3ParseObjectInit(p, db);
111160 if( zSql==0 ){
111161 return SQLITE_NOMEM7;
111162 }
111163 if( sqlite3StrNICmpsqlite3_strnicmp(zSql,"CREATE ",7)!=0 ){
111164 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(111164);
111165 }
111166 db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
111167 p->eParseMode = PARSE_MODE_RENAME2;
111168 p->db = db;
111169 p->nQueryLoop = 1;
111170 rc = sqlite3RunParser(p, zSql);
111171 if( db->mallocFailed ) rc = SQLITE_NOMEM7;
111172 if( rc==SQLITE_OK0
111173 && NEVER(p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0)(p->pNewTable==0 && p->pNewIndex==0 && p
->pNewTrigger==0)
111174 ){
111175 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(111175);
111176 }
111177
111178#ifdef SQLITE_DEBUG
111179 /* Ensure that all mappings in the Parse.pRename list really do map to
111180 ** a part of the input string. */
111181 if( rc==SQLITE_OK0 ){
111182 int nSql = sqlite3Strlen30(zSql);
111183 RenameToken *pToken;
111184 for(pToken=p->pRename; pToken; pToken=pToken->pNext){
111185 assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] )((void) (0));
111186 }
111187 }
111188#endif
111189
111190 db->init.iDb = 0;
111191 return rc;
111192}
111193
111194/*
111195** This function edits SQL statement zSql, replacing each token identified
111196** by the linked list pRename with the text of zNew. If argument bQuote is
111197** true, then zNew is always quoted first. If no error occurs, the result
111198** is loaded into context object pCtx as the result.
111199**
111200** Or, if an error occurs (i.e. an OOM condition), an error is left in
111201** pCtx and an SQLite error code returned.
111202*/
111203static int renameEditSql(
111204 sqlite3_context *pCtx, /* Return result here */
111205 RenameCtx *pRename, /* Rename context */
111206 const char *zSql, /* SQL statement to edit */
111207 const char *zNew, /* New token text */
111208 int bQuote /* True to always quote token */
111209){
111210 i64 nNew = sqlite3Strlen30(zNew);
111211 i64 nSql = sqlite3Strlen30(zSql);
111212 sqlite3 *db = sqlite3_context_db_handle(pCtx);
111213 int rc = SQLITE_OK0;
111214 char *zQuot = 0;
111215 char *zOut;
111216 i64 nQuot = 0;
111217 char *zBuf1 = 0;
111218 char *zBuf2 = 0;
111219
111220 if( zNew ){
111221 /* Set zQuot to point to a buffer containing a quoted copy of the
111222 ** identifier zNew. If the corresponding identifier in the original
111223 ** ALTER TABLE statement was quoted (bQuote==1), then set zNew to
111224 ** point to zQuot so that all substitutions are made using the
111225 ** quoted version of the new column name. */
111226 zQuot = sqlite3MPrintf(db, "\"%w\" ", zNew);
111227 if( zQuot==0 ){
111228 return SQLITE_NOMEM7;
111229 }else{
111230 nQuot = sqlite3Strlen30(zQuot)-1;
111231 }
111232
111233 assert( nQuot>=nNew )((void) (0));
111234 zOut = sqlite3DbMallocZero(db, nSql + pRename->nList*nQuot + 1);
111235 }else{
111236 zOut = (char*)sqlite3DbMallocZero(db, (nSql*2+1) * 3);
111237 if( zOut ){
111238 zBuf1 = &zOut[nSql*2+1];
111239 zBuf2 = &zOut[nSql*4+2];
111240 }
111241 }
111242
111243 /* At this point pRename->pList contains a list of RenameToken objects
111244 ** corresponding to all tokens in the input SQL that must be replaced
111245 ** with the new column name, or with single-quoted versions of themselves.
111246 ** All that remains is to construct and return the edited SQL string. */
111247 if( zOut ){
111248 int nOut = nSql;
111249 memcpy(zOut, zSql, nSql);
111250 while( pRename->pList ){
111251 int iOff; /* Offset of token to replace in zOut */
111252 u32 nReplace;
111253 const char *zReplace;
111254 RenameToken *pBest = renameColumnTokenNext(pRename);
111255
111256 if( zNew ){
111257 if( bQuote==0 && sqlite3IsIdChar(*pBest->t.z) ){
111258 nReplace = nNew;
111259 zReplace = zNew;
111260 }else{
111261 nReplace = nQuot;
111262 zReplace = zQuot;
111263 if( pBest->t.z[pBest->t.n]=='"' ) nReplace++;
111264 }
111265 }else{
111266 /* Dequote the double-quoted token. Then requote it again, this time
111267 ** using single quotes. If the character immediately following the
111268 ** original token within the input SQL was a single quote ('), then
111269 ** add another space after the new, single-quoted version of the
111270 ** token. This is so that (SELECT "string"'alias') maps to
111271 ** (SELECT 'string' 'alias'), and not (SELECT 'string''alias'). */
111272 memcpy(zBuf1, pBest->t.z, pBest->t.n);
111273 zBuf1[pBest->t.n] = 0;
111274 sqlite3Dequote(zBuf1);
111275 sqlite3_snprintf(nSql*2, zBuf2, "%Q%s", zBuf1,
111276 pBest->t.z[pBest->t.n]=='\'' ? " " : ""
111277 );
111278 zReplace = zBuf2;
111279 nReplace = sqlite3Strlen30(zReplace);
111280 }
111281
111282 iOff = pBest->t.z - zSql;
111283 if( pBest->t.n!=nReplace ){
111284 memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n],
111285 nOut - (iOff + pBest->t.n)
111286 );
111287 nOut += nReplace - pBest->t.n;
111288 zOut[nOut] = '\0';
111289 }
111290 memcpy(&zOut[iOff], zReplace, nReplace);
111291 sqlite3DbFree(db, pBest);
111292 }
111293
111294 sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
111295 sqlite3DbFree(db, zOut);
111296 }else{
111297 rc = SQLITE_NOMEM7;
111298 }
111299
111300 sqlite3_free(zQuot);
111301 return rc;
111302}
111303
111304/*
111305** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
111306** it was read from the schema of database zDb. Return SQLITE_OK if
111307** successful. Otherwise, return an SQLite error code and leave an error
111308** message in the Parse object.
111309*/
111310static int renameResolveTrigger(Parse *pParse){
111311 sqlite3 *db = pParse->db;
111312 Trigger *pNew = pParse->pNewTrigger;
111313 TriggerStep *pStep;
111314 NameContext sNC;
111315 int rc = SQLITE_OK0;
111316
111317 memset(&sNC, 0, sizeof(sNC));
111318 sNC.pParse = pParse;
111319 assert( pNew->pTabSchema )((void) (0));
111320 pParse->pTriggerTab = sqlite3FindTable(db, pNew->table,
111321 db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
111322 );
111323 pParse->eTriggerOp = pNew->op;
111324 /* ALWAYS() because if the table of the trigger does not exist, the
111325 ** error would have been hit before this point */
111326 if( ALWAYS(pParse->pTriggerTab)(pParse->pTriggerTab) ){
111327 rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab);
111328 }
111329
111330 /* Resolve symbols in WHEN clause */
111331 if( rc==SQLITE_OK0 && pNew->pWhen ){
111332 rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
111333 }
111334
111335 for(pStep=pNew->step_list; rc==SQLITE_OK0 && pStep; pStep=pStep->pNext){
111336 if( pStep->pSelect ){
111337 sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
111338 if( pParse->nErr ) rc = pParse->rc;
111339 }
111340 if( rc==SQLITE_OK0 && pStep->zTarget ){
111341 SrcList *pSrc = sqlite3TriggerStepSrc(pParse, pStep);
111342 if( pSrc ){
111343 Select *pSel = sqlite3SelectNew(
111344 pParse, pStep->pExprList, pSrc, 0, 0, 0, 0, 0, 0
111345 );
111346 if( pSel==0 ){
111347 pStep->pExprList = 0;
111348 pSrc = 0;
111349 rc = SQLITE_NOMEM7;
111350 }else{
111351 sqlite3SelectPrep(pParse, pSel, 0);
111352 rc = pParse->nErr ? SQLITE_ERROR1 : SQLITE_OK0;
111353 assert( pStep->pExprList==0 || pStep->pExprList==pSel->pEList )((void) (0));
111354 assert( pSrc==pSel->pSrc )((void) (0));
111355 if( pStep->pExprList ) pSel->pEList = 0;
111356 pSel->pSrc = 0;
111357 sqlite3SelectDelete(db, pSel);
111358 }
111359 if( pStep->pFrom ){
111360 int i;
111361 for(i=0; i<pStep->pFrom->nSrc && rc==SQLITE_OK0; i++){
111362 SrcItem *p = &pStep->pFrom->a[i];
111363 if( p->pSelect ){
111364 sqlite3SelectPrep(pParse, p->pSelect, 0);
111365 }
111366 }
111367 }
111368
111369 if( db->mallocFailed ){
111370 rc = SQLITE_NOMEM7;
111371 }
111372 sNC.pSrcList = pSrc;
111373 if( rc==SQLITE_OK0 && pStep->pWhere ){
111374 rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere);
111375 }
111376 if( rc==SQLITE_OK0 ){
111377 rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList);
111378 }
111379 assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) )((void) (0));
111380 if( pStep->pUpsert && rc==SQLITE_OK0 ){
111381 Upsert *pUpsert = pStep->pUpsert;
111382 pUpsert->pUpsertSrc = pSrc;
111383 sNC.uNC.pUpsert = pUpsert;
111384 sNC.ncFlags = NC_UUpsert0x000200;
111385 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
111386 if( rc==SQLITE_OK0 ){
111387 ExprList *pUpsertSet = pUpsert->pUpsertSet;
111388 rc = sqlite3ResolveExprListNames(&sNC, pUpsertSet);
111389 }
111390 if( rc==SQLITE_OK0 ){
111391 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere);
111392 }
111393 if( rc==SQLITE_OK0 ){
111394 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
111395 }
111396 sNC.ncFlags = 0;
111397 }
111398 sNC.pSrcList = 0;
111399 sqlite3SrcListDelete(db, pSrc);
111400 }else{
111401 rc = SQLITE_NOMEM7;
111402 }
111403 }
111404 }
111405 return rc;
111406}
111407
111408/*
111409** Invoke sqlite3WalkExpr() or sqlite3WalkSelect() on all Select or Expr
111410** objects that are part of the trigger passed as the second argument.
111411*/
111412static void renameWalkTrigger(Walker *pWalker, Trigger *pTrigger){
111413 TriggerStep *pStep;
111414
111415 /* Find tokens to edit in WHEN clause */
111416 sqlite3WalkExpr(pWalker, pTrigger->pWhen);
111417
111418 /* Find tokens to edit in trigger steps */
111419 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
111420 sqlite3WalkSelect(pWalker, pStep->pSelect);
111421 sqlite3WalkExpr(pWalker, pStep->pWhere);
111422 sqlite3WalkExprList(pWalker, pStep->pExprList);
111423 if( pStep->pUpsert ){
111424 Upsert *pUpsert = pStep->pUpsert;
111425 sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget);
111426 sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
111427 sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
111428 sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
111429 }
111430 if( pStep->pFrom ){
111431 int i;
111432 for(i=0; i<pStep->pFrom->nSrc; i++){
111433 sqlite3WalkSelect(pWalker, pStep->pFrom->a[i].pSelect);
111434 }
111435 }
111436 }
111437}
111438
111439/*
111440** Free the contents of Parse object (*pParse). Do not free the memory
111441** occupied by the Parse object itself.
111442*/
111443static void renameParseCleanup(Parse *pParse){
111444 sqlite3 *db = pParse->db;
111445 Index *pIdx;
111446 if( pParse->pVdbe ){
111447 sqlite3VdbeFinalize(pParse->pVdbe);
111448 }
111449 sqlite3DeleteTable(db, pParse->pNewTable);
111450 while( (pIdx = pParse->pNewIndex)!=0 ){
111451 pParse->pNewIndex = pIdx->pNext;
111452 sqlite3FreeIndex(db, pIdx);
111453 }
111454 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
111455 sqlite3DbFree(db, pParse->zErrMsg);
111456 renameTokenFree(db, pParse->pRename);
111457 sqlite3ParseObjectReset(pParse);
111458}
111459
111460/*
111461** SQL function:
111462**
111463** sqlite_rename_column(SQL,TYPE,OBJ,DB,TABLE,COL,NEWNAME,QUOTE,TEMP)
111464**
111465** 0. zSql: SQL statement to rewrite
111466** 1. type: Type of object ("table", "view" etc.)
111467** 2. object: Name of object
111468** 3. Database: Database name (e.g. "main")
111469** 4. Table: Table name
111470** 5. iCol: Index of column to rename
111471** 6. zNew: New column name
111472** 7. bQuote: Non-zero if the new column name should be quoted.
111473** 8. bTemp: True if zSql comes from temp schema
111474**
111475** Do a column rename operation on the CREATE statement given in zSql.
111476** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
111477** into zNew. The name should be quoted if bQuote is true.
111478**
111479** This function is used internally by the ALTER TABLE RENAME COLUMN command.
111480** It is only accessible to SQL created using sqlite3NestedParse(). It is
111481** not reachable from ordinary SQL passed into sqlite3_prepare() unless the
111482** SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test setting is enabled.
111483*/
111484static void renameColumnFunc(
111485 sqlite3_context *context,
111486 int NotUsed,
111487 sqlite3_value **argv
111488){
111489 sqlite3 *db = sqlite3_context_db_handle(context);
111490 RenameCtx sCtx;
111491 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
111492 const char *zDb = (const char*)sqlite3_value_text(argv[3]);
111493 const char *zTable = (const char*)sqlite3_value_text(argv[4]);
111494 int iCol = sqlite3_value_int(argv[5]);
111495 const char *zNew = (const char*)sqlite3_value_text(argv[6]);
111496 int bQuote = sqlite3_value_int(argv[7]);
111497 int bTemp = sqlite3_value_int(argv[8]);
111498 const char *zOld;
111499 int rc;
111500 Parse sParse;
111501 Walker sWalker;
111502 Index *pIdx;
111503 int i;
111504 Table *pTab;
111505#ifndef SQLITE_OMIT_AUTHORIZATION
111506 sqlite3_xauth xAuth = db->xAuth;
111507#endif
111508
111509 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
111510 if( zSql==0 ) return;
111511 if( zTable==0 ) return;
111512 if( zNew==0 ) return;
111513 if( iCol<0 ) return;
111514 sqlite3BtreeEnterAll(db);
111515 pTab = sqlite3FindTable(db, zTable, zDb);
111516 if( pTab==0 || iCol>=pTab->nCol ){
111517 sqlite3BtreeLeaveAll(db);
111518 return;
111519 }
111520 zOld = pTab->aCol[iCol].zCnName;
111521 memset(&sCtx, 0, sizeof(sCtx));
111522 sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
111523
111524#ifndef SQLITE_OMIT_AUTHORIZATION
111525 db->xAuth = 0;
111526#endif
111527 rc = renameParseSql(&sParse, zDb, db, zSql, bTemp);
111528
111529 /* Find tokens that need to be replaced. */
111530 memset(&sWalker, 0, sizeof(Walker));
111531 sWalker.pParse = &sParse;
111532 sWalker.xExprCallback = renameColumnExprCb;
111533 sWalker.xSelectCallback = renameColumnSelectCb;
111534 sWalker.u.pRename = &sCtx;
111535
111536 sCtx.pTab = pTab;
111537 if( rc!=SQLITE_OK0 ) goto renameColumnFunc_done;
111538 if( sParse.pNewTable ){
111539 if( IsView(sParse.pNewTable)((sParse.pNewTable)->eTabType==2) ){
111540 Select *pSelect = sParse.pNewTable->u.view.pSelect;
111541 pSelect->selFlags &= ~SF_View0x0200000;
111542 sParse.rc = SQLITE_OK0;
111543 sqlite3SelectPrep(&sParse, pSelect, 0);
111544 rc = (db->mallocFailed ? SQLITE_NOMEM7 : sParse.rc);
111545 if( rc==SQLITE_OK0 ){
111546 sqlite3WalkSelect(&sWalker, pSelect);
111547 }
111548 if( rc!=SQLITE_OK0 ) goto renameColumnFunc_done;
111549 }else if( IsOrdinaryTable(sParse.pNewTable)((sParse.pNewTable)->eTabType==0) ){
111550 /* A regular table */
111551 int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
111552 FKey *pFKey;
111553 sCtx.pTab = sParse.pNewTable;
111554 if( bFKOnly==0 ){
111555 if( iCol<sParse.pNewTable->nCol ){
111556 renameTokenFind(
111557 &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zCnName
111558 );
111559 }
111560 if( sCtx.iCol<0 ){
111561 renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
111562 }
111563 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
111564 for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
111565 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
111566 }
111567 for(pIdx=sParse.pNewIndex; pIdx; pIdx=pIdx->pNext){
111568 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
111569 }
111570#ifndef SQLITE_OMIT_GENERATED_COLUMNS
111571 for(i=0; i<sParse.pNewTable->nCol; i++){
111572 Expr *pExpr = sqlite3ColumnExpr(sParse.pNewTable,
111573 &sParse.pNewTable->aCol[i]);
111574 sqlite3WalkExpr(&sWalker, pExpr);
111575 }
111576#endif
111577 }
111578
111579 assert( IsOrdinaryTable(sParse.pNewTable) )((void) (0));
111580 for(pFKey=sParse.pNewTable->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
111581 for(i=0; i<pFKey->nCol; i++){
111582 if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){
111583 renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
111584 }
111585 if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
111586 && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
111587 ){
111588 renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
111589 }
111590 }
111591 }
111592 }
111593 }else if( sParse.pNewIndex ){
111594 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
111595 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
111596 }else{
111597 /* A trigger */
111598 TriggerStep *pStep;
111599 rc = renameResolveTrigger(&sParse);
111600 if( rc!=SQLITE_OK0 ) goto renameColumnFunc_done;
111601
111602 for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){
111603 if( pStep->zTarget ){
111604 Table *pTarget = sqlite3LocateTable(&sParse, 0, pStep->zTarget, zDb);
111605 if( pTarget==pTab ){
111606 if( pStep->pUpsert ){
111607 ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet;
111608 renameColumnElistNames(&sParse, &sCtx, pUpsertSet, zOld);
111609 }
111610 renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld);
111611 renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld);
111612 }
111613 }
111614 }
111615
111616
111617 /* Find tokens to edit in UPDATE OF clause */
111618 if( sParse.pTriggerTab==pTab ){
111619 renameColumnIdlistNames(&sParse, &sCtx,sParse.pNewTrigger->pColumns,zOld);
111620 }
111621
111622 /* Find tokens to edit in various expressions and selects */
111623 renameWalkTrigger(&sWalker, sParse.pNewTrigger);
111624 }
111625
111626 assert( rc==SQLITE_OK )((void) (0));
111627 rc = renameEditSql(context, &sCtx, zSql, zNew, bQuote);
111628
111629renameColumnFunc_done:
111630 if( rc!=SQLITE_OK0 ){
111631 if( rc==SQLITE_ERROR1 && sqlite3WritableSchema(db) ){
111632 sqlite3_result_value(context, argv[0]);
111633 }else if( sParse.zErrMsg ){
111634 renameColumnParseError(context, "", argv[1], argv[2], &sParse);
111635 }else{
111636 sqlite3_result_error_code(context, rc);
111637 }
111638 }
111639
111640 renameParseCleanup(&sParse);
111641 renameTokenFree(db, sCtx.pList);
111642#ifndef SQLITE_OMIT_AUTHORIZATION
111643 db->xAuth = xAuth;
111644#endif
111645 sqlite3BtreeLeaveAll(db);
111646}
111647
111648/*
111649** Walker expression callback used by "RENAME TABLE".
111650*/
111651static int renameTableExprCb(Walker *pWalker, Expr *pExpr){
111652 RenameCtx *p = pWalker->u.pRename;
111653 if( pExpr->op==TK_COLUMN167
111654 && ALWAYS(ExprUseYTab(pExpr))((((pExpr)->flags&(0x1000000|0x2000000))==0))
111655 && p->pTab==pExpr->y.pTab
111656 ){
111657 renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab);
111658 }
111659 return WRC_Continue0;
111660}
111661
111662/*
111663** Walker select callback used by "RENAME TABLE".
111664*/
111665static int renameTableSelectCb(Walker *pWalker, Select *pSelect){
111666 int i;
111667 RenameCtx *p = pWalker->u.pRename;
111668 SrcList *pSrc = pSelect->pSrc;
111669 if( pSelect->selFlags & (SF_View0x0200000|SF_CopyCte0x4000000) ){
111670 testcase( pSelect->selFlags & SF_View );
111671 testcase( pSelect->selFlags & SF_CopyCte );
111672 return WRC_Prune1;
111673 }
111674 if( NEVER(pSrc==0)(pSrc==0) ){
111675 assert( pWalker->pParse->db->mallocFailed )((void) (0));
111676 return WRC_Abort2;
111677 }
111678 for(i=0; i<pSrc->nSrc; i++){
111679 SrcItem *pItem = &pSrc->a[i];
111680 if( pItem->pTab==p->pTab ){
111681 renameTokenFind(pWalker->pParse, p, pItem->zName);
111682 }
111683 }
111684 renameWalkWith(pWalker, pSelect);
111685
111686 return WRC_Continue0;
111687}
111688
111689
111690/*
111691** This C function implements an SQL user function that is used by SQL code
111692** generated by the ALTER TABLE ... RENAME command to modify the definition
111693** of any foreign key constraints that use the table being renamed as the
111694** parent table. It is passed three arguments:
111695**
111696** 0: The database containing the table being renamed.
111697** 1. type: Type of object ("table", "view" etc.)
111698** 2. object: Name of object
111699** 3: The complete text of the schema statement being modified,
111700** 4: The old name of the table being renamed, and
111701** 5: The new name of the table being renamed.
111702** 6: True if the schema statement comes from the temp db.
111703**
111704** It returns the new schema statement. For example:
111705**
111706** sqlite_rename_table('main', 'CREATE TABLE t1(a REFERENCES t2)','t2','t3',0)
111707** -> 'CREATE TABLE t1(a REFERENCES t3)'
111708*/
111709static void renameTableFunc(
111710 sqlite3_context *context,
111711 int NotUsed,
111712 sqlite3_value **argv
111713){
111714 sqlite3 *db = sqlite3_context_db_handle(context);
111715 const char *zDb = (const char*)sqlite3_value_text(argv[0]);
111716 const char *zInput = (const char*)sqlite3_value_text(argv[3]);
111717 const char *zOld = (const char*)sqlite3_value_text(argv[4]);
111718 const char *zNew = (const char*)sqlite3_value_text(argv[5]);
111719 int bTemp = sqlite3_value_int(argv[6]);
111720 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
111721
111722 if( zInput && zOld && zNew ){
111723 Parse sParse;
111724 int rc;
111725 int bQuote = 1;
111726 RenameCtx sCtx;
111727 Walker sWalker;
111728
111729#ifndef SQLITE_OMIT_AUTHORIZATION
111730 sqlite3_xauth xAuth = db->xAuth;
111731 db->xAuth = 0;
111732#endif
111733
111734 sqlite3BtreeEnterAll(db);
111735
111736 memset(&sCtx, 0, sizeof(RenameCtx));
111737 sCtx.pTab = sqlite3FindTable(db, zOld, zDb);
111738 memset(&sWalker, 0, sizeof(Walker));
111739 sWalker.pParse = &sParse;
111740 sWalker.xExprCallback = renameTableExprCb;
111741 sWalker.xSelectCallback = renameTableSelectCb;
111742 sWalker.u.pRename = &sCtx;
111743
111744 rc = renameParseSql(&sParse, zDb, db, zInput, bTemp);
111745
111746 if( rc==SQLITE_OK0 ){
111747 int isLegacy = (db->flags & SQLITE_LegacyAlter0x04000000);
111748 if( sParse.pNewTable ){
111749 Table *pTab = sParse.pNewTable;
111750
111751 if( IsView(pTab)((pTab)->eTabType==2) ){
111752 if( isLegacy==0 ){
111753 Select *pSelect = pTab->u.view.pSelect;
111754 NameContext sNC;
111755 memset(&sNC, 0, sizeof(sNC));
111756 sNC.pParse = &sParse;
111757
111758 assert( pSelect->selFlags & SF_View )((void) (0));
111759 pSelect->selFlags &= ~SF_View0x0200000;
111760 sqlite3SelectPrep(&sParse, pTab->u.view.pSelect, &sNC);
111761 if( sParse.nErr ){
111762 rc = sParse.rc;
111763 }else{
111764 sqlite3WalkSelect(&sWalker, pTab->u.view.pSelect);
111765 }
111766 }
111767 }else{
111768 /* Modify any FK definitions to point to the new table. */
111769#ifndef SQLITE_OMIT_FOREIGN_KEY
111770 if( (isLegacy==0 || (db->flags & SQLITE_ForeignKeys0x00004000))
111771 && !IsVirtual(pTab)((pTab)->eTabType==1)
111772 ){
111773 FKey *pFKey;
111774 assert( IsOrdinaryTable(pTab) )((void) (0));
111775 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
111776 if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
111777 renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
111778 }
111779 }
111780 }
111781#endif
111782
111783 /* If this is the table being altered, fix any table refs in CHECK
111784 ** expressions. Also update the name that appears right after the
111785 ** "CREATE [VIRTUAL] TABLE" bit. */
111786 if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
111787 sCtx.pTab = pTab;
111788 if( isLegacy==0 ){
111789 sqlite3WalkExprList(&sWalker, pTab->pCheck);
111790 }
111791 renameTokenFind(&sParse, &sCtx, pTab->zName);
111792 }
111793 }
111794 }
111795
111796 else if( sParse.pNewIndex ){
111797 renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
111798 if( isLegacy==0 ){
111799 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
111800 }
111801 }
111802
111803#ifndef SQLITE_OMIT_TRIGGER
111804 else{
111805 Trigger *pTrigger = sParse.pNewTrigger;
111806 TriggerStep *pStep;
111807 if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld)
111808 && sCtx.pTab->pSchema==pTrigger->pTabSchema
111809 ){
111810 renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
111811 }
111812
111813 if( isLegacy==0 ){
111814 rc = renameResolveTrigger(&sParse);
111815 if( rc==SQLITE_OK0 ){
111816 renameWalkTrigger(&sWalker, pTrigger);
111817 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
111818 if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
111819 renameTokenFind(&sParse, &sCtx, pStep->zTarget);
111820 }
111821 if( pStep->pFrom ){
111822 int i;
111823 for(i=0; i<pStep->pFrom->nSrc; i++){
111824 SrcItem *pItem = &pStep->pFrom->a[i];
111825 if( 0==sqlite3_stricmp(pItem->zName, zOld) ){
111826 renameTokenFind(&sParse, &sCtx, pItem->zName);
111827 }
111828 }
111829 }
111830 }
111831 }
111832 }
111833 }
111834#endif
111835 }
111836
111837 if( rc==SQLITE_OK0 ){
111838 rc = renameEditSql(context, &sCtx, zInput, zNew, bQuote);
111839 }
111840 if( rc!=SQLITE_OK0 ){
111841 if( rc==SQLITE_ERROR1 && sqlite3WritableSchema(db) ){
111842 sqlite3_result_value(context, argv[3]);
111843 }else if( sParse.zErrMsg ){
111844 renameColumnParseError(context, "", argv[1], argv[2], &sParse);
111845 }else{
111846 sqlite3_result_error_code(context, rc);
111847 }
111848 }
111849
111850 renameParseCleanup(&sParse);
111851 renameTokenFree(db, sCtx.pList);
111852 sqlite3BtreeLeaveAll(db);
111853#ifndef SQLITE_OMIT_AUTHORIZATION
111854 db->xAuth = xAuth;
111855#endif
111856 }
111857
111858 return;
111859}
111860
111861static int renameQuotefixExprCb(Walker *pWalker, Expr *pExpr){
111862 if( pExpr->op==TK_STRING117 && (pExpr->flags & EP_DblQuoted0x000080) ){
111863 renameTokenFind(pWalker->pParse, pWalker->u.pRename, (const void*)pExpr);
111864 }
111865 return WRC_Continue0;
111866}
111867
111868/* SQL function: sqlite_rename_quotefix(DB,SQL)
111869**
111870** Rewrite the DDL statement "SQL" so that any string literals that use
111871** double-quotes use single quotes instead.
111872**
111873** Two arguments must be passed:
111874**
111875** 0: Database name ("main", "temp" etc.).
111876** 1: SQL statement to edit.
111877**
111878** The returned value is the modified SQL statement. For example, given
111879** the database schema:
111880**
111881** CREATE TABLE t1(a, b, c);
111882**
111883** SELECT sqlite_rename_quotefix('main',
111884** 'CREATE VIEW v1 AS SELECT "a", "string" FROM t1'
111885** );
111886**
111887** returns the string:
111888**
111889** CREATE VIEW v1 AS SELECT "a", 'string' FROM t1
111890**
111891** If there is a error in the input SQL, then raise an error, except
111892** if PRAGMA writable_schema=ON, then just return the input string
111893** unmodified following an error.
111894*/
111895static void renameQuotefixFunc(
111896 sqlite3_context *context,
111897 int NotUsed,
111898 sqlite3_value **argv
111899){
111900 sqlite3 *db = sqlite3_context_db_handle(context);
111901 char const *zDb = (const char*)sqlite3_value_text(argv[0]);
111902 char const *zInput = (const char*)sqlite3_value_text(argv[1]);
111903
111904#ifndef SQLITE_OMIT_AUTHORIZATION
111905 sqlite3_xauth xAuth = db->xAuth;
111906 db->xAuth = 0;
111907#endif
111908
111909 sqlite3BtreeEnterAll(db);
111910
111911 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
111912 if( zDb && zInput ){
111913 int rc;
111914 Parse sParse;
111915 rc = renameParseSql(&sParse, zDb, db, zInput, 0);
111916
111917 if( rc==SQLITE_OK0 ){
111918 RenameCtx sCtx;
111919 Walker sWalker;
111920
111921 /* Walker to find tokens that need to be replaced. */
111922 memset(&sCtx, 0, sizeof(RenameCtx));
111923 memset(&sWalker, 0, sizeof(Walker));
111924 sWalker.pParse = &sParse;
111925 sWalker.xExprCallback = renameQuotefixExprCb;
111926 sWalker.xSelectCallback = renameColumnSelectCb;
111927 sWalker.u.pRename = &sCtx;
111928
111929 if( sParse.pNewTable ){
111930 if( IsView(sParse.pNewTable)((sParse.pNewTable)->eTabType==2) ){
111931 Select *pSelect = sParse.pNewTable->u.view.pSelect;
111932 pSelect->selFlags &= ~SF_View0x0200000;
111933 sParse.rc = SQLITE_OK0;
111934 sqlite3SelectPrep(&sParse, pSelect, 0);
111935 rc = (db->mallocFailed ? SQLITE_NOMEM7 : sParse.rc);
111936 if( rc==SQLITE_OK0 ){
111937 sqlite3WalkSelect(&sWalker, pSelect);
111938 }
111939 }else{
111940 int i;
111941 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
111942#ifndef SQLITE_OMIT_GENERATED_COLUMNS
111943 for(i=0; i<sParse.pNewTable->nCol; i++){
111944 sqlite3WalkExpr(&sWalker,
111945 sqlite3ColumnExpr(sParse.pNewTable,
111946 &sParse.pNewTable->aCol[i]));
111947 }
111948#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
111949 }
111950 }else if( sParse.pNewIndex ){
111951 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
111952 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
111953 }else{
111954#ifndef SQLITE_OMIT_TRIGGER
111955 rc = renameResolveTrigger(&sParse);
111956 if( rc==SQLITE_OK0 ){
111957 renameWalkTrigger(&sWalker, sParse.pNewTrigger);
111958 }
111959#endif /* SQLITE_OMIT_TRIGGER */
111960 }
111961
111962 if( rc==SQLITE_OK0 ){
111963 rc = renameEditSql(context, &sCtx, zInput, 0, 0);
111964 }
111965 renameTokenFree(db, sCtx.pList);
111966 }
111967 if( rc!=SQLITE_OK0 ){
111968 if( sqlite3WritableSchema(db) && rc==SQLITE_ERROR1 ){
111969 sqlite3_result_value(context, argv[1]);
111970 }else{
111971 sqlite3_result_error_code(context, rc);
111972 }
111973 }
111974 renameParseCleanup(&sParse);
111975 }
111976
111977#ifndef SQLITE_OMIT_AUTHORIZATION
111978 db->xAuth = xAuth;
111979#endif
111980
111981 sqlite3BtreeLeaveAll(db);
111982}
111983
111984/* Function: sqlite_rename_test(DB,SQL,TYPE,NAME,ISTEMP,WHEN,DQS)
111985**
111986** An SQL user function that checks that there are no parse or symbol
111987** resolution problems in a CREATE TRIGGER|TABLE|VIEW|INDEX statement.
111988** After an ALTER TABLE .. RENAME operation is performed and the schema
111989** reloaded, this function is called on each SQL statement in the schema
111990** to ensure that it is still usable.
111991**
111992** 0: Database name ("main", "temp" etc.).
111993** 1: SQL statement.
111994** 2: Object type ("view", "table", "trigger" or "index").
111995** 3: Object name.
111996** 4: True if object is from temp schema.
111997** 5: "when" part of error message.
111998** 6: True to disable the DQS quirk when parsing SQL.
111999**
112000** The return value is computed as follows:
112001**
112002** A. If an error is seen and not in PRAGMA writable_schema=ON mode,
112003** then raise the error.
112004** B. Else if a trigger is created and the the table that the trigger is
112005** attached to is in database zDb, then return 1.
112006** C. Otherwise return NULL.
112007*/
112008static void renameTableTest(
112009 sqlite3_context *context,
112010 int NotUsed,
112011 sqlite3_value **argv
112012){
112013 sqlite3 *db = sqlite3_context_db_handle(context);
112014 char const *zDb = (const char*)sqlite3_value_text(argv[0]);
112015 char const *zInput = (const char*)sqlite3_value_text(argv[1]);
112016 int bTemp = sqlite3_value_int(argv[4]);
112017 int isLegacy = (db->flags & SQLITE_LegacyAlter0x04000000);
112018 char const *zWhen = (const char*)sqlite3_value_text(argv[5]);
112019 int bNoDQS = sqlite3_value_int(argv[6]);
112020
112021#ifndef SQLITE_OMIT_AUTHORIZATION
112022 sqlite3_xauth xAuth = db->xAuth;
112023 db->xAuth = 0;
112024#endif
112025
112026 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
112027
112028 if( zDb && zInput ){
112029 int rc;
112030 Parse sParse;
112031 int flags = db->flags;
112032 if( bNoDQS ) db->flags &= ~(SQLITE_DqsDML0x40000000|SQLITE_DqsDDL0x20000000);
112033 rc = renameParseSql(&sParse, zDb, db, zInput, bTemp);
112034 db->flags |= (flags & (SQLITE_DqsDML0x40000000|SQLITE_DqsDDL0x20000000));
112035 if( rc==SQLITE_OK0 ){
112036 if( isLegacy==0 && sParse.pNewTable && IsView(sParse.pNewTable)((sParse.pNewTable)->eTabType==2) ){
112037 NameContext sNC;
112038 memset(&sNC, 0, sizeof(sNC));
112039 sNC.pParse = &sParse;
112040 sqlite3SelectPrep(&sParse, sParse.pNewTable->u.view.pSelect, &sNC);
112041 if( sParse.nErr ) rc = sParse.rc;
112042 }
112043
112044 else if( sParse.pNewTrigger ){
112045 if( isLegacy==0 ){
112046 rc = renameResolveTrigger(&sParse);
112047 }
112048 if( rc==SQLITE_OK0 ){
112049 int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
112050 int i2 = sqlite3FindDbName(db, zDb);
112051 if( i1==i2 ){
112052 /* Handle output case B */
112053 sqlite3_result_int(context, 1);
112054 }
112055 }
112056 }
112057 }
112058
112059 if( rc!=SQLITE_OK0 && zWhen && !sqlite3WritableSchema(db) ){
112060 /* Output case A */
112061 renameColumnParseError(context, zWhen, argv[2], argv[3],&sParse);
112062 }
112063 renameParseCleanup(&sParse);
112064 }
112065
112066#ifndef SQLITE_OMIT_AUTHORIZATION
112067 db->xAuth = xAuth;
112068#endif
112069}
112070
112071/*
112072** The implementation of internal UDF sqlite_drop_column().
112073**
112074** Arguments:
112075**
112076** argv[0]: An integer - the index of the schema containing the table
112077** argv[1]: CREATE TABLE statement to modify.
112078** argv[2]: An integer - the index of the column to remove.
112079**
112080** The value returned is a string containing the CREATE TABLE statement
112081** with column argv[2] removed.
112082*/
112083static void dropColumnFunc(
112084 sqlite3_context *context,
112085 int NotUsed,
112086 sqlite3_value **argv
112087){
112088 sqlite3 *db = sqlite3_context_db_handle(context);
112089 int iSchema = sqlite3_value_int(argv[0]);
112090 const char *zSql = (const char*)sqlite3_value_text(argv[1]);
112091 int iCol = sqlite3_value_int(argv[2]);
112092 const char *zDb = db->aDb[iSchema].zDbSName;
112093 int rc;
112094 Parse sParse;
112095 RenameToken *pCol;
112096 Table *pTab;
112097 const char *zEnd;
112098 char *zNew = 0;
112099
112100#ifndef SQLITE_OMIT_AUTHORIZATION
112101 sqlite3_xauth xAuth = db->xAuth;
112102 db->xAuth = 0;
112103#endif
112104
112105 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
112106 rc = renameParseSql(&sParse, zDb, db, zSql, iSchema==1);
112107 if( rc!=SQLITE_OK0 ) goto drop_column_done;
112108 pTab = sParse.pNewTable;
112109 if( pTab==0 || pTab->nCol==1 || iCol>=pTab->nCol ){
112110 /* This can happen if the sqlite_schema table is corrupt */
112111 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(112111);
112112 goto drop_column_done;
112113 }
112114
112115 pCol = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol].zCnName);
112116 if( iCol<pTab->nCol-1 ){
112117 RenameToken *pEnd;
112118 pEnd = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol+1].zCnName);
112119 zEnd = (const char*)pEnd->t.z;
112120 }else{
112121 assert( IsOrdinaryTable(pTab) )((void) (0));
112122 zEnd = (const char*)&zSql[pTab->u.tab.addColOffset];
112123 while( ALWAYS(pCol->t.z[0]!=0)(pCol->t.z[0]!=0) && pCol->t.z[0]!=',' ) pCol->t.z--;
112124 }
112125
112126 zNew = sqlite3MPrintf(db, "%.*s%s", pCol->t.z-zSql, zSql, zEnd);
112127 sqlite3_result_text(context, zNew, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
112128 sqlite3_free(zNew);
112129
112130drop_column_done:
112131 renameParseCleanup(&sParse);
112132#ifndef SQLITE_OMIT_AUTHORIZATION
112133 db->xAuth = xAuth;
112134#endif
112135 if( rc!=SQLITE_OK0 ){
112136 sqlite3_result_error_code(context, rc);
112137 }
112138}
112139
112140/*
112141** This function is called by the parser upon parsing an
112142**
112143** ALTER TABLE pSrc DROP COLUMN pName
112144**
112145** statement. Argument pSrc contains the possibly qualified name of the
112146** table being edited, and token pName the name of the column to drop.
112147*/
112148SQLITE_PRIVATEstatic void sqlite3AlterDropColumn(Parse *pParse, SrcList *pSrc, const Token *pName){
112149 sqlite3 *db = pParse->db; /* Database handle */
112150 Table *pTab; /* Table to modify */
112151 int iDb; /* Index of db containing pTab in aDb[] */
112152 const char *zDb; /* Database containing pTab ("main" etc.) */
112153 char *zCol = 0; /* Name of column to drop */
112154 int iCol; /* Index of column zCol in pTab->aCol[] */
112155
112156 /* Look up the table being altered. */
112157 assert( pParse->pNewTable==0 )((void) (0));
112158 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
112159 if( NEVER(db->mallocFailed)(db->mallocFailed) ) goto exit_drop_column;
112160 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
112161 if( !pTab ) goto exit_drop_column;
112162
112163 /* Make sure this is not an attempt to ALTER a view, virtual table or
112164 ** system table. */
112165 if( SQLITE_OK0!=isAlterableTable(pParse, pTab) ) goto exit_drop_column;
112166 if( SQLITE_OK0!=isRealTable(pParse, pTab, 1) ) goto exit_drop_column;
112167
112168 /* Find the index of the column being dropped. */
112169 zCol = sqlite3NameFromToken(db, pName);
112170 if( zCol==0 ){
112171 assert( db->mallocFailed )((void) (0));
112172 goto exit_drop_column;
112173 }
112174 iCol = sqlite3ColumnIndex(pTab, zCol);
112175 if( iCol<0 ){
112176 sqlite3ErrorMsg(pParse, "no such column: \"%T\"", pName);
112177 goto exit_drop_column;
112178 }
112179
112180 /* Do not allow the user to drop a PRIMARY KEY column or a column
112181 ** constrained by a UNIQUE constraint. */
112182 if( pTab->aCol[iCol].colFlags & (COLFLAG_PRIMKEY0x0001|COLFLAG_UNIQUE0x0008) ){
112183 sqlite3ErrorMsg(pParse, "cannot drop %s column: \"%s\"",
112184 (pTab->aCol[iCol].colFlags&COLFLAG_PRIMKEY0x0001) ? "PRIMARY KEY" : "UNIQUE",
112185 zCol
112186 );
112187 goto exit_drop_column;
112188 }
112189
112190 /* Do not allow the number of columns to go to zero */
112191 if( pTab->nCol<=1 ){
112192 sqlite3ErrorMsg(pParse, "cannot drop column \"%s\": no other columns exist",zCol);
112193 goto exit_drop_column;
112194 }
112195
112196 /* Edit the sqlite_schema table */
112197 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
112198 assert( iDb>=0 )((void) (0));
112199 zDb = db->aDb[iDb].zDbSName;
112200#ifndef SQLITE_OMIT_AUTHORIZATION
112201 /* Invoke the authorization callback. */
112202 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE26, zDb, pTab->zName, zCol) ){
112203 goto exit_drop_column;
112204 }
112205#endif
112206 renameTestSchema(pParse, zDb, iDb==1, "", 0);
112207 renameFixQuotes(pParse, zDb, iDb==1);
112208 sqlite3NestedParse(pParse,
112209 "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE"sqlite_master" " SET "
112210 "sql = sqlite_drop_column(%d, sql, %d) "
112211 "WHERE (type=='table' AND tbl_name=%Q COLLATE nocase)"
112212 , zDb, iDb, iCol, pTab->zName
112213 );
112214
112215 /* Drop and reload the database schema. */
112216 renameReloadSchema(pParse, iDb, INITFLAG_AlterDrop0x0002);
112217 renameTestSchema(pParse, zDb, iDb==1, "after drop column", 1);
112218
112219 /* Edit rows of table on disk */
112220 if( pParse->nErr==0 && (pTab->aCol[iCol].colFlags & COLFLAG_VIRTUAL0x0020)==0 ){
112221 int i;
112222 int addr;
112223 int reg;
112224 int regRec;
112225 Index *pPk = 0;
112226 int nField = 0; /* Number of non-virtual columns after drop */
112227 int iCur;
112228 Vdbe *v = sqlite3GetVdbe(pParse);
112229 iCur = pParse->nTab++;
112230 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite113);
112231 addr = sqlite3VdbeAddOp1(v, OP_Rewind35, iCur); VdbeCoverage(v);
112232 reg = ++pParse->nMem;
112233 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
112234 sqlite3VdbeAddOp2(v, OP_Rowid135, iCur, reg);
112235 pParse->nMem += pTab->nCol;
112236 }else{
112237 pPk = sqlite3PrimaryKeyIndex(pTab);
112238 pParse->nMem += pPk->nColumn;
112239 for(i=0; i<pPk->nKeyCol; i++){
112240 sqlite3VdbeAddOp3(v, OP_Column94, iCur, i, reg+i+1);
112241 }
112242 nField = pPk->nKeyCol;
112243 }
112244 regRec = ++pParse->nMem;
112245 for(i=0; i<pTab->nCol; i++){
112246 if( i!=iCol && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020)==0 ){
112247 int regOut;
112248 if( pPk ){
112249 int iPos = sqlite3TableColumnToIndex(pPk, i);
112250 int iColPos = sqlite3TableColumnToIndex(pPk, iCol);
112251 if( iPos<pPk->nKeyCol ) continue;
112252 regOut = reg+1+iPos-(iPos>iColPos);
112253 }else{
112254 regOut = reg+1+nField;
112255 }
112256 if( i==pTab->iPKey ){
112257 sqlite3VdbeAddOp2(v, OP_Null75, 0, regOut);
112258 }else{
112259 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, i, regOut);
112260 }
112261 nField++;
112262 }
112263 }
112264 if( nField==0 ){
112265 /* dbsqlfuzz 5f09e7bcc78b4954d06bf9f2400d7715f48d1fef */
112266 pParse->nMem++;
112267 sqlite3VdbeAddOp2(v, OP_Null75, 0, reg+1);
112268 nField = 1;
112269 }
112270 sqlite3VdbeAddOp3(v, OP_MakeRecord97, reg+1, nField, regRec);
112271 if( pPk ){
112272 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iCur, regRec, reg+1, pPk->nKeyCol);
112273 }else{
112274 sqlite3VdbeAddOp3(v, OP_Insert128, iCur, regRec, reg);
112275 }
112276 sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION0x02);
112277
112278 sqlite3VdbeAddOp2(v, OP_Next38, iCur, addr+1); VdbeCoverage(v);
112279 sqlite3VdbeJumpHere(v, addr);
112280 }
112281
112282exit_drop_column:
112283 sqlite3DbFree(db, zCol);
112284 sqlite3SrcListDelete(db, pSrc);
112285}
112286
112287/*
112288** Register built-in functions used to help implement ALTER TABLE
112289*/
112290SQLITE_PRIVATEstatic void sqlite3AlterFunctions(void){
112291 static FuncDef aAlterTableFuncs[] = {
112292 INTERNAL_FUNCTION(sqlite_rename_column, 9, renameColumnFunc){9, 0x00800000| 0x00040000|1|0x0800, 0, 0, renameColumnFunc, 0
, 0, 0, "sqlite_rename_column", {0} }
,
112293 INTERNAL_FUNCTION(sqlite_rename_table, 7, renameTableFunc){7, 0x00800000| 0x00040000|1|0x0800, 0, 0, renameTableFunc, 0
, 0, 0, "sqlite_rename_table", {0} }
,
112294 INTERNAL_FUNCTION(sqlite_rename_test, 7, renameTableTest){7, 0x00800000| 0x00040000|1|0x0800, 0, 0, renameTableTest, 0
, 0, 0, "sqlite_rename_test", {0} }
,
112295 INTERNAL_FUNCTION(sqlite_drop_column, 3, dropColumnFunc){3, 0x00800000| 0x00040000|1|0x0800, 0, 0, dropColumnFunc, 0,
0, 0, "sqlite_drop_column", {0} }
,
112296 INTERNAL_FUNCTION(sqlite_rename_quotefix,2, renameQuotefixFunc){2, 0x00800000| 0x00040000|1|0x0800, 0, 0, renameQuotefixFunc
, 0, 0, 0, "sqlite_rename_quotefix", {0} }
,
112297 };
112298 sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs)((int)(sizeof(aAlterTableFuncs)/sizeof(aAlterTableFuncs[0]))));
112299}
112300#endif /* SQLITE_ALTER_TABLE */
112301
112302/************** End of alter.c ***********************************************/
112303/************** Begin file analyze.c *****************************************/
112304/*
112305** 2005-07-08
112306**
112307** The author disclaims copyright to this source code. In place of
112308** a legal notice, here is a blessing:
112309**
112310** May you do good and not evil.
112311** May you find forgiveness for yourself and forgive others.
112312** May you share freely, never taking more than you give.
112313**
112314*************************************************************************
112315** This file contains code associated with the ANALYZE command.
112316**
112317** The ANALYZE command gather statistics about the content of tables
112318** and indices. These statistics are made available to the query planner
112319** to help it make better decisions about how to perform queries.
112320**
112321** The following system tables are or have been supported:
112322**
112323** CREATE TABLE sqlite_stat1(tbl, idx, stat);
112324** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
112325** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
112326** CREATE TABLE sqlite_stat4(tbl, idx, nEq, nLt, nDLt, sample);
112327**
112328** Additional tables might be added in future releases of SQLite.
112329** The sqlite_stat2 table is not created or used unless the SQLite version
112330** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
112331** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated.
112332** The sqlite_stat2 table is superseded by sqlite_stat3, which is only
112333** created and used by SQLite versions 3.7.9 through 3.29.0 when
112334** SQLITE_ENABLE_STAT3 defined. The functionality of sqlite_stat3
112335** is a superset of sqlite_stat2 and is also now deprecated. The
112336** sqlite_stat4 is an enhanced version of sqlite_stat3 and is only
112337** available when compiled with SQLITE_ENABLE_STAT4 and in SQLite
112338** versions 3.8.1 and later. STAT4 is the only variant that is still
112339** supported.
112340**
112341** For most applications, sqlite_stat1 provides all the statistics required
112342** for the query planner to make good choices.
112343**
112344** Format of sqlite_stat1:
112345**
112346** There is normally one row per index, with the index identified by the
112347** name in the idx column. The tbl column is the name of the table to
112348** which the index belongs. In each such row, the stat column will be
112349** a string consisting of a list of integers. The first integer in this
112350** list is the number of rows in the index. (This is the same as the
112351** number of rows in the table, except for partial indices.) The second
112352** integer is the average number of rows in the index that have the same
112353** value in the first column of the index. The third integer is the average
112354** number of rows in the index that have the same value for the first two
112355** columns. The N-th integer (for N>1) is the average number of rows in
112356** the index which have the same value for the first N-1 columns. For
112357** a K-column index, there will be K+1 integers in the stat column. If
112358** the index is unique, then the last integer will be 1.
112359**
112360** The list of integers in the stat column can optionally be followed
112361** by the keyword "unordered". The "unordered" keyword, if it is present,
112362** must be separated from the last integer by a single space. If the
112363** "unordered" keyword is present, then the query planner assumes that
112364** the index is unordered and will not use the index for a range query.
112365**
112366** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
112367** column contains a single integer which is the (estimated) number of
112368** rows in the table identified by sqlite_stat1.tbl.
112369**
112370** Format of sqlite_stat2:
112371**
112372** The sqlite_stat2 is only created and is only used if SQLite is compiled
112373** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
112374** 3.6.18 and 3.7.8. The "stat2" table contains additional information
112375** about the distribution of keys within an index. The index is identified by
112376** the "idx" column and the "tbl" column is the name of the table to which
112377** the index belongs. There are usually 10 rows in the sqlite_stat2
112378** table for each index.
112379**
112380** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
112381** inclusive are samples of the left-most key value in the index taken at
112382** evenly spaced points along the index. Let the number of samples be S
112383** (10 in the standard build) and let C be the number of rows in the index.
112384** Then the sampled rows are given by:
112385**
112386** rownumber = (i*C*2 + C)/(S*2)
112387**
112388** For i between 0 and S-1. Conceptually, the index space is divided into
112389** S uniform buckets and the samples are the middle row from each bucket.
112390**
112391** The format for sqlite_stat2 is recorded here for legacy reference. This
112392** version of SQLite does not support sqlite_stat2. It neither reads nor
112393** writes the sqlite_stat2 table. This version of SQLite only supports
112394** sqlite_stat3.
112395**
112396** Format for sqlite_stat3:
112397**
112398** The sqlite_stat3 format is a subset of sqlite_stat4. Hence, the
112399** sqlite_stat4 format will be described first. Further information
112400** about sqlite_stat3 follows the sqlite_stat4 description.
112401**
112402** Format for sqlite_stat4:
112403**
112404** As with sqlite_stat2, the sqlite_stat4 table contains histogram data
112405** to aid the query planner in choosing good indices based on the values
112406** that indexed columns are compared against in the WHERE clauses of
112407** queries.
112408**
112409** The sqlite_stat4 table contains multiple entries for each index.
112410** The idx column names the index and the tbl column is the table of the
112411** index. If the idx and tbl columns are the same, then the sample is
112412** of the INTEGER PRIMARY KEY. The sample column is a blob which is the
112413** binary encoding of a key from the index. The nEq column is a
112414** list of integers. The first integer is the approximate number
112415** of entries in the index whose left-most column exactly matches
112416** the left-most column of the sample. The second integer in nEq
112417** is the approximate number of entries in the index where the
112418** first two columns match the first two columns of the sample.
112419** And so forth. nLt is another list of integers that show the approximate
112420** number of entries that are strictly less than the sample. The first
112421** integer in nLt contains the number of entries in the index where the
112422** left-most column is less than the left-most column of the sample.
112423** The K-th integer in the nLt entry is the number of index entries
112424** where the first K columns are less than the first K columns of the
112425** sample. The nDLt column is like nLt except that it contains the
112426** number of distinct entries in the index that are less than the
112427** sample.
112428**
112429** There can be an arbitrary number of sqlite_stat4 entries per index.
112430** The ANALYZE command will typically generate sqlite_stat4 tables
112431** that contain between 10 and 40 samples which are distributed across
112432** the key space, though not uniformly, and which include samples with
112433** large nEq values.
112434**
112435** Format for sqlite_stat3 redux:
112436**
112437** The sqlite_stat3 table is like sqlite_stat4 except that it only
112438** looks at the left-most column of the index. The sqlite_stat3.sample
112439** column contains the actual value of the left-most column instead
112440** of a blob encoding of the complete index key as is found in
112441** sqlite_stat4.sample. The nEq, nLt, and nDLt entries of sqlite_stat3
112442** all contain just a single integer which is the same as the first
112443** integer in the equivalent columns in sqlite_stat4.
112444*/
112445#ifndef SQLITE_OMIT_ANALYZE
112446/* #include "sqliteInt.h" */
112447
112448#if defined(SQLITE_ENABLE_STAT4)
112449# define IsStat40 1
112450#else
112451# define IsStat40 0
112452# undef SQLITE_STAT4_SAMPLES1
112453# define SQLITE_STAT4_SAMPLES1 1
112454#endif
112455
112456/*
112457** This routine generates code that opens the sqlite_statN tables.
112458** The sqlite_stat1 table is always relevant. sqlite_stat2 is now
112459** obsolete. sqlite_stat3 and sqlite_stat4 are only opened when
112460** appropriate compile-time options are provided.
112461**
112462** If the sqlite_statN tables do not previously exist, it is created.
112463**
112464** Argument zWhere may be a pointer to a buffer containing a table name,
112465** or it may be a NULL pointer. If it is not NULL, then all entries in
112466** the sqlite_statN tables associated with the named table are deleted.
112467** If zWhere==0, then code is generated to delete all stat table entries.
112468*/
112469static void openStatTable(
112470 Parse *pParse, /* Parsing context */
112471 int iDb, /* The database we are looking in */
112472 int iStatCur, /* Open the sqlite_stat1 table on this cursor */
112473 const char *zWhere, /* Delete entries for this table or index */
112474 const char *zWhereType /* Either "tbl" or "idx" */
112475){
112476 static const struct {
112477 const char *zName;
112478 const char *zCols;
112479 } aTable[] = {
112480 { "sqlite_stat1", "tbl,idx,stat" },
112481#if defined(SQLITE_ENABLE_STAT4)
112482 { "sqlite_stat4", "tbl,idx,neq,nlt,ndlt,sample" },
112483#else
112484 { "sqlite_stat4", 0 },
112485#endif
112486 { "sqlite_stat3", 0 },
112487 };
112488 int i;
112489 sqlite3 *db = pParse->db;
112490 Db *pDb;
112491 Vdbe *v = sqlite3GetVdbe(pParse);
112492 u32 aRoot[ArraySize(aTable)((int)(sizeof(aTable)/sizeof(aTable[0])))];
112493 u8 aCreateTbl[ArraySize(aTable)((int)(sizeof(aTable)/sizeof(aTable[0])))];
112494#ifdef SQLITE_ENABLE_STAT4
112495 const int nToOpen = OptimizationEnabled(db,SQLITE_Stat4)(((db)->dbOptFlags&(0x00000800))==0) ? 2 : 1;
112496#else
112497 const int nToOpen = 1;
112498#endif
112499
112500 if( v==0 ) return;
112501 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
112502 assert( sqlite3VdbeDb(v)==db )((void) (0));
112503 pDb = &db->aDb[iDb];
112504
112505 /* Create new statistic tables if they do not exist, or clear them
112506 ** if they do already exist.
112507 */
112508 for(i=0; i<ArraySize(aTable)((int)(sizeof(aTable)/sizeof(aTable[0]))); i++){
112509 const char *zTab = aTable[i].zName;
112510 Table *pStat;
112511 aCreateTbl[i] = 0;
112512 if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
112513 if( i<nToOpen ){
112514 /* The sqlite_statN table does not exist. Create it. Note that a
112515 ** side-effect of the CREATE TABLE statement is to leave the rootpage
112516 ** of the new table in register pParse->regRoot. This is important
112517 ** because the OpenWrite opcode below will be needing it. */
112518 sqlite3NestedParse(pParse,
112519 "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols
112520 );
112521 aRoot[i] = (u32)pParse->regRoot;
112522 aCreateTbl[i] = OPFLAG_P2ISREG0x10;
112523 }
112524 }else{
112525 /* The table already exists. If zWhere is not NULL, delete all entries
112526 ** associated with the table zWhere. If zWhere is NULL, delete the
112527 ** entire contents of the table. */
112528 aRoot[i] = pStat->tnum;
112529 sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
112530 if( zWhere ){
112531 sqlite3NestedParse(pParse,
112532 "DELETE FROM %Q.%s WHERE %s=%Q",
112533 pDb->zDbSName, zTab, zWhereType, zWhere
112534 );
112535#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
112536 }else if( db->xPreUpdateCallback ){
112537 sqlite3NestedParse(pParse, "DELETE FROM %Q.%s", pDb->zDbSName, zTab);
112538#endif
112539 }else{
112540 /* The sqlite_stat[134] table already exists. Delete all rows. */
112541 sqlite3VdbeAddOp2(v, OP_Clear145, (int)aRoot[i], iDb);
112542 }
112543 }
112544 }
112545
112546 /* Open the sqlite_stat[134] tables for writing. */
112547 for(i=0; i<nToOpen; i++){
112548 assert( i<ArraySize(aTable) )((void) (0));
112549 sqlite3VdbeAddOp4Int(v, OP_OpenWrite113, iStatCur+i, (int)aRoot[i], iDb, 3);
112550 sqlite3VdbeChangeP5(v, aCreateTbl[i]);
112551 VdbeComment((v, aTable[i].zName));
112552 }
112553}
112554
112555/*
112556** Recommended number of samples for sqlite_stat4
112557*/
112558#ifndef SQLITE_STAT4_SAMPLES1
112559# define SQLITE_STAT4_SAMPLES1 24
112560#endif
112561
112562/*
112563** Three SQL functions - stat_init(), stat_push(), and stat_get() -
112564** share an instance of the following structure to hold their state
112565** information.
112566*/
112567typedef struct StatAccum StatAccum;
112568typedef struct StatSample StatSample;
112569struct StatSample {
112570 tRowcnt *anEq; /* sqlite_stat4.nEq */
112571 tRowcnt *anDLt; /* sqlite_stat4.nDLt */
112572#ifdef SQLITE_ENABLE_STAT4
112573 tRowcnt *anLt; /* sqlite_stat4.nLt */
112574 union {
112575 i64 iRowid; /* Rowid in main table of the key */
112576 u8 *aRowid; /* Key for WITHOUT ROWID tables */
112577 } u;
112578 u32 nRowid; /* Sizeof aRowid[] */
112579 u8 isPSample; /* True if a periodic sample */
112580 int iCol; /* If !isPSample, the reason for inclusion */
112581 u32 iHash; /* Tiebreaker hash */
112582#endif
112583};
112584struct StatAccum {
112585 sqlite3 *db; /* Database connection, for malloc() */
112586 tRowcnt nEst; /* Estimated number of rows */
112587 tRowcnt nRow; /* Number of rows visited so far */
112588 int nLimit; /* Analysis row-scan limit */
112589 int nCol; /* Number of columns in index + pk/rowid */
112590 int nKeyCol; /* Number of index columns w/o the pk/rowid */
112591 u8 nSkipAhead; /* Number of times of skip-ahead */
112592 StatSample current; /* Current row as a StatSample */
112593#ifdef SQLITE_ENABLE_STAT4
112594 tRowcnt nPSample; /* How often to do a periodic sample */
112595 int mxSample; /* Maximum number of samples to accumulate */
112596 u32 iPrn; /* Pseudo-random number used for sampling */
112597 StatSample *aBest; /* Array of nCol best samples */
112598 int iMin; /* Index in a[] of entry with minimum score */
112599 int nSample; /* Current number of samples */
112600 int nMaxEqZero; /* Max leading 0 in anEq[] for any a[] entry */
112601 int iGet; /* Index of current sample accessed by stat_get() */
112602 StatSample *a; /* Array of mxSample StatSample objects */
112603#endif
112604};
112605
112606/* Reclaim memory used by a StatSample
112607*/
112608#ifdef SQLITE_ENABLE_STAT4
112609static void sampleClear(sqlite3 *db, StatSample *p){
112610 assert( db!=0 )((void) (0));
112611 if( p->nRowid ){
112612 sqlite3DbFree(db, p->u.aRowid);
112613 p->nRowid = 0;
112614 }
112615}
112616#endif
112617
112618/* Initialize the BLOB value of a ROWID
112619*/
112620#ifdef SQLITE_ENABLE_STAT4
112621static void sampleSetRowid(sqlite3 *db, StatSample *p, int n, const u8 *pData){
112622 assert( db!=0 )((void) (0));
112623 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
112624 p->u.aRowid = sqlite3DbMallocRawNN(db, n);
112625 if( p->u.aRowid ){
112626 p->nRowid = n;
112627 memcpy(p->u.aRowid, pData, n);
112628 }else{
112629 p->nRowid = 0;
112630 }
112631}
112632#endif
112633
112634/* Initialize the INTEGER value of a ROWID.
112635*/
112636#ifdef SQLITE_ENABLE_STAT4
112637static void sampleSetRowidInt64(sqlite3 *db, StatSample *p, i64 iRowid){
112638 assert( db!=0 )((void) (0));
112639 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
112640 p->nRowid = 0;
112641 p->u.iRowid = iRowid;
112642}
112643#endif
112644
112645
112646/*
112647** Copy the contents of object (*pFrom) into (*pTo).
112648*/
112649#ifdef SQLITE_ENABLE_STAT4
112650static void sampleCopy(StatAccum *p, StatSample *pTo, StatSample *pFrom){
112651 pTo->isPSample = pFrom->isPSample;
112652 pTo->iCol = pFrom->iCol;
112653 pTo->iHash = pFrom->iHash;
112654 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
112655 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
112656 memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
112657 if( pFrom->nRowid ){
112658 sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
112659 }else{
112660 sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
112661 }
112662}
112663#endif
112664
112665/*
112666** Reclaim all memory of a StatAccum structure.
112667*/
112668static void statAccumDestructor(void *pOld){
112669 StatAccum *p = (StatAccum*)pOld;
112670#ifdef SQLITE_ENABLE_STAT4
112671 if( p->mxSample ){
112672 int i;
112673 for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
112674 for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
112675 sampleClear(p->db, &p->current);
112676 }
112677#endif
112678 sqlite3DbFree(p->db, p);
112679}
112680
112681/*
112682** Implementation of the stat_init(N,K,C,L) SQL function. The four parameters
112683** are:
112684** N: The number of columns in the index including the rowid/pk (note 1)
112685** K: The number of columns in the index excluding the rowid/pk.
112686** C: Estimated number of rows in the index
112687** L: A limit on the number of rows to scan, or 0 for no-limit
112688**
112689** Note 1: In the special case of the covering index that implements a
112690** WITHOUT ROWID table, N is the number of PRIMARY KEY columns, not the
112691** total number of columns in the table.
112692**
112693** For indexes on ordinary rowid tables, N==K+1. But for indexes on
112694** WITHOUT ROWID tables, N=K+P where P is the number of columns in the
112695** PRIMARY KEY of the table. The covering index that implements the
112696** original WITHOUT ROWID table as N==K as a special case.
112697**
112698** This routine allocates the StatAccum object in heap memory. The return
112699** value is a pointer to the StatAccum object. The datatype of the
112700** return value is BLOB, but it is really just a pointer to the StatAccum
112701** object.
112702*/
112703static void statInit(
112704 sqlite3_context *context,
112705 int argc,
112706 sqlite3_value **argv
112707){
112708 StatAccum *p;
112709 int nCol; /* Number of columns in index being sampled */
112710 int nKeyCol; /* Number of key columns */
112711 int nColUp; /* nCol rounded up for alignment */
112712 int n; /* Bytes of space to allocate */
112713 sqlite3 *db = sqlite3_context_db_handle(context); /* Database connection */
112714#ifdef SQLITE_ENABLE_STAT4
112715 /* Maximum number of samples. 0 if STAT4 data is not collected */
112716 int mxSample = OptimizationEnabled(db,SQLITE_Stat4)(((db)->dbOptFlags&(0x00000800))==0) ?SQLITE_STAT4_SAMPLES1 :0;
112717#endif
112718
112719 /* Decode the three function arguments */
112720 UNUSED_PARAMETER(argc)(void)(argc);
112721 nCol = sqlite3_value_int(argv[0]);
112722 assert( nCol>0 )((void) (0));
112723 nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
112724 nKeyCol = sqlite3_value_int(argv[1]);
112725 assert( nKeyCol<=nCol )((void) (0));
112726 assert( nKeyCol>0 )((void) (0));
112727
112728 /* Allocate the space required for the StatAccum object */
112729 n = sizeof(*p)
112730 + sizeof(tRowcnt)*nColUp /* StatAccum.anEq */
112731 + sizeof(tRowcnt)*nColUp; /* StatAccum.anDLt */
112732#ifdef SQLITE_ENABLE_STAT4
112733 if( mxSample ){
112734 n += sizeof(tRowcnt)*nColUp /* StatAccum.anLt */
112735 + sizeof(StatSample)*(nCol+mxSample) /* StatAccum.aBest[], a[] */
112736 + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample);
112737 }
112738#endif
112739 p = sqlite3DbMallocZero(db, n);
112740 if( p==0 ){
112741 sqlite3_result_error_nomem(context);
112742 return;
112743 }
112744
112745 p->db = db;
112746 p->nEst = sqlite3_value_int64(argv[2]);
112747 p->nRow = 0;
112748 p->nLimit = sqlite3_value_int64(argv[3]);
112749 p->nCol = nCol;
112750 p->nKeyCol = nKeyCol;
112751 p->nSkipAhead = 0;
112752 p->current.anDLt = (tRowcnt*)&p[1];
112753 p->current.anEq = &p->current.anDLt[nColUp];
112754
112755#ifdef SQLITE_ENABLE_STAT4
112756 p->mxSample = p->nLimit==0 ? mxSample : 0;
112757 if( mxSample ){
112758 u8 *pSpace; /* Allocated space not yet assigned */
112759 int i; /* Used to iterate through p->aSample[] */
112760
112761 p->iGet = -1;
112762 p->nPSample = (tRowcnt)(p->nEst/(mxSample/3+1) + 1);
112763 p->current.anLt = &p->current.anEq[nColUp];
112764 p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
112765
112766 /* Set up the StatAccum.a[] and aBest[] arrays */
112767 p->a = (struct StatSample*)&p->current.anLt[nColUp];
112768 p->aBest = &p->a[mxSample];
112769 pSpace = (u8*)(&p->a[mxSample+nCol]);
112770 for(i=0; i<(mxSample+nCol); i++){
112771 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
112772 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
112773 p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
112774 }
112775 assert( (pSpace - (u8*)p)==n )((void) (0));
112776
112777 for(i=0; i<nCol; i++){
112778 p->aBest[i].iCol = i;
112779 }
112780 }
112781#endif
112782
112783 /* Return a pointer to the allocated object to the caller. Note that
112784 ** only the pointer (the 2nd parameter) matters. The size of the object
112785 ** (given by the 3rd parameter) is never used and can be any positive
112786 ** value. */
112787 sqlite3_result_blob(context, p, sizeof(*p), statAccumDestructor);
112788}
112789static const FuncDef statInitFuncdef = {
112790 4, /* nArg */
112791 SQLITE_UTF81, /* funcFlags */
112792 0, /* pUserData */
112793 0, /* pNext */
112794 statInit, /* xSFunc */
112795 0, /* xFinalize */
112796 0, 0, /* xValue, xInverse */
112797 "stat_init", /* zName */
112798 {0}
112799};
112800
112801#ifdef SQLITE_ENABLE_STAT4
112802/*
112803** pNew and pOld are both candidate non-periodic samples selected for
112804** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
112805** considering only any trailing columns and the sample hash value, this
112806** function returns true if sample pNew is to be preferred over pOld.
112807** In other words, if we assume that the cardinalities of the selected
112808** column for pNew and pOld are equal, is pNew to be preferred over pOld.
112809**
112810** This function assumes that for each argument sample, the contents of
112811** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
112812*/
112813static int sampleIsBetterPost(
112814 StatAccum *pAccum,
112815 StatSample *pNew,
112816 StatSample *pOld
112817){
112818 int nCol = pAccum->nCol;
112819 int i;
112820 assert( pNew->iCol==pOld->iCol )((void) (0));
112821 for(i=pNew->iCol+1; i<nCol; i++){
112822 if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
112823 if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
112824 }
112825 if( pNew->iHash>pOld->iHash ) return 1;
112826 return 0;
112827}
112828#endif
112829
112830#ifdef SQLITE_ENABLE_STAT4
112831/*
112832** Return true if pNew is to be preferred over pOld.
112833**
112834** This function assumes that for each argument sample, the contents of
112835** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
112836*/
112837static int sampleIsBetter(
112838 StatAccum *pAccum,
112839 StatSample *pNew,
112840 StatSample *pOld
112841){
112842 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
112843 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
112844
112845 assert( pOld->isPSample==0 && pNew->isPSample==0 )((void) (0));
112846 assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) )((void) (0));
112847
112848 if( (nEqNew>nEqOld) ) return 1;
112849 if( nEqNew==nEqOld ){
112850 if( pNew->iCol<pOld->iCol ) return 1;
112851 return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
112852 }
112853 return 0;
112854}
112855
112856/*
112857** Copy the contents of sample *pNew into the p->a[] array. If necessary,
112858** remove the least desirable sample from p->a[] to make room.
112859*/
112860static void sampleInsert(StatAccum *p, StatSample *pNew, int nEqZero){
112861 StatSample *pSample = 0;
112862 int i;
112863
112864 assert( IsStat4 || nEqZero==0 )((void) (0));
112865
112866 /* StatAccum.nMaxEqZero is set to the maximum number of leading 0
112867 ** values in the anEq[] array of any sample in StatAccum.a[]. In
112868 ** other words, if nMaxEqZero is n, then it is guaranteed that there
112869 ** are no samples with StatSample.anEq[m]==0 for (m>=n). */
112870 if( nEqZero>p->nMaxEqZero ){
112871 p->nMaxEqZero = nEqZero;
112872 }
112873 if( pNew->isPSample==0 ){
112874 StatSample *pUpgrade = 0;
112875 assert( pNew->anEq[pNew->iCol]>0 )((void) (0));
112876
112877 /* This sample is being added because the prefix that ends in column
112878 ** iCol occurs many times in the table. However, if we have already
112879 ** added a sample that shares this prefix, there is no need to add
112880 ** this one. Instead, upgrade the priority of the highest priority
112881 ** existing sample that shares this prefix. */
112882 for(i=p->nSample-1; i>=0; i--){
112883 StatSample *pOld = &p->a[i];
112884 if( pOld->anEq[pNew->iCol]==0 ){
112885 if( pOld->isPSample ) return;
112886 assert( pOld->iCol>pNew->iCol )((void) (0));
112887 assert( sampleIsBetter(p, pNew, pOld) )((void) (0));
112888 if( pUpgrade==0 || sampleIsBetter(p, pOld, pUpgrade) ){
112889 pUpgrade = pOld;
112890 }
112891 }
112892 }
112893 if( pUpgrade ){
112894 pUpgrade->iCol = pNew->iCol;
112895 pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
112896 goto find_new_min;
112897 }
112898 }
112899
112900 /* If necessary, remove sample iMin to make room for the new sample. */
112901 if( p->nSample>=p->mxSample ){
112902 StatSample *pMin = &p->a[p->iMin];
112903 tRowcnt *anEq = pMin->anEq;
112904 tRowcnt *anLt = pMin->anLt;
112905 tRowcnt *anDLt = pMin->anDLt;
112906 sampleClear(p->db, pMin);
112907 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
112908 pSample = &p->a[p->nSample-1];
112909 pSample->nRowid = 0;
112910 pSample->anEq = anEq;
112911 pSample->anDLt = anDLt;
112912 pSample->anLt = anLt;
112913 p->nSample = p->mxSample-1;
112914 }
112915
112916 /* The "rows less-than" for the rowid column must be greater than that
112917 ** for the last sample in the p->a[] array. Otherwise, the samples would
112918 ** be out of order. */
112919 assert( p->nSample==0((void) (0))
112920 || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] )((void) (0));
112921
112922 /* Insert the new sample */
112923 pSample = &p->a[p->nSample];
112924 sampleCopy(p, pSample, pNew);
112925 p->nSample++;
112926
112927 /* Zero the first nEqZero entries in the anEq[] array. */
112928 memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
112929
112930find_new_min:
112931 if( p->nSample>=p->mxSample ){
112932 int iMin = -1;
112933 for(i=0; i<p->mxSample; i++){
112934 if( p->a[i].isPSample ) continue;
112935 if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
112936 iMin = i;
112937 }
112938 }
112939 assert( iMin>=0 )((void) (0));
112940 p->iMin = iMin;
112941 }
112942}
112943#endif /* SQLITE_ENABLE_STAT4 */
112944
112945#ifdef SQLITE_ENABLE_STAT4
112946/*
112947** Field iChng of the index being scanned has changed. So at this point
112948** p->current contains a sample that reflects the previous row of the
112949** index. The value of anEq[iChng] and subsequent anEq[] elements are
112950** correct at this point.
112951*/
112952static void samplePushPrevious(StatAccum *p, int iChng){
112953 int i;
112954
112955 /* Check if any samples from the aBest[] array should be pushed
112956 ** into IndexSample.a[] at this point. */
112957 for(i=(p->nCol-2); i>=iChng; i--){
112958 StatSample *pBest = &p->aBest[i];
112959 pBest->anEq[i] = p->current.anEq[i];
112960 if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
112961 sampleInsert(p, pBest, i);
112962 }
112963 }
112964
112965 /* Check that no sample contains an anEq[] entry with an index of
112966 ** p->nMaxEqZero or greater set to zero. */
112967 for(i=p->nSample-1; i>=0; i--){
112968 int j;
112969 for(j=p->nMaxEqZero; j<p->nCol; j++) assert( p->a[i].anEq[j]>0 )((void) (0));
112970 }
112971
112972 /* Update the anEq[] fields of any samples already collected. */
112973 if( iChng<p->nMaxEqZero ){
112974 for(i=p->nSample-1; i>=0; i--){
112975 int j;
112976 for(j=iChng; j<p->nCol; j++){
112977 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
112978 }
112979 }
112980 p->nMaxEqZero = iChng;
112981 }
112982}
112983#endif /* SQLITE_ENABLE_STAT4 */
112984
112985/*
112986** Implementation of the stat_push SQL function: stat_push(P,C,R)
112987** Arguments:
112988**
112989** P Pointer to the StatAccum object created by stat_init()
112990** C Index of left-most column to differ from previous row
112991** R Rowid for the current row. Might be a key record for
112992** WITHOUT ROWID tables.
112993**
112994** The purpose of this routine is to collect statistical data and/or
112995** samples from the index being analyzed into the StatAccum object.
112996** The stat_get() SQL function will be used afterwards to
112997** retrieve the information gathered.
112998**
112999** This SQL function usually returns NULL, but might return an integer
113000** if it wants the byte-code to do special processing.
113001**
113002** The R parameter is only used for STAT4
113003*/
113004static void statPush(
113005 sqlite3_context *context,
113006 int argc,
113007 sqlite3_value **argv
113008){
113009 int i;
113010
113011 /* The three function arguments */
113012 StatAccum *p = (StatAccum*)sqlite3_value_blob(argv[0]);
113013 int iChng = sqlite3_value_int(argv[1]);
113014
113015 UNUSED_PARAMETER( argc )(void)(argc);
113016 UNUSED_PARAMETER( context )(void)(context);
113017 assert( p->nCol>0 )((void) (0));
113018 assert( iChng<p->nCol )((void) (0));
113019
113020 if( p->nRow==0 ){
113021 /* This is the first call to this function. Do initialization. */
113022 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
113023 }else{
113024 /* Second and subsequent calls get processed here */
113025#ifdef SQLITE_ENABLE_STAT4
113026 if( p->mxSample ) samplePushPrevious(p, iChng);
113027#endif
113028
113029 /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
113030 ** to the current row of the index. */
113031 for(i=0; i<iChng; i++){
113032 p->current.anEq[i]++;
113033 }
113034 for(i=iChng; i<p->nCol; i++){
113035 p->current.anDLt[i]++;
113036#ifdef SQLITE_ENABLE_STAT4
113037 if( p->mxSample ) p->current.anLt[i] += p->current.anEq[i];
113038#endif
113039 p->current.anEq[i] = 1;
113040 }
113041 }
113042
113043 p->nRow++;
113044#ifdef SQLITE_ENABLE_STAT4
113045 if( p->mxSample ){
113046 tRowcnt nLt;
113047 if( sqlite3_value_type(argv[2])==SQLITE_INTEGER1 ){
113048 sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
113049 }else{
113050 sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
113051 sqlite3_value_blob(argv[2]));
113052 }
113053 p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
113054
113055 nLt = p->current.anLt[p->nCol-1];
113056 /* Check if this is to be a periodic sample. If so, add it. */
113057 if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
113058 p->current.isPSample = 1;
113059 p->current.iCol = 0;
113060 sampleInsert(p, &p->current, p->nCol-1);
113061 p->current.isPSample = 0;
113062 }
113063
113064 /* Update the aBest[] array. */
113065 for(i=0; i<(p->nCol-1); i++){
113066 p->current.iCol = i;
113067 if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
113068 sampleCopy(p, &p->aBest[i], &p->current);
113069 }
113070 }
113071 }else
113072#endif
113073 if( p->nLimit && p->nRow>(tRowcnt)p->nLimit*(p->nSkipAhead+1) ){
113074 p->nSkipAhead++;
113075 sqlite3_result_int(context, p->current.anDLt[0]>0);
113076 }
113077}
113078
113079static const FuncDef statPushFuncdef = {
113080 2+IsStat40, /* nArg */
113081 SQLITE_UTF81, /* funcFlags */
113082 0, /* pUserData */
113083 0, /* pNext */
113084 statPush, /* xSFunc */
113085 0, /* xFinalize */
113086 0, 0, /* xValue, xInverse */
113087 "stat_push", /* zName */
113088 {0}
113089};
113090
113091#define STAT_GET_STAT10 0 /* "stat" column of stat1 table */
113092#define STAT_GET_ROWID1 1 /* "rowid" column of stat[34] entry */
113093#define STAT_GET_NEQ2 2 /* "neq" column of stat[34] entry */
113094#define STAT_GET_NLT3 3 /* "nlt" column of stat[34] entry */
113095#define STAT_GET_NDLT4 4 /* "ndlt" column of stat[34] entry */
113096
113097/*
113098** Implementation of the stat_get(P,J) SQL function. This routine is
113099** used to query statistical information that has been gathered into
113100** the StatAccum object by prior calls to stat_push(). The P parameter
113101** has type BLOB but it is really just a pointer to the StatAccum object.
113102** The content to returned is determined by the parameter J
113103** which is one of the STAT_GET_xxxx values defined above.
113104**
113105** The stat_get(P,J) function is not available to generic SQL. It is
113106** inserted as part of a manually constructed bytecode program. (See
113107** the callStatGet() routine below.) It is guaranteed that the P
113108** parameter will always be a pointer to a StatAccum object, never a
113109** NULL.
113110**
113111** If STAT4 is not enabled, then J is always
113112** STAT_GET_STAT1 and is hence omitted and this routine becomes
113113** a one-parameter function, stat_get(P), that always returns the
113114** stat1 table entry information.
113115*/
113116static void statGet(
113117 sqlite3_context *context,
113118 int argc,
113119 sqlite3_value **argv
113120){
113121 StatAccum *p = (StatAccum*)sqlite3_value_blob(argv[0]);
113122#ifdef SQLITE_ENABLE_STAT4
113123 /* STAT4 has a parameter on this routine. */
113124 int eCall = sqlite3_value_int(argv[1]);
113125 assert( argc==2 )((void) (0));
113126 assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ((void) (0))
113127 || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT((void) (0))
113128 || eCall==STAT_GET_NDLT((void) (0))
113129 )((void) (0));
113130 assert( eCall==STAT_GET_STAT1 || p->mxSample )((void) (0));
113131 if( eCall==STAT_GET_STAT10 )
113132#else
113133 assert( argc==1 )((void) (0));
113134#endif
113135 {
113136 /* Return the value to store in the "stat" column of the sqlite_stat1
113137 ** table for this index.
113138 **
113139 ** The value is a string composed of a list of integers describing
113140 ** the index. The first integer in the list is the total number of
113141 ** entries in the index. There is one additional integer in the list
113142 ** for each indexed column. This additional integer is an estimate of
113143 ** the number of rows matched by a equality query on the index using
113144 ** a key with the corresponding number of fields. In other words,
113145 ** if the index is on columns (a,b) and the sqlite_stat1 value is
113146 ** "100 10 2", then SQLite estimates that:
113147 **
113148 ** * the index contains 100 rows,
113149 ** * "WHERE a=?" matches 10 rows, and
113150 ** * "WHERE a=? AND b=?" matches 2 rows.
113151 **
113152 ** If D is the count of distinct values and K is the total number of
113153 ** rows, then each estimate is usually computed as:
113154 **
113155 ** I = (K+D-1)/D
113156 **
113157 ** In other words, I is K/D rounded up to the next whole integer.
113158 ** However, if I is between 1.0 and 1.1 (in other words if I is
113159 ** close to 1.0 but just a little larger) then do not round up but
113160 ** instead keep the I value at 1.0.
113161 */
113162 sqlite3_str sStat; /* Text of the constructed "stat" line */
113163 int i; /* Loop counter */
113164
113165 sqlite3StrAccumInit(&sStat, 0, 0, 0, (p->nKeyCol+1)*100);
113166 sqlite3_str_appendf(&sStat, "%llu",
113167 p->nSkipAhead ? (u64)p->nEst : (u64)p->nRow);
113168 for(i=0; i<p->nKeyCol; i++){
113169 u64 nDistinct = p->current.anDLt[i] + 1;
113170 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
113171 if( iVal==2 && p->nRow*10 <= nDistinct*11 ) iVal = 1;
113172 sqlite3_str_appendf(&sStat, " %llu", iVal);
113173 assert( p->current.anEq[i] )((void) (0));
113174 }
113175 sqlite3ResultStrAccum(context, &sStat);
113176 }
113177#ifdef SQLITE_ENABLE_STAT4
113178 else if( eCall==STAT_GET_ROWID1 ){
113179 if( p->iGet<0 ){
113180 samplePushPrevious(p, 0);
113181 p->iGet = 0;
113182 }
113183 if( p->iGet<p->nSample ){
113184 StatSample *pS = p->a + p->iGet;
113185 if( pS->nRowid==0 ){
113186 sqlite3_result_int64(context, pS->u.iRowid);
113187 }else{
113188 sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
113189 SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
113190 }
113191 }
113192 }else{
113193 tRowcnt *aCnt = 0;
113194 sqlite3_str sStat;
113195 int i;
113196
113197 assert( p->iGet<p->nSample )((void) (0));
113198 switch( eCall ){
113199 case STAT_GET_NEQ2: aCnt = p->a[p->iGet].anEq; break;
113200 case STAT_GET_NLT3: aCnt = p->a[p->iGet].anLt; break;
113201 default: {
113202 aCnt = p->a[p->iGet].anDLt;
113203 p->iGet++;
113204 break;
113205 }
113206 }
113207 sqlite3StrAccumInit(&sStat, 0, 0, 0, p->nCol*100);
113208 for(i=0; i<p->nCol; i++){
113209 sqlite3_str_appendf(&sStat, "%llu ", (u64)aCnt[i]);
113210 }
113211 if( sStat.nChar ) sStat.nChar--;
113212 sqlite3ResultStrAccum(context, &sStat);
113213 }
113214#endif /* SQLITE_ENABLE_STAT4 */
113215#ifndef SQLITE_DEBUG
113216 UNUSED_PARAMETER( argc )(void)(argc);
113217#endif
113218}
113219static const FuncDef statGetFuncdef = {
113220 1+IsStat40, /* nArg */
113221 SQLITE_UTF81, /* funcFlags */
113222 0, /* pUserData */
113223 0, /* pNext */
113224 statGet, /* xSFunc */
113225 0, /* xFinalize */
113226 0, 0, /* xValue, xInverse */
113227 "stat_get", /* zName */
113228 {0}
113229};
113230
113231static void callStatGet(Parse *pParse, int regStat, int iParam, int regOut){
113232#ifdef SQLITE_ENABLE_STAT4
113233 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Integer71, iParam, regStat+1);
113234#elif SQLITE_DEBUG
113235 assert( iParam==STAT_GET_STAT1 )((void) (0));
113236#else
113237 UNUSED_PARAMETER( iParam )(void)(iParam);
113238#endif
113239 assert( regOut!=regStat && regOut!=regStat+1 )((void) (0));
113240 sqlite3VdbeAddFunctionCall(pParse, 0, regStat, regOut, 1+IsStat40,
113241 &statGetFuncdef, 0);
113242}
113243
113244#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
113245/* Add a comment to the most recent VDBE opcode that is the name
113246** of the k-th column of the pIdx index.
113247*/
113248static void analyzeVdbeCommentIndexWithColumnName(
113249 Vdbe *v, /* Prepared statement under construction */
113250 Index *pIdx, /* Index whose column is being loaded */
113251 int k /* Which column index */
113252){
113253 int i; /* Index of column in the table */
113254 assert( k>=0 && k<pIdx->nColumn )((void) (0));
113255 i = pIdx->aiColumn[k];
113256 if( NEVER(i==XN_ROWID)(i==(-1)) ){
113257 VdbeComment((v,"%s.rowid",pIdx->zName));
113258 }else if( i==XN_EXPR(-2) ){
113259 VdbeComment((v,"%s.expr(%d)",pIdx->zName, k));
113260 }else{
113261 VdbeComment((v,"%s.%s", pIdx->zName, pIdx->pTable->aCol[i].zCnName));
113262 }
113263}
113264#else
113265# define analyzeVdbeCommentIndexWithColumnName(a,b,c)
113266#endif /* SQLITE_DEBUG */
113267
113268/*
113269** Generate code to do an analysis of all indices associated with
113270** a single table.
113271*/
113272static void analyzeOneTable(
113273 Parse *pParse, /* Parser context */
113274 Table *pTab, /* Table whose indices are to be analyzed */
113275 Index *pOnlyIdx, /* If not NULL, only analyze this one index */
113276 int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */
113277 int iMem, /* Available memory locations begin here */
113278 int iTab /* Next available cursor */
113279){
113280 sqlite3 *db = pParse->db; /* Database handle */
113281 Index *pIdx; /* An index to being analyzed */
113282 int iIdxCur; /* Cursor open on index being analyzed */
113283 int iTabCur; /* Table cursor */
113284 Vdbe *v; /* The virtual machine being built up */
113285 int i; /* Loop counter */
113286 int jZeroRows = -1; /* Jump from here if number of rows is zero */
113287 int iDb; /* Index of database containing pTab */
113288 u8 needTableCnt = 1; /* True to count the table */
113289 int regNewRowid = iMem++; /* Rowid for the inserted record */
113290 int regStat = iMem++; /* Register to hold StatAccum object */
113291 int regChng = iMem++; /* Index of changed index field */
113292 int regRowid = iMem++; /* Rowid argument passed to stat_push() */
113293 int regTemp = iMem++; /* Temporary use register */
113294 int regTemp2 = iMem++; /* Second temporary use register */
113295 int regTabname = iMem++; /* Register containing table name */
113296 int regIdxname = iMem++; /* Register containing index name */
113297 int regStat1 = iMem++; /* Value for the stat column of sqlite_stat1 */
113298 int regPrev = iMem; /* MUST BE LAST (see below) */
113299#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
113300 Table *pStat1 = 0;
113301#endif
113302
113303 pParse->nMem = MAX(pParse->nMem, iMem)((pParse->nMem)>(iMem)?(pParse->nMem):(iMem));
113304 v = sqlite3GetVdbe(pParse);
113305 if( v==0 || NEVER(pTab==0)(pTab==0) ){
113306 return;
113307 }
113308 if( !IsOrdinaryTable(pTab)((pTab)->eTabType==0) ){
113309 /* Do not gather statistics on views or virtual tables */
113310 return;
113311 }
113312 if( sqlite3_strlike("sqlite\\_%", pTab->zName, '\\')==0 ){
113313 /* Do not gather statistics on system tables */
113314 return;
113315 }
113316 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
113317 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
113318 assert( iDb>=0 )((void) (0));
113319 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
113320#ifndef SQLITE_OMIT_AUTHORIZATION
113321 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE28, pTab->zName, 0,
113322 db->aDb[iDb].zDbSName ) ){
113323 return;
113324 }
113325#endif
113326
113327#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
113328 if( db->xPreUpdateCallback ){
113329 pStat1 = (Table*)sqlite3DbMallocZero(db, sizeof(Table) + 13);
113330 if( pStat1==0 ) return;
113331 pStat1->zName = (char*)&pStat1[1];
113332 memcpy(pStat1->zName, "sqlite_stat1", 13);
113333 pStat1->nCol = 3;
113334 pStat1->iPKey = -1;
113335 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Noop184, 0, 0, 0,(char*)pStat1,P4_DYNAMIC(-6));
113336 }
113337#endif
113338
113339 /* Establish a read-lock on the table at the shared-cache level.
113340 ** Open a read-only cursor on the table. Also allocate a cursor number
113341 ** to use for scanning indexes (iIdxCur). No index cursor is opened at
113342 ** this time though. */
113343 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
113344 iTabCur = iTab++;
113345 iIdxCur = iTab++;
113346 pParse->nTab = MAX(pParse->nTab, iTab)((pParse->nTab)>(iTab)?(pParse->nTab):(iTab));
113347 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead112);
113348 sqlite3VdbeLoadString(v, regTabname, pTab->zName);
113349
113350 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
113351 int nCol; /* Number of columns in pIdx. "N" */
113352 int addrRewind; /* Address of "OP_Rewind iIdxCur" */
113353 int addrNextRow; /* Address of "next_row:" */
113354 const char *zIdxName; /* Name of the index */
113355 int nColTest; /* Number of columns to test for changes */
113356
113357 if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
113358 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
113359 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) && IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ){
113360 nCol = pIdx->nKeyCol;
113361 zIdxName = pTab->zName;
113362 nColTest = nCol - 1;
113363 }else{
113364 nCol = pIdx->nColumn;
113365 zIdxName = pIdx->zName;
113366 nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
113367 }
113368
113369 /* Populate the register containing the index name. */
113370 sqlite3VdbeLoadString(v, regIdxname, zIdxName);
113371 VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
113372
113373 /*
113374 ** Pseudo-code for loop that calls stat_push():
113375 **
113376 ** Rewind csr
113377 ** if eof(csr) goto end_of_scan;
113378 ** regChng = 0
113379 ** goto chng_addr_0;
113380 **
113381 ** next_row:
113382 ** regChng = 0
113383 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
113384 ** regChng = 1
113385 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
113386 ** ...
113387 ** regChng = N
113388 ** goto chng_addr_N
113389 **
113390 ** chng_addr_0:
113391 ** regPrev(0) = idx(0)
113392 ** chng_addr_1:
113393 ** regPrev(1) = idx(1)
113394 ** ...
113395 **
113396 ** endDistinctTest:
113397 ** regRowid = idx(rowid)
113398 ** stat_push(P, regChng, regRowid)
113399 ** Next csr
113400 ** if !eof(csr) goto next_row;
113401 **
113402 ** end_of_scan:
113403 */
113404
113405 /* Make sure there are enough memory cells allocated to accommodate
113406 ** the regPrev array and a trailing rowid (the rowid slot is required
113407 ** when building a record to insert into the sample column of
113408 ** the sqlite_stat4 table. */
113409 pParse->nMem = MAX(pParse->nMem, regPrev+nColTest)((pParse->nMem)>(regPrev+nColTest)?(pParse->nMem):(regPrev
+nColTest))
;
113410
113411 /* Open a read-only cursor on the index being analyzed. */
113412 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) )((void) (0));
113413 sqlite3VdbeAddOp3(v, OP_OpenRead112, iIdxCur, pIdx->tnum, iDb);
113414 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
113415 VdbeComment((v, "%s", pIdx->zName));
113416
113417 /* Invoke the stat_init() function. The arguments are:
113418 **
113419 ** (1) the number of columns in the index including the rowid
113420 ** (or for a WITHOUT ROWID table, the number of PK columns),
113421 ** (2) the number of columns in the key without the rowid/pk
113422 ** (3) estimated number of rows in the index,
113423 */
113424 sqlite3VdbeAddOp2(v, OP_Integer71, nCol, regStat+1);
113425 assert( regRowid==regStat+2 )((void) (0));
113426 sqlite3VdbeAddOp2(v, OP_Integer71, pIdx->nKeyCol, regRowid);
113427#ifdef SQLITE_ENABLE_STAT4
113428 if( OptimizationEnabled(db, SQLITE_Stat4)(((db)->dbOptFlags&(0x00000800))==0) ){
113429 sqlite3VdbeAddOp2(v, OP_Count98, iIdxCur, regTemp);
113430 addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind35, iIdxCur);
113431 VdbeCoverage(v);
113432 }else
113433#endif
113434 {
113435 addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind35, iIdxCur);
113436 VdbeCoverage(v);
113437 sqlite3VdbeAddOp3(v, OP_Count98, iIdxCur, regTemp, 1);
113438 }
113439 assert( regTemp2==regStat+4 )((void) (0));
113440 sqlite3VdbeAddOp2(v, OP_Integer71, db->nAnalysisLimit, regTemp2);
113441 sqlite3VdbeAddFunctionCall(pParse, 0, regStat+1, regStat, 4,
113442 &statInitFuncdef, 0);
113443
113444 /* Implementation of the following:
113445 **
113446 ** Rewind csr
113447 ** if eof(csr) goto end_of_scan;
113448 ** regChng = 0
113449 ** goto next_push_0;
113450 **
113451 */
113452 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regChng);
113453 addrNextRow = sqlite3VdbeCurrentAddr(v);
113454
113455 if( nColTest>0 ){
113456 int endDistinctTest = sqlite3VdbeMakeLabel(pParse);
113457 int *aGotoChng; /* Array of jump instruction addresses */
113458 aGotoChng = sqlite3DbMallocRawNN(db, sizeof(int)*nColTest);
113459 if( aGotoChng==0 ) continue;
113460
113461 /*
113462 ** next_row:
113463 ** regChng = 0
113464 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
113465 ** regChng = 1
113466 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
113467 ** ...
113468 ** regChng = N
113469 ** goto endDistinctTest
113470 */
113471 sqlite3VdbeAddOp0(v, OP_Goto8);
113472 addrNextRow = sqlite3VdbeCurrentAddr(v);
113473 if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx)((pIdx)->onError!=0) ){
113474 /* For a single-column UNIQUE index, once we have found a non-NULL
113475 ** row, we know that all the rest will be distinct, so skip
113476 ** subsequent distinctness tests. */
113477 sqlite3VdbeAddOp2(v, OP_NotNull51, regPrev, endDistinctTest);
113478 VdbeCoverage(v);
113479 }
113480 for(i=0; i<nColTest; i++){
113481 char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
113482 sqlite3VdbeAddOp2(v, OP_Integer71, i, regChng);
113483 sqlite3VdbeAddOp3(v, OP_Column94, iIdxCur, i, regTemp);
113484 analyzeVdbeCommentIndexWithColumnName(v,pIdx,i);
113485 aGotoChng[i] =
113486 sqlite3VdbeAddOp4(v, OP_Ne52, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ(-2));
113487 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ0x80);
113488 VdbeCoverage(v);
113489 }
113490 sqlite3VdbeAddOp2(v, OP_Integer71, nColTest, regChng);
113491 sqlite3VdbeGoto(v, endDistinctTest);
113492
113493
113494 /*
113495 ** chng_addr_0:
113496 ** regPrev(0) = idx(0)
113497 ** chng_addr_1:
113498 ** regPrev(1) = idx(1)
113499 ** ...
113500 */
113501 sqlite3VdbeJumpHere(v, addrNextRow-1);
113502 for(i=0; i<nColTest; i++){
113503 sqlite3VdbeJumpHere(v, aGotoChng[i]);
113504 sqlite3VdbeAddOp3(v, OP_Column94, iIdxCur, i, regPrev+i);
113505 analyzeVdbeCommentIndexWithColumnName(v,pIdx,i);
113506 }
113507 sqlite3VdbeResolveLabel(v, endDistinctTest);
113508 sqlite3DbFree(db, aGotoChng);
113509 }
113510
113511 /*
113512 ** chng_addr_N:
113513 ** regRowid = idx(rowid) // STAT4 only
113514 ** stat_push(P, regChng, regRowid) // 3rd parameter STAT4 only
113515 ** Next csr
113516 ** if !eof(csr) goto next_row;
113517 */
113518#ifdef SQLITE_ENABLE_STAT4
113519 if( OptimizationEnabled(db, SQLITE_Stat4)(((db)->dbOptFlags&(0x00000800))==0) ){
113520 assert( regRowid==(regStat+2) )((void) (0));
113521 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
113522 sqlite3VdbeAddOp2(v, OP_IdxRowid142, iIdxCur, regRowid);
113523 }else{
113524 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
113525 int j, k, regKey;
113526 regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
113527 for(j=0; j<pPk->nKeyCol; j++){
113528 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
113529 assert( k>=0 && k<pIdx->nColumn )((void) (0));
113530 sqlite3VdbeAddOp3(v, OP_Column94, iIdxCur, k, regKey+j);
113531 analyzeVdbeCommentIndexWithColumnName(v,pIdx,k);
113532 }
113533 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regKey, pPk->nKeyCol, regRowid);
113534 sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
113535 }
113536 }
113537#endif
113538 assert( regChng==(regStat+1) )((void) (0));
113539 {
113540 sqlite3VdbeAddFunctionCall(pParse, 1, regStat, regTemp, 2+IsStat40,
113541 &statPushFuncdef, 0);
113542 if( db->nAnalysisLimit ){
113543 int j1, j2, j3;
113544 j1 = sqlite3VdbeAddOp1(v, OP_IsNull50, regTemp); VdbeCoverage(v);
113545 j2 = sqlite3VdbeAddOp1(v, OP_If15, regTemp); VdbeCoverage(v);
113546 j3 = sqlite3VdbeAddOp4Int(v, OP_SeekGT23, iIdxCur, 0, regPrev, 1);
113547 VdbeCoverage(v);
113548 sqlite3VdbeJumpHere(v, j1);
113549 sqlite3VdbeAddOp2(v, OP_Next38, iIdxCur, addrNextRow); VdbeCoverage(v);
113550 sqlite3VdbeJumpHere(v, j2);
113551 sqlite3VdbeJumpHere(v, j3);
113552 }else{
113553 sqlite3VdbeAddOp2(v, OP_Next38, iIdxCur, addrNextRow); VdbeCoverage(v);
113554 }
113555 }
113556
113557 /* Add the entry to the stat1 table. */
113558 callStatGet(pParse, regStat, STAT_GET_STAT10, regStat1);
113559 assert( "BBB"[0]==SQLITE_AFF_TEXT )((void) (0));
113560 sqlite3VdbeAddOp4(v, OP_MakeRecord97, regTabname, 3, regTemp, "BBB", 0);
113561 sqlite3VdbeAddOp2(v, OP_NewRowid127, iStatCur, regNewRowid);
113562 sqlite3VdbeAddOp3(v, OP_Insert128, iStatCur, regTemp, regNewRowid);
113563#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
113564 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE(-5));
113565#endif
113566 sqlite3VdbeChangeP5(v, OPFLAG_APPEND0x08);
113567
113568 /* Add the entries to the stat4 table. */
113569#ifdef SQLITE_ENABLE_STAT4
113570 if( OptimizationEnabled(db, SQLITE_Stat4)(((db)->dbOptFlags&(0x00000800))==0) && db->nAnalysisLimit==0 ){
113571 int regEq = regStat1;
113572 int regLt = regStat1+1;
113573 int regDLt = regStat1+2;
113574 int regSample = regStat1+3;
113575 int regCol = regStat1+4;
113576 int regSampleRowid = regCol + nCol;
113577 int addrNext;
113578 int addrIsNull;
113579 u8 seekOp = HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ? OP_NotExists30 : OP_NotFound27;
113580
113581 pParse->nMem = MAX(pParse->nMem, regCol+nCol)((pParse->nMem)>(regCol+nCol)?(pParse->nMem):(regCol
+nCol))
;
113582
113583 addrNext = sqlite3VdbeCurrentAddr(v);
113584 callStatGet(pParse, regStat, STAT_GET_ROWID1, regSampleRowid);
113585 addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull50, regSampleRowid);
113586 VdbeCoverage(v);
113587 callStatGet(pParse, regStat, STAT_GET_NEQ2, regEq);
113588 callStatGet(pParse, regStat, STAT_GET_NLT3, regLt);
113589 callStatGet(pParse, regStat, STAT_GET_NDLT4, regDLt);
113590 sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0);
113591 VdbeCoverage(v);
113592 for(i=0; i<nCol; i++){
113593 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, i, regCol+i);
113594 }
113595 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regCol, nCol, regSample);
113596 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regTabname, 6, regTemp);
113597 sqlite3VdbeAddOp2(v, OP_NewRowid127, iStatCur+1, regNewRowid);
113598 sqlite3VdbeAddOp3(v, OP_Insert128, iStatCur+1, regTemp, regNewRowid);
113599 sqlite3VdbeAddOp2(v, OP_Goto8, 1, addrNext); /* P1==1 for end-of-loop */
113600 sqlite3VdbeJumpHere(v, addrIsNull);
113601 }
113602#endif /* SQLITE_ENABLE_STAT4 */
113603
113604 /* End of analysis */
113605 sqlite3VdbeJumpHere(v, addrRewind);
113606 }
113607
113608
113609 /* Create a single sqlite_stat1 entry containing NULL as the index
113610 ** name and the row count as the content.
113611 */
113612 if( pOnlyIdx==0 && needTableCnt ){
113613 VdbeComment((v, "%s", pTab->zName));
113614 sqlite3VdbeAddOp2(v, OP_Count98, iTabCur, regStat1);
113615 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot16, regStat1); VdbeCoverage(v);
113616 sqlite3VdbeAddOp2(v, OP_Null75, 0, regIdxname);
113617 assert( "BBB"[0]==SQLITE_AFF_TEXT )((void) (0));
113618 sqlite3VdbeAddOp4(v, OP_MakeRecord97, regTabname, 3, regTemp, "BBB", 0);
113619 sqlite3VdbeAddOp2(v, OP_NewRowid127, iStatCur, regNewRowid);
113620 sqlite3VdbeAddOp3(v, OP_Insert128, iStatCur, regTemp, regNewRowid);
113621 sqlite3VdbeChangeP5(v, OPFLAG_APPEND0x08);
113622#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
113623 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE(-5));
113624#endif
113625 sqlite3VdbeJumpHere(v, jZeroRows);
113626 }
113627}
113628
113629
113630/*
113631** Generate code that will cause the most recent index analysis to
113632** be loaded into internal hash tables where is can be used.
113633*/
113634static void loadAnalysis(Parse *pParse, int iDb){
113635 Vdbe *v = sqlite3GetVdbe(pParse);
113636 if( v ){
113637 sqlite3VdbeAddOp1(v, OP_LoadAnalysis150, iDb);
113638 }
113639}
113640
113641/*
113642** Generate code that will do an analysis of an entire database
113643*/
113644static void analyzeDatabase(Parse *pParse, int iDb){
113645 sqlite3 *db = pParse->db;
113646 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
113647 HashElem *k;
113648 int iStatCur;
113649 int iMem;
113650 int iTab;
113651
113652 sqlite3BeginWriteOperation(pParse, 0, iDb);
113653 iStatCur = pParse->nTab;
113654 pParse->nTab += 3;
113655 openStatTable(pParse, iDb, iStatCur, 0, 0);
113656 iMem = pParse->nMem+1;
113657 iTab = pParse->nTab;
113658 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
113659 for(k=sqliteHashFirst(&pSchema->tblHash)((&pSchema->tblHash)->first); k; k=sqliteHashNext(k)((k)->next)){
113660 Table *pTab = (Table*)sqliteHashData(k)((k)->data);
113661 analyzeOneTable(pParse, pTab, 0, iStatCur, iMem, iTab);
113662 }
113663 loadAnalysis(pParse, iDb);
113664}
113665
113666/*
113667** Generate code that will do an analysis of a single table in
113668** a database. If pOnlyIdx is not NULL then it is a single index
113669** in pTab that should be analyzed.
113670*/
113671static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
113672 int iDb;
113673 int iStatCur;
113674
113675 assert( pTab!=0 )((void) (0));
113676 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) )((void) (0));
113677 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
113678 sqlite3BeginWriteOperation(pParse, 0, iDb);
113679 iStatCur = pParse->nTab;
113680 pParse->nTab += 3;
113681 if( pOnlyIdx ){
113682 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
113683 }else{
113684 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
113685 }
113686 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
113687 loadAnalysis(pParse, iDb);
113688}
113689
113690/*
113691** Generate code for the ANALYZE command. The parser calls this routine
113692** when it recognizes an ANALYZE command.
113693**
113694** ANALYZE -- 1
113695** ANALYZE <database> -- 2
113696** ANALYZE ?<database>.?<tablename> -- 3
113697**
113698** Form 1 causes all indices in all attached databases to be analyzed.
113699** Form 2 analyzes all indices the single database named.
113700** Form 3 analyzes all indices associated with the named table.
113701*/
113702SQLITE_PRIVATEstatic void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
113703 sqlite3 *db = pParse->db;
113704 int iDb;
113705 int i;
113706 char *z, *zDb;
113707 Table *pTab;
113708 Index *pIdx;
113709 Token *pTableName;
113710 Vdbe *v;
113711
113712 /* Read the database schema. If an error occurs, leave an error message
113713 ** and code in pParse and return NULL. */
113714 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) )((void) (0));
113715 if( SQLITE_OK0!=sqlite3ReadSchema(pParse) ){
113716 return;
113717 }
113718
113719 assert( pName2!=0 || pName1==0 )((void) (0));
113720 if( pName1==0 ){
113721 /* Form 1: Analyze everything */
113722 for(i=0; i<db->nDb; i++){
113723 if( i==1 ) continue; /* Do not analyze the TEMP database */
113724 analyzeDatabase(pParse, i);
113725 }
113726 }else if( pName2->n==0 && (iDb = sqlite3FindDb(db, pName1))>=0 ){
113727 /* Analyze the schema named as the argument */
113728 analyzeDatabase(pParse, iDb);
113729 }else{
113730 /* Form 3: Analyze the table or index named as an argument */
113731 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
113732 if( iDb>=0 ){
113733 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
113734 z = sqlite3NameFromToken(db, pTableName);
113735 if( z ){
113736 if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
113737 analyzeTable(pParse, pIdx->pTable, pIdx);
113738 }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
113739 analyzeTable(pParse, pTab, 0);
113740 }
113741 sqlite3DbFree(db, z);
113742 }
113743 }
113744 }
113745 if( db->nSqlExec==0 && (v = sqlite3GetVdbe(pParse))!=0 ){
113746 sqlite3VdbeAddOp0(v, OP_Expire166);
113747 }
113748}
113749
113750/*
113751** Used to pass information from the analyzer reader through to the
113752** callback routine.
113753*/
113754typedef struct analysisInfo analysisInfo;
113755struct analysisInfo {
113756 sqlite3 *db;
113757 const char *zDatabase;
113758};
113759
113760/*
113761** The first argument points to a nul-terminated string containing a
113762** list of space separated integers. Read the first nOut of these into
113763** the array aOut[].
113764*/
113765static void decodeIntArray(
113766 char *zIntArray, /* String containing int array to decode */
113767 int nOut, /* Number of slots in aOut[] */
113768 tRowcnt *aOut, /* Store integers here */
113769 LogEst *aLog, /* Or, if aOut==0, here */
113770 Index *pIndex /* Handle extra flags for this index, if not NULL */
113771){
113772 char *z = zIntArray;
113773 int c;
113774 int i;
113775 tRowcnt v;
113776
113777#ifdef SQLITE_ENABLE_STAT4
113778 if( z==0 ) z = "";
113779#else
113780 assert( z!=0 )((void) (0));
113781#endif
113782 for(i=0; *z && i<nOut; i++){
113783 v = 0;
113784 while( (c=z[0])>='0' && c<='9' ){
113785 v = v*10 + c - '0';
113786 z++;
113787 }
113788#ifdef SQLITE_ENABLE_STAT4
113789 if( aOut ) aOut[i] = v;
113790 if( aLog ) aLog[i] = sqlite3LogEst(v);
113791#else
113792 assert( aOut==0 )((void) (0));
113793 UNUSED_PARAMETER(aOut)(void)(aOut);
113794 assert( aLog!=0 )((void) (0));
113795 aLog[i] = sqlite3LogEst(v);
113796#endif
113797 if( *z==' ' ) z++;
113798 }
113799#ifndef SQLITE_ENABLE_STAT4
113800 assert( pIndex!=0 )((void) (0)); {
113801#else
113802 if( pIndex ){
113803#endif
113804 pIndex->bUnordered = 0;
113805 pIndex->noSkipScan = 0;
113806 while( z[0] ){
113807 if( sqlite3_strglob("unordered*", z)==0 ){
113808 pIndex->bUnordered = 1;
113809 }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
113810 int sz = sqlite3Atoi(z+3);
113811 if( sz<2 ) sz = 2;
113812 pIndex->szIdxRow = sqlite3LogEst(sz);
113813 }else if( sqlite3_strglob("noskipscan*", z)==0 ){
113814 pIndex->noSkipScan = 1;
113815 }
113816#ifdef SQLITE_ENABLE_COSTMULT
113817 else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
113818 pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
113819 }
113820#endif
113821 while( z[0]!=0 && z[0]!=' ' ) z++;
113822 while( z[0]==' ' ) z++;
113823 }
113824 }
113825}
113826
113827/*
113828** This callback is invoked once for each index when reading the
113829** sqlite_stat1 table.
113830**
113831** argv[0] = name of the table
113832** argv[1] = name of the index (might be NULL)
113833** argv[2] = results of analysis - on integer for each column
113834**
113835** Entries for which argv[1]==NULL simply record the number of rows in
113836** the table.
113837*/
113838static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
113839 analysisInfo *pInfo = (analysisInfo*)pData;
113840 Index *pIndex;
113841 Table *pTable;
113842 const char *z;
113843
113844 assert( argc==3 )((void) (0));
113845 UNUSED_PARAMETER2(NotUsed, argc)(void)(NotUsed),(void)(argc);
113846
113847 if( argv==0 || argv[0]==0 || argv[2]==0 ){
113848 return 0;
113849 }
113850 pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
113851 if( pTable==0 ){
113852 return 0;
113853 }
113854 if( argv[1]==0 ){
113855 pIndex = 0;
113856 }else if( sqlite3_stricmp(argv[0],argv[1])==0 ){
113857 pIndex = sqlite3PrimaryKeyIndex(pTable);
113858 }else{
113859 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
113860 }
113861 z = argv[2];
113862
113863 if( pIndex ){
113864 tRowcnt *aiRowEst = 0;
113865 int nCol = pIndex->nKeyCol+1;
113866#ifdef SQLITE_ENABLE_STAT4
113867 /* Index.aiRowEst may already be set here if there are duplicate
113868 ** sqlite_stat1 entries for this index. In that case just clobber
113869 ** the old data with the new instead of allocating a new array. */
113870 if( pIndex->aiRowEst==0 ){
113871 pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
113872 if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
113873 }
113874 aiRowEst = pIndex->aiRowEst;
113875#endif
113876 pIndex->bUnordered = 0;
113877 decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
113878 pIndex->hasStat1 = 1;
113879 if( pIndex->pPartIdxWhere==0 ){
113880 pTable->nRowLogEst = pIndex->aiRowLogEst[0];
113881 pTable->tabFlags |= TF_HasStat10x00000010;
113882 }
113883 }else{
113884 Index fakeIdx;
113885 fakeIdx.szIdxRow = pTable->szTabRow;
113886#ifdef SQLITE_ENABLE_COSTMULT
113887 fakeIdx.pTable = pTable;
113888#endif
113889 decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
113890 pTable->szTabRow = fakeIdx.szIdxRow;
113891 pTable->tabFlags |= TF_HasStat10x00000010;
113892 }
113893
113894 return 0;
113895}
113896
113897/*
113898** If the Index.aSample variable is not NULL, delete the aSample[] array
113899** and its contents.
113900*/
113901SQLITE_PRIVATEstatic void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
113902#ifdef SQLITE_ENABLE_STAT4
113903 if( pIdx->aSample ){
113904 int j;
113905 for(j=0; j<pIdx->nSample; j++){
113906 IndexSample *p = &pIdx->aSample[j];
113907 sqlite3DbFree(db, p->p);
113908 }
113909 sqlite3DbFree(db, pIdx->aSample);
113910 }
113911 if( db && db->pnBytesFreed==0 ){
113912 pIdx->nSample = 0;
113913 pIdx->aSample = 0;
113914 }
113915#else
113916 UNUSED_PARAMETER(db)(void)(db);
113917 UNUSED_PARAMETER(pIdx)(void)(pIdx);
113918#endif /* SQLITE_ENABLE_STAT4 */
113919}
113920
113921#ifdef SQLITE_ENABLE_STAT4
113922/*
113923** Populate the pIdx->aAvgEq[] array based on the samples currently
113924** stored in pIdx->aSample[].
113925*/
113926static void initAvgEq(Index *pIdx){
113927 if( pIdx ){
113928 IndexSample *aSample = pIdx->aSample;
113929 IndexSample *pFinal = &aSample[pIdx->nSample-1];
113930 int iCol;
113931 int nCol = 1;
113932 if( pIdx->nSampleCol>1 ){
113933 /* If this is stat4 data, then calculate aAvgEq[] values for all
113934 ** sample columns except the last. The last is always set to 1, as
113935 ** once the trailing PK fields are considered all index keys are
113936 ** unique. */
113937 nCol = pIdx->nSampleCol-1;
113938 pIdx->aAvgEq[nCol] = 1;
113939 }
113940 for(iCol=0; iCol<nCol; iCol++){
113941 int nSample = pIdx->nSample;
113942 int i; /* Used to iterate through samples */
113943 tRowcnt sumEq = 0; /* Sum of the nEq values */
113944 tRowcnt avgEq = 0;
113945 tRowcnt nRow; /* Number of rows in index */
113946 i64 nSum100 = 0; /* Number of terms contributing to sumEq */
113947 i64 nDist100; /* Number of distinct values in index */
113948
113949 if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
113950 nRow = pFinal->anLt[iCol];
113951 nDist100 = (i64)100 * pFinal->anDLt[iCol];
113952 nSample--;
113953 }else{
113954 nRow = pIdx->aiRowEst[0];
113955 nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
113956 }
113957 pIdx->nRowEst0 = nRow;
113958
113959 /* Set nSum to the number of distinct (iCol+1) field prefixes that
113960 ** occur in the stat4 table for this index. Set sumEq to the sum of
113961 ** the nEq values for column iCol for the same set (adding the value
113962 ** only once where there exist duplicate prefixes). */
113963 for(i=0; i<nSample; i++){
113964 if( i==(pIdx->nSample-1)
113965 || aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol]
113966 ){
113967 sumEq += aSample[i].anEq[iCol];
113968 nSum100 += 100;
113969 }
113970 }
113971
113972 if( nDist100>nSum100 && sumEq<nRow ){
113973 avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
113974 }
113975 if( avgEq==0 ) avgEq = 1;
113976 pIdx->aAvgEq[iCol] = avgEq;
113977 }
113978 }
113979}
113980
113981/*
113982** Look up an index by name. Or, if the name of a WITHOUT ROWID table
113983** is supplied instead, find the PRIMARY KEY index for that table.
113984*/
113985static Index *findIndexOrPrimaryKey(
113986 sqlite3 *db,
113987 const char *zName,
113988 const char *zDb
113989){
113990 Index *pIdx = sqlite3FindIndex(db, zName, zDb);
113991 if( pIdx==0 ){
113992 Table *pTab = sqlite3FindTable(db, zName, zDb);
113993 if( pTab && !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ) pIdx = sqlite3PrimaryKeyIndex(pTab);
113994 }
113995 return pIdx;
113996}
113997
113998/*
113999** Load the content from either the sqlite_stat4
114000** into the relevant Index.aSample[] arrays.
114001**
114002** Arguments zSql1 and zSql2 must point to SQL statements that return
114003** data equivalent to the following:
114004**
114005** zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx
114006** zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4
114007**
114008** where %Q is replaced with the database name before the SQL is executed.
114009*/
114010static int loadStatTbl(
114011 sqlite3 *db, /* Database handle */
114012 const char *zSql1, /* SQL statement 1 (see above) */
114013 const char *zSql2, /* SQL statement 2 (see above) */
114014 const char *zDb /* Database name (e.g. "main") */
114015){
114016 int rc; /* Result codes from subroutines */
114017 sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
114018 char *zSql; /* Text of the SQL statement */
114019 Index *pPrevIdx = 0; /* Previous index in the loop */
114020 IndexSample *pSample; /* A slot in pIdx->aSample[] */
114021
114022 assert( db->lookaside.bDisable )((void) (0));
114023 zSql = sqlite3MPrintf(db, zSql1, zDb);
114024 if( !zSql ){
114025 return SQLITE_NOMEM_BKPT7;
114026 }
114027 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
114028 sqlite3DbFree(db, zSql);
114029 if( rc ) return rc;
114030
114031 while( sqlite3_step(pStmt)==SQLITE_ROW100 ){
114032 int nIdxCol = 1; /* Number of columns in stat4 records */
114033
114034 char *zIndex; /* Index name */
114035 Index *pIdx; /* Pointer to the index object */
114036 int nSample; /* Number of samples */
114037 int nByte; /* Bytes of space required */
114038 int i; /* Bytes of space required */
114039 tRowcnt *pSpace;
114040
114041 zIndex = (char *)sqlite3_column_text(pStmt, 0);
114042 if( zIndex==0 ) continue;
114043 nSample = sqlite3_column_int(pStmt, 1);
114044 pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
114045 assert( pIdx==0 || pIdx->nSample==0 )((void) (0));
114046 if( pIdx==0 ) continue;
114047 assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 )((void) (0));
114048 if( !HasRowid(pIdx->pTable)(((pIdx->pTable)->tabFlags & 0x00000080)==0) && IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ){
114049 nIdxCol = pIdx->nKeyCol;
114050 }else{
114051 nIdxCol = pIdx->nColumn;
114052 }
114053 pIdx->nSampleCol = nIdxCol;
114054 nByte = sizeof(IndexSample) * nSample;
114055 nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
114056 nByte += nIdxCol * sizeof(tRowcnt); /* Space for Index.aAvgEq[] */
114057
114058 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
114059 if( pIdx->aSample==0 ){
114060 sqlite3_finalize(pStmt);
114061 return SQLITE_NOMEM_BKPT7;
114062 }
114063 pSpace = (tRowcnt*)&pIdx->aSample[nSample];
114064 pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
114065 pIdx->pTable->tabFlags |= TF_HasStat40x00002000;
114066 for(i=0; i<nSample; i++){
114067 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
114068 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
114069 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
114070 }
114071 assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) )((void) (0));
114072 }
114073 rc = sqlite3_finalize(pStmt);
114074 if( rc ) return rc;
114075
114076 zSql = sqlite3MPrintf(db, zSql2, zDb);
114077 if( !zSql ){
114078 return SQLITE_NOMEM_BKPT7;
114079 }
114080 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
114081 sqlite3DbFree(db, zSql);
114082 if( rc ) return rc;
114083
114084 while( sqlite3_step(pStmt)==SQLITE_ROW100 ){
114085 char *zIndex; /* Index name */
114086 Index *pIdx; /* Pointer to the index object */
114087 int nCol = 1; /* Number of columns in index */
114088
114089 zIndex = (char *)sqlite3_column_text(pStmt, 0);
114090 if( zIndex==0 ) continue;
114091 pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
114092 if( pIdx==0 ) continue;
114093 /* This next condition is true if data has already been loaded from
114094 ** the sqlite_stat4 table. */
114095 nCol = pIdx->nSampleCol;
114096 if( pIdx!=pPrevIdx ){
114097 initAvgEq(pPrevIdx);
114098 pPrevIdx = pIdx;
114099 }
114100 pSample = &pIdx->aSample[pIdx->nSample];
114101 decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
114102 decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
114103 decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
114104
114105 /* Take a copy of the sample. Add two 0x00 bytes the end of the buffer.
114106 ** This is in case the sample record is corrupted. In that case, the
114107 ** sqlite3VdbeRecordCompare() may read up to two varints past the
114108 ** end of the allocated buffer before it realizes it is dealing with
114109 ** a corrupt record. Adding the two 0x00 bytes prevents this from causing
114110 ** a buffer overread. */
114111 pSample->n = sqlite3_column_bytes(pStmt, 4);
114112 pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
114113 if( pSample->p==0 ){
114114 sqlite3_finalize(pStmt);
114115 return SQLITE_NOMEM_BKPT7;
114116 }
114117 if( pSample->n ){
114118 memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
114119 }
114120 pIdx->nSample++;
114121 }
114122 rc = sqlite3_finalize(pStmt);
114123 if( rc==SQLITE_OK0 ) initAvgEq(pPrevIdx);
114124 return rc;
114125}
114126
114127/*
114128** Load content from the sqlite_stat4 table into
114129** the Index.aSample[] arrays of all indices.
114130*/
114131static int loadStat4(sqlite3 *db, const char *zDb){
114132 int rc = SQLITE_OK0; /* Result codes from subroutines */
114133 const Table *pStat4;
114134
114135 assert( db->lookaside.bDisable )((void) (0));
114136 if( (pStat4 = sqlite3FindTable(db, "sqlite_stat4", zDb))!=0
114137 && IsOrdinaryTable(pStat4)((pStat4)->eTabType==0)
114138 ){
114139 rc = loadStatTbl(db,
114140 "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx",
114141 "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
114142 zDb
114143 );
114144 }
114145 return rc;
114146}
114147#endif /* SQLITE_ENABLE_STAT4 */
114148
114149/*
114150** Load the content of the sqlite_stat1 and sqlite_stat4 tables. The
114151** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
114152** arrays. The contents of sqlite_stat4 are used to populate the
114153** Index.aSample[] arrays.
114154**
114155** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
114156** is returned. In this case, even if SQLITE_ENABLE_STAT4 was defined
114157** during compilation and the sqlite_stat4 table is present, no data is
114158** read from it.
114159**
114160** If SQLITE_ENABLE_STAT4 was defined during compilation and the
114161** sqlite_stat4 table is not present in the database, SQLITE_ERROR is
114162** returned. However, in this case, data is read from the sqlite_stat1
114163** table (if it is present) before returning.
114164**
114165** If an OOM error occurs, this function always sets db->mallocFailed.
114166** This means if the caller does not care about other errors, the return
114167** code may be ignored.
114168*/
114169SQLITE_PRIVATEstatic int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
114170 analysisInfo sInfo;
114171 HashElem *i;
114172 char *zSql;
114173 int rc = SQLITE_OK0;
114174 Schema *pSchema = db->aDb[iDb].pSchema;
114175 const Table *pStat1;
114176
114177 assert( iDb>=0 && iDb<db->nDb )((void) (0));
114178 assert( db->aDb[iDb].pBt!=0 )((void) (0));
114179
114180 /* Clear any prior statistics */
114181 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
114182 for(i=sqliteHashFirst(&pSchema->tblHash)((&pSchema->tblHash)->first); i; i=sqliteHashNext(i)((i)->next)){
114183 Table *pTab = sqliteHashData(i)((i)->data);
114184 pTab->tabFlags &= ~TF_HasStat10x00000010;
114185 }
114186 for(i=sqliteHashFirst(&pSchema->idxHash)((&pSchema->idxHash)->first); i; i=sqliteHashNext(i)((i)->next)){
114187 Index *pIdx = sqliteHashData(i)((i)->data);
114188 pIdx->hasStat1 = 0;
114189#ifdef SQLITE_ENABLE_STAT4
114190 sqlite3DeleteIndexSamples(db, pIdx);
114191 pIdx->aSample = 0;
114192#endif
114193 }
114194
114195 /* Load new statistics out of the sqlite_stat1 table */
114196 sInfo.db = db;
114197 sInfo.zDatabase = db->aDb[iDb].zDbSName;
114198 if( (pStat1 = sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase))
114199 && IsOrdinaryTable(pStat1)((pStat1)->eTabType==0)
114200 ){
114201 zSql = sqlite3MPrintf(db,
114202 "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
114203 if( zSql==0 ){
114204 rc = SQLITE_NOMEM_BKPT7;
114205 }else{
114206 rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
114207 sqlite3DbFree(db, zSql);
114208 }
114209 }
114210
114211 /* Set appropriate defaults on all indexes not in the sqlite_stat1 table */
114212 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
114213 for(i=sqliteHashFirst(&pSchema->idxHash)((&pSchema->idxHash)->first); i; i=sqliteHashNext(i)((i)->next)){
114214 Index *pIdx = sqliteHashData(i)((i)->data);
114215 if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
114216 }
114217
114218 /* Load the statistics from the sqlite_stat4 table. */
114219#ifdef SQLITE_ENABLE_STAT4
114220 if( rc==SQLITE_OK0 ){
114221 DisableLookasidedb->lookaside.bDisable++;db->lookaside.sz=0;
114222 rc = loadStat4(db, sInfo.zDatabase);
114223 EnableLookasidedb->lookaside.bDisable--; db->lookaside.sz=db->lookaside
.bDisable?0:db->lookaside.szTrue
;
114224 }
114225 for(i=sqliteHashFirst(&pSchema->idxHash)((&pSchema->idxHash)->first); i; i=sqliteHashNext(i)((i)->next)){
114226 Index *pIdx = sqliteHashData(i)((i)->data);
114227 sqlite3_free(pIdx->aiRowEst);
114228 pIdx->aiRowEst = 0;
114229 }
114230#endif
114231
114232 if( rc==SQLITE_NOMEM7 ){
114233 sqlite3OomFault(db);
114234 }
114235 return rc;
114236}
114237
114238
114239#endif /* SQLITE_OMIT_ANALYZE */
114240
114241/************** End of analyze.c *********************************************/
114242/************** Begin file attach.c ******************************************/
114243/*
114244** 2003 April 6
114245**
114246** The author disclaims copyright to this source code. In place of
114247** a legal notice, here is a blessing:
114248**
114249** May you do good and not evil.
114250** May you find forgiveness for yourself and forgive others.
114251** May you share freely, never taking more than you give.
114252**
114253*************************************************************************
114254** This file contains code used to implement the ATTACH and DETACH commands.
114255*/
114256/* #include "sqliteInt.h" */
114257
114258#ifndef SQLITE_OMIT_ATTACH
114259/*
114260** Resolve an expression that was part of an ATTACH or DETACH statement. This
114261** is slightly different from resolving a normal SQL expression, because simple
114262** identifiers are treated as strings, not possible column names or aliases.
114263**
114264** i.e. if the parser sees:
114265**
114266** ATTACH DATABASE abc AS def
114267**
114268** it treats the two expressions as literal strings 'abc' and 'def' instead of
114269** looking for columns of the same name.
114270**
114271** This only applies to the root node of pExpr, so the statement:
114272**
114273** ATTACH DATABASE abc||def AS 'db2'
114274**
114275** will fail because neither abc or def can be resolved.
114276*/
114277static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
114278{
114279 int rc = SQLITE_OK0;
114280 if( pExpr ){
114281 if( pExpr->op!=TK_ID59 ){
114282 rc = sqlite3ResolveExprNames(pName, pExpr);
114283 }else{
114284 pExpr->op = TK_STRING117;
114285 }
114286 }
114287 return rc;
114288}
114289
114290/*
114291** Return true if zName points to a name that may be used to refer to
114292** database iDb attached to handle db.
114293*/
114294SQLITE_PRIVATEstatic int sqlite3DbIsNamed(sqlite3 *db, int iDb, const char *zName){
114295 return (
114296 sqlite3StrICmp(db->aDb[iDb].zDbSName, zName)==0
114297 || (iDb==0 && sqlite3StrICmp("main", zName)==0)
114298 );
114299}
114300
114301/*
114302** An SQL user-function registered to do the work of an ATTACH statement. The
114303** three arguments to the function come directly from an attach statement:
114304**
114305** ATTACH DATABASE x AS y KEY z
114306**
114307** SELECT sqlite_attach(x, y, z)
114308**
114309** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
114310** third argument.
114311**
114312** If the db->init.reopenMemdb flags is set, then instead of attaching a
114313** new database, close the database on db->init.iDb and reopen it as an
114314** empty MemDB.
114315*/
114316static void attachFunc(
114317 sqlite3_context *context,
114318 int NotUsed,
114319 sqlite3_value **argv
114320){
114321 int i;
114322 int rc = 0;
114323 sqlite3 *db = sqlite3_context_db_handle(context);
114324 const char *zName;
114325 const char *zFile;
114326 char *zPath = 0;
114327 char *zErr = 0;
114328 unsigned int flags;
114329 Db *aNew; /* New array of Db pointers */
114330 Db *pNew; /* Db object for the newly attached database */
114331 char *zErrDyn = 0;
114332 sqlite3_vfs *pVfs;
114333
114334 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
114335 zFile = (const char *)sqlite3_value_text(argv[0]);
114336 zName = (const char *)sqlite3_value_text(argv[1]);
114337 if( zFile==0 ) zFile = "";
114338 if( zName==0 ) zName = "";
114339
114340#ifndef SQLITE_OMIT_DESERIALIZE
114341# define REOPEN_AS_MEMDB(db)(db->init.reopenMemdb) (db->init.reopenMemdb)
114342#else
114343# define REOPEN_AS_MEMDB(db)(db->init.reopenMemdb) (0)
114344#endif
114345
114346 if( REOPEN_AS_MEMDB(db)(db->init.reopenMemdb) ){
114347 /* This is not a real ATTACH. Instead, this routine is being called
114348 ** from sqlite3_deserialize() to close database db->init.iDb and
114349 ** reopen it as a MemDB */
114350 pVfs = sqlite3_vfs_find("memdb");
114351 if( pVfs==0 ) return;
114352 pNew = &db->aDb[db->init.iDb];
114353 if( pNew->pBt ) sqlite3BtreeClose(pNew->pBt);
114354 pNew->pBt = 0;
114355 pNew->pSchema = 0;
114356 rc = sqlite3BtreeOpen(pVfs, "x\0", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB0x00000100);
114357 }else{
114358 /* This is a real ATTACH
114359 **
114360 ** Check for the following errors:
114361 **
114362 ** * Too many attached databases,
114363 ** * Transaction currently open
114364 ** * Specified database name already being used.
114365 */
114366 if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED7]+2 ){
114367 zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
114368 db->aLimit[SQLITE_LIMIT_ATTACHED7]
114369 );
114370 goto attach_error;
114371 }
114372 for(i=0; i<db->nDb; i++){
114373 assert( zName )((void) (0));
114374 if( sqlite3DbIsNamed(db, i, zName) ){
114375 zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
114376 goto attach_error;
114377 }
114378 }
114379
114380 /* Allocate the new entry in the db->aDb[] array and initialize the schema
114381 ** hash tables.
114382 */
114383 if( db->aDb==db->aDbStatic ){
114384 aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
114385 if( aNew==0 ) return;
114386 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
114387 }else{
114388 aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
114389 if( aNew==0 ) return;
114390 }
114391 db->aDb = aNew;
114392 pNew = &db->aDb[db->nDb];
114393 memset(pNew, 0, sizeof(*pNew));
114394
114395 /* Open the database file. If the btree is successfully opened, use
114396 ** it to obtain the database schema. At this point the schema may
114397 ** or may not be initialized.
114398 */
114399 flags = db->openFlags;
114400 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
114401 if( rc!=SQLITE_OK0 ){
114402 if( rc==SQLITE_NOMEM7 ) sqlite3OomFault(db);
114403 sqlite3_result_error(context, zErr, -1);
114404 sqlite3_free(zErr);
114405 return;
114406 }
114407 assert( pVfs )((void) (0));
114408 flags |= SQLITE_OPEN_MAIN_DB0x00000100;
114409 rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
114410 db->nDb++;
114411 pNew->zDbSName = sqlite3DbStrDup(db, zName);
114412 }
114413 db->noSharedCache = 0;
114414 if( rc==SQLITE_CONSTRAINT19 ){
114415 rc = SQLITE_ERROR1;
114416 zErrDyn = sqlite3MPrintf(db, "database is already attached");
114417 }else if( rc==SQLITE_OK0 ){
114418 Pager *pPager;
114419 pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
114420 if( !pNew->pSchema ){
114421 rc = SQLITE_NOMEM_BKPT7;
114422 }else if( pNew->pSchema->file_format && pNew->pSchema->enc!=ENC(db)((db)->enc) ){
114423 zErrDyn = sqlite3MPrintf(db,
114424 "attached databases must use the same text encoding as main database");
114425 rc = SQLITE_ERROR1;
114426 }
114427 sqlite3BtreeEnter(pNew->pBt);
114428 pPager = sqlite3BtreePager(pNew->pBt);
114429 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
114430 sqlite3BtreeSecureDelete(pNew->pBt,
114431 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
114432#ifndef SQLITE_OMIT_PAGER_PRAGMAS
114433 sqlite3BtreeSetPagerFlags(pNew->pBt,
114434 PAGER_SYNCHRONOUS_FULL0x03 | (db->flags & PAGER_FLAGS_MASK0x38));
114435#endif
114436 sqlite3BtreeLeave(pNew->pBt);
114437 }
114438 pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS2+1;
114439 if( rc==SQLITE_OK0 && pNew->zDbSName==0 ){
114440 rc = SQLITE_NOMEM_BKPT7;
114441 }
114442 sqlite3_free_filename( zPath );
114443
114444 /* If the file was opened successfully, read the schema for the new database.
114445 ** If this fails, or if opening the file failed, then close the file and
114446 ** remove the entry from the db->aDb[] array. i.e. put everything back the
114447 ** way we found it.
114448 */
114449 if( rc==SQLITE_OK0 ){
114450 sqlite3BtreeEnterAll(db);
114451 db->init.iDb = 0;
114452 db->mDbFlags &= ~(DBFLAG_SchemaKnownOk0x0010);
114453 if( !REOPEN_AS_MEMDB(db)(db->init.reopenMemdb) ){
114454 rc = sqlite3Init(db, &zErrDyn);
114455 }
114456 sqlite3BtreeLeaveAll(db);
114457 assert( zErrDyn==0 || rc!=SQLITE_OK )((void) (0));
114458 }
114459#ifdef SQLITE_USER_AUTHENTICATION
114460 if( rc==SQLITE_OK0 && !REOPEN_AS_MEMDB(db)(db->init.reopenMemdb) ){
114461 u8 newAuth = 0;
114462 rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
114463 if( newAuth<db->auth.authLevel ){
114464 rc = SQLITE_AUTH_USER(23 | (1<<8));
114465 }
114466 }
114467#endif
114468 if( rc ){
114469 if( !REOPEN_AS_MEMDB(db)(db->init.reopenMemdb) ){
114470 int iDb = db->nDb - 1;
114471 assert( iDb>=2 )((void) (0));
114472 if( db->aDb[iDb].pBt ){
114473 sqlite3BtreeClose(db->aDb[iDb].pBt);
114474 db->aDb[iDb].pBt = 0;
114475 db->aDb[iDb].pSchema = 0;
114476 }
114477 sqlite3ResetAllSchemasOfConnection(db);
114478 db->nDb = iDb;
114479 if( rc==SQLITE_NOMEM7 || rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
114480 sqlite3OomFault(db);
114481 sqlite3DbFree(db, zErrDyn);
114482 zErrDyn = sqlite3MPrintf(db, "out of memory");
114483 }else if( zErrDyn==0 ){
114484 zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
114485 }
114486 }
114487 goto attach_error;
114488 }
114489
114490 return;
114491
114492attach_error:
114493 /* Return an error if we get here */
114494 if( zErrDyn ){
114495 sqlite3_result_error(context, zErrDyn, -1);
114496 sqlite3DbFree(db, zErrDyn);
114497 }
114498 if( rc ) sqlite3_result_error_code(context, rc);
114499}
114500
114501/*
114502** An SQL user-function registered to do the work of an DETACH statement. The
114503** three arguments to the function come directly from a detach statement:
114504**
114505** DETACH DATABASE x
114506**
114507** SELECT sqlite_detach(x)
114508*/
114509static void detachFunc(
114510 sqlite3_context *context,
114511 int NotUsed,
114512 sqlite3_value **argv
114513){
114514 const char *zName = (const char *)sqlite3_value_text(argv[0]);
114515 sqlite3 *db = sqlite3_context_db_handle(context);
114516 int i;
114517 Db *pDb = 0;
114518 HashElem *pEntry;
114519 char zErr[128];
114520
114521 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
114522
114523 if( zName==0 ) zName = "";
114524 for(i=0; i<db->nDb; i++){
114525 pDb = &db->aDb[i];
114526 if( pDb->pBt==0 ) continue;
114527 if( sqlite3DbIsNamed(db, i, zName) ) break;
114528 }
114529
114530 if( i>=db->nDb ){
114531 sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
114532 goto detach_error;
114533 }
114534 if( i<2 ){
114535 sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
114536 goto detach_error;
114537 }
114538 if( sqlite3BtreeTxnState(pDb->pBt)!=SQLITE_TXN_NONE0
114539 || sqlite3BtreeIsInBackup(pDb->pBt)
114540 ){
114541 sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
114542 goto detach_error;
114543 }
114544
114545 /* If any TEMP triggers reference the schema being detached, move those
114546 ** triggers to reference the TEMP schema itself. */
114547 assert( db->aDb[1].pSchema )((void) (0));
114548 pEntry = sqliteHashFirst(&db->aDb[1].pSchema->trigHash)((&db->aDb[1].pSchema->trigHash)->first);
114549 while( pEntry ){
114550 Trigger *pTrig = (Trigger*)sqliteHashData(pEntry)((pEntry)->data);
114551 if( pTrig->pTabSchema==pDb->pSchema ){
114552 pTrig->pTabSchema = pTrig->pSchema;
114553 }
114554 pEntry = sqliteHashNext(pEntry)((pEntry)->next);
114555 }
114556
114557 sqlite3BtreeClose(pDb->pBt);
114558 pDb->pBt = 0;
114559 pDb->pSchema = 0;
114560 sqlite3CollapseDatabaseArray(db);
114561 return;
114562
114563detach_error:
114564 sqlite3_result_error(context, zErr, -1);
114565}
114566
114567/*
114568** This procedure generates VDBE code for a single invocation of either the
114569** sqlite_detach() or sqlite_attach() SQL user functions.
114570*/
114571static void codeAttach(
114572 Parse *pParse, /* The parser context */
114573 int type, /* Either SQLITE_ATTACH or SQLITE_DETACH */
114574 FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */
114575 Expr *pAuthArg, /* Expression to pass to authorization callback */
114576 Expr *pFilename, /* Name of database file */
114577 Expr *pDbname, /* Name of the database to use internally */
114578 Expr *pKey /* Database key for encryption extension */
114579){
114580 int rc;
114581 NameContext sName;
114582 Vdbe *v;
114583 sqlite3* db = pParse->db;
114584 int regArgs;
114585
114586 if( pParse->nErr ) goto attach_end;
114587 memset(&sName, 0, sizeof(NameContext));
114588 sName.pParse = pParse;
114589
114590 if(
114591 SQLITE_OK0!=resolveAttachExpr(&sName, pFilename) ||
114592 SQLITE_OK0!=resolveAttachExpr(&sName, pDbname) ||
114593 SQLITE_OK0!=resolveAttachExpr(&sName, pKey)
114594 ){
114595 goto attach_end;
114596 }
114597
114598#ifndef SQLITE_OMIT_AUTHORIZATION
114599 if( ALWAYS(pAuthArg)(pAuthArg) ){
114600 char *zAuthArg;
114601 if( pAuthArg->op==TK_STRING117 ){
114602 assert( !ExprHasProperty(pAuthArg, EP_IntValue) )((void) (0));
114603 zAuthArg = pAuthArg->u.zToken;
114604 }else{
114605 zAuthArg = 0;
114606 }
114607 rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
114608 if(rc!=SQLITE_OK0 ){
114609 goto attach_end;
114610 }
114611 }
114612#endif /* SQLITE_OMIT_AUTHORIZATION */
114613
114614
114615 v = sqlite3GetVdbe(pParse);
114616 regArgs = sqlite3GetTempRange(pParse, 4);
114617 sqlite3ExprCode(pParse, pFilename, regArgs);
114618 sqlite3ExprCode(pParse, pDbname, regArgs+1);
114619 sqlite3ExprCode(pParse, pKey, regArgs+2);
114620
114621 assert( v || db->mallocFailed )((void) (0));
114622 if( v ){
114623 sqlite3VdbeAddFunctionCall(pParse, 0, regArgs+3-pFunc->nArg, regArgs+3,
114624 pFunc->nArg, pFunc, 0);
114625 /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
114626 ** statement only). For DETACH, set it to false (expire all existing
114627 ** statements).
114628 */
114629 sqlite3VdbeAddOp1(v, OP_Expire166, (type==SQLITE_ATTACH24));
114630 }
114631
114632attach_end:
114633 sqlite3ExprDelete(db, pFilename);
114634 sqlite3ExprDelete(db, pDbname);
114635 sqlite3ExprDelete(db, pKey);
114636}
114637
114638/*
114639** Called by the parser to compile a DETACH statement.
114640**
114641** DETACH pDbname
114642*/
114643SQLITE_PRIVATEstatic void sqlite3Detach(Parse *pParse, Expr *pDbname){
114644 static const FuncDef detach_func = {
114645 1, /* nArg */
114646 SQLITE_UTF81, /* funcFlags */
114647 0, /* pUserData */
114648 0, /* pNext */
114649 detachFunc, /* xSFunc */
114650 0, /* xFinalize */
114651 0, 0, /* xValue, xInverse */
114652 "sqlite_detach", /* zName */
114653 {0}
114654 };
114655 codeAttach(pParse, SQLITE_DETACH25, &detach_func, pDbname, 0, 0, pDbname);
114656}
114657
114658/*
114659** Called by the parser to compile an ATTACH statement.
114660**
114661** ATTACH p AS pDbname KEY pKey
114662*/
114663SQLITE_PRIVATEstatic void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
114664 static const FuncDef attach_func = {
114665 3, /* nArg */
114666 SQLITE_UTF81, /* funcFlags */
114667 0, /* pUserData */
114668 0, /* pNext */
114669 attachFunc, /* xSFunc */
114670 0, /* xFinalize */
114671 0, 0, /* xValue, xInverse */
114672 "sqlite_attach", /* zName */
114673 {0}
114674 };
114675 codeAttach(pParse, SQLITE_ATTACH24, &attach_func, p, p, pDbname, pKey);
114676}
114677#endif /* SQLITE_OMIT_ATTACH */
114678
114679/*
114680** Expression callback used by sqlite3FixAAAA() routines.
114681*/
114682static int fixExprCb(Walker *p, Expr *pExpr){
114683 DbFixer *pFix = p->u.pFix;
114684 if( !pFix->bTemp ) ExprSetProperty(pExpr, EP_FromDDL)(pExpr)->flags|=(0x40000000);
114685 if( pExpr->op==TK_VARIABLE156 ){
114686 if( pFix->pParse->db->init.busy ){
114687 pExpr->op = TK_NULL121;
114688 }else{
114689 sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
114690 return WRC_Abort2;
114691 }
114692 }
114693 return WRC_Continue0;
114694}
114695
114696/*
114697** Select callback used by sqlite3FixAAAA() routines.
114698*/
114699static int fixSelectCb(Walker *p, Select *pSelect){
114700 DbFixer *pFix = p->u.pFix;
114701 int i;
114702 SrcItem *pItem;
114703 sqlite3 *db = pFix->pParse->db;
114704 int iDb = sqlite3FindDbName(db, pFix->zDb);
114705 SrcList *pList = pSelect->pSrc;
114706
114707 if( NEVER(pList==0)(pList==0) ) return WRC_Continue0;
114708 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
114709 if( pFix->bTemp==0 ){
114710 if( pItem->zDatabase ){
114711 if( iDb!=sqlite3FindDbName(db, pItem->zDatabase) ){
114712 sqlite3ErrorMsg(pFix->pParse,
114713 "%s %T cannot reference objects in database %s",
114714 pFix->zType, pFix->pName, pItem->zDatabase);
114715 return WRC_Abort2;
114716 }
114717 sqlite3DbFree(db, pItem->zDatabase);
114718 pItem->zDatabase = 0;
114719 pItem->fg.notCte = 1;
114720 }
114721 pItem->pSchema = pFix->pSchema;
114722 pItem->fg.fromDDL = 1;
114723 }
114724#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
114725 if( pList->a[i].fg.isUsing==0
114726 && sqlite3WalkExpr(&pFix->w, pList->a[i].u3.pOn)
114727 ){
114728 return WRC_Abort2;
114729 }
114730#endif
114731 }
114732 if( pSelect->pWith ){
114733 for(i=0; i<pSelect->pWith->nCte; i++){
114734 if( sqlite3WalkSelect(p, pSelect->pWith->a[i].pSelect) ){
114735 return WRC_Abort2;
114736 }
114737 }
114738 }
114739 return WRC_Continue0;
114740}
114741
114742/*
114743** Initialize a DbFixer structure. This routine must be called prior
114744** to passing the structure to one of the sqliteFixAAAA() routines below.
114745*/
114746SQLITE_PRIVATEstatic void sqlite3FixInit(
114747 DbFixer *pFix, /* The fixer to be initialized */
114748 Parse *pParse, /* Error messages will be written here */
114749 int iDb, /* This is the database that must be used */
114750 const char *zType, /* "view", "trigger", or "index" */
114751 const Token *pName /* Name of the view, trigger, or index */
114752){
114753 sqlite3 *db = pParse->db;
114754 assert( db->nDb>iDb )((void) (0));
114755 pFix->pParse = pParse;
114756 pFix->zDb = db->aDb[iDb].zDbSName;
114757 pFix->pSchema = db->aDb[iDb].pSchema;
114758 pFix->zType = zType;
114759 pFix->pName = pName;
114760 pFix->bTemp = (iDb==1);
114761 pFix->w.pParse = pParse;
114762 pFix->w.xExprCallback = fixExprCb;
114763 pFix->w.xSelectCallback = fixSelectCb;
114764 pFix->w.xSelectCallback2 = sqlite3WalkWinDefnDummyCallback;
114765 pFix->w.walkerDepth = 0;
114766 pFix->w.eCode = 0;
114767 pFix->w.u.pFix = pFix;
114768}
114769
114770/*
114771** The following set of routines walk through the parse tree and assign
114772** a specific database to all table references where the database name
114773** was left unspecified in the original SQL statement. The pFix structure
114774** must have been initialized by a prior call to sqlite3FixInit().
114775**
114776** These routines are used to make sure that an index, trigger, or
114777** view in one database does not refer to objects in a different database.
114778** (Exception: indices, triggers, and views in the TEMP database are
114779** allowed to refer to anything.) If a reference is explicitly made
114780** to an object in a different database, an error message is added to
114781** pParse->zErrMsg and these routines return non-zero. If everything
114782** checks out, these routines return 0.
114783*/
114784SQLITE_PRIVATEstatic int sqlite3FixSrcList(
114785 DbFixer *pFix, /* Context of the fixation */
114786 SrcList *pList /* The Source list to check and modify */
114787){
114788 int res = 0;
114789 if( pList ){
114790 Select s;
114791 memset(&s, 0, sizeof(s));
114792 s.pSrc = pList;
114793 res = sqlite3WalkSelect(&pFix->w, &s);
114794 }
114795 return res;
114796}
114797#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
114798SQLITE_PRIVATEstatic int sqlite3FixSelect(
114799 DbFixer *pFix, /* Context of the fixation */
114800 Select *pSelect /* The SELECT statement to be fixed to one database */
114801){
114802 return sqlite3WalkSelect(&pFix->w, pSelect);
114803}
114804SQLITE_PRIVATEstatic int sqlite3FixExpr(
114805 DbFixer *pFix, /* Context of the fixation */
114806 Expr *pExpr /* The expression to be fixed to one database */
114807){
114808 return sqlite3WalkExpr(&pFix->w, pExpr);
114809}
114810#endif
114811
114812#ifndef SQLITE_OMIT_TRIGGER
114813SQLITE_PRIVATEstatic int sqlite3FixTriggerStep(
114814 DbFixer *pFix, /* Context of the fixation */
114815 TriggerStep *pStep /* The trigger step be fixed to one database */
114816){
114817 while( pStep ){
114818 if( sqlite3WalkSelect(&pFix->w, pStep->pSelect)
114819 || sqlite3WalkExpr(&pFix->w, pStep->pWhere)
114820 || sqlite3WalkExprList(&pFix->w, pStep->pExprList)
114821 || sqlite3FixSrcList(pFix, pStep->pFrom)
114822 ){
114823 return 1;
114824 }
114825#ifndef SQLITE_OMIT_UPSERT
114826 {
114827 Upsert *pUp;
114828 for(pUp=pStep->pUpsert; pUp; pUp=pUp->pNextUpsert){
114829 if( sqlite3WalkExprList(&pFix->w, pUp->pUpsertTarget)
114830 || sqlite3WalkExpr(&pFix->w, pUp->pUpsertTargetWhere)
114831 || sqlite3WalkExprList(&pFix->w, pUp->pUpsertSet)
114832 || sqlite3WalkExpr(&pFix->w, pUp->pUpsertWhere)
114833 ){
114834 return 1;
114835 }
114836 }
114837 }
114838#endif
114839 pStep = pStep->pNext;
114840 }
114841
114842 return 0;
114843}
114844#endif
114845
114846/************** End of attach.c **********************************************/
114847/************** Begin file auth.c ********************************************/
114848/*
114849** 2003 January 11
114850**
114851** The author disclaims copyright to this source code. In place of
114852** a legal notice, here is a blessing:
114853**
114854** May you do good and not evil.
114855** May you find forgiveness for yourself and forgive others.
114856** May you share freely, never taking more than you give.
114857**
114858*************************************************************************
114859** This file contains code used to implement the sqlite3_set_authorizer()
114860** API. This facility is an optional feature of the library. Embedded
114861** systems that do not need this facility may omit it by recompiling
114862** the library with -DSQLITE_OMIT_AUTHORIZATION=1
114863*/
114864/* #include "sqliteInt.h" */
114865
114866/*
114867** All of the code in this file may be omitted by defining a single
114868** macro.
114869*/
114870#ifndef SQLITE_OMIT_AUTHORIZATION
114871
114872/*
114873** Set or clear the access authorization function.
114874**
114875** The access authorization function is be called during the compilation
114876** phase to verify that the user has read and/or write access permission on
114877** various fields of the database. The first argument to the auth function
114878** is a copy of the 3rd argument to this routine. The second argument
114879** to the auth function is one of these constants:
114880**
114881** SQLITE_CREATE_INDEX
114882** SQLITE_CREATE_TABLE
114883** SQLITE_CREATE_TEMP_INDEX
114884** SQLITE_CREATE_TEMP_TABLE
114885** SQLITE_CREATE_TEMP_TRIGGER
114886** SQLITE_CREATE_TEMP_VIEW
114887** SQLITE_CREATE_TRIGGER
114888** SQLITE_CREATE_VIEW
114889** SQLITE_DELETE
114890** SQLITE_DROP_INDEX
114891** SQLITE_DROP_TABLE
114892** SQLITE_DROP_TEMP_INDEX
114893** SQLITE_DROP_TEMP_TABLE
114894** SQLITE_DROP_TEMP_TRIGGER
114895** SQLITE_DROP_TEMP_VIEW
114896** SQLITE_DROP_TRIGGER
114897** SQLITE_DROP_VIEW
114898** SQLITE_INSERT
114899** SQLITE_PRAGMA
114900** SQLITE_READ
114901** SQLITE_SELECT
114902** SQLITE_TRANSACTION
114903** SQLITE_UPDATE
114904**
114905** The third and fourth arguments to the auth function are the name of
114906** the table and the column that are being accessed. The auth function
114907** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE. If
114908** SQLITE_OK is returned, it means that access is allowed. SQLITE_DENY
114909** means that the SQL statement will never-run - the sqlite3_exec() call
114910** will return with an error. SQLITE_IGNORE means that the SQL statement
114911** should run but attempts to read the specified column will return NULL
114912** and attempts to write the column will be ignored.
114913**
114914** Setting the auth function to NULL disables this hook. The default
114915** setting of the auth function is NULL.
114916*/
114917SQLITE_API int sqlite3_set_authorizer(
114918 sqlite3 *db,
114919 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
114920 void *pArg
114921){
114922#ifdef SQLITE_ENABLE_API_ARMOR1
114923 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(114923);
114924#endif
114925 sqlite3_mutex_enter(db->mutex);
114926 db->xAuth = (sqlite3_xauth)xAuth;
114927 db->pAuthArg = pArg;
114928 if( db->xAuth ) sqlite3ExpirePreparedStatements(db, 1);
114929 sqlite3_mutex_leave(db->mutex);
114930 return SQLITE_OK0;
114931}
114932
114933/*
114934** Write an error message into pParse->zErrMsg that explains that the
114935** user-supplied authorization function returned an illegal value.
114936*/
114937static void sqliteAuthBadReturnCode(Parse *pParse){
114938 sqlite3ErrorMsg(pParse, "authorizer malfunction");
114939 pParse->rc = SQLITE_ERROR1;
114940}
114941
114942/*
114943** Invoke the authorization callback for permission to read column zCol from
114944** table zTab in database zDb. This function assumes that an authorization
114945** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
114946**
114947** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
114948** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
114949** is treated as SQLITE_DENY. In this case an error is left in pParse.
114950*/
114951SQLITE_PRIVATEstatic int sqlite3AuthReadCol(
114952 Parse *pParse, /* The parser context */
114953 const char *zTab, /* Table name */
114954 const char *zCol, /* Column name */
114955 int iDb /* Index of containing database. */
114956){
114957 sqlite3 *db = pParse->db; /* Database handle */
114958 char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
114959 int rc; /* Auth callback return code */
114960
114961 if( db->init.busy ) return SQLITE_OK0;
114962 rc = db->xAuth(db->pAuthArg, SQLITE_READ20, zTab,zCol,zDb,pParse->zAuthContext
114963#ifdef SQLITE_USER_AUTHENTICATION
114964 ,db->auth.zAuthUser
114965#endif
114966 );
114967 if( rc==SQLITE_DENY1 ){
114968 char *z = sqlite3_mprintf("%s.%s", zTab, zCol);
114969 if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf("%s.%z", zDb, z);
114970 sqlite3ErrorMsg(pParse, "access to %z is prohibited", z);
114971 pParse->rc = SQLITE_AUTH23;
114972 }else if( rc!=SQLITE_IGNORE2 && rc!=SQLITE_OK0 ){
114973 sqliteAuthBadReturnCode(pParse);
114974 }
114975 return rc;
114976}
114977
114978/*
114979** The pExpr should be a TK_COLUMN expression. The table referred to
114980** is in pTabList or else it is the NEW or OLD table of a trigger.
114981** Check to see if it is OK to read this particular column.
114982**
114983** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN
114984** instruction into a TK_NULL. If the auth function returns SQLITE_DENY,
114985** then generate an error.
114986*/
114987SQLITE_PRIVATEstatic void sqlite3AuthRead(
114988 Parse *pParse, /* The parser context */
114989 Expr *pExpr, /* The expression to check authorization on */
114990 Schema *pSchema, /* The schema of the expression */
114991 SrcList *pTabList /* All table that pExpr might refer to */
114992){
114993 Table *pTab = 0; /* The table being read */
114994 const char *zCol; /* Name of the column of the table */
114995 int iSrc; /* Index in pTabList->a[] of table being read */
114996 int iDb; /* The index of the database the expression refers to */
114997 int iCol; /* Index of column in table */
114998
114999 assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER )((void) (0));
115000 assert( !IN_RENAME_OBJECT )((void) (0));
115001 assert( pParse->db->xAuth!=0 )((void) (0));
115002 iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
115003 if( iDb<0 ){
115004 /* An attempt to read a column out of a subquery or other
115005 ** temporary table. */
115006 return;
115007 }
115008
115009 if( pExpr->op==TK_TRIGGER77 ){
115010 pTab = pParse->pTriggerTab;
115011 }else{
115012 assert( pTabList )((void) (0));
115013 for(iSrc=0; iSrc<pTabList->nSrc; iSrc++){
115014 if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
115015 pTab = pTabList->a[iSrc].pTab;
115016 break;
115017 }
115018 }
115019 }
115020 iCol = pExpr->iColumn;
115021 if( pTab==0 ) return;
115022
115023 if( iCol>=0 ){
115024 assert( iCol<pTab->nCol )((void) (0));
115025 zCol = pTab->aCol[iCol].zCnName;
115026 }else if( pTab->iPKey>=0 ){
115027 assert( pTab->iPKey<pTab->nCol )((void) (0));
115028 zCol = pTab->aCol[pTab->iPKey].zCnName;
115029 }else{
115030 zCol = "ROWID";
115031 }
115032 assert( iDb>=0 && iDb<pParse->db->nDb )((void) (0));
115033 if( SQLITE_IGNORE2==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
115034 pExpr->op = TK_NULL121;
115035 }
115036}
115037
115038/*
115039** Do an authorization check using the code and arguments given. Return
115040** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY. If SQLITE_DENY
115041** is returned, then the error count and error message in pParse are
115042** modified appropriately.
115043*/
115044SQLITE_PRIVATEstatic int sqlite3AuthCheck(
115045 Parse *pParse,
115046 int code,
115047 const char *zArg1,
115048 const char *zArg2,
115049 const char *zArg3
115050){
115051 sqlite3 *db = pParse->db;
115052 int rc;
115053
115054 /* Don't do any authorization checks if the database is initialising
115055 ** or if the parser is being invoked from within sqlite3_declare_vtab.
115056 */
115057 assert( !IN_RENAME_OBJECT || db->xAuth==0 )((void) (0));
115058 if( db->xAuth==0 || db->init.busy || IN_SPECIAL_PARSE(pParse->eParseMode!=0) ){
115059 return SQLITE_OK0;
115060 }
115061
115062 /* EVIDENCE-OF: R-43249-19882 The third through sixth parameters to the
115063 ** callback are either NULL pointers or zero-terminated strings that
115064 ** contain additional details about the action to be authorized.
115065 **
115066 ** The following testcase() macros show that any of the 3rd through 6th
115067 ** parameters can be either NULL or a string. */
115068 testcase( zArg1==0 );
115069 testcase( zArg2==0 );
115070 testcase( zArg3==0 );
115071 testcase( pParse->zAuthContext==0 );
115072
115073 rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
115074#ifdef SQLITE_USER_AUTHENTICATION
115075 ,db->auth.zAuthUser
115076#endif
115077 );
115078 if( rc==SQLITE_DENY1 ){
115079 sqlite3ErrorMsg(pParse, "not authorized");
115080 pParse->rc = SQLITE_AUTH23;
115081 }else if( rc!=SQLITE_OK0 && rc!=SQLITE_IGNORE2 ){
115082 rc = SQLITE_DENY1;
115083 sqliteAuthBadReturnCode(pParse);
115084 }
115085 return rc;
115086}
115087
115088/*
115089** Push an authorization context. After this routine is called, the
115090** zArg3 argument to authorization callbacks will be zContext until
115091** popped. Or if pParse==0, this routine is a no-op.
115092*/
115093SQLITE_PRIVATEstatic void sqlite3AuthContextPush(
115094 Parse *pParse,
115095 AuthContext *pContext,
115096 const char *zContext
115097){
115098 assert( pParse )((void) (0));
115099 pContext->pParse = pParse;
115100 pContext->zAuthContext = pParse->zAuthContext;
115101 pParse->zAuthContext = zContext;
115102}
115103
115104/*
115105** Pop an authorization context that was previously pushed
115106** by sqlite3AuthContextPush
115107*/
115108SQLITE_PRIVATEstatic void sqlite3AuthContextPop(AuthContext *pContext){
115109 if( pContext->pParse ){
115110 pContext->pParse->zAuthContext = pContext->zAuthContext;
115111 pContext->pParse = 0;
115112 }
115113}
115114
115115#endif /* SQLITE_OMIT_AUTHORIZATION */
115116
115117/************** End of auth.c ************************************************/
115118/************** Begin file build.c *******************************************/
115119/*
115120** 2001 September 15
115121**
115122** The author disclaims copyright to this source code. In place of
115123** a legal notice, here is a blessing:
115124**
115125** May you do good and not evil.
115126** May you find forgiveness for yourself and forgive others.
115127** May you share freely, never taking more than you give.
115128**
115129*************************************************************************
115130** This file contains C code routines that are called by the SQLite parser
115131** when syntax rules are reduced. The routines in this file handle the
115132** following kinds of SQL syntax:
115133**
115134** CREATE TABLE
115135** DROP TABLE
115136** CREATE INDEX
115137** DROP INDEX
115138** creating ID lists
115139** BEGIN TRANSACTION
115140** COMMIT
115141** ROLLBACK
115142*/
115143/* #include "sqliteInt.h" */
115144
115145#ifndef SQLITE_OMIT_SHARED_CACHE
115146/*
115147** The TableLock structure is only used by the sqlite3TableLock() and
115148** codeTableLocks() functions.
115149*/
115150struct TableLock {
115151 int iDb; /* The database containing the table to be locked */
115152 Pgno iTab; /* The root page of the table to be locked */
115153 u8 isWriteLock; /* True for write lock. False for a read lock */
115154 const char *zLockName; /* Name of the table */
115155};
115156
115157/*
115158** Record the fact that we want to lock a table at run-time.
115159**
115160** The table to be locked has root page iTab and is found in database iDb.
115161** A read or a write lock can be taken depending on isWritelock.
115162**
115163** This routine just records the fact that the lock is desired. The
115164** code to make the lock occur is generated by a later call to
115165** codeTableLocks() which occurs during sqlite3FinishCoding().
115166*/
115167static SQLITE_NOINLINE__attribute__((noinline)) void lockTable(
115168 Parse *pParse, /* Parsing context */
115169 int iDb, /* Index of the database containing the table to lock */
115170 Pgno iTab, /* Root page number of the table to be locked */
115171 u8 isWriteLock, /* True for a write lock */
115172 const char *zName /* Name of the table to be locked */
115173){
115174 Parse *pToplevel;
115175 int i;
115176 int nBytes;
115177 TableLock *p;
115178 assert( iDb>=0 )((void) (0));
115179
115180 pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
115181 for(i=0; i<pToplevel->nTableLock; i++){
115182 p = &pToplevel->aTableLock[i];
115183 if( p->iDb==iDb && p->iTab==iTab ){
115184 p->isWriteLock = (p->isWriteLock || isWriteLock);
115185 return;
115186 }
115187 }
115188
115189 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
115190 pToplevel->aTableLock =
115191 sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
115192 if( pToplevel->aTableLock ){
115193 p = &pToplevel->aTableLock[pToplevel->nTableLock++];
115194 p->iDb = iDb;
115195 p->iTab = iTab;
115196 p->isWriteLock = isWriteLock;
115197 p->zLockName = zName;
115198 }else{
115199 pToplevel->nTableLock = 0;
115200 sqlite3OomFault(pToplevel->db);
115201 }
115202}
115203SQLITE_PRIVATEstatic void sqlite3TableLock(
115204 Parse *pParse, /* Parsing context */
115205 int iDb, /* Index of the database containing the table to lock */
115206 Pgno iTab, /* Root page number of the table to be locked */
115207 u8 isWriteLock, /* True for a write lock */
115208 const char *zName /* Name of the table to be locked */
115209){
115210 if( iDb==1 ) return;
115211 if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return;
115212 lockTable(pParse, iDb, iTab, isWriteLock, zName);
115213}
115214
115215/*
115216** Code an OP_TableLock instruction for each table locked by the
115217** statement (configured by calls to sqlite3TableLock()).
115218*/
115219static void codeTableLocks(Parse *pParse){
115220 int i;
115221 Vdbe *pVdbe = pParse->pVdbe;
115222 assert( pVdbe!=0 )((void) (0));
115223
115224 for(i=0; i<pParse->nTableLock; i++){
115225 TableLock *p = &pParse->aTableLock[i];
115226 int p1 = p->iDb;
115227 sqlite3VdbeAddOp4(pVdbe, OP_TableLock169, p1, p->iTab, p->isWriteLock,
115228 p->zLockName, P4_STATIC(-1));
115229 }
115230}
115231#else
115232 #define codeTableLocks(x)
115233#endif
115234
115235/*
115236** Return TRUE if the given yDbMask object is empty - if it contains no
115237** 1 bits. This routine is used by the DbMaskAllZero() and DbMaskNotZero()
115238** macros when SQLITE_MAX_ATTACHED is greater than 30.
115239*/
115240#if SQLITE_MAX_ATTACHED10>30
115241SQLITE_PRIVATEstatic int sqlite3DbMaskAllZero(yDbMask m){
115242 int i;
115243 for(i=0; i<sizeof(yDbMask); i++) if( m[i] ) return 0;
115244 return 1;
115245}
115246#endif
115247
115248/*
115249** This routine is called after a single SQL statement has been
115250** parsed and a VDBE program to execute that statement has been
115251** prepared. This routine puts the finishing touches on the
115252** VDBE program and resets the pParse structure for the next
115253** parse.
115254**
115255** Note that if an error occurred, it might be the case that
115256** no VDBE code was generated.
115257*/
115258SQLITE_PRIVATEstatic void sqlite3FinishCoding(Parse *pParse){
115259 sqlite3 *db;
115260 Vdbe *v;
115261
115262 assert( pParse->pToplevel==0 )((void) (0));
115263 db = pParse->db;
115264 assert( db->pParse==pParse )((void) (0));
115265 if( pParse->nested ) return;
115266 if( pParse->nErr ){
115267 if( db->mallocFailed ) pParse->rc = SQLITE_NOMEM7;
115268 return;
115269 }
115270 assert( db->mallocFailed==0 )((void) (0));
115271
115272 /* Begin by generating some termination code at the end of the
115273 ** vdbe program
115274 */
115275 v = pParse->pVdbe;
115276 if( v==0 ){
115277 if( db->init.busy ){
115278 pParse->rc = SQLITE_DONE101;
115279 return;
115280 }
115281 v = sqlite3GetVdbe(pParse);
115282 if( v==0 ) pParse->rc = SQLITE_ERROR1;
115283 }
115284 assert( !pParse->isMultiWrite((void) (0))
115285 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort))((void) (0));
115286 if( v ){
115287 if( pParse->bReturning ){
115288 Returning *pReturning = pParse->u1.pReturning;
115289 int addrRewind;
115290 int i;
115291 int reg;
115292
115293 if( pReturning->nRetCol ){
115294 sqlite3VdbeAddOp0(v, OP_FkCheck83);
115295 addrRewind =
115296 sqlite3VdbeAddOp1(v, OP_Rewind35, pReturning->iRetCur);
115297 VdbeCoverage(v);
115298 reg = pReturning->iRetReg;
115299 for(i=0; i<pReturning->nRetCol; i++){
115300 sqlite3VdbeAddOp3(v, OP_Column94, pReturning->iRetCur, i, reg+i);
115301 }
115302 sqlite3VdbeAddOp2(v, OP_ResultRow84, reg, i);
115303 sqlite3VdbeAddOp2(v, OP_Next38, pReturning->iRetCur, addrRewind+1);
115304 VdbeCoverage(v);
115305 sqlite3VdbeJumpHere(v, addrRewind);
115306 }
115307 }
115308 sqlite3VdbeAddOp0(v, OP_Halt70);
115309
115310#if SQLITE_USER_AUTHENTICATION
115311 if( pParse->nTableLock>0 && db->init.busy==0 ){
115312 sqlite3UserAuthInit(db);
115313 if( db->auth.authLevel<UAUTH_User ){
115314 sqlite3ErrorMsg(pParse, "user not authenticated");
115315 pParse->rc = SQLITE_AUTH_USER(23 | (1<<8));
115316 return;
115317 }
115318 }
115319#endif
115320
115321 /* The cookie mask contains one bit for each database file open.
115322 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
115323 ** set for each database that is used. Generate code to start a
115324 ** transaction on each used database and to verify the schema cookie
115325 ** on each used database.
115326 */
115327 if( db->mallocFailed==0
115328 && (DbMaskNonZero(pParse->cookieMask)(pParse->cookieMask)!=0 || pParse->pConstExpr)
115329 ){
115330 int iDb, i;
115331 assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init )((void) (0));
115332 sqlite3VdbeJumpHere(v, 0);
115333 assert( db->nDb>0 )((void) (0));
115334 iDb = 0;
115335 do{
115336 Schema *pSchema;
115337 if( DbMaskTest(pParse->cookieMask, iDb)(((pParse->cookieMask)&(((yDbMask)1)<<(iDb)))!=0
)
==0 ) continue;
115338 sqlite3VdbeUsesBtree(v, iDb);
115339 pSchema = db->aDb[iDb].pSchema;
115340 sqlite3VdbeAddOp4Int(v,
115341 OP_Transaction2, /* Opcode */
115342 iDb, /* P1 */
115343 DbMaskTest(pParse->writeMask,iDb)(((pParse->writeMask)&(((yDbMask)1)<<(iDb)))!=0), /* P2 */
115344 pSchema->schema_cookie, /* P3 */
115345 pSchema->iGeneration /* P4 */
115346 );
115347 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
115348 VdbeComment((v,
115349 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
115350 }while( ++iDb<db->nDb );
115351#ifndef SQLITE_OMIT_VIRTUALTABLE
115352 for(i=0; i<pParse->nVtabLock; i++){
115353 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
115354 sqlite3VdbeAddOp4(v, OP_VBegin170, 0, 0, 0, vtab, P4_VTAB(-11));
115355 }
115356 pParse->nVtabLock = 0;
115357#endif
115358
115359 /* Once all the cookies have been verified and transactions opened,
115360 ** obtain the required table-locks. This is a no-op unless the
115361 ** shared-cache feature is enabled.
115362 */
115363 codeTableLocks(pParse);
115364
115365 /* Initialize any AUTOINCREMENT data structures required.
115366 */
115367 sqlite3AutoincrementBegin(pParse);
115368
115369 /* Code constant expressions that where factored out of inner loops.
115370 **
115371 ** The pConstExpr list might also contain expressions that we simply
115372 ** want to keep around until the Parse object is deleted. Such
115373 ** expressions have iConstExprReg==0. Do not generate code for
115374 ** those expressions, of course.
115375 */
115376 if( pParse->pConstExpr ){
115377 ExprList *pEL = pParse->pConstExpr;
115378 pParse->okConstFactor = 0;
115379 for(i=0; i<pEL->nExpr; i++){
115380 int iReg = pEL->a[i].u.iConstExprReg;
115381 if( iReg>0 ){
115382 sqlite3ExprCode(pParse, pEL->a[i].pExpr, iReg);
115383 }
115384 }
115385 }
115386
115387 if( pParse->bReturning ){
115388 Returning *pRet = pParse->u1.pReturning;
115389 if( pRet->nRetCol ){
115390 sqlite3VdbeAddOp2(v, OP_OpenEphemeral118, pRet->iRetCur, pRet->nRetCol);
115391 }
115392 }
115393
115394 /* Finally, jump back to the beginning of the executable code. */
115395 sqlite3VdbeGoto(v, 1);
115396 }
115397 }
115398
115399 /* Get the VDBE program ready for execution
115400 */
115401 assert( v!=0 || pParse->nErr )((void) (0));
115402 assert( db->mallocFailed==0 || pParse->nErr )((void) (0));
115403 if( pParse->nErr==0 ){
115404 /* A minimum of one cursor is required if autoincrement is used
115405 * See ticket [a696379c1f08866] */
115406 assert( pParse->pAinc==0 || pParse->nTab>0 )((void) (0));
115407 sqlite3VdbeMakeReady(v, pParse);
115408 pParse->rc = SQLITE_DONE101;
115409 }else{
115410 pParse->rc = SQLITE_ERROR1;
115411 }
115412}
115413
115414/*
115415** Run the parser and code generator recursively in order to generate
115416** code for the SQL statement given onto the end of the pParse context
115417** currently under construction. Notes:
115418**
115419** * The final OP_Halt is not appended and other initialization
115420** and finalization steps are omitted because those are handling by the
115421** outermost parser.
115422**
115423** * Built-in SQL functions always take precedence over application-defined
115424** SQL functions. In other words, it is not possible to override a
115425** built-in function.
115426*/
115427SQLITE_PRIVATEstatic void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
115428 va_list ap;
115429 char *zSql;
115430 sqlite3 *db = pParse->db;
115431 u32 savedDbFlags = db->mDbFlags;
115432 char saveBuf[PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken))];
115433
115434 if( pParse->nErr ) return;
115435 assert( pParse->nested<10 )((void) (0)); /* Nesting should only be of limited depth */
115436 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
115437 zSql = sqlite3VMPrintf(db, zFormat, ap);
115438 va_end(ap)__builtin_va_end(ap);
115439 if( zSql==0 ){
115440 /* This can result either from an OOM or because the formatted string
115441 ** exceeds SQLITE_LIMIT_LENGTH. In the latter case, we need to set
115442 ** an error */
115443 if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG18;
115444 pParse->nErr++;
115445 return;
115446 }
115447 pParse->nested++;
115448 memcpy(saveBuf, PARSE_TAIL(pParse)(((char*)(pParse))+__builtin_offsetof(Parse, sLastToken)), PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken)));
115449 memset(PARSE_TAIL(pParse)(((char*)(pParse))+__builtin_offsetof(Parse, sLastToken)), 0, PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken)));
115450 db->mDbFlags |= DBFLAG_PreferBuiltin0x0002;
115451 sqlite3RunParser(pParse, zSql);
115452 db->mDbFlags = savedDbFlags;
115453 sqlite3DbFree(db, zSql);
115454 memcpy(PARSE_TAIL(pParse)(((char*)(pParse))+__builtin_offsetof(Parse, sLastToken)), saveBuf, PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken)));
115455 pParse->nested--;
115456}
115457
115458#if SQLITE_USER_AUTHENTICATION
115459/*
115460** Return TRUE if zTable is the name of the system table that stores the
115461** list of users and their access credentials.
115462*/
115463SQLITE_PRIVATEstatic int sqlite3UserAuthTable(const char *zTable){
115464 return sqlite3_stricmp(zTable, "sqlite_user")==0;
115465}
115466#endif
115467
115468/*
115469** Locate the in-memory structure that describes a particular database
115470** table given the name of that table and (optionally) the name of the
115471** database containing the table. Return NULL if not found.
115472**
115473** If zDatabase is 0, all databases are searched for the table and the
115474** first matching table is returned. (No checking for duplicate table
115475** names is done.) The search order is TEMP first, then MAIN, then any
115476** auxiliary databases added using the ATTACH command.
115477**
115478** See also sqlite3LocateTable().
115479*/
115480SQLITE_PRIVATEstatic Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
115481 Table *p = 0;
115482 int i;
115483
115484 /* All mutexes are required for schema access. Make sure we hold them. */
115485 assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
115486#if SQLITE_USER_AUTHENTICATION
115487 /* Only the admin user is allowed to know that the sqlite_user table
115488 ** exists */
115489 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
115490 return 0;
115491 }
115492#endif
115493 if( zDatabase ){
115494 for(i=0; i<db->nDb; i++){
115495 if( sqlite3StrICmp(zDatabase, db->aDb[i].zDbSName)==0 ) break;
115496 }
115497 if( i>=db->nDb ){
115498 /* No match against the official names. But always match "main"
115499 ** to schema 0 as a legacy fallback. */
115500 if( sqlite3StrICmp(zDatabase,"main")==0 ){
115501 i = 0;
115502 }else{
115503 return 0;
115504 }
115505 }
115506 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
115507 if( p==0 && sqlite3StrNICmpsqlite3_strnicmp(zName, "sqlite_", 7)==0 ){
115508 if( i==1 ){
115509 if( sqlite3StrICmp(zName+7, &PREFERRED_TEMP_SCHEMA_TABLE"sqlite_temp_schema"[7])==0
115510 || sqlite3StrICmp(zName+7, &PREFERRED_SCHEMA_TABLE"sqlite_schema"[7])==0
115511 || sqlite3StrICmp(zName+7, &LEGACY_SCHEMA_TABLE"sqlite_master"[7])==0
115512 ){
115513 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
115514 LEGACY_TEMP_SCHEMA_TABLE"sqlite_temp_master");
115515 }
115516 }else{
115517 if( sqlite3StrICmp(zName+7, &PREFERRED_SCHEMA_TABLE"sqlite_schema"[7])==0 ){
115518 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash,
115519 LEGACY_SCHEMA_TABLE"sqlite_master");
115520 }
115521 }
115522 }
115523 }else{
115524 /* Match against TEMP first */
115525 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, zName);
115526 if( p ) return p;
115527 /* The main database is second */
115528 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, zName);
115529 if( p ) return p;
115530 /* Attached databases are in order of attachment */
115531 for(i=2; i<db->nDb; i++){
115532 assert( sqlite3SchemaMutexHeld(db, i, 0) )((void) (0));
115533 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
115534 if( p ) break;
115535 }
115536 if( p==0 && sqlite3StrNICmpsqlite3_strnicmp(zName, "sqlite_", 7)==0 ){
115537 if( sqlite3StrICmp(zName+7, &PREFERRED_SCHEMA_TABLE"sqlite_schema"[7])==0 ){
115538 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, LEGACY_SCHEMA_TABLE"sqlite_master");
115539 }else if( sqlite3StrICmp(zName+7, &PREFERRED_TEMP_SCHEMA_TABLE"sqlite_temp_schema"[7])==0 ){
115540 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
115541 LEGACY_TEMP_SCHEMA_TABLE"sqlite_temp_master");
115542 }
115543 }
115544 }
115545 return p;
115546}
115547
115548/*
115549** Locate the in-memory structure that describes a particular database
115550** table given the name of that table and (optionally) the name of the
115551** database containing the table. Return NULL if not found. Also leave an
115552** error message in pParse->zErrMsg.
115553**
115554** The difference between this routine and sqlite3FindTable() is that this
115555** routine leaves an error message in pParse->zErrMsg where
115556** sqlite3FindTable() does not.
115557*/
115558SQLITE_PRIVATEstatic Table *sqlite3LocateTable(
115559 Parse *pParse, /* context in which to report errors */
115560 u32 flags, /* LOCATE_VIEW or LOCATE_NOERR */
115561 const char *zName, /* Name of the table we are looking for */
115562 const char *zDbase /* Name of the database. Might be NULL */
115563){
115564 Table *p;
115565 sqlite3 *db = pParse->db;
115566
115567 /* Read the database schema. If an error occurs, leave an error message
115568 ** and code in pParse and return NULL. */
115569 if( (db->mDbFlags & DBFLAG_SchemaKnownOk0x0010)==0
115570 && SQLITE_OK0!=sqlite3ReadSchema(pParse)
115571 ){
115572 return 0;
115573 }
115574
115575 p = sqlite3FindTable(db, zName, zDbase);
115576 if( p==0 ){
115577#ifndef SQLITE_OMIT_VIRTUALTABLE
115578 /* If zName is the not the name of a table in the schema created using
115579 ** CREATE, then check to see if it is the name of an virtual table that
115580 ** can be an eponymous virtual table. */
115581 if( pParse->disableVtab==0 && db->init.busy==0 ){
115582 Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
115583 if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){
115584 pMod = sqlite3PragmaVtabRegister(db, zName);
115585 }
115586 if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
115587 testcase( pMod->pEpoTab==0 );
115588 return pMod->pEpoTab;
115589 }
115590 }
115591#endif
115592 if( flags & LOCATE_NOERR0x02 ) return 0;
115593 pParse->checkSchema = 1;
115594 }else if( IsVirtual(p)((p)->eTabType==1) && pParse->disableVtab ){
115595 p = 0;
115596 }
115597
115598 if( p==0 ){
115599 const char *zMsg = flags & LOCATE_VIEW0x01 ? "no such view" : "no such table";
115600 if( zDbase ){
115601 sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
115602 }else{
115603 sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
115604 }
115605 }else{
115606 assert( HasRowid(p) || p->iPKey<0 )((void) (0));
115607 }
115608
115609 return p;
115610}
115611
115612/*
115613** Locate the table identified by *p.
115614**
115615** This is a wrapper around sqlite3LocateTable(). The difference between
115616** sqlite3LocateTable() and this function is that this function restricts
115617** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
115618** non-NULL if it is part of a view or trigger program definition. See
115619** sqlite3FixSrcList() for details.
115620*/
115621SQLITE_PRIVATEstatic Table *sqlite3LocateTableItem(
115622 Parse *pParse,
115623 u32 flags,
115624 SrcItem *p
115625){
115626 const char *zDb;
115627 assert( p->pSchema==0 || p->zDatabase==0 )((void) (0));
115628 if( p->pSchema ){
115629 int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
115630 zDb = pParse->db->aDb[iDb].zDbSName;
115631 }else{
115632 zDb = p->zDatabase;
115633 }
115634 return sqlite3LocateTable(pParse, flags, p->zName, zDb);
115635}
115636
115637/*
115638** Return the preferred table name for system tables. Translate legacy
115639** names into the new preferred names, as appropriate.
115640*/
115641SQLITE_PRIVATEstatic const char *sqlite3PreferredTableName(const char *zName){
115642 if( sqlite3StrNICmpsqlite3_strnicmp(zName, "sqlite_", 7)==0 ){
115643 if( sqlite3StrICmp(zName+7, &LEGACY_SCHEMA_TABLE"sqlite_master"[7])==0 ){
115644 return PREFERRED_SCHEMA_TABLE"sqlite_schema";
115645 }
115646 if( sqlite3StrICmp(zName+7, &LEGACY_TEMP_SCHEMA_TABLE"sqlite_temp_master"[7])==0 ){
115647 return PREFERRED_TEMP_SCHEMA_TABLE"sqlite_temp_schema";
115648 }
115649 }
115650 return zName;
115651}
115652
115653/*
115654** Locate the in-memory structure that describes
115655** a particular index given the name of that index
115656** and the name of the database that contains the index.
115657** Return NULL if not found.
115658**
115659** If zDatabase is 0, all databases are searched for the
115660** table and the first matching index is returned. (No checking
115661** for duplicate index names is done.) The search order is
115662** TEMP first, then MAIN, then any auxiliary databases added
115663** using the ATTACH command.
115664*/
115665SQLITE_PRIVATEstatic Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
115666 Index *p = 0;
115667 int i;
115668 /* All mutexes are required for schema access. Make sure we hold them. */
115669 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
115670 for(i=OMIT_TEMPDB0; i<db->nDb; i++){
115671 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
115672 Schema *pSchema = db->aDb[j].pSchema;
115673 assert( pSchema )((void) (0));
115674 if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
115675 assert( sqlite3SchemaMutexHeld(db, j, 0) )((void) (0));
115676 p = sqlite3HashFind(&pSchema->idxHash, zName);
115677 if( p ) break;
115678 }
115679 return p;
115680}
115681
115682/*
115683** Reclaim the memory used by an index
115684*/
115685SQLITE_PRIVATEstatic void sqlite3FreeIndex(sqlite3 *db, Index *p){
115686#ifndef SQLITE_OMIT_ANALYZE
115687 sqlite3DeleteIndexSamples(db, p);
115688#endif
115689 sqlite3ExprDelete(db, p->pPartIdxWhere);
115690 sqlite3ExprListDelete(db, p->aColExpr);
115691 sqlite3DbFree(db, p->zColAff);
115692 if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
115693#ifdef SQLITE_ENABLE_STAT4
115694 sqlite3_free(p->aiRowEst);
115695#endif
115696 sqlite3DbFree(db, p);
115697}
115698
115699/*
115700** For the index called zIdxName which is found in the database iDb,
115701** unlike that index from its Table then remove the index from
115702** the index hash table and free all memory structures associated
115703** with the index.
115704*/
115705SQLITE_PRIVATEstatic void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
115706 Index *pIndex;
115707 Hash *pHash;
115708
115709 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
115710 pHash = &db->aDb[iDb].pSchema->idxHash;
115711 pIndex = sqlite3HashInsert(pHash, zIdxName, 0);
115712 if( ALWAYS(pIndex)(pIndex) ){
115713 if( pIndex->pTable->pIndex==pIndex ){
115714 pIndex->pTable->pIndex = pIndex->pNext;
115715 }else{
115716 Index *p;
115717 /* Justification of ALWAYS(); The index must be on the list of
115718 ** indices. */
115719 p = pIndex->pTable->pIndex;
115720 while( ALWAYS(p)(p) && p->pNext!=pIndex ){ p = p->pNext; }
115721 if( ALWAYS(p && p->pNext==pIndex)(p && p->pNext==pIndex) ){
115722 p->pNext = pIndex->pNext;
115723 }
115724 }
115725 sqlite3FreeIndex(db, pIndex);
115726 }
115727 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
115728}
115729
115730/*
115731** Look through the list of open database files in db->aDb[] and if
115732** any have been closed, remove them from the list. Reallocate the
115733** db->aDb[] structure to a smaller size, if possible.
115734**
115735** Entry 0 (the "main" database) and entry 1 (the "temp" database)
115736** are never candidates for being collapsed.
115737*/
115738SQLITE_PRIVATEstatic void sqlite3CollapseDatabaseArray(sqlite3 *db){
115739 int i, j;
115740 for(i=j=2; i<db->nDb; i++){
115741 struct Db *pDb = &db->aDb[i];
115742 if( pDb->pBt==0 ){
115743 sqlite3DbFree(db, pDb->zDbSName);
115744 pDb->zDbSName = 0;
115745 continue;
115746 }
115747 if( j<i ){
115748 db->aDb[j] = db->aDb[i];
115749 }
115750 j++;
115751 }
115752 db->nDb = j;
115753 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
115754 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
115755 sqlite3DbFree(db, db->aDb);
115756 db->aDb = db->aDbStatic;
115757 }
115758}
115759
115760/*
115761** Reset the schema for the database at index iDb. Also reset the
115762** TEMP schema. The reset is deferred if db->nSchemaLock is not zero.
115763** Deferred resets may be run by calling with iDb<0.
115764*/
115765SQLITE_PRIVATEstatic void sqlite3ResetOneSchema(sqlite3 *db, int iDb){
115766 int i;
115767 assert( iDb<db->nDb )((void) (0));
115768
115769 if( iDb>=0 ){
115770 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
115771 DbSetProperty(db, iDb, DB_ResetWanted)(db)->aDb[iDb].pSchema->schemaFlags|=(0x0008);
115772 DbSetProperty(db, 1, DB_ResetWanted)(db)->aDb[1].pSchema->schemaFlags|=(0x0008);
115773 db->mDbFlags &= ~DBFLAG_SchemaKnownOk0x0010;
115774 }
115775
115776 if( db->nSchemaLock==0 ){
115777 for(i=0; i<db->nDb; i++){
115778 if( DbHasProperty(db, i, DB_ResetWanted)(((db)->aDb[i].pSchema->schemaFlags&(0x0008))==(0x0008
))
){
115779 sqlite3SchemaClear(db->aDb[i].pSchema);
115780 }
115781 }
115782 }
115783}
115784
115785/*
115786** Erase all schema information from all attached databases (including
115787** "main" and "temp") for a single database connection.
115788*/
115789SQLITE_PRIVATEstatic void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){
115790 int i;
115791 sqlite3BtreeEnterAll(db);
115792 for(i=0; i<db->nDb; i++){
115793 Db *pDb = &db->aDb[i];
115794 if( pDb->pSchema ){
115795 if( db->nSchemaLock==0 ){
115796 sqlite3SchemaClear(pDb->pSchema);
115797 }else{
115798 DbSetProperty(db, i, DB_ResetWanted)(db)->aDb[i].pSchema->schemaFlags|=(0x0008);
115799 }
115800 }
115801 }
115802 db->mDbFlags &= ~(DBFLAG_SchemaChange0x0001|DBFLAG_SchemaKnownOk0x0010);
115803 sqlite3VtabUnlockList(db);
115804 sqlite3BtreeLeaveAll(db);
115805 if( db->nSchemaLock==0 ){
115806 sqlite3CollapseDatabaseArray(db);
115807 }
115808}
115809
115810/*
115811** This routine is called when a commit occurs.
115812*/
115813SQLITE_PRIVATEstatic void sqlite3CommitInternalChanges(sqlite3 *db){
115814 db->mDbFlags &= ~DBFLAG_SchemaChange0x0001;
115815}
115816
115817/*
115818** Set the expression associated with a column. This is usually
115819** the DEFAULT value, but might also be the expression that computes
115820** the value for a generated column.
115821*/
115822SQLITE_PRIVATEstatic void sqlite3ColumnSetExpr(
115823 Parse *pParse, /* Parsing context */
115824 Table *pTab, /* The table containing the column */
115825 Column *pCol, /* The column to receive the new DEFAULT expression */
115826 Expr *pExpr /* The new default expression */
115827){
115828 ExprList *pList;
115829 assert( IsOrdinaryTable(pTab) )((void) (0));
115830 pList = pTab->u.tab.pDfltList;
115831 if( pCol->iDflt==0
115832 || NEVER(pList==0)(pList==0)
115833 || NEVER(pList->nExpr<pCol->iDflt)(pList->nExpr<pCol->iDflt)
115834 ){
115835 pCol->iDflt = pList==0 ? 1 : pList->nExpr+1;
115836 pTab->u.tab.pDfltList = sqlite3ExprListAppend(pParse, pList, pExpr);
115837 }else{
115838 sqlite3ExprDelete(pParse->db, pList->a[pCol->iDflt-1].pExpr);
115839 pList->a[pCol->iDflt-1].pExpr = pExpr;
115840 }
115841}
115842
115843/*
115844** Return the expression associated with a column. The expression might be
115845** the DEFAULT clause or the AS clause of a generated column.
115846** Return NULL if the column has no associated expression.
115847*/
115848SQLITE_PRIVATEstatic Expr *sqlite3ColumnExpr(Table *pTab, Column *pCol){
115849 if( pCol->iDflt==0 ) return 0;
115850 if( NEVER(!IsOrdinaryTable(pTab))(!((pTab)->eTabType==0)) ) return 0;
115851 if( NEVER(pTab->u.tab.pDfltList==0)(pTab->u.tab.pDfltList==0) ) return 0;
115852 if( NEVER(pTab->u.tab.pDfltList->nExpr<pCol->iDflt)(pTab->u.tab.pDfltList->nExpr<pCol->iDflt) ) return 0;
115853 return pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr;
115854}
115855
115856/*
115857** Set the collating sequence name for a column.
115858*/
115859SQLITE_PRIVATEstatic void sqlite3ColumnSetColl(
115860 sqlite3 *db,
115861 Column *pCol,
115862 const char *zColl
115863){
115864 i64 nColl;
115865 i64 n;
115866 char *zNew;
115867 assert( zColl!=0 )((void) (0));
115868 n = sqlite3Strlen30(pCol->zCnName) + 1;
115869 if( pCol->colFlags & COLFLAG_HASTYPE0x0004 ){
115870 n += sqlite3Strlen30(pCol->zCnName+n) + 1;
115871 }
115872 nColl = sqlite3Strlen30(zColl) + 1;
115873 zNew = sqlite3DbRealloc(db, pCol->zCnName, nColl+n);
115874 if( zNew ){
115875 pCol->zCnName = zNew;
115876 memcpy(pCol->zCnName + n, zColl, nColl);
115877 pCol->colFlags |= COLFLAG_HASCOLL0x0200;
115878 }
115879}
115880
115881/*
115882** Return the collating squence name for a column
115883*/
115884SQLITE_PRIVATEstatic const char *sqlite3ColumnColl(Column *pCol){
115885 const char *z;
115886 if( (pCol->colFlags & COLFLAG_HASCOLL0x0200)==0 ) return 0;
115887 z = pCol->zCnName;
115888 while( *z ){ z++; }
115889 if( pCol->colFlags & COLFLAG_HASTYPE0x0004 ){
115890 do{ z++; }while( *z );
115891 }
115892 return z+1;
115893}
115894
115895/*
115896** Delete memory allocated for the column names of a table or view (the
115897** Table.aCol[] array).
115898*/
115899SQLITE_PRIVATEstatic void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){
115900 int i;
115901 Column *pCol;
115902 assert( pTable!=0 )((void) (0));
115903 if( (pCol = pTable->aCol)!=0 ){
115904 for(i=0; i<pTable->nCol; i++, pCol++){
115905 assert( pCol->zCnName==0 || pCol->hName==sqlite3StrIHash(pCol->zCnName) )((void) (0));
115906 sqlite3DbFree(db, pCol->zCnName);
115907 }
115908 sqlite3DbFree(db, pTable->aCol);
115909 if( IsOrdinaryTable(pTable)((pTable)->eTabType==0) ){
115910 sqlite3ExprListDelete(db, pTable->u.tab.pDfltList);
115911 }
115912 if( db==0 || db->pnBytesFreed==0 ){
115913 pTable->aCol = 0;
115914 pTable->nCol = 0;
115915 if( IsOrdinaryTable(pTable)((pTable)->eTabType==0) ){
115916 pTable->u.tab.pDfltList = 0;
115917 }
115918 }
115919 }
115920}
115921
115922/*
115923** Remove the memory data structures associated with the given
115924** Table. No changes are made to disk by this routine.
115925**
115926** This routine just deletes the data structure. It does not unlink
115927** the table data structure from the hash table. But it does destroy
115928** memory structures of the indices and foreign keys associated with
115929** the table.
115930**
115931** The db parameter is optional. It is needed if the Table object
115932** contains lookaside memory. (Table objects in the schema do not use
115933** lookaside memory, but some ephemeral Table objects do.) Or the
115934** db parameter can be used with db->pnBytesFreed to measure the memory
115935** used by the Table object.
115936*/
115937static void SQLITE_NOINLINE__attribute__((noinline)) deleteTable(sqlite3 *db, Table *pTable){
115938 Index *pIndex, *pNext;
115939
115940#ifdef SQLITE_DEBUG
115941 /* Record the number of outstanding lookaside allocations in schema Tables
115942 ** prior to doing any free() operations. Since schema Tables do not use
115943 ** lookaside, this number should not change.
115944 **
115945 ** If malloc has already failed, it may be that it failed while allocating
115946 ** a Table object that was going to be marked ephemeral. So do not check
115947 ** that no lookaside memory is used in this case either. */
115948 int nLookaside = 0;
115949 if( db && !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral0x00004000)==0 ){
115950 nLookaside = sqlite3LookasideUsed(db, 0);
115951 }
115952#endif
115953
115954 /* Delete all indices associated with this table. */
115955 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
115956 pNext = pIndex->pNext;
115957 assert( pIndex->pSchema==pTable->pSchema((void) (0))
115958 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) )((void) (0));
115959 if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable)((pTable)->eTabType==1) ){
115960 char *zName = pIndex->zName;
115961 TESTONLY ( Index *pOld = ) sqlite3HashInsert(
115962 &pIndex->pSchema->idxHash, zName, 0
115963 );
115964 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) )((void) (0));
115965 assert( pOld==pIndex || pOld==0 )((void) (0));
115966 }
115967 sqlite3FreeIndex(db, pIndex);
115968 }
115969
115970 if( IsOrdinaryTable(pTable)((pTable)->eTabType==0) ){
115971 sqlite3FkDelete(db, pTable);
115972 }
115973#ifndef SQLITE_OMIT_VIRTUAL_TABLE
115974 else if( IsVirtual(pTable)((pTable)->eTabType==1) ){
115975 sqlite3VtabClear(db, pTable);
115976 }
115977#endif
115978 else{
115979 assert( IsView(pTable) )((void) (0));
115980 sqlite3SelectDelete(db, pTable->u.view.pSelect);
115981 }
115982
115983 /* Delete the Table structure itself.
115984 */
115985 sqlite3DeleteColumnNames(db, pTable);
115986 sqlite3DbFree(db, pTable->zName);
115987 sqlite3DbFree(db, pTable->zColAff);
115988 sqlite3ExprListDelete(db, pTable->pCheck);
115989 sqlite3DbFree(db, pTable);
115990
115991 /* Verify that no lookaside memory was used by schema tables */
115992 assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) )((void) (0));
115993}
115994SQLITE_PRIVATEstatic void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
115995 /* Do not delete the table until the reference count reaches zero. */
115996 if( !pTable ) return;
115997 if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return;
115998 deleteTable(db, pTable);
115999}
116000
116001
116002/*
116003** Unlink the given table from the hash tables and the delete the
116004** table structure with all its indices and foreign keys.
116005*/
116006SQLITE_PRIVATEstatic void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
116007 Table *p;
116008 Db *pDb;
116009
116010 assert( db!=0 )((void) (0));
116011 assert( iDb>=0 && iDb<db->nDb )((void) (0));
116012 assert( zTabName )((void) (0));
116013 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
116014 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */
116015 pDb = &db->aDb[iDb];
116016 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
116017 sqlite3DeleteTable(db, p);
116018 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
116019}
116020
116021/*
116022** Given a token, return a string that consists of the text of that
116023** token. Space to hold the returned string
116024** is obtained from sqliteMalloc() and must be freed by the calling
116025** function.
116026**
116027** Any quotation marks (ex: "name", 'name', [name], or `name`) that
116028** surround the body of the token are removed.
116029**
116030** Tokens are often just pointers into the original SQL text and so
116031** are not \000 terminated and are not persistent. The returned string
116032** is \000 terminated and is persistent.
116033*/
116034SQLITE_PRIVATEstatic char *sqlite3NameFromToken(sqlite3 *db, const Token *pName){
116035 char *zName;
116036 if( pName ){
116037 zName = sqlite3DbStrNDup(db, (const char*)pName->z, pName->n);
116038 sqlite3Dequote(zName);
116039 }else{
116040 zName = 0;
116041 }
116042 return zName;
116043}
116044
116045/*
116046** Open the sqlite_schema table stored in database number iDb for
116047** writing. The table is opened using cursor 0.
116048*/
116049SQLITE_PRIVATEstatic void sqlite3OpenSchemaTable(Parse *p, int iDb){
116050 Vdbe *v = sqlite3GetVdbe(p);
116051 sqlite3TableLock(p, iDb, SCHEMA_ROOT1, 1, LEGACY_SCHEMA_TABLE"sqlite_master");
116052 sqlite3VdbeAddOp4Int(v, OP_OpenWrite113, 0, SCHEMA_ROOT1, iDb, 5);
116053 if( p->nTab==0 ){
116054 p->nTab = 1;
116055 }
116056}
116057
116058/*
116059** Parameter zName points to a nul-terminated buffer containing the name
116060** of a database ("main", "temp" or the name of an attached db). This
116061** function returns the index of the named database in db->aDb[], or
116062** -1 if the named db cannot be found.
116063*/
116064SQLITE_PRIVATEstatic int sqlite3FindDbName(sqlite3 *db, const char *zName){
116065 int i = -1; /* Database number */
116066 if( zName ){
116067 Db *pDb;
116068 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
116069 if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break;
116070 /* "main" is always an acceptable alias for the primary database
116071 ** even if it has been renamed using SQLITE_DBCONFIG_MAINDBNAME. */
116072 if( i==0 && 0==sqlite3_stricmp("main", zName) ) break;
116073 }
116074 }
116075 return i;
116076}
116077
116078/*
116079** The token *pName contains the name of a database (either "main" or
116080** "temp" or the name of an attached db). This routine returns the
116081** index of the named database in db->aDb[], or -1 if the named db
116082** does not exist.
116083*/
116084SQLITE_PRIVATEstatic int sqlite3FindDb(sqlite3 *db, Token *pName){
116085 int i; /* Database number */
116086 char *zName; /* Name we are searching for */
116087 zName = sqlite3NameFromToken(db, pName);
116088 i = sqlite3FindDbName(db, zName);
116089 sqlite3DbFree(db, zName);
116090 return i;
116091}
116092
116093/* The table or view or trigger name is passed to this routine via tokens
116094** pName1 and pName2. If the table name was fully qualified, for example:
116095**
116096** CREATE TABLE xxx.yyy (...);
116097**
116098** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
116099** the table name is not fully qualified, i.e.:
116100**
116101** CREATE TABLE yyy(...);
116102**
116103** Then pName1 is set to "yyy" and pName2 is "".
116104**
116105** This routine sets the *ppUnqual pointer to point at the token (pName1 or
116106** pName2) that stores the unqualified table name. The index of the
116107** database "xxx" is returned.
116108*/
116109SQLITE_PRIVATEstatic int sqlite3TwoPartName(
116110 Parse *pParse, /* Parsing and code generating context */
116111 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */
116112 Token *pName2, /* The "yyy" in the name "xxx.yyy" */
116113 Token **pUnqual /* Write the unqualified object name here */
116114){
116115 int iDb; /* Database holding the object */
116116 sqlite3 *db = pParse->db;
116117
116118 assert( pName2!=0 )((void) (0));
116119 if( pName2->n>0 ){
116120 if( db->init.busy ) {
116121 sqlite3ErrorMsg(pParse, "corrupt database");
116122 return -1;
116123 }
116124 *pUnqual = pName2;
116125 iDb = sqlite3FindDb(db, pName1);
116126 if( iDb<0 ){
116127 sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
116128 return -1;
116129 }
116130 }else{
116131 assert( db->init.iDb==0 || db->init.busy || IN_SPECIAL_PARSE((void) (0))
116132 || (db->mDbFlags & DBFLAG_Vacuum)!=0)((void) (0));
116133 iDb = db->init.iDb;
116134 *pUnqual = pName1;
116135 }
116136 return iDb;
116137}
116138
116139/*
116140** True if PRAGMA writable_schema is ON
116141*/
116142SQLITE_PRIVATEstatic int sqlite3WritableSchema(sqlite3 *db){
116143 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 );
116144 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
116145 SQLITE_WriteSchema );
116146 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
116147 SQLITE_Defensive );
116148 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
116149 (SQLITE_WriteSchema|SQLITE_Defensive) );
116150 return (db->flags&(SQLITE_WriteSchema0x00000001|SQLITE_Defensive0x10000000))==SQLITE_WriteSchema0x00000001;
116151}
116152
116153/*
116154** This routine is used to check if the UTF-8 string zName is a legal
116155** unqualified name for a new schema object (table, index, view or
116156** trigger). All names are legal except those that begin with the string
116157** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
116158** is reserved for internal use.
116159**
116160** When parsing the sqlite_schema table, this routine also checks to
116161** make sure the "type", "name", and "tbl_name" columns are consistent
116162** with the SQL.
116163*/
116164SQLITE_PRIVATEstatic int sqlite3CheckObjectName(
116165 Parse *pParse, /* Parsing context */
116166 const char *zName, /* Name of the object to check */
116167 const char *zType, /* Type of this object */
116168 const char *zTblName /* Parent table name for triggers and indexes */
116169){
116170 sqlite3 *db = pParse->db;
116171 if( sqlite3WritableSchema(db)
116172 || db->init.imposterTable
116173 || !sqlite3Config.bExtraSchemaChecks
116174 ){
116175 /* Skip these error checks for writable_schema=ON */
116176 return SQLITE_OK0;
116177 }
116178 if( db->init.busy ){
116179 if( sqlite3_stricmp(zType, db->init.azInit[0])
116180 || sqlite3_stricmp(zName, db->init.azInit[1])
116181 || sqlite3_stricmp(zTblName, db->init.azInit[2])
116182 ){
116183 sqlite3ErrorMsg(pParse, ""); /* corruptSchema() will supply the error */
116184 return SQLITE_ERROR1;
116185 }
116186 }else{
116187 if( (pParse->nested==0 && 0==sqlite3StrNICmpsqlite3_strnicmp(zName, "sqlite_", 7))
116188 || (sqlite3ReadOnlyShadowTables(db) && sqlite3ShadowTableName(db, zName))
116189 ){
116190 sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s",
116191 zName);
116192 return SQLITE_ERROR1;
116193 }
116194
116195 }
116196 return SQLITE_OK0;
116197}
116198
116199/*
116200** Return the PRIMARY KEY index of a table
116201*/
116202SQLITE_PRIVATEstatic Index *sqlite3PrimaryKeyIndex(Table *pTab){
116203 Index *p;
116204 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p)((p)->idxType==2); p=p->pNext){}
116205 return p;
116206}
116207
116208/*
116209** Convert an table column number into a index column number. That is,
116210** for the column iCol in the table (as defined by the CREATE TABLE statement)
116211** find the (first) offset of that column in index pIdx. Or return -1
116212** if column iCol is not used in index pIdx.
116213*/
116214SQLITE_PRIVATEstatic i16 sqlite3TableColumnToIndex(Index *pIdx, i16 iCol){
116215 int i;
116216 for(i=0; i<pIdx->nColumn; i++){
116217 if( iCol==pIdx->aiColumn[i] ) return i;
116218 }
116219 return -1;
116220}
116221
116222#ifndef SQLITE_OMIT_GENERATED_COLUMNS
116223/* Convert a storage column number into a table column number.
116224**
116225** The storage column number (0,1,2,....) is the index of the value
116226** as it appears in the record on disk. The true column number
116227** is the index (0,1,2,...) of the column in the CREATE TABLE statement.
116228**
116229** The storage column number is less than the table column number if
116230** and only there are VIRTUAL columns to the left.
116231**
116232** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro.
116233*/
116234SQLITE_PRIVATEstatic i16 sqlite3StorageColumnToTable(Table *pTab, i16 iCol){
116235 if( pTab->tabFlags & TF_HasVirtual0x00000020 ){
116236 int i;
116237 for(i=0; i<=iCol; i++){
116238 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020 ) iCol++;
116239 }
116240 }
116241 return iCol;
116242}
116243#endif
116244
116245#ifndef SQLITE_OMIT_GENERATED_COLUMNS
116246/* Convert a table column number into a storage column number.
116247**
116248** The storage column number (0,1,2,....) is the index of the value
116249** as it appears in the record on disk. Or, if the input column is
116250** the N-th virtual column (zero-based) then the storage number is
116251** the number of non-virtual columns in the table plus N.
116252**
116253** The true column number is the index (0,1,2,...) of the column in
116254** the CREATE TABLE statement.
116255**
116256** If the input column is a VIRTUAL column, then it should not appear
116257** in storage. But the value sometimes is cached in registers that
116258** follow the range of registers used to construct storage. This
116259** avoids computing the same VIRTUAL column multiple times, and provides
116260** values for use by OP_Param opcodes in triggers. Hence, if the
116261** input column is a VIRTUAL table, put it after all the other columns.
116262**
116263** In the following, N means "normal column", S means STORED, and
116264** V means VIRTUAL. Suppose the CREATE TABLE has columns like this:
116265**
116266** CREATE TABLE ex(N,S,V,N,S,V,N,S,V);
116267** -- 0 1 2 3 4 5 6 7 8
116268**
116269** Then the mapping from this function is as follows:
116270**
116271** INPUTS: 0 1 2 3 4 5 6 7 8
116272** OUTPUTS: 0 1 6 2 3 7 4 5 8
116273**
116274** So, in other words, this routine shifts all the virtual columns to
116275** the end.
116276**
116277** If SQLITE_OMIT_GENERATED_COLUMNS then there are no virtual columns and
116278** this routine is a no-op macro. If the pTab does not have any virtual
116279** columns, then this routine is no-op that always return iCol. If iCol
116280** is negative (indicating the ROWID column) then this routine return iCol.
116281*/
116282SQLITE_PRIVATEstatic i16 sqlite3TableColumnToStorage(Table *pTab, i16 iCol){
116283 int i;
116284 i16 n;
116285 assert( iCol<pTab->nCol )((void) (0));
116286 if( (pTab->tabFlags & TF_HasVirtual0x00000020)==0 || iCol<0 ) return iCol;
116287 for(i=0, n=0; i<iCol; i++){
116288 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020)==0 ) n++;
116289 }
116290 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020 ){
116291 /* iCol is a virtual column itself */
116292 return pTab->nNVCol + i - n;
116293 }else{
116294 /* iCol is a normal or stored column */
116295 return n;
116296 }
116297}
116298#endif
116299
116300/*
116301** Insert a single OP_JournalMode query opcode in order to force the
116302** prepared statement to return false for sqlite3_stmt_readonly(). This
116303** is used by CREATE TABLE IF NOT EXISTS and similar if the table already
116304** exists, so that the prepared statement for CREATE TABLE IF NOT EXISTS
116305** will return false for sqlite3_stmt_readonly() even if that statement
116306** is a read-only no-op.
116307*/
116308static void sqlite3ForceNotReadOnly(Parse *pParse){
116309 int iReg = ++pParse->nMem;
116310 Vdbe *v = sqlite3GetVdbe(pParse);
116311 if( v ){
116312 sqlite3VdbeAddOp3(v, OP_JournalMode4, 0, iReg, PAGER_JOURNALMODE_QUERY(-1));
116313 sqlite3VdbeUsesBtree(v, 0);
116314 }
116315}
116316
116317/*
116318** Begin constructing a new table representation in memory. This is
116319** the first of several action routines that get called in response
116320** to a CREATE TABLE statement. In particular, this routine is called
116321** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
116322** flag is true if the table should be stored in the auxiliary database
116323** file instead of in the main database file. This is normally the case
116324** when the "TEMP" or "TEMPORARY" keyword occurs in between
116325** CREATE and TABLE.
116326**
116327** The new table record is initialized and put in pParse->pNewTable.
116328** As more of the CREATE TABLE statement is parsed, additional action
116329** routines will be called to add more information to this record.
116330** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
116331** is called to complete the construction of the new table record.
116332*/
116333SQLITE_PRIVATEstatic void sqlite3StartTable(
116334 Parse *pParse, /* Parser context */
116335 Token *pName1, /* First part of the name of the table or view */
116336 Token *pName2, /* Second part of the name of the table or view */
116337 int isTemp, /* True if this is a TEMP table */
116338 int isView, /* True if this is a VIEW */
116339 int isVirtual, /* True if this is a VIRTUAL table */
116340 int noErr /* Do nothing if table already exists */
116341){
116342 Table *pTable;
116343 char *zName = 0; /* The name of the new table */
116344 sqlite3 *db = pParse->db;
116345 Vdbe *v;
116346 int iDb; /* Database number to create the table in */
116347 Token *pName; /* Unqualified name of the table to create */
116348
116349 if( db->init.busy && db->init.newTnum==1 ){
116350 /* Special case: Parsing the sqlite_schema or sqlite_temp_schema schema */
116351 iDb = db->init.iDb;
116352 zName = sqlite3DbStrDup(db, SCHEMA_TABLE(iDb)((!0)&&(iDb==1)?"sqlite_temp_master":"sqlite_master"));
116353 pName = pName1;
116354 }else{
116355 /* The common case */
116356 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
116357 if( iDb<0 ) return;
116358 if( !OMIT_TEMPDB0 && isTemp && pName2->n>0 && iDb!=1 ){
116359 /* If creating a temp table, the name may not be qualified. Unless
116360 ** the database name is "temp" anyway. */
116361 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
116362 return;
116363 }
116364 if( !OMIT_TEMPDB0 && isTemp ) iDb = 1;
116365 zName = sqlite3NameFromToken(db, pName);
116366 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
116367 sqlite3RenameTokenMap(pParse, (void*)zName, pName);
116368 }
116369 }
116370 pParse->sNameToken = *pName;
116371 if( zName==0 ) return;
116372 if( sqlite3CheckObjectName(pParse, zName, isView?"view":"table", zName) ){
116373 goto begin_table_error;
116374 }
116375 if( db->init.iDb==1 ) isTemp = 1;
116376#ifndef SQLITE_OMIT_AUTHORIZATION
116377 assert( isTemp==0 || isTemp==1 )((void) (0));
116378 assert( isView==0 || isView==1 )((void) (0));
116379 {
116380 static const u8 aCode[] = {
116381 SQLITE_CREATE_TABLE2,
116382 SQLITE_CREATE_TEMP_TABLE4,
116383 SQLITE_CREATE_VIEW8,
116384 SQLITE_CREATE_TEMP_VIEW6
116385 };
116386 char *zDb = db->aDb[iDb].zDbSName;
116387 if( sqlite3AuthCheck(pParse, SQLITE_INSERT18, SCHEMA_TABLE(isTemp)((!0)&&(isTemp==1)?"sqlite_temp_master":"sqlite_master"
)
, 0, zDb) ){
116388 goto begin_table_error;
116389 }
116390 if( !isVirtual && sqlite3AuthCheck(pParse, (int)aCode[isTemp+2*isView],
116391 zName, 0, zDb) ){
116392 goto begin_table_error;
116393 }
116394 }
116395#endif
116396
116397 /* Make sure the new table name does not collide with an existing
116398 ** index or table name in the same database. Issue an error message if
116399 ** it does. The exception is if the statement being parsed was passed
116400 ** to an sqlite3_declare_vtab() call. In that case only the column names
116401 ** and types will be used, so there is no need to test for namespace
116402 ** collisions.
116403 */
116404 if( !IN_SPECIAL_PARSE(pParse->eParseMode!=0) ){
116405 char *zDb = db->aDb[iDb].zDbSName;
116406 if( SQLITE_OK0!=sqlite3ReadSchema(pParse) ){
116407 goto begin_table_error;
116408 }
116409 pTable = sqlite3FindTable(db, zName, zDb);
116410 if( pTable ){
116411 if( !noErr ){
116412 sqlite3ErrorMsg(pParse, "%s %T already exists",
116413 (IsView(pTable)((pTable)->eTabType==2)? "view" : "table"), pName);
116414 }else{
116415 assert( !db->init.busy || CORRUPT_DB )((void) (0));
116416 sqlite3CodeVerifySchema(pParse, iDb);
116417 sqlite3ForceNotReadOnly(pParse);
116418 }
116419 goto begin_table_error;
116420 }
116421 if( sqlite3FindIndex(db, zName, zDb)!=0 ){
116422 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
116423 goto begin_table_error;
116424 }
116425 }
116426
116427 pTable = sqlite3DbMallocZero(db, sizeof(Table));
116428 if( pTable==0 ){
116429 assert( db->mallocFailed )((void) (0));
116430 pParse->rc = SQLITE_NOMEM_BKPT7;
116431 pParse->nErr++;
116432 goto begin_table_error;
116433 }
116434 pTable->zName = zName;
116435 pTable->iPKey = -1;
116436 pTable->pSchema = db->aDb[iDb].pSchema;
116437 pTable->nTabRef = 1;
116438#ifdef SQLITE_DEFAULT_ROWEST
116439 pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST);
116440#else
116441 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) )((void) (0));
116442#endif
116443 assert( pParse->pNewTable==0 )((void) (0));
116444 pParse->pNewTable = pTable;
116445
116446 /* Begin generating the code that will insert the table record into
116447 ** the schema table. Note in particular that we must go ahead
116448 ** and allocate the record number for the table entry now. Before any
116449 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
116450 ** indices to be created and the table record must come before the
116451 ** indices. Hence, the record number for the table must be allocated
116452 ** now.
116453 */
116454 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
116455 int addr1;
116456 int fileFormat;
116457 int reg1, reg2, reg3;
116458 /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */
116459 static const char nullRow[] = { 6, 0, 0, 0, 0, 0 };
116460 sqlite3BeginWriteOperation(pParse, 1, iDb);
116461
116462#ifndef SQLITE_OMIT_VIRTUALTABLE
116463 if( isVirtual ){
116464 sqlite3VdbeAddOp0(v, OP_VBegin170);
116465 }
116466#endif
116467
116468 /* If the file format and encoding in the database have not been set,
116469 ** set them now.
116470 */
116471 reg1 = pParse->regRowid = ++pParse->nMem;
116472 reg2 = pParse->regRoot = ++pParse->nMem;
116473 reg3 = ++pParse->nMem;
116474 sqlite3VdbeAddOp3(v, OP_ReadCookie99, iDb, reg3, BTREE_FILE_FORMAT2);
116475 sqlite3VdbeUsesBtree(v, iDb);
116476 addr1 = sqlite3VdbeAddOp1(v, OP_If15, reg3); VdbeCoverage(v);
116477 fileFormat = (db->flags & SQLITE_LegacyFileFmt0x00000002)!=0 ?
116478 1 : SQLITE_MAX_FILE_FORMAT4;
116479 sqlite3VdbeAddOp3(v, OP_SetCookie100, iDb, BTREE_FILE_FORMAT2, fileFormat);
116480 sqlite3VdbeAddOp3(v, OP_SetCookie100, iDb, BTREE_TEXT_ENCODING5, ENC(db)((db)->enc));
116481 sqlite3VdbeJumpHere(v, addr1);
116482
116483 /* This just creates a place-holder record in the sqlite_schema table.
116484 ** The record created does not contain anything yet. It will be replaced
116485 ** by the real entry in code generated at sqlite3EndTable().
116486 **
116487 ** The rowid for the new entry is left in register pParse->regRowid.
116488 ** The root page number of the new table is left in reg pParse->regRoot.
116489 ** The rowid and root page number values are needed by the code that
116490 ** sqlite3EndTable will generate.
116491 */
116492#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
116493 if( isView || isVirtual ){
116494 sqlite3VdbeAddOp2(v, OP_Integer71, 0, reg2);
116495 }else
116496#endif
116497 {
116498 assert( !pParse->bReturning )((void) (0));
116499 pParse->u1.addrCrTab =
116500 sqlite3VdbeAddOp3(v, OP_CreateBtree147, iDb, reg2, BTREE_INTKEY1);
116501 }
116502 sqlite3OpenSchemaTable(pParse, iDb);
116503 sqlite3VdbeAddOp2(v, OP_NewRowid127, 0, reg1);
116504 sqlite3VdbeAddOp4(v, OP_Blob77, 6, reg3, 0, nullRow, P4_STATIC(-1));
116505 sqlite3VdbeAddOp3(v, OP_Insert128, 0, reg3, reg1);
116506 sqlite3VdbeChangeP5(v, OPFLAG_APPEND0x08);
116507 sqlite3VdbeAddOp0(v, OP_Close122);
116508 }
116509
116510 /* Normal (non-error) return. */
116511 return;
116512
116513 /* If an error occurs, we jump here */
116514begin_table_error:
116515 pParse->checkSchema = 1;
116516 sqlite3DbFree(db, zName);
116517 return;
116518}
116519
116520/* Set properties of a table column based on the (magical)
116521** name of the column.
116522*/
116523#if SQLITE_ENABLE_HIDDEN_COLUMNS
116524SQLITE_PRIVATEstatic void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){
116525 if( sqlite3_strnicmp(pCol->zCnName, "__hidden__", 10)==0 ){
116526 pCol->colFlags |= COLFLAG_HIDDEN0x0002;
116527 if( pTab ) pTab->tabFlags |= TF_HasHidden0x00000002;
116528 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN0x0002) ){
116529 pTab->tabFlags |= TF_OOOHidden0x00000400;
116530 }
116531}
116532#endif
116533
116534/*
116535** Name of the special TEMP trigger used to implement RETURNING. The
116536** name begins with "sqlite_" so that it is guaranteed not to collide
116537** with any application-generated triggers.
116538*/
116539#define RETURNING_TRIGGER_NAME"sqlite_returning" "sqlite_returning"
116540
116541/*
116542** Clean up the data structures associated with the RETURNING clause.
116543*/
116544static void sqlite3DeleteReturning(sqlite3 *db, Returning *pRet){
116545 Hash *pHash;
116546 pHash = &(db->aDb[1].pSchema->trigHash);
116547 sqlite3HashInsert(pHash, RETURNING_TRIGGER_NAME"sqlite_returning", 0);
116548 sqlite3ExprListDelete(db, pRet->pReturnEL);
116549 sqlite3DbFree(db, pRet);
116550}
116551
116552/*
116553** Add the RETURNING clause to the parse currently underway.
116554**
116555** This routine creates a special TEMP trigger that will fire for each row
116556** of the DML statement. That TEMP trigger contains a single SELECT
116557** statement with a result set that is the argument of the RETURNING clause.
116558** The trigger has the Trigger.bReturning flag and an opcode of
116559** TK_RETURNING instead of TK_SELECT, so that the trigger code generator
116560** knows to handle it specially. The TEMP trigger is automatically
116561** removed at the end of the parse.
116562**
116563** When this routine is called, we do not yet know if the RETURNING clause
116564** is attached to a DELETE, INSERT, or UPDATE, so construct it as a
116565** RETURNING trigger instead. It will then be converted into the appropriate
116566** type on the first call to sqlite3TriggersExist().
116567*/
116568SQLITE_PRIVATEstatic void sqlite3AddReturning(Parse *pParse, ExprList *pList){
116569 Returning *pRet;
116570 Hash *pHash;
116571 sqlite3 *db = pParse->db;
116572 if( pParse->pNewTrigger ){
116573 sqlite3ErrorMsg(pParse, "cannot use RETURNING in a trigger");
116574 }else{
116575 assert( pParse->bReturning==0 )((void) (0));
116576 }
116577 pParse->bReturning = 1;
116578 pRet = sqlite3DbMallocZero(db, sizeof(*pRet));
116579 if( pRet==0 ){
116580 sqlite3ExprListDelete(db, pList);
116581 return;
116582 }
116583 pParse->u1.pReturning = pRet;
116584 pRet->pParse = pParse;
116585 pRet->pReturnEL = pList;
116586 sqlite3ParserAddCleanup(pParse,
116587 (void(*)(sqlite3*,void*))sqlite3DeleteReturning, pRet);
116588 testcase( pParse->earlyCleanup );
116589 if( db->mallocFailed ) return;
116590 pRet->retTrig.zName = RETURNING_TRIGGER_NAME"sqlite_returning";
116591 pRet->retTrig.op = TK_RETURNING150;
116592 pRet->retTrig.tr_tm = TRIGGER_AFTER2;
116593 pRet->retTrig.bReturning = 1;
116594 pRet->retTrig.pSchema = db->aDb[1].pSchema;
116595 pRet->retTrig.pTabSchema = db->aDb[1].pSchema;
116596 pRet->retTrig.step_list = &pRet->retTStep;
116597 pRet->retTStep.op = TK_RETURNING150;
116598 pRet->retTStep.pTrig = &pRet->retTrig;
116599 pRet->retTStep.pExprList = pList;
116600 pHash = &(db->aDb[1].pSchema->trigHash);
116601 assert( sqlite3HashFind(pHash, RETURNING_TRIGGER_NAME)==0 || pParse->nErr )((void) (0));
116602 if( sqlite3HashInsert(pHash, RETURNING_TRIGGER_NAME"sqlite_returning", &pRet->retTrig)
116603 ==&pRet->retTrig ){
116604 sqlite3OomFault(db);
116605 }
116606}
116607
116608/*
116609** Add a new column to the table currently being constructed.
116610**
116611** The parser calls this routine once for each column declaration
116612** in a CREATE TABLE statement. sqlite3StartTable() gets called
116613** first to get things going. Then this routine is called for each
116614** column.
116615*/
116616SQLITE_PRIVATEstatic void sqlite3AddColumn(Parse *pParse, Token sName, Token sType){
116617 Table *p;
116618 int i;
116619 char *z;
116620 char *zType;
116621 Column *pCol;
116622 sqlite3 *db = pParse->db;
116623 u8 hName;
116624 Column *aNew;
116625 u8 eType = COLTYPE_CUSTOM0;
116626 u8 szEst = 1;
116627 char affinity = SQLITE_AFF_BLOB0x41;
116628
116629 if( (p = pParse->pNewTable)==0 ) return;
116630 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN2] ){
116631 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
116632 return;
116633 }
116634 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ) sqlite3DequoteToken(&sName);
116635
116636 /* Because keywords GENERATE ALWAYS can be converted into indentifiers
116637 ** by the parser, we can sometimes end up with a typename that ends
116638 ** with "generated always". Check for this case and omit the surplus
116639 ** text. */
116640 if( sType.n>=16
116641 && sqlite3_strnicmp(sType.z+(sType.n-6),"always",6)==0
116642 ){
116643 sType.n -= 6;
116644 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--;
116645 if( sType.n>=9
116646 && sqlite3_strnicmp(sType.z+(sType.n-9),"generated",9)==0
116647 ){
116648 sType.n -= 9;
116649 while( sType.n>0 && sqlite3Isspace(sType.z[sType.n-1])(sqlite3CtypeMap[(unsigned char)(sType.z[sType.n-1])]&0x01
)
) sType.n--;
116650 }
116651 }
116652
116653 /* Check for standard typenames. For standard typenames we will
116654 ** set the Column.eType field rather than storing the typename after
116655 ** the column name, in order to save space. */
116656 if( sType.n>=3 ){
116657 sqlite3DequoteToken(&sType);
116658 for(i=0; i<SQLITE_N_STDTYPE6; i++){
116659 if( sType.n==sqlite3StdTypeLen[i]
116660 && sqlite3_strnicmp(sType.z, sqlite3StdType[i], sType.n)==0
116661 ){
116662 sType.n = 0;
116663 eType = i+1;
116664 affinity = sqlite3StdTypeAffinity[i];
116665 if( affinity<=SQLITE_AFF_TEXT0x42 ) szEst = 5;
116666 break;
116667 }
116668 }
116669 }
116670
116671 z = sqlite3DbMallocRaw(db, (i64)sName.n + 1 + (i64)sType.n + (sType.n>0) );
116672 if( z==0 ) return;
116673 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ) sqlite3RenameTokenMap(pParse, (void*)z, &sName);
116674 memcpy(z, sName.z, sName.n);
116675 z[sName.n] = 0;
116676 sqlite3Dequote(z);
116677 hName = sqlite3StrIHash(z);
116678 for(i=0; i<p->nCol; i++){
116679 if( p->aCol[i].hName==hName && sqlite3StrICmp(z, p->aCol[i].zCnName)==0 ){
116680 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
116681 sqlite3DbFree(db, z);
116682 return;
116683 }
116684 }
116685 aNew = sqlite3DbRealloc(db,p->aCol,((i64)p->nCol+1)*sizeof(p->aCol[0]));
116686 if( aNew==0 ){
116687 sqlite3DbFree(db, z);
116688 return;
116689 }
116690 p->aCol = aNew;
116691 pCol = &p->aCol[p->nCol];
116692 memset(pCol, 0, sizeof(p->aCol[0]));
116693 pCol->zCnName = z;
116694 pCol->hName = hName;
116695 sqlite3ColumnPropertiesFromName(p, pCol);
116696
116697 if( sType.n==0 ){
116698 /* If there is no type specified, columns have the default affinity
116699 ** 'BLOB' with a default size of 4 bytes. */
116700 pCol->affinity = affinity;
116701 pCol->eCType = eType;
116702 pCol->szEst = szEst;
116703#ifdef SQLITE_ENABLE_SORTER_REFERENCES
116704 if( affinity==SQLITE_AFF_BLOB0x41 ){
116705 if( 4>=sqlite3GlobalConfigsqlite3Config.szSorterRef ){
116706 pCol->colFlags |= COLFLAG_SORTERREF0x0010;
116707 }
116708 }
116709#endif
116710 }else{
116711 zType = z + sqlite3Strlen30(z) + 1;
116712 memcpy(zType, sType.z, sType.n);
116713 zType[sType.n] = 0;
116714 sqlite3Dequote(zType);
116715 pCol->affinity = sqlite3AffinityType(zType, pCol);
116716 pCol->colFlags |= COLFLAG_HASTYPE0x0004;
116717 }
116718 p->nCol++;
116719 p->nNVCol++;
116720 pParse->constraintName.n = 0;
116721}
116722
116723/*
116724** This routine is called by the parser while in the middle of
116725** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
116726** been seen on a column. This routine sets the notNull flag on
116727** the column currently under construction.
116728*/
116729SQLITE_PRIVATEstatic void sqlite3AddNotNull(Parse *pParse, int onError){
116730 Table *p;
116731 Column *pCol;
116732 p = pParse->pNewTable;
116733 if( p==0 || NEVER(p->nCol<1)(p->nCol<1) ) return;
116734 pCol = &p->aCol[p->nCol-1];
116735 pCol->notNull = (u8)onError;
116736 p->tabFlags |= TF_HasNotNull0x00000800;
116737
116738 /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created
116739 ** on this column. */
116740 if( pCol->colFlags & COLFLAG_UNIQUE0x0008 ){
116741 Index *pIdx;
116742 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
116743 assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None )((void) (0));
116744 if( pIdx->aiColumn[0]==p->nCol-1 ){
116745 pIdx->uniqNotNull = 1;
116746 }
116747 }
116748 }
116749}
116750
116751/*
116752** Scan the column type name zType (length nType) and return the
116753** associated affinity type.
116754**
116755** This routine does a case-independent search of zType for the
116756** substrings in the following table. If one of the substrings is
116757** found, the corresponding affinity is returned. If zType contains
116758** more than one of the substrings, entries toward the top of
116759** the table take priority. For example, if zType is 'BLOBINT',
116760** SQLITE_AFF_INTEGER is returned.
116761**
116762** Substring | Affinity
116763** --------------------------------
116764** 'INT' | SQLITE_AFF_INTEGER
116765** 'CHAR' | SQLITE_AFF_TEXT
116766** 'CLOB' | SQLITE_AFF_TEXT
116767** 'TEXT' | SQLITE_AFF_TEXT
116768** 'BLOB' | SQLITE_AFF_BLOB
116769** 'REAL' | SQLITE_AFF_REAL
116770** 'FLOA' | SQLITE_AFF_REAL
116771** 'DOUB' | SQLITE_AFF_REAL
116772**
116773** If none of the substrings in the above table are found,
116774** SQLITE_AFF_NUMERIC is returned.
116775*/
116776SQLITE_PRIVATEstatic char sqlite3AffinityType(const char *zIn, Column *pCol){
116777 u32 h = 0;
116778 char aff = SQLITE_AFF_NUMERIC0x43;
116779 const char *zChar = 0;
116780
116781 assert( zIn!=0 )((void) (0));
116782 while( zIn[0] ){
116783 h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
116784 zIn++;
116785 if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */
116786 aff = SQLITE_AFF_TEXT0x42;
116787 zChar = zIn;
116788 }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */
116789 aff = SQLITE_AFF_TEXT0x42;
116790 }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */
116791 aff = SQLITE_AFF_TEXT0x42;
116792 }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */
116793 && (aff==SQLITE_AFF_NUMERIC0x43 || aff==SQLITE_AFF_REAL0x45) ){
116794 aff = SQLITE_AFF_BLOB0x41;
116795 if( zIn[0]=='(' ) zChar = zIn;
116796#ifndef SQLITE_OMIT_FLOATING_POINT
116797 }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */
116798 && aff==SQLITE_AFF_NUMERIC0x43 ){
116799 aff = SQLITE_AFF_REAL0x45;
116800 }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */
116801 && aff==SQLITE_AFF_NUMERIC0x43 ){
116802 aff = SQLITE_AFF_REAL0x45;
116803 }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */
116804 && aff==SQLITE_AFF_NUMERIC0x43 ){
116805 aff = SQLITE_AFF_REAL0x45;
116806#endif
116807 }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */
116808 aff = SQLITE_AFF_INTEGER0x44;
116809 break;
116810 }
116811 }
116812
116813 /* If pCol is not NULL, store an estimate of the field size. The
116814 ** estimate is scaled so that the size of an integer is 1. */
116815 if( pCol ){
116816 int v = 0; /* default size is approx 4 bytes */
116817 if( aff<SQLITE_AFF_NUMERIC0x43 ){
116818 if( zChar ){
116819 while( zChar[0] ){
116820 if( sqlite3Isdigit(zChar[0])(sqlite3CtypeMap[(unsigned char)(zChar[0])]&0x04) ){
116821 /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
116822 sqlite3GetInt32(zChar, &v);
116823 break;
116824 }
116825 zChar++;
116826 }
116827 }else{
116828 v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
116829 }
116830 }
116831#ifdef SQLITE_ENABLE_SORTER_REFERENCES
116832 if( v>=sqlite3GlobalConfigsqlite3Config.szSorterRef ){
116833 pCol->colFlags |= COLFLAG_SORTERREF0x0010;
116834 }
116835#endif
116836 v = v/4 + 1;
116837 if( v>255 ) v = 255;
116838 pCol->szEst = v;
116839 }
116840 return aff;
116841}
116842
116843/*
116844** The expression is the default value for the most recently added column
116845** of the table currently under construction.
116846**
116847** Default value expressions must be constant. Raise an exception if this
116848** is not the case.
116849**
116850** This routine is called by the parser while in the middle of
116851** parsing a CREATE TABLE statement.
116852*/
116853SQLITE_PRIVATEstatic void sqlite3AddDefaultValue(
116854 Parse *pParse, /* Parsing context */
116855 Expr *pExpr, /* The parsed expression of the default value */
116856 const char *zStart, /* Start of the default value text */
116857 const char *zEnd /* First character past end of defaut value text */
116858){
116859 Table *p;
116860 Column *pCol;
116861 sqlite3 *db = pParse->db;
116862 p = pParse->pNewTable;
116863 if( p!=0 ){
116864 int isInit = db->init.busy && db->init.iDb!=1;
116865 pCol = &(p->aCol[p->nCol-1]);
116866 if( !sqlite3ExprIsConstantOrFunction(pExpr, isInit) ){
116867 sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
116868 pCol->zCnName);
116869#ifndef SQLITE_OMIT_GENERATED_COLUMNS
116870 }else if( pCol->colFlags & COLFLAG_GENERATED0x0060 ){
116871 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
116872 testcase( pCol->colFlags & COLFLAG_STORED );
116873 sqlite3ErrorMsg(pParse, "cannot use DEFAULT on a generated column");
116874#endif
116875 }else{
116876 /* A copy of pExpr is used instead of the original, as pExpr contains
116877 ** tokens that point to volatile memory.
116878 */
116879 Expr x, *pDfltExpr;
116880 memset(&x, 0, sizeof(x));
116881 x.op = TK_SPAN181;
116882 x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd);
116883 x.pLeft = pExpr;
116884 x.flags = EP_Skip0x002000;
116885 pDfltExpr = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE0x0001);
116886 sqlite3DbFree(db, x.u.zToken);
116887 sqlite3ColumnSetExpr(pParse, p, pCol, pDfltExpr);
116888 }
116889 }
116890 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
116891 sqlite3RenameExprUnmap(pParse, pExpr);
116892 }
116893 sqlite3ExprDelete(db, pExpr);
116894}
116895
116896/*
116897** Backwards Compatibility Hack:
116898**
116899** Historical versions of SQLite accepted strings as column names in
116900** indexes and PRIMARY KEY constraints and in UNIQUE constraints. Example:
116901**
116902** CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim)
116903** CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC);
116904**
116905** This is goofy. But to preserve backwards compatibility we continue to
116906** accept it. This routine does the necessary conversion. It converts
116907** the expression given in its argument from a TK_STRING into a TK_ID
116908** if the expression is just a TK_STRING with an optional COLLATE clause.
116909** If the expression is anything other than TK_STRING, the expression is
116910** unchanged.
116911*/
116912static void sqlite3StringToId(Expr *p){
116913 if( p->op==TK_STRING117 ){
116914 p->op = TK_ID59;
116915 }else if( p->op==TK_COLLATE113 && p->pLeft->op==TK_STRING117 ){
116916 p->pLeft->op = TK_ID59;
116917 }
116918}
116919
116920/*
116921** Tag the given column as being part of the PRIMARY KEY
116922*/
116923static void makeColumnPartOfPrimaryKey(Parse *pParse, Column *pCol){
116924 pCol->colFlags |= COLFLAG_PRIMKEY0x0001;
116925#ifndef SQLITE_OMIT_GENERATED_COLUMNS
116926 if( pCol->colFlags & COLFLAG_GENERATED0x0060 ){
116927 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
116928 testcase( pCol->colFlags & COLFLAG_STORED );
116929 sqlite3ErrorMsg(pParse,
116930 "generated columns cannot be part of the PRIMARY KEY");
116931 }
116932#endif
116933}
116934
116935/*
116936** Designate the PRIMARY KEY for the table. pList is a list of names
116937** of columns that form the primary key. If pList is NULL, then the
116938** most recently added column of the table is the primary key.
116939**
116940** A table can have at most one primary key. If the table already has
116941** a primary key (and this is the second primary key) then create an
116942** error.
116943**
116944** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
116945** then we will try to use that column as the rowid. Set the Table.iPKey
116946** field of the table under construction to be the index of the
116947** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
116948** no INTEGER PRIMARY KEY.
116949**
116950** If the key is not an INTEGER PRIMARY KEY, then create a unique
116951** index for the key. No index is created for INTEGER PRIMARY KEYs.
116952*/
116953SQLITE_PRIVATEstatic void sqlite3AddPrimaryKey(
116954 Parse *pParse, /* Parsing context */
116955 ExprList *pList, /* List of field names to be indexed */
116956 int onError, /* What to do with a uniqueness conflict */
116957 int autoInc, /* True if the AUTOINCREMENT keyword is present */
116958 int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */
116959){
116960 Table *pTab = pParse->pNewTable;
116961 Column *pCol = 0;
116962 int iCol = -1, i;
116963 int nTerm;
116964 if( pTab==0 ) goto primary_key_exit;
116965 if( pTab->tabFlags & TF_HasPrimaryKey0x00000004 ){
116966 sqlite3ErrorMsg(pParse,
116967 "table \"%s\" has more than one primary key", pTab->zName);
116968 goto primary_key_exit;
116969 }
116970 pTab->tabFlags |= TF_HasPrimaryKey0x00000004;
116971 if( pList==0 ){
116972 iCol = pTab->nCol - 1;
116973 pCol = &pTab->aCol[iCol];
116974 makeColumnPartOfPrimaryKey(pParse, pCol);
116975 nTerm = 1;
116976 }else{
116977 nTerm = pList->nExpr;
116978 for(i=0; i<nTerm; i++){
116979 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr);
116980 assert( pCExpr!=0 )((void) (0));
116981 sqlite3StringToId(pCExpr);
116982 if( pCExpr->op==TK_ID59 ){
116983 const char *zCName;
116984 assert( !ExprHasProperty(pCExpr, EP_IntValue) )((void) (0));
116985 zCName = pCExpr->u.zToken;
116986 for(iCol=0; iCol<pTab->nCol; iCol++){
116987 if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zCnName)==0 ){
116988 pCol = &pTab->aCol[iCol];
116989 makeColumnPartOfPrimaryKey(pParse, pCol);
116990 break;
116991 }
116992 }
116993 }
116994 }
116995 }
116996 if( nTerm==1
116997 && pCol
116998 && pCol->eCType==COLTYPE_INTEGER4
116999 && sortOrder!=SQLITE_SO_DESC1
117000 ){
117001 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && pList ){
117002 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr);
117003 sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr);
117004 }
117005 pTab->iPKey = iCol;
117006 pTab->keyConf = (u8)onError;
117007 assert( autoInc==0 || autoInc==1 )((void) (0));
117008 pTab->tabFlags |= autoInc*TF_Autoincrement0x00000008;
117009 if( pList ) pParse->iPkSortOrder = pList->a[0].fg.sortFlags;
117010 (void)sqlite3HasExplicitNulls(pParse, pList);
117011 }else if( autoInc ){
117012#ifndef SQLITE_OMIT_AUTOINCREMENT
117013 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
117014 "INTEGER PRIMARY KEY");
117015#endif
117016 }else{
117017 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
117018 0, sortOrder, 0, SQLITE_IDXTYPE_PRIMARYKEY2);
117019 pList = 0;
117020 }
117021
117022primary_key_exit:
117023 sqlite3ExprListDelete(pParse->db, pList);
117024 return;
117025}
117026
117027/*
117028** Add a new CHECK constraint to the table currently under construction.
117029*/
117030SQLITE_PRIVATEstatic void sqlite3AddCheckConstraint(
117031 Parse *pParse, /* Parsing context */
117032 Expr *pCheckExpr, /* The check expression */
117033 const char *zStart, /* Opening "(" */
117034 const char *zEnd /* Closing ")" */
117035){
117036#ifndef SQLITE_OMIT_CHECK
117037 Table *pTab = pParse->pNewTable;
117038 sqlite3 *db = pParse->db;
117039 if( pTab && !IN_DECLARE_VTAB(pParse->eParseMode==1)
117040 && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
117041 ){
117042 pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
117043 if( pParse->constraintName.n ){
117044 sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
117045 }else{
117046 Token t;
117047 for(zStart++; sqlite3Isspace(zStart[0])(sqlite3CtypeMap[(unsigned char)(zStart[0])]&0x01); zStart++){}
117048 while( sqlite3Isspace(zEnd[-1])(sqlite3CtypeMap[(unsigned char)(zEnd[-1])]&0x01) ){ zEnd--; }
117049 t.z = zStart;
117050 t.n = (int)(zEnd - t.z);
117051 sqlite3ExprListSetName(pParse, pTab->pCheck, &t, 1);
117052 }
117053 }else
117054#endif
117055 {
117056 sqlite3ExprDelete(pParse->db, pCheckExpr);
117057 }
117058}
117059
117060/*
117061** Set the collation function of the most recently parsed table column
117062** to the CollSeq given.
117063*/
117064SQLITE_PRIVATEstatic void sqlite3AddCollateType(Parse *pParse, Token *pToken){
117065 Table *p;
117066 int i;
117067 char *zColl; /* Dequoted name of collation sequence */
117068 sqlite3 *db;
117069
117070 if( (p = pParse->pNewTable)==0 || IN_RENAME_OBJECT(pParse->eParseMode>=2) ) return;
117071 i = p->nCol-1;
117072 db = pParse->db;
117073 zColl = sqlite3NameFromToken(db, pToken);
117074 if( !zColl ) return;
117075
117076 if( sqlite3LocateCollSeq(pParse, zColl) ){
117077 Index *pIdx;
117078 sqlite3ColumnSetColl(db, &p->aCol[i], zColl);
117079
117080 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
117081 ** then an index may have been created on this column before the
117082 ** collation type was added. Correct this if it is the case.
117083 */
117084 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
117085 assert( pIdx->nKeyCol==1 )((void) (0));
117086 if( pIdx->aiColumn[0]==i ){
117087 pIdx->azColl[0] = sqlite3ColumnColl(&p->aCol[i]);
117088 }
117089 }
117090 }
117091 sqlite3DbFree(db, zColl);
117092}
117093
117094/* Change the most recently parsed column to be a GENERATED ALWAYS AS
117095** column.
117096*/
117097SQLITE_PRIVATEstatic void sqlite3AddGenerated(Parse *pParse, Expr *pExpr, Token *pType){
117098#ifndef SQLITE_OMIT_GENERATED_COLUMNS
117099 u8 eType = COLFLAG_VIRTUAL0x0020;
117100 Table *pTab = pParse->pNewTable;
117101 Column *pCol;
117102 if( pTab==0 ){
117103 /* generated column in an CREATE TABLE IF NOT EXISTS that already exists */
117104 goto generated_done;
117105 }
117106 pCol = &(pTab->aCol[pTab->nCol-1]);
117107 if( IN_DECLARE_VTAB(pParse->eParseMode==1) ){
117108 sqlite3ErrorMsg(pParse, "virtual tables cannot use computed columns");
117109 goto generated_done;
117110 }
117111 if( pCol->iDflt>0 ) goto generated_error;
117112 if( pType ){
117113 if( pType->n==7 && sqlite3StrNICmpsqlite3_strnicmp("virtual",pType->z,7)==0 ){
117114 /* no-op */
117115 }else if( pType->n==6 && sqlite3StrNICmpsqlite3_strnicmp("stored",pType->z,6)==0 ){
117116 eType = COLFLAG_STORED0x0040;
117117 }else{
117118 goto generated_error;
117119 }
117120 }
117121 if( eType==COLFLAG_VIRTUAL0x0020 ) pTab->nNVCol--;
117122 pCol->colFlags |= eType;
117123 assert( TF_HasVirtual==COLFLAG_VIRTUAL )((void) (0));
117124 assert( TF_HasStored==COLFLAG_STORED )((void) (0));
117125 pTab->tabFlags |= eType;
117126 if( pCol->colFlags & COLFLAG_PRIMKEY0x0001 ){
117127 makeColumnPartOfPrimaryKey(pParse, pCol); /* For the error message */
117128 }
117129 sqlite3ColumnSetExpr(pParse, pTab, pCol, pExpr);
117130 pExpr = 0;
117131 goto generated_done;
117132
117133generated_error:
117134 sqlite3ErrorMsg(pParse, "error in generated column \"%s\"",
117135 pCol->zCnName);
117136generated_done:
117137 sqlite3ExprDelete(pParse->db, pExpr);
117138#else
117139 /* Throw and error for the GENERATED ALWAYS AS clause if the
117140 ** SQLITE_OMIT_GENERATED_COLUMNS compile-time option is used. */
117141 sqlite3ErrorMsg(pParse, "generated columns not supported");
117142 sqlite3ExprDelete(pParse->db, pExpr);
117143#endif
117144}
117145
117146/*
117147** Generate code that will increment the schema cookie.
117148**
117149** The schema cookie is used to determine when the schema for the
117150** database changes. After each schema change, the cookie value
117151** changes. When a process first reads the schema it records the
117152** cookie. Thereafter, whenever it goes to access the database,
117153** it checks the cookie to make sure the schema has not changed
117154** since it was last read.
117155**
117156** This plan is not completely bullet-proof. It is possible for
117157** the schema to change multiple times and for the cookie to be
117158** set back to prior value. But schema changes are infrequent
117159** and the probability of hitting the same cookie value is only
117160** 1 chance in 2^32. So we're safe enough.
117161**
117162** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
117163** the schema-version whenever the schema changes.
117164*/
117165SQLITE_PRIVATEstatic void sqlite3ChangeCookie(Parse *pParse, int iDb){
117166 sqlite3 *db = pParse->db;
117167 Vdbe *v = pParse->pVdbe;
117168 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
117169 sqlite3VdbeAddOp3(v, OP_SetCookie100, iDb, BTREE_SCHEMA_VERSION1,
117170 (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie));
117171}
117172
117173/*
117174** Measure the number of characters needed to output the given
117175** identifier. The number returned includes any quotes used
117176** but does not include the null terminator.
117177**
117178** The estimate is conservative. It might be larger that what is
117179** really needed.
117180*/
117181static int identLength(const char *z){
117182 int n;
117183 for(n=0; *z; n++, z++){
117184 if( *z=='"' ){ n++; }
117185 }
117186 return n + 2;
117187}
117188
117189/*
117190** The first parameter is a pointer to an output buffer. The second
117191** parameter is a pointer to an integer that contains the offset at
117192** which to write into the output buffer. This function copies the
117193** nul-terminated string pointed to by the third parameter, zSignedIdent,
117194** to the specified offset in the buffer and updates *pIdx to refer
117195** to the first byte after the last byte written before returning.
117196**
117197** If the string zSignedIdent consists entirely of alpha-numeric
117198** characters, does not begin with a digit and is not an SQL keyword,
117199** then it is copied to the output buffer exactly as it is. Otherwise,
117200** it is quoted using double-quotes.
117201*/
117202static void identPut(char *z, int *pIdx, char *zSignedIdent){
117203 unsigned char *zIdent = (unsigned char*)zSignedIdent;
117204 int i, j, needQuote;
117205 i = *pIdx;
117206
117207 for(j=0; zIdent[j]; j++){
117208 if( !sqlite3Isalnum(zIdent[j])(sqlite3CtypeMap[(unsigned char)(zIdent[j])]&0x06) && zIdent[j]!='_' ) break;
117209 }
117210 needQuote = sqlite3Isdigit(zIdent[0])(sqlite3CtypeMap[(unsigned char)(zIdent[0])]&0x04)
117211 || sqlite3KeywordCode(zIdent, j)!=TK_ID59
117212 || zIdent[j]!=0
117213 || j==0;
117214
117215 if( needQuote ) z[i++] = '"';
117216 for(j=0; zIdent[j]; j++){
117217 z[i++] = zIdent[j];
117218 if( zIdent[j]=='"' ) z[i++] = '"';
117219 }
117220 if( needQuote ) z[i++] = '"';
117221 z[i] = 0;
117222 *pIdx = i;
117223}
117224
117225/*
117226** Generate a CREATE TABLE statement appropriate for the given
117227** table. Memory to hold the text of the statement is obtained
117228** from sqliteMalloc() and must be freed by the calling function.
117229*/
117230static char *createTableStmt(sqlite3 *db, Table *p){
117231 int i, k, n;
117232 char *zStmt;
117233 char *zSep, *zSep2, *zEnd;
117234 Column *pCol;
117235 n = 0;
117236 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
117237 n += identLength(pCol->zCnName) + 5;
117238 }
117239 n += identLength(p->zName);
117240 if( n<50 ){
117241 zSep = "";
117242 zSep2 = ",";
117243 zEnd = ")";
117244 }else{
117245 zSep = "\n ";
117246 zSep2 = ",\n ";
117247 zEnd = "\n)";
117248 }
117249 n += 35 + 6*p->nCol;
117250 zStmt = sqlite3DbMallocRaw(0, n);
117251 if( zStmt==0 ){
117252 sqlite3OomFault(db);
117253 return 0;
117254 }
117255 sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
117256 k = sqlite3Strlen30(zStmt);
117257 identPut(zStmt, &k, p->zName);
117258 zStmt[k++] = '(';
117259 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
117260 static const char * const azType[] = {
117261 /* SQLITE_AFF_BLOB */ "",
117262 /* SQLITE_AFF_TEXT */ " TEXT",
117263 /* SQLITE_AFF_NUMERIC */ " NUM",
117264 /* SQLITE_AFF_INTEGER */ " INT",
117265 /* SQLITE_AFF_REAL */ " REAL"
117266 };
117267 int len;
117268 const char *zType;
117269
117270 sqlite3_snprintf(n-k, &zStmt[k], zSep);
117271 k += sqlite3Strlen30(&zStmt[k]);
117272 zSep = zSep2;
117273 identPut(zStmt, &k, pCol->zCnName);
117274 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 )((void) (0));
117275 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) )((void) (0));
117276 testcase( pCol->affinity==SQLITE_AFF_BLOB );
117277 testcase( pCol->affinity==SQLITE_AFF_TEXT );
117278 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
117279 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
117280 testcase( pCol->affinity==SQLITE_AFF_REAL );
117281
117282 zType = azType[pCol->affinity - SQLITE_AFF_BLOB0x41];
117283 len = sqlite3Strlen30(zType);
117284 assert( pCol->affinity==SQLITE_AFF_BLOB((void) (0))
117285 || pCol->affinity==sqlite3AffinityType(zType, 0) )((void) (0));
117286 memcpy(&zStmt[k], zType, len);
117287 k += len;
117288 assert( k<=n )((void) (0));
117289 }
117290 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
117291 return zStmt;
117292}
117293
117294/*
117295** Resize an Index object to hold N columns total. Return SQLITE_OK
117296** on success and SQLITE_NOMEM on an OOM error.
117297*/
117298static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){
117299 char *zExtra;
117300 int nByte;
117301 if( pIdx->nColumn>=N ) return SQLITE_OK0;
117302 assert( pIdx->isResized==0 )((void) (0));
117303 nByte = (sizeof(char*) + sizeof(LogEst) + sizeof(i16) + 1)*N;
117304 zExtra = sqlite3DbMallocZero(db, nByte);
117305 if( zExtra==0 ) return SQLITE_NOMEM_BKPT7;
117306 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
117307 pIdx->azColl = (const char**)zExtra;
117308 zExtra += sizeof(char*)*N;
117309 memcpy(zExtra, pIdx->aiRowLogEst, sizeof(LogEst)*(pIdx->nKeyCol+1));
117310 pIdx->aiRowLogEst = (LogEst*)zExtra;
117311 zExtra += sizeof(LogEst)*N;
117312 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
117313 pIdx->aiColumn = (i16*)zExtra;
117314 zExtra += sizeof(i16)*N;
117315 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
117316 pIdx->aSortOrder = (u8*)zExtra;
117317 pIdx->nColumn = N;
117318 pIdx->isResized = 1;
117319 return SQLITE_OK0;
117320}
117321
117322/*
117323** Estimate the total row width for a table.
117324*/
117325static void estimateTableWidth(Table *pTab){
117326 unsigned wTable = 0;
117327 const Column *pTabCol;
117328 int i;
117329 for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
117330 wTable += pTabCol->szEst;
117331 }
117332 if( pTab->iPKey<0 ) wTable++;
117333 pTab->szTabRow = sqlite3LogEst(wTable*4);
117334}
117335
117336/*
117337** Estimate the average size of a row for an index.
117338*/
117339static void estimateIndexWidth(Index *pIdx){
117340 unsigned wIndex = 0;
117341 int i;
117342 const Column *aCol = pIdx->pTable->aCol;
117343 for(i=0; i<pIdx->nColumn; i++){
117344 i16 x = pIdx->aiColumn[i];
117345 assert( x<pIdx->pTable->nCol )((void) (0));
117346 wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst;
117347 }
117348 pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
117349}
117350
117351/* Return true if column number x is any of the first nCol entries of aiCol[].
117352** This is used to determine if the column number x appears in any of the
117353** first nCol entries of an index.
117354*/
117355static int hasColumn(const i16 *aiCol, int nCol, int x){
117356 while( nCol-- > 0 ){
117357 if( x==*(aiCol++) ){
117358 return 1;
117359 }
117360 }
117361 return 0;
117362}
117363
117364/*
117365** Return true if any of the first nKey entries of index pIdx exactly
117366** match the iCol-th entry of pPk. pPk is always a WITHOUT ROWID
117367** PRIMARY KEY index. pIdx is an index on the same table. pIdx may
117368** or may not be the same index as pPk.
117369**
117370** The first nKey entries of pIdx are guaranteed to be ordinary columns,
117371** not a rowid or expression.
117372**
117373** This routine differs from hasColumn() in that both the column and the
117374** collating sequence must match for this routine, but for hasColumn() only
117375** the column name must match.
117376*/
117377static int isDupColumn(Index *pIdx, int nKey, Index *pPk, int iCol){
117378 int i, j;
117379 assert( nKey<=pIdx->nColumn )((void) (0));
117380 assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) )((void) (0));
117381 assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY )((void) (0));
117382 assert( pPk->pTable->tabFlags & TF_WithoutRowid )((void) (0));
117383 assert( pPk->pTable==pIdx->pTable )((void) (0));
117384 testcase( pPk==pIdx );
117385 j = pPk->aiColumn[iCol];
117386 assert( j!=XN_ROWID && j!=XN_EXPR )((void) (0));
117387 for(i=0; i<nKey; i++){
117388 assert( pIdx->aiColumn[i]>=0 || j>=0 )((void) (0));
117389 if( pIdx->aiColumn[i]==j
117390 && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0
117391 ){
117392 return 1;
117393 }
117394 }
117395 return 0;
117396}
117397
117398/* Recompute the colNotIdxed field of the Index.
117399**
117400** colNotIdxed is a bitmask that has a 0 bit representing each indexed
117401** columns that are within the first 63 columns of the table. The
117402** high-order bit of colNotIdxed is always 1. All unindexed columns
117403** of the table have a 1.
117404**
117405** 2019-10-24: For the purpose of this computation, virtual columns are
117406** not considered to be covered by the index, even if they are in the
117407** index, because we do not trust the logic in whereIndexExprTrans() to be
117408** able to find all instances of a reference to the indexed table column
117409** and convert them into references to the index. Hence we always want
117410** the actual table at hand in order to recompute the virtual column, if
117411** necessary.
117412**
117413** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask
117414** to determine if the index is covering index.
117415*/
117416static void recomputeColumnsNotIndexed(Index *pIdx){
117417 Bitmask m = 0;
117418 int j;
117419 Table *pTab = pIdx->pTable;
117420 for(j=pIdx->nColumn-1; j>=0; j--){
117421 int x = pIdx->aiColumn[j];
117422 if( x>=0 && (pTab->aCol[x].colFlags & COLFLAG_VIRTUAL0x0020)==0 ){
117423 testcase( x==BMS-1 );
117424 testcase( x==BMS-2 );
117425 if( x<BMS((int)(sizeof(Bitmask)*8))-1 ) m |= MASKBIT(x)(((Bitmask)1)<<(x));
117426 }
117427 }
117428 pIdx->colNotIdxed = ~m;
117429 assert( (pIdx->colNotIdxed>>63)==1 )((void) (0));
117430}
117431
117432/*
117433** This routine runs at the end of parsing a CREATE TABLE statement that
117434** has a WITHOUT ROWID clause. The job of this routine is to convert both
117435** internal schema data structures and the generated VDBE code so that they
117436** are appropriate for a WITHOUT ROWID table instead of a rowid table.
117437** Changes include:
117438**
117439** (1) Set all columns of the PRIMARY KEY schema object to be NOT NULL.
117440** (2) Convert P3 parameter of the OP_CreateBtree from BTREE_INTKEY
117441** into BTREE_BLOBKEY.
117442** (3) Bypass the creation of the sqlite_schema table entry
117443** for the PRIMARY KEY as the primary key index is now
117444** identified by the sqlite_schema table entry of the table itself.
117445** (4) Set the Index.tnum of the PRIMARY KEY Index object in the
117446** schema to the rootpage from the main table.
117447** (5) Add all table columns to the PRIMARY KEY Index object
117448** so that the PRIMARY KEY is a covering index. The surplus
117449** columns are part of KeyInfo.nAllField and are not used for
117450** sorting or lookup or uniqueness checks.
117451** (6) Replace the rowid tail on all automatically generated UNIQUE
117452** indices with the PRIMARY KEY columns.
117453**
117454** For virtual tables, only (1) is performed.
117455*/
117456static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
117457 Index *pIdx;
117458 Index *pPk;
117459 int nPk;
117460 int nExtra;
117461 int i, j;
117462 sqlite3 *db = pParse->db;
117463 Vdbe *v = pParse->pVdbe;
117464
117465 /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables)
117466 */
117467 if( !db->init.imposterTable ){
117468 for(i=0; i<pTab->nCol; i++){
117469 if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY0x0001)!=0
117470 && (pTab->aCol[i].notNull==OE_None0)
117471 ){
117472 pTab->aCol[i].notNull = OE_Abort2;
117473 }
117474 }
117475 pTab->tabFlags |= TF_HasNotNull0x00000800;
117476 }
117477
117478 /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY
117479 ** into BTREE_BLOBKEY.
117480 */
117481 assert( !pParse->bReturning )((void) (0));
117482 if( pParse->u1.addrCrTab ){
117483 assert( v )((void) (0));
117484 sqlite3VdbeChangeP3(v, pParse->u1.addrCrTab, BTREE_BLOBKEY2);
117485 }
117486
117487 /* Locate the PRIMARY KEY index. Or, if this table was originally
117488 ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index.
117489 */
117490 if( pTab->iPKey>=0 ){
117491 ExprList *pList;
117492 Token ipkToken;
117493 sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zCnName);
117494 pList = sqlite3ExprListAppend(pParse, 0,
117495 sqlite3ExprAlloc(db, TK_ID59, &ipkToken, 0));
117496 if( pList==0 ){
117497 pTab->tabFlags &= ~TF_WithoutRowid0x00000080;
117498 return;
117499 }
117500 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
117501 sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey);
117502 }
117503 pList->a[0].fg.sortFlags = pParse->iPkSortOrder;
117504 assert( pParse->pNewTable==pTab )((void) (0));
117505 pTab->iPKey = -1;
117506 sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
117507 SQLITE_IDXTYPE_PRIMARYKEY2);
117508 if( pParse->nErr ){
117509 pTab->tabFlags &= ~TF_WithoutRowid0x00000080;
117510 return;
117511 }
117512 assert( db->mallocFailed==0 )((void) (0));
117513 pPk = sqlite3PrimaryKeyIndex(pTab);
117514 assert( pPk->nKeyCol==1 )((void) (0));
117515 }else{
117516 pPk = sqlite3PrimaryKeyIndex(pTab);
117517 assert( pPk!=0 )((void) (0));
117518
117519 /*
117520 ** Remove all redundant columns from the PRIMARY KEY. For example, change
117521 ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later
117522 ** code assumes the PRIMARY KEY contains no repeated columns.
117523 */
117524 for(i=j=1; i<pPk->nKeyCol; i++){
117525 if( isDupColumn(pPk, j, pPk, i) ){
117526 pPk->nColumn--;
117527 }else{
117528 testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) );
117529 pPk->azColl[j] = pPk->azColl[i];
117530 pPk->aSortOrder[j] = pPk->aSortOrder[i];
117531 pPk->aiColumn[j++] = pPk->aiColumn[i];
117532 }
117533 }
117534 pPk->nKeyCol = j;
117535 }
117536 assert( pPk!=0 )((void) (0));
117537 pPk->isCovering = 1;
117538 if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
117539 nPk = pPk->nColumn = pPk->nKeyCol;
117540
117541 /* Bypass the creation of the PRIMARY KEY btree and the sqlite_schema
117542 ** table entry. This is only required if currently generating VDBE
117543 ** code for a CREATE TABLE (not when parsing one as part of reading
117544 ** a database schema). */
117545 if( v && pPk->tnum>0 ){
117546 assert( db->init.busy==0 )((void) (0));
117547 sqlite3VdbeChangeOpcode(v, (int)pPk->tnum, OP_Goto8);
117548 }
117549
117550 /* The root page of the PRIMARY KEY is the table root page */
117551 pPk->tnum = pTab->tnum;
117552
117553 /* Update the in-memory representation of all UNIQUE indices by converting
117554 ** the final rowid column into one or more columns of the PRIMARY KEY.
117555 */
117556 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
117557 int n;
117558 if( IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ) continue;
117559 for(i=n=0; i<nPk; i++){
117560 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
117561 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
117562 n++;
117563 }
117564 }
117565 if( n==0 ){
117566 /* This index is a superset of the primary key */
117567 pIdx->nColumn = pIdx->nKeyCol;
117568 continue;
117569 }
117570 if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
117571 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
117572 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
117573 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
117574 pIdx->aiColumn[j] = pPk->aiColumn[i];
117575 pIdx->azColl[j] = pPk->azColl[i];
117576 if( pPk->aSortOrder[i] ){
117577 /* See ticket https://www.sqlite.org/src/info/bba7b69f9849b5bf */
117578 pIdx->bAscKeyBug = 1;
117579 }
117580 j++;
117581 }
117582 }
117583 assert( pIdx->nColumn>=pIdx->nKeyCol+n )((void) (0));
117584 assert( pIdx->nColumn>=j )((void) (0));
117585 }
117586
117587 /* Add all table columns to the PRIMARY KEY index
117588 */
117589 nExtra = 0;
117590 for(i=0; i<pTab->nCol; i++){
117591 if( !hasColumn(pPk->aiColumn, nPk, i)
117592 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020)==0 ) nExtra++;
117593 }
117594 if( resizeIndexObject(db, pPk, nPk+nExtra) ) return;
117595 for(i=0, j=nPk; i<pTab->nCol; i++){
117596 if( !hasColumn(pPk->aiColumn, j, i)
117597 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020)==0
117598 ){
117599 assert( j<pPk->nColumn )((void) (0));
117600 pPk->aiColumn[j] = i;
117601 pPk->azColl[j] = sqlite3StrBINARY;
117602 j++;
117603 }
117604 }
117605 assert( pPk->nColumn==j )((void) (0));
117606 assert( pTab->nNVCol<=j )((void) (0));
117607 recomputeColumnsNotIndexed(pPk);
117608}
117609
117610
117611#ifndef SQLITE_OMIT_VIRTUALTABLE
117612/*
117613** Return true if pTab is a virtual table and zName is a shadow table name
117614** for that virtual table.
117615*/
117616SQLITE_PRIVATEstatic int sqlite3IsShadowTableOf(sqlite3 *db, Table *pTab, const char *zName){
117617 int nName; /* Length of zName */
117618 Module *pMod; /* Module for the virtual table */
117619
117620 if( !IsVirtual(pTab)((pTab)->eTabType==1) ) return 0;
117621 nName = sqlite3Strlen30(pTab->zName);
117622 if( sqlite3_strnicmp(zName, pTab->zName, nName)!=0 ) return 0;
117623 if( zName[nName]!='_' ) return 0;
117624 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
117625 if( pMod==0 ) return 0;
117626 if( pMod->pModule->iVersion<3 ) return 0;
117627 if( pMod->pModule->xShadowName==0 ) return 0;
117628 return pMod->pModule->xShadowName(zName+nName+1);
117629}
117630#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
117631
117632#ifndef SQLITE_OMIT_VIRTUALTABLE
117633/*
117634** Table pTab is a virtual table. If it the virtual table implementation
117635** exists and has an xShadowName method, then loop over all other ordinary
117636** tables within the same schema looking for shadow tables of pTab, and mark
117637** any shadow tables seen using the TF_Shadow flag.
117638*/
117639SQLITE_PRIVATEstatic void sqlite3MarkAllShadowTablesOf(sqlite3 *db, Table *pTab){
117640 int nName; /* Length of pTab->zName */
117641 Module *pMod; /* Module for the virtual table */
117642 HashElem *k; /* For looping through the symbol table */
117643
117644 assert( IsVirtual(pTab) )((void) (0));
117645 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
117646 if( pMod==0 ) return;
117647 if( NEVER(pMod->pModule==0)(pMod->pModule==0) ) return;
117648 if( pMod->pModule->iVersion<3 ) return;
117649 if( pMod->pModule->xShadowName==0 ) return;
117650 assert( pTab->zName!=0 )((void) (0));
117651 nName = sqlite3Strlen30(pTab->zName);
117652 for(k=sqliteHashFirst(&pTab->pSchema->tblHash)((&pTab->pSchema->tblHash)->first); k; k=sqliteHashNext(k)((k)->next)){
117653 Table *pOther = sqliteHashData(k)((k)->data);
117654 assert( pOther->zName!=0 )((void) (0));
117655 if( !IsOrdinaryTable(pOther)((pOther)->eTabType==0) ) continue;
117656 if( pOther->tabFlags & TF_Shadow0x00001000 ) continue;
117657 if( sqlite3StrNICmpsqlite3_strnicmp(pOther->zName, pTab->zName, nName)==0
117658 && pOther->zName[nName]=='_'
117659 && pMod->pModule->xShadowName(pOther->zName+nName+1)
117660 ){
117661 pOther->tabFlags |= TF_Shadow0x00001000;
117662 }
117663 }
117664}
117665#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
117666
117667#ifndef SQLITE_OMIT_VIRTUALTABLE
117668/*
117669** Return true if zName is a shadow table name in the current database
117670** connection.
117671**
117672** zName is temporarily modified while this routine is running, but is
117673** restored to its original value prior to this routine returning.
117674*/
117675SQLITE_PRIVATEstatic int sqlite3ShadowTableName(sqlite3 *db, const char *zName){
117676 char *zTail; /* Pointer to the last "_" in zName */
117677 Table *pTab; /* Table that zName is a shadow of */
117678 zTail = strrchr(zName, '_');
117679 if( zTail==0 ) return 0;
117680 *zTail = 0;
117681 pTab = sqlite3FindTable(db, zName, 0);
117682 *zTail = '_';
117683 if( pTab==0 ) return 0;
117684 if( !IsVirtual(pTab)((pTab)->eTabType==1) ) return 0;
117685 return sqlite3IsShadowTableOf(db, pTab, zName);
117686}
117687#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
117688
117689
117690#ifdef SQLITE_DEBUG
117691/*
117692** Mark all nodes of an expression as EP_Immutable, indicating that
117693** they should not be changed. Expressions attached to a table or
117694** index definition are tagged this way to help ensure that we do
117695** not pass them into code generator routines by mistake.
117696*/
117697static int markImmutableExprStep(Walker *pWalker, Expr *pExpr){
117698 ExprSetVVAProperty(pExpr, EP_Immutable);
117699 return WRC_Continue0;
117700}
117701static void markExprListImmutable(ExprList *pList){
117702 if( pList ){
117703 Walker w;
117704 memset(&w, 0, sizeof(w));
117705 w.xExprCallback = markImmutableExprStep;
117706 w.xSelectCallback = sqlite3SelectWalkNoop;
117707 w.xSelectCallback2 = 0;
117708 sqlite3WalkExprList(&w, pList);
117709 }
117710}
117711#else
117712#define markExprListImmutable(X) /* no-op */
117713#endif /* SQLITE_DEBUG */
117714
117715
117716/*
117717** This routine is called to report the final ")" that terminates
117718** a CREATE TABLE statement.
117719**
117720** The table structure that other action routines have been building
117721** is added to the internal hash tables, assuming no errors have
117722** occurred.
117723**
117724** An entry for the table is made in the schema table on disk, unless
117725** this is a temporary table or db->init.busy==1. When db->init.busy==1
117726** it means we are reading the sqlite_schema table because we just
117727** connected to the database or because the sqlite_schema table has
117728** recently changed, so the entry for this table already exists in
117729** the sqlite_schema table. We do not want to create it again.
117730**
117731** If the pSelect argument is not NULL, it means that this routine
117732** was called to create a table generated from a
117733** "CREATE TABLE ... AS SELECT ..." statement. The column names of
117734** the new table will match the result set of the SELECT.
117735*/
117736SQLITE_PRIVATEstatic void sqlite3EndTable(
117737 Parse *pParse, /* Parse context */
117738 Token *pCons, /* The ',' token after the last column defn. */
117739 Token *pEnd, /* The ')' before options in the CREATE TABLE */
117740 u32 tabOpts, /* Extra table options. Usually 0. */
117741 Select *pSelect /* Select from a "CREATE ... AS SELECT" */
117742){
117743 Table *p; /* The new table */
117744 sqlite3 *db = pParse->db; /* The database connection */
117745 int iDb; /* Database in which the table lives */
117746 Index *pIdx; /* An implied index of the table */
117747
117748 if( pEnd==0 && pSelect==0 ){
117749 return;
117750 }
117751 p = pParse->pNewTable;
117752 if( p==0 ) return;
117753
117754 if( pSelect==0 && sqlite3ShadowTableName(db, p->zName) ){
117755 p->tabFlags |= TF_Shadow0x00001000;
117756 }
117757
117758 /* If the db->init.busy is 1 it means we are reading the SQL off the
117759 ** "sqlite_schema" or "sqlite_temp_schema" table on the disk.
117760 ** So do not write to the disk again. Extract the root page number
117761 ** for the table from the db->init.newTnum field. (The page number
117762 ** should have been put there by the sqliteOpenCb routine.)
117763 **
117764 ** If the root page number is 1, that means this is the sqlite_schema
117765 ** table itself. So mark it read-only.
117766 */
117767 if( db->init.busy ){
117768 if( pSelect || (!IsOrdinaryTable(p)((p)->eTabType==0) && db->init.newTnum) ){
117769 sqlite3ErrorMsg(pParse, "");
117770 return;
117771 }
117772 p->tnum = db->init.newTnum;
117773 if( p->tnum==1 ) p->tabFlags |= TF_Readonly0x00000001;
117774 }
117775
117776 /* Special processing for tables that include the STRICT keyword:
117777 **
117778 ** * Do not allow custom column datatypes. Every column must have
117779 ** a datatype that is one of INT, INTEGER, REAL, TEXT, or BLOB.
117780 **
117781 ** * If a PRIMARY KEY is defined, other than the INTEGER PRIMARY KEY,
117782 ** then all columns of the PRIMARY KEY must have a NOT NULL
117783 ** constraint.
117784 */
117785 if( tabOpts & TF_Strict0x00010000 ){
117786 int ii;
117787 p->tabFlags |= TF_Strict0x00010000;
117788 for(ii=0; ii<p->nCol; ii++){
117789 Column *pCol = &p->aCol[ii];
117790 if( pCol->eCType==COLTYPE_CUSTOM0 ){
117791 if( pCol->colFlags & COLFLAG_HASTYPE0x0004 ){
117792 sqlite3ErrorMsg(pParse,
117793 "unknown datatype for %s.%s: \"%s\"",
117794 p->zName, pCol->zCnName, sqlite3ColumnType(pCol, "")
117795 );
117796 }else{
117797 sqlite3ErrorMsg(pParse, "missing datatype for %s.%s",
117798 p->zName, pCol->zCnName);
117799 }
117800 return;
117801 }else if( pCol->eCType==COLTYPE_ANY1 ){
117802 pCol->affinity = SQLITE_AFF_BLOB0x41;
117803 }
117804 if( (pCol->colFlags & COLFLAG_PRIMKEY0x0001)!=0
117805 && p->iPKey!=ii
117806 && pCol->notNull == OE_None0
117807 ){
117808 pCol->notNull = OE_Abort2;
117809 p->tabFlags |= TF_HasNotNull0x00000800;
117810 }
117811 }
117812 }
117813
117814 assert( (p->tabFlags & TF_HasPrimaryKey)==0((void) (0))
117815 || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 )((void) (0));
117816 assert( (p->tabFlags & TF_HasPrimaryKey)!=0((void) (0))
117817 || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) )((void) (0));
117818
117819 /* Special processing for WITHOUT ROWID Tables */
117820 if( tabOpts & TF_WithoutRowid0x00000080 ){
117821 if( (p->tabFlags & TF_Autoincrement0x00000008) ){
117822 sqlite3ErrorMsg(pParse,
117823 "AUTOINCREMENT not allowed on WITHOUT ROWID tables");
117824 return;
117825 }
117826 if( (p->tabFlags & TF_HasPrimaryKey0x00000004)==0 ){
117827 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
117828 return;
117829 }
117830 p->tabFlags |= TF_WithoutRowid0x00000080 | TF_NoVisibleRowid0x00000200;
117831 convertToWithoutRowidTable(pParse, p);
117832 }
117833 iDb = sqlite3SchemaToIndex(db, p->pSchema);
117834
117835#ifndef SQLITE_OMIT_CHECK
117836 /* Resolve names in all CHECK constraint expressions.
117837 */
117838 if( p->pCheck ){
117839 sqlite3ResolveSelfReference(pParse, p, NC_IsCheck0x000004, 0, p->pCheck);
117840 if( pParse->nErr ){
117841 /* If errors are seen, delete the CHECK constraints now, else they might
117842 ** actually be used if PRAGMA writable_schema=ON is set. */
117843 sqlite3ExprListDelete(db, p->pCheck);
117844 p->pCheck = 0;
117845 }else{
117846 markExprListImmutable(p->pCheck);
117847 }
117848 }
117849#endif /* !defined(SQLITE_OMIT_CHECK) */
117850#ifndef SQLITE_OMIT_GENERATED_COLUMNS
117851 if( p->tabFlags & TF_HasGenerated0x00000060 ){
117852 int ii, nNG = 0;
117853 testcase( p->tabFlags & TF_HasVirtual );
117854 testcase( p->tabFlags & TF_HasStored );
117855 for(ii=0; ii<p->nCol; ii++){
117856 u32 colFlags = p->aCol[ii].colFlags;
117857 if( (colFlags & COLFLAG_GENERATED0x0060)!=0 ){
117858 Expr *pX = sqlite3ColumnExpr(p, &p->aCol[ii]);
117859 testcase( colFlags & COLFLAG_VIRTUAL );
117860 testcase( colFlags & COLFLAG_STORED );
117861 if( sqlite3ResolveSelfReference(pParse, p, NC_GenCol0x000008, pX, 0) ){
117862 /* If there are errors in resolving the expression, change the
117863 ** expression to a NULL. This prevents code generators that operate
117864 ** on the expression from inserting extra parts into the expression
117865 ** tree that have been allocated from lookaside memory, which is
117866 ** illegal in a schema and will lead to errors or heap corruption
117867 ** when the database connection closes. */
117868 sqlite3ColumnSetExpr(pParse, p, &p->aCol[ii],
117869 sqlite3ExprAlloc(db, TK_NULL121, 0, 0));
117870 }
117871 }else{
117872 nNG++;
117873 }
117874 }
117875 if( nNG==0 ){
117876 sqlite3ErrorMsg(pParse, "must have at least one non-generated column");
117877 return;
117878 }
117879 }
117880#endif
117881
117882 /* Estimate the average row size for the table and for all implied indices */
117883 estimateTableWidth(p);
117884 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
117885 estimateIndexWidth(pIdx);
117886 }
117887
117888 /* If not initializing, then create a record for the new table
117889 ** in the schema table of the database.
117890 **
117891 ** If this is a TEMPORARY table, write the entry into the auxiliary
117892 ** file instead of into the main database file.
117893 */
117894 if( !db->init.busy ){
117895 int n;
117896 Vdbe *v;
117897 char *zType; /* "view" or "table" */
117898 char *zType2; /* "VIEW" or "TABLE" */
117899 char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */
117900
117901 v = sqlite3GetVdbe(pParse);
117902 if( NEVER(v==0)(v==0) ) return;
117903
117904 sqlite3VdbeAddOp1(v, OP_Close122, 0);
117905
117906 /*
117907 ** Initialize zType for the new view or table.
117908 */
117909 if( IsOrdinaryTable(p)((p)->eTabType==0) ){
117910 /* A regular table */
117911 zType = "table";
117912 zType2 = "TABLE";
117913#ifndef SQLITE_OMIT_VIEW
117914 }else{
117915 /* A view */
117916 zType = "view";
117917 zType2 = "VIEW";
117918#endif
117919 }
117920
117921 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
117922 ** statement to populate the new table. The root-page number for the
117923 ** new table is in register pParse->regRoot.
117924 **
117925 ** Once the SELECT has been coded by sqlite3Select(), it is in a
117926 ** suitable state to query for the column names and types to be used
117927 ** by the new table.
117928 **
117929 ** A shared-cache write-lock is not required to write to the new table,
117930 ** as a schema-lock must have already been obtained to create it. Since
117931 ** a schema-lock excludes all other database users, the write-lock would
117932 ** be redundant.
117933 */
117934 if( pSelect ){
117935 SelectDest dest; /* Where the SELECT should store results */
117936 int regYield; /* Register holding co-routine entry-point */
117937 int addrTop; /* Top of the co-routine */
117938 int regRec; /* A record to be insert into the new table */
117939 int regRowid; /* Rowid of the next row to insert */
117940 int addrInsLoop; /* Top of the loop for inserting rows */
117941 Table *pSelTab; /* A table that describes the SELECT results */
117942
117943 if( IN_SPECIAL_PARSE(pParse->eParseMode!=0) ){
117944 pParse->rc = SQLITE_ERROR1;
117945 pParse->nErr++;
117946 return;
117947 }
117948 regYield = ++pParse->nMem;
117949 regRec = ++pParse->nMem;
117950 regRowid = ++pParse->nMem;
117951 assert(pParse->nTab==1)((void) (0));
117952 sqlite3MayAbort(pParse);
117953 sqlite3VdbeAddOp3(v, OP_OpenWrite113, 1, pParse->regRoot, iDb);
117954 sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG0x10);
117955 pParse->nTab = 2;
117956 addrTop = sqlite3VdbeCurrentAddr(v) + 1;
117957 sqlite3VdbeAddOp3(v, OP_InitCoroutine10, regYield, 0, addrTop);
117958 if( pParse->nErr ) return;
117959 pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect, SQLITE_AFF_BLOB0x41);
117960 if( pSelTab==0 ) return;
117961 assert( p->aCol==0 )((void) (0));
117962 p->nCol = p->nNVCol = pSelTab->nCol;
117963 p->aCol = pSelTab->aCol;
117964 pSelTab->nCol = 0;
117965 pSelTab->aCol = 0;
117966 sqlite3DeleteTable(db, pSelTab);
117967 sqlite3SelectDestInit(&dest, SRT_Coroutine13, regYield);
117968 sqlite3Select(pParse, pSelect, &dest);
117969 if( pParse->nErr ) return;
117970 sqlite3VdbeEndCoroutine(v, regYield);
117971 sqlite3VdbeJumpHere(v, addrTop - 1);
117972 addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield11, dest.iSDParm);
117973 VdbeCoverage(v);
117974 sqlite3VdbeAddOp3(v, OP_MakeRecord97, dest.iSdst, dest.nSdst, regRec);
117975 sqlite3TableAffinity(v, p, 0);
117976 sqlite3VdbeAddOp2(v, OP_NewRowid127, 1, regRowid);
117977 sqlite3VdbeAddOp3(v, OP_Insert128, 1, regRec, regRowid);
117978 sqlite3VdbeGoto(v, addrInsLoop);
117979 sqlite3VdbeJumpHere(v, addrInsLoop);
117980 sqlite3VdbeAddOp1(v, OP_Close122, 1);
117981 }
117982
117983 /* Compute the complete text of the CREATE statement */
117984 if( pSelect ){
117985 zStmt = createTableStmt(db, p);
117986 }else{
117987 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
117988 n = (int)(pEnd2->z - pParse->sNameToken.z);
117989 if( pEnd2->z[0]!=';' ) n += pEnd2->n;
117990 zStmt = sqlite3MPrintf(db,
117991 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
117992 );
117993 }
117994
117995 /* A slot for the record has already been allocated in the
117996 ** schema table. We just need to update that slot with all
117997 ** the information we've collected.
117998 */
117999 sqlite3NestedParse(pParse,
118000 "UPDATE %Q." LEGACY_SCHEMA_TABLE"sqlite_master"
118001 " SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q"
118002 " WHERE rowid=#%d",
118003 db->aDb[iDb].zDbSName,
118004 zType,
118005 p->zName,
118006 p->zName,
118007 pParse->regRoot,
118008 zStmt,
118009 pParse->regRowid
118010 );
118011 sqlite3DbFree(db, zStmt);
118012 sqlite3ChangeCookie(pParse, iDb);
118013
118014#ifndef SQLITE_OMIT_AUTOINCREMENT
118015 /* Check to see if we need to create an sqlite_sequence table for
118016 ** keeping track of autoincrement keys.
118017 */
118018 if( (p->tabFlags & TF_Autoincrement0x00000008)!=0 && !IN_SPECIAL_PARSE(pParse->eParseMode!=0) ){
118019 Db *pDb = &db->aDb[iDb];
118020 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
118021 if( pDb->pSchema->pSeqTab==0 ){
118022 sqlite3NestedParse(pParse,
118023 "CREATE TABLE %Q.sqlite_sequence(name,seq)",
118024 pDb->zDbSName
118025 );
118026 }
118027 }
118028#endif
118029
118030 /* Reparse everything to update our internal data structures */
118031 sqlite3VdbeAddParseSchemaOp(v, iDb,
118032 sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName),0);
118033 }
118034
118035 /* Add the table to the in-memory representation of the database.
118036 */
118037 if( db->init.busy ){
118038 Table *pOld;
118039 Schema *pSchema = p->pSchema;
118040 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
118041 assert( HasRowid(p) || p->iPKey<0 )((void) (0));
118042 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
118043 if( pOld ){
118044 assert( p==pOld )((void) (0)); /* Malloc must have failed inside HashInsert() */
118045 sqlite3OomFault(db);
118046 return;
118047 }
118048 pParse->pNewTable = 0;
118049 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
118050
118051 /* If this is the magic sqlite_sequence table used by autoincrement,
118052 ** then record a pointer to this table in the main database structure
118053 ** so that INSERT can find the table easily. */
118054 assert( !pParse->nested )((void) (0));
118055#ifndef SQLITE_OMIT_AUTOINCREMENT
118056 if( strcmp(p->zName, "sqlite_sequence")==0 ){
118057 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
118058 p->pSchema->pSeqTab = p;
118059 }
118060#endif
118061 }
118062
118063#ifndef SQLITE_OMIT_ALTERTABLE
118064 if( !pSelect && IsOrdinaryTable(p)((p)->eTabType==0) ){
118065 assert( pCons && pEnd )((void) (0));
118066 if( pCons->z==0 ){
118067 pCons = pEnd;
118068 }
118069 p->u.tab.addColOffset = 13 + (int)(pCons->z - pParse->sNameToken.z);
118070 }
118071#endif
118072}
118073
118074#ifndef SQLITE_OMIT_VIEW
118075/*
118076** The parser calls this routine in order to create a new VIEW
118077*/
118078SQLITE_PRIVATEstatic void sqlite3CreateView(
118079 Parse *pParse, /* The parsing context */
118080 Token *pBegin, /* The CREATE token that begins the statement */
118081 Token *pName1, /* The token that holds the name of the view */
118082 Token *pName2, /* The token that holds the name of the view */
118083 ExprList *pCNames, /* Optional list of view column names */
118084 Select *pSelect, /* A SELECT statement that will become the new view */
118085 int isTemp, /* TRUE for a TEMPORARY view */
118086 int noErr /* Suppress error messages if VIEW already exists */
118087){
118088 Table *p;
118089 int n;
118090 const char *z;
118091 Token sEnd;
118092 DbFixer sFix;
118093 Token *pName = 0;
118094 int iDb;
118095 sqlite3 *db = pParse->db;
118096
118097 if( pParse->nVar>0 ){
118098 sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
118099 goto create_view_fail;
118100 }
118101 sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
118102 p = pParse->pNewTable;
118103 if( p==0 || pParse->nErr ) goto create_view_fail;
118104
118105 /* Legacy versions of SQLite allowed the use of the magic "rowid" column
118106 ** on a view, even though views do not have rowids. The following flag
118107 ** setting fixes this problem. But the fix can be disabled by compiling
118108 ** with -DSQLITE_ALLOW_ROWID_IN_VIEW in case there are legacy apps that
118109 ** depend upon the old buggy behavior. */
118110#ifndef SQLITE_ALLOW_ROWID_IN_VIEW
118111 p->tabFlags |= TF_NoVisibleRowid0x00000200;
118112#endif
118113
118114 sqlite3TwoPartName(pParse, pName1, pName2, &pName);
118115 iDb = sqlite3SchemaToIndex(db, p->pSchema);
118116 sqlite3FixInit(&sFix, pParse, iDb, "view", pName);
118117 if( sqlite3FixSelect(&sFix, pSelect) ) goto create_view_fail;
118118
118119 /* Make a copy of the entire SELECT statement that defines the view.
118120 ** This will force all the Expr.token.z values to be dynamically
118121 ** allocated rather than point to the input string - which means that
118122 ** they will persist after the current sqlite3_exec() call returns.
118123 */
118124 pSelect->selFlags |= SF_View0x0200000;
118125 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
118126 p->u.view.pSelect = pSelect;
118127 pSelect = 0;
118128 }else{
118129 p->u.view.pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE0x0001);
118130 }
118131 p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE0x0001);
118132 p->eTabType = TABTYP_VIEW2;
118133 if( db->mallocFailed ) goto create_view_fail;
118134
118135 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
118136 ** the end.
118137 */
118138 sEnd = pParse->sLastToken;
118139 assert( sEnd.z[0]!=0 || sEnd.n==0 )((void) (0));
118140 if( sEnd.z[0]!=';' ){
118141 sEnd.z += sEnd.n;
118142 }
118143 sEnd.n = 0;
118144 n = (int)(sEnd.z - pBegin->z);
118145 assert( n>0 )((void) (0));
118146 z = pBegin->z;
118147 while( sqlite3Isspace(z[n-1])(sqlite3CtypeMap[(unsigned char)(z[n-1])]&0x01) ){ n--; }
118148 sEnd.z = &z[n-1];
118149 sEnd.n = 1;
118150
118151 /* Use sqlite3EndTable() to add the view to the schema table */
118152 sqlite3EndTable(pParse, 0, &sEnd, 0, 0);
118153
118154create_view_fail:
118155 sqlite3SelectDelete(db, pSelect);
118156 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
118157 sqlite3RenameExprlistUnmap(pParse, pCNames);
118158 }
118159 sqlite3ExprListDelete(db, pCNames);
118160 return;
118161}
118162#endif /* SQLITE_OMIT_VIEW */
118163
118164#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
118165/*
118166** The Table structure pTable is really a VIEW. Fill in the names of
118167** the columns of the view in the pTable structure. Return the number
118168** of errors. If an error is seen leave an error message in pParse->zErrMsg.
118169*/
118170SQLITE_PRIVATEstatic int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
118171 Table *pSelTab; /* A fake table from which we get the result set */
118172 Select *pSel; /* Copy of the SELECT that implements the view */
118173 int nErr = 0; /* Number of errors encountered */
118174 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
118175#ifndef SQLITE_OMIT_VIRTUALTABLE
118176 int rc;
118177#endif
118178#ifndef SQLITE_OMIT_AUTHORIZATION
118179 sqlite3_xauth xAuth; /* Saved xAuth pointer */
118180#endif
118181
118182 assert( pTable )((void) (0));
118183
118184#ifndef SQLITE_OMIT_VIRTUALTABLE
118185 if( IsVirtual(pTable)((pTable)->eTabType==1) ){
118186 db->nSchemaLock++;
118187 rc = sqlite3VtabCallConnect(pParse, pTable);
118188 db->nSchemaLock--;
118189 return rc;
118190 }
118191#endif
118192
118193#ifndef SQLITE_OMIT_VIEW
118194 /* A positive nCol means the columns names for this view are
118195 ** already known.
118196 */
118197 if( pTable->nCol>0 ) return 0;
118198
118199 /* A negative nCol is a special marker meaning that we are currently
118200 ** trying to compute the column names. If we enter this routine with
118201 ** a negative nCol, it means two or more views form a loop, like this:
118202 **
118203 ** CREATE VIEW one AS SELECT * FROM two;
118204 ** CREATE VIEW two AS SELECT * FROM one;
118205 **
118206 ** Actually, the error above is now caught prior to reaching this point.
118207 ** But the following test is still important as it does come up
118208 ** in the following:
118209 **
118210 ** CREATE TABLE main.ex1(a);
118211 ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
118212 ** SELECT * FROM temp.ex1;
118213 */
118214 if( pTable->nCol<0 ){
118215 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
118216 return 1;
118217 }
118218 assert( pTable->nCol>=0 )((void) (0));
118219
118220 /* If we get this far, it means we need to compute the table names.
118221 ** Note that the call to sqlite3ResultSetOfSelect() will expand any
118222 ** "*" elements in the results set of the view and will assign cursors
118223 ** to the elements of the FROM clause. But we do not want these changes
118224 ** to be permanent. So the computation is done on a copy of the SELECT
118225 ** statement that defines the view.
118226 */
118227 assert( IsView(pTable) )((void) (0));
118228 pSel = sqlite3SelectDup(db, pTable->u.view.pSelect, 0);
118229 if( pSel ){
118230 u8 eParseMode = pParse->eParseMode;
118231 int nTab = pParse->nTab;
118232 int nSelect = pParse->nSelect;
118233 pParse->eParseMode = PARSE_MODE_NORMAL0;
118234 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
118235 pTable->nCol = -1;
118236 DisableLookasidedb->lookaside.bDisable++;db->lookaside.sz=0;
118237#ifndef SQLITE_OMIT_AUTHORIZATION
118238 xAuth = db->xAuth;
118239 db->xAuth = 0;
118240 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE0x40);
118241 db->xAuth = xAuth;
118242#else
118243 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE0x40);
118244#endif
118245 pParse->nTab = nTab;
118246 pParse->nSelect = nSelect;
118247 if( pSelTab==0 ){
118248 pTable->nCol = 0;
118249 nErr++;
118250 }else if( pTable->pCheck ){
118251 /* CREATE VIEW name(arglist) AS ...
118252 ** The names of the columns in the table are taken from
118253 ** arglist which is stored in pTable->pCheck. The pCheck field
118254 ** normally holds CHECK constraints on an ordinary table, but for
118255 ** a VIEW it holds the list of column names.
118256 */
118257 sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
118258 &pTable->nCol, &pTable->aCol);
118259 if( pParse->nErr==0
118260 && pTable->nCol==pSel->pEList->nExpr
118261 ){
118262 assert( db->mallocFailed==0 )((void) (0));
118263 sqlite3SelectAddColumnTypeAndCollation(pParse, pTable, pSel,
118264 SQLITE_AFF_NONE0x40);
118265 }
118266 }else{
118267 /* CREATE VIEW name AS... without an argument list. Construct
118268 ** the column names from the SELECT statement that defines the view.
118269 */
118270 assert( pTable->aCol==0 )((void) (0));
118271 pTable->nCol = pSelTab->nCol;
118272 pTable->aCol = pSelTab->aCol;
118273 pTable->tabFlags |= (pSelTab->tabFlags & COLFLAG_NOINSERT0x0062);
118274 pSelTab->nCol = 0;
118275 pSelTab->aCol = 0;
118276 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) )((void) (0));
118277 }
118278 pTable->nNVCol = pTable->nCol;
118279 sqlite3DeleteTable(db, pSelTab);
118280 sqlite3SelectDelete(db, pSel);
118281 EnableLookasidedb->lookaside.bDisable--; db->lookaside.sz=db->lookaside
.bDisable?0:db->lookaside.szTrue
;
118282 pParse->eParseMode = eParseMode;
118283 } else {
118284 nErr++;
118285 }
118286 pTable->pSchema->schemaFlags |= DB_UnresetViews0x0002;
118287 if( db->mallocFailed ){
118288 sqlite3DeleteColumnNames(db, pTable);
118289 }
118290#endif /* SQLITE_OMIT_VIEW */
118291 return nErr;
118292}
118293#endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
118294
118295#ifndef SQLITE_OMIT_VIEW
118296/*
118297** Clear the column names from every VIEW in database idx.
118298*/
118299static void sqliteViewResetAll(sqlite3 *db, int idx){
118300 HashElem *i;
118301 assert( sqlite3SchemaMutexHeld(db, idx, 0) )((void) (0));
118302 if( !DbHasProperty(db, idx, DB_UnresetViews)(((db)->aDb[idx].pSchema->schemaFlags&(0x0002))==(0x0002
))
) return;
118303 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash)((&db->aDb[idx].pSchema->tblHash)->first); i;i=sqliteHashNext(i)((i)->next)){
118304 Table *pTab = sqliteHashData(i)((i)->data);
118305 if( IsView(pTab)((pTab)->eTabType==2) ){
118306 sqlite3DeleteColumnNames(db, pTab);
118307 }
118308 }
118309 DbClearProperty(db, idx, DB_UnresetViews)(db)->aDb[idx].pSchema->schemaFlags&=~(0x0002);
118310}
118311#else
118312# define sqliteViewResetAll(A,B)
118313#endif /* SQLITE_OMIT_VIEW */
118314
118315/*
118316** This function is called by the VDBE to adjust the internal schema
118317** used by SQLite when the btree layer moves a table root page. The
118318** root-page of a table or index in database iDb has changed from iFrom
118319** to iTo.
118320**
118321** Ticket #1728: The symbol table might still contain information
118322** on tables and/or indices that are the process of being deleted.
118323** If you are unlucky, one of those deleted indices or tables might
118324** have the same rootpage number as the real table or index that is
118325** being moved. So we cannot stop searching after the first match
118326** because the first match might be for one of the deleted indices
118327** or tables and not the table/index that is actually being moved.
118328** We must continue looping until all tables and indices with
118329** rootpage==iFrom have been converted to have a rootpage of iTo
118330** in order to be certain that we got the right one.
118331*/
118332#ifndef SQLITE_OMIT_AUTOVACUUM
118333SQLITE_PRIVATEstatic void sqlite3RootPageMoved(sqlite3 *db, int iDb, Pgno iFrom, Pgno iTo){
118334 HashElem *pElem;
118335 Hash *pHash;
118336 Db *pDb;
118337
118338 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
118339 pDb = &db->aDb[iDb];
118340 pHash = &pDb->pSchema->tblHash;
118341 for(pElem=sqliteHashFirst(pHash)((pHash)->first); pElem; pElem=sqliteHashNext(pElem)((pElem)->next)){
118342 Table *pTab = sqliteHashData(pElem)((pElem)->data);
118343 if( pTab->tnum==iFrom ){
118344 pTab->tnum = iTo;
118345 }
118346 }
118347 pHash = &pDb->pSchema->idxHash;
118348 for(pElem=sqliteHashFirst(pHash)((pHash)->first); pElem; pElem=sqliteHashNext(pElem)((pElem)->next)){
118349 Index *pIdx = sqliteHashData(pElem)((pElem)->data);
118350 if( pIdx->tnum==iFrom ){
118351 pIdx->tnum = iTo;
118352 }
118353 }
118354}
118355#endif
118356
118357/*
118358** Write code to erase the table with root-page iTable from database iDb.
118359** Also write code to modify the sqlite_schema table and internal schema
118360** if a root-page of another table is moved by the btree-layer whilst
118361** erasing iTable (this can happen with an auto-vacuum database).
118362*/
118363static void destroyRootPage(Parse *pParse, int iTable, int iDb){
118364 Vdbe *v = sqlite3GetVdbe(pParse);
118365 int r1 = sqlite3GetTempReg(pParse);
118366 if( iTable<2 ) sqlite3ErrorMsg(pParse, "corrupt schema");
118367 sqlite3VdbeAddOp3(v, OP_Destroy144, iTable, r1, iDb);
118368 sqlite3MayAbort(pParse);
118369#ifndef SQLITE_OMIT_AUTOVACUUM
118370 /* OP_Destroy stores an in integer r1. If this integer
118371 ** is non-zero, then it is the root page number of a table moved to
118372 ** location iTable. The following code modifies the sqlite_schema table to
118373 ** reflect this.
118374 **
118375 ** The "#NNN" in the SQL is a special constant that means whatever value
118376 ** is in register NNN. See grammar rules associated with the TK_REGISTER
118377 ** token for additional information.
118378 */
118379 sqlite3NestedParse(pParse,
118380 "UPDATE %Q." LEGACY_SCHEMA_TABLE"sqlite_master"
118381 " SET rootpage=%d WHERE #%d AND rootpage=#%d",
118382 pParse->db->aDb[iDb].zDbSName, iTable, r1, r1);
118383#endif
118384 sqlite3ReleaseTempReg(pParse, r1);
118385}
118386
118387/*
118388** Write VDBE code to erase table pTab and all associated indices on disk.
118389** Code to update the sqlite_schema tables and internal schema definitions
118390** in case a root-page belonging to another table is moved by the btree layer
118391** is also added (this can happen with an auto-vacuum database).
118392*/
118393static void destroyTable(Parse *pParse, Table *pTab){
118394 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
118395 ** is not defined), then it is important to call OP_Destroy on the
118396 ** table and index root-pages in order, starting with the numerically
118397 ** largest root-page number. This guarantees that none of the root-pages
118398 ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
118399 ** following were coded:
118400 **
118401 ** OP_Destroy 4 0
118402 ** ...
118403 ** OP_Destroy 5 0
118404 **
118405 ** and root page 5 happened to be the largest root-page number in the
118406 ** database, then root page 5 would be moved to page 4 by the
118407 ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
118408 ** a free-list page.
118409 */
118410 Pgno iTab = pTab->tnum;
118411 Pgno iDestroyed = 0;
118412
118413 while( 1 ){
118414 Index *pIdx;
118415 Pgno iLargest = 0;
118416
118417 if( iDestroyed==0 || iTab<iDestroyed ){
118418 iLargest = iTab;
118419 }
118420 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
118421 Pgno iIdx = pIdx->tnum;
118422 assert( pIdx->pSchema==pTab->pSchema )((void) (0));
118423 if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
118424 iLargest = iIdx;
118425 }
118426 }
118427 if( iLargest==0 ){
118428 return;
118429 }else{
118430 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
118431 assert( iDb>=0 && iDb<pParse->db->nDb )((void) (0));
118432 destroyRootPage(pParse, iLargest, iDb);
118433 iDestroyed = iLargest;
118434 }
118435 }
118436}
118437
118438/*
118439** Remove entries from the sqlite_statN tables (for N in (1,2,3))
118440** after a DROP INDEX or DROP TABLE command.
118441*/
118442static void sqlite3ClearStatTables(
118443 Parse *pParse, /* The parsing context */
118444 int iDb, /* The database number */
118445 const char *zType, /* "idx" or "tbl" */
118446 const char *zName /* Name of index or table */
118447){
118448 int i;
118449 const char *zDbName = pParse->db->aDb[iDb].zDbSName;
118450 for(i=1; i<=4; i++){
118451 char zTab[24];
118452 sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
118453 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
118454 sqlite3NestedParse(pParse,
118455 "DELETE FROM %Q.%s WHERE %s=%Q",
118456 zDbName, zTab, zType, zName
118457 );
118458 }
118459 }
118460}
118461
118462/*
118463** Generate code to drop a table.
118464*/
118465SQLITE_PRIVATEstatic void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
118466 Vdbe *v;
118467 sqlite3 *db = pParse->db;
118468 Trigger *pTrigger;
118469 Db *pDb = &db->aDb[iDb];
118470
118471 v = sqlite3GetVdbe(pParse);
118472 assert( v!=0 )((void) (0));
118473 sqlite3BeginWriteOperation(pParse, 1, iDb);
118474
118475#ifndef SQLITE_OMIT_VIRTUALTABLE
118476 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
118477 sqlite3VdbeAddOp0(v, OP_VBegin170);
118478 }
118479#endif
118480
118481 /* Drop all triggers associated with the table being dropped. Code
118482 ** is generated to remove entries from sqlite_schema and/or
118483 ** sqlite_temp_schema if required.
118484 */
118485 pTrigger = sqlite3TriggerList(pParse, pTab);
118486 while( pTrigger ){
118487 assert( pTrigger->pSchema==pTab->pSchema ||((void) (0))
118488 pTrigger->pSchema==db->aDb[1].pSchema )((void) (0));
118489 sqlite3DropTriggerPtr(pParse, pTrigger);
118490 pTrigger = pTrigger->pNext;
118491 }
118492
118493#ifndef SQLITE_OMIT_AUTOINCREMENT
118494 /* Remove any entries of the sqlite_sequence table associated with
118495 ** the table being dropped. This is done before the table is dropped
118496 ** at the btree level, in case the sqlite_sequence table needs to
118497 ** move as a result of the drop (can happen in auto-vacuum mode).
118498 */
118499 if( pTab->tabFlags & TF_Autoincrement0x00000008 ){
118500 sqlite3NestedParse(pParse,
118501 "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
118502 pDb->zDbSName, pTab->zName
118503 );
118504 }
118505#endif
118506
118507 /* Drop all entries in the schema table that refer to the
118508 ** table. The program name loops through the schema table and deletes
118509 ** every row that refers to a table of the same name as the one being
118510 ** dropped. Triggers are handled separately because a trigger can be
118511 ** created in the temp database that refers to a table in another
118512 ** database.
118513 */
118514 sqlite3NestedParse(pParse,
118515 "DELETE FROM %Q." LEGACY_SCHEMA_TABLE"sqlite_master"
118516 " WHERE tbl_name=%Q and type!='trigger'",
118517 pDb->zDbSName, pTab->zName);
118518 if( !isView && !IsVirtual(pTab)((pTab)->eTabType==1) ){
118519 destroyTable(pParse, pTab);
118520 }
118521
118522 /* Remove the table entry from SQLite's internal schema and modify
118523 ** the schema cookie.
118524 */
118525 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
118526 sqlite3VdbeAddOp4(v, OP_VDestroy172, iDb, 0, 0, pTab->zName, 0);
118527 sqlite3MayAbort(pParse);
118528 }
118529 sqlite3VdbeAddOp4(v, OP_DropTable151, iDb, 0, 0, pTab->zName, 0);
118530 sqlite3ChangeCookie(pParse, iDb);
118531 sqliteViewResetAll(db, iDb);
118532}
118533
118534/*
118535** Return TRUE if shadow tables should be read-only in the current
118536** context.
118537*/
118538SQLITE_PRIVATEstatic int sqlite3ReadOnlyShadowTables(sqlite3 *db){
118539#ifndef SQLITE_OMIT_VIRTUALTABLE
118540 if( (db->flags & SQLITE_Defensive0x10000000)!=0
118541 && db->pVtabCtx==0
118542 && db->nVdbeExec==0
118543 && !sqlite3VtabInSync(db)((db)->nVTrans>0 && (db)->aVTrans==0)
118544 ){
118545 return 1;
118546 }
118547#endif
118548 return 0;
118549}
118550
118551/*
118552** Return true if it is not allowed to drop the given table
118553*/
118554static int tableMayNotBeDropped(sqlite3 *db, Table *pTab){
118555 if( sqlite3StrNICmpsqlite3_strnicmp(pTab->zName, "sqlite_", 7)==0 ){
118556 if( sqlite3StrNICmpsqlite3_strnicmp(pTab->zName+7, "stat", 4)==0 ) return 0;
118557 if( sqlite3StrNICmpsqlite3_strnicmp(pTab->zName+7, "parameters", 10)==0 ) return 0;
118558 return 1;
118559 }
118560 if( (pTab->tabFlags & TF_Shadow0x00001000)!=0 && sqlite3ReadOnlyShadowTables(db) ){
118561 return 1;
118562 }
118563 if( pTab->tabFlags & TF_Eponymous0x00008000 ){
118564 return 1;
118565 }
118566 return 0;
118567}
118568
118569/*
118570** This routine is called to do the work of a DROP TABLE statement.
118571** pName is the name of the table to be dropped.
118572*/
118573SQLITE_PRIVATEstatic void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
118574 Table *pTab;
118575 Vdbe *v;
118576 sqlite3 *db = pParse->db;
118577 int iDb;
118578
118579 if( db->mallocFailed ){
118580 goto exit_drop_table;
118581 }
118582 assert( pParse->nErr==0 )((void) (0));
118583 assert( pName->nSrc==1 )((void) (0));
118584 if( sqlite3ReadSchema(pParse) ) goto exit_drop_table;
118585 if( noErr ) db->suppressErr++;
118586 assert( isView==0 || isView==LOCATE_VIEW )((void) (0));
118587 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
118588 if( noErr ) db->suppressErr--;
118589
118590 if( pTab==0 ){
118591 if( noErr ){
118592 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
118593 sqlite3ForceNotReadOnly(pParse);
118594 }
118595 goto exit_drop_table;
118596 }
118597 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
118598 assert( iDb>=0 && iDb<db->nDb )((void) (0));
118599
118600 /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
118601 ** it is initialized.
118602 */
118603 if( IsVirtual(pTab)((pTab)->eTabType==1) && sqlite3ViewGetColumnNames(pParse, pTab) ){
118604 goto exit_drop_table;
118605 }
118606#ifndef SQLITE_OMIT_AUTHORIZATION
118607 {
118608 int code;
118609 const char *zTab = SCHEMA_TABLE(iDb)((!0)&&(iDb==1)?"sqlite_temp_master":"sqlite_master");
118610 const char *zDb = db->aDb[iDb].zDbSName;
118611 const char *zArg2 = 0;
118612 if( sqlite3AuthCheck(pParse, SQLITE_DELETE9, zTab, 0, zDb)){
118613 goto exit_drop_table;
118614 }
118615 if( isView ){
118616 if( !OMIT_TEMPDB0 && iDb==1 ){
118617 code = SQLITE_DROP_TEMP_VIEW15;
118618 }else{
118619 code = SQLITE_DROP_VIEW17;
118620 }
118621#ifndef SQLITE_OMIT_VIRTUALTABLE
118622 }else if( IsVirtual(pTab)((pTab)->eTabType==1) ){
118623 code = SQLITE_DROP_VTABLE30;
118624 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
118625#endif
118626 }else{
118627 if( !OMIT_TEMPDB0 && iDb==1 ){
118628 code = SQLITE_DROP_TEMP_TABLE13;
118629 }else{
118630 code = SQLITE_DROP_TABLE11;
118631 }
118632 }
118633 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
118634 goto exit_drop_table;
118635 }
118636 if( sqlite3AuthCheck(pParse, SQLITE_DELETE9, pTab->zName, 0, zDb) ){
118637 goto exit_drop_table;
118638 }
118639 }
118640#endif
118641 if( tableMayNotBeDropped(db, pTab) ){
118642 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
118643 goto exit_drop_table;
118644 }
118645
118646#ifndef SQLITE_OMIT_VIEW
118647 /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
118648 ** on a table.
118649 */
118650 if( isView && !IsView(pTab)((pTab)->eTabType==2) ){
118651 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
118652 goto exit_drop_table;
118653 }
118654 if( !isView && IsView(pTab)((pTab)->eTabType==2) ){
118655 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
118656 goto exit_drop_table;
118657 }
118658#endif
118659
118660 /* Generate code to remove the table from the schema table
118661 ** on disk.
118662 */
118663 v = sqlite3GetVdbe(pParse);
118664 if( v ){
118665 sqlite3BeginWriteOperation(pParse, 1, iDb);
118666 if( !isView ){
118667 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
118668 sqlite3FkDropTable(pParse, pName, pTab);
118669 }
118670 sqlite3CodeDropTable(pParse, pTab, iDb, isView);
118671 }
118672
118673exit_drop_table:
118674 sqlite3SrcListDelete(db, pName);
118675}
118676
118677/*
118678** This routine is called to create a new foreign key on the table
118679** currently under construction. pFromCol determines which columns
118680** in the current table point to the foreign key. If pFromCol==0 then
118681** connect the key to the last column inserted. pTo is the name of
118682** the table referred to (a.k.a the "parent" table). pToCol is a list
118683** of tables in the parent pTo table. flags contains all
118684** information about the conflict resolution algorithms specified
118685** in the ON DELETE, ON UPDATE and ON INSERT clauses.
118686**
118687** An FKey structure is created and added to the table currently
118688** under construction in the pParse->pNewTable field.
118689**
118690** The foreign key is set for IMMEDIATE processing. A subsequent call
118691** to sqlite3DeferForeignKey() might change this to DEFERRED.
118692*/
118693SQLITE_PRIVATEstatic void sqlite3CreateForeignKey(
118694 Parse *pParse, /* Parsing context */
118695 ExprList *pFromCol, /* Columns in this table that point to other table */
118696 Token *pTo, /* Name of the other table */
118697 ExprList *pToCol, /* Columns in the other table */
118698 int flags /* Conflict resolution algorithms. */
118699){
118700 sqlite3 *db = pParse->db;
118701#ifndef SQLITE_OMIT_FOREIGN_KEY
118702 FKey *pFKey = 0;
118703 FKey *pNextTo;
118704 Table *p = pParse->pNewTable;
118705 i64 nByte;
118706 int i;
118707 int nCol;
118708 char *z;
118709
118710 assert( pTo!=0 )((void) (0));
118711 if( p==0 || IN_DECLARE_VTAB(pParse->eParseMode==1) ) goto fk_end;
118712 if( pFromCol==0 ){
118713 int iCol = p->nCol-1;
118714 if( NEVER(iCol<0)(iCol<0) ) goto fk_end;
118715 if( pToCol && pToCol->nExpr!=1 ){
118716 sqlite3ErrorMsg(pParse, "foreign key on %s"
118717 " should reference only one column of table %T",
118718 p->aCol[iCol].zCnName, pTo);
118719 goto fk_end;
118720 }
118721 nCol = 1;
118722 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
118723 sqlite3ErrorMsg(pParse,
118724 "number of columns in foreign key does not match the number of "
118725 "columns in the referenced table");
118726 goto fk_end;
118727 }else{
118728 nCol = pFromCol->nExpr;
118729 }
118730 nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
118731 if( pToCol ){
118732 for(i=0; i<pToCol->nExpr; i++){
118733 nByte += sqlite3Strlen30(pToCol->a[i].zEName) + 1;
118734 }
118735 }
118736 pFKey = sqlite3DbMallocZero(db, nByte );
118737 if( pFKey==0 ){
118738 goto fk_end;
118739 }
118740 pFKey->pFrom = p;
118741 assert( IsOrdinaryTable(p) )((void) (0));
118742 pFKey->pNextFrom = p->u.tab.pFKey;
118743 z = (char*)&pFKey->aCol[nCol];
118744 pFKey->zTo = z;
118745 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
118746 sqlite3RenameTokenMap(pParse, (void*)z, pTo);
118747 }
118748 memcpy(z, pTo->z, pTo->n);
118749 z[pTo->n] = 0;
118750 sqlite3Dequote(z);
118751 z += pTo->n+1;
118752 pFKey->nCol = nCol;
118753 if( pFromCol==0 ){
118754 pFKey->aCol[0].iFrom = p->nCol-1;
118755 }else{
118756 for(i=0; i<nCol; i++){
118757 int j;
118758 for(j=0; j<p->nCol; j++){
118759 if( sqlite3StrICmp(p->aCol[j].zCnName, pFromCol->a[i].zEName)==0 ){
118760 pFKey->aCol[i].iFrom = j;
118761 break;
118762 }
118763 }
118764 if( j>=p->nCol ){
118765 sqlite3ErrorMsg(pParse,
118766 "unknown column \"%s\" in foreign key definition",
118767 pFromCol->a[i].zEName);
118768 goto fk_end;
118769 }
118770 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
118771 sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zEName);
118772 }
118773 }
118774 }
118775 if( pToCol ){
118776 for(i=0; i<nCol; i++){
118777 int n = sqlite3Strlen30(pToCol->a[i].zEName);
118778 pFKey->aCol[i].zCol = z;
118779 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
118780 sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zEName);
118781 }
118782 memcpy(z, pToCol->a[i].zEName, n);
118783 z[n] = 0;
118784 z += n+1;
118785 }
118786 }
118787 pFKey->isDeferred = 0;
118788 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */
118789 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */
118790
118791 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) )((void) (0));
118792 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
118793 pFKey->zTo, (void *)pFKey
118794 );
118795 if( pNextTo==pFKey ){
118796 sqlite3OomFault(db);
118797 goto fk_end;
118798 }
118799 if( pNextTo ){
118800 assert( pNextTo->pPrevTo==0 )((void) (0));
118801 pFKey->pNextTo = pNextTo;
118802 pNextTo->pPrevTo = pFKey;
118803 }
118804
118805 /* Link the foreign key to the table as the last step.
118806 */
118807 assert( IsOrdinaryTable(p) )((void) (0));
118808 p->u.tab.pFKey = pFKey;
118809 pFKey = 0;
118810
118811fk_end:
118812 sqlite3DbFree(db, pFKey);
118813#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
118814 sqlite3ExprListDelete(db, pFromCol);
118815 sqlite3ExprListDelete(db, pToCol);
118816}
118817
118818/*
118819** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
118820** clause is seen as part of a foreign key definition. The isDeferred
118821** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
118822** The behavior of the most recently created foreign key is adjusted
118823** accordingly.
118824*/
118825SQLITE_PRIVATEstatic void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
118826#ifndef SQLITE_OMIT_FOREIGN_KEY
118827 Table *pTab;
118828 FKey *pFKey;
118829 if( (pTab = pParse->pNewTable)==0 ) return;
118830 if( NEVER(!IsOrdinaryTable(pTab))(!((pTab)->eTabType==0)) ) return;
118831 if( (pFKey = pTab->u.tab.pFKey)==0 ) return;
118832 assert( isDeferred==0 || isDeferred==1 )((void) (0)); /* EV: R-30323-21917 */
118833 pFKey->isDeferred = (u8)isDeferred;
118834#endif
118835}
118836
118837/*
118838** Generate code that will erase and refill index *pIdx. This is
118839** used to initialize a newly created index or to recompute the
118840** content of an index in response to a REINDEX command.
118841**
118842** if memRootPage is not negative, it means that the index is newly
118843** created. The register specified by memRootPage contains the
118844** root page number of the index. If memRootPage is negative, then
118845** the index already exists and must be cleared before being refilled and
118846** the root page number of the index is taken from pIndex->tnum.
118847*/
118848static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
118849 Table *pTab = pIndex->pTable; /* The table that is indexed */
118850 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
118851 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
118852 int iSorter; /* Cursor opened by OpenSorter (if in use) */
118853 int addr1; /* Address of top of loop */
118854 int addr2; /* Address to jump to for next iteration */
118855 Pgno tnum; /* Root page of index */
118856 int iPartIdxLabel; /* Jump to this label to skip a row */
118857 Vdbe *v; /* Generate code into this virtual machine */
118858 KeyInfo *pKey; /* KeyInfo for index */
118859 int regRecord; /* Register holding assembled index record */
118860 sqlite3 *db = pParse->db; /* The database connection */
118861 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
118862
118863#ifndef SQLITE_OMIT_AUTHORIZATION
118864 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX27, pIndex->zName, 0,
118865 db->aDb[iDb].zDbSName ) ){
118866 return;
118867 }
118868#endif
118869
118870 /* Require a write-lock on the table to perform this operation */
118871 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
118872
118873 v = sqlite3GetVdbe(pParse);
118874 if( v==0 ) return;
118875 if( memRootPage>=0 ){
118876 tnum = (Pgno)memRootPage;
118877 }else{
118878 tnum = pIndex->tnum;
118879 }
118880 pKey = sqlite3KeyInfoOfIndex(pParse, pIndex);
118881 assert( pKey!=0 || pParse->nErr )((void) (0));
118882
118883 /* Open the sorter cursor if we are to use one. */
118884 iSorter = pParse->nTab++;
118885 sqlite3VdbeAddOp4(v, OP_SorterOpen119, iSorter, 0, pIndex->nKeyCol, (char*)
118886 sqlite3KeyInfoRef(pKey), P4_KEYINFO(-8));
118887
118888 /* Open the table. Loop through all rows of the table, inserting index
118889 ** records into the sorter. */
118890 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead112);
118891 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind35, iTab, 0); VdbeCoverage(v);
118892 regRecord = sqlite3GetTempReg(pParse);
118893 sqlite3MultiWrite(pParse);
118894
118895 sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
118896 sqlite3VdbeAddOp2(v, OP_SorterInsert139, iSorter, regRecord);
118897 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
118898 sqlite3VdbeAddOp2(v, OP_Next38, iTab, addr1+1); VdbeCoverage(v);
118899 sqlite3VdbeJumpHere(v, addr1);
118900 if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear145, tnum, iDb);
118901 sqlite3VdbeAddOp4(v, OP_OpenWrite113, iIdx, (int)tnum, iDb,
118902 (char *)pKey, P4_KEYINFO(-8));
118903 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR0x01|((memRootPage>=0)?OPFLAG_P2ISREG0x10:0));
118904
118905 addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort33, iSorter, 0); VdbeCoverage(v);
118906 if( IsUniqueIndex(pIndex)((pIndex)->onError!=0) ){
118907 int j2 = sqlite3VdbeGoto(v, 1);
118908 addr2 = sqlite3VdbeCurrentAddr(v);
118909 sqlite3VdbeVerifyAbortable(v, OE_Abort);
118910 sqlite3VdbeAddOp4Int(v, OP_SorterCompare132, iSorter, j2, regRecord,
118911 pIndex->nKeyCol); VdbeCoverage(v);
118912 sqlite3UniqueConstraint(pParse, OE_Abort2, pIndex);
118913 sqlite3VdbeJumpHere(v, j2);
118914 }else{
118915 /* Most CREATE INDEX and REINDEX statements that are not UNIQUE can not
118916 ** abort. The exception is if one of the indexed expressions contains a
118917 ** user function that throws an exception when it is evaluated. But the
118918 ** overhead of adding a statement journal to a CREATE INDEX statement is
118919 ** very small (since most of the pages written do not contain content that
118920 ** needs to be restored if the statement aborts), so we call
118921 ** sqlite3MayAbort() for all CREATE INDEX statements. */
118922 sqlite3MayAbort(pParse);
118923 addr2 = sqlite3VdbeCurrentAddr(v);
118924 }
118925 sqlite3VdbeAddOp3(v, OP_SorterData133, iSorter, regRecord, iIdx);
118926 if( !pIndex->bAscKeyBug ){
118927 /* This OP_SeekEnd opcode makes index insert for a REINDEX go much
118928 ** faster by avoiding unnecessary seeks. But the optimization does
118929 ** not work for UNIQUE constraint indexes on WITHOUT ROWID tables
118930 ** with DESC primary keys, since those indexes have there keys in
118931 ** a different order from the main table.
118932 ** See ticket: https://www.sqlite.org/src/info/bba7b69f9849b5bf
118933 */
118934 sqlite3VdbeAddOp1(v, OP_SeekEnd137, iIdx);
118935 }
118936 sqlite3VdbeAddOp2(v, OP_IdxInsert138, iIdx, regRecord);
118937 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT0x10);
118938 sqlite3ReleaseTempReg(pParse, regRecord);
118939 sqlite3VdbeAddOp2(v, OP_SorterNext36, iSorter, addr2); VdbeCoverage(v);
118940 sqlite3VdbeJumpHere(v, addr1);
118941
118942 sqlite3VdbeAddOp1(v, OP_Close122, iTab);
118943 sqlite3VdbeAddOp1(v, OP_Close122, iIdx);
118944 sqlite3VdbeAddOp1(v, OP_Close122, iSorter);
118945}
118946
118947/*
118948** Allocate heap space to hold an Index object with nCol columns.
118949**
118950** Increase the allocation size to provide an extra nExtra bytes
118951** of 8-byte aligned space after the Index object and return a
118952** pointer to this extra space in *ppExtra.
118953*/
118954SQLITE_PRIVATEstatic Index *sqlite3AllocateIndexObject(
118955 sqlite3 *db, /* Database connection */
118956 i16 nCol, /* Total number of columns in the index */
118957 int nExtra, /* Number of bytes of extra space to alloc */
118958 char **ppExtra /* Pointer to the "extra" space */
118959){
118960 Index *p; /* Allocated index object */
118961 int nByte; /* Bytes of space for Index object + arrays */
118962
118963 nByte = ROUND8(sizeof(Index))(((sizeof(Index))+7)&~7) + /* Index structure */
118964 ROUND8(sizeof(char*)*nCol)(((sizeof(char*)*nCol)+7)&~7) + /* Index.azColl */
118965 ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */(((sizeof(LogEst)*(nCol+1) + sizeof(i16)*nCol + sizeof(u8)*nCol
)+7)&~7)
118966 sizeof(i16)*nCol + /* Index.aiColumn */(((sizeof(LogEst)*(nCol+1) + sizeof(i16)*nCol + sizeof(u8)*nCol
)+7)&~7)
118967 sizeof(u8)*nCol)(((sizeof(LogEst)*(nCol+1) + sizeof(i16)*nCol + sizeof(u8)*nCol
)+7)&~7)
; /* Index.aSortOrder */
118968 p = sqlite3DbMallocZero(db, nByte + nExtra);
118969 if( p ){
118970 char *pExtra = ((char*)p)+ROUND8(sizeof(Index))(((sizeof(Index))+7)&~7);
118971 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol)(((sizeof(char*)*nCol)+7)&~7);
118972 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
118973 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
118974 p->aSortOrder = (u8*)pExtra;
118975 p->nColumn = nCol;
118976 p->nKeyCol = nCol - 1;
118977 *ppExtra = ((char*)p) + nByte;
118978 }
118979 return p;
118980}
118981
118982/*
118983** If expression list pList contains an expression that was parsed with
118984** an explicit "NULLS FIRST" or "NULLS LAST" clause, leave an error in
118985** pParse and return non-zero. Otherwise, return zero.
118986*/
118987SQLITE_PRIVATEstatic int sqlite3HasExplicitNulls(Parse *pParse, ExprList *pList){
118988 if( pList ){
118989 int i;
118990 for(i=0; i<pList->nExpr; i++){
118991 if( pList->a[i].fg.bNulls ){
118992 u8 sf = pList->a[i].fg.sortFlags;
118993 sqlite3ErrorMsg(pParse, "unsupported use of NULLS %s",
118994 (sf==0 || sf==3) ? "FIRST" : "LAST"
118995 );
118996 return 1;
118997 }
118998 }
118999 }
119000 return 0;
119001}
119002
119003/*
119004** Create a new index for an SQL table. pName1.pName2 is the name of the index
119005** and pTblList is the name of the table that is to be indexed. Both will
119006** be NULL for a primary key or an index that is created to satisfy a
119007** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
119008** as the table to be indexed. pParse->pNewTable is a table that is
119009** currently being constructed by a CREATE TABLE statement.
119010**
119011** pList is a list of columns to be indexed. pList will be NULL if this
119012** is a primary key or unique-constraint on the most recent column added
119013** to the table currently under construction.
119014*/
119015SQLITE_PRIVATEstatic void sqlite3CreateIndex(
119016 Parse *pParse, /* All information about this parse */
119017 Token *pName1, /* First part of index name. May be NULL */
119018 Token *pName2, /* Second part of index name. May be NULL */
119019 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
119020 ExprList *pList, /* A list of columns to be indexed */
119021 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
119022 Token *pStart, /* The CREATE token that begins this statement */
119023 Expr *pPIWhere, /* WHERE clause for partial indices */
119024 int sortOrder, /* Sort order of primary key when pList==NULL */
119025 int ifNotExist, /* Omit error if index already exists */
119026 u8 idxType /* The index type */
119027){
119028 Table *pTab = 0; /* Table to be indexed */
119029 Index *pIndex = 0; /* The index to be created */
119030 char *zName = 0; /* Name of the index */
119031 int nName; /* Number of characters in zName */
119032 int i, j;
119033 DbFixer sFix; /* For assigning database names to pTable */
119034 int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */
119035 sqlite3 *db = pParse->db;
119036 Db *pDb; /* The specific table containing the indexed database */
119037 int iDb; /* Index of the database that is being written */
119038 Token *pName = 0; /* Unqualified name of the index to create */
119039 struct ExprList_item *pListItem; /* For looping over pList */
119040 int nExtra = 0; /* Space allocated for zExtra[] */
119041 int nExtraCol; /* Number of extra columns needed */
119042 char *zExtra = 0; /* Extra space after the Index object */
119043 Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */
119044
119045 assert( db->pParse==pParse )((void) (0));
119046 if( pParse->nErr ){
119047 goto exit_create_index;
119048 }
119049 assert( db->mallocFailed==0 )((void) (0));
119050 if( IN_DECLARE_VTAB(pParse->eParseMode==1) && idxType!=SQLITE_IDXTYPE_PRIMARYKEY2 ){
119051 goto exit_create_index;
119052 }
119053 if( SQLITE_OK0!=sqlite3ReadSchema(pParse) ){
119054 goto exit_create_index;
119055 }
119056 if( sqlite3HasExplicitNulls(pParse, pList) ){
119057 goto exit_create_index;
119058 }
119059
119060 /*
119061 ** Find the table that is to be indexed. Return early if not found.
119062 */
119063 if( pTblName!=0 ){
119064
119065 /* Use the two-part index name to determine the database
119066 ** to search for the table. 'Fix' the table name to this db
119067 ** before looking up the table.
119068 */
119069 assert( pName1 && pName2 )((void) (0));
119070 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
119071 if( iDb<0 ) goto exit_create_index;
119072 assert( pName && pName->z )((void) (0));
119073
119074#ifndef SQLITE_OMIT_TEMPDB
119075 /* If the index name was unqualified, check if the table
119076 ** is a temp table. If so, set the database to 1. Do not do this
119077 ** if initialising a database schema.
119078 */
119079 if( !db->init.busy ){
119080 pTab = sqlite3SrcListLookup(pParse, pTblName);
119081 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
119082 iDb = 1;
119083 }
119084 }
119085#endif
119086
119087 sqlite3FixInit(&sFix, pParse, iDb, "index", pName);
119088 if( sqlite3FixSrcList(&sFix, pTblName) ){
119089 /* Because the parser constructs pTblName from a single identifier,
119090 ** sqlite3FixSrcList can never fail. */
119091 assert(0)((void) (0));
119092 }
119093 pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
119094 assert( db->mallocFailed==0 || pTab==0 )((void) (0));
119095 if( pTab==0 ) goto exit_create_index;
119096 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
119097 sqlite3ErrorMsg(pParse,
119098 "cannot create a TEMP index on non-TEMP table \"%s\"",
119099 pTab->zName);
119100 goto exit_create_index;
119101 }
119102 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ) pPk = sqlite3PrimaryKeyIndex(pTab);
119103 }else{
119104 assert( pName==0 )((void) (0));
119105 assert( pStart==0 )((void) (0));
119106 pTab = pParse->pNewTable;
119107 if( !pTab ) goto exit_create_index;
119108 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
119109 }
119110 pDb = &db->aDb[iDb];
119111
119112 assert( pTab!=0 )((void) (0));
119113 if( sqlite3StrNICmpsqlite3_strnicmp(pTab->zName, "sqlite_", 7)==0
119114 && db->init.busy==0
119115 && pTblName!=0
119116#if SQLITE_USER_AUTHENTICATION
119117 && sqlite3UserAuthTable(pTab->zName)==0
119118#endif
119119 ){
119120 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
119121 goto exit_create_index;
119122 }
119123#ifndef SQLITE_OMIT_VIEW
119124 if( IsView(pTab)((pTab)->eTabType==2) ){
119125 sqlite3ErrorMsg(pParse, "views may not be indexed");
119126 goto exit_create_index;
119127 }
119128#endif
119129#ifndef SQLITE_OMIT_VIRTUALTABLE
119130 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
119131 sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
119132 goto exit_create_index;
119133 }
119134#endif
119135
119136 /*
119137 ** Find the name of the index. Make sure there is not already another
119138 ** index or table with the same name.
119139 **
119140 ** Exception: If we are reading the names of permanent indices from the
119141 ** sqlite_schema table (because some other process changed the schema) and
119142 ** one of the index names collides with the name of a temporary table or
119143 ** index, then we will continue to process this index.
119144 **
119145 ** If pName==0 it means that we are
119146 ** dealing with a primary key or UNIQUE constraint. We have to invent our
119147 ** own name.
119148 */
119149 if( pName ){
119150 zName = sqlite3NameFromToken(db, pName);
119151 if( zName==0 ) goto exit_create_index;
119152 assert( pName->z!=0 )((void) (0));
119153 if( SQLITE_OK0!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){
119154 goto exit_create_index;
119155 }
119156 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
119157 if( !db->init.busy ){
119158 if( sqlite3FindTable(db, zName, 0)!=0 ){
119159 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
119160 goto exit_create_index;
119161 }
119162 }
119163 if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
119164 if( !ifNotExist ){
119165 sqlite3ErrorMsg(pParse, "index %s already exists", zName);
119166 }else{
119167 assert( !db->init.busy )((void) (0));
119168 sqlite3CodeVerifySchema(pParse, iDb);
119169 sqlite3ForceNotReadOnly(pParse);
119170 }
119171 goto exit_create_index;
119172 }
119173 }
119174 }else{
119175 int n;
119176 Index *pLoop;
119177 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
119178 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
119179 if( zName==0 ){
119180 goto exit_create_index;
119181 }
119182
119183 /* Automatic index names generated from within sqlite3_declare_vtab()
119184 ** must have names that are distinct from normal automatic index names.
119185 ** The following statement converts "sqlite3_autoindex..." into
119186 ** "sqlite3_butoindex..." in order to make the names distinct.
119187 ** The "vtab_err.test" test demonstrates the need of this statement. */
119188 if( IN_SPECIAL_PARSE(pParse->eParseMode!=0) ) zName[7]++;
119189 }
119190
119191 /* Check for authorization to create an index.
119192 */
119193#ifndef SQLITE_OMIT_AUTHORIZATION
119194 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
119195 const char *zDb = pDb->zDbSName;
119196 if( sqlite3AuthCheck(pParse, SQLITE_INSERT18, SCHEMA_TABLE(iDb)((!0)&&(iDb==1)?"sqlite_temp_master":"sqlite_master"), 0, zDb) ){
119197 goto exit_create_index;
119198 }
119199 i = SQLITE_CREATE_INDEX1;
119200 if( !OMIT_TEMPDB0 && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX3;
119201 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
119202 goto exit_create_index;
119203 }
119204 }
119205#endif
119206
119207 /* If pList==0, it means this routine was called to make a primary
119208 ** key out of the last column added to the table under construction.
119209 ** So create a fake list to simulate this.
119210 */
119211 if( pList==0 ){
119212 Token prevCol;
119213 Column *pCol = &pTab->aCol[pTab->nCol-1];
119214 pCol->colFlags |= COLFLAG_UNIQUE0x0008;
119215 sqlite3TokenInit(&prevCol, pCol->zCnName);
119216 pList = sqlite3ExprListAppend(pParse, 0,
119217 sqlite3ExprAlloc(db, TK_ID59, &prevCol, 0));
119218 if( pList==0 ) goto exit_create_index;
119219 assert( pList->nExpr==1 )((void) (0));
119220 sqlite3ExprListSetSortOrder(pList, sortOrder, SQLITE_SO_UNDEFINED-1);
119221 }else{
119222 sqlite3ExprListCheckLength(pParse, pList, "index");
119223 if( pParse->nErr ) goto exit_create_index;
119224 }
119225
119226 /* Figure out how many bytes of space are required to store explicitly
119227 ** specified collation sequence names.
119228 */
119229 for(i=0; i<pList->nExpr; i++){
119230 Expr *pExpr = pList->a[i].pExpr;
119231 assert( pExpr!=0 )((void) (0));
119232 if( pExpr->op==TK_COLLATE113 ){
119233 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
119234 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
119235 }
119236 }
119237
119238 /*
119239 ** Allocate the index structure.
119240 */
119241 nName = sqlite3Strlen30(zName);
119242 nExtraCol = pPk ? pPk->nKeyCol : 1;
119243 assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ )((void) (0));
119244 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
119245 nName + nExtra + 1, &zExtra);
119246 if( db->mallocFailed ){
119247 goto exit_create_index;
119248 }
119249 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) )((void) (0));
119250 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) )((void) (0));
119251 pIndex->zName = zExtra;
119252 zExtra += nName + 1;
119253 memcpy(pIndex->zName, zName, nName+1);
119254 pIndex->pTable = pTab;
119255 pIndex->onError = (u8)onError;
119256 pIndex->uniqNotNull = onError!=OE_None0;
119257 pIndex->idxType = idxType;
119258 pIndex->pSchema = db->aDb[iDb].pSchema;
119259 pIndex->nKeyCol = pList->nExpr;
119260 if( pPIWhere ){
119261 sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx0x000002, pPIWhere, 0);
119262 pIndex->pPartIdxWhere = pPIWhere;
119263 pPIWhere = 0;
119264 }
119265 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
119266
119267 /* Check to see if we should honor DESC requests on index columns
119268 */
119269 if( pDb->pSchema->file_format>=4 ){
119270 sortOrderMask = -1; /* Honor DESC */
119271 }else{
119272 sortOrderMask = 0; /* Ignore DESC */
119273 }
119274
119275 /* Analyze the list of expressions that form the terms of the index and
119276 ** report any errors. In the common case where the expression is exactly
119277 ** a table column, store that column in aiColumn[]. For general expressions,
119278 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
119279 **
119280 ** TODO: Issue a warning if two or more columns of the index are identical.
119281 ** TODO: Issue a warning if the table primary key is used as part of the
119282 ** index key.
119283 */
119284 pListItem = pList->a;
119285 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
119286 pIndex->aColExpr = pList;
119287 pList = 0;
119288 }
119289 for(i=0; i<pIndex->nKeyCol; i++, pListItem++){
119290 Expr *pCExpr; /* The i-th index expression */
119291 int requestedSortOrder; /* ASC or DESC on the i-th expression */
119292 const char *zColl; /* Collation sequence name */
119293
119294 sqlite3StringToId(pListItem->pExpr);
119295 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr0x000020, pListItem->pExpr, 0);
119296 if( pParse->nErr ) goto exit_create_index;
119297 pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr);
119298 if( pCExpr->op!=TK_COLUMN167 ){
119299 if( pTab==pParse->pNewTable ){
119300 sqlite3ErrorMsg(pParse, "expressions prohibited in PRIMARY KEY and "
119301 "UNIQUE constraints");
119302 goto exit_create_index;
119303 }
119304 if( pIndex->aColExpr==0 ){
119305 pIndex->aColExpr = pList;
119306 pList = 0;
119307 }
119308 j = XN_EXPR(-2);
119309 pIndex->aiColumn[i] = XN_EXPR(-2);
119310 pIndex->uniqNotNull = 0;
119311 }else{
119312 j = pCExpr->iColumn;
119313 assert( j<=0x7fff )((void) (0));
119314 if( j<0 ){
119315 j = pTab->iPKey;
119316 }else{
119317 if( pTab->aCol[j].notNull==0 ){
119318 pIndex->uniqNotNull = 0;
119319 }
119320 if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL0x0020 ){
119321 pIndex->bHasVCol = 1;
119322 }
119323 }
119324 pIndex->aiColumn[i] = (i16)j;
119325 }
119326 zColl = 0;
119327 if( pListItem->pExpr->op==TK_COLLATE113 ){
119328 int nColl;
119329 assert( !ExprHasProperty(pListItem->pExpr, EP_IntValue) )((void) (0));
119330 zColl = pListItem->pExpr->u.zToken;
119331 nColl = sqlite3Strlen30(zColl) + 1;
119332 assert( nExtra>=nColl )((void) (0));
119333 memcpy(zExtra, zColl, nColl);
119334 zColl = zExtra;
119335 zExtra += nColl;
119336 nExtra -= nColl;
119337 }else if( j>=0 ){
119338 zColl = sqlite3ColumnColl(&pTab->aCol[j]);
119339 }
119340 if( !zColl ) zColl = sqlite3StrBINARY;
119341 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
119342 goto exit_create_index;
119343 }
119344 pIndex->azColl[i] = zColl;
119345 requestedSortOrder = pListItem->fg.sortFlags & sortOrderMask;
119346 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
119347 }
119348
119349 /* Append the table key to the end of the index. For WITHOUT ROWID
119350 ** tables (when pPk!=0) this will be the declared PRIMARY KEY. For
119351 ** normal tables (when pPk==0) this will be the rowid.
119352 */
119353 if( pPk ){
119354 for(j=0; j<pPk->nKeyCol; j++){
119355 int x = pPk->aiColumn[j];
119356 assert( x>=0 )((void) (0));
119357 if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){
119358 pIndex->nColumn--;
119359 }else{
119360 testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) );
119361 pIndex->aiColumn[i] = x;
119362 pIndex->azColl[i] = pPk->azColl[j];
119363 pIndex->aSortOrder[i] = pPk->aSortOrder[j];
119364 i++;
119365 }
119366 }
119367 assert( i==pIndex->nColumn )((void) (0));
119368 }else{
119369 pIndex->aiColumn[i] = XN_ROWID(-1);
119370 pIndex->azColl[i] = sqlite3StrBINARY;
119371 }
119372 sqlite3DefaultRowEst(pIndex);
119373 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
119374
119375 /* If this index contains every column of its table, then mark
119376 ** it as a covering index */
119377 assert( HasRowid(pTab)((void) (0))
119378 || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 )((void) (0));
119379 recomputeColumnsNotIndexed(pIndex);
119380 if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
119381 pIndex->isCovering = 1;
119382 for(j=0; j<pTab->nCol; j++){
119383 if( j==pTab->iPKey ) continue;
119384 if( sqlite3TableColumnToIndex(pIndex,j)>=0 ) continue;
119385 pIndex->isCovering = 0;
119386 break;
119387 }
119388 }
119389
119390 if( pTab==pParse->pNewTable ){
119391 /* This routine has been called to create an automatic index as a
119392 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
119393 ** a PRIMARY KEY or UNIQUE clause following the column definitions.
119394 ** i.e. one of:
119395 **
119396 ** CREATE TABLE t(x PRIMARY KEY, y);
119397 ** CREATE TABLE t(x, y, UNIQUE(x, y));
119398 **
119399 ** Either way, check to see if the table already has such an index. If
119400 ** so, don't bother creating this one. This only applies to
119401 ** automatically created indices. Users can do as they wish with
119402 ** explicit indices.
119403 **
119404 ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
119405 ** (and thus suppressing the second one) even if they have different
119406 ** sort orders.
119407 **
119408 ** If there are different collating sequences or if the columns of
119409 ** the constraint occur in different orders, then the constraints are
119410 ** considered distinct and both result in separate indices.
119411 */
119412 Index *pIdx;
119413 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
119414 int k;
119415 assert( IsUniqueIndex(pIdx) )((void) (0));
119416 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF )((void) (0));
119417 assert( IsUniqueIndex(pIndex) )((void) (0));
119418
119419 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
119420 for(k=0; k<pIdx->nKeyCol; k++){
119421 const char *z1;
119422 const char *z2;
119423 assert( pIdx->aiColumn[k]>=0 )((void) (0));
119424 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
119425 z1 = pIdx->azColl[k];
119426 z2 = pIndex->azColl[k];
119427 if( sqlite3StrICmp(z1, z2) ) break;
119428 }
119429 if( k==pIdx->nKeyCol ){
119430 if( pIdx->onError!=pIndex->onError ){
119431 /* This constraint creates the same index as a previous
119432 ** constraint specified somewhere in the CREATE TABLE statement.
119433 ** However the ON CONFLICT clauses are different. If both this
119434 ** constraint and the previous equivalent constraint have explicit
119435 ** ON CONFLICT clauses this is an error. Otherwise, use the
119436 ** explicitly specified behavior for the index.
119437 */
119438 if( !(pIdx->onError==OE_Default11 || pIndex->onError==OE_Default11) ){
119439 sqlite3ErrorMsg(pParse,
119440 "conflicting ON CONFLICT clauses specified", 0);
119441 }
119442 if( pIdx->onError==OE_Default11 ){
119443 pIdx->onError = pIndex->onError;
119444 }
119445 }
119446 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY2 ) pIdx->idxType = idxType;
119447 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
119448 pIndex->pNext = pParse->pNewIndex;
119449 pParse->pNewIndex = pIndex;
119450 pIndex = 0;
119451 }
119452 goto exit_create_index;
119453 }
119454 }
119455 }
119456
119457 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
119458
119459 /* Link the new Index structure to its table and to the other
119460 ** in-memory database structures.
119461 */
119462 assert( pParse->nErr==0 )((void) (0));
119463 if( db->init.busy ){
119464 Index *p;
119465 assert( !IN_SPECIAL_PARSE )((void) (0));
119466 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) )((void) (0));
119467 if( pTblName!=0 ){
119468 pIndex->tnum = db->init.newTnum;
119469 if( sqlite3IndexHasDuplicateRootPage(pIndex) ){
119470 sqlite3ErrorMsg(pParse, "invalid rootpage");
119471 pParse->rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(119471);
119472 goto exit_create_index;
119473 }
119474 }
119475 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
119476 pIndex->zName, pIndex);
119477 if( p ){
119478 assert( p==pIndex )((void) (0)); /* Malloc must have failed */
119479 sqlite3OomFault(db);
119480 goto exit_create_index;
119481 }
119482 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
119483 }
119484
119485 /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the
119486 ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
119487 ** emit code to allocate the index rootpage on disk and make an entry for
119488 ** the index in the sqlite_schema table and populate the index with
119489 ** content. But, do not do this if we are simply reading the sqlite_schema
119490 ** table to parse the schema, or if this index is the PRIMARY KEY index
119491 ** of a WITHOUT ROWID table.
119492 **
119493 ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY
119494 ** or UNIQUE index in a CREATE TABLE statement. Since the table
119495 ** has just been created, it contains no data and the index initialization
119496 ** step can be skipped.
119497 */
119498 else if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) || pTblName!=0 ){
119499 Vdbe *v;
119500 char *zStmt;
119501 int iMem = ++pParse->nMem;
119502
119503 v = sqlite3GetVdbe(pParse);
119504 if( v==0 ) goto exit_create_index;
119505
119506 sqlite3BeginWriteOperation(pParse, 1, iDb);
119507
119508 /* Create the rootpage for the index using CreateIndex. But before
119509 ** doing so, code a Noop instruction and store its address in
119510 ** Index.tnum. This is required in case this index is actually a
119511 ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In
119512 ** that case the convertToWithoutRowidTable() routine will replace
119513 ** the Noop with a Goto to jump over the VDBE code generated below. */
119514 pIndex->tnum = (Pgno)sqlite3VdbeAddOp0(v, OP_Noop184);
119515 sqlite3VdbeAddOp3(v, OP_CreateBtree147, iDb, iMem, BTREE_BLOBKEY2);
119516
119517 /* Gather the complete text of the CREATE INDEX statement into
119518 ** the zStmt variable
119519 */
119520 assert( pName!=0 || pStart==0 )((void) (0));
119521 if( pStart ){
119522 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
119523 if( pName->z[n-1]==';' ) n--;
119524 /* A named index with an explicit CREATE INDEX statement */
119525 zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
119526 onError==OE_None0 ? "" : " UNIQUE", n, pName->z);
119527 }else{
119528 /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
119529 /* zStmt = sqlite3MPrintf(""); */
119530 zStmt = 0;
119531 }
119532
119533 /* Add an entry in sqlite_schema for this index
119534 */
119535 sqlite3NestedParse(pParse,
119536 "INSERT INTO %Q." LEGACY_SCHEMA_TABLE"sqlite_master" " VALUES('index',%Q,%Q,#%d,%Q);",
119537 db->aDb[iDb].zDbSName,
119538 pIndex->zName,
119539 pTab->zName,
119540 iMem,
119541 zStmt
119542 );
119543 sqlite3DbFree(db, zStmt);
119544
119545 /* Fill the index with data and reparse the schema. Code an OP_Expire
119546 ** to invalidate all pre-compiled statements.
119547 */
119548 if( pTblName ){
119549 sqlite3RefillIndex(pParse, pIndex, iMem);
119550 sqlite3ChangeCookie(pParse, iDb);
119551 sqlite3VdbeAddParseSchemaOp(v, iDb,
119552 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), 0);
119553 sqlite3VdbeAddOp2(v, OP_Expire166, 0, 1);
119554 }
119555
119556 sqlite3VdbeJumpHere(v, (int)pIndex->tnum);
119557 }
119558 }
119559 if( db->init.busy || pTblName==0 ){
119560 pIndex->pNext = pTab->pIndex;
119561 pTab->pIndex = pIndex;
119562 pIndex = 0;
119563 }
119564 else if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
119565 assert( pParse->pNewIndex==0 )((void) (0));
119566 pParse->pNewIndex = pIndex;
119567 pIndex = 0;
119568 }
119569
119570 /* Clean up before exiting */
119571exit_create_index:
119572 if( pIndex ) sqlite3FreeIndex(db, pIndex);
119573 if( pTab ){
119574 /* Ensure all REPLACE indexes on pTab are at the end of the pIndex list.
119575 ** The list was already ordered when this routine was entered, so at this
119576 ** point at most a single index (the newly added index) will be out of
119577 ** order. So we have to reorder at most one index. */
119578 Index **ppFrom;
119579 Index *pThis;
119580 for(ppFrom=&pTab->pIndex; (pThis = *ppFrom)!=0; ppFrom=&pThis->pNext){
119581 Index *pNext;
119582 if( pThis->onError!=OE_Replace5 ) continue;
119583 while( (pNext = pThis->pNext)!=0 && pNext->onError!=OE_Replace5 ){
119584 *ppFrom = pNext;
119585 pThis->pNext = pNext->pNext;
119586 pNext->pNext = pThis;
119587 ppFrom = &pNext->pNext;
119588 }
119589 break;
119590 }
119591#ifdef SQLITE_DEBUG
119592 /* Verify that all REPLACE indexes really are now at the end
119593 ** of the index list. In other words, no other index type ever
119594 ** comes after a REPLACE index on the list. */
119595 for(pThis = pTab->pIndex; pThis; pThis=pThis->pNext){
119596 assert( pThis->onError!=OE_Replace((void) (0))
119597 || pThis->pNext==0((void) (0))
119598 || pThis->pNext->onError==OE_Replace )((void) (0));
119599 }
119600#endif
119601 }
119602 sqlite3ExprDelete(db, pPIWhere);
119603 sqlite3ExprListDelete(db, pList);
119604 sqlite3SrcListDelete(db, pTblName);
119605 sqlite3DbFree(db, zName);
119606}
119607
119608/*
119609** Fill the Index.aiRowEst[] array with default information - information
119610** to be used when we have not run the ANALYZE command.
119611**
119612** aiRowEst[0] is supposed to contain the number of elements in the index.
119613** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the
119614** number of rows in the table that match any particular value of the
119615** first column of the index. aiRowEst[2] is an estimate of the number
119616** of rows that match any particular combination of the first 2 columns
119617** of the index. And so forth. It must always be the case that
119618*
119619** aiRowEst[N]<=aiRowEst[N-1]
119620** aiRowEst[N]>=1
119621**
119622** Apart from that, we have little to go on besides intuition as to
119623** how aiRowEst[] should be initialized. The numbers generated here
119624** are based on typical values found in actual indices.
119625*/
119626SQLITE_PRIVATEstatic void sqlite3DefaultRowEst(Index *pIdx){
119627 /* 10, 9, 8, 7, 6 */
119628 static const LogEst aVal[] = { 33, 32, 30, 28, 26 };
119629 LogEst *a = pIdx->aiRowLogEst;
119630 LogEst x;
119631 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol)((((int)(sizeof(aVal)/sizeof(aVal[0]))))<(pIdx->nKeyCol
)?(((int)(sizeof(aVal)/sizeof(aVal[0])))):(pIdx->nKeyCol))
;
119632 int i;
119633
119634 /* Indexes with default row estimates should not have stat1 data */
119635 assert( !pIdx->hasStat1 )((void) (0));
119636
119637 /* Set the first entry (number of rows in the index) to the estimated
119638 ** number of rows in the table, or half the number of rows in the table
119639 ** for a partial index.
119640 **
119641 ** 2020-05-27: If some of the stat data is coming from the sqlite_stat1
119642 ** table but other parts we are having to guess at, then do not let the
119643 ** estimated number of rows in the table be less than 1000 (LogEst 99).
119644 ** Failure to do this can cause the indexes for which we do not have
119645 ** stat1 data to be ignored by the query planner.
119646 */
119647 x = pIdx->pTable->nRowLogEst;
119648 assert( 99==sqlite3LogEst(1000) )((void) (0));
119649 if( x<99 ){
119650 pIdx->pTable->nRowLogEst = x = 99;
119651 }
119652 if( pIdx->pPartIdxWhere!=0 ){ x -= 10; assert( 10==sqlite3LogEst(2) )((void) (0)); }
119653 a[0] = x;
119654
119655 /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is
119656 ** 6 and each subsequent value (if any) is 5. */
119657 memcpy(&a[1], aVal, nCopy*sizeof(LogEst));
119658 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
119659 a[i] = 23; assert( 23==sqlite3LogEst(5) )((void) (0));
119660 }
119661
119662 assert( 0==sqlite3LogEst(1) )((void) (0));
119663 if( IsUniqueIndex(pIdx)((pIdx)->onError!=0) ) a[pIdx->nKeyCol] = 0;
119664}
119665
119666/*
119667** This routine will drop an existing named index. This routine
119668** implements the DROP INDEX statement.
119669*/
119670SQLITE_PRIVATEstatic void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
119671 Index *pIndex;
119672 Vdbe *v;
119673 sqlite3 *db = pParse->db;
119674 int iDb;
119675
119676 if( db->mallocFailed ){
119677 goto exit_drop_index;
119678 }
119679 assert( pParse->nErr==0 )((void) (0)); /* Never called with prior non-OOM errors */
119680 assert( pName->nSrc==1 )((void) (0));
119681 if( SQLITE_OK0!=sqlite3ReadSchema(pParse) ){
119682 goto exit_drop_index;
119683 }
119684 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
119685 if( pIndex==0 ){
119686 if( !ifExists ){
119687 sqlite3ErrorMsg(pParse, "no such index: %S", pName->a);
119688 }else{
119689 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
119690 sqlite3ForceNotReadOnly(pParse);
119691 }
119692 pParse->checkSchema = 1;
119693 goto exit_drop_index;
119694 }
119695 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF0 ){
119696 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
119697 "or PRIMARY KEY constraint cannot be dropped", 0);
119698 goto exit_drop_index;
119699 }
119700 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
119701#ifndef SQLITE_OMIT_AUTHORIZATION
119702 {
119703 int code = SQLITE_DROP_INDEX10;
119704 Table *pTab = pIndex->pTable;
119705 const char *zDb = db->aDb[iDb].zDbSName;
119706 const char *zTab = SCHEMA_TABLE(iDb)((!0)&&(iDb==1)?"sqlite_temp_master":"sqlite_master");
119707 if( sqlite3AuthCheck(pParse, SQLITE_DELETE9, zTab, 0, zDb) ){
119708 goto exit_drop_index;
119709 }
119710 if( !OMIT_TEMPDB0 && iDb==1 ) code = SQLITE_DROP_TEMP_INDEX12;
119711 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
119712 goto exit_drop_index;
119713 }
119714 }
119715#endif
119716
119717 /* Generate code to remove the index and from the schema table */
119718 v = sqlite3GetVdbe(pParse);
119719 if( v ){
119720 sqlite3BeginWriteOperation(pParse, 1, iDb);
119721 sqlite3NestedParse(pParse,
119722 "DELETE FROM %Q." LEGACY_SCHEMA_TABLE"sqlite_master" " WHERE name=%Q AND type='index'",
119723 db->aDb[iDb].zDbSName, pIndex->zName
119724 );
119725 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
119726 sqlite3ChangeCookie(pParse, iDb);
119727 destroyRootPage(pParse, pIndex->tnum, iDb);
119728 sqlite3VdbeAddOp4(v, OP_DropIndex152, iDb, 0, 0, pIndex->zName, 0);
119729 }
119730
119731exit_drop_index:
119732 sqlite3SrcListDelete(db, pName);
119733}
119734
119735/*
119736** pArray is a pointer to an array of objects. Each object in the
119737** array is szEntry bytes in size. This routine uses sqlite3DbRealloc()
119738** to extend the array so that there is space for a new object at the end.
119739**
119740** When this function is called, *pnEntry contains the current size of
119741** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
119742** in total).
119743**
119744** If the realloc() is successful (i.e. if no OOM condition occurs), the
119745** space allocated for the new object is zeroed, *pnEntry updated to
119746** reflect the new size of the array and a pointer to the new allocation
119747** returned. *pIdx is set to the index of the new array entry in this case.
119748**
119749** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
119750** unchanged and a copy of pArray returned.
119751*/
119752SQLITE_PRIVATEstatic void *sqlite3ArrayAllocate(
119753 sqlite3 *db, /* Connection to notify of malloc failures */
119754 void *pArray, /* Array of objects. Might be reallocated */
119755 int szEntry, /* Size of each object in the array */
119756 int *pnEntry, /* Number of objects currently in use */
119757 int *pIdx /* Write the index of a new slot here */
119758){
119759 char *z;
119760 sqlite3_int64 n = *pIdx = *pnEntry;
119761 if( (n & (n-1))==0 ){
119762 sqlite3_int64 sz = (n==0) ? 1 : 2*n;
119763 void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry);
119764 if( pNew==0 ){
119765 *pIdx = -1;
119766 return pArray;
119767 }
119768 pArray = pNew;
119769 }
119770 z = (char*)pArray;
119771 memset(&z[n * szEntry], 0, szEntry);
119772 ++*pnEntry;
119773 return pArray;
119774}
119775
119776/*
119777** Append a new element to the given IdList. Create a new IdList if
119778** need be.
119779**
119780** A new IdList is returned, or NULL if malloc() fails.
119781*/
119782SQLITE_PRIVATEstatic IdList *sqlite3IdListAppend(Parse *pParse, IdList *pList, Token *pToken){
119783 sqlite3 *db = pParse->db;
119784 int i;
119785 if( pList==0 ){
119786 pList = sqlite3DbMallocZero(db, sizeof(IdList) );
119787 if( pList==0 ) return 0;
119788 }else{
119789 IdList *pNew;
119790 pNew = sqlite3DbRealloc(db, pList,
119791 sizeof(IdList) + pList->nId*sizeof(pList->a));
119792 if( pNew==0 ){
119793 sqlite3IdListDelete(db, pList);
119794 return 0;
119795 }
119796 pList = pNew;
119797 }
119798 i = pList->nId++;
119799 pList->a[i].zName = sqlite3NameFromToken(db, pToken);
119800 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && pList->a[i].zName ){
119801 sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken);
119802 }
119803 return pList;
119804}
119805
119806/*
119807** Delete an IdList.
119808*/
119809SQLITE_PRIVATEstatic void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
119810 int i;
119811 if( pList==0 ) return;
119812 assert( pList->eU4!=EU4_EXPR )((void) (0)); /* EU4_EXPR mode is not currently used */
119813 for(i=0; i<pList->nId; i++){
119814 sqlite3DbFree(db, pList->a[i].zName);
119815 }
119816 sqlite3DbFreeNN(db, pList);
119817}
119818
119819/*
119820** Return the index in pList of the identifier named zId. Return -1
119821** if not found.
119822*/
119823SQLITE_PRIVATEstatic int sqlite3IdListIndex(IdList *pList, const char *zName){
119824 int i;
119825 assert( pList!=0 )((void) (0));
119826 for(i=0; i<pList->nId; i++){
119827 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
119828 }
119829 return -1;
119830}
119831
119832/*
119833** Maximum size of a SrcList object.
119834** The SrcList object is used to represent the FROM clause of a
119835** SELECT statement, and the query planner cannot deal with more
119836** than 64 tables in a join. So any value larger than 64 here
119837** is sufficient for most uses. Smaller values, like say 10, are
119838** appropriate for small and memory-limited applications.
119839*/
119840#ifndef SQLITE_MAX_SRCLIST200
119841# define SQLITE_MAX_SRCLIST200 200
119842#endif
119843
119844/*
119845** Expand the space allocated for the given SrcList object by
119846** creating nExtra new slots beginning at iStart. iStart is zero based.
119847** New slots are zeroed.
119848**
119849** For example, suppose a SrcList initially contains two entries: A,B.
119850** To append 3 new entries onto the end, do this:
119851**
119852** sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
119853**
119854** After the call above it would contain: A, B, nil, nil, nil.
119855** If the iStart argument had been 1 instead of 2, then the result
119856** would have been: A, nil, nil, nil, B. To prepend the new slots,
119857** the iStart value would be 0. The result then would
119858** be: nil, nil, nil, A, B.
119859**
119860** If a memory allocation fails or the SrcList becomes too large, leave
119861** the original SrcList unchanged, return NULL, and leave an error message
119862** in pParse.
119863*/
119864SQLITE_PRIVATEstatic SrcList *sqlite3SrcListEnlarge(
119865 Parse *pParse, /* Parsing context into which errors are reported */
119866 SrcList *pSrc, /* The SrcList to be enlarged */
119867 int nExtra, /* Number of new slots to add to pSrc->a[] */
119868 int iStart /* Index in pSrc->a[] of first new slot */
119869){
119870 int i;
119871
119872 /* Sanity checking on calling parameters */
119873 assert( iStart>=0 )((void) (0));
119874 assert( nExtra>=1 )((void) (0));
119875 assert( pSrc!=0 )((void) (0));
119876 assert( iStart<=pSrc->nSrc )((void) (0));
119877
119878 /* Allocate additional space if needed */
119879 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
119880 SrcList *pNew;
119881 sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra;
119882 sqlite3 *db = pParse->db;
119883
119884 if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST200 ){
119885 sqlite3ErrorMsg(pParse, "too many FROM clause terms, max: %d",
119886 SQLITE_MAX_SRCLIST200);
119887 return 0;
119888 }
119889 if( nAlloc>SQLITE_MAX_SRCLIST200 ) nAlloc = SQLITE_MAX_SRCLIST200;
119890 pNew = sqlite3DbRealloc(db, pSrc,
119891 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
119892 if( pNew==0 ){
119893 assert( db->mallocFailed )((void) (0));
119894 return 0;
119895 }
119896 pSrc = pNew;
119897 pSrc->nAlloc = nAlloc;
119898 }
119899
119900 /* Move existing slots that come after the newly inserted slots
119901 ** out of the way */
119902 for(i=pSrc->nSrc-1; i>=iStart; i--){
119903 pSrc->a[i+nExtra] = pSrc->a[i];
119904 }
119905 pSrc->nSrc += nExtra;
119906
119907 /* Zero the newly allocated slots */
119908 memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
119909 for(i=iStart; i<iStart+nExtra; i++){
119910 pSrc->a[i].iCursor = -1;
119911 }
119912
119913 /* Return a pointer to the enlarged SrcList */
119914 return pSrc;
119915}
119916
119917
119918/*
119919** Append a new table name to the given SrcList. Create a new SrcList if
119920** need be. A new entry is created in the SrcList even if pTable is NULL.
119921**
119922** A SrcList is returned, or NULL if there is an OOM error or if the
119923** SrcList grows to large. The returned
119924** SrcList might be the same as the SrcList that was input or it might be
119925** a new one. If an OOM error does occurs, then the prior value of pList
119926** that is input to this routine is automatically freed.
119927**
119928** If pDatabase is not null, it means that the table has an optional
119929** database name prefix. Like this: "database.table". The pDatabase
119930** points to the table name and the pTable points to the database name.
119931** The SrcList.a[].zName field is filled with the table name which might
119932** come from pTable (if pDatabase is NULL) or from pDatabase.
119933** SrcList.a[].zDatabase is filled with the database name from pTable,
119934** or with NULL if no database is specified.
119935**
119936** In other words, if call like this:
119937**
119938** sqlite3SrcListAppend(D,A,B,0);
119939**
119940** Then B is a table name and the database name is unspecified. If called
119941** like this:
119942**
119943** sqlite3SrcListAppend(D,A,B,C);
119944**
119945** Then C is the table name and B is the database name. If C is defined
119946** then so is B. In other words, we never have a case where:
119947**
119948** sqlite3SrcListAppend(D,A,0,C);
119949**
119950** Both pTable and pDatabase are assumed to be quoted. They are dequoted
119951** before being added to the SrcList.
119952*/
119953SQLITE_PRIVATEstatic SrcList *sqlite3SrcListAppend(
119954 Parse *pParse, /* Parsing context, in which errors are reported */
119955 SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */
119956 Token *pTable, /* Table to append */
119957 Token *pDatabase /* Database of the table */
119958){
119959 SrcItem *pItem;
119960 sqlite3 *db;
119961 assert( pDatabase==0 || pTable!=0 )((void) (0)); /* Cannot have C without B */
119962 assert( pParse!=0 )((void) (0));
119963 assert( pParse->db!=0 )((void) (0));
119964 db = pParse->db;
119965 if( pList==0 ){
119966 pList = sqlite3DbMallocRawNN(pParse->db, sizeof(SrcList) );
119967 if( pList==0 ) return 0;
119968 pList->nAlloc = 1;
119969 pList->nSrc = 1;
119970 memset(&pList->a[0], 0, sizeof(pList->a[0]));
119971 pList->a[0].iCursor = -1;
119972 }else{
119973 SrcList *pNew = sqlite3SrcListEnlarge(pParse, pList, 1, pList->nSrc);
119974 if( pNew==0 ){
119975 sqlite3SrcListDelete(db, pList);
119976 return 0;
119977 }else{
119978 pList = pNew;
119979 }
119980 }
119981 pItem = &pList->a[pList->nSrc-1];
119982 if( pDatabase && pDatabase->z==0 ){
119983 pDatabase = 0;
119984 }
119985 if( pDatabase ){
119986 pItem->zName = sqlite3NameFromToken(db, pDatabase);
119987 pItem->zDatabase = sqlite3NameFromToken(db, pTable);
119988 }else{
119989 pItem->zName = sqlite3NameFromToken(db, pTable);
119990 pItem->zDatabase = 0;
119991 }
119992 return pList;
119993}
119994
119995/*
119996** Assign VdbeCursor index numbers to all tables in a SrcList
119997*/
119998SQLITE_PRIVATEstatic void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
119999 int i;
120000 SrcItem *pItem;
120001 assert( pList || pParse->db->mallocFailed )((void) (0));
120002 if( ALWAYS(pList)(pList) ){
120003 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
120004 if( pItem->iCursor>=0 ) continue;
120005 pItem->iCursor = pParse->nTab++;
120006 if( pItem->pSelect ){
120007 sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
120008 }
120009 }
120010 }
120011}
120012
120013/*
120014** Delete an entire SrcList including all its substructure.
120015*/
120016SQLITE_PRIVATEstatic void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
120017 int i;
120018 SrcItem *pItem;
120019 if( pList==0 ) return;
120020 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
120021 if( pItem->zDatabase ) sqlite3DbFreeNN(db, pItem->zDatabase);
120022 sqlite3DbFree(db, pItem->zName);
120023 if( pItem->zAlias ) sqlite3DbFreeNN(db, pItem->zAlias);
120024 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
120025 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
120026 sqlite3DeleteTable(db, pItem->pTab);
120027 if( pItem->pSelect ) sqlite3SelectDelete(db, pItem->pSelect);
120028 if( pItem->fg.isUsing ){
120029 sqlite3IdListDelete(db, pItem->u3.pUsing);
120030 }else if( pItem->u3.pOn ){
120031 sqlite3ExprDelete(db, pItem->u3.pOn);
120032 }
120033 }
120034 sqlite3DbFreeNN(db, pList);
120035}
120036
120037/*
120038** This routine is called by the parser to add a new term to the
120039** end of a growing FROM clause. The "p" parameter is the part of
120040** the FROM clause that has already been constructed. "p" is NULL
120041** if this is the first term of the FROM clause. pTable and pDatabase
120042** are the name of the table and database named in the FROM clause term.
120043** pDatabase is NULL if the database name qualifier is missing - the
120044** usual case. If the term has an alias, then pAlias points to the
120045** alias token. If the term is a subquery, then pSubquery is the
120046** SELECT statement that the subquery encodes. The pTable and
120047** pDatabase parameters are NULL for subqueries. The pOn and pUsing
120048** parameters are the content of the ON and USING clauses.
120049**
120050** Return a new SrcList which encodes is the FROM with the new
120051** term added.
120052*/
120053SQLITE_PRIVATEstatic SrcList *sqlite3SrcListAppendFromTerm(
120054 Parse *pParse, /* Parsing context */
120055 SrcList *p, /* The left part of the FROM clause already seen */
120056 Token *pTable, /* Name of the table to add to the FROM clause */
120057 Token *pDatabase, /* Name of the database containing pTable */
120058 Token *pAlias, /* The right-hand side of the AS subexpression */
120059 Select *pSubquery, /* A subquery used in place of a table name */
120060 OnOrUsing *pOnUsing /* Either the ON clause or the USING clause */
120061){
120062 SrcItem *pItem;
120063 sqlite3 *db = pParse->db;
120064 if( !p && pOnUsing!=0 && (pOnUsing->pOn || pOnUsing->pUsing) ){
120065 sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s",
120066 (pOnUsing->pOn ? "ON" : "USING")
120067 );
120068 goto append_from_error;
120069 }
120070 p = sqlite3SrcListAppend(pParse, p, pTable, pDatabase);
120071 if( p==0 ){
120072 goto append_from_error;
120073 }
120074 assert( p->nSrc>0 )((void) (0));
120075 pItem = &p->a[p->nSrc-1];
120076 assert( (pTable==0)==(pDatabase==0) )((void) (0));
120077 assert( pItem->zName==0 || pDatabase!=0 )((void) (0));
120078 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && pItem->zName ){
120079 Token *pToken = (ALWAYS(pDatabase)(pDatabase) && pDatabase->z) ? pDatabase : pTable;
120080 sqlite3RenameTokenMap(pParse, pItem->zName, pToken);
120081 }
120082 assert( pAlias!=0 )((void) (0));
120083 if( pAlias->n ){
120084 pItem->zAlias = sqlite3NameFromToken(db, pAlias);
120085 }
120086 if( pSubquery ){
120087 pItem->pSelect = pSubquery;
120088 if( pSubquery->selFlags & SF_NestedFrom0x0000800 ){
120089 pItem->fg.isNestedFrom = 1;
120090 }
120091 }
120092 assert( pOnUsing==0 || pOnUsing->pOn==0 || pOnUsing->pUsing==0 )((void) (0));
120093 assert( pItem->fg.isUsing==0 )((void) (0));
120094 if( pOnUsing==0 ){
120095 pItem->u3.pOn = 0;
120096 }else if( pOnUsing->pUsing ){
120097 pItem->fg.isUsing = 1;
120098 pItem->u3.pUsing = pOnUsing->pUsing;
120099 }else{
120100 pItem->u3.pOn = pOnUsing->pOn;
120101 }
120102 return p;
120103
120104append_from_error:
120105 assert( p==0 )((void) (0));
120106 sqlite3ClearOnOrUsing(db, pOnUsing);
120107 sqlite3SelectDelete(db, pSubquery);
120108 return 0;
120109}
120110
120111/*
120112** Add an INDEXED BY or NOT INDEXED clause to the most recently added
120113** element of the source-list passed as the second argument.
120114*/
120115SQLITE_PRIVATEstatic void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
120116 assert( pIndexedBy!=0 )((void) (0));
120117 if( p && pIndexedBy->n>0 ){
120118 SrcItem *pItem;
120119 assert( p->nSrc>0 )((void) (0));
120120 pItem = &p->a[p->nSrc-1];
120121 assert( pItem->fg.notIndexed==0 )((void) (0));
120122 assert( pItem->fg.isIndexedBy==0 )((void) (0));
120123 assert( pItem->fg.isTabFunc==0 )((void) (0));
120124 if( pIndexedBy->n==1 && !pIndexedBy->z ){
120125 /* A "NOT INDEXED" clause was supplied. See parse.y
120126 ** construct "indexed_opt" for details. */
120127 pItem->fg.notIndexed = 1;
120128 }else{
120129 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
120130 pItem->fg.isIndexedBy = 1;
120131 assert( pItem->fg.isCte==0 )((void) (0)); /* No collision on union u2 */
120132 }
120133 }
120134}
120135
120136/*
120137** Append the contents of SrcList p2 to SrcList p1 and return the resulting
120138** SrcList. Or, if an error occurs, return NULL. In all cases, p1 and p2
120139** are deleted by this function.
120140*/
120141SQLITE_PRIVATEstatic SrcList *sqlite3SrcListAppendList(Parse *pParse, SrcList *p1, SrcList *p2){
120142 assert( p1 && p1->nSrc==1 )((void) (0));
120143 if( p2 ){
120144 SrcList *pNew = sqlite3SrcListEnlarge(pParse, p1, p2->nSrc, 1);
120145 if( pNew==0 ){
120146 sqlite3SrcListDelete(pParse->db, p2);
120147 }else{
120148 p1 = pNew;
120149 memcpy(&p1->a[1], p2->a, p2->nSrc*sizeof(SrcItem));
120150 sqlite3DbFree(pParse->db, p2);
120151 p1->a[0].fg.jointype |= (JT_LTORJ0x40 & p1->a[1].fg.jointype);
120152 }
120153 }
120154 return p1;
120155}
120156
120157/*
120158** Add the list of function arguments to the SrcList entry for a
120159** table-valued-function.
120160*/
120161SQLITE_PRIVATEstatic void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){
120162 if( p ){
120163 SrcItem *pItem = &p->a[p->nSrc-1];
120164 assert( pItem->fg.notIndexed==0 )((void) (0));
120165 assert( pItem->fg.isIndexedBy==0 )((void) (0));
120166 assert( pItem->fg.isTabFunc==0 )((void) (0));
120167 pItem->u1.pFuncArg = pList;
120168 pItem->fg.isTabFunc = 1;
120169 }else{
120170 sqlite3ExprListDelete(pParse->db, pList);
120171 }
120172}
120173
120174/*
120175** When building up a FROM clause in the parser, the join operator
120176** is initially attached to the left operand. But the code generator
120177** expects the join operator to be on the right operand. This routine
120178** Shifts all join operators from left to right for an entire FROM
120179** clause.
120180**
120181** Example: Suppose the join is like this:
120182**
120183** A natural cross join B
120184**
120185** The operator is "natural cross join". The A and B operands are stored
120186** in p->a[0] and p->a[1], respectively. The parser initially stores the
120187** operator with A. This routine shifts that operator over to B.
120188**
120189** Additional changes:
120190**
120191** * All tables to the left of the right-most RIGHT JOIN are tagged with
120192** JT_LTORJ (mnemonic: Left Table Of Right Join) so that the
120193** code generator can easily tell that the table is part of
120194** the left operand of at least one RIGHT JOIN.
120195*/
120196SQLITE_PRIVATEstatic void sqlite3SrcListShiftJoinType(Parse *pParse, SrcList *p){
120197 (void)pParse;
120198 if( p && p->nSrc>1 ){
120199 int i = p->nSrc-1;
120200 u8 allFlags = 0;
120201 do{
120202 allFlags |= p->a[i].fg.jointype = p->a[i-1].fg.jointype;
120203 }while( (--i)>0 );
120204 p->a[0].fg.jointype = 0;
120205
120206 /* All terms to the left of a RIGHT JOIN should be tagged with the
120207 ** JT_LTORJ flags */
120208 if( allFlags & JT_RIGHT0x10 ){
120209 for(i=p->nSrc-1; ALWAYS(i>0)(i>0) && (p->a[i].fg.jointype&JT_RIGHT0x10)==0; i--){}
120210 i--;
120211 assert( i>=0 )((void) (0));
120212 do{
120213 p->a[i].fg.jointype |= JT_LTORJ0x40;
120214 }while( (--i)>=0 );
120215 }
120216 }
120217}
120218
120219/*
120220** Generate VDBE code for a BEGIN statement.
120221*/
120222SQLITE_PRIVATEstatic void sqlite3BeginTransaction(Parse *pParse, int type){
120223 sqlite3 *db;
120224 Vdbe *v;
120225 int i;
120226
120227 assert( pParse!=0 )((void) (0));
120228 db = pParse->db;
120229 assert( db!=0 )((void) (0));
120230 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION22, "BEGIN", 0, 0) ){
120231 return;
120232 }
120233 v = sqlite3GetVdbe(pParse);
120234 if( !v ) return;
120235 if( type!=TK_DEFERRED7 ){
120236 for(i=0; i<db->nDb; i++){
120237 int eTxnType;
120238 Btree *pBt = db->aDb[i].pBt;
120239 if( pBt && sqlite3BtreeIsReadonly(pBt) ){
120240 eTxnType = 0; /* Read txn */
120241 }else if( type==TK_EXCLUSIVE9 ){
120242 eTxnType = 2; /* Exclusive txn */
120243 }else{
120244 eTxnType = 1; /* Write txn */
120245 }
120246 sqlite3VdbeAddOp2(v, OP_Transaction2, i, eTxnType);
120247 sqlite3VdbeUsesBtree(v, i);
120248 }
120249 }
120250 sqlite3VdbeAddOp0(v, OP_AutoCommit1);
120251}
120252
120253/*
120254** Generate VDBE code for a COMMIT or ROLLBACK statement.
120255** Code for ROLLBACK is generated if eType==TK_ROLLBACK. Otherwise
120256** code is generated for a COMMIT.
120257*/
120258SQLITE_PRIVATEstatic void sqlite3EndTransaction(Parse *pParse, int eType){
120259 Vdbe *v;
120260 int isRollback;
120261
120262 assert( pParse!=0 )((void) (0));
120263 assert( pParse->db!=0 )((void) (0));
120264 assert( eType==TK_COMMIT || eType==TK_END || eType==TK_ROLLBACK )((void) (0));
120265 isRollback = eType==TK_ROLLBACK12;
120266 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION22,
120267 isRollback ? "ROLLBACK" : "COMMIT", 0, 0) ){
120268 return;
120269 }
120270 v = sqlite3GetVdbe(pParse);
120271 if( v ){
120272 sqlite3VdbeAddOp2(v, OP_AutoCommit1, 1, isRollback);
120273 }
120274}
120275
120276/*
120277** This function is called by the parser when it parses a command to create,
120278** release or rollback an SQL savepoint.
120279*/
120280SQLITE_PRIVATEstatic void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
120281 char *zName = sqlite3NameFromToken(pParse->db, pName);
120282 if( zName ){
120283 Vdbe *v = sqlite3GetVdbe(pParse);
120284#ifndef SQLITE_OMIT_AUTHORIZATION
120285 static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
120286 assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 )((void) (0));
120287#endif
120288 if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT32, az[op], zName, 0) ){
120289 sqlite3DbFree(pParse->db, zName);
120290 return;
120291 }
120292 sqlite3VdbeAddOp4(v, OP_Savepoint0, op, 0, 0, zName, P4_DYNAMIC(-6));
120293 }
120294}
120295
120296/*
120297** Make sure the TEMP database is open and available for use. Return
120298** the number of errors. Leave any error messages in the pParse structure.
120299*/
120300SQLITE_PRIVATEstatic int sqlite3OpenTempDatabase(Parse *pParse){
120301 sqlite3 *db = pParse->db;
120302 if( db->aDb[1].pBt==0 && !pParse->explain ){
120303 int rc;
120304 Btree *pBt;
120305 static const int flags =
120306 SQLITE_OPEN_READWRITE0x00000002 |
120307 SQLITE_OPEN_CREATE0x00000004 |
120308 SQLITE_OPEN_EXCLUSIVE0x00000010 |
120309 SQLITE_OPEN_DELETEONCLOSE0x00000008 |
120310 SQLITE_OPEN_TEMP_DB0x00000200;
120311
120312 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
120313 if( rc!=SQLITE_OK0 ){
120314 sqlite3ErrorMsg(pParse, "unable to open a temporary database "
120315 "file for storing temporary tables");
120316 pParse->rc = rc;
120317 return 1;
120318 }
120319 db->aDb[1].pBt = pBt;
120320 assert( db->aDb[1].pSchema )((void) (0));
120321 if( SQLITE_NOMEM7==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, 0, 0) ){
120322 sqlite3OomFault(db);
120323 return 1;
120324 }
120325 }
120326 return 0;
120327}
120328
120329/*
120330** Record the fact that the schema cookie will need to be verified
120331** for database iDb. The code to actually verify the schema cookie
120332** will occur at the end of the top-level VDBE and will be generated
120333** later, by sqlite3FinishCoding().
120334*/
120335static void sqlite3CodeVerifySchemaAtToplevel(Parse *pToplevel, int iDb){
120336 assert( iDb>=0 && iDb<pToplevel->db->nDb )((void) (0));
120337 assert( pToplevel->db->aDb[iDb].pBt!=0 || iDb==1 )((void) (0));
120338 assert( iDb<SQLITE_MAX_DB )((void) (0));
120339 assert( sqlite3SchemaMutexHeld(pToplevel->db, iDb, 0) )((void) (0));
120340 if( DbMaskTest(pToplevel->cookieMask, iDb)(((pToplevel->cookieMask)&(((yDbMask)1)<<(iDb)))
!=0)
==0 ){
120341 DbMaskSet(pToplevel->cookieMask, iDb)(pToplevel->cookieMask)|=(((yDbMask)1)<<(iDb));
120342 if( !OMIT_TEMPDB0 && iDb==1 ){
120343 sqlite3OpenTempDatabase(pToplevel);
120344 }
120345 }
120346}
120347SQLITE_PRIVATEstatic void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
120348 sqlite3CodeVerifySchemaAtToplevel(sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse)), iDb);
120349}
120350
120351
120352/*
120353** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each
120354** attached database. Otherwise, invoke it for the database named zDb only.
120355*/
120356SQLITE_PRIVATEstatic void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
120357 sqlite3 *db = pParse->db;
120358 int i;
120359 for(i=0; i<db->nDb; i++){
120360 Db *pDb = &db->aDb[i];
120361 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
120362 sqlite3CodeVerifySchema(pParse, i);
120363 }
120364 }
120365}
120366
120367/*
120368** Generate VDBE code that prepares for doing an operation that
120369** might change the database.
120370**
120371** This routine starts a new transaction if we are not already within
120372** a transaction. If we are already within a transaction, then a checkpoint
120373** is set if the setStatement parameter is true. A checkpoint should
120374** be set for operations that might fail (due to a constraint) part of
120375** the way through and which will need to undo some writes without having to
120376** rollback the whole transaction. For operations where all constraints
120377** can be checked before any changes are made to the database, it is never
120378** necessary to undo a write and the checkpoint should not be set.
120379*/
120380SQLITE_PRIVATEstatic void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
120381 Parse *pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
120382 sqlite3CodeVerifySchemaAtToplevel(pToplevel, iDb);
120383 DbMaskSet(pToplevel->writeMask, iDb)(pToplevel->writeMask)|=(((yDbMask)1)<<(iDb));
120384 pToplevel->isMultiWrite |= setStatement;
120385}
120386
120387/*
120388** Indicate that the statement currently under construction might write
120389** more than one entry (example: deleting one row then inserting another,
120390** inserting multiple rows in a table, or inserting a row and index entries.)
120391** If an abort occurs after some of these writes have completed, then it will
120392** be necessary to undo the completed writes.
120393*/
120394SQLITE_PRIVATEstatic void sqlite3MultiWrite(Parse *pParse){
120395 Parse *pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
120396 pToplevel->isMultiWrite = 1;
120397}
120398
120399/*
120400** The code generator calls this routine if is discovers that it is
120401** possible to abort a statement prior to completion. In order to
120402** perform this abort without corrupting the database, we need to make
120403** sure that the statement is protected by a statement transaction.
120404**
120405** Technically, we only need to set the mayAbort flag if the
120406** isMultiWrite flag was previously set. There is a time dependency
120407** such that the abort must occur after the multiwrite. This makes
120408** some statements involving the REPLACE conflict resolution algorithm
120409** go a little faster. But taking advantage of this time dependency
120410** makes it more difficult to prove that the code is correct (in
120411** particular, it prevents us from writing an effective
120412** implementation of sqlite3AssertMayAbort()) and so we have chosen
120413** to take the safe route and skip the optimization.
120414*/
120415SQLITE_PRIVATEstatic void sqlite3MayAbort(Parse *pParse){
120416 Parse *pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
120417 pToplevel->mayAbort = 1;
120418}
120419
120420/*
120421** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
120422** error. The onError parameter determines which (if any) of the statement
120423** and/or current transaction is rolled back.
120424*/
120425SQLITE_PRIVATEstatic void sqlite3HaltConstraint(
120426 Parse *pParse, /* Parsing context */
120427 int errCode, /* extended error code */
120428 int onError, /* Constraint type */
120429 char *p4, /* Error message */
120430 i8 p4type, /* P4_STATIC or P4_TRANSIENT */
120431 u8 p5Errmsg /* P5_ErrMsg type */
120432){
120433 Vdbe *v;
120434 assert( pParse->pVdbe!=0 )((void) (0));
120435 v = sqlite3GetVdbe(pParse);
120436 assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested )((void) (0));
120437 if( onError==OE_Abort2 ){
120438 sqlite3MayAbort(pParse);
120439 }
120440 sqlite3VdbeAddOp4(v, OP_Halt70, errCode, onError, 0, p4, p4type);
120441 sqlite3VdbeChangeP5(v, p5Errmsg);
120442}
120443
120444/*
120445** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation.
120446*/
120447SQLITE_PRIVATEstatic void sqlite3UniqueConstraint(
120448 Parse *pParse, /* Parsing context */
120449 int onError, /* Constraint type */
120450 Index *pIdx /* The index that triggers the constraint */
120451){
120452 char *zErr;
120453 int j;
120454 StrAccum errMsg;
120455 Table *pTab = pIdx->pTable;
120456
120457 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0,
120458 pParse->db->aLimit[SQLITE_LIMIT_LENGTH0]);
120459 if( pIdx->aColExpr ){
120460 sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
120461 }else{
120462 for(j=0; j<pIdx->nKeyCol; j++){
120463 char *zCol;
120464 assert( pIdx->aiColumn[j]>=0 )((void) (0));
120465 zCol = pTab->aCol[pIdx->aiColumn[j]].zCnName;
120466 if( j ) sqlite3_str_append(&errMsg, ", ", 2);
120467 sqlite3_str_appendall(&errMsg, pTab->zName);
120468 sqlite3_str_append(&errMsg, ".", 1);
120469 sqlite3_str_appendall(&errMsg, zCol);
120470 }
120471 }
120472 zErr = sqlite3StrAccumFinish(&errMsg);
120473 sqlite3HaltConstraint(pParse,
120474 IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ? SQLITE_CONSTRAINT_PRIMARYKEY(19 | (6<<8))
120475 : SQLITE_CONSTRAINT_UNIQUE(19 | (8<<8)),
120476 onError, zErr, P4_DYNAMIC(-6), P5_ConstraintUnique2);
120477}
120478
120479
120480/*
120481** Code an OP_Halt due to non-unique rowid.
120482*/
120483SQLITE_PRIVATEstatic void sqlite3RowidConstraint(
120484 Parse *pParse, /* Parsing context */
120485 int onError, /* Conflict resolution algorithm */
120486 Table *pTab /* The table with the non-unique rowid */
120487){
120488 char *zMsg;
120489 int rc;
120490 if( pTab->iPKey>=0 ){
120491 zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
120492 pTab->aCol[pTab->iPKey].zCnName);
120493 rc = SQLITE_CONSTRAINT_PRIMARYKEY(19 | (6<<8));
120494 }else{
120495 zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
120496 rc = SQLITE_CONSTRAINT_ROWID(19 |(10<<8));
120497 }
120498 sqlite3HaltConstraint(pParse, rc, onError, zMsg, P4_DYNAMIC(-6),
120499 P5_ConstraintUnique2);
120500}
120501
120502/*
120503** Check to see if pIndex uses the collating sequence pColl. Return
120504** true if it does and false if it does not.
120505*/
120506#ifndef SQLITE_OMIT_REINDEX
120507static int collationMatch(const char *zColl, Index *pIndex){
120508 int i;
120509 assert( zColl!=0 )((void) (0));
120510 for(i=0; i<pIndex->nColumn; i++){
120511 const char *z = pIndex->azColl[i];
120512 assert( z!=0 || pIndex->aiColumn[i]<0 )((void) (0));
120513 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
120514 return 1;
120515 }
120516 }
120517 return 0;
120518}
120519#endif
120520
120521/*
120522** Recompute all indices of pTab that use the collating sequence pColl.
120523** If pColl==0 then recompute all indices of pTab.
120524*/
120525#ifndef SQLITE_OMIT_REINDEX
120526static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
120527 if( !IsVirtual(pTab)((pTab)->eTabType==1) ){
120528 Index *pIndex; /* An index associated with pTab */
120529
120530 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
120531 if( zColl==0 || collationMatch(zColl, pIndex) ){
120532 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
120533 sqlite3BeginWriteOperation(pParse, 0, iDb);
120534 sqlite3RefillIndex(pParse, pIndex, -1);
120535 }
120536 }
120537 }
120538}
120539#endif
120540
120541/*
120542** Recompute all indices of all tables in all databases where the
120543** indices use the collating sequence pColl. If pColl==0 then recompute
120544** all indices everywhere.
120545*/
120546#ifndef SQLITE_OMIT_REINDEX
120547static void reindexDatabases(Parse *pParse, char const *zColl){
120548 Db *pDb; /* A single database */
120549 int iDb; /* The database index number */
120550 sqlite3 *db = pParse->db; /* The database connection */
120551 HashElem *k; /* For looping over tables in pDb */
120552 Table *pTab; /* A table in the database */
120553
120554 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0)); /* Needed for schema access */
120555 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
120556 assert( pDb!=0 )((void) (0));
120557 for(k=sqliteHashFirst(&pDb->pSchema->tblHash)((&pDb->pSchema->tblHash)->first); k; k=sqliteHashNext(k)((k)->next)){
120558 pTab = (Table*)sqliteHashData(k)((k)->data);
120559 reindexTable(pParse, pTab, zColl);
120560 }
120561 }
120562}
120563#endif
120564
120565/*
120566** Generate code for the REINDEX command.
120567**
120568** REINDEX -- 1
120569** REINDEX <collation> -- 2
120570** REINDEX ?<database>.?<tablename> -- 3
120571** REINDEX ?<database>.?<indexname> -- 4
120572**
120573** Form 1 causes all indices in all attached databases to be rebuilt.
120574** Form 2 rebuilds all indices in all databases that use the named
120575** collating function. Forms 3 and 4 rebuild the named index or all
120576** indices associated with the named table.
120577*/
120578#ifndef SQLITE_OMIT_REINDEX
120579SQLITE_PRIVATEstatic void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
120580 CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */
120581 char *z; /* Name of a table or index */
120582 const char *zDb; /* Name of the database */
120583 Table *pTab; /* A table in the database */
120584 Index *pIndex; /* An index associated with pTab */
120585 int iDb; /* The database index number */
120586 sqlite3 *db = pParse->db; /* The database connection */
120587 Token *pObjName; /* Name of the table or index to be reindexed */
120588
120589 /* Read the database schema. If an error occurs, leave an error message
120590 ** and code in pParse and return NULL. */
120591 if( SQLITE_OK0!=sqlite3ReadSchema(pParse) ){
120592 return;
120593 }
120594
120595 if( pName1==0 ){
120596 reindexDatabases(pParse, 0);
120597 return;
120598 }else if( NEVER(pName2==0)(pName2==0) || pName2->z==0 ){
120599 char *zColl;
120600 assert( pName1->z )((void) (0));
120601 zColl = sqlite3NameFromToken(pParse->db, pName1);
120602 if( !zColl ) return;
120603 pColl = sqlite3FindCollSeq(db, ENC(db)((db)->enc), zColl, 0);
120604 if( pColl ){
120605 reindexDatabases(pParse, zColl);
120606 sqlite3DbFree(db, zColl);
120607 return;
120608 }
120609 sqlite3DbFree(db, zColl);
120610 }
120611 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
120612 if( iDb<0 ) return;
120613 z = sqlite3NameFromToken(db, pObjName);
120614 if( z==0 ) return;
120615 zDb = db->aDb[iDb].zDbSName;
120616 pTab = sqlite3FindTable(db, z, zDb);
120617 if( pTab ){
120618 reindexTable(pParse, pTab, 0);
120619 sqlite3DbFree(db, z);
120620 return;
120621 }
120622 pIndex = sqlite3FindIndex(db, z, zDb);
120623 sqlite3DbFree(db, z);
120624 if( pIndex ){
120625 sqlite3BeginWriteOperation(pParse, 0, iDb);
120626 sqlite3RefillIndex(pParse, pIndex, -1);
120627 return;
120628 }
120629 sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
120630}
120631#endif
120632
120633/*
120634** Return a KeyInfo structure that is appropriate for the given Index.
120635**
120636** The caller should invoke sqlite3KeyInfoUnref() on the returned object
120637** when it has finished using it.
120638*/
120639SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){
120640 int i;
120641 int nCol = pIdx->nColumn;
120642 int nKey = pIdx->nKeyCol;
120643 KeyInfo *pKey;
120644 if( pParse->nErr ) return 0;
120645 if( pIdx->uniqNotNull ){
120646 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
120647 }else{
120648 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
120649 }
120650 if( pKey ){
120651 assert( sqlite3KeyInfoIsWriteable(pKey) )((void) (0));
120652 for(i=0; i<nCol; i++){
120653 const char *zColl = pIdx->azColl[i];
120654 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
120655 sqlite3LocateCollSeq(pParse, zColl);
120656 pKey->aSortFlags[i] = pIdx->aSortOrder[i];
120657 assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) )((void) (0));
120658 }
120659 if( pParse->nErr ){
120660 assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ )((void) (0));
120661 if( pIdx->bNoQuery==0 ){
120662 /* Deactivate the index because it contains an unknown collating
120663 ** sequence. The only way to reactive the index is to reload the
120664 ** schema. Adding the missing collating sequence later does not
120665 ** reactive the index. The application had the chance to register
120666 ** the missing index using the collation-needed callback. For
120667 ** simplicity, SQLite will not give the application a second chance.
120668 */
120669 pIdx->bNoQuery = 1;
120670 pParse->rc = SQLITE_ERROR_RETRY(1 | (2<<8));
120671 }
120672 sqlite3KeyInfoUnref(pKey);
120673 pKey = 0;
120674 }
120675 }
120676 return pKey;
120677}
120678
120679#ifndef SQLITE_OMIT_CTE
120680/*
120681** Create a new CTE object
120682*/
120683SQLITE_PRIVATEstatic Cte *sqlite3CteNew(
120684 Parse *pParse, /* Parsing context */
120685 Token *pName, /* Name of the common-table */
120686 ExprList *pArglist, /* Optional column name list for the table */
120687 Select *pQuery, /* Query used to initialize the table */
120688 u8 eM10d /* The MATERIALIZED flag */
120689){
120690 Cte *pNew;
120691 sqlite3 *db = pParse->db;
120692
120693 pNew = sqlite3DbMallocZero(db, sizeof(*pNew));
120694 assert( pNew!=0 || db->mallocFailed )((void) (0));
120695
120696 if( db->mallocFailed ){
120697 sqlite3ExprListDelete(db, pArglist);
120698 sqlite3SelectDelete(db, pQuery);
120699 }else{
120700 pNew->pSelect = pQuery;
120701 pNew->pCols = pArglist;
120702 pNew->zName = sqlite3NameFromToken(pParse->db, pName);
120703 pNew->eM10d = eM10d;
120704 }
120705 return pNew;
120706}
120707
120708/*
120709** Clear information from a Cte object, but do not deallocate storage
120710** for the object itself.
120711*/
120712static void cteClear(sqlite3 *db, Cte *pCte){
120713 assert( pCte!=0 )((void) (0));
120714 sqlite3ExprListDelete(db, pCte->pCols);
120715 sqlite3SelectDelete(db, pCte->pSelect);
120716 sqlite3DbFree(db, pCte->zName);
120717}
120718
120719/*
120720** Free the contents of the CTE object passed as the second argument.
120721*/
120722SQLITE_PRIVATEstatic void sqlite3CteDelete(sqlite3 *db, Cte *pCte){
120723 assert( pCte!=0 )((void) (0));
120724 cteClear(db, pCte);
120725 sqlite3DbFree(db, pCte);
120726}
120727
120728/*
120729** This routine is invoked once per CTE by the parser while parsing a
120730** WITH clause. The CTE described by teh third argument is added to
120731** the WITH clause of the second argument. If the second argument is
120732** NULL, then a new WITH argument is created.
120733*/
120734SQLITE_PRIVATEstatic With *sqlite3WithAdd(
120735 Parse *pParse, /* Parsing context */
120736 With *pWith, /* Existing WITH clause, or NULL */
120737 Cte *pCte /* CTE to add to the WITH clause */
120738){
120739 sqlite3 *db = pParse->db;
120740 With *pNew;
120741 char *zName;
120742
120743 if( pCte==0 ){
120744 return pWith;
120745 }
120746
120747 /* Check that the CTE name is unique within this WITH clause. If
120748 ** not, store an error in the Parse structure. */
120749 zName = pCte->zName;
120750 if( zName && pWith ){
120751 int i;
120752 for(i=0; i<pWith->nCte; i++){
120753 if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
120754 sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName);
120755 }
120756 }
120757 }
120758
120759 if( pWith ){
120760 sqlite3_int64 nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
120761 pNew = sqlite3DbRealloc(db, pWith, nByte);
120762 }else{
120763 pNew = sqlite3DbMallocZero(db, sizeof(*pWith));
120764 }
120765 assert( (pNew!=0 && zName!=0) || db->mallocFailed )((void) (0));
120766
120767 if( db->mallocFailed ){
120768 sqlite3CteDelete(db, pCte);
120769 pNew = pWith;
120770 }else{
120771 pNew->a[pNew->nCte++] = *pCte;
120772 sqlite3DbFree(db, pCte);
120773 }
120774
120775 return pNew;
120776}
120777
120778/*
120779** Free the contents of the With object passed as the second argument.
120780*/
120781SQLITE_PRIVATEstatic void sqlite3WithDelete(sqlite3 *db, With *pWith){
120782 if( pWith ){
120783 int i;
120784 for(i=0; i<pWith->nCte; i++){
120785 cteClear(db, &pWith->a[i]);
120786 }
120787 sqlite3DbFree(db, pWith);
120788 }
120789}
120790#endif /* !defined(SQLITE_OMIT_CTE) */
120791
120792/************** End of build.c ***********************************************/
120793/************** Begin file callback.c ****************************************/
120794/*
120795** 2005 May 23
120796**
120797** The author disclaims copyright to this source code. In place of
120798** a legal notice, here is a blessing:
120799**
120800** May you do good and not evil.
120801** May you find forgiveness for yourself and forgive others.
120802** May you share freely, never taking more than you give.
120803**
120804*************************************************************************
120805**
120806** This file contains functions used to access the internal hash tables
120807** of user defined functions and collation sequences.
120808*/
120809
120810/* #include "sqliteInt.h" */
120811
120812/*
120813** Invoke the 'collation needed' callback to request a collation sequence
120814** in the encoding enc of name zName, length nName.
120815*/
120816static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
120817 assert( !db->xCollNeeded || !db->xCollNeeded16 )((void) (0));
120818 if( db->xCollNeeded ){
120819 char *zExternal = sqlite3DbStrDup(db, zName);
120820 if( !zExternal ) return;
120821 db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
120822 sqlite3DbFree(db, zExternal);
120823 }
120824#ifndef SQLITE_OMIT_UTF16
120825 if( db->xCollNeeded16 ){
120826 char const *zExternal;
120827 sqlite3_value *pTmp = sqlite3ValueNew(db);
120828 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF81, SQLITE_STATIC((sqlite3_destructor_type)0));
120829 zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE2);
120830 if( zExternal ){
120831 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db)((db)->enc), zExternal);
120832 }
120833 sqlite3ValueFree(pTmp);
120834 }
120835#endif
120836}
120837
120838/*
120839** This routine is called if the collation factory fails to deliver a
120840** collation function in the best encoding but there may be other versions
120841** of this collation function (for other text encodings) available. Use one
120842** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
120843** possible.
120844*/
120845static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
120846 CollSeq *pColl2;
120847 char *z = pColl->zName;
120848 int i;
120849 static const u8 aEnc[] = { SQLITE_UTF16BE3, SQLITE_UTF16LE2, SQLITE_UTF81 };
120850 for(i=0; i<3; i++){
120851 pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
120852 if( pColl2->xCmp!=0 ){
120853 memcpy(pColl, pColl2, sizeof(CollSeq));
120854 pColl->xDel = 0; /* Do not copy the destructor */
120855 return SQLITE_OK0;
120856 }
120857 }
120858 return SQLITE_ERROR1;
120859}
120860
120861/*
120862** This routine is called on a collation sequence before it is used to
120863** check that it is defined. An undefined collation sequence exists when
120864** a database is loaded that contains references to collation sequences
120865** that have not been defined by sqlite3_create_collation() etc.
120866**
120867** If required, this routine calls the 'collation needed' callback to
120868** request a definition of the collating sequence. If this doesn't work,
120869** an equivalent collating sequence that uses a text encoding different
120870** from the main database is substituted, if one is available.
120871*/
120872SQLITE_PRIVATEstatic int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
120873 if( pColl && pColl->xCmp==0 ){
120874 const char *zName = pColl->zName;
120875 sqlite3 *db = pParse->db;
120876 CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db)((db)->enc), pColl, zName);
120877 if( !p ){
120878 return SQLITE_ERROR1;
120879 }
120880 assert( p==pColl )((void) (0));
120881 }
120882 return SQLITE_OK0;
120883}
120884
120885
120886
120887/*
120888** Locate and return an entry from the db.aCollSeq hash table. If the entry
120889** specified by zName and nName is not found and parameter 'create' is
120890** true, then create a new entry. Otherwise return NULL.
120891**
120892** Each pointer stored in the sqlite3.aCollSeq hash table contains an
120893** array of three CollSeq structures. The first is the collation sequence
120894** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
120895**
120896** Stored immediately after the three collation sequences is a copy of
120897** the collation sequence name. A pointer to this string is stored in
120898** each collation sequence structure.
120899*/
120900static CollSeq *findCollSeqEntry(
120901 sqlite3 *db, /* Database connection */
120902 const char *zName, /* Name of the collating sequence */
120903 int create /* Create a new entry if true */
120904){
120905 CollSeq *pColl;
120906 pColl = sqlite3HashFind(&db->aCollSeq, zName);
120907
120908 if( 0==pColl && create ){
120909 int nName = sqlite3Strlen30(zName) + 1;
120910 pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName);
120911 if( pColl ){
120912 CollSeq *pDel = 0;
120913 pColl[0].zName = (char*)&pColl[3];
120914 pColl[0].enc = SQLITE_UTF81;
120915 pColl[1].zName = (char*)&pColl[3];
120916 pColl[1].enc = SQLITE_UTF16LE2;
120917 pColl[2].zName = (char*)&pColl[3];
120918 pColl[2].enc = SQLITE_UTF16BE3;
120919 memcpy(pColl[0].zName, zName, nName);
120920 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
120921
120922 /* If a malloc() failure occurred in sqlite3HashInsert(), it will
120923 ** return the pColl pointer to be deleted (because it wasn't added
120924 ** to the hash table).
120925 */
120926 assert( pDel==0 || pDel==pColl )((void) (0));
120927 if( pDel!=0 ){
120928 sqlite3OomFault(db);
120929 sqlite3DbFree(db, pDel);
120930 pColl = 0;
120931 }
120932 }
120933 }
120934 return pColl;
120935}
120936
120937/*
120938** Parameter zName points to a UTF-8 encoded string nName bytes long.
120939** Return the CollSeq* pointer for the collation sequence named zName
120940** for the encoding 'enc' from the database 'db'.
120941**
120942** If the entry specified is not found and 'create' is true, then create a
120943** new entry. Otherwise return NULL.
120944**
120945** A separate function sqlite3LocateCollSeq() is a wrapper around
120946** this routine. sqlite3LocateCollSeq() invokes the collation factory
120947** if necessary and generates an error message if the collating sequence
120948** cannot be found.
120949**
120950** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
120951*/
120952SQLITE_PRIVATEstatic CollSeq *sqlite3FindCollSeq(
120953 sqlite3 *db, /* Database connection to search */
120954 u8 enc, /* Desired text encoding */
120955 const char *zName, /* Name of the collating sequence. Might be NULL */
120956 int create /* True to create CollSeq if doesn't already exist */
120957){
120958 CollSeq *pColl;
120959 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 )((void) (0));
120960 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE )((void) (0));
120961 if( zName ){
120962 pColl = findCollSeqEntry(db, zName, create);
120963 if( pColl ) pColl += enc-1;
120964 }else{
120965 pColl = db->pDfltColl;
120966 }
120967 return pColl;
120968}
120969
120970/*
120971** Change the text encoding for a database connection. This means that
120972** the pDfltColl must change as well.
120973*/
120974SQLITE_PRIVATEstatic void sqlite3SetTextEncoding(sqlite3 *db, u8 enc){
120975 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE )((void) (0));
120976 db->enc = enc;
120977 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
120978 ** strings is BINARY.
120979 */
120980 db->pDfltColl = sqlite3FindCollSeq(db, enc, sqlite3StrBINARY, 0);
120981}
120982
120983/*
120984** This function is responsible for invoking the collation factory callback
120985** or substituting a collation sequence of a different encoding when the
120986** requested collation sequence is not available in the desired encoding.
120987**
120988** If it is not NULL, then pColl must point to the database native encoding
120989** collation sequence with name zName, length nName.
120990**
120991** The return value is either the collation sequence to be used in database
120992** db for collation type name zName, length nName, or NULL, if no collation
120993** sequence can be found. If no collation is found, leave an error message.
120994**
120995** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
120996*/
120997SQLITE_PRIVATEstatic CollSeq *sqlite3GetCollSeq(
120998 Parse *pParse, /* Parsing context */
120999 u8 enc, /* The desired encoding for the collating sequence */
121000 CollSeq *pColl, /* Collating sequence with native encoding, or NULL */
121001 const char *zName /* Collating sequence name */
121002){
121003 CollSeq *p;
121004 sqlite3 *db = pParse->db;
121005
121006 p = pColl;
121007 if( !p ){
121008 p = sqlite3FindCollSeq(db, enc, zName, 0);
121009 }
121010 if( !p || !p->xCmp ){
121011 /* No collation sequence of this type for this encoding is registered.
121012 ** Call the collation factory to see if it can supply us with one.
121013 */
121014 callCollNeeded(db, enc, zName);
121015 p = sqlite3FindCollSeq(db, enc, zName, 0);
121016 }
121017 if( p && !p->xCmp && synthCollSeq(db, p) ){
121018 p = 0;
121019 }
121020 assert( !p || p->xCmp )((void) (0));
121021 if( p==0 ){
121022 sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
121023 pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ(1 | (1<<8));
121024 }
121025 return p;
121026}
121027
121028/*
121029** This function returns the collation sequence for database native text
121030** encoding identified by the string zName.
121031**
121032** If the requested collation sequence is not available, or not available
121033** in the database native encoding, the collation factory is invoked to
121034** request it. If the collation factory does not supply such a sequence,
121035** and the sequence is available in another text encoding, then that is
121036** returned instead.
121037**
121038** If no versions of the requested collations sequence are available, or
121039** another error occurs, NULL is returned and an error message written into
121040** pParse.
121041**
121042** This routine is a wrapper around sqlite3FindCollSeq(). This routine
121043** invokes the collation factory if the named collation cannot be found
121044** and generates an error message.
121045**
121046** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
121047*/
121048SQLITE_PRIVATEstatic CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
121049 sqlite3 *db = pParse->db;
121050 u8 enc = ENC(db)((db)->enc);
121051 u8 initbusy = db->init.busy;
121052 CollSeq *pColl;
121053
121054 pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
121055 if( !initbusy && (!pColl || !pColl->xCmp) ){
121056 pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName);
121057 }
121058
121059 return pColl;
121060}
121061
121062/* During the search for the best function definition, this procedure
121063** is called to test how well the function passed as the first argument
121064** matches the request for a function with nArg arguments in a system
121065** that uses encoding enc. The value returned indicates how well the
121066** request is matched. A higher value indicates a better match.
121067**
121068** If nArg is -1 that means to only return a match (non-zero) if p->nArg
121069** is also -1. In other words, we are searching for a function that
121070** takes a variable number of arguments.
121071**
121072** If nArg is -2 that means that we are searching for any function
121073** regardless of the number of arguments it uses, so return a positive
121074** match score for any
121075**
121076** The returned value is always between 0 and 6, as follows:
121077**
121078** 0: Not a match.
121079** 1: UTF8/16 conversion required and function takes any number of arguments.
121080** 2: UTF16 byte order change required and function takes any number of args.
121081** 3: encoding matches and function takes any number of arguments
121082** 4: UTF8/16 conversion required - argument count matches exactly
121083** 5: UTF16 byte order conversion required - argument count matches exactly
121084** 6: Perfect match: encoding and argument count match exactly.
121085**
121086** If nArg==(-2) then any function with a non-null xSFunc is
121087** a perfect match and any function with xSFunc NULL is
121088** a non-match.
121089*/
121090#define FUNC_PERFECT_MATCH6 6 /* The score for a perfect match */
121091static int matchQuality(
121092 FuncDef *p, /* The function we are evaluating for match quality */
121093 int nArg, /* Desired number of arguments. (-1)==any */
121094 u8 enc /* Desired text encoding */
121095){
121096 int match;
121097 assert( p->nArg>=-1 )((void) (0));
121098
121099 /* Wrong number of arguments means "no match" */
121100 if( p->nArg!=nArg ){
121101 if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH6;
121102 if( p->nArg>=0 ) return 0;
121103 }
121104
121105 /* Give a better score to a function with a specific number of arguments
121106 ** than to function that accepts any number of arguments. */
121107 if( p->nArg==nArg ){
121108 match = 4;
121109 }else{
121110 match = 1;
121111 }
121112
121113 /* Bonus points if the text encoding matches */
121114 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK0x0003) ){
121115 match += 2; /* Exact encoding match */
121116 }else if( (enc & p->funcFlags & 2)!=0 ){
121117 match += 1; /* Both are UTF16, but with different byte orders */
121118 }
121119
121120 return match;
121121}
121122
121123/*
121124** Search a FuncDefHash for a function with the given name. Return
121125** a pointer to the matching FuncDef if found, or 0 if there is no match.
121126*/
121127SQLITE_PRIVATEstatic FuncDef *sqlite3FunctionSearch(
121128 int h, /* Hash of the name */
121129 const char *zFunc /* Name of function */
121130){
121131 FuncDef *p;
121132 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
121133 assert( p->funcFlags & SQLITE_FUNC_BUILTIN )((void) (0));
121134 if( sqlite3StrICmp(p->zName, zFunc)==0 ){
121135 return p;
121136 }
121137 }
121138 return 0;
121139}
121140
121141/*
121142** Insert a new FuncDef into a FuncDefHash hash table.
121143*/
121144SQLITE_PRIVATEstatic void sqlite3InsertBuiltinFuncs(
121145 FuncDef *aDef, /* List of global functions to be inserted */
121146 int nDef /* Length of the apDef[] list */
121147){
121148 int i;
121149 for(i=0; i<nDef; i++){
121150 FuncDef *pOther;
121151 const char *zName = aDef[i].zName;
121152 int nName = sqlite3Strlen30(zName);
121153 int h = SQLITE_FUNC_HASH(zName[0], nName)(((zName[0])+(nName))%23);
121154 assert( aDef[i].funcFlags & SQLITE_FUNC_BUILTIN )((void) (0));
121155 pOther = sqlite3FunctionSearch(h, zName);
121156 if( pOther ){
121157 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] )((void) (0));
121158 aDef[i].pNext = pOther->pNext;
121159 pOther->pNext = &aDef[i];
121160 }else{
121161 aDef[i].pNext = 0;
121162 aDef[i].u.pHash = sqlite3BuiltinFunctions.a[h];
121163 sqlite3BuiltinFunctions.a[h] = &aDef[i];
121164 }
121165 }
121166}
121167
121168
121169
121170/*
121171** Locate a user function given a name, a number of arguments and a flag
121172** indicating whether the function prefers UTF-16 over UTF-8. Return a
121173** pointer to the FuncDef structure that defines that function, or return
121174** NULL if the function does not exist.
121175**
121176** If the createFlag argument is true, then a new (blank) FuncDef
121177** structure is created and liked into the "db" structure if a
121178** no matching function previously existed.
121179**
121180** If nArg is -2, then the first valid function found is returned. A
121181** function is valid if xSFunc is non-zero. The nArg==(-2)
121182** case is used to see if zName is a valid function name for some number
121183** of arguments. If nArg is -2, then createFlag must be 0.
121184**
121185** If createFlag is false, then a function with the required name and
121186** number of arguments may be returned even if the eTextRep flag does not
121187** match that requested.
121188*/
121189SQLITE_PRIVATEstatic FuncDef *sqlite3FindFunction(
121190 sqlite3 *db, /* An open database */
121191 const char *zName, /* Name of the function. zero-terminated */
121192 int nArg, /* Number of arguments. -1 means any number */
121193 u8 enc, /* Preferred text encoding */
121194 u8 createFlag /* Create new entry if true and does not otherwise exist */
121195){
121196 FuncDef *p; /* Iterator variable */
121197 FuncDef *pBest = 0; /* Best match found so far */
121198 int bestScore = 0; /* Score of best match */
121199 int h; /* Hash value */
121200 int nName; /* Length of the name */
121201
121202 assert( nArg>=(-2) )((void) (0));
121203 assert( nArg>=(-1) || createFlag==0 )((void) (0));
121204 nName = sqlite3Strlen30(zName);
121205
121206 /* First search for a match amongst the application-defined functions.
121207 */
121208 p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
121209 while( p ){
121210 int score = matchQuality(p, nArg, enc);
121211 if( score>bestScore ){
121212 pBest = p;
121213 bestScore = score;
121214 }
121215 p = p->pNext;
121216 }
121217
121218 /* If no match is found, search the built-in functions.
121219 **
121220 ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
121221 ** functions even if a prior app-defined function was found. And give
121222 ** priority to built-in functions.
121223 **
121224 ** Except, if createFlag is true, that means that we are trying to
121225 ** install a new function. Whatever FuncDef structure is returned it will
121226 ** have fields overwritten with new information appropriate for the
121227 ** new function. But the FuncDefs for built-in functions are read-only.
121228 ** So we must not search for built-ins when creating a new function.
121229 */
121230 if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin0x0002)!=0) ){
121231 bestScore = 0;
121232 h = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zName[0]], nName)(((sqlite3UpperToLower[(u8)zName[0]])+(nName))%23);
121233 p = sqlite3FunctionSearch(h, zName);
121234 while( p ){
121235 int score = matchQuality(p, nArg, enc);
121236 if( score>bestScore ){
121237 pBest = p;
121238 bestScore = score;
121239 }
121240 p = p->pNext;
121241 }
121242 }
121243
121244 /* If the createFlag parameter is true and the search did not reveal an
121245 ** exact match for the name, number of arguments and encoding, then add a
121246 ** new entry to the hash table and return it.
121247 */
121248 if( createFlag && bestScore<FUNC_PERFECT_MATCH6 &&
121249 (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
121250 FuncDef *pOther;
121251 u8 *z;
121252 pBest->zName = (const char*)&pBest[1];
121253 pBest->nArg = (u16)nArg;
121254 pBest->funcFlags = enc;
121255 memcpy((char*)&pBest[1], zName, nName+1);
121256 for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
121257 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
121258 if( pOther==pBest ){
121259 sqlite3DbFree(db, pBest);
121260 sqlite3OomFault(db);
121261 return 0;
121262 }else{
121263 pBest->pNext = pOther;
121264 }
121265 }
121266
121267 if( pBest && (pBest->xSFunc || createFlag) ){
121268 return pBest;
121269 }
121270 return 0;
121271}
121272
121273/*
121274** Free all resources held by the schema structure. The void* argument points
121275** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
121276** pointer itself, it just cleans up subsidiary resources (i.e. the contents
121277** of the schema hash tables).
121278**
121279** The Schema.cache_size variable is not cleared.
121280*/
121281SQLITE_PRIVATEstatic void sqlite3SchemaClear(void *p){
121282 Hash temp1;
121283 Hash temp2;
121284 HashElem *pElem;
121285 Schema *pSchema = (Schema *)p;
121286
121287 temp1 = pSchema->tblHash;
121288 temp2 = pSchema->trigHash;
121289 sqlite3HashInit(&pSchema->trigHash);
121290 sqlite3HashClear(&pSchema->idxHash);
121291 for(pElem=sqliteHashFirst(&temp2)((&temp2)->first); pElem; pElem=sqliteHashNext(pElem)((pElem)->next)){
121292 sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem)((pElem)->data));
121293 }
121294 sqlite3HashClear(&temp2);
121295 sqlite3HashInit(&pSchema->tblHash);
121296 for(pElem=sqliteHashFirst(&temp1)((&temp1)->first); pElem; pElem=sqliteHashNext(pElem)((pElem)->next)){
121297 Table *pTab = sqliteHashData(pElem)((pElem)->data);
121298 sqlite3DeleteTable(0, pTab);
121299 }
121300 sqlite3HashClear(&temp1);
121301 sqlite3HashClear(&pSchema->fkeyHash);
121302 pSchema->pSeqTab = 0;
121303 if( pSchema->schemaFlags & DB_SchemaLoaded0x0001 ){
121304 pSchema->iGeneration++;
121305 }
121306 pSchema->schemaFlags &= ~(DB_SchemaLoaded0x0001|DB_ResetWanted0x0008);
121307}
121308
121309/*
121310** Find and return the schema associated with a BTree. Create
121311** a new one if necessary.
121312*/
121313SQLITE_PRIVATEstatic Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
121314 Schema * p;
121315 if( pBt ){
121316 p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
121317 }else{
121318 p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
121319 }
121320 if( !p ){
121321 sqlite3OomFault(db);
121322 }else if ( 0==p->file_format ){
121323 sqlite3HashInit(&p->tblHash);
121324 sqlite3HashInit(&p->idxHash);
121325 sqlite3HashInit(&p->trigHash);
121326 sqlite3HashInit(&p->fkeyHash);
121327 p->enc = SQLITE_UTF81;
121328 }
121329 return p;
121330}
121331
121332/************** End of callback.c ********************************************/
121333/************** Begin file delete.c ******************************************/
121334/*
121335** 2001 September 15
121336**
121337** The author disclaims copyright to this source code. In place of
121338** a legal notice, here is a blessing:
121339**
121340** May you do good and not evil.
121341** May you find forgiveness for yourself and forgive others.
121342** May you share freely, never taking more than you give.
121343**
121344*************************************************************************
121345** This file contains C code routines that are called by the parser
121346** in order to generate code for DELETE FROM statements.
121347*/
121348/* #include "sqliteInt.h" */
121349
121350/*
121351** While a SrcList can in general represent multiple tables and subqueries
121352** (as in the FROM clause of a SELECT statement) in this case it contains
121353** the name of a single table, as one might find in an INSERT, DELETE,
121354** or UPDATE statement. Look up that table in the symbol table and
121355** return a pointer. Set an error message and return NULL if the table
121356** name is not found or if any other error occurs.
121357**
121358** The following fields are initialized appropriate in pSrc:
121359**
121360** pSrc->a[0].pTab Pointer to the Table object
121361** pSrc->a[0].pIndex Pointer to the INDEXED BY index, if there is one
121362**
121363*/
121364SQLITE_PRIVATEstatic Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
121365 SrcItem *pItem = pSrc->a;
121366 Table *pTab;
121367 assert( pItem && pSrc->nSrc>=1 )((void) (0));
121368 pTab = sqlite3LocateTableItem(pParse, 0, pItem);
121369 sqlite3DeleteTable(pParse->db, pItem->pTab);
121370 pItem->pTab = pTab;
121371 if( pTab ){
121372 pTab->nTabRef++;
121373 if( pItem->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pItem) ){
121374 pTab = 0;
121375 }
121376 }
121377 return pTab;
121378}
121379
121380/* Generate byte-code that will report the number of rows modified
121381** by a DELETE, INSERT, or UPDATE statement.
121382*/
121383SQLITE_PRIVATEstatic void sqlite3CodeChangeCount(Vdbe *v, int regCounter, const char *zColName){
121384 sqlite3VdbeAddOp0(v, OP_FkCheck83);
121385 sqlite3VdbeAddOp2(v, OP_ResultRow84, regCounter, 1);
121386 sqlite3VdbeSetNumCols(v, 1);
121387 sqlite3VdbeSetColName(v, 0, COLNAME_NAME0, zColName, SQLITE_STATIC((sqlite3_destructor_type)0));
121388}
121389
121390/* Return true if table pTab is read-only.
121391**
121392** A table is read-only if any of the following are true:
121393**
121394** 1) It is a virtual table and no implementation of the xUpdate method
121395** has been provided
121396**
121397** 2) It is a system table (i.e. sqlite_schema), this call is not
121398** part of a nested parse and writable_schema pragma has not
121399** been specified
121400**
121401** 3) The table is a shadow table, the database connection is in
121402** defensive mode, and the current sqlite3_prepare()
121403** is for a top-level SQL statement.
121404*/
121405static int tabIsReadOnly(Parse *pParse, Table *pTab){
121406 sqlite3 *db;
121407 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
121408 return sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0;
121409 }
121410 if( (pTab->tabFlags & (TF_Readonly0x00000001|TF_Shadow0x00001000))==0 ) return 0;
121411 db = pParse->db;
121412 if( (pTab->tabFlags & TF_Readonly0x00000001)!=0 ){
121413 return sqlite3WritableSchema(db)==0 && pParse->nested==0;
121414 }
121415 assert( pTab->tabFlags & TF_Shadow )((void) (0));
121416 return sqlite3ReadOnlyShadowTables(db);
121417}
121418
121419/*
121420** Check to make sure the given table is writable. If it is not
121421** writable, generate an error message and return 1. If it is
121422** writable return 0;
121423*/
121424SQLITE_PRIVATEstatic int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
121425 if( tabIsReadOnly(pParse, pTab) ){
121426 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
121427 return 1;
121428 }
121429#ifndef SQLITE_OMIT_VIEW
121430 if( !viewOk && IsView(pTab)((pTab)->eTabType==2) ){
121431 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
121432 return 1;
121433 }
121434#endif
121435 return 0;
121436}
121437
121438
121439#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
121440/*
121441** Evaluate a view and store its result in an ephemeral table. The
121442** pWhere argument is an optional WHERE clause that restricts the
121443** set of rows in the view that are to be added to the ephemeral table.
121444*/
121445SQLITE_PRIVATEstatic void sqlite3MaterializeView(
121446 Parse *pParse, /* Parsing context */
121447 Table *pView, /* View definition */
121448 Expr *pWhere, /* Optional WHERE clause to be added */
121449 ExprList *pOrderBy, /* Optional ORDER BY clause */
121450 Expr *pLimit, /* Optional LIMIT clause */
121451 int iCur /* Cursor number for ephemeral table */
121452){
121453 SelectDest dest;
121454 Select *pSel;
121455 SrcList *pFrom;
121456 sqlite3 *db = pParse->db;
121457 int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
121458 pWhere = sqlite3ExprDup(db, pWhere, 0);
121459 pFrom = sqlite3SrcListAppend(pParse, 0, 0, 0);
121460 if( pFrom ){
121461 assert( pFrom->nSrc==1 )((void) (0));
121462 pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
121463 pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
121464 assert( pFrom->a[0].fg.isUsing==0 )((void) (0));
121465 assert( pFrom->a[0].u3.pOn==0 )((void) (0));
121466 }
121467 pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, pOrderBy,
121468 SF_IncludeHidden0x0020000, pLimit);
121469 sqlite3SelectDestInit(&dest, SRT_EphemTab12, iCur);
121470 sqlite3Select(pParse, pSel, &dest);
121471 sqlite3SelectDelete(db, pSel);
121472}
121473#endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
121474
121475#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT1) && !defined(SQLITE_OMIT_SUBQUERY)
121476/*
121477** Generate an expression tree to implement the WHERE, ORDER BY,
121478** and LIMIT/OFFSET portion of DELETE and UPDATE statements.
121479**
121480** DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
121481** \__________________________/
121482** pLimitWhere (pInClause)
121483*/
121484SQLITE_PRIVATEstatic Expr *sqlite3LimitWhere(
121485 Parse *pParse, /* The parser context */
121486 SrcList *pSrc, /* the FROM clause -- which tables to scan */
121487 Expr *pWhere, /* The WHERE clause. May be null */
121488 ExprList *pOrderBy, /* The ORDER BY clause. May be null */
121489 Expr *pLimit, /* The LIMIT clause. May be null */
121490 char *zStmtType /* Either DELETE or UPDATE. For err msgs. */
121491){
121492 sqlite3 *db = pParse->db;
121493 Expr *pLhs = NULL((void*)0); /* LHS of IN(SELECT...) operator */
121494 Expr *pInClause = NULL((void*)0); /* WHERE rowid IN ( select ) */
121495 ExprList *pEList = NULL((void*)0); /* Expression list contaning only pSelectRowid */
121496 SrcList *pSelectSrc = NULL((void*)0); /* SELECT rowid FROM x ... (dup of pSrc) */
121497 Select *pSelect = NULL((void*)0); /* Complete SELECT tree */
121498 Table *pTab;
121499
121500 /* Check that there isn't an ORDER BY without a LIMIT clause.
121501 */
121502 if( pOrderBy && pLimit==0 ) {
121503 sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
121504 sqlite3ExprDelete(pParse->db, pWhere);
121505 sqlite3ExprListDelete(pParse->db, pOrderBy);
121506 return 0;
121507 }
121508
121509 /* We only need to generate a select expression if there
121510 ** is a limit/offset term to enforce.
121511 */
121512 if( pLimit == 0 ) {
121513 return pWhere;
121514 }
121515
121516 /* Generate a select expression tree to enforce the limit/offset
121517 ** term for the DELETE or UPDATE statement. For example:
121518 ** DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
121519 ** becomes:
121520 ** DELETE FROM table_a WHERE rowid IN (
121521 ** SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
121522 ** );
121523 */
121524
121525 pTab = pSrc->a[0].pTab;
121526 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
121527 pLhs = sqlite3PExpr(pParse, TK_ROW75, 0, 0);
121528 pEList = sqlite3ExprListAppend(
121529 pParse, 0, sqlite3PExpr(pParse, TK_ROW75, 0, 0)
121530 );
121531 }else{
121532 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
121533 if( pPk->nKeyCol==1 ){
121534 const char *zName = pTab->aCol[pPk->aiColumn[0]].zCnName;
121535 pLhs = sqlite3Expr(db, TK_ID59, zName);
121536 pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID59, zName));
121537 }else{
121538 int i;
121539 for(i=0; i<pPk->nKeyCol; i++){
121540 Expr *p = sqlite3Expr(db, TK_ID59, pTab->aCol[pPk->aiColumn[i]].zCnName);
121541 pEList = sqlite3ExprListAppend(pParse, pEList, p);
121542 }
121543 pLhs = sqlite3PExpr(pParse, TK_VECTOR177, 0, 0);
121544 if( pLhs ){
121545 pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0);
121546 }
121547 }
121548 }
121549
121550 /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
121551 ** and the SELECT subtree. */
121552 pSrc->a[0].pTab = 0;
121553 pSelectSrc = sqlite3SrcListDup(db, pSrc, 0);
121554 pSrc->a[0].pTab = pTab;
121555 if( pSrc->a[0].fg.isIndexedBy ){
121556 assert( pSrc->a[0].fg.isCte==0 )((void) (0));
121557 pSrc->a[0].u2.pIBIndex = 0;
121558 pSrc->a[0].fg.isIndexedBy = 0;
121559 sqlite3DbFree(db, pSrc->a[0].u1.zIndexedBy);
121560 }else if( pSrc->a[0].fg.isCte ){
121561 pSrc->a[0].u2.pCteUse->nUse++;
121562 }
121563
121564 /* generate the SELECT expression tree. */
121565 pSelect = sqlite3SelectNew(pParse, pEList, pSelectSrc, pWhere, 0 ,0,
121566 pOrderBy,0,pLimit
121567 );
121568
121569 /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
121570 pInClause = sqlite3PExpr(pParse, TK_IN49, pLhs, 0);
121571 sqlite3PExprAddSelect(pParse, pInClause, pSelect);
121572 return pInClause;
121573}
121574#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) */
121575 /* && !defined(SQLITE_OMIT_SUBQUERY) */
121576
121577/*
121578** Generate code for a DELETE FROM statement.
121579**
121580** DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
121581** \________/ \________________/
121582** pTabList pWhere
121583*/
121584SQLITE_PRIVATEstatic void sqlite3DeleteFrom(
121585 Parse *pParse, /* The parser context */
121586 SrcList *pTabList, /* The table from which we should delete things */
121587 Expr *pWhere, /* The WHERE clause. May be null */
121588 ExprList *pOrderBy, /* ORDER BY clause. May be null */
121589 Expr *pLimit /* LIMIT clause. May be null */
121590){
121591 Vdbe *v; /* The virtual database engine */
121592 Table *pTab; /* The table from which records will be deleted */
121593 int i; /* Loop counter */
121594 WhereInfo *pWInfo; /* Information about the WHERE clause */
121595 Index *pIdx; /* For looping over indices of the table */
121596 int iTabCur; /* Cursor number for the table */
121597 int iDataCur = 0; /* VDBE cursor for the canonical data source */
121598 int iIdxCur = 0; /* Cursor number of the first index */
121599 int nIdx; /* Number of indices */
121600 sqlite3 *db; /* Main database structure */
121601 AuthContext sContext; /* Authorization context */
121602 NameContext sNC; /* Name context to resolve expressions in */
121603 int iDb; /* Database number */
121604 int memCnt = 0; /* Memory cell used for change counting */
121605 int rcauth; /* Value returned by authorization callback */
121606 int eOnePass; /* ONEPASS_OFF or _SINGLE or _MULTI */
121607 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
121608 u8 *aToOpen = 0; /* Open cursor iTabCur+j if aToOpen[j] is true */
121609 Index *pPk; /* The PRIMARY KEY index on the table */
121610 int iPk = 0; /* First of nPk registers holding PRIMARY KEY value */
121611 i16 nPk = 1; /* Number of columns in the PRIMARY KEY */
121612 int iKey; /* Memory cell holding key of row to be deleted */
121613 i16 nKey; /* Number of memory cells in the row key */
121614 int iEphCur = 0; /* Ephemeral table holding all primary key values */
121615 int iRowSet = 0; /* Register for rowset of rows to delete */
121616 int addrBypass = 0; /* Address of jump over the delete logic */
121617 int addrLoop = 0; /* Top of the delete loop */
121618 int addrEphOpen = 0; /* Instruction to open the Ephemeral table */
121619 int bComplex; /* True if there are triggers or FKs or
121620 ** subqueries in the WHERE clause */
121621
121622#ifndef SQLITE_OMIT_TRIGGER
121623 int isView; /* True if attempting to delete from a view */
121624 Trigger *pTrigger; /* List of table triggers, if required */
121625#endif
121626
121627 memset(&sContext, 0, sizeof(sContext));
121628 db = pParse->db;
121629 assert( db->pParse==pParse )((void) (0));
121630 if( pParse->nErr ){
121631 goto delete_from_cleanup;
121632 }
121633 assert( db->mallocFailed==0 )((void) (0));
121634 assert( pTabList->nSrc==1 )((void) (0));
121635
121636 /* Locate the table which we want to delete. This table has to be
121637 ** put in an SrcList structure because some of the subroutines we
121638 ** will be calling are designed to work with multiple tables and expect
121639 ** an SrcList* parameter instead of just a Table* parameter.
121640 */
121641 pTab = sqlite3SrcListLookup(pParse, pTabList);
121642 if( pTab==0 ) goto delete_from_cleanup;
121643
121644 /* Figure out if we have any triggers and if the table being
121645 ** deleted from is a view
121646 */
121647#ifndef SQLITE_OMIT_TRIGGER
121648 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE128, 0, 0);
121649 isView = IsView(pTab)((pTab)->eTabType==2);
121650#else
121651# define pTrigger 0
121652# define isView 0
121653#endif
121654 bComplex = pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0);
121655#ifdef SQLITE_OMIT_VIEW
121656# undef isView
121657# define isView 0
121658#endif
121659
121660#if TREETRACE_ENABLED0
121661 if( sqlite3TreeTrace & 0x10000 ){
121662 sqlite3TreeViewLine(0, "In sqlite3Delete() at %s:%d", __FILE__"3rdparty/sqlite3/sqlite3.c", __LINE__121662);
121663 sqlite3TreeViewDelete(pParse->pWith, pTabList, pWhere,
121664 pOrderBy, pLimit, pTrigger);
121665 }
121666#endif
121667
121668#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT1
121669 if( !isView ){
121670 pWhere = sqlite3LimitWhere(
121671 pParse, pTabList, pWhere, pOrderBy, pLimit, "DELETE"
121672 );
121673 pOrderBy = 0;
121674 pLimit = 0;
121675 }
121676#endif
121677
121678 /* If pTab is really a view, make sure it has been initialized.
121679 */
121680 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
121681 goto delete_from_cleanup;
121682 }
121683
121684 if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
121685 goto delete_from_cleanup;
121686 }
121687 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
121688 assert( iDb<db->nDb )((void) (0));
121689 rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE9, pTab->zName, 0,
121690 db->aDb[iDb].zDbSName);
121691 assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE )((void) (0));
121692 if( rcauth==SQLITE_DENY1 ){
121693 goto delete_from_cleanup;
121694 }
121695 assert(!isView || pTrigger)((void) (0));
121696
121697 /* Assign cursor numbers to the table and all its indices.
121698 */
121699 assert( pTabList->nSrc==1 )((void) (0));
121700 iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
121701 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
121702 pParse->nTab++;
121703 }
121704
121705 /* Start the view context
121706 */
121707 if( isView ){
121708 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
121709 }
121710
121711 /* Begin generating code.
121712 */
121713 v = sqlite3GetVdbe(pParse);
121714 if( v==0 ){
121715 goto delete_from_cleanup;
121716 }
121717 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
121718 sqlite3BeginWriteOperation(pParse, bComplex, iDb);
121719
121720 /* If we are trying to delete from a view, realize that view into
121721 ** an ephemeral table.
121722 */
121723#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
121724 if( isView ){
121725 sqlite3MaterializeView(pParse, pTab,
121726 pWhere, pOrderBy, pLimit, iTabCur
121727 );
121728 iDataCur = iIdxCur = iTabCur;
121729 pOrderBy = 0;
121730 pLimit = 0;
121731 }
121732#endif
121733
121734 /* Resolve the column names in the WHERE clause.
121735 */
121736 memset(&sNC, 0, sizeof(sNC));
121737 sNC.pParse = pParse;
121738 sNC.pSrcList = pTabList;
121739 if( sqlite3ResolveExprNames(&sNC, pWhere) ){
121740 goto delete_from_cleanup;
121741 }
121742
121743 /* Initialize the counter of the number of rows deleted, if
121744 ** we are counting rows.
121745 */
121746 if( (db->flags & SQLITE_CountRows((u64)(0x00001)<<32))!=0
121747 && !pParse->nested
121748 && !pParse->pTriggerTab
121749 && !pParse->bReturning
121750 ){
121751 memCnt = ++pParse->nMem;
121752 sqlite3VdbeAddOp2(v, OP_Integer71, 0, memCnt);
121753 }
121754
121755#ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
121756 /* Special case: A DELETE without a WHERE clause deletes everything.
121757 ** It is easier just to erase the whole table. Prior to version 3.6.5,
121758 ** this optimization caused the row change count (the value returned by
121759 ** API function sqlite3_count_changes) to be set incorrectly.
121760 **
121761 ** The "rcauth==SQLITE_OK" terms is the
121762 ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and
121763 ** the callback returns SQLITE_IGNORE then the DELETE operation proceeds but
121764 ** the truncate optimization is disabled and all rows are deleted
121765 ** individually.
121766 */
121767 if( rcauth==SQLITE_OK0
121768 && pWhere==0
121769 && !bComplex
121770 && !IsVirtual(pTab)((pTab)->eTabType==1)
121771#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
121772 && db->xPreUpdateCallback==0
121773#endif
121774 ){
121775 assert( !isView )((void) (0));
121776 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
121777 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
121778 sqlite3VdbeAddOp4(v, OP_Clear145, pTab->tnum, iDb, memCnt ? memCnt : -1,
121779 pTab->zName, P4_STATIC(-1));
121780 }
121781 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
121782 assert( pIdx->pSchema==pTab->pSchema )((void) (0));
121783 sqlite3VdbeAddOp2(v, OP_Clear145, pIdx->tnum, iDb);
121784 if( IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) && !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
121785 sqlite3VdbeChangeP3(v, -1, memCnt ? memCnt : -1);
121786 }
121787 }
121788 }else
121789#endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
121790 {
121791 u16 wcf = WHERE_ONEPASS_DESIRED0x0004|WHERE_DUPLICATES_OK0x0010;
121792 if( sNC.ncFlags & NC_VarSelect0x000040 ) bComplex = 1;
121793 wcf |= (bComplex ? 0 : WHERE_ONEPASS_MULTIROW0x0008);
121794 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
121795 /* For a rowid table, initialize the RowSet to an empty set */
121796 pPk = 0;
121797 nPk = 1;
121798 iRowSet = ++pParse->nMem;
121799 sqlite3VdbeAddOp2(v, OP_Null75, 0, iRowSet);
121800 }else{
121801 /* For a WITHOUT ROWID table, create an ephemeral table used to
121802 ** hold all primary keys for rows to be deleted. */
121803 pPk = sqlite3PrimaryKeyIndex(pTab);
121804 assert( pPk!=0 )((void) (0));
121805 nPk = pPk->nKeyCol;
121806 iPk = pParse->nMem+1;
121807 pParse->nMem += nPk;
121808 iEphCur = pParse->nTab++;
121809 addrEphOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral118, iEphCur, nPk);
121810 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
121811 }
121812
121813 /* Construct a query to find the rowid or primary key for every row
121814 ** to be deleted, based on the WHERE clause. Set variable eOnePass
121815 ** to indicate the strategy used to implement this delete:
121816 **
121817 ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values.
121818 ** ONEPASS_SINGLE: One-pass approach - at most one row deleted.
121819 ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted.
121820 */
121821 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0,0,wcf,iTabCur+1);
121822 if( pWInfo==0 ) goto delete_from_cleanup;
121823 eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
121824 assert( IsVirtual(pTab)==0 || eOnePass!=ONEPASS_MULTI )((void) (0));
121825 assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF )((void) (0));
121826 if( eOnePass!=ONEPASS_SINGLE1 ) sqlite3MultiWrite(pParse);
121827 if( sqlite3WhereUsesDeferredSeek(pWInfo) ){
121828 sqlite3VdbeAddOp1(v, OP_FinishSeek143, iTabCur);
121829 }
121830
121831 /* Keep track of the number of rows to be deleted */
121832 if( memCnt ){
121833 sqlite3VdbeAddOp2(v, OP_AddImm86, memCnt, 1);
121834 }
121835
121836 /* Extract the rowid or primary key for the current row */
121837 if( pPk ){
121838 for(i=0; i<nPk; i++){
121839 assert( pPk->aiColumn[i]>=0 )((void) (0));
121840 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
121841 pPk->aiColumn[i], iPk+i);
121842 }
121843 iKey = iPk;
121844 }else{
121845 iKey = ++pParse->nMem;
121846 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, -1, iKey);
121847 }
121848
121849 if( eOnePass!=ONEPASS_OFF0 ){
121850 /* For ONEPASS, no need to store the rowid/primary-key. There is only
121851 ** one, so just keep it in its register(s) and fall through to the
121852 ** delete code. */
121853 nKey = nPk; /* OP_Found will use an unpacked key */
121854 aToOpen = sqlite3DbMallocRawNN(db, nIdx+2);
121855 if( aToOpen==0 ){
121856 sqlite3WhereEnd(pWInfo);
121857 goto delete_from_cleanup;
121858 }
121859 memset(aToOpen, 1, nIdx+1);
121860 aToOpen[nIdx+1] = 0;
121861 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
121862 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
121863 if( addrEphOpen ) sqlite3VdbeChangeToNoop(v, addrEphOpen);
121864 addrBypass = sqlite3VdbeMakeLabel(pParse);
121865 }else{
121866 if( pPk ){
121867 /* Add the PK key for this row to the temporary table */
121868 iKey = ++pParse->nMem;
121869 nKey = 0; /* Zero tells OP_Found to use a composite key */
121870 sqlite3VdbeAddOp4(v, OP_MakeRecord97, iPk, nPk, iKey,
121871 sqlite3IndexAffinityStr(pParse->db, pPk), nPk);
121872 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iEphCur, iKey, iPk, nPk);
121873 }else{
121874 /* Add the rowid of the row to be deleted to the RowSet */
121875 nKey = 1; /* OP_DeferredSeek always uses a single rowid */
121876 sqlite3VdbeAddOp2(v, OP_RowSetAdd156, iRowSet, iKey);
121877 }
121878 sqlite3WhereEnd(pWInfo);
121879 }
121880
121881 /* Unless this is a view, open cursors for the table we are
121882 ** deleting from and all its indices. If this is a view, then the
121883 ** only effect this statement has is to fire the INSTEAD OF
121884 ** triggers.
121885 */
121886 if( !isView ){
121887 int iAddrOnce = 0;
121888 if( eOnePass==ONEPASS_MULTI2 ){
121889 iAddrOnce = sqlite3VdbeAddOp0(v, OP_Once14); VdbeCoverage(v);
121890 }
121891 testcase( IsVirtual(pTab) );
121892 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite113, OPFLAG_FORDELETE0x08,
121893 iTabCur, aToOpen, &iDataCur, &iIdxCur);
121894 assert( pPk || IsVirtual(pTab) || iDataCur==iTabCur )((void) (0));
121895 assert( pPk || IsVirtual(pTab) || iIdxCur==iDataCur+1 )((void) (0));
121896 if( eOnePass==ONEPASS_MULTI2 ){
121897 sqlite3VdbeJumpHereOrPopInst(v, iAddrOnce);
121898 }
121899 }
121900
121901 /* Set up a loop over the rowids/primary-keys that were found in the
121902 ** where-clause loop above.
121903 */
121904 if( eOnePass!=ONEPASS_OFF0 ){
121905 assert( nKey==nPk )((void) (0)); /* OP_Found will use an unpacked key */
121906 if( !IsVirtual(pTab)((pTab)->eTabType==1) && aToOpen[iDataCur-iTabCur] ){
121907 assert( pPk!=0 || IsView(pTab) )((void) (0));
121908 sqlite3VdbeAddOp4Int(v, OP_NotFound27, iDataCur, addrBypass, iKey, nKey);
121909 VdbeCoverage(v);
121910 }
121911 }else if( pPk ){
121912 addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind35, iEphCur); VdbeCoverage(v);
121913 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
121914 sqlite3VdbeAddOp3(v, OP_Column94, iEphCur, 0, iKey);
121915 }else{
121916 sqlite3VdbeAddOp2(v, OP_RowData134, iEphCur, iKey);
121917 }
121918 assert( nKey==0 )((void) (0)); /* OP_Found will use a composite key */
121919 }else{
121920 addrLoop = sqlite3VdbeAddOp3(v, OP_RowSetRead45, iRowSet, 0, iKey);
121921 VdbeCoverage(v);
121922 assert( nKey==1 )((void) (0));
121923 }
121924
121925 /* Delete the row */
121926#ifndef SQLITE_OMIT_VIRTUALTABLE
121927 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
121928 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
121929 sqlite3VtabMakeWritable(pParse, pTab);
121930 assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE )((void) (0));
121931 sqlite3MayAbort(pParse);
121932 if( eOnePass==ONEPASS_SINGLE1 ){
121933 sqlite3VdbeAddOp1(v, OP_Close122, iTabCur);
121934 if( sqlite3IsToplevel(pParse)((pParse)->pToplevel==0) ){
121935 pParse->isMultiWrite = 0;
121936 }
121937 }
121938 sqlite3VdbeAddOp4(v, OP_VUpdate7, 0, 1, iKey, pVTab, P4_VTAB(-11));
121939 sqlite3VdbeChangeP5(v, OE_Abort2);
121940 }else
121941#endif
121942 {
121943 int count = (pParse->nested==0); /* True to count changes */
121944 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
121945 iKey, nKey, count, OE_Default11, eOnePass, aiCurOnePass[1]);
121946 }
121947
121948 /* End of the loop over all rowids/primary-keys. */
121949 if( eOnePass!=ONEPASS_OFF0 ){
121950 sqlite3VdbeResolveLabel(v, addrBypass);
121951 sqlite3WhereEnd(pWInfo);
121952 }else if( pPk ){
121953 sqlite3VdbeAddOp2(v, OP_Next38, iEphCur, addrLoop+1); VdbeCoverage(v);
121954 sqlite3VdbeJumpHere(v, addrLoop);
121955 }else{
121956 sqlite3VdbeGoto(v, addrLoop);
121957 sqlite3VdbeJumpHere(v, addrLoop);
121958 }
121959 } /* End non-truncate path */
121960
121961 /* Update the sqlite_sequence table by storing the content of the
121962 ** maximum rowid counter values recorded while inserting into
121963 ** autoincrement tables.
121964 */
121965 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
121966 sqlite3AutoincrementEnd(pParse);
121967 }
121968
121969 /* Return the number of rows that were deleted. If this routine is
121970 ** generating code because of a call to sqlite3NestedParse(), do not
121971 ** invoke the callback function.
121972 */
121973 if( memCnt ){
121974 sqlite3CodeChangeCount(v, memCnt, "rows deleted");
121975 }
121976
121977delete_from_cleanup:
121978 sqlite3AuthContextPop(&sContext);
121979 sqlite3SrcListDelete(db, pTabList);
121980 sqlite3ExprDelete(db, pWhere);
121981#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT1)
121982 sqlite3ExprListDelete(db, pOrderBy);
121983 sqlite3ExprDelete(db, pLimit);
121984#endif
121985 sqlite3DbFree(db, aToOpen);
121986 return;
121987}
121988/* Make sure "isView" and other macros defined above are undefined. Otherwise
121989** they may interfere with compilation of other functions in this file
121990** (or in another file, if this file becomes part of the amalgamation). */
121991#ifdef isView
121992 #undef isView
121993#endif
121994#ifdef pTrigger
121995 #undef pTrigger
121996#endif
121997
121998/*
121999** This routine generates VDBE code that causes a single row of a
122000** single table to be deleted. Both the original table entry and
122001** all indices are removed.
122002**
122003** Preconditions:
122004**
122005** 1. iDataCur is an open cursor on the btree that is the canonical data
122006** store for the table. (This will be either the table itself,
122007** in the case of a rowid table, or the PRIMARY KEY index in the case
122008** of a WITHOUT ROWID table.)
122009**
122010** 2. Read/write cursors for all indices of pTab must be open as
122011** cursor number iIdxCur+i for the i-th index.
122012**
122013** 3. The primary key for the row to be deleted must be stored in a
122014** sequence of nPk memory cells starting at iPk. If nPk==0 that means
122015** that a search record formed from OP_MakeRecord is contained in the
122016** single memory location iPk.
122017**
122018** eMode:
122019** Parameter eMode may be passed either ONEPASS_OFF (0), ONEPASS_SINGLE, or
122020** ONEPASS_MULTI. If eMode is not ONEPASS_OFF, then the cursor
122021** iDataCur already points to the row to delete. If eMode is ONEPASS_OFF
122022** then this function must seek iDataCur to the entry identified by iPk
122023** and nPk before reading from it.
122024**
122025** If eMode is ONEPASS_MULTI, then this call is being made as part
122026** of a ONEPASS delete that affects multiple rows. In this case, if
122027** iIdxNoSeek is a valid cursor number (>=0) and is not the same as
122028** iDataCur, then its position should be preserved following the delete
122029** operation. Or, if iIdxNoSeek is not a valid cursor number, the
122030** position of iDataCur should be preserved instead.
122031**
122032** iIdxNoSeek:
122033** If iIdxNoSeek is a valid cursor number (>=0) not equal to iDataCur,
122034** then it identifies an index cursor (from within array of cursors
122035** starting at iIdxCur) that already points to the index entry to be deleted.
122036** Except, this optimization is disabled if there are BEFORE triggers since
122037** the trigger body might have moved the cursor.
122038*/
122039SQLITE_PRIVATEstatic void sqlite3GenerateRowDelete(
122040 Parse *pParse, /* Parsing context */
122041 Table *pTab, /* Table containing the row to be deleted */
122042 Trigger *pTrigger, /* List of triggers to (potentially) fire */
122043 int iDataCur, /* Cursor from which column data is extracted */
122044 int iIdxCur, /* First index cursor */
122045 int iPk, /* First memory cell containing the PRIMARY KEY */
122046 i16 nPk, /* Number of PRIMARY KEY memory cells */
122047 u8 count, /* If non-zero, increment the row change counter */
122048 u8 onconf, /* Default ON CONFLICT policy for triggers */
122049 u8 eMode, /* ONEPASS_OFF, _SINGLE, or _MULTI. See above */
122050 int iIdxNoSeek /* Cursor number of cursor that does not need seeking */
122051){
122052 Vdbe *v = pParse->pVdbe; /* Vdbe */
122053 int iOld = 0; /* First register in OLD.* array */
122054 int iLabel; /* Label resolved to end of generated code */
122055 u8 opSeek; /* Seek opcode */
122056
122057 /* Vdbe is guaranteed to have been allocated by this stage. */
122058 assert( v )((void) (0));
122059 VdbeModuleComment((v, "BEGIN: GenRowDel(%d,%d,%d,%d)",
122060 iDataCur, iIdxCur, iPk, (int)nPk));
122061
122062 /* Seek cursor iCur to the row to delete. If this row no longer exists
122063 ** (this can happen if a trigger program has already deleted it), do
122064 ** not attempt to delete it or fire any DELETE triggers. */
122065 iLabel = sqlite3VdbeMakeLabel(pParse);
122066 opSeek = HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ? OP_NotExists30 : OP_NotFound27;
122067 if( eMode==ONEPASS_OFF0 ){
122068 sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
122069 VdbeCoverageIf(v, opSeek==OP_NotExists);
122070 VdbeCoverageIf(v, opSeek==OP_NotFound);
122071 }
122072
122073 /* If there are any triggers to fire, allocate a range of registers to
122074 ** use for the old.* references in the triggers. */
122075 if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
122076 u32 mask; /* Mask of OLD.* columns in use */
122077 int iCol; /* Iterator used while populating OLD.* */
122078 int addrStart; /* Start of BEFORE trigger programs */
122079
122080 /* TODO: Could use temporary registers here. Also could attempt to
122081 ** avoid copying the contents of the rowid register. */
122082 mask = sqlite3TriggerColmask(
122083 pParse, pTrigger, 0, 0, TRIGGER_BEFORE1|TRIGGER_AFTER2, pTab, onconf
122084 );
122085 mask |= sqlite3FkOldmask(pParse, pTab);
122086 iOld = pParse->nMem+1;
122087 pParse->nMem += (1 + pTab->nCol);
122088
122089 /* Populate the OLD.* pseudo-table register array. These values will be
122090 ** used by any BEFORE and AFTER triggers that exist. */
122091 sqlite3VdbeAddOp2(v, OP_Copy80, iPk, iOld);
122092 for(iCol=0; iCol<pTab->nCol; iCol++){
122093 testcase( mask!=0xffffffff && iCol==31 );
122094 testcase( mask!=0xffffffff && iCol==32 );
122095 if( mask==0xffffffff || (iCol<=31 && (mask & MASKBIT32(iCol)(((unsigned int)1)<<(iCol)))!=0) ){
122096 int kk = sqlite3TableColumnToStorage(pTab, iCol);
122097 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, iCol, iOld+kk+1);
122098 }
122099 }
122100
122101 /* Invoke BEFORE DELETE trigger programs. */
122102 addrStart = sqlite3VdbeCurrentAddr(v);
122103 sqlite3CodeRowTrigger(pParse, pTrigger,
122104 TK_DELETE128, 0, TRIGGER_BEFORE1, pTab, iOld, onconf, iLabel
122105 );
122106
122107 /* If any BEFORE triggers were coded, then seek the cursor to the
122108 ** row to be deleted again. It may be that the BEFORE triggers moved
122109 ** the cursor or already deleted the row that the cursor was
122110 ** pointing to.
122111 **
122112 ** Also disable the iIdxNoSeek optimization since the BEFORE trigger
122113 ** may have moved that cursor.
122114 */
122115 if( addrStart<sqlite3VdbeCurrentAddr(v) ){
122116 sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
122117 VdbeCoverageIf(v, opSeek==OP_NotExists);
122118 VdbeCoverageIf(v, opSeek==OP_NotFound);
122119 testcase( iIdxNoSeek>=0 );
122120 iIdxNoSeek = -1;
122121 }
122122
122123 /* Do FK processing. This call checks that any FK constraints that
122124 ** refer to this table (i.e. constraints attached to other tables)
122125 ** are not violated by deleting this row. */
122126 sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
122127 }
122128
122129 /* Delete the index and table entries. Skip this step if pTab is really
122130 ** a view (in which case the only effect of the DELETE statement is to
122131 ** fire the INSTEAD OF triggers).
122132 **
122133 ** If variable 'count' is non-zero, then this OP_Delete instruction should
122134 ** invoke the update-hook. The pre-update-hook, on the other hand should
122135 ** be invoked unless table pTab is a system table. The difference is that
122136 ** the update-hook is not invoked for rows removed by REPLACE, but the
122137 ** pre-update-hook is.
122138 */
122139 if( !IsView(pTab)((pTab)->eTabType==2) ){
122140 u8 p5 = 0;
122141 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
122142 sqlite3VdbeAddOp2(v, OP_Delete130, iDataCur, (count?OPFLAG_NCHANGE0x01:0));
122143 if( pParse->nested==0 || 0==sqlite3_stricmp(pTab->zName, "sqlite_stat1") ){
122144 sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE(-5));
122145 }
122146 if( eMode!=ONEPASS_OFF0 ){
122147 sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE0x04);
122148 }
122149 if( iIdxNoSeek>=0 && iIdxNoSeek!=iDataCur ){
122150 sqlite3VdbeAddOp1(v, OP_Delete130, iIdxNoSeek);
122151 }
122152 if( eMode==ONEPASS_MULTI2 ) p5 |= OPFLAG_SAVEPOSITION0x02;
122153 sqlite3VdbeChangeP5(v, p5);
122154 }
122155
122156 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
122157 ** handle rows (possibly in other tables) that refer via a foreign key
122158 ** to the row just deleted. */
122159 sqlite3FkActions(pParse, pTab, 0, iOld, 0, 0);
122160
122161 /* Invoke AFTER DELETE trigger programs. */
122162 sqlite3CodeRowTrigger(pParse, pTrigger,
122163 TK_DELETE128, 0, TRIGGER_AFTER2, pTab, iOld, onconf, iLabel
122164 );
122165
122166 /* Jump here if the row had already been deleted before any BEFORE
122167 ** trigger programs were invoked. Or if a trigger program throws a
122168 ** RAISE(IGNORE) exception. */
122169 sqlite3VdbeResolveLabel(v, iLabel);
122170 VdbeModuleComment((v, "END: GenRowDel()"));
122171}
122172
122173/*
122174** This routine generates VDBE code that causes the deletion of all
122175** index entries associated with a single row of a single table, pTab
122176**
122177** Preconditions:
122178**
122179** 1. A read/write cursor "iDataCur" must be open on the canonical storage
122180** btree for the table pTab. (This will be either the table itself
122181** for rowid tables or to the primary key index for WITHOUT ROWID
122182** tables.)
122183**
122184** 2. Read/write cursors for all indices of pTab must be open as
122185** cursor number iIdxCur+i for the i-th index. (The pTab->pIndex
122186** index is the 0-th index.)
122187**
122188** 3. The "iDataCur" cursor must be already be positioned on the row
122189** that is to be deleted.
122190*/
122191SQLITE_PRIVATEstatic void sqlite3GenerateRowIndexDelete(
122192 Parse *pParse, /* Parsing and code generating context */
122193 Table *pTab, /* Table containing the row to be deleted */
122194 int iDataCur, /* Cursor of table holding data. */
122195 int iIdxCur, /* First index cursor */
122196 int *aRegIdx, /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
122197 int iIdxNoSeek /* Do not delete from this cursor */
122198){
122199 int i; /* Index loop counter */
122200 int r1 = -1; /* Register holding an index key */
122201 int iPartIdxLabel; /* Jump destination for skipping partial index entries */
122202 Index *pIdx; /* Current index */
122203 Index *pPrior = 0; /* Prior index */
122204 Vdbe *v; /* The prepared statement under construction */
122205 Index *pPk; /* PRIMARY KEY index, or NULL for rowid tables */
122206
122207 v = pParse->pVdbe;
122208 pPk = HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ? 0 : sqlite3PrimaryKeyIndex(pTab);
122209 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
122210 assert( iIdxCur+i!=iDataCur || pPk==pIdx )((void) (0));
122211 if( aRegIdx!=0 && aRegIdx[i]==0 ) continue;
122212 if( pIdx==pPk ) continue;
122213 if( iIdxCur+i==iIdxNoSeek ) continue;
122214 VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
122215 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1,
122216 &iPartIdxLabel, pPrior, r1);
122217 sqlite3VdbeAddOp3(v, OP_IdxDelete140, iIdxCur+i, r1,
122218 pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
122219 sqlite3VdbeChangeP5(v, 1); /* Cause IdxDelete to error if no entry found */
122220 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
122221 pPrior = pIdx;
122222 }
122223}
122224
122225/*
122226** Generate code that will assemble an index key and stores it in register
122227** regOut. The key with be for index pIdx which is an index on pTab.
122228** iCur is the index of a cursor open on the pTab table and pointing to
122229** the entry that needs indexing. If pTab is a WITHOUT ROWID table, then
122230** iCur must be the cursor of the PRIMARY KEY index.
122231**
122232** Return a register number which is the first in a block of
122233** registers that holds the elements of the index key. The
122234** block of registers has already been deallocated by the time
122235** this routine returns.
122236**
122237** If *piPartIdxLabel is not NULL, fill it in with a label and jump
122238** to that label if pIdx is a partial index that should be skipped.
122239** The label should be resolved using sqlite3ResolvePartIdxLabel().
122240** A partial index should be skipped if its WHERE clause evaluates
122241** to false or null. If pIdx is not a partial index, *piPartIdxLabel
122242** will be set to zero which is an empty label that is ignored by
122243** sqlite3ResolvePartIdxLabel().
122244**
122245** The pPrior and regPrior parameters are used to implement a cache to
122246** avoid unnecessary register loads. If pPrior is not NULL, then it is
122247** a pointer to a different index for which an index key has just been
122248** computed into register regPrior. If the current pIdx index is generating
122249** its key into the same sequence of registers and if pPrior and pIdx share
122250** a column in common, then the register corresponding to that column already
122251** holds the correct value and the loading of that register is skipped.
122252** This optimization is helpful when doing a DELETE or an INTEGRITY_CHECK
122253** on a table with multiple indices, and especially with the ROWID or
122254** PRIMARY KEY columns of the index.
122255*/
122256SQLITE_PRIVATEstatic int sqlite3GenerateIndexKey(
122257 Parse *pParse, /* Parsing context */
122258 Index *pIdx, /* The index for which to generate a key */
122259 int iDataCur, /* Cursor number from which to take column data */
122260 int regOut, /* Put the new key into this register if not 0 */
122261 int prefixOnly, /* Compute only a unique prefix of the key */
122262 int *piPartIdxLabel, /* OUT: Jump to this label to skip partial index */
122263 Index *pPrior, /* Previously generated index key */
122264 int regPrior /* Register holding previous generated key */
122265){
122266 Vdbe *v = pParse->pVdbe;
122267 int j;
122268 int regBase;
122269 int nCol;
122270
122271 if( piPartIdxLabel ){
122272 if( pIdx->pPartIdxWhere ){
122273 *piPartIdxLabel = sqlite3VdbeMakeLabel(pParse);
122274 pParse->iSelfTab = iDataCur + 1;
122275 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
122276 SQLITE_JUMPIFNULL0x10);
122277 pParse->iSelfTab = 0;
122278 pPrior = 0; /* Ticket a9efb42811fa41ee 2019-11-02;
122279 ** pPartIdxWhere may have corrupted regPrior registers */
122280 }else{
122281 *piPartIdxLabel = 0;
122282 }
122283 }
122284 nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
122285 regBase = sqlite3GetTempRange(pParse, nCol);
122286 if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
122287 for(j=0; j<nCol; j++){
122288 if( pPrior
122289 && pPrior->aiColumn[j]==pIdx->aiColumn[j]
122290 && pPrior->aiColumn[j]!=XN_EXPR(-2)
122291 ){
122292 /* This column was already computed by the previous index */
122293 continue;
122294 }
122295 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iDataCur, j, regBase+j);
122296 if( pIdx->aiColumn[j]>=0 ){
122297 /* If the column affinity is REAL but the number is an integer, then it
122298 ** might be stored in the table as an integer (using a compact
122299 ** representation) then converted to REAL by an OP_RealAffinity opcode.
122300 ** But we are getting ready to store this value back into an index, where
122301 ** it should be converted by to INTEGER again. So omit the
122302 ** OP_RealAffinity opcode if it is present */
122303 sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity87);
122304 }
122305 }
122306 if( regOut ){
122307 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regBase, nCol, regOut);
122308 }
122309 sqlite3ReleaseTempRange(pParse, regBase, nCol);
122310 return regBase;
122311}
122312
122313/*
122314** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
122315** because it was a partial index, then this routine should be called to
122316** resolve that label.
122317*/
122318SQLITE_PRIVATEstatic void sqlite3ResolvePartIdxLabel(Parse *pParse, int iLabel){
122319 if( iLabel ){
122320 sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
122321 }
122322}
122323
122324/************** End of delete.c **********************************************/
122325/************** Begin file func.c ********************************************/
122326/*
122327** 2002 February 23
122328**
122329** The author disclaims copyright to this source code. In place of
122330** a legal notice, here is a blessing:
122331**
122332** May you do good and not evil.
122333** May you find forgiveness for yourself and forgive others.
122334** May you share freely, never taking more than you give.
122335**
122336*************************************************************************
122337** This file contains the C-language implementations for many of the SQL
122338** functions of SQLite. (Some function, and in particular the date and
122339** time functions, are implemented separately.)
122340*/
122341/* #include "sqliteInt.h" */
122342/* #include <stdlib.h> */
122343/* #include <assert.h> */
122344#ifndef SQLITE_OMIT_FLOATING_POINT
122345/* #include <math.h> */
122346#endif
122347/* #include "vdbeInt.h" */
122348
122349/*
122350** Return the collating function associated with a function.
122351*/
122352static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
122353 VdbeOp *pOp;
122354 assert( context->pVdbe!=0 )((void) (0));
122355 pOp = &context->pVdbe->aOp[context->iOp-1];
122356 assert( pOp->opcode==OP_CollSeq )((void) (0));
122357 assert( pOp->p4type==P4_COLLSEQ )((void) (0));
122358 return pOp->p4.pColl;
122359}
122360
122361/*
122362** Indicate that the accumulator load should be skipped on this
122363** iteration of the aggregate loop.
122364*/
122365static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
122366 assert( context->isError<=0 )((void) (0));
122367 context->isError = -1;
122368 context->skipFlag = 1;
122369}
122370
122371/*
122372** Implementation of the non-aggregate min() and max() functions
122373*/
122374static void minmaxFunc(
122375 sqlite3_context *context,
122376 int argc,
122377 sqlite3_value **argv
122378){
122379 int i;
122380 int mask; /* 0 for min() or 0xffffffff for max() */
122381 int iBest;
122382 CollSeq *pColl;
122383
122384 assert( argc>1 )((void) (0));
122385 mask = sqlite3_user_data(context)==0 ? 0 : -1;
122386 pColl = sqlite3GetFuncCollSeq(context);
122387 assert( pColl )((void) (0));
122388 assert( mask==-1 || mask==0 )((void) (0));
122389 iBest = 0;
122390 if( sqlite3_value_type(argv[0])==SQLITE_NULL5 ) return;
122391 for(i=1; i<argc; i++){
122392 if( sqlite3_value_type(argv[i])==SQLITE_NULL5 ) return;
122393 if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
122394 testcase( mask==0 );
122395 iBest = i;
122396 }
122397 }
122398 sqlite3_result_value(context, argv[iBest]);
122399}
122400
122401/*
122402** Return the type of the argument.
122403*/
122404static void typeofFunc(
122405 sqlite3_context *context,
122406 int NotUsed,
122407 sqlite3_value **argv
122408){
122409 static const char *azType[] = { "integer", "real", "text", "blob", "null" };
122410 int i = sqlite3_value_type(argv[0]) - 1;
122411 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
122412 assert( i>=0 && i<ArraySize(azType) )((void) (0));
122413 assert( SQLITE_INTEGER==1 )((void) (0));
122414 assert( SQLITE_FLOAT==2 )((void) (0));
122415 assert( SQLITE_TEXT==3 )((void) (0));
122416 assert( SQLITE_BLOB==4 )((void) (0));
122417 assert( SQLITE_NULL==5 )((void) (0));
122418 /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns
122419 ** the datatype code for the initial datatype of the sqlite3_value object
122420 ** V. The returned value is one of SQLITE_INTEGER, SQLITE_FLOAT,
122421 ** SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL. */
122422 sqlite3_result_text(context, azType[i], -1, SQLITE_STATIC((sqlite3_destructor_type)0));
122423}
122424
122425/* subtype(X)
122426**
122427** Return the subtype of X
122428*/
122429static void subtypeFunc(
122430 sqlite3_context *context,
122431 int argc,
122432 sqlite3_value **argv
122433){
122434 UNUSED_PARAMETER(argc)(void)(argc);
122435 sqlite3_result_int(context, sqlite3_value_subtype(argv[0]));
122436}
122437
122438/*
122439** Implementation of the length() function
122440*/
122441static void lengthFunc(
122442 sqlite3_context *context,
122443 int argc,
122444 sqlite3_value **argv
122445){
122446 assert( argc==1 )((void) (0));
122447 UNUSED_PARAMETER(argc)(void)(argc);
122448 switch( sqlite3_value_type(argv[0]) ){
122449 case SQLITE_BLOB4:
122450 case SQLITE_INTEGER1:
122451 case SQLITE_FLOAT2: {
122452 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
122453 break;
122454 }
122455 case SQLITE_TEXT3: {
122456 const unsigned char *z = sqlite3_value_text(argv[0]);
122457 const unsigned char *z0;
122458 unsigned char c;
122459 if( z==0 ) return;
122460 z0 = z;
122461 while( (c = *z)!=0 ){
122462 z++;
122463 if( c>=0xc0 ){
122464 while( (*z & 0xc0)==0x80 ){ z++; z0++; }
122465 }
122466 }
122467 sqlite3_result_int(context, (int)(z-z0));
122468 break;
122469 }
122470 default: {
122471 sqlite3_result_null(context);
122472 break;
122473 }
122474 }
122475}
122476
122477/*
122478** Implementation of the abs() function.
122479**
122480** IMP: R-23979-26855 The abs(X) function returns the absolute value of
122481** the numeric argument X.
122482*/
122483static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
122484 assert( argc==1 )((void) (0));
122485 UNUSED_PARAMETER(argc)(void)(argc);
122486 switch( sqlite3_value_type(argv[0]) ){
122487 case SQLITE_INTEGER1: {
122488 i64 iVal = sqlite3_value_int64(argv[0]);
122489 if( iVal<0 ){
122490 if( iVal==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ){
122491 /* IMP: R-31676-45509 If X is the integer -9223372036854775808
122492 ** then abs(X) throws an integer overflow error since there is no
122493 ** equivalent positive 64-bit two complement value. */
122494 sqlite3_result_error(context, "integer overflow", -1);
122495 return;
122496 }
122497 iVal = -iVal;
122498 }
122499 sqlite3_result_int64(context, iVal);
122500 break;
122501 }
122502 case SQLITE_NULL5: {
122503 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
122504 sqlite3_result_null(context);
122505 break;
122506 }
122507 default: {
122508 /* Because sqlite3_value_double() returns 0.0 if the argument is not
122509 ** something that can be converted into a number, we have:
122510 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
122511 ** that cannot be converted to a numeric value.
122512 */
122513 double rVal = sqlite3_value_double(argv[0]);
122514 if( rVal<0 ) rVal = -rVal;
122515 sqlite3_result_double(context, rVal);
122516 break;
122517 }
122518 }
122519}
122520
122521/*
122522** Implementation of the instr() function.
122523**
122524** instr(haystack,needle) finds the first occurrence of needle
122525** in haystack and returns the number of previous characters plus 1,
122526** or 0 if needle does not occur within haystack.
122527**
122528** If both haystack and needle are BLOBs, then the result is one more than
122529** the number of bytes in haystack prior to the first occurrence of needle,
122530** or 0 if needle never occurs in haystack.
122531*/
122532static void instrFunc(
122533 sqlite3_context *context,
122534 int argc,
122535 sqlite3_value **argv
122536){
122537 const unsigned char *zHaystack;
122538 const unsigned char *zNeedle;
122539 int nHaystack;
122540 int nNeedle;
122541 int typeHaystack, typeNeedle;
122542 int N = 1;
122543 int isText;
122544 unsigned char firstChar;
122545 sqlite3_value *pC1 = 0;
122546 sqlite3_value *pC2 = 0;
122547
122548 UNUSED_PARAMETER(argc)(void)(argc);
122549 typeHaystack = sqlite3_value_type(argv[0]);
122550 typeNeedle = sqlite3_value_type(argv[1]);
122551 if( typeHaystack==SQLITE_NULL5 || typeNeedle==SQLITE_NULL5 ) return;
122552 nHaystack = sqlite3_value_bytes(argv[0]);
122553 nNeedle = sqlite3_value_bytes(argv[1]);
122554 if( nNeedle>0 ){
122555 if( typeHaystack==SQLITE_BLOB4 && typeNeedle==SQLITE_BLOB4 ){
122556 zHaystack = sqlite3_value_blob(argv[0]);
122557 zNeedle = sqlite3_value_blob(argv[1]);
122558 isText = 0;
122559 }else if( typeHaystack!=SQLITE_BLOB4 && typeNeedle!=SQLITE_BLOB4 ){
122560 zHaystack = sqlite3_value_text(argv[0]);
122561 zNeedle = sqlite3_value_text(argv[1]);
122562 isText = 1;
122563 }else{
122564 pC1 = sqlite3_value_dup(argv[0]);
122565 zHaystack = sqlite3_value_text(pC1);
122566 if( zHaystack==0 ) goto endInstrOOM;
122567 nHaystack = sqlite3_value_bytes(pC1);
122568 pC2 = sqlite3_value_dup(argv[1]);
122569 zNeedle = sqlite3_value_text(pC2);
122570 if( zNeedle==0 ) goto endInstrOOM;
122571 nNeedle = sqlite3_value_bytes(pC2);
122572 isText = 1;
122573 }
122574 if( zNeedle==0 || (nHaystack && zHaystack==0) ) goto endInstrOOM;
122575 firstChar = zNeedle[0];
122576 while( nNeedle<=nHaystack
122577 && (zHaystack[0]!=firstChar || memcmp(zHaystack, zNeedle, nNeedle)!=0)
122578 ){
122579 N++;
122580 do{
122581 nHaystack--;
122582 zHaystack++;
122583 }while( isText && (zHaystack[0]&0xc0)==0x80 );
122584 }
122585 if( nNeedle>nHaystack ) N = 0;
122586 }
122587 sqlite3_result_int(context, N);
122588endInstr:
122589 sqlite3_value_free(pC1);
122590 sqlite3_value_free(pC2);
122591 return;
122592endInstrOOM:
122593 sqlite3_result_error_nomem(context);
122594 goto endInstr;
122595}
122596
122597/*
122598** Implementation of the printf() (a.k.a. format()) SQL function.
122599*/
122600static void printfFunc(
122601 sqlite3_context *context,
122602 int argc,
122603 sqlite3_value **argv
122604){
122605 PrintfArguments x;
122606 StrAccum str;
122607 const char *zFormat;
122608 int n;
122609 sqlite3 *db = sqlite3_context_db_handle(context);
122610
122611 if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){
122612 x.nArg = argc-1;
122613 x.nUsed = 0;
122614 x.apArg = argv+1;
122615 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH0]);
122616 str.printfFlags = SQLITE_PRINTF_SQLFUNC0x02;
122617 sqlite3_str_appendf(&str, zFormat, &x);
122618 n = str.nChar;
122619 sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n,
122620 SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
122621 }
122622}
122623
122624/*
122625** Implementation of the substr() function.
122626**
122627** substr(x,p1,p2) returns p2 characters of x[] beginning with p1.
122628** p1 is 1-indexed. So substr(x,1,1) returns the first character
122629** of x. If x is text, then we actually count UTF-8 characters.
122630** If x is a blob, then we count bytes.
122631**
122632** If p1 is negative, then we begin abs(p1) from the end of x[].
122633**
122634** If p2 is negative, return the p2 characters preceding p1.
122635*/
122636static void substrFunc(
122637 sqlite3_context *context,
122638 int argc,
122639 sqlite3_value **argv
122640){
122641 const unsigned char *z;
122642 const unsigned char *z2;
122643 int len;
122644 int p0type;
122645 i64 p1, p2;
122646 int negP2 = 0;
122647
122648 assert( argc==3 || argc==2 )((void) (0));
122649 if( sqlite3_value_type(argv[1])==SQLITE_NULL5
122650 || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL5)
122651 ){
122652 return;
122653 }
122654 p0type = sqlite3_value_type(argv[0]);
122655 p1 = sqlite3_value_int(argv[1]);
122656 if( p0type==SQLITE_BLOB4 ){
122657 len = sqlite3_value_bytes(argv[0]);
122658 z = sqlite3_value_blob(argv[0]);
122659 if( z==0 ) return;
122660 assert( len==sqlite3_value_bytes(argv[0]) )((void) (0));
122661 }else{
122662 z = sqlite3_value_text(argv[0]);
122663 if( z==0 ) return;
122664 len = 0;
122665 if( p1<0 ){
122666 for(z2=z; *z2; len++){
122667 SQLITE_SKIP_UTF8(z2){ if( (*(z2++))>=0xc0 ){ while( (*z2 & 0xc0)==0x80 ){ z2
++; } } }
;
122668 }
122669 }
122670 }
122671#ifdef SQLITE_SUBSTR_COMPATIBILITY1
122672 /* If SUBSTR_COMPATIBILITY is defined then substr(X,0,N) work the same as
122673 ** as substr(X,1,N) - it returns the first N characters of X. This
122674 ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8]
122675 ** from 2009-02-02 for compatibility of applications that exploited the
122676 ** old buggy behavior. */
122677 if( p1==0 ) p1 = 1; /* <rdar://problem/6778339> */
122678#endif
122679 if( argc==3 ){
122680 p2 = sqlite3_value_int(argv[2]);
122681 if( p2<0 ){
122682 p2 = -p2;
122683 negP2 = 1;
122684 }
122685 }else{
122686 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH0];
122687 }
122688 if( p1<0 ){
122689 p1 += len;
122690 if( p1<0 ){
122691 p2 += p1;
122692 if( p2<0 ) p2 = 0;
122693 p1 = 0;
122694 }
122695 }else if( p1>0 ){
122696 p1--;
122697 }else if( p2>0 ){
122698 p2--;
122699 }
122700 if( negP2 ){
122701 p1 -= p2;
122702 if( p1<0 ){
122703 p2 += p1;
122704 p1 = 0;
122705 }
122706 }
122707 assert( p1>=0 && p2>=0 )((void) (0));
122708 if( p0type!=SQLITE_BLOB4 ){
122709 while( *z && p1 ){
122710 SQLITE_SKIP_UTF8(z){ if( (*(z++))>=0xc0 ){ while( (*z & 0xc0)==0x80 ){ z++
; } } }
;
122711 p1--;
122712 }
122713 for(z2=z; *z2 && p2; p2--){
122714 SQLITE_SKIP_UTF8(z2){ if( (*(z2++))>=0xc0 ){ while( (*z2 & 0xc0)==0x80 ){ z2
++; } } }
;
122715 }
122716 sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT((sqlite3_destructor_type)-1),
122717 SQLITE_UTF81);
122718 }else{
122719 if( p1+p2>len ){
122720 p2 = len-p1;
122721 if( p2<0 ) p2 = 0;
122722 }
122723 sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
122724 }
122725}
122726
122727/*
122728** Implementation of the round() function
122729*/
122730#ifndef SQLITE_OMIT_FLOATING_POINT
122731static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
122732 int n = 0;
122733 double r;
122734 char *zBuf;
122735 assert( argc==1 || argc==2 )((void) (0));
122736 if( argc==2 ){
122737 if( SQLITE_NULL5==sqlite3_value_type(argv[1]) ) return;
122738 n = sqlite3_value_int(argv[1]);
122739 if( n>30 ) n = 30;
122740 if( n<0 ) n = 0;
122741 }
122742 if( sqlite3_value_type(argv[0])==SQLITE_NULL5 ) return;
122743 r = sqlite3_value_double(argv[0]);
122744 /* If Y==0 and X will fit in a 64-bit int,
122745 ** handle the rounding directly,
122746 ** otherwise use printf.
122747 */
122748 if( r<-4503599627370496.0 || r>+4503599627370496.0 ){
122749 /* The value has no fractional part so there is nothing to round */
122750 }else if( n==0 ){
122751 r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5)));
122752 }else{
122753 zBuf = sqlite3_mprintf("%.*f",n,r);
122754 if( zBuf==0 ){
122755 sqlite3_result_error_nomem(context);
122756 return;
122757 }
122758 sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF81);
122759 sqlite3_free(zBuf);
122760 }
122761 sqlite3_result_double(context, r);
122762}
122763#endif
122764
122765/*
122766** Allocate nByte bytes of space using sqlite3Malloc(). If the
122767** allocation fails, call sqlite3_result_error_nomem() to notify
122768** the database handle that malloc() has failed and return NULL.
122769** If nByte is larger than the maximum string or blob length, then
122770** raise an SQLITE_TOOBIG exception and return NULL.
122771*/
122772static void *contextMalloc(sqlite3_context *context, i64 nByte){
122773 char *z;
122774 sqlite3 *db = sqlite3_context_db_handle(context);
122775 assert( nByte>0 )((void) (0));
122776 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
122777 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
122778 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH0] ){
122779 sqlite3_result_error_toobig(context);
122780 z = 0;
122781 }else{
122782 z = sqlite3Malloc(nByte);
122783 if( !z ){
122784 sqlite3_result_error_nomem(context);
122785 }
122786 }
122787 return z;
122788}
122789
122790/*
122791** Implementation of the upper() and lower() SQL functions.
122792*/
122793static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
122794 char *z1;
122795 const char *z2;
122796 int i, n;
122797 UNUSED_PARAMETER(argc)(void)(argc);
122798 z2 = (char*)sqlite3_value_text(argv[0]);
122799 n = sqlite3_value_bytes(argv[0]);
122800 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
122801 assert( z2==(char*)sqlite3_value_text(argv[0]) )((void) (0));
122802 if( z2 ){
122803 z1 = contextMalloc(context, ((i64)n)+1);
122804 if( z1 ){
122805 for(i=0; i<n; i++){
122806 z1[i] = (char)sqlite3Toupper(z2[i])((z2[i])&~(sqlite3CtypeMap[(unsigned char)(z2[i])]&0x20
))
;
122807 }
122808 sqlite3_result_text(context, z1, n, sqlite3_free);
122809 }
122810 }
122811}
122812static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
122813 char *z1;
122814 const char *z2;
122815 int i, n;
122816 UNUSED_PARAMETER(argc)(void)(argc);
122817 z2 = (char*)sqlite3_value_text(argv[0]);
122818 n = sqlite3_value_bytes(argv[0]);
122819 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
122820 assert( z2==(char*)sqlite3_value_text(argv[0]) )((void) (0));
122821 if( z2 ){
122822 z1 = contextMalloc(context, ((i64)n)+1);
122823 if( z1 ){
122824 for(i=0; i<n; i++){
122825 z1[i] = sqlite3Tolower(z2[i])(sqlite3UpperToLower[(unsigned char)(z2[i])]);
122826 }
122827 sqlite3_result_text(context, z1, n, sqlite3_free);
122828 }
122829 }
122830}
122831
122832/*
122833** Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented
122834** as VDBE code so that unused argument values do not have to be computed.
122835** However, we still need some kind of function implementation for this
122836** routines in the function table. The noopFunc macro provides this.
122837** noopFunc will never be called so it doesn't matter what the implementation
122838** is. We might as well use the "version()" function as a substitute.
122839*/
122840#define noopFuncversionFunc versionFunc /* Substitute function - never called */
122841
122842/*
122843** Implementation of random(). Return a random integer.
122844*/
122845static void randomFunc(
122846 sqlite3_context *context,
122847 int NotUsed,
122848 sqlite3_value **NotUsed2
122849){
122850 sqlite_int64 r;
122851 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
122852 sqlite3_randomness(sizeof(r), &r);
122853 if( r<0 ){
122854 /* We need to prevent a random number of 0x8000000000000000
122855 ** (or -9223372036854775808) since when you do abs() of that
122856 ** number of you get the same value back again. To do this
122857 ** in a way that is testable, mask the sign bit off of negative
122858 ** values, resulting in a positive value. Then take the
122859 ** 2s complement of that positive value. The end result can
122860 ** therefore be no less than -9223372036854775807.
122861 */
122862 r = -(r & LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)));
122863 }
122864 sqlite3_result_int64(context, r);
122865}
122866
122867/*
122868** Implementation of randomblob(N). Return a random blob
122869** that is N bytes long.
122870*/
122871static void randomBlob(
122872 sqlite3_context *context,
122873 int argc,
122874 sqlite3_value **argv
122875){
122876 sqlite3_int64 n;
122877 unsigned char *p;
122878 assert( argc==1 )((void) (0));
122879 UNUSED_PARAMETER(argc)(void)(argc);
122880 n = sqlite3_value_int64(argv[0]);
122881 if( n<1 ){
122882 n = 1;
122883 }
122884 p = contextMalloc(context, n);
122885 if( p ){
122886 sqlite3_randomness(n, p);
122887 sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
122888 }
122889}
122890
122891/*
122892** Implementation of the last_insert_rowid() SQL function. The return
122893** value is the same as the sqlite3_last_insert_rowid() API function.
122894*/
122895static void last_insert_rowid(
122896 sqlite3_context *context,
122897 int NotUsed,
122898 sqlite3_value **NotUsed2
122899){
122900 sqlite3 *db = sqlite3_context_db_handle(context);
122901 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
122902 /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
122903 ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface
122904 ** function. */
122905 sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
122906}
122907
122908/*
122909** Implementation of the changes() SQL function.
122910**
122911** IMP: R-32760-32347 The changes() SQL function is a wrapper
122912** around the sqlite3_changes64() C/C++ function and hence follows the
122913** same rules for counting changes.
122914*/
122915static void changes(
122916 sqlite3_context *context,
122917 int NotUsed,
122918 sqlite3_value **NotUsed2
122919){
122920 sqlite3 *db = sqlite3_context_db_handle(context);
122921 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
122922 sqlite3_result_int64(context, sqlite3_changes64(db));
122923}
122924
122925/*
122926** Implementation of the total_changes() SQL function. The return value is
122927** the same as the sqlite3_total_changes64() API function.
122928*/
122929static void total_changes(
122930 sqlite3_context *context,
122931 int NotUsed,
122932 sqlite3_value **NotUsed2
122933){
122934 sqlite3 *db = sqlite3_context_db_handle(context);
122935 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
122936 /* IMP: R-11217-42568 This function is a wrapper around the
122937 ** sqlite3_total_changes64() C/C++ interface. */
122938 sqlite3_result_int64(context, sqlite3_total_changes64(db));
122939}
122940
122941/*
122942** A structure defining how to do GLOB-style comparisons.
122943*/
122944struct compareInfo {
122945 u8 matchAll; /* "*" or "%" */
122946 u8 matchOne; /* "?" or "_" */
122947 u8 matchSet; /* "[" or 0 */
122948 u8 noCase; /* true to ignore case differences */
122949};
122950
122951/*
122952** For LIKE and GLOB matching on EBCDIC machines, assume that every
122953** character is exactly one byte in size. Also, provde the Utf8Read()
122954** macro for fast reading of the next character in the common case where
122955** the next character is ASCII.
122956*/
122957#if defined(SQLITE_EBCDIC)
122958# define sqlite3Utf8Read(A) (*((*A)++))
122959# define Utf8Read(A)(A[0]<0x80?*(A++):sqlite3Utf8Read(&A)) (*(A++))
122960#else
122961# define Utf8Read(A)(A[0]<0x80?*(A++):sqlite3Utf8Read(&A)) (A[0]<0x80?*(A++):sqlite3Utf8Read(&A))
122962#endif
122963
122964static const struct compareInfo globInfo = { '*', '?', '[', 0 };
122965/* The correct SQL-92 behavior is for the LIKE operator to ignore
122966** case. Thus 'a' LIKE 'A' would be true. */
122967static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 };
122968/* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
122969** is case sensitive causing 'a' LIKE 'A' to be false */
122970static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 };
122971
122972/*
122973** Possible error returns from patternMatch()
122974*/
122975#define SQLITE_MATCH0 0
122976#define SQLITE_NOMATCH1 1
122977#define SQLITE_NOWILDCARDMATCH2 2
122978
122979/*
122980** Compare two UTF-8 strings for equality where the first string is
122981** a GLOB or LIKE expression. Return values:
122982**
122983** SQLITE_MATCH: Match
122984** SQLITE_NOMATCH: No match
122985** SQLITE_NOWILDCARDMATCH: No match in spite of having * or % wildcards.
122986**
122987** Globbing rules:
122988**
122989** '*' Matches any sequence of zero or more characters.
122990**
122991** '?' Matches exactly one character.
122992**
122993** [...] Matches one character from the enclosed list of
122994** characters.
122995**
122996** [^...] Matches one character not in the enclosed list.
122997**
122998** With the [...] and [^...] matching, a ']' character can be included
122999** in the list by making it the first character after '[' or '^'. A
123000** range of characters can be specified using '-'. Example:
123001** "[a-z]" matches any single lower-case letter. To match a '-', make
123002** it the last character in the list.
123003**
123004** Like matching rules:
123005**
123006** '%' Matches any sequence of zero or more characters
123007**
123008*** '_' Matches any one character
123009**
123010** Ec Where E is the "esc" character and c is any other
123011** character, including '%', '_', and esc, match exactly c.
123012**
123013** The comments within this routine usually assume glob matching.
123014**
123015** This routine is usually quick, but can be N**2 in the worst case.
123016*/
123017static int patternCompare(
123018 const u8 *zPattern, /* The glob pattern */
123019 const u8 *zString, /* The string to compare against the glob */
123020 const struct compareInfo *pInfo, /* Information about how to do the compare */
123021 u32 matchOther /* The escape char (LIKE) or '[' (GLOB) */
123022){
123023 u32 c, c2; /* Next pattern and input string chars */
123024 u32 matchOne = pInfo->matchOne; /* "?" or "_" */
123025 u32 matchAll = pInfo->matchAll; /* "*" or "%" */
123026 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */
123027 const u8 *zEscaped = 0; /* One past the last escaped input char */
123028
123029 while( (c = Utf8Read(zPattern)(zPattern[0]<0x80?*(zPattern++):sqlite3Utf8Read(&zPattern
))
)!=0 ){
123030 if( c==matchAll ){ /* Match "*" */
123031 /* Skip over multiple "*" characters in the pattern. If there
123032 ** are also "?" characters, skip those as well, but consume a
123033 ** single character of the input string for each "?" skipped */
123034 while( (c=Utf8Read(zPattern)(zPattern[0]<0x80?*(zPattern++):sqlite3Utf8Read(&zPattern
))
) == matchAll
123035 || (c == matchOne && matchOne!=0) ){
123036 if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
123037 return SQLITE_NOWILDCARDMATCH2;
123038 }
123039 }
123040 if( c==0 ){
123041 return SQLITE_MATCH0; /* "*" at the end of the pattern matches */
123042 }else if( c==matchOther ){
123043 if( pInfo->matchSet==0 ){
123044 c = sqlite3Utf8Read(&zPattern);
123045 if( c==0 ) return SQLITE_NOWILDCARDMATCH2;
123046 }else{
123047 /* "[...]" immediately follows the "*". We have to do a slow
123048 ** recursive search in this case, but it is an unusual case. */
123049 assert( matchOther<0x80 )((void) (0)); /* '[' is a single-byte character */
123050 while( *zString ){
123051 int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther);
123052 if( bMatch!=SQLITE_NOMATCH1 ) return bMatch;
123053 SQLITE_SKIP_UTF8(zString){ if( (*(zString++))>=0xc0 ){ while( (*zString & 0xc0)
==0x80 ){ zString++; } } }
;
123054 }
123055 return SQLITE_NOWILDCARDMATCH2;
123056 }
123057 }
123058
123059 /* At this point variable c contains the first character of the
123060 ** pattern string past the "*". Search in the input string for the
123061 ** first matching character and recursively continue the match from
123062 ** that point.
123063 **
123064 ** For a case-insensitive search, set variable cx to be the same as
123065 ** c but in the other case and search the input string for either
123066 ** c or cx.
123067 */
123068 if( c<=0x80 ){
123069 char zStop[3];
123070 int bMatch;
123071 if( noCase ){
123072 zStop[0] = sqlite3Toupper(c)((c)&~(sqlite3CtypeMap[(unsigned char)(c)]&0x20));
123073 zStop[1] = sqlite3Tolower(c)(sqlite3UpperToLower[(unsigned char)(c)]);
123074 zStop[2] = 0;
123075 }else{
123076 zStop[0] = c;
123077 zStop[1] = 0;
123078 }
123079 while(1){
123080 zString += strcspn((const char*)zString, zStop);
123081 if( zString[0]==0 ) break;
123082 zString++;
123083 bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
123084 if( bMatch!=SQLITE_NOMATCH1 ) return bMatch;
123085 }
123086 }else{
123087 int bMatch;
123088 while( (c2 = Utf8Read(zString)(zString[0]<0x80?*(zString++):sqlite3Utf8Read(&zString
))
)!=0 ){
123089 if( c2!=c ) continue;
123090 bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
123091 if( bMatch!=SQLITE_NOMATCH1 ) return bMatch;
123092 }
123093 }
123094 return SQLITE_NOWILDCARDMATCH2;
123095 }
123096 if( c==matchOther ){
123097 if( pInfo->matchSet==0 ){
123098 c = sqlite3Utf8Read(&zPattern);
123099 if( c==0 ) return SQLITE_NOMATCH1;
123100 zEscaped = zPattern;
123101 }else{
123102 u32 prior_c = 0;
123103 int seen = 0;
123104 int invert = 0;
123105 c = sqlite3Utf8Read(&zString);
123106 if( c==0 ) return SQLITE_NOMATCH1;
123107 c2 = sqlite3Utf8Read(&zPattern);
123108 if( c2=='^' ){
123109 invert = 1;
123110 c2 = sqlite3Utf8Read(&zPattern);
123111 }
123112 if( c2==']' ){
123113 if( c==']' ) seen = 1;
123114 c2 = sqlite3Utf8Read(&zPattern);
123115 }
123116 while( c2 && c2!=']' ){
123117 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
123118 c2 = sqlite3Utf8Read(&zPattern);
123119 if( c>=prior_c && c<=c2 ) seen = 1;
123120 prior_c = 0;
123121 }else{
123122 if( c==c2 ){
123123 seen = 1;
123124 }
123125 prior_c = c2;
123126 }
123127 c2 = sqlite3Utf8Read(&zPattern);
123128 }
123129 if( c2==0 || (seen ^ invert)==0 ){
123130 return SQLITE_NOMATCH1;
123131 }
123132 continue;
123133 }
123134 }
123135 c2 = Utf8Read(zString)(zString[0]<0x80?*(zString++):sqlite3Utf8Read(&zString
))
;
123136 if( c==c2 ) continue;
123137 if( noCase && sqlite3Tolower(c)(sqlite3UpperToLower[(unsigned char)(c)])==sqlite3Tolower(c2)(sqlite3UpperToLower[(unsigned char)(c2)]) && c<0x80 && c2<0x80 ){
123138 continue;
123139 }
123140 if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue;
123141 return SQLITE_NOMATCH1;
123142 }
123143 return *zString==0 ? SQLITE_MATCH0 : SQLITE_NOMATCH1;
123144}
123145
123146/*
123147** The sqlite3_strglob() interface. Return 0 on a match (like strcmp()) and
123148** non-zero if there is no match.
123149*/
123150SQLITE_API int sqlite3_strglob(const char *zGlobPattern, const char *zString){
123151 return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '[');
123152}
123153
123154/*
123155** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
123156** a miss - like strcmp().
123157*/
123158SQLITE_API int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){
123159 return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc);
123160}
123161
123162/*
123163** Count the number of times that the LIKE operator (or GLOB which is
123164** just a variation of LIKE) gets called. This is used for testing
123165** only.
123166*/
123167#ifdef SQLITE_TEST
123168SQLITE_API int sqlite3_like_count = 0;
123169#endif
123170
123171
123172/*
123173** Implementation of the like() SQL function. This function implements
123174** the build-in LIKE operator. The first argument to the function is the
123175** pattern and the second argument is the string. So, the SQL statements:
123176**
123177** A LIKE B
123178**
123179** is implemented as like(B,A).
123180**
123181** This same function (with a different compareInfo structure) computes
123182** the GLOB operator.
123183*/
123184static void likeFunc(
123185 sqlite3_context *context,
123186 int argc,
123187 sqlite3_value **argv
123188){
123189 const unsigned char *zA, *zB;
123190 u32 escape;
123191 int nPat;
123192 sqlite3 *db = sqlite3_context_db_handle(context);
123193 struct compareInfo *pInfo = sqlite3_user_data(context);
123194 struct compareInfo backupInfo;
123195
123196#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
123197 if( sqlite3_value_type(argv[0])==SQLITE_BLOB4
123198 || sqlite3_value_type(argv[1])==SQLITE_BLOB4
123199 ){
123200#ifdef SQLITE_TEST
123201 sqlite3_like_count++;
123202#endif
123203 sqlite3_result_int(context, 0);
123204 return;
123205 }
123206#endif
123207
123208 /* Limit the length of the LIKE or GLOB pattern to avoid problems
123209 ** of deep recursion and N*N behavior in patternCompare().
123210 */
123211 nPat = sqlite3_value_bytes(argv[0]);
123212 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
123213 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
123214 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH8] ){
123215 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
123216 return;
123217 }
123218 if( argc==3 ){
123219 /* The escape character string must consist of a single UTF-8 character.
123220 ** Otherwise, return an error.
123221 */
123222 const unsigned char *zEsc = sqlite3_value_text(argv[2]);
123223 if( zEsc==0 ) return;
123224 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
123225 sqlite3_result_error(context,
123226 "ESCAPE expression must be a single character", -1);
123227 return;
123228 }
123229 escape = sqlite3Utf8Read(&zEsc);
123230 if( escape==pInfo->matchAll || escape==pInfo->matchOne ){
123231 memcpy(&backupInfo, pInfo, sizeof(backupInfo));
123232 pInfo = &backupInfo;
123233 if( escape==pInfo->matchAll ) pInfo->matchAll = 0;
123234 if( escape==pInfo->matchOne ) pInfo->matchOne = 0;
123235 }
123236 }else{
123237 escape = pInfo->matchSet;
123238 }
123239 zB = sqlite3_value_text(argv[0]);
123240 zA = sqlite3_value_text(argv[1]);
123241 if( zA && zB ){
123242#ifdef SQLITE_TEST
123243 sqlite3_like_count++;
123244#endif
123245 sqlite3_result_int(context,
123246 patternCompare(zB, zA, pInfo, escape)==SQLITE_MATCH0);
123247 }
123248}
123249
123250/*
123251** Implementation of the NULLIF(x,y) function. The result is the first
123252** argument if the arguments are different. The result is NULL if the
123253** arguments are equal to each other.
123254*/
123255static void nullifFunc(
123256 sqlite3_context *context,
123257 int NotUsed,
123258 sqlite3_value **argv
123259){
123260 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
123261 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
123262 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
123263 sqlite3_result_value(context, argv[0]);
123264 }
123265}
123266
123267/*
123268** Implementation of the sqlite_version() function. The result is the version
123269** of the SQLite library that is running.
123270*/
123271static void versionFunc(
123272 sqlite3_context *context,
123273 int NotUsed,
123274 sqlite3_value **NotUsed2
123275){
123276 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
123277 /* IMP: R-48699-48617 This function is an SQL wrapper around the
123278 ** sqlite3_libversion() C-interface. */
123279 sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC((sqlite3_destructor_type)0));
123280}
123281
123282/*
123283** Implementation of the sqlite_source_id() function. The result is a string
123284** that identifies the particular version of the source code used to build
123285** SQLite.
123286*/
123287static void sourceidFunc(
123288 sqlite3_context *context,
123289 int NotUsed,
123290 sqlite3_value **NotUsed2
123291){
123292 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
123293 /* IMP: R-24470-31136 This function is an SQL wrapper around the
123294 ** sqlite3_sourceid() C interface. */
123295 sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC((sqlite3_destructor_type)0));
123296}
123297
123298/*
123299** Implementation of the sqlite_log() function. This is a wrapper around
123300** sqlite3_log(). The return value is NULL. The function exists purely for
123301** its side-effects.
123302*/
123303static void errlogFunc(
123304 sqlite3_context *context,
123305 int argc,
123306 sqlite3_value **argv
123307){
123308 UNUSED_PARAMETER(argc)(void)(argc);
123309 UNUSED_PARAMETER(context)(void)(context);
123310 sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1]));
123311}
123312
123313/*
123314** Implementation of the sqlite_compileoption_used() function.
123315** The result is an integer that identifies if the compiler option
123316** was used to build SQLite.
123317*/
123318#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
123319static void compileoptionusedFunc(
123320 sqlite3_context *context,
123321 int argc,
123322 sqlite3_value **argv
123323){
123324 const char *zOptName;
123325 assert( argc==1 )((void) (0));
123326 UNUSED_PARAMETER(argc)(void)(argc);
123327 /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
123328 ** function is a wrapper around the sqlite3_compileoption_used() C/C++
123329 ** function.
123330 */
123331 if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){
123332 sqlite3_result_int(context, sqlite3_compileoption_used(zOptName));
123333 }
123334}
123335#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
123336
123337/*
123338** Implementation of the sqlite_compileoption_get() function.
123339** The result is a string that identifies the compiler options
123340** used to build SQLite.
123341*/
123342#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
123343static void compileoptiongetFunc(
123344 sqlite3_context *context,
123345 int argc,
123346 sqlite3_value **argv
123347){
123348 int n;
123349 assert( argc==1 )((void) (0));
123350 UNUSED_PARAMETER(argc)(void)(argc);
123351 /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
123352 ** is a wrapper around the sqlite3_compileoption_get() C/C++ function.
123353 */
123354 n = sqlite3_value_int(argv[0]);
123355 sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC((sqlite3_destructor_type)0));
123356}
123357#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
123358
123359/* Array for converting from half-bytes (nybbles) into ASCII hex
123360** digits. */
123361static const char hexdigits[] = {
123362 '0', '1', '2', '3', '4', '5', '6', '7',
123363 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
123364};
123365
123366/*
123367** Append to pStr text that is the SQL literal representation of the
123368** value contained in pValue.
123369*/
123370SQLITE_PRIVATEstatic void sqlite3QuoteValue(StrAccum *pStr, sqlite3_value *pValue){
123371 /* As currently implemented, the string must be initially empty.
123372 ** we might relax this requirement in the future, but that will
123373 ** require enhancements to the implementation. */
123374 assert( pStr!=0 && pStr->nChar==0 )((void) (0));
123375
123376 switch( sqlite3_value_type(pValue) ){
123377 case SQLITE_FLOAT2: {
123378 double r1, r2;
123379 const char *zVal;
123380 r1 = sqlite3_value_double(pValue);
123381 sqlite3_str_appendf(pStr, "%!.15g", r1);
123382 zVal = sqlite3_str_value(pStr);
123383 if( zVal ){
123384 sqlite3AtoF(zVal, &r2, pStr->nChar, SQLITE_UTF81);
123385 if( r1!=r2 ){
123386 sqlite3_str_reset(pStr);
123387 sqlite3_str_appendf(pStr, "%!.20e", r1);
123388 }
123389 }
123390 break;
123391 }
123392 case SQLITE_INTEGER1: {
123393 sqlite3_str_appendf(pStr, "%lld", sqlite3_value_int64(pValue));
123394 break;
123395 }
123396 case SQLITE_BLOB4: {
123397 char const *zBlob = sqlite3_value_blob(pValue);
123398 int nBlob = sqlite3_value_bytes(pValue);
123399 assert( zBlob==sqlite3_value_blob(pValue) )((void) (0)); /* No encoding change */
123400 sqlite3StrAccumEnlarge(pStr, nBlob*2 + 4);
123401 if( pStr->accError==0 ){
123402 char *zText = pStr->zText;
123403 int i;
123404 for(i=0; i<nBlob; i++){
123405 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
123406 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
123407 }
123408 zText[(nBlob*2)+2] = '\'';
123409 zText[(nBlob*2)+3] = '\0';
123410 zText[0] = 'X';
123411 zText[1] = '\'';
123412 pStr->nChar = nBlob*2 + 3;
123413 }
123414 break;
123415 }
123416 case SQLITE_TEXT3: {
123417 const unsigned char *zArg = sqlite3_value_text(pValue);
123418 sqlite3_str_appendf(pStr, "%Q", zArg);
123419 break;
123420 }
123421 default: {
123422 assert( sqlite3_value_type(pValue)==SQLITE_NULL )((void) (0));
123423 sqlite3_str_append(pStr, "NULL", 4);
123424 break;
123425 }
123426 }
123427}
123428
123429/*
123430** Implementation of the QUOTE() function.
123431**
123432** The quote(X) function returns the text of an SQL literal which is the
123433** value of its argument suitable for inclusion into an SQL statement.
123434** Strings are surrounded by single-quotes with escapes on interior quotes
123435** as needed. BLOBs are encoded as hexadecimal literals. Strings with
123436** embedded NUL characters cannot be represented as string literals in SQL
123437** and hence the returned string literal is truncated prior to the first NUL.
123438*/
123439static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
123440 sqlite3_str str;
123441 sqlite3 *db = sqlite3_context_db_handle(context);
123442 assert( argc==1 )((void) (0));
123443 UNUSED_PARAMETER(argc)(void)(argc);
123444 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH0]);
123445 sqlite3QuoteValue(&str,argv[0]);
123446 sqlite3_result_text(context, sqlite3StrAccumFinish(&str), str.nChar,
123447 SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
123448 if( str.accError!=SQLITE_OK0 ){
123449 sqlite3_result_null(context);
123450 sqlite3_result_error_code(context, str.accError);
123451 }
123452}
123453
123454/*
123455** The unicode() function. Return the integer unicode code-point value
123456** for the first character of the input string.
123457*/
123458static void unicodeFunc(
123459 sqlite3_context *context,
123460 int argc,
123461 sqlite3_value **argv
123462){
123463 const unsigned char *z = sqlite3_value_text(argv[0]);
123464 (void)argc;
123465 if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z));
123466}
123467
123468/*
123469** The char() function takes zero or more arguments, each of which is
123470** an integer. It constructs a string where each character of the string
123471** is the unicode character for the corresponding integer argument.
123472*/
123473static void charFunc(
123474 sqlite3_context *context,
123475 int argc,
123476 sqlite3_value **argv
123477){
123478 unsigned char *z, *zOut;
123479 int i;
123480 zOut = z = sqlite3_malloc64( argc*4+1 );
123481 if( z==0 ){
123482 sqlite3_result_error_nomem(context);
123483 return;
123484 }
123485 for(i=0; i<argc; i++){
123486 sqlite3_int64 x;
123487 unsigned c;
123488 x = sqlite3_value_int64(argv[i]);
123489 if( x<0 || x>0x10ffff ) x = 0xfffd;
123490 c = (unsigned)(x & 0x1fffff);
123491 if( c<0x00080 ){
123492 *zOut++ = (u8)(c&0xFF);
123493 }else if( c<0x00800 ){
123494 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);
123495 *zOut++ = 0x80 + (u8)(c & 0x3F);
123496 }else if( c<0x10000 ){
123497 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);
123498 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
123499 *zOut++ = 0x80 + (u8)(c & 0x3F);
123500 }else{
123501 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);
123502 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);
123503 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
123504 *zOut++ = 0x80 + (u8)(c & 0x3F);
123505 } \
123506 }
123507 sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF81);
123508}
123509
123510/*
123511** The hex() function. Interpret the argument as a blob. Return
123512** a hexadecimal rendering as text.
123513*/
123514static void hexFunc(
123515 sqlite3_context *context,
123516 int argc,
123517 sqlite3_value **argv
123518){
123519 int i, n;
123520 const unsigned char *pBlob;
123521 char *zHex, *z;
123522 assert( argc==1 )((void) (0));
123523 UNUSED_PARAMETER(argc)(void)(argc);
123524 pBlob = sqlite3_value_blob(argv[0]);
123525 n = sqlite3_value_bytes(argv[0]);
123526 assert( pBlob==sqlite3_value_blob(argv[0]) )((void) (0)); /* No encoding change */
123527 z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
123528 if( zHex ){
123529 for(i=0; i<n; i++, pBlob++){
123530 unsigned char c = *pBlob;
123531 *(z++) = hexdigits[(c>>4)&0xf];
123532 *(z++) = hexdigits[c&0xf];
123533 }
123534 *z = 0;
123535 sqlite3_result_text(context, zHex, n*2, sqlite3_free);
123536 }
123537}
123538
123539/*
123540** The zeroblob(N) function returns a zero-filled blob of size N bytes.
123541*/
123542static void zeroblobFunc(
123543 sqlite3_context *context,
123544 int argc,
123545 sqlite3_value **argv
123546){
123547 i64 n;
123548 int rc;
123549 assert( argc==1 )((void) (0));
123550 UNUSED_PARAMETER(argc)(void)(argc);
123551 n = sqlite3_value_int64(argv[0]);
123552 if( n<0 ) n = 0;
123553 rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */
123554 if( rc ){
123555 sqlite3_result_error_code(context, rc);
123556 }
123557}
123558
123559/*
123560** The replace() function. Three arguments are all strings: call
123561** them A, B, and C. The result is also a string which is derived
123562** from A by replacing every occurrence of B with C. The match
123563** must be exact. Collating sequences are not used.
123564*/
123565static void replaceFunc(
123566 sqlite3_context *context,
123567 int argc,
123568 sqlite3_value **argv
123569){
123570 const unsigned char *zStr; /* The input string A */
123571 const unsigned char *zPattern; /* The pattern string B */
123572 const unsigned char *zRep; /* The replacement string C */
123573 unsigned char *zOut; /* The output */
123574 int nStr; /* Size of zStr */
123575 int nPattern; /* Size of zPattern */
123576 int nRep; /* Size of zRep */
123577 i64 nOut; /* Maximum size of zOut */
123578 int loopLimit; /* Last zStr[] that might match zPattern[] */
123579 int i, j; /* Loop counters */
123580 unsigned cntExpand; /* Number zOut expansions */
123581 sqlite3 *db = sqlite3_context_db_handle(context);
123582
123583 assert( argc==3 )((void) (0));
123584 UNUSED_PARAMETER(argc)(void)(argc);
123585 zStr = sqlite3_value_text(argv[0]);
123586 if( zStr==0 ) return;
123587 nStr = sqlite3_value_bytes(argv[0]);
123588 assert( zStr==sqlite3_value_text(argv[0]) )((void) (0)); /* No encoding change */
123589 zPattern = sqlite3_value_text(argv[1]);
123590 if( zPattern==0 ){
123591 assert( sqlite3_value_type(argv[1])==SQLITE_NULL((void) (0))
123592 || sqlite3_context_db_handle(context)->mallocFailed )((void) (0));
123593 return;
123594 }
123595 if( zPattern[0]==0 ){
123596 assert( sqlite3_value_type(argv[1])!=SQLITE_NULL )((void) (0));
123597 sqlite3_result_value(context, argv[0]);
123598 return;
123599 }
123600 nPattern = sqlite3_value_bytes(argv[1]);
123601 assert( zPattern==sqlite3_value_text(argv[1]) )((void) (0)); /* No encoding change */
123602 zRep = sqlite3_value_text(argv[2]);
123603 if( zRep==0 ) return;
123604 nRep = sqlite3_value_bytes(argv[2]);
123605 assert( zRep==sqlite3_value_text(argv[2]) )((void) (0));
123606 nOut = nStr + 1;
123607 assert( nOut<SQLITE_MAX_LENGTH )((void) (0));
123608 zOut = contextMalloc(context, (i64)nOut);
123609 if( zOut==0 ){
123610 return;
123611 }
123612 loopLimit = nStr - nPattern;
123613 cntExpand = 0;
123614 for(i=j=0; i<=loopLimit; i++){
123615 if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
123616 zOut[j++] = zStr[i];
123617 }else{
123618 if( nRep>nPattern ){
123619 nOut += nRep - nPattern;
123620 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
123621 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
123622 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH0] ){
123623 sqlite3_result_error_toobig(context);
123624 sqlite3_free(zOut);
123625 return;
123626 }
123627 cntExpand++;
123628 if( (cntExpand&(cntExpand-1))==0 ){
123629 /* Grow the size of the output buffer only on substitutions
123630 ** whose index is a power of two: 1, 2, 4, 8, 16, 32, ... */
123631 u8 *zOld;
123632 zOld = zOut;
123633 zOut = sqlite3Realloc(zOut, (int)nOut + (nOut - nStr - 1));
123634 if( zOut==0 ){
123635 sqlite3_result_error_nomem(context);
123636 sqlite3_free(zOld);
123637 return;
123638 }
123639 }
123640 }
123641 memcpy(&zOut[j], zRep, nRep);
123642 j += nRep;
123643 i += nPattern-1;
123644 }
123645 }
123646 assert( j+nStr-i+1<=nOut )((void) (0));
123647 memcpy(&zOut[j], &zStr[i], nStr-i);
123648 j += nStr - i;
123649 assert( j<=nOut )((void) (0));
123650 zOut[j] = 0;
123651 sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
123652}
123653
123654/*
123655** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
123656** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
123657*/
123658static void trimFunc(
123659 sqlite3_context *context,
123660 int argc,
123661 sqlite3_value **argv
123662){
123663 const unsigned char *zIn; /* Input string */
123664 const unsigned char *zCharSet; /* Set of characters to trim */
123665 unsigned int nIn; /* Number of bytes in input */
123666 int flags; /* 1: trimleft 2: trimright 3: trim */
123667 int i; /* Loop counter */
123668 unsigned int *aLen = 0; /* Length of each character in zCharSet */
123669 unsigned char **azChar = 0; /* Individual characters in zCharSet */
123670 int nChar; /* Number of characters in zCharSet */
123671
123672 if( sqlite3_value_type(argv[0])==SQLITE_NULL5 ){
123673 return;
123674 }
123675 zIn = sqlite3_value_text(argv[0]);
123676 if( zIn==0 ) return;
123677 nIn = (unsigned)sqlite3_value_bytes(argv[0]);
123678 assert( zIn==sqlite3_value_text(argv[0]) )((void) (0));
123679 if( argc==1 ){
123680 static const unsigned lenOne[] = { 1 };
123681 static unsigned char * const azOne[] = { (u8*)" " };
123682 nChar = 1;
123683 aLen = (unsigned*)lenOne;
123684 azChar = (unsigned char **)azOne;
123685 zCharSet = 0;
123686 }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
123687 return;
123688 }else{
123689 const unsigned char *z;
123690 for(z=zCharSet, nChar=0; *z; nChar++){
123691 SQLITE_SKIP_UTF8(z){ if( (*(z++))>=0xc0 ){ while( (*z & 0xc0)==0x80 ){ z++
; } } }
;
123692 }
123693 if( nChar>0 ){
123694 azChar = contextMalloc(context,
123695 ((i64)nChar)*(sizeof(char*)+sizeof(unsigned)));
123696 if( azChar==0 ){
123697 return;
123698 }
123699 aLen = (unsigned*)&azChar[nChar];
123700 for(z=zCharSet, nChar=0; *z; nChar++){
123701 azChar[nChar] = (unsigned char *)z;
123702 SQLITE_SKIP_UTF8(z){ if( (*(z++))>=0xc0 ){ while( (*z & 0xc0)==0x80 ){ z++
; } } }
;
123703 aLen[nChar] = (unsigned)(z - azChar[nChar]);
123704 }
123705 }
123706 }
123707 if( nChar>0 ){
123708 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context))((int)(long int)(sqlite3_user_data(context)));
123709 if( flags & 1 ){
123710 while( nIn>0 ){
123711 unsigned int len = 0;
123712 for(i=0; i<nChar; i++){
123713 len = aLen[i];
123714 if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break;
123715 }
123716 if( i>=nChar ) break;
123717 zIn += len;
123718 nIn -= len;
123719 }
123720 }
123721 if( flags & 2 ){
123722 while( nIn>0 ){
123723 unsigned int len = 0;
123724 for(i=0; i<nChar; i++){
123725 len = aLen[i];
123726 if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
123727 }
123728 if( i>=nChar ) break;
123729 nIn -= len;
123730 }
123731 }
123732 if( zCharSet ){
123733 sqlite3_free(azChar);
123734 }
123735 }
123736 sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
123737}
123738
123739
123740#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION1
123741/*
123742** The "unknown" function is automatically substituted in place of
123743** any unrecognized function name when doing an EXPLAIN or EXPLAIN QUERY PLAN
123744** when the SQLITE_ENABLE_UNKNOWN_FUNCTION compile-time option is used.
123745** When the "sqlite3" command-line shell is built using this functionality,
123746** that allows an EXPLAIN or EXPLAIN QUERY PLAN for complex queries
123747** involving application-defined functions to be examined in a generic
123748** sqlite3 shell.
123749*/
123750static void unknownFunc(
123751 sqlite3_context *context,
123752 int argc,
123753 sqlite3_value **argv
123754){
123755 /* no-op */
123756}
123757#endif /*SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION*/
123758
123759
123760/* IMP: R-25361-16150 This function is omitted from SQLite by default. It
123761** is only available if the SQLITE_SOUNDEX compile-time option is used
123762** when SQLite is built.
123763*/
123764#ifdef SQLITE_SOUNDEX
123765/*
123766** Compute the soundex encoding of a word.
123767**
123768** IMP: R-59782-00072 The soundex(X) function returns a string that is the
123769** soundex encoding of the string X.
123770*/
123771static void soundexFunc(
123772 sqlite3_context *context,
123773 int argc,
123774 sqlite3_value **argv
123775){
123776 char zResult[8];
123777 const u8 *zIn;
123778 int i, j;
123779 static const unsigned char iCode[] = {
123780 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
123781 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
123782 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
123783 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
123784 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
123785 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
123786 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
123787 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
123788 };
123789 assert( argc==1 )((void) (0));
123790 zIn = (u8*)sqlite3_value_text(argv[0]);
123791 if( zIn==0 ) zIn = (u8*)"";
123792 for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i])(sqlite3CtypeMap[(unsigned char)(zIn[i])]&0x02); i++){}
123793 if( zIn[i] ){
123794 u8 prevcode = iCode[zIn[i]&0x7f];
123795 zResult[0] = sqlite3Toupper(zIn[i])((zIn[i])&~(sqlite3CtypeMap[(unsigned char)(zIn[i])]&
0x20))
;
123796 for(j=1; j<4 && zIn[i]; i++){
123797 int code = iCode[zIn[i]&0x7f];
123798 if( code>0 ){
123799 if( code!=prevcode ){
123800 prevcode = code;
123801 zResult[j++] = code + '0';
123802 }
123803 }else{
123804 prevcode = 0;
123805 }
123806 }
123807 while( j<4 ){
123808 zResult[j++] = '0';
123809 }
123810 zResult[j] = 0;
123811 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
123812 }else{
123813 /* IMP: R-64894-50321 The string "?000" is returned if the argument
123814 ** is NULL or contains no ASCII alphabetic characters. */
123815 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC((sqlite3_destructor_type)0));
123816 }
123817}
123818#endif /* SQLITE_SOUNDEX */
123819
123820#ifndef SQLITE_OMIT_LOAD_EXTENSION1
123821/*
123822** A function that loads a shared-library extension then returns NULL.
123823*/
123824static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
123825 const char *zFile = (const char *)sqlite3_value_text(argv[0]);
123826 const char *zProc;
123827 sqlite3 *db = sqlite3_context_db_handle(context);
123828 char *zErrMsg = 0;
123829
123830 /* Disallow the load_extension() SQL function unless the SQLITE_LoadExtFunc
123831 ** flag is set. See the sqlite3_enable_load_extension() API.
123832 */
123833 if( (db->flags & SQLITE_LoadExtFunc0x00020000)==0 ){
123834 sqlite3_result_error(context, "not authorized", -1);
123835 return;
123836 }
123837
123838 if( argc==2 ){
123839 zProc = (const char *)sqlite3_value_text(argv[1]);
123840 }else{
123841 zProc = 0;
123842 }
123843 if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
123844 sqlite3_result_error(context, zErrMsg, -1);
123845 sqlite3_free(zErrMsg);
123846 }
123847}
123848#endif
123849
123850
123851/*
123852** An instance of the following structure holds the context of a
123853** sum() or avg() aggregate computation.
123854*/
123855typedef struct SumCtx SumCtx;
123856struct SumCtx {
123857 double rSum; /* Floating point sum */
123858 i64 iSum; /* Integer sum */
123859 i64 cnt; /* Number of elements summed */
123860 u8 overflow; /* True if integer overflow seen */
123861 u8 approx; /* True if non-integer value was input to the sum */
123862};
123863
123864/*
123865** Routines used to compute the sum, average, and total.
123866**
123867** The SUM() function follows the (broken) SQL standard which means
123868** that it returns NULL if it sums over no inputs. TOTAL returns
123869** 0.0 in that case. In addition, TOTAL always returns a float where
123870** SUM might return an integer if it never encounters a floating point
123871** value. TOTAL never fails, but SUM might through an exception if
123872** it overflows an integer.
123873*/
123874static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
123875 SumCtx *p;
123876 int type;
123877 assert( argc==1 )((void) (0));
123878 UNUSED_PARAMETER(argc)(void)(argc);
123879 p = sqlite3_aggregate_context(context, sizeof(*p));
123880 type = sqlite3_value_numeric_type(argv[0]);
123881 if( p && type!=SQLITE_NULL5 ){
123882 p->cnt++;
123883 if( type==SQLITE_INTEGER1 ){
123884 i64 v = sqlite3_value_int64(argv[0]);
123885 p->rSum += v;
123886 if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
123887 p->approx = p->overflow = 1;
123888 }
123889 }else{
123890 p->rSum += sqlite3_value_double(argv[0]);
123891 p->approx = 1;
123892 }
123893 }
123894}
123895#ifndef SQLITE_OMIT_WINDOWFUNC
123896static void sumInverse(sqlite3_context *context, int argc, sqlite3_value**argv){
123897 SumCtx *p;
123898 int type;
123899 assert( argc==1 )((void) (0));
123900 UNUSED_PARAMETER(argc)(void)(argc);
123901 p = sqlite3_aggregate_context(context, sizeof(*p));
123902 type = sqlite3_value_numeric_type(argv[0]);
123903 /* p is always non-NULL because sumStep() will have been called first
123904 ** to initialize it */
123905 if( ALWAYS(p)(p) && type!=SQLITE_NULL5 ){
123906 assert( p->cnt>0 )((void) (0));
123907 p->cnt--;
123908 assert( type==SQLITE_INTEGER || p->approx )((void) (0));
123909 if( type==SQLITE_INTEGER1 && p->approx==0 ){
123910 i64 v = sqlite3_value_int64(argv[0]);
123911 p->rSum -= v;
123912 p->iSum -= v;
123913 }else{
123914 p->rSum -= sqlite3_value_double(argv[0]);
123915 }
123916 }
123917}
123918#else
123919# define sumInverse 0
123920#endif /* SQLITE_OMIT_WINDOWFUNC */
123921static void sumFinalize(sqlite3_context *context){
123922 SumCtx *p;
123923 p = sqlite3_aggregate_context(context, 0);
123924 if( p && p->cnt>0 ){
123925 if( p->overflow ){
123926 sqlite3_result_error(context,"integer overflow",-1);
123927 }else if( p->approx ){
123928 sqlite3_result_double(context, p->rSum);
123929 }else{
123930 sqlite3_result_int64(context, p->iSum);
123931 }
123932 }
123933}
123934static void avgFinalize(sqlite3_context *context){
123935 SumCtx *p;
123936 p = sqlite3_aggregate_context(context, 0);
123937 if( p && p->cnt>0 ){
123938 sqlite3_result_double(context, p->rSum/(double)p->cnt);
123939 }
123940}
123941static void totalFinalize(sqlite3_context *context){
123942 SumCtx *p;
123943 p = sqlite3_aggregate_context(context, 0);
123944 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
123945 sqlite3_result_double(context, p ? p->rSum : (double)0);
123946}
123947
123948/*
123949** The following structure keeps track of state information for the
123950** count() aggregate function.
123951*/
123952typedef struct CountCtx CountCtx;
123953struct CountCtx {
123954 i64 n;
123955#ifdef SQLITE_DEBUG
123956 int bInverse; /* True if xInverse() ever called */
123957#endif
123958};
123959
123960/*
123961** Routines to implement the count() aggregate function.
123962*/
123963static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
123964 CountCtx *p;
123965 p = sqlite3_aggregate_context(context, sizeof(*p));
123966 if( (argc==0 || SQLITE_NULL5!=sqlite3_value_type(argv[0])) && p ){
123967 p->n++;
123968 }
123969
123970#ifndef SQLITE_OMIT_DEPRECATED1
123971 /* The sqlite3_aggregate_count() function is deprecated. But just to make
123972 ** sure it still operates correctly, verify that its count agrees with our
123973 ** internal count when using count(*) and when the total count can be
123974 ** expressed as a 32-bit integer. */
123975 assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse((void) (0))
123976 || p->n==sqlite3_aggregate_count(context) )((void) (0));
123977#endif
123978}
123979static void countFinalize(sqlite3_context *context){
123980 CountCtx *p;
123981 p = sqlite3_aggregate_context(context, 0);
123982 sqlite3_result_int64(context, p ? p->n : 0);
123983}
123984#ifndef SQLITE_OMIT_WINDOWFUNC
123985static void countInverse(sqlite3_context *ctx, int argc, sqlite3_value **argv){
123986 CountCtx *p;
123987 p = sqlite3_aggregate_context(ctx, sizeof(*p));
123988 /* p is always non-NULL since countStep() will have been called first */
123989 if( (argc==0 || SQLITE_NULL5!=sqlite3_value_type(argv[0])) && ALWAYS(p)(p) ){
123990 p->n--;
123991#ifdef SQLITE_DEBUG
123992 p->bInverse = 1;
123993#endif
123994 }
123995}
123996#else
123997# define countInverse 0
123998#endif /* SQLITE_OMIT_WINDOWFUNC */
123999
124000/*
124001** Routines to implement min() and max() aggregate functions.
124002*/
124003static void minmaxStep(
124004 sqlite3_context *context,
124005 int NotUsed,
124006 sqlite3_value **argv
124007){
124008 Mem *pArg = (Mem *)argv[0];
124009 Mem *pBest;
124010 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
124011
124012 pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
124013 if( !pBest ) return;
124014
124015 if( sqlite3_value_type(pArg)==SQLITE_NULL5 ){
124016 if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
124017 }else if( pBest->flags ){
124018 int max;
124019 int cmp;
124020 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
124021 /* This step function is used for both the min() and max() aggregates,
124022 ** the only difference between the two being that the sense of the
124023 ** comparison is inverted. For the max() aggregate, the
124024 ** sqlite3_user_data() function returns (void *)-1. For min() it
124025 ** returns (void *)db, where db is the sqlite3* database pointer.
124026 ** Therefore the next statement sets variable 'max' to 1 for the max()
124027 ** aggregate, or 0 for min().
124028 */
124029 max = sqlite3_user_data(context)!=0;
124030 cmp = sqlite3MemCompare(pBest, pArg, pColl);
124031 if( (max && cmp<0) || (!max && cmp>0) ){
124032 sqlite3VdbeMemCopy(pBest, pArg);
124033 }else{
124034 sqlite3SkipAccumulatorLoad(context);
124035 }
124036 }else{
124037 pBest->db = sqlite3_context_db_handle(context);
124038 sqlite3VdbeMemCopy(pBest, pArg);
124039 }
124040}
124041static void minMaxValueFinalize(sqlite3_context *context, int bValue){
124042 sqlite3_value *pRes;
124043 pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
124044 if( pRes ){
124045 if( pRes->flags ){
124046 sqlite3_result_value(context, pRes);
124047 }
124048 if( bValue==0 ) sqlite3VdbeMemRelease(pRes);
124049 }
124050}
124051#ifndef SQLITE_OMIT_WINDOWFUNC
124052static void minMaxValue(sqlite3_context *context){
124053 minMaxValueFinalize(context, 1);
124054}
124055#else
124056# define minMaxValue 0
124057#endif /* SQLITE_OMIT_WINDOWFUNC */
124058static void minMaxFinalize(sqlite3_context *context){
124059 minMaxValueFinalize(context, 0);
124060}
124061
124062/*
124063** group_concat(EXPR, ?SEPARATOR?)
124064**
124065** The SEPARATOR goes before the EXPR string. This is tragic. The
124066** groupConcatInverse() implementation would have been easier if the
124067** SEPARATOR were appended after EXPR. And the order is undocumented,
124068** so we could change it, in theory. But the old behavior has been
124069** around for so long that we dare not, for fear of breaking something.
124070*/
124071typedef struct {
124072 StrAccum str; /* The accumulated concatenation */
124073#ifndef SQLITE_OMIT_WINDOWFUNC
124074 int nAccum; /* Number of strings presently concatenated */
124075 int nFirstSepLength; /* Used to detect separator length change */
124076 /* If pnSepLengths!=0, refs an array of inter-string separator lengths,
124077 ** stored as actually incorporated into presently accumulated result.
124078 ** (Hence, its slots in use number nAccum-1 between method calls.)
124079 ** If pnSepLengths==0, nFirstSepLength is the length used throughout.
124080 */
124081 int *pnSepLengths;
124082#endif
124083} GroupConcatCtx;
124084
124085static void groupConcatStep(
124086 sqlite3_context *context,
124087 int argc,
124088 sqlite3_value **argv
124089){
124090 const char *zVal;
124091 GroupConcatCtx *pGCC;
124092 const char *zSep;
124093 int nVal, nSep;
124094 assert( argc==1 || argc==2 )((void) (0));
124095 if( sqlite3_value_type(argv[0])==SQLITE_NULL5 ) return;
124096 pGCC = (GroupConcatCtx*)sqlite3_aggregate_context(context, sizeof(*pGCC));
124097 if( pGCC ){
124098 sqlite3 *db = sqlite3_context_db_handle(context);
124099 int firstTerm = pGCC->str.mxAlloc==0;
124100 pGCC->str.mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH0];
124101 if( argc==1 ){
124102 if( !firstTerm ){
124103 sqlite3_str_appendchar(&pGCC->str, 1, ',');
124104 }
124105#ifndef SQLITE_OMIT_WINDOWFUNC
124106 else{
124107 pGCC->nFirstSepLength = 1;
124108 }
124109#endif
124110 }else if( !firstTerm ){
124111 zSep = (char*)sqlite3_value_text(argv[1]);
124112 nSep = sqlite3_value_bytes(argv[1]);
124113 if( zSep ){
124114 sqlite3_str_append(&pGCC->str, zSep, nSep);
124115 }
124116#ifndef SQLITE_OMIT_WINDOWFUNC
124117 else{
124118 nSep = 0;
124119 }
124120 if( nSep != pGCC->nFirstSepLength || pGCC->pnSepLengths != 0 ){
124121 int *pnsl = pGCC->pnSepLengths;
124122 if( pnsl == 0 ){
124123 /* First separator length variation seen, start tracking them. */
124124 pnsl = (int*)sqlite3_malloc64((pGCC->nAccum+1) * sizeof(int));
124125 if( pnsl!=0 ){
124126 int i = 0, nA = pGCC->nAccum-1;
124127 while( i<nA ) pnsl[i++] = pGCC->nFirstSepLength;
124128 }
124129 }else{
124130 pnsl = (int*)sqlite3_realloc64(pnsl, pGCC->nAccum * sizeof(int));
124131 }
124132 if( pnsl!=0 ){
124133 if( ALWAYS(pGCC->nAccum>0)(pGCC->nAccum>0) ){
124134 pnsl[pGCC->nAccum-1] = nSep;
124135 }
124136 pGCC->pnSepLengths = pnsl;
124137 }else{
124138 sqlite3StrAccumSetError(&pGCC->str, SQLITE_NOMEM7);
124139 }
124140 }
124141#endif
124142 }
124143#ifndef SQLITE_OMIT_WINDOWFUNC
124144 else{
124145 pGCC->nFirstSepLength = sqlite3_value_bytes(argv[1]);
124146 }
124147 pGCC->nAccum += 1;
124148#endif
124149 zVal = (char*)sqlite3_value_text(argv[0]);
124150 nVal = sqlite3_value_bytes(argv[0]);
124151 if( zVal ) sqlite3_str_append(&pGCC->str, zVal, nVal);
124152 }
124153}
124154
124155#ifndef SQLITE_OMIT_WINDOWFUNC
124156static void groupConcatInverse(
124157 sqlite3_context *context,
124158 int argc,
124159 sqlite3_value **argv
124160){
124161 GroupConcatCtx *pGCC;
124162 assert( argc==1 || argc==2 )((void) (0));
124163 (void)argc; /* Suppress unused parameter warning */
124164 if( sqlite3_value_type(argv[0])==SQLITE_NULL5 ) return;
124165 pGCC = (GroupConcatCtx*)sqlite3_aggregate_context(context, sizeof(*pGCC));
124166 /* pGCC is always non-NULL since groupConcatStep() will have always
124167 ** run frist to initialize it */
124168 if( ALWAYS(pGCC)(pGCC) ){
124169 int nVS;
124170 /* Must call sqlite3_value_text() to convert the argument into text prior
124171 ** to invoking sqlite3_value_bytes(), in case the text encoding is UTF16 */
124172 (void)sqlite3_value_text(argv[0]);
124173 nVS = sqlite3_value_bytes(argv[0]);
124174 pGCC->nAccum -= 1;
124175 if( pGCC->pnSepLengths!=0 ){
124176 assert(pGCC->nAccum >= 0)((void) (0));
124177 if( pGCC->nAccum>0 ){
124178 nVS += *pGCC->pnSepLengths;
124179 memmove(pGCC->pnSepLengths, pGCC->pnSepLengths+1,
124180 (pGCC->nAccum-1)*sizeof(int));
124181 }
124182 }else{
124183 /* If removing single accumulated string, harmlessly over-do. */
124184 nVS += pGCC->nFirstSepLength;
124185 }
124186 if( nVS>=(int)pGCC->str.nChar ){
124187 pGCC->str.nChar = 0;
124188 }else{
124189 pGCC->str.nChar -= nVS;
124190 memmove(pGCC->str.zText, &pGCC->str.zText[nVS], pGCC->str.nChar);
124191 }
124192 if( pGCC->str.nChar==0 ){
124193 pGCC->str.mxAlloc = 0;
124194 sqlite3_free(pGCC->pnSepLengths);
124195 pGCC->pnSepLengths = 0;
124196 }
124197 }
124198}
124199#else
124200# define groupConcatInverse 0
124201#endif /* SQLITE_OMIT_WINDOWFUNC */
124202static void groupConcatFinalize(sqlite3_context *context){
124203 GroupConcatCtx *pGCC
124204 = (GroupConcatCtx*)sqlite3_aggregate_context(context, 0);
124205 if( pGCC ){
124206 sqlite3ResultStrAccum(context, &pGCC->str);
124207#ifndef SQLITE_OMIT_WINDOWFUNC
124208 sqlite3_free(pGCC->pnSepLengths);
124209#endif
124210 }
124211}
124212#ifndef SQLITE_OMIT_WINDOWFUNC
124213static void groupConcatValue(sqlite3_context *context){
124214 GroupConcatCtx *pGCC
124215 = (GroupConcatCtx*)sqlite3_aggregate_context(context, 0);
124216 if( pGCC ){
124217 StrAccum *pAccum = &pGCC->str;
124218 if( pAccum->accError==SQLITE_TOOBIG18 ){
124219 sqlite3_result_error_toobig(context);
124220 }else if( pAccum->accError==SQLITE_NOMEM7 ){
124221 sqlite3_result_error_nomem(context);
124222 }else{
124223 const char *zText = sqlite3_str_value(pAccum);
124224 sqlite3_result_text(context, zText, pAccum->nChar, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
124225 }
124226 }
124227}
124228#else
124229# define groupConcatValue 0
124230#endif /* SQLITE_OMIT_WINDOWFUNC */
124231
124232/*
124233** This routine does per-connection function registration. Most
124234** of the built-in functions above are part of the global function set.
124235** This routine only deals with those that are not global.
124236*/
124237SQLITE_PRIVATEstatic void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3 *db){
124238 int rc = sqlite3_overload_function(db, "MATCH", 2);
124239 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK )((void) (0));
124240 if( rc==SQLITE_NOMEM7 ){
124241 sqlite3OomFault(db);
124242 }
124243}
124244
124245/*
124246** Re-register the built-in LIKE functions. The caseSensitive
124247** parameter determines whether or not the LIKE operator is case
124248** sensitive.
124249*/
124250SQLITE_PRIVATEstatic void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
124251 struct compareInfo *pInfo;
124252 int flags;
124253 if( caseSensitive ){
124254 pInfo = (struct compareInfo*)&likeInfoAlt;
124255 flags = SQLITE_FUNC_LIKE0x0004 | SQLITE_FUNC_CASE0x0008;
124256 }else{
124257 pInfo = (struct compareInfo*)&likeInfoNorm;
124258 flags = SQLITE_FUNC_LIKE0x0004;
124259 }
124260 sqlite3CreateFunc(db, "like", 2, SQLITE_UTF81, pInfo, likeFunc, 0, 0, 0, 0, 0);
124261 sqlite3CreateFunc(db, "like", 3, SQLITE_UTF81, pInfo, likeFunc, 0, 0, 0, 0, 0);
124262 sqlite3FindFunction(db, "like", 2, SQLITE_UTF81, 0)->funcFlags |= flags;
124263 sqlite3FindFunction(db, "like", 3, SQLITE_UTF81, 0)->funcFlags |= flags;
124264}
124265
124266/*
124267** pExpr points to an expression which implements a function. If
124268** it is appropriate to apply the LIKE optimization to that function
124269** then set aWc[0] through aWc[2] to the wildcard characters and the
124270** escape character and then return TRUE. If the function is not a
124271** LIKE-style function then return FALSE.
124272**
124273** The expression "a LIKE b ESCAPE c" is only considered a valid LIKE
124274** operator if c is a string literal that is exactly one byte in length.
124275** That one byte is stored in aWc[3]. aWc[3] is set to zero if there is
124276** no ESCAPE clause.
124277**
124278** *pIsNocase is set to true if uppercase and lowercase are equivalent for
124279** the function (default for LIKE). If the function makes the distinction
124280** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to
124281** false.
124282*/
124283SQLITE_PRIVATEstatic int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
124284 FuncDef *pDef;
124285 int nExpr;
124286 assert( pExpr!=0 )((void) (0));
124287 assert( pExpr->op==TK_FUNCTION )((void) (0));
124288 assert( ExprUseXList(pExpr) )((void) (0));
124289 if( !pExpr->x.pList ){
124290 return 0;
124291 }
124292 nExpr = pExpr->x.pList->nExpr;
124293 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
124294 pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF81, 0);
124295#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION1
124296 if( pDef==0 ) return 0;
124297#endif
124298 if( NEVER(pDef==0)(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE0x0004)==0 ){
124299 return 0;
124300 }
124301
124302 /* The memcpy() statement assumes that the wildcard characters are
124303 ** the first three statements in the compareInfo structure. The
124304 ** asserts() that follow verify that assumption
124305 */
124306 memcpy(aWc, pDef->pUserData, 3);
124307 assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll )((void) (0));
124308 assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne )((void) (0));
124309 assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet )((void) (0));
124310
124311 if( nExpr<3 ){
124312 aWc[3] = 0;
124313 }else{
124314 Expr *pEscape = pExpr->x.pList->a[2].pExpr;
124315 char *zEscape;
124316 if( pEscape->op!=TK_STRING117 ) return 0;
124317 assert( !ExprHasProperty(pEscape, EP_IntValue) )((void) (0));
124318 zEscape = pEscape->u.zToken;
124319 if( zEscape[0]==0 || zEscape[1]!=0 ) return 0;
124320 if( zEscape[0]==aWc[0] ) return 0;
124321 if( zEscape[0]==aWc[1] ) return 0;
124322 aWc[3] = zEscape[0];
124323 }
124324
124325 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE0x0008)==0;
124326 return 1;
124327}
124328
124329/* Mathematical Constants */
124330#ifndef M_PI3.14159265358979323846
124331# define M_PI3.14159265358979323846 3.141592653589793238462643383279502884
124332#endif
124333#ifndef M_LN102.30258509299404568402
124334# define M_LN102.30258509299404568402 2.302585092994045684017991454684364208
124335#endif
124336#ifndef M_LN20.69314718055994530942
124337# define M_LN20.69314718055994530942 0.693147180559945309417232121458176568
124338#endif
124339
124340
124341/* Extra math functions that require linking with -lm
124342*/
124343#ifdef SQLITE_ENABLE_MATH_FUNCTIONS1
124344/*
124345** Implementation SQL functions:
124346**
124347** ceil(X)
124348** ceiling(X)
124349** floor(X)
124350**
124351** The sqlite3_user_data() pointer is a pointer to the libm implementation
124352** of the underlying C function.
124353*/
124354static void ceilingFunc(
124355 sqlite3_context *context,
124356 int argc,
124357 sqlite3_value **argv
124358){
124359 assert( argc==1 )((void) (0));
124360 switch( sqlite3_value_numeric_type(argv[0]) ){
124361 case SQLITE_INTEGER1: {
124362 sqlite3_result_int64(context, sqlite3_value_int64(argv[0]));
124363 break;
124364 }
124365 case SQLITE_FLOAT2: {
124366 double (*x)(double) = (double(*)(double))sqlite3_user_data(context);
124367 sqlite3_result_double(context, x(sqlite3_value_double(argv[0])));
124368 break;
124369 }
124370 default: {
124371 break;
124372 }
124373 }
124374}
124375
124376/*
124377** On some systems, ceil() and floor() are intrinsic function. You are
124378** unable to take a pointer to these functions. Hence, we here wrap them
124379** in our own actual functions.
124380*/
124381static double xCeil(double x){ return ceil(x); }
124382static double xFloor(double x){ return floor(x); }
124383
124384/*
124385** Implementation of SQL functions:
124386**
124387** ln(X) - natural logarithm
124388** log(X) - log X base 10
124389** log10(X) - log X base 10
124390** log(B,X) - log X base B
124391*/
124392static void logFunc(
124393 sqlite3_context *context,
124394 int argc,
124395 sqlite3_value **argv
124396){
124397 double x, b, ans;
124398 assert( argc==1 || argc==2 )((void) (0));
124399 switch( sqlite3_value_numeric_type(argv[0]) ){
124400 case SQLITE_INTEGER1:
124401 case SQLITE_FLOAT2:
124402 x = sqlite3_value_double(argv[0]);
124403 if( x<=0.0 ) return;
124404 break;
124405 default:
124406 return;
124407 }
124408 if( argc==2 ){
124409 switch( sqlite3_value_numeric_type(argv[0]) ){
124410 case SQLITE_INTEGER1:
124411 case SQLITE_FLOAT2:
124412 b = log(x);
124413 if( b<=0.0 ) return;
124414 x = sqlite3_value_double(argv[1]);
124415 if( x<=0.0 ) return;
124416 break;
124417 default:
124418 return;
124419 }
124420 ans = log(x)/b;
124421 }else{
124422 ans = log(x);
124423 switch( SQLITE_PTR_TO_INT(sqlite3_user_data(context))((int)(long int)(sqlite3_user_data(context))) ){
124424 case 1:
124425 /* Convert from natural logarithm to log base 10 */
124426 ans /= M_LN102.30258509299404568402;
124427 break;
124428 case 2:
124429 /* Convert from natural logarithm to log base 2 */
124430 ans /= M_LN20.69314718055994530942;
124431 break;
124432 default:
124433 break;
124434 }
124435 }
124436 sqlite3_result_double(context, ans);
124437}
124438
124439/*
124440** Functions to converts degrees to radians and radians to degrees.
124441*/
124442static double degToRad(double x){ return x*(M_PI3.14159265358979323846/180.0); }
124443static double radToDeg(double x){ return x*(180.0/M_PI3.14159265358979323846); }
124444
124445/*
124446** Implementation of 1-argument SQL math functions:
124447**
124448** exp(X) - Compute e to the X-th power
124449*/
124450static void math1Func(
124451 sqlite3_context *context,
124452 int argc,
124453 sqlite3_value **argv
124454){
124455 int type0;
124456 double v0, ans;
124457 double (*x)(double);
124458 assert( argc==1 )((void) (0));
124459 type0 = sqlite3_value_numeric_type(argv[0]);
124460 if( type0!=SQLITE_INTEGER1 && type0!=SQLITE_FLOAT2 ) return;
124461 v0 = sqlite3_value_double(argv[0]);
124462 x = (double(*)(double))sqlite3_user_data(context);
124463 ans = x(v0);
124464 sqlite3_result_double(context, ans);
124465}
124466
124467/*
124468** Implementation of 2-argument SQL math functions:
124469**
124470** power(X,Y) - Compute X to the Y-th power
124471*/
124472static void math2Func(
124473 sqlite3_context *context,
124474 int argc,
124475 sqlite3_value **argv
124476){
124477 int type0, type1;
124478 double v0, v1, ans;
124479 double (*x)(double,double);
124480 assert( argc==2 )((void) (0));
124481 type0 = sqlite3_value_numeric_type(argv[0]);
124482 if( type0!=SQLITE_INTEGER1 && type0!=SQLITE_FLOAT2 ) return;
124483 type1 = sqlite3_value_numeric_type(argv[1]);
124484 if( type1!=SQLITE_INTEGER1 && type1!=SQLITE_FLOAT2 ) return;
124485 v0 = sqlite3_value_double(argv[0]);
124486 v1 = sqlite3_value_double(argv[1]);
124487 x = (double(*)(double,double))sqlite3_user_data(context);
124488 ans = x(v0, v1);
124489 sqlite3_result_double(context, ans);
124490}
124491
124492/*
124493** Implementation of 0-argument pi() function.
124494*/
124495static void piFunc(
124496 sqlite3_context *context,
124497 int argc,
124498 sqlite3_value **argv
124499){
124500 assert( argc==0 )((void) (0));
124501 sqlite3_result_double(context, M_PI3.14159265358979323846);
124502}
124503
124504#endif /* SQLITE_ENABLE_MATH_FUNCTIONS */
124505
124506/*
124507** Implementation of sign(X) function.
124508*/
124509static void signFunc(
124510 sqlite3_context *context,
124511 int argc,
124512 sqlite3_value **argv
124513){
124514 int type0;
124515 double x;
124516 UNUSED_PARAMETER(argc)(void)(argc);
124517 assert( argc==1 )((void) (0));
124518 type0 = sqlite3_value_numeric_type(argv[0]);
124519 if( type0!=SQLITE_INTEGER1 && type0!=SQLITE_FLOAT2 ) return;
124520 x = sqlite3_value_double(argv[0]);
124521 sqlite3_result_int(context, x<0.0 ? -1 : x>0.0 ? +1 : 0);
124522}
124523
124524/*
124525** All of the FuncDef structures in the aBuiltinFunc[] array above
124526** to the global function hash table. This occurs at start-time (as
124527** a consequence of calling sqlite3_initialize()).
124528**
124529** After this routine runs
124530*/
124531SQLITE_PRIVATEstatic void sqlite3RegisterBuiltinFunctions(void){
124532 /*
124533 ** The following array holds FuncDef structures for all of the functions
124534 ** defined in this file.
124535 **
124536 ** The array cannot be constant since changes are made to the
124537 ** FuncDef.pHash elements at start-time. The elements of this array
124538 ** are read-only after initialization is complete.
124539 **
124540 ** For peak efficiency, put the most frequently used function last.
124541 */
124542 static FuncDef aBuiltinFunc[] = {
124543/***** Functions only available with SQLITE_TESTCTRL_INTERNAL_FUNCTIONS *****/
124544#if !defined(SQLITE_UNTESTABLE)
124545 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} }
,
124546 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} }
,
124547 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} }
,
124548 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} }
,
124549#endif /* !defined(SQLITE_UNTESTABLE) */
124550/***** Regular functions *****/
124551#ifdef SQLITE_SOUNDEX
124552 FUNCTION(soundex, 1, 0, 0, soundexFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, soundexFunc, 0, 0, 0, "soundex", {0} }
,
124553#endif
124554#ifndef SQLITE_OMIT_LOAD_EXTENSION1
124555 SFUNCTION(load_extension, 1, 0, 0, loadExt ){1, 0x00800000|1|0x000080000|0x00200000, ((void*)(long int)(0
)), 0, loadExt, 0, 0, 0, "load_extension", {0} }
,
124556 SFUNCTION(load_extension, 2, 0, 0, loadExt ){2, 0x00800000|1|0x000080000|0x00200000, ((void*)(long int)(0
)), 0, loadExt, 0, 0, 0, "load_extension", {0} }
,
124557#endif
124558#if SQLITE_USER_AUTHENTICATION
124559 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} }
,
124560#endif
124561#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
124562 DFUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ){1, 0x00800000|0x2000|1, 0, 0, compileoptionusedFunc, 0, 0, 0
, "sqlite_compileoption_used", {0} }
,
124563 DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ){1, 0x00800000|0x2000|1, 0, 0, compileoptiongetFunc, 0, 0, 0,
"sqlite_compileoption_get", {0} }
,
124564#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
124565 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} }
,
124566 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} }
,
124567 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} }
,
124568#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
124569 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} }
,
124570#endif
124571 FUNCTION(ltrim, 1, 1, 0, trimFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(1)), 0
, trimFunc, 0, 0, 0, "ltrim", {0} }
,
124572 FUNCTION(ltrim, 2, 1, 0, trimFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(1)), 0
, trimFunc, 0, 0, 0, "ltrim", {0} }
,
124573 FUNCTION(rtrim, 1, 2, 0, trimFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(2)), 0
, trimFunc, 0, 0, 0, "rtrim", {0} }
,
124574 FUNCTION(rtrim, 2, 2, 0, trimFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(2)), 0
, trimFunc, 0, 0, 0, "rtrim", {0} }
,
124575 FUNCTION(trim, 1, 3, 0, trimFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(3)), 0
, trimFunc, 0, 0, 0, "trim", {0} }
,
124576 FUNCTION(trim, 2, 3, 0, trimFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(3)), 0
, trimFunc, 0, 0, 0, "trim", {0} }
,
124577 FUNCTION(min, -1, 0, 1, minmaxFunc ){-1, 0x00800000| 0x0800|1|(1*0x0020), ((void*)(long int)(0)),
0, minmaxFunc, 0, 0, 0, "min", {0} }
,
124578 FUNCTION(min, 0, 0, 1, 0 ){0, 0x00800000| 0x0800|1|(1*0x0020), ((void*)(long int)(0)), 0
, 0, 0, 0, 0, "min", {0} }
,
124579 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}}
124580 SQLITE_FUNC_MINMAX|SQLITE_FUNC_ANYORDER ){1, 0x00800000|1|(1*0x0020)|0x1000|0x08000000, ((void*)(long int
)(0)), 0, minmaxStep,minMaxFinalize,minMaxValue,0,"min", {0}}
,
124581 FUNCTION(max, -1, 1, 1, minmaxFunc ){-1, 0x00800000| 0x0800|1|(1*0x0020), ((void*)(long int)(1)),
0, minmaxFunc, 0, 0, 0, "max", {0} }
,
124582 FUNCTION(max, 0, 1, 1, 0 ){0, 0x00800000| 0x0800|1|(1*0x0020), ((void*)(long int)(1)), 0
, 0, 0, 0, 0, "max", {0} }
,
124583 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}}
124584 SQLITE_FUNC_MINMAX|SQLITE_FUNC_ANYORDER ){1, 0x00800000|1|(1*0x0020)|0x1000|0x08000000, ((void*)(long int
)(1)), 0, minmaxStep,minMaxFinalize,minMaxValue,0,"max", {0}}
,
124585 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} }
,
124586 FUNCTION2(subtype, 1, 0, 0, subtypeFunc, SQLITE_FUNC_TYPEOF){1, 0x00800000| 0x0800|1|(0*0x0020)|0x0080, ((void*)(long int
)(0)), 0, subtypeFunc, 0, 0, 0, "subtype", {0} }
,
124587 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} }
,
124588 FUNCTION(instr, 2, 0, 0, instrFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, instrFunc, 0, 0, 0, "instr", {0} }
,
124589 FUNCTION(printf, -1, 0, 0, printfFunc ){-1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)),
0, printfFunc, 0, 0, 0, "printf", {0} }
,
124590 FUNCTION(format, -1, 0, 0, printfFunc ){-1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)),
0, printfFunc, 0, 0, 0, "format", {0} }
,
124591 FUNCTION(unicode, 1, 0, 0, unicodeFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, unicodeFunc, 0, 0, 0, "unicode", {0} }
,
124592 FUNCTION(char, -1, 0, 0, charFunc ){-1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)),
0, charFunc, 0, 0, 0, "char", {0} }
,
124593 FUNCTION(abs, 1, 0, 0, absFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, absFunc, 0, 0, 0, "abs", {0} }
,
124594#ifndef SQLITE_OMIT_FLOATING_POINT
124595 FUNCTION(round, 1, 0, 0, roundFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, roundFunc, 0, 0, 0, "round", {0} }
,
124596 FUNCTION(round, 2, 0, 0, roundFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, roundFunc, 0, 0, 0, "round", {0} }
,
124597#endif
124598 FUNCTION(upper, 1, 0, 0, upperFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, upperFunc, 0, 0, 0, "upper", {0} }
,
124599 FUNCTION(lower, 1, 0, 0, lowerFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, lowerFunc, 0, 0, 0, "lower", {0} }
,
124600 FUNCTION(hex, 1, 0, 0, hexFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, hexFunc, 0, 0, 0, "hex", {0} }
,
124601 INLINE_FUNC(ifnull, 2, INLINEFUNC_coalesce, 0 ){2, 0x00800000| 1|0x00400000|0x0800|(0), ((void*)(long int)(0
)), 0, versionFunc, 0, 0, 0, "ifnull", {0} }
,
124602 VFUNCTION(random, 0, 0, 0, randomFunc ){0, 0x00800000|1|(0*0x0020), ((void*)(long int)(0)), 0, randomFunc
, 0, 0, 0, "random", {0} }
,
124603 VFUNCTION(randomblob, 1, 0, 0, randomBlob ){1, 0x00800000|1|(0*0x0020), ((void*)(long int)(0)), 0, randomBlob
, 0, 0, 0, "randomblob", {0} }
,
124604 FUNCTION(nullif, 2, 0, 1, nullifFunc ){2, 0x00800000| 0x0800|1|(1*0x0020), ((void*)(long int)(0)), 0
, nullifFunc, 0, 0, 0, "nullif", {0} }
,
124605 DFUNCTION(sqlite_version, 0, 0, 0, versionFunc ){0, 0x00800000|0x2000|1, 0, 0, versionFunc, 0, 0, 0, "sqlite_version"
, {0} }
,
124606 DFUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ){0, 0x00800000|0x2000|1, 0, 0, sourceidFunc, 0, 0, 0, "sqlite_source_id"
, {0} }
,
124607 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} }
,
124608 FUNCTION(quote, 1, 0, 0, quoteFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, quoteFunc, 0, 0, 0, "quote", {0} }
,
124609 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} }
,
124610 VFUNCTION(changes, 0, 0, 0, changes ){0, 0x00800000|1|(0*0x0020), ((void*)(long int)(0)), 0, changes
, 0, 0, 0, "changes", {0} }
,
124611 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} }
,
124612 FUNCTION(replace, 3, 0, 0, replaceFunc ){3, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, replaceFunc, 0, 0, 0, "replace", {0} }
,
124613 FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, zeroblobFunc, 0, 0, 0, "zeroblob", {0} }
,
124614 FUNCTION(substr, 2, 0, 0, substrFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, substrFunc, 0, 0, 0, "substr", {0} }
,
124615 FUNCTION(substr, 3, 0, 0, substrFunc ){3, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, substrFunc, 0, 0, 0, "substr", {0} }
,
124616 FUNCTION(substring, 2, 0, 0, substrFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, substrFunc, 0, 0, 0, "substring", {0} }
,
124617 FUNCTION(substring, 3, 0, 0, substrFunc ){3, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, substrFunc, 0, 0, 0, "substring", {0} }
,
124618 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}}
,
124619 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}}
,
124620 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}}
,
124621 WAGGREGATE(count, 0,0,0, countStep,{0, 0x00800000|1|(0*0x0020)|0x0100|0x08000000, ((void*)(long int
)(0)), 0, countStep,countFinalize,countFinalize,countInverse,
"count", {0}}
124622 countFinalize, countFinalize, countInverse,{0, 0x00800000|1|(0*0x0020)|0x0100|0x08000000, ((void*)(long int
)(0)), 0, countStep,countFinalize,countFinalize,countInverse,
"count", {0}}
124623 SQLITE_FUNC_COUNT|SQLITE_FUNC_ANYORDER ){0, 0x00800000|1|(0*0x0020)|0x0100|0x08000000, ((void*)(long int
)(0)), 0, countStep,countFinalize,countFinalize,countInverse,
"count", {0}}
,
124624 WAGGREGATE(count, 1,0,0, countStep,{1, 0x00800000|1|(0*0x0020)|0x08000000, ((void*)(long int)(0)
), 0, countStep,countFinalize,countFinalize,countInverse,"count"
, {0}}
124625 countFinalize, countFinalize, countInverse, SQLITE_FUNC_ANYORDER ){1, 0x00800000|1|(0*0x0020)|0x08000000, ((void*)(long int)(0)
), 0, countStep,countFinalize,countFinalize,countInverse,"count"
, {0}}
,
124626 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}}
124627 groupConcatFinalize, groupConcatValue, groupConcatInverse, 0){1, 0x00800000|1|(0*0x0020)|0, ((void*)(long int)(0)), 0, groupConcatStep
,groupConcatFinalize,groupConcatValue,groupConcatInverse,"group_concat"
, {0}}
,
124628 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}}
124629 groupConcatFinalize, groupConcatValue, groupConcatInverse, 0){2, 0x00800000|1|(0*0x0020)|0, ((void*)(long int)(0)), 0, groupConcatStep
,groupConcatFinalize,groupConcatValue,groupConcatInverse,"group_concat"
, {0}}
,
124630
124631 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} }
,
124632#ifdef SQLITE_CASE_SENSITIVE_LIKE
124633 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} }
,
124634 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} }
,
124635#else
124636 LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE){2, 0x00800000|0x0800|1|0x0004, (void *)&likeInfoNorm, 0,
likeFunc, 0, 0, 0, "like", {0} }
,
124637 LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE){3, 0x00800000|0x0800|1|0x0004, (void *)&likeInfoNorm, 0,
likeFunc, 0, 0, 0, "like", {0} }
,
124638#endif
124639#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION1
124640 FUNCTION(unknown, -1, 0, 0, unknownFunc ){-1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)),
0, unknownFunc, 0, 0, 0, "unknown", {0} }
,
124641#endif
124642 FUNCTION(coalesce, 1, 0, 0, 0 ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, 0, 0, 0, 0, "coalesce", {0} }
,
124643 FUNCTION(coalesce, 0, 0, 0, 0 ){0, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, 0, 0, 0, 0, "coalesce", {0} }
,
124644#ifdef SQLITE_ENABLE_MATH_FUNCTIONS1
124645 MFUNCTION(ceil, 1, xCeil, ceilingFunc ){1, 0x00800000|0x0800|1, xCeil, 0, ceilingFunc, 0, 0, 0, "ceil"
, {0} }
,
124646 MFUNCTION(ceiling, 1, xCeil, ceilingFunc ){1, 0x00800000|0x0800|1, xCeil, 0, ceilingFunc, 0, 0, 0, "ceiling"
, {0} }
,
124647 MFUNCTION(floor, 1, xFloor, ceilingFunc ){1, 0x00800000|0x0800|1, xFloor, 0, ceilingFunc, 0, 0, 0, "floor"
, {0} }
,
124648#if SQLITE_HAVE_C99_MATH_FUNCS(1)
124649 MFUNCTION(trunc, 1, trunc, ceilingFunc ){1, 0x00800000|0x0800|1, trunc, 0, ceilingFunc, 0, 0, 0, "trunc"
, {0} }
,
124650#endif
124651 FUNCTION(ln, 1, 0, 0, logFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, logFunc, 0, 0, 0, "ln", {0} }
,
124652 FUNCTION(log, 1, 1, 0, logFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(1)), 0
, logFunc, 0, 0, 0, "log", {0} }
,
124653 FUNCTION(log10, 1, 1, 0, logFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(1)), 0
, logFunc, 0, 0, 0, "log10", {0} }
,
124654 FUNCTION(log2, 1, 2, 0, logFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(2)), 0
, logFunc, 0, 0, 0, "log2", {0} }
,
124655 FUNCTION(log, 2, 0, 0, logFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, logFunc, 0, 0, 0, "log", {0} }
,
124656 MFUNCTION(exp, 1, exp, math1Func ){1, 0x00800000|0x0800|1, exp, 0, math1Func, 0, 0, 0, "exp", {
0} }
,
124657 MFUNCTION(pow, 2, pow, math2Func ){2, 0x00800000|0x0800|1, pow, 0, math2Func, 0, 0, 0, "pow", {
0} }
,
124658 MFUNCTION(power, 2, pow, math2Func ){2, 0x00800000|0x0800|1, pow, 0, math2Func, 0, 0, 0, "power",
{0} }
,
124659 MFUNCTION(mod, 2, fmod, math2Func ){2, 0x00800000|0x0800|1, fmod, 0, math2Func, 0, 0, 0, "mod", {
0} }
,
124660 MFUNCTION(acos, 1, acos, math1Func ){1, 0x00800000|0x0800|1, acos, 0, math1Func, 0, 0, 0, "acos",
{0} }
,
124661 MFUNCTION(asin, 1, asin, math1Func ){1, 0x00800000|0x0800|1, asin, 0, math1Func, 0, 0, 0, "asin",
{0} }
,
124662 MFUNCTION(atan, 1, atan, math1Func ){1, 0x00800000|0x0800|1, atan, 0, math1Func, 0, 0, 0, "atan",
{0} }
,
124663 MFUNCTION(atan2, 2, atan2, math2Func ){2, 0x00800000|0x0800|1, atan2, 0, math2Func, 0, 0, 0, "atan2"
, {0} }
,
124664 MFUNCTION(cos, 1, cos, math1Func ){1, 0x00800000|0x0800|1, cos, 0, math1Func, 0, 0, 0, "cos", {
0} }
,
124665 MFUNCTION(sin, 1, sin, math1Func ){1, 0x00800000|0x0800|1, sin, 0, math1Func, 0, 0, 0, "sin", {
0} }
,
124666 MFUNCTION(tan, 1, tan, math1Func ){1, 0x00800000|0x0800|1, tan, 0, math1Func, 0, 0, 0, "tan", {
0} }
,
124667 MFUNCTION(cosh, 1, cosh, math1Func ){1, 0x00800000|0x0800|1, cosh, 0, math1Func, 0, 0, 0, "cosh",
{0} }
,
124668 MFUNCTION(sinh, 1, sinh, math1Func ){1, 0x00800000|0x0800|1, sinh, 0, math1Func, 0, 0, 0, "sinh",
{0} }
,
124669 MFUNCTION(tanh, 1, tanh, math1Func ){1, 0x00800000|0x0800|1, tanh, 0, math1Func, 0, 0, 0, "tanh",
{0} }
,
124670#if SQLITE_HAVE_C99_MATH_FUNCS(1)
124671 MFUNCTION(acosh, 1, acosh, math1Func ){1, 0x00800000|0x0800|1, acosh, 0, math1Func, 0, 0, 0, "acosh"
, {0} }
,
124672 MFUNCTION(asinh, 1, asinh, math1Func ){1, 0x00800000|0x0800|1, asinh, 0, math1Func, 0, 0, 0, "asinh"
, {0} }
,
124673 MFUNCTION(atanh, 1, atanh, math1Func ){1, 0x00800000|0x0800|1, atanh, 0, math1Func, 0, 0, 0, "atanh"
, {0} }
,
124674#endif
124675 MFUNCTION(sqrt, 1, sqrt, math1Func ){1, 0x00800000|0x0800|1, sqrt, 0, math1Func, 0, 0, 0, "sqrt",
{0} }
,
124676 MFUNCTION(radians, 1, degToRad, math1Func ){1, 0x00800000|0x0800|1, degToRad, 0, math1Func, 0, 0, 0, "radians"
, {0} }
,
124677 MFUNCTION(degrees, 1, radToDeg, math1Func ){1, 0x00800000|0x0800|1, radToDeg, 0, math1Func, 0, 0, 0, "degrees"
, {0} }
,
124678 FUNCTION(pi, 0, 0, 0, piFunc ){0, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, piFunc, 0, 0, 0, "pi", {0} }
,
124679#endif /* SQLITE_ENABLE_MATH_FUNCTIONS */
124680 FUNCTION(sign, 1, 0, 0, signFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, signFunc, 0, 0, 0, "sign", {0} }
,
124681 INLINE_FUNC(coalesce, -1, INLINEFUNC_coalesce, 0 ){-1, 0x00800000| 1|0x00400000|0x0800|(0), ((void*)(long int)(
0)), 0, versionFunc, 0, 0, 0, "coalesce", {0} }
,
124682 INLINE_FUNC(iif, 3, INLINEFUNC_iif, 0 ){3, 0x00800000| 1|0x00400000|0x0800|(0), ((void*)(long int)(5
)), 0, versionFunc, 0, 0, 0, "iif", {0} }
,
124683 };
124684#ifndef SQLITE_OMIT_ALTERTABLE
124685 sqlite3AlterFunctions();
124686#endif
124687 sqlite3WindowFunctions();
124688 sqlite3RegisterDateTimeFunctions();
124689 sqlite3RegisterJsonFunctions();
124690 sqlite3InsertBuiltinFuncs(aBuiltinFunc, ArraySize(aBuiltinFunc)((int)(sizeof(aBuiltinFunc)/sizeof(aBuiltinFunc[0]))));
124691
124692#if 0 /* Enable to print out how the built-in functions are hashed */
124693 {
124694 int i;
124695 FuncDef *p;
124696 for(i=0; i<SQLITE_FUNC_HASH_SZ23; i++){
124697 printf("FUNC-HASH %02d:", i);
124698 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){
124699 int n = sqlite3Strlen30(p->zName);
124700 int h = p->zName[0] + n;
124701 assert( p->funcFlags & SQLITE_FUNC_BUILTIN )((void) (0));
124702 printf(" %s(%d)", p->zName, h);
124703 }
124704 printf("\n");
124705 }
124706 }
124707#endif
124708}
124709
124710/************** End of func.c ************************************************/
124711/************** Begin file fkey.c ********************************************/
124712/*
124713**
124714** The author disclaims copyright to this source code. In place of
124715** a legal notice, here is a blessing:
124716**
124717** May you do good and not evil.
124718** May you find forgiveness for yourself and forgive others.
124719** May you share freely, never taking more than you give.
124720**
124721*************************************************************************
124722** This file contains code used by the compiler to add foreign key
124723** support to compiled SQL statements.
124724*/
124725/* #include "sqliteInt.h" */
124726
124727#ifndef SQLITE_OMIT_FOREIGN_KEY
124728#ifndef SQLITE_OMIT_TRIGGER
124729
124730/*
124731** Deferred and Immediate FKs
124732** --------------------------
124733**
124734** Foreign keys in SQLite come in two flavours: deferred and immediate.
124735** If an immediate foreign key constraint is violated,
124736** SQLITE_CONSTRAINT_FOREIGNKEY is returned and the current
124737** statement transaction rolled back. If a
124738** deferred foreign key constraint is violated, no action is taken
124739** immediately. However if the application attempts to commit the
124740** transaction before fixing the constraint violation, the attempt fails.
124741**
124742** Deferred constraints are implemented using a simple counter associated
124743** with the database handle. The counter is set to zero each time a
124744** database transaction is opened. Each time a statement is executed
124745** that causes a foreign key violation, the counter is incremented. Each
124746** time a statement is executed that removes an existing violation from
124747** the database, the counter is decremented. When the transaction is
124748** committed, the commit fails if the current value of the counter is
124749** greater than zero. This scheme has two big drawbacks:
124750**
124751** * When a commit fails due to a deferred foreign key constraint,
124752** there is no way to tell which foreign constraint is not satisfied,
124753** or which row it is not satisfied for.
124754**
124755** * If the database contains foreign key violations when the
124756** transaction is opened, this may cause the mechanism to malfunction.
124757**
124758** Despite these problems, this approach is adopted as it seems simpler
124759** than the alternatives.
124760**
124761** INSERT operations:
124762**
124763** I.1) For each FK for which the table is the child table, search
124764** the parent table for a match. If none is found increment the
124765** constraint counter.
124766**
124767** I.2) For each FK for which the table is the parent table,
124768** search the child table for rows that correspond to the new
124769** row in the parent table. Decrement the counter for each row
124770** found (as the constraint is now satisfied).
124771**
124772** DELETE operations:
124773**
124774** D.1) For each FK for which the table is the child table,
124775** search the parent table for a row that corresponds to the
124776** deleted row in the child table. If such a row is not found,
124777** decrement the counter.
124778**
124779** D.2) For each FK for which the table is the parent table, search
124780** the child table for rows that correspond to the deleted row
124781** in the parent table. For each found increment the counter.
124782**
124783** UPDATE operations:
124784**
124785** An UPDATE command requires that all 4 steps above are taken, but only
124786** for FK constraints for which the affected columns are actually
124787** modified (values must be compared at runtime).
124788**
124789** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2.
124790** This simplifies the implementation a bit.
124791**
124792** For the purposes of immediate FK constraints, the OR REPLACE conflict
124793** resolution is considered to delete rows before the new row is inserted.
124794** If a delete caused by OR REPLACE violates an FK constraint, an exception
124795** is thrown, even if the FK constraint would be satisfied after the new
124796** row is inserted.
124797**
124798** Immediate constraints are usually handled similarly. The only difference
124799** is that the counter used is stored as part of each individual statement
124800** object (struct Vdbe). If, after the statement has run, its immediate
124801** constraint counter is greater than zero,
124802** it returns SQLITE_CONSTRAINT_FOREIGNKEY
124803** and the statement transaction is rolled back. An exception is an INSERT
124804** statement that inserts a single row only (no triggers). In this case,
124805** instead of using a counter, an exception is thrown immediately if the
124806** INSERT violates a foreign key constraint. This is necessary as such
124807** an INSERT does not open a statement transaction.
124808**
124809** TODO: How should dropping a table be handled? How should renaming a
124810** table be handled?
124811**
124812**
124813** Query API Notes
124814** ---------------
124815**
124816** Before coding an UPDATE or DELETE row operation, the code-generator
124817** for those two operations needs to know whether or not the operation
124818** requires any FK processing and, if so, which columns of the original
124819** row are required by the FK processing VDBE code (i.e. if FKs were
124820** implemented using triggers, which of the old.* columns would be
124821** accessed). No information is required by the code-generator before
124822** coding an INSERT operation. The functions used by the UPDATE/DELETE
124823** generation code to query for this information are:
124824**
124825** sqlite3FkRequired() - Test to see if FK processing is required.
124826** sqlite3FkOldmask() - Query for the set of required old.* columns.
124827**
124828**
124829** Externally accessible module functions
124830** --------------------------------------
124831**
124832** sqlite3FkCheck() - Check for foreign key violations.
124833** sqlite3FkActions() - Code triggers for ON UPDATE/ON DELETE actions.
124834** sqlite3FkDelete() - Delete an FKey structure.
124835*/
124836
124837/*
124838** VDBE Calling Convention
124839** -----------------------
124840**
124841** Example:
124842**
124843** For the following INSERT statement:
124844**
124845** CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c);
124846** INSERT INTO t1 VALUES(1, 2, 3.1);
124847**
124848** Register (x): 2 (type integer)
124849** Register (x+1): 1 (type integer)
124850** Register (x+2): NULL (type NULL)
124851** Register (x+3): 3.1 (type real)
124852*/
124853
124854/*
124855** A foreign key constraint requires that the key columns in the parent
124856** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
124857** Given that pParent is the parent table for foreign key constraint pFKey,
124858** search the schema for a unique index on the parent key columns.
124859**
124860** If successful, zero is returned. If the parent key is an INTEGER PRIMARY
124861** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx
124862** is set to point to the unique index.
124863**
124864** If the parent key consists of a single column (the foreign key constraint
124865** is not a composite foreign key), output variable *paiCol is set to NULL.
124866** Otherwise, it is set to point to an allocated array of size N, where
124867** N is the number of columns in the parent key. The first element of the
124868** array is the index of the child table column that is mapped by the FK
124869** constraint to the parent table column stored in the left-most column
124870** of index *ppIdx. The second element of the array is the index of the
124871** child table column that corresponds to the second left-most column of
124872** *ppIdx, and so on.
124873**
124874** If the required index cannot be found, either because:
124875**
124876** 1) The named parent key columns do not exist, or
124877**
124878** 2) The named parent key columns do exist, but are not subject to a
124879** UNIQUE or PRIMARY KEY constraint, or
124880**
124881** 3) No parent key columns were provided explicitly as part of the
124882** foreign key definition, and the parent table does not have a
124883** PRIMARY KEY, or
124884**
124885** 4) No parent key columns were provided explicitly as part of the
124886** foreign key definition, and the PRIMARY KEY of the parent table
124887** consists of a different number of columns to the child key in
124888** the child table.
124889**
124890** then non-zero is returned, and a "foreign key mismatch" error loaded
124891** into pParse. If an OOM error occurs, non-zero is returned and the
124892** pParse->db->mallocFailed flag is set.
124893*/
124894SQLITE_PRIVATEstatic int sqlite3FkLocateIndex(
124895 Parse *pParse, /* Parse context to store any error in */
124896 Table *pParent, /* Parent table of FK constraint pFKey */
124897 FKey *pFKey, /* Foreign key to find index for */
124898 Index **ppIdx, /* OUT: Unique index on parent table */
124899 int **paiCol /* OUT: Map of index columns in pFKey */
124900){
124901 Index *pIdx = 0; /* Value to return via *ppIdx */
124902 int *aiCol = 0; /* Value to return via *paiCol */
124903 int nCol = pFKey->nCol; /* Number of columns in parent key */
124904 char *zKey = pFKey->aCol[0].zCol; /* Name of left-most parent key column */
124905
124906 /* The caller is responsible for zeroing output parameters. */
124907 assert( ppIdx && *ppIdx==0 )((void) (0));
124908 assert( !paiCol || *paiCol==0 )((void) (0));
124909 assert( pParse )((void) (0));
124910
124911 /* If this is a non-composite (single column) foreign key, check if it
124912 ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx
124913 ** and *paiCol set to zero and return early.
124914 **
124915 ** Otherwise, for a composite foreign key (more than one column), allocate
124916 ** space for the aiCol array (returned via output parameter *paiCol).
124917 ** Non-composite foreign keys do not require the aiCol array.
124918 */
124919 if( nCol==1 ){
124920 /* The FK maps to the IPK if any of the following are true:
124921 **
124922 ** 1) There is an INTEGER PRIMARY KEY column and the FK is implicitly
124923 ** mapped to the primary key of table pParent, or
124924 ** 2) The FK is explicitly mapped to a column declared as INTEGER
124925 ** PRIMARY KEY.
124926 */
124927 if( pParent->iPKey>=0 ){
124928 if( !zKey ) return 0;
124929 if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zCnName, zKey) ){
124930 return 0;
124931 }
124932 }
124933 }else if( paiCol ){
124934 assert( nCol>1 )((void) (0));
124935 aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int));
124936 if( !aiCol ) return 1;
124937 *paiCol = aiCol;
124938 }
124939
124940 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
124941 if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx)((pIdx)->onError!=0) && pIdx->pPartIdxWhere==0 ){
124942 /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
124943 ** of columns. If each indexed column corresponds to a foreign key
124944 ** column of pFKey, then this index is a winner. */
124945
124946 if( zKey==0 ){
124947 /* If zKey is NULL, then this foreign key is implicitly mapped to
124948 ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be
124949 ** identified by the test. */
124950 if( IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ){
124951 if( aiCol ){
124952 int i;
124953 for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
124954 }
124955 break;
124956 }
124957 }else{
124958 /* If zKey is non-NULL, then this foreign key was declared to
124959 ** map to an explicit list of columns in table pParent. Check if this
124960 ** index matches those columns. Also, check that the index uses
124961 ** the default collation sequences for each column. */
124962 int i, j;
124963 for(i=0; i<nCol; i++){
124964 i16 iCol = pIdx->aiColumn[i]; /* Index of column in parent tbl */
124965 const char *zDfltColl; /* Def. collation for column */
124966 char *zIdxCol; /* Name of indexed column */
124967
124968 if( iCol<0 ) break; /* No foreign keys against expression indexes */
124969
124970 /* If the index uses a collation sequence that is different from
124971 ** the default collation sequence for the column, this index is
124972 ** unusable. Bail out early in this case. */
124973 zDfltColl = sqlite3ColumnColl(&pParent->aCol[iCol]);
124974 if( !zDfltColl ) zDfltColl = sqlite3StrBINARY;
124975 if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
124976
124977 zIdxCol = pParent->aCol[iCol].zCnName;
124978 for(j=0; j<nCol; j++){
124979 if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
124980 if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
124981 break;
124982 }
124983 }
124984 if( j==nCol ) break;
124985 }
124986 if( i==nCol ) break; /* pIdx is usable */
124987 }
124988 }
124989 }
124990
124991 if( !pIdx ){
124992 if( !pParse->disableTriggers ){
124993 sqlite3ErrorMsg(pParse,
124994 "foreign key mismatch - \"%w\" referencing \"%w\"",
124995 pFKey->pFrom->zName, pFKey->zTo);
124996 }
124997 sqlite3DbFree(pParse->db, aiCol);
124998 return 1;
124999 }
125000
125001 *ppIdx = pIdx;
125002 return 0;
125003}
125004
125005/*
125006** This function is called when a row is inserted into or deleted from the
125007** child table of foreign key constraint pFKey. If an SQL UPDATE is executed
125008** on the child table of pFKey, this function is invoked twice for each row
125009** affected - once to "delete" the old row, and then again to "insert" the
125010** new row.
125011**
125012** Each time it is called, this function generates VDBE code to locate the
125013** row in the parent table that corresponds to the row being inserted into
125014** or deleted from the child table. If the parent row can be found, no
125015** special action is taken. Otherwise, if the parent row can *not* be
125016** found in the parent table:
125017**
125018** Operation | FK type | Action taken
125019** --------------------------------------------------------------------------
125020** INSERT immediate Increment the "immediate constraint counter".
125021**
125022** DELETE immediate Decrement the "immediate constraint counter".
125023**
125024** INSERT deferred Increment the "deferred constraint counter".
125025**
125026** DELETE deferred Decrement the "deferred constraint counter".
125027**
125028** These operations are identified in the comment at the top of this file
125029** (fkey.c) as "I.1" and "D.1".
125030*/
125031static void fkLookupParent(
125032 Parse *pParse, /* Parse context */
125033 int iDb, /* Index of database housing pTab */
125034 Table *pTab, /* Parent table of FK pFKey */
125035 Index *pIdx, /* Unique index on parent key columns in pTab */
125036 FKey *pFKey, /* Foreign key constraint */
125037 int *aiCol, /* Map from parent key columns to child table columns */
125038 int regData, /* Address of array containing child table row */
125039 int nIncr, /* Increment constraint counter by this */
125040 int isIgnore /* If true, pretend pTab contains all NULL values */
125041){
125042 int i; /* Iterator variable */
125043 Vdbe *v = sqlite3GetVdbe(pParse); /* Vdbe to add code to */
125044 int iCur = pParse->nTab - 1; /* Cursor number to use */
125045 int iOk = sqlite3VdbeMakeLabel(pParse); /* jump here if parent key found */
125046
125047 sqlite3VdbeVerifyAbortable(v,
125048 (!pFKey->isDeferred
125049 && !(pParse->db->flags & SQLITE_DeferFKs)
125050 && !pParse->pToplevel
125051 && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore);
125052
125053 /* If nIncr is less than zero, then check at runtime if there are any
125054 ** outstanding constraints to resolve. If there are not, there is no need
125055 ** to check if deleting this row resolves any outstanding violations.
125056 **
125057 ** Check if any of the key columns in the child table row are NULL. If
125058 ** any are, then the constraint is considered satisfied. No need to
125059 ** search for a matching row in the parent table. */
125060 if( nIncr<0 ){
125061 sqlite3VdbeAddOp2(v, OP_FkIfZero48, pFKey->isDeferred, iOk);
125062 VdbeCoverage(v);
125063 }
125064 for(i=0; i<pFKey->nCol; i++){
125065 int iReg = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i]) + regData + 1;
125066 sqlite3VdbeAddOp2(v, OP_IsNull50, iReg, iOk); VdbeCoverage(v);
125067 }
125068
125069 if( isIgnore==0 ){
125070 if( pIdx==0 ){
125071 /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
125072 ** column of the parent table (table pTab). */
125073 int iMustBeInt; /* Address of MustBeInt instruction */
125074 int regTemp = sqlite3GetTempReg(pParse);
125075
125076 /* Invoke MustBeInt to coerce the child key value to an integer (i.e.
125077 ** apply the affinity of the parent key). If this fails, then there
125078 ** is no matching parent key. Before using MustBeInt, make a copy of
125079 ** the value. Otherwise, the value inserted into the child key column
125080 ** will have INTEGER affinity applied to it, which may not be correct. */
125081 sqlite3VdbeAddOp2(v, OP_SCopy81,
125082 sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[0])+1+regData, regTemp);
125083 iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt12, regTemp, 0);
125084 VdbeCoverage(v);
125085
125086 /* If the parent table is the same as the child table, and we are about
125087 ** to increment the constraint-counter (i.e. this is an INSERT operation),
125088 ** then check if the row being inserted matches itself. If so, do not
125089 ** increment the constraint-counter. */
125090 if( pTab==pFKey->pFrom && nIncr==1 ){
125091 sqlite3VdbeAddOp3(v, OP_Eq53, regData, iOk, regTemp); VdbeCoverage(v);
125092 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL0x90);
125093 }
125094
125095 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead112);
125096 sqlite3VdbeAddOp3(v, OP_NotExists30, iCur, 0, regTemp); VdbeCoverage(v);
125097 sqlite3VdbeGoto(v, iOk);
125098 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
125099 sqlite3VdbeJumpHere(v, iMustBeInt);
125100 sqlite3ReleaseTempReg(pParse, regTemp);
125101 }else{
125102 int nCol = pFKey->nCol;
125103 int regTemp = sqlite3GetTempRange(pParse, nCol);
125104
125105 sqlite3VdbeAddOp3(v, OP_OpenRead112, iCur, pIdx->tnum, iDb);
125106 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
125107 for(i=0; i<nCol; i++){
125108 sqlite3VdbeAddOp2(v, OP_Copy80,
125109 sqlite3TableColumnToStorage(pFKey->pFrom, aiCol[i])+1+regData,
125110 regTemp+i);
125111 }
125112
125113 /* If the parent table is the same as the child table, and we are about
125114 ** to increment the constraint-counter (i.e. this is an INSERT operation),
125115 ** then check if the row being inserted matches itself. If so, do not
125116 ** increment the constraint-counter.
125117 **
125118 ** If any of the parent-key values are NULL, then the row cannot match
125119 ** itself. So set JUMPIFNULL to make sure we do the OP_Found if any
125120 ** of the parent-key values are NULL (at this point it is known that
125121 ** none of the child key values are).
125122 */
125123 if( pTab==pFKey->pFrom && nIncr==1 ){
125124 int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1;
125125 for(i=0; i<nCol; i++){
125126 int iChild = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i])
125127 +1+regData;
125128 int iParent = 1+regData;
125129 iParent += sqlite3TableColumnToStorage(pIdx->pTable,
125130 pIdx->aiColumn[i]);
125131 assert( pIdx->aiColumn[i]>=0 )((void) (0));
125132 assert( aiCol[i]!=pTab->iPKey )((void) (0));
125133 if( pIdx->aiColumn[i]==pTab->iPKey ){
125134 /* The parent key is a composite key that includes the IPK column */
125135 iParent = regData;
125136 }
125137 sqlite3VdbeAddOp3(v, OP_Ne52, iChild, iJump, iParent); VdbeCoverage(v);
125138 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL0x10);
125139 }
125140 sqlite3VdbeGoto(v, iOk);
125141 }
125142
125143 sqlite3VdbeAddOp4(v, OP_Affinity96, regTemp, nCol, 0,
125144 sqlite3IndexAffinityStr(pParse->db,pIdx), nCol);
125145 sqlite3VdbeAddOp4Int(v, OP_Found28, iCur, iOk, regTemp, nCol);
125146 VdbeCoverage(v);
125147 sqlite3ReleaseTempRange(pParse, regTemp, nCol);
125148 }
125149 }
125150
125151 if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs0x00080000)
125152 && !pParse->pToplevel
125153 && !pParse->isMultiWrite
125154 ){
125155 /* Special case: If this is an INSERT statement that will insert exactly
125156 ** one row into the table, raise a constraint immediately instead of
125157 ** incrementing a counter. This is necessary as the VM code is being
125158 ** generated for will not open a statement transaction. */
125159 assert( nIncr==1 )((void) (0));
125160 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8)),
125161 OE_Abort2, 0, P4_STATIC(-1), P5_ConstraintFK4);
125162 }else{
125163 if( nIncr>0 && pFKey->isDeferred==0 ){
125164 sqlite3MayAbort(pParse);
125165 }
125166 sqlite3VdbeAddOp2(v, OP_FkCounter158, pFKey->isDeferred, nIncr);
125167 }
125168
125169 sqlite3VdbeResolveLabel(v, iOk);
125170 sqlite3VdbeAddOp1(v, OP_Close122, iCur);
125171}
125172
125173
125174/*
125175** Return an Expr object that refers to a memory register corresponding
125176** to column iCol of table pTab.
125177**
125178** regBase is the first of an array of register that contains the data
125179** for pTab. regBase itself holds the rowid. regBase+1 holds the first
125180** column. regBase+2 holds the second column, and so forth.
125181*/
125182static Expr *exprTableRegister(
125183 Parse *pParse, /* Parsing and code generating context */
125184 Table *pTab, /* The table whose content is at r[regBase]... */
125185 int regBase, /* Contents of table pTab */
125186 i16 iCol /* Which column of pTab is desired */
125187){
125188 Expr *pExpr;
125189 Column *pCol;
125190 const char *zColl;
125191 sqlite3 *db = pParse->db;
125192
125193 pExpr = sqlite3Expr(db, TK_REGISTER176, 0);
125194 if( pExpr ){
125195 if( iCol>=0 && iCol!=pTab->iPKey ){
125196 pCol = &pTab->aCol[iCol];
125197 pExpr->iTable = regBase + sqlite3TableColumnToStorage(pTab,iCol) + 1;
125198 pExpr->affExpr = pCol->affinity;
125199 zColl = sqlite3ColumnColl(pCol);
125200 if( zColl==0 ) zColl = db->pDfltColl->zName;
125201 pExpr = sqlite3ExprAddCollateString(pParse, pExpr, zColl);
125202 }else{
125203 pExpr->iTable = regBase;
125204 pExpr->affExpr = SQLITE_AFF_INTEGER0x44;
125205 }
125206 }
125207 return pExpr;
125208}
125209
125210/*
125211** Return an Expr object that refers to column iCol of table pTab which
125212** has cursor iCur.
125213*/
125214static Expr *exprTableColumn(
125215 sqlite3 *db, /* The database connection */
125216 Table *pTab, /* The table whose column is desired */
125217 int iCursor, /* The open cursor on the table */
125218 i16 iCol /* The column that is wanted */
125219){
125220 Expr *pExpr = sqlite3Expr(db, TK_COLUMN167, 0);
125221 if( pExpr ){
125222 assert( ExprUseYTab(pExpr) )((void) (0));
125223 pExpr->y.pTab = pTab;
125224 pExpr->iTable = iCursor;
125225 pExpr->iColumn = iCol;
125226 }
125227 return pExpr;
125228}
125229
125230/*
125231** This function is called to generate code executed when a row is deleted
125232** from the parent table of foreign key constraint pFKey and, if pFKey is
125233** deferred, when a row is inserted into the same table. When generating
125234** code for an SQL UPDATE operation, this function may be called twice -
125235** once to "delete" the old row and once to "insert" the new row.
125236**
125237** Parameter nIncr is passed -1 when inserting a row (as this may decrease
125238** the number of FK violations in the db) or +1 when deleting one (as this
125239** may increase the number of FK constraint problems).
125240**
125241** The code generated by this function scans through the rows in the child
125242** table that correspond to the parent table row being deleted or inserted.
125243** For each child row found, one of the following actions is taken:
125244**
125245** Operation | FK type | Action taken
125246** --------------------------------------------------------------------------
125247** DELETE immediate Increment the "immediate constraint counter".
125248**
125249** INSERT immediate Decrement the "immediate constraint counter".
125250**
125251** DELETE deferred Increment the "deferred constraint counter".
125252**
125253** INSERT deferred Decrement the "deferred constraint counter".
125254**
125255** These operations are identified in the comment at the top of this file
125256** (fkey.c) as "I.2" and "D.2".
125257*/
125258static void fkScanChildren(
125259 Parse *pParse, /* Parse context */
125260 SrcList *pSrc, /* The child table to be scanned */
125261 Table *pTab, /* The parent table */
125262 Index *pIdx, /* Index on parent covering the foreign key */
125263 FKey *pFKey, /* The foreign key linking pSrc to pTab */
125264 int *aiCol, /* Map from pIdx cols to child table cols */
125265 int regData, /* Parent row data starts here */
125266 int nIncr /* Amount to increment deferred counter by */
125267){
125268 sqlite3 *db = pParse->db; /* Database handle */
125269 int i; /* Iterator variable */
125270 Expr *pWhere = 0; /* WHERE clause to scan with */
125271 NameContext sNameContext; /* Context used to resolve WHERE clause */
125272 WhereInfo *pWInfo; /* Context used by sqlite3WhereXXX() */
125273 int iFkIfZero = 0; /* Address of OP_FkIfZero */
125274 Vdbe *v = sqlite3GetVdbe(pParse);
125275
125276 assert( pIdx==0 || pIdx->pTable==pTab )((void) (0));
125277 assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol )((void) (0));
125278 assert( pIdx!=0 || pFKey->nCol==1 )((void) (0));
125279 assert( pIdx!=0 || HasRowid(pTab) )((void) (0));
125280
125281 if( nIncr<0 ){
125282 iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero48, pFKey->isDeferred, 0);
125283 VdbeCoverage(v);
125284 }
125285
125286 /* Create an Expr object representing an SQL expression like:
125287 **
125288 ** <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
125289 **
125290 ** The collation sequence used for the comparison should be that of
125291 ** the parent key columns. The affinity of the parent key column should
125292 ** be applied to each child key value before the comparison takes place.
125293 */
125294 for(i=0; i<pFKey->nCol; i++){
125295 Expr *pLeft; /* Value from parent table row */
125296 Expr *pRight; /* Column ref to child table */
125297 Expr *pEq; /* Expression (pLeft = pRight) */
125298 i16 iCol; /* Index of column in child table */
125299 const char *zCol; /* Name of column in child table */
125300
125301 iCol = pIdx ? pIdx->aiColumn[i] : -1;
125302 pLeft = exprTableRegister(pParse, pTab, regData, iCol);
125303 iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
125304 assert( iCol>=0 )((void) (0));
125305 zCol = pFKey->pFrom->aCol[iCol].zCnName;
125306 pRight = sqlite3Expr(db, TK_ID59, zCol);
125307 pEq = sqlite3PExpr(pParse, TK_EQ53, pLeft, pRight);
125308 pWhere = sqlite3ExprAnd(pParse, pWhere, pEq);
125309 }
125310
125311 /* If the child table is the same as the parent table, then add terms
125312 ** to the WHERE clause that prevent this entry from being scanned.
125313 ** The added WHERE clause terms are like this:
125314 **
125315 ** $current_rowid!=rowid
125316 ** NOT( $current_a==a AND $current_b==b AND ... )
125317 **
125318 ** The first form is used for rowid tables. The second form is used
125319 ** for WITHOUT ROWID tables. In the second form, the *parent* key is
125320 ** (a,b,...). Either the parent or primary key could be used to
125321 ** uniquely identify the current row, but the parent key is more convenient
125322 ** as the required values have already been loaded into registers
125323 ** by the caller.
125324 */
125325 if( pTab==pFKey->pFrom && nIncr>0 ){
125326 Expr *pNe; /* Expression (pLeft != pRight) */
125327 Expr *pLeft; /* Value from parent table row */
125328 Expr *pRight; /* Column ref to child table */
125329 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
125330 pLeft = exprTableRegister(pParse, pTab, regData, -1);
125331 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
125332 pNe = sqlite3PExpr(pParse, TK_NE52, pLeft, pRight);
125333 }else{
125334 Expr *pEq, *pAll = 0;
125335 assert( pIdx!=0 )((void) (0));
125336 for(i=0; i<pIdx->nKeyCol; i++){
125337 i16 iCol = pIdx->aiColumn[i];
125338 assert( iCol>=0 )((void) (0));
125339 pLeft = exprTableRegister(pParse, pTab, regData, iCol);
125340 pRight = sqlite3Expr(db, TK_ID59, pTab->aCol[iCol].zCnName);
125341 pEq = sqlite3PExpr(pParse, TK_IS45, pLeft, pRight);
125342 pAll = sqlite3ExprAnd(pParse, pAll, pEq);
125343 }
125344 pNe = sqlite3PExpr(pParse, TK_NOT19, pAll, 0);
125345 }
125346 pWhere = sqlite3ExprAnd(pParse, pWhere, pNe);
125347 }
125348
125349 /* Resolve the references in the WHERE clause. */
125350 memset(&sNameContext, 0, sizeof(NameContext));
125351 sNameContext.pSrcList = pSrc;
125352 sNameContext.pParse = pParse;
125353 sqlite3ResolveExprNames(&sNameContext, pWhere);
125354
125355 /* Create VDBE to loop through the entries in pSrc that match the WHERE
125356 ** clause. For each row found, increment either the deferred or immediate
125357 ** foreign key constraint counter. */
125358 if( pParse->nErr==0 ){
125359 pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0, 0);
125360 sqlite3VdbeAddOp2(v, OP_FkCounter158, pFKey->isDeferred, nIncr);
125361 if( pWInfo ){
125362 sqlite3WhereEnd(pWInfo);
125363 }
125364 }
125365
125366 /* Clean up the WHERE clause constructed above. */
125367 sqlite3ExprDelete(db, pWhere);
125368 if( iFkIfZero ){
125369 sqlite3VdbeJumpHereOrPopInst(v, iFkIfZero);
125370 }
125371}
125372
125373/*
125374** This function returns a linked list of FKey objects (connected by
125375** FKey.pNextTo) holding all children of table pTab. For example,
125376** given the following schema:
125377**
125378** CREATE TABLE t1(a PRIMARY KEY);
125379** CREATE TABLE t2(b REFERENCES t1(a);
125380**
125381** Calling this function with table "t1" as an argument returns a pointer
125382** to the FKey structure representing the foreign key constraint on table
125383** "t2". Calling this function with "t2" as the argument would return a
125384** NULL pointer (as there are no FK constraints for which t2 is the parent
125385** table).
125386*/
125387SQLITE_PRIVATEstatic FKey *sqlite3FkReferences(Table *pTab){
125388 return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName);
125389}
125390
125391/*
125392** The second argument is a Trigger structure allocated by the
125393** fkActionTrigger() routine. This function deletes the Trigger structure
125394** and all of its sub-components.
125395**
125396** The Trigger structure or any of its sub-components may be allocated from
125397** the lookaside buffer belonging to database handle dbMem.
125398*/
125399static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){
125400 if( p ){
125401 TriggerStep *pStep = p->step_list;
125402 sqlite3ExprDelete(dbMem, pStep->pWhere);
125403 sqlite3ExprListDelete(dbMem, pStep->pExprList);
125404 sqlite3SelectDelete(dbMem, pStep->pSelect);
125405 sqlite3ExprDelete(dbMem, p->pWhen);
125406 sqlite3DbFree(dbMem, p);
125407 }
125408}
125409
125410/*
125411** Clear the apTrigger[] cache of CASCADE triggers for all foreign keys
125412** in a particular database. This needs to happen when the schema
125413** changes.
125414*/
125415SQLITE_PRIVATEstatic void sqlite3FkClearTriggerCache(sqlite3 *db, int iDb){
125416 HashElem *k;
125417 Hash *pHash = &db->aDb[iDb].pSchema->tblHash;
125418 for(k=sqliteHashFirst(pHash)((pHash)->first); k; k=sqliteHashNext(k)((k)->next)){
125419 Table *pTab = sqliteHashData(k)((k)->data);
125420 FKey *pFKey;
125421 if( !IsOrdinaryTable(pTab)((pTab)->eTabType==0) ) continue;
125422 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
125423 fkTriggerDelete(db, pFKey->apTrigger[0]); pFKey->apTrigger[0] = 0;
125424 fkTriggerDelete(db, pFKey->apTrigger[1]); pFKey->apTrigger[1] = 0;
125425 }
125426 }
125427}
125428
125429/*
125430** This function is called to generate code that runs when table pTab is
125431** being dropped from the database. The SrcList passed as the second argument
125432** to this function contains a single entry guaranteed to resolve to
125433** table pTab.
125434**
125435** Normally, no code is required. However, if either
125436**
125437** (a) The table is the parent table of a FK constraint, or
125438** (b) The table is the child table of a deferred FK constraint and it is
125439** determined at runtime that there are outstanding deferred FK
125440** constraint violations in the database,
125441**
125442** then the equivalent of "DELETE FROM <tbl>" is executed before dropping
125443** the table from the database. Triggers are disabled while running this
125444** DELETE, but foreign key actions are not.
125445*/
125446SQLITE_PRIVATEstatic void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){
125447 sqlite3 *db = pParse->db;
125448 if( (db->flags&SQLITE_ForeignKeys0x00004000) && IsOrdinaryTable(pTab)((pTab)->eTabType==0) ){
125449 int iSkip = 0;
125450 Vdbe *v = sqlite3GetVdbe(pParse);
125451
125452 assert( v )((void) (0)); /* VDBE has already been allocated */
125453 assert( IsOrdinaryTable(pTab) )((void) (0));
125454 if( sqlite3FkReferences(pTab)==0 ){
125455 /* Search for a deferred foreign key constraint for which this table
125456 ** is the child table. If one cannot be found, return without
125457 ** generating any VDBE code. If one can be found, then jump over
125458 ** the entire DELETE if there are no outstanding deferred constraints
125459 ** when this statement is run. */
125460 FKey *p;
125461 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
125462 if( p->isDeferred || (db->flags & SQLITE_DeferFKs0x00080000) ) break;
125463 }
125464 if( !p ) return;
125465 iSkip = sqlite3VdbeMakeLabel(pParse);
125466 sqlite3VdbeAddOp2(v, OP_FkIfZero48, 1, iSkip); VdbeCoverage(v);
125467 }
125468
125469 pParse->disableTriggers = 1;
125470 sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0, 0, 0);
125471 pParse->disableTriggers = 0;
125472
125473 /* If the DELETE has generated immediate foreign key constraint
125474 ** violations, halt the VDBE and return an error at this point, before
125475 ** any modifications to the schema are made. This is because statement
125476 ** transactions are not able to rollback schema changes.
125477 **
125478 ** If the SQLITE_DeferFKs flag is set, then this is not required, as
125479 ** the statement transaction will not be rolled back even if FK
125480 ** constraints are violated.
125481 */
125482 if( (db->flags & SQLITE_DeferFKs0x00080000)==0 ){
125483 sqlite3VdbeVerifyAbortable(v, OE_Abort);
125484 sqlite3VdbeAddOp2(v, OP_FkIfZero48, 0, sqlite3VdbeCurrentAddr(v)+2);
125485 VdbeCoverage(v);
125486 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8)),
125487 OE_Abort2, 0, P4_STATIC(-1), P5_ConstraintFK4);
125488 }
125489
125490 if( iSkip ){
125491 sqlite3VdbeResolveLabel(v, iSkip);
125492 }
125493 }
125494}
125495
125496
125497/*
125498** The second argument points to an FKey object representing a foreign key
125499** for which pTab is the child table. An UPDATE statement against pTab
125500** is currently being processed. For each column of the table that is
125501** actually updated, the corresponding element in the aChange[] array
125502** is zero or greater (if a column is unmodified the corresponding element
125503** is set to -1). If the rowid column is modified by the UPDATE statement
125504** the bChngRowid argument is non-zero.
125505**
125506** This function returns true if any of the columns that are part of the
125507** child key for FK constraint *p are modified.
125508*/
125509static int fkChildIsModified(
125510 Table *pTab, /* Table being updated */
125511 FKey *p, /* Foreign key for which pTab is the child */
125512 int *aChange, /* Array indicating modified columns */
125513 int bChngRowid /* True if rowid is modified by this update */
125514){
125515 int i;
125516 for(i=0; i<p->nCol; i++){
125517 int iChildKey = p->aCol[i].iFrom;
125518 if( aChange[iChildKey]>=0 ) return 1;
125519 if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
125520 }
125521 return 0;
125522}
125523
125524/*
125525** The second argument points to an FKey object representing a foreign key
125526** for which pTab is the parent table. An UPDATE statement against pTab
125527** is currently being processed. For each column of the table that is
125528** actually updated, the corresponding element in the aChange[] array
125529** is zero or greater (if a column is unmodified the corresponding element
125530** is set to -1). If the rowid column is modified by the UPDATE statement
125531** the bChngRowid argument is non-zero.
125532**
125533** This function returns true if any of the columns that are part of the
125534** parent key for FK constraint *p are modified.
125535*/
125536static int fkParentIsModified(
125537 Table *pTab,
125538 FKey *p,
125539 int *aChange,
125540 int bChngRowid
125541){
125542 int i;
125543 for(i=0; i<p->nCol; i++){
125544 char *zKey = p->aCol[i].zCol;
125545 int iKey;
125546 for(iKey=0; iKey<pTab->nCol; iKey++){
125547 if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
125548 Column *pCol = &pTab->aCol[iKey];
125549 if( zKey ){
125550 if( 0==sqlite3StrICmp(pCol->zCnName, zKey) ) return 1;
125551 }else if( pCol->colFlags & COLFLAG_PRIMKEY0x0001 ){
125552 return 1;
125553 }
125554 }
125555 }
125556 }
125557 return 0;
125558}
125559
125560/*
125561** Return true if the parser passed as the first argument is being
125562** used to code a trigger that is really a "SET NULL" action belonging
125563** to trigger pFKey.
125564*/
125565static int isSetNullAction(Parse *pParse, FKey *pFKey){
125566 Parse *pTop = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
125567 if( pTop->pTriggerPrg ){
125568 Trigger *p = pTop->pTriggerPrg->pTrigger;
125569 if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull8)
125570 || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull8)
125571 ){
125572 return 1;
125573 }
125574 }
125575 return 0;
125576}
125577
125578/*
125579** This function is called when inserting, deleting or updating a row of
125580** table pTab to generate VDBE code to perform foreign key constraint
125581** processing for the operation.
125582**
125583** For a DELETE operation, parameter regOld is passed the index of the
125584** first register in an array of (pTab->nCol+1) registers containing the
125585** rowid of the row being deleted, followed by each of the column values
125586** of the row being deleted, from left to right. Parameter regNew is passed
125587** zero in this case.
125588**
125589** For an INSERT operation, regOld is passed zero and regNew is passed the
125590** first register of an array of (pTab->nCol+1) registers containing the new
125591** row data.
125592**
125593** For an UPDATE operation, this function is called twice. Once before
125594** the original record is deleted from the table using the calling convention
125595** described for DELETE. Then again after the original record is deleted
125596** but before the new record is inserted using the INSERT convention.
125597*/
125598SQLITE_PRIVATEstatic void sqlite3FkCheck(
125599 Parse *pParse, /* Parse context */
125600 Table *pTab, /* Row is being deleted from this table */
125601 int regOld, /* Previous row data is stored here */
125602 int regNew, /* New row data is stored here */
125603 int *aChange, /* Array indicating UPDATEd columns (or 0) */
125604 int bChngRowid /* True if rowid is UPDATEd */
125605){
125606 sqlite3 *db = pParse->db; /* Database handle */
125607 FKey *pFKey; /* Used to iterate through FKs */
125608 int iDb; /* Index of database containing pTab */
125609 const char *zDb; /* Name of database containing pTab */
125610 int isIgnoreErrors = pParse->disableTriggers;
125611
125612 /* Exactly one of regOld and regNew should be non-zero. */
125613 assert( (regOld==0)!=(regNew==0) )((void) (0));
125614
125615 /* If foreign-keys are disabled, this function is a no-op. */
125616 if( (db->flags&SQLITE_ForeignKeys0x00004000)==0 ) return;
125617 if( !IsOrdinaryTable(pTab)((pTab)->eTabType==0) ) return;
125618
125619 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
125620 zDb = db->aDb[iDb].zDbSName;
125621
125622 /* Loop through all the foreign key constraints for which pTab is the
125623 ** child table (the table that the foreign key definition is part of). */
125624 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
125625 Table *pTo; /* Parent table of foreign key pFKey */
125626 Index *pIdx = 0; /* Index on key columns in pTo */
125627 int *aiFree = 0;
125628 int *aiCol;
125629 int iCol;
125630 int i;
125631 int bIgnore = 0;
125632
125633 if( aChange
125634 && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
125635 && fkChildIsModified(pTab, pFKey, aChange, bChngRowid)==0
125636 ){
125637 continue;
125638 }
125639
125640 /* Find the parent table of this foreign key. Also find a unique index
125641 ** on the parent key columns in the parent table. If either of these
125642 ** schema items cannot be located, set an error in pParse and return
125643 ** early. */
125644 if( pParse->disableTriggers ){
125645 pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
125646 }else{
125647 pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
125648 }
125649 if( !pTo || sqlite3FkLocateIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
125650 assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) )((void) (0));
125651 if( !isIgnoreErrors || db->mallocFailed ) return;
125652 if( pTo==0 ){
125653 /* If isIgnoreErrors is true, then a table is being dropped. In this
125654 ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
125655 ** before actually dropping it in order to check FK constraints.
125656 ** If the parent table of an FK constraint on the current table is
125657 ** missing, behave as if it is empty. i.e. decrement the relevant
125658 ** FK counter for each row of the current table with non-NULL keys.
125659 */
125660 Vdbe *v = sqlite3GetVdbe(pParse);
125661 int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
125662 for(i=0; i<pFKey->nCol; i++){
125663 int iFromCol, iReg;
125664 iFromCol = pFKey->aCol[i].iFrom;
125665 iReg = sqlite3TableColumnToStorage(pFKey->pFrom,iFromCol) + regOld+1;
125666 sqlite3VdbeAddOp2(v, OP_IsNull50, iReg, iJump); VdbeCoverage(v);
125667 }
125668 sqlite3VdbeAddOp2(v, OP_FkCounter158, pFKey->isDeferred, -1);
125669 }
125670 continue;
125671 }
125672 assert( pFKey->nCol==1 || (aiFree && pIdx) )((void) (0));
125673
125674 if( aiFree ){
125675 aiCol = aiFree;
125676 }else{
125677 iCol = pFKey->aCol[0].iFrom;
125678 aiCol = &iCol;
125679 }
125680 for(i=0; i<pFKey->nCol; i++){
125681 if( aiCol[i]==pTab->iPKey ){
125682 aiCol[i] = -1;
125683 }
125684 assert( pIdx==0 || pIdx->aiColumn[i]>=0 )((void) (0));
125685#ifndef SQLITE_OMIT_AUTHORIZATION
125686 /* Request permission to read the parent key columns. If the
125687 ** authorization callback returns SQLITE_IGNORE, behave as if any
125688 ** values read from the parent table are NULL. */
125689 if( db->xAuth ){
125690 int rcauth;
125691 char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zCnName;
125692 rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
125693 bIgnore = (rcauth==SQLITE_IGNORE2);
125694 }
125695#endif
125696 }
125697
125698 /* Take a shared-cache advisory read-lock on the parent table. Allocate
125699 ** a cursor to use to search the unique index on the parent key columns
125700 ** in the parent table. */
125701 sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
125702 pParse->nTab++;
125703
125704 if( regOld!=0 ){
125705 /* A row is being removed from the child table. Search for the parent.
125706 ** If the parent does not exist, removing the child row resolves an
125707 ** outstanding foreign key constraint violation. */
125708 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore);
125709 }
125710 if( regNew!=0 && !isSetNullAction(pParse, pFKey) ){
125711 /* A row is being added to the child table. If a parent row cannot
125712 ** be found, adding the child row has violated the FK constraint.
125713 **
125714 ** If this operation is being performed as part of a trigger program
125715 ** that is actually a "SET NULL" action belonging to this very
125716 ** foreign key, then omit this scan altogether. As all child key
125717 ** values are guaranteed to be NULL, it is not possible for adding
125718 ** this row to cause an FK violation. */
125719 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1, bIgnore);
125720 }
125721
125722 sqlite3DbFree(db, aiFree);
125723 }
125724
125725 /* Loop through all the foreign key constraints that refer to this table.
125726 ** (the "child" constraints) */
125727 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
125728 Index *pIdx = 0; /* Foreign key index for pFKey */
125729 SrcList *pSrc;
125730 int *aiCol = 0;
125731
125732 if( aChange && fkParentIsModified(pTab, pFKey, aChange, bChngRowid)==0 ){
125733 continue;
125734 }
125735
125736 if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs0x00080000)
125737 && !pParse->pToplevel && !pParse->isMultiWrite
125738 ){
125739 assert( regOld==0 && regNew!=0 )((void) (0));
125740 /* Inserting a single row into a parent table cannot cause (or fix)
125741 ** an immediate foreign key violation. So do nothing in this case. */
125742 continue;
125743 }
125744
125745 if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
125746 if( !isIgnoreErrors || db->mallocFailed ) return;
125747 continue;
125748 }
125749 assert( aiCol || pFKey->nCol==1 )((void) (0));
125750
125751 /* Create a SrcList structure containing the child table. We need the
125752 ** child table as a SrcList for sqlite3WhereBegin() */
125753 pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
125754 if( pSrc ){
125755 SrcItem *pItem = pSrc->a;
125756 pItem->pTab = pFKey->pFrom;
125757 pItem->zName = pFKey->pFrom->zName;
125758 pItem->pTab->nTabRef++;
125759 pItem->iCursor = pParse->nTab++;
125760
125761 if( regNew!=0 ){
125762 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
125763 }
125764 if( regOld!=0 ){
125765 int eAction = pFKey->aAction[aChange!=0];
125766 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1);
125767 /* If this is a deferred FK constraint, or a CASCADE or SET NULL
125768 ** action applies, then any foreign key violations caused by
125769 ** removing the parent key will be rectified by the action trigger.
125770 ** So do not set the "may-abort" flag in this case.
125771 **
125772 ** Note 1: If the FK is declared "ON UPDATE CASCADE", then the
125773 ** may-abort flag will eventually be set on this statement anyway
125774 ** (when this function is called as part of processing the UPDATE
125775 ** within the action trigger).
125776 **
125777 ** Note 2: At first glance it may seem like SQLite could simply omit
125778 ** all OP_FkCounter related scans when either CASCADE or SET NULL
125779 ** applies. The trouble starts if the CASCADE or SET NULL action
125780 ** trigger causes other triggers or action rules attached to the
125781 ** child table to fire. In these cases the fk constraint counters
125782 ** might be set incorrectly if any OP_FkCounter related scans are
125783 ** omitted. */
125784 if( !pFKey->isDeferred && eAction!=OE_Cascade10 && eAction!=OE_SetNull8 ){
125785 sqlite3MayAbort(pParse);
125786 }
125787 }
125788 pItem->zName = 0;
125789 sqlite3SrcListDelete(db, pSrc);
125790 }
125791 sqlite3DbFree(db, aiCol);
125792 }
125793}
125794
125795#define COLUMN_MASK(x)(((x)>31) ? 0xffffffff : ((u32)1<<(x))) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))
125796
125797/*
125798** This function is called before generating code to update or delete a
125799** row contained in table pTab.
125800*/
125801SQLITE_PRIVATEstatic u32 sqlite3FkOldmask(
125802 Parse *pParse, /* Parse context */
125803 Table *pTab /* Table being modified */
125804){
125805 u32 mask = 0;
125806 if( pParse->db->flags&SQLITE_ForeignKeys0x00004000 && IsOrdinaryTable(pTab)((pTab)->eTabType==0) ){
125807 FKey *p;
125808 int i;
125809 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
125810 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)))
;
125811 }
125812 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
125813 Index *pIdx = 0;
125814 sqlite3FkLocateIndex(pParse, pTab, p, &pIdx, 0);
125815 if( pIdx ){
125816 for(i=0; i<pIdx->nKeyCol; i++){
125817 assert( pIdx->aiColumn[i]>=0 )((void) (0));
125818 mask |= COLUMN_MASK(pIdx->aiColumn[i])(((pIdx->aiColumn[i])>31) ? 0xffffffff : ((u32)1<<
(pIdx->aiColumn[i])))
;
125819 }
125820 }
125821 }
125822 }
125823 return mask;
125824}
125825
125826
125827/*
125828** This function is called before generating code to update or delete a
125829** row contained in table pTab. If the operation is a DELETE, then
125830** parameter aChange is passed a NULL value. For an UPDATE, aChange points
125831** to an array of size N, where N is the number of columns in table pTab.
125832** If the i'th column is not modified by the UPDATE, then the corresponding
125833** entry in the aChange[] array is set to -1. If the column is modified,
125834** the value is 0 or greater. Parameter chngRowid is set to true if the
125835** UPDATE statement modifies the rowid fields of the table.
125836**
125837** If any foreign key processing will be required, this function returns
125838** non-zero. If there is no foreign key related processing, this function
125839** returns zero.
125840**
125841** For an UPDATE, this function returns 2 if:
125842**
125843** * There are any FKs for which pTab is the child and the parent table
125844** and any FK processing at all is required (even of a different FK), or
125845**
125846** * the UPDATE modifies one or more parent keys for which the action is
125847** not "NO ACTION" (i.e. is CASCADE, SET DEFAULT or SET NULL).
125848**
125849** Or, assuming some other foreign key processing is required, 1.
125850*/
125851SQLITE_PRIVATEstatic int sqlite3FkRequired(
125852 Parse *pParse, /* Parse context */
125853 Table *pTab, /* Table being modified */
125854 int *aChange, /* Non-NULL for UPDATE operations */
125855 int chngRowid /* True for UPDATE that affects rowid */
125856){
125857 int eRet = 1; /* Value to return if bHaveFK is true */
125858 int bHaveFK = 0; /* If FK processing is required */
125859 if( pParse->db->flags&SQLITE_ForeignKeys0x00004000 && IsOrdinaryTable(pTab)((pTab)->eTabType==0) ){
125860 if( !aChange ){
125861 /* A DELETE operation. Foreign key processing is required if the
125862 ** table in question is either the child or parent table for any
125863 ** foreign key constraint. */
125864 bHaveFK = (sqlite3FkReferences(pTab) || pTab->u.tab.pFKey);
125865 }else{
125866 /* This is an UPDATE. Foreign key processing is only required if the
125867 ** operation modifies one or more child or parent key columns. */
125868 FKey *p;
125869
125870 /* Check if any child key columns are being modified. */
125871 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
125872 if( fkChildIsModified(pTab, p, aChange, chngRowid) ){
125873 if( 0==sqlite3_stricmp(pTab->zName, p->zTo) ) eRet = 2;
125874 bHaveFK = 1;
125875 }
125876 }
125877
125878 /* Check if any parent key columns are being modified. */
125879 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
125880 if( fkParentIsModified(pTab, p, aChange, chngRowid) ){
125881 if( p->aAction[1]!=OE_None0 ) return 2;
125882 bHaveFK = 1;
125883 }
125884 }
125885 }
125886 }
125887 return bHaveFK ? eRet : 0;
125888}
125889
125890/*
125891** This function is called when an UPDATE or DELETE operation is being
125892** compiled on table pTab, which is the parent table of foreign-key pFKey.
125893** If the current operation is an UPDATE, then the pChanges parameter is
125894** passed a pointer to the list of columns being modified. If it is a
125895** DELETE, pChanges is passed a NULL pointer.
125896**
125897** It returns a pointer to a Trigger structure containing a trigger
125898** equivalent to the ON UPDATE or ON DELETE action specified by pFKey.
125899** If the action is "NO ACTION" then a NULL pointer is returned (these actions
125900** require no special handling by the triggers sub-system, code for them is
125901** created by fkScanChildren()).
125902**
125903** For example, if pFKey is the foreign key and pTab is table "p" in
125904** the following schema:
125905**
125906** CREATE TABLE p(pk PRIMARY KEY);
125907** CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE);
125908**
125909** then the returned trigger structure is equivalent to:
125910**
125911** CREATE TRIGGER ... DELETE ON p BEGIN
125912** DELETE FROM c WHERE ck = old.pk;
125913** END;
125914**
125915** The returned pointer is cached as part of the foreign key object. It
125916** is eventually freed along with the rest of the foreign key object by
125917** sqlite3FkDelete().
125918*/
125919static Trigger *fkActionTrigger(
125920 Parse *pParse, /* Parse context */
125921 Table *pTab, /* Table being updated or deleted from */
125922 FKey *pFKey, /* Foreign key to get action for */
125923 ExprList *pChanges /* Change-list for UPDATE, NULL for DELETE */
125924){
125925 sqlite3 *db = pParse->db; /* Database handle */
125926 int action; /* One of OE_None, OE_Cascade etc. */
125927 Trigger *pTrigger; /* Trigger definition to return */
125928 int iAction = (pChanges!=0); /* 1 for UPDATE, 0 for DELETE */
125929
125930 action = pFKey->aAction[iAction];
125931 if( action==OE_Restrict7 && (db->flags & SQLITE_DeferFKs0x00080000) ){
125932 return 0;
125933 }
125934 pTrigger = pFKey->apTrigger[iAction];
125935
125936 if( action!=OE_None0 && !pTrigger ){
125937 char const *zFrom; /* Name of child table */
125938 int nFrom; /* Length in bytes of zFrom */
125939 Index *pIdx = 0; /* Parent key index for this FK */
125940 int *aiCol = 0; /* child table cols -> parent key cols */
125941 TriggerStep *pStep = 0; /* First (only) step of trigger program */
125942 Expr *pWhere = 0; /* WHERE clause of trigger step */
125943 ExprList *pList = 0; /* Changes list if ON UPDATE CASCADE */
125944 Select *pSelect = 0; /* If RESTRICT, "SELECT RAISE(...)" */
125945 int i; /* Iterator variable */
125946 Expr *pWhen = 0; /* WHEN clause for the trigger */
125947
125948 if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
125949 assert( aiCol || pFKey->nCol==1 )((void) (0));
125950
125951 for(i=0; i<pFKey->nCol; i++){
125952 Token tOld = { "old", 3 }; /* Literal "old" token */
125953 Token tNew = { "new", 3 }; /* Literal "new" token */
125954 Token tFromCol; /* Name of column in child table */
125955 Token tToCol; /* Name of column in parent table */
125956 int iFromCol; /* Idx of column in child table */
125957 Expr *pEq; /* tFromCol = OLD.tToCol */
125958
125959 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
125960 assert( iFromCol>=0 )((void) (0));
125961 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) )((void) (0));
125962 assert( pIdx==0 || pIdx->aiColumn[i]>=0 )((void) (0));
125963 sqlite3TokenInit(&tToCol,
125964 pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zCnName);
125965 sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zCnName);
125966
125967 /* Create the expression "OLD.zToCol = zFromCol". It is important
125968 ** that the "OLD.zToCol" term is on the LHS of the = operator, so
125969 ** that the affinity and collation sequence associated with the
125970 ** parent table are used for the comparison. */
125971 pEq = sqlite3PExpr(pParse, TK_EQ53,
125972 sqlite3PExpr(pParse, TK_DOT141,
125973 sqlite3ExprAlloc(db, TK_ID59, &tOld, 0),
125974 sqlite3ExprAlloc(db, TK_ID59, &tToCol, 0)),
125975 sqlite3ExprAlloc(db, TK_ID59, &tFromCol, 0)
125976 );
125977 pWhere = sqlite3ExprAnd(pParse, pWhere, pEq);
125978
125979 /* For ON UPDATE, construct the next term of the WHEN clause.
125980 ** The final WHEN clause will be like this:
125981 **
125982 ** WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
125983 */
125984 if( pChanges ){
125985 pEq = sqlite3PExpr(pParse, TK_IS45,
125986 sqlite3PExpr(pParse, TK_DOT141,
125987 sqlite3ExprAlloc(db, TK_ID59, &tOld, 0),
125988 sqlite3ExprAlloc(db, TK_ID59, &tToCol, 0)),
125989 sqlite3PExpr(pParse, TK_DOT141,
125990 sqlite3ExprAlloc(db, TK_ID59, &tNew, 0),
125991 sqlite3ExprAlloc(db, TK_ID59, &tToCol, 0))
125992 );
125993 pWhen = sqlite3ExprAnd(pParse, pWhen, pEq);
125994 }
125995
125996 if( action!=OE_Restrict7 && (action!=OE_Cascade10 || pChanges) ){
125997 Expr *pNew;
125998 if( action==OE_Cascade10 ){
125999 pNew = sqlite3PExpr(pParse, TK_DOT141,
126000 sqlite3ExprAlloc(db, TK_ID59, &tNew, 0),
126001 sqlite3ExprAlloc(db, TK_ID59, &tToCol, 0));
126002 }else if( action==OE_SetDflt9 ){
126003 Column *pCol = pFKey->pFrom->aCol + iFromCol;
126004 Expr *pDflt;
126005 if( pCol->colFlags & COLFLAG_GENERATED0x0060 ){
126006 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
126007 testcase( pCol->colFlags & COLFLAG_STORED );
126008 pDflt = 0;
126009 }else{
126010 pDflt = sqlite3ColumnExpr(pFKey->pFrom, pCol);
126011 }
126012 if( pDflt ){
126013 pNew = sqlite3ExprDup(db, pDflt, 0);
126014 }else{
126015 pNew = sqlite3ExprAlloc(db, TK_NULL121, 0, 0);
126016 }
126017 }else{
126018 pNew = sqlite3ExprAlloc(db, TK_NULL121, 0, 0);
126019 }
126020 pList = sqlite3ExprListAppend(pParse, pList, pNew);
126021 sqlite3ExprListSetName(pParse, pList, &tFromCol, 0);
126022 }
126023 }
126024 sqlite3DbFree(db, aiCol);
126025
126026 zFrom = pFKey->pFrom->zName;
126027 nFrom = sqlite3Strlen30(zFrom);
126028
126029 if( action==OE_Restrict7 ){
126030 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
126031 Token tFrom;
126032 Token tDb;
126033 Expr *pRaise;
126034
126035 tFrom.z = zFrom;
126036 tFrom.n = nFrom;
126037 tDb.z = db->aDb[iDb].zDbSName;
126038 tDb.n = sqlite3Strlen30(tDb.z);
126039
126040 pRaise = sqlite3Expr(db, TK_RAISE71, "FOREIGN KEY constraint failed");
126041 if( pRaise ){
126042 pRaise->affExpr = OE_Abort2;
126043 }
126044 pSelect = sqlite3SelectNew(pParse,
126045 sqlite3ExprListAppend(pParse, 0, pRaise),
126046 sqlite3SrcListAppend(pParse, 0, &tDb, &tFrom),
126047 pWhere,
126048 0, 0, 0, 0, 0
126049 );
126050 pWhere = 0;
126051 }
126052
126053 /* Disable lookaside memory allocation */
126054 DisableLookasidedb->lookaside.bDisable++;db->lookaside.sz=0;
126055
126056 pTrigger = (Trigger *)sqlite3DbMallocZero(db,
126057 sizeof(Trigger) + /* struct Trigger */
126058 sizeof(TriggerStep) + /* Single step in trigger program */
126059 nFrom + 1 /* Space for pStep->zTarget */
126060 );
126061 if( pTrigger ){
126062 pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
126063 pStep->zTarget = (char *)&pStep[1];
126064 memcpy((char *)pStep->zTarget, zFrom, nFrom);
126065
126066 pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE0x0001);
126067 pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE0x0001);
126068 pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE0x0001);
126069 if( pWhen ){
126070 pWhen = sqlite3PExpr(pParse, TK_NOT19, pWhen, 0);
126071 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE0x0001);
126072 }
126073 }
126074
126075 /* Re-enable the lookaside buffer, if it was disabled earlier. */
126076 EnableLookasidedb->lookaside.bDisable--; db->lookaside.sz=db->lookaside
.bDisable?0:db->lookaside.szTrue
;
126077
126078 sqlite3ExprDelete(db, pWhere);
126079 sqlite3ExprDelete(db, pWhen);
126080 sqlite3ExprListDelete(db, pList);
126081 sqlite3SelectDelete(db, pSelect);
126082 if( db->mallocFailed==1 ){
126083 fkTriggerDelete(db, pTrigger);
126084 return 0;
126085 }
126086 assert( pStep!=0 )((void) (0));
126087 assert( pTrigger!=0 )((void) (0));
126088
126089 switch( action ){
126090 case OE_Restrict7:
126091 pStep->op = TK_SELECT138;
126092 break;
126093 case OE_Cascade10:
126094 if( !pChanges ){
126095 pStep->op = TK_DELETE128;
126096 break;
126097 }
126098 /* no break */ deliberate_fall_through
126099 default:
126100 pStep->op = TK_UPDATE129;
126101 }
126102 pStep->pTrig = pTrigger;
126103 pTrigger->pSchema = pTab->pSchema;
126104 pTrigger->pTabSchema = pTab->pSchema;
126105 pFKey->apTrigger[iAction] = pTrigger;
126106 pTrigger->op = (pChanges ? TK_UPDATE129 : TK_DELETE128);
126107 }
126108
126109 return pTrigger;
126110}
126111
126112/*
126113** This function is called when deleting or updating a row to implement
126114** any required CASCADE, SET NULL or SET DEFAULT actions.
126115*/
126116SQLITE_PRIVATEstatic void sqlite3FkActions(
126117 Parse *pParse, /* Parse context */
126118 Table *pTab, /* Table being updated or deleted from */
126119 ExprList *pChanges, /* Change-list for UPDATE, NULL for DELETE */
126120 int regOld, /* Address of array containing old row */
126121 int *aChange, /* Array indicating UPDATEd columns (or 0) */
126122 int bChngRowid /* True if rowid is UPDATEd */
126123){
126124 /* If foreign-key support is enabled, iterate through all FKs that
126125 ** refer to table pTab. If there is an action associated with the FK
126126 ** for this operation (either update or delete), invoke the associated
126127 ** trigger sub-program. */
126128 if( pParse->db->flags&SQLITE_ForeignKeys0x00004000 ){
126129 FKey *pFKey; /* Iterator variable */
126130 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
126131 if( aChange==0 || fkParentIsModified(pTab, pFKey, aChange, bChngRowid) ){
126132 Trigger *pAct = fkActionTrigger(pParse, pTab, pFKey, pChanges);
126133 if( pAct ){
126134 sqlite3CodeRowTriggerDirect(pParse, pAct, pTab, regOld, OE_Abort2, 0);
126135 }
126136 }
126137 }
126138 }
126139}
126140
126141#endif /* ifndef SQLITE_OMIT_TRIGGER */
126142
126143/*
126144** Free all memory associated with foreign key definitions attached to
126145** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
126146** hash table.
126147*/
126148SQLITE_PRIVATEstatic void sqlite3FkDelete(sqlite3 *db, Table *pTab){
126149 FKey *pFKey; /* Iterator variable */
126150 FKey *pNext; /* Copy of pFKey->pNextFrom */
126151
126152 assert( IsOrdinaryTable(pTab) )((void) (0));
126153 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pNext){
126154 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) )((void) (0));
126155
126156 /* Remove the FK from the fkeyHash hash table. */
126157 if( !db || db->pnBytesFreed==0 ){
126158 if( pFKey->pPrevTo ){
126159 pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
126160 }else{
126161 void *p = (void *)pFKey->pNextTo;
126162 const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
126163 sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, p);
126164 }
126165 if( pFKey->pNextTo ){
126166 pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
126167 }
126168 }
126169
126170 /* EV: R-30323-21917 Each foreign key constraint in SQLite is
126171 ** classified as either immediate or deferred.
126172 */
126173 assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 )((void) (0));
126174
126175 /* Delete any triggers created to implement actions for this FK. */
126176#ifndef SQLITE_OMIT_TRIGGER
126177 fkTriggerDelete(db, pFKey->apTrigger[0]);
126178 fkTriggerDelete(db, pFKey->apTrigger[1]);
126179#endif
126180
126181 pNext = pFKey->pNextFrom;
126182 sqlite3DbFree(db, pFKey);
126183 }
126184}
126185#endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */
126186
126187/************** End of fkey.c ************************************************/
126188/************** Begin file insert.c ******************************************/
126189/*
126190** 2001 September 15
126191**
126192** The author disclaims copyright to this source code. In place of
126193** a legal notice, here is a blessing:
126194**
126195** May you do good and not evil.
126196** May you find forgiveness for yourself and forgive others.
126197** May you share freely, never taking more than you give.
126198**
126199*************************************************************************
126200** This file contains C code routines that are called by the parser
126201** to handle INSERT statements in SQLite.
126202*/
126203/* #include "sqliteInt.h" */
126204
126205/*
126206** Generate code that will
126207**
126208** (1) acquire a lock for table pTab then
126209** (2) open pTab as cursor iCur.
126210**
126211** If pTab is a WITHOUT ROWID table, then it is the PRIMARY KEY index
126212** for that table that is actually opened.
126213*/
126214SQLITE_PRIVATEstatic void sqlite3OpenTable(
126215 Parse *pParse, /* Generate code into this VDBE */
126216 int iCur, /* The cursor number of the table */
126217 int iDb, /* The database index in sqlite3.aDb[] */
126218 Table *pTab, /* The table to be opened */
126219 int opcode /* OP_OpenRead or OP_OpenWrite */
126220){
126221 Vdbe *v;
126222 assert( !IsVirtual(pTab) )((void) (0));
126223 assert( pParse->pVdbe!=0 )((void) (0));
126224 v = pParse->pVdbe;
126225 assert( opcode==OP_OpenWrite || opcode==OP_OpenRead )((void) (0));
126226 sqlite3TableLock(pParse, iDb, pTab->tnum,
126227 (opcode==OP_OpenWrite113)?1:0, pTab->zName);
126228 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
126229 sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nNVCol);
126230 VdbeComment((v, "%s", pTab->zName));
126231 }else{
126232 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
126233 assert( pPk!=0 )((void) (0));
126234 assert( pPk->tnum==pTab->tnum || CORRUPT_DB )((void) (0));
126235 sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
126236 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
126237 VdbeComment((v, "%s", pTab->zName));
126238 }
126239}
126240
126241/*
126242** Return a pointer to the column affinity string associated with index
126243** pIdx. A column affinity string has one character for each column in
126244** the table, according to the affinity of the column:
126245**
126246** Character Column affinity
126247** ------------------------------
126248** 'A' BLOB
126249** 'B' TEXT
126250** 'C' NUMERIC
126251** 'D' INTEGER
126252** 'F' REAL
126253**
126254** An extra 'D' is appended to the end of the string to cover the
126255** rowid that appears as the last column in every index.
126256**
126257** Memory for the buffer containing the column index affinity string
126258** is managed along with the rest of the Index structure. It will be
126259** released when sqlite3DeleteIndex() is called.
126260*/
126261SQLITE_PRIVATEstatic const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){
126262 if( !pIdx->zColAff ){
126263 /* The first time a column affinity string for a particular index is
126264 ** required, it is allocated and populated here. It is then stored as
126265 ** a member of the Index structure for subsequent use.
126266 **
126267 ** The column affinity string will eventually be deleted by
126268 ** sqliteDeleteIndex() when the Index structure itself is cleaned
126269 ** up.
126270 */
126271 int n;
126272 Table *pTab = pIdx->pTable;
126273 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
126274 if( !pIdx->zColAff ){
126275 sqlite3OomFault(db);
126276 return 0;
126277 }
126278 for(n=0; n<pIdx->nColumn; n++){
126279 i16 x = pIdx->aiColumn[n];
126280 char aff;
126281 if( x>=0 ){
126282 aff = pTab->aCol[x].affinity;
126283 }else if( x==XN_ROWID(-1) ){
126284 aff = SQLITE_AFF_INTEGER0x44;
126285 }else{
126286 assert( x==XN_EXPR )((void) (0));
126287 assert( pIdx->aColExpr!=0 )((void) (0));
126288 aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
126289 }
126290 if( aff<SQLITE_AFF_BLOB0x41 ) aff = SQLITE_AFF_BLOB0x41;
126291 if( aff>SQLITE_AFF_NUMERIC0x43) aff = SQLITE_AFF_NUMERIC0x43;
126292 pIdx->zColAff[n] = aff;
126293 }
126294 pIdx->zColAff[n] = 0;
126295 }
126296
126297 return pIdx->zColAff;
126298}
126299
126300/*
126301** Make changes to the evolving bytecode to do affinity transformations
126302** of values that are about to be gathered into a row for table pTab.
126303**
126304** For ordinary (legacy, non-strict) tables:
126305** -----------------------------------------
126306**
126307** Compute the affinity string for table pTab, if it has not already been
126308** computed. As an optimization, omit trailing SQLITE_AFF_BLOB affinities.
126309**
126310** If the affinity string is empty (because it was all SQLITE_AFF_BLOB entries
126311** which were then optimized out) then this routine becomes a no-op.
126312**
126313** Otherwise if iReg>0 then code an OP_Affinity opcode that will set the
126314** affinities for register iReg and following. Or if iReg==0,
126315** then just set the P4 operand of the previous opcode (which should be
126316** an OP_MakeRecord) to the affinity string.
126317**
126318** A column affinity string has one character per column:
126319**
126320** Character Column affinity
126321** --------- ---------------
126322** 'A' BLOB
126323** 'B' TEXT
126324** 'C' NUMERIC
126325** 'D' INTEGER
126326** 'E' REAL
126327**
126328** For STRICT tables:
126329** ------------------
126330**
126331** Generate an appropropriate OP_TypeCheck opcode that will verify the
126332** datatypes against the column definitions in pTab. If iReg==0, that
126333** means an OP_MakeRecord opcode has already been generated and should be
126334** the last opcode generated. The new OP_TypeCheck needs to be inserted
126335** before the OP_MakeRecord. The new OP_TypeCheck should use the same
126336** register set as the OP_MakeRecord. If iReg>0 then register iReg is
126337** the first of a series of registers that will form the new record.
126338** Apply the type checking to that array of registers.
126339*/
126340SQLITE_PRIVATEstatic void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
126341 int i, j;
126342 char *zColAff;
126343 if( pTab->tabFlags & TF_Strict0x00010000 ){
126344 if( iReg==0 ){
126345 /* Move the previous opcode (which should be OP_MakeRecord) forward
126346 ** by one slot and insert a new OP_TypeCheck where the current
126347 ** OP_MakeRecord is found */
126348 VdbeOp *pPrev;
126349 sqlite3VdbeAppendP4(v, pTab, P4_TABLE(-5));
126350 pPrev = sqlite3VdbeGetOp(v, -1);
126351 assert( pPrev!=0 )((void) (0));
126352 assert( pPrev->opcode==OP_MakeRecord || sqlite3VdbeDb(v)->mallocFailed )((void) (0));
126353 pPrev->opcode = OP_TypeCheck95;
126354 sqlite3VdbeAddOp3(v, OP_MakeRecord97, pPrev->p1, pPrev->p2, pPrev->p3);
126355 }else{
126356 /* Insert an isolated OP_Typecheck */
126357 sqlite3VdbeAddOp2(v, OP_TypeCheck95, iReg, pTab->nNVCol);
126358 sqlite3VdbeAppendP4(v, pTab, P4_TABLE(-5));
126359 }
126360 return;
126361 }
126362 zColAff = pTab->zColAff;
126363 if( zColAff==0 ){
126364 sqlite3 *db = sqlite3VdbeDb(v);
126365 zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
126366 if( !zColAff ){
126367 sqlite3OomFault(db);
126368 return;
126369 }
126370
126371 for(i=j=0; i<pTab->nCol; i++){
126372 assert( pTab->aCol[i].affinity!=0 || sqlite3VdbeParser(v)->nErr>0 )((void) (0));
126373 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020)==0 ){
126374 zColAff[j++] = pTab->aCol[i].affinity;
126375 }
126376 }
126377 do{
126378 zColAff[j--] = 0;
126379 }while( j>=0 && zColAff[j]<=SQLITE_AFF_BLOB0x41 );
126380 pTab->zColAff = zColAff;
126381 }
126382 assert( zColAff!=0 )((void) (0));
126383 i = sqlite3Strlen30NN(zColAff)(strlen(zColAff)&0x3fffffff);
126384 if( i ){
126385 if( iReg ){
126386 sqlite3VdbeAddOp4(v, OP_Affinity96, iReg, i, 0, zColAff, i);
126387 }else{
126388 assert( sqlite3VdbeGetOp(v, -1)->opcode==OP_MakeRecord((void) (0))
126389 || sqlite3VdbeDb(v)->mallocFailed )((void) (0));
126390 sqlite3VdbeChangeP4(v, -1, zColAff, i);
126391 }
126392 }
126393}
126394
126395/*
126396** Return non-zero if the table pTab in database iDb or any of its indices
126397** have been opened at any point in the VDBE program. This is used to see if
126398** a statement of the form "INSERT INTO <iDb, pTab> SELECT ..." can
126399** run without using a temporary table for the results of the SELECT.
126400*/
126401static int readsTable(Parse *p, int iDb, Table *pTab){
126402 Vdbe *v = sqlite3GetVdbe(p);
126403 int i;
126404 int iEnd = sqlite3VdbeCurrentAddr(v);
126405#ifndef SQLITE_OMIT_VIRTUALTABLE
126406 VTable *pVTab = IsVirtual(pTab)((pTab)->eTabType==1) ? sqlite3GetVTable(p->db, pTab) : 0;
126407#endif
126408
126409 for(i=1; i<iEnd; i++){
126410 VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
126411 assert( pOp!=0 )((void) (0));
126412 if( pOp->opcode==OP_OpenRead112 && pOp->p3==iDb ){
126413 Index *pIndex;
126414 Pgno tnum = pOp->p2;
126415 if( tnum==pTab->tnum ){
126416 return 1;
126417 }
126418 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
126419 if( tnum==pIndex->tnum ){
126420 return 1;
126421 }
126422 }
126423 }
126424#ifndef SQLITE_OMIT_VIRTUALTABLE
126425 if( pOp->opcode==OP_VOpen173 && pOp->p4.pVtab==pVTab ){
126426 assert( pOp->p4.pVtab!=0 )((void) (0));
126427 assert( pOp->p4type==P4_VTAB )((void) (0));
126428 return 1;
126429 }
126430#endif
126431 }
126432 return 0;
126433}
126434
126435/* This walker callback will compute the union of colFlags flags for all
126436** referenced columns in a CHECK constraint or generated column expression.
126437*/
126438static int exprColumnFlagUnion(Walker *pWalker, Expr *pExpr){
126439 if( pExpr->op==TK_COLUMN167 && pExpr->iColumn>=0 ){
126440 assert( pExpr->iColumn < pWalker->u.pTab->nCol )((void) (0));
126441 pWalker->eCode |= pWalker->u.pTab->aCol[pExpr->iColumn].colFlags;
126442 }
126443 return WRC_Continue0;
126444}
126445
126446#ifndef SQLITE_OMIT_GENERATED_COLUMNS
126447/*
126448** All regular columns for table pTab have been puts into registers
126449** starting with iRegStore. The registers that correspond to STORED
126450** or VIRTUAL columns have not yet been initialized. This routine goes
126451** back and computes the values for those columns based on the previously
126452** computed normal columns.
126453*/
126454SQLITE_PRIVATEstatic void sqlite3ComputeGeneratedColumns(
126455 Parse *pParse, /* Parsing context */
126456 int iRegStore, /* Register holding the first column */
126457 Table *pTab /* The table */
126458){
126459 int i;
126460 Walker w;
126461 Column *pRedo;
126462 int eProgress;
126463 VdbeOp *pOp;
126464
126465 assert( pTab->tabFlags & TF_HasGenerated )((void) (0));
126466 testcase( pTab->tabFlags & TF_HasVirtual );
126467 testcase( pTab->tabFlags & TF_HasStored );
126468
126469 /* Before computing generated columns, first go through and make sure
126470 ** that appropriate affinity has been applied to the regular columns
126471 */
126472 sqlite3TableAffinity(pParse->pVdbe, pTab, iRegStore);
126473 if( (pTab->tabFlags & TF_HasStored0x00000040)!=0 ){
126474 pOp = sqlite3VdbeGetOp(pParse->pVdbe,-1);
126475 if( pOp->opcode==OP_Affinity96 ){
126476 /* Change the OP_Affinity argument to '@' (NONE) for all stored
126477 ** columns. '@' is the no-op affinity and those columns have not
126478 ** yet been computed. */
126479 int ii, jj;
126480 char *zP4 = pOp->p4.z;
126481 assert( zP4!=0 )((void) (0));
126482 assert( pOp->p4type==P4_DYNAMIC )((void) (0));
126483 for(ii=jj=0; zP4[jj]; ii++){
126484 if( pTab->aCol[ii].colFlags & COLFLAG_VIRTUAL0x0020 ){
126485 continue;
126486 }
126487 if( pTab->aCol[ii].colFlags & COLFLAG_STORED0x0040 ){
126488 zP4[jj] = SQLITE_AFF_NONE0x40;
126489 }
126490 jj++;
126491 }
126492 }else if( pOp->opcode==OP_TypeCheck95 ){
126493 /* If an OP_TypeCheck was generated because the table is STRICT,
126494 ** then set the P3 operand to indicate that generated columns should
126495 ** not be checked */
126496 pOp->p3 = 1;
126497 }
126498 }
126499
126500 /* Because there can be multiple generated columns that refer to one another,
126501 ** this is a two-pass algorithm. On the first pass, mark all generated
126502 ** columns as "not available".
126503 */
126504 for(i=0; i<pTab->nCol; i++){
126505 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED0x0060 ){
126506 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
126507 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
126508 pTab->aCol[i].colFlags |= COLFLAG_NOTAVAIL0x0080;
126509 }
126510 }
126511
126512 w.u.pTab = pTab;
126513 w.xExprCallback = exprColumnFlagUnion;
126514 w.xSelectCallback = 0;
126515 w.xSelectCallback2 = 0;
126516
126517 /* On the second pass, compute the value of each NOT-AVAILABLE column.
126518 ** Companion code in the TK_COLUMN case of sqlite3ExprCodeTarget() will
126519 ** compute dependencies and mark remove the COLSPAN_NOTAVAIL mark, as
126520 ** they are needed.
126521 */
126522 pParse->iSelfTab = -iRegStore;
126523 do{
126524 eProgress = 0;
126525 pRedo = 0;
126526 for(i=0; i<pTab->nCol; i++){
126527 Column *pCol = pTab->aCol + i;
126528 if( (pCol->colFlags & COLFLAG_NOTAVAIL0x0080)!=0 ){
126529 int x;
126530 pCol->colFlags |= COLFLAG_BUSY0x0100;
126531 w.eCode = 0;
126532 sqlite3WalkExpr(&w, sqlite3ColumnExpr(pTab, pCol));
126533 pCol->colFlags &= ~COLFLAG_BUSY0x0100;
126534 if( w.eCode & COLFLAG_NOTAVAIL0x0080 ){
126535 pRedo = pCol;
126536 continue;
126537 }
126538 eProgress = 1;
126539 assert( pCol->colFlags & COLFLAG_GENERATED )((void) (0));
126540 x = sqlite3TableColumnToStorage(pTab, i) + iRegStore;
126541 sqlite3ExprCodeGeneratedColumn(pParse, pTab, pCol, x);
126542 pCol->colFlags &= ~COLFLAG_NOTAVAIL0x0080;
126543 }
126544 }
126545 }while( pRedo && eProgress );
126546 if( pRedo ){
126547 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", pRedo->zCnName);
126548 }
126549 pParse->iSelfTab = 0;
126550}
126551#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
126552
126553
126554#ifndef SQLITE_OMIT_AUTOINCREMENT
126555/*
126556** Locate or create an AutoincInfo structure associated with table pTab
126557** which is in database iDb. Return the register number for the register
126558** that holds the maximum rowid. Return zero if pTab is not an AUTOINCREMENT
126559** table. (Also return zero when doing a VACUUM since we do not want to
126560** update the AUTOINCREMENT counters during a VACUUM.)
126561**
126562** There is at most one AutoincInfo structure per table even if the
126563** same table is autoincremented multiple times due to inserts within
126564** triggers. A new AutoincInfo structure is created if this is the
126565** first use of table pTab. On 2nd and subsequent uses, the original
126566** AutoincInfo structure is used.
126567**
126568** Four consecutive registers are allocated:
126569**
126570** (1) The name of the pTab table.
126571** (2) The maximum ROWID of pTab.
126572** (3) The rowid in sqlite_sequence of pTab
126573** (4) The original value of the max ROWID in pTab, or NULL if none
126574**
126575** The 2nd register is the one that is returned. That is all the
126576** insert routine needs to know about.
126577*/
126578static int autoIncBegin(
126579 Parse *pParse, /* Parsing context */
126580 int iDb, /* Index of the database holding pTab */
126581 Table *pTab /* The table we are writing to */
126582){
126583 int memId = 0; /* Register holding maximum rowid */
126584 assert( pParse->db->aDb[iDb].pSchema!=0 )((void) (0));
126585 if( (pTab->tabFlags & TF_Autoincrement0x00000008)!=0
126586 && (pParse->db->mDbFlags & DBFLAG_Vacuum0x0004)==0
126587 ){
126588 Parse *pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
126589 AutoincInfo *pInfo;
126590 Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
126591
126592 /* Verify that the sqlite_sequence table exists and is an ordinary
126593 ** rowid table with exactly two columns.
126594 ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
126595 if( pSeqTab==0
126596 || !HasRowid(pSeqTab)(((pSeqTab)->tabFlags & 0x00000080)==0)
126597 || NEVER(IsVirtual(pSeqTab))(((pSeqTab)->eTabType==1))
126598 || pSeqTab->nCol!=2
126599 ){
126600 pParse->nErr++;
126601 pParse->rc = SQLITE_CORRUPT_SEQUENCE(11 | (2<<8));
126602 return 0;
126603 }
126604
126605 pInfo = pToplevel->pAinc;
126606 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
126607 if( pInfo==0 ){
126608 pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
126609 sqlite3ParserAddCleanup(pToplevel, sqlite3DbFree, pInfo);
126610 testcase( pParse->earlyCleanup );
126611 if( pParse->db->mallocFailed ) return 0;
126612 pInfo->pNext = pToplevel->pAinc;
126613 pToplevel->pAinc = pInfo;
126614 pInfo->pTab = pTab;
126615 pInfo->iDb = iDb;
126616 pToplevel->nMem++; /* Register to hold name of table */
126617 pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
126618 pToplevel->nMem +=2; /* Rowid in sqlite_sequence + orig max val */
126619 }
126620 memId = pInfo->regCtr;
126621 }
126622 return memId;
126623}
126624
126625/*
126626** This routine generates code that will initialize all of the
126627** register used by the autoincrement tracker.
126628*/
126629SQLITE_PRIVATEstatic void sqlite3AutoincrementBegin(Parse *pParse){
126630 AutoincInfo *p; /* Information about an AUTOINCREMENT */
126631 sqlite3 *db = pParse->db; /* The database connection */
126632 Db *pDb; /* Database only autoinc table */
126633 int memId; /* Register holding max rowid */
126634 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
126635
126636 /* This routine is never called during trigger-generation. It is
126637 ** only called from the top-level */
126638 assert( pParse->pTriggerTab==0 )((void) (0));
126639 assert( sqlite3IsToplevel(pParse) )((void) (0));
126640
126641 assert( v )((void) (0)); /* We failed long ago if this is not so */
126642 for(p = pParse->pAinc; p; p = p->pNext){
126643 static const int iLn = VDBE_OFFSET_LINENO(2)0;
126644 static const VdbeOpList autoInc[] = {
126645 /* 0 */ {OP_Null75, 0, 0, 0},
126646 /* 1 */ {OP_Rewind35, 0, 10, 0},
126647 /* 2 */ {OP_Column94, 0, 0, 0},
126648 /* 3 */ {OP_Ne52, 0, 9, 0},
126649 /* 4 */ {OP_Rowid135, 0, 0, 0},
126650 /* 5 */ {OP_Column94, 0, 1, 0},
126651 /* 6 */ {OP_AddImm86, 0, 0, 0},
126652 /* 7 */ {OP_Copy80, 0, 0, 0},
126653 /* 8 */ {OP_Goto8, 0, 11, 0},
126654 /* 9 */ {OP_Next38, 0, 2, 0},
126655 /* 10 */ {OP_Integer71, 0, 0, 0},
126656 /* 11 */ {OP_Close122, 0, 0, 0}
126657 };
126658 VdbeOp *aOp;
126659 pDb = &db->aDb[p->iDb];
126660 memId = p->regCtr;
126661 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) )((void) (0));
126662 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead112);
126663 sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
126664 aOp = sqlite3VdbeAddOpList(v, ArraySize(autoInc)((int)(sizeof(autoInc)/sizeof(autoInc[0]))), autoInc, iLn);
126665 if( aOp==0 ) break;
126666 aOp[0].p2 = memId;
126667 aOp[0].p3 = memId+2;
126668 aOp[2].p3 = memId;
126669 aOp[3].p1 = memId-1;
126670 aOp[3].p3 = memId;
126671 aOp[3].p5 = SQLITE_JUMPIFNULL0x10;
126672 aOp[4].p2 = memId+1;
126673 aOp[5].p3 = memId;
126674 aOp[6].p1 = memId;
126675 aOp[7].p2 = memId+2;
126676 aOp[7].p1 = memId;
126677 aOp[10].p2 = memId;
126678 if( pParse->nTab==0 ) pParse->nTab = 1;
126679 }
126680}
126681
126682/*
126683** Update the maximum rowid for an autoincrement calculation.
126684**
126685** This routine should be called when the regRowid register holds a
126686** new rowid that is about to be inserted. If that new rowid is
126687** larger than the maximum rowid in the memId memory cell, then the
126688** memory cell is updated.
126689*/
126690static void autoIncStep(Parse *pParse, int memId, int regRowid){
126691 if( memId>0 ){
126692 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax159, memId, regRowid);
126693 }
126694}
126695
126696/*
126697** This routine generates the code needed to write autoincrement
126698** maximum rowid values back into the sqlite_sequence register.
126699** Every statement that might do an INSERT into an autoincrement
126700** table (either directly or through triggers) needs to call this
126701** routine just before the "exit" code.
126702*/
126703static SQLITE_NOINLINE__attribute__((noinline)) void autoIncrementEnd(Parse *pParse){
126704 AutoincInfo *p;
126705 Vdbe *v = pParse->pVdbe;
126706 sqlite3 *db = pParse->db;
126707
126708 assert( v )((void) (0));
126709 for(p = pParse->pAinc; p; p = p->pNext){
126710 static const int iLn = VDBE_OFFSET_LINENO(2)0;
126711 static const VdbeOpList autoIncEnd[] = {
126712 /* 0 */ {OP_NotNull51, 0, 2, 0},
126713 /* 1 */ {OP_NewRowid127, 0, 0, 0},
126714 /* 2 */ {OP_MakeRecord97, 0, 2, 0},
126715 /* 3 */ {OP_Insert128, 0, 0, 0},
126716 /* 4 */ {OP_Close122, 0, 0, 0}
126717 };
126718 VdbeOp *aOp;
126719 Db *pDb = &db->aDb[p->iDb];
126720 int iRec;
126721 int memId = p->regCtr;
126722
126723 iRec = sqlite3GetTempReg(pParse);
126724 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) )((void) (0));
126725 sqlite3VdbeAddOp3(v, OP_Le55, memId+2, sqlite3VdbeCurrentAddr(v)+7, memId);
126726 VdbeCoverage(v);
126727 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite113);
126728 aOp = sqlite3VdbeAddOpList(v, ArraySize(autoIncEnd)((int)(sizeof(autoIncEnd)/sizeof(autoIncEnd[0]))), autoIncEnd, iLn);
126729 if( aOp==0 ) break;
126730 aOp[0].p1 = memId+1;
126731 aOp[1].p2 = memId+1;
126732 aOp[2].p1 = memId-1;
126733 aOp[2].p3 = iRec;
126734 aOp[3].p2 = iRec;
126735 aOp[3].p3 = memId+1;
126736 aOp[3].p5 = OPFLAG_APPEND0x08;
126737 sqlite3ReleaseTempReg(pParse, iRec);
126738 }
126739}
126740SQLITE_PRIVATEstatic void sqlite3AutoincrementEnd(Parse *pParse){
126741 if( pParse->pAinc ) autoIncrementEnd(pParse);
126742}
126743#else
126744/*
126745** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
126746** above are all no-ops
126747*/
126748# define autoIncBegin(A,B,C) (0)
126749# define autoIncStep(A,B,C)
126750#endif /* SQLITE_OMIT_AUTOINCREMENT */
126751
126752
126753/* Forward declaration */
126754static int xferOptimization(
126755 Parse *pParse, /* Parser context */
126756 Table *pDest, /* The table we are inserting into */
126757 Select *pSelect, /* A SELECT statement to use as the data source */
126758 int onError, /* How to handle constraint errors */
126759 int iDbDest /* The database of pDest */
126760);
126761
126762/*
126763** This routine is called to handle SQL of the following forms:
126764**
126765** insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),...
126766** insert into TABLE (IDLIST) select
126767** insert into TABLE (IDLIST) default values
126768**
126769** The IDLIST following the table name is always optional. If omitted,
126770** then a list of all (non-hidden) columns for the table is substituted.
126771** The IDLIST appears in the pColumn parameter. pColumn is NULL if IDLIST
126772** is omitted.
126773**
126774** For the pSelect parameter holds the values to be inserted for the
126775** first two forms shown above. A VALUES clause is really just short-hand
126776** for a SELECT statement that omits the FROM clause and everything else
126777** that follows. If the pSelect parameter is NULL, that means that the
126778** DEFAULT VALUES form of the INSERT statement is intended.
126779**
126780** The code generated follows one of four templates. For a simple
126781** insert with data coming from a single-row VALUES clause, the code executes
126782** once straight down through. Pseudo-code follows (we call this
126783** the "1st template"):
126784**
126785** open write cursor to <table> and its indices
126786** put VALUES clause expressions into registers
126787** write the resulting record into <table>
126788** cleanup
126789**
126790** The three remaining templates assume the statement is of the form
126791**
126792** INSERT INTO <table> SELECT ...
126793**
126794** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
126795** in other words if the SELECT pulls all columns from a single table
126796** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
126797** if <table2> and <table1> are distinct tables but have identical
126798** schemas, including all the same indices, then a special optimization
126799** is invoked that copies raw records from <table2> over to <table1>.
126800** See the xferOptimization() function for the implementation of this
126801** template. This is the 2nd template.
126802**
126803** open a write cursor to <table>
126804** open read cursor on <table2>
126805** transfer all records in <table2> over to <table>
126806** close cursors
126807** foreach index on <table>
126808** open a write cursor on the <table> index
126809** open a read cursor on the corresponding <table2> index
126810** transfer all records from the read to the write cursors
126811** close cursors
126812** end foreach
126813**
126814** The 3rd template is for when the second template does not apply
126815** and the SELECT clause does not read from <table> at any time.
126816** The generated code follows this template:
126817**
126818** X <- A
126819** goto B
126820** A: setup for the SELECT
126821** loop over the rows in the SELECT
126822** load values into registers R..R+n
126823** yield X
126824** end loop
126825** cleanup after the SELECT
126826** end-coroutine X
126827** B: open write cursor to <table> and its indices
126828** C: yield X, at EOF goto D
126829** insert the select result into <table> from R..R+n
126830** goto C
126831** D: cleanup
126832**
126833** The 4th template is used if the insert statement takes its
126834** values from a SELECT but the data is being inserted into a table
126835** that is also read as part of the SELECT. In the third form,
126836** we have to use an intermediate table to store the results of
126837** the select. The template is like this:
126838**
126839** X <- A
126840** goto B
126841** A: setup for the SELECT
126842** loop over the tables in the SELECT
126843** load value into register R..R+n
126844** yield X
126845** end loop
126846** cleanup after the SELECT
126847** end co-routine R
126848** B: open temp table
126849** L: yield X, at EOF goto M
126850** insert row from R..R+n into temp table
126851** goto L
126852** M: open write cursor to <table> and its indices
126853** rewind temp table
126854** C: loop over rows of intermediate table
126855** transfer values form intermediate table into <table>
126856** end loop
126857** D: cleanup
126858*/
126859SQLITE_PRIVATEstatic void sqlite3Insert(
126860 Parse *pParse, /* Parser context */
126861 SrcList *pTabList, /* Name of table into which we are inserting */
126862 Select *pSelect, /* A SELECT statement to use as the data source */
126863 IdList *pColumn, /* Column names corresponding to IDLIST, or NULL. */
126864 int onError, /* How to handle constraint errors */
126865 Upsert *pUpsert /* ON CONFLICT clauses for upsert, or NULL */
126866){
126867 sqlite3 *db; /* The main database structure */
126868 Table *pTab; /* The table to insert into. aka TABLE */
126869 int i, j; /* Loop counters */
126870 Vdbe *v; /* Generate code into this virtual machine */
126871 Index *pIdx; /* For looping over indices of the table */
126872 int nColumn; /* Number of columns in the data */
126873 int nHidden = 0; /* Number of hidden columns if TABLE is virtual */
126874 int iDataCur = 0; /* VDBE cursor that is the main data repository */
126875 int iIdxCur = 0; /* First index cursor */
126876 int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
126877 int endOfLoop; /* Label for the end of the insertion loop */
126878 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */
126879 int addrInsTop = 0; /* Jump to label "D" */
126880 int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */
126881 SelectDest dest; /* Destination for SELECT on rhs of INSERT */
126882 int iDb; /* Index of database holding TABLE */
126883 u8 useTempTable = 0; /* Store SELECT results in intermediate table */
126884 u8 appendFlag = 0; /* True if the insert is likely to be an append */
126885 u8 withoutRowid; /* 0 for normal table. 1 for WITHOUT ROWID table */
126886 u8 bIdListInOrder; /* True if IDLIST is in table order */
126887 ExprList *pList = 0; /* List of VALUES() to be inserted */
126888 int iRegStore; /* Register in which to store next column */
126889
126890 /* Register allocations */
126891 int regFromSelect = 0;/* Base register for data coming from SELECT */
126892 int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */
126893 int regRowCount = 0; /* Memory cell used for the row counter */
126894 int regIns; /* Block of regs holding rowid+data being inserted */
126895 int regRowid; /* registers holding insert rowid */
126896 int regData; /* register holding first column to insert */
126897 int *aRegIdx = 0; /* One register allocated to each index */
126898
126899#ifndef SQLITE_OMIT_TRIGGER
126900 int isView; /* True if attempting to insert into a view */
126901 Trigger *pTrigger; /* List of triggers on pTab, if required */
126902 int tmask; /* Mask of trigger times */
126903#endif
126904
126905 db = pParse->db;
126906 assert( db->pParse==pParse )((void) (0));
126907 if( pParse->nErr ){
126908 goto insert_cleanup;
126909 }
126910 assert( db->mallocFailed==0 )((void) (0));
126911 dest.iSDParm = 0; /* Suppress a harmless compiler warning */
126912
126913 /* If the Select object is really just a simple VALUES() list with a
126914 ** single row (the common case) then keep that one row of values
126915 ** and discard the other (unused) parts of the pSelect object
126916 */
126917 if( pSelect && (pSelect->selFlags & SF_Values0x0000200)!=0 && pSelect->pPrior==0 ){
126918 pList = pSelect->pEList;
126919 pSelect->pEList = 0;
126920 sqlite3SelectDelete(db, pSelect);
126921 pSelect = 0;
126922 }
126923
126924 /* Locate the table into which we will be inserting new information.
126925 */
126926 assert( pTabList->nSrc==1 )((void) (0));
126927 pTab = sqlite3SrcListLookup(pParse, pTabList);
126928 if( pTab==0 ){
126929 goto insert_cleanup;
126930 }
126931 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
126932 assert( iDb<db->nDb )((void) (0));
126933 if( sqlite3AuthCheck(pParse, SQLITE_INSERT18, pTab->zName, 0,
126934 db->aDb[iDb].zDbSName) ){
126935 goto insert_cleanup;
126936 }
126937 withoutRowid = !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0);
126938
126939 /* Figure out if we have any triggers and if the table being
126940 ** inserted into is a view
126941 */
126942#ifndef SQLITE_OMIT_TRIGGER
126943 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT127, 0, &tmask);
126944 isView = IsView(pTab)((pTab)->eTabType==2);
126945#else
126946# define pTrigger 0
126947# define tmask 0
126948# define isView 0
126949#endif
126950#ifdef SQLITE_OMIT_VIEW
126951# undef isView
126952# define isView 0
126953#endif
126954 assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) )((void) (0));
126955
126956#if TREETRACE_ENABLED0
126957 if( sqlite3TreeTrace & 0x10000 ){
126958 sqlite3TreeViewLine(0, "In sqlite3Insert() at %s:%d", __FILE__"3rdparty/sqlite3/sqlite3.c", __LINE__126958);
126959 sqlite3TreeViewInsert(pParse->pWith, pTabList, pColumn, pSelect, pList,
126960 onError, pUpsert, pTrigger);
126961 }
126962#endif
126963
126964 /* If pTab is really a view, make sure it has been initialized.
126965 ** ViewGetColumnNames() is a no-op if pTab is not a view.
126966 */
126967 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
126968 goto insert_cleanup;
126969 }
126970
126971 /* Cannot insert into a read-only table.
126972 */
126973 if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
126974 goto insert_cleanup;
126975 }
126976
126977 /* Allocate a VDBE
126978 */
126979 v = sqlite3GetVdbe(pParse);
126980 if( v==0 ) goto insert_cleanup;
126981 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
126982 sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
126983
126984#ifndef SQLITE_OMIT_XFER_OPT
126985 /* If the statement is of the form
126986 **
126987 ** INSERT INTO <table1> SELECT * FROM <table2>;
126988 **
126989 ** Then special optimizations can be applied that make the transfer
126990 ** very fast and which reduce fragmentation of indices.
126991 **
126992 ** This is the 2nd template.
126993 */
126994 if( pColumn==0
126995 && pSelect!=0
126996 && pTrigger==0
126997 && xferOptimization(pParse, pTab, pSelect, onError, iDb)
126998 ){
126999 assert( !pTrigger )((void) (0));
127000 assert( pList==0 )((void) (0));
127001 goto insert_end;
127002 }
127003#endif /* SQLITE_OMIT_XFER_OPT */
127004
127005 /* If this is an AUTOINCREMENT table, look up the sequence number in the
127006 ** sqlite_sequence table and store it in memory cell regAutoinc.
127007 */
127008 regAutoinc = autoIncBegin(pParse, iDb, pTab);
127009
127010 /* Allocate a block registers to hold the rowid and the values
127011 ** for all columns of the new row.
127012 */
127013 regRowid = regIns = pParse->nMem+1;
127014 pParse->nMem += pTab->nCol + 1;
127015 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
127016 regRowid++;
127017 pParse->nMem++;
127018 }
127019 regData = regRowid+1;
127020
127021 /* If the INSERT statement included an IDLIST term, then make sure
127022 ** all elements of the IDLIST really are columns of the table and
127023 ** remember the column indices.
127024 **
127025 ** If the table has an INTEGER PRIMARY KEY column and that column
127026 ** is named in the IDLIST, then record in the ipkColumn variable
127027 ** the index into IDLIST of the primary key column. ipkColumn is
127028 ** the index of the primary key as it appears in IDLIST, not as
127029 ** is appears in the original table. (The index of the INTEGER
127030 ** PRIMARY KEY in the original table is pTab->iPKey.) After this
127031 ** loop, if ipkColumn==(-1), that means that integer primary key
127032 ** is unspecified, and hence the table is either WITHOUT ROWID or
127033 ** it will automatically generated an integer primary key.
127034 **
127035 ** bIdListInOrder is true if the columns in IDLIST are in storage
127036 ** order. This enables an optimization that avoids shuffling the
127037 ** columns into storage order. False negatives are harmless,
127038 ** but false positives will cause database corruption.
127039 */
127040 bIdListInOrder = (pTab->tabFlags & (TF_OOOHidden0x00000400|TF_HasStored0x00000040))==0;
127041 if( pColumn ){
127042 assert( pColumn->eU4!=EU4_EXPR )((void) (0));
127043 pColumn->eU4 = EU4_IDX1;
127044 for(i=0; i<pColumn->nId; i++){
127045 pColumn->a[i].u4.idx = -1;
127046 }
127047 for(i=0; i<pColumn->nId; i++){
127048 for(j=0; j<pTab->nCol; j++){
127049 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zCnName)==0 ){
127050 pColumn->a[i].u4.idx = j;
127051 if( i!=j ) bIdListInOrder = 0;
127052 if( j==pTab->iPKey ){
127053 ipkColumn = i; assert( !withoutRowid )((void) (0));
127054 }
127055#ifndef SQLITE_OMIT_GENERATED_COLUMNS
127056 if( pTab->aCol[j].colFlags & (COLFLAG_STORED0x0040|COLFLAG_VIRTUAL0x0020) ){
127057 sqlite3ErrorMsg(pParse,
127058 "cannot INSERT into generated column \"%s\"",
127059 pTab->aCol[j].zCnName);
127060 goto insert_cleanup;
127061 }
127062#endif
127063 break;
127064 }
127065 }
127066 if( j>=pTab->nCol ){
127067 if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
127068 ipkColumn = i;
127069 bIdListInOrder = 0;
127070 }else{
127071 sqlite3ErrorMsg(pParse, "table %S has no column named %s",
127072 pTabList->a, pColumn->a[i].zName);
127073 pParse->checkSchema = 1;
127074 goto insert_cleanup;
127075 }
127076 }
127077 }
127078 }
127079
127080 /* Figure out how many columns of data are supplied. If the data
127081 ** is coming from a SELECT statement, then generate a co-routine that
127082 ** produces a single row of the SELECT on each invocation. The
127083 ** co-routine is the common header to the 3rd and 4th templates.
127084 */
127085 if( pSelect ){
127086 /* Data is coming from a SELECT or from a multi-row VALUES clause.
127087 ** Generate a co-routine to run the SELECT. */
127088 int regYield; /* Register holding co-routine entry-point */
127089 int addrTop; /* Top of the co-routine */
127090 int rc; /* Result code */
127091
127092 regYield = ++pParse->nMem;
127093 addrTop = sqlite3VdbeCurrentAddr(v) + 1;
127094 sqlite3VdbeAddOp3(v, OP_InitCoroutine10, regYield, 0, addrTop);
127095 sqlite3SelectDestInit(&dest, SRT_Coroutine13, regYield);
127096 dest.iSdst = bIdListInOrder ? regData : 0;
127097 dest.nSdst = pTab->nCol;
127098 rc = sqlite3Select(pParse, pSelect, &dest);
127099 regFromSelect = dest.iSdst;
127100 assert( db->pParse==pParse )((void) (0));
127101 if( rc || pParse->nErr ) goto insert_cleanup;
127102 assert( db->mallocFailed==0 )((void) (0));
127103 sqlite3VdbeEndCoroutine(v, regYield);
127104 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
127105 assert( pSelect->pEList )((void) (0));
127106 nColumn = pSelect->pEList->nExpr;
127107
127108 /* Set useTempTable to TRUE if the result of the SELECT statement
127109 ** should be written into a temporary table (template 4). Set to
127110 ** FALSE if each output row of the SELECT can be written directly into
127111 ** the destination table (template 3).
127112 **
127113 ** A temp table must be used if the table being updated is also one
127114 ** of the tables being read by the SELECT statement. Also use a
127115 ** temp table in the case of row triggers.
127116 */
127117 if( pTrigger || readsTable(pParse, iDb, pTab) ){
127118 useTempTable = 1;
127119 }
127120
127121 if( useTempTable ){
127122 /* Invoke the coroutine to extract information from the SELECT
127123 ** and add it to a transient table srcTab. The code generated
127124 ** here is from the 4th template:
127125 **
127126 ** B: open temp table
127127 ** L: yield X, goto M at EOF
127128 ** insert row from R..R+n into temp table
127129 ** goto L
127130 ** M: ...
127131 */
127132 int regRec; /* Register to hold packed record */
127133 int regTempRowid; /* Register to hold temp table ROWID */
127134 int addrL; /* Label "L" */
127135
127136 srcTab = pParse->nTab++;
127137 regRec = sqlite3GetTempReg(pParse);
127138 regTempRowid = sqlite3GetTempReg(pParse);
127139 sqlite3VdbeAddOp2(v, OP_OpenEphemeral118, srcTab, nColumn);
127140 addrL = sqlite3VdbeAddOp1(v, OP_Yield11, dest.iSDParm); VdbeCoverage(v);
127141 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regFromSelect, nColumn, regRec);
127142 sqlite3VdbeAddOp2(v, OP_NewRowid127, srcTab, regTempRowid);
127143 sqlite3VdbeAddOp3(v, OP_Insert128, srcTab, regRec, regTempRowid);
127144 sqlite3VdbeGoto(v, addrL);
127145 sqlite3VdbeJumpHere(v, addrL);
127146 sqlite3ReleaseTempReg(pParse, regRec);
127147 sqlite3ReleaseTempReg(pParse, regTempRowid);
127148 }
127149 }else{
127150 /* This is the case if the data for the INSERT is coming from a
127151 ** single-row VALUES clause
127152 */
127153 NameContext sNC;
127154 memset(&sNC, 0, sizeof(sNC));
127155 sNC.pParse = pParse;
127156 srcTab = -1;
127157 assert( useTempTable==0 )((void) (0));
127158 if( pList ){
127159 nColumn = pList->nExpr;
127160 if( sqlite3ResolveExprListNames(&sNC, pList) ){
127161 goto insert_cleanup;
127162 }
127163 }else{
127164 nColumn = 0;
127165 }
127166 }
127167
127168 /* If there is no IDLIST term but the table has an integer primary
127169 ** key, the set the ipkColumn variable to the integer primary key
127170 ** column index in the original table definition.
127171 */
127172 if( pColumn==0 && nColumn>0 ){
127173 ipkColumn = pTab->iPKey;
127174#ifndef SQLITE_OMIT_GENERATED_COLUMNS
127175 if( ipkColumn>=0 && (pTab->tabFlags & TF_HasGenerated0x00000060)!=0 ){
127176 testcase( pTab->tabFlags & TF_HasVirtual );
127177 testcase( pTab->tabFlags & TF_HasStored );
127178 for(i=ipkColumn-1; i>=0; i--){
127179 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED0x0060 ){
127180 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
127181 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
127182 ipkColumn--;
127183 }
127184 }
127185 }
127186#endif
127187
127188 /* Make sure the number of columns in the source data matches the number
127189 ** of columns to be inserted into the table.
127190 */
127191 assert( TF_HasHidden==COLFLAG_HIDDEN )((void) (0));
127192 assert( TF_HasGenerated==COLFLAG_GENERATED )((void) (0));
127193 assert( COLFLAG_NOINSERT==(COLFLAG_GENERATED|COLFLAG_HIDDEN) )((void) (0));
127194 if( (pTab->tabFlags & (TF_HasGenerated0x00000060|TF_HasHidden0x00000002))!=0 ){
127195 for(i=0; i<pTab->nCol; i++){
127196 if( pTab->aCol[i].colFlags & COLFLAG_NOINSERT0x0062 ) nHidden++;
127197 }
127198 }
127199 if( nColumn!=(pTab->nCol-nHidden) ){
127200 sqlite3ErrorMsg(pParse,
127201 "table %S has %d columns but %d values were supplied",
127202 pTabList->a, pTab->nCol-nHidden, nColumn);
127203 goto insert_cleanup;
127204 }
127205 }
127206 if( pColumn!=0 && nColumn!=pColumn->nId ){
127207 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
127208 goto insert_cleanup;
127209 }
127210
127211 /* Initialize the count of rows to be inserted
127212 */
127213 if( (db->flags & SQLITE_CountRows((u64)(0x00001)<<32))!=0
127214 && !pParse->nested
127215 && !pParse->pTriggerTab
127216 && !pParse->bReturning
127217 ){
127218 regRowCount = ++pParse->nMem;
127219 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regRowCount);
127220 }
127221
127222 /* If this is not a view, open the table and and all indices */
127223 if( !isView ){
127224 int nIdx;
127225 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite113, 0, -1, 0,
127226 &iDataCur, &iIdxCur);
127227 aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+2));
127228 if( aRegIdx==0 ){
127229 goto insert_cleanup;
127230 }
127231 for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
127232 assert( pIdx )((void) (0));
127233 aRegIdx[i] = ++pParse->nMem;
127234 pParse->nMem += pIdx->nColumn;
127235 }
127236 aRegIdx[i] = ++pParse->nMem; /* Register to store the table record */
127237 }
127238#ifndef SQLITE_OMIT_UPSERT
127239 if( pUpsert ){
127240 Upsert *pNx;
127241 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
127242 sqlite3ErrorMsg(pParse, "UPSERT not implemented for virtual table \"%s\"",
127243 pTab->zName);
127244 goto insert_cleanup;
127245 }
127246 if( IsView(pTab)((pTab)->eTabType==2) ){
127247 sqlite3ErrorMsg(pParse, "cannot UPSERT a view");
127248 goto insert_cleanup;
127249 }
127250 if( sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget) ){
127251 goto insert_cleanup;
127252 }
127253 pTabList->a[0].iCursor = iDataCur;
127254 pNx = pUpsert;
127255 do{
127256 pNx->pUpsertSrc = pTabList;
127257 pNx->regData = regData;
127258 pNx->iDataCur = iDataCur;
127259 pNx->iIdxCur = iIdxCur;
127260 if( pNx->pUpsertTarget ){
127261 if( sqlite3UpsertAnalyzeTarget(pParse, pTabList, pNx) ){
127262 goto insert_cleanup;
127263 }
127264 }
127265 pNx = pNx->pNextUpsert;
127266 }while( pNx!=0 );
127267 }
127268#endif
127269
127270
127271 /* This is the top of the main insertion loop */
127272 if( useTempTable ){
127273 /* This block codes the top of loop only. The complete loop is the
127274 ** following pseudocode (template 4):
127275 **
127276 ** rewind temp table, if empty goto D
127277 ** C: loop over rows of intermediate table
127278 ** transfer values form intermediate table into <table>
127279 ** end loop
127280 ** D: ...
127281 */
127282 addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind35, srcTab); VdbeCoverage(v);
127283 addrCont = sqlite3VdbeCurrentAddr(v);
127284 }else if( pSelect ){
127285 /* This block codes the top of loop only. The complete loop is the
127286 ** following pseudocode (template 3):
127287 **
127288 ** C: yield X, at EOF goto D
127289 ** insert the select result into <table> from R..R+n
127290 ** goto C
127291 ** D: ...
127292 */
127293 sqlite3VdbeReleaseRegisters(pParse, regData, pTab->nCol, 0, 0);
127294 addrInsTop = addrCont = sqlite3VdbeAddOp1(v, OP_Yield11, dest.iSDParm);
127295 VdbeCoverage(v);
127296 if( ipkColumn>=0 ){
127297 /* tag-20191021-001: If the INTEGER PRIMARY KEY is being generated by the
127298 ** SELECT, go ahead and copy the value into the rowid slot now, so that
127299 ** the value does not get overwritten by a NULL at tag-20191021-002. */
127300 sqlite3VdbeAddOp2(v, OP_Copy80, regFromSelect+ipkColumn, regRowid);
127301 }
127302 }
127303
127304 /* Compute data for ordinary columns of the new entry. Values
127305 ** are written in storage order into registers starting with regData.
127306 ** Only ordinary columns are computed in this loop. The rowid
127307 ** (if there is one) is computed later and generated columns are
127308 ** computed after the rowid since they might depend on the value
127309 ** of the rowid.
127310 */
127311 nHidden = 0;
127312 iRegStore = regData; assert( regData==regRowid+1 )((void) (0));
127313 for(i=0; i<pTab->nCol; i++, iRegStore++){
127314 int k;
127315 u32 colFlags;
127316 assert( i>=nHidden )((void) (0));
127317 if( i==pTab->iPKey ){
127318 /* tag-20191021-002: References to the INTEGER PRIMARY KEY are filled
127319 ** using the rowid. So put a NULL in the IPK slot of the record to avoid
127320 ** using excess space. The file format definition requires this extra
127321 ** NULL - we cannot optimize further by skipping the column completely */
127322 sqlite3VdbeAddOp1(v, OP_SoftNull76, iRegStore);
127323 continue;
127324 }
127325 if( ((colFlags = pTab->aCol[i].colFlags) & COLFLAG_NOINSERT0x0062)!=0 ){
127326 nHidden++;
127327 if( (colFlags & COLFLAG_VIRTUAL0x0020)!=0 ){
127328 /* Virtual columns do not participate in OP_MakeRecord. So back up
127329 ** iRegStore by one slot to compensate for the iRegStore++ in the
127330 ** outer for() loop */
127331 iRegStore--;
127332 continue;
127333 }else if( (colFlags & COLFLAG_STORED0x0040)!=0 ){
127334 /* Stored columns are computed later. But if there are BEFORE
127335 ** triggers, the slots used for stored columns will be OP_Copy-ed
127336 ** to a second block of registers, so the register needs to be
127337 ** initialized to NULL to avoid an uninitialized register read */
127338 if( tmask & TRIGGER_BEFORE1 ){
127339 sqlite3VdbeAddOp1(v, OP_SoftNull76, iRegStore);
127340 }
127341 continue;
127342 }else if( pColumn==0 ){
127343 /* Hidden columns that are not explicitly named in the INSERT
127344 ** get there default value */
127345 sqlite3ExprCodeFactorable(pParse,
127346 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
127347 iRegStore);
127348 continue;
127349 }
127350 }
127351 if( pColumn ){
127352 assert( pColumn->eU4==EU4_IDX )((void) (0));
127353 for(j=0; j<pColumn->nId && pColumn->a[j].u4.idx!=i; j++){}
127354 if( j>=pColumn->nId ){
127355 /* A column not named in the insert column list gets its
127356 ** default value */
127357 sqlite3ExprCodeFactorable(pParse,
127358 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
127359 iRegStore);
127360 continue;
127361 }
127362 k = j;
127363 }else if( nColumn==0 ){
127364 /* This is INSERT INTO ... DEFAULT VALUES. Load the default value. */
127365 sqlite3ExprCodeFactorable(pParse,
127366 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
127367 iRegStore);
127368 continue;
127369 }else{
127370 k = i - nHidden;
127371 }
127372
127373 if( useTempTable ){
127374 sqlite3VdbeAddOp3(v, OP_Column94, srcTab, k, iRegStore);
127375 }else if( pSelect ){
127376 if( regFromSelect!=regData ){
127377 sqlite3VdbeAddOp2(v, OP_SCopy81, regFromSelect+k, iRegStore);
127378 }
127379 }else{
127380 sqlite3ExprCode(pParse, pList->a[k].pExpr, iRegStore);
127381 }
127382 }
127383
127384
127385 /* Run the BEFORE and INSTEAD OF triggers, if there are any
127386 */
127387 endOfLoop = sqlite3VdbeMakeLabel(pParse);
127388 if( tmask & TRIGGER_BEFORE1 ){
127389 int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
127390
127391 /* build the NEW.* reference row. Note that if there is an INTEGER
127392 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
127393 ** translated into a unique ID for the row. But on a BEFORE trigger,
127394 ** we do not know what the unique ID will be (because the insert has
127395 ** not happened yet) so we substitute a rowid of -1
127396 */
127397 if( ipkColumn<0 ){
127398 sqlite3VdbeAddOp2(v, OP_Integer71, -1, regCols);
127399 }else{
127400 int addr1;
127401 assert( !withoutRowid )((void) (0));
127402 if( useTempTable ){
127403 sqlite3VdbeAddOp3(v, OP_Column94, srcTab, ipkColumn, regCols);
127404 }else{
127405 assert( pSelect==0 )((void) (0)); /* Otherwise useTempTable is true */
127406 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
127407 }
127408 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull51, regCols); VdbeCoverage(v);
127409 sqlite3VdbeAddOp2(v, OP_Integer71, -1, regCols);
127410 sqlite3VdbeJumpHere(v, addr1);
127411 sqlite3VdbeAddOp1(v, OP_MustBeInt12, regCols); VdbeCoverage(v);
127412 }
127413
127414 /* Copy the new data already generated. */
127415 assert( pTab->nNVCol>0 )((void) (0));
127416 sqlite3VdbeAddOp3(v, OP_Copy80, regRowid+1, regCols+1, pTab->nNVCol-1);
127417
127418#ifndef SQLITE_OMIT_GENERATED_COLUMNS
127419 /* Compute the new value for generated columns after all other
127420 ** columns have already been computed. This must be done after
127421 ** computing the ROWID in case one of the generated columns
127422 ** refers to the ROWID. */
127423 if( pTab->tabFlags & TF_HasGenerated0x00000060 ){
127424 testcase( pTab->tabFlags & TF_HasVirtual );
127425 testcase( pTab->tabFlags & TF_HasStored );
127426 sqlite3ComputeGeneratedColumns(pParse, regCols+1, pTab);
127427 }
127428#endif
127429
127430 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
127431 ** do not attempt any conversions before assembling the record.
127432 ** If this is a real table, attempt conversions as required by the
127433 ** table column affinities.
127434 */
127435 if( !isView ){
127436 sqlite3TableAffinity(v, pTab, regCols+1);
127437 }
127438
127439 /* Fire BEFORE or INSTEAD OF triggers */
127440 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT127, 0, TRIGGER_BEFORE1,
127441 pTab, regCols-pTab->nCol-1, onError, endOfLoop);
127442
127443 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
127444 }
127445
127446 if( !isView ){
127447 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
127448 /* The row that the VUpdate opcode will delete: none */
127449 sqlite3VdbeAddOp2(v, OP_Null75, 0, regIns);
127450 }
127451 if( ipkColumn>=0 ){
127452 /* Compute the new rowid */
127453 if( useTempTable ){
127454 sqlite3VdbeAddOp3(v, OP_Column94, srcTab, ipkColumn, regRowid);
127455 }else if( pSelect ){
127456 /* Rowid already initialized at tag-20191021-001 */
127457 }else{
127458 Expr *pIpk = pList->a[ipkColumn].pExpr;
127459 if( pIpk->op==TK_NULL121 && !IsVirtual(pTab)((pTab)->eTabType==1) ){
127460 sqlite3VdbeAddOp3(v, OP_NewRowid127, iDataCur, regRowid, regAutoinc);
127461 appendFlag = 1;
127462 }else{
127463 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
127464 }
127465 }
127466 /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
127467 ** to generate a unique primary key value.
127468 */
127469 if( !appendFlag ){
127470 int addr1;
127471 if( !IsVirtual(pTab)((pTab)->eTabType==1) ){
127472 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull51, regRowid); VdbeCoverage(v);
127473 sqlite3VdbeAddOp3(v, OP_NewRowid127, iDataCur, regRowid, regAutoinc);
127474 sqlite3VdbeJumpHere(v, addr1);
127475 }else{
127476 addr1 = sqlite3VdbeCurrentAddr(v);
127477 sqlite3VdbeAddOp2(v, OP_IsNull50, regRowid, addr1+2); VdbeCoverage(v);
127478 }
127479 sqlite3VdbeAddOp1(v, OP_MustBeInt12, regRowid); VdbeCoverage(v);
127480 }
127481 }else if( IsVirtual(pTab)((pTab)->eTabType==1) || withoutRowid ){
127482 sqlite3VdbeAddOp2(v, OP_Null75, 0, regRowid);
127483 }else{
127484 sqlite3VdbeAddOp3(v, OP_NewRowid127, iDataCur, regRowid, regAutoinc);
127485 appendFlag = 1;
127486 }
127487 autoIncStep(pParse, regAutoinc, regRowid);
127488
127489#ifndef SQLITE_OMIT_GENERATED_COLUMNS
127490 /* Compute the new value for generated columns after all other
127491 ** columns have already been computed. This must be done after
127492 ** computing the ROWID in case one of the generated columns
127493 ** is derived from the INTEGER PRIMARY KEY. */
127494 if( pTab->tabFlags & TF_HasGenerated0x00000060 ){
127495 sqlite3ComputeGeneratedColumns(pParse, regRowid+1, pTab);
127496 }
127497#endif
127498
127499 /* Generate code to check constraints and generate index keys and
127500 ** do the insertion.
127501 */
127502#ifndef SQLITE_OMIT_VIRTUALTABLE
127503 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
127504 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
127505 sqlite3VtabMakeWritable(pParse, pTab);
127506 sqlite3VdbeAddOp4(v, OP_VUpdate7, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB(-11));
127507 sqlite3VdbeChangeP5(v, onError==OE_Default11 ? OE_Abort2 : onError);
127508 sqlite3MayAbort(pParse);
127509 }else
127510#endif
127511 {
127512 int isReplace = 0;/* Set to true if constraints may cause a replace */
127513 int bUseSeek; /* True to use OPFLAG_SEEKRESULT */
127514 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
127515 regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0, pUpsert
127516 );
127517 sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
127518
127519 /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE
127520 ** constraints or (b) there are no triggers and this table is not a
127521 ** parent table in a foreign key constraint. It is safe to set the
127522 ** flag in the second case as if any REPLACE constraint is hit, an
127523 ** OP_Delete or OP_IdxDelete instruction will be executed on each
127524 ** cursor that is disturbed. And these instructions both clear the
127525 ** VdbeCursor.seekResult variable, disabling the OPFLAG_USESEEKRESULT
127526 ** functionality. */
127527 bUseSeek = (isReplace==0 || !sqlite3VdbeHasSubProgram(v));
127528 sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
127529 regIns, aRegIdx, 0, appendFlag, bUseSeek
127530 );
127531 }
127532#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
127533 }else if( pParse->bReturning ){
127534 /* If there is a RETURNING clause, populate the rowid register with
127535 ** constant value -1, in case one or more of the returned expressions
127536 ** refer to the "rowid" of the view. */
127537 sqlite3VdbeAddOp2(v, OP_Integer71, -1, regRowid);
127538#endif
127539 }
127540
127541 /* Update the count of rows that are inserted
127542 */
127543 if( regRowCount ){
127544 sqlite3VdbeAddOp2(v, OP_AddImm86, regRowCount, 1);
127545 }
127546
127547 if( pTrigger ){
127548 /* Code AFTER triggers */
127549 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT127, 0, TRIGGER_AFTER2,
127550 pTab, regData-2-pTab->nCol, onError, endOfLoop);
127551 }
127552
127553 /* The bottom of the main insertion loop, if the data source
127554 ** is a SELECT statement.
127555 */
127556 sqlite3VdbeResolveLabel(v, endOfLoop);
127557 if( useTempTable ){
127558 sqlite3VdbeAddOp2(v, OP_Next38, srcTab, addrCont); VdbeCoverage(v);
127559 sqlite3VdbeJumpHere(v, addrInsTop);
127560 sqlite3VdbeAddOp1(v, OP_Close122, srcTab);
127561 }else if( pSelect ){
127562 sqlite3VdbeGoto(v, addrCont);
127563#ifdef SQLITE_DEBUG
127564 /* If we are jumping back to an OP_Yield that is preceded by an
127565 ** OP_ReleaseReg, set the p5 flag on the OP_Goto so that the
127566 ** OP_ReleaseReg will be included in the loop. */
127567 if( sqlite3VdbeGetOp(v, addrCont-1)->opcode==OP_ReleaseReg183 ){
127568 assert( sqlite3VdbeGetOp(v, addrCont)->opcode==OP_Yield )((void) (0));
127569 sqlite3VdbeChangeP5(v, 1);
127570 }
127571#endif
127572 sqlite3VdbeJumpHere(v, addrInsTop);
127573 }
127574
127575#ifndef SQLITE_OMIT_XFER_OPT
127576insert_end:
127577#endif /* SQLITE_OMIT_XFER_OPT */
127578 /* Update the sqlite_sequence table by storing the content of the
127579 ** maximum rowid counter values recorded while inserting into
127580 ** autoincrement tables.
127581 */
127582 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
127583 sqlite3AutoincrementEnd(pParse);
127584 }
127585
127586 /*
127587 ** Return the number of rows inserted. If this routine is
127588 ** generating code because of a call to sqlite3NestedParse(), do not
127589 ** invoke the callback function.
127590 */
127591 if( regRowCount ){
127592 sqlite3CodeChangeCount(v, regRowCount, "rows inserted");
127593 }
127594
127595insert_cleanup:
127596 sqlite3SrcListDelete(db, pTabList);
127597 sqlite3ExprListDelete(db, pList);
127598 sqlite3UpsertDelete(db, pUpsert);
127599 sqlite3SelectDelete(db, pSelect);
127600 sqlite3IdListDelete(db, pColumn);
127601 sqlite3DbFree(db, aRegIdx);
127602}
127603
127604/* Make sure "isView" and other macros defined above are undefined. Otherwise
127605** they may interfere with compilation of other functions in this file
127606** (or in another file, if this file becomes part of the amalgamation). */
127607#ifdef isView
127608 #undef isView
127609#endif
127610#ifdef pTrigger
127611 #undef pTrigger
127612#endif
127613#ifdef tmask
127614 #undef tmask
127615#endif
127616
127617/*
127618** Meanings of bits in of pWalker->eCode for
127619** sqlite3ExprReferencesUpdatedColumn()
127620*/
127621#define CKCNSTRNT_COLUMN0x01 0x01 /* CHECK constraint uses a changing column */
127622#define CKCNSTRNT_ROWID0x02 0x02 /* CHECK constraint references the ROWID */
127623
127624/* This is the Walker callback from sqlite3ExprReferencesUpdatedColumn().
127625* Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
127626** expression node references any of the
127627** columns that are being modifed by an UPDATE statement.
127628*/
127629static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){
127630 if( pExpr->op==TK_COLUMN167 ){
127631 assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 )((void) (0));
127632 if( pExpr->iColumn>=0 ){
127633 if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
127634 pWalker->eCode |= CKCNSTRNT_COLUMN0x01;
127635 }
127636 }else{
127637 pWalker->eCode |= CKCNSTRNT_ROWID0x02;
127638 }
127639 }
127640 return WRC_Continue0;
127641}
127642
127643/*
127644** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
127645** only columns that are modified by the UPDATE are those for which
127646** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true.
127647**
127648** Return true if CHECK constraint pExpr uses any of the
127649** changing columns (or the rowid if it is changing). In other words,
127650** return true if this CHECK constraint must be validated for
127651** the new row in the UPDATE statement.
127652**
127653** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
127654** The operation of this routine is the same - return true if an only if
127655** the expression uses one or more of columns identified by the second and
127656** third arguments.
127657*/
127658SQLITE_PRIVATEstatic int sqlite3ExprReferencesUpdatedColumn(
127659 Expr *pExpr, /* The expression to be checked */
127660 int *aiChng, /* aiChng[x]>=0 if column x changed by the UPDATE */
127661 int chngRowid /* True if UPDATE changes the rowid */
127662){
127663 Walker w;
127664 memset(&w, 0, sizeof(w));
127665 w.eCode = 0;
127666 w.xExprCallback = checkConstraintExprNode;
127667 w.u.aiCol = aiChng;
127668 sqlite3WalkExpr(&w, pExpr);
127669 if( !chngRowid ){
127670 testcase( (w.eCode & CKCNSTRNT_ROWID)!=0 );
127671 w.eCode &= ~CKCNSTRNT_ROWID0x02;
127672 }
127673 testcase( w.eCode==0 );
127674 testcase( w.eCode==CKCNSTRNT_COLUMN );
127675 testcase( w.eCode==CKCNSTRNT_ROWID );
127676 testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) );
127677 return w.eCode!=0;
127678}
127679
127680/*
127681** The sqlite3GenerateConstraintChecks() routine usually wants to visit
127682** the indexes of a table in the order provided in the Table->pIndex list.
127683** However, sometimes (rarely - when there is an upsert) it wants to visit
127684** the indexes in a different order. The following data structures accomplish
127685** this.
127686**
127687** The IndexIterator object is used to walk through all of the indexes
127688** of a table in either Index.pNext order, or in some other order established
127689** by an array of IndexListTerm objects.
127690*/
127691typedef struct IndexListTerm IndexListTerm;
127692typedef struct IndexIterator IndexIterator;
127693struct IndexIterator {
127694 int eType; /* 0 for Index.pNext list. 1 for an array of IndexListTerm */
127695 int i; /* Index of the current item from the list */
127696 union {
127697 struct { /* Use this object for eType==0: A Index.pNext list */
127698 Index *pIdx; /* The current Index */
127699 } lx;
127700 struct { /* Use this object for eType==1; Array of IndexListTerm */
127701 int nIdx; /* Size of the array */
127702 IndexListTerm *aIdx; /* Array of IndexListTerms */
127703 } ax;
127704 } u;
127705};
127706
127707/* When IndexIterator.eType==1, then each index is an array of instances
127708** of the following object
127709*/
127710struct IndexListTerm {
127711 Index *p; /* The index */
127712 int ix; /* Which entry in the original Table.pIndex list is this index*/
127713};
127714
127715/* Return the first index on the list */
127716static Index *indexIteratorFirst(IndexIterator *pIter, int *pIx){
127717 assert( pIter->i==0 )((void) (0));
127718 if( pIter->eType ){
127719 *pIx = pIter->u.ax.aIdx[0].ix;
127720 return pIter->u.ax.aIdx[0].p;
127721 }else{
127722 *pIx = 0;
127723 return pIter->u.lx.pIdx;
127724 }
127725}
127726
127727/* Return the next index from the list. Return NULL when out of indexes */
127728static Index *indexIteratorNext(IndexIterator *pIter, int *pIx){
127729 if( pIter->eType ){
127730 int i = ++pIter->i;
127731 if( i>=pIter->u.ax.nIdx ){
127732 *pIx = i;
127733 return 0;
127734 }
127735 *pIx = pIter->u.ax.aIdx[i].ix;
127736 return pIter->u.ax.aIdx[i].p;
127737 }else{
127738 ++(*pIx);
127739 pIter->u.lx.pIdx = pIter->u.lx.pIdx->pNext;
127740 return pIter->u.lx.pIdx;
127741 }
127742}
127743
127744/*
127745** Generate code to do constraint checks prior to an INSERT or an UPDATE
127746** on table pTab.
127747**
127748** The regNewData parameter is the first register in a range that contains
127749** the data to be inserted or the data after the update. There will be
127750** pTab->nCol+1 registers in this range. The first register (the one
127751** that regNewData points to) will contain the new rowid, or NULL in the
127752** case of a WITHOUT ROWID table. The second register in the range will
127753** contain the content of the first table column. The third register will
127754** contain the content of the second table column. And so forth.
127755**
127756** The regOldData parameter is similar to regNewData except that it contains
127757** the data prior to an UPDATE rather than afterwards. regOldData is zero
127758** for an INSERT. This routine can distinguish between UPDATE and INSERT by
127759** checking regOldData for zero.
127760**
127761** For an UPDATE, the pkChng boolean is true if the true primary key (the
127762** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table)
127763** might be modified by the UPDATE. If pkChng is false, then the key of
127764** the iDataCur content table is guaranteed to be unchanged by the UPDATE.
127765**
127766** For an INSERT, the pkChng boolean indicates whether or not the rowid
127767** was explicitly specified as part of the INSERT statement. If pkChng
127768** is zero, it means that the either rowid is computed automatically or
127769** that the table is a WITHOUT ROWID table and has no rowid. On an INSERT,
127770** pkChng will only be true if the INSERT statement provides an integer
127771** value for either the rowid column or its INTEGER PRIMARY KEY alias.
127772**
127773** The code generated by this routine will store new index entries into
127774** registers identified by aRegIdx[]. No index entry is created for
127775** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is
127776** the same as the order of indices on the linked list of indices
127777** at pTab->pIndex.
127778**
127779** (2019-05-07) The generated code also creates a new record for the
127780** main table, if pTab is a rowid table, and stores that record in the
127781** register identified by aRegIdx[nIdx] - in other words in the first
127782** entry of aRegIdx[] past the last index. It is important that the
127783** record be generated during constraint checks to avoid affinity changes
127784** to the register content that occur after constraint checks but before
127785** the new record is inserted.
127786**
127787** The caller must have already opened writeable cursors on the main
127788** table and all applicable indices (that is to say, all indices for which
127789** aRegIdx[] is not zero). iDataCur is the cursor for the main table when
127790** inserting or updating a rowid table, or the cursor for the PRIMARY KEY
127791** index when operating on a WITHOUT ROWID table. iIdxCur is the cursor
127792** for the first index in the pTab->pIndex list. Cursors for other indices
127793** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
127794**
127795** This routine also generates code to check constraints. NOT NULL,
127796** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
127797** then the appropriate action is performed. There are five possible
127798** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
127799**
127800** Constraint type Action What Happens
127801** --------------- ---------- ----------------------------------------
127802** any ROLLBACK The current transaction is rolled back and
127803** sqlite3_step() returns immediately with a
127804** return code of SQLITE_CONSTRAINT.
127805**
127806** any ABORT Back out changes from the current command
127807** only (do not do a complete rollback) then
127808** cause sqlite3_step() to return immediately
127809** with SQLITE_CONSTRAINT.
127810**
127811** any FAIL Sqlite3_step() returns immediately with a
127812** return code of SQLITE_CONSTRAINT. The
127813** transaction is not rolled back and any
127814** changes to prior rows are retained.
127815**
127816** any IGNORE The attempt in insert or update the current
127817** row is skipped, without throwing an error.
127818** Processing continues with the next row.
127819** (There is an immediate jump to ignoreDest.)
127820**
127821** NOT NULL REPLACE The NULL value is replace by the default
127822** value for that column. If the default value
127823** is NULL, the action is the same as ABORT.
127824**
127825** UNIQUE REPLACE The other row that conflicts with the row
127826** being inserted is removed.
127827**
127828** CHECK REPLACE Illegal. The results in an exception.
127829**
127830** Which action to take is determined by the overrideError parameter.
127831** Or if overrideError==OE_Default, then the pParse->onError parameter
127832** is used. Or if pParse->onError==OE_Default then the onError value
127833** for the constraint is used.
127834*/
127835SQLITE_PRIVATEstatic void sqlite3GenerateConstraintChecks(
127836 Parse *pParse, /* The parser context */
127837 Table *pTab, /* The table being inserted or updated */
127838 int *aRegIdx, /* Use register aRegIdx[i] for index i. 0 for unused */
127839 int iDataCur, /* Canonical data cursor (main table or PK index) */
127840 int iIdxCur, /* First index cursor */
127841 int regNewData, /* First register in a range holding values to insert */
127842 int regOldData, /* Previous content. 0 for INSERTs */
127843 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
127844 u8 overrideError, /* Override onError to this if not OE_Default */
127845 int ignoreDest, /* Jump to this label on an OE_Ignore resolution */
127846 int *pbMayReplace, /* OUT: Set to true if constraint may cause a replace */
127847 int *aiChng, /* column i is unchanged if aiChng[i]<0 */
127848 Upsert *pUpsert /* ON CONFLICT clauses, if any. NULL otherwise */
127849){
127850 Vdbe *v; /* VDBE under constrution */
127851 Index *pIdx; /* Pointer to one of the indices */
127852 Index *pPk = 0; /* The PRIMARY KEY index for WITHOUT ROWID tables */
127853 sqlite3 *db; /* Database connection */
127854 int i; /* loop counter */
127855 int ix; /* Index loop counter */
127856 int nCol; /* Number of columns */
127857 int onError; /* Conflict resolution strategy */
127858 int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
127859 int nPkField; /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
127860 Upsert *pUpsertClause = 0; /* The specific ON CONFLICT clause for pIdx */
127861 u8 isUpdate; /* True if this is an UPDATE operation */
127862 u8 bAffinityDone = 0; /* True if the OP_Affinity operation has been run */
127863 int upsertIpkReturn = 0; /* Address of Goto at end of IPK uniqueness check */
127864 int upsertIpkDelay = 0; /* Address of Goto to bypass initial IPK check */
127865 int ipkTop = 0; /* Top of the IPK uniqueness check */
127866 int ipkBottom = 0; /* OP_Goto at the end of the IPK uniqueness check */
127867 /* Variables associated with retesting uniqueness constraints after
127868 ** replace triggers fire have run */
127869 int regTrigCnt; /* Register used to count replace trigger invocations */
127870 int addrRecheck = 0; /* Jump here to recheck all uniqueness constraints */
127871 int lblRecheckOk = 0; /* Each recheck jumps to this label if it passes */
127872 Trigger *pTrigger; /* List of DELETE triggers on the table pTab */
127873 int nReplaceTrig = 0; /* Number of replace triggers coded */
127874 IndexIterator sIdxIter; /* Index iterator */
127875
127876 isUpdate = regOldData!=0;
127877 db = pParse->db;
127878 v = pParse->pVdbe;
127879 assert( v!=0 )((void) (0));
127880 assert( !IsView(pTab) )((void) (0)); /* This table is not a VIEW */
127881 nCol = pTab->nCol;
127882
127883 /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
127884 ** normal rowid tables. nPkField is the number of key fields in the
127885 ** pPk index or 1 for a rowid table. In other words, nPkField is the
127886 ** number of fields in the true primary key of the table. */
127887 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
127888 pPk = 0;
127889 nPkField = 1;
127890 }else{
127891 pPk = sqlite3PrimaryKeyIndex(pTab);
127892 nPkField = pPk->nKeyCol;
127893 }
127894
127895 /* Record that this module has started */
127896 VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)",
127897 iDataCur, iIdxCur, regNewData, regOldData, pkChng));
127898
127899 /* Test all NOT NULL constraints.
127900 */
127901 if( pTab->tabFlags & TF_HasNotNull0x00000800 ){
127902 int b2ndPass = 0; /* True if currently running 2nd pass */
127903 int nSeenReplace = 0; /* Number of ON CONFLICT REPLACE operations */
127904 int nGenerated = 0; /* Number of generated columns with NOT NULL */
127905 while(1){ /* Make 2 passes over columns. Exit loop via "break" */
127906 for(i=0; i<nCol; i++){
127907 int iReg; /* Register holding column value */
127908 Column *pCol = &pTab->aCol[i]; /* The column to check for NOT NULL */
127909 int isGenerated; /* non-zero if column is generated */
127910 onError = pCol->notNull;
127911 if( onError==OE_None0 ) continue; /* No NOT NULL on this column */
127912 if( i==pTab->iPKey ){
127913 continue; /* ROWID is never NULL */
127914 }
127915 isGenerated = pCol->colFlags & COLFLAG_GENERATED0x0060;
127916 if( isGenerated && !b2ndPass ){
127917 nGenerated++;
127918 continue; /* Generated columns processed on 2nd pass */
127919 }
127920 if( aiChng && aiChng[i]<0 && !isGenerated ){
127921 /* Do not check NOT NULL on columns that do not change */
127922 continue;
127923 }
127924 if( overrideError!=OE_Default11 ){
127925 onError = overrideError;
127926 }else if( onError==OE_Default11 ){
127927 onError = OE_Abort2;
127928 }
127929 if( onError==OE_Replace5 ){
127930 if( b2ndPass /* REPLACE becomes ABORT on the 2nd pass */
127931 || pCol->iDflt==0 /* REPLACE is ABORT if no DEFAULT value */
127932 ){
127933 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
127934 testcase( pCol->colFlags & COLFLAG_STORED );
127935 testcase( pCol->colFlags & COLFLAG_GENERATED );
127936 onError = OE_Abort2;
127937 }else{
127938 assert( !isGenerated )((void) (0));
127939 }
127940 }else if( b2ndPass && !isGenerated ){
127941 continue;
127942 }
127943 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail((void) (0))
127944 || onError==OE_Ignore || onError==OE_Replace )((void) (0));
127945 testcase( i!=sqlite3TableColumnToStorage(pTab, i) );
127946 iReg = sqlite3TableColumnToStorage(pTab, i) + regNewData + 1;
127947 switch( onError ){
127948 case OE_Replace5: {
127949 int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull51, iReg);
127950 VdbeCoverage(v);
127951 assert( (pCol->colFlags & COLFLAG_GENERATED)==0 )((void) (0));
127952 nSeenReplace++;
127953 sqlite3ExprCodeCopy(pParse,
127954 sqlite3ColumnExpr(pTab, pCol), iReg);
127955 sqlite3VdbeJumpHere(v, addr1);
127956 break;
127957 }
127958 case OE_Abort2:
127959 sqlite3MayAbort(pParse);
127960 /* no break */ deliberate_fall_through
127961 case OE_Rollback1:
127962 case OE_Fail3: {
127963 char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
127964 pCol->zCnName);
127965 sqlite3VdbeAddOp3(v, OP_HaltIfNull69, SQLITE_CONSTRAINT_NOTNULL(19 | (5<<8)),
127966 onError, iReg);
127967 sqlite3VdbeAppendP4(v, zMsg, P4_DYNAMIC(-6));
127968 sqlite3VdbeChangeP5(v, P5_ConstraintNotNull1);
127969 VdbeCoverage(v);
127970 break;
127971 }
127972 default: {
127973 assert( onError==OE_Ignore )((void) (0));
127974 sqlite3VdbeAddOp2(v, OP_IsNull50, iReg, ignoreDest);
127975 VdbeCoverage(v);
127976 break;
127977 }
127978 } /* end switch(onError) */
127979 } /* end loop i over columns */
127980 if( nGenerated==0 && nSeenReplace==0 ){
127981 /* If there are no generated columns with NOT NULL constraints
127982 ** and no NOT NULL ON CONFLICT REPLACE constraints, then a single
127983 ** pass is sufficient */
127984 break;
127985 }
127986 if( b2ndPass ) break; /* Never need more than 2 passes */
127987 b2ndPass = 1;
127988#ifndef SQLITE_OMIT_GENERATED_COLUMNS
127989 if( nSeenReplace>0 && (pTab->tabFlags & TF_HasGenerated0x00000060)!=0 ){
127990 /* If any NOT NULL ON CONFLICT REPLACE constraints fired on the
127991 ** first pass, recomputed values for all generated columns, as
127992 ** those values might depend on columns affected by the REPLACE.
127993 */
127994 sqlite3ComputeGeneratedColumns(pParse, regNewData+1, pTab);
127995 }
127996#endif
127997 } /* end of 2-pass loop */
127998 } /* end if( has-not-null-constraints ) */
127999
128000 /* Test all CHECK constraints
128001 */
128002#ifndef SQLITE_OMIT_CHECK
128003 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks0x00000200)==0 ){
128004 ExprList *pCheck = pTab->pCheck;
128005 pParse->iSelfTab = -(regNewData+1);
128006 onError = overrideError!=OE_Default11 ? overrideError : OE_Abort2;
128007 for(i=0; i<pCheck->nExpr; i++){
128008 int allOk;
128009 Expr *pCopy;
128010 Expr *pExpr = pCheck->a[i].pExpr;
128011 if( aiChng
128012 && !sqlite3ExprReferencesUpdatedColumn(pExpr, aiChng, pkChng)
128013 ){
128014 /* The check constraints do not reference any of the columns being
128015 ** updated so there is no point it verifying the check constraint */
128016 continue;
128017 }
128018 if( bAffinityDone==0 ){
128019 sqlite3TableAffinity(v, pTab, regNewData+1);
128020 bAffinityDone = 1;
128021 }
128022 allOk = sqlite3VdbeMakeLabel(pParse);
128023 sqlite3VdbeVerifyAbortable(v, onError);
128024 pCopy = sqlite3ExprDup(db, pExpr, 0);
128025 if( !db->mallocFailed ){
128026 sqlite3ExprIfTrue(pParse, pCopy, allOk, SQLITE_JUMPIFNULL0x10);
128027 }
128028 sqlite3ExprDelete(db, pCopy);
128029 if( onError==OE_Ignore4 ){
128030 sqlite3VdbeGoto(v, ignoreDest);
128031 }else{
128032 char *zName = pCheck->a[i].zEName;
128033 assert( zName!=0 || pParse->db->mallocFailed )((void) (0));
128034 if( onError==OE_Replace5 ) onError = OE_Abort2; /* IMP: R-26383-51744 */
128035 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK(19 | (1<<8)),
128036 onError, zName, P4_TRANSIENT0,
128037 P5_ConstraintCheck3);
128038 }
128039 sqlite3VdbeResolveLabel(v, allOk);
128040 }
128041 pParse->iSelfTab = 0;
128042 }
128043#endif /* !defined(SQLITE_OMIT_CHECK) */
128044
128045 /* UNIQUE and PRIMARY KEY constraints should be handled in the following
128046 ** order:
128047 **
128048 ** (1) OE_Update
128049 ** (2) OE_Abort, OE_Fail, OE_Rollback, OE_Ignore
128050 ** (3) OE_Replace
128051 **
128052 ** OE_Fail and OE_Ignore must happen before any changes are made.
128053 ** OE_Update guarantees that only a single row will change, so it
128054 ** must happen before OE_Replace. Technically, OE_Abort and OE_Rollback
128055 ** could happen in any order, but they are grouped up front for
128056 ** convenience.
128057 **
128058 ** 2018-08-14: Ticket https://www.sqlite.org/src/info/908f001483982c43
128059 ** The order of constraints used to have OE_Update as (2) and OE_Abort
128060 ** and so forth as (1). But apparently PostgreSQL checks the OE_Update
128061 ** constraint before any others, so it had to be moved.
128062 **
128063 ** Constraint checking code is generated in this order:
128064 ** (A) The rowid constraint
128065 ** (B) Unique index constraints that do not have OE_Replace as their
128066 ** default conflict resolution strategy
128067 ** (C) Unique index that do use OE_Replace by default.
128068 **
128069 ** The ordering of (2) and (3) is accomplished by making sure the linked
128070 ** list of indexes attached to a table puts all OE_Replace indexes last
128071 ** in the list. See sqlite3CreateIndex() for where that happens.
128072 */
128073 sIdxIter.eType = 0;
128074 sIdxIter.i = 0;
128075 sIdxIter.u.ax.aIdx = 0; /* Silence harmless compiler warning */
128076 sIdxIter.u.lx.pIdx = pTab->pIndex;
128077 if( pUpsert ){
128078 if( pUpsert->pUpsertTarget==0 ){
128079 /* There is just on ON CONFLICT clause and it has no constraint-target */
128080 assert( pUpsert->pNextUpsert==0 )((void) (0));
128081 if( pUpsert->isDoUpdate==0 ){
128082 /* A single ON CONFLICT DO NOTHING clause, without a constraint-target.
128083 ** Make all unique constraint resolution be OE_Ignore */
128084 overrideError = OE_Ignore4;
128085 pUpsert = 0;
128086 }else{
128087 /* A single ON CONFLICT DO UPDATE. Make all resolutions OE_Update */
128088 overrideError = OE_Update6;
128089 }
128090 }else if( pTab->pIndex!=0 ){
128091 /* Otherwise, we'll need to run the IndexListTerm array version of the
128092 ** iterator to ensure that all of the ON CONFLICT conditions are
128093 ** checked first and in order. */
128094 int nIdx, jj;
128095 u64 nByte;
128096 Upsert *pTerm;
128097 u8 *bUsed;
128098 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
128099 assert( aRegIdx[nIdx]>0 )((void) (0));
128100 }
128101 sIdxIter.eType = 1;
128102 sIdxIter.u.ax.nIdx = nIdx;
128103 nByte = (sizeof(IndexListTerm)+1)*nIdx + nIdx;
128104 sIdxIter.u.ax.aIdx = sqlite3DbMallocZero(db, nByte);
128105 if( sIdxIter.u.ax.aIdx==0 ) return; /* OOM */
128106 bUsed = (u8*)&sIdxIter.u.ax.aIdx[nIdx];
128107 pUpsert->pToFree = sIdxIter.u.ax.aIdx;
128108 for(i=0, pTerm=pUpsert; pTerm; pTerm=pTerm->pNextUpsert){
128109 if( pTerm->pUpsertTarget==0 ) break;
128110 if( pTerm->pUpsertIdx==0 ) continue; /* Skip ON CONFLICT for the IPK */
128111 jj = 0;
128112 pIdx = pTab->pIndex;
128113 while( ALWAYS(pIdx!=0)(pIdx!=0) && pIdx!=pTerm->pUpsertIdx ){
128114 pIdx = pIdx->pNext;
128115 jj++;
128116 }
128117 if( bUsed[jj] ) continue; /* Duplicate ON CONFLICT clause ignored */
128118 bUsed[jj] = 1;
128119 sIdxIter.u.ax.aIdx[i].p = pIdx;
128120 sIdxIter.u.ax.aIdx[i].ix = jj;
128121 i++;
128122 }
128123 for(jj=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, jj++){
128124 if( bUsed[jj] ) continue;
128125 sIdxIter.u.ax.aIdx[i].p = pIdx;
128126 sIdxIter.u.ax.aIdx[i].ix = jj;
128127 i++;
128128 }
128129 assert( i==nIdx )((void) (0));
128130 }
128131 }
128132
128133 /* Determine if it is possible that triggers (either explicitly coded
128134 ** triggers or FK resolution actions) might run as a result of deletes
128135 ** that happen when OE_Replace conflict resolution occurs. (Call these
128136 ** "replace triggers".) If any replace triggers run, we will need to
128137 ** recheck all of the uniqueness constraints after they have all run.
128138 ** But on the recheck, the resolution is OE_Abort instead of OE_Replace.
128139 **
128140 ** If replace triggers are a possibility, then
128141 **
128142 ** (1) Allocate register regTrigCnt and initialize it to zero.
128143 ** That register will count the number of replace triggers that
128144 ** fire. Constraint recheck only occurs if the number is positive.
128145 ** (2) Initialize pTrigger to the list of all DELETE triggers on pTab.
128146 ** (3) Initialize addrRecheck and lblRecheckOk
128147 **
128148 ** The uniqueness rechecking code will create a series of tests to run
128149 ** in a second pass. The addrRecheck and lblRecheckOk variables are
128150 ** used to link together these tests which are separated from each other
128151 ** in the generate bytecode.
128152 */
128153 if( (db->flags & (SQLITE_RecTriggers0x00002000|SQLITE_ForeignKeys0x00004000))==0 ){
128154 /* There are not DELETE triggers nor FK constraints. No constraint
128155 ** rechecks are needed. */
128156 pTrigger = 0;
128157 regTrigCnt = 0;
128158 }else{
128159 if( db->flags&SQLITE_RecTriggers0x00002000 ){
128160 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE128, 0, 0);
128161 regTrigCnt = pTrigger!=0 || sqlite3FkRequired(pParse, pTab, 0, 0);
128162 }else{
128163 pTrigger = 0;
128164 regTrigCnt = sqlite3FkRequired(pParse, pTab, 0, 0);
128165 }
128166 if( regTrigCnt ){
128167 /* Replace triggers might exist. Allocate the counter and
128168 ** initialize it to zero. */
128169 regTrigCnt = ++pParse->nMem;
128170 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regTrigCnt);
128171 VdbeComment((v, "trigger count"));
128172 lblRecheckOk = sqlite3VdbeMakeLabel(pParse);
128173 addrRecheck = lblRecheckOk;
128174 }
128175 }
128176
128177 /* If rowid is changing, make sure the new rowid does not previously
128178 ** exist in the table.
128179 */
128180 if( pkChng && pPk==0 ){
128181 int addrRowidOk = sqlite3VdbeMakeLabel(pParse);
128182
128183 /* Figure out what action to take in case of a rowid collision */
128184 onError = pTab->keyConf;
128185 if( overrideError!=OE_Default11 ){
128186 onError = overrideError;
128187 }else if( onError==OE_Default11 ){
128188 onError = OE_Abort2;
128189 }
128190
128191 /* figure out whether or not upsert applies in this case */
128192 if( pUpsert ){
128193 pUpsertClause = sqlite3UpsertOfIndex(pUpsert,0);
128194 if( pUpsertClause!=0 ){
128195 if( pUpsertClause->isDoUpdate==0 ){
128196 onError = OE_Ignore4; /* DO NOTHING is the same as INSERT OR IGNORE */
128197 }else{
128198 onError = OE_Update6; /* DO UPDATE */
128199 }
128200 }
128201 if( pUpsertClause!=pUpsert ){
128202 /* The first ON CONFLICT clause has a conflict target other than
128203 ** the IPK. We have to jump ahead to that first ON CONFLICT clause
128204 ** and then come back here and deal with the IPK afterwards */
128205 upsertIpkDelay = sqlite3VdbeAddOp0(v, OP_Goto8);
128206 }
128207 }
128208
128209 /* If the response to a rowid conflict is REPLACE but the response
128210 ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
128211 ** to defer the running of the rowid conflict checking until after
128212 ** the UNIQUE constraints have run.
128213 */
128214 if( onError==OE_Replace5 /* IPK rule is REPLACE */
128215 && onError!=overrideError /* Rules for other constraints are different */
128216 && pTab->pIndex /* There exist other constraints */
128217 && !upsertIpkDelay /* IPK check already deferred by UPSERT */
128218 ){
128219 ipkTop = sqlite3VdbeAddOp0(v, OP_Goto8)+1;
128220 VdbeComment((v, "defer IPK REPLACE until last"));
128221 }
128222
128223 if( isUpdate ){
128224 /* pkChng!=0 does not mean that the rowid has changed, only that
128225 ** it might have changed. Skip the conflict logic below if the rowid
128226 ** is unchanged. */
128227 sqlite3VdbeAddOp3(v, OP_Eq53, regNewData, addrRowidOk, regOldData);
128228 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL0x90);
128229 VdbeCoverage(v);
128230 }
128231
128232 /* Check to see if the new rowid already exists in the table. Skip
128233 ** the following conflict logic if it does not. */
128234 VdbeNoopComment((v, "uniqueness check for ROWID"));
128235 sqlite3VdbeVerifyAbortable(v, onError);
128236 sqlite3VdbeAddOp3(v, OP_NotExists30, iDataCur, addrRowidOk, regNewData);
128237 VdbeCoverage(v);
128238
128239 switch( onError ){
128240 default: {
128241 onError = OE_Abort2;
128242 /* no break */ deliberate_fall_through
128243 }
128244 case OE_Rollback1:
128245 case OE_Abort2:
128246 case OE_Fail3: {
128247 testcase( onError==OE_Rollback );
128248 testcase( onError==OE_Abort );
128249 testcase( onError==OE_Fail );
128250 sqlite3RowidConstraint(pParse, onError, pTab);
128251 break;
128252 }
128253 case OE_Replace5: {
128254 /* If there are DELETE triggers on this table and the
128255 ** recursive-triggers flag is set, call GenerateRowDelete() to
128256 ** remove the conflicting row from the table. This will fire
128257 ** the triggers and remove both the table and index b-tree entries.
128258 **
128259 ** Otherwise, if there are no triggers or the recursive-triggers
128260 ** flag is not set, but the table has one or more indexes, call
128261 ** GenerateRowIndexDelete(). This removes the index b-tree entries
128262 ** only. The table b-tree entry will be replaced by the new entry
128263 ** when it is inserted.
128264 **
128265 ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
128266 ** also invoke MultiWrite() to indicate that this VDBE may require
128267 ** statement rollback (if the statement is aborted after the delete
128268 ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
128269 ** but being more selective here allows statements like:
128270 **
128271 ** REPLACE INTO t(rowid) VALUES($newrowid)
128272 **
128273 ** to run without a statement journal if there are no indexes on the
128274 ** table.
128275 */
128276 if( regTrigCnt ){
128277 sqlite3MultiWrite(pParse);
128278 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
128279 regNewData, 1, 0, OE_Replace5, 1, -1);
128280 sqlite3VdbeAddOp2(v, OP_AddImm86, regTrigCnt, 1); /* incr trigger cnt */
128281 nReplaceTrig++;
128282 }else{
128283#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
128284 assert( HasRowid(pTab) )((void) (0));
128285 /* This OP_Delete opcode fires the pre-update-hook only. It does
128286 ** not modify the b-tree. It is more efficient to let the coming
128287 ** OP_Insert replace the existing entry than it is to delete the
128288 ** existing entry and then insert a new one. */
128289 sqlite3VdbeAddOp2(v, OP_Delete130, iDataCur, OPFLAG_ISNOOP0x40);
128290 sqlite3VdbeAppendP4(v, pTab, P4_TABLE(-5));
128291#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
128292 if( pTab->pIndex ){
128293 sqlite3MultiWrite(pParse);
128294 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
128295 }
128296 }
128297 seenReplace = 1;
128298 break;
128299 }
128300#ifndef SQLITE_OMIT_UPSERT
128301 case OE_Update6: {
128302 sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, 0, iDataCur);
128303 /* no break */ deliberate_fall_through
128304 }
128305#endif
128306 case OE_Ignore4: {
128307 testcase( onError==OE_Ignore );
128308 sqlite3VdbeGoto(v, ignoreDest);
128309 break;
128310 }
128311 }
128312 sqlite3VdbeResolveLabel(v, addrRowidOk);
128313 if( pUpsert && pUpsertClause!=pUpsert ){
128314 upsertIpkReturn = sqlite3VdbeAddOp0(v, OP_Goto8);
128315 }else if( ipkTop ){
128316 ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto8);
128317 sqlite3VdbeJumpHere(v, ipkTop-1);
128318 }
128319 }
128320
128321 /* Test all UNIQUE constraints by creating entries for each UNIQUE
128322 ** index and making sure that duplicate entries do not already exist.
128323 ** Compute the revised record entries for indices as we go.
128324 **
128325 ** This loop also handles the case of the PRIMARY KEY index for a
128326 ** WITHOUT ROWID table.
128327 */
128328 for(pIdx = indexIteratorFirst(&sIdxIter, &ix);
128329 pIdx;
128330 pIdx = indexIteratorNext(&sIdxIter, &ix)
128331 ){
128332 int regIdx; /* Range of registers hold conent for pIdx */
128333 int regR; /* Range of registers holding conflicting PK */
128334 int iThisCur; /* Cursor for this UNIQUE index */
128335 int addrUniqueOk; /* Jump here if the UNIQUE constraint is satisfied */
128336 int addrConflictCk; /* First opcode in the conflict check logic */
128337
128338 if( aRegIdx[ix]==0 ) continue; /* Skip indices that do not change */
128339 if( pUpsert ){
128340 pUpsertClause = sqlite3UpsertOfIndex(pUpsert, pIdx);
128341 if( upsertIpkDelay && pUpsertClause==pUpsert ){
128342 sqlite3VdbeJumpHere(v, upsertIpkDelay);
128343 }
128344 }
128345 addrUniqueOk = sqlite3VdbeMakeLabel(pParse);
128346 if( bAffinityDone==0 ){
128347 sqlite3TableAffinity(v, pTab, regNewData+1);
128348 bAffinityDone = 1;
128349 }
128350 VdbeNoopComment((v, "prep index %s", pIdx->zName));
128351 iThisCur = iIdxCur+ix;
128352
128353
128354 /* Skip partial indices for which the WHERE clause is not true */
128355 if( pIdx->pPartIdxWhere ){
128356 sqlite3VdbeAddOp2(v, OP_Null75, 0, aRegIdx[ix]);
128357 pParse->iSelfTab = -(regNewData+1);
128358 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
128359 SQLITE_JUMPIFNULL0x10);
128360 pParse->iSelfTab = 0;
128361 }
128362
128363 /* Create a record for this index entry as it should appear after
128364 ** the insert or update. Store that record in the aRegIdx[ix] register
128365 */
128366 regIdx = aRegIdx[ix]+1;
128367 for(i=0; i<pIdx->nColumn; i++){
128368 int iField = pIdx->aiColumn[i];
128369 int x;
128370 if( iField==XN_EXPR(-2) ){
128371 pParse->iSelfTab = -(regNewData+1);
128372 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
128373 pParse->iSelfTab = 0;
128374 VdbeComment((v, "%s column %d", pIdx->zName, i));
128375 }else if( iField==XN_ROWID(-1) || iField==pTab->iPKey ){
128376 x = regNewData;
128377 sqlite3VdbeAddOp2(v, OP_IntCopy82, x, regIdx+i);
128378 VdbeComment((v, "rowid"));
128379 }else{
128380 testcase( sqlite3TableColumnToStorage(pTab, iField)!=iField );
128381 x = sqlite3TableColumnToStorage(pTab, iField) + regNewData + 1;
128382 sqlite3VdbeAddOp2(v, OP_SCopy81, x, regIdx+i);
128383 VdbeComment((v, "%s", pTab->aCol[iField].zCnName));
128384 }
128385 }
128386 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regIdx, pIdx->nColumn, aRegIdx[ix]);
128387 VdbeComment((v, "for %s", pIdx->zName));
128388#ifdef SQLITE_ENABLE_NULL_TRIM
128389 if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY2 ){
128390 sqlite3SetMakeRecordP5(v, pIdx->pTable);
128391 }
128392#endif
128393 sqlite3VdbeReleaseRegisters(pParse, regIdx, pIdx->nColumn, 0, 0);
128394
128395 /* In an UPDATE operation, if this index is the PRIMARY KEY index
128396 ** of a WITHOUT ROWID table and there has been no change the
128397 ** primary key, then no collision is possible. The collision detection
128398 ** logic below can all be skipped. */
128399 if( isUpdate && pPk==pIdx && pkChng==0 ){
128400 sqlite3VdbeResolveLabel(v, addrUniqueOk);
128401 continue;
128402 }
128403
128404 /* Find out what action to take in case there is a uniqueness conflict */
128405 onError = pIdx->onError;
128406 if( onError==OE_None0 ){
128407 sqlite3VdbeResolveLabel(v, addrUniqueOk);
128408 continue; /* pIdx is not a UNIQUE index */
128409 }
128410 if( overrideError!=OE_Default11 ){
128411 onError = overrideError;
128412 }else if( onError==OE_Default11 ){
128413 onError = OE_Abort2;
128414 }
128415
128416 /* Figure out if the upsert clause applies to this index */
128417 if( pUpsertClause ){
128418 if( pUpsertClause->isDoUpdate==0 ){
128419 onError = OE_Ignore4; /* DO NOTHING is the same as INSERT OR IGNORE */
128420 }else{
128421 onError = OE_Update6; /* DO UPDATE */
128422 }
128423 }
128424
128425 /* Collision detection may be omitted if all of the following are true:
128426 ** (1) The conflict resolution algorithm is REPLACE
128427 ** (2) The table is a WITHOUT ROWID table
128428 ** (3) There are no secondary indexes on the table
128429 ** (4) No delete triggers need to be fired if there is a conflict
128430 ** (5) No FK constraint counters need to be updated if a conflict occurs.
128431 **
128432 ** This is not possible for ENABLE_PREUPDATE_HOOK builds, as the row
128433 ** must be explicitly deleted in order to ensure any pre-update hook
128434 ** is invoked. */
128435 assert( IsOrdinaryTable(pTab) )((void) (0));
128436#ifndef SQLITE_ENABLE_PREUPDATE_HOOK1
128437 if( (ix==0 && pIdx->pNext==0) /* Condition 3 */
128438 && pPk==pIdx /* Condition 2 */
128439 && onError==OE_Replace5 /* Condition 1 */
128440 && ( 0==(db->flags&SQLITE_RecTriggers0x00002000) || /* Condition 4 */
128441 0==sqlite3TriggersExist(pParse, pTab, TK_DELETE128, 0, 0))
128442 && ( 0==(db->flags&SQLITE_ForeignKeys0x00004000) || /* Condition 5 */
128443 (0==pTab->u.tab.pFKey && 0==sqlite3FkReferences(pTab)))
128444 ){
128445 sqlite3VdbeResolveLabel(v, addrUniqueOk);
128446 continue;
128447 }
128448#endif /* ifndef SQLITE_ENABLE_PREUPDATE_HOOK */
128449
128450 /* Check to see if the new index entry will be unique */
128451 sqlite3VdbeVerifyAbortable(v, onError);
128452 addrConflictCk =
128453 sqlite3VdbeAddOp4Int(v, OP_NoConflict26, iThisCur, addrUniqueOk,
128454 regIdx, pIdx->nKeyCol); VdbeCoverage(v);
128455
128456 /* Generate code to handle collisions */
128457 regR = pIdx==pPk ? regIdx : sqlite3GetTempRange(pParse, nPkField);
128458 if( isUpdate || onError==OE_Replace5 ){
128459 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
128460 sqlite3VdbeAddOp2(v, OP_IdxRowid142, iThisCur, regR);
128461 /* Conflict only if the rowid of the existing index entry
128462 ** is different from old-rowid */
128463 if( isUpdate ){
128464 sqlite3VdbeAddOp3(v, OP_Eq53, regR, addrUniqueOk, regOldData);
128465 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL0x90);
128466 VdbeCoverage(v);
128467 }
128468 }else{
128469 int x;
128470 /* Extract the PRIMARY KEY from the end of the index entry and
128471 ** store it in registers regR..regR+nPk-1 */
128472 if( pIdx!=pPk ){
128473 for(i=0; i<pPk->nKeyCol; i++){
128474 assert( pPk->aiColumn[i]>=0 )((void) (0));
128475 x = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
128476 sqlite3VdbeAddOp3(v, OP_Column94, iThisCur, x, regR+i);
128477 VdbeComment((v, "%s.%s", pTab->zName,
128478 pTab->aCol[pPk->aiColumn[i]].zCnName));
128479 }
128480 }
128481 if( isUpdate ){
128482 /* If currently processing the PRIMARY KEY of a WITHOUT ROWID
128483 ** table, only conflict if the new PRIMARY KEY values are actually
128484 ** different from the old. See TH3 withoutrowid04.test.
128485 **
128486 ** For a UNIQUE index, only conflict if the PRIMARY KEY values
128487 ** of the matched index row are different from the original PRIMARY
128488 ** KEY values of this row before the update. */
128489 int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
128490 int op = OP_Ne52;
128491 int regCmp = (IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ? regIdx : regR);
128492
128493 for(i=0; i<pPk->nKeyCol; i++){
128494 char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
128495 x = pPk->aiColumn[i];
128496 assert( x>=0 )((void) (0));
128497 if( i==(pPk->nKeyCol-1) ){
128498 addrJump = addrUniqueOk;
128499 op = OP_Eq53;
128500 }
128501 x = sqlite3TableColumnToStorage(pTab, x);
128502 sqlite3VdbeAddOp4(v, op,
128503 regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ(-2)
128504 );
128505 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL0x90);
128506 VdbeCoverageIf(v, op==OP_Eq);
128507 VdbeCoverageIf(v, op==OP_Ne);
128508 }
128509 }
128510 }
128511 }
128512
128513 /* Generate code that executes if the new index entry is not unique */
128514 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail((void) (0))
128515 || onError==OE_Ignore || onError==OE_Replace || onError==OE_Update )((void) (0));
128516 switch( onError ){
128517 case OE_Rollback1:
128518 case OE_Abort2:
128519 case OE_Fail3: {
128520 testcase( onError==OE_Rollback );
128521 testcase( onError==OE_Abort );
128522 testcase( onError==OE_Fail );
128523 sqlite3UniqueConstraint(pParse, onError, pIdx);
128524 break;
128525 }
128526#ifndef SQLITE_OMIT_UPSERT
128527 case OE_Update6: {
128528 sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, pIdx, iIdxCur+ix);
128529 /* no break */ deliberate_fall_through
128530 }
128531#endif
128532 case OE_Ignore4: {
128533 testcase( onError==OE_Ignore );
128534 sqlite3VdbeGoto(v, ignoreDest);
128535 break;
128536 }
128537 default: {
128538 int nConflictCk; /* Number of opcodes in conflict check logic */
128539
128540 assert( onError==OE_Replace )((void) (0));
128541 nConflictCk = sqlite3VdbeCurrentAddr(v) - addrConflictCk;
128542 assert( nConflictCk>0 || db->mallocFailed )((void) (0));
128543 testcase( nConflictCk<=0 );
128544 testcase( nConflictCk>1 );
128545 if( regTrigCnt ){
128546 sqlite3MultiWrite(pParse);
128547 nReplaceTrig++;
128548 }
128549 if( pTrigger && isUpdate ){
128550 sqlite3VdbeAddOp1(v, OP_CursorLock167, iDataCur);
128551 }
128552 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
128553 regR, nPkField, 0, OE_Replace5,
128554 (pIdx==pPk ? ONEPASS_SINGLE1 : ONEPASS_OFF0), iThisCur);
128555 if( pTrigger && isUpdate ){
128556 sqlite3VdbeAddOp1(v, OP_CursorUnlock168, iDataCur);
128557 }
128558 if( regTrigCnt ){
128559 int addrBypass; /* Jump destination to bypass recheck logic */
128560
128561 sqlite3VdbeAddOp2(v, OP_AddImm86, regTrigCnt, 1); /* incr trigger cnt */
128562 addrBypass = sqlite3VdbeAddOp0(v, OP_Goto8); /* Bypass recheck */
128563 VdbeComment((v, "bypass recheck"));
128564
128565 /* Here we insert code that will be invoked after all constraint
128566 ** checks have run, if and only if one or more replace triggers
128567 ** fired. */
128568 sqlite3VdbeResolveLabel(v, lblRecheckOk);
128569 lblRecheckOk = sqlite3VdbeMakeLabel(pParse);
128570 if( pIdx->pPartIdxWhere ){
128571 /* Bypass the recheck if this partial index is not defined
128572 ** for the current row */
128573 sqlite3VdbeAddOp2(v, OP_IsNull50, regIdx-1, lblRecheckOk);
128574 VdbeCoverage(v);
128575 }
128576 /* Copy the constraint check code from above, except change
128577 ** the constraint-ok jump destination to be the address of
128578 ** the next retest block */
128579 while( nConflictCk>0 ){
128580 VdbeOp x; /* Conflict check opcode to copy */
128581 /* The sqlite3VdbeAddOp4() call might reallocate the opcode array.
128582 ** Hence, make a complete copy of the opcode, rather than using
128583 ** a pointer to the opcode. */
128584 x = *sqlite3VdbeGetOp(v, addrConflictCk);
128585 if( x.opcode!=OP_IdxRowid142 ){
128586 int p2; /* New P2 value for copied conflict check opcode */
128587 const char *zP4;
128588 if( sqlite3OpcodeProperty[x.opcode]&OPFLG_JUMP0x01 ){
128589 p2 = lblRecheckOk;
128590 }else{
128591 p2 = x.p2;
128592 }
128593 zP4 = x.p4type==P4_INT32(-3) ? SQLITE_INT_TO_PTR(x.p4.i)((void*)(long int)(x.p4.i)) : x.p4.z;
128594 sqlite3VdbeAddOp4(v, x.opcode, x.p1, p2, x.p3, zP4, x.p4type);
128595 sqlite3VdbeChangeP5(v, x.p5);
128596 VdbeCoverageIf(v, p2!=x.p2);
128597 }
128598 nConflictCk--;
128599 addrConflictCk++;
128600 }
128601 /* If the retest fails, issue an abort */
128602 sqlite3UniqueConstraint(pParse, OE_Abort2, pIdx);
128603
128604 sqlite3VdbeJumpHere(v, addrBypass); /* Terminate the recheck bypass */
128605 }
128606 seenReplace = 1;
128607 break;
128608 }
128609 }
128610 sqlite3VdbeResolveLabel(v, addrUniqueOk);
128611 if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField);
128612 if( pUpsertClause
128613 && upsertIpkReturn
128614 && sqlite3UpsertNextIsIPK(pUpsertClause)
128615 ){
128616 sqlite3VdbeGoto(v, upsertIpkDelay+1);
128617 sqlite3VdbeJumpHere(v, upsertIpkReturn);
128618 upsertIpkReturn = 0;
128619 }
128620 }
128621
128622 /* If the IPK constraint is a REPLACE, run it last */
128623 if( ipkTop ){
128624 sqlite3VdbeGoto(v, ipkTop);
128625 VdbeComment((v, "Do IPK REPLACE"));
128626 assert( ipkBottom>0 )((void) (0));
128627 sqlite3VdbeJumpHere(v, ipkBottom);
128628 }
128629
128630 /* Recheck all uniqueness constraints after replace triggers have run */
128631 testcase( regTrigCnt!=0 && nReplaceTrig==0 );
128632 assert( regTrigCnt!=0 || nReplaceTrig==0 )((void) (0));
128633 if( nReplaceTrig ){
128634 sqlite3VdbeAddOp2(v, OP_IfNot16, regTrigCnt, lblRecheckOk);VdbeCoverage(v);
128635 if( !pPk ){
128636 if( isUpdate ){
128637 sqlite3VdbeAddOp3(v, OP_Eq53, regNewData, addrRecheck, regOldData);
128638 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL0x90);
128639 VdbeCoverage(v);
128640 }
128641 sqlite3VdbeAddOp3(v, OP_NotExists30, iDataCur, addrRecheck, regNewData);
128642 VdbeCoverage(v);
128643 sqlite3RowidConstraint(pParse, OE_Abort2, pTab);
128644 }else{
128645 sqlite3VdbeGoto(v, addrRecheck);
128646 }
128647 sqlite3VdbeResolveLabel(v, lblRecheckOk);
128648 }
128649
128650 /* Generate the table record */
128651 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
128652 int regRec = aRegIdx[ix];
128653 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regNewData+1, pTab->nNVCol, regRec);
128654 sqlite3SetMakeRecordP5(v, pTab);
128655 if( !bAffinityDone ){
128656 sqlite3TableAffinity(v, pTab, 0);
128657 }
128658 }
128659
128660 *pbMayReplace = seenReplace;
128661 VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
128662}
128663
128664#ifdef SQLITE_ENABLE_NULL_TRIM
128665/*
128666** Change the P5 operand on the last opcode (which should be an OP_MakeRecord)
128667** to be the number of columns in table pTab that must not be NULL-trimmed.
128668**
128669** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
128670*/
128671SQLITE_PRIVATEstatic void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){
128672 u16 i;
128673
128674 /* Records with omitted columns are only allowed for schema format
128675 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
128676 if( pTab->pSchema->file_format<2 ) return;
128677
128678 for(i=pTab->nCol-1; i>0; i--){
128679 if( pTab->aCol[i].iDflt!=0 ) break;
128680 if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY0x0001 ) break;
128681 }
128682 sqlite3VdbeChangeP5(v, i+1);
128683}
128684#endif
128685
128686/*
128687** Table pTab is a WITHOUT ROWID table that is being written to. The cursor
128688** number is iCur, and register regData contains the new record for the
128689** PK index. This function adds code to invoke the pre-update hook,
128690** if one is registered.
128691*/
128692#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
128693static void codeWithoutRowidPreupdate(
128694 Parse *pParse, /* Parse context */
128695 Table *pTab, /* Table being updated */
128696 int iCur, /* Cursor number for table */
128697 int regData /* Data containing new record */
128698){
128699 Vdbe *v = pParse->pVdbe;
128700 int r = sqlite3GetTempReg(pParse);
128701 assert( !HasRowid(pTab) )((void) (0));
128702 assert( 0==(pParse->db->mDbFlags & DBFLAG_Vacuum) || CORRUPT_DB )((void) (0));
128703 sqlite3VdbeAddOp2(v, OP_Integer71, 0, r);
128704 sqlite3VdbeAddOp4(v, OP_Insert128, iCur, regData, r, (char*)pTab, P4_TABLE(-5));
128705 sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP0x40);
128706 sqlite3ReleaseTempReg(pParse, r);
128707}
128708#else
128709# define codeWithoutRowidPreupdate(a,b,c,d)
128710#endif
128711
128712/*
128713** This routine generates code to finish the INSERT or UPDATE operation
128714** that was started by a prior call to sqlite3GenerateConstraintChecks.
128715** A consecutive range of registers starting at regNewData contains the
128716** rowid and the content to be inserted.
128717**
128718** The arguments to this routine should be the same as the first six
128719** arguments to sqlite3GenerateConstraintChecks.
128720*/
128721SQLITE_PRIVATEstatic void sqlite3CompleteInsertion(
128722 Parse *pParse, /* The parser context */
128723 Table *pTab, /* the table into which we are inserting */
128724 int iDataCur, /* Cursor of the canonical data source */
128725 int iIdxCur, /* First index cursor */
128726 int regNewData, /* Range of content */
128727 int *aRegIdx, /* Register used by each index. 0 for unused indices */
128728 int update_flags, /* True for UPDATE, False for INSERT */
128729 int appendBias, /* True if this is likely to be an append */
128730 int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
128731){
128732 Vdbe *v; /* Prepared statements under construction */
128733 Index *pIdx; /* An index being inserted or updated */
128734 u8 pik_flags; /* flag values passed to the btree insert */
128735 int i; /* Loop counter */
128736
128737 assert( update_flags==0((void) (0))
128738 || update_flags==OPFLAG_ISUPDATE((void) (0))
128739 || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION)((void) (0))
128740 )((void) (0));
128741
128742 v = pParse->pVdbe;
128743 assert( v!=0 )((void) (0));
128744 assert( !IsView(pTab) )((void) (0)); /* This table is not a VIEW */
128745 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
128746 /* All REPLACE indexes are at the end of the list */
128747 assert( pIdx->onError!=OE_Replace((void) (0))
128748 || pIdx->pNext==0((void) (0))
128749 || pIdx->pNext->onError==OE_Replace )((void) (0));
128750 if( aRegIdx[i]==0 ) continue;
128751 if( pIdx->pPartIdxWhere ){
128752 sqlite3VdbeAddOp2(v, OP_IsNull50, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
128753 VdbeCoverage(v);
128754 }
128755 pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT0x10 : 0);
128756 if( IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) && !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
128757 pik_flags |= OPFLAG_NCHANGE0x01;
128758 pik_flags |= (update_flags & OPFLAG_SAVEPOSITION0x02);
128759 if( update_flags==0 ){
128760 codeWithoutRowidPreupdate(pParse, pTab, iIdxCur+i, aRegIdx[i]);
128761 }
128762 }
128763 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iIdxCur+i, aRegIdx[i],
128764 aRegIdx[i]+1,
128765 pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
128766 sqlite3VdbeChangeP5(v, pik_flags);
128767 }
128768 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ) return;
128769 if( pParse->nested ){
128770 pik_flags = 0;
128771 }else{
128772 pik_flags = OPFLAG_NCHANGE0x01;
128773 pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID0x20);
128774 }
128775 if( appendBias ){
128776 pik_flags |= OPFLAG_APPEND0x08;
128777 }
128778 if( useSeekResult ){
128779 pik_flags |= OPFLAG_USESEEKRESULT0x10;
128780 }
128781 sqlite3VdbeAddOp3(v, OP_Insert128, iDataCur, aRegIdx[i], regNewData);
128782 if( !pParse->nested ){
128783 sqlite3VdbeAppendP4(v, pTab, P4_TABLE(-5));
128784 }
128785 sqlite3VdbeChangeP5(v, pik_flags);
128786}
128787
128788/*
128789** Allocate cursors for the pTab table and all its indices and generate
128790** code to open and initialized those cursors.
128791**
128792** The cursor for the object that contains the complete data (normally
128793** the table itself, but the PRIMARY KEY index in the case of a WITHOUT
128794** ROWID table) is returned in *piDataCur. The first index cursor is
128795** returned in *piIdxCur. The number of indices is returned.
128796**
128797** Use iBase as the first cursor (either the *piDataCur for rowid tables
128798** or the first index for WITHOUT ROWID tables) if it is non-negative.
128799** If iBase is negative, then allocate the next available cursor.
128800**
128801** For a rowid table, *piDataCur will be exactly one less than *piIdxCur.
128802** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range
128803** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the
128804** pTab->pIndex list.
128805**
128806** If pTab is a virtual table, then this routine is a no-op and the
128807** *piDataCur and *piIdxCur values are left uninitialized.
128808*/
128809SQLITE_PRIVATEstatic int sqlite3OpenTableAndIndices(
128810 Parse *pParse, /* Parsing context */
128811 Table *pTab, /* Table to be opened */
128812 int op, /* OP_OpenRead or OP_OpenWrite */
128813 u8 p5, /* P5 value for OP_Open* opcodes (except on WITHOUT ROWID) */
128814 int iBase, /* Use this for the table cursor, if there is one */
128815 u8 *aToOpen, /* If not NULL: boolean for each table and index */
128816 int *piDataCur, /* Write the database source cursor number here */
128817 int *piIdxCur /* Write the first index cursor number here */
128818){
128819 int i;
128820 int iDb;
128821 int iDataCur;
128822 Index *pIdx;
128823 Vdbe *v;
128824
128825 assert( op==OP_OpenRead || op==OP_OpenWrite )((void) (0));
128826 assert( op==OP_OpenWrite || p5==0 )((void) (0));
128827 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
128828 /* This routine is a no-op for virtual tables. Leave the output
128829 ** variables *piDataCur and *piIdxCur set to illegal cursor numbers
128830 ** for improved error detection. */
128831 *piDataCur = *piIdxCur = -999;
128832 return 0;
128833 }
128834 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
128835 v = pParse->pVdbe;
128836 assert( v!=0 )((void) (0));
128837 if( iBase<0 ) iBase = pParse->nTab;
128838 iDataCur = iBase++;
128839 if( piDataCur ) *piDataCur = iDataCur;
128840 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) && (aToOpen==0 || aToOpen[0]) ){
128841 sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op);
128842 }else{
128843 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite113, pTab->zName);
128844 }
128845 if( piIdxCur ) *piIdxCur = iBase;
128846 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
128847 int iIdxCur = iBase++;
128848 assert( pIdx->pSchema==pTab->pSchema )((void) (0));
128849 if( IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) && !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
128850 if( piDataCur ) *piDataCur = iIdxCur;
128851 p5 = 0;
128852 }
128853 if( aToOpen==0 || aToOpen[i+1] ){
128854 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
128855 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
128856 sqlite3VdbeChangeP5(v, p5);
128857 VdbeComment((v, "%s", pIdx->zName));
128858 }
128859 }
128860 if( iBase>pParse->nTab ) pParse->nTab = iBase;
128861 return i;
128862}
128863
128864
128865#ifdef SQLITE_TEST
128866/*
128867** The following global variable is incremented whenever the
128868** transfer optimization is used. This is used for testing
128869** purposes only - to make sure the transfer optimization really
128870** is happening when it is supposed to.
128871*/
128872SQLITE_API int sqlite3_xferopt_count;
128873#endif /* SQLITE_TEST */
128874
128875
128876#ifndef SQLITE_OMIT_XFER_OPT
128877/*
128878** Check to see if index pSrc is compatible as a source of data
128879** for index pDest in an insert transfer optimization. The rules
128880** for a compatible index:
128881**
128882** * The index is over the same set of columns
128883** * The same DESC and ASC markings occurs on all columns
128884** * The same onError processing (OE_Abort, OE_Ignore, etc)
128885** * The same collating sequence on each column
128886** * The index has the exact same WHERE clause
128887*/
128888static int xferCompatibleIndex(Index *pDest, Index *pSrc){
128889 int i;
128890 assert( pDest && pSrc )((void) (0));
128891 assert( pDest->pTable!=pSrc->pTable )((void) (0));
128892 if( pDest->nKeyCol!=pSrc->nKeyCol || pDest->nColumn!=pSrc->nColumn ){
128893 return 0; /* Different number of columns */
128894 }
128895 if( pDest->onError!=pSrc->onError ){
128896 return 0; /* Different conflict resolution strategies */
128897 }
128898 for(i=0; i<pSrc->nKeyCol; i++){
128899 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
128900 return 0; /* Different columns indexed */
128901 }
128902 if( pSrc->aiColumn[i]==XN_EXPR(-2) ){
128903 assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 )((void) (0));
128904 if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr,
128905 pDest->aColExpr->a[i].pExpr, -1)!=0 ){
128906 return 0; /* Different expressions in the index */
128907 }
128908 }
128909 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
128910 return 0; /* Different sort orders */
128911 }
128912 if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){
128913 return 0; /* Different collating sequences */
128914 }
128915 }
128916 if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
128917 return 0; /* Different WHERE clauses */
128918 }
128919
128920 /* If no test above fails then the indices must be compatible */
128921 return 1;
128922}
128923
128924/*
128925** Attempt the transfer optimization on INSERTs of the form
128926**
128927** INSERT INTO tab1 SELECT * FROM tab2;
128928**
128929** The xfer optimization transfers raw records from tab2 over to tab1.
128930** Columns are not decoded and reassembled, which greatly improves
128931** performance. Raw index records are transferred in the same way.
128932**
128933** The xfer optimization is only attempted if tab1 and tab2 are compatible.
128934** There are lots of rules for determining compatibility - see comments
128935** embedded in the code for details.
128936**
128937** This routine returns TRUE if the optimization is guaranteed to be used.
128938** Sometimes the xfer optimization will only work if the destination table
128939** is empty - a factor that can only be determined at run-time. In that
128940** case, this routine generates code for the xfer optimization but also
128941** does a test to see if the destination table is empty and jumps over the
128942** xfer optimization code if the test fails. In that case, this routine
128943** returns FALSE so that the caller will know to go ahead and generate
128944** an unoptimized transfer. This routine also returns FALSE if there
128945** is no chance that the xfer optimization can be applied.
128946**
128947** This optimization is particularly useful at making VACUUM run faster.
128948*/
128949static int xferOptimization(
128950 Parse *pParse, /* Parser context */
128951 Table *pDest, /* The table we are inserting into */
128952 Select *pSelect, /* A SELECT statement to use as the data source */
128953 int onError, /* How to handle constraint errors */
128954 int iDbDest /* The database of pDest */
128955){
128956 sqlite3 *db = pParse->db;
128957 ExprList *pEList; /* The result set of the SELECT */
128958 Table *pSrc; /* The table in the FROM clause of SELECT */
128959 Index *pSrcIdx, *pDestIdx; /* Source and destination indices */
128960 SrcItem *pItem; /* An element of pSelect->pSrc */
128961 int i; /* Loop counter */
128962 int iDbSrc; /* The database of pSrc */
128963 int iSrc, iDest; /* Cursors from source and destination */
128964 int addr1, addr2; /* Loop addresses */
128965 int emptyDestTest = 0; /* Address of test for empty pDest */
128966 int emptySrcTest = 0; /* Address of test for empty pSrc */
128967 Vdbe *v; /* The VDBE we are building */
128968 int regAutoinc; /* Memory register used by AUTOINC */
128969 int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */
128970 int regData, regRowid; /* Registers holding data and rowid */
128971
128972 assert( pSelect!=0 )((void) (0));
128973 if( pParse->pWith || pSelect->pWith ){
128974 /* Do not attempt to process this query if there are an WITH clauses
128975 ** attached to it. Proceeding may generate a false "no such table: xxx"
128976 ** error if pSelect reads from a CTE named "xxx". */
128977 return 0;
128978 }
128979#ifndef SQLITE_OMIT_VIRTUALTABLE
128980 if( IsVirtual(pDest)((pDest)->eTabType==1) ){
128981 return 0; /* tab1 must not be a virtual table */
128982 }
128983#endif
128984 if( onError==OE_Default11 ){
128985 if( pDest->iPKey>=0 ) onError = pDest->keyConf;
128986 if( onError==OE_Default11 ) onError = OE_Abort2;
128987 }
128988 assert(pSelect->pSrc)((void) (0)); /* allocated even if there is no FROM clause */
128989 if( pSelect->pSrc->nSrc!=1 ){
128990 return 0; /* FROM clause must have exactly one term */
128991 }
128992 if( pSelect->pSrc->a[0].pSelect ){
128993 return 0; /* FROM clause cannot contain a subquery */
128994 }
128995 if( pSelect->pWhere ){
128996 return 0; /* SELECT may not have a WHERE clause */
128997 }
128998 if( pSelect->pOrderBy ){
128999 return 0; /* SELECT may not have an ORDER BY clause */
129000 }
129001 /* Do not need to test for a HAVING clause. If HAVING is present but
129002 ** there is no ORDER BY, we will get an error. */
129003 if( pSelect->pGroupBy ){
129004 return 0; /* SELECT may not have a GROUP BY clause */
129005 }
129006 if( pSelect->pLimit ){
129007 return 0; /* SELECT may not have a LIMIT clause */
129008 }
129009 if( pSelect->pPrior ){
129010 return 0; /* SELECT may not be a compound query */
129011 }
129012 if( pSelect->selFlags & SF_Distinct0x0000001 ){
129013 return 0; /* SELECT may not be DISTINCT */
129014 }
129015 pEList = pSelect->pEList;
129016 assert( pEList!=0 )((void) (0));
129017 if( pEList->nExpr!=1 ){
129018 return 0; /* The result set must have exactly one column */
129019 }
129020 assert( pEList->a[0].pExpr )((void) (0));
129021 if( pEList->a[0].pExpr->op!=TK_ASTERISK180 ){
129022 return 0; /* The result set must be the special operator "*" */
129023 }
129024
129025 /* At this point we have established that the statement is of the
129026 ** correct syntactic form to participate in this optimization. Now
129027 ** we have to check the semantics.
129028 */
129029 pItem = pSelect->pSrc->a;
129030 pSrc = sqlite3LocateTableItem(pParse, 0, pItem);
129031 if( pSrc==0 ){
129032 return 0; /* FROM clause does not contain a real table */
129033 }
129034 if( pSrc->tnum==pDest->tnum && pSrc->pSchema==pDest->pSchema ){
129035 testcase( pSrc!=pDest ); /* Possible due to bad sqlite_schema.rootpage */
129036 return 0; /* tab1 and tab2 may not be the same table */
129037 }
129038 if( HasRowid(pDest)(((pDest)->tabFlags & 0x00000080)==0)!=HasRowid(pSrc)(((pSrc)->tabFlags & 0x00000080)==0) ){
129039 return 0; /* source and destination must both be WITHOUT ROWID or not */
129040 }
129041 if( !IsOrdinaryTable(pSrc)((pSrc)->eTabType==0) ){
129042 return 0; /* tab2 may not be a view or virtual table */
129043 }
129044 if( pDest->nCol!=pSrc->nCol ){
129045 return 0; /* Number of columns must be the same in tab1 and tab2 */
129046 }
129047 if( pDest->iPKey!=pSrc->iPKey ){
129048 return 0; /* Both tables must have the same INTEGER PRIMARY KEY */
129049 }
129050 if( (pDest->tabFlags & TF_Strict0x00010000)!=0 && (pSrc->tabFlags & TF_Strict0x00010000)==0 ){
129051 return 0; /* Cannot feed from a non-strict into a strict table */
129052 }
129053 for(i=0; i<pDest->nCol; i++){
129054 Column *pDestCol = &pDest->aCol[i];
129055 Column *pSrcCol = &pSrc->aCol[i];
129056#ifdef SQLITE_ENABLE_HIDDEN_COLUMNS
129057 if( (db->mDbFlags & DBFLAG_Vacuum0x0004)==0
129058 && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN0x0002
129059 ){
129060 return 0; /* Neither table may have __hidden__ columns */
129061 }
129062#endif
129063#ifndef SQLITE_OMIT_GENERATED_COLUMNS
129064 /* Even if tables t1 and t2 have identical schemas, if they contain
129065 ** generated columns, then this statement is semantically incorrect:
129066 **
129067 ** INSERT INTO t2 SELECT * FROM t1;
129068 **
129069 ** The reason is that generated column values are returned by the
129070 ** the SELECT statement on the right but the INSERT statement on the
129071 ** left wants them to be omitted.
129072 **
129073 ** Nevertheless, this is a useful notational shorthand to tell SQLite
129074 ** to do a bulk transfer all of the content from t1 over to t2.
129075 **
129076 ** We could, in theory, disable this (except for internal use by the
129077 ** VACUUM command where it is actually needed). But why do that? It
129078 ** seems harmless enough, and provides a useful service.
129079 */
129080 if( (pDestCol->colFlags & COLFLAG_GENERATED0x0060) !=
129081 (pSrcCol->colFlags & COLFLAG_GENERATED0x0060) ){
129082 return 0; /* Both columns have the same generated-column type */
129083 }
129084 /* But the transfer is only allowed if both the source and destination
129085 ** tables have the exact same expressions for generated columns.
129086 ** This requirement could be relaxed for VIRTUAL columns, I suppose.
129087 */
129088 if( (pDestCol->colFlags & COLFLAG_GENERATED0x0060)!=0 ){
129089 if( sqlite3ExprCompare(0,
129090 sqlite3ColumnExpr(pSrc, pSrcCol),
129091 sqlite3ColumnExpr(pDest, pDestCol), -1)!=0 ){
129092 testcase( pDestCol->colFlags & COLFLAG_VIRTUAL );
129093 testcase( pDestCol->colFlags & COLFLAG_STORED );
129094 return 0; /* Different generator expressions */
129095 }
129096 }
129097#endif
129098 if( pDestCol->affinity!=pSrcCol->affinity ){
129099 return 0; /* Affinity must be the same on all columns */
129100 }
129101 if( sqlite3_stricmp(sqlite3ColumnColl(pDestCol),
129102 sqlite3ColumnColl(pSrcCol))!=0 ){
129103 return 0; /* Collating sequence must be the same on all columns */
129104 }
129105 if( pDestCol->notNull && !pSrcCol->notNull ){
129106 return 0; /* tab2 must be NOT NULL if tab1 is */
129107 }
129108 /* Default values for second and subsequent columns need to match. */
129109 if( (pDestCol->colFlags & COLFLAG_GENERATED0x0060)==0 && i>0 ){
129110 Expr *pDestExpr = sqlite3ColumnExpr(pDest, pDestCol);
129111 Expr *pSrcExpr = sqlite3ColumnExpr(pSrc, pSrcCol);
129112 assert( pDestExpr==0 || pDestExpr->op==TK_SPAN )((void) (0));
129113 assert( pDestExpr==0 || !ExprHasProperty(pDestExpr, EP_IntValue) )((void) (0));
129114 assert( pSrcExpr==0 || pSrcExpr->op==TK_SPAN )((void) (0));
129115 assert( pSrcExpr==0 || !ExprHasProperty(pSrcExpr, EP_IntValue) )((void) (0));
129116 if( (pDestExpr==0)!=(pSrcExpr==0)
129117 || (pDestExpr!=0 && strcmp(pDestExpr->u.zToken,
129118 pSrcExpr->u.zToken)!=0)
129119 ){
129120 return 0; /* Default values must be the same for all columns */
129121 }
129122 }
129123 }
129124 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
129125 if( IsUniqueIndex(pDestIdx)((pDestIdx)->onError!=0) ){
129126 destHasUniqueIdx = 1;
129127 }
129128 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
129129 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
129130 }
129131 if( pSrcIdx==0 ){
129132 return 0; /* pDestIdx has no corresponding index in pSrc */
129133 }
129134 if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema
129135 && sqlite3FaultSim(411)==SQLITE_OK0 ){
129136 /* The sqlite3FaultSim() call allows this corruption test to be
129137 ** bypassed during testing, in order to exercise other corruption tests
129138 ** further downstream. */
129139 return 0; /* Corrupt schema - two indexes on the same btree */
129140 }
129141 }
129142#ifndef SQLITE_OMIT_CHECK
129143 if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
129144 return 0; /* Tables have different CHECK constraints. Ticket #2252 */
129145 }
129146#endif
129147#ifndef SQLITE_OMIT_FOREIGN_KEY
129148 /* Disallow the transfer optimization if the destination table constains
129149 ** any foreign key constraints. This is more restrictive than necessary.
129150 ** But the main beneficiary of the transfer optimization is the VACUUM
129151 ** command, and the VACUUM command disables foreign key constraints. So
129152 ** the extra complication to make this rule less restrictive is probably
129153 ** not worth the effort. Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
129154 */
129155 assert( IsOrdinaryTable(pDest) )((void) (0));
129156 if( (db->flags & SQLITE_ForeignKeys0x00004000)!=0 && pDest->u.tab.pFKey!=0 ){
129157 return 0;
129158 }
129159#endif
129160 if( (db->flags & SQLITE_CountRows((u64)(0x00001)<<32))!=0 ){
129161 return 0; /* xfer opt does not play well with PRAGMA count_changes */
129162 }
129163
129164 /* If we get this far, it means that the xfer optimization is at
129165 ** least a possibility, though it might only work if the destination
129166 ** table (tab1) is initially empty.
129167 */
129168#ifdef SQLITE_TEST
129169 sqlite3_xferopt_count++;
129170#endif
129171 iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
129172 v = sqlite3GetVdbe(pParse);
129173 sqlite3CodeVerifySchema(pParse, iDbSrc);
129174 iSrc = pParse->nTab++;
129175 iDest = pParse->nTab++;
129176 regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
129177 regData = sqlite3GetTempReg(pParse);
129178 sqlite3VdbeAddOp2(v, OP_Null75, 0, regData);
129179 regRowid = sqlite3GetTempReg(pParse);
129180 sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite113);
129181 assert( HasRowid(pDest) || destHasUniqueIdx )((void) (0));
129182 if( (db->mDbFlags & DBFLAG_Vacuum0x0004)==0 && (
129183 (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
129184 || destHasUniqueIdx /* (2) */
129185 || (onError!=OE_Abort2 && onError!=OE_Rollback1) /* (3) */
129186 )){
129187 /* In some circumstances, we are able to run the xfer optimization
129188 ** only if the destination table is initially empty. Unless the
129189 ** DBFLAG_Vacuum flag is set, this block generates code to make
129190 ** that determination. If DBFLAG_Vacuum is set, then the destination
129191 ** table is always empty.
129192 **
129193 ** Conditions under which the destination must be empty:
129194 **
129195 ** (1) There is no INTEGER PRIMARY KEY but there are indices.
129196 ** (If the destination is not initially empty, the rowid fields
129197 ** of index entries might need to change.)
129198 **
129199 ** (2) The destination has a unique index. (The xfer optimization
129200 ** is unable to test uniqueness.)
129201 **
129202 ** (3) onError is something other than OE_Abort and OE_Rollback.
129203 */
129204 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind35, iDest, 0); VdbeCoverage(v);
129205 emptyDestTest = sqlite3VdbeAddOp0(v, OP_Goto8);
129206 sqlite3VdbeJumpHere(v, addr1);
129207 }
129208 if( HasRowid(pSrc)(((pSrc)->tabFlags & 0x00000080)==0) ){
129209 u8 insFlags;
129210 sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead112);
129211 emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind35, iSrc, 0); VdbeCoverage(v);
129212 if( pDest->iPKey>=0 ){
129213 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid135, iSrc, regRowid);
129214 if( (db->mDbFlags & DBFLAG_Vacuum0x0004)==0 ){
129215 sqlite3VdbeVerifyAbortable(v, onError);
129216 addr2 = sqlite3VdbeAddOp3(v, OP_NotExists30, iDest, 0, regRowid);
129217 VdbeCoverage(v);
129218 sqlite3RowidConstraint(pParse, onError, pDest);
129219 sqlite3VdbeJumpHere(v, addr2);
129220 }
129221 autoIncStep(pParse, regAutoinc, regRowid);
129222 }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto0x0008) ){
129223 addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid127, iDest, regRowid);
129224 }else{
129225 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid135, iSrc, regRowid);
129226 assert( (pDest->tabFlags & TF_Autoincrement)==0 )((void) (0));
129227 }
129228
129229 if( db->mDbFlags & DBFLAG_Vacuum0x0004 ){
129230 sqlite3VdbeAddOp1(v, OP_SeekEnd137, iDest);
129231 insFlags = OPFLAG_APPEND0x08|OPFLAG_USESEEKRESULT0x10|OPFLAG_PREFORMAT0x80;
129232 }else{
129233 insFlags = OPFLAG_NCHANGE0x01|OPFLAG_LASTROWID0x20|OPFLAG_APPEND0x08|OPFLAG_PREFORMAT0x80;
129234 }
129235#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
129236 if( (db->mDbFlags & DBFLAG_Vacuum0x0004)==0 ){
129237 sqlite3VdbeAddOp3(v, OP_RowData134, iSrc, regData, 1);
129238 insFlags &= ~OPFLAG_PREFORMAT0x80;
129239 }else
129240#endif
129241 {
129242 sqlite3VdbeAddOp3(v, OP_RowCell129, iDest, iSrc, regRowid);
129243 }
129244 sqlite3VdbeAddOp3(v, OP_Insert128, iDest, regData, regRowid);
129245 if( (db->mDbFlags & DBFLAG_Vacuum0x0004)==0 ){
129246 sqlite3VdbeChangeP4(v, -1, (char*)pDest, P4_TABLE(-5));
129247 }
129248 sqlite3VdbeChangeP5(v, insFlags);
129249
129250 sqlite3VdbeAddOp2(v, OP_Next38, iSrc, addr1); VdbeCoverage(v);
129251 sqlite3VdbeAddOp2(v, OP_Close122, iSrc, 0);
129252 sqlite3VdbeAddOp2(v, OP_Close122, iDest, 0);
129253 }else{
129254 sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
129255 sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
129256 }
129257 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
129258 u8 idxInsFlags = 0;
129259 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx)(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
129260 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
129261 }
129262 assert( pSrcIdx )((void) (0));
129263 sqlite3VdbeAddOp3(v, OP_OpenRead112, iSrc, pSrcIdx->tnum, iDbSrc);
129264 sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
129265 VdbeComment((v, "%s", pSrcIdx->zName));
129266 sqlite3VdbeAddOp3(v, OP_OpenWrite113, iDest, pDestIdx->tnum, iDbDest);
129267 sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
129268 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR0x01);
129269 VdbeComment((v, "%s", pDestIdx->zName));
129270 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind35, iSrc, 0); VdbeCoverage(v);
129271 if( db->mDbFlags & DBFLAG_Vacuum0x0004 ){
129272 /* This INSERT command is part of a VACUUM operation, which guarantees
129273 ** that the destination table is empty. If all indexed columns use
129274 ** collation sequence BINARY, then it can also be assumed that the
129275 ** index will be populated by inserting keys in strictly sorted
129276 ** order. In this case, instead of seeking within the b-tree as part
129277 ** of every OP_IdxInsert opcode, an OP_SeekEnd is added before the
129278 ** OP_IdxInsert to seek to the point within the b-tree where each key
129279 ** should be inserted. This is faster.
129280 **
129281 ** If any of the indexed columns use a collation sequence other than
129282 ** BINARY, this optimization is disabled. This is because the user
129283 ** might change the definition of a collation sequence and then run
129284 ** a VACUUM command. In that case keys may not be written in strictly
129285 ** sorted order. */
129286 for(i=0; i<pSrcIdx->nColumn; i++){
129287 const char *zColl = pSrcIdx->azColl[i];
129288 if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break;
129289 }
129290 if( i==pSrcIdx->nColumn ){
129291 idxInsFlags = OPFLAG_USESEEKRESULT0x10|OPFLAG_PREFORMAT0x80;
129292 sqlite3VdbeAddOp1(v, OP_SeekEnd137, iDest);
129293 sqlite3VdbeAddOp2(v, OP_RowCell129, iDest, iSrc);
129294 }
129295 }else if( !HasRowid(pSrc)(((pSrc)->tabFlags & 0x00000080)==0) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY2 ){
129296 idxInsFlags |= OPFLAG_NCHANGE0x01;
129297 }
129298 if( idxInsFlags!=(OPFLAG_USESEEKRESULT0x10|OPFLAG_PREFORMAT0x80) ){
129299 sqlite3VdbeAddOp3(v, OP_RowData134, iSrc, regData, 1);
129300 if( (db->mDbFlags & DBFLAG_Vacuum0x0004)==0
129301 && !HasRowid(pDest)(((pDest)->tabFlags & 0x00000080)==0)
129302 && IsPrimaryKeyIndex(pDestIdx)((pDestIdx)->idxType==2)
129303 ){
129304 codeWithoutRowidPreupdate(pParse, pDest, iDest, regData);
129305 }
129306 }
129307 sqlite3VdbeAddOp2(v, OP_IdxInsert138, iDest, regData);
129308 sqlite3VdbeChangeP5(v, idxInsFlags|OPFLAG_APPEND0x08);
129309 sqlite3VdbeAddOp2(v, OP_Next38, iSrc, addr1+1); VdbeCoverage(v);
129310 sqlite3VdbeJumpHere(v, addr1);
129311 sqlite3VdbeAddOp2(v, OP_Close122, iSrc, 0);
129312 sqlite3VdbeAddOp2(v, OP_Close122, iDest, 0);
129313 }
129314 if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest);
129315 sqlite3ReleaseTempReg(pParse, regRowid);
129316 sqlite3ReleaseTempReg(pParse, regData);
129317 if( emptyDestTest ){
129318 sqlite3AutoincrementEnd(pParse);
129319 sqlite3VdbeAddOp2(v, OP_Halt70, SQLITE_OK0, 0);
129320 sqlite3VdbeJumpHere(v, emptyDestTest);
129321 sqlite3VdbeAddOp2(v, OP_Close122, iDest, 0);
129322 return 0;
129323 }else{
129324 return 1;
129325 }
129326}
129327#endif /* SQLITE_OMIT_XFER_OPT */
129328
129329/************** End of insert.c **********************************************/
129330/************** Begin file legacy.c ******************************************/
129331/*
129332** 2001 September 15
129333**
129334** The author disclaims copyright to this source code. In place of
129335** a legal notice, here is a blessing:
129336**
129337** May you do good and not evil.
129338** May you find forgiveness for yourself and forgive others.
129339** May you share freely, never taking more than you give.
129340**
129341*************************************************************************
129342** Main file for the SQLite library. The routines in this file
129343** implement the programmer interface to the library. Routines in
129344** other files are for internal use by SQLite and should not be
129345** accessed by users of the library.
129346*/
129347
129348/* #include "sqliteInt.h" */
129349
129350/*
129351** Execute SQL code. Return one of the SQLITE_ success/failure
129352** codes. Also write an error message into memory obtained from
129353** malloc() and make *pzErrMsg point to that message.
129354**
129355** If the SQL is a query, then for each row in the query result
129356** the xCallback() function is called. pArg becomes the first
129357** argument to xCallback(). If xCallback=NULL then no callback
129358** is invoked, even for queries.
129359*/
129360SQLITE_API int sqlite3_exec(
129361 sqlite3 *db, /* The database on which the SQL executes */
129362 const char *zSql, /* The SQL to be executed */
129363 sqlite3_callback xCallback, /* Invoke this callback routine */
129364 void *pArg, /* First argument to xCallback() */
129365 char **pzErrMsg /* Write error messages here */
129366){
129367 int rc = SQLITE_OK0; /* Return code */
129368 const char *zLeftover; /* Tail of unprocessed SQL */
129369 sqlite3_stmt *pStmt = 0; /* The current SQL statement */
129370 char **azCols = 0; /* Names of result columns */
129371 int callbackIsInit; /* True if callback data is initialized */
129372
129373 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(129373);
129374 if( zSql==0 ) zSql = "";
129375
129376 sqlite3_mutex_enter(db->mutex);
129377 sqlite3Error(db, SQLITE_OK0);
129378 while( rc==SQLITE_OK0 && zSql[0] ){
129379 int nCol = 0;
129380 char **azVals = 0;
129381
129382 pStmt = 0;
129383 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
129384 assert( rc==SQLITE_OK || pStmt==0 )((void) (0));
129385 if( rc!=SQLITE_OK0 ){
129386 continue;
129387 }
129388 if( !pStmt ){
129389 /* this happens for a comment or white-space */
129390 zSql = zLeftover;
129391 continue;
129392 }
129393 callbackIsInit = 0;
129394
129395 while( 1 ){
129396 int i;
129397 rc = sqlite3_step(pStmt);
129398
129399 /* Invoke the callback function if required */
129400 if( xCallback && (SQLITE_ROW100==rc ||
129401 (SQLITE_DONE101==rc && !callbackIsInit
129402 && db->flags&SQLITE_NullCallback0x00000100)) ){
129403 if( !callbackIsInit ){
129404 nCol = sqlite3_column_count(pStmt);
129405 azCols = sqlite3DbMallocRaw(db, (2*nCol+1)*sizeof(const char*));
129406 if( azCols==0 ){
129407 goto exec_out;
129408 }
129409 for(i=0; i<nCol; i++){
129410 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
129411 /* sqlite3VdbeSetColName() installs column names as UTF8
129412 ** strings so there is no way for sqlite3_column_name() to fail. */
129413 assert( azCols[i]!=0 )((void) (0));
129414 }
129415 callbackIsInit = 1;
129416 }
129417 if( rc==SQLITE_ROW100 ){
129418 azVals = &azCols[nCol];
129419 for(i=0; i<nCol; i++){
129420 azVals[i] = (char *)sqlite3_column_text(pStmt, i);
129421 if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL5 ){
129422 sqlite3OomFault(db);
129423 goto exec_out;
129424 }
129425 }
129426 azVals[i] = 0;
129427 }
129428 if( xCallback(pArg, nCol, azVals, azCols) ){
129429 /* EVIDENCE-OF: R-38229-40159 If the callback function to
129430 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
129431 ** return SQLITE_ABORT. */
129432 rc = SQLITE_ABORT4;
129433 sqlite3VdbeFinalize((Vdbe *)pStmt);
129434 pStmt = 0;
129435 sqlite3Error(db, SQLITE_ABORT4);
129436 goto exec_out;
129437 }
129438 }
129439
129440 if( rc!=SQLITE_ROW100 ){
129441 rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
129442 pStmt = 0;
129443 zSql = zLeftover;
129444 while( sqlite3Isspace(zSql[0])(sqlite3CtypeMap[(unsigned char)(zSql[0])]&0x01) ) zSql++;
129445 break;
129446 }
129447 }
129448
129449 sqlite3DbFree(db, azCols);
129450 azCols = 0;
129451 }
129452
129453exec_out:
129454 if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
129455 sqlite3DbFree(db, azCols);
129456
129457 rc = sqlite3ApiExit(db, rc);
129458 if( rc!=SQLITE_OK0 && pzErrMsg ){
129459 *pzErrMsg = sqlite3DbStrDup(0, sqlite3_errmsg(db));
129460 if( *pzErrMsg==0 ){
129461 rc = SQLITE_NOMEM_BKPT7;
129462 sqlite3Error(db, SQLITE_NOMEM7);
129463 }
129464 }else if( pzErrMsg ){
129465 *pzErrMsg = 0;
129466 }
129467
129468 assert( (rc&db->errMask)==rc )((void) (0));
129469 sqlite3_mutex_leave(db->mutex);
129470 return rc;
129471}
129472
129473/************** End of legacy.c **********************************************/
129474/************** Begin file loadext.c *****************************************/
129475/*
129476** 2006 June 7
129477**
129478** The author disclaims copyright to this source code. In place of
129479** a legal notice, here is a blessing:
129480**
129481** May you do good and not evil.
129482** May you find forgiveness for yourself and forgive others.
129483** May you share freely, never taking more than you give.
129484**
129485*************************************************************************
129486** This file contains code used to dynamically load extensions into
129487** the SQLite library.
129488*/
129489
129490#ifndef SQLITE_CORE1
129491 #define SQLITE_CORE1 1 /* Disable the API redefinition in sqlite3ext.h */
129492#endif
129493/************** Include sqlite3ext.h in the middle of loadext.c **************/
129494/************** Begin file sqlite3ext.h **************************************/
129495/*
129496** 2006 June 7
129497**
129498** The author disclaims copyright to this source code. In place of
129499** a legal notice, here is a blessing:
129500**
129501** May you do good and not evil.
129502** May you find forgiveness for yourself and forgive others.
129503** May you share freely, never taking more than you give.
129504**
129505*************************************************************************
129506** This header file defines the SQLite interface for use by
129507** shared libraries that want to be imported as extensions into
129508** an SQLite instance. Shared libraries that intend to be loaded
129509** as extensions by SQLite should #include this file instead of
129510** sqlite3.h.
129511*/
129512#ifndef SQLITE3EXT_H
129513#define SQLITE3EXT_H
129514/* #include "sqlite3.h" */
129515
129516/*
129517** The following structure holds pointers to all of the SQLite API
129518** routines.
129519**
129520** WARNING: In order to maintain backwards compatibility, add new
129521** interfaces to the end of this structure only. If you insert new
129522** interfaces in the middle of this structure, then older different
129523** versions of SQLite will not be able to load each other's shared
129524** libraries!
129525*/
129526struct sqlite3_api_routines {
129527 void * (*aggregate_context)(sqlite3_context*,int nBytes);
129528 int (*aggregate_count)(sqlite3_context*);
129529 int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
129530 int (*bind_double)(sqlite3_stmt*,int,double);
129531 int (*bind_int)(sqlite3_stmt*,int,int);
129532 int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
129533 int (*bind_null)(sqlite3_stmt*,int);
129534 int (*bind_parameter_count)(sqlite3_stmt*);
129535 int (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
129536 const char * (*bind_parameter_name)(sqlite3_stmt*,int);
129537 int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
129538 int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
129539 int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
129540 int (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
129541 int (*busy_timeout)(sqlite3*,int ms);
129542 int (*changes)(sqlite3*);
129543 int (*close)(sqlite3*);
129544 int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
129545 int eTextRep,const char*));
129546 int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
129547 int eTextRep,const void*));
129548 const void * (*column_blob)(sqlite3_stmt*,int iCol);
129549 int (*column_bytes)(sqlite3_stmt*,int iCol);
129550 int (*column_bytes16)(sqlite3_stmt*,int iCol);
129551 int (*column_count)(sqlite3_stmt*pStmt);
129552 const char * (*column_database_name)(sqlite3_stmt*,int);
129553 const void * (*column_database_name16)(sqlite3_stmt*,int);
129554 const char * (*column_decltype)(sqlite3_stmt*,int i);
129555 const void * (*column_decltype16)(sqlite3_stmt*,int);
129556 double (*column_double)(sqlite3_stmt*,int iCol);
129557 int (*column_int)(sqlite3_stmt*,int iCol);
129558 sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol);
129559 const char * (*column_name)(sqlite3_stmt*,int);
129560 const void * (*column_name16)(sqlite3_stmt*,int);
129561 const char * (*column_origin_name)(sqlite3_stmt*,int);
129562 const void * (*column_origin_name16)(sqlite3_stmt*,int);
129563 const char * (*column_table_name)(sqlite3_stmt*,int);
129564 const void * (*column_table_name16)(sqlite3_stmt*,int);
129565 const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
129566 const void * (*column_text16)(sqlite3_stmt*,int iCol);
129567 int (*column_type)(sqlite3_stmt*,int iCol);
129568 sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
129569 void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
129570 int (*complete)(const char*sql);
129571 int (*complete16)(const void*sql);
129572 int (*create_collation)(sqlite3*,const char*,int,void*,
129573 int(*)(void*,int,const void*,int,const void*));
129574 int (*create_collation16)(sqlite3*,const void*,int,void*,
129575 int(*)(void*,int,const void*,int,const void*));
129576 int (*create_function)(sqlite3*,const char*,int,int,void*,
129577 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
129578 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
129579 void (*xFinal)(sqlite3_context*));
129580 int (*create_function16)(sqlite3*,const void*,int,int,void*,
129581 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
129582 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
129583 void (*xFinal)(sqlite3_context*));
129584 int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
129585 int (*data_count)(sqlite3_stmt*pStmt);
129586 sqlite3 * (*db_handle)(sqlite3_stmt*);
129587 int (*declare_vtab)(sqlite3*,const char*);
129588 int (*enable_shared_cache)(int);
129589 int (*errcode)(sqlite3*db);
129590 const char * (*errmsg)(sqlite3*);
129591 const void * (*errmsg16)(sqlite3*);
129592 int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
129593 int (*expired)(sqlite3_stmt*);
129594 int (*finalize)(sqlite3_stmt*pStmt);
129595 void (*free)(void*);
129596 void (*free_table)(char**result);
129597 int (*get_autocommit)(sqlite3*);
129598 void * (*get_auxdata)(sqlite3_context*,int);
129599 int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
129600 int (*global_recover)(void);
129601 void (*interruptx)(sqlite3*);
129602 sqlite_int64 (*last_insert_rowid)(sqlite3*);
129603 const char * (*libversion)(void);
129604 int (*libversion_number)(void);
129605 void *(*malloc)(int);
129606 char * (*mprintf)(const char*,...);
129607 int (*open)(const char*,sqlite3**);
129608 int (*open16)(const void*,sqlite3**);
129609 int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
129610 int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
129611 void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
129612 void (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
129613 void *(*realloc)(void*,int);
129614 int (*reset)(sqlite3_stmt*pStmt);
129615 void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
129616 void (*result_double)(sqlite3_context*,double);
129617 void (*result_error)(sqlite3_context*,const char*,int);
129618 void (*result_error16)(sqlite3_context*,const void*,int);
129619 void (*result_int)(sqlite3_context*,int);
129620 void (*result_int64)(sqlite3_context*,sqlite_int64);
129621 void (*result_null)(sqlite3_context*);
129622 void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
129623 void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
129624 void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
129625 void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
129626 void (*result_value)(sqlite3_context*,sqlite3_value*);
129627 void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
129628 int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
129629 const char*,const char*),void*);
129630 void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
129631 char * (*xsnprintf)(int,char*,const char*,...);
129632 int (*step)(sqlite3_stmt*);
129633 int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
129634 char const**,char const**,int*,int*,int*);
129635 void (*thread_cleanup)(void);
129636 int (*total_changes)(sqlite3*);
129637 void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
129638 int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
129639 void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
129640 sqlite_int64),void*);
129641 void * (*user_data)(sqlite3_context*);
129642 const void * (*value_blob)(sqlite3_value*);
129643 int (*value_bytes)(sqlite3_value*);
129644 int (*value_bytes16)(sqlite3_value*);
129645 double (*value_double)(sqlite3_value*);
129646 int (*value_int)(sqlite3_value*);
129647 sqlite_int64 (*value_int64)(sqlite3_value*);
129648 int (*value_numeric_type)(sqlite3_value*);
129649 const unsigned char * (*value_text)(sqlite3_value*);
129650 const void * (*value_text16)(sqlite3_value*);
129651 const void * (*value_text16be)(sqlite3_value*);
129652 const void * (*value_text16le)(sqlite3_value*);
129653 int (*value_type)(sqlite3_value*);
129654 char *(*vmprintf)(const char*,va_list);
129655 /* Added ??? */
129656 int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
129657 /* Added by 3.3.13 */
129658 int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
129659 int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
129660 int (*clear_bindings)(sqlite3_stmt*);
129661 /* Added by 3.4.1 */
129662 int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
129663 void (*xDestroy)(void *));
129664 /* Added by 3.5.0 */
129665 int (*bind_zeroblob)(sqlite3_stmt*,int,int);
129666 int (*blob_bytes)(sqlite3_blob*);
129667 int (*blob_close)(sqlite3_blob*);
129668 int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
129669 int,sqlite3_blob**);
129670 int (*blob_read)(sqlite3_blob*,void*,int,int);
129671 int (*blob_write)(sqlite3_blob*,const void*,int,int);
129672 int (*create_collation_v2)(sqlite3*,const char*,int,void*,
129673 int(*)(void*,int,const void*,int,const void*),
129674 void(*)(void*));
129675 int (*file_control)(sqlite3*,const char*,int,void*);
129676 sqlite3_int64 (*memory_highwater)(int);
129677 sqlite3_int64 (*memory_used)(void);
129678 sqlite3_mutex *(*mutex_alloc)(int);
129679 void (*mutex_enter)(sqlite3_mutex*);
129680 void (*mutex_free)(sqlite3_mutex*);
129681 void (*mutex_leave)(sqlite3_mutex*);
129682 int (*mutex_try)(sqlite3_mutex*);
129683 int (*open_v2)(const char*,sqlite3**,int,const char*);
129684 int (*release_memory)(int);
129685 void (*result_error_nomem)(sqlite3_context*);
129686 void (*result_error_toobig)(sqlite3_context*);
129687 int (*sleep)(int);
129688 void (*soft_heap_limit)(int);
129689 sqlite3_vfs *(*vfs_find)(const char*);
129690 int (*vfs_register)(sqlite3_vfs*,int);
129691 int (*vfs_unregister)(sqlite3_vfs*);
129692 int (*xthreadsafe)(void);
129693 void (*result_zeroblob)(sqlite3_context*,int);
129694 void (*result_error_code)(sqlite3_context*,int);
129695 int (*test_control)(int, ...);
129696 void (*randomness)(int,void*);
129697 sqlite3 *(*context_db_handle)(sqlite3_context*);
129698 int (*extended_result_codes)(sqlite3*,int);
129699 int (*limit)(sqlite3*,int,int);
129700 sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
129701 const char *(*sql)(sqlite3_stmt*);
129702 int (*status)(int,int*,int*,int);
129703 int (*backup_finish)(sqlite3_backup*);
129704 sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
129705 int (*backup_pagecount)(sqlite3_backup*);
129706 int (*backup_remaining)(sqlite3_backup*);
129707 int (*backup_step)(sqlite3_backup*,int);
129708 const char *(*compileoption_get)(int);
129709 int (*compileoption_used)(const char*);
129710 int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
129711 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
129712 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
129713 void (*xFinal)(sqlite3_context*),
129714 void(*xDestroy)(void*));
129715 int (*db_config)(sqlite3*,int,...);
129716 sqlite3_mutex *(*db_mutex)(sqlite3*);
129717 int (*db_status)(sqlite3*,int,int*,int*,int);
129718 int (*extended_errcode)(sqlite3*);
129719 void (*log)(int,const char*,...);
129720 sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
129721 const char *(*sourceid)(void);
129722 int (*stmt_status)(sqlite3_stmt*,int,int);
129723 int (*strnicmp)(const char*,const char*,int);
129724 int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
129725 int (*wal_autocheckpoint)(sqlite3*,int);
129726 int (*wal_checkpoint)(sqlite3*,const char*);
129727 void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
129728 int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
129729 int (*vtab_config)(sqlite3*,int op,...);
129730 int (*vtab_on_conflict)(sqlite3*);
129731 /* Version 3.7.16 and later */
129732 int (*close_v2)(sqlite3*);
129733 const char *(*db_filename)(sqlite3*,const char*);
129734 int (*db_readonly)(sqlite3*,const char*);
129735 int (*db_release_memory)(sqlite3*);
129736 const char *(*errstr)(int);
129737 int (*stmt_busy)(sqlite3_stmt*);
129738 int (*stmt_readonly)(sqlite3_stmt*);
129739 int (*stricmp)(const char*,const char*);
129740 int (*uri_boolean)(const char*,const char*,int);
129741 sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64);
129742 const char *(*uri_parameter)(const char*,const char*);
129743 char *(*xvsnprintf)(int,char*,const char*,va_list);
129744 int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*);
129745 /* Version 3.8.7 and later */
129746 int (*auto_extension)(void(*)(void));
129747 int (*bind_blob64)(sqlite3_stmt*,int,const void*,sqlite3_uint64,
129748 void(*)(void*));
129749 int (*bind_text64)(sqlite3_stmt*,int,const char*,sqlite3_uint64,
129750 void(*)(void*),unsigned char);
129751 int (*cancel_auto_extension)(void(*)(void));
129752 int (*load_extension)(sqlite3*,const char*,const char*,char**);
129753 void *(*malloc64)(sqlite3_uint64);
129754 sqlite3_uint64 (*msize)(void*);
129755 void *(*realloc64)(void*,sqlite3_uint64);
129756 void (*reset_auto_extension)(void);
129757 void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64,
129758 void(*)(void*));
129759 void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
129760 void(*)(void*), unsigned char);
129761 int (*strglob)(const char*,const char*);
129762 /* Version 3.8.11 and later */
129763 sqlite3_value *(*value_dup)(const sqlite3_value*);
129764 void (*value_free)(sqlite3_value*);
129765 int (*result_zeroblob64)(sqlite3_context*,sqlite3_uint64);
129766 int (*bind_zeroblob64)(sqlite3_stmt*, int, sqlite3_uint64);
129767 /* Version 3.9.0 and later */
129768 unsigned int (*value_subtype)(sqlite3_value*);
129769 void (*result_subtype)(sqlite3_context*,unsigned int);
129770 /* Version 3.10.0 and later */
129771 int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int);
129772 int (*strlike)(const char*,const char*,unsigned int);
129773 int (*db_cacheflush)(sqlite3*);
129774 /* Version 3.12.0 and later */
129775 int (*system_errno)(sqlite3*);
129776 /* Version 3.14.0 and later */
129777 int (*trace_v2)(sqlite3*,unsigned,int(*)(unsigned,void*,void*,void*),void*);
129778 char *(*expanded_sql)(sqlite3_stmt*);
129779 /* Version 3.18.0 and later */
129780 void (*set_last_insert_rowid)(sqlite3*,sqlite3_int64);
129781 /* Version 3.20.0 and later */
129782 int (*prepare_v3)(sqlite3*,const char*,int,unsigned int,
129783 sqlite3_stmt**,const char**);
129784 int (*prepare16_v3)(sqlite3*,const void*,int,unsigned int,
129785 sqlite3_stmt**,const void**);
129786 int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*,void(*)(void*));
129787 void (*result_pointer)(sqlite3_context*,void*,const char*,void(*)(void*));
129788 void *(*value_pointer)(sqlite3_value*,const char*);
129789 int (*vtab_nochange)(sqlite3_context*);
129790 int (*value_nochange)(sqlite3_value*);
129791 const char *(*vtab_collation)(sqlite3_index_info*,int);
129792 /* Version 3.24.0 and later */
129793 int (*keyword_count)(void);
129794 int (*keyword_name)(int,const char**,int*);
129795 int (*keyword_check)(const char*,int);
129796 sqlite3_str *(*str_new)(sqlite3*);
129797 char *(*str_finish)(sqlite3_str*);
129798 void (*str_appendf)(sqlite3_str*, const char *zFormat, ...);
129799 void (*str_vappendf)(sqlite3_str*, const char *zFormat, va_list);
129800 void (*str_append)(sqlite3_str*, const char *zIn, int N);
129801 void (*str_appendall)(sqlite3_str*, const char *zIn);
129802 void (*str_appendchar)(sqlite3_str*, int N, char C);
129803 void (*str_reset)(sqlite3_str*);
129804 int (*str_errcode)(sqlite3_str*);
129805 int (*str_length)(sqlite3_str*);
129806 char *(*str_value)(sqlite3_str*);
129807 /* Version 3.25.0 and later */
129808 int (*create_window_function)(sqlite3*,const char*,int,int,void*,
129809 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
129810 void (*xFinal)(sqlite3_context*),
129811 void (*xValue)(sqlite3_context*),
129812 void (*xInv)(sqlite3_context*,int,sqlite3_value**),
129813 void(*xDestroy)(void*));
129814 /* Version 3.26.0 and later */
129815 const char *(*normalized_sql)(sqlite3_stmt*);
129816 /* Version 3.28.0 and later */
129817 int (*stmt_isexplain)(sqlite3_stmt*);
129818 int (*value_frombind)(sqlite3_value*);
129819 /* Version 3.30.0 and later */
129820 int (*drop_modules)(sqlite3*,const char**);
129821 /* Version 3.31.0 and later */
129822 sqlite3_int64 (*hard_heap_limit64)(sqlite3_int64);
129823 const char *(*uri_key)(const char*,int);
129824 const char *(*filename_database)(const char*);
129825 const char *(*filename_journal)(const char*);
129826 const char *(*filename_wal)(const char*);
129827 /* Version 3.32.0 and later */
129828 char *(*create_filename)(const char*,const char*,const char*,
129829 int,const char**);
129830 void (*free_filename)(char*);
129831 sqlite3_file *(*database_file_object)(const char*);
129832 /* Version 3.34.0 and later */
129833 int (*txn_state)(sqlite3*,const char*);
129834 /* Version 3.36.1 and later */
129835 sqlite3_int64 (*changes64)(sqlite3*);
129836 sqlite3_int64 (*total_changes64)(sqlite3*);
129837 /* Version 3.37.0 and later */
129838 int (*autovacuum_pages)(sqlite3*,
129839 unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int),
129840 void*, void(*)(void*));
129841 /* Version 3.38.0 and later */
129842 int (*error_offset)(sqlite3*);
129843 int (*vtab_rhs_value)(sqlite3_index_info*,int,sqlite3_value**);
129844 int (*vtab_distinct)(sqlite3_index_info*);
129845 int (*vtab_in)(sqlite3_index_info*,int,int);
129846 int (*vtab_in_first)(sqlite3_value*,sqlite3_value**);
129847 int (*vtab_in_next)(sqlite3_value*,sqlite3_value**);
129848 /* Version 3.39.0 and later */
129849 int (*deserialize)(sqlite3*,const char*,unsigned char*,
129850 sqlite3_int64,sqlite3_int64,unsigned);
129851 unsigned char *(*serialize)(sqlite3*,const char *,sqlite3_int64*,
129852 unsigned int);
129853 const char *(*db_name)(sqlite3*,int);
129854};
129855
129856/*
129857** This is the function signature used for all extension entry points. It
129858** is also defined in the file "loadext.c".
129859*/
129860typedef int (*sqlite3_loadext_entry)(
129861 sqlite3 *db, /* Handle to the database. */
129862 char **pzErrMsg, /* Used to set error string on failure. */
129863 const sqlite3_api_routines *pThunk /* Extension API function pointers. */
129864);
129865
129866/*
129867** The following macros redefine the API routines so that they are
129868** redirected through the global sqlite3_api structure.
129869**
129870** This header file is also used by the loadext.c source file
129871** (part of the main SQLite library - not an extension) so that
129872** it can get access to the sqlite3_api_routines structure
129873** definition. But the main library does not want to redefine
129874** the API. So the redefinition macros are only valid if the
129875** SQLITE_CORE macros is undefined.
129876*/
129877#if !defined(SQLITE_CORE1) && !defined(SQLITE_OMIT_LOAD_EXTENSION1)
129878#define sqlite3_aggregate_context sqlite3_api->aggregate_context
129879#ifndef SQLITE_OMIT_DEPRECATED1
129880#define sqlite3_aggregate_count sqlite3_api->aggregate_count
129881#endif
129882#define sqlite3_bind_blob sqlite3_api->bind_blob
129883#define sqlite3_bind_double sqlite3_api->bind_double
129884#define sqlite3_bind_int sqlite3_api->bind_int
129885#define sqlite3_bind_int64 sqlite3_api->bind_int64
129886#define sqlite3_bind_null sqlite3_api->bind_null
129887#define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
129888#define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
129889#define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
129890#define sqlite3_bind_text sqlite3_api->bind_text
129891#define sqlite3_bind_text16 sqlite3_api->bind_text16
129892#define sqlite3_bind_value sqlite3_api->bind_value
129893#define sqlite3_busy_handler sqlite3_api->busy_handler
129894#define sqlite3_busy_timeout sqlite3_api->busy_timeout
129895#define sqlite3_changes sqlite3_api->changes
129896#define sqlite3_close sqlite3_api->close
129897#define sqlite3_collation_needed sqlite3_api->collation_needed
129898#define sqlite3_collation_needed16 sqlite3_api->collation_needed16
129899#define sqlite3_column_blob sqlite3_api->column_blob
129900#define sqlite3_column_bytes sqlite3_api->column_bytes
129901#define sqlite3_column_bytes16 sqlite3_api->column_bytes16
129902#define sqlite3_column_count sqlite3_api->column_count
129903#define sqlite3_column_database_name sqlite3_api->column_database_name
129904#define sqlite3_column_database_name16 sqlite3_api->column_database_name16
129905#define sqlite3_column_decltype sqlite3_api->column_decltype
129906#define sqlite3_column_decltype16 sqlite3_api->column_decltype16
129907#define sqlite3_column_double sqlite3_api->column_double
129908#define sqlite3_column_int sqlite3_api->column_int
129909#define sqlite3_column_int64 sqlite3_api->column_int64
129910#define sqlite3_column_name sqlite3_api->column_name
129911#define sqlite3_column_name16 sqlite3_api->column_name16
129912#define sqlite3_column_origin_name sqlite3_api->column_origin_name
129913#define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
129914#define sqlite3_column_table_name sqlite3_api->column_table_name
129915#define sqlite3_column_table_name16 sqlite3_api->column_table_name16
129916#define sqlite3_column_text sqlite3_api->column_text
129917#define sqlite3_column_text16 sqlite3_api->column_text16
129918#define sqlite3_column_type sqlite3_api->column_type
129919#define sqlite3_column_value sqlite3_api->column_value
129920#define sqlite3_commit_hook sqlite3_api->commit_hook
129921#define sqlite3_complete sqlite3_api->complete
129922#define sqlite3_complete16 sqlite3_api->complete16
129923#define sqlite3_create_collation sqlite3_api->create_collation
129924#define sqlite3_create_collation16 sqlite3_api->create_collation16
129925#define sqlite3_create_function sqlite3_api->create_function
129926#define sqlite3_create_function16 sqlite3_api->create_function16
129927#define sqlite3_create_module sqlite3_api->create_module
129928#define sqlite3_create_module_v2 sqlite3_api->create_module_v2
129929#define sqlite3_data_count sqlite3_api->data_count
129930#define sqlite3_db_handle sqlite3_api->db_handle
129931#define sqlite3_declare_vtab sqlite3_api->declare_vtab
129932#define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
129933#define sqlite3_errcode sqlite3_api->errcode
129934#define sqlite3_errmsg sqlite3_api->errmsg
129935#define sqlite3_errmsg16 sqlite3_api->errmsg16
129936#define sqlite3_exec sqlite3_api->exec
129937#ifndef SQLITE_OMIT_DEPRECATED1
129938#define sqlite3_expired sqlite3_api->expired
129939#endif
129940#define sqlite3_finalize sqlite3_api->finalize
129941#define sqlite3_free sqlite3_api->free
129942#define sqlite3_free_table sqlite3_api->free_table
129943#define sqlite3_get_autocommit sqlite3_api->get_autocommit
129944#define sqlite3_get_auxdata sqlite3_api->get_auxdata
129945#define sqlite3_get_table sqlite3_api->get_table
129946#ifndef SQLITE_OMIT_DEPRECATED1
129947#define sqlite3_global_recover sqlite3_api->global_recover
129948#endif
129949#define sqlite3_interrupt sqlite3_api->interruptx
129950#define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
129951#define sqlite3_libversion sqlite3_api->libversion
129952#define sqlite3_libversion_number sqlite3_api->libversion_number
129953#define sqlite3_malloc sqlite3_api->malloc
129954#define sqlite3_mprintf sqlite3_api->mprintf
129955#define sqlite3_open sqlite3_api->open
129956#define sqlite3_open16 sqlite3_api->open16
129957#define sqlite3_prepare sqlite3_api->prepare
129958#define sqlite3_prepare16 sqlite3_api->prepare16
129959#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
129960#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
129961#define sqlite3_profile sqlite3_api->profile
129962#define sqlite3_progress_handler sqlite3_api->progress_handler
129963#define sqlite3_realloc sqlite3_api->realloc
129964#define sqlite3_reset sqlite3_api->reset
129965#define sqlite3_result_blob sqlite3_api->result_blob
129966#define sqlite3_result_double sqlite3_api->result_double
129967#define sqlite3_result_error sqlite3_api->result_error
129968#define sqlite3_result_error16 sqlite3_api->result_error16
129969#define sqlite3_result_int sqlite3_api->result_int
129970#define sqlite3_result_int64 sqlite3_api->result_int64
129971#define sqlite3_result_null sqlite3_api->result_null
129972#define sqlite3_result_text sqlite3_api->result_text
129973#define sqlite3_result_text16 sqlite3_api->result_text16
129974#define sqlite3_result_text16be sqlite3_api->result_text16be
129975#define sqlite3_result_text16le sqlite3_api->result_text16le
129976#define sqlite3_result_value sqlite3_api->result_value
129977#define sqlite3_rollback_hook sqlite3_api->rollback_hook
129978#define sqlite3_set_authorizer sqlite3_api->set_authorizer
129979#define sqlite3_set_auxdata sqlite3_api->set_auxdata
129980#define sqlite3_snprintf sqlite3_api->xsnprintf
129981#define sqlite3_step sqlite3_api->step
129982#define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
129983#define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
129984#define sqlite3_total_changes sqlite3_api->total_changes
129985#define sqlite3_trace sqlite3_api->trace
129986#ifndef SQLITE_OMIT_DEPRECATED1
129987#define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
129988#endif
129989#define sqlite3_update_hook sqlite3_api->update_hook
129990#define sqlite3_user_data sqlite3_api->user_data
129991#define sqlite3_value_blob sqlite3_api->value_blob
129992#define sqlite3_value_bytes sqlite3_api->value_bytes
129993#define sqlite3_value_bytes16 sqlite3_api->value_bytes16
129994#define sqlite3_value_double sqlite3_api->value_double
129995#define sqlite3_value_int sqlite3_api->value_int
129996#define sqlite3_value_int64 sqlite3_api->value_int64
129997#define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
129998#define sqlite3_value_text sqlite3_api->value_text
129999#define sqlite3_value_text16 sqlite3_api->value_text16
130000#define sqlite3_value_text16be sqlite3_api->value_text16be
130001#define sqlite3_value_text16le sqlite3_api->value_text16le
130002#define sqlite3_value_type sqlite3_api->value_type
130003#define sqlite3_vmprintf sqlite3_api->vmprintf
130004#define sqlite3_vsnprintf sqlite3_api->xvsnprintf
130005#define sqlite3_overload_function sqlite3_api->overload_function
130006#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
130007#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
130008#define sqlite3_clear_bindings sqlite3_api->clear_bindings
130009#define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
130010#define sqlite3_blob_bytes sqlite3_api->blob_bytes
130011#define sqlite3_blob_close sqlite3_api->blob_close
130012#define sqlite3_blob_open sqlite3_api->blob_open
130013#define sqlite3_blob_read sqlite3_api->blob_read
130014#define sqlite3_blob_write sqlite3_api->blob_write
130015#define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
130016#define sqlite3_file_control sqlite3_api->file_control
130017#define sqlite3_memory_highwater sqlite3_api->memory_highwater
130018#define sqlite3_memory_used sqlite3_api->memory_used
130019#define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
130020#define sqlite3_mutex_enter sqlite3_api->mutex_enter
130021#define sqlite3_mutex_free sqlite3_api->mutex_free
130022#define sqlite3_mutex_leave sqlite3_api->mutex_leave
130023#define sqlite3_mutex_try sqlite3_api->mutex_try
130024#define sqlite3_open_v2 sqlite3_api->open_v2
130025#define sqlite3_release_memory sqlite3_api->release_memory
130026#define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
130027#define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
130028#define sqlite3_sleep sqlite3_api->sleep
130029#define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
130030#define sqlite3_vfs_find sqlite3_api->vfs_find
130031#define sqlite3_vfs_register sqlite3_api->vfs_register
130032#define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
130033#define sqlite3_threadsafe sqlite3_api->xthreadsafe
130034#define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
130035#define sqlite3_result_error_code sqlite3_api->result_error_code
130036#define sqlite3_test_control sqlite3_api->test_control
130037#define sqlite3_randomness sqlite3_api->randomness
130038#define sqlite3_context_db_handle sqlite3_api->context_db_handle
130039#define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
130040#define sqlite3_limit sqlite3_api->limit
130041#define sqlite3_next_stmt sqlite3_api->next_stmt
130042#define sqlite3_sql sqlite3_api->sql
130043#define sqlite3_status sqlite3_api->status
130044#define sqlite3_backup_finish sqlite3_api->backup_finish
130045#define sqlite3_backup_init sqlite3_api->backup_init
130046#define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
130047#define sqlite3_backup_remaining sqlite3_api->backup_remaining
130048#define sqlite3_backup_step sqlite3_api->backup_step
130049#define sqlite3_compileoption_get sqlite3_api->compileoption_get
130050#define sqlite3_compileoption_used sqlite3_api->compileoption_used
130051#define sqlite3_create_function_v2 sqlite3_api->create_function_v2
130052#define sqlite3_db_config sqlite3_api->db_config
130053#define sqlite3_db_mutex sqlite3_api->db_mutex
130054#define sqlite3_db_status sqlite3_api->db_status
130055#define sqlite3_extended_errcode sqlite3_api->extended_errcode
130056#define sqlite3_log sqlite3_api->log
130057#define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
130058#define sqlite3_sourceid sqlite3_api->sourceid
130059#define sqlite3_stmt_status sqlite3_api->stmt_status
130060#define sqlite3_strnicmp sqlite3_api->strnicmp
130061#define sqlite3_unlock_notify sqlite3_api->unlock_notify
130062#define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
130063#define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
130064#define sqlite3_wal_hook sqlite3_api->wal_hook
130065#define sqlite3_blob_reopen sqlite3_api->blob_reopen
130066#define sqlite3_vtab_config sqlite3_api->vtab_config
130067#define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
130068/* Version 3.7.16 and later */
130069#define sqlite3_close_v2 sqlite3_api->close_v2
130070#define sqlite3_db_filename sqlite3_api->db_filename
130071#define sqlite3_db_readonly sqlite3_api->db_readonly
130072#define sqlite3_db_release_memory sqlite3_api->db_release_memory
130073#define sqlite3_errstr sqlite3_api->errstr
130074#define sqlite3_stmt_busy sqlite3_api->stmt_busy
130075#define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
130076#define sqlite3_stricmp sqlite3_api->stricmp
130077#define sqlite3_uri_boolean sqlite3_api->uri_boolean
130078#define sqlite3_uri_int64 sqlite3_api->uri_int64
130079#define sqlite3_uri_parameter sqlite3_api->uri_parameter
130080#define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf
130081#define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
130082/* Version 3.8.7 and later */
130083#define sqlite3_auto_extension sqlite3_api->auto_extension
130084#define sqlite3_bind_blob64 sqlite3_api->bind_blob64
130085#define sqlite3_bind_text64 sqlite3_api->bind_text64
130086#define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
130087#define sqlite3_load_extension sqlite3_api->load_extension
130088#define sqlite3_malloc64 sqlite3_api->malloc64
130089#define sqlite3_msize sqlite3_api->msize
130090#define sqlite3_realloc64 sqlite3_api->realloc64
130091#define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
130092#define sqlite3_result_blob64 sqlite3_api->result_blob64
130093#define sqlite3_result_text64 sqlite3_api->result_text64
130094#define sqlite3_strglob sqlite3_api->strglob
130095/* Version 3.8.11 and later */
130096#define sqlite3_value_dup sqlite3_api->value_dup
130097#define sqlite3_value_free sqlite3_api->value_free
130098#define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
130099#define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
130100/* Version 3.9.0 and later */
130101#define sqlite3_value_subtype sqlite3_api->value_subtype
130102#define sqlite3_result_subtype sqlite3_api->result_subtype
130103/* Version 3.10.0 and later */
130104#define sqlite3_status64 sqlite3_api->status64
130105#define sqlite3_strlike sqlite3_api->strlike
130106#define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
130107/* Version 3.12.0 and later */
130108#define sqlite3_system_errno sqlite3_api->system_errno
130109/* Version 3.14.0 and later */
130110#define sqlite3_trace_v2 sqlite3_api->trace_v2
130111#define sqlite3_expanded_sql sqlite3_api->expanded_sql
130112/* Version 3.18.0 and later */
130113#define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid
130114/* Version 3.20.0 and later */
130115#define sqlite3_prepare_v3 sqlite3_api->prepare_v3
130116#define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3
130117#define sqlite3_bind_pointer sqlite3_api->bind_pointer
130118#define sqlite3_result_pointer sqlite3_api->result_pointer
130119#define sqlite3_value_pointer sqlite3_api->value_pointer
130120/* Version 3.22.0 and later */
130121#define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
130122#define sqlite3_value_nochange sqlite3_api->value_nochange
130123#define sqlite3_vtab_collation sqlite3_api->vtab_collation
130124/* Version 3.24.0 and later */
130125#define sqlite3_keyword_count sqlite3_api->keyword_count
130126#define sqlite3_keyword_name sqlite3_api->keyword_name
130127#define sqlite3_keyword_check sqlite3_api->keyword_check
130128#define sqlite3_str_new sqlite3_api->str_new
130129#define sqlite3_str_finish sqlite3_api->str_finish
130130#define sqlite3_str_appendf sqlite3_api->str_appendf
130131#define sqlite3_str_vappendf sqlite3_api->str_vappendf
130132#define sqlite3_str_append sqlite3_api->str_append
130133#define sqlite3_str_appendall sqlite3_api->str_appendall
130134#define sqlite3_str_appendchar sqlite3_api->str_appendchar
130135#define sqlite3_str_reset sqlite3_api->str_reset
130136#define sqlite3_str_errcode sqlite3_api->str_errcode
130137#define sqlite3_str_length sqlite3_api->str_length
130138#define sqlite3_str_value sqlite3_api->str_value
130139/* Version 3.25.0 and later */
130140#define sqlite3_create_window_function sqlite3_api->create_window_function
130141/* Version 3.26.0 and later */
130142#define sqlite3_normalized_sql sqlite3_api->normalized_sql
130143/* Version 3.28.0 and later */
130144#define sqlite3_stmt_isexplain sqlite3_api->stmt_isexplain
130145#define sqlite3_value_frombind sqlite3_api->value_frombind
130146/* Version 3.30.0 and later */
130147#define sqlite3_drop_modules sqlite3_api->drop_modules
130148/* Version 3.31.0 and later */
130149#define sqlite3_hard_heap_limit64 sqlite3_api->hard_heap_limit64
130150#define sqlite3_uri_key sqlite3_api->uri_key
130151#define sqlite3_filename_database sqlite3_api->filename_database
130152#define sqlite3_filename_journal sqlite3_api->filename_journal
130153#define sqlite3_filename_wal sqlite3_api->filename_wal
130154/* Version 3.32.0 and later */
130155#define sqlite3_create_filename sqlite3_api->create_filename
130156#define sqlite3_free_filename sqlite3_api->free_filename
130157#define sqlite3_database_file_object sqlite3_api->database_file_object
130158/* Version 3.34.0 and later */
130159#define sqlite3_txn_state sqlite3_api->txn_state
130160/* Version 3.36.1 and later */
130161#define sqlite3_changes64 sqlite3_api->changes64
130162#define sqlite3_total_changes64 sqlite3_api->total_changes64
130163/* Version 3.37.0 and later */
130164#define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages
130165/* Version 3.38.0 and later */
130166#define sqlite3_error_offset sqlite3_api->error_offset
130167#define sqlite3_vtab_rhs_value sqlite3_api->vtab_rhs_value
130168#define sqlite3_vtab_distinct sqlite3_api->vtab_distinct
130169#define sqlite3_vtab_in sqlite3_api->vtab_in
130170#define sqlite3_vtab_in_first sqlite3_api->vtab_in_first
130171#define sqlite3_vtab_in_next sqlite3_api->vtab_in_next
130172/* Version 3.39.0 and later */
130173#ifndef SQLITE_OMIT_DESERIALIZE
130174#define sqlite3_deserialize sqlite3_api->deserialize
130175#define sqlite3_serialize sqlite3_api->serialize
130176#endif
130177#define sqlite3_db_name sqlite3_api->db_name
130178#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
130179
130180#if !defined(SQLITE_CORE1) && !defined(SQLITE_OMIT_LOAD_EXTENSION1)
130181 /* This case when the file really is being compiled as a loadable
130182 ** extension */
130183# define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api=0;
130184# define SQLITE_EXTENSION_INIT2(v)(void)v; sqlite3_api=v;
130185# define SQLITE_EXTENSION_INIT3 \
130186 extern const sqlite3_api_routines *sqlite3_api;
130187#else
130188 /* This case when the file is being statically linked into the
130189 ** application */
130190# define SQLITE_EXTENSION_INIT1 /*no-op*/
130191# define SQLITE_EXTENSION_INIT2(v)(void)v; (void)v; /* unused parameter */
130192# define SQLITE_EXTENSION_INIT3 /*no-op*/
130193#endif
130194
130195#endif /* SQLITE3EXT_H */
130196
130197/************** End of sqlite3ext.h ******************************************/
130198/************** Continuing where we left off in loadext.c ********************/
130199/* #include "sqliteInt.h" */
130200
130201#ifndef SQLITE_OMIT_LOAD_EXTENSION1
130202/*
130203** Some API routines are omitted when various features are
130204** excluded from a build of SQLite. Substitute a NULL pointer
130205** for any missing APIs.
130206*/
130207#ifndef SQLITE_ENABLE_COLUMN_METADATA1
130208# define sqlite3_column_database_name 0
130209# define sqlite3_column_database_name16 0
130210# define sqlite3_column_table_name 0
130211# define sqlite3_column_table_name16 0
130212# define sqlite3_column_origin_name 0
130213# define sqlite3_column_origin_name16 0
130214#endif
130215
130216#ifdef SQLITE_OMIT_AUTHORIZATION
130217# define sqlite3_set_authorizer 0
130218#endif
130219
130220#ifdef SQLITE_OMIT_UTF16
130221# define sqlite3_bind_text16 0
130222# define sqlite3_collation_needed16 0
130223# define sqlite3_column_decltype16 0
130224# define sqlite3_column_name16 0
130225# define sqlite3_column_text16 0
130226# define sqlite3_complete16 0
130227# define sqlite3_create_collation16 0
130228# define sqlite3_create_function16 0
130229# define sqlite3_errmsg16 0
130230# define sqlite3_open16 0
130231# define sqlite3_prepare16 0
130232# define sqlite3_prepare16_v2 0
130233# define sqlite3_prepare16_v3 0
130234# define sqlite3_result_error16 0
130235# define sqlite3_result_text16 0
130236# define sqlite3_result_text16be 0
130237# define sqlite3_result_text16le 0
130238# define sqlite3_value_text16 0
130239# define sqlite3_value_text16be 0
130240# define sqlite3_value_text16le 0
130241# define sqlite3_column_database_name16 0
130242# define sqlite3_column_table_name16 0
130243# define sqlite3_column_origin_name16 0
130244#endif
130245
130246#ifdef SQLITE_OMIT_COMPLETE
130247# define sqlite3_complete 0
130248# define sqlite3_complete16 0
130249#endif
130250
130251#ifdef SQLITE_OMIT_DECLTYPE
130252# define sqlite3_column_decltype16 0
130253# define sqlite3_column_decltype 0
130254#endif
130255
130256#ifdef SQLITE_OMIT_PROGRESS_CALLBACK1
130257# define sqlite3_progress_handler 0
130258#endif
130259
130260#ifdef SQLITE_OMIT_VIRTUALTABLE
130261# define sqlite3_create_module 0
130262# define sqlite3_create_module_v2 0
130263# define sqlite3_declare_vtab 0
130264# define sqlite3_vtab_config 0
130265# define sqlite3_vtab_on_conflict 0
130266# define sqlite3_vtab_collation 0
130267#endif
130268
130269#ifdef SQLITE_OMIT_SHARED_CACHE
130270# define sqlite3_enable_shared_cache 0
130271#endif
130272
130273#if defined(SQLITE_OMIT_TRACE) || defined(SQLITE_OMIT_DEPRECATED1)
130274# define sqlite3_profile 0
130275# define sqlite3_trace 0
130276#endif
130277
130278#ifdef SQLITE_OMIT_GET_TABLE
130279# define sqlite3_free_table 0
130280# define sqlite3_get_table 0
130281#endif
130282
130283#ifdef SQLITE_OMIT_INCRBLOB
130284#define sqlite3_bind_zeroblob 0
130285#define sqlite3_blob_bytes 0
130286#define sqlite3_blob_close 0
130287#define sqlite3_blob_open 0
130288#define sqlite3_blob_read 0
130289#define sqlite3_blob_write 0
130290#define sqlite3_blob_reopen 0
130291#endif
130292
130293#if defined(SQLITE_OMIT_TRACE)
130294# define sqlite3_trace_v2 0
130295#endif
130296
130297/*
130298** The following structure contains pointers to all SQLite API routines.
130299** A pointer to this structure is passed into extensions when they are
130300** loaded so that the extension can make calls back into the SQLite
130301** library.
130302**
130303** When adding new APIs, add them to the bottom of this structure
130304** in order to preserve backwards compatibility.
130305**
130306** Extensions that use newer APIs should first call the
130307** sqlite3_libversion_number() to make sure that the API they
130308** intend to use is supported by the library. Extensions should
130309** also check to make sure that the pointer to the function is
130310** not NULL before calling it.
130311*/
130312static const sqlite3_api_routines sqlite3Apis = {
130313 sqlite3_aggregate_context,
130314#ifndef SQLITE_OMIT_DEPRECATED1
130315 sqlite3_aggregate_count,
130316#else
130317 0,
130318#endif
130319 sqlite3_bind_blob,
130320 sqlite3_bind_double,
130321 sqlite3_bind_int,
130322 sqlite3_bind_int64,
130323 sqlite3_bind_null,
130324 sqlite3_bind_parameter_count,
130325 sqlite3_bind_parameter_index,
130326 sqlite3_bind_parameter_name,
130327 sqlite3_bind_text,
130328 sqlite3_bind_text16,
130329 sqlite3_bind_value,
130330 sqlite3_busy_handler,
130331 sqlite3_busy_timeout,
130332 sqlite3_changes,
130333 sqlite3_close,
130334 sqlite3_collation_needed,
130335 sqlite3_collation_needed16,
130336 sqlite3_column_blob,
130337 sqlite3_column_bytes,
130338 sqlite3_column_bytes16,
130339 sqlite3_column_count,
130340 sqlite3_column_database_name,
130341 sqlite3_column_database_name16,
130342 sqlite3_column_decltype,
130343 sqlite3_column_decltype16,
130344 sqlite3_column_double,
130345 sqlite3_column_int,
130346 sqlite3_column_int64,
130347 sqlite3_column_name,
130348 sqlite3_column_name16,
130349 sqlite3_column_origin_name,
130350 sqlite3_column_origin_name16,
130351 sqlite3_column_table_name,
130352 sqlite3_column_table_name16,
130353 sqlite3_column_text,
130354 sqlite3_column_text16,
130355 sqlite3_column_type,
130356 sqlite3_column_value,
130357 sqlite3_commit_hook,
130358 sqlite3_complete,
130359 sqlite3_complete16,
130360 sqlite3_create_collation,
130361 sqlite3_create_collation16,
130362 sqlite3_create_function,
130363 sqlite3_create_function16,
130364 sqlite3_create_module,
130365 sqlite3_data_count,
130366 sqlite3_db_handle,
130367 sqlite3_declare_vtab,
130368 sqlite3_enable_shared_cache,
130369 sqlite3_errcode,
130370 sqlite3_errmsg,
130371 sqlite3_errmsg16,
130372 sqlite3_exec,
130373#ifndef SQLITE_OMIT_DEPRECATED1
130374 sqlite3_expired,
130375#else
130376 0,
130377#endif
130378 sqlite3_finalize,
130379 sqlite3_free,
130380 sqlite3_free_table,
130381 sqlite3_get_autocommit,
130382 sqlite3_get_auxdata,
130383 sqlite3_get_table,
130384 0, /* Was sqlite3_global_recover(), but that function is deprecated */
130385 sqlite3_interrupt,
130386 sqlite3_last_insert_rowid,
130387 sqlite3_libversion,
130388 sqlite3_libversion_number,
130389 sqlite3_malloc,
130390 sqlite3_mprintf,
130391 sqlite3_open,
130392 sqlite3_open16,
130393 sqlite3_prepare,
130394 sqlite3_prepare16,
130395 sqlite3_profile,
130396 sqlite3_progress_handler,
130397 sqlite3_realloc,
130398 sqlite3_reset,
130399 sqlite3_result_blob,
130400 sqlite3_result_double,
130401 sqlite3_result_error,
130402 sqlite3_result_error16,
130403 sqlite3_result_int,
130404 sqlite3_result_int64,
130405 sqlite3_result_null,
130406 sqlite3_result_text,
130407 sqlite3_result_text16,
130408 sqlite3_result_text16be,
130409 sqlite3_result_text16le,
130410 sqlite3_result_value,
130411 sqlite3_rollback_hook,
130412 sqlite3_set_authorizer,
130413 sqlite3_set_auxdata,
130414 sqlite3_snprintf,
130415 sqlite3_step,
130416 sqlite3_table_column_metadata,
130417#ifndef SQLITE_OMIT_DEPRECATED1
130418 sqlite3_thread_cleanup,
130419#else
130420 0,
130421#endif
130422 sqlite3_total_changes,
130423 sqlite3_trace,
130424#ifndef SQLITE_OMIT_DEPRECATED1
130425 sqlite3_transfer_bindings,
130426#else
130427 0,
130428#endif
130429 sqlite3_update_hook,
130430 sqlite3_user_data,
130431 sqlite3_value_blob,
130432 sqlite3_value_bytes,
130433 sqlite3_value_bytes16,
130434 sqlite3_value_double,
130435 sqlite3_value_int,
130436 sqlite3_value_int64,
130437 sqlite3_value_numeric_type,
130438 sqlite3_value_text,
130439 sqlite3_value_text16,
130440 sqlite3_value_text16be,
130441 sqlite3_value_text16le,
130442 sqlite3_value_type,
130443 sqlite3_vmprintf,
130444 /*
130445 ** The original API set ends here. All extensions can call any
130446 ** of the APIs above provided that the pointer is not NULL. But
130447 ** before calling APIs that follow, extension should check the
130448 ** sqlite3_libversion_number() to make sure they are dealing with
130449 ** a library that is new enough to support that API.
130450 *************************************************************************
130451 */
130452 sqlite3_overload_function,
130453
130454 /*
130455 ** Added after 3.3.13
130456 */
130457 sqlite3_prepare_v2,
130458 sqlite3_prepare16_v2,
130459 sqlite3_clear_bindings,
130460
130461 /*
130462 ** Added for 3.4.1
130463 */
130464 sqlite3_create_module_v2,
130465
130466 /*
130467 ** Added for 3.5.0
130468 */
130469 sqlite3_bind_zeroblob,
130470 sqlite3_blob_bytes,
130471 sqlite3_blob_close,
130472 sqlite3_blob_open,
130473 sqlite3_blob_read,
130474 sqlite3_blob_write,
130475 sqlite3_create_collation_v2,
130476 sqlite3_file_control,
130477 sqlite3_memory_highwater,
130478 sqlite3_memory_used,
130479#ifdef SQLITE_MUTEX_OMIT
130480 0,
130481 0,
130482 0,
130483 0,
130484 0,
130485#else
130486 sqlite3_mutex_alloc,
130487 sqlite3_mutex_enter,
130488 sqlite3_mutex_free,
130489 sqlite3_mutex_leave,
130490 sqlite3_mutex_try,
130491#endif
130492 sqlite3_open_v2,
130493 sqlite3_release_memory,
130494 sqlite3_result_error_nomem,
130495 sqlite3_result_error_toobig,
130496 sqlite3_sleep,
130497 sqlite3_soft_heap_limit,
130498 sqlite3_vfs_find,
130499 sqlite3_vfs_register,
130500 sqlite3_vfs_unregister,
130501
130502 /*
130503 ** Added for 3.5.8
130504 */
130505 sqlite3_threadsafe,
130506 sqlite3_result_zeroblob,
130507 sqlite3_result_error_code,
130508 sqlite3_test_control,
130509 sqlite3_randomness,
130510 sqlite3_context_db_handle,
130511
130512 /*
130513 ** Added for 3.6.0
130514 */
130515 sqlite3_extended_result_codes,
130516 sqlite3_limit,
130517 sqlite3_next_stmt,
130518 sqlite3_sql,
130519 sqlite3_status,
130520
130521 /*
130522 ** Added for 3.7.4
130523 */
130524 sqlite3_backup_finish,
130525 sqlite3_backup_init,
130526 sqlite3_backup_pagecount,
130527 sqlite3_backup_remaining,
130528 sqlite3_backup_step,
130529#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
130530 sqlite3_compileoption_get,
130531 sqlite3_compileoption_used,
130532#else
130533 0,
130534 0,
130535#endif
130536 sqlite3_create_function_v2,
130537 sqlite3_db_config,
130538 sqlite3_db_mutex,
130539 sqlite3_db_status,
130540 sqlite3_extended_errcode,
130541 sqlite3_log,
130542 sqlite3_soft_heap_limit64,
130543 sqlite3_sourceid,
130544 sqlite3_stmt_status,
130545 sqlite3_strnicmp,
130546#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
130547 sqlite3_unlock_notify,
130548#else
130549 0,
130550#endif
130551#ifndef SQLITE_OMIT_WAL
130552 sqlite3_wal_autocheckpoint,
130553 sqlite3_wal_checkpoint,
130554 sqlite3_wal_hook,
130555#else
130556 0,
130557 0,
130558 0,
130559#endif
130560 sqlite3_blob_reopen,
130561 sqlite3_vtab_config,
130562 sqlite3_vtab_on_conflict,
130563 sqlite3_close_v2,
130564 sqlite3_db_filename,
130565 sqlite3_db_readonly,
130566 sqlite3_db_release_memory,
130567 sqlite3_errstr,
130568 sqlite3_stmt_busy,
130569 sqlite3_stmt_readonly,
130570 sqlite3_stricmp,
130571 sqlite3_uri_boolean,
130572 sqlite3_uri_int64,
130573 sqlite3_uri_parameter,
130574 sqlite3_vsnprintf,
130575 sqlite3_wal_checkpoint_v2,
130576 /* Version 3.8.7 and later */
130577 sqlite3_auto_extension,
130578 sqlite3_bind_blob64,
130579 sqlite3_bind_text64,
130580 sqlite3_cancel_auto_extension,
130581 sqlite3_load_extension,
130582 sqlite3_malloc64,
130583 sqlite3_msize,
130584 sqlite3_realloc64,
130585 sqlite3_reset_auto_extension,
130586 sqlite3_result_blob64,
130587 sqlite3_result_text64,
130588 sqlite3_strglob,
130589 /* Version 3.8.11 and later */
130590 (sqlite3_value*(*)(const sqlite3_value*))sqlite3_value_dup,
130591 sqlite3_value_free,
130592 sqlite3_result_zeroblob64,
130593 sqlite3_bind_zeroblob64,
130594 /* Version 3.9.0 and later */
130595 sqlite3_value_subtype,
130596 sqlite3_result_subtype,
130597 /* Version 3.10.0 and later */
130598 sqlite3_status64,
130599 sqlite3_strlike,
130600 sqlite3_db_cacheflush,
130601 /* Version 3.12.0 and later */
130602 sqlite3_system_errno,
130603 /* Version 3.14.0 and later */
130604 sqlite3_trace_v2,
130605 sqlite3_expanded_sql,
130606 /* Version 3.18.0 and later */
130607 sqlite3_set_last_insert_rowid,
130608 /* Version 3.20.0 and later */
130609 sqlite3_prepare_v3,
130610 sqlite3_prepare16_v3,
130611 sqlite3_bind_pointer,
130612 sqlite3_result_pointer,
130613 sqlite3_value_pointer,
130614 /* Version 3.22.0 and later */
130615 sqlite3_vtab_nochange,
130616 sqlite3_value_nochange,
130617 sqlite3_vtab_collation,
130618 /* Version 3.24.0 and later */
130619 sqlite3_keyword_count,
130620 sqlite3_keyword_name,
130621 sqlite3_keyword_check,
130622 sqlite3_str_new,
130623 sqlite3_str_finish,
130624 sqlite3_str_appendf,
130625 sqlite3_str_vappendf,
130626 sqlite3_str_append,
130627 sqlite3_str_appendall,
130628 sqlite3_str_appendchar,
130629 sqlite3_str_reset,
130630 sqlite3_str_errcode,
130631 sqlite3_str_length,
130632 sqlite3_str_value,
130633 /* Version 3.25.0 and later */
130634 sqlite3_create_window_function,
130635 /* Version 3.26.0 and later */
130636#ifdef SQLITE_ENABLE_NORMALIZE
130637 sqlite3_normalized_sql,
130638#else
130639 0,
130640#endif
130641 /* Version 3.28.0 and later */
130642 sqlite3_stmt_isexplain,
130643 sqlite3_value_frombind,
130644 /* Version 3.30.0 and later */
130645#ifndef SQLITE_OMIT_VIRTUALTABLE
130646 sqlite3_drop_modules,
130647#else
130648 0,
130649#endif
130650 /* Version 3.31.0 and later */
130651 sqlite3_hard_heap_limit64,
130652 sqlite3_uri_key,
130653 sqlite3_filename_database,
130654 sqlite3_filename_journal,
130655 sqlite3_filename_wal,
130656 /* Version 3.32.0 and later */
130657 sqlite3_create_filename,
130658 sqlite3_free_filename,
130659 sqlite3_database_file_object,
130660 /* Version 3.34.0 and later */
130661 sqlite3_txn_state,
130662 /* Version 3.36.1 and later */
130663 sqlite3_changes64,
130664 sqlite3_total_changes64,
130665 /* Version 3.37.0 and later */
130666 sqlite3_autovacuum_pages,
130667 /* Version 3.38.0 and later */
130668 sqlite3_error_offset,
130669#ifndef SQLITE_OMIT_VIRTUALTABLE
130670 sqlite3_vtab_rhs_value,
130671 sqlite3_vtab_distinct,
130672 sqlite3_vtab_in,
130673 sqlite3_vtab_in_first,
130674 sqlite3_vtab_in_next,
130675#else
130676 0,
130677 0,
130678 0,
130679 0,
130680 0,
130681#endif
130682 /* Version 3.39.0 and later */
130683#ifndef SQLITE_OMIT_DESERIALIZE
130684 sqlite3_deserialize,
130685 sqlite3_serialize,
130686#else
130687 0,
130688 0,
130689#endif
130690 sqlite3_db_name
130691};
130692
130693/* True if x is the directory separator character
130694*/
130695#if SQLITE_OS_WIN0
130696# define DirSep(X) ((X)=='/'||(X)=='\\')
130697#else
130698# define DirSep(X) ((X)=='/')
130699#endif
130700
130701/*
130702** Attempt to load an SQLite extension library contained in the file
130703** zFile. The entry point is zProc. zProc may be 0 in which case a
130704** default entry point name (sqlite3_extension_init) is used. Use
130705** of the default name is recommended.
130706**
130707** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
130708**
130709** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
130710** error message text. The calling function should free this memory
130711** by calling sqlite3DbFree(db, ).
130712*/
130713static int sqlite3LoadExtension(
130714 sqlite3 *db, /* Load the extension into this database connection */
130715 const char *zFile, /* Name of the shared library containing extension */
130716 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
130717 char **pzErrMsg /* Put error message here if not 0 */
130718){
130719 sqlite3_vfs *pVfs = db->pVfs;
130720 void *handle;
130721 sqlite3_loadext_entry xInit;
130722 char *zErrmsg = 0;
130723 const char *zEntry;
130724 char *zAltEntry = 0;
130725 void **aHandle;
130726 u64 nMsg = strlen(zFile);
130727 int ii;
130728 int rc;
130729
130730 /* Shared library endings to try if zFile cannot be loaded as written */
130731 static const char *azEndings[] = {
130732#if SQLITE_OS_WIN0
130733 "dll"
130734#elif defined(__APPLE__)
130735 "dylib"
130736#else
130737 "so"
130738#endif
130739 };
130740
130741
130742 if( pzErrMsg ) *pzErrMsg = 0;
130743
130744 /* Ticket #1863. To avoid a creating security problems for older
130745 ** applications that relink against newer versions of SQLite, the
130746 ** ability to run load_extension is turned off by default. One
130747 ** must call either sqlite3_enable_load_extension(db) or
130748 ** sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, 0)
130749 ** to turn on extension loading.
130750 */
130751 if( (db->flags & SQLITE_LoadExtension0x00010000)==0 ){
130752 if( pzErrMsg ){
130753 *pzErrMsg = sqlite3_mprintf("not authorized");
130754 }
130755 return SQLITE_ERROR1;
130756 }
130757
130758 zEntry = zProc ? zProc : "sqlite3_extension_init";
130759
130760 /* tag-20210611-1. Some dlopen() implementations will segfault if given
130761 ** an oversize filename. Most filesystems have a pathname limit of 4K,
130762 ** so limit the extension filename length to about twice that.
130763 ** https://sqlite.org/forum/forumpost/08a0d6d9bf */
130764 if( nMsg>SQLITE_MAX_PATHLEN4096 ) goto extension_not_found;
130765
130766 handle = sqlite3OsDlOpen(pVfs, zFile);
130767#if SQLITE_OS_UNIX1 || SQLITE_OS_WIN0
130768 for(ii=0; ii<ArraySize(azEndings)((int)(sizeof(azEndings)/sizeof(azEndings[0]))) && handle==0; ii++){
130769 char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]);
130770 if( zAltFile==0 ) return SQLITE_NOMEM_BKPT7;
130771 handle = sqlite3OsDlOpen(pVfs, zAltFile);
130772 sqlite3_free(zAltFile);
130773 }
130774#endif
130775 if( handle==0 ) goto extension_not_found;
130776 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
130777
130778 /* If no entry point was specified and the default legacy
130779 ** entry point name "sqlite3_extension_init" was not found, then
130780 ** construct an entry point name "sqlite3_X_init" where the X is
130781 ** replaced by the lowercase value of every ASCII alphabetic
130782 ** character in the filename after the last "/" upto the first ".",
130783 ** and eliding the first three characters if they are "lib".
130784 ** Examples:
130785 **
130786 ** /usr/local/lib/libExample5.4.3.so ==> sqlite3_example_init
130787 ** C:/lib/mathfuncs.dll ==> sqlite3_mathfuncs_init
130788 */
130789 if( xInit==0 && zProc==0 ){
130790 int iFile, iEntry, c;
130791 int ncFile = sqlite3Strlen30(zFile);
130792 zAltEntry = sqlite3_malloc64(ncFile+30);
130793 if( zAltEntry==0 ){
130794 sqlite3OsDlClose(pVfs, handle);
130795 return SQLITE_NOMEM_BKPT7;
130796 }
130797 memcpy(zAltEntry, "sqlite3_", 8);
130798 for(iFile=ncFile-1; iFile>=0 && !DirSep(zFile[iFile]); iFile--){}
130799 iFile++;
130800 if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3;
130801 for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){
130802 if( sqlite3Isalpha(c)(sqlite3CtypeMap[(unsigned char)(c)]&0x02) ){
130803 zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c];
130804 }
130805 }
130806 memcpy(zAltEntry+iEntry, "_init", 6);
130807 zEntry = zAltEntry;
130808 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
130809 }
130810 if( xInit==0 ){
130811 if( pzErrMsg ){
130812 nMsg += strlen(zEntry) + 300;
130813 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
130814 if( zErrmsg ){
130815 assert( nMsg<0x7fffffff )((void) (0)); /* zErrmsg would be NULL if not so */
130816 sqlite3_snprintf((int)nMsg, zErrmsg,
130817 "no entry point [%s] in shared library [%s]", zEntry, zFile);
130818 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
130819 }
130820 }
130821 sqlite3OsDlClose(pVfs, handle);
130822 sqlite3_free(zAltEntry);
130823 return SQLITE_ERROR1;
130824 }
130825 sqlite3_free(zAltEntry);
130826 rc = xInit(db, &zErrmsg, &sqlite3Apis);
130827 if( rc ){
130828 if( rc==SQLITE_OK_LOAD_PERMANENTLY(0 | (1<<8)) ) return SQLITE_OK0;
130829 if( pzErrMsg ){
130830 *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
130831 }
130832 sqlite3_free(zErrmsg);
130833 sqlite3OsDlClose(pVfs, handle);
130834 return SQLITE_ERROR1;
130835 }
130836
130837 /* Append the new shared library handle to the db->aExtension array. */
130838 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
130839 if( aHandle==0 ){
130840 return SQLITE_NOMEM_BKPT7;
130841 }
130842 if( db->nExtension>0 ){
130843 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
130844 }
130845 sqlite3DbFree(db, db->aExtension);
130846 db->aExtension = aHandle;
130847
130848 db->aExtension[db->nExtension++] = handle;
130849 return SQLITE_OK0;
130850
130851extension_not_found:
130852 if( pzErrMsg ){
130853 nMsg += 300;
130854 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
130855 if( zErrmsg ){
130856 assert( nMsg<0x7fffffff )((void) (0)); /* zErrmsg would be NULL if not so */
130857 sqlite3_snprintf((int)nMsg, zErrmsg,
130858 "unable to open shared library [%.*s]", SQLITE_MAX_PATHLEN4096, zFile);
130859 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
130860 }
130861 }
130862 return SQLITE_ERROR1;
130863}
130864SQLITE_API int sqlite3_load_extension(
130865 sqlite3 *db, /* Load the extension into this database connection */
130866 const char *zFile, /* Name of the shared library containing extension */
130867 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
130868 char **pzErrMsg /* Put error message here if not 0 */
130869){
130870 int rc;
130871 sqlite3_mutex_enter(db->mutex);
130872 rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
130873 rc = sqlite3ApiExit(db, rc);
130874 sqlite3_mutex_leave(db->mutex);
130875 return rc;
130876}
130877
130878/*
130879** Call this routine when the database connection is closing in order
130880** to clean up loaded extensions
130881*/
130882SQLITE_PRIVATEstatic void sqlite3CloseExtensions(sqlite3 *db){
130883 int i;
130884 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
130885 for(i=0; i<db->nExtension; i++){
130886 sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
130887 }
130888 sqlite3DbFree(db, db->aExtension);
130889}
130890
130891/*
130892** Enable or disable extension loading. Extension loading is disabled by
130893** default so as not to open security holes in older applications.
130894*/
130895SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
130896 sqlite3_mutex_enter(db->mutex);
130897 if( onoff ){
130898 db->flags |= SQLITE_LoadExtension0x00010000|SQLITE_LoadExtFunc0x00020000;
130899 }else{
130900 db->flags &= ~(u64)(SQLITE_LoadExtension0x00010000|SQLITE_LoadExtFunc0x00020000);
130901 }
130902 sqlite3_mutex_leave(db->mutex);
130903 return SQLITE_OK0;
130904}
130905
130906#endif /* !defined(SQLITE_OMIT_LOAD_EXTENSION) */
130907
130908/*
130909** The following object holds the list of automatically loaded
130910** extensions.
130911**
130912** This list is shared across threads. The SQLITE_MUTEX_STATIC_MAIN
130913** mutex must be held while accessing this list.
130914*/
130915typedef struct sqlite3AutoExtList sqlite3AutoExtList;
130916static SQLITE_WSD struct sqlite3AutoExtList {
130917 u32 nExt; /* Number of entries in aExt[] */
130918 void (**aExt)(void); /* Pointers to the extension init functions */
130919} sqlite3Autoext = { 0, 0 };
130920
130921/* The "wsdAutoext" macro will resolve to the autoextension
130922** state vector. If writable static data is unsupported on the target,
130923** we have to locate the state vector at run-time. In the more common
130924** case where writable static data is supported, wsdStat can refer directly
130925** to the "sqlite3Autoext" state vector declared above.
130926*/
130927#ifdef SQLITE_OMIT_WSD
130928# define wsdAutoextInit \
130929 sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)sqlite3Autoext
130930# define wsdAutoextsqlite3Autoext x[0]
130931#else
130932# define wsdAutoextInit
130933# define wsdAutoextsqlite3Autoext sqlite3Autoext
130934#endif
130935
130936
130937/*
130938** Register a statically linked extension that is automatically
130939** loaded by every new database connection.
130940*/
130941SQLITE_API int sqlite3_auto_extension(
130942 void (*xInit)(void)
130943){
130944 int rc = SQLITE_OK0;
130945#ifndef SQLITE_OMIT_AUTOINIT
130946 rc = sqlite3_initialize();
130947 if( rc ){
130948 return rc;
130949 }else
130950#endif
130951 {
130952 u32 i;
130953#if SQLITE_THREADSAFE2
130954 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2);
130955#endif
130956 wsdAutoextInit;
130957 sqlite3_mutex_enter(mutex);
130958 for(i=0; i<wsdAutoextsqlite3Autoext.nExt; i++){
130959 if( wsdAutoextsqlite3Autoext.aExt[i]==xInit ) break;
130960 }
130961 if( i==wsdAutoextsqlite3Autoext.nExt ){
130962 u64 nByte = (wsdAutoextsqlite3Autoext.nExt+1)*sizeof(wsdAutoextsqlite3Autoext.aExt[0]);
130963 void (**aNew)(void);
130964 aNew = sqlite3_realloc64(wsdAutoextsqlite3Autoext.aExt, nByte);
130965 if( aNew==0 ){
130966 rc = SQLITE_NOMEM_BKPT7;
130967 }else{
130968 wsdAutoextsqlite3Autoext.aExt = aNew;
130969 wsdAutoextsqlite3Autoext.aExt[wsdAutoextsqlite3Autoext.nExt] = xInit;
130970 wsdAutoextsqlite3Autoext.nExt++;
130971 }
130972 }
130973 sqlite3_mutex_leave(mutex);
130974 assert( (rc&0xff)==rc )((void) (0));
130975 return rc;
130976 }
130977}
130978
130979/*
130980** Cancel a prior call to sqlite3_auto_extension. Remove xInit from the
130981** set of routines that is invoked for each new database connection, if it
130982** is currently on the list. If xInit is not on the list, then this
130983** routine is a no-op.
130984**
130985** Return 1 if xInit was found on the list and removed. Return 0 if xInit
130986** was not on the list.
130987*/
130988SQLITE_API int sqlite3_cancel_auto_extension(
130989 void (*xInit)(void)
130990){
130991#if SQLITE_THREADSAFE2
130992 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2);
130993#endif
130994 int i;
130995 int n = 0;
130996 wsdAutoextInit;
130997 sqlite3_mutex_enter(mutex);
130998 for(i=(int)wsdAutoextsqlite3Autoext.nExt-1; i>=0; i--){
130999 if( wsdAutoextsqlite3Autoext.aExt[i]==xInit ){
131000 wsdAutoextsqlite3Autoext.nExt--;
131001 wsdAutoextsqlite3Autoext.aExt[i] = wsdAutoextsqlite3Autoext.aExt[wsdAutoextsqlite3Autoext.nExt];
131002 n++;
131003 break;
131004 }
131005 }
131006 sqlite3_mutex_leave(mutex);
131007 return n;
131008}
131009
131010/*
131011** Reset the automatic extension loading mechanism.
131012*/
131013SQLITE_API void sqlite3_reset_auto_extension(void){
131014#ifndef SQLITE_OMIT_AUTOINIT
131015 if( sqlite3_initialize()==SQLITE_OK0 )
131016#endif
131017 {
131018#if SQLITE_THREADSAFE2
131019 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2);
131020#endif
131021 wsdAutoextInit;
131022 sqlite3_mutex_enter(mutex);
131023 sqlite3_free(wsdAutoextsqlite3Autoext.aExt);
131024 wsdAutoextsqlite3Autoext.aExt = 0;
131025 wsdAutoextsqlite3Autoext.nExt = 0;
131026 sqlite3_mutex_leave(mutex);
131027 }
131028}
131029
131030/*
131031** Load all automatic extensions.
131032**
131033** If anything goes wrong, set an error in the database connection.
131034*/
131035SQLITE_PRIVATEstatic void sqlite3AutoLoadExtensions(sqlite3 *db){
131036 u32 i;
131037 int go = 1;
131038 int rc;
131039 sqlite3_loadext_entry xInit;
131040
131041 wsdAutoextInit;
131042 if( wsdAutoextsqlite3Autoext.nExt==0 ){
131043 /* Common case: early out without every having to acquire a mutex */
131044 return;
131045 }
131046 for(i=0; go; i++){
131047 char *zErrmsg;
131048#if SQLITE_THREADSAFE2
131049 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2);
131050#endif
131051#ifdef SQLITE_OMIT_LOAD_EXTENSION1
131052 const sqlite3_api_routines *pThunk = 0;
131053#else
131054 const sqlite3_api_routines *pThunk = &sqlite3Apis;
131055#endif
131056 sqlite3_mutex_enter(mutex);
131057 if( i>=wsdAutoextsqlite3Autoext.nExt ){
131058 xInit = 0;
131059 go = 0;
131060 }else{
131061 xInit = (sqlite3_loadext_entry)wsdAutoextsqlite3Autoext.aExt[i];
131062 }
131063 sqlite3_mutex_leave(mutex);
131064 zErrmsg = 0;
131065 if( xInit && (rc = xInit(db, &zErrmsg, pThunk))!=0 ){
131066 sqlite3ErrorWithMsg(db, rc,
131067 "automatic extension loading failed: %s", zErrmsg);
131068 go = 0;
131069 }
131070 sqlite3_free(zErrmsg);
131071 }
131072}
131073
131074/************** End of loadext.c *********************************************/
131075/************** Begin file pragma.c ******************************************/
131076/*
131077** 2003 April 6
131078**
131079** The author disclaims copyright to this source code. In place of
131080** a legal notice, here is a blessing:
131081**
131082** May you do good and not evil.
131083** May you find forgiveness for yourself and forgive others.
131084** May you share freely, never taking more than you give.
131085**
131086*************************************************************************
131087** This file contains code used to implement the PRAGMA command.
131088*/
131089/* #include "sqliteInt.h" */
131090
131091#if !defined(SQLITE_ENABLE_LOCKING_STYLE0)
131092# if defined(__APPLE__)
131093# define SQLITE_ENABLE_LOCKING_STYLE0 1
131094# else
131095# define SQLITE_ENABLE_LOCKING_STYLE0 0
131096# endif
131097#endif
131098
131099/***************************************************************************
131100** The "pragma.h" include file is an automatically generated file that
131101** that includes the PragType_XXXX macro definitions and the aPragmaName[]
131102** object. This ensures that the aPragmaName[] table is arranged in
131103** lexicographical order to facility a binary search of the pragma name.
131104** Do not edit pragma.h directly. Edit and rerun the script in at
131105** ../tool/mkpragmatab.tcl. */
131106/************** Include pragma.h in the middle of pragma.c *******************/
131107/************** Begin file pragma.h ******************************************/
131108/* DO NOT EDIT!
131109** This file is automatically generated by the script at
131110** ../tool/mkpragmatab.tcl. To update the set of pragmas, edit
131111** that script and rerun it.
131112*/
131113
131114/* The various pragma types */
131115#define PragTyp_ACTIVATE_EXTENSIONS0 0
131116#define PragTyp_ANALYSIS_LIMIT1 1
131117#define PragTyp_HEADER_VALUE2 2
131118#define PragTyp_AUTO_VACUUM3 3
131119#define PragTyp_FLAG4 4
131120#define PragTyp_BUSY_TIMEOUT5 5
131121#define PragTyp_CACHE_SIZE6 6
131122#define PragTyp_CACHE_SPILL7 7
131123#define PragTyp_CASE_SENSITIVE_LIKE8 8
131124#define PragTyp_COLLATION_LIST9 9
131125#define PragTyp_COMPILE_OPTIONS10 10
131126#define PragTyp_DATA_STORE_DIRECTORY11 11
131127#define PragTyp_DATABASE_LIST12 12
131128#define PragTyp_DEFAULT_CACHE_SIZE13 13
131129#define PragTyp_ENCODING14 14
131130#define PragTyp_FOREIGN_KEY_CHECK15 15
131131#define PragTyp_FOREIGN_KEY_LIST16 16
131132#define PragTyp_FUNCTION_LIST17 17
131133#define PragTyp_HARD_HEAP_LIMIT18 18
131134#define PragTyp_INCREMENTAL_VACUUM19 19
131135#define PragTyp_INDEX_INFO20 20
131136#define PragTyp_INDEX_LIST21 21
131137#define PragTyp_INTEGRITY_CHECK22 22
131138#define PragTyp_JOURNAL_MODE23 23
131139#define PragTyp_JOURNAL_SIZE_LIMIT24 24
131140#define PragTyp_LOCK_PROXY_FILE25 25
131141#define PragTyp_LOCKING_MODE26 26
131142#define PragTyp_PAGE_COUNT27 27
131143#define PragTyp_MMAP_SIZE28 28
131144#define PragTyp_MODULE_LIST29 29
131145#define PragTyp_OPTIMIZE30 30
131146#define PragTyp_PAGE_SIZE31 31
131147#define PragTyp_PRAGMA_LIST32 32
131148#define PragTyp_SECURE_DELETE33 33
131149#define PragTyp_SHRINK_MEMORY34 34
131150#define PragTyp_SOFT_HEAP_LIMIT35 35
131151#define PragTyp_SYNCHRONOUS36 36
131152#define PragTyp_TABLE_INFO37 37
131153#define PragTyp_TABLE_LIST38 38
131154#define PragTyp_TEMP_STORE39 39
131155#define PragTyp_TEMP_STORE_DIRECTORY40 40
131156#define PragTyp_THREADS41 41
131157#define PragTyp_WAL_AUTOCHECKPOINT42 42
131158#define PragTyp_WAL_CHECKPOINT43 43
131159#define PragTyp_LOCK_STATUS44 44
131160#define PragTyp_STATS45 45
131161
131162/* Property flags associated with various pragma. */
131163#define PragFlg_NeedSchema0x01 0x01 /* Force schema load before running */
131164#define PragFlg_NoColumns0x02 0x02 /* OP_ResultRow called with zero columns */
131165#define PragFlg_NoColumns10x04 0x04 /* zero columns if RHS argument is present */
131166#define PragFlg_ReadOnly0x08 0x08 /* Read-only HEADER_VALUE */
131167#define PragFlg_Result00x10 0x10 /* Acts as query when no argument */
131168#define PragFlg_Result10x20 0x20 /* Acts as query when has one argument */
131169#define PragFlg_SchemaOpt0x40 0x40 /* Schema restricts name search if present */
131170#define PragFlg_SchemaReq0x80 0x80 /* Schema required - "main" is default */
131171
131172/* Names of columns for pragmas that return multi-column result
131173** or that return single-column results where the name of the
131174** result column is different from the name of the pragma
131175*/
131176static const char *const pragCName[] = {
131177 /* 0 */ "id", /* Used by: foreign_key_list */
131178 /* 1 */ "seq",
131179 /* 2 */ "table",
131180 /* 3 */ "from",
131181 /* 4 */ "to",
131182 /* 5 */ "on_update",
131183 /* 6 */ "on_delete",
131184 /* 7 */ "match",
131185 /* 8 */ "cid", /* Used by: table_xinfo */
131186 /* 9 */ "name",
131187 /* 10 */ "type",
131188 /* 11 */ "notnull",
131189 /* 12 */ "dflt_value",
131190 /* 13 */ "pk",
131191 /* 14 */ "hidden",
131192 /* table_info reuses 8 */
131193 /* 15 */ "schema", /* Used by: table_list */
131194 /* 16 */ "name",
131195 /* 17 */ "type",
131196 /* 18 */ "ncol",
131197 /* 19 */ "wr",
131198 /* 20 */ "strict",
131199 /* 21 */ "seqno", /* Used by: index_xinfo */
131200 /* 22 */ "cid",
131201 /* 23 */ "name",
131202 /* 24 */ "desc",
131203 /* 25 */ "coll",
131204 /* 26 */ "key",
131205 /* 27 */ "name", /* Used by: function_list */
131206 /* 28 */ "builtin",
131207 /* 29 */ "type",
131208 /* 30 */ "enc",
131209 /* 31 */ "narg",
131210 /* 32 */ "flags",
131211 /* 33 */ "tbl", /* Used by: stats */
131212 /* 34 */ "idx",
131213 /* 35 */ "wdth",
131214 /* 36 */ "hght",
131215 /* 37 */ "flgs",
131216 /* 38 */ "seq", /* Used by: index_list */
131217 /* 39 */ "name",
131218 /* 40 */ "unique",
131219 /* 41 */ "origin",
131220 /* 42 */ "partial",
131221 /* 43 */ "table", /* Used by: foreign_key_check */
131222 /* 44 */ "rowid",
131223 /* 45 */ "parent",
131224 /* 46 */ "fkid",
131225 /* index_info reuses 21 */
131226 /* 47 */ "seq", /* Used by: database_list */
131227 /* 48 */ "name",
131228 /* 49 */ "file",
131229 /* 50 */ "busy", /* Used by: wal_checkpoint */
131230 /* 51 */ "log",
131231 /* 52 */ "checkpointed",
131232 /* collation_list reuses 38 */
131233 /* 53 */ "database", /* Used by: lock_status */
131234 /* 54 */ "status",
131235 /* 55 */ "cache_size", /* Used by: default_cache_size */
131236 /* module_list pragma_list reuses 9 */
131237 /* 56 */ "timeout", /* Used by: busy_timeout */
131238};
131239
131240/* Definitions of all built-in pragmas */
131241typedef struct PragmaName {
131242 const char *const zName; /* Name of pragma */
131243 u8 ePragTyp; /* PragTyp_XXX value */
131244 u8 mPragFlg; /* Zero or more PragFlg_XXX values */
131245 u8 iPragCName; /* Start of column names in pragCName[] */
131246 u8 nPragCName; /* Num of col names. 0 means use pragma name */
131247 u64 iArg; /* Extra argument */
131248} PragmaName;
131249static const PragmaName aPragmaName[] = {
131250#if defined(SQLITE_ENABLE_CEROD)
131251 {/* zName: */ "activate_extensions",
131252 /* ePragTyp: */ PragTyp_ACTIVATE_EXTENSIONS0,
131253 /* ePragFlg: */ 0,
131254 /* ColNames: */ 0, 0,
131255 /* iArg: */ 0 },
131256#endif
131257 {/* zName: */ "analysis_limit",
131258 /* ePragTyp: */ PragTyp_ANALYSIS_LIMIT1,
131259 /* ePragFlg: */ PragFlg_Result00x10,
131260 /* ColNames: */ 0, 0,
131261 /* iArg: */ 0 },
131262#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
131263 {/* zName: */ "application_id",
131264 /* ePragTyp: */ PragTyp_HEADER_VALUE2,
131265 /* ePragFlg: */ PragFlg_NoColumns10x04|PragFlg_Result00x10,
131266 /* ColNames: */ 0, 0,
131267 /* iArg: */ BTREE_APPLICATION_ID8 },
131268#endif
131269#if !defined(SQLITE_OMIT_AUTOVACUUM)
131270 {/* zName: */ "auto_vacuum",
131271 /* ePragTyp: */ PragTyp_AUTO_VACUUM3,
131272 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80|PragFlg_NoColumns10x04,
131273 /* ColNames: */ 0, 0,
131274 /* iArg: */ 0 },
131275#endif
131276#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
131277#if !defined(SQLITE_OMIT_AUTOMATIC_INDEX)
131278 {/* zName: */ "automatic_index",
131279 /* ePragTyp: */ PragTyp_FLAG4,
131280 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131281 /* ColNames: */ 0, 0,
131282 /* iArg: */ SQLITE_AutoIndex0x00008000 },
131283#endif
131284#endif
131285 {/* zName: */ "busy_timeout",
131286 /* ePragTyp: */ PragTyp_BUSY_TIMEOUT5,
131287 /* ePragFlg: */ PragFlg_Result00x10,
131288 /* ColNames: */ 56, 1,
131289 /* iArg: */ 0 },
131290#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
131291 {/* zName: */ "cache_size",
131292 /* ePragTyp: */ PragTyp_CACHE_SIZE6,
131293 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80|PragFlg_NoColumns10x04,
131294 /* ColNames: */ 0, 0,
131295 /* iArg: */ 0 },
131296#endif
131297#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
131298 {/* zName: */ "cache_spill",
131299 /* ePragTyp: */ PragTyp_CACHE_SPILL7,
131300 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_SchemaReq0x80|PragFlg_NoColumns10x04,
131301 /* ColNames: */ 0, 0,
131302 /* iArg: */ 0 },
131303#endif
131304#if !defined(SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA)
131305 {/* zName: */ "case_sensitive_like",
131306 /* ePragTyp: */ PragTyp_CASE_SENSITIVE_LIKE8,
131307 /* ePragFlg: */ PragFlg_NoColumns0x02,
131308 /* ColNames: */ 0, 0,
131309 /* iArg: */ 0 },
131310#endif
131311 {/* zName: */ "cell_size_check",
131312 /* ePragTyp: */ PragTyp_FLAG4,
131313 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131314 /* ColNames: */ 0, 0,
131315 /* iArg: */ SQLITE_CellSizeCk0x00200000 },
131316#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
131317 {/* zName: */ "checkpoint_fullfsync",
131318 /* ePragTyp: */ PragTyp_FLAG4,
131319 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131320 /* ColNames: */ 0, 0,
131321 /* iArg: */ SQLITE_CkptFullFSync0x00000010 },
131322#endif
131323#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
131324 {/* zName: */ "collation_list",
131325 /* ePragTyp: */ PragTyp_COLLATION_LIST9,
131326 /* ePragFlg: */ PragFlg_Result00x10,
131327 /* ColNames: */ 38, 2,
131328 /* iArg: */ 0 },
131329#endif
131330#if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
131331 {/* zName: */ "compile_options",
131332 /* ePragTyp: */ PragTyp_COMPILE_OPTIONS10,
131333 /* ePragFlg: */ PragFlg_Result00x10,
131334 /* ColNames: */ 0, 0,
131335 /* iArg: */ 0 },
131336#endif
131337#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
131338 {/* zName: */ "count_changes",
131339 /* ePragTyp: */ PragTyp_FLAG4,
131340 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131341 /* ColNames: */ 0, 0,
131342 /* iArg: */ SQLITE_CountRows((u64)(0x00001)<<32) },
131343#endif
131344#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN0
131345 {/* zName: */ "data_store_directory",
131346 /* ePragTyp: */ PragTyp_DATA_STORE_DIRECTORY11,
131347 /* ePragFlg: */ PragFlg_NoColumns10x04,
131348 /* ColNames: */ 0, 0,
131349 /* iArg: */ 0 },
131350#endif
131351#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
131352 {/* zName: */ "data_version",
131353 /* ePragTyp: */ PragTyp_HEADER_VALUE2,
131354 /* ePragFlg: */ PragFlg_ReadOnly0x08|PragFlg_Result00x10,
131355 /* ColNames: */ 0, 0,
131356 /* iArg: */ BTREE_DATA_VERSION15 },
131357#endif
131358#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
131359 {/* zName: */ "database_list",
131360 /* ePragTyp: */ PragTyp_DATABASE_LIST12,
131361 /* ePragFlg: */ PragFlg_Result00x10,
131362 /* ColNames: */ 47, 3,
131363 /* iArg: */ 0 },
131364#endif
131365#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED1)
131366 {/* zName: */ "default_cache_size",
131367 /* ePragTyp: */ PragTyp_DEFAULT_CACHE_SIZE13,
131368 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80|PragFlg_NoColumns10x04,
131369 /* ColNames: */ 55, 1,
131370 /* iArg: */ 0 },
131371#endif
131372#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
131373#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
131374 {/* zName: */ "defer_foreign_keys",
131375 /* ePragTyp: */ PragTyp_FLAG4,
131376 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131377 /* ColNames: */ 0, 0,
131378 /* iArg: */ SQLITE_DeferFKs0x00080000 },
131379#endif
131380#endif
131381#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
131382 {/* zName: */ "empty_result_callbacks",
131383 /* ePragTyp: */ PragTyp_FLAG4,
131384 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131385 /* ColNames: */ 0, 0,
131386 /* iArg: */ SQLITE_NullCallback0x00000100 },
131387#endif
131388#if !defined(SQLITE_OMIT_UTF16)
131389 {/* zName: */ "encoding",
131390 /* ePragTyp: */ PragTyp_ENCODING14,
131391 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131392 /* ColNames: */ 0, 0,
131393 /* iArg: */ 0 },
131394#endif
131395#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
131396 {/* zName: */ "foreign_key_check",
131397 /* ePragTyp: */ PragTyp_FOREIGN_KEY_CHECK15,
131398 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
131399 /* ColNames: */ 43, 4,
131400 /* iArg: */ 0 },
131401#endif
131402#if !defined(SQLITE_OMIT_FOREIGN_KEY)
131403 {/* zName: */ "foreign_key_list",
131404 /* ePragTyp: */ PragTyp_FOREIGN_KEY_LIST16,
131405 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
131406 /* ColNames: */ 0, 8,
131407 /* iArg: */ 0 },
131408#endif
131409#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
131410#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
131411 {/* zName: */ "foreign_keys",
131412 /* ePragTyp: */ PragTyp_FLAG4,
131413 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131414 /* ColNames: */ 0, 0,
131415 /* iArg: */ SQLITE_ForeignKeys0x00004000 },
131416#endif
131417#endif
131418#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
131419 {/* zName: */ "freelist_count",
131420 /* ePragTyp: */ PragTyp_HEADER_VALUE2,
131421 /* ePragFlg: */ PragFlg_ReadOnly0x08|PragFlg_Result00x10,
131422 /* ColNames: */ 0, 0,
131423 /* iArg: */ BTREE_FREE_PAGE_COUNT0 },
131424#endif
131425#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
131426 {/* zName: */ "full_column_names",
131427 /* ePragTyp: */ PragTyp_FLAG4,
131428 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131429 /* ColNames: */ 0, 0,
131430 /* iArg: */ SQLITE_FullColNames0x00000004 },
131431 {/* zName: */ "fullfsync",
131432 /* ePragTyp: */ PragTyp_FLAG4,
131433 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131434 /* ColNames: */ 0, 0,
131435 /* iArg: */ SQLITE_FullFSync0x00000008 },
131436#endif
131437#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
131438#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
131439 {/* zName: */ "function_list",
131440 /* ePragTyp: */ PragTyp_FUNCTION_LIST17,
131441 /* ePragFlg: */ PragFlg_Result00x10,
131442 /* ColNames: */ 27, 6,
131443 /* iArg: */ 0 },
131444#endif
131445#endif
131446 {/* zName: */ "hard_heap_limit",
131447 /* ePragTyp: */ PragTyp_HARD_HEAP_LIMIT18,
131448 /* ePragFlg: */ PragFlg_Result00x10,
131449 /* ColNames: */ 0, 0,
131450 /* iArg: */ 0 },
131451#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
131452#if !defined(SQLITE_OMIT_CHECK)
131453 {/* zName: */ "ignore_check_constraints",
131454 /* ePragTyp: */ PragTyp_FLAG4,
131455 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131456 /* ColNames: */ 0, 0,
131457 /* iArg: */ SQLITE_IgnoreChecks0x00000200 },
131458#endif
131459#endif
131460#if !defined(SQLITE_OMIT_AUTOVACUUM)
131461 {/* zName: */ "incremental_vacuum",
131462 /* ePragTyp: */ PragTyp_INCREMENTAL_VACUUM19,
131463 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_NoColumns0x02,
131464 /* ColNames: */ 0, 0,
131465 /* iArg: */ 0 },
131466#endif
131467#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
131468 {/* zName: */ "index_info",
131469 /* ePragTyp: */ PragTyp_INDEX_INFO20,
131470 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
131471 /* ColNames: */ 21, 3,
131472 /* iArg: */ 0 },
131473 {/* zName: */ "index_list",
131474 /* ePragTyp: */ PragTyp_INDEX_LIST21,
131475 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
131476 /* ColNames: */ 38, 5,
131477 /* iArg: */ 0 },
131478 {/* zName: */ "index_xinfo",
131479 /* ePragTyp: */ PragTyp_INDEX_INFO20,
131480 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
131481 /* ColNames: */ 21, 6,
131482 /* iArg: */ 1 },
131483#endif
131484#if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
131485 {/* zName: */ "integrity_check",
131486 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK22,
131487 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
131488 /* ColNames: */ 0, 0,
131489 /* iArg: */ 0 },
131490#endif
131491#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
131492 {/* zName: */ "journal_mode",
131493 /* ePragTyp: */ PragTyp_JOURNAL_MODE23,
131494 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80,
131495 /* ColNames: */ 0, 0,
131496 /* iArg: */ 0 },
131497 {/* zName: */ "journal_size_limit",
131498 /* ePragTyp: */ PragTyp_JOURNAL_SIZE_LIMIT24,
131499 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_SchemaReq0x80,
131500 /* ColNames: */ 0, 0,
131501 /* iArg: */ 0 },
131502#endif
131503#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
131504 {/* zName: */ "legacy_alter_table",
131505 /* ePragTyp: */ PragTyp_FLAG4,
131506 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131507 /* ColNames: */ 0, 0,
131508 /* iArg: */ SQLITE_LegacyAlter0x04000000 },
131509#endif
131510#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE0
131511 {/* zName: */ "lock_proxy_file",
131512 /* ePragTyp: */ PragTyp_LOCK_PROXY_FILE25,
131513 /* ePragFlg: */ PragFlg_NoColumns10x04,
131514 /* ColNames: */ 0, 0,
131515 /* iArg: */ 0 },
131516#endif
131517#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
131518 {/* zName: */ "lock_status",
131519 /* ePragTyp: */ PragTyp_LOCK_STATUS44,
131520 /* ePragFlg: */ PragFlg_Result00x10,
131521 /* ColNames: */ 53, 2,
131522 /* iArg: */ 0 },
131523#endif
131524#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
131525 {/* zName: */ "locking_mode",
131526 /* ePragTyp: */ PragTyp_LOCKING_MODE26,
131527 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_SchemaReq0x80,
131528 /* ColNames: */ 0, 0,
131529 /* iArg: */ 0 },
131530 {/* zName: */ "max_page_count",
131531 /* ePragTyp: */ PragTyp_PAGE_COUNT27,
131532 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80,
131533 /* ColNames: */ 0, 0,
131534 /* iArg: */ 0 },
131535 {/* zName: */ "mmap_size",
131536 /* ePragTyp: */ PragTyp_MMAP_SIZE28,
131537 /* ePragFlg: */ 0,
131538 /* ColNames: */ 0, 0,
131539 /* iArg: */ 0 },
131540#endif
131541#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
131542#if !defined(SQLITE_OMIT_VIRTUALTABLE)
131543#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
131544 {/* zName: */ "module_list",
131545 /* ePragTyp: */ PragTyp_MODULE_LIST29,
131546 /* ePragFlg: */ PragFlg_Result00x10,
131547 /* ColNames: */ 9, 1,
131548 /* iArg: */ 0 },
131549#endif
131550#endif
131551#endif
131552 {/* zName: */ "optimize",
131553 /* ePragTyp: */ PragTyp_OPTIMIZE30,
131554 /* ePragFlg: */ PragFlg_Result10x20|PragFlg_NeedSchema0x01,
131555 /* ColNames: */ 0, 0,
131556 /* iArg: */ 0 },
131557#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
131558 {/* zName: */ "page_count",
131559 /* ePragTyp: */ PragTyp_PAGE_COUNT27,
131560 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80,
131561 /* ColNames: */ 0, 0,
131562 /* iArg: */ 0 },
131563 {/* zName: */ "page_size",
131564 /* ePragTyp: */ PragTyp_PAGE_SIZE31,
131565 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_SchemaReq0x80|PragFlg_NoColumns10x04,
131566 /* ColNames: */ 0, 0,
131567 /* iArg: */ 0 },
131568#endif
131569#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
131570#if defined(SQLITE_DEBUG)
131571 {/* zName: */ "parser_trace",
131572 /* ePragTyp: */ PragTyp_FLAG4,
131573 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131574 /* ColNames: */ 0, 0,
131575 /* iArg: */ SQLITE_ParserTrace },
131576#endif
131577#endif
131578#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
131579 {/* zName: */ "pragma_list",
131580 /* ePragTyp: */ PragTyp_PRAGMA_LIST32,
131581 /* ePragFlg: */ PragFlg_Result00x10,
131582 /* ColNames: */ 9, 1,
131583 /* iArg: */ 0 },
131584#endif
131585#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
131586 {/* zName: */ "query_only",
131587 /* ePragTyp: */ PragTyp_FLAG4,
131588 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131589 /* ColNames: */ 0, 0,
131590 /* iArg: */ SQLITE_QueryOnly0x00100000 },
131591#endif
131592#if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
131593 {/* zName: */ "quick_check",
131594 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK22,
131595 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
131596 /* ColNames: */ 0, 0,
131597 /* iArg: */ 0 },
131598#endif
131599#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
131600 {/* zName: */ "read_uncommitted",
131601 /* ePragTyp: */ PragTyp_FLAG4,
131602 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131603 /* ColNames: */ 0, 0,
131604 /* iArg: */ SQLITE_ReadUncommit0x00000400 },
131605 {/* zName: */ "recursive_triggers",
131606 /* ePragTyp: */ PragTyp_FLAG4,
131607 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131608 /* ColNames: */ 0, 0,
131609 /* iArg: */ SQLITE_RecTriggers0x00002000 },
131610 {/* zName: */ "reverse_unordered_selects",
131611 /* ePragTyp: */ PragTyp_FLAG4,
131612 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131613 /* ColNames: */ 0, 0,
131614 /* iArg: */ SQLITE_ReverseOrder0x00001000 },
131615#endif
131616#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
131617 {/* zName: */ "schema_version",
131618 /* ePragTyp: */ PragTyp_HEADER_VALUE2,
131619 /* ePragFlg: */ PragFlg_NoColumns10x04|PragFlg_Result00x10,
131620 /* ColNames: */ 0, 0,
131621 /* iArg: */ BTREE_SCHEMA_VERSION1 },
131622#endif
131623#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
131624 {/* zName: */ "secure_delete",
131625 /* ePragTyp: */ PragTyp_SECURE_DELETE33,
131626 /* ePragFlg: */ PragFlg_Result00x10,
131627 /* ColNames: */ 0, 0,
131628 /* iArg: */ 0 },
131629#endif
131630#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
131631 {/* zName: */ "short_column_names",
131632 /* ePragTyp: */ PragTyp_FLAG4,
131633 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131634 /* ColNames: */ 0, 0,
131635 /* iArg: */ SQLITE_ShortColNames0x00000040 },
131636#endif
131637 {/* zName: */ "shrink_memory",
131638 /* ePragTyp: */ PragTyp_SHRINK_MEMORY34,
131639 /* ePragFlg: */ PragFlg_NoColumns0x02,
131640 /* ColNames: */ 0, 0,
131641 /* iArg: */ 0 },
131642 {/* zName: */ "soft_heap_limit",
131643 /* ePragTyp: */ PragTyp_SOFT_HEAP_LIMIT35,
131644 /* ePragFlg: */ PragFlg_Result00x10,
131645 /* ColNames: */ 0, 0,
131646 /* iArg: */ 0 },
131647#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
131648#if defined(SQLITE_DEBUG)
131649 {/* zName: */ "sql_trace",
131650 /* ePragTyp: */ PragTyp_FLAG4,
131651 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131652 /* ColNames: */ 0, 0,
131653 /* iArg: */ SQLITE_SqlTrace },
131654#endif
131655#endif
131656#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && defined(SQLITE_DEBUG)
131657 {/* zName: */ "stats",
131658 /* ePragTyp: */ PragTyp_STATS45,
131659 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80,
131660 /* ColNames: */ 33, 5,
131661 /* iArg: */ 0 },
131662#endif
131663#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
131664 {/* zName: */ "synchronous",
131665 /* ePragTyp: */ PragTyp_SYNCHRONOUS36,
131666 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80|PragFlg_NoColumns10x04,
131667 /* ColNames: */ 0, 0,
131668 /* iArg: */ 0 },
131669#endif
131670#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
131671 {/* zName: */ "table_info",
131672 /* ePragTyp: */ PragTyp_TABLE_INFO37,
131673 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
131674 /* ColNames: */ 8, 6,
131675 /* iArg: */ 0 },
131676 {/* zName: */ "table_list",
131677 /* ePragTyp: */ PragTyp_TABLE_LIST38,
131678 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20,
131679 /* ColNames: */ 15, 6,
131680 /* iArg: */ 0 },
131681 {/* zName: */ "table_xinfo",
131682 /* ePragTyp: */ PragTyp_TABLE_INFO37,
131683 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
131684 /* ColNames: */ 8, 7,
131685 /* iArg: */ 1 },
131686#endif
131687#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
131688 {/* zName: */ "temp_store",
131689 /* ePragTyp: */ PragTyp_TEMP_STORE39,
131690 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131691 /* ColNames: */ 0, 0,
131692 /* iArg: */ 0 },
131693 {/* zName: */ "temp_store_directory",
131694 /* ePragTyp: */ PragTyp_TEMP_STORE_DIRECTORY40,
131695 /* ePragFlg: */ PragFlg_NoColumns10x04,
131696 /* ColNames: */ 0, 0,
131697 /* iArg: */ 0 },
131698#endif
131699 {/* zName: */ "threads",
131700 /* ePragTyp: */ PragTyp_THREADS41,
131701 /* ePragFlg: */ PragFlg_Result00x10,
131702 /* ColNames: */ 0, 0,
131703 /* iArg: */ 0 },
131704#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
131705 {/* zName: */ "trusted_schema",
131706 /* ePragTyp: */ PragTyp_FLAG4,
131707 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131708 /* ColNames: */ 0, 0,
131709 /* iArg: */ SQLITE_TrustedSchema0x00000080 },
131710#endif
131711#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
131712 {/* zName: */ "user_version",
131713 /* ePragTyp: */ PragTyp_HEADER_VALUE2,
131714 /* ePragFlg: */ PragFlg_NoColumns10x04|PragFlg_Result00x10,
131715 /* ColNames: */ 0, 0,
131716 /* iArg: */ BTREE_USER_VERSION6 },
131717#endif
131718#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
131719#if defined(SQLITE_DEBUG)
131720 {/* zName: */ "vdbe_addoptrace",
131721 /* ePragTyp: */ PragTyp_FLAG4,
131722 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131723 /* ColNames: */ 0, 0,
131724 /* iArg: */ SQLITE_VdbeAddopTrace },
131725 {/* zName: */ "vdbe_debug",
131726 /* ePragTyp: */ PragTyp_FLAG4,
131727 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131728 /* ColNames: */ 0, 0,
131729 /* iArg: */ SQLITE_SqlTrace|SQLITE_VdbeListing|SQLITE_VdbeTrace },
131730 {/* zName: */ "vdbe_eqp",
131731 /* ePragTyp: */ PragTyp_FLAG4,
131732 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131733 /* ColNames: */ 0, 0,
131734 /* iArg: */ SQLITE_VdbeEQP },
131735 {/* zName: */ "vdbe_listing",
131736 /* ePragTyp: */ PragTyp_FLAG4,
131737 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131738 /* ColNames: */ 0, 0,
131739 /* iArg: */ SQLITE_VdbeListing },
131740 {/* zName: */ "vdbe_trace",
131741 /* ePragTyp: */ PragTyp_FLAG4,
131742 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131743 /* ColNames: */ 0, 0,
131744 /* iArg: */ SQLITE_VdbeTrace },
131745#endif
131746#endif
131747#if !defined(SQLITE_OMIT_WAL)
131748 {/* zName: */ "wal_autocheckpoint",
131749 /* ePragTyp: */ PragTyp_WAL_AUTOCHECKPOINT42,
131750 /* ePragFlg: */ 0,
131751 /* ColNames: */ 0, 0,
131752 /* iArg: */ 0 },
131753 {/* zName: */ "wal_checkpoint",
131754 /* ePragTyp: */ PragTyp_WAL_CHECKPOINT43,
131755 /* ePragFlg: */ PragFlg_NeedSchema0x01,
131756 /* ColNames: */ 50, 3,
131757 /* iArg: */ 0 },
131758#endif
131759#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
131760 {/* zName: */ "writable_schema",
131761 /* ePragTyp: */ PragTyp_FLAG4,
131762 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
131763 /* ColNames: */ 0, 0,
131764 /* iArg: */ SQLITE_WriteSchema0x00000001|SQLITE_NoSchemaError0x08000000 },
131765#endif
131766};
131767/* Number of pragmas: 68 on by default, 78 total. */
131768
131769/************** End of pragma.h **********************************************/
131770/************** Continuing where we left off in pragma.c *********************/
131771
131772/*
131773** Interpret the given string as a safety level. Return 0 for OFF,
131774** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA. Return 1 for an empty or
131775** unrecognized string argument. The FULL and EXTRA option is disallowed
131776** if the omitFull parameter it 1.
131777**
131778** Note that the values returned are one less that the values that
131779** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
131780** to support legacy SQL code. The safety level used to be boolean
131781** and older scripts may have used numbers 0 for OFF and 1 for ON.
131782*/
131783static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){
131784 /* 123456789 123456789 123 */
131785 static const char zText[] = "onoffalseyestruextrafull";
131786 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 15, 20};
131787 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 5, 4};
131788 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 3, 2};
131789 /* on no off false yes true extra full */
131790 int i, n;
131791 if( sqlite3Isdigit(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x04) ){
131792 return (u8)sqlite3Atoi(z);
131793 }
131794 n = sqlite3Strlen30(z);
131795 for(i=0; i<ArraySize(iLength)((int)(sizeof(iLength)/sizeof(iLength[0]))); i++){
131796 if( iLength[i]==n && sqlite3StrNICmpsqlite3_strnicmp(&zText[iOffset[i]],z,n)==0
131797 && (!omitFull || iValue[i]<=1)
131798 ){
131799 return iValue[i];
131800 }
131801 }
131802 return dflt;
131803}
131804
131805/*
131806** Interpret the given string as a boolean value.
131807*/
131808SQLITE_PRIVATEstatic u8 sqlite3GetBoolean(const char *z, u8 dflt){
131809 return getSafetyLevel(z,1,dflt)!=0;
131810}
131811
131812/* The sqlite3GetBoolean() function is used by other modules but the
131813** remainder of this file is specific to PRAGMA processing. So omit
131814** the rest of the file if PRAGMAs are omitted from the build.
131815*/
131816#if !defined(SQLITE_OMIT_PRAGMA)
131817
131818/*
131819** Interpret the given string as a locking mode value.
131820*/
131821static int getLockingMode(const char *z){
131822 if( z ){
131823 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE1;
131824 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL0;
131825 }
131826 return PAGER_LOCKINGMODE_QUERY-1;
131827}
131828
131829#ifndef SQLITE_OMIT_AUTOVACUUM
131830/*
131831** Interpret the given string as an auto-vacuum mode value.
131832**
131833** The following strings, "none", "full" and "incremental" are
131834** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
131835*/
131836static int getAutoVacuum(const char *z){
131837 int i;
131838 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE0;
131839 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL1;
131840 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR2;
131841 i = sqlite3Atoi(z);
131842 return (u8)((i>=0&&i<=2)?i:0);
131843}
131844#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
131845
131846#ifndef SQLITE_OMIT_PAGER_PRAGMAS
131847/*
131848** Interpret the given string as a temp db location. Return 1 for file
131849** backed temporary databases, 2 for the Red-Black tree in memory database
131850** and 0 to use the compile-time default.
131851*/
131852static int getTempStore(const char *z){
131853 if( z[0]>='0' && z[0]<='2' ){
131854 return z[0] - '0';
131855 }else if( sqlite3StrICmp(z, "file")==0 ){
131856 return 1;
131857 }else if( sqlite3StrICmp(z, "memory")==0 ){
131858 return 2;
131859 }else{
131860 return 0;
131861 }
131862}
131863#endif /* SQLITE_PAGER_PRAGMAS */
131864
131865#ifndef SQLITE_OMIT_PAGER_PRAGMAS
131866/*
131867** Invalidate temp storage, either when the temp storage is changed
131868** from default, or when 'file' and the temp_store_directory has changed
131869*/
131870static int invalidateTempStorage(Parse *pParse){
131871 sqlite3 *db = pParse->db;
131872 if( db->aDb[1].pBt!=0 ){
131873 if( !db->autoCommit
131874 || sqlite3BtreeTxnState(db->aDb[1].pBt)!=SQLITE_TXN_NONE0
131875 ){
131876 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
131877 "from within a transaction");
131878 return SQLITE_ERROR1;
131879 }
131880 sqlite3BtreeClose(db->aDb[1].pBt);
131881 db->aDb[1].pBt = 0;
131882 sqlite3ResetAllSchemasOfConnection(db);
131883 }
131884 return SQLITE_OK0;
131885}
131886#endif /* SQLITE_PAGER_PRAGMAS */
131887
131888#ifndef SQLITE_OMIT_PAGER_PRAGMAS
131889/*
131890** If the TEMP database is open, close it and mark the database schema
131891** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
131892** or DEFAULT_TEMP_STORE pragmas.
131893*/
131894static int changeTempStorage(Parse *pParse, const char *zStorageType){
131895 int ts = getTempStore(zStorageType);
131896 sqlite3 *db = pParse->db;
131897 if( db->temp_store==ts ) return SQLITE_OK0;
131898 if( invalidateTempStorage( pParse ) != SQLITE_OK0 ){
131899 return SQLITE_ERROR1;
131900 }
131901 db->temp_store = (u8)ts;
131902 return SQLITE_OK0;
131903}
131904#endif /* SQLITE_PAGER_PRAGMAS */
131905
131906/*
131907** Set result column names for a pragma.
131908*/
131909static void setPragmaResultColumnNames(
131910 Vdbe *v, /* The query under construction */
131911 const PragmaName *pPragma /* The pragma */
131912){
131913 u8 n = pPragma->nPragCName;
131914 sqlite3VdbeSetNumCols(v, n==0 ? 1 : n);
131915 if( n==0 ){
131916 sqlite3VdbeSetColName(v, 0, COLNAME_NAME0, pPragma->zName, SQLITE_STATIC((sqlite3_destructor_type)0));
131917 }else{
131918 int i, j;
131919 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
131920 sqlite3VdbeSetColName(v, i, COLNAME_NAME0, pragCName[j], SQLITE_STATIC((sqlite3_destructor_type)0));
131921 }
131922 }
131923}
131924
131925/*
131926** Generate code to return a single integer value.
131927*/
131928static void returnSingleInt(Vdbe *v, i64 value){
131929 sqlite3VdbeAddOp4Dup8(v, OP_Int6472, 0, 1, 0, (const u8*)&value, P4_INT64(-13));
131930 sqlite3VdbeAddOp2(v, OP_ResultRow84, 1, 1);
131931}
131932
131933/*
131934** Generate code to return a single text value.
131935*/
131936static void returnSingleText(
131937 Vdbe *v, /* Prepared statement under construction */
131938 const char *zValue /* Value to be returned */
131939){
131940 if( zValue ){
131941 sqlite3VdbeLoadString(v, 1, (const char*)zValue);
131942 sqlite3VdbeAddOp2(v, OP_ResultRow84, 1, 1);
131943 }
131944}
131945
131946
131947/*
131948** Set the safety_level and pager flags for pager iDb. Or if iDb<0
131949** set these values for all pagers.
131950*/
131951#ifndef SQLITE_OMIT_PAGER_PRAGMAS
131952static void setAllPagerFlags(sqlite3 *db){
131953 if( db->autoCommit ){
131954 Db *pDb = db->aDb;
131955 int n = db->nDb;
131956 assert( SQLITE_FullFSync==PAGER_FULLFSYNC )((void) (0));
131957 assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC )((void) (0));
131958 assert( SQLITE_CacheSpill==PAGER_CACHESPILL )((void) (0));
131959 assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)((void) (0))
131960 == PAGER_FLAGS_MASK )((void) (0));
131961 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level )((void) (0));
131962 while( (n--) > 0 ){
131963 if( pDb->pBt ){
131964 sqlite3BtreeSetPagerFlags(pDb->pBt,
131965 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK0x38) );
131966 }
131967 pDb++;
131968 }
131969 }
131970}
131971#else
131972# define setAllPagerFlags(X) /* no-op */
131973#endif
131974
131975
131976/*
131977** Return a human-readable name for a constraint resolution action.
131978*/
131979#ifndef SQLITE_OMIT_FOREIGN_KEY
131980static const char *actionName(u8 action){
131981 const char *zName;
131982 switch( action ){
131983 case OE_SetNull8: zName = "SET NULL"; break;
131984 case OE_SetDflt9: zName = "SET DEFAULT"; break;
131985 case OE_Cascade10: zName = "CASCADE"; break;
131986 case OE_Restrict7: zName = "RESTRICT"; break;
131987 default: zName = "NO ACTION";
131988 assert( action==OE_None )((void) (0)); break;
131989 }
131990 return zName;
131991}
131992#endif
131993
131994
131995/*
131996** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
131997** defined in pager.h. This function returns the associated lowercase
131998** journal-mode name.
131999*/
132000SQLITE_PRIVATEstatic const char *sqlite3JournalModename(int eMode){
132001 static char * const azModeName[] = {
132002 "delete", "persist", "off", "truncate", "memory"
132003#ifndef SQLITE_OMIT_WAL
132004 , "wal"
132005#endif
132006 };
132007 assert( PAGER_JOURNALMODE_DELETE==0 )((void) (0));
132008 assert( PAGER_JOURNALMODE_PERSIST==1 )((void) (0));
132009 assert( PAGER_JOURNALMODE_OFF==2 )((void) (0));
132010 assert( PAGER_JOURNALMODE_TRUNCATE==3 )((void) (0));
132011 assert( PAGER_JOURNALMODE_MEMORY==4 )((void) (0));
132012 assert( PAGER_JOURNALMODE_WAL==5 )((void) (0));
132013 assert( eMode>=0 && eMode<=ArraySize(azModeName) )((void) (0));
132014
132015 if( eMode==ArraySize(azModeName)((int)(sizeof(azModeName)/sizeof(azModeName[0]))) ) return 0;
132016 return azModeName[eMode];
132017}
132018
132019/*
132020** Locate a pragma in the aPragmaName[] array.
132021*/
132022static const PragmaName *pragmaLocate(const char *zName){
132023 int upr, lwr, mid = 0, rc;
132024 lwr = 0;
132025 upr = ArraySize(aPragmaName)((int)(sizeof(aPragmaName)/sizeof(aPragmaName[0])))-1;
132026 while( lwr<=upr ){
132027 mid = (lwr+upr)/2;
132028 rc = sqlite3_stricmp(zName, aPragmaName[mid].zName);
132029 if( rc==0 ) break;
132030 if( rc<0 ){
132031 upr = mid - 1;
132032 }else{
132033 lwr = mid + 1;
132034 }
132035 }
132036 return lwr>upr ? 0 : &aPragmaName[mid];
132037}
132038
132039/*
132040** Create zero or more entries in the output for the SQL functions
132041** defined by FuncDef p.
132042*/
132043static void pragmaFunclistLine(
132044 Vdbe *v, /* The prepared statement being created */
132045 FuncDef *p, /* A particular function definition */
132046 int isBuiltin, /* True if this is a built-in function */
132047 int showInternFuncs /* True if showing internal functions */
132048){
132049 u32 mask =
132050 SQLITE_DETERMINISTIC0x000000800 |
132051 SQLITE_DIRECTONLY0x000080000 |
132052 SQLITE_SUBTYPE0x000100000 |
132053 SQLITE_INNOCUOUS0x000200000 |
132054 SQLITE_FUNC_INTERNAL0x00040000
132055 ;
132056 if( showInternFuncs ) mask = 0xffffffff;
132057 for(; p; p=p->pNext){
132058 const char *zType;
132059 static const char *azEnc[] = { 0, "utf8", "utf16le", "utf16be" };
132060
132061 assert( SQLITE_FUNC_ENCMASK==0x3 )((void) (0));
132062 assert( strcmp(azEnc[SQLITE_UTF8],"utf8")==0 )((void) (0));
132063 assert( strcmp(azEnc[SQLITE_UTF16LE],"utf16le")==0 )((void) (0));
132064 assert( strcmp(azEnc[SQLITE_UTF16BE],"utf16be")==0 )((void) (0));
132065
132066 if( p->xSFunc==0 ) continue;
132067 if( (p->funcFlags & SQLITE_FUNC_INTERNAL0x00040000)!=0
132068 && showInternFuncs==0
132069 ){
132070 continue;
132071 }
132072 if( p->xValue!=0 ){
132073 zType = "w";
132074 }else if( p->xFinalize!=0 ){
132075 zType = "a";
132076 }else{
132077 zType = "s";
132078 }
132079 sqlite3VdbeMultiLoad(v, 1, "sissii",
132080 p->zName, isBuiltin,
132081 zType, azEnc[p->funcFlags&SQLITE_FUNC_ENCMASK0x0003],
132082 p->nArg,
132083 (p->funcFlags & mask) ^ SQLITE_INNOCUOUS0x000200000
132084 );
132085 }
132086}
132087
132088
132089/*
132090** Helper subroutine for PRAGMA integrity_check:
132091**
132092** Generate code to output a single-column result row with a value of the
132093** string held in register 3. Decrement the result count in register 1
132094** and halt if the maximum number of result rows have been issued.
132095*/
132096static int integrityCheckResultRow(Vdbe *v){
132097 int addr;
132098 sqlite3VdbeAddOp2(v, OP_ResultRow84, 3, 1);
132099 addr = sqlite3VdbeAddOp3(v, OP_IfPos49, 1, sqlite3VdbeCurrentAddr(v)+2, 1);
132100 VdbeCoverage(v);
132101 sqlite3VdbeAddOp0(v, OP_Halt70);
132102 return addr;
132103}
132104
132105/*
132106** Process a pragma statement.
132107**
132108** Pragmas are of this form:
132109**
132110** PRAGMA [schema.]id [= value]
132111**
132112** The identifier might also be a string. The value is a string, and
132113** identifier, or a number. If minusFlag is true, then the value is
132114** a number that was preceded by a minus sign.
132115**
132116** If the left side is "database.id" then pId1 is the database name
132117** and pId2 is the id. If the left side is just "id" then pId1 is the
132118** id and pId2 is any empty string.
132119*/
132120SQLITE_PRIVATEstatic void sqlite3Pragma(
132121 Parse *pParse,
132122 Token *pId1, /* First part of [schema.]id field */
132123 Token *pId2, /* Second part of [schema.]id field, or NULL */
132124 Token *pValue, /* Token for <value>, or NULL */
132125 int minusFlag /* True if a '-' sign preceded <value> */
132126){
132127 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
132128 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
132129 const char *zDb = 0; /* The database name */
132130 Token *pId; /* Pointer to <id> token */
132131 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
132132 int iDb; /* Database index for <database> */
132133 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
132134 sqlite3 *db = pParse->db; /* The database connection */
132135 Db *pDb; /* The specific database being pragmaed */
132136 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
132137 const PragmaName *pPragma; /* The pragma */
132138
132139 if( v==0 ) return;
132140 sqlite3VdbeRunOnlyOnce(v);
132141 pParse->nMem = 2;
132142
132143 /* Interpret the [schema.] part of the pragma statement. iDb is the
132144 ** index of the database this pragma is being applied to in db.aDb[]. */
132145 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
132146 if( iDb<0 ) return;
132147 pDb = &db->aDb[iDb];
132148
132149 /* If the temp database has been explicitly named as part of the
132150 ** pragma, make sure it is open.
132151 */
132152 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
132153 return;
132154 }
132155
132156 zLeft = sqlite3NameFromToken(db, pId);
132157 if( !zLeft ) return;
132158 if( minusFlag ){
132159 zRight = sqlite3MPrintf(db, "-%T", pValue);
132160 }else{
132161 zRight = sqlite3NameFromToken(db, pValue);
132162 }
132163
132164 assert( pId2 )((void) (0));
132165 zDb = pId2->n>0 ? pDb->zDbSName : 0;
132166 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA19, zLeft, zRight, zDb) ){
132167 goto pragma_out;
132168 }
132169
132170 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
132171 ** connection. If it returns SQLITE_OK, then assume that the VFS
132172 ** handled the pragma and generate a no-op prepared statement.
132173 **
132174 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
132175 ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file
132176 ** object corresponding to the database file to which the pragma
132177 ** statement refers.
132178 **
132179 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
132180 ** file control is an array of pointers to strings (char**) in which the
132181 ** second element of the array is the name of the pragma and the third
132182 ** element is the argument to the pragma or NULL if the pragma has no
132183 ** argument.
132184 */
132185 aFcntl[0] = 0;
132186 aFcntl[1] = zLeft;
132187 aFcntl[2] = zRight;
132188 aFcntl[3] = 0;
132189 db->busyHandler.nBusy = 0;
132190 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA14, (void*)aFcntl);
132191 if( rc==SQLITE_OK0 ){
132192 sqlite3VdbeSetNumCols(v, 1);
132193 sqlite3VdbeSetColName(v, 0, COLNAME_NAME0, aFcntl[0], SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
132194 returnSingleText(v, aFcntl[0]);
132195 sqlite3_free(aFcntl[0]);
132196 goto pragma_out;
132197 }
132198 if( rc!=SQLITE_NOTFOUND12 ){
132199 if( aFcntl[0] ){
132200 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
132201 sqlite3_free(aFcntl[0]);
132202 }
132203 pParse->nErr++;
132204 pParse->rc = rc;
132205 goto pragma_out;
132206 }
132207
132208 /* Locate the pragma in the lookup table */
132209 pPragma = pragmaLocate(zLeft);
132210 if( pPragma==0 ){
132211 /* IMP: R-43042-22504 No error messages are generated if an
132212 ** unknown pragma is issued. */
132213 goto pragma_out;
132214 }
132215
132216 /* Make sure the database schema is loaded if the pragma requires that */
132217 if( (pPragma->mPragFlg & PragFlg_NeedSchema0x01)!=0 ){
132218 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
132219 }
132220
132221 /* Register the result column names for pragmas that return results */
132222 if( (pPragma->mPragFlg & PragFlg_NoColumns0x02)==0
132223 && ((pPragma->mPragFlg & PragFlg_NoColumns10x04)==0 || zRight==0)
132224 ){
132225 setPragmaResultColumnNames(v, pPragma);
132226 }
132227
132228 /* Jump to the appropriate pragma handler */
132229 switch( pPragma->ePragTyp ){
132230
132231#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED1)
132232 /*
132233 ** PRAGMA [schema.]default_cache_size
132234 ** PRAGMA [schema.]default_cache_size=N
132235 **
132236 ** The first form reports the current persistent setting for the
132237 ** page cache size. The value returned is the maximum number of
132238 ** pages in the page cache. The second form sets both the current
132239 ** page cache size value and the persistent page cache size value
132240 ** stored in the database file.
132241 **
132242 ** Older versions of SQLite would set the default cache size to a
132243 ** negative number to indicate synchronous=OFF. These days, synchronous
132244 ** is always on by default regardless of the sign of the default cache
132245 ** size. But continue to take the absolute value of the default cache
132246 ** size of historical compatibility.
132247 */
132248 case PragTyp_DEFAULT_CACHE_SIZE13: {
132249 static const int iLn = VDBE_OFFSET_LINENO(2)0;
132250 static const VdbeOpList getCacheSize[] = {
132251 { OP_Transaction2, 0, 0, 0}, /* 0 */
132252 { OP_ReadCookie99, 0, 1, BTREE_DEFAULT_CACHE_SIZE3}, /* 1 */
132253 { OP_IfPos49, 1, 8, 0},
132254 { OP_Integer71, 0, 2, 0},
132255 { OP_Subtract107, 1, 2, 1},
132256 { OP_IfPos49, 1, 8, 0},
132257 { OP_Integer71, 0, 1, 0}, /* 6 */
132258 { OP_Noop184, 0, 0, 0},
132259 { OP_ResultRow84, 1, 1, 0},
132260 };
132261 VdbeOp *aOp;
132262 sqlite3VdbeUsesBtree(v, iDb);
132263 if( !zRight ){
132264 pParse->nMem += 2;
132265 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize));
132266 aOp = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize)((int)(sizeof(getCacheSize)/sizeof(getCacheSize[0]))), getCacheSize, iLn);
132267 if( ONLY_IF_REALLOC_STRESS(aOp==0)(0) ) break;
132268 aOp[0].p1 = iDb;
132269 aOp[1].p1 = iDb;
132270 aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE128;
132271 }else{
132272 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
132273 sqlite3BeginWriteOperation(pParse, 0, iDb);
132274 sqlite3VdbeAddOp3(v, OP_SetCookie100, iDb, BTREE_DEFAULT_CACHE_SIZE3, size);
132275 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
132276 pDb->pSchema->cache_size = size;
132277 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
132278 }
132279 break;
132280 }
132281#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
132282
132283#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
132284 /*
132285 ** PRAGMA [schema.]page_size
132286 ** PRAGMA [schema.]page_size=N
132287 **
132288 ** The first form reports the current setting for the
132289 ** database page size in bytes. The second form sets the
132290 ** database page size value. The value can only be set if
132291 ** the database has not yet been created.
132292 */
132293 case PragTyp_PAGE_SIZE31: {
132294 Btree *pBt = pDb->pBt;
132295 assert( pBt!=0 )((void) (0));
132296 if( !zRight ){
132297 int size = ALWAYS(pBt)(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
132298 returnSingleInt(v, size);
132299 }else{
132300 /* Malloc may fail when setting the page-size, as there is an internal
132301 ** buffer that the pager module resizes using sqlite3_realloc().
132302 */
132303 db->nextPagesize = sqlite3Atoi(zRight);
132304 if( SQLITE_NOMEM7==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,0,0) ){
132305 sqlite3OomFault(db);
132306 }
132307 }
132308 break;
132309 }
132310
132311 /*
132312 ** PRAGMA [schema.]secure_delete
132313 ** PRAGMA [schema.]secure_delete=ON/OFF/FAST
132314 **
132315 ** The first form reports the current setting for the
132316 ** secure_delete flag. The second form changes the secure_delete
132317 ** flag setting and reports the new value.
132318 */
132319 case PragTyp_SECURE_DELETE33: {
132320 Btree *pBt = pDb->pBt;
132321 int b = -1;
132322 assert( pBt!=0 )((void) (0));
132323 if( zRight ){
132324 if( sqlite3_stricmp(zRight, "fast")==0 ){
132325 b = 2;
132326 }else{
132327 b = sqlite3GetBoolean(zRight, 0);
132328 }
132329 }
132330 if( pId2->n==0 && b>=0 ){
132331 int ii;
132332 for(ii=0; ii<db->nDb; ii++){
132333 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
132334 }
132335 }
132336 b = sqlite3BtreeSecureDelete(pBt, b);
132337 returnSingleInt(v, b);
132338 break;
132339 }
132340
132341 /*
132342 ** PRAGMA [schema.]max_page_count
132343 ** PRAGMA [schema.]max_page_count=N
132344 **
132345 ** The first form reports the current setting for the
132346 ** maximum number of pages in the database file. The
132347 ** second form attempts to change this setting. Both
132348 ** forms return the current setting.
132349 **
132350 ** The absolute value of N is used. This is undocumented and might
132351 ** change. The only purpose is to provide an easy way to test
132352 ** the sqlite3AbsInt32() function.
132353 **
132354 ** PRAGMA [schema.]page_count
132355 **
132356 ** Return the number of pages in the specified database.
132357 */
132358 case PragTyp_PAGE_COUNT27: {
132359 int iReg;
132360 i64 x = 0;
132361 sqlite3CodeVerifySchema(pParse, iDb);
132362 iReg = ++pParse->nMem;
132363 if( sqlite3Tolower(zLeft[0])(sqlite3UpperToLower[(unsigned char)(zLeft[0])])=='p' ){
132364 sqlite3VdbeAddOp2(v, OP_Pagecount177, iDb, iReg);
132365 }else{
132366 if( zRight && sqlite3DecOrHexToI64(zRight,&x)==0 ){
132367 if( x<0 ) x = 0;
132368 else if( x>0xfffffffe ) x = 0xfffffffe;
132369 }else{
132370 x = 0;
132371 }
132372 sqlite3VdbeAddOp3(v, OP_MaxPgcnt178, iDb, iReg, (int)x);
132373 }
132374 sqlite3VdbeAddOp2(v, OP_ResultRow84, iReg, 1);
132375 break;
132376 }
132377
132378 /*
132379 ** PRAGMA [schema.]locking_mode
132380 ** PRAGMA [schema.]locking_mode = (normal|exclusive)
132381 */
132382 case PragTyp_LOCKING_MODE26: {
132383 const char *zRet = "normal";
132384 int eMode = getLockingMode(zRight);
132385
132386 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY-1 ){
132387 /* Simple "PRAGMA locking_mode;" statement. This is a query for
132388 ** the current default locking mode (which may be different to
132389 ** the locking-mode of the main database).
132390 */
132391 eMode = db->dfltLockMode;
132392 }else{
132393 Pager *pPager;
132394 if( pId2->n==0 ){
132395 /* This indicates that no database name was specified as part
132396 ** of the PRAGMA command. In this case the locking-mode must be
132397 ** set on all attached databases, as well as the main db file.
132398 **
132399 ** Also, the sqlite3.dfltLockMode variable is set so that
132400 ** any subsequently attached databases also use the specified
132401 ** locking mode.
132402 */
132403 int ii;
132404 assert(pDb==&db->aDb[0])((void) (0));
132405 for(ii=2; ii<db->nDb; ii++){
132406 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
132407 sqlite3PagerLockingMode(pPager, eMode);
132408 }
132409 db->dfltLockMode = (u8)eMode;
132410 }
132411 pPager = sqlite3BtreePager(pDb->pBt);
132412 eMode = sqlite3PagerLockingMode(pPager, eMode);
132413 }
132414
132415 assert( eMode==PAGER_LOCKINGMODE_NORMAL((void) (0))
132416 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE )((void) (0));
132417 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE1 ){
132418 zRet = "exclusive";
132419 }
132420 returnSingleText(v, zRet);
132421 break;
132422 }
132423
132424 /*
132425 ** PRAGMA [schema.]journal_mode
132426 ** PRAGMA [schema.]journal_mode =
132427 ** (delete|persist|off|truncate|memory|wal|off)
132428 */
132429 case PragTyp_JOURNAL_MODE23: {
132430 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
132431 int ii; /* Loop counter */
132432
132433 if( zRight==0 ){
132434 /* If there is no "=MODE" part of the pragma, do a query for the
132435 ** current mode */
132436 eMode = PAGER_JOURNALMODE_QUERY(-1);
132437 }else{
132438 const char *zMode;
132439 int n = sqlite3Strlen30(zRight);
132440 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
132441 if( sqlite3StrNICmpsqlite3_strnicmp(zRight, zMode, n)==0 ) break;
132442 }
132443 if( !zMode ){
132444 /* If the "=MODE" part does not match any known journal mode,
132445 ** then do a query */
132446 eMode = PAGER_JOURNALMODE_QUERY(-1);
132447 }
132448 if( eMode==PAGER_JOURNALMODE_OFF2 && (db->flags & SQLITE_Defensive0x10000000)!=0 ){
132449 /* Do not allow journal-mode "OFF" in defensive since the database
132450 ** can become corrupted using ordinary SQL when the journal is off */
132451 eMode = PAGER_JOURNALMODE_QUERY(-1);
132452 }
132453 }
132454 if( eMode==PAGER_JOURNALMODE_QUERY(-1) && pId2->n==0 ){
132455 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
132456 iDb = 0;
132457 pId2->n = 1;
132458 }
132459 for(ii=db->nDb-1; ii>=0; ii--){
132460 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
132461 sqlite3VdbeUsesBtree(v, ii);
132462 sqlite3VdbeAddOp3(v, OP_JournalMode4, ii, 1, eMode);
132463 }
132464 }
132465 sqlite3VdbeAddOp2(v, OP_ResultRow84, 1, 1);
132466 break;
132467 }
132468
132469 /*
132470 ** PRAGMA [schema.]journal_size_limit
132471 ** PRAGMA [schema.]journal_size_limit=N
132472 **
132473 ** Get or set the size limit on rollback journal files.
132474 */
132475 case PragTyp_JOURNAL_SIZE_LIMIT24: {
132476 Pager *pPager = sqlite3BtreePager(pDb->pBt);
132477 i64 iLimit = -2;
132478 if( zRight ){
132479 sqlite3DecOrHexToI64(zRight, &iLimit);
132480 if( iLimit<-1 ) iLimit = -1;
132481 }
132482 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
132483 returnSingleInt(v, iLimit);
132484 break;
132485 }
132486
132487#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
132488
132489 /*
132490 ** PRAGMA [schema.]auto_vacuum
132491 ** PRAGMA [schema.]auto_vacuum=N
132492 **
132493 ** Get or set the value of the database 'auto-vacuum' parameter.
132494 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
132495 */
132496#ifndef SQLITE_OMIT_AUTOVACUUM
132497 case PragTyp_AUTO_VACUUM3: {
132498 Btree *pBt = pDb->pBt;
132499 assert( pBt!=0 )((void) (0));
132500 if( !zRight ){
132501 returnSingleInt(v, sqlite3BtreeGetAutoVacuum(pBt));
132502 }else{
132503 int eAuto = getAutoVacuum(zRight);
132504 assert( eAuto>=0 && eAuto<=2 )((void) (0));
132505 db->nextAutovac = (u8)eAuto;
132506 /* Call SetAutoVacuum() to set initialize the internal auto and
132507 ** incr-vacuum flags. This is required in case this connection
132508 ** creates the database file. It is important that it is created
132509 ** as an auto-vacuum capable db.
132510 */
132511 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
132512 if( rc==SQLITE_OK0 && (eAuto==1 || eAuto==2) ){
132513 /* When setting the auto_vacuum mode to either "full" or
132514 ** "incremental", write the value of meta[6] in the database
132515 ** file. Before writing to meta[6], check that meta[3] indicates
132516 ** that this really is an auto-vacuum capable database.
132517 */
132518 static const int iLn = VDBE_OFFSET_LINENO(2)0;
132519 static const VdbeOpList setMeta6[] = {
132520 { OP_Transaction2, 0, 1, 0}, /* 0 */
132521 { OP_ReadCookie99, 0, 1, BTREE_LARGEST_ROOT_PAGE4},
132522 { OP_If15, 1, 0, 0}, /* 2 */
132523 { OP_Halt70, SQLITE_OK0, OE_Abort2, 0}, /* 3 */
132524 { OP_SetCookie100, 0, BTREE_INCR_VACUUM7, 0}, /* 4 */
132525 };
132526 VdbeOp *aOp;
132527 int iAddr = sqlite3VdbeCurrentAddr(v);
132528 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6));
132529 aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6)((int)(sizeof(setMeta6)/sizeof(setMeta6[0]))), setMeta6, iLn);
132530 if( ONLY_IF_REALLOC_STRESS(aOp==0)(0) ) break;
132531 aOp[0].p1 = iDb;
132532 aOp[1].p1 = iDb;
132533 aOp[2].p2 = iAddr+4;
132534 aOp[4].p1 = iDb;
132535 aOp[4].p3 = eAuto - 1;
132536 sqlite3VdbeUsesBtree(v, iDb);
132537 }
132538 }
132539 break;
132540 }
132541#endif
132542
132543 /*
132544 ** PRAGMA [schema.]incremental_vacuum(N)
132545 **
132546 ** Do N steps of incremental vacuuming on a database.
132547 */
132548#ifndef SQLITE_OMIT_AUTOVACUUM
132549 case PragTyp_INCREMENTAL_VACUUM19: {
132550 int iLimit = 0, addr;
132551 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
132552 iLimit = 0x7fffffff;
132553 }
132554 sqlite3BeginWriteOperation(pParse, 0, iDb);
132555 sqlite3VdbeAddOp2(v, OP_Integer71, iLimit, 1);
132556 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum61, iDb); VdbeCoverage(v);
132557 sqlite3VdbeAddOp1(v, OP_ResultRow84, 1);
132558 sqlite3VdbeAddOp2(v, OP_AddImm86, 1, -1);
132559 sqlite3VdbeAddOp2(v, OP_IfPos49, 1, addr); VdbeCoverage(v);
132560 sqlite3VdbeJumpHere(v, addr);
132561 break;
132562 }
132563#endif
132564
132565#ifndef SQLITE_OMIT_PAGER_PRAGMAS
132566 /*
132567 ** PRAGMA [schema.]cache_size
132568 ** PRAGMA [schema.]cache_size=N
132569 **
132570 ** The first form reports the current local setting for the
132571 ** page cache size. The second form sets the local
132572 ** page cache size value. If N is positive then that is the
132573 ** number of pages in the cache. If N is negative, then the
132574 ** number of pages is adjusted so that the cache uses -N kibibytes
132575 ** of memory.
132576 */
132577 case PragTyp_CACHE_SIZE6: {
132578 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
132579 if( !zRight ){
132580 returnSingleInt(v, pDb->pSchema->cache_size);
132581 }else{
132582 int size = sqlite3Atoi(zRight);
132583 pDb->pSchema->cache_size = size;
132584 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
132585 }
132586 break;
132587 }
132588
132589 /*
132590 ** PRAGMA [schema.]cache_spill
132591 ** PRAGMA cache_spill=BOOLEAN
132592 ** PRAGMA [schema.]cache_spill=N
132593 **
132594 ** The first form reports the current local setting for the
132595 ** page cache spill size. The second form turns cache spill on
132596 ** or off. When turnning cache spill on, the size is set to the
132597 ** current cache_size. The third form sets a spill size that
132598 ** may be different form the cache size.
132599 ** If N is positive then that is the
132600 ** number of pages in the cache. If N is negative, then the
132601 ** number of pages is adjusted so that the cache uses -N kibibytes
132602 ** of memory.
132603 **
132604 ** If the number of cache_spill pages is less then the number of
132605 ** cache_size pages, no spilling occurs until the page count exceeds
132606 ** the number of cache_size pages.
132607 **
132608 ** The cache_spill=BOOLEAN setting applies to all attached schemas,
132609 ** not just the schema specified.
132610 */
132611 case PragTyp_CACHE_SPILL7: {
132612 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
132613 if( !zRight ){
132614 returnSingleInt(v,
132615 (db->flags & SQLITE_CacheSpill0x00000020)==0 ? 0 :
132616 sqlite3BtreeSetSpillSize(pDb->pBt,0));
132617 }else{
132618 int size = 1;
132619 if( sqlite3GetInt32(zRight, &size) ){
132620 sqlite3BtreeSetSpillSize(pDb->pBt, size);
132621 }
132622 if( sqlite3GetBoolean(zRight, size!=0) ){
132623 db->flags |= SQLITE_CacheSpill0x00000020;
132624 }else{
132625 db->flags &= ~(u64)SQLITE_CacheSpill0x00000020;
132626 }
132627 setAllPagerFlags(db);
132628 }
132629 break;
132630 }
132631
132632 /*
132633 ** PRAGMA [schema.]mmap_size(N)
132634 **
132635 ** Used to set mapping size limit. The mapping size limit is
132636 ** used to limit the aggregate size of all memory mapped regions of the
132637 ** database file. If this parameter is set to zero, then memory mapping
132638 ** is not used at all. If N is negative, then the default memory map
132639 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
132640 ** The parameter N is measured in bytes.
132641 **
132642 ** This value is advisory. The underlying VFS is free to memory map
132643 ** as little or as much as it wants. Except, if N is set to 0 then the
132644 ** upper layers will never invoke the xFetch interfaces to the VFS.
132645 */
132646 case PragTyp_MMAP_SIZE28: {
132647 sqlite3_int64 sz;
132648#if SQLITE_MAX_MMAP_SIZE20971520>0
132649 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
132650 if( zRight ){
132651 int ii;
132652 sqlite3DecOrHexToI64(zRight, &sz);
132653 if( sz<0 ) sz = sqlite3GlobalConfigsqlite3Config.szMmap;
132654 if( pId2->n==0 ) db->szMmap = sz;
132655 for(ii=db->nDb-1; ii>=0; ii--){
132656 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
132657 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
132658 }
132659 }
132660 }
132661 sz = -1;
132662 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE18, &sz);
132663#else
132664 sz = 0;
132665 rc = SQLITE_OK0;
132666#endif
132667 if( rc==SQLITE_OK0 ){
132668 returnSingleInt(v, sz);
132669 }else if( rc!=SQLITE_NOTFOUND12 ){
132670 pParse->nErr++;
132671 pParse->rc = rc;
132672 }
132673 break;
132674 }
132675
132676 /*
132677 ** PRAGMA temp_store
132678 ** PRAGMA temp_store = "default"|"memory"|"file"
132679 **
132680 ** Return or set the local value of the temp_store flag. Changing
132681 ** the local value does not make changes to the disk file and the default
132682 ** value will be restored the next time the database is opened.
132683 **
132684 ** Note that it is possible for the library compile-time options to
132685 ** override this setting
132686 */
132687 case PragTyp_TEMP_STORE39: {
132688 if( !zRight ){
132689 returnSingleInt(v, db->temp_store);
132690 }else{
132691 changeTempStorage(pParse, zRight);
132692 }
132693 break;
132694 }
132695
132696 /*
132697 ** PRAGMA temp_store_directory
132698 ** PRAGMA temp_store_directory = ""|"directory_name"
132699 **
132700 ** Return or set the local value of the temp_store_directory flag. Changing
132701 ** the value sets a specific directory to be used for temporary files.
132702 ** Setting to a null string reverts to the default temporary directory search.
132703 ** If temporary directory is changed, then invalidateTempStorage.
132704 **
132705 */
132706 case PragTyp_TEMP_STORE_DIRECTORY40: {
132707 if( !zRight ){
132708 returnSingleText(v, sqlite3_temp_directory);
132709 }else{
132710#ifndef SQLITE_OMIT_WSD
132711 if( zRight[0] ){
132712 int res;
132713 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE1, &res);
132714 if( rc!=SQLITE_OK0 || res==0 ){
132715 sqlite3ErrorMsg(pParse, "not a writable directory");
132716 goto pragma_out;
132717 }
132718 }
132719 if( SQLITE_TEMP_STORE1==0
132720 || (SQLITE_TEMP_STORE1==1 && db->temp_store<=1)
132721 || (SQLITE_TEMP_STORE1==2 && db->temp_store==1)
132722 ){
132723 invalidateTempStorage(pParse);
132724 }
132725 sqlite3_free(sqlite3_temp_directory);
132726 if( zRight[0] ){
132727 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
132728 }else{
132729 sqlite3_temp_directory = 0;
132730 }
132731#endif /* SQLITE_OMIT_WSD */
132732 }
132733 break;
132734 }
132735
132736#if SQLITE_OS_WIN0
132737 /*
132738 ** PRAGMA data_store_directory
132739 ** PRAGMA data_store_directory = ""|"directory_name"
132740 **
132741 ** Return or set the local value of the data_store_directory flag. Changing
132742 ** the value sets a specific directory to be used for database files that
132743 ** were specified with a relative pathname. Setting to a null string reverts
132744 ** to the default database directory, which for database files specified with
132745 ** a relative path will probably be based on the current directory for the
132746 ** process. Database file specified with an absolute path are not impacted
132747 ** by this setting, regardless of its value.
132748 **
132749 */
132750 case PragTyp_DATA_STORE_DIRECTORY11: {
132751 if( !zRight ){
132752 returnSingleText(v, sqlite3_data_directory);
132753 }else{
132754#ifndef SQLITE_OMIT_WSD
132755 if( zRight[0] ){
132756 int res;
132757 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE1, &res);
132758 if( rc!=SQLITE_OK0 || res==0 ){
132759 sqlite3ErrorMsg(pParse, "not a writable directory");
132760 goto pragma_out;
132761 }
132762 }
132763 sqlite3_free(sqlite3_data_directory);
132764 if( zRight[0] ){
132765 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
132766 }else{
132767 sqlite3_data_directory = 0;
132768 }
132769#endif /* SQLITE_OMIT_WSD */
132770 }
132771 break;
132772 }
132773#endif
132774
132775#if SQLITE_ENABLE_LOCKING_STYLE0
132776 /*
132777 ** PRAGMA [schema.]lock_proxy_file
132778 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
132779 **
132780 ** Return or set the value of the lock_proxy_file flag. Changing
132781 ** the value sets a specific file to be used for database access locks.
132782 **
132783 */
132784 case PragTyp_LOCK_PROXY_FILE25: {
132785 if( !zRight ){
132786 Pager *pPager = sqlite3BtreePager(pDb->pBt);
132787 char *proxy_file_path = NULL((void*)0);
132788 sqlite3_file *pFile = sqlite3PagerFile(pPager);
132789 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE2,
132790 &proxy_file_path);
132791 returnSingleText(v, proxy_file_path);
132792 }else{
132793 Pager *pPager = sqlite3BtreePager(pDb->pBt);
132794 sqlite3_file *pFile = sqlite3PagerFile(pPager);
132795 int res;
132796 if( zRight[0] ){
132797 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE3,
132798 zRight);
132799 } else {
132800 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE3,
132801 NULL((void*)0));
132802 }
132803 if( res!=SQLITE_OK0 ){
132804 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
132805 goto pragma_out;
132806 }
132807 }
132808 break;
132809 }
132810#endif /* SQLITE_ENABLE_LOCKING_STYLE */
132811
132812 /*
132813 ** PRAGMA [schema.]synchronous
132814 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
132815 **
132816 ** Return or set the local value of the synchronous flag. Changing
132817 ** the local value does not make changes to the disk file and the
132818 ** default value will be restored the next time the database is
132819 ** opened.
132820 */
132821 case PragTyp_SYNCHRONOUS36: {
132822 if( !zRight ){
132823 returnSingleInt(v, pDb->safety_level-1);
132824 }else{
132825 if( !db->autoCommit ){
132826 sqlite3ErrorMsg(pParse,
132827 "Safety level may not be changed inside a transaction");
132828 }else if( iDb!=1 ){
132829 int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK0x07;
132830 if( iLevel==0 ) iLevel = 1;
132831 pDb->safety_level = iLevel;
132832 pDb->bSyncSet = 1;
132833 setAllPagerFlags(db);
132834 }
132835 }
132836 break;
132837 }
132838#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
132839
132840#ifndef SQLITE_OMIT_FLAG_PRAGMAS
132841 case PragTyp_FLAG4: {
132842 if( zRight==0 ){
132843 setPragmaResultColumnNames(v, pPragma);
132844 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
132845 }else{
132846 u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */
132847 if( db->autoCommit==0 ){
132848 /* Foreign key support may not be enabled or disabled while not
132849 ** in auto-commit mode. */
132850 mask &= ~(SQLITE_ForeignKeys0x00004000);
132851 }
132852#if SQLITE_USER_AUTHENTICATION
132853 if( db->auth.authLevel==UAUTH_User ){
132854 /* Do not allow non-admin users to modify the schema arbitrarily */
132855 mask &= ~(SQLITE_WriteSchema0x00000001);
132856 }
132857#endif
132858
132859 if( sqlite3GetBoolean(zRight, 0) ){
132860 db->flags |= mask;
132861 }else{
132862 db->flags &= ~mask;
132863 if( mask==SQLITE_DeferFKs0x00080000 ) db->nDeferredImmCons = 0;
132864 if( (mask & SQLITE_WriteSchema0x00000001)!=0
132865 && sqlite3_stricmp(zRight, "reset")==0
132866 ){
132867 /* IMP: R-60817-01178 If the argument is "RESET" then schema
132868 ** writing is disabled (as with "PRAGMA writable_schema=OFF") and,
132869 ** in addition, the schema is reloaded. */
132870 sqlite3ResetAllSchemasOfConnection(db);
132871 }
132872 }
132873
132874 /* Many of the flag-pragmas modify the code generated by the SQL
132875 ** compiler (eg. count_changes). So add an opcode to expire all
132876 ** compiled SQL statements after modifying a pragma value.
132877 */
132878 sqlite3VdbeAddOp0(v, OP_Expire166);
132879 setAllPagerFlags(db);
132880 }
132881 break;
132882 }
132883#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
132884
132885#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
132886 /*
132887 ** PRAGMA table_info(<table>)
132888 **
132889 ** Return a single row for each column of the named table. The columns of
132890 ** the returned data set are:
132891 **
132892 ** cid: Column id (numbered from left to right, starting at 0)
132893 ** name: Column name
132894 ** type: Column declaration type.
132895 ** notnull: True if 'NOT NULL' is part of column declaration
132896 ** dflt_value: The default value for the column, if any.
132897 ** pk: Non-zero for PK fields.
132898 */
132899 case PragTyp_TABLE_INFO37: if( zRight ){
132900 Table *pTab;
132901 sqlite3CodeVerifyNamedSchema(pParse, zDb);
132902 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR0x02, zRight, zDb);
132903 if( pTab ){
132904 int i, k;
132905 int nHidden = 0;
132906 Column *pCol;
132907 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
132908 pParse->nMem = 7;
132909 sqlite3ViewGetColumnNames(pParse, pTab);
132910 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
132911 int isHidden = 0;
132912 const Expr *pColExpr;
132913 if( pCol->colFlags & COLFLAG_NOINSERT0x0062 ){
132914 if( pPragma->iArg==0 ){
132915 nHidden++;
132916 continue;
132917 }
132918 if( pCol->colFlags & COLFLAG_VIRTUAL0x0020 ){
132919 isHidden = 2; /* GENERATED ALWAYS AS ... VIRTUAL */
132920 }else if( pCol->colFlags & COLFLAG_STORED0x0040 ){
132921 isHidden = 3; /* GENERATED ALWAYS AS ... STORED */
132922 }else{ assert( pCol->colFlags & COLFLAG_HIDDEN )((void) (0));
132923 isHidden = 1; /* HIDDEN */
132924 }
132925 }
132926 if( (pCol->colFlags & COLFLAG_PRIMKEY0x0001)==0 ){
132927 k = 0;
132928 }else if( pPk==0 ){
132929 k = 1;
132930 }else{
132931 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
132932 }
132933 pColExpr = sqlite3ColumnExpr(pTab,pCol);
132934 assert( pColExpr==0 || pColExpr->op==TK_SPAN || isHidden>=2 )((void) (0));
132935 assert( pColExpr==0 || !ExprHasProperty(pColExpr, EP_IntValue)((void) (0))
132936 || isHidden>=2 )((void) (0));
132937 sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
132938 i-nHidden,
132939 pCol->zCnName,
132940 sqlite3ColumnType(pCol,""),
132941 pCol->notNull ? 1 : 0,
132942 (isHidden>=2 || pColExpr==0) ? 0 : pColExpr->u.zToken,
132943 k,
132944 isHidden);
132945 }
132946 }
132947 }
132948 break;
132949
132950 /*
132951 ** PRAGMA table_list
132952 **
132953 ** Return a single row for each table, virtual table, or view in the
132954 ** entire schema.
132955 **
132956 ** schema: Name of attached database hold this table
132957 ** name: Name of the table itself
132958 ** type: "table", "view", "virtual", "shadow"
132959 ** ncol: Number of columns
132960 ** wr: True for a WITHOUT ROWID table
132961 ** strict: True for a STRICT table
132962 */
132963 case PragTyp_TABLE_LIST38: {
132964 int ii;
132965 pParse->nMem = 6;
132966 sqlite3CodeVerifyNamedSchema(pParse, zDb);
132967 for(ii=0; ii<db->nDb; ii++){
132968 HashElem *k;
132969 Hash *pHash;
132970 int initNCol;
132971 if( zDb && sqlite3_stricmp(zDb, db->aDb[ii].zDbSName)!=0 ) continue;
132972
132973 /* Ensure that the Table.nCol field is initialized for all views
132974 ** and virtual tables. Each time we initialize a Table.nCol value
132975 ** for a table, that can potentially disrupt the hash table, so restart
132976 ** the initialization scan.
132977 */
132978 pHash = &db->aDb[ii].pSchema->tblHash;
132979 initNCol = sqliteHashCount(pHash)((pHash)->count);
132980 while( initNCol-- ){
132981 for(k=sqliteHashFirst(pHash)((pHash)->first); 1; k=sqliteHashNext(k)((k)->next) ){
132982 Table *pTab;
132983 if( k==0 ){ initNCol = 0; break; }
132984 pTab = sqliteHashData(k)((k)->data);
132985 if( pTab->nCol==0 ){
132986 char *zSql = sqlite3MPrintf(db, "SELECT*FROM\"%w\"", pTab->zName);
132987 if( zSql ){
132988 sqlite3_stmt *pDummy = 0;
132989 (void)sqlite3_prepare(db, zSql, -1, &pDummy, 0);
132990 (void)sqlite3_finalize(pDummy);
132991 sqlite3DbFree(db, zSql);
132992 }
132993 if( db->mallocFailed ){
132994 sqlite3ErrorMsg(db->pParse, "out of memory");
132995 db->pParse->rc = SQLITE_NOMEM_BKPT7;
132996 }
132997 pHash = &db->aDb[ii].pSchema->tblHash;
132998 break;
132999 }
133000 }
133001 }
133002
133003 for(k=sqliteHashFirst(pHash)((pHash)->first); k; k=sqliteHashNext(k)((k)->next) ){
133004 Table *pTab = sqliteHashData(k)((k)->data);
133005 const char *zType;
133006 if( zRight && sqlite3_stricmp(zRight, pTab->zName)!=0 ) continue;
133007 if( IsView(pTab)((pTab)->eTabType==2) ){
133008 zType = "view";
133009 }else if( IsVirtual(pTab)((pTab)->eTabType==1) ){
133010 zType = "virtual";
133011 }else if( pTab->tabFlags & TF_Shadow0x00001000 ){
133012 zType = "shadow";
133013 }else{
133014 zType = "table";
133015 }
133016 sqlite3VdbeMultiLoad(v, 1, "sssiii",
133017 db->aDb[ii].zDbSName,
133018 sqlite3PreferredTableName(pTab->zName),
133019 zType,
133020 pTab->nCol,
133021 (pTab->tabFlags & TF_WithoutRowid0x00000080)!=0,
133022 (pTab->tabFlags & TF_Strict0x00010000)!=0
133023 );
133024 }
133025 }
133026 }
133027 break;
133028
133029#ifdef SQLITE_DEBUG
133030 case PragTyp_STATS45: {
133031 Index *pIdx;
133032 HashElem *i;
133033 pParse->nMem = 5;
133034 sqlite3CodeVerifySchema(pParse, iDb);
133035 for(i=sqliteHashFirst(&pDb->pSchema->tblHash)((&pDb->pSchema->tblHash)->first); i; i=sqliteHashNext(i)((i)->next)){
133036 Table *pTab = sqliteHashData(i)((i)->data);
133037 sqlite3VdbeMultiLoad(v, 1, "ssiii",
133038 sqlite3PreferredTableName(pTab->zName),
133039 0,
133040 pTab->szTabRow,
133041 pTab->nRowLogEst,
133042 pTab->tabFlags);
133043 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
133044 sqlite3VdbeMultiLoad(v, 2, "siiiX",
133045 pIdx->zName,
133046 pIdx->szIdxRow,
133047 pIdx->aiRowLogEst[0],
133048 pIdx->hasStat1);
133049 sqlite3VdbeAddOp2(v, OP_ResultRow84, 1, 5);
133050 }
133051 }
133052 }
133053 break;
133054#endif
133055
133056 case PragTyp_INDEX_INFO20: if( zRight ){
133057 Index *pIdx;
133058 Table *pTab;
133059 pIdx = sqlite3FindIndex(db, zRight, zDb);
133060 if( pIdx==0 ){
133061 /* If there is no index named zRight, check to see if there is a
133062 ** WITHOUT ROWID table named zRight, and if there is, show the
133063 ** structure of the PRIMARY KEY index for that table. */
133064 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR0x02, zRight, zDb);
133065 if( pTab && !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
133066 pIdx = sqlite3PrimaryKeyIndex(pTab);
133067 }
133068 }
133069 if( pIdx ){
133070 int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
133071 int i;
133072 int mx;
133073 if( pPragma->iArg ){
133074 /* PRAGMA index_xinfo (newer version with more rows and columns) */
133075 mx = pIdx->nColumn;
133076 pParse->nMem = 6;
133077 }else{
133078 /* PRAGMA index_info (legacy version) */
133079 mx = pIdx->nKeyCol;
133080 pParse->nMem = 3;
133081 }
133082 pTab = pIdx->pTable;
133083 sqlite3CodeVerifySchema(pParse, iIdxDb);
133084 assert( pParse->nMem<=pPragma->nPragCName )((void) (0));
133085 for(i=0; i<mx; i++){
133086 i16 cnum = pIdx->aiColumn[i];
133087 sqlite3VdbeMultiLoad(v, 1, "iisX", i, cnum,
133088 cnum<0 ? 0 : pTab->aCol[cnum].zCnName);
133089 if( pPragma->iArg ){
133090 sqlite3VdbeMultiLoad(v, 4, "isiX",
133091 pIdx->aSortOrder[i],
133092 pIdx->azColl[i],
133093 i<pIdx->nKeyCol);
133094 }
133095 sqlite3VdbeAddOp2(v, OP_ResultRow84, 1, pParse->nMem);
133096 }
133097 }
133098 }
133099 break;
133100
133101 case PragTyp_INDEX_LIST21: if( zRight ){
133102 Index *pIdx;
133103 Table *pTab;
133104 int i;
133105 pTab = sqlite3FindTable(db, zRight, zDb);
133106 if( pTab ){
133107 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
133108 pParse->nMem = 5;
133109 sqlite3CodeVerifySchema(pParse, iTabDb);
133110 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
133111 const char *azOrigin[] = { "c", "u", "pk" };
133112 sqlite3VdbeMultiLoad(v, 1, "isisi",
133113 i,
133114 pIdx->zName,
133115 IsUniqueIndex(pIdx)((pIdx)->onError!=0),
133116 azOrigin[pIdx->idxType],
133117 pIdx->pPartIdxWhere!=0);
133118 }
133119 }
133120 }
133121 break;
133122
133123 case PragTyp_DATABASE_LIST12: {
133124 int i;
133125 pParse->nMem = 3;
133126 for(i=0; i<db->nDb; i++){
133127 if( db->aDb[i].pBt==0 ) continue;
133128 assert( db->aDb[i].zDbSName!=0 )((void) (0));
133129 sqlite3VdbeMultiLoad(v, 1, "iss",
133130 i,
133131 db->aDb[i].zDbSName,
133132 sqlite3BtreeGetFilename(db->aDb[i].pBt));
133133 }
133134 }
133135 break;
133136
133137 case PragTyp_COLLATION_LIST9: {
133138 int i = 0;
133139 HashElem *p;
133140 pParse->nMem = 2;
133141 for(p=sqliteHashFirst(&db->aCollSeq)((&db->aCollSeq)->first); p; p=sqliteHashNext(p)((p)->next)){
133142 CollSeq *pColl = (CollSeq *)sqliteHashData(p)((p)->data);
133143 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
133144 }
133145 }
133146 break;
133147
133148#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
133149 case PragTyp_FUNCTION_LIST17: {
133150 int i;
133151 HashElem *j;
133152 FuncDef *p;
133153 int showInternFunc = (db->mDbFlags & DBFLAG_InternalFunc0x0020)!=0;
133154 pParse->nMem = 6;
133155 for(i=0; i<SQLITE_FUNC_HASH_SZ23; i++){
133156 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
133157 assert( p->funcFlags & SQLITE_FUNC_BUILTIN )((void) (0));
133158 pragmaFunclistLine(v, p, 1, showInternFunc);
133159 }
133160 }
133161 for(j=sqliteHashFirst(&db->aFunc)((&db->aFunc)->first); j; j=sqliteHashNext(j)((j)->next)){
133162 p = (FuncDef*)sqliteHashData(j)((j)->data);
133163 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 )((void) (0));
133164 pragmaFunclistLine(v, p, 0, showInternFunc);
133165 }
133166 }
133167 break;
133168
133169#ifndef SQLITE_OMIT_VIRTUALTABLE
133170 case PragTyp_MODULE_LIST29: {
133171 HashElem *j;
133172 pParse->nMem = 1;
133173 for(j=sqliteHashFirst(&db->aModule)((&db->aModule)->first); j; j=sqliteHashNext(j)((j)->next)){
133174 Module *pMod = (Module*)sqliteHashData(j)((j)->data);
133175 sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
133176 }
133177 }
133178 break;
133179#endif /* SQLITE_OMIT_VIRTUALTABLE */
133180
133181 case PragTyp_PRAGMA_LIST32: {
133182 int i;
133183 for(i=0; i<ArraySize(aPragmaName)((int)(sizeof(aPragmaName)/sizeof(aPragmaName[0]))); i++){
133184 sqlite3VdbeMultiLoad(v, 1, "s", aPragmaName[i].zName);
133185 }
133186 }
133187 break;
133188#endif /* SQLITE_INTROSPECTION_PRAGMAS */
133189
133190#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
133191
133192#ifndef SQLITE_OMIT_FOREIGN_KEY
133193 case PragTyp_FOREIGN_KEY_LIST16: if( zRight ){
133194 FKey *pFK;
133195 Table *pTab;
133196 pTab = sqlite3FindTable(db, zRight, zDb);
133197 if( pTab && IsOrdinaryTable(pTab)((pTab)->eTabType==0) ){
133198 pFK = pTab->u.tab.pFKey;
133199 if( pFK ){
133200 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
133201 int i = 0;
133202 pParse->nMem = 8;
133203 sqlite3CodeVerifySchema(pParse, iTabDb);
133204 while(pFK){
133205 int j;
133206 for(j=0; j<pFK->nCol; j++){
133207 sqlite3VdbeMultiLoad(v, 1, "iissssss",
133208 i,
133209 j,
133210 pFK->zTo,
133211 pTab->aCol[pFK->aCol[j].iFrom].zCnName,
133212 pFK->aCol[j].zCol,
133213 actionName(pFK->aAction[1]), /* ON UPDATE */
133214 actionName(pFK->aAction[0]), /* ON DELETE */
133215 "NONE");
133216 }
133217 ++i;
133218 pFK = pFK->pNextFrom;
133219 }
133220 }
133221 }
133222 }
133223 break;
133224#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
133225
133226#ifndef SQLITE_OMIT_FOREIGN_KEY
133227#ifndef SQLITE_OMIT_TRIGGER
133228 case PragTyp_FOREIGN_KEY_CHECK15: {
133229 FKey *pFK; /* A foreign key constraint */
133230 Table *pTab; /* Child table contain "REFERENCES" keyword */
133231 Table *pParent; /* Parent table that child points to */
133232 Index *pIdx; /* Index in the parent table */
133233 int i; /* Loop counter: Foreign key number for pTab */
133234 int j; /* Loop counter: Field of the foreign key */
133235 HashElem *k; /* Loop counter: Next table in schema */
133236 int x; /* result variable */
133237 int regResult; /* 3 registers to hold a result row */
133238 int regRow; /* Registers to hold a row from pTab */
133239 int addrTop; /* Top of a loop checking foreign keys */
133240 int addrOk; /* Jump here if the key is OK */
133241 int *aiCols; /* child to parent column mapping */
133242
133243 regResult = pParse->nMem+1;
133244 pParse->nMem += 4;
133245 regRow = ++pParse->nMem;
133246 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash)((&db->aDb[iDb].pSchema->tblHash)->first);
133247 while( k ){
133248 if( zRight ){
133249 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
133250 k = 0;
133251 }else{
133252 pTab = (Table*)sqliteHashData(k)((k)->data);
133253 k = sqliteHashNext(k)((k)->next);
133254 }
133255 if( pTab==0 || !IsOrdinaryTable(pTab)((pTab)->eTabType==0) || pTab->u.tab.pFKey==0 ) continue;
133256 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
133257 zDb = db->aDb[iDb].zDbSName;
133258 sqlite3CodeVerifySchema(pParse, iDb);
133259 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
133260 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
133261 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead112);
133262 sqlite3VdbeLoadString(v, regResult, pTab->zName);
133263 assert( IsOrdinaryTable(pTab) )((void) (0));
133264 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
133265 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
133266 if( pParent==0 ) continue;
133267 pIdx = 0;
133268 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
133269 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
133270 if( x==0 ){
133271 if( pIdx==0 ){
133272 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead112);
133273 }else{
133274 sqlite3VdbeAddOp3(v, OP_OpenRead112, i, pIdx->tnum, iDb);
133275 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
133276 }
133277 }else{
133278 k = 0;
133279 break;
133280 }
133281 }
133282 assert( pParse->nErr>0 || pFK==0 )((void) (0));
133283 if( pFK ) break;
133284 if( pParse->nTab<i ) pParse->nTab = i;
133285 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind35, 0); VdbeCoverage(v);
133286 assert( IsOrdinaryTable(pTab) )((void) (0));
133287 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
133288 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
133289 pIdx = 0;
133290 aiCols = 0;
133291 if( pParent ){
133292 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
133293 assert( x==0 || db->mallocFailed )((void) (0));
133294 }
133295 addrOk = sqlite3VdbeMakeLabel(pParse);
133296
133297 /* Generate code to read the child key values into registers
133298 ** regRow..regRow+n. If any of the child key values are NULL, this
133299 ** row cannot cause an FK violation. Jump directly to addrOk in
133300 ** this case. */
133301 if( regRow+pFK->nCol>pParse->nMem ) pParse->nMem = regRow+pFK->nCol;
133302 for(j=0; j<pFK->nCol; j++){
133303 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
133304 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0, iCol, regRow+j);
133305 sqlite3VdbeAddOp2(v, OP_IsNull50, regRow+j, addrOk); VdbeCoverage(v);
133306 }
133307
133308 /* Generate code to query the parent index for a matching parent
133309 ** key. If a match is found, jump to addrOk. */
133310 if( pIdx ){
133311 sqlite3VdbeAddOp4(v, OP_Affinity96, regRow, pFK->nCol, 0,
133312 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
133313 sqlite3VdbeAddOp4Int(v, OP_Found28, i, addrOk, regRow, pFK->nCol);
133314 VdbeCoverage(v);
133315 }else if( pParent ){
133316 int jmp = sqlite3VdbeCurrentAddr(v)+2;
133317 sqlite3VdbeAddOp3(v, OP_SeekRowid29, i, jmp, regRow); VdbeCoverage(v);
133318 sqlite3VdbeGoto(v, addrOk);
133319 assert( pFK->nCol==1 || db->mallocFailed )((void) (0));
133320 }
133321
133322 /* Generate code to report an FK violation to the caller. */
133323 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
133324 sqlite3VdbeAddOp2(v, OP_Rowid135, 0, regResult+1);
133325 }else{
133326 sqlite3VdbeAddOp2(v, OP_Null75, 0, regResult+1);
133327 }
133328 sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
133329 sqlite3VdbeAddOp2(v, OP_ResultRow84, regResult, 4);
133330 sqlite3VdbeResolveLabel(v, addrOk);
133331 sqlite3DbFree(db, aiCols);
133332 }
133333 sqlite3VdbeAddOp2(v, OP_Next38, 0, addrTop+1); VdbeCoverage(v);
133334 sqlite3VdbeJumpHere(v, addrTop);
133335 }
133336 }
133337 break;
133338#endif /* !defined(SQLITE_OMIT_TRIGGER) */
133339#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
133340
133341#ifndef SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA
133342 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
133343 ** used will be case sensitive or not depending on the RHS.
133344 */
133345 case PragTyp_CASE_SENSITIVE_LIKE8: {
133346 if( zRight ){
133347 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
133348 }
133349 }
133350 break;
133351#endif /* SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA */
133352
133353#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX100
133354# define SQLITE_INTEGRITY_CHECK_ERROR_MAX100 100
133355#endif
133356
133357#ifndef SQLITE_OMIT_INTEGRITY_CHECK
133358 /* PRAGMA integrity_check
133359 ** PRAGMA integrity_check(N)
133360 ** PRAGMA quick_check
133361 ** PRAGMA quick_check(N)
133362 **
133363 ** Verify the integrity of the database.
133364 **
133365 ** The "quick_check" is reduced version of
133366 ** integrity_check designed to detect most database corruption
133367 ** without the overhead of cross-checking indexes. Quick_check
133368 ** is linear time wherease integrity_check is O(NlogN).
133369 **
133370 ** The maximum nubmer of errors is 100 by default. A different default
133371 ** can be specified using a numeric parameter N.
133372 **
133373 ** Or, the parameter N can be the name of a table. In that case, only
133374 ** the one table named is verified. The freelist is only verified if
133375 ** the named table is "sqlite_schema" (or one of its aliases).
133376 **
133377 ** All schemas are checked by default. To check just a single
133378 ** schema, use the form:
133379 **
133380 ** PRAGMA schema.integrity_check;
133381 */
133382 case PragTyp_INTEGRITY_CHECK22: {
133383 int i, j, addr, mxErr;
133384 Table *pObjTab = 0; /* Check only this one table, if not NULL */
133385
133386 int isQuick = (sqlite3Tolower(zLeft[0])(sqlite3UpperToLower[(unsigned char)(zLeft[0])])=='q');
133387
133388 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
133389 ** then iDb is set to the index of the database identified by <db>.
133390 ** In this case, the integrity of database iDb only is verified by
133391 ** the VDBE created below.
133392 **
133393 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
133394 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
133395 ** to -1 here, to indicate that the VDBE should verify the integrity
133396 ** of all attached databases. */
133397 assert( iDb>=0 )((void) (0));
133398 assert( iDb==0 || pId2->z )((void) (0));
133399 if( pId2->z==0 ) iDb = -1;
133400
133401 /* Initialize the VDBE program */
133402 pParse->nMem = 6;
133403
133404 /* Set the maximum error count */
133405 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX100;
133406 if( zRight ){
133407 if( sqlite3GetInt32(zRight, &mxErr) ){
133408 if( mxErr<=0 ){
133409 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX100;
133410 }
133411 }else{
133412 pObjTab = sqlite3LocateTable(pParse, 0, zRight,
133413 iDb>=0 ? db->aDb[iDb].zDbSName : 0);
133414 }
133415 }
133416 sqlite3VdbeAddOp2(v, OP_Integer71, mxErr-1, 1); /* reg[1] holds errors left */
133417
133418 /* Do an integrity check on each database file */
133419 for(i=0; i<db->nDb; i++){
133420 HashElem *x; /* For looping over tables in the schema */
133421 Hash *pTbls; /* Set of all tables in the schema */
133422 int *aRoot; /* Array of root page numbers of all btrees */
133423 int cnt = 0; /* Number of entries in aRoot[] */
133424 int mxIdx = 0; /* Maximum number of indexes for any table */
133425
133426 if( OMIT_TEMPDB0 && i==1 ) continue;
133427 if( iDb>=0 && i!=iDb ) continue;
133428
133429 sqlite3CodeVerifySchema(pParse, i);
133430
133431 /* Do an integrity check of the B-Tree
133432 **
133433 ** Begin by finding the root pages numbers
133434 ** for all tables and indices in the database.
133435 */
133436 assert( sqlite3SchemaMutexHeld(db, i, 0) )((void) (0));
133437 pTbls = &db->aDb[i].pSchema->tblHash;
133438 for(cnt=0, x=sqliteHashFirst(pTbls)((pTbls)->first); x; x=sqliteHashNext(x)((x)->next)){
133439 Table *pTab = sqliteHashData(x)((x)->data); /* Current table */
133440 Index *pIdx; /* An index on pTab */
133441 int nIdx; /* Number of indexes on pTab */
133442 if( pObjTab && pObjTab!=pTab ) continue;
133443 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ) cnt++;
133444 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
133445 if( nIdx>mxIdx ) mxIdx = nIdx;
133446 }
133447 if( cnt==0 ) continue;
133448 if( pObjTab ) cnt++;
133449 aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1));
133450 if( aRoot==0 ) break;
133451 cnt = 0;
133452 if( pObjTab ) aRoot[++cnt] = 0;
133453 for(x=sqliteHashFirst(pTbls)((pTbls)->first); x; x=sqliteHashNext(x)((x)->next)){
133454 Table *pTab = sqliteHashData(x)((x)->data);
133455 Index *pIdx;
133456 if( pObjTab && pObjTab!=pTab ) continue;
133457 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ) aRoot[++cnt] = pTab->tnum;
133458 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
133459 aRoot[++cnt] = pIdx->tnum;
133460 }
133461 }
133462 aRoot[0] = cnt;
133463
133464 /* Make sure sufficient number of registers have been allocated */
133465 pParse->nMem = MAX( pParse->nMem, 8+mxIdx )((pParse->nMem)>(8+mxIdx)?(pParse->nMem):(8+mxIdx));
133466 sqlite3ClearTempRegCache(pParse);
133467
133468 /* Do the b-tree integrity checks */
133469 sqlite3VdbeAddOp4(v, OP_IntegrityCk155, 2, cnt, 1, (char*)aRoot,P4_INTARRAY(-14));
133470 sqlite3VdbeChangeP5(v, (u8)i);
133471 addr = sqlite3VdbeAddOp1(v, OP_IsNull50, 2); VdbeCoverage(v);
133472 sqlite3VdbeAddOp4(v, OP_String8117, 0, 3, 0,
133473 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
133474 P4_DYNAMIC(-6));
133475 sqlite3VdbeAddOp3(v, OP_Concat111, 2, 3, 3);
133476 integrityCheckResultRow(v);
133477 sqlite3VdbeJumpHere(v, addr);
133478
133479 /* Make sure all the indices are constructed correctly.
133480 */
133481 for(x=sqliteHashFirst(pTbls)((pTbls)->first); x; x=sqliteHashNext(x)((x)->next)){
133482 Table *pTab = sqliteHashData(x)((x)->data);
133483 Index *pIdx, *pPk;
133484 Index *pPrior = 0;
133485 int loopTop;
133486 int iDataCur, iIdxCur;
133487 int r1 = -1;
133488 int bStrict;
133489
133490 if( !IsOrdinaryTable(pTab)((pTab)->eTabType==0) ) continue;
133491 if( pObjTab && pObjTab!=pTab ) continue;
133492 pPk = HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ? 0 : sqlite3PrimaryKeyIndex(pTab);
133493 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead112, 0,
133494 1, 0, &iDataCur, &iIdxCur);
133495 /* reg[7] counts the number of entries in the table.
133496 ** reg[8+i] counts the number of entries in the i-th index
133497 */
133498 sqlite3VdbeAddOp2(v, OP_Integer71, 0, 7);
133499 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
133500 sqlite3VdbeAddOp2(v, OP_Integer71, 0, 8+j); /* index entries counter */
133501 }
133502 assert( pParse->nMem>=8+j )((void) (0));
133503 assert( sqlite3NoTempsInRange(pParse,1,7+j) )((void) (0));
133504 sqlite3VdbeAddOp2(v, OP_Rewind35, iDataCur, 0); VdbeCoverage(v);
133505 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm86, 7, 1);
133506 if( !isQuick ){
133507 /* Sanity check on record header decoding */
133508 sqlite3VdbeAddOp3(v, OP_Column94, iDataCur, pTab->nNVCol-1,3);
133509 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG0x80);
133510 VdbeComment((v, "(right-most column)"));
133511 }
133512 /* Verify that all NOT NULL columns really are NOT NULL. At the
133513 ** same time verify the type of the content of STRICT tables */
133514 bStrict = (pTab->tabFlags & TF_Strict0x00010000)!=0;
133515 for(j=0; j<pTab->nCol; j++){
133516 char *zErr;
133517 Column *pCol = pTab->aCol + j;
133518 int doError, jmp2;
133519 if( j==pTab->iPKey ) continue;
133520 if( pCol->notNull==0 && !bStrict ) continue;
133521 doError = bStrict ? sqlite3VdbeMakeLabel(pParse) : 0;
133522 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
133523 if( sqlite3VdbeGetOp(v,-1)->opcode==OP_Column94 ){
133524 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG0x80);
133525 }
133526 if( pCol->notNull ){
133527 jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull51, 3); VdbeCoverage(v);
133528 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
133529 pCol->zCnName);
133530 sqlite3VdbeAddOp4(v, OP_String8117, 0, 3, 0, zErr, P4_DYNAMIC(-6));
133531 if( bStrict && pCol->eCType!=COLTYPE_ANY1 ){
133532 sqlite3VdbeGoto(v, doError);
133533 }else{
133534 integrityCheckResultRow(v);
133535 }
133536 sqlite3VdbeJumpHere(v, jmp2);
133537 }
133538 if( (pTab->tabFlags & TF_Strict0x00010000)!=0
133539 && pCol->eCType!=COLTYPE_ANY1
133540 ){
133541 jmp2 = sqlite3VdbeAddOp3(v, OP_IsNullOrType17, 3, 0,
133542 sqlite3StdTypeMap[pCol->eCType-1]);
133543 VdbeCoverage(v);
133544 zErr = sqlite3MPrintf(db, "non-%s value in %s.%s",
133545 sqlite3StdType[pCol->eCType-1],
133546 pTab->zName, pTab->aCol[j].zCnName);
133547 sqlite3VdbeAddOp4(v, OP_String8117, 0, 3, 0, zErr, P4_DYNAMIC(-6));
133548 sqlite3VdbeResolveLabel(v, doError);
133549 integrityCheckResultRow(v);
133550 sqlite3VdbeJumpHere(v, jmp2);
133551 }
133552 }
133553 /* Verify CHECK constraints */
133554 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks0x00000200)==0 ){
133555 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
133556 if( db->mallocFailed==0 ){
133557 int addrCkFault = sqlite3VdbeMakeLabel(pParse);
133558 int addrCkOk = sqlite3VdbeMakeLabel(pParse);
133559 char *zErr;
133560 int k;
133561 pParse->iSelfTab = iDataCur + 1;
133562 for(k=pCheck->nExpr-1; k>0; k--){
133563 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
133564 }
133565 sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
133566 SQLITE_JUMPIFNULL0x10);
133567 sqlite3VdbeResolveLabel(v, addrCkFault);
133568 pParse->iSelfTab = 0;
133569 zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s",
133570 pTab->zName);
133571 sqlite3VdbeAddOp4(v, OP_String8117, 0, 3, 0, zErr, P4_DYNAMIC(-6));
133572 integrityCheckResultRow(v);
133573 sqlite3VdbeResolveLabel(v, addrCkOk);
133574 }
133575 sqlite3ExprListDelete(db, pCheck);
133576 }
133577 if( !isQuick ){ /* Omit the remaining tests for quick_check */
133578 /* Validate index entries for the current row */
133579 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
133580 int jmp2, jmp3, jmp4, jmp5;
133581 int ckUniq = sqlite3VdbeMakeLabel(pParse);
133582 if( pPk==pIdx ) continue;
133583 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
133584 pPrior, r1);
133585 pPrior = pIdx;
133586 sqlite3VdbeAddOp2(v, OP_AddImm86, 8+j, 1);/* increment entry count */
133587 /* Verify that an index entry exists for the current table row */
133588 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found28, iIdxCur+j, ckUniq, r1,
133589 pIdx->nColumn); VdbeCoverage(v);
133590 sqlite3VdbeLoadString(v, 3, "row ");
133591 sqlite3VdbeAddOp3(v, OP_Concat111, 7, 3, 3);
133592 sqlite3VdbeLoadString(v, 4, " missing from index ");
133593 sqlite3VdbeAddOp3(v, OP_Concat111, 4, 3, 3);
133594 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
133595 sqlite3VdbeAddOp3(v, OP_Concat111, 4, 3, 3);
133596 jmp4 = integrityCheckResultRow(v);
133597 sqlite3VdbeJumpHere(v, jmp2);
133598 /* For UNIQUE indexes, verify that only one entry exists with the
133599 ** current key. The entry is unique if (1) any column is NULL
133600 ** or (2) the next entry has a different key */
133601 if( IsUniqueIndex(pIdx)((pIdx)->onError!=0) ){
133602 int uniqOk = sqlite3VdbeMakeLabel(pParse);
133603 int jmp6;
133604 int kk;
133605 for(kk=0; kk<pIdx->nKeyCol; kk++){
133606 int iCol = pIdx->aiColumn[kk];
133607 assert( iCol!=XN_ROWID && iCol<pTab->nCol )((void) (0));
133608 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
133609 sqlite3VdbeAddOp2(v, OP_IsNull50, r1+kk, uniqOk);
133610 VdbeCoverage(v);
133611 }
133612 jmp6 = sqlite3VdbeAddOp1(v, OP_Next38, iIdxCur+j); VdbeCoverage(v);
133613 sqlite3VdbeGoto(v, uniqOk);
133614 sqlite3VdbeJumpHere(v, jmp6);
133615 sqlite3VdbeAddOp4Int(v, OP_IdxGT40, iIdxCur+j, uniqOk, r1,
133616 pIdx->nKeyCol); VdbeCoverage(v);
133617 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
133618 sqlite3VdbeGoto(v, jmp5);
133619 sqlite3VdbeResolveLabel(v, uniqOk);
133620 }
133621 sqlite3VdbeJumpHere(v, jmp4);
133622 sqlite3ResolvePartIdxLabel(pParse, jmp3);
133623 }
133624 }
133625 sqlite3VdbeAddOp2(v, OP_Next38, iDataCur, loopTop); VdbeCoverage(v);
133626 sqlite3VdbeJumpHere(v, loopTop-1);
133627 if( !isQuick ){
133628 sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
133629 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
133630 if( pPk==pIdx ) continue;
133631 sqlite3VdbeAddOp2(v, OP_Count98, iIdxCur+j, 3);
133632 addr = sqlite3VdbeAddOp3(v, OP_Eq53, 8+j, 0, 3); VdbeCoverage(v);
133633 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL0x90);
133634 sqlite3VdbeLoadString(v, 4, pIdx->zName);
133635 sqlite3VdbeAddOp3(v, OP_Concat111, 4, 2, 3);
133636 integrityCheckResultRow(v);
133637 sqlite3VdbeJumpHere(v, addr);
133638 }
133639 }
133640 }
133641 }
133642 {
133643 static const int iLn = VDBE_OFFSET_LINENO(2)0;
133644 static const VdbeOpList endCode[] = {
133645 { OP_AddImm86, 1, 0, 0}, /* 0 */
133646 { OP_IfNotZero59, 1, 4, 0}, /* 1 */
133647 { OP_String8117, 0, 3, 0}, /* 2 */
133648 { OP_ResultRow84, 3, 1, 0}, /* 3 */
133649 { OP_Halt70, 0, 0, 0}, /* 4 */
133650 { OP_String8117, 0, 3, 0}, /* 5 */
133651 { OP_Goto8, 0, 3, 0}, /* 6 */
133652 };
133653 VdbeOp *aOp;
133654
133655 aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode)((int)(sizeof(endCode)/sizeof(endCode[0]))), endCode, iLn);
133656 if( aOp ){
133657 aOp[0].p2 = 1-mxErr;
133658 aOp[2].p4type = P4_STATIC(-1);
133659 aOp[2].p4.z = "ok";
133660 aOp[5].p4type = P4_STATIC(-1);
133661 aOp[5].p4.z = (char*)sqlite3ErrStr(SQLITE_CORRUPT11);
133662 }
133663 sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
133664 }
133665 }
133666 break;
133667#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
133668
133669#ifndef SQLITE_OMIT_UTF16
133670 /*
133671 ** PRAGMA encoding
133672 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
133673 **
133674 ** In its first form, this pragma returns the encoding of the main
133675 ** database. If the database is not initialized, it is initialized now.
133676 **
133677 ** The second form of this pragma is a no-op if the main database file
133678 ** has not already been initialized. In this case it sets the default
133679 ** encoding that will be used for the main database file if a new file
133680 ** is created. If an existing main database file is opened, then the
133681 ** default text encoding for the existing database is used.
133682 **
133683 ** In all cases new databases created using the ATTACH command are
133684 ** created to use the same default text encoding as the main database. If
133685 ** the main database has not been initialized and/or created when ATTACH
133686 ** is executed, this is done before the ATTACH operation.
133687 **
133688 ** In the second form this pragma sets the text encoding to be used in
133689 ** new database files created using this database handle. It is only
133690 ** useful if invoked immediately after the main database i
133691 */
133692 case PragTyp_ENCODING14: {
133693 static const struct EncName {
133694 char *zName;
133695 u8 enc;
133696 } encnames[] = {
133697 { "UTF8", SQLITE_UTF81 },
133698 { "UTF-8", SQLITE_UTF81 }, /* Must be element [1] */
133699 { "UTF-16le", SQLITE_UTF16LE2 }, /* Must be element [2] */
133700 { "UTF-16be", SQLITE_UTF16BE3 }, /* Must be element [3] */
133701 { "UTF16le", SQLITE_UTF16LE2 },
133702 { "UTF16be", SQLITE_UTF16BE3 },
133703 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
133704 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
133705 { 0, 0 }
133706 };
133707 const struct EncName *pEnc;
133708 if( !zRight ){ /* "PRAGMA encoding" */
133709 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
133710 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 )((void) (0));
133711 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE )((void) (0));
133712 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE )((void) (0));
133713 returnSingleText(v, encnames[ENC(pParse->db)((pParse->db)->enc)].zName);
133714 }else{ /* "PRAGMA encoding = XXX" */
133715 /* Only change the value of sqlite.enc if the database handle is not
133716 ** initialized. If the main database exists, the new sqlite.enc value
133717 ** will be overwritten when the schema is next loaded. If it does not
133718 ** already exists, it will be created to use the new encoding value.
133719 */
133720 if( (db->mDbFlags & DBFLAG_EncodingFixed0x0040)==0 ){
133721 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
133722 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
133723 u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE2;
133724 SCHEMA_ENC(db)((db)->aDb[0].pSchema->enc) = enc;
133725 sqlite3SetTextEncoding(db, enc);
133726 break;
133727 }
133728 }
133729 if( !pEnc->zName ){
133730 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
133731 }
133732 }
133733 }
133734 }
133735 break;
133736#endif /* SQLITE_OMIT_UTF16 */
133737
133738#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
133739 /*
133740 ** PRAGMA [schema.]schema_version
133741 ** PRAGMA [schema.]schema_version = <integer>
133742 **
133743 ** PRAGMA [schema.]user_version
133744 ** PRAGMA [schema.]user_version = <integer>
133745 **
133746 ** PRAGMA [schema.]freelist_count
133747 **
133748 ** PRAGMA [schema.]data_version
133749 **
133750 ** PRAGMA [schema.]application_id
133751 ** PRAGMA [schema.]application_id = <integer>
133752 **
133753 ** The pragma's schema_version and user_version are used to set or get
133754 ** the value of the schema-version and user-version, respectively. Both
133755 ** the schema-version and the user-version are 32-bit signed integers
133756 ** stored in the database header.
133757 **
133758 ** The schema-cookie is usually only manipulated internally by SQLite. It
133759 ** is incremented by SQLite whenever the database schema is modified (by
133760 ** creating or dropping a table or index). The schema version is used by
133761 ** SQLite each time a query is executed to ensure that the internal cache
133762 ** of the schema used when compiling the SQL query matches the schema of
133763 ** the database against which the compiled query is actually executed.
133764 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
133765 ** the schema-version is potentially dangerous and may lead to program
133766 ** crashes or database corruption. Use with caution!
133767 **
133768 ** The user-version is not used internally by SQLite. It may be used by
133769 ** applications for any purpose.
133770 */
133771 case PragTyp_HEADER_VALUE2: {
133772 int iCookie = pPragma->iArg; /* Which cookie to read or write */
133773 sqlite3VdbeUsesBtree(v, iDb);
133774 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly0x08)==0 ){
133775 /* Write the specified cookie value */
133776 static const VdbeOpList setCookie[] = {
133777 { OP_Transaction2, 0, 1, 0}, /* 0 */
133778 { OP_SetCookie100, 0, 0, 0}, /* 1 */
133779 };
133780 VdbeOp *aOp;
133781 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
133782 aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie)((int)(sizeof(setCookie)/sizeof(setCookie[0]))), setCookie, 0);
133783 if( ONLY_IF_REALLOC_STRESS(aOp==0)(0) ) break;
133784 aOp[0].p1 = iDb;
133785 aOp[1].p1 = iDb;
133786 aOp[1].p2 = iCookie;
133787 aOp[1].p3 = sqlite3Atoi(zRight);
133788 aOp[1].p5 = 1;
133789 }else{
133790 /* Read the specified cookie value */
133791 static const VdbeOpList readCookie[] = {
133792 { OP_Transaction2, 0, 0, 0}, /* 0 */
133793 { OP_ReadCookie99, 0, 1, 0}, /* 1 */
133794 { OP_ResultRow84, 1, 1, 0}
133795 };
133796 VdbeOp *aOp;
133797 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie));
133798 aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie)((int)(sizeof(readCookie)/sizeof(readCookie[0]))),readCookie,0);
133799 if( ONLY_IF_REALLOC_STRESS(aOp==0)(0) ) break;
133800 aOp[0].p1 = iDb;
133801 aOp[1].p1 = iDb;
133802 aOp[1].p3 = iCookie;
133803 sqlite3VdbeReusable(v);
133804 }
133805 }
133806 break;
133807#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
133808
133809#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
133810 /*
133811 ** PRAGMA compile_options
133812 **
133813 ** Return the names of all compile-time options used in this build,
133814 ** one option per row.
133815 */
133816 case PragTyp_COMPILE_OPTIONS10: {
133817 int i = 0;
133818 const char *zOpt;
133819 pParse->nMem = 1;
133820 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
133821 sqlite3VdbeLoadString(v, 1, zOpt);
133822 sqlite3VdbeAddOp2(v, OP_ResultRow84, 1, 1);
133823 }
133824 sqlite3VdbeReusable(v);
133825 }
133826 break;
133827#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
133828
133829#ifndef SQLITE_OMIT_WAL
133830 /*
133831 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
133832 **
133833 ** Checkpoint the database.
133834 */
133835 case PragTyp_WAL_CHECKPOINT43: {
133836 int iBt = (pId2->z?iDb:SQLITE_MAX_DB(10 +2));
133837 int eMode = SQLITE_CHECKPOINT_PASSIVE0;
133838 if( zRight ){
133839 if( sqlite3StrICmp(zRight, "full")==0 ){
133840 eMode = SQLITE_CHECKPOINT_FULL1;
133841 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
133842 eMode = SQLITE_CHECKPOINT_RESTART2;
133843 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
133844 eMode = SQLITE_CHECKPOINT_TRUNCATE3;
133845 }
133846 }
133847 pParse->nMem = 3;
133848 sqlite3VdbeAddOp3(v, OP_Checkpoint3, iBt, eMode, 1);
133849 sqlite3VdbeAddOp2(v, OP_ResultRow84, 1, 3);
133850 }
133851 break;
133852
133853 /*
133854 ** PRAGMA wal_autocheckpoint
133855 ** PRAGMA wal_autocheckpoint = N
133856 **
133857 ** Configure a database connection to automatically checkpoint a database
133858 ** after accumulating N frames in the log. Or query for the current value
133859 ** of N.
133860 */
133861 case PragTyp_WAL_AUTOCHECKPOINT42: {
133862 if( zRight ){
133863 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
133864 }
133865 returnSingleInt(v,
133866 db->xWalCallback==sqlite3WalDefaultHook ?
133867 SQLITE_PTR_TO_INT(db->pWalArg)((int)(long int)(db->pWalArg)) : 0);
133868 }
133869 break;
133870#endif
133871
133872 /*
133873 ** PRAGMA shrink_memory
133874 **
133875 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
133876 ** connection on which it is invoked to free up as much memory as it
133877 ** can, by calling sqlite3_db_release_memory().
133878 */
133879 case PragTyp_SHRINK_MEMORY34: {
133880 sqlite3_db_release_memory(db);
133881 break;
133882 }
133883
133884 /*
133885 ** PRAGMA optimize
133886 ** PRAGMA optimize(MASK)
133887 ** PRAGMA schema.optimize
133888 ** PRAGMA schema.optimize(MASK)
133889 **
133890 ** Attempt to optimize the database. All schemas are optimized in the first
133891 ** two forms, and only the specified schema is optimized in the latter two.
133892 **
133893 ** The details of optimizations performed by this pragma are expected
133894 ** to change and improve over time. Applications should anticipate that
133895 ** this pragma will perform new optimizations in future releases.
133896 **
133897 ** The optional argument is a bitmask of optimizations to perform:
133898 **
133899 ** 0x0001 Debugging mode. Do not actually perform any optimizations
133900 ** but instead return one line of text for each optimization
133901 ** that would have been done. Off by default.
133902 **
133903 ** 0x0002 Run ANALYZE on tables that might benefit. On by default.
133904 ** See below for additional information.
133905 **
133906 ** 0x0004 (Not yet implemented) Record usage and performance
133907 ** information from the current session in the
133908 ** database file so that it will be available to "optimize"
133909 ** pragmas run by future database connections.
133910 **
133911 ** 0x0008 (Not yet implemented) Create indexes that might have
133912 ** been helpful to recent queries
133913 **
133914 ** The default MASK is and always shall be 0xfffe. 0xfffe means perform all
133915 ** of the optimizations listed above except Debug Mode, including new
133916 ** optimizations that have not yet been invented. If new optimizations are
133917 ** ever added that should be off by default, those off-by-default
133918 ** optimizations will have bitmasks of 0x10000 or larger.
133919 **
133920 ** DETERMINATION OF WHEN TO RUN ANALYZE
133921 **
133922 ** In the current implementation, a table is analyzed if only if all of
133923 ** the following are true:
133924 **
133925 ** (1) MASK bit 0x02 is set.
133926 **
133927 ** (2) The query planner used sqlite_stat1-style statistics for one or
133928 ** more indexes of the table at some point during the lifetime of
133929 ** the current connection.
133930 **
133931 ** (3) One or more indexes of the table are currently unanalyzed OR
133932 ** the number of rows in the table has increased by 25 times or more
133933 ** since the last time ANALYZE was run.
133934 **
133935 ** The rules for when tables are analyzed are likely to change in
133936 ** future releases.
133937 */
133938 case PragTyp_OPTIMIZE30: {
133939 int iDbLast; /* Loop termination point for the schema loop */
133940 int iTabCur; /* Cursor for a table whose size needs checking */
133941 HashElem *k; /* Loop over tables of a schema */
133942 Schema *pSchema; /* The current schema */
133943 Table *pTab; /* A table in the schema */
133944 Index *pIdx; /* An index of the table */
133945 LogEst szThreshold; /* Size threshold above which reanalysis is needd */
133946 char *zSubSql; /* SQL statement for the OP_SqlExec opcode */
133947 u32 opMask; /* Mask of operations to perform */
133948
133949 if( zRight ){
133950 opMask = (u32)sqlite3Atoi(zRight);
133951 if( (opMask & 0x02)==0 ) break;
133952 }else{
133953 opMask = 0xfffe;
133954 }
133955 iTabCur = pParse->nTab++;
133956 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
133957 if( iDb==1 ) continue;
133958 sqlite3CodeVerifySchema(pParse, iDb);
133959 pSchema = db->aDb[iDb].pSchema;
133960 for(k=sqliteHashFirst(&pSchema->tblHash)((&pSchema->tblHash)->first); k; k=sqliteHashNext(k)((k)->next)){
133961 pTab = (Table*)sqliteHashData(k)((k)->data);
133962
133963 /* If table pTab has not been used in a way that would benefit from
133964 ** having analysis statistics during the current session, then skip it.
133965 ** This also has the effect of skipping virtual tables and views */
133966 if( (pTab->tabFlags & TF_StatsUsed0x00000100)==0 ) continue;
133967
133968 /* Reanalyze if the table is 25 times larger than the last analysis */
133969 szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 )((void) (0));
133970 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
133971 if( !pIdx->hasStat1 ){
133972 szThreshold = 0; /* Always analyze if any index lacks statistics */
133973 break;
133974 }
133975 }
133976 if( szThreshold ){
133977 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead112);
133978 sqlite3VdbeAddOp3(v, OP_IfSmaller32, iTabCur,
133979 sqlite3VdbeCurrentAddr(v)+2+(opMask&1), szThreshold);
133980 VdbeCoverage(v);
133981 }
133982 zSubSql = sqlite3MPrintf(db, "ANALYZE \"%w\".\"%w\"",
133983 db->aDb[iDb].zDbSName, pTab->zName);
133984 if( opMask & 0x01 ){
133985 int r1 = sqlite3GetTempReg(pParse);
133986 sqlite3VdbeAddOp4(v, OP_String8117, 0, r1, 0, zSubSql, P4_DYNAMIC(-6));
133987 sqlite3VdbeAddOp2(v, OP_ResultRow84, r1, 1);
133988 }else{
133989 sqlite3VdbeAddOp4(v, OP_SqlExec148, 0, 0, 0, zSubSql, P4_DYNAMIC(-6));
133990 }
133991 }
133992 }
133993 sqlite3VdbeAddOp0(v, OP_Expire166);
133994 break;
133995 }
133996
133997 /*
133998 ** PRAGMA busy_timeout
133999 ** PRAGMA busy_timeout = N
134000 **
134001 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
134002 ** if one is set. If no busy handler or a different busy handler is set
134003 ** then 0 is returned. Setting the busy_timeout to 0 or negative
134004 ** disables the timeout.
134005 */
134006 /*case PragTyp_BUSY_TIMEOUT*/ default: {
134007 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT )((void) (0));
134008 if( zRight ){
134009 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
134010 }
134011 returnSingleInt(v, db->busyTimeout);
134012 break;
134013 }
134014
134015 /*
134016 ** PRAGMA soft_heap_limit
134017 ** PRAGMA soft_heap_limit = N
134018 **
134019 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
134020 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
134021 ** specified and is a non-negative integer.
134022 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
134023 ** returns the same integer that would be returned by the
134024 ** sqlite3_soft_heap_limit64(-1) C-language function.
134025 */
134026 case PragTyp_SOFT_HEAP_LIMIT35: {
134027 sqlite3_int64 N;
134028 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK0 ){
134029 sqlite3_soft_heap_limit64(N);
134030 }
134031 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
134032 break;
134033 }
134034
134035 /*
134036 ** PRAGMA hard_heap_limit
134037 ** PRAGMA hard_heap_limit = N
134038 **
134039 ** Invoke sqlite3_hard_heap_limit64() to query or set the hard heap
134040 ** limit. The hard heap limit can be activated or lowered by this
134041 ** pragma, but not raised or deactivated. Only the
134042 ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate
134043 ** the hard heap limit. This allows an application to set a heap limit
134044 ** constraint that cannot be relaxed by an untrusted SQL script.
134045 */
134046 case PragTyp_HARD_HEAP_LIMIT18: {
134047 sqlite3_int64 N;
134048 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK0 ){
134049 sqlite3_int64 iPrior = sqlite3_hard_heap_limit64(-1);
134050 if( N>0 && (iPrior==0 || iPrior>N) ) sqlite3_hard_heap_limit64(N);
134051 }
134052 returnSingleInt(v, sqlite3_hard_heap_limit64(-1));
134053 break;
134054 }
134055
134056 /*
134057 ** PRAGMA threads
134058 ** PRAGMA threads = N
134059 **
134060 ** Configure the maximum number of worker threads. Return the new
134061 ** maximum, which might be less than requested.
134062 */
134063 case PragTyp_THREADS41: {
134064 sqlite3_int64 N;
134065 if( zRight
134066 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK0
134067 && N>=0
134068 ){
134069 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS11, (int)(N&0x7fffffff));
134070 }
134071 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS11, -1));
134072 break;
134073 }
134074
134075 /*
134076 ** PRAGMA analysis_limit
134077 ** PRAGMA analysis_limit = N
134078 **
134079 ** Configure the maximum number of rows that ANALYZE will examine
134080 ** in each index that it looks at. Return the new limit.
134081 */
134082 case PragTyp_ANALYSIS_LIMIT1: {
134083 sqlite3_int64 N;
134084 if( zRight
134085 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK0 /* IMP: R-40975-20399 */
134086 && N>=0
134087 ){
134088 db->nAnalysisLimit = (int)(N&0x7fffffff);
134089 }
134090 returnSingleInt(v, db->nAnalysisLimit); /* IMP: R-57594-65522 */
134091 break;
134092 }
134093
134094#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
134095 /*
134096 ** Report the current state of file logs for all databases
134097 */
134098 case PragTyp_LOCK_STATUS44: {
134099 static const char *const azLockName[] = {
134100 "unlocked", "shared", "reserved", "pending", "exclusive"
134101 };
134102 int i;
134103 pParse->nMem = 2;
134104 for(i=0; i<db->nDb; i++){
134105 Btree *pBt;
134106 const char *zState = "unknown";
134107 int j;
134108 if( db->aDb[i].zDbSName==0 ) continue;
134109 pBt = db->aDb[i].pBt;
134110 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
134111 zState = "closed";
134112 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
134113 SQLITE_FCNTL_LOCKSTATE1, &j)==SQLITE_OK0 ){
134114 zState = azLockName[j];
134115 }
134116 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
134117 }
134118 break;
134119 }
134120#endif
134121
134122#if defined(SQLITE_ENABLE_CEROD)
134123 case PragTyp_ACTIVATE_EXTENSIONS0: if( zRight ){
134124 if( sqlite3StrNICmpsqlite3_strnicmp(zRight, "cerod-", 6)==0 ){
134125 sqlite3_activate_cerod(&zRight[6]);
134126 }
134127 }
134128 break;
134129#endif
134130
134131 } /* End of the PRAGMA switch */
134132
134133 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
134134 ** purpose is to execute assert() statements to verify that if the
134135 ** PragFlg_NoColumns1 flag is set and the caller specified an argument
134136 ** to the PRAGMA, the implementation has not added any OP_ResultRow
134137 ** instructions to the VM. */
134138 if( (pPragma->mPragFlg & PragFlg_NoColumns10x04) && zRight ){
134139 sqlite3VdbeVerifyNoResultRow(v);
134140 }
134141
134142pragma_out:
134143 sqlite3DbFree(db, zLeft);
134144 sqlite3DbFree(db, zRight);
134145}
134146#ifndef SQLITE_OMIT_VIRTUALTABLE
134147/*****************************************************************************
134148** Implementation of an eponymous virtual table that runs a pragma.
134149**
134150*/
134151typedef struct PragmaVtab PragmaVtab;
134152typedef struct PragmaVtabCursor PragmaVtabCursor;
134153struct PragmaVtab {
134154 sqlite3_vtab base; /* Base class. Must be first */
134155 sqlite3 *db; /* The database connection to which it belongs */
134156 const PragmaName *pName; /* Name of the pragma */
134157 u8 nHidden; /* Number of hidden columns */
134158 u8 iHidden; /* Index of the first hidden column */
134159};
134160struct PragmaVtabCursor {
134161 sqlite3_vtab_cursor base; /* Base class. Must be first */
134162 sqlite3_stmt *pPragma; /* The pragma statement to run */
134163 sqlite_int64 iRowid; /* Current rowid */
134164 char *azArg[2]; /* Value of the argument and schema */
134165};
134166
134167/*
134168** Pragma virtual table module xConnect method.
134169*/
134170static int pragmaVtabConnect(
134171 sqlite3 *db,
134172 void *pAux,
134173 int argc, const char *const*argv,
134174 sqlite3_vtab **ppVtab,
134175 char **pzErr
134176){
134177 const PragmaName *pPragma = (const PragmaName*)pAux;
134178 PragmaVtab *pTab = 0;
134179 int rc;
134180 int i, j;
134181 char cSep = '(';
134182 StrAccum acc;
134183 char zBuf[200];
134184
134185 UNUSED_PARAMETER(argc)(void)(argc);
134186 UNUSED_PARAMETER(argv)(void)(argv);
134187 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
134188 sqlite3_str_appendall(&acc, "CREATE TABLE x");
134189 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
134190 sqlite3_str_appendf(&acc, "%c\"%s\"", cSep, pragCName[j]);
134191 cSep = ',';
134192 }
134193 if( i==0 ){
134194 sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
134195 i++;
134196 }
134197 j = 0;
134198 if( pPragma->mPragFlg & PragFlg_Result10x20 ){
134199 sqlite3_str_appendall(&acc, ",arg HIDDEN");
134200 j++;
134201 }
134202 if( pPragma->mPragFlg & (PragFlg_SchemaOpt0x40|PragFlg_SchemaReq0x80) ){
134203 sqlite3_str_appendall(&acc, ",schema HIDDEN");
134204 j++;
134205 }
134206 sqlite3_str_append(&acc, ")", 1);
134207 sqlite3StrAccumFinish(&acc);
134208 assert( strlen(zBuf) < sizeof(zBuf)-1 )((void) (0));
134209 rc = sqlite3_declare_vtab(db, zBuf);
134210 if( rc==SQLITE_OK0 ){
134211 pTab = (PragmaVtab*)sqlite3_malloc(sizeof(PragmaVtab));
134212 if( pTab==0 ){
134213 rc = SQLITE_NOMEM7;
134214 }else{
134215 memset(pTab, 0, sizeof(PragmaVtab));
134216 pTab->pName = pPragma;
134217 pTab->db = db;
134218 pTab->iHidden = i;
134219 pTab->nHidden = j;
134220 }
134221 }else{
134222 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
134223 }
134224
134225 *ppVtab = (sqlite3_vtab*)pTab;
134226 return rc;
134227}
134228
134229/*
134230** Pragma virtual table module xDisconnect method.
134231*/
134232static int pragmaVtabDisconnect(sqlite3_vtab *pVtab){
134233 PragmaVtab *pTab = (PragmaVtab*)pVtab;
134234 sqlite3_free(pTab);
134235 return SQLITE_OK0;
134236}
134237
134238/* Figure out the best index to use to search a pragma virtual table.
134239**
134240** There are not really any index choices. But we want to encourage the
134241** query planner to give == constraints on as many hidden parameters as
134242** possible, and especially on the first hidden parameter. So return a
134243** high cost if hidden parameters are unconstrained.
134244*/
134245static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
134246 PragmaVtab *pTab = (PragmaVtab*)tab;
134247 const struct sqlite3_index_constraint *pConstraint;
134248 int i, j;
134249 int seen[2];
134250
134251 pIdxInfo->estimatedCost = (double)1;
134252 if( pTab->nHidden==0 ){ return SQLITE_OK0; }
134253 pConstraint = pIdxInfo->aConstraint;
134254 seen[0] = 0;
134255 seen[1] = 0;
134256 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
134257 if( pConstraint->usable==0 ) continue;
134258 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ2 ) continue;
134259 if( pConstraint->iColumn < pTab->iHidden ) continue;
134260 j = pConstraint->iColumn - pTab->iHidden;
134261 assert( j < 2 )((void) (0));
134262 seen[j] = i+1;
134263 }
134264 if( seen[0]==0 ){
134265 pIdxInfo->estimatedCost = (double)2147483647;
134266 pIdxInfo->estimatedRows = 2147483647;
134267 return SQLITE_OK0;
134268 }
134269 j = seen[0]-1;
134270 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
134271 pIdxInfo->aConstraintUsage[j].omit = 1;
134272 if( seen[1]==0 ) return SQLITE_OK0;
134273 pIdxInfo->estimatedCost = (double)20;
134274 pIdxInfo->estimatedRows = 20;
134275 j = seen[1]-1;
134276 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
134277 pIdxInfo->aConstraintUsage[j].omit = 1;
134278 return SQLITE_OK0;
134279}
134280
134281/* Create a new cursor for the pragma virtual table */
134282static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){
134283 PragmaVtabCursor *pCsr;
134284 pCsr = (PragmaVtabCursor*)sqlite3_malloc(sizeof(*pCsr));
134285 if( pCsr==0 ) return SQLITE_NOMEM7;
134286 memset(pCsr, 0, sizeof(PragmaVtabCursor));
134287 pCsr->base.pVtab = pVtab;
134288 *ppCursor = &pCsr->base;
134289 return SQLITE_OK0;
134290}
134291
134292/* Clear all content from pragma virtual table cursor. */
134293static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){
134294 int i;
134295 sqlite3_finalize(pCsr->pPragma);
134296 pCsr->pPragma = 0;
134297 for(i=0; i<ArraySize(pCsr->azArg)((int)(sizeof(pCsr->azArg)/sizeof(pCsr->azArg[0]))); i++){
134298 sqlite3_free(pCsr->azArg[i]);
134299 pCsr->azArg[i] = 0;
134300 }
134301}
134302
134303/* Close a pragma virtual table cursor */
134304static int pragmaVtabClose(sqlite3_vtab_cursor *cur){
134305 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)cur;
134306 pragmaVtabCursorClear(pCsr);
134307 sqlite3_free(pCsr);
134308 return SQLITE_OK0;
134309}
134310
134311/* Advance the pragma virtual table cursor to the next row */
134312static int pragmaVtabNext(sqlite3_vtab_cursor *pVtabCursor){
134313 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
134314 int rc = SQLITE_OK0;
134315
134316 /* Increment the xRowid value */
134317 pCsr->iRowid++;
134318 assert( pCsr->pPragma )((void) (0));
134319 if( SQLITE_ROW100!=sqlite3_step(pCsr->pPragma) ){
134320 rc = sqlite3_finalize(pCsr->pPragma);
134321 pCsr->pPragma = 0;
134322 pragmaVtabCursorClear(pCsr);
134323 }
134324 return rc;
134325}
134326
134327/*
134328** Pragma virtual table module xFilter method.
134329*/
134330static int pragmaVtabFilter(
134331 sqlite3_vtab_cursor *pVtabCursor,
134332 int idxNum, const char *idxStr,
134333 int argc, sqlite3_value **argv
134334){
134335 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
134336 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
134337 int rc;
134338 int i, j;
134339 StrAccum acc;
134340 char *zSql;
134341
134342 UNUSED_PARAMETER(idxNum)(void)(idxNum);
134343 UNUSED_PARAMETER(idxStr)(void)(idxStr);
134344 pragmaVtabCursorClear(pCsr);
134345 j = (pTab->pName->mPragFlg & PragFlg_Result10x20)!=0 ? 0 : 1;
134346 for(i=0; i<argc; i++, j++){
134347 const char *zText = (const char*)sqlite3_value_text(argv[i]);
134348 assert( j<ArraySize(pCsr->azArg) )((void) (0));
134349 assert( pCsr->azArg[j]==0 )((void) (0));
134350 if( zText ){
134351 pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
134352 if( pCsr->azArg[j]==0 ){
134353 return SQLITE_NOMEM7;
134354 }
134355 }
134356 }
134357 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH1]);
134358 sqlite3_str_appendall(&acc, "PRAGMA ");
134359 if( pCsr->azArg[1] ){
134360 sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
134361 }
134362 sqlite3_str_appendall(&acc, pTab->pName->zName);
134363 if( pCsr->azArg[0] ){
134364 sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
134365 }
134366 zSql = sqlite3StrAccumFinish(&acc);
134367 if( zSql==0 ) return SQLITE_NOMEM7;
134368 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
134369 sqlite3_free(zSql);
134370 if( rc!=SQLITE_OK0 ){
134371 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
134372 return rc;
134373 }
134374 return pragmaVtabNext(pVtabCursor);
134375}
134376
134377/*
134378** Pragma virtual table module xEof method.
134379*/
134380static int pragmaVtabEof(sqlite3_vtab_cursor *pVtabCursor){
134381 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
134382 return (pCsr->pPragma==0);
134383}
134384
134385/* The xColumn method simply returns the corresponding column from
134386** the PRAGMA.
134387*/
134388static int pragmaVtabColumn(
134389 sqlite3_vtab_cursor *pVtabCursor,
134390 sqlite3_context *ctx,
134391 int i
134392){
134393 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
134394 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
134395 if( i<pTab->iHidden ){
134396 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
134397 }else{
134398 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
134399 }
134400 return SQLITE_OK0;
134401}
134402
134403/*
134404** Pragma virtual table module xRowid method.
134405*/
134406static int pragmaVtabRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *p){
134407 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
134408 *p = pCsr->iRowid;
134409 return SQLITE_OK0;
134410}
134411
134412/* The pragma virtual table object */
134413static const sqlite3_module pragmaVtabModule = {
134414 0, /* iVersion */
134415 0, /* xCreate - create a table */
134416 pragmaVtabConnect, /* xConnect - connect to an existing table */
134417 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
134418 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
134419 0, /* xDestroy - Drop a table */
134420 pragmaVtabOpen, /* xOpen - open a cursor */
134421 pragmaVtabClose, /* xClose - close a cursor */
134422 pragmaVtabFilter, /* xFilter - configure scan constraints */
134423 pragmaVtabNext, /* xNext - advance a cursor */
134424 pragmaVtabEof, /* xEof */
134425 pragmaVtabColumn, /* xColumn - read data */
134426 pragmaVtabRowid, /* xRowid - read data */
134427 0, /* xUpdate - write data */
134428 0, /* xBegin - begin transaction */
134429 0, /* xSync - sync transaction */
134430 0, /* xCommit - commit transaction */
134431 0, /* xRollback - rollback transaction */
134432 0, /* xFindFunction - function overloading */
134433 0, /* xRename - rename the table */
134434 0, /* xSavepoint */
134435 0, /* xRelease */
134436 0, /* xRollbackTo */
134437 0 /* xShadowName */
134438};
134439
134440/*
134441** Check to see if zTabName is really the name of a pragma. If it is,
134442** then register an eponymous virtual table for that pragma and return
134443** a pointer to the Module object for the new virtual table.
134444*/
134445SQLITE_PRIVATEstatic Module *sqlite3PragmaVtabRegister(sqlite3 *db, const char *zName){
134446 const PragmaName *pName;
134447 assert( sqlite3_strnicmp(zName, "pragma_", 7)==0 )((void) (0));
134448 pName = pragmaLocate(zName+7);
134449 if( pName==0 ) return 0;
134450 if( (pName->mPragFlg & (PragFlg_Result00x10|PragFlg_Result10x20))==0 ) return 0;
134451 assert( sqlite3HashFind(&db->aModule, zName)==0 )((void) (0));
134452 return sqlite3VtabCreateModule(db, zName, &pragmaVtabModule, (void*)pName, 0);
134453}
134454
134455#endif /* SQLITE_OMIT_VIRTUALTABLE */
134456
134457#endif /* SQLITE_OMIT_PRAGMA */
134458
134459/************** End of pragma.c **********************************************/
134460/************** Begin file prepare.c *****************************************/
134461/*
134462** 2005 May 25
134463**
134464** The author disclaims copyright to this source code. In place of
134465** a legal notice, here is a blessing:
134466**
134467** May you do good and not evil.
134468** May you find forgiveness for yourself and forgive others.
134469** May you share freely, never taking more than you give.
134470**
134471*************************************************************************
134472** This file contains the implementation of the sqlite3_prepare()
134473** interface, and routines that contribute to loading the database schema
134474** from disk.
134475*/
134476/* #include "sqliteInt.h" */
134477
134478/*
134479** Fill the InitData structure with an error message that indicates
134480** that the database is corrupt.
134481*/
134482static void corruptSchema(
134483 InitData *pData, /* Initialization context */
134484 char **azObj, /* Type and name of object being parsed */
134485 const char *zExtra /* Error information */
134486){
134487 sqlite3 *db = pData->db;
134488 if( db->mallocFailed ){
134489 pData->rc = SQLITE_NOMEM_BKPT7;
134490 }else if( pData->pzErrMsg[0]!=0 ){
134491 /* A error message has already been generated. Do not overwrite it */
134492 }else if( pData->mInitFlags & (INITFLAG_AlterMask0x0003) ){
134493 static const char *azAlterType[] = {
134494 "rename",
134495 "drop column",
134496 "add column"
134497 };
134498 *pData->pzErrMsg = sqlite3MPrintf(db,
134499 "error in %s %s after %s: %s", azObj[0], azObj[1],
134500 azAlterType[(pData->mInitFlags&INITFLAG_AlterMask0x0003)-1],
134501 zExtra
134502 );
134503 pData->rc = SQLITE_ERROR1;
134504 }else if( db->flags & SQLITE_WriteSchema0x00000001 ){
134505 pData->rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(134505);
134506 }else{
134507 char *z;
134508 const char *zObj = azObj[1] ? azObj[1] : "?";
134509 z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj);
134510 if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
134511 *pData->pzErrMsg = z;
134512 pData->rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(134512);
134513 }
134514}
134515
134516/*
134517** Check to see if any sibling index (another index on the same table)
134518** of pIndex has the same root page number, and if it does, return true.
134519** This would indicate a corrupt schema.
134520*/
134521SQLITE_PRIVATEstatic int sqlite3IndexHasDuplicateRootPage(Index *pIndex){
134522 Index *p;
134523 for(p=pIndex->pTable->pIndex; p; p=p->pNext){
134524 if( p->tnum==pIndex->tnum && p!=pIndex ) return 1;
134525 }
134526 return 0;
134527}
134528
134529/* forward declaration */
134530static int sqlite3Prepare(
134531 sqlite3 *db, /* Database handle. */
134532 const char *zSql, /* UTF-8 encoded SQL statement. */
134533 int nBytes, /* Length of zSql in bytes. */
134534 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
134535 Vdbe *pReprepare, /* VM being reprepared */
134536 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
134537 const char **pzTail /* OUT: End of parsed string */
134538);
134539
134540
134541/*
134542** This is the callback routine for the code that initializes the
134543** database. See sqlite3Init() below for additional information.
134544** This routine is also called from the OP_ParseSchema opcode of the VDBE.
134545**
134546** Each callback contains the following information:
134547**
134548** argv[0] = type of object: "table", "index", "trigger", or "view".
134549** argv[1] = name of thing being created
134550** argv[2] = associated table if an index or trigger
134551** argv[3] = root page number for table or index. 0 for trigger or view.
134552** argv[4] = SQL text for the CREATE statement.
134553**
134554*/
134555SQLITE_PRIVATEstatic int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
134556 InitData *pData = (InitData*)pInit;
134557 sqlite3 *db = pData->db;
134558 int iDb = pData->iDb;
134559
134560 assert( argc==5 )((void) (0));
134561 UNUSED_PARAMETER2(NotUsed, argc)(void)(NotUsed),(void)(argc);
134562 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
134563 db->mDbFlags |= DBFLAG_EncodingFixed0x0040;
134564 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
134565 pData->nInitRow++;
134566 if( db->mallocFailed ){
134567 corruptSchema(pData, argv, 0);
134568 return 1;
134569 }
134570
134571 assert( iDb>=0 && iDb<db->nDb )((void) (0));
134572 if( argv[3]==0 ){
134573 corruptSchema(pData, argv, 0);
134574 }else if( argv[4]
134575 && 'c'==sqlite3UpperToLower[(unsigned char)argv[4][0]]
134576 && 'r'==sqlite3UpperToLower[(unsigned char)argv[4][1]] ){
134577 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
134578 ** But because db->init.busy is set to 1, no VDBE code is generated
134579 ** or executed. All the parser does is build the internal data
134580 ** structures that describe the table, index, or view.
134581 **
134582 ** No other valid SQL statement, other than the variable CREATE statements,
134583 ** can begin with the letters "C" and "R". Thus, it is not possible run
134584 ** any other kind of statement while parsing the schema, even a corrupt
134585 ** schema.
134586 */
134587 int rc;
134588 u8 saved_iDb = db->init.iDb;
134589 sqlite3_stmt *pStmt;
134590 TESTONLY(int rcp); /* Return code from sqlite3_prepare() */
134591
134592 assert( db->init.busy )((void) (0));
134593 db->init.iDb = iDb;
134594 if( sqlite3GetUInt32(argv[3], &db->init.newTnum)==0
134595 || (db->init.newTnum>pData->mxPage && pData->mxPage>0)
134596 ){
134597 if( sqlite3Config.bExtraSchemaChecks ){
134598 corruptSchema(pData, argv, "invalid rootpage");
134599 }
134600 }
134601 db->init.orphanTrigger = 0;
134602 db->init.azInit = (const char**)argv;
134603 pStmt = 0;
134604 TESTONLY(rcp = ) sqlite3Prepare(db, argv[4], -1, 0, 0, &pStmt, 0);
134605 rc = db->errCode;
134606 assert( (rc&0xFF)==(rcp&0xFF) )((void) (0));
134607 db->init.iDb = saved_iDb;
134608 /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
134609 if( SQLITE_OK0!=rc ){
134610 if( db->init.orphanTrigger ){
134611 assert( iDb==1 )((void) (0));
134612 }else{
134613 if( rc > pData->rc ) pData->rc = rc;
134614 if( rc==SQLITE_NOMEM7 ){
134615 sqlite3OomFault(db);
134616 }else if( rc!=SQLITE_INTERRUPT9 && (rc&0xFF)!=SQLITE_LOCKED6 ){
134617 corruptSchema(pData, argv, sqlite3_errmsg(db));
134618 }
134619 }
134620 }
134621 db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */
134622 sqlite3_finalize(pStmt);
134623 }else if( argv[1]==0 || (argv[4]!=0 && argv[4][0]!=0) ){
134624 corruptSchema(pData, argv, 0);
134625 }else{
134626 /* If the SQL column is blank it means this is an index that
134627 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
134628 ** constraint for a CREATE TABLE. The index should have already
134629 ** been created when we processed the CREATE TABLE. All we have
134630 ** to do here is record the root page number for that index.
134631 */
134632 Index *pIndex;
134633 pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zDbSName);
134634 if( pIndex==0 ){
134635 corruptSchema(pData, argv, "orphan index");
134636 }else
134637 if( sqlite3GetUInt32(argv[3],&pIndex->tnum)==0
134638 || pIndex->tnum<2
134639 || pIndex->tnum>pData->mxPage
134640 || sqlite3IndexHasDuplicateRootPage(pIndex)
134641 ){
134642 if( sqlite3Config.bExtraSchemaChecks ){
134643 corruptSchema(pData, argv, "invalid rootpage");
134644 }
134645 }
134646 }
134647 return 0;
134648}
134649
134650/*
134651** Attempt to read the database schema and initialize internal
134652** data structures for a single database file. The index of the
134653** database file is given by iDb. iDb==0 is used for the main
134654** database. iDb==1 should never be used. iDb>=2 is used for
134655** auxiliary databases. Return one of the SQLITE_ error codes to
134656** indicate success or failure.
134657*/
134658SQLITE_PRIVATEstatic int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg, u32 mFlags){
134659 int rc;
134660 int i;
134661#ifndef SQLITE_OMIT_DEPRECATED1
134662 int size;
134663#endif
134664 Db *pDb;
134665 char const *azArg[6];
134666 int meta[5];
134667 InitData initData;
134668 const char *zSchemaTabName;
134669 int openedTransaction = 0;
134670 int mask = ((db->mDbFlags & DBFLAG_EncodingFixed0x0040) | ~DBFLAG_EncodingFixed0x0040);
134671
134672 assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 )((void) (0));
134673 assert( iDb>=0 && iDb<db->nDb )((void) (0));
134674 assert( db->aDb[iDb].pSchema )((void) (0));
134675 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
134676 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) )((void) (0));
134677
134678 db->init.busy = 1;
134679
134680 /* Construct the in-memory representation schema tables (sqlite_schema or
134681 ** sqlite_temp_schema) by invoking the parser directly. The appropriate
134682 ** table name will be inserted automatically by the parser so we can just
134683 ** use the abbreviation "x" here. The parser will also automatically tag
134684 ** the schema table as read-only. */
134685 azArg[0] = "table";
134686 azArg[1] = zSchemaTabName = SCHEMA_TABLE(iDb)((!0)&&(iDb==1)?"sqlite_temp_master":"sqlite_master");
134687 azArg[2] = azArg[1];
134688 azArg[3] = "1";
134689 azArg[4] = "CREATE TABLE x(type text,name text,tbl_name text,"
134690 "rootpage int,sql text)";
134691 azArg[5] = 0;
134692 initData.db = db;
134693 initData.iDb = iDb;
134694 initData.rc = SQLITE_OK0;
134695 initData.pzErrMsg = pzErrMsg;
134696 initData.mInitFlags = mFlags;
134697 initData.nInitRow = 0;
134698 initData.mxPage = 0;
134699 sqlite3InitCallback(&initData, 5, (char **)azArg, 0);
134700 db->mDbFlags &= mask;
134701 if( initData.rc ){
134702 rc = initData.rc;
134703 goto error_out;
134704 }
134705
134706 /* Create a cursor to hold the database open
134707 */
134708 pDb = &db->aDb[iDb];
134709 if( pDb->pBt==0 ){
134710 assert( iDb==1 )((void) (0));
134711 DbSetProperty(db, 1, DB_SchemaLoaded)(db)->aDb[1].pSchema->schemaFlags|=(0x0001);
134712 rc = SQLITE_OK0;
134713 goto error_out;
134714 }
134715
134716 /* If there is not already a read-only (or read-write) transaction opened
134717 ** on the b-tree database, open one now. If a transaction is opened, it
134718 ** will be closed before this function returns. */
134719 sqlite3BtreeEnter(pDb->pBt);
134720 if( sqlite3BtreeTxnState(pDb->pBt)==SQLITE_TXN_NONE0 ){
134721 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0);
134722 if( rc!=SQLITE_OK0 ){
134723 sqlite3SetString(pzErrMsg, db, sqlite3ErrStr(rc));
134724 goto initone_error_out;
134725 }
134726 openedTransaction = 1;
134727 }
134728
134729 /* Get the database meta information.
134730 **
134731 ** Meta values are as follows:
134732 ** meta[0] Schema cookie. Changes with each schema change.
134733 ** meta[1] File format of schema layer.
134734 ** meta[2] Size of the page cache.
134735 ** meta[3] Largest rootpage (auto/incr_vacuum mode)
134736 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
134737 ** meta[5] User version
134738 ** meta[6] Incremental vacuum mode
134739 ** meta[7] unused
134740 ** meta[8] unused
134741 ** meta[9] unused
134742 **
134743 ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
134744 ** the possible values of meta[4].
134745 */
134746 for(i=0; i<ArraySize(meta)((int)(sizeof(meta)/sizeof(meta[0]))); i++){
134747 sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
134748 }
134749 if( (db->flags & SQLITE_ResetDatabase0x02000000)!=0 ){
134750 memset(meta, 0, sizeof(meta));
134751 }
134752 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION1-1];
134753
134754 /* If opening a non-empty database, check the text encoding. For the
134755 ** main database, set sqlite3.enc to the encoding of the main database.
134756 ** For an attached db, it is an error if the encoding is not the same
134757 ** as sqlite3.enc.
134758 */
134759 if( meta[BTREE_TEXT_ENCODING5-1] ){ /* text encoding */
134760 if( iDb==0 && (db->mDbFlags & DBFLAG_EncodingFixed0x0040)==0 ){
134761 u8 encoding;
134762#ifndef SQLITE_OMIT_UTF16
134763 /* If opening the main database, set ENC(db). */
134764 encoding = (u8)meta[BTREE_TEXT_ENCODING5-1] & 3;
134765 if( encoding==0 ) encoding = SQLITE_UTF81;
134766#else
134767 encoding = SQLITE_UTF81;
134768#endif
134769 sqlite3SetTextEncoding(db, encoding);
134770 }else{
134771 /* If opening an attached database, the encoding much match ENC(db) */
134772 if( (meta[BTREE_TEXT_ENCODING5-1] & 3)!=ENC(db)((db)->enc) ){
134773 sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
134774 " text encoding as main database");
134775 rc = SQLITE_ERROR1;
134776 goto initone_error_out;
134777 }
134778 }
134779 }
134780 pDb->pSchema->enc = ENC(db)((db)->enc);
134781
134782 if( pDb->pSchema->cache_size==0 ){
134783#ifndef SQLITE_OMIT_DEPRECATED1
134784 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE3-1]);
134785 if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE128; }
134786 pDb->pSchema->cache_size = size;
134787#else
134788 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE128;
134789#endif
134790 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
134791 }
134792
134793 /*
134794 ** file_format==1 Version 3.0.0.
134795 ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN
134796 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
134797 ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants
134798 */
134799 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT2-1];
134800 if( pDb->pSchema->file_format==0 ){
134801 pDb->pSchema->file_format = 1;
134802 }
134803 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT4 ){
134804 sqlite3SetString(pzErrMsg, db, "unsupported file format");
134805 rc = SQLITE_ERROR1;
134806 goto initone_error_out;
134807 }
134808
134809 /* Ticket #2804: When we open a database in the newer file format,
134810 ** clear the legacy_file_format pragma flag so that a VACUUM will
134811 ** not downgrade the database and thus invalidate any descending
134812 ** indices that the user might have created.
134813 */
134814 if( iDb==0 && meta[BTREE_FILE_FORMAT2-1]>=4 ){
134815 db->flags &= ~(u64)SQLITE_LegacyFileFmt0x00000002;
134816 }
134817
134818 /* Read the schema information out of the schema tables
134819 */
134820 assert( db->init.busy )((void) (0));
134821 initData.mxPage = sqlite3BtreeLastPage(pDb->pBt);
134822 {
134823 char *zSql;
134824 zSql = sqlite3MPrintf(db,
134825 "SELECT*FROM\"%w\".%s ORDER BY rowid",
134826 db->aDb[iDb].zDbSName, zSchemaTabName);
134827#ifndef SQLITE_OMIT_AUTHORIZATION
134828 {
134829 sqlite3_xauth xAuth;
134830 xAuth = db->xAuth;
134831 db->xAuth = 0;
134832#endif
134833 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
134834#ifndef SQLITE_OMIT_AUTHORIZATION
134835 db->xAuth = xAuth;
134836 }
134837#endif
134838 if( rc==SQLITE_OK0 ) rc = initData.rc;
134839 sqlite3DbFree(db, zSql);
134840#ifndef SQLITE_OMIT_ANALYZE
134841 if( rc==SQLITE_OK0 ){
134842 sqlite3AnalysisLoad(db, iDb);
134843 }
134844#endif
134845 }
134846 assert( pDb == &(db->aDb[iDb]) )((void) (0));
134847 if( db->mallocFailed ){
134848 rc = SQLITE_NOMEM_BKPT7;
134849 sqlite3ResetAllSchemasOfConnection(db);
134850 pDb = &db->aDb[iDb];
134851 }else
134852 if( rc==SQLITE_OK0 || ((db->flags&SQLITE_NoSchemaError0x08000000) && rc!=SQLITE_NOMEM7)){
134853 /* Hack: If the SQLITE_NoSchemaError flag is set, then consider
134854 ** the schema loaded, even if errors (other than OOM) occurred. In
134855 ** this situation the current sqlite3_prepare() operation will fail,
134856 ** but the following one will attempt to compile the supplied statement
134857 ** against whatever subset of the schema was loaded before the error
134858 ** occurred.
134859 **
134860 ** The primary purpose of this is to allow access to the sqlite_schema
134861 ** table even when its contents have been corrupted.
134862 */
134863 DbSetProperty(db, iDb, DB_SchemaLoaded)(db)->aDb[iDb].pSchema->schemaFlags|=(0x0001);
134864 rc = SQLITE_OK0;
134865 }
134866
134867 /* Jump here for an error that occurs after successfully allocating
134868 ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
134869 ** before that point, jump to error_out.
134870 */
134871initone_error_out:
134872 if( openedTransaction ){
134873 sqlite3BtreeCommit(pDb->pBt);
134874 }
134875 sqlite3BtreeLeave(pDb->pBt);
134876
134877error_out:
134878 if( rc ){
134879 if( rc==SQLITE_NOMEM7 || rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
134880 sqlite3OomFault(db);
134881 }
134882 sqlite3ResetOneSchema(db, iDb);
134883 }
134884 db->init.busy = 0;
134885 return rc;
134886}
134887
134888/*
134889** Initialize all database files - the main database file, the file
134890** used to store temporary tables, and any additional database files
134891** created using ATTACH statements. Return a success code. If an
134892** error occurs, write an error message into *pzErrMsg.
134893**
134894** After a database is initialized, the DB_SchemaLoaded bit is set
134895** bit is set in the flags field of the Db structure.
134896*/
134897SQLITE_PRIVATEstatic int sqlite3Init(sqlite3 *db, char **pzErrMsg){
134898 int i, rc;
134899 int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange0x0001);
134900
134901 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
134902 assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) )((void) (0));
134903 assert( db->init.busy==0 )((void) (0));
134904 ENC(db)((db)->enc) = SCHEMA_ENC(db)((db)->aDb[0].pSchema->enc);
134905 assert( db->nDb>0 )((void) (0));
134906 /* Do the main schema first */
134907 if( !DbHasProperty(db, 0, DB_SchemaLoaded)(((db)->aDb[0].pSchema->schemaFlags&(0x0001))==(0x0001
))
){
134908 rc = sqlite3InitOne(db, 0, pzErrMsg, 0);
134909 if( rc ) return rc;
134910 }
134911 /* All other schemas after the main schema. The "temp" schema must be last */
134912 for(i=db->nDb-1; i>0; i--){
134913 assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) )((void) (0));
134914 if( !DbHasProperty(db, i, DB_SchemaLoaded)(((db)->aDb[i].pSchema->schemaFlags&(0x0001))==(0x0001
))
){
134915 rc = sqlite3InitOne(db, i, pzErrMsg, 0);
134916 if( rc ) return rc;
134917 }
134918 }
134919 if( commit_internal ){
134920 sqlite3CommitInternalChanges(db);
134921 }
134922 return SQLITE_OK0;
134923}
134924
134925/*
134926** This routine is a no-op if the database schema is already initialized.
134927** Otherwise, the schema is loaded. An error code is returned.
134928*/
134929SQLITE_PRIVATEstatic int sqlite3ReadSchema(Parse *pParse){
134930 int rc = SQLITE_OK0;
134931 sqlite3 *db = pParse->db;
134932 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
134933 if( !db->init.busy ){
134934 rc = sqlite3Init(db, &pParse->zErrMsg);
134935 if( rc!=SQLITE_OK0 ){
134936 pParse->rc = rc;
134937 pParse->nErr++;
134938 }else if( db->noSharedCache ){
134939 db->mDbFlags |= DBFLAG_SchemaKnownOk0x0010;
134940 }
134941 }
134942 return rc;
134943}
134944
134945
134946/*
134947** Check schema cookies in all databases. If any cookie is out
134948** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies
134949** make no changes to pParse->rc.
134950*/
134951static void schemaIsValid(Parse *pParse){
134952 sqlite3 *db = pParse->db;
134953 int iDb;
134954 int rc;
134955 int cookie;
134956
134957 assert( pParse->checkSchema )((void) (0));
134958 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
134959 for(iDb=0; iDb<db->nDb; iDb++){
134960 int openedTransaction = 0; /* True if a transaction is opened */
134961 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
134962 if( pBt==0 ) continue;
134963
134964 /* If there is not already a read-only (or read-write) transaction opened
134965 ** on the b-tree database, open one now. If a transaction is opened, it
134966 ** will be closed immediately after reading the meta-value. */
134967 if( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_NONE0 ){
134968 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
134969 if( rc==SQLITE_NOMEM7 || rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
134970 sqlite3OomFault(db);
134971 pParse->rc = SQLITE_NOMEM7;
134972 }
134973 if( rc!=SQLITE_OK0 ) return;
134974 openedTransaction = 1;
134975 }
134976
134977 /* Read the schema cookie from the database. If it does not match the
134978 ** value stored as part of the in-memory schema representation,
134979 ** set Parse.rc to SQLITE_SCHEMA. */
134980 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION1, (u32 *)&cookie);
134981 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
134982 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
134983 sqlite3ResetOneSchema(db, iDb);
134984 pParse->rc = SQLITE_SCHEMA17;
134985 }
134986
134987 /* Close the transaction, if one was opened. */
134988 if( openedTransaction ){
134989 sqlite3BtreeCommit(pBt);
134990 }
134991 }
134992}
134993
134994/*
134995** Convert a schema pointer into the iDb index that indicates
134996** which database file in db->aDb[] the schema refers to.
134997**
134998** If the same database is attached more than once, the first
134999** attached database is returned.
135000*/
135001SQLITE_PRIVATEstatic int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
135002 int i = -32768;
135003
135004 /* If pSchema is NULL, then return -32768. This happens when code in
135005 ** expr.c is trying to resolve a reference to a transient table (i.e. one
135006 ** created by a sub-select). In this case the return value of this
135007 ** function should never be used.
135008 **
135009 ** We return -32768 instead of the more usual -1 simply because using
135010 ** -32768 as the incorrect index into db->aDb[] is much
135011 ** more likely to cause a segfault than -1 (of course there are assert()
135012 ** statements too, but it never hurts to play the odds) and
135013 ** -32768 will still fit into a 16-bit signed integer.
135014 */
135015 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
135016 if( pSchema ){
135017 for(i=0; 1; i++){
135018 assert( i<db->nDb )((void) (0));
135019 if( db->aDb[i].pSchema==pSchema ){
135020 break;
135021 }
135022 }
135023 assert( i>=0 && i<db->nDb )((void) (0));
135024 }
135025 return i;
135026}
135027
135028/*
135029** Free all memory allocations in the pParse object
135030*/
135031SQLITE_PRIVATEstatic void sqlite3ParseObjectReset(Parse *pParse){
135032 sqlite3 *db = pParse->db;
135033 assert( db!=0 )((void) (0));
135034 assert( db->pParse==pParse )((void) (0));
135035 assert( pParse->nested==0 )((void) (0));
135036#ifndef SQLITE_OMIT_SHARED_CACHE
135037 sqlite3DbFree(db, pParse->aTableLock);
135038#endif
135039 while( pParse->pCleanup ){
135040 ParseCleanup *pCleanup = pParse->pCleanup;
135041 pParse->pCleanup = pCleanup->pNext;
135042 pCleanup->xCleanup(db, pCleanup->pPtr);
135043 sqlite3DbFreeNN(db, pCleanup);
135044 }
135045 sqlite3DbFree(db, pParse->aLabel);
135046 if( pParse->pConstExpr ){
135047 sqlite3ExprListDelete(db, pParse->pConstExpr);
135048 }
135049 assert( db->lookaside.bDisable >= pParse->disableLookaside )((void) (0));
135050 db->lookaside.bDisable -= pParse->disableLookaside;
135051 db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue;
135052 assert( pParse->db->pParse==pParse )((void) (0));
135053 db->pParse = pParse->pOuterParse;
135054 pParse->db = 0;
135055 pParse->disableLookaside = 0;
135056}
135057
135058/*
135059** Add a new cleanup operation to a Parser. The cleanup should happen when
135060** the parser object is destroyed. But, beware: the cleanup might happen
135061** immediately.
135062**
135063** Use this mechanism for uncommon cleanups. There is a higher setup
135064** cost for this mechansim (an extra malloc), so it should not be used
135065** for common cleanups that happen on most calls. But for less
135066** common cleanups, we save a single NULL-pointer comparison in
135067** sqlite3ParseObjectReset(), which reduces the total CPU cycle count.
135068**
135069** If a memory allocation error occurs, then the cleanup happens immediately.
135070** When either SQLITE_DEBUG or SQLITE_COVERAGE_TEST are defined, the
135071** pParse->earlyCleanup flag is set in that case. Calling code show verify
135072** that test cases exist for which this happens, to guard against possible
135073** use-after-free errors following an OOM. The preferred way to do this is
135074** to immediately follow the call to this routine with:
135075**
135076** testcase( pParse->earlyCleanup );
135077**
135078** This routine returns a copy of its pPtr input (the third parameter)
135079** except if an early cleanup occurs, in which case it returns NULL. So
135080** another way to check for early cleanup is to check the return value.
135081** Or, stop using the pPtr parameter with this call and use only its
135082** return value thereafter. Something like this:
135083**
135084** pObj = sqlite3ParserAddCleanup(pParse, destructor, pObj);
135085*/
135086SQLITE_PRIVATEstatic void *sqlite3ParserAddCleanup(
135087 Parse *pParse, /* Destroy when this Parser finishes */
135088 void (*xCleanup)(sqlite3*,void*), /* The cleanup routine */
135089 void *pPtr /* Pointer to object to be cleaned up */
135090){
135091 ParseCleanup *pCleanup = sqlite3DbMallocRaw(pParse->db, sizeof(*pCleanup));
135092 if( pCleanup ){
135093 pCleanup->pNext = pParse->pCleanup;
135094 pParse->pCleanup = pCleanup;
135095 pCleanup->pPtr = pPtr;
135096 pCleanup->xCleanup = xCleanup;
135097 }else{
135098 xCleanup(pParse->db, pPtr);
135099 pPtr = 0;
135100#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
135101 pParse->earlyCleanup = 1;
135102#endif
135103 }
135104 return pPtr;
135105}
135106
135107/*
135108** Turn bulk memory into a valid Parse object and link that Parse object
135109** into database connection db.
135110**
135111** Call sqlite3ParseObjectReset() to undo this operation.
135112**
135113** Caution: Do not confuse this routine with sqlite3ParseObjectInit() which
135114** is generated by Lemon.
135115*/
135116SQLITE_PRIVATEstatic void sqlite3ParseObjectInit(Parse *pParse, sqlite3 *db){
135117 memset(PARSE_HDR(pParse)(((char*)(pParse))+__builtin_offsetof(Parse, zErrMsg)), 0, PARSE_HDR_SZ(__builtin_offsetof(Parse, aTempReg)-__builtin_offsetof(Parse
, zErrMsg))
);
135118 memset(PARSE_TAIL(pParse)(((char*)(pParse))+__builtin_offsetof(Parse, sLastToken)), 0, PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken)));
135119 assert( db->pParse!=pParse )((void) (0));
135120 pParse->pOuterParse = db->pParse;
135121 db->pParse = pParse;
135122 pParse->db = db;
135123 if( db->mallocFailed ) sqlite3ErrorMsg(pParse, "out of memory");
135124}
135125
135126/*
135127** Maximum number of times that we will try again to prepare a statement
135128** that returns SQLITE_ERROR_RETRY.
135129*/
135130#ifndef SQLITE_MAX_PREPARE_RETRY25
135131# define SQLITE_MAX_PREPARE_RETRY25 25
135132#endif
135133
135134/*
135135** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
135136*/
135137static int sqlite3Prepare(
135138 sqlite3 *db, /* Database handle. */
135139 const char *zSql, /* UTF-8 encoded SQL statement. */
135140 int nBytes, /* Length of zSql in bytes. */
135141 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
135142 Vdbe *pReprepare, /* VM being reprepared */
135143 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
135144 const char **pzTail /* OUT: End of parsed string */
135145){
135146 int rc = SQLITE_OK0; /* Result code */
135147 int i; /* Loop counter */
135148 Parse sParse; /* Parsing context */
135149
135150 /* sqlite3ParseObjectInit(&sParse, db); // inlined for performance */
135151 memset(PARSE_HDR(&sParse)(((char*)(&sParse))+__builtin_offsetof(Parse, zErrMsg)), 0, PARSE_HDR_SZ(__builtin_offsetof(Parse, aTempReg)-__builtin_offsetof(Parse
, zErrMsg))
);
135152 memset(PARSE_TAIL(&sParse)(((char*)(&sParse))+__builtin_offsetof(Parse, sLastToken)
)
, 0, PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken)));
135153 sParse.pOuterParse = db->pParse;
135154 db->pParse = &sParse;
135155 sParse.db = db;
135156 sParse.pReprepare = pReprepare;
135157 assert( ppStmt && *ppStmt==0 )((void) (0));
135158 if( db->mallocFailed ) sqlite3ErrorMsg(&sParse, "out of memory");
135159 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
135160
135161 /* For a long-term use prepared statement avoid the use of
135162 ** lookaside memory.
135163 */
135164 if( prepFlags & SQLITE_PREPARE_PERSISTENT0x01 ){
135165 sParse.disableLookaside++;
135166 DisableLookasidedb->lookaside.bDisable++;db->lookaside.sz=0;
135167 }
135168 sParse.disableVtab = (prepFlags & SQLITE_PREPARE_NO_VTAB0x04)!=0;
135169
135170 /* Check to verify that it is possible to get a read lock on all
135171 ** database schemas. The inability to get a read lock indicates that
135172 ** some other database connection is holding a write-lock, which in
135173 ** turn means that the other connection has made uncommitted changes
135174 ** to the schema.
135175 **
135176 ** Were we to proceed and prepare the statement against the uncommitted
135177 ** schema changes and if those schema changes are subsequently rolled
135178 ** back and different changes are made in their place, then when this
135179 ** prepared statement goes to run the schema cookie would fail to detect
135180 ** the schema change. Disaster would follow.
135181 **
135182 ** This thread is currently holding mutexes on all Btrees (because
135183 ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
135184 ** is not possible for another thread to start a new schema change
135185 ** while this routine is running. Hence, we do not need to hold
135186 ** locks on the schema, we just need to make sure nobody else is
135187 ** holding them.
135188 **
135189 ** Note that setting READ_UNCOMMITTED overrides most lock detection,
135190 ** but it does *not* override schema lock detection, so this all still
135191 ** works even if READ_UNCOMMITTED is set.
135192 */
135193 if( !db->noSharedCache ){
135194 for(i=0; i<db->nDb; i++) {
135195 Btree *pBt = db->aDb[i].pBt;
135196 if( pBt ){
135197 assert( sqlite3BtreeHoldsMutex(pBt) )((void) (0));
135198 rc = sqlite3BtreeSchemaLocked(pBt);
135199 if( rc ){
135200 const char *zDb = db->aDb[i].zDbSName;
135201 sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb);
135202 testcase( db->flags & SQLITE_ReadUncommit );
135203 goto end_prepare;
135204 }
135205 }
135206 }
135207 }
135208
135209 sqlite3VtabUnlockList(db);
135210
135211 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
135212 char *zSqlCopy;
135213 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH1];
135214 testcase( nBytes==mxLen );
135215 testcase( nBytes==mxLen+1 );
135216 if( nBytes>mxLen ){
135217 sqlite3ErrorWithMsg(db, SQLITE_TOOBIG18, "statement too long");
135218 rc = sqlite3ApiExit(db, SQLITE_TOOBIG18);
135219 goto end_prepare;
135220 }
135221 zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
135222 if( zSqlCopy ){
135223 sqlite3RunParser(&sParse, zSqlCopy);
135224 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
135225 sqlite3DbFree(db, zSqlCopy);
135226 }else{
135227 sParse.zTail = &zSql[nBytes];
135228 }
135229 }else{
135230 sqlite3RunParser(&sParse, zSql);
135231 }
135232 assert( 0==sParse.nQueryLoop )((void) (0));
135233
135234 if( pzTail ){
135235 *pzTail = sParse.zTail;
135236 }
135237
135238 if( db->init.busy==0 ){
135239 sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
135240 }
135241 if( db->mallocFailed ){
135242 sParse.rc = SQLITE_NOMEM_BKPT7;
135243 sParse.checkSchema = 0;
135244 }
135245 if( sParse.rc!=SQLITE_OK0 && sParse.rc!=SQLITE_DONE101 ){
135246 if( sParse.checkSchema && db->init.busy==0 ){
135247 schemaIsValid(&sParse);
135248 }
135249 if( sParse.pVdbe ){
135250 sqlite3VdbeFinalize(sParse.pVdbe);
135251 }
135252 assert( 0==(*ppStmt) )((void) (0));
135253 rc = sParse.rc;
135254 if( sParse.zErrMsg ){
135255 sqlite3ErrorWithMsg(db, rc, "%s", sParse.zErrMsg);
135256 sqlite3DbFree(db, sParse.zErrMsg);
135257 }else{
135258 sqlite3Error(db, rc);
135259 }
135260 }else{
135261 assert( sParse.zErrMsg==0 )((void) (0));
135262 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
135263 rc = SQLITE_OK0;
135264 sqlite3ErrorClear(db);
135265 }
135266
135267
135268 /* Delete any TriggerPrg structures allocated while parsing this statement. */
135269 while( sParse.pTriggerPrg ){
135270 TriggerPrg *pT = sParse.pTriggerPrg;
135271 sParse.pTriggerPrg = pT->pNext;
135272 sqlite3DbFree(db, pT);
135273 }
135274
135275end_prepare:
135276
135277 sqlite3ParseObjectReset(&sParse);
135278 return rc;
135279}
135280static int sqlite3LockAndPrepare(
135281 sqlite3 *db, /* Database handle. */
135282 const char *zSql, /* UTF-8 encoded SQL statement. */
135283 int nBytes, /* Length of zSql in bytes. */
135284 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
135285 Vdbe *pOld, /* VM being reprepared */
135286 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
135287 const char **pzTail /* OUT: End of parsed string */
135288){
135289 int rc;
135290 int cnt = 0;
135291
135292#ifdef SQLITE_ENABLE_API_ARMOR1
135293 if( ppStmt==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(135293);
135294#endif
135295 *ppStmt = 0;
135296 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
135297 return SQLITE_MISUSE_BKPTsqlite3MisuseError(135297);
135298 }
135299 sqlite3_mutex_enter(db->mutex);
135300 sqlite3BtreeEnterAll(db);
135301 do{
135302 /* Make multiple attempts to compile the SQL, until it either succeeds
135303 ** or encounters a permanent error. A schema problem after one schema
135304 ** reset is considered a permanent error. */
135305 rc = sqlite3Prepare(db, zSql, nBytes, prepFlags, pOld, ppStmt, pzTail);
135306 assert( rc==SQLITE_OK || *ppStmt==0 )((void) (0));
135307 if( rc==SQLITE_OK0 || db->mallocFailed ) break;
135308 }while( (rc==SQLITE_ERROR_RETRY(1 | (2<<8)) && (cnt++)<SQLITE_MAX_PREPARE_RETRY25)
135309 || (rc==SQLITE_SCHEMA17 && (sqlite3ResetOneSchema(db,-1), cnt++)==0) );
135310 sqlite3BtreeLeaveAll(db);
135311 rc = sqlite3ApiExit(db, rc);
135312 assert( (rc&db->errMask)==rc )((void) (0));
135313 db->busyHandler.nBusy = 0;
135314 sqlite3_mutex_leave(db->mutex);
135315 return rc;
135316}
135317
135318
135319/*
135320** Rerun the compilation of a statement after a schema change.
135321**
135322** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
135323** if the statement cannot be recompiled because another connection has
135324** locked the sqlite3_schema table, return SQLITE_LOCKED. If any other error
135325** occurs, return SQLITE_SCHEMA.
135326*/
135327SQLITE_PRIVATEstatic int sqlite3Reprepare(Vdbe *p){
135328 int rc;
135329 sqlite3_stmt *pNew;
135330 const char *zSql;
135331 sqlite3 *db;
135332 u8 prepFlags;
135333
135334 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) )((void) (0));
135335 zSql = sqlite3_sql((sqlite3_stmt *)p);
135336 assert( zSql!=0 )((void) (0)); /* Reprepare only called for prepare_v2() statements */
135337 db = sqlite3VdbeDb(p);
135338 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
135339 prepFlags = sqlite3VdbePrepareFlags(p);
135340 rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0);
135341 if( rc ){
135342 if( rc==SQLITE_NOMEM7 ){
135343 sqlite3OomFault(db);
135344 }
135345 assert( pNew==0 )((void) (0));
135346 return rc;
135347 }else{
135348 assert( pNew!=0 )((void) (0));
135349 }
135350 sqlite3VdbeSwap((Vdbe*)pNew, p);
135351 sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
135352 sqlite3VdbeResetStepResult((Vdbe*)pNew);
135353 sqlite3VdbeFinalize((Vdbe*)pNew);
135354 return SQLITE_OK0;
135355}
135356
135357
135358/*
135359** Two versions of the official API. Legacy and new use. In the legacy
135360** version, the original SQL text is not saved in the prepared statement
135361** and so if a schema change occurs, SQLITE_SCHEMA is returned by
135362** sqlite3_step(). In the new version, the original SQL text is retained
135363** and the statement is automatically recompiled if an schema change
135364** occurs.
135365*/
135366SQLITE_API int sqlite3_prepare(
135367 sqlite3 *db, /* Database handle. */
135368 const char *zSql, /* UTF-8 encoded SQL statement. */
135369 int nBytes, /* Length of zSql in bytes. */
135370 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
135371 const char **pzTail /* OUT: End of parsed string */
135372){
135373 int rc;
135374 rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail);
135375 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 )((void) (0)); /* VERIFY: F13021 */
135376 return rc;
135377}
135378SQLITE_API int sqlite3_prepare_v2(
135379 sqlite3 *db, /* Database handle. */
135380 const char *zSql, /* UTF-8 encoded SQL statement. */
135381 int nBytes, /* Length of zSql in bytes. */
135382 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
135383 const char **pzTail /* OUT: End of parsed string */
135384){
135385 int rc;
135386 /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
135387 ** exactly the same as sqlite3_prepare_v3() with a zero prepFlags
135388 ** parameter.
135389 **
135390 ** Proof in that the 5th parameter to sqlite3LockAndPrepare is 0 */
135391 rc = sqlite3LockAndPrepare(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL0x80,0,
135392 ppStmt,pzTail);
135393 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 )((void) (0));
135394 return rc;
135395}
135396SQLITE_API int sqlite3_prepare_v3(
135397 sqlite3 *db, /* Database handle. */
135398 const char *zSql, /* UTF-8 encoded SQL statement. */
135399 int nBytes, /* Length of zSql in bytes. */
135400 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
135401 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
135402 const char **pzTail /* OUT: End of parsed string */
135403){
135404 int rc;
135405 /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
135406 ** sqlite3_prepare_v2() only in having the extra prepFlags parameter,
135407 ** which is a bit array consisting of zero or more of the
135408 ** SQLITE_PREPARE_* flags.
135409 **
135410 ** Proof by comparison to the implementation of sqlite3_prepare_v2()
135411 ** directly above. */
135412 rc = sqlite3LockAndPrepare(db,zSql,nBytes,
135413 SQLITE_PREPARE_SAVESQL0x80|(prepFlags&SQLITE_PREPARE_MASK0x0f),
135414 0,ppStmt,pzTail);
135415 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 )((void) (0));
135416 return rc;
135417}
135418
135419
135420#ifndef SQLITE_OMIT_UTF16
135421/*
135422** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
135423*/
135424static int sqlite3Prepare16(
135425 sqlite3 *db, /* Database handle. */
135426 const void *zSql, /* UTF-16 encoded SQL statement. */
135427 int nBytes, /* Length of zSql in bytes. */
135428 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
135429 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
135430 const void **pzTail /* OUT: End of parsed string */
135431){
135432 /* This function currently works by first transforming the UTF-16
135433 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
135434 ** tricky bit is figuring out the pointer to return in *pzTail.
135435 */
135436 char *zSql8;
135437 const char *zTail8 = 0;
135438 int rc = SQLITE_OK0;
135439
135440#ifdef SQLITE_ENABLE_API_ARMOR1
135441 if( ppStmt==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(135441);
135442#endif
135443 *ppStmt = 0;
135444 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
135445 return SQLITE_MISUSE_BKPTsqlite3MisuseError(135445);
135446 }
135447 if( nBytes>=0 ){
135448 int sz;
135449 const char *z = (const char*)zSql;
135450 for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){}
135451 nBytes = sz;
135452 }
135453 sqlite3_mutex_enter(db->mutex);
135454 zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE2);
135455 if( zSql8 ){
135456 rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8);
135457 }
135458
135459 if( zTail8 && pzTail ){
135460 /* If sqlite3_prepare returns a tail pointer, we calculate the
135461 ** equivalent pointer into the UTF-16 string by counting the unicode
135462 ** characters between zSql8 and zTail8, and then returning a pointer
135463 ** the same number of characters into the UTF-16 string.
135464 */
135465 int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
135466 *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
135467 }
135468 sqlite3DbFree(db, zSql8);
135469 rc = sqlite3ApiExit(db, rc);
135470 sqlite3_mutex_leave(db->mutex);
135471 return rc;
135472}
135473
135474/*
135475** Two versions of the official API. Legacy and new use. In the legacy
135476** version, the original SQL text is not saved in the prepared statement
135477** and so if a schema change occurs, SQLITE_SCHEMA is returned by
135478** sqlite3_step(). In the new version, the original SQL text is retained
135479** and the statement is automatically recompiled if an schema change
135480** occurs.
135481*/
135482SQLITE_API int sqlite3_prepare16(
135483 sqlite3 *db, /* Database handle. */
135484 const void *zSql, /* UTF-16 encoded SQL statement. */
135485 int nBytes, /* Length of zSql in bytes. */
135486 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
135487 const void **pzTail /* OUT: End of parsed string */
135488){
135489 int rc;
135490 rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
135491 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 )((void) (0)); /* VERIFY: F13021 */
135492 return rc;
135493}
135494SQLITE_API int sqlite3_prepare16_v2(
135495 sqlite3 *db, /* Database handle. */
135496 const void *zSql, /* UTF-16 encoded SQL statement. */
135497 int nBytes, /* Length of zSql in bytes. */
135498 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
135499 const void **pzTail /* OUT: End of parsed string */
135500){
135501 int rc;
135502 rc = sqlite3Prepare16(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL0x80,ppStmt,pzTail);
135503 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 )((void) (0)); /* VERIFY: F13021 */
135504 return rc;
135505}
135506SQLITE_API int sqlite3_prepare16_v3(
135507 sqlite3 *db, /* Database handle. */
135508 const void *zSql, /* UTF-16 encoded SQL statement. */
135509 int nBytes, /* Length of zSql in bytes. */
135510 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
135511 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
135512 const void **pzTail /* OUT: End of parsed string */
135513){
135514 int rc;
135515 rc = sqlite3Prepare16(db,zSql,nBytes,
135516 SQLITE_PREPARE_SAVESQL0x80|(prepFlags&SQLITE_PREPARE_MASK0x0f),
135517 ppStmt,pzTail);
135518 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 )((void) (0)); /* VERIFY: F13021 */
135519 return rc;
135520}
135521
135522#endif /* SQLITE_OMIT_UTF16 */
135523
135524/************** End of prepare.c *********************************************/
135525/************** Begin file select.c ******************************************/
135526/*
135527** 2001 September 15
135528**
135529** The author disclaims copyright to this source code. In place of
135530** a legal notice, here is a blessing:
135531**
135532** May you do good and not evil.
135533** May you find forgiveness for yourself and forgive others.
135534** May you share freely, never taking more than you give.
135535**
135536*************************************************************************
135537** This file contains C code routines that are called by the parser
135538** to handle SELECT statements in SQLite.
135539*/
135540/* #include "sqliteInt.h" */
135541
135542/*
135543** An instance of the following object is used to record information about
135544** how to process the DISTINCT keyword, to simplify passing that information
135545** into the selectInnerLoop() routine.
135546*/
135547typedef struct DistinctCtx DistinctCtx;
135548struct DistinctCtx {
135549 u8 isTnct; /* 0: Not distinct. 1: DISTICT 2: DISTINCT and ORDER BY */
135550 u8 eTnctType; /* One of the WHERE_DISTINCT_* operators */
135551 int tabTnct; /* Ephemeral table used for DISTINCT processing */
135552 int addrTnct; /* Address of OP_OpenEphemeral opcode for tabTnct */
135553};
135554
135555/*
135556** An instance of the following object is used to record information about
135557** the ORDER BY (or GROUP BY) clause of query is being coded.
135558**
135559** The aDefer[] array is used by the sorter-references optimization. For
135560** example, assuming there is no index that can be used for the ORDER BY,
135561** for the query:
135562**
135563** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10;
135564**
135565** it may be more efficient to add just the "a" values to the sorter, and
135566** retrieve the associated "bigblob" values directly from table t1 as the
135567** 10 smallest "a" values are extracted from the sorter.
135568**
135569** When the sorter-reference optimization is used, there is one entry in the
135570** aDefer[] array for each database table that may be read as values are
135571** extracted from the sorter.
135572*/
135573typedef struct SortCtx SortCtx;
135574struct SortCtx {
135575 ExprList *pOrderBy; /* The ORDER BY (or GROUP BY clause) */
135576 int nOBSat; /* Number of ORDER BY terms satisfied by indices */
135577 int iECursor; /* Cursor number for the sorter */
135578 int regReturn; /* Register holding block-output return address */
135579 int labelBkOut; /* Start label for the block-output subroutine */
135580 int addrSortIndex; /* Address of the OP_SorterOpen or OP_OpenEphemeral */
135581 int labelDone; /* Jump here when done, ex: LIMIT reached */
135582 int labelOBLopt; /* Jump here when sorter is full */
135583 u8 sortFlags; /* Zero or more SORTFLAG_* bits */
135584#ifdef SQLITE_ENABLE_SORTER_REFERENCES
135585 u8 nDefer; /* Number of valid entries in aDefer[] */
135586 struct DeferredCsr {
135587 Table *pTab; /* Table definition */
135588 int iCsr; /* Cursor number for table */
135589 int nKey; /* Number of PK columns for table pTab (>=1) */
135590 } aDefer[4];
135591#endif
135592 struct RowLoadInfo *pDeferredRowLoad; /* Deferred row loading info or NULL */
135593};
135594#define SORTFLAG_UseSorter0x01 0x01 /* Use SorterOpen instead of OpenEphemeral */
135595
135596/*
135597** Delete all the content of a Select structure. Deallocate the structure
135598** itself depending on the value of bFree
135599**
135600** If bFree==1, call sqlite3DbFree() on the p object.
135601** If bFree==0, Leave the first Select object unfreed
135602*/
135603static void clearSelect(sqlite3 *db, Select *p, int bFree){
135604 while( p ){
135605 Select *pPrior = p->pPrior;
135606 sqlite3ExprListDelete(db, p->pEList);
135607 sqlite3SrcListDelete(db, p->pSrc);
135608 sqlite3ExprDelete(db, p->pWhere);
135609 sqlite3ExprListDelete(db, p->pGroupBy);
135610 sqlite3ExprDelete(db, p->pHaving);
135611 sqlite3ExprListDelete(db, p->pOrderBy);
135612 sqlite3ExprDelete(db, p->pLimit);
135613 if( OK_IF_ALWAYS_TRUE(p->pWith)(p->pWith) ) sqlite3WithDelete(db, p->pWith);
135614#ifndef SQLITE_OMIT_WINDOWFUNC
135615 if( OK_IF_ALWAYS_TRUE(p->pWinDefn)(p->pWinDefn) ){
135616 sqlite3WindowListDelete(db, p->pWinDefn);
135617 }
135618 while( p->pWin ){
135619 assert( p->pWin->ppThis==&p->pWin )((void) (0));
135620 sqlite3WindowUnlinkFromSelect(p->pWin);
135621 }
135622#endif
135623 if( bFree ) sqlite3DbFreeNN(db, p);
135624 p = pPrior;
135625 bFree = 1;
135626 }
135627}
135628
135629/*
135630** Initialize a SelectDest structure.
135631*/
135632SQLITE_PRIVATEstatic void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
135633 pDest->eDest = (u8)eDest;
135634 pDest->iSDParm = iParm;
135635 pDest->iSDParm2 = 0;
135636 pDest->zAffSdst = 0;
135637 pDest->iSdst = 0;
135638 pDest->nSdst = 0;
135639}
135640
135641
135642/*
135643** Allocate a new Select structure and return a pointer to that
135644** structure.
135645*/
135646SQLITE_PRIVATEstatic Select *sqlite3SelectNew(
135647 Parse *pParse, /* Parsing context */
135648 ExprList *pEList, /* which columns to include in the result */
135649 SrcList *pSrc, /* the FROM clause -- which tables to scan */
135650 Expr *pWhere, /* the WHERE clause */
135651 ExprList *pGroupBy, /* the GROUP BY clause */
135652 Expr *pHaving, /* the HAVING clause */
135653 ExprList *pOrderBy, /* the ORDER BY clause */
135654 u32 selFlags, /* Flag parameters, such as SF_Distinct */
135655 Expr *pLimit /* LIMIT value. NULL means not used */
135656){
135657 Select *pNew, *pAllocated;
135658 Select standin;
135659 pAllocated = pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
135660 if( pNew==0 ){
135661 assert( pParse->db->mallocFailed )((void) (0));
135662 pNew = &standin;
135663 }
135664 if( pEList==0 ){
135665 pEList = sqlite3ExprListAppend(pParse, 0,
135666 sqlite3Expr(pParse->db,TK_ASTERISK180,0));
135667 }
135668 pNew->pEList = pEList;
135669 pNew->op = TK_SELECT138;
135670 pNew->selFlags = selFlags;
135671 pNew->iLimit = 0;
135672 pNew->iOffset = 0;
135673 pNew->selId = ++pParse->nSelect;
135674 pNew->addrOpenEphm[0] = -1;
135675 pNew->addrOpenEphm[1] = -1;
135676 pNew->nSelectRow = 0;
135677 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*pSrc));
135678 pNew->pSrc = pSrc;
135679 pNew->pWhere = pWhere;
135680 pNew->pGroupBy = pGroupBy;
135681 pNew->pHaving = pHaving;
135682 pNew->pOrderBy = pOrderBy;
135683 pNew->pPrior = 0;
135684 pNew->pNext = 0;
135685 pNew->pLimit = pLimit;
135686 pNew->pWith = 0;
135687#ifndef SQLITE_OMIT_WINDOWFUNC
135688 pNew->pWin = 0;
135689 pNew->pWinDefn = 0;
135690#endif
135691 if( pParse->db->mallocFailed ) {
135692 clearSelect(pParse->db, pNew, pNew!=&standin);
135693 pAllocated = 0;
135694 }else{
135695 assert( pNew->pSrc!=0 || pParse->nErr>0 )((void) (0));
135696 }
135697 return pAllocated;
135698}
135699
135700
135701/*
135702** Delete the given Select structure and all of its substructures.
135703*/
135704SQLITE_PRIVATEstatic void sqlite3SelectDelete(sqlite3 *db, Select *p){
135705 if( OK_IF_ALWAYS_TRUE(p)(p) ) clearSelect(db, p, 1);
135706}
135707
135708/*
135709** Return a pointer to the right-most SELECT statement in a compound.
135710*/
135711static Select *findRightmost(Select *p){
135712 while( p->pNext ) p = p->pNext;
135713 return p;
135714}
135715
135716/*
135717** Given 1 to 3 identifiers preceding the JOIN keyword, determine the
135718** type of join. Return an integer constant that expresses that type
135719** in terms of the following bit values:
135720**
135721** JT_INNER
135722** JT_CROSS
135723** JT_OUTER
135724** JT_NATURAL
135725** JT_LEFT
135726** JT_RIGHT
135727**
135728** A full outer join is the combination of JT_LEFT and JT_RIGHT.
135729**
135730** If an illegal or unsupported join type is seen, then still return
135731** a join type, but put an error in the pParse structure.
135732**
135733** These are the valid join types:
135734**
135735**
135736** pA pB pC Return Value
135737** ------- ----- ----- ------------
135738** CROSS - - JT_CROSS
135739** INNER - - JT_INNER
135740** LEFT - - JT_LEFT|JT_OUTER
135741** LEFT OUTER - JT_LEFT|JT_OUTER
135742** RIGHT - - JT_RIGHT|JT_OUTER
135743** RIGHT OUTER - JT_RIGHT|JT_OUTER
135744** FULL - - JT_LEFT|JT_RIGHT|JT_OUTER
135745** FULL OUTER - JT_LEFT|JT_RIGHT|JT_OUTER
135746** NATURAL INNER - JT_NATURAL|JT_INNER
135747** NATURAL LEFT - JT_NATURAL|JT_LEFT|JT_OUTER
135748** NATURAL LEFT OUTER JT_NATURAL|JT_LEFT|JT_OUTER
135749** NATURAL RIGHT - JT_NATURAL|JT_RIGHT|JT_OUTER
135750** NATURAL RIGHT OUTER JT_NATURAL|JT_RIGHT|JT_OUTER
135751** NATURAL FULL - JT_NATURAL|JT_LEFT|JT_RIGHT
135752** NATURAL FULL OUTER JT_NATRUAL|JT_LEFT|JT_RIGHT
135753**
135754** To preserve historical compatibly, SQLite also accepts a variety
135755** of other non-standard and in many cases non-sensical join types.
135756** This routine makes as much sense at it can from the nonsense join
135757** type and returns a result. Examples of accepted nonsense join types
135758** include but are not limited to:
135759**
135760** INNER CROSS JOIN -> same as JOIN
135761** NATURAL CROSS JOIN -> same as NATURAL JOIN
135762** OUTER LEFT JOIN -> same as LEFT JOIN
135763** LEFT NATURAL JOIN -> same as NATURAL LEFT JOIN
135764** LEFT RIGHT JOIN -> same as FULL JOIN
135765** RIGHT OUTER FULL JOIN -> same as FULL JOIN
135766** CROSS CROSS CROSS JOIN -> same as JOIN
135767**
135768** The only restrictions on the join type name are:
135769**
135770** * "INNER" cannot appear together with "OUTER", "LEFT", "RIGHT",
135771** or "FULL".
135772**
135773** * "CROSS" cannot appear together with "OUTER", "LEFT", "RIGHT,
135774** or "FULL".
135775**
135776** * If "OUTER" is present then there must also be one of
135777** "LEFT", "RIGHT", or "FULL"
135778*/
135779SQLITE_PRIVATEstatic int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
135780 int jointype = 0;
135781 Token *apAll[3];
135782 Token *p;
135783 /* 0123456789 123456789 123456789 123 */
135784 static const char zKeyText[] = "naturaleftouterightfullinnercross";
135785 static const struct {
135786 u8 i; /* Beginning of keyword text in zKeyText[] */
135787 u8 nChar; /* Length of the keyword in characters */
135788 u8 code; /* Join type mask */
135789 } aKeyword[] = {
135790 /* (0) natural */ { 0, 7, JT_NATURAL0x04 },
135791 /* (1) left */ { 6, 4, JT_LEFT0x08|JT_OUTER0x20 },
135792 /* (2) outer */ { 10, 5, JT_OUTER0x20 },
135793 /* (3) right */ { 14, 5, JT_RIGHT0x10|JT_OUTER0x20 },
135794 /* (4) full */ { 19, 4, JT_LEFT0x08|JT_RIGHT0x10|JT_OUTER0x20 },
135795 /* (5) inner */ { 23, 5, JT_INNER0x01 },
135796 /* (6) cross */ { 28, 5, JT_INNER0x01|JT_CROSS0x02 },
135797 };
135798 int i, j;
135799 apAll[0] = pA;
135800 apAll[1] = pB;
135801 apAll[2] = pC;
135802 for(i=0; i<3 && apAll[i]; i++){
135803 p = apAll[i];
135804 for(j=0; j<ArraySize(aKeyword)((int)(sizeof(aKeyword)/sizeof(aKeyword[0]))); j++){
135805 if( p->n==aKeyword[j].nChar
135806 && sqlite3StrNICmpsqlite3_strnicmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
135807 jointype |= aKeyword[j].code;
135808 break;
135809 }
135810 }
135811 testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
135812 if( j>=ArraySize(aKeyword)((int)(sizeof(aKeyword)/sizeof(aKeyword[0]))) ){
135813 jointype |= JT_ERROR0x80;
135814 break;
135815 }
135816 }
135817 if(
135818 (jointype & (JT_INNER0x01|JT_OUTER0x20))==(JT_INNER0x01|JT_OUTER0x20) ||
135819 (jointype & JT_ERROR0x80)!=0 ||
135820 (jointype & (JT_OUTER0x20|JT_LEFT0x08|JT_RIGHT0x10))==JT_OUTER0x20
135821 ){
135822 const char *zSp1 = " ";
135823 const char *zSp2 = " ";
135824 if( pB==0 ){ zSp1++; }
135825 if( pC==0 ){ zSp2++; }
135826 sqlite3ErrorMsg(pParse, "unknown join type: "
135827 "%T%s%T%s%T", pA, zSp1, pB, zSp2, pC);
135828 jointype = JT_INNER0x01;
135829 }
135830 return jointype;
135831}
135832
135833/*
135834** Return the index of a column in a table. Return -1 if the column
135835** is not contained in the table.
135836*/
135837SQLITE_PRIVATEstatic int sqlite3ColumnIndex(Table *pTab, const char *zCol){
135838 int i;
135839 u8 h = sqlite3StrIHash(zCol);
135840 Column *pCol;
135841 for(pCol=pTab->aCol, i=0; i<pTab->nCol; pCol++, i++){
135842 if( pCol->hName==h && sqlite3StrICmp(pCol->zCnName, zCol)==0 ) return i;
135843 }
135844 return -1;
135845}
135846
135847/*
135848** Mark a subquery result column as having been used.
135849*/
135850SQLITE_PRIVATEstatic void sqlite3SrcItemColumnUsed(SrcItem *pItem, int iCol){
135851 assert( pItem!=0 )((void) (0));
135852 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) )((void) (0));
135853 if( pItem->fg.isNestedFrom ){
135854 ExprList *pResults;
135855 assert( pItem->pSelect!=0 )((void) (0));
135856 pResults = pItem->pSelect->pEList;
135857 assert( pResults!=0 )((void) (0));
135858 assert( iCol>=0 && iCol<pResults->nExpr )((void) (0));
135859 pResults->a[iCol].fg.bUsed = 1;
135860 }
135861}
135862
135863/*
135864** Search the tables iStart..iEnd (inclusive) in pSrc, looking for a
135865** table that has a column named zCol. The search is left-to-right.
135866** The first match found is returned.
135867**
135868** When found, set *piTab and *piCol to the table index and column index
135869** of the matching column and return TRUE.
135870**
135871** If not found, return FALSE.
135872*/
135873static int tableAndColumnIndex(
135874 SrcList *pSrc, /* Array of tables to search */
135875 int iStart, /* First member of pSrc->a[] to check */
135876 int iEnd, /* Last member of pSrc->a[] to check */
135877 const char *zCol, /* Name of the column we are looking for */
135878 int *piTab, /* Write index of pSrc->a[] here */
135879 int *piCol, /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
135880 int bIgnoreHidden /* Ignore hidden columns */
135881){
135882 int i; /* For looping over tables in pSrc */
135883 int iCol; /* Index of column matching zCol */
135884
135885 assert( iEnd<pSrc->nSrc )((void) (0));
135886 assert( iStart>=0 )((void) (0));
135887 assert( (piTab==0)==(piCol==0) )((void) (0)); /* Both or neither are NULL */
135888
135889 for(i=iStart; i<=iEnd; i++){
135890 iCol = sqlite3ColumnIndex(pSrc->a[i].pTab, zCol);
135891 if( iCol>=0
135892 && (bIgnoreHidden==0 || IsHiddenColumn(&pSrc->a[i].pTab->aCol[iCol])(((&pSrc->a[i].pTab->aCol[iCol])->colFlags &
0x0002)!=0)
==0)
135893 ){
135894 if( piTab ){
135895 sqlite3SrcItemColumnUsed(&pSrc->a[i], iCol);
135896 *piTab = i;
135897 *piCol = iCol;
135898 }
135899 return 1;
135900 }
135901 }
135902 return 0;
135903}
135904
135905/*
135906** Set the EP_OuterON property on all terms of the given expression.
135907** And set the Expr.w.iJoin to iTable for every term in the
135908** expression.
135909**
135910** The EP_OuterON property is used on terms of an expression to tell
135911** the OUTER JOIN processing logic that this term is part of the
135912** join restriction specified in the ON or USING clause and not a part
135913** of the more general WHERE clause. These terms are moved over to the
135914** WHERE clause during join processing but we need to remember that they
135915** originated in the ON or USING clause.
135916**
135917** The Expr.w.iJoin tells the WHERE clause processing that the
135918** expression depends on table w.iJoin even if that table is not
135919** explicitly mentioned in the expression. That information is needed
135920** for cases like this:
135921**
135922** SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
135923**
135924** The where clause needs to defer the handling of the t1.x=5
135925** term until after the t2 loop of the join. In that way, a
135926** NULL t2 row will be inserted whenever t1.x!=5. If we do not
135927** defer the handling of t1.x=5, it will be processed immediately
135928** after the t1 loop and rows with t1.x!=5 will never appear in
135929** the output, which is incorrect.
135930*/
135931SQLITE_PRIVATEstatic void sqlite3SetJoinExpr(Expr *p, int iTable, u32 joinFlag){
135932 assert( joinFlag==EP_OuterON || joinFlag==EP_InnerON )((void) (0));
135933 while( p ){
135934 ExprSetProperty(p, joinFlag)(p)->flags|=(joinFlag);
135935 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) )((void) (0));
135936 ExprSetVVAProperty(p, EP_NoReduce);
135937 p->w.iJoin = iTable;
135938 if( p->op==TK_FUNCTION172 ){
135939 assert( ExprUseXList(p) )((void) (0));
135940 if( p->x.pList ){
135941 int i;
135942 for(i=0; i<p->x.pList->nExpr; i++){
135943 sqlite3SetJoinExpr(p->x.pList->a[i].pExpr, iTable, joinFlag);
135944 }
135945 }
135946 }
135947 sqlite3SetJoinExpr(p->pLeft, iTable, joinFlag);
135948 p = p->pRight;
135949 }
135950}
135951
135952/* Undo the work of sqlite3SetJoinExpr(). This is used when a LEFT JOIN
135953** is simplified into an ordinary JOIN, and when an ON expression is
135954** "pushed down" into the WHERE clause of a subquery.
135955**
135956** Convert every term that is marked with EP_OuterON and w.iJoin==iTable into
135957** an ordinary term that omits the EP_OuterON mark. Or if iTable<0, then
135958** just clear every EP_OuterON and EP_InnerON mark from the expression tree.
135959**
135960** If nullable is true, that means that Expr p might evaluate to NULL even
135961** if it is a reference to a NOT NULL column. This can happen, for example,
135962** if the table that p references is on the left side of a RIGHT JOIN.
135963** If nullable is true, then take care to not remove the EP_CanBeNull bit.
135964** See forum thread https://sqlite.org/forum/forumpost/b40696f50145d21c
135965*/
135966static void unsetJoinExpr(Expr *p, int iTable, int nullable){
135967 while( p ){
135968 if( iTable<0 || (ExprHasProperty(p, EP_OuterON)(((p)->flags&(0x000001))!=0) && p->w.iJoin==iTable) ){
135969 ExprClearProperty(p, EP_OuterON|EP_InnerON)(p)->flags&=~(0x000001|0x000002);
135970 if( iTable>=0 ) ExprSetProperty(p, EP_InnerON)(p)->flags|=(0x000002);
135971 }
135972 if( p->op==TK_COLUMN167 && p->iTable==iTable && !nullable ){
135973 ExprClearProperty(p, EP_CanBeNull)(p)->flags&=~(0x200000);
135974 }
135975 if( p->op==TK_FUNCTION172 ){
135976 assert( ExprUseXList(p) )((void) (0));
135977 if( p->x.pList ){
135978 int i;
135979 for(i=0; i<p->x.pList->nExpr; i++){
135980 unsetJoinExpr(p->x.pList->a[i].pExpr, iTable, nullable);
135981 }
135982 }
135983 }
135984 unsetJoinExpr(p->pLeft, iTable, nullable);
135985 p = p->pRight;
135986 }
135987}
135988
135989/*
135990** This routine processes the join information for a SELECT statement.
135991**
135992** * A NATURAL join is converted into a USING join. After that, we
135993** do not need to be concerned with NATURAL joins and we only have
135994** think about USING joins.
135995**
135996** * ON and USING clauses result in extra terms being added to the
135997** WHERE clause to enforce the specified constraints. The extra
135998** WHERE clause terms will be tagged with EP_OuterON or
135999** EP_InnerON so that we know that they originated in ON/USING.
136000**
136001** The terms of a FROM clause are contained in the Select.pSrc structure.
136002** The left most table is the first entry in Select.pSrc. The right-most
136003** table is the last entry. The join operator is held in the entry to
136004** the right. Thus entry 1 contains the join operator for the join between
136005** entries 0 and 1. Any ON or USING clauses associated with the join are
136006** also attached to the right entry.
136007**
136008** This routine returns the number of errors encountered.
136009*/
136010static int sqlite3ProcessJoin(Parse *pParse, Select *p){
136011 SrcList *pSrc; /* All tables in the FROM clause */
136012 int i, j; /* Loop counters */
136013 SrcItem *pLeft; /* Left table being joined */
136014 SrcItem *pRight; /* Right table being joined */
136015
136016 pSrc = p->pSrc;
136017 pLeft = &pSrc->a[0];
136018 pRight = &pLeft[1];
136019 for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
136020 Table *pRightTab = pRight->pTab;
136021 u32 joinType;
136022
136023 if( NEVER(pLeft->pTab==0 || pRightTab==0)(pLeft->pTab==0 || pRightTab==0) ) continue;
136024 joinType = (pRight->fg.jointype & JT_OUTER0x20)!=0 ? EP_OuterON0x000001 : EP_InnerON0x000002;
136025
136026 /* If this is a NATURAL join, synthesize an approprate USING clause
136027 ** to specify which columns should be joined.
136028 */
136029 if( pRight->fg.jointype & JT_NATURAL0x04 ){
136030 IdList *pUsing = 0;
136031 if( pRight->fg.isUsing || pRight->u3.pOn ){
136032 sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
136033 "an ON or USING clause", 0);
136034 return 1;
136035 }
136036 for(j=0; j<pRightTab->nCol; j++){
136037 char *zName; /* Name of column in the right table */
136038
136039 if( IsHiddenColumn(&pRightTab->aCol[j])(((&pRightTab->aCol[j])->colFlags & 0x0002)!=0) ) continue;
136040 zName = pRightTab->aCol[j].zCnName;
136041 if( tableAndColumnIndex(pSrc, 0, i, zName, 0, 0, 1) ){
136042 pUsing = sqlite3IdListAppend(pParse, pUsing, 0);
136043 if( pUsing ){
136044 assert( pUsing->nId>0 )((void) (0));
136045 assert( pUsing->a[pUsing->nId-1].zName==0 )((void) (0));
136046 pUsing->a[pUsing->nId-1].zName = sqlite3DbStrDup(pParse->db, zName);
136047 }
136048 }
136049 }
136050 if( pUsing ){
136051 pRight->fg.isUsing = 1;
136052 pRight->fg.isSynthUsing = 1;
136053 pRight->u3.pUsing = pUsing;
136054 }
136055 if( pParse->nErr ) return 1;
136056 }
136057
136058 /* Create extra terms on the WHERE clause for each column named
136059 ** in the USING clause. Example: If the two tables to be joined are
136060 ** A and B and the USING clause names X, Y, and Z, then add this
136061 ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
136062 ** Report an error if any column mentioned in the USING clause is
136063 ** not contained in both tables to be joined.
136064 */
136065 if( pRight->fg.isUsing ){
136066 IdList *pList = pRight->u3.pUsing;
136067 sqlite3 *db = pParse->db;
136068 assert( pList!=0 )((void) (0));
136069 for(j=0; j<pList->nId; j++){
136070 char *zName; /* Name of the term in the USING clause */
136071 int iLeft; /* Table on the left with matching column name */
136072 int iLeftCol; /* Column number of matching column on the left */
136073 int iRightCol; /* Column number of matching column on the right */
136074 Expr *pE1; /* Reference to the column on the LEFT of the join */
136075 Expr *pE2; /* Reference to the column on the RIGHT of the join */
136076 Expr *pEq; /* Equality constraint. pE1 == pE2 */
136077
136078 zName = pList->a[j].zName;
136079 iRightCol = sqlite3ColumnIndex(pRightTab, zName);
136080 if( iRightCol<0
136081 || tableAndColumnIndex(pSrc, 0, i, zName, &iLeft, &iLeftCol,
136082 pRight->fg.isSynthUsing)==0
136083 ){
136084 sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
136085 "not present in both tables", zName);
136086 return 1;
136087 }
136088 pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iLeftCol);
136089 sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol);
136090 if( (pSrc->a[0].fg.jointype & JT_LTORJ0x40)!=0 ){
136091 /* This branch runs if the query contains one or more RIGHT or FULL
136092 ** JOINs. If only a single table on the left side of this join
136093 ** contains the zName column, then this branch is a no-op.
136094 ** But if there are two or more tables on the left side
136095 ** of the join, construct a coalesce() function that gathers all
136096 ** such tables. Raise an error if more than one of those references
136097 ** to zName is not also within a prior USING clause.
136098 **
136099 ** We really ought to raise an error if there are two or more
136100 ** non-USING references to zName on the left of an INNER or LEFT
136101 ** JOIN. But older versions of SQLite do not do that, so we avoid
136102 ** adding a new error so as to not break legacy applications.
136103 */
136104 ExprList *pFuncArgs = 0; /* Arguments to the coalesce() */
136105 static const Token tkCoalesce = { "coalesce", 8 };
136106 while( tableAndColumnIndex(pSrc, iLeft+1, i, zName, &iLeft, &iLeftCol,
136107 pRight->fg.isSynthUsing)!=0 ){
136108 if( pSrc->a[iLeft].fg.isUsing==0
136109 || sqlite3IdListIndex(pSrc->a[iLeft].u3.pUsing, zName)<0
136110 ){
136111 sqlite3ErrorMsg(pParse, "ambiguous reference to %s in USING()",
136112 zName);
136113 break;
136114 }
136115 pFuncArgs = sqlite3ExprListAppend(pParse, pFuncArgs, pE1);
136116 pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iLeftCol);
136117 sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol);
136118 }
136119 if( pFuncArgs ){
136120 pFuncArgs = sqlite3ExprListAppend(pParse, pFuncArgs, pE1);
136121 pE1 = sqlite3ExprFunction(pParse, pFuncArgs, &tkCoalesce, 0);
136122 }
136123 }
136124 pE2 = sqlite3CreateColumnExpr(db, pSrc, i+1, iRightCol);
136125 sqlite3SrcItemColumnUsed(pRight, iRightCol);
136126 pEq = sqlite3PExpr(pParse, TK_EQ53, pE1, pE2);
136127 assert( pE2!=0 || pEq==0 )((void) (0));
136128 if( pEq ){
136129 ExprSetProperty(pEq, joinType)(pEq)->flags|=(joinType);
136130 assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) )((void) (0));
136131 ExprSetVVAProperty(pEq, EP_NoReduce);
136132 pEq->w.iJoin = pE2->iTable;
136133 }
136134 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pEq);
136135 }
136136 }
136137
136138 /* Add the ON clause to the end of the WHERE clause, connected by
136139 ** an AND operator.
136140 */
136141 else if( pRight->u3.pOn ){
136142 sqlite3SetJoinExpr(pRight->u3.pOn, pRight->iCursor, joinType);
136143 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pRight->u3.pOn);
136144 pRight->u3.pOn = 0;
136145 pRight->fg.isOn = 1;
136146 }
136147 }
136148 return 0;
136149}
136150
136151/*
136152** An instance of this object holds information (beyond pParse and pSelect)
136153** needed to load the next result row that is to be added to the sorter.
136154*/
136155typedef struct RowLoadInfo RowLoadInfo;
136156struct RowLoadInfo {
136157 int regResult; /* Store results in array of registers here */
136158 u8 ecelFlags; /* Flag argument to ExprCodeExprList() */
136159#ifdef SQLITE_ENABLE_SORTER_REFERENCES
136160 ExprList *pExtra; /* Extra columns needed by sorter refs */
136161 int regExtraResult; /* Where to load the extra columns */
136162#endif
136163};
136164
136165/*
136166** This routine does the work of loading query data into an array of
136167** registers so that it can be added to the sorter.
136168*/
136169static void innerLoopLoadRow(
136170 Parse *pParse, /* Statement under construction */
136171 Select *pSelect, /* The query being coded */
136172 RowLoadInfo *pInfo /* Info needed to complete the row load */
136173){
136174 sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult,
136175 0, pInfo->ecelFlags);
136176#ifdef SQLITE_ENABLE_SORTER_REFERENCES
136177 if( pInfo->pExtra ){
136178 sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
136179 sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
136180 }
136181#endif
136182}
136183
136184/*
136185** Code the OP_MakeRecord instruction that generates the entry to be
136186** added into the sorter.
136187**
136188** Return the register in which the result is stored.
136189*/
136190static int makeSorterRecord(
136191 Parse *pParse,
136192 SortCtx *pSort,
136193 Select *pSelect,
136194 int regBase,
136195 int nBase
136196){
136197 int nOBSat = pSort->nOBSat;
136198 Vdbe *v = pParse->pVdbe;
136199 int regOut = ++pParse->nMem;
136200 if( pSort->pDeferredRowLoad ){
136201 innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad);
136202 }
136203 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regBase+nOBSat, nBase-nOBSat, regOut);
136204 return regOut;
136205}
136206
136207/*
136208** Generate code that will push the record in registers regData
136209** through regData+nData-1 onto the sorter.
136210*/
136211static void pushOntoSorter(
136212 Parse *pParse, /* Parser context */
136213 SortCtx *pSort, /* Information about the ORDER BY clause */
136214 Select *pSelect, /* The whole SELECT statement */
136215 int regData, /* First register holding data to be sorted */
136216 int regOrigData, /* First register holding data before packing */
136217 int nData, /* Number of elements in the regData data array */
136218 int nPrefixReg /* No. of reg prior to regData available for use */
136219){
136220 Vdbe *v = pParse->pVdbe; /* Stmt under construction */
136221 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter0x01)==0);
136222 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
136223 int nBase = nExpr + bSeq + nData; /* Fields in sorter record */
136224 int regBase; /* Regs for sorter record */
136225 int regRecord = 0; /* Assembled sorter record */
136226 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
136227 int op; /* Opcode to add sorter record to sorter */
136228 int iLimit; /* LIMIT counter */
136229 int iSkip = 0; /* End of the sorter insert loop */
136230
136231 assert( bSeq==0 || bSeq==1 )((void) (0));
136232
136233 /* Three cases:
136234 ** (1) The data to be sorted has already been packed into a Record
136235 ** by a prior OP_MakeRecord. In this case nData==1 and regData
136236 ** will be completely unrelated to regOrigData.
136237 ** (2) All output columns are included in the sort record. In that
136238 ** case regData==regOrigData.
136239 ** (3) Some output columns are omitted from the sort record due to
136240 ** the SQLITE_ENABLE_SORTER_REFERENCE optimization, or due to the
136241 ** SQLITE_ECEL_OMITREF optimization, or due to the
136242 ** SortCtx.pDeferredRowLoad optimiation. In any of these cases
136243 ** regOrigData is 0 to prevent this routine from trying to copy
136244 ** values that might not yet exist.
136245 */
136246 assert( nData==1 || regData==regOrigData || regOrigData==0 )((void) (0));
136247
136248 if( nPrefixReg ){
136249 assert( nPrefixReg==nExpr+bSeq )((void) (0));
136250 regBase = regData - nPrefixReg;
136251 }else{
136252 regBase = pParse->nMem + 1;
136253 pParse->nMem += nBase;
136254 }
136255 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 )((void) (0));
136256 iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
136257 pSort->labelDone = sqlite3VdbeMakeLabel(pParse);
136258 sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
136259 SQLITE_ECEL_DUP0x01 | (regOrigData? SQLITE_ECEL_REF0x04 : 0));
136260 if( bSeq ){
136261 sqlite3VdbeAddOp2(v, OP_Sequence126, pSort->iECursor, regBase+nExpr);
136262 }
136263 if( nPrefixReg==0 && nData>0 ){
136264 sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData);
136265 }
136266 if( nOBSat>0 ){
136267 int regPrevKey; /* The first nOBSat columns of the previous row */
136268 int addrFirst; /* Address of the OP_IfNot opcode */
136269 int addrJmp; /* Address of the OP_Jump opcode */
136270 VdbeOp *pOp; /* Opcode that opens the sorter */
136271 int nKey; /* Number of sorting key columns, including OP_Sequence */
136272 KeyInfo *pKI; /* Original KeyInfo on the sorter table */
136273
136274 regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
136275 regPrevKey = pParse->nMem+1;
136276 pParse->nMem += pSort->nOBSat;
136277 nKey = nExpr - pSort->nOBSat + bSeq;
136278 if( bSeq ){
136279 addrFirst = sqlite3VdbeAddOp1(v, OP_IfNot16, regBase+nExpr);
136280 }else{
136281 addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest120, pSort->iECursor);
136282 }
136283 VdbeCoverage(v);
136284 sqlite3VdbeAddOp3(v, OP_Compare90, regPrevKey, regBase, pSort->nOBSat);
136285 pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
136286 if( pParse->db->mallocFailed ) return;
136287 pOp->p2 = nKey + nData;
136288 pKI = pOp->p4.pKeyInfo;
136289 memset(pKI->aSortFlags, 0, pKI->nKeyField); /* Makes OP_Jump testable */
136290 sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO(-8));
136291 testcase( pKI->nAllField > pKI->nKeyField+2 );
136292 pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat,
136293 pKI->nAllField-pKI->nKeyField-1);
136294 pOp = 0; /* Ensure pOp not used after sqltie3VdbeAddOp3() */
136295 addrJmp = sqlite3VdbeCurrentAddr(v);
136296 sqlite3VdbeAddOp3(v, OP_Jump13, addrJmp+1, 0, addrJmp+1); VdbeCoverage(v);
136297 pSort->labelBkOut = sqlite3VdbeMakeLabel(pParse);
136298 pSort->regReturn = ++pParse->nMem;
136299 sqlite3VdbeAddOp2(v, OP_Gosub9, pSort->regReturn, pSort->labelBkOut);
136300 sqlite3VdbeAddOp1(v, OP_ResetSorter146, pSort->iECursor);
136301 if( iLimit ){
136302 sqlite3VdbeAddOp2(v, OP_IfNot16, iLimit, pSort->labelDone);
136303 VdbeCoverage(v);
136304 }
136305 sqlite3VdbeJumpHere(v, addrFirst);
136306 sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
136307 sqlite3VdbeJumpHere(v, addrJmp);
136308 }
136309 if( iLimit ){
136310 /* At this point the values for the new sorter entry are stored
136311 ** in an array of registers. They need to be composed into a record
136312 ** and inserted into the sorter if either (a) there are currently
136313 ** less than LIMIT+OFFSET items or (b) the new record is smaller than
136314 ** the largest record currently in the sorter. If (b) is true and there
136315 ** are already LIMIT+OFFSET items in the sorter, delete the largest
136316 ** entry before inserting the new one. This way there are never more
136317 ** than LIMIT+OFFSET items in the sorter.
136318 **
136319 ** If the new record does not need to be inserted into the sorter,
136320 ** jump to the next iteration of the loop. If the pSort->labelOBLopt
136321 ** value is not zero, then it is a label of where to jump. Otherwise,
136322 ** just bypass the row insert logic. See the header comment on the
136323 ** sqlite3WhereOrderByLimitOptLabel() function for additional info.
136324 */
136325 int iCsr = pSort->iECursor;
136326 sqlite3VdbeAddOp2(v, OP_IfNotZero59, iLimit, sqlite3VdbeCurrentAddr(v)+4);
136327 VdbeCoverage(v);
136328 sqlite3VdbeAddOp2(v, OP_Last31, iCsr, 0);
136329 iSkip = sqlite3VdbeAddOp4Int(v, OP_IdxLE39,
136330 iCsr, 0, regBase+nOBSat, nExpr-nOBSat);
136331 VdbeCoverage(v);
136332 sqlite3VdbeAddOp1(v, OP_Delete130, iCsr);
136333 }
136334 if( regRecord==0 ){
136335 regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
136336 }
136337 if( pSort->sortFlags & SORTFLAG_UseSorter0x01 ){
136338 op = OP_SorterInsert139;
136339 }else{
136340 op = OP_IdxInsert138;
136341 }
136342 sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
136343 regBase+nOBSat, nBase-nOBSat);
136344 if( iSkip ){
136345 sqlite3VdbeChangeP2(v, iSkip,
136346 pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(v));
136347 }
136348}
136349
136350/*
136351** Add code to implement the OFFSET
136352*/
136353static void codeOffset(
136354 Vdbe *v, /* Generate code into this VM */
136355 int iOffset, /* Register holding the offset counter */
136356 int iContinue /* Jump here to skip the current record */
136357){
136358 if( iOffset>0 ){
136359 sqlite3VdbeAddOp3(v, OP_IfPos49, iOffset, iContinue, 1); VdbeCoverage(v);
136360 VdbeComment((v, "OFFSET"));
136361 }
136362}
136363
136364/*
136365** Add code that will check to make sure the array of registers starting at
136366** iMem form a distinct entry. This is used by both "SELECT DISTINCT ..." and
136367** distinct aggregates ("SELECT count(DISTINCT <expr>) ..."). Three strategies
136368** are available. Which is used depends on the value of parameter eTnctType,
136369** as follows:
136370**
136371** WHERE_DISTINCT_UNORDERED/WHERE_DISTINCT_NOOP:
136372** Build an ephemeral table that contains all entries seen before and
136373** skip entries which have been seen before.
136374**
136375** Parameter iTab is the cursor number of an ephemeral table that must
136376** be opened before the VM code generated by this routine is executed.
136377** The ephemeral cursor table is queried for a record identical to the
136378** record formed by the current array of registers. If one is found,
136379** jump to VM address addrRepeat. Otherwise, insert a new record into
136380** the ephemeral cursor and proceed.
136381**
136382** The returned value in this case is a copy of parameter iTab.
136383**
136384** WHERE_DISTINCT_ORDERED:
136385** In this case rows are being delivered sorted order. The ephermal
136386** table is not required. Instead, the current set of values
136387** is compared against previous row. If they match, the new row
136388** is not distinct and control jumps to VM address addrRepeat. Otherwise,
136389** the VM program proceeds with processing the new row.
136390**
136391** The returned value in this case is the register number of the first
136392** in an array of registers used to store the previous result row so that
136393** it can be compared to the next. The caller must ensure that this
136394** register is initialized to NULL. (The fixDistinctOpenEph() routine
136395** will take care of this initialization.)
136396**
136397** WHERE_DISTINCT_UNIQUE:
136398** In this case it has already been determined that the rows are distinct.
136399** No special action is required. The return value is zero.
136400**
136401** Parameter pEList is the list of expressions used to generated the
136402** contents of each row. It is used by this routine to determine (a)
136403** how many elements there are in the array of registers and (b) the
136404** collation sequences that should be used for the comparisons if
136405** eTnctType is WHERE_DISTINCT_ORDERED.
136406*/
136407static int codeDistinct(
136408 Parse *pParse, /* Parsing and code generating context */
136409 int eTnctType, /* WHERE_DISTINCT_* value */
136410 int iTab, /* A sorting index used to test for distinctness */
136411 int addrRepeat, /* Jump to here if not distinct */
136412 ExprList *pEList, /* Expression for each element */
136413 int regElem /* First element */
136414){
136415 int iRet = 0;
136416 int nResultCol = pEList->nExpr;
136417 Vdbe *v = pParse->pVdbe;
136418
136419 switch( eTnctType ){
136420 case WHERE_DISTINCT_ORDERED2: {
136421 int i;
136422 int iJump; /* Jump destination */
136423 int regPrev; /* Previous row content */
136424
136425 /* Allocate space for the previous row */
136426 iRet = regPrev = pParse->nMem+1;
136427 pParse->nMem += nResultCol;
136428
136429 iJump = sqlite3VdbeCurrentAddr(v) + nResultCol;
136430 for(i=0; i<nResultCol; i++){
136431 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
136432 if( i<nResultCol-1 ){
136433 sqlite3VdbeAddOp3(v, OP_Ne52, regElem+i, iJump, regPrev+i);
136434 VdbeCoverage(v);
136435 }else{
136436 sqlite3VdbeAddOp3(v, OP_Eq53, regElem+i, addrRepeat, regPrev+i);
136437 VdbeCoverage(v);
136438 }
136439 sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ(-2));
136440 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ0x80);
136441 }
136442 assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed )((void) (0));
136443 sqlite3VdbeAddOp3(v, OP_Copy80, regElem, regPrev, nResultCol-1);
136444 break;
136445 }
136446
136447 case WHERE_DISTINCT_UNIQUE1: {
136448 /* nothing to do */
136449 break;
136450 }
136451
136452 default: {
136453 int r1 = sqlite3GetTempReg(pParse);
136454 sqlite3VdbeAddOp4Int(v, OP_Found28, iTab, addrRepeat, regElem, nResultCol);
136455 VdbeCoverage(v);
136456 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regElem, nResultCol, r1);
136457 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iTab, r1, regElem, nResultCol);
136458 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT0x10);
136459 sqlite3ReleaseTempReg(pParse, r1);
136460 iRet = iTab;
136461 break;
136462 }
136463 }
136464
136465 return iRet;
136466}
136467
136468/*
136469** This routine runs after codeDistinct(). It makes necessary
136470** adjustments to the OP_OpenEphemeral opcode that the codeDistinct()
136471** routine made use of. This processing must be done separately since
136472** sometimes codeDistinct is called before the OP_OpenEphemeral is actually
136473** laid down.
136474**
136475** WHERE_DISTINCT_NOOP:
136476** WHERE_DISTINCT_UNORDERED:
136477**
136478** No adjustments necessary. This function is a no-op.
136479**
136480** WHERE_DISTINCT_UNIQUE:
136481**
136482** The ephemeral table is not needed. So change the
136483** OP_OpenEphemeral opcode into an OP_Noop.
136484**
136485** WHERE_DISTINCT_ORDERED:
136486**
136487** The ephemeral table is not needed. But we do need register
136488** iVal to be initialized to NULL. So change the OP_OpenEphemeral
136489** into an OP_Null on the iVal register.
136490*/
136491static void fixDistinctOpenEph(
136492 Parse *pParse, /* Parsing and code generating context */
136493 int eTnctType, /* WHERE_DISTINCT_* value */
136494 int iVal, /* Value returned by codeDistinct() */
136495 int iOpenEphAddr /* Address of OP_OpenEphemeral instruction for iTab */
136496){
136497 if( pParse->nErr==0
136498 && (eTnctType==WHERE_DISTINCT_UNIQUE1 || eTnctType==WHERE_DISTINCT_ORDERED2)
136499 ){
136500 Vdbe *v = pParse->pVdbe;
136501 sqlite3VdbeChangeToNoop(v, iOpenEphAddr);
136502 if( sqlite3VdbeGetOp(v, iOpenEphAddr+1)->opcode==OP_Explain185 ){
136503 sqlite3VdbeChangeToNoop(v, iOpenEphAddr+1);
136504 }
136505 if( eTnctType==WHERE_DISTINCT_ORDERED2 ){
136506 /* Change the OP_OpenEphemeral to an OP_Null that sets the MEM_Cleared
136507 ** bit on the first register of the previous value. This will cause the
136508 ** OP_Ne added in codeDistinct() to always fail on the first iteration of
136509 ** the loop even if the first row is all NULLs. */
136510 VdbeOp *pOp = sqlite3VdbeGetOp(v, iOpenEphAddr);
136511 pOp->opcode = OP_Null75;
136512 pOp->p1 = 1;
136513 pOp->p2 = iVal;
136514 }
136515 }
136516}
136517
136518#ifdef SQLITE_ENABLE_SORTER_REFERENCES
136519/*
136520** This function is called as part of inner-loop generation for a SELECT
136521** statement with an ORDER BY that is not optimized by an index. It
136522** determines the expressions, if any, that the sorter-reference
136523** optimization should be used for. The sorter-reference optimization
136524** is used for SELECT queries like:
136525**
136526** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10
136527**
136528** If the optimization is used for expression "bigblob", then instead of
136529** storing values read from that column in the sorter records, the PK of
136530** the row from table t1 is stored instead. Then, as records are extracted from
136531** the sorter to return to the user, the required value of bigblob is
136532** retrieved directly from table t1. If the values are very large, this
136533** can be more efficient than storing them directly in the sorter records.
136534**
136535** The ExprList_item.fg.bSorterRef flag is set for each expression in pEList
136536** for which the sorter-reference optimization should be enabled.
136537** Additionally, the pSort->aDefer[] array is populated with entries
136538** for all cursors required to evaluate all selected expressions. Finally.
136539** output variable (*ppExtra) is set to an expression list containing
136540** expressions for all extra PK values that should be stored in the
136541** sorter records.
136542*/
136543static void selectExprDefer(
136544 Parse *pParse, /* Leave any error here */
136545 SortCtx *pSort, /* Sorter context */
136546 ExprList *pEList, /* Expressions destined for sorter */
136547 ExprList **ppExtra /* Expressions to append to sorter record */
136548){
136549 int i;
136550 int nDefer = 0;
136551 ExprList *pExtra = 0;
136552 for(i=0; i<pEList->nExpr; i++){
136553 struct ExprList_item *pItem = &pEList->a[i];
136554 if( pItem->u.x.iOrderByCol==0 ){
136555 Expr *pExpr = pItem->pExpr;
136556 Table *pTab;
136557 if( pExpr->op==TK_COLUMN167
136558 && pExpr->iColumn>=0
136559 && ALWAYS( ExprUseYTab(pExpr) )((((pExpr)->flags&(0x1000000|0x2000000))==0))
136560 && (pTab = pExpr->y.pTab)!=0
136561 && IsOrdinaryTable(pTab)((pTab)->eTabType==0)
136562 && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF0x0010)!=0
136563 ){
136564 int j;
136565 for(j=0; j<nDefer; j++){
136566 if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
136567 }
136568 if( j==nDefer ){
136569 if( nDefer==ArraySize(pSort->aDefer)((int)(sizeof(pSort->aDefer)/sizeof(pSort->aDefer[0]))) ){
136570 continue;
136571 }else{
136572 int nKey = 1;
136573 int k;
136574 Index *pPk = 0;
136575 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
136576 pPk = sqlite3PrimaryKeyIndex(pTab);
136577 nKey = pPk->nKeyCol;
136578 }
136579 for(k=0; k<nKey; k++){
136580 Expr *pNew = sqlite3PExpr(pParse, TK_COLUMN167, 0, 0);
136581 if( pNew ){
136582 pNew->iTable = pExpr->iTable;
136583 assert( ExprUseYTab(pNew) )((void) (0));
136584 pNew->y.pTab = pExpr->y.pTab;
136585 pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
136586 pExtra = sqlite3ExprListAppend(pParse, pExtra, pNew);
136587 }
136588 }
136589 pSort->aDefer[nDefer].pTab = pExpr->y.pTab;
136590 pSort->aDefer[nDefer].iCsr = pExpr->iTable;
136591 pSort->aDefer[nDefer].nKey = nKey;
136592 nDefer++;
136593 }
136594 }
136595 pItem->fg.bSorterRef = 1;
136596 }
136597 }
136598 }
136599 pSort->nDefer = (u8)nDefer;
136600 *ppExtra = pExtra;
136601}
136602#endif
136603
136604/*
136605** This routine generates the code for the inside of the inner loop
136606** of a SELECT.
136607**
136608** If srcTab is negative, then the p->pEList expressions
136609** are evaluated in order to get the data for this row. If srcTab is
136610** zero or more, then data is pulled from srcTab and p->pEList is used only
136611** to get the number of columns and the collation sequence for each column.
136612*/
136613static void selectInnerLoop(
136614 Parse *pParse, /* The parser context */
136615 Select *p, /* The complete select statement being coded */
136616 int srcTab, /* Pull data from this table if non-negative */
136617 SortCtx *pSort, /* If not NULL, info on how to process ORDER BY */
136618 DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
136619 SelectDest *pDest, /* How to dispose of the results */
136620 int iContinue, /* Jump here to continue with next row */
136621 int iBreak /* Jump here to break out of the inner loop */
136622){
136623 Vdbe *v = pParse->pVdbe;
136624 int i;
136625 int hasDistinct; /* True if the DISTINCT keyword is present */
136626 int eDest = pDest->eDest; /* How to dispose of results */
136627 int iParm = pDest->iSDParm; /* First argument to disposal method */
136628 int nResultCol; /* Number of result columns */
136629 int nPrefixReg = 0; /* Number of extra registers before regResult */
136630 RowLoadInfo sRowLoadInfo; /* Info for deferred row loading */
136631
136632 /* Usually, regResult is the first cell in an array of memory cells
136633 ** containing the current result row. In this case regOrig is set to the
136634 ** same value. However, if the results are being sent to the sorter, the
136635 ** values for any expressions that are also part of the sort-key are omitted
136636 ** from this array. In this case regOrig is set to zero. */
136637 int regResult; /* Start of memory holding current results */
136638 int regOrig; /* Start of memory holding full result (or 0) */
136639
136640 assert( v )((void) (0));
136641 assert( p->pEList!=0 )((void) (0));
136642 hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP0;
136643 if( pSort && pSort->pOrderBy==0 ) pSort = 0;
136644 if( pSort==0 && !hasDistinct ){
136645 assert( iContinue!=0 )((void) (0));
136646 codeOffset(v, p->iOffset, iContinue);
136647 }
136648
136649 /* Pull the requested columns.
136650 */
136651 nResultCol = p->pEList->nExpr;
136652
136653 if( pDest->iSdst==0 ){
136654 if( pSort ){
136655 nPrefixReg = pSort->pOrderBy->nExpr;
136656 if( !(pSort->sortFlags & SORTFLAG_UseSorter0x01) ) nPrefixReg++;
136657 pParse->nMem += nPrefixReg;
136658 }
136659 pDest->iSdst = pParse->nMem+1;
136660 pParse->nMem += nResultCol;
136661 }else if( pDest->iSdst+nResultCol > pParse->nMem ){
136662 /* This is an error condition that can result, for example, when a SELECT
136663 ** on the right-hand side of an INSERT contains more result columns than
136664 ** there are columns in the table on the left. The error will be caught
136665 ** and reported later. But we need to make sure enough memory is allocated
136666 ** to avoid other spurious errors in the meantime. */
136667 pParse->nMem += nResultCol;
136668 }
136669 pDest->nSdst = nResultCol;
136670 regOrig = regResult = pDest->iSdst;
136671 if( srcTab>=0 ){
136672 for(i=0; i<nResultCol; i++){
136673 sqlite3VdbeAddOp3(v, OP_Column94, srcTab, i, regResult+i);
136674 VdbeComment((v, "%s", p->pEList->a[i].zEName));
136675 }
136676 }else if( eDest!=SRT_Exists3 ){
136677#ifdef SQLITE_ENABLE_SORTER_REFERENCES
136678 ExprList *pExtra = 0;
136679#endif
136680 /* If the destination is an EXISTS(...) expression, the actual
136681 ** values returned by the SELECT are not required.
136682 */
136683 u8 ecelFlags; /* "ecel" is an abbreviation of "ExprCodeExprList" */
136684 ExprList *pEList;
136685 if( eDest==SRT_Mem10 || eDest==SRT_Output9 || eDest==SRT_Coroutine13 ){
136686 ecelFlags = SQLITE_ECEL_DUP0x01;
136687 }else{
136688 ecelFlags = 0;
136689 }
136690 if( pSort && hasDistinct==0 && eDest!=SRT_EphemTab12 && eDest!=SRT_Table14 ){
136691 /* For each expression in p->pEList that is a copy of an expression in
136692 ** the ORDER BY clause (pSort->pOrderBy), set the associated
136693 ** iOrderByCol value to one more than the index of the ORDER BY
136694 ** expression within the sort-key that pushOntoSorter() will generate.
136695 ** This allows the p->pEList field to be omitted from the sorted record,
136696 ** saving space and CPU cycles. */
136697 ecelFlags |= (SQLITE_ECEL_OMITREF0x08|SQLITE_ECEL_REF0x04);
136698
136699 for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
136700 int j;
136701 if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
136702 p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
136703 }
136704 }
136705#ifdef SQLITE_ENABLE_SORTER_REFERENCES
136706 selectExprDefer(pParse, pSort, p->pEList, &pExtra);
136707 if( pExtra && pParse->db->mallocFailed==0 ){
136708 /* If there are any extra PK columns to add to the sorter records,
136709 ** allocate extra memory cells and adjust the OpenEphemeral
136710 ** instruction to account for the larger records. This is only
136711 ** required if there are one or more WITHOUT ROWID tables with
136712 ** composite primary keys in the SortCtx.aDefer[] array. */
136713 VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
136714 pOp->p2 += (pExtra->nExpr - pSort->nDefer);
136715 pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
136716 pParse->nMem += pExtra->nExpr;
136717 }
136718#endif
136719
136720 /* Adjust nResultCol to account for columns that are omitted
136721 ** from the sorter by the optimizations in this branch */
136722 pEList = p->pEList;
136723 for(i=0; i<pEList->nExpr; i++){
136724 if( pEList->a[i].u.x.iOrderByCol>0
136725#ifdef SQLITE_ENABLE_SORTER_REFERENCES
136726 || pEList->a[i].fg.bSorterRef
136727#endif
136728 ){
136729 nResultCol--;
136730 regOrig = 0;
136731 }
136732 }
136733
136734 testcase( regOrig );
136735 testcase( eDest==SRT_Set );
136736 testcase( eDest==SRT_Mem );
136737 testcase( eDest==SRT_Coroutine );
136738 testcase( eDest==SRT_Output );
136739 assert( eDest==SRT_Set || eDest==SRT_Mem((void) (0))
136740 || eDest==SRT_Coroutine || eDest==SRT_Output((void) (0))
136741 || eDest==SRT_Upfrom )((void) (0));
136742 }
136743 sRowLoadInfo.regResult = regResult;
136744 sRowLoadInfo.ecelFlags = ecelFlags;
136745#ifdef SQLITE_ENABLE_SORTER_REFERENCES
136746 sRowLoadInfo.pExtra = pExtra;
136747 sRowLoadInfo.regExtraResult = regResult + nResultCol;
136748 if( pExtra ) nResultCol += pExtra->nExpr;
136749#endif
136750 if( p->iLimit
136751 && (ecelFlags & SQLITE_ECEL_OMITREF0x08)!=0
136752 && nPrefixReg>0
136753 ){
136754 assert( pSort!=0 )((void) (0));
136755 assert( hasDistinct==0 )((void) (0));
136756 pSort->pDeferredRowLoad = &sRowLoadInfo;
136757 regOrig = 0;
136758 }else{
136759 innerLoopLoadRow(pParse, p, &sRowLoadInfo);
136760 }
136761 }
136762
136763 /* If the DISTINCT keyword was present on the SELECT statement
136764 ** and this row has been seen before, then do not make this row
136765 ** part of the result.
136766 */
136767 if( hasDistinct ){
136768 int eType = pDistinct->eTnctType;
136769 int iTab = pDistinct->tabTnct;
136770 assert( nResultCol==p->pEList->nExpr )((void) (0));
136771 iTab = codeDistinct(pParse, eType, iTab, iContinue, p->pEList, regResult);
136772 fixDistinctOpenEph(pParse, eType, iTab, pDistinct->addrTnct);
136773 if( pSort==0 ){
136774 codeOffset(v, p->iOffset, iContinue);
136775 }
136776 }
136777
136778 switch( eDest ){
136779 /* In this mode, write each query result to the key of the temporary
136780 ** table iParm.
136781 */
136782#ifndef SQLITE_OMIT_COMPOUND_SELECT
136783 case SRT_Union1: {
136784 int r1;
136785 r1 = sqlite3GetTempReg(pParse);
136786 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regResult, nResultCol, r1);
136787 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iParm, r1, regResult, nResultCol);
136788 sqlite3ReleaseTempReg(pParse, r1);
136789 break;
136790 }
136791
136792 /* Construct a record from the query result, but instead of
136793 ** saving that record, use it as a key to delete elements from
136794 ** the temporary table iParm.
136795 */
136796 case SRT_Except2: {
136797 sqlite3VdbeAddOp3(v, OP_IdxDelete140, iParm, regResult, nResultCol);
136798 break;
136799 }
136800#endif /* SQLITE_OMIT_COMPOUND_SELECT */
136801
136802 /* Store the result as data using a unique key.
136803 */
136804 case SRT_Fifo8:
136805 case SRT_DistFifo5:
136806 case SRT_Table14:
136807 case SRT_EphemTab12: {
136808 int r1 = sqlite3GetTempRange(pParse, nPrefixReg+1);
136809 testcase( eDest==SRT_Table );
136810 testcase( eDest==SRT_EphemTab );
136811 testcase( eDest==SRT_Fifo );
136812 testcase( eDest==SRT_DistFifo );
136813 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regResult, nResultCol, r1+nPrefixReg);
136814#ifndef SQLITE_OMIT_CTE
136815 if( eDest==SRT_DistFifo5 ){
136816 /* If the destination is DistFifo, then cursor (iParm+1) is open
136817 ** on an ephemeral index. If the current row is already present
136818 ** in the index, do not write it to the output. If not, add the
136819 ** current row to the index and proceed with writing it to the
136820 ** output table as well. */
136821 int addr = sqlite3VdbeCurrentAddr(v) + 4;
136822 sqlite3VdbeAddOp4Int(v, OP_Found28, iParm+1, addr, r1, 0);
136823 VdbeCoverage(v);
136824 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iParm+1, r1,regResult,nResultCol);
136825 assert( pSort==0 )((void) (0));
136826 }
136827#endif
136828 if( pSort ){
136829 assert( regResult==regOrig )((void) (0));
136830 pushOntoSorter(pParse, pSort, p, r1+nPrefixReg, regOrig, 1, nPrefixReg);
136831 }else{
136832 int r2 = sqlite3GetTempReg(pParse);
136833 sqlite3VdbeAddOp2(v, OP_NewRowid127, iParm, r2);
136834 sqlite3VdbeAddOp3(v, OP_Insert128, iParm, r1, r2);
136835 sqlite3VdbeChangeP5(v, OPFLAG_APPEND0x08);
136836 sqlite3ReleaseTempReg(pParse, r2);
136837 }
136838 sqlite3ReleaseTempRange(pParse, r1, nPrefixReg+1);
136839 break;
136840 }
136841
136842 case SRT_Upfrom15: {
136843 if( pSort ){
136844 pushOntoSorter(
136845 pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
136846 }else{
136847 int i2 = pDest->iSDParm2;
136848 int r1 = sqlite3GetTempReg(pParse);
136849
136850 /* If the UPDATE FROM join is an aggregate that matches no rows, it
136851 ** might still be trying to return one row, because that is what
136852 ** aggregates do. Don't record that empty row in the output table. */
136853 sqlite3VdbeAddOp2(v, OP_IsNull50, regResult, iBreak); VdbeCoverage(v);
136854
136855 sqlite3VdbeAddOp3(v, OP_MakeRecord97,
136856 regResult+(i2<0), nResultCol-(i2<0), r1);
136857 if( i2<0 ){
136858 sqlite3VdbeAddOp3(v, OP_Insert128, iParm, r1, regResult);
136859 }else{
136860 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iParm, r1, regResult, i2);
136861 }
136862 }
136863 break;
136864 }
136865
136866#ifndef SQLITE_OMIT_SUBQUERY
136867 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
136868 ** then there should be a single item on the stack. Write this
136869 ** item into the set table with bogus data.
136870 */
136871 case SRT_Set11: {
136872 if( pSort ){
136873 /* At first glance you would think we could optimize out the
136874 ** ORDER BY in this case since the order of entries in the set
136875 ** does not matter. But there might be a LIMIT clause, in which
136876 ** case the order does matter */
136877 pushOntoSorter(
136878 pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
136879 }else{
136880 int r1 = sqlite3GetTempReg(pParse);
136881 assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol )((void) (0));
136882 sqlite3VdbeAddOp4(v, OP_MakeRecord97, regResult, nResultCol,
136883 r1, pDest->zAffSdst, nResultCol);
136884 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iParm, r1, regResult, nResultCol);
136885 sqlite3ReleaseTempReg(pParse, r1);
136886 }
136887 break;
136888 }
136889
136890
136891 /* If any row exist in the result set, record that fact and abort.
136892 */
136893 case SRT_Exists3: {
136894 sqlite3VdbeAddOp2(v, OP_Integer71, 1, iParm);
136895 /* The LIMIT clause will terminate the loop for us */
136896 break;
136897 }
136898
136899 /* If this is a scalar select that is part of an expression, then
136900 ** store the results in the appropriate memory cell or array of
136901 ** memory cells and break out of the scan loop.
136902 */
136903 case SRT_Mem10: {
136904 if( pSort ){
136905 assert( nResultCol<=pDest->nSdst )((void) (0));
136906 pushOntoSorter(
136907 pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
136908 }else{
136909 assert( nResultCol==pDest->nSdst )((void) (0));
136910 assert( regResult==iParm )((void) (0));
136911 /* The LIMIT clause will jump out of the loop for us */
136912 }
136913 break;
136914 }
136915#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
136916
136917 case SRT_Coroutine13: /* Send data to a co-routine */
136918 case SRT_Output9: { /* Return the results */
136919 testcase( eDest==SRT_Coroutine );
136920 testcase( eDest==SRT_Output );
136921 if( pSort ){
136922 pushOntoSorter(pParse, pSort, p, regResult, regOrig, nResultCol,
136923 nPrefixReg);
136924 }else if( eDest==SRT_Coroutine13 ){
136925 sqlite3VdbeAddOp1(v, OP_Yield11, pDest->iSDParm);
136926 }else{
136927 sqlite3VdbeAddOp2(v, OP_ResultRow84, regResult, nResultCol);
136928 }
136929 break;
136930 }
136931
136932#ifndef SQLITE_OMIT_CTE
136933 /* Write the results into a priority queue that is order according to
136934 ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
136935 ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
136936 ** pSO->nExpr columns, then make sure all keys are unique by adding a
136937 ** final OP_Sequence column. The last column is the record as a blob.
136938 */
136939 case SRT_DistQueue6:
136940 case SRT_Queue7: {
136941 int nKey;
136942 int r1, r2, r3;
136943 int addrTest = 0;
136944 ExprList *pSO;
136945 pSO = pDest->pOrderBy;
136946 assert( pSO )((void) (0));
136947 nKey = pSO->nExpr;
136948 r1 = sqlite3GetTempReg(pParse);
136949 r2 = sqlite3GetTempRange(pParse, nKey+2);
136950 r3 = r2+nKey+1;
136951 if( eDest==SRT_DistQueue6 ){
136952 /* If the destination is DistQueue, then cursor (iParm+1) is open
136953 ** on a second ephemeral index that holds all values every previously
136954 ** added to the queue. */
136955 addrTest = sqlite3VdbeAddOp4Int(v, OP_Found28, iParm+1, 0,
136956 regResult, nResultCol);
136957 VdbeCoverage(v);
136958 }
136959 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regResult, nResultCol, r3);
136960 if( eDest==SRT_DistQueue6 ){
136961 sqlite3VdbeAddOp2(v, OP_IdxInsert138, iParm+1, r3);
136962 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT0x10);
136963 }
136964 for(i=0; i<nKey; i++){
136965 sqlite3VdbeAddOp2(v, OP_SCopy81,
136966 regResult + pSO->a[i].u.x.iOrderByCol - 1,
136967 r2+i);
136968 }
136969 sqlite3VdbeAddOp2(v, OP_Sequence126, iParm, r2+nKey);
136970 sqlite3VdbeAddOp3(v, OP_MakeRecord97, r2, nKey+2, r1);
136971 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iParm, r1, r2, nKey+2);
136972 if( addrTest ) sqlite3VdbeJumpHere(v, addrTest);
136973 sqlite3ReleaseTempReg(pParse, r1);
136974 sqlite3ReleaseTempRange(pParse, r2, nKey+2);
136975 break;
136976 }
136977#endif /* SQLITE_OMIT_CTE */
136978
136979
136980
136981#if !defined(SQLITE_OMIT_TRIGGER)
136982 /* Discard the results. This is used for SELECT statements inside
136983 ** the body of a TRIGGER. The purpose of such selects is to call
136984 ** user-defined functions that have side effects. We do not care
136985 ** about the actual results of the select.
136986 */
136987 default: {
136988 assert( eDest==SRT_Discard )((void) (0));
136989 break;
136990 }
136991#endif
136992 }
136993
136994 /* Jump to the end of the loop if the LIMIT is reached. Except, if
136995 ** there is a sorter, in which case the sorter has already limited
136996 ** the output for us.
136997 */
136998 if( pSort==0 && p->iLimit ){
136999 sqlite3VdbeAddOp2(v, OP_DecrJumpZero60, p->iLimit, iBreak); VdbeCoverage(v);
137000 }
137001}
137002
137003/*
137004** Allocate a KeyInfo object sufficient for an index of N key columns and
137005** X extra columns.
137006*/
137007SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){
137008 int nExtra = (N+X)*(sizeof(CollSeq*)+1) - sizeof(CollSeq*);
137009 KeyInfo *p = sqlite3DbMallocRawNN(db, sizeof(KeyInfo) + nExtra);
137010 if( p ){
137011 p->aSortFlags = (u8*)&p->aColl[N+X];
137012 p->nKeyField = (u16)N;
137013 p->nAllField = (u16)(N+X);
137014 p->enc = ENC(db)((db)->enc);
137015 p->db = db;
137016 p->nRef = 1;
137017 memset(&p[1], 0, nExtra);
137018 }else{
137019 return (KeyInfo*)sqlite3OomFault(db);
137020 }
137021 return p;
137022}
137023
137024/*
137025** Deallocate a KeyInfo object
137026*/
137027SQLITE_PRIVATEstatic void sqlite3KeyInfoUnref(KeyInfo *p){
137028 if( p ){
137029 assert( p->nRef>0 )((void) (0));
137030 p->nRef--;
137031 if( p->nRef==0 ) sqlite3DbFreeNN(p->db, p);
137032 }
137033}
137034
137035/*
137036** Make a new pointer to a KeyInfo object
137037*/
137038SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoRef(KeyInfo *p){
137039 if( p ){
137040 assert( p->nRef>0 )((void) (0));
137041 p->nRef++;
137042 }
137043 return p;
137044}
137045
137046#ifdef SQLITE_DEBUG
137047/*
137048** Return TRUE if a KeyInfo object can be change. The KeyInfo object
137049** can only be changed if this is just a single reference to the object.
137050**
137051** This routine is used only inside of assert() statements.
137052*/
137053SQLITE_PRIVATEstatic int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
137054#endif /* SQLITE_DEBUG */
137055
137056/*
137057** Given an expression list, generate a KeyInfo structure that records
137058** the collating sequence for each expression in that expression list.
137059**
137060** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
137061** KeyInfo structure is appropriate for initializing a virtual index to
137062** implement that clause. If the ExprList is the result set of a SELECT
137063** then the KeyInfo structure is appropriate for initializing a virtual
137064** index to implement a DISTINCT test.
137065**
137066** Space to hold the KeyInfo structure is obtained from malloc. The calling
137067** function is responsible for seeing that this structure is eventually
137068** freed.
137069*/
137070SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoFromExprList(
137071 Parse *pParse, /* Parsing context */
137072 ExprList *pList, /* Form the KeyInfo object from this ExprList */
137073 int iStart, /* Begin with this column of pList */
137074 int nExtra /* Add this many extra columns to the end */
137075){
137076 int nExpr;
137077 KeyInfo *pInfo;
137078 struct ExprList_item *pItem;
137079 sqlite3 *db = pParse->db;
137080 int i;
137081
137082 nExpr = pList->nExpr;
137083 pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
137084 if( pInfo ){
137085 assert( sqlite3KeyInfoIsWriteable(pInfo) )((void) (0));
137086 for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
137087 pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr);
137088 pInfo->aSortFlags[i-iStart] = pItem->fg.sortFlags;
137089 }
137090 }
137091 return pInfo;
137092}
137093
137094/*
137095** Name of the connection operator, used for error messages.
137096*/
137097SQLITE_PRIVATEstatic const char *sqlite3SelectOpName(int id){
137098 char *z;
137099 switch( id ){
137100 case TK_ALL135: z = "UNION ALL"; break;
137101 case TK_INTERSECT137: z = "INTERSECT"; break;
137102 case TK_EXCEPT136: z = "EXCEPT"; break;
137103 default: z = "UNION"; break;
137104 }
137105 return z;
137106}
137107
137108#ifndef SQLITE_OMIT_EXPLAIN
137109/*
137110** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
137111** is a no-op. Otherwise, it adds a single row of output to the EQP result,
137112** where the caption is of the form:
137113**
137114** "USE TEMP B-TREE FOR xxx"
137115**
137116** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
137117** is determined by the zUsage argument.
137118*/
137119static void explainTempTable(Parse *pParse, const char *zUsage){
137120 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage))sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR %s", zUsage
)
;
137121}
137122
137123/*
137124** Assign expression b to lvalue a. A second, no-op, version of this macro
137125** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
137126** in sqlite3Select() to assign values to structure member variables that
137127** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
137128** code with #ifndef directives.
137129*/
137130# define explainSetInteger(a, b)a = b a = b
137131
137132#else
137133/* No-op versions of the explainXXX() functions and macros. */
137134# define explainTempTable(y,z)
137135# define explainSetInteger(y,z)y = z
137136#endif
137137
137138
137139/*
137140** If the inner loop was generated using a non-null pOrderBy argument,
137141** then the results were placed in a sorter. After the loop is terminated
137142** we need to run the sorter and output the results. The following
137143** routine generates the code needed to do that.
137144*/
137145static void generateSortTail(
137146 Parse *pParse, /* Parsing context */
137147 Select *p, /* The SELECT statement */
137148 SortCtx *pSort, /* Information on the ORDER BY clause */
137149 int nColumn, /* Number of columns of data */
137150 SelectDest *pDest /* Write the sorted results here */
137151){
137152 Vdbe *v = pParse->pVdbe; /* The prepared statement */
137153 int addrBreak = pSort->labelDone; /* Jump here to exit loop */
137154 int addrContinue = sqlite3VdbeMakeLabel(pParse);/* Jump here for next cycle */
137155 int addr; /* Top of output loop. Jump for Next. */
137156 int addrOnce = 0;
137157 int iTab;
137158 ExprList *pOrderBy = pSort->pOrderBy;
137159 int eDest = pDest->eDest;
137160 int iParm = pDest->iSDParm;
137161 int regRow;
137162 int regRowid;
137163 int iCol;
137164 int nKey; /* Number of key columns in sorter record */
137165 int iSortTab; /* Sorter cursor to read from */
137166 int i;
137167 int bSeq; /* True if sorter record includes seq. no. */
137168 int nRefKey = 0;
137169 struct ExprList_item *aOutEx = p->pEList->a;
137170
137171 assert( addrBreak<0 )((void) (0));
137172 if( pSort->labelBkOut ){
137173 sqlite3VdbeAddOp2(v, OP_Gosub9, pSort->regReturn, pSort->labelBkOut);
137174 sqlite3VdbeGoto(v, addrBreak);
137175 sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
137176 }
137177
137178#ifdef SQLITE_ENABLE_SORTER_REFERENCES
137179 /* Open any cursors needed for sorter-reference expressions */
137180 for(i=0; i<pSort->nDefer; i++){
137181 Table *pTab = pSort->aDefer[i].pTab;
137182 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
137183 sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead112);
137184 nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey)((nRefKey)>(pSort->aDefer[i].nKey)?(nRefKey):(pSort->
aDefer[i].nKey))
;
137185 }
137186#endif
137187
137188 iTab = pSort->iECursor;
137189 if( eDest==SRT_Output9 || eDest==SRT_Coroutine13 || eDest==SRT_Mem10 ){
137190 if( eDest==SRT_Mem10 && p->iOffset ){
137191 sqlite3VdbeAddOp2(v, OP_Null75, 0, pDest->iSdst);
137192 }
137193 regRowid = 0;
137194 regRow = pDest->iSdst;
137195 }else{
137196 regRowid = sqlite3GetTempReg(pParse);
137197 if( eDest==SRT_EphemTab12 || eDest==SRT_Table14 ){
137198 regRow = sqlite3GetTempReg(pParse);
137199 nColumn = 0;
137200 }else{
137201 regRow = sqlite3GetTempRange(pParse, nColumn);
137202 }
137203 }
137204 nKey = pOrderBy->nExpr - pSort->nOBSat;
137205 if( pSort->sortFlags & SORTFLAG_UseSorter0x01 ){
137206 int regSortOut = ++pParse->nMem;
137207 iSortTab = pParse->nTab++;
137208 if( pSort->labelBkOut ){
137209 addrOnce = sqlite3VdbeAddOp0(v, OP_Once14); VdbeCoverage(v);
137210 }
137211 sqlite3VdbeAddOp3(v, OP_OpenPseudo121, iSortTab, regSortOut,
137212 nKey+1+nColumn+nRefKey);
137213 if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
137214 addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort33, iTab, addrBreak);
137215 VdbeCoverage(v);
137216 codeOffset(v, p->iOffset, addrContinue);
137217 sqlite3VdbeAddOp3(v, OP_SorterData133, iTab, regSortOut, iSortTab);
137218 bSeq = 0;
137219 }else{
137220 addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort34, iTab, addrBreak); VdbeCoverage(v);
137221 codeOffset(v, p->iOffset, addrContinue);
137222 iSortTab = iTab;
137223 bSeq = 1;
137224 }
137225 for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
137226#ifdef SQLITE_ENABLE_SORTER_REFERENCES
137227 if( aOutEx[i].fg.bSorterRef ) continue;
137228#endif
137229 if( aOutEx[i].u.x.iOrderByCol==0 ) iCol++;
137230 }
137231#ifdef SQLITE_ENABLE_SORTER_REFERENCES
137232 if( pSort->nDefer ){
137233 int iKey = iCol+1;
137234 int regKey = sqlite3GetTempRange(pParse, nRefKey);
137235
137236 for(i=0; i<pSort->nDefer; i++){
137237 int iCsr = pSort->aDefer[i].iCsr;
137238 Table *pTab = pSort->aDefer[i].pTab;
137239 int nKey = pSort->aDefer[i].nKey;
137240
137241 sqlite3VdbeAddOp1(v, OP_NullRow136, iCsr);
137242 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
137243 sqlite3VdbeAddOp3(v, OP_Column94, iSortTab, iKey++, regKey);
137244 sqlite3VdbeAddOp3(v, OP_SeekRowid29, iCsr,
137245 sqlite3VdbeCurrentAddr(v)+1, regKey);
137246 }else{
137247 int k;
137248 int iJmp;
137249 assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey )((void) (0));
137250 for(k=0; k<nKey; k++){
137251 sqlite3VdbeAddOp3(v, OP_Column94, iSortTab, iKey++, regKey+k);
137252 }
137253 iJmp = sqlite3VdbeCurrentAddr(v);
137254 sqlite3VdbeAddOp4Int(v, OP_SeekGE22, iCsr, iJmp+2, regKey, nKey);
137255 sqlite3VdbeAddOp4Int(v, OP_IdxLE39, iCsr, iJmp+3, regKey, nKey);
137256 sqlite3VdbeAddOp1(v, OP_NullRow136, iCsr);
137257 }
137258 }
137259 sqlite3ReleaseTempRange(pParse, regKey, nRefKey);
137260 }
137261#endif
137262 for(i=nColumn-1; i>=0; i--){
137263#ifdef SQLITE_ENABLE_SORTER_REFERENCES
137264 if( aOutEx[i].fg.bSorterRef ){
137265 sqlite3ExprCode(pParse, aOutEx[i].pExpr, regRow+i);
137266 }else
137267#endif
137268 {
137269 int iRead;
137270 if( aOutEx[i].u.x.iOrderByCol ){
137271 iRead = aOutEx[i].u.x.iOrderByCol-1;
137272 }else{
137273 iRead = iCol--;
137274 }
137275 sqlite3VdbeAddOp3(v, OP_Column94, iSortTab, iRead, regRow+i);
137276 VdbeComment((v, "%s", aOutEx[i].zEName));
137277 }
137278 }
137279 switch( eDest ){
137280 case SRT_Table14:
137281 case SRT_EphemTab12: {
137282 sqlite3VdbeAddOp3(v, OP_Column94, iSortTab, nKey+bSeq, regRow);
137283 sqlite3VdbeAddOp2(v, OP_NewRowid127, iParm, regRowid);
137284 sqlite3VdbeAddOp3(v, OP_Insert128, iParm, regRow, regRowid);
137285 sqlite3VdbeChangeP5(v, OPFLAG_APPEND0x08);
137286 break;
137287 }
137288#ifndef SQLITE_OMIT_SUBQUERY
137289 case SRT_Set11: {
137290 assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) )((void) (0));
137291 sqlite3VdbeAddOp4(v, OP_MakeRecord97, regRow, nColumn, regRowid,
137292 pDest->zAffSdst, nColumn);
137293 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iParm, regRowid, regRow, nColumn);
137294 break;
137295 }
137296 case SRT_Mem10: {
137297 /* The LIMIT clause will terminate the loop for us */
137298 break;
137299 }
137300#endif
137301 case SRT_Upfrom15: {
137302 int i2 = pDest->iSDParm2;
137303 int r1 = sqlite3GetTempReg(pParse);
137304 sqlite3VdbeAddOp3(v, OP_MakeRecord97,regRow+(i2<0),nColumn-(i2<0),r1);
137305 if( i2<0 ){
137306 sqlite3VdbeAddOp3(v, OP_Insert128, iParm, r1, regRow);
137307 }else{
137308 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iParm, r1, regRow, i2);
137309 }
137310 break;
137311 }
137312 default: {
137313 assert( eDest==SRT_Output || eDest==SRT_Coroutine )((void) (0));
137314 testcase( eDest==SRT_Output );
137315 testcase( eDest==SRT_Coroutine );
137316 if( eDest==SRT_Output9 ){
137317 sqlite3VdbeAddOp2(v, OP_ResultRow84, pDest->iSdst, nColumn);
137318 }else{
137319 sqlite3VdbeAddOp1(v, OP_Yield11, pDest->iSDParm);
137320 }
137321 break;
137322 }
137323 }
137324 if( regRowid ){
137325 if( eDest==SRT_Set11 ){
137326 sqlite3ReleaseTempRange(pParse, regRow, nColumn);
137327 }else{
137328 sqlite3ReleaseTempReg(pParse, regRow);
137329 }
137330 sqlite3ReleaseTempReg(pParse, regRowid);
137331 }
137332 /* The bottom of the loop
137333 */
137334 sqlite3VdbeResolveLabel(v, addrContinue);
137335 if( pSort->sortFlags & SORTFLAG_UseSorter0x01 ){
137336 sqlite3VdbeAddOp2(v, OP_SorterNext36, iTab, addr); VdbeCoverage(v);
137337 }else{
137338 sqlite3VdbeAddOp2(v, OP_Next38, iTab, addr); VdbeCoverage(v);
137339 }
137340 if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return67, pSort->regReturn);
137341 sqlite3VdbeResolveLabel(v, addrBreak);
137342}
137343
137344/*
137345** Return a pointer to a string containing the 'declaration type' of the
137346** expression pExpr. The string may be treated as static by the caller.
137347**
137348** Also try to estimate the size of the returned value and return that
137349** result in *pEstWidth.
137350**
137351** The declaration type is the exact datatype definition extracted from the
137352** original CREATE TABLE statement if the expression is a column. The
137353** declaration type for a ROWID field is INTEGER. Exactly when an expression
137354** is considered a column can be complex in the presence of subqueries. The
137355** result-set expression in all of the following SELECT statements is
137356** considered a column by this function.
137357**
137358** SELECT col FROM tbl;
137359** SELECT (SELECT col FROM tbl;
137360** SELECT (SELECT col FROM tbl);
137361** SELECT abc FROM (SELECT col AS abc FROM tbl);
137362**
137363** The declaration type for any expression other than a column is NULL.
137364**
137365** This routine has either 3 or 6 parameters depending on whether or not
137366** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
137367*/
137368#ifdef SQLITE_ENABLE_COLUMN_METADATA1
137369# define columnType(A,B,C,D,E)columnTypeImpl(A,B,C,D,E) columnTypeImpl(A,B,C,D,E)
137370#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
137371# define columnType(A,B,C,D,E)columnTypeImpl(A,B,C,D,E) columnTypeImpl(A,B)
137372#endif
137373static const char *columnTypeImpl(
137374 NameContext *pNC,
137375#ifndef SQLITE_ENABLE_COLUMN_METADATA1
137376 Expr *pExpr
137377#else
137378 Expr *pExpr,
137379 const char **pzOrigDb,
137380 const char **pzOrigTab,
137381 const char **pzOrigCol
137382#endif
137383){
137384 char const *zType = 0;
137385 int j;
137386#ifdef SQLITE_ENABLE_COLUMN_METADATA1
137387 char const *zOrigDb = 0;
137388 char const *zOrigTab = 0;
137389 char const *zOrigCol = 0;
137390#endif
137391
137392 assert( pExpr!=0 )((void) (0));
137393 assert( pNC->pSrcList!=0 )((void) (0));
137394 switch( pExpr->op ){
137395 case TK_COLUMN167: {
137396 /* The expression is a column. Locate the table the column is being
137397 ** extracted from in NameContext.pSrcList. This table may be real
137398 ** database table or a subquery.
137399 */
137400 Table *pTab = 0; /* Table structure column is extracted from */
137401 Select *pS = 0; /* Select the column is extracted from */
137402 int iCol = pExpr->iColumn; /* Index of column in pTab */
137403 while( pNC && !pTab ){
137404 SrcList *pTabList = pNC->pSrcList;
137405 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
137406 if( j<pTabList->nSrc ){
137407 pTab = pTabList->a[j].pTab;
137408 pS = pTabList->a[j].pSelect;
137409 }else{
137410 pNC = pNC->pNext;
137411 }
137412 }
137413
137414 if( pTab==0 ){
137415 /* At one time, code such as "SELECT new.x" within a trigger would
137416 ** cause this condition to run. Since then, we have restructured how
137417 ** trigger code is generated and so this condition is no longer
137418 ** possible. However, it can still be true for statements like
137419 ** the following:
137420 **
137421 ** CREATE TABLE t1(col INTEGER);
137422 ** SELECT (SELECT t1.col) FROM FROM t1;
137423 **
137424 ** when columnType() is called on the expression "t1.col" in the
137425 ** sub-select. In this case, set the column type to NULL, even
137426 ** though it should really be "INTEGER".
137427 **
137428 ** This is not a problem, as the column type of "t1.col" is never
137429 ** used. When columnType() is called on the expression
137430 ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
137431 ** branch below. */
137432 break;
137433 }
137434
137435 assert( pTab && ExprUseYTab(pExpr) && pExpr->y.pTab==pTab )((void) (0));
137436 if( pS ){
137437 /* The "table" is actually a sub-select or a view in the FROM clause
137438 ** of the SELECT statement. Return the declaration type and origin
137439 ** data for the result-set column of the sub-select.
137440 */
137441 if( iCol<pS->pEList->nExpr
137442#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
137443 && iCol>=0
137444#else
137445 && ALWAYS(iCol>=0)(iCol>=0)
137446#endif
137447 ){
137448 /* If iCol is less than zero, then the expression requests the
137449 ** rowid of the sub-select or view. This expression is legal (see
137450 ** test case misc2.2.2) - it always evaluates to NULL.
137451 */
137452 NameContext sNC;
137453 Expr *p = pS->pEList->a[iCol].pExpr;
137454 sNC.pSrcList = pS->pSrc;
137455 sNC.pNext = pNC;
137456 sNC.pParse = pNC->pParse;
137457 zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol)columnTypeImpl(&sNC,p,&zOrigDb,&zOrigTab,&zOrigCol
)
;
137458 }
137459 }else{
137460 /* A real table or a CTE table */
137461 assert( !pS )((void) (0));
137462#ifdef SQLITE_ENABLE_COLUMN_METADATA1
137463 if( iCol<0 ) iCol = pTab->iPKey;
137464 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) )((void) (0));
137465 if( iCol<0 ){
137466 zType = "INTEGER";
137467 zOrigCol = "rowid";
137468 }else{
137469 zOrigCol = pTab->aCol[iCol].zCnName;
137470 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
137471 }
137472 zOrigTab = pTab->zName;
137473 if( pNC->pParse && pTab->pSchema ){
137474 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
137475 zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
137476 }
137477#else
137478 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) )((void) (0));
137479 if( iCol<0 ){
137480 zType = "INTEGER";
137481 }else{
137482 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
137483 }
137484#endif
137485 }
137486 break;
137487 }
137488#ifndef SQLITE_OMIT_SUBQUERY
137489 case TK_SELECT138: {
137490 /* The expression is a sub-select. Return the declaration type and
137491 ** origin info for the single column in the result set of the SELECT
137492 ** statement.
137493 */
137494 NameContext sNC;
137495 Select *pS;
137496 Expr *p;
137497 assert( ExprUseXSelect(pExpr) )((void) (0));
137498 pS = pExpr->x.pSelect;
137499 p = pS->pEList->a[0].pExpr;
137500 sNC.pSrcList = pS->pSrc;
137501 sNC.pNext = pNC;
137502 sNC.pParse = pNC->pParse;
137503 zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol)columnTypeImpl(&sNC,p,&zOrigDb,&zOrigTab,&zOrigCol
)
;
137504 break;
137505 }
137506#endif
137507 }
137508
137509#ifdef SQLITE_ENABLE_COLUMN_METADATA1
137510 if( pzOrigDb ){
137511 assert( pzOrigTab && pzOrigCol )((void) (0));
137512 *pzOrigDb = zOrigDb;
137513 *pzOrigTab = zOrigTab;
137514 *pzOrigCol = zOrigCol;
137515 }
137516#endif
137517 return zType;
137518}
137519
137520/*
137521** Generate code that will tell the VDBE the declaration types of columns
137522** in the result set.
137523*/
137524static void generateColumnTypes(
137525 Parse *pParse, /* Parser context */
137526 SrcList *pTabList, /* List of tables */
137527 ExprList *pEList /* Expressions defining the result set */
137528){
137529#ifndef SQLITE_OMIT_DECLTYPE
137530 Vdbe *v = pParse->pVdbe;
137531 int i;
137532 NameContext sNC;
137533 sNC.pSrcList = pTabList;
137534 sNC.pParse = pParse;
137535 sNC.pNext = 0;
137536 for(i=0; i<pEList->nExpr; i++){
137537 Expr *p = pEList->a[i].pExpr;
137538 const char *zType;
137539#ifdef SQLITE_ENABLE_COLUMN_METADATA1
137540 const char *zOrigDb = 0;
137541 const char *zOrigTab = 0;
137542 const char *zOrigCol = 0;
137543 zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol)columnTypeImpl(&sNC,p,&zOrigDb,&zOrigTab,&zOrigCol
)
;
137544
137545 /* The vdbe must make its own copy of the column-type and other
137546 ** column specific strings, in case the schema is reset before this
137547 ** virtual machine is deleted.
137548 */
137549 sqlite3VdbeSetColName(v, i, COLNAME_DATABASE2, zOrigDb, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
137550 sqlite3VdbeSetColName(v, i, COLNAME_TABLE3, zOrigTab, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
137551 sqlite3VdbeSetColName(v, i, COLNAME_COLUMN4, zOrigCol, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
137552#else
137553 zType = columnType(&sNC, p, 0, 0, 0)columnTypeImpl(&sNC,p,0,0,0);
137554#endif
137555 sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE1, zType, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
137556 }
137557#endif /* !defined(SQLITE_OMIT_DECLTYPE) */
137558}
137559
137560
137561/*
137562** Compute the column names for a SELECT statement.
137563**
137564** The only guarantee that SQLite makes about column names is that if the
137565** column has an AS clause assigning it a name, that will be the name used.
137566** That is the only documented guarantee. However, countless applications
137567** developed over the years have made baseless assumptions about column names
137568** and will break if those assumptions changes. Hence, use extreme caution
137569** when modifying this routine to avoid breaking legacy.
137570**
137571** See Also: sqlite3ColumnsFromExprList()
137572**
137573** The PRAGMA short_column_names and PRAGMA full_column_names settings are
137574** deprecated. The default setting is short=ON, full=OFF. 99.9% of all
137575** applications should operate this way. Nevertheless, we need to support the
137576** other modes for legacy:
137577**
137578** short=OFF, full=OFF: Column name is the text of the expression has it
137579** originally appears in the SELECT statement. In
137580** other words, the zSpan of the result expression.
137581**
137582** short=ON, full=OFF: (This is the default setting). If the result
137583** refers directly to a table column, then the
137584** result column name is just the table column
137585** name: COLUMN. Otherwise use zSpan.
137586**
137587** full=ON, short=ANY: If the result refers directly to a table column,
137588** then the result column name with the table name
137589** prefix, ex: TABLE.COLUMN. Otherwise use zSpan.
137590*/
137591SQLITE_PRIVATEstatic void sqlite3GenerateColumnNames(
137592 Parse *pParse, /* Parser context */
137593 Select *pSelect /* Generate column names for this SELECT statement */
137594){
137595 Vdbe *v = pParse->pVdbe;
137596 int i;
137597 Table *pTab;
137598 SrcList *pTabList;
137599 ExprList *pEList;
137600 sqlite3 *db = pParse->db;
137601 int fullName; /* TABLE.COLUMN if no AS clause and is a direct table ref */
137602 int srcName; /* COLUMN or TABLE.COLUMN if no AS clause and is direct */
137603
137604#ifndef SQLITE_OMIT_EXPLAIN
137605 /* If this is an EXPLAIN, skip this step */
137606 if( pParse->explain ){
137607 return;
137608 }
137609#endif
137610
137611 if( pParse->colNamesSet ) return;
137612 /* Column names are determined by the left-most term of a compound select */
137613 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
137614 SELECTTRACE(1,pParse,pSelect,("generating column names\n"));
137615 pTabList = pSelect->pSrc;
137616 pEList = pSelect->pEList;
137617 assert( v!=0 )((void) (0));
137618 assert( pTabList!=0 )((void) (0));
137619 pParse->colNamesSet = 1;
137620 fullName = (db->flags & SQLITE_FullColNames0x00000004)!=0;
137621 srcName = (db->flags & SQLITE_ShortColNames0x00000040)!=0 || fullName;
137622 sqlite3VdbeSetNumCols(v, pEList->nExpr);
137623 for(i=0; i<pEList->nExpr; i++){
137624 Expr *p = pEList->a[i].pExpr;
137625
137626 assert( p!=0 )((void) (0));
137627 assert( p->op!=TK_AGG_COLUMN )((void) (0)); /* Agg processing has not run yet */
137628 assert( p->op!=TK_COLUMN((void) (0))
137629 || (ExprUseYTab(p) && p->y.pTab!=0) )((void) (0)); /* Covering idx not yet coded */
137630 if( pEList->a[i].zEName && pEList->a[i].fg.eEName==ENAME_NAME0 ){
137631 /* An AS clause always takes first priority */
137632 char *zName = pEList->a[i].zEName;
137633 sqlite3VdbeSetColName(v, i, COLNAME_NAME0, zName, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
137634 }else if( srcName && p->op==TK_COLUMN167 ){
137635 char *zCol;
137636 int iCol = p->iColumn;
137637 pTab = p->y.pTab;
137638 assert( pTab!=0 )((void) (0));
137639 if( iCol<0 ) iCol = pTab->iPKey;
137640 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) )((void) (0));
137641 if( iCol<0 ){
137642 zCol = "rowid";
137643 }else{
137644 zCol = pTab->aCol[iCol].zCnName;
137645 }
137646 if( fullName ){
137647 char *zName = 0;
137648 zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
137649 sqlite3VdbeSetColName(v, i, COLNAME_NAME0, zName, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
137650 }else{
137651 sqlite3VdbeSetColName(v, i, COLNAME_NAME0, zCol, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
137652 }
137653 }else{
137654 const char *z = pEList->a[i].zEName;
137655 z = z==0 ? sqlite3MPrintf(db, "column%d", i+1) : sqlite3DbStrDup(db, z);
137656 sqlite3VdbeSetColName(v, i, COLNAME_NAME0, z, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
137657 }
137658 }
137659 generateColumnTypes(pParse, pTabList, pEList);
137660}
137661
137662/*
137663** Given an expression list (which is really the list of expressions
137664** that form the result set of a SELECT statement) compute appropriate
137665** column names for a table that would hold the expression list.
137666**
137667** All column names will be unique.
137668**
137669** Only the column names are computed. Column.zType, Column.zColl,
137670** and other fields of Column are zeroed.
137671**
137672** Return SQLITE_OK on success. If a memory allocation error occurs,
137673** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
137674**
137675** The only guarantee that SQLite makes about column names is that if the
137676** column has an AS clause assigning it a name, that will be the name used.
137677** That is the only documented guarantee. However, countless applications
137678** developed over the years have made baseless assumptions about column names
137679** and will break if those assumptions changes. Hence, use extreme caution
137680** when modifying this routine to avoid breaking legacy.
137681**
137682** See Also: sqlite3GenerateColumnNames()
137683*/
137684SQLITE_PRIVATEstatic int sqlite3ColumnsFromExprList(
137685 Parse *pParse, /* Parsing context */
137686 ExprList *pEList, /* Expr list from which to derive column names */
137687 i16 *pnCol, /* Write the number of columns here */
137688 Column **paCol /* Write the new column list here */
137689){
137690 sqlite3 *db = pParse->db; /* Database connection */
137691 int i, j; /* Loop counters */
137692 u32 cnt; /* Index added to make the name unique */
137693 Column *aCol, *pCol; /* For looping over result columns */
137694 int nCol; /* Number of columns in the result set */
137695 char *zName; /* Column name */
137696 int nName; /* Size of name in zName[] */
137697 Hash ht; /* Hash table of column names */
137698 Table *pTab;
137699
137700 sqlite3HashInit(&ht);
137701 if( pEList ){
137702 nCol = pEList->nExpr;
137703 aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
137704 testcase( aCol==0 );
137705 if( NEVER(nCol>32767)(nCol>32767) ) nCol = 32767;
137706 }else{
137707 nCol = 0;
137708 aCol = 0;
137709 }
137710 assert( nCol==(i16)nCol )((void) (0));
137711 *pnCol = nCol;
137712 *paCol = aCol;
137713
137714 for(i=0, pCol=aCol; i<nCol && !db->mallocFailed; i++, pCol++){
137715 struct ExprList_item *pX = &pEList->a[i];
137716 struct ExprList_item *pCollide;
137717 /* Get an appropriate name for the column
137718 */
137719 if( (zName = pX->zEName)!=0 && pX->fg.eEName==ENAME_NAME0 ){
137720 /* If the column contains an "AS <name>" phrase, use <name> as the name */
137721 }else{
137722 Expr *pColExpr = sqlite3ExprSkipCollateAndLikely(pX->pExpr);
137723 while( ALWAYS(pColExpr!=0)(pColExpr!=0) && pColExpr->op==TK_DOT141 ){
137724 pColExpr = pColExpr->pRight;
137725 assert( pColExpr!=0 )((void) (0));
137726 }
137727 if( pColExpr->op==TK_COLUMN167
137728 && ALWAYS( ExprUseYTab(pColExpr) )((((pColExpr)->flags&(0x1000000|0x2000000))==0))
137729 && ALWAYS( pColExpr->y.pTab!=0 )(pColExpr->y.pTab!=0)
137730 ){
137731 /* For columns use the column name name */
137732 int iCol = pColExpr->iColumn;
137733 pTab = pColExpr->y.pTab;
137734 if( iCol<0 ) iCol = pTab->iPKey;
137735 zName = iCol>=0 ? pTab->aCol[iCol].zCnName : "rowid";
137736 }else if( pColExpr->op==TK_ID59 ){
137737 assert( !ExprHasProperty(pColExpr, EP_IntValue) )((void) (0));
137738 zName = pColExpr->u.zToken;
137739 }else{
137740 /* Use the original text of the column expression as its name */
137741 assert( zName==pX->zEName )((void) (0)); /* pointer comparison intended */
137742 }
137743 }
137744 if( zName && !sqlite3IsTrueOrFalse(zName) ){
137745 zName = sqlite3DbStrDup(db, zName);
137746 }else{
137747 zName = sqlite3MPrintf(db,"column%d",i+1);
137748 }
137749
137750 /* Make sure the column name is unique. If the name is not unique,
137751 ** append an integer to the name so that it becomes unique.
137752 */
137753 cnt = 0;
137754 while( zName && (pCollide = sqlite3HashFind(&ht, zName))!=0 ){
137755 if( pCollide->fg.bUsingTerm ){
137756 pCol->colFlags |= COLFLAG_NOEXPAND0x0400;
137757 }
137758 nName = sqlite3Strlen30(zName);
137759 if( nName>0 ){
137760 for(j=nName-1; j>0 && sqlite3Isdigit(zName[j])(sqlite3CtypeMap[(unsigned char)(zName[j])]&0x04); j--){}
137761 if( zName[j]==':' ) nName = j;
137762 }
137763 zName = sqlite3MPrintf(db, "%.*z:%u", nName, zName, ++cnt);
137764 if( cnt>3 ) sqlite3_randomness(sizeof(cnt), &cnt);
137765 }
137766 pCol->zCnName = zName;
137767 pCol->hName = sqlite3StrIHash(zName);
137768 if( pX->fg.bNoExpand ){
137769 pCol->colFlags |= COLFLAG_NOEXPAND0x0400;
137770 }
137771 sqlite3ColumnPropertiesFromName(0, pCol);
137772 if( zName && sqlite3HashInsert(&ht, zName, pX)==pX ){
137773 sqlite3OomFault(db);
137774 }
137775 }
137776 sqlite3HashClear(&ht);
137777 if( db->mallocFailed ){
137778 for(j=0; j<i; j++){
137779 sqlite3DbFree(db, aCol[j].zCnName);
137780 }
137781 sqlite3DbFree(db, aCol);
137782 *paCol = 0;
137783 *pnCol = 0;
137784 return SQLITE_NOMEM_BKPT7;
137785 }
137786 return SQLITE_OK0;
137787}
137788
137789/*
137790** Add type and collation information to a column list based on
137791** a SELECT statement.
137792**
137793** The column list presumably came from selectColumnNamesFromExprList().
137794** The column list has only names, not types or collations. This
137795** routine goes through and adds the types and collations.
137796**
137797** This routine requires that all identifiers in the SELECT
137798** statement be resolved.
137799*/
137800SQLITE_PRIVATEstatic void sqlite3SelectAddColumnTypeAndCollation(
137801 Parse *pParse, /* Parsing contexts */
137802 Table *pTab, /* Add column type information to this table */
137803 Select *pSelect, /* SELECT used to determine types and collations */
137804 char aff /* Default affinity for columns */
137805){
137806 sqlite3 *db = pParse->db;
137807 NameContext sNC;
137808 Column *pCol;
137809 CollSeq *pColl;
137810 int i;
137811 Expr *p;
137812 struct ExprList_item *a;
137813
137814 assert( pSelect!=0 )((void) (0));
137815 assert( (pSelect->selFlags & SF_Resolved)!=0 )((void) (0));
137816 assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed )((void) (0));
137817 if( db->mallocFailed ) return;
137818 memset(&sNC, 0, sizeof(sNC));
137819 sNC.pSrcList = pSelect->pSrc;
137820 a = pSelect->pEList->a;
137821 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
137822 const char *zType;
137823 i64 n, m;
137824 pTab->tabFlags |= (pCol->colFlags & COLFLAG_NOINSERT0x0062);
137825 p = a[i].pExpr;
137826 zType = columnType(&sNC, p, 0, 0, 0)columnTypeImpl(&sNC,p,0,0,0);
137827 /* pCol->szEst = ... // Column size est for SELECT tables never used */
137828 pCol->affinity = sqlite3ExprAffinity(p);
137829 if( zType ){
137830 m = sqlite3Strlen30(zType);
137831 n = sqlite3Strlen30(pCol->zCnName);
137832 pCol->zCnName = sqlite3DbReallocOrFree(db, pCol->zCnName, n+m+2);
137833 if( pCol->zCnName ){
137834 memcpy(&pCol->zCnName[n+1], zType, m+1);
137835 pCol->colFlags |= COLFLAG_HASTYPE0x0004;
137836 }else{
137837 testcase( pCol->colFlags & COLFLAG_HASTYPE );
137838 pCol->colFlags &= ~(COLFLAG_HASTYPE0x0004|COLFLAG_HASCOLL0x0200);
137839 }
137840 }
137841 if( pCol->affinity<=SQLITE_AFF_NONE0x40 ) pCol->affinity = aff;
137842 pColl = sqlite3ExprCollSeq(pParse, p);
137843 if( pColl ){
137844 assert( pTab->pIndex==0 )((void) (0));
137845 sqlite3ColumnSetColl(db, pCol, pColl->zName);
137846 }
137847 }
137848 pTab->szTabRow = 1; /* Any non-zero value works */
137849}
137850
137851/*
137852** Given a SELECT statement, generate a Table structure that describes
137853** the result set of that SELECT.
137854*/
137855SQLITE_PRIVATEstatic Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect, char aff){
137856 Table *pTab;
137857 sqlite3 *db = pParse->db;
137858 u64 savedFlags;
137859
137860 savedFlags = db->flags;
137861 db->flags &= ~(u64)SQLITE_FullColNames0x00000004;
137862 db->flags |= SQLITE_ShortColNames0x00000040;
137863 sqlite3SelectPrep(pParse, pSelect, 0);
137864 db->flags = savedFlags;
137865 if( pParse->nErr ) return 0;
137866 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
137867 pTab = sqlite3DbMallocZero(db, sizeof(Table) );
137868 if( pTab==0 ){
137869 return 0;
137870 }
137871 pTab->nTabRef = 1;
137872 pTab->zName = 0;
137873 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) )((void) (0));
137874 sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
137875 sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSelect, aff);
137876 pTab->iPKey = -1;
137877 if( db->mallocFailed ){
137878 sqlite3DeleteTable(db, pTab);
137879 return 0;
137880 }
137881 return pTab;
137882}
137883
137884/*
137885** Get a VDBE for the given parser context. Create a new one if necessary.
137886** If an error occurs, return NULL and leave a message in pParse.
137887*/
137888SQLITE_PRIVATEstatic Vdbe *sqlite3GetVdbe(Parse *pParse){
137889 if( pParse->pVdbe ){
137890 return pParse->pVdbe;
137891 }
137892 if( pParse->pToplevel==0
137893 && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)(((pParse->db)->dbOptFlags&(0x00000008))==0)
137894 ){
137895 pParse->okConstFactor = 1;
137896 }
137897 return sqlite3VdbeCreate(pParse);
137898}
137899
137900
137901/*
137902** Compute the iLimit and iOffset fields of the SELECT based on the
137903** pLimit expressions. pLimit->pLeft and pLimit->pRight hold the expressions
137904** that appear in the original SQL statement after the LIMIT and OFFSET
137905** keywords. Or NULL if those keywords are omitted. iLimit and iOffset
137906** are the integer memory register numbers for counters used to compute
137907** the limit and offset. If there is no limit and/or offset, then
137908** iLimit and iOffset are negative.
137909**
137910** This routine changes the values of iLimit and iOffset only if
137911** a limit or offset is defined by pLimit->pLeft and pLimit->pRight. iLimit
137912** and iOffset should have been preset to appropriate default values (zero)
137913** prior to calling this routine.
137914**
137915** The iOffset register (if it exists) is initialized to the value
137916** of the OFFSET. The iLimit register is initialized to LIMIT. Register
137917** iOffset+1 is initialized to LIMIT+OFFSET.
137918**
137919** Only if pLimit->pLeft!=0 do the limit registers get
137920** redefined. The UNION ALL operator uses this property to force
137921** the reuse of the same limit and offset registers across multiple
137922** SELECT statements.
137923*/
137924static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
137925 Vdbe *v = 0;
137926 int iLimit = 0;
137927 int iOffset;
137928 int n;
137929 Expr *pLimit = p->pLimit;
137930
137931 if( p->iLimit ) return;
137932
137933 /*
137934 ** "LIMIT -1" always shows all rows. There is some
137935 ** controversy about what the correct behavior should be.
137936 ** The current implementation interprets "LIMIT 0" to mean
137937 ** no rows.
137938 */
137939 if( pLimit ){
137940 assert( pLimit->op==TK_LIMIT )((void) (0));
137941 assert( pLimit->pLeft!=0 )((void) (0));
137942 p->iLimit = iLimit = ++pParse->nMem;
137943 v = sqlite3GetVdbe(pParse);
137944 assert( v!=0 )((void) (0));
137945 if( sqlite3ExprIsInteger(pLimit->pLeft, &n) ){
137946 sqlite3VdbeAddOp2(v, OP_Integer71, n, iLimit);
137947 VdbeComment((v, "LIMIT counter"));
137948 if( n==0 ){
137949 sqlite3VdbeGoto(v, iBreak);
137950 }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){
137951 p->nSelectRow = sqlite3LogEst((u64)n);
137952 p->selFlags |= SF_FixedLimit0x0004000;
137953 }
137954 }else{
137955 sqlite3ExprCode(pParse, pLimit->pLeft, iLimit);
137956 sqlite3VdbeAddOp1(v, OP_MustBeInt12, iLimit); VdbeCoverage(v);
137957 VdbeComment((v, "LIMIT counter"));
137958 sqlite3VdbeAddOp2(v, OP_IfNot16, iLimit, iBreak); VdbeCoverage(v);
137959 }
137960 if( pLimit->pRight ){
137961 p->iOffset = iOffset = ++pParse->nMem;
137962 pParse->nMem++; /* Allocate an extra register for limit+offset */
137963 sqlite3ExprCode(pParse, pLimit->pRight, iOffset);
137964 sqlite3VdbeAddOp1(v, OP_MustBeInt12, iOffset); VdbeCoverage(v);
137965 VdbeComment((v, "OFFSET counter"));
137966 sqlite3VdbeAddOp3(v, OP_OffsetLimit160, iLimit, iOffset+1, iOffset);
137967 VdbeComment((v, "LIMIT+OFFSET"));
137968 }
137969 }
137970}
137971
137972#ifndef SQLITE_OMIT_COMPOUND_SELECT
137973/*
137974** Return the appropriate collating sequence for the iCol-th column of
137975** the result set for the compound-select statement "p". Return NULL if
137976** the column has no default collating sequence.
137977**
137978** The collating sequence for the compound select is taken from the
137979** left-most term of the select that has a collating sequence.
137980*/
137981static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
137982 CollSeq *pRet;
137983 if( p->pPrior ){
137984 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
137985 }else{
137986 pRet = 0;
137987 }
137988 assert( iCol>=0 )((void) (0));
137989 /* iCol must be less than p->pEList->nExpr. Otherwise an error would
137990 ** have been thrown during name resolution and we would not have gotten
137991 ** this far */
137992 if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr)(iCol<p->pEList->nExpr) ){
137993 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
137994 }
137995 return pRet;
137996}
137997
137998/*
137999** The select statement passed as the second parameter is a compound SELECT
138000** with an ORDER BY clause. This function allocates and returns a KeyInfo
138001** structure suitable for implementing the ORDER BY.
138002**
138003** Space to hold the KeyInfo structure is obtained from malloc. The calling
138004** function is responsible for ensuring that this structure is eventually
138005** freed.
138006*/
138007static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){
138008 ExprList *pOrderBy = p->pOrderBy;
138009 int nOrderBy = ALWAYS(pOrderBy!=0)(pOrderBy!=0) ? pOrderBy->nExpr : 0;
138010 sqlite3 *db = pParse->db;
138011 KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1);
138012 if( pRet ){
138013 int i;
138014 for(i=0; i<nOrderBy; i++){
138015 struct ExprList_item *pItem = &pOrderBy->a[i];
138016 Expr *pTerm = pItem->pExpr;
138017 CollSeq *pColl;
138018
138019 if( pTerm->flags & EP_Collate0x000200 ){
138020 pColl = sqlite3ExprCollSeq(pParse, pTerm);
138021 }else{
138022 pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
138023 if( pColl==0 ) pColl = db->pDfltColl;
138024 pOrderBy->a[i].pExpr =
138025 sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
138026 }
138027 assert( sqlite3KeyInfoIsWriteable(pRet) )((void) (0));
138028 pRet->aColl[i] = pColl;
138029 pRet->aSortFlags[i] = pOrderBy->a[i].fg.sortFlags;
138030 }
138031 }
138032
138033 return pRet;
138034}
138035
138036#ifndef SQLITE_OMIT_CTE
138037/*
138038** This routine generates VDBE code to compute the content of a WITH RECURSIVE
138039** query of the form:
138040**
138041** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
138042** \___________/ \_______________/
138043** p->pPrior p
138044**
138045**
138046** There is exactly one reference to the recursive-table in the FROM clause
138047** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
138048**
138049** The setup-query runs once to generate an initial set of rows that go
138050** into a Queue table. Rows are extracted from the Queue table one by
138051** one. Each row extracted from Queue is output to pDest. Then the single
138052** extracted row (now in the iCurrent table) becomes the content of the
138053** recursive-table for a recursive-query run. The output of the recursive-query
138054** is added back into the Queue table. Then another row is extracted from Queue
138055** and the iteration continues until the Queue table is empty.
138056**
138057** If the compound query operator is UNION then no duplicate rows are ever
138058** inserted into the Queue table. The iDistinct table keeps a copy of all rows
138059** that have ever been inserted into Queue and causes duplicates to be
138060** discarded. If the operator is UNION ALL, then duplicates are allowed.
138061**
138062** If the query has an ORDER BY, then entries in the Queue table are kept in
138063** ORDER BY order and the first entry is extracted for each cycle. Without
138064** an ORDER BY, the Queue table is just a FIFO.
138065**
138066** If a LIMIT clause is provided, then the iteration stops after LIMIT rows
138067** have been output to pDest. A LIMIT of zero means to output no rows and a
138068** negative LIMIT means to output all rows. If there is also an OFFSET clause
138069** with a positive value, then the first OFFSET outputs are discarded rather
138070** than being sent to pDest. The LIMIT count does not begin until after OFFSET
138071** rows have been skipped.
138072*/
138073static void generateWithRecursiveQuery(
138074 Parse *pParse, /* Parsing context */
138075 Select *p, /* The recursive SELECT to be coded */
138076 SelectDest *pDest /* What to do with query results */
138077){
138078 SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */
138079 int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */
138080 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
138081 Select *pSetup; /* The setup query */
138082 Select *pFirstRec; /* Left-most recursive term */
138083 int addrTop; /* Top of the loop */
138084 int addrCont, addrBreak; /* CONTINUE and BREAK addresses */
138085 int iCurrent = 0; /* The Current table */
138086 int regCurrent; /* Register holding Current table */
138087 int iQueue; /* The Queue table */
138088 int iDistinct = 0; /* To ensure unique results if UNION */
138089 int eDest = SRT_Fifo8; /* How to write to Queue */
138090 SelectDest destQueue; /* SelectDest targetting the Queue table */
138091 int i; /* Loop counter */
138092 int rc; /* Result code */
138093 ExprList *pOrderBy; /* The ORDER BY clause */
138094 Expr *pLimit; /* Saved LIMIT and OFFSET */
138095 int regLimit, regOffset; /* Registers used by LIMIT and OFFSET */
138096
138097#ifndef SQLITE_OMIT_WINDOWFUNC
138098 if( p->pWin ){
138099 sqlite3ErrorMsg(pParse, "cannot use window functions in recursive queries");
138100 return;
138101 }
138102#endif
138103
138104 /* Obtain authorization to do a recursive query */
138105 if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE33, 0, 0, 0) ) return;
138106
138107 /* Process the LIMIT and OFFSET clauses, if they exist */
138108 addrBreak = sqlite3VdbeMakeLabel(pParse);
138109 p->nSelectRow = 320; /* 4 billion rows */
138110 computeLimitRegisters(pParse, p, addrBreak);
138111 pLimit = p->pLimit;
138112 regLimit = p->iLimit;
138113 regOffset = p->iOffset;
138114 p->pLimit = 0;
138115 p->iLimit = p->iOffset = 0;
138116 pOrderBy = p->pOrderBy;
138117
138118 /* Locate the cursor number of the Current table */
138119 for(i=0; ALWAYS(i<pSrc->nSrc)(i<pSrc->nSrc); i++){
138120 if( pSrc->a[i].fg.isRecursive ){
138121 iCurrent = pSrc->a[i].iCursor;
138122 break;
138123 }
138124 }
138125
138126 /* Allocate cursors numbers for Queue and Distinct. The cursor number for
138127 ** the Distinct table must be exactly one greater than Queue in order
138128 ** for the SRT_DistFifo and SRT_DistQueue destinations to work. */
138129 iQueue = pParse->nTab++;
138130 if( p->op==TK_UNION134 ){
138131 eDest = pOrderBy ? SRT_DistQueue6 : SRT_DistFifo5;
138132 iDistinct = pParse->nTab++;
138133 }else{
138134 eDest = pOrderBy ? SRT_Queue7 : SRT_Fifo8;
138135 }
138136 sqlite3SelectDestInit(&destQueue, eDest, iQueue);
138137
138138 /* Allocate cursors for Current, Queue, and Distinct. */
138139 regCurrent = ++pParse->nMem;
138140 sqlite3VdbeAddOp3(v, OP_OpenPseudo121, iCurrent, regCurrent, nCol);
138141 if( pOrderBy ){
138142 KeyInfo *pKeyInfo = multiSelectOrderByKeyInfo(pParse, p, 1);
138143 sqlite3VdbeAddOp4(v, OP_OpenEphemeral118, iQueue, pOrderBy->nExpr+2, 0,
138144 (char*)pKeyInfo, P4_KEYINFO(-8));
138145 destQueue.pOrderBy = pOrderBy;
138146 }else{
138147 sqlite3VdbeAddOp2(v, OP_OpenEphemeral118, iQueue, nCol);
138148 }
138149 VdbeComment((v, "Queue table"));
138150 if( iDistinct ){
138151 p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral118, iDistinct, 0);
138152 p->selFlags |= SF_UsesEphemeral0x0000020;
138153 }
138154
138155 /* Detach the ORDER BY clause from the compound SELECT */
138156 p->pOrderBy = 0;
138157
138158 /* Figure out how many elements of the compound SELECT are part of the
138159 ** recursive query. Make sure no recursive elements use aggregate
138160 ** functions. Mark the recursive elements as UNION ALL even if they
138161 ** are really UNION because the distinctness will be enforced by the
138162 ** iDistinct table. pFirstRec is left pointing to the left-most
138163 ** recursive term of the CTE.
138164 */
138165 for(pFirstRec=p; ALWAYS(pFirstRec!=0)(pFirstRec!=0); pFirstRec=pFirstRec->pPrior){
138166 if( pFirstRec->selFlags & SF_Aggregate0x0000008 ){
138167 sqlite3ErrorMsg(pParse, "recursive aggregate queries not supported");
138168 goto end_of_recursive_query;
138169 }
138170 pFirstRec->op = TK_ALL135;
138171 if( (pFirstRec->pPrior->selFlags & SF_Recursive0x0002000)==0 ) break;
138172 }
138173
138174 /* Store the results of the setup-query in Queue. */
138175 pSetup = pFirstRec->pPrior;
138176 pSetup->pNext = 0;
138177 ExplainQueryPlan((pParse, 1, "SETUP"))sqlite3VdbeExplain (pParse, 1, "SETUP");
138178 rc = sqlite3Select(pParse, pSetup, &destQueue);
138179 pSetup->pNext = p;
138180 if( rc ) goto end_of_recursive_query;
138181
138182 /* Find the next row in the Queue and output that row */
138183 addrTop = sqlite3VdbeAddOp2(v, OP_Rewind35, iQueue, addrBreak); VdbeCoverage(v);
138184
138185 /* Transfer the next row in Queue over to Current */
138186 sqlite3VdbeAddOp1(v, OP_NullRow136, iCurrent); /* To reset column cache */
138187 if( pOrderBy ){
138188 sqlite3VdbeAddOp3(v, OP_Column94, iQueue, pOrderBy->nExpr+1, regCurrent);
138189 }else{
138190 sqlite3VdbeAddOp2(v, OP_RowData134, iQueue, regCurrent);
138191 }
138192 sqlite3VdbeAddOp1(v, OP_Delete130, iQueue);
138193
138194 /* Output the single row in Current */
138195 addrCont = sqlite3VdbeMakeLabel(pParse);
138196 codeOffset(v, regOffset, addrCont);
138197 selectInnerLoop(pParse, p, iCurrent,
138198 0, 0, pDest, addrCont, addrBreak);
138199 if( regLimit ){
138200 sqlite3VdbeAddOp2(v, OP_DecrJumpZero60, regLimit, addrBreak);
138201 VdbeCoverage(v);
138202 }
138203 sqlite3VdbeResolveLabel(v, addrCont);
138204
138205 /* Execute the recursive SELECT taking the single row in Current as
138206 ** the value for the recursive-table. Store the results in the Queue.
138207 */
138208 pFirstRec->pPrior = 0;
138209 ExplainQueryPlan((pParse, 1, "RECURSIVE STEP"))sqlite3VdbeExplain (pParse, 1, "RECURSIVE STEP");
138210 sqlite3Select(pParse, p, &destQueue);
138211 assert( pFirstRec->pPrior==0 )((void) (0));
138212 pFirstRec->pPrior = pSetup;
138213
138214 /* Keep running the loop until the Queue is empty */
138215 sqlite3VdbeGoto(v, addrTop);
138216 sqlite3VdbeResolveLabel(v, addrBreak);
138217
138218end_of_recursive_query:
138219 sqlite3ExprListDelete(pParse->db, p->pOrderBy);
138220 p->pOrderBy = pOrderBy;
138221 p->pLimit = pLimit;
138222 return;
138223}
138224#endif /* SQLITE_OMIT_CTE */
138225
138226/* Forward references */
138227static int multiSelectOrderBy(
138228 Parse *pParse, /* Parsing context */
138229 Select *p, /* The right-most of SELECTs to be coded */
138230 SelectDest *pDest /* What to do with query results */
138231);
138232
138233/*
138234** Handle the special case of a compound-select that originates from a
138235** VALUES clause. By handling this as a special case, we avoid deep
138236** recursion, and thus do not need to enforce the SQLITE_LIMIT_COMPOUND_SELECT
138237** on a VALUES clause.
138238**
138239** Because the Select object originates from a VALUES clause:
138240** (1) There is no LIMIT or OFFSET or else there is a LIMIT of exactly 1
138241** (2) All terms are UNION ALL
138242** (3) There is no ORDER BY clause
138243**
138244** The "LIMIT of exactly 1" case of condition (1) comes about when a VALUES
138245** clause occurs within scalar expression (ex: "SELECT (VALUES(1),(2),(3))").
138246** The sqlite3CodeSubselect will have added the LIMIT 1 clause in tht case.
138247** Since the limit is exactly 1, we only need to evaluate the left-most VALUES.
138248*/
138249static int multiSelectValues(
138250 Parse *pParse, /* Parsing context */
138251 Select *p, /* The right-most of SELECTs to be coded */
138252 SelectDest *pDest /* What to do with query results */
138253){
138254 int nRow = 1;
138255 int rc = 0;
138256 int bShowAll = p->pLimit==0;
138257 assert( p->selFlags & SF_MultiValue )((void) (0));
138258 do{
138259 assert( p->selFlags & SF_Values )((void) (0));
138260 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) )((void) (0));
138261 assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr )((void) (0));
138262#ifndef SQLITE_OMIT_WINDOWFUNC
138263 if( p->pWin ) return -1;
138264#endif
138265 if( p->pPrior==0 ) break;
138266 assert( p->pPrior->pNext==p )((void) (0));
138267 p = p->pPrior;
138268 nRow += bShowAll;
138269 }while(1);
138270 ExplainQueryPlan((pParse, 0, "SCAN %d CONSTANT ROW%s", nRow,sqlite3VdbeExplain (pParse, 0, "SCAN %d CONSTANT ROW%s", nRow
, nRow==1 ? "" : "S")
138271 nRow==1 ? "" : "S"))sqlite3VdbeExplain (pParse, 0, "SCAN %d CONSTANT ROW%s", nRow
, nRow==1 ? "" : "S")
;
138272 while( p ){
138273 selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1);
138274 if( !bShowAll ) break;
138275 p->nSelectRow = nRow;
138276 p = p->pNext;
138277 }
138278 return rc;
138279}
138280
138281/*
138282** Return true if the SELECT statement which is known to be the recursive
138283** part of a recursive CTE still has its anchor terms attached. If the
138284** anchor terms have already been removed, then return false.
138285*/
138286static int hasAnchor(Select *p){
138287 while( p && (p->selFlags & SF_Recursive0x0002000)!=0 ){ p = p->pPrior; }
138288 return p!=0;
138289}
138290
138291/*
138292** This routine is called to process a compound query form from
138293** two or more separate queries using UNION, UNION ALL, EXCEPT, or
138294** INTERSECT
138295**
138296** "p" points to the right-most of the two queries. the query on the
138297** left is p->pPrior. The left query could also be a compound query
138298** in which case this routine will be called recursively.
138299**
138300** The results of the total query are to be written into a destination
138301** of type eDest with parameter iParm.
138302**
138303** Example 1: Consider a three-way compound SQL statement.
138304**
138305** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
138306**
138307** This statement is parsed up as follows:
138308**
138309** SELECT c FROM t3
138310** |
138311** `-----> SELECT b FROM t2
138312** |
138313** `------> SELECT a FROM t1
138314**
138315** The arrows in the diagram above represent the Select.pPrior pointer.
138316** So if this routine is called with p equal to the t3 query, then
138317** pPrior will be the t2 query. p->op will be TK_UNION in this case.
138318**
138319** Notice that because of the way SQLite parses compound SELECTs, the
138320** individual selects always group from left to right.
138321*/
138322static int multiSelect(
138323 Parse *pParse, /* Parsing context */
138324 Select *p, /* The right-most of SELECTs to be coded */
138325 SelectDest *pDest /* What to do with query results */
138326){
138327 int rc = SQLITE_OK0; /* Success code from a subroutine */
138328 Select *pPrior; /* Another SELECT immediately to our left */
138329 Vdbe *v; /* Generate code to this VDBE */
138330 SelectDest dest; /* Alternative data destination */
138331 Select *pDelete = 0; /* Chain of simple selects to delete */
138332 sqlite3 *db; /* Database connection */
138333
138334 /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only
138335 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
138336 */
138337 assert( p && p->pPrior )((void) (0)); /* Calling function guarantees this much */
138338 assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION )((void) (0));
138339 assert( p->selFlags & SF_Compound )((void) (0));
138340 db = pParse->db;
138341 pPrior = p->pPrior;
138342 dest = *pDest;
138343 assert( pPrior->pOrderBy==0 )((void) (0));
138344 assert( pPrior->pLimit==0 )((void) (0));
138345
138346 v = sqlite3GetVdbe(pParse);
138347 assert( v!=0 )((void) (0)); /* The VDBE already created by calling function */
138348
138349 /* Create the destination temporary table if necessary
138350 */
138351 if( dest.eDest==SRT_EphemTab12 ){
138352 assert( p->pEList )((void) (0));
138353 sqlite3VdbeAddOp2(v, OP_OpenEphemeral118, dest.iSDParm, p->pEList->nExpr);
138354 dest.eDest = SRT_Table14;
138355 }
138356
138357 /* Special handling for a compound-select that originates as a VALUES clause.
138358 */
138359 if( p->selFlags & SF_MultiValue0x0000400 ){
138360 rc = multiSelectValues(pParse, p, &dest);
138361 if( rc>=0 ) goto multi_select_end;
138362 rc = SQLITE_OK0;
138363 }
138364
138365 /* Make sure all SELECTs in the statement have the same number of elements
138366 ** in their result sets.
138367 */
138368 assert( p->pEList && pPrior->pEList )((void) (0));
138369 assert( p->pEList->nExpr==pPrior->pEList->nExpr )((void) (0));
138370
138371#ifndef SQLITE_OMIT_CTE
138372 if( (p->selFlags & SF_Recursive0x0002000)!=0 && hasAnchor(p) ){
138373 generateWithRecursiveQuery(pParse, p, &dest);
138374 }else
138375#endif
138376
138377 /* Compound SELECTs that have an ORDER BY clause are handled separately.
138378 */
138379 if( p->pOrderBy ){
138380 return multiSelectOrderBy(pParse, p, pDest);
138381 }else{
138382
138383#ifndef SQLITE_OMIT_EXPLAIN
138384 if( pPrior->pPrior==0 ){
138385 ExplainQueryPlan((pParse, 1, "COMPOUND QUERY"))sqlite3VdbeExplain (pParse, 1, "COMPOUND QUERY");
138386 ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"))sqlite3VdbeExplain (pParse, 1, "LEFT-MOST SUBQUERY");
138387 }
138388#endif
138389
138390 /* Generate code for the left and right SELECT statements.
138391 */
138392 switch( p->op ){
138393 case TK_ALL135: {
138394 int addr = 0;
138395 int nLimit = 0; /* Initialize to suppress harmless compiler warning */
138396 assert( !pPrior->pLimit )((void) (0));
138397 pPrior->iLimit = p->iLimit;
138398 pPrior->iOffset = p->iOffset;
138399 pPrior->pLimit = p->pLimit;
138400 SELECTTRACE(1, pParse, p, ("multiSelect UNION ALL left...\n"));
138401 rc = sqlite3Select(pParse, pPrior, &dest);
138402 pPrior->pLimit = 0;
138403 if( rc ){
138404 goto multi_select_end;
138405 }
138406 p->pPrior = 0;
138407 p->iLimit = pPrior->iLimit;
138408 p->iOffset = pPrior->iOffset;
138409 if( p->iLimit ){
138410 addr = sqlite3VdbeAddOp1(v, OP_IfNot16, p->iLimit); VdbeCoverage(v);
138411 VdbeComment((v, "Jump ahead if LIMIT reached"));
138412 if( p->iOffset ){
138413 sqlite3VdbeAddOp3(v, OP_OffsetLimit160,
138414 p->iLimit, p->iOffset+1, p->iOffset);
138415 }
138416 }
138417 ExplainQueryPlan((pParse, 1, "UNION ALL"))sqlite3VdbeExplain (pParse, 1, "UNION ALL");
138418 SELECTTRACE(1, pParse, p, ("multiSelect UNION ALL right...\n"));
138419 rc = sqlite3Select(pParse, p, &dest);
138420 testcase( rc!=SQLITE_OK );
138421 pDelete = p->pPrior;
138422 p->pPrior = pPrior;
138423 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
138424 if( p->pLimit
138425 && sqlite3ExprIsInteger(p->pLimit->pLeft, &nLimit)
138426 && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
138427 ){
138428 p->nSelectRow = sqlite3LogEst((u64)nLimit);
138429 }
138430 if( addr ){
138431 sqlite3VdbeJumpHere(v, addr);
138432 }
138433 break;
138434 }
138435 case TK_EXCEPT136:
138436 case TK_UNION134: {
138437 int unionTab; /* Cursor number of the temp table holding result */
138438 u8 op = 0; /* One of the SRT_ operations to apply to self */
138439 int priorOp; /* The SRT_ operation to apply to prior selects */
138440 Expr *pLimit; /* Saved values of p->nLimit */
138441 int addr;
138442 SelectDest uniondest;
138443
138444 testcase( p->op==TK_EXCEPT );
138445 testcase( p->op==TK_UNION );
138446 priorOp = SRT_Union1;
138447 if( dest.eDest==priorOp ){
138448 /* We can reuse a temporary table generated by a SELECT to our
138449 ** right.
138450 */
138451 assert( p->pLimit==0 )((void) (0)); /* Not allowed on leftward elements */
138452 unionTab = dest.iSDParm;
138453 }else{
138454 /* We will need to create our own temporary table to hold the
138455 ** intermediate results.
138456 */
138457 unionTab = pParse->nTab++;
138458 assert( p->pOrderBy==0 )((void) (0));
138459 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral118, unionTab, 0);
138460 assert( p->addrOpenEphm[0] == -1 )((void) (0));
138461 p->addrOpenEphm[0] = addr;
138462 findRightmost(p)->selFlags |= SF_UsesEphemeral0x0000020;
138463 assert( p->pEList )((void) (0));
138464 }
138465
138466
138467 /* Code the SELECT statements to our left
138468 */
138469 assert( !pPrior->pOrderBy )((void) (0));
138470 sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
138471 SELECTTRACE(1, pParse, p, ("multiSelect EXCEPT/UNION left...\n"));
138472 rc = sqlite3Select(pParse, pPrior, &uniondest);
138473 if( rc ){
138474 goto multi_select_end;
138475 }
138476
138477 /* Code the current SELECT statement
138478 */
138479 if( p->op==TK_EXCEPT136 ){
138480 op = SRT_Except2;
138481 }else{
138482 assert( p->op==TK_UNION )((void) (0));
138483 op = SRT_Union1;
138484 }
138485 p->pPrior = 0;
138486 pLimit = p->pLimit;
138487 p->pLimit = 0;
138488 uniondest.eDest = op;
138489 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",sqlite3VdbeExplain (pParse, 1, "%s USING TEMP B-TREE", sqlite3SelectOpName
(p->op))
138490 sqlite3SelectOpName(p->op)))sqlite3VdbeExplain (pParse, 1, "%s USING TEMP B-TREE", sqlite3SelectOpName
(p->op))
;
138491 SELECTTRACE(1, pParse, p, ("multiSelect EXCEPT/UNION right...\n"));
138492 rc = sqlite3Select(pParse, p, &uniondest);
138493 testcase( rc!=SQLITE_OK );
138494 assert( p->pOrderBy==0 )((void) (0));
138495 pDelete = p->pPrior;
138496 p->pPrior = pPrior;
138497 p->pOrderBy = 0;
138498 if( p->op==TK_UNION134 ){
138499 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
138500 }
138501 sqlite3ExprDelete(db, p->pLimit);
138502 p->pLimit = pLimit;
138503 p->iLimit = 0;
138504 p->iOffset = 0;
138505
138506 /* Convert the data in the temporary table into whatever form
138507 ** it is that we currently need.
138508 */
138509 assert( unionTab==dest.iSDParm || dest.eDest!=priorOp )((void) (0));
138510 assert( p->pEList || db->mallocFailed )((void) (0));
138511 if( dest.eDest!=priorOp && db->mallocFailed==0 ){
138512 int iCont, iBreak, iStart;
138513 iBreak = sqlite3VdbeMakeLabel(pParse);
138514 iCont = sqlite3VdbeMakeLabel(pParse);
138515 computeLimitRegisters(pParse, p, iBreak);
138516 sqlite3VdbeAddOp2(v, OP_Rewind35, unionTab, iBreak); VdbeCoverage(v);
138517 iStart = sqlite3VdbeCurrentAddr(v);
138518 selectInnerLoop(pParse, p, unionTab,
138519 0, 0, &dest, iCont, iBreak);
138520 sqlite3VdbeResolveLabel(v, iCont);
138521 sqlite3VdbeAddOp2(v, OP_Next38, unionTab, iStart); VdbeCoverage(v);
138522 sqlite3VdbeResolveLabel(v, iBreak);
138523 sqlite3VdbeAddOp2(v, OP_Close122, unionTab, 0);
138524 }
138525 break;
138526 }
138527 default: assert( p->op==TK_INTERSECT )((void) (0)); {
138528 int tab1, tab2;
138529 int iCont, iBreak, iStart;
138530 Expr *pLimit;
138531 int addr;
138532 SelectDest intersectdest;
138533 int r1;
138534
138535 /* INTERSECT is different from the others since it requires
138536 ** two temporary tables. Hence it has its own case. Begin
138537 ** by allocating the tables we will need.
138538 */
138539 tab1 = pParse->nTab++;
138540 tab2 = pParse->nTab++;
138541 assert( p->pOrderBy==0 )((void) (0));
138542
138543 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral118, tab1, 0);
138544 assert( p->addrOpenEphm[0] == -1 )((void) (0));
138545 p->addrOpenEphm[0] = addr;
138546 findRightmost(p)->selFlags |= SF_UsesEphemeral0x0000020;
138547 assert( p->pEList )((void) (0));
138548
138549 /* Code the SELECTs to our left into temporary table "tab1".
138550 */
138551 sqlite3SelectDestInit(&intersectdest, SRT_Union1, tab1);
138552 SELECTTRACE(1, pParse, p, ("multiSelect INTERSECT left...\n"));
138553 rc = sqlite3Select(pParse, pPrior, &intersectdest);
138554 if( rc ){
138555 goto multi_select_end;
138556 }
138557
138558 /* Code the current SELECT into temporary table "tab2"
138559 */
138560 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral118, tab2, 0);
138561 assert( p->addrOpenEphm[1] == -1 )((void) (0));
138562 p->addrOpenEphm[1] = addr;
138563 p->pPrior = 0;
138564 pLimit = p->pLimit;
138565 p->pLimit = 0;
138566 intersectdest.iSDParm = tab2;
138567 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",sqlite3VdbeExplain (pParse, 1, "%s USING TEMP B-TREE", sqlite3SelectOpName
(p->op))
138568 sqlite3SelectOpName(p->op)))sqlite3VdbeExplain (pParse, 1, "%s USING TEMP B-TREE", sqlite3SelectOpName
(p->op))
;
138569 SELECTTRACE(1, pParse, p, ("multiSelect INTERSECT right...\n"));
138570 rc = sqlite3Select(pParse, p, &intersectdest);
138571 testcase( rc!=SQLITE_OK );
138572 pDelete = p->pPrior;
138573 p->pPrior = pPrior;
138574 if( p->nSelectRow>pPrior->nSelectRow ){
138575 p->nSelectRow = pPrior->nSelectRow;
138576 }
138577 sqlite3ExprDelete(db, p->pLimit);
138578 p->pLimit = pLimit;
138579
138580 /* Generate code to take the intersection of the two temporary
138581 ** tables.
138582 */
138583 if( rc ) break;
138584 assert( p->pEList )((void) (0));
138585 iBreak = sqlite3VdbeMakeLabel(pParse);
138586 iCont = sqlite3VdbeMakeLabel(pParse);
138587 computeLimitRegisters(pParse, p, iBreak);
138588 sqlite3VdbeAddOp2(v, OP_Rewind35, tab1, iBreak); VdbeCoverage(v);
138589 r1 = sqlite3GetTempReg(pParse);
138590 iStart = sqlite3VdbeAddOp2(v, OP_RowData134, tab1, r1);
138591 sqlite3VdbeAddOp4Int(v, OP_NotFound27, tab2, iCont, r1, 0);
138592 VdbeCoverage(v);
138593 sqlite3ReleaseTempReg(pParse, r1);
138594 selectInnerLoop(pParse, p, tab1,
138595 0, 0, &dest, iCont, iBreak);
138596 sqlite3VdbeResolveLabel(v, iCont);
138597 sqlite3VdbeAddOp2(v, OP_Next38, tab1, iStart); VdbeCoverage(v);
138598 sqlite3VdbeResolveLabel(v, iBreak);
138599 sqlite3VdbeAddOp2(v, OP_Close122, tab2, 0);
138600 sqlite3VdbeAddOp2(v, OP_Close122, tab1, 0);
138601 break;
138602 }
138603 }
138604
138605 #ifndef SQLITE_OMIT_EXPLAIN
138606 if( p->pNext==0 ){
138607 ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
138608 }
138609 #endif
138610 }
138611 if( pParse->nErr ) goto multi_select_end;
138612
138613 /* Compute collating sequences used by
138614 ** temporary tables needed to implement the compound select.
138615 ** Attach the KeyInfo structure to all temporary tables.
138616 **
138617 ** This section is run by the right-most SELECT statement only.
138618 ** SELECT statements to the left always skip this part. The right-most
138619 ** SELECT might also skip this part if it has no ORDER BY clause and
138620 ** no temp tables are required.
138621 */
138622 if( p->selFlags & SF_UsesEphemeral0x0000020 ){
138623 int i; /* Loop counter */
138624 KeyInfo *pKeyInfo; /* Collating sequence for the result set */
138625 Select *pLoop; /* For looping through SELECT statements */
138626 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
138627 int nCol; /* Number of columns in result set */
138628
138629 assert( p->pNext==0 )((void) (0));
138630 assert( p->pEList!=0 )((void) (0));
138631 nCol = p->pEList->nExpr;
138632 pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1);
138633 if( !pKeyInfo ){
138634 rc = SQLITE_NOMEM_BKPT7;
138635 goto multi_select_end;
138636 }
138637 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
138638 *apColl = multiSelectCollSeq(pParse, p, i);
138639 if( 0==*apColl ){
138640 *apColl = db->pDfltColl;
138641 }
138642 }
138643
138644 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
138645 for(i=0; i<2; i++){
138646 int addr = pLoop->addrOpenEphm[i];
138647 if( addr<0 ){
138648 /* If [0] is unused then [1] is also unused. So we can
138649 ** always safely abort as soon as the first unused slot is found */
138650 assert( pLoop->addrOpenEphm[1]<0 )((void) (0));
138651 break;
138652 }
138653 sqlite3VdbeChangeP2(v, addr, nCol);
138654 sqlite3VdbeChangeP4(v, addr, (char*)sqlite3KeyInfoRef(pKeyInfo),
138655 P4_KEYINFO(-8));
138656 pLoop->addrOpenEphm[i] = -1;
138657 }
138658 }
138659 sqlite3KeyInfoUnref(pKeyInfo);
138660 }
138661
138662multi_select_end:
138663 pDest->iSdst = dest.iSdst;
138664 pDest->nSdst = dest.nSdst;
138665 if( pDelete ){
138666 sqlite3ParserAddCleanup(pParse,
138667 (void(*)(sqlite3*,void*))sqlite3SelectDelete,
138668 pDelete);
138669 }
138670 return rc;
138671}
138672#endif /* SQLITE_OMIT_COMPOUND_SELECT */
138673
138674/*
138675** Error message for when two or more terms of a compound select have different
138676** size result sets.
138677*/
138678SQLITE_PRIVATEstatic void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){
138679 if( p->selFlags & SF_Values0x0000200 ){
138680 sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
138681 }else{
138682 sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
138683 " do not have the same number of result columns",
138684 sqlite3SelectOpName(p->op));
138685 }
138686}
138687
138688/*
138689** Code an output subroutine for a coroutine implementation of a
138690** SELECT statment.
138691**
138692** The data to be output is contained in pIn->iSdst. There are
138693** pIn->nSdst columns to be output. pDest is where the output should
138694** be sent.
138695**
138696** regReturn is the number of the register holding the subroutine
138697** return address.
138698**
138699** If regPrev>0 then it is the first register in a vector that
138700** records the previous output. mem[regPrev] is a flag that is false
138701** if there has been no previous output. If regPrev>0 then code is
138702** generated to suppress duplicates. pKeyInfo is used for comparing
138703** keys.
138704**
138705** If the LIMIT found in p->iLimit is reached, jump immediately to
138706** iBreak.
138707*/
138708static int generateOutputSubroutine(
138709 Parse *pParse, /* Parsing context */
138710 Select *p, /* The SELECT statement */
138711 SelectDest *pIn, /* Coroutine supplying data */
138712 SelectDest *pDest, /* Where to send the data */
138713 int regReturn, /* The return address register */
138714 int regPrev, /* Previous result register. No uniqueness if 0 */
138715 KeyInfo *pKeyInfo, /* For comparing with previous entry */
138716 int iBreak /* Jump here if we hit the LIMIT */
138717){
138718 Vdbe *v = pParse->pVdbe;
138719 int iContinue;
138720 int addr;
138721
138722 addr = sqlite3VdbeCurrentAddr(v);
138723 iContinue = sqlite3VdbeMakeLabel(pParse);
138724
138725 /* Suppress duplicates for UNION, EXCEPT, and INTERSECT
138726 */
138727 if( regPrev ){
138728 int addr1, addr2;
138729 addr1 = sqlite3VdbeAddOp1(v, OP_IfNot16, regPrev); VdbeCoverage(v);
138730 addr2 = sqlite3VdbeAddOp4(v, OP_Compare90, pIn->iSdst, regPrev+1, pIn->nSdst,
138731 (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO(-8));
138732 sqlite3VdbeAddOp3(v, OP_Jump13, addr2+2, iContinue, addr2+2); VdbeCoverage(v);
138733 sqlite3VdbeJumpHere(v, addr1);
138734 sqlite3VdbeAddOp3(v, OP_Copy80, pIn->iSdst, regPrev+1, pIn->nSdst-1);
138735 sqlite3VdbeAddOp2(v, OP_Integer71, 1, regPrev);
138736 }
138737 if( pParse->db->mallocFailed ) return 0;
138738
138739 /* Suppress the first OFFSET entries if there is an OFFSET clause
138740 */
138741 codeOffset(v, p->iOffset, iContinue);
138742
138743 assert( pDest->eDest!=SRT_Exists )((void) (0));
138744 assert( pDest->eDest!=SRT_Table )((void) (0));
138745 switch( pDest->eDest ){
138746 /* Store the result as data using a unique key.
138747 */
138748 case SRT_EphemTab12: {
138749 int r1 = sqlite3GetTempReg(pParse);
138750 int r2 = sqlite3GetTempReg(pParse);
138751 sqlite3VdbeAddOp3(v, OP_MakeRecord97, pIn->iSdst, pIn->nSdst, r1);
138752 sqlite3VdbeAddOp2(v, OP_NewRowid127, pDest->iSDParm, r2);
138753 sqlite3VdbeAddOp3(v, OP_Insert128, pDest->iSDParm, r1, r2);
138754 sqlite3VdbeChangeP5(v, OPFLAG_APPEND0x08);
138755 sqlite3ReleaseTempReg(pParse, r2);
138756 sqlite3ReleaseTempReg(pParse, r1);
138757 break;
138758 }
138759
138760#ifndef SQLITE_OMIT_SUBQUERY
138761 /* If we are creating a set for an "expr IN (SELECT ...)".
138762 */
138763 case SRT_Set11: {
138764 int r1;
138765 testcase( pIn->nSdst>1 );
138766 r1 = sqlite3GetTempReg(pParse);
138767 sqlite3VdbeAddOp4(v, OP_MakeRecord97, pIn->iSdst, pIn->nSdst,
138768 r1, pDest->zAffSdst, pIn->nSdst);
138769 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, pDest->iSDParm, r1,
138770 pIn->iSdst, pIn->nSdst);
138771 sqlite3ReleaseTempReg(pParse, r1);
138772 break;
138773 }
138774
138775 /* If this is a scalar select that is part of an expression, then
138776 ** store the results in the appropriate memory cell and break out
138777 ** of the scan loop. Note that the select might return multiple columns
138778 ** if it is the RHS of a row-value IN operator.
138779 */
138780 case SRT_Mem10: {
138781 testcase( pIn->nSdst>1 );
138782 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, pIn->nSdst);
138783 /* The LIMIT clause will jump out of the loop for us */
138784 break;
138785 }
138786#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
138787
138788 /* The results are stored in a sequence of registers
138789 ** starting at pDest->iSdst. Then the co-routine yields.
138790 */
138791 case SRT_Coroutine13: {
138792 if( pDest->iSdst==0 ){
138793 pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
138794 pDest->nSdst = pIn->nSdst;
138795 }
138796 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
138797 sqlite3VdbeAddOp1(v, OP_Yield11, pDest->iSDParm);
138798 break;
138799 }
138800
138801 /* If none of the above, then the result destination must be
138802 ** SRT_Output. This routine is never called with any other
138803 ** destination other than the ones handled above or SRT_Output.
138804 **
138805 ** For SRT_Output, results are stored in a sequence of registers.
138806 ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
138807 ** return the next row of result.
138808 */
138809 default: {
138810 assert( pDest->eDest==SRT_Output )((void) (0));
138811 sqlite3VdbeAddOp2(v, OP_ResultRow84, pIn->iSdst, pIn->nSdst);
138812 break;
138813 }
138814 }
138815
138816 /* Jump to the end of the loop if the LIMIT is reached.
138817 */
138818 if( p->iLimit ){
138819 sqlite3VdbeAddOp2(v, OP_DecrJumpZero60, p->iLimit, iBreak); VdbeCoverage(v);
138820 }
138821
138822 /* Generate the subroutine return
138823 */
138824 sqlite3VdbeResolveLabel(v, iContinue);
138825 sqlite3VdbeAddOp1(v, OP_Return67, regReturn);
138826
138827 return addr;
138828}
138829
138830/*
138831** Alternative compound select code generator for cases when there
138832** is an ORDER BY clause.
138833**
138834** We assume a query of the following form:
138835**
138836** <selectA> <operator> <selectB> ORDER BY <orderbylist>
138837**
138838** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT. The idea
138839** is to code both <selectA> and <selectB> with the ORDER BY clause as
138840** co-routines. Then run the co-routines in parallel and merge the results
138841** into the output. In addition to the two coroutines (called selectA and
138842** selectB) there are 7 subroutines:
138843**
138844** outA: Move the output of the selectA coroutine into the output
138845** of the compound query.
138846**
138847** outB: Move the output of the selectB coroutine into the output
138848** of the compound query. (Only generated for UNION and
138849** UNION ALL. EXCEPT and INSERTSECT never output a row that
138850** appears only in B.)
138851**
138852** AltB: Called when there is data from both coroutines and A<B.
138853**
138854** AeqB: Called when there is data from both coroutines and A==B.
138855**
138856** AgtB: Called when there is data from both coroutines and A>B.
138857**
138858** EofA: Called when data is exhausted from selectA.
138859**
138860** EofB: Called when data is exhausted from selectB.
138861**
138862** The implementation of the latter five subroutines depend on which
138863** <operator> is used:
138864**
138865**
138866** UNION ALL UNION EXCEPT INTERSECT
138867** ------------- ----------------- -------------- -----------------
138868** AltB: outA, nextA outA, nextA outA, nextA nextA
138869**
138870** AeqB: outA, nextA nextA nextA outA, nextA
138871**
138872** AgtB: outB, nextB outB, nextB nextB nextB
138873**
138874** EofA: outB, nextB outB, nextB halt halt
138875**
138876** EofB: outA, nextA outA, nextA outA, nextA halt
138877**
138878** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
138879** causes an immediate jump to EofA and an EOF on B following nextB causes
138880** an immediate jump to EofB. Within EofA and EofB, and EOF on entry or
138881** following nextX causes a jump to the end of the select processing.
138882**
138883** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
138884** within the output subroutine. The regPrev register set holds the previously
138885** output value. A comparison is made against this value and the output
138886** is skipped if the next results would be the same as the previous.
138887**
138888** The implementation plan is to implement the two coroutines and seven
138889** subroutines first, then put the control logic at the bottom. Like this:
138890**
138891** goto Init
138892** coA: coroutine for left query (A)
138893** coB: coroutine for right query (B)
138894** outA: output one row of A
138895** outB: output one row of B (UNION and UNION ALL only)
138896** EofA: ...
138897** EofB: ...
138898** AltB: ...
138899** AeqB: ...
138900** AgtB: ...
138901** Init: initialize coroutine registers
138902** yield coA
138903** if eof(A) goto EofA
138904** yield coB
138905** if eof(B) goto EofB
138906** Cmpr: Compare A, B
138907** Jump AltB, AeqB, AgtB
138908** End: ...
138909**
138910** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
138911** actually called using Gosub and they do not Return. EofA and EofB loop
138912** until all data is exhausted then jump to the "end" labe. AltB, AeqB,
138913** and AgtB jump to either L2 or to one of EofA or EofB.
138914*/
138915#ifndef SQLITE_OMIT_COMPOUND_SELECT
138916static int multiSelectOrderBy(
138917 Parse *pParse, /* Parsing context */
138918 Select *p, /* The right-most of SELECTs to be coded */
138919 SelectDest *pDest /* What to do with query results */
138920){
138921 int i, j; /* Loop counters */
138922 Select *pPrior; /* Another SELECT immediately to our left */
138923 Select *pSplit; /* Left-most SELECT in the right-hand group */
138924 int nSelect; /* Number of SELECT statements in the compound */
138925 Vdbe *v; /* Generate code to this VDBE */
138926 SelectDest destA; /* Destination for coroutine A */
138927 SelectDest destB; /* Destination for coroutine B */
138928 int regAddrA; /* Address register for select-A coroutine */
138929 int regAddrB; /* Address register for select-B coroutine */
138930 int addrSelectA; /* Address of the select-A coroutine */
138931 int addrSelectB; /* Address of the select-B coroutine */
138932 int regOutA; /* Address register for the output-A subroutine */
138933 int regOutB; /* Address register for the output-B subroutine */
138934 int addrOutA; /* Address of the output-A subroutine */
138935 int addrOutB = 0; /* Address of the output-B subroutine */
138936 int addrEofA; /* Address of the select-A-exhausted subroutine */
138937 int addrEofA_noB; /* Alternate addrEofA if B is uninitialized */
138938 int addrEofB; /* Address of the select-B-exhausted subroutine */
138939 int addrAltB; /* Address of the A<B subroutine */
138940 int addrAeqB; /* Address of the A==B subroutine */
138941 int addrAgtB; /* Address of the A>B subroutine */
138942 int regLimitA; /* Limit register for select-A */
138943 int regLimitB; /* Limit register for select-A */
138944 int regPrev; /* A range of registers to hold previous output */
138945 int savedLimit; /* Saved value of p->iLimit */
138946 int savedOffset; /* Saved value of p->iOffset */
138947 int labelCmpr; /* Label for the start of the merge algorithm */
138948 int labelEnd; /* Label for the end of the overall SELECT stmt */
138949 int addr1; /* Jump instructions that get retargetted */
138950 int op; /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
138951 KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
138952 KeyInfo *pKeyMerge; /* Comparison information for merging rows */
138953 sqlite3 *db; /* Database connection */
138954 ExprList *pOrderBy; /* The ORDER BY clause */
138955 int nOrderBy; /* Number of terms in the ORDER BY clause */
138956 u32 *aPermute; /* Mapping from ORDER BY terms to result set columns */
138957
138958 assert( p->pOrderBy!=0 )((void) (0));
138959 assert( pKeyDup==0 )((void) (0)); /* "Managed" code needs this. Ticket #3382. */
138960 db = pParse->db;
138961 v = pParse->pVdbe;
138962 assert( v!=0 )((void) (0)); /* Already thrown the error if VDBE alloc failed */
138963 labelEnd = sqlite3VdbeMakeLabel(pParse);
138964 labelCmpr = sqlite3VdbeMakeLabel(pParse);
138965
138966
138967 /* Patch up the ORDER BY clause
138968 */
138969 op = p->op;
138970 assert( p->pPrior->pOrderBy==0 )((void) (0));
138971 pOrderBy = p->pOrderBy;
138972 assert( pOrderBy )((void) (0));
138973 nOrderBy = pOrderBy->nExpr;
138974
138975 /* For operators other than UNION ALL we have to make sure that
138976 ** the ORDER BY clause covers every term of the result set. Add
138977 ** terms to the ORDER BY clause as necessary.
138978 */
138979 if( op!=TK_ALL135 ){
138980 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
138981 struct ExprList_item *pItem;
138982 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
138983 assert( pItem!=0 )((void) (0));
138984 assert( pItem->u.x.iOrderByCol>0 )((void) (0));
138985 if( pItem->u.x.iOrderByCol==i ) break;
138986 }
138987 if( j==nOrderBy ){
138988 Expr *pNew = sqlite3Expr(db, TK_INTEGER155, 0);
138989 if( pNew==0 ) return SQLITE_NOMEM_BKPT7;
138990 pNew->flags |= EP_IntValue0x000800;
138991 pNew->u.iValue = i;
138992 p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
138993 if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
138994 }
138995 }
138996 }
138997
138998 /* Compute the comparison permutation and keyinfo that is used with
138999 ** the permutation used to determine if the next
139000 ** row of results comes from selectA or selectB. Also add explicit
139001 ** collations to the ORDER BY clause terms so that when the subqueries
139002 ** to the right and the left are evaluated, they use the correct
139003 ** collation.
139004 */
139005 aPermute = sqlite3DbMallocRawNN(db, sizeof(u32)*(nOrderBy + 1));
139006 if( aPermute ){
139007 struct ExprList_item *pItem;
139008 aPermute[0] = nOrderBy;
139009 for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
139010 assert( pItem!=0 )((void) (0));
139011 assert( pItem->u.x.iOrderByCol>0 )((void) (0));
139012 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr )((void) (0));
139013 aPermute[i] = pItem->u.x.iOrderByCol - 1;
139014 }
139015 pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
139016 }else{
139017 pKeyMerge = 0;
139018 }
139019
139020 /* Allocate a range of temporary registers and the KeyInfo needed
139021 ** for the logic that removes duplicate result rows when the
139022 ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
139023 */
139024 if( op==TK_ALL135 ){
139025 regPrev = 0;
139026 }else{
139027 int nExpr = p->pEList->nExpr;
139028 assert( nOrderBy>=nExpr || db->mallocFailed )((void) (0));
139029 regPrev = pParse->nMem+1;
139030 pParse->nMem += nExpr+1;
139031 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regPrev);
139032 pKeyDup = sqlite3KeyInfoAlloc(db, nExpr, 1);
139033 if( pKeyDup ){
139034 assert( sqlite3KeyInfoIsWriteable(pKeyDup) )((void) (0));
139035 for(i=0; i<nExpr; i++){
139036 pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
139037 pKeyDup->aSortFlags[i] = 0;
139038 }
139039 }
139040 }
139041
139042 /* Separate the left and the right query from one another
139043 */
139044 nSelect = 1;
139045 if( (op==TK_ALL135 || op==TK_UNION134)
139046 && OptimizationEnabled(db, SQLITE_BalancedMerge)(((db)->dbOptFlags&(0x00200000))==0)
139047 ){
139048 for(pSplit=p; pSplit->pPrior!=0 && pSplit->op==op; pSplit=pSplit->pPrior){
139049 nSelect++;
139050 assert( pSplit->pPrior->pNext==pSplit )((void) (0));
139051 }
139052 }
139053 if( nSelect<=3 ){
139054 pSplit = p;
139055 }else{
139056 pSplit = p;
139057 for(i=2; i<nSelect; i+=2){ pSplit = pSplit->pPrior; }
139058 }
139059 pPrior = pSplit->pPrior;
139060 assert( pPrior!=0 )((void) (0));
139061 pSplit->pPrior = 0;
139062 pPrior->pNext = 0;
139063 assert( p->pOrderBy == pOrderBy )((void) (0));
139064 assert( pOrderBy!=0 || db->mallocFailed )((void) (0));
139065 pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
139066 sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
139067 sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
139068
139069 /* Compute the limit registers */
139070 computeLimitRegisters(pParse, p, labelEnd);
139071 if( p->iLimit && op==TK_ALL135 ){
139072 regLimitA = ++pParse->nMem;
139073 regLimitB = ++pParse->nMem;
139074 sqlite3VdbeAddOp2(v, OP_Copy80, p->iOffset ? p->iOffset+1 : p->iLimit,
139075 regLimitA);
139076 sqlite3VdbeAddOp2(v, OP_Copy80, regLimitA, regLimitB);
139077 }else{
139078 regLimitA = regLimitB = 0;
139079 }
139080 sqlite3ExprDelete(db, p->pLimit);
139081 p->pLimit = 0;
139082
139083 regAddrA = ++pParse->nMem;
139084 regAddrB = ++pParse->nMem;
139085 regOutA = ++pParse->nMem;
139086 regOutB = ++pParse->nMem;
139087 sqlite3SelectDestInit(&destA, SRT_Coroutine13, regAddrA);
139088 sqlite3SelectDestInit(&destB, SRT_Coroutine13, regAddrB);
139089
139090 ExplainQueryPlan((pParse, 1, "MERGE (%s)", sqlite3SelectOpName(p->op)))sqlite3VdbeExplain (pParse, 1, "MERGE (%s)", sqlite3SelectOpName
(p->op))
;
139091
139092 /* Generate a coroutine to evaluate the SELECT statement to the
139093 ** left of the compound operator - the "A" select.
139094 */
139095 addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
139096 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine10, regAddrA, 0, addrSelectA);
139097 VdbeComment((v, "left SELECT"));
139098 pPrior->iLimit = regLimitA;
139099 ExplainQueryPlan((pParse, 1, "LEFT"))sqlite3VdbeExplain (pParse, 1, "LEFT");
139100 sqlite3Select(pParse, pPrior, &destA);
139101 sqlite3VdbeEndCoroutine(v, regAddrA);
139102 sqlite3VdbeJumpHere(v, addr1);
139103
139104 /* Generate a coroutine to evaluate the SELECT statement on
139105 ** the right - the "B" select
139106 */
139107 addrSelectB = sqlite3VdbeCurrentAddr(v) + 1;
139108 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine10, regAddrB, 0, addrSelectB);
139109 VdbeComment((v, "right SELECT"));
139110 savedLimit = p->iLimit;
139111 savedOffset = p->iOffset;
139112 p->iLimit = regLimitB;
139113 p->iOffset = 0;
139114 ExplainQueryPlan((pParse, 1, "RIGHT"))sqlite3VdbeExplain (pParse, 1, "RIGHT");
139115 sqlite3Select(pParse, p, &destB);
139116 p->iLimit = savedLimit;
139117 p->iOffset = savedOffset;
139118 sqlite3VdbeEndCoroutine(v, regAddrB);
139119
139120 /* Generate a subroutine that outputs the current row of the A
139121 ** select as the next output row of the compound select.
139122 */
139123 VdbeNoopComment((v, "Output routine for A"));
139124 addrOutA = generateOutputSubroutine(pParse,
139125 p, &destA, pDest, regOutA,
139126 regPrev, pKeyDup, labelEnd);
139127
139128 /* Generate a subroutine that outputs the current row of the B
139129 ** select as the next output row of the compound select.
139130 */
139131 if( op==TK_ALL135 || op==TK_UNION134 ){
139132 VdbeNoopComment((v, "Output routine for B"));
139133 addrOutB = generateOutputSubroutine(pParse,
139134 p, &destB, pDest, regOutB,
139135 regPrev, pKeyDup, labelEnd);
139136 }
139137 sqlite3KeyInfoUnref(pKeyDup);
139138
139139 /* Generate a subroutine to run when the results from select A
139140 ** are exhausted and only data in select B remains.
139141 */
139142 if( op==TK_EXCEPT136 || op==TK_INTERSECT137 ){
139143 addrEofA_noB = addrEofA = labelEnd;
139144 }else{
139145 VdbeNoopComment((v, "eof-A subroutine"));
139146 addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub9, regOutB, addrOutB);
139147 addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield11, regAddrB, labelEnd);
139148 VdbeCoverage(v);
139149 sqlite3VdbeGoto(v, addrEofA);
139150 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
139151 }
139152
139153 /* Generate a subroutine to run when the results from select B
139154 ** are exhausted and only data in select A remains.
139155 */
139156 if( op==TK_INTERSECT137 ){
139157 addrEofB = addrEofA;
139158 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
139159 }else{
139160 VdbeNoopComment((v, "eof-B subroutine"));
139161 addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub9, regOutA, addrOutA);
139162 sqlite3VdbeAddOp2(v, OP_Yield11, regAddrA, labelEnd); VdbeCoverage(v);
139163 sqlite3VdbeGoto(v, addrEofB);
139164 }
139165
139166 /* Generate code to handle the case of A<B
139167 */
139168 VdbeNoopComment((v, "A-lt-B subroutine"));
139169 addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub9, regOutA, addrOutA);
139170 sqlite3VdbeAddOp2(v, OP_Yield11, regAddrA, addrEofA); VdbeCoverage(v);
139171 sqlite3VdbeGoto(v, labelCmpr);
139172
139173 /* Generate code to handle the case of A==B
139174 */
139175 if( op==TK_ALL135 ){
139176 addrAeqB = addrAltB;
139177 }else if( op==TK_INTERSECT137 ){
139178 addrAeqB = addrAltB;
139179 addrAltB++;
139180 }else{
139181 VdbeNoopComment((v, "A-eq-B subroutine"));
139182 addrAeqB =
139183 sqlite3VdbeAddOp2(v, OP_Yield11, regAddrA, addrEofA); VdbeCoverage(v);
139184 sqlite3VdbeGoto(v, labelCmpr);
139185 }
139186
139187 /* Generate code to handle the case of A>B
139188 */
139189 VdbeNoopComment((v, "A-gt-B subroutine"));
139190 addrAgtB = sqlite3VdbeCurrentAddr(v);
139191 if( op==TK_ALL135 || op==TK_UNION134 ){
139192 sqlite3VdbeAddOp2(v, OP_Gosub9, regOutB, addrOutB);
139193 }
139194 sqlite3VdbeAddOp2(v, OP_Yield11, regAddrB, addrEofB); VdbeCoverage(v);
139195 sqlite3VdbeGoto(v, labelCmpr);
139196
139197 /* This code runs once to initialize everything.
139198 */
139199 sqlite3VdbeJumpHere(v, addr1);
139200 sqlite3VdbeAddOp2(v, OP_Yield11, regAddrA, addrEofA_noB); VdbeCoverage(v);
139201 sqlite3VdbeAddOp2(v, OP_Yield11, regAddrB, addrEofB); VdbeCoverage(v);
139202
139203 /* Implement the main merge loop
139204 */
139205 sqlite3VdbeResolveLabel(v, labelCmpr);
139206 sqlite3VdbeAddOp4(v, OP_Permutation89, 0, 0, 0, (char*)aPermute, P4_INTARRAY(-14));
139207 sqlite3VdbeAddOp4(v, OP_Compare90, destA.iSdst, destB.iSdst, nOrderBy,
139208 (char*)pKeyMerge, P4_KEYINFO(-8));
139209 sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE0x01);
139210 sqlite3VdbeAddOp3(v, OP_Jump13, addrAltB, addrAeqB, addrAgtB); VdbeCoverage(v);
139211
139212 /* Jump to the this point in order to terminate the query.
139213 */
139214 sqlite3VdbeResolveLabel(v, labelEnd);
139215
139216 /* Reassembly the compound query so that it will be freed correctly
139217 ** by the calling function */
139218 if( pSplit->pPrior ){
139219 sqlite3SelectDelete(db, pSplit->pPrior);
139220 }
139221 pSplit->pPrior = pPrior;
139222 pPrior->pNext = pSplit;
139223 sqlite3ExprListDelete(db, pPrior->pOrderBy);
139224 pPrior->pOrderBy = 0;
139225
139226 /*** TBD: Insert subroutine calls to close cursors on incomplete
139227 **** subqueries ****/
139228 ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
139229 return pParse->nErr!=0;
139230}
139231#endif
139232
139233#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
139234
139235/* An instance of the SubstContext object describes an substitution edit
139236** to be performed on a parse tree.
139237**
139238** All references to columns in table iTable are to be replaced by corresponding
139239** expressions in pEList.
139240**
139241** ## About "isOuterJoin":
139242**
139243** The isOuterJoin column indicates that the replacement will occur into a
139244** position in the parent that NULL-able due to an OUTER JOIN. Either the
139245** target slot in the parent is the right operand of a LEFT JOIN, or one of
139246** the left operands of a RIGHT JOIN. In either case, we need to potentially
139247** bypass the substituted expression with OP_IfNullRow.
139248**
139249** Suppose the original expression integer constant. Even though the table
139250** has the nullRow flag set, because the expression is an integer constant,
139251** it will not be NULLed out. So instead, we insert an OP_IfNullRow opcode
139252** that checks to see if the nullRow flag is set on the table. If the nullRow
139253** flag is set, then the value in the register is set to NULL and the original
139254** expression is bypassed. If the nullRow flag is not set, then the original
139255** expression runs to populate the register.
139256**
139257** Example where this is needed:
139258**
139259** CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT);
139260** CREATE TABLE t2(x INT UNIQUE);
139261**
139262** SELECT a,b,m,x FROM t1 LEFT JOIN (SELECT 59 AS m,x FROM t2) ON b=x;
139263**
139264** When the subquery on the right side of the LEFT JOIN is flattened, we
139265** have to add OP_IfNullRow in front of the OP_Integer that implements the
139266** "m" value of the subquery so that a NULL will be loaded instead of 59
139267** when processing a non-matched row of the left.
139268*/
139269typedef struct SubstContext {
139270 Parse *pParse; /* The parsing context */
139271 int iTable; /* Replace references to this table */
139272 int iNewTable; /* New table number */
139273 int isOuterJoin; /* Add TK_IF_NULL_ROW opcodes on each replacement */
139274 ExprList *pEList; /* Replacement expressions */
139275} SubstContext;
139276
139277/* Forward Declarations */
139278static void substExprList(SubstContext*, ExprList*);
139279static void substSelect(SubstContext*, Select*, int);
139280
139281/*
139282** Scan through the expression pExpr. Replace every reference to
139283** a column in table number iTable with a copy of the iColumn-th
139284** entry in pEList. (But leave references to the ROWID column
139285** unchanged.)
139286**
139287** This routine is part of the flattening procedure. A subquery
139288** whose result set is defined by pEList appears as entry in the
139289** FROM clause of a SELECT such that the VDBE cursor assigned to that
139290** FORM clause entry is iTable. This routine makes the necessary
139291** changes to pExpr so that it refers directly to the source table
139292** of the subquery rather the result set of the subquery.
139293*/
139294static Expr *substExpr(
139295 SubstContext *pSubst, /* Description of the substitution */
139296 Expr *pExpr /* Expr in which substitution occurs */
139297){
139298 if( pExpr==0 ) return 0;
139299 if( ExprHasProperty(pExpr, EP_OuterON|EP_InnerON)(((pExpr)->flags&(0x000001|0x000002))!=0)
139300 && pExpr->w.iJoin==pSubst->iTable
139301 ){
139302 testcase( ExprHasProperty(pExpr, EP_InnerON) );
139303 pExpr->w.iJoin = pSubst->iNewTable;
139304 }
139305 if( pExpr->op==TK_COLUMN167
139306 && pExpr->iTable==pSubst->iTable
139307 && !ExprHasProperty(pExpr, EP_FixedCol)(((pExpr)->flags&(0x000020))!=0)
139308 ){
139309#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
139310 if( pExpr->iColumn<0 ){
139311 pExpr->op = TK_NULL121;
139312 }else
139313#endif
139314 {
139315 Expr *pNew;
139316 Expr *pCopy = pSubst->pEList->a[pExpr->iColumn].pExpr;
139317 Expr ifNullRow;
139318 assert( pSubst->pEList!=0 && pExpr->iColumn<pSubst->pEList->nExpr )((void) (0));
139319 assert( pExpr->pRight==0 )((void) (0));
139320 if( sqlite3ExprIsVector(pCopy) ){
139321 sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
139322 }else{
139323 sqlite3 *db = pSubst->pParse->db;
139324 if( pSubst->isOuterJoin && pCopy->op!=TK_COLUMN167 ){
139325 memset(&ifNullRow, 0, sizeof(ifNullRow));
139326 ifNullRow.op = TK_IF_NULL_ROW179;
139327 ifNullRow.pLeft = pCopy;
139328 ifNullRow.iTable = pSubst->iNewTable;
139329 ifNullRow.flags = EP_IfNullRow0x040000;
139330 pCopy = &ifNullRow;
139331 }
139332 testcase( ExprHasProperty(pCopy, EP_Subquery) );
139333 pNew = sqlite3ExprDup(db, pCopy, 0);
139334 if( db->mallocFailed ){
139335 sqlite3ExprDelete(db, pNew);
139336 return pExpr;
139337 }
139338 if( pSubst->isOuterJoin ){
139339 ExprSetProperty(pNew, EP_CanBeNull)(pNew)->flags|=(0x200000);
139340 }
139341 if( ExprHasProperty(pExpr,EP_OuterON|EP_InnerON)(((pExpr)->flags&(0x000001|0x000002))!=0) ){
139342 sqlite3SetJoinExpr(pNew, pExpr->w.iJoin,
139343 pExpr->flags & (EP_OuterON0x000001|EP_InnerON0x000002));
139344 }
139345 sqlite3ExprDelete(db, pExpr);
139346 pExpr = pNew;
139347 if( pExpr->op==TK_TRUEFALSE170 ){
139348 pExpr->u.iValue = sqlite3ExprTruthValue(pExpr);
139349 pExpr->op = TK_INTEGER155;
139350 ExprSetProperty(pExpr, EP_IntValue)(pExpr)->flags|=(0x000800);
139351 }
139352
139353 /* Ensure that the expression now has an implicit collation sequence,
139354 ** just as it did when it was a column of a view or sub-query. */
139355 if( pExpr->op!=TK_COLUMN167 && pExpr->op!=TK_COLLATE113 ){
139356 CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
139357 pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr,
139358 (pColl ? pColl->zName : "BINARY")
139359 );
139360 }
139361 ExprClearProperty(pExpr, EP_Collate)(pExpr)->flags&=~(0x000200);
139362 }
139363 }
139364 }else{
139365 if( pExpr->op==TK_IF_NULL_ROW179 && pExpr->iTable==pSubst->iTable ){
139366 pExpr->iTable = pSubst->iNewTable;
139367 }
139368 pExpr->pLeft = substExpr(pSubst, pExpr->pLeft);
139369 pExpr->pRight = substExpr(pSubst, pExpr->pRight);
139370 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
139371 substSelect(pSubst, pExpr->x.pSelect, 1);
139372 }else{
139373 substExprList(pSubst, pExpr->x.pList);
139374 }
139375#ifndef SQLITE_OMIT_WINDOWFUNC
139376 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
139377 Window *pWin = pExpr->y.pWin;
139378 pWin->pFilter = substExpr(pSubst, pWin->pFilter);
139379 substExprList(pSubst, pWin->pPartition);
139380 substExprList(pSubst, pWin->pOrderBy);
139381 }
139382#endif
139383 }
139384 return pExpr;
139385}
139386static void substExprList(
139387 SubstContext *pSubst, /* Description of the substitution */
139388 ExprList *pList /* List to scan and in which to make substitutes */
139389){
139390 int i;
139391 if( pList==0 ) return;
139392 for(i=0; i<pList->nExpr; i++){
139393 pList->a[i].pExpr = substExpr(pSubst, pList->a[i].pExpr);
139394 }
139395}
139396static void substSelect(
139397 SubstContext *pSubst, /* Description of the substitution */
139398 Select *p, /* SELECT statement in which to make substitutions */
139399 int doPrior /* Do substitutes on p->pPrior too */
139400){
139401 SrcList *pSrc;
139402 SrcItem *pItem;
139403 int i;
139404 if( !p ) return;
139405 do{
139406 substExprList(pSubst, p->pEList);
139407 substExprList(pSubst, p->pGroupBy);
139408 substExprList(pSubst, p->pOrderBy);
139409 p->pHaving = substExpr(pSubst, p->pHaving);
139410 p->pWhere = substExpr(pSubst, p->pWhere);
139411 pSrc = p->pSrc;
139412 assert( pSrc!=0 )((void) (0));
139413 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
139414 substSelect(pSubst, pItem->pSelect, 1);
139415 if( pItem->fg.isTabFunc ){
139416 substExprList(pSubst, pItem->u1.pFuncArg);
139417 }
139418 }
139419 }while( doPrior && (p = p->pPrior)!=0 );
139420}
139421#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
139422
139423#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
139424/*
139425** pSelect is a SELECT statement and pSrcItem is one item in the FROM
139426** clause of that SELECT.
139427**
139428** This routine scans the entire SELECT statement and recomputes the
139429** pSrcItem->colUsed mask.
139430*/
139431static int recomputeColumnsUsedExpr(Walker *pWalker, Expr *pExpr){
139432 SrcItem *pItem;
139433 if( pExpr->op!=TK_COLUMN167 ) return WRC_Continue0;
139434 pItem = pWalker->u.pSrcItem;
139435 if( pItem->iCursor!=pExpr->iTable ) return WRC_Continue0;
139436 if( pExpr->iColumn<0 ) return WRC_Continue0;
139437 pItem->colUsed |= sqlite3ExprColUsed(pExpr);
139438 return WRC_Continue0;
139439}
139440static void recomputeColumnsUsed(
139441 Select *pSelect, /* The complete SELECT statement */
139442 SrcItem *pSrcItem /* Which FROM clause item to recompute */
139443){
139444 Walker w;
139445 if( NEVER(pSrcItem->pTab==0)(pSrcItem->pTab==0) ) return;
139446 memset(&w, 0, sizeof(w));
139447 w.xExprCallback = recomputeColumnsUsedExpr;
139448 w.xSelectCallback = sqlite3SelectWalkNoop;
139449 w.u.pSrcItem = pSrcItem;
139450 pSrcItem->colUsed = 0;
139451 sqlite3WalkSelect(&w, pSelect);
139452}
139453#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
139454
139455#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
139456/*
139457** Assign new cursor numbers to each of the items in pSrc. For each
139458** new cursor number assigned, set an entry in the aCsrMap[] array
139459** to map the old cursor number to the new:
139460**
139461** aCsrMap[iOld+1] = iNew;
139462**
139463** The array is guaranteed by the caller to be large enough for all
139464** existing cursor numbers in pSrc. aCsrMap[0] is the array size.
139465**
139466** If pSrc contains any sub-selects, call this routine recursively
139467** on the FROM clause of each such sub-select, with iExcept set to -1.
139468*/
139469static void srclistRenumberCursors(
139470 Parse *pParse, /* Parse context */
139471 int *aCsrMap, /* Array to store cursor mappings in */
139472 SrcList *pSrc, /* FROM clause to renumber */
139473 int iExcept /* FROM clause item to skip */
139474){
139475 int i;
139476 SrcItem *pItem;
139477 for(i=0, pItem=pSrc->a; i<pSrc->nSrc; i++, pItem++){
139478 if( i!=iExcept ){
139479 Select *p;
139480 assert( pItem->iCursor < aCsrMap[0] )((void) (0));
139481 if( !pItem->fg.isRecursive || aCsrMap[pItem->iCursor+1]==0 ){
139482 aCsrMap[pItem->iCursor+1] = pParse->nTab++;
139483 }
139484 pItem->iCursor = aCsrMap[pItem->iCursor+1];
139485 for(p=pItem->pSelect; p; p=p->pPrior){
139486 srclistRenumberCursors(pParse, aCsrMap, p->pSrc, -1);
139487 }
139488 }
139489 }
139490}
139491
139492/*
139493** *piCursor is a cursor number. Change it if it needs to be mapped.
139494*/
139495static void renumberCursorDoMapping(Walker *pWalker, int *piCursor){
139496 int *aCsrMap = pWalker->u.aiCol;
139497 int iCsr = *piCursor;
139498 if( iCsr < aCsrMap[0] && aCsrMap[iCsr+1]>0 ){
139499 *piCursor = aCsrMap[iCsr+1];
139500 }
139501}
139502
139503/*
139504** Expression walker callback used by renumberCursors() to update
139505** Expr objects to match newly assigned cursor numbers.
139506*/
139507static int renumberCursorsCb(Walker *pWalker, Expr *pExpr){
139508 int op = pExpr->op;
139509 if( op==TK_COLUMN167 || op==TK_IF_NULL_ROW179 ){
139510 renumberCursorDoMapping(pWalker, &pExpr->iTable);
139511 }
139512 if( ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(0x000001))!=0) ){
139513 renumberCursorDoMapping(pWalker, &pExpr->w.iJoin);
139514 }
139515 return WRC_Continue0;
139516}
139517
139518/*
139519** Assign a new cursor number to each cursor in the FROM clause (Select.pSrc)
139520** of the SELECT statement passed as the second argument, and to each
139521** cursor in the FROM clause of any FROM clause sub-selects, recursively.
139522** Except, do not assign a new cursor number to the iExcept'th element in
139523** the FROM clause of (*p). Update all expressions and other references
139524** to refer to the new cursor numbers.
139525**
139526** Argument aCsrMap is an array that may be used for temporary working
139527** space. Two guarantees are made by the caller:
139528**
139529** * the array is larger than the largest cursor number used within the
139530** select statement passed as an argument, and
139531**
139532** * the array entries for all cursor numbers that do *not* appear in
139533** FROM clauses of the select statement as described above are
139534** initialized to zero.
139535*/
139536static void renumberCursors(
139537 Parse *pParse, /* Parse context */
139538 Select *p, /* Select to renumber cursors within */
139539 int iExcept, /* FROM clause item to skip */
139540 int *aCsrMap /* Working space */
139541){
139542 Walker w;
139543 srclistRenumberCursors(pParse, aCsrMap, p->pSrc, iExcept);
139544 memset(&w, 0, sizeof(w));
139545 w.u.aiCol = aCsrMap;
139546 w.xExprCallback = renumberCursorsCb;
139547 w.xSelectCallback = sqlite3SelectWalkNoop;
139548 sqlite3WalkSelect(&w, p);
139549}
139550#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
139551
139552#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
139553/*
139554** This routine attempts to flatten subqueries as a performance optimization.
139555** This routine returns 1 if it makes changes and 0 if no flattening occurs.
139556**
139557** To understand the concept of flattening, consider the following
139558** query:
139559**
139560** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
139561**
139562** The default way of implementing this query is to execute the
139563** subquery first and store the results in a temporary table, then
139564** run the outer query on that temporary table. This requires two
139565** passes over the data. Furthermore, because the temporary table
139566** has no indices, the WHERE clause on the outer query cannot be
139567** optimized.
139568**
139569** This routine attempts to rewrite queries such as the above into
139570** a single flat select, like this:
139571**
139572** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
139573**
139574** The code generated for this simplification gives the same result
139575** but only has to scan the data once. And because indices might
139576** exist on the table t1, a complete scan of the data might be
139577** avoided.
139578**
139579** Flattening is subject to the following constraints:
139580**
139581** (**) We no longer attempt to flatten aggregate subqueries. Was:
139582** The subquery and the outer query cannot both be aggregates.
139583**
139584** (**) We no longer attempt to flatten aggregate subqueries. Was:
139585** (2) If the subquery is an aggregate then
139586** (2a) the outer query must not be a join and
139587** (2b) the outer query must not use subqueries
139588** other than the one FROM-clause subquery that is a candidate
139589** for flattening. (This is due to ticket [2f7170d73bf9abf80]
139590** from 2015-02-09.)
139591**
139592** (3) If the subquery is the right operand of a LEFT JOIN then
139593** (3a) the subquery may not be a join and
139594** (3b) the FROM clause of the subquery may not contain a virtual
139595** table and
139596** (3c) the outer query may not be an aggregate.
139597** (3d) the outer query may not be DISTINCT.
139598** See also (26) for restrictions on RIGHT JOIN.
139599**
139600** (4) The subquery can not be DISTINCT.
139601**
139602** (**) At one point restrictions (4) and (5) defined a subset of DISTINCT
139603** sub-queries that were excluded from this optimization. Restriction
139604** (4) has since been expanded to exclude all DISTINCT subqueries.
139605**
139606** (**) We no longer attempt to flatten aggregate subqueries. Was:
139607** If the subquery is aggregate, the outer query may not be DISTINCT.
139608**
139609** (7) The subquery must have a FROM clause. TODO: For subqueries without
139610** A FROM clause, consider adding a FROM clause with the special
139611** table sqlite_once that consists of a single row containing a
139612** single NULL.
139613**
139614** (8) If the subquery uses LIMIT then the outer query may not be a join.
139615**
139616** (9) If the subquery uses LIMIT then the outer query may not be aggregate.
139617**
139618** (**) Restriction (10) was removed from the code on 2005-02-05 but we
139619** accidently carried the comment forward until 2014-09-15. Original
139620** constraint: "If the subquery is aggregate then the outer query
139621** may not use LIMIT."
139622**
139623** (11) The subquery and the outer query may not both have ORDER BY clauses.
139624**
139625** (**) Not implemented. Subsumed into restriction (3). Was previously
139626** a separate restriction deriving from ticket #350.
139627**
139628** (13) The subquery and outer query may not both use LIMIT.
139629**
139630** (14) The subquery may not use OFFSET.
139631**
139632** (15) If the outer query is part of a compound select, then the
139633** subquery may not use LIMIT.
139634** (See ticket #2339 and ticket [02a8e81d44]).
139635**
139636** (16) If the outer query is aggregate, then the subquery may not
139637** use ORDER BY. (Ticket #2942) This used to not matter
139638** until we introduced the group_concat() function.
139639**
139640** (17) If the subquery is a compound select, then
139641** (17a) all compound operators must be a UNION ALL, and
139642** (17b) no terms within the subquery compound may be aggregate
139643** or DISTINCT, and
139644** (17c) every term within the subquery compound must have a FROM clause
139645** (17d) the outer query may not be
139646** (17d1) aggregate, or
139647** (17d2) DISTINCT
139648** (17e) the subquery may not contain window functions, and
139649** (17f) the subquery must not be the RHS of a LEFT JOIN.
139650** (17g) either the subquery is the first element of the outer
139651** query or there are no RIGHT or FULL JOINs in any arm
139652** of the subquery. (This is a duplicate of condition (27b).)
139653**
139654** The parent and sub-query may contain WHERE clauses. Subject to
139655** rules (11), (13) and (14), they may also contain ORDER BY,
139656** LIMIT and OFFSET clauses. The subquery cannot use any compound
139657** operator other than UNION ALL because all the other compound
139658** operators have an implied DISTINCT which is disallowed by
139659** restriction (4).
139660**
139661** Also, each component of the sub-query must return the same number
139662** of result columns. This is actually a requirement for any compound
139663** SELECT statement, but all the code here does is make sure that no
139664** such (illegal) sub-query is flattened. The caller will detect the
139665** syntax error and return a detailed message.
139666**
139667** (18) If the sub-query is a compound select, then all terms of the
139668** ORDER BY clause of the parent must be copies of a term returned
139669** by the parent query.
139670**
139671** (19) If the subquery uses LIMIT then the outer query may not
139672** have a WHERE clause.
139673**
139674** (20) If the sub-query is a compound select, then it must not use
139675** an ORDER BY clause. Ticket #3773. We could relax this constraint
139676** somewhat by saying that the terms of the ORDER BY clause must
139677** appear as unmodified result columns in the outer query. But we
139678** have other optimizations in mind to deal with that case.
139679**
139680** (21) If the subquery uses LIMIT then the outer query may not be
139681** DISTINCT. (See ticket [752e1646fc]).
139682**
139683** (22) The subquery may not be a recursive CTE.
139684**
139685** (23) If the outer query is a recursive CTE, then the sub-query may not be
139686** a compound query. This restriction is because transforming the
139687** parent to a compound query confuses the code that handles
139688** recursive queries in multiSelect().
139689**
139690** (**) We no longer attempt to flatten aggregate subqueries. Was:
139691** The subquery may not be an aggregate that uses the built-in min() or
139692** or max() functions. (Without this restriction, a query like:
139693** "SELECT x FROM (SELECT max(y), x FROM t1)" would not necessarily
139694** return the value X for which Y was maximal.)
139695**
139696** (25) If either the subquery or the parent query contains a window
139697** function in the select list or ORDER BY clause, flattening
139698** is not attempted.
139699**
139700** (26) The subquery may not be the right operand of a RIGHT JOIN.
139701** See also (3) for restrictions on LEFT JOIN.
139702**
139703** (27) The subquery may not contain a FULL or RIGHT JOIN unless it
139704** is the first element of the parent query. This must be the
139705** the case if:
139706** (27a) the subquery is not compound query, and
139707** (27b) the subquery is a compound query and the RIGHT JOIN occurs
139708** in any arm of the compound query. (See also (17g).)
139709**
139710** (28) The subquery is not a MATERIALIZED CTE.
139711**
139712** (29) Either the subquery is not the right-hand operand of a join with an
139713** ON or USING clause nor the right-hand operand of a NATURAL JOIN, or
139714** the right-most table within the FROM clause of the subquery
139715** is not part of an outer join.
139716**
139717**
139718** In this routine, the "p" parameter is a pointer to the outer query.
139719** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
139720** uses aggregates.
139721**
139722** If flattening is not attempted, this routine is a no-op and returns 0.
139723** If flattening is attempted this routine returns 1.
139724**
139725** All of the expression analysis must occur on both the outer query and
139726** the subquery before this routine runs.
139727*/
139728static int flattenSubquery(
139729 Parse *pParse, /* Parsing context */
139730 Select *p, /* The parent or outer SELECT statement */
139731 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
139732 int isAgg /* True if outer SELECT uses aggregate functions */
139733){
139734 const char *zSavedAuthContext = pParse->zAuthContext;
139735 Select *pParent; /* Current UNION ALL term of the other query */
139736 Select *pSub; /* The inner query or "subquery" */
139737 Select *pSub1; /* Pointer to the rightmost select in sub-query */
139738 SrcList *pSrc; /* The FROM clause of the outer query */
139739 SrcList *pSubSrc; /* The FROM clause of the subquery */
139740 int iParent; /* VDBE cursor number of the pSub result set temp table */
139741 int iNewParent = -1;/* Replacement table for iParent */
139742 int isOuterJoin = 0; /* True if pSub is the right side of a LEFT JOIN */
139743 int i; /* Loop counter */
139744 Expr *pWhere; /* The WHERE clause */
139745 SrcItem *pSubitem; /* The subquery */
139746 sqlite3 *db = pParse->db;
139747 Walker w; /* Walker to persist agginfo data */
139748 int *aCsrMap = 0;
139749
139750 /* Check to see if flattening is permitted. Return 0 if not.
139751 */
139752 assert( p!=0 )((void) (0));
139753 assert( p->pPrior==0 )((void) (0));
139754 if( OptimizationDisabled(db, SQLITE_QueryFlattener)(((db)->dbOptFlags&(0x00000001))!=0) ) return 0;
1
Assuming the condition is false
2
Taking false branch
139755 pSrc = p->pSrc;
139756 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc )((void) (0));
139757 pSubitem = &pSrc->a[iFrom];
139758 iParent = pSubitem->iCursor;
139759 pSub = pSubitem->pSelect;
139760 assert( pSub!=0 )((void) (0));
139761
139762#ifndef SQLITE_OMIT_WINDOWFUNC
139763 if( p->pWin || pSub->pWin ) return 0; /* Restriction (25) */
3
Assuming field 'pWin' is null
4
Assuming field 'pWin' is null
5
Taking false branch
139764#endif
139765
139766 pSubSrc = pSub->pSrc;
139767 assert( pSubSrc )((void) (0));
139768 /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
139769 ** not arbitrary expressions, we allowed some combining of LIMIT and OFFSET
139770 ** because they could be computed at compile-time. But when LIMIT and OFFSET
139771 ** became arbitrary expressions, we were forced to add restrictions (13)
139772 ** and (14). */
139773 if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
6
Assuming field 'pLimit' is null
139774 if( pSub->pLimit
6.1
Field 'pLimit' is null
&& pSub->pLimit->pRight ) return 0; /* Restriction (14) */
139775 if( (p->selFlags & SF_Compound0x0000100)!=0 && pSub->pLimit ){
7
Assuming the condition is false
139776 return 0; /* Restriction (15) */
139777 }
139778 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
8
Assuming field 'nSrc' is not equal to 0
9
Taking false branch
139779 if( pSub->selFlags & SF_Distinct0x0000001 ) return 0; /* Restriction (4) */
10
Assuming the condition is false
139780 if( pSub->pLimit
10.1
Field 'pLimit' is null
&& (pSrc->nSrc>1 || isAgg) ){
139781 return 0; /* Restrictions (8)(9) */
139782 }
139783 if( p->pOrderBy && pSub->pOrderBy ){
11
Assuming field 'pOrderBy' is null
139784 return 0; /* Restriction (11) */
139785 }
139786 if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
12
Assuming 'isAgg' is 0
139787 if( pSub->pLimit
12.1
Field 'pLimit' is null
&& p->pWhere ) return 0; /* Restriction (19) */
139788 if( pSub->pLimit
12.2
Field 'pLimit' is null
&& (p->selFlags & SF_Distinct0x0000001)!=0 ){
139789 return 0; /* Restriction (21) */
139790 }
139791 if( pSub->selFlags & (SF_Recursive0x0002000) ){
13
Assuming the condition is false
14
Taking false branch
139792 return 0; /* Restrictions (22) */
139793 }
139794
139795 /*
139796 ** If the subquery is the right operand of a LEFT JOIN, then the
139797 ** subquery may not be a join itself (3a). Example of why this is not
139798 ** allowed:
139799 **
139800 ** t1 LEFT OUTER JOIN (t2 JOIN t3)
139801 **
139802 ** If we flatten the above, we would get
139803 **
139804 ** (t1 LEFT OUTER JOIN t2) JOIN t3
139805 **
139806 ** which is not at all the same thing.
139807 **
139808 ** If the subquery is the right operand of a LEFT JOIN, then the outer
139809 ** query cannot be an aggregate. (3c) This is an artifact of the way
139810 ** aggregates are processed - there is no mechanism to determine if
139811 ** the LEFT JOIN table should be all-NULL.
139812 **
139813 ** See also tickets #306, #350, and #3300.
139814 */
139815 if( (pSubitem->fg.jointype & (JT_OUTER0x20|JT_LTORJ0x40))!=0 ){
15
Assuming the condition is false
16
Taking false branch
139816 if( pSubSrc->nSrc>1 /* (3a) */
139817 || isAgg /* (3c) */
139818 || IsVirtual(pSubSrc->a[0].pTab)((pSubSrc->a[0].pTab)->eTabType==1) /* (3b) */
139819 || (p->selFlags & SF_Distinct0x0000001)!=0 /* (3d) */
139820 || (pSubitem->fg.jointype & JT_RIGHT0x10)!=0 /* (26) */
139821 ){
139822 return 0;
139823 }
139824 isOuterJoin = 1;
139825 }
139826#ifdef SQLITE_EXTRA_IFNULLROW
139827 else if( iFrom>0 && !isAgg ){
139828 /* Setting isOuterJoin to -1 causes OP_IfNullRow opcodes to be generated for
139829 ** every reference to any result column from subquery in a join, even
139830 ** though they are not necessary. This will stress-test the OP_IfNullRow
139831 ** opcode. */
139832 isOuterJoin = -1;
139833 }
139834#endif
139835
139836 assert( pSubSrc->nSrc>0 )((void) (0)); /* True by restriction (7) */
139837 if( iFrom>0 && (pSubSrc->a[0].fg.jointype & JT_LTORJ0x40)!=0 ){
17
Assuming 'iFrom' is <= 0
139838 return 0; /* Restriction (27a) */
139839 }
139840 if( pSubitem->fg.isCte && pSubitem->u2.pCteUse->eM10d==M10d_Yes0 ){
18
Assuming field 'isCte' is 0
139841 return 0; /* (28) */
139842 }
139843
139844 /* Restriction (29):
139845 **
139846 ** We do not want two constraints on the same term of the flattened
139847 ** query where one constraint has EP_InnerON and the other is EP_OuterON.
139848 ** To prevent this, one or the other of the following conditions must be
139849 ** false:
139850 **
139851 ** (29a) The right-most entry in the FROM clause of the subquery
139852 ** must not be part of an outer join.
139853 **
139854 ** (29b) The subquery itself must not be the right operand of a
139855 ** NATURAL join or a join that as an ON or USING clause.
139856 **
139857 ** These conditions are sufficient to keep an EP_OuterON from being
139858 ** flattened into an EP_InnerON. Restrictions (3a) and (27a) prevent
139859 ** an EP_InnerON from being flattened into an EP_OuterON.
139860 */
139861 if( pSubSrc->nSrc>=2
19
Assuming field 'nSrc' is < 2
139862 && (pSubSrc->a[pSubSrc->nSrc-1].fg.jointype & JT_OUTER0x20)!=0
139863 ){
139864 if( (pSubitem->fg.jointype & JT_NATURAL0x04)!=0
139865 || pSubitem->fg.isUsing
139866 || NEVER(pSubitem->u3.pOn!=0)(pSubitem->u3.pOn!=0) /* ON clause already shifted into WHERE */
139867 || pSubitem->fg.isOn
139868 ){
139869 return 0;
139870 }
139871 }
139872
139873 /* Restriction (17): If the sub-query is a compound SELECT, then it must
139874 ** use only the UNION ALL operator. And none of the simple select queries
139875 ** that make up the compound SELECT are allowed to be aggregate or distinct
139876 ** queries.
139877 */
139878 if( pSub->pPrior ){
20
Assuming field 'pPrior' is null
21
Taking false branch
139879 if( pSub->pOrderBy ){
139880 return 0; /* Restriction (20) */
139881 }
139882 if( isAgg || (p->selFlags & SF_Distinct0x0000001)!=0 || isOuterJoin>0 ){
139883 return 0; /* (17d1), (17d2), or (17f) */
139884 }
139885 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
139886 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
139887 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
139888 assert( pSub->pSrc!=0 )((void) (0));
139889 assert( (pSub->selFlags & SF_Recursive)==0 )((void) (0));
139890 assert( pSub->pEList->nExpr==pSub1->pEList->nExpr )((void) (0));
139891 if( (pSub1->selFlags & (SF_Distinct0x0000001|SF_Aggregate0x0000008))!=0 /* (17b) */
139892 || (pSub1->pPrior && pSub1->op!=TK_ALL135) /* (17a) */
139893 || pSub1->pSrc->nSrc<1 /* (17c) */
139894#ifndef SQLITE_OMIT_WINDOWFUNC
139895 || pSub1->pWin /* (17e) */
139896#endif
139897 ){
139898 return 0;
139899 }
139900 if( iFrom>0 && (pSub1->pSrc->a[0].fg.jointype & JT_LTORJ0x40)!=0 ){
139901 /* Without this restriction, the JT_LTORJ flag would end up being
139902 ** omitted on left-hand tables of the right join that is being
139903 ** flattened. */
139904 return 0; /* Restrictions (17g), (27b) */
139905 }
139906 testcase( pSub1->pSrc->nSrc>1 );
139907 }
139908
139909 /* Restriction (18). */
139910 if( p->pOrderBy ){
139911 int ii;
139912 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
139913 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
139914 }
139915 }
139916
139917 /* Restriction (23) */
139918 if( (p->selFlags & SF_Recursive0x0002000) ) return 0;
139919
139920 if( pSrc->nSrc>1 ){
139921 if( pParse->nSelect>500 ) return 0;
139922 if( OptimizationDisabled(db, SQLITE_FlttnUnionAll)(((db)->dbOptFlags&(0x00800000))!=0) ) return 0;
139923 aCsrMap = sqlite3DbMallocZero(db, ((i64)pParse->nTab+1)*sizeof(int));
139924 if( aCsrMap ) aCsrMap[0] = pParse->nTab;
139925 }
139926 }
139927
139928 /***** If we reach this point, flattening is permitted. *****/
139929 SELECTTRACE(1,pParse,p,("flatten %u.%p from term %d\n",
139930 pSub->selId, pSub, iFrom));
139931
139932 /* Authorize the subquery */
139933 pParse->zAuthContext = pSubitem->zName;
139934 TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT21, 0, 0, 0);
139935 testcase( i==SQLITE_DENY );
139936 pParse->zAuthContext = zSavedAuthContext;
139937
139938 /* Delete the transient structures associated with thesubquery */
139939 pSub1 = pSubitem->pSelect;
139940 sqlite3DbFree(db, pSubitem->zDatabase);
139941 sqlite3DbFree(db, pSubitem->zName);
139942 sqlite3DbFree(db, pSubitem->zAlias);
139943 pSubitem->zDatabase = 0;
139944 pSubitem->zName = 0;
139945 pSubitem->zAlias = 0;
139946 pSubitem->pSelect = 0;
139947 assert( pSubitem->fg.isUsing!=0 || pSubitem->u3.pOn==0 )((void) (0));
139948
139949 /* If the sub-query is a compound SELECT statement, then (by restrictions
139950 ** 17 and 18 above) it must be a UNION ALL and the parent query must
139951 ** be of the form:
139952 **
139953 ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
139954 **
139955 ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
139956 ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
139957 ** OFFSET clauses and joins them to the left-hand-side of the original
139958 ** using UNION ALL operators. In this case N is the number of simple
139959 ** select statements in the compound sub-query.
139960 **
139961 ** Example:
139962 **
139963 ** SELECT a+1 FROM (
139964 ** SELECT x FROM tab
139965 ** UNION ALL
139966 ** SELECT y FROM tab
139967 ** UNION ALL
139968 ** SELECT abs(z*2) FROM tab2
139969 ** ) WHERE a!=5 ORDER BY 1
139970 **
139971 ** Transformed into:
139972 **
139973 ** SELECT x+1 FROM tab WHERE x+1!=5
139974 ** UNION ALL
139975 ** SELECT y+1 FROM tab WHERE y+1!=5
139976 ** UNION ALL
139977 ** SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
139978 ** ORDER BY 1
139979 **
139980 ** We call this the "compound-subquery flattening".
139981 */
139982 for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
22
Loop condition is false. Execution continues on line 140013
139983 Select *pNew;
139984 ExprList *pOrderBy = p->pOrderBy;
139985 Expr *pLimit = p->pLimit;
139986 Select *pPrior = p->pPrior;
139987 Table *pItemTab = pSubitem->pTab;
139988 pSubitem->pTab = 0;
139989 p->pOrderBy = 0;
139990 p->pPrior = 0;
139991 p->pLimit = 0;
139992 pNew = sqlite3SelectDup(db, p, 0);
139993 p->pLimit = pLimit;
139994 p->pOrderBy = pOrderBy;
139995 p->op = TK_ALL135;
139996 pSubitem->pTab = pItemTab;
139997 if( pNew==0 ){
139998 p->pPrior = pPrior;
139999 }else{
140000 pNew->selId = ++pParse->nSelect;
140001 if( aCsrMap && ALWAYS(db->mallocFailed==0)(db->mallocFailed==0) ){
140002 renumberCursors(pParse, pNew, iFrom, aCsrMap);
140003 }
140004 pNew->pPrior = pPrior;
140005 if( pPrior ) pPrior->pNext = pNew;
140006 pNew->pNext = p;
140007 p->pPrior = pNew;
140008 SELECTTRACE(2,pParse,p,("compound-subquery flattener"
140009 " creates %u as peer\n",pNew->selId));
140010 }
140011 assert( pSubitem->pSelect==0 )((void) (0));
140012 }
140013 sqlite3DbFree(db, aCsrMap);
140014 if( db->mallocFailed ){
23
Assuming field 'mallocFailed' is 0
24
Taking false branch
140015 pSubitem->pSelect = pSub1;
140016 return 1;
140017 }
140018
140019 /* Defer deleting the Table object associated with the
140020 ** subquery until code generation is
140021 ** complete, since there may still exist Expr.pTab entries that
140022 ** refer to the subquery even after flattening. Ticket #3346.
140023 **
140024 ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
140025 */
140026 if( ALWAYS(pSubitem->pTab!=0)(pSubitem->pTab!=0) ){
25
Assuming field 'pTab' is equal to null
26
Taking false branch
140027 Table *pTabToDel = pSubitem->pTab;
140028 if( pTabToDel->nTabRef==1 ){
140029 Parse *pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
140030 sqlite3ParserAddCleanup(pToplevel,
140031 (void(*)(sqlite3*,void*))sqlite3DeleteTable,
140032 pTabToDel);
140033 testcase( pToplevel->earlyCleanup );
140034 }else{
140035 pTabToDel->nTabRef--;
140036 }
140037 pSubitem->pTab = 0;
140038 }
140039
140040 /* The following loop runs once for each term in a compound-subquery
140041 ** flattening (as described above). If we are doing a different kind
140042 ** of flattening - a flattening other than a compound-subquery flattening -
140043 ** then this loop only runs once.
140044 **
140045 ** This loop moves all of the FROM elements of the subquery into the
140046 ** the FROM clause of the outer query. Before doing this, remember
140047 ** the cursor number for the original outer query FROM element in
140048 ** iParent. The iParent cursor will never be used. Subsequent code
140049 ** will scan expressions looking for iParent references and replace
140050 ** those references with expressions that resolve to the subquery FROM
140051 ** elements we are now copying in.
140052 */
140053 pSub = pSub1;
140054 for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
27
Loop condition is true. Entering loop body
42
Null pointer value stored to 'pSub'
43
Loop condition is true. Entering loop body
140055 int nSubSrc;
140056 u8 jointype = 0;
140057 u8 ltorj = pSrc->a[iFrom].fg.jointype & JT_LTORJ0x40;
140058 assert( pSub!=0 )((void) (0));
140059 pSubSrc = pSub->pSrc; /* FROM clause of subquery */
44
Access to field 'pSrc' results in a dereference of a null pointer (loaded from variable 'pSub')
140060 nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
140061 pSrc = pParent->pSrc; /* FROM clause of the outer query */
140062
140063 if( pParent
27.1
'pParent' is equal to 'p'
==p ){
28
Taking true branch
140064 jointype = pSubitem->fg.jointype; /* First time through the loop */
140065 }
140066
140067 /* The subquery uses a single slot of the FROM clause of the outer
140068 ** query. If the subquery has more than one element in its FROM clause,
140069 ** then expand the outer query to make space for it to hold all elements
140070 ** of the subquery.
140071 **
140072 ** Example:
140073 **
140074 ** SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
140075 **
140076 ** The outer query has 3 slots in its FROM clause. One slot of the
140077 ** outer query (the middle slot) is used by the subquery. The next
140078 ** block of code will expand the outer query FROM clause to 4 slots.
140079 ** The middle slot is expanded to two slots in order to make space
140080 ** for the two elements in the FROM clause of the subquery.
140081 */
140082 if( nSubSrc
28.1
'nSubSrc' is <= 1
>1 ){
29
Taking false branch
140083 pSrc = sqlite3SrcListEnlarge(pParse, pSrc, nSubSrc-1,iFrom+1);
140084 if( pSrc==0 ) break;
140085 pParent->pSrc = pSrc;
140086 }
140087
140088 /* Transfer the FROM clause terms from the subquery into the
140089 ** outer query.
140090 */
140091 for(i=0; i<nSubSrc; i++){
30
Assuming 'i' is >= 'nSubSrc'
31
Loop condition is false. Execution continues on line 140100
140092 SrcItem *pItem = &pSrc->a[i+iFrom];
140093 if( pItem->fg.isUsing ) sqlite3IdListDelete(db, pItem->u3.pUsing);
140094 assert( pItem->fg.isTabFunc==0 )((void) (0));
140095 *pItem = pSubSrc->a[i];
140096 pItem->fg.jointype |= ltorj;
140097 iNewParent = pSubSrc->a[i].iCursor;
140098 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
140099 }
140100 pSrc->a[iFrom].fg.jointype &= JT_LTORJ0x40;
140101 pSrc->a[iFrom].fg.jointype |= jointype | ltorj;
140102
140103 /* Now begin substituting subquery result set expressions for
140104 ** references to the iParent in the outer query.
140105 **
140106 ** Example:
140107 **
140108 ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
140109 ** \ \_____________ subquery __________/ /
140110 ** \_____________________ outer query ______________________________/
140111 **
140112 ** We look at every expression in the outer query and every place we see
140113 ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
140114 */
140115 if( pSub->pOrderBy && (pParent->selFlags & SF_NoopOrderBy0x0400000)==0 ){
32
Assuming field 'pOrderBy' is null
140116 /* At this point, any non-zero iOrderByCol values indicate that the
140117 ** ORDER BY column expression is identical to the iOrderByCol'th
140118 ** expression returned by SELECT statement pSub. Since these values
140119 ** do not necessarily correspond to columns in SELECT statement pParent,
140120 ** zero them before transfering the ORDER BY clause.
140121 **
140122 ** Not doing this may cause an error if a subsequent call to this
140123 ** function attempts to flatten a compound sub-query into pParent
140124 ** (the only way this can happen is if the compound sub-query is
140125 ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
140126 ExprList *pOrderBy = pSub->pOrderBy;
140127 for(i=0; i<pOrderBy->nExpr; i++){
140128 pOrderBy->a[i].u.x.iOrderByCol = 0;
140129 }
140130 assert( pParent->pOrderBy==0 )((void) (0));
140131 pParent->pOrderBy = pOrderBy;
140132 pSub->pOrderBy = 0;
140133 }
140134 pWhere = pSub->pWhere;
140135 pSub->pWhere = 0;
140136 if( isOuterJoin
32.1
'isOuterJoin' is <= 0
>0 ){
33
Taking false branch
140137 sqlite3SetJoinExpr(pWhere, iNewParent, EP_OuterON0x000001);
140138 }
140139 if( pWhere ){
34
Assuming 'pWhere' is non-null
35
Taking true branch
140140 if( pParent->pWhere ){
36
Assuming field 'pWhere' is non-null
37
Taking true branch
140141 pParent->pWhere = sqlite3PExpr(pParse, TK_AND44, pWhere, pParent->pWhere);
140142 }else{
140143 pParent->pWhere = pWhere;
140144 }
140145 }
140146 if( db->mallocFailed==0 ){
38
Assuming field 'mallocFailed' is not equal to 0
39
Taking false branch
140147 SubstContext x;
140148 x.pParse = pParse;
140149 x.iTable = iParent;
140150 x.iNewTable = iNewParent;
140151 x.isOuterJoin = isOuterJoin;
140152 x.pEList = pSub->pEList;
140153 substSelect(&x, pParent, 0);
140154 }
140155
140156 /* The flattened query is a compound if either the inner or the
140157 ** outer query is a compound. */
140158 pParent->selFlags |= pSub->selFlags & SF_Compound0x0000100;
140159 assert( (pSub->selFlags & SF_Distinct)==0 )((void) (0)); /* restriction (17b) */
140160
140161 /*
140162 ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
140163 **
140164 ** One is tempted to try to add a and b to combine the limits. But this
140165 ** does not work if either limit is negative.
140166 */
140167 if( pSub->pLimit
39.1
Field 'pLimit' is null
){
40
Taking false branch
140168 pParent->pLimit = pSub->pLimit;
140169 pSub->pLimit = 0;
140170 }
140171
140172 /* Recompute the SrcList_item.colUsed masks for the flattened
140173 ** tables. */
140174 for(i=0; i
40.1
'i' is >= 'nSubSrc'
<nSubSrc; i++){
41
Loop condition is false. Execution continues on line 140054
140175 recomputeColumnsUsed(pParent, &pSrc->a[i+iFrom]);
140176 }
140177 }
140178
140179 /* Finially, delete what is left of the subquery and return
140180 ** success.
140181 */
140182 sqlite3AggInfoPersistWalkerInit(&w, pParse);
140183 sqlite3WalkSelect(&w,pSub1);
140184 sqlite3SelectDelete(db, pSub1);
140185
140186#if TREETRACE_ENABLED0
140187 if( sqlite3TreeTrace & 0x100 ){
140188 SELECTTRACE(0x100,pParse,p,("After flattening:\n"));
140189 sqlite3TreeViewSelect(0, p, 0);
140190 }
140191#endif
140192
140193 return 1;
140194}
140195#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
140196
140197/*
140198** A structure to keep track of all of the column values that are fixed to
140199** a known value due to WHERE clause constraints of the form COLUMN=VALUE.
140200*/
140201typedef struct WhereConst WhereConst;
140202struct WhereConst {
140203 Parse *pParse; /* Parsing context */
140204 u8 *pOomFault; /* Pointer to pParse->db->mallocFailed */
140205 int nConst; /* Number for COLUMN=CONSTANT terms */
140206 int nChng; /* Number of times a constant is propagated */
140207 int bHasAffBlob; /* At least one column in apExpr[] as affinity BLOB */
140208 u32 mExcludeOn; /* Which ON expressions to exclude from considertion.
140209 ** Either EP_OuterON or EP_InnerON|EP_OuterON */
140210 Expr **apExpr; /* [i*2] is COLUMN and [i*2+1] is VALUE */
140211};
140212
140213/*
140214** Add a new entry to the pConst object. Except, do not add duplicate
140215** pColumn entires. Also, do not add if doing so would not be appropriate.
140216**
140217** The caller guarantees the pColumn is a column and pValue is a constant.
140218** This routine has to do some additional checks before completing the
140219** insert.
140220*/
140221static void constInsert(
140222 WhereConst *pConst, /* The WhereConst into which we are inserting */
140223 Expr *pColumn, /* The COLUMN part of the constraint */
140224 Expr *pValue, /* The VALUE part of the constraint */
140225 Expr *pExpr /* Overall expression: COLUMN=VALUE or VALUE=COLUMN */
140226){
140227 int i;
140228 assert( pColumn->op==TK_COLUMN )((void) (0));
140229 assert( sqlite3ExprIsConstant(pValue) )((void) (0));
140230
140231 if( ExprHasProperty(pColumn, EP_FixedCol)(((pColumn)->flags&(0x000020))!=0) ) return;
140232 if( sqlite3ExprAffinity(pValue)!=0 ) return;
140233 if( !sqlite3IsBinary(sqlite3ExprCompareCollSeq(pConst->pParse,pExpr)) ){
140234 return;
140235 }
140236
140237 /* 2018-10-25 ticket [cf5ed20f]
140238 ** Make sure the same pColumn is not inserted more than once */
140239 for(i=0; i<pConst->nConst; i++){
140240 const Expr *pE2 = pConst->apExpr[i*2];
140241 assert( pE2->op==TK_COLUMN )((void) (0));
140242 if( pE2->iTable==pColumn->iTable
140243 && pE2->iColumn==pColumn->iColumn
140244 ){
140245 return; /* Already present. Return without doing anything. */
140246 }
140247 }
140248 if( sqlite3ExprAffinity(pColumn)==SQLITE_AFF_BLOB0x41 ){
140249 pConst->bHasAffBlob = 1;
140250 }
140251
140252 pConst->nConst++;
140253 pConst->apExpr = sqlite3DbReallocOrFree(pConst->pParse->db, pConst->apExpr,
140254 pConst->nConst*2*sizeof(Expr*));
140255 if( pConst->apExpr==0 ){
140256 pConst->nConst = 0;
140257 }else{
140258 pConst->apExpr[pConst->nConst*2-2] = pColumn;
140259 pConst->apExpr[pConst->nConst*2-1] = pValue;
140260 }
140261}
140262
140263/*
140264** Find all terms of COLUMN=VALUE or VALUE=COLUMN in pExpr where VALUE
140265** is a constant expression and where the term must be true because it
140266** is part of the AND-connected terms of the expression. For each term
140267** found, add it to the pConst structure.
140268*/
140269static void findConstInWhere(WhereConst *pConst, Expr *pExpr){
140270 Expr *pRight, *pLeft;
140271 if( NEVER(pExpr==0)(pExpr==0) ) return;
140272 if( ExprHasProperty(pExpr, pConst->mExcludeOn)(((pExpr)->flags&(pConst->mExcludeOn))!=0) ){
140273 testcase( ExprHasProperty(pExpr, EP_OuterON) );
140274 testcase( ExprHasProperty(pExpr, EP_InnerON) );
140275 return;
140276 }
140277 if( pExpr->op==TK_AND44 ){
140278 findConstInWhere(pConst, pExpr->pRight);
140279 findConstInWhere(pConst, pExpr->pLeft);
140280 return;
140281 }
140282 if( pExpr->op!=TK_EQ53 ) return;
140283 pRight = pExpr->pRight;
140284 pLeft = pExpr->pLeft;
140285 assert( pRight!=0 )((void) (0));
140286 assert( pLeft!=0 )((void) (0));
140287 if( pRight->op==TK_COLUMN167 && sqlite3ExprIsConstant(pLeft) ){
140288 constInsert(pConst,pRight,pLeft,pExpr);
140289 }
140290 if( pLeft->op==TK_COLUMN167 && sqlite3ExprIsConstant(pRight) ){
140291 constInsert(pConst,pLeft,pRight,pExpr);
140292 }
140293}
140294
140295/*
140296** This is a helper function for Walker callback propagateConstantExprRewrite().
140297**
140298** Argument pExpr is a candidate expression to be replaced by a value. If
140299** pExpr is equivalent to one of the columns named in pWalker->u.pConst,
140300** then overwrite it with the corresponding value. Except, do not do so
140301** if argument bIgnoreAffBlob is non-zero and the affinity of pExpr
140302** is SQLITE_AFF_BLOB.
140303*/
140304static int propagateConstantExprRewriteOne(
140305 WhereConst *pConst,
140306 Expr *pExpr,
140307 int bIgnoreAffBlob
140308){
140309 int i;
140310 if( pConst->pOomFault[0] ) return WRC_Prune1;
140311 if( pExpr->op!=TK_COLUMN167 ) return WRC_Continue0;
140312 if( ExprHasProperty(pExpr, EP_FixedCol|pConst->mExcludeOn)(((pExpr)->flags&(0x000020|pConst->mExcludeOn))!=0) ){
140313 testcase( ExprHasProperty(pExpr, EP_FixedCol) );
140314 testcase( ExprHasProperty(pExpr, EP_OuterON) );
140315 testcase( ExprHasProperty(pExpr, EP_InnerON) );
140316 return WRC_Continue0;
140317 }
140318 for(i=0; i<pConst->nConst; i++){
140319 Expr *pColumn = pConst->apExpr[i*2];
140320 if( pColumn==pExpr ) continue;
140321 if( pColumn->iTable!=pExpr->iTable ) continue;
140322 if( pColumn->iColumn!=pExpr->iColumn ) continue;
140323 if( bIgnoreAffBlob && sqlite3ExprAffinity(pColumn)==SQLITE_AFF_BLOB0x41 ){
140324 break;
140325 }
140326 /* A match is found. Add the EP_FixedCol property */
140327 pConst->nChng++;
140328 ExprClearProperty(pExpr, EP_Leaf)(pExpr)->flags&=~(0x800000);
140329 ExprSetProperty(pExpr, EP_FixedCol)(pExpr)->flags|=(0x000020);
140330 assert( pExpr->pLeft==0 )((void) (0));
140331 pExpr->pLeft = sqlite3ExprDup(pConst->pParse->db, pConst->apExpr[i*2+1], 0);
140332 if( pConst->pParse->db->mallocFailed ) return WRC_Prune1;
140333 break;
140334 }
140335 return WRC_Prune1;
140336}
140337
140338/*
140339** This is a Walker expression callback. pExpr is a node from the WHERE
140340** clause of a SELECT statement. This function examines pExpr to see if
140341** any substitutions based on the contents of pWalker->u.pConst should
140342** be made to pExpr or its immediate children.
140343**
140344** A substitution is made if:
140345**
140346** + pExpr is a column with an affinity other than BLOB that matches
140347** one of the columns in pWalker->u.pConst, or
140348**
140349** + pExpr is a binary comparison operator (=, <=, >=, <, >) that
140350** uses an affinity other than TEXT and one of its immediate
140351** children is a column that matches one of the columns in
140352** pWalker->u.pConst.
140353*/
140354static int propagateConstantExprRewrite(Walker *pWalker, Expr *pExpr){
140355 WhereConst *pConst = pWalker->u.pConst;
140356 assert( TK_GT==TK_EQ+1 )((void) (0));
140357 assert( TK_LE==TK_EQ+2 )((void) (0));
140358 assert( TK_LT==TK_EQ+3 )((void) (0));
140359 assert( TK_GE==TK_EQ+4 )((void) (0));
140360 if( pConst->bHasAffBlob ){
140361 if( (pExpr->op>=TK_EQ53 && pExpr->op<=TK_GE57)
140362 || pExpr->op==TK_IS45
140363 ){
140364 propagateConstantExprRewriteOne(pConst, pExpr->pLeft, 0);
140365 if( pConst->pOomFault[0] ) return WRC_Prune1;
140366 if( sqlite3ExprAffinity(pExpr->pLeft)!=SQLITE_AFF_TEXT0x42 ){
140367 propagateConstantExprRewriteOne(pConst, pExpr->pRight, 0);
140368 }
140369 }
140370 }
140371 return propagateConstantExprRewriteOne(pConst, pExpr, pConst->bHasAffBlob);
140372}
140373
140374/*
140375** The WHERE-clause constant propagation optimization.
140376**
140377** If the WHERE clause contains terms of the form COLUMN=CONSTANT or
140378** CONSTANT=COLUMN that are top-level AND-connected terms that are not
140379** part of a ON clause from a LEFT JOIN, then throughout the query
140380** replace all other occurrences of COLUMN with CONSTANT.
140381**
140382** For example, the query:
140383**
140384** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=t1.a AND t3.c=t2.b
140385**
140386** Is transformed into
140387**
140388** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=39 AND t3.c=39
140389**
140390** Return true if any transformations where made and false if not.
140391**
140392** Implementation note: Constant propagation is tricky due to affinity
140393** and collating sequence interactions. Consider this example:
140394**
140395** CREATE TABLE t1(a INT,b TEXT);
140396** INSERT INTO t1 VALUES(123,'0123');
140397** SELECT * FROM t1 WHERE a=123 AND b=a;
140398** SELECT * FROM t1 WHERE a=123 AND b=123;
140399**
140400** The two SELECT statements above should return different answers. b=a
140401** is alway true because the comparison uses numeric affinity, but b=123
140402** is false because it uses text affinity and '0123' is not the same as '123'.
140403** To work around this, the expression tree is not actually changed from
140404** "b=a" to "b=123" but rather the "a" in "b=a" is tagged with EP_FixedCol
140405** and the "123" value is hung off of the pLeft pointer. Code generator
140406** routines know to generate the constant "123" instead of looking up the
140407** column value. Also, to avoid collation problems, this optimization is
140408** only attempted if the "a=123" term uses the default BINARY collation.
140409**
140410** 2021-05-25 forum post 6a06202608: Another troublesome case is...
140411**
140412** CREATE TABLE t1(x);
140413** INSERT INTO t1 VALUES(10.0);
140414** SELECT 1 FROM t1 WHERE x=10 AND x LIKE 10;
140415**
140416** The query should return no rows, because the t1.x value is '10.0' not '10'
140417** and '10.0' is not LIKE '10'. But if we are not careful, the first WHERE
140418** term "x=10" will cause the second WHERE term to become "10 LIKE 10",
140419** resulting in a false positive. To avoid this, constant propagation for
140420** columns with BLOB affinity is only allowed if the constant is used with
140421** operators ==, <=, <, >=, >, or IS in a way that will cause the correct
140422** type conversions to occur. See logic associated with the bHasAffBlob flag
140423** for details.
140424*/
140425static int propagateConstants(
140426 Parse *pParse, /* The parsing context */
140427 Select *p /* The query in which to propagate constants */
140428){
140429 WhereConst x;
140430 Walker w;
140431 int nChng = 0;
140432 x.pParse = pParse;
140433 x.pOomFault = &pParse->db->mallocFailed;
140434 do{
140435 x.nConst = 0;
140436 x.nChng = 0;
140437 x.apExpr = 0;
140438 x.bHasAffBlob = 0;
140439 if( ALWAYS(p->pSrc!=0)(p->pSrc!=0)
140440 && p->pSrc->nSrc>0
140441 && (p->pSrc->a[0].fg.jointype & JT_LTORJ0x40)!=0
140442 ){
140443 /* Do not propagate constants on any ON clause if there is a
140444 ** RIGHT JOIN anywhere in the query */
140445 x.mExcludeOn = EP_InnerON0x000002 | EP_OuterON0x000001;
140446 }else{
140447 /* Do not propagate constants through the ON clause of a LEFT JOIN */
140448 x.mExcludeOn = EP_OuterON0x000001;
140449 }
140450 findConstInWhere(&x, p->pWhere);
140451 if( x.nConst ){
140452 memset(&w, 0, sizeof(w));
140453 w.pParse = pParse;
140454 w.xExprCallback = propagateConstantExprRewrite;
140455 w.xSelectCallback = sqlite3SelectWalkNoop;
140456 w.xSelectCallback2 = 0;
140457 w.walkerDepth = 0;
140458 w.u.pConst = &x;
140459 sqlite3WalkExpr(&w, p->pWhere);
140460 sqlite3DbFree(x.pParse->db, x.apExpr);
140461 nChng += x.nChng;
140462 }
140463 }while( x.nChng );
140464 return nChng;
140465}
140466
140467#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
140468# if !defined(SQLITE_OMIT_WINDOWFUNC)
140469/*
140470** This function is called to determine whether or not it is safe to
140471** push WHERE clause expression pExpr down to FROM clause sub-query
140472** pSubq, which contains at least one window function. Return 1
140473** if it is safe and the expression should be pushed down, or 0
140474** otherwise.
140475**
140476** It is only safe to push the expression down if it consists only
140477** of constants and copies of expressions that appear in the PARTITION
140478** BY clause of all window function used by the sub-query. It is safe
140479** to filter out entire partitions, but not rows within partitions, as
140480** this may change the results of the window functions.
140481**
140482** At the time this function is called it is guaranteed that
140483**
140484** * the sub-query uses only one distinct window frame, and
140485** * that the window frame has a PARTITION BY clase.
140486*/
140487static int pushDownWindowCheck(Parse *pParse, Select *pSubq, Expr *pExpr){
140488 assert( pSubq->pWin->pPartition )((void) (0));
140489 assert( (pSubq->selFlags & SF_MultiPart)==0 )((void) (0));
140490 assert( pSubq->pPrior==0 )((void) (0));
140491 return sqlite3ExprIsConstantOrGroupBy(pParse, pExpr, pSubq->pWin->pPartition);
140492}
140493# endif /* SQLITE_OMIT_WINDOWFUNC */
140494#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
140495
140496#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
140497/*
140498** Make copies of relevant WHERE clause terms of the outer query into
140499** the WHERE clause of subquery. Example:
140500**
140501** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
140502**
140503** Transformed into:
140504**
140505** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
140506** WHERE x=5 AND y=10;
140507**
140508** The hope is that the terms added to the inner query will make it more
140509** efficient.
140510**
140511** Do not attempt this optimization if:
140512**
140513** (1) (** This restriction was removed on 2017-09-29. We used to
140514** disallow this optimization for aggregate subqueries, but now
140515** it is allowed by putting the extra terms on the HAVING clause.
140516** The added HAVING clause is pointless if the subquery lacks
140517** a GROUP BY clause. But such a HAVING clause is also harmless
140518** so there does not appear to be any reason to add extra logic
140519** to suppress it. **)
140520**
140521** (2) The inner query is the recursive part of a common table expression.
140522**
140523** (3) The inner query has a LIMIT clause (since the changes to the WHERE
140524** clause would change the meaning of the LIMIT).
140525**
140526** (4) The inner query is the right operand of a LEFT JOIN and the
140527** expression to be pushed down does not come from the ON clause
140528** on that LEFT JOIN.
140529**
140530** (5) The WHERE clause expression originates in the ON or USING clause
140531** of a LEFT JOIN where iCursor is not the right-hand table of that
140532** left join. An example:
140533**
140534** SELECT *
140535** FROM (SELECT 1 AS a1 UNION ALL SELECT 2) AS aa
140536** JOIN (SELECT 1 AS b2 UNION ALL SELECT 2) AS bb ON (a1=b2)
140537** LEFT JOIN (SELECT 8 AS c3 UNION ALL SELECT 9) AS cc ON (b2=2);
140538**
140539** The correct answer is three rows: (1,1,NULL),(2,2,8),(2,2,9).
140540** But if the (b2=2) term were to be pushed down into the bb subquery,
140541** then the (1,1,NULL) row would be suppressed.
140542**
140543** (6) Window functions make things tricky as changes to the WHERE clause
140544** of the inner query could change the window over which window
140545** functions are calculated. Therefore, do not attempt the optimization
140546** if:
140547**
140548** (6a) The inner query uses multiple incompatible window partitions.
140549**
140550** (6b) The inner query is a compound and uses window-functions.
140551**
140552** (6c) The WHERE clause does not consist entirely of constants and
140553** copies of expressions found in the PARTITION BY clause of
140554** all window-functions used by the sub-query. It is safe to
140555** filter out entire partitions, as this does not change the
140556** window over which any window-function is calculated.
140557**
140558** (7) The inner query is a Common Table Expression (CTE) that should
140559** be materialized. (This restriction is implemented in the calling
140560** routine.)
140561**
140562** Return 0 if no changes are made and non-zero if one or more WHERE clause
140563** terms are duplicated into the subquery.
140564*/
140565static int pushDownWhereTerms(
140566 Parse *pParse, /* Parse context (for malloc() and error reporting) */
140567 Select *pSubq, /* The subquery whose WHERE clause is to be augmented */
140568 Expr *pWhere, /* The WHERE clause of the outer query */
140569 SrcItem *pSrc /* The subquery term of the outer FROM clause */
140570){
140571 Expr *pNew;
140572 int nChng = 0;
140573 if( pWhere==0 ) return 0;
140574 if( pSubq->selFlags & (SF_Recursive0x0002000|SF_MultiPart0x2000000) ) return 0;
140575 if( pSrc->fg.jointype & (JT_LTORJ0x40|JT_RIGHT0x10) ) return 0;
140576
140577#ifndef SQLITE_OMIT_WINDOWFUNC
140578 if( pSubq->pPrior ){
140579 Select *pSel;
140580 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
140581 if( pSel->pWin ) return 0; /* restriction (6b) */
140582 }
140583 }else{
140584 if( pSubq->pWin && pSubq->pWin->pPartition==0 ) return 0;
140585 }
140586#endif
140587
140588#ifdef SQLITE_DEBUG
140589 /* Only the first term of a compound can have a WITH clause. But make
140590 ** sure no other terms are marked SF_Recursive in case something changes
140591 ** in the future.
140592 */
140593 {
140594 Select *pX;
140595 for(pX=pSubq; pX; pX=pX->pPrior){
140596 assert( (pX->selFlags & (SF_Recursive))==0 )((void) (0));
140597 }
140598 }
140599#endif
140600
140601 if( pSubq->pLimit!=0 ){
140602 return 0; /* restriction (3) */
140603 }
140604 while( pWhere->op==TK_AND44 ){
140605 nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight, pSrc);
140606 pWhere = pWhere->pLeft;
140607 }
140608
140609#if 0 /* Legacy code. Checks now done by sqlite3ExprIsTableConstraint() */
140610 if( isLeftJoin
140611 && (ExprHasProperty(pWhere,EP_OuterON)(((pWhere)->flags&(0x000001))!=0)==0
140612 || pWhere->w.iJoin!=iCursor)
140613 ){
140614 return 0; /* restriction (4) */
140615 }
140616 if( ExprHasProperty(pWhere,EP_OuterON)(((pWhere)->flags&(0x000001))!=0)
140617 && pWhere->w.iJoin!=iCursor
140618 ){
140619 return 0; /* restriction (5) */
140620 }
140621#endif
140622
140623 if( sqlite3ExprIsTableConstraint(pWhere, pSrc) ){
140624 nChng++;
140625 pSubq->selFlags |= SF_PushDown0x1000000;
140626 while( pSubq ){
140627 SubstContext x;
140628 pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
140629 unsetJoinExpr(pNew, -1, 1);
140630 x.pParse = pParse;
140631 x.iTable = pSrc->iCursor;
140632 x.iNewTable = pSrc->iCursor;
140633 x.isOuterJoin = 0;
140634 x.pEList = pSubq->pEList;
140635 pNew = substExpr(&x, pNew);
140636#ifndef SQLITE_OMIT_WINDOWFUNC
140637 if( pSubq->pWin && 0==pushDownWindowCheck(pParse, pSubq, pNew) ){
140638 /* Restriction 6c has prevented push-down in this case */
140639 sqlite3ExprDelete(pParse->db, pNew);
140640 nChng--;
140641 break;
140642 }
140643#endif
140644 if( pSubq->selFlags & SF_Aggregate0x0000008 ){
140645 pSubq->pHaving = sqlite3ExprAnd(pParse, pSubq->pHaving, pNew);
140646 }else{
140647 pSubq->pWhere = sqlite3ExprAnd(pParse, pSubq->pWhere, pNew);
140648 }
140649 pSubq = pSubq->pPrior;
140650 }
140651 }
140652 return nChng;
140653}
140654#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
140655
140656/*
140657** The pFunc is the only aggregate function in the query. Check to see
140658** if the query is a candidate for the min/max optimization.
140659**
140660** If the query is a candidate for the min/max optimization, then set
140661** *ppMinMax to be an ORDER BY clause to be used for the optimization
140662** and return either WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX depending on
140663** whether pFunc is a min() or max() function.
140664**
140665** If the query is not a candidate for the min/max optimization, return
140666** WHERE_ORDERBY_NORMAL (which must be zero).
140667**
140668** This routine must be called after aggregate functions have been
140669** located but before their arguments have been subjected to aggregate
140670** analysis.
140671*/
140672static u8 minMaxQuery(sqlite3 *db, Expr *pFunc, ExprList **ppMinMax){
140673 int eRet = WHERE_ORDERBY_NORMAL0x0000; /* Return value */
140674 ExprList *pEList; /* Arguments to agg function */
140675 const char *zFunc; /* Name of aggregate function pFunc */
140676 ExprList *pOrderBy;
140677 u8 sortFlags = 0;
140678
140679 assert( *ppMinMax==0 )((void) (0));
140680 assert( pFunc->op==TK_AGG_FUNCTION )((void) (0));
140681 assert( !IsWindowFunc(pFunc) )((void) (0));
140682 assert( ExprUseXList(pFunc) )((void) (0));
140683 pEList = pFunc->x.pList;
140684 if( pEList==0
140685 || pEList->nExpr!=1
140686 || ExprHasProperty(pFunc, EP_WinFunc)(((pFunc)->flags&(0x1000000))!=0)
140687 || OptimizationDisabled(db, SQLITE_MinMaxOpt)(((db)->dbOptFlags&(0x00010000))!=0)
140688 ){
140689 return eRet;
140690 }
140691 assert( !ExprHasProperty(pFunc, EP_IntValue) )((void) (0));
140692 zFunc = pFunc->u.zToken;
140693 if( sqlite3StrICmp(zFunc, "min")==0 ){
140694 eRet = WHERE_ORDERBY_MIN0x0001;
140695 if( sqlite3ExprCanBeNull(pEList->a[0].pExpr) ){
140696 sortFlags = KEYINFO_ORDER_BIGNULL0x02;
140697 }
140698 }else if( sqlite3StrICmp(zFunc, "max")==0 ){
140699 eRet = WHERE_ORDERBY_MAX0x0002;
140700 sortFlags = KEYINFO_ORDER_DESC0x01;
140701 }else{
140702 return eRet;
140703 }
140704 *ppMinMax = pOrderBy = sqlite3ExprListDup(db, pEList, 0);
140705 assert( pOrderBy!=0 || db->mallocFailed )((void) (0));
140706 if( pOrderBy ) pOrderBy->a[0].fg.sortFlags = sortFlags;
140707 return eRet;
140708}
140709
140710/*
140711** The select statement passed as the first argument is an aggregate query.
140712** The second argument is the associated aggregate-info object. This
140713** function tests if the SELECT is of the form:
140714**
140715** SELECT count(*) FROM <tbl>
140716**
140717** where table is a database table, not a sub-select or view. If the query
140718** does match this pattern, then a pointer to the Table object representing
140719** <tbl> is returned. Otherwise, NULL is returned.
140720**
140721** This routine checks to see if it is safe to use the count optimization.
140722** A correct answer is still obtained (though perhaps more slowly) if
140723** this routine returns NULL when it could have returned a table pointer.
140724** But returning the pointer when NULL should have been returned can
140725** result in incorrect answers and/or crashes. So, when in doubt, return NULL.
140726*/
140727static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
140728 Table *pTab;
140729 Expr *pExpr;
140730
140731 assert( !p->pGroupBy )((void) (0));
140732
140733 if( p->pWhere
140734 || p->pEList->nExpr!=1
140735 || p->pSrc->nSrc!=1
140736 || p->pSrc->a[0].pSelect
140737 || pAggInfo->nFunc!=1
140738 ){
140739 return 0;
140740 }
140741 pTab = p->pSrc->a[0].pTab;
140742 assert( pTab!=0 )((void) (0));
140743 assert( !IsView(pTab) )((void) (0));
140744 if( !IsOrdinaryTable(pTab)((pTab)->eTabType==0) ) return 0;
140745 pExpr = p->pEList->a[0].pExpr;
140746 assert( pExpr!=0 )((void) (0));
140747 if( pExpr->op!=TK_AGG_FUNCTION168 ) return 0;
140748 if( pExpr->pAggInfo!=pAggInfo ) return 0;
140749 if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT0x0100)==0 ) return 0;
140750 assert( pAggInfo->aFunc[0].pFExpr==pExpr )((void) (0));
140751 testcase( ExprHasProperty(pExpr, EP_Distinct) );
140752 testcase( ExprHasProperty(pExpr, EP_WinFunc) );
140753 if( ExprHasProperty(pExpr, EP_Distinct|EP_WinFunc)(((pExpr)->flags&(0x000004|0x1000000))!=0) ) return 0;
140754
140755 return pTab;
140756}
140757
140758/*
140759** If the source-list item passed as an argument was augmented with an
140760** INDEXED BY clause, then try to locate the specified index. If there
140761** was such a clause and the named index cannot be found, return
140762** SQLITE_ERROR and leave an error in pParse. Otherwise, populate
140763** pFrom->pIndex and return SQLITE_OK.
140764*/
140765SQLITE_PRIVATEstatic int sqlite3IndexedByLookup(Parse *pParse, SrcItem *pFrom){
140766 Table *pTab = pFrom->pTab;
140767 char *zIndexedBy = pFrom->u1.zIndexedBy;
140768 Index *pIdx;
140769 assert( pTab!=0 )((void) (0));
140770 assert( pFrom->fg.isIndexedBy!=0 )((void) (0));
140771
140772 for(pIdx=pTab->pIndex;
140773 pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy);
140774 pIdx=pIdx->pNext
140775 );
140776 if( !pIdx ){
140777 sqlite3ErrorMsg(pParse, "no such index: %s", zIndexedBy, 0);
140778 pParse->checkSchema = 1;
140779 return SQLITE_ERROR1;
140780 }
140781 assert( pFrom->fg.isCte==0 )((void) (0));
140782 pFrom->u2.pIBIndex = pIdx;
140783 return SQLITE_OK0;
140784}
140785
140786/*
140787** Detect compound SELECT statements that use an ORDER BY clause with
140788** an alternative collating sequence.
140789**
140790** SELECT ... FROM t1 EXCEPT SELECT ... FROM t2 ORDER BY .. COLLATE ...
140791**
140792** These are rewritten as a subquery:
140793**
140794** SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2)
140795** ORDER BY ... COLLATE ...
140796**
140797** This transformation is necessary because the multiSelectOrderBy() routine
140798** above that generates the code for a compound SELECT with an ORDER BY clause
140799** uses a merge algorithm that requires the same collating sequence on the
140800** result columns as on the ORDER BY clause. See ticket
140801** http://www.sqlite.org/src/info/6709574d2a
140802**
140803** This transformation is only needed for EXCEPT, INTERSECT, and UNION.
140804** The UNION ALL operator works fine with multiSelectOrderBy() even when
140805** there are COLLATE terms in the ORDER BY.
140806*/
140807static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){
140808 int i;
140809 Select *pNew;
140810 Select *pX;
140811 sqlite3 *db;
140812 struct ExprList_item *a;
140813 SrcList *pNewSrc;
140814 Parse *pParse;
140815 Token dummy;
140816
140817 if( p->pPrior==0 ) return WRC_Continue0;
140818 if( p->pOrderBy==0 ) return WRC_Continue0;
140819 for(pX=p; pX && (pX->op==TK_ALL135 || pX->op==TK_SELECT138); pX=pX->pPrior){}
140820 if( pX==0 ) return WRC_Continue0;
140821 a = p->pOrderBy->a;
140822#ifndef SQLITE_OMIT_WINDOWFUNC
140823 /* If iOrderByCol is already non-zero, then it has already been matched
140824 ** to a result column of the SELECT statement. This occurs when the
140825 ** SELECT is rewritten for window-functions processing and then passed
140826 ** to sqlite3SelectPrep() and similar a second time. The rewriting done
140827 ** by this function is not required in this case. */
140828 if( a[0].u.x.iOrderByCol ) return WRC_Continue0;
140829#endif
140830 for(i=p->pOrderBy->nExpr-1; i>=0; i--){
140831 if( a[i].pExpr->flags & EP_Collate0x000200 ) break;
140832 }
140833 if( i<0 ) return WRC_Continue0;
140834
140835 /* If we reach this point, that means the transformation is required. */
140836
140837 pParse = pWalker->pParse;
140838 db = pParse->db;
140839 pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
140840 if( pNew==0 ) return WRC_Abort2;
140841 memset(&dummy, 0, sizeof(dummy));
140842 pNewSrc = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&dummy,pNew,0);
140843 if( pNewSrc==0 ) return WRC_Abort2;
140844 *pNew = *p;
140845 p->pSrc = pNewSrc;
140846 p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK180, 0));
140847 p->op = TK_SELECT138;
140848 p->pWhere = 0;
140849 pNew->pGroupBy = 0;
140850 pNew->pHaving = 0;
140851 pNew->pOrderBy = 0;
140852 p->pPrior = 0;
140853 p->pNext = 0;
140854 p->pWith = 0;
140855#ifndef SQLITE_OMIT_WINDOWFUNC
140856 p->pWinDefn = 0;
140857#endif
140858 p->selFlags &= ~SF_Compound0x0000100;
140859 assert( (p->selFlags & SF_Converted)==0 )((void) (0));
140860 p->selFlags |= SF_Converted0x0010000;
140861 assert( pNew->pPrior!=0 )((void) (0));
140862 pNew->pPrior->pNext = pNew;
140863 pNew->pLimit = 0;
140864 return WRC_Continue0;
140865}
140866
140867/*
140868** Check to see if the FROM clause term pFrom has table-valued function
140869** arguments. If it does, leave an error message in pParse and return
140870** non-zero, since pFrom is not allowed to be a table-valued function.
140871*/
140872static int cannotBeFunction(Parse *pParse, SrcItem *pFrom){
140873 if( pFrom->fg.isTabFunc ){
140874 sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName);
140875 return 1;
140876 }
140877 return 0;
140878}
140879
140880#ifndef SQLITE_OMIT_CTE
140881/*
140882** Argument pWith (which may be NULL) points to a linked list of nested
140883** WITH contexts, from inner to outermost. If the table identified by
140884** FROM clause element pItem is really a common-table-expression (CTE)
140885** then return a pointer to the CTE definition for that table. Otherwise
140886** return NULL.
140887**
140888** If a non-NULL value is returned, set *ppContext to point to the With
140889** object that the returned CTE belongs to.
140890*/
140891static struct Cte *searchWith(
140892 With *pWith, /* Current innermost WITH clause */
140893 SrcItem *pItem, /* FROM clause element to resolve */
140894 With **ppContext /* OUT: WITH clause return value belongs to */
140895){
140896 const char *zName = pItem->zName;
140897 With *p;
140898 assert( pItem->zDatabase==0 )((void) (0));
140899 assert( zName!=0 )((void) (0));
140900 for(p=pWith; p; p=p->pOuter){
140901 int i;
140902 for(i=0; i<p->nCte; i++){
140903 if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
140904 *ppContext = p;
140905 return &p->a[i];
140906 }
140907 }
140908 if( p->bView ) break;
140909 }
140910 return 0;
140911}
140912
140913/* The code generator maintains a stack of active WITH clauses
140914** with the inner-most WITH clause being at the top of the stack.
140915**
140916** This routine pushes the WITH clause passed as the second argument
140917** onto the top of the stack. If argument bFree is true, then this
140918** WITH clause will never be popped from the stack but should instead
140919** be freed along with the Parse object. In other cases, when
140920** bFree==0, the With object will be freed along with the SELECT
140921** statement with which it is associated.
140922**
140923** This routine returns a copy of pWith. Or, if bFree is true and
140924** the pWith object is destroyed immediately due to an OOM condition,
140925** then this routine return NULL.
140926**
140927** If bFree is true, do not continue to use the pWith pointer after
140928** calling this routine, Instead, use only the return value.
140929*/
140930SQLITE_PRIVATEstatic With *sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){
140931 if( pWith ){
140932 if( bFree ){
140933 pWith = (With*)sqlite3ParserAddCleanup(pParse,
140934 (void(*)(sqlite3*,void*))sqlite3WithDelete,
140935 pWith);
140936 if( pWith==0 ) return 0;
140937 }
140938 if( pParse->nErr==0 ){
140939 assert( pParse->pWith!=pWith )((void) (0));
140940 pWith->pOuter = pParse->pWith;
140941 pParse->pWith = pWith;
140942 }
140943 }
140944 return pWith;
140945}
140946
140947/*
140948** This function checks if argument pFrom refers to a CTE declared by
140949** a WITH clause on the stack currently maintained by the parser (on the
140950** pParse->pWith linked list). And if currently processing a CTE
140951** CTE expression, through routine checks to see if the reference is
140952** a recursive reference to the CTE.
140953**
140954** If pFrom matches a CTE according to either of these two above, pFrom->pTab
140955** and other fields are populated accordingly.
140956**
140957** Return 0 if no match is found.
140958** Return 1 if a match is found.
140959** Return 2 if an error condition is detected.
140960*/
140961static int resolveFromTermToCte(
140962 Parse *pParse, /* The parsing context */
140963 Walker *pWalker, /* Current tree walker */
140964 SrcItem *pFrom /* The FROM clause term to check */
140965){
140966 Cte *pCte; /* Matched CTE (or NULL if no match) */
140967 With *pWith; /* The matching WITH */
140968
140969 assert( pFrom->pTab==0 )((void) (0));
140970 if( pParse->pWith==0 ){
140971 /* There are no WITH clauses in the stack. No match is possible */
140972 return 0;
140973 }
140974 if( pParse->nErr ){
140975 /* Prior errors might have left pParse->pWith in a goofy state, so
140976 ** go no further. */
140977 return 0;
140978 }
140979 if( pFrom->zDatabase!=0 ){
140980 /* The FROM term contains a schema qualifier (ex: main.t1) and so
140981 ** it cannot possibly be a CTE reference. */
140982 return 0;
140983 }
140984 if( pFrom->fg.notCte ){
140985 /* The FROM term is specifically excluded from matching a CTE.
140986 ** (1) It is part of a trigger that used to have zDatabase but had
140987 ** zDatabase removed by sqlite3FixTriggerStep().
140988 ** (2) This is the first term in the FROM clause of an UPDATE.
140989 */
140990 return 0;
140991 }
140992 pCte = searchWith(pParse->pWith, pFrom, &pWith);
140993 if( pCte ){
140994 sqlite3 *db = pParse->db;
140995 Table *pTab;
140996 ExprList *pEList;
140997 Select *pSel;
140998 Select *pLeft; /* Left-most SELECT statement */
140999 Select *pRecTerm; /* Left-most recursive term */
141000 int bMayRecursive; /* True if compound joined by UNION [ALL] */
141001 With *pSavedWith; /* Initial value of pParse->pWith */
141002 int iRecTab = -1; /* Cursor for recursive table */
141003 CteUse *pCteUse;
141004
141005 /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
141006 ** recursive reference to CTE pCte. Leave an error in pParse and return
141007 ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
141008 ** In this case, proceed. */
141009 if( pCte->zCteErr ){
141010 sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName);
141011 return 2;
141012 }
141013 if( cannotBeFunction(pParse, pFrom) ) return 2;
141014
141015 assert( pFrom->pTab==0 )((void) (0));
141016 pTab = sqlite3DbMallocZero(db, sizeof(Table));
141017 if( pTab==0 ) return 2;
141018 pCteUse = pCte->pUse;
141019 if( pCteUse==0 ){
141020 pCte->pUse = pCteUse = sqlite3DbMallocZero(db, sizeof(pCteUse[0]));
141021 if( pCteUse==0
141022 || sqlite3ParserAddCleanup(pParse,sqlite3DbFree,pCteUse)==0
141023 ){
141024 sqlite3DbFree(db, pTab);
141025 return 2;
141026 }
141027 pCteUse->eM10d = pCte->eM10d;
141028 }
141029 pFrom->pTab = pTab;
141030 pTab->nTabRef = 1;
141031 pTab->zName = sqlite3DbStrDup(db, pCte->zName);
141032 pTab->iPKey = -1;
141033 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) )((void) (0));
141034 pTab->tabFlags |= TF_Ephemeral0x00004000 | TF_NoVisibleRowid0x00000200;
141035 pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
141036 if( db->mallocFailed ) return 2;
141037 pFrom->pSelect->selFlags |= SF_CopyCte0x4000000;
141038 assert( pFrom->pSelect )((void) (0));
141039 if( pFrom->fg.isIndexedBy ){
141040 sqlite3ErrorMsg(pParse, "no such index: \"%s\"", pFrom->u1.zIndexedBy);
141041 return 2;
141042 }
141043 pFrom->fg.isCte = 1;
141044 pFrom->u2.pCteUse = pCteUse;
141045 pCteUse->nUse++;
141046 if( pCteUse->nUse>=2 && pCteUse->eM10d==M10d_Any1 ){
141047 pCteUse->eM10d = M10d_Yes0;
141048 }
141049
141050 /* Check if this is a recursive CTE. */
141051 pRecTerm = pSel = pFrom->pSelect;
141052 bMayRecursive = ( pSel->op==TK_ALL135 || pSel->op==TK_UNION134 );
141053 while( bMayRecursive && pRecTerm->op==pSel->op ){
141054 int i;
141055 SrcList *pSrc = pRecTerm->pSrc;
141056 assert( pRecTerm->pPrior!=0 )((void) (0));
141057 for(i=0; i<pSrc->nSrc; i++){
141058 SrcItem *pItem = &pSrc->a[i];
141059 if( pItem->zDatabase==0
141060 && pItem->zName!=0
141061 && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
141062 ){
141063 pItem->pTab = pTab;
141064 pTab->nTabRef++;
141065 pItem->fg.isRecursive = 1;
141066 if( pRecTerm->selFlags & SF_Recursive0x0002000 ){
141067 sqlite3ErrorMsg(pParse,
141068 "multiple references to recursive table: %s", pCte->zName
141069 );
141070 return 2;
141071 }
141072 pRecTerm->selFlags |= SF_Recursive0x0002000;
141073 if( iRecTab<0 ) iRecTab = pParse->nTab++;
141074 pItem->iCursor = iRecTab;
141075 }
141076 }
141077 if( (pRecTerm->selFlags & SF_Recursive0x0002000)==0 ) break;
141078 pRecTerm = pRecTerm->pPrior;
141079 }
141080
141081 pCte->zCteErr = "circular reference: %s";
141082 pSavedWith = pParse->pWith;
141083 pParse->pWith = pWith;
141084 if( pSel->selFlags & SF_Recursive0x0002000 ){
141085 int rc;
141086 assert( pRecTerm!=0 )((void) (0));
141087 assert( (pRecTerm->selFlags & SF_Recursive)==0 )((void) (0));
141088 assert( pRecTerm->pNext!=0 )((void) (0));
141089 assert( (pRecTerm->pNext->selFlags & SF_Recursive)!=0 )((void) (0));
141090 assert( pRecTerm->pWith==0 )((void) (0));
141091 pRecTerm->pWith = pSel->pWith;
141092 rc = sqlite3WalkSelect(pWalker, pRecTerm);
141093 pRecTerm->pWith = 0;
141094 if( rc ){
141095 pParse->pWith = pSavedWith;
141096 return 2;
141097 }
141098 }else{
141099 if( sqlite3WalkSelect(pWalker, pSel) ){
141100 pParse->pWith = pSavedWith;
141101 return 2;
141102 }
141103 }
141104 pParse->pWith = pWith;
141105
141106 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
141107 pEList = pLeft->pEList;
141108 if( pCte->pCols ){
141109 if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
141110 sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns",
141111 pCte->zName, pEList->nExpr, pCte->pCols->nExpr
141112 );
141113 pParse->pWith = pSavedWith;
141114 return 2;
141115 }
141116 pEList = pCte->pCols;
141117 }
141118
141119 sqlite3ColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
141120 if( bMayRecursive ){
141121 if( pSel->selFlags & SF_Recursive0x0002000 ){
141122 pCte->zCteErr = "multiple recursive references: %s";
141123 }else{
141124 pCte->zCteErr = "recursive reference in a subquery: %s";
141125 }
141126 sqlite3WalkSelect(pWalker, pSel);
141127 }
141128 pCte->zCteErr = 0;
141129 pParse->pWith = pSavedWith;
141130 return 1; /* Success */
141131 }
141132 return 0; /* No match */
141133}
141134#endif
141135
141136#ifndef SQLITE_OMIT_CTE
141137/*
141138** If the SELECT passed as the second argument has an associated WITH
141139** clause, pop it from the stack stored as part of the Parse object.
141140**
141141** This function is used as the xSelectCallback2() callback by
141142** sqlite3SelectExpand() when walking a SELECT tree to resolve table
141143** names and other FROM clause elements.
141144*/
141145SQLITE_PRIVATEstatic void sqlite3SelectPopWith(Walker *pWalker, Select *p){
141146 Parse *pParse = pWalker->pParse;
141147 if( OK_IF_ALWAYS_TRUE(pParse->pWith)(pParse->pWith) && p->pPrior==0 ){
141148 With *pWith = findRightmost(p)->pWith;
141149 if( pWith!=0 ){
141150 assert( pParse->pWith==pWith || pParse->nErr )((void) (0));
141151 pParse->pWith = pWith->pOuter;
141152 }
141153 }
141154}
141155#endif
141156
141157/*
141158** The SrcList_item structure passed as the second argument represents a
141159** sub-query in the FROM clause of a SELECT statement. This function
141160** allocates and populates the SrcList_item.pTab object. If successful,
141161** SQLITE_OK is returned. Otherwise, if an OOM error is encountered,
141162** SQLITE_NOMEM.
141163*/
141164SQLITE_PRIVATEstatic int sqlite3ExpandSubquery(Parse *pParse, SrcItem *pFrom){
141165 Select *pSel = pFrom->pSelect;
141166 Table *pTab;
141167
141168 assert( pSel )((void) (0));
141169 pFrom->pTab = pTab = sqlite3DbMallocZero(pParse->db, sizeof(Table));
141170 if( pTab==0 ) return SQLITE_NOMEM7;
141171 pTab->nTabRef = 1;
141172 if( pFrom->zAlias ){
141173 pTab->zName = sqlite3DbStrDup(pParse->db, pFrom->zAlias);
141174 }else{
141175 pTab->zName = sqlite3MPrintf(pParse->db, "%!S", pFrom);
141176 }
141177 while( pSel->pPrior ){ pSel = pSel->pPrior; }
141178 sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol);
141179 pTab->iPKey = -1;
141180 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) )((void) (0));
141181#ifndef SQLITE_ALLOW_ROWID_IN_VIEW
141182 /* The usual case - do not allow ROWID on a subquery */
141183 pTab->tabFlags |= TF_Ephemeral0x00004000 | TF_NoVisibleRowid0x00000200;
141184#else
141185 pTab->tabFlags |= TF_Ephemeral0x00004000; /* Legacy compatibility mode */
141186#endif
141187 return pParse->nErr ? SQLITE_ERROR1 : SQLITE_OK0;
141188}
141189
141190
141191/*
141192** Check the N SrcItem objects to the right of pBase. (N might be zero!)
141193** If any of those SrcItem objects have a USING clause containing zName
141194** then return true.
141195**
141196** If N is zero, or none of the N SrcItem objects to the right of pBase
141197** contains a USING clause, or if none of the USING clauses contain zName,
141198** then return false.
141199*/
141200static int inAnyUsingClause(
141201 const char *zName, /* Name we are looking for */
141202 SrcItem *pBase, /* The base SrcItem. Looking at pBase[1] and following */
141203 int N /* How many SrcItems to check */
141204){
141205 while( N>0 ){
141206 N--;
141207 pBase++;
141208 if( pBase->fg.isUsing==0 ) continue;
141209 if( NEVER(pBase->u3.pUsing==0)(pBase->u3.pUsing==0) ) continue;
141210 if( sqlite3IdListIndex(pBase->u3.pUsing, zName)>=0 ) return 1;
141211 }
141212 return 0;
141213}
141214
141215
141216/*
141217** This routine is a Walker callback for "expanding" a SELECT statement.
141218** "Expanding" means to do the following:
141219**
141220** (1) Make sure VDBE cursor numbers have been assigned to every
141221** element of the FROM clause.
141222**
141223** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
141224** defines FROM clause. When views appear in the FROM clause,
141225** fill pTabList->a[].pSelect with a copy of the SELECT statement
141226** that implements the view. A copy is made of the view's SELECT
141227** statement so that we can freely modify or delete that statement
141228** without worrying about messing up the persistent representation
141229** of the view.
141230**
141231** (3) Add terms to the WHERE clause to accommodate the NATURAL keyword
141232** on joins and the ON and USING clause of joins.
141233**
141234** (4) Scan the list of columns in the result set (pEList) looking
141235** for instances of the "*" operator or the TABLE.* operator.
141236** If found, expand each "*" to be every column in every table
141237** and TABLE.* to be every column in TABLE.
141238**
141239*/
141240static int selectExpander(Walker *pWalker, Select *p){
141241 Parse *pParse = pWalker->pParse;
141242 int i, j, k, rc;
141243 SrcList *pTabList;
141244 ExprList *pEList;
141245 SrcItem *pFrom;
141246 sqlite3 *db = pParse->db;
141247 Expr *pE, *pRight, *pExpr;
141248 u16 selFlags = p->selFlags;
141249 u32 elistFlags = 0;
141250
141251 p->selFlags |= SF_Expanded0x0000040;
141252 if( db->mallocFailed ){
141253 return WRC_Abort2;
141254 }
141255 assert( p->pSrc!=0 )((void) (0));
141256 if( (selFlags & SF_Expanded0x0000040)!=0 ){
141257 return WRC_Prune1;
141258 }
141259 if( pWalker->eCode ){
141260 /* Renumber selId because it has been copied from a view */
141261 p->selId = ++pParse->nSelect;
141262 }
141263 pTabList = p->pSrc;
141264 pEList = p->pEList;
141265 if( pParse->pWith && (p->selFlags & SF_View0x0200000) ){
141266 if( p->pWith==0 ){
141267 p->pWith = (With*)sqlite3DbMallocZero(db, sizeof(With));
141268 if( p->pWith==0 ){
141269 return WRC_Abort2;
141270 }
141271 }
141272 p->pWith->bView = 1;
141273 }
141274 sqlite3WithPush(pParse, p->pWith, 0);
141275
141276 /* Make sure cursor numbers have been assigned to all entries in
141277 ** the FROM clause of the SELECT statement.
141278 */
141279 sqlite3SrcListAssignCursors(pParse, pTabList);
141280
141281 /* Look up every table named in the FROM clause of the select. If
141282 ** an entry of the FROM clause is a subquery instead of a table or view,
141283 ** then create a transient table structure to describe the subquery.
141284 */
141285 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
141286 Table *pTab;
141287 assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 )((void) (0));
141288 if( pFrom->pTab ) continue;
141289 assert( pFrom->fg.isRecursive==0 )((void) (0));
141290 if( pFrom->zName==0 ){
141291#ifndef SQLITE_OMIT_SUBQUERY
141292 Select *pSel = pFrom->pSelect;
141293 /* A sub-query in the FROM clause of a SELECT */
141294 assert( pSel!=0 )((void) (0));
141295 assert( pFrom->pTab==0 )((void) (0));
141296 if( sqlite3WalkSelect(pWalker, pSel) ) return WRC_Abort2;
141297 if( sqlite3ExpandSubquery(pParse, pFrom) ) return WRC_Abort2;
141298#endif
141299#ifndef SQLITE_OMIT_CTE
141300 }else if( (rc = resolveFromTermToCte(pParse, pWalker, pFrom))!=0 ){
141301 if( rc>1 ) return WRC_Abort2;
141302 pTab = pFrom->pTab;
141303 assert( pTab!=0 )((void) (0));
141304#endif
141305 }else{
141306 /* An ordinary table or view name in the FROM clause */
141307 assert( pFrom->pTab==0 )((void) (0));
141308 pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
141309 if( pTab==0 ) return WRC_Abort2;
141310 if( pTab->nTabRef>=0xffff ){
141311 sqlite3ErrorMsg(pParse, "too many references to \"%s\": max 65535",
141312 pTab->zName);
141313 pFrom->pTab = 0;
141314 return WRC_Abort2;
141315 }
141316 pTab->nTabRef++;
141317 if( !IsVirtual(pTab)((pTab)->eTabType==1) && cannotBeFunction(pParse, pFrom) ){
141318 return WRC_Abort2;
141319 }
141320#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
141321 if( !IsOrdinaryTable(pTab)((pTab)->eTabType==0) ){
141322 i16 nCol;
141323 u8 eCodeOrig = pWalker->eCode;
141324 if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort2;
141325 assert( pFrom->pSelect==0 )((void) (0));
141326 if( IsView(pTab)((pTab)->eTabType==2) ){
141327 if( (db->flags & SQLITE_EnableView0x80000000)==0
141328 && pTab->pSchema!=db->aDb[1].pSchema
141329 ){
141330 sqlite3ErrorMsg(pParse, "access to view \"%s\" prohibited",
141331 pTab->zName);
141332 }
141333 pFrom->pSelect = sqlite3SelectDup(db, pTab->u.view.pSelect, 0);
141334 }
141335#ifndef SQLITE_OMIT_VIRTUALTABLE
141336 else if( ALWAYS(IsVirtual(pTab))(((pTab)->eTabType==1))
141337 && pFrom->fg.fromDDL
141338 && ALWAYS(pTab->u.vtab.p!=0)(pTab->u.vtab.p!=0)
141339 && pTab->u.vtab.p->eVtabRisk > ((db->flags & SQLITE_TrustedSchema0x00000080)!=0)
141340 ){
141341 sqlite3ErrorMsg(pParse, "unsafe use of virtual table \"%s\"",
141342 pTab->zName);
141343 }
141344 assert( SQLITE_VTABRISK_Normal==1 && SQLITE_VTABRISK_High==2 )((void) (0));
141345#endif
141346 nCol = pTab->nCol;
141347 pTab->nCol = -1;
141348 pWalker->eCode = 1; /* Turn on Select.selId renumbering */
141349 sqlite3WalkSelect(pWalker, pFrom->pSelect);
141350 pWalker->eCode = eCodeOrig;
141351 pTab->nCol = nCol;
141352 }
141353#endif
141354 }
141355
141356 /* Locate the index named by the INDEXED BY clause, if any. */
141357 if( pFrom->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pFrom) ){
141358 return WRC_Abort2;
141359 }
141360 }
141361
141362 /* Process NATURAL keywords, and ON and USING clauses of joins.
141363 */
141364 assert( db->mallocFailed==0 || pParse->nErr!=0 )((void) (0));
141365 if( pParse->nErr || sqlite3ProcessJoin(pParse, p) ){
141366 return WRC_Abort2;
141367 }
141368
141369 /* For every "*" that occurs in the column list, insert the names of
141370 ** all columns in all tables. And for every TABLE.* insert the names
141371 ** of all columns in TABLE. The parser inserted a special expression
141372 ** with the TK_ASTERISK operator for each "*" that it found in the column
141373 ** list. The following code just has to locate the TK_ASTERISK
141374 ** expressions and expand each one to the list of all columns in
141375 ** all tables.
141376 **
141377 ** The first loop just checks to see if there are any "*" operators
141378 ** that need expanding.
141379 */
141380 for(k=0; k<pEList->nExpr; k++){
141381 pE = pEList->a[k].pExpr;
141382 if( pE->op==TK_ASTERISK180 ) break;
141383 assert( pE->op!=TK_DOT || pE->pRight!=0 )((void) (0));
141384 assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) )((void) (0));
141385 if( pE->op==TK_DOT141 && pE->pRight->op==TK_ASTERISK180 ) break;
141386 elistFlags |= pE->flags;
141387 }
141388 if( k<pEList->nExpr ){
141389 /*
141390 ** If we get here it means the result set contains one or more "*"
141391 ** operators that need to be expanded. Loop through each expression
141392 ** in the result set and expand them one by one.
141393 */
141394 struct ExprList_item *a = pEList->a;
141395 ExprList *pNew = 0;
141396 int flags = pParse->db->flags;
141397 int longNames = (flags & SQLITE_FullColNames0x00000004)!=0
141398 && (flags & SQLITE_ShortColNames0x00000040)==0;
141399
141400 for(k=0; k<pEList->nExpr; k++){
141401 pE = a[k].pExpr;
141402 elistFlags |= pE->flags;
141403 pRight = pE->pRight;
141404 assert( pE->op!=TK_DOT || pRight!=0 )((void) (0));
141405 if( pE->op!=TK_ASTERISK180
141406 && (pE->op!=TK_DOT141 || pRight->op!=TK_ASTERISK180)
141407 ){
141408 /* This particular expression does not need to be expanded.
141409 */
141410 pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
141411 if( pNew ){
141412 pNew->a[pNew->nExpr-1].zEName = a[k].zEName;
141413 pNew->a[pNew->nExpr-1].fg.eEName = a[k].fg.eEName;
141414 a[k].zEName = 0;
141415 }
141416 a[k].pExpr = 0;
141417 }else{
141418 /* This expression is a "*" or a "TABLE.*" and needs to be
141419 ** expanded. */
141420 int tableSeen = 0; /* Set to 1 when TABLE matches */
141421 char *zTName = 0; /* text of name of TABLE */
141422 if( pE->op==TK_DOT141 ){
141423 assert( pE->pLeft!=0 )((void) (0));
141424 assert( !ExprHasProperty(pE->pLeft, EP_IntValue) )((void) (0));
141425 zTName = pE->pLeft->u.zToken;
141426 }
141427 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
141428 Table *pTab = pFrom->pTab; /* Table for this data source */
141429 ExprList *pNestedFrom; /* Result-set of a nested FROM clause */
141430 char *zTabName; /* AS name for this data source */
141431 const char *zSchemaName = 0; /* Schema name for this data source */
141432 int iDb; /* Schema index for this data src */
141433 IdList *pUsing; /* USING clause for pFrom[1] */
141434
141435 if( (zTabName = pFrom->zAlias)==0 ){
141436 zTabName = pTab->zName;
141437 }
141438 if( db->mallocFailed ) break;
141439 assert( (int)pFrom->fg.isNestedFrom == IsNestedFrom(pFrom->pSelect) )((void) (0));
141440 if( pFrom->fg.isNestedFrom ){
141441 assert( pFrom->pSelect!=0 )((void) (0));
141442 pNestedFrom = pFrom->pSelect->pEList;
141443 assert( pNestedFrom!=0 )((void) (0));
141444 assert( pNestedFrom->nExpr==pTab->nCol )((void) (0));
141445 }else{
141446 if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
141447 continue;
141448 }
141449 pNestedFrom = 0;
141450 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
141451 zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*";
141452 }
141453 if( i+1<pTabList->nSrc
141454 && pFrom[1].fg.isUsing
141455 && (selFlags & SF_NestedFrom0x0000800)!=0
141456 ){
141457 int ii;
141458 pUsing = pFrom[1].u3.pUsing;
141459 for(ii=0; ii<pUsing->nId; ii++){
141460 const char *zUName = pUsing->a[ii].zName;
141461 pRight = sqlite3Expr(db, TK_ID59, zUName);
141462 pNew = sqlite3ExprListAppend(pParse, pNew, pRight);
141463 if( pNew ){
141464 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
141465 assert( pX->zEName==0 )((void) (0));
141466 pX->zEName = sqlite3MPrintf(db,"..%s", zUName);
141467 pX->fg.eEName = ENAME_TAB2;
141468 pX->fg.bUsingTerm = 1;
141469 }
141470 }
141471 }else{
141472 pUsing = 0;
141473 }
141474 for(j=0; j<pTab->nCol; j++){
141475 char *zName = pTab->aCol[j].zCnName;
141476 struct ExprList_item *pX; /* Newly added ExprList term */
141477
141478 assert( zName )((void) (0));
141479 if( zTName
141480 && pNestedFrom
141481 && sqlite3MatchEName(&pNestedFrom->a[j], 0, zTName, 0)==0
141482 ){
141483 continue;
141484 }
141485
141486 /* If a column is marked as 'hidden', omit it from the expanded
141487 ** result-set list unless the SELECT has the SF_IncludeHidden
141488 ** bit set.
141489 */
141490 if( (p->selFlags & SF_IncludeHidden0x0020000)==0
141491 && IsHiddenColumn(&pTab->aCol[j])(((&pTab->aCol[j])->colFlags & 0x0002)!=0)
141492 ){
141493 continue;
141494 }
141495 if( (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND0x0400)!=0
141496 && zTName==0
141497 && (selFlags & (SF_NestedFrom0x0000800))==0
141498 ){
141499 continue;
141500 }
141501 tableSeen = 1;
141502
141503 if( i>0 && zTName==0 && (selFlags & SF_NestedFrom0x0000800)==0 ){
141504 if( pFrom->fg.isUsing
141505 && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0
141506 ){
141507 /* In a join with a USING clause, omit columns in the
141508 ** using clause from the table on the right. */
141509 continue;
141510 }
141511 }
141512 pRight = sqlite3Expr(db, TK_ID59, zName);
141513 if( (pTabList->nSrc>1
141514 && ( (pFrom->fg.jointype & JT_LTORJ0x40)==0
141515 || (selFlags & SF_NestedFrom0x0000800)!=0
141516 || !inAnyUsingClause(zName,pFrom,pTabList->nSrc-i-1)
141517 )
141518 )
141519 || IN_RENAME_OBJECT(pParse->eParseMode>=2)
141520 ){
141521 Expr *pLeft;
141522 pLeft = sqlite3Expr(db, TK_ID59, zTabName);
141523 pExpr = sqlite3PExpr(pParse, TK_DOT141, pLeft, pRight);
141524 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && pE->pLeft ){
141525 sqlite3RenameTokenRemap(pParse, pLeft, pE->pLeft);
141526 }
141527 if( zSchemaName ){
141528 pLeft = sqlite3Expr(db, TK_ID59, zSchemaName);
141529 pExpr = sqlite3PExpr(pParse, TK_DOT141, pLeft, pExpr);
141530 }
141531 }else{
141532 pExpr = pRight;
141533 }
141534 pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
141535 if( pNew==0 ){
141536 break; /* OOM */
141537 }
141538 pX = &pNew->a[pNew->nExpr-1];
141539 assert( pX->zEName==0 )((void) (0));
141540 if( (selFlags & SF_NestedFrom0x0000800)!=0 && !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
141541 if( pNestedFrom ){
141542 pX->zEName = sqlite3DbStrDup(db, pNestedFrom->a[j].zEName);
141543 testcase( pX->zEName==0 );
141544 }else{
141545 pX->zEName = sqlite3MPrintf(db, "%s.%s.%s",
141546 zSchemaName, zTabName, zName);
141547 testcase( pX->zEName==0 );
141548 }
141549 pX->fg.eEName = ENAME_TAB2;
141550 if( (pFrom->fg.isUsing
141551 && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0)
141552 || (pUsing && sqlite3IdListIndex(pUsing, zName)>=0)
141553 || (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND0x0400)!=0
141554 ){
141555 pX->fg.bNoExpand = 1;
141556 }
141557 }else if( longNames ){
141558 pX->zEName = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
141559 pX->fg.eEName = ENAME_NAME0;
141560 }else{
141561 pX->zEName = sqlite3DbStrDup(db, zName);
141562 pX->fg.eEName = ENAME_NAME0;
141563 }
141564 }
141565 }
141566 if( !tableSeen ){
141567 if( zTName ){
141568 sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
141569 }else{
141570 sqlite3ErrorMsg(pParse, "no tables specified");
141571 }
141572 }
141573 }
141574 }
141575 sqlite3ExprListDelete(db, pEList);
141576 p->pEList = pNew;
141577 }
141578 if( p->pEList ){
141579 if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN2] ){
141580 sqlite3ErrorMsg(pParse, "too many columns in result set");
141581 return WRC_Abort2;
141582 }
141583 if( (elistFlags & (EP_HasFunc0x000008|EP_Subquery0x400000))!=0 ){
141584 p->selFlags |= SF_ComplexResult0x0040000;
141585 }
141586 }
141587#if TREETRACE_ENABLED0
141588 if( sqlite3TreeTrace & 0x100 ){
141589 SELECTTRACE(0x100,pParse,p,("After result-set wildcard expansion:\n"));
141590 sqlite3TreeViewSelect(0, p, 0);
141591 }
141592#endif
141593 return WRC_Continue0;
141594}
141595
141596#if SQLITE_DEBUG
141597/*
141598** Always assert. This xSelectCallback2 implementation proves that the
141599** xSelectCallback2 is never invoked.
141600*/
141601SQLITE_PRIVATEstatic void sqlite3SelectWalkAssert2(Walker *NotUsed, Select *NotUsed2){
141602 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
141603 assert( 0 )((void) (0));
141604}
141605#endif
141606/*
141607** This routine "expands" a SELECT statement and all of its subqueries.
141608** For additional information on what it means to "expand" a SELECT
141609** statement, see the comment on the selectExpand worker callback above.
141610**
141611** Expanding a SELECT statement is the first step in processing a
141612** SELECT statement. The SELECT statement must be expanded before
141613** name resolution is performed.
141614**
141615** If anything goes wrong, an error message is written into pParse.
141616** The calling function can detect the problem by looking at pParse->nErr
141617** and/or pParse->db->mallocFailed.
141618*/
141619static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
141620 Walker w;
141621 w.xExprCallback = sqlite3ExprWalkNoop;
141622 w.pParse = pParse;
141623 if( OK_IF_ALWAYS_TRUE(pParse->hasCompound)(pParse->hasCompound) ){
141624 w.xSelectCallback = convertCompoundSelectToSubquery;
141625 w.xSelectCallback2 = 0;
141626 sqlite3WalkSelect(&w, pSelect);
141627 }
141628 w.xSelectCallback = selectExpander;
141629 w.xSelectCallback2 = sqlite3SelectPopWith;
141630 w.eCode = 0;
141631 sqlite3WalkSelect(&w, pSelect);
141632}
141633
141634
141635#ifndef SQLITE_OMIT_SUBQUERY
141636/*
141637** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
141638** interface.
141639**
141640** For each FROM-clause subquery, add Column.zType and Column.zColl
141641** information to the Table structure that represents the result set
141642** of that subquery.
141643**
141644** The Table structure that represents the result set was constructed
141645** by selectExpander() but the type and collation information was omitted
141646** at that point because identifiers had not yet been resolved. This
141647** routine is called after identifier resolution.
141648*/
141649static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
141650 Parse *pParse;
141651 int i;
141652 SrcList *pTabList;
141653 SrcItem *pFrom;
141654
141655 assert( p->selFlags & SF_Resolved )((void) (0));
141656 if( p->selFlags & SF_HasTypeInfo0x0000080 ) return;
141657 p->selFlags |= SF_HasTypeInfo0x0000080;
141658 pParse = pWalker->pParse;
141659 pTabList = p->pSrc;
141660 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
141661 Table *pTab = pFrom->pTab;
141662 assert( pTab!=0 )((void) (0));
141663 if( (pTab->tabFlags & TF_Ephemeral0x00004000)!=0 ){
141664 /* A sub-query in the FROM clause of a SELECT */
141665 Select *pSel = pFrom->pSelect;
141666 if( pSel ){
141667 while( pSel->pPrior ) pSel = pSel->pPrior;
141668 sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSel,
141669 SQLITE_AFF_NONE0x40);
141670 }
141671 }
141672 }
141673}
141674#endif
141675
141676
141677/*
141678** This routine adds datatype and collating sequence information to
141679** the Table structures of all FROM-clause subqueries in a
141680** SELECT statement.
141681**
141682** Use this routine after name resolution.
141683*/
141684static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
141685#ifndef SQLITE_OMIT_SUBQUERY
141686 Walker w;
141687 w.xSelectCallback = sqlite3SelectWalkNoop;
141688 w.xSelectCallback2 = selectAddSubqueryTypeInfo;
141689 w.xExprCallback = sqlite3ExprWalkNoop;
141690 w.pParse = pParse;
141691 sqlite3WalkSelect(&w, pSelect);
141692#endif
141693}
141694
141695
141696/*
141697** This routine sets up a SELECT statement for processing. The
141698** following is accomplished:
141699**
141700** * VDBE Cursor numbers are assigned to all FROM-clause terms.
141701** * Ephemeral Table objects are created for all FROM-clause subqueries.
141702** * ON and USING clauses are shifted into WHERE statements
141703** * Wildcards "*" and "TABLE.*" in result sets are expanded.
141704** * Identifiers in expression are matched to tables.
141705**
141706** This routine acts recursively on all subqueries within the SELECT.
141707*/
141708SQLITE_PRIVATEstatic void sqlite3SelectPrep(
141709 Parse *pParse, /* The parser context */
141710 Select *p, /* The SELECT statement being coded. */
141711 NameContext *pOuterNC /* Name context for container */
141712){
141713 assert( p!=0 || pParse->db->mallocFailed )((void) (0));
141714 assert( pParse->db->pParse==pParse )((void) (0));
141715 if( pParse->db->mallocFailed ) return;
141716 if( p->selFlags & SF_HasTypeInfo0x0000080 ) return;
141717 sqlite3SelectExpand(pParse, p);
141718 if( pParse->nErr ) return;
141719 sqlite3ResolveSelectNames(pParse, p, pOuterNC);
141720 if( pParse->nErr ) return;
141721 sqlite3SelectAddTypeInfo(pParse, p);
141722}
141723
141724/*
141725** Reset the aggregate accumulator.
141726**
141727** The aggregate accumulator is a set of memory cells that hold
141728** intermediate results while calculating an aggregate. This
141729** routine generates code that stores NULLs in all of those memory
141730** cells.
141731*/
141732static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
141733 Vdbe *v = pParse->pVdbe;
141734 int i;
141735 struct AggInfo_func *pFunc;
141736 int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
141737 assert( pParse->db->pParse==pParse )((void) (0));
141738 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 )((void) (0));
141739 if( nReg==0 ) return;
141740 if( pParse->nErr ) return;
141741#ifdef SQLITE_DEBUG
141742 /* Verify that all AggInfo registers are within the range specified by
141743 ** AggInfo.mnReg..AggInfo.mxReg */
141744 assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 )((void) (0));
141745 for(i=0; i<pAggInfo->nColumn; i++){
141746 assert( pAggInfo->aCol[i].iMem>=pAggInfo->mnReg((void) (0))
141747 && pAggInfo->aCol[i].iMem<=pAggInfo->mxReg )((void) (0));
141748 }
141749 for(i=0; i<pAggInfo->nFunc; i++){
141750 assert( pAggInfo->aFunc[i].iMem>=pAggInfo->mnReg((void) (0))
141751 && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg )((void) (0));
141752 }
141753#endif
141754 sqlite3VdbeAddOp3(v, OP_Null75, 0, pAggInfo->mnReg, pAggInfo->mxReg);
141755 for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
141756 if( pFunc->iDistinct>=0 ){
141757 Expr *pE = pFunc->pFExpr;
141758 assert( ExprUseXList(pE) )((void) (0));
141759 if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
141760 sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
141761 "argument");
141762 pFunc->iDistinct = -1;
141763 }else{
141764 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pE->x.pList,0,0);
141765 pFunc->iDistAddr = sqlite3VdbeAddOp4(v, OP_OpenEphemeral118,
141766 pFunc->iDistinct, 0, 0, (char*)pKeyInfo, P4_KEYINFO(-8));
141767 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s(DISTINCT)",sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR %s(DISTINCT)"
, pFunc->pFunc->zName)
141768 pFunc->pFunc->zName))sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR %s(DISTINCT)"
, pFunc->pFunc->zName)
;
141769 }
141770 }
141771 }
141772}
141773
141774/*
141775** Invoke the OP_AggFinalize opcode for every aggregate function
141776** in the AggInfo structure.
141777*/
141778static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
141779 Vdbe *v = pParse->pVdbe;
141780 int i;
141781 struct AggInfo_func *pF;
141782 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
141783 ExprList *pList;
141784 assert( ExprUseXList(pF->pFExpr) )((void) (0));
141785 pList = pF->pFExpr->x.pList;
141786 sqlite3VdbeAddOp2(v, OP_AggFinal165, pF->iMem, pList ? pList->nExpr : 0);
141787 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF(-7));
141788 }
141789}
141790
141791
141792/*
141793** Update the accumulator memory cells for an aggregate based on
141794** the current cursor position.
141795**
141796** If regAcc is non-zero and there are no min() or max() aggregates
141797** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
141798** registers if register regAcc contains 0. The caller will take care
141799** of setting and clearing regAcc.
141800*/
141801static void updateAccumulator(
141802 Parse *pParse,
141803 int regAcc,
141804 AggInfo *pAggInfo,
141805 int eDistinctType
141806){
141807 Vdbe *v = pParse->pVdbe;
141808 int i;
141809 int regHit = 0;
141810 int addrHitTest = 0;
141811 struct AggInfo_func *pF;
141812 struct AggInfo_col *pC;
141813
141814 pAggInfo->directMode = 1;
141815 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
141816 int nArg;
141817 int addrNext = 0;
141818 int regAgg;
141819 ExprList *pList;
141820 assert( ExprUseXList(pF->pFExpr) )((void) (0));
141821 assert( !IsWindowFunc(pF->pFExpr) )((void) (0));
141822 pList = pF->pFExpr->x.pList;
141823 if( ExprHasProperty(pF->pFExpr, EP_WinFunc)(((pF->pFExpr)->flags&(0x1000000))!=0) ){
141824 Expr *pFilter = pF->pFExpr->y.pWin->pFilter;
141825 if( pAggInfo->nAccumulator
141826 && (pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL0x0020)
141827 && regAcc
141828 ){
141829 /* If regAcc==0, there there exists some min() or max() function
141830 ** without a FILTER clause that will ensure the magnet registers
141831 ** are populated. */
141832 if( regHit==0 ) regHit = ++pParse->nMem;
141833 /* If this is the first row of the group (regAcc contains 0), clear the
141834 ** "magnet" register regHit so that the accumulator registers
141835 ** are populated if the FILTER clause jumps over the the
141836 ** invocation of min() or max() altogether. Or, if this is not
141837 ** the first row (regAcc contains 1), set the magnet register so that
141838 ** the accumulators are not populated unless the min()/max() is invoked
141839 ** and indicates that they should be. */
141840 sqlite3VdbeAddOp2(v, OP_Copy80, regAcc, regHit);
141841 }
141842 addrNext = sqlite3VdbeMakeLabel(pParse);
141843 sqlite3ExprIfFalse(pParse, pFilter, addrNext, SQLITE_JUMPIFNULL0x10);
141844 }
141845 if( pList ){
141846 nArg = pList->nExpr;
141847 regAgg = sqlite3GetTempRange(pParse, nArg);
141848 sqlite3ExprCodeExprList(pParse, pList, regAgg, 0, SQLITE_ECEL_DUP0x01);
141849 }else{
141850 nArg = 0;
141851 regAgg = 0;
141852 }
141853 if( pF->iDistinct>=0 && pList ){
141854 if( addrNext==0 ){
141855 addrNext = sqlite3VdbeMakeLabel(pParse);
141856 }
141857 pF->iDistinct = codeDistinct(pParse, eDistinctType,
141858 pF->iDistinct, addrNext, pList, regAgg);
141859 }
141860 if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL0x0020 ){
141861 CollSeq *pColl = 0;
141862 struct ExprList_item *pItem;
141863 int j;
141864 assert( pList!=0 )((void) (0)); /* pList!=0 if pF->pFunc has NEEDCOLL */
141865 for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
141866 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
141867 }
141868 if( !pColl ){
141869 pColl = pParse->db->pDfltColl;
141870 }
141871 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
141872 sqlite3VdbeAddOp4(v, OP_CollSeq85, regHit, 0, 0, (char *)pColl, P4_COLLSEQ(-2));
141873 }
141874 sqlite3VdbeAddOp3(v, OP_AggStep162, 0, regAgg, pF->iMem);
141875 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF(-7));
141876 sqlite3VdbeChangeP5(v, (u8)nArg);
141877 sqlite3ReleaseTempRange(pParse, regAgg, nArg);
141878 if( addrNext ){
141879 sqlite3VdbeResolveLabel(v, addrNext);
141880 }
141881 }
141882 if( regHit==0 && pAggInfo->nAccumulator ){
141883 regHit = regAcc;
141884 }
141885 if( regHit ){
141886 addrHitTest = sqlite3VdbeAddOp1(v, OP_If15, regHit); VdbeCoverage(v);
141887 }
141888 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
141889 sqlite3ExprCode(pParse, pC->pCExpr, pC->iMem);
141890 }
141891
141892 pAggInfo->directMode = 0;
141893 if( addrHitTest ){
141894 sqlite3VdbeJumpHereOrPopInst(v, addrHitTest);
141895 }
141896}
141897
141898/*
141899** Add a single OP_Explain instruction to the VDBE to explain a simple
141900** count(*) query ("SELECT count(*) FROM pTab").
141901*/
141902#ifndef SQLITE_OMIT_EXPLAIN
141903static void explainSimpleCount(
141904 Parse *pParse, /* Parse context */
141905 Table *pTab, /* Table being queried */
141906 Index *pIdx /* Index used to optimize scan, or NULL */
141907){
141908 if( pParse->explain==2 ){
141909 int bCover = (pIdx!=0 && (HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) || !IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2)));
141910 sqlite3VdbeExplain(pParse, 0, "SCAN %s%s%s",
141911 pTab->zName,
141912 bCover ? " USING COVERING INDEX " : "",
141913 bCover ? pIdx->zName : ""
141914 );
141915 }
141916}
141917#else
141918# define explainSimpleCount(a,b,c)
141919#endif
141920
141921/*
141922** sqlite3WalkExpr() callback used by havingToWhere().
141923**
141924** If the node passed to the callback is a TK_AND node, return
141925** WRC_Continue to tell sqlite3WalkExpr() to iterate through child nodes.
141926**
141927** Otherwise, return WRC_Prune. In this case, also check if the
141928** sub-expression matches the criteria for being moved to the WHERE
141929** clause. If so, add it to the WHERE clause and replace the sub-expression
141930** within the HAVING expression with a constant "1".
141931*/
141932static int havingToWhereExprCb(Walker *pWalker, Expr *pExpr){
141933 if( pExpr->op!=TK_AND44 ){
141934 Select *pS = pWalker->u.pSelect;
141935 /* This routine is called before the HAVING clause of the current
141936 ** SELECT is analyzed for aggregates. So if pExpr->pAggInfo is set
141937 ** here, it indicates that the expression is a correlated reference to a
141938 ** column from an outer aggregate query, or an aggregate function that
141939 ** belongs to an outer query. Do not move the expression to the WHERE
141940 ** clause in this obscure case, as doing so may corrupt the outer Select
141941 ** statements AggInfo structure. */
141942 if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy)
141943 && ExprAlwaysFalse(pExpr)(((pExpr)->flags&(0x000001|0x20000000))==0x20000000)==0
141944 && pExpr->pAggInfo==0
141945 ){
141946 sqlite3 *db = pWalker->pParse->db;
141947 Expr *pNew = sqlite3Expr(db, TK_INTEGER155, "1");
141948 if( pNew ){
141949 Expr *pWhere = pS->pWhere;
141950 SWAP(Expr, *pNew, *pExpr){Expr t=*pNew; *pNew=*pExpr; *pExpr=t;};
141951 pNew = sqlite3ExprAnd(pWalker->pParse, pWhere, pNew);
141952 pS->pWhere = pNew;
141953 pWalker->eCode = 1;
141954 }
141955 }
141956 return WRC_Prune1;
141957 }
141958 return WRC_Continue0;
141959}
141960
141961/*
141962** Transfer eligible terms from the HAVING clause of a query, which is
141963** processed after grouping, to the WHERE clause, which is processed before
141964** grouping. For example, the query:
141965**
141966** SELECT * FROM <tables> WHERE a=? GROUP BY b HAVING b=? AND c=?
141967**
141968** can be rewritten as:
141969**
141970** SELECT * FROM <tables> WHERE a=? AND b=? GROUP BY b HAVING c=?
141971**
141972** A term of the HAVING expression is eligible for transfer if it consists
141973** entirely of constants and expressions that are also GROUP BY terms that
141974** use the "BINARY" collation sequence.
141975*/
141976static void havingToWhere(Parse *pParse, Select *p){
141977 Walker sWalker;
141978 memset(&sWalker, 0, sizeof(sWalker));
141979 sWalker.pParse = pParse;
141980 sWalker.xExprCallback = havingToWhereExprCb;
141981 sWalker.u.pSelect = p;
141982 sqlite3WalkExpr(&sWalker, p->pHaving);
141983#if TREETRACE_ENABLED0
141984 if( sWalker.eCode && (sqlite3TreeTrace & 0x100)!=0 ){
141985 SELECTTRACE(0x100,pParse,p,("Move HAVING terms into WHERE:\n"));
141986 sqlite3TreeViewSelect(0, p, 0);
141987 }
141988#endif
141989}
141990
141991/*
141992** Check to see if the pThis entry of pTabList is a self-join of a prior view.
141993** If it is, then return the SrcList_item for the prior view. If it is not,
141994** then return 0.
141995*/
141996static SrcItem *isSelfJoinView(
141997 SrcList *pTabList, /* Search for self-joins in this FROM clause */
141998 SrcItem *pThis /* Search for prior reference to this subquery */
141999){
142000 SrcItem *pItem;
142001 assert( pThis->pSelect!=0 )((void) (0));
142002 if( pThis->pSelect->selFlags & SF_PushDown0x1000000 ) return 0;
142003 for(pItem = pTabList->a; pItem<pThis; pItem++){
142004 Select *pS1;
142005 if( pItem->pSelect==0 ) continue;
142006 if( pItem->fg.viaCoroutine ) continue;
142007 if( pItem->zName==0 ) continue;
142008 assert( pItem->pTab!=0 )((void) (0));
142009 assert( pThis->pTab!=0 )((void) (0));
142010 if( pItem->pTab->pSchema!=pThis->pTab->pSchema ) continue;
142011 if( sqlite3_stricmp(pItem->zName, pThis->zName)!=0 ) continue;
142012 pS1 = pItem->pSelect;
142013 if( pItem->pTab->pSchema==0 && pThis->pSelect->selId!=pS1->selId ){
142014 /* The query flattener left two different CTE tables with identical
142015 ** names in the same FROM clause. */
142016 continue;
142017 }
142018 if( pItem->pSelect->selFlags & SF_PushDown0x1000000 ){
142019 /* The view was modified by some other optimization such as
142020 ** pushDownWhereTerms() */
142021 continue;
142022 }
142023 return pItem;
142024 }
142025 return 0;
142026}
142027
142028/*
142029** Deallocate a single AggInfo object
142030*/
142031static void agginfoFree(sqlite3 *db, AggInfo *p){
142032 sqlite3DbFree(db, p->aCol);
142033 sqlite3DbFree(db, p->aFunc);
142034 sqlite3DbFreeNN(db, p);
142035}
142036
142037#ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION
142038/*
142039** Attempt to transform a query of the form
142040**
142041** SELECT count(*) FROM (SELECT x FROM t1 UNION ALL SELECT y FROM t2)
142042**
142043** Into this:
142044**
142045** SELECT (SELECT count(*) FROM t1)+(SELECT count(*) FROM t2)
142046**
142047** The transformation only works if all of the following are true:
142048**
142049** * The subquery is a UNION ALL of two or more terms
142050** * The subquery does not have a LIMIT clause
142051** * There is no WHERE or GROUP BY or HAVING clauses on the subqueries
142052** * The outer query is a simple count(*) with no WHERE clause or other
142053** extraneous syntax.
142054**
142055** Return TRUE if the optimization is undertaken.
142056*/
142057static int countOfViewOptimization(Parse *pParse, Select *p){
142058 Select *pSub, *pPrior;
142059 Expr *pExpr;
142060 Expr *pCount;
142061 sqlite3 *db;
142062 if( (p->selFlags & SF_Aggregate0x0000008)==0 ) return 0; /* This is an aggregate */
142063 if( p->pEList->nExpr!=1 ) return 0; /* Single result column */
142064 if( p->pWhere ) return 0;
142065 if( p->pGroupBy ) return 0;
142066 pExpr = p->pEList->a[0].pExpr;
142067 if( pExpr->op!=TK_AGG_FUNCTION168 ) return 0; /* Result is an aggregate */
142068 assert( ExprUseUToken(pExpr) )((void) (0));
142069 if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0; /* Is count() */
142070 assert( ExprUseXList(pExpr) )((void) (0));
142071 if( pExpr->x.pList!=0 ) return 0; /* Must be count(*) */
142072 if( p->pSrc->nSrc!=1 ) return 0; /* One table in FROM */
142073 pSub = p->pSrc->a[0].pSelect;
142074 if( pSub==0 ) return 0; /* The FROM is a subquery */
142075 if( pSub->pPrior==0 ) return 0; /* Must be a compound ry */
142076 do{
142077 if( pSub->op!=TK_ALL135 && pSub->pPrior ) return 0; /* Must be UNION ALL */
142078 if( pSub->pWhere ) return 0; /* No WHERE clause */
142079 if( pSub->pLimit ) return 0; /* No LIMIT clause */
142080 if( pSub->selFlags & SF_Aggregate0x0000008 ) return 0; /* Not an aggregate */
142081 pSub = pSub->pPrior; /* Repeat over compound */
142082 }while( pSub );
142083
142084 /* If we reach this point then it is OK to perform the transformation */
142085
142086 db = pParse->db;
142087 pCount = pExpr;
142088 pExpr = 0;
142089 pSub = p->pSrc->a[0].pSelect;
142090 p->pSrc->a[0].pSelect = 0;
142091 sqlite3SrcListDelete(db, p->pSrc);
142092 p->pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*p->pSrc));
142093 while( pSub ){
142094 Expr *pTerm;
142095 pPrior = pSub->pPrior;
142096 pSub->pPrior = 0;
142097 pSub->pNext = 0;
142098 pSub->selFlags |= SF_Aggregate0x0000008;
142099 pSub->selFlags &= ~SF_Compound0x0000100;
142100 pSub->nSelectRow = 0;
142101 sqlite3ExprListDelete(db, pSub->pEList);
142102 pTerm = pPrior ? sqlite3ExprDup(db, pCount, 0) : pCount;
142103 pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
142104 pTerm = sqlite3PExpr(pParse, TK_SELECT138, 0, 0);
142105 sqlite3PExprAddSelect(pParse, pTerm, pSub);
142106 if( pExpr==0 ){
142107 pExpr = pTerm;
142108 }else{
142109 pExpr = sqlite3PExpr(pParse, TK_PLUS106, pTerm, pExpr);
142110 }
142111 pSub = pPrior;
142112 }
142113 p->pEList->a[0].pExpr = pExpr;
142114 p->selFlags &= ~SF_Aggregate0x0000008;
142115
142116#if TREETRACE_ENABLED0
142117 if( sqlite3TreeTrace & 0x400 ){
142118 SELECTTRACE(0x400,pParse,p,("After count-of-view optimization:\n"));
142119 sqlite3TreeViewSelect(0, p, 0);
142120 }
142121#endif
142122 return 1;
142123}
142124#endif /* SQLITE_COUNTOFVIEW_OPTIMIZATION */
142125
142126/*
142127** If any term of pSrc, or any SF_NestedFrom sub-query, is not the same
142128** as pSrcItem but has the same alias as p0, then return true.
142129** Otherwise return false.
142130*/
142131static int sameSrcAlias(SrcItem *p0, SrcList *pSrc){
142132 int i;
142133 for(i=0; i<pSrc->nSrc; i++){
142134 SrcItem *p1 = &pSrc->a[i];
142135 if( p1==p0 ) continue;
142136 if( p0->pTab==p1->pTab && 0==sqlite3_stricmp(p0->zAlias, p1->zAlias) ){
142137 return 1;
142138 }
142139 if( p1->pSelect
142140 && (p1->pSelect->selFlags & SF_NestedFrom0x0000800)!=0
142141 && sameSrcAlias(p0, p1->pSelect->pSrc)
142142 ){
142143 return 1;
142144 }
142145 }
142146 return 0;
142147}
142148
142149/*
142150** Generate code for the SELECT statement given in the p argument.
142151**
142152** The results are returned according to the SelectDest structure.
142153** See comments in sqliteInt.h for further information.
142154**
142155** This routine returns the number of errors. If any errors are
142156** encountered, then an appropriate error message is left in
142157** pParse->zErrMsg.
142158**
142159** This routine does NOT free the Select structure passed in. The
142160** calling function needs to do that.
142161*/
142162SQLITE_PRIVATEstatic int sqlite3Select(
142163 Parse *pParse, /* The parser context */
142164 Select *p, /* The SELECT statement being coded. */
142165 SelectDest *pDest /* What to do with the query results */
142166){
142167 int i, j; /* Loop counters */
142168 WhereInfo *pWInfo; /* Return from sqlite3WhereBegin() */
142169 Vdbe *v; /* The virtual machine under construction */
142170 int isAgg; /* True for select lists like "count(*)" */
142171 ExprList *pEList = 0; /* List of columns to extract. */
142172 SrcList *pTabList; /* List of tables to select from */
142173 Expr *pWhere; /* The WHERE clause. May be NULL */
142174 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
142175 Expr *pHaving; /* The HAVING clause. May be NULL */
142176 AggInfo *pAggInfo = 0; /* Aggregate information */
142177 int rc = 1; /* Value to return from this function */
142178 DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
142179 SortCtx sSort; /* Info on how to code the ORDER BY clause */
142180 int iEnd; /* Address of the end of the query */
142181 sqlite3 *db; /* The database connection */
142182 ExprList *pMinMaxOrderBy = 0; /* Added ORDER BY for min/max queries */
142183 u8 minMaxFlag; /* Flag for min/max queries */
142184
142185 db = pParse->db;
142186 assert( pParse==db->pParse )((void) (0));
142187 v = sqlite3GetVdbe(pParse);
142188 if( p==0 || pParse->nErr ){
142189 return 1;
142190 }
142191 assert( db->mallocFailed==0 )((void) (0));
142192 if( sqlite3AuthCheck(pParse, SQLITE_SELECT21, 0, 0, 0) ) return 1;
142193#if TREETRACE_ENABLED0
142194 SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain));
142195 if( sqlite3TreeTrace & 0x10100 ){
142196 if( (sqlite3TreeTrace & 0x10001)==0x10000 ){
142197 sqlite3TreeViewLine(0, "In sqlite3Select() at %s:%d",
142198 __FILE__"3rdparty/sqlite3/sqlite3.c", __LINE__142198);
142199 }
142200 sqlite3ShowSelect(p);
142201 }
142202#endif
142203
142204 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo )((void) (0));
142205 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo )((void) (0));
142206 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue )((void) (0));
142207 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue )((void) (0));
142208 if( IgnorableDistinct(pDest)((pDest->eDest)<=6) ){
142209 assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||((void) (0))
142210 pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||((void) (0))
142211 pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_DistFifo )((void) (0));
142212 /* All of these destinations are also able to ignore the ORDER BY clause */
142213 if( p->pOrderBy ){
142214#if TREETRACE_ENABLED0
142215 SELECTTRACE(1,pParse,p, ("dropping superfluous ORDER BY:\n"));
142216 if( sqlite3TreeTrace & 0x100 ){
142217 sqlite3TreeViewExprList(0, p->pOrderBy, 0, "ORDERBY");
142218 }
142219#endif
142220 sqlite3ParserAddCleanup(pParse,
142221 (void(*)(sqlite3*,void*))sqlite3ExprListDelete,
142222 p->pOrderBy);
142223 testcase( pParse->earlyCleanup );
142224 p->pOrderBy = 0;
142225 }
142226 p->selFlags &= ~SF_Distinct0x0000001;
142227 p->selFlags |= SF_NoopOrderBy0x0400000;
142228 }
142229 sqlite3SelectPrep(pParse, p, 0);
142230 if( pParse->nErr ){
142231 goto select_end;
142232 }
142233 assert( db->mallocFailed==0 )((void) (0));
142234 assert( p->pEList!=0 )((void) (0));
142235#if TREETRACE_ENABLED0
142236 if( sqlite3TreeTrace & 0x104 ){
142237 SELECTTRACE(0x104,pParse,p, ("after name resolution:\n"));
142238 sqlite3TreeViewSelect(0, p, 0);
142239 }
142240#endif
142241
142242 /* If the SF_UFSrcCheck flag is set, then this function is being called
142243 ** as part of populating the temp table for an UPDATE...FROM statement.
142244 ** In this case, it is an error if the target object (pSrc->a[0]) name
142245 ** or alias is duplicated within FROM clause (pSrc->a[1..n]).
142246 **
142247 ** Postgres disallows this case too. The reason is that some other
142248 ** systems handle this case differently, and not all the same way,
142249 ** which is just confusing. To avoid this, we follow PG's lead and
142250 ** disallow it altogether. */
142251 if( p->selFlags & SF_UFSrcCheck0x0800000 ){
142252 SrcItem *p0 = &p->pSrc->a[0];
142253 if( sameSrcAlias(p0, p->pSrc) ){
142254 sqlite3ErrorMsg(pParse,
142255 "target object/alias may not appear in FROM clause: %s",
142256 p0->zAlias ? p0->zAlias : p0->pTab->zName
142257 );
142258 goto select_end;
142259 }
142260
142261 /* Clear the SF_UFSrcCheck flag. The check has already been performed,
142262 ** and leaving this flag set can cause errors if a compound sub-query
142263 ** in p->pSrc is flattened into this query and this function called
142264 ** again as part of compound SELECT processing. */
142265 p->selFlags &= ~SF_UFSrcCheck0x0800000;
142266 }
142267
142268 if( pDest->eDest==SRT_Output9 ){
142269 sqlite3GenerateColumnNames(pParse, p);
142270 }
142271
142272#ifndef SQLITE_OMIT_WINDOWFUNC
142273 if( sqlite3WindowRewrite(pParse, p) ){
142274 assert( pParse->nErr )((void) (0));
142275 goto select_end;
142276 }
142277#if TREETRACE_ENABLED0
142278 if( p->pWin && (sqlite3TreeTrace & 0x108)!=0 ){
142279 SELECTTRACE(0x104,pParse,p, ("after window rewrite:\n"));
142280 sqlite3TreeViewSelect(0, p, 0);
142281 }
142282#endif
142283#endif /* SQLITE_OMIT_WINDOWFUNC */
142284 pTabList = p->pSrc;
142285 isAgg = (p->selFlags & SF_Aggregate0x0000008)!=0;
142286 memset(&sSort, 0, sizeof(sSort));
142287 sSort.pOrderBy = p->pOrderBy;
142288
142289 /* Try to do various optimizations (flattening subqueries, and strength
142290 ** reduction of join operators) in the FROM clause up into the main query
142291 */
142292#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
142293 for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
142294 SrcItem *pItem = &pTabList->a[i];
142295 Select *pSub = pItem->pSelect;
142296 Table *pTab = pItem->pTab;
142297
142298 /* The expander should have already created transient Table objects
142299 ** even for FROM clause elements such as subqueries that do not correspond
142300 ** to a real table */
142301 assert( pTab!=0 )((void) (0));
142302
142303 /* Convert LEFT JOIN into JOIN if there are terms of the right table
142304 ** of the LEFT JOIN used in the WHERE clause.
142305 */
142306 if( (pItem->fg.jointype & (JT_LEFT0x08|JT_RIGHT0x10))==JT_LEFT0x08
142307 && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor)
142308 && OptimizationEnabled(db, SQLITE_SimplifyJoin)(((db)->dbOptFlags&(0x00002000))==0)
142309 ){
142310 SELECTTRACE(0x100,pParse,p,
142311 ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
142312 pItem->fg.jointype &= ~(JT_LEFT0x08|JT_OUTER0x20);
142313 assert( pItem->iCursor>=0 )((void) (0));
142314 unsetJoinExpr(p->pWhere, pItem->iCursor,
142315 pTabList->a[0].fg.jointype & JT_LTORJ0x40);
142316 }
142317
142318 /* No futher action if this term of the FROM clause is no a subquery */
142319 if( pSub==0 ) continue;
142320
142321 /* Catch mismatch in the declared columns of a view and the number of
142322 ** columns in the SELECT on the RHS */
142323 if( pTab->nCol!=pSub->pEList->nExpr ){
142324 sqlite3ErrorMsg(pParse, "expected %d columns for '%s' but got %d",
142325 pTab->nCol, pTab->zName, pSub->pEList->nExpr);
142326 goto select_end;
142327 }
142328
142329 /* Do not try to flatten an aggregate subquery.
142330 **
142331 ** Flattening an aggregate subquery is only possible if the outer query
142332 ** is not a join. But if the outer query is not a join, then the subquery
142333 ** will be implemented as a co-routine and there is no advantage to
142334 ** flattening in that case.
142335 */
142336 if( (pSub->selFlags & SF_Aggregate0x0000008)!=0 ) continue;
142337 assert( pSub->pGroupBy==0 )((void) (0));
142338
142339 /* If a FROM-clause subquery has an ORDER BY clause that is not
142340 ** really doing anything, then delete it now so that it does not
142341 ** interfere with query flattening. See the discussion at
142342 ** https://sqlite.org/forum/forumpost/2d76f2bcf65d256a
142343 **
142344 ** Beware of these cases where the ORDER BY clause may not be safely
142345 ** omitted:
142346 **
142347 ** (1) There is also a LIMIT clause
142348 ** (2) The subquery was added to help with window-function
142349 ** processing
142350 ** (3) The subquery is in the FROM clause of an UPDATE
142351 ** (4) The outer query uses an aggregate function other than
142352 ** the built-in count(), min(), or max().
142353 ** (5) The ORDER BY isn't going to accomplish anything because
142354 ** one of:
142355 ** (a) The outer query has a different ORDER BY clause
142356 ** (b) The subquery is part of a join
142357 ** See forum post 062d576715d277c8
142358 */
142359 if( pSub->pOrderBy!=0
142360 && (p->pOrderBy!=0 || pTabList->nSrc>1) /* Condition (5) */
142361 && pSub->pLimit==0 /* Condition (1) */
142362 && (pSub->selFlags & SF_OrderByReqd0x8000000)==0 /* Condition (2) */
142363 && (p->selFlags & SF_OrderByReqd0x8000000)==0 /* Condition (3) and (4) */
142364 && OptimizationEnabled(db, SQLITE_OmitOrderBy)(((db)->dbOptFlags&(0x00040000))==0)
142365 ){
142366 SELECTTRACE(0x100,pParse,p,
142367 ("omit superfluous ORDER BY on %r FROM-clause subquery\n",i+1));
142368 sqlite3ParserAddCleanup(pParse,
142369 (void(*)(sqlite3*,void*))sqlite3ExprListDelete,
142370 pSub->pOrderBy);
142371 pSub->pOrderBy = 0;
142372 }
142373
142374 /* If the outer query contains a "complex" result set (that is,
142375 ** if the result set of the outer query uses functions or subqueries)
142376 ** and if the subquery contains an ORDER BY clause and if
142377 ** it will be implemented as a co-routine, then do not flatten. This
142378 ** restriction allows SQL constructs like this:
142379 **
142380 ** SELECT expensive_function(x)
142381 ** FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
142382 **
142383 ** The expensive_function() is only computed on the 10 rows that
142384 ** are output, rather than every row of the table.
142385 **
142386 ** The requirement that the outer query have a complex result set
142387 ** means that flattening does occur on simpler SQL constraints without
142388 ** the expensive_function() like:
142389 **
142390 ** SELECT x FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
142391 */
142392 if( pSub->pOrderBy!=0
142393 && i==0
142394 && (p->selFlags & SF_ComplexResult0x0040000)!=0
142395 && (pTabList->nSrc==1
142396 || (pTabList->a[1].fg.jointype&(JT_OUTER0x20|JT_CROSS0x02))!=0)
142397 ){
142398 continue;
142399 }
142400
142401 if( flattenSubquery(pParse, p, i, isAgg) ){
142402 if( pParse->nErr ) goto select_end;
142403 /* This subquery can be absorbed into its parent. */
142404 i = -1;
142405 }
142406 pTabList = p->pSrc;
142407 if( db->mallocFailed ) goto select_end;
142408 if( !IgnorableOrderby(pDest)((pDest->eDest)<=8) ){
142409 sSort.pOrderBy = p->pOrderBy;
142410 }
142411 }
142412#endif
142413
142414#ifndef SQLITE_OMIT_COMPOUND_SELECT
142415 /* Handle compound SELECT statements using the separate multiSelect()
142416 ** procedure.
142417 */
142418 if( p->pPrior ){
142419 rc = multiSelect(pParse, p, pDest);
142420#if TREETRACE_ENABLED0
142421 SELECTTRACE(0x1,pParse,p,("end compound-select processing\n"));
142422 if( (sqlite3TreeTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)sqlite3VdbeExplainParent(pParse)==0 ){
142423 sqlite3TreeViewSelect(0, p, 0);
142424 }
142425#endif
142426 if( p->pNext==0 ) ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
142427 return rc;
142428 }
142429#endif
142430
142431 /* Do the WHERE-clause constant propagation optimization if this is
142432 ** a join. No need to speed time on this operation for non-join queries
142433 ** as the equivalent optimization will be handled by query planner in
142434 ** sqlite3WhereBegin().
142435 */
142436 if( p->pWhere!=0
142437 && p->pWhere->op==TK_AND44
142438 && OptimizationEnabled(db, SQLITE_PropagateConst)(((db)->dbOptFlags&(0x00008000))==0)
142439 && propagateConstants(pParse, p)
142440 ){
142441#if TREETRACE_ENABLED0
142442 if( sqlite3TreeTrace & 0x100 ){
142443 SELECTTRACE(0x100,pParse,p,("After constant propagation:\n"));
142444 sqlite3TreeViewSelect(0, p, 0);
142445 }
142446#endif
142447 }else{
142448 SELECTTRACE(0x100,pParse,p,("Constant propagation not helpful\n"));
142449 }
142450
142451#ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION
142452 if( OptimizationEnabled(db, SQLITE_QueryFlattener|SQLITE_CountOfView)(((db)->dbOptFlags&(0x00000001|0x00000200))==0)
142453 && countOfViewOptimization(pParse, p)
142454 ){
142455 if( db->mallocFailed ) goto select_end;
142456 pEList = p->pEList;
142457 pTabList = p->pSrc;
142458 }
142459#endif
142460
142461 /* For each term in the FROM clause, do two things:
142462 ** (1) Authorized unreferenced tables
142463 ** (2) Generate code for all sub-queries
142464 */
142465 for(i=0; i<pTabList->nSrc; i++){
142466 SrcItem *pItem = &pTabList->a[i];
142467 SrcItem *pPrior;
142468 SelectDest dest;
142469 Select *pSub;
142470#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
142471 const char *zSavedAuthContext;
142472#endif
142473
142474 /* Issue SQLITE_READ authorizations with a fake column name for any
142475 ** tables that are referenced but from which no values are extracted.
142476 ** Examples of where these kinds of null SQLITE_READ authorizations
142477 ** would occur:
142478 **
142479 ** SELECT count(*) FROM t1; -- SQLITE_READ t1.""
142480 ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2.""
142481 **
142482 ** The fake column name is an empty string. It is possible for a table to
142483 ** have a column named by the empty string, in which case there is no way to
142484 ** distinguish between an unreferenced table and an actual reference to the
142485 ** "" column. The original design was for the fake column name to be a NULL,
142486 ** which would be unambiguous. But legacy authorization callbacks might
142487 ** assume the column name is non-NULL and segfault. The use of an empty
142488 ** string for the fake column name seems safer.
142489 */
142490 if( pItem->colUsed==0 && pItem->zName!=0 ){
142491 sqlite3AuthCheck(pParse, SQLITE_READ20, pItem->zName, "", pItem->zDatabase);
142492 }
142493
142494#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
142495 /* Generate code for all sub-queries in the FROM clause
142496 */
142497 pSub = pItem->pSelect;
142498 if( pSub==0 ) continue;
142499
142500 /* The code for a subquery should only be generated once. */
142501 assert( pItem->addrFillSub==0 )((void) (0));
142502
142503 /* Increment Parse.nHeight by the height of the largest expression
142504 ** tree referred to by this, the parent select. The child select
142505 ** may contain expression trees of at most
142506 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
142507 ** more conservative than necessary, but much easier than enforcing
142508 ** an exact limit.
142509 */
142510 pParse->nHeight += sqlite3SelectExprHeight(p);
142511
142512 /* Make copies of constant WHERE-clause terms in the outer query down
142513 ** inside the subquery. This can help the subquery to run more efficiently.
142514 */
142515 if( OptimizationEnabled(db, SQLITE_PushDown)(((db)->dbOptFlags&(0x00001000))==0)
142516 && (pItem->fg.isCte==0
142517 || (pItem->u2.pCteUse->eM10d!=M10d_Yes0 && pItem->u2.pCteUse->nUse<2))
142518 && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem)
142519 ){
142520#if TREETRACE_ENABLED0
142521 if( sqlite3TreeTrace & 0x100 ){
142522 SELECTTRACE(0x100,pParse,p,
142523 ("After WHERE-clause push-down into subquery %d:\n", pSub->selId));
142524 sqlite3TreeViewSelect(0, p, 0);
142525 }
142526#endif
142527 assert( pItem->pSelect && (pItem->pSelect->selFlags & SF_PushDown)!=0 )((void) (0));
142528 }else{
142529 SELECTTRACE(0x100,pParse,p,("Push-down not possible\n"));
142530 }
142531
142532 zSavedAuthContext = pParse->zAuthContext;
142533 pParse->zAuthContext = pItem->zName;
142534
142535 /* Generate code to implement the subquery
142536 **
142537 ** The subquery is implemented as a co-routine if all of the following are
142538 ** true:
142539 **
142540 ** (1) the subquery is guaranteed to be the outer loop (so that
142541 ** it does not need to be computed more than once), and
142542 ** (2) the subquery is not a CTE that should be materialized
142543 ** (3) the subquery is not part of a left operand for a RIGHT JOIN
142544 */
142545 if( i==0
142546 && (pTabList->nSrc==1
142547 || (pTabList->a[1].fg.jointype&(JT_OUTER0x20|JT_CROSS0x02))!=0) /* (1) */
142548 && (pItem->fg.isCte==0 || pItem->u2.pCteUse->eM10d!=M10d_Yes0) /* (2) */
142549 && (pTabList->a[0].fg.jointype & JT_LTORJ0x40)==0 /* (3) */
142550 ){
142551 /* Implement a co-routine that will return a single row of the result
142552 ** set on each invocation.
142553 */
142554 int addrTop = sqlite3VdbeCurrentAddr(v)+1;
142555
142556 pItem->regReturn = ++pParse->nMem;
142557 sqlite3VdbeAddOp3(v, OP_InitCoroutine10, pItem->regReturn, 0, addrTop);
142558 VdbeComment((v, "%!S", pItem));
142559 pItem->addrFillSub = addrTop;
142560 sqlite3SelectDestInit(&dest, SRT_Coroutine13, pItem->regReturn);
142561 ExplainQueryPlan((pParse, 1, "CO-ROUTINE %!S", pItem))sqlite3VdbeExplain (pParse, 1, "CO-ROUTINE %!S", pItem);
142562 sqlite3Select(pParse, pSub, &dest);
142563 pItem->pTab->nRowLogEst = pSub->nSelectRow;
142564 pItem->fg.viaCoroutine = 1;
142565 pItem->regResult = dest.iSdst;
142566 sqlite3VdbeEndCoroutine(v, pItem->regReturn);
142567 sqlite3VdbeJumpHere(v, addrTop-1);
142568 sqlite3ClearTempRegCache(pParse);
142569 }else if( pItem->fg.isCte && pItem->u2.pCteUse->addrM9e>0 ){
142570 /* This is a CTE for which materialization code has already been
142571 ** generated. Invoke the subroutine to compute the materialization,
142572 ** the make the pItem->iCursor be a copy of the ephemerial table that
142573 ** holds the result of the materialization. */
142574 CteUse *pCteUse = pItem->u2.pCteUse;
142575 sqlite3VdbeAddOp2(v, OP_Gosub9, pCteUse->regRtn, pCteUse->addrM9e);
142576 if( pItem->iCursor!=pCteUse->iCur ){
142577 sqlite3VdbeAddOp2(v, OP_OpenDup115, pItem->iCursor, pCteUse->iCur);
142578 VdbeComment((v, "%!S", pItem));
142579 }
142580 pSub->nSelectRow = pCteUse->nRowEst;
142581 }else if( (pPrior = isSelfJoinView(pTabList, pItem))!=0 ){
142582 /* This view has already been materialized by a prior entry in
142583 ** this same FROM clause. Reuse it. */
142584 if( pPrior->addrFillSub ){
142585 sqlite3VdbeAddOp2(v, OP_Gosub9, pPrior->regReturn, pPrior->addrFillSub);
142586 }
142587 sqlite3VdbeAddOp2(v, OP_OpenDup115, pItem->iCursor, pPrior->iCursor);
142588 pSub->nSelectRow = pPrior->pSelect->nSelectRow;
142589 }else{
142590 /* Materialize the view. If the view is not correlated, generate a
142591 ** subroutine to do the materialization so that subsequent uses of
142592 ** the same view can reuse the materialization. */
142593 int topAddr;
142594 int onceAddr = 0;
142595
142596 pItem->regReturn = ++pParse->nMem;
142597 topAddr = sqlite3VdbeAddOp0(v, OP_Goto8);
142598 pItem->addrFillSub = topAddr+1;
142599 pItem->fg.isMaterialized = 1;
142600 if( pItem->fg.isCorrelated==0 ){
142601 /* If the subquery is not correlated and if we are not inside of
142602 ** a trigger, then we only need to compute the value of the subquery
142603 ** once. */
142604 onceAddr = sqlite3VdbeAddOp0(v, OP_Once14); VdbeCoverage(v);
142605 VdbeComment((v, "materialize %!S", pItem));
142606 }else{
142607 VdbeNoopComment((v, "materialize %!S", pItem));
142608 }
142609 sqlite3SelectDestInit(&dest, SRT_EphemTab12, pItem->iCursor);
142610 ExplainQueryPlan((pParse, 1, "MATERIALIZE %!S", pItem))sqlite3VdbeExplain (pParse, 1, "MATERIALIZE %!S", pItem);
142611 sqlite3Select(pParse, pSub, &dest);
142612 pItem->pTab->nRowLogEst = pSub->nSelectRow;
142613 if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
142614 sqlite3VdbeAddOp2(v, OP_Return67, pItem->regReturn, topAddr+1);
142615 VdbeComment((v, "end %!S", pItem));
142616 sqlite3VdbeJumpHere(v, topAddr);
142617 sqlite3ClearTempRegCache(pParse);
142618 if( pItem->fg.isCte && pItem->fg.isCorrelated==0 ){
142619 CteUse *pCteUse = pItem->u2.pCteUse;
142620 pCteUse->addrM9e = pItem->addrFillSub;
142621 pCteUse->regRtn = pItem->regReturn;
142622 pCteUse->iCur = pItem->iCursor;
142623 pCteUse->nRowEst = pSub->nSelectRow;
142624 }
142625 }
142626 if( db->mallocFailed ) goto select_end;
142627 pParse->nHeight -= sqlite3SelectExprHeight(p);
142628 pParse->zAuthContext = zSavedAuthContext;
142629#endif
142630 }
142631
142632 /* Various elements of the SELECT copied into local variables for
142633 ** convenience */
142634 pEList = p->pEList;
142635 pWhere = p->pWhere;
142636 pGroupBy = p->pGroupBy;
142637 pHaving = p->pHaving;
142638 sDistinct.isTnct = (p->selFlags & SF_Distinct0x0000001)!=0;
142639
142640#if TREETRACE_ENABLED0
142641 if( sqlite3TreeTrace & 0x400 ){
142642 SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
142643 sqlite3TreeViewSelect(0, p, 0);
142644 }
142645#endif
142646
142647 /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
142648 ** if the select-list is the same as the ORDER BY list, then this query
142649 ** can be rewritten as a GROUP BY. In other words, this:
142650 **
142651 ** SELECT DISTINCT xyz FROM ... ORDER BY xyz
142652 **
142653 ** is transformed to:
142654 **
142655 ** SELECT xyz FROM ... GROUP BY xyz ORDER BY xyz
142656 **
142657 ** The second form is preferred as a single index (or temp-table) may be
142658 ** used for both the ORDER BY and DISTINCT processing. As originally
142659 ** written the query must use a temp-table for at least one of the ORDER
142660 ** BY and DISTINCT, and an index or separate temp-table for the other.
142661 */
142662 if( (p->selFlags & (SF_Distinct0x0000001|SF_Aggregate0x0000008))==SF_Distinct0x0000001
142663 && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
142664#ifndef SQLITE_OMIT_WINDOWFUNC
142665 && p->pWin==0
142666#endif
142667 ){
142668 p->selFlags &= ~SF_Distinct0x0000001;
142669 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
142670 p->selFlags |= SF_Aggregate0x0000008;
142671 /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
142672 ** the sDistinct.isTnct is still set. Hence, isTnct represents the
142673 ** original setting of the SF_Distinct flag, not the current setting */
142674 assert( sDistinct.isTnct )((void) (0));
142675 sDistinct.isTnct = 2;
142676
142677#if TREETRACE_ENABLED0
142678 if( sqlite3TreeTrace & 0x400 ){
142679 SELECTTRACE(0x400,pParse,p,("Transform DISTINCT into GROUP BY:\n"));
142680 sqlite3TreeViewSelect(0, p, 0);
142681 }
142682#endif
142683 }
142684
142685 /* If there is an ORDER BY clause, then create an ephemeral index to
142686 ** do the sorting. But this sorting ephemeral index might end up
142687 ** being unused if the data can be extracted in pre-sorted order.
142688 ** If that is the case, then the OP_OpenEphemeral instruction will be
142689 ** changed to an OP_Noop once we figure out that the sorting index is
142690 ** not needed. The sSort.addrSortIndex variable is used to facilitate
142691 ** that change.
142692 */
142693 if( sSort.pOrderBy ){
142694 KeyInfo *pKeyInfo;
142695 pKeyInfo = sqlite3KeyInfoFromExprList(
142696 pParse, sSort.pOrderBy, 0, pEList->nExpr);
142697 sSort.iECursor = pParse->nTab++;
142698 sSort.addrSortIndex =
142699 sqlite3VdbeAddOp4(v, OP_OpenEphemeral118,
142700 sSort.iECursor, sSort.pOrderBy->nExpr+1+pEList->nExpr, 0,
142701 (char*)pKeyInfo, P4_KEYINFO(-8)
142702 );
142703 }else{
142704 sSort.addrSortIndex = -1;
142705 }
142706
142707 /* If the output is destined for a temporary table, open that table.
142708 */
142709 if( pDest->eDest==SRT_EphemTab12 ){
142710 sqlite3VdbeAddOp2(v, OP_OpenEphemeral118, pDest->iSDParm, pEList->nExpr);
142711 if( p->selFlags & SF_NestedFrom0x0000800 ){
142712 /* Delete or NULL-out result columns that will never be used */
142713 int ii;
142714 for(ii=pEList->nExpr-1; ii>0 && pEList->a[ii].fg.bUsed==0; ii--){
142715 sqlite3ExprDelete(db, pEList->a[ii].pExpr);
142716 sqlite3DbFree(db, pEList->a[ii].zEName);
142717 pEList->nExpr--;
142718 }
142719 for(ii=0; ii<pEList->nExpr; ii++){
142720 if( pEList->a[ii].fg.bUsed==0 ) pEList->a[ii].pExpr->op = TK_NULL121;
142721 }
142722 }
142723 }
142724
142725 /* Set the limiter.
142726 */
142727 iEnd = sqlite3VdbeMakeLabel(pParse);
142728 if( (p->selFlags & SF_FixedLimit0x0004000)==0 ){
142729 p->nSelectRow = 320; /* 4 billion rows */
142730 }
142731 computeLimitRegisters(pParse, p, iEnd);
142732 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
142733 sqlite3VdbeChangeOpcode(v, sSort.addrSortIndex, OP_SorterOpen119);
142734 sSort.sortFlags |= SORTFLAG_UseSorter0x01;
142735 }
142736
142737 /* Open an ephemeral index to use for the distinct set.
142738 */
142739 if( p->selFlags & SF_Distinct0x0000001 ){
142740 sDistinct.tabTnct = pParse->nTab++;
142741 sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral118,
142742 sDistinct.tabTnct, 0, 0,
142743 (char*)sqlite3KeyInfoFromExprList(pParse, p->pEList,0,0),
142744 P4_KEYINFO(-8));
142745 sqlite3VdbeChangeP5(v, BTREE_UNORDERED8);
142746 sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED3;
142747 }else{
142748 sDistinct.eTnctType = WHERE_DISTINCT_NOOP0;
142749 }
142750
142751 if( !isAgg && pGroupBy==0 ){
142752 /* No aggregate functions and no GROUP BY clause */
142753 u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT0x0100 : 0)
142754 | (p->selFlags & SF_FixedLimit0x0004000);
142755#ifndef SQLITE_OMIT_WINDOWFUNC
142756 Window *pWin = p->pWin; /* Main window object (or NULL) */
142757 if( pWin ){
142758 sqlite3WindowCodeInit(pParse, p);
142759 }
142760#endif
142761 assert( WHERE_USE_LIMIT==SF_FixedLimit )((void) (0));
142762
142763
142764 /* Begin the database scan. */
142765 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
142766 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy,
142767 p->pEList, p, wctrlFlags, p->nSelectRow);
142768 if( pWInfo==0 ) goto select_end;
142769 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
142770 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
142771 }
142772 if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){
142773 sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo);
142774 }
142775 if( sSort.pOrderBy ){
142776 sSort.nOBSat = sqlite3WhereIsOrdered(pWInfo);
142777 sSort.labelOBLopt = sqlite3WhereOrderByLimitOptLabel(pWInfo);
142778 if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
142779 sSort.pOrderBy = 0;
142780 }
142781 }
142782 SELECTTRACE(1,pParse,p,("WhereBegin returns\n"));
142783
142784 /* If sorting index that was created by a prior OP_OpenEphemeral
142785 ** instruction ended up not being needed, then change the OP_OpenEphemeral
142786 ** into an OP_Noop.
142787 */
142788 if( sSort.addrSortIndex>=0 && sSort.pOrderBy==0 ){
142789 sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
142790 }
142791
142792 assert( p->pEList==pEList )((void) (0));
142793#ifndef SQLITE_OMIT_WINDOWFUNC
142794 if( pWin ){
142795 int addrGosub = sqlite3VdbeMakeLabel(pParse);
142796 int iCont = sqlite3VdbeMakeLabel(pParse);
142797 int iBreak = sqlite3VdbeMakeLabel(pParse);
142798 int regGosub = ++pParse->nMem;
142799
142800 sqlite3WindowCodeStep(pParse, p, pWInfo, regGosub, addrGosub);
142801
142802 sqlite3VdbeAddOp2(v, OP_Goto8, 0, iBreak);
142803 sqlite3VdbeResolveLabel(v, addrGosub);
142804 VdbeNoopComment((v, "inner-loop subroutine"));
142805 sSort.labelOBLopt = 0;
142806 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest, iCont, iBreak);
142807 sqlite3VdbeResolveLabel(v, iCont);
142808 sqlite3VdbeAddOp1(v, OP_Return67, regGosub);
142809 VdbeComment((v, "end inner-loop subroutine"));
142810 sqlite3VdbeResolveLabel(v, iBreak);
142811 }else
142812#endif /* SQLITE_OMIT_WINDOWFUNC */
142813 {
142814 /* Use the standard inner loop. */
142815 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest,
142816 sqlite3WhereContinueLabel(pWInfo),
142817 sqlite3WhereBreakLabel(pWInfo));
142818
142819 /* End the database scan loop.
142820 */
142821 SELECTTRACE(1,pParse,p,("WhereEnd\n"));
142822 sqlite3WhereEnd(pWInfo);
142823 }
142824 }else{
142825 /* This case when there exist aggregate functions or a GROUP BY clause
142826 ** or both */
142827 NameContext sNC; /* Name context for processing aggregate information */
142828 int iAMem; /* First Mem address for storing current GROUP BY */
142829 int iBMem; /* First Mem address for previous GROUP BY */
142830 int iUseFlag; /* Mem address holding flag indicating that at least
142831 ** one row of the input to the aggregator has been
142832 ** processed */
142833 int iAbortFlag; /* Mem address which causes query abort if positive */
142834 int groupBySort; /* Rows come from source in GROUP BY order */
142835 int addrEnd; /* End of processing for this SELECT */
142836 int sortPTab = 0; /* Pseudotable used to decode sorting results */
142837 int sortOut = 0; /* Output register from the sorter */
142838 int orderByGrp = 0; /* True if the GROUP BY and ORDER BY are the same */
142839
142840 /* Remove any and all aliases between the result set and the
142841 ** GROUP BY clause.
142842 */
142843 if( pGroupBy ){
142844 int k; /* Loop counter */
142845 struct ExprList_item *pItem; /* For looping over expression in a list */
142846
142847 for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
142848 pItem->u.x.iAlias = 0;
142849 }
142850 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
142851 pItem->u.x.iAlias = 0;
142852 }
142853 assert( 66==sqlite3LogEst(100) )((void) (0));
142854 if( p->nSelectRow>66 ) p->nSelectRow = 66;
142855
142856 /* If there is both a GROUP BY and an ORDER BY clause and they are
142857 ** identical, then it may be possible to disable the ORDER BY clause
142858 ** on the grounds that the GROUP BY will cause elements to come out
142859 ** in the correct order. It also may not - the GROUP BY might use a
142860 ** database index that causes rows to be grouped together as required
142861 ** but not actually sorted. Either way, record the fact that the
142862 ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
142863 ** variable. */
142864 if( sSort.pOrderBy && pGroupBy->nExpr==sSort.pOrderBy->nExpr ){
142865 int ii;
142866 /* The GROUP BY processing doesn't care whether rows are delivered in
142867 ** ASC or DESC order - only that each group is returned contiguously.
142868 ** So set the ASC/DESC flags in the GROUP BY to match those in the
142869 ** ORDER BY to maximize the chances of rows being delivered in an
142870 ** order that makes the ORDER BY redundant. */
142871 for(ii=0; ii<pGroupBy->nExpr; ii++){
142872 u8 sortFlags;
142873 sortFlags = sSort.pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_DESC0x01;
142874 pGroupBy->a[ii].fg.sortFlags = sortFlags;
142875 }
142876 if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
142877 orderByGrp = 1;
142878 }
142879 }
142880 }else{
142881 assert( 0==sqlite3LogEst(1) )((void) (0));
142882 p->nSelectRow = 0;
142883 }
142884
142885 /* Create a label to jump to when we want to abort the query */
142886 addrEnd = sqlite3VdbeMakeLabel(pParse);
142887
142888 /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
142889 ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
142890 ** SELECT statement.
142891 */
142892 pAggInfo = sqlite3DbMallocZero(db, sizeof(*pAggInfo) );
142893 if( pAggInfo ){
142894 sqlite3ParserAddCleanup(pParse,
142895 (void(*)(sqlite3*,void*))agginfoFree, pAggInfo);
142896 testcase( pParse->earlyCleanup );
142897 }
142898 if( db->mallocFailed ){
142899 goto select_end;
142900 }
142901 pAggInfo->selId = p->selId;
142902 memset(&sNC, 0, sizeof(sNC));
142903 sNC.pParse = pParse;
142904 sNC.pSrcList = pTabList;
142905 sNC.uNC.pAggInfo = pAggInfo;
142906 VVA_ONLY( sNC.ncFlags = NC_UAggInfo; )
142907 pAggInfo->mnReg = pParse->nMem+1;
142908 pAggInfo->nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
142909 pAggInfo->pGroupBy = pGroupBy;
142910 sqlite3ExprAnalyzeAggList(&sNC, pEList);
142911 sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy);
142912 if( pHaving ){
142913 if( pGroupBy ){
142914 assert( pWhere==p->pWhere )((void) (0));
142915 assert( pHaving==p->pHaving )((void) (0));
142916 assert( pGroupBy==p->pGroupBy )((void) (0));
142917 havingToWhere(pParse, p);
142918 pWhere = p->pWhere;
142919 }
142920 sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
142921 }
142922 pAggInfo->nAccumulator = pAggInfo->nColumn;
142923 if( p->pGroupBy==0 && p->pHaving==0 && pAggInfo->nFunc==1 ){
142924 minMaxFlag = minMaxQuery(db, pAggInfo->aFunc[0].pFExpr, &pMinMaxOrderBy);
142925 }else{
142926 minMaxFlag = WHERE_ORDERBY_NORMAL0x0000;
142927 }
142928 for(i=0; i<pAggInfo->nFunc; i++){
142929 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
142930 assert( ExprUseXList(pExpr) )((void) (0));
142931 sNC.ncFlags |= NC_InAggFunc0x020000;
142932 sqlite3ExprAnalyzeAggList(&sNC, pExpr->x.pList);
142933#ifndef SQLITE_OMIT_WINDOWFUNC
142934 assert( !IsWindowFunc(pExpr) )((void) (0));
142935 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
142936 sqlite3ExprAnalyzeAggregates(&sNC, pExpr->y.pWin->pFilter);
142937 }
142938#endif
142939 sNC.ncFlags &= ~NC_InAggFunc0x020000;
142940 }
142941 pAggInfo->mxReg = pParse->nMem;
142942 if( db->mallocFailed ) goto select_end;
142943#if TREETRACE_ENABLED0
142944 if( sqlite3TreeTrace & 0x400 ){
142945 int ii;
142946 SELECTTRACE(0x400,pParse,p,("After aggregate analysis %p:\n", pAggInfo));
142947 sqlite3TreeViewSelect(0, p, 0);
142948 if( minMaxFlag ){
142949 sqlite3DebugPrintf("MIN/MAX Optimization (0x%02x) adds:\n", minMaxFlag);
142950 sqlite3TreeViewExprList(0, pMinMaxOrderBy, 0, "ORDERBY");
142951 }
142952 for(ii=0; ii<pAggInfo->nColumn; ii++){
142953 sqlite3DebugPrintf("agg-column[%d] iMem=%d\n",
142954 ii, pAggInfo->aCol[ii].iMem);
142955 sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0);
142956 }
142957 for(ii=0; ii<pAggInfo->nFunc; ii++){
142958 sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
142959 ii, pAggInfo->aFunc[ii].iMem);
142960 sqlite3TreeViewExpr(0, pAggInfo->aFunc[ii].pFExpr, 0);
142961 }
142962 }
142963#endif
142964
142965
142966 /* Processing for aggregates with GROUP BY is very different and
142967 ** much more complex than aggregates without a GROUP BY.
142968 */
142969 if( pGroupBy ){
142970 KeyInfo *pKeyInfo; /* Keying information for the group by clause */
142971 int addr1; /* A-vs-B comparision jump */
142972 int addrOutputRow; /* Start of subroutine that outputs a result row */
142973 int regOutputRow; /* Return address register for output subroutine */
142974 int addrSetAbort; /* Set the abort flag and return */
142975 int addrTopOfLoop; /* Top of the input loop */
142976 int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
142977 int addrReset; /* Subroutine for resetting the accumulator */
142978 int regReset; /* Return address register for reset subroutine */
142979 ExprList *pDistinct = 0;
142980 u16 distFlag = 0;
142981 int eDist = WHERE_DISTINCT_NOOP0;
142982
142983 if( pAggInfo->nFunc==1
142984 && pAggInfo->aFunc[0].iDistinct>=0
142985 && ALWAYS(pAggInfo->aFunc[0].pFExpr!=0)(pAggInfo->aFunc[0].pFExpr!=0)
142986 && ALWAYS(ExprUseXList(pAggInfo->aFunc[0].pFExpr))((((pAggInfo->aFunc[0].pFExpr)->flags&0x001000)==0)
)
142987 && pAggInfo->aFunc[0].pFExpr->x.pList!=0
142988 ){
142989 Expr *pExpr = pAggInfo->aFunc[0].pFExpr->x.pList->a[0].pExpr;
142990 pExpr = sqlite3ExprDup(db, pExpr, 0);
142991 pDistinct = sqlite3ExprListDup(db, pGroupBy, 0);
142992 pDistinct = sqlite3ExprListAppend(pParse, pDistinct, pExpr);
142993 distFlag = pDistinct ? (WHERE_WANT_DISTINCT0x0100|WHERE_AGG_DISTINCT0x0400) : 0;
142994 }
142995
142996 /* If there is a GROUP BY clause we might need a sorting index to
142997 ** implement it. Allocate that sorting index now. If it turns out
142998 ** that we do not need it after all, the OP_SorterOpen instruction
142999 ** will be converted into a Noop.
143000 */
143001 pAggInfo->sortingIdx = pParse->nTab++;
143002 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pGroupBy,
143003 0, pAggInfo->nColumn);
143004 addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen119,
143005 pAggInfo->sortingIdx, pAggInfo->nSortingColumn,
143006 0, (char*)pKeyInfo, P4_KEYINFO(-8));
143007
143008 /* Initialize memory locations used by GROUP BY aggregate processing
143009 */
143010 iUseFlag = ++pParse->nMem;
143011 iAbortFlag = ++pParse->nMem;
143012 regOutputRow = ++pParse->nMem;
143013 addrOutputRow = sqlite3VdbeMakeLabel(pParse);
143014 regReset = ++pParse->nMem;
143015 addrReset = sqlite3VdbeMakeLabel(pParse);
143016 iAMem = pParse->nMem + 1;
143017 pParse->nMem += pGroupBy->nExpr;
143018 iBMem = pParse->nMem + 1;
143019 pParse->nMem += pGroupBy->nExpr;
143020 sqlite3VdbeAddOp2(v, OP_Integer71, 0, iAbortFlag);
143021 VdbeComment((v, "clear abort flag"));
143022 sqlite3VdbeAddOp3(v, OP_Null75, 0, iAMem, iAMem+pGroupBy->nExpr-1);
143023
143024 /* Begin a loop that will extract all source rows in GROUP BY order.
143025 ** This might involve two separate loops with an OP_Sort in between, or
143026 ** it might be a single loop that uses an index to extract information
143027 ** in the right order to begin with.
143028 */
143029 sqlite3VdbeAddOp2(v, OP_Gosub9, regReset, addrReset);
143030 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
143031 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, pDistinct,
143032 0, (sDistinct.isTnct==2 ? WHERE_DISTINCTBY0x0080 : WHERE_GROUPBY0x0040)
143033 | (orderByGrp ? WHERE_SORTBYGROUP0x0200 : 0) | distFlag, 0
143034 );
143035 if( pWInfo==0 ){
143036 sqlite3ExprListDelete(db, pDistinct);
143037 goto select_end;
143038 }
143039 eDist = sqlite3WhereIsDistinct(pWInfo);
143040 SELECTTRACE(1,pParse,p,("WhereBegin returns\n"));
143041 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
143042 /* The optimizer is able to deliver rows in group by order so
143043 ** we do not have to sort. The OP_OpenEphemeral table will be
143044 ** cancelled later because we still need to use the pKeyInfo
143045 */
143046 groupBySort = 0;
143047 }else{
143048 /* Rows are coming out in undetermined order. We have to push
143049 ** each row into a sorting index, terminate the first loop,
143050 ** then loop over the sorting index in order to get the output
143051 ** in sorted order
143052 */
143053 int regBase;
143054 int regRecord;
143055 int nCol;
143056 int nGroupBy;
143057
143058 explainTempTable(pParse,
143059 (sDistinct.isTnct && (p->selFlags&SF_Distinct0x0000001)==0) ?
143060 "DISTINCT" : "GROUP BY");
143061
143062 groupBySort = 1;
143063 nGroupBy = pGroupBy->nExpr;
143064 nCol = nGroupBy;
143065 j = nGroupBy;
143066 for(i=0; i<pAggInfo->nColumn; i++){
143067 if( pAggInfo->aCol[i].iSorterColumn>=j ){
143068 nCol++;
143069 j++;
143070 }
143071 }
143072 regBase = sqlite3GetTempRange(pParse, nCol);
143073 sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0, 0);
143074 j = nGroupBy;
143075 for(i=0; i<pAggInfo->nColumn; i++){
143076 struct AggInfo_col *pCol = &pAggInfo->aCol[i];
143077 if( pCol->iSorterColumn>=j ){
143078 int r1 = j + regBase;
143079 sqlite3ExprCodeGetColumnOfTable(v,
143080 pCol->pTab, pCol->iTable, pCol->iColumn, r1);
143081 j++;
143082 }
143083 }
143084 regRecord = sqlite3GetTempReg(pParse);
143085 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regBase, nCol, regRecord);
143086 sqlite3VdbeAddOp2(v, OP_SorterInsert139, pAggInfo->sortingIdx, regRecord);
143087 sqlite3ReleaseTempReg(pParse, regRecord);
143088 sqlite3ReleaseTempRange(pParse, regBase, nCol);
143089 SELECTTRACE(1,pParse,p,("WhereEnd\n"));
143090 sqlite3WhereEnd(pWInfo);
143091 pAggInfo->sortingIdxPTab = sortPTab = pParse->nTab++;
143092 sortOut = sqlite3GetTempReg(pParse);
143093 sqlite3VdbeAddOp3(v, OP_OpenPseudo121, sortPTab, sortOut, nCol);
143094 sqlite3VdbeAddOp2(v, OP_SorterSort33, pAggInfo->sortingIdx, addrEnd);
143095 VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
143096 pAggInfo->useSortingIdx = 1;
143097 }
143098
143099 /* If the index or temporary table used by the GROUP BY sort
143100 ** will naturally deliver rows in the order required by the ORDER BY
143101 ** clause, cancel the ephemeral table open coded earlier.
143102 **
143103 ** This is an optimization - the correct answer should result regardless.
143104 ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER to
143105 ** disable this optimization for testing purposes. */
143106 if( orderByGrp && OptimizationEnabled(db, SQLITE_GroupByOrder)(((db)->dbOptFlags&(0x00000004))==0)
143107 && (groupBySort || sqlite3WhereIsSorted(pWInfo))
143108 ){
143109 sSort.pOrderBy = 0;
143110 sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
143111 }
143112
143113 /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
143114 ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
143115 ** Then compare the current GROUP BY terms against the GROUP BY terms
143116 ** from the previous row currently stored in a0, a1, a2...
143117 */
143118 addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
143119 if( groupBySort ){
143120 sqlite3VdbeAddOp3(v, OP_SorterData133, pAggInfo->sortingIdx,
143121 sortOut, sortPTab);
143122 }
143123 for(j=0; j<pGroupBy->nExpr; j++){
143124 if( groupBySort ){
143125 sqlite3VdbeAddOp3(v, OP_Column94, sortPTab, j, iBMem+j);
143126 }else{
143127 pAggInfo->directMode = 1;
143128 sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
143129 }
143130 }
143131 sqlite3VdbeAddOp4(v, OP_Compare90, iAMem, iBMem, pGroupBy->nExpr,
143132 (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO(-8));
143133 addr1 = sqlite3VdbeCurrentAddr(v);
143134 sqlite3VdbeAddOp3(v, OP_Jump13, addr1+1, 0, addr1+1); VdbeCoverage(v);
143135
143136 /* Generate code that runs whenever the GROUP BY changes.
143137 ** Changes in the GROUP BY are detected by the previous code
143138 ** block. If there were no changes, this block is skipped.
143139 **
143140 ** This code copies current group by terms in b0,b1,b2,...
143141 ** over to a0,a1,a2. It then calls the output subroutine
143142 ** and resets the aggregate accumulator registers in preparation
143143 ** for the next GROUP BY batch.
143144 */
143145 sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
143146 sqlite3VdbeAddOp2(v, OP_Gosub9, regOutputRow, addrOutputRow);
143147 VdbeComment((v, "output one row"));
143148 sqlite3VdbeAddOp2(v, OP_IfPos49, iAbortFlag, addrEnd); VdbeCoverage(v);
143149 VdbeComment((v, "check abort flag"));
143150 sqlite3VdbeAddOp2(v, OP_Gosub9, regReset, addrReset);
143151 VdbeComment((v, "reset accumulator"));
143152
143153 /* Update the aggregate accumulators based on the content of
143154 ** the current row
143155 */
143156 sqlite3VdbeJumpHere(v, addr1);
143157 updateAccumulator(pParse, iUseFlag, pAggInfo, eDist);
143158 sqlite3VdbeAddOp2(v, OP_Integer71, 1, iUseFlag);
143159 VdbeComment((v, "indicate data in accumulator"));
143160
143161 /* End of the loop
143162 */
143163 if( groupBySort ){
143164 sqlite3VdbeAddOp2(v, OP_SorterNext36, pAggInfo->sortingIdx,addrTopOfLoop);
143165 VdbeCoverage(v);
143166 }else{
143167 SELECTTRACE(1,pParse,p,("WhereEnd\n"));
143168 sqlite3WhereEnd(pWInfo);
143169 sqlite3VdbeChangeToNoop(v, addrSortingIdx);
143170 }
143171 sqlite3ExprListDelete(db, pDistinct);
143172
143173 /* Output the final row of result
143174 */
143175 sqlite3VdbeAddOp2(v, OP_Gosub9, regOutputRow, addrOutputRow);
143176 VdbeComment((v, "output final row"));
143177
143178 /* Jump over the subroutines
143179 */
143180 sqlite3VdbeGoto(v, addrEnd);
143181
143182 /* Generate a subroutine that outputs a single row of the result
143183 ** set. This subroutine first looks at the iUseFlag. If iUseFlag
143184 ** is less than or equal to zero, the subroutine is a no-op. If
143185 ** the processing calls for the query to abort, this subroutine
143186 ** increments the iAbortFlag memory location before returning in
143187 ** order to signal the caller to abort.
143188 */
143189 addrSetAbort = sqlite3VdbeCurrentAddr(v);
143190 sqlite3VdbeAddOp2(v, OP_Integer71, 1, iAbortFlag);
143191 VdbeComment((v, "set abort flag"));
143192 sqlite3VdbeAddOp1(v, OP_Return67, regOutputRow);
143193 sqlite3VdbeResolveLabel(v, addrOutputRow);
143194 addrOutputRow = sqlite3VdbeCurrentAddr(v);
143195 sqlite3VdbeAddOp2(v, OP_IfPos49, iUseFlag, addrOutputRow+2);
143196 VdbeCoverage(v);
143197 VdbeComment((v, "Groupby result generator entry point"));
143198 sqlite3VdbeAddOp1(v, OP_Return67, regOutputRow);
143199 finalizeAggFunctions(pParse, pAggInfo);
143200 sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL0x10);
143201 selectInnerLoop(pParse, p, -1, &sSort,
143202 &sDistinct, pDest,
143203 addrOutputRow+1, addrSetAbort);
143204 sqlite3VdbeAddOp1(v, OP_Return67, regOutputRow);
143205 VdbeComment((v, "end groupby result generator"));
143206
143207 /* Generate a subroutine that will reset the group-by accumulator
143208 */
143209 sqlite3VdbeResolveLabel(v, addrReset);
143210 resetAccumulator(pParse, pAggInfo);
143211 sqlite3VdbeAddOp2(v, OP_Integer71, 0, iUseFlag);
143212 VdbeComment((v, "indicate accumulator empty"));
143213 sqlite3VdbeAddOp1(v, OP_Return67, regReset);
143214
143215 if( distFlag!=0 && eDist!=WHERE_DISTINCT_NOOP0 ){
143216 struct AggInfo_func *pF = &pAggInfo->aFunc[0];
143217 fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr);
143218 }
143219 } /* endif pGroupBy. Begin aggregate queries without GROUP BY: */
143220 else {
143221 Table *pTab;
143222 if( (pTab = isSimpleCount(p, pAggInfo))!=0 ){
143223 /* If isSimpleCount() returns a pointer to a Table structure, then
143224 ** the SQL statement is of the form:
143225 **
143226 ** SELECT count(*) FROM <tbl>
143227 **
143228 ** where the Table structure returned represents table <tbl>.
143229 **
143230 ** This statement is so common that it is optimized specially. The
143231 ** OP_Count instruction is executed either on the intkey table that
143232 ** contains the data for table <tbl> or on one of its indexes. It
143233 ** is better to execute the op on an index, as indexes are almost
143234 ** always spread across less pages than their corresponding tables.
143235 */
143236 const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
143237 const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */
143238 Index *pIdx; /* Iterator variable */
143239 KeyInfo *pKeyInfo = 0; /* Keyinfo for scanned index */
143240 Index *pBest = 0; /* Best index found so far */
143241 Pgno iRoot = pTab->tnum; /* Root page of scanned b-tree */
143242
143243 sqlite3CodeVerifySchema(pParse, iDb);
143244 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
143245
143246 /* Search for the index that has the lowest scan cost.
143247 **
143248 ** (2011-04-15) Do not do a full scan of an unordered index.
143249 **
143250 ** (2013-10-03) Do not count the entries in a partial index.
143251 **
143252 ** In practice the KeyInfo structure will not be used. It is only
143253 ** passed to keep OP_OpenRead happy.
143254 */
143255 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ) pBest = sqlite3PrimaryKeyIndex(pTab);
143256 if( !p->pSrc->a[0].fg.notIndexed ){
143257 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
143258 if( pIdx->bUnordered==0
143259 && pIdx->szIdxRow<pTab->szTabRow
143260 && pIdx->pPartIdxWhere==0
143261 && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
143262 ){
143263 pBest = pIdx;
143264 }
143265 }
143266 }
143267 if( pBest ){
143268 iRoot = pBest->tnum;
143269 pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pBest);
143270 }
143271
143272 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
143273 sqlite3VdbeAddOp4Int(v, OP_OpenRead112, iCsr, (int)iRoot, iDb, 1);
143274 if( pKeyInfo ){
143275 sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO(-8));
143276 }
143277 sqlite3VdbeAddOp2(v, OP_Count98, iCsr, pAggInfo->aFunc[0].iMem);
143278 sqlite3VdbeAddOp1(v, OP_Close122, iCsr);
143279 explainSimpleCount(pParse, pTab, pBest);
143280 }else{
143281 int regAcc = 0; /* "populate accumulators" flag */
143282 ExprList *pDistinct = 0;
143283 u16 distFlag = 0;
143284 int eDist;
143285
143286 /* If there are accumulator registers but no min() or max() functions
143287 ** without FILTER clauses, allocate register regAcc. Register regAcc
143288 ** will contain 0 the first time the inner loop runs, and 1 thereafter.
143289 ** The code generated by updateAccumulator() uses this to ensure
143290 ** that the accumulator registers are (a) updated only once if
143291 ** there are no min() or max functions or (b) always updated for the
143292 ** first row visited by the aggregate, so that they are updated at
143293 ** least once even if the FILTER clause means the min() or max()
143294 ** function visits zero rows. */
143295 if( pAggInfo->nAccumulator ){
143296 for(i=0; i<pAggInfo->nFunc; i++){
143297 if( ExprHasProperty(pAggInfo->aFunc[i].pFExpr, EP_WinFunc)(((pAggInfo->aFunc[i].pFExpr)->flags&(0x1000000))!=
0)
){
143298 continue;
143299 }
143300 if( pAggInfo->aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL0x0020 ){
143301 break;
143302 }
143303 }
143304 if( i==pAggInfo->nFunc ){
143305 regAcc = ++pParse->nMem;
143306 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regAcc);
143307 }
143308 }else if( pAggInfo->nFunc==1 && pAggInfo->aFunc[0].iDistinct>=0 ){
143309 assert( ExprUseXList(pAggInfo->aFunc[0].pFExpr) )((void) (0));
143310 pDistinct = pAggInfo->aFunc[0].pFExpr->x.pList;
143311 distFlag = pDistinct ? (WHERE_WANT_DISTINCT0x0100|WHERE_AGG_DISTINCT0x0400) : 0;
143312 }
143313
143314 /* This case runs if the aggregate has no GROUP BY clause. The
143315 ** processing is much simpler since there is only a single row
143316 ** of output.
143317 */
143318 assert( p->pGroupBy==0 )((void) (0));
143319 resetAccumulator(pParse, pAggInfo);
143320
143321 /* If this query is a candidate for the min/max optimization, then
143322 ** minMaxFlag will have been previously set to either
143323 ** WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX and pMinMaxOrderBy will
143324 ** be an appropriate ORDER BY expression for the optimization.
143325 */
143326 assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 )((void) (0));
143327 assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 )((void) (0));
143328
143329 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
143330 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
143331 pDistinct, 0, minMaxFlag|distFlag, 0);
143332 if( pWInfo==0 ){
143333 goto select_end;
143334 }
143335 SELECTTRACE(1,pParse,p,("WhereBegin returns\n"));
143336 eDist = sqlite3WhereIsDistinct(pWInfo);
143337 updateAccumulator(pParse, regAcc, pAggInfo, eDist);
143338 if( eDist!=WHERE_DISTINCT_NOOP0 ){
143339 struct AggInfo_func *pF = pAggInfo->aFunc;
143340 if( pF ){
143341 fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr);
143342 }
143343 }
143344
143345 if( regAcc ) sqlite3VdbeAddOp2(v, OP_Integer71, 1, regAcc);
143346 if( minMaxFlag ){
143347 sqlite3WhereMinMaxOptEarlyOut(v, pWInfo);
143348 }
143349 SELECTTRACE(1,pParse,p,("WhereEnd\n"));
143350 sqlite3WhereEnd(pWInfo);
143351 finalizeAggFunctions(pParse, pAggInfo);
143352 }
143353
143354 sSort.pOrderBy = 0;
143355 sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL0x10);
143356 selectInnerLoop(pParse, p, -1, 0, 0,
143357 pDest, addrEnd, addrEnd);
143358 }
143359 sqlite3VdbeResolveLabel(v, addrEnd);
143360
143361 } /* endif aggregate query */
143362
143363 if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED3 ){
143364 explainTempTable(pParse, "DISTINCT");
143365 }
143366
143367 /* If there is an ORDER BY clause, then we need to sort the results
143368 ** and send them to the callback one by one.
143369 */
143370 if( sSort.pOrderBy ){
143371 explainTempTable(pParse,
143372 sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
143373 assert( p->pEList==pEList )((void) (0));
143374 generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
143375 }
143376
143377 /* Jump here to skip this query
143378 */
143379 sqlite3VdbeResolveLabel(v, iEnd);
143380
143381 /* The SELECT has been coded. If there is an error in the Parse structure,
143382 ** set the return code to 1. Otherwise 0. */
143383 rc = (pParse->nErr>0);
143384
143385 /* Control jumps to here if an error is encountered above, or upon
143386 ** successful coding of the SELECT.
143387 */
143388select_end:
143389 assert( db->mallocFailed==0 || db->mallocFailed==1 )((void) (0));
143390 assert( db->mallocFailed==0 || pParse->nErr!=0 )((void) (0));
143391 sqlite3ExprListDelete(db, pMinMaxOrderBy);
143392#ifdef SQLITE_DEBUG
143393 if( pAggInfo && !db->mallocFailed ){
143394 for(i=0; i<pAggInfo->nColumn; i++){
143395 Expr *pExpr = pAggInfo->aCol[i].pCExpr;
143396 assert( pExpr!=0 )((void) (0));
143397 assert( pExpr->pAggInfo==pAggInfo )((void) (0));
143398 assert( pExpr->iAgg==i )((void) (0));
143399 }
143400 for(i=0; i<pAggInfo->nFunc; i++){
143401 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
143402 assert( pExpr!=0 )((void) (0));
143403 assert( pExpr->pAggInfo==pAggInfo )((void) (0));
143404 assert( pExpr->iAgg==i )((void) (0));
143405 }
143406 }
143407#endif
143408
143409#if TREETRACE_ENABLED0
143410 SELECTTRACE(0x1,pParse,p,("end processing\n"));
143411 if( (sqlite3TreeTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)sqlite3VdbeExplainParent(pParse)==0 ){
143412 sqlite3TreeViewSelect(0, p, 0);
143413 }
143414#endif
143415 ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
143416 return rc;
143417}
143418
143419/************** End of select.c **********************************************/
143420/************** Begin file table.c *******************************************/
143421/*
143422** 2001 September 15
143423**
143424** The author disclaims copyright to this source code. In place of
143425** a legal notice, here is a blessing:
143426**
143427** May you do good and not evil.
143428** May you find forgiveness for yourself and forgive others.
143429** May you share freely, never taking more than you give.
143430**
143431*************************************************************************
143432** This file contains the sqlite3_get_table() and sqlite3_free_table()
143433** interface routines. These are just wrappers around the main
143434** interface routine of sqlite3_exec().
143435**
143436** These routines are in a separate files so that they will not be linked
143437** if they are not used.
143438*/
143439/* #include "sqliteInt.h" */
143440
143441#ifndef SQLITE_OMIT_GET_TABLE
143442
143443/*
143444** This structure is used to pass data from sqlite3_get_table() through
143445** to the callback function is uses to build the result.
143446*/
143447typedef struct TabResult {
143448 char **azResult; /* Accumulated output */
143449 char *zErrMsg; /* Error message text, if an error occurs */
143450 u32 nAlloc; /* Slots allocated for azResult[] */
143451 u32 nRow; /* Number of rows in the result */
143452 u32 nColumn; /* Number of columns in the result */
143453 u32 nData; /* Slots used in azResult[]. (nRow+1)*nColumn */
143454 int rc; /* Return code from sqlite3_exec() */
143455} TabResult;
143456
143457/*
143458** This routine is called once for each row in the result table. Its job
143459** is to fill in the TabResult structure appropriately, allocating new
143460** memory as necessary.
143461*/
143462static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
143463 TabResult *p = (TabResult*)pArg; /* Result accumulator */
143464 int need; /* Slots needed in p->azResult[] */
143465 int i; /* Loop counter */
143466 char *z; /* A single column of result */
143467
143468 /* Make sure there is enough space in p->azResult to hold everything
143469 ** we need to remember from this invocation of the callback.
143470 */
143471 if( p->nRow==0 && argv!=0 ){
143472 need = nCol*2;
143473 }else{
143474 need = nCol;
143475 }
143476 if( p->nData + need > p->nAlloc ){
143477 char **azNew;
143478 p->nAlloc = p->nAlloc*2 + need;
143479 azNew = sqlite3Realloc( p->azResult, sizeof(char*)*p->nAlloc );
143480 if( azNew==0 ) goto malloc_failed;
143481 p->azResult = azNew;
143482 }
143483
143484 /* If this is the first row, then generate an extra row containing
143485 ** the names of all columns.
143486 */
143487 if( p->nRow==0 ){
143488 p->nColumn = nCol;
143489 for(i=0; i<nCol; i++){
143490 z = sqlite3_mprintf("%s", colv[i]);
143491 if( z==0 ) goto malloc_failed;
143492 p->azResult[p->nData++] = z;
143493 }
143494 }else if( (int)p->nColumn!=nCol ){
143495 sqlite3_free(p->zErrMsg);
143496 p->zErrMsg = sqlite3_mprintf(
143497 "sqlite3_get_table() called with two or more incompatible queries"
143498 );
143499 p->rc = SQLITE_ERROR1;
143500 return 1;
143501 }
143502
143503 /* Copy over the row data
143504 */
143505 if( argv!=0 ){
143506 for(i=0; i<nCol; i++){
143507 if( argv[i]==0 ){
143508 z = 0;
143509 }else{
143510 int n = sqlite3Strlen30(argv[i])+1;
143511 z = sqlite3_malloc64( n );
143512 if( z==0 ) goto malloc_failed;
143513 memcpy(z, argv[i], n);
143514 }
143515 p->azResult[p->nData++] = z;
143516 }
143517 p->nRow++;
143518 }
143519 return 0;
143520
143521malloc_failed:
143522 p->rc = SQLITE_NOMEM_BKPT7;
143523 return 1;
143524}
143525
143526/*
143527** Query the database. But instead of invoking a callback for each row,
143528** malloc() for space to hold the result and return the entire results
143529** at the conclusion of the call.
143530**
143531** The result that is written to ***pazResult is held in memory obtained
143532** from malloc(). But the caller cannot free this memory directly.
143533** Instead, the entire table should be passed to sqlite3_free_table() when
143534** the calling procedure is finished using it.
143535*/
143536SQLITE_API int sqlite3_get_table(
143537 sqlite3 *db, /* The database on which the SQL executes */
143538 const char *zSql, /* The SQL to be executed */
143539 char ***pazResult, /* Write the result table here */
143540 int *pnRow, /* Write the number of rows in the result here */
143541 int *pnColumn, /* Write the number of columns of result here */
143542 char **pzErrMsg /* Write error messages here */
143543){
143544 int rc;
143545 TabResult res;
143546
143547#ifdef SQLITE_ENABLE_API_ARMOR1
143548 if( !sqlite3SafetyCheckOk(db) || pazResult==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(143548);
143549#endif
143550 *pazResult = 0;
143551 if( pnColumn ) *pnColumn = 0;
143552 if( pnRow ) *pnRow = 0;
143553 if( pzErrMsg ) *pzErrMsg = 0;
143554 res.zErrMsg = 0;
143555 res.nRow = 0;
143556 res.nColumn = 0;
143557 res.nData = 1;
143558 res.nAlloc = 20;
143559 res.rc = SQLITE_OK0;
143560 res.azResult = sqlite3_malloc64(sizeof(char*)*res.nAlloc );
143561 if( res.azResult==0 ){
143562 db->errCode = SQLITE_NOMEM7;
143563 return SQLITE_NOMEM_BKPT7;
143564 }
143565 res.azResult[0] = 0;
143566 rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
143567 assert( sizeof(res.azResult[0])>= sizeof(res.nData) )((void) (0));
143568 res.azResult[0] = SQLITE_INT_TO_PTR(res.nData)((void*)(long int)(res.nData));
143569 if( (rc&0xff)==SQLITE_ABORT4 ){
143570 sqlite3_free_table(&res.azResult[1]);
143571 if( res.zErrMsg ){
143572 if( pzErrMsg ){
143573 sqlite3_free(*pzErrMsg);
143574 *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
143575 }
143576 sqlite3_free(res.zErrMsg);
143577 }
143578 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
143579 return res.rc;
143580 }
143581 sqlite3_free(res.zErrMsg);
143582 if( rc!=SQLITE_OK0 ){
143583 sqlite3_free_table(&res.azResult[1]);
143584 return rc;
143585 }
143586 if( res.nAlloc>res.nData ){
143587 char **azNew;
143588 azNew = sqlite3Realloc( res.azResult, sizeof(char*)*res.nData );
143589 if( azNew==0 ){
143590 sqlite3_free_table(&res.azResult[1]);
143591 db->errCode = SQLITE_NOMEM7;
143592 return SQLITE_NOMEM_BKPT7;
143593 }
143594 res.azResult = azNew;
143595 }
143596 *pazResult = &res.azResult[1];
143597 if( pnColumn ) *pnColumn = res.nColumn;
143598 if( pnRow ) *pnRow = res.nRow;
143599 return rc;
143600}
143601
143602/*
143603** This routine frees the space the sqlite3_get_table() malloced.
143604*/
143605SQLITE_API void sqlite3_free_table(
143606 char **azResult /* Result returned from sqlite3_get_table() */
143607){
143608 if( azResult ){
143609 int i, n;
143610 azResult--;
143611 assert( azResult!=0 )((void) (0));
143612 n = SQLITE_PTR_TO_INT(azResult[0])((int)(long int)(azResult[0]));
143613 for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
143614 sqlite3_free(azResult);
143615 }
143616}
143617
143618#endif /* SQLITE_OMIT_GET_TABLE */
143619
143620/************** End of table.c ***********************************************/
143621/************** Begin file trigger.c *****************************************/
143622/*
143623**
143624** The author disclaims copyright to this source code. In place of
143625** a legal notice, here is a blessing:
143626**
143627** May you do good and not evil.
143628** May you find forgiveness for yourself and forgive others.
143629** May you share freely, never taking more than you give.
143630**
143631*************************************************************************
143632** This file contains the implementation for TRIGGERs
143633*/
143634/* #include "sqliteInt.h" */
143635
143636#ifndef SQLITE_OMIT_TRIGGER
143637/*
143638** Delete a linked list of TriggerStep structures.
143639*/
143640SQLITE_PRIVATEstatic void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
143641 while( pTriggerStep ){
143642 TriggerStep * pTmp = pTriggerStep;
143643 pTriggerStep = pTriggerStep->pNext;
143644
143645 sqlite3ExprDelete(db, pTmp->pWhere);
143646 sqlite3ExprListDelete(db, pTmp->pExprList);
143647 sqlite3SelectDelete(db, pTmp->pSelect);
143648 sqlite3IdListDelete(db, pTmp->pIdList);
143649 sqlite3UpsertDelete(db, pTmp->pUpsert);
143650 sqlite3SrcListDelete(db, pTmp->pFrom);
143651 sqlite3DbFree(db, pTmp->zSpan);
143652
143653 sqlite3DbFree(db, pTmp);
143654 }
143655}
143656
143657/*
143658** Given table pTab, return a list of all the triggers attached to
143659** the table. The list is connected by Trigger.pNext pointers.
143660**
143661** All of the triggers on pTab that are in the same database as pTab
143662** are already attached to pTab->pTrigger. But there might be additional
143663** triggers on pTab in the TEMP schema. This routine prepends all
143664** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
143665** and returns the combined list.
143666**
143667** To state it another way: This routine returns a list of all triggers
143668** that fire off of pTab. The list will include any TEMP triggers on
143669** pTab as well as the triggers lised in pTab->pTrigger.
143670*/
143671SQLITE_PRIVATEstatic Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
143672 Schema *pTmpSchema; /* Schema of the pTab table */
143673 Trigger *pList; /* List of triggers to return */
143674 HashElem *p; /* Loop variable for TEMP triggers */
143675
143676 assert( pParse->disableTriggers==0 )((void) (0));
143677 pTmpSchema = pParse->db->aDb[1].pSchema;
143678 p = sqliteHashFirst(&pTmpSchema->trigHash)((&pTmpSchema->trigHash)->first);
143679 pList = pTab->pTrigger;
143680 while( p ){
143681 Trigger *pTrig = (Trigger *)sqliteHashData(p)((p)->data);
143682 if( pTrig->pTabSchema==pTab->pSchema
143683 && pTrig->table
143684 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
143685 && pTrig->pTabSchema!=pTmpSchema
143686 ){
143687 pTrig->pNext = pList;
143688 pList = pTrig;
143689 }else if( pTrig->op==TK_RETURNING150 ){
143690#ifndef SQLITE_OMIT_VIRTUALTABLE
143691 assert( pParse->db->pVtabCtx==0 )((void) (0));
143692#endif
143693 assert( pParse->bReturning )((void) (0));
143694 assert( &(pParse->u1.pReturning->retTrig) == pTrig )((void) (0));
143695 pTrig->table = pTab->zName;
143696 pTrig->pTabSchema = pTab->pSchema;
143697 pTrig->pNext = pList;
143698 pList = pTrig;
143699 }
143700 p = sqliteHashNext(p)((p)->next);
143701 }
143702#if 0
143703 if( pList ){
143704 Trigger *pX;
143705 printf("Triggers for %s:", pTab->zName);
143706 for(pX=pList; pX; pX=pX->pNext){
143707 printf(" %s", pX->zName);
143708 }
143709 printf("\n");
143710 fflush(stdoutstdout);
143711 }
143712#endif
143713 return pList;
143714}
143715
143716/*
143717** This is called by the parser when it sees a CREATE TRIGGER statement
143718** up to the point of the BEGIN before the trigger actions. A Trigger
143719** structure is generated based on the information available and stored
143720** in pParse->pNewTrigger. After the trigger actions have been parsed, the
143721** sqlite3FinishTrigger() function is called to complete the trigger
143722** construction process.
143723*/
143724SQLITE_PRIVATEstatic void sqlite3BeginTrigger(
143725 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
143726 Token *pName1, /* The name of the trigger */
143727 Token *pName2, /* The name of the trigger */
143728 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
143729 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
143730 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
143731 SrcList *pTableName,/* The name of the table/view the trigger applies to */
143732 Expr *pWhen, /* WHEN clause */
143733 int isTemp, /* True if the TEMPORARY keyword is present */
143734 int noErr /* Suppress errors if the trigger already exists */
143735){
143736 Trigger *pTrigger = 0; /* The new trigger */
143737 Table *pTab; /* Table that the trigger fires off of */
143738 char *zName = 0; /* Name of the trigger */
143739 sqlite3 *db = pParse->db; /* The database connection */
143740 int iDb; /* The database to store the trigger in */
143741 Token *pName; /* The unqualified db name */
143742 DbFixer sFix; /* State vector for the DB fixer */
143743
143744 assert( pName1!=0 )((void) (0)); /* pName1->z might be NULL, but not pName1 itself */
143745 assert( pName2!=0 )((void) (0));
143746 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE )((void) (0));
143747 assert( op>0 && op<0xff )((void) (0));
143748 if( isTemp ){
143749 /* If TEMP was specified, then the trigger name may not be qualified. */
143750 if( pName2->n>0 ){
143751 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
143752 goto trigger_cleanup;
143753 }
143754 iDb = 1;
143755 pName = pName1;
143756 }else{
143757 /* Figure out the db that the trigger will be created in */
143758 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
143759 if( iDb<0 ){
143760 goto trigger_cleanup;
143761 }
143762 }
143763 if( !pTableName || db->mallocFailed ){
143764 goto trigger_cleanup;
143765 }
143766
143767 /* A long-standing parser bug is that this syntax was allowed:
143768 **
143769 ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
143770 ** ^^^^^^^^
143771 **
143772 ** To maintain backwards compatibility, ignore the database
143773 ** name on pTableName if we are reparsing out of the schema table
143774 */
143775 if( db->init.busy && iDb!=1 ){
143776 sqlite3DbFree(db, pTableName->a[0].zDatabase);
143777 pTableName->a[0].zDatabase = 0;
143778 }
143779
143780 /* If the trigger name was unqualified, and the table is a temp table,
143781 ** then set iDb to 1 to create the trigger in the temporary database.
143782 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
143783 ** exist, the error is caught by the block below.
143784 */
143785 pTab = sqlite3SrcListLookup(pParse, pTableName);
143786 if( db->init.busy==0 && pName2->n==0 && pTab
143787 && pTab->pSchema==db->aDb[1].pSchema ){
143788 iDb = 1;
143789 }
143790
143791 /* Ensure the table name matches database name and that the table exists */
143792 if( db->mallocFailed ) goto trigger_cleanup;
143793 assert( pTableName->nSrc==1 )((void) (0));
143794 sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName);
143795 if( sqlite3FixSrcList(&sFix, pTableName) ){
143796 goto trigger_cleanup;
143797 }
143798 pTab = sqlite3SrcListLookup(pParse, pTableName);
143799 if( !pTab ){
143800 /* The table does not exist. */
143801 goto trigger_orphan_error;
143802 }
143803 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
143804 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
143805 goto trigger_orphan_error;
143806 }
143807
143808 /* Check that the trigger name is not reserved and that no trigger of the
143809 ** specified name exists */
143810 zName = sqlite3NameFromToken(db, pName);
143811 if( zName==0 ){
143812 assert( db->mallocFailed )((void) (0));
143813 goto trigger_cleanup;
143814 }
143815 if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){
143816 goto trigger_cleanup;
143817 }
143818 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
143819 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
143820 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
143821 if( !noErr ){
143822 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
143823 }else{
143824 assert( !db->init.busy )((void) (0));
143825 sqlite3CodeVerifySchema(pParse, iDb);
143826 }
143827 goto trigger_cleanup;
143828 }
143829 }
143830
143831 /* Do not create a trigger on a system table */
143832 if( sqlite3StrNICmpsqlite3_strnicmp(pTab->zName, "sqlite_", 7)==0 ){
143833 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
143834 goto trigger_cleanup;
143835 }
143836
143837 /* INSTEAD of triggers are only for views and views only support INSTEAD
143838 ** of triggers.
143839 */
143840 if( IsView(pTab)((pTab)->eTabType==2) && tr_tm!=TK_INSTEAD65 ){
143841 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
143842 (tr_tm == TK_BEFORE33)?"BEFORE":"AFTER", pTableName->a);
143843 goto trigger_orphan_error;
143844 }
143845 if( !IsView(pTab)((pTab)->eTabType==2) && tr_tm==TK_INSTEAD65 ){
143846 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
143847 " trigger on table: %S", pTableName->a);
143848 goto trigger_orphan_error;
143849 }
143850
143851#ifndef SQLITE_OMIT_AUTHORIZATION
143852 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
143853 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
143854 int code = SQLITE_CREATE_TRIGGER7;
143855 const char *zDb = db->aDb[iTabDb].zDbSName;
143856 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
143857 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER5;
143858 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
143859 goto trigger_cleanup;
143860 }
143861 if( sqlite3AuthCheck(pParse, SQLITE_INSERT18, SCHEMA_TABLE(iTabDb)((!0)&&(iTabDb==1)?"sqlite_temp_master":"sqlite_master"
)
,0,zDb)){
143862 goto trigger_cleanup;
143863 }
143864 }
143865#endif
143866
143867 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
143868 ** cannot appear on views. So we might as well translate every
143869 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
143870 ** elsewhere.
143871 */
143872 if (tr_tm == TK_INSTEAD65){
143873 tr_tm = TK_BEFORE33;
143874 }
143875
143876 /* Build the Trigger object */
143877 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
143878 if( pTrigger==0 ) goto trigger_cleanup;
143879 pTrigger->zName = zName;
143880 zName = 0;
143881 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
143882 pTrigger->pSchema = db->aDb[iDb].pSchema;
143883 pTrigger->pTabSchema = pTab->pSchema;
143884 pTrigger->op = (u8)op;
143885 pTrigger->tr_tm = tr_tm==TK_BEFORE33 ? TRIGGER_BEFORE1 : TRIGGER_AFTER2;
143886 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
143887 sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
143888 pTrigger->pWhen = pWhen;
143889 pWhen = 0;
143890 }else{
143891 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE0x0001);
143892 }
143893 pTrigger->pColumns = pColumns;
143894 pColumns = 0;
143895 assert( pParse->pNewTrigger==0 )((void) (0));
143896 pParse->pNewTrigger = pTrigger;
143897
143898trigger_cleanup:
143899 sqlite3DbFree(db, zName);
143900 sqlite3SrcListDelete(db, pTableName);
143901 sqlite3IdListDelete(db, pColumns);
143902 sqlite3ExprDelete(db, pWhen);
143903 if( !pParse->pNewTrigger ){
143904 sqlite3DeleteTrigger(db, pTrigger);
143905 }else{
143906 assert( pParse->pNewTrigger==pTrigger )((void) (0));
143907 }
143908 return;
143909
143910trigger_orphan_error:
143911 if( db->init.iDb==1 ){
143912 /* Ticket #3810.
143913 ** Normally, whenever a table is dropped, all associated triggers are
143914 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
143915 ** and the table is dropped by a different database connection, the
143916 ** trigger is not visible to the database connection that does the
143917 ** drop so the trigger cannot be dropped. This results in an
143918 ** "orphaned trigger" - a trigger whose associated table is missing.
143919 **
143920 ** 2020-11-05 see also https://sqlite.org/forum/forumpost/157dc791df
143921 */
143922 db->init.orphanTrigger = 1;
143923 }
143924 goto trigger_cleanup;
143925}
143926
143927/*
143928** This routine is called after all of the trigger actions have been parsed
143929** in order to complete the process of building the trigger.
143930*/
143931SQLITE_PRIVATEstatic void sqlite3FinishTrigger(
143932 Parse *pParse, /* Parser context */
143933 TriggerStep *pStepList, /* The triggered program */
143934 Token *pAll /* Token that describes the complete CREATE TRIGGER */
143935){
143936 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
143937 char *zName; /* Name of trigger */
143938 sqlite3 *db = pParse->db; /* The database */
143939 DbFixer sFix; /* Fixer object */
143940 int iDb; /* Database containing the trigger */
143941 Token nameToken; /* Trigger name for error reporting */
143942
143943 pParse->pNewTrigger = 0;
143944 if( NEVER(pParse->nErr)(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
143945 zName = pTrig->zName;
143946 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
143947 pTrig->step_list = pStepList;
143948 while( pStepList ){
143949 pStepList->pTrig = pTrig;
143950 pStepList = pStepList->pNext;
143951 }
143952 sqlite3TokenInit(&nameToken, pTrig->zName);
143953 sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
143954 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
143955 || sqlite3FixExpr(&sFix, pTrig->pWhen)
143956 ){
143957 goto triggerfinish_cleanup;
143958 }
143959
143960#ifndef SQLITE_OMIT_ALTERTABLE
143961 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
143962 assert( !db->init.busy )((void) (0));
143963 pParse->pNewTrigger = pTrig;
143964 pTrig = 0;
143965 }else
143966#endif
143967
143968 /* if we are not initializing,
143969 ** build the sqlite_schema entry
143970 */
143971 if( !db->init.busy ){
143972 Vdbe *v;
143973 char *z;
143974
143975 /* Make an entry in the sqlite_schema table */
143976 v = sqlite3GetVdbe(pParse);
143977 if( v==0 ) goto triggerfinish_cleanup;
143978 sqlite3BeginWriteOperation(pParse, 0, iDb);
143979 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
143980 testcase( z==0 );
143981 sqlite3NestedParse(pParse,
143982 "INSERT INTO %Q." LEGACY_SCHEMA_TABLE"sqlite_master"
143983 " VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
143984 db->aDb[iDb].zDbSName, zName,
143985 pTrig->table, z);
143986 sqlite3DbFree(db, z);
143987 sqlite3ChangeCookie(pParse, iDb);
143988 sqlite3VdbeAddParseSchemaOp(v, iDb,
143989 sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName), 0);
143990 }
143991
143992 if( db->init.busy ){
143993 Trigger *pLink = pTrig;
143994 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
143995 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
143996 assert( pLink!=0 )((void) (0));
143997 pTrig = sqlite3HashInsert(pHash, zName, pTrig);
143998 if( pTrig ){
143999 sqlite3OomFault(db);
144000 }else if( pLink->pSchema==pLink->pTabSchema ){
144001 Table *pTab;
144002 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
144003 assert( pTab!=0 )((void) (0));
144004 pLink->pNext = pTab->pTrigger;
144005 pTab->pTrigger = pLink;
144006 }
144007 }
144008
144009triggerfinish_cleanup:
144010 sqlite3DeleteTrigger(db, pTrig);
144011 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger )((void) (0));
144012 sqlite3DeleteTriggerStep(db, pStepList);
144013}
144014
144015/*
144016** Duplicate a range of text from an SQL statement, then convert all
144017** whitespace characters into ordinary space characters.
144018*/
144019static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
144020 char *z = sqlite3DbSpanDup(db, zStart, zEnd);
144021 int i;
144022 if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x01) ) z[i] = ' ';
144023 return z;
144024}
144025
144026/*
144027** Turn a SELECT statement (that the pSelect parameter points to) into
144028** a trigger step. Return a pointer to a TriggerStep structure.
144029**
144030** The parser calls this routine when it finds a SELECT statement in
144031** body of a TRIGGER.
144032*/
144033SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerSelectStep(
144034 sqlite3 *db, /* Database connection */
144035 Select *pSelect, /* The SELECT statement */
144036 const char *zStart, /* Start of SQL text */
144037 const char *zEnd /* End of SQL text */
144038){
144039 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
144040 if( pTriggerStep==0 ) {
144041 sqlite3SelectDelete(db, pSelect);
144042 return 0;
144043 }
144044 pTriggerStep->op = TK_SELECT138;
144045 pTriggerStep->pSelect = pSelect;
144046 pTriggerStep->orconf = OE_Default11;
144047 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
144048 return pTriggerStep;
144049}
144050
144051/*
144052** Allocate space to hold a new trigger step. The allocated space
144053** holds both the TriggerStep object and the TriggerStep.target.z string.
144054**
144055** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
144056*/
144057static TriggerStep *triggerStepAllocate(
144058 Parse *pParse, /* Parser context */
144059 u8 op, /* Trigger opcode */
144060 Token *pName, /* The target name */
144061 const char *zStart, /* Start of SQL text */
144062 const char *zEnd /* End of SQL text */
144063){
144064 sqlite3 *db = pParse->db;
144065 TriggerStep *pTriggerStep;
144066
144067 if( pParse->nErr ) return 0;
144068 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
144069 if( pTriggerStep ){
144070 char *z = (char*)&pTriggerStep[1];
144071 memcpy(z, pName->z, pName->n);
144072 sqlite3Dequote(z);
144073 pTriggerStep->zTarget = z;
144074 pTriggerStep->op = op;
144075 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
144076 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
144077 sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
144078 }
144079 }
144080 return pTriggerStep;
144081}
144082
144083/*
144084** Build a trigger step out of an INSERT statement. Return a pointer
144085** to the new trigger step.
144086**
144087** The parser calls this routine when it sees an INSERT inside the
144088** body of a trigger.
144089*/
144090SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerInsertStep(
144091 Parse *pParse, /* Parser */
144092 Token *pTableName, /* Name of the table into which we insert */
144093 IdList *pColumn, /* List of columns in pTableName to insert into */
144094 Select *pSelect, /* A SELECT statement that supplies values */
144095 u8 orconf, /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
144096 Upsert *pUpsert, /* ON CONFLICT clauses for upsert */
144097 const char *zStart, /* Start of SQL text */
144098 const char *zEnd /* End of SQL text */
144099){
144100 sqlite3 *db = pParse->db;
144101 TriggerStep *pTriggerStep;
144102
144103 assert(pSelect != 0 || db->mallocFailed)((void) (0));
144104
144105 pTriggerStep = triggerStepAllocate(pParse, TK_INSERT127, pTableName,zStart,zEnd);
144106 if( pTriggerStep ){
144107 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
144108 pTriggerStep->pSelect = pSelect;
144109 pSelect = 0;
144110 }else{
144111 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE0x0001);
144112 }
144113 pTriggerStep->pIdList = pColumn;
144114 pTriggerStep->pUpsert = pUpsert;
144115 pTriggerStep->orconf = orconf;
144116 if( pUpsert ){
144117 sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget);
144118 }
144119 }else{
144120 testcase( pColumn );
144121 sqlite3IdListDelete(db, pColumn);
144122 testcase( pUpsert );
144123 sqlite3UpsertDelete(db, pUpsert);
144124 }
144125 sqlite3SelectDelete(db, pSelect);
144126
144127 return pTriggerStep;
144128}
144129
144130/*
144131** Construct a trigger step that implements an UPDATE statement and return
144132** a pointer to that trigger step. The parser calls this routine when it
144133** sees an UPDATE statement inside the body of a CREATE TRIGGER.
144134*/
144135SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerUpdateStep(
144136 Parse *pParse, /* Parser */
144137 Token *pTableName, /* Name of the table to be updated */
144138 SrcList *pFrom, /* FROM clause for an UPDATE-FROM, or NULL */
144139 ExprList *pEList, /* The SET clause: list of column and new values */
144140 Expr *pWhere, /* The WHERE clause */
144141 u8 orconf, /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
144142 const char *zStart, /* Start of SQL text */
144143 const char *zEnd /* End of SQL text */
144144){
144145 sqlite3 *db = pParse->db;
144146 TriggerStep *pTriggerStep;
144147
144148 pTriggerStep = triggerStepAllocate(pParse, TK_UPDATE129, pTableName,zStart,zEnd);
144149 if( pTriggerStep ){
144150 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
144151 pTriggerStep->pExprList = pEList;
144152 pTriggerStep->pWhere = pWhere;
144153 pTriggerStep->pFrom = pFrom;
144154 pEList = 0;
144155 pWhere = 0;
144156 pFrom = 0;
144157 }else{
144158 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE0x0001);
144159 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE0x0001);
144160 pTriggerStep->pFrom = sqlite3SrcListDup(db, pFrom, EXPRDUP_REDUCE0x0001);
144161 }
144162 pTriggerStep->orconf = orconf;
144163 }
144164 sqlite3ExprListDelete(db, pEList);
144165 sqlite3ExprDelete(db, pWhere);
144166 sqlite3SrcListDelete(db, pFrom);
144167 return pTriggerStep;
144168}
144169
144170/*
144171** Construct a trigger step that implements a DELETE statement and return
144172** a pointer to that trigger step. The parser calls this routine when it
144173** sees a DELETE statement inside the body of a CREATE TRIGGER.
144174*/
144175SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerDeleteStep(
144176 Parse *pParse, /* Parser */
144177 Token *pTableName, /* The table from which rows are deleted */
144178 Expr *pWhere, /* The WHERE clause */
144179 const char *zStart, /* Start of SQL text */
144180 const char *zEnd /* End of SQL text */
144181){
144182 sqlite3 *db = pParse->db;
144183 TriggerStep *pTriggerStep;
144184
144185 pTriggerStep = triggerStepAllocate(pParse, TK_DELETE128, pTableName,zStart,zEnd);
144186 if( pTriggerStep ){
144187 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
144188 pTriggerStep->pWhere = pWhere;
144189 pWhere = 0;
144190 }else{
144191 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE0x0001);
144192 }
144193 pTriggerStep->orconf = OE_Default11;
144194 }
144195 sqlite3ExprDelete(db, pWhere);
144196 return pTriggerStep;
144197}
144198
144199/*
144200** Recursively delete a Trigger structure
144201*/
144202SQLITE_PRIVATEstatic void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
144203 if( pTrigger==0 || pTrigger->bReturning ) return;
144204 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
144205 sqlite3DbFree(db, pTrigger->zName);
144206 sqlite3DbFree(db, pTrigger->table);
144207 sqlite3ExprDelete(db, pTrigger->pWhen);
144208 sqlite3IdListDelete(db, pTrigger->pColumns);
144209 sqlite3DbFree(db, pTrigger);
144210}
144211
144212/*
144213** This function is called to drop a trigger from the database schema.
144214**
144215** This may be called directly from the parser and therefore identifies
144216** the trigger by name. The sqlite3DropTriggerPtr() routine does the
144217** same job as this routine except it takes a pointer to the trigger
144218** instead of the trigger name.
144219**/
144220SQLITE_PRIVATEstatic void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
144221 Trigger *pTrigger = 0;
144222 int i;
144223 const char *zDb;
144224 const char *zName;
144225 sqlite3 *db = pParse->db;
144226
144227 if( db->mallocFailed ) goto drop_trigger_cleanup;
144228 if( SQLITE_OK0!=sqlite3ReadSchema(pParse) ){
144229 goto drop_trigger_cleanup;
144230 }
144231
144232 assert( pName->nSrc==1 )((void) (0));
144233 zDb = pName->a[0].zDatabase;
144234 zName = pName->a[0].zName;
144235 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
144236 for(i=OMIT_TEMPDB0; i<db->nDb; i++){
144237 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
144238 if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
144239 assert( sqlite3SchemaMutexHeld(db, j, 0) )((void) (0));
144240 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
144241 if( pTrigger ) break;
144242 }
144243 if( !pTrigger ){
144244 if( !noErr ){
144245 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName->a);
144246 }else{
144247 sqlite3CodeVerifyNamedSchema(pParse, zDb);
144248 }
144249 pParse->checkSchema = 1;
144250 goto drop_trigger_cleanup;
144251 }
144252 sqlite3DropTriggerPtr(pParse, pTrigger);
144253
144254drop_trigger_cleanup:
144255 sqlite3SrcListDelete(db, pName);
144256}
144257
144258/*
144259** Return a pointer to the Table structure for the table that a trigger
144260** is set on.
144261*/
144262static Table *tableOfTrigger(Trigger *pTrigger){
144263 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
144264}
144265
144266
144267/*
144268** Drop a trigger given a pointer to that trigger.
144269*/
144270SQLITE_PRIVATEstatic void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
144271 Table *pTable;
144272 Vdbe *v;
144273 sqlite3 *db = pParse->db;
144274 int iDb;
144275
144276 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
144277 assert( iDb>=0 && iDb<db->nDb )((void) (0));
144278 pTable = tableOfTrigger(pTrigger);
144279 assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 )((void) (0));
144280#ifndef SQLITE_OMIT_AUTHORIZATION
144281 if( pTable ){
144282 int code = SQLITE_DROP_TRIGGER16;
144283 const char *zDb = db->aDb[iDb].zDbSName;
144284 const char *zTab = SCHEMA_TABLE(iDb)((!0)&&(iDb==1)?"sqlite_temp_master":"sqlite_master");
144285 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER14;
144286 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
144287 sqlite3AuthCheck(pParse, SQLITE_DELETE9, zTab, 0, zDb) ){
144288 return;
144289 }
144290 }
144291#endif
144292
144293 /* Generate code to destroy the database record of the trigger.
144294 */
144295 if( (v = sqlite3GetVdbe(pParse))!=0 ){
144296 sqlite3NestedParse(pParse,
144297 "DELETE FROM %Q." LEGACY_SCHEMA_TABLE"sqlite_master" " WHERE name=%Q AND type='trigger'",
144298 db->aDb[iDb].zDbSName, pTrigger->zName
144299 );
144300 sqlite3ChangeCookie(pParse, iDb);
144301 sqlite3VdbeAddOp4(v, OP_DropTrigger154, iDb, 0, 0, pTrigger->zName, 0);
144302 }
144303}
144304
144305/*
144306** Remove a trigger from the hash tables of the sqlite* pointer.
144307*/
144308SQLITE_PRIVATEstatic void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
144309 Trigger *pTrigger;
144310 Hash *pHash;
144311
144312 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
144313 pHash = &(db->aDb[iDb].pSchema->trigHash);
144314 pTrigger = sqlite3HashInsert(pHash, zName, 0);
144315 if( ALWAYS(pTrigger)(pTrigger) ){
144316 if( pTrigger->pSchema==pTrigger->pTabSchema ){
144317 Table *pTab = tableOfTrigger(pTrigger);
144318 if( pTab ){
144319 Trigger **pp;
144320 for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
144321 if( *pp==pTrigger ){
144322 *pp = (*pp)->pNext;
144323 break;
144324 }
144325 }
144326 }
144327 }
144328 sqlite3DeleteTrigger(db, pTrigger);
144329 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
144330 }
144331}
144332
144333/*
144334** pEList is the SET clause of an UPDATE statement. Each entry
144335** in pEList is of the format <id>=<expr>. If any of the entries
144336** in pEList have an <id> which matches an identifier in pIdList,
144337** then return TRUE. If pIdList==NULL, then it is considered a
144338** wildcard that matches anything. Likewise if pEList==NULL then
144339** it matches anything so always return true. Return false only
144340** if there is no match.
144341*/
144342static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
144343 int e;
144344 if( pIdList==0 || NEVER(pEList==0)(pEList==0) ) return 1;
144345 for(e=0; e<pEList->nExpr; e++){
144346 if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1;
144347 }
144348 return 0;
144349}
144350
144351/*
144352** Return true if any TEMP triggers exist
144353*/
144354static int tempTriggersExist(sqlite3 *db){
144355 if( NEVER(db->aDb[1].pSchema==0)(db->aDb[1].pSchema==0) ) return 0;
144356 if( sqliteHashFirst(&db->aDb[1].pSchema->trigHash)((&db->aDb[1].pSchema->trigHash)->first)==0 ) return 0;
144357 return 1;
144358}
144359
144360/*
144361** Return a list of all triggers on table pTab if there exists at least
144362** one trigger that must be fired when an operation of type 'op' is
144363** performed on the table, and, if that operation is an UPDATE, if at
144364** least one of the columns in pChanges is being modified.
144365*/
144366static SQLITE_NOINLINE__attribute__((noinline)) Trigger *triggersReallyExist(
144367 Parse *pParse, /* Parse context */
144368 Table *pTab, /* The table the contains the triggers */
144369 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
144370 ExprList *pChanges, /* Columns that change in an UPDATE statement */
144371 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
144372){
144373 int mask = 0;
144374 Trigger *pList = 0;
144375 Trigger *p;
144376
144377 pList = sqlite3TriggerList(pParse, pTab);
144378 assert( pList==0 || IsVirtual(pTab)==0((void) (0))
144379 || (pList->bReturning && pList->pNext==0) )((void) (0));
144380 if( pList!=0 ){
144381 p = pList;
144382 if( (pParse->db->flags & SQLITE_EnableTrigger0x00040000)==0
144383 && pTab->pTrigger!=0
144384 ){
144385 /* The SQLITE_DBCONFIG_ENABLE_TRIGGER setting is off. That means that
144386 ** only TEMP triggers are allowed. Truncate the pList so that it
144387 ** includes only TEMP triggers */
144388 if( pList==pTab->pTrigger ){
144389 pList = 0;
144390 goto exit_triggers_exist;
144391 }
144392 while( ALWAYS(p->pNext)(p->pNext) && p->pNext!=pTab->pTrigger ) p = p->pNext;
144393 p->pNext = 0;
144394 p = pList;
144395 }
144396 do{
144397 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
144398 mask |= p->tr_tm;
144399 }else if( p->op==TK_RETURNING150 ){
144400 /* The first time a RETURNING trigger is seen, the "op" value tells
144401 ** us what time of trigger it should be. */
144402 assert( sqlite3IsToplevel(pParse) )((void) (0));
144403 p->op = op;
144404 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
144405 if( op!=TK_INSERT127 ){
144406 sqlite3ErrorMsg(pParse,
144407 "%s RETURNING is not available on virtual tables",
144408 op==TK_DELETE128 ? "DELETE" : "UPDATE");
144409 }
144410 p->tr_tm = TRIGGER_BEFORE1;
144411 }else{
144412 p->tr_tm = TRIGGER_AFTER2;
144413 }
144414 mask |= p->tr_tm;
144415 }else if( p->bReturning && p->op==TK_INSERT127 && op==TK_UPDATE129
144416 && sqlite3IsToplevel(pParse)((pParse)->pToplevel==0) ){
144417 /* Also fire a RETURNING trigger for an UPSERT */
144418 mask |= p->tr_tm;
144419 }
144420 p = p->pNext;
144421 }while( p );
144422 }
144423exit_triggers_exist:
144424 if( pMask ){
144425 *pMask = mask;
144426 }
144427 return (mask ? pList : 0);
144428}
144429SQLITE_PRIVATEstatic Trigger *sqlite3TriggersExist(
144430 Parse *pParse, /* Parse context */
144431 Table *pTab, /* The table the contains the triggers */
144432 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
144433 ExprList *pChanges, /* Columns that change in an UPDATE statement */
144434 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
144435){
144436 assert( pTab!=0 )((void) (0));
144437 if( (pTab->pTrigger==0 && !tempTriggersExist(pParse->db))
144438 || pParse->disableTriggers
144439 ){
144440 if( pMask ) *pMask = 0;
144441 return 0;
144442 }
144443 return triggersReallyExist(pParse,pTab,op,pChanges,pMask);
144444}
144445
144446/*
144447** Convert the pStep->zTarget string into a SrcList and return a pointer
144448** to that SrcList.
144449**
144450** This routine adds a specific database name, if needed, to the target when
144451** forming the SrcList. This prevents a trigger in one database from
144452** referring to a target in another database. An exception is when the
144453** trigger is in TEMP in which case it can refer to any other database it
144454** wants.
144455*/
144456SQLITE_PRIVATEstatic SrcList *sqlite3TriggerStepSrc(
144457 Parse *pParse, /* The parsing context */
144458 TriggerStep *pStep /* The trigger containing the target token */
144459){
144460 sqlite3 *db = pParse->db;
144461 SrcList *pSrc; /* SrcList to be returned */
144462 char *zName = sqlite3DbStrDup(db, pStep->zTarget);
144463 pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
144464 assert( pSrc==0 || pSrc->nSrc==1 )((void) (0));
144465 assert( zName || pSrc==0 )((void) (0));
144466 if( pSrc ){
144467 Schema *pSchema = pStep->pTrig->pSchema;
144468 pSrc->a[0].zName = zName;
144469 if( pSchema!=db->aDb[1].pSchema ){
144470 pSrc->a[0].pSchema = pSchema;
144471 }
144472 if( pStep->pFrom ){
144473 SrcList *pDup = sqlite3SrcListDup(db, pStep->pFrom, 0);
144474 if( pDup && pDup->nSrc>1 && !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
144475 Select *pSubquery;
144476 Token as;
144477 pSubquery = sqlite3SelectNew(pParse,0,pDup,0,0,0,0,SF_NestedFrom0x0000800,0);
144478 as.n = 0;
144479 as.z = 0;
144480 pDup = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0);
144481 }
144482 pSrc = sqlite3SrcListAppendList(pParse, pSrc, pDup);
144483 }
144484 }else{
144485 sqlite3DbFree(db, zName);
144486 }
144487 return pSrc;
144488}
144489
144490/*
144491** Return true if the pExpr term from the RETURNING clause argument
144492** list is of the form "*". Raise an error if the terms if of the
144493** form "table.*".
144494*/
144495static int isAsteriskTerm(
144496 Parse *pParse, /* Parsing context */
144497 Expr *pTerm /* A term in the RETURNING clause */
144498){
144499 assert( pTerm!=0 )((void) (0));
144500 if( pTerm->op==TK_ASTERISK180 ) return 1;
144501 if( pTerm->op!=TK_DOT141 ) return 0;
144502 assert( pTerm->pRight!=0 )((void) (0));
144503 assert( pTerm->pLeft!=0 )((void) (0));
144504 if( pTerm->pRight->op!=TK_ASTERISK180 ) return 0;
144505 sqlite3ErrorMsg(pParse, "RETURNING may not use \"TABLE.*\" wildcards");
144506 return 1;
144507}
144508
144509/* The input list pList is the list of result set terms from a RETURNING
144510** clause. The table that we are returning from is pTab.
144511**
144512** This routine makes a copy of the pList, and at the same time expands
144513** any "*" wildcards to be the complete set of columns from pTab.
144514*/
144515static ExprList *sqlite3ExpandReturning(
144516 Parse *pParse, /* Parsing context */
144517 ExprList *pList, /* The arguments to RETURNING */
144518 Table *pTab /* The table being updated */
144519){
144520 ExprList *pNew = 0;
144521 sqlite3 *db = pParse->db;
144522 int i;
144523
144524 for(i=0; i<pList->nExpr; i++){
144525 Expr *pOldExpr = pList->a[i].pExpr;
144526 if( NEVER(pOldExpr==0)(pOldExpr==0) ) continue;
144527 if( isAsteriskTerm(pParse, pOldExpr) ){
144528 int jj;
144529 for(jj=0; jj<pTab->nCol; jj++){
144530 Expr *pNewExpr;
144531 if( IsHiddenColumn(pTab->aCol+jj)(((pTab->aCol+jj)->colFlags & 0x0002)!=0) ) continue;
144532 pNewExpr = sqlite3Expr(db, TK_ID59, pTab->aCol[jj].zCnName);
144533 pNew = sqlite3ExprListAppend(pParse, pNew, pNewExpr);
144534 if( !db->mallocFailed ){
144535 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
144536 pItem->zEName = sqlite3DbStrDup(db, pTab->aCol[jj].zCnName);
144537 pItem->fg.eEName = ENAME_NAME0;
144538 }
144539 }
144540 }else{
144541 Expr *pNewExpr = sqlite3ExprDup(db, pOldExpr, 0);
144542 pNew = sqlite3ExprListAppend(pParse, pNew, pNewExpr);
144543 if( !db->mallocFailed && ALWAYS(pList->a[i].zEName!=0)(pList->a[i].zEName!=0) ){
144544 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
144545 pItem->zEName = sqlite3DbStrDup(db, pList->a[i].zEName);
144546 pItem->fg.eEName = pList->a[i].fg.eEName;
144547 }
144548 }
144549 }
144550 return pNew;
144551}
144552
144553/*
144554** Generate code for the RETURNING trigger. Unlike other triggers
144555** that invoke a subprogram in the bytecode, the code for RETURNING
144556** is generated in-line.
144557*/
144558static void codeReturningTrigger(
144559 Parse *pParse, /* Parse context */
144560 Trigger *pTrigger, /* The trigger step that defines the RETURNING */
144561 Table *pTab, /* The table to code triggers from */
144562 int regIn /* The first in an array of registers */
144563){
144564 Vdbe *v = pParse->pVdbe;
144565 sqlite3 *db = pParse->db;
144566 ExprList *pNew;
144567 Returning *pReturning;
144568 Select sSelect;
144569 SrcList sFrom;
144570
144571 assert( v!=0 )((void) (0));
144572 assert( pParse->bReturning )((void) (0));
144573 assert( db->pParse==pParse )((void) (0));
144574 pReturning = pParse->u1.pReturning;
144575 assert( pTrigger == &(pReturning->retTrig) )((void) (0));
144576 memset(&sSelect, 0, sizeof(sSelect));
144577 memset(&sFrom, 0, sizeof(sFrom));
144578 sSelect.pEList = sqlite3ExprListDup(db, pReturning->pReturnEL, 0);
144579 sSelect.pSrc = &sFrom;
144580 sFrom.nSrc = 1;
144581 sFrom.a[0].pTab = pTab;
144582 sFrom.a[0].iCursor = -1;
144583 sqlite3SelectPrep(pParse, &sSelect, 0);
144584 if( pParse->nErr==0 ){
144585 assert( db->mallocFailed==0 )((void) (0));
144586 sqlite3GenerateColumnNames(pParse, &sSelect);
144587 }
144588 sqlite3ExprListDelete(db, sSelect.pEList);
144589 pNew = sqlite3ExpandReturning(pParse, pReturning->pReturnEL, pTab);
144590 if( !db->mallocFailed ){
144591 NameContext sNC;
144592 memset(&sNC, 0, sizeof(sNC));
144593 if( pReturning->nRetCol==0 ){
144594 pReturning->nRetCol = pNew->nExpr;
144595 pReturning->iRetCur = pParse->nTab++;
144596 }
144597 sNC.pParse = pParse;
144598 sNC.uNC.iBaseReg = regIn;
144599 sNC.ncFlags = NC_UBaseReg0x000400;
144600 pParse->eTriggerOp = pTrigger->op;
144601 pParse->pTriggerTab = pTab;
144602 if( sqlite3ResolveExprListNames(&sNC, pNew)==SQLITE_OK0
144603 && ALWAYS(!db->mallocFailed)(!db->mallocFailed)
144604 ){
144605 int i;
144606 int nCol = pNew->nExpr;
144607 int reg = pParse->nMem+1;
144608 pParse->nMem += nCol+2;
144609 pReturning->iRetReg = reg;
144610 for(i=0; i<nCol; i++){
144611 Expr *pCol = pNew->a[i].pExpr;
144612 assert( pCol!=0 )((void) (0)); /* Due to !db->mallocFailed ~9 lines above */
144613 sqlite3ExprCodeFactorable(pParse, pCol, reg+i);
144614 if( sqlite3ExprAffinity(pCol)==SQLITE_AFF_REAL0x45 ){
144615 sqlite3VdbeAddOp1(v, OP_RealAffinity87, reg+i);
144616 }
144617 }
144618 sqlite3VdbeAddOp3(v, OP_MakeRecord97, reg, i, reg+i);
144619 sqlite3VdbeAddOp2(v, OP_NewRowid127, pReturning->iRetCur, reg+i+1);
144620 sqlite3VdbeAddOp3(v, OP_Insert128, pReturning->iRetCur, reg+i, reg+i+1);
144621 }
144622 }
144623 sqlite3ExprListDelete(db, pNew);
144624 pParse->eTriggerOp = 0;
144625 pParse->pTriggerTab = 0;
144626}
144627
144628
144629
144630/*
144631** Generate VDBE code for the statements inside the body of a single
144632** trigger.
144633*/
144634static int codeTriggerProgram(
144635 Parse *pParse, /* The parser context */
144636 TriggerStep *pStepList, /* List of statements inside the trigger body */
144637 int orconf /* Conflict algorithm. (OE_Abort, etc) */
144638){
144639 TriggerStep *pStep;
144640 Vdbe *v = pParse->pVdbe;
144641 sqlite3 *db = pParse->db;
144642
144643 assert( pParse->pTriggerTab && pParse->pToplevel )((void) (0));
144644 assert( pStepList )((void) (0));
144645 assert( v!=0 )((void) (0));
144646 for(pStep=pStepList; pStep; pStep=pStep->pNext){
144647 /* Figure out the ON CONFLICT policy that will be used for this step
144648 ** of the trigger program. If the statement that caused this trigger
144649 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
144650 ** the ON CONFLICT policy that was specified as part of the trigger
144651 ** step statement. Example:
144652 **
144653 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
144654 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
144655 ** END;
144656 **
144657 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
144658 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
144659 */
144660 pParse->eOrconf = (orconf==OE_Default11)?pStep->orconf:(u8)orconf;
144661 assert( pParse->okConstFactor==0 )((void) (0));
144662
144663#ifndef SQLITE_OMIT_TRACE
144664 if( pStep->zSpan ){
144665 sqlite3VdbeAddOp4(v, OP_Trace181, 0x7fffffff, 1, 0,
144666 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
144667 P4_DYNAMIC(-6));
144668 }
144669#endif
144670
144671 switch( pStep->op ){
144672 case TK_UPDATE129: {
144673 sqlite3Update(pParse,
144674 sqlite3TriggerStepSrc(pParse, pStep),
144675 sqlite3ExprListDup(db, pStep->pExprList, 0),
144676 sqlite3ExprDup(db, pStep->pWhere, 0),
144677 pParse->eOrconf, 0, 0, 0
144678 );
144679 sqlite3VdbeAddOp0(v, OP_ResetCount131);
144680 break;
144681 }
144682 case TK_INSERT127: {
144683 sqlite3Insert(pParse,
144684 sqlite3TriggerStepSrc(pParse, pStep),
144685 sqlite3SelectDup(db, pStep->pSelect, 0),
144686 sqlite3IdListDup(db, pStep->pIdList),
144687 pParse->eOrconf,
144688 sqlite3UpsertDup(db, pStep->pUpsert)
144689 );
144690 sqlite3VdbeAddOp0(v, OP_ResetCount131);
144691 break;
144692 }
144693 case TK_DELETE128: {
144694 sqlite3DeleteFrom(pParse,
144695 sqlite3TriggerStepSrc(pParse, pStep),
144696 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
144697 );
144698 sqlite3VdbeAddOp0(v, OP_ResetCount131);
144699 break;
144700 }
144701 default: assert( pStep->op==TK_SELECT )((void) (0)); {
144702 SelectDest sDest;
144703 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
144704 sqlite3SelectDestInit(&sDest, SRT_Discard4, 0);
144705 sqlite3Select(pParse, pSelect, &sDest);
144706 sqlite3SelectDelete(db, pSelect);
144707 break;
144708 }
144709 }
144710 }
144711
144712 return 0;
144713}
144714
144715#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
144716/*
144717** This function is used to add VdbeComment() annotations to a VDBE
144718** program. It is not used in production code, only for debugging.
144719*/
144720static const char *onErrorText(int onError){
144721 switch( onError ){
144722 case OE_Abort2: return "abort";
144723 case OE_Rollback1: return "rollback";
144724 case OE_Fail3: return "fail";
144725 case OE_Replace5: return "replace";
144726 case OE_Ignore4: return "ignore";
144727 case OE_Default11: return "default";
144728 }
144729 return "n/a";
144730}
144731#endif
144732
144733/*
144734** Parse context structure pFrom has just been used to create a sub-vdbe
144735** (trigger program). If an error has occurred, transfer error information
144736** from pFrom to pTo.
144737*/
144738static void transferParseError(Parse *pTo, Parse *pFrom){
144739 assert( pFrom->zErrMsg==0 || pFrom->nErr )((void) (0));
144740 assert( pTo->zErrMsg==0 || pTo->nErr )((void) (0));
144741 if( pTo->nErr==0 ){
144742 pTo->zErrMsg = pFrom->zErrMsg;
144743 pTo->nErr = pFrom->nErr;
144744 pTo->rc = pFrom->rc;
144745 }else{
144746 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
144747 }
144748}
144749
144750/*
144751** Create and populate a new TriggerPrg object with a sub-program
144752** implementing trigger pTrigger with ON CONFLICT policy orconf.
144753*/
144754static TriggerPrg *codeRowTrigger(
144755 Parse *pParse, /* Current parse context */
144756 Trigger *pTrigger, /* Trigger to code */
144757 Table *pTab, /* The table pTrigger is attached to */
144758 int orconf /* ON CONFLICT policy to code trigger program with */
144759){
144760 Parse *pTop = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
144761 sqlite3 *db = pParse->db; /* Database handle */
144762 TriggerPrg *pPrg; /* Value to return */
144763 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
144764 Vdbe *v; /* Temporary VM */
144765 NameContext sNC; /* Name context for sub-vdbe */
144766 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
144767 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
144768 Parse sSubParse; /* Parse context for sub-vdbe */
144769
144770 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) )((void) (0));
144771 assert( pTop->pVdbe )((void) (0));
144772
144773 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
144774 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
144775 ** list of the top-level Parse object sooner rather than later. */
144776 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
144777 if( !pPrg ) return 0;
144778 pPrg->pNext = pTop->pTriggerPrg;
144779 pTop->pTriggerPrg = pPrg;
144780 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
144781 if( !pProgram ) return 0;
144782 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
144783 pPrg->pTrigger = pTrigger;
144784 pPrg->orconf = orconf;
144785 pPrg->aColmask[0] = 0xffffffff;
144786 pPrg->aColmask[1] = 0xffffffff;
144787
144788 /* Allocate and populate a new Parse context to use for coding the
144789 ** trigger sub-program. */
144790 sqlite3ParseObjectInit(&sSubParse, db);
144791 memset(&sNC, 0, sizeof(sNC));
144792 sNC.pParse = &sSubParse;
144793 sSubParse.pTriggerTab = pTab;
144794 sSubParse.pToplevel = pTop;
144795 sSubParse.zAuthContext = pTrigger->zName;
144796 sSubParse.eTriggerOp = pTrigger->op;
144797 sSubParse.nQueryLoop = pParse->nQueryLoop;
144798 sSubParse.disableVtab = pParse->disableVtab;
144799
144800 v = sqlite3GetVdbe(&sSubParse);
144801 if( v ){
144802 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
144803 pTrigger->zName, onErrorText(orconf),
144804 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
144805 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
144806 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
144807 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
144808 pTab->zName
144809 ));
144810#ifndef SQLITE_OMIT_TRACE
144811 if( pTrigger->zName ){
144812 sqlite3VdbeChangeP4(v, -1,
144813 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC(-6)
144814 );
144815 }
144816#endif
144817
144818 /* If one was specified, code the WHEN clause. If it evaluates to false
144819 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
144820 ** OP_Halt inserted at the end of the program. */
144821 if( pTrigger->pWhen ){
144822 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
144823 if( db->mallocFailed==0
144824 && SQLITE_OK0==sqlite3ResolveExprNames(&sNC, pWhen)
144825 ){
144826 iEndTrigger = sqlite3VdbeMakeLabel(&sSubParse);
144827 sqlite3ExprIfFalse(&sSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL0x10);
144828 }
144829 sqlite3ExprDelete(db, pWhen);
144830 }
144831
144832 /* Code the trigger program into the sub-vdbe. */
144833 codeTriggerProgram(&sSubParse, pTrigger->step_list, orconf);
144834
144835 /* Insert an OP_Halt at the end of the sub-program. */
144836 if( iEndTrigger ){
144837 sqlite3VdbeResolveLabel(v, iEndTrigger);
144838 }
144839 sqlite3VdbeAddOp0(v, OP_Halt70);
144840 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
144841 transferParseError(pParse, &sSubParse);
144842
144843 if( pParse->nErr==0 ){
144844 assert( db->mallocFailed==0 )((void) (0));
144845 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
144846 }
144847 pProgram->nMem = sSubParse.nMem;
144848 pProgram->nCsr = sSubParse.nTab;
144849 pProgram->token = (void *)pTrigger;
144850 pPrg->aColmask[0] = sSubParse.oldmask;
144851 pPrg->aColmask[1] = sSubParse.newmask;
144852 sqlite3VdbeDelete(v);
144853 }else{
144854 transferParseError(pParse, &sSubParse);
144855 }
144856
144857 assert( !sSubParse.pTriggerPrg && !sSubParse.nMaxArg )((void) (0));
144858 sqlite3ParseObjectReset(&sSubParse);
144859 return pPrg;
144860}
144861
144862/*
144863** Return a pointer to a TriggerPrg object containing the sub-program for
144864** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
144865** TriggerPrg object exists, a new object is allocated and populated before
144866** being returned.
144867*/
144868static TriggerPrg *getRowTrigger(
144869 Parse *pParse, /* Current parse context */
144870 Trigger *pTrigger, /* Trigger to code */
144871 Table *pTab, /* The table trigger pTrigger is attached to */
144872 int orconf /* ON CONFLICT algorithm. */
144873){
144874 Parse *pRoot = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
144875 TriggerPrg *pPrg;
144876
144877 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) )((void) (0));
144878
144879 /* It may be that this trigger has already been coded (or is in the
144880 ** process of being coded). If this is the case, then an entry with
144881 ** a matching TriggerPrg.pTrigger field will be present somewhere
144882 ** in the Parse.pTriggerPrg list. Search for such an entry. */
144883 for(pPrg=pRoot->pTriggerPrg;
144884 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
144885 pPrg=pPrg->pNext
144886 );
144887
144888 /* If an existing TriggerPrg could not be located, create a new one. */
144889 if( !pPrg ){
144890 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
144891 pParse->db->errByteOffset = -1;
144892 }
144893
144894 return pPrg;
144895}
144896
144897/*
144898** Generate code for the trigger program associated with trigger p on
144899** table pTab. The reg, orconf and ignoreJump parameters passed to this
144900** function are the same as those described in the header function for
144901** sqlite3CodeRowTrigger()
144902*/
144903SQLITE_PRIVATEstatic void sqlite3CodeRowTriggerDirect(
144904 Parse *pParse, /* Parse context */
144905 Trigger *p, /* Trigger to code */
144906 Table *pTab, /* The table to code triggers from */
144907 int reg, /* Reg array containing OLD.* and NEW.* values */
144908 int orconf, /* ON CONFLICT policy */
144909 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
144910){
144911 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
144912 TriggerPrg *pPrg;
144913 pPrg = getRowTrigger(pParse, p, pTab, orconf);
144914 assert( pPrg || pParse->nErr )((void) (0));
144915
144916 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
144917 ** is a pointer to the sub-vdbe containing the trigger program. */
144918 if( pPrg ){
144919 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers0x00002000));
144920
144921 sqlite3VdbeAddOp4(v, OP_Program47, reg, ignoreJump, ++pParse->nMem,
144922 (const char *)pPrg->pProgram, P4_SUBPROGRAM(-4));
144923 VdbeComment(
144924 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
144925
144926 /* Set the P5 operand of the OP_Program instruction to non-zero if
144927 ** recursive invocation of this trigger program is disallowed. Recursive
144928 ** invocation is disallowed if (a) the sub-program is really a trigger,
144929 ** not a foreign key action, and (b) the flag to enable recursive triggers
144930 ** is clear. */
144931 sqlite3VdbeChangeP5(v, (u8)bRecursive);
144932 }
144933}
144934
144935/*
144936** This is called to code the required FOR EACH ROW triggers for an operation
144937** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
144938** is given by the op parameter. The tr_tm parameter determines whether the
144939** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
144940** parameter pChanges is passed the list of columns being modified.
144941**
144942** If there are no triggers that fire at the specified time for the specified
144943** operation on pTab, this function is a no-op.
144944**
144945** The reg argument is the address of the first in an array of registers
144946** that contain the values substituted for the new.* and old.* references
144947** in the trigger program. If N is the number of columns in table pTab
144948** (a copy of pTab->nCol), then registers are populated as follows:
144949**
144950** Register Contains
144951** ------------------------------------------------------
144952** reg+0 OLD.rowid
144953** reg+1 OLD.* value of left-most column of pTab
144954** ... ...
144955** reg+N OLD.* value of right-most column of pTab
144956** reg+N+1 NEW.rowid
144957** reg+N+2 NEW.* value of left-most column of pTab
144958** ... ...
144959** reg+N+N+1 NEW.* value of right-most column of pTab
144960**
144961** For ON DELETE triggers, the registers containing the NEW.* values will
144962** never be accessed by the trigger program, so they are not allocated or
144963** populated by the caller (there is no data to populate them with anyway).
144964** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
144965** are never accessed, and so are not allocated by the caller. So, for an
144966** ON INSERT trigger, the value passed to this function as parameter reg
144967** is not a readable register, although registers (reg+N) through
144968** (reg+N+N+1) are.
144969**
144970** Parameter orconf is the default conflict resolution algorithm for the
144971** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
144972** is the instruction that control should jump to if a trigger program
144973** raises an IGNORE exception.
144974*/
144975SQLITE_PRIVATEstatic void sqlite3CodeRowTrigger(
144976 Parse *pParse, /* Parse context */
144977 Trigger *pTrigger, /* List of triggers on table pTab */
144978 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
144979 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
144980 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
144981 Table *pTab, /* The table to code triggers from */
144982 int reg, /* The first in an array of registers (see above) */
144983 int orconf, /* ON CONFLICT policy */
144984 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
144985){
144986 Trigger *p; /* Used to iterate through pTrigger list */
144987
144988 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE )((void) (0));
144989 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER )((void) (0));
144990 assert( (op==TK_UPDATE)==(pChanges!=0) )((void) (0));
144991
144992 for(p=pTrigger; p; p=p->pNext){
144993
144994 /* Sanity checking: The schema for the trigger and for the table are
144995 ** always defined. The trigger must be in the same schema as the table
144996 ** or else it must be a TEMP trigger. */
144997 assert( p->pSchema!=0 )((void) (0));
144998 assert( p->pTabSchema!=0 )((void) (0));
144999 assert( p->pSchema==p->pTabSchema((void) (0))
145000 || p->pSchema==pParse->db->aDb[1].pSchema )((void) (0));
145001
145002 /* Determine whether we should code this trigger. One of two choices:
145003 ** 1. The trigger is an exact match to the current DML statement
145004 ** 2. This is a RETURNING trigger for INSERT but we are currently
145005 ** doing the UPDATE part of an UPSERT.
145006 */
145007 if( (p->op==op || (p->bReturning && p->op==TK_INSERT127 && op==TK_UPDATE129))
145008 && p->tr_tm==tr_tm
145009 && checkColumnOverlap(p->pColumns, pChanges)
145010 ){
145011 if( !p->bReturning ){
145012 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
145013 }else if( sqlite3IsToplevel(pParse)((pParse)->pToplevel==0) ){
145014 codeReturningTrigger(pParse, p, pTab, reg);
145015 }
145016 }
145017 }
145018}
145019
145020/*
145021** Triggers may access values stored in the old.* or new.* pseudo-table.
145022** This function returns a 32-bit bitmask indicating which columns of the
145023** old.* or new.* tables actually are used by triggers. This information
145024** may be used by the caller, for example, to avoid having to load the entire
145025** old.* record into memory when executing an UPDATE or DELETE command.
145026**
145027** Bit 0 of the returned mask is set if the left-most column of the
145028** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
145029** the second leftmost column value is required, and so on. If there
145030** are more than 32 columns in the table, and at least one of the columns
145031** with an index greater than 32 may be accessed, 0xffffffff is returned.
145032**
145033** It is not possible to determine if the old.rowid or new.rowid column is
145034** accessed by triggers. The caller must always assume that it is.
145035**
145036** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
145037** applies to the old.* table. If 1, the new.* table.
145038**
145039** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
145040** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
145041** included in the returned mask if the TRIGGER_BEFORE bit is set in the
145042** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
145043** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
145044*/
145045SQLITE_PRIVATEstatic u32 sqlite3TriggerColmask(
145046 Parse *pParse, /* Parse context */
145047 Trigger *pTrigger, /* List of triggers on table pTab */
145048 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
145049 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
145050 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
145051 Table *pTab, /* The table to code triggers from */
145052 int orconf /* Default ON CONFLICT policy for trigger steps */
145053){
145054 const int op = pChanges ? TK_UPDATE129 : TK_DELETE128;
145055 u32 mask = 0;
145056 Trigger *p;
145057
145058 assert( isNew==1 || isNew==0 )((void) (0));
145059 for(p=pTrigger; p; p=p->pNext){
145060 if( p->op==op
145061 && (tr_tm&p->tr_tm)
145062 && checkColumnOverlap(p->pColumns,pChanges)
145063 ){
145064 if( p->bReturning ){
145065 mask = 0xffffffff;
145066 }else{
145067 TriggerPrg *pPrg;
145068 pPrg = getRowTrigger(pParse, p, pTab, orconf);
145069 if( pPrg ){
145070 mask |= pPrg->aColmask[isNew];
145071 }
145072 }
145073 }
145074 }
145075
145076 return mask;
145077}
145078
145079#endif /* !defined(SQLITE_OMIT_TRIGGER) */
145080
145081/************** End of trigger.c *********************************************/
145082/************** Begin file update.c ******************************************/
145083/*
145084** 2001 September 15
145085**
145086** The author disclaims copyright to this source code. In place of
145087** a legal notice, here is a blessing:
145088**
145089** May you do good and not evil.
145090** May you find forgiveness for yourself and forgive others.
145091** May you share freely, never taking more than you give.
145092**
145093*************************************************************************
145094** This file contains C code routines that are called by the parser
145095** to handle UPDATE statements.
145096*/
145097/* #include "sqliteInt.h" */
145098
145099#ifndef SQLITE_OMIT_VIRTUALTABLE
145100/* Forward declaration */
145101static void updateVirtualTable(
145102 Parse *pParse, /* The parsing context */
145103 SrcList *pSrc, /* The virtual table to be modified */
145104 Table *pTab, /* The virtual table */
145105 ExprList *pChanges, /* The columns to change in the UPDATE statement */
145106 Expr *pRowidExpr, /* Expression used to recompute the rowid */
145107 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
145108 Expr *pWhere, /* WHERE clause of the UPDATE statement */
145109 int onError /* ON CONFLICT strategy */
145110);
145111#endif /* SQLITE_OMIT_VIRTUALTABLE */
145112
145113/*
145114** The most recently coded instruction was an OP_Column to retrieve the
145115** i-th column of table pTab. This routine sets the P4 parameter of the
145116** OP_Column to the default value, if any.
145117**
145118** The default value of a column is specified by a DEFAULT clause in the
145119** column definition. This was either supplied by the user when the table
145120** was created, or added later to the table definition by an ALTER TABLE
145121** command. If the latter, then the row-records in the table btree on disk
145122** may not contain a value for the column and the default value, taken
145123** from the P4 parameter of the OP_Column instruction, is returned instead.
145124** If the former, then all row-records are guaranteed to include a value
145125** for the column and the P4 value is not required.
145126**
145127** Column definitions created by an ALTER TABLE command may only have
145128** literal default values specified: a number, null or a string. (If a more
145129** complicated default expression value was provided, it is evaluated
145130** when the ALTER TABLE is executed and one of the literal values written
145131** into the sqlite_schema table.)
145132**
145133** Therefore, the P4 parameter is only required if the default value for
145134** the column is a literal number, string or null. The sqlite3ValueFromExpr()
145135** function is capable of transforming these types of expressions into
145136** sqlite3_value objects.
145137**
145138** If column as REAL affinity and the table is an ordinary b-tree table
145139** (not a virtual table) then the value might have been stored as an
145140** integer. In that case, add an OP_RealAffinity opcode to make sure
145141** it has been converted into REAL.
145142*/
145143SQLITE_PRIVATEstatic void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
145144 assert( pTab!=0 )((void) (0));
145145 if( !IsView(pTab)((pTab)->eTabType==2) ){
145146 sqlite3_value *pValue = 0;
145147 u8 enc = ENC(sqlite3VdbeDb(v))((sqlite3VdbeDb(v))->enc);
145148 Column *pCol = &pTab->aCol[i];
145149 VdbeComment((v, "%s.%s", pTab->zName, pCol->zCnName));
145150 assert( i<pTab->nCol )((void) (0));
145151 sqlite3ValueFromExpr(sqlite3VdbeDb(v),
145152 sqlite3ColumnExpr(pTab,pCol), enc,
145153 pCol->affinity, &pValue);
145154 if( pValue ){
145155 sqlite3VdbeAppendP4(v, pValue, P4_MEM(-10));
145156 }
145157 }
145158#ifndef SQLITE_OMIT_FLOATING_POINT
145159 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL0x45 && !IsVirtual(pTab)((pTab)->eTabType==1) ){
145160 sqlite3VdbeAddOp1(v, OP_RealAffinity87, iReg);
145161 }
145162#endif
145163}
145164
145165/*
145166** Check to see if column iCol of index pIdx references any of the
145167** columns defined by aXRef and chngRowid. Return true if it does
145168** and false if not. This is an optimization. False-positives are a
145169** performance degradation, but false-negatives can result in a corrupt
145170** index and incorrect answers.
145171**
145172** aXRef[j] will be non-negative if column j of the original table is
145173** being updated. chngRowid will be true if the rowid of the table is
145174** being updated.
145175*/
145176static int indexColumnIsBeingUpdated(
145177 Index *pIdx, /* The index to check */
145178 int iCol, /* Which column of the index to check */
145179 int *aXRef, /* aXRef[j]>=0 if column j is being updated */
145180 int chngRowid /* true if the rowid is being updated */
145181){
145182 i16 iIdxCol = pIdx->aiColumn[iCol];
145183 assert( iIdxCol!=XN_ROWID )((void) (0)); /* Cannot index rowid */
145184 if( iIdxCol>=0 ){
145185 return aXRef[iIdxCol]>=0;
145186 }
145187 assert( iIdxCol==XN_EXPR )((void) (0));
145188 assert( pIdx->aColExpr!=0 )((void) (0));
145189 assert( pIdx->aColExpr->a[iCol].pExpr!=0 )((void) (0));
145190 return sqlite3ExprReferencesUpdatedColumn(pIdx->aColExpr->a[iCol].pExpr,
145191 aXRef,chngRowid);
145192}
145193
145194/*
145195** Check to see if index pIdx is a partial index whose conditional
145196** expression might change values due to an UPDATE. Return true if
145197** the index is subject to change and false if the index is guaranteed
145198** to be unchanged. This is an optimization. False-positives are a
145199** performance degradation, but false-negatives can result in a corrupt
145200** index and incorrect answers.
145201**
145202** aXRef[j] will be non-negative if column j of the original table is
145203** being updated. chngRowid will be true if the rowid of the table is
145204** being updated.
145205*/
145206static int indexWhereClauseMightChange(
145207 Index *pIdx, /* The index to check */
145208 int *aXRef, /* aXRef[j]>=0 if column j is being updated */
145209 int chngRowid /* true if the rowid is being updated */
145210){
145211 if( pIdx->pPartIdxWhere==0 ) return 0;
145212 return sqlite3ExprReferencesUpdatedColumn(pIdx->pPartIdxWhere,
145213 aXRef, chngRowid);
145214}
145215
145216/*
145217** Allocate and return a pointer to an expression of type TK_ROW with
145218** Expr.iColumn set to value (iCol+1). The resolver will modify the
145219** expression to be a TK_COLUMN reading column iCol of the first
145220** table in the source-list (pSrc->a[0]).
145221*/
145222static Expr *exprRowColumn(Parse *pParse, int iCol){
145223 Expr *pRet = sqlite3PExpr(pParse, TK_ROW75, 0, 0);
145224 if( pRet ) pRet->iColumn = iCol+1;
145225 return pRet;
145226}
145227
145228/*
145229** Assuming both the pLimit and pOrderBy parameters are NULL, this function
145230** generates VM code to run the query:
145231**
145232** SELECT <other-columns>, pChanges FROM pTabList WHERE pWhere
145233**
145234** and write the results to the ephemeral table already opened as cursor
145235** iEph. None of pChanges, pTabList or pWhere are modified or consumed by
145236** this function, they must be deleted by the caller.
145237**
145238** Or, if pLimit and pOrderBy are not NULL, and pTab is not a view:
145239**
145240** SELECT <other-columns>, pChanges FROM pTabList
145241** WHERE pWhere
145242** GROUP BY <other-columns>
145243** ORDER BY pOrderBy LIMIT pLimit
145244**
145245** If pTab is a view, the GROUP BY clause is omitted.
145246**
145247** Exactly how results are written to table iEph, and exactly what
145248** the <other-columns> in the query above are is determined by the type
145249** of table pTabList->a[0].pTab.
145250**
145251** If the table is a WITHOUT ROWID table, then argument pPk must be its
145252** PRIMARY KEY. In this case <other-columns> are the primary key columns
145253** of the table, in order. The results of the query are written to ephemeral
145254** table iEph as index keys, using OP_IdxInsert.
145255**
145256** If the table is actually a view, then <other-columns> are all columns of
145257** the view. The results are written to the ephemeral table iEph as records
145258** with automatically assigned integer keys.
145259**
145260** If the table is a virtual or ordinary intkey table, then <other-columns>
145261** is its rowid. For a virtual table, the results are written to iEph as
145262** records with automatically assigned integer keys For intkey tables, the
145263** rowid value in <other-columns> is used as the integer key, and the
145264** remaining fields make up the table record.
145265*/
145266static void updateFromSelect(
145267 Parse *pParse, /* Parse context */
145268 int iEph, /* Cursor for open eph. table */
145269 Index *pPk, /* PK if table 0 is WITHOUT ROWID */
145270 ExprList *pChanges, /* List of expressions to return */
145271 SrcList *pTabList, /* List of tables to select from */
145272 Expr *pWhere, /* WHERE clause for query */
145273 ExprList *pOrderBy, /* ORDER BY clause */
145274 Expr *pLimit /* LIMIT clause */
145275){
145276 int i;
145277 SelectDest dest;
145278 Select *pSelect = 0;
145279 ExprList *pList = 0;
145280 ExprList *pGrp = 0;
145281 Expr *pLimit2 = 0;
145282 ExprList *pOrderBy2 = 0;
145283 sqlite3 *db = pParse->db;
145284 Table *pTab = pTabList->a[0].pTab;
145285 SrcList *pSrc;
145286 Expr *pWhere2;
145287 int eDest;
145288
145289#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT1
145290 if( pOrderBy && pLimit==0 ) {
145291 sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on UPDATE");
145292 return;
145293 }
145294 pOrderBy2 = sqlite3ExprListDup(db, pOrderBy, 0);
145295 pLimit2 = sqlite3ExprDup(db, pLimit, 0);
145296#else
145297 UNUSED_PARAMETER(pOrderBy)(void)(pOrderBy);
145298 UNUSED_PARAMETER(pLimit)(void)(pLimit);
145299#endif
145300
145301 pSrc = sqlite3SrcListDup(db, pTabList, 0);
145302 pWhere2 = sqlite3ExprDup(db, pWhere, 0);
145303
145304 assert( pTabList->nSrc>1 )((void) (0));
145305 if( pSrc ){
145306 pSrc->a[0].fg.notCte = 1;
145307 pSrc->a[0].iCursor = -1;
145308 pSrc->a[0].pTab->nTabRef--;
145309 pSrc->a[0].pTab = 0;
145310 }
145311 if( pPk ){
145312 for(i=0; i<pPk->nKeyCol; i++){
145313 Expr *pNew = exprRowColumn(pParse, pPk->aiColumn[i]);
145314#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT1
145315 if( pLimit ){
145316 pGrp = sqlite3ExprListAppend(pParse, pGrp, sqlite3ExprDup(db, pNew, 0));
145317 }
145318#endif
145319 pList = sqlite3ExprListAppend(pParse, pList, pNew);
145320 }
145321 eDest = IsVirtual(pTab)((pTab)->eTabType==1) ? SRT_Table14 : SRT_Upfrom15;
145322 }else if( IsView(pTab)((pTab)->eTabType==2) ){
145323 for(i=0; i<pTab->nCol; i++){
145324 pList = sqlite3ExprListAppend(pParse, pList, exprRowColumn(pParse, i));
145325 }
145326 eDest = SRT_Table14;
145327 }else{
145328 eDest = IsVirtual(pTab)((pTab)->eTabType==1) ? SRT_Table14 : SRT_Upfrom15;
145329 pList = sqlite3ExprListAppend(pParse, 0, sqlite3PExpr(pParse,TK_ROW75,0,0));
145330#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT1
145331 if( pLimit ){
145332 pGrp = sqlite3ExprListAppend(pParse, 0, sqlite3PExpr(pParse,TK_ROW75,0,0));
145333 }
145334#endif
145335 }
145336 assert( pChanges!=0 || pParse->db->mallocFailed )((void) (0));
145337 if( pChanges ){
145338 for(i=0; i<pChanges->nExpr; i++){
145339 pList = sqlite3ExprListAppend(pParse, pList,
145340 sqlite3ExprDup(db, pChanges->a[i].pExpr, 0)
145341 );
145342 }
145343 }
145344 pSelect = sqlite3SelectNew(pParse, pList,
145345 pSrc, pWhere2, pGrp, 0, pOrderBy2, SF_UFSrcCheck0x0800000|SF_IncludeHidden0x0020000, pLimit2
145346 );
145347 if( pSelect ) pSelect->selFlags |= SF_OrderByReqd0x8000000;
145348 sqlite3SelectDestInit(&dest, eDest, iEph);
145349 dest.iSDParm2 = (pPk ? pPk->nKeyCol : -1);
145350 sqlite3Select(pParse, pSelect, &dest);
145351 sqlite3SelectDelete(db, pSelect);
145352}
145353
145354/*
145355** Process an UPDATE statement.
145356**
145357** UPDATE OR IGNORE tbl SET a=b, c=d FROM tbl2... WHERE e<5 AND f NOT NULL;
145358** \_______/ \_/ \______/ \_____/ \________________/
145359** onError | pChanges | pWhere
145360** \_______________________/
145361** pTabList
145362*/
145363SQLITE_PRIVATEstatic void sqlite3Update(
145364 Parse *pParse, /* The parser context */
145365 SrcList *pTabList, /* The table in which we should change things */
145366 ExprList *pChanges, /* Things to be changed */
145367 Expr *pWhere, /* The WHERE clause. May be null */
145368 int onError, /* How to handle constraint errors */
145369 ExprList *pOrderBy, /* ORDER BY clause. May be null */
145370 Expr *pLimit, /* LIMIT clause. May be null */
145371 Upsert *pUpsert /* ON CONFLICT clause, or null */
145372){
145373 int i, j, k; /* Loop counters */
145374 Table *pTab; /* The table to be updated */
145375 int addrTop = 0; /* VDBE instruction address of the start of the loop */
145376 WhereInfo *pWInfo = 0; /* Information about the WHERE clause */
145377 Vdbe *v; /* The virtual database engine */
145378 Index *pIdx; /* For looping over indices */
145379 Index *pPk; /* The PRIMARY KEY index for WITHOUT ROWID tables */
145380 int nIdx; /* Number of indices that need updating */
145381 int nAllIdx; /* Total number of indexes */
145382 int iBaseCur; /* Base cursor number */
145383 int iDataCur; /* Cursor for the canonical data btree */
145384 int iIdxCur; /* Cursor for the first index */
145385 sqlite3 *db; /* The database structure */
145386 int *aRegIdx = 0; /* Registers for to each index and the main table */
145387 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
145388 ** an expression for the i-th column of the table.
145389 ** aXRef[i]==-1 if the i-th column is not changed. */
145390 u8 *aToOpen; /* 1 for tables and indices to be opened */
145391 u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */
145392 u8 chngRowid; /* Rowid changed in a normal table */
145393 u8 chngKey; /* Either chngPk or chngRowid */
145394 Expr *pRowidExpr = 0; /* Expression defining the new record number */
145395 int iRowidExpr = -1; /* Index of "rowid=" (or IPK) assignment in pChanges */
145396 AuthContext sContext; /* The authorization context */
145397 NameContext sNC; /* The name-context to resolve expressions in */
145398 int iDb; /* Database containing the table being updated */
145399 int eOnePass; /* ONEPASS_XXX value from where.c */
145400 int hasFK; /* True if foreign key processing is required */
145401 int labelBreak; /* Jump here to break out of UPDATE loop */
145402 int labelContinue; /* Jump here to continue next step of UPDATE loop */
145403 int flags; /* Flags for sqlite3WhereBegin() */
145404
145405#ifndef SQLITE_OMIT_TRIGGER
145406 int isView; /* True when updating a view (INSTEAD OF trigger) */
145407 Trigger *pTrigger; /* List of triggers on pTab, if required */
145408 int tmask; /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
145409#endif
145410 int newmask; /* Mask of NEW.* columns accessed by BEFORE triggers */
145411 int iEph = 0; /* Ephemeral table holding all primary key values */
145412 int nKey = 0; /* Number of elements in regKey for WITHOUT ROWID */
145413 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
145414 int addrOpen = 0; /* Address of OP_OpenEphemeral */
145415 int iPk = 0; /* First of nPk cells holding PRIMARY KEY value */
145416 i16 nPk = 0; /* Number of components of the PRIMARY KEY */
145417 int bReplace = 0; /* True if REPLACE conflict resolution might happen */
145418 int bFinishSeek = 1; /* The OP_FinishSeek opcode is needed */
145419 int nChangeFrom = 0; /* If there is a FROM, pChanges->nExpr, else 0 */
145420
145421 /* Register Allocations */
145422 int regRowCount = 0; /* A count of rows changed */
145423 int regOldRowid = 0; /* The old rowid */
145424 int regNewRowid = 0; /* The new rowid */
145425 int regNew = 0; /* Content of the NEW.* table in triggers */
145426 int regOld = 0; /* Content of OLD.* table in triggers */
145427 int regRowSet = 0; /* Rowset of rows to be updated */
145428 int regKey = 0; /* composite PRIMARY KEY value */
145429
145430 memset(&sContext, 0, sizeof(sContext));
145431 db = pParse->db;
145432 assert( db->pParse==pParse )((void) (0));
145433 if( pParse->nErr ){
145434 goto update_cleanup;
145435 }
145436 assert( db->mallocFailed==0 )((void) (0));
145437
145438 /* Locate the table which we want to update.
145439 */
145440 pTab = sqlite3SrcListLookup(pParse, pTabList);
145441 if( pTab==0 ) goto update_cleanup;
145442 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
145443
145444 /* Figure out if we have any triggers and if the table being
145445 ** updated is a view.
145446 */
145447#ifndef SQLITE_OMIT_TRIGGER
145448 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE129, pChanges, &tmask);
145449 isView = IsView(pTab)((pTab)->eTabType==2);
145450 assert( pTrigger || tmask==0 )((void) (0));
145451#else
145452# define pTrigger 0
145453# define isView 0
145454# define tmask 0
145455#endif
145456#ifdef SQLITE_OMIT_VIEW
145457# undef isView
145458# define isView 0
145459#endif
145460
145461#if TREETRACE_ENABLED0
145462 if( sqlite3TreeTrace & 0x10000 ){
145463 sqlite3TreeViewLine(0, "In sqlite3Update() at %s:%d", __FILE__"3rdparty/sqlite3/sqlite3.c", __LINE__145463);
145464 sqlite3TreeViewUpdate(pParse->pWith, pTabList, pChanges, pWhere,
145465 onError, pOrderBy, pLimit, pUpsert, pTrigger);
145466 }
145467#endif
145468
145469 /* If there was a FROM clause, set nChangeFrom to the number of expressions
145470 ** in the change-list. Otherwise, set it to 0. There cannot be a FROM
145471 ** clause if this function is being called to generate code for part of
145472 ** an UPSERT statement. */
145473 nChangeFrom = (pTabList->nSrc>1) ? pChanges->nExpr : 0;
145474 assert( nChangeFrom==0 || pUpsert==0 )((void) (0));
145475
145476#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT1
145477 if( !isView && nChangeFrom==0 ){
145478 pWhere = sqlite3LimitWhere(
145479 pParse, pTabList, pWhere, pOrderBy, pLimit, "UPDATE"
145480 );
145481 pOrderBy = 0;
145482 pLimit = 0;
145483 }
145484#endif
145485
145486 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
145487 goto update_cleanup;
145488 }
145489 if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
145490 goto update_cleanup;
145491 }
145492
145493 /* Allocate a cursors for the main database table and for all indices.
145494 ** The index cursors might not be used, but if they are used they
145495 ** need to occur right after the database cursor. So go ahead and
145496 ** allocate enough space, just in case.
145497 */
145498 iBaseCur = iDataCur = pParse->nTab++;
145499 iIdxCur = iDataCur+1;
145500 pPk = HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ? 0 : sqlite3PrimaryKeyIndex(pTab);
145501 testcase( pPk!=0 && pPk!=pTab->pIndex );
145502 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
145503 if( pPk==pIdx ){
145504 iDataCur = pParse->nTab;
145505 }
145506 pParse->nTab++;
145507 }
145508 if( pUpsert ){
145509 /* On an UPSERT, reuse the same cursors already opened by INSERT */
145510 iDataCur = pUpsert->iDataCur;
145511 iIdxCur = pUpsert->iIdxCur;
145512 pParse->nTab = iBaseCur;
145513 }
145514 pTabList->a[0].iCursor = iDataCur;
145515
145516 /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].
145517 ** Initialize aXRef[] and aToOpen[] to their default values.
145518 */
145519 aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx+1) + nIdx+2 );
145520 if( aXRef==0 ) goto update_cleanup;
145521 aRegIdx = aXRef+pTab->nCol;
145522 aToOpen = (u8*)(aRegIdx+nIdx+1);
145523 memset(aToOpen, 1, nIdx+1);
145524 aToOpen[nIdx+1] = 0;
145525 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
145526
145527 /* Initialize the name-context */
145528 memset(&sNC, 0, sizeof(sNC));
145529 sNC.pParse = pParse;
145530 sNC.pSrcList = pTabList;
145531 sNC.uNC.pUpsert = pUpsert;
145532 sNC.ncFlags = NC_UUpsert0x000200;
145533
145534 /* Begin generating code. */
145535 v = sqlite3GetVdbe(pParse);
145536 if( v==0 ) goto update_cleanup;
145537
145538 /* Resolve the column names in all the expressions of the
145539 ** of the UPDATE statement. Also find the column index
145540 ** for each column to be updated in the pChanges array. For each
145541 ** column to be updated, make sure we have authorization to change
145542 ** that column.
145543 */
145544 chngRowid = chngPk = 0;
145545 for(i=0; i<pChanges->nExpr; i++){
145546 u8 hCol = sqlite3StrIHash(pChanges->a[i].zEName);
145547 /* If this is an UPDATE with a FROM clause, do not resolve expressions
145548 ** here. The call to sqlite3Select() below will do that. */
145549 if( nChangeFrom==0 && sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
145550 goto update_cleanup;
145551 }
145552 for(j=0; j<pTab->nCol; j++){
145553 if( pTab->aCol[j].hName==hCol
145554 && sqlite3StrICmp(pTab->aCol[j].zCnName, pChanges->a[i].zEName)==0
145555 ){
145556 if( j==pTab->iPKey ){
145557 chngRowid = 1;
145558 pRowidExpr = pChanges->a[i].pExpr;
145559 iRowidExpr = i;
145560 }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY0x0001)!=0 ){
145561 chngPk = 1;
145562 }
145563#ifndef SQLITE_OMIT_GENERATED_COLUMNS
145564 else if( pTab->aCol[j].colFlags & COLFLAG_GENERATED0x0060 ){
145565 testcase( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL );
145566 testcase( pTab->aCol[j].colFlags & COLFLAG_STORED );
145567 sqlite3ErrorMsg(pParse,
145568 "cannot UPDATE generated column \"%s\"",
145569 pTab->aCol[j].zCnName);
145570 goto update_cleanup;
145571 }
145572#endif
145573 aXRef[j] = i;
145574 break;
145575 }
145576 }
145577 if( j>=pTab->nCol ){
145578 if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zEName) ){
145579 j = -1;
145580 chngRowid = 1;
145581 pRowidExpr = pChanges->a[i].pExpr;
145582 iRowidExpr = i;
145583 }else{
145584 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zEName);
145585 pParse->checkSchema = 1;
145586 goto update_cleanup;
145587 }
145588 }
145589#ifndef SQLITE_OMIT_AUTHORIZATION
145590 {
145591 int rc;
145592 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE23, pTab->zName,
145593 j<0 ? "ROWID" : pTab->aCol[j].zCnName,
145594 db->aDb[iDb].zDbSName);
145595 if( rc==SQLITE_DENY1 ){
145596 goto update_cleanup;
145597 }else if( rc==SQLITE_IGNORE2 ){
145598 aXRef[j] = -1;
145599 }
145600 }
145601#endif
145602 }
145603 assert( (chngRowid & chngPk)==0 )((void) (0));
145604 assert( chngRowid==0 || chngRowid==1 )((void) (0));
145605 assert( chngPk==0 || chngPk==1 )((void) (0));
145606 chngKey = chngRowid + chngPk;
145607
145608#ifndef SQLITE_OMIT_GENERATED_COLUMNS
145609 /* Mark generated columns as changing if their generator expressions
145610 ** reference any changing column. The actual aXRef[] value for
145611 ** generated expressions is not used, other than to check to see that it
145612 ** is non-negative, so the value of aXRef[] for generated columns can be
145613 ** set to any non-negative number. We use 99999 so that the value is
145614 ** obvious when looking at aXRef[] in a symbolic debugger.
145615 */
145616 if( pTab->tabFlags & TF_HasGenerated0x00000060 ){
145617 int bProgress;
145618 testcase( pTab->tabFlags & TF_HasVirtual );
145619 testcase( pTab->tabFlags & TF_HasStored );
145620 do{
145621 bProgress = 0;
145622 for(i=0; i<pTab->nCol; i++){
145623 if( aXRef[i]>=0 ) continue;
145624 if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED0x0060)==0 ) continue;
145625 if( sqlite3ExprReferencesUpdatedColumn(
145626 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
145627 aXRef, chngRowid)
145628 ){
145629 aXRef[i] = 99999;
145630 bProgress = 1;
145631 }
145632 }
145633 }while( bProgress );
145634 }
145635#endif
145636
145637 /* The SET expressions are not actually used inside the WHERE loop.
145638 ** So reset the colUsed mask. Unless this is a virtual table. In that
145639 ** case, set all bits of the colUsed mask (to ensure that the virtual
145640 ** table implementation makes all columns available).
145641 */
145642 pTabList->a[0].colUsed = IsVirtual(pTab)((pTab)->eTabType==1) ? ALLBITS((Bitmask)-1) : 0;
145643
145644 hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);
145645
145646 /* There is one entry in the aRegIdx[] array for each index on the table
145647 ** being updated. Fill in aRegIdx[] with a register number that will hold
145648 ** the key for accessing each index.
145649 */
145650 if( onError==OE_Replace5 ) bReplace = 1;
145651 for(nAllIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nAllIdx++){
145652 int reg;
145653 if( chngKey || hasFK>1 || pIdx==pPk
145654 || indexWhereClauseMightChange(pIdx,aXRef,chngRowid)
145655 ){
145656 reg = ++pParse->nMem;
145657 pParse->nMem += pIdx->nColumn;
145658 }else{
145659 reg = 0;
145660 for(i=0; i<pIdx->nKeyCol; i++){
145661 if( indexColumnIsBeingUpdated(pIdx, i, aXRef, chngRowid) ){
145662 reg = ++pParse->nMem;
145663 pParse->nMem += pIdx->nColumn;
145664 if( onError==OE_Default11 && pIdx->onError==OE_Replace5 ){
145665 bReplace = 1;
145666 }
145667 break;
145668 }
145669 }
145670 }
145671 if( reg==0 ) aToOpen[nAllIdx+1] = 0;
145672 aRegIdx[nAllIdx] = reg;
145673 }
145674 aRegIdx[nAllIdx] = ++pParse->nMem; /* Register storing the table record */
145675 if( bReplace ){
145676 /* If REPLACE conflict resolution might be invoked, open cursors on all
145677 ** indexes in case they are needed to delete records. */
145678 memset(aToOpen, 1, nIdx+1);
145679 }
145680
145681 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
145682 sqlite3BeginWriteOperation(pParse, pTrigger || hasFK, iDb);
145683
145684 /* Allocate required registers. */
145685 if( !IsVirtual(pTab)((pTab)->eTabType==1) ){
145686 /* For now, regRowSet and aRegIdx[nAllIdx] share the same register.
145687 ** If regRowSet turns out to be needed, then aRegIdx[nAllIdx] will be
145688 ** reallocated. aRegIdx[nAllIdx] is the register in which the main
145689 ** table record is written. regRowSet holds the RowSet for the
145690 ** two-pass update algorithm. */
145691 assert( aRegIdx[nAllIdx]==pParse->nMem )((void) (0));
145692 regRowSet = aRegIdx[nAllIdx];
145693 regOldRowid = regNewRowid = ++pParse->nMem;
145694 if( chngPk || pTrigger || hasFK ){
145695 regOld = pParse->nMem + 1;
145696 pParse->nMem += pTab->nCol;
145697 }
145698 if( chngKey || pTrigger || hasFK ){
145699 regNewRowid = ++pParse->nMem;
145700 }
145701 regNew = pParse->nMem + 1;
145702 pParse->nMem += pTab->nCol;
145703 }
145704
145705 /* Start the view context. */
145706 if( isView ){
145707 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
145708 }
145709
145710 /* If we are trying to update a view, realize that view into
145711 ** an ephemeral table.
145712 */
145713#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
145714 if( nChangeFrom==0 && isView ){
145715 sqlite3MaterializeView(pParse, pTab,
145716 pWhere, pOrderBy, pLimit, iDataCur
145717 );
145718 pOrderBy = 0;
145719 pLimit = 0;
145720 }
145721#endif
145722
145723 /* Resolve the column names in all the expressions in the
145724 ** WHERE clause.
145725 */
145726 if( nChangeFrom==0 && sqlite3ResolveExprNames(&sNC, pWhere) ){
145727 goto update_cleanup;
145728 }
145729
145730#ifndef SQLITE_OMIT_VIRTUALTABLE
145731 /* Virtual tables must be handled separately */
145732 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
145733 updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
145734 pWhere, onError);
145735 goto update_cleanup;
145736 }
145737#endif
145738
145739 /* Jump to labelBreak to abandon further processing of this UPDATE */
145740 labelContinue = labelBreak = sqlite3VdbeMakeLabel(pParse);
145741
145742 /* Not an UPSERT. Normal processing. Begin by
145743 ** initialize the count of updated rows */
145744 if( (db->flags&SQLITE_CountRows((u64)(0x00001)<<32))!=0
145745 && !pParse->pTriggerTab
145746 && !pParse->nested
145747 && !pParse->bReturning
145748 && pUpsert==0
145749 ){
145750 regRowCount = ++pParse->nMem;
145751 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regRowCount);
145752 }
145753
145754 if( nChangeFrom==0 && HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
145755 sqlite3VdbeAddOp3(v, OP_Null75, 0, regRowSet, regOldRowid);
145756 iEph = pParse->nTab++;
145757 addrOpen = sqlite3VdbeAddOp3(v, OP_OpenEphemeral118, iEph, 0, regRowSet);
145758 }else{
145759 assert( pPk!=0 || HasRowid(pTab) )((void) (0));
145760 nPk = pPk ? pPk->nKeyCol : 0;
145761 iPk = pParse->nMem+1;
145762 pParse->nMem += nPk;
145763 pParse->nMem += nChangeFrom;
145764 regKey = ++pParse->nMem;
145765 if( pUpsert==0 ){
145766 int nEphCol = nPk + nChangeFrom + (isView ? pTab->nCol : 0);
145767 iEph = pParse->nTab++;
145768 if( pPk ) sqlite3VdbeAddOp3(v, OP_Null75, 0, iPk, iPk+nPk-1);
145769 addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral118, iEph, nEphCol);
145770 if( pPk ){
145771 KeyInfo *pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pPk);
145772 if( pKeyInfo ){
145773 pKeyInfo->nAllField = nEphCol;
145774 sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO(-8));
145775 }
145776 }
145777 if( nChangeFrom ){
145778 updateFromSelect(
145779 pParse, iEph, pPk, pChanges, pTabList, pWhere, pOrderBy, pLimit
145780 );
145781#ifndef SQLITE_OMIT_SUBQUERY
145782 if( isView ) iDataCur = iEph;
145783#endif
145784 }
145785 }
145786 }
145787
145788 if( nChangeFrom ){
145789 sqlite3MultiWrite(pParse);
145790 eOnePass = ONEPASS_OFF0;
145791 nKey = nPk;
145792 regKey = iPk;
145793 }else{
145794 if( pUpsert ){
145795 /* If this is an UPSERT, then all cursors have already been opened by
145796 ** the outer INSERT and the data cursor should be pointing at the row
145797 ** that is to be updated. So bypass the code that searches for the
145798 ** row(s) to be updated.
145799 */
145800 pWInfo = 0;
145801 eOnePass = ONEPASS_SINGLE1;
145802 sqlite3ExprIfFalse(pParse, pWhere, labelBreak, SQLITE_JUMPIFNULL0x10);
145803 bFinishSeek = 0;
145804 }else{
145805 /* Begin the database scan.
145806 **
145807 ** Do not consider a single-pass strategy for a multi-row update if
145808 ** there are any triggers or foreign keys to process, or rows may
145809 ** be deleted as a result of REPLACE conflict handling. Any of these
145810 ** things might disturb a cursor being used to scan through the table
145811 ** or index, causing a single-pass approach to malfunction. */
145812 flags = WHERE_ONEPASS_DESIRED0x0004;
145813 if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
145814 flags |= WHERE_ONEPASS_MULTIROW0x0008;
145815 }
145816 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere,0,0,0,flags,iIdxCur);
145817 if( pWInfo==0 ) goto update_cleanup;
145818
145819 /* A one-pass strategy that might update more than one row may not
145820 ** be used if any column of the index used for the scan is being
145821 ** updated. Otherwise, if there is an index on "b", statements like
145822 ** the following could create an infinite loop:
145823 **
145824 ** UPDATE t1 SET b=b+1 WHERE b>?
145825 **
145826 ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI
145827 ** strategy that uses an index for which one or more columns are being
145828 ** updated. */
145829 eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
145830 bFinishSeek = sqlite3WhereUsesDeferredSeek(pWInfo);
145831 if( eOnePass!=ONEPASS_SINGLE1 ){
145832 sqlite3MultiWrite(pParse);
145833 if( eOnePass==ONEPASS_MULTI2 ){
145834 int iCur = aiCurOnePass[1];
145835 if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
145836 eOnePass = ONEPASS_OFF0;
145837 }
145838 assert( iCur!=iDataCur || !HasRowid(pTab) )((void) (0));
145839 }
145840 }
145841 }
145842
145843 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
145844 /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF
145845 ** mode, write the rowid into the FIFO. In either of the one-pass modes,
145846 ** leave it in register regOldRowid. */
145847 sqlite3VdbeAddOp2(v, OP_Rowid135, iDataCur, regOldRowid);
145848 if( eOnePass==ONEPASS_OFF0 ){
145849 aRegIdx[nAllIdx] = ++pParse->nMem;
145850 sqlite3VdbeAddOp3(v, OP_Insert128, iEph, regRowSet, regOldRowid);
145851 }else{
145852 if( ALWAYS(addrOpen)(addrOpen) ) sqlite3VdbeChangeToNoop(v, addrOpen);
145853 }
145854 }else{
145855 /* Read the PK of the current row into an array of registers. In
145856 ** ONEPASS_OFF mode, serialize the array into a record and store it in
145857 ** the ephemeral table. Or, in ONEPASS_SINGLE or MULTI mode, change
145858 ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table
145859 ** is not required) and leave the PK fields in the array of registers. */
145860 for(i=0; i<nPk; i++){
145861 assert( pPk->aiColumn[i]>=0 )((void) (0));
145862 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur,
145863 pPk->aiColumn[i], iPk+i);
145864 }
145865 if( eOnePass ){
145866 if( addrOpen ) sqlite3VdbeChangeToNoop(v, addrOpen);
145867 nKey = nPk;
145868 regKey = iPk;
145869 }else{
145870 sqlite3VdbeAddOp4(v, OP_MakeRecord97, iPk, nPk, regKey,
145871 sqlite3IndexAffinityStr(db, pPk), nPk);
145872 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iEph, regKey, iPk, nPk);
145873 }
145874 }
145875 }
145876
145877 if( pUpsert==0 ){
145878 if( nChangeFrom==0 && eOnePass!=ONEPASS_MULTI2 ){
145879 sqlite3WhereEnd(pWInfo);
145880 }
145881
145882 if( !isView ){
145883 int addrOnce = 0;
145884
145885 /* Open every index that needs updating. */
145886 if( eOnePass!=ONEPASS_OFF0 ){
145887 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
145888 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
145889 }
145890
145891 if( eOnePass==ONEPASS_MULTI2 && (nIdx-(aiCurOnePass[1]>=0))>0 ){
145892 addrOnce = sqlite3VdbeAddOp0(v, OP_Once14); VdbeCoverage(v);
145893 }
145894 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite113, 0, iBaseCur,
145895 aToOpen, 0, 0);
145896 if( addrOnce ){
145897 sqlite3VdbeJumpHereOrPopInst(v, addrOnce);
145898 }
145899 }
145900
145901 /* Top of the update loop */
145902 if( eOnePass!=ONEPASS_OFF0 ){
145903 if( aiCurOnePass[0]!=iDataCur
145904 && aiCurOnePass[1]!=iDataCur
145905#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
145906 && !isView
145907#endif
145908 ){
145909 assert( pPk )((void) (0));
145910 sqlite3VdbeAddOp4Int(v, OP_NotFound27, iDataCur, labelBreak, regKey,nKey);
145911 VdbeCoverage(v);
145912 }
145913 if( eOnePass!=ONEPASS_SINGLE1 ){
145914 labelContinue = sqlite3VdbeMakeLabel(pParse);
145915 }
145916 sqlite3VdbeAddOp2(v, OP_IsNull50, pPk ? regKey : regOldRowid, labelBreak);
145917 VdbeCoverageIf(v, pPk==0);
145918 VdbeCoverageIf(v, pPk!=0);
145919 }else if( pPk || nChangeFrom ){
145920 labelContinue = sqlite3VdbeMakeLabel(pParse);
145921 sqlite3VdbeAddOp2(v, OP_Rewind35, iEph, labelBreak); VdbeCoverage(v);
145922 addrTop = sqlite3VdbeCurrentAddr(v);
145923 if( nChangeFrom ){
145924 if( !isView ){
145925 if( pPk ){
145926 for(i=0; i<nPk; i++){
145927 sqlite3VdbeAddOp3(v, OP_Column94, iEph, i, iPk+i);
145928 }
145929 sqlite3VdbeAddOp4Int(
145930 v, OP_NotFound27, iDataCur, labelContinue, iPk, nPk
145931 ); VdbeCoverage(v);
145932 }else{
145933 sqlite3VdbeAddOp2(v, OP_Rowid135, iEph, regOldRowid);
145934 sqlite3VdbeAddOp3(
145935 v, OP_NotExists30, iDataCur, labelContinue, regOldRowid
145936 ); VdbeCoverage(v);
145937 }
145938 }
145939 }else{
145940 sqlite3VdbeAddOp2(v, OP_RowData134, iEph, regKey);
145941 sqlite3VdbeAddOp4Int(v, OP_NotFound27, iDataCur, labelContinue, regKey,0);
145942 VdbeCoverage(v);
145943 }
145944 }else{
145945 sqlite3VdbeAddOp2(v, OP_Rewind35, iEph, labelBreak); VdbeCoverage(v);
145946 labelContinue = sqlite3VdbeMakeLabel(pParse);
145947 addrTop = sqlite3VdbeAddOp2(v, OP_Rowid135, iEph, regOldRowid);
145948 VdbeCoverage(v);
145949 sqlite3VdbeAddOp3(v, OP_NotExists30, iDataCur, labelContinue, regOldRowid);
145950 VdbeCoverage(v);
145951 }
145952 }
145953
145954 /* If the rowid value will change, set register regNewRowid to
145955 ** contain the new value. If the rowid is not being modified,
145956 ** then regNewRowid is the same register as regOldRowid, which is
145957 ** already populated. */
145958 assert( chngKey || pTrigger || hasFK || regOldRowid==regNewRowid )((void) (0));
145959 if( chngRowid ){
145960 assert( iRowidExpr>=0 )((void) (0));
145961 if( nChangeFrom==0 ){
145962 sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
145963 }else{
145964 sqlite3VdbeAddOp3(v, OP_Column94, iEph, iRowidExpr, regNewRowid);
145965 }
145966 sqlite3VdbeAddOp1(v, OP_MustBeInt12, regNewRowid); VdbeCoverage(v);
145967 }
145968
145969 /* Compute the old pre-UPDATE content of the row being changed, if that
145970 ** information is needed */
145971 if( chngPk || hasFK || pTrigger ){
145972 u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
145973 oldmask |= sqlite3TriggerColmask(pParse,
145974 pTrigger, pChanges, 0, TRIGGER_BEFORE1|TRIGGER_AFTER2, pTab, onError
145975 );
145976 for(i=0; i<pTab->nCol; i++){
145977 u32 colFlags = pTab->aCol[i].colFlags;
145978 k = sqlite3TableColumnToStorage(pTab, i) + regOld;
145979 if( oldmask==0xffffffff
145980 || (i<32 && (oldmask & MASKBIT32(i)(((unsigned int)1)<<(i)))!=0)
145981 || (colFlags & COLFLAG_PRIMKEY0x0001)!=0
145982 ){
145983 testcase( oldmask!=0xffffffff && i==31 );
145984 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, k);
145985 }else{
145986 sqlite3VdbeAddOp2(v, OP_Null75, 0, k);
145987 }
145988 }
145989 if( chngRowid==0 && pPk==0 ){
145990 sqlite3VdbeAddOp2(v, OP_Copy80, regOldRowid, regNewRowid);
145991 }
145992 }
145993
145994 /* Populate the array of registers beginning at regNew with the new
145995 ** row data. This array is used to check constants, create the new
145996 ** table and index records, and as the values for any new.* references
145997 ** made by triggers.
145998 **
145999 ** If there are one or more BEFORE triggers, then do not populate the
146000 ** registers associated with columns that are (a) not modified by
146001 ** this UPDATE statement and (b) not accessed by new.* references. The
146002 ** values for registers not modified by the UPDATE must be reloaded from
146003 ** the database after the BEFORE triggers are fired anyway (as the trigger
146004 ** may have modified them). So not loading those that are not going to
146005 ** be used eliminates some redundant opcodes.
146006 */
146007 newmask = sqlite3TriggerColmask(
146008 pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE1, pTab, onError
146009 );
146010 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
146011 if( i==pTab->iPKey ){
146012 sqlite3VdbeAddOp2(v, OP_Null75, 0, k);
146013 }else if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED0x0060)!=0 ){
146014 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020 ) k--;
146015 }else{
146016 j = aXRef[i];
146017 if( j>=0 ){
146018 if( nChangeFrom ){
146019 int nOff = (isView ? pTab->nCol : nPk);
146020 assert( eOnePass==ONEPASS_OFF )((void) (0));
146021 sqlite3VdbeAddOp3(v, OP_Column94, iEph, nOff+j, k);
146022 }else{
146023 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, k);
146024 }
146025 }else if( 0==(tmask&TRIGGER_BEFORE1) || i>31 || (newmask & MASKBIT32(i)(((unsigned int)1)<<(i))) ){
146026 /* This branch loads the value of a column that will not be changed
146027 ** into a register. This is done if there are no BEFORE triggers, or
146028 ** if there are one or more BEFORE triggers that use this value via
146029 ** a new.* reference in a trigger program.
146030 */
146031 testcase( i==31 );
146032 testcase( i==32 );
146033 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, k);
146034 bFinishSeek = 0;
146035 }else{
146036 sqlite3VdbeAddOp2(v, OP_Null75, 0, k);
146037 }
146038 }
146039 }
146040#ifndef SQLITE_OMIT_GENERATED_COLUMNS
146041 if( pTab->tabFlags & TF_HasGenerated0x00000060 ){
146042 testcase( pTab->tabFlags & TF_HasVirtual );
146043 testcase( pTab->tabFlags & TF_HasStored );
146044 sqlite3ComputeGeneratedColumns(pParse, regNew, pTab);
146045 }
146046#endif
146047
146048 /* Fire any BEFORE UPDATE triggers. This happens before constraints are
146049 ** verified. One could argue that this is wrong.
146050 */
146051 if( tmask&TRIGGER_BEFORE1 ){
146052 sqlite3TableAffinity(v, pTab, regNew);
146053 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE129, pChanges,
146054 TRIGGER_BEFORE1, pTab, regOldRowid, onError, labelContinue);
146055
146056 if( !isView ){
146057 /* The row-trigger may have deleted the row being updated. In this
146058 ** case, jump to the next row. No updates or AFTER triggers are
146059 ** required. This behavior - what happens when the row being updated
146060 ** is deleted or renamed by a BEFORE trigger - is left undefined in the
146061 ** documentation.
146062 */
146063 if( pPk ){
146064 sqlite3VdbeAddOp4Int(v, OP_NotFound27,iDataCur,labelContinue,regKey,nKey);
146065 VdbeCoverage(v);
146066 }else{
146067 sqlite3VdbeAddOp3(v, OP_NotExists30, iDataCur, labelContinue,regOldRowid);
146068 VdbeCoverage(v);
146069 }
146070
146071 /* After-BEFORE-trigger-reload-loop:
146072 ** If it did not delete it, the BEFORE trigger may still have modified
146073 ** some of the columns of the row being updated. Load the values for
146074 ** all columns not modified by the update statement into their registers
146075 ** in case this has happened. Only unmodified columns are reloaded.
146076 ** The values computed for modified columns use the values before the
146077 ** BEFORE trigger runs. See test case trigger1-18.0 (added 2018-04-26)
146078 ** for an example.
146079 */
146080 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
146081 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED0x0060 ){
146082 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020 ) k--;
146083 }else if( aXRef[i]<0 && i!=pTab->iPKey ){
146084 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, k);
146085 }
146086 }
146087#ifndef SQLITE_OMIT_GENERATED_COLUMNS
146088 if( pTab->tabFlags & TF_HasGenerated0x00000060 ){
146089 testcase( pTab->tabFlags & TF_HasVirtual );
146090 testcase( pTab->tabFlags & TF_HasStored );
146091 sqlite3ComputeGeneratedColumns(pParse, regNew, pTab);
146092 }
146093#endif
146094 }
146095 }
146096
146097 if( !isView ){
146098 /* Do constraint checks. */
146099 assert( regOldRowid>0 )((void) (0));
146100 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
146101 regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace,
146102 aXRef, 0);
146103
146104 /* If REPLACE conflict handling may have been used, or if the PK of the
146105 ** row is changing, then the GenerateConstraintChecks() above may have
146106 ** moved cursor iDataCur. Reseek it. */
146107 if( bReplace || chngKey ){
146108 if( pPk ){
146109 sqlite3VdbeAddOp4Int(v, OP_NotFound27,iDataCur,labelContinue,regKey,nKey);
146110 }else{
146111 sqlite3VdbeAddOp3(v, OP_NotExists30, iDataCur, labelContinue,regOldRowid);
146112 }
146113 VdbeCoverage(v);
146114 }
146115
146116 /* Do FK constraint checks. */
146117 if( hasFK ){
146118 sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngKey);
146119 }
146120
146121 /* Delete the index entries associated with the current record. */
146122 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, -1);
146123
146124 /* We must run the OP_FinishSeek opcode to resolve a prior
146125 ** OP_DeferredSeek if there is any possibility that there have been
146126 ** no OP_Column opcodes since the OP_DeferredSeek was issued. But
146127 ** we want to avoid the OP_FinishSeek if possible, as running it
146128 ** costs CPU cycles. */
146129 if( bFinishSeek ){
146130 sqlite3VdbeAddOp1(v, OP_FinishSeek143, iDataCur);
146131 }
146132
146133 /* If changing the rowid value, or if there are foreign key constraints
146134 ** to process, delete the old record. Otherwise, add a noop OP_Delete
146135 ** to invoke the pre-update hook.
146136 **
146137 ** That (regNew==regnewRowid+1) is true is also important for the
146138 ** pre-update hook. If the caller invokes preupdate_new(), the returned
146139 ** value is copied from memory cell (regNewRowid+1+iCol), where iCol
146140 ** is the column index supplied by the user.
146141 */
146142 assert( regNew==regNewRowid+1 )((void) (0));
146143#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
146144 sqlite3VdbeAddOp3(v, OP_Delete130, iDataCur,
146145 OPFLAG_ISUPDATE0x04 | ((hasFK>1 || chngKey) ? 0 : OPFLAG_ISNOOP0x40),
146146 regNewRowid
146147 );
146148 if( eOnePass==ONEPASS_MULTI2 ){
146149 assert( hasFK==0 && chngKey==0 )((void) (0));
146150 sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION0x02);
146151 }
146152 if( !pParse->nested ){
146153 sqlite3VdbeAppendP4(v, pTab, P4_TABLE(-5));
146154 }
146155#else
146156 if( hasFK>1 || chngKey ){
146157 sqlite3VdbeAddOp2(v, OP_Delete130, iDataCur, 0);
146158 }
146159#endif
146160
146161 if( hasFK ){
146162 sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey);
146163 }
146164
146165 /* Insert the new index entries and the new record. */
146166 sqlite3CompleteInsertion(
146167 pParse, pTab, iDataCur, iIdxCur, regNewRowid, aRegIdx,
146168 OPFLAG_ISUPDATE0x04 | (eOnePass==ONEPASS_MULTI2 ? OPFLAG_SAVEPOSITION0x02 : 0),
146169 0, 0
146170 );
146171
146172 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
146173 ** handle rows (possibly in other tables) that refer via a foreign key
146174 ** to the row just updated. */
146175 if( hasFK ){
146176 sqlite3FkActions(pParse, pTab, pChanges, regOldRowid, aXRef, chngKey);
146177 }
146178 }
146179
146180 /* Increment the row counter
146181 */
146182 if( regRowCount ){
146183 sqlite3VdbeAddOp2(v, OP_AddImm86, regRowCount, 1);
146184 }
146185
146186 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE129, pChanges,
146187 TRIGGER_AFTER2, pTab, regOldRowid, onError, labelContinue);
146188
146189 /* Repeat the above with the next record to be updated, until
146190 ** all record selected by the WHERE clause have been updated.
146191 */
146192 if( eOnePass==ONEPASS_SINGLE1 ){
146193 /* Nothing to do at end-of-loop for a single-pass */
146194 }else if( eOnePass==ONEPASS_MULTI2 ){
146195 sqlite3VdbeResolveLabel(v, labelContinue);
146196 sqlite3WhereEnd(pWInfo);
146197 }else{
146198 sqlite3VdbeResolveLabel(v, labelContinue);
146199 sqlite3VdbeAddOp2(v, OP_Next38, iEph, addrTop); VdbeCoverage(v);
146200 }
146201 sqlite3VdbeResolveLabel(v, labelBreak);
146202
146203 /* Update the sqlite_sequence table by storing the content of the
146204 ** maximum rowid counter values recorded while inserting into
146205 ** autoincrement tables.
146206 */
146207 if( pParse->nested==0 && pParse->pTriggerTab==0 && pUpsert==0 ){
146208 sqlite3AutoincrementEnd(pParse);
146209 }
146210
146211 /*
146212 ** Return the number of rows that were changed, if we are tracking
146213 ** that information.
146214 */
146215 if( regRowCount ){
146216 sqlite3CodeChangeCount(v, regRowCount, "rows updated");
146217 }
146218
146219update_cleanup:
146220 sqlite3AuthContextPop(&sContext);
146221 sqlite3DbFree(db, aXRef); /* Also frees aRegIdx[] and aToOpen[] */
146222 sqlite3SrcListDelete(db, pTabList);
146223 sqlite3ExprListDelete(db, pChanges);
146224 sqlite3ExprDelete(db, pWhere);
146225#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT1)
146226 sqlite3ExprListDelete(db, pOrderBy);
146227 sqlite3ExprDelete(db, pLimit);
146228#endif
146229 return;
146230}
146231/* Make sure "isView" and other macros defined above are undefined. Otherwise
146232** they may interfere with compilation of other functions in this file
146233** (or in another file, if this file becomes part of the amalgamation). */
146234#ifdef isView
146235 #undef isView
146236#endif
146237#ifdef pTrigger
146238 #undef pTrigger
146239#endif
146240
146241#ifndef SQLITE_OMIT_VIRTUALTABLE
146242/*
146243** Generate code for an UPDATE of a virtual table.
146244**
146245** There are two possible strategies - the default and the special
146246** "onepass" strategy. Onepass is only used if the virtual table
146247** implementation indicates that pWhere may match at most one row.
146248**
146249** The default strategy is to create an ephemeral table that contains
146250** for each row to be changed:
146251**
146252** (A) The original rowid of that row.
146253** (B) The revised rowid for the row.
146254** (C) The content of every column in the row.
146255**
146256** Then loop through the contents of this ephemeral table executing a
146257** VUpdate for each row. When finished, drop the ephemeral table.
146258**
146259** The "onepass" strategy does not use an ephemeral table. Instead, it
146260** stores the same values (A, B and C above) in a register array and
146261** makes a single invocation of VUpdate.
146262*/
146263static void updateVirtualTable(
146264 Parse *pParse, /* The parsing context */
146265 SrcList *pSrc, /* The virtual table to be modified */
146266 Table *pTab, /* The virtual table */
146267 ExprList *pChanges, /* The columns to change in the UPDATE statement */
146268 Expr *pRowid, /* Expression used to recompute the rowid */
146269 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
146270 Expr *pWhere, /* WHERE clause of the UPDATE statement */
146271 int onError /* ON CONFLICT strategy */
146272){
146273 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
146274 int ephemTab; /* Table holding the result of the SELECT */
146275 int i; /* Loop counter */
146276 sqlite3 *db = pParse->db; /* Database connection */
146277 const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
146278 WhereInfo *pWInfo = 0;
146279 int nArg = 2 + pTab->nCol; /* Number of arguments to VUpdate */
146280 int regArg; /* First register in VUpdate arg array */
146281 int regRec; /* Register in which to assemble record */
146282 int regRowid; /* Register for ephem table rowid */
146283 int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */
146284 int aDummy[2]; /* Unused arg for sqlite3WhereOkOnePass() */
146285 int eOnePass; /* True to use onepass strategy */
146286 int addr; /* Address of OP_OpenEphemeral */
146287
146288 /* Allocate nArg registers in which to gather the arguments for VUpdate. Then
146289 ** create and open the ephemeral table in which the records created from
146290 ** these arguments will be temporarily stored. */
146291 assert( v )((void) (0));
146292 ephemTab = pParse->nTab++;
146293 addr= sqlite3VdbeAddOp2(v, OP_OpenEphemeral118, ephemTab, nArg);
146294 regArg = pParse->nMem + 1;
146295 pParse->nMem += nArg;
146296 if( pSrc->nSrc>1 ){
146297 Index *pPk = 0;
146298 Expr *pRow;
146299 ExprList *pList;
146300 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
146301 if( pRowid ){
146302 pRow = sqlite3ExprDup(db, pRowid, 0);
146303 }else{
146304 pRow = sqlite3PExpr(pParse, TK_ROW75, 0, 0);
146305 }
146306 }else{
146307 i16 iPk; /* PRIMARY KEY column */
146308 pPk = sqlite3PrimaryKeyIndex(pTab);
146309 assert( pPk!=0 )((void) (0));
146310 assert( pPk->nKeyCol==1 )((void) (0));
146311 iPk = pPk->aiColumn[0];
146312 if( aXRef[iPk]>=0 ){
146313 pRow = sqlite3ExprDup(db, pChanges->a[aXRef[iPk]].pExpr, 0);
146314 }else{
146315 pRow = exprRowColumn(pParse, iPk);
146316 }
146317 }
146318 pList = sqlite3ExprListAppend(pParse, 0, pRow);
146319
146320 for(i=0; i<pTab->nCol; i++){
146321 if( aXRef[i]>=0 ){
146322 pList = sqlite3ExprListAppend(pParse, pList,
146323 sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0)
146324 );
146325 }else{
146326 pList = sqlite3ExprListAppend(pParse, pList, exprRowColumn(pParse, i));
146327 }
146328 }
146329
146330 updateFromSelect(pParse, ephemTab, pPk, pList, pSrc, pWhere, 0, 0);
146331 sqlite3ExprListDelete(db, pList);
146332 eOnePass = ONEPASS_OFF0;
146333 }else{
146334 regRec = ++pParse->nMem;
146335 regRowid = ++pParse->nMem;
146336
146337 /* Start scanning the virtual table */
146338 pWInfo = sqlite3WhereBegin(
146339 pParse, pSrc, pWhere, 0, 0, 0, WHERE_ONEPASS_DESIRED0x0004, 0
146340 );
146341 if( pWInfo==0 ) return;
146342
146343 /* Populate the argument registers. */
146344 for(i=0; i<pTab->nCol; i++){
146345 assert( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 )((void) (0));
146346 if( aXRef[i]>=0 ){
146347 sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i);
146348 }else{
146349 sqlite3VdbeAddOp3(v, OP_VColumn175, iCsr, i, regArg+2+i);
146350 sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG0x01);/* For sqlite3_vtab_nochange() */
146351 }
146352 }
146353 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
146354 sqlite3VdbeAddOp2(v, OP_Rowid135, iCsr, regArg);
146355 if( pRowid ){
146356 sqlite3ExprCode(pParse, pRowid, regArg+1);
146357 }else{
146358 sqlite3VdbeAddOp2(v, OP_Rowid135, iCsr, regArg+1);
146359 }
146360 }else{
146361 Index *pPk; /* PRIMARY KEY index */
146362 i16 iPk; /* PRIMARY KEY column */
146363 pPk = sqlite3PrimaryKeyIndex(pTab);
146364 assert( pPk!=0 )((void) (0));
146365 assert( pPk->nKeyCol==1 )((void) (0));
146366 iPk = pPk->aiColumn[0];
146367 sqlite3VdbeAddOp3(v, OP_VColumn175, iCsr, iPk, regArg);
146368 sqlite3VdbeAddOp2(v, OP_SCopy81, regArg+2+iPk, regArg+1);
146369 }
146370
146371 eOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy);
146372
146373 /* There is no ONEPASS_MULTI on virtual tables */
146374 assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE )((void) (0));
146375
146376 if( eOnePass ){
146377 /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
146378 ** above. */
146379 sqlite3VdbeChangeToNoop(v, addr);
146380 sqlite3VdbeAddOp1(v, OP_Close122, iCsr);
146381 }else{
146382 /* Create a record from the argument register contents and insert it into
146383 ** the ephemeral table. */
146384 sqlite3MultiWrite(pParse);
146385 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regArg, nArg, regRec);
146386#if defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_NULL_TRIM)
146387 /* Signal an assert() within OP_MakeRecord that it is allowed to
146388 ** accept no-change records with serial_type 10 */
146389 sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG_MAGIC0x6d);
146390#endif
146391 sqlite3VdbeAddOp2(v, OP_NewRowid127, ephemTab, regRowid);
146392 sqlite3VdbeAddOp3(v, OP_Insert128, ephemTab, regRec, regRowid);
146393 }
146394 }
146395
146396
146397 if( eOnePass==ONEPASS_OFF0 ){
146398 /* End the virtual table scan */
146399 if( pSrc->nSrc==1 ){
146400 sqlite3WhereEnd(pWInfo);
146401 }
146402
146403 /* Begin scannning through the ephemeral table. */
146404 addr = sqlite3VdbeAddOp1(v, OP_Rewind35, ephemTab); VdbeCoverage(v);
146405
146406 /* Extract arguments from the current row of the ephemeral table and
146407 ** invoke the VUpdate method. */
146408 for(i=0; i<nArg; i++){
146409 sqlite3VdbeAddOp3(v, OP_Column94, ephemTab, i, regArg+i);
146410 }
146411 }
146412 sqlite3VtabMakeWritable(pParse, pTab);
146413 sqlite3VdbeAddOp4(v, OP_VUpdate7, 0, nArg, regArg, pVTab, P4_VTAB(-11));
146414 sqlite3VdbeChangeP5(v, onError==OE_Default11 ? OE_Abort2 : onError);
146415 sqlite3MayAbort(pParse);
146416
146417 /* End of the ephemeral table scan. Or, if using the onepass strategy,
146418 ** jump to here if the scan visited zero rows. */
146419 if( eOnePass==ONEPASS_OFF0 ){
146420 sqlite3VdbeAddOp2(v, OP_Next38, ephemTab, addr+1); VdbeCoverage(v);
146421 sqlite3VdbeJumpHere(v, addr);
146422 sqlite3VdbeAddOp2(v, OP_Close122, ephemTab, 0);
146423 }else{
146424 sqlite3WhereEnd(pWInfo);
146425 }
146426}
146427#endif /* SQLITE_OMIT_VIRTUALTABLE */
146428
146429/************** End of update.c **********************************************/
146430/************** Begin file upsert.c ******************************************/
146431/*
146432** 2018-04-12
146433**
146434** The author disclaims copyright to this source code. In place of
146435** a legal notice, here is a blessing:
146436**
146437** May you do good and not evil.
146438** May you find forgiveness for yourself and forgive others.
146439** May you share freely, never taking more than you give.
146440**
146441*************************************************************************
146442** This file contains code to implement various aspects of UPSERT
146443** processing and handling of the Upsert object.
146444*/
146445/* #include "sqliteInt.h" */
146446
146447#ifndef SQLITE_OMIT_UPSERT
146448/*
146449** Free a list of Upsert objects
146450*/
146451static void SQLITE_NOINLINE__attribute__((noinline)) upsertDelete(sqlite3 *db, Upsert *p){
146452 do{
146453 Upsert *pNext = p->pNextUpsert;
146454 sqlite3ExprListDelete(db, p->pUpsertTarget);
146455 sqlite3ExprDelete(db, p->pUpsertTargetWhere);
146456 sqlite3ExprListDelete(db, p->pUpsertSet);
146457 sqlite3ExprDelete(db, p->pUpsertWhere);
146458 sqlite3DbFree(db, p->pToFree);
146459 sqlite3DbFree(db, p);
146460 p = pNext;
146461 }while( p );
146462}
146463SQLITE_PRIVATEstatic void sqlite3UpsertDelete(sqlite3 *db, Upsert *p){
146464 if( p ) upsertDelete(db, p);
146465}
146466
146467
146468/*
146469** Duplicate an Upsert object.
146470*/
146471SQLITE_PRIVATEstatic Upsert *sqlite3UpsertDup(sqlite3 *db, Upsert *p){
146472 if( p==0 ) return 0;
146473 return sqlite3UpsertNew(db,
146474 sqlite3ExprListDup(db, p->pUpsertTarget, 0),
146475 sqlite3ExprDup(db, p->pUpsertTargetWhere, 0),
146476 sqlite3ExprListDup(db, p->pUpsertSet, 0),
146477 sqlite3ExprDup(db, p->pUpsertWhere, 0),
146478 sqlite3UpsertDup(db, p->pNextUpsert)
146479 );
146480}
146481
146482/*
146483** Create a new Upsert object.
146484*/
146485SQLITE_PRIVATEstatic Upsert *sqlite3UpsertNew(
146486 sqlite3 *db, /* Determines which memory allocator to use */
146487 ExprList *pTarget, /* Target argument to ON CONFLICT, or NULL */
146488 Expr *pTargetWhere, /* Optional WHERE clause on the target */
146489 ExprList *pSet, /* UPDATE columns, or NULL for a DO NOTHING */
146490 Expr *pWhere, /* WHERE clause for the ON CONFLICT UPDATE */
146491 Upsert *pNext /* Next ON CONFLICT clause in the list */
146492){
146493 Upsert *pNew;
146494 pNew = sqlite3DbMallocZero(db, sizeof(Upsert));
146495 if( pNew==0 ){
146496 sqlite3ExprListDelete(db, pTarget);
146497 sqlite3ExprDelete(db, pTargetWhere);
146498 sqlite3ExprListDelete(db, pSet);
146499 sqlite3ExprDelete(db, pWhere);
146500 sqlite3UpsertDelete(db, pNext);
146501 return 0;
146502 }else{
146503 pNew->pUpsertTarget = pTarget;
146504 pNew->pUpsertTargetWhere = pTargetWhere;
146505 pNew->pUpsertSet = pSet;
146506 pNew->pUpsertWhere = pWhere;
146507 pNew->isDoUpdate = pSet!=0;
146508 pNew->pNextUpsert = pNext;
146509 }
146510 return pNew;
146511}
146512
146513/*
146514** Analyze the ON CONFLICT clause described by pUpsert. Resolve all
146515** symbols in the conflict-target.
146516**
146517** Return SQLITE_OK if everything works, or an error code is something
146518** is wrong.
146519*/
146520SQLITE_PRIVATEstatic int sqlite3UpsertAnalyzeTarget(
146521 Parse *pParse, /* The parsing context */
146522 SrcList *pTabList, /* Table into which we are inserting */
146523 Upsert *pUpsert /* The ON CONFLICT clauses */
146524){
146525 Table *pTab; /* That table into which we are inserting */
146526 int rc; /* Result code */
146527 int iCursor; /* Cursor used by pTab */
146528 Index *pIdx; /* One of the indexes of pTab */
146529 ExprList *pTarget; /* The conflict-target clause */
146530 Expr *pTerm; /* One term of the conflict-target clause */
146531 NameContext sNC; /* Context for resolving symbolic names */
146532 Expr sCol[2]; /* Index column converted into an Expr */
146533 int nClause = 0; /* Counter of ON CONFLICT clauses */
146534
146535 assert( pTabList->nSrc==1 )((void) (0));
146536 assert( pTabList->a[0].pTab!=0 )((void) (0));
146537 assert( pUpsert!=0 )((void) (0));
146538 assert( pUpsert->pUpsertTarget!=0 )((void) (0));
146539
146540 /* Resolve all symbolic names in the conflict-target clause, which
146541 ** includes both the list of columns and the optional partial-index
146542 ** WHERE clause.
146543 */
146544 memset(&sNC, 0, sizeof(sNC));
146545 sNC.pParse = pParse;
146546 sNC.pSrcList = pTabList;
146547 for(; pUpsert && pUpsert->pUpsertTarget;
146548 pUpsert=pUpsert->pNextUpsert, nClause++){
146549 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
146550 if( rc ) return rc;
146551 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
146552 if( rc ) return rc;
146553
146554 /* Check to see if the conflict target matches the rowid. */
146555 pTab = pTabList->a[0].pTab;
146556 pTarget = pUpsert->pUpsertTarget;
146557 iCursor = pTabList->a[0].iCursor;
146558 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0)
146559 && pTarget->nExpr==1
146560 && (pTerm = pTarget->a[0].pExpr)->op==TK_COLUMN167
146561 && pTerm->iColumn==XN_ROWID(-1)
146562 ){
146563 /* The conflict-target is the rowid of the primary table */
146564 assert( pUpsert->pUpsertIdx==0 )((void) (0));
146565 continue;
146566 }
146567
146568 /* Initialize sCol[0..1] to be an expression parse tree for a
146569 ** single column of an index. The sCol[0] node will be the TK_COLLATE
146570 ** operator and sCol[1] will be the TK_COLUMN operator. Code below
146571 ** will populate the specific collation and column number values
146572 ** prior to comparing against the conflict-target expression.
146573 */
146574 memset(sCol, 0, sizeof(sCol));
146575 sCol[0].op = TK_COLLATE113;
146576 sCol[0].pLeft = &sCol[1];
146577 sCol[1].op = TK_COLUMN167;
146578 sCol[1].iTable = pTabList->a[0].iCursor;
146579
146580 /* Check for matches against other indexes */
146581 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
146582 int ii, jj, nn;
146583 if( !IsUniqueIndex(pIdx)((pIdx)->onError!=0) ) continue;
146584 if( pTarget->nExpr!=pIdx->nKeyCol ) continue;
146585 if( pIdx->pPartIdxWhere ){
146586 if( pUpsert->pUpsertTargetWhere==0 ) continue;
146587 if( sqlite3ExprCompare(pParse, pUpsert->pUpsertTargetWhere,
146588 pIdx->pPartIdxWhere, iCursor)!=0 ){
146589 continue;
146590 }
146591 }
146592 nn = pIdx->nKeyCol;
146593 for(ii=0; ii<nn; ii++){
146594 Expr *pExpr;
146595 sCol[0].u.zToken = (char*)pIdx->azColl[ii];
146596 if( pIdx->aiColumn[ii]==XN_EXPR(-2) ){
146597 assert( pIdx->aColExpr!=0 )((void) (0));
146598 assert( pIdx->aColExpr->nExpr>ii )((void) (0));
146599 pExpr = pIdx->aColExpr->a[ii].pExpr;
146600 if( pExpr->op!=TK_COLLATE113 ){
146601 sCol[0].pLeft = pExpr;
146602 pExpr = &sCol[0];
146603 }
146604 }else{
146605 sCol[0].pLeft = &sCol[1];
146606 sCol[1].iColumn = pIdx->aiColumn[ii];
146607 pExpr = &sCol[0];
146608 }
146609 for(jj=0; jj<nn; jj++){
146610 if( sqlite3ExprCompare(pParse,pTarget->a[jj].pExpr,pExpr,iCursor)<2 ){
146611 break; /* Column ii of the index matches column jj of target */
146612 }
146613 }
146614 if( jj>=nn ){
146615 /* The target contains no match for column jj of the index */
146616 break;
146617 }
146618 }
146619 if( ii<nn ){
146620 /* Column ii of the index did not match any term of the conflict target.
146621 ** Continue the search with the next index. */
146622 continue;
146623 }
146624 pUpsert->pUpsertIdx = pIdx;
146625 break;
146626 }
146627 if( pUpsert->pUpsertIdx==0 ){
146628 char zWhich[16];
146629 if( nClause==0 && pUpsert->pNextUpsert==0 ){
146630 zWhich[0] = 0;
146631 }else{
146632 sqlite3_snprintf(sizeof(zWhich),zWhich,"%r ", nClause+1);
146633 }
146634 sqlite3ErrorMsg(pParse, "%sON CONFLICT clause does not match any "
146635 "PRIMARY KEY or UNIQUE constraint", zWhich);
146636 return SQLITE_ERROR1;
146637 }
146638 }
146639 return SQLITE_OK0;
146640}
146641
146642/*
146643** Return true if pUpsert is the last ON CONFLICT clause with a
146644** conflict target, or if pUpsert is followed by another ON CONFLICT
146645** clause that targets the INTEGER PRIMARY KEY.
146646*/
146647SQLITE_PRIVATEstatic int sqlite3UpsertNextIsIPK(Upsert *pUpsert){
146648 Upsert *pNext;
146649 if( NEVER(pUpsert==0)(pUpsert==0) ) return 0;
146650 pNext = pUpsert->pNextUpsert;
146651 if( pNext==0 ) return 1;
146652 if( pNext->pUpsertTarget==0 ) return 1;
146653 if( pNext->pUpsertIdx==0 ) return 1;
146654 return 0;
146655}
146656
146657/*
146658** Given the list of ON CONFLICT clauses described by pUpsert, and
146659** a particular index pIdx, return a pointer to the particular ON CONFLICT
146660** clause that applies to the index. Or, if the index is not subject to
146661** any ON CONFLICT clause, return NULL.
146662*/
146663SQLITE_PRIVATEstatic Upsert *sqlite3UpsertOfIndex(Upsert *pUpsert, Index *pIdx){
146664 while(
146665 pUpsert
146666 && pUpsert->pUpsertTarget!=0
146667 && pUpsert->pUpsertIdx!=pIdx
146668 ){
146669 pUpsert = pUpsert->pNextUpsert;
146670 }
146671 return pUpsert;
146672}
146673
146674/*
146675** Generate bytecode that does an UPDATE as part of an upsert.
146676**
146677** If pIdx is NULL, then the UNIQUE constraint that failed was the IPK.
146678** In this case parameter iCur is a cursor open on the table b-tree that
146679** currently points to the conflicting table row. Otherwise, if pIdx
146680** is not NULL, then pIdx is the constraint that failed and iCur is a
146681** cursor points to the conflicting row.
146682*/
146683SQLITE_PRIVATEstatic void sqlite3UpsertDoUpdate(
146684 Parse *pParse, /* The parsing and code-generating context */
146685 Upsert *pUpsert, /* The ON CONFLICT clause for the upsert */
146686 Table *pTab, /* The table being updated */
146687 Index *pIdx, /* The UNIQUE constraint that failed */
146688 int iCur /* Cursor for pIdx (or pTab if pIdx==NULL) */
146689){
146690 Vdbe *v = pParse->pVdbe;
146691 sqlite3 *db = pParse->db;
146692 SrcList *pSrc; /* FROM clause for the UPDATE */
146693 int iDataCur;
146694 int i;
146695 Upsert *pTop = pUpsert;
146696
146697 assert( v!=0 )((void) (0));
146698 assert( pUpsert!=0 )((void) (0));
146699 iDataCur = pUpsert->iDataCur;
146700 pUpsert = sqlite3UpsertOfIndex(pTop, pIdx);
146701 VdbeNoopComment((v, "Begin DO UPDATE of UPSERT"));
146702 if( pIdx && iCur!=iDataCur ){
146703 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
146704 int regRowid = sqlite3GetTempReg(pParse);
146705 sqlite3VdbeAddOp2(v, OP_IdxRowid142, iCur, regRowid);
146706 sqlite3VdbeAddOp3(v, OP_SeekRowid29, iDataCur, 0, regRowid);
146707 VdbeCoverage(v);
146708 sqlite3ReleaseTempReg(pParse, regRowid);
146709 }else{
146710 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
146711 int nPk = pPk->nKeyCol;
146712 int iPk = pParse->nMem+1;
146713 pParse->nMem += nPk;
146714 for(i=0; i<nPk; i++){
146715 int k;
146716 assert( pPk->aiColumn[i]>=0 )((void) (0));
146717 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
146718 sqlite3VdbeAddOp3(v, OP_Column94, iCur, k, iPk+i);
146719 VdbeComment((v, "%s.%s", pIdx->zName,
146720 pTab->aCol[pPk->aiColumn[i]].zCnName));
146721 }
146722 sqlite3VdbeVerifyAbortable(v, OE_Abort);
146723 i = sqlite3VdbeAddOp4Int(v, OP_Found28, iDataCur, 0, iPk, nPk);
146724 VdbeCoverage(v);
146725 sqlite3VdbeAddOp4(v, OP_Halt70, SQLITE_CORRUPT11, OE_Abort2, 0,
146726 "corrupt database", P4_STATIC(-1));
146727 sqlite3MayAbort(pParse);
146728 sqlite3VdbeJumpHere(v, i);
146729 }
146730 }
146731 /* pUpsert does not own pTop->pUpsertSrc - the outer INSERT statement does.
146732 ** So we have to make a copy before passing it down into sqlite3Update() */
146733 pSrc = sqlite3SrcListDup(db, pTop->pUpsertSrc, 0);
146734 /* excluded.* columns of type REAL need to be converted to a hard real */
146735 for(i=0; i<pTab->nCol; i++){
146736 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL0x45 ){
146737 sqlite3VdbeAddOp1(v, OP_RealAffinity87, pTop->regData+i);
146738 }
146739 }
146740 sqlite3Update(pParse, pSrc, sqlite3ExprListDup(db,pUpsert->pUpsertSet,0),
146741 sqlite3ExprDup(db,pUpsert->pUpsertWhere,0), OE_Abort2, 0, 0, pUpsert);
146742 VdbeNoopComment((v, "End DO UPDATE of UPSERT"));
146743}
146744
146745#endif /* SQLITE_OMIT_UPSERT */
146746
146747/************** End of upsert.c **********************************************/
146748/************** Begin file vacuum.c ******************************************/
146749/*
146750** 2003 April 6
146751**
146752** The author disclaims copyright to this source code. In place of
146753** a legal notice, here is a blessing:
146754**
146755** May you do good and not evil.
146756** May you find forgiveness for yourself and forgive others.
146757** May you share freely, never taking more than you give.
146758**
146759*************************************************************************
146760** This file contains code used to implement the VACUUM command.
146761**
146762** Most of the code in this file may be omitted by defining the
146763** SQLITE_OMIT_VACUUM macro.
146764*/
146765/* #include "sqliteInt.h" */
146766/* #include "vdbeInt.h" */
146767
146768#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
146769
146770/*
146771** Execute zSql on database db.
146772**
146773** If zSql returns rows, then each row will have exactly one
146774** column. (This will only happen if zSql begins with "SELECT".)
146775** Take each row of result and call execSql() again recursively.
146776**
146777** The execSqlF() routine does the same thing, except it accepts
146778** a format string as its third argument
146779*/
146780static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
146781 sqlite3_stmt *pStmt;
146782 int rc;
146783
146784 /* printf("SQL: [%s]\n", zSql); fflush(stdout); */
146785 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
146786 if( rc!=SQLITE_OK0 ) return rc;
146787 while( SQLITE_ROW100==(rc = sqlite3_step(pStmt)) ){
146788 const char *zSubSql = (const char*)sqlite3_column_text(pStmt,0);
146789 assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 )((void) (0));
146790 /* The secondary SQL must be one of CREATE TABLE, CREATE INDEX,
146791 ** or INSERT. Historically there have been attacks that first
146792 ** corrupt the sqlite_schema.sql field with other kinds of statements
146793 ** then run VACUUM to get those statements to execute at inappropriate
146794 ** times. */
146795 if( zSubSql
146796 && (strncmp(zSubSql,"CRE",3)==0 || strncmp(zSubSql,"INS",3)==0)
146797 ){
146798 rc = execSql(db, pzErrMsg, zSubSql);
146799 if( rc!=SQLITE_OK0 ) break;
146800 }
146801 }
146802 assert( rc!=SQLITE_ROW )((void) (0));
146803 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
146804 if( rc ){
146805 sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
146806 }
146807 (void)sqlite3_finalize(pStmt);
146808 return rc;
146809}
146810static int execSqlF(sqlite3 *db, char **pzErrMsg, const char *zSql, ...){
146811 char *z;
146812 va_list ap;
146813 int rc;
146814 va_start(ap, zSql)__builtin_va_start(ap, zSql);
146815 z = sqlite3VMPrintf(db, zSql, ap);
146816 va_end(ap)__builtin_va_end(ap);
146817 if( z==0 ) return SQLITE_NOMEM7;
146818 rc = execSql(db, pzErrMsg, z);
146819 sqlite3DbFree(db, z);
146820 return rc;
146821}
146822
146823/*
146824** The VACUUM command is used to clean up the database,
146825** collapse free space, etc. It is modelled after the VACUUM command
146826** in PostgreSQL. The VACUUM command works as follows:
146827**
146828** (1) Create a new transient database file
146829** (2) Copy all content from the database being vacuumed into
146830** the new transient database file
146831** (3) Copy content from the transient database back into the
146832** original database.
146833**
146834** The transient database requires temporary disk space approximately
146835** equal to the size of the original database. The copy operation of
146836** step (3) requires additional temporary disk space approximately equal
146837** to the size of the original database for the rollback journal.
146838** Hence, temporary disk space that is approximately 2x the size of the
146839** original database is required. Every page of the database is written
146840** approximately 3 times: Once for step (2) and twice for step (3).
146841** Two writes per page are required in step (3) because the original
146842** database content must be written into the rollback journal prior to
146843** overwriting the database with the vacuumed content.
146844**
146845** Only 1x temporary space and only 1x writes would be required if
146846** the copy of step (3) were replaced by deleting the original database
146847** and renaming the transient database as the original. But that will
146848** not work if other processes are attached to the original database.
146849** And a power loss in between deleting the original and renaming the
146850** transient would cause the database file to appear to be deleted
146851** following reboot.
146852*/
146853SQLITE_PRIVATEstatic void sqlite3Vacuum(Parse *pParse, Token *pNm, Expr *pInto){
146854 Vdbe *v = sqlite3GetVdbe(pParse);
146855 int iDb = 0;
146856 if( v==0 ) goto build_vacuum_end;
146857 if( pParse->nErr ) goto build_vacuum_end;
146858 if( pNm ){
146859#ifndef SQLITE_BUG_COMPATIBLE_20160819
146860 /* Default behavior: Report an error if the argument to VACUUM is
146861 ** not recognized */
146862 iDb = sqlite3TwoPartName(pParse, pNm, pNm, &pNm);
146863 if( iDb<0 ) goto build_vacuum_end;
146864#else
146865 /* When SQLITE_BUG_COMPATIBLE_20160819 is defined, unrecognized arguments
146866 ** to VACUUM are silently ignored. This is a back-out of a bug fix that
146867 ** occurred on 2016-08-19 (https://www.sqlite.org/src/info/083f9e6270).
146868 ** The buggy behavior is required for binary compatibility with some
146869 ** legacy applications. */
146870 iDb = sqlite3FindDb(pParse->db, pNm);
146871 if( iDb<0 ) iDb = 0;
146872#endif
146873 }
146874 if( iDb!=1 ){
146875 int iIntoReg = 0;
146876 if( pInto && sqlite3ResolveSelfReference(pParse,0,0,pInto,0)==0 ){
146877 iIntoReg = ++pParse->nMem;
146878 sqlite3ExprCode(pParse, pInto, iIntoReg);
146879 }
146880 sqlite3VdbeAddOp2(v, OP_Vacuum5, iDb, iIntoReg);
146881 sqlite3VdbeUsesBtree(v, iDb);
146882 }
146883build_vacuum_end:
146884 sqlite3ExprDelete(pParse->db, pInto);
146885 return;
146886}
146887
146888/*
146889** This routine implements the OP_Vacuum opcode of the VDBE.
146890*/
146891SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) int sqlite3RunVacuum(
146892 char **pzErrMsg, /* Write error message here */
146893 sqlite3 *db, /* Database connection */
146894 int iDb, /* Which attached DB to vacuum */
146895 sqlite3_value *pOut /* Write results here, if not NULL. VACUUM INTO */
146896){
146897 int rc = SQLITE_OK0; /* Return code from service routines */
146898 Btree *pMain; /* The database being vacuumed */
146899 Btree *pTemp; /* The temporary database we vacuum into */
146900 u32 saved_mDbFlags; /* Saved value of db->mDbFlags */
146901 u64 saved_flags; /* Saved value of db->flags */
146902 i64 saved_nChange; /* Saved value of db->nChange */
146903 i64 saved_nTotalChange; /* Saved value of db->nTotalChange */
146904 u32 saved_openFlags; /* Saved value of db->openFlags */
146905 u8 saved_mTrace; /* Saved trace settings */
146906 Db *pDb = 0; /* Database to detach at end of vacuum */
146907 int isMemDb; /* True if vacuuming a :memory: database */
146908 int nRes; /* Bytes of reserved space at the end of each page */
146909 int nDb; /* Number of attached databases */
146910 const char *zDbMain; /* Schema name of database to vacuum */
146911 const char *zOut; /* Name of output file */
146912
146913 if( !db->autoCommit ){
146914 sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
146915 return SQLITE_ERROR1; /* IMP: R-12218-18073 */
146916 }
146917 if( db->nVdbeActive>1 ){
146918 sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
146919 return SQLITE_ERROR1; /* IMP: R-15610-35227 */
146920 }
146921 saved_openFlags = db->openFlags;
146922 if( pOut ){
146923 if( sqlite3_value_type(pOut)!=SQLITE_TEXT3 ){
146924 sqlite3SetString(pzErrMsg, db, "non-text filename");
146925 return SQLITE_ERROR1;
146926 }
146927 zOut = (const char*)sqlite3_value_text(pOut);
146928 db->openFlags &= ~SQLITE_OPEN_READONLY0x00000001;
146929 db->openFlags |= SQLITE_OPEN_CREATE0x00000004|SQLITE_OPEN_READWRITE0x00000002;
146930 }else{
146931 zOut = "";
146932 }
146933
146934 /* Save the current value of the database flags so that it can be
146935 ** restored before returning. Then set the writable-schema flag, and
146936 ** disable CHECK and foreign key constraints. */
146937 saved_flags = db->flags;
146938 saved_mDbFlags = db->mDbFlags;
146939 saved_nChange = db->nChange;
146940 saved_nTotalChange = db->nTotalChange;
146941 saved_mTrace = db->mTrace;
146942 db->flags |= SQLITE_WriteSchema0x00000001 | SQLITE_IgnoreChecks0x00000200;
146943 db->mDbFlags |= DBFLAG_PreferBuiltin0x0002 | DBFLAG_Vacuum0x0004;
146944 db->flags &= ~(u64)(SQLITE_ForeignKeys0x00004000 | SQLITE_ReverseOrder0x00001000
146945 | SQLITE_Defensive0x10000000 | SQLITE_CountRows((u64)(0x00001)<<32));
146946 db->mTrace = 0;
146947
146948 zDbMain = db->aDb[iDb].zDbSName;
146949 pMain = db->aDb[iDb].pBt;
146950 isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
146951
146952 /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
146953 ** can be set to 'off' for this file, as it is not recovered if a crash
146954 ** occurs anyway. The integrity of the database is maintained by a
146955 ** (possibly synchronous) transaction opened on the main database before
146956 ** sqlite3BtreeCopyFile() is called.
146957 **
146958 ** An optimisation would be to use a non-journaled pager.
146959 ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but
146960 ** that actually made the VACUUM run slower. Very little journalling
146961 ** actually occurs when doing a vacuum since the vacuum_db is initially
146962 ** empty. Only the journal header is written. Apparently it takes more
146963 ** time to parse and run the PRAGMA to turn journalling off than it does
146964 ** to write the journal header file.
146965 */
146966 nDb = db->nDb;
146967 rc = execSqlF(db, pzErrMsg, "ATTACH %Q AS vacuum_db", zOut);
146968 db->openFlags = saved_openFlags;
146969 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
146970 assert( (db->nDb-1)==nDb )((void) (0));
146971 pDb = &db->aDb[nDb];
146972 assert( strcmp(pDb->zDbSName,"vacuum_db")==0 )((void) (0));
146973 pTemp = pDb->pBt;
146974 if( pOut ){
146975 sqlite3_file *id = sqlite3PagerFile(sqlite3BtreePager(pTemp));
146976 i64 sz = 0;
146977 if( id->pMethods!=0 && (sqlite3OsFileSize(id, &sz)!=SQLITE_OK0 || sz>0) ){
146978 rc = SQLITE_ERROR1;
146979 sqlite3SetString(pzErrMsg, db, "output file already exists");
146980 goto end_of_vacuum;
146981 }
146982 db->mDbFlags |= DBFLAG_VacuumInto0x0008;
146983 }
146984 nRes = sqlite3BtreeGetRequestedReserve(pMain);
146985
146986 sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
146987 sqlite3BtreeSetSpillSize(pTemp, sqlite3BtreeSetSpillSize(pMain,0));
146988 sqlite3BtreeSetPagerFlags(pTemp, PAGER_SYNCHRONOUS_OFF0x01|PAGER_CACHESPILL0x20);
146989
146990 /* Begin a transaction and take an exclusive lock on the main database
146991 ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
146992 ** to ensure that we do not try to change the page-size on a WAL database.
146993 */
146994 rc = execSql(db, pzErrMsg, "BEGIN");
146995 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
146996 rc = sqlite3BtreeBeginTrans(pMain, pOut==0 ? 2 : 0, 0);
146997 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
146998
146999 /* Do not attempt to change the page size for a WAL database */
147000 if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
147001 ==PAGER_JOURNALMODE_WAL5
147002 && pOut==0
147003 ){
147004 db->nextPagesize = 0;
147005 }
147006
147007 if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
147008 || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
147009 || NEVER(db->mallocFailed)(db->mallocFailed)
147010 ){
147011 rc = SQLITE_NOMEM_BKPT7;
147012 goto end_of_vacuum;
147013 }
147014
147015#ifndef SQLITE_OMIT_AUTOVACUUM
147016 sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
147017 sqlite3BtreeGetAutoVacuum(pMain));
147018#endif
147019
147020 /* Query the schema of the main database. Create a mirror schema
147021 ** in the temporary database.
147022 */
147023 db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
147024 rc = execSqlF(db, pzErrMsg,
147025 "SELECT sql FROM \"%w\".sqlite_schema"
147026 " WHERE type='table'AND name<>'sqlite_sequence'"
147027 " AND coalesce(rootpage,1)>0",
147028 zDbMain
147029 );
147030 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
147031 rc = execSqlF(db, pzErrMsg,
147032 "SELECT sql FROM \"%w\".sqlite_schema"
147033 " WHERE type='index'",
147034 zDbMain
147035 );
147036 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
147037 db->init.iDb = 0;
147038
147039 /* Loop through the tables in the main database. For each, do
147040 ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
147041 ** the contents to the temporary database.
147042 */
147043 rc = execSqlF(db, pzErrMsg,
147044 "SELECT'INSERT INTO vacuum_db.'||quote(name)"
147045 "||' SELECT*FROM\"%w\".'||quote(name)"
147046 "FROM vacuum_db.sqlite_schema "
147047 "WHERE type='table'AND coalesce(rootpage,1)>0",
147048 zDbMain
147049 );
147050 assert( (db->mDbFlags & DBFLAG_Vacuum)!=0 )((void) (0));
147051 db->mDbFlags &= ~DBFLAG_Vacuum0x0004;
147052 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
147053
147054 /* Copy the triggers, views, and virtual tables from the main database
147055 ** over to the temporary database. None of these objects has any
147056 ** associated storage, so all we have to do is copy their entries
147057 ** from the schema table.
147058 */
147059 rc = execSqlF(db, pzErrMsg,
147060 "INSERT INTO vacuum_db.sqlite_schema"
147061 " SELECT*FROM \"%w\".sqlite_schema"
147062 " WHERE type IN('view','trigger')"
147063 " OR(type='table'AND rootpage=0)",
147064 zDbMain
147065 );
147066 if( rc ) goto end_of_vacuum;
147067
147068 /* At this point, there is a write transaction open on both the
147069 ** vacuum database and the main database. Assuming no error occurs,
147070 ** both transactions are closed by this block - the main database
147071 ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
147072 ** call to sqlite3BtreeCommit().
147073 */
147074 {
147075 u32 meta;
147076 int i;
147077
147078 /* This array determines which meta meta values are preserved in the
147079 ** vacuum. Even entries are the meta value number and odd entries
147080 ** are an increment to apply to the meta value after the vacuum.
147081 ** The increment is used to increase the schema cookie so that other
147082 ** connections to the same database will know to reread the schema.
147083 */
147084 static const unsigned char aCopy[] = {
147085 BTREE_SCHEMA_VERSION1, 1, /* Add one to the old schema cookie */
147086 BTREE_DEFAULT_CACHE_SIZE3, 0, /* Preserve the default page cache size */
147087 BTREE_TEXT_ENCODING5, 0, /* Preserve the text encoding */
147088 BTREE_USER_VERSION6, 0, /* Preserve the user version */
147089 BTREE_APPLICATION_ID8, 0, /* Preserve the application id */
147090 };
147091
147092 assert( SQLITE_TXN_WRITE==sqlite3BtreeTxnState(pTemp) )((void) (0));
147093 assert( pOut!=0 || SQLITE_TXN_WRITE==sqlite3BtreeTxnState(pMain) )((void) (0));
147094
147095 /* Copy Btree meta values */
147096 for(i=0; i<ArraySize(aCopy)((int)(sizeof(aCopy)/sizeof(aCopy[0]))); i+=2){
147097 /* GetMeta() and UpdateMeta() cannot fail in this context because
147098 ** we already have page 1 loaded into cache and marked dirty. */
147099 sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
147100 rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
147101 if( NEVER(rc!=SQLITE_OK)(rc!=0) ) goto end_of_vacuum;
147102 }
147103
147104 if( pOut==0 ){
147105 rc = sqlite3BtreeCopyFile(pMain, pTemp);
147106 }
147107 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
147108 rc = sqlite3BtreeCommit(pTemp);
147109 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
147110#ifndef SQLITE_OMIT_AUTOVACUUM
147111 if( pOut==0 ){
147112 sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
147113 }
147114#endif
147115 }
147116
147117 assert( rc==SQLITE_OK )((void) (0));
147118 if( pOut==0 ){
147119 nRes = sqlite3BtreeGetRequestedReserve(pTemp);
147120 rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
147121 }
147122
147123end_of_vacuum:
147124 /* Restore the original value of db->flags */
147125 db->init.iDb = 0;
147126 db->mDbFlags = saved_mDbFlags;
147127 db->flags = saved_flags;
147128 db->nChange = saved_nChange;
147129 db->nTotalChange = saved_nTotalChange;
147130 db->mTrace = saved_mTrace;
147131 sqlite3BtreeSetPageSize(pMain, -1, 0, 1);
147132
147133 /* Currently there is an SQL level transaction open on the vacuum
147134 ** database. No locks are held on any other files (since the main file
147135 ** was committed at the btree level). So it safe to end the transaction
147136 ** by manually setting the autoCommit flag to true and detaching the
147137 ** vacuum database. The vacuum_db journal file is deleted when the pager
147138 ** is closed by the DETACH.
147139 */
147140 db->autoCommit = 1;
147141
147142 if( pDb ){
147143 sqlite3BtreeClose(pDb->pBt);
147144 pDb->pBt = 0;
147145 pDb->pSchema = 0;
147146 }
147147
147148 /* This both clears the schemas and reduces the size of the db->aDb[]
147149 ** array. */
147150 sqlite3ResetAllSchemasOfConnection(db);
147151
147152 return rc;
147153}
147154
147155#endif /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */
147156
147157/************** End of vacuum.c **********************************************/
147158/************** Begin file vtab.c ********************************************/
147159/*
147160** 2006 June 10
147161**
147162** The author disclaims copyright to this source code. In place of
147163** a legal notice, here is a blessing:
147164**
147165** May you do good and not evil.
147166** May you find forgiveness for yourself and forgive others.
147167** May you share freely, never taking more than you give.
147168**
147169*************************************************************************
147170** This file contains code used to help implement virtual tables.
147171*/
147172#ifndef SQLITE_OMIT_VIRTUALTABLE
147173/* #include "sqliteInt.h" */
147174
147175/*
147176** Before a virtual table xCreate() or xConnect() method is invoked, the
147177** sqlite3.pVtabCtx member variable is set to point to an instance of
147178** this struct allocated on the stack. It is used by the implementation of
147179** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
147180** are invoked only from within xCreate and xConnect methods.
147181*/
147182struct VtabCtx {
147183 VTable *pVTable; /* The virtual table being constructed */
147184 Table *pTab; /* The Table object to which the virtual table belongs */
147185 VtabCtx *pPrior; /* Parent context (if any) */
147186 int bDeclared; /* True after sqlite3_declare_vtab() is called */
147187};
147188
147189/*
147190** Construct and install a Module object for a virtual table. When this
147191** routine is called, it is guaranteed that all appropriate locks are held
147192** and the module is not already part of the connection.
147193**
147194** If there already exists a module with zName, replace it with the new one.
147195** If pModule==0, then delete the module zName if it exists.
147196*/
147197SQLITE_PRIVATEstatic Module *sqlite3VtabCreateModule(
147198 sqlite3 *db, /* Database in which module is registered */
147199 const char *zName, /* Name assigned to this module */
147200 const sqlite3_module *pModule, /* The definition of the module */
147201 void *pAux, /* Context pointer for xCreate/xConnect */
147202 void (*xDestroy)(void *) /* Module destructor function */
147203){
147204 Module *pMod;
147205 Module *pDel;
147206 char *zCopy;
147207 if( pModule==0 ){
147208 zCopy = (char*)zName;
147209 pMod = 0;
147210 }else{
147211 int nName = sqlite3Strlen30(zName);
147212 pMod = (Module *)sqlite3Malloc(sizeof(Module) + nName + 1);
147213 if( pMod==0 ){
147214 sqlite3OomFault(db);
147215 return 0;
147216 }
147217 zCopy = (char *)(&pMod[1]);
147218 memcpy(zCopy, zName, nName+1);
147219 pMod->zName = zCopy;
147220 pMod->pModule = pModule;
147221 pMod->pAux = pAux;
147222 pMod->xDestroy = xDestroy;
147223 pMod->pEpoTab = 0;
147224 pMod->nRefModule = 1;
147225 }
147226 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
147227 if( pDel ){
147228 if( pDel==pMod ){
147229 sqlite3OomFault(db);
147230 sqlite3DbFree(db, pDel);
147231 pMod = 0;
147232 }else{
147233 sqlite3VtabEponymousTableClear(db, pDel);
147234 sqlite3VtabModuleUnref(db, pDel);
147235 }
147236 }
147237 return pMod;
147238}
147239
147240/*
147241** The actual function that does the work of creating a new module.
147242** This function implements the sqlite3_create_module() and
147243** sqlite3_create_module_v2() interfaces.
147244*/
147245static int createModule(
147246 sqlite3 *db, /* Database in which module is registered */
147247 const char *zName, /* Name assigned to this module */
147248 const sqlite3_module *pModule, /* The definition of the module */
147249 void *pAux, /* Context pointer for xCreate/xConnect */
147250 void (*xDestroy)(void *) /* Module destructor function */
147251){
147252 int rc = SQLITE_OK0;
147253
147254 sqlite3_mutex_enter(db->mutex);
147255 (void)sqlite3VtabCreateModule(db, zName, pModule, pAux, xDestroy);
147256 rc = sqlite3ApiExit(db, rc);
147257 if( rc!=SQLITE_OK0 && xDestroy ) xDestroy(pAux);
147258 sqlite3_mutex_leave(db->mutex);
147259 return rc;
147260}
147261
147262
147263/*
147264** External API function used to create a new virtual-table module.
147265*/
147266SQLITE_API int sqlite3_create_module(
147267 sqlite3 *db, /* Database in which module is registered */
147268 const char *zName, /* Name assigned to this module */
147269 const sqlite3_module *pModule, /* The definition of the module */
147270 void *pAux /* Context pointer for xCreate/xConnect */
147271){
147272#ifdef SQLITE_ENABLE_API_ARMOR1
147273 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(147273);
147274#endif
147275 return createModule(db, zName, pModule, pAux, 0);
147276}
147277
147278/*
147279** External API function used to create a new virtual-table module.
147280*/
147281SQLITE_API int sqlite3_create_module_v2(
147282 sqlite3 *db, /* Database in which module is registered */
147283 const char *zName, /* Name assigned to this module */
147284 const sqlite3_module *pModule, /* The definition of the module */
147285 void *pAux, /* Context pointer for xCreate/xConnect */
147286 void (*xDestroy)(void *) /* Module destructor function */
147287){
147288#ifdef SQLITE_ENABLE_API_ARMOR1
147289 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(147289);
147290#endif
147291 return createModule(db, zName, pModule, pAux, xDestroy);
147292}
147293
147294/*
147295** External API to drop all virtual-table modules, except those named
147296** on the azNames list.
147297*/
147298SQLITE_API int sqlite3_drop_modules(sqlite3 *db, const char** azNames){
147299 HashElem *pThis, *pNext;
147300#ifdef SQLITE_ENABLE_API_ARMOR1
147301 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(147301);
147302#endif
147303 for(pThis=sqliteHashFirst(&db->aModule)((&db->aModule)->first); pThis; pThis=pNext){
147304 Module *pMod = (Module*)sqliteHashData(pThis)((pThis)->data);
147305 pNext = sqliteHashNext(pThis)((pThis)->next);
147306 if( azNames ){
147307 int ii;
147308 for(ii=0; azNames[ii]!=0 && strcmp(azNames[ii],pMod->zName)!=0; ii++){}
147309 if( azNames[ii]!=0 ) continue;
147310 }
147311 createModule(db, pMod->zName, 0, 0, 0);
147312 }
147313 return SQLITE_OK0;
147314}
147315
147316/*
147317** Decrement the reference count on a Module object. Destroy the
147318** module when the reference count reaches zero.
147319*/
147320SQLITE_PRIVATEstatic void sqlite3VtabModuleUnref(sqlite3 *db, Module *pMod){
147321 assert( pMod->nRefModule>0 )((void) (0));
147322 pMod->nRefModule--;
147323 if( pMod->nRefModule==0 ){
147324 if( pMod->xDestroy ){
147325 pMod->xDestroy(pMod->pAux);
147326 }
147327 assert( pMod->pEpoTab==0 )((void) (0));
147328 sqlite3DbFree(db, pMod);
147329 }
147330}
147331
147332/*
147333** Lock the virtual table so that it cannot be disconnected.
147334** Locks nest. Every lock should have a corresponding unlock.
147335** If an unlock is omitted, resources leaks will occur.
147336**
147337** If a disconnect is attempted while a virtual table is locked,
147338** the disconnect is deferred until all locks have been removed.
147339*/
147340SQLITE_PRIVATEstatic void sqlite3VtabLock(VTable *pVTab){
147341 pVTab->nRef++;
147342}
147343
147344
147345/*
147346** pTab is a pointer to a Table structure representing a virtual-table.
147347** Return a pointer to the VTable object used by connection db to access
147348** this virtual-table, if one has been created, or NULL otherwise.
147349*/
147350SQLITE_PRIVATEstatic VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
147351 VTable *pVtab;
147352 assert( IsVirtual(pTab) )((void) (0));
147353 for(pVtab=pTab->u.vtab.p; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
147354 return pVtab;
147355}
147356
147357/*
147358** Decrement the ref-count on a virtual table object. When the ref-count
147359** reaches zero, call the xDisconnect() method to delete the object.
147360*/
147361SQLITE_PRIVATEstatic void sqlite3VtabUnlock(VTable *pVTab){
147362 sqlite3 *db = pVTab->db;
147363
147364 assert( db )((void) (0));
147365 assert( pVTab->nRef>0 )((void) (0));
147366 assert( db->eOpenState==SQLITE_STATE_OPEN((void) (0))
147367 || db->eOpenState==SQLITE_STATE_ZOMBIE )((void) (0));
147368
147369 pVTab->nRef--;
147370 if( pVTab->nRef==0 ){
147371 sqlite3_vtab *p = pVTab->pVtab;
147372 sqlite3VtabModuleUnref(pVTab->db, pVTab->pMod);
147373 if( p ){
147374 p->pModule->xDisconnect(p);
147375 }
147376 sqlite3DbFree(db, pVTab);
147377 }
147378}
147379
147380/*
147381** Table p is a virtual table. This function moves all elements in the
147382** p->u.vtab.p list to the sqlite3.pDisconnect lists of their associated
147383** database connections to be disconnected at the next opportunity.
147384** Except, if argument db is not NULL, then the entry associated with
147385** connection db is left in the p->u.vtab.p list.
147386*/
147387static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
147388 VTable *pRet = 0;
147389 VTable *pVTable;
147390
147391 assert( IsVirtual(p) )((void) (0));
147392 pVTable = p->u.vtab.p;
147393 p->u.vtab.p = 0;
147394
147395 /* Assert that the mutex (if any) associated with the BtShared database
147396 ** that contains table p is held by the caller. See header comments
147397 ** above function sqlite3VtabUnlockList() for an explanation of why
147398 ** this makes it safe to access the sqlite3.pDisconnect list of any
147399 ** database connection that may have an entry in the p->u.vtab.p list.
147400 */
147401 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) )((void) (0));
147402
147403 while( pVTable ){
147404 sqlite3 *db2 = pVTable->db;
147405 VTable *pNext = pVTable->pNext;
147406 assert( db2 )((void) (0));
147407 if( db2==db ){
147408 pRet = pVTable;
147409 p->u.vtab.p = pRet;
147410 pRet->pNext = 0;
147411 }else{
147412 pVTable->pNext = db2->pDisconnect;
147413 db2->pDisconnect = pVTable;
147414 }
147415 pVTable = pNext;
147416 }
147417
147418 assert( !db || pRet )((void) (0));
147419 return pRet;
147420}
147421
147422/*
147423** Table *p is a virtual table. This function removes the VTable object
147424** for table *p associated with database connection db from the linked
147425** list in p->pVTab. It also decrements the VTable ref count. This is
147426** used when closing database connection db to free all of its VTable
147427** objects without disturbing the rest of the Schema object (which may
147428** be being used by other shared-cache connections).
147429*/
147430SQLITE_PRIVATEstatic void sqlite3VtabDisconnect(sqlite3 *db, Table *p){
147431 VTable **ppVTab;
147432
147433 assert( IsVirtual(p) )((void) (0));
147434 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
147435 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
147436
147437 for(ppVTab=&p->u.vtab.p; *ppVTab; ppVTab=&(*ppVTab)->pNext){
147438 if( (*ppVTab)->db==db ){
147439 VTable *pVTab = *ppVTab;
147440 *ppVTab = pVTab->pNext;
147441 sqlite3VtabUnlock(pVTab);
147442 break;
147443 }
147444 }
147445}
147446
147447
147448/*
147449** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
147450**
147451** This function may only be called when the mutexes associated with all
147452** shared b-tree databases opened using connection db are held by the
147453** caller. This is done to protect the sqlite3.pDisconnect list. The
147454** sqlite3.pDisconnect list is accessed only as follows:
147455**
147456** 1) By this function. In this case, all BtShared mutexes and the mutex
147457** associated with the database handle itself must be held.
147458**
147459** 2) By function vtabDisconnectAll(), when it adds a VTable entry to
147460** the sqlite3.pDisconnect list. In this case either the BtShared mutex
147461** associated with the database the virtual table is stored in is held
147462** or, if the virtual table is stored in a non-sharable database, then
147463** the database handle mutex is held.
147464**
147465** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously
147466** by multiple threads. It is thread-safe.
147467*/
147468SQLITE_PRIVATEstatic void sqlite3VtabUnlockList(sqlite3 *db){
147469 VTable *p = db->pDisconnect;
147470
147471 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
147472 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
147473
147474 if( p ){
147475 db->pDisconnect = 0;
147476 sqlite3ExpirePreparedStatements(db, 0);
147477 do {
147478 VTable *pNext = p->pNext;
147479 sqlite3VtabUnlock(p);
147480 p = pNext;
147481 }while( p );
147482 }
147483}
147484
147485/*
147486** Clear any and all virtual-table information from the Table record.
147487** This routine is called, for example, just before deleting the Table
147488** record.
147489**
147490** Since it is a virtual-table, the Table structure contains a pointer
147491** to the head of a linked list of VTable structures. Each VTable
147492** structure is associated with a single sqlite3* user of the schema.
147493** The reference count of the VTable structure associated with database
147494** connection db is decremented immediately (which may lead to the
147495** structure being xDisconnected and free). Any other VTable structures
147496** in the list are moved to the sqlite3.pDisconnect list of the associated
147497** database connection.
147498*/
147499SQLITE_PRIVATEstatic void sqlite3VtabClear(sqlite3 *db, Table *p){
147500 assert( IsVirtual(p) )((void) (0));
147501 if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
147502 if( p->u.vtab.azArg ){
147503 int i;
147504 for(i=0; i<p->u.vtab.nArg; i++){
147505 if( i!=1 ) sqlite3DbFree(db, p->u.vtab.azArg[i]);
147506 }
147507 sqlite3DbFree(db, p->u.vtab.azArg);
147508 }
147509}
147510
147511/*
147512** Add a new module argument to pTable->u.vtab.azArg[].
147513** The string is not copied - the pointer is stored. The
147514** string will be freed automatically when the table is
147515** deleted.
147516*/
147517static void addModuleArgument(Parse *pParse, Table *pTable, char *zArg){
147518 sqlite3_int64 nBytes;
147519 char **azModuleArg;
147520 sqlite3 *db = pParse->db;
147521
147522 assert( IsVirtual(pTable) )((void) (0));
147523 nBytes = sizeof(char *)*(2+pTable->u.vtab.nArg);
147524 if( pTable->u.vtab.nArg+3>=db->aLimit[SQLITE_LIMIT_COLUMN2] ){
147525 sqlite3ErrorMsg(pParse, "too many columns on %s", pTable->zName);
147526 }
147527 azModuleArg = sqlite3DbRealloc(db, pTable->u.vtab.azArg, nBytes);
147528 if( azModuleArg==0 ){
147529 sqlite3DbFree(db, zArg);
147530 }else{
147531 int i = pTable->u.vtab.nArg++;
147532 azModuleArg[i] = zArg;
147533 azModuleArg[i+1] = 0;
147534 pTable->u.vtab.azArg = azModuleArg;
147535 }
147536}
147537
147538/*
147539** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
147540** statement. The module name has been parsed, but the optional list
147541** of parameters that follow the module name are still pending.
147542*/
147543SQLITE_PRIVATEstatic void sqlite3VtabBeginParse(
147544 Parse *pParse, /* Parsing context */
147545 Token *pName1, /* Name of new table, or database name */
147546 Token *pName2, /* Name of new table or NULL */
147547 Token *pModuleName, /* Name of the module for the virtual table */
147548 int ifNotExists /* No error if the table already exists */
147549){
147550 Table *pTable; /* The new virtual table */
147551 sqlite3 *db; /* Database connection */
147552
147553 sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists);
147554 pTable = pParse->pNewTable;
147555 if( pTable==0 ) return;
147556 assert( 0==pTable->pIndex )((void) (0));
147557 pTable->eTabType = TABTYP_VTAB1;
147558
147559 db = pParse->db;
147560
147561 assert( pTable->u.vtab.nArg==0 )((void) (0));
147562 addModuleArgument(pParse, pTable, sqlite3NameFromToken(db, pModuleName));
147563 addModuleArgument(pParse, pTable, 0);
147564 addModuleArgument(pParse, pTable, sqlite3DbStrDup(db, pTable->zName));
147565 assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)((void) (0))
147566 || (pParse->sNameToken.z==pName1->z && pName2->z==0)((void) (0))
147567 )((void) (0));
147568 pParse->sNameToken.n = (int)(
147569 &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
147570 );
147571
147572#ifndef SQLITE_OMIT_AUTHORIZATION
147573 /* Creating a virtual table invokes the authorization callback twice.
147574 ** The first invocation, to obtain permission to INSERT a row into the
147575 ** sqlite_schema table, has already been made by sqlite3StartTable().
147576 ** The second call, to obtain permission to create the table, is made now.
147577 */
147578 if( pTable->u.vtab.azArg ){
147579 int iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
147580 assert( iDb>=0 )((void) (0)); /* The database the table is being created in */
147581 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE29, pTable->zName,
147582 pTable->u.vtab.azArg[0], pParse->db->aDb[iDb].zDbSName);
147583 }
147584#endif
147585}
147586
147587/*
147588** This routine takes the module argument that has been accumulating
147589** in pParse->zArg[] and appends it to the list of arguments on the
147590** virtual table currently under construction in pParse->pTable.
147591*/
147592static void addArgumentToVtab(Parse *pParse){
147593 if( pParse->sArg.z && pParse->pNewTable ){
147594 const char *z = (const char*)pParse->sArg.z;
147595 int n = pParse->sArg.n;
147596 sqlite3 *db = pParse->db;
147597 addModuleArgument(pParse, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
147598 }
147599}
147600
147601/*
147602** The parser calls this routine after the CREATE VIRTUAL TABLE statement
147603** has been completely parsed.
147604*/
147605SQLITE_PRIVATEstatic void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
147606 Table *pTab = pParse->pNewTable; /* The table being constructed */
147607 sqlite3 *db = pParse->db; /* The database connection */
147608
147609 if( pTab==0 ) return;
147610 assert( IsVirtual(pTab) )((void) (0));
147611 addArgumentToVtab(pParse);
147612 pParse->sArg.z = 0;
147613 if( pTab->u.vtab.nArg<1 ) return;
147614
147615 /* If the CREATE VIRTUAL TABLE statement is being entered for the
147616 ** first time (in other words if the virtual table is actually being
147617 ** created now instead of just being read out of sqlite_schema) then
147618 ** do additional initialization work and store the statement text
147619 ** in the sqlite_schema table.
147620 */
147621 if( !db->init.busy ){
147622 char *zStmt;
147623 char *zWhere;
147624 int iDb;
147625 int iReg;
147626 Vdbe *v;
147627
147628 sqlite3MayAbort(pParse);
147629
147630 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
147631 if( pEnd ){
147632 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
147633 }
147634 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
147635
147636 /* A slot for the record has already been allocated in the
147637 ** schema table. We just need to update that slot with all
147638 ** the information we've collected.
147639 **
147640 ** The VM register number pParse->regRowid holds the rowid of an
147641 ** entry in the sqlite_schema table tht was created for this vtab
147642 ** by sqlite3StartTable().
147643 */
147644 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
147645 sqlite3NestedParse(pParse,
147646 "UPDATE %Q." LEGACY_SCHEMA_TABLE"sqlite_master" " "
147647 "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
147648 "WHERE rowid=#%d",
147649 db->aDb[iDb].zDbSName,
147650 pTab->zName,
147651 pTab->zName,
147652 zStmt,
147653 pParse->regRowid
147654 );
147655 v = sqlite3GetVdbe(pParse);
147656 sqlite3ChangeCookie(pParse, iDb);
147657
147658 sqlite3VdbeAddOp0(v, OP_Expire166);
147659 zWhere = sqlite3MPrintf(db, "name=%Q AND sql=%Q", pTab->zName, zStmt);
147660 sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere, 0);
147661 sqlite3DbFree(db, zStmt);
147662
147663 iReg = ++pParse->nMem;
147664 sqlite3VdbeLoadString(v, iReg, pTab->zName);
147665 sqlite3VdbeAddOp2(v, OP_VCreate171, iDb, iReg);
147666 }else{
147667 /* If we are rereading the sqlite_schema table create the in-memory
147668 ** record of the table. */
147669 Table *pOld;
147670 Schema *pSchema = pTab->pSchema;
147671 const char *zName = pTab->zName;
147672 assert( zName!=0 )((void) (0));
147673 sqlite3MarkAllShadowTablesOf(db, pTab);
147674 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
147675 if( pOld ){
147676 sqlite3OomFault(db);
147677 assert( pTab==pOld )((void) (0)); /* Malloc must have failed inside HashInsert() */
147678 return;
147679 }
147680 pParse->pNewTable = 0;
147681 }
147682}
147683
147684/*
147685** The parser calls this routine when it sees the first token
147686** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
147687*/
147688SQLITE_PRIVATEstatic void sqlite3VtabArgInit(Parse *pParse){
147689 addArgumentToVtab(pParse);
147690 pParse->sArg.z = 0;
147691 pParse->sArg.n = 0;
147692}
147693
147694/*
147695** The parser calls this routine for each token after the first token
147696** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
147697*/
147698SQLITE_PRIVATEstatic void sqlite3VtabArgExtend(Parse *pParse, Token *p){
147699 Token *pArg = &pParse->sArg;
147700 if( pArg->z==0 ){
147701 pArg->z = p->z;
147702 pArg->n = p->n;
147703 }else{
147704 assert(pArg->z <= p->z)((void) (0));
147705 pArg->n = (int)(&p->z[p->n] - pArg->z);
147706 }
147707}
147708
147709/*
147710** Invoke a virtual table constructor (either xCreate or xConnect). The
147711** pointer to the function to invoke is passed as the fourth parameter
147712** to this procedure.
147713*/
147714static int vtabCallConstructor(
147715 sqlite3 *db,
147716 Table *pTab,
147717 Module *pMod,
147718 int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
147719 char **pzErr
147720){
147721 VtabCtx sCtx;
147722 VTable *pVTable;
147723 int rc;
147724 const char *const*azArg;
147725 int nArg = pTab->u.vtab.nArg;
147726 char *zErr = 0;
147727 char *zModuleName;
147728 int iDb;
147729 VtabCtx *pCtx;
147730
147731 assert( IsVirtual(pTab) )((void) (0));
147732 azArg = (const char *const*)pTab->u.vtab.azArg;
147733
147734 /* Check that the virtual-table is not already being initialized */
147735 for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
147736 if( pCtx->pTab==pTab ){
147737 *pzErr = sqlite3MPrintf(db,
147738 "vtable constructor called recursively: %s", pTab->zName
147739 );
147740 return SQLITE_LOCKED6;
147741 }
147742 }
147743
147744 zModuleName = sqlite3DbStrDup(db, pTab->zName);
147745 if( !zModuleName ){
147746 return SQLITE_NOMEM_BKPT7;
147747 }
147748
147749 pVTable = sqlite3MallocZero(sizeof(VTable));
147750 if( !pVTable ){
147751 sqlite3OomFault(db);
147752 sqlite3DbFree(db, zModuleName);
147753 return SQLITE_NOMEM_BKPT7;
147754 }
147755 pVTable->db = db;
147756 pVTable->pMod = pMod;
147757 pVTable->eVtabRisk = SQLITE_VTABRISK_Normal1;
147758
147759 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
147760 pTab->u.vtab.azArg[1] = db->aDb[iDb].zDbSName;
147761
147762 /* Invoke the virtual table constructor */
147763 assert( &db->pVtabCtx )((void) (0));
147764 assert( xConstruct )((void) (0));
147765 sCtx.pTab = pTab;
147766 sCtx.pVTable = pVTable;
147767 sCtx.pPrior = db->pVtabCtx;
147768 sCtx.bDeclared = 0;
147769 db->pVtabCtx = &sCtx;
147770 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
147771 db->pVtabCtx = sCtx.pPrior;
147772 if( rc==SQLITE_NOMEM7 ) sqlite3OomFault(db);
147773 assert( sCtx.pTab==pTab )((void) (0));
147774
147775 if( SQLITE_OK0!=rc ){
147776 if( zErr==0 ){
147777 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
147778 }else {
147779 *pzErr = sqlite3MPrintf(db, "%s", zErr);
147780 sqlite3_free(zErr);
147781 }
147782 sqlite3DbFree(db, pVTable);
147783 }else if( ALWAYS(pVTable->pVtab)(pVTable->pVtab) ){
147784 /* Justification of ALWAYS(): A correct vtab constructor must allocate
147785 ** the sqlite3_vtab object if successful. */
147786 memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0]));
147787 pVTable->pVtab->pModule = pMod->pModule;
147788 pMod->nRefModule++;
147789 pVTable->nRef = 1;
147790 if( sCtx.bDeclared==0 ){
147791 const char *zFormat = "vtable constructor did not declare schema: %s";
147792 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
147793 sqlite3VtabUnlock(pVTable);
147794 rc = SQLITE_ERROR1;
147795 }else{
147796 int iCol;
147797 u16 oooHidden = 0;
147798 /* If everything went according to plan, link the new VTable structure
147799 ** into the linked list headed by pTab->u.vtab.p. Then loop through the
147800 ** columns of the table to see if any of them contain the token "hidden".
147801 ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from
147802 ** the type string. */
147803 pVTable->pNext = pTab->u.vtab.p;
147804 pTab->u.vtab.p = pVTable;
147805
147806 for(iCol=0; iCol<pTab->nCol; iCol++){
147807 char *zType = sqlite3ColumnType(&pTab->aCol[iCol], "");
147808 int nType;
147809 int i = 0;
147810 nType = sqlite3Strlen30(zType);
147811 for(i=0; i<nType; i++){
147812 if( 0==sqlite3StrNICmpsqlite3_strnicmp("hidden", &zType[i], 6)
147813 && (i==0 || zType[i-1]==' ')
147814 && (zType[i+6]=='\0' || zType[i+6]==' ')
147815 ){
147816 break;
147817 }
147818 }
147819 if( i<nType ){
147820 int j;
147821 int nDel = 6 + (zType[i+6] ? 1 : 0);
147822 for(j=i; (j+nDel)<=nType; j++){
147823 zType[j] = zType[j+nDel];
147824 }
147825 if( zType[i]=='\0' && i>0 ){
147826 assert(zType[i-1]==' ')((void) (0));
147827 zType[i-1] = '\0';
147828 }
147829 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN0x0002;
147830 pTab->tabFlags |= TF_HasHidden0x00000002;
147831 oooHidden = TF_OOOHidden0x00000400;
147832 }else{
147833 pTab->tabFlags |= oooHidden;
147834 }
147835 }
147836 }
147837 }
147838
147839 sqlite3DbFree(db, zModuleName);
147840 return rc;
147841}
147842
147843/*
147844** This function is invoked by the parser to call the xConnect() method
147845** of the virtual table pTab. If an error occurs, an error code is returned
147846** and an error left in pParse.
147847**
147848** This call is a no-op if table pTab is not a virtual table.
147849*/
147850SQLITE_PRIVATEstatic int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
147851 sqlite3 *db = pParse->db;
147852 const char *zMod;
147853 Module *pMod;
147854 int rc;
147855
147856 assert( pTab )((void) (0));
147857 assert( IsVirtual(pTab) )((void) (0));
147858 if( sqlite3GetVTable(db, pTab) ){
147859 return SQLITE_OK0;
147860 }
147861
147862 /* Locate the required virtual table module */
147863 zMod = pTab->u.vtab.azArg[0];
147864 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
147865
147866 if( !pMod ){
147867 const char *zModule = pTab->u.vtab.azArg[0];
147868 sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
147869 rc = SQLITE_ERROR1;
147870 }else{
147871 char *zErr = 0;
147872 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
147873 if( rc!=SQLITE_OK0 ){
147874 sqlite3ErrorMsg(pParse, "%s", zErr);
147875 pParse->rc = rc;
147876 }
147877 sqlite3DbFree(db, zErr);
147878 }
147879
147880 return rc;
147881}
147882/*
147883** Grow the db->aVTrans[] array so that there is room for at least one
147884** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
147885*/
147886static int growVTrans(sqlite3 *db){
147887 const int ARRAY_INCR = 5;
147888
147889 /* Grow the sqlite3.aVTrans array if required */
147890 if( (db->nVTrans%ARRAY_INCR)==0 ){
147891 VTable **aVTrans;
147892 sqlite3_int64 nBytes = sizeof(sqlite3_vtab*)*
147893 ((sqlite3_int64)db->nVTrans + ARRAY_INCR);
147894 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
147895 if( !aVTrans ){
147896 return SQLITE_NOMEM_BKPT7;
147897 }
147898 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
147899 db->aVTrans = aVTrans;
147900 }
147901
147902 return SQLITE_OK0;
147903}
147904
147905/*
147906** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
147907** have already been reserved using growVTrans().
147908*/
147909static void addToVTrans(sqlite3 *db, VTable *pVTab){
147910 /* Add pVtab to the end of sqlite3.aVTrans */
147911 db->aVTrans[db->nVTrans++] = pVTab;
147912 sqlite3VtabLock(pVTab);
147913}
147914
147915/*
147916** This function is invoked by the vdbe to call the xCreate method
147917** of the virtual table named zTab in database iDb.
147918**
147919** If an error occurs, *pzErr is set to point to an English language
147920** description of the error and an SQLITE_XXX error code is returned.
147921** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
147922*/
147923SQLITE_PRIVATEstatic int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
147924 int rc = SQLITE_OK0;
147925 Table *pTab;
147926 Module *pMod;
147927 const char *zMod;
147928
147929 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
147930 assert( pTab && IsVirtual(pTab) && !pTab->u.vtab.p )((void) (0));
147931
147932 /* Locate the required virtual table module */
147933 zMod = pTab->u.vtab.azArg[0];
147934 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
147935
147936 /* If the module has been registered and includes a Create method,
147937 ** invoke it now. If the module has not been registered, return an
147938 ** error. Otherwise, do nothing.
147939 */
147940 if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){
147941 *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
147942 rc = SQLITE_ERROR1;
147943 }else{
147944 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
147945 }
147946
147947 /* Justification of ALWAYS(): The xConstructor method is required to
147948 ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
147949 if( rc==SQLITE_OK0 && ALWAYS(sqlite3GetVTable(db, pTab))(sqlite3GetVTable(db, pTab)) ){
147950 rc = growVTrans(db);
147951 if( rc==SQLITE_OK0 ){
147952 addToVTrans(db, sqlite3GetVTable(db, pTab));
147953 }
147954 }
147955
147956 return rc;
147957}
147958
147959/*
147960** This function is used to set the schema of a virtual table. It is only
147961** valid to call this function from within the xCreate() or xConnect() of a
147962** virtual table module.
147963*/
147964SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
147965 VtabCtx *pCtx;
147966 int rc = SQLITE_OK0;
147967 Table *pTab;
147968 Parse sParse;
147969 int initBusy;
147970
147971#ifdef SQLITE_ENABLE_API_ARMOR1
147972 if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){
147973 return SQLITE_MISUSE_BKPTsqlite3MisuseError(147973);
147974 }
147975#endif
147976 sqlite3_mutex_enter(db->mutex);
147977 pCtx = db->pVtabCtx;
147978 if( !pCtx || pCtx->bDeclared ){
147979 sqlite3Error(db, SQLITE_MISUSE21);
147980 sqlite3_mutex_leave(db->mutex);
147981 return SQLITE_MISUSE_BKPTsqlite3MisuseError(147981);
147982 }
147983 pTab = pCtx->pTab;
147984 assert( IsVirtual(pTab) )((void) (0));
147985
147986 sqlite3ParseObjectInit(&sParse, db);
147987 sParse.eParseMode = PARSE_MODE_DECLARE_VTAB1;
147988 sParse.disableTriggers = 1;
147989 /* We should never be able to reach this point while loading the
147990 ** schema. Nevertheless, defend against that (turn off db->init.busy)
147991 ** in case a bug arises. */
147992 assert( db->init.busy==0 )((void) (0));
147993 initBusy = db->init.busy;
147994 db->init.busy = 0;
147995 sParse.nQueryLoop = 1;
147996 if( SQLITE_OK0==sqlite3RunParser(&sParse, zCreateTable)
147997 && ALWAYS(sParse.pNewTable!=0)(sParse.pNewTable!=0)
147998 && ALWAYS(!db->mallocFailed)(!db->mallocFailed)
147999 && IsOrdinaryTable(sParse.pNewTable)((sParse.pNewTable)->eTabType==0)
148000 ){
148001 assert( sParse.zErrMsg==0 )((void) (0));
148002 if( !pTab->aCol ){
148003 Table *pNew = sParse.pNewTable;
148004 Index *pIdx;
148005 pTab->aCol = pNew->aCol;
148006 sqlite3ExprListDelete(db, pNew->u.tab.pDfltList);
148007 pTab->nNVCol = pTab->nCol = pNew->nCol;
148008 pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid0x00000080|TF_NoVisibleRowid0x00000200);
148009 pNew->nCol = 0;
148010 pNew->aCol = 0;
148011 assert( pTab->pIndex==0 )((void) (0));
148012 assert( HasRowid(pNew) || sqlite3PrimaryKeyIndex(pNew)!=0 )((void) (0));
148013 if( !HasRowid(pNew)(((pNew)->tabFlags & 0x00000080)==0)
148014 && pCtx->pVTable->pMod->pModule->xUpdate!=0
148015 && sqlite3PrimaryKeyIndex(pNew)->nKeyCol!=1
148016 ){
148017 /* WITHOUT ROWID virtual tables must either be read-only (xUpdate==0)
148018 ** or else must have a single-column PRIMARY KEY */
148019 rc = SQLITE_ERROR1;
148020 }
148021 pIdx = pNew->pIndex;
148022 if( pIdx ){
148023 assert( pIdx->pNext==0 )((void) (0));
148024 pTab->pIndex = pIdx;
148025 pNew->pIndex = 0;
148026 pIdx->pTable = pTab;
148027 }
148028 }
148029 pCtx->bDeclared = 1;
148030 }else{
148031 sqlite3ErrorWithMsg(db, SQLITE_ERROR1,
148032 (sParse.zErrMsg ? "%s" : 0), sParse.zErrMsg);
148033 sqlite3DbFree(db, sParse.zErrMsg);
148034 rc = SQLITE_ERROR1;
148035 }
148036 sParse.eParseMode = PARSE_MODE_NORMAL0;
148037
148038 if( sParse.pVdbe ){
148039 sqlite3VdbeFinalize(sParse.pVdbe);
148040 }
148041 sqlite3DeleteTable(db, sParse.pNewTable);
148042 sqlite3ParseObjectReset(&sParse);
148043 db->init.busy = initBusy;
148044
148045 assert( (rc&0xff)==rc )((void) (0));
148046 rc = sqlite3ApiExit(db, rc);
148047 sqlite3_mutex_leave(db->mutex);
148048 return rc;
148049}
148050
148051/*
148052** This function is invoked by the vdbe to call the xDestroy method
148053** of the virtual table named zTab in database iDb. This occurs
148054** when a DROP TABLE is mentioned.
148055**
148056** This call is a no-op if zTab is not a virtual table.
148057*/
148058SQLITE_PRIVATEstatic int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
148059 int rc = SQLITE_OK0;
148060 Table *pTab;
148061
148062 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
148063 if( ALWAYS(pTab!=0)(pTab!=0)
148064 && ALWAYS(IsVirtual(pTab))(((pTab)->eTabType==1))
148065 && ALWAYS(pTab->u.vtab.p!=0)(pTab->u.vtab.p!=0)
148066 ){
148067 VTable *p;
148068 int (*xDestroy)(sqlite3_vtab *);
148069 for(p=pTab->u.vtab.p; p; p=p->pNext){
148070 assert( p->pVtab )((void) (0));
148071 if( p->pVtab->nRef>0 ){
148072 return SQLITE_LOCKED6;
148073 }
148074 }
148075 p = vtabDisconnectAll(db, pTab);
148076 xDestroy = p->pMod->pModule->xDestroy;
148077 if( xDestroy==0 ) xDestroy = p->pMod->pModule->xDisconnect;
148078 assert( xDestroy!=0 )((void) (0));
148079 pTab->nTabRef++;
148080 rc = xDestroy(p->pVtab);
148081 /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
148082 if( rc==SQLITE_OK0 ){
148083 assert( pTab->u.vtab.p==p && p->pNext==0 )((void) (0));
148084 p->pVtab = 0;
148085 pTab->u.vtab.p = 0;
148086 sqlite3VtabUnlock(p);
148087 }
148088 sqlite3DeleteTable(db, pTab);
148089 }
148090
148091 return rc;
148092}
148093
148094/*
148095** This function invokes either the xRollback or xCommit method
148096** of each of the virtual tables in the sqlite3.aVTrans array. The method
148097** called is identified by the second argument, "offset", which is
148098** the offset of the method to call in the sqlite3_module structure.
148099**
148100** The array is cleared after invoking the callbacks.
148101*/
148102static void callFinaliser(sqlite3 *db, int offset){
148103 int i;
148104 if( db->aVTrans ){
148105 VTable **aVTrans = db->aVTrans;
148106 db->aVTrans = 0;
148107 for(i=0; i<db->nVTrans; i++){
148108 VTable *pVTab = aVTrans[i];
148109 sqlite3_vtab *p = pVTab->pVtab;
148110 if( p ){
148111 int (*x)(sqlite3_vtab *);
148112 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
148113 if( x ) x(p);
148114 }
148115 pVTab->iSavepoint = 0;
148116 sqlite3VtabUnlock(pVTab);
148117 }
148118 sqlite3DbFree(db, aVTrans);
148119 db->nVTrans = 0;
148120 }
148121}
148122
148123/*
148124** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
148125** array. Return the error code for the first error that occurs, or
148126** SQLITE_OK if all xSync operations are successful.
148127**
148128** If an error message is available, leave it in p->zErrMsg.
148129*/
148130SQLITE_PRIVATEstatic int sqlite3VtabSync(sqlite3 *db, Vdbe *p){
148131 int i;
148132 int rc = SQLITE_OK0;
148133 VTable **aVTrans = db->aVTrans;
148134
148135 db->aVTrans = 0;
148136 for(i=0; rc==SQLITE_OK0 && i<db->nVTrans; i++){
148137 int (*x)(sqlite3_vtab *);
148138 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
148139 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
148140 rc = x(pVtab);
148141 sqlite3VtabImportErrmsg(p, pVtab);
148142 }
148143 }
148144 db->aVTrans = aVTrans;
148145 return rc;
148146}
148147
148148/*
148149** Invoke the xRollback method of all virtual tables in the
148150** sqlite3.aVTrans array. Then clear the array itself.
148151*/
148152SQLITE_PRIVATEstatic int sqlite3VtabRollback(sqlite3 *db){
148153 callFinaliser(db, offsetof(sqlite3_module,xRollback)__builtin_offsetof(sqlite3_module, xRollback));
148154 return SQLITE_OK0;
148155}
148156
148157/*
148158** Invoke the xCommit method of all virtual tables in the
148159** sqlite3.aVTrans array. Then clear the array itself.
148160*/
148161SQLITE_PRIVATEstatic int sqlite3VtabCommit(sqlite3 *db){
148162 callFinaliser(db, offsetof(sqlite3_module,xCommit)__builtin_offsetof(sqlite3_module, xCommit));
148163 return SQLITE_OK0;
148164}
148165
148166/*
148167** If the virtual table pVtab supports the transaction interface
148168** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
148169** not currently open, invoke the xBegin method now.
148170**
148171** If the xBegin call is successful, place the sqlite3_vtab pointer
148172** in the sqlite3.aVTrans array.
148173*/
148174SQLITE_PRIVATEstatic int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
148175 int rc = SQLITE_OK0;
148176 const sqlite3_module *pModule;
148177
148178 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
148179 ** than zero, then this function is being called from within a
148180 ** virtual module xSync() callback. It is illegal to write to
148181 ** virtual module tables in this case, so return SQLITE_LOCKED.
148182 */
148183 if( sqlite3VtabInSync(db)((db)->nVTrans>0 && (db)->aVTrans==0) ){
148184 return SQLITE_LOCKED6;
148185 }
148186 if( !pVTab ){
148187 return SQLITE_OK0;
148188 }
148189 pModule = pVTab->pVtab->pModule;
148190
148191 if( pModule->xBegin ){
148192 int i;
148193
148194 /* If pVtab is already in the aVTrans array, return early */
148195 for(i=0; i<db->nVTrans; i++){
148196 if( db->aVTrans[i]==pVTab ){
148197 return SQLITE_OK0;
148198 }
148199 }
148200
148201 /* Invoke the xBegin method. If successful, add the vtab to the
148202 ** sqlite3.aVTrans[] array. */
148203 rc = growVTrans(db);
148204 if( rc==SQLITE_OK0 ){
148205 rc = pModule->xBegin(pVTab->pVtab);
148206 if( rc==SQLITE_OK0 ){
148207 int iSvpt = db->nStatement + db->nSavepoint;
148208 addToVTrans(db, pVTab);
148209 if( iSvpt && pModule->xSavepoint ){
148210 pVTab->iSavepoint = iSvpt;
148211 rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1);
148212 }
148213 }
148214 }
148215 }
148216 return rc;
148217}
148218
148219/*
148220** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
148221** virtual tables that currently have an open transaction. Pass iSavepoint
148222** as the second argument to the virtual table method invoked.
148223**
148224** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is
148225** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is
148226** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with
148227** an open transaction is invoked.
148228**
148229** If any virtual table method returns an error code other than SQLITE_OK,
148230** processing is abandoned and the error returned to the caller of this
148231** function immediately. If all calls to virtual table methods are successful,
148232** SQLITE_OK is returned.
148233*/
148234SQLITE_PRIVATEstatic int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
148235 int rc = SQLITE_OK0;
148236
148237 assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN )((void) (0));
148238 assert( iSavepoint>=-1 )((void) (0));
148239 if( db->aVTrans ){
148240 int i;
148241 for(i=0; rc==SQLITE_OK0 && i<db->nVTrans; i++){
148242 VTable *pVTab = db->aVTrans[i];
148243 const sqlite3_module *pMod = pVTab->pMod->pModule;
148244 if( pVTab->pVtab && pMod->iVersion>=2 ){
148245 int (*xMethod)(sqlite3_vtab *, int);
148246 sqlite3VtabLock(pVTab);
148247 switch( op ){
148248 case SAVEPOINT_BEGIN0:
148249 xMethod = pMod->xSavepoint;
148250 pVTab->iSavepoint = iSavepoint+1;
148251 break;
148252 case SAVEPOINT_ROLLBACK2:
148253 xMethod = pMod->xRollbackTo;
148254 break;
148255 default:
148256 xMethod = pMod->xRelease;
148257 break;
148258 }
148259 if( xMethod && pVTab->iSavepoint>iSavepoint ){
148260 rc = xMethod(pVTab->pVtab, iSavepoint);
148261 }
148262 sqlite3VtabUnlock(pVTab);
148263 }
148264 }
148265 }
148266 return rc;
148267}
148268
148269/*
148270** The first parameter (pDef) is a function implementation. The
148271** second parameter (pExpr) is the first argument to this function.
148272** If pExpr is a column in a virtual table, then let the virtual
148273** table implementation have an opportunity to overload the function.
148274**
148275** This routine is used to allow virtual table implementations to
148276** overload MATCH, LIKE, GLOB, and REGEXP operators.
148277**
148278** Return either the pDef argument (indicating no change) or a
148279** new FuncDef structure that is marked as ephemeral using the
148280** SQLITE_FUNC_EPHEM flag.
148281*/
148282SQLITE_PRIVATEstatic FuncDef *sqlite3VtabOverloadFunction(
148283 sqlite3 *db, /* Database connection for reporting malloc problems */
148284 FuncDef *pDef, /* Function to possibly overload */
148285 int nArg, /* Number of arguments to the function */
148286 Expr *pExpr /* First argument to the function */
148287){
148288 Table *pTab;
148289 sqlite3_vtab *pVtab;
148290 sqlite3_module *pMod;
148291 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
148292 void *pArg = 0;
148293 FuncDef *pNew;
148294 int rc = 0;
148295
148296 /* Check to see the left operand is a column in a virtual table */
148297 if( NEVER(pExpr==0)(pExpr==0) ) return pDef;
148298 if( pExpr->op!=TK_COLUMN167 ) return pDef;
148299 assert( ExprUseYTab(pExpr) )((void) (0));
148300 pTab = pExpr->y.pTab;
148301 if( pTab==0 ) return pDef;
148302 if( !IsVirtual(pTab)((pTab)->eTabType==1) ) return pDef;
148303 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
148304 assert( pVtab!=0 )((void) (0));
148305 assert( pVtab->pModule!=0 )((void) (0));
148306 pMod = (sqlite3_module *)pVtab->pModule;
148307 if( pMod->xFindFunction==0 ) return pDef;
148308
148309 /* Call the xFindFunction method on the virtual table implementation
148310 ** to see if the implementation wants to overload this function.
148311 **
148312 ** Though undocumented, we have historically always invoked xFindFunction
148313 ** with an all lower-case function name. Continue in this tradition to
148314 ** avoid any chance of an incompatibility.
148315 */
148316#ifdef SQLITE_DEBUG
148317 {
148318 int i;
148319 for(i=0; pDef->zName[i]; i++){
148320 unsigned char x = (unsigned char)pDef->zName[i];
148321 assert( x==sqlite3UpperToLower[x] )((void) (0));
148322 }
148323 }
148324#endif
148325 rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg);
148326 if( rc==0 ){
148327 return pDef;
148328 }
148329
148330 /* Create a new ephemeral function definition for the overloaded
148331 ** function */
148332 pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
148333 + sqlite3Strlen30(pDef->zName) + 1);
148334 if( pNew==0 ){
148335 return pDef;
148336 }
148337 *pNew = *pDef;
148338 pNew->zName = (const char*)&pNew[1];
148339 memcpy((char*)&pNew[1], pDef->zName, sqlite3Strlen30(pDef->zName)+1);
148340 pNew->xSFunc = xSFunc;
148341 pNew->pUserData = pArg;
148342 pNew->funcFlags |= SQLITE_FUNC_EPHEM0x0010;
148343 return pNew;
148344}
148345
148346/*
148347** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
148348** array so that an OP_VBegin will get generated for it. Add pTab to the
148349** array if it is missing. If pTab is already in the array, this routine
148350** is a no-op.
148351*/
148352SQLITE_PRIVATEstatic void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
148353 Parse *pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
148354 int i, n;
148355 Table **apVtabLock;
148356
148357 assert( IsVirtual(pTab) )((void) (0));
148358 for(i=0; i<pToplevel->nVtabLock; i++){
148359 if( pTab==pToplevel->apVtabLock[i] ) return;
148360 }
148361 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
148362 apVtabLock = sqlite3Realloc(pToplevel->apVtabLock, n);
148363 if( apVtabLock ){
148364 pToplevel->apVtabLock = apVtabLock;
148365 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
148366 }else{
148367 sqlite3OomFault(pToplevel->db);
148368 }
148369}
148370
148371/*
148372** Check to see if virtual table module pMod can be have an eponymous
148373** virtual table instance. If it can, create one if one does not already
148374** exist. Return non-zero if either the eponymous virtual table instance
148375** exists when this routine returns or if an attempt to create it failed
148376** and an error message was left in pParse.
148377**
148378** An eponymous virtual table instance is one that is named after its
148379** module, and more importantly, does not require a CREATE VIRTUAL TABLE
148380** statement in order to come into existance. Eponymous virtual table
148381** instances always exist. They cannot be DROP-ed.
148382**
148383** Any virtual table module for which xConnect and xCreate are the same
148384** method can have an eponymous virtual table instance.
148385*/
148386SQLITE_PRIVATEstatic int sqlite3VtabEponymousTableInit(Parse *pParse, Module *pMod){
148387 const sqlite3_module *pModule = pMod->pModule;
148388 Table *pTab;
148389 char *zErr = 0;
148390 int rc;
148391 sqlite3 *db = pParse->db;
148392 if( pMod->pEpoTab ) return 1;
148393 if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0;
148394 pTab = sqlite3DbMallocZero(db, sizeof(Table));
148395 if( pTab==0 ) return 0;
148396 pTab->zName = sqlite3DbStrDup(db, pMod->zName);
148397 if( pTab->zName==0 ){
148398 sqlite3DbFree(db, pTab);
148399 return 0;
148400 }
148401 pMod->pEpoTab = pTab;
148402 pTab->nTabRef = 1;
148403 pTab->eTabType = TABTYP_VTAB1;
148404 pTab->pSchema = db->aDb[0].pSchema;
148405 assert( pTab->u.vtab.nArg==0 )((void) (0));
148406 pTab->iPKey = -1;
148407 pTab->tabFlags |= TF_Eponymous0x00008000;
148408 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
148409 addModuleArgument(pParse, pTab, 0);
148410 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
148411 rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
148412 if( rc ){
148413 sqlite3ErrorMsg(pParse, "%s", zErr);
148414 sqlite3DbFree(db, zErr);
148415 sqlite3VtabEponymousTableClear(db, pMod);
148416 }
148417 return 1;
148418}
148419
148420/*
148421** Erase the eponymous virtual table instance associated with
148422** virtual table module pMod, if it exists.
148423*/
148424SQLITE_PRIVATEstatic void sqlite3VtabEponymousTableClear(sqlite3 *db, Module *pMod){
148425 Table *pTab = pMod->pEpoTab;
148426 if( pTab!=0 ){
148427 /* Mark the table as Ephemeral prior to deleting it, so that the
148428 ** sqlite3DeleteTable() routine will know that it is not stored in
148429 ** the schema. */
148430 pTab->tabFlags |= TF_Ephemeral0x00004000;
148431 sqlite3DeleteTable(db, pTab);
148432 pMod->pEpoTab = 0;
148433 }
148434}
148435
148436/*
148437** Return the ON CONFLICT resolution mode in effect for the virtual
148438** table update operation currently in progress.
148439**
148440** The results of this routine are undefined unless it is called from
148441** within an xUpdate method.
148442*/
148443SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){
148444 static const unsigned char aMap[] = {
148445 SQLITE_ROLLBACK1, SQLITE_ABORT4, SQLITE_FAIL3, SQLITE_IGNORE2, SQLITE_REPLACE5
148446 };
148447#ifdef SQLITE_ENABLE_API_ARMOR1
148448 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(148448);
148449#endif
148450 assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 )((void) (0));
148451 assert( OE_Ignore==4 && OE_Replace==5 )((void) (0));
148452 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 )((void) (0));
148453 return (int)aMap[db->vtabOnConflict-1];
148454}
148455
148456/*
148457** Call from within the xCreate() or xConnect() methods to provide
148458** the SQLite core with additional information about the behavior
148459** of the virtual table being implemented.
148460*/
148461SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){
148462 va_list ap;
148463 int rc = SQLITE_OK0;
148464 VtabCtx *p;
148465
148466#ifdef SQLITE_ENABLE_API_ARMOR1
148467 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(148467);
148468#endif
148469 sqlite3_mutex_enter(db->mutex);
148470 p = db->pVtabCtx;
148471 if( !p ){
148472 rc = SQLITE_MISUSE_BKPTsqlite3MisuseError(148472);
148473 }else{
148474 assert( p->pTab==0 || IsVirtual(p->pTab) )((void) (0));
148475 va_start(ap, op)__builtin_va_start(ap, op);
148476 switch( op ){
148477 case SQLITE_VTAB_CONSTRAINT_SUPPORT1: {
148478 p->pVTable->bConstraint = (u8)va_arg(ap, int)__builtin_va_arg(ap, int);
148479 break;
148480 }
148481 case SQLITE_VTAB_INNOCUOUS2: {
148482 p->pVTable->eVtabRisk = SQLITE_VTABRISK_Low0;
148483 break;
148484 }
148485 case SQLITE_VTAB_DIRECTONLY3: {
148486 p->pVTable->eVtabRisk = SQLITE_VTABRISK_High2;
148487 break;
148488 }
148489 default: {
148490 rc = SQLITE_MISUSE_BKPTsqlite3MisuseError(148490);
148491 break;
148492 }
148493 }
148494 va_end(ap)__builtin_va_end(ap);
148495 }
148496
148497 if( rc!=SQLITE_OK0 ) sqlite3Error(db, rc);
148498 sqlite3_mutex_leave(db->mutex);
148499 return rc;
148500}
148501
148502#endif /* SQLITE_OMIT_VIRTUALTABLE */
148503
148504/************** End of vtab.c ************************************************/
148505/************** Begin file wherecode.c ***************************************/
148506/*
148507** 2015-06-06
148508**
148509** The author disclaims copyright to this source code. In place of
148510** a legal notice, here is a blessing:
148511**
148512** May you do good and not evil.
148513** May you find forgiveness for yourself and forgive others.
148514** May you share freely, never taking more than you give.
148515**
148516*************************************************************************
148517** This module contains C code that generates VDBE code used to process
148518** the WHERE clause of SQL statements.
148519**
148520** This file was split off from where.c on 2015-06-06 in order to reduce the
148521** size of where.c and make it easier to edit. This file contains the routines
148522** that actually generate the bulk of the WHERE loop code. The original where.c
148523** file retains the code that does query planning and analysis.
148524*/
148525/* #include "sqliteInt.h" */
148526/************** Include whereInt.h in the middle of wherecode.c **************/
148527/************** Begin file whereInt.h ****************************************/
148528/*
148529** 2013-11-12
148530**
148531** The author disclaims copyright to this source code. In place of
148532** a legal notice, here is a blessing:
148533**
148534** May you do good and not evil.
148535** May you find forgiveness for yourself and forgive others.
148536** May you share freely, never taking more than you give.
148537**
148538*************************************************************************
148539**
148540** This file contains structure and macro definitions for the query
148541** planner logic in "where.c". These definitions are broken out into
148542** a separate source file for easier editing.
148543*/
148544#ifndef SQLITE_WHEREINT_H
148545#define SQLITE_WHEREINT_H
148546
148547
148548/* Forward references
148549*/
148550typedef struct WhereClause WhereClause;
148551typedef struct WhereMaskSet WhereMaskSet;
148552typedef struct WhereOrInfo WhereOrInfo;
148553typedef struct WhereAndInfo WhereAndInfo;
148554typedef struct WhereLevel WhereLevel;
148555typedef struct WhereLoop WhereLoop;
148556typedef struct WherePath WherePath;
148557typedef struct WhereTerm WhereTerm;
148558typedef struct WhereLoopBuilder WhereLoopBuilder;
148559typedef struct WhereScan WhereScan;
148560typedef struct WhereOrCost WhereOrCost;
148561typedef struct WhereOrSet WhereOrSet;
148562typedef struct WhereMemBlock WhereMemBlock;
148563typedef struct WhereRightJoin WhereRightJoin;
148564
148565/*
148566** This object is a header on a block of allocated memory that will be
148567** automatically freed when its WInfo oject is destructed.
148568*/
148569struct WhereMemBlock {
148570 WhereMemBlock *pNext; /* Next block in the chain */
148571 u64 sz; /* Bytes of space */
148572};
148573
148574/*
148575** Extra information attached to a WhereLevel that is a RIGHT JOIN.
148576*/
148577struct WhereRightJoin {
148578 int iMatch; /* Cursor used to determine prior matched rows */
148579 int regBloom; /* Bloom filter for iRJMatch */
148580 int regReturn; /* Return register for the interior subroutine */
148581 int addrSubrtn; /* Starting address for the interior subroutine */
148582 int endSubrtn; /* The last opcode in the interior subroutine */
148583};
148584
148585/*
148586** This object contains information needed to implement a single nested
148587** loop in WHERE clause.
148588**
148589** Contrast this object with WhereLoop. This object describes the
148590** implementation of the loop. WhereLoop describes the algorithm.
148591** This object contains a pointer to the WhereLoop algorithm as one of
148592** its elements.
148593**
148594** The WhereInfo object contains a single instance of this object for
148595** each term in the FROM clause (which is to say, for each of the
148596** nested loops as implemented). The order of WhereLevel objects determines
148597** the loop nested order, with WhereInfo.a[0] being the outer loop and
148598** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
148599*/
148600struct WhereLevel {
148601 int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */
148602 int iTabCur; /* The VDBE cursor used to access the table */
148603 int iIdxCur; /* The VDBE cursor used to access pIdx */
148604 int addrBrk; /* Jump here to break out of the loop */
148605 int addrNxt; /* Jump here to start the next IN combination */
148606 int addrSkip; /* Jump here for next iteration of skip-scan */
148607 int addrCont; /* Jump here to continue with the next loop cycle */
148608 int addrFirst; /* First instruction of interior of the loop */
148609 int addrBody; /* Beginning of the body of this loop */
148610 int regBignull; /* big-null flag reg. True if a NULL-scan is needed */
148611 int addrBignull; /* Jump here for next part of big-null scan */
148612#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
148613 u32 iLikeRepCntr; /* LIKE range processing counter register (times 2) */
148614 int addrLikeRep; /* LIKE range processing address */
148615#endif
148616 int regFilter; /* Bloom filter */
148617 WhereRightJoin *pRJ; /* Extra information for RIGHT JOIN */
148618 u8 iFrom; /* Which entry in the FROM clause */
148619 u8 op, p3, p5; /* Opcode, P3 & P5 of the opcode that ends the loop */
148620 int p1, p2; /* Operands of the opcode used to end the loop */
148621 union { /* Information that depends on pWLoop->wsFlags */
148622 struct {
148623 int nIn; /* Number of entries in aInLoop[] */
148624 struct InLoop {
148625 int iCur; /* The VDBE cursor used by this IN operator */
148626 int addrInTop; /* Top of the IN loop */
148627 int iBase; /* Base register of multi-key index record */
148628 int nPrefix; /* Number of prior entires in the key */
148629 u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */
148630 } *aInLoop; /* Information about each nested IN operator */
148631 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
148632 Index *pCoveringIdx; /* Possible covering index for WHERE_MULTI_OR */
148633 } u;
148634 struct WhereLoop *pWLoop; /* The selected WhereLoop object */
148635 Bitmask notReady; /* FROM entries not usable at this level */
148636#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
148637 int addrVisit; /* Address at which row is visited */
148638#endif
148639};
148640
148641/*
148642** Each instance of this object represents an algorithm for evaluating one
148643** term of a join. Every term of the FROM clause will have at least
148644** one corresponding WhereLoop object (unless INDEXED BY constraints
148645** prevent a query solution - which is an error) and many terms of the
148646** FROM clause will have multiple WhereLoop objects, each describing a
148647** potential way of implementing that FROM-clause term, together with
148648** dependencies and cost estimates for using the chosen algorithm.
148649**
148650** Query planning consists of building up a collection of these WhereLoop
148651** objects, then computing a particular sequence of WhereLoop objects, with
148652** one WhereLoop object per FROM clause term, that satisfy all dependencies
148653** and that minimize the overall cost.
148654*/
148655struct WhereLoop {
148656 Bitmask prereq; /* Bitmask of other loops that must run first */
148657 Bitmask maskSelf; /* Bitmask identifying table iTab */
148658#ifdef SQLITE_DEBUG
148659 char cId; /* Symbolic ID of this loop for debugging use */
148660#endif
148661 u8 iTab; /* Position in FROM clause of table for this loop */
148662 u8 iSortIdx; /* Sorting index number. 0==None */
148663 LogEst rSetup; /* One-time setup cost (ex: create transient index) */
148664 LogEst rRun; /* Cost of running each loop */
148665 LogEst nOut; /* Estimated number of output rows */
148666 union {
148667 struct { /* Information for internal btree tables */
148668 u16 nEq; /* Number of equality constraints */
148669 u16 nBtm; /* Size of BTM vector */
148670 u16 nTop; /* Size of TOP vector */
148671 u16 nDistinctCol; /* Index columns used to sort for DISTINCT */
148672 Index *pIndex; /* Index used, or NULL */
148673 } btree;
148674 struct { /* Information for virtual tables */
148675 int idxNum; /* Index number */
148676 u32 needFree : 1; /* True if sqlite3_free(idxStr) is needed */
148677 u32 bOmitOffset : 1; /* True to let virtual table handle offset */
148678 i8 isOrdered; /* True if satisfies ORDER BY */
148679 u16 omitMask; /* Terms that may be omitted */
148680 char *idxStr; /* Index identifier string */
148681 u32 mHandleIn; /* Terms to handle as IN(...) instead of == */
148682 } vtab;
148683 } u;
148684 u32 wsFlags; /* WHERE_* flags describing the plan */
148685 u16 nLTerm; /* Number of entries in aLTerm[] */
148686 u16 nSkip; /* Number of NULL aLTerm[] entries */
148687 /**** whereLoopXfer() copies fields above ***********************/
148688# define WHERE_LOOP_XFER_SZ__builtin_offsetof(WhereLoop, nLSlot) offsetof(WhereLoop,nLSlot)__builtin_offsetof(WhereLoop, nLSlot)
148689 u16 nLSlot; /* Number of slots allocated for aLTerm[] */
148690 WhereTerm **aLTerm; /* WhereTerms used */
148691 WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */
148692 WhereTerm *aLTermSpace[3]; /* Initial aLTerm[] space */
148693};
148694
148695/* This object holds the prerequisites and the cost of running a
148696** subquery on one operand of an OR operator in the WHERE clause.
148697** See WhereOrSet for additional information
148698*/
148699struct WhereOrCost {
148700 Bitmask prereq; /* Prerequisites */
148701 LogEst rRun; /* Cost of running this subquery */
148702 LogEst nOut; /* Number of outputs for this subquery */
148703};
148704
148705/* The WhereOrSet object holds a set of possible WhereOrCosts that
148706** correspond to the subquery(s) of OR-clause processing. Only the
148707** best N_OR_COST elements are retained.
148708*/
148709#define N_OR_COST3 3
148710struct WhereOrSet {
148711 u16 n; /* Number of valid a[] entries */
148712 WhereOrCost a[N_OR_COST3]; /* Set of best costs */
148713};
148714
148715/*
148716** Each instance of this object holds a sequence of WhereLoop objects
148717** that implement some or all of a query plan.
148718**
148719** Think of each WhereLoop object as a node in a graph with arcs
148720** showing dependencies and costs for travelling between nodes. (That is
148721** not a completely accurate description because WhereLoop costs are a
148722** vector, not a scalar, and because dependencies are many-to-one, not
148723** one-to-one as are graph nodes. But it is a useful visualization aid.)
148724** Then a WherePath object is a path through the graph that visits some
148725** or all of the WhereLoop objects once.
148726**
148727** The "solver" works by creating the N best WherePath objects of length
148728** 1. Then using those as a basis to compute the N best WherePath objects
148729** of length 2. And so forth until the length of WherePaths equals the
148730** number of nodes in the FROM clause. The best (lowest cost) WherePath
148731** at the end is the chosen query plan.
148732*/
148733struct WherePath {
148734 Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */
148735 Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */
148736 LogEst nRow; /* Estimated number of rows generated by this path */
148737 LogEst rCost; /* Total cost of this path */
148738 LogEst rUnsorted; /* Total cost of this path ignoring sorting costs */
148739 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
148740 WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */
148741};
148742
148743/*
148744** The query generator uses an array of instances of this structure to
148745** help it analyze the subexpressions of the WHERE clause. Each WHERE
148746** clause subexpression is separated from the others by AND operators,
148747** usually, or sometimes subexpressions separated by OR.
148748**
148749** All WhereTerms are collected into a single WhereClause structure.
148750** The following identity holds:
148751**
148752** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
148753**
148754** When a term is of the form:
148755**
148756** X <op> <expr>
148757**
148758** where X is a column name and <op> is one of certain operators,
148759** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
148760** cursor number and column number for X. WhereTerm.eOperator records
148761** the <op> using a bitmask encoding defined by WO_xxx below. The
148762** use of a bitmask encoding for the operator allows us to search
148763** quickly for terms that match any of several different operators.
148764**
148765** A WhereTerm might also be two or more subterms connected by OR:
148766**
148767** (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
148768**
148769** In this second case, wtFlag has the TERM_ORINFO bit set and eOperator==WO_OR
148770** and the WhereTerm.u.pOrInfo field points to auxiliary information that
148771** is collected about the OR clause.
148772**
148773** If a term in the WHERE clause does not match either of the two previous
148774** categories, then eOperator==0. The WhereTerm.pExpr field is still set
148775** to the original subexpression content and wtFlags is set up appropriately
148776** but no other fields in the WhereTerm object are meaningful.
148777**
148778** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
148779** but they do so indirectly. A single WhereMaskSet structure translates
148780** cursor number into bits and the translated bit is stored in the prereq
148781** fields. The translation is used in order to maximize the number of
148782** bits that will fit in a Bitmask. The VDBE cursor numbers might be
148783** spread out over the non-negative integers. For example, the cursor
148784** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The WhereMaskSet
148785** translates these sparse cursor numbers into consecutive integers
148786** beginning with 0 in order to make the best possible use of the available
148787** bits in the Bitmask. So, in the example above, the cursor numbers
148788** would be mapped into integers 0 through 7.
148789**
148790** The number of terms in a join is limited by the number of bits
148791** in prereqRight and prereqAll. The default is 64 bits, hence SQLite
148792** is only able to process joins with 64 or fewer tables.
148793*/
148794struct WhereTerm {
148795 Expr *pExpr; /* Pointer to the subexpression that is this term */
148796 WhereClause *pWC; /* The clause this term is part of */
148797 LogEst truthProb; /* Probability of truth for this expression */
148798 u16 wtFlags; /* TERM_xxx bit flags. See below */
148799 u16 eOperator; /* A WO_xx value describing <op> */
148800 u8 nChild; /* Number of children that must disable us */
148801 u8 eMatchOp; /* Op for vtab MATCH/LIKE/GLOB/REGEXP terms */
148802 int iParent; /* Disable pWC->a[iParent] when this term disabled */
148803 int leftCursor; /* Cursor number of X in "X <op> <expr>" */
148804 union {
148805 struct {
148806 int leftColumn; /* Column number of X in "X <op> <expr>" */
148807 int iField; /* Field in (?,?,?) IN (SELECT...) vector */
148808 } x; /* Opcode other than OP_OR or OP_AND */
148809 WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */
148810 WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */
148811 } u;
148812 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
148813 Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */
148814};
148815
148816/*
148817** Allowed values of WhereTerm.wtFlags
148818*/
148819#define TERM_DYNAMIC0x0001 0x0001 /* Need to call sqlite3ExprDelete(db, pExpr) */
148820#define TERM_VIRTUAL0x0002 0x0002 /* Added by the optimizer. Do not code */
148821#define TERM_CODED0x0004 0x0004 /* This term is already coded */
148822#define TERM_COPIED0x0008 0x0008 /* Has a child */
148823#define TERM_ORINFO0x0010 0x0010 /* Need to free the WhereTerm.u.pOrInfo object */
148824#define TERM_ANDINFO0x0020 0x0020 /* Need to free the WhereTerm.u.pAndInfo obj */
148825#define TERM_OK0x0040 0x0040 /* Used during OR-clause processing */
148826#define TERM_VNULL0x0080 0x0080 /* Manufactured x>NULL or x<=NULL term */
148827#define TERM_LIKEOPT0x0100 0x0100 /* Virtual terms from the LIKE optimization */
148828#define TERM_LIKECOND0x0200 0x0200 /* Conditionally this LIKE operator term */
148829#define TERM_LIKE0x0400 0x0400 /* The original LIKE operator */
148830#define TERM_IS0x0800 0x0800 /* Term.pExpr is an IS operator */
148831#define TERM_VARSELECT0x1000 0x1000 /* Term.pExpr contains a correlated sub-query */
148832#define TERM_HEURTRUTH0x2000 0x2000 /* Heuristic truthProb used */
148833#ifdef SQLITE_ENABLE_STAT4
148834# define TERM_HIGHTRUTH0 0x4000 /* Term excludes few rows */
148835#else
148836# define TERM_HIGHTRUTH0 0 /* Only used with STAT4 */
148837#endif
148838#define TERM_SLICE0x8000 0x8000 /* One slice of a row-value/vector comparison */
148839
148840/*
148841** An instance of the WhereScan object is used as an iterator for locating
148842** terms in the WHERE clause that are useful to the query planner.
148843*/
148844struct WhereScan {
148845 WhereClause *pOrigWC; /* Original, innermost WhereClause */
148846 WhereClause *pWC; /* WhereClause currently being scanned */
148847 const char *zCollName; /* Required collating sequence, if not NULL */
148848 Expr *pIdxExpr; /* Search for this index expression */
148849 int k; /* Resume scanning at this->pWC->a[this->k] */
148850 u32 opMask; /* Acceptable operators */
148851 char idxaff; /* Must match this affinity, if zCollName!=NULL */
148852 unsigned char iEquiv; /* Current slot in aiCur[] and aiColumn[] */
148853 unsigned char nEquiv; /* Number of entries in aiCur[] and aiColumn[] */
148854 int aiCur[11]; /* Cursors in the equivalence class */
148855 i16 aiColumn[11]; /* Corresponding column number in the eq-class */
148856};
148857
148858/*
148859** An instance of the following structure holds all information about a
148860** WHERE clause. Mostly this is a container for one or more WhereTerms.
148861**
148862** Explanation of pOuter: For a WHERE clause of the form
148863**
148864** a AND ((b AND c) OR (d AND e)) AND f
148865**
148866** There are separate WhereClause objects for the whole clause and for
148867** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the
148868** subclauses points to the WhereClause object for the whole clause.
148869*/
148870struct WhereClause {
148871 WhereInfo *pWInfo; /* WHERE clause processing context */
148872 WhereClause *pOuter; /* Outer conjunction */
148873 u8 op; /* Split operator. TK_AND or TK_OR */
148874 u8 hasOr; /* True if any a[].eOperator is WO_OR */
148875 int nTerm; /* Number of terms */
148876 int nSlot; /* Number of entries in a[] */
148877 int nBase; /* Number of terms through the last non-Virtual */
148878 WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */
148879#if defined(SQLITE_SMALL_STACK)
148880 WhereTerm aStatic[1]; /* Initial static space for a[] */
148881#else
148882 WhereTerm aStatic[8]; /* Initial static space for a[] */
148883#endif
148884};
148885
148886/*
148887** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
148888** a dynamically allocated instance of the following structure.
148889*/
148890struct WhereOrInfo {
148891 WhereClause wc; /* Decomposition into subterms */
148892 Bitmask indexable; /* Bitmask of all indexable tables in the clause */
148893};
148894
148895/*
148896** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
148897** a dynamically allocated instance of the following structure.
148898*/
148899struct WhereAndInfo {
148900 WhereClause wc; /* The subexpression broken out */
148901};
148902
148903/*
148904** An instance of the following structure keeps track of a mapping
148905** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
148906**
148907** The VDBE cursor numbers are small integers contained in
148908** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE
148909** clause, the cursor numbers might not begin with 0 and they might
148910** contain gaps in the numbering sequence. But we want to make maximum
148911** use of the bits in our bitmasks. This structure provides a mapping
148912** from the sparse cursor numbers into consecutive integers beginning
148913** with 0.
148914**
148915** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
148916** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
148917**
148918** For example, if the WHERE clause expression used these VDBE
148919** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure
148920** would map those cursor numbers into bits 0 through 5.
148921**
148922** Note that the mapping is not necessarily ordered. In the example
148923** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
148924** 57->5, 73->4. Or one of 719 other combinations might be used. It
148925** does not really matter. What is important is that sparse cursor
148926** numbers all get mapped into bit numbers that begin with 0 and contain
148927** no gaps.
148928*/
148929struct WhereMaskSet {
148930 int bVarSelect; /* Used by sqlite3WhereExprUsage() */
148931 int n; /* Number of assigned cursor values */
148932 int ix[BMS((int)(sizeof(Bitmask)*8))]; /* Cursor assigned to each bit */
148933};
148934
148935/*
148936** This object is a convenience wrapper holding all information needed
148937** to construct WhereLoop objects for a particular query.
148938*/
148939struct WhereLoopBuilder {
148940 WhereInfo *pWInfo; /* Information about this WHERE */
148941 WhereClause *pWC; /* WHERE clause terms */
148942 WhereLoop *pNew; /* Template WhereLoop */
148943 WhereOrSet *pOrSet; /* Record best loops here, if not NULL */
148944#ifdef SQLITE_ENABLE_STAT4
148945 UnpackedRecord *pRec; /* Probe for stat4 (if required) */
148946 int nRecValid; /* Number of valid fields currently in pRec */
148947#endif
148948 unsigned char bldFlags1; /* First set of SQLITE_BLDF_* flags */
148949 unsigned char bldFlags2; /* Second set of SQLITE_BLDF_* flags */
148950 unsigned int iPlanLimit; /* Search limiter */
148951};
148952
148953/* Allowed values for WhereLoopBuider.bldFlags */
148954#define SQLITE_BLDF1_INDEXED0x0001 0x0001 /* An index is used */
148955#define SQLITE_BLDF1_UNIQUE0x0002 0x0002 /* All keys of a UNIQUE index used */
148956
148957#define SQLITE_BLDF2_2NDPASS0x0004 0x0004 /* Second builder pass needed */
148958
148959/* The WhereLoopBuilder.iPlanLimit is used to limit the number of
148960** index+constraint combinations the query planner will consider for a
148961** particular query. If this parameter is unlimited, then certain
148962** pathological queries can spend excess time in the sqlite3WhereBegin()
148963** routine. The limit is high enough that is should not impact real-world
148964** queries.
148965**
148966** SQLITE_QUERY_PLANNER_LIMIT is the baseline limit. The limit is
148967** increased by SQLITE_QUERY_PLANNER_LIMIT_INCR before each term of the FROM
148968** clause is processed, so that every table in a join is guaranteed to be
148969** able to propose a some index+constraint combinations even if the initial
148970** baseline limit was exhausted by prior tables of the join.
148971*/
148972#ifndef SQLITE_QUERY_PLANNER_LIMIT20000
148973# define SQLITE_QUERY_PLANNER_LIMIT20000 20000
148974#endif
148975#ifndef SQLITE_QUERY_PLANNER_LIMIT_INCR1000
148976# define SQLITE_QUERY_PLANNER_LIMIT_INCR1000 1000
148977#endif
148978
148979/*
148980** Each instance of this object records a change to a single node
148981** in an expression tree to cause that node to point to a column
148982** of an index rather than an expression or a virtual column. All
148983** such transformations need to be undone at the end of WHERE clause
148984** processing.
148985*/
148986typedef struct WhereExprMod WhereExprMod;
148987struct WhereExprMod {
148988 WhereExprMod *pNext; /* Next translation on a list of them all */
148989 Expr *pExpr; /* The Expr node that was transformed */
148990 Expr orig; /* Original value of the Expr node */
148991};
148992
148993/*
148994** The WHERE clause processing routine has two halves. The
148995** first part does the start of the WHERE loop and the second
148996** half does the tail of the WHERE loop. An instance of
148997** this structure is returned by the first half and passed
148998** into the second half to give some continuity.
148999**
149000** An instance of this object holds the complete state of the query
149001** planner.
149002*/
149003struct WhereInfo {
149004 Parse *pParse; /* Parsing and code generating context */
149005 SrcList *pTabList; /* List of tables in the join */
149006 ExprList *pOrderBy; /* The ORDER BY clause or NULL */
149007 ExprList *pResultSet; /* Result set of the query */
149008 Expr *pWhere; /* The complete WHERE clause */
149009#ifndef SQLITE_OMIT_VIRTUALTABLE
149010 Select *pLimit; /* Used to access LIMIT expr/registers for vtabs */
149011#endif
149012 int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */
149013 int iContinue; /* Jump here to continue with next record */
149014 int iBreak; /* Jump here to break out of the loop */
149015 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
149016 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
149017 LogEst iLimit; /* LIMIT if wctrlFlags has WHERE_USE_LIMIT */
149018 u8 nLevel; /* Number of nested loop */
149019 i8 nOBSat; /* Number of ORDER BY terms satisfied by indices */
149020 u8 eOnePass; /* ONEPASS_OFF, or _SINGLE, or _MULTI */
149021 u8 eDistinct; /* One of the WHERE_DISTINCT_* values */
149022 unsigned bDeferredSeek :1; /* Uses OP_DeferredSeek */
149023 unsigned untestedTerms :1; /* Not all WHERE terms resolved by outer loop */
149024 unsigned bOrderedInnerLoop:1;/* True if only the inner-most loop is ordered */
149025 unsigned sorted :1; /* True if really sorted (not just grouped) */
149026 LogEst nRowOut; /* Estimated number of output rows */
149027 int iTop; /* The very beginning of the WHERE loop */
149028 int iEndWhere; /* End of the WHERE clause itself */
149029 WhereLoop *pLoops; /* List of all WhereLoop objects */
149030 WhereExprMod *pExprMods; /* Expression modifications */
149031 WhereMemBlock *pMemToFree;/* Memory to free when this object destroyed */
149032 Bitmask revMask; /* Mask of ORDER BY terms that need reversing */
149033 WhereClause sWC; /* Decomposition of the WHERE clause */
149034 WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */
149035 WhereLevel a[1]; /* Information about each nest loop in WHERE */
149036};
149037
149038/*
149039** Private interfaces - callable only by other where.c routines.
149040**
149041** where.c:
149042*/
149043SQLITE_PRIVATEstatic Bitmask sqlite3WhereGetMask(WhereMaskSet*,int);
149044#ifdef WHERETRACE_ENABLED
149045SQLITE_PRIVATEstatic void sqlite3WhereClausePrint(WhereClause *pWC);
149046SQLITE_PRIVATEstatic void sqlite3WhereTermPrint(WhereTerm *pTerm, int iTerm);
149047SQLITE_PRIVATEstatic void sqlite3WhereLoopPrint(WhereLoop *p, WhereClause *pWC);
149048#endif
149049SQLITE_PRIVATEstatic WhereTerm *sqlite3WhereFindTerm(
149050 WhereClause *pWC, /* The WHERE clause to be searched */
149051 int iCur, /* Cursor number of LHS */
149052 int iColumn, /* Column number of LHS */
149053 Bitmask notReady, /* RHS must not overlap with this mask */
149054 u32 op, /* Mask of WO_xx values describing operator */
149055 Index *pIdx /* Must be compatible with this index, if not NULL */
149056);
149057SQLITE_PRIVATEstatic void *sqlite3WhereMalloc(WhereInfo *pWInfo, u64 nByte);
149058SQLITE_PRIVATEstatic void *sqlite3WhereRealloc(WhereInfo *pWInfo, void *pOld, u64 nByte);
149059
149060/* wherecode.c: */
149061#ifndef SQLITE_OMIT_EXPLAIN
149062SQLITE_PRIVATEstatic int sqlite3WhereExplainOneScan(
149063 Parse *pParse, /* Parse context */
149064 SrcList *pTabList, /* Table list this loop refers to */
149065 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
149066 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
149067);
149068SQLITE_PRIVATEstatic int sqlite3WhereExplainBloomFilter(
149069 const Parse *pParse, /* Parse context */
149070 const WhereInfo *pWInfo, /* WHERE clause */
149071 const WhereLevel *pLevel /* Bloom filter on this level */
149072);
149073#else
149074# define sqlite3WhereExplainOneScan(u,v,w,x) 0
149075# define sqlite3WhereExplainBloomFilter(u,v,w) 0
149076#endif /* SQLITE_OMIT_EXPLAIN */
149077#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
149078SQLITE_PRIVATEstatic void sqlite3WhereAddScanStatus(
149079 Vdbe *v, /* Vdbe to add scanstatus entry to */
149080 SrcList *pSrclist, /* FROM clause pLvl reads data from */
149081 WhereLevel *pLvl, /* Level to add scanstatus() entry for */
149082 int addrExplain /* Address of OP_Explain (or 0) */
149083);
149084#else
149085# define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d)
149086#endif
149087SQLITE_PRIVATEstatic Bitmask sqlite3WhereCodeOneLoopStart(
149088 Parse *pParse, /* Parsing context */
149089 Vdbe *v, /* Prepared statement under construction */
149090 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
149091 int iLevel, /* Which level of pWInfo->a[] should be coded */
149092 WhereLevel *pLevel, /* The current level pointer */
149093 Bitmask notReady /* Which tables are currently available */
149094);
149095SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) void sqlite3WhereRightJoinLoop(
149096 WhereInfo *pWInfo,
149097 int iLevel,
149098 WhereLevel *pLevel
149099);
149100
149101/* whereexpr.c: */
149102SQLITE_PRIVATEstatic void sqlite3WhereClauseInit(WhereClause*,WhereInfo*);
149103SQLITE_PRIVATEstatic void sqlite3WhereClauseClear(WhereClause*);
149104SQLITE_PRIVATEstatic void sqlite3WhereSplit(WhereClause*,Expr*,u8);
149105SQLITE_PRIVATEstatic void sqlite3WhereAddLimit(WhereClause*, Select*);
149106SQLITE_PRIVATEstatic Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*);
149107SQLITE_PRIVATEstatic Bitmask sqlite3WhereExprUsageNN(WhereMaskSet*, Expr*);
149108SQLITE_PRIVATEstatic Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*);
149109SQLITE_PRIVATEstatic void sqlite3WhereExprAnalyze(SrcList*, WhereClause*);
149110SQLITE_PRIVATEstatic void sqlite3WhereTabFuncArgs(Parse*, SrcItem*, WhereClause*);
149111
149112
149113
149114
149115
149116/*
149117** Bitmasks for the operators on WhereTerm objects. These are all
149118** operators that are of interest to the query planner. An
149119** OR-ed combination of these values can be used when searching for
149120** particular WhereTerms within a WhereClause.
149121**
149122** Value constraints:
149123** WO_EQ == SQLITE_INDEX_CONSTRAINT_EQ
149124** WO_LT == SQLITE_INDEX_CONSTRAINT_LT
149125** WO_LE == SQLITE_INDEX_CONSTRAINT_LE
149126** WO_GT == SQLITE_INDEX_CONSTRAINT_GT
149127** WO_GE == SQLITE_INDEX_CONSTRAINT_GE
149128*/
149129#define WO_IN0x0001 0x0001
149130#define WO_EQ0x0002 0x0002
149131#define WO_LT(0x0002<<(56 -53)) (WO_EQ0x0002<<(TK_LT56-TK_EQ53))
149132#define WO_LE(0x0002<<(55 -53)) (WO_EQ0x0002<<(TK_LE55-TK_EQ53))
149133#define WO_GT(0x0002<<(54 -53)) (WO_EQ0x0002<<(TK_GT54-TK_EQ53))
149134#define WO_GE(0x0002<<(57 -53)) (WO_EQ0x0002<<(TK_GE57-TK_EQ53))
149135#define WO_AUX0x0040 0x0040 /* Op useful to virtual tables only */
149136#define WO_IS0x0080 0x0080
149137#define WO_ISNULL0x0100 0x0100
149138#define WO_OR0x0200 0x0200 /* Two or more OR-connected terms */
149139#define WO_AND0x0400 0x0400 /* Two or more AND-connected terms */
149140#define WO_EQUIV0x0800 0x0800 /* Of the form A==B, both columns */
149141#define WO_NOOP0x1000 0x1000 /* This term does not restrict search space */
149142#define WO_ROWVAL0x2000 0x2000 /* A row-value term */
149143
149144#define WO_ALL0x3fff 0x3fff /* Mask of all possible WO_* values */
149145#define WO_SINGLE0x01ff 0x01ff /* Mask of all non-compound WO_* values */
149146
149147/*
149148** These are definitions of bits in the WhereLoop.wsFlags field.
149149** The particular combination of bits in each WhereLoop help to
149150** determine the algorithm that WhereLoop represents.
149151*/
149152#define WHERE_COLUMN_EQ0x00000001 0x00000001 /* x=EXPR */
149153#define WHERE_COLUMN_RANGE0x00000002 0x00000002 /* x<EXPR and/or x>EXPR */
149154#define WHERE_COLUMN_IN0x00000004 0x00000004 /* x IN (...) */
149155#define WHERE_COLUMN_NULL0x00000008 0x00000008 /* x IS NULL */
149156#define WHERE_CONSTRAINT0x0000000f 0x0000000f /* Any of the WHERE_COLUMN_xxx values */
149157#define WHERE_TOP_LIMIT0x00000010 0x00000010 /* x<EXPR or x<=EXPR constraint */
149158#define WHERE_BTM_LIMIT0x00000020 0x00000020 /* x>EXPR or x>=EXPR constraint */
149159#define WHERE_BOTH_LIMIT0x00000030 0x00000030 /* Both x>EXPR and x<EXPR */
149160#define WHERE_IDX_ONLY0x00000040 0x00000040 /* Use index only - omit table */
149161#define WHERE_IPK0x00000100 0x00000100 /* x is the INTEGER PRIMARY KEY */
149162#define WHERE_INDEXED0x00000200 0x00000200 /* WhereLoop.u.btree.pIndex is valid */
149163#define WHERE_VIRTUALTABLE0x00000400 0x00000400 /* WhereLoop.u.vtab is valid */
149164#define WHERE_IN_ABLE0x00000800 0x00000800 /* Able to support an IN operator */
149165#define WHERE_ONEROW0x00001000 0x00001000 /* Selects no more than one row */
149166#define WHERE_MULTI_OR0x00002000 0x00002000 /* OR using multiple indices */
149167#define WHERE_AUTO_INDEX0x00004000 0x00004000 /* Uses an ephemeral index */
149168#define WHERE_SKIPSCAN0x00008000 0x00008000 /* Uses the skip-scan algorithm */
149169#define WHERE_UNQ_WANTED0x00010000 0x00010000 /* WHERE_ONEROW would have been helpful*/
149170#define WHERE_PARTIALIDX0x00020000 0x00020000 /* The automatic index is partial */
149171#define WHERE_IN_EARLYOUT0x00040000 0x00040000 /* Perhaps quit IN loops early */
149172#define WHERE_BIGNULL_SORT0x00080000 0x00080000 /* Column nEq of index is BIGNULL */
149173#define WHERE_IN_SEEKSCAN0x00100000 0x00100000 /* Seek-scan optimization for IN */
149174#define WHERE_TRANSCONS0x00200000 0x00200000 /* Uses a transitive constraint */
149175#define WHERE_BLOOMFILTER0x00400000 0x00400000 /* Consider using a Bloom-filter */
149176#define WHERE_SELFCULL0x00800000 0x00800000 /* nOut reduced by extra WHERE terms */
149177#define WHERE_OMIT_OFFSET0x01000000 0x01000000 /* Set offset counter to zero */
149178
149179#endif /* !defined(SQLITE_WHEREINT_H) */
149180
149181/************** End of whereInt.h ********************************************/
149182/************** Continuing where we left off in wherecode.c ******************/
149183
149184#ifndef SQLITE_OMIT_EXPLAIN
149185
149186/*
149187** Return the name of the i-th column of the pIdx index.
149188*/
149189static const char *explainIndexColumnName(Index *pIdx, int i){
149190 i = pIdx->aiColumn[i];
149191 if( i==XN_EXPR(-2) ) return "<expr>";
149192 if( i==XN_ROWID(-1) ) return "rowid";
149193 return pIdx->pTable->aCol[i].zCnName;
149194}
149195
149196/*
149197** This routine is a helper for explainIndexRange() below
149198**
149199** pStr holds the text of an expression that we are building up one term
149200** at a time. This routine adds a new term to the end of the expression.
149201** Terms are separated by AND so add the "AND" text for second and subsequent
149202** terms only.
149203*/
149204static void explainAppendTerm(
149205 StrAccum *pStr, /* The text expression being built */
149206 Index *pIdx, /* Index to read column names from */
149207 int nTerm, /* Number of terms */
149208 int iTerm, /* Zero-based index of first term. */
149209 int bAnd, /* Non-zero to append " AND " */
149210 const char *zOp /* Name of the operator */
149211){
149212 int i;
149213
149214 assert( nTerm>=1 )((void) (0));
149215 if( bAnd ) sqlite3_str_append(pStr, " AND ", 5);
149216
149217 if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1);
149218 for(i=0; i<nTerm; i++){
149219 if( i ) sqlite3_str_append(pStr, ",", 1);
149220 sqlite3_str_appendall(pStr, explainIndexColumnName(pIdx, iTerm+i));
149221 }
149222 if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1);
149223
149224 sqlite3_str_append(pStr, zOp, 1);
149225
149226 if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1);
149227 for(i=0; i<nTerm; i++){
149228 if( i ) sqlite3_str_append(pStr, ",", 1);
149229 sqlite3_str_append(pStr, "?", 1);
149230 }
149231 if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1);
149232}
149233
149234/*
149235** Argument pLevel describes a strategy for scanning table pTab. This
149236** function appends text to pStr that describes the subset of table
149237** rows scanned by the strategy in the form of an SQL expression.
149238**
149239** For example, if the query:
149240**
149241** SELECT * FROM t1 WHERE a=1 AND b>2;
149242**
149243** is run and there is an index on (a, b), then this function returns a
149244** string similar to:
149245**
149246** "a=? AND b>?"
149247*/
149248static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop){
149249 Index *pIndex = pLoop->u.btree.pIndex;
149250 u16 nEq = pLoop->u.btree.nEq;
149251 u16 nSkip = pLoop->nSkip;
149252 int i, j;
149253
149254 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT0x00000020|WHERE_TOP_LIMIT0x00000010))==0 ) return;
149255 sqlite3_str_append(pStr, " (", 2);
149256 for(i=0; i<nEq; i++){
149257 const char *z = explainIndexColumnName(pIndex, i);
149258 if( i ) sqlite3_str_append(pStr, " AND ", 5);
149259 sqlite3_str_appendf(pStr, i>=nSkip ? "%s=?" : "ANY(%s)", z);
149260 }
149261
149262 j = i;
149263 if( pLoop->wsFlags&WHERE_BTM_LIMIT0x00000020 ){
149264 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">");
149265 i = 1;
149266 }
149267 if( pLoop->wsFlags&WHERE_TOP_LIMIT0x00000010 ){
149268 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<");
149269 }
149270 sqlite3_str_append(pStr, ")", 1);
149271}
149272
149273/*
149274** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
149275** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was
149276** defined at compile-time. If it is not a no-op, a single OP_Explain opcode
149277** is added to the output to describe the table scan strategy in pLevel.
149278**
149279** If an OP_Explain opcode is added to the VM, its address is returned.
149280** Otherwise, if no OP_Explain is coded, zero is returned.
149281*/
149282SQLITE_PRIVATEstatic int sqlite3WhereExplainOneScan(
149283 Parse *pParse, /* Parse context */
149284 SrcList *pTabList, /* Table list this loop refers to */
149285 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
149286 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
149287){
149288 int ret = 0;
149289#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS1)
149290 if( sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse))->explain==2 )
149291#endif
149292 {
149293 SrcItem *pItem = &pTabList->a[pLevel->iFrom];
149294 Vdbe *v = pParse->pVdbe; /* VM being constructed */
149295 sqlite3 *db = pParse->db; /* Database handle */
149296 int isSearch; /* True for a SEARCH. False for SCAN. */
149297 WhereLoop *pLoop; /* The controlling WhereLoop object */
149298 u32 flags; /* Flags that describe this loop */
149299 char *zMsg; /* Text to add to EQP output */
149300 StrAccum str; /* EQP output string */
149301 char zBuf[100]; /* Initial space for EQP output string */
149302
149303 pLoop = pLevel->pWLoop;
149304 flags = pLoop->wsFlags;
149305 if( (flags&WHERE_MULTI_OR0x00002000) || (wctrlFlags&WHERE_OR_SUBCLAUSE0x0020) ) return 0;
149306
149307 isSearch = (flags&(WHERE_BTM_LIMIT0x00000020|WHERE_TOP_LIMIT0x00000010))!=0
149308 || ((flags&WHERE_VIRTUALTABLE0x00000400)==0 && (pLoop->u.btree.nEq>0))
149309 || (wctrlFlags&(WHERE_ORDERBY_MIN0x0001|WHERE_ORDERBY_MAX0x0002));
149310
149311 sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH2147483645);
149312 str.printfFlags = SQLITE_PRINTF_INTERNAL0x01;
149313 sqlite3_str_appendf(&str, "%s %S", isSearch ? "SEARCH" : "SCAN", pItem);
149314 if( (flags & (WHERE_IPK0x00000100|WHERE_VIRTUALTABLE0x00000400))==0 ){
149315 const char *zFmt = 0;
149316 Index *pIdx;
149317
149318 assert( pLoop->u.btree.pIndex!=0 )((void) (0));
149319 pIdx = pLoop->u.btree.pIndex;
149320 assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) )((void) (0));
149321 if( !HasRowid(pItem->pTab)(((pItem->pTab)->tabFlags & 0x00000080)==0) && IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ){
149322 if( isSearch ){
149323 zFmt = "PRIMARY KEY";
149324 }
149325 }else if( flags & WHERE_PARTIALIDX0x00020000 ){
149326 zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
149327 }else if( flags & WHERE_AUTO_INDEX0x00004000 ){
149328 zFmt = "AUTOMATIC COVERING INDEX";
149329 }else if( flags & WHERE_IDX_ONLY0x00000040 ){
149330 zFmt = "COVERING INDEX %s";
149331 }else{
149332 zFmt = "INDEX %s";
149333 }
149334 if( zFmt ){
149335 sqlite3_str_append(&str, " USING ", 7);
149336 sqlite3_str_appendf(&str, zFmt, pIdx->zName);
149337 explainIndexRange(&str, pLoop);
149338 }
149339 }else if( (flags & WHERE_IPK0x00000100)!=0 && (flags & WHERE_CONSTRAINT0x0000000f)!=0 ){
149340 char cRangeOp;
149341#if 0 /* Better output, but breaks many tests */
149342 const Table *pTab = pItem->pTab;
149343 const char *zRowid = pTab->iPKey>=0 ? pTab->aCol[pTab->iPKey].zCnName:
149344 "rowid";
149345#else
149346 const char *zRowid = "rowid";
149347#endif
149348 sqlite3_str_appendf(&str, " USING INTEGER PRIMARY KEY (%s", zRowid);
149349 if( flags&(WHERE_COLUMN_EQ0x00000001|WHERE_COLUMN_IN0x00000004) ){
149350 cRangeOp = '=';
149351 }else if( (flags&WHERE_BOTH_LIMIT0x00000030)==WHERE_BOTH_LIMIT0x00000030 ){
149352 sqlite3_str_appendf(&str, ">? AND %s", zRowid);
149353 cRangeOp = '<';
149354 }else if( flags&WHERE_BTM_LIMIT0x00000020 ){
149355 cRangeOp = '>';
149356 }else{
149357 assert( flags&WHERE_TOP_LIMIT)((void) (0));
149358 cRangeOp = '<';
149359 }
149360 sqlite3_str_appendf(&str, "%c?)", cRangeOp);
149361 }
149362#ifndef SQLITE_OMIT_VIRTUALTABLE
149363 else if( (flags & WHERE_VIRTUALTABLE0x00000400)!=0 ){
149364 sqlite3_str_appendf(&str, " VIRTUAL TABLE INDEX %d:%s",
149365 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
149366 }
149367#endif
149368 if( pItem->fg.jointype & JT_LEFT0x08 ){
149369 sqlite3_str_appendf(&str, " LEFT-JOIN");
149370 }
149371#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
149372 if( pLoop->nOut>=10 ){
149373 sqlite3_str_appendf(&str, " (~%llu rows)",
149374 sqlite3LogEstToInt(pLoop->nOut));
149375 }else{
149376 sqlite3_str_append(&str, " (~1 row)", 9);
149377 }
149378#endif
149379 zMsg = sqlite3StrAccumFinish(&str);
149380 sqlite3ExplainBreakpoint("",zMsg);
149381 ret = sqlite3VdbeAddOp4(v, OP_Explain185, sqlite3VdbeCurrentAddr(v),
149382 pParse->addrExplain, 0, zMsg,P4_DYNAMIC(-6));
149383 }
149384 return ret;
149385}
149386
149387/*
149388** Add a single OP_Explain opcode that describes a Bloom filter.
149389**
149390** Or if not processing EXPLAIN QUERY PLAN and not in a SQLITE_DEBUG and/or
149391** SQLITE_ENABLE_STMT_SCANSTATUS build, then OP_Explain opcodes are not
149392** required and this routine is a no-op.
149393**
149394** If an OP_Explain opcode is added to the VM, its address is returned.
149395** Otherwise, if no OP_Explain is coded, zero is returned.
149396*/
149397SQLITE_PRIVATEstatic int sqlite3WhereExplainBloomFilter(
149398 const Parse *pParse, /* Parse context */
149399 const WhereInfo *pWInfo, /* WHERE clause */
149400 const WhereLevel *pLevel /* Bloom filter on this level */
149401){
149402 int ret = 0;
149403 SrcItem *pItem = &pWInfo->pTabList->a[pLevel->iFrom];
149404 Vdbe *v = pParse->pVdbe; /* VM being constructed */
149405 sqlite3 *db = pParse->db; /* Database handle */
149406 char *zMsg; /* Text to add to EQP output */
149407 int i; /* Loop counter */
149408 WhereLoop *pLoop; /* The where loop */
149409 StrAccum str; /* EQP output string */
149410 char zBuf[100]; /* Initial space for EQP output string */
149411
149412 sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH2147483645);
149413 str.printfFlags = SQLITE_PRINTF_INTERNAL0x01;
149414 sqlite3_str_appendf(&str, "BLOOM FILTER ON %S (", pItem);
149415 pLoop = pLevel->pWLoop;
149416 if( pLoop->wsFlags & WHERE_IPK0x00000100 ){
149417 const Table *pTab = pItem->pTab;
149418 if( pTab->iPKey>=0 ){
149419 sqlite3_str_appendf(&str, "%s=?", pTab->aCol[pTab->iPKey].zCnName);
149420 }else{
149421 sqlite3_str_appendf(&str, "rowid=?");
149422 }
149423 }else{
149424 for(i=pLoop->nSkip; i<pLoop->u.btree.nEq; i++){
149425 const char *z = explainIndexColumnName(pLoop->u.btree.pIndex, i);
149426 if( i>pLoop->nSkip ) sqlite3_str_append(&str, " AND ", 5);
149427 sqlite3_str_appendf(&str, "%s=?", z);
149428 }
149429 }
149430 sqlite3_str_append(&str, ")", 1);
149431 zMsg = sqlite3StrAccumFinish(&str);
149432 ret = sqlite3VdbeAddOp4(v, OP_Explain185, sqlite3VdbeCurrentAddr(v),
149433 pParse->addrExplain, 0, zMsg,P4_DYNAMIC(-6));
149434 return ret;
149435}
149436#endif /* SQLITE_OMIT_EXPLAIN */
149437
149438#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
149439/*
149440** Configure the VM passed as the first argument with an
149441** sqlite3_stmt_scanstatus() entry corresponding to the scan used to
149442** implement level pLvl. Argument pSrclist is a pointer to the FROM
149443** clause that the scan reads data from.
149444**
149445** If argument addrExplain is not 0, it must be the address of an
149446** OP_Explain instruction that describes the same loop.
149447*/
149448SQLITE_PRIVATEstatic void sqlite3WhereAddScanStatus(
149449 Vdbe *v, /* Vdbe to add scanstatus entry to */
149450 SrcList *pSrclist, /* FROM clause pLvl reads data from */
149451 WhereLevel *pLvl, /* Level to add scanstatus() entry for */
149452 int addrExplain /* Address of OP_Explain (or 0) */
149453){
149454 const char *zObj = 0;
149455 WhereLoop *pLoop = pLvl->pWLoop;
149456 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE0x00000400)==0 && pLoop->u.btree.pIndex!=0 ){
149457 zObj = pLoop->u.btree.pIndex->zName;
149458 }else{
149459 zObj = pSrclist->a[pLvl->iFrom].zName;
149460 }
149461 sqlite3VdbeScanStatus(
149462 v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
149463 );
149464}
149465#endif
149466
149467
149468/*
149469** Disable a term in the WHERE clause. Except, do not disable the term
149470** if it controls a LEFT OUTER JOIN and it did not originate in the ON
149471** or USING clause of that join.
149472**
149473** Consider the term t2.z='ok' in the following queries:
149474**
149475** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
149476** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
149477** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
149478**
149479** The t2.z='ok' is disabled in the in (2) because it originates
149480** in the ON clause. The term is disabled in (3) because it is not part
149481** of a LEFT OUTER JOIN. In (1), the term is not disabled.
149482**
149483** Disabling a term causes that term to not be tested in the inner loop
149484** of the join. Disabling is an optimization. When terms are satisfied
149485** by indices, we disable them to prevent redundant tests in the inner
149486** loop. We would get the correct results if nothing were ever disabled,
149487** but joins might run a little slower. The trick is to disable as much
149488** as we can without disabling too much. If we disabled in (1), we'd get
149489** the wrong answer. See ticket #813.
149490**
149491** If all the children of a term are disabled, then that term is also
149492** automatically disabled. In this way, terms get disabled if derived
149493** virtual terms are tested first. For example:
149494**
149495** x GLOB 'abc*' AND x>='abc' AND x<'acd'
149496** \___________/ \______/ \_____/
149497** parent child1 child2
149498**
149499** Only the parent term was in the original WHERE clause. The child1
149500** and child2 terms were added by the LIKE optimization. If both of
149501** the virtual child terms are valid, then testing of the parent can be
149502** skipped.
149503**
149504** Usually the parent term is marked as TERM_CODED. But if the parent
149505** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
149506** The TERM_LIKECOND marking indicates that the term should be coded inside
149507** a conditional such that is only evaluated on the second pass of a
149508** LIKE-optimization loop, when scanning BLOBs instead of strings.
149509*/
149510static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
149511 int nLoop = 0;
149512 assert( pTerm!=0 )((void) (0));
149513 while( (pTerm->wtFlags & TERM_CODED0x0004)==0
149514 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_OuterON)(((pTerm->pExpr)->flags&(0x000001))!=0))
149515 && (pLevel->notReady & pTerm->prereqAll)==0
149516 ){
149517 if( nLoop && (pTerm->wtFlags & TERM_LIKE0x0400)!=0 ){
149518 pTerm->wtFlags |= TERM_LIKECOND0x0200;
149519 }else{
149520 pTerm->wtFlags |= TERM_CODED0x0004;
149521 }
149522#ifdef WHERETRACE_ENABLED
149523 if( sqlite3WhereTrace & 0x20000 ){
149524 sqlite3DebugPrintf("DISABLE-");
149525 sqlite3WhereTermPrint(pTerm, (int)(pTerm - (pTerm->pWC->a)));
149526 }
149527#endif
149528 if( pTerm->iParent<0 ) break;
149529 pTerm = &pTerm->pWC->a[pTerm->iParent];
149530 assert( pTerm!=0 )((void) (0));
149531 pTerm->nChild--;
149532 if( pTerm->nChild!=0 ) break;
149533 nLoop++;
149534 }
149535}
149536
149537/*
149538** Code an OP_Affinity opcode to apply the column affinity string zAff
149539** to the n registers starting at base.
149540**
149541** As an optimization, SQLITE_AFF_BLOB and SQLITE_AFF_NONE entries (which
149542** are no-ops) at the beginning and end of zAff are ignored. If all entries
149543** in zAff are SQLITE_AFF_BLOB or SQLITE_AFF_NONE, then no code gets generated.
149544**
149545** This routine makes its own copy of zAff so that the caller is free
149546** to modify zAff after this routine returns.
149547*/
149548static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
149549 Vdbe *v = pParse->pVdbe;
149550 if( zAff==0 ){
149551 assert( pParse->db->mallocFailed )((void) (0));
149552 return;
149553 }
149554 assert( v!=0 )((void) (0));
149555
149556 /* Adjust base and n to skip over SQLITE_AFF_BLOB and SQLITE_AFF_NONE
149557 ** entries at the beginning and end of the affinity string.
149558 */
149559 assert( SQLITE_AFF_NONE<SQLITE_AFF_BLOB )((void) (0));
149560 while( n>0 && zAff[0]<=SQLITE_AFF_BLOB0x41 ){
149561 n--;
149562 base++;
149563 zAff++;
149564 }
149565 while( n>1 && zAff[n-1]<=SQLITE_AFF_BLOB0x41 ){
149566 n--;
149567 }
149568
149569 /* Code the OP_Affinity opcode if there is anything left to do. */
149570 if( n>0 ){
149571 sqlite3VdbeAddOp4(v, OP_Affinity96, base, n, 0, zAff, n);
149572 }
149573}
149574
149575/*
149576** Expression pRight, which is the RHS of a comparison operation, is
149577** either a vector of n elements or, if n==1, a scalar expression.
149578** Before the comparison operation, affinity zAff is to be applied
149579** to the pRight values. This function modifies characters within the
149580** affinity string to SQLITE_AFF_BLOB if either:
149581**
149582** * the comparison will be performed with no affinity, or
149583** * the affinity change in zAff is guaranteed not to change the value.
149584*/
149585static void updateRangeAffinityStr(
149586 Expr *pRight, /* RHS of comparison */
149587 int n, /* Number of vector elements in comparison */
149588 char *zAff /* Affinity string to modify */
149589){
149590 int i;
149591 for(i=0; i<n; i++){
149592 Expr *p = sqlite3VectorFieldSubexpr(pRight, i);
149593 if( sqlite3CompareAffinity(p, zAff[i])==SQLITE_AFF_BLOB0x41
149594 || sqlite3ExprNeedsNoAffinityChange(p, zAff[i])
149595 ){
149596 zAff[i] = SQLITE_AFF_BLOB0x41;
149597 }
149598 }
149599}
149600
149601
149602/*
149603** pX is an expression of the form: (vector) IN (SELECT ...)
149604** In other words, it is a vector IN operator with a SELECT clause on the
149605** LHS. But not all terms in the vector are indexable and the terms might
149606** not be in the correct order for indexing.
149607**
149608** This routine makes a copy of the input pX expression and then adjusts
149609** the vector on the LHS with corresponding changes to the SELECT so that
149610** the vector contains only index terms and those terms are in the correct
149611** order. The modified IN expression is returned. The caller is responsible
149612** for deleting the returned expression.
149613**
149614** Example:
149615**
149616** CREATE TABLE t1(a,b,c,d,e,f);
149617** CREATE INDEX t1x1 ON t1(e,c);
149618** SELECT * FROM t1 WHERE (a,b,c,d,e) IN (SELECT v,w,x,y,z FROM t2)
149619** \_______________________________________/
149620** The pX expression
149621**
149622** Since only columns e and c can be used with the index, in that order,
149623** the modified IN expression that is returned will be:
149624**
149625** (e,c) IN (SELECT z,x FROM t2)
149626**
149627** The reduced pX is different from the original (obviously) and thus is
149628** only used for indexing, to improve performance. The original unaltered
149629** IN expression must also be run on each output row for correctness.
149630*/
149631static Expr *removeUnindexableInClauseTerms(
149632 Parse *pParse, /* The parsing context */
149633 int iEq, /* Look at loop terms starting here */
149634 WhereLoop *pLoop, /* The current loop */
149635 Expr *pX /* The IN expression to be reduced */
149636){
149637 sqlite3 *db = pParse->db;
149638 Expr *pNew;
149639 pNew = sqlite3ExprDup(db, pX, 0);
149640 if( db->mallocFailed==0 ){
149641 ExprList *pOrigRhs; /* Original unmodified RHS */
149642 ExprList *pOrigLhs; /* Original unmodified LHS */
149643 ExprList *pRhs = 0; /* New RHS after modifications */
149644 ExprList *pLhs = 0; /* New LHS after mods */
149645 int i; /* Loop counter */
149646 Select *pSelect; /* Pointer to the SELECT on the RHS */
149647
149648 assert( ExprUseXSelect(pNew) )((void) (0));
149649 pOrigRhs = pNew->x.pSelect->pEList;
149650 assert( pNew->pLeft!=0 )((void) (0));
149651 assert( ExprUseXList(pNew->pLeft) )((void) (0));
149652 pOrigLhs = pNew->pLeft->x.pList;
149653 for(i=iEq; i<pLoop->nLTerm; i++){
149654 if( pLoop->aLTerm[i]->pExpr==pX ){
149655 int iField;
149656 assert( (pLoop->aLTerm[i]->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
149657 iField = pLoop->aLTerm[i]->u.x.iField - 1;
149658 if( pOrigRhs->a[iField].pExpr==0 ) continue; /* Duplicate PK column */
149659 pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr);
149660 pOrigRhs->a[iField].pExpr = 0;
149661 assert( pOrigLhs->a[iField].pExpr!=0 )((void) (0));
149662 pLhs = sqlite3ExprListAppend(pParse, pLhs, pOrigLhs->a[iField].pExpr);
149663 pOrigLhs->a[iField].pExpr = 0;
149664 }
149665 }
149666 sqlite3ExprListDelete(db, pOrigRhs);
149667 sqlite3ExprListDelete(db, pOrigLhs);
149668 pNew->pLeft->x.pList = pLhs;
149669 pNew->x.pSelect->pEList = pRhs;
149670 if( pLhs && pLhs->nExpr==1 ){
149671 /* Take care here not to generate a TK_VECTOR containing only a
149672 ** single value. Since the parser never creates such a vector, some
149673 ** of the subroutines do not handle this case. */
149674 Expr *p = pLhs->a[0].pExpr;
149675 pLhs->a[0].pExpr = 0;
149676 sqlite3ExprDelete(db, pNew->pLeft);
149677 pNew->pLeft = p;
149678 }
149679 pSelect = pNew->x.pSelect;
149680 if( pSelect->pOrderBy ){
149681 /* If the SELECT statement has an ORDER BY clause, zero the
149682 ** iOrderByCol variables. These are set to non-zero when an
149683 ** ORDER BY term exactly matches one of the terms of the
149684 ** result-set. Since the result-set of the SELECT statement may
149685 ** have been modified or reordered, these variables are no longer
149686 ** set correctly. Since setting them is just an optimization,
149687 ** it's easiest just to zero them here. */
149688 ExprList *pOrderBy = pSelect->pOrderBy;
149689 for(i=0; i<pOrderBy->nExpr; i++){
149690 pOrderBy->a[i].u.x.iOrderByCol = 0;
149691 }
149692 }
149693
149694#if 0
149695 printf("For indexing, change the IN expr:\n");
149696 sqlite3TreeViewExpr(0, pX, 0);
149697 printf("Into:\n");
149698 sqlite3TreeViewExpr(0, pNew, 0);
149699#endif
149700 }
149701 return pNew;
149702}
149703
149704
149705/*
149706** Generate code for a single equality term of the WHERE clause. An equality
149707** term can be either X=expr or X IN (...). pTerm is the term to be
149708** coded.
149709**
149710** The current value for the constraint is left in a register, the index
149711** of which is returned. An attempt is made store the result in iTarget but
149712** this is only guaranteed for TK_ISNULL and TK_IN constraints. If the
149713** constraint is a TK_EQ or TK_IS, then the current value might be left in
149714** some other register and it is the caller's responsibility to compensate.
149715**
149716** For a constraint of the form X=expr, the expression is evaluated in
149717** straight-line code. For constraints of the form X IN (...)
149718** this routine sets up a loop that will iterate over all values of X.
149719*/
149720static int codeEqualityTerm(
149721 Parse *pParse, /* The parsing context */
149722 WhereTerm *pTerm, /* The term of the WHERE clause to be coded */
149723 WhereLevel *pLevel, /* The level of the FROM clause we are working on */
149724 int iEq, /* Index of the equality term within this level */
149725 int bRev, /* True for reverse-order IN operations */
149726 int iTarget /* Attempt to leave results in this register */
149727){
149728 Expr *pX = pTerm->pExpr;
149729 Vdbe *v = pParse->pVdbe;
149730 int iReg; /* Register holding results */
149731
149732 assert( pLevel->pWLoop->aLTerm[iEq]==pTerm )((void) (0));
149733 assert( iTarget>0 )((void) (0));
149734 if( pX->op==TK_EQ53 || pX->op==TK_IS45 ){
149735 iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
149736 }else if( pX->op==TK_ISNULL50 ){
149737 iReg = iTarget;
149738 sqlite3VdbeAddOp2(v, OP_Null75, 0, iReg);
149739#ifndef SQLITE_OMIT_SUBQUERY
149740 }else{
149741 int eType = IN_INDEX_NOOP5;
149742 int iTab;
149743 struct InLoop *pIn;
149744 WhereLoop *pLoop = pLevel->pWLoop;
149745 int i;
149746 int nEq = 0;
149747 int *aiMap = 0;
149748
149749 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE0x00000400)==0
149750 && pLoop->u.btree.pIndex!=0
149751 && pLoop->u.btree.pIndex->aSortOrder[iEq]
149752 ){
149753 testcase( iEq==0 );
149754 testcase( bRev );
149755 bRev = !bRev;
149756 }
149757 assert( pX->op==TK_IN )((void) (0));
149758 iReg = iTarget;
149759
149760 for(i=0; i<iEq; i++){
149761 if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){
149762 disableTerm(pLevel, pTerm);
149763 return iTarget;
149764 }
149765 }
149766 for(i=iEq;i<pLoop->nLTerm; i++){
149767 assert( pLoop->aLTerm[i]!=0 )((void) (0));
149768 if( pLoop->aLTerm[i]->pExpr==pX ) nEq++;
149769 }
149770
149771 iTab = 0;
149772 if( !ExprUseXSelect(pX)(((pX)->flags&0x001000)!=0) || pX->x.pSelect->pEList->nExpr==1 ){
149773 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP0x0004, 0, 0, &iTab);
149774 }else{
149775 Expr *pExpr = pTerm->pExpr;
149776 if( pExpr->iTable==0 || !ExprHasProperty(pExpr, EP_Subrtn)(((pExpr)->flags&(0x2000000))!=0) ){
149777 sqlite3 *db = pParse->db;
149778 pX = removeUnindexableInClauseTerms(pParse, iEq, pLoop, pX);
149779 if( !db->mallocFailed ){
149780 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
149781 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP0x0004, 0, aiMap,&iTab);
149782 pExpr->iTable = iTab;
149783 }
149784 sqlite3ExprDelete(db, pX);
149785 }else{
149786 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
149787 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP0x0004, 0, aiMap, &iTab);
149788 }
149789 pX = pExpr;
149790 }
149791
149792 if( eType==IN_INDEX_INDEX_DESC4 ){
149793 testcase( bRev );
149794 bRev = !bRev;
149795 }
149796 sqlite3VdbeAddOp2(v, bRev ? OP_Last31 : OP_Rewind35, iTab, 0);
149797 VdbeCoverageIf(v, bRev);
149798 VdbeCoverageIf(v, !bRev);
149799
149800 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 )((void) (0));
149801 pLoop->wsFlags |= WHERE_IN_ABLE0x00000800;
149802 if( pLevel->u.in.nIn==0 ){
149803 pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
149804 }
149805 if( iEq>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN0x00100000)==0 ){
149806 pLoop->wsFlags |= WHERE_IN_EARLYOUT0x00040000;
149807 }
149808
149809 i = pLevel->u.in.nIn;
149810 pLevel->u.in.nIn += nEq;
149811 pLevel->u.in.aInLoop =
149812 sqlite3WhereRealloc(pTerm->pWC->pWInfo,
149813 pLevel->u.in.aInLoop,
149814 sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
149815 pIn = pLevel->u.in.aInLoop;
149816 if( pIn ){
149817 int iMap = 0; /* Index in aiMap[] */
149818 pIn += i;
149819 for(i=iEq;i<pLoop->nLTerm; i++){
149820 if( pLoop->aLTerm[i]->pExpr==pX ){
149821 int iOut = iReg + i - iEq;
149822 if( eType==IN_INDEX_ROWID1 ){
149823 pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid135, iTab, iOut);
149824 }else{
149825 int iCol = aiMap ? aiMap[iMap++] : 0;
149826 pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column94,iTab, iCol, iOut);
149827 }
149828 sqlite3VdbeAddOp1(v, OP_IsNull50, iOut); VdbeCoverage(v);
149829 if( i==iEq ){
149830 pIn->iCur = iTab;
149831 pIn->eEndLoopOp = bRev ? OP_Prev37 : OP_Next38;
149832 if( iEq>0 ){
149833 pIn->iBase = iReg - i;
149834 pIn->nPrefix = i;
149835 }else{
149836 pIn->nPrefix = 0;
149837 }
149838 }else{
149839 pIn->eEndLoopOp = OP_Noop184;
149840 }
149841 pIn++;
149842 }
149843 }
149844 testcase( iEq>0
149845 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0
149846 && (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 );
149847 if( iEq>0
149848 && (pLoop->wsFlags & (WHERE_IN_SEEKSCAN0x00100000|WHERE_VIRTUALTABLE0x00000400))==0
149849 ){
149850 sqlite3VdbeAddOp3(v, OP_SeekHit125, pLevel->iIdxCur, 0, iEq);
149851 }
149852 }else{
149853 pLevel->u.in.nIn = 0;
149854 }
149855 sqlite3DbFree(pParse->db, aiMap);
149856#endif
149857 }
149858
149859 /* As an optimization, try to disable the WHERE clause term that is
149860 ** driving the index as it will always be true. The correct answer is
149861 ** obtained regardless, but we might get the answer with fewer CPU cycles
149862 ** by omitting the term.
149863 **
149864 ** But do not disable the term unless we are certain that the term is
149865 ** not a transitive constraint. For an example of where that does not
149866 ** work, see https://sqlite.org/forum/forumpost/eb8613976a (2021-05-04)
149867 */
149868 if( (pLevel->pWLoop->wsFlags & WHERE_TRANSCONS0x00200000)==0
149869 || (pTerm->eOperator & WO_EQUIV0x0800)==0
149870 ){
149871 disableTerm(pLevel, pTerm);
149872 }
149873
149874 return iReg;
149875}
149876
149877/*
149878** Generate code that will evaluate all == and IN constraints for an
149879** index scan.
149880**
149881** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
149882** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10
149883** The index has as many as three equality constraints, but in this
149884** example, the third "c" value is an inequality. So only two
149885** constraints are coded. This routine will generate code to evaluate
149886** a==5 and b IN (1,2,3). The current values for a and b will be stored
149887** in consecutive registers and the index of the first register is returned.
149888**
149889** In the example above nEq==2. But this subroutine works for any value
149890** of nEq including 0. If nEq==0, this routine is nearly a no-op.
149891** The only thing it does is allocate the pLevel->iMem memory cell and
149892** compute the affinity string.
149893**
149894** The nExtraReg parameter is 0 or 1. It is 0 if all WHERE clause constraints
149895** are == or IN and are covered by the nEq. nExtraReg is 1 if there is
149896** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
149897** occurs after the nEq quality constraints.
149898**
149899** This routine allocates a range of nEq+nExtraReg memory cells and returns
149900** the index of the first memory cell in that range. The code that
149901** calls this routine will use that memory range to store keys for
149902** start and termination conditions of the loop.
149903** key value of the loop. If one or more IN operators appear, then
149904** this routine allocates an additional nEq memory cells for internal
149905** use.
149906**
149907** Before returning, *pzAff is set to point to a buffer containing a
149908** copy of the column affinity string of the index allocated using
149909** sqlite3DbMalloc(). Except, entries in the copy of the string associated
149910** with equality constraints that use BLOB or NONE affinity are set to
149911** SQLITE_AFF_BLOB. This is to deal with SQL such as the following:
149912**
149913** CREATE TABLE t1(a TEXT PRIMARY KEY, b);
149914** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
149915**
149916** In the example above, the index on t1(a) has TEXT affinity. But since
149917** the right hand side of the equality constraint (t2.b) has BLOB/NONE affinity,
149918** no conversion should be attempted before using a t2.b value as part of
149919** a key to search the index. Hence the first byte in the returned affinity
149920** string in this example would be set to SQLITE_AFF_BLOB.
149921*/
149922static int codeAllEqualityTerms(
149923 Parse *pParse, /* Parsing context */
149924 WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */
149925 int bRev, /* Reverse the order of IN operators */
149926 int nExtraReg, /* Number of extra registers to allocate */
149927 char **pzAff /* OUT: Set to point to affinity string */
149928){
149929 u16 nEq; /* The number of == or IN constraints to code */
149930 u16 nSkip; /* Number of left-most columns to skip */
149931 Vdbe *v = pParse->pVdbe; /* The vm under construction */
149932 Index *pIdx; /* The index being used for this loop */
149933 WhereTerm *pTerm; /* A single constraint term */
149934 WhereLoop *pLoop; /* The WhereLoop object */
149935 int j; /* Loop counter */
149936 int regBase; /* Base register */
149937 int nReg; /* Number of registers to allocate */
149938 char *zAff; /* Affinity string to return */
149939
149940 /* This module is only called on query plans that use an index. */
149941 pLoop = pLevel->pWLoop;
149942 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 )((void) (0));
149943 nEq = pLoop->u.btree.nEq;
149944 nSkip = pLoop->nSkip;
149945 pIdx = pLoop->u.btree.pIndex;
149946 assert( pIdx!=0 )((void) (0));
149947
149948 /* Figure out how many memory cells we will need then allocate them.
149949 */
149950 regBase = pParse->nMem + 1;
149951 nReg = pLoop->u.btree.nEq + nExtraReg;
149952 pParse->nMem += nReg;
149953
149954 zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx));
149955 assert( zAff!=0 || pParse->db->mallocFailed )((void) (0));
149956
149957 if( nSkip ){
149958 int iIdxCur = pLevel->iIdxCur;
149959 sqlite3VdbeAddOp3(v, OP_Null75, 0, regBase, regBase+nSkip-1);
149960 sqlite3VdbeAddOp1(v, (bRev?OP_Last31:OP_Rewind35), iIdxCur);
149961 VdbeCoverageIf(v, bRev==0);
149962 VdbeCoverageIf(v, bRev!=0);
149963 VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
149964 j = sqlite3VdbeAddOp0(v, OP_Goto8);
149965 assert( pLevel->addrSkip==0 )((void) (0));
149966 pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT20:OP_SeekGT23),
149967 iIdxCur, 0, regBase, nSkip);
149968 VdbeCoverageIf(v, bRev==0);
149969 VdbeCoverageIf(v, bRev!=0);
149970 sqlite3VdbeJumpHere(v, j);
149971 for(j=0; j<nSkip; j++){
149972 sqlite3VdbeAddOp3(v, OP_Column94, iIdxCur, j, regBase+j);
149973 testcase( pIdx->aiColumn[j]==XN_EXPR );
149974 VdbeComment((v, "%s", explainIndexColumnName(pIdx, j)));
149975 }
149976 }
149977
149978 /* Evaluate the equality constraints
149979 */
149980 assert( zAff==0 || (int)strlen(zAff)>=nEq )((void) (0));
149981 for(j=nSkip; j<nEq; j++){
149982 int r1;
149983 pTerm = pLoop->aLTerm[j];
149984 assert( pTerm!=0 )((void) (0));
149985 /* The following testcase is true for indices with redundant columns.
149986 ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
149987 testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
149988 testcase( pTerm->wtFlags & TERM_VIRTUAL );
149989 r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
149990 if( r1!=regBase+j ){
149991 if( nReg==1 ){
149992 sqlite3ReleaseTempReg(pParse, regBase);
149993 regBase = r1;
149994 }else{
149995 sqlite3VdbeAddOp2(v, OP_Copy80, r1, regBase+j);
149996 }
149997 }
149998 }
149999 for(j=nSkip; j<nEq; j++){
150000 pTerm = pLoop->aLTerm[j];
150001 if( pTerm->eOperator & WO_IN0x0001 ){
150002 if( pTerm->pExpr->flags & EP_xIsSelect0x001000 ){
150003 /* No affinity ever needs to be (or should be) applied to a value
150004 ** from the RHS of an "? IN (SELECT ...)" expression. The
150005 ** sqlite3FindInIndex() routine has already ensured that the
150006 ** affinity of the comparison has been applied to the value. */
150007 if( zAff ) zAff[j] = SQLITE_AFF_BLOB0x41;
150008 }
150009 }else if( (pTerm->eOperator & WO_ISNULL0x0100)==0 ){
150010 Expr *pRight = pTerm->pExpr->pRight;
150011 if( (pTerm->wtFlags & TERM_IS0x0800)==0 && sqlite3ExprCanBeNull(pRight) ){
150012 sqlite3VdbeAddOp2(v, OP_IsNull50, regBase+j, pLevel->addrBrk);
150013 VdbeCoverage(v);
150014 }
150015 if( pParse->nErr==0 ){
150016 assert( pParse->db->mallocFailed==0 )((void) (0));
150017 if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB0x41 ){
150018 zAff[j] = SQLITE_AFF_BLOB0x41;
150019 }
150020 if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
150021 zAff[j] = SQLITE_AFF_BLOB0x41;
150022 }
150023 }
150024 }
150025 }
150026 *pzAff = zAff;
150027 return regBase;
150028}
150029
150030#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
150031/*
150032** If the most recently coded instruction is a constant range constraint
150033** (a string literal) that originated from the LIKE optimization, then
150034** set P3 and P5 on the OP_String opcode so that the string will be cast
150035** to a BLOB at appropriate times.
150036**
150037** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
150038** expression: "x>='ABC' AND x<'abd'". But this requires that the range
150039** scan loop run twice, once for strings and a second time for BLOBs.
150040** The OP_String opcodes on the second pass convert the upper and lower
150041** bound string constants to blobs. This routine makes the necessary changes
150042** to the OP_String opcodes for that to happen.
150043**
150044** Except, of course, if SQLITE_LIKE_DOESNT_MATCH_BLOBS is defined, then
150045** only the one pass through the string space is required, so this routine
150046** becomes a no-op.
150047*/
150048static void whereLikeOptimizationStringFixup(
150049 Vdbe *v, /* prepared statement under construction */
150050 WhereLevel *pLevel, /* The loop that contains the LIKE operator */
150051 WhereTerm *pTerm /* The upper or lower bound just coded */
150052){
150053 if( pTerm->wtFlags & TERM_LIKEOPT0x0100 ){
150054 VdbeOp *pOp;
150055 assert( pLevel->iLikeRepCntr>0 )((void) (0));
150056 pOp = sqlite3VdbeGetOp(v, -1);
150057 assert( pOp!=0 )((void) (0));
150058 assert( pOp->opcode==OP_String8((void) (0))
150059 || pTerm->pWC->pWInfo->pParse->db->mallocFailed )((void) (0));
150060 pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */
150061 pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */
150062 }
150063}
150064#else
150065# define whereLikeOptimizationStringFixup(A,B,C)
150066#endif
150067
150068#ifdef SQLITE_ENABLE_CURSOR_HINTS
150069/*
150070** Information is passed from codeCursorHint() down to individual nodes of
150071** the expression tree (by sqlite3WalkExpr()) using an instance of this
150072** structure.
150073*/
150074struct CCurHint {
150075 int iTabCur; /* Cursor for the main table */
150076 int iIdxCur; /* Cursor for the index, if pIdx!=0. Unused otherwise */
150077 Index *pIdx; /* The index used to access the table */
150078};
150079
150080/*
150081** This function is called for every node of an expression that is a candidate
150082** for a cursor hint on an index cursor. For TK_COLUMN nodes that reference
150083** the table CCurHint.iTabCur, verify that the same column can be
150084** accessed through the index. If it cannot, then set pWalker->eCode to 1.
150085*/
150086static int codeCursorHintCheckExpr(Walker *pWalker, Expr *pExpr){
150087 struct CCurHint *pHint = pWalker->u.pCCurHint;
150088 assert( pHint->pIdx!=0 )((void) (0));
150089 if( pExpr->op==TK_COLUMN167
150090 && pExpr->iTable==pHint->iTabCur
150091 && sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn)<0
150092 ){
150093 pWalker->eCode = 1;
150094 }
150095 return WRC_Continue0;
150096}
150097
150098/*
150099** Test whether or not expression pExpr, which was part of a WHERE clause,
150100** should be included in the cursor-hint for a table that is on the rhs
150101** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the
150102** expression is not suitable.
150103**
150104** An expression is unsuitable if it might evaluate to non NULL even if
150105** a TK_COLUMN node that does affect the value of the expression is set
150106** to NULL. For example:
150107**
150108** col IS NULL
150109** col IS NOT NULL
150110** coalesce(col, 1)
150111** CASE WHEN col THEN 0 ELSE 1 END
150112*/
150113static int codeCursorHintIsOrFunction(Walker *pWalker, Expr *pExpr){
150114 if( pExpr->op==TK_IS45
150115 || pExpr->op==TK_ISNULL50 || pExpr->op==TK_ISNOT171
150116 || pExpr->op==TK_NOTNULL51 || pExpr->op==TK_CASE157
150117 ){
150118 pWalker->eCode = 1;
150119 }else if( pExpr->op==TK_FUNCTION172 ){
150120 int d1;
150121 char d2[4];
150122 if( 0==sqlite3IsLikeFunction(pWalker->pParse->db, pExpr, &d1, d2) ){
150123 pWalker->eCode = 1;
150124 }
150125 }
150126
150127 return WRC_Continue0;
150128}
150129
150130
150131/*
150132** This function is called on every node of an expression tree used as an
150133** argument to the OP_CursorHint instruction. If the node is a TK_COLUMN
150134** that accesses any table other than the one identified by
150135** CCurHint.iTabCur, then do the following:
150136**
150137** 1) allocate a register and code an OP_Column instruction to read
150138** the specified column into the new register, and
150139**
150140** 2) transform the expression node to a TK_REGISTER node that reads
150141** from the newly populated register.
150142**
150143** Also, if the node is a TK_COLUMN that does access the table idenified
150144** by pCCurHint.iTabCur, and an index is being used (which we will
150145** know because CCurHint.pIdx!=0) then transform the TK_COLUMN into
150146** an access of the index rather than the original table.
150147*/
150148static int codeCursorHintFixExpr(Walker *pWalker, Expr *pExpr){
150149 int rc = WRC_Continue0;
150150 struct CCurHint *pHint = pWalker->u.pCCurHint;
150151 if( pExpr->op==TK_COLUMN167 ){
150152 if( pExpr->iTable!=pHint->iTabCur ){
150153 int reg = ++pWalker->pParse->nMem; /* Register for column value */
150154 sqlite3ExprCode(pWalker->pParse, pExpr, reg);
150155 pExpr->op = TK_REGISTER176;
150156 pExpr->iTable = reg;
150157 }else if( pHint->pIdx!=0 ){
150158 pExpr->iTable = pHint->iIdxCur;
150159 pExpr->iColumn = sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn);
150160 assert( pExpr->iColumn>=0 )((void) (0));
150161 }
150162 }else if( pExpr->op==TK_AGG_FUNCTION168 ){
150163 /* An aggregate function in the WHERE clause of a query means this must
150164 ** be a correlated sub-query, and expression pExpr is an aggregate from
150165 ** the parent context. Do not walk the function arguments in this case.
150166 **
150167 ** todo: It should be possible to replace this node with a TK_REGISTER
150168 ** expression, as the result of the expression must be stored in a
150169 ** register at this point. The same holds for TK_AGG_COLUMN nodes. */
150170 rc = WRC_Prune1;
150171 }
150172 return rc;
150173}
150174
150175/*
150176** Insert an OP_CursorHint instruction if it is appropriate to do so.
150177*/
150178static void codeCursorHint(
150179 SrcItem *pTabItem, /* FROM clause item */
150180 WhereInfo *pWInfo, /* The where clause */
150181 WhereLevel *pLevel, /* Which loop to provide hints for */
150182 WhereTerm *pEndRange /* Hint this end-of-scan boundary term if not NULL */
150183){
150184 Parse *pParse = pWInfo->pParse;
150185 sqlite3 *db = pParse->db;
150186 Vdbe *v = pParse->pVdbe;
150187 Expr *pExpr = 0;
150188 WhereLoop *pLoop = pLevel->pWLoop;
150189 int iCur;
150190 WhereClause *pWC;
150191 WhereTerm *pTerm;
150192 int i, j;
150193 struct CCurHint sHint;
150194 Walker sWalker;
150195
150196 if( OptimizationDisabled(db, SQLITE_CursorHints)(((db)->dbOptFlags&(0x00000400))!=0) ) return;
150197 iCur = pLevel->iTabCur;
150198 assert( iCur==pWInfo->pTabList->a[pLevel->iFrom].iCursor )((void) (0));
150199 sHint.iTabCur = iCur;
150200 sHint.iIdxCur = pLevel->iIdxCur;
150201 sHint.pIdx = pLoop->u.btree.pIndex;
150202 memset(&sWalker, 0, sizeof(sWalker));
150203 sWalker.pParse = pParse;
150204 sWalker.u.pCCurHint = &sHint;
150205 pWC = &pWInfo->sWC;
150206 for(i=0; i<pWC->nBase; i++){
150207 pTerm = &pWC->a[i];
150208 if( pTerm->wtFlags & (TERM_VIRTUAL0x0002|TERM_CODED0x0004) ) continue;
150209 if( pTerm->prereqAll & pLevel->notReady ) continue;
150210
150211 /* Any terms specified as part of the ON(...) clause for any LEFT
150212 ** JOIN for which the current table is not the rhs are omitted
150213 ** from the cursor-hint.
150214 **
150215 ** If this table is the rhs of a LEFT JOIN, "IS" or "IS NULL" terms
150216 ** that were specified as part of the WHERE clause must be excluded.
150217 ** This is to address the following:
150218 **
150219 ** SELECT ... t1 LEFT JOIN t2 ON (t1.a=t2.b) WHERE t2.c IS NULL;
150220 **
150221 ** Say there is a single row in t2 that matches (t1.a=t2.b), but its
150222 ** t2.c values is not NULL. If the (t2.c IS NULL) constraint is
150223 ** pushed down to the cursor, this row is filtered out, causing
150224 ** SQLite to synthesize a row of NULL values. Which does match the
150225 ** WHERE clause, and so the query returns a row. Which is incorrect.
150226 **
150227 ** For the same reason, WHERE terms such as:
150228 **
150229 ** WHERE 1 = (t2.c IS NULL)
150230 **
150231 ** are also excluded. See codeCursorHintIsOrFunction() for details.
150232 */
150233 if( pTabItem->fg.jointype & JT_LEFT0x08 ){
150234 Expr *pExpr = pTerm->pExpr;
150235 if( !ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(0x000001))!=0)
150236 || pExpr->w.iJoin!=pTabItem->iCursor
150237 ){
150238 sWalker.eCode = 0;
150239 sWalker.xExprCallback = codeCursorHintIsOrFunction;
150240 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
150241 if( sWalker.eCode ) continue;
150242 }
150243 }else{
150244 if( ExprHasProperty(pTerm->pExpr, EP_OuterON)(((pTerm->pExpr)->flags&(0x000001))!=0) ) continue;
150245 }
150246
150247 /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize
150248 ** the cursor. These terms are not needed as hints for a pure range
150249 ** scan (that has no == terms) so omit them. */
150250 if( pLoop->u.btree.nEq==0 && pTerm!=pEndRange ){
150251 for(j=0; j<pLoop->nLTerm && pLoop->aLTerm[j]!=pTerm; j++){}
150252 if( j<pLoop->nLTerm ) continue;
150253 }
150254
150255 /* No subqueries or non-deterministic functions allowed */
150256 if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue;
150257
150258 /* For an index scan, make sure referenced columns are actually in
150259 ** the index. */
150260 if( sHint.pIdx!=0 ){
150261 sWalker.eCode = 0;
150262 sWalker.xExprCallback = codeCursorHintCheckExpr;
150263 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
150264 if( sWalker.eCode ) continue;
150265 }
150266
150267 /* If we survive all prior tests, that means this term is worth hinting */
150268 pExpr = sqlite3ExprAnd(pParse, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0));
150269 }
150270 if( pExpr!=0 ){
150271 sWalker.xExprCallback = codeCursorHintFixExpr;
150272 sqlite3WalkExpr(&sWalker, pExpr);
150273 sqlite3VdbeAddOp4(v, OP_CursorHint182,
150274 (sHint.pIdx ? sHint.iIdxCur : sHint.iTabCur), 0, 0,
150275 (const char*)pExpr, P4_EXPR(-9));
150276 }
150277}
150278#else
150279# define codeCursorHint(A,B,C,D) /* No-op */
150280#endif /* SQLITE_ENABLE_CURSOR_HINTS */
150281
150282/*
150283** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains
150284** a rowid value just read from cursor iIdxCur, open on index pIdx. This
150285** function generates code to do a deferred seek of cursor iCur to the
150286** rowid stored in register iRowid.
150287**
150288** Normally, this is just:
150289**
150290** OP_DeferredSeek $iCur $iRowid
150291**
150292** Which causes a seek on $iCur to the row with rowid $iRowid.
150293**
150294** However, if the scan currently being coded is a branch of an OR-loop and
150295** the statement currently being coded is a SELECT, then additional information
150296** is added that might allow OP_Column to omit the seek and instead do its
150297** lookup on the index, thus avoiding an expensive seek operation. To
150298** enable this optimization, the P3 of OP_DeferredSeek is set to iIdxCur
150299** and P4 is set to an array of integers containing one entry for each column
150300** in the table. For each table column, if the column is the i'th
150301** column of the index, then the corresponding array entry is set to (i+1).
150302** If the column does not appear in the index at all, the array entry is set
150303** to 0. The OP_Column opcode can check this array to see if the column it
150304** wants is in the index and if it is, it will substitute the index cursor
150305** and column number and continue with those new values, rather than seeking
150306** the table cursor.
150307*/
150308static void codeDeferredSeek(
150309 WhereInfo *pWInfo, /* Where clause context */
150310 Index *pIdx, /* Index scan is using */
150311 int iCur, /* Cursor for IPK b-tree */
150312 int iIdxCur /* Index cursor */
150313){
150314 Parse *pParse = pWInfo->pParse; /* Parse context */
150315 Vdbe *v = pParse->pVdbe; /* Vdbe to generate code within */
150316
150317 assert( iIdxCur>0 )((void) (0));
150318 assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 )((void) (0));
150319
150320 pWInfo->bDeferredSeek = 1;
150321 sqlite3VdbeAddOp3(v, OP_DeferredSeek141, iIdxCur, 0, iCur);
150322 if( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE0x0020|WHERE_RIGHT_JOIN0x1000))
150323 && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask)(((pParse)->pToplevel ? (pParse)->pToplevel : (pParse))
->writeMask)==0
150324 ){
150325 int i;
150326 Table *pTab = pIdx->pTable;
150327 u32 *ai = (u32*)sqlite3DbMallocZero(pParse->db, sizeof(u32)*(pTab->nCol+1));
150328 if( ai ){
150329 ai[0] = pTab->nCol;
150330 for(i=0; i<pIdx->nColumn-1; i++){
150331 int x1, x2;
150332 assert( pIdx->aiColumn[i]<pTab->nCol )((void) (0));
150333 x1 = pIdx->aiColumn[i];
150334 x2 = sqlite3TableColumnToStorage(pTab, x1);
150335 testcase( x1!=x2 );
150336 if( x1>=0 ) ai[x2+1] = i+1;
150337 }
150338 sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY(-14));
150339 }
150340 }
150341}
150342
150343/*
150344** If the expression passed as the second argument is a vector, generate
150345** code to write the first nReg elements of the vector into an array
150346** of registers starting with iReg.
150347**
150348** If the expression is not a vector, then nReg must be passed 1. In
150349** this case, generate code to evaluate the expression and leave the
150350** result in register iReg.
150351*/
150352static void codeExprOrVector(Parse *pParse, Expr *p, int iReg, int nReg){
150353 assert( nReg>0 )((void) (0));
150354 if( p && sqlite3ExprIsVector(p) ){
150355#ifndef SQLITE_OMIT_SUBQUERY
150356 if( ExprUseXSelect(p)(((p)->flags&0x001000)!=0) ){
150357 Vdbe *v = pParse->pVdbe;
150358 int iSelect;
150359 assert( p->op==TK_SELECT )((void) (0));
150360 iSelect = sqlite3CodeSubselect(pParse, p);
150361 sqlite3VdbeAddOp3(v, OP_Copy80, iSelect, iReg, nReg-1);
150362 }else
150363#endif
150364 {
150365 int i;
150366 const ExprList *pList;
150367 assert( ExprUseXList(p) )((void) (0));
150368 pList = p->x.pList;
150369 assert( nReg<=pList->nExpr )((void) (0));
150370 for(i=0; i<nReg; i++){
150371 sqlite3ExprCode(pParse, pList->a[i].pExpr, iReg+i);
150372 }
150373 }
150374 }else{
150375 assert( nReg==1 || pParse->nErr )((void) (0));
150376 sqlite3ExprCode(pParse, p, iReg);
150377 }
150378}
150379
150380/* An instance of the IdxExprTrans object carries information about a
150381** mapping from an expression on table columns into a column in an index
150382** down through the Walker.
150383*/
150384typedef struct IdxExprTrans {
150385 Expr *pIdxExpr; /* The index expression */
150386 int iTabCur; /* The cursor of the corresponding table */
150387 int iIdxCur; /* The cursor for the index */
150388 int iIdxCol; /* The column for the index */
150389 int iTabCol; /* The column for the table */
150390 WhereInfo *pWInfo; /* Complete WHERE clause information */
150391 sqlite3 *db; /* Database connection (for malloc()) */
150392} IdxExprTrans;
150393
150394/*
150395** Preserve pExpr on the WhereETrans list of the WhereInfo.
150396*/
150397static void preserveExpr(IdxExprTrans *pTrans, Expr *pExpr){
150398 WhereExprMod *pNew;
150399 pNew = sqlite3DbMallocRaw(pTrans->db, sizeof(*pNew));
150400 if( pNew==0 ) return;
150401 pNew->pNext = pTrans->pWInfo->pExprMods;
150402 pTrans->pWInfo->pExprMods = pNew;
150403 pNew->pExpr = pExpr;
150404 memcpy(&pNew->orig, pExpr, sizeof(*pExpr));
150405}
150406
150407/* The walker node callback used to transform matching expressions into
150408** a reference to an index column for an index on an expression.
150409**
150410** If pExpr matches, then transform it into a reference to the index column
150411** that contains the value of pExpr.
150412*/
150413static int whereIndexExprTransNode(Walker *p, Expr *pExpr){
150414 IdxExprTrans *pX = p->u.pIdxTrans;
150415 if( sqlite3ExprCompare(0, pExpr, pX->pIdxExpr, pX->iTabCur)==0 ){
150416 pExpr = sqlite3ExprSkipCollate(pExpr);
150417 preserveExpr(pX, pExpr);
150418 pExpr->affExpr = sqlite3ExprAffinity(pExpr);
150419 pExpr->op = TK_COLUMN167;
150420 pExpr->iTable = pX->iIdxCur;
150421 pExpr->iColumn = pX->iIdxCol;
150422 testcase( ExprHasProperty(pExpr, EP_Unlikely) );
150423 ExprClearProperty(pExpr, EP_Skip|EP_Unlikely|EP_WinFunc|EP_Subrtn)(pExpr)->flags&=~(0x002000|0x080000|0x1000000|0x2000000
)
;
150424 pExpr->y.pTab = 0;
150425 return WRC_Prune1;
150426 }else{
150427 return WRC_Continue0;
150428 }
150429}
150430
150431#ifndef SQLITE_OMIT_GENERATED_COLUMNS
150432/* A walker node callback that translates a column reference to a table
150433** into a corresponding column reference of an index.
150434*/
150435static int whereIndexExprTransColumn(Walker *p, Expr *pExpr){
150436 if( pExpr->op==TK_COLUMN167 ){
150437 IdxExprTrans *pX = p->u.pIdxTrans;
150438 if( pExpr->iTable==pX->iTabCur && pExpr->iColumn==pX->iTabCol ){
150439 assert( ExprUseYTab(pExpr) && pExpr->y.pTab!=0 )((void) (0));
150440 preserveExpr(pX, pExpr);
150441 pExpr->affExpr = sqlite3TableColumnAffinity(pExpr->y.pTab,pExpr->iColumn);
150442 pExpr->iTable = pX->iIdxCur;
150443 pExpr->iColumn = pX->iIdxCol;
150444 pExpr->y.pTab = 0;
150445 }
150446 }
150447 return WRC_Continue0;
150448}
150449#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
150450
150451/*
150452** For an indexes on expression X, locate every instance of expression X
150453** in pExpr and change that subexpression into a reference to the appropriate
150454** column of the index.
150455**
150456** 2019-10-24: Updated to also translate references to a VIRTUAL column in
150457** the table into references to the corresponding (stored) column of the
150458** index.
150459*/
150460static void whereIndexExprTrans(
150461 Index *pIdx, /* The Index */
150462 int iTabCur, /* Cursor of the table that is being indexed */
150463 int iIdxCur, /* Cursor of the index itself */
150464 WhereInfo *pWInfo /* Transform expressions in this WHERE clause */
150465){
150466 int iIdxCol; /* Column number of the index */
150467 ExprList *aColExpr; /* Expressions that are indexed */
150468 Table *pTab;
150469 Walker w;
150470 IdxExprTrans x;
150471 aColExpr = pIdx->aColExpr;
150472 if( aColExpr==0 && !pIdx->bHasVCol ){
150473 /* The index does not reference any expressions or virtual columns
150474 ** so no translations are needed. */
150475 return;
150476 }
150477 pTab = pIdx->pTable;
150478 memset(&w, 0, sizeof(w));
150479 w.u.pIdxTrans = &x;
150480 x.iTabCur = iTabCur;
150481 x.iIdxCur = iIdxCur;
150482 x.pWInfo = pWInfo;
150483 x.db = pWInfo->pParse->db;
150484 for(iIdxCol=0; iIdxCol<pIdx->nColumn; iIdxCol++){
150485 i16 iRef = pIdx->aiColumn[iIdxCol];
150486 if( iRef==XN_EXPR(-2) ){
150487 assert( aColExpr!=0 && aColExpr->a[iIdxCol].pExpr!=0 )((void) (0));
150488 x.pIdxExpr = aColExpr->a[iIdxCol].pExpr;
150489 if( sqlite3ExprIsConstant(x.pIdxExpr) ) continue;
150490 w.xExprCallback = whereIndexExprTransNode;
150491#ifndef SQLITE_OMIT_GENERATED_COLUMNS
150492 }else if( iRef>=0
150493 && (pTab->aCol[iRef].colFlags & COLFLAG_VIRTUAL0x0020)!=0
150494 && ((pTab->aCol[iRef].colFlags & COLFLAG_HASCOLL0x0200)==0
150495 || sqlite3StrICmp(sqlite3ColumnColl(&pTab->aCol[iRef]),
150496 sqlite3StrBINARY)==0)
150497 ){
150498 /* Check to see if there are direct references to generated columns
150499 ** that are contained in the index. Pulling the generated column
150500 ** out of the index is an optimization only - the main table is always
150501 ** available if the index cannot be used. To avoid unnecessary
150502 ** complication, omit this optimization if the collating sequence for
150503 ** the column is non-standard */
150504 x.iTabCol = iRef;
150505 w.xExprCallback = whereIndexExprTransColumn;
150506#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
150507 }else{
150508 continue;
150509 }
150510 x.iIdxCol = iIdxCol;
150511 sqlite3WalkExpr(&w, pWInfo->pWhere);
150512 sqlite3WalkExprList(&w, pWInfo->pOrderBy);
150513 sqlite3WalkExprList(&w, pWInfo->pResultSet);
150514 }
150515}
150516
150517/*
150518** The pTruth expression is always true because it is the WHERE clause
150519** a partial index that is driving a query loop. Look through all of the
150520** WHERE clause terms on the query, and if any of those terms must be
150521** true because pTruth is true, then mark those WHERE clause terms as
150522** coded.
150523*/
150524static void whereApplyPartialIndexConstraints(
150525 Expr *pTruth,
150526 int iTabCur,
150527 WhereClause *pWC
150528){
150529 int i;
150530 WhereTerm *pTerm;
150531 while( pTruth->op==TK_AND44 ){
150532 whereApplyPartialIndexConstraints(pTruth->pLeft, iTabCur, pWC);
150533 pTruth = pTruth->pRight;
150534 }
150535 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
150536 Expr *pExpr;
150537 if( pTerm->wtFlags & TERM_CODED0x0004 ) continue;
150538 pExpr = pTerm->pExpr;
150539 if( sqlite3ExprCompare(0, pExpr, pTruth, iTabCur)==0 ){
150540 pTerm->wtFlags |= TERM_CODED0x0004;
150541 }
150542 }
150543}
150544
150545/*
150546** This routine is called right after An OP_Filter has been generated and
150547** before the corresponding index search has been performed. This routine
150548** checks to see if there are additional Bloom filters in inner loops that
150549** can be checked prior to doing the index lookup. If there are available
150550** inner-loop Bloom filters, then evaluate those filters now, before the
150551** index lookup. The idea is that a Bloom filter check is way faster than
150552** an index lookup, and the Bloom filter might return false, meaning that
150553** the index lookup can be skipped.
150554**
150555** We know that an inner loop uses a Bloom filter because it has the
150556** WhereLevel.regFilter set. If an inner-loop Bloom filter is checked,
150557** then clear the WhereLevel.regFilter value to prevent the Bloom filter
150558** from being checked a second time when the inner loop is evaluated.
150559*/
150560static SQLITE_NOINLINE__attribute__((noinline)) void filterPullDown(
150561 Parse *pParse, /* Parsing context */
150562 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
150563 int iLevel, /* Which level of pWInfo->a[] should be coded */
150564 int addrNxt, /* Jump here to bypass inner loops */
150565 Bitmask notReady /* Loops that are not ready */
150566){
150567 while( ++iLevel < pWInfo->nLevel ){
150568 WhereLevel *pLevel = &pWInfo->a[iLevel];
150569 WhereLoop *pLoop = pLevel->pWLoop;
150570 if( pLevel->regFilter==0 ) continue;
150571 if( pLevel->pWLoop->nSkip ) continue;
150572 /* ,--- Because sqlite3ConstructBloomFilter() has will not have set
150573 ** vvvvv--' pLevel->regFilter if this were true. */
150574 if( NEVER(pLoop->prereq & notReady)(pLoop->prereq & notReady) ) continue;
150575 assert( pLevel->addrBrk==0 )((void) (0));
150576 pLevel->addrBrk = addrNxt;
150577 if( pLoop->wsFlags & WHERE_IPK0x00000100 ){
150578 WhereTerm *pTerm = pLoop->aLTerm[0];
150579 int regRowid;
150580 assert( pTerm!=0 )((void) (0));
150581 assert( pTerm->pExpr!=0 )((void) (0));
150582 testcase( pTerm->wtFlags & TERM_VIRTUAL );
150583 regRowid = sqlite3GetTempReg(pParse);
150584 regRowid = codeEqualityTerm(pParse, pTerm, pLevel, 0, 0, regRowid);
150585 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter63, pLevel->regFilter,
150586 addrNxt, regRowid, 1);
150587 VdbeCoverage(pParse->pVdbe);
150588 }else{
150589 u16 nEq = pLoop->u.btree.nEq;
150590 int r1;
150591 char *zStartAff;
150592
150593 assert( pLoop->wsFlags & WHERE_INDEXED )((void) (0));
150594 assert( (pLoop->wsFlags & WHERE_COLUMN_IN)==0 )((void) (0));
150595 r1 = codeAllEqualityTerms(pParse,pLevel,0,0,&zStartAff);
150596 codeApplyAffinity(pParse, r1, nEq, zStartAff);
150597 sqlite3DbFree(pParse->db, zStartAff);
150598 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter63, pLevel->regFilter,
150599 addrNxt, r1, nEq);
150600 VdbeCoverage(pParse->pVdbe);
150601 }
150602 pLevel->regFilter = 0;
150603 pLevel->addrBrk = 0;
150604 }
150605}
150606
150607/*
150608** Generate code for the start of the iLevel-th loop in the WHERE clause
150609** implementation described by pWInfo.
150610*/
150611SQLITE_PRIVATEstatic Bitmask sqlite3WhereCodeOneLoopStart(
150612 Parse *pParse, /* Parsing context */
150613 Vdbe *v, /* Prepared statement under construction */
150614 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
150615 int iLevel, /* Which level of pWInfo->a[] should be coded */
150616 WhereLevel *pLevel, /* The current level pointer */
150617 Bitmask notReady /* Which tables are currently available */
150618){
150619 int j, k; /* Loop counters */
150620 int iCur; /* The VDBE cursor for the table */
150621 int addrNxt; /* Where to jump to continue with the next IN case */
150622 int bRev; /* True if we need to scan in reverse order */
150623 WhereLoop *pLoop; /* The WhereLoop object being coded */
150624 WhereClause *pWC; /* Decomposition of the entire WHERE clause */
150625 WhereTerm *pTerm; /* A WHERE clause term */
150626 sqlite3 *db; /* Database connection */
150627 SrcItem *pTabItem; /* FROM clause term being coded */
150628 int addrBrk; /* Jump here to break out of the loop */
150629 int addrHalt; /* addrBrk for the outermost loop */
150630 int addrCont; /* Jump here to continue with next cycle */
150631 int iRowidReg = 0; /* Rowid is stored in this register, if not zero */
150632 int iReleaseReg = 0; /* Temp register to free before returning */
150633 Index *pIdx = 0; /* Index used by loop (if any) */
150634 int iLoop; /* Iteration of constraint generator loop */
150635
150636 pWC = &pWInfo->sWC;
150637 db = pParse->db;
150638 pLoop = pLevel->pWLoop;
150639 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
150640 iCur = pTabItem->iCursor;
150641 pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
150642 bRev = (pWInfo->revMask>>iLevel)&1;
150643 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
150644#if WHERETRACE_ENABLED /* 0x20800 */
150645 if( sqlite3WhereTrace & 0x800 ){
150646 sqlite3DebugPrintf("Coding level %d of %d: notReady=%llx iFrom=%d\n",
150647 iLevel, pWInfo->nLevel, (u64)notReady, pLevel->iFrom);
150648 sqlite3WhereLoopPrint(pLoop, pWC);
150649 }
150650 if( sqlite3WhereTrace & 0x20000 ){
150651 if( iLevel==0 ){
150652 sqlite3DebugPrintf("WHERE clause being coded:\n");
150653 sqlite3TreeViewExpr(0, pWInfo->pWhere, 0);
150654 }
150655 sqlite3DebugPrintf("All WHERE-clause terms before coding:\n");
150656 sqlite3WhereClausePrint(pWC);
150657 }
150658#endif
150659
150660 /* Create labels for the "break" and "continue" instructions
150661 ** for the current loop. Jump to addrBrk to break out of a loop.
150662 ** Jump to cont to go immediately to the next iteration of the
150663 ** loop.
150664 **
150665 ** When there is an IN operator, we also have a "addrNxt" label that
150666 ** means to continue with the next IN value combination. When
150667 ** there are no IN operators in the constraints, the "addrNxt" label
150668 ** is the same as "addrBrk".
150669 */
150670 addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
150671 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(pParse);
150672
150673 /* If this is the right table of a LEFT OUTER JOIN, allocate and
150674 ** initialize a memory cell that records if this table matches any
150675 ** row of the left table of the join.
150676 */
150677 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))((void) (0))
150678 || pLevel->iFrom>0 || (pTabItem[0].fg.jointype & JT_LEFT)==0((void) (0))
150679 )((void) (0));
150680 if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT0x08)!=0 ){
150681 pLevel->iLeftJoin = ++pParse->nMem;
150682 sqlite3VdbeAddOp2(v, OP_Integer71, 0, pLevel->iLeftJoin);
150683 VdbeComment((v, "init LEFT JOIN no-match flag"));
150684 }
150685
150686 /* Compute a safe address to jump to if we discover that the table for
150687 ** this loop is empty and can never contribute content. */
150688 for(j=iLevel; j>0; j--){
150689 if( pWInfo->a[j].iLeftJoin ) break;
150690 if( pWInfo->a[j].pRJ ) break;
150691 }
150692 addrHalt = pWInfo->a[j].addrBrk;
150693
150694 /* Special case of a FROM clause subquery implemented as a co-routine */
150695 if( pTabItem->fg.viaCoroutine ){
150696 int regYield = pTabItem->regReturn;
150697 sqlite3VdbeAddOp3(v, OP_InitCoroutine10, regYield, 0, pTabItem->addrFillSub);
150698 pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield11, regYield, addrBrk);
150699 VdbeCoverage(v);
150700 VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
150701 pLevel->op = OP_Goto8;
150702 }else
150703
150704#ifndef SQLITE_OMIT_VIRTUALTABLE
150705 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE0x00000400)!=0 ){
150706 /* Case 1: The table is a virtual-table. Use the VFilter and VNext
150707 ** to access the data.
150708 */
150709 int iReg; /* P3 Value for OP_VFilter */
150710 int addrNotFound;
150711 int nConstraint = pLoop->nLTerm;
150712
150713 iReg = sqlite3GetTempRange(pParse, nConstraint+2);
150714 addrNotFound = pLevel->addrBrk;
150715 for(j=0; j<nConstraint; j++){
150716 int iTarget = iReg+j+2;
150717 pTerm = pLoop->aLTerm[j];
150718 if( NEVER(pTerm==0)(pTerm==0) ) continue;
150719 if( pTerm->eOperator & WO_IN0x0001 ){
150720 if( SMASKBIT32(j)((j)<=31?((unsigned int)1)<<(j):0) & pLoop->u.vtab.mHandleIn ){
150721 int iTab = pParse->nTab++;
150722 int iCache = ++pParse->nMem;
150723 sqlite3CodeRhsOfIN(pParse, pTerm->pExpr, iTab);
150724 sqlite3VdbeAddOp3(v, OP_VInitIn174, iTab, iTarget, iCache);
150725 }else{
150726 codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
150727 addrNotFound = pLevel->addrNxt;
150728 }
150729 }else{
150730 Expr *pRight = pTerm->pExpr->pRight;
150731 codeExprOrVector(pParse, pRight, iTarget, 1);
150732 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET74
150733 && pLoop->u.vtab.bOmitOffset
150734 ){
150735 assert( pTerm->eOperator==WO_AUX )((void) (0));
150736 assert( pWInfo->pLimit!=0 )((void) (0));
150737 assert( pWInfo->pLimit->iOffset>0 )((void) (0));
150738 sqlite3VdbeAddOp2(v, OP_Integer71, 0, pWInfo->pLimit->iOffset);
150739 VdbeComment((v,"Zero OFFSET counter"));
150740 }
150741 }
150742 }
150743 sqlite3VdbeAddOp2(v, OP_Integer71, pLoop->u.vtab.idxNum, iReg);
150744 sqlite3VdbeAddOp2(v, OP_Integer71, nConstraint, iReg+1);
150745 sqlite3VdbeAddOp4(v, OP_VFilter6, iCur, addrNotFound, iReg,
150746 pLoop->u.vtab.idxStr,
150747 pLoop->u.vtab.needFree ? P4_DYNAMIC(-6) : P4_STATIC(-1));
150748 VdbeCoverage(v);
150749 pLoop->u.vtab.needFree = 0;
150750 /* An OOM inside of AddOp4(OP_VFilter) instruction above might have freed
150751 ** the u.vtab.idxStr. NULL it out to prevent a use-after-free */
150752 if( db->mallocFailed ) pLoop->u.vtab.idxStr = 0;
150753 pLevel->p1 = iCur;
150754 pLevel->op = pWInfo->eOnePass ? OP_Noop184 : OP_VNext62;
150755 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
150756 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 )((void) (0));
150757
150758 for(j=0; j<nConstraint; j++){
150759 pTerm = pLoop->aLTerm[j];
150760 if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){
150761 disableTerm(pLevel, pTerm);
150762 continue;
150763 }
150764 if( (pTerm->eOperator & WO_IN0x0001)!=0
150765 && (SMASKBIT32(j)((j)<=31?((unsigned int)1)<<(j):0) & pLoop->u.vtab.mHandleIn)==0
150766 && !db->mallocFailed
150767 ){
150768 Expr *pCompare; /* The comparison operator */
150769 Expr *pRight; /* RHS of the comparison */
150770 VdbeOp *pOp; /* Opcode to access the value of the IN constraint */
150771 int iIn; /* IN loop corresponding to the j-th constraint */
150772
150773 /* Reload the constraint value into reg[iReg+j+2]. The same value
150774 ** was loaded into the same register prior to the OP_VFilter, but
150775 ** the xFilter implementation might have changed the datatype or
150776 ** encoding of the value in the register, so it *must* be reloaded.
150777 */
150778 for(iIn=0; ALWAYS(iIn<pLevel->u.in.nIn)(iIn<pLevel->u.in.nIn); iIn++){
150779 pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[iIn].addrInTop);
150780 if( (pOp->opcode==OP_Column94 && pOp->p3==iReg+j+2)
150781 || (pOp->opcode==OP_Rowid135 && pOp->p2==iReg+j+2)
150782 ){
150783 testcase( pOp->opcode==OP_Rowid );
150784 sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3);
150785 break;
150786 }
150787 }
150788
150789 /* Generate code that will continue to the next row if
150790 ** the IN constraint is not satisfied
150791 */
150792 pCompare = sqlite3PExpr(pParse, TK_EQ53, 0, 0);
150793 if( !db->mallocFailed ){
150794 int iFld = pTerm->u.x.iField;
150795 Expr *pLeft = pTerm->pExpr->pLeft;
150796 assert( pLeft!=0 )((void) (0));
150797 if( iFld>0 ){
150798 assert( pLeft->op==TK_VECTOR )((void) (0));
150799 assert( ExprUseXList(pLeft) )((void) (0));
150800 assert( iFld<=pLeft->x.pList->nExpr )((void) (0));
150801 pCompare->pLeft = pLeft->x.pList->a[iFld-1].pExpr;
150802 }else{
150803 pCompare->pLeft = pLeft;
150804 }
150805 pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER176, 0);
150806 if( pRight ){
150807 pRight->iTable = iReg+j+2;
150808 sqlite3ExprIfFalse(
150809 pParse, pCompare, pLevel->addrCont, SQLITE_JUMPIFNULL0x10
150810 );
150811 }
150812 pCompare->pLeft = 0;
150813 }
150814 sqlite3ExprDelete(db, pCompare);
150815 }
150816 }
150817
150818 /* These registers need to be preserved in case there is an IN operator
150819 ** loop. So we could deallocate the registers here (and potentially
150820 ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems
150821 ** simpler and safer to simply not reuse the registers.
150822 **
150823 ** sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
150824 */
150825 }else
150826#endif /* SQLITE_OMIT_VIRTUALTABLE */
150827
150828 if( (pLoop->wsFlags & WHERE_IPK0x00000100)!=0
150829 && (pLoop->wsFlags & (WHERE_COLUMN_IN0x00000004|WHERE_COLUMN_EQ0x00000001))!=0
150830 ){
150831 /* Case 2: We can directly reference a single row using an
150832 ** equality comparison against the ROWID field. Or
150833 ** we reference multiple rows using a "rowid IN (...)"
150834 ** construct.
150835 */
150836 assert( pLoop->u.btree.nEq==1 )((void) (0));
150837 pTerm = pLoop->aLTerm[0];
150838 assert( pTerm!=0 )((void) (0));
150839 assert( pTerm->pExpr!=0 )((void) (0));
150840 testcase( pTerm->wtFlags & TERM_VIRTUAL );
150841 iReleaseReg = ++pParse->nMem;
150842 iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
150843 if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
150844 addrNxt = pLevel->addrNxt;
150845 if( pLevel->regFilter ){
150846 sqlite3VdbeAddOp4Int(v, OP_Filter63, pLevel->regFilter, addrNxt,
150847 iRowidReg, 1);
150848 VdbeCoverage(v);
150849 filterPullDown(pParse, pWInfo, iLevel, addrNxt, notReady);
150850 }
150851 sqlite3VdbeAddOp3(v, OP_SeekRowid29, iCur, addrNxt, iRowidReg);
150852 VdbeCoverage(v);
150853 pLevel->op = OP_Noop184;
150854 }else if( (pLoop->wsFlags & WHERE_IPK0x00000100)!=0
150855 && (pLoop->wsFlags & WHERE_COLUMN_RANGE0x00000002)!=0
150856 ){
150857 /* Case 3: We have an inequality comparison against the ROWID field.
150858 */
150859 int testOp = OP_Noop184;
150860 int start;
150861 int memEndValue = 0;
150862 WhereTerm *pStart, *pEnd;
150863
150864 j = 0;
150865 pStart = pEnd = 0;
150866 if( pLoop->wsFlags & WHERE_BTM_LIMIT0x00000020 ) pStart = pLoop->aLTerm[j++];
150867 if( pLoop->wsFlags & WHERE_TOP_LIMIT0x00000010 ) pEnd = pLoop->aLTerm[j++];
150868 assert( pStart!=0 || pEnd!=0 )((void) (0));
150869 if( bRev ){
150870 pTerm = pStart;
150871 pStart = pEnd;
150872 pEnd = pTerm;
150873 }
150874 codeCursorHint(pTabItem, pWInfo, pLevel, pEnd);
150875 if( pStart ){
150876 Expr *pX; /* The expression that defines the start bound */
150877 int r1, rTemp; /* Registers for holding the start boundary */
150878 int op; /* Cursor seek operation */
150879
150880 /* The following constant maps TK_xx codes into corresponding
150881 ** seek opcodes. It depends on a particular ordering of TK_xx
150882 */
150883 const u8 aMoveOp[] = {
150884 /* TK_GT */ OP_SeekGT23,
150885 /* TK_LE */ OP_SeekLE21,
150886 /* TK_LT */ OP_SeekLT20,
150887 /* TK_GE */ OP_SeekGE22
150888 };
150889 assert( TK_LE==TK_GT+1 )((void) (0)); /* Make sure the ordering.. */
150890 assert( TK_LT==TK_GT+2 )((void) (0)); /* ... of the TK_xx values... */
150891 assert( TK_GE==TK_GT+3 )((void) (0)); /* ... is correcct. */
150892
150893 assert( (pStart->wtFlags & TERM_VNULL)==0 )((void) (0));
150894 testcase( pStart->wtFlags & TERM_VIRTUAL );
150895 pX = pStart->pExpr;
150896 assert( pX!=0 )((void) (0));
150897 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
150898 if( sqlite3ExprIsVector(pX->pRight) ){
150899 r1 = rTemp = sqlite3GetTempReg(pParse);
150900 codeExprOrVector(pParse, pX->pRight, r1, 1);
150901 testcase( pX->op==TK_GT );
150902 testcase( pX->op==TK_GE );
150903 testcase( pX->op==TK_LT );
150904 testcase( pX->op==TK_LE );
150905 op = aMoveOp[((pX->op - TK_GT54 - 1) & 0x3) | 0x1];
150906 assert( pX->op!=TK_GT || op==OP_SeekGE )((void) (0));
150907 assert( pX->op!=TK_GE || op==OP_SeekGE )((void) (0));
150908 assert( pX->op!=TK_LT || op==OP_SeekLE )((void) (0));
150909 assert( pX->op!=TK_LE || op==OP_SeekLE )((void) (0));
150910 }else{
150911 r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
150912 disableTerm(pLevel, pStart);
150913 op = aMoveOp[(pX->op - TK_GT54)];
150914 }
150915 sqlite3VdbeAddOp3(v, op, iCur, addrBrk, r1);
150916 VdbeComment((v, "pk"));
150917 VdbeCoverageIf(v, pX->op==TK_GT);
150918 VdbeCoverageIf(v, pX->op==TK_LE);
150919 VdbeCoverageIf(v, pX->op==TK_LT);
150920 VdbeCoverageIf(v, pX->op==TK_GE);
150921 sqlite3ReleaseTempReg(pParse, rTemp);
150922 }else{
150923 sqlite3VdbeAddOp2(v, bRev ? OP_Last31 : OP_Rewind35, iCur, addrHalt);
150924 VdbeCoverageIf(v, bRev==0);
150925 VdbeCoverageIf(v, bRev!=0);
150926 }
150927 if( pEnd ){
150928 Expr *pX;
150929 pX = pEnd->pExpr;
150930 assert( pX!=0 )((void) (0));
150931 assert( (pEnd->wtFlags & TERM_VNULL)==0 )((void) (0));
150932 testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
150933 testcase( pEnd->wtFlags & TERM_VIRTUAL );
150934 memEndValue = ++pParse->nMem;
150935 codeExprOrVector(pParse, pX->pRight, memEndValue, 1);
150936 if( 0==sqlite3ExprIsVector(pX->pRight)
150937 && (pX->op==TK_LT56 || pX->op==TK_GT54)
150938 ){
150939 testOp = bRev ? OP_Le55 : OP_Ge57;
150940 }else{
150941 testOp = bRev ? OP_Lt56 : OP_Gt54;
150942 }
150943 if( 0==sqlite3ExprIsVector(pX->pRight) ){
150944 disableTerm(pLevel, pEnd);
150945 }
150946 }
150947 start = sqlite3VdbeCurrentAddr(v);
150948 pLevel->op = bRev ? OP_Prev37 : OP_Next38;
150949 pLevel->p1 = iCur;
150950 pLevel->p2 = start;
150951 assert( pLevel->p5==0 )((void) (0));
150952 if( testOp!=OP_Noop184 ){
150953 iRowidReg = ++pParse->nMem;
150954 sqlite3VdbeAddOp2(v, OP_Rowid135, iCur, iRowidReg);
150955 sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
150956 VdbeCoverageIf(v, testOp==OP_Le);
150957 VdbeCoverageIf(v, testOp==OP_Lt);
150958 VdbeCoverageIf(v, testOp==OP_Ge);
150959 VdbeCoverageIf(v, testOp==OP_Gt);
150960 sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC0x43 | SQLITE_JUMPIFNULL0x10);
150961 }
150962 }else if( pLoop->wsFlags & WHERE_INDEXED0x00000200 ){
150963 /* Case 4: A scan using an index.
150964 **
150965 ** The WHERE clause may contain zero or more equality
150966 ** terms ("==" or "IN" operators) that refer to the N
150967 ** left-most columns of the index. It may also contain
150968 ** inequality constraints (>, <, >= or <=) on the indexed
150969 ** column that immediately follows the N equalities. Only
150970 ** the right-most column can be an inequality - the rest must
150971 ** use the "==" and "IN" operators. For example, if the
150972 ** index is on (x,y,z), then the following clauses are all
150973 ** optimized:
150974 **
150975 ** x=5
150976 ** x=5 AND y=10
150977 ** x=5 AND y<10
150978 ** x=5 AND y>5 AND y<10
150979 ** x=5 AND y=5 AND z<=10
150980 **
150981 ** The z<10 term of the following cannot be used, only
150982 ** the x=5 term:
150983 **
150984 ** x=5 AND z<10
150985 **
150986 ** N may be zero if there are inequality constraints.
150987 ** If there are no inequality constraints, then N is at
150988 ** least one.
150989 **
150990 ** This case is also used when there are no WHERE clause
150991 ** constraints but an index is selected anyway, in order
150992 ** to force the output order to conform to an ORDER BY.
150993 */
150994 static const u8 aStartOp[] = {
150995 0,
150996 0,
150997 OP_Rewind35, /* 2: (!start_constraints && startEq && !bRev) */
150998 OP_Last31, /* 3: (!start_constraints && startEq && bRev) */
150999 OP_SeekGT23, /* 4: (start_constraints && !startEq && !bRev) */
151000 OP_SeekLT20, /* 5: (start_constraints && !startEq && bRev) */
151001 OP_SeekGE22, /* 6: (start_constraints && startEq && !bRev) */
151002 OP_SeekLE21 /* 7: (start_constraints && startEq && bRev) */
151003 };
151004 static const u8 aEndOp[] = {
151005 OP_IdxGE42, /* 0: (end_constraints && !bRev && !endEq) */
151006 OP_IdxGT40, /* 1: (end_constraints && !bRev && endEq) */
151007 OP_IdxLE39, /* 2: (end_constraints && bRev && !endEq) */
151008 OP_IdxLT41, /* 3: (end_constraints && bRev && endEq) */
151009 };
151010 u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */
151011 u16 nBtm = pLoop->u.btree.nBtm; /* Length of BTM vector */
151012 u16 nTop = pLoop->u.btree.nTop; /* Length of TOP vector */
151013 int regBase; /* Base register holding constraint values */
151014 WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */
151015 WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */
151016 int startEq; /* True if range start uses ==, >= or <= */
151017 int endEq; /* True if range end uses ==, >= or <= */
151018 int start_constraints; /* Start of range is constrained */
151019 int nConstraint; /* Number of constraint terms */
151020 int iIdxCur; /* The VDBE cursor for the index */
151021 int nExtraReg = 0; /* Number of extra registers needed */
151022 int op; /* Instruction opcode */
151023 char *zStartAff; /* Affinity for start of range constraint */
151024 char *zEndAff = 0; /* Affinity for end of range constraint */
151025 u8 bSeekPastNull = 0; /* True to seek past initial nulls */
151026 u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */
151027 int omitTable; /* True if we use the index only */
151028 int regBignull = 0; /* big-null flag register */
151029 int addrSeekScan = 0; /* Opcode of the OP_SeekScan, if any */
151030
151031 pIdx = pLoop->u.btree.pIndex;
151032 iIdxCur = pLevel->iIdxCur;
151033 assert( nEq>=pLoop->nSkip )((void) (0));
151034
151035 /* Find any inequality constraint terms for the start and end
151036 ** of the range.
151037 */
151038 j = nEq;
151039 if( pLoop->wsFlags & WHERE_BTM_LIMIT0x00000020 ){
151040 pRangeStart = pLoop->aLTerm[j++];
151041 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm)((nExtraReg)>(pLoop->u.btree.nBtm)?(nExtraReg):(pLoop->
u.btree.nBtm))
;
151042 /* Like optimization range constraints always occur in pairs */
151043 assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||((void) (0))
151044 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 )((void) (0));
151045 }
151046 if( pLoop->wsFlags & WHERE_TOP_LIMIT0x00000010 ){
151047 pRangeEnd = pLoop->aLTerm[j++];
151048 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop)((nExtraReg)>(pLoop->u.btree.nTop)?(nExtraReg):(pLoop->
u.btree.nTop))
;
151049#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
151050 if( (pRangeEnd->wtFlags & TERM_LIKEOPT0x0100)!=0 ){
151051 assert( pRangeStart!=0 )((void) (0)); /* LIKE opt constraints */
151052 assert( pRangeStart->wtFlags & TERM_LIKEOPT )((void) (0)); /* occur in pairs */
151053 pLevel->iLikeRepCntr = (u32)++pParse->nMem;
151054 sqlite3VdbeAddOp2(v, OP_Integer71, 1, (int)pLevel->iLikeRepCntr);
151055 VdbeComment((v, "LIKE loop counter"));
151056 pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
151057 /* iLikeRepCntr actually stores 2x the counter register number. The
151058 ** bottom bit indicates whether the search order is ASC or DESC. */
151059 testcase( bRev );
151060 testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
151061 assert( (bRev & ~1)==0 )((void) (0));
151062 pLevel->iLikeRepCntr <<=1;
151063 pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC1);
151064 }
151065#endif
151066 if( pRangeStart==0 ){
151067 j = pIdx->aiColumn[nEq];
151068 if( (j>=0 && pIdx->pTable->aCol[j].notNull==0) || j==XN_EXPR(-2) ){
151069 bSeekPastNull = 1;
151070 }
151071 }
151072 }
151073 assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 )((void) (0));
151074
151075 /* If the WHERE_BIGNULL_SORT flag is set, then index column nEq uses
151076 ** a non-default "big-null" sort (either ASC NULLS LAST or DESC NULLS
151077 ** FIRST). In both cases separate ordered scans are made of those
151078 ** index entries for which the column is null and for those for which
151079 ** it is not. For an ASC sort, the non-NULL entries are scanned first.
151080 ** For DESC, NULL entries are scanned first.
151081 */
151082 if( (pLoop->wsFlags & (WHERE_TOP_LIMIT0x00000010|WHERE_BTM_LIMIT0x00000020))==0
151083 && (pLoop->wsFlags & WHERE_BIGNULL_SORT0x00080000)!=0
151084 ){
151085 assert( bSeekPastNull==0 && nExtraReg==0 && nBtm==0 && nTop==0 )((void) (0));
151086 assert( pRangeEnd==0 && pRangeStart==0 )((void) (0));
151087 testcase( pLoop->nSkip>0 );
151088 nExtraReg = 1;
151089 bSeekPastNull = 1;
151090 pLevel->regBignull = regBignull = ++pParse->nMem;
151091 if( pLevel->iLeftJoin ){
151092 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regBignull);
151093 }
151094 pLevel->addrBignull = sqlite3VdbeMakeLabel(pParse);
151095 }
151096
151097 /* If we are doing a reverse order scan on an ascending index, or
151098 ** a forward order scan on a descending index, interchange the
151099 ** start and end terms (pRangeStart and pRangeEnd).
151100 */
151101 if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC0)) ){
151102 SWAP(WhereTerm *, pRangeEnd, pRangeStart){WhereTerm * t=pRangeEnd; pRangeEnd=pRangeStart; pRangeStart=
t;}
;
151103 SWAP(u8, bSeekPastNull, bStopAtNull){u8 t=bSeekPastNull; bSeekPastNull=bStopAtNull; bStopAtNull=t
;}
;
151104 SWAP(u8, nBtm, nTop){u8 t=nBtm; nBtm=nTop; nTop=t;};
151105 }
151106
151107 if( iLevel>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN0x00100000)!=0 ){
151108 /* In case OP_SeekScan is used, ensure that the index cursor does not
151109 ** point to a valid row for the first iteration of this loop. */
151110 sqlite3VdbeAddOp1(v, OP_NullRow136, iIdxCur);
151111 }
151112
151113 /* Generate code to evaluate all constraint terms using == or IN
151114 ** and store the values of those terms in an array of registers
151115 ** starting at regBase.
151116 */
151117 codeCursorHint(pTabItem, pWInfo, pLevel, pRangeEnd);
151118 regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
151119 assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq )((void) (0));
151120 if( zStartAff && nTop ){
151121 zEndAff = sqlite3DbStrDup(db, &zStartAff[nEq]);
151122 }
151123 addrNxt = (regBignull ? pLevel->addrBignull : pLevel->addrNxt);
151124
151125 testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
151126 testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
151127 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
151128 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
151129 startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE(0x0002<<(55 -53))|WO_GE(0x0002<<(57 -53)));
151130 endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE(0x0002<<(55 -53))|WO_GE(0x0002<<(57 -53)));
151131 start_constraints = pRangeStart || nEq>0;
151132
151133 /* Seek the index cursor to the start of the range. */
151134 nConstraint = nEq;
151135 if( pRangeStart ){
151136 Expr *pRight = pRangeStart->pExpr->pRight;
151137 codeExprOrVector(pParse, pRight, regBase+nEq, nBtm);
151138 whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
151139 if( (pRangeStart->wtFlags & TERM_VNULL0x0080)==0
151140 && sqlite3ExprCanBeNull(pRight)
151141 ){
151142 sqlite3VdbeAddOp2(v, OP_IsNull50, regBase+nEq, addrNxt);
151143 VdbeCoverage(v);
151144 }
151145 if( zStartAff ){
151146 updateRangeAffinityStr(pRight, nBtm, &zStartAff[nEq]);
151147 }
151148 nConstraint += nBtm;
151149 testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
151150 if( sqlite3ExprIsVector(pRight)==0 ){
151151 disableTerm(pLevel, pRangeStart);
151152 }else{
151153 startEq = 1;
151154 }
151155 bSeekPastNull = 0;
151156 }else if( bSeekPastNull ){
151157 startEq = 0;
151158 sqlite3VdbeAddOp2(v, OP_Null75, 0, regBase+nEq);
151159 start_constraints = 1;
151160 nConstraint++;
151161 }else if( regBignull ){
151162 sqlite3VdbeAddOp2(v, OP_Null75, 0, regBase+nEq);
151163 start_constraints = 1;
151164 nConstraint++;
151165 }
151166 codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
151167 if( pLoop->nSkip>0 && nConstraint==pLoop->nSkip ){
151168 /* The skip-scan logic inside the call to codeAllEqualityConstraints()
151169 ** above has already left the cursor sitting on the correct row,
151170 ** so no further seeking is needed */
151171 }else{
151172 if( regBignull ){
151173 sqlite3VdbeAddOp2(v, OP_Integer71, 1, regBignull);
151174 VdbeComment((v, "NULL-scan pass ctr"));
151175 }
151176 if( pLevel->regFilter ){
151177 sqlite3VdbeAddOp4Int(v, OP_Filter63, pLevel->regFilter, addrNxt,
151178 regBase, nEq);
151179 VdbeCoverage(v);
151180 filterPullDown(pParse, pWInfo, iLevel, addrNxt, notReady);
151181 }
151182
151183 op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
151184 assert( op!=0 )((void) (0));
151185 if( (pLoop->wsFlags & WHERE_IN_SEEKSCAN0x00100000)!=0 && op==OP_SeekGE22 ){
151186 assert( regBignull==0 )((void) (0));
151187 /* TUNING: The OP_SeekScan opcode seeks to reduce the number
151188 ** of expensive seek operations by replacing a single seek with
151189 ** 1 or more step operations. The question is, how many steps
151190 ** should we try before giving up and going with a seek. The cost
151191 ** of a seek is proportional to the logarithm of the of the number
151192 ** of entries in the tree, so basing the number of steps to try
151193 ** on the estimated number of rows in the btree seems like a good
151194 ** guess. */
151195 addrSeekScan = sqlite3VdbeAddOp1(v, OP_SeekScan124,
151196 (pIdx->aiRowLogEst[0]+9)/10);
151197 VdbeCoverage(v);
151198 }
151199 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
151200 VdbeCoverage(v);
151201 VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind );
151202 VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last );
151203 VdbeCoverageIf(v, op==OP_SeekGT); testcase( op==OP_SeekGT );
151204 VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE );
151205 VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE );
151206 VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT );
151207
151208 assert( bSeekPastNull==0 || bStopAtNull==0 )((void) (0));
151209 if( regBignull ){
151210 assert( bSeekPastNull==1 || bStopAtNull==1 )((void) (0));
151211 assert( bSeekPastNull==!bStopAtNull )((void) (0));
151212 assert( bStopAtNull==startEq )((void) (0));
151213 sqlite3VdbeAddOp2(v, OP_Goto8, 0, sqlite3VdbeCurrentAddr(v)+2);
151214 op = aStartOp[(nConstraint>1)*4 + 2 + bRev];
151215 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase,
151216 nConstraint-startEq);
151217 VdbeCoverage(v);
151218 VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind );
151219 VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last );
151220 VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE );
151221 VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE );
151222 assert( op==OP_Rewind || op==OP_Last || op==OP_SeekGE || op==OP_SeekLE)((void) (0));
151223 }
151224 }
151225
151226 /* Load the value for the inequality constraint at the end of the
151227 ** range (if any).
151228 */
151229 nConstraint = nEq;
151230 assert( pLevel->p2==0 )((void) (0));
151231 if( pRangeEnd ){
151232 Expr *pRight = pRangeEnd->pExpr->pRight;
151233 if( addrSeekScan ){
151234 /* For a seek-scan that has a range on the lowest term of the index,
151235 ** we have to make the top of the loop be code that sets the end
151236 ** condition of the range. Otherwise, the OP_SeekScan might jump
151237 ** over that initialization, leaving the range-end value set to the
151238 ** range-start value, resulting in a wrong answer.
151239 ** See ticket 5981a8c041a3c2f3 (2021-11-02).
151240 */
151241 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
151242 }
151243 codeExprOrVector(pParse, pRight, regBase+nEq, nTop);
151244 whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
151245 if( (pRangeEnd->wtFlags & TERM_VNULL0x0080)==0
151246 && sqlite3ExprCanBeNull(pRight)
151247 ){
151248 sqlite3VdbeAddOp2(v, OP_IsNull50, regBase+nEq, addrNxt);
151249 VdbeCoverage(v);
151250 }
151251 if( zEndAff ){
151252 updateRangeAffinityStr(pRight, nTop, zEndAff);
151253 codeApplyAffinity(pParse, regBase+nEq, nTop, zEndAff);
151254 }else{
151255 assert( pParse->db->mallocFailed )((void) (0));
151256 }
151257 nConstraint += nTop;
151258 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
151259
151260 if( sqlite3ExprIsVector(pRight)==0 ){
151261 disableTerm(pLevel, pRangeEnd);
151262 }else{
151263 endEq = 1;
151264 }
151265 }else if( bStopAtNull ){
151266 if( regBignull==0 ){
151267 sqlite3VdbeAddOp2(v, OP_Null75, 0, regBase+nEq);
151268 endEq = 0;
151269 }
151270 nConstraint++;
151271 }
151272 sqlite3DbFree(db, zStartAff);
151273 sqlite3DbFree(db, zEndAff);
151274
151275 /* Top of the loop body */
151276 if( pLevel->p2==0 ) pLevel->p2 = sqlite3VdbeCurrentAddr(v);
151277
151278 /* Check if the index cursor is past the end of the range. */
151279 if( nConstraint ){
151280 if( regBignull ){
151281 /* Except, skip the end-of-range check while doing the NULL-scan */
151282 sqlite3VdbeAddOp2(v, OP_IfNot16, regBignull, sqlite3VdbeCurrentAddr(v)+3);
151283 VdbeComment((v, "If NULL-scan 2nd pass"));
151284 VdbeCoverage(v);
151285 }
151286 op = aEndOp[bRev*2 + endEq];
151287 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
151288 testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT );
151289 testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE );
151290 testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT );
151291 testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE );
151292 if( addrSeekScan ) sqlite3VdbeJumpHere(v, addrSeekScan);
151293 }
151294 if( regBignull ){
151295 /* During a NULL-scan, check to see if we have reached the end of
151296 ** the NULLs */
151297 assert( bSeekPastNull==!bStopAtNull )((void) (0));
151298 assert( bSeekPastNull+bStopAtNull==1 )((void) (0));
151299 assert( nConstraint+bSeekPastNull>0 )((void) (0));
151300 sqlite3VdbeAddOp2(v, OP_If15, regBignull, sqlite3VdbeCurrentAddr(v)+2);
151301 VdbeComment((v, "If NULL-scan 1st pass"));
151302 VdbeCoverage(v);
151303 op = aEndOp[bRev*2 + bSeekPastNull];
151304 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase,
151305 nConstraint+bSeekPastNull);
151306 testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT );
151307 testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE );
151308 testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT );
151309 testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE );
151310 }
151311
151312 if( (pLoop->wsFlags & WHERE_IN_EARLYOUT0x00040000)!=0 ){
151313 sqlite3VdbeAddOp3(v, OP_SeekHit125, iIdxCur, nEq, nEq);
151314 }
151315
151316 /* Seek the table cursor, if required */
151317 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY0x00000040)!=0
151318 && (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE0x0020|WHERE_RIGHT_JOIN0x1000))==0;
151319 if( omitTable ){
151320 /* pIdx is a covering index. No need to access the main table. */
151321 }else if( HasRowid(pIdx->pTable)(((pIdx->pTable)->tabFlags & 0x00000080)==0) ){
151322 codeDeferredSeek(pWInfo, pIdx, iCur, iIdxCur);
151323 }else if( iCur!=iIdxCur ){
151324 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
151325 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
151326 for(j=0; j<pPk->nKeyCol; j++){
151327 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
151328 sqlite3VdbeAddOp3(v, OP_Column94, iIdxCur, k, iRowidReg+j);
151329 }
151330 sqlite3VdbeAddOp4Int(v, OP_NotFound27, iCur, addrCont,
151331 iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
151332 }
151333
151334 if( pLevel->iLeftJoin==0 ){
151335 /* If pIdx is an index on one or more expressions, then look through
151336 ** all the expressions in pWInfo and try to transform matching expressions
151337 ** into reference to index columns. Also attempt to translate references
151338 ** to virtual columns in the table into references to (stored) columns
151339 ** of the index.
151340 **
151341 ** Do not do this for the RHS of a LEFT JOIN. This is because the
151342 ** expression may be evaluated after OP_NullRow has been executed on
151343 ** the cursor. In this case it is important to do the full evaluation,
151344 ** as the result of the expression may not be NULL, even if all table
151345 ** column values are. https://www.sqlite.org/src/info/7fa8049685b50b5a
151346 **
151347 ** Also, do not do this when processing one index an a multi-index
151348 ** OR clause, since the transformation will become invalid once we
151349 ** move forward to the next index.
151350 ** https://sqlite.org/src/info/4e8e4857d32d401f
151351 */
151352 if( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE0x0020|WHERE_RIGHT_JOIN0x1000))==0 ){
151353 whereIndexExprTrans(pIdx, iCur, iIdxCur, pWInfo);
151354 }
151355
151356 /* If a partial index is driving the loop, try to eliminate WHERE clause
151357 ** terms from the query that must be true due to the WHERE clause of
151358 ** the partial index.
151359 **
151360 ** 2019-11-02 ticket 623eff57e76d45f6: This optimization does not work
151361 ** for a LEFT JOIN.
151362 */
151363 if( pIdx->pPartIdxWhere ){
151364 whereApplyPartialIndexConstraints(pIdx->pPartIdxWhere, iCur, pWC);
151365 }
151366 }else{
151367 testcase( pIdx->pPartIdxWhere );
151368 /* The following assert() is not a requirement, merely an observation:
151369 ** The OR-optimization doesn't work for the right hand table of
151370 ** a LEFT JOIN: */
151371 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0 )((void) (0));
151372 }
151373
151374 /* Record the instruction used to terminate the loop. */
151375 if( pLoop->wsFlags & WHERE_ONEROW0x00001000 ){
151376 pLevel->op = OP_Noop184;
151377 }else if( bRev ){
151378 pLevel->op = OP_Prev37;
151379 }else{
151380 pLevel->op = OP_Next38;
151381 }
151382 pLevel->p1 = iIdxCur;
151383 pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED0x00010000)!=0 ? 1:0;
151384 if( (pLoop->wsFlags & WHERE_CONSTRAINT0x0000000f)==0 ){
151385 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP1;
151386 }else{
151387 assert( pLevel->p5==0 )((void) (0));
151388 }
151389 if( omitTable ) pIdx = 0;
151390 }else
151391
151392#ifndef SQLITE_OMIT_OR_OPTIMIZATION
151393 if( pLoop->wsFlags & WHERE_MULTI_OR0x00002000 ){
151394 /* Case 5: Two or more separately indexed terms connected by OR
151395 **
151396 ** Example:
151397 **
151398 ** CREATE TABLE t1(a,b,c,d);
151399 ** CREATE INDEX i1 ON t1(a);
151400 ** CREATE INDEX i2 ON t1(b);
151401 ** CREATE INDEX i3 ON t1(c);
151402 **
151403 ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
151404 **
151405 ** In the example, there are three indexed terms connected by OR.
151406 ** The top of the loop looks like this:
151407 **
151408 ** Null 1 # Zero the rowset in reg 1
151409 **
151410 ** Then, for each indexed term, the following. The arguments to
151411 ** RowSetTest are such that the rowid of the current row is inserted
151412 ** into the RowSet. If it is already present, control skips the
151413 ** Gosub opcode and jumps straight to the code generated by WhereEnd().
151414 **
151415 ** sqlite3WhereBegin(<term>)
151416 ** RowSetTest # Insert rowid into rowset
151417 ** Gosub 2 A
151418 ** sqlite3WhereEnd()
151419 **
151420 ** Following the above, code to terminate the loop. Label A, the target
151421 ** of the Gosub above, jumps to the instruction right after the Goto.
151422 **
151423 ** Null 1 # Zero the rowset in reg 1
151424 ** Goto B # The loop is finished.
151425 **
151426 ** A: <loop body> # Return data, whatever.
151427 **
151428 ** Return 2 # Jump back to the Gosub
151429 **
151430 ** B: <after the loop>
151431 **
151432 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
151433 ** use an ephemeral index instead of a RowSet to record the primary
151434 ** keys of the rows we have already seen.
151435 **
151436 */
151437 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
151438 SrcList *pOrTab; /* Shortened table list or OR-clause generation */
151439 Index *pCov = 0; /* Potential covering index (or NULL) */
151440 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
151441
151442 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
151443 int regRowset = 0; /* Register for RowSet object */
151444 int regRowid = 0; /* Register holding rowid */
151445 int iLoopBody = sqlite3VdbeMakeLabel(pParse);/* Start of loop body */
151446 int iRetInit; /* Address of regReturn init */
151447 int untestedTerms = 0; /* Some terms not completely tested */
151448 int ii; /* Loop counter */
151449 Expr *pAndExpr = 0; /* An ".. AND (...)" expression */
151450 Table *pTab = pTabItem->pTab;
151451
151452 pTerm = pLoop->aLTerm[0];
151453 assert( pTerm!=0 )((void) (0));
151454 assert( pTerm->eOperator & WO_OR )((void) (0));
151455 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 )((void) (0));
151456 pOrWc = &pTerm->u.pOrInfo->wc;
151457 pLevel->op = OP_Return67;
151458 pLevel->p1 = regReturn;
151459
151460 /* Set up a new SrcList in pOrTab containing the table being scanned
151461 ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
151462 ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
151463 */
151464 if( pWInfo->nLevel>1 ){
151465 int nNotReady; /* The number of notReady tables */
151466 SrcItem *origSrc; /* Original list of tables */
151467 nNotReady = pWInfo->nLevel - iLevel - 1;
151468 pOrTab = sqlite3StackAllocRaw(db,sqlite3DbMallocRaw(db,sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab
->a[0]))
151469 sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]))sqlite3DbMallocRaw(db,sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab
->a[0]))
;
151470 if( pOrTab==0 ) return notReady;
151471 pOrTab->nAlloc = (u8)(nNotReady + 1);
151472 pOrTab->nSrc = pOrTab->nAlloc;
151473 memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
151474 origSrc = pWInfo->pTabList->a;
151475 for(k=1; k<=nNotReady; k++){
151476 memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
151477 }
151478 }else{
151479 pOrTab = pWInfo->pTabList;
151480 }
151481
151482 /* Initialize the rowset register to contain NULL. An SQL NULL is
151483 ** equivalent to an empty rowset. Or, create an ephemeral index
151484 ** capable of holding primary keys in the case of a WITHOUT ROWID.
151485 **
151486 ** Also initialize regReturn to contain the address of the instruction
151487 ** immediately following the OP_Return at the bottom of the loop. This
151488 ** is required in a few obscure LEFT JOIN cases where control jumps
151489 ** over the top of the loop into the body of it. In this case the
151490 ** correct response for the end-of-loop code (the OP_Return) is to
151491 ** fall through to the next instruction, just as an OP_Next does if
151492 ** called on an uninitialized cursor.
151493 */
151494 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK0x0010)==0 ){
151495 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
151496 regRowset = ++pParse->nMem;
151497 sqlite3VdbeAddOp2(v, OP_Null75, 0, regRowset);
151498 }else{
151499 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
151500 regRowset = pParse->nTab++;
151501 sqlite3VdbeAddOp2(v, OP_OpenEphemeral118, regRowset, pPk->nKeyCol);
151502 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
151503 }
151504 regRowid = ++pParse->nMem;
151505 }
151506 iRetInit = sqlite3VdbeAddOp2(v, OP_Integer71, 0, regReturn);
151507
151508 /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y
151509 ** Then for every term xN, evaluate as the subexpression: xN AND y
151510 ** That way, terms in y that are factored into the disjunction will
151511 ** be picked up by the recursive calls to sqlite3WhereBegin() below.
151512 **
151513 ** Actually, each subexpression is converted to "xN AND w" where w is
151514 ** the "interesting" terms of z - terms that did not originate in the
151515 ** ON or USING clause of a LEFT JOIN, and terms that are usable as
151516 ** indices.
151517 **
151518 ** This optimization also only applies if the (x1 OR x2 OR ...) term
151519 ** is not contained in the ON clause of a LEFT JOIN.
151520 ** See ticket http://www.sqlite.org/src/info/f2369304e4
151521 **
151522 ** 2022-02-04: Do not push down slices of a row-value comparison.
151523 ** In other words, "w" or "y" may not be a slice of a vector. Otherwise,
151524 ** the initialization of the right-hand operand of the vector comparison
151525 ** might not occur, or might occur only in an OR branch that is not
151526 ** taken. dbsqlfuzz 80a9fade844b4fb43564efc972bcb2c68270f5d1.
151527 **
151528 ** 2022-03-03: Do not push down expressions that involve subqueries.
151529 ** The subquery might get coded as a subroutine. Any table-references
151530 ** in the subquery might be resolved to index-references for the index on
151531 ** the OR branch in which the subroutine is coded. But if the subroutine
151532 ** is invoked from a different OR branch that uses a different index, such
151533 ** index-references will not work. tag-20220303a
151534 ** https://sqlite.org/forum/forumpost/36937b197273d403
151535 */
151536 if( pWC->nTerm>1 ){
151537 int iTerm;
151538 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
151539 Expr *pExpr = pWC->a[iTerm].pExpr;
151540 if( &pWC->a[iTerm] == pTerm ) continue;
151541 testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
151542 testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
151543 testcase( pWC->a[iTerm].wtFlags & TERM_SLICE );
151544 if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL0x0002|TERM_CODED0x0004|TERM_SLICE0x8000))!=0 ){
151545 continue;
151546 }
151547 if( (pWC->a[iTerm].eOperator & WO_ALL0x3fff)==0 ) continue;
151548 if( ExprHasProperty(pExpr, EP_Subquery)(((pExpr)->flags&(0x400000))!=0) ) continue; /* tag-20220303a */
151549 pExpr = sqlite3ExprDup(db, pExpr, 0);
151550 pAndExpr = sqlite3ExprAnd(pParse, pAndExpr, pExpr);
151551 }
151552 if( pAndExpr ){
151553 /* The extra 0x10000 bit on the opcode is masked off and does not
151554 ** become part of the new Expr.op. However, it does make the
151555 ** op==TK_AND comparison inside of sqlite3PExpr() false, and this
151556 ** prevents sqlite3PExpr() from applying the AND short-circuit
151557 ** optimization, which we do not want here. */
151558 pAndExpr = sqlite3PExpr(pParse, TK_AND44|0x10000, 0, pAndExpr);
151559 }
151560 }
151561
151562 /* Run a separate WHERE clause for each term of the OR clause. After
151563 ** eliminating duplicates from other WHERE clauses, the action for each
151564 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
151565 */
151566 ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR"))sqlite3VdbeExplain (pParse, 1, "MULTI-INDEX OR");
151567 for(ii=0; ii<pOrWc->nTerm; ii++){
151568 WhereTerm *pOrTerm = &pOrWc->a[ii];
151569 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND0x0400)!=0 ){
151570 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
151571 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
151572 Expr *pDelete; /* Local copy of OR clause term */
151573 int jmp1 = 0; /* Address of jump operation */
151574 testcase( (pTabItem[0].fg.jointype & JT_LEFT)!=0
151575 && !ExprHasProperty(pOrExpr, EP_OuterON)
151576 ); /* See TH3 vtab25.400 and ticket 614b25314c766238 */
151577 pDelete = pOrExpr = sqlite3ExprDup(db, pOrExpr, 0);
151578 if( db->mallocFailed ){
151579 sqlite3ExprDelete(db, pDelete);
151580 continue;
151581 }
151582 if( pAndExpr ){
151583 pAndExpr->pLeft = pOrExpr;
151584 pOrExpr = pAndExpr;
151585 }
151586 /* Loop through table entries that match term pOrTerm. */
151587 ExplainQueryPlan((pParse, 1, "INDEX %d", ii+1))sqlite3VdbeExplain (pParse, 1, "INDEX %d", ii+1);
151588 WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
151589 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, 0,
151590 WHERE_OR_SUBCLAUSE0x0020, iCovCur);
151591 assert( pSubWInfo || pParse->nErr )((void) (0));
151592 if( pSubWInfo ){
151593 WhereLoop *pSubLoop;
151594 int addrExplain = sqlite3WhereExplainOneScan(
151595 pParse, pOrTab, &pSubWInfo->a[0], 0
151596 );
151597 sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
151598
151599 /* This is the sub-WHERE clause body. First skip over
151600 ** duplicate rows from prior sub-WHERE clauses, and record the
151601 ** rowid (or PRIMARY KEY) for the current row so that the same
151602 ** row will be skipped in subsequent sub-WHERE clauses.
151603 */
151604 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK0x0010)==0 ){
151605 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
151606 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
151607 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, regRowid);
151608 jmp1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest46, regRowset, 0,
151609 regRowid, iSet);
151610 VdbeCoverage(v);
151611 }else{
151612 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
151613 int nPk = pPk->nKeyCol;
151614 int iPk;
151615 int r;
151616
151617 /* Read the PK into an array of temp registers. */
151618 r = sqlite3GetTempRange(pParse, nPk);
151619 for(iPk=0; iPk<nPk; iPk++){
151620 int iCol = pPk->aiColumn[iPk];
151621 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol,r+iPk);
151622 }
151623
151624 /* Check if the temp table already contains this key. If so,
151625 ** the row has already been included in the result set and
151626 ** can be ignored (by jumping past the Gosub below). Otherwise,
151627 ** insert the key into the temp table and proceed with processing
151628 ** the row.
151629 **
151630 ** Use some of the same optimizations as OP_RowSetTest: If iSet
151631 ** is zero, assume that the key cannot already be present in
151632 ** the temp table. And if iSet is -1, assume that there is no
151633 ** need to insert the key into the temp table, as it will never
151634 ** be tested for. */
151635 if( iSet ){
151636 jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found28, regRowset, 0, r, nPk);
151637 VdbeCoverage(v);
151638 }
151639 if( iSet>=0 ){
151640 sqlite3VdbeAddOp3(v, OP_MakeRecord97, r, nPk, regRowid);
151641 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, regRowset, regRowid,
151642 r, nPk);
151643 if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT0x10);
151644 }
151645
151646 /* Release the array of temp registers */
151647 sqlite3ReleaseTempRange(pParse, r, nPk);
151648 }
151649 }
151650
151651 /* Invoke the main loop body as a subroutine */
151652 sqlite3VdbeAddOp2(v, OP_Gosub9, regReturn, iLoopBody);
151653
151654 /* Jump here (skipping the main loop body subroutine) if the
151655 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
151656 if( jmp1 ) sqlite3VdbeJumpHere(v, jmp1);
151657
151658 /* The pSubWInfo->untestedTerms flag means that this OR term
151659 ** contained one or more AND term from a notReady table. The
151660 ** terms from the notReady table could not be tested and will
151661 ** need to be tested later.
151662 */
151663 if( pSubWInfo->untestedTerms ) untestedTerms = 1;
151664
151665 /* If all of the OR-connected terms are optimized using the same
151666 ** index, and the index is opened using the same cursor number
151667 ** by each call to sqlite3WhereBegin() made by this loop, it may
151668 ** be possible to use that index as a covering index.
151669 **
151670 ** If the call to sqlite3WhereBegin() above resulted in a scan that
151671 ** uses an index, and this is either the first OR-connected term
151672 ** processed or the index is the same as that used by all previous
151673 ** terms, set pCov to the candidate covering index. Otherwise, set
151674 ** pCov to NULL to indicate that no candidate covering index will
151675 ** be available.
151676 */
151677 pSubLoop = pSubWInfo->a[0].pWLoop;
151678 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 )((void) (0));
151679 if( (pSubLoop->wsFlags & WHERE_INDEXED0x00000200)!=0
151680 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
151681 && (HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex)((pSubLoop->u.btree.pIndex)->idxType==2))
151682 ){
151683 assert( pSubWInfo->a[0].iIdxCur==iCovCur )((void) (0));
151684 pCov = pSubLoop->u.btree.pIndex;
151685 }else{
151686 pCov = 0;
151687 }
151688 if( sqlite3WhereUsesDeferredSeek(pSubWInfo) ){
151689 pWInfo->bDeferredSeek = 1;
151690 }
151691
151692 /* Finish the loop through table entries that match term pOrTerm. */
151693 sqlite3WhereEnd(pSubWInfo);
151694 ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
151695 }
151696 sqlite3ExprDelete(db, pDelete);
151697 }
151698 }
151699 ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
151700 assert( pLevel->pWLoop==pLoop )((void) (0));
151701 assert( (pLoop->wsFlags & WHERE_MULTI_OR)!=0 )((void) (0));
151702 assert( (pLoop->wsFlags & WHERE_IN_ABLE)==0 )((void) (0));
151703 pLevel->u.pCoveringIdx = pCov;
151704 if( pCov ) pLevel->iIdxCur = iCovCur;
151705 if( pAndExpr ){
151706 pAndExpr->pLeft = 0;
151707 sqlite3ExprDelete(db, pAndExpr);
151708 }
151709 sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
151710 sqlite3VdbeGoto(v, pLevel->addrBrk);
151711 sqlite3VdbeResolveLabel(v, iLoopBody);
151712
151713 /* Set the P2 operand of the OP_Return opcode that will end the current
151714 ** loop to point to this spot, which is the top of the next containing
151715 ** loop. The byte-code formatter will use that P2 value as a hint to
151716 ** indent everything in between the this point and the final OP_Return.
151717 ** See tag-20220407a in vdbe.c and shell.c */
151718 assert( pLevel->op==OP_Return )((void) (0));
151719 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
151720
151721 if( pWInfo->nLevel>1 ){ sqlite3StackFree(db, pOrTab)sqlite3DbFree(db,pOrTab); }
151722 if( !untestedTerms ) disableTerm(pLevel, pTerm);
151723 }else
151724#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
151725
151726 {
151727 /* Case 6: There is no usable index. We must do a complete
151728 ** scan of the entire table.
151729 */
151730 static const u8 aStep[] = { OP_Next38, OP_Prev37 };
151731 static const u8 aStart[] = { OP_Rewind35, OP_Last31 };
151732 assert( bRev==0 || bRev==1 )((void) (0));
151733 if( pTabItem->fg.isRecursive ){
151734 /* Tables marked isRecursive have only a single row that is stored in
151735 ** a pseudo-cursor. No need to Rewind or Next such cursors. */
151736 pLevel->op = OP_Noop184;
151737 }else{
151738 codeCursorHint(pTabItem, pWInfo, pLevel, 0);
151739 pLevel->op = aStep[bRev];
151740 pLevel->p1 = iCur;
151741 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrHalt);
151742 VdbeCoverageIf(v, bRev==0);
151743 VdbeCoverageIf(v, bRev!=0);
151744 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP1;
151745 }
151746 }
151747
151748#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
151749 pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
151750#endif
151751
151752 /* Insert code to test every subexpression that can be completely
151753 ** computed using the current set of tables.
151754 **
151755 ** This loop may run between one and three times, depending on the
151756 ** constraints to be generated. The value of stack variable iLoop
151757 ** determines the constraints coded by each iteration, as follows:
151758 **
151759 ** iLoop==1: Code only expressions that are entirely covered by pIdx.
151760 ** iLoop==2: Code remaining expressions that do not contain correlated
151761 ** sub-queries.
151762 ** iLoop==3: Code all remaining expressions.
151763 **
151764 ** An effort is made to skip unnecessary iterations of the loop.
151765 */
151766 iLoop = (pIdx ? 1 : 2);
151767 do{
151768 int iNext = 0; /* Next value for iLoop */
151769 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
151770 Expr *pE;
151771 int skipLikeAddr = 0;
151772 testcase( pTerm->wtFlags & TERM_VIRTUAL );
151773 testcase( pTerm->wtFlags & TERM_CODED );
151774 if( pTerm->wtFlags & (TERM_VIRTUAL0x0002|TERM_CODED0x0004) ) continue;
151775 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
151776 testcase( pWInfo->untestedTerms==0
151777 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 );
151778 pWInfo->untestedTerms = 1;
151779 continue;
151780 }
151781 pE = pTerm->pExpr;
151782 assert( pE!=0 )((void) (0));
151783 if( pTabItem->fg.jointype & (JT_LEFT0x08|JT_LTORJ0x40|JT_RIGHT0x10) ){
151784 if( !ExprHasProperty(pE,EP_OuterON|EP_InnerON)(((pE)->flags&(0x000001|0x000002))!=0) ){
151785 /* Defer processing WHERE clause constraints until after outer
151786 ** join processing. tag-20220513a */
151787 continue;
151788 }else if( (pTabItem->fg.jointype & JT_LEFT0x08)==JT_LEFT0x08
151789 && !ExprHasProperty(pE,EP_OuterON)(((pE)->flags&(0x000001))!=0) ){
151790 continue;
151791 }else{
151792 Bitmask m = sqlite3WhereGetMask(&pWInfo->sMaskSet, pE->w.iJoin);
151793 if( m & pLevel->notReady ){
151794 /* An ON clause that is not ripe */
151795 continue;
151796 }
151797 }
151798 }
151799 if( iLoop==1 && !sqlite3ExprCoveredByIndex(pE, pLevel->iTabCur, pIdx) ){
151800 iNext = 2;
151801 continue;
151802 }
151803 if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT0x1000) ){
151804 if( iNext==0 ) iNext = 3;
151805 continue;
151806 }
151807
151808 if( (pTerm->wtFlags & TERM_LIKECOND0x0200)!=0 ){
151809 /* If the TERM_LIKECOND flag is set, that means that the range search
151810 ** is sufficient to guarantee that the LIKE operator is true, so we
151811 ** can skip the call to the like(A,B) function. But this only works
151812 ** for strings. So do not skip the call to the function on the pass
151813 ** that compares BLOBs. */
151814#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
151815 continue;
151816#else
151817 u32 x = pLevel->iLikeRepCntr;
151818 if( x>0 ){
151819 skipLikeAddr = sqlite3VdbeAddOp1(v, (x&1)?OP_IfNot16:OP_If15,(int)(x>>1));
151820 VdbeCoverageIf(v, (x&1)==1);
151821 VdbeCoverageIf(v, (x&1)==0);
151822 }
151823#endif
151824 }
151825#ifdef WHERETRACE_ENABLED /* 0xffff */
151826 if( sqlite3WhereTrace ){
151827 VdbeNoopComment((v, "WhereTerm[%d] (%p) priority=%d",
151828 pWC->nTerm-j, pTerm, iLoop));
151829 }
151830 if( sqlite3WhereTrace & 0x800 ){
151831 sqlite3DebugPrintf("Coding auxiliary constraint:\n");
151832 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
151833 }
151834#endif
151835 sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL0x10);
151836 if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr);
151837 pTerm->wtFlags |= TERM_CODED0x0004;
151838 }
151839 iLoop = iNext;
151840 }while( iLoop>0 );
151841
151842 /* Insert code to test for implied constraints based on transitivity
151843 ** of the "==" operator.
151844 **
151845 ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
151846 ** and we are coding the t1 loop and the t2 loop has not yet coded,
151847 ** then we cannot use the "t1.a=t2.b" constraint, but we can code
151848 ** the implied "t1.a=123" constraint.
151849 */
151850 for(pTerm=pWC->a, j=pWC->nBase; j>0; j--, pTerm++){
151851 Expr *pE, sEAlt;
151852 WhereTerm *pAlt;
151853 if( pTerm->wtFlags & (TERM_VIRTUAL0x0002|TERM_CODED0x0004) ) continue;
151854 if( (pTerm->eOperator & (WO_EQ0x0002|WO_IS0x0080))==0 ) continue;
151855 if( (pTerm->eOperator & WO_EQUIV0x0800)==0 ) continue;
151856 if( pTerm->leftCursor!=iCur ) continue;
151857 if( pTabItem->fg.jointype & (JT_LEFT0x08|JT_LTORJ0x40|JT_RIGHT0x10) ) continue;
151858 pE = pTerm->pExpr;
151859#ifdef WHERETRACE_ENABLED /* 0x800 */
151860 if( sqlite3WhereTrace & 0x800 ){
151861 sqlite3DebugPrintf("Coding transitive constraint:\n");
151862 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
151863 }
151864#endif
151865 assert( !ExprHasProperty(pE, EP_OuterON) )((void) (0));
151866 assert( (pTerm->prereqRight & pLevel->notReady)!=0 )((void) (0));
151867 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
151868 pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.x.leftColumn, notReady,
151869 WO_EQ0x0002|WO_IN0x0001|WO_IS0x0080, 0);
151870 if( pAlt==0 ) continue;
151871 if( pAlt->wtFlags & (TERM_CODED0x0004) ) continue;
151872 if( (pAlt->eOperator & WO_IN0x0001)
151873 && ExprUseXSelect(pAlt->pExpr)(((pAlt->pExpr)->flags&0x001000)!=0)
151874 && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
151875 ){
151876 continue;
151877 }
151878 testcase( pAlt->eOperator & WO_EQ );
151879 testcase( pAlt->eOperator & WO_IS );
151880 testcase( pAlt->eOperator & WO_IN );
151881 VdbeModuleComment((v, "begin transitive constraint"));
151882 sEAlt = *pAlt->pExpr;
151883 sEAlt.pLeft = pE->pLeft;
151884 sqlite3ExprIfFalse(pParse, &sEAlt, addrCont, SQLITE_JUMPIFNULL0x10);
151885 pAlt->wtFlags |= TERM_CODED0x0004;
151886 }
151887
151888 /* For a RIGHT OUTER JOIN, record the fact that the current row has
151889 ** been matched at least once.
151890 */
151891 if( pLevel->pRJ ){
151892 Table *pTab;
151893 int nPk;
151894 int r;
151895 int jmp1 = 0;
151896 WhereRightJoin *pRJ = pLevel->pRJ;
151897
151898 /* pTab is the right-hand table of the RIGHT JOIN. Generate code that
151899 ** will record that the current row of that table has been matched at
151900 ** least once. This is accomplished by storing the PK for the row in
151901 ** both the iMatch index and the regBloom Bloom filter.
151902 */
151903 pTab = pWInfo->pTabList->a[pLevel->iFrom].pTab;
151904 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
151905 r = sqlite3GetTempRange(pParse, 2);
151906 sqlite3ExprCodeGetColumnOfTable(v, pTab, pLevel->iTabCur, -1, r+1);
151907 nPk = 1;
151908 }else{
151909 int iPk;
151910 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
151911 nPk = pPk->nKeyCol;
151912 r = sqlite3GetTempRange(pParse, nPk+1);
151913 for(iPk=0; iPk<nPk; iPk++){
151914 int iCol = pPk->aiColumn[iPk];
151915 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol,r+1+iPk);
151916 }
151917 }
151918 jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found28, pRJ->iMatch, 0, r+1, nPk);
151919 VdbeCoverage(v);
151920 VdbeComment((v, "match against %s", pTab->zName));
151921 sqlite3VdbeAddOp3(v, OP_MakeRecord97, r+1, nPk, r);
151922 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, pRJ->iMatch, r, r+1, nPk);
151923 sqlite3VdbeAddOp4Int(v, OP_FilterAdd180, pRJ->regBloom, 0, r+1, nPk);
151924 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT0x10);
151925 sqlite3VdbeJumpHere(v, jmp1);
151926 sqlite3ReleaseTempRange(pParse, r, nPk+1);
151927 }
151928
151929 /* For a LEFT OUTER JOIN, generate code that will record the fact that
151930 ** at least one row of the right table has matched the left table.
151931 */
151932 if( pLevel->iLeftJoin ){
151933 pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
151934 sqlite3VdbeAddOp2(v, OP_Integer71, 1, pLevel->iLeftJoin);
151935 VdbeComment((v, "record LEFT JOIN hit"));
151936 if( pLevel->pRJ==0 ){
151937 goto code_outer_join_constraints; /* WHERE clause constraints */
151938 }
151939 }
151940
151941 if( pLevel->pRJ ){
151942 /* Create a subroutine used to process all interior loops and code
151943 ** of the RIGHT JOIN. During normal operation, the subroutine will
151944 ** be in-line with the rest of the code. But at the end, a separate
151945 ** loop will run that invokes this subroutine for unmatched rows
151946 ** of pTab, with all tables to left begin set to NULL.
151947 */
151948 WhereRightJoin *pRJ = pLevel->pRJ;
151949 sqlite3VdbeAddOp2(v, OP_BeginSubrtn74, 0, pRJ->regReturn);
151950 pRJ->addrSubrtn = sqlite3VdbeCurrentAddr(v);
151951 assert( pParse->withinRJSubrtn < 255 )((void) (0));
151952 pParse->withinRJSubrtn++;
151953
151954 /* WHERE clause constraints must be deferred until after outer join
151955 ** row elimination has completed, since WHERE clause constraints apply
151956 ** to the results of the OUTER JOIN. The following loop generates the
151957 ** appropriate WHERE clause constraint checks. tag-20220513a.
151958 */
151959 code_outer_join_constraints:
151960 for(pTerm=pWC->a, j=0; j<pWC->nBase; j++, pTerm++){
151961 testcase( pTerm->wtFlags & TERM_VIRTUAL );
151962 testcase( pTerm->wtFlags & TERM_CODED );
151963 if( pTerm->wtFlags & (TERM_VIRTUAL0x0002|TERM_CODED0x0004) ) continue;
151964 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
151965 assert( pWInfo->untestedTerms )((void) (0));
151966 continue;
151967 }
151968 if( pTabItem->fg.jointype & JT_LTORJ0x40 ) continue;
151969 assert( pTerm->pExpr )((void) (0));
151970 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL0x10);
151971 pTerm->wtFlags |= TERM_CODED0x0004;
151972 }
151973 }
151974
151975#if WHERETRACE_ENABLED /* 0x20800 */
151976 if( sqlite3WhereTrace & 0x20000 ){
151977 sqlite3DebugPrintf("All WHERE-clause terms after coding level %d:\n",
151978 iLevel);
151979 sqlite3WhereClausePrint(pWC);
151980 }
151981 if( sqlite3WhereTrace & 0x800 ){
151982 sqlite3DebugPrintf("End Coding level %d: notReady=%llx\n",
151983 iLevel, (u64)pLevel->notReady);
151984 }
151985#endif
151986 return pLevel->notReady;
151987}
151988
151989/*
151990** Generate the code for the loop that finds all non-matched terms
151991** for a RIGHT JOIN.
151992*/
151993SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) void sqlite3WhereRightJoinLoop(
151994 WhereInfo *pWInfo,
151995 int iLevel,
151996 WhereLevel *pLevel
151997){
151998 Parse *pParse = pWInfo->pParse;
151999 Vdbe *v = pParse->pVdbe;
152000 WhereRightJoin *pRJ = pLevel->pRJ;
152001 Expr *pSubWhere = 0;
152002 WhereClause *pWC = &pWInfo->sWC;
152003 WhereInfo *pSubWInfo;
152004 WhereLoop *pLoop = pLevel->pWLoop;
152005 SrcItem *pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
152006 SrcList sFrom;
152007 Bitmask mAll = 0;
152008 int k;
152009
152010 ExplainQueryPlan((pParse, 1, "RIGHT-JOIN %s", pTabItem->pTab->zName))sqlite3VdbeExplain (pParse, 1, "RIGHT-JOIN %s", pTabItem->
pTab->zName)
;
152011 sqlite3VdbeNoJumpsOutsideSubrtn(v, pRJ->addrSubrtn, pRJ->endSubrtn,
152012 pRJ->regReturn);
152013 for(k=0; k<iLevel; k++){
152014 int iIdxCur;
152015 mAll |= pWInfo->a[k].pWLoop->maskSelf;
152016 sqlite3VdbeAddOp1(v, OP_NullRow136, pWInfo->a[k].iTabCur);
152017 iIdxCur = pWInfo->a[k].iIdxCur;
152018 if( iIdxCur ){
152019 sqlite3VdbeAddOp1(v, OP_NullRow136, iIdxCur);
152020 }
152021 }
152022 if( (pTabItem->fg.jointype & JT_LTORJ0x40)==0 ){
152023 mAll |= pLoop->maskSelf;
152024 for(k=0; k<pWC->nTerm; k++){
152025 WhereTerm *pTerm = &pWC->a[k];
152026 if( (pTerm->wtFlags & (TERM_VIRTUAL0x0002|TERM_SLICE0x8000))!=0
152027 && pTerm->eOperator!=WO_ROWVAL0x2000
152028 ){
152029 break;
152030 }
152031 if( pTerm->prereqAll & ~mAll ) continue;
152032 if( ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON)(((pTerm->pExpr)->flags&(0x000001|0x000002))!=0) ) continue;
152033 pSubWhere = sqlite3ExprAnd(pParse, pSubWhere,
152034 sqlite3ExprDup(pParse->db, pTerm->pExpr, 0));
152035 }
152036 }
152037 sFrom.nSrc = 1;
152038 sFrom.nAlloc = 1;
152039 memcpy(&sFrom.a[0], pTabItem, sizeof(SrcItem));
152040 sFrom.a[0].fg.jointype = 0;
152041 assert( pParse->withinRJSubrtn < 100 )((void) (0));
152042 pParse->withinRJSubrtn++;
152043 pSubWInfo = sqlite3WhereBegin(pParse, &sFrom, pSubWhere, 0, 0, 0,
152044 WHERE_RIGHT_JOIN0x1000, 0);
152045 if( pSubWInfo ){
152046 int iCur = pLevel->iTabCur;
152047 int r = ++pParse->nMem;
152048 int nPk;
152049 int jmp;
152050 int addrCont = sqlite3WhereContinueLabel(pSubWInfo);
152051 Table *pTab = pTabItem->pTab;
152052 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
152053 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, r);
152054 nPk = 1;
152055 }else{
152056 int iPk;
152057 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
152058 nPk = pPk->nKeyCol;
152059 pParse->nMem += nPk - 1;
152060 for(iPk=0; iPk<nPk; iPk++){
152061 int iCol = pPk->aiColumn[iPk];
152062 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol,r+iPk);
152063 }
152064 }
152065 jmp = sqlite3VdbeAddOp4Int(v, OP_Filter63, pRJ->regBloom, 0, r, nPk);
152066 VdbeCoverage(v);
152067 sqlite3VdbeAddOp4Int(v, OP_Found28, pRJ->iMatch, addrCont, r, nPk);
152068 VdbeCoverage(v);
152069 sqlite3VdbeJumpHere(v, jmp);
152070 sqlite3VdbeAddOp2(v, OP_Gosub9, pRJ->regReturn, pRJ->addrSubrtn);
152071 sqlite3WhereEnd(pSubWInfo);
152072 }
152073 sqlite3ExprDelete(pParse->db, pSubWhere);
152074 ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
152075 assert( pParse->withinRJSubrtn>0 )((void) (0));
152076 pParse->withinRJSubrtn--;
152077}
152078
152079/************** End of wherecode.c *******************************************/
152080/************** Begin file whereexpr.c ***************************************/
152081/*
152082** 2015-06-08
152083**
152084** The author disclaims copyright to this source code. In place of
152085** a legal notice, here is a blessing:
152086**
152087** May you do good and not evil.
152088** May you find forgiveness for yourself and forgive others.
152089** May you share freely, never taking more than you give.
152090**
152091*************************************************************************
152092** This module contains C code that generates VDBE code used to process
152093** the WHERE clause of SQL statements.
152094**
152095** This file was originally part of where.c but was split out to improve
152096** readability and editabiliity. This file contains utility routines for
152097** analyzing Expr objects in the WHERE clause.
152098*/
152099/* #include "sqliteInt.h" */
152100/* #include "whereInt.h" */
152101
152102/* Forward declarations */
152103static void exprAnalyze(SrcList*, WhereClause*, int);
152104
152105/*
152106** Deallocate all memory associated with a WhereOrInfo object.
152107*/
152108static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
152109 sqlite3WhereClauseClear(&p->wc);
152110 sqlite3DbFree(db, p);
152111}
152112
152113/*
152114** Deallocate all memory associated with a WhereAndInfo object.
152115*/
152116static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
152117 sqlite3WhereClauseClear(&p->wc);
152118 sqlite3DbFree(db, p);
152119}
152120
152121/*
152122** Add a single new WhereTerm entry to the WhereClause object pWC.
152123** The new WhereTerm object is constructed from Expr p and with wtFlags.
152124** The index in pWC->a[] of the new WhereTerm is returned on success.
152125** 0 is returned if the new WhereTerm could not be added due to a memory
152126** allocation error. The memory allocation failure will be recorded in
152127** the db->mallocFailed flag so that higher-level functions can detect it.
152128**
152129** This routine will increase the size of the pWC->a[] array as necessary.
152130**
152131** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
152132** for freeing the expression p is assumed by the WhereClause object pWC.
152133** This is true even if this routine fails to allocate a new WhereTerm.
152134**
152135** WARNING: This routine might reallocate the space used to store
152136** WhereTerms. All pointers to WhereTerms should be invalidated after
152137** calling this routine. Such pointers may be reinitialized by referencing
152138** the pWC->a[] array.
152139*/
152140static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){
152141 WhereTerm *pTerm;
152142 int idx;
152143 testcase( wtFlags & TERM_VIRTUAL );
152144 if( pWC->nTerm>=pWC->nSlot ){
152145 WhereTerm *pOld = pWC->a;
152146 sqlite3 *db = pWC->pWInfo->pParse->db;
152147 pWC->a = sqlite3WhereMalloc(pWC->pWInfo, sizeof(pWC->a[0])*pWC->nSlot*2 );
152148 if( pWC->a==0 ){
152149 if( wtFlags & TERM_DYNAMIC0x0001 ){
152150 sqlite3ExprDelete(db, p);
152151 }
152152 pWC->a = pOld;
152153 return 0;
152154 }
152155 memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
152156 pWC->nSlot = pWC->nSlot*2;
152157 }
152158 pTerm = &pWC->a[idx = pWC->nTerm++];
152159 if( (wtFlags & TERM_VIRTUAL0x0002)==0 ) pWC->nBase = pWC->nTerm;
152160 if( p && ExprHasProperty(p, EP_Unlikely)(((p)->flags&(0x080000))!=0) ){
152161 pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
152162 }else{
152163 pTerm->truthProb = 1;
152164 }
152165 pTerm->pExpr = sqlite3ExprSkipCollateAndLikely(p);
152166 pTerm->wtFlags = wtFlags;
152167 pTerm->pWC = pWC;
152168 pTerm->iParent = -1;
152169 memset(&pTerm->eOperator, 0,
152170 sizeof(WhereTerm) - offsetof(WhereTerm,eOperator)__builtin_offsetof(WhereTerm, eOperator));
152171 return idx;
152172}
152173
152174/*
152175** Return TRUE if the given operator is one of the operators that is
152176** allowed for an indexable WHERE clause term. The allowed operators are
152177** "=", "<", ">", "<=", ">=", "IN", "IS", and "IS NULL"
152178*/
152179static int allowedOp(int op){
152180 assert( TK_GT>TK_EQ && TK_GT<TK_GE )((void) (0));
152181 assert( TK_LT>TK_EQ && TK_LT<TK_GE )((void) (0));
152182 assert( TK_LE>TK_EQ && TK_LE<TK_GE )((void) (0));
152183 assert( TK_GE==TK_EQ+4 )((void) (0));
152184 return op==TK_IN49 || (op>=TK_EQ53 && op<=TK_GE57) || op==TK_ISNULL50 || op==TK_IS45;
152185}
152186
152187/*
152188** Commute a comparison operator. Expressions of the form "X op Y"
152189** are converted into "Y op X".
152190*/
152191static u16 exprCommute(Parse *pParse, Expr *pExpr){
152192 if( pExpr->pLeft->op==TK_VECTOR177
152193 || pExpr->pRight->op==TK_VECTOR177
152194 || sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight) !=
152195 sqlite3BinaryCompareCollSeq(pParse, pExpr->pRight, pExpr->pLeft)
152196 ){
152197 pExpr->flags ^= EP_Commuted0x000400;
152198 }
152199 SWAP(Expr*,pExpr->pRight,pExpr->pLeft){Expr* t=pExpr->pRight; pExpr->pRight=pExpr->pLeft; pExpr
->pLeft=t;}
;
152200 if( pExpr->op>=TK_GT54 ){
152201 assert( TK_LT==TK_GT+2 )((void) (0));
152202 assert( TK_GE==TK_LE+2 )((void) (0));
152203 assert( TK_GT>TK_EQ )((void) (0));
152204 assert( TK_GT<TK_LE )((void) (0));
152205 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE )((void) (0));
152206 pExpr->op = ((pExpr->op-TK_GT54)^2)+TK_GT54;
152207 }
152208 return 0;
152209}
152210
152211/*
152212** Translate from TK_xx operator to WO_xx bitmask.
152213*/
152214static u16 operatorMask(int op){
152215 u16 c;
152216 assert( allowedOp(op) )((void) (0));
152217 if( op==TK_IN49 ){
152218 c = WO_IN0x0001;
152219 }else if( op==TK_ISNULL50 ){
152220 c = WO_ISNULL0x0100;
152221 }else if( op==TK_IS45 ){
152222 c = WO_IS0x0080;
152223 }else{
152224 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff )((void) (0));
152225 c = (u16)(WO_EQ0x0002<<(op-TK_EQ53));
152226 }
152227 assert( op!=TK_ISNULL || c==WO_ISNULL )((void) (0));
152228 assert( op!=TK_IN || c==WO_IN )((void) (0));
152229 assert( op!=TK_EQ || c==WO_EQ )((void) (0));
152230 assert( op!=TK_LT || c==WO_LT )((void) (0));
152231 assert( op!=TK_LE || c==WO_LE )((void) (0));
152232 assert( op!=TK_GT || c==WO_GT )((void) (0));
152233 assert( op!=TK_GE || c==WO_GE )((void) (0));
152234 assert( op!=TK_IS || c==WO_IS )((void) (0));
152235 return c;
152236}
152237
152238
152239#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
152240/*
152241** Check to see if the given expression is a LIKE or GLOB operator that
152242** can be optimized using inequality constraints. Return TRUE if it is
152243** so and false if not.
152244**
152245** In order for the operator to be optimizible, the RHS must be a string
152246** literal that does not begin with a wildcard. The LHS must be a column
152247** that may only be NULL, a string, or a BLOB, never a number. (This means
152248** that virtual tables cannot participate in the LIKE optimization.) The
152249** collating sequence for the column on the LHS must be appropriate for
152250** the operator.
152251*/
152252static int isLikeOrGlob(
152253 Parse *pParse, /* Parsing and code generating context */
152254 Expr *pExpr, /* Test this expression */
152255 Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */
152256 int *pisComplete, /* True if the only wildcard is % in the last character */
152257 int *pnoCase /* True if uppercase is equivalent to lowercase */
152258){
152259 const u8 *z = 0; /* String on RHS of LIKE operator */
152260 Expr *pRight, *pLeft; /* Right and left size of LIKE operator */
152261 ExprList *pList; /* List of operands to the LIKE operator */
152262 u8 c; /* One character in z[] */
152263 int cnt; /* Number of non-wildcard prefix characters */
152264 u8 wc[4]; /* Wildcard characters */
152265 sqlite3 *db = pParse->db; /* Database connection */
152266 sqlite3_value *pVal = 0;
152267 int op; /* Opcode of pRight */
152268 int rc; /* Result code to return */
152269
152270 if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, (char*)wc) ){
152271 return 0;
152272 }
152273#ifdef SQLITE_EBCDIC
152274 if( *pnoCase ) return 0;
152275#endif
152276 assert( ExprUseXList(pExpr) )((void) (0));
152277 pList = pExpr->x.pList;
152278 pLeft = pList->a[1].pExpr;
152279
152280 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
152281 op = pRight->op;
152282 if( op==TK_VARIABLE156 && (db->flags & SQLITE_EnableQPSG0x00800000)==0 ){
152283 Vdbe *pReprepare = pParse->pReprepare;
152284 int iCol = pRight->iColumn;
152285 pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB0x41);
152286 if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT3 ){
152287 z = sqlite3_value_text(pVal);
152288 }
152289 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
152290 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER )((void) (0));
152291 }else if( op==TK_STRING117 ){
152292 assert( !ExprHasProperty(pRight, EP_IntValue) )((void) (0));
152293 z = (u8*)pRight->u.zToken;
152294 }
152295 if( z ){
152296
152297 /* Count the number of prefix characters prior to the first wildcard */
152298 cnt = 0;
152299 while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
152300 cnt++;
152301 if( c==wc[3] && z[cnt]!=0 ) cnt++;
152302 }
152303
152304 /* The optimization is possible only if (1) the pattern does not begin
152305 ** with a wildcard and if (2) the non-wildcard prefix does not end with
152306 ** an (illegal 0xff) character, or (3) the pattern does not consist of
152307 ** a single escape character. The second condition is necessary so
152308 ** that we can increment the prefix key to find an upper bound for the
152309 ** range search. The third is because the caller assumes that the pattern
152310 ** consists of at least one character after all escapes have been
152311 ** removed. */
152312 if( cnt!=0 && 255!=(u8)z[cnt-1] && (cnt>1 || z[0]!=wc[3]) ){
152313 Expr *pPrefix;
152314
152315 /* A "complete" match if the pattern ends with "*" or "%" */
152316 *pisComplete = c==wc[0] && z[cnt+1]==0;
152317
152318 /* Get the pattern prefix. Remove all escapes from the prefix. */
152319 pPrefix = sqlite3Expr(db, TK_STRING117, (char*)z);
152320 if( pPrefix ){
152321 int iFrom, iTo;
152322 char *zNew;
152323 assert( !ExprHasProperty(pPrefix, EP_IntValue) )((void) (0));
152324 zNew = pPrefix->u.zToken;
152325 zNew[cnt] = 0;
152326 for(iFrom=iTo=0; iFrom<cnt; iFrom++){
152327 if( zNew[iFrom]==wc[3] ) iFrom++;
152328 zNew[iTo++] = zNew[iFrom];
152329 }
152330 zNew[iTo] = 0;
152331 assert( iTo>0 )((void) (0));
152332
152333 /* If the LHS is not an ordinary column with TEXT affinity, then the
152334 ** pattern prefix boundaries (both the start and end boundaries) must
152335 ** not look like a number. Otherwise the pattern might be treated as
152336 ** a number, which will invalidate the LIKE optimization.
152337 **
152338 ** Getting this right has been a persistent source of bugs in the
152339 ** LIKE optimization. See, for example:
152340 ** 2018-09-10 https://sqlite.org/src/info/c94369cae9b561b1
152341 ** 2019-05-02 https://sqlite.org/src/info/b043a54c3de54b28
152342 ** 2019-06-10 https://sqlite.org/src/info/fd76310a5e843e07
152343 ** 2019-06-14 https://sqlite.org/src/info/ce8717f0885af975
152344 ** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a
152345 */
152346 if( pLeft->op!=TK_COLUMN167
152347 || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT0x42
152348 || (ALWAYS( ExprUseYTab(pLeft) )((((pLeft)->flags&(0x1000000|0x2000000))==0))
152349 && pLeft->y.pTab
152350 && IsVirtual(pLeft->y.pTab)((pLeft->y.pTab)->eTabType==1)) /* Might be numeric */
152351 ){
152352 int isNum;
152353 double rDummy;
152354 isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF81);
152355 if( isNum<=0 ){
152356 if( iTo==1 && zNew[0]=='-' ){
152357 isNum = +1;
152358 }else{
152359 zNew[iTo-1]++;
152360 isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF81);
152361 zNew[iTo-1]--;
152362 }
152363 }
152364 if( isNum>0 ){
152365 sqlite3ExprDelete(db, pPrefix);
152366 sqlite3ValueFree(pVal);
152367 return 0;
152368 }
152369 }
152370 }
152371 *ppPrefix = pPrefix;
152372
152373 /* If the RHS pattern is a bound parameter, make arrangements to
152374 ** reprepare the statement when that parameter is rebound */
152375 if( op==TK_VARIABLE156 ){
152376 Vdbe *v = pParse->pVdbe;
152377 sqlite3VdbeSetVarmask(v, pRight->iColumn);
152378 assert( !ExprHasProperty(pRight, EP_IntValue) )((void) (0));
152379 if( *pisComplete && pRight->u.zToken[1] ){
152380 /* If the rhs of the LIKE expression is a variable, and the current
152381 ** value of the variable means there is no need to invoke the LIKE
152382 ** function, then no OP_Variable will be added to the program.
152383 ** This causes problems for the sqlite3_bind_parameter_name()
152384 ** API. To work around them, add a dummy OP_Variable here.
152385 */
152386 int r1 = sqlite3GetTempReg(pParse);
152387 sqlite3ExprCodeTarget(pParse, pRight, r1);
152388 sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
152389 sqlite3ReleaseTempReg(pParse, r1);
152390 }
152391 }
152392 }else{
152393 z = 0;
152394 }
152395 }
152396
152397 rc = (z!=0);
152398 sqlite3ValueFree(pVal);
152399 return rc;
152400}
152401#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
152402
152403
152404#ifndef SQLITE_OMIT_VIRTUALTABLE
152405/*
152406** Check to see if the pExpr expression is a form that needs to be passed
152407** to the xBestIndex method of virtual tables. Forms of interest include:
152408**
152409** Expression Virtual Table Operator
152410** ----------------------- ---------------------------------
152411** 1. column MATCH expr SQLITE_INDEX_CONSTRAINT_MATCH
152412** 2. column GLOB expr SQLITE_INDEX_CONSTRAINT_GLOB
152413** 3. column LIKE expr SQLITE_INDEX_CONSTRAINT_LIKE
152414** 4. column REGEXP expr SQLITE_INDEX_CONSTRAINT_REGEXP
152415** 5. column != expr SQLITE_INDEX_CONSTRAINT_NE
152416** 6. expr != column SQLITE_INDEX_CONSTRAINT_NE
152417** 7. column IS NOT expr SQLITE_INDEX_CONSTRAINT_ISNOT
152418** 8. expr IS NOT column SQLITE_INDEX_CONSTRAINT_ISNOT
152419** 9. column IS NOT NULL SQLITE_INDEX_CONSTRAINT_ISNOTNULL
152420**
152421** In every case, "column" must be a column of a virtual table. If there
152422** is a match, set *ppLeft to the "column" expression, set *ppRight to the
152423** "expr" expression (even though in forms (6) and (8) the column is on the
152424** right and the expression is on the left). Also set *peOp2 to the
152425** appropriate virtual table operator. The return value is 1 or 2 if there
152426** is a match. The usual return is 1, but if the RHS is also a column
152427** of virtual table in forms (5) or (7) then return 2.
152428**
152429** If the expression matches none of the patterns above, return 0.
152430*/
152431static int isAuxiliaryVtabOperator(
152432 sqlite3 *db, /* Parsing context */
152433 Expr *pExpr, /* Test this expression */
152434 unsigned char *peOp2, /* OUT: 0 for MATCH, or else an op2 value */
152435 Expr **ppLeft, /* Column expression to left of MATCH/op2 */
152436 Expr **ppRight /* Expression to left of MATCH/op2 */
152437){
152438 if( pExpr->op==TK_FUNCTION172 ){
152439 static const struct Op2 {
152440 const char *zOp;
152441 unsigned char eOp2;
152442 } aOp[] = {
152443 { "match", SQLITE_INDEX_CONSTRAINT_MATCH64 },
152444 { "glob", SQLITE_INDEX_CONSTRAINT_GLOB66 },
152445 { "like", SQLITE_INDEX_CONSTRAINT_LIKE65 },
152446 { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP67 }
152447 };
152448 ExprList *pList;
152449 Expr *pCol; /* Column reference */
152450 int i;
152451
152452 assert( ExprUseXList(pExpr) )((void) (0));
152453 pList = pExpr->x.pList;
152454 if( pList==0 || pList->nExpr!=2 ){
152455 return 0;
152456 }
152457
152458 /* Built-in operators MATCH, GLOB, LIKE, and REGEXP attach to a
152459 ** virtual table on their second argument, which is the same as
152460 ** the left-hand side operand in their in-fix form.
152461 **
152462 ** vtab_column MATCH expression
152463 ** MATCH(expression,vtab_column)
152464 */
152465 pCol = pList->a[1].pExpr;
152466 assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) )((void) (0));
152467 testcase( pCol->op==TK_COLUMN && pCol->y.pTab==0 );
152468 if( ExprIsVtab(pCol)((pCol)->op==167 && (pCol)->y.pTab!=0 &&
(pCol)->y.pTab->eTabType==1)
){
152469 for(i=0; i<ArraySize(aOp)((int)(sizeof(aOp)/sizeof(aOp[0]))); i++){
152470 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
152471 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
152472 *peOp2 = aOp[i].eOp2;
152473 *ppRight = pList->a[0].pExpr;
152474 *ppLeft = pCol;
152475 return 1;
152476 }
152477 }
152478 }
152479
152480 /* We can also match against the first column of overloaded
152481 ** functions where xFindFunction returns a value of at least
152482 ** SQLITE_INDEX_CONSTRAINT_FUNCTION.
152483 **
152484 ** OVERLOADED(vtab_column,expression)
152485 **
152486 ** Historically, xFindFunction expected to see lower-case function
152487 ** names. But for this use case, xFindFunction is expected to deal
152488 ** with function names in an arbitrary case.
152489 */
152490 pCol = pList->a[0].pExpr;
152491 assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) )((void) (0));
152492 testcase( pCol->op==TK_COLUMN && pCol->y.pTab==0 );
152493 if( ExprIsVtab(pCol)((pCol)->op==167 && (pCol)->y.pTab!=0 &&
(pCol)->y.pTab->eTabType==1)
){
152494 sqlite3_vtab *pVtab;
152495 sqlite3_module *pMod;
152496 void (*xNotUsed)(sqlite3_context*,int,sqlite3_value**);
152497 void *pNotUsed;
152498 pVtab = sqlite3GetVTable(db, pCol->y.pTab)->pVtab;
152499 assert( pVtab!=0 )((void) (0));
152500 assert( pVtab->pModule!=0 )((void) (0));
152501 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
152502 pMod = (sqlite3_module *)pVtab->pModule;
152503 if( pMod->xFindFunction!=0 ){
152504 i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed);
152505 if( i>=SQLITE_INDEX_CONSTRAINT_FUNCTION150 ){
152506 *peOp2 = i;
152507 *ppRight = pList->a[1].pExpr;
152508 *ppLeft = pCol;
152509 return 1;
152510 }
152511 }
152512 }
152513 }else if( pExpr->op==TK_NE52 || pExpr->op==TK_ISNOT171 || pExpr->op==TK_NOTNULL51 ){
152514 int res = 0;
152515 Expr *pLeft = pExpr->pLeft;
152516 Expr *pRight = pExpr->pRight;
152517 assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) )((void) (0));
152518 testcase( pLeft->op==TK_COLUMN && pLeft->y.pTab==0 );
152519 if( ExprIsVtab(pLeft)((pLeft)->op==167 && (pLeft)->y.pTab!=0 &&
(pLeft)->y.pTab->eTabType==1)
){
152520 res++;
152521 }
152522 assert( pRight==0 || pRight->op!=TK_COLUMN || ExprUseYTab(pRight) )((void) (0));
152523 testcase( pRight && pRight->op==TK_COLUMN && pRight->y.pTab==0 );
152524 if( pRight && ExprIsVtab(pRight)((pRight)->op==167 && (pRight)->y.pTab!=0 &&
(pRight)->y.pTab->eTabType==1)
){
152525 res++;
152526 SWAP(Expr*, pLeft, pRight){Expr* t=pLeft; pLeft=pRight; pRight=t;};
152527 }
152528 *ppLeft = pLeft;
152529 *ppRight = pRight;
152530 if( pExpr->op==TK_NE52 ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE68;
152531 if( pExpr->op==TK_ISNOT171 ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT69;
152532 if( pExpr->op==TK_NOTNULL51 ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOTNULL70;
152533 return res;
152534 }
152535 return 0;
152536}
152537#endif /* SQLITE_OMIT_VIRTUALTABLE */
152538
152539/*
152540** If the pBase expression originated in the ON or USING clause of
152541** a join, then transfer the appropriate markings over to derived.
152542*/
152543static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
152544 if( pDerived && ExprHasProperty(pBase, EP_OuterON|EP_InnerON)(((pBase)->flags&(0x000001|0x000002))!=0) ){
152545 pDerived->flags |= pBase->flags & (EP_OuterON0x000001|EP_InnerON0x000002);
152546 pDerived->w.iJoin = pBase->w.iJoin;
152547 }
152548}
152549
152550/*
152551** Mark term iChild as being a child of term iParent
152552*/
152553static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){
152554 pWC->a[iChild].iParent = iParent;
152555 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
152556 pWC->a[iParent].nChild++;
152557}
152558
152559/*
152560** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
152561** a conjunction, then return just pTerm when N==0. If N is exceeds
152562** the number of available subterms, return NULL.
152563*/
152564static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){
152565 if( pTerm->eOperator!=WO_AND0x0400 ){
152566 return N==0 ? pTerm : 0;
152567 }
152568 if( N<pTerm->u.pAndInfo->wc.nTerm ){
152569 return &pTerm->u.pAndInfo->wc.a[N];
152570 }
152571 return 0;
152572}
152573
152574/*
152575** Subterms pOne and pTwo are contained within WHERE clause pWC. The
152576** two subterms are in disjunction - they are OR-ed together.
152577**
152578** If these two terms are both of the form: "A op B" with the same
152579** A and B values but different operators and if the operators are
152580** compatible (if one is = and the other is <, for example) then
152581** add a new virtual AND term to pWC that is the combination of the
152582** two.
152583**
152584** Some examples:
152585**
152586** x<y OR x=y --> x<=y
152587** x=y OR x=y --> x=y
152588** x<=y OR x<y --> x<=y
152589**
152590** The following is NOT generated:
152591**
152592** x<y OR x>y --> x!=y
152593*/
152594static void whereCombineDisjuncts(
152595 SrcList *pSrc, /* the FROM clause */
152596 WhereClause *pWC, /* The complete WHERE clause */
152597 WhereTerm *pOne, /* First disjunct */
152598 WhereTerm *pTwo /* Second disjunct */
152599){
152600 u16 eOp = pOne->eOperator | pTwo->eOperator;
152601 sqlite3 *db; /* Database connection (for malloc) */
152602 Expr *pNew; /* New virtual expression */
152603 int op; /* Operator for the combined expression */
152604 int idxNew; /* Index in pWC of the next virtual term */
152605
152606 if( (pOne->wtFlags | pTwo->wtFlags) & TERM_VNULL0x0080 ) return;
152607 if( (pOne->eOperator & (WO_EQ0x0002|WO_LT(0x0002<<(56 -53))|WO_LE(0x0002<<(55 -53))|WO_GT(0x0002<<(54 -53))|WO_GE(0x0002<<(57 -53))))==0 ) return;
152608 if( (pTwo->eOperator & (WO_EQ0x0002|WO_LT(0x0002<<(56 -53))|WO_LE(0x0002<<(55 -53))|WO_GT(0x0002<<(54 -53))|WO_GE(0x0002<<(57 -53))))==0 ) return;
152609 if( (eOp & (WO_EQ0x0002|WO_LT(0x0002<<(56 -53))|WO_LE(0x0002<<(55 -53))))!=eOp
152610 && (eOp & (WO_EQ0x0002|WO_GT(0x0002<<(54 -53))|WO_GE(0x0002<<(57 -53))))!=eOp ) return;
152611 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 )((void) (0));
152612 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 )((void) (0));
152613 if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
152614 if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return;
152615 /* If we reach this point, it means the two subterms can be combined */
152616 if( (eOp & (eOp-1))!=0 ){
152617 if( eOp & (WO_LT(0x0002<<(56 -53))|WO_LE(0x0002<<(55 -53))) ){
152618 eOp = WO_LE(0x0002<<(55 -53));
152619 }else{
152620 assert( eOp & (WO_GT|WO_GE) )((void) (0));
152621 eOp = WO_GE(0x0002<<(57 -53));
152622 }
152623 }
152624 db = pWC->pWInfo->pParse->db;
152625 pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
152626 if( pNew==0 ) return;
152627 for(op=TK_EQ53; eOp!=(WO_EQ0x0002<<(op-TK_EQ53)); op++){ assert( op<TK_GE )((void) (0)); }
152628 pNew->op = op;
152629 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL0x0002|TERM_DYNAMIC0x0001);
152630 exprAnalyze(pSrc, pWC, idxNew);
152631}
152632
152633#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
152634/*
152635** Analyze a term that consists of two or more OR-connected
152636** subterms. So in:
152637**
152638** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
152639** ^^^^^^^^^^^^^^^^^^^^
152640**
152641** This routine analyzes terms such as the middle term in the above example.
152642** A WhereOrTerm object is computed and attached to the term under
152643** analysis, regardless of the outcome of the analysis. Hence:
152644**
152645** WhereTerm.wtFlags |= TERM_ORINFO
152646** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object
152647**
152648** The term being analyzed must have two or more of OR-connected subterms.
152649** A single subterm might be a set of AND-connected sub-subterms.
152650** Examples of terms under analysis:
152651**
152652** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
152653** (B) x=expr1 OR expr2=x OR x=expr3
152654** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
152655** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
152656** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
152657** (F) x>A OR (x=A AND y>=B)
152658**
152659** CASE 1:
152660**
152661** If all subterms are of the form T.C=expr for some single column of C and
152662** a single table T (as shown in example B above) then create a new virtual
152663** term that is an equivalent IN expression. In other words, if the term
152664** being analyzed is:
152665**
152666** x = expr1 OR expr2 = x OR x = expr3
152667**
152668** then create a new virtual term like this:
152669**
152670** x IN (expr1,expr2,expr3)
152671**
152672** CASE 2:
152673**
152674** If there are exactly two disjuncts and one side has x>A and the other side
152675** has x=A (for the same x and A) then add a new virtual conjunct term to the
152676** WHERE clause of the form "x>=A". Example:
152677**
152678** x>A OR (x=A AND y>B) adds: x>=A
152679**
152680** The added conjunct can sometimes be helpful in query planning.
152681**
152682** CASE 3:
152683**
152684** If all subterms are indexable by a single table T, then set
152685**
152686** WhereTerm.eOperator = WO_OR
152687** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
152688**
152689** A subterm is "indexable" if it is of the form
152690** "T.C <op> <expr>" where C is any column of table T and
152691** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
152692** A subterm is also indexable if it is an AND of two or more
152693** subsubterms at least one of which is indexable. Indexable AND
152694** subterms have their eOperator set to WO_AND and they have
152695** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
152696**
152697** From another point of view, "indexable" means that the subterm could
152698** potentially be used with an index if an appropriate index exists.
152699** This analysis does not consider whether or not the index exists; that
152700** is decided elsewhere. This analysis only looks at whether subterms
152701** appropriate for indexing exist.
152702**
152703** All examples A through E above satisfy case 3. But if a term
152704** also satisfies case 1 (such as B) we know that the optimizer will
152705** always prefer case 1, so in that case we pretend that case 3 is not
152706** satisfied.
152707**
152708** It might be the case that multiple tables are indexable. For example,
152709** (E) above is indexable on tables P, Q, and R.
152710**
152711** Terms that satisfy case 3 are candidates for lookup by using
152712** separate indices to find rowids for each subterm and composing
152713** the union of all rowids using a RowSet object. This is similar
152714** to "bitmap indices" in other database engines.
152715**
152716** OTHERWISE:
152717**
152718** If none of cases 1, 2, or 3 apply, then leave the eOperator set to
152719** zero. This term is not useful for search.
152720*/
152721static void exprAnalyzeOrTerm(
152722 SrcList *pSrc, /* the FROM clause */
152723 WhereClause *pWC, /* the complete WHERE clause */
152724 int idxTerm /* Index of the OR-term to be analyzed */
152725){
152726 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
152727 Parse *pParse = pWInfo->pParse; /* Parser context */
152728 sqlite3 *db = pParse->db; /* Database connection */
152729 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
152730 Expr *pExpr = pTerm->pExpr; /* The expression of the term */
152731 int i; /* Loop counters */
152732 WhereClause *pOrWc; /* Breakup of pTerm into subterms */
152733 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
152734 WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */
152735 Bitmask chngToIN; /* Tables that might satisfy case 1 */
152736 Bitmask indexable; /* Tables that are indexable, satisfying case 2 */
152737
152738 /*
152739 ** Break the OR clause into its separate subterms. The subterms are
152740 ** stored in a WhereClause structure containing within the WhereOrInfo
152741 ** object that is attached to the original OR clause term.
152742 */
152743 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 )((void) (0));
152744 assert( pExpr->op==TK_OR )((void) (0));
152745 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
152746 if( pOrInfo==0 ) return;
152747 pTerm->wtFlags |= TERM_ORINFO0x0010;
152748 pOrWc = &pOrInfo->wc;
152749 memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic));
152750 sqlite3WhereClauseInit(pOrWc, pWInfo);
152751 sqlite3WhereSplit(pOrWc, pExpr, TK_OR43);
152752 sqlite3WhereExprAnalyze(pSrc, pOrWc);
152753 if( db->mallocFailed ) return;
152754 assert( pOrWc->nTerm>=2 )((void) (0));
152755
152756 /*
152757 ** Compute the set of tables that might satisfy cases 1 or 3.
152758 */
152759 indexable = ~(Bitmask)0;
152760 chngToIN = ~(Bitmask)0;
152761 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
152762 if( (pOrTerm->eOperator & WO_SINGLE0x01ff)==0 ){
152763 WhereAndInfo *pAndInfo;
152764 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 )((void) (0));
152765 chngToIN = 0;
152766 pAndInfo = sqlite3DbMallocRawNN(db, sizeof(*pAndInfo));
152767 if( pAndInfo ){
152768 WhereClause *pAndWC;
152769 WhereTerm *pAndTerm;
152770 int j;
152771 Bitmask b = 0;
152772 pOrTerm->u.pAndInfo = pAndInfo;
152773 pOrTerm->wtFlags |= TERM_ANDINFO0x0020;
152774 pOrTerm->eOperator = WO_AND0x0400;
152775 pOrTerm->leftCursor = -1;
152776 pAndWC = &pAndInfo->wc;
152777 memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic));
152778 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
152779 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND44);
152780 sqlite3WhereExprAnalyze(pSrc, pAndWC);
152781 pAndWC->pOuter = pWC;
152782 if( !db->mallocFailed ){
152783 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
152784 assert( pAndTerm->pExpr )((void) (0));
152785 if( allowedOp(pAndTerm->pExpr->op)
152786 || pAndTerm->eOperator==WO_AUX0x0040
152787 ){
152788 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
152789 }
152790 }
152791 }
152792 indexable &= b;
152793 }
152794 }else if( pOrTerm->wtFlags & TERM_COPIED0x0008 ){
152795 /* Skip this term for now. We revisit it when we process the
152796 ** corresponding TERM_VIRTUAL term */
152797 }else{
152798 Bitmask b;
152799 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
152800 if( pOrTerm->wtFlags & TERM_VIRTUAL0x0002 ){
152801 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
152802 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
152803 }
152804 indexable &= b;
152805 if( (pOrTerm->eOperator & WO_EQ0x0002)==0 ){
152806 chngToIN = 0;
152807 }else{
152808 chngToIN &= b;
152809 }
152810 }
152811 }
152812
152813 /*
152814 ** Record the set of tables that satisfy case 3. The set might be
152815 ** empty.
152816 */
152817 pOrInfo->indexable = indexable;
152818 pTerm->eOperator = WO_OR0x0200;
152819 pTerm->leftCursor = -1;
152820 if( indexable ){
152821 pWC->hasOr = 1;
152822 }
152823
152824 /* For a two-way OR, attempt to implementation case 2.
152825 */
152826 if( indexable && pOrWc->nTerm==2 ){
152827 int iOne = 0;
152828 WhereTerm *pOne;
152829 while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
152830 int iTwo = 0;
152831 WhereTerm *pTwo;
152832 while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
152833 whereCombineDisjuncts(pSrc, pWC, pOne, pTwo);
152834 }
152835 }
152836 }
152837
152838 /*
152839 ** chngToIN holds a set of tables that *might* satisfy case 1. But
152840 ** we have to do some additional checking to see if case 1 really
152841 ** is satisfied.
152842 **
152843 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
152844 ** that there is no possibility of transforming the OR clause into an
152845 ** IN operator because one or more terms in the OR clause contain
152846 ** something other than == on a column in the single table. The 1-bit
152847 ** case means that every term of the OR clause is of the form
152848 ** "table.column=expr" for some single table. The one bit that is set
152849 ** will correspond to the common table. We still need to check to make
152850 ** sure the same column is used on all terms. The 2-bit case is when
152851 ** the all terms are of the form "table1.column=table2.column". It
152852 ** might be possible to form an IN operator with either table1.column
152853 ** or table2.column as the LHS if either is common to every term of
152854 ** the OR clause.
152855 **
152856 ** Note that terms of the form "table.column1=table.column2" (the
152857 ** same table on both sizes of the ==) cannot be optimized.
152858 */
152859 if( chngToIN ){
152860 int okToChngToIN = 0; /* True if the conversion to IN is valid */
152861 int iColumn = -1; /* Column index on lhs of IN operator */
152862 int iCursor = -1; /* Table cursor common to all terms */
152863 int j = 0; /* Loop counter */
152864
152865 /* Search for a table and column that appears on one side or the
152866 ** other of the == operator in every subterm. That table and column
152867 ** will be recorded in iCursor and iColumn. There might not be any
152868 ** such table and column. Set okToChngToIN if an appropriate table
152869 ** and column is found but leave okToChngToIN false if not found.
152870 */
152871 for(j=0; j<2 && !okToChngToIN; j++){
152872 Expr *pLeft = 0;
152873 pOrTerm = pOrWc->a;
152874 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
152875 assert( pOrTerm->eOperator & WO_EQ )((void) (0));
152876 pOrTerm->wtFlags &= ~TERM_OK0x0040;
152877 if( pOrTerm->leftCursor==iCursor ){
152878 /* This is the 2-bit case and we are on the second iteration and
152879 ** current term is from the first iteration. So skip this term. */
152880 assert( j==1 )((void) (0));
152881 continue;
152882 }
152883 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
152884 pOrTerm->leftCursor))==0 ){
152885 /* This term must be of the form t1.a==t2.b where t2 is in the
152886 ** chngToIN set but t1 is not. This term will be either preceded
152887 ** or follwed by an inverted copy (t2.b==t1.a). Skip this term
152888 ** and use its inversion. */
152889 testcase( pOrTerm->wtFlags & TERM_COPIED );
152890 testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
152891 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) )((void) (0));
152892 continue;
152893 }
152894 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
152895 iColumn = pOrTerm->u.x.leftColumn;
152896 iCursor = pOrTerm->leftCursor;
152897 pLeft = pOrTerm->pExpr->pLeft;
152898 break;
152899 }
152900 if( i<0 ){
152901 /* No candidate table+column was found. This can only occur
152902 ** on the second iteration */
152903 assert( j==1 )((void) (0));
152904 assert( IsPowerOfTwo(chngToIN) )((void) (0));
152905 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) )((void) (0));
152906 break;
152907 }
152908 testcase( j==1 );
152909
152910 /* We have found a candidate table and column. Check to see if that
152911 ** table and column is common to every term in the OR clause */
152912 okToChngToIN = 1;
152913 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
152914 assert( pOrTerm->eOperator & WO_EQ )((void) (0));
152915 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
152916 if( pOrTerm->leftCursor!=iCursor ){
152917 pOrTerm->wtFlags &= ~TERM_OK0x0040;
152918 }else if( pOrTerm->u.x.leftColumn!=iColumn || (iColumn==XN_EXPR(-2)
152919 && sqlite3ExprCompare(pParse, pOrTerm->pExpr->pLeft, pLeft, -1)
152920 )){
152921 okToChngToIN = 0;
152922 }else{
152923 int affLeft, affRight;
152924 /* If the right-hand side is also a column, then the affinities
152925 ** of both right and left sides must be such that no type
152926 ** conversions are required on the right. (Ticket #2249)
152927 */
152928 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
152929 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
152930 if( affRight!=0 && affRight!=affLeft ){
152931 okToChngToIN = 0;
152932 }else{
152933 pOrTerm->wtFlags |= TERM_OK0x0040;
152934 }
152935 }
152936 }
152937 }
152938
152939 /* At this point, okToChngToIN is true if original pTerm satisfies
152940 ** case 1. In that case, construct a new virtual term that is
152941 ** pTerm converted into an IN operator.
152942 */
152943 if( okToChngToIN ){
152944 Expr *pDup; /* A transient duplicate expression */
152945 ExprList *pList = 0; /* The RHS of the IN operator */
152946 Expr *pLeft = 0; /* The LHS of the IN operator */
152947 Expr *pNew; /* The complete IN operator */
152948
152949 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
152950 if( (pOrTerm->wtFlags & TERM_OK0x0040)==0 ) continue;
152951 assert( pOrTerm->eOperator & WO_EQ )((void) (0));
152952 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
152953 assert( pOrTerm->leftCursor==iCursor )((void) (0));
152954 assert( pOrTerm->u.x.leftColumn==iColumn )((void) (0));
152955 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
152956 pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
152957 pLeft = pOrTerm->pExpr->pLeft;
152958 }
152959 assert( pLeft!=0 )((void) (0));
152960 pDup = sqlite3ExprDup(db, pLeft, 0);
152961 pNew = sqlite3PExpr(pParse, TK_IN49, pDup, 0);
152962 if( pNew ){
152963 int idxNew;
152964 transferJoinMarkings(pNew, pExpr);
152965 assert( ExprUseXList(pNew) )((void) (0));
152966 pNew->x.pList = pList;
152967 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL0x0002|TERM_DYNAMIC0x0001);
152968 testcase( idxNew==0 );
152969 exprAnalyze(pSrc, pWC, idxNew);
152970 /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where reused */
152971 markTermAsChild(pWC, idxNew, idxTerm);
152972 }else{
152973 sqlite3ExprListDelete(db, pList);
152974 }
152975 }
152976 }
152977}
152978#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
152979
152980/*
152981** We already know that pExpr is a binary operator where both operands are
152982** column references. This routine checks to see if pExpr is an equivalence
152983** relation:
152984** 1. The SQLITE_Transitive optimization must be enabled
152985** 2. Must be either an == or an IS operator
152986** 3. Not originating in the ON clause of an OUTER JOIN
152987** 4. The affinities of A and B must be compatible
152988** 5a. Both operands use the same collating sequence OR
152989** 5b. The overall collating sequence is BINARY
152990** If this routine returns TRUE, that means that the RHS can be substituted
152991** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
152992** This is an optimization. No harm comes from returning 0. But if 1 is
152993** returned when it should not be, then incorrect answers might result.
152994*/
152995static int termIsEquivalence(Parse *pParse, Expr *pExpr){
152996 char aff1, aff2;
152997 CollSeq *pColl;
152998 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive)(((pParse->db)->dbOptFlags&(0x00000080))==0) ) return 0;
152999 if( pExpr->op!=TK_EQ53 && pExpr->op!=TK_IS45 ) return 0;
153000 if( ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(0x000001))!=0) ) return 0;
153001 aff1 = sqlite3ExprAffinity(pExpr->pLeft);
153002 aff2 = sqlite3ExprAffinity(pExpr->pRight);
153003 if( aff1!=aff2
153004 && (!sqlite3IsNumericAffinity(aff1)((aff1)>=0x43) || !sqlite3IsNumericAffinity(aff2)((aff2)>=0x43))
153005 ){
153006 return 0;
153007 }
153008 pColl = sqlite3ExprCompareCollSeq(pParse, pExpr);
153009 if( sqlite3IsBinary(pColl) ) return 1;
153010 return sqlite3ExprCollSeqMatch(pParse, pExpr->pLeft, pExpr->pRight);
153011}
153012
153013/*
153014** Recursively walk the expressions of a SELECT statement and generate
153015** a bitmask indicating which tables are used in that expression
153016** tree.
153017*/
153018static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){
153019 Bitmask mask = 0;
153020 while( pS ){
153021 SrcList *pSrc = pS->pSrc;
153022 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
153023 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
153024 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
153025 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
153026 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
153027 if( ALWAYS(pSrc!=0)(pSrc!=0) ){
153028 int i;
153029 for(i=0; i<pSrc->nSrc; i++){
153030 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
153031 if( pSrc->a[i].fg.isUsing==0 ){
153032 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].u3.pOn);
153033 }
153034 if( pSrc->a[i].fg.isTabFunc ){
153035 mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
153036 }
153037 }
153038 }
153039 pS = pS->pPrior;
153040 }
153041 return mask;
153042}
153043
153044/*
153045** Expression pExpr is one operand of a comparison operator that might
153046** be useful for indexing. This routine checks to see if pExpr appears
153047** in any index. Return TRUE (1) if pExpr is an indexed term and return
153048** FALSE (0) if not. If TRUE is returned, also set aiCurCol[0] to the cursor
153049** number of the table that is indexed and aiCurCol[1] to the column number
153050** of the column that is indexed, or XN_EXPR (-2) if an expression is being
153051** indexed.
153052**
153053** If pExpr is a TK_COLUMN column reference, then this routine always returns
153054** true even if that particular column is not indexed, because the column
153055** might be added to an automatic index later.
153056*/
153057static SQLITE_NOINLINE__attribute__((noinline)) int exprMightBeIndexed2(
153058 SrcList *pFrom, /* The FROM clause */
153059 Bitmask mPrereq, /* Bitmask of FROM clause terms referenced by pExpr */
153060 int *aiCurCol, /* Write the referenced table cursor and column here */
153061 Expr *pExpr /* An operand of a comparison operator */
153062){
153063 Index *pIdx;
153064 int i;
153065 int iCur;
153066 for(i=0; mPrereq>1; i++, mPrereq>>=1){}
153067 iCur = pFrom->a[i].iCursor;
153068 for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
153069 if( pIdx->aColExpr==0 ) continue;
153070 for(i=0; i<pIdx->nKeyCol; i++){
153071 if( pIdx->aiColumn[i]!=XN_EXPR(-2) ) continue;
153072 if( sqlite3ExprCompareSkip(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
153073 aiCurCol[0] = iCur;
153074 aiCurCol[1] = XN_EXPR(-2);
153075 return 1;
153076 }
153077 }
153078 }
153079 return 0;
153080}
153081static int exprMightBeIndexed(
153082 SrcList *pFrom, /* The FROM clause */
153083 Bitmask mPrereq, /* Bitmask of FROM clause terms referenced by pExpr */
153084 int *aiCurCol, /* Write the referenced table cursor & column here */
153085 Expr *pExpr, /* An operand of a comparison operator */
153086 int op /* The specific comparison operator */
153087){
153088 /* If this expression is a vector to the left or right of a
153089 ** inequality constraint (>, <, >= or <=), perform the processing
153090 ** on the first element of the vector. */
153091 assert( TK_GT+1==TK_LE && TK_GT+2==TK_LT && TK_GT+3==TK_GE )((void) (0));
153092 assert( TK_IS<TK_GE && TK_ISNULL<TK_GE && TK_IN<TK_GE )((void) (0));
153093 assert( op<=TK_GE )((void) (0));
153094 if( pExpr->op==TK_VECTOR177 && (op>=TK_GT54 && ALWAYS(op<=TK_GE)(op<=57)) ){
153095 assert( ExprUseXList(pExpr) )((void) (0));
153096 pExpr = pExpr->x.pList->a[0].pExpr;
153097
153098 }
153099
153100 if( pExpr->op==TK_COLUMN167 ){
153101 aiCurCol[0] = pExpr->iTable;
153102 aiCurCol[1] = pExpr->iColumn;
153103 return 1;
153104 }
153105 if( mPrereq==0 ) return 0; /* No table references */
153106 if( (mPrereq&(mPrereq-1))!=0 ) return 0; /* Refs more than one table */
153107 return exprMightBeIndexed2(pFrom,mPrereq,aiCurCol,pExpr);
153108}
153109
153110
153111/*
153112** The input to this routine is an WhereTerm structure with only the
153113** "pExpr" field filled in. The job of this routine is to analyze the
153114** subexpression and populate all the other fields of the WhereTerm
153115** structure.
153116**
153117** If the expression is of the form "<expr> <op> X" it gets commuted
153118** to the standard form of "X <op> <expr>".
153119**
153120** If the expression is of the form "X <op> Y" where both X and Y are
153121** columns, then the original expression is unchanged and a new virtual
153122** term of the form "Y <op> X" is added to the WHERE clause and
153123** analyzed separately. The original term is marked with TERM_COPIED
153124** and the new term is marked with TERM_DYNAMIC (because it's pExpr
153125** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
153126** is a commuted copy of a prior term.) The original term has nChild=1
153127** and the copy has idxParent set to the index of the original term.
153128*/
153129static void exprAnalyze(
153130 SrcList *pSrc, /* the FROM clause */
153131 WhereClause *pWC, /* the WHERE clause */
153132 int idxTerm /* Index of the term to be analyzed */
153133){
153134 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
153135 WhereTerm *pTerm; /* The term to be analyzed */
153136 WhereMaskSet *pMaskSet; /* Set of table index masks */
153137 Expr *pExpr; /* The expression to be analyzed */
153138 Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */
153139 Bitmask prereqAll; /* Prerequesites of pExpr */
153140 Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */
153141 Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */
153142 int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */
153143 int noCase = 0; /* uppercase equivalent to lowercase */
153144 int op; /* Top-level operator. pExpr->op */
153145 Parse *pParse = pWInfo->pParse; /* Parsing context */
153146 sqlite3 *db = pParse->db; /* Database connection */
153147 unsigned char eOp2 = 0; /* op2 value for LIKE/REGEXP/GLOB */
153148 int nLeft; /* Number of elements on left side vector */
153149
153150 if( db->mallocFailed ){
153151 return;
153152 }
153153 assert( pWC->nTerm > idxTerm )((void) (0));
153154 pTerm = &pWC->a[idxTerm];
153155 pMaskSet = &pWInfo->sMaskSet;
153156 pExpr = pTerm->pExpr;
153157 assert( pExpr!=0 )((void) (0)); /* Because malloc() has not failed */
153158 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE )((void) (0));
153159 pMaskSet->bVarSelect = 0;
153160 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
153161 op = pExpr->op;
153162 if( op==TK_IN49 ){
153163 assert( pExpr->pRight==0 )((void) (0));
153164 if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
153165 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
153166 pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
153167 }else{
153168 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
153169 }
153170 prereqAll = prereqLeft | pTerm->prereqRight;
153171 }else{
153172 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
153173 if( pExpr->pLeft==0
153174 || ExprHasProperty(pExpr, EP_xIsSelect|EP_IfNullRow)(((pExpr)->flags&(0x001000|0x040000))!=0)
153175 || pExpr->x.pList!=0
153176 ){
153177 prereqAll = sqlite3WhereExprUsageNN(pMaskSet, pExpr);
153178 }else{
153179 prereqAll = prereqLeft | pTerm->prereqRight;
153180 }
153181 }
153182 if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT0x1000;
153183
153184#ifdef SQLITE_DEBUG
153185 if( prereqAll!=sqlite3WhereExprUsageNN(pMaskSet, pExpr) ){
153186 printf("\n*** Incorrect prereqAll computed for:\n");
153187 sqlite3TreeViewExpr(0,pExpr,0);
153188 assert( 0 )((void) (0));
153189 }
153190#endif
153191
153192 if( ExprHasProperty(pExpr, EP_OuterON|EP_InnerON)(((pExpr)->flags&(0x000001|0x000002))!=0) ){
153193 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->w.iJoin);
153194 if( ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(0x000001))!=0) ){
153195 prereqAll |= x;
153196 extraRight = x-1; /* ON clause terms may not be used with an index
153197 ** on left table of a LEFT JOIN. Ticket #3015 */
153198 if( (prereqAll>>1)>=x ){
153199 sqlite3ErrorMsg(pParse, "ON clause references tables to its right");
153200 return;
153201 }
153202 }else if( (prereqAll>>1)>=x ){
153203 /* The ON clause of an INNER JOIN references a table to its right.
153204 ** Most other SQL database engines raise an error. But SQLite versions
153205 ** 3.0 through 3.38 just put the ON clause constraint into the WHERE
153206 ** clause and carried on. Beginning with 3.39, raise an error only
153207 ** if there is a RIGHT or FULL JOIN in the query. This makes SQLite
153208 ** more like other systems, and also preserves legacy. */
153209 if( ALWAYS(pSrc->nSrc>0)(pSrc->nSrc>0) && (pSrc->a[0].fg.jointype & JT_LTORJ0x40)!=0 ){
153210 sqlite3ErrorMsg(pParse, "ON clause references tables to its right");
153211 return;
153212 }
153213 ExprClearProperty(pExpr, EP_InnerON)(pExpr)->flags&=~(0x000002);
153214 }
153215 }
153216 pTerm->prereqAll = prereqAll;
153217 pTerm->leftCursor = -1;
153218 pTerm->iParent = -1;
153219 pTerm->eOperator = 0;
153220 if( allowedOp(op) ){
153221 int aiCurCol[2];
153222 Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
153223 Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
153224 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL0x3fff : WO_EQUIV0x0800;
153225
153226 if( pTerm->u.x.iField>0 ){
153227 assert( op==TK_IN )((void) (0));
153228 assert( pLeft->op==TK_VECTOR )((void) (0));
153229 assert( ExprUseXList(pLeft) )((void) (0));
153230 pLeft = pLeft->x.pList->a[pTerm->u.x.iField-1].pExpr;
153231 }
153232
153233 if( exprMightBeIndexed(pSrc, prereqLeft, aiCurCol, pLeft, op) ){
153234 pTerm->leftCursor = aiCurCol[0];
153235 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
153236 pTerm->u.x.leftColumn = aiCurCol[1];
153237 pTerm->eOperator = operatorMask(op) & opMask;
153238 }
153239 if( op==TK_IS45 ) pTerm->wtFlags |= TERM_IS0x0800;
153240 if( pRight
153241 && exprMightBeIndexed(pSrc, pTerm->prereqRight, aiCurCol, pRight, op)
153242 && !ExprHasProperty(pRight, EP_FixedCol)(((pRight)->flags&(0x000020))!=0)
153243 ){
153244 WhereTerm *pNew;
153245 Expr *pDup;
153246 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
153247 assert( pTerm->u.x.iField==0 )((void) (0));
153248 if( pTerm->leftCursor>=0 ){
153249 int idxNew;
153250 pDup = sqlite3ExprDup(db, pExpr, 0);
153251 if( db->mallocFailed ){
153252 sqlite3ExprDelete(db, pDup);
153253 return;
153254 }
153255 idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL0x0002|TERM_DYNAMIC0x0001);
153256 if( idxNew==0 ) return;
153257 pNew = &pWC->a[idxNew];
153258 markTermAsChild(pWC, idxNew, idxTerm);
153259 if( op==TK_IS45 ) pNew->wtFlags |= TERM_IS0x0800;
153260 pTerm = &pWC->a[idxTerm];
153261 pTerm->wtFlags |= TERM_COPIED0x0008;
153262
153263 if( termIsEquivalence(pParse, pDup) ){
153264 pTerm->eOperator |= WO_EQUIV0x0800;
153265 eExtraOp = WO_EQUIV0x0800;
153266 }
153267 }else{
153268 pDup = pExpr;
153269 pNew = pTerm;
153270 }
153271 pNew->wtFlags |= exprCommute(pParse, pDup);
153272 pNew->leftCursor = aiCurCol[0];
153273 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
153274 pNew->u.x.leftColumn = aiCurCol[1];
153275 testcase( (prereqLeft | extraRight) != prereqLeft );
153276 pNew->prereqRight = prereqLeft | extraRight;
153277 pNew->prereqAll = prereqAll;
153278 pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
153279 }else
153280 if( op==TK_ISNULL50
153281 && !ExprHasProperty(pExpr,EP_OuterON)(((pExpr)->flags&(0x000001))!=0)
153282 && 0==sqlite3ExprCanBeNull(pLeft)
153283 ){
153284 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
153285 pExpr->op = TK_TRUEFALSE170;
153286 pExpr->u.zToken = "false";
153287 ExprSetProperty(pExpr, EP_IsFalse)(pExpr)->flags|=(0x20000000);
153288 pTerm->prereqAll = 0;
153289 pTerm->eOperator = 0;
153290 }
153291 }
153292
153293#ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
153294 /* If a term is the BETWEEN operator, create two new virtual terms
153295 ** that define the range that the BETWEEN implements. For example:
153296 **
153297 ** a BETWEEN b AND c
153298 **
153299 ** is converted into:
153300 **
153301 ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
153302 **
153303 ** The two new terms are added onto the end of the WhereClause object.
153304 ** The new terms are "dynamic" and are children of the original BETWEEN
153305 ** term. That means that if the BETWEEN term is coded, the children are
153306 ** skipped. Or, if the children are satisfied by an index, the original
153307 ** BETWEEN term is skipped.
153308 */
153309 else if( pExpr->op==TK_BETWEEN48 && pWC->op==TK_AND44 ){
153310 ExprList *pList;
153311 int i;
153312 static const u8 ops[] = {TK_GE57, TK_LE55};
153313 assert( ExprUseXList(pExpr) )((void) (0));
153314 pList = pExpr->x.pList;
153315 assert( pList!=0 )((void) (0));
153316 assert( pList->nExpr==2 )((void) (0));
153317 for(i=0; i<2; i++){
153318 Expr *pNewExpr;
153319 int idxNew;
153320 pNewExpr = sqlite3PExpr(pParse, ops[i],
153321 sqlite3ExprDup(db, pExpr->pLeft, 0),
153322 sqlite3ExprDup(db, pList->a[i].pExpr, 0));
153323 transferJoinMarkings(pNewExpr, pExpr);
153324 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL0x0002|TERM_DYNAMIC0x0001);
153325 testcase( idxNew==0 );
153326 exprAnalyze(pSrc, pWC, idxNew);
153327 pTerm = &pWC->a[idxTerm];
153328 markTermAsChild(pWC, idxNew, idxTerm);
153329 }
153330 }
153331#endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
153332
153333#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
153334 /* Analyze a term that is composed of two or more subterms connected by
153335 ** an OR operator.
153336 */
153337 else if( pExpr->op==TK_OR43 ){
153338 assert( pWC->op==TK_AND )((void) (0));
153339 exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
153340 pTerm = &pWC->a[idxTerm];
153341 }
153342#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
153343 /* The form "x IS NOT NULL" can sometimes be evaluated more efficiently
153344 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
153345 ** virtual term of that form.
153346 **
153347 ** The virtual term must be tagged with TERM_VNULL.
153348 */
153349 else if( pExpr->op==TK_NOTNULL51 ){
153350 if( pExpr->pLeft->op==TK_COLUMN167
153351 && pExpr->pLeft->iColumn>=0
153352 && !ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(0x000001))!=0)
153353 ){
153354 Expr *pNewExpr;
153355 Expr *pLeft = pExpr->pLeft;
153356 int idxNew;
153357 WhereTerm *pNewTerm;
153358
153359 pNewExpr = sqlite3PExpr(pParse, TK_GT54,
153360 sqlite3ExprDup(db, pLeft, 0),
153361 sqlite3ExprAlloc(db, TK_NULL121, 0, 0));
153362
153363 idxNew = whereClauseInsert(pWC, pNewExpr,
153364 TERM_VIRTUAL0x0002|TERM_DYNAMIC0x0001|TERM_VNULL0x0080);
153365 if( idxNew ){
153366 pNewTerm = &pWC->a[idxNew];
153367 pNewTerm->prereqRight = 0;
153368 pNewTerm->leftCursor = pLeft->iTable;
153369 pNewTerm->u.x.leftColumn = pLeft->iColumn;
153370 pNewTerm->eOperator = WO_GT(0x0002<<(54 -53));
153371 markTermAsChild(pWC, idxNew, idxTerm);
153372 pTerm = &pWC->a[idxTerm];
153373 pTerm->wtFlags |= TERM_COPIED0x0008;
153374 pNewTerm->prereqAll = pTerm->prereqAll;
153375 }
153376 }
153377 }
153378
153379
153380#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
153381 /* Add constraints to reduce the search space on a LIKE or GLOB
153382 ** operator.
153383 **
153384 ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints
153385 **
153386 ** x>='ABC' AND x<'abd' AND x LIKE 'aBc%'
153387 **
153388 ** The last character of the prefix "abc" is incremented to form the
153389 ** termination condition "abd". If case is not significant (the default
153390 ** for LIKE) then the lower-bound is made all uppercase and the upper-
153391 ** bound is made all lowercase so that the bounds also work when comparing
153392 ** BLOBs.
153393 */
153394 else if( pExpr->op==TK_FUNCTION172
153395 && pWC->op==TK_AND44
153396 && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
153397 ){
153398 Expr *pLeft; /* LHS of LIKE/GLOB operator */
153399 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
153400 Expr *pNewExpr1;
153401 Expr *pNewExpr2;
153402 int idxNew1;
153403 int idxNew2;
153404 const char *zCollSeqName; /* Name of collating sequence */
153405 const u16 wtFlags = TERM_LIKEOPT0x0100 | TERM_VIRTUAL0x0002 | TERM_DYNAMIC0x0001;
153406
153407 assert( ExprUseXList(pExpr) )((void) (0));
153408 pLeft = pExpr->x.pList->a[1].pExpr;
153409 pStr2 = sqlite3ExprDup(db, pStr1, 0);
153410 assert( pStr1==0 || !ExprHasProperty(pStr1, EP_IntValue) )((void) (0));
153411 assert( pStr2==0 || !ExprHasProperty(pStr2, EP_IntValue) )((void) (0));
153412
153413
153414 /* Convert the lower bound to upper-case and the upper bound to
153415 ** lower-case (upper-case is less than lower-case in ASCII) so that
153416 ** the range constraints also work for BLOBs
153417 */
153418 if( noCase && !pParse->db->mallocFailed ){
153419 int i;
153420 char c;
153421 pTerm->wtFlags |= TERM_LIKE0x0400;
153422 for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
153423 pStr1->u.zToken[i] = sqlite3Toupper(c)((c)&~(sqlite3CtypeMap[(unsigned char)(c)]&0x20));
153424 pStr2->u.zToken[i] = sqlite3Tolower(c)(sqlite3UpperToLower[(unsigned char)(c)]);
153425 }
153426 }
153427
153428 if( !db->mallocFailed ){
153429 u8 c, *pC; /* Last character before the first wildcard */
153430 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
153431 c = *pC;
153432 if( noCase ){
153433 /* The point is to increment the last character before the first
153434 ** wildcard. But if we increment '@', that will push it into the
153435 ** alphabetic range where case conversions will mess up the
153436 ** inequality. To avoid this, make sure to also run the full
153437 ** LIKE on all candidate expressions by clearing the isComplete flag
153438 */
153439 if( c=='A'-1 ) isComplete = 0;
153440 c = sqlite3UpperToLower[c];
153441 }
153442 *pC = c + 1;
153443 }
153444 zCollSeqName = noCase ? "NOCASE" : sqlite3StrBINARY;
153445 pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
153446 pNewExpr1 = sqlite3PExpr(pParse, TK_GE57,
153447 sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName),
153448 pStr1);
153449 transferJoinMarkings(pNewExpr1, pExpr);
153450 idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags);
153451 testcase( idxNew1==0 );
153452 exprAnalyze(pSrc, pWC, idxNew1);
153453 pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
153454 pNewExpr2 = sqlite3PExpr(pParse, TK_LT56,
153455 sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName),
153456 pStr2);
153457 transferJoinMarkings(pNewExpr2, pExpr);
153458 idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags);
153459 testcase( idxNew2==0 );
153460 exprAnalyze(pSrc, pWC, idxNew2);
153461 pTerm = &pWC->a[idxTerm];
153462 if( isComplete ){
153463 markTermAsChild(pWC, idxNew1, idxTerm);
153464 markTermAsChild(pWC, idxNew2, idxTerm);
153465 }
153466 }
153467#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
153468
153469 /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
153470 ** new terms for each component comparison - "a = ?" and "b = ?". The
153471 ** new terms completely replace the original vector comparison, which is
153472 ** no longer used.
153473 **
153474 ** This is only required if at least one side of the comparison operation
153475 ** is not a sub-select.
153476 **
153477 ** tag-20220128a
153478 */
153479 if( (pExpr->op==TK_EQ53 || pExpr->op==TK_IS45)
153480 && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
153481 && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
153482 && ( (pExpr->pLeft->flags & EP_xIsSelect0x001000)==0
153483 || (pExpr->pRight->flags & EP_xIsSelect0x001000)==0)
153484 && pWC->op==TK_AND44
153485 ){
153486 int i;
153487 for(i=0; i<nLeft; i++){
153488 int idxNew;
153489 Expr *pNew;
153490 Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i, nLeft);
153491 Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i, nLeft);
153492
153493 pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
153494 transferJoinMarkings(pNew, pExpr);
153495 idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC0x0001|TERM_SLICE0x8000);
153496 exprAnalyze(pSrc, pWC, idxNew);
153497 }
153498 pTerm = &pWC->a[idxTerm];
153499 pTerm->wtFlags |= TERM_CODED0x0004|TERM_VIRTUAL0x0002; /* Disable the original */
153500 pTerm->eOperator = WO_ROWVAL0x2000;
153501 }
153502
153503 /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
153504 ** a virtual term for each vector component. The expression object
153505 ** used by each such virtual term is pExpr (the full vector IN(...)
153506 ** expression). The WhereTerm.u.x.iField variable identifies the index within
153507 ** the vector on the LHS that the virtual term represents.
153508 **
153509 ** This only works if the RHS is a simple SELECT (not a compound) that does
153510 ** not use window functions.
153511 */
153512 else if( pExpr->op==TK_IN49
153513 && pTerm->u.x.iField==0
153514 && pExpr->pLeft->op==TK_VECTOR177
153515 && ALWAYS( ExprUseXSelect(pExpr) )((((pExpr)->flags&0x001000)!=0))
153516 && pExpr->x.pSelect->pPrior==0
153517#ifndef SQLITE_OMIT_WINDOWFUNC
153518 && pExpr->x.pSelect->pWin==0
153519#endif
153520 && pWC->op==TK_AND44
153521 ){
153522 int i;
153523 for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){
153524 int idxNew;
153525 idxNew = whereClauseInsert(pWC, pExpr, TERM_VIRTUAL0x0002|TERM_SLICE0x8000);
153526 pWC->a[idxNew].u.x.iField = i+1;
153527 exprAnalyze(pSrc, pWC, idxNew);
153528 markTermAsChild(pWC, idxNew, idxTerm);
153529 }
153530 }
153531
153532#ifndef SQLITE_OMIT_VIRTUALTABLE
153533 /* Add a WO_AUX auxiliary term to the constraint set if the
153534 ** current expression is of the form "column OP expr" where OP
153535 ** is an operator that gets passed into virtual tables but which is
153536 ** not normally optimized for ordinary tables. In other words, OP
153537 ** is one of MATCH, LIKE, GLOB, REGEXP, !=, IS, IS NOT, or NOT NULL.
153538 ** This information is used by the xBestIndex methods of
153539 ** virtual tables. The native query optimizer does not attempt
153540 ** to do anything with MATCH functions.
153541 */
153542 else if( pWC->op==TK_AND44 ){
153543 Expr *pRight = 0, *pLeft = 0;
153544 int res = isAuxiliaryVtabOperator(db, pExpr, &eOp2, &pLeft, &pRight);
153545 while( res-- > 0 ){
153546 int idxNew;
153547 WhereTerm *pNewTerm;
153548 Bitmask prereqColumn, prereqExpr;
153549
153550 prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight);
153551 prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft);
153552 if( (prereqExpr & prereqColumn)==0 ){
153553 Expr *pNewExpr;
153554 pNewExpr = sqlite3PExpr(pParse, TK_MATCH46,
153555 0, sqlite3ExprDup(db, pRight, 0));
153556 if( ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(0x000001))!=0) && pNewExpr ){
153557 ExprSetProperty(pNewExpr, EP_OuterON)(pNewExpr)->flags|=(0x000001);
153558 pNewExpr->w.iJoin = pExpr->w.iJoin;
153559 }
153560 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL0x0002|TERM_DYNAMIC0x0001);
153561 testcase( idxNew==0 );
153562 pNewTerm = &pWC->a[idxNew];
153563 pNewTerm->prereqRight = prereqExpr;
153564 pNewTerm->leftCursor = pLeft->iTable;
153565 pNewTerm->u.x.leftColumn = pLeft->iColumn;
153566 pNewTerm->eOperator = WO_AUX0x0040;
153567 pNewTerm->eMatchOp = eOp2;
153568 markTermAsChild(pWC, idxNew, idxTerm);
153569 pTerm = &pWC->a[idxTerm];
153570 pTerm->wtFlags |= TERM_COPIED0x0008;
153571 pNewTerm->prereqAll = pTerm->prereqAll;
153572 }
153573 SWAP(Expr*, pLeft, pRight){Expr* t=pLeft; pLeft=pRight; pRight=t;};
153574 }
153575 }
153576#endif /* SQLITE_OMIT_VIRTUALTABLE */
153577
153578 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
153579 ** an index for tables to the left of the join.
153580 */
153581 testcase( pTerm!=&pWC->a[idxTerm] );
153582 pTerm = &pWC->a[idxTerm];
153583 pTerm->prereqRight |= extraRight;
153584}
153585
153586/***************************************************************************
153587** Routines with file scope above. Interface to the rest of the where.c
153588** subsystem follows.
153589***************************************************************************/
153590
153591/*
153592** This routine identifies subexpressions in the WHERE clause where
153593** each subexpression is separated by the AND operator or some other
153594** operator specified in the op parameter. The WhereClause structure
153595** is filled with pointers to subexpressions. For example:
153596**
153597** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
153598** \________/ \_______________/ \________________/
153599** slot[0] slot[1] slot[2]
153600**
153601** The original WHERE clause in pExpr is unaltered. All this routine
153602** does is make slot[] entries point to substructure within pExpr.
153603**
153604** In the previous sentence and in the diagram, "slot[]" refers to
153605** the WhereClause.a[] array. The slot[] array grows as needed to contain
153606** all terms of the WHERE clause.
153607*/
153608SQLITE_PRIVATEstatic void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
153609 Expr *pE2 = sqlite3ExprSkipCollateAndLikely(pExpr);
153610 pWC->op = op;
153611 assert( pE2!=0 || pExpr==0 )((void) (0));
153612 if( pE2==0 ) return;
153613 if( pE2->op!=op ){
153614 whereClauseInsert(pWC, pExpr, 0);
153615 }else{
153616 sqlite3WhereSplit(pWC, pE2->pLeft, op);
153617 sqlite3WhereSplit(pWC, pE2->pRight, op);
153618 }
153619}
153620
153621/*
153622** Add either a LIMIT (if eMatchOp==SQLITE_INDEX_CONSTRAINT_LIMIT) or
153623** OFFSET (if eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET) term to the
153624** where-clause passed as the first argument. The value for the term
153625** is found in register iReg.
153626**
153627** In the common case where the value is a simple integer
153628** (example: "LIMIT 5 OFFSET 10") then the expression codes as a
153629** TK_INTEGER so that it will be available to sqlite3_vtab_rhs_value().
153630** If not, then it codes as a TK_REGISTER expression.
153631*/
153632static void whereAddLimitExpr(
153633 WhereClause *pWC, /* Add the constraint to this WHERE clause */
153634 int iReg, /* Register that will hold value of the limit/offset */
153635 Expr *pExpr, /* Expression that defines the limit/offset */
153636 int iCsr, /* Cursor to which the constraint applies */
153637 int eMatchOp /* SQLITE_INDEX_CONSTRAINT_LIMIT or _OFFSET */
153638){
153639 Parse *pParse = pWC->pWInfo->pParse;
153640 sqlite3 *db = pParse->db;
153641 Expr *pNew;
153642 int iVal = 0;
153643
153644 if( sqlite3ExprIsInteger(pExpr, &iVal) && iVal>=0 ){
153645 Expr *pVal = sqlite3Expr(db, TK_INTEGER155, 0);
153646 if( pVal==0 ) return;
153647 ExprSetProperty(pVal, EP_IntValue)(pVal)->flags|=(0x000800);
153648 pVal->u.iValue = iVal;
153649 pNew = sqlite3PExpr(pParse, TK_MATCH46, 0, pVal);
153650 }else{
153651 Expr *pVal = sqlite3Expr(db, TK_REGISTER176, 0);
153652 if( pVal==0 ) return;
153653 pVal->iTable = iReg;
153654 pNew = sqlite3PExpr(pParse, TK_MATCH46, 0, pVal);
153655 }
153656 if( pNew ){
153657 WhereTerm *pTerm;
153658 int idx;
153659 idx = whereClauseInsert(pWC, pNew, TERM_DYNAMIC0x0001|TERM_VIRTUAL0x0002);
153660 pTerm = &pWC->a[idx];
153661 pTerm->leftCursor = iCsr;
153662 pTerm->eOperator = WO_AUX0x0040;
153663 pTerm->eMatchOp = eMatchOp;
153664 }
153665}
153666
153667/*
153668** Possibly add terms corresponding to the LIMIT and OFFSET clauses of the
153669** SELECT statement passed as the second argument. These terms are only
153670** added if:
153671**
153672** 1. The SELECT statement has a LIMIT clause, and
153673** 2. The SELECT statement is not an aggregate or DISTINCT query, and
153674** 3. The SELECT statement has exactly one object in its from clause, and
153675** that object is a virtual table, and
153676** 4. There are no terms in the WHERE clause that will not be passed
153677** to the virtual table xBestIndex method.
153678** 5. The ORDER BY clause, if any, will be made available to the xBestIndex
153679** method.
153680**
153681** LIMIT and OFFSET terms are ignored by most of the planner code. They
153682** exist only so that they may be passed to the xBestIndex method of the
153683** single virtual table in the FROM clause of the SELECT.
153684*/
153685SQLITE_PRIVATEstatic void sqlite3WhereAddLimit(WhereClause *pWC, Select *p){
153686 assert( p==0 || (p->pGroupBy==0 && (p->selFlags & SF_Aggregate)==0) )((void) (0));
153687 if( (p && p->pLimit) /* 1 */
153688 && (p->selFlags & (SF_Distinct0x0000001|SF_Aggregate0x0000008))==0 /* 2 */
153689 && (p->pSrc->nSrc==1 && IsVirtual(p->pSrc->a[0].pTab)((p->pSrc->a[0].pTab)->eTabType==1)) /* 3 */
153690 ){
153691 ExprList *pOrderBy = p->pOrderBy;
153692 int iCsr = p->pSrc->a[0].iCursor;
153693 int ii;
153694
153695 /* Check condition (4). Return early if it is not met. */
153696 for(ii=0; ii<pWC->nTerm; ii++){
153697 if( pWC->a[ii].wtFlags & TERM_CODED0x0004 ){
153698 /* This term is a vector operation that has been decomposed into
153699 ** other, subsequent terms. It can be ignored. See tag-20220128a */
153700 assert( pWC->a[ii].wtFlags & TERM_VIRTUAL )((void) (0));
153701 assert( pWC->a[ii].eOperator==WO_ROWVAL )((void) (0));
153702 continue;
153703 }
153704 if( pWC->a[ii].leftCursor!=iCsr ) return;
153705 }
153706
153707 /* Check condition (5). Return early if it is not met. */
153708 if( pOrderBy ){
153709 for(ii=0; ii<pOrderBy->nExpr; ii++){
153710 Expr *pExpr = pOrderBy->a[ii].pExpr;
153711 if( pExpr->op!=TK_COLUMN167 ) return;
153712 if( pExpr->iTable!=iCsr ) return;
153713 if( pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_BIGNULL0x02 ) return;
153714 }
153715 }
153716
153717 /* All conditions are met. Add the terms to the where-clause object. */
153718 assert( p->pLimit->op==TK_LIMIT )((void) (0));
153719 whereAddLimitExpr(pWC, p->iLimit, p->pLimit->pLeft,
153720 iCsr, SQLITE_INDEX_CONSTRAINT_LIMIT73);
153721 if( p->iOffset>0 ){
153722 whereAddLimitExpr(pWC, p->iOffset, p->pLimit->pRight,
153723 iCsr, SQLITE_INDEX_CONSTRAINT_OFFSET74);
153724 }
153725 }
153726}
153727
153728/*
153729** Initialize a preallocated WhereClause structure.
153730*/
153731SQLITE_PRIVATEstatic void sqlite3WhereClauseInit(
153732 WhereClause *pWC, /* The WhereClause to be initialized */
153733 WhereInfo *pWInfo /* The WHERE processing context */
153734){
153735 pWC->pWInfo = pWInfo;
153736 pWC->hasOr = 0;
153737 pWC->pOuter = 0;
153738 pWC->nTerm = 0;
153739 pWC->nBase = 0;
153740 pWC->nSlot = ArraySize(pWC->aStatic)((int)(sizeof(pWC->aStatic)/sizeof(pWC->aStatic[0])));
153741 pWC->a = pWC->aStatic;
153742}
153743
153744/*
153745** Deallocate a WhereClause structure. The WhereClause structure
153746** itself is not freed. This routine is the inverse of
153747** sqlite3WhereClauseInit().
153748*/
153749SQLITE_PRIVATEstatic void sqlite3WhereClauseClear(WhereClause *pWC){
153750 sqlite3 *db = pWC->pWInfo->pParse->db;
153751 assert( pWC->nTerm>=pWC->nBase )((void) (0));
153752 if( pWC->nTerm>0 ){
153753 WhereTerm *a = pWC->a;
153754 WhereTerm *aLast = &pWC->a[pWC->nTerm-1];
153755#ifdef SQLITE_DEBUG
153756 int i;
153757 /* Verify that every term past pWC->nBase is virtual */
153758 for(i=pWC->nBase; i<pWC->nTerm; i++){
153759 assert( (pWC->a[i].wtFlags & TERM_VIRTUAL)!=0 )((void) (0));
153760 }
153761#endif
153762 while(1){
153763 assert( a->eMatchOp==0 || a->eOperator==WO_AUX )((void) (0));
153764 if( a->wtFlags & TERM_DYNAMIC0x0001 ){
153765 sqlite3ExprDelete(db, a->pExpr);
153766 }
153767 if( a->wtFlags & (TERM_ORINFO0x0010|TERM_ANDINFO0x0020) ){
153768 if( a->wtFlags & TERM_ORINFO0x0010 ){
153769 assert( (a->wtFlags & TERM_ANDINFO)==0 )((void) (0));
153770 whereOrInfoDelete(db, a->u.pOrInfo);
153771 }else{
153772 assert( (a->wtFlags & TERM_ANDINFO)!=0 )((void) (0));
153773 whereAndInfoDelete(db, a->u.pAndInfo);
153774 }
153775 }
153776 if( a==aLast ) break;
153777 a++;
153778 }
153779 }
153780}
153781
153782
153783/*
153784** These routines walk (recursively) an expression tree and generate
153785** a bitmask indicating which tables are used in that expression
153786** tree.
153787**
153788** sqlite3WhereExprUsage(MaskSet, Expr) ->
153789**
153790** Return a Bitmask of all tables referenced by Expr. Expr can be
153791** be NULL, in which case 0 is returned.
153792**
153793** sqlite3WhereExprUsageNN(MaskSet, Expr) ->
153794**
153795** Same as sqlite3WhereExprUsage() except that Expr must not be
153796** NULL. The "NN" suffix on the name stands for "Not Null".
153797**
153798** sqlite3WhereExprListUsage(MaskSet, ExprList) ->
153799**
153800** Return a Bitmask of all tables referenced by every expression
153801** in the expression list ExprList. ExprList can be NULL, in which
153802** case 0 is returned.
153803**
153804** sqlite3WhereExprUsageFull(MaskSet, ExprList) ->
153805**
153806** Internal use only. Called only by sqlite3WhereExprUsageNN() for
153807** complex expressions that require pushing register values onto
153808** the stack. Many calls to sqlite3WhereExprUsageNN() do not need
153809** the more complex analysis done by this routine. Hence, the
153810** computations done by this routine are broken out into a separate
153811** "no-inline" function to avoid the stack push overhead in the
153812** common case where it is not needed.
153813*/
153814static SQLITE_NOINLINE__attribute__((noinline)) Bitmask sqlite3WhereExprUsageFull(
153815 WhereMaskSet *pMaskSet,
153816 Expr *p
153817){
153818 Bitmask mask;
153819 mask = (p->op==TK_IF_NULL_ROW179) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0;
153820 if( p->pLeft ) mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pLeft);
153821 if( p->pRight ){
153822 mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pRight);
153823 assert( p->x.pList==0 )((void) (0));
153824 }else if( ExprUseXSelect(p)(((p)->flags&0x001000)!=0) ){
153825 if( ExprHasProperty(p, EP_VarSelect)(((p)->flags&(0x000040))!=0) ) pMaskSet->bVarSelect = 1;
153826 mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
153827 }else if( p->x.pList ){
153828 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
153829 }
153830#ifndef SQLITE_OMIT_WINDOWFUNC
153831 if( (p->op==TK_FUNCTION172 || p->op==TK_AGG_FUNCTION168) && ExprUseYWin(p)(((p)->flags&0x1000000)!=0) ){
153832 assert( p->y.pWin!=0 )((void) (0));
153833 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pPartition);
153834 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pOrderBy);
153835 mask |= sqlite3WhereExprUsage(pMaskSet, p->y.pWin->pFilter);
153836 }
153837#endif
153838 return mask;
153839}
153840SQLITE_PRIVATEstatic Bitmask sqlite3WhereExprUsageNN(WhereMaskSet *pMaskSet, Expr *p){
153841 if( p->op==TK_COLUMN167 && !ExprHasProperty(p, EP_FixedCol)(((p)->flags&(0x000020))!=0) ){
153842 return sqlite3WhereGetMask(pMaskSet, p->iTable);
153843 }else if( ExprHasProperty(p, EP_TokenOnly|EP_Leaf)(((p)->flags&(0x010000|0x800000))!=0) ){
153844 assert( p->op!=TK_IF_NULL_ROW )((void) (0));
153845 return 0;
153846 }
153847 return sqlite3WhereExprUsageFull(pMaskSet, p);
153848}
153849SQLITE_PRIVATEstatic Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
153850 return p ? sqlite3WhereExprUsageNN(pMaskSet,p) : 0;
153851}
153852SQLITE_PRIVATEstatic Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){
153853 int i;
153854 Bitmask mask = 0;
153855 if( pList ){
153856 for(i=0; i<pList->nExpr; i++){
153857 mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
153858 }
153859 }
153860 return mask;
153861}
153862
153863
153864/*
153865** Call exprAnalyze on all terms in a WHERE clause.
153866**
153867** Note that exprAnalyze() might add new virtual terms onto the
153868** end of the WHERE clause. We do not want to analyze these new
153869** virtual terms, so start analyzing at the end and work forward
153870** so that the added virtual terms are never processed.
153871*/
153872SQLITE_PRIVATEstatic void sqlite3WhereExprAnalyze(
153873 SrcList *pTabList, /* the FROM clause */
153874 WhereClause *pWC /* the WHERE clause to be analyzed */
153875){
153876 int i;
153877 for(i=pWC->nTerm-1; i>=0; i--){
153878 exprAnalyze(pTabList, pWC, i);
153879 }
153880}
153881
153882/*
153883** For table-valued-functions, transform the function arguments into
153884** new WHERE clause terms.
153885**
153886** Each function argument translates into an equality constraint against
153887** a HIDDEN column in the table.
153888*/
153889SQLITE_PRIVATEstatic void sqlite3WhereTabFuncArgs(
153890 Parse *pParse, /* Parsing context */
153891 SrcItem *pItem, /* The FROM clause term to process */
153892 WhereClause *pWC /* Xfer function arguments to here */
153893){
153894 Table *pTab;
153895 int j, k;
153896 ExprList *pArgs;
153897 Expr *pColRef;
153898 Expr *pTerm;
153899 if( pItem->fg.isTabFunc==0 ) return;
153900 pTab = pItem->pTab;
153901 assert( pTab!=0 )((void) (0));
153902 pArgs = pItem->u1.pFuncArg;
153903 if( pArgs==0 ) return;
153904 for(j=k=0; j<pArgs->nExpr; j++){
153905 Expr *pRhs;
153906 u32 joinType;
153907 while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN0x0002)==0 ){k++;}
153908 if( k>=pTab->nCol ){
153909 sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
153910 pTab->zName, j);
153911 return;
153912 }
153913 pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN167, 0, 0);
153914 if( pColRef==0 ) return;
153915 pColRef->iTable = pItem->iCursor;
153916 pColRef->iColumn = k++;
153917 assert( ExprUseYTab(pColRef) )((void) (0));
153918 pColRef->y.pTab = pTab;
153919 pItem->colUsed |= sqlite3ExprColUsed(pColRef);
153920 pRhs = sqlite3PExpr(pParse, TK_UPLUS174,
153921 sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
153922 pTerm = sqlite3PExpr(pParse, TK_EQ53, pColRef, pRhs);
153923 if( pItem->fg.jointype & (JT_LEFT0x08|JT_LTORJ0x40) ){
153924 joinType = EP_OuterON0x000001;
153925 }else{
153926 joinType = EP_InnerON0x000002;
153927 }
153928 sqlite3SetJoinExpr(pTerm, pItem->iCursor, joinType);
153929 whereClauseInsert(pWC, pTerm, TERM_DYNAMIC0x0001);
153930 }
153931}
153932
153933/************** End of whereexpr.c *******************************************/
153934/************** Begin file where.c *******************************************/
153935/*
153936** 2001 September 15
153937**
153938** The author disclaims copyright to this source code. In place of
153939** a legal notice, here is a blessing:
153940**
153941** May you do good and not evil.
153942** May you find forgiveness for yourself and forgive others.
153943** May you share freely, never taking more than you give.
153944**
153945*************************************************************************
153946** This module contains C code that generates VDBE code used to process
153947** the WHERE clause of SQL statements. This module is responsible for
153948** generating the code that loops through a table looking for applicable
153949** rows. Indices are selected and used to speed the search when doing
153950** so is applicable. Because this module is responsible for selecting
153951** indices, you might also think of this module as the "query optimizer".
153952*/
153953/* #include "sqliteInt.h" */
153954/* #include "whereInt.h" */
153955
153956/*
153957** Extra information appended to the end of sqlite3_index_info but not
153958** visible to the xBestIndex function, at least not directly. The
153959** sqlite3_vtab_collation() interface knows how to reach it, however.
153960**
153961** This object is not an API and can be changed from one release to the
153962** next. As long as allocateIndexInfo() and sqlite3_vtab_collation()
153963** agree on the structure, all will be well.
153964*/
153965typedef struct HiddenIndexInfo HiddenIndexInfo;
153966struct HiddenIndexInfo {
153967 WhereClause *pWC; /* The Where clause being analyzed */
153968 Parse *pParse; /* The parsing context */
153969 int eDistinct; /* Value to return from sqlite3_vtab_distinct() */
153970 u32 mIn; /* Mask of terms that are <col> IN (...) */
153971 u32 mHandleIn; /* Terms that vtab will handle as <col> IN (...) */
153972 sqlite3_value *aRhs[1]; /* RHS values for constraints. MUST BE LAST
153973 ** because extra space is allocated to hold up
153974 ** to nTerm such values */
153975};
153976
153977/* Forward declaration of methods */
153978static int whereLoopResize(sqlite3*, WhereLoop*, int);
153979
153980/*
153981** Return the estimated number of output rows from a WHERE clause
153982*/
153983SQLITE_PRIVATEstatic LogEst sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
153984 return pWInfo->nRowOut;
153985}
153986
153987/*
153988** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
153989** WHERE clause returns outputs for DISTINCT processing.
153990*/
153991SQLITE_PRIVATEstatic int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
153992 return pWInfo->eDistinct;
153993}
153994
153995/*
153996** Return the number of ORDER BY terms that are satisfied by the
153997** WHERE clause. A return of 0 means that the output must be
153998** completely sorted. A return equal to the number of ORDER BY
153999** terms means that no sorting is needed at all. A return that
154000** is positive but less than the number of ORDER BY terms means that
154001** block sorting is required.
154002*/
154003SQLITE_PRIVATEstatic int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
154004 return pWInfo->nOBSat;
154005}
154006
154007/*
154008** In the ORDER BY LIMIT optimization, if the inner-most loop is known
154009** to emit rows in increasing order, and if the last row emitted by the
154010** inner-most loop did not fit within the sorter, then we can skip all
154011** subsequent rows for the current iteration of the inner loop (because they
154012** will not fit in the sorter either) and continue with the second inner
154013** loop - the loop immediately outside the inner-most.
154014**
154015** When a row does not fit in the sorter (because the sorter already
154016** holds LIMIT+OFFSET rows that are smaller), then a jump is made to the
154017** label returned by this function.
154018**
154019** If the ORDER BY LIMIT optimization applies, the jump destination should
154020** be the continuation for the second-inner-most loop. If the ORDER BY
154021** LIMIT optimization does not apply, then the jump destination should
154022** be the continuation for the inner-most loop.
154023**
154024** It is always safe for this routine to return the continuation of the
154025** inner-most loop, in the sense that a correct answer will result.
154026** Returning the continuation the second inner loop is an optimization
154027** that might make the code run a little faster, but should not change
154028** the final answer.
154029*/
154030SQLITE_PRIVATEstatic int sqlite3WhereOrderByLimitOptLabel(WhereInfo *pWInfo){
154031 WhereLevel *pInner;
154032 if( !pWInfo->bOrderedInnerLoop ){
154033 /* The ORDER BY LIMIT optimization does not apply. Jump to the
154034 ** continuation of the inner-most loop. */
154035 return pWInfo->iContinue;
154036 }
154037 pInner = &pWInfo->a[pWInfo->nLevel-1];
154038 assert( pInner->addrNxt!=0 )((void) (0));
154039 return pInner->pRJ ? pWInfo->iContinue : pInner->addrNxt;
154040}
154041
154042/*
154043** While generating code for the min/max optimization, after handling
154044** the aggregate-step call to min() or max(), check to see if any
154045** additional looping is required. If the output order is such that
154046** we are certain that the correct answer has already been found, then
154047** code an OP_Goto to by pass subsequent processing.
154048**
154049** Any extra OP_Goto that is coded here is an optimization. The
154050** correct answer should be obtained regardless. This OP_Goto just
154051** makes the answer appear faster.
154052*/
154053SQLITE_PRIVATEstatic void sqlite3WhereMinMaxOptEarlyOut(Vdbe *v, WhereInfo *pWInfo){
154054 WhereLevel *pInner;
154055 int i;
154056 if( !pWInfo->bOrderedInnerLoop ) return;
154057 if( pWInfo->nOBSat==0 ) return;
154058 for(i=pWInfo->nLevel-1; i>=0; i--){
154059 pInner = &pWInfo->a[i];
154060 if( (pInner->pWLoop->wsFlags & WHERE_COLUMN_IN0x00000004)!=0 ){
154061 sqlite3VdbeGoto(v, pInner->addrNxt);
154062 return;
154063 }
154064 }
154065 sqlite3VdbeGoto(v, pWInfo->iBreak);
154066}
154067
154068/*
154069** Return the VDBE address or label to jump to in order to continue
154070** immediately with the next row of a WHERE clause.
154071*/
154072SQLITE_PRIVATEstatic int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
154073 assert( pWInfo->iContinue!=0 )((void) (0));
154074 return pWInfo->iContinue;
154075}
154076
154077/*
154078** Return the VDBE address or label to jump to in order to break
154079** out of a WHERE loop.
154080*/
154081SQLITE_PRIVATEstatic int sqlite3WhereBreakLabel(WhereInfo *pWInfo){
154082 return pWInfo->iBreak;
154083}
154084
154085/*
154086** Return ONEPASS_OFF (0) if an UPDATE or DELETE statement is unable to
154087** operate directly on the rowids returned by a WHERE clause. Return
154088** ONEPASS_SINGLE (1) if the statement can operation directly because only
154089** a single row is to be changed. Return ONEPASS_MULTI (2) if the one-pass
154090** optimization can be used on multiple
154091**
154092** If the ONEPASS optimization is used (if this routine returns true)
154093** then also write the indices of open cursors used by ONEPASS
154094** into aiCur[0] and aiCur[1]. iaCur[0] gets the cursor of the data
154095** table and iaCur[1] gets the cursor used by an auxiliary index.
154096** Either value may be -1, indicating that cursor is not used.
154097** Any cursors returned will have been opened for writing.
154098**
154099** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
154100** unable to use the ONEPASS optimization.
154101*/
154102SQLITE_PRIVATEstatic int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){
154103 memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
154104#ifdef WHERETRACE_ENABLED
154105 if( sqlite3WhereTrace && pWInfo->eOnePass!=ONEPASS_OFF0 ){
154106 sqlite3DebugPrintf("%s cursors: %d %d\n",
154107 pWInfo->eOnePass==ONEPASS_SINGLE1 ? "ONEPASS_SINGLE" : "ONEPASS_MULTI",
154108 aiCur[0], aiCur[1]);
154109 }
154110#endif
154111 return pWInfo->eOnePass;
154112}
154113
154114/*
154115** Return TRUE if the WHERE loop uses the OP_DeferredSeek opcode to move
154116** the data cursor to the row selected by the index cursor.
154117*/
154118SQLITE_PRIVATEstatic int sqlite3WhereUsesDeferredSeek(WhereInfo *pWInfo){
154119 return pWInfo->bDeferredSeek;
154120}
154121
154122/*
154123** Move the content of pSrc into pDest
154124*/
154125static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){
154126 pDest->n = pSrc->n;
154127 memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
154128}
154129
154130/*
154131** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet.
154132**
154133** The new entry might overwrite an existing entry, or it might be
154134** appended, or it might be discarded. Do whatever is the right thing
154135** so that pSet keeps the N_OR_COST best entries seen so far.
154136*/
154137static int whereOrInsert(
154138 WhereOrSet *pSet, /* The WhereOrSet to be updated */
154139 Bitmask prereq, /* Prerequisites of the new entry */
154140 LogEst rRun, /* Run-cost of the new entry */
154141 LogEst nOut /* Number of outputs for the new entry */
154142){
154143 u16 i;
154144 WhereOrCost *p;
154145 for(i=pSet->n, p=pSet->a; i>0; i--, p++){
154146 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
154147 goto whereOrInsert_done;
154148 }
154149 if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
154150 return 0;
154151 }
154152 }
154153 if( pSet->n<N_OR_COST3 ){
154154 p = &pSet->a[pSet->n++];
154155 p->nOut = nOut;
154156 }else{
154157 p = pSet->a;
154158 for(i=1; i<pSet->n; i++){
154159 if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
154160 }
154161 if( p->rRun<=rRun ) return 0;
154162 }
154163whereOrInsert_done:
154164 p->prereq = prereq;
154165 p->rRun = rRun;
154166 if( p->nOut>nOut ) p->nOut = nOut;
154167 return 1;
154168}
154169
154170/*
154171** Return the bitmask for the given cursor number. Return 0 if
154172** iCursor is not in the set.
154173*/
154174SQLITE_PRIVATEstatic Bitmask sqlite3WhereGetMask(WhereMaskSet *pMaskSet, int iCursor){
154175 int i;
154176 assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 )((void) (0));
154177 assert( pMaskSet->n>0 || pMaskSet->ix[0]<0 )((void) (0));
154178 assert( iCursor>=-1 )((void) (0));
154179 if( pMaskSet->ix[0]==iCursor ){
154180 return 1;
154181 }
154182 for(i=1; i<pMaskSet->n; i++){
154183 if( pMaskSet->ix[i]==iCursor ){
154184 return MASKBIT(i)(((Bitmask)1)<<(i));
154185 }
154186 }
154187 return 0;
154188}
154189
154190/* Allocate memory that is automatically freed when pWInfo is freed.
154191*/
154192SQLITE_PRIVATEstatic void *sqlite3WhereMalloc(WhereInfo *pWInfo, u64 nByte){
154193 WhereMemBlock *pBlock;
154194 pBlock = sqlite3DbMallocRawNN(pWInfo->pParse->db, nByte+sizeof(*pBlock));
154195 if( pBlock ){
154196 pBlock->pNext = pWInfo->pMemToFree;
154197 pBlock->sz = nByte;
154198 pWInfo->pMemToFree = pBlock;
154199 pBlock++;
154200 }
154201 return (void*)pBlock;
154202}
154203SQLITE_PRIVATEstatic void *sqlite3WhereRealloc(WhereInfo *pWInfo, void *pOld, u64 nByte){
154204 void *pNew = sqlite3WhereMalloc(pWInfo, nByte);
154205 if( pNew && pOld ){
154206 WhereMemBlock *pOldBlk = (WhereMemBlock*)pOld;
154207 pOldBlk--;
154208 assert( pOldBlk->sz<nByte )((void) (0));
154209 memcpy(pNew, pOld, pOldBlk->sz);
154210 }
154211 return pNew;
154212}
154213
154214/*
154215** Create a new mask for cursor iCursor.
154216**
154217** There is one cursor per table in the FROM clause. The number of
154218** tables in the FROM clause is limited by a test early in the
154219** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
154220** array will never overflow.
154221*/
154222static void createMask(WhereMaskSet *pMaskSet, int iCursor){
154223 assert( pMaskSet->n < ArraySize(pMaskSet->ix) )((void) (0));
154224 pMaskSet->ix[pMaskSet->n++] = iCursor;
154225}
154226
154227/*
154228** If the right-hand branch of the expression is a TK_COLUMN, then return
154229** a pointer to the right-hand branch. Otherwise, return NULL.
154230*/
154231static Expr *whereRightSubexprIsColumn(Expr *p){
154232 p = sqlite3ExprSkipCollateAndLikely(p->pRight);
154233 if( ALWAYS(p!=0)(p!=0) && p->op==TK_COLUMN167 && !ExprHasProperty(p, EP_FixedCol)(((p)->flags&(0x000020))!=0) ){
154234 return p;
154235 }
154236 return 0;
154237}
154238
154239/*
154240** Advance to the next WhereTerm that matches according to the criteria
154241** established when the pScan object was initialized by whereScanInit().
154242** Return NULL if there are no more matching WhereTerms.
154243*/
154244static WhereTerm *whereScanNext(WhereScan *pScan){
154245 int iCur; /* The cursor on the LHS of the term */
154246 i16 iColumn; /* The column on the LHS of the term. -1 for IPK */
154247 Expr *pX; /* An expression being tested */
154248 WhereClause *pWC; /* Shorthand for pScan->pWC */
154249 WhereTerm *pTerm; /* The term being tested */
154250 int k = pScan->k; /* Where to start scanning */
154251
154252 assert( pScan->iEquiv<=pScan->nEquiv )((void) (0));
154253 pWC = pScan->pWC;
154254 while(1){
154255 iColumn = pScan->aiColumn[pScan->iEquiv-1];
154256 iCur = pScan->aiCur[pScan->iEquiv-1];
154257 assert( pWC!=0 )((void) (0));
154258 assert( iCur>=0 )((void) (0));
154259 do{
154260 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
154261 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 || pTerm->leftCursor<0 )((void) (0));
154262 if( pTerm->leftCursor==iCur
154263 && pTerm->u.x.leftColumn==iColumn
154264 && (iColumn!=XN_EXPR(-2)
154265 || sqlite3ExprCompareSkip(pTerm->pExpr->pLeft,
154266 pScan->pIdxExpr,iCur)==0)
154267 && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_OuterON)(((pTerm->pExpr)->flags&(0x000001))!=0))
154268 ){
154269 if( (pTerm->eOperator & WO_EQUIV0x0800)!=0
154270 && pScan->nEquiv<ArraySize(pScan->aiCur)((int)(sizeof(pScan->aiCur)/sizeof(pScan->aiCur[0])))
154271 && (pX = whereRightSubexprIsColumn(pTerm->pExpr))!=0
154272 ){
154273 int j;
154274 for(j=0; j<pScan->nEquiv; j++){
154275 if( pScan->aiCur[j]==pX->iTable
154276 && pScan->aiColumn[j]==pX->iColumn ){
154277 break;
154278 }
154279 }
154280 if( j==pScan->nEquiv ){
154281 pScan->aiCur[j] = pX->iTable;
154282 pScan->aiColumn[j] = pX->iColumn;
154283 pScan->nEquiv++;
154284 }
154285 }
154286 if( (pTerm->eOperator & pScan->opMask)!=0 ){
154287 /* Verify the affinity and collating sequence match */
154288 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL0x0100)==0 ){
154289 CollSeq *pColl;
154290 Parse *pParse = pWC->pWInfo->pParse;
154291 pX = pTerm->pExpr;
154292 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
154293 continue;
154294 }
154295 assert(pX->pLeft)((void) (0));
154296 pColl = sqlite3ExprCompareCollSeq(pParse, pX);
154297 if( pColl==0 ) pColl = pParse->db->pDfltColl;
154298 if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
154299 continue;
154300 }
154301 }
154302 if( (pTerm->eOperator & (WO_EQ0x0002|WO_IS0x0080))!=0
154303 && (pX = pTerm->pExpr->pRight, ALWAYS(pX!=0)(pX!=0))
154304 && pX->op==TK_COLUMN167
154305 && pX->iTable==pScan->aiCur[0]
154306 && pX->iColumn==pScan->aiColumn[0]
154307 ){
154308 testcase( pTerm->eOperator & WO_IS );
154309 continue;
154310 }
154311 pScan->pWC = pWC;
154312 pScan->k = k+1;
154313#ifdef WHERETRACE_ENABLED
154314 if( sqlite3WhereTrace & 0x20000 ){
154315 int ii;
154316 sqlite3DebugPrintf("SCAN-TERM %p: nEquiv=%d",
154317 pTerm, pScan->nEquiv);
154318 for(ii=0; ii<pScan->nEquiv; ii++){
154319 sqlite3DebugPrintf(" {%d:%d}",
154320 pScan->aiCur[ii], pScan->aiColumn[ii]);
154321 }
154322 sqlite3DebugPrintf("\n");
154323 }
154324#endif
154325 return pTerm;
154326 }
154327 }
154328 }
154329 pWC = pWC->pOuter;
154330 k = 0;
154331 }while( pWC!=0 );
154332 if( pScan->iEquiv>=pScan->nEquiv ) break;
154333 pWC = pScan->pOrigWC;
154334 k = 0;
154335 pScan->iEquiv++;
154336 }
154337 return 0;
154338}
154339
154340/*
154341** This is whereScanInit() for the case of an index on an expression.
154342** It is factored out into a separate tail-recursion subroutine so that
154343** the normal whereScanInit() routine, which is a high-runner, does not
154344** need to push registers onto the stack as part of its prologue.
154345*/
154346static SQLITE_NOINLINE__attribute__((noinline)) WhereTerm *whereScanInitIndexExpr(WhereScan *pScan){
154347 pScan->idxaff = sqlite3ExprAffinity(pScan->pIdxExpr);
154348 return whereScanNext(pScan);
154349}
154350
154351/*
154352** Initialize a WHERE clause scanner object. Return a pointer to the
154353** first match. Return NULL if there are no matches.
154354**
154355** The scanner will be searching the WHERE clause pWC. It will look
154356** for terms of the form "X <op> <expr>" where X is column iColumn of table
154357** iCur. Or if pIdx!=0 then X is column iColumn of index pIdx. pIdx
154358** must be one of the indexes of table iCur.
154359**
154360** The <op> must be one of the operators described by opMask.
154361**
154362** If the search is for X and the WHERE clause contains terms of the
154363** form X=Y then this routine might also return terms of the form
154364** "Y <op> <expr>". The number of levels of transitivity is limited,
154365** but is enough to handle most commonly occurring SQL statements.
154366**
154367** If X is not the INTEGER PRIMARY KEY then X must be compatible with
154368** index pIdx.
154369*/
154370static WhereTerm *whereScanInit(
154371 WhereScan *pScan, /* The WhereScan object being initialized */
154372 WhereClause *pWC, /* The WHERE clause to be scanned */
154373 int iCur, /* Cursor to scan for */
154374 int iColumn, /* Column to scan for */
154375 u32 opMask, /* Operator(s) to scan for */
154376 Index *pIdx /* Must be compatible with this index */
154377){
154378 pScan->pOrigWC = pWC;
154379 pScan->pWC = pWC;
154380 pScan->pIdxExpr = 0;
154381 pScan->idxaff = 0;
154382 pScan->zCollName = 0;
154383 pScan->opMask = opMask;
154384 pScan->k = 0;
154385 pScan->aiCur[0] = iCur;
154386 pScan->nEquiv = 1;
154387 pScan->iEquiv = 1;
154388 if( pIdx ){
154389 int j = iColumn;
154390 iColumn = pIdx->aiColumn[j];
154391 if( iColumn==pIdx->pTable->iPKey ){
154392 iColumn = XN_ROWID(-1);
154393 }else if( iColumn>=0 ){
154394 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
154395 pScan->zCollName = pIdx->azColl[j];
154396 }else if( iColumn==XN_EXPR(-2) ){
154397 pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
154398 pScan->zCollName = pIdx->azColl[j];
154399 pScan->aiColumn[0] = XN_EXPR(-2);
154400 return whereScanInitIndexExpr(pScan);
154401 }
154402 }else if( iColumn==XN_EXPR(-2) ){
154403 return 0;
154404 }
154405 pScan->aiColumn[0] = iColumn;
154406 return whereScanNext(pScan);
154407}
154408
154409/*
154410** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
154411** where X is a reference to the iColumn of table iCur or of index pIdx
154412** if pIdx!=0 and <op> is one of the WO_xx operator codes specified by
154413** the op parameter. Return a pointer to the term. Return 0 if not found.
154414**
154415** If pIdx!=0 then it must be one of the indexes of table iCur.
154416** Search for terms matching the iColumn-th column of pIdx
154417** rather than the iColumn-th column of table iCur.
154418**
154419** The term returned might by Y=<expr> if there is another constraint in
154420** the WHERE clause that specifies that X=Y. Any such constraints will be
154421** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
154422** aiCur[]/iaColumn[] arrays hold X and all its equivalents. There are 11
154423** slots in aiCur[]/aiColumn[] so that means we can look for X plus up to 10
154424** other equivalent values. Hence a search for X will return <expr> if X=A1
154425** and A1=A2 and A2=A3 and ... and A9=A10 and A10=<expr>.
154426**
154427** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
154428** then try for the one with no dependencies on <expr> - in other words where
154429** <expr> is a constant expression of some kind. Only return entries of
154430** the form "X <op> Y" where Y is a column in another table if no terms of
154431** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
154432** exist, try to return a term that does not use WO_EQUIV.
154433*/
154434SQLITE_PRIVATEstatic WhereTerm *sqlite3WhereFindTerm(
154435 WhereClause *pWC, /* The WHERE clause to be searched */
154436 int iCur, /* Cursor number of LHS */
154437 int iColumn, /* Column number of LHS */
154438 Bitmask notReady, /* RHS must not overlap with this mask */
154439 u32 op, /* Mask of WO_xx values describing operator */
154440 Index *pIdx /* Must be compatible with this index, if not NULL */
154441){
154442 WhereTerm *pResult = 0;
154443 WhereTerm *p;
154444 WhereScan scan;
154445
154446 p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
154447 op &= WO_EQ0x0002|WO_IS0x0080;
154448 while( p ){
154449 if( (p->prereqRight & notReady)==0 ){
154450 if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
154451 testcase( p->eOperator & WO_IS );
154452 return p;
154453 }
154454 if( pResult==0 ) pResult = p;
154455 }
154456 p = whereScanNext(&scan);
154457 }
154458 return pResult;
154459}
154460
154461/*
154462** This function searches pList for an entry that matches the iCol-th column
154463** of index pIdx.
154464**
154465** If such an expression is found, its index in pList->a[] is returned. If
154466** no expression is found, -1 is returned.
154467*/
154468static int findIndexCol(
154469 Parse *pParse, /* Parse context */
154470 ExprList *pList, /* Expression list to search */
154471 int iBase, /* Cursor for table associated with pIdx */
154472 Index *pIdx, /* Index to match column of */
154473 int iCol /* Column of index to match */
154474){
154475 int i;
154476 const char *zColl = pIdx->azColl[iCol];
154477
154478 for(i=0; i<pList->nExpr; i++){
154479 Expr *p = sqlite3ExprSkipCollateAndLikely(pList->a[i].pExpr);
154480 if( ALWAYS(p!=0)(p!=0)
154481 && (p->op==TK_COLUMN167 || p->op==TK_AGG_COLUMN169)
154482 && p->iColumn==pIdx->aiColumn[iCol]
154483 && p->iTable==iBase
154484 ){
154485 CollSeq *pColl = sqlite3ExprNNCollSeq(pParse, pList->a[i].pExpr);
154486 if( 0==sqlite3StrICmp(pColl->zName, zColl) ){
154487 return i;
154488 }
154489 }
154490 }
154491
154492 return -1;
154493}
154494
154495/*
154496** Return TRUE if the iCol-th column of index pIdx is NOT NULL
154497*/
154498static int indexColumnNotNull(Index *pIdx, int iCol){
154499 int j;
154500 assert( pIdx!=0 )((void) (0));
154501 assert( iCol>=0 && iCol<pIdx->nColumn )((void) (0));
154502 j = pIdx->aiColumn[iCol];
154503 if( j>=0 ){
154504 return pIdx->pTable->aCol[j].notNull;
154505 }else if( j==(-1) ){
154506 return 1;
154507 }else{
154508 assert( j==(-2) )((void) (0));
154509 return 0; /* Assume an indexed expression can always yield a NULL */
154510
154511 }
154512}
154513
154514/*
154515** Return true if the DISTINCT expression-list passed as the third argument
154516** is redundant.
154517**
154518** A DISTINCT list is redundant if any subset of the columns in the
154519** DISTINCT list are collectively unique and individually non-null.
154520*/
154521static int isDistinctRedundant(
154522 Parse *pParse, /* Parsing context */
154523 SrcList *pTabList, /* The FROM clause */
154524 WhereClause *pWC, /* The WHERE clause */
154525 ExprList *pDistinct /* The result set that needs to be DISTINCT */
154526){
154527 Table *pTab;
154528 Index *pIdx;
154529 int i;
154530 int iBase;
154531
154532 /* If there is more than one table or sub-select in the FROM clause of
154533 ** this query, then it will not be possible to show that the DISTINCT
154534 ** clause is redundant. */
154535 if( pTabList->nSrc!=1 ) return 0;
154536 iBase = pTabList->a[0].iCursor;
154537 pTab = pTabList->a[0].pTab;
154538
154539 /* If any of the expressions is an IPK column on table iBase, then return
154540 ** true. Note: The (p->iTable==iBase) part of this test may be false if the
154541 ** current SELECT is a correlated sub-query.
154542 */
154543 for(i=0; i<pDistinct->nExpr; i++){
154544 Expr *p = sqlite3ExprSkipCollateAndLikely(pDistinct->a[i].pExpr);
154545 if( NEVER(p==0)(p==0) ) continue;
154546 if( p->op!=TK_COLUMN167 && p->op!=TK_AGG_COLUMN169 ) continue;
154547 if( p->iTable==iBase && p->iColumn<0 ) return 1;
154548 }
154549
154550 /* Loop through all indices on the table, checking each to see if it makes
154551 ** the DISTINCT qualifier redundant. It does so if:
154552 **
154553 ** 1. The index is itself UNIQUE, and
154554 **
154555 ** 2. All of the columns in the index are either part of the pDistinct
154556 ** list, or else the WHERE clause contains a term of the form "col=X",
154557 ** where X is a constant value. The collation sequences of the
154558 ** comparison and select-list expressions must match those of the index.
154559 **
154560 ** 3. All of those index columns for which the WHERE clause does not
154561 ** contain a "col=X" term are subject to a NOT NULL constraint.
154562 */
154563 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
154564 if( !IsUniqueIndex(pIdx)((pIdx)->onError!=0) ) continue;
154565 if( pIdx->pPartIdxWhere ) continue;
154566 for(i=0; i<pIdx->nKeyCol; i++){
154567 if( 0==sqlite3WhereFindTerm(pWC, iBase, i, ~(Bitmask)0, WO_EQ0x0002, pIdx) ){
154568 if( findIndexCol(pParse, pDistinct, iBase, pIdx, i)<0 ) break;
154569 if( indexColumnNotNull(pIdx, i)==0 ) break;
154570 }
154571 }
154572 if( i==pIdx->nKeyCol ){
154573 /* This index implies that the DISTINCT qualifier is redundant. */
154574 return 1;
154575 }
154576 }
154577
154578 return 0;
154579}
154580
154581
154582/*
154583** Estimate the logarithm of the input value to base 2.
154584*/
154585static LogEst estLog(LogEst N){
154586 return N<=10 ? 0 : sqlite3LogEst(N) - 33;
154587}
154588
154589/*
154590** Convert OP_Column opcodes to OP_Copy in previously generated code.
154591**
154592** This routine runs over generated VDBE code and translates OP_Column
154593** opcodes into OP_Copy when the table is being accessed via co-routine
154594** instead of via table lookup.
154595**
154596** If the iAutoidxCur is not zero, then any OP_Rowid instructions on
154597** cursor iTabCur are transformed into OP_Sequence opcode for the
154598** iAutoidxCur cursor, in order to generate unique rowids for the
154599** automatic index being generated.
154600*/
154601static void translateColumnToCopy(
154602 Parse *pParse, /* Parsing context */
154603 int iStart, /* Translate from this opcode to the end */
154604 int iTabCur, /* OP_Column/OP_Rowid references to this table */
154605 int iRegister, /* The first column is in this register */
154606 int iAutoidxCur /* If non-zero, cursor of autoindex being generated */
154607){
154608 Vdbe *v = pParse->pVdbe;
154609 VdbeOp *pOp = sqlite3VdbeGetOp(v, iStart);
154610 int iEnd = sqlite3VdbeCurrentAddr(v);
154611 if( pParse->db->mallocFailed ) return;
154612 for(; iStart<iEnd; iStart++, pOp++){
154613 if( pOp->p1!=iTabCur ) continue;
154614 if( pOp->opcode==OP_Column94 ){
154615 pOp->opcode = OP_Copy80;
154616 pOp->p1 = pOp->p2 + iRegister;
154617 pOp->p2 = pOp->p3;
154618 pOp->p3 = 0;
154619 pOp->p5 = 2; /* Cause the MEM_Subtype flag to be cleared */
154620 }else if( pOp->opcode==OP_Rowid135 ){
154621 pOp->opcode = OP_Sequence126;
154622 pOp->p1 = iAutoidxCur;
154623#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
154624 if( iAutoidxCur==0 ){
154625 pOp->opcode = OP_Null75;
154626 pOp->p3 = 0;
154627 }
154628#endif
154629 }
154630 }
154631}
154632
154633/*
154634** Two routines for printing the content of an sqlite3_index_info
154635** structure. Used for testing and debugging only. If neither
154636** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
154637** are no-ops.
154638*/
154639#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED)
154640static void whereTraceIndexInfoInputs(sqlite3_index_info *p){
154641 int i;
154642 if( !sqlite3WhereTrace ) return;
154643 for(i=0; i<p->nConstraint; i++){
154644 sqlite3DebugPrintf(
154645 " constraint[%d]: col=%d termid=%d op=%d usabled=%d collseq=%s\n",
154646 i,
154647 p->aConstraint[i].iColumn,
154648 p->aConstraint[i].iTermOffset,
154649 p->aConstraint[i].op,
154650 p->aConstraint[i].usable,
154651 sqlite3_vtab_collation(p,i));
154652 }
154653 for(i=0; i<p->nOrderBy; i++){
154654 sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n",
154655 i,
154656 p->aOrderBy[i].iColumn,
154657 p->aOrderBy[i].desc);
154658 }
154659}
154660static void whereTraceIndexInfoOutputs(sqlite3_index_info *p){
154661 int i;
154662 if( !sqlite3WhereTrace ) return;
154663 for(i=0; i<p->nConstraint; i++){
154664 sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n",
154665 i,
154666 p->aConstraintUsage[i].argvIndex,
154667 p->aConstraintUsage[i].omit);
154668 }
154669 sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum);
154670 sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr);
154671 sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed);
154672 sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost);
154673 sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows);
154674}
154675#else
154676#define whereTraceIndexInfoInputs(A)
154677#define whereTraceIndexInfoOutputs(A)
154678#endif
154679
154680#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
154681/*
154682** Return TRUE if the WHERE clause term pTerm is of a form where it
154683** could be used with an index to access pSrc, assuming an appropriate
154684** index existed.
154685*/
154686static int termCanDriveIndex(
154687 const WhereTerm *pTerm, /* WHERE clause term to check */
154688 const SrcItem *pSrc, /* Table we are trying to access */
154689 const Bitmask notReady /* Tables in outer loops of the join */
154690){
154691 char aff;
154692 if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
154693 if( (pTerm->eOperator & (WO_EQ0x0002|WO_IS0x0080))==0 ) return 0;
154694 assert( (pSrc->fg.jointype & JT_RIGHT)==0 )((void) (0));
154695 if( (pSrc->fg.jointype & (JT_LEFT0x08|JT_LTORJ0x40|JT_RIGHT0x10))!=0 ){
154696 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LEFT );
154697 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LTORJ );
154698 testcase( ExprHasProperty(pTerm->pExpr, EP_OuterON) )
154699 testcase( ExprHasProperty(pTerm->pExpr, EP_InnerON) );
154700 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON)(((pTerm->pExpr)->flags&(0x000001|0x000002))!=0)
154701 || pTerm->pExpr->w.iJoin != pSrc->iCursor
154702 ){
154703 return 0; /* See tag-20191211-001 */
154704 }
154705 }
154706 if( (pTerm->prereqRight & notReady)!=0 ) return 0;
154707 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
154708 if( pTerm->u.x.leftColumn<0 ) return 0;
154709 aff = pSrc->pTab->aCol[pTerm->u.x.leftColumn].affinity;
154710 if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
154711 testcase( pTerm->pExpr->op==TK_IS );
154712 return 1;
154713}
154714#endif
154715
154716
154717#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
154718/*
154719** Generate code to construct the Index object for an automatic index
154720** and to set up the WhereLevel object pLevel so that the code generator
154721** makes use of the automatic index.
154722*/
154723static SQLITE_NOINLINE__attribute__((noinline)) void constructAutomaticIndex(
154724 Parse *pParse, /* The parsing context */
154725 const WhereClause *pWC, /* The WHERE clause */
154726 const SrcItem *pSrc, /* The FROM clause term to get the next index */
154727 const Bitmask notReady, /* Mask of cursors that are not available */
154728 WhereLevel *pLevel /* Write new index here */
154729){
154730 int nKeyCol; /* Number of columns in the constructed index */
154731 WhereTerm *pTerm; /* A single term of the WHERE clause */
154732 WhereTerm *pWCEnd; /* End of pWC->a[] */
154733 Index *pIdx; /* Object describing the transient index */
154734 Vdbe *v; /* Prepared statement under construction */
154735 int addrInit; /* Address of the initialization bypass jump */
154736 Table *pTable; /* The table being indexed */
154737 int addrTop; /* Top of the index fill loop */
154738 int regRecord; /* Register holding an index record */
154739 int n; /* Column counter */
154740 int i; /* Loop counter */
154741 int mxBitCol; /* Maximum column in pSrc->colUsed */
154742 CollSeq *pColl; /* Collating sequence to on a column */
154743 WhereLoop *pLoop; /* The Loop object */
154744 char *zNotUsed; /* Extra space on the end of pIdx */
154745 Bitmask idxCols; /* Bitmap of columns used for indexing */
154746 Bitmask extraCols; /* Bitmap of additional columns */
154747 u8 sentWarning = 0; /* True if a warnning has been issued */
154748 Expr *pPartial = 0; /* Partial Index Expression */
154749 int iContinue = 0; /* Jump here to skip excluded rows */
154750 SrcItem *pTabItem; /* FROM clause term being indexed */
154751 int addrCounter = 0; /* Address where integer counter is initialized */
154752 int regBase; /* Array of registers where record is assembled */
154753
154754 /* Generate code to skip over the creation and initialization of the
154755 ** transient index on 2nd and subsequent iterations of the loop. */
154756 v = pParse->pVdbe;
154757 assert( v!=0 )((void) (0));
154758 addrInit = sqlite3VdbeAddOp0(v, OP_Once14); VdbeCoverage(v);
154759
154760 /* Count the number of columns that will be added to the index
154761 ** and used to match WHERE clause constraints */
154762 nKeyCol = 0;
154763 pTable = pSrc->pTab;
154764 pWCEnd = &pWC->a[pWC->nTerm];
154765 pLoop = pLevel->pWLoop;
154766 idxCols = 0;
154767 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
154768 Expr *pExpr = pTerm->pExpr;
154769 /* Make the automatic index a partial index if there are terms in the
154770 ** WHERE clause (or the ON clause of a LEFT join) that constrain which
154771 ** rows of the target table (pSrc) that can be used. */
154772 if( (pTerm->wtFlags & TERM_VIRTUAL0x0002)==0
154773 && sqlite3ExprIsTableConstraint(pExpr, pSrc)
154774 ){
154775 pPartial = sqlite3ExprAnd(pParse, pPartial,
154776 sqlite3ExprDup(pParse->db, pExpr, 0));
154777 }
154778 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
154779 int iCol;
154780 Bitmask cMask;
154781 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
154782 iCol = pTerm->u.x.leftColumn;
154783 cMask = iCol>=BMS((int)(sizeof(Bitmask)*8)) ? MASKBIT(BMS-1)(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1)) : MASKBIT(iCol)(((Bitmask)1)<<(iCol));
154784 testcase( iCol==BMS );
154785 testcase( iCol==BMS-1 );
154786 if( !sentWarning ){
154787 sqlite3_log(SQLITE_WARNING_AUTOINDEX(28 | (1<<8)),
154788 "automatic index on %s(%s)", pTable->zName,
154789 pTable->aCol[iCol].zCnName);
154790 sentWarning = 1;
154791 }
154792 if( (idxCols & cMask)==0 ){
154793 if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){
154794 goto end_auto_index_create;
154795 }
154796 pLoop->aLTerm[nKeyCol++] = pTerm;
154797 idxCols |= cMask;
154798 }
154799 }
154800 }
154801 assert( nKeyCol>0 || pParse->db->mallocFailed )((void) (0));
154802 pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
154803 pLoop->wsFlags = WHERE_COLUMN_EQ0x00000001 | WHERE_IDX_ONLY0x00000040 | WHERE_INDEXED0x00000200
154804 | WHERE_AUTO_INDEX0x00004000;
154805
154806 /* Count the number of additional columns needed to create a
154807 ** covering index. A "covering index" is an index that contains all
154808 ** columns that are needed by the query. With a covering index, the
154809 ** original table never needs to be accessed. Automatic indices must
154810 ** be a covering index because the index will not be updated if the
154811 ** original table changes and the index and table cannot both be used
154812 ** if they go out of sync.
154813 */
154814 extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1)));
154815 mxBitCol = MIN(BMS-1,pTable->nCol)((((int)(sizeof(Bitmask)*8))-1)<(pTable->nCol)?(((int)(
sizeof(Bitmask)*8))-1):(pTable->nCol))
;
154816 testcase( pTable->nCol==BMS-1 );
154817 testcase( pTable->nCol==BMS-2 );
154818 for(i=0; i<mxBitCol; i++){
154819 if( extraCols & MASKBIT(i)(((Bitmask)1)<<(i)) ) nKeyCol++;
154820 }
154821 if( pSrc->colUsed & MASKBIT(BMS-1)(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1)) ){
154822 nKeyCol += pTable->nCol - BMS((int)(sizeof(Bitmask)*8)) + 1;
154823 }
154824
154825 /* Construct the Index object to describe this index */
154826 pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
154827 if( pIdx==0 ) goto end_auto_index_create;
154828 pLoop->u.btree.pIndex = pIdx;
154829 pIdx->zName = "auto-index";
154830 pIdx->pTable = pTable;
154831 n = 0;
154832 idxCols = 0;
154833 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
154834 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
154835 int iCol;
154836 Bitmask cMask;
154837 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
154838 iCol = pTerm->u.x.leftColumn;
154839 cMask = iCol>=BMS((int)(sizeof(Bitmask)*8)) ? MASKBIT(BMS-1)(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1)) : MASKBIT(iCol)(((Bitmask)1)<<(iCol));
154840 testcase( iCol==BMS-1 );
154841 testcase( iCol==BMS );
154842 if( (idxCols & cMask)==0 ){
154843 Expr *pX = pTerm->pExpr;
154844 idxCols |= cMask;
154845 pIdx->aiColumn[n] = pTerm->u.x.leftColumn;
154846 pColl = sqlite3ExprCompareCollSeq(pParse, pX);
154847 assert( pColl!=0 || pParse->nErr>0 )((void) (0)); /* TH3 collate01.800 */
154848 pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY;
154849 n++;
154850 }
154851 }
154852 }
154853 assert( (u32)n==pLoop->u.btree.nEq )((void) (0));
154854
154855 /* Add additional columns needed to make the automatic index into
154856 ** a covering index */
154857 for(i=0; i<mxBitCol; i++){
154858 if( extraCols & MASKBIT(i)(((Bitmask)1)<<(i)) ){
154859 pIdx->aiColumn[n] = i;
154860 pIdx->azColl[n] = sqlite3StrBINARY;
154861 n++;
154862 }
154863 }
154864 if( pSrc->colUsed & MASKBIT(BMS-1)(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1)) ){
154865 for(i=BMS((int)(sizeof(Bitmask)*8))-1; i<pTable->nCol; i++){
154866 pIdx->aiColumn[n] = i;
154867 pIdx->azColl[n] = sqlite3StrBINARY;
154868 n++;
154869 }
154870 }
154871 assert( n==nKeyCol )((void) (0));
154872 pIdx->aiColumn[n] = XN_ROWID(-1);
154873 pIdx->azColl[n] = sqlite3StrBINARY;
154874
154875 /* Create the automatic index */
154876 assert( pLevel->iIdxCur>=0 )((void) (0));
154877 pLevel->iIdxCur = pParse->nTab++;
154878 sqlite3VdbeAddOp2(v, OP_OpenAutoindex116, pLevel->iIdxCur, nKeyCol+1);
154879 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
154880 VdbeComment((v, "for %s", pTable->zName));
154881 if( OptimizationEnabled(pParse->db, SQLITE_BloomFilter)(((pParse->db)->dbOptFlags&(0x00080000))==0) ){
154882 pLevel->regFilter = ++pParse->nMem;
154883 sqlite3VdbeAddOp2(v, OP_Blob77, 10000, pLevel->regFilter);
154884 }
154885
154886 /* Fill the automatic index with content */
154887 pTabItem = &pWC->pWInfo->pTabList->a[pLevel->iFrom];
154888 if( pTabItem->fg.viaCoroutine ){
154889 int regYield = pTabItem->regReturn;
154890 addrCounter = sqlite3VdbeAddOp2(v, OP_Integer71, 0, 0);
154891 sqlite3VdbeAddOp3(v, OP_InitCoroutine10, regYield, 0, pTabItem->addrFillSub);
154892 addrTop = sqlite3VdbeAddOp1(v, OP_Yield11, regYield);
154893 VdbeCoverage(v);
154894 VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
154895 }else{
154896 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind35, pLevel->iTabCur); VdbeCoverage(v);
154897 }
154898 if( pPartial ){
154899 iContinue = sqlite3VdbeMakeLabel(pParse);
154900 sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL0x10);
154901 pLoop->wsFlags |= WHERE_PARTIALIDX0x00020000;
154902 }
154903 regRecord = sqlite3GetTempReg(pParse);
154904 regBase = sqlite3GenerateIndexKey(
154905 pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0
154906 );
154907 if( pLevel->regFilter ){
154908 sqlite3VdbeAddOp4Int(v, OP_FilterAdd180, pLevel->regFilter, 0,
154909 regBase, pLoop->u.btree.nEq);
154910 }
154911 sqlite3VdbeAddOp2(v, OP_IdxInsert138, pLevel->iIdxCur, regRecord);
154912 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT0x10);
154913 if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue);
154914 if( pTabItem->fg.viaCoroutine ){
154915 sqlite3VdbeChangeP2(v, addrCounter, regBase+n);
154916 testcase( pParse->db->mallocFailed );
154917 assert( pLevel->iIdxCur>0 )((void) (0));
154918 translateColumnToCopy(pParse, addrTop, pLevel->iTabCur,
154919 pTabItem->regResult, pLevel->iIdxCur);
154920 sqlite3VdbeGoto(v, addrTop);
154921 pTabItem->fg.viaCoroutine = 0;
154922 }else{
154923 sqlite3VdbeAddOp2(v, OP_Next38, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
154924 sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX3);
154925 }
154926 sqlite3VdbeJumpHere(v, addrTop);
154927 sqlite3ReleaseTempReg(pParse, regRecord);
154928
154929 /* Jump here when skipping the initialization */
154930 sqlite3VdbeJumpHere(v, addrInit);
154931
154932end_auto_index_create:
154933 sqlite3ExprDelete(pParse->db, pPartial);
154934}
154935#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
154936
154937/*
154938** Generate bytecode that will initialize a Bloom filter that is appropriate
154939** for pLevel.
154940**
154941** If there are inner loops within pLevel that have the WHERE_BLOOMFILTER
154942** flag set, initialize a Bloomfilter for them as well. Except don't do
154943** this recursive initialization if the SQLITE_BloomPulldown optimization has
154944** been turned off.
154945**
154946** When the Bloom filter is initialized, the WHERE_BLOOMFILTER flag is cleared
154947** from the loop, but the regFilter value is set to a register that implements
154948** the Bloom filter. When regFilter is positive, the
154949** sqlite3WhereCodeOneLoopStart() will generate code to test the Bloom filter
154950** and skip the subsequence B-Tree seek if the Bloom filter indicates that
154951** no matching rows exist.
154952**
154953** This routine may only be called if it has previously been determined that
154954** the loop would benefit from a Bloom filter, and the WHERE_BLOOMFILTER bit
154955** is set.
154956*/
154957static SQLITE_NOINLINE__attribute__((noinline)) void sqlite3ConstructBloomFilter(
154958 WhereInfo *pWInfo, /* The WHERE clause */
154959 int iLevel, /* Index in pWInfo->a[] that is pLevel */
154960 WhereLevel *pLevel, /* Make a Bloom filter for this FROM term */
154961 Bitmask notReady /* Loops that are not ready */
154962){
154963 int addrOnce; /* Address of opening OP_Once */
154964 int addrTop; /* Address of OP_Rewind */
154965 int addrCont; /* Jump here to skip a row */
154966 const WhereTerm *pTerm; /* For looping over WHERE clause terms */
154967 const WhereTerm *pWCEnd; /* Last WHERE clause term */
154968 Parse *pParse = pWInfo->pParse; /* Parsing context */
154969 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
154970 WhereLoop *pLoop = pLevel->pWLoop; /* The loop being coded */
154971 int iCur; /* Cursor for table getting the filter */
154972
154973 assert( pLoop!=0 )((void) (0));
154974 assert( v!=0 )((void) (0));
154975 assert( pLoop->wsFlags & WHERE_BLOOMFILTER )((void) (0));
154976
154977 addrOnce = sqlite3VdbeAddOp0(v, OP_Once14); VdbeCoverage(v);
154978 do{
154979 const SrcItem *pItem;
154980 const Table *pTab;
154981 u64 sz;
154982 sqlite3WhereExplainBloomFilter(pParse, pWInfo, pLevel);
154983 addrCont = sqlite3VdbeMakeLabel(pParse);
154984 iCur = pLevel->iTabCur;
154985 pLevel->regFilter = ++pParse->nMem;
154986
154987 /* The Bloom filter is a Blob held in a register. Initialize it
154988 ** to zero-filled blob of at least 80K bits, but maybe more if the
154989 ** estimated size of the table is larger. We could actually
154990 ** measure the size of the table at run-time using OP_Count with
154991 ** P3==1 and use that value to initialize the blob. But that makes
154992 ** testing complicated. By basing the blob size on the value in the
154993 ** sqlite_stat1 table, testing is much easier.
154994 */
154995 pItem = &pWInfo->pTabList->a[pLevel->iFrom];
154996 assert( pItem!=0 )((void) (0));
154997 pTab = pItem->pTab;
154998 assert( pTab!=0 )((void) (0));
154999 sz = sqlite3LogEstToInt(pTab->nRowLogEst);
155000 if( sz<10000 ){
155001 sz = 10000;
155002 }else if( sz>10000000 ){
155003 sz = 10000000;
155004 }
155005 sqlite3VdbeAddOp2(v, OP_Blob77, (int)sz, pLevel->regFilter);
155006
155007 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind35, iCur); VdbeCoverage(v);
155008 pWCEnd = &pWInfo->sWC.a[pWInfo->sWC.nTerm];
155009 for(pTerm=pWInfo->sWC.a; pTerm<pWCEnd; pTerm++){
155010 Expr *pExpr = pTerm->pExpr;
155011 if( (pTerm->wtFlags & TERM_VIRTUAL0x0002)==0
155012 && sqlite3ExprIsTableConstraint(pExpr, pItem)
155013 ){
155014 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL0x10);
155015 }
155016 }
155017 if( pLoop->wsFlags & WHERE_IPK0x00000100 ){
155018 int r1 = sqlite3GetTempReg(pParse);
155019 sqlite3VdbeAddOp2(v, OP_Rowid135, iCur, r1);
155020 sqlite3VdbeAddOp4Int(v, OP_FilterAdd180, pLevel->regFilter, 0, r1, 1);
155021 sqlite3ReleaseTempReg(pParse, r1);
155022 }else{
155023 Index *pIdx = pLoop->u.btree.pIndex;
155024 int n = pLoop->u.btree.nEq;
155025 int r1 = sqlite3GetTempRange(pParse, n);
155026 int jj;
155027 for(jj=0; jj<n; jj++){
155028 int iCol = pIdx->aiColumn[jj];
155029 assert( pIdx->pTable==pItem->pTab )((void) (0));
155030 sqlite3ExprCodeGetColumnOfTable(v, pIdx->pTable, iCur, iCol,r1+jj);
155031 }
155032 sqlite3VdbeAddOp4Int(v, OP_FilterAdd180, pLevel->regFilter, 0, r1, n);
155033 sqlite3ReleaseTempRange(pParse, r1, n);
155034 }
155035 sqlite3VdbeResolveLabel(v, addrCont);
155036 sqlite3VdbeAddOp2(v, OP_Next38, pLevel->iTabCur, addrTop+1);
155037 VdbeCoverage(v);
155038 sqlite3VdbeJumpHere(v, addrTop);
155039 pLoop->wsFlags &= ~WHERE_BLOOMFILTER0x00400000;
155040 if( OptimizationDisabled(pParse->db, SQLITE_BloomPulldown)(((pParse->db)->dbOptFlags&(0x00100000))!=0) ) break;
155041 while( ++iLevel < pWInfo->nLevel ){
155042 const SrcItem *pTabItem;
155043 pLevel = &pWInfo->a[iLevel];
155044 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
155045 if( pTabItem->fg.jointype & (JT_LEFT0x08|JT_LTORJ0x40) ) continue;
155046 pLoop = pLevel->pWLoop;
155047 if( NEVER(pLoop==0)(pLoop==0) ) continue;
155048 if( pLoop->prereq & notReady ) continue;
155049 if( (pLoop->wsFlags & (WHERE_BLOOMFILTER0x00400000|WHERE_COLUMN_IN0x00000004))
155050 ==WHERE_BLOOMFILTER0x00400000
155051 ){
155052 /* This is a candidate for bloom-filter pull-down (early evaluation).
155053 ** The test that WHERE_COLUMN_IN is omitted is important, as we are
155054 ** not able to do early evaluation of bloom filters that make use of
155055 ** the IN operator */
155056 break;
155057 }
155058 }
155059 }while( iLevel < pWInfo->nLevel );
155060 sqlite3VdbeJumpHere(v, addrOnce);
155061}
155062
155063
155064#ifndef SQLITE_OMIT_VIRTUALTABLE
155065/*
155066** Allocate and populate an sqlite3_index_info structure. It is the
155067** responsibility of the caller to eventually release the structure
155068** by passing the pointer returned by this function to freeIndexInfo().
155069*/
155070static sqlite3_index_info *allocateIndexInfo(
155071 WhereInfo *pWInfo, /* The WHERE clause */
155072 WhereClause *pWC, /* The WHERE clause being analyzed */
155073 Bitmask mUnusable, /* Ignore terms with these prereqs */
155074 SrcItem *pSrc, /* The FROM clause term that is the vtab */
155075 u16 *pmNoOmit /* Mask of terms not to omit */
155076){
155077 int i, j;
155078 int nTerm;
155079 Parse *pParse = pWInfo->pParse;
155080 struct sqlite3_index_constraint *pIdxCons;
155081 struct sqlite3_index_orderby *pIdxOrderBy;
155082 struct sqlite3_index_constraint_usage *pUsage;
155083 struct HiddenIndexInfo *pHidden;
155084 WhereTerm *pTerm;
155085 int nOrderBy;
155086 sqlite3_index_info *pIdxInfo;
155087 u16 mNoOmit = 0;
155088 const Table *pTab;
155089 int eDistinct = 0;
155090 ExprList *pOrderBy = pWInfo->pOrderBy;
155091
155092 assert( pSrc!=0 )((void) (0));
155093 pTab = pSrc->pTab;
155094 assert( pTab!=0 )((void) (0));
155095 assert( IsVirtual(pTab) )((void) (0));
155096
155097 /* Find all WHERE clause constraints referring to this virtual table.
155098 ** Mark each term with the TERM_OK flag. Set nTerm to the number of
155099 ** terms found.
155100 */
155101 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
155102 pTerm->wtFlags &= ~TERM_OK0x0040;
155103 if( pTerm->leftCursor != pSrc->iCursor ) continue;
155104 if( pTerm->prereqRight & mUnusable ) continue;
155105 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) )((void) (0));
155106 testcase( pTerm->eOperator & WO_IN );
155107 testcase( pTerm->eOperator & WO_ISNULL );
155108 testcase( pTerm->eOperator & WO_IS );
155109 testcase( pTerm->eOperator & WO_ALL );
155110 if( (pTerm->eOperator & ~(WO_EQUIV0x0800))==0 ) continue;
155111 if( pTerm->wtFlags & TERM_VNULL0x0080 ) continue;
155112
155113 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
155114 assert( pTerm->u.x.leftColumn>=XN_ROWID )((void) (0));
155115 assert( pTerm->u.x.leftColumn<pTab->nCol )((void) (0));
155116
155117 /* tag-20191211-002: WHERE-clause constraints are not useful to the
155118 ** right-hand table of a LEFT JOIN nor to the either table of a
155119 ** RIGHT JOIN. See tag-20191211-001 for the
155120 ** equivalent restriction for ordinary tables. */
155121 if( (pSrc->fg.jointype & (JT_LEFT0x08|JT_LTORJ0x40|JT_RIGHT0x10))!=0 ){
155122 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LEFT );
155123 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_RIGHT );
155124 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LTORJ );
155125 testcase( ExprHasProperty(pTerm->pExpr, EP_OuterON) );
155126 testcase( ExprHasProperty(pTerm->pExpr, EP_InnerON) );
155127 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON)(((pTerm->pExpr)->flags&(0x000001|0x000002))!=0)
155128 || pTerm->pExpr->w.iJoin != pSrc->iCursor
155129 ){
155130 continue;
155131 }
155132 }
155133 nTerm++;
155134 pTerm->wtFlags |= TERM_OK0x0040;
155135 }
155136
155137 /* If the ORDER BY clause contains only columns in the current
155138 ** virtual table then allocate space for the aOrderBy part of
155139 ** the sqlite3_index_info structure.
155140 */
155141 nOrderBy = 0;
155142 if( pOrderBy ){
155143 int n = pOrderBy->nExpr;
155144 for(i=0; i<n; i++){
155145 Expr *pExpr = pOrderBy->a[i].pExpr;
155146 Expr *pE2;
155147
155148 /* Skip over constant terms in the ORDER BY clause */
155149 if( sqlite3ExprIsConstant(pExpr) ){
155150 continue;
155151 }
155152
155153 /* Virtual tables are unable to deal with NULLS FIRST */
155154 if( pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_BIGNULL0x02 ) break;
155155
155156 /* First case - a direct column references without a COLLATE operator */
155157 if( pExpr->op==TK_COLUMN167 && pExpr->iTable==pSrc->iCursor ){
155158 assert( pExpr->iColumn>=XN_ROWID && pExpr->iColumn<pTab->nCol )((void) (0));
155159 continue;
155160 }
155161
155162 /* 2nd case - a column reference with a COLLATE operator. Only match
155163 ** of the COLLATE operator matches the collation of the column. */
155164 if( pExpr->op==TK_COLLATE113
155165 && (pE2 = pExpr->pLeft)->op==TK_COLUMN167
155166 && pE2->iTable==pSrc->iCursor
155167 ){
155168 const char *zColl; /* The collating sequence name */
155169 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
155170 assert( pExpr->u.zToken!=0 )((void) (0));
155171 assert( pE2->iColumn>=XN_ROWID && pE2->iColumn<pTab->nCol )((void) (0));
155172 pExpr->iColumn = pE2->iColumn;
155173 if( pE2->iColumn<0 ) continue; /* Collseq does not matter for rowid */
155174 zColl = sqlite3ColumnColl(&pTab->aCol[pE2->iColumn]);
155175 if( zColl==0 ) zColl = sqlite3StrBINARY;
155176 if( sqlite3_stricmp(pExpr->u.zToken, zColl)==0 ) continue;
155177 }
155178
155179 /* No matches cause a break out of the loop */
155180 break;
155181 }
155182 if( i==n ){
155183 nOrderBy = n;
155184 if( (pWInfo->wctrlFlags & WHERE_DISTINCTBY0x0080) ){
155185 eDistinct = 2 + ((pWInfo->wctrlFlags & WHERE_SORTBYGROUP0x0200)!=0);
155186 }else if( pWInfo->wctrlFlags & WHERE_GROUPBY0x0040 ){
155187 eDistinct = 1;
155188 }
155189 }
155190 }
155191
155192 /* Allocate the sqlite3_index_info structure
155193 */
155194 pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
155195 + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
155196 + sizeof(*pIdxOrderBy)*nOrderBy + sizeof(*pHidden)
155197 + sizeof(sqlite3_value*)*nTerm );
155198 if( pIdxInfo==0 ){
155199 sqlite3ErrorMsg(pParse, "out of memory");
155200 return 0;
155201 }
155202 pHidden = (struct HiddenIndexInfo*)&pIdxInfo[1];
155203 pIdxCons = (struct sqlite3_index_constraint*)&pHidden->aRhs[nTerm];
155204 pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
155205 pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
155206 pIdxInfo->aConstraint = pIdxCons;
155207 pIdxInfo->aOrderBy = pIdxOrderBy;
155208 pIdxInfo->aConstraintUsage = pUsage;
155209 pHidden->pWC = pWC;
155210 pHidden->pParse = pParse;
155211 pHidden->eDistinct = eDistinct;
155212 pHidden->mIn = 0;
155213 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
155214 u16 op;
155215 if( (pTerm->wtFlags & TERM_OK0x0040)==0 ) continue;
155216 pIdxCons[j].iColumn = pTerm->u.x.leftColumn;
155217 pIdxCons[j].iTermOffset = i;
155218 op = pTerm->eOperator & WO_ALL0x3fff;
155219 if( op==WO_IN0x0001 ){
155220 if( (pTerm->wtFlags & TERM_SLICE0x8000)==0 ){
155221 pHidden->mIn |= SMASKBIT32(j)((j)<=31?((unsigned int)1)<<(j):0);
155222 }
155223 op = WO_EQ0x0002;
155224 }
155225 if( op==WO_AUX0x0040 ){
155226 pIdxCons[j].op = pTerm->eMatchOp;
155227 }else if( op & (WO_ISNULL0x0100|WO_IS0x0080) ){
155228 if( op==WO_ISNULL0x0100 ){
155229 pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_ISNULL71;
155230 }else{
155231 pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_IS72;
155232 }
155233 }else{
155234 pIdxCons[j].op = (u8)op;
155235 /* The direct assignment in the previous line is possible only because
155236 ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The
155237 ** following asserts verify this fact. */
155238 assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ )((void) (0));
155239 assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT )((void) (0));
155240 assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE )((void) (0));
155241 assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT )((void) (0));
155242 assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE )((void) (0));
155243 assert( pTerm->eOperator&(WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_AUX) )((void) (0));
155244
155245 if( op & (WO_LT(0x0002<<(56 -53))|WO_LE(0x0002<<(55 -53))|WO_GT(0x0002<<(54 -53))|WO_GE(0x0002<<(57 -53)))
155246 && sqlite3ExprIsVector(pTerm->pExpr->pRight)
155247 ){
155248 testcase( j!=i );
155249 if( j<16 ) mNoOmit |= (1 << j);
155250 if( op==WO_LT(0x0002<<(56 -53)) ) pIdxCons[j].op = WO_LE(0x0002<<(55 -53));
155251 if( op==WO_GT(0x0002<<(54 -53)) ) pIdxCons[j].op = WO_GE(0x0002<<(57 -53));
155252 }
155253 }
155254
155255 j++;
155256 }
155257 assert( j==nTerm )((void) (0));
155258 pIdxInfo->nConstraint = j;
155259 for(i=j=0; i<nOrderBy; i++){
155260 Expr *pExpr = pOrderBy->a[i].pExpr;
155261 if( sqlite3ExprIsConstant(pExpr) ) continue;
155262 assert( pExpr->op==TK_COLUMN((void) (0))
155263 || (pExpr->op==TK_COLLATE && pExpr->pLeft->op==TK_COLUMN((void) (0))
155264 && pExpr->iColumn==pExpr->pLeft->iColumn) )((void) (0));
155265 pIdxOrderBy[j].iColumn = pExpr->iColumn;
155266 pIdxOrderBy[j].desc = pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC0x01;
155267 j++;
155268 }
155269 pIdxInfo->nOrderBy = j;
155270
155271 *pmNoOmit = mNoOmit;
155272 return pIdxInfo;
155273}
155274
155275/*
155276** Free an sqlite3_index_info structure allocated by allocateIndexInfo()
155277** and possibly modified by xBestIndex methods.
155278*/
155279static void freeIndexInfo(sqlite3 *db, sqlite3_index_info *pIdxInfo){
155280 HiddenIndexInfo *pHidden;
155281 int i;
155282 assert( pIdxInfo!=0 )((void) (0));
155283 pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
155284 assert( pHidden->pParse!=0 )((void) (0));
155285 assert( pHidden->pParse->db==db )((void) (0));
155286 for(i=0; i<pIdxInfo->nConstraint; i++){
155287 sqlite3ValueFree(pHidden->aRhs[i]); /* IMP: R-14553-25174 */
155288 pHidden->aRhs[i] = 0;
155289 }
155290 sqlite3DbFree(db, pIdxInfo);
155291}
155292
155293/*
155294** The table object reference passed as the second argument to this function
155295** must represent a virtual table. This function invokes the xBestIndex()
155296** method of the virtual table with the sqlite3_index_info object that
155297** comes in as the 3rd argument to this function.
155298**
155299** If an error occurs, pParse is populated with an error message and an
155300** appropriate error code is returned. A return of SQLITE_CONSTRAINT from
155301** xBestIndex is not considered an error. SQLITE_CONSTRAINT indicates that
155302** the current configuration of "unusable" flags in sqlite3_index_info can
155303** not result in a valid plan.
155304**
155305** Whether or not an error is returned, it is the responsibility of the
155306** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
155307** that this is required.
155308*/
155309static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
155310 sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
155311 int rc;
155312
155313 whereTraceIndexInfoInputs(p);
155314 pParse->db->nSchemaLock++;
155315 rc = pVtab->pModule->xBestIndex(pVtab, p);
155316 pParse->db->nSchemaLock--;
155317 whereTraceIndexInfoOutputs(p);
155318
155319 if( rc!=SQLITE_OK0 && rc!=SQLITE_CONSTRAINT19 ){
155320 if( rc==SQLITE_NOMEM7 ){
155321 sqlite3OomFault(pParse->db);
155322 }else if( !pVtab->zErrMsg ){
155323 sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
155324 }else{
155325 sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
155326 }
155327 }
155328 sqlite3_free(pVtab->zErrMsg);
155329 pVtab->zErrMsg = 0;
155330 return rc;
155331}
155332#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
155333
155334#ifdef SQLITE_ENABLE_STAT4
155335/*
155336** Estimate the location of a particular key among all keys in an
155337** index. Store the results in aStat as follows:
155338**
155339** aStat[0] Est. number of rows less than pRec
155340** aStat[1] Est. number of rows equal to pRec
155341**
155342** Return the index of the sample that is the smallest sample that
155343** is greater than or equal to pRec. Note that this index is not an index
155344** into the aSample[] array - it is an index into a virtual set of samples
155345** based on the contents of aSample[] and the number of fields in record
155346** pRec.
155347*/
155348static int whereKeyStats(
155349 Parse *pParse, /* Database connection */
155350 Index *pIdx, /* Index to consider domain of */
155351 UnpackedRecord *pRec, /* Vector of values to consider */
155352 int roundUp, /* Round up if true. Round down if false */
155353 tRowcnt *aStat /* OUT: stats written here */
155354){
155355 IndexSample *aSample = pIdx->aSample;
155356 int iCol; /* Index of required stats in anEq[] etc. */
155357 int i; /* Index of first sample >= pRec */
155358 int iSample; /* Smallest sample larger than or equal to pRec */
155359 int iMin = 0; /* Smallest sample not yet tested */
155360 int iTest; /* Next sample to test */
155361 int res; /* Result of comparison operation */
155362 int nField; /* Number of fields in pRec */
155363 tRowcnt iLower = 0; /* anLt[] + anEq[] of largest sample pRec is > */
155364
155365#ifndef SQLITE_DEBUG
155366 UNUSED_PARAMETER( pParse )(void)(pParse);
155367#endif
155368 assert( pRec!=0 )((void) (0));
155369 assert( pIdx->nSample>0 )((void) (0));
155370 assert( pRec->nField>0 )((void) (0));
155371
155372 /* Do a binary search to find the first sample greater than or equal
155373 ** to pRec. If pRec contains a single field, the set of samples to search
155374 ** is simply the aSample[] array. If the samples in aSample[] contain more
155375 ** than one fields, all fields following the first are ignored.
155376 **
155377 ** If pRec contains N fields, where N is more than one, then as well as the
155378 ** samples in aSample[] (truncated to N fields), the search also has to
155379 ** consider prefixes of those samples. For example, if the set of samples
155380 ** in aSample is:
155381 **
155382 ** aSample[0] = (a, 5)
155383 ** aSample[1] = (a, 10)
155384 ** aSample[2] = (b, 5)
155385 ** aSample[3] = (c, 100)
155386 ** aSample[4] = (c, 105)
155387 **
155388 ** Then the search space should ideally be the samples above and the
155389 ** unique prefixes [a], [b] and [c]. But since that is hard to organize,
155390 ** the code actually searches this set:
155391 **
155392 ** 0: (a)
155393 ** 1: (a, 5)
155394 ** 2: (a, 10)
155395 ** 3: (a, 10)
155396 ** 4: (b)
155397 ** 5: (b, 5)
155398 ** 6: (c)
155399 ** 7: (c, 100)
155400 ** 8: (c, 105)
155401 ** 9: (c, 105)
155402 **
155403 ** For each sample in the aSample[] array, N samples are present in the
155404 ** effective sample array. In the above, samples 0 and 1 are based on
155405 ** sample aSample[0]. Samples 2 and 3 on aSample[1] etc.
155406 **
155407 ** Often, sample i of each block of N effective samples has (i+1) fields.
155408 ** Except, each sample may be extended to ensure that it is greater than or
155409 ** equal to the previous sample in the array. For example, in the above,
155410 ** sample 2 is the first sample of a block of N samples, so at first it
155411 ** appears that it should be 1 field in size. However, that would make it
155412 ** smaller than sample 1, so the binary search would not work. As a result,
155413 ** it is extended to two fields. The duplicates that this creates do not
155414 ** cause any problems.
155415 */
155416 nField = MIN(pRec->nField, pIdx->nSample)((pRec->nField)<(pIdx->nSample)?(pRec->nField):(pIdx
->nSample))
;
155417 iCol = 0;
155418 iSample = pIdx->nSample * nField;
155419 do{
155420 int iSamp; /* Index in aSample[] of test sample */
155421 int n; /* Number of fields in test sample */
155422
155423 iTest = (iMin+iSample)/2;
155424 iSamp = iTest / nField;
155425 if( iSamp>0 ){
155426 /* The proposed effective sample is a prefix of sample aSample[iSamp].
155427 ** Specifically, the shortest prefix of at least (1 + iTest%nField)
155428 ** fields that is greater than the previous effective sample. */
155429 for(n=(iTest % nField) + 1; n<nField; n++){
155430 if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break;
155431 }
155432 }else{
155433 n = iTest + 1;
155434 }
155435
155436 pRec->nField = n;
155437 res = sqlite3VdbeRecordCompare(aSample[iSamp].n, aSample[iSamp].p, pRec);
155438 if( res<0 ){
155439 iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1];
155440 iMin = iTest+1;
155441 }else if( res==0 && n<nField ){
155442 iLower = aSample[iSamp].anLt[n-1];
155443 iMin = iTest+1;
155444 res = -1;
155445 }else{
155446 iSample = iTest;
155447 iCol = n-1;
155448 }
155449 }while( res && iMin<iSample );
155450 i = iSample / nField;
155451
155452#ifdef SQLITE_DEBUG
155453 /* The following assert statements check that the binary search code
155454 ** above found the right answer. This block serves no purpose other
155455 ** than to invoke the asserts. */
155456 if( pParse->db->mallocFailed==0 ){
155457 if( res==0 ){
155458 /* If (res==0) is true, then pRec must be equal to sample i. */
155459 assert( i<pIdx->nSample )((void) (0));
155460 assert( iCol==nField-1 )((void) (0));
155461 pRec->nField = nField;
155462 assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)((void) (0))
155463 || pParse->db->mallocFailed((void) (0))
155464 )((void) (0));
155465 }else{
155466 /* Unless i==pIdx->nSample, indicating that pRec is larger than
155467 ** all samples in the aSample[] array, pRec must be smaller than the
155468 ** (iCol+1) field prefix of sample i. */
155469 assert( i<=pIdx->nSample && i>=0 )((void) (0));
155470 pRec->nField = iCol+1;
155471 assert( i==pIdx->nSample((void) (0))
155472 || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0((void) (0))
155473 || pParse->db->mallocFailed )((void) (0));
155474
155475 /* if i==0 and iCol==0, then record pRec is smaller than all samples
155476 ** in the aSample[] array. Otherwise, if (iCol>0) then pRec must
155477 ** be greater than or equal to the (iCol) field prefix of sample i.
155478 ** If (i>0), then pRec must also be greater than sample (i-1). */
155479 if( iCol>0 ){
155480 pRec->nField = iCol;
155481 assert( sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)<=0((void) (0))
155482 || pParse->db->mallocFailed )((void) (0));
155483 }
155484 if( i>0 ){
155485 pRec->nField = nField;
155486 assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0((void) (0))
155487 || pParse->db->mallocFailed )((void) (0));
155488 }
155489 }
155490 }
155491#endif /* ifdef SQLITE_DEBUG */
155492
155493 if( res==0 ){
155494 /* Record pRec is equal to sample i */
155495 assert( iCol==nField-1 )((void) (0));
155496 aStat[0] = aSample[i].anLt[iCol];
155497 aStat[1] = aSample[i].anEq[iCol];
155498 }else{
155499 /* At this point, the (iCol+1) field prefix of aSample[i] is the first
155500 ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
155501 ** is larger than all samples in the array. */
155502 tRowcnt iUpper, iGap;
155503 if( i>=pIdx->nSample ){
155504 iUpper = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
155505 }else{
155506 iUpper = aSample[i].anLt[iCol];
155507 }
155508
155509 if( iLower>=iUpper ){
155510 iGap = 0;
155511 }else{
155512 iGap = iUpper - iLower;
155513 }
155514 if( roundUp ){
155515 iGap = (iGap*2)/3;
155516 }else{
155517 iGap = iGap/3;
155518 }
155519 aStat[0] = iLower + iGap;
155520 aStat[1] = pIdx->aAvgEq[nField-1];
155521 }
155522
155523 /* Restore the pRec->nField value before returning. */
155524 pRec->nField = nField;
155525 return i;
155526}
155527#endif /* SQLITE_ENABLE_STAT4 */
155528
155529/*
155530** If it is not NULL, pTerm is a term that provides an upper or lower
155531** bound on a range scan. Without considering pTerm, it is estimated
155532** that the scan will visit nNew rows. This function returns the number
155533** estimated to be visited after taking pTerm into account.
155534**
155535** If the user explicitly specified a likelihood() value for this term,
155536** then the return value is the likelihood multiplied by the number of
155537** input rows. Otherwise, this function assumes that an "IS NOT NULL" term
155538** has a likelihood of 0.50, and any other term a likelihood of 0.25.
155539*/
155540static LogEst whereRangeAdjust(WhereTerm *pTerm, LogEst nNew){
155541 LogEst nRet = nNew;
155542 if( pTerm ){
155543 if( pTerm->truthProb<=0 ){
155544 nRet += pTerm->truthProb;
155545 }else if( (pTerm->wtFlags & TERM_VNULL0x0080)==0 ){
155546 nRet -= 20; assert( 20==sqlite3LogEst(4) )((void) (0));
155547 }
155548 }
155549 return nRet;
155550}
155551
155552
155553#ifdef SQLITE_ENABLE_STAT4
155554/*
155555** Return the affinity for a single column of an index.
155556*/
155557SQLITE_PRIVATEstatic char sqlite3IndexColumnAffinity(sqlite3 *db, Index *pIdx, int iCol){
155558 assert( iCol>=0 && iCol<pIdx->nColumn )((void) (0));
155559 if( !pIdx->zColAff ){
155560 if( sqlite3IndexAffinityStr(db, pIdx)==0 ) return SQLITE_AFF_BLOB0x41;
155561 }
155562 assert( pIdx->zColAff[iCol]!=0 )((void) (0));
155563 return pIdx->zColAff[iCol];
155564}
155565#endif
155566
155567
155568#ifdef SQLITE_ENABLE_STAT4
155569/*
155570** This function is called to estimate the number of rows visited by a
155571** range-scan on a skip-scan index. For example:
155572**
155573** CREATE INDEX i1 ON t1(a, b, c);
155574** SELECT * FROM t1 WHERE a=? AND c BETWEEN ? AND ?;
155575**
155576** Value pLoop->nOut is currently set to the estimated number of rows
155577** visited for scanning (a=? AND b=?). This function reduces that estimate
155578** by some factor to account for the (c BETWEEN ? AND ?) expression based
155579** on the stat4 data for the index. this scan will be peformed multiple
155580** times (once for each (a,b) combination that matches a=?) is dealt with
155581** by the caller.
155582**
155583** It does this by scanning through all stat4 samples, comparing values
155584** extracted from pLower and pUpper with the corresponding column in each
155585** sample. If L and U are the number of samples found to be less than or
155586** equal to the values extracted from pLower and pUpper respectively, and
155587** N is the total number of samples, the pLoop->nOut value is adjusted
155588** as follows:
155589**
155590** nOut = nOut * ( min(U - L, 1) / N )
155591**
155592** If pLower is NULL, or a value cannot be extracted from the term, L is
155593** set to zero. If pUpper is NULL, or a value cannot be extracted from it,
155594** U is set to N.
155595**
155596** Normally, this function sets *pbDone to 1 before returning. However,
155597** if no value can be extracted from either pLower or pUpper (and so the
155598** estimate of the number of rows delivered remains unchanged), *pbDone
155599** is left as is.
155600**
155601** If an error occurs, an SQLite error code is returned. Otherwise,
155602** SQLITE_OK.
155603*/
155604static int whereRangeSkipScanEst(
155605 Parse *pParse, /* Parsing & code generating context */
155606 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
155607 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
155608 WhereLoop *pLoop, /* Update the .nOut value of this loop */
155609 int *pbDone /* Set to true if at least one expr. value extracted */
155610){
155611 Index *p = pLoop->u.btree.pIndex;
155612 int nEq = pLoop->u.btree.nEq;
155613 sqlite3 *db = pParse->db;
155614 int nLower = -1;
155615 int nUpper = p->nSample+1;
155616 int rc = SQLITE_OK0;
155617 u8 aff = sqlite3IndexColumnAffinity(db, p, nEq);
155618 CollSeq *pColl;
155619
155620 sqlite3_value *p1 = 0; /* Value extracted from pLower */
155621 sqlite3_value *p2 = 0; /* Value extracted from pUpper */
155622 sqlite3_value *pVal = 0; /* Value extracted from record */
155623
155624 pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
155625 if( pLower ){
155626 rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
155627 nLower = 0;
155628 }
155629 if( pUpper && rc==SQLITE_OK0 ){
155630 rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
155631 nUpper = p2 ? 0 : p->nSample;
155632 }
155633
155634 if( p1 || p2 ){
155635 int i;
155636 int nDiff;
155637 for(i=0; rc==SQLITE_OK0 && i<p->nSample; i++){
155638 rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
155639 if( rc==SQLITE_OK0 && p1 ){
155640 int res = sqlite3MemCompare(p1, pVal, pColl);
155641 if( res>=0 ) nLower++;
155642 }
155643 if( rc==SQLITE_OK0 && p2 ){
155644 int res = sqlite3MemCompare(p2, pVal, pColl);
155645 if( res>=0 ) nUpper++;
155646 }
155647 }
155648 nDiff = (nUpper - nLower);
155649 if( nDiff<=0 ) nDiff = 1;
155650
155651 /* If there is both an upper and lower bound specified, and the
155652 ** comparisons indicate that they are close together, use the fallback
155653 ** method (assume that the scan visits 1/64 of the rows) for estimating
155654 ** the number of rows visited. Otherwise, estimate the number of rows
155655 ** using the method described in the header comment for this function. */
155656 if( nDiff!=1 || pUpper==0 || pLower==0 ){
155657 int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
155658 pLoop->nOut -= nAdjust;
155659 *pbDone = 1;
155660 WHERETRACE(0x10, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
155661 nLower, nUpper, nAdjust*-1, pLoop->nOut));
155662 }
155663
155664 }else{
155665 assert( *pbDone==0 )((void) (0));
155666 }
155667
155668 sqlite3ValueFree(p1);
155669 sqlite3ValueFree(p2);
155670 sqlite3ValueFree(pVal);
155671
155672 return rc;
155673}
155674#endif /* SQLITE_ENABLE_STAT4 */
155675
155676/*
155677** This function is used to estimate the number of rows that will be visited
155678** by scanning an index for a range of values. The range may have an upper
155679** bound, a lower bound, or both. The WHERE clause terms that set the upper
155680** and lower bounds are represented by pLower and pUpper respectively. For
155681** example, assuming that index p is on t1(a):
155682**
155683** ... FROM t1 WHERE a > ? AND a < ? ...
155684** |_____| |_____|
155685** | |
155686** pLower pUpper
155687**
155688** If either of the upper or lower bound is not present, then NULL is passed in
155689** place of the corresponding WhereTerm.
155690**
155691** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index
155692** column subject to the range constraint. Or, equivalently, the number of
155693** equality constraints optimized by the proposed index scan. For example,
155694** assuming index p is on t1(a, b), and the SQL query is:
155695**
155696** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
155697**
155698** then nEq is set to 1 (as the range restricted column, b, is the second
155699** left-most column of the index). Or, if the query is:
155700**
155701** ... FROM t1 WHERE a > ? AND a < ? ...
155702**
155703** then nEq is set to 0.
155704**
155705** When this function is called, *pnOut is set to the sqlite3LogEst() of the
155706** number of rows that the index scan is expected to visit without
155707** considering the range constraints. If nEq is 0, then *pnOut is the number of
155708** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
155709** to account for the range constraints pLower and pUpper.
155710**
155711** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
155712** used, a single range inequality reduces the search space by a factor of 4.
155713** and a pair of constraints (x>? AND x<?) reduces the expected number of
155714** rows visited by a factor of 64.
155715*/
155716static int whereRangeScanEst(
155717 Parse *pParse, /* Parsing & code generating context */
155718 WhereLoopBuilder *pBuilder,
155719 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
155720 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
155721 WhereLoop *pLoop /* Modify the .nOut and maybe .rRun fields */
155722){
155723 int rc = SQLITE_OK0;
155724 int nOut = pLoop->nOut;
155725 LogEst nNew;
155726
155727#ifdef SQLITE_ENABLE_STAT4
155728 Index *p = pLoop->u.btree.pIndex;
155729 int nEq = pLoop->u.btree.nEq;
155730
155731 if( p->nSample>0 && ALWAYS(nEq<p->nSampleCol)(nEq<p->nSampleCol)
155732 && OptimizationEnabled(pParse->db, SQLITE_Stat4)(((pParse->db)->dbOptFlags&(0x00000800))==0)
155733 ){
155734 if( nEq==pBuilder->nRecValid ){
155735 UnpackedRecord *pRec = pBuilder->pRec;
155736 tRowcnt a[2];
155737 int nBtm = pLoop->u.btree.nBtm;
155738 int nTop = pLoop->u.btree.nTop;
155739
155740 /* Variable iLower will be set to the estimate of the number of rows in
155741 ** the index that are less than the lower bound of the range query. The
155742 ** lower bound being the concatenation of $P and $L, where $P is the
155743 ** key-prefix formed by the nEq values matched against the nEq left-most
155744 ** columns of the index, and $L is the value in pLower.
155745 **
155746 ** Or, if pLower is NULL or $L cannot be extracted from it (because it
155747 ** is not a simple variable or literal value), the lower bound of the
155748 ** range is $P. Due to a quirk in the way whereKeyStats() works, even
155749 ** if $L is available, whereKeyStats() is called for both ($P) and
155750 ** ($P:$L) and the larger of the two returned values is used.
155751 **
155752 ** Similarly, iUpper is to be set to the estimate of the number of rows
155753 ** less than the upper bound of the range query. Where the upper bound
155754 ** is either ($P) or ($P:$U). Again, even if $U is available, both values
155755 ** of iUpper are requested of whereKeyStats() and the smaller used.
155756 **
155757 ** The number of rows between the two bounds is then just iUpper-iLower.
155758 */
155759 tRowcnt iLower; /* Rows less than the lower bound */
155760 tRowcnt iUpper; /* Rows less than the upper bound */
155761 int iLwrIdx = -2; /* aSample[] for the lower bound */
155762 int iUprIdx = -1; /* aSample[] for the upper bound */
155763
155764 if( pRec ){
155765 testcase( pRec->nField!=pBuilder->nRecValid );
155766 pRec->nField = pBuilder->nRecValid;
155767 }
155768 /* Determine iLower and iUpper using ($P) only. */
155769 if( nEq==0 ){
155770 iLower = 0;
155771 iUpper = p->nRowEst0;
155772 }else{
155773 /* Note: this call could be optimized away - since the same values must
155774 ** have been requested when testing key $P in whereEqualScanEst(). */
155775 whereKeyStats(pParse, p, pRec, 0, a);
155776 iLower = a[0];
155777 iUpper = a[0] + a[1];
155778 }
155779
155780 assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 )((void) (0));
155781 assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 )((void) (0));
155782 assert( p->aSortOrder!=0 )((void) (0));
155783 if( p->aSortOrder[nEq] ){
155784 /* The roles of pLower and pUpper are swapped for a DESC index */
155785 SWAP(WhereTerm*, pLower, pUpper){WhereTerm* t=pLower; pLower=pUpper; pUpper=t;};
155786 SWAP(int, nBtm, nTop){int t=nBtm; nBtm=nTop; nTop=t;};
155787 }
155788
155789 /* If possible, improve on the iLower estimate using ($P:$L). */
155790 if( pLower ){
155791 int n; /* Values extracted from pExpr */
155792 Expr *pExpr = pLower->pExpr->pRight;
155793 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nBtm, nEq, &n);
155794 if( rc==SQLITE_OK0 && n ){
155795 tRowcnt iNew;
155796 u16 mask = WO_GT(0x0002<<(54 -53))|WO_LE(0x0002<<(55 -53));
155797 if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE(0x0002<<(55 -53))|WO_LT(0x0002<<(56 -53)));
155798 iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a);
155799 iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0);
155800 if( iNew>iLower ) iLower = iNew;
155801 nOut--;
155802 pLower = 0;
155803 }
155804 }
155805
155806 /* If possible, improve on the iUpper estimate using ($P:$U). */
155807 if( pUpper ){
155808 int n; /* Values extracted from pExpr */
155809 Expr *pExpr = pUpper->pExpr->pRight;
155810 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nTop, nEq, &n);
155811 if( rc==SQLITE_OK0 && n ){
155812 tRowcnt iNew;
155813 u16 mask = WO_GT(0x0002<<(54 -53))|WO_LE(0x0002<<(55 -53));
155814 if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE(0x0002<<(55 -53))|WO_LT(0x0002<<(56 -53)));
155815 iUprIdx = whereKeyStats(pParse, p, pRec, 1, a);
155816 iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0);
155817 if( iNew<iUpper ) iUpper = iNew;
155818 nOut--;
155819 pUpper = 0;
155820 }
155821 }
155822
155823 pBuilder->pRec = pRec;
155824 if( rc==SQLITE_OK0 ){
155825 if( iUpper>iLower ){
155826 nNew = sqlite3LogEst(iUpper - iLower);
155827 /* TUNING: If both iUpper and iLower are derived from the same
155828 ** sample, then assume they are 4x more selective. This brings
155829 ** the estimated selectivity more in line with what it would be
155830 ** if estimated without the use of STAT4 tables. */
155831 if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) )((void) (0));
155832 }else{
155833 nNew = 10; assert( 10==sqlite3LogEst(2) )((void) (0));
155834 }
155835 if( nNew<nOut ){
155836 nOut = nNew;
155837 }
155838 WHERETRACE(0x10, ("STAT4 range scan: %u..%u est=%d\n",
155839 (u32)iLower, (u32)iUpper, nOut));
155840 }
155841 }else{
155842 int bDone = 0;
155843 rc = whereRangeSkipScanEst(pParse, pLower, pUpper, pLoop, &bDone);
155844 if( bDone ) return rc;
155845 }
155846 }
155847#else
155848 UNUSED_PARAMETER(pParse)(void)(pParse);
155849 UNUSED_PARAMETER(pBuilder)(void)(pBuilder);
155850 assert( pLower || pUpper )((void) (0));
155851#endif
155852 assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 )((void) (0));
155853 nNew = whereRangeAdjust(pLower, nOut);
155854 nNew = whereRangeAdjust(pUpper, nNew);
155855
155856 /* TUNING: If there is both an upper and lower limit and neither limit
155857 ** has an application-defined likelihood(), assume the range is
155858 ** reduced by an additional 75%. This means that, by default, an open-ended
155859 ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the
155860 ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to
155861 ** match 1/64 of the index. */
155862 if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){
155863 nNew -= 20;
155864 }
155865
155866 nOut -= (pLower!=0) + (pUpper!=0);
155867 if( nNew<10 ) nNew = 10;
155868 if( nNew<nOut ) nOut = nNew;
155869#if defined(WHERETRACE_ENABLED)
155870 if( pLoop->nOut>nOut ){
155871 WHERETRACE(0x10,("Range scan lowers nOut from %d to %d\n",
155872 pLoop->nOut, nOut));
155873 }
155874#endif
155875 pLoop->nOut = (LogEst)nOut;
155876 return rc;
155877}
155878
155879#ifdef SQLITE_ENABLE_STAT4
155880/*
155881** Estimate the number of rows that will be returned based on
155882** an equality constraint x=VALUE and where that VALUE occurs in
155883** the histogram data. This only works when x is the left-most
155884** column of an index and sqlite_stat4 histogram data is available
155885** for that index. When pExpr==NULL that means the constraint is
155886** "x IS NULL" instead of "x=VALUE".
155887**
155888** Write the estimated row count into *pnRow and return SQLITE_OK.
155889** If unable to make an estimate, leave *pnRow unchanged and return
155890** non-zero.
155891**
155892** This routine can fail if it is unable to load a collating sequence
155893** required for string comparison, or if unable to allocate memory
155894** for a UTF conversion required for comparison. The error is stored
155895** in the pParse structure.
155896*/
155897static int whereEqualScanEst(
155898 Parse *pParse, /* Parsing & code generating context */
155899 WhereLoopBuilder *pBuilder,
155900 Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
155901 tRowcnt *pnRow /* Write the revised row estimate here */
155902){
155903 Index *p = pBuilder->pNew->u.btree.pIndex;
155904 int nEq = pBuilder->pNew->u.btree.nEq;
155905 UnpackedRecord *pRec = pBuilder->pRec;
155906 int rc; /* Subfunction return code */
155907 tRowcnt a[2]; /* Statistics */
155908 int bOk;
155909
155910 assert( nEq>=1 )((void) (0));
155911 assert( nEq<=p->nColumn )((void) (0));
155912 assert( p->aSample!=0 )((void) (0));
155913 assert( p->nSample>0 )((void) (0));
155914 assert( pBuilder->nRecValid<nEq )((void) (0));
155915
155916 /* If values are not available for all fields of the index to the left
155917 ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */
155918 if( pBuilder->nRecValid<(nEq-1) ){
155919 return SQLITE_NOTFOUND12;
155920 }
155921
155922 /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue()
155923 ** below would return the same value. */
155924 if( nEq>=p->nColumn ){
155925 *pnRow = 1;
155926 return SQLITE_OK0;
155927 }
155928
155929 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, 1, nEq-1, &bOk);
155930 pBuilder->pRec = pRec;
155931 if( rc!=SQLITE_OK0 ) return rc;
155932 if( bOk==0 ) return SQLITE_NOTFOUND12;
155933 pBuilder->nRecValid = nEq;
155934
155935 whereKeyStats(pParse, p, pRec, 0, a);
155936 WHERETRACE(0x10,("equality scan regions %s(%d): %d\n",
155937 p->zName, nEq-1, (int)a[1]));
155938 *pnRow = a[1];
155939
155940 return rc;
155941}
155942#endif /* SQLITE_ENABLE_STAT4 */
155943
155944#ifdef SQLITE_ENABLE_STAT4
155945/*
155946** Estimate the number of rows that will be returned based on
155947** an IN constraint where the right-hand side of the IN operator
155948** is a list of values. Example:
155949**
155950** WHERE x IN (1,2,3,4)
155951**
155952** Write the estimated row count into *pnRow and return SQLITE_OK.
155953** If unable to make an estimate, leave *pnRow unchanged and return
155954** non-zero.
155955**
155956** This routine can fail if it is unable to load a collating sequence
155957** required for string comparison, or if unable to allocate memory
155958** for a UTF conversion required for comparison. The error is stored
155959** in the pParse structure.
155960*/
155961static int whereInScanEst(
155962 Parse *pParse, /* Parsing & code generating context */
155963 WhereLoopBuilder *pBuilder,
155964 ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
155965 tRowcnt *pnRow /* Write the revised row estimate here */
155966){
155967 Index *p = pBuilder->pNew->u.btree.pIndex;
155968 i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
155969 int nRecValid = pBuilder->nRecValid;
155970 int rc = SQLITE_OK0; /* Subfunction return code */
155971 tRowcnt nEst; /* Number of rows for a single term */
155972 tRowcnt nRowEst = 0; /* New estimate of the number of rows */
155973 int i; /* Loop counter */
155974
155975 assert( p->aSample!=0 )((void) (0));
155976 for(i=0; rc==SQLITE_OK0 && i<pList->nExpr; i++){
155977 nEst = nRow0;
155978 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
155979 nRowEst += nEst;
155980 pBuilder->nRecValid = nRecValid;
155981 }
155982
155983 if( rc==SQLITE_OK0 ){
155984 if( nRowEst > nRow0 ) nRowEst = nRow0;
155985 *pnRow = nRowEst;
155986 WHERETRACE(0x10,("IN row estimate: est=%d\n", nRowEst));
155987 }
155988 assert( pBuilder->nRecValid==nRecValid )((void) (0));
155989 return rc;
155990}
155991#endif /* SQLITE_ENABLE_STAT4 */
155992
155993
155994#ifdef WHERETRACE_ENABLED
155995/*
155996** Print the content of a WhereTerm object
155997*/
155998SQLITE_PRIVATEstatic void sqlite3WhereTermPrint(WhereTerm *pTerm, int iTerm){
155999 if( pTerm==0 ){
156000 sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm);
156001 }else{
156002 char zType[8];
156003 char zLeft[50];
156004 memcpy(zType, "....", 5);
156005 if( pTerm->wtFlags & TERM_VIRTUAL0x0002 ) zType[0] = 'V';
156006 if( pTerm->eOperator & WO_EQUIV0x0800 ) zType[1] = 'E';
156007 if( ExprHasProperty(pTerm->pExpr, EP_OuterON)(((pTerm->pExpr)->flags&(0x000001))!=0) ) zType[2] = 'L';
156008 if( pTerm->wtFlags & TERM_CODED0x0004 ) zType[3] = 'C';
156009 if( pTerm->eOperator & WO_SINGLE0x01ff ){
156010 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
156011 sqlite3_snprintf(sizeof(zLeft),zLeft,"left={%d:%d}",
156012 pTerm->leftCursor, pTerm->u.x.leftColumn);
156013 }else if( (pTerm->eOperator & WO_OR0x0200)!=0 && pTerm->u.pOrInfo!=0 ){
156014 sqlite3_snprintf(sizeof(zLeft),zLeft,"indexable=0x%llx",
156015 pTerm->u.pOrInfo->indexable);
156016 }else{
156017 sqlite3_snprintf(sizeof(zLeft),zLeft,"left=%d", pTerm->leftCursor);
156018 }
156019 sqlite3DebugPrintf(
156020 "TERM-%-3d %p %s %-12s op=%03x wtFlags=%04x",
156021 iTerm, pTerm, zType, zLeft, pTerm->eOperator, pTerm->wtFlags);
156022 /* The 0x10000 .wheretrace flag causes extra information to be
156023 ** shown about each Term */
156024 if( sqlite3WhereTrace & 0x10000 ){
156025 sqlite3DebugPrintf(" prob=%-3d prereq=%llx,%llx",
156026 pTerm->truthProb, (u64)pTerm->prereqAll, (u64)pTerm->prereqRight);
156027 }
156028 if( (pTerm->eOperator & (WO_OR0x0200|WO_AND0x0400))==0 && pTerm->u.x.iField ){
156029 sqlite3DebugPrintf(" iField=%d", pTerm->u.x.iField);
156030 }
156031 if( pTerm->iParent>=0 ){
156032 sqlite3DebugPrintf(" iParent=%d", pTerm->iParent);
156033 }
156034 sqlite3DebugPrintf("\n");
156035 sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
156036 }
156037}
156038#endif
156039
156040#ifdef WHERETRACE_ENABLED
156041/*
156042** Show the complete content of a WhereClause
156043*/
156044SQLITE_PRIVATEstatic void sqlite3WhereClausePrint(WhereClause *pWC){
156045 int i;
156046 for(i=0; i<pWC->nTerm; i++){
156047 sqlite3WhereTermPrint(&pWC->a[i], i);
156048 }
156049}
156050#endif
156051
156052#ifdef WHERETRACE_ENABLED
156053/*
156054** Print a WhereLoop object for debugging purposes
156055*/
156056SQLITE_PRIVATEstatic void sqlite3WhereLoopPrint(WhereLoop *p, WhereClause *pWC){
156057 WhereInfo *pWInfo = pWC->pWInfo;
156058 int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
156059 SrcItem *pItem = pWInfo->pTabList->a + p->iTab;
156060 Table *pTab = pItem->pTab;
156061 Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
156062 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
156063 p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
156064 sqlite3DebugPrintf(" %12s",
156065 pItem->zAlias ? pItem->zAlias : pTab->zName);
156066 if( (p->wsFlags & WHERE_VIRTUALTABLE0x00000400)==0 ){
156067 const char *zName;
156068 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
156069 if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){
156070 int i = sqlite3Strlen30(zName) - 1;
156071 while( zName[i]!='_' ) i--;
156072 zName += i;
156073 }
156074 sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
156075 }else{
156076 sqlite3DebugPrintf("%20s","");
156077 }
156078 }else{
156079 char *z;
156080 if( p->u.vtab.idxStr ){
156081 z = sqlite3_mprintf("(%d,\"%s\",%#x)",
156082 p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
156083 }else{
156084 z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
156085 }
156086 sqlite3DebugPrintf(" %-19s", z);
156087 sqlite3_free(z);
156088 }
156089 if( p->wsFlags & WHERE_SKIPSCAN0x00008000 ){
156090 sqlite3DebugPrintf(" f %06x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
156091 }else{
156092 sqlite3DebugPrintf(" f %06x N %d", p->wsFlags, p->nLTerm);
156093 }
156094 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
156095 if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){
156096 int i;
156097 for(i=0; i<p->nLTerm; i++){
156098 sqlite3WhereTermPrint(p->aLTerm[i], i);
156099 }
156100 }
156101}
156102#endif
156103
156104/*
156105** Convert bulk memory into a valid WhereLoop that can be passed
156106** to whereLoopClear harmlessly.
156107*/
156108static void whereLoopInit(WhereLoop *p){
156109 p->aLTerm = p->aLTermSpace;
156110 p->nLTerm = 0;
156111 p->nLSlot = ArraySize(p->aLTermSpace)((int)(sizeof(p->aLTermSpace)/sizeof(p->aLTermSpace[0])
))
;
156112 p->wsFlags = 0;
156113}
156114
156115/*
156116** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact.
156117*/
156118static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){
156119 if( p->wsFlags & (WHERE_VIRTUALTABLE0x00000400|WHERE_AUTO_INDEX0x00004000) ){
156120 if( (p->wsFlags & WHERE_VIRTUALTABLE0x00000400)!=0 && p->u.vtab.needFree ){
156121 sqlite3_free(p->u.vtab.idxStr);
156122 p->u.vtab.needFree = 0;
156123 p->u.vtab.idxStr = 0;
156124 }else if( (p->wsFlags & WHERE_AUTO_INDEX0x00004000)!=0 && p->u.btree.pIndex!=0 ){
156125 sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
156126 sqlite3DbFreeNN(db, p->u.btree.pIndex);
156127 p->u.btree.pIndex = 0;
156128 }
156129 }
156130}
156131
156132/*
156133** Deallocate internal memory used by a WhereLoop object
156134*/
156135static void whereLoopClear(sqlite3 *db, WhereLoop *p){
156136 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
156137 whereLoopClearUnion(db, p);
156138 whereLoopInit(p);
156139}
156140
156141/*
156142** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
156143*/
156144static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){
156145 WhereTerm **paNew;
156146 if( p->nLSlot>=n ) return SQLITE_OK0;
156147 n = (n+7)&~7;
156148 paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n);
156149 if( paNew==0 ) return SQLITE_NOMEM_BKPT7;
156150 memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
156151 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
156152 p->aLTerm = paNew;
156153 p->nLSlot = n;
156154 return SQLITE_OK0;
156155}
156156
156157/*
156158** Transfer content from the second pLoop into the first.
156159*/
156160static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){
156161 whereLoopClearUnion(db, pTo);
156162 if( whereLoopResize(db, pTo, pFrom->nLTerm) ){
156163 memset(pTo, 0, WHERE_LOOP_XFER_SZ__builtin_offsetof(WhereLoop, nLSlot));
156164 return SQLITE_NOMEM_BKPT7;
156165 }
156166 memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ__builtin_offsetof(WhereLoop, nLSlot));
156167 memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
156168 if( pFrom->wsFlags & WHERE_VIRTUALTABLE0x00000400 ){
156169 pFrom->u.vtab.needFree = 0;
156170 }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX0x00004000)!=0 ){
156171 pFrom->u.btree.pIndex = 0;
156172 }
156173 return SQLITE_OK0;
156174}
156175
156176/*
156177** Delete a WhereLoop object
156178*/
156179static void whereLoopDelete(sqlite3 *db, WhereLoop *p){
156180 whereLoopClear(db, p);
156181 sqlite3DbFreeNN(db, p);
156182}
156183
156184/*
156185** Free a WhereInfo structure
156186*/
156187static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
156188 assert( pWInfo!=0 )((void) (0));
156189 sqlite3WhereClauseClear(&pWInfo->sWC);
156190 while( pWInfo->pLoops ){
156191 WhereLoop *p = pWInfo->pLoops;
156192 pWInfo->pLoops = p->pNextLoop;
156193 whereLoopDelete(db, p);
156194 }
156195 assert( pWInfo->pExprMods==0 )((void) (0));
156196 while( pWInfo->pMemToFree ){
156197 WhereMemBlock *pNext = pWInfo->pMemToFree->pNext;
156198 sqlite3DbFreeNN(db, pWInfo->pMemToFree);
156199 pWInfo->pMemToFree = pNext;
156200 }
156201 sqlite3DbFreeNN(db, pWInfo);
156202}
156203
156204/* Undo all Expr node modifications
156205*/
156206static void whereUndoExprMods(WhereInfo *pWInfo){
156207 while( pWInfo->pExprMods ){
156208 WhereExprMod *p = pWInfo->pExprMods;
156209 pWInfo->pExprMods = p->pNext;
156210 memcpy(p->pExpr, &p->orig, sizeof(p->orig));
156211 sqlite3DbFree(pWInfo->pParse->db, p);
156212 }
156213}
156214
156215/*
156216** Return TRUE if all of the following are true:
156217**
156218** (1) X has the same or lower cost, or returns the same or fewer rows,
156219** than Y.
156220** (2) X uses fewer WHERE clause terms than Y
156221** (3) Every WHERE clause term used by X is also used by Y
156222** (4) X skips at least as many columns as Y
156223** (5) If X is a covering index, than Y is too
156224**
156225** Conditions (2) and (3) mean that X is a "proper subset" of Y.
156226** If X is a proper subset of Y then Y is a better choice and ought
156227** to have a lower cost. This routine returns TRUE when that cost
156228** relationship is inverted and needs to be adjusted. Constraint (4)
156229** was added because if X uses skip-scan less than Y it still might
156230** deserve a lower cost even if it is a proper subset of Y. Constraint (5)
156231** was added because a covering index probably deserves to have a lower cost
156232** than a non-covering index even if it is a proper subset.
156233*/
156234static int whereLoopCheaperProperSubset(
156235 const WhereLoop *pX, /* First WhereLoop to compare */
156236 const WhereLoop *pY /* Compare against this WhereLoop */
156237){
156238 int i, j;
156239 if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
156240 return 0; /* X is not a subset of Y */
156241 }
156242 if( pX->rRun>pY->rRun && pX->nOut>pY->nOut ) return 0;
156243 if( pY->nSkip > pX->nSkip ) return 0;
156244 for(i=pX->nLTerm-1; i>=0; i--){
156245 if( pX->aLTerm[i]==0 ) continue;
156246 for(j=pY->nLTerm-1; j>=0; j--){
156247 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
156248 }
156249 if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */
156250 }
156251 if( (pX->wsFlags&WHERE_IDX_ONLY0x00000040)!=0
156252 && (pY->wsFlags&WHERE_IDX_ONLY0x00000040)==0 ){
156253 return 0; /* Constraint (5) */
156254 }
156255 return 1; /* All conditions meet */
156256}
156257
156258/*
156259** Try to adjust the cost and number of output rows of WhereLoop pTemplate
156260** upwards or downwards so that:
156261**
156262** (1) pTemplate costs less than any other WhereLoops that are a proper
156263** subset of pTemplate
156264**
156265** (2) pTemplate costs more than any other WhereLoops for which pTemplate
156266** is a proper subset.
156267**
156268** To say "WhereLoop X is a proper subset of Y" means that X uses fewer
156269** WHERE clause terms than Y and that every WHERE clause term used by X is
156270** also used by Y.
156271*/
156272static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){
156273 if( (pTemplate->wsFlags & WHERE_INDEXED0x00000200)==0 ) return;
156274 for(; p; p=p->pNextLoop){
156275 if( p->iTab!=pTemplate->iTab ) continue;
156276 if( (p->wsFlags & WHERE_INDEXED0x00000200)==0 ) continue;
156277 if( whereLoopCheaperProperSubset(p, pTemplate) ){
156278 /* Adjust pTemplate cost downward so that it is cheaper than its
156279 ** subset p. */
156280 WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
156281 pTemplate->rRun, pTemplate->nOut,
156282 MIN(p->rRun, pTemplate->rRun),
156283 MIN(p->nOut - 1, pTemplate->nOut)));
156284 pTemplate->rRun = MIN(p->rRun, pTemplate->rRun)((p->rRun)<(pTemplate->rRun)?(p->rRun):(pTemplate
->rRun))
;
156285 pTemplate->nOut = MIN(p->nOut - 1, pTemplate->nOut)((p->nOut - 1)<(pTemplate->nOut)?(p->nOut - 1):(pTemplate
->nOut))
;
156286 }else if( whereLoopCheaperProperSubset(pTemplate, p) ){
156287 /* Adjust pTemplate cost upward so that it is costlier than p since
156288 ** pTemplate is a proper subset of p */
156289 WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
156290 pTemplate->rRun, pTemplate->nOut,
156291 MAX(p->rRun, pTemplate->rRun),
156292 MAX(p->nOut + 1, pTemplate->nOut)));
156293 pTemplate->rRun = MAX(p->rRun, pTemplate->rRun)((p->rRun)>(pTemplate->rRun)?(p->rRun):(pTemplate
->rRun))
;
156294 pTemplate->nOut = MAX(p->nOut + 1, pTemplate->nOut)((p->nOut + 1)>(pTemplate->nOut)?(p->nOut + 1):(pTemplate
->nOut))
;
156295 }
156296 }
156297}
156298
156299/*
156300** Search the list of WhereLoops in *ppPrev looking for one that can be
156301** replaced by pTemplate.
156302**
156303** Return NULL if pTemplate does not belong on the WhereLoop list.
156304** In other words if pTemplate ought to be dropped from further consideration.
156305**
156306** If pX is a WhereLoop that pTemplate can replace, then return the
156307** link that points to pX.
156308**
156309** If pTemplate cannot replace any existing element of the list but needs
156310** to be added to the list as a new entry, then return a pointer to the
156311** tail of the list.
156312*/
156313static WhereLoop **whereLoopFindLesser(
156314 WhereLoop **ppPrev,
156315 const WhereLoop *pTemplate
156316){
156317 WhereLoop *p;
156318 for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
156319 if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
156320 /* If either the iTab or iSortIdx values for two WhereLoop are different
156321 ** then those WhereLoops need to be considered separately. Neither is
156322 ** a candidate to replace the other. */
156323 continue;
156324 }
156325 /* In the current implementation, the rSetup value is either zero
156326 ** or the cost of building an automatic index (NlogN) and the NlogN
156327 ** is the same for compatible WhereLoops. */
156328 assert( p->rSetup==0 || pTemplate->rSetup==0((void) (0))
156329 || p->rSetup==pTemplate->rSetup )((void) (0));
156330
156331 /* whereLoopAddBtree() always generates and inserts the automatic index
156332 ** case first. Hence compatible candidate WhereLoops never have a larger
156333 ** rSetup. Call this SETUP-INVARIANT */
156334 assert( p->rSetup>=pTemplate->rSetup )((void) (0));
156335
156336 /* Any loop using an appliation-defined index (or PRIMARY KEY or
156337 ** UNIQUE constraint) with one or more == constraints is better
156338 ** than an automatic index. Unless it is a skip-scan. */
156339 if( (p->wsFlags & WHERE_AUTO_INDEX0x00004000)!=0
156340 && (pTemplate->nSkip)==0
156341 && (pTemplate->wsFlags & WHERE_INDEXED0x00000200)!=0
156342 && (pTemplate->wsFlags & WHERE_COLUMN_EQ0x00000001)!=0
156343 && (p->prereq & pTemplate->prereq)==pTemplate->prereq
156344 ){
156345 break;
156346 }
156347
156348 /* If existing WhereLoop p is better than pTemplate, pTemplate can be
156349 ** discarded. WhereLoop p is better if:
156350 ** (1) p has no more dependencies than pTemplate, and
156351 ** (2) p has an equal or lower cost than pTemplate
156352 */
156353 if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */
156354 && p->rSetup<=pTemplate->rSetup /* (2a) */
156355 && p->rRun<=pTemplate->rRun /* (2b) */
156356 && p->nOut<=pTemplate->nOut /* (2c) */
156357 ){
156358 return 0; /* Discard pTemplate */
156359 }
156360
156361 /* If pTemplate is always better than p, then cause p to be overwritten
156362 ** with pTemplate. pTemplate is better than p if:
156363 ** (1) pTemplate has no more dependences than p, and
156364 ** (2) pTemplate has an equal or lower cost than p.
156365 */
156366 if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */
156367 && p->rRun>=pTemplate->rRun /* (2a) */
156368 && p->nOut>=pTemplate->nOut /* (2b) */
156369 ){
156370 assert( p->rSetup>=pTemplate->rSetup )((void) (0)); /* SETUP-INVARIANT above */
156371 break; /* Cause p to be overwritten by pTemplate */
156372 }
156373 }
156374 return ppPrev;
156375}
156376
156377/*
156378** Insert or replace a WhereLoop entry using the template supplied.
156379**
156380** An existing WhereLoop entry might be overwritten if the new template
156381** is better and has fewer dependencies. Or the template will be ignored
156382** and no insert will occur if an existing WhereLoop is faster and has
156383** fewer dependencies than the template. Otherwise a new WhereLoop is
156384** added based on the template.
156385**
156386** If pBuilder->pOrSet is not NULL then we care about only the
156387** prerequisites and rRun and nOut costs of the N best loops. That
156388** information is gathered in the pBuilder->pOrSet object. This special
156389** processing mode is used only for OR clause processing.
156390**
156391** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
156392** still might overwrite similar loops with the new template if the
156393** new template is better. Loops may be overwritten if the following
156394** conditions are met:
156395**
156396** (1) They have the same iTab.
156397** (2) They have the same iSortIdx.
156398** (3) The template has same or fewer dependencies than the current loop
156399** (4) The template has the same or lower cost than the current loop
156400*/
156401static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
156402 WhereLoop **ppPrev, *p;
156403 WhereInfo *pWInfo = pBuilder->pWInfo;
156404 sqlite3 *db = pWInfo->pParse->db;
156405 int rc;
156406
156407 /* Stop the search once we hit the query planner search limit */
156408 if( pBuilder->iPlanLimit==0 ){
156409 WHERETRACE(0xffffffff,("=== query planner search limit reached ===\n"));
156410 if( pBuilder->pOrSet ) pBuilder->pOrSet->n = 0;
156411 return SQLITE_DONE101;
156412 }
156413 pBuilder->iPlanLimit--;
156414
156415 whereLoopAdjustCost(pWInfo->pLoops, pTemplate);
156416
156417 /* If pBuilder->pOrSet is defined, then only keep track of the costs
156418 ** and prereqs.
156419 */
156420 if( pBuilder->pOrSet!=0 ){
156421 if( pTemplate->nLTerm ){
156422#if WHERETRACE_ENABLED
156423 u16 n = pBuilder->pOrSet->n;
156424 int x =
156425#endif
156426 whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
156427 pTemplate->nOut);
156428#if WHERETRACE_ENABLED /* 0x8 */
156429 if( sqlite3WhereTrace & 0x8 ){
156430 sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n);
156431 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
156432 }
156433#endif
156434 }
156435 return SQLITE_OK0;
156436 }
156437
156438 /* Look for an existing WhereLoop to replace with pTemplate
156439 */
156440 ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate);
156441
156442 if( ppPrev==0 ){
156443 /* There already exists a WhereLoop on the list that is better
156444 ** than pTemplate, so just ignore pTemplate */
156445#if WHERETRACE_ENABLED /* 0x8 */
156446 if( sqlite3WhereTrace & 0x8 ){
156447 sqlite3DebugPrintf(" skip: ");
156448 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
156449 }
156450#endif
156451 return SQLITE_OK0;
156452 }else{
156453 p = *ppPrev;
156454 }
156455
156456 /* If we reach this point it means that either p[] should be overwritten
156457 ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new
156458 ** WhereLoop and insert it.
156459 */
156460#if WHERETRACE_ENABLED /* 0x8 */
156461 if( sqlite3WhereTrace & 0x8 ){
156462 if( p!=0 ){
156463 sqlite3DebugPrintf("replace: ");
156464 sqlite3WhereLoopPrint(p, pBuilder->pWC);
156465 sqlite3DebugPrintf(" with: ");
156466 }else{
156467 sqlite3DebugPrintf(" add: ");
156468 }
156469 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
156470 }
156471#endif
156472 if( p==0 ){
156473 /* Allocate a new WhereLoop to add to the end of the list */
156474 *ppPrev = p = sqlite3DbMallocRawNN(db, sizeof(WhereLoop));
156475 if( p==0 ) return SQLITE_NOMEM_BKPT7;
156476 whereLoopInit(p);
156477 p->pNextLoop = 0;
156478 }else{
156479 /* We will be overwriting WhereLoop p[]. But before we do, first
156480 ** go through the rest of the list and delete any other entries besides
156481 ** p[] that are also supplated by pTemplate */
156482 WhereLoop **ppTail = &p->pNextLoop;
156483 WhereLoop *pToDel;
156484 while( *ppTail ){
156485 ppTail = whereLoopFindLesser(ppTail, pTemplate);
156486 if( ppTail==0 ) break;
156487 pToDel = *ppTail;
156488 if( pToDel==0 ) break;
156489 *ppTail = pToDel->pNextLoop;
156490#if WHERETRACE_ENABLED /* 0x8 */
156491 if( sqlite3WhereTrace & 0x8 ){
156492 sqlite3DebugPrintf(" delete: ");
156493 sqlite3WhereLoopPrint(pToDel, pBuilder->pWC);
156494 }
156495#endif
156496 whereLoopDelete(db, pToDel);
156497 }
156498 }
156499 rc = whereLoopXfer(db, p, pTemplate);
156500 if( (p->wsFlags & WHERE_VIRTUALTABLE0x00000400)==0 ){
156501 Index *pIndex = p->u.btree.pIndex;
156502 if( pIndex && pIndex->idxType==SQLITE_IDXTYPE_IPK3 ){
156503 p->u.btree.pIndex = 0;
156504 }
156505 }
156506 return rc;
156507}
156508
156509/*
156510** Adjust the WhereLoop.nOut value downward to account for terms of the
156511** WHERE clause that reference the loop but which are not used by an
156512** index.
156513*
156514** For every WHERE clause term that is not used by the index
156515** and which has a truth probability assigned by one of the likelihood(),
156516** likely(), or unlikely() SQL functions, reduce the estimated number
156517** of output rows by the probability specified.
156518**
156519** TUNING: For every WHERE clause term that is not used by the index
156520** and which does not have an assigned truth probability, heuristics
156521** described below are used to try to estimate the truth probability.
156522** TODO --> Perhaps this is something that could be improved by better
156523** table statistics.
156524**
156525** Heuristic 1: Estimate the truth probability as 93.75%. The 93.75%
156526** value corresponds to -1 in LogEst notation, so this means decrement
156527** the WhereLoop.nOut field for every such WHERE clause term.
156528**
156529** Heuristic 2: If there exists one or more WHERE clause terms of the
156530** form "x==EXPR" and EXPR is not a constant 0 or 1, then make sure the
156531** final output row estimate is no greater than 1/4 of the total number
156532** of rows in the table. In other words, assume that x==EXPR will filter
156533** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the
156534** "x" column is boolean or else -1 or 0 or 1 is a common default value
156535** on the "x" column and so in that case only cap the output row estimate
156536** at 1/2 instead of 1/4.
156537*/
156538static void whereLoopOutputAdjust(
156539 WhereClause *pWC, /* The WHERE clause */
156540 WhereLoop *pLoop, /* The loop to adjust downward */
156541 LogEst nRow /* Number of rows in the entire table */
156542){
156543 WhereTerm *pTerm, *pX;
156544 Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
156545 int i, j;
156546 LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */
156547
156548 assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 )((void) (0));
156549 for(i=pWC->nBase, pTerm=pWC->a; i>0; i--, pTerm++){
156550 assert( pTerm!=0 )((void) (0));
156551 if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
156552 if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
156553 if( (pTerm->wtFlags & TERM_VIRTUAL0x0002)!=0 ) continue;
156554 for(j=pLoop->nLTerm-1; j>=0; j--){
156555 pX = pLoop->aLTerm[j];
156556 if( pX==0 ) continue;
156557 if( pX==pTerm ) break;
156558 if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
156559 }
156560 if( j<0 ){
156561 if( pLoop->maskSelf==pTerm->prereqAll ){
156562 /* If there are extra terms in the WHERE clause not used by an index
156563 ** that depend only on the table being scanned, and that will tend to
156564 ** cause many rows to be omitted, then mark that table as
156565 ** "self-culling".
156566 **
156567 ** 2022-03-24: Self-culling only applies if either the extra terms
156568 ** are straight comparison operators that are non-true with NULL
156569 ** operand, or if the loop is not an OUTER JOIN.
156570 */
156571 if( (pTerm->eOperator & 0x3f)!=0
156572 || (pWC->pWInfo->pTabList->a[pLoop->iTab].fg.jointype
156573 & (JT_LEFT0x08|JT_LTORJ0x40))==0
156574 ){
156575 pLoop->wsFlags |= WHERE_SELFCULL0x00800000;
156576 }
156577 }
156578 if( pTerm->truthProb<=0 ){
156579 /* If a truth probability is specified using the likelihood() hints,
156580 ** then use the probability provided by the application. */
156581 pLoop->nOut += pTerm->truthProb;
156582 }else{
156583 /* In the absence of explicit truth probabilities, use heuristics to
156584 ** guess a reasonable truth probability. */
156585 pLoop->nOut--;
156586 if( (pTerm->eOperator&(WO_EQ0x0002|WO_IS0x0080))!=0
156587 && (pTerm->wtFlags & TERM_HIGHTRUTH0)==0 /* tag-20200224-1 */
156588 ){
156589 Expr *pRight = pTerm->pExpr->pRight;
156590 int k = 0;
156591 testcase( pTerm->pExpr->op==TK_IS );
156592 if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
156593 k = 10;
156594 }else{
156595 k = 20;
156596 }
156597 if( iReduce<k ){
156598 pTerm->wtFlags |= TERM_HEURTRUTH0x2000;
156599 iReduce = k;
156600 }
156601 }
156602 }
156603 }
156604 }
156605 if( pLoop->nOut > nRow-iReduce ){
156606 pLoop->nOut = nRow - iReduce;
156607 }
156608}
156609
156610/*
156611** Term pTerm is a vector range comparison operation. The first comparison
156612** in the vector can be optimized using column nEq of the index. This
156613** function returns the total number of vector elements that can be used
156614** as part of the range comparison.
156615**
156616** For example, if the query is:
156617**
156618** WHERE a = ? AND (b, c, d) > (?, ?, ?)
156619**
156620** and the index:
156621**
156622** CREATE INDEX ... ON (a, b, c, d, e)
156623**
156624** then this function would be invoked with nEq=1. The value returned in
156625** this case is 3.
156626*/
156627static int whereRangeVectorLen(
156628 Parse *pParse, /* Parsing context */
156629 int iCur, /* Cursor open on pIdx */
156630 Index *pIdx, /* The index to be used for a inequality constraint */
156631 int nEq, /* Number of prior equality constraints on same index */
156632 WhereTerm *pTerm /* The vector inequality constraint */
156633){
156634 int nCmp = sqlite3ExprVectorSize(pTerm->pExpr->pLeft);
156635 int i;
156636
156637 nCmp = MIN(nCmp, (pIdx->nColumn - nEq))((nCmp)<((pIdx->nColumn - nEq))?(nCmp):((pIdx->nColumn
- nEq)))
;
156638 for(i=1; i<nCmp; i++){
156639 /* Test if comparison i of pTerm is compatible with column (i+nEq)
156640 ** of the index. If not, exit the loop. */
156641 char aff; /* Comparison affinity */
156642 char idxaff = 0; /* Indexed columns affinity */
156643 CollSeq *pColl; /* Comparison collation sequence */
156644 Expr *pLhs, *pRhs;
156645
156646 assert( ExprUseXList(pTerm->pExpr->pLeft) )((void) (0));
156647 pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr;
156648 pRhs = pTerm->pExpr->pRight;
156649 if( ExprUseXSelect(pRhs)(((pRhs)->flags&0x001000)!=0) ){
156650 pRhs = pRhs->x.pSelect->pEList->a[i].pExpr;
156651 }else{
156652 pRhs = pRhs->x.pList->a[i].pExpr;
156653 }
156654
156655 /* Check that the LHS of the comparison is a column reference to
156656 ** the right column of the right source table. And that the sort
156657 ** order of the index column is the same as the sort order of the
156658 ** leftmost index column. */
156659 if( pLhs->op!=TK_COLUMN167
156660 || pLhs->iTable!=iCur
156661 || pLhs->iColumn!=pIdx->aiColumn[i+nEq]
156662 || pIdx->aSortOrder[i+nEq]!=pIdx->aSortOrder[nEq]
156663 ){
156664 break;
156665 }
156666
156667 testcase( pLhs->iColumn==XN_ROWID );
156668 aff = sqlite3CompareAffinity(pRhs, sqlite3ExprAffinity(pLhs));
156669 idxaff = sqlite3TableColumnAffinity(pIdx->pTable, pLhs->iColumn);
156670 if( aff!=idxaff ) break;
156671
156672 pColl = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs);
156673 if( pColl==0 ) break;
156674 if( sqlite3StrICmp(pColl->zName, pIdx->azColl[i+nEq]) ) break;
156675 }
156676 return i;
156677}
156678
156679/*
156680** Adjust the cost C by the costMult facter T. This only occurs if
156681** compiled with -DSQLITE_ENABLE_COSTMULT
156682*/
156683#ifdef SQLITE_ENABLE_COSTMULT
156684# define ApplyCostMultiplier(C,T) C += T
156685#else
156686# define ApplyCostMultiplier(C,T)
156687#endif
156688
156689/*
156690** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
156691** index pIndex. Try to match one more.
156692**
156693** When this function is called, pBuilder->pNew->nOut contains the
156694** number of rows expected to be visited by filtering using the nEq
156695** terms only. If it is modified, this value is restored before this
156696** function returns.
156697**
156698** If pProbe->idxType==SQLITE_IDXTYPE_IPK, that means pIndex is
156699** a fake index used for the INTEGER PRIMARY KEY.
156700*/
156701static int whereLoopAddBtreeIndex(
156702 WhereLoopBuilder *pBuilder, /* The WhereLoop factory */
156703 SrcItem *pSrc, /* FROM clause term being analyzed */
156704 Index *pProbe, /* An index on pSrc */
156705 LogEst nInMul /* log(Number of iterations due to IN) */
156706){
156707 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */
156708 Parse *pParse = pWInfo->pParse; /* Parsing context */
156709 sqlite3 *db = pParse->db; /* Database connection malloc context */
156710 WhereLoop *pNew; /* Template WhereLoop under construction */
156711 WhereTerm *pTerm; /* A WhereTerm under consideration */
156712 int opMask; /* Valid operators for constraints */
156713 WhereScan scan; /* Iterator for WHERE terms */
156714 Bitmask saved_prereq; /* Original value of pNew->prereq */
156715 u16 saved_nLTerm; /* Original value of pNew->nLTerm */
156716 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */
156717 u16 saved_nBtm; /* Original value of pNew->u.btree.nBtm */
156718 u16 saved_nTop; /* Original value of pNew->u.btree.nTop */
156719 u16 saved_nSkip; /* Original value of pNew->nSkip */
156720 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
156721 LogEst saved_nOut; /* Original value of pNew->nOut */
156722 int rc = SQLITE_OK0; /* Return code */
156723 LogEst rSize; /* Number of rows in the table */
156724 LogEst rLogSize; /* Logarithm of table size */
156725 WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */
156726
156727 pNew = pBuilder->pNew;
156728 if( db->mallocFailed ) return SQLITE_NOMEM_BKPT7;
156729 WHERETRACE(0x800, ("BEGIN %s.addBtreeIdx(%s), nEq=%d, nSkip=%d, rRun=%d\n",
156730 pProbe->pTable->zName,pProbe->zName,
156731 pNew->u.btree.nEq, pNew->nSkip, pNew->rRun));
156732
156733 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 )((void) (0));
156734 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 )((void) (0));
156735 if( pNew->wsFlags & WHERE_BTM_LIMIT0x00000020 ){
156736 opMask = WO_LT(0x0002<<(56 -53))|WO_LE(0x0002<<(55 -53));
156737 }else{
156738 assert( pNew->u.btree.nBtm==0 )((void) (0));
156739 opMask = WO_EQ0x0002|WO_IN0x0001|WO_GT(0x0002<<(54 -53))|WO_GE(0x0002<<(57 -53))|WO_LT(0x0002<<(56 -53))|WO_LE(0x0002<<(55 -53))|WO_ISNULL0x0100|WO_IS0x0080;
156740 }
156741 if( pProbe->bUnordered ) opMask &= ~(WO_GT(0x0002<<(54 -53))|WO_GE(0x0002<<(57 -53))|WO_LT(0x0002<<(56 -53))|WO_LE(0x0002<<(55 -53)));
156742
156743 assert( pNew->u.btree.nEq<pProbe->nColumn )((void) (0));
156744 assert( pNew->u.btree.nEq<pProbe->nKeyCol((void) (0))
156745 || pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY )((void) (0));
156746
156747 saved_nEq = pNew->u.btree.nEq;
156748 saved_nBtm = pNew->u.btree.nBtm;
156749 saved_nTop = pNew->u.btree.nTop;
156750 saved_nSkip = pNew->nSkip;
156751 saved_nLTerm = pNew->nLTerm;
156752 saved_wsFlags = pNew->wsFlags;
156753 saved_prereq = pNew->prereq;
156754 saved_nOut = pNew->nOut;
156755 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, saved_nEq,
156756 opMask, pProbe);
156757 pNew->rSetup = 0;
156758 rSize = pProbe->aiRowLogEst[0];
156759 rLogSize = estLog(rSize);
156760 for(; rc==SQLITE_OK0 && pTerm!=0; pTerm = whereScanNext(&scan)){
156761 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
156762 LogEst rCostIdx;
156763 LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */
156764 int nIn = 0;
156765#ifdef SQLITE_ENABLE_STAT4
156766 int nRecValid = pBuilder->nRecValid;
156767#endif
156768 if( (eOp==WO_ISNULL0x0100 || (pTerm->wtFlags&TERM_VNULL0x0080)!=0)
156769 && indexColumnNotNull(pProbe, saved_nEq)
156770 ){
156771 continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
156772 }
156773 if( pTerm->prereqRight & pNew->maskSelf ) continue;
156774
156775 /* Do not allow the upper bound of a LIKE optimization range constraint
156776 ** to mix with a lower range bound from some other source */
156777 if( pTerm->wtFlags & TERM_LIKEOPT0x0100 && pTerm->eOperator==WO_LT(0x0002<<(56 -53)) ) continue;
156778
156779 /* tag-20191211-001: Do not allow constraints from the WHERE clause to
156780 ** be used by the right table of a LEFT JOIN nor by the left table of a
156781 ** RIGHT JOIN. Only constraints in the ON clause are allowed.
156782 ** See tag-20191211-002 for the vtab equivalent.
156783 **
156784 ** 2022-06-06: See https://sqlite.org/forum/forumpost/206d99a16dd9212f
156785 ** for an example of a WHERE clause constraints that may not be used on
156786 ** the right table of a RIGHT JOIN because the constraint implies a
156787 ** not-NULL condition on the left table of the RIGHT JOIN.
156788 **
156789 ** 2022-06-10: The same condition applies to termCanDriveIndex() above.
156790 ** https://sqlite.org/forum/forumpost/51e6959f61
156791 */
156792 if( (pSrc->fg.jointype & (JT_LEFT0x08|JT_LTORJ0x40|JT_RIGHT0x10))!=0 ){
156793 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LEFT );
156794 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_RIGHT );
156795 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LTORJ );
156796 testcase( ExprHasProperty(pTerm->pExpr, EP_OuterON) )
156797 testcase( ExprHasProperty(pTerm->pExpr, EP_InnerON) );
156798 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON)(((pTerm->pExpr)->flags&(0x000001|0x000002))!=0)
156799 || pTerm->pExpr->w.iJoin != pSrc->iCursor
156800 ){
156801 continue;
156802 }
156803 }
156804
156805 if( IsUniqueIndex(pProbe)((pProbe)->onError!=0) && saved_nEq==pProbe->nKeyCol-1 ){
156806 pBuilder->bldFlags1 |= SQLITE_BLDF1_UNIQUE0x0002;
156807 }else{
156808 pBuilder->bldFlags1 |= SQLITE_BLDF1_INDEXED0x0001;
156809 }
156810 pNew->wsFlags = saved_wsFlags;
156811 pNew->u.btree.nEq = saved_nEq;
156812 pNew->u.btree.nBtm = saved_nBtm;
156813 pNew->u.btree.nTop = saved_nTop;
156814 pNew->nLTerm = saved_nLTerm;
156815 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
156816 pNew->aLTerm[pNew->nLTerm++] = pTerm;
156817 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
156818
156819 assert( nInMul==0((void) (0))
156820 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0((void) (0))
156821 || (pNew->wsFlags & WHERE_COLUMN_IN)!=0((void) (0))
156822 || (pNew->wsFlags & WHERE_SKIPSCAN)!=0((void) (0))
156823 )((void) (0));
156824
156825 if( eOp & WO_IN0x0001 ){
156826 Expr *pExpr = pTerm->pExpr;
156827 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
156828 /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */
156829 int i;
156830 nIn = 46; assert( 46==sqlite3LogEst(25) )((void) (0));
156831
156832 /* The expression may actually be of the form (x, y) IN (SELECT...).
156833 ** In this case there is a separate term for each of (x) and (y).
156834 ** However, the nIn multiplier should only be applied once, not once
156835 ** for each such term. The following loop checks that pTerm is the
156836 ** first such term in use, and sets nIn back to 0 if it is not. */
156837 for(i=0; i<pNew->nLTerm-1; i++){
156838 if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ) nIn = 0;
156839 }
156840 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr)(pExpr->x.pList && pExpr->x.pList->nExpr) ){
156841 /* "x IN (value, value, ...)" */
156842 nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
156843 }
156844 if( pProbe->hasStat1 && rLogSize>=10 ){
156845 LogEst M, logK, x;
156846 /* Let:
156847 ** N = the total number of rows in the table
156848 ** K = the number of entries on the RHS of the IN operator
156849 ** M = the number of rows in the table that match terms to the
156850 ** to the left in the same index. If the IN operator is on
156851 ** the left-most index column, M==N.
156852 **
156853 ** Given the definitions above, it is better to omit the IN operator
156854 ** from the index lookup and instead do a scan of the M elements,
156855 ** testing each scanned row against the IN operator separately, if:
156856 **
156857 ** M*log(K) < K*log(N)
156858 **
156859 ** Our estimates for M, K, and N might be inaccurate, so we build in
156860 ** a safety margin of 2 (LogEst: 10) that favors using the IN operator
156861 ** with the index, as using an index has better worst-case behavior.
156862 ** If we do not have real sqlite_stat1 data, always prefer to use
156863 ** the index. Do not bother with this optimization on very small
156864 ** tables (less than 2 rows) as it is pointless in that case.
156865 */
156866 M = pProbe->aiRowLogEst[saved_nEq];
156867 logK = estLog(nIn);
156868 /* TUNING v----- 10 to bias toward indexed IN */
156869 x = M + logK + 10 - (nIn + rLogSize);
156870 if( x>=0 ){
156871 WHERETRACE(0x40,
156872 ("IN operator (N=%d M=%d logK=%d nIn=%d rLogSize=%d x=%d) "
156873 "prefers indexed lookup\n",
156874 saved_nEq, M, logK, nIn, rLogSize, x));
156875 }else if( nInMul<2 && OptimizationEnabled(db, SQLITE_SeekScan)(((db)->dbOptFlags&(0x00020000))==0) ){
156876 WHERETRACE(0x40,
156877 ("IN operator (N=%d M=%d logK=%d nIn=%d rLogSize=%d x=%d"
156878 " nInMul=%d) prefers skip-scan\n",
156879 saved_nEq, M, logK, nIn, rLogSize, x, nInMul));
156880 pNew->wsFlags |= WHERE_IN_SEEKSCAN0x00100000;
156881 }else{
156882 WHERETRACE(0x40,
156883 ("IN operator (N=%d M=%d logK=%d nIn=%d rLogSize=%d x=%d"
156884 " nInMul=%d) prefers normal scan\n",
156885 saved_nEq, M, logK, nIn, rLogSize, x, nInMul));
156886 continue;
156887 }
156888 }
156889 pNew->wsFlags |= WHERE_COLUMN_IN0x00000004;
156890 }else if( eOp & (WO_EQ0x0002|WO_IS0x0080) ){
156891 int iCol = pProbe->aiColumn[saved_nEq];
156892 pNew->wsFlags |= WHERE_COLUMN_EQ0x00000001;
156893 assert( saved_nEq==pNew->u.btree.nEq )((void) (0));
156894 if( iCol==XN_ROWID(-1)
156895 || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
156896 ){
156897 if( iCol==XN_ROWID(-1) || pProbe->uniqNotNull
156898 || (pProbe->nKeyCol==1 && pProbe->onError && eOp==WO_EQ0x0002)
156899 ){
156900 pNew->wsFlags |= WHERE_ONEROW0x00001000;
156901 }else{
156902 pNew->wsFlags |= WHERE_UNQ_WANTED0x00010000;
156903 }
156904 }
156905 if( scan.iEquiv>1 ) pNew->wsFlags |= WHERE_TRANSCONS0x00200000;
156906 }else if( eOp & WO_ISNULL0x0100 ){
156907 pNew->wsFlags |= WHERE_COLUMN_NULL0x00000008;
156908 }else if( eOp & (WO_GT(0x0002<<(54 -53))|WO_GE(0x0002<<(57 -53))) ){
156909 testcase( eOp & WO_GT );
156910 testcase( eOp & WO_GE );
156911 pNew->wsFlags |= WHERE_COLUMN_RANGE0x00000002|WHERE_BTM_LIMIT0x00000020;
156912 pNew->u.btree.nBtm = whereRangeVectorLen(
156913 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
156914 );
156915 pBtm = pTerm;
156916 pTop = 0;
156917 if( pTerm->wtFlags & TERM_LIKEOPT0x0100 ){
156918 /* Range constraints that come from the LIKE optimization are
156919 ** always used in pairs. */
156920 pTop = &pTerm[1];
156921 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm )((void) (0));
156922 assert( pTop->wtFlags & TERM_LIKEOPT )((void) (0));
156923 assert( pTop->eOperator==WO_LT )((void) (0));
156924 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
156925 pNew->aLTerm[pNew->nLTerm++] = pTop;
156926 pNew->wsFlags |= WHERE_TOP_LIMIT0x00000010;
156927 pNew->u.btree.nTop = 1;
156928 }
156929 }else{
156930 assert( eOp & (WO_LT|WO_LE) )((void) (0));
156931 testcase( eOp & WO_LT );
156932 testcase( eOp & WO_LE );
156933 pNew->wsFlags |= WHERE_COLUMN_RANGE0x00000002|WHERE_TOP_LIMIT0x00000010;
156934 pNew->u.btree.nTop = whereRangeVectorLen(
156935 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
156936 );
156937 pTop = pTerm;
156938 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT0x00000020)!=0 ?
156939 pNew->aLTerm[pNew->nLTerm-2] : 0;
156940 }
156941
156942 /* At this point pNew->nOut is set to the number of rows expected to
156943 ** be visited by the index scan before considering term pTerm, or the
156944 ** values of nIn and nInMul. In other words, assuming that all
156945 ** "x IN(...)" terms are replaced with "x = ?". This block updates
156946 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */
156947 assert( pNew->nOut==saved_nOut )((void) (0));
156948 if( pNew->wsFlags & WHERE_COLUMN_RANGE0x00000002 ){
156949 /* Adjust nOut using stat4 data. Or, if there is no stat4
156950 ** data, using some other estimate. */
156951 whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
156952 }else{
156953 int nEq = ++pNew->u.btree.nEq;
156954 assert( eOp & (WO_ISNULL|WO_EQ|WO_IN|WO_IS) )((void) (0));
156955
156956 assert( pNew->nOut==saved_nOut )((void) (0));
156957 if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){
156958 assert( (eOp & WO_IN) || nIn==0 )((void) (0));
156959 testcase( eOp & WO_IN );
156960 pNew->nOut += pTerm->truthProb;
156961 pNew->nOut -= nIn;
156962 }else{
156963#ifdef SQLITE_ENABLE_STAT4
156964 tRowcnt nOut = 0;
156965 if( nInMul==0
156966 && pProbe->nSample
156967 && ALWAYS(pNew->u.btree.nEq<=pProbe->nSampleCol)(pNew->u.btree.nEq<=pProbe->nSampleCol)
156968 && ((eOp & WO_IN0x0001)==0 || ExprUseXList(pTerm->pExpr)(((pTerm->pExpr)->flags&0x001000)==0))
156969 && OptimizationEnabled(db, SQLITE_Stat4)(((db)->dbOptFlags&(0x00000800))==0)
156970 ){
156971 Expr *pExpr = pTerm->pExpr;
156972 if( (eOp & (WO_EQ0x0002|WO_ISNULL0x0100|WO_IS0x0080))!=0 ){
156973 testcase( eOp & WO_EQ );
156974 testcase( eOp & WO_IS );
156975 testcase( eOp & WO_ISNULL );
156976 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
156977 }else{
156978 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
156979 }
156980 if( rc==SQLITE_NOTFOUND12 ) rc = SQLITE_OK0;
156981 if( rc!=SQLITE_OK0 ) break; /* Jump out of the pTerm loop */
156982 if( nOut ){
156983 pNew->nOut = sqlite3LogEst(nOut);
156984 if( nEq==1
156985 /* TUNING: Mark terms as "low selectivity" if they seem likely
156986 ** to be true for half or more of the rows in the table.
156987 ** See tag-202002240-1 */
156988 && pNew->nOut+10 > pProbe->aiRowLogEst[0]
156989 ){
156990#if WHERETRACE_ENABLED /* 0x01 */
156991 if( sqlite3WhereTrace & 0x01 ){
156992 sqlite3DebugPrintf(
156993 "STAT4 determines term has low selectivity:\n");
156994 sqlite3WhereTermPrint(pTerm, 999);
156995 }
156996#endif
156997 pTerm->wtFlags |= TERM_HIGHTRUTH0;
156998 if( pTerm->wtFlags & TERM_HEURTRUTH0x2000 ){
156999 /* If the term has previously been used with an assumption of
157000 ** higher selectivity, then set the flag to rerun the
157001 ** loop computations. */
157002 pBuilder->bldFlags2 |= SQLITE_BLDF2_2NDPASS0x0004;
157003 }
157004 }
157005 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
157006 pNew->nOut -= nIn;
157007 }
157008 }
157009 if( nOut==0 )
157010#endif
157011 {
157012 pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
157013 if( eOp & WO_ISNULL0x0100 ){
157014 /* TUNING: If there is no likelihood() value, assume that a
157015 ** "col IS NULL" expression matches twice as many rows
157016 ** as (col=?). */
157017 pNew->nOut += 10;
157018 }
157019 }
157020 }
157021 }
157022
157023 /* Set rCostIdx to the cost of visiting selected rows in index. Add
157024 ** it to pNew->rRun, which is currently set to the cost of the index
157025 ** seek only. Then, if this is a non-covering index, add the cost of
157026 ** visiting the rows in the main table. */
157027 assert( pSrc->pTab->szTabRow>0 )((void) (0));
157028 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
157029 pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
157030 if( (pNew->wsFlags & (WHERE_IDX_ONLY0x00000040|WHERE_IPK0x00000100))==0 ){
157031 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
157032 }
157033 ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
157034
157035 nOutUnadjusted = pNew->nOut;
157036 pNew->rRun += nInMul + nIn;
157037 pNew->nOut += nInMul + nIn;
157038 whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
157039 rc = whereLoopInsert(pBuilder, pNew);
157040
157041 if( pNew->wsFlags & WHERE_COLUMN_RANGE0x00000002 ){
157042 pNew->nOut = saved_nOut;
157043 }else{
157044 pNew->nOut = nOutUnadjusted;
157045 }
157046
157047 if( (pNew->wsFlags & WHERE_TOP_LIMIT0x00000010)==0
157048 && pNew->u.btree.nEq<pProbe->nColumn
157049 && (pNew->u.btree.nEq<pProbe->nKeyCol ||
157050 pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY2)
157051 ){
157052 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
157053 }
157054 pNew->nOut = saved_nOut;
157055#ifdef SQLITE_ENABLE_STAT4
157056 pBuilder->nRecValid = nRecValid;
157057#endif
157058 }
157059 pNew->prereq = saved_prereq;
157060 pNew->u.btree.nEq = saved_nEq;
157061 pNew->u.btree.nBtm = saved_nBtm;
157062 pNew->u.btree.nTop = saved_nTop;
157063 pNew->nSkip = saved_nSkip;
157064 pNew->wsFlags = saved_wsFlags;
157065 pNew->nOut = saved_nOut;
157066 pNew->nLTerm = saved_nLTerm;
157067
157068 /* Consider using a skip-scan if there are no WHERE clause constraints
157069 ** available for the left-most terms of the index, and if the average
157070 ** number of repeats in the left-most terms is at least 18.
157071 **
157072 ** The magic number 18 is selected on the basis that scanning 17 rows
157073 ** is almost always quicker than an index seek (even though if the index
157074 ** contains fewer than 2^17 rows we assume otherwise in other parts of
157075 ** the code). And, even if it is not, it should not be too much slower.
157076 ** On the other hand, the extra seeks could end up being significantly
157077 ** more expensive. */
157078 assert( 42==sqlite3LogEst(18) )((void) (0));
157079 if( saved_nEq==saved_nSkip
157080 && saved_nEq+1<pProbe->nKeyCol
157081 && saved_nEq==pNew->nLTerm
157082 && pProbe->noSkipScan==0
157083 && pProbe->hasStat1!=0
157084 && OptimizationEnabled(db, SQLITE_SkipScan)(((db)->dbOptFlags&(0x00004000))==0)
157085 && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
157086 && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK0
157087 ){
157088 LogEst nIter;
157089 pNew->u.btree.nEq++;
157090 pNew->nSkip++;
157091 pNew->aLTerm[pNew->nLTerm++] = 0;
157092 pNew->wsFlags |= WHERE_SKIPSCAN0x00008000;
157093 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
157094 pNew->nOut -= nIter;
157095 /* TUNING: Because uncertainties in the estimates for skip-scan queries,
157096 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
157097 nIter += 5;
157098 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul);
157099 pNew->nOut = saved_nOut;
157100 pNew->u.btree.nEq = saved_nEq;
157101 pNew->nSkip = saved_nSkip;
157102 pNew->wsFlags = saved_wsFlags;
157103 }
157104
157105 WHERETRACE(0x800, ("END %s.addBtreeIdx(%s), nEq=%d, rc=%d\n",
157106 pProbe->pTable->zName, pProbe->zName, saved_nEq, rc));
157107 return rc;
157108}
157109
157110/*
157111** Return True if it is possible that pIndex might be useful in
157112** implementing the ORDER BY clause in pBuilder.
157113**
157114** Return False if pBuilder does not contain an ORDER BY clause or
157115** if there is no way for pIndex to be useful in implementing that
157116** ORDER BY clause.
157117*/
157118static int indexMightHelpWithOrderBy(
157119 WhereLoopBuilder *pBuilder,
157120 Index *pIndex,
157121 int iCursor
157122){
157123 ExprList *pOB;
157124 ExprList *aColExpr;
157125 int ii, jj;
157126
157127 if( pIndex->bUnordered ) return 0;
157128 if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
157129 for(ii=0; ii<pOB->nExpr; ii++){
157130 Expr *pExpr = sqlite3ExprSkipCollateAndLikely(pOB->a[ii].pExpr);
157131 if( NEVER(pExpr==0)(pExpr==0) ) continue;
157132 if( pExpr->op==TK_COLUMN167 && pExpr->iTable==iCursor ){
157133 if( pExpr->iColumn<0 ) return 1;
157134 for(jj=0; jj<pIndex->nKeyCol; jj++){
157135 if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
157136 }
157137 }else if( (aColExpr = pIndex->aColExpr)!=0 ){
157138 for(jj=0; jj<pIndex->nKeyCol; jj++){
157139 if( pIndex->aiColumn[jj]!=XN_EXPR(-2) ) continue;
157140 if( sqlite3ExprCompareSkip(pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){
157141 return 1;
157142 }
157143 }
157144 }
157145 }
157146 return 0;
157147}
157148
157149/* Check to see if a partial index with pPartIndexWhere can be used
157150** in the current query. Return true if it can be and false if not.
157151*/
157152static int whereUsablePartialIndex(
157153 int iTab, /* The table for which we want an index */
157154 u8 jointype, /* The JT_* flags on the join */
157155 WhereClause *pWC, /* The WHERE clause of the query */
157156 Expr *pWhere /* The WHERE clause from the partial index */
157157){
157158 int i;
157159 WhereTerm *pTerm;
157160 Parse *pParse;
157161
157162 if( jointype & JT_LTORJ0x40 ) return 0;
157163 pParse = pWC->pWInfo->pParse;
157164 while( pWhere->op==TK_AND44 ){
157165 if( !whereUsablePartialIndex(iTab,jointype,pWC,pWhere->pLeft) ) return 0;
157166 pWhere = pWhere->pRight;
157167 }
157168 if( pParse->db->flags & SQLITE_EnableQPSG0x00800000 ) pParse = 0;
157169 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
157170 Expr *pExpr;
157171 pExpr = pTerm->pExpr;
157172 if( (!ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(0x000001))!=0) || pExpr->w.iJoin==iTab)
157173 && ((jointype & JT_OUTER0x20)==0 || ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(0x000001))!=0))
157174 && sqlite3ExprImpliesExpr(pParse, pExpr, pWhere, iTab)
157175 && (pTerm->wtFlags & TERM_VNULL0x0080)==0
157176 ){
157177 return 1;
157178 }
157179 }
157180 return 0;
157181}
157182
157183/*
157184** Add all WhereLoop objects for a single table of the join where the table
157185** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
157186** a b-tree table, not a virtual table.
157187**
157188** The costs (WhereLoop.rRun) of the b-tree loops added by this function
157189** are calculated as follows:
157190**
157191** For a full scan, assuming the table (or index) contains nRow rows:
157192**
157193** cost = nRow * 3.0 // full-table scan
157194** cost = nRow * K // scan of covering index
157195** cost = nRow * (K+3.0) // scan of non-covering index
157196**
157197** where K is a value between 1.1 and 3.0 set based on the relative
157198** estimated average size of the index and table records.
157199**
157200** For an index scan, where nVisit is the number of index rows visited
157201** by the scan, and nSeek is the number of seek operations required on
157202** the index b-tree:
157203**
157204** cost = nSeek * (log(nRow) + K * nVisit) // covering index
157205** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
157206**
157207** Normally, nSeek is 1. nSeek values greater than 1 come about if the
157208** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when
157209** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
157210**
157211** The estimated values (nRow, nVisit, nSeek) often contain a large amount
157212** of uncertainty. For this reason, scoring is designed to pick plans that
157213** "do the least harm" if the estimates are inaccurate. For example, a
157214** log(nRow) factor is omitted from a non-covering index scan in order to
157215** bias the scoring in favor of using an index, since the worst-case
157216** performance of using an index is far better than the worst-case performance
157217** of a full table scan.
157218*/
157219static int whereLoopAddBtree(
157220 WhereLoopBuilder *pBuilder, /* WHERE clause information */
157221 Bitmask mPrereq /* Extra prerequesites for using this table */
157222){
157223 WhereInfo *pWInfo; /* WHERE analysis context */
157224 Index *pProbe; /* An index we are evaluating */
157225 Index sPk; /* A fake index object for the primary key */
157226 LogEst aiRowEstPk[2]; /* The aiRowLogEst[] value for the sPk index */
157227 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
157228 SrcList *pTabList; /* The FROM clause */
157229 SrcItem *pSrc; /* The FROM clause btree term to add */
157230 WhereLoop *pNew; /* Template WhereLoop object */
157231 int rc = SQLITE_OK0; /* Return code */
157232 int iSortIdx = 1; /* Index number */
157233 int b; /* A boolean value */
157234 LogEst rSize; /* number of rows in the table */
157235 WhereClause *pWC; /* The parsed WHERE clause */
157236 Table *pTab; /* Table being queried */
157237
157238 pNew = pBuilder->pNew;
157239 pWInfo = pBuilder->pWInfo;
157240 pTabList = pWInfo->pTabList;
157241 pSrc = pTabList->a + pNew->iTab;
157242 pTab = pSrc->pTab;
157243 pWC = pBuilder->pWC;
157244 assert( !IsVirtual(pSrc->pTab) )((void) (0));
157245
157246 if( pSrc->fg.isIndexedBy ){
157247 assert( pSrc->fg.isCte==0 )((void) (0));
157248 /* An INDEXED BY clause specifies a particular index to use */
157249 pProbe = pSrc->u2.pIBIndex;
157250 }else if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
157251 pProbe = pTab->pIndex;
157252 }else{
157253 /* There is no INDEXED BY clause. Create a fake Index object in local
157254 ** variable sPk to represent the rowid primary key index. Make this
157255 ** fake index the first in a chain of Index objects with all of the real
157256 ** indices to follow */
157257 Index *pFirst; /* First of real indices on the table */
157258 memset(&sPk, 0, sizeof(Index));
157259 sPk.nKeyCol = 1;
157260 sPk.nColumn = 1;
157261 sPk.aiColumn = &aiColumnPk;
157262 sPk.aiRowLogEst = aiRowEstPk;
157263 sPk.onError = OE_Replace5;
157264 sPk.pTable = pTab;
157265 sPk.szIdxRow = pTab->szTabRow;
157266 sPk.idxType = SQLITE_IDXTYPE_IPK3;
157267 aiRowEstPk[0] = pTab->nRowLogEst;
157268 aiRowEstPk[1] = 0;
157269 pFirst = pSrc->pTab->pIndex;
157270 if( pSrc->fg.notIndexed==0 ){
157271 /* The real indices of the table are only considered if the
157272 ** NOT INDEXED qualifier is omitted from the FROM clause */
157273 sPk.pNext = pFirst;
157274 }
157275 pProbe = &sPk;
157276 }
157277 rSize = pTab->nRowLogEst;
157278
157279#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
157280 /* Automatic indexes */
157281 if( !pBuilder->pOrSet /* Not part of an OR optimization */
157282 && (pWInfo->wctrlFlags & (WHERE_RIGHT_JOIN0x1000|WHERE_OR_SUBCLAUSE0x0020))==0
157283 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex0x00008000)!=0
157284 && !pSrc->fg.isIndexedBy /* Has no INDEXED BY clause */
157285 && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */
157286 && HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) /* Not WITHOUT ROWID table. (FIXME: Why not?) */
157287 && !pSrc->fg.isCorrelated /* Not a correlated subquery */
157288 && !pSrc->fg.isRecursive /* Not a recursive common table expression. */
157289 && (pSrc->fg.jointype & JT_RIGHT0x10)==0 /* Not the right tab of a RIGHT JOIN */
157290 ){
157291 /* Generate auto-index WhereLoops */
157292 LogEst rLogSize; /* Logarithm of the number of rows in the table */
157293 WhereTerm *pTerm;
157294 WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
157295 rLogSize = estLog(rSize);
157296 for(pTerm=pWC->a; rc==SQLITE_OK0 && pTerm<pWCEnd; pTerm++){
157297 if( pTerm->prereqRight & pNew->maskSelf ) continue;
157298 if( termCanDriveIndex(pTerm, pSrc, 0) ){
157299 pNew->u.btree.nEq = 1;
157300 pNew->nSkip = 0;
157301 pNew->u.btree.pIndex = 0;
157302 pNew->nLTerm = 1;
157303 pNew->aLTerm[0] = pTerm;
157304 /* TUNING: One-time cost for computing the automatic index is
157305 ** estimated to be X*N*log2(N) where N is the number of rows in
157306 ** the table being indexed and where X is 7 (LogEst=28) for normal
157307 ** tables or 0.5 (LogEst=-10) for views and subqueries. The value
157308 ** of X is smaller for views and subqueries so that the query planner
157309 ** will be more aggressive about generating automatic indexes for
157310 ** those objects, since there is no opportunity to add schema
157311 ** indexes on subqueries and views. */
157312 pNew->rSetup = rLogSize + rSize;
157313 if( !IsView(pTab)((pTab)->eTabType==2) && (pTab->tabFlags & TF_Ephemeral0x00004000)==0 ){
157314 pNew->rSetup += 28;
157315 }else{
157316 pNew->rSetup -= 10;
157317 }
157318 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
157319 if( pNew->rSetup<0 ) pNew->rSetup = 0;
157320 /* TUNING: Each index lookup yields 20 rows in the table. This
157321 ** is more than the usual guess of 10 rows, since we have no way
157322 ** of knowing how selective the index will ultimately be. It would
157323 ** not be unreasonable to make this value much larger. */
157324 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) )((void) (0));
157325 pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
157326 pNew->wsFlags = WHERE_AUTO_INDEX0x00004000;
157327 pNew->prereq = mPrereq | pTerm->prereqRight;
157328 rc = whereLoopInsert(pBuilder, pNew);
157329 }
157330 }
157331 }
157332#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
157333
157334 /* Loop over all indices. If there was an INDEXED BY clause, then only
157335 ** consider index pProbe. */
157336 for(; rc==SQLITE_OK0 && pProbe;
157337 pProbe=(pSrc->fg.isIndexedBy ? 0 : pProbe->pNext), iSortIdx++
157338 ){
157339 if( pProbe->pPartIdxWhere!=0
157340 && !whereUsablePartialIndex(pSrc->iCursor, pSrc->fg.jointype, pWC,
157341 pProbe->pPartIdxWhere)
157342 ){
157343 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */
157344 continue; /* Partial index inappropriate for this query */
157345 }
157346 if( pProbe->bNoQuery ) continue;
157347 rSize = pProbe->aiRowLogEst[0];
157348 pNew->u.btree.nEq = 0;
157349 pNew->u.btree.nBtm = 0;
157350 pNew->u.btree.nTop = 0;
157351 pNew->nSkip = 0;
157352 pNew->nLTerm = 0;
157353 pNew->iSortIdx = 0;
157354 pNew->rSetup = 0;
157355 pNew->prereq = mPrereq;
157356 pNew->nOut = rSize;
157357 pNew->u.btree.pIndex = pProbe;
157358 b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
157359
157360 /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */
157361 assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 )((void) (0));
157362 if( pProbe->idxType==SQLITE_IDXTYPE_IPK3 ){
157363 /* Integer primary key index */
157364 pNew->wsFlags = WHERE_IPK0x00000100;
157365
157366 /* Full table scan */
157367 pNew->iSortIdx = b ? iSortIdx : 0;
157368 /* TUNING: Cost of full table scan is 3.0*N. The 3.0 factor is an
157369 ** extra cost designed to discourage the use of full table scans,
157370 ** since index lookups have better worst-case performance if our
157371 ** stat guesses are wrong. Reduce the 3.0 penalty slightly
157372 ** (to 2.75) if we have valid STAT4 information for the table.
157373 ** At 2.75, a full table scan is preferred over using an index on
157374 ** a column with just two distinct values where each value has about
157375 ** an equal number of appearances. Without STAT4 data, we still want
157376 ** to use an index in that case, since the constraint might be for
157377 ** the scarcer of the two values, and in that case an index lookup is
157378 ** better.
157379 */
157380#ifdef SQLITE_ENABLE_STAT4
157381 pNew->rRun = rSize + 16 - 2*((pTab->tabFlags & TF_HasStat40x00002000)!=0);
157382#else
157383 pNew->rRun = rSize + 16;
157384#endif
157385 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
157386 whereLoopOutputAdjust(pWC, pNew, rSize);
157387 rc = whereLoopInsert(pBuilder, pNew);
157388 pNew->nOut = rSize;
157389 if( rc ) break;
157390 }else{
157391 Bitmask m;
157392 if( pProbe->isCovering ){
157393 pNew->wsFlags = WHERE_IDX_ONLY0x00000040 | WHERE_INDEXED0x00000200;
157394 m = 0;
157395 }else{
157396 m = pSrc->colUsed & pProbe->colNotIdxed;
157397 pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY0x00000040|WHERE_INDEXED0x00000200) : WHERE_INDEXED0x00000200;
157398 }
157399
157400 /* Full scan via index */
157401 if( b
157402 || !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0)
157403 || pProbe->pPartIdxWhere!=0
157404 || pSrc->fg.isIndexedBy
157405 || ( m==0
157406 && pProbe->bUnordered==0
157407 && (pProbe->szIdxRow<pTab->szTabRow)
157408 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED0x0004)==0
157409 && sqlite3GlobalConfigsqlite3Config.bUseCis
157410 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)(((pWInfo->pParse->db)->dbOptFlags&(0x00000020))
==0)
157411 )
157412 ){
157413 pNew->iSortIdx = b ? iSortIdx : 0;
157414
157415 /* The cost of visiting the index rows is N*K, where K is
157416 ** between 1.1 and 3.0, depending on the relative sizes of the
157417 ** index and table rows. */
157418 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
157419 if( m!=0 ){
157420 /* If this is a non-covering index scan, add in the cost of
157421 ** doing table lookups. The cost will be 3x the number of
157422 ** lookups. Take into account WHERE clause terms that can be
157423 ** satisfied using just the index, and that do not require a
157424 ** table lookup. */
157425 LogEst nLookup = rSize + 16; /* Base cost: N*3 */
157426 int ii;
157427 int iCur = pSrc->iCursor;
157428 WhereClause *pWC2 = &pWInfo->sWC;
157429 for(ii=0; ii<pWC2->nTerm; ii++){
157430 WhereTerm *pTerm = &pWC2->a[ii];
157431 if( !sqlite3ExprCoveredByIndex(pTerm->pExpr, iCur, pProbe) ){
157432 break;
157433 }
157434 /* pTerm can be evaluated using just the index. So reduce
157435 ** the expected number of table lookups accordingly */
157436 if( pTerm->truthProb<=0 ){
157437 nLookup += pTerm->truthProb;
157438 }else{
157439 nLookup--;
157440 if( pTerm->eOperator & (WO_EQ0x0002|WO_IS0x0080) ) nLookup -= 19;
157441 }
157442 }
157443
157444 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, nLookup);
157445 }
157446 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
157447 whereLoopOutputAdjust(pWC, pNew, rSize);
157448 if( (pSrc->fg.jointype & JT_RIGHT0x10)!=0 && pProbe->aColExpr ){
157449 /* Do not do an SCAN of a index-on-expression in a RIGHT JOIN
157450 ** because the cursor used to access the index might not be
157451 ** positioned to the correct row during the right-join no-match
157452 ** loop. */
157453 }else{
157454 rc = whereLoopInsert(pBuilder, pNew);
157455 }
157456 pNew->nOut = rSize;
157457 if( rc ) break;
157458 }
157459 }
157460
157461 pBuilder->bldFlags1 = 0;
157462 rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
157463 if( pBuilder->bldFlags1==SQLITE_BLDF1_INDEXED0x0001 ){
157464 /* If a non-unique index is used, or if a prefix of the key for
157465 ** unique index is used (making the index functionally non-unique)
157466 ** then the sqlite_stat1 data becomes important for scoring the
157467 ** plan */
157468 pTab->tabFlags |= TF_StatsUsed0x00000100;
157469 }
157470#ifdef SQLITE_ENABLE_STAT4
157471 sqlite3Stat4ProbeFree(pBuilder->pRec);
157472 pBuilder->nRecValid = 0;
157473 pBuilder->pRec = 0;
157474#endif
157475 }
157476 return rc;
157477}
157478
157479#ifndef SQLITE_OMIT_VIRTUALTABLE
157480
157481/*
157482** Return true if pTerm is a virtual table LIMIT or OFFSET term.
157483*/
157484static int isLimitTerm(WhereTerm *pTerm){
157485 assert( pTerm->eOperator==WO_AUX || pTerm->eMatchOp==0 )((void) (0));
157486 return pTerm->eMatchOp>=SQLITE_INDEX_CONSTRAINT_LIMIT73
157487 && pTerm->eMatchOp<=SQLITE_INDEX_CONSTRAINT_OFFSET74;
157488}
157489
157490/*
157491** Argument pIdxInfo is already populated with all constraints that may
157492** be used by the virtual table identified by pBuilder->pNew->iTab. This
157493** function marks a subset of those constraints usable, invokes the
157494** xBestIndex method and adds the returned plan to pBuilder.
157495**
157496** A constraint is marked usable if:
157497**
157498** * Argument mUsable indicates that its prerequisites are available, and
157499**
157500** * It is not one of the operators specified in the mExclude mask passed
157501** as the fourth argument (which in practice is either WO_IN or 0).
157502**
157503** Argument mPrereq is a mask of tables that must be scanned before the
157504** virtual table in question. These are added to the plans prerequisites
157505** before it is added to pBuilder.
157506**
157507** Output parameter *pbIn is set to true if the plan added to pBuilder
157508** uses one or more WO_IN terms, or false otherwise.
157509*/
157510static int whereLoopAddVirtualOne(
157511 WhereLoopBuilder *pBuilder,
157512 Bitmask mPrereq, /* Mask of tables that must be used. */
157513 Bitmask mUsable, /* Mask of usable tables */
157514 u16 mExclude, /* Exclude terms using these operators */
157515 sqlite3_index_info *pIdxInfo, /* Populated object for xBestIndex */
157516 u16 mNoOmit, /* Do not omit these constraints */
157517 int *pbIn, /* OUT: True if plan uses an IN(...) op */
157518 int *pbRetryLimit /* OUT: Retry without LIMIT/OFFSET */
157519){
157520 WhereClause *pWC = pBuilder->pWC;
157521 HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
157522 struct sqlite3_index_constraint *pIdxCons;
157523 struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage;
157524 int i;
157525 int mxTerm;
157526 int rc = SQLITE_OK0;
157527 WhereLoop *pNew = pBuilder->pNew;
157528 Parse *pParse = pBuilder->pWInfo->pParse;
157529 SrcItem *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab];
157530 int nConstraint = pIdxInfo->nConstraint;
157531
157532 assert( (mUsable & mPrereq)==mPrereq )((void) (0));
157533 *pbIn = 0;
157534 pNew->prereq = mPrereq;
157535
157536 /* Set the usable flag on the subset of constraints identified by
157537 ** arguments mUsable and mExclude. */
157538 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
157539 for(i=0; i<nConstraint; i++, pIdxCons++){
157540 WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset];
157541 pIdxCons->usable = 0;
157542 if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight
157543 && (pTerm->eOperator & mExclude)==0
157544 && (pbRetryLimit || !isLimitTerm(pTerm))
157545 ){
157546 pIdxCons->usable = 1;
157547 }
157548 }
157549
157550 /* Initialize the output fields of the sqlite3_index_info structure */
157551 memset(pUsage, 0, sizeof(pUsage[0])*nConstraint);
157552 assert( pIdxInfo->needToFreeIdxStr==0 )((void) (0));
157553 pIdxInfo->idxStr = 0;
157554 pIdxInfo->idxNum = 0;
157555 pIdxInfo->orderByConsumed = 0;
157556 pIdxInfo->estimatedCost = SQLITE_BIG_DBL(1e99) / (double)2;
157557 pIdxInfo->estimatedRows = 25;
157558 pIdxInfo->idxFlags = 0;
157559 pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed;
157560 pHidden->mHandleIn = 0;
157561
157562 /* Invoke the virtual table xBestIndex() method */
157563 rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo);
157564 if( rc ){
157565 if( rc==SQLITE_CONSTRAINT19 ){
157566 /* If the xBestIndex method returns SQLITE_CONSTRAINT, that means
157567 ** that the particular combination of parameters provided is unusable.
157568 ** Make no entries in the loop table.
157569 */
157570 WHERETRACE(0xffff, (" ^^^^--- non-viable plan rejected!\n"));
157571 return SQLITE_OK0;
157572 }
157573 return rc;
157574 }
157575
157576 mxTerm = -1;
157577 assert( pNew->nLSlot>=nConstraint )((void) (0));
157578 memset(pNew->aLTerm, 0, sizeof(pNew->aLTerm[0])*nConstraint );
157579 memset(&pNew->u.vtab, 0, sizeof(pNew->u.vtab));
157580 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
157581 for(i=0; i<nConstraint; i++, pIdxCons++){
157582 int iTerm;
157583 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
157584 WhereTerm *pTerm;
157585 int j = pIdxCons->iTermOffset;
157586 if( iTerm>=nConstraint
157587 || j<0
157588 || j>=pWC->nTerm
157589 || pNew->aLTerm[iTerm]!=0
157590 || pIdxCons->usable==0
157591 ){
157592 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
157593 testcase( pIdxInfo->needToFreeIdxStr );
157594 return SQLITE_ERROR1;
157595 }
157596 testcase( iTerm==nConstraint-1 );
157597 testcase( j==0 );
157598 testcase( j==pWC->nTerm-1 );
157599 pTerm = &pWC->a[j];
157600 pNew->prereq |= pTerm->prereqRight;
157601 assert( iTerm<pNew->nLSlot )((void) (0));
157602 pNew->aLTerm[iTerm] = pTerm;
157603 if( iTerm>mxTerm ) mxTerm = iTerm;
157604 testcase( iTerm==15 );
157605 testcase( iTerm==16 );
157606 if( pUsage[i].omit ){
157607 if( i<16 && ((1<<i)&mNoOmit)==0 ){
157608 testcase( i!=iTerm );
157609 pNew->u.vtab.omitMask |= 1<<iTerm;
157610 }else{
157611 testcase( i!=iTerm );
157612 }
157613 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET74 ){
157614 pNew->u.vtab.bOmitOffset = 1;
157615 }
157616 }
157617 if( SMASKBIT32(i)((i)<=31?((unsigned int)1)<<(i):0) & pHidden->mHandleIn ){
157618 pNew->u.vtab.mHandleIn |= MASKBIT32(iTerm)(((unsigned int)1)<<(iTerm));
157619 }else if( (pTerm->eOperator & WO_IN0x0001)!=0 ){
157620 /* A virtual table that is constrained by an IN clause may not
157621 ** consume the ORDER BY clause because (1) the order of IN terms
157622 ** is not necessarily related to the order of output terms and
157623 ** (2) Multiple outputs from a single IN value will not merge
157624 ** together. */
157625 pIdxInfo->orderByConsumed = 0;
157626 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE1;
157627 *pbIn = 1; assert( (mExclude & WO_IN)==0 )((void) (0));
157628 }
157629
157630 assert( pbRetryLimit || !isLimitTerm(pTerm) )((void) (0));
157631 if( isLimitTerm(pTerm) && *pbIn ){
157632 /* If there is an IN(...) term handled as an == (separate call to
157633 ** xFilter for each value on the RHS of the IN) and a LIMIT or
157634 ** OFFSET term handled as well, the plan is unusable. Set output
157635 ** variable *pbRetryLimit to true to tell the caller to retry with
157636 ** LIMIT and OFFSET disabled. */
157637 if( pIdxInfo->needToFreeIdxStr ){
157638 sqlite3_free(pIdxInfo->idxStr);
157639 pIdxInfo->idxStr = 0;
157640 pIdxInfo->needToFreeIdxStr = 0;
157641 }
157642 *pbRetryLimit = 1;
157643 return SQLITE_OK0;
157644 }
157645 }
157646 }
157647
157648 pNew->nLTerm = mxTerm+1;
157649 for(i=0; i<=mxTerm; i++){
157650 if( pNew->aLTerm[i]==0 ){
157651 /* The non-zero argvIdx values must be contiguous. Raise an
157652 ** error if they are not */
157653 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
157654 testcase( pIdxInfo->needToFreeIdxStr );
157655 return SQLITE_ERROR1;
157656 }
157657 }
157658 assert( pNew->nLTerm<=pNew->nLSlot )((void) (0));
157659 pNew->u.vtab.idxNum = pIdxInfo->idxNum;
157660 pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
157661 pIdxInfo->needToFreeIdxStr = 0;
157662 pNew->u.vtab.idxStr = pIdxInfo->idxStr;
157663 pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
157664 pIdxInfo->nOrderBy : 0);
157665 pNew->rSetup = 0;
157666 pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
157667 pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
157668
157669 /* Set the WHERE_ONEROW flag if the xBestIndex() method indicated
157670 ** that the scan will visit at most one row. Clear it otherwise. */
157671 if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE1 ){
157672 pNew->wsFlags |= WHERE_ONEROW0x00001000;
157673 }else{
157674 pNew->wsFlags &= ~WHERE_ONEROW0x00001000;
157675 }
157676 rc = whereLoopInsert(pBuilder, pNew);
157677 if( pNew->u.vtab.needFree ){
157678 sqlite3_free(pNew->u.vtab.idxStr);
157679 pNew->u.vtab.needFree = 0;
157680 }
157681 WHERETRACE(0xffff, (" bIn=%d prereqIn=%04llx prereqOut=%04llx\n",
157682 *pbIn, (sqlite3_uint64)mPrereq,
157683 (sqlite3_uint64)(pNew->prereq & ~mPrereq)));
157684
157685 return rc;
157686}
157687
157688/*
157689** Return the collating sequence for a constraint passed into xBestIndex.
157690**
157691** pIdxInfo must be an sqlite3_index_info structure passed into xBestIndex.
157692** This routine depends on there being a HiddenIndexInfo structure immediately
157693** following the sqlite3_index_info structure.
157694**
157695** Return a pointer to the collation name:
157696**
157697** 1. If there is an explicit COLLATE operator on the constaint, return it.
157698**
157699** 2. Else, if the column has an alternative collation, return that.
157700**
157701** 3. Otherwise, return "BINARY".
157702*/
157703SQLITE_API const char *sqlite3_vtab_collation(sqlite3_index_info *pIdxInfo, int iCons){
157704 HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
157705 const char *zRet = 0;
157706 if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
157707 CollSeq *pC = 0;
157708 int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset;
157709 Expr *pX = pHidden->pWC->a[iTerm].pExpr;
157710 if( pX->pLeft ){
157711 pC = sqlite3ExprCompareCollSeq(pHidden->pParse, pX);
157712 }
157713 zRet = (pC ? pC->zName : sqlite3StrBINARY);
157714 }
157715 return zRet;
157716}
157717
157718/*
157719** Return true if constraint iCons is really an IN(...) constraint, or
157720** false otherwise. If iCons is an IN(...) constraint, set (if bHandle!=0)
157721** or clear (if bHandle==0) the flag to handle it using an iterator.
157722*/
157723SQLITE_API int sqlite3_vtab_in(sqlite3_index_info *pIdxInfo, int iCons, int bHandle){
157724 HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
157725 u32 m = SMASKBIT32(iCons)((iCons)<=31?((unsigned int)1)<<(iCons):0);
157726 if( m & pHidden->mIn ){
157727 if( bHandle==0 ){
157728 pHidden->mHandleIn &= ~m;
157729 }else if( bHandle>0 ){
157730 pHidden->mHandleIn |= m;
157731 }
157732 return 1;
157733 }
157734 return 0;
157735}
157736
157737/*
157738** This interface is callable from within the xBestIndex callback only.
157739**
157740** If possible, set (*ppVal) to point to an object containing the value
157741** on the right-hand-side of constraint iCons.
157742*/
157743SQLITE_API int sqlite3_vtab_rhs_value(
157744 sqlite3_index_info *pIdxInfo, /* Copy of first argument to xBestIndex */
157745 int iCons, /* Constraint for which RHS is wanted */
157746 sqlite3_value **ppVal /* Write value extracted here */
157747){
157748 HiddenIndexInfo *pH = (HiddenIndexInfo*)&pIdxInfo[1];
157749 sqlite3_value *pVal = 0;
157750 int rc = SQLITE_OK0;
157751 if( iCons<0 || iCons>=pIdxInfo->nConstraint ){
157752 rc = SQLITE_MISUSE21; /* EV: R-30545-25046 */
157753 }else{
157754 if( pH->aRhs[iCons]==0 ){
157755 WhereTerm *pTerm = &pH->pWC->a[pIdxInfo->aConstraint[iCons].iTermOffset];
157756 rc = sqlite3ValueFromExpr(
157757 pH->pParse->db, pTerm->pExpr->pRight, ENC(pH->pParse->db)((pH->pParse->db)->enc),
157758 SQLITE_AFF_BLOB0x41, &pH->aRhs[iCons]
157759 );
157760 testcase( rc!=SQLITE_OK );
157761 }
157762 pVal = pH->aRhs[iCons];
157763 }
157764 *ppVal = pVal;
157765
157766 if( rc==SQLITE_OK0 && pVal==0 ){ /* IMP: R-19933-32160 */
157767 rc = SQLITE_NOTFOUND12; /* IMP: R-36424-56542 */
157768 }
157769
157770 return rc;
157771}
157772
157773/*
157774** Return true if ORDER BY clause may be handled as DISTINCT.
157775*/
157776SQLITE_API int sqlite3_vtab_distinct(sqlite3_index_info *pIdxInfo){
157777 HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
157778 assert( pHidden->eDistinct>=0 && pHidden->eDistinct<=3 )((void) (0));
157779 return pHidden->eDistinct;
157780}
157781
157782#if (defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)) \
157783 && !defined(SQLITE_OMIT_VIRTUALTABLE)
157784/*
157785** Cause the prepared statement that is associated with a call to
157786** xBestIndex to potentiall use all schemas. If the statement being
157787** prepared is read-only, then just start read transactions on all
157788** schemas. But if this is a write operation, start writes on all
157789** schemas.
157790**
157791** This is used by the (built-in) sqlite_dbpage virtual table.
157792*/
157793SQLITE_PRIVATEstatic void sqlite3VtabUsesAllSchemas(sqlite3_index_info *pIdxInfo){
157794 HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
157795 Parse *pParse = pHidden->pParse;
157796 int nDb = pParse->db->nDb;
157797 int i;
157798 for(i=0; i<nDb; i++){
157799 sqlite3CodeVerifySchema(pParse, i);
157800 }
157801 if( pParse->writeMask ){
157802 for(i=0; i<nDb; i++){
157803 sqlite3BeginWriteOperation(pParse, 0, i);
157804 }
157805 }
157806}
157807#endif
157808
157809/*
157810** Add all WhereLoop objects for a table of the join identified by
157811** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
157812**
157813** If there are no LEFT or CROSS JOIN joins in the query, both mPrereq and
157814** mUnusable are set to 0. Otherwise, mPrereq is a mask of all FROM clause
157815** entries that occur before the virtual table in the FROM clause and are
157816** separated from it by at least one LEFT or CROSS JOIN. Similarly, the
157817** mUnusable mask contains all FROM clause entries that occur after the
157818** virtual table and are separated from it by at least one LEFT or
157819** CROSS JOIN.
157820**
157821** For example, if the query were:
157822**
157823** ... FROM t1, t2 LEFT JOIN t3, t4, vt CROSS JOIN t5, t6;
157824**
157825** then mPrereq corresponds to (t1, t2) and mUnusable to (t5, t6).
157826**
157827** All the tables in mPrereq must be scanned before the current virtual
157828** table. So any terms for which all prerequisites are satisfied by
157829** mPrereq may be specified as "usable" in all calls to xBestIndex.
157830** Conversely, all tables in mUnusable must be scanned after the current
157831** virtual table, so any terms for which the prerequisites overlap with
157832** mUnusable should always be configured as "not-usable" for xBestIndex.
157833*/
157834static int whereLoopAddVirtual(
157835 WhereLoopBuilder *pBuilder, /* WHERE clause information */
157836 Bitmask mPrereq, /* Tables that must be scanned before this one */
157837 Bitmask mUnusable /* Tables that must be scanned after this one */
157838){
157839 int rc = SQLITE_OK0; /* Return code */
157840 WhereInfo *pWInfo; /* WHERE analysis context */
157841 Parse *pParse; /* The parsing context */
157842 WhereClause *pWC; /* The WHERE clause */
157843 SrcItem *pSrc; /* The FROM clause term to search */
157844 sqlite3_index_info *p; /* Object to pass to xBestIndex() */
157845 int nConstraint; /* Number of constraints in p */
157846 int bIn; /* True if plan uses IN(...) operator */
157847 WhereLoop *pNew;
157848 Bitmask mBest; /* Tables used by best possible plan */
157849 u16 mNoOmit;
157850 int bRetry = 0; /* True to retry with LIMIT/OFFSET disabled */
157851
157852 assert( (mPrereq & mUnusable)==0 )((void) (0));
157853 pWInfo = pBuilder->pWInfo;
157854 pParse = pWInfo->pParse;
157855 pWC = pBuilder->pWC;
157856 pNew = pBuilder->pNew;
157857 pSrc = &pWInfo->pTabList->a[pNew->iTab];
157858 assert( IsVirtual(pSrc->pTab) )((void) (0));
157859 p = allocateIndexInfo(pWInfo, pWC, mUnusable, pSrc, &mNoOmit);
157860 if( p==0 ) return SQLITE_NOMEM_BKPT7;
157861 pNew->rSetup = 0;
157862 pNew->wsFlags = WHERE_VIRTUALTABLE0x00000400;
157863 pNew->nLTerm = 0;
157864 pNew->u.vtab.needFree = 0;
157865 nConstraint = p->nConstraint;
157866 if( whereLoopResize(pParse->db, pNew, nConstraint) ){
157867 freeIndexInfo(pParse->db, p);
157868 return SQLITE_NOMEM_BKPT7;
157869 }
157870
157871 /* First call xBestIndex() with all constraints usable. */
157872 WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
157873 WHERETRACE(0x40, (" VirtualOne: all usable\n"));
157874 rc = whereLoopAddVirtualOne(
157875 pBuilder, mPrereq, ALLBITS((Bitmask)-1), 0, p, mNoOmit, &bIn, &bRetry
157876 );
157877 if( bRetry ){
157878 assert( rc==SQLITE_OK )((void) (0));
157879 rc = whereLoopAddVirtualOne(
157880 pBuilder, mPrereq, ALLBITS((Bitmask)-1), 0, p, mNoOmit, &bIn, 0
157881 );
157882 }
157883
157884 /* If the call to xBestIndex() with all terms enabled produced a plan
157885 ** that does not require any source tables (IOW: a plan with mBest==0)
157886 ** and does not use an IN(...) operator, then there is no point in making
157887 ** any further calls to xBestIndex() since they will all return the same
157888 ** result (if the xBestIndex() implementation is sane). */
157889 if( rc==SQLITE_OK0 && ((mBest = (pNew->prereq & ~mPrereq))!=0 || bIn) ){
157890 int seenZero = 0; /* True if a plan with no prereqs seen */
157891 int seenZeroNoIN = 0; /* Plan with no prereqs and no IN(...) seen */
157892 Bitmask mPrev = 0;
157893 Bitmask mBestNoIn = 0;
157894
157895 /* If the plan produced by the earlier call uses an IN(...) term, call
157896 ** xBestIndex again, this time with IN(...) terms disabled. */
157897 if( bIn ){
157898 WHERETRACE(0x40, (" VirtualOne: all usable w/o IN\n"));
157899 rc = whereLoopAddVirtualOne(
157900 pBuilder, mPrereq, ALLBITS((Bitmask)-1), WO_IN0x0001, p, mNoOmit, &bIn, 0);
157901 assert( bIn==0 )((void) (0));
157902 mBestNoIn = pNew->prereq & ~mPrereq;
157903 if( mBestNoIn==0 ){
157904 seenZero = 1;
157905 seenZeroNoIN = 1;
157906 }
157907 }
157908
157909 /* Call xBestIndex once for each distinct value of (prereqRight & ~mPrereq)
157910 ** in the set of terms that apply to the current virtual table. */
157911 while( rc==SQLITE_OK0 ){
157912 int i;
157913 Bitmask mNext = ALLBITS((Bitmask)-1);
157914 assert( mNext>0 )((void) (0));
157915 for(i=0; i<nConstraint; i++){
157916 Bitmask mThis = (
157917 pWC->a[p->aConstraint[i].iTermOffset].prereqRight & ~mPrereq
157918 );
157919 if( mThis>mPrev && mThis<mNext ) mNext = mThis;
157920 }
157921 mPrev = mNext;
157922 if( mNext==ALLBITS((Bitmask)-1) ) break;
157923 if( mNext==mBest || mNext==mBestNoIn ) continue;
157924 WHERETRACE(0x40, (" VirtualOne: mPrev=%04llx mNext=%04llx\n",
157925 (sqlite3_uint64)mPrev, (sqlite3_uint64)mNext));
157926 rc = whereLoopAddVirtualOne(
157927 pBuilder, mPrereq, mNext|mPrereq, 0, p, mNoOmit, &bIn, 0);
157928 if( pNew->prereq==mPrereq ){
157929 seenZero = 1;
157930 if( bIn==0 ) seenZeroNoIN = 1;
157931 }
157932 }
157933
157934 /* If the calls to xBestIndex() in the above loop did not find a plan
157935 ** that requires no source tables at all (i.e. one guaranteed to be
157936 ** usable), make a call here with all source tables disabled */
157937 if( rc==SQLITE_OK0 && seenZero==0 ){
157938 WHERETRACE(0x40, (" VirtualOne: all disabled\n"));
157939 rc = whereLoopAddVirtualOne(
157940 pBuilder, mPrereq, mPrereq, 0, p, mNoOmit, &bIn, 0);
157941 if( bIn==0 ) seenZeroNoIN = 1;
157942 }
157943
157944 /* If the calls to xBestIndex() have so far failed to find a plan
157945 ** that requires no source tables at all and does not use an IN(...)
157946 ** operator, make a final call to obtain one here. */
157947 if( rc==SQLITE_OK0 && seenZeroNoIN==0 ){
157948 WHERETRACE(0x40, (" VirtualOne: all disabled and w/o IN\n"));
157949 rc = whereLoopAddVirtualOne(
157950 pBuilder, mPrereq, mPrereq, WO_IN0x0001, p, mNoOmit, &bIn, 0);
157951 }
157952 }
157953
157954 if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr);
157955 freeIndexInfo(pParse->db, p);
157956 WHERETRACE(0x800, ("END %s.addVirtual(), rc=%d\n", pSrc->pTab->zName, rc));
157957 return rc;
157958}
157959#endif /* SQLITE_OMIT_VIRTUALTABLE */
157960
157961/*
157962** Add WhereLoop entries to handle OR terms. This works for either
157963** btrees or virtual tables.
157964*/
157965static int whereLoopAddOr(
157966 WhereLoopBuilder *pBuilder,
157967 Bitmask mPrereq,
157968 Bitmask mUnusable
157969){
157970 WhereInfo *pWInfo = pBuilder->pWInfo;
157971 WhereClause *pWC;
157972 WhereLoop *pNew;
157973 WhereTerm *pTerm, *pWCEnd;
157974 int rc = SQLITE_OK0;
157975 int iCur;
157976 WhereClause tempWC;
157977 WhereLoopBuilder sSubBuild;
157978 WhereOrSet sSum, sCur;
157979 SrcItem *pItem;
157980
157981 pWC = pBuilder->pWC;
157982 pWCEnd = pWC->a + pWC->nTerm;
157983 pNew = pBuilder->pNew;
157984 memset(&sSum, 0, sizeof(sSum));
157985 pItem = pWInfo->pTabList->a + pNew->iTab;
157986 iCur = pItem->iCursor;
157987
157988 /* The multi-index OR optimization does not work for RIGHT and FULL JOIN */
157989 if( pItem->fg.jointype & JT_RIGHT0x10 ) return SQLITE_OK0;
157990
157991 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK0; pTerm++){
157992 if( (pTerm->eOperator & WO_OR0x0200)!=0
157993 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
157994 ){
157995 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
157996 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
157997 WhereTerm *pOrTerm;
157998 int once = 1;
157999 int i, j;
158000
158001 sSubBuild = *pBuilder;
158002 sSubBuild.pOrSet = &sCur;
158003
158004 WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm));
158005 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
158006 if( (pOrTerm->eOperator & WO_AND0x0400)!=0 ){
158007 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
158008 }else if( pOrTerm->leftCursor==iCur ){
158009 tempWC.pWInfo = pWC->pWInfo;
158010 tempWC.pOuter = pWC;
158011 tempWC.op = TK_AND44;
158012 tempWC.nTerm = 1;
158013 tempWC.nBase = 1;
158014 tempWC.a = pOrTerm;
158015 sSubBuild.pWC = &tempWC;
158016 }else{
158017 continue;
158018 }
158019 sCur.n = 0;
158020#ifdef WHERETRACE_ENABLED
158021 WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n",
158022 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
158023 if( sqlite3WhereTrace & 0x400 ){
158024 sqlite3WhereClausePrint(sSubBuild.pWC);
158025 }
158026#endif
158027#ifndef SQLITE_OMIT_VIRTUALTABLE
158028 if( IsVirtual(pItem->pTab)((pItem->pTab)->eTabType==1) ){
158029 rc = whereLoopAddVirtual(&sSubBuild, mPrereq, mUnusable);
158030 }else
158031#endif
158032 {
158033 rc = whereLoopAddBtree(&sSubBuild, mPrereq);
158034 }
158035 if( rc==SQLITE_OK0 ){
158036 rc = whereLoopAddOr(&sSubBuild, mPrereq, mUnusable);
158037 }
158038 assert( rc==SQLITE_OK || rc==SQLITE_DONE || sCur.n==0((void) (0))
158039 || rc==SQLITE_NOMEM )((void) (0));
158040 testcase( rc==SQLITE_NOMEM && sCur.n>0 );
158041 testcase( rc==SQLITE_DONE );
158042 if( sCur.n==0 ){
158043 sSum.n = 0;
158044 break;
158045 }else if( once ){
158046 whereOrMove(&sSum, &sCur);
158047 once = 0;
158048 }else{
158049 WhereOrSet sPrev;
158050 whereOrMove(&sPrev, &sSum);
158051 sSum.n = 0;
158052 for(i=0; i<sPrev.n; i++){
158053 for(j=0; j<sCur.n; j++){
158054 whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq,
158055 sqlite3LogEstAdd(sPrev.a[i].rRun, sCur.a[j].rRun),
158056 sqlite3LogEstAdd(sPrev.a[i].nOut, sCur.a[j].nOut));
158057 }
158058 }
158059 }
158060 }
158061 pNew->nLTerm = 1;
158062 pNew->aLTerm[0] = pTerm;
158063 pNew->wsFlags = WHERE_MULTI_OR0x00002000;
158064 pNew->rSetup = 0;
158065 pNew->iSortIdx = 0;
158066 memset(&pNew->u, 0, sizeof(pNew->u));
158067 for(i=0; rc==SQLITE_OK0 && i<sSum.n; i++){
158068 /* TUNING: Currently sSum.a[i].rRun is set to the sum of the costs
158069 ** of all sub-scans required by the OR-scan. However, due to rounding
158070 ** errors, it may be that the cost of the OR-scan is equal to its
158071 ** most expensive sub-scan. Add the smallest possible penalty
158072 ** (equivalent to multiplying the cost by 1.07) to ensure that
158073 ** this does not happen. Otherwise, for WHERE clauses such as the
158074 ** following where there is an index on "y":
158075 **
158076 ** WHERE likelihood(x=?, 0.99) OR y=?
158077 **
158078 ** the planner may elect to "OR" together a full-table scan and an
158079 ** index lookup. And other similarly odd results. */
158080 pNew->rRun = sSum.a[i].rRun + 1;
158081 pNew->nOut = sSum.a[i].nOut;
158082 pNew->prereq = sSum.a[i].prereq;
158083 rc = whereLoopInsert(pBuilder, pNew);
158084 }
158085 WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm));
158086 }
158087 }
158088 return rc;
158089}
158090
158091/*
158092** Add all WhereLoop objects for all tables
158093*/
158094static int whereLoopAddAll(WhereLoopBuilder *pBuilder){
158095 WhereInfo *pWInfo = pBuilder->pWInfo;
158096 Bitmask mPrereq = 0;
158097 Bitmask mPrior = 0;
158098 int iTab;
158099 SrcList *pTabList = pWInfo->pTabList;
158100 SrcItem *pItem;
158101 SrcItem *pEnd = &pTabList->a[pWInfo->nLevel];
158102 sqlite3 *db = pWInfo->pParse->db;
158103 int rc = SQLITE_OK0;
158104 int bFirstPastRJ = 0;
158105 int hasRightJoin = 0;
158106 WhereLoop *pNew;
158107
158108
158109 /* Loop over the tables in the join, from left to right */
158110 pNew = pBuilder->pNew;
158111 whereLoopInit(pNew);
158112 pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT20000;
158113 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
158114 Bitmask mUnusable = 0;
158115 pNew->iTab = iTab;
158116 pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR1000;
158117 pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
158118 if( bFirstPastRJ
158119 || (pItem->fg.jointype & (JT_OUTER0x20|JT_CROSS0x02|JT_LTORJ0x40))!=0
158120 ){
158121 /* Add prerequisites to prevent reordering of FROM clause terms
158122 ** across CROSS joins and outer joins. The bFirstPastRJ boolean
158123 ** prevents the right operand of a RIGHT JOIN from being swapped with
158124 ** other elements even further to the right.
158125 **
158126 ** The JT_LTORJ case and the hasRightJoin flag work together to
158127 ** prevent FROM-clause terms from moving from the right side of
158128 ** a LEFT JOIN over to the left side of that join if the LEFT JOIN
158129 ** is itself on the left side of a RIGHT JOIN.
158130 */
158131 if( pItem->fg.jointype & JT_LTORJ0x40 ) hasRightJoin = 1;
158132 mPrereq |= mPrior;
158133 bFirstPastRJ = (pItem->fg.jointype & JT_RIGHT0x10)!=0;
158134 }else if( !hasRightJoin ){
158135 mPrereq = 0;
158136 }
158137#ifndef SQLITE_OMIT_VIRTUALTABLE
158138 if( IsVirtual(pItem->pTab)((pItem->pTab)->eTabType==1) ){
158139 SrcItem *p;
158140 for(p=&pItem[1]; p<pEnd; p++){
158141 if( mUnusable || (p->fg.jointype & (JT_OUTER0x20|JT_CROSS0x02)) ){
158142 mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor);
158143 }
158144 }
158145 rc = whereLoopAddVirtual(pBuilder, mPrereq, mUnusable);
158146 }else
158147#endif /* SQLITE_OMIT_VIRTUALTABLE */
158148 {
158149 rc = whereLoopAddBtree(pBuilder, mPrereq);
158150 }
158151 if( rc==SQLITE_OK0 && pBuilder->pWC->hasOr ){
158152 rc = whereLoopAddOr(pBuilder, mPrereq, mUnusable);
158153 }
158154 mPrior |= pNew->maskSelf;
158155 if( rc || db->mallocFailed ){
158156 if( rc==SQLITE_DONE101 ){
158157 /* We hit the query planner search limit set by iPlanLimit */
158158 sqlite3_log(SQLITE_WARNING28, "abbreviated query algorithm search");
158159 rc = SQLITE_OK0;
158160 }else{
158161 break;
158162 }
158163 }
158164 }
158165
158166 whereLoopClear(db, pNew);
158167 return rc;
158168}
158169
158170/*
158171** Examine a WherePath (with the addition of the extra WhereLoop of the 6th
158172** parameters) to see if it outputs rows in the requested ORDER BY
158173** (or GROUP BY) without requiring a separate sort operation. Return N:
158174**
158175** N>0: N terms of the ORDER BY clause are satisfied
158176** N==0: No terms of the ORDER BY clause are satisfied
158177** N<0: Unknown yet how many terms of ORDER BY might be satisfied.
158178**
158179** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as
158180** strict. With GROUP BY and DISTINCT the only requirement is that
158181** equivalent rows appear immediately adjacent to one another. GROUP BY
158182** and DISTINCT do not require rows to appear in any particular order as long
158183** as equivalent rows are grouped together. Thus for GROUP BY and DISTINCT
158184** the pOrderBy terms can be matched in any order. With ORDER BY, the
158185** pOrderBy terms must be matched in strict left-to-right order.
158186*/
158187static i8 wherePathSatisfiesOrderBy(
158188 WhereInfo *pWInfo, /* The WHERE clause */
158189 ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */
158190 WherePath *pPath, /* The WherePath to check */
158191 u16 wctrlFlags, /* WHERE_GROUPBY or _DISTINCTBY or _ORDERBY_LIMIT */
158192 u16 nLoop, /* Number of entries in pPath->aLoop[] */
158193 WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */
158194 Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */
158195){
158196 u8 revSet; /* True if rev is known */
158197 u8 rev; /* Composite sort order */
158198 u8 revIdx; /* Index sort order */
158199 u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */
158200 u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */
158201 u8 isMatch; /* iColumn matches a term of the ORDER BY clause */
158202 u16 eqOpMask; /* Allowed equality operators */
158203 u16 nKeyCol; /* Number of key columns in pIndex */
158204 u16 nColumn; /* Total number of ordered columns in the index */
158205 u16 nOrderBy; /* Number terms in the ORDER BY clause */
158206 int iLoop; /* Index of WhereLoop in pPath being processed */
158207 int i, j; /* Loop counters */
158208 int iCur; /* Cursor number for current WhereLoop */
158209 int iColumn; /* A column number within table iCur */
158210 WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */
158211 WhereTerm *pTerm; /* A single term of the WHERE clause */
158212 Expr *pOBExpr; /* An expression from the ORDER BY clause */
158213 CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */
158214 Index *pIndex; /* The index associated with pLoop */
158215 sqlite3 *db = pWInfo->pParse->db; /* Database connection */
158216 Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */
158217 Bitmask obDone; /* Mask of all ORDER BY terms */
158218 Bitmask orderDistinctMask; /* Mask of all well-ordered loops */
158219 Bitmask ready; /* Mask of inner loops */
158220
158221 /*
158222 ** We say the WhereLoop is "one-row" if it generates no more than one
158223 ** row of output. A WhereLoop is one-row if all of the following are true:
158224 ** (a) All index columns match with WHERE_COLUMN_EQ.
158225 ** (b) The index is unique
158226 ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
158227 ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
158228 **
158229 ** We say the WhereLoop is "order-distinct" if the set of columns from
158230 ** that WhereLoop that are in the ORDER BY clause are different for every
158231 ** row of the WhereLoop. Every one-row WhereLoop is automatically
158232 ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause
158233 ** is not order-distinct. To be order-distinct is not quite the same as being
158234 ** UNIQUE since a UNIQUE column or index can have multiple rows that
158235 ** are NULL and NULL values are equivalent for the purpose of order-distinct.
158236 ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
158237 **
158238 ** The rowid for a table is always UNIQUE and NOT NULL so whenever the
158239 ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is
158240 ** automatically order-distinct.
158241 */
158242
158243 assert( pOrderBy!=0 )((void) (0));
158244 if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin)(((db)->dbOptFlags&(0x00000040))!=0) ) return 0;
158245
158246 nOrderBy = pOrderBy->nExpr;
158247 testcase( nOrderBy==BMS-1 );
158248 if( nOrderBy>BMS((int)(sizeof(Bitmask)*8))-1 ) return 0; /* Cannot optimize overly large ORDER BYs */
158249 isOrderDistinct = 1;
158250 obDone = MASKBIT(nOrderBy)(((Bitmask)1)<<(nOrderBy))-1;
158251 orderDistinctMask = 0;
158252 ready = 0;
158253 eqOpMask = WO_EQ0x0002 | WO_IS0x0080 | WO_ISNULL0x0100;
158254 if( wctrlFlags & (WHERE_ORDERBY_LIMIT0x0800|WHERE_ORDERBY_MAX0x0002|WHERE_ORDERBY_MIN0x0001) ){
158255 eqOpMask |= WO_IN0x0001;
158256 }
158257 for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){
158258 if( iLoop>0 ) ready |= pLoop->maskSelf;
158259 if( iLoop<nLoop ){
158260 pLoop = pPath->aLoop[iLoop];
158261 if( wctrlFlags & WHERE_ORDERBY_LIMIT0x0800 ) continue;
158262 }else{
158263 pLoop = pLast;
158264 }
158265 if( pLoop->wsFlags & WHERE_VIRTUALTABLE0x00000400 ){
158266 if( pLoop->u.vtab.isOrdered
158267 && ((wctrlFlags&(WHERE_DISTINCTBY0x0080|WHERE_SORTBYGROUP0x0200))!=WHERE_DISTINCTBY0x0080)
158268 ){
158269 obSat = obDone;
158270 }
158271 break;
158272 }else if( wctrlFlags & WHERE_DISTINCTBY0x0080 ){
158273 pLoop->u.btree.nDistinctCol = 0;
158274 }
158275 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
158276
158277 /* Mark off any ORDER BY term X that is a column in the table of
158278 ** the current loop for which there is term in the WHERE
158279 ** clause of the form X IS NULL or X=? that reference only outer
158280 ** loops.
158281 */
158282 for(i=0; i<nOrderBy; i++){
158283 if( MASKBIT(i)(((Bitmask)1)<<(i)) & obSat ) continue;
158284 pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
158285 if( NEVER(pOBExpr==0)(pOBExpr==0) ) continue;
158286 if( pOBExpr->op!=TK_COLUMN167 && pOBExpr->op!=TK_AGG_COLUMN169 ) continue;
158287 if( pOBExpr->iTable!=iCur ) continue;
158288 pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
158289 ~ready, eqOpMask, 0);
158290 if( pTerm==0 ) continue;
158291 if( pTerm->eOperator==WO_IN0x0001 ){
158292 /* IN terms are only valid for sorting in the ORDER BY LIMIT
158293 ** optimization, and then only if they are actually used
158294 ** by the query plan */
158295 assert( wctrlFlags &((void) (0))
158296 (WHERE_ORDERBY_LIMIT|WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX) )((void) (0));
158297 for(j=0; j<pLoop->nLTerm && pTerm!=pLoop->aLTerm[j]; j++){}
158298 if( j>=pLoop->nLTerm ) continue;
158299 }
158300 if( (pTerm->eOperator&(WO_EQ0x0002|WO_IS0x0080))!=0 && pOBExpr->iColumn>=0 ){
158301 Parse *pParse = pWInfo->pParse;
158302 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[i].pExpr);
158303 CollSeq *pColl2 = sqlite3ExprCompareCollSeq(pParse, pTerm->pExpr);
158304 assert( pColl1 )((void) (0));
158305 if( pColl2==0 || sqlite3StrICmp(pColl1->zName, pColl2->zName) ){
158306 continue;
158307 }
158308 testcase( pTerm->pExpr->op==TK_IS );
158309 }
158310 obSat |= MASKBIT(i)(((Bitmask)1)<<(i));
158311 }
158312
158313 if( (pLoop->wsFlags & WHERE_ONEROW0x00001000)==0 ){
158314 if( pLoop->wsFlags & WHERE_IPK0x00000100 ){
158315 pIndex = 0;
158316 nKeyCol = 0;
158317 nColumn = 1;
158318 }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
158319 return 0;
158320 }else{
158321 nKeyCol = pIndex->nKeyCol;
158322 nColumn = pIndex->nColumn;
158323 assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) )((void) (0));
158324 assert( pIndex->aiColumn[nColumn-1]==XN_ROWID((void) (0))
158325 || !HasRowid(pIndex->pTable))((void) (0));
158326 /* All relevant terms of the index must also be non-NULL in order
158327 ** for isOrderDistinct to be true. So the isOrderDistint value
158328 ** computed here might be a false positive. Corrections will be
158329 ** made at tag-20210426-1 below */
158330 isOrderDistinct = IsUniqueIndex(pIndex)((pIndex)->onError!=0)
158331 && (pLoop->wsFlags & WHERE_SKIPSCAN0x00008000)==0;
158332 }
158333
158334 /* Loop through all columns of the index and deal with the ones
158335 ** that are not constrained by == or IN.
158336 */
158337 rev = revSet = 0;
158338 distinctColumns = 0;
158339 for(j=0; j<nColumn; j++){
158340 u8 bOnce = 1; /* True to run the ORDER BY search loop */
158341
158342 assert( j>=pLoop->u.btree.nEq((void) (0))
158343 || (pLoop->aLTerm[j]==0)==(j<pLoop->nSkip)((void) (0))
158344 )((void) (0));
158345 if( j<pLoop->u.btree.nEq && j>=pLoop->nSkip ){
158346 u16 eOp = pLoop->aLTerm[j]->eOperator;
158347
158348 /* Skip over == and IS and ISNULL terms. (Also skip IN terms when
158349 ** doing WHERE_ORDERBY_LIMIT processing). Except, IS and ISNULL
158350 ** terms imply that the index is not UNIQUE NOT NULL in which case
158351 ** the loop need to be marked as not order-distinct because it can
158352 ** have repeated NULL rows.
158353 **
158354 ** If the current term is a column of an ((?,?) IN (SELECT...))
158355 ** expression for which the SELECT returns more than one column,
158356 ** check that it is the only column used by this loop. Otherwise,
158357 ** if it is one of two or more, none of the columns can be
158358 ** considered to match an ORDER BY term.
158359 */
158360 if( (eOp & eqOpMask)!=0 ){
158361 if( eOp & (WO_ISNULL0x0100|WO_IS0x0080) ){
158362 testcase( eOp & WO_ISNULL );
158363 testcase( eOp & WO_IS );
158364 testcase( isOrderDistinct );
158365 isOrderDistinct = 0;
158366 }
158367 continue;
158368 }else if( ALWAYS(eOp & WO_IN)(eOp & 0x0001) ){
158369 /* ALWAYS() justification: eOp is an equality operator due to the
158370 ** j<pLoop->u.btree.nEq constraint above. Any equality other
158371 ** than WO_IN is captured by the previous "if". So this one
158372 ** always has to be WO_IN. */
158373 Expr *pX = pLoop->aLTerm[j]->pExpr;
158374 for(i=j+1; i<pLoop->u.btree.nEq; i++){
158375 if( pLoop->aLTerm[i]->pExpr==pX ){
158376 assert( (pLoop->aLTerm[i]->eOperator & WO_IN) )((void) (0));
158377 bOnce = 0;
158378 break;
158379 }
158380 }
158381 }
158382 }
158383
158384 /* Get the column number in the table (iColumn) and sort order
158385 ** (revIdx) for the j-th column of the index.
158386 */
158387 if( pIndex ){
158388 iColumn = pIndex->aiColumn[j];
158389 revIdx = pIndex->aSortOrder[j] & KEYINFO_ORDER_DESC0x01;
158390 if( iColumn==pIndex->pTable->iPKey ) iColumn = XN_ROWID(-1);
158391 }else{
158392 iColumn = XN_ROWID(-1);
158393 revIdx = 0;
158394 }
158395
158396 /* An unconstrained column that might be NULL means that this
158397 ** WhereLoop is not well-ordered. tag-20210426-1
158398 */
158399 if( isOrderDistinct ){
158400 if( iColumn>=0
158401 && j>=pLoop->u.btree.nEq
158402 && pIndex->pTable->aCol[iColumn].notNull==0
158403 ){
158404 isOrderDistinct = 0;
158405 }
158406 if( iColumn==XN_EXPR(-2) ){
158407 isOrderDistinct = 0;
158408 }
158409 }
158410
158411 /* Find the ORDER BY term that corresponds to the j-th column
158412 ** of the index and mark that ORDER BY term off
158413 */
158414 isMatch = 0;
158415 for(i=0; bOnce && i<nOrderBy; i++){
158416 if( MASKBIT(i)(((Bitmask)1)<<(i)) & obSat ) continue;
158417 pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
158418 testcase( wctrlFlags & WHERE_GROUPBY );
158419 testcase( wctrlFlags & WHERE_DISTINCTBY );
158420 if( NEVER(pOBExpr==0)(pOBExpr==0) ) continue;
158421 if( (wctrlFlags & (WHERE_GROUPBY0x0040|WHERE_DISTINCTBY0x0080))==0 ) bOnce = 0;
158422 if( iColumn>=XN_ROWID(-1) ){
158423 if( pOBExpr->op!=TK_COLUMN167 && pOBExpr->op!=TK_AGG_COLUMN169 ) continue;
158424 if( pOBExpr->iTable!=iCur ) continue;
158425 if( pOBExpr->iColumn!=iColumn ) continue;
158426 }else{
158427 Expr *pIdxExpr = pIndex->aColExpr->a[j].pExpr;
158428 if( sqlite3ExprCompareSkip(pOBExpr, pIdxExpr, iCur) ){
158429 continue;
158430 }
158431 }
158432 if( iColumn!=XN_ROWID(-1) ){
158433 pColl = sqlite3ExprNNCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
158434 if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
158435 }
158436 if( wctrlFlags & WHERE_DISTINCTBY0x0080 ){
158437 pLoop->u.btree.nDistinctCol = j+1;
158438 }
158439 isMatch = 1;
158440 break;
158441 }
158442 if( isMatch && (wctrlFlags & WHERE_GROUPBY0x0040)==0 ){
158443 /* Make sure the sort order is compatible in an ORDER BY clause.
158444 ** Sort order is irrelevant for a GROUP BY clause. */
158445 if( revSet ){
158446 if( (rev ^ revIdx)
158447 != (pOrderBy->a[i].fg.sortFlags&KEYINFO_ORDER_DESC0x01)
158448 ){
158449 isMatch = 0;
158450 }
158451 }else{
158452 rev = revIdx ^ (pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC0x01);
158453 if( rev ) *pRevMask |= MASKBIT(iLoop)(((Bitmask)1)<<(iLoop));
158454 revSet = 1;
158455 }
158456 }
158457 if( isMatch && (pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_BIGNULL0x02) ){
158458 if( j==pLoop->u.btree.nEq ){
158459 pLoop->wsFlags |= WHERE_BIGNULL_SORT0x00080000;
158460 }else{
158461 isMatch = 0;
158462 }
158463 }
158464 if( isMatch ){
158465 if( iColumn==XN_ROWID(-1) ){
158466 testcase( distinctColumns==0 );
158467 distinctColumns = 1;
158468 }
158469 obSat |= MASKBIT(i)(((Bitmask)1)<<(i));
158470 }else{
158471 /* No match found */
158472 if( j==0 || j<nKeyCol ){
158473 testcase( isOrderDistinct!=0 );
158474 isOrderDistinct = 0;
158475 }
158476 break;
158477 }
158478 } /* end Loop over all index columns */
158479 if( distinctColumns ){
158480 testcase( isOrderDistinct==0 );
158481 isOrderDistinct = 1;
158482 }
158483 } /* end-if not one-row */
158484
158485 /* Mark off any other ORDER BY terms that reference pLoop */
158486 if( isOrderDistinct ){
158487 orderDistinctMask |= pLoop->maskSelf;
158488 for(i=0; i<nOrderBy; i++){
158489 Expr *p;
158490 Bitmask mTerm;
158491 if( MASKBIT(i)(((Bitmask)1)<<(i)) & obSat ) continue;
158492 p = pOrderBy->a[i].pExpr;
158493 mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
158494 if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue;
158495 if( (mTerm&~orderDistinctMask)==0 ){
158496 obSat |= MASKBIT(i)(((Bitmask)1)<<(i));
158497 }
158498 }
158499 }
158500 } /* End the loop over all WhereLoops from outer-most down to inner-most */
158501 if( obSat==obDone ) return (i8)nOrderBy;
158502 if( !isOrderDistinct ){
158503 for(i=nOrderBy-1; i>0; i--){
158504 Bitmask m = ALWAYS(i<BMS)(i<((int)(sizeof(Bitmask)*8))) ? MASKBIT(i)(((Bitmask)1)<<(i)) - 1 : 0;
158505 if( (obSat&m)==m ) return i;
158506 }
158507 return 0;
158508 }
158509 return -1;
158510}
158511
158512
158513/*
158514** If the WHERE_GROUPBY flag is set in the mask passed to sqlite3WhereBegin(),
158515** the planner assumes that the specified pOrderBy list is actually a GROUP
158516** BY clause - and so any order that groups rows as required satisfies the
158517** request.
158518**
158519** Normally, in this case it is not possible for the caller to determine
158520** whether or not the rows are really being delivered in sorted order, or
158521** just in some other order that provides the required grouping. However,
158522** if the WHERE_SORTBYGROUP flag is also passed to sqlite3WhereBegin(), then
158523** this function may be called on the returned WhereInfo object. It returns
158524** true if the rows really will be sorted in the specified order, or false
158525** otherwise.
158526**
158527** For example, assuming:
158528**
158529** CREATE INDEX i1 ON t1(x, Y);
158530**
158531** then
158532**
158533** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1
158534** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0
158535*/
158536SQLITE_PRIVATEstatic int sqlite3WhereIsSorted(WhereInfo *pWInfo){
158537 assert( pWInfo->wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY) )((void) (0));
158538 assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP )((void) (0));
158539 return pWInfo->sorted;
158540}
158541
158542#ifdef WHERETRACE_ENABLED
158543/* For debugging use only: */
158544static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){
158545 static char zName[65];
158546 int i;
158547 for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
158548 if( pLast ) zName[i++] = pLast->cId;
158549 zName[i] = 0;
158550 return zName;
158551}
158552#endif
158553
158554/*
158555** Return the cost of sorting nRow rows, assuming that the keys have
158556** nOrderby columns and that the first nSorted columns are already in
158557** order.
158558*/
158559static LogEst whereSortingCost(
158560 WhereInfo *pWInfo,
158561 LogEst nRow,
158562 int nOrderBy,
158563 int nSorted
158564){
158565 /* TUNING: Estimated cost of a full external sort, where N is
158566 ** the number of rows to sort is:
158567 **
158568 ** cost = (3.0 * N * log(N)).
158569 **
158570 ** Or, if the order-by clause has X terms but only the last Y
158571 ** terms are out of order, then block-sorting will reduce the
158572 ** sorting cost to:
158573 **
158574 ** cost = (3.0 * N * log(N)) * (Y/X)
158575 **
158576 ** The (Y/X) term is implemented using stack variable rScale
158577 ** below.
158578 */
158579 LogEst rScale, rSortCost;
158580 assert( nOrderBy>0 && 66==sqlite3LogEst(100) )((void) (0));
158581 rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
158582 rSortCost = nRow + rScale + 16;
158583
158584 /* Multiple by log(M) where M is the number of output rows.
158585 ** Use the LIMIT for M if it is smaller. Or if this sort is for
158586 ** a DISTINCT operator, M will be the number of distinct output
158587 ** rows, so fudge it downwards a bit.
158588 */
158589 if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT0x4000)!=0 && pWInfo->iLimit<nRow ){
158590 nRow = pWInfo->iLimit;
158591 }else if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT0x0100) ){
158592 /* TUNING: In the sort for a DISTINCT operator, assume that the DISTINCT
158593 ** reduces the number of output rows by a factor of 2 */
158594 if( nRow>10 ){ nRow -= 10; assert( 10==sqlite3LogEst(2) )((void) (0)); }
158595 }
158596 rSortCost += estLog(nRow);
158597 return rSortCost;
158598}
158599
158600/*
158601** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
158602** attempts to find the lowest cost path that visits each WhereLoop
158603** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
158604**
158605** Assume that the total number of output rows that will need to be sorted
158606** will be nRowEst (in the 10*log2 representation). Or, ignore sorting
158607** costs if nRowEst==0.
158608**
158609** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation
158610** error occurs.
158611*/
158612static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
158613 int mxChoice; /* Maximum number of simultaneous paths tracked */
158614 int nLoop; /* Number of terms in the join */
158615 Parse *pParse; /* Parsing context */
158616 sqlite3 *db; /* The database connection */
158617 int iLoop; /* Loop counter over the terms of the join */
158618 int ii, jj; /* Loop counters */
158619 int mxI = 0; /* Index of next entry to replace */
158620 int nOrderBy; /* Number of ORDER BY clause terms */
158621 LogEst mxCost = 0; /* Maximum cost of a set of paths */
158622 LogEst mxUnsorted = 0; /* Maximum unsorted cost of a set of path */
158623 int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */
158624 WherePath *aFrom; /* All nFrom paths at the previous level */
158625 WherePath *aTo; /* The nTo best paths at the current level */
158626 WherePath *pFrom; /* An element of aFrom[] that we are working on */
158627 WherePath *pTo; /* An element of aTo[] that we are working on */
158628 WhereLoop *pWLoop; /* One of the WhereLoop objects */
158629 WhereLoop **pX; /* Used to divy up the pSpace memory */
158630 LogEst *aSortCost = 0; /* Sorting and partial sorting costs */
158631 char *pSpace; /* Temporary memory used by this routine */
158632 int nSpace; /* Bytes of space allocated at pSpace */
158633
158634 pParse = pWInfo->pParse;
158635 db = pParse->db;
158636 nLoop = pWInfo->nLevel;
158637 /* TUNING: For simple queries, only the best path is tracked.
158638 ** For 2-way joins, the 5 best paths are followed.
158639 ** For joins of 3 or more tables, track the 10 best paths */
158640 mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10);
158641 assert( nLoop<=pWInfo->pTabList->nSrc )((void) (0));
158642 WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d)\n", nRowEst));
158643
158644 /* If nRowEst is zero and there is an ORDER BY clause, ignore it. In this
158645 ** case the purpose of this call is to estimate the number of rows returned
158646 ** by the overall query. Once this estimate has been obtained, the caller
158647 ** will invoke this function a second time, passing the estimate as the
158648 ** nRowEst parameter. */
158649 if( pWInfo->pOrderBy==0 || nRowEst==0 ){
158650 nOrderBy = 0;
158651 }else{
158652 nOrderBy = pWInfo->pOrderBy->nExpr;
158653 }
158654
158655 /* Allocate and initialize space for aTo, aFrom and aSortCost[] */
158656 nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
158657 nSpace += sizeof(LogEst) * nOrderBy;
158658 pSpace = sqlite3DbMallocRawNN(db, nSpace);
158659 if( pSpace==0 ) return SQLITE_NOMEM_BKPT7;
158660 aTo = (WherePath*)pSpace;
158661 aFrom = aTo+mxChoice;
158662 memset(aFrom, 0, sizeof(aFrom[0]));
158663 pX = (WhereLoop**)(aFrom+mxChoice);
158664 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
158665 pFrom->aLoop = pX;
158666 }
158667 if( nOrderBy ){
158668 /* If there is an ORDER BY clause and it is not being ignored, set up
158669 ** space for the aSortCost[] array. Each element of the aSortCost array
158670 ** is either zero - meaning it has not yet been initialized - or the
158671 ** cost of sorting nRowEst rows of data where the first X terms of
158672 ** the ORDER BY clause are already in order, where X is the array
158673 ** index. */
158674 aSortCost = (LogEst*)pX;
158675 memset(aSortCost, 0, sizeof(LogEst) * nOrderBy);
158676 }
158677 assert( aSortCost==0 || &pSpace[nSpace]==(char*)&aSortCost[nOrderBy] )((void) (0));
158678 assert( aSortCost!=0 || &pSpace[nSpace]==(char*)pX )((void) (0));
158679
158680 /* Seed the search with a single WherePath containing zero WhereLoops.
158681 **
158682 ** TUNING: Do not let the number of iterations go above 28. If the cost
158683 ** of computing an automatic index is not paid back within the first 28
158684 ** rows, then do not use the automatic index. */
158685 aFrom[0].nRow = MIN(pParse->nQueryLoop, 48)((pParse->nQueryLoop)<(48)?(pParse->nQueryLoop):(48)
)
; assert( 48==sqlite3LogEst(28) )((void) (0));
158686 nFrom = 1;
158687 assert( aFrom[0].isOrdered==0 )((void) (0));
158688 if( nOrderBy ){
158689 /* If nLoop is zero, then there are no FROM terms in the query. Since
158690 ** in this case the query may return a maximum of one row, the results
158691 ** are already in the requested order. Set isOrdered to nOrderBy to
158692 ** indicate this. Or, if nLoop is greater than zero, set isOrdered to
158693 ** -1, indicating that the result set may or may not be ordered,
158694 ** depending on the loops added to the current plan. */
158695 aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
158696 }
158697
158698 /* Compute successively longer WherePaths using the previous generation
158699 ** of WherePaths as the basis for the next. Keep track of the mxChoice
158700 ** best paths at each generation */
158701 for(iLoop=0; iLoop<nLoop; iLoop++){
158702 nTo = 0;
158703 for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
158704 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
158705 LogEst nOut; /* Rows visited by (pFrom+pWLoop) */
158706 LogEst rCost; /* Cost of path (pFrom+pWLoop) */
158707 LogEst rUnsorted; /* Unsorted cost of (pFrom+pWLoop) */
158708 i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */
158709 Bitmask maskNew; /* Mask of src visited by (..) */
158710 Bitmask revMask = 0; /* Mask of rev-order loops for (..) */
158711
158712 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
158713 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
158714 if( (pWLoop->wsFlags & WHERE_AUTO_INDEX0x00004000)!=0 && pFrom->nRow<3 ){
158715 /* Do not use an automatic index if the this loop is expected
158716 ** to run less than 1.25 times. It is tempting to also exclude
158717 ** automatic index usage on an outer loop, but sometimes an automatic
158718 ** index is useful in the outer loop of a correlated subquery. */
158719 assert( 10==sqlite3LogEst(2) )((void) (0));
158720 continue;
158721 }
158722
158723 /* At this point, pWLoop is a candidate to be the next loop.
158724 ** Compute its cost */
158725 rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
158726 rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
158727 nOut = pFrom->nRow + pWLoop->nOut;
158728 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
158729 if( isOrdered<0 ){
158730 isOrdered = wherePathSatisfiesOrderBy(pWInfo,
158731 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
158732 iLoop, pWLoop, &revMask);
158733 }else{
158734 revMask = pFrom->revLoop;
158735 }
158736 if( isOrdered>=0 && isOrdered<nOrderBy ){
158737 if( aSortCost[isOrdered]==0 ){
158738 aSortCost[isOrdered] = whereSortingCost(
158739 pWInfo, nRowEst, nOrderBy, isOrdered
158740 );
158741 }
158742 /* TUNING: Add a small extra penalty (5) to sorting as an
158743 ** extra encouragment to the query planner to select a plan
158744 ** where the rows emerge in the correct order without any sorting
158745 ** required. */
158746 rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]) + 5;
158747
158748 WHERETRACE(0x002,
158749 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
158750 aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
158751 rUnsorted, rCost));
158752 }else{
158753 rCost = rUnsorted;
158754 rUnsorted -= 2; /* TUNING: Slight bias in favor of no-sort plans */
158755 }
158756
158757 /* Check to see if pWLoop should be added to the set of
158758 ** mxChoice best-so-far paths.
158759 **
158760 ** First look for an existing path among best-so-far paths
158761 ** that covers the same set of loops and has the same isOrdered
158762 ** setting as the current path candidate.
158763 **
158764 ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
158765 ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
158766 ** of legal values for isOrdered, -1..64.
158767 */
158768 for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){
158769 if( pTo->maskLoop==maskNew
158770 && ((pTo->isOrdered^isOrdered)&0x80)==0
158771 ){
158772 testcase( jj==nTo-1 );
158773 break;
158774 }
158775 }
158776 if( jj>=nTo ){
158777 /* None of the existing best-so-far paths match the candidate. */
158778 if( nTo>=mxChoice
158779 && (rCost>mxCost || (rCost==mxCost && rUnsorted>=mxUnsorted))
158780 ){
158781 /* The current candidate is no better than any of the mxChoice
158782 ** paths currently in the best-so-far buffer. So discard
158783 ** this candidate as not viable. */
158784#ifdef WHERETRACE_ENABLED /* 0x4 */
158785 if( sqlite3WhereTrace&0x4 ){
158786 sqlite3DebugPrintf("Skip %s cost=%-3d,%3d,%3d order=%c\n",
158787 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
158788 isOrdered>=0 ? isOrdered+'0' : '?');
158789 }
158790#endif
158791 continue;
158792 }
158793 /* If we reach this points it means that the new candidate path
158794 ** needs to be added to the set of best-so-far paths. */
158795 if( nTo<mxChoice ){
158796 /* Increase the size of the aTo set by one */
158797 jj = nTo++;
158798 }else{
158799 /* New path replaces the prior worst to keep count below mxChoice */
158800 jj = mxI;
158801 }
158802 pTo = &aTo[jj];
158803#ifdef WHERETRACE_ENABLED /* 0x4 */
158804 if( sqlite3WhereTrace&0x4 ){
158805 sqlite3DebugPrintf("New %s cost=%-3d,%3d,%3d order=%c\n",
158806 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
158807 isOrdered>=0 ? isOrdered+'0' : '?');
158808 }
158809#endif
158810 }else{
158811 /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
158812 ** same set of loops and has the same isOrdered setting as the
158813 ** candidate path. Check to see if the candidate should replace
158814 ** pTo or if the candidate should be skipped.
158815 **
158816 ** The conditional is an expanded vector comparison equivalent to:
158817 ** (pTo->rCost,pTo->nRow,pTo->rUnsorted) <= (rCost,nOut,rUnsorted)
158818 */
158819 if( pTo->rCost<rCost
158820 || (pTo->rCost==rCost
158821 && (pTo->nRow<nOut
158822 || (pTo->nRow==nOut && pTo->rUnsorted<=rUnsorted)
158823 )
158824 )
158825 ){
158826#ifdef WHERETRACE_ENABLED /* 0x4 */
158827 if( sqlite3WhereTrace&0x4 ){
158828 sqlite3DebugPrintf(
158829 "Skip %s cost=%-3d,%3d,%3d order=%c",
158830 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
158831 isOrdered>=0 ? isOrdered+'0' : '?');
158832 sqlite3DebugPrintf(" vs %s cost=%-3d,%3d,%3d order=%c\n",
158833 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
158834 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
158835 }
158836#endif
158837 /* Discard the candidate path from further consideration */
158838 testcase( pTo->rCost==rCost );
158839 continue;
158840 }
158841 testcase( pTo->rCost==rCost+1 );
158842 /* Control reaches here if the candidate path is better than the
158843 ** pTo path. Replace pTo with the candidate. */
158844#ifdef WHERETRACE_ENABLED /* 0x4 */
158845 if( sqlite3WhereTrace&0x4 ){
158846 sqlite3DebugPrintf(
158847 "Update %s cost=%-3d,%3d,%3d order=%c",
158848 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
158849 isOrdered>=0 ? isOrdered+'0' : '?');
158850 sqlite3DebugPrintf(" was %s cost=%-3d,%3d,%3d order=%c\n",
158851 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
158852 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
158853 }
158854#endif
158855 }
158856 /* pWLoop is a winner. Add it to the set of best so far */
158857 pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
158858 pTo->revLoop = revMask;
158859 pTo->nRow = nOut;
158860 pTo->rCost = rCost;
158861 pTo->rUnsorted = rUnsorted;
158862 pTo->isOrdered = isOrdered;
158863 memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
158864 pTo->aLoop[iLoop] = pWLoop;
158865 if( nTo>=mxChoice ){
158866 mxI = 0;
158867 mxCost = aTo[0].rCost;
158868 mxUnsorted = aTo[0].nRow;
158869 for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){
158870 if( pTo->rCost>mxCost
158871 || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted)
158872 ){
158873 mxCost = pTo->rCost;
158874 mxUnsorted = pTo->rUnsorted;
158875 mxI = jj;
158876 }
158877 }
158878 }
158879 }
158880 }
158881
158882#ifdef WHERETRACE_ENABLED /* >=2 */
158883 if( sqlite3WhereTrace & 0x02 ){
158884 sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
158885 for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){
158886 sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
158887 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
158888 pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
158889 if( pTo->isOrdered>0 ){
158890 sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
158891 }else{
158892 sqlite3DebugPrintf("\n");
158893 }
158894 }
158895 }
158896#endif
158897
158898 /* Swap the roles of aFrom and aTo for the next generation */
158899 pFrom = aTo;
158900 aTo = aFrom;
158901 aFrom = pFrom;
158902 nFrom = nTo;
158903 }
158904
158905 if( nFrom==0 ){
158906 sqlite3ErrorMsg(pParse, "no query solution");
158907 sqlite3DbFreeNN(db, pSpace);
158908 return SQLITE_ERROR1;
158909 }
158910
158911 /* Find the lowest cost path. pFrom will be left pointing to that path */
158912 pFrom = aFrom;
158913 for(ii=1; ii<nFrom; ii++){
158914 if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
158915 }
158916 assert( pWInfo->nLevel==nLoop )((void) (0));
158917 /* Load the lowest cost path into pWInfo */
158918 for(iLoop=0; iLoop<nLoop; iLoop++){
158919 WhereLevel *pLevel = pWInfo->a + iLoop;
158920 pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
158921 pLevel->iFrom = pWLoop->iTab;
158922 pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
158923 }
158924 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT0x0100)!=0
158925 && (pWInfo->wctrlFlags & WHERE_DISTINCTBY0x0080)==0
158926 && pWInfo->eDistinct==WHERE_DISTINCT_NOOP0
158927 && nRowEst
158928 ){
158929 Bitmask notUsed;
158930 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
158931 WHERE_DISTINCTBY0x0080, nLoop-1, pFrom->aLoop[nLoop-1], &notUsed);
158932 if( rc==pWInfo->pResultSet->nExpr ){
158933 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED2;
158934 }
158935 }
158936 pWInfo->bOrderedInnerLoop = 0;
158937 if( pWInfo->pOrderBy ){
158938 pWInfo->nOBSat = pFrom->isOrdered;
158939 if( pWInfo->wctrlFlags & WHERE_DISTINCTBY0x0080 ){
158940 if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
158941 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED2;
158942 }
158943 }else{
158944 pWInfo->revMask = pFrom->revLoop;
158945 if( pWInfo->nOBSat<=0 ){
158946 pWInfo->nOBSat = 0;
158947 if( nLoop>0 ){
158948 u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags;
158949 if( (wsFlags & WHERE_ONEROW0x00001000)==0
158950 && (wsFlags&(WHERE_IPK0x00000100|WHERE_COLUMN_IN0x00000004))!=(WHERE_IPK0x00000100|WHERE_COLUMN_IN0x00000004)
158951 ){
158952 Bitmask m = 0;
158953 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, pFrom,
158954 WHERE_ORDERBY_LIMIT0x0800, nLoop-1, pFrom->aLoop[nLoop-1], &m);
158955 testcase( wsFlags & WHERE_IPK );
158956 testcase( wsFlags & WHERE_COLUMN_IN );
158957 if( rc==pWInfo->pOrderBy->nExpr ){
158958 pWInfo->bOrderedInnerLoop = 1;
158959 pWInfo->revMask = m;
158960 }
158961 }
158962 }
158963 }else if( nLoop
158964 && pWInfo->nOBSat==1
158965 && (pWInfo->wctrlFlags & (WHERE_ORDERBY_MIN0x0001|WHERE_ORDERBY_MAX0x0002))!=0
158966 ){
158967 pWInfo->bOrderedInnerLoop = 1;
158968 }
158969 }
158970 if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP0x0200)
158971 && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
158972 ){
158973 Bitmask revMask = 0;
158974 int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy,
158975 pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask
158976 );
158977 assert( pWInfo->sorted==0 )((void) (0));
158978 if( nOrder==pWInfo->pOrderBy->nExpr ){
158979 pWInfo->sorted = 1;
158980 pWInfo->revMask = revMask;
158981 }
158982 }
158983 }
158984
158985
158986 pWInfo->nRowOut = pFrom->nRow;
158987
158988 /* Free temporary memory and return success */
158989 sqlite3DbFreeNN(db, pSpace);
158990 return SQLITE_OK0;
158991}
158992
158993/*
158994** Most queries use only a single table (they are not joins) and have
158995** simple == constraints against indexed fields. This routine attempts
158996** to plan those simple cases using much less ceremony than the
158997** general-purpose query planner, and thereby yield faster sqlite3_prepare()
158998** times for the common case.
158999**
159000** Return non-zero on success, if this query can be handled by this
159001** no-frills query planner. Return zero if this query needs the
159002** general-purpose query planner.
159003*/
159004static int whereShortCut(WhereLoopBuilder *pBuilder){
159005 WhereInfo *pWInfo;
159006 SrcItem *pItem;
159007 WhereClause *pWC;
159008 WhereTerm *pTerm;
159009 WhereLoop *pLoop;
159010 int iCur;
159011 int j;
159012 Table *pTab;
159013 Index *pIdx;
159014 WhereScan scan;
159015
159016 pWInfo = pBuilder->pWInfo;
159017 if( pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE0x0020 ) return 0;
159018 assert( pWInfo->pTabList->nSrc>=1 )((void) (0));
159019 pItem = pWInfo->pTabList->a;
159020 pTab = pItem->pTab;
159021 if( IsVirtual(pTab)((pTab)->eTabType==1) ) return 0;
159022 if( pItem->fg.isIndexedBy || pItem->fg.notIndexed ){
159023 testcase( pItem->fg.isIndexedBy );
159024 testcase( pItem->fg.notIndexed );
159025 return 0;
159026 }
159027 iCur = pItem->iCursor;
159028 pWC = &pWInfo->sWC;
159029 pLoop = pBuilder->pNew;
159030 pLoop->wsFlags = 0;
159031 pLoop->nSkip = 0;
159032 pTerm = whereScanInit(&scan, pWC, iCur, -1, WO_EQ0x0002|WO_IS0x0080, 0);
159033 while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(&scan);
159034 if( pTerm ){
159035 testcase( pTerm->eOperator & WO_IS );
159036 pLoop->wsFlags = WHERE_COLUMN_EQ0x00000001|WHERE_IPK0x00000100|WHERE_ONEROW0x00001000;
159037 pLoop->aLTerm[0] = pTerm;
159038 pLoop->nLTerm = 1;
159039 pLoop->u.btree.nEq = 1;
159040 /* TUNING: Cost of a rowid lookup is 10 */
159041 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
159042 }else{
159043 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
159044 int opMask;
159045 assert( pLoop->aLTermSpace==pLoop->aLTerm )((void) (0));
159046 if( !IsUniqueIndex(pIdx)((pIdx)->onError!=0)
159047 || pIdx->pPartIdxWhere!=0
159048 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)((int)(sizeof(pLoop->aLTermSpace)/sizeof(pLoop->aLTermSpace
[0])))
159049 ) continue;
159050 opMask = pIdx->uniqNotNull ? (WO_EQ0x0002|WO_IS0x0080) : WO_EQ0x0002;
159051 for(j=0; j<pIdx->nKeyCol; j++){
159052 pTerm = whereScanInit(&scan, pWC, iCur, j, opMask, pIdx);
159053 while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(&scan);
159054 if( pTerm==0 ) break;
159055 testcase( pTerm->eOperator & WO_IS );
159056 pLoop->aLTerm[j] = pTerm;
159057 }
159058 if( j!=pIdx->nKeyCol ) continue;
159059 pLoop->wsFlags = WHERE_COLUMN_EQ0x00000001|WHERE_ONEROW0x00001000|WHERE_INDEXED0x00000200;
159060 if( pIdx->isCovering || (pItem->colUsed & pIdx->colNotIdxed)==0 ){
159061 pLoop->wsFlags |= WHERE_IDX_ONLY0x00000040;
159062 }
159063 pLoop->nLTerm = j;
159064 pLoop->u.btree.nEq = j;
159065 pLoop->u.btree.pIndex = pIdx;
159066 /* TUNING: Cost of a unique index lookup is 15 */
159067 pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */
159068 break;
159069 }
159070 }
159071 if( pLoop->wsFlags ){
159072 pLoop->nOut = (LogEst)1;
159073 pWInfo->a[0].pWLoop = pLoop;
159074 assert( pWInfo->sMaskSet.n==1 && iCur==pWInfo->sMaskSet.ix[0] )((void) (0));
159075 pLoop->maskSelf = 1; /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */
159076 pWInfo->a[0].iTabCur = iCur;
159077 pWInfo->nRowOut = 1;
159078 if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr;
159079 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT0x0100 ){
159080 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE1;
159081 }
159082 if( scan.iEquiv>1 ) pLoop->wsFlags |= WHERE_TRANSCONS0x00200000;
159083#ifdef SQLITE_DEBUG
159084 pLoop->cId = '0';
159085#endif
159086#ifdef WHERETRACE_ENABLED
159087 if( sqlite3WhereTrace ){
159088 sqlite3DebugPrintf("whereShortCut() used to compute solution\n");
159089 }
159090#endif
159091 return 1;
159092 }
159093 return 0;
159094}
159095
159096/*
159097** Helper function for exprIsDeterministic().
159098*/
159099static int exprNodeIsDeterministic(Walker *pWalker, Expr *pExpr){
159100 if( pExpr->op==TK_FUNCTION172 && ExprHasProperty(pExpr, EP_ConstFunc)(((pExpr)->flags&(0x100000))!=0)==0 ){
159101 pWalker->eCode = 0;
159102 return WRC_Abort2;
159103 }
159104 return WRC_Continue0;
159105}
159106
159107/*
159108** Return true if the expression contains no non-deterministic SQL
159109** functions. Do not consider non-deterministic SQL functions that are
159110** part of sub-select statements.
159111*/
159112static int exprIsDeterministic(Expr *p){
159113 Walker w;
159114 memset(&w, 0, sizeof(w));
159115 w.eCode = 1;
159116 w.xExprCallback = exprNodeIsDeterministic;
159117 w.xSelectCallback = sqlite3SelectWalkFail;
159118 sqlite3WalkExpr(&w, p);
159119 return w.eCode;
159120}
159121
159122
159123#ifdef WHERETRACE_ENABLED
159124/*
159125** Display all WhereLoops in pWInfo
159126*/
159127static void showAllWhereLoops(WhereInfo *pWInfo, WhereClause *pWC){
159128 if( sqlite3WhereTrace ){ /* Display all of the WhereLoop objects */
159129 WhereLoop *p;
159130 int i;
159131 static const char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
159132 "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
159133 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
159134 p->cId = zLabel[i%(sizeof(zLabel)-1)];
159135 sqlite3WhereLoopPrint(p, pWC);
159136 }
159137 }
159138}
159139# define WHERETRACE_ALL_LOOPS(W,C) showAllWhereLoops(W,C)
159140#else
159141# define WHERETRACE_ALL_LOOPS(W,C)
159142#endif
159143
159144/* Attempt to omit tables from a join that do not affect the result.
159145** For a table to not affect the result, the following must be true:
159146**
159147** 1) The query must not be an aggregate.
159148** 2) The table must be the RHS of a LEFT JOIN.
159149** 3) Either the query must be DISTINCT, or else the ON or USING clause
159150** must contain a constraint that limits the scan of the table to
159151** at most a single row.
159152** 4) The table must not be referenced by any part of the query apart
159153** from its own USING or ON clause.
159154**
159155** For example, given:
159156**
159157** CREATE TABLE t1(ipk INTEGER PRIMARY KEY, v1);
159158** CREATE TABLE t2(ipk INTEGER PRIMARY KEY, v2);
159159** CREATE TABLE t3(ipk INTEGER PRIMARY KEY, v3);
159160**
159161** then table t2 can be omitted from the following:
159162**
159163** SELECT v1, v3 FROM t1
159164** LEFT JOIN t2 ON (t1.ipk=t2.ipk)
159165** LEFT JOIN t3 ON (t1.ipk=t3.ipk)
159166**
159167** or from:
159168**
159169** SELECT DISTINCT v1, v3 FROM t1
159170** LEFT JOIN t2
159171** LEFT JOIN t3 ON (t1.ipk=t3.ipk)
159172*/
159173static SQLITE_NOINLINE__attribute__((noinline)) Bitmask whereOmitNoopJoin(
159174 WhereInfo *pWInfo,
159175 Bitmask notReady
159176){
159177 int i;
159178 Bitmask tabUsed;
159179
159180 /* Preconditions checked by the caller */
159181 assert( pWInfo->nLevel>=2 )((void) (0));
159182 assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_OmitNoopJoin) )((void) (0));
159183
159184 /* These two preconditions checked by the caller combine to guarantee
159185 ** condition (1) of the header comment */
159186 assert( pWInfo->pResultSet!=0 )((void) (0));
159187 assert( 0==(pWInfo->wctrlFlags & WHERE_AGG_DISTINCT) )((void) (0));
159188
159189 tabUsed = sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pResultSet);
159190 if( pWInfo->pOrderBy ){
159191 tabUsed |= sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pOrderBy);
159192 }
159193 for(i=pWInfo->nLevel-1; i>=1; i--){
159194 WhereTerm *pTerm, *pEnd;
159195 SrcItem *pItem;
159196 WhereLoop *pLoop;
159197 pLoop = pWInfo->a[i].pWLoop;
159198 pItem = &pWInfo->pTabList->a[pLoop->iTab];
159199 if( (pItem->fg.jointype & (JT_LEFT0x08|JT_RIGHT0x10))!=JT_LEFT0x08 ) continue;
159200 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT0x0100)==0
159201 && (pLoop->wsFlags & WHERE_ONEROW0x00001000)==0
159202 ){
159203 continue;
159204 }
159205 if( (tabUsed & pLoop->maskSelf)!=0 ) continue;
159206 pEnd = pWInfo->sWC.a + pWInfo->sWC.nTerm;
159207 for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
159208 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
159209 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON)(((pTerm->pExpr)->flags&(0x000001))!=0)
159210 || pTerm->pExpr->w.iJoin!=pItem->iCursor
159211 ){
159212 break;
159213 }
159214 }
159215 }
159216 if( pTerm<pEnd ) continue;
159217 WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
159218 notReady &= ~pLoop->maskSelf;
159219 for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
159220 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
159221 pTerm->wtFlags |= TERM_CODED0x0004;
159222 }
159223 }
159224 if( i!=pWInfo->nLevel-1 ){
159225 int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel);
159226 memmove(&pWInfo->a[i], &pWInfo->a[i+1], nByte);
159227 }
159228 pWInfo->nLevel--;
159229 assert( pWInfo->nLevel>0 )((void) (0));
159230 }
159231 return notReady;
159232}
159233
159234/*
159235** Check to see if there are any SEARCH loops that might benefit from
159236** using a Bloom filter. Consider a Bloom filter if:
159237**
159238** (1) The SEARCH happens more than N times where N is the number
159239** of rows in the table that is being considered for the Bloom
159240** filter.
159241** (2) Some searches are expected to find zero rows. (This is determined
159242** by the WHERE_SELFCULL flag on the term.)
159243** (3) Bloom-filter processing is not disabled. (Checked by the
159244** caller.)
159245** (4) The size of the table being searched is known by ANALYZE.
159246**
159247** This block of code merely checks to see if a Bloom filter would be
159248** appropriate, and if so sets the WHERE_BLOOMFILTER flag on the
159249** WhereLoop. The implementation of the Bloom filter comes further
159250** down where the code for each WhereLoop is generated.
159251*/
159252static SQLITE_NOINLINE__attribute__((noinline)) void whereCheckIfBloomFilterIsUseful(
159253 const WhereInfo *pWInfo
159254){
159255 int i;
159256 LogEst nSearch;
159257
159258 assert( pWInfo->nLevel>=2 )((void) (0));
159259 assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_BloomFilter) )((void) (0));
159260 nSearch = pWInfo->a[0].pWLoop->nOut;
159261 for(i=1; i<pWInfo->nLevel; i++){
159262 WhereLoop *pLoop = pWInfo->a[i].pWLoop;
159263 const unsigned int reqFlags = (WHERE_SELFCULL0x00800000|WHERE_COLUMN_EQ0x00000001);
159264 if( (pLoop->wsFlags & reqFlags)==reqFlags
159265 /* vvvvvv--- Always the case if WHERE_COLUMN_EQ is defined */
159266 && ALWAYS((pLoop->wsFlags & (WHERE_IPK|WHERE_INDEXED))!=0)((pLoop->wsFlags & (0x00000100|0x00000200))!=0)
159267 ){
159268 SrcItem *pItem = &pWInfo->pTabList->a[pLoop->iTab];
159269 Table *pTab = pItem->pTab;
159270 pTab->tabFlags |= TF_StatsUsed0x00000100;
159271 if( nSearch > pTab->nRowLogEst
159272 && (pTab->tabFlags & TF_HasStat10x00000010)!=0
159273 ){
159274 testcase( pItem->fg.jointype & JT_LEFT );
159275 pLoop->wsFlags |= WHERE_BLOOMFILTER0x00400000;
159276 pLoop->wsFlags &= ~WHERE_IDX_ONLY0x00000040;
159277 WHERETRACE(0xffff, (
159278 "-> use Bloom-filter on loop %c because there are ~%.1e "
159279 "lookups into %s which has only ~%.1e rows\n",
159280 pLoop->cId, (double)sqlite3LogEstToInt(nSearch), pTab->zName,
159281 (double)sqlite3LogEstToInt(pTab->nRowLogEst)));
159282 }
159283 }
159284 nSearch += pLoop->nOut;
159285 }
159286}
159287
159288/*
159289** Generate the beginning of the loop used for WHERE clause processing.
159290** The return value is a pointer to an opaque structure that contains
159291** information needed to terminate the loop. Later, the calling routine
159292** should invoke sqlite3WhereEnd() with the return value of this function
159293** in order to complete the WHERE clause processing.
159294**
159295** If an error occurs, this routine returns NULL.
159296**
159297** The basic idea is to do a nested loop, one loop for each table in
159298** the FROM clause of a select. (INSERT and UPDATE statements are the
159299** same as a SELECT with only a single table in the FROM clause.) For
159300** example, if the SQL is this:
159301**
159302** SELECT * FROM t1, t2, t3 WHERE ...;
159303**
159304** Then the code generated is conceptually like the following:
159305**
159306** foreach row1 in t1 do \ Code generated
159307** foreach row2 in t2 do |-- by sqlite3WhereBegin()
159308** foreach row3 in t3 do /
159309** ...
159310** end \ Code generated
159311** end |-- by sqlite3WhereEnd()
159312** end /
159313**
159314** Note that the loops might not be nested in the order in which they
159315** appear in the FROM clause if a different order is better able to make
159316** use of indices. Note also that when the IN operator appears in
159317** the WHERE clause, it might result in additional nested loops for
159318** scanning through all values on the right-hand side of the IN.
159319**
159320** There are Btree cursors associated with each table. t1 uses cursor
159321** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
159322** And so forth. This routine generates code to open those VDBE cursors
159323** and sqlite3WhereEnd() generates the code to close them.
159324**
159325** The code that sqlite3WhereBegin() generates leaves the cursors named
159326** in pTabList pointing at their appropriate entries. The [...] code
159327** can use OP_Column and OP_Rowid opcodes on these cursors to extract
159328** data from the various tables of the loop.
159329**
159330** If the WHERE clause is empty, the foreach loops must each scan their
159331** entire tables. Thus a three-way join is an O(N^3) operation. But if
159332** the tables have indices and there are terms in the WHERE clause that
159333** refer to those indices, a complete table scan can be avoided and the
159334** code will run much faster. Most of the work of this routine is checking
159335** to see if there are indices that can be used to speed up the loop.
159336**
159337** Terms of the WHERE clause are also used to limit which rows actually
159338** make it to the "..." in the middle of the loop. After each "foreach",
159339** terms of the WHERE clause that use only terms in that loop and outer
159340** loops are evaluated and if false a jump is made around all subsequent
159341** inner loops (or around the "..." if the test occurs within the inner-
159342** most loop)
159343**
159344** OUTER JOINS
159345**
159346** An outer join of tables t1 and t2 is conceptally coded as follows:
159347**
159348** foreach row1 in t1 do
159349** flag = 0
159350** foreach row2 in t2 do
159351** start:
159352** ...
159353** flag = 1
159354** end
159355** if flag==0 then
159356** move the row2 cursor to a null row
159357** goto start
159358** fi
159359** end
159360**
159361** ORDER BY CLAUSE PROCESSING
159362**
159363** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause
159364** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement
159365** if there is one. If there is no ORDER BY clause or if this routine
159366** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
159367**
159368** The iIdxCur parameter is the cursor number of an index. If
159369** WHERE_OR_SUBCLAUSE is set, iIdxCur is the cursor number of an index
159370** to use for OR clause processing. The WHERE clause should use this
159371** specific cursor. If WHERE_ONEPASS_DESIRED is set, then iIdxCur is
159372** the first cursor in an array of cursors for all indices. iIdxCur should
159373** be used to compute the appropriate cursor depending on which index is
159374** used.
159375*/
159376SQLITE_PRIVATEstatic WhereInfo *sqlite3WhereBegin(
159377 Parse *pParse, /* The parser context */
159378 SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */
159379 Expr *pWhere, /* The WHERE clause */
159380 ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */
159381 ExprList *pResultSet, /* Query result set. Req'd for DISTINCT */
159382 Select *pLimit, /* Use this LIMIT/OFFSET clause, if any */
159383 u16 wctrlFlags, /* The WHERE_* flags defined in sqliteInt.h */
159384 int iAuxArg /* If WHERE_OR_SUBCLAUSE is set, index cursor number
159385 ** If WHERE_USE_LIMIT, then the limit amount */
159386){
159387 int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */
159388 int nTabList; /* Number of elements in pTabList */
159389 WhereInfo *pWInfo; /* Will become the return value of this function */
159390 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
159391 Bitmask notReady; /* Cursors that are not yet positioned */
159392 WhereLoopBuilder sWLB; /* The WhereLoop builder */
159393 WhereMaskSet *pMaskSet; /* The expression mask set */
159394 WhereLevel *pLevel; /* A single level in pWInfo->a[] */
159395 WhereLoop *pLoop; /* Pointer to a single WhereLoop object */
159396 int ii; /* Loop counter */
159397 sqlite3 *db; /* Database connection */
159398 int rc; /* Return code */
159399 u8 bFordelete = 0; /* OPFLAG_FORDELETE or zero, as appropriate */
159400
159401 assert( (wctrlFlags & WHERE_ONEPASS_MULTIROW)==0 || (((void) (0))
159402 (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0((void) (0))
159403 && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0((void) (0))
159404 ))((void) (0));
159405
159406 /* Only one of WHERE_OR_SUBCLAUSE or WHERE_USE_LIMIT */
159407 assert( (wctrlFlags & WHERE_OR_SUBCLAUSE)==0((void) (0))
159408 || (wctrlFlags & WHERE_USE_LIMIT)==0 )((void) (0));
159409
159410 /* Variable initialization */
159411 db = pParse->db;
159412 memset(&sWLB, 0, sizeof(sWLB));
159413
159414 /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */
159415 testcase( pOrderBy && pOrderBy->nExpr==BMS-1 );
159416 if( pOrderBy && pOrderBy->nExpr>=BMS((int)(sizeof(Bitmask)*8)) ) pOrderBy = 0;
159417
159418 /* The number of tables in the FROM clause is limited by the number of
159419 ** bits in a Bitmask
159420 */
159421 testcase( pTabList->nSrc==BMS );
159422 if( pTabList->nSrc>BMS((int)(sizeof(Bitmask)*8)) ){
159423 sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS((int)(sizeof(Bitmask)*8)));
159424 return 0;
159425 }
159426
159427 /* This function normally generates a nested loop for all tables in
159428 ** pTabList. But if the WHERE_OR_SUBCLAUSE flag is set, then we should
159429 ** only generate code for the first table in pTabList and assume that
159430 ** any cursors associated with subsequent tables are uninitialized.
159431 */
159432 nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE0x0020) ? 1 : pTabList->nSrc;
159433
159434 /* Allocate and initialize the WhereInfo structure that will become the
159435 ** return value. A single allocation is used to store the WhereInfo
159436 ** struct, the contents of WhereInfo.a[], the WhereClause structure
159437 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
159438 ** field (type Bitmask) it must be aligned on an 8-byte boundary on
159439 ** some architectures. Hence the ROUND8() below.
159440 */
159441 nByteWInfo = ROUND8P(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel))(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
159442 pWInfo = sqlite3DbMallocRawNN(db, nByteWInfo + sizeof(WhereLoop));
159443 if( db->mallocFailed ){
159444 sqlite3DbFree(db, pWInfo);
159445 pWInfo = 0;
159446 goto whereBeginError;
159447 }
159448 pWInfo->pParse = pParse;
159449 pWInfo->pTabList = pTabList;
159450 pWInfo->pOrderBy = pOrderBy;
159451 pWInfo->pWhere = pWhere;
159452 pWInfo->pResultSet = pResultSet;
159453 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
159454 pWInfo->nLevel = nTabList;
159455 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse);
159456 pWInfo->wctrlFlags = wctrlFlags;
159457 pWInfo->iLimit = iAuxArg;
159458 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
159459#ifndef SQLITE_OMIT_VIRTUALTABLE
159460 pWInfo->pLimit = pLimit;
159461#endif
159462 memset(&pWInfo->nOBSat, 0,
159463 offsetof(WhereInfo,sWC)__builtin_offsetof(WhereInfo, sWC) - offsetof(WhereInfo,nOBSat)__builtin_offsetof(WhereInfo, nOBSat));
159464 memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
159465 assert( pWInfo->eOnePass==ONEPASS_OFF )((void) (0)); /* ONEPASS defaults to OFF */
159466 pMaskSet = &pWInfo->sMaskSet;
159467 pMaskSet->n = 0;
159468 pMaskSet->ix[0] = -99; /* Initialize ix[0] to a value that can never be
159469 ** a valid cursor number, to avoid an initial
159470 ** test for pMaskSet->n==0 in sqlite3WhereGetMask() */
159471 sWLB.pWInfo = pWInfo;
159472 sWLB.pWC = &pWInfo->sWC;
159473 sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo);
159474 assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) )((void) (0));
159475 whereLoopInit(sWLB.pNew);
159476#ifdef SQLITE_DEBUG
159477 sWLB.pNew->cId = '*';
159478#endif
159479
159480 /* Split the WHERE clause into separate subexpressions where each
159481 ** subexpression is separated by an AND operator.
159482 */
159483 sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo);
159484 sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND44);
159485
159486 /* Special case: No FROM clause
159487 */
159488 if( nTabList==0 ){
159489 if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
159490 if( (wctrlFlags & WHERE_WANT_DISTINCT0x0100)!=0
159491 && OptimizationEnabled(db, SQLITE_DistinctOpt)(((db)->dbOptFlags&(0x00000010))==0)
159492 ){
159493 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE1;
159494 }
159495 ExplainQueryPlan((pParse, 0, "SCAN CONSTANT ROW"))sqlite3VdbeExplain (pParse, 0, "SCAN CONSTANT ROW");
159496 }else{
159497 /* Assign a bit from the bitmask to every term in the FROM clause.
159498 **
159499 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
159500 **
159501 ** The rule of the previous sentence ensures thta if X is the bitmask for
159502 ** a table T, then X-1 is the bitmask for all other tables to the left of T.
159503 ** Knowing the bitmask for all tables to the left of a left join is
159504 ** important. Ticket #3015.
159505 **
159506 ** Note that bitmasks are created for all pTabList->nSrc tables in
159507 ** pTabList, not just the first nTabList tables. nTabList is normally
159508 ** equal to pTabList->nSrc but might be shortened to 1 if the
159509 ** WHERE_OR_SUBCLAUSE flag is set.
159510 */
159511 ii = 0;
159512 do{
159513 createMask(pMaskSet, pTabList->a[ii].iCursor);
159514 sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC);
159515 }while( (++ii)<pTabList->nSrc );
159516 #ifdef SQLITE_DEBUG
159517 {
159518 Bitmask mx = 0;
159519 for(ii=0; ii<pTabList->nSrc; ii++){
159520 Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
159521 assert( m>=mx )((void) (0));
159522 mx = m;
159523 }
159524 }
159525 #endif
159526 }
159527
159528 /* Analyze all of the subexpressions. */
159529 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
159530 sqlite3WhereAddLimit(&pWInfo->sWC, pLimit);
159531 if( pParse->nErr ) goto whereBeginError;
159532
159533 /* Special case: WHERE terms that do not refer to any tables in the join
159534 ** (constant expressions). Evaluate each such term, and jump over all the
159535 ** generated code if the result is not true.
159536 **
159537 ** Do not do this if the expression contains non-deterministic functions
159538 ** that are not within a sub-select. This is not strictly required, but
159539 ** preserves SQLite's legacy behaviour in the following two cases:
159540 **
159541 ** FROM ... WHERE random()>0; -- eval random() once per row
159542 ** FROM ... WHERE (SELECT random())>0; -- eval random() once overall
159543 */
159544 for(ii=0; ii<sWLB.pWC->nBase; ii++){
159545 WhereTerm *pT = &sWLB.pWC->a[ii];
159546 if( pT->wtFlags & TERM_VIRTUAL0x0002 ) continue;
159547 if( pT->prereqAll==0 && (nTabList==0 || exprIsDeterministic(pT->pExpr)) ){
159548 sqlite3ExprIfFalse(pParse, pT->pExpr, pWInfo->iBreak, SQLITE_JUMPIFNULL0x10);
159549 pT->wtFlags |= TERM_CODED0x0004;
159550 }
159551 }
159552
159553 if( wctrlFlags & WHERE_WANT_DISTINCT0x0100 ){
159554 if( OptimizationDisabled(db, SQLITE_DistinctOpt)(((db)->dbOptFlags&(0x00000010))!=0) ){
159555 /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
159556 ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
159557 wctrlFlags &= ~WHERE_WANT_DISTINCT0x0100;
159558 pWInfo->wctrlFlags &= ~WHERE_WANT_DISTINCT0x0100;
159559 }else if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
159560 /* The DISTINCT marking is pointless. Ignore it. */
159561 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE1;
159562 }else if( pOrderBy==0 ){
159563 /* Try to ORDER BY the result set to make distinct processing easier */
159564 pWInfo->wctrlFlags |= WHERE_DISTINCTBY0x0080;
159565 pWInfo->pOrderBy = pResultSet;
159566 }
159567 }
159568
159569 /* Construct the WhereLoop objects */
159570#if defined(WHERETRACE_ENABLED)
159571 if( sqlite3WhereTrace & 0xffff ){
159572 sqlite3DebugPrintf("*** Optimizer Start *** (wctrlFlags: 0x%x",wctrlFlags);
159573 if( wctrlFlags & WHERE_USE_LIMIT0x4000 ){
159574 sqlite3DebugPrintf(", limit: %d", iAuxArg);
159575 }
159576 sqlite3DebugPrintf(")\n");
159577 if( sqlite3WhereTrace & 0x100 ){
159578 Select sSelect;
159579 memset(&sSelect, 0, sizeof(sSelect));
159580 sSelect.selFlags = SF_WhereBegin0x0080000;
159581 sSelect.pSrc = pTabList;
159582 sSelect.pWhere = pWhere;
159583 sSelect.pOrderBy = pOrderBy;
159584 sSelect.pEList = pResultSet;
159585 sqlite3TreeViewSelect(0, &sSelect, 0);
159586 }
159587 }
159588 if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */
159589 sqlite3DebugPrintf("---- WHERE clause at start of analysis:\n");
159590 sqlite3WhereClausePrint(sWLB.pWC);
159591 }
159592#endif
159593
159594 if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
159595 rc = whereLoopAddAll(&sWLB);
159596 if( rc ) goto whereBeginError;
159597
159598#ifdef SQLITE_ENABLE_STAT4
159599 /* If one or more WhereTerm.truthProb values were used in estimating
159600 ** loop parameters, but then those truthProb values were subsequently
159601 ** changed based on STAT4 information while computing subsequent loops,
159602 ** then we need to rerun the whole loop building process so that all
159603 ** loops will be built using the revised truthProb values. */
159604 if( sWLB.bldFlags2 & SQLITE_BLDF2_2NDPASS0x0004 ){
159605 WHERETRACE_ALL_LOOPS(pWInfo, sWLB.pWC);
159606 WHERETRACE(0xffff,
159607 ("**** Redo all loop computations due to"
159608 " TERM_HIGHTRUTH changes ****\n"));
159609 while( pWInfo->pLoops ){
159610 WhereLoop *p = pWInfo->pLoops;
159611 pWInfo->pLoops = p->pNextLoop;
159612 whereLoopDelete(db, p);
159613 }
159614 rc = whereLoopAddAll(&sWLB);
159615 if( rc ) goto whereBeginError;
159616 }
159617#endif
159618 WHERETRACE_ALL_LOOPS(pWInfo, sWLB.pWC);
159619
159620 wherePathSolver(pWInfo, 0);
159621 if( db->mallocFailed ) goto whereBeginError;
159622 if( pWInfo->pOrderBy ){
159623 wherePathSolver(pWInfo, pWInfo->nRowOut+1);
159624 if( db->mallocFailed ) goto whereBeginError;
159625 }
159626 }
159627 if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder0x00001000)!=0 ){
159628 pWInfo->revMask = ALLBITS((Bitmask)-1);
159629 }
159630 if( pParse->nErr ){
159631 goto whereBeginError;
159632 }
159633 assert( db->mallocFailed==0 )((void) (0));
159634#ifdef WHERETRACE_ENABLED
159635 if( sqlite3WhereTrace ){
159636 sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
159637 if( pWInfo->nOBSat>0 ){
159638 sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
159639 }
159640 switch( pWInfo->eDistinct ){
159641 case WHERE_DISTINCT_UNIQUE1: {
159642 sqlite3DebugPrintf(" DISTINCT=unique");
159643 break;
159644 }
159645 case WHERE_DISTINCT_ORDERED2: {
159646 sqlite3DebugPrintf(" DISTINCT=ordered");
159647 break;
159648 }
159649 case WHERE_DISTINCT_UNORDERED3: {
159650 sqlite3DebugPrintf(" DISTINCT=unordered");
159651 break;
159652 }
159653 }
159654 sqlite3DebugPrintf("\n");
159655 for(ii=0; ii<pWInfo->nLevel; ii++){
159656 sqlite3WhereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
159657 }
159658 }
159659#endif
159660
159661 /* Attempt to omit tables from a join that do not affect the result.
159662 ** See the comment on whereOmitNoopJoin() for further information.
159663 **
159664 ** This query optimization is factored out into a separate "no-inline"
159665 ** procedure to keep the sqlite3WhereBegin() procedure from becoming
159666 ** too large. If sqlite3WhereBegin() becomes too large, that prevents
159667 ** some C-compiler optimizers from in-lining the
159668 ** sqlite3WhereCodeOneLoopStart() procedure, and it is important to
159669 ** in-line sqlite3WhereCodeOneLoopStart() for performance reasons.
159670 */
159671 notReady = ~(Bitmask)0;
159672 if( pWInfo->nLevel>=2
159673 && pResultSet!=0 /* these two combine to guarantee */
159674 && 0==(wctrlFlags & WHERE_AGG_DISTINCT0x0400) /* condition (1) above */
159675 && OptimizationEnabled(db, SQLITE_OmitNoopJoin)(((db)->dbOptFlags&(0x00000100))==0)
159676 ){
159677 notReady = whereOmitNoopJoin(pWInfo, notReady);
159678 nTabList = pWInfo->nLevel;
159679 assert( nTabList>0 )((void) (0));
159680 }
159681
159682 /* Check to see if there are any SEARCH loops that might benefit from
159683 ** using a Bloom filter.
159684 */
159685 if( pWInfo->nLevel>=2
159686 && OptimizationEnabled(db, SQLITE_BloomFilter)(((db)->dbOptFlags&(0x00080000))==0)
159687 ){
159688 whereCheckIfBloomFilterIsUseful(pWInfo);
159689 }
159690
159691#if defined(WHERETRACE_ENABLED)
159692 if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */
159693 sqlite3DebugPrintf("---- WHERE clause at end of analysis:\n");
159694 sqlite3WhereClausePrint(sWLB.pWC);
159695 }
159696 WHERETRACE(0xffff,("*** Optimizer Finished ***\n"));
159697#endif
159698 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
159699
159700 /* If the caller is an UPDATE or DELETE statement that is requesting
159701 ** to use a one-pass algorithm, determine if this is appropriate.
159702 **
159703 ** A one-pass approach can be used if the caller has requested one
159704 ** and either (a) the scan visits at most one row or (b) each
159705 ** of the following are true:
159706 **
159707 ** * the caller has indicated that a one-pass approach can be used
159708 ** with multiple rows (by setting WHERE_ONEPASS_MULTIROW), and
159709 ** * the table is not a virtual table, and
159710 ** * either the scan does not use the OR optimization or the caller
159711 ** is a DELETE operation (WHERE_DUPLICATES_OK is only specified
159712 ** for DELETE).
159713 **
159714 ** The last qualification is because an UPDATE statement uses
159715 ** WhereInfo.aiCurOnePass[1] to determine whether or not it really can
159716 ** use a one-pass approach, and this is not set accurately for scans
159717 ** that use the OR optimization.
159718 */
159719 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 )((void) (0));
159720 if( (wctrlFlags & WHERE_ONEPASS_DESIRED0x0004)!=0 ){
159721 int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
159722 int bOnerow = (wsFlags & WHERE_ONEROW0x00001000)!=0;
159723 assert( !(wsFlags & WHERE_VIRTUALTABLE) || IsVirtual(pTabList->a[0].pTab) )((void) (0));
159724 if( bOnerow || (
159725 0!=(wctrlFlags & WHERE_ONEPASS_MULTIROW0x0008)
159726 && !IsVirtual(pTabList->a[0].pTab)((pTabList->a[0].pTab)->eTabType==1)
159727 && (0==(wsFlags & WHERE_MULTI_OR0x00002000) || (wctrlFlags & WHERE_DUPLICATES_OK0x0010))
159728 )){
159729 pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE1 : ONEPASS_MULTI2;
159730 if( HasRowid(pTabList->a[0].pTab)(((pTabList->a[0].pTab)->tabFlags & 0x00000080)==0) && (wsFlags & WHERE_IDX_ONLY0x00000040) ){
159731 if( wctrlFlags & WHERE_ONEPASS_MULTIROW0x0008 ){
159732 bFordelete = OPFLAG_FORDELETE0x08;
159733 }
159734 pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY0x00000040);
159735 }
159736 }
159737 }
159738
159739 /* Open all tables in the pTabList and any indices selected for
159740 ** searching those tables.
159741 */
159742 for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
159743 Table *pTab; /* Table to open */
159744 int iDb; /* Index of database containing table/index */
159745 SrcItem *pTabItem;
159746
159747 pTabItem = &pTabList->a[pLevel->iFrom];
159748 pTab = pTabItem->pTab;
159749 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
159750 pLoop = pLevel->pWLoop;
159751 if( (pTab->tabFlags & TF_Ephemeral0x00004000)!=0 || IsView(pTab)((pTab)->eTabType==2) ){
159752 /* Do nothing */
159753 }else
159754#ifndef SQLITE_OMIT_VIRTUALTABLE
159755 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE0x00000400)!=0 ){
159756 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
159757 int iCur = pTabItem->iCursor;
159758 sqlite3VdbeAddOp4(v, OP_VOpen173, iCur, 0, 0, pVTab, P4_VTAB(-11));
159759 }else if( IsVirtual(pTab)((pTab)->eTabType==1) ){
159760 /* noop */
159761 }else
159762#endif
159763 if( ((pLoop->wsFlags & WHERE_IDX_ONLY0x00000040)==0
159764 && (wctrlFlags & WHERE_OR_SUBCLAUSE0x0020)==0)
159765 || (pTabItem->fg.jointype & (JT_LTORJ0x40|JT_RIGHT0x10))!=0
159766 ){
159767 int op = OP_OpenRead112;
159768 if( pWInfo->eOnePass!=ONEPASS_OFF0 ){
159769 op = OP_OpenWrite113;
159770 pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
159771 };
159772 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
159773 assert( pTabItem->iCursor==pLevel->iTabCur )((void) (0));
159774 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 );
159775 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS );
159776 if( pWInfo->eOnePass==ONEPASS_OFF0
159777 && pTab->nCol<BMS((int)(sizeof(Bitmask)*8))
159778 && (pTab->tabFlags & (TF_HasGenerated0x00000060|TF_WithoutRowid0x00000080))==0
159779 && (pLoop->wsFlags & (WHERE_AUTO_INDEX0x00004000|WHERE_BLOOMFILTER0x00400000))==0
159780 ){
159781 /* If we know that only a prefix of the record will be used,
159782 ** it is advantageous to reduce the "column count" field in
159783 ** the P4 operand of the OP_OpenRead/Write opcode. */
159784 Bitmask b = pTabItem->colUsed;
159785 int n = 0;
159786 for(; b; b=b>>1, n++){}
159787 sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(n)((void*)(long int)(n)), P4_INT32(-3));
159788 assert( n<=pTab->nCol )((void) (0));
159789 }
159790#ifdef SQLITE_ENABLE_CURSOR_HINTS
159791 if( pLoop->u.btree.pIndex!=0 ){
159792 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ0x02|bFordelete);
159793 }else
159794#endif
159795 {
159796 sqlite3VdbeChangeP5(v, bFordelete);
159797 }
159798#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
159799 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed123, pTabItem->iCursor, 0, 0,
159800 (const u8*)&pTabItem->colUsed, P4_INT64(-13));
159801#endif
159802 }else{
159803 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
159804 }
159805 if( pLoop->wsFlags & WHERE_INDEXED0x00000200 ){
159806 Index *pIx = pLoop->u.btree.pIndex;
159807 int iIndexCur;
159808 int op = OP_OpenRead112;
159809 /* iAuxArg is always set to a positive value if ONEPASS is possible */
159810 assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 )((void) (0));
159811 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) && IsPrimaryKeyIndex(pIx)((pIx)->idxType==2)
159812 && (wctrlFlags & WHERE_OR_SUBCLAUSE0x0020)!=0
159813 ){
159814 /* This is one term of an OR-optimization using the PRIMARY KEY of a
159815 ** WITHOUT ROWID table. No need for a separate index */
159816 iIndexCur = pLevel->iTabCur;
159817 op = 0;
159818 }else if( pWInfo->eOnePass!=ONEPASS_OFF0 ){
159819 Index *pJ = pTabItem->pTab->pIndex;
159820 iIndexCur = iAuxArg;
159821 assert( wctrlFlags & WHERE_ONEPASS_DESIRED )((void) (0));
159822 while( ALWAYS(pJ)(pJ) && pJ!=pIx ){
159823 iIndexCur++;
159824 pJ = pJ->pNext;
159825 }
159826 op = OP_OpenWrite113;
159827 pWInfo->aiCurOnePass[1] = iIndexCur;
159828 }else if( iAuxArg && (wctrlFlags & WHERE_OR_SUBCLAUSE0x0020)!=0 ){
159829 iIndexCur = iAuxArg;
159830 op = OP_ReopenIdx101;
159831 }else{
159832 iIndexCur = pParse->nTab++;
159833 }
159834 pLevel->iIdxCur = iIndexCur;
159835 assert( pIx!=0 )((void) (0));
159836 assert( pIx->pSchema==pTab->pSchema )((void) (0));
159837 assert( iIndexCur>=0 )((void) (0));
159838 if( op ){
159839 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
159840 sqlite3VdbeSetP4KeyInfo(pParse, pIx);
159841 if( (pLoop->wsFlags & WHERE_CONSTRAINT0x0000000f)!=0
159842 && (pLoop->wsFlags & (WHERE_COLUMN_RANGE0x00000002|WHERE_SKIPSCAN0x00008000))==0
159843 && (pLoop->wsFlags & WHERE_BIGNULL_SORT0x00080000)==0
159844 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN0x00100000)==0
159845 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN0x0001)==0
159846 && pWInfo->eDistinct!=WHERE_DISTINCT_ORDERED2
159847 ){
159848 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ0x02);
159849 }
159850 VdbeComment((v, "%s", pIx->zName));
159851#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
159852 {
159853 u64 colUsed = 0;
159854 int ii, jj;
159855 for(ii=0; ii<pIx->nColumn; ii++){
159856 jj = pIx->aiColumn[ii];
159857 if( jj<0 ) continue;
159858 if( jj>63 ) jj = 63;
159859 if( (pTabItem->colUsed & MASKBIT(jj)(((Bitmask)1)<<(jj)))==0 ) continue;
159860 colUsed |= ((u64)1)<<(ii<63 ? ii : 63);
159861 }
159862 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed123, iIndexCur, 0, 0,
159863 (u8*)&colUsed, P4_INT64(-13));
159864 }
159865#endif /* SQLITE_ENABLE_COLUMN_USED_MASK */
159866 }
159867 }
159868 if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
159869 if( (pTabItem->fg.jointype & JT_RIGHT0x10)!=0
159870 && (pLevel->pRJ = sqlite3WhereMalloc(pWInfo, sizeof(WhereRightJoin)))!=0
159871 ){
159872 WhereRightJoin *pRJ = pLevel->pRJ;
159873 pRJ->iMatch = pParse->nTab++;
159874 pRJ->regBloom = ++pParse->nMem;
159875 sqlite3VdbeAddOp2(v, OP_Blob77, 65536, pRJ->regBloom);
159876 pRJ->regReturn = ++pParse->nMem;
159877 sqlite3VdbeAddOp2(v, OP_Null75, 0, pRJ->regReturn);
159878 assert( pTab==pTabItem->pTab )((void) (0));
159879 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
159880 KeyInfo *pInfo;
159881 sqlite3VdbeAddOp2(v, OP_OpenEphemeral118, pRJ->iMatch, 1);
159882 pInfo = sqlite3KeyInfoAlloc(pParse->db, 1, 0);
159883 if( pInfo ){
159884 pInfo->aColl[0] = 0;
159885 pInfo->aSortFlags[0] = 0;
159886 sqlite3VdbeAppendP4(v, pInfo, P4_KEYINFO(-8));
159887 }
159888 }else{
159889 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
159890 sqlite3VdbeAddOp2(v, OP_OpenEphemeral118, pRJ->iMatch, pPk->nKeyCol);
159891 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
159892 }
159893 pLoop->wsFlags &= ~WHERE_IDX_ONLY0x00000040;
159894 /* The nature of RIGHT JOIN processing is such that it messes up
159895 ** the output order. So omit any ORDER BY/GROUP BY elimination
159896 ** optimizations. We need to do an actual sort for RIGHT JOIN. */
159897 pWInfo->nOBSat = 0;
159898 pWInfo->eDistinct = WHERE_DISTINCT_UNORDERED3;
159899 }
159900 }
159901 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
159902 if( db->mallocFailed ) goto whereBeginError;
159903
159904 /* Generate the code to do the search. Each iteration of the for
159905 ** loop below generates code for a single nested loop of the VM
159906 ** program.
159907 */
159908 for(ii=0; ii<nTabList; ii++){
159909 int addrExplain;
159910 int wsFlags;
159911 SrcItem *pSrc;
159912 if( pParse->nErr ) goto whereBeginError;
159913 pLevel = &pWInfo->a[ii];
159914 wsFlags = pLevel->pWLoop->wsFlags;
159915 pSrc = &pTabList->a[pLevel->iFrom];
159916 if( pSrc->fg.isMaterialized ){
159917 if( pSrc->fg.isCorrelated ){
159918 sqlite3VdbeAddOp2(v, OP_Gosub9, pSrc->regReturn, pSrc->addrFillSub);
159919 }else{
159920 int iOnce = sqlite3VdbeAddOp0(v, OP_Once14); VdbeCoverage(v);
159921 sqlite3VdbeAddOp2(v, OP_Gosub9, pSrc->regReturn, pSrc->addrFillSub);
159922 sqlite3VdbeJumpHere(v, iOnce);
159923 }
159924 }
159925 if( (wsFlags & (WHERE_AUTO_INDEX0x00004000|WHERE_BLOOMFILTER0x00400000))!=0 ){
159926 if( (wsFlags & WHERE_AUTO_INDEX0x00004000)!=0 ){
159927#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
159928 constructAutomaticIndex(pParse, &pWInfo->sWC,
159929 &pTabList->a[pLevel->iFrom], notReady, pLevel);
159930#endif
159931 }else{
159932 sqlite3ConstructBloomFilter(pWInfo, ii, pLevel, notReady);
159933 }
159934 if( db->mallocFailed ) goto whereBeginError;
159935 }
159936 addrExplain = sqlite3WhereExplainOneScan(
159937 pParse, pTabList, pLevel, wctrlFlags
159938 );
159939 pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
159940 notReady = sqlite3WhereCodeOneLoopStart(pParse,v,pWInfo,ii,pLevel,notReady);
159941 pWInfo->iContinue = pLevel->addrCont;
159942 if( (wsFlags&WHERE_MULTI_OR0x00002000)==0 && (wctrlFlags&WHERE_OR_SUBCLAUSE0x0020)==0 ){
159943 sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain);
159944 }
159945 }
159946
159947 /* Done. */
159948 VdbeModuleComment((v, "Begin WHERE-core"));
159949 pWInfo->iEndWhere = sqlite3VdbeCurrentAddr(v);
159950 return pWInfo;
159951
159952 /* Jump here if malloc fails */
159953whereBeginError:
159954 if( pWInfo ){
159955 testcase( pWInfo->pExprMods!=0 );
159956 whereUndoExprMods(pWInfo);
159957 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
159958 whereInfoFree(db, pWInfo);
159959 }
159960 return 0;
159961}
159962
159963/*
159964** Part of sqlite3WhereEnd() will rewrite opcodes to reference the
159965** index rather than the main table. In SQLITE_DEBUG mode, we want
159966** to trace those changes if PRAGMA vdbe_addoptrace=on. This routine
159967** does that.
159968*/
159969#ifndef SQLITE_DEBUG
159970# define OpcodeRewriteTrace(D,K,P) /* no-op */
159971#else
159972# define OpcodeRewriteTrace(D,K,P) sqlite3WhereOpcodeRewriteTrace(D,K,P)
159973 static void sqlite3WhereOpcodeRewriteTrace(
159974 sqlite3 *db,
159975 int pc,
159976 VdbeOp *pOp
159977 ){
159978 if( (db->flags & SQLITE_VdbeAddopTrace)==0 ) return;
159979 sqlite3VdbePrintOp(0, pc, pOp);
159980 }
159981#endif
159982
159983#ifdef SQLITE_DEBUG
159984/*
159985** Return true if cursor iCur is opened by instruction k of the
159986** bytecode. Used inside of assert() only.
159987*/
159988static int cursorIsOpen(Vdbe *v, int iCur, int k){
159989 while( k>=0 ){
159990 VdbeOp *pOp = sqlite3VdbeGetOp(v,k--);
159991 if( pOp->p1!=iCur ) continue;
159992 if( pOp->opcode==OP_Close122 ) return 0;
159993 if( pOp->opcode==OP_OpenRead112 ) return 1;
159994 if( pOp->opcode==OP_OpenWrite113 ) return 1;
159995 if( pOp->opcode==OP_OpenDup115 ) return 1;
159996 if( pOp->opcode==OP_OpenAutoindex116 ) return 1;
159997 if( pOp->opcode==OP_OpenEphemeral118 ) return 1;
159998 }
159999 return 0;
160000}
160001#endif /* SQLITE_DEBUG */
160002
160003/*
160004** Generate the end of the WHERE loop. See comments on
160005** sqlite3WhereBegin() for additional information.
160006*/
160007SQLITE_PRIVATEstatic void sqlite3WhereEnd(WhereInfo *pWInfo){
160008 Parse *pParse = pWInfo->pParse;
160009 Vdbe *v = pParse->pVdbe;
160010 int i;
160011 WhereLevel *pLevel;
160012 WhereLoop *pLoop;
160013 SrcList *pTabList = pWInfo->pTabList;
160014 sqlite3 *db = pParse->db;
160015 int iEnd = sqlite3VdbeCurrentAddr(v);
160016 int nRJ = 0;
160017
160018 /* Generate loop termination code.
160019 */
160020 VdbeModuleComment((v, "End WHERE-core"));
160021 for(i=pWInfo->nLevel-1; i>=0; i--){
160022 int addr;
160023 pLevel = &pWInfo->a[i];
160024 if( pLevel->pRJ ){
160025 /* Terminate the subroutine that forms the interior of the loop of
160026 ** the RIGHT JOIN table */
160027 WhereRightJoin *pRJ = pLevel->pRJ;
160028 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
160029 pLevel->addrCont = 0;
160030 pRJ->endSubrtn = sqlite3VdbeCurrentAddr(v);
160031 sqlite3VdbeAddOp3(v, OP_Return67, pRJ->regReturn, pRJ->addrSubrtn, 1);
160032 VdbeCoverage(v);
160033 nRJ++;
160034 }
160035 pLoop = pLevel->pWLoop;
160036 if( pLevel->op!=OP_Noop184 ){
160037#ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
160038 int addrSeek = 0;
160039 Index *pIdx;
160040 int n;
160041 if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED2
160042 && i==pWInfo->nLevel-1 /* Ticket [ef9318757b152e3] 2017-10-21 */
160043 && (pLoop->wsFlags & WHERE_INDEXED0x00000200)!=0
160044 && (pIdx = pLoop->u.btree.pIndex)->hasStat1
160045 && (n = pLoop->u.btree.nDistinctCol)>0
160046 && pIdx->aiRowLogEst[n]>=36
160047 ){
160048 int r1 = pParse->nMem+1;
160049 int j, op;
160050 for(j=0; j<n; j++){
160051 sqlite3VdbeAddOp3(v, OP_Column94, pLevel->iIdxCur, j, r1+j);
160052 }
160053 pParse->nMem += n+1;
160054 op = pLevel->op==OP_Prev37 ? OP_SeekLT20 : OP_SeekGT23;
160055 addrSeek = sqlite3VdbeAddOp4Int(v, op, pLevel->iIdxCur, 0, r1, n);
160056 VdbeCoverageIf(v, op==OP_SeekLT);
160057 VdbeCoverageIf(v, op==OP_SeekGT);
160058 sqlite3VdbeAddOp2(v, OP_Goto8, 1, pLevel->p2);
160059 }
160060#endif /* SQLITE_DISABLE_SKIPAHEAD_DISTINCT */
160061 /* The common case: Advance to the next row */
160062 if( pLevel->addrCont ) sqlite3VdbeResolveLabel(v, pLevel->addrCont);
160063 sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
160064 sqlite3VdbeChangeP5(v, pLevel->p5);
160065 VdbeCoverage(v);
160066 VdbeCoverageIf(v, pLevel->op==OP_Next);
160067 VdbeCoverageIf(v, pLevel->op==OP_Prev);
160068 VdbeCoverageIf(v, pLevel->op==OP_VNext);
160069 if( pLevel->regBignull ){
160070 sqlite3VdbeResolveLabel(v, pLevel->addrBignull);
160071 sqlite3VdbeAddOp2(v, OP_DecrJumpZero60, pLevel->regBignull, pLevel->p2-1);
160072 VdbeCoverage(v);
160073 }
160074#ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
160075 if( addrSeek ) sqlite3VdbeJumpHere(v, addrSeek);
160076#endif
160077 }else if( pLevel->addrCont ){
160078 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
160079 }
160080 if( (pLoop->wsFlags & WHERE_IN_ABLE0x00000800)!=0 && pLevel->u.in.nIn>0 ){
160081 struct InLoop *pIn;
160082 int j;
160083 sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
160084 for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
160085 assert( sqlite3VdbeGetOp(v, pIn->addrInTop+1)->opcode==OP_IsNull((void) (0))
160086 || pParse->db->mallocFailed )((void) (0));
160087 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
160088 if( pIn->eEndLoopOp!=OP_Noop184 ){
160089 if( pIn->nPrefix ){
160090 int bEarlyOut =
160091 (pLoop->wsFlags & WHERE_VIRTUALTABLE0x00000400)==0
160092 && (pLoop->wsFlags & WHERE_IN_EARLYOUT0x00040000)!=0;
160093 if( pLevel->iLeftJoin ){
160094 /* For LEFT JOIN queries, cursor pIn->iCur may not have been
160095 ** opened yet. This occurs for WHERE clauses such as
160096 ** "a = ? AND b IN (...)", where the index is on (a, b). If
160097 ** the RHS of the (a=?) is NULL, then the "b IN (...)" may
160098 ** never have been coded, but the body of the loop run to
160099 ** return the null-row. So, if the cursor is not open yet,
160100 ** jump over the OP_Next or OP_Prev instruction about to
160101 ** be coded. */
160102 sqlite3VdbeAddOp2(v, OP_IfNotOpen24, pIn->iCur,
160103 sqlite3VdbeCurrentAddr(v) + 2 + bEarlyOut);
160104 VdbeCoverage(v);
160105 }
160106 if( bEarlyOut ){
160107 sqlite3VdbeAddOp4Int(v, OP_IfNoHope25, pLevel->iIdxCur,
160108 sqlite3VdbeCurrentAddr(v)+2,
160109 pIn->iBase, pIn->nPrefix);
160110 VdbeCoverage(v);
160111 /* Retarget the OP_IsNull against the left operand of IN so
160112 ** it jumps past the OP_IfNoHope. This is because the
160113 ** OP_IsNull also bypasses the OP_Affinity opcode that is
160114 ** required by OP_IfNoHope. */
160115 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
160116 }
160117 }
160118 sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
160119 VdbeCoverage(v);
160120 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev);
160121 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Next);
160122 }
160123 sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
160124 }
160125 }
160126 sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
160127 if( pLevel->pRJ ){
160128 sqlite3VdbeAddOp3(v, OP_Return67, pLevel->pRJ->regReturn, 0, 1);
160129 VdbeCoverage(v);
160130 }
160131 if( pLevel->addrSkip ){
160132 sqlite3VdbeGoto(v, pLevel->addrSkip);
160133 VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
160134 sqlite3VdbeJumpHere(v, pLevel->addrSkip);
160135 sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
160136 }
160137#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
160138 if( pLevel->addrLikeRep ){
160139 sqlite3VdbeAddOp2(v, OP_DecrJumpZero60, (int)(pLevel->iLikeRepCntr>>1),
160140 pLevel->addrLikeRep);
160141 VdbeCoverage(v);
160142 }
160143#endif
160144 if( pLevel->iLeftJoin ){
160145 int ws = pLoop->wsFlags;
160146 addr = sqlite3VdbeAddOp1(v, OP_IfPos49, pLevel->iLeftJoin); VdbeCoverage(v);
160147 assert( (ws & WHERE_IDX_ONLY)==0 || (ws & WHERE_INDEXED)!=0 )((void) (0));
160148 if( (ws & WHERE_IDX_ONLY0x00000040)==0 ){
160149 assert( pLevel->iTabCur==pTabList->a[pLevel->iFrom].iCursor )((void) (0));
160150 sqlite3VdbeAddOp1(v, OP_NullRow136, pLevel->iTabCur);
160151 }
160152 if( (ws & WHERE_INDEXED0x00000200)
160153 || ((ws & WHERE_MULTI_OR0x00002000) && pLevel->u.pCoveringIdx)
160154 ){
160155 if( ws & WHERE_MULTI_OR0x00002000 ){
160156 Index *pIx = pLevel->u.pCoveringIdx;
160157 int iDb = sqlite3SchemaToIndex(db, pIx->pSchema);
160158 sqlite3VdbeAddOp3(v, OP_ReopenIdx101, pLevel->iIdxCur, pIx->tnum, iDb);
160159 sqlite3VdbeSetP4KeyInfo(pParse, pIx);
160160 }
160161 sqlite3VdbeAddOp1(v, OP_NullRow136, pLevel->iIdxCur);
160162 }
160163 if( pLevel->op==OP_Return67 ){
160164 sqlite3VdbeAddOp2(v, OP_Gosub9, pLevel->p1, pLevel->addrFirst);
160165 }else{
160166 sqlite3VdbeGoto(v, pLevel->addrFirst);
160167 }
160168 sqlite3VdbeJumpHere(v, addr);
160169 }
160170 VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
160171 pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
160172 }
160173
160174 assert( pWInfo->nLevel<=pTabList->nSrc )((void) (0));
160175 if( pWInfo->pExprMods ) whereUndoExprMods(pWInfo);
160176 for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
160177 int k, last;
160178 VdbeOp *pOp, *pLastOp;
160179 Index *pIdx = 0;
160180 SrcItem *pTabItem = &pTabList->a[pLevel->iFrom];
160181 Table *pTab = pTabItem->pTab;
160182 assert( pTab!=0 )((void) (0));
160183 pLoop = pLevel->pWLoop;
160184
160185 /* Do RIGHT JOIN processing. Generate code that will output the
160186 ** unmatched rows of the right operand of the RIGHT JOIN with
160187 ** all of the columns of the left operand set to NULL.
160188 */
160189 if( pLevel->pRJ ){
160190 sqlite3WhereRightJoinLoop(pWInfo, i, pLevel);
160191 continue;
160192 }
160193
160194 /* For a co-routine, change all OP_Column references to the table of
160195 ** the co-routine into OP_Copy of result contained in a register.
160196 ** OP_Rowid becomes OP_Null.
160197 */
160198 if( pTabItem->fg.viaCoroutine ){
160199 testcase( pParse->db->mallocFailed );
160200 translateColumnToCopy(pParse, pLevel->addrBody, pLevel->iTabCur,
160201 pTabItem->regResult, 0);
160202 continue;
160203 }
160204
160205 /* If this scan uses an index, make VDBE code substitutions to read data
160206 ** from the index instead of from the table where possible. In some cases
160207 ** this optimization prevents the table from ever being read, which can
160208 ** yield a significant performance boost.
160209 **
160210 ** Calls to the code generator in between sqlite3WhereBegin and
160211 ** sqlite3WhereEnd will have created code that references the table
160212 ** directly. This loop scans all that code looking for opcodes
160213 ** that reference the table and converts them into opcodes that
160214 ** reference the index.
160215 */
160216 if( pLoop->wsFlags & (WHERE_INDEXED0x00000200|WHERE_IDX_ONLY0x00000040) ){
160217 pIdx = pLoop->u.btree.pIndex;
160218 }else if( pLoop->wsFlags & WHERE_MULTI_OR0x00002000 ){
160219 pIdx = pLevel->u.pCoveringIdx;
160220 }
160221 if( pIdx
160222 && !db->mallocFailed
160223 ){
160224 if( pWInfo->eOnePass==ONEPASS_OFF0 || !HasRowid(pIdx->pTable)(((pIdx->pTable)->tabFlags & 0x00000080)==0) ){
160225 last = iEnd;
160226 }else{
160227 last = pWInfo->iEndWhere;
160228 }
160229 k = pLevel->addrBody + 1;
160230#ifdef SQLITE_DEBUG
160231 if( db->flags & SQLITE_VdbeAddopTrace ){
160232 printf("TRANSLATE opcodes in range %d..%d\n", k, last-1);
160233 }
160234 /* Proof that the "+1" on the k value above is safe */
160235 pOp = sqlite3VdbeGetOp(v, k - 1);
160236 assert( pOp->opcode!=OP_Column || pOp->p1!=pLevel->iTabCur )((void) (0));
160237 assert( pOp->opcode!=OP_Rowid || pOp->p1!=pLevel->iTabCur )((void) (0));
160238 assert( pOp->opcode!=OP_IfNullRow || pOp->p1!=pLevel->iTabCur )((void) (0));
160239#endif
160240 pOp = sqlite3VdbeGetOp(v, k);
160241 pLastOp = pOp + (last - k);
160242 assert( pOp<=pLastOp )((void) (0));
160243 do{
160244 if( pOp->p1!=pLevel->iTabCur ){
160245 /* no-op */
160246 }else if( pOp->opcode==OP_Column94
160247#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
160248 || pOp->opcode==OP_Offset93
160249#endif
160250 ){
160251 int x = pOp->p2;
160252 assert( pIdx->pTable==pTab )((void) (0));
160253#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
160254 if( pOp->opcode==OP_Offset93 ){
160255 /* Do not need to translate the column number */
160256 }else
160257#endif
160258 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
160259 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
160260 x = pPk->aiColumn[x];
160261 assert( x>=0 )((void) (0));
160262 }else{
160263 testcase( x!=sqlite3StorageColumnToTable(pTab,x) );
160264 x = sqlite3StorageColumnToTable(pTab,x);
160265 }
160266 x = sqlite3TableColumnToIndex(pIdx, x);
160267 if( x>=0 ){
160268 pOp->p2 = x;
160269 pOp->p1 = pLevel->iIdxCur;
160270 OpcodeRewriteTrace(db, k, pOp);
160271 }else{
160272 /* Unable to translate the table reference into an index
160273 ** reference. Verify that this is harmless - that the
160274 ** table being referenced really is open.
160275 */
160276#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
160277 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0((void) (0))
160278 || cursorIsOpen(v,pOp->p1,k)((void) (0))
160279 || pOp->opcode==OP_Offset((void) (0))
160280 )((void) (0));
160281#else
160282 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0((void) (0))
160283 || cursorIsOpen(v,pOp->p1,k)((void) (0))
160284 )((void) (0));
160285#endif
160286 }
160287 }else if( pOp->opcode==OP_Rowid135 ){
160288 pOp->p1 = pLevel->iIdxCur;
160289 pOp->opcode = OP_IdxRowid142;
160290 OpcodeRewriteTrace(db, k, pOp);
160291 }else if( pOp->opcode==OP_IfNullRow18 ){
160292 pOp->p1 = pLevel->iIdxCur;
160293 OpcodeRewriteTrace(db, k, pOp);
160294 }
160295#ifdef SQLITE_DEBUG
160296 k++;
160297#endif
160298 }while( (++pOp)<pLastOp );
160299#ifdef SQLITE_DEBUG
160300 if( db->flags & SQLITE_VdbeAddopTrace ) printf("TRANSLATE complete\n");
160301#endif
160302 }
160303 }
160304
160305 /* The "break" point is here, just past the end of the outer loop.
160306 ** Set it.
160307 */
160308 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
160309
160310 /* Final cleanup
160311 */
160312 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
160313 whereInfoFree(db, pWInfo);
160314 pParse->withinRJSubrtn -= nRJ;
160315 return;
160316}
160317
160318/************** End of where.c ***********************************************/
160319/************** Begin file window.c ******************************************/
160320/*
160321** 2018 May 08
160322**
160323** The author disclaims copyright to this source code. In place of
160324** a legal notice, here is a blessing:
160325**
160326** May you do good and not evil.
160327** May you find forgiveness for yourself and forgive others.
160328** May you share freely, never taking more than you give.
160329**
160330*************************************************************************
160331*/
160332/* #include "sqliteInt.h" */
160333
160334#ifndef SQLITE_OMIT_WINDOWFUNC
160335
160336/*
160337** SELECT REWRITING
160338**
160339** Any SELECT statement that contains one or more window functions in
160340** either the select list or ORDER BY clause (the only two places window
160341** functions may be used) is transformed by function sqlite3WindowRewrite()
160342** in order to support window function processing. For example, with the
160343** schema:
160344**
160345** CREATE TABLE t1(a, b, c, d, e, f, g);
160346**
160347** the statement:
160348**
160349** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM t1 ORDER BY e;
160350**
160351** is transformed to:
160352**
160353** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM (
160354** SELECT a, e, c, d, b FROM t1 ORDER BY c, d
160355** ) ORDER BY e;
160356**
160357** The flattening optimization is disabled when processing this transformed
160358** SELECT statement. This allows the implementation of the window function
160359** (in this case max()) to process rows sorted in order of (c, d), which
160360** makes things easier for obvious reasons. More generally:
160361**
160362** * FROM, WHERE, GROUP BY and HAVING clauses are all moved to
160363** the sub-query.
160364**
160365** * ORDER BY, LIMIT and OFFSET remain part of the parent query.
160366**
160367** * Terminals from each of the expression trees that make up the
160368** select-list and ORDER BY expressions in the parent query are
160369** selected by the sub-query. For the purposes of the transformation,
160370** terminals are column references and aggregate functions.
160371**
160372** If there is more than one window function in the SELECT that uses
160373** the same window declaration (the OVER bit), then a single scan may
160374** be used to process more than one window function. For example:
160375**
160376** SELECT max(b) OVER (PARTITION BY c ORDER BY d),
160377** min(e) OVER (PARTITION BY c ORDER BY d)
160378** FROM t1;
160379**
160380** is transformed in the same way as the example above. However:
160381**
160382** SELECT max(b) OVER (PARTITION BY c ORDER BY d),
160383** min(e) OVER (PARTITION BY a ORDER BY b)
160384** FROM t1;
160385**
160386** Must be transformed to:
160387**
160388** SELECT max(b) OVER (PARTITION BY c ORDER BY d) FROM (
160389** SELECT e, min(e) OVER (PARTITION BY a ORDER BY b), c, d, b FROM
160390** SELECT a, e, c, d, b FROM t1 ORDER BY a, b
160391** ) ORDER BY c, d
160392** ) ORDER BY e;
160393**
160394** so that both min() and max() may process rows in the order defined by
160395** their respective window declarations.
160396**
160397** INTERFACE WITH SELECT.C
160398**
160399** When processing the rewritten SELECT statement, code in select.c calls
160400** sqlite3WhereBegin() to begin iterating through the results of the
160401** sub-query, which is always implemented as a co-routine. It then calls
160402** sqlite3WindowCodeStep() to process rows and finish the scan by calling
160403** sqlite3WhereEnd().
160404**
160405** sqlite3WindowCodeStep() generates VM code so that, for each row returned
160406** by the sub-query a sub-routine (OP_Gosub) coded by select.c is invoked.
160407** When the sub-routine is invoked:
160408**
160409** * The results of all window-functions for the row are stored
160410** in the associated Window.regResult registers.
160411**
160412** * The required terminal values are stored in the current row of
160413** temp table Window.iEphCsr.
160414**
160415** In some cases, depending on the window frame and the specific window
160416** functions invoked, sqlite3WindowCodeStep() caches each entire partition
160417** in a temp table before returning any rows. In other cases it does not.
160418** This detail is encapsulated within this file, the code generated by
160419** select.c is the same in either case.
160420**
160421** BUILT-IN WINDOW FUNCTIONS
160422**
160423** This implementation features the following built-in window functions:
160424**
160425** row_number()
160426** rank()
160427** dense_rank()
160428** percent_rank()
160429** cume_dist()
160430** ntile(N)
160431** lead(expr [, offset [, default]])
160432** lag(expr [, offset [, default]])
160433** first_value(expr)
160434** last_value(expr)
160435** nth_value(expr, N)
160436**
160437** These are the same built-in window functions supported by Postgres.
160438** Although the behaviour of aggregate window functions (functions that
160439** can be used as either aggregates or window funtions) allows them to
160440** be implemented using an API, built-in window functions are much more
160441** esoteric. Additionally, some window functions (e.g. nth_value())
160442** may only be implemented by caching the entire partition in memory.
160443** As such, some built-in window functions use the same API as aggregate
160444** window functions and some are implemented directly using VDBE
160445** instructions. Additionally, for those functions that use the API, the
160446** window frame is sometimes modified before the SELECT statement is
160447** rewritten. For example, regardless of the specified window frame, the
160448** row_number() function always uses:
160449**
160450** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
160451**
160452** See sqlite3WindowUpdate() for details.
160453**
160454** As well as some of the built-in window functions, aggregate window
160455** functions min() and max() are implemented using VDBE instructions if
160456** the start of the window frame is declared as anything other than
160457** UNBOUNDED PRECEDING.
160458*/
160459
160460/*
160461** Implementation of built-in window function row_number(). Assumes that the
160462** window frame has been coerced to:
160463**
160464** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
160465*/
160466static void row_numberStepFunc(
160467 sqlite3_context *pCtx,
160468 int nArg,
160469 sqlite3_value **apArg
160470){
160471 i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p));
160472 if( p ) (*p)++;
160473 UNUSED_PARAMETER(nArg)(void)(nArg);
160474 UNUSED_PARAMETER(apArg)(void)(apArg);
160475}
160476static void row_numberValueFunc(sqlite3_context *pCtx){
160477 i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p));
160478 sqlite3_result_int64(pCtx, (p ? *p : 0));
160479}
160480
160481/*
160482** Context object type used by rank(), dense_rank(), percent_rank() and
160483** cume_dist().
160484*/
160485struct CallCount {
160486 i64 nValue;
160487 i64 nStep;
160488 i64 nTotal;
160489};
160490
160491/*
160492** Implementation of built-in window function dense_rank(). Assumes that
160493** the window frame has been set to:
160494**
160495** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
160496*/
160497static void dense_rankStepFunc(
160498 sqlite3_context *pCtx,
160499 int nArg,
160500 sqlite3_value **apArg
160501){
160502 struct CallCount *p;
160503 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
160504 if( p ) p->nStep = 1;
160505 UNUSED_PARAMETER(nArg)(void)(nArg);
160506 UNUSED_PARAMETER(apArg)(void)(apArg);
160507}
160508static void dense_rankValueFunc(sqlite3_context *pCtx){
160509 struct CallCount *p;
160510 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
160511 if( p ){
160512 if( p->nStep ){
160513 p->nValue++;
160514 p->nStep = 0;
160515 }
160516 sqlite3_result_int64(pCtx, p->nValue);
160517 }
160518}
160519
160520/*
160521** Implementation of built-in window function nth_value(). This
160522** implementation is used in "slow mode" only - when the EXCLUDE clause
160523** is not set to the default value "NO OTHERS".
160524*/
160525struct NthValueCtx {
160526 i64 nStep;
160527 sqlite3_value *pValue;
160528};
160529static void nth_valueStepFunc(
160530 sqlite3_context *pCtx,
160531 int nArg,
160532 sqlite3_value **apArg
160533){
160534 struct NthValueCtx *p;
160535 p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
160536 if( p ){
160537 i64 iVal;
160538 switch( sqlite3_value_numeric_type(apArg[1]) ){
160539 case SQLITE_INTEGER1:
160540 iVal = sqlite3_value_int64(apArg[1]);
160541 break;
160542 case SQLITE_FLOAT2: {
160543 double fVal = sqlite3_value_double(apArg[1]);
160544 if( ((i64)fVal)!=fVal ) goto error_out;
160545 iVal = (i64)fVal;
160546 break;
160547 }
160548 default:
160549 goto error_out;
160550 }
160551 if( iVal<=0 ) goto error_out;
160552
160553 p->nStep++;
160554 if( iVal==p->nStep ){
160555 p->pValue = sqlite3_value_dup(apArg[0]);
160556 if( !p->pValue ){
160557 sqlite3_result_error_nomem(pCtx);
160558 }
160559 }
160560 }
160561 UNUSED_PARAMETER(nArg)(void)(nArg);
160562 UNUSED_PARAMETER(apArg)(void)(apArg);
160563 return;
160564
160565 error_out:
160566 sqlite3_result_error(
160567 pCtx, "second argument to nth_value must be a positive integer", -1
160568 );
160569}
160570static void nth_valueFinalizeFunc(sqlite3_context *pCtx){
160571 struct NthValueCtx *p;
160572 p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, 0);
160573 if( p && p->pValue ){
160574 sqlite3_result_value(pCtx, p->pValue);
160575 sqlite3_value_free(p->pValue);
160576 p->pValue = 0;
160577 }
160578}
160579#define nth_valueInvFuncnoopStepFunc noopStepFunc
160580#define nth_valueValueFuncnoopValueFunc noopValueFunc
160581
160582static void first_valueStepFunc(
160583 sqlite3_context *pCtx,
160584 int nArg,
160585 sqlite3_value **apArg
160586){
160587 struct NthValueCtx *p;
160588 p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
160589 if( p && p->pValue==0 ){
160590 p->pValue = sqlite3_value_dup(apArg[0]);
160591 if( !p->pValue ){
160592 sqlite3_result_error_nomem(pCtx);
160593 }
160594 }
160595 UNUSED_PARAMETER(nArg)(void)(nArg);
160596 UNUSED_PARAMETER(apArg)(void)(apArg);
160597}
160598static void first_valueFinalizeFunc(sqlite3_context *pCtx){
160599 struct NthValueCtx *p;
160600 p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
160601 if( p && p->pValue ){
160602 sqlite3_result_value(pCtx, p->pValue);
160603 sqlite3_value_free(p->pValue);
160604 p->pValue = 0;
160605 }
160606}
160607#define first_valueInvFuncnoopStepFunc noopStepFunc
160608#define first_valueValueFuncnoopValueFunc noopValueFunc
160609
160610/*
160611** Implementation of built-in window function rank(). Assumes that
160612** the window frame has been set to:
160613**
160614** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
160615*/
160616static void rankStepFunc(
160617 sqlite3_context *pCtx,
160618 int nArg,
160619 sqlite3_value **apArg
160620){
160621 struct CallCount *p;
160622 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
160623 if( p ){
160624 p->nStep++;
160625 if( p->nValue==0 ){
160626 p->nValue = p->nStep;
160627 }
160628 }
160629 UNUSED_PARAMETER(nArg)(void)(nArg);
160630 UNUSED_PARAMETER(apArg)(void)(apArg);
160631}
160632static void rankValueFunc(sqlite3_context *pCtx){
160633 struct CallCount *p;
160634 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
160635 if( p ){
160636 sqlite3_result_int64(pCtx, p->nValue);
160637 p->nValue = 0;
160638 }
160639}
160640
160641/*
160642** Implementation of built-in window function percent_rank(). Assumes that
160643** the window frame has been set to:
160644**
160645** GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
160646*/
160647static void percent_rankStepFunc(
160648 sqlite3_context *pCtx,
160649 int nArg,
160650 sqlite3_value **apArg
160651){
160652 struct CallCount *p;
160653 UNUSED_PARAMETER(nArg)(void)(nArg); assert( nArg==0 )((void) (0));
160654 UNUSED_PARAMETER(apArg)(void)(apArg);
160655 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
160656 if( p ){
160657 p->nTotal++;
160658 }
160659}
160660static void percent_rankInvFunc(
160661 sqlite3_context *pCtx,
160662 int nArg,
160663 sqlite3_value **apArg
160664){
160665 struct CallCount *p;
160666 UNUSED_PARAMETER(nArg)(void)(nArg); assert( nArg==0 )((void) (0));
160667 UNUSED_PARAMETER(apArg)(void)(apArg);
160668 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
160669 p->nStep++;
160670}
160671static void percent_rankValueFunc(sqlite3_context *pCtx){
160672 struct CallCount *p;
160673 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
160674 if( p ){
160675 p->nValue = p->nStep;
160676 if( p->nTotal>1 ){
160677 double r = (double)p->nValue / (double)(p->nTotal-1);
160678 sqlite3_result_double(pCtx, r);
160679 }else{
160680 sqlite3_result_double(pCtx, 0.0);
160681 }
160682 }
160683}
160684#define percent_rankFinalizeFuncpercent_rankValueFunc percent_rankValueFunc
160685
160686/*
160687** Implementation of built-in window function cume_dist(). Assumes that
160688** the window frame has been set to:
160689**
160690** GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING
160691*/
160692static void cume_distStepFunc(
160693 sqlite3_context *pCtx,
160694 int nArg,
160695 sqlite3_value **apArg
160696){
160697 struct CallCount *p;
160698 UNUSED_PARAMETER(nArg)(void)(nArg); assert( nArg==0 )((void) (0));
160699 UNUSED_PARAMETER(apArg)(void)(apArg);
160700 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
160701 if( p ){
160702 p->nTotal++;
160703 }
160704}
160705static void cume_distInvFunc(
160706 sqlite3_context *pCtx,
160707 int nArg,
160708 sqlite3_value **apArg
160709){
160710 struct CallCount *p;
160711 UNUSED_PARAMETER(nArg)(void)(nArg); assert( nArg==0 )((void) (0));
160712 UNUSED_PARAMETER(apArg)(void)(apArg);
160713 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
160714 p->nStep++;
160715}
160716static void cume_distValueFunc(sqlite3_context *pCtx){
160717 struct CallCount *p;
160718 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, 0);
160719 if( p ){
160720 double r = (double)(p->nStep) / (double)(p->nTotal);
160721 sqlite3_result_double(pCtx, r);
160722 }
160723}
160724#define cume_distFinalizeFunccume_distValueFunc cume_distValueFunc
160725
160726/*
160727** Context object for ntile() window function.
160728*/
160729struct NtileCtx {
160730 i64 nTotal; /* Total rows in partition */
160731 i64 nParam; /* Parameter passed to ntile(N) */
160732 i64 iRow; /* Current row */
160733};
160734
160735/*
160736** Implementation of ntile(). This assumes that the window frame has
160737** been coerced to:
160738**
160739** ROWS CURRENT ROW AND UNBOUNDED FOLLOWING
160740*/
160741static void ntileStepFunc(
160742 sqlite3_context *pCtx,
160743 int nArg,
160744 sqlite3_value **apArg
160745){
160746 struct NtileCtx *p;
160747 assert( nArg==1 )((void) (0)); UNUSED_PARAMETER(nArg)(void)(nArg);
160748 p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
160749 if( p ){
160750 if( p->nTotal==0 ){
160751 p->nParam = sqlite3_value_int64(apArg[0]);
160752 if( p->nParam<=0 ){
160753 sqlite3_result_error(
160754 pCtx, "argument of ntile must be a positive integer", -1
160755 );
160756 }
160757 }
160758 p->nTotal++;
160759 }
160760}
160761static void ntileInvFunc(
160762 sqlite3_context *pCtx,
160763 int nArg,
160764 sqlite3_value **apArg
160765){
160766 struct NtileCtx *p;
160767 assert( nArg==1 )((void) (0)); UNUSED_PARAMETER(nArg)(void)(nArg);
160768 UNUSED_PARAMETER(apArg)(void)(apArg);
160769 p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
160770 p->iRow++;
160771}
160772static void ntileValueFunc(sqlite3_context *pCtx){
160773 struct NtileCtx *p;
160774 p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
160775 if( p && p->nParam>0 ){
160776 int nSize = (p->nTotal / p->nParam);
160777 if( nSize==0 ){
160778 sqlite3_result_int64(pCtx, p->iRow+1);
160779 }else{
160780 i64 nLarge = p->nTotal - p->nParam*nSize;
160781 i64 iSmall = nLarge*(nSize+1);
160782 i64 iRow = p->iRow;
160783
160784 assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal )((void) (0));
160785
160786 if( iRow<iSmall ){
160787 sqlite3_result_int64(pCtx, 1 + iRow/(nSize+1));
160788 }else{
160789 sqlite3_result_int64(pCtx, 1 + nLarge + (iRow-iSmall)/nSize);
160790 }
160791 }
160792 }
160793}
160794#define ntileFinalizeFuncntileValueFunc ntileValueFunc
160795
160796/*
160797** Context object for last_value() window function.
160798*/
160799struct LastValueCtx {
160800 sqlite3_value *pVal;
160801 int nVal;
160802};
160803
160804/*
160805** Implementation of last_value().
160806*/
160807static void last_valueStepFunc(
160808 sqlite3_context *pCtx,
160809 int nArg,
160810 sqlite3_value **apArg
160811){
160812 struct LastValueCtx *p;
160813 UNUSED_PARAMETER(nArg)(void)(nArg);
160814 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
160815 if( p ){
160816 sqlite3_value_free(p->pVal);
160817 p->pVal = sqlite3_value_dup(apArg[0]);
160818 if( p->pVal==0 ){
160819 sqlite3_result_error_nomem(pCtx);
160820 }else{
160821 p->nVal++;
160822 }
160823 }
160824}
160825static void last_valueInvFunc(
160826 sqlite3_context *pCtx,
160827 int nArg,
160828 sqlite3_value **apArg
160829){
160830 struct LastValueCtx *p;
160831 UNUSED_PARAMETER(nArg)(void)(nArg);
160832 UNUSED_PARAMETER(apArg)(void)(apArg);
160833 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
160834 if( ALWAYS(p)(p) ){
160835 p->nVal--;
160836 if( p->nVal==0 ){
160837 sqlite3_value_free(p->pVal);
160838 p->pVal = 0;
160839 }
160840 }
160841}
160842static void last_valueValueFunc(sqlite3_context *pCtx){
160843 struct LastValueCtx *p;
160844 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, 0);
160845 if( p && p->pVal ){
160846 sqlite3_result_value(pCtx, p->pVal);
160847 }
160848}
160849static void last_valueFinalizeFunc(sqlite3_context *pCtx){
160850 struct LastValueCtx *p;
160851 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
160852 if( p && p->pVal ){
160853 sqlite3_result_value(pCtx, p->pVal);
160854 sqlite3_value_free(p->pVal);
160855 p->pVal = 0;
160856 }
160857}
160858
160859/*
160860** Static names for the built-in window function names. These static
160861** names are used, rather than string literals, so that FuncDef objects
160862** can be associated with a particular window function by direct
160863** comparison of the zName pointer. Example:
160864**
160865** if( pFuncDef->zName==row_valueName ){ ... }
160866*/
160867static const char row_numberName[] = "row_number";
160868static const char dense_rankName[] = "dense_rank";
160869static const char rankName[] = "rank";
160870static const char percent_rankName[] = "percent_rank";
160871static const char cume_distName[] = "cume_dist";
160872static const char ntileName[] = "ntile";
160873static const char last_valueName[] = "last_value";
160874static const char nth_valueName[] = "nth_value";
160875static const char first_valueName[] = "first_value";
160876static const char leadName[] = "lead";
160877static const char lagName[] = "lag";
160878
160879/*
160880** No-op implementations of xStep() and xFinalize(). Used as place-holders
160881** for built-in window functions that never call those interfaces.
160882**
160883** The noopValueFunc() is called but is expected to do nothing. The
160884** noopStepFunc() is never called, and so it is marked with NO_TEST to
160885** let the test coverage routine know not to expect this function to be
160886** invoked.
160887*/
160888static void noopStepFunc( /*NO_TEST*/
160889 sqlite3_context *p, /*NO_TEST*/
160890 int n, /*NO_TEST*/
160891 sqlite3_value **a /*NO_TEST*/
160892){ /*NO_TEST*/
160893 UNUSED_PARAMETER(p)(void)(p); /*NO_TEST*/
160894 UNUSED_PARAMETER(n)(void)(n); /*NO_TEST*/
160895 UNUSED_PARAMETER(a)(void)(a); /*NO_TEST*/
160896 assert(0)((void) (0)); /*NO_TEST*/
160897} /*NO_TEST*/
160898static void noopValueFunc(sqlite3_context *p){ UNUSED_PARAMETER(p)(void)(p); /*no-op*/ }
160899
160900/* Window functions that use all window interfaces: xStep, xFinal,
160901** xValue, and xInverse */
160902#define WINDOWFUNCALL(name,nArg,extra){ nArg, (0x00800000|1|0x00010000|extra), 0, 0, nameStepFunc, nameFinalizeFunc
, nameValueFunc, nameInvFunc, nameName, {0} }
{ \
160903 nArg, (SQLITE_FUNC_BUILTIN0x00800000|SQLITE_UTF81|SQLITE_FUNC_WINDOW0x00010000|extra), 0, 0, \
160904 name ## StepFunc, name ## FinalizeFunc, name ## ValueFunc, \
160905 name ## InvFunc, name ## Name, {0} \
160906}
160907
160908/* Window functions that are implemented using bytecode and thus have
160909** no-op routines for their methods */
160910#define WINDOWFUNCNOOP(name,nArg,extra){ nArg, (0x00800000|1|0x00010000|extra), 0, 0, noopStepFunc, noopValueFunc
, noopValueFunc, noopStepFunc, nameName, {0} }
{ \
160911 nArg, (SQLITE_FUNC_BUILTIN0x00800000|SQLITE_UTF81|SQLITE_FUNC_WINDOW0x00010000|extra), 0, 0, \
160912 noopStepFunc, noopValueFunc, noopValueFunc, \
160913 noopStepFunc, name ## Name, {0} \
160914}
160915
160916/* Window functions that use all window interfaces: xStep, the
160917** same routine for xFinalize and xValue and which never call
160918** xInverse. */
160919#define WINDOWFUNCX(name,nArg,extra){ nArg, (0x00800000|1|0x00010000|extra), 0, 0, nameStepFunc, nameValueFunc
, nameValueFunc, noopStepFunc, nameName, {0} }
{ \
160920 nArg, (SQLITE_FUNC_BUILTIN0x00800000|SQLITE_UTF81|SQLITE_FUNC_WINDOW0x00010000|extra), 0, 0, \
160921 name ## StepFunc, name ## ValueFunc, name ## ValueFunc, \
160922 noopStepFunc, name ## Name, {0} \
160923}
160924
160925
160926/*
160927** Register those built-in window functions that are not also aggregates.
160928*/
160929SQLITE_PRIVATEstatic void sqlite3WindowFunctions(void){
160930 static FuncDef aWindowFuncs[] = {
160931 WINDOWFUNCX(row_number, 0, 0){ 0, (0x00800000|1|0x00010000|0), 0, 0, row_numberStepFunc, row_numberValueFunc
, row_numberValueFunc, noopStepFunc, row_numberName, {0} }
,
160932 WINDOWFUNCX(dense_rank, 0, 0){ 0, (0x00800000|1|0x00010000|0), 0, 0, dense_rankStepFunc, dense_rankValueFunc
, dense_rankValueFunc, noopStepFunc, dense_rankName, {0} }
,
160933 WINDOWFUNCX(rank, 0, 0){ 0, (0x00800000|1|0x00010000|0), 0, 0, rankStepFunc, rankValueFunc
, rankValueFunc, noopStepFunc, rankName, {0} }
,
160934 WINDOWFUNCALL(percent_rank, 0, 0){ 0, (0x00800000|1|0x00010000|0), 0, 0, percent_rankStepFunc,
percent_rankValueFunc, percent_rankValueFunc, percent_rankInvFunc
, percent_rankName, {0} }
,
160935 WINDOWFUNCALL(cume_dist, 0, 0){ 0, (0x00800000|1|0x00010000|0), 0, 0, cume_distStepFunc, cume_distValueFunc
, cume_distValueFunc, cume_distInvFunc, cume_distName, {0} }
,
160936 WINDOWFUNCALL(ntile, 1, 0){ 1, (0x00800000|1|0x00010000|0), 0, 0, ntileStepFunc, ntileValueFunc
, ntileValueFunc, ntileInvFunc, ntileName, {0} }
,
160937 WINDOWFUNCALL(last_value, 1, 0){ 1, (0x00800000|1|0x00010000|0), 0, 0, last_valueStepFunc, last_valueFinalizeFunc
, last_valueValueFunc, last_valueInvFunc, last_valueName, {0}
}
,
160938 WINDOWFUNCALL(nth_value, 2, 0){ 2, (0x00800000|1|0x00010000|0), 0, 0, nth_valueStepFunc, nth_valueFinalizeFunc
, noopValueFunc, noopStepFunc, nth_valueName, {0} }
,
160939 WINDOWFUNCALL(first_value, 1, 0){ 1, (0x00800000|1|0x00010000|0), 0, 0, first_valueStepFunc, first_valueFinalizeFunc
, noopValueFunc, noopStepFunc, first_valueName, {0} }
,
160940 WINDOWFUNCNOOP(lead, 1, 0){ 1, (0x00800000|1|0x00010000|0), 0, 0, noopStepFunc, noopValueFunc
, noopValueFunc, noopStepFunc, leadName, {0} }
,
160941 WINDOWFUNCNOOP(lead, 2, 0){ 2, (0x00800000|1|0x00010000|0), 0, 0, noopStepFunc, noopValueFunc
, noopValueFunc, noopStepFunc, leadName, {0} }
,
160942 WINDOWFUNCNOOP(lead, 3, 0){ 3, (0x00800000|1|0x00010000|0), 0, 0, noopStepFunc, noopValueFunc
, noopValueFunc, noopStepFunc, leadName, {0} }
,
160943 WINDOWFUNCNOOP(lag, 1, 0){ 1, (0x00800000|1|0x00010000|0), 0, 0, noopStepFunc, noopValueFunc
, noopValueFunc, noopStepFunc, lagName, {0} }
,
160944 WINDOWFUNCNOOP(lag, 2, 0){ 2, (0x00800000|1|0x00010000|0), 0, 0, noopStepFunc, noopValueFunc
, noopValueFunc, noopStepFunc, lagName, {0} }
,
160945 WINDOWFUNCNOOP(lag, 3, 0){ 3, (0x00800000|1|0x00010000|0), 0, 0, noopStepFunc, noopValueFunc
, noopValueFunc, noopStepFunc, lagName, {0} }
,
160946 };
160947 sqlite3InsertBuiltinFuncs(aWindowFuncs, ArraySize(aWindowFuncs)((int)(sizeof(aWindowFuncs)/sizeof(aWindowFuncs[0]))));
160948}
160949
160950static Window *windowFind(Parse *pParse, Window *pList, const char *zName){
160951 Window *p;
160952 for(p=pList; p; p=p->pNextWin){
160953 if( sqlite3StrICmp(p->zName, zName)==0 ) break;
160954 }
160955 if( p==0 ){
160956 sqlite3ErrorMsg(pParse, "no such window: %s", zName);
160957 }
160958 return p;
160959}
160960
160961/*
160962** This function is called immediately after resolving the function name
160963** for a window function within a SELECT statement. Argument pList is a
160964** linked list of WINDOW definitions for the current SELECT statement.
160965** Argument pFunc is the function definition just resolved and pWin
160966** is the Window object representing the associated OVER clause. This
160967** function updates the contents of pWin as follows:
160968**
160969** * If the OVER clause refered to a named window (as in "max(x) OVER win"),
160970** search list pList for a matching WINDOW definition, and update pWin
160971** accordingly. If no such WINDOW clause can be found, leave an error
160972** in pParse.
160973**
160974** * If the function is a built-in window function that requires the
160975** window to be coerced (see "BUILT-IN WINDOW FUNCTIONS" at the top
160976** of this file), pWin is updated here.
160977*/
160978SQLITE_PRIVATEstatic void sqlite3WindowUpdate(
160979 Parse *pParse,
160980 Window *pList, /* List of named windows for this SELECT */
160981 Window *pWin, /* Window frame to update */
160982 FuncDef *pFunc /* Window function definition */
160983){
160984 if( pWin->zName && pWin->eFrmType==0 ){
160985 Window *p = windowFind(pParse, pList, pWin->zName);
160986 if( p==0 ) return;
160987 pWin->pPartition = sqlite3ExprListDup(pParse->db, p->pPartition, 0);
160988 pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0);
160989 pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0);
160990 pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0);
160991 pWin->eStart = p->eStart;
160992 pWin->eEnd = p->eEnd;
160993 pWin->eFrmType = p->eFrmType;
160994 pWin->eExclude = p->eExclude;
160995 }else{
160996 sqlite3WindowChain(pParse, pWin, pList);
160997 }
160998 if( (pWin->eFrmType==TK_RANGE89)
160999 && (pWin->pStart || pWin->pEnd)
161000 && (pWin->pOrderBy==0 || pWin->pOrderBy->nExpr!=1)
161001 ){
161002 sqlite3ErrorMsg(pParse,
161003 "RANGE with offset PRECEDING/FOLLOWING requires one ORDER BY expression"
161004 );
161005 }else
161006 if( pFunc->funcFlags & SQLITE_FUNC_WINDOW0x00010000 ){
161007 sqlite3 *db = pParse->db;
161008 if( pWin->pFilter ){
161009 sqlite3ErrorMsg(pParse,
161010 "FILTER clause may only be used with aggregate window functions"
161011 );
161012 }else{
161013 struct WindowUpdate {
161014 const char *zFunc;
161015 int eFrmType;
161016 int eStart;
161017 int eEnd;
161018 } aUp[] = {
161019 { row_numberName, TK_ROWS76, TK_UNBOUNDED90, TK_CURRENT85 },
161020 { dense_rankName, TK_RANGE89, TK_UNBOUNDED90, TK_CURRENT85 },
161021 { rankName, TK_RANGE89, TK_UNBOUNDED90, TK_CURRENT85 },
161022 { percent_rankName, TK_GROUPS92, TK_CURRENT85, TK_UNBOUNDED90 },
161023 { cume_distName, TK_GROUPS92, TK_FOLLOWING86, TK_UNBOUNDED90 },
161024 { ntileName, TK_ROWS76, TK_CURRENT85, TK_UNBOUNDED90 },
161025 { leadName, TK_ROWS76, TK_UNBOUNDED90, TK_UNBOUNDED90 },
161026 { lagName, TK_ROWS76, TK_UNBOUNDED90, TK_CURRENT85 },
161027 };
161028 int i;
161029 for(i=0; i<ArraySize(aUp)((int)(sizeof(aUp)/sizeof(aUp[0]))); i++){
161030 if( pFunc->zName==aUp[i].zFunc ){
161031 sqlite3ExprDelete(db, pWin->pStart);
161032 sqlite3ExprDelete(db, pWin->pEnd);
161033 pWin->pEnd = pWin->pStart = 0;
161034 pWin->eFrmType = aUp[i].eFrmType;
161035 pWin->eStart = aUp[i].eStart;
161036 pWin->eEnd = aUp[i].eEnd;
161037 pWin->eExclude = 0;
161038 if( pWin->eStart==TK_FOLLOWING86 ){
161039 pWin->pStart = sqlite3Expr(db, TK_INTEGER155, "1");
161040 }
161041 break;
161042 }
161043 }
161044 }
161045 }
161046 pWin->pWFunc = pFunc;
161047}
161048
161049/*
161050** Context object passed through sqlite3WalkExprList() to
161051** selectWindowRewriteExprCb() by selectWindowRewriteEList().
161052*/
161053typedef struct WindowRewrite WindowRewrite;
161054struct WindowRewrite {
161055 Window *pWin;
161056 SrcList *pSrc;
161057 ExprList *pSub;
161058 Table *pTab;
161059 Select *pSubSelect; /* Current sub-select, if any */
161060};
161061
161062/*
161063** Callback function used by selectWindowRewriteEList(). If necessary,
161064** this function appends to the output expression-list and updates
161065** expression (*ppExpr) in place.
161066*/
161067static int selectWindowRewriteExprCb(Walker *pWalker, Expr *pExpr){
161068 struct WindowRewrite *p = pWalker->u.pRewrite;
161069 Parse *pParse = pWalker->pParse;
161070 assert( p!=0 )((void) (0));
161071 assert( p->pWin!=0 )((void) (0));
161072
161073 /* If this function is being called from within a scalar sub-select
161074 ** that used by the SELECT statement being processed, only process
161075 ** TK_COLUMN expressions that refer to it (the outer SELECT). Do
161076 ** not process aggregates or window functions at all, as they belong
161077 ** to the scalar sub-select. */
161078 if( p->pSubSelect ){
161079 if( pExpr->op!=TK_COLUMN167 ){
161080 return WRC_Continue0;
161081 }else{
161082 int nSrc = p->pSrc->nSrc;
161083 int i;
161084 for(i=0; i<nSrc; i++){
161085 if( pExpr->iTable==p->pSrc->a[i].iCursor ) break;
161086 }
161087 if( i==nSrc ) return WRC_Continue0;
161088 }
161089 }
161090
161091 switch( pExpr->op ){
161092
161093 case TK_FUNCTION172:
161094 if( !ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
161095 break;
161096 }else{
161097 Window *pWin;
161098 for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
161099 if( pExpr->y.pWin==pWin ){
161100 assert( pWin->pOwner==pExpr )((void) (0));
161101 return WRC_Prune1;
161102 }
161103 }
161104 }
161105 /* no break */ deliberate_fall_through
161106
161107 case TK_AGG_FUNCTION168:
161108 case TK_COLUMN167: {
161109 int iCol = -1;
161110 if( pParse->db->mallocFailed ) return WRC_Abort2;
161111 if( p->pSub ){
161112 int i;
161113 for(i=0; i<p->pSub->nExpr; i++){
161114 if( 0==sqlite3ExprCompare(0, p->pSub->a[i].pExpr, pExpr, -1) ){
161115 iCol = i;
161116 break;
161117 }
161118 }
161119 }
161120 if( iCol<0 ){
161121 Expr *pDup = sqlite3ExprDup(pParse->db, pExpr, 0);
161122 if( pDup && pDup->op==TK_AGG_FUNCTION168 ) pDup->op = TK_FUNCTION172;
161123 p->pSub = sqlite3ExprListAppend(pParse, p->pSub, pDup);
161124 }
161125 if( p->pSub ){
161126 int f = pExpr->flags & EP_Collate0x000200;
161127 assert( ExprHasProperty(pExpr, EP_Static)==0 )((void) (0));
161128 ExprSetProperty(pExpr, EP_Static)(pExpr)->flags|=(0x8000000);
161129 sqlite3ExprDelete(pParse->db, pExpr);
161130 ExprClearProperty(pExpr, EP_Static)(pExpr)->flags&=~(0x8000000);
161131 memset(pExpr, 0, sizeof(Expr));
161132
161133 pExpr->op = TK_COLUMN167;
161134 pExpr->iColumn = (iCol<0 ? p->pSub->nExpr-1: iCol);
161135 pExpr->iTable = p->pWin->iEphCsr;
161136 pExpr->y.pTab = p->pTab;
161137 pExpr->flags = f;
161138 }
161139 if( pParse->db->mallocFailed ) return WRC_Abort2;
161140 break;
161141 }
161142
161143 default: /* no-op */
161144 break;
161145 }
161146
161147 return WRC_Continue0;
161148}
161149static int selectWindowRewriteSelectCb(Walker *pWalker, Select *pSelect){
161150 struct WindowRewrite *p = pWalker->u.pRewrite;
161151 Select *pSave = p->pSubSelect;
161152 if( pSave==pSelect ){
161153 return WRC_Continue0;
161154 }else{
161155 p->pSubSelect = pSelect;
161156 sqlite3WalkSelect(pWalker, pSelect);
161157 p->pSubSelect = pSave;
161158 }
161159 return WRC_Prune1;
161160}
161161
161162
161163/*
161164** Iterate through each expression in expression-list pEList. For each:
161165**
161166** * TK_COLUMN,
161167** * aggregate function, or
161168** * window function with a Window object that is not a member of the
161169** Window list passed as the second argument (pWin).
161170**
161171** Append the node to output expression-list (*ppSub). And replace it
161172** with a TK_COLUMN that reads the (N-1)th element of table
161173** pWin->iEphCsr, where N is the number of elements in (*ppSub) after
161174** appending the new one.
161175*/
161176static void selectWindowRewriteEList(
161177 Parse *pParse,
161178 Window *pWin,
161179 SrcList *pSrc,
161180 ExprList *pEList, /* Rewrite expressions in this list */
161181 Table *pTab,
161182 ExprList **ppSub /* IN/OUT: Sub-select expression-list */
161183){
161184 Walker sWalker;
161185 WindowRewrite sRewrite;
161186
161187 assert( pWin!=0 )((void) (0));
161188 memset(&sWalker, 0, sizeof(Walker));
161189 memset(&sRewrite, 0, sizeof(WindowRewrite));
161190
161191 sRewrite.pSub = *ppSub;
161192 sRewrite.pWin = pWin;
161193 sRewrite.pSrc = pSrc;
161194 sRewrite.pTab = pTab;
161195
161196 sWalker.pParse = pParse;
161197 sWalker.xExprCallback = selectWindowRewriteExprCb;
161198 sWalker.xSelectCallback = selectWindowRewriteSelectCb;
161199 sWalker.u.pRewrite = &sRewrite;
161200
161201 (void)sqlite3WalkExprList(&sWalker, pEList);
161202
161203 *ppSub = sRewrite.pSub;
161204}
161205
161206/*
161207** Append a copy of each expression in expression-list pAppend to
161208** expression list pList. Return a pointer to the result list.
161209*/
161210static ExprList *exprListAppendList(
161211 Parse *pParse, /* Parsing context */
161212 ExprList *pList, /* List to which to append. Might be NULL */
161213 ExprList *pAppend, /* List of values to append. Might be NULL */
161214 int bIntToNull
161215){
161216 if( pAppend ){
161217 int i;
161218 int nInit = pList ? pList->nExpr : 0;
161219 for(i=0; i<pAppend->nExpr; i++){
161220 sqlite3 *db = pParse->db;
161221 Expr *pDup = sqlite3ExprDup(db, pAppend->a[i].pExpr, 0);
161222 assert( pDup==0 || !ExprHasProperty(pDup, EP_MemToken) )((void) (0));
161223 if( db->mallocFailed ){
161224 sqlite3ExprDelete(db, pDup);
161225 break;
161226 }
161227 if( bIntToNull ){
161228 int iDummy;
161229 Expr *pSub;
161230 pSub = sqlite3ExprSkipCollateAndLikely(pDup);
161231 if( sqlite3ExprIsInteger(pSub, &iDummy) ){
161232 pSub->op = TK_NULL121;
161233 pSub->flags &= ~(EP_IntValue0x000800|EP_IsTrue0x10000000|EP_IsFalse0x20000000);
161234 pSub->u.zToken = 0;
161235 }
161236 }
161237 pList = sqlite3ExprListAppend(pParse, pList, pDup);
161238 if( pList ) pList->a[nInit+i].fg.sortFlags = pAppend->a[i].fg.sortFlags;
161239 }
161240 }
161241 return pList;
161242}
161243
161244/*
161245** When rewriting a query, if the new subquery in the FROM clause
161246** contains TK_AGG_FUNCTION nodes that refer to an outer query,
161247** then we have to increase the Expr->op2 values of those nodes
161248** due to the extra subquery layer that was added.
161249**
161250** See also the incrAggDepth() routine in resolve.c
161251*/
161252static int sqlite3WindowExtraAggFuncDepth(Walker *pWalker, Expr *pExpr){
161253 if( pExpr->op==TK_AGG_FUNCTION168
161254 && pExpr->op2>=pWalker->walkerDepth
161255 ){
161256 pExpr->op2++;
161257 }
161258 return WRC_Continue0;
161259}
161260
161261static int disallowAggregatesInOrderByCb(Walker *pWalker, Expr *pExpr){
161262 if( pExpr->op==TK_AGG_FUNCTION168 && pExpr->pAggInfo==0 ){
161263 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
161264 sqlite3ErrorMsg(pWalker->pParse,
161265 "misuse of aggregate: %s()", pExpr->u.zToken);
161266 }
161267 return WRC_Continue0;
161268}
161269
161270/*
161271** If the SELECT statement passed as the second argument does not invoke
161272** any SQL window functions, this function is a no-op. Otherwise, it
161273** rewrites the SELECT statement so that window function xStep functions
161274** are invoked in the correct order as described under "SELECT REWRITING"
161275** at the top of this file.
161276*/
161277SQLITE_PRIVATEstatic int sqlite3WindowRewrite(Parse *pParse, Select *p){
161278 int rc = SQLITE_OK0;
161279 if( p->pWin
161280 && p->pPrior==0
161281 && ALWAYS((p->selFlags & SF_WinRewrite)==0)((p->selFlags & 0x0100000)==0)
161282 && ALWAYS(!IN_RENAME_OBJECT)(!(pParse->eParseMode>=2))
161283 ){
161284 Vdbe *v = sqlite3GetVdbe(pParse);
161285 sqlite3 *db = pParse->db;
161286 Select *pSub = 0; /* The subquery */
161287 SrcList *pSrc = p->pSrc;
161288 Expr *pWhere = p->pWhere;
161289 ExprList *pGroupBy = p->pGroupBy;
161290 Expr *pHaving = p->pHaving;
161291 ExprList *pSort = 0;
161292
161293 ExprList *pSublist = 0; /* Expression list for sub-query */
161294 Window *pMWin = p->pWin; /* Main window object */
161295 Window *pWin; /* Window object iterator */
161296 Table *pTab;
161297 Walker w;
161298
161299 u32 selFlags = p->selFlags;
161300
161301 pTab = sqlite3DbMallocZero(db, sizeof(Table));
161302 if( pTab==0 ){
161303 return sqlite3ErrorToParser(db, SQLITE_NOMEM7);
161304 }
161305 sqlite3AggInfoPersistWalkerInit(&w, pParse);
161306 sqlite3WalkSelect(&w, p);
161307 if( (p->selFlags & SF_Aggregate0x0000008)==0 ){
161308 w.xExprCallback = disallowAggregatesInOrderByCb;
161309 w.xSelectCallback = 0;
161310 sqlite3WalkExprList(&w, p->pOrderBy);
161311 }
161312
161313 p->pSrc = 0;
161314 p->pWhere = 0;
161315 p->pGroupBy = 0;
161316 p->pHaving = 0;
161317 p->selFlags &= ~SF_Aggregate0x0000008;
161318 p->selFlags |= SF_WinRewrite0x0100000;
161319
161320 /* Create the ORDER BY clause for the sub-select. This is the concatenation
161321 ** of the window PARTITION and ORDER BY clauses. Then, if this makes it
161322 ** redundant, remove the ORDER BY from the parent SELECT. */
161323 pSort = exprListAppendList(pParse, 0, pMWin->pPartition, 1);
161324 pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy, 1);
161325 if( pSort && p->pOrderBy && p->pOrderBy->nExpr<=pSort->nExpr ){
161326 int nSave = pSort->nExpr;
161327 pSort->nExpr = p->pOrderBy->nExpr;
161328 if( sqlite3ExprListCompare(pSort, p->pOrderBy, -1)==0 ){
161329 sqlite3ExprListDelete(db, p->pOrderBy);
161330 p->pOrderBy = 0;
161331 }
161332 pSort->nExpr = nSave;
161333 }
161334
161335 /* Assign a cursor number for the ephemeral table used to buffer rows.
161336 ** The OpenEphemeral instruction is coded later, after it is known how
161337 ** many columns the table will have. */
161338 pMWin->iEphCsr = pParse->nTab++;
161339 pParse->nTab += 3;
161340
161341 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pEList, pTab, &pSublist);
161342 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pOrderBy, pTab, &pSublist);
161343 pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
161344
161345 /* Append the PARTITION BY and ORDER BY expressions to the to the
161346 ** sub-select expression list. They are required to figure out where
161347 ** boundaries for partitions and sets of peer rows lie. */
161348 pSublist = exprListAppendList(pParse, pSublist, pMWin->pPartition, 0);
161349 pSublist = exprListAppendList(pParse, pSublist, pMWin->pOrderBy, 0);
161350
161351 /* Append the arguments passed to each window function to the
161352 ** sub-select expression list. Also allocate two registers for each
161353 ** window function - one for the accumulator, another for interim
161354 ** results. */
161355 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
161356 ExprList *pArgs;
161357 assert( ExprUseXList(pWin->pOwner) )((void) (0));
161358 assert( pWin->pWFunc!=0 )((void) (0));
161359 pArgs = pWin->pOwner->x.pList;
161360 if( pWin->pWFunc->funcFlags & SQLITE_FUNC_SUBTYPE0x00100000 ){
161361 selectWindowRewriteEList(pParse, pMWin, pSrc, pArgs, pTab, &pSublist);
161362 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
161363 pWin->bExprArgs = 1;
161364 }else{
161365 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
161366 pSublist = exprListAppendList(pParse, pSublist, pArgs, 0);
161367 }
161368 if( pWin->pFilter ){
161369 Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0);
161370 pSublist = sqlite3ExprListAppend(pParse, pSublist, pFilter);
161371 }
161372 pWin->regAccum = ++pParse->nMem;
161373 pWin->regResult = ++pParse->nMem;
161374 sqlite3VdbeAddOp2(v, OP_Null75, 0, pWin->regAccum);
161375 }
161376
161377 /* If there is no ORDER BY or PARTITION BY clause, and the window
161378 ** function accepts zero arguments, and there are no other columns
161379 ** selected (e.g. "SELECT row_number() OVER () FROM t1"), it is possible
161380 ** that pSublist is still NULL here. Add a constant expression here to
161381 ** keep everything legal in this case.
161382 */
161383 if( pSublist==0 ){
161384 pSublist = sqlite3ExprListAppend(pParse, 0,
161385 sqlite3Expr(db, TK_INTEGER155, "0")
161386 );
161387 }
161388
161389 pSub = sqlite3SelectNew(
161390 pParse, pSublist, pSrc, pWhere, pGroupBy, pHaving, pSort, 0, 0
161391 );
161392 SELECTTRACE(1,pParse,pSub,
161393 ("New window-function subquery in FROM clause of (%u/%p)\n",
161394 p->selId, p));
161395 p->pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
161396 assert( pSub!=0 || p->pSrc==0 )((void) (0)); /* Due to db->mallocFailed test inside
161397 ** of sqlite3DbMallocRawNN() called from
161398 ** sqlite3SrcListAppend() */
161399 if( p->pSrc ){
161400 Table *pTab2;
161401 p->pSrc->a[0].pSelect = pSub;
161402 sqlite3SrcListAssignCursors(pParse, p->pSrc);
161403 pSub->selFlags |= SF_Expanded0x0000040|SF_OrderByReqd0x8000000;
161404 pTab2 = sqlite3ResultSetOfSelect(pParse, pSub, SQLITE_AFF_NONE0x40);
161405 pSub->selFlags |= (selFlags & SF_Aggregate0x0000008);
161406 if( pTab2==0 ){
161407 /* Might actually be some other kind of error, but in that case
161408 ** pParse->nErr will be set, so if SQLITE_NOMEM is set, we will get
161409 ** the correct error message regardless. */
161410 rc = SQLITE_NOMEM7;
161411 }else{
161412 memcpy(pTab, pTab2, sizeof(Table));
161413 pTab->tabFlags |= TF_Ephemeral0x00004000;
161414 p->pSrc->a[0].pTab = pTab;
161415 pTab = pTab2;
161416 memset(&w, 0, sizeof(w));
161417 w.xExprCallback = sqlite3WindowExtraAggFuncDepth;
161418 w.xSelectCallback = sqlite3WalkerDepthIncrease;
161419 w.xSelectCallback2 = sqlite3WalkerDepthDecrease;
161420 sqlite3WalkSelect(&w, pSub);
161421 }
161422 }else{
161423 sqlite3SelectDelete(db, pSub);
161424 }
161425 if( db->mallocFailed ) rc = SQLITE_NOMEM7;
161426
161427 /* Defer deleting the temporary table pTab because if an error occurred,
161428 ** there could still be references to that table embedded in the
161429 ** result-set or ORDER BY clause of the SELECT statement p. */
161430 sqlite3ParserAddCleanup(pParse, sqlite3DbFree, pTab);
161431 }
161432
161433 assert( rc==SQLITE_OK || pParse->nErr!=0 )((void) (0));
161434 return rc;
161435}
161436
161437/*
161438** Unlink the Window object from the Select to which it is attached,
161439** if it is attached.
161440*/
161441SQLITE_PRIVATEstatic void sqlite3WindowUnlinkFromSelect(Window *p){
161442 if( p->ppThis ){
161443 *p->ppThis = p->pNextWin;
161444 if( p->pNextWin ) p->pNextWin->ppThis = p->ppThis;
161445 p->ppThis = 0;
161446 }
161447}
161448
161449/*
161450** Free the Window object passed as the second argument.
161451*/
161452SQLITE_PRIVATEstatic void sqlite3WindowDelete(sqlite3 *db, Window *p){
161453 if( p ){
161454 sqlite3WindowUnlinkFromSelect(p);
161455 sqlite3ExprDelete(db, p->pFilter);
161456 sqlite3ExprListDelete(db, p->pPartition);
161457 sqlite3ExprListDelete(db, p->pOrderBy);
161458 sqlite3ExprDelete(db, p->pEnd);
161459 sqlite3ExprDelete(db, p->pStart);
161460 sqlite3DbFree(db, p->zName);
161461 sqlite3DbFree(db, p->zBase);
161462 sqlite3DbFree(db, p);
161463 }
161464}
161465
161466/*
161467** Free the linked list of Window objects starting at the second argument.
161468*/
161469SQLITE_PRIVATEstatic void sqlite3WindowListDelete(sqlite3 *db, Window *p){
161470 while( p ){
161471 Window *pNext = p->pNextWin;
161472 sqlite3WindowDelete(db, p);
161473 p = pNext;
161474 }
161475}
161476
161477/*
161478** The argument expression is an PRECEDING or FOLLOWING offset. The
161479** value should be a non-negative integer. If the value is not a
161480** constant, change it to NULL. The fact that it is then a non-negative
161481** integer will be caught later. But it is important not to leave
161482** variable values in the expression tree.
161483*/
161484static Expr *sqlite3WindowOffsetExpr(Parse *pParse, Expr *pExpr){
161485 if( 0==sqlite3ExprIsConstant(pExpr) ){
161486 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ) sqlite3RenameExprUnmap(pParse, pExpr);
161487 sqlite3ExprDelete(pParse->db, pExpr);
161488 pExpr = sqlite3ExprAlloc(pParse->db, TK_NULL121, 0, 0);
161489 }
161490 return pExpr;
161491}
161492
161493/*
161494** Allocate and return a new Window object describing a Window Definition.
161495*/
161496SQLITE_PRIVATEstatic Window *sqlite3WindowAlloc(
161497 Parse *pParse, /* Parsing context */
161498 int eType, /* Frame type. TK_RANGE, TK_ROWS, TK_GROUPS, or 0 */
161499 int eStart, /* Start type: CURRENT, PRECEDING, FOLLOWING, UNBOUNDED */
161500 Expr *pStart, /* Start window size if TK_PRECEDING or FOLLOWING */
161501 int eEnd, /* End type: CURRENT, FOLLOWING, TK_UNBOUNDED, PRECEDING */
161502 Expr *pEnd, /* End window size if TK_FOLLOWING or PRECEDING */
161503 u8 eExclude /* EXCLUDE clause */
161504){
161505 Window *pWin = 0;
161506 int bImplicitFrame = 0;
161507
161508 /* Parser assures the following: */
161509 assert( eType==0 || eType==TK_RANGE || eType==TK_ROWS || eType==TK_GROUPS )((void) (0));
161510 assert( eStart==TK_CURRENT || eStart==TK_PRECEDING((void) (0))
161511 || eStart==TK_UNBOUNDED || eStart==TK_FOLLOWING )((void) (0));
161512 assert( eEnd==TK_CURRENT || eEnd==TK_FOLLOWING((void) (0))
161513 || eEnd==TK_UNBOUNDED || eEnd==TK_PRECEDING )((void) (0));
161514 assert( (eStart==TK_PRECEDING || eStart==TK_FOLLOWING)==(pStart!=0) )((void) (0));
161515 assert( (eEnd==TK_FOLLOWING || eEnd==TK_PRECEDING)==(pEnd!=0) )((void) (0));
161516
161517 if( eType==0 ){
161518 bImplicitFrame = 1;
161519 eType = TK_RANGE89;
161520 }
161521
161522 /* Additionally, the
161523 ** starting boundary type may not occur earlier in the following list than
161524 ** the ending boundary type:
161525 **
161526 ** UNBOUNDED PRECEDING
161527 ** <expr> PRECEDING
161528 ** CURRENT ROW
161529 ** <expr> FOLLOWING
161530 ** UNBOUNDED FOLLOWING
161531 **
161532 ** The parser ensures that "UNBOUNDED PRECEDING" cannot be used as an ending
161533 ** boundary, and than "UNBOUNDED FOLLOWING" cannot be used as a starting
161534 ** frame boundary.
161535 */
161536 if( (eStart==TK_CURRENT85 && eEnd==TK_PRECEDING88)
161537 || (eStart==TK_FOLLOWING86 && (eEnd==TK_PRECEDING88 || eEnd==TK_CURRENT85))
161538 ){
161539 sqlite3ErrorMsg(pParse, "unsupported frame specification");
161540 goto windowAllocErr;
161541 }
161542
161543 pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
161544 if( pWin==0 ) goto windowAllocErr;
161545 pWin->eFrmType = eType;
161546 pWin->eStart = eStart;
161547 pWin->eEnd = eEnd;
161548 if( eExclude==0 && OptimizationDisabled(pParse->db, SQLITE_WindowFunc)(((pParse->db)->dbOptFlags&(0x00000002))!=0) ){
161549 eExclude = TK_NO66;
161550 }
161551 pWin->eExclude = eExclude;
161552 pWin->bImplicitFrame = bImplicitFrame;
161553 pWin->pEnd = sqlite3WindowOffsetExpr(pParse, pEnd);
161554 pWin->pStart = sqlite3WindowOffsetExpr(pParse, pStart);
161555 return pWin;
161556
161557windowAllocErr:
161558 sqlite3ExprDelete(pParse->db, pEnd);
161559 sqlite3ExprDelete(pParse->db, pStart);
161560 return 0;
161561}
161562
161563/*
161564** Attach PARTITION and ORDER BY clauses pPartition and pOrderBy to window
161565** pWin. Also, if parameter pBase is not NULL, set pWin->zBase to the
161566** equivalent nul-terminated string.
161567*/
161568SQLITE_PRIVATEstatic Window *sqlite3WindowAssemble(
161569 Parse *pParse,
161570 Window *pWin,
161571 ExprList *pPartition,
161572 ExprList *pOrderBy,
161573 Token *pBase
161574){
161575 if( pWin ){
161576 pWin->pPartition = pPartition;
161577 pWin->pOrderBy = pOrderBy;
161578 if( pBase ){
161579 pWin->zBase = sqlite3DbStrNDup(pParse->db, pBase->z, pBase->n);
161580 }
161581 }else{
161582 sqlite3ExprListDelete(pParse->db, pPartition);
161583 sqlite3ExprListDelete(pParse->db, pOrderBy);
161584 }
161585 return pWin;
161586}
161587
161588/*
161589** Window *pWin has just been created from a WINDOW clause. Tokne pBase
161590** is the base window. Earlier windows from the same WINDOW clause are
161591** stored in the linked list starting at pWin->pNextWin. This function
161592** either updates *pWin according to the base specification, or else
161593** leaves an error in pParse.
161594*/
161595SQLITE_PRIVATEstatic void sqlite3WindowChain(Parse *pParse, Window *pWin, Window *pList){
161596 if( pWin->zBase ){
161597 sqlite3 *db = pParse->db;
161598 Window *pExist = windowFind(pParse, pList, pWin->zBase);
161599 if( pExist ){
161600 const char *zErr = 0;
161601 /* Check for errors */
161602 if( pWin->pPartition ){
161603 zErr = "PARTITION clause";
161604 }else if( pExist->pOrderBy && pWin->pOrderBy ){
161605 zErr = "ORDER BY clause";
161606 }else if( pExist->bImplicitFrame==0 ){
161607 zErr = "frame specification";
161608 }
161609 if( zErr ){
161610 sqlite3ErrorMsg(pParse,
161611 "cannot override %s of window: %s", zErr, pWin->zBase
161612 );
161613 }else{
161614 pWin->pPartition = sqlite3ExprListDup(db, pExist->pPartition, 0);
161615 if( pExist->pOrderBy ){
161616 assert( pWin->pOrderBy==0 )((void) (0));
161617 pWin->pOrderBy = sqlite3ExprListDup(db, pExist->pOrderBy, 0);
161618 }
161619 sqlite3DbFree(db, pWin->zBase);
161620 pWin->zBase = 0;
161621 }
161622 }
161623 }
161624}
161625
161626/*
161627** Attach window object pWin to expression p.
161628*/
161629SQLITE_PRIVATEstatic void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){
161630 if( p ){
161631 assert( p->op==TK_FUNCTION )((void) (0));
161632 assert( pWin )((void) (0));
161633 p->y.pWin = pWin;
161634 ExprSetProperty(p, EP_WinFunc)(p)->flags|=(0x1000000);
161635 pWin->pOwner = p;
161636 if( (p->flags & EP_Distinct0x000004) && pWin->eFrmType!=TK_FILTER166 ){
161637 sqlite3ErrorMsg(pParse,
161638 "DISTINCT is not supported for window functions"
161639 );
161640 }
161641 }else{
161642 sqlite3WindowDelete(pParse->db, pWin);
161643 }
161644}
161645
161646/*
161647** Possibly link window pWin into the list at pSel->pWin (window functions
161648** to be processed as part of SELECT statement pSel). The window is linked
161649** in if either (a) there are no other windows already linked to this
161650** SELECT, or (b) the windows already linked use a compatible window frame.
161651*/
161652SQLITE_PRIVATEstatic void sqlite3WindowLink(Select *pSel, Window *pWin){
161653 if( pSel ){
161654 if( 0==pSel->pWin || 0==sqlite3WindowCompare(0, pSel->pWin, pWin, 0) ){
161655 pWin->pNextWin = pSel->pWin;
161656 if( pSel->pWin ){
161657 pSel->pWin->ppThis = &pWin->pNextWin;
161658 }
161659 pSel->pWin = pWin;
161660 pWin->ppThis = &pSel->pWin;
161661 }else{
161662 if( sqlite3ExprListCompare(pWin->pPartition, pSel->pWin->pPartition,-1) ){
161663 pSel->selFlags |= SF_MultiPart0x2000000;
161664 }
161665 }
161666 }
161667}
161668
161669/*
161670** Return 0 if the two window objects are identical, 1 if they are
161671** different, or 2 if it cannot be determined if the objects are identical
161672** or not. Identical window objects can be processed in a single scan.
161673*/
161674SQLITE_PRIVATEstatic int sqlite3WindowCompare(
161675 const Parse *pParse,
161676 const Window *p1,
161677 const Window *p2,
161678 int bFilter
161679){
161680 int res;
161681 if( NEVER(p1==0)(p1==0) || NEVER(p2==0)(p2==0) ) return 1;
161682 if( p1->eFrmType!=p2->eFrmType ) return 1;
161683 if( p1->eStart!=p2->eStart ) return 1;
161684 if( p1->eEnd!=p2->eEnd ) return 1;
161685 if( p1->eExclude!=p2->eExclude ) return 1;
161686 if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1;
161687 if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1;
161688 if( (res = sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1)) ){
161689 return res;
161690 }
161691 if( (res = sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1)) ){
161692 return res;
161693 }
161694 if( bFilter ){
161695 if( (res = sqlite3ExprCompare(pParse, p1->pFilter, p2->pFilter, -1)) ){
161696 return res;
161697 }
161698 }
161699 return 0;
161700}
161701
161702
161703/*
161704** This is called by code in select.c before it calls sqlite3WhereBegin()
161705** to begin iterating through the sub-query results. It is used to allocate
161706** and initialize registers and cursors used by sqlite3WindowCodeStep().
161707*/
161708SQLITE_PRIVATEstatic void sqlite3WindowCodeInit(Parse *pParse, Select *pSelect){
161709 int nEphExpr = pSelect->pSrc->a[0].pSelect->pEList->nExpr;
161710 Window *pMWin = pSelect->pWin;
161711 Window *pWin;
161712 Vdbe *v = sqlite3GetVdbe(pParse);
161713
161714 sqlite3VdbeAddOp2(v, OP_OpenEphemeral118, pMWin->iEphCsr, nEphExpr);
161715 sqlite3VdbeAddOp2(v, OP_OpenDup115, pMWin->iEphCsr+1, pMWin->iEphCsr);
161716 sqlite3VdbeAddOp2(v, OP_OpenDup115, pMWin->iEphCsr+2, pMWin->iEphCsr);
161717 sqlite3VdbeAddOp2(v, OP_OpenDup115, pMWin->iEphCsr+3, pMWin->iEphCsr);
161718
161719 /* Allocate registers to use for PARTITION BY values, if any. Initialize
161720 ** said registers to NULL. */
161721 if( pMWin->pPartition ){
161722 int nExpr = pMWin->pPartition->nExpr;
161723 pMWin->regPart = pParse->nMem+1;
161724 pParse->nMem += nExpr;
161725 sqlite3VdbeAddOp3(v, OP_Null75, 0, pMWin->regPart, pMWin->regPart+nExpr-1);
161726 }
161727
161728 pMWin->regOne = ++pParse->nMem;
161729 sqlite3VdbeAddOp2(v, OP_Integer71, 1, pMWin->regOne);
161730
161731 if( pMWin->eExclude ){
161732 pMWin->regStartRowid = ++pParse->nMem;
161733 pMWin->regEndRowid = ++pParse->nMem;
161734 pMWin->csrApp = pParse->nTab++;
161735 sqlite3VdbeAddOp2(v, OP_Integer71, 1, pMWin->regStartRowid);
161736 sqlite3VdbeAddOp2(v, OP_Integer71, 0, pMWin->regEndRowid);
161737 sqlite3VdbeAddOp2(v, OP_OpenDup115, pMWin->csrApp, pMWin->iEphCsr);
161738 return;
161739 }
161740
161741 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
161742 FuncDef *p = pWin->pWFunc;
161743 if( (p->funcFlags & SQLITE_FUNC_MINMAX0x1000) && pWin->eStart!=TK_UNBOUNDED90 ){
161744 /* The inline versions of min() and max() require a single ephemeral
161745 ** table and 3 registers. The registers are used as follows:
161746 **
161747 ** regApp+0: slot to copy min()/max() argument to for MakeRecord
161748 ** regApp+1: integer value used to ensure keys are unique
161749 ** regApp+2: output of MakeRecord
161750 */
161751 ExprList *pList;
161752 KeyInfo *pKeyInfo;
161753 assert( ExprUseXList(pWin->pOwner) )((void) (0));
161754 pList = pWin->pOwner->x.pList;
161755 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList, 0, 0);
161756 pWin->csrApp = pParse->nTab++;
161757 pWin->regApp = pParse->nMem+1;
161758 pParse->nMem += 3;
161759 if( pKeyInfo && pWin->pWFunc->zName[1]=='i' ){
161760 assert( pKeyInfo->aSortFlags[0]==0 )((void) (0));
161761 pKeyInfo->aSortFlags[0] = KEYINFO_ORDER_DESC0x01;
161762 }
161763 sqlite3VdbeAddOp2(v, OP_OpenEphemeral118, pWin->csrApp, 2);
161764 sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO(-8));
161765 sqlite3VdbeAddOp2(v, OP_Integer71, 0, pWin->regApp+1);
161766 }
161767 else if( p->zName==nth_valueName || p->zName==first_valueName ){
161768 /* Allocate two registers at pWin->regApp. These will be used to
161769 ** store the start and end index of the current frame. */
161770 pWin->regApp = pParse->nMem+1;
161771 pWin->csrApp = pParse->nTab++;
161772 pParse->nMem += 2;
161773 sqlite3VdbeAddOp2(v, OP_OpenDup115, pWin->csrApp, pMWin->iEphCsr);
161774 }
161775 else if( p->zName==leadName || p->zName==lagName ){
161776 pWin->csrApp = pParse->nTab++;
161777 sqlite3VdbeAddOp2(v, OP_OpenDup115, pWin->csrApp, pMWin->iEphCsr);
161778 }
161779 }
161780}
161781
161782#define WINDOW_STARTING_INT0 0
161783#define WINDOW_ENDING_INT1 1
161784#define WINDOW_NTH_VALUE_INT2 2
161785#define WINDOW_STARTING_NUM3 3
161786#define WINDOW_ENDING_NUM4 4
161787
161788/*
161789** A "PRECEDING <expr>" (eCond==0) or "FOLLOWING <expr>" (eCond==1) or the
161790** value of the second argument to nth_value() (eCond==2) has just been
161791** evaluated and the result left in register reg. This function generates VM
161792** code to check that the value is a non-negative integer and throws an
161793** exception if it is not.
161794*/
161795static void windowCheckValue(Parse *pParse, int reg, int eCond){
161796 static const char *azErr[] = {
161797 "frame starting offset must be a non-negative integer",
161798 "frame ending offset must be a non-negative integer",
161799 "second argument to nth_value must be a positive integer",
161800 "frame starting offset must be a non-negative number",
161801 "frame ending offset must be a non-negative number",
161802 };
161803 static int aOp[] = { OP_Ge57, OP_Ge57, OP_Gt54, OP_Ge57, OP_Ge57 };
161804 Vdbe *v = sqlite3GetVdbe(pParse);
161805 int regZero = sqlite3GetTempReg(pParse);
161806 assert( eCond>=0 && eCond<ArraySize(azErr) )((void) (0));
161807 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regZero);
161808 if( eCond>=WINDOW_STARTING_NUM3 ){
161809 int regString = sqlite3GetTempReg(pParse);
161810 sqlite3VdbeAddOp4(v, OP_String8117, 0, regString, 0, "", P4_STATIC(-1));
161811 sqlite3VdbeAddOp3(v, OP_Ge57, regString, sqlite3VdbeCurrentAddr(v)+2, reg);
161812 sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC0x43|SQLITE_JUMPIFNULL0x10);
161813 VdbeCoverage(v);
161814 assert( eCond==3 || eCond==4 )((void) (0));
161815 VdbeCoverageIf(v, eCond==3);
161816 VdbeCoverageIf(v, eCond==4);
161817 }else{
161818 sqlite3VdbeAddOp2(v, OP_MustBeInt12, reg, sqlite3VdbeCurrentAddr(v)+2);
161819 VdbeCoverage(v);
161820 assert( eCond==0 || eCond==1 || eCond==2 )((void) (0));
161821 VdbeCoverageIf(v, eCond==0);
161822 VdbeCoverageIf(v, eCond==1);
161823 VdbeCoverageIf(v, eCond==2);
161824 }
161825 sqlite3VdbeAddOp3(v, aOp[eCond], regZero, sqlite3VdbeCurrentAddr(v)+2, reg);
161826 sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC0x43);
161827 VdbeCoverageNeverNullIf(v, eCond==0); /* NULL case captured by */
161828 VdbeCoverageNeverNullIf(v, eCond==1); /* the OP_MustBeInt */
161829 VdbeCoverageNeverNullIf(v, eCond==2);
161830 VdbeCoverageNeverNullIf(v, eCond==3); /* NULL case caught by */
161831 VdbeCoverageNeverNullIf(v, eCond==4); /* the OP_Ge */
161832 sqlite3MayAbort(pParse);
161833 sqlite3VdbeAddOp2(v, OP_Halt70, SQLITE_ERROR1, OE_Abort2);
161834 sqlite3VdbeAppendP4(v, (void*)azErr[eCond], P4_STATIC(-1));
161835 sqlite3ReleaseTempReg(pParse, regZero);
161836}
161837
161838/*
161839** Return the number of arguments passed to the window-function associated
161840** with the object passed as the only argument to this function.
161841*/
161842static int windowArgCount(Window *pWin){
161843 const ExprList *pList;
161844 assert( ExprUseXList(pWin->pOwner) )((void) (0));
161845 pList = pWin->pOwner->x.pList;
161846 return (pList ? pList->nExpr : 0);
161847}
161848
161849typedef struct WindowCodeArg WindowCodeArg;
161850typedef struct WindowCsrAndReg WindowCsrAndReg;
161851
161852/*
161853** See comments above struct WindowCodeArg.
161854*/
161855struct WindowCsrAndReg {
161856 int csr; /* Cursor number */
161857 int reg; /* First in array of peer values */
161858};
161859
161860/*
161861** A single instance of this structure is allocated on the stack by
161862** sqlite3WindowCodeStep() and a pointer to it passed to the various helper
161863** routines. This is to reduce the number of arguments required by each
161864** helper function.
161865**
161866** regArg:
161867** Each window function requires an accumulator register (just as an
161868** ordinary aggregate function does). This variable is set to the first
161869** in an array of accumulator registers - one for each window function
161870** in the WindowCodeArg.pMWin list.
161871**
161872** eDelete:
161873** The window functions implementation sometimes caches the input rows
161874** that it processes in a temporary table. If it is not zero, this
161875** variable indicates when rows may be removed from the temp table (in
161876** order to reduce memory requirements - it would always be safe just
161877** to leave them there). Possible values for eDelete are:
161878**
161879** WINDOW_RETURN_ROW:
161880** An input row can be discarded after it is returned to the caller.
161881**
161882** WINDOW_AGGINVERSE:
161883** An input row can be discarded after the window functions xInverse()
161884** callbacks have been invoked in it.
161885**
161886** WINDOW_AGGSTEP:
161887** An input row can be discarded after the window functions xStep()
161888** callbacks have been invoked in it.
161889**
161890** start,current,end
161891** Consider a window-frame similar to the following:
161892**
161893** (ORDER BY a, b GROUPS BETWEEN 2 PRECEDING AND 2 FOLLOWING)
161894**
161895** The windows functions implmentation caches the input rows in a temp
161896** table, sorted by "a, b" (it actually populates the cache lazily, and
161897** aggressively removes rows once they are no longer required, but that's
161898** a mere detail). It keeps three cursors open on the temp table. One
161899** (current) that points to the next row to return to the query engine
161900** once its window function values have been calculated. Another (end)
161901** points to the next row to call the xStep() method of each window function
161902** on (so that it is 2 groups ahead of current). And a third (start) that
161903** points to the next row to call the xInverse() method of each window
161904** function on.
161905**
161906** Each cursor (start, current and end) consists of a VDBE cursor
161907** (WindowCsrAndReg.csr) and an array of registers (starting at
161908** WindowCodeArg.reg) that always contains a copy of the peer values
161909** read from the corresponding cursor.
161910**
161911** Depending on the window-frame in question, all three cursors may not
161912** be required. In this case both WindowCodeArg.csr and reg are set to
161913** 0.
161914*/
161915struct WindowCodeArg {
161916 Parse *pParse; /* Parse context */
161917 Window *pMWin; /* First in list of functions being processed */
161918 Vdbe *pVdbe; /* VDBE object */
161919 int addrGosub; /* OP_Gosub to this address to return one row */
161920 int regGosub; /* Register used with OP_Gosub(addrGosub) */
161921 int regArg; /* First in array of accumulator registers */
161922 int eDelete; /* See above */
161923 int regRowid;
161924
161925 WindowCsrAndReg start;
161926 WindowCsrAndReg current;
161927 WindowCsrAndReg end;
161928};
161929
161930/*
161931** Generate VM code to read the window frames peer values from cursor csr into
161932** an array of registers starting at reg.
161933*/
161934static void windowReadPeerValues(
161935 WindowCodeArg *p,
161936 int csr,
161937 int reg
161938){
161939 Window *pMWin = p->pMWin;
161940 ExprList *pOrderBy = pMWin->pOrderBy;
161941 if( pOrderBy ){
161942 Vdbe *v = sqlite3GetVdbe(p->pParse);
161943 ExprList *pPart = pMWin->pPartition;
161944 int iColOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
161945 int i;
161946 for(i=0; i<pOrderBy->nExpr; i++){
161947 sqlite3VdbeAddOp3(v, OP_Column94, csr, iColOff+i, reg+i);
161948 }
161949 }
161950}
161951
161952/*
161953** Generate VM code to invoke either xStep() (if bInverse is 0) or
161954** xInverse (if bInverse is non-zero) for each window function in the
161955** linked list starting at pMWin. Or, for built-in window functions
161956** that do not use the standard function API, generate the required
161957** inline VM code.
161958**
161959** If argument csr is greater than or equal to 0, then argument reg is
161960** the first register in an array of registers guaranteed to be large
161961** enough to hold the array of arguments for each function. In this case
161962** the arguments are extracted from the current row of csr into the
161963** array of registers before invoking OP_AggStep or OP_AggInverse
161964**
161965** Or, if csr is less than zero, then the array of registers at reg is
161966** already populated with all columns from the current row of the sub-query.
161967**
161968** If argument regPartSize is non-zero, then it is a register containing the
161969** number of rows in the current partition.
161970*/
161971static void windowAggStep(
161972 WindowCodeArg *p,
161973 Window *pMWin, /* Linked list of window functions */
161974 int csr, /* Read arguments from this cursor */
161975 int bInverse, /* True to invoke xInverse instead of xStep */
161976 int reg /* Array of registers */
161977){
161978 Parse *pParse = p->pParse;
161979 Vdbe *v = sqlite3GetVdbe(pParse);
161980 Window *pWin;
161981 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
161982 FuncDef *pFunc = pWin->pWFunc;
161983 int regArg;
161984 int nArg = pWin->bExprArgs ? 0 : windowArgCount(pWin);
161985 int i;
161986
161987 assert( bInverse==0 || pWin->eStart!=TK_UNBOUNDED )((void) (0));
161988
161989 /* All OVER clauses in the same window function aggregate step must
161990 ** be the same. */
161991 assert( pWin==pMWin || sqlite3WindowCompare(pParse,pWin,pMWin,0)!=1 )((void) (0));
161992
161993 for(i=0; i<nArg; i++){
161994 if( i!=1 || pFunc->zName!=nth_valueName ){
161995 sqlite3VdbeAddOp3(v, OP_Column94, csr, pWin->iArgCol+i, reg+i);
161996 }else{
161997 sqlite3VdbeAddOp3(v, OP_Column94, pMWin->iEphCsr, pWin->iArgCol+i, reg+i);
161998 }
161999 }
162000 regArg = reg;
162001
162002 if( pMWin->regStartRowid==0
162003 && (pFunc->funcFlags & SQLITE_FUNC_MINMAX0x1000)
162004 && (pWin->eStart!=TK_UNBOUNDED90)
162005 ){
162006 int addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull50, regArg);
162007 VdbeCoverage(v);
162008 if( bInverse==0 ){
162009 sqlite3VdbeAddOp2(v, OP_AddImm86, pWin->regApp+1, 1);
162010 sqlite3VdbeAddOp2(v, OP_SCopy81, regArg, pWin->regApp);
162011 sqlite3VdbeAddOp3(v, OP_MakeRecord97, pWin->regApp, 2, pWin->regApp+2);
162012 sqlite3VdbeAddOp2(v, OP_IdxInsert138, pWin->csrApp, pWin->regApp+2);
162013 }else{
162014 sqlite3VdbeAddOp4Int(v, OP_SeekGE22, pWin->csrApp, 0, regArg, 1);
162015 VdbeCoverageNeverTaken(v);
162016 sqlite3VdbeAddOp1(v, OP_Delete130, pWin->csrApp);
162017 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
162018 }
162019 sqlite3VdbeJumpHere(v, addrIsNull);
162020 }else if( pWin->regApp ){
162021 assert( pFunc->zName==nth_valueName((void) (0))
162022 || pFunc->zName==first_valueName((void) (0))
162023 )((void) (0));
162024 assert( bInverse==0 || bInverse==1 )((void) (0));
162025 sqlite3VdbeAddOp2(v, OP_AddImm86, pWin->regApp+1-bInverse, 1);
162026 }else if( pFunc->xSFunc!=noopStepFunc ){
162027 int addrIf = 0;
162028 if( pWin->pFilter ){
162029 int regTmp;
162030 assert( ExprUseXList(pWin->pOwner) )((void) (0));
162031 assert( pWin->bExprArgs || !nArg ||nArg==pWin->pOwner->x.pList->nExpr )((void) (0));
162032 assert( pWin->bExprArgs || nArg ||pWin->pOwner->x.pList==0 )((void) (0));
162033 regTmp = sqlite3GetTempReg(pParse);
162034 sqlite3VdbeAddOp3(v, OP_Column94, csr, pWin->iArgCol+nArg,regTmp);
162035 addrIf = sqlite3VdbeAddOp3(v, OP_IfNot16, regTmp, 0, 1);
162036 VdbeCoverage(v);
162037 sqlite3ReleaseTempReg(pParse, regTmp);
162038 }
162039
162040 if( pWin->bExprArgs ){
162041 int iOp = sqlite3VdbeCurrentAddr(v);
162042 int iEnd;
162043
162044 assert( ExprUseXList(pWin->pOwner) )((void) (0));
162045 nArg = pWin->pOwner->x.pList->nExpr;
162046 regArg = sqlite3GetTempRange(pParse, nArg);
162047 sqlite3ExprCodeExprList(pParse, pWin->pOwner->x.pList, regArg, 0, 0);
162048
162049 for(iEnd=sqlite3VdbeCurrentAddr(v); iOp<iEnd; iOp++){
162050 VdbeOp *pOp = sqlite3VdbeGetOp(v, iOp);
162051 if( pOp->opcode==OP_Column94 && pOp->p1==pMWin->iEphCsr ){
162052 pOp->p1 = csr;
162053 }
162054 }
162055 }
162056 if( pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL0x0020 ){
162057 CollSeq *pColl;
162058 assert( nArg>0 )((void) (0));
162059 assert( ExprUseXList(pWin->pOwner) )((void) (0));
162060 pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
162061 sqlite3VdbeAddOp4(v, OP_CollSeq85, 0,0,0, (const char*)pColl, P4_COLLSEQ(-2));
162062 }
162063 sqlite3VdbeAddOp3(v, bInverse? OP_AggInverse161 : OP_AggStep162,
162064 bInverse, regArg, pWin->regAccum);
162065 sqlite3VdbeAppendP4(v, pFunc, P4_FUNCDEF(-7));
162066 sqlite3VdbeChangeP5(v, (u8)nArg);
162067 if( pWin->bExprArgs ){
162068 sqlite3ReleaseTempRange(pParse, regArg, nArg);
162069 }
162070 if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
162071 }
162072 }
162073}
162074
162075/*
162076** Values that may be passed as the second argument to windowCodeOp().
162077*/
162078#define WINDOW_RETURN_ROW1 1
162079#define WINDOW_AGGINVERSE2 2
162080#define WINDOW_AGGSTEP3 3
162081
162082/*
162083** Generate VM code to invoke either xValue() (bFin==0) or xFinalize()
162084** (bFin==1) for each window function in the linked list starting at
162085** pMWin. Or, for built-in window-functions that do not use the standard
162086** API, generate the equivalent VM code.
162087*/
162088static void windowAggFinal(WindowCodeArg *p, int bFin){
162089 Parse *pParse = p->pParse;
162090 Window *pMWin = p->pMWin;
162091 Vdbe *v = sqlite3GetVdbe(pParse);
162092 Window *pWin;
162093
162094 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
162095 if( pMWin->regStartRowid==0
162096 && (pWin->pWFunc->funcFlags & SQLITE_FUNC_MINMAX0x1000)
162097 && (pWin->eStart!=TK_UNBOUNDED90)
162098 ){
162099 sqlite3VdbeAddOp2(v, OP_Null75, 0, pWin->regResult);
162100 sqlite3VdbeAddOp1(v, OP_Last31, pWin->csrApp);
162101 VdbeCoverage(v);
162102 sqlite3VdbeAddOp3(v, OP_Column94, pWin->csrApp, 0, pWin->regResult);
162103 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
162104 }else if( pWin->regApp ){
162105 assert( pMWin->regStartRowid==0 )((void) (0));
162106 }else{
162107 int nArg = windowArgCount(pWin);
162108 if( bFin ){
162109 sqlite3VdbeAddOp2(v, OP_AggFinal165, pWin->regAccum, nArg);
162110 sqlite3VdbeAppendP4(v, pWin->pWFunc, P4_FUNCDEF(-7));
162111 sqlite3VdbeAddOp2(v, OP_Copy80, pWin->regAccum, pWin->regResult);
162112 sqlite3VdbeAddOp2(v, OP_Null75, 0, pWin->regAccum);
162113 }else{
162114 sqlite3VdbeAddOp3(v, OP_AggValue164,pWin->regAccum,nArg,pWin->regResult);
162115 sqlite3VdbeAppendP4(v, pWin->pWFunc, P4_FUNCDEF(-7));
162116 }
162117 }
162118 }
162119}
162120
162121/*
162122** Generate code to calculate the current values of all window functions in the
162123** p->pMWin list by doing a full scan of the current window frame. Store the
162124** results in the Window.regResult registers, ready to return the upper
162125** layer.
162126*/
162127static void windowFullScan(WindowCodeArg *p){
162128 Window *pWin;
162129 Parse *pParse = p->pParse;
162130 Window *pMWin = p->pMWin;
162131 Vdbe *v = p->pVdbe;
162132
162133 int regCRowid = 0; /* Current rowid value */
162134 int regCPeer = 0; /* Current peer values */
162135 int regRowid = 0; /* AggStep rowid value */
162136 int regPeer = 0; /* AggStep peer values */
162137
162138 int nPeer;
162139 int lblNext;
162140 int lblBrk;
162141 int addrNext;
162142 int csr;
162143
162144 VdbeModuleComment((v, "windowFullScan begin"));
162145
162146 assert( pMWin!=0 )((void) (0));
162147 csr = pMWin->csrApp;
162148 nPeer = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
162149
162150 lblNext = sqlite3VdbeMakeLabel(pParse);
162151 lblBrk = sqlite3VdbeMakeLabel(pParse);
162152
162153 regCRowid = sqlite3GetTempReg(pParse);
162154 regRowid = sqlite3GetTempReg(pParse);
162155 if( nPeer ){
162156 regCPeer = sqlite3GetTempRange(pParse, nPeer);
162157 regPeer = sqlite3GetTempRange(pParse, nPeer);
162158 }
162159
162160 sqlite3VdbeAddOp2(v, OP_Rowid135, pMWin->iEphCsr, regCRowid);
162161 windowReadPeerValues(p, pMWin->iEphCsr, regCPeer);
162162
162163 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
162164 sqlite3VdbeAddOp2(v, OP_Null75, 0, pWin->regAccum);
162165 }
162166
162167 sqlite3VdbeAddOp3(v, OP_SeekGE22, csr, lblBrk, pMWin->regStartRowid);
162168 VdbeCoverage(v);
162169 addrNext = sqlite3VdbeCurrentAddr(v);
162170 sqlite3VdbeAddOp2(v, OP_Rowid135, csr, regRowid);
162171 sqlite3VdbeAddOp3(v, OP_Gt54, pMWin->regEndRowid, lblBrk, regRowid);
162172 VdbeCoverageNeverNull(v);
162173
162174 if( pMWin->eExclude==TK_CURRENT85 ){
162175 sqlite3VdbeAddOp3(v, OP_Eq53, regCRowid, lblNext, regRowid);
162176 VdbeCoverageNeverNull(v);
162177 }else if( pMWin->eExclude!=TK_NO66 ){
162178 int addr;
162179 int addrEq = 0;
162180 KeyInfo *pKeyInfo = 0;
162181
162182 if( pMWin->pOrderBy ){
162183 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pMWin->pOrderBy, 0, 0);
162184 }
162185 if( pMWin->eExclude==TK_TIES94 ){
162186 addrEq = sqlite3VdbeAddOp3(v, OP_Eq53, regCRowid, 0, regRowid);
162187 VdbeCoverageNeverNull(v);
162188 }
162189 if( pKeyInfo ){
162190 windowReadPeerValues(p, csr, regPeer);
162191 sqlite3VdbeAddOp3(v, OP_Compare90, regPeer, regCPeer, nPeer);
162192 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO(-8));
162193 addr = sqlite3VdbeCurrentAddr(v)+1;
162194 sqlite3VdbeAddOp3(v, OP_Jump13, addr, lblNext, addr);
162195 VdbeCoverageEqNe(v);
162196 }else{
162197 sqlite3VdbeAddOp2(v, OP_Goto8, 0, lblNext);
162198 }
162199 if( addrEq ) sqlite3VdbeJumpHere(v, addrEq);
162200 }
162201
162202 windowAggStep(p, pMWin, csr, 0, p->regArg);
162203
162204 sqlite3VdbeResolveLabel(v, lblNext);
162205 sqlite3VdbeAddOp2(v, OP_Next38, csr, addrNext);
162206 VdbeCoverage(v);
162207 sqlite3VdbeJumpHere(v, addrNext-1);
162208 sqlite3VdbeJumpHere(v, addrNext+1);
162209 sqlite3ReleaseTempReg(pParse, regRowid);
162210 sqlite3ReleaseTempReg(pParse, regCRowid);
162211 if( nPeer ){
162212 sqlite3ReleaseTempRange(pParse, regPeer, nPeer);
162213 sqlite3ReleaseTempRange(pParse, regCPeer, nPeer);
162214 }
162215
162216 windowAggFinal(p, 1);
162217 VdbeModuleComment((v, "windowFullScan end"));
162218}
162219
162220/*
162221** Invoke the sub-routine at regGosub (generated by code in select.c) to
162222** return the current row of Window.iEphCsr. If all window functions are
162223** aggregate window functions that use the standard API, a single
162224** OP_Gosub instruction is all that this routine generates. Extra VM code
162225** for per-row processing is only generated for the following built-in window
162226** functions:
162227**
162228** nth_value()
162229** first_value()
162230** lag()
162231** lead()
162232*/
162233static void windowReturnOneRow(WindowCodeArg *p){
162234 Window *pMWin = p->pMWin;
162235 Vdbe *v = p->pVdbe;
162236
162237 if( pMWin->regStartRowid ){
162238 windowFullScan(p);
162239 }else{
162240 Parse *pParse = p->pParse;
162241 Window *pWin;
162242
162243 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
162244 FuncDef *pFunc = pWin->pWFunc;
162245 assert( ExprUseXList(pWin->pOwner) )((void) (0));
162246 if( pFunc->zName==nth_valueName
162247 || pFunc->zName==first_valueName
162248 ){
162249 int csr = pWin->csrApp;
162250 int lbl = sqlite3VdbeMakeLabel(pParse);
162251 int tmpReg = sqlite3GetTempReg(pParse);
162252 sqlite3VdbeAddOp2(v, OP_Null75, 0, pWin->regResult);
162253
162254 if( pFunc->zName==nth_valueName ){
162255 sqlite3VdbeAddOp3(v, OP_Column94,pMWin->iEphCsr,pWin->iArgCol+1,tmpReg);
162256 windowCheckValue(pParse, tmpReg, 2);
162257 }else{
162258 sqlite3VdbeAddOp2(v, OP_Integer71, 1, tmpReg);
162259 }
162260 sqlite3VdbeAddOp3(v, OP_Add106, tmpReg, pWin->regApp, tmpReg);
162261 sqlite3VdbeAddOp3(v, OP_Gt54, pWin->regApp+1, lbl, tmpReg);
162262 VdbeCoverageNeverNull(v);
162263 sqlite3VdbeAddOp3(v, OP_SeekRowid29, csr, 0, tmpReg);
162264 VdbeCoverageNeverTaken(v);
162265 sqlite3VdbeAddOp3(v, OP_Column94, csr, pWin->iArgCol, pWin->regResult);
162266 sqlite3VdbeResolveLabel(v, lbl);
162267 sqlite3ReleaseTempReg(pParse, tmpReg);
162268 }
162269 else if( pFunc->zName==leadName || pFunc->zName==lagName ){
162270 int nArg = pWin->pOwner->x.pList->nExpr;
162271 int csr = pWin->csrApp;
162272 int lbl = sqlite3VdbeMakeLabel(pParse);
162273 int tmpReg = sqlite3GetTempReg(pParse);
162274 int iEph = pMWin->iEphCsr;
162275
162276 if( nArg<3 ){
162277 sqlite3VdbeAddOp2(v, OP_Null75, 0, pWin->regResult);
162278 }else{
162279 sqlite3VdbeAddOp3(v, OP_Column94, iEph,pWin->iArgCol+2,pWin->regResult);
162280 }
162281 sqlite3VdbeAddOp2(v, OP_Rowid135, iEph, tmpReg);
162282 if( nArg<2 ){
162283 int val = (pFunc->zName==leadName ? 1 : -1);
162284 sqlite3VdbeAddOp2(v, OP_AddImm86, tmpReg, val);
162285 }else{
162286 int op = (pFunc->zName==leadName ? OP_Add106 : OP_Subtract107);
162287 int tmpReg2 = sqlite3GetTempReg(pParse);
162288 sqlite3VdbeAddOp3(v, OP_Column94, iEph, pWin->iArgCol+1, tmpReg2);
162289 sqlite3VdbeAddOp3(v, op, tmpReg2, tmpReg, tmpReg);
162290 sqlite3ReleaseTempReg(pParse, tmpReg2);
162291 }
162292
162293 sqlite3VdbeAddOp3(v, OP_SeekRowid29, csr, lbl, tmpReg);
162294 VdbeCoverage(v);
162295 sqlite3VdbeAddOp3(v, OP_Column94, csr, pWin->iArgCol, pWin->regResult);
162296 sqlite3VdbeResolveLabel(v, lbl);
162297 sqlite3ReleaseTempReg(pParse, tmpReg);
162298 }
162299 }
162300 }
162301 sqlite3VdbeAddOp2(v, OP_Gosub9, p->regGosub, p->addrGosub);
162302}
162303
162304/*
162305** Generate code to set the accumulator register for each window function
162306** in the linked list passed as the second argument to NULL. And perform
162307** any equivalent initialization required by any built-in window functions
162308** in the list.
162309*/
162310static int windowInitAccum(Parse *pParse, Window *pMWin){
162311 Vdbe *v = sqlite3GetVdbe(pParse);
162312 int regArg;
162313 int nArg = 0;
162314 Window *pWin;
162315 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
162316 FuncDef *pFunc = pWin->pWFunc;
162317 assert( pWin->regAccum )((void) (0));
162318 sqlite3VdbeAddOp2(v, OP_Null75, 0, pWin->regAccum);
162319 nArg = MAX(nArg, windowArgCount(pWin))((nArg)>(windowArgCount(pWin))?(nArg):(windowArgCount(pWin
)))
;
162320 if( pMWin->regStartRowid==0 ){
162321 if( pFunc->zName==nth_valueName || pFunc->zName==first_valueName ){
162322 sqlite3VdbeAddOp2(v, OP_Integer71, 0, pWin->regApp);
162323 sqlite3VdbeAddOp2(v, OP_Integer71, 0, pWin->regApp+1);
162324 }
162325
162326 if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX0x1000) && pWin->csrApp ){
162327 assert( pWin->eStart!=TK_UNBOUNDED )((void) (0));
162328 sqlite3VdbeAddOp1(v, OP_ResetSorter146, pWin->csrApp);
162329 sqlite3VdbeAddOp2(v, OP_Integer71, 0, pWin->regApp+1);
162330 }
162331 }
162332 }
162333 regArg = pParse->nMem+1;
162334 pParse->nMem += nArg;
162335 return regArg;
162336}
162337
162338/*
162339** Return true if the current frame should be cached in the ephemeral table,
162340** even if there are no xInverse() calls required.
162341*/
162342static int windowCacheFrame(Window *pMWin){
162343 Window *pWin;
162344 if( pMWin->regStartRowid ) return 1;
162345 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
162346 FuncDef *pFunc = pWin->pWFunc;
162347 if( (pFunc->zName==nth_valueName)
162348 || (pFunc->zName==first_valueName)
162349 || (pFunc->zName==leadName)
162350 || (pFunc->zName==lagName)
162351 ){
162352 return 1;
162353 }
162354 }
162355 return 0;
162356}
162357
162358/*
162359** regOld and regNew are each the first register in an array of size
162360** pOrderBy->nExpr. This function generates code to compare the two
162361** arrays of registers using the collation sequences and other comparison
162362** parameters specified by pOrderBy.
162363**
162364** If the two arrays are not equal, the contents of regNew is copied to
162365** regOld and control falls through. Otherwise, if the contents of the arrays
162366** are equal, an OP_Goto is executed. The address of the OP_Goto is returned.
162367*/
162368static void windowIfNewPeer(
162369 Parse *pParse,
162370 ExprList *pOrderBy,
162371 int regNew, /* First in array of new values */
162372 int regOld, /* First in array of old values */
162373 int addr /* Jump here */
162374){
162375 Vdbe *v = sqlite3GetVdbe(pParse);
162376 if( pOrderBy ){
162377 int nVal = pOrderBy->nExpr;
162378 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
162379 sqlite3VdbeAddOp3(v, OP_Compare90, regOld, regNew, nVal);
162380 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO(-8));
162381 sqlite3VdbeAddOp3(v, OP_Jump13,
162382 sqlite3VdbeCurrentAddr(v)+1, addr, sqlite3VdbeCurrentAddr(v)+1
162383 );
162384 VdbeCoverageEqNe(v);
162385 sqlite3VdbeAddOp3(v, OP_Copy80, regNew, regOld, nVal-1);
162386 }else{
162387 sqlite3VdbeAddOp2(v, OP_Goto8, 0, addr);
162388 }
162389}
162390
162391/*
162392** This function is called as part of generating VM programs for RANGE
162393** offset PRECEDING/FOLLOWING frame boundaries. Assuming "ASC" order for
162394** the ORDER BY term in the window, and that argument op is OP_Ge, it generates
162395** code equivalent to:
162396**
162397** if( csr1.peerVal + regVal >= csr2.peerVal ) goto lbl;
162398**
162399** The value of parameter op may also be OP_Gt or OP_Le. In these cases the
162400** operator in the above pseudo-code is replaced with ">" or "<=", respectively.
162401**
162402** If the sort-order for the ORDER BY term in the window is DESC, then the
162403** comparison is reversed. Instead of adding regVal to csr1.peerVal, it is
162404** subtracted. And the comparison operator is inverted to - ">=" becomes "<=",
162405** ">" becomes "<", and so on. So, with DESC sort order, if the argument op
162406** is OP_Ge, the generated code is equivalent to:
162407**
162408** if( csr1.peerVal - regVal <= csr2.peerVal ) goto lbl;
162409**
162410** A special type of arithmetic is used such that if csr1.peerVal is not
162411** a numeric type (real or integer), then the result of the addition
162412** or subtraction is a a copy of csr1.peerVal.
162413*/
162414static void windowCodeRangeTest(
162415 WindowCodeArg *p,
162416 int op, /* OP_Ge, OP_Gt, or OP_Le */
162417 int csr1, /* Cursor number for cursor 1 */
162418 int regVal, /* Register containing non-negative number */
162419 int csr2, /* Cursor number for cursor 2 */
162420 int lbl /* Jump destination if condition is true */
162421){
162422 Parse *pParse = p->pParse;
162423 Vdbe *v = sqlite3GetVdbe(pParse);
162424 ExprList *pOrderBy = p->pMWin->pOrderBy; /* ORDER BY clause for window */
162425 int reg1 = sqlite3GetTempReg(pParse); /* Reg. for csr1.peerVal+regVal */
162426 int reg2 = sqlite3GetTempReg(pParse); /* Reg. for csr2.peerVal */
162427 int regString = ++pParse->nMem; /* Reg. for constant value '' */
162428 int arith = OP_Add106; /* OP_Add or OP_Subtract */
162429 int addrGe; /* Jump destination */
162430 int addrDone = sqlite3VdbeMakeLabel(pParse); /* Address past OP_Ge */
162431 CollSeq *pColl;
162432
162433 /* Read the peer-value from each cursor into a register */
162434 windowReadPeerValues(p, csr1, reg1);
162435 windowReadPeerValues(p, csr2, reg2);
162436
162437 assert( op==OP_Ge || op==OP_Gt || op==OP_Le )((void) (0));
162438 assert( pOrderBy && pOrderBy->nExpr==1 )((void) (0));
162439 if( pOrderBy->a[0].fg.sortFlags & KEYINFO_ORDER_DESC0x01 ){
162440 switch( op ){
162441 case OP_Ge57: op = OP_Le55; break;
162442 case OP_Gt54: op = OP_Lt56; break;
162443 default: assert( op==OP_Le )((void) (0)); op = OP_Ge57; break;
162444 }
162445 arith = OP_Subtract107;
162446 }
162447
162448 VdbeModuleComment((v, "CodeRangeTest: if( R%d %s R%d %s R%d ) goto lbl",
162449 reg1, (arith==OP_Add ? "+" : "-"), regVal,
162450 ((op==OP_Ge) ? ">=" : (op==OP_Le) ? "<=" : (op==OP_Gt) ? ">" : "<"), reg2
162451 ));
162452
162453 /* If the BIGNULL flag is set for the ORDER BY, then it is required to
162454 ** consider NULL values to be larger than all other values, instead of
162455 ** the usual smaller. The VDBE opcodes OP_Ge and so on do not handle this
162456 ** (and adding that capability causes a performance regression), so
162457 ** instead if the BIGNULL flag is set then cases where either reg1 or
162458 ** reg2 are NULL are handled separately in the following block. The code
162459 ** generated is equivalent to:
162460 **
162461 ** if( reg1 IS NULL ){
162462 ** if( op==OP_Ge ) goto lbl;
162463 ** if( op==OP_Gt && reg2 IS NOT NULL ) goto lbl;
162464 ** if( op==OP_Le && reg2 IS NULL ) goto lbl;
162465 ** }else if( reg2 IS NULL ){
162466 ** if( op==OP_Le ) goto lbl;
162467 ** }
162468 **
162469 ** Additionally, if either reg1 or reg2 are NULL but the jump to lbl is
162470 ** not taken, control jumps over the comparison operator coded below this
162471 ** block. */
162472 if( pOrderBy->a[0].fg.sortFlags & KEYINFO_ORDER_BIGNULL0x02 ){
162473 /* This block runs if reg1 contains a NULL. */
162474 int addr = sqlite3VdbeAddOp1(v, OP_NotNull51, reg1); VdbeCoverage(v);
162475 switch( op ){
162476 case OP_Ge57:
162477 sqlite3VdbeAddOp2(v, OP_Goto8, 0, lbl);
162478 break;
162479 case OP_Gt54:
162480 sqlite3VdbeAddOp2(v, OP_NotNull51, reg2, lbl);
162481 VdbeCoverage(v);
162482 break;
162483 case OP_Le55:
162484 sqlite3VdbeAddOp2(v, OP_IsNull50, reg2, lbl);
162485 VdbeCoverage(v);
162486 break;
162487 default: assert( op==OP_Lt )((void) (0)); /* no-op */ break;
162488 }
162489 sqlite3VdbeAddOp2(v, OP_Goto8, 0, addrDone);
162490
162491 /* This block runs if reg1 is not NULL, but reg2 is. */
162492 sqlite3VdbeJumpHere(v, addr);
162493 sqlite3VdbeAddOp2(v, OP_IsNull50, reg2, lbl); VdbeCoverage(v);
162494 if( op==OP_Gt54 || op==OP_Ge57 ){
162495 sqlite3VdbeChangeP2(v, -1, addrDone);
162496 }
162497 }
162498
162499 /* Register reg1 currently contains csr1.peerVal (the peer-value from csr1).
162500 ** This block adds (or subtracts for DESC) the numeric value in regVal
162501 ** from it. Or, if reg1 is not numeric (it is a NULL, a text value or a blob),
162502 ** then leave reg1 as it is. In pseudo-code, this is implemented as:
162503 **
162504 ** if( reg1>='' ) goto addrGe;
162505 ** reg1 = reg1 +/- regVal
162506 ** addrGe:
162507 **
162508 ** Since all strings and blobs are greater-than-or-equal-to an empty string,
162509 ** the add/subtract is skipped for these, as required. If reg1 is a NULL,
162510 ** then the arithmetic is performed, but since adding or subtracting from
162511 ** NULL is always NULL anyway, this case is handled as required too. */
162512 sqlite3VdbeAddOp4(v, OP_String8117, 0, regString, 0, "", P4_STATIC(-1));
162513 addrGe = sqlite3VdbeAddOp3(v, OP_Ge57, regString, 0, reg1);
162514 VdbeCoverage(v);
162515 if( (op==OP_Ge57 && arith==OP_Add106) || (op==OP_Le55 && arith==OP_Subtract107) ){
162516 sqlite3VdbeAddOp3(v, op, reg2, lbl, reg1); VdbeCoverage(v);
162517 }
162518 sqlite3VdbeAddOp3(v, arith, regVal, reg1, reg1);
162519 sqlite3VdbeJumpHere(v, addrGe);
162520
162521 /* Compare registers reg2 and reg1, taking the jump if required. Note that
162522 ** control skips over this test if the BIGNULL flag is set and either
162523 ** reg1 or reg2 contain a NULL value. */
162524 sqlite3VdbeAddOp3(v, op, reg2, lbl, reg1); VdbeCoverage(v);
162525 pColl = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[0].pExpr);
162526 sqlite3VdbeAppendP4(v, (void*)pColl, P4_COLLSEQ(-2));
162527 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ0x80);
162528 sqlite3VdbeResolveLabel(v, addrDone);
162529
162530 assert( op==OP_Ge || op==OP_Gt || op==OP_Lt || op==OP_Le )((void) (0));
162531 testcase(op==OP_Ge); VdbeCoverageIf(v, op==OP_Ge);
162532 testcase(op==OP_Lt); VdbeCoverageIf(v, op==OP_Lt);
162533 testcase(op==OP_Le); VdbeCoverageIf(v, op==OP_Le);
162534 testcase(op==OP_Gt); VdbeCoverageIf(v, op==OP_Gt);
162535 sqlite3ReleaseTempReg(pParse, reg1);
162536 sqlite3ReleaseTempReg(pParse, reg2);
162537
162538 VdbeModuleComment((v, "CodeRangeTest: end"));
162539}
162540
162541/*
162542** Helper function for sqlite3WindowCodeStep(). Each call to this function
162543** generates VM code for a single RETURN_ROW, AGGSTEP or AGGINVERSE
162544** operation. Refer to the header comment for sqlite3WindowCodeStep() for
162545** details.
162546*/
162547static int windowCodeOp(
162548 WindowCodeArg *p, /* Context object */
162549 int op, /* WINDOW_RETURN_ROW, AGGSTEP or AGGINVERSE */
162550 int regCountdown, /* Register for OP_IfPos countdown */
162551 int jumpOnEof /* Jump here if stepped cursor reaches EOF */
162552){
162553 int csr, reg;
162554 Parse *pParse = p->pParse;
162555 Window *pMWin = p->pMWin;
162556 int ret = 0;
162557 Vdbe *v = p->pVdbe;
162558 int addrContinue = 0;
162559 int bPeer = (pMWin->eFrmType!=TK_ROWS76);
162560
162561 int lblDone = sqlite3VdbeMakeLabel(pParse);
162562 int addrNextRange = 0;
162563
162564 /* Special case - WINDOW_AGGINVERSE is always a no-op if the frame
162565 ** starts with UNBOUNDED PRECEDING. */
162566 if( op==WINDOW_AGGINVERSE2 && pMWin->eStart==TK_UNBOUNDED90 ){
162567 assert( regCountdown==0 && jumpOnEof==0 )((void) (0));
162568 return 0;
162569 }
162570
162571 if( regCountdown>0 ){
162572 if( pMWin->eFrmType==TK_RANGE89 ){
162573 addrNextRange = sqlite3VdbeCurrentAddr(v);
162574 assert( op==WINDOW_AGGINVERSE || op==WINDOW_AGGSTEP )((void) (0));
162575 if( op==WINDOW_AGGINVERSE2 ){
162576 if( pMWin->eStart==TK_FOLLOWING86 ){
162577 windowCodeRangeTest(
162578 p, OP_Le55, p->current.csr, regCountdown, p->start.csr, lblDone
162579 );
162580 }else{
162581 windowCodeRangeTest(
162582 p, OP_Ge57, p->start.csr, regCountdown, p->current.csr, lblDone
162583 );
162584 }
162585 }else{
162586 windowCodeRangeTest(
162587 p, OP_Gt54, p->end.csr, regCountdown, p->current.csr, lblDone
162588 );
162589 }
162590 }else{
162591 sqlite3VdbeAddOp3(v, OP_IfPos49, regCountdown, lblDone, 1);
162592 VdbeCoverage(v);
162593 }
162594 }
162595
162596 if( op==WINDOW_RETURN_ROW1 && pMWin->regStartRowid==0 ){
162597 windowAggFinal(p, 0);
162598 }
162599 addrContinue = sqlite3VdbeCurrentAddr(v);
162600
162601 /* If this is a (RANGE BETWEEN a FOLLOWING AND b FOLLOWING) or
162602 ** (RANGE BETWEEN b PRECEDING AND a PRECEDING) frame, ensure the
162603 ** start cursor does not advance past the end cursor within the
162604 ** temporary table. It otherwise might, if (a>b). Also ensure that,
162605 ** if the input cursor is still finding new rows, that the end
162606 ** cursor does not go past it to EOF. */
162607 if( pMWin->eStart==pMWin->eEnd && regCountdown
162608 && pMWin->eFrmType==TK_RANGE89
162609 ){
162610 int regRowid1 = sqlite3GetTempReg(pParse);
162611 int regRowid2 = sqlite3GetTempReg(pParse);
162612 if( op==WINDOW_AGGINVERSE2 ){
162613 sqlite3VdbeAddOp2(v, OP_Rowid135, p->start.csr, regRowid1);
162614 sqlite3VdbeAddOp2(v, OP_Rowid135, p->end.csr, regRowid2);
162615 sqlite3VdbeAddOp3(v, OP_Ge57, regRowid2, lblDone, regRowid1);
162616 VdbeCoverage(v);
162617 }else if( p->regRowid ){
162618 sqlite3VdbeAddOp2(v, OP_Rowid135, p->end.csr, regRowid1);
162619 sqlite3VdbeAddOp3(v, OP_Ge57, p->regRowid, lblDone, regRowid1);
162620 VdbeCoverageNeverNull(v);
162621 }
162622 sqlite3ReleaseTempReg(pParse, regRowid1);
162623 sqlite3ReleaseTempReg(pParse, regRowid2);
162624 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING )((void) (0));
162625 }
162626
162627 switch( op ){
162628 case WINDOW_RETURN_ROW1:
162629 csr = p->current.csr;
162630 reg = p->current.reg;
162631 windowReturnOneRow(p);
162632 break;
162633
162634 case WINDOW_AGGINVERSE2:
162635 csr = p->start.csr;
162636 reg = p->start.reg;
162637 if( pMWin->regStartRowid ){
162638 assert( pMWin->regEndRowid )((void) (0));
162639 sqlite3VdbeAddOp2(v, OP_AddImm86, pMWin->regStartRowid, 1);
162640 }else{
162641 windowAggStep(p, pMWin, csr, 1, p->regArg);
162642 }
162643 break;
162644
162645 default:
162646 assert( op==WINDOW_AGGSTEP )((void) (0));
162647 csr = p->end.csr;
162648 reg = p->end.reg;
162649 if( pMWin->regStartRowid ){
162650 assert( pMWin->regEndRowid )((void) (0));
162651 sqlite3VdbeAddOp2(v, OP_AddImm86, pMWin->regEndRowid, 1);
162652 }else{
162653 windowAggStep(p, pMWin, csr, 0, p->regArg);
162654 }
162655 break;
162656 }
162657
162658 if( op==p->eDelete ){
162659 sqlite3VdbeAddOp1(v, OP_Delete130, csr);
162660 sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION0x02);
162661 }
162662
162663 if( jumpOnEof ){
162664 sqlite3VdbeAddOp2(v, OP_Next38, csr, sqlite3VdbeCurrentAddr(v)+2);
162665 VdbeCoverage(v);
162666 ret = sqlite3VdbeAddOp0(v, OP_Goto8);
162667 }else{
162668 sqlite3VdbeAddOp2(v, OP_Next38, csr, sqlite3VdbeCurrentAddr(v)+1+bPeer);
162669 VdbeCoverage(v);
162670 if( bPeer ){
162671 sqlite3VdbeAddOp2(v, OP_Goto8, 0, lblDone);
162672 }
162673 }
162674
162675 if( bPeer ){
162676 int nReg = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
162677 int regTmp = (nReg ? sqlite3GetTempRange(pParse, nReg) : 0);
162678 windowReadPeerValues(p, csr, regTmp);
162679 windowIfNewPeer(pParse, pMWin->pOrderBy, regTmp, reg, addrContinue);
162680 sqlite3ReleaseTempRange(pParse, regTmp, nReg);
162681 }
162682
162683 if( addrNextRange ){
162684 sqlite3VdbeAddOp2(v, OP_Goto8, 0, addrNextRange);
162685 }
162686 sqlite3VdbeResolveLabel(v, lblDone);
162687 return ret;
162688}
162689
162690
162691/*
162692** Allocate and return a duplicate of the Window object indicated by the
162693** third argument. Set the Window.pOwner field of the new object to
162694** pOwner.
162695*/
162696SQLITE_PRIVATEstatic Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p){
162697 Window *pNew = 0;
162698 if( ALWAYS(p)(p) ){
162699 pNew = sqlite3DbMallocZero(db, sizeof(Window));
162700 if( pNew ){
162701 pNew->zName = sqlite3DbStrDup(db, p->zName);
162702 pNew->zBase = sqlite3DbStrDup(db, p->zBase);
162703 pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
162704 pNew->pWFunc = p->pWFunc;
162705 pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
162706 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
162707 pNew->eFrmType = p->eFrmType;
162708 pNew->eEnd = p->eEnd;
162709 pNew->eStart = p->eStart;
162710 pNew->eExclude = p->eExclude;
162711 pNew->regResult = p->regResult;
162712 pNew->regAccum = p->regAccum;
162713 pNew->iArgCol = p->iArgCol;
162714 pNew->iEphCsr = p->iEphCsr;
162715 pNew->bExprArgs = p->bExprArgs;
162716 pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
162717 pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
162718 pNew->pOwner = pOwner;
162719 pNew->bImplicitFrame = p->bImplicitFrame;
162720 }
162721 }
162722 return pNew;
162723}
162724
162725/*
162726** Return a copy of the linked list of Window objects passed as the
162727** second argument.
162728*/
162729SQLITE_PRIVATEstatic Window *sqlite3WindowListDup(sqlite3 *db, Window *p){
162730 Window *pWin;
162731 Window *pRet = 0;
162732 Window **pp = &pRet;
162733
162734 for(pWin=p; pWin; pWin=pWin->pNextWin){
162735 *pp = sqlite3WindowDup(db, 0, pWin);
162736 if( *pp==0 ) break;
162737 pp = &((*pp)->pNextWin);
162738 }
162739
162740 return pRet;
162741}
162742
162743/*
162744** Return true if it can be determined at compile time that expression
162745** pExpr evaluates to a value that, when cast to an integer, is greater
162746** than zero. False otherwise.
162747**
162748** If an OOM error occurs, this function sets the Parse.db.mallocFailed
162749** flag and returns zero.
162750*/
162751static int windowExprGtZero(Parse *pParse, Expr *pExpr){
162752 int ret = 0;
162753 sqlite3 *db = pParse->db;
162754 sqlite3_value *pVal = 0;
162755 sqlite3ValueFromExpr(db, pExpr, db->enc, SQLITE_AFF_NUMERIC0x43, &pVal);
162756 if( pVal && sqlite3_value_int(pVal)>0 ){
162757 ret = 1;
162758 }
162759 sqlite3ValueFree(pVal);
162760 return ret;
162761}
162762
162763/*
162764** sqlite3WhereBegin() has already been called for the SELECT statement
162765** passed as the second argument when this function is invoked. It generates
162766** code to populate the Window.regResult register for each window function
162767** and invoke the sub-routine at instruction addrGosub once for each row.
162768** sqlite3WhereEnd() is always called before returning.
162769**
162770** This function handles several different types of window frames, which
162771** require slightly different processing. The following pseudo code is
162772** used to implement window frames of the form:
162773**
162774** ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
162775**
162776** Other window frame types use variants of the following:
162777**
162778** ... loop started by sqlite3WhereBegin() ...
162779** if( new partition ){
162780** Gosub flush
162781** }
162782** Insert new row into eph table.
162783**
162784** if( first row of partition ){
162785** // Rewind three cursors, all open on the eph table.
162786** Rewind(csrEnd);
162787** Rewind(csrStart);
162788** Rewind(csrCurrent);
162789**
162790** regEnd = <expr2> // FOLLOWING expression
162791** regStart = <expr1> // PRECEDING expression
162792** }else{
162793** // First time this branch is taken, the eph table contains two
162794** // rows. The first row in the partition, which all three cursors
162795** // currently point to, and the following row.
162796** AGGSTEP
162797** if( (regEnd--)<=0 ){
162798** RETURN_ROW
162799** if( (regStart--)<=0 ){
162800** AGGINVERSE
162801** }
162802** }
162803** }
162804** }
162805** flush:
162806** AGGSTEP
162807** while( 1 ){
162808** RETURN ROW
162809** if( csrCurrent is EOF ) break;
162810** if( (regStart--)<=0 ){
162811** AggInverse(csrStart)
162812** Next(csrStart)
162813** }
162814** }
162815**
162816** The pseudo-code above uses the following shorthand:
162817**
162818** AGGSTEP: invoke the aggregate xStep() function for each window function
162819** with arguments read from the current row of cursor csrEnd, then
162820** step cursor csrEnd forward one row (i.e. sqlite3BtreeNext()).
162821**
162822** RETURN_ROW: return a row to the caller based on the contents of the
162823** current row of csrCurrent and the current state of all
162824** aggregates. Then step cursor csrCurrent forward one row.
162825**
162826** AGGINVERSE: invoke the aggregate xInverse() function for each window
162827** functions with arguments read from the current row of cursor
162828** csrStart. Then step csrStart forward one row.
162829**
162830** There are two other ROWS window frames that are handled significantly
162831** differently from the above - "BETWEEN <expr> PRECEDING AND <expr> PRECEDING"
162832** and "BETWEEN <expr> FOLLOWING AND <expr> FOLLOWING". These are special
162833** cases because they change the order in which the three cursors (csrStart,
162834** csrCurrent and csrEnd) iterate through the ephemeral table. Cases that
162835** use UNBOUNDED or CURRENT ROW are much simpler variations on one of these
162836** three.
162837**
162838** ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
162839**
162840** ... loop started by sqlite3WhereBegin() ...
162841** if( new partition ){
162842** Gosub flush
162843** }
162844** Insert new row into eph table.
162845** if( first row of partition ){
162846** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
162847** regEnd = <expr2>
162848** regStart = <expr1>
162849** }else{
162850** if( (regEnd--)<=0 ){
162851** AGGSTEP
162852** }
162853** RETURN_ROW
162854** if( (regStart--)<=0 ){
162855** AGGINVERSE
162856** }
162857** }
162858** }
162859** flush:
162860** if( (regEnd--)<=0 ){
162861** AGGSTEP
162862** }
162863** RETURN_ROW
162864**
162865**
162866** ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
162867**
162868** ... loop started by sqlite3WhereBegin() ...
162869** if( new partition ){
162870** Gosub flush
162871** }
162872** Insert new row into eph table.
162873** if( first row of partition ){
162874** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
162875** regEnd = <expr2>
162876** regStart = regEnd - <expr1>
162877** }else{
162878** AGGSTEP
162879** if( (regEnd--)<=0 ){
162880** RETURN_ROW
162881** }
162882** if( (regStart--)<=0 ){
162883** AGGINVERSE
162884** }
162885** }
162886** }
162887** flush:
162888** AGGSTEP
162889** while( 1 ){
162890** if( (regEnd--)<=0 ){
162891** RETURN_ROW
162892** if( eof ) break;
162893** }
162894** if( (regStart--)<=0 ){
162895** AGGINVERSE
162896** if( eof ) break
162897** }
162898** }
162899** while( !eof csrCurrent ){
162900** RETURN_ROW
162901** }
162902**
162903** For the most part, the patterns above are adapted to support UNBOUNDED by
162904** assuming that it is equivalent to "infinity PRECEDING/FOLLOWING" and
162905** CURRENT ROW by assuming that it is equivilent to "0 PRECEDING/FOLLOWING".
162906** This is optimized of course - branches that will never be taken and
162907** conditions that are always true are omitted from the VM code. The only
162908** exceptional case is:
162909**
162910** ROWS BETWEEN <expr1> FOLLOWING AND UNBOUNDED FOLLOWING
162911**
162912** ... loop started by sqlite3WhereBegin() ...
162913** if( new partition ){
162914** Gosub flush
162915** }
162916** Insert new row into eph table.
162917** if( first row of partition ){
162918** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
162919** regStart = <expr1>
162920** }else{
162921** AGGSTEP
162922** }
162923** }
162924** flush:
162925** AGGSTEP
162926** while( 1 ){
162927** if( (regStart--)<=0 ){
162928** AGGINVERSE
162929** if( eof ) break
162930** }
162931** RETURN_ROW
162932** }
162933** while( !eof csrCurrent ){
162934** RETURN_ROW
162935** }
162936**
162937** Also requiring special handling are the cases:
162938**
162939** ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
162940** ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
162941**
162942** when (expr1 < expr2). This is detected at runtime, not by this function.
162943** To handle this case, the pseudo-code programs depicted above are modified
162944** slightly to be:
162945**
162946** ... loop started by sqlite3WhereBegin() ...
162947** if( new partition ){
162948** Gosub flush
162949** }
162950** Insert new row into eph table.
162951** if( first row of partition ){
162952** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
162953** regEnd = <expr2>
162954** regStart = <expr1>
162955** if( regEnd < regStart ){
162956** RETURN_ROW
162957** delete eph table contents
162958** continue
162959** }
162960** ...
162961**
162962** The new "continue" statement in the above jumps to the next iteration
162963** of the outer loop - the one started by sqlite3WhereBegin().
162964**
162965** The various GROUPS cases are implemented using the same patterns as
162966** ROWS. The VM code is modified slightly so that:
162967**
162968** 1. The else branch in the main loop is only taken if the row just
162969** added to the ephemeral table is the start of a new group. In
162970** other words, it becomes:
162971**
162972** ... loop started by sqlite3WhereBegin() ...
162973** if( new partition ){
162974** Gosub flush
162975** }
162976** Insert new row into eph table.
162977** if( first row of partition ){
162978** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
162979** regEnd = <expr2>
162980** regStart = <expr1>
162981** }else if( new group ){
162982** ...
162983** }
162984** }
162985**
162986** 2. Instead of processing a single row, each RETURN_ROW, AGGSTEP or
162987** AGGINVERSE step processes the current row of the relevant cursor and
162988** all subsequent rows belonging to the same group.
162989**
162990** RANGE window frames are a little different again. As for GROUPS, the
162991** main loop runs once per group only. And RETURN_ROW, AGGSTEP and AGGINVERSE
162992** deal in groups instead of rows. As for ROWS and GROUPS, there are three
162993** basic cases:
162994**
162995** RANGE BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
162996**
162997** ... loop started by sqlite3WhereBegin() ...
162998** if( new partition ){
162999** Gosub flush
163000** }
163001** Insert new row into eph table.
163002** if( first row of partition ){
163003** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
163004** regEnd = <expr2>
163005** regStart = <expr1>
163006** }else{
163007** AGGSTEP
163008** while( (csrCurrent.key + regEnd) < csrEnd.key ){
163009** RETURN_ROW
163010** while( csrStart.key + regStart) < csrCurrent.key ){
163011** AGGINVERSE
163012** }
163013** }
163014** }
163015** }
163016** flush:
163017** AGGSTEP
163018** while( 1 ){
163019** RETURN ROW
163020** if( csrCurrent is EOF ) break;
163021** while( csrStart.key + regStart) < csrCurrent.key ){
163022** AGGINVERSE
163023** }
163024** }
163025** }
163026**
163027** In the above notation, "csr.key" means the current value of the ORDER BY
163028** expression (there is only ever 1 for a RANGE that uses an <expr> FOLLOWING
163029** or <expr PRECEDING) read from cursor csr.
163030**
163031** RANGE BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
163032**
163033** ... loop started by sqlite3WhereBegin() ...
163034** if( new partition ){
163035** Gosub flush
163036** }
163037** Insert new row into eph table.
163038** if( first row of partition ){
163039** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
163040** regEnd = <expr2>
163041** regStart = <expr1>
163042** }else{
163043** while( (csrEnd.key + regEnd) <= csrCurrent.key ){
163044** AGGSTEP
163045** }
163046** while( (csrStart.key + regStart) < csrCurrent.key ){
163047** AGGINVERSE
163048** }
163049** RETURN_ROW
163050** }
163051** }
163052** flush:
163053** while( (csrEnd.key + regEnd) <= csrCurrent.key ){
163054** AGGSTEP
163055** }
163056** while( (csrStart.key + regStart) < csrCurrent.key ){
163057** AGGINVERSE
163058** }
163059** RETURN_ROW
163060**
163061** RANGE BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
163062**
163063** ... loop started by sqlite3WhereBegin() ...
163064** if( new partition ){
163065** Gosub flush
163066** }
163067** Insert new row into eph table.
163068** if( first row of partition ){
163069** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
163070** regEnd = <expr2>
163071** regStart = <expr1>
163072** }else{
163073** AGGSTEP
163074** while( (csrCurrent.key + regEnd) < csrEnd.key ){
163075** while( (csrCurrent.key + regStart) > csrStart.key ){
163076** AGGINVERSE
163077** }
163078** RETURN_ROW
163079** }
163080** }
163081** }
163082** flush:
163083** AGGSTEP
163084** while( 1 ){
163085** while( (csrCurrent.key + regStart) > csrStart.key ){
163086** AGGINVERSE
163087** if( eof ) break "while( 1 )" loop.
163088** }
163089** RETURN_ROW
163090** }
163091** while( !eof csrCurrent ){
163092** RETURN_ROW
163093** }
163094**
163095** The text above leaves out many details. Refer to the code and comments
163096** below for a more complete picture.
163097*/
163098SQLITE_PRIVATEstatic void sqlite3WindowCodeStep(
163099 Parse *pParse, /* Parse context */
163100 Select *p, /* Rewritten SELECT statement */
163101 WhereInfo *pWInfo, /* Context returned by sqlite3WhereBegin() */
163102 int regGosub, /* Register for OP_Gosub */
163103 int addrGosub /* OP_Gosub here to return each row */
163104){
163105 Window *pMWin = p->pWin;
163106 ExprList *pOrderBy = pMWin->pOrderBy;
163107 Vdbe *v = sqlite3GetVdbe(pParse);
163108 int csrWrite; /* Cursor used to write to eph. table */
163109 int csrInput = p->pSrc->a[0].iCursor; /* Cursor of sub-select */
163110 int nInput = p->pSrc->a[0].pTab->nCol; /* Number of cols returned by sub */
163111 int iInput; /* To iterate through sub cols */
163112 int addrNe; /* Address of OP_Ne */
163113 int addrGosubFlush = 0; /* Address of OP_Gosub to flush: */
163114 int addrInteger = 0; /* Address of OP_Integer */
163115 int addrEmpty; /* Address of OP_Rewind in flush: */
163116 int regNew; /* Array of registers holding new input row */
163117 int regRecord; /* regNew array in record form */
163118 int regNewPeer = 0; /* Peer values for new row (part of regNew) */
163119 int regPeer = 0; /* Peer values for current row */
163120 int regFlushPart = 0; /* Register for "Gosub flush_partition" */
163121 WindowCodeArg s; /* Context object for sub-routines */
163122 int lblWhereEnd; /* Label just before sqlite3WhereEnd() code */
163123 int regStart = 0; /* Value of <expr> PRECEDING */
163124 int regEnd = 0; /* Value of <expr> FOLLOWING */
163125
163126 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_CURRENT((void) (0))
163127 || pMWin->eStart==TK_FOLLOWING || pMWin->eStart==TK_UNBOUNDED((void) (0))
163128 )((void) (0));
163129 assert( pMWin->eEnd==TK_FOLLOWING || pMWin->eEnd==TK_CURRENT((void) (0))
163130 || pMWin->eEnd==TK_UNBOUNDED || pMWin->eEnd==TK_PRECEDING((void) (0))
163131 )((void) (0));
163132 assert( pMWin->eExclude==0 || pMWin->eExclude==TK_CURRENT((void) (0))
163133 || pMWin->eExclude==TK_GROUP || pMWin->eExclude==TK_TIES((void) (0))
163134 || pMWin->eExclude==TK_NO((void) (0))
163135 )((void) (0));
163136
163137 lblWhereEnd = sqlite3VdbeMakeLabel(pParse);
163138
163139 /* Fill in the context object */
163140 memset(&s, 0, sizeof(WindowCodeArg));
163141 s.pParse = pParse;
163142 s.pMWin = pMWin;
163143 s.pVdbe = v;
163144 s.regGosub = regGosub;
163145 s.addrGosub = addrGosub;
163146 s.current.csr = pMWin->iEphCsr;
163147 csrWrite = s.current.csr+1;
163148 s.start.csr = s.current.csr+2;
163149 s.end.csr = s.current.csr+3;
163150
163151 /* Figure out when rows may be deleted from the ephemeral table. There
163152 ** are four options - they may never be deleted (eDelete==0), they may
163153 ** be deleted as soon as they are no longer part of the window frame
163154 ** (eDelete==WINDOW_AGGINVERSE), they may be deleted as after the row
163155 ** has been returned to the caller (WINDOW_RETURN_ROW), or they may
163156 ** be deleted after they enter the frame (WINDOW_AGGSTEP). */
163157 switch( pMWin->eStart ){
163158 case TK_FOLLOWING86:
163159 if( pMWin->eFrmType!=TK_RANGE89
163160 && windowExprGtZero(pParse, pMWin->pStart)
163161 ){
163162 s.eDelete = WINDOW_RETURN_ROW1;
163163 }
163164 break;
163165 case TK_UNBOUNDED90:
163166 if( windowCacheFrame(pMWin)==0 ){
163167 if( pMWin->eEnd==TK_PRECEDING88 ){
163168 if( pMWin->eFrmType!=TK_RANGE89
163169 && windowExprGtZero(pParse, pMWin->pEnd)
163170 ){
163171 s.eDelete = WINDOW_AGGSTEP3;
163172 }
163173 }else{
163174 s.eDelete = WINDOW_RETURN_ROW1;
163175 }
163176 }
163177 break;
163178 default:
163179 s.eDelete = WINDOW_AGGINVERSE2;
163180 break;
163181 }
163182
163183 /* Allocate registers for the array of values from the sub-query, the
163184 ** samve values in record form, and the rowid used to insert said record
163185 ** into the ephemeral table. */
163186 regNew = pParse->nMem+1;
163187 pParse->nMem += nInput;
163188 regRecord = ++pParse->nMem;
163189 s.regRowid = ++pParse->nMem;
163190
163191 /* If the window frame contains an "<expr> PRECEDING" or "<expr> FOLLOWING"
163192 ** clause, allocate registers to store the results of evaluating each
163193 ** <expr>. */
163194 if( pMWin->eStart==TK_PRECEDING88 || pMWin->eStart==TK_FOLLOWING86 ){
163195 regStart = ++pParse->nMem;
163196 }
163197 if( pMWin->eEnd==TK_PRECEDING88 || pMWin->eEnd==TK_FOLLOWING86 ){
163198 regEnd = ++pParse->nMem;
163199 }
163200
163201 /* If this is not a "ROWS BETWEEN ..." frame, then allocate arrays of
163202 ** registers to store copies of the ORDER BY expressions (peer values)
163203 ** for the main loop, and for each cursor (start, current and end). */
163204 if( pMWin->eFrmType!=TK_ROWS76 ){
163205 int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
163206 regNewPeer = regNew + pMWin->nBufferCol;
163207 if( pMWin->pPartition ) regNewPeer += pMWin->pPartition->nExpr;
163208 regPeer = pParse->nMem+1; pParse->nMem += nPeer;
163209 s.start.reg = pParse->nMem+1; pParse->nMem += nPeer;
163210 s.current.reg = pParse->nMem+1; pParse->nMem += nPeer;
163211 s.end.reg = pParse->nMem+1; pParse->nMem += nPeer;
163212 }
163213
163214 /* Load the column values for the row returned by the sub-select
163215 ** into an array of registers starting at regNew. Assemble them into
163216 ** a record in register regRecord. */
163217 for(iInput=0; iInput<nInput; iInput++){
163218 sqlite3VdbeAddOp3(v, OP_Column94, csrInput, iInput, regNew+iInput);
163219 }
163220 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regNew, nInput, regRecord);
163221
163222 /* An input row has just been read into an array of registers starting
163223 ** at regNew. If the window has a PARTITION clause, this block generates
163224 ** VM code to check if the input row is the start of a new partition.
163225 ** If so, it does an OP_Gosub to an address to be filled in later. The
163226 ** address of the OP_Gosub is stored in local variable addrGosubFlush. */
163227 if( pMWin->pPartition ){
163228 int addr;
163229 ExprList *pPart = pMWin->pPartition;
163230 int nPart = pPart->nExpr;
163231 int regNewPart = regNew + pMWin->nBufferCol;
163232 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
163233
163234 regFlushPart = ++pParse->nMem;
163235 addr = sqlite3VdbeAddOp3(v, OP_Compare90, regNewPart, pMWin->regPart, nPart);
163236 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO(-8));
163237 sqlite3VdbeAddOp3(v, OP_Jump13, addr+2, addr+4, addr+2);
163238 VdbeCoverageEqNe(v);
163239 addrGosubFlush = sqlite3VdbeAddOp1(v, OP_Gosub9, regFlushPart);
163240 VdbeComment((v, "call flush_partition"));
163241 sqlite3VdbeAddOp3(v, OP_Copy80, regNewPart, pMWin->regPart, nPart-1);
163242 }
163243
163244 /* Insert the new row into the ephemeral table */
163245 sqlite3VdbeAddOp2(v, OP_NewRowid127, csrWrite, s.regRowid);
163246 sqlite3VdbeAddOp3(v, OP_Insert128, csrWrite, regRecord, s.regRowid);
163247 addrNe = sqlite3VdbeAddOp3(v, OP_Ne52, pMWin->regOne, 0, s.regRowid);
163248 VdbeCoverageNeverNull(v);
163249
163250 /* This block is run for the first row of each partition */
163251 s.regArg = windowInitAccum(pParse, pMWin);
163252
163253 if( regStart ){
163254 sqlite3ExprCode(pParse, pMWin->pStart, regStart);
163255 windowCheckValue(pParse, regStart, 0 + (pMWin->eFrmType==TK_RANGE89?3:0));
163256 }
163257 if( regEnd ){
163258 sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
163259 windowCheckValue(pParse, regEnd, 1 + (pMWin->eFrmType==TK_RANGE89?3:0));
163260 }
163261
163262 if( pMWin->eFrmType!=TK_RANGE89 && pMWin->eStart==pMWin->eEnd && regStart ){
163263 int op = ((pMWin->eStart==TK_FOLLOWING86) ? OP_Ge57 : OP_Le55);
163264 int addrGe = sqlite3VdbeAddOp3(v, op, regStart, 0, regEnd);
163265 VdbeCoverageNeverNullIf(v, op==OP_Ge); /* NeverNull because bound <expr> */
163266 VdbeCoverageNeverNullIf(v, op==OP_Le); /* values previously checked */
163267 windowAggFinal(&s, 0);
163268 sqlite3VdbeAddOp2(v, OP_Rewind35, s.current.csr, 1);
163269 VdbeCoverageNeverTaken(v);
163270 windowReturnOneRow(&s);
163271 sqlite3VdbeAddOp1(v, OP_ResetSorter146, s.current.csr);
163272 sqlite3VdbeAddOp2(v, OP_Goto8, 0, lblWhereEnd);
163273 sqlite3VdbeJumpHere(v, addrGe);
163274 }
163275 if( pMWin->eStart==TK_FOLLOWING86 && pMWin->eFrmType!=TK_RANGE89 && regEnd ){
163276 assert( pMWin->eEnd==TK_FOLLOWING )((void) (0));
163277 sqlite3VdbeAddOp3(v, OP_Subtract107, regStart, regEnd, regStart);
163278 }
163279
163280 if( pMWin->eStart!=TK_UNBOUNDED90 ){
163281 sqlite3VdbeAddOp2(v, OP_Rewind35, s.start.csr, 1);
163282 VdbeCoverageNeverTaken(v);
163283 }
163284 sqlite3VdbeAddOp2(v, OP_Rewind35, s.current.csr, 1);
163285 VdbeCoverageNeverTaken(v);
163286 sqlite3VdbeAddOp2(v, OP_Rewind35, s.end.csr, 1);
163287 VdbeCoverageNeverTaken(v);
163288 if( regPeer && pOrderBy ){
163289 sqlite3VdbeAddOp3(v, OP_Copy80, regNewPeer, regPeer, pOrderBy->nExpr-1);
163290 sqlite3VdbeAddOp3(v, OP_Copy80, regPeer, s.start.reg, pOrderBy->nExpr-1);
163291 sqlite3VdbeAddOp3(v, OP_Copy80, regPeer, s.current.reg, pOrderBy->nExpr-1);
163292 sqlite3VdbeAddOp3(v, OP_Copy80, regPeer, s.end.reg, pOrderBy->nExpr-1);
163293 }
163294
163295 sqlite3VdbeAddOp2(v, OP_Goto8, 0, lblWhereEnd);
163296
163297 sqlite3VdbeJumpHere(v, addrNe);
163298
163299 /* Beginning of the block executed for the second and subsequent rows. */
163300 if( regPeer ){
163301 windowIfNewPeer(pParse, pOrderBy, regNewPeer, regPeer, lblWhereEnd);
163302 }
163303 if( pMWin->eStart==TK_FOLLOWING86 ){
163304 windowCodeOp(&s, WINDOW_AGGSTEP3, 0, 0);
163305 if( pMWin->eEnd!=TK_UNBOUNDED90 ){
163306 if( pMWin->eFrmType==TK_RANGE89 ){
163307 int lbl = sqlite3VdbeMakeLabel(pParse);
163308 int addrNext = sqlite3VdbeCurrentAddr(v);
163309 windowCodeRangeTest(&s, OP_Ge57, s.current.csr, regEnd, s.end.csr, lbl);
163310 windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
163311 windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 0);
163312 sqlite3VdbeAddOp2(v, OP_Goto8, 0, addrNext);
163313 sqlite3VdbeResolveLabel(v, lbl);
163314 }else{
163315 windowCodeOp(&s, WINDOW_RETURN_ROW1, regEnd, 0);
163316 windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
163317 }
163318 }
163319 }else
163320 if( pMWin->eEnd==TK_PRECEDING88 ){
163321 int bRPS = (pMWin->eStart==TK_PRECEDING88 && pMWin->eFrmType==TK_RANGE89);
163322 windowCodeOp(&s, WINDOW_AGGSTEP3, regEnd, 0);
163323 if( bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
163324 windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 0);
163325 if( !bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
163326 }else{
163327 int addr = 0;
163328 windowCodeOp(&s, WINDOW_AGGSTEP3, 0, 0);
163329 if( pMWin->eEnd!=TK_UNBOUNDED90 ){
163330 if( pMWin->eFrmType==TK_RANGE89 ){
163331 int lbl = 0;
163332 addr = sqlite3VdbeCurrentAddr(v);
163333 if( regEnd ){
163334 lbl = sqlite3VdbeMakeLabel(pParse);
163335 windowCodeRangeTest(&s, OP_Ge57, s.current.csr, regEnd, s.end.csr, lbl);
163336 }
163337 windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 0);
163338 windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
163339 if( regEnd ){
163340 sqlite3VdbeAddOp2(v, OP_Goto8, 0, addr);
163341 sqlite3VdbeResolveLabel(v, lbl);
163342 }
163343 }else{
163344 if( regEnd ){
163345 addr = sqlite3VdbeAddOp3(v, OP_IfPos49, regEnd, 0, 1);
163346 VdbeCoverage(v);
163347 }
163348 windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 0);
163349 windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
163350 if( regEnd ) sqlite3VdbeJumpHere(v, addr);
163351 }
163352 }
163353 }
163354
163355 /* End of the main input loop */
163356 sqlite3VdbeResolveLabel(v, lblWhereEnd);
163357 sqlite3WhereEnd(pWInfo);
163358
163359 /* Fall through */
163360 if( pMWin->pPartition ){
163361 addrInteger = sqlite3VdbeAddOp2(v, OP_Integer71, 0, regFlushPart);
163362 sqlite3VdbeJumpHere(v, addrGosubFlush);
163363 }
163364
163365 s.regRowid = 0;
163366 addrEmpty = sqlite3VdbeAddOp1(v, OP_Rewind35, csrWrite);
163367 VdbeCoverage(v);
163368 if( pMWin->eEnd==TK_PRECEDING88 ){
163369 int bRPS = (pMWin->eStart==TK_PRECEDING88 && pMWin->eFrmType==TK_RANGE89);
163370 windowCodeOp(&s, WINDOW_AGGSTEP3, regEnd, 0);
163371 if( bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
163372 windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 0);
163373 }else if( pMWin->eStart==TK_FOLLOWING86 ){
163374 int addrStart;
163375 int addrBreak1;
163376 int addrBreak2;
163377 int addrBreak3;
163378 windowCodeOp(&s, WINDOW_AGGSTEP3, 0, 0);
163379 if( pMWin->eFrmType==TK_RANGE89 ){
163380 addrStart = sqlite3VdbeCurrentAddr(v);
163381 addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 1);
163382 addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 1);
163383 }else
163384 if( pMWin->eEnd==TK_UNBOUNDED90 ){
163385 addrStart = sqlite3VdbeCurrentAddr(v);
163386 addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW1, regStart, 1);
163387 addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE2, 0, 1);
163388 }else{
163389 assert( pMWin->eEnd==TK_FOLLOWING )((void) (0));
163390 addrStart = sqlite3VdbeCurrentAddr(v);
163391 addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW1, regEnd, 1);
163392 addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 1);
163393 }
163394 sqlite3VdbeAddOp2(v, OP_Goto8, 0, addrStart);
163395 sqlite3VdbeJumpHere(v, addrBreak2);
163396 addrStart = sqlite3VdbeCurrentAddr(v);
163397 addrBreak3 = windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 1);
163398 sqlite3VdbeAddOp2(v, OP_Goto8, 0, addrStart);
163399 sqlite3VdbeJumpHere(v, addrBreak1);
163400 sqlite3VdbeJumpHere(v, addrBreak3);
163401 }else{
163402 int addrBreak;
163403 int addrStart;
163404 windowCodeOp(&s, WINDOW_AGGSTEP3, 0, 0);
163405 addrStart = sqlite3VdbeCurrentAddr(v);
163406 addrBreak = windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 1);
163407 windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
163408 sqlite3VdbeAddOp2(v, OP_Goto8, 0, addrStart);
163409 sqlite3VdbeJumpHere(v, addrBreak);
163410 }
163411 sqlite3VdbeJumpHere(v, addrEmpty);
163412
163413 sqlite3VdbeAddOp1(v, OP_ResetSorter146, s.current.csr);
163414 if( pMWin->pPartition ){
163415 if( pMWin->regStartRowid ){
163416 sqlite3VdbeAddOp2(v, OP_Integer71, 1, pMWin->regStartRowid);
163417 sqlite3VdbeAddOp2(v, OP_Integer71, 0, pMWin->regEndRowid);
163418 }
163419 sqlite3VdbeChangeP1(v, addrInteger, sqlite3VdbeCurrentAddr(v));
163420 sqlite3VdbeAddOp1(v, OP_Return67, regFlushPart);
163421 }
163422}
163423
163424#endif /* SQLITE_OMIT_WINDOWFUNC */
163425
163426/************** End of window.c **********************************************/
163427/************** Begin file parse.c *******************************************/
163428/* This file is automatically generated by Lemon from input grammar
163429** source file "parse.y". */
163430/*
163431** 2001-09-15
163432**
163433** The author disclaims copyright to this source code. In place of
163434** a legal notice, here is a blessing:
163435**
163436** May you do good and not evil.
163437** May you find forgiveness for yourself and forgive others.
163438** May you share freely, never taking more than you give.
163439**
163440*************************************************************************
163441** This file contains SQLite's SQL parser.
163442**
163443** The canonical source code to this file ("parse.y") is a Lemon grammar
163444** file that specifies the input grammar and actions to take while parsing.
163445** That input file is processed by Lemon to generate a C-language
163446** implementation of a parser for the given grammer. You might be reading
163447** this comment as part of the translated C-code. Edits should be made
163448** to the original parse.y sources.
163449*/
163450
163451/* #include "sqliteInt.h" */
163452
163453/*
163454** Disable all error recovery processing in the parser push-down
163455** automaton.
163456*/
163457#define YYNOERRORRECOVERY1 1
163458
163459/*
163460** Make yytestcase() the same as testcase()
163461*/
163462#define yytestcase(X) testcase(X)
163463
163464/*
163465** Indicate that sqlite3ParserFree() will never be called with a null
163466** pointer.
163467*/
163468#define YYPARSEFREENEVERNULL1 1
163469
163470/*
163471** In the amalgamation, the parse.c file generated by lemon and the
163472** tokenize.c file are concatenated. In that case, sqlite3RunParser()
163473** has access to the the size of the yyParser object and so the parser
163474** engine can be allocated from stack. In that case, only the
163475** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
163476** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
163477** omitted.
163478*/
163479#ifdef SQLITE_AMALGAMATION1
163480# define sqlite3Parser_ENGINEALWAYSONSTACK1 1
163481#endif
163482
163483/*
163484** Alternative datatype for the argument to the malloc() routine passed
163485** into sqlite3ParserAlloc(). The default is size_t.
163486*/
163487#define YYMALLOCARGTYPEu64 u64
163488
163489/*
163490** An instance of the following structure describes the event of a
163491** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
163492** TK_DELETE, or TK_INSTEAD. If the event is of the form
163493**
163494** UPDATE ON (a,b,c)
163495**
163496** Then the "b" IdList records the list "a,b,c".
163497*/
163498struct TrigEvent { int a; IdList * b; };
163499
163500struct FrameBound { int eType; Expr *pExpr; };
163501
163502/*
163503** Disable lookaside memory allocation for objects that might be
163504** shared across database connections.
163505*/
163506static void disableLookaside(Parse *pParse){
163507 sqlite3 *db = pParse->db;
163508 pParse->disableLookaside++;
163509 DisableLookasidedb->lookaside.bDisable++;db->lookaside.sz=0;
163510}
163511
163512#if !defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT1) \
163513 && defined(SQLITE_UDL_CAPABLE_PARSER)
163514/*
163515** Issue an error message if an ORDER BY or LIMIT clause occurs on an
163516** UPDATE or DELETE statement.
163517*/
163518static void updateDeleteLimitError(
163519 Parse *pParse,
163520 ExprList *pOrderBy,
163521 Expr *pLimit
163522){
163523 if( pOrderBy ){
163524 sqlite3ErrorMsg(pParse, "syntax error near \"ORDER BY\"");
163525 }else{
163526 sqlite3ErrorMsg(pParse, "syntax error near \"LIMIT\"");
163527 }
163528 sqlite3ExprListDelete(pParse->db, pOrderBy);
163529 sqlite3ExprDelete(pParse->db, pLimit);
163530}
163531#endif /* SQLITE_ENABLE_UPDATE_DELETE_LIMIT */
163532
163533
163534 /*
163535 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
163536 ** all elements in the list. And make sure list length does not exceed
163537 ** SQLITE_LIMIT_COMPOUND_SELECT.
163538 */
163539 static void parserDoubleLinkSelect(Parse *pParse, Select *p){
163540 assert( p!=0 )((void) (0));
163541 if( p->pPrior ){
163542 Select *pNext = 0, *pLoop = p;
163543 int mxSelect, cnt = 1;
163544 while(1){
163545 pLoop->pNext = pNext;
163546 pLoop->selFlags |= SF_Compound0x0000100;
163547 pNext = pLoop;
163548 pLoop = pLoop->pPrior;
163549 if( pLoop==0 ) break;
163550 cnt++;
163551 if( pLoop->pOrderBy || pLoop->pLimit ){
163552 sqlite3ErrorMsg(pParse,"%s clause should come after %s not before",
163553 pLoop->pOrderBy!=0 ? "ORDER BY" : "LIMIT",
163554 sqlite3SelectOpName(pNext->op));
163555 break;
163556 }
163557 }
163558 if( (p->selFlags & SF_MultiValue0x0000400)==0 &&
163559 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT4])>0 &&
163560 cnt>mxSelect
163561 ){
163562 sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
163563 }
163564 }
163565 }
163566
163567 /* Attach a With object describing the WITH clause to a Select
163568 ** object describing the query for which the WITH clause is a prefix.
163569 */
163570 static Select *attachWithToSelect(Parse *pParse, Select *pSelect, With *pWith){
163571 if( pSelect ){
163572 pSelect->pWith = pWith;
163573 parserDoubleLinkSelect(pParse, pSelect);
163574 }else{
163575 sqlite3WithDelete(pParse->db, pWith);
163576 }
163577 return pSelect;
163578 }
163579
163580
163581 /* Construct a new Expr object from a single token */
163582 static Expr *tokenExpr(Parse *pParse, int op, Token t){
163583 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
163584 if( p ){
163585 /* memset(p, 0, sizeof(Expr)); */
163586 p->op = (u8)op;
163587 p->affExpr = 0;
163588 p->flags = EP_Leaf0x800000;
163589 ExprClearVVAProperties(p);
163590 /* p->iAgg = -1; // Not required */
163591 p->pLeft = p->pRight = 0;
163592 p->pAggInfo = 0;
163593 memset(&p->x, 0, sizeof(p->x));
163594 memset(&p->y, 0, sizeof(p->y));
163595 p->op2 = 0;
163596 p->iTable = 0;
163597 p->iColumn = 0;
163598 p->u.zToken = (char*)&p[1];
163599 memcpy(p->u.zToken, t.z, t.n);
163600 p->u.zToken[t.n] = 0;
163601 p->w.iOfst = (int)(t.z - pParse->zTail);
163602 if( sqlite3Isquote(p->u.zToken[0])(sqlite3CtypeMap[(unsigned char)(p->u.zToken[0])]&0x80
)
){
163603 sqlite3DequoteExpr(p);
163604 }
163605#if SQLITE_MAX_EXPR_DEPTH1000>0
163606 p->nHeight = 1;
163607#endif
163608 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
163609 return (Expr*)sqlite3RenameTokenMap(pParse, (void*)p, &t);
163610 }
163611 }
163612 return p;
163613 }
163614
163615
163616 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
163617 ** unary TK_ISNULL or TK_NOTNULL expression. */
163618 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
163619 sqlite3 *db = pParse->db;
163620 if( pA && pY && pY->op==TK_NULL121 && !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
163621 pA->op = (u8)op;
163622 sqlite3ExprDelete(db, pA->pRight);
163623 pA->pRight = 0;
163624 }
163625 }
163626
163627 /* Add a single new term to an ExprList that is used to store a
163628 ** list of identifiers. Report an error if the ID list contains
163629 ** a COLLATE clause or an ASC or DESC keyword, except ignore the
163630 ** error while parsing a legacy schema.
163631 */
163632 static ExprList *parserAddExprIdListTerm(
163633 Parse *pParse,
163634 ExprList *pPrior,
163635 Token *pIdToken,
163636 int hasCollate,
163637 int sortOrder
163638 ){
163639 ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
163640 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED-1)
163641 && pParse->db->init.busy==0
163642 ){
163643 sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
163644 pIdToken->n, pIdToken->z);
163645 }
163646 sqlite3ExprListSetName(pParse, p, pIdToken, 1);
163647 return p;
163648 }
163649
163650#if TK_SPAN181>255
163651# error too many tokens in the grammar
163652#endif
163653/**************** End of %include directives **********************************/
163654/* These constants specify the various numeric values for terminal symbols.
163655***************** Begin token definitions *************************************/
163656#ifndef TK_SEMI1
163657#define TK_SEMI1 1
163658#define TK_EXPLAIN2 2
163659#define TK_QUERY3 3
163660#define TK_PLAN4 4
163661#define TK_BEGIN5 5
163662#define TK_TRANSACTION6 6
163663#define TK_DEFERRED7 7
163664#define TK_IMMEDIATE8 8
163665#define TK_EXCLUSIVE9 9
163666#define TK_COMMIT10 10
163667#define TK_END11 11
163668#define TK_ROLLBACK12 12
163669#define TK_SAVEPOINT13 13
163670#define TK_RELEASE14 14
163671#define TK_TO15 15
163672#define TK_TABLE16 16
163673#define TK_CREATE17 17
163674#define TK_IF18 18
163675#define TK_NOT19 19
163676#define TK_EXISTS20 20
163677#define TK_TEMP21 21
163678#define TK_LP22 22
163679#define TK_RP23 23
163680#define TK_AS24 24
163681#define TK_COMMA25 25
163682#define TK_WITHOUT26 26
163683#define TK_ABORT27 27
163684#define TK_ACTION28 28
163685#define TK_AFTER29 29
163686#define TK_ANALYZE30 30
163687#define TK_ASC31 31
163688#define TK_ATTACH32 32
163689#define TK_BEFORE33 33
163690#define TK_BY34 34
163691#define TK_CASCADE35 35
163692#define TK_CAST36 36
163693#define TK_CONFLICT37 37
163694#define TK_DATABASE38 38
163695#define TK_DESC39 39
163696#define TK_DETACH40 40
163697#define TK_EACH41 41
163698#define TK_FAIL42 42
163699#define TK_OR43 43
163700#define TK_AND44 44
163701#define TK_IS45 45
163702#define TK_MATCH46 46
163703#define TK_LIKE_KW47 47
163704#define TK_BETWEEN48 48
163705#define TK_IN49 49
163706#define TK_ISNULL50 50
163707#define TK_NOTNULL51 51
163708#define TK_NE52 52
163709#define TK_EQ53 53
163710#define TK_GT54 54
163711#define TK_LE55 55
163712#define TK_LT56 56
163713#define TK_GE57 57
163714#define TK_ESCAPE58 58
163715#define TK_ID59 59
163716#define TK_COLUMNKW60 60
163717#define TK_DO61 61
163718#define TK_FOR62 62
163719#define TK_IGNORE63 63
163720#define TK_INITIALLY64 64
163721#define TK_INSTEAD65 65
163722#define TK_NO66 66
163723#define TK_KEY67 67
163724#define TK_OF68 68
163725#define TK_OFFSET69 69
163726#define TK_PRAGMA70 70
163727#define TK_RAISE71 71
163728#define TK_RECURSIVE72 72
163729#define TK_REPLACE73 73
163730#define TK_RESTRICT74 74
163731#define TK_ROW75 75
163732#define TK_ROWS76 76
163733#define TK_TRIGGER77 77
163734#define TK_VACUUM78 78
163735#define TK_VIEW79 79
163736#define TK_VIRTUAL80 80
163737#define TK_WITH81 81
163738#define TK_NULLS82 82
163739#define TK_FIRST83 83
163740#define TK_LAST84 84
163741#define TK_CURRENT85 85
163742#define TK_FOLLOWING86 86
163743#define TK_PARTITION87 87
163744#define TK_PRECEDING88 88
163745#define TK_RANGE89 89
163746#define TK_UNBOUNDED90 90
163747#define TK_EXCLUDE91 91
163748#define TK_GROUPS92 92
163749#define TK_OTHERS93 93
163750#define TK_TIES94 94
163751#define TK_GENERATED95 95
163752#define TK_ALWAYS96 96
163753#define TK_MATERIALIZED97 97
163754#define TK_REINDEX98 98
163755#define TK_RENAME99 99
163756#define TK_CTIME_KW100 100
163757#define TK_ANY101 101
163758#define TK_BITAND102 102
163759#define TK_BITOR103 103
163760#define TK_LSHIFT104 104
163761#define TK_RSHIFT105 105
163762#define TK_PLUS106 106
163763#define TK_MINUS107 107
163764#define TK_STAR108 108
163765#define TK_SLASH109 109
163766#define TK_REM110 110
163767#define TK_CONCAT111 111
163768#define TK_PTR112 112
163769#define TK_COLLATE113 113
163770#define TK_BITNOT114 114
163771#define TK_ON115 115
163772#define TK_INDEXED116 116
163773#define TK_STRING117 117
163774#define TK_JOIN_KW118 118
163775#define TK_CONSTRAINT119 119
163776#define TK_DEFAULT120 120
163777#define TK_NULL121 121
163778#define TK_PRIMARY122 122
163779#define TK_UNIQUE123 123
163780#define TK_CHECK124 124
163781#define TK_REFERENCES125 125
163782#define TK_AUTOINCR126 126
163783#define TK_INSERT127 127
163784#define TK_DELETE128 128
163785#define TK_UPDATE129 129
163786#define TK_SET130 130
163787#define TK_DEFERRABLE131 131
163788#define TK_FOREIGN132 132
163789#define TK_DROP133 133
163790#define TK_UNION134 134
163791#define TK_ALL135 135
163792#define TK_EXCEPT136 136
163793#define TK_INTERSECT137 137
163794#define TK_SELECT138 138
163795#define TK_VALUES139 139
163796#define TK_DISTINCT140 140
163797#define TK_DOT141 141
163798#define TK_FROM142 142
163799#define TK_JOIN143 143
163800#define TK_USING144 144
163801#define TK_ORDER145 145
163802#define TK_GROUP146 146
163803#define TK_HAVING147 147
163804#define TK_LIMIT148 148
163805#define TK_WHERE149 149
163806#define TK_RETURNING150 150
163807#define TK_INTO151 151
163808#define TK_NOTHING152 152
163809#define TK_FLOAT153 153
163810#define TK_BLOB154 154
163811#define TK_INTEGER155 155
163812#define TK_VARIABLE156 156
163813#define TK_CASE157 157
163814#define TK_WHEN158 158
163815#define TK_THEN159 159
163816#define TK_ELSE160 160
163817#define TK_INDEX161 161
163818#define TK_ALTER162 162
163819#define TK_ADD163 163
163820#define TK_WINDOW164 164
163821#define TK_OVER165 165
163822#define TK_FILTER166 166
163823#define TK_COLUMN167 167
163824#define TK_AGG_FUNCTION168 168
163825#define TK_AGG_COLUMN169 169
163826#define TK_TRUEFALSE170 170
163827#define TK_ISNOT171 171
163828#define TK_FUNCTION172 172
163829#define TK_UMINUS173 173
163830#define TK_UPLUS174 174
163831#define TK_TRUTH175 175
163832#define TK_REGISTER176 176
163833#define TK_VECTOR177 177
163834#define TK_SELECT_COLUMN178 178
163835#define TK_IF_NULL_ROW179 179
163836#define TK_ASTERISK180 180
163837#define TK_SPAN181 181
163838#define TK_ERROR182 182
163839#define TK_SPACE183 183
163840#define TK_ILLEGAL184 184
163841#endif
163842/**************** End token definitions ***************************************/
163843
163844/* The next sections is a series of control #defines.
163845** various aspects of the generated parser.
163846** YYCODETYPE is the data type used to store the integer codes
163847** that represent terminal and non-terminal symbols.
163848** "unsigned char" is used if there are fewer than
163849** 256 symbols. Larger types otherwise.
163850** YYNOCODE is a number of type YYCODETYPE that is not used for
163851** any terminal or nonterminal symbol.
163852** YYFALLBACK If defined, this indicates that one or more tokens
163853** (also known as: "terminal symbols") have fall-back
163854** values which should be used if the original symbol
163855** would not parse. This permits keywords to sometimes
163856** be used as identifiers, for example.
163857** YYACTIONTYPE is the data type used for "action codes" - numbers
163858** that indicate what to do in response to the next
163859** token.
163860** sqlite3ParserTOKENTYPE is the data type used for minor type for terminal
163861** symbols. Background: A "minor type" is a semantic
163862** value associated with a terminal or non-terminal
163863** symbols. For example, for an "ID" terminal symbol,
163864** the minor type might be the name of the identifier.
163865** Each non-terminal can have a different minor type.
163866** Terminal symbols all have the same minor type, though.
163867** This macros defines the minor type for terminal
163868** symbols.
163869** YYMINORTYPE is the data type used for all minor types.
163870** This is typically a union of many types, one of
163871** which is sqlite3ParserTOKENTYPE. The entry in the union
163872** for terminal symbols is called "yy0".
163873** YYSTACKDEPTH is the maximum depth of the parser's stack. If
163874** zero the stack is dynamically sized using realloc()
163875** sqlite3ParserARG_SDECL A static variable declaration for the %extra_argument
163876** sqlite3ParserARG_PDECL A parameter declaration for the %extra_argument
163877** sqlite3ParserARG_PARAM Code to pass %extra_argument as a subroutine parameter
163878** sqlite3ParserARG_STORE Code to store %extra_argument into yypParser
163879** sqlite3ParserARG_FETCH Code to extract %extra_argument from yypParser
163880** sqlite3ParserCTX_* As sqlite3ParserARG_ except for %extra_context
163881** YYERRORSYMBOL is the code number of the error symbol. If not
163882** defined, then do no error processing.
163883** YYNSTATE the combined number of states.
163884** YYNRULE the number of rules in the grammar
163885** YYNTOKEN Number of terminal symbols
163886** YY_MAX_SHIFT Maximum value for shift actions
163887** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
163888** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
163889** YY_ERROR_ACTION The yy_action[] code for syntax error
163890** YY_ACCEPT_ACTION The yy_action[] code for accept
163891** YY_NO_ACTION The yy_action[] code for no-op
163892** YY_MIN_REDUCE Minimum value for reduce actions
163893** YY_MAX_REDUCE Maximum value for reduce actions
163894*/
163895#ifndef INTERFACE1
163896# define INTERFACE1 1
163897#endif
163898/************* Begin control #defines *****************************************/
163899#define YYCODETYPEunsigned short int unsigned short int
163900#define YYNOCODE319 319
163901#define YYACTIONTYPEunsigned short int unsigned short int
163902#define YYWILDCARD101 101
163903#define sqlite3ParserTOKENTYPEToken Token
163904typedef union {
163905 int yyinit;
163906 sqlite3ParserTOKENTYPEToken yy0;
163907 TriggerStep* yy33;
163908 Window* yy41;
163909 Select* yy47;
163910 SrcList* yy131;
163911 struct TrigEvent yy180;
163912 struct {int value; int mask;} yy231;
163913 IdList* yy254;
163914 u32 yy285;
163915 ExprList* yy322;
163916 Cte* yy385;
163917 int yy394;
163918 Upsert* yy444;
163919 u8 yy516;
163920 With* yy521;
163921 const char* yy522;
163922 Expr* yy528;
163923 OnOrUsing yy561;
163924 struct FrameBound yy595;
163925} YYMINORTYPE;
163926#ifndef YYSTACKDEPTH100
163927#define YYSTACKDEPTH100 100
163928#endif
163929#define sqlite3ParserARG_SDECL
163930#define sqlite3ParserARG_PDECL
163931#define sqlite3ParserARG_PARAM
163932#define sqlite3ParserARG_FETCH
163933#define sqlite3ParserARG_STORE
163934#define sqlite3ParserCTX_SDECLParse *pParse; Parse *pParse;
163935#define sqlite3ParserCTX_PDECL,Parse *pParse ,Parse *pParse
163936#define sqlite3ParserCTX_PARAM,pParse ,pParse
163937#define sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse; Parse *pParse=yypParser->pParse;
163938#define sqlite3ParserCTX_STOREyypParser->pParse=pParse; yypParser->pParse=pParse;
163939#define YYFALLBACK1 1
163940#define YYNSTATE576 576
163941#define YYNRULE405 405
163942#define YYNRULE_WITH_ACTION342 342
163943#define YYNTOKEN185 185
163944#define YY_MAX_SHIFT575 575
163945#define YY_MIN_SHIFTREDUCE835 835
163946#define YY_MAX_SHIFTREDUCE1239 1239
163947#define YY_ERROR_ACTION1240 1240
163948#define YY_ACCEPT_ACTION1241 1241
163949#define YY_NO_ACTION1242 1242
163950#define YY_MIN_REDUCE1243 1243
163951#define YY_MAX_REDUCE1647 1647
163952/************* End control #defines *******************************************/
163953#define YY_NLOOKAHEAD((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0]))) ((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])))
163954
163955/* Define the yytestcase() macro to be a no-op if is not already defined
163956** otherwise.
163957**
163958** Applications can choose to define yytestcase() in the %include section
163959** to a macro that can assist in verifying code coverage. For production
163960** code the yytestcase() macro should be turned off. But it is useful
163961** for testing.
163962*/
163963#ifndef yytestcase
163964# define yytestcase(X)
163965#endif
163966
163967
163968/* Next are the tables used to determine what action to take based on the
163969** current state and lookahead token. These tables are used to implement
163970** functions that take a state number and lookahead value and return an
163971** action integer.
163972**
163973** Suppose the action integer is N. Then the action is determined as
163974** follows
163975**
163976** 0 <= N <= YY_MAX_SHIFT Shift N. That is, push the lookahead
163977** token onto the stack and goto state N.
163978**
163979** N between YY_MIN_SHIFTREDUCE Shift to an arbitrary state then
163980** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE.
163981**
163982** N == YY_ERROR_ACTION A syntax error has occurred.
163983**
163984** N == YY_ACCEPT_ACTION The parser accepts its input.
163985**
163986** N == YY_NO_ACTION No such action. Denotes unused
163987** slots in the yy_action[] table.
163988**
163989** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE
163990** and YY_MAX_REDUCE
163991**
163992** The action table is constructed as a single large table named yy_action[].
163993** Given state S and lookahead X, the action is computed as either:
163994**
163995** (A) N = yy_action[ yy_shift_ofst[S] + X ]
163996** (B) N = yy_default[S]
163997**
163998** The (A) formula is preferred. The B formula is used instead if
163999** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X.
164000**
164001** The formulas above are for computing the action when the lookahead is
164002** a terminal symbol. If the lookahead is a non-terminal (as occurs after
164003** a reduce action) then the yy_reduce_ofst[] array is used in place of
164004** the yy_shift_ofst[] array.
164005**
164006** The following are the tables generated in this section:
164007**
164008** yy_action[] A single table containing all actions.
164009** yy_lookahead[] A table containing the lookahead for each entry in
164010** yy_action. Used to detect hash collisions.
164011** yy_shift_ofst[] For each state, the offset into yy_action for
164012** shifting terminals.
164013** yy_reduce_ofst[] For each state, the offset into yy_action for
164014** shifting non-terminals after a reduce.
164015** yy_default[] Default action for each state.
164016**
164017*********** Begin parsing tables **********************************************/
164018#define YY_ACTTAB_COUNT(2098) (2098)
164019static const YYACTIONTYPEunsigned short int yy_action[] = {
164020 /* 0 */ 568, 208, 568, 118, 115, 229, 568, 118, 115, 229,
164021 /* 10 */ 568, 1314, 377, 1293, 408, 562, 562, 562, 568, 409,
164022 /* 20 */ 378, 1314, 1276, 41, 41, 41, 41, 208, 1526, 71,
164023 /* 30 */ 71, 971, 419, 41, 41, 491, 303, 279, 303, 972,
164024 /* 40 */ 397, 71, 71, 125, 126, 80, 1217, 1217, 1050, 1053,
164025 /* 50 */ 1040, 1040, 123, 123, 124, 124, 124, 124, 476, 409,
164026 /* 60 */ 1241, 1, 1, 575, 2, 1245, 550, 118, 115, 229,
164027 /* 70 */ 317, 480, 146, 480, 524, 118, 115, 229, 529, 1327,
164028 /* 80 */ 417, 523, 142, 125, 126, 80, 1217, 1217, 1050, 1053,
164029 /* 90 */ 1040, 1040, 123, 123, 124, 124, 124, 124, 118, 115,
164030 /* 100 */ 229, 327, 122, 122, 122, 122, 121, 121, 120, 120,
164031 /* 110 */ 120, 119, 116, 444, 284, 284, 284, 284, 442, 442,
164032 /* 120 */ 442, 1567, 376, 1569, 1192, 375, 1163, 565, 1163, 565,
164033 /* 130 */ 409, 1567, 537, 259, 226, 444, 101, 145, 449, 316,
164034 /* 140 */ 559, 240, 122, 122, 122, 122, 121, 121, 120, 120,
164035 /* 150 */ 120, 119, 116, 444, 125, 126, 80, 1217, 1217, 1050,
164036 /* 160 */ 1053, 1040, 1040, 123, 123, 124, 124, 124, 124, 142,
164037 /* 170 */ 294, 1192, 339, 448, 120, 120, 120, 119, 116, 444,
164038 /* 180 */ 127, 1192, 1193, 1194, 148, 441, 440, 568, 119, 116,
164039 /* 190 */ 444, 124, 124, 124, 124, 117, 122, 122, 122, 122,
164040 /* 200 */ 121, 121, 120, 120, 120, 119, 116, 444, 454, 113,
164041 /* 210 */ 13, 13, 546, 122, 122, 122, 122, 121, 121, 120,
164042 /* 220 */ 120, 120, 119, 116, 444, 422, 316, 559, 1192, 1193,
164043 /* 230 */ 1194, 149, 1224, 409, 1224, 124, 124, 124, 124, 122,
164044 /* 240 */ 122, 122, 122, 121, 121, 120, 120, 120, 119, 116,
164045 /* 250 */ 444, 465, 342, 1037, 1037, 1051, 1054, 125, 126, 80,
164046 /* 260 */ 1217, 1217, 1050, 1053, 1040, 1040, 123, 123, 124, 124,
164047 /* 270 */ 124, 124, 1279, 522, 222, 1192, 568, 409, 224, 514,
164048 /* 280 */ 175, 82, 83, 122, 122, 122, 122, 121, 121, 120,
164049 /* 290 */ 120, 120, 119, 116, 444, 1007, 16, 16, 1192, 133,
164050 /* 300 */ 133, 125, 126, 80, 1217, 1217, 1050, 1053, 1040, 1040,
164051 /* 310 */ 123, 123, 124, 124, 124, 124, 122, 122, 122, 122,
164052 /* 320 */ 121, 121, 120, 120, 120, 119, 116, 444, 1041, 546,
164053 /* 330 */ 1192, 373, 1192, 1193, 1194, 252, 1434, 399, 504, 501,
164054 /* 340 */ 500, 111, 560, 566, 4, 926, 926, 433, 499, 340,
164055 /* 350 */ 460, 328, 360, 394, 1237, 1192, 1193, 1194, 563, 568,
164056 /* 360 */ 122, 122, 122, 122, 121, 121, 120, 120, 120, 119,
164057 /* 370 */ 116, 444, 284, 284, 369, 1580, 1607, 441, 440, 154,
164058 /* 380 */ 409, 445, 71, 71, 1286, 565, 1221, 1192, 1193, 1194,
164059 /* 390 */ 85, 1223, 271, 557, 543, 515, 1561, 568, 98, 1222,
164060 /* 400 */ 6, 1278, 472, 142, 125, 126, 80, 1217, 1217, 1050,
164061 /* 410 */ 1053, 1040, 1040, 123, 123, 124, 124, 124, 124, 550,
164062 /* 420 */ 13, 13, 1027, 507, 1224, 1192, 1224, 549, 109, 109,
164063 /* 430 */ 222, 568, 1238, 175, 568, 427, 110, 197, 445, 570,
164064 /* 440 */ 569, 430, 1552, 1017, 325, 551, 1192, 270, 287, 368,
164065 /* 450 */ 510, 363, 509, 257, 71, 71, 543, 71, 71, 359,
164066 /* 460 */ 316, 559, 1613, 122, 122, 122, 122, 121, 121, 120,
164067 /* 470 */ 120, 120, 119, 116, 444, 1017, 1017, 1019, 1020, 27,
164068 /* 480 */ 284, 284, 1192, 1193, 1194, 1158, 568, 1612, 409, 901,
164069 /* 490 */ 190, 550, 356, 565, 550, 937, 533, 517, 1158, 516,
164070 /* 500 */ 413, 1158, 552, 1192, 1193, 1194, 568, 544, 1554, 51,
164071 /* 510 */ 51, 214, 125, 126, 80, 1217, 1217, 1050, 1053, 1040,
164072 /* 520 */ 1040, 123, 123, 124, 124, 124, 124, 1192, 474, 135,
164073 /* 530 */ 135, 409, 284, 284, 1490, 505, 121, 121, 120, 120,
164074 /* 540 */ 120, 119, 116, 444, 1007, 565, 518, 217, 541, 1561,
164075 /* 550 */ 316, 559, 142, 6, 532, 125, 126, 80, 1217, 1217,
164076 /* 560 */ 1050, 1053, 1040, 1040, 123, 123, 124, 124, 124, 124,
164077 /* 570 */ 1555, 122, 122, 122, 122, 121, 121, 120, 120, 120,
164078 /* 580 */ 119, 116, 444, 485, 1192, 1193, 1194, 482, 281, 1267,
164079 /* 590 */ 957, 252, 1192, 373, 504, 501, 500, 1192, 340, 571,
164080 /* 600 */ 1192, 571, 409, 292, 499, 957, 876, 191, 480, 316,
164081 /* 610 */ 559, 384, 290, 380, 122, 122, 122, 122, 121, 121,
164082 /* 620 */ 120, 120, 120, 119, 116, 444, 125, 126, 80, 1217,
164083 /* 630 */ 1217, 1050, 1053, 1040, 1040, 123, 123, 124, 124, 124,
164084 /* 640 */ 124, 409, 394, 1136, 1192, 869, 100, 284, 284, 1192,
164085 /* 650 */ 1193, 1194, 373, 1093, 1192, 1193, 1194, 1192, 1193, 1194,
164086 /* 660 */ 565, 455, 32, 373, 233, 125, 126, 80, 1217, 1217,
164087 /* 670 */ 1050, 1053, 1040, 1040, 123, 123, 124, 124, 124, 124,
164088 /* 680 */ 1433, 959, 568, 228, 958, 122, 122, 122, 122, 121,
164089 /* 690 */ 121, 120, 120, 120, 119, 116, 444, 1158, 228, 1192,
164090 /* 700 */ 157, 1192, 1193, 1194, 1553, 13, 13, 301, 957, 1232,
164091 /* 710 */ 1158, 153, 409, 1158, 373, 1583, 1176, 5, 369, 1580,
164092 /* 720 */ 429, 1238, 3, 957, 122, 122, 122, 122, 121, 121,
164093 /* 730 */ 120, 120, 120, 119, 116, 444, 125, 126, 80, 1217,
164094 /* 740 */ 1217, 1050, 1053, 1040, 1040, 123, 123, 124, 124, 124,
164095 /* 750 */ 124, 409, 208, 567, 1192, 1028, 1192, 1193, 1194, 1192,
164096 /* 760 */ 388, 852, 155, 1552, 286, 402, 1098, 1098, 488, 568,
164097 /* 770 */ 465, 342, 1319, 1319, 1552, 125, 126, 80, 1217, 1217,
164098 /* 780 */ 1050, 1053, 1040, 1040, 123, 123, 124, 124, 124, 124,
164099 /* 790 */ 129, 568, 13, 13, 374, 122, 122, 122, 122, 121,
164100 /* 800 */ 121, 120, 120, 120, 119, 116, 444, 302, 568, 453,
164101 /* 810 */ 528, 1192, 1193, 1194, 13, 13, 1192, 1193, 1194, 1297,
164102 /* 820 */ 463, 1267, 409, 1317, 1317, 1552, 1012, 453, 452, 200,
164103 /* 830 */ 299, 71, 71, 1265, 122, 122, 122, 122, 121, 121,
164104 /* 840 */ 120, 120, 120, 119, 116, 444, 125, 126, 80, 1217,
164105 /* 850 */ 1217, 1050, 1053, 1040, 1040, 123, 123, 124, 124, 124,
164106 /* 860 */ 124, 409, 227, 1073, 1158, 284, 284, 419, 312, 278,
164107 /* 870 */ 278, 285, 285, 1419, 406, 405, 382, 1158, 565, 568,
164108 /* 880 */ 1158, 1196, 565, 1600, 565, 125, 126, 80, 1217, 1217,
164109 /* 890 */ 1050, 1053, 1040, 1040, 123, 123, 124, 124, 124, 124,
164110 /* 900 */ 453, 1482, 13, 13, 1536, 122, 122, 122, 122, 121,
164111 /* 910 */ 121, 120, 120, 120, 119, 116, 444, 201, 568, 354,
164112 /* 920 */ 1586, 575, 2, 1245, 840, 841, 842, 1562, 317, 1212,
164113 /* 930 */ 146, 6, 409, 255, 254, 253, 206, 1327, 9, 1196,
164114 /* 940 */ 262, 71, 71, 424, 122, 122, 122, 122, 121, 121,
164115 /* 950 */ 120, 120, 120, 119, 116, 444, 125, 126, 80, 1217,
164116 /* 960 */ 1217, 1050, 1053, 1040, 1040, 123, 123, 124, 124, 124,
164117 /* 970 */ 124, 568, 284, 284, 568, 1213, 409, 574, 313, 1245,
164118 /* 980 */ 349, 1296, 352, 419, 317, 565, 146, 491, 525, 1643,
164119 /* 990 */ 395, 371, 491, 1327, 70, 70, 1295, 71, 71, 240,
164120 /* 1000 */ 1325, 104, 80, 1217, 1217, 1050, 1053, 1040, 1040, 123,
164121 /* 1010 */ 123, 124, 124, 124, 124, 122, 122, 122, 122, 121,
164122 /* 1020 */ 121, 120, 120, 120, 119, 116, 444, 1114, 284, 284,
164123 /* 1030 */ 428, 448, 1525, 1213, 439, 284, 284, 1489, 1352, 311,
164124 /* 1040 */ 474, 565, 1115, 971, 491, 491, 217, 1263, 565, 1538,
164125 /* 1050 */ 568, 972, 207, 568, 1027, 240, 383, 1116, 519, 122,
164126 /* 1060 */ 122, 122, 122, 121, 121, 120, 120, 120, 119, 116,
164127 /* 1070 */ 444, 1018, 107, 71, 71, 1017, 13, 13, 912, 568,
164128 /* 1080 */ 1495, 568, 284, 284, 97, 526, 491, 448, 913, 1326,
164129 /* 1090 */ 1322, 545, 409, 284, 284, 565, 151, 209, 1495, 1497,
164130 /* 1100 */ 262, 450, 55, 55, 56, 56, 565, 1017, 1017, 1019,
164131 /* 1110 */ 443, 332, 409, 527, 12, 295, 125, 126, 80, 1217,
164132 /* 1120 */ 1217, 1050, 1053, 1040, 1040, 123, 123, 124, 124, 124,
164133 /* 1130 */ 124, 347, 409, 864, 1534, 1213, 125, 126, 80, 1217,
164134 /* 1140 */ 1217, 1050, 1053, 1040, 1040, 123, 123, 124, 124, 124,
164135 /* 1150 */ 124, 1137, 1641, 474, 1641, 371, 125, 114, 80, 1217,
164136 /* 1160 */ 1217, 1050, 1053, 1040, 1040, 123, 123, 124, 124, 124,
164137 /* 1170 */ 124, 1495, 329, 474, 331, 122, 122, 122, 122, 121,
164138 /* 1180 */ 121, 120, 120, 120, 119, 116, 444, 203, 1419, 568,
164139 /* 1190 */ 1294, 864, 464, 1213, 436, 122, 122, 122, 122, 121,
164140 /* 1200 */ 121, 120, 120, 120, 119, 116, 444, 553, 1137, 1642,
164141 /* 1210 */ 539, 1642, 15, 15, 892, 122, 122, 122, 122, 121,
164142 /* 1220 */ 121, 120, 120, 120, 119, 116, 444, 568, 298, 538,
164143 /* 1230 */ 1135, 1419, 1559, 1560, 1331, 409, 6, 6, 1169, 1268,
164144 /* 1240 */ 415, 320, 284, 284, 1419, 508, 565, 525, 300, 457,
164145 /* 1250 */ 43, 43, 568, 893, 12, 565, 330, 478, 425, 407,
164146 /* 1260 */ 126, 80, 1217, 1217, 1050, 1053, 1040, 1040, 123, 123,
164147 /* 1270 */ 124, 124, 124, 124, 568, 57, 57, 288, 1192, 1419,
164148 /* 1280 */ 496, 458, 392, 392, 391, 273, 389, 1135, 1558, 849,
164149 /* 1290 */ 1169, 407, 6, 568, 321, 1158, 470, 44, 44, 1557,
164150 /* 1300 */ 1114, 426, 234, 6, 323, 256, 540, 256, 1158, 431,
164151 /* 1310 */ 568, 1158, 322, 17, 487, 1115, 58, 58, 122, 122,
164152 /* 1320 */ 122, 122, 121, 121, 120, 120, 120, 119, 116, 444,
164153 /* 1330 */ 1116, 216, 481, 59, 59, 1192, 1193, 1194, 111, 560,
164154 /* 1340 */ 324, 4, 236, 456, 526, 568, 237, 456, 568, 437,
164155 /* 1350 */ 168, 556, 420, 141, 479, 563, 568, 293, 568, 1095,
164156 /* 1360 */ 568, 293, 568, 1095, 531, 568, 872, 8, 60, 60,
164157 /* 1370 */ 235, 61, 61, 568, 414, 568, 414, 568, 445, 62,
164158 /* 1380 */ 62, 45, 45, 46, 46, 47, 47, 199, 49, 49,
164159 /* 1390 */ 557, 568, 359, 568, 100, 486, 50, 50, 63, 63,
164160 /* 1400 */ 64, 64, 561, 415, 535, 410, 568, 1027, 568, 534,
164161 /* 1410 */ 316, 559, 316, 559, 65, 65, 14, 14, 568, 1027,
164162 /* 1420 */ 568, 512, 932, 872, 1018, 109, 109, 931, 1017, 66,
164163 /* 1430 */ 66, 131, 131, 110, 451, 445, 570, 569, 416, 177,
164164 /* 1440 */ 1017, 132, 132, 67, 67, 568, 467, 568, 932, 471,
164165 /* 1450 */ 1364, 283, 226, 931, 315, 1363, 407, 568, 459, 407,
164166 /* 1460 */ 1017, 1017, 1019, 239, 407, 86, 213, 1350, 52, 52,
164167 /* 1470 */ 68, 68, 1017, 1017, 1019, 1020, 27, 1585, 1180, 447,
164168 /* 1480 */ 69, 69, 288, 97, 108, 1541, 106, 392, 392, 391,
164169 /* 1490 */ 273, 389, 568, 879, 849, 883, 568, 111, 560, 466,
164170 /* 1500 */ 4, 568, 152, 30, 38, 568, 1132, 234, 396, 323,
164171 /* 1510 */ 111, 560, 527, 4, 563, 53, 53, 322, 568, 163,
164172 /* 1520 */ 163, 568, 337, 468, 164, 164, 333, 563, 76, 76,
164173 /* 1530 */ 568, 289, 1514, 568, 31, 1513, 568, 445, 338, 483,
164174 /* 1540 */ 100, 54, 54, 344, 72, 72, 296, 236, 1080, 557,
164175 /* 1550 */ 445, 879, 1360, 134, 134, 168, 73, 73, 141, 161,
164176 /* 1560 */ 161, 1574, 557, 535, 568, 319, 568, 348, 536, 1009,
164177 /* 1570 */ 473, 261, 261, 891, 890, 235, 535, 568, 1027, 568,
164178 /* 1580 */ 475, 534, 261, 367, 109, 109, 521, 136, 136, 130,
164179 /* 1590 */ 130, 1027, 110, 366, 445, 570, 569, 109, 109, 1017,
164180 /* 1600 */ 162, 162, 156, 156, 568, 110, 1080, 445, 570, 569,
164181 /* 1610 */ 410, 351, 1017, 568, 353, 316, 559, 568, 343, 568,
164182 /* 1620 */ 100, 497, 357, 258, 100, 898, 899, 140, 140, 355,
164183 /* 1630 */ 1310, 1017, 1017, 1019, 1020, 27, 139, 139, 362, 451,
164184 /* 1640 */ 137, 137, 138, 138, 1017, 1017, 1019, 1020, 27, 1180,
164185 /* 1650 */ 447, 568, 372, 288, 111, 560, 1021, 4, 392, 392,
164186 /* 1660 */ 391, 273, 389, 568, 1141, 849, 568, 1076, 568, 258,
164187 /* 1670 */ 492, 563, 568, 211, 75, 75, 555, 962, 234, 261,
164188 /* 1680 */ 323, 111, 560, 929, 4, 113, 77, 77, 322, 74,
164189 /* 1690 */ 74, 42, 42, 1373, 445, 48, 48, 1418, 563, 974,
164190 /* 1700 */ 975, 1092, 1091, 1092, 1091, 862, 557, 150, 930, 1346,
164191 /* 1710 */ 113, 1358, 554, 1424, 1021, 1275, 1266, 1254, 236, 1253,
164192 /* 1720 */ 1255, 445, 1593, 1343, 308, 276, 168, 309, 11, 141,
164193 /* 1730 */ 393, 310, 232, 557, 1405, 1027, 335, 291, 1400, 219,
164194 /* 1740 */ 336, 109, 109, 936, 297, 1410, 235, 341, 477, 110,
164195 /* 1750 */ 502, 445, 570, 569, 1393, 1409, 1017, 400, 1293, 365,
164196 /* 1760 */ 223, 1486, 1027, 1485, 1355, 1356, 1354, 1353, 109, 109,
164197 /* 1770 */ 204, 1596, 1232, 558, 265, 218, 110, 205, 445, 570,
164198 /* 1780 */ 569, 410, 387, 1017, 1533, 179, 316, 559, 1017, 1017,
164199 /* 1790 */ 1019, 1020, 27, 230, 1531, 1229, 79, 560, 85, 4,
164200 /* 1800 */ 418, 215, 548, 81, 84, 188, 1406, 173, 181, 461,
164201 /* 1810 */ 451, 35, 462, 563, 183, 1017, 1017, 1019, 1020, 27,
164202 /* 1820 */ 184, 1491, 185, 186, 495, 242, 98, 398, 1412, 36,
164203 /* 1830 */ 1411, 484, 91, 469, 401, 1414, 445, 192, 1480, 246,
164204 /* 1840 */ 1502, 490, 346, 277, 248, 196, 493, 511, 557, 350,
164205 /* 1850 */ 1256, 249, 250, 403, 1313, 1312, 111, 560, 432, 4,
164206 /* 1860 */ 1311, 1304, 93, 1611, 883, 1610, 224, 404, 434, 520,
164207 /* 1870 */ 263, 435, 1579, 563, 1283, 1282, 364, 1027, 306, 1281,
164208 /* 1880 */ 264, 1609, 1565, 109, 109, 370, 1303, 307, 1564, 438,
164209 /* 1890 */ 128, 110, 1378, 445, 570, 569, 445, 546, 1017, 10,
164210 /* 1900 */ 1466, 105, 381, 1377, 34, 572, 99, 1336, 557, 314,
164211 /* 1910 */ 1186, 530, 272, 274, 379, 210, 1335, 547, 385, 386,
164212 /* 1920 */ 275, 573, 1251, 1246, 411, 412, 1518, 165, 178, 1519,
164213 /* 1930 */ 1017, 1017, 1019, 1020, 27, 1517, 1516, 1027, 78, 147,
164214 /* 1940 */ 166, 220, 221, 109, 109, 836, 304, 167, 446, 212,
164215 /* 1950 */ 318, 110, 231, 445, 570, 569, 144, 1090, 1017, 1088,
164216 /* 1960 */ 326, 180, 169, 1212, 182, 334, 238, 915, 241, 1104,
164217 /* 1970 */ 187, 170, 171, 421, 87, 88, 423, 189, 89, 90,
164218 /* 1980 */ 172, 1107, 243, 1103, 244, 158, 18, 245, 345, 247,
164219 /* 1990 */ 1017, 1017, 1019, 1020, 27, 261, 1096, 193, 1226, 489,
164220 /* 2000 */ 194, 37, 366, 851, 494, 251, 195, 506, 92, 19,
164221 /* 2010 */ 498, 358, 20, 503, 881, 361, 94, 894, 305, 159,
164222 /* 2020 */ 513, 39, 95, 1174, 160, 1056, 966, 1143, 96, 174,
164223 /* 2030 */ 1142, 225, 280, 282, 198, 960, 113, 1164, 1160, 260,
164224 /* 2040 */ 21, 22, 23, 1162, 1168, 1167, 1148, 24, 33, 25,
164225 /* 2050 */ 202, 542, 26, 100, 1071, 102, 1057, 103, 7, 1055,
164226 /* 2060 */ 1059, 1113, 1060, 1112, 266, 267, 28, 40, 390, 1022,
164227 /* 2070 */ 863, 112, 29, 564, 1182, 1181, 268, 176, 143, 925,
164228 /* 2080 */ 1242, 1242, 1242, 1242, 1242, 1242, 1242, 1242, 1242, 1242,
164229 /* 2090 */ 1242, 1242, 1242, 1242, 269, 1602, 1242, 1601,
164230};
164231static const YYCODETYPEunsigned short int yy_lookahead[] = {
164232 /* 0 */ 193, 193, 193, 274, 275, 276, 193, 274, 275, 276,
164233 /* 10 */ 193, 223, 219, 225, 206, 210, 211, 212, 193, 19,
164234 /* 20 */ 219, 233, 216, 216, 217, 216, 217, 193, 295, 216,
164235 /* 30 */ 217, 31, 193, 216, 217, 193, 228, 213, 230, 39,
164236 /* 40 */ 206, 216, 217, 43, 44, 45, 46, 47, 48, 49,
164237 /* 50 */ 50, 51, 52, 53, 54, 55, 56, 57, 193, 19,
164238 /* 60 */ 185, 186, 187, 188, 189, 190, 253, 274, 275, 276,
164239 /* 70 */ 195, 193, 197, 193, 261, 274, 275, 276, 253, 204,
164240 /* 80 */ 238, 204, 81, 43, 44, 45, 46, 47, 48, 49,
164241 /* 90 */ 50, 51, 52, 53, 54, 55, 56, 57, 274, 275,
164242 /* 100 */ 276, 262, 102, 103, 104, 105, 106, 107, 108, 109,
164243 /* 110 */ 110, 111, 112, 113, 239, 240, 239, 240, 210, 211,
164244 /* 120 */ 212, 314, 315, 314, 59, 316, 86, 252, 88, 252,
164245 /* 130 */ 19, 314, 315, 256, 257, 113, 25, 72, 296, 138,
164246 /* 140 */ 139, 266, 102, 103, 104, 105, 106, 107, 108, 109,
164247 /* 150 */ 110, 111, 112, 113, 43, 44, 45, 46, 47, 48,
164248 /* 160 */ 49, 50, 51, 52, 53, 54, 55, 56, 57, 81,
164249 /* 170 */ 292, 59, 292, 298, 108, 109, 110, 111, 112, 113,
164250 /* 180 */ 69, 116, 117, 118, 72, 106, 107, 193, 111, 112,
164251 /* 190 */ 113, 54, 55, 56, 57, 58, 102, 103, 104, 105,
164252 /* 200 */ 106, 107, 108, 109, 110, 111, 112, 113, 120, 25,
164253 /* 210 */ 216, 217, 145, 102, 103, 104, 105, 106, 107, 108,
164254 /* 220 */ 109, 110, 111, 112, 113, 231, 138, 139, 116, 117,
164255 /* 230 */ 118, 164, 153, 19, 155, 54, 55, 56, 57, 102,
164256 /* 240 */ 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
164257 /* 250 */ 113, 128, 129, 46, 47, 48, 49, 43, 44, 45,
164258 /* 260 */ 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
164259 /* 270 */ 56, 57, 216, 193, 25, 59, 193, 19, 165, 166,
164260 /* 280 */ 193, 67, 24, 102, 103, 104, 105, 106, 107, 108,
164261 /* 290 */ 109, 110, 111, 112, 113, 73, 216, 217, 59, 216,
164262 /* 300 */ 217, 43, 44, 45, 46, 47, 48, 49, 50, 51,
164263 /* 310 */ 52, 53, 54, 55, 56, 57, 102, 103, 104, 105,
164264 /* 320 */ 106, 107, 108, 109, 110, 111, 112, 113, 121, 145,
164265 /* 330 */ 59, 193, 116, 117, 118, 119, 273, 204, 122, 123,
164266 /* 340 */ 124, 19, 20, 134, 22, 136, 137, 19, 132, 127,
164267 /* 350 */ 128, 129, 24, 22, 23, 116, 117, 118, 36, 193,
164268 /* 360 */ 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
164269 /* 370 */ 112, 113, 239, 240, 311, 312, 215, 106, 107, 241,
164270 /* 380 */ 19, 59, 216, 217, 223, 252, 115, 116, 117, 118,
164271 /* 390 */ 151, 120, 26, 71, 193, 308, 309, 193, 149, 128,
164272 /* 400 */ 313, 216, 269, 81, 43, 44, 45, 46, 47, 48,
164273 /* 410 */ 49, 50, 51, 52, 53, 54, 55, 56, 57, 253,
164274 /* 420 */ 216, 217, 100, 95, 153, 59, 155, 261, 106, 107,
164275 /* 430 */ 25, 193, 101, 193, 193, 231, 114, 25, 116, 117,
164276 /* 440 */ 118, 113, 304, 121, 193, 204, 59, 119, 120, 121,
164277 /* 450 */ 122, 123, 124, 125, 216, 217, 193, 216, 217, 131,
164278 /* 460 */ 138, 139, 230, 102, 103, 104, 105, 106, 107, 108,
164279 /* 470 */ 109, 110, 111, 112, 113, 153, 154, 155, 156, 157,
164280 /* 480 */ 239, 240, 116, 117, 118, 76, 193, 23, 19, 25,
164281 /* 490 */ 22, 253, 23, 252, 253, 108, 87, 204, 89, 261,
164282 /* 500 */ 198, 92, 261, 116, 117, 118, 193, 306, 307, 216,
164283 /* 510 */ 217, 150, 43, 44, 45, 46, 47, 48, 49, 50,
164284 /* 520 */ 51, 52, 53, 54, 55, 56, 57, 59, 193, 216,
164285 /* 530 */ 217, 19, 239, 240, 283, 23, 106, 107, 108, 109,
164286 /* 540 */ 110, 111, 112, 113, 73, 252, 253, 142, 308, 309,
164287 /* 550 */ 138, 139, 81, 313, 145, 43, 44, 45, 46, 47,
164288 /* 560 */ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
164289 /* 570 */ 307, 102, 103, 104, 105, 106, 107, 108, 109, 110,
164290 /* 580 */ 111, 112, 113, 281, 116, 117, 118, 285, 23, 193,
164291 /* 590 */ 25, 119, 59, 193, 122, 123, 124, 59, 127, 203,
164292 /* 600 */ 59, 205, 19, 268, 132, 25, 23, 22, 193, 138,
164293 /* 610 */ 139, 249, 204, 251, 102, 103, 104, 105, 106, 107,
164294 /* 620 */ 108, 109, 110, 111, 112, 113, 43, 44, 45, 46,
164295 /* 630 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
164296 /* 640 */ 57, 19, 22, 23, 59, 23, 25, 239, 240, 116,
164297 /* 650 */ 117, 118, 193, 11, 116, 117, 118, 116, 117, 118,
164298 /* 660 */ 252, 269, 22, 193, 15, 43, 44, 45, 46, 47,
164299 /* 670 */ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
164300 /* 680 */ 273, 143, 193, 118, 143, 102, 103, 104, 105, 106,
164301 /* 690 */ 107, 108, 109, 110, 111, 112, 113, 76, 118, 59,
164302 /* 700 */ 241, 116, 117, 118, 304, 216, 217, 292, 143, 60,
164303 /* 710 */ 89, 241, 19, 92, 193, 193, 23, 22, 311, 312,
164304 /* 720 */ 231, 101, 22, 143, 102, 103, 104, 105, 106, 107,
164305 /* 730 */ 108, 109, 110, 111, 112, 113, 43, 44, 45, 46,
164306 /* 740 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
164307 /* 750 */ 57, 19, 193, 193, 59, 23, 116, 117, 118, 59,
164308 /* 760 */ 201, 21, 241, 304, 22, 206, 127, 128, 129, 193,
164309 /* 770 */ 128, 129, 235, 236, 304, 43, 44, 45, 46, 47,
164310 /* 780 */ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
164311 /* 790 */ 22, 193, 216, 217, 193, 102, 103, 104, 105, 106,
164312 /* 800 */ 107, 108, 109, 110, 111, 112, 113, 231, 193, 193,
164313 /* 810 */ 193, 116, 117, 118, 216, 217, 116, 117, 118, 226,
164314 /* 820 */ 80, 193, 19, 235, 236, 304, 23, 211, 212, 231,
164315 /* 830 */ 204, 216, 217, 205, 102, 103, 104, 105, 106, 107,
164316 /* 840 */ 108, 109, 110, 111, 112, 113, 43, 44, 45, 46,
164317 /* 850 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
164318 /* 860 */ 57, 19, 193, 123, 76, 239, 240, 193, 253, 239,
164319 /* 870 */ 240, 239, 240, 193, 106, 107, 193, 89, 252, 193,
164320 /* 880 */ 92, 59, 252, 141, 252, 43, 44, 45, 46, 47,
164321 /* 890 */ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
164322 /* 900 */ 284, 161, 216, 217, 193, 102, 103, 104, 105, 106,
164323 /* 910 */ 107, 108, 109, 110, 111, 112, 113, 231, 193, 16,
164324 /* 920 */ 187, 188, 189, 190, 7, 8, 9, 309, 195, 25,
164325 /* 930 */ 197, 313, 19, 127, 128, 129, 262, 204, 22, 117,
164326 /* 940 */ 24, 216, 217, 263, 102, 103, 104, 105, 106, 107,
164327 /* 950 */ 108, 109, 110, 111, 112, 113, 43, 44, 45, 46,
164328 /* 960 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
164329 /* 970 */ 57, 193, 239, 240, 193, 59, 19, 188, 253, 190,
164330 /* 980 */ 77, 226, 79, 193, 195, 252, 197, 193, 19, 301,
164331 /* 990 */ 302, 193, 193, 204, 216, 217, 226, 216, 217, 266,
164332 /* 1000 */ 204, 159, 45, 46, 47, 48, 49, 50, 51, 52,
164333 /* 1010 */ 53, 54, 55, 56, 57, 102, 103, 104, 105, 106,
164334 /* 1020 */ 107, 108, 109, 110, 111, 112, 113, 12, 239, 240,
164335 /* 1030 */ 232, 298, 238, 117, 253, 239, 240, 238, 259, 260,
164336 /* 1040 */ 193, 252, 27, 31, 193, 193, 142, 204, 252, 193,
164337 /* 1050 */ 193, 39, 262, 193, 100, 266, 278, 42, 204, 102,
164338 /* 1060 */ 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
164339 /* 1070 */ 113, 117, 159, 216, 217, 121, 216, 217, 63, 193,
164340 /* 1080 */ 193, 193, 239, 240, 115, 116, 193, 298, 73, 238,
164341 /* 1090 */ 238, 231, 19, 239, 240, 252, 22, 24, 211, 212,
164342 /* 1100 */ 24, 193, 216, 217, 216, 217, 252, 153, 154, 155,
164343 /* 1110 */ 253, 16, 19, 144, 213, 268, 43, 44, 45, 46,
164344 /* 1120 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
164345 /* 1130 */ 57, 238, 19, 59, 193, 59, 43, 44, 45, 46,
164346 /* 1140 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
164347 /* 1150 */ 57, 22, 23, 193, 25, 193, 43, 44, 45, 46,
164348 /* 1160 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
164349 /* 1170 */ 57, 284, 77, 193, 79, 102, 103, 104, 105, 106,
164350 /* 1180 */ 107, 108, 109, 110, 111, 112, 113, 286, 193, 193,
164351 /* 1190 */ 193, 117, 291, 117, 232, 102, 103, 104, 105, 106,
164352 /* 1200 */ 107, 108, 109, 110, 111, 112, 113, 204, 22, 23,
164353 /* 1210 */ 66, 25, 216, 217, 35, 102, 103, 104, 105, 106,
164354 /* 1220 */ 107, 108, 109, 110, 111, 112, 113, 193, 268, 85,
164355 /* 1230 */ 101, 193, 309, 309, 240, 19, 313, 313, 94, 208,
164356 /* 1240 */ 209, 193, 239, 240, 193, 66, 252, 19, 268, 244,
164357 /* 1250 */ 216, 217, 193, 74, 213, 252, 161, 19, 263, 254,
164358 /* 1260 */ 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
164359 /* 1270 */ 54, 55, 56, 57, 193, 216, 217, 5, 59, 193,
164360 /* 1280 */ 19, 244, 10, 11, 12, 13, 14, 101, 309, 17,
164361 /* 1290 */ 146, 254, 313, 193, 193, 76, 115, 216, 217, 309,
164362 /* 1300 */ 12, 263, 30, 313, 32, 46, 87, 46, 89, 130,
164363 /* 1310 */ 193, 92, 40, 22, 263, 27, 216, 217, 102, 103,
164364 /* 1320 */ 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
164365 /* 1330 */ 42, 150, 291, 216, 217, 116, 117, 118, 19, 20,
164366 /* 1340 */ 193, 22, 70, 260, 116, 193, 24, 264, 193, 263,
164367 /* 1350 */ 78, 63, 61, 81, 116, 36, 193, 260, 193, 29,
164368 /* 1360 */ 193, 264, 193, 33, 145, 193, 59, 48, 216, 217,
164369 /* 1370 */ 98, 216, 217, 193, 115, 193, 115, 193, 59, 216,
164370 /* 1380 */ 217, 216, 217, 216, 217, 216, 217, 255, 216, 217,
164371 /* 1390 */ 71, 193, 131, 193, 25, 65, 216, 217, 216, 217,
164372 /* 1400 */ 216, 217, 208, 209, 85, 133, 193, 100, 193, 90,
164373 /* 1410 */ 138, 139, 138, 139, 216, 217, 216, 217, 193, 100,
164374 /* 1420 */ 193, 108, 135, 116, 117, 106, 107, 140, 121, 216,
164375 /* 1430 */ 217, 216, 217, 114, 162, 116, 117, 118, 299, 300,
164376 /* 1440 */ 121, 216, 217, 216, 217, 193, 244, 193, 135, 244,
164377 /* 1450 */ 193, 256, 257, 140, 244, 193, 254, 193, 193, 254,
164378 /* 1460 */ 153, 154, 155, 141, 254, 149, 150, 258, 216, 217,
164379 /* 1470 */ 216, 217, 153, 154, 155, 156, 157, 0, 1, 2,
164380 /* 1480 */ 216, 217, 5, 115, 158, 193, 160, 10, 11, 12,
164381 /* 1490 */ 13, 14, 193, 59, 17, 126, 193, 19, 20, 129,
164382 /* 1500 */ 22, 193, 22, 22, 24, 193, 23, 30, 25, 32,
164383 /* 1510 */ 19, 20, 144, 22, 36, 216, 217, 40, 193, 216,
164384 /* 1520 */ 217, 193, 152, 129, 216, 217, 193, 36, 216, 217,
164385 /* 1530 */ 193, 99, 193, 193, 53, 193, 193, 59, 23, 193,
164386 /* 1540 */ 25, 216, 217, 193, 216, 217, 152, 70, 59, 71,
164387 /* 1550 */ 59, 117, 193, 216, 217, 78, 216, 217, 81, 216,
164388 /* 1560 */ 217, 318, 71, 85, 193, 133, 193, 193, 90, 23,
164389 /* 1570 */ 23, 25, 25, 120, 121, 98, 85, 193, 100, 193,
164390 /* 1580 */ 23, 90, 25, 121, 106, 107, 19, 216, 217, 216,
164391 /* 1590 */ 217, 100, 114, 131, 116, 117, 118, 106, 107, 121,
164392 /* 1600 */ 216, 217, 216, 217, 193, 114, 117, 116, 117, 118,
164393 /* 1610 */ 133, 193, 121, 193, 193, 138, 139, 193, 23, 193,
164394 /* 1620 */ 25, 23, 23, 25, 25, 7, 8, 216, 217, 193,
164395 /* 1630 */ 193, 153, 154, 155, 156, 157, 216, 217, 193, 162,
164396 /* 1640 */ 216, 217, 216, 217, 153, 154, 155, 156, 157, 1,
164397 /* 1650 */ 2, 193, 193, 5, 19, 20, 59, 22, 10, 11,
164398 /* 1660 */ 12, 13, 14, 193, 97, 17, 193, 23, 193, 25,
164399 /* 1670 */ 288, 36, 193, 242, 216, 217, 236, 23, 30, 25,
164400 /* 1680 */ 32, 19, 20, 23, 22, 25, 216, 217, 40, 216,
164401 /* 1690 */ 217, 216, 217, 193, 59, 216, 217, 193, 36, 83,
164402 /* 1700 */ 84, 153, 153, 155, 155, 23, 71, 25, 23, 193,
164403 /* 1710 */ 25, 193, 193, 193, 117, 193, 193, 193, 70, 193,
164404 /* 1720 */ 193, 59, 193, 255, 255, 287, 78, 255, 243, 81,
164405 /* 1730 */ 191, 255, 297, 71, 271, 100, 293, 245, 267, 214,
164406 /* 1740 */ 246, 106, 107, 108, 246, 271, 98, 245, 293, 114,
164407 /* 1750 */ 220, 116, 117, 118, 267, 271, 121, 271, 225, 219,
164408 /* 1760 */ 229, 219, 100, 219, 259, 259, 259, 259, 106, 107,
164409 /* 1770 */ 249, 196, 60, 280, 141, 243, 114, 249, 116, 117,
164410 /* 1780 */ 118, 133, 245, 121, 200, 297, 138, 139, 153, 154,
164411 /* 1790 */ 155, 156, 157, 297, 200, 38, 19, 20, 151, 22,
164412 /* 1800 */ 200, 150, 140, 294, 294, 22, 272, 43, 234, 18,
164413 /* 1810 */ 162, 270, 200, 36, 237, 153, 154, 155, 156, 157,
164414 /* 1820 */ 237, 283, 237, 237, 18, 199, 149, 246, 272, 270,
164415 /* 1830 */ 272, 200, 158, 246, 246, 234, 59, 234, 246, 199,
164416 /* 1840 */ 290, 62, 289, 200, 199, 22, 221, 115, 71, 200,
164417 /* 1850 */ 200, 199, 199, 221, 218, 218, 19, 20, 64, 22,
164418 /* 1860 */ 218, 227, 22, 224, 126, 224, 165, 221, 24, 305,
164419 /* 1870 */ 200, 113, 312, 36, 218, 220, 218, 100, 282, 218,
164420 /* 1880 */ 91, 218, 317, 106, 107, 221, 227, 282, 317, 82,
164421 /* 1890 */ 148, 114, 265, 116, 117, 118, 59, 145, 121, 22,
164422 /* 1900 */ 277, 158, 200, 265, 25, 202, 147, 250, 71, 279,
164423 /* 1910 */ 13, 146, 194, 194, 249, 248, 250, 140, 247, 246,
164424 /* 1920 */ 6, 192, 192, 192, 303, 303, 213, 207, 300, 213,
164425 /* 1930 */ 153, 154, 155, 156, 157, 213, 213, 100, 213, 222,
164426 /* 1940 */ 207, 214, 214, 106, 107, 4, 222, 207, 3, 22,
164427 /* 1950 */ 163, 114, 15, 116, 117, 118, 16, 23, 121, 23,
164428 /* 1960 */ 139, 151, 130, 25, 142, 16, 24, 20, 144, 1,
164429 /* 1970 */ 142, 130, 130, 61, 53, 53, 37, 151, 53, 53,
164430 /* 1980 */ 130, 116, 34, 1, 141, 5, 22, 115, 161, 141,
164431 /* 1990 */ 153, 154, 155, 156, 157, 25, 68, 68, 75, 41,
164432 /* 2000 */ 115, 24, 131, 20, 19, 125, 22, 96, 22, 22,
164433 /* 2010 */ 67, 23, 22, 67, 59, 24, 22, 28, 67, 23,
164434 /* 2020 */ 22, 22, 149, 23, 23, 23, 116, 23, 25, 37,
164435 /* 2030 */ 97, 141, 23, 23, 22, 143, 25, 75, 88, 34,
164436 /* 2040 */ 34, 34, 34, 86, 75, 93, 23, 34, 22, 34,
164437 /* 2050 */ 25, 24, 34, 25, 23, 142, 23, 142, 44, 23,
164438 /* 2060 */ 23, 23, 11, 23, 25, 22, 22, 22, 15, 23,
164439 /* 2070 */ 23, 22, 22, 25, 1, 1, 141, 25, 23, 135,
164440 /* 2080 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
164441 /* 2090 */ 319, 319, 319, 319, 141, 141, 319, 141, 319, 319,
164442 /* 2100 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
164443 /* 2110 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
164444 /* 2120 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
164445 /* 2130 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
164446 /* 2140 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
164447 /* 2150 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
164448 /* 2160 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
164449 /* 2170 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
164450 /* 2180 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
164451 /* 2190 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
164452 /* 2200 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
164453 /* 2210 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
164454 /* 2220 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
164455 /* 2230 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
164456 /* 2240 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
164457 /* 2250 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
164458 /* 2260 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
164459 /* 2270 */ 319, 319, 319, 319, 319, 319, 319, 319, 319, 319,
164460 /* 2280 */ 319, 319, 319,
164461};
164462#define YY_SHIFT_COUNT(575) (575)
164463#define YY_SHIFT_MIN(0) (0)
164464#define YY_SHIFT_MAX(2074) (2074)
164465static const unsigned short int yy_shift_ofst[] = {
164466 /* 0 */ 1648, 1477, 1272, 322, 322, 1, 1319, 1478, 1491, 1837,
164467 /* 10 */ 1837, 1837, 471, 0, 0, 214, 1093, 1837, 1837, 1837,
164468 /* 20 */ 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837,
164469 /* 30 */ 271, 271, 1219, 1219, 216, 88, 1, 1, 1, 1,
164470 /* 40 */ 1, 40, 111, 258, 361, 469, 512, 583, 622, 693,
164471 /* 50 */ 732, 803, 842, 913, 1073, 1093, 1093, 1093, 1093, 1093,
164472 /* 60 */ 1093, 1093, 1093, 1093, 1093, 1093, 1093, 1093, 1093, 1093,
164473 /* 70 */ 1093, 1093, 1093, 1113, 1093, 1216, 957, 957, 1635, 1662,
164474 /* 80 */ 1777, 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837,
164475 /* 90 */ 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837,
164476 /* 100 */ 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837,
164477 /* 110 */ 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837,
164478 /* 120 */ 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837, 1837,
164479 /* 130 */ 137, 181, 181, 181, 181, 181, 181, 181, 94, 430,
164480 /* 140 */ 66, 65, 112, 366, 533, 533, 740, 1261, 533, 533,
164481 /* 150 */ 79, 79, 533, 412, 412, 412, 77, 412, 123, 113,
164482 /* 160 */ 113, 22, 22, 2098, 2098, 328, 328, 328, 239, 468,
164483 /* 170 */ 468, 468, 468, 1015, 1015, 409, 366, 1129, 1186, 533,
164484 /* 180 */ 533, 533, 533, 533, 533, 533, 533, 533, 533, 533,
164485 /* 190 */ 533, 533, 533, 533, 533, 533, 533, 533, 533, 969,
164486 /* 200 */ 621, 621, 533, 642, 788, 788, 1228, 1228, 822, 822,
164487 /* 210 */ 67, 1274, 2098, 2098, 2098, 2098, 2098, 2098, 2098, 1307,
164488 /* 220 */ 954, 954, 585, 472, 640, 387, 695, 538, 541, 700,
164489 /* 230 */ 533, 533, 533, 533, 533, 533, 533, 533, 533, 533,
164490 /* 240 */ 222, 533, 533, 533, 533, 533, 533, 533, 533, 533,
164491 /* 250 */ 533, 533, 533, 1179, 1179, 1179, 533, 533, 533, 565,
164492 /* 260 */ 533, 533, 533, 916, 1144, 533, 533, 1288, 533, 533,
164493 /* 270 */ 533, 533, 533, 533, 533, 533, 639, 1330, 209, 1076,
164494 /* 280 */ 1076, 1076, 1076, 580, 209, 209, 1313, 768, 917, 649,
164495 /* 290 */ 1181, 1316, 405, 1316, 1238, 249, 1181, 1181, 249, 1181,
164496 /* 300 */ 405, 1238, 1369, 464, 1259, 1012, 1012, 1012, 1368, 1368,
164497 /* 310 */ 1368, 1368, 184, 184, 1326, 904, 1287, 1480, 1712, 1712,
164498 /* 320 */ 1633, 1633, 1757, 1757, 1633, 1647, 1651, 1783, 1764, 1791,
164499 /* 330 */ 1791, 1791, 1791, 1633, 1806, 1677, 1651, 1651, 1677, 1783,
164500 /* 340 */ 1764, 1677, 1764, 1677, 1633, 1806, 1674, 1779, 1633, 1806,
164501 /* 350 */ 1823, 1633, 1806, 1633, 1806, 1823, 1732, 1732, 1732, 1794,
164502 /* 360 */ 1840, 1840, 1823, 1732, 1738, 1732, 1794, 1732, 1732, 1701,
164503 /* 370 */ 1844, 1758, 1758, 1823, 1633, 1789, 1789, 1807, 1807, 1742,
164504 /* 380 */ 1752, 1877, 1633, 1743, 1742, 1759, 1765, 1677, 1879, 1897,
164505 /* 390 */ 1897, 1914, 1914, 1914, 2098, 2098, 2098, 2098, 2098, 2098,
164506 /* 400 */ 2098, 2098, 2098, 2098, 2098, 2098, 2098, 2098, 2098, 207,
164507 /* 410 */ 1095, 331, 620, 903, 806, 1074, 1483, 1432, 1481, 1322,
164508 /* 420 */ 1370, 1394, 1515, 1291, 1546, 1547, 1557, 1595, 1598, 1599,
164509 /* 430 */ 1434, 1453, 1618, 1462, 1567, 1489, 1644, 1654, 1616, 1660,
164510 /* 440 */ 1548, 1549, 1682, 1685, 1597, 742, 1941, 1945, 1927, 1787,
164511 /* 450 */ 1937, 1940, 1934, 1936, 1821, 1810, 1832, 1938, 1938, 1942,
164512 /* 460 */ 1822, 1947, 1824, 1949, 1968, 1828, 1841, 1938, 1842, 1912,
164513 /* 470 */ 1939, 1938, 1826, 1921, 1922, 1925, 1926, 1850, 1865, 1948,
164514 /* 480 */ 1843, 1982, 1980, 1964, 1872, 1827, 1928, 1970, 1929, 1923,
164515 /* 490 */ 1958, 1848, 1885, 1977, 1983, 1985, 1871, 1880, 1984, 1943,
164516 /* 500 */ 1986, 1987, 1988, 1990, 1946, 1955, 1991, 1911, 1989, 1994,
164517 /* 510 */ 1951, 1992, 1996, 1873, 1998, 2000, 2001, 2002, 2003, 2004,
164518 /* 520 */ 1999, 1933, 1890, 2009, 2010, 1910, 2005, 2012, 1892, 2011,
164519 /* 530 */ 2006, 2007, 2008, 2013, 1950, 1962, 1957, 2014, 1969, 1952,
164520 /* 540 */ 2015, 2023, 2026, 2027, 2025, 2028, 2018, 1913, 1915, 2031,
164521 /* 550 */ 2011, 2033, 2036, 2037, 2038, 2039, 2040, 2043, 2051, 2044,
164522 /* 560 */ 2045, 2046, 2047, 2049, 2050, 2048, 1944, 1935, 1953, 1954,
164523 /* 570 */ 1956, 2052, 2055, 2053, 2073, 2074,
164524};
164525#define YY_REDUCE_COUNT(408) (408)
164526#define YY_REDUCE_MIN(-271) (-271)
164527#define YY_REDUCE_MAX(1740) (1740)
164528static const short yy_reduce_ofst[] = {
164529 /* 0 */ -125, 733, 789, 241, 293, -123, -193, -191, -183, -187,
164530 /* 10 */ 166, 238, 133, -207, -199, -267, -176, -6, 204, 489,
164531 /* 20 */ 576, -175, 598, 686, 615, 725, 860, 778, 781, 857,
164532 /* 30 */ 616, 887, 87, 240, -192, 408, 626, 796, 843, 854,
164533 /* 40 */ 1003, -271, -271, -271, -271, -271, -271, -271, -271, -271,
164534 /* 50 */ -271, -271, -271, -271, -271, -271, -271, -271, -271, -271,
164535 /* 60 */ -271, -271, -271, -271, -271, -271, -271, -271, -271, -271,
164536 /* 70 */ -271, -271, -271, -271, -271, -271, -271, -271, 80, 83,
164537 /* 80 */ 313, 886, 888, 996, 1034, 1059, 1081, 1100, 1117, 1152,
164538 /* 90 */ 1155, 1163, 1165, 1167, 1169, 1172, 1180, 1182, 1184, 1198,
164539 /* 100 */ 1200, 1213, 1215, 1225, 1227, 1252, 1254, 1264, 1299, 1303,
164540 /* 110 */ 1308, 1312, 1325, 1328, 1337, 1340, 1343, 1371, 1373, 1384,
164541 /* 120 */ 1386, 1411, 1420, 1424, 1426, 1458, 1470, 1473, 1475, 1479,
164542 /* 130 */ -271, -271, -271, -271, -271, -271, -271, -271, -271, -271,
164543 /* 140 */ -271, 138, 459, 396, -158, 470, 302, -212, 521, 201,
164544 /* 150 */ -195, -92, 559, 630, 632, 630, -271, 632, 901, 63,
164545 /* 160 */ 407, -271, -271, -271, -271, 161, 161, 161, 251, 335,
164546 /* 170 */ 847, 960, 980, 537, 588, 618, 628, 688, 688, -166,
164547 /* 180 */ -161, 674, 790, 794, 799, 851, 852, -122, 680, -120,
164548 /* 190 */ 995, 1038, 415, 1051, 893, 798, 962, 400, 1086, 779,
164549 /* 200 */ 923, 924, 263, 1041, 979, 990, 1083, 1097, 1031, 1194,
164550 /* 210 */ 362, 994, 1139, 1005, 1037, 1202, 1205, 1195, 1210, -194,
164551 /* 220 */ 56, 185, -135, 232, 522, 560, 601, 617, 669, 683,
164552 /* 230 */ 711, 856, 908, 941, 1048, 1101, 1147, 1257, 1262, 1265,
164553 /* 240 */ 392, 1292, 1333, 1339, 1342, 1346, 1350, 1359, 1374, 1418,
164554 /* 250 */ 1421, 1436, 1437, 593, 755, 770, 997, 1445, 1459, 1209,
164555 /* 260 */ 1500, 1504, 1516, 1132, 1243, 1518, 1519, 1440, 1520, 560,
164556 /* 270 */ 1522, 1523, 1524, 1526, 1527, 1529, 1382, 1438, 1431, 1468,
164557 /* 280 */ 1469, 1472, 1476, 1209, 1431, 1431, 1485, 1525, 1539, 1435,
164558 /* 290 */ 1463, 1471, 1492, 1487, 1443, 1494, 1474, 1484, 1498, 1486,
164559 /* 300 */ 1502, 1455, 1530, 1531, 1533, 1540, 1542, 1544, 1505, 1506,
164560 /* 310 */ 1507, 1508, 1521, 1528, 1493, 1537, 1532, 1575, 1488, 1496,
164561 /* 320 */ 1584, 1594, 1509, 1510, 1600, 1538, 1534, 1541, 1574, 1577,
164562 /* 330 */ 1583, 1585, 1586, 1612, 1626, 1581, 1556, 1558, 1587, 1559,
164563 /* 340 */ 1601, 1588, 1603, 1592, 1631, 1640, 1550, 1553, 1643, 1645,
164564 /* 350 */ 1625, 1649, 1652, 1650, 1653, 1632, 1636, 1637, 1642, 1634,
164565 /* 360 */ 1639, 1641, 1646, 1656, 1655, 1658, 1659, 1661, 1663, 1560,
164566 /* 370 */ 1564, 1596, 1605, 1664, 1670, 1565, 1571, 1627, 1638, 1657,
164567 /* 380 */ 1665, 1623, 1702, 1630, 1666, 1667, 1671, 1673, 1703, 1718,
164568 /* 390 */ 1719, 1729, 1730, 1731, 1621, 1622, 1628, 1720, 1713, 1716,
164569 /* 400 */ 1722, 1723, 1733, 1717, 1724, 1727, 1728, 1725, 1740,
164570};
164571static const YYACTIONTYPEunsigned short int yy_default[] = {
164572 /* 0 */ 1647, 1647, 1647, 1475, 1240, 1351, 1240, 1240, 1240, 1475,
164573 /* 10 */ 1475, 1475, 1240, 1381, 1381, 1528, 1273, 1240, 1240, 1240,
164574 /* 20 */ 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1474, 1240, 1240,
164575 /* 30 */ 1240, 1240, 1563, 1563, 1240, 1240, 1240, 1240, 1240, 1240,
164576 /* 40 */ 1240, 1240, 1390, 1240, 1397, 1240, 1240, 1240, 1240, 1240,
164577 /* 50 */ 1476, 1477, 1240, 1240, 1240, 1527, 1529, 1492, 1404, 1403,
164578 /* 60 */ 1402, 1401, 1510, 1369, 1395, 1388, 1392, 1470, 1471, 1469,
164579 /* 70 */ 1473, 1477, 1476, 1240, 1391, 1438, 1454, 1437, 1240, 1240,
164580 /* 80 */ 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240,
164581 /* 90 */ 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240,
164582 /* 100 */ 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240,
164583 /* 110 */ 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240,
164584 /* 120 */ 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240,
164585 /* 130 */ 1446, 1453, 1452, 1451, 1460, 1450, 1447, 1440, 1439, 1441,
164586 /* 140 */ 1442, 1240, 1240, 1264, 1240, 1240, 1261, 1315, 1240, 1240,
164587 /* 150 */ 1240, 1240, 1240, 1547, 1546, 1240, 1443, 1240, 1273, 1432,
164588 /* 160 */ 1431, 1457, 1444, 1456, 1455, 1535, 1599, 1598, 1493, 1240,
164589 /* 170 */ 1240, 1240, 1240, 1240, 1240, 1563, 1240, 1240, 1240, 1240,
164590 /* 180 */ 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240,
164591 /* 190 */ 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1371,
164592 /* 200 */ 1563, 1563, 1240, 1273, 1563, 1563, 1372, 1372, 1269, 1269,
164593 /* 210 */ 1375, 1240, 1542, 1342, 1342, 1342, 1342, 1351, 1342, 1240,
164594 /* 220 */ 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240,
164595 /* 230 */ 1240, 1240, 1240, 1240, 1532, 1530, 1240, 1240, 1240, 1240,
164596 /* 240 */ 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240,
164597 /* 250 */ 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240,
164598 /* 260 */ 1240, 1240, 1240, 1347, 1240, 1240, 1240, 1240, 1240, 1240,
164599 /* 270 */ 1240, 1240, 1240, 1240, 1240, 1592, 1240, 1505, 1329, 1347,
164600 /* 280 */ 1347, 1347, 1347, 1349, 1330, 1328, 1341, 1274, 1247, 1639,
164601 /* 290 */ 1407, 1396, 1348, 1396, 1636, 1394, 1407, 1407, 1394, 1407,
164602 /* 300 */ 1348, 1636, 1290, 1615, 1285, 1381, 1381, 1381, 1371, 1371,
164603 /* 310 */ 1371, 1371, 1375, 1375, 1472, 1348, 1341, 1240, 1639, 1639,
164604 /* 320 */ 1357, 1357, 1638, 1638, 1357, 1493, 1623, 1416, 1318, 1324,
164605 /* 330 */ 1324, 1324, 1324, 1357, 1258, 1394, 1623, 1623, 1394, 1416,
164606 /* 340 */ 1318, 1394, 1318, 1394, 1357, 1258, 1509, 1633, 1357, 1258,
164607 /* 350 */ 1483, 1357, 1258, 1357, 1258, 1483, 1316, 1316, 1316, 1305,
164608 /* 360 */ 1240, 1240, 1483, 1316, 1290, 1316, 1305, 1316, 1316, 1581,
164609 /* 370 */ 1240, 1487, 1487, 1483, 1357, 1573, 1573, 1384, 1384, 1389,
164610 /* 380 */ 1375, 1478, 1357, 1240, 1389, 1387, 1385, 1394, 1308, 1595,
164611 /* 390 */ 1595, 1591, 1591, 1591, 1644, 1644, 1542, 1608, 1273, 1273,
164612 /* 400 */ 1273, 1273, 1608, 1292, 1292, 1274, 1274, 1273, 1608, 1240,
164613 /* 410 */ 1240, 1240, 1240, 1240, 1240, 1603, 1240, 1537, 1494, 1361,
164614 /* 420 */ 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240,
164615 /* 430 */ 1240, 1240, 1240, 1240, 1548, 1240, 1240, 1240, 1240, 1240,
164616 /* 440 */ 1240, 1240, 1240, 1240, 1240, 1421, 1240, 1243, 1539, 1240,
164617 /* 450 */ 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1398, 1399, 1362,
164618 /* 460 */ 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1413, 1240, 1240,
164619 /* 470 */ 1240, 1408, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240,
164620 /* 480 */ 1635, 1240, 1240, 1240, 1240, 1240, 1240, 1508, 1507, 1240,
164621 /* 490 */ 1240, 1359, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240,
164622 /* 500 */ 1240, 1240, 1240, 1240, 1240, 1288, 1240, 1240, 1240, 1240,
164623 /* 510 */ 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240,
164624 /* 520 */ 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1386,
164625 /* 530 */ 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240,
164626 /* 540 */ 1240, 1240, 1240, 1240, 1578, 1376, 1240, 1240, 1240, 1240,
164627 /* 550 */ 1626, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240, 1240,
164628 /* 560 */ 1240, 1240, 1240, 1240, 1240, 1619, 1332, 1423, 1240, 1422,
164629 /* 570 */ 1426, 1262, 1240, 1252, 1240, 1240,
164630};
164631/********** End of lemon-generated parsing tables *****************************/
164632
164633/* The next table maps tokens (terminal symbols) into fallback tokens.
164634** If a construct like the following:
164635**
164636** %fallback ID X Y Z.
164637**
164638** appears in the grammar, then ID becomes a fallback token for X, Y,
164639** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
164640** but it does not parse, the type of the token is changed to ID and
164641** the parse is retried before an error is thrown.
164642**
164643** This feature can be used, for example, to cause some keywords in a language
164644** to revert to identifiers if they keyword does not apply in the context where
164645** it appears.
164646*/
164647#ifdef YYFALLBACK1
164648static const YYCODETYPEunsigned short int yyFallback[] = {
164649 0, /* $ => nothing */
164650 0, /* SEMI => nothing */
164651 59, /* EXPLAIN => ID */
164652 59, /* QUERY => ID */
164653 59, /* PLAN => ID */
164654 59, /* BEGIN => ID */
164655 0, /* TRANSACTION => nothing */
164656 59, /* DEFERRED => ID */
164657 59, /* IMMEDIATE => ID */
164658 59, /* EXCLUSIVE => ID */
164659 0, /* COMMIT => nothing */
164660 59, /* END => ID */
164661 59, /* ROLLBACK => ID */
164662 59, /* SAVEPOINT => ID */
164663 59, /* RELEASE => ID */
164664 0, /* TO => nothing */
164665 0, /* TABLE => nothing */
164666 0, /* CREATE => nothing */
164667 59, /* IF => ID */
164668 0, /* NOT => nothing */
164669 0, /* EXISTS => nothing */
164670 59, /* TEMP => ID */
164671 0, /* LP => nothing */
164672 0, /* RP => nothing */
164673 0, /* AS => nothing */
164674 0, /* COMMA => nothing */
164675 59, /* WITHOUT => ID */
164676 59, /* ABORT => ID */
164677 59, /* ACTION => ID */
164678 59, /* AFTER => ID */
164679 59, /* ANALYZE => ID */
164680 59, /* ASC => ID */
164681 59, /* ATTACH => ID */
164682 59, /* BEFORE => ID */
164683 59, /* BY => ID */
164684 59, /* CASCADE => ID */
164685 59, /* CAST => ID */
164686 59, /* CONFLICT => ID */
164687 59, /* DATABASE => ID */
164688 59, /* DESC => ID */
164689 59, /* DETACH => ID */
164690 59, /* EACH => ID */
164691 59, /* FAIL => ID */
164692 0, /* OR => nothing */
164693 0, /* AND => nothing */
164694 0, /* IS => nothing */
164695 59, /* MATCH => ID */
164696 59, /* LIKE_KW => ID */
164697 0, /* BETWEEN => nothing */
164698 0, /* IN => nothing */
164699 0, /* ISNULL => nothing */
164700 0, /* NOTNULL => nothing */
164701 0, /* NE => nothing */
164702 0, /* EQ => nothing */
164703 0, /* GT => nothing */
164704 0, /* LE => nothing */
164705 0, /* LT => nothing */
164706 0, /* GE => nothing */
164707 0, /* ESCAPE => nothing */
164708 0, /* ID => nothing */
164709 59, /* COLUMNKW => ID */
164710 59, /* DO => ID */
164711 59, /* FOR => ID */
164712 59, /* IGNORE => ID */
164713 59, /* INITIALLY => ID */
164714 59, /* INSTEAD => ID */
164715 59, /* NO => ID */
164716 59, /* KEY => ID */
164717 59, /* OF => ID */
164718 59, /* OFFSET => ID */
164719 59, /* PRAGMA => ID */
164720 59, /* RAISE => ID */
164721 59, /* RECURSIVE => ID */
164722 59, /* REPLACE => ID */
164723 59, /* RESTRICT => ID */
164724 59, /* ROW => ID */
164725 59, /* ROWS => ID */
164726 59, /* TRIGGER => ID */
164727 59, /* VACUUM => ID */
164728 59, /* VIEW => ID */
164729 59, /* VIRTUAL => ID */
164730 59, /* WITH => ID */
164731 59, /* NULLS => ID */
164732 59, /* FIRST => ID */
164733 59, /* LAST => ID */
164734 59, /* CURRENT => ID */
164735 59, /* FOLLOWING => ID */
164736 59, /* PARTITION => ID */
164737 59, /* PRECEDING => ID */
164738 59, /* RANGE => ID */
164739 59, /* UNBOUNDED => ID */
164740 59, /* EXCLUDE => ID */
164741 59, /* GROUPS => ID */
164742 59, /* OTHERS => ID */
164743 59, /* TIES => ID */
164744 59, /* GENERATED => ID */
164745 59, /* ALWAYS => ID */
164746 59, /* MATERIALIZED => ID */
164747 59, /* REINDEX => ID */
164748 59, /* RENAME => ID */
164749 59, /* CTIME_KW => ID */
164750 0, /* ANY => nothing */
164751 0, /* BITAND => nothing */
164752 0, /* BITOR => nothing */
164753 0, /* LSHIFT => nothing */
164754 0, /* RSHIFT => nothing */
164755 0, /* PLUS => nothing */
164756 0, /* MINUS => nothing */
164757 0, /* STAR => nothing */
164758 0, /* SLASH => nothing */
164759 0, /* REM => nothing */
164760 0, /* CONCAT => nothing */
164761 0, /* PTR => nothing */
164762 0, /* COLLATE => nothing */
164763 0, /* BITNOT => nothing */
164764 0, /* ON => nothing */
164765 0, /* INDEXED => nothing */
164766 0, /* STRING => nothing */
164767 0, /* JOIN_KW => nothing */
164768 0, /* CONSTRAINT => nothing */
164769 0, /* DEFAULT => nothing */
164770 0, /* NULL => nothing */
164771 0, /* PRIMARY => nothing */
164772 0, /* UNIQUE => nothing */
164773 0, /* CHECK => nothing */
164774 0, /* REFERENCES => nothing */
164775 0, /* AUTOINCR => nothing */
164776 0, /* INSERT => nothing */
164777 0, /* DELETE => nothing */
164778 0, /* UPDATE => nothing */
164779 0, /* SET => nothing */
164780 0, /* DEFERRABLE => nothing */
164781 0, /* FOREIGN => nothing */
164782 0, /* DROP => nothing */
164783 0, /* UNION => nothing */
164784 0, /* ALL => nothing */
164785 0, /* EXCEPT => nothing */
164786 0, /* INTERSECT => nothing */
164787 0, /* SELECT => nothing */
164788 0, /* VALUES => nothing */
164789 0, /* DISTINCT => nothing */
164790 0, /* DOT => nothing */
164791 0, /* FROM => nothing */
164792 0, /* JOIN => nothing */
164793 0, /* USING => nothing */
164794 0, /* ORDER => nothing */
164795 0, /* GROUP => nothing */
164796 0, /* HAVING => nothing */
164797 0, /* LIMIT => nothing */
164798 0, /* WHERE => nothing */
164799 0, /* RETURNING => nothing */
164800 0, /* INTO => nothing */
164801 0, /* NOTHING => nothing */
164802 0, /* FLOAT => nothing */
164803 0, /* BLOB => nothing */
164804 0, /* INTEGER => nothing */
164805 0, /* VARIABLE => nothing */
164806 0, /* CASE => nothing */
164807 0, /* WHEN => nothing */
164808 0, /* THEN => nothing */
164809 0, /* ELSE => nothing */
164810 0, /* INDEX => nothing */
164811 0, /* ALTER => nothing */
164812 0, /* ADD => nothing */
164813 0, /* WINDOW => nothing */
164814 0, /* OVER => nothing */
164815 0, /* FILTER => nothing */
164816 0, /* COLUMN => nothing */
164817 0, /* AGG_FUNCTION => nothing */
164818 0, /* AGG_COLUMN => nothing */
164819 0, /* TRUEFALSE => nothing */
164820 0, /* ISNOT => nothing */
164821 0, /* FUNCTION => nothing */
164822 0, /* UMINUS => nothing */
164823 0, /* UPLUS => nothing */
164824 0, /* TRUTH => nothing */
164825 0, /* REGISTER => nothing */
164826 0, /* VECTOR => nothing */
164827 0, /* SELECT_COLUMN => nothing */
164828 0, /* IF_NULL_ROW => nothing */
164829 0, /* ASTERISK => nothing */
164830 0, /* SPAN => nothing */
164831 0, /* ERROR => nothing */
164832 0, /* SPACE => nothing */
164833 0, /* ILLEGAL => nothing */
164834};
164835#endif /* YYFALLBACK */
164836
164837/* The following structure represents a single element of the
164838** parser's stack. Information stored includes:
164839**
164840** + The state number for the parser at this level of the stack.
164841**
164842** + The value of the token stored at this level of the stack.
164843** (In other words, the "major" token.)
164844**
164845** + The semantic value stored at this level of the stack. This is
164846** the information used by the action routines in the grammar.
164847** It is sometimes called the "minor" token.
164848**
164849** After the "shift" half of a SHIFTREDUCE action, the stateno field
164850** actually contains the reduce action for the second half of the
164851** SHIFTREDUCE.
164852*/
164853struct yyStackEntry {
164854 YYACTIONTYPEunsigned short int stateno; /* The state-number, or reduce action in SHIFTREDUCE */
164855 YYCODETYPEunsigned short int major; /* The major token value. This is the code
164856 ** number for the token at this stack level */
164857 YYMINORTYPE minor; /* The user-supplied minor token value. This
164858 ** is the value of the token */
164859};
164860typedef struct yyStackEntry yyStackEntry;
164861
164862/* The state of the parser is completely contained in an instance of
164863** the following structure */
164864struct yyParser {
164865 yyStackEntry *yytos; /* Pointer to top element of the stack */
164866#ifdef YYTRACKMAXSTACKDEPTH
164867 int yyhwm; /* High-water mark of the stack */
164868#endif
164869#ifndef YYNOERRORRECOVERY1
164870 int yyerrcnt; /* Shifts left before out of the error */
164871#endif
164872 sqlite3ParserARG_SDECL /* A place to hold %extra_argument */
164873 sqlite3ParserCTX_SDECLParse *pParse; /* A place to hold %extra_context */
164874#if YYSTACKDEPTH100<=0
164875 int yystksz; /* Current side of the stack */
164876 yyStackEntry *yystack; /* The parser's stack */
164877 yyStackEntry yystk0; /* First stack entry */
164878#else
164879 yyStackEntry yystack[YYSTACKDEPTH100]; /* The parser's stack */
164880 yyStackEntry *yystackEnd; /* Last entry in the stack */
164881#endif
164882};
164883typedef struct yyParser yyParser;
164884
164885/* #include <assert.h> */
164886#ifndef NDEBUG1
164887/* #include <stdio.h> */
164888static FILE *yyTraceFILE = 0;
164889static char *yyTracePrompt = 0;
164890#endif /* NDEBUG */
164891
164892#ifndef NDEBUG1
164893/*
164894** Turn parser tracing on by giving a stream to which to write the trace
164895** and a prompt to preface each trace message. Tracing is turned off
164896** by making either argument NULL
164897**
164898** Inputs:
164899** <ul>
164900** <li> A FILE* to which trace output should be written.
164901** If NULL, then tracing is turned off.
164902** <li> A prefix string written at the beginning of every
164903** line of trace output. If NULL, then tracing is
164904** turned off.
164905** </ul>
164906**
164907** Outputs:
164908** None.
164909*/
164910SQLITE_PRIVATEstatic void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){
164911 yyTraceFILE = TraceFILE;
164912 yyTracePrompt = zTracePrompt;
164913 if( yyTraceFILE==0 ) yyTracePrompt = 0;
164914 else if( yyTracePrompt==0 ) yyTraceFILE = 0;
164915}
164916#endif /* NDEBUG */
164917
164918#if defined(YYCOVERAGE) || !defined(NDEBUG1)
164919/* For tracing shifts, the names of all terminals and nonterminals
164920** are required. The following table supplies these names */
164921static const char *const yyTokenName[] = {
164922 /* 0 */ "$",
164923 /* 1 */ "SEMI",
164924 /* 2 */ "EXPLAIN",
164925 /* 3 */ "QUERY",
164926 /* 4 */ "PLAN",
164927 /* 5 */ "BEGIN",
164928 /* 6 */ "TRANSACTION",
164929 /* 7 */ "DEFERRED",
164930 /* 8 */ "IMMEDIATE",
164931 /* 9 */ "EXCLUSIVE",
164932 /* 10 */ "COMMIT",
164933 /* 11 */ "END",
164934 /* 12 */ "ROLLBACK",
164935 /* 13 */ "SAVEPOINT",
164936 /* 14 */ "RELEASE",
164937 /* 15 */ "TO",
164938 /* 16 */ "TABLE",
164939 /* 17 */ "CREATE",
164940 /* 18 */ "IF",
164941 /* 19 */ "NOT",
164942 /* 20 */ "EXISTS",
164943 /* 21 */ "TEMP",
164944 /* 22 */ "LP",
164945 /* 23 */ "RP",
164946 /* 24 */ "AS",
164947 /* 25 */ "COMMA",
164948 /* 26 */ "WITHOUT",
164949 /* 27 */ "ABORT",
164950 /* 28 */ "ACTION",
164951 /* 29 */ "AFTER",
164952 /* 30 */ "ANALYZE",
164953 /* 31 */ "ASC",
164954 /* 32 */ "ATTACH",
164955 /* 33 */ "BEFORE",
164956 /* 34 */ "BY",
164957 /* 35 */ "CASCADE",
164958 /* 36 */ "CAST",
164959 /* 37 */ "CONFLICT",
164960 /* 38 */ "DATABASE",
164961 /* 39 */ "DESC",
164962 /* 40 */ "DETACH",
164963 /* 41 */ "EACH",
164964 /* 42 */ "FAIL",
164965 /* 43 */ "OR",
164966 /* 44 */ "AND",
164967 /* 45 */ "IS",
164968 /* 46 */ "MATCH",
164969 /* 47 */ "LIKE_KW",
164970 /* 48 */ "BETWEEN",
164971 /* 49 */ "IN",
164972 /* 50 */ "ISNULL",
164973 /* 51 */ "NOTNULL",
164974 /* 52 */ "NE",
164975 /* 53 */ "EQ",
164976 /* 54 */ "GT",
164977 /* 55 */ "LE",
164978 /* 56 */ "LT",
164979 /* 57 */ "GE",
164980 /* 58 */ "ESCAPE",
164981 /* 59 */ "ID",
164982 /* 60 */ "COLUMNKW",
164983 /* 61 */ "DO",
164984 /* 62 */ "FOR",
164985 /* 63 */ "IGNORE",
164986 /* 64 */ "INITIALLY",
164987 /* 65 */ "INSTEAD",
164988 /* 66 */ "NO",
164989 /* 67 */ "KEY",
164990 /* 68 */ "OF",
164991 /* 69 */ "OFFSET",
164992 /* 70 */ "PRAGMA",
164993 /* 71 */ "RAISE",
164994 /* 72 */ "RECURSIVE",
164995 /* 73 */ "REPLACE",
164996 /* 74 */ "RESTRICT",
164997 /* 75 */ "ROW",
164998 /* 76 */ "ROWS",
164999 /* 77 */ "TRIGGER",
165000 /* 78 */ "VACUUM",
165001 /* 79 */ "VIEW",
165002 /* 80 */ "VIRTUAL",
165003 /* 81 */ "WITH",
165004 /* 82 */ "NULLS",
165005 /* 83 */ "FIRST",
165006 /* 84 */ "LAST",
165007 /* 85 */ "CURRENT",
165008 /* 86 */ "FOLLOWING",
165009 /* 87 */ "PARTITION",
165010 /* 88 */ "PRECEDING",
165011 /* 89 */ "RANGE",
165012 /* 90 */ "UNBOUNDED",
165013 /* 91 */ "EXCLUDE",
165014 /* 92 */ "GROUPS",
165015 /* 93 */ "OTHERS",
165016 /* 94 */ "TIES",
165017 /* 95 */ "GENERATED",
165018 /* 96 */ "ALWAYS",
165019 /* 97 */ "MATERIALIZED",
165020 /* 98 */ "REINDEX",
165021 /* 99 */ "RENAME",
165022 /* 100 */ "CTIME_KW",
165023 /* 101 */ "ANY",
165024 /* 102 */ "BITAND",
165025 /* 103 */ "BITOR",
165026 /* 104 */ "LSHIFT",
165027 /* 105 */ "RSHIFT",
165028 /* 106 */ "PLUS",
165029 /* 107 */ "MINUS",
165030 /* 108 */ "STAR",
165031 /* 109 */ "SLASH",
165032 /* 110 */ "REM",
165033 /* 111 */ "CONCAT",
165034 /* 112 */ "PTR",
165035 /* 113 */ "COLLATE",
165036 /* 114 */ "BITNOT",
165037 /* 115 */ "ON",
165038 /* 116 */ "INDEXED",
165039 /* 117 */ "STRING",
165040 /* 118 */ "JOIN_KW",
165041 /* 119 */ "CONSTRAINT",
165042 /* 120 */ "DEFAULT",
165043 /* 121 */ "NULL",
165044 /* 122 */ "PRIMARY",
165045 /* 123 */ "UNIQUE",
165046 /* 124 */ "CHECK",
165047 /* 125 */ "REFERENCES",
165048 /* 126 */ "AUTOINCR",
165049 /* 127 */ "INSERT",
165050 /* 128 */ "DELETE",
165051 /* 129 */ "UPDATE",
165052 /* 130 */ "SET",
165053 /* 131 */ "DEFERRABLE",
165054 /* 132 */ "FOREIGN",
165055 /* 133 */ "DROP",
165056 /* 134 */ "UNION",
165057 /* 135 */ "ALL",
165058 /* 136 */ "EXCEPT",
165059 /* 137 */ "INTERSECT",
165060 /* 138 */ "SELECT",
165061 /* 139 */ "VALUES",
165062 /* 140 */ "DISTINCT",
165063 /* 141 */ "DOT",
165064 /* 142 */ "FROM",
165065 /* 143 */ "JOIN",
165066 /* 144 */ "USING",
165067 /* 145 */ "ORDER",
165068 /* 146 */ "GROUP",
165069 /* 147 */ "HAVING",
165070 /* 148 */ "LIMIT",
165071 /* 149 */ "WHERE",
165072 /* 150 */ "RETURNING",
165073 /* 151 */ "INTO",
165074 /* 152 */ "NOTHING",
165075 /* 153 */ "FLOAT",
165076 /* 154 */ "BLOB",
165077 /* 155 */ "INTEGER",
165078 /* 156 */ "VARIABLE",
165079 /* 157 */ "CASE",
165080 /* 158 */ "WHEN",
165081 /* 159 */ "THEN",
165082 /* 160 */ "ELSE",
165083 /* 161 */ "INDEX",
165084 /* 162 */ "ALTER",
165085 /* 163 */ "ADD",
165086 /* 164 */ "WINDOW",
165087 /* 165 */ "OVER",
165088 /* 166 */ "FILTER",
165089 /* 167 */ "COLUMN",
165090 /* 168 */ "AGG_FUNCTION",
165091 /* 169 */ "AGG_COLUMN",
165092 /* 170 */ "TRUEFALSE",
165093 /* 171 */ "ISNOT",
165094 /* 172 */ "FUNCTION",
165095 /* 173 */ "UMINUS",
165096 /* 174 */ "UPLUS",
165097 /* 175 */ "TRUTH",
165098 /* 176 */ "REGISTER",
165099 /* 177 */ "VECTOR",
165100 /* 178 */ "SELECT_COLUMN",
165101 /* 179 */ "IF_NULL_ROW",
165102 /* 180 */ "ASTERISK",
165103 /* 181 */ "SPAN",
165104 /* 182 */ "ERROR",
165105 /* 183 */ "SPACE",
165106 /* 184 */ "ILLEGAL",
165107 /* 185 */ "input",
165108 /* 186 */ "cmdlist",
165109 /* 187 */ "ecmd",
165110 /* 188 */ "cmdx",
165111 /* 189 */ "explain",
165112 /* 190 */ "cmd",
165113 /* 191 */ "transtype",
165114 /* 192 */ "trans_opt",
165115 /* 193 */ "nm",
165116 /* 194 */ "savepoint_opt",
165117 /* 195 */ "create_table",
165118 /* 196 */ "create_table_args",
165119 /* 197 */ "createkw",
165120 /* 198 */ "temp",
165121 /* 199 */ "ifnotexists",
165122 /* 200 */ "dbnm",
165123 /* 201 */ "columnlist",
165124 /* 202 */ "conslist_opt",
165125 /* 203 */ "table_option_set",
165126 /* 204 */ "select",
165127 /* 205 */ "table_option",
165128 /* 206 */ "columnname",
165129 /* 207 */ "carglist",
165130 /* 208 */ "typetoken",
165131 /* 209 */ "typename",
165132 /* 210 */ "signed",
165133 /* 211 */ "plus_num",
165134 /* 212 */ "minus_num",
165135 /* 213 */ "scanpt",
165136 /* 214 */ "scantok",
165137 /* 215 */ "ccons",
165138 /* 216 */ "term",
165139 /* 217 */ "expr",
165140 /* 218 */ "onconf",
165141 /* 219 */ "sortorder",
165142 /* 220 */ "autoinc",
165143 /* 221 */ "eidlist_opt",
165144 /* 222 */ "refargs",
165145 /* 223 */ "defer_subclause",
165146 /* 224 */ "generated",
165147 /* 225 */ "refarg",
165148 /* 226 */ "refact",
165149 /* 227 */ "init_deferred_pred_opt",
165150 /* 228 */ "conslist",
165151 /* 229 */ "tconscomma",
165152 /* 230 */ "tcons",
165153 /* 231 */ "sortlist",
165154 /* 232 */ "eidlist",
165155 /* 233 */ "defer_subclause_opt",
165156 /* 234 */ "orconf",
165157 /* 235 */ "resolvetype",
165158 /* 236 */ "raisetype",
165159 /* 237 */ "ifexists",
165160 /* 238 */ "fullname",
165161 /* 239 */ "selectnowith",
165162 /* 240 */ "oneselect",
165163 /* 241 */ "wqlist",
165164 /* 242 */ "multiselect_op",
165165 /* 243 */ "distinct",
165166 /* 244 */ "selcollist",
165167 /* 245 */ "from",
165168 /* 246 */ "where_opt",
165169 /* 247 */ "groupby_opt",
165170 /* 248 */ "having_opt",
165171 /* 249 */ "orderby_opt",
165172 /* 250 */ "limit_opt",
165173 /* 251 */ "window_clause",
165174 /* 252 */ "values",
165175 /* 253 */ "nexprlist",
165176 /* 254 */ "sclp",
165177 /* 255 */ "as",
165178 /* 256 */ "seltablist",
165179 /* 257 */ "stl_prefix",
165180 /* 258 */ "joinop",
165181 /* 259 */ "on_using",
165182 /* 260 */ "indexed_by",
165183 /* 261 */ "exprlist",
165184 /* 262 */ "xfullname",
165185 /* 263 */ "idlist",
165186 /* 264 */ "indexed_opt",
165187 /* 265 */ "nulls",
165188 /* 266 */ "with",
165189 /* 267 */ "where_opt_ret",
165190 /* 268 */ "setlist",
165191 /* 269 */ "insert_cmd",
165192 /* 270 */ "idlist_opt",
165193 /* 271 */ "upsert",
165194 /* 272 */ "returning",
165195 /* 273 */ "filter_over",
165196 /* 274 */ "likeop",
165197 /* 275 */ "between_op",
165198 /* 276 */ "in_op",
165199 /* 277 */ "paren_exprlist",
165200 /* 278 */ "case_operand",
165201 /* 279 */ "case_exprlist",
165202 /* 280 */ "case_else",
165203 /* 281 */ "uniqueflag",
165204 /* 282 */ "collate",
165205 /* 283 */ "vinto",
165206 /* 284 */ "nmnum",
165207 /* 285 */ "trigger_decl",
165208 /* 286 */ "trigger_cmd_list",
165209 /* 287 */ "trigger_time",
165210 /* 288 */ "trigger_event",
165211 /* 289 */ "foreach_clause",
165212 /* 290 */ "when_clause",
165213 /* 291 */ "trigger_cmd",
165214 /* 292 */ "trnm",
165215 /* 293 */ "tridxby",
165216 /* 294 */ "database_kw_opt",
165217 /* 295 */ "key_opt",
165218 /* 296 */ "add_column_fullname",
165219 /* 297 */ "kwcolumn_opt",
165220 /* 298 */ "create_vtab",
165221 /* 299 */ "vtabarglist",
165222 /* 300 */ "vtabarg",
165223 /* 301 */ "vtabargtoken",
165224 /* 302 */ "lp",
165225 /* 303 */ "anylist",
165226 /* 304 */ "wqitem",
165227 /* 305 */ "wqas",
165228 /* 306 */ "windowdefn_list",
165229 /* 307 */ "windowdefn",
165230 /* 308 */ "window",
165231 /* 309 */ "frame_opt",
165232 /* 310 */ "part_opt",
165233 /* 311 */ "filter_clause",
165234 /* 312 */ "over_clause",
165235 /* 313 */ "range_or_rows",
165236 /* 314 */ "frame_bound",
165237 /* 315 */ "frame_bound_s",
165238 /* 316 */ "frame_bound_e",
165239 /* 317 */ "frame_exclude_opt",
165240 /* 318 */ "frame_exclude",
165241};
165242#endif /* defined(YYCOVERAGE) || !defined(NDEBUG) */
165243
165244#ifndef NDEBUG1
165245/* For tracing reduce actions, the names of all rules are required.
165246*/
165247static const char *const yyRuleName[] = {
165248 /* 0 */ "explain ::= EXPLAIN",
165249 /* 1 */ "explain ::= EXPLAIN QUERY PLAN",
165250 /* 2 */ "cmdx ::= cmd",
165251 /* 3 */ "cmd ::= BEGIN transtype trans_opt",
165252 /* 4 */ "transtype ::=",
165253 /* 5 */ "transtype ::= DEFERRED",
165254 /* 6 */ "transtype ::= IMMEDIATE",
165255 /* 7 */ "transtype ::= EXCLUSIVE",
165256 /* 8 */ "cmd ::= COMMIT|END trans_opt",
165257 /* 9 */ "cmd ::= ROLLBACK trans_opt",
165258 /* 10 */ "cmd ::= SAVEPOINT nm",
165259 /* 11 */ "cmd ::= RELEASE savepoint_opt nm",
165260 /* 12 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm",
165261 /* 13 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm",
165262 /* 14 */ "createkw ::= CREATE",
165263 /* 15 */ "ifnotexists ::=",
165264 /* 16 */ "ifnotexists ::= IF NOT EXISTS",
165265 /* 17 */ "temp ::= TEMP",
165266 /* 18 */ "temp ::=",
165267 /* 19 */ "create_table_args ::= LP columnlist conslist_opt RP table_option_set",
165268 /* 20 */ "create_table_args ::= AS select",
165269 /* 21 */ "table_option_set ::=",
165270 /* 22 */ "table_option_set ::= table_option_set COMMA table_option",
165271 /* 23 */ "table_option ::= WITHOUT nm",
165272 /* 24 */ "table_option ::= nm",
165273 /* 25 */ "columnname ::= nm typetoken",
165274 /* 26 */ "typetoken ::=",
165275 /* 27 */ "typetoken ::= typename LP signed RP",
165276 /* 28 */ "typetoken ::= typename LP signed COMMA signed RP",
165277 /* 29 */ "typename ::= typename ID|STRING",
165278 /* 30 */ "scanpt ::=",
165279 /* 31 */ "scantok ::=",
165280 /* 32 */ "ccons ::= CONSTRAINT nm",
165281 /* 33 */ "ccons ::= DEFAULT scantok term",
165282 /* 34 */ "ccons ::= DEFAULT LP expr RP",
165283 /* 35 */ "ccons ::= DEFAULT PLUS scantok term",
165284 /* 36 */ "ccons ::= DEFAULT MINUS scantok term",
165285 /* 37 */ "ccons ::= DEFAULT scantok ID|INDEXED",
165286 /* 38 */ "ccons ::= NOT NULL onconf",
165287 /* 39 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
165288 /* 40 */ "ccons ::= UNIQUE onconf",
165289 /* 41 */ "ccons ::= CHECK LP expr RP",
165290 /* 42 */ "ccons ::= REFERENCES nm eidlist_opt refargs",
165291 /* 43 */ "ccons ::= defer_subclause",
165292 /* 44 */ "ccons ::= COLLATE ID|STRING",
165293 /* 45 */ "generated ::= LP expr RP",
165294 /* 46 */ "generated ::= LP expr RP ID",
165295 /* 47 */ "autoinc ::=",
165296 /* 48 */ "autoinc ::= AUTOINCR",
165297 /* 49 */ "refargs ::=",
165298 /* 50 */ "refargs ::= refargs refarg",
165299 /* 51 */ "refarg ::= MATCH nm",
165300 /* 52 */ "refarg ::= ON INSERT refact",
165301 /* 53 */ "refarg ::= ON DELETE refact",
165302 /* 54 */ "refarg ::= ON UPDATE refact",
165303 /* 55 */ "refact ::= SET NULL",
165304 /* 56 */ "refact ::= SET DEFAULT",
165305 /* 57 */ "refact ::= CASCADE",
165306 /* 58 */ "refact ::= RESTRICT",
165307 /* 59 */ "refact ::= NO ACTION",
165308 /* 60 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
165309 /* 61 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
165310 /* 62 */ "init_deferred_pred_opt ::=",
165311 /* 63 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
165312 /* 64 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
165313 /* 65 */ "conslist_opt ::=",
165314 /* 66 */ "tconscomma ::= COMMA",
165315 /* 67 */ "tcons ::= CONSTRAINT nm",
165316 /* 68 */ "tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf",
165317 /* 69 */ "tcons ::= UNIQUE LP sortlist RP onconf",
165318 /* 70 */ "tcons ::= CHECK LP expr RP onconf",
165319 /* 71 */ "tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt",
165320 /* 72 */ "defer_subclause_opt ::=",
165321 /* 73 */ "onconf ::=",
165322 /* 74 */ "onconf ::= ON CONFLICT resolvetype",
165323 /* 75 */ "orconf ::=",
165324 /* 76 */ "orconf ::= OR resolvetype",
165325 /* 77 */ "resolvetype ::= IGNORE",
165326 /* 78 */ "resolvetype ::= REPLACE",
165327 /* 79 */ "cmd ::= DROP TABLE ifexists fullname",
165328 /* 80 */ "ifexists ::= IF EXISTS",
165329 /* 81 */ "ifexists ::=",
165330 /* 82 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select",
165331 /* 83 */ "cmd ::= DROP VIEW ifexists fullname",
165332 /* 84 */ "cmd ::= select",
165333 /* 85 */ "select ::= WITH wqlist selectnowith",
165334 /* 86 */ "select ::= WITH RECURSIVE wqlist selectnowith",
165335 /* 87 */ "select ::= selectnowith",
165336 /* 88 */ "selectnowith ::= selectnowith multiselect_op oneselect",
165337 /* 89 */ "multiselect_op ::= UNION",
165338 /* 90 */ "multiselect_op ::= UNION ALL",
165339 /* 91 */ "multiselect_op ::= EXCEPT|INTERSECT",
165340 /* 92 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
165341 /* 93 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt",
165342 /* 94 */ "values ::= VALUES LP nexprlist RP",
165343 /* 95 */ "values ::= values COMMA LP nexprlist RP",
165344 /* 96 */ "distinct ::= DISTINCT",
165345 /* 97 */ "distinct ::= ALL",
165346 /* 98 */ "distinct ::=",
165347 /* 99 */ "sclp ::=",
165348 /* 100 */ "selcollist ::= sclp scanpt expr scanpt as",
165349 /* 101 */ "selcollist ::= sclp scanpt STAR",
165350 /* 102 */ "selcollist ::= sclp scanpt nm DOT STAR",
165351 /* 103 */ "as ::= AS nm",
165352 /* 104 */ "as ::=",
165353 /* 105 */ "from ::=",
165354 /* 106 */ "from ::= FROM seltablist",
165355 /* 107 */ "stl_prefix ::= seltablist joinop",
165356 /* 108 */ "stl_prefix ::=",
165357 /* 109 */ "seltablist ::= stl_prefix nm dbnm as on_using",
165358 /* 110 */ "seltablist ::= stl_prefix nm dbnm as indexed_by on_using",
165359 /* 111 */ "seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_using",
165360 /* 112 */ "seltablist ::= stl_prefix LP select RP as on_using",
165361 /* 113 */ "seltablist ::= stl_prefix LP seltablist RP as on_using",
165362 /* 114 */ "dbnm ::=",
165363 /* 115 */ "dbnm ::= DOT nm",
165364 /* 116 */ "fullname ::= nm",
165365 /* 117 */ "fullname ::= nm DOT nm",
165366 /* 118 */ "xfullname ::= nm",
165367 /* 119 */ "xfullname ::= nm DOT nm",
165368 /* 120 */ "xfullname ::= nm DOT nm AS nm",
165369 /* 121 */ "xfullname ::= nm AS nm",
165370 /* 122 */ "joinop ::= COMMA|JOIN",
165371 /* 123 */ "joinop ::= JOIN_KW JOIN",
165372 /* 124 */ "joinop ::= JOIN_KW nm JOIN",
165373 /* 125 */ "joinop ::= JOIN_KW nm nm JOIN",
165374 /* 126 */ "on_using ::= ON expr",
165375 /* 127 */ "on_using ::= USING LP idlist RP",
165376 /* 128 */ "on_using ::=",
165377 /* 129 */ "indexed_opt ::=",
165378 /* 130 */ "indexed_by ::= INDEXED BY nm",
165379 /* 131 */ "indexed_by ::= NOT INDEXED",
165380 /* 132 */ "orderby_opt ::=",
165381 /* 133 */ "orderby_opt ::= ORDER BY sortlist",
165382 /* 134 */ "sortlist ::= sortlist COMMA expr sortorder nulls",
165383 /* 135 */ "sortlist ::= expr sortorder nulls",
165384 /* 136 */ "sortorder ::= ASC",
165385 /* 137 */ "sortorder ::= DESC",
165386 /* 138 */ "sortorder ::=",
165387 /* 139 */ "nulls ::= NULLS FIRST",
165388 /* 140 */ "nulls ::= NULLS LAST",
165389 /* 141 */ "nulls ::=",
165390 /* 142 */ "groupby_opt ::=",
165391 /* 143 */ "groupby_opt ::= GROUP BY nexprlist",
165392 /* 144 */ "having_opt ::=",
165393 /* 145 */ "having_opt ::= HAVING expr",
165394 /* 146 */ "limit_opt ::=",
165395 /* 147 */ "limit_opt ::= LIMIT expr",
165396 /* 148 */ "limit_opt ::= LIMIT expr OFFSET expr",
165397 /* 149 */ "limit_opt ::= LIMIT expr COMMA expr",
165398 /* 150 */ "cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret",
165399 /* 151 */ "where_opt ::=",
165400 /* 152 */ "where_opt ::= WHERE expr",
165401 /* 153 */ "where_opt_ret ::=",
165402 /* 154 */ "where_opt_ret ::= WHERE expr",
165403 /* 155 */ "where_opt_ret ::= RETURNING selcollist",
165404 /* 156 */ "where_opt_ret ::= WHERE expr RETURNING selcollist",
165405 /* 157 */ "cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret",
165406 /* 158 */ "setlist ::= setlist COMMA nm EQ expr",
165407 /* 159 */ "setlist ::= setlist COMMA LP idlist RP EQ expr",
165408 /* 160 */ "setlist ::= nm EQ expr",
165409 /* 161 */ "setlist ::= LP idlist RP EQ expr",
165410 /* 162 */ "cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert",
165411 /* 163 */ "cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning",
165412 /* 164 */ "upsert ::=",
165413 /* 165 */ "upsert ::= RETURNING selcollist",
165414 /* 166 */ "upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt upsert",
165415 /* 167 */ "upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert",
165416 /* 168 */ "upsert ::= ON CONFLICT DO NOTHING returning",
165417 /* 169 */ "upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning",
165418 /* 170 */ "returning ::= RETURNING selcollist",
165419 /* 171 */ "insert_cmd ::= INSERT orconf",
165420 /* 172 */ "insert_cmd ::= REPLACE",
165421 /* 173 */ "idlist_opt ::=",
165422 /* 174 */ "idlist_opt ::= LP idlist RP",
165423 /* 175 */ "idlist ::= idlist COMMA nm",
165424 /* 176 */ "idlist ::= nm",
165425 /* 177 */ "expr ::= LP expr RP",
165426 /* 178 */ "expr ::= ID|INDEXED",
165427 /* 179 */ "expr ::= JOIN_KW",
165428 /* 180 */ "expr ::= nm DOT nm",
165429 /* 181 */ "expr ::= nm DOT nm DOT nm",
165430 /* 182 */ "term ::= NULL|FLOAT|BLOB",
165431 /* 183 */ "term ::= STRING",
165432 /* 184 */ "term ::= INTEGER",
165433 /* 185 */ "expr ::= VARIABLE",
165434 /* 186 */ "expr ::= expr COLLATE ID|STRING",
165435 /* 187 */ "expr ::= CAST LP expr AS typetoken RP",
165436 /* 188 */ "expr ::= ID|INDEXED LP distinct exprlist RP",
165437 /* 189 */ "expr ::= ID|INDEXED LP STAR RP",
165438 /* 190 */ "expr ::= ID|INDEXED LP distinct exprlist RP filter_over",
165439 /* 191 */ "expr ::= ID|INDEXED LP STAR RP filter_over",
165440 /* 192 */ "term ::= CTIME_KW",
165441 /* 193 */ "expr ::= LP nexprlist COMMA expr RP",
165442 /* 194 */ "expr ::= expr AND expr",
165443 /* 195 */ "expr ::= expr OR expr",
165444 /* 196 */ "expr ::= expr LT|GT|GE|LE expr",
165445 /* 197 */ "expr ::= expr EQ|NE expr",
165446 /* 198 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
165447 /* 199 */ "expr ::= expr PLUS|MINUS expr",
165448 /* 200 */ "expr ::= expr STAR|SLASH|REM expr",
165449 /* 201 */ "expr ::= expr CONCAT expr",
165450 /* 202 */ "likeop ::= NOT LIKE_KW|MATCH",
165451 /* 203 */ "expr ::= expr likeop expr",
165452 /* 204 */ "expr ::= expr likeop expr ESCAPE expr",
165453 /* 205 */ "expr ::= expr ISNULL|NOTNULL",
165454 /* 206 */ "expr ::= expr NOT NULL",
165455 /* 207 */ "expr ::= expr IS expr",
165456 /* 208 */ "expr ::= expr IS NOT expr",
165457 /* 209 */ "expr ::= expr IS NOT DISTINCT FROM expr",
165458 /* 210 */ "expr ::= expr IS DISTINCT FROM expr",
165459 /* 211 */ "expr ::= NOT expr",
165460 /* 212 */ "expr ::= BITNOT expr",
165461 /* 213 */ "expr ::= PLUS|MINUS expr",
165462 /* 214 */ "expr ::= expr PTR expr",
165463 /* 215 */ "between_op ::= BETWEEN",
165464 /* 216 */ "between_op ::= NOT BETWEEN",
165465 /* 217 */ "expr ::= expr between_op expr AND expr",
165466 /* 218 */ "in_op ::= IN",
165467 /* 219 */ "in_op ::= NOT IN",
165468 /* 220 */ "expr ::= expr in_op LP exprlist RP",
165469 /* 221 */ "expr ::= LP select RP",
165470 /* 222 */ "expr ::= expr in_op LP select RP",
165471 /* 223 */ "expr ::= expr in_op nm dbnm paren_exprlist",
165472 /* 224 */ "expr ::= EXISTS LP select RP",
165473 /* 225 */ "expr ::= CASE case_operand case_exprlist case_else END",
165474 /* 226 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
165475 /* 227 */ "case_exprlist ::= WHEN expr THEN expr",
165476 /* 228 */ "case_else ::= ELSE expr",
165477 /* 229 */ "case_else ::=",
165478 /* 230 */ "case_operand ::= expr",
165479 /* 231 */ "case_operand ::=",
165480 /* 232 */ "exprlist ::=",
165481 /* 233 */ "nexprlist ::= nexprlist COMMA expr",
165482 /* 234 */ "nexprlist ::= expr",
165483 /* 235 */ "paren_exprlist ::=",
165484 /* 236 */ "paren_exprlist ::= LP exprlist RP",
165485 /* 237 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt",
165486 /* 238 */ "uniqueflag ::= UNIQUE",
165487 /* 239 */ "uniqueflag ::=",
165488 /* 240 */ "eidlist_opt ::=",
165489 /* 241 */ "eidlist_opt ::= LP eidlist RP",
165490 /* 242 */ "eidlist ::= eidlist COMMA nm collate sortorder",
165491 /* 243 */ "eidlist ::= nm collate sortorder",
165492 /* 244 */ "collate ::=",
165493 /* 245 */ "collate ::= COLLATE ID|STRING",
165494 /* 246 */ "cmd ::= DROP INDEX ifexists fullname",
165495 /* 247 */ "cmd ::= VACUUM vinto",
165496 /* 248 */ "cmd ::= VACUUM nm vinto",
165497 /* 249 */ "vinto ::= INTO expr",
165498 /* 250 */ "vinto ::=",
165499 /* 251 */ "cmd ::= PRAGMA nm dbnm",
165500 /* 252 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
165501 /* 253 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
165502 /* 254 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
165503 /* 255 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
165504 /* 256 */ "plus_num ::= PLUS INTEGER|FLOAT",
165505 /* 257 */ "minus_num ::= MINUS INTEGER|FLOAT",
165506 /* 258 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
165507 /* 259 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
165508 /* 260 */ "trigger_time ::= BEFORE|AFTER",
165509 /* 261 */ "trigger_time ::= INSTEAD OF",
165510 /* 262 */ "trigger_time ::=",
165511 /* 263 */ "trigger_event ::= DELETE|INSERT",
165512 /* 264 */ "trigger_event ::= UPDATE",
165513 /* 265 */ "trigger_event ::= UPDATE OF idlist",
165514 /* 266 */ "when_clause ::=",
165515 /* 267 */ "when_clause ::= WHEN expr",
165516 /* 268 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
165517 /* 269 */ "trigger_cmd_list ::= trigger_cmd SEMI",
165518 /* 270 */ "trnm ::= nm DOT nm",
165519 /* 271 */ "tridxby ::= INDEXED BY nm",
165520 /* 272 */ "tridxby ::= NOT INDEXED",
165521 /* 273 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt",
165522 /* 274 */ "trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt",
165523 /* 275 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt",
165524 /* 276 */ "trigger_cmd ::= scanpt select scanpt",
165525 /* 277 */ "expr ::= RAISE LP IGNORE RP",
165526 /* 278 */ "expr ::= RAISE LP raisetype COMMA nm RP",
165527 /* 279 */ "raisetype ::= ROLLBACK",
165528 /* 280 */ "raisetype ::= ABORT",
165529 /* 281 */ "raisetype ::= FAIL",
165530 /* 282 */ "cmd ::= DROP TRIGGER ifexists fullname",
165531 /* 283 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
165532 /* 284 */ "cmd ::= DETACH database_kw_opt expr",
165533 /* 285 */ "key_opt ::=",
165534 /* 286 */ "key_opt ::= KEY expr",
165535 /* 287 */ "cmd ::= REINDEX",
165536 /* 288 */ "cmd ::= REINDEX nm dbnm",
165537 /* 289 */ "cmd ::= ANALYZE",
165538 /* 290 */ "cmd ::= ANALYZE nm dbnm",
165539 /* 291 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
165540 /* 292 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist",
165541 /* 293 */ "cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm",
165542 /* 294 */ "add_column_fullname ::= fullname",
165543 /* 295 */ "cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm",
165544 /* 296 */ "cmd ::= create_vtab",
165545 /* 297 */ "cmd ::= create_vtab LP vtabarglist RP",
165546 /* 298 */ "create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm",
165547 /* 299 */ "vtabarg ::=",
165548 /* 300 */ "vtabargtoken ::= ANY",
165549 /* 301 */ "vtabargtoken ::= lp anylist RP",
165550 /* 302 */ "lp ::= LP",
165551 /* 303 */ "with ::= WITH wqlist",
165552 /* 304 */ "with ::= WITH RECURSIVE wqlist",
165553 /* 305 */ "wqas ::= AS",
165554 /* 306 */ "wqas ::= AS MATERIALIZED",
165555 /* 307 */ "wqas ::= AS NOT MATERIALIZED",
165556 /* 308 */ "wqitem ::= nm eidlist_opt wqas LP select RP",
165557 /* 309 */ "wqlist ::= wqitem",
165558 /* 310 */ "wqlist ::= wqlist COMMA wqitem",
165559 /* 311 */ "windowdefn_list ::= windowdefn",
165560 /* 312 */ "windowdefn_list ::= windowdefn_list COMMA windowdefn",
165561 /* 313 */ "windowdefn ::= nm AS LP window RP",
165562 /* 314 */ "window ::= PARTITION BY nexprlist orderby_opt frame_opt",
165563 /* 315 */ "window ::= nm PARTITION BY nexprlist orderby_opt frame_opt",
165564 /* 316 */ "window ::= ORDER BY sortlist frame_opt",
165565 /* 317 */ "window ::= nm ORDER BY sortlist frame_opt",
165566 /* 318 */ "window ::= frame_opt",
165567 /* 319 */ "window ::= nm frame_opt",
165568 /* 320 */ "frame_opt ::=",
165569 /* 321 */ "frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt",
165570 /* 322 */ "frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt",
165571 /* 323 */ "range_or_rows ::= RANGE|ROWS|GROUPS",
165572 /* 324 */ "frame_bound_s ::= frame_bound",
165573 /* 325 */ "frame_bound_s ::= UNBOUNDED PRECEDING",
165574 /* 326 */ "frame_bound_e ::= frame_bound",
165575 /* 327 */ "frame_bound_e ::= UNBOUNDED FOLLOWING",
165576 /* 328 */ "frame_bound ::= expr PRECEDING|FOLLOWING",
165577 /* 329 */ "frame_bound ::= CURRENT ROW",
165578 /* 330 */ "frame_exclude_opt ::=",
165579 /* 331 */ "frame_exclude_opt ::= EXCLUDE frame_exclude",
165580 /* 332 */ "frame_exclude ::= NO OTHERS",
165581 /* 333 */ "frame_exclude ::= CURRENT ROW",
165582 /* 334 */ "frame_exclude ::= GROUP|TIES",
165583 /* 335 */ "window_clause ::= WINDOW windowdefn_list",
165584 /* 336 */ "filter_over ::= filter_clause over_clause",
165585 /* 337 */ "filter_over ::= over_clause",
165586 /* 338 */ "filter_over ::= filter_clause",
165587 /* 339 */ "over_clause ::= OVER LP window RP",
165588 /* 340 */ "over_clause ::= OVER nm",
165589 /* 341 */ "filter_clause ::= FILTER LP WHERE expr RP",
165590 /* 342 */ "input ::= cmdlist",
165591 /* 343 */ "cmdlist ::= cmdlist ecmd",
165592 /* 344 */ "cmdlist ::= ecmd",
165593 /* 345 */ "ecmd ::= SEMI",
165594 /* 346 */ "ecmd ::= cmdx SEMI",
165595 /* 347 */ "ecmd ::= explain cmdx SEMI",
165596 /* 348 */ "trans_opt ::=",
165597 /* 349 */ "trans_opt ::= TRANSACTION",
165598 /* 350 */ "trans_opt ::= TRANSACTION nm",
165599 /* 351 */ "savepoint_opt ::= SAVEPOINT",
165600 /* 352 */ "savepoint_opt ::=",
165601 /* 353 */ "cmd ::= create_table create_table_args",
165602 /* 354 */ "table_option_set ::= table_option",
165603 /* 355 */ "columnlist ::= columnlist COMMA columnname carglist",
165604 /* 356 */ "columnlist ::= columnname carglist",
165605 /* 357 */ "nm ::= ID|INDEXED",
165606 /* 358 */ "nm ::= STRING",
165607 /* 359 */ "nm ::= JOIN_KW",
165608 /* 360 */ "typetoken ::= typename",
165609 /* 361 */ "typename ::= ID|STRING",
165610 /* 362 */ "signed ::= plus_num",
165611 /* 363 */ "signed ::= minus_num",
165612 /* 364 */ "carglist ::= carglist ccons",
165613 /* 365 */ "carglist ::=",
165614 /* 366 */ "ccons ::= NULL onconf",
165615 /* 367 */ "ccons ::= GENERATED ALWAYS AS generated",
165616 /* 368 */ "ccons ::= AS generated",
165617 /* 369 */ "conslist_opt ::= COMMA conslist",
165618 /* 370 */ "conslist ::= conslist tconscomma tcons",
165619 /* 371 */ "conslist ::= tcons",
165620 /* 372 */ "tconscomma ::=",
165621 /* 373 */ "defer_subclause_opt ::= defer_subclause",
165622 /* 374 */ "resolvetype ::= raisetype",
165623 /* 375 */ "selectnowith ::= oneselect",
165624 /* 376 */ "oneselect ::= values",
165625 /* 377 */ "sclp ::= selcollist COMMA",
165626 /* 378 */ "as ::= ID|STRING",
165627 /* 379 */ "indexed_opt ::= indexed_by",
165628 /* 380 */ "returning ::=",
165629 /* 381 */ "expr ::= term",
165630 /* 382 */ "likeop ::= LIKE_KW|MATCH",
165631 /* 383 */ "exprlist ::= nexprlist",
165632 /* 384 */ "nmnum ::= plus_num",
165633 /* 385 */ "nmnum ::= nm",
165634 /* 386 */ "nmnum ::= ON",
165635 /* 387 */ "nmnum ::= DELETE",
165636 /* 388 */ "nmnum ::= DEFAULT",
165637 /* 389 */ "plus_num ::= INTEGER|FLOAT",
165638 /* 390 */ "foreach_clause ::=",
165639 /* 391 */ "foreach_clause ::= FOR EACH ROW",
165640 /* 392 */ "trnm ::= nm",
165641 /* 393 */ "tridxby ::=",
165642 /* 394 */ "database_kw_opt ::= DATABASE",
165643 /* 395 */ "database_kw_opt ::=",
165644 /* 396 */ "kwcolumn_opt ::=",
165645 /* 397 */ "kwcolumn_opt ::= COLUMNKW",
165646 /* 398 */ "vtabarglist ::= vtabarg",
165647 /* 399 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
165648 /* 400 */ "vtabarg ::= vtabarg vtabargtoken",
165649 /* 401 */ "anylist ::=",
165650 /* 402 */ "anylist ::= anylist LP anylist RP",
165651 /* 403 */ "anylist ::= anylist ANY",
165652 /* 404 */ "with ::=",
165653};
165654#endif /* NDEBUG */
165655
165656
165657#if YYSTACKDEPTH100<=0
165658/*
165659** Try to increase the size of the parser stack. Return the number
165660** of errors. Return 0 on success.
165661*/
165662static int yyGrowStack(yyParser *p){
165663 int newSize;
165664 int idx;
165665 yyStackEntry *pNew;
165666
165667 newSize = p->yystksz*2 + 100;
165668 idx = p->yytos ? (int)(p->yytos - p->yystack) : 0;
165669 if( p->yystack==&p->yystk0 ){
165670 pNew = malloc(newSize*sizeof(pNew[0]));
165671 if( pNew ) pNew[0] = p->yystk0;
165672 }else{
165673 pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
165674 }
165675 if( pNew ){
165676 p->yystack = pNew;
165677 p->yytos = &p->yystack[idx];
165678#ifndef NDEBUG1
165679 if( yyTraceFILE ){
165680 fprintf(yyTraceFILE,"%sStack grows from %d to %d entries.\n",
165681 yyTracePrompt, p->yystksz, newSize);
165682 }
165683#endif
165684 p->yystksz = newSize;
165685 }
165686 return pNew==0;
165687}
165688#endif
165689
165690/* Datatype of the argument to the memory allocated passed as the
165691** second argument to sqlite3ParserAlloc() below. This can be changed by
165692** putting an appropriate #define in the %include section of the input
165693** grammar.
165694*/
165695#ifndef YYMALLOCARGTYPEu64
165696# define YYMALLOCARGTYPEu64 size_t
165697#endif
165698
165699/* Initialize a new parser that has already been allocated.
165700*/
165701SQLITE_PRIVATEstatic void sqlite3ParserInit(void *yypRawParser sqlite3ParserCTX_PDECL,Parse *pParse){
165702 yyParser *yypParser = (yyParser*)yypRawParser;
165703 sqlite3ParserCTX_STOREyypParser->pParse=pParse;
165704#ifdef YYTRACKMAXSTACKDEPTH
165705 yypParser->yyhwm = 0;
165706#endif
165707#if YYSTACKDEPTH100<=0
165708 yypParser->yytos = NULL((void*)0);
165709 yypParser->yystack = NULL((void*)0);
165710 yypParser->yystksz = 0;
165711 if( yyGrowStack(yypParser) ){
165712 yypParser->yystack = &yypParser->yystk0;
165713 yypParser->yystksz = 1;
165714 }
165715#endif
165716#ifndef YYNOERRORRECOVERY1
165717 yypParser->yyerrcnt = -1;
165718#endif
165719 yypParser->yytos = yypParser->yystack;
165720 yypParser->yystack[0].stateno = 0;
165721 yypParser->yystack[0].major = 0;
165722#if YYSTACKDEPTH100>0
165723 yypParser->yystackEnd = &yypParser->yystack[YYSTACKDEPTH100-1];
165724#endif
165725}
165726
165727#ifndef sqlite3Parser_ENGINEALWAYSONSTACK1
165728/*
165729** This function allocates a new parser.
165730** The only argument is a pointer to a function which works like
165731** malloc.
165732**
165733** Inputs:
165734** A pointer to the function used to allocate memory.
165735**
165736** Outputs:
165737** A pointer to a parser. This pointer is used in subsequent calls
165738** to sqlite3Parser and sqlite3ParserFree.
165739*/
165740SQLITE_PRIVATEstatic void *sqlite3ParserAlloc(void *(*mallocProc)(YYMALLOCARGTYPEu64) sqlite3ParserCTX_PDECL,Parse *pParse){
165741 yyParser *yypParser;
165742 yypParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPEu64)sizeof(yyParser) );
165743 if( yypParser ){
165744 sqlite3ParserCTX_STOREyypParser->pParse=pParse;
165745 sqlite3ParserInit(yypParser sqlite3ParserCTX_PARAM,pParse);
165746 }
165747 return (void*)yypParser;
165748}
165749#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
165750
165751
165752/* The following function deletes the "minor type" or semantic value
165753** associated with a symbol. The symbol can be either a terminal
165754** or nonterminal. "yymajor" is the symbol code, and "yypminor" is
165755** a pointer to the value to be deleted. The code used to do the
165756** deletions is derived from the %destructor and/or %token_destructor
165757** directives of the input grammar.
165758*/
165759static void yy_destructor(
165760 yyParser *yypParser, /* The parser */
165761 YYCODETYPEunsigned short int yymajor, /* Type code for object to destroy */
165762 YYMINORTYPE *yypminor /* The object to be destroyed */
165763){
165764 sqlite3ParserARG_FETCH
165765 sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse;
165766 switch( yymajor ){
165767 /* Here is inserted the actions which take place when a
165768 ** terminal or non-terminal is destroyed. This can happen
165769 ** when the symbol is popped from the stack during a
165770 ** reduce or during error processing or when a parser is
165771 ** being destroyed before it is finished parsing.
165772 **
165773 ** Note: during a reduce, the only symbols destroyed are those
165774 ** which appear on the RHS of the rule, but which are *not* used
165775 ** inside the C code.
165776 */
165777/********* Begin destructor definitions ***************************************/
165778 case 204: /* select */
165779 case 239: /* selectnowith */
165780 case 240: /* oneselect */
165781 case 252: /* values */
165782{
165783sqlite3SelectDelete(pParse->db, (yypminor->yy47));
165784}
165785 break;
165786 case 216: /* term */
165787 case 217: /* expr */
165788 case 246: /* where_opt */
165789 case 248: /* having_opt */
165790 case 267: /* where_opt_ret */
165791 case 278: /* case_operand */
165792 case 280: /* case_else */
165793 case 283: /* vinto */
165794 case 290: /* when_clause */
165795 case 295: /* key_opt */
165796 case 311: /* filter_clause */
165797{
165798sqlite3ExprDelete(pParse->db, (yypminor->yy528));
165799}
165800 break;
165801 case 221: /* eidlist_opt */
165802 case 231: /* sortlist */
165803 case 232: /* eidlist */
165804 case 244: /* selcollist */
165805 case 247: /* groupby_opt */
165806 case 249: /* orderby_opt */
165807 case 253: /* nexprlist */
165808 case 254: /* sclp */
165809 case 261: /* exprlist */
165810 case 268: /* setlist */
165811 case 277: /* paren_exprlist */
165812 case 279: /* case_exprlist */
165813 case 310: /* part_opt */
165814{
165815sqlite3ExprListDelete(pParse->db, (yypminor->yy322));
165816}
165817 break;
165818 case 238: /* fullname */
165819 case 245: /* from */
165820 case 256: /* seltablist */
165821 case 257: /* stl_prefix */
165822 case 262: /* xfullname */
165823{
165824sqlite3SrcListDelete(pParse->db, (yypminor->yy131));
165825}
165826 break;
165827 case 241: /* wqlist */
165828{
165829sqlite3WithDelete(pParse->db, (yypminor->yy521));
165830}
165831 break;
165832 case 251: /* window_clause */
165833 case 306: /* windowdefn_list */
165834{
165835sqlite3WindowListDelete(pParse->db, (yypminor->yy41));
165836}
165837 break;
165838 case 263: /* idlist */
165839 case 270: /* idlist_opt */
165840{
165841sqlite3IdListDelete(pParse->db, (yypminor->yy254));
165842}
165843 break;
165844 case 273: /* filter_over */
165845 case 307: /* windowdefn */
165846 case 308: /* window */
165847 case 309: /* frame_opt */
165848 case 312: /* over_clause */
165849{
165850sqlite3WindowDelete(pParse->db, (yypminor->yy41));
165851}
165852 break;
165853 case 286: /* trigger_cmd_list */
165854 case 291: /* trigger_cmd */
165855{
165856sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy33));
165857}
165858 break;
165859 case 288: /* trigger_event */
165860{
165861sqlite3IdListDelete(pParse->db, (yypminor->yy180).b);
165862}
165863 break;
165864 case 314: /* frame_bound */
165865 case 315: /* frame_bound_s */
165866 case 316: /* frame_bound_e */
165867{
165868sqlite3ExprDelete(pParse->db, (yypminor->yy595).pExpr);
165869}
165870 break;
165871/********* End destructor definitions *****************************************/
165872 default: break; /* If no destructor action specified: do nothing */
165873 }
165874}
165875
165876/*
165877** Pop the parser's stack once.
165878**
165879** If there is a destructor routine associated with the token which
165880** is popped from the stack, then call it.
165881*/
165882static void yy_pop_parser_stack(yyParser *pParser){
165883 yyStackEntry *yytos;
165884 assert( pParser->yytos!=0 )((void) (0));
165885 assert( pParser->yytos > pParser->yystack )((void) (0));
165886 yytos = pParser->yytos--;
165887#ifndef NDEBUG1
165888 if( yyTraceFILE ){
165889 fprintf(yyTraceFILE,"%sPopping %s\n",
165890 yyTracePrompt,
165891 yyTokenName[yytos->major]);
165892 }
165893#endif
165894 yy_destructor(pParser, yytos->major, &yytos->minor);
165895}
165896
165897/*
165898** Clear all secondary memory allocations from the parser
165899*/
165900SQLITE_PRIVATEstatic void sqlite3ParserFinalize(void *p){
165901 yyParser *pParser = (yyParser*)p;
165902 while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
165903#if YYSTACKDEPTH100<=0
165904 if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
165905#endif
165906}
165907
165908#ifndef sqlite3Parser_ENGINEALWAYSONSTACK1
165909/*
165910** Deallocate and destroy a parser. Destructors are called for
165911** all stack elements before shutting the parser down.
165912**
165913** If the YYPARSEFREENEVERNULL macro exists (for example because it
165914** is defined in a %include section of the input grammar) then it is
165915** assumed that the input pointer is never NULL.
165916*/
165917SQLITE_PRIVATEstatic void sqlite3ParserFree(
165918 void *p, /* The parser to be deleted */
165919 void (*freeProc)(void*) /* Function used to reclaim memory */
165920){
165921#ifndef YYPARSEFREENEVERNULL1
165922 if( p==0 ) return;
165923#endif
165924 sqlite3ParserFinalize(p);
165925 (*freeProc)(p);
165926}
165927#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
165928
165929/*
165930** Return the peak depth of the stack for a parser.
165931*/
165932#ifdef YYTRACKMAXSTACKDEPTH
165933SQLITE_PRIVATEstatic int sqlite3ParserStackPeak(void *p){
165934 yyParser *pParser = (yyParser*)p;
165935 return pParser->yyhwm;
165936}
165937#endif
165938
165939/* This array of booleans keeps track of the parser statement
165940** coverage. The element yycoverage[X][Y] is set when the parser
165941** is in state X and has a lookahead token Y. In a well-tested
165942** systems, every element of this matrix should end up being set.
165943*/
165944#if defined(YYCOVERAGE)
165945static unsigned char yycoverage[YYNSTATE576][YYNTOKEN185];
165946#endif
165947
165948/*
165949** Write into out a description of every state/lookahead combination that
165950**
165951** (1) has not been used by the parser, and
165952** (2) is not a syntax error.
165953**
165954** Return the number of missed state/lookahead combinations.
165955*/
165956#if defined(YYCOVERAGE)
165957SQLITE_PRIVATEstatic int sqlite3ParserCoverage(FILE *out){
165958 int stateno, iLookAhead, i;
165959 int nMissed = 0;
165960 for(stateno=0; stateno<YYNSTATE576; stateno++){
165961 i = yy_shift_ofst[stateno];
165962 for(iLookAhead=0; iLookAhead<YYNTOKEN185; iLookAhead++){
165963 if( yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
165964 if( yycoverage[stateno][iLookAhead]==0 ) nMissed++;
165965 if( out ){
165966 fprintf(out,"State %d lookahead %s %s\n", stateno,
165967 yyTokenName[iLookAhead],
165968 yycoverage[stateno][iLookAhead] ? "ok" : "missed");
165969 }
165970 }
165971 }
165972 return nMissed;
165973}
165974#endif
165975
165976/*
165977** Find the appropriate action for a parser given the terminal
165978** look-ahead token iLookAhead.
165979*/
165980static YYACTIONTYPEunsigned short int yy_find_shift_action(
165981 YYCODETYPEunsigned short int iLookAhead, /* The look-ahead token */
165982 YYACTIONTYPEunsigned short int stateno /* Current state number */
165983){
165984 int i;
165985
165986 if( stateno>YY_MAX_SHIFT575 ) return stateno;
165987 assert( stateno <= YY_SHIFT_COUNT )((void) (0));
165988#if defined(YYCOVERAGE)
165989 yycoverage[stateno][iLookAhead] = 1;
165990#endif
165991 do{
165992 i = yy_shift_ofst[stateno];
165993 assert( i>=0 )((void) (0));
165994 assert( i<=YY_ACTTAB_COUNT )((void) (0));
165995 assert( i+YYNTOKEN<=(int)YY_NLOOKAHEAD )((void) (0));
165996 assert( iLookAhead!=YYNOCODE )((void) (0));
165997 assert( iLookAhead < YYNTOKEN )((void) (0));
165998 i += iLookAhead;
165999 assert( i<(int)YY_NLOOKAHEAD )((void) (0));
166000 if( yy_lookahead[i]!=iLookAhead ){
166001#ifdef YYFALLBACK1
166002 YYCODETYPEunsigned short int iFallback; /* Fallback token */
166003 assert( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0]) )((void) (0));
166004 iFallback = yyFallback[iLookAhead];
166005 if( iFallback!=0 ){
166006#ifndef NDEBUG1
166007 if( yyTraceFILE ){
166008 fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
166009 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
166010 }
166011#endif
166012 assert( yyFallback[iFallback]==0 )((void) (0)); /* Fallback loop must terminate */
166013 iLookAhead = iFallback;
166014 continue;
166015 }
166016#endif
166017#ifdef YYWILDCARD101
166018 {
166019 int j = i - iLookAhead + YYWILDCARD101;
166020 assert( j<(int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])) )((void) (0));
166021 if( yy_lookahead[j]==YYWILDCARD101 && iLookAhead>0 ){
166022#ifndef NDEBUG1
166023 if( yyTraceFILE ){
166024 fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
166025 yyTracePrompt, yyTokenName[iLookAhead],
166026 yyTokenName[YYWILDCARD101]);
166027 }
166028#endif /* NDEBUG */
166029 return yy_action[j];
166030 }
166031 }
166032#endif /* YYWILDCARD */
166033 return yy_default[stateno];
166034 }else{
166035 assert( i>=0 && i<(int)(sizeof(yy_action)/sizeof(yy_action[0])) )((void) (0));
166036 return yy_action[i];
166037 }
166038 }while(1);
166039}
166040
166041/*
166042** Find the appropriate action for a parser given the non-terminal
166043** look-ahead token iLookAhead.
166044*/
166045static YYACTIONTYPEunsigned short int yy_find_reduce_action(
166046 YYACTIONTYPEunsigned short int stateno, /* Current state number */
166047 YYCODETYPEunsigned short int iLookAhead /* The look-ahead token */
166048){
166049 int i;
166050#ifdef YYERRORSYMBOL
166051 if( stateno>YY_REDUCE_COUNT(408) ){
166052 return yy_default[stateno];
166053 }
166054#else
166055 assert( stateno<=YY_REDUCE_COUNT )((void) (0));
166056#endif
166057 i = yy_reduce_ofst[stateno];
166058 assert( iLookAhead!=YYNOCODE )((void) (0));
166059 i += iLookAhead;
166060#ifdef YYERRORSYMBOL
166061 if( i<0 || i>=YY_ACTTAB_COUNT(2098) || yy_lookahead[i]!=iLookAhead ){
166062 return yy_default[stateno];
166063 }
166064#else
166065 assert( i>=0 && i<YY_ACTTAB_COUNT )((void) (0));
166066 assert( yy_lookahead[i]==iLookAhead )((void) (0));
166067#endif
166068 return yy_action[i];
166069}
166070
166071/*
166072** The following routine is called if the stack overflows.
166073*/
166074static void yyStackOverflow(yyParser *yypParser){
166075 sqlite3ParserARG_FETCH
166076 sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse;
166077#ifndef NDEBUG1
166078 if( yyTraceFILE ){
166079 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
166080 }
166081#endif
166082 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
166083 /* Here code is inserted which will execute if the parser
166084 ** stack every overflows */
166085/******** Begin %stack_overflow code ******************************************/
166086
166087 sqlite3ErrorMsg(pParse, "parser stack overflow");
166088/******** End %stack_overflow code ********************************************/
166089 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument var */
166090 sqlite3ParserCTX_STOREyypParser->pParse=pParse;
166091}
166092
166093/*
166094** Print tracing information for a SHIFT action
166095*/
166096#ifndef NDEBUG1
166097static void yyTraceShift(yyParser *yypParser, int yyNewState, const char *zTag){
166098 if( yyTraceFILE ){
166099 if( yyNewState<YYNSTATE576 ){
166100 fprintf(yyTraceFILE,"%s%s '%s', go to state %d\n",
166101 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
166102 yyNewState);
166103 }else{
166104 fprintf(yyTraceFILE,"%s%s '%s', pending reduce %d\n",
166105 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
166106 yyNewState - YY_MIN_REDUCE1243);
166107 }
166108 }
166109}
166110#else
166111# define yyTraceShift(X,Y,Z)
166112#endif
166113
166114/*
166115** Perform a shift action.
166116*/
166117static void yy_shift(
166118 yyParser *yypParser, /* The parser to be shifted */
166119 YYACTIONTYPEunsigned short int yyNewState, /* The new state to shift in */
166120 YYCODETYPEunsigned short int yyMajor, /* The major token to shift in */
166121 sqlite3ParserTOKENTYPEToken yyMinor /* The minor token to shift in */
166122){
166123 yyStackEntry *yytos;
166124 yypParser->yytos++;
166125#ifdef YYTRACKMAXSTACKDEPTH
166126 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
166127 yypParser->yyhwm++;
166128 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) )((void) (0));
166129 }
166130#endif
166131#if YYSTACKDEPTH100>0
166132 if( yypParser->yytos>yypParser->yystackEnd ){
166133 yypParser->yytos--;
166134 yyStackOverflow(yypParser);
166135 return;
166136 }
166137#else
166138 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){
166139 if( yyGrowStack(yypParser) ){
166140 yypParser->yytos--;
166141 yyStackOverflow(yypParser);
166142 return;
166143 }
166144 }
166145#endif
166146 if( yyNewState > YY_MAX_SHIFT575 ){
166147 yyNewState += YY_MIN_REDUCE1243 - YY_MIN_SHIFTREDUCE835;
166148 }
166149 yytos = yypParser->yytos;
166150 yytos->stateno = yyNewState;
166151 yytos->major = yyMajor;
166152 yytos->minor.yy0 = yyMinor;
166153 yyTraceShift(yypParser, yyNewState, "Shift");
166154}
166155
166156/* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side
166157** of that rule */
166158static const YYCODETYPEunsigned short int yyRuleInfoLhs[] = {
166159 189, /* (0) explain ::= EXPLAIN */
166160 189, /* (1) explain ::= EXPLAIN QUERY PLAN */
166161 188, /* (2) cmdx ::= cmd */
166162 190, /* (3) cmd ::= BEGIN transtype trans_opt */
166163 191, /* (4) transtype ::= */
166164 191, /* (5) transtype ::= DEFERRED */
166165 191, /* (6) transtype ::= IMMEDIATE */
166166 191, /* (7) transtype ::= EXCLUSIVE */
166167 190, /* (8) cmd ::= COMMIT|END trans_opt */
166168 190, /* (9) cmd ::= ROLLBACK trans_opt */
166169 190, /* (10) cmd ::= SAVEPOINT nm */
166170 190, /* (11) cmd ::= RELEASE savepoint_opt nm */
166171 190, /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
166172 195, /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
166173 197, /* (14) createkw ::= CREATE */
166174 199, /* (15) ifnotexists ::= */
166175 199, /* (16) ifnotexists ::= IF NOT EXISTS */
166176 198, /* (17) temp ::= TEMP */
166177 198, /* (18) temp ::= */
166178 196, /* (19) create_table_args ::= LP columnlist conslist_opt RP table_option_set */
166179 196, /* (20) create_table_args ::= AS select */
166180 203, /* (21) table_option_set ::= */
166181 203, /* (22) table_option_set ::= table_option_set COMMA table_option */
166182 205, /* (23) table_option ::= WITHOUT nm */
166183 205, /* (24) table_option ::= nm */
166184 206, /* (25) columnname ::= nm typetoken */
166185 208, /* (26) typetoken ::= */
166186 208, /* (27) typetoken ::= typename LP signed RP */
166187 208, /* (28) typetoken ::= typename LP signed COMMA signed RP */
166188 209, /* (29) typename ::= typename ID|STRING */
166189 213, /* (30) scanpt ::= */
166190 214, /* (31) scantok ::= */
166191 215, /* (32) ccons ::= CONSTRAINT nm */
166192 215, /* (33) ccons ::= DEFAULT scantok term */
166193 215, /* (34) ccons ::= DEFAULT LP expr RP */
166194 215, /* (35) ccons ::= DEFAULT PLUS scantok term */
166195 215, /* (36) ccons ::= DEFAULT MINUS scantok term */
166196 215, /* (37) ccons ::= DEFAULT scantok ID|INDEXED */
166197 215, /* (38) ccons ::= NOT NULL onconf */
166198 215, /* (39) ccons ::= PRIMARY KEY sortorder onconf autoinc */
166199 215, /* (40) ccons ::= UNIQUE onconf */
166200 215, /* (41) ccons ::= CHECK LP expr RP */
166201 215, /* (42) ccons ::= REFERENCES nm eidlist_opt refargs */
166202 215, /* (43) ccons ::= defer_subclause */
166203 215, /* (44) ccons ::= COLLATE ID|STRING */
166204 224, /* (45) generated ::= LP expr RP */
166205 224, /* (46) generated ::= LP expr RP ID */
166206 220, /* (47) autoinc ::= */
166207 220, /* (48) autoinc ::= AUTOINCR */
166208 222, /* (49) refargs ::= */
166209 222, /* (50) refargs ::= refargs refarg */
166210 225, /* (51) refarg ::= MATCH nm */
166211 225, /* (52) refarg ::= ON INSERT refact */
166212 225, /* (53) refarg ::= ON DELETE refact */
166213 225, /* (54) refarg ::= ON UPDATE refact */
166214 226, /* (55) refact ::= SET NULL */
166215 226, /* (56) refact ::= SET DEFAULT */
166216 226, /* (57) refact ::= CASCADE */
166217 226, /* (58) refact ::= RESTRICT */
166218 226, /* (59) refact ::= NO ACTION */
166219 223, /* (60) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
166220 223, /* (61) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
166221 227, /* (62) init_deferred_pred_opt ::= */
166222 227, /* (63) init_deferred_pred_opt ::= INITIALLY DEFERRED */
166223 227, /* (64) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
166224 202, /* (65) conslist_opt ::= */
166225 229, /* (66) tconscomma ::= COMMA */
166226 230, /* (67) tcons ::= CONSTRAINT nm */
166227 230, /* (68) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
166228 230, /* (69) tcons ::= UNIQUE LP sortlist RP onconf */
166229 230, /* (70) tcons ::= CHECK LP expr RP onconf */
166230 230, /* (71) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
166231 233, /* (72) defer_subclause_opt ::= */
166232 218, /* (73) onconf ::= */
166233 218, /* (74) onconf ::= ON CONFLICT resolvetype */
166234 234, /* (75) orconf ::= */
166235 234, /* (76) orconf ::= OR resolvetype */
166236 235, /* (77) resolvetype ::= IGNORE */
166237 235, /* (78) resolvetype ::= REPLACE */
166238 190, /* (79) cmd ::= DROP TABLE ifexists fullname */
166239 237, /* (80) ifexists ::= IF EXISTS */
166240 237, /* (81) ifexists ::= */
166241 190, /* (82) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
166242 190, /* (83) cmd ::= DROP VIEW ifexists fullname */
166243 190, /* (84) cmd ::= select */
166244 204, /* (85) select ::= WITH wqlist selectnowith */
166245 204, /* (86) select ::= WITH RECURSIVE wqlist selectnowith */
166246 204, /* (87) select ::= selectnowith */
166247 239, /* (88) selectnowith ::= selectnowith multiselect_op oneselect */
166248 242, /* (89) multiselect_op ::= UNION */
166249 242, /* (90) multiselect_op ::= UNION ALL */
166250 242, /* (91) multiselect_op ::= EXCEPT|INTERSECT */
166251 240, /* (92) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
166252 240, /* (93) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
166253 252, /* (94) values ::= VALUES LP nexprlist RP */
166254 252, /* (95) values ::= values COMMA LP nexprlist RP */
166255 243, /* (96) distinct ::= DISTINCT */
166256 243, /* (97) distinct ::= ALL */
166257 243, /* (98) distinct ::= */
166258 254, /* (99) sclp ::= */
166259 244, /* (100) selcollist ::= sclp scanpt expr scanpt as */
166260 244, /* (101) selcollist ::= sclp scanpt STAR */
166261 244, /* (102) selcollist ::= sclp scanpt nm DOT STAR */
166262 255, /* (103) as ::= AS nm */
166263 255, /* (104) as ::= */
166264 245, /* (105) from ::= */
166265 245, /* (106) from ::= FROM seltablist */
166266 257, /* (107) stl_prefix ::= seltablist joinop */
166267 257, /* (108) stl_prefix ::= */
166268 256, /* (109) seltablist ::= stl_prefix nm dbnm as on_using */
166269 256, /* (110) seltablist ::= stl_prefix nm dbnm as indexed_by on_using */
166270 256, /* (111) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_using */
166271 256, /* (112) seltablist ::= stl_prefix LP select RP as on_using */
166272 256, /* (113) seltablist ::= stl_prefix LP seltablist RP as on_using */
166273 200, /* (114) dbnm ::= */
166274 200, /* (115) dbnm ::= DOT nm */
166275 238, /* (116) fullname ::= nm */
166276 238, /* (117) fullname ::= nm DOT nm */
166277 262, /* (118) xfullname ::= nm */
166278 262, /* (119) xfullname ::= nm DOT nm */
166279 262, /* (120) xfullname ::= nm DOT nm AS nm */
166280 262, /* (121) xfullname ::= nm AS nm */
166281 258, /* (122) joinop ::= COMMA|JOIN */
166282 258, /* (123) joinop ::= JOIN_KW JOIN */
166283 258, /* (124) joinop ::= JOIN_KW nm JOIN */
166284 258, /* (125) joinop ::= JOIN_KW nm nm JOIN */
166285 259, /* (126) on_using ::= ON expr */
166286 259, /* (127) on_using ::= USING LP idlist RP */
166287 259, /* (128) on_using ::= */
166288 264, /* (129) indexed_opt ::= */
166289 260, /* (130) indexed_by ::= INDEXED BY nm */
166290 260, /* (131) indexed_by ::= NOT INDEXED */
166291 249, /* (132) orderby_opt ::= */
166292 249, /* (133) orderby_opt ::= ORDER BY sortlist */
166293 231, /* (134) sortlist ::= sortlist COMMA expr sortorder nulls */
166294 231, /* (135) sortlist ::= expr sortorder nulls */
166295 219, /* (136) sortorder ::= ASC */
166296 219, /* (137) sortorder ::= DESC */
166297 219, /* (138) sortorder ::= */
166298 265, /* (139) nulls ::= NULLS FIRST */
166299 265, /* (140) nulls ::= NULLS LAST */
166300 265, /* (141) nulls ::= */
166301 247, /* (142) groupby_opt ::= */
166302 247, /* (143) groupby_opt ::= GROUP BY nexprlist */
166303 248, /* (144) having_opt ::= */
166304 248, /* (145) having_opt ::= HAVING expr */
166305 250, /* (146) limit_opt ::= */
166306 250, /* (147) limit_opt ::= LIMIT expr */
166307 250, /* (148) limit_opt ::= LIMIT expr OFFSET expr */
166308 250, /* (149) limit_opt ::= LIMIT expr COMMA expr */
166309 190, /* (150) cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret */
166310 246, /* (151) where_opt ::= */
166311 246, /* (152) where_opt ::= WHERE expr */
166312 267, /* (153) where_opt_ret ::= */
166313 267, /* (154) where_opt_ret ::= WHERE expr */
166314 267, /* (155) where_opt_ret ::= RETURNING selcollist */
166315 267, /* (156) where_opt_ret ::= WHERE expr RETURNING selcollist */
166316 190, /* (157) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */
166317 268, /* (158) setlist ::= setlist COMMA nm EQ expr */
166318 268, /* (159) setlist ::= setlist COMMA LP idlist RP EQ expr */
166319 268, /* (160) setlist ::= nm EQ expr */
166320 268, /* (161) setlist ::= LP idlist RP EQ expr */
166321 190, /* (162) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
166322 190, /* (163) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning */
166323 271, /* (164) upsert ::= */
166324 271, /* (165) upsert ::= RETURNING selcollist */
166325 271, /* (166) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt upsert */
166326 271, /* (167) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert */
166327 271, /* (168) upsert ::= ON CONFLICT DO NOTHING returning */
166328 271, /* (169) upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning */
166329 272, /* (170) returning ::= RETURNING selcollist */
166330 269, /* (171) insert_cmd ::= INSERT orconf */
166331 269, /* (172) insert_cmd ::= REPLACE */
166332 270, /* (173) idlist_opt ::= */
166333 270, /* (174) idlist_opt ::= LP idlist RP */
166334 263, /* (175) idlist ::= idlist COMMA nm */
166335 263, /* (176) idlist ::= nm */
166336 217, /* (177) expr ::= LP expr RP */
166337 217, /* (178) expr ::= ID|INDEXED */
166338 217, /* (179) expr ::= JOIN_KW */
166339 217, /* (180) expr ::= nm DOT nm */
166340 217, /* (181) expr ::= nm DOT nm DOT nm */
166341 216, /* (182) term ::= NULL|FLOAT|BLOB */
166342 216, /* (183) term ::= STRING */
166343 216, /* (184) term ::= INTEGER */
166344 217, /* (185) expr ::= VARIABLE */
166345 217, /* (186) expr ::= expr COLLATE ID|STRING */
166346 217, /* (187) expr ::= CAST LP expr AS typetoken RP */
166347 217, /* (188) expr ::= ID|INDEXED LP distinct exprlist RP */
166348 217, /* (189) expr ::= ID|INDEXED LP STAR RP */
166349 217, /* (190) expr ::= ID|INDEXED LP distinct exprlist RP filter_over */
166350 217, /* (191) expr ::= ID|INDEXED LP STAR RP filter_over */
166351 216, /* (192) term ::= CTIME_KW */
166352 217, /* (193) expr ::= LP nexprlist COMMA expr RP */
166353 217, /* (194) expr ::= expr AND expr */
166354 217, /* (195) expr ::= expr OR expr */
166355 217, /* (196) expr ::= expr LT|GT|GE|LE expr */
166356 217, /* (197) expr ::= expr EQ|NE expr */
166357 217, /* (198) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
166358 217, /* (199) expr ::= expr PLUS|MINUS expr */
166359 217, /* (200) expr ::= expr STAR|SLASH|REM expr */
166360 217, /* (201) expr ::= expr CONCAT expr */
166361 274, /* (202) likeop ::= NOT LIKE_KW|MATCH */
166362 217, /* (203) expr ::= expr likeop expr */
166363 217, /* (204) expr ::= expr likeop expr ESCAPE expr */
166364 217, /* (205) expr ::= expr ISNULL|NOTNULL */
166365 217, /* (206) expr ::= expr NOT NULL */
166366 217, /* (207) expr ::= expr IS expr */
166367 217, /* (208) expr ::= expr IS NOT expr */
166368 217, /* (209) expr ::= expr IS NOT DISTINCT FROM expr */
166369 217, /* (210) expr ::= expr IS DISTINCT FROM expr */
166370 217, /* (211) expr ::= NOT expr */
166371 217, /* (212) expr ::= BITNOT expr */
166372 217, /* (213) expr ::= PLUS|MINUS expr */
166373 217, /* (214) expr ::= expr PTR expr */
166374 275, /* (215) between_op ::= BETWEEN */
166375 275, /* (216) between_op ::= NOT BETWEEN */
166376 217, /* (217) expr ::= expr between_op expr AND expr */
166377 276, /* (218) in_op ::= IN */
166378 276, /* (219) in_op ::= NOT IN */
166379 217, /* (220) expr ::= expr in_op LP exprlist RP */
166380 217, /* (221) expr ::= LP select RP */
166381 217, /* (222) expr ::= expr in_op LP select RP */
166382 217, /* (223) expr ::= expr in_op nm dbnm paren_exprlist */
166383 217, /* (224) expr ::= EXISTS LP select RP */
166384 217, /* (225) expr ::= CASE case_operand case_exprlist case_else END */
166385 279, /* (226) case_exprlist ::= case_exprlist WHEN expr THEN expr */
166386 279, /* (227) case_exprlist ::= WHEN expr THEN expr */
166387 280, /* (228) case_else ::= ELSE expr */
166388 280, /* (229) case_else ::= */
166389 278, /* (230) case_operand ::= expr */
166390 278, /* (231) case_operand ::= */
166391 261, /* (232) exprlist ::= */
166392 253, /* (233) nexprlist ::= nexprlist COMMA expr */
166393 253, /* (234) nexprlist ::= expr */
166394 277, /* (235) paren_exprlist ::= */
166395 277, /* (236) paren_exprlist ::= LP exprlist RP */
166396 190, /* (237) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
166397 281, /* (238) uniqueflag ::= UNIQUE */
166398 281, /* (239) uniqueflag ::= */
166399 221, /* (240) eidlist_opt ::= */
166400 221, /* (241) eidlist_opt ::= LP eidlist RP */
166401 232, /* (242) eidlist ::= eidlist COMMA nm collate sortorder */
166402 232, /* (243) eidlist ::= nm collate sortorder */
166403 282, /* (244) collate ::= */
166404 282, /* (245) collate ::= COLLATE ID|STRING */
166405 190, /* (246) cmd ::= DROP INDEX ifexists fullname */
166406 190, /* (247) cmd ::= VACUUM vinto */
166407 190, /* (248) cmd ::= VACUUM nm vinto */
166408 283, /* (249) vinto ::= INTO expr */
166409 283, /* (250) vinto ::= */
166410 190, /* (251) cmd ::= PRAGMA nm dbnm */
166411 190, /* (252) cmd ::= PRAGMA nm dbnm EQ nmnum */
166412 190, /* (253) cmd ::= PRAGMA nm dbnm LP nmnum RP */
166413 190, /* (254) cmd ::= PRAGMA nm dbnm EQ minus_num */
166414 190, /* (255) cmd ::= PRAGMA nm dbnm LP minus_num RP */
166415 211, /* (256) plus_num ::= PLUS INTEGER|FLOAT */
166416 212, /* (257) minus_num ::= MINUS INTEGER|FLOAT */
166417 190, /* (258) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
166418 285, /* (259) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
166419 287, /* (260) trigger_time ::= BEFORE|AFTER */
166420 287, /* (261) trigger_time ::= INSTEAD OF */
166421 287, /* (262) trigger_time ::= */
166422 288, /* (263) trigger_event ::= DELETE|INSERT */
166423 288, /* (264) trigger_event ::= UPDATE */
166424 288, /* (265) trigger_event ::= UPDATE OF idlist */
166425 290, /* (266) when_clause ::= */
166426 290, /* (267) when_clause ::= WHEN expr */
166427 286, /* (268) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
166428 286, /* (269) trigger_cmd_list ::= trigger_cmd SEMI */
166429 292, /* (270) trnm ::= nm DOT nm */
166430 293, /* (271) tridxby ::= INDEXED BY nm */
166431 293, /* (272) tridxby ::= NOT INDEXED */
166432 291, /* (273) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt */
166433 291, /* (274) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
166434 291, /* (275) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
166435 291, /* (276) trigger_cmd ::= scanpt select scanpt */
166436 217, /* (277) expr ::= RAISE LP IGNORE RP */
166437 217, /* (278) expr ::= RAISE LP raisetype COMMA nm RP */
166438 236, /* (279) raisetype ::= ROLLBACK */
166439 236, /* (280) raisetype ::= ABORT */
166440 236, /* (281) raisetype ::= FAIL */
166441 190, /* (282) cmd ::= DROP TRIGGER ifexists fullname */
166442 190, /* (283) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
166443 190, /* (284) cmd ::= DETACH database_kw_opt expr */
166444 295, /* (285) key_opt ::= */
166445 295, /* (286) key_opt ::= KEY expr */
166446 190, /* (287) cmd ::= REINDEX */
166447 190, /* (288) cmd ::= REINDEX nm dbnm */
166448 190, /* (289) cmd ::= ANALYZE */
166449 190, /* (290) cmd ::= ANALYZE nm dbnm */
166450 190, /* (291) cmd ::= ALTER TABLE fullname RENAME TO nm */
166451 190, /* (292) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
166452 190, /* (293) cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm */
166453 296, /* (294) add_column_fullname ::= fullname */
166454 190, /* (295) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
166455 190, /* (296) cmd ::= create_vtab */
166456 190, /* (297) cmd ::= create_vtab LP vtabarglist RP */
166457 298, /* (298) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
166458 300, /* (299) vtabarg ::= */
166459 301, /* (300) vtabargtoken ::= ANY */
166460 301, /* (301) vtabargtoken ::= lp anylist RP */
166461 302, /* (302) lp ::= LP */
166462 266, /* (303) with ::= WITH wqlist */
166463 266, /* (304) with ::= WITH RECURSIVE wqlist */
166464 305, /* (305) wqas ::= AS */
166465 305, /* (306) wqas ::= AS MATERIALIZED */
166466 305, /* (307) wqas ::= AS NOT MATERIALIZED */
166467 304, /* (308) wqitem ::= nm eidlist_opt wqas LP select RP */
166468 241, /* (309) wqlist ::= wqitem */
166469 241, /* (310) wqlist ::= wqlist COMMA wqitem */
166470 306, /* (311) windowdefn_list ::= windowdefn */
166471 306, /* (312) windowdefn_list ::= windowdefn_list COMMA windowdefn */
166472 307, /* (313) windowdefn ::= nm AS LP window RP */
166473 308, /* (314) window ::= PARTITION BY nexprlist orderby_opt frame_opt */
166474 308, /* (315) window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
166475 308, /* (316) window ::= ORDER BY sortlist frame_opt */
166476 308, /* (317) window ::= nm ORDER BY sortlist frame_opt */
166477 308, /* (318) window ::= frame_opt */
166478 308, /* (319) window ::= nm frame_opt */
166479 309, /* (320) frame_opt ::= */
166480 309, /* (321) frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
166481 309, /* (322) frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
166482 313, /* (323) range_or_rows ::= RANGE|ROWS|GROUPS */
166483 315, /* (324) frame_bound_s ::= frame_bound */
166484 315, /* (325) frame_bound_s ::= UNBOUNDED PRECEDING */
166485 316, /* (326) frame_bound_e ::= frame_bound */
166486 316, /* (327) frame_bound_e ::= UNBOUNDED FOLLOWING */
166487 314, /* (328) frame_bound ::= expr PRECEDING|FOLLOWING */
166488 314, /* (329) frame_bound ::= CURRENT ROW */
166489 317, /* (330) frame_exclude_opt ::= */
166490 317, /* (331) frame_exclude_opt ::= EXCLUDE frame_exclude */
166491 318, /* (332) frame_exclude ::= NO OTHERS */
166492 318, /* (333) frame_exclude ::= CURRENT ROW */
166493 318, /* (334) frame_exclude ::= GROUP|TIES */
166494 251, /* (335) window_clause ::= WINDOW windowdefn_list */
166495 273, /* (336) filter_over ::= filter_clause over_clause */
166496 273, /* (337) filter_over ::= over_clause */
166497 273, /* (338) filter_over ::= filter_clause */
166498 312, /* (339) over_clause ::= OVER LP window RP */
166499 312, /* (340) over_clause ::= OVER nm */
166500 311, /* (341) filter_clause ::= FILTER LP WHERE expr RP */
166501 185, /* (342) input ::= cmdlist */
166502 186, /* (343) cmdlist ::= cmdlist ecmd */
166503 186, /* (344) cmdlist ::= ecmd */
166504 187, /* (345) ecmd ::= SEMI */
166505 187, /* (346) ecmd ::= cmdx SEMI */
166506 187, /* (347) ecmd ::= explain cmdx SEMI */
166507 192, /* (348) trans_opt ::= */
166508 192, /* (349) trans_opt ::= TRANSACTION */
166509 192, /* (350) trans_opt ::= TRANSACTION nm */
166510 194, /* (351) savepoint_opt ::= SAVEPOINT */
166511 194, /* (352) savepoint_opt ::= */
166512 190, /* (353) cmd ::= create_table create_table_args */
166513 203, /* (354) table_option_set ::= table_option */
166514 201, /* (355) columnlist ::= columnlist COMMA columnname carglist */
166515 201, /* (356) columnlist ::= columnname carglist */
166516 193, /* (357) nm ::= ID|INDEXED */
166517 193, /* (358) nm ::= STRING */
166518 193, /* (359) nm ::= JOIN_KW */
166519 208, /* (360) typetoken ::= typename */
166520 209, /* (361) typename ::= ID|STRING */
166521 210, /* (362) signed ::= plus_num */
166522 210, /* (363) signed ::= minus_num */
166523 207, /* (364) carglist ::= carglist ccons */
166524 207, /* (365) carglist ::= */
166525 215, /* (366) ccons ::= NULL onconf */
166526 215, /* (367) ccons ::= GENERATED ALWAYS AS generated */
166527 215, /* (368) ccons ::= AS generated */
166528 202, /* (369) conslist_opt ::= COMMA conslist */
166529 228, /* (370) conslist ::= conslist tconscomma tcons */
166530 228, /* (371) conslist ::= tcons */
166531 229, /* (372) tconscomma ::= */
166532 233, /* (373) defer_subclause_opt ::= defer_subclause */
166533 235, /* (374) resolvetype ::= raisetype */
166534 239, /* (375) selectnowith ::= oneselect */
166535 240, /* (376) oneselect ::= values */
166536 254, /* (377) sclp ::= selcollist COMMA */
166537 255, /* (378) as ::= ID|STRING */
166538 264, /* (379) indexed_opt ::= indexed_by */
166539 272, /* (380) returning ::= */
166540 217, /* (381) expr ::= term */
166541 274, /* (382) likeop ::= LIKE_KW|MATCH */
166542 261, /* (383) exprlist ::= nexprlist */
166543 284, /* (384) nmnum ::= plus_num */
166544 284, /* (385) nmnum ::= nm */
166545 284, /* (386) nmnum ::= ON */
166546 284, /* (387) nmnum ::= DELETE */
166547 284, /* (388) nmnum ::= DEFAULT */
166548 211, /* (389) plus_num ::= INTEGER|FLOAT */
166549 289, /* (390) foreach_clause ::= */
166550 289, /* (391) foreach_clause ::= FOR EACH ROW */
166551 292, /* (392) trnm ::= nm */
166552 293, /* (393) tridxby ::= */
166553 294, /* (394) database_kw_opt ::= DATABASE */
166554 294, /* (395) database_kw_opt ::= */
166555 297, /* (396) kwcolumn_opt ::= */
166556 297, /* (397) kwcolumn_opt ::= COLUMNKW */
166557 299, /* (398) vtabarglist ::= vtabarg */
166558 299, /* (399) vtabarglist ::= vtabarglist COMMA vtabarg */
166559 300, /* (400) vtabarg ::= vtabarg vtabargtoken */
166560 303, /* (401) anylist ::= */
166561 303, /* (402) anylist ::= anylist LP anylist RP */
166562 303, /* (403) anylist ::= anylist ANY */
166563 266, /* (404) with ::= */
166564};
166565
166566/* For rule J, yyRuleInfoNRhs[J] contains the negative of the number
166567** of symbols on the right-hand side of that rule. */
166568static const signed char yyRuleInfoNRhs[] = {
166569 -1, /* (0) explain ::= EXPLAIN */
166570 -3, /* (1) explain ::= EXPLAIN QUERY PLAN */
166571 -1, /* (2) cmdx ::= cmd */
166572 -3, /* (3) cmd ::= BEGIN transtype trans_opt */
166573 0, /* (4) transtype ::= */
166574 -1, /* (5) transtype ::= DEFERRED */
166575 -1, /* (6) transtype ::= IMMEDIATE */
166576 -1, /* (7) transtype ::= EXCLUSIVE */
166577 -2, /* (8) cmd ::= COMMIT|END trans_opt */
166578 -2, /* (9) cmd ::= ROLLBACK trans_opt */
166579 -2, /* (10) cmd ::= SAVEPOINT nm */
166580 -3, /* (11) cmd ::= RELEASE savepoint_opt nm */
166581 -5, /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
166582 -6, /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
166583 -1, /* (14) createkw ::= CREATE */
166584 0, /* (15) ifnotexists ::= */
166585 -3, /* (16) ifnotexists ::= IF NOT EXISTS */
166586 -1, /* (17) temp ::= TEMP */
166587 0, /* (18) temp ::= */
166588 -5, /* (19) create_table_args ::= LP columnlist conslist_opt RP table_option_set */
166589 -2, /* (20) create_table_args ::= AS select */
166590 0, /* (21) table_option_set ::= */
166591 -3, /* (22) table_option_set ::= table_option_set COMMA table_option */
166592 -2, /* (23) table_option ::= WITHOUT nm */
166593 -1, /* (24) table_option ::= nm */
166594 -2, /* (25) columnname ::= nm typetoken */
166595 0, /* (26) typetoken ::= */
166596 -4, /* (27) typetoken ::= typename LP signed RP */
166597 -6, /* (28) typetoken ::= typename LP signed COMMA signed RP */
166598 -2, /* (29) typename ::= typename ID|STRING */
166599 0, /* (30) scanpt ::= */
166600 0, /* (31) scantok ::= */
166601 -2, /* (32) ccons ::= CONSTRAINT nm */
166602 -3, /* (33) ccons ::= DEFAULT scantok term */
166603 -4, /* (34) ccons ::= DEFAULT LP expr RP */
166604 -4, /* (35) ccons ::= DEFAULT PLUS scantok term */
166605 -4, /* (36) ccons ::= DEFAULT MINUS scantok term */
166606 -3, /* (37) ccons ::= DEFAULT scantok ID|INDEXED */
166607 -3, /* (38) ccons ::= NOT NULL onconf */
166608 -5, /* (39) ccons ::= PRIMARY KEY sortorder onconf autoinc */
166609 -2, /* (40) ccons ::= UNIQUE onconf */
166610 -4, /* (41) ccons ::= CHECK LP expr RP */
166611 -4, /* (42) ccons ::= REFERENCES nm eidlist_opt refargs */
166612 -1, /* (43) ccons ::= defer_subclause */
166613 -2, /* (44) ccons ::= COLLATE ID|STRING */
166614 -3, /* (45) generated ::= LP expr RP */
166615 -4, /* (46) generated ::= LP expr RP ID */
166616 0, /* (47) autoinc ::= */
166617 -1, /* (48) autoinc ::= AUTOINCR */
166618 0, /* (49) refargs ::= */
166619 -2, /* (50) refargs ::= refargs refarg */
166620 -2, /* (51) refarg ::= MATCH nm */
166621 -3, /* (52) refarg ::= ON INSERT refact */
166622 -3, /* (53) refarg ::= ON DELETE refact */
166623 -3, /* (54) refarg ::= ON UPDATE refact */
166624 -2, /* (55) refact ::= SET NULL */
166625 -2, /* (56) refact ::= SET DEFAULT */
166626 -1, /* (57) refact ::= CASCADE */
166627 -1, /* (58) refact ::= RESTRICT */
166628 -2, /* (59) refact ::= NO ACTION */
166629 -3, /* (60) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
166630 -2, /* (61) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
166631 0, /* (62) init_deferred_pred_opt ::= */
166632 -2, /* (63) init_deferred_pred_opt ::= INITIALLY DEFERRED */
166633 -2, /* (64) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
166634 0, /* (65) conslist_opt ::= */
166635 -1, /* (66) tconscomma ::= COMMA */
166636 -2, /* (67) tcons ::= CONSTRAINT nm */
166637 -7, /* (68) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
166638 -5, /* (69) tcons ::= UNIQUE LP sortlist RP onconf */
166639 -5, /* (70) tcons ::= CHECK LP expr RP onconf */
166640 -10, /* (71) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
166641 0, /* (72) defer_subclause_opt ::= */
166642 0, /* (73) onconf ::= */
166643 -3, /* (74) onconf ::= ON CONFLICT resolvetype */
166644 0, /* (75) orconf ::= */
166645 -2, /* (76) orconf ::= OR resolvetype */
166646 -1, /* (77) resolvetype ::= IGNORE */
166647 -1, /* (78) resolvetype ::= REPLACE */
166648 -4, /* (79) cmd ::= DROP TABLE ifexists fullname */
166649 -2, /* (80) ifexists ::= IF EXISTS */
166650 0, /* (81) ifexists ::= */
166651 -9, /* (82) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
166652 -4, /* (83) cmd ::= DROP VIEW ifexists fullname */
166653 -1, /* (84) cmd ::= select */
166654 -3, /* (85) select ::= WITH wqlist selectnowith */
166655 -4, /* (86) select ::= WITH RECURSIVE wqlist selectnowith */
166656 -1, /* (87) select ::= selectnowith */
166657 -3, /* (88) selectnowith ::= selectnowith multiselect_op oneselect */
166658 -1, /* (89) multiselect_op ::= UNION */
166659 -2, /* (90) multiselect_op ::= UNION ALL */
166660 -1, /* (91) multiselect_op ::= EXCEPT|INTERSECT */
166661 -9, /* (92) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
166662 -10, /* (93) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
166663 -4, /* (94) values ::= VALUES LP nexprlist RP */
166664 -5, /* (95) values ::= values COMMA LP nexprlist RP */
166665 -1, /* (96) distinct ::= DISTINCT */
166666 -1, /* (97) distinct ::= ALL */
166667 0, /* (98) distinct ::= */
166668 0, /* (99) sclp ::= */
166669 -5, /* (100) selcollist ::= sclp scanpt expr scanpt as */
166670 -3, /* (101) selcollist ::= sclp scanpt STAR */
166671 -5, /* (102) selcollist ::= sclp scanpt nm DOT STAR */
166672 -2, /* (103) as ::= AS nm */
166673 0, /* (104) as ::= */
166674 0, /* (105) from ::= */
166675 -2, /* (106) from ::= FROM seltablist */
166676 -2, /* (107) stl_prefix ::= seltablist joinop */
166677 0, /* (108) stl_prefix ::= */
166678 -5, /* (109) seltablist ::= stl_prefix nm dbnm as on_using */
166679 -6, /* (110) seltablist ::= stl_prefix nm dbnm as indexed_by on_using */
166680 -8, /* (111) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_using */
166681 -6, /* (112) seltablist ::= stl_prefix LP select RP as on_using */
166682 -6, /* (113) seltablist ::= stl_prefix LP seltablist RP as on_using */
166683 0, /* (114) dbnm ::= */
166684 -2, /* (115) dbnm ::= DOT nm */
166685 -1, /* (116) fullname ::= nm */
166686 -3, /* (117) fullname ::= nm DOT nm */
166687 -1, /* (118) xfullname ::= nm */
166688 -3, /* (119) xfullname ::= nm DOT nm */
166689 -5, /* (120) xfullname ::= nm DOT nm AS nm */
166690 -3, /* (121) xfullname ::= nm AS nm */
166691 -1, /* (122) joinop ::= COMMA|JOIN */
166692 -2, /* (123) joinop ::= JOIN_KW JOIN */
166693 -3, /* (124) joinop ::= JOIN_KW nm JOIN */
166694 -4, /* (125) joinop ::= JOIN_KW nm nm JOIN */
166695 -2, /* (126) on_using ::= ON expr */
166696 -4, /* (127) on_using ::= USING LP idlist RP */
166697 0, /* (128) on_using ::= */
166698 0, /* (129) indexed_opt ::= */
166699 -3, /* (130) indexed_by ::= INDEXED BY nm */
166700 -2, /* (131) indexed_by ::= NOT INDEXED */
166701 0, /* (132) orderby_opt ::= */
166702 -3, /* (133) orderby_opt ::= ORDER BY sortlist */
166703 -5, /* (134) sortlist ::= sortlist COMMA expr sortorder nulls */
166704 -3, /* (135) sortlist ::= expr sortorder nulls */
166705 -1, /* (136) sortorder ::= ASC */
166706 -1, /* (137) sortorder ::= DESC */
166707 0, /* (138) sortorder ::= */
166708 -2, /* (139) nulls ::= NULLS FIRST */
166709 -2, /* (140) nulls ::= NULLS LAST */
166710 0, /* (141) nulls ::= */
166711 0, /* (142) groupby_opt ::= */
166712 -3, /* (143) groupby_opt ::= GROUP BY nexprlist */
166713 0, /* (144) having_opt ::= */
166714 -2, /* (145) having_opt ::= HAVING expr */
166715 0, /* (146) limit_opt ::= */
166716 -2, /* (147) limit_opt ::= LIMIT expr */
166717 -4, /* (148) limit_opt ::= LIMIT expr OFFSET expr */
166718 -4, /* (149) limit_opt ::= LIMIT expr COMMA expr */
166719 -6, /* (150) cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret */
166720 0, /* (151) where_opt ::= */
166721 -2, /* (152) where_opt ::= WHERE expr */
166722 0, /* (153) where_opt_ret ::= */
166723 -2, /* (154) where_opt_ret ::= WHERE expr */
166724 -2, /* (155) where_opt_ret ::= RETURNING selcollist */
166725 -4, /* (156) where_opt_ret ::= WHERE expr RETURNING selcollist */
166726 -9, /* (157) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */
166727 -5, /* (158) setlist ::= setlist COMMA nm EQ expr */
166728 -7, /* (159) setlist ::= setlist COMMA LP idlist RP EQ expr */
166729 -3, /* (160) setlist ::= nm EQ expr */
166730 -5, /* (161) setlist ::= LP idlist RP EQ expr */
166731 -7, /* (162) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
166732 -8, /* (163) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning */
166733 0, /* (164) upsert ::= */
166734 -2, /* (165) upsert ::= RETURNING selcollist */
166735 -12, /* (166) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt upsert */
166736 -9, /* (167) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert */
166737 -5, /* (168) upsert ::= ON CONFLICT DO NOTHING returning */
166738 -8, /* (169) upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning */
166739 -2, /* (170) returning ::= RETURNING selcollist */
166740 -2, /* (171) insert_cmd ::= INSERT orconf */
166741 -1, /* (172) insert_cmd ::= REPLACE */
166742 0, /* (173) idlist_opt ::= */
166743 -3, /* (174) idlist_opt ::= LP idlist RP */
166744 -3, /* (175) idlist ::= idlist COMMA nm */
166745 -1, /* (176) idlist ::= nm */
166746 -3, /* (177) expr ::= LP expr RP */
166747 -1, /* (178) expr ::= ID|INDEXED */
166748 -1, /* (179) expr ::= JOIN_KW */
166749 -3, /* (180) expr ::= nm DOT nm */
166750 -5, /* (181) expr ::= nm DOT nm DOT nm */
166751 -1, /* (182) term ::= NULL|FLOAT|BLOB */
166752 -1, /* (183) term ::= STRING */
166753 -1, /* (184) term ::= INTEGER */
166754 -1, /* (185) expr ::= VARIABLE */
166755 -3, /* (186) expr ::= expr COLLATE ID|STRING */
166756 -6, /* (187) expr ::= CAST LP expr AS typetoken RP */
166757 -5, /* (188) expr ::= ID|INDEXED LP distinct exprlist RP */
166758 -4, /* (189) expr ::= ID|INDEXED LP STAR RP */
166759 -6, /* (190) expr ::= ID|INDEXED LP distinct exprlist RP filter_over */
166760 -5, /* (191) expr ::= ID|INDEXED LP STAR RP filter_over */
166761 -1, /* (192) term ::= CTIME_KW */
166762 -5, /* (193) expr ::= LP nexprlist COMMA expr RP */
166763 -3, /* (194) expr ::= expr AND expr */
166764 -3, /* (195) expr ::= expr OR expr */
166765 -3, /* (196) expr ::= expr LT|GT|GE|LE expr */
166766 -3, /* (197) expr ::= expr EQ|NE expr */
166767 -3, /* (198) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
166768 -3, /* (199) expr ::= expr PLUS|MINUS expr */
166769 -3, /* (200) expr ::= expr STAR|SLASH|REM expr */
166770 -3, /* (201) expr ::= expr CONCAT expr */
166771 -2, /* (202) likeop ::= NOT LIKE_KW|MATCH */
166772 -3, /* (203) expr ::= expr likeop expr */
166773 -5, /* (204) expr ::= expr likeop expr ESCAPE expr */
166774 -2, /* (205) expr ::= expr ISNULL|NOTNULL */
166775 -3, /* (206) expr ::= expr NOT NULL */
166776 -3, /* (207) expr ::= expr IS expr */
166777 -4, /* (208) expr ::= expr IS NOT expr */
166778 -6, /* (209) expr ::= expr IS NOT DISTINCT FROM expr */
166779 -5, /* (210) expr ::= expr IS DISTINCT FROM expr */
166780 -2, /* (211) expr ::= NOT expr */
166781 -2, /* (212) expr ::= BITNOT expr */
166782 -2, /* (213) expr ::= PLUS|MINUS expr */
166783 -3, /* (214) expr ::= expr PTR expr */
166784 -1, /* (215) between_op ::= BETWEEN */
166785 -2, /* (216) between_op ::= NOT BETWEEN */
166786 -5, /* (217) expr ::= expr between_op expr AND expr */
166787 -1, /* (218) in_op ::= IN */
166788 -2, /* (219) in_op ::= NOT IN */
166789 -5, /* (220) expr ::= expr in_op LP exprlist RP */
166790 -3, /* (221) expr ::= LP select RP */
166791 -5, /* (222) expr ::= expr in_op LP select RP */
166792 -5, /* (223) expr ::= expr in_op nm dbnm paren_exprlist */
166793 -4, /* (224) expr ::= EXISTS LP select RP */
166794 -5, /* (225) expr ::= CASE case_operand case_exprlist case_else END */
166795 -5, /* (226) case_exprlist ::= case_exprlist WHEN expr THEN expr */
166796 -4, /* (227) case_exprlist ::= WHEN expr THEN expr */
166797 -2, /* (228) case_else ::= ELSE expr */
166798 0, /* (229) case_else ::= */
166799 -1, /* (230) case_operand ::= expr */
166800 0, /* (231) case_operand ::= */
166801 0, /* (232) exprlist ::= */
166802 -3, /* (233) nexprlist ::= nexprlist COMMA expr */
166803 -1, /* (234) nexprlist ::= expr */
166804 0, /* (235) paren_exprlist ::= */
166805 -3, /* (236) paren_exprlist ::= LP exprlist RP */
166806 -12, /* (237) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
166807 -1, /* (238) uniqueflag ::= UNIQUE */
166808 0, /* (239) uniqueflag ::= */
166809 0, /* (240) eidlist_opt ::= */
166810 -3, /* (241) eidlist_opt ::= LP eidlist RP */
166811 -5, /* (242) eidlist ::= eidlist COMMA nm collate sortorder */
166812 -3, /* (243) eidlist ::= nm collate sortorder */
166813 0, /* (244) collate ::= */
166814 -2, /* (245) collate ::= COLLATE ID|STRING */
166815 -4, /* (246) cmd ::= DROP INDEX ifexists fullname */
166816 -2, /* (247) cmd ::= VACUUM vinto */
166817 -3, /* (248) cmd ::= VACUUM nm vinto */
166818 -2, /* (249) vinto ::= INTO expr */
166819 0, /* (250) vinto ::= */
166820 -3, /* (251) cmd ::= PRAGMA nm dbnm */
166821 -5, /* (252) cmd ::= PRAGMA nm dbnm EQ nmnum */
166822 -6, /* (253) cmd ::= PRAGMA nm dbnm LP nmnum RP */
166823 -5, /* (254) cmd ::= PRAGMA nm dbnm EQ minus_num */
166824 -6, /* (255) cmd ::= PRAGMA nm dbnm LP minus_num RP */
166825 -2, /* (256) plus_num ::= PLUS INTEGER|FLOAT */
166826 -2, /* (257) minus_num ::= MINUS INTEGER|FLOAT */
166827 -5, /* (258) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
166828 -11, /* (259) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
166829 -1, /* (260) trigger_time ::= BEFORE|AFTER */
166830 -2, /* (261) trigger_time ::= INSTEAD OF */
166831 0, /* (262) trigger_time ::= */
166832 -1, /* (263) trigger_event ::= DELETE|INSERT */
166833 -1, /* (264) trigger_event ::= UPDATE */
166834 -3, /* (265) trigger_event ::= UPDATE OF idlist */
166835 0, /* (266) when_clause ::= */
166836 -2, /* (267) when_clause ::= WHEN expr */
166837 -3, /* (268) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
166838 -2, /* (269) trigger_cmd_list ::= trigger_cmd SEMI */
166839 -3, /* (270) trnm ::= nm DOT nm */
166840 -3, /* (271) tridxby ::= INDEXED BY nm */
166841 -2, /* (272) tridxby ::= NOT INDEXED */
166842 -9, /* (273) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt */
166843 -8, /* (274) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
166844 -6, /* (275) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
166845 -3, /* (276) trigger_cmd ::= scanpt select scanpt */
166846 -4, /* (277) expr ::= RAISE LP IGNORE RP */
166847 -6, /* (278) expr ::= RAISE LP raisetype COMMA nm RP */
166848 -1, /* (279) raisetype ::= ROLLBACK */
166849 -1, /* (280) raisetype ::= ABORT */
166850 -1, /* (281) raisetype ::= FAIL */
166851 -4, /* (282) cmd ::= DROP TRIGGER ifexists fullname */
166852 -6, /* (283) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
166853 -3, /* (284) cmd ::= DETACH database_kw_opt expr */
166854 0, /* (285) key_opt ::= */
166855 -2, /* (286) key_opt ::= KEY expr */
166856 -1, /* (287) cmd ::= REINDEX */
166857 -3, /* (288) cmd ::= REINDEX nm dbnm */
166858 -1, /* (289) cmd ::= ANALYZE */
166859 -3, /* (290) cmd ::= ANALYZE nm dbnm */
166860 -6, /* (291) cmd ::= ALTER TABLE fullname RENAME TO nm */
166861 -7, /* (292) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
166862 -6, /* (293) cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm */
166863 -1, /* (294) add_column_fullname ::= fullname */
166864 -8, /* (295) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
166865 -1, /* (296) cmd ::= create_vtab */
166866 -4, /* (297) cmd ::= create_vtab LP vtabarglist RP */
166867 -8, /* (298) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
166868 0, /* (299) vtabarg ::= */
166869 -1, /* (300) vtabargtoken ::= ANY */
166870 -3, /* (301) vtabargtoken ::= lp anylist RP */
166871 -1, /* (302) lp ::= LP */
166872 -2, /* (303) with ::= WITH wqlist */
166873 -3, /* (304) with ::= WITH RECURSIVE wqlist */
166874 -1, /* (305) wqas ::= AS */
166875 -2, /* (306) wqas ::= AS MATERIALIZED */
166876 -3, /* (307) wqas ::= AS NOT MATERIALIZED */
166877 -6, /* (308) wqitem ::= nm eidlist_opt wqas LP select RP */
166878 -1, /* (309) wqlist ::= wqitem */
166879 -3, /* (310) wqlist ::= wqlist COMMA wqitem */
166880 -1, /* (311) windowdefn_list ::= windowdefn */
166881 -3, /* (312) windowdefn_list ::= windowdefn_list COMMA windowdefn */
166882 -5, /* (313) windowdefn ::= nm AS LP window RP */
166883 -5, /* (314) window ::= PARTITION BY nexprlist orderby_opt frame_opt */
166884 -6, /* (315) window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
166885 -4, /* (316) window ::= ORDER BY sortlist frame_opt */
166886 -5, /* (317) window ::= nm ORDER BY sortlist frame_opt */
166887 -1, /* (318) window ::= frame_opt */
166888 -2, /* (319) window ::= nm frame_opt */
166889 0, /* (320) frame_opt ::= */
166890 -3, /* (321) frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
166891 -6, /* (322) frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
166892 -1, /* (323) range_or_rows ::= RANGE|ROWS|GROUPS */
166893 -1, /* (324) frame_bound_s ::= frame_bound */
166894 -2, /* (325) frame_bound_s ::= UNBOUNDED PRECEDING */
166895 -1, /* (326) frame_bound_e ::= frame_bound */
166896 -2, /* (327) frame_bound_e ::= UNBOUNDED FOLLOWING */
166897 -2, /* (328) frame_bound ::= expr PRECEDING|FOLLOWING */
166898 -2, /* (329) frame_bound ::= CURRENT ROW */
166899 0, /* (330) frame_exclude_opt ::= */
166900 -2, /* (331) frame_exclude_opt ::= EXCLUDE frame_exclude */
166901 -2, /* (332) frame_exclude ::= NO OTHERS */
166902 -2, /* (333) frame_exclude ::= CURRENT ROW */
166903 -1, /* (334) frame_exclude ::= GROUP|TIES */
166904 -2, /* (335) window_clause ::= WINDOW windowdefn_list */
166905 -2, /* (336) filter_over ::= filter_clause over_clause */
166906 -1, /* (337) filter_over ::= over_clause */
166907 -1, /* (338) filter_over ::= filter_clause */
166908 -4, /* (339) over_clause ::= OVER LP window RP */
166909 -2, /* (340) over_clause ::= OVER nm */
166910 -5, /* (341) filter_clause ::= FILTER LP WHERE expr RP */
166911 -1, /* (342) input ::= cmdlist */
166912 -2, /* (343) cmdlist ::= cmdlist ecmd */
166913 -1, /* (344) cmdlist ::= ecmd */
166914 -1, /* (345) ecmd ::= SEMI */
166915 -2, /* (346) ecmd ::= cmdx SEMI */
166916 -3, /* (347) ecmd ::= explain cmdx SEMI */
166917 0, /* (348) trans_opt ::= */
166918 -1, /* (349) trans_opt ::= TRANSACTION */
166919 -2, /* (350) trans_opt ::= TRANSACTION nm */
166920 -1, /* (351) savepoint_opt ::= SAVEPOINT */
166921 0, /* (352) savepoint_opt ::= */
166922 -2, /* (353) cmd ::= create_table create_table_args */
166923 -1, /* (354) table_option_set ::= table_option */
166924 -4, /* (355) columnlist ::= columnlist COMMA columnname carglist */
166925 -2, /* (356) columnlist ::= columnname carglist */
166926 -1, /* (357) nm ::= ID|INDEXED */
166927 -1, /* (358) nm ::= STRING */
166928 -1, /* (359) nm ::= JOIN_KW */
166929 -1, /* (360) typetoken ::= typename */
166930 -1, /* (361) typename ::= ID|STRING */
166931 -1, /* (362) signed ::= plus_num */
166932 -1, /* (363) signed ::= minus_num */
166933 -2, /* (364) carglist ::= carglist ccons */
166934 0, /* (365) carglist ::= */
166935 -2, /* (366) ccons ::= NULL onconf */
166936 -4, /* (367) ccons ::= GENERATED ALWAYS AS generated */
166937 -2, /* (368) ccons ::= AS generated */
166938 -2, /* (369) conslist_opt ::= COMMA conslist */
166939 -3, /* (370) conslist ::= conslist tconscomma tcons */
166940 -1, /* (371) conslist ::= tcons */
166941 0, /* (372) tconscomma ::= */
166942 -1, /* (373) defer_subclause_opt ::= defer_subclause */
166943 -1, /* (374) resolvetype ::= raisetype */
166944 -1, /* (375) selectnowith ::= oneselect */
166945 -1, /* (376) oneselect ::= values */
166946 -2, /* (377) sclp ::= selcollist COMMA */
166947 -1, /* (378) as ::= ID|STRING */
166948 -1, /* (379) indexed_opt ::= indexed_by */
166949 0, /* (380) returning ::= */
166950 -1, /* (381) expr ::= term */
166951 -1, /* (382) likeop ::= LIKE_KW|MATCH */
166952 -1, /* (383) exprlist ::= nexprlist */
166953 -1, /* (384) nmnum ::= plus_num */
166954 -1, /* (385) nmnum ::= nm */
166955 -1, /* (386) nmnum ::= ON */
166956 -1, /* (387) nmnum ::= DELETE */
166957 -1, /* (388) nmnum ::= DEFAULT */
166958 -1, /* (389) plus_num ::= INTEGER|FLOAT */
166959 0, /* (390) foreach_clause ::= */
166960 -3, /* (391) foreach_clause ::= FOR EACH ROW */
166961 -1, /* (392) trnm ::= nm */
166962 0, /* (393) tridxby ::= */
166963 -1, /* (394) database_kw_opt ::= DATABASE */
166964 0, /* (395) database_kw_opt ::= */
166965 0, /* (396) kwcolumn_opt ::= */
166966 -1, /* (397) kwcolumn_opt ::= COLUMNKW */
166967 -1, /* (398) vtabarglist ::= vtabarg */
166968 -3, /* (399) vtabarglist ::= vtabarglist COMMA vtabarg */
166969 -2, /* (400) vtabarg ::= vtabarg vtabargtoken */
166970 0, /* (401) anylist ::= */
166971 -4, /* (402) anylist ::= anylist LP anylist RP */
166972 -2, /* (403) anylist ::= anylist ANY */
166973 0, /* (404) with ::= */
166974};
166975
166976static void yy_accept(yyParser*); /* Forward Declaration */
166977
166978/*
166979** Perform a reduce action and the shift that must immediately
166980** follow the reduce.
166981**
166982** The yyLookahead and yyLookaheadToken parameters provide reduce actions
166983** access to the lookahead token (if any). The yyLookahead will be YYNOCODE
166984** if the lookahead token has already been consumed. As this procedure is
166985** only called from one place, optimizing compilers will in-line it, which
166986** means that the extra parameters have no performance impact.
166987*/
166988static YYACTIONTYPEunsigned short int yy_reduce(
166989 yyParser *yypParser, /* The parser */
166990 unsigned int yyruleno, /* Number of the rule by which to reduce */
166991 int yyLookahead, /* Lookahead token, or YYNOCODE if none */
166992 sqlite3ParserTOKENTYPEToken yyLookaheadToken /* Value of the lookahead token */
166993 sqlite3ParserCTX_PDECL,Parse *pParse /* %extra_context */
166994){
166995 int yygoto; /* The next state */
166996 YYACTIONTYPEunsigned short int yyact; /* The next action */
166997 yyStackEntry *yymsp; /* The top of the parser's stack */
166998 int yysize; /* Amount to pop the stack */
166999 sqlite3ParserARG_FETCH
167000 (void)yyLookahead;
167001 (void)yyLookaheadToken;
167002 yymsp = yypParser->yytos;
167003
167004 switch( yyruleno ){
167005 /* Beginning here are the reduction cases. A typical example
167006 ** follows:
167007 ** case 0:
167008 ** #line <lineno> <grammarfile>
167009 ** { ... } // User supplied code
167010 ** #line <lineno> <thisfile>
167011 ** break;
167012 */
167013/********** Begin reduce actions **********************************************/
167014 YYMINORTYPE yylhsminor;
167015 case 0: /* explain ::= EXPLAIN */
167016{ pParse->explain = 1; }
167017 break;
167018 case 1: /* explain ::= EXPLAIN QUERY PLAN */
167019{ pParse->explain = 2; }
167020 break;
167021 case 2: /* cmdx ::= cmd */
167022{ sqlite3FinishCoding(pParse); }
167023 break;
167024 case 3: /* cmd ::= BEGIN transtype trans_opt */
167025{sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy394);}
167026 break;
167027 case 4: /* transtype ::= */
167028{yymsp[1].minor.yy394 = TK_DEFERRED7;}
167029 break;
167030 case 5: /* transtype ::= DEFERRED */
167031 case 6: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==6);
167032 case 7: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==7);
167033 case 323: /* range_or_rows ::= RANGE|ROWS|GROUPS */ yytestcase(yyruleno==323);
167034{yymsp[0].minor.yy394 = yymsp[0].major; /*A-overwrites-X*/}
167035 break;
167036 case 8: /* cmd ::= COMMIT|END trans_opt */
167037 case 9: /* cmd ::= ROLLBACK trans_opt */ yytestcase(yyruleno==9);
167038{sqlite3EndTransaction(pParse,yymsp[-1].major);}
167039 break;
167040 case 10: /* cmd ::= SAVEPOINT nm */
167041{
167042 sqlite3Savepoint(pParse, SAVEPOINT_BEGIN0, &yymsp[0].minor.yy0);
167043}
167044 break;
167045 case 11: /* cmd ::= RELEASE savepoint_opt nm */
167046{
167047 sqlite3Savepoint(pParse, SAVEPOINT_RELEASE1, &yymsp[0].minor.yy0);
167048}
167049 break;
167050 case 12: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
167051{
167052 sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK2, &yymsp[0].minor.yy0);
167053}
167054 break;
167055 case 13: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */
167056{
167057 sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy394,0,0,yymsp[-2].minor.yy394);
167058}
167059 break;
167060 case 14: /* createkw ::= CREATE */
167061{disableLookaside(pParse);}
167062 break;
167063 case 15: /* ifnotexists ::= */
167064 case 18: /* temp ::= */ yytestcase(yyruleno==18);
167065 case 47: /* autoinc ::= */ yytestcase(yyruleno==47);
167066 case 62: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==62);
167067 case 72: /* defer_subclause_opt ::= */ yytestcase(yyruleno==72);
167068 case 81: /* ifexists ::= */ yytestcase(yyruleno==81);
167069 case 98: /* distinct ::= */ yytestcase(yyruleno==98);
167070 case 244: /* collate ::= */ yytestcase(yyruleno==244);
167071{yymsp[1].minor.yy394 = 0;}
167072 break;
167073 case 16: /* ifnotexists ::= IF NOT EXISTS */
167074{yymsp[-2].minor.yy394 = 1;}
167075 break;
167076 case 17: /* temp ::= TEMP */
167077{yymsp[0].minor.yy394 = pParse->db->init.busy==0;}
167078 break;
167079 case 19: /* create_table_args ::= LP columnlist conslist_opt RP table_option_set */
167080{
167081 sqlite3EndTable(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,yymsp[0].minor.yy285,0);
167082}
167083 break;
167084 case 20: /* create_table_args ::= AS select */
167085{
167086 sqlite3EndTable(pParse,0,0,0,yymsp[0].minor.yy47);
167087 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy47);
167088}
167089 break;
167090 case 21: /* table_option_set ::= */
167091{yymsp[1].minor.yy285 = 0;}
167092 break;
167093 case 22: /* table_option_set ::= table_option_set COMMA table_option */
167094{yylhsminor.yy285 = yymsp[-2].minor.yy285|yymsp[0].minor.yy285;}
167095 yymsp[-2].minor.yy285 = yylhsminor.yy285;
167096 break;
167097 case 23: /* table_option ::= WITHOUT nm */
167098{
167099 if( yymsp[0].minor.yy0.n==5 && sqlite3_strnicmp(yymsp[0].minor.yy0.z,"rowid",5)==0 ){
167100 yymsp[-1].minor.yy285 = TF_WithoutRowid0x00000080 | TF_NoVisibleRowid0x00000200;
167101 }else{
167102 yymsp[-1].minor.yy285 = 0;
167103 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.z);
167104 }
167105}
167106 break;
167107 case 24: /* table_option ::= nm */
167108{
167109 if( yymsp[0].minor.yy0.n==6 && sqlite3_strnicmp(yymsp[0].minor.yy0.z,"strict",6)==0 ){
167110 yylhsminor.yy285 = TF_Strict0x00010000;
167111 }else{
167112 yylhsminor.yy285 = 0;
167113 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.z);
167114 }
167115}
167116 yymsp[0].minor.yy285 = yylhsminor.yy285;
167117 break;
167118 case 25: /* columnname ::= nm typetoken */
167119{sqlite3AddColumn(pParse,yymsp[-1].minor.yy0,yymsp[0].minor.yy0);}
167120 break;
167121 case 26: /* typetoken ::= */
167122 case 65: /* conslist_opt ::= */ yytestcase(yyruleno==65);
167123 case 104: /* as ::= */ yytestcase(yyruleno==104);
167124{yymsp[1].minor.yy0.n = 0; yymsp[1].minor.yy0.z = 0;}
167125 break;
167126 case 27: /* typetoken ::= typename LP signed RP */
167127{
167128 yymsp[-3].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
167129}
167130 break;
167131 case 28: /* typetoken ::= typename LP signed COMMA signed RP */
167132{
167133 yymsp[-5].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
167134}
167135 break;
167136 case 29: /* typename ::= typename ID|STRING */
167137{yymsp[-1].minor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
167138 break;
167139 case 30: /* scanpt ::= */
167140{
167141 assert( yyLookahead!=YYNOCODE )((void) (0));
167142 yymsp[1].minor.yy522 = yyLookaheadToken.z;
167143}
167144 break;
167145 case 31: /* scantok ::= */
167146{
167147 assert( yyLookahead!=YYNOCODE )((void) (0));
167148 yymsp[1].minor.yy0 = yyLookaheadToken;
167149}
167150 break;
167151 case 32: /* ccons ::= CONSTRAINT nm */
167152 case 67: /* tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==67);
167153{pParse->constraintName = yymsp[0].minor.yy0;}
167154 break;
167155 case 33: /* ccons ::= DEFAULT scantok term */
167156{sqlite3AddDefaultValue(pParse,yymsp[0].minor.yy528,yymsp[-1].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]);}
167157 break;
167158 case 34: /* ccons ::= DEFAULT LP expr RP */
167159{sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy528,yymsp[-2].minor.yy0.z+1,yymsp[0].minor.yy0.z);}
167160 break;
167161 case 35: /* ccons ::= DEFAULT PLUS scantok term */
167162{sqlite3AddDefaultValue(pParse,yymsp[0].minor.yy528,yymsp[-2].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]);}
167163 break;
167164 case 36: /* ccons ::= DEFAULT MINUS scantok term */
167165{
167166 Expr *p = sqlite3PExpr(pParse, TK_UMINUS173, yymsp[0].minor.yy528, 0);
167167 sqlite3AddDefaultValue(pParse,p,yymsp[-2].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]);
167168}
167169 break;
167170 case 37: /* ccons ::= DEFAULT scantok ID|INDEXED */
167171{
167172 Expr *p = tokenExpr(pParse, TK_STRING117, yymsp[0].minor.yy0);
167173 if( p ){
167174 sqlite3ExprIdToTrueFalse(p);
167175 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
167176 }
167177 sqlite3AddDefaultValue(pParse,p,yymsp[0].minor.yy0.z,yymsp[0].minor.yy0.z+yymsp[0].minor.yy0.n);
167178}
167179 break;
167180 case 38: /* ccons ::= NOT NULL onconf */
167181{sqlite3AddNotNull(pParse, yymsp[0].minor.yy394);}
167182 break;
167183 case 39: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
167184{sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy394,yymsp[0].minor.yy394,yymsp[-2].minor.yy394);}
167185 break;
167186 case 40: /* ccons ::= UNIQUE onconf */
167187{sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy394,0,0,0,0,
167188 SQLITE_IDXTYPE_UNIQUE1);}
167189 break;
167190 case 41: /* ccons ::= CHECK LP expr RP */
167191{sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy528,yymsp[-2].minor.yy0.z,yymsp[0].minor.yy0.z);}
167192 break;
167193 case 42: /* ccons ::= REFERENCES nm eidlist_opt refargs */
167194{sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy322,yymsp[0].minor.yy394);}
167195 break;
167196 case 43: /* ccons ::= defer_subclause */
167197{sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy394);}
167198 break;
167199 case 44: /* ccons ::= COLLATE ID|STRING */
167200{sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);}
167201 break;
167202 case 45: /* generated ::= LP expr RP */
167203{sqlite3AddGenerated(pParse,yymsp[-1].minor.yy528,0);}
167204 break;
167205 case 46: /* generated ::= LP expr RP ID */
167206{sqlite3AddGenerated(pParse,yymsp[-2].minor.yy528,&yymsp[0].minor.yy0);}
167207 break;
167208 case 48: /* autoinc ::= AUTOINCR */
167209{yymsp[0].minor.yy394 = 1;}
167210 break;
167211 case 49: /* refargs ::= */
167212{ yymsp[1].minor.yy394 = OE_None0*0x0101; /* EV: R-19803-45884 */}
167213 break;
167214 case 50: /* refargs ::= refargs refarg */
167215{ yymsp[-1].minor.yy394 = (yymsp[-1].minor.yy394 & ~yymsp[0].minor.yy231.mask) | yymsp[0].minor.yy231.value; }
167216 break;
167217 case 51: /* refarg ::= MATCH nm */
167218{ yymsp[-1].minor.yy231.value = 0; yymsp[-1].minor.yy231.mask = 0x000000; }
167219 break;
167220 case 52: /* refarg ::= ON INSERT refact */
167221{ yymsp[-2].minor.yy231.value = 0; yymsp[-2].minor.yy231.mask = 0x000000; }
167222 break;
167223 case 53: /* refarg ::= ON DELETE refact */
167224{ yymsp[-2].minor.yy231.value = yymsp[0].minor.yy394; yymsp[-2].minor.yy231.mask = 0x0000ff; }
167225 break;
167226 case 54: /* refarg ::= ON UPDATE refact */
167227{ yymsp[-2].minor.yy231.value = yymsp[0].minor.yy394<<8; yymsp[-2].minor.yy231.mask = 0x00ff00; }
167228 break;
167229 case 55: /* refact ::= SET NULL */
167230{ yymsp[-1].minor.yy394 = OE_SetNull8; /* EV: R-33326-45252 */}
167231 break;
167232 case 56: /* refact ::= SET DEFAULT */
167233{ yymsp[-1].minor.yy394 = OE_SetDflt9; /* EV: R-33326-45252 */}
167234 break;
167235 case 57: /* refact ::= CASCADE */
167236{ yymsp[0].minor.yy394 = OE_Cascade10; /* EV: R-33326-45252 */}
167237 break;
167238 case 58: /* refact ::= RESTRICT */
167239{ yymsp[0].minor.yy394 = OE_Restrict7; /* EV: R-33326-45252 */}
167240 break;
167241 case 59: /* refact ::= NO ACTION */
167242{ yymsp[-1].minor.yy394 = OE_None0; /* EV: R-33326-45252 */}
167243 break;
167244 case 60: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
167245{yymsp[-2].minor.yy394 = 0;}
167246 break;
167247 case 61: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
167248 case 76: /* orconf ::= OR resolvetype */ yytestcase(yyruleno==76);
167249 case 171: /* insert_cmd ::= INSERT orconf */ yytestcase(yyruleno==171);
167250{yymsp[-1].minor.yy394 = yymsp[0].minor.yy394;}
167251 break;
167252 case 63: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */
167253 case 80: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==80);
167254 case 216: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==216);
167255 case 219: /* in_op ::= NOT IN */ yytestcase(yyruleno==219);
167256 case 245: /* collate ::= COLLATE ID|STRING */ yytestcase(yyruleno==245);
167257{yymsp[-1].minor.yy394 = 1;}
167258 break;
167259 case 64: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
167260{yymsp[-1].minor.yy394 = 0;}
167261 break;
167262 case 66: /* tconscomma ::= COMMA */
167263{pParse->constraintName.n = 0;}
167264 break;
167265 case 68: /* tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
167266{sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy322,yymsp[0].minor.yy394,yymsp[-2].minor.yy394,0);}
167267 break;
167268 case 69: /* tcons ::= UNIQUE LP sortlist RP onconf */
167269{sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy322,yymsp[0].minor.yy394,0,0,0,0,
167270 SQLITE_IDXTYPE_UNIQUE1);}
167271 break;
167272 case 70: /* tcons ::= CHECK LP expr RP onconf */
167273{sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy528,yymsp[-3].minor.yy0.z,yymsp[-1].minor.yy0.z);}
167274 break;
167275 case 71: /* tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
167276{
167277 sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy322, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy322, yymsp[-1].minor.yy394);
167278 sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy394);
167279}
167280 break;
167281 case 73: /* onconf ::= */
167282 case 75: /* orconf ::= */ yytestcase(yyruleno==75);
167283{yymsp[1].minor.yy394 = OE_Default11;}
167284 break;
167285 case 74: /* onconf ::= ON CONFLICT resolvetype */
167286{yymsp[-2].minor.yy394 = yymsp[0].minor.yy394;}
167287 break;
167288 case 77: /* resolvetype ::= IGNORE */
167289{yymsp[0].minor.yy394 = OE_Ignore4;}
167290 break;
167291 case 78: /* resolvetype ::= REPLACE */
167292 case 172: /* insert_cmd ::= REPLACE */ yytestcase(yyruleno==172);
167293{yymsp[0].minor.yy394 = OE_Replace5;}
167294 break;
167295 case 79: /* cmd ::= DROP TABLE ifexists fullname */
167296{
167297 sqlite3DropTable(pParse, yymsp[0].minor.yy131, 0, yymsp[-1].minor.yy394);
167298}
167299 break;
167300 case 82: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
167301{
167302 sqlite3CreateView(pParse, &yymsp[-8].minor.yy0, &yymsp[-4].minor.yy0, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy322, yymsp[0].minor.yy47, yymsp[-7].minor.yy394, yymsp[-5].minor.yy394);
167303}
167304 break;
167305 case 83: /* cmd ::= DROP VIEW ifexists fullname */
167306{
167307 sqlite3DropTable(pParse, yymsp[0].minor.yy131, 1, yymsp[-1].minor.yy394);
167308}
167309 break;
167310 case 84: /* cmd ::= select */
167311{
167312 SelectDest dest = {SRT_Output9, 0, 0, 0, 0, 0, 0};
167313 sqlite3Select(pParse, yymsp[0].minor.yy47, &dest);
167314 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy47);
167315}
167316 break;
167317 case 85: /* select ::= WITH wqlist selectnowith */
167318{yymsp[-2].minor.yy47 = attachWithToSelect(pParse,yymsp[0].minor.yy47,yymsp[-1].minor.yy521);}
167319 break;
167320 case 86: /* select ::= WITH RECURSIVE wqlist selectnowith */
167321{yymsp[-3].minor.yy47 = attachWithToSelect(pParse,yymsp[0].minor.yy47,yymsp[-1].minor.yy521);}
167322 break;
167323 case 87: /* select ::= selectnowith */
167324{
167325 Select *p = yymsp[0].minor.yy47;
167326 if( p ){
167327 parserDoubleLinkSelect(pParse, p);
167328 }
167329 yymsp[0].minor.yy47 = p; /*A-overwrites-X*/
167330}
167331 break;
167332 case 88: /* selectnowith ::= selectnowith multiselect_op oneselect */
167333{
167334 Select *pRhs = yymsp[0].minor.yy47;
167335 Select *pLhs = yymsp[-2].minor.yy47;
167336 if( pRhs && pRhs->pPrior ){
167337 SrcList *pFrom;
167338 Token x;
167339 x.n = 0;
167340 parserDoubleLinkSelect(pParse, pRhs);
167341 pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0);
167342 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
167343 }
167344 if( pRhs ){
167345 pRhs->op = (u8)yymsp[-1].minor.yy394;
167346 pRhs->pPrior = pLhs;
167347 if( ALWAYS(pLhs)(pLhs) ) pLhs->selFlags &= ~SF_MultiValue0x0000400;
167348 pRhs->selFlags &= ~SF_MultiValue0x0000400;
167349 if( yymsp[-1].minor.yy394!=TK_ALL135 ) pParse->hasCompound = 1;
167350 }else{
167351 sqlite3SelectDelete(pParse->db, pLhs);
167352 }
167353 yymsp[-2].minor.yy47 = pRhs;
167354}
167355 break;
167356 case 89: /* multiselect_op ::= UNION */
167357 case 91: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==91);
167358{yymsp[0].minor.yy394 = yymsp[0].major; /*A-overwrites-OP*/}
167359 break;
167360 case 90: /* multiselect_op ::= UNION ALL */
167361{yymsp[-1].minor.yy394 = TK_ALL135;}
167362 break;
167363 case 92: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
167364{
167365 yymsp[-8].minor.yy47 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy322,yymsp[-5].minor.yy131,yymsp[-4].minor.yy528,yymsp[-3].minor.yy322,yymsp[-2].minor.yy528,yymsp[-1].minor.yy322,yymsp[-7].minor.yy394,yymsp[0].minor.yy528);
167366}
167367 break;
167368 case 93: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
167369{
167370 yymsp[-9].minor.yy47 = sqlite3SelectNew(pParse,yymsp[-7].minor.yy322,yymsp[-6].minor.yy131,yymsp[-5].minor.yy528,yymsp[-4].minor.yy322,yymsp[-3].minor.yy528,yymsp[-1].minor.yy322,yymsp[-8].minor.yy394,yymsp[0].minor.yy528);
167371 if( yymsp[-9].minor.yy47 ){
167372 yymsp[-9].minor.yy47->pWinDefn = yymsp[-2].minor.yy41;
167373 }else{
167374 sqlite3WindowListDelete(pParse->db, yymsp[-2].minor.yy41);
167375 }
167376}
167377 break;
167378 case 94: /* values ::= VALUES LP nexprlist RP */
167379{
167380 yymsp[-3].minor.yy47 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy322,0,0,0,0,0,SF_Values0x0000200,0);
167381}
167382 break;
167383 case 95: /* values ::= values COMMA LP nexprlist RP */
167384{
167385 Select *pRight, *pLeft = yymsp[-4].minor.yy47;
167386 pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy322,0,0,0,0,0,SF_Values0x0000200|SF_MultiValue0x0000400,0);
167387 if( ALWAYS(pLeft)(pLeft) ) pLeft->selFlags &= ~SF_MultiValue0x0000400;
167388 if( pRight ){
167389 pRight->op = TK_ALL135;
167390 pRight->pPrior = pLeft;
167391 yymsp[-4].minor.yy47 = pRight;
167392 }else{
167393 yymsp[-4].minor.yy47 = pLeft;
167394 }
167395}
167396 break;
167397 case 96: /* distinct ::= DISTINCT */
167398{yymsp[0].minor.yy394 = SF_Distinct0x0000001;}
167399 break;
167400 case 97: /* distinct ::= ALL */
167401{yymsp[0].minor.yy394 = SF_All0x0000002;}
167402 break;
167403 case 99: /* sclp ::= */
167404 case 132: /* orderby_opt ::= */ yytestcase(yyruleno==132);
167405 case 142: /* groupby_opt ::= */ yytestcase(yyruleno==142);
167406 case 232: /* exprlist ::= */ yytestcase(yyruleno==232);
167407 case 235: /* paren_exprlist ::= */ yytestcase(yyruleno==235);
167408 case 240: /* eidlist_opt ::= */ yytestcase(yyruleno==240);
167409{yymsp[1].minor.yy322 = 0;}
167410 break;
167411 case 100: /* selcollist ::= sclp scanpt expr scanpt as */
167412{
167413 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy322, yymsp[-2].minor.yy528);
167414 if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy322, &yymsp[0].minor.yy0, 1);
167415 sqlite3ExprListSetSpan(pParse,yymsp[-4].minor.yy322,yymsp[-3].minor.yy522,yymsp[-1].minor.yy522);
167416}
167417 break;
167418 case 101: /* selcollist ::= sclp scanpt STAR */
167419{
167420 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK180, 0);
167421 yymsp[-2].minor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy322, p);
167422}
167423 break;
167424 case 102: /* selcollist ::= sclp scanpt nm DOT STAR */
167425{
167426 Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK180, 0, 0);
167427 Expr *pLeft = tokenExpr(pParse, TK_ID59, yymsp[-2].minor.yy0);
167428 Expr *pDot = sqlite3PExpr(pParse, TK_DOT141, pLeft, pRight);
167429 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, pDot);
167430}
167431 break;
167432 case 103: /* as ::= AS nm */
167433 case 115: /* dbnm ::= DOT nm */ yytestcase(yyruleno==115);
167434 case 256: /* plus_num ::= PLUS INTEGER|FLOAT */ yytestcase(yyruleno==256);
167435 case 257: /* minus_num ::= MINUS INTEGER|FLOAT */ yytestcase(yyruleno==257);
167436{yymsp[-1].minor.yy0 = yymsp[0].minor.yy0;}
167437 break;
167438 case 105: /* from ::= */
167439 case 108: /* stl_prefix ::= */ yytestcase(yyruleno==108);
167440{yymsp[1].minor.yy131 = 0;}
167441 break;
167442 case 106: /* from ::= FROM seltablist */
167443{
167444 yymsp[-1].minor.yy131 = yymsp[0].minor.yy131;
167445 sqlite3SrcListShiftJoinType(pParse,yymsp[-1].minor.yy131);
167446}
167447 break;
167448 case 107: /* stl_prefix ::= seltablist joinop */
167449{
167450 if( ALWAYS(yymsp[-1].minor.yy131 && yymsp[-1].minor.yy131->nSrc>0)(yymsp[-1].minor.yy131 && yymsp[-1].minor.yy131->nSrc
>0)
) yymsp[-1].minor.yy131->a[yymsp[-1].minor.yy131->nSrc-1].fg.jointype = (u8)yymsp[0].minor.yy394;
167451}
167452 break;
167453 case 109: /* seltablist ::= stl_prefix nm dbnm as on_using */
167454{
167455 yymsp[-4].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-4].minor.yy131,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0,&yymsp[0].minor.yy561);
167456}
167457 break;
167458 case 110: /* seltablist ::= stl_prefix nm dbnm as indexed_by on_using */
167459{
167460 yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy131,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,0,&yymsp[0].minor.yy561);
167461 sqlite3SrcListIndexedBy(pParse, yymsp[-5].minor.yy131, &yymsp[-1].minor.yy0);
167462}
167463 break;
167464 case 111: /* seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_using */
167465{
167466 yymsp[-7].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-7].minor.yy131,&yymsp[-6].minor.yy0,&yymsp[-5].minor.yy0,&yymsp[-1].minor.yy0,0,&yymsp[0].minor.yy561);
167467 sqlite3SrcListFuncArgs(pParse, yymsp[-7].minor.yy131, yymsp[-3].minor.yy322);
167468}
167469 break;
167470 case 112: /* seltablist ::= stl_prefix LP select RP as on_using */
167471{
167472 yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy131,0,0,&yymsp[-1].minor.yy0,yymsp[-3].minor.yy47,&yymsp[0].minor.yy561);
167473 }
167474 break;
167475 case 113: /* seltablist ::= stl_prefix LP seltablist RP as on_using */
167476{
167477 if( yymsp[-5].minor.yy131==0 && yymsp[-1].minor.yy0.n==0 && yymsp[0].minor.yy561.pOn==0 && yymsp[0].minor.yy561.pUsing==0 ){
167478 yymsp[-5].minor.yy131 = yymsp[-3].minor.yy131;
167479 }else if( yymsp[-3].minor.yy131->nSrc==1 ){
167480 yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy131,0,0,&yymsp[-1].minor.yy0,0,&yymsp[0].minor.yy561);
167481 if( yymsp[-5].minor.yy131 ){
167482 SrcItem *pNew = &yymsp[-5].minor.yy131->a[yymsp[-5].minor.yy131->nSrc-1];
167483 SrcItem *pOld = yymsp[-3].minor.yy131->a;
167484 pNew->zName = pOld->zName;
167485 pNew->zDatabase = pOld->zDatabase;
167486 pNew->pSelect = pOld->pSelect;
167487 if( pNew->pSelect && (pNew->pSelect->selFlags & SF_NestedFrom0x0000800)!=0 ){
167488 pNew->fg.isNestedFrom = 1;
167489 }
167490 if( pOld->fg.isTabFunc ){
167491 pNew->u1.pFuncArg = pOld->u1.pFuncArg;
167492 pOld->u1.pFuncArg = 0;
167493 pOld->fg.isTabFunc = 0;
167494 pNew->fg.isTabFunc = 1;
167495 }
167496 pOld->zName = pOld->zDatabase = 0;
167497 pOld->pSelect = 0;
167498 }
167499 sqlite3SrcListDelete(pParse->db, yymsp[-3].minor.yy131);
167500 }else{
167501 Select *pSubquery;
167502 sqlite3SrcListShiftJoinType(pParse,yymsp[-3].minor.yy131);
167503 pSubquery = sqlite3SelectNew(pParse,0,yymsp[-3].minor.yy131,0,0,0,0,SF_NestedFrom0x0000800,0);
167504 yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy131,0,0,&yymsp[-1].minor.yy0,pSubquery,&yymsp[0].minor.yy561);
167505 }
167506 }
167507 break;
167508 case 114: /* dbnm ::= */
167509 case 129: /* indexed_opt ::= */ yytestcase(yyruleno==129);
167510{yymsp[1].minor.yy0.z=0; yymsp[1].minor.yy0.n=0;}
167511 break;
167512 case 116: /* fullname ::= nm */
167513{
167514 yylhsminor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[0].minor.yy0,0);
167515 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && yylhsminor.yy131 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy131->a[0].zName, &yymsp[0].minor.yy0);
167516}
167517 yymsp[0].minor.yy131 = yylhsminor.yy131;
167518 break;
167519 case 117: /* fullname ::= nm DOT nm */
167520{
167521 yylhsminor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
167522 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && yylhsminor.yy131 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy131->a[0].zName, &yymsp[0].minor.yy0);
167523}
167524 yymsp[-2].minor.yy131 = yylhsminor.yy131;
167525 break;
167526 case 118: /* xfullname ::= nm */
167527{yymsp[0].minor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[0].minor.yy0,0); /*A-overwrites-X*/}
167528 break;
167529 case 119: /* xfullname ::= nm DOT nm */
167530{yymsp[-2].minor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-X*/}
167531 break;
167532 case 120: /* xfullname ::= nm DOT nm AS nm */
167533{
167534 yymsp[-4].minor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,&yymsp[-2].minor.yy0); /*A-overwrites-X*/
167535 if( yymsp[-4].minor.yy131 ) yymsp[-4].minor.yy131->a[0].zAlias = sqlite3NameFromToken(pParse->db, &yymsp[0].minor.yy0);
167536}
167537 break;
167538 case 121: /* xfullname ::= nm AS nm */
167539{
167540 yymsp[-2].minor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,0); /*A-overwrites-X*/
167541 if( yymsp[-2].minor.yy131 ) yymsp[-2].minor.yy131->a[0].zAlias = sqlite3NameFromToken(pParse->db, &yymsp[0].minor.yy0);
167542}
167543 break;
167544 case 122: /* joinop ::= COMMA|JOIN */
167545{ yymsp[0].minor.yy394 = JT_INNER0x01; }
167546 break;
167547 case 123: /* joinop ::= JOIN_KW JOIN */
167548{yymsp[-1].minor.yy394 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); /*X-overwrites-A*/}
167549 break;
167550 case 124: /* joinop ::= JOIN_KW nm JOIN */
167551{yymsp[-2].minor.yy394 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); /*X-overwrites-A*/}
167552 break;
167553 case 125: /* joinop ::= JOIN_KW nm nm JOIN */
167554{yymsp[-3].minor.yy394 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);/*X-overwrites-A*/}
167555 break;
167556 case 126: /* on_using ::= ON expr */
167557{yymsp[-1].minor.yy561.pOn = yymsp[0].minor.yy528; yymsp[-1].minor.yy561.pUsing = 0;}
167558 break;
167559 case 127: /* on_using ::= USING LP idlist RP */
167560{yymsp[-3].minor.yy561.pOn = 0; yymsp[-3].minor.yy561.pUsing = yymsp[-1].minor.yy254;}
167561 break;
167562 case 128: /* on_using ::= */
167563{yymsp[1].minor.yy561.pOn = 0; yymsp[1].minor.yy561.pUsing = 0;}
167564 break;
167565 case 130: /* indexed_by ::= INDEXED BY nm */
167566{yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;}
167567 break;
167568 case 131: /* indexed_by ::= NOT INDEXED */
167569{yymsp[-1].minor.yy0.z=0; yymsp[-1].minor.yy0.n=1;}
167570 break;
167571 case 133: /* orderby_opt ::= ORDER BY sortlist */
167572 case 143: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==143);
167573{yymsp[-2].minor.yy322 = yymsp[0].minor.yy322;}
167574 break;
167575 case 134: /* sortlist ::= sortlist COMMA expr sortorder nulls */
167576{
167577 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322,yymsp[-2].minor.yy528);
167578 sqlite3ExprListSetSortOrder(yymsp[-4].minor.yy322,yymsp[-1].minor.yy394,yymsp[0].minor.yy394);
167579}
167580 break;
167581 case 135: /* sortlist ::= expr sortorder nulls */
167582{
167583 yymsp[-2].minor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[-2].minor.yy528); /*A-overwrites-Y*/
167584 sqlite3ExprListSetSortOrder(yymsp[-2].minor.yy322,yymsp[-1].minor.yy394,yymsp[0].minor.yy394);
167585}
167586 break;
167587 case 136: /* sortorder ::= ASC */
167588{yymsp[0].minor.yy394 = SQLITE_SO_ASC0;}
167589 break;
167590 case 137: /* sortorder ::= DESC */
167591{yymsp[0].minor.yy394 = SQLITE_SO_DESC1;}
167592 break;
167593 case 138: /* sortorder ::= */
167594 case 141: /* nulls ::= */ yytestcase(yyruleno==141);
167595{yymsp[1].minor.yy394 = SQLITE_SO_UNDEFINED-1;}
167596 break;
167597 case 139: /* nulls ::= NULLS FIRST */
167598{yymsp[-1].minor.yy394 = SQLITE_SO_ASC0;}
167599 break;
167600 case 140: /* nulls ::= NULLS LAST */
167601{yymsp[-1].minor.yy394 = SQLITE_SO_DESC1;}
167602 break;
167603 case 144: /* having_opt ::= */
167604 case 146: /* limit_opt ::= */ yytestcase(yyruleno==146);
167605 case 151: /* where_opt ::= */ yytestcase(yyruleno==151);
167606 case 153: /* where_opt_ret ::= */ yytestcase(yyruleno==153);
167607 case 229: /* case_else ::= */ yytestcase(yyruleno==229);
167608 case 231: /* case_operand ::= */ yytestcase(yyruleno==231);
167609 case 250: /* vinto ::= */ yytestcase(yyruleno==250);
167610{yymsp[1].minor.yy528 = 0;}
167611 break;
167612 case 145: /* having_opt ::= HAVING expr */
167613 case 152: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==152);
167614 case 154: /* where_opt_ret ::= WHERE expr */ yytestcase(yyruleno==154);
167615 case 228: /* case_else ::= ELSE expr */ yytestcase(yyruleno==228);
167616 case 249: /* vinto ::= INTO expr */ yytestcase(yyruleno==249);
167617{yymsp[-1].minor.yy528 = yymsp[0].minor.yy528;}
167618 break;
167619 case 147: /* limit_opt ::= LIMIT expr */
167620{yymsp[-1].minor.yy528 = sqlite3PExpr(pParse,TK_LIMIT148,yymsp[0].minor.yy528,0);}
167621 break;
167622 case 148: /* limit_opt ::= LIMIT expr OFFSET expr */
167623{yymsp[-3].minor.yy528 = sqlite3PExpr(pParse,TK_LIMIT148,yymsp[-2].minor.yy528,yymsp[0].minor.yy528);}
167624 break;
167625 case 149: /* limit_opt ::= LIMIT expr COMMA expr */
167626{yymsp[-3].minor.yy528 = sqlite3PExpr(pParse,TK_LIMIT148,yymsp[0].minor.yy528,yymsp[-2].minor.yy528);}
167627 break;
167628 case 150: /* cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret */
167629{
167630 sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy131, &yymsp[-1].minor.yy0);
167631 sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy131,yymsp[0].minor.yy528,0,0);
167632}
167633 break;
167634 case 155: /* where_opt_ret ::= RETURNING selcollist */
167635{sqlite3AddReturning(pParse,yymsp[0].minor.yy322); yymsp[-1].minor.yy528 = 0;}
167636 break;
167637 case 156: /* where_opt_ret ::= WHERE expr RETURNING selcollist */
167638{sqlite3AddReturning(pParse,yymsp[0].minor.yy322); yymsp[-3].minor.yy528 = yymsp[-2].minor.yy528;}
167639 break;
167640 case 157: /* cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */
167641{
167642 sqlite3SrcListIndexedBy(pParse, yymsp[-5].minor.yy131, &yymsp[-4].minor.yy0);
167643 sqlite3ExprListCheckLength(pParse,yymsp[-2].minor.yy322,"set list");
167644 if( yymsp[-1].minor.yy131 ){
167645 SrcList *pFromClause = yymsp[-1].minor.yy131;
167646 if( pFromClause->nSrc>1 ){
167647 Select *pSubquery;
167648 Token as;
167649 pSubquery = sqlite3SelectNew(pParse,0,pFromClause,0,0,0,0,SF_NestedFrom0x0000800,0);
167650 as.n = 0;
167651 as.z = 0;
167652 pFromClause = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0);
167653 }
167654 yymsp[-5].minor.yy131 = sqlite3SrcListAppendList(pParse, yymsp[-5].minor.yy131, pFromClause);
167655 }
167656 sqlite3Update(pParse,yymsp[-5].minor.yy131,yymsp[-2].minor.yy322,yymsp[0].minor.yy528,yymsp[-6].minor.yy394,0,0,0);
167657}
167658 break;
167659 case 158: /* setlist ::= setlist COMMA nm EQ expr */
167660{
167661 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy322, yymsp[0].minor.yy528);
167662 sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy322, &yymsp[-2].minor.yy0, 1);
167663}
167664 break;
167665 case 159: /* setlist ::= setlist COMMA LP idlist RP EQ expr */
167666{
167667 yymsp[-6].minor.yy322 = sqlite3ExprListAppendVector(pParse, yymsp[-6].minor.yy322, yymsp[-3].minor.yy254, yymsp[0].minor.yy528);
167668}
167669 break;
167670 case 160: /* setlist ::= nm EQ expr */
167671{
167672 yylhsminor.yy322 = sqlite3ExprListAppend(pParse, 0, yymsp[0].minor.yy528);
167673 sqlite3ExprListSetName(pParse, yylhsminor.yy322, &yymsp[-2].minor.yy0, 1);
167674}
167675 yymsp[-2].minor.yy322 = yylhsminor.yy322;
167676 break;
167677 case 161: /* setlist ::= LP idlist RP EQ expr */
167678{
167679 yymsp[-4].minor.yy322 = sqlite3ExprListAppendVector(pParse, 0, yymsp[-3].minor.yy254, yymsp[0].minor.yy528);
167680}
167681 break;
167682 case 162: /* cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
167683{
167684 sqlite3Insert(pParse, yymsp[-3].minor.yy131, yymsp[-1].minor.yy47, yymsp[-2].minor.yy254, yymsp[-5].minor.yy394, yymsp[0].minor.yy444);
167685}
167686 break;
167687 case 163: /* cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning */
167688{
167689 sqlite3Insert(pParse, yymsp[-4].minor.yy131, 0, yymsp[-3].minor.yy254, yymsp[-6].minor.yy394, 0);
167690}
167691 break;
167692 case 164: /* upsert ::= */
167693{ yymsp[1].minor.yy444 = 0; }
167694 break;
167695 case 165: /* upsert ::= RETURNING selcollist */
167696{ yymsp[-1].minor.yy444 = 0; sqlite3AddReturning(pParse,yymsp[0].minor.yy322); }
167697 break;
167698 case 166: /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt upsert */
167699{ yymsp[-11].minor.yy444 = sqlite3UpsertNew(pParse->db,yymsp[-8].minor.yy322,yymsp[-6].minor.yy528,yymsp[-2].minor.yy322,yymsp[-1].minor.yy528,yymsp[0].minor.yy444);}
167700 break;
167701 case 167: /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert */
167702{ yymsp[-8].minor.yy444 = sqlite3UpsertNew(pParse->db,yymsp[-5].minor.yy322,yymsp[-3].minor.yy528,0,0,yymsp[0].minor.yy444); }
167703 break;
167704 case 168: /* upsert ::= ON CONFLICT DO NOTHING returning */
167705{ yymsp[-4].minor.yy444 = sqlite3UpsertNew(pParse->db,0,0,0,0,0); }
167706 break;
167707 case 169: /* upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning */
167708{ yymsp[-7].minor.yy444 = sqlite3UpsertNew(pParse->db,0,0,yymsp[-2].minor.yy322,yymsp[-1].minor.yy528,0);}
167709 break;
167710 case 170: /* returning ::= RETURNING selcollist */
167711{sqlite3AddReturning(pParse,yymsp[0].minor.yy322);}
167712 break;
167713 case 173: /* idlist_opt ::= */
167714{yymsp[1].minor.yy254 = 0;}
167715 break;
167716 case 174: /* idlist_opt ::= LP idlist RP */
167717{yymsp[-2].minor.yy254 = yymsp[-1].minor.yy254;}
167718 break;
167719 case 175: /* idlist ::= idlist COMMA nm */
167720{yymsp[-2].minor.yy254 = sqlite3IdListAppend(pParse,yymsp[-2].minor.yy254,&yymsp[0].minor.yy0);}
167721 break;
167722 case 176: /* idlist ::= nm */
167723{yymsp[0].minor.yy254 = sqlite3IdListAppend(pParse,0,&yymsp[0].minor.yy0); /*A-overwrites-Y*/}
167724 break;
167725 case 177: /* expr ::= LP expr RP */
167726{yymsp[-2].minor.yy528 = yymsp[-1].minor.yy528;}
167727 break;
167728 case 178: /* expr ::= ID|INDEXED */
167729 case 179: /* expr ::= JOIN_KW */ yytestcase(yyruleno==179);
167730{yymsp[0].minor.yy528=tokenExpr(pParse,TK_ID59,yymsp[0].minor.yy0); /*A-overwrites-X*/}
167731 break;
167732 case 180: /* expr ::= nm DOT nm */
167733{
167734 Expr *temp1 = tokenExpr(pParse,TK_ID59,yymsp[-2].minor.yy0);
167735 Expr *temp2 = tokenExpr(pParse,TK_ID59,yymsp[0].minor.yy0);
167736 yylhsminor.yy528 = sqlite3PExpr(pParse, TK_DOT141, temp1, temp2);
167737}
167738 yymsp[-2].minor.yy528 = yylhsminor.yy528;
167739 break;
167740 case 181: /* expr ::= nm DOT nm DOT nm */
167741{
167742 Expr *temp1 = tokenExpr(pParse,TK_ID59,yymsp[-4].minor.yy0);
167743 Expr *temp2 = tokenExpr(pParse,TK_ID59,yymsp[-2].minor.yy0);
167744 Expr *temp3 = tokenExpr(pParse,TK_ID59,yymsp[0].minor.yy0);
167745 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT141, temp2, temp3);
167746 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
167747 sqlite3RenameTokenRemap(pParse, 0, temp1);
167748 }
167749 yylhsminor.yy528 = sqlite3PExpr(pParse, TK_DOT141, temp1, temp4);
167750}
167751 yymsp[-4].minor.yy528 = yylhsminor.yy528;
167752 break;
167753 case 182: /* term ::= NULL|FLOAT|BLOB */
167754 case 183: /* term ::= STRING */ yytestcase(yyruleno==183);
167755{yymsp[0].minor.yy528=tokenExpr(pParse,yymsp[0].major,yymsp[0].minor.yy0); /*A-overwrites-X*/}
167756 break;
167757 case 184: /* term ::= INTEGER */
167758{
167759 yylhsminor.yy528 = sqlite3ExprAlloc(pParse->db, TK_INTEGER155, &yymsp[0].minor.yy0, 1);
167760 if( yylhsminor.yy528 ) yylhsminor.yy528->w.iOfst = (int)(yymsp[0].minor.yy0.z - pParse->zTail);
167761}
167762 yymsp[0].minor.yy528 = yylhsminor.yy528;
167763 break;
167764 case 185: /* expr ::= VARIABLE */
167765{
167766 if( !(yymsp[0].minor.yy0.z[0]=='#' && sqlite3Isdigit(yymsp[0].minor.yy0.z[1])(sqlite3CtypeMap[(unsigned char)(yymsp[0].minor.yy0.z[1])]&
0x04)
) ){
167767 u32 n = yymsp[0].minor.yy0.n;
167768 yymsp[0].minor.yy528 = tokenExpr(pParse, TK_VARIABLE156, yymsp[0].minor.yy0);
167769 sqlite3ExprAssignVarNumber(pParse, yymsp[0].minor.yy528, n);
167770 }else{
167771 /* When doing a nested parse, one can include terms in an expression
167772 ** that look like this: #1 #2 ... These terms refer to registers
167773 ** in the virtual machine. #N is the N-th register. */
167774 Token t = yymsp[0].minor.yy0; /*A-overwrites-X*/
167775 assert( t.n>=2 )((void) (0));
167776 if( pParse->nested==0 ){
167777 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
167778 yymsp[0].minor.yy528 = 0;
167779 }else{
167780 yymsp[0].minor.yy528 = sqlite3PExpr(pParse, TK_REGISTER176, 0, 0);
167781 if( yymsp[0].minor.yy528 ) sqlite3GetInt32(&t.z[1], &yymsp[0].minor.yy528->iTable);
167782 }
167783 }
167784}
167785 break;
167786 case 186: /* expr ::= expr COLLATE ID|STRING */
167787{
167788 yymsp[-2].minor.yy528 = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy528, &yymsp[0].minor.yy0, 1);
167789}
167790 break;
167791 case 187: /* expr ::= CAST LP expr AS typetoken RP */
167792{
167793 yymsp[-5].minor.yy528 = sqlite3ExprAlloc(pParse->db, TK_CAST36, &yymsp[-1].minor.yy0, 1);
167794 sqlite3ExprAttachSubtrees(pParse->db, yymsp[-5].minor.yy528, yymsp[-3].minor.yy528, 0);
167795}
167796 break;
167797 case 188: /* expr ::= ID|INDEXED LP distinct exprlist RP */
167798{
167799 yylhsminor.yy528 = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy322, &yymsp[-4].minor.yy0, yymsp[-2].minor.yy394);
167800}
167801 yymsp[-4].minor.yy528 = yylhsminor.yy528;
167802 break;
167803 case 189: /* expr ::= ID|INDEXED LP STAR RP */
167804{
167805 yylhsminor.yy528 = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0, 0);
167806}
167807 yymsp[-3].minor.yy528 = yylhsminor.yy528;
167808 break;
167809 case 190: /* expr ::= ID|INDEXED LP distinct exprlist RP filter_over */
167810{
167811 yylhsminor.yy528 = sqlite3ExprFunction(pParse, yymsp[-2].minor.yy322, &yymsp[-5].minor.yy0, yymsp[-3].minor.yy394);
167812 sqlite3WindowAttach(pParse, yylhsminor.yy528, yymsp[0].minor.yy41);
167813}
167814 yymsp[-5].minor.yy528 = yylhsminor.yy528;
167815 break;
167816 case 191: /* expr ::= ID|INDEXED LP STAR RP filter_over */
167817{
167818 yylhsminor.yy528 = sqlite3ExprFunction(pParse, 0, &yymsp[-4].minor.yy0, 0);
167819 sqlite3WindowAttach(pParse, yylhsminor.yy528, yymsp[0].minor.yy41);
167820}
167821 yymsp[-4].minor.yy528 = yylhsminor.yy528;
167822 break;
167823 case 192: /* term ::= CTIME_KW */
167824{
167825 yylhsminor.yy528 = sqlite3ExprFunction(pParse, 0, &yymsp[0].minor.yy0, 0);
167826}
167827 yymsp[0].minor.yy528 = yylhsminor.yy528;
167828 break;
167829 case 193: /* expr ::= LP nexprlist COMMA expr RP */
167830{
167831 ExprList *pList = sqlite3ExprListAppend(pParse, yymsp[-3].minor.yy322, yymsp[-1].minor.yy528);
167832 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_VECTOR177, 0, 0);
167833 if( yymsp[-4].minor.yy528 ){
167834 yymsp[-4].minor.yy528->x.pList = pList;
167835 if( ALWAYS(pList->nExpr)(pList->nExpr) ){
167836 yymsp[-4].minor.yy528->flags |= pList->a[0].pExpr->flags & EP_Propagate(0x000200|0x400000|0x000008);
167837 }
167838 }else{
167839 sqlite3ExprListDelete(pParse->db, pList);
167840 }
167841}
167842 break;
167843 case 194: /* expr ::= expr AND expr */
167844{yymsp[-2].minor.yy528=sqlite3ExprAnd(pParse,yymsp[-2].minor.yy528,yymsp[0].minor.yy528);}
167845 break;
167846 case 195: /* expr ::= expr OR expr */
167847 case 196: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==196);
167848 case 197: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==197);
167849 case 198: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==198);
167850 case 199: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==199);
167851 case 200: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==200);
167852 case 201: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==201);
167853{yymsp[-2].minor.yy528=sqlite3PExpr(pParse,yymsp[-1].major,yymsp[-2].minor.yy528,yymsp[0].minor.yy528);}
167854 break;
167855 case 202: /* likeop ::= NOT LIKE_KW|MATCH */
167856{yymsp[-1].minor.yy0=yymsp[0].minor.yy0; yymsp[-1].minor.yy0.n|=0x80000000; /*yymsp[-1].minor.yy0-overwrite-yymsp[0].minor.yy0*/}
167857 break;
167858 case 203: /* expr ::= expr likeop expr */
167859{
167860 ExprList *pList;
167861 int bNot = yymsp[-1].minor.yy0.n & 0x80000000;
167862 yymsp[-1].minor.yy0.n &= 0x7fffffff;
167863 pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy528);
167864 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy528);
167865 yymsp[-2].minor.yy528 = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy0, 0);
167866 if( bNot ) yymsp[-2].minor.yy528 = sqlite3PExpr(pParse, TK_NOT19, yymsp[-2].minor.yy528, 0);
167867 if( yymsp[-2].minor.yy528 ) yymsp[-2].minor.yy528->flags |= EP_InfixFunc0x000100;
167868}
167869 break;
167870 case 204: /* expr ::= expr likeop expr ESCAPE expr */
167871{
167872 ExprList *pList;
167873 int bNot = yymsp[-3].minor.yy0.n & 0x80000000;
167874 yymsp[-3].minor.yy0.n &= 0x7fffffff;
167875 pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy528);
167876 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy528);
167877 pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy528);
167878 yymsp[-4].minor.yy528 = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy0, 0);
167879 if( bNot ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT19, yymsp[-4].minor.yy528, 0);
167880 if( yymsp[-4].minor.yy528 ) yymsp[-4].minor.yy528->flags |= EP_InfixFunc0x000100;
167881}
167882 break;
167883 case 205: /* expr ::= expr ISNULL|NOTNULL */
167884{yymsp[-1].minor.yy528 = sqlite3PExpr(pParse,yymsp[0].major,yymsp[-1].minor.yy528,0);}
167885 break;
167886 case 206: /* expr ::= expr NOT NULL */
167887{yymsp[-2].minor.yy528 = sqlite3PExpr(pParse,TK_NOTNULL51,yymsp[-2].minor.yy528,0);}
167888 break;
167889 case 207: /* expr ::= expr IS expr */
167890{
167891 yymsp[-2].minor.yy528 = sqlite3PExpr(pParse,TK_IS45,yymsp[-2].minor.yy528,yymsp[0].minor.yy528);
167892 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy528, yymsp[-2].minor.yy528, TK_ISNULL50);
167893}
167894 break;
167895 case 208: /* expr ::= expr IS NOT expr */
167896{
167897 yymsp[-3].minor.yy528 = sqlite3PExpr(pParse,TK_ISNOT171,yymsp[-3].minor.yy528,yymsp[0].minor.yy528);
167898 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy528, yymsp[-3].minor.yy528, TK_NOTNULL51);
167899}
167900 break;
167901 case 209: /* expr ::= expr IS NOT DISTINCT FROM expr */
167902{
167903 yymsp[-5].minor.yy528 = sqlite3PExpr(pParse,TK_IS45,yymsp[-5].minor.yy528,yymsp[0].minor.yy528);
167904 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy528, yymsp[-5].minor.yy528, TK_ISNULL50);
167905}
167906 break;
167907 case 210: /* expr ::= expr IS DISTINCT FROM expr */
167908{
167909 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse,TK_ISNOT171,yymsp[-4].minor.yy528,yymsp[0].minor.yy528);
167910 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy528, yymsp[-4].minor.yy528, TK_NOTNULL51);
167911}
167912 break;
167913 case 211: /* expr ::= NOT expr */
167914 case 212: /* expr ::= BITNOT expr */ yytestcase(yyruleno==212);
167915{yymsp[-1].minor.yy528 = sqlite3PExpr(pParse, yymsp[-1].major, yymsp[0].minor.yy528, 0);/*A-overwrites-B*/}
167916 break;
167917 case 213: /* expr ::= PLUS|MINUS expr */
167918{
167919 yymsp[-1].minor.yy528 = sqlite3PExpr(pParse, yymsp[-1].major==TK_PLUS106 ? TK_UPLUS174 : TK_UMINUS173, yymsp[0].minor.yy528, 0);
167920 /*A-overwrites-B*/
167921}
167922 break;
167923 case 214: /* expr ::= expr PTR expr */
167924{
167925 ExprList *pList = sqlite3ExprListAppend(pParse, 0, yymsp[-2].minor.yy528);
167926 pList = sqlite3ExprListAppend(pParse, pList, yymsp[0].minor.yy528);
167927 yylhsminor.yy528 = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy0, 0);
167928}
167929 yymsp[-2].minor.yy528 = yylhsminor.yy528;
167930 break;
167931 case 215: /* between_op ::= BETWEEN */
167932 case 218: /* in_op ::= IN */ yytestcase(yyruleno==218);
167933{yymsp[0].minor.yy394 = 0;}
167934 break;
167935 case 217: /* expr ::= expr between_op expr AND expr */
167936{
167937 ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy528);
167938 pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy528);
167939 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_BETWEEN48, yymsp[-4].minor.yy528, 0);
167940 if( yymsp[-4].minor.yy528 ){
167941 yymsp[-4].minor.yy528->x.pList = pList;
167942 }else{
167943 sqlite3ExprListDelete(pParse->db, pList);
167944 }
167945 if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT19, yymsp[-4].minor.yy528, 0);
167946}
167947 break;
167948 case 220: /* expr ::= expr in_op LP exprlist RP */
167949{
167950 if( yymsp[-1].minor.yy322==0 ){
167951 /* Expressions of the form
167952 **
167953 ** expr1 IN ()
167954 ** expr1 NOT IN ()
167955 **
167956 ** simplify to constants 0 (false) and 1 (true), respectively,
167957 ** regardless of the value of expr1.
167958 */
167959 sqlite3ExprUnmapAndDelete(pParse, yymsp[-4].minor.yy528);
167960 yymsp[-4].minor.yy528 = sqlite3Expr(pParse->db, TK_STRING117, yymsp[-3].minor.yy394 ? "true" : "false");
167961 if( yymsp[-4].minor.yy528 ) sqlite3ExprIdToTrueFalse(yymsp[-4].minor.yy528);
167962 }else{
167963 Expr *pRHS = yymsp[-1].minor.yy322->a[0].pExpr;
167964 if( yymsp[-1].minor.yy322->nExpr==1 && sqlite3ExprIsConstant(pRHS) && yymsp[-4].minor.yy528->op!=TK_VECTOR177 ){
167965 yymsp[-1].minor.yy322->a[0].pExpr = 0;
167966 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy322);
167967 pRHS = sqlite3PExpr(pParse, TK_UPLUS174, pRHS, 0);
167968 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_EQ53, yymsp[-4].minor.yy528, pRHS);
167969 }else{
167970 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_IN49, yymsp[-4].minor.yy528, 0);
167971 if( yymsp[-4].minor.yy528==0 ){
167972 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy322);
167973 }else if( yymsp[-4].minor.yy528->pLeft->op==TK_VECTOR177 ){
167974 int nExpr = yymsp[-4].minor.yy528->pLeft->x.pList->nExpr;
167975 Select *pSelectRHS = sqlite3ExprListToValues(pParse, nExpr, yymsp[-1].minor.yy322);
167976 if( pSelectRHS ){
167977 parserDoubleLinkSelect(pParse, pSelectRHS);
167978 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy528, pSelectRHS);
167979 }
167980 }else{
167981 yymsp[-4].minor.yy528->x.pList = yymsp[-1].minor.yy322;
167982 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy528);
167983 }
167984 }
167985 if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT19, yymsp[-4].minor.yy528, 0);
167986 }
167987 }
167988 break;
167989 case 221: /* expr ::= LP select RP */
167990{
167991 yymsp[-2].minor.yy528 = sqlite3PExpr(pParse, TK_SELECT138, 0, 0);
167992 sqlite3PExprAddSelect(pParse, yymsp[-2].minor.yy528, yymsp[-1].minor.yy47);
167993 }
167994 break;
167995 case 222: /* expr ::= expr in_op LP select RP */
167996{
167997 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_IN49, yymsp[-4].minor.yy528, 0);
167998 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy528, yymsp[-1].minor.yy47);
167999 if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT19, yymsp[-4].minor.yy528, 0);
168000 }
168001 break;
168002 case 223: /* expr ::= expr in_op nm dbnm paren_exprlist */
168003{
168004 SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);
168005 Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
168006 if( yymsp[0].minor.yy322 ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, yymsp[0].minor.yy322);
168007 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_IN49, yymsp[-4].minor.yy528, 0);
168008 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy528, pSelect);
168009 if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT19, yymsp[-4].minor.yy528, 0);
168010 }
168011 break;
168012 case 224: /* expr ::= EXISTS LP select RP */
168013{
168014 Expr *p;
168015 p = yymsp[-3].minor.yy528 = sqlite3PExpr(pParse, TK_EXISTS20, 0, 0);
168016 sqlite3PExprAddSelect(pParse, p, yymsp[-1].minor.yy47);
168017 }
168018 break;
168019 case 225: /* expr ::= CASE case_operand case_exprlist case_else END */
168020{
168021 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_CASE157, yymsp[-3].minor.yy528, 0);
168022 if( yymsp[-4].minor.yy528 ){
168023 yymsp[-4].minor.yy528->x.pList = yymsp[-1].minor.yy528 ? sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy322,yymsp[-1].minor.yy528) : yymsp[-2].minor.yy322;
168024 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy528);
168025 }else{
168026 sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy322);
168027 sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy528);
168028 }
168029}
168030 break;
168031 case 226: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
168032{
168033 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, yymsp[-2].minor.yy528);
168034 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, yymsp[0].minor.yy528);
168035}
168036 break;
168037 case 227: /* case_exprlist ::= WHEN expr THEN expr */
168038{
168039 yymsp[-3].minor.yy322 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy528);
168040 yymsp[-3].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy322, yymsp[0].minor.yy528);
168041}
168042 break;
168043 case 230: /* case_operand ::= expr */
168044{yymsp[0].minor.yy528 = yymsp[0].minor.yy528; /*A-overwrites-X*/}
168045 break;
168046 case 233: /* nexprlist ::= nexprlist COMMA expr */
168047{yymsp[-2].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy322,yymsp[0].minor.yy528);}
168048 break;
168049 case 234: /* nexprlist ::= expr */
168050{yymsp[0].minor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy528); /*A-overwrites-Y*/}
168051 break;
168052 case 236: /* paren_exprlist ::= LP exprlist RP */
168053 case 241: /* eidlist_opt ::= LP eidlist RP */ yytestcase(yyruleno==241);
168054{yymsp[-2].minor.yy322 = yymsp[-1].minor.yy322;}
168055 break;
168056 case 237: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
168057{
168058 sqlite3CreateIndex(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0,
168059 sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,0), yymsp[-2].minor.yy322, yymsp[-10].minor.yy394,
168060 &yymsp[-11].minor.yy0, yymsp[0].minor.yy528, SQLITE_SO_ASC0, yymsp[-8].minor.yy394, SQLITE_IDXTYPE_APPDEF0);
168061 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && pParse->pNewIndex ){
168062 sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &yymsp[-4].minor.yy0);
168063 }
168064}
168065 break;
168066 case 238: /* uniqueflag ::= UNIQUE */
168067 case 280: /* raisetype ::= ABORT */ yytestcase(yyruleno==280);
168068{yymsp[0].minor.yy394 = OE_Abort2;}
168069 break;
168070 case 239: /* uniqueflag ::= */
168071{yymsp[1].minor.yy394 = OE_None0;}
168072 break;
168073 case 242: /* eidlist ::= eidlist COMMA nm collate sortorder */
168074{
168075 yymsp[-4].minor.yy322 = parserAddExprIdListTerm(pParse, yymsp[-4].minor.yy322, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy394, yymsp[0].minor.yy394);
168076}
168077 break;
168078 case 243: /* eidlist ::= nm collate sortorder */
168079{
168080 yymsp[-2].minor.yy322 = parserAddExprIdListTerm(pParse, 0, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy394, yymsp[0].minor.yy394); /*A-overwrites-Y*/
168081}
168082 break;
168083 case 246: /* cmd ::= DROP INDEX ifexists fullname */
168084{sqlite3DropIndex(pParse, yymsp[0].minor.yy131, yymsp[-1].minor.yy394);}
168085 break;
168086 case 247: /* cmd ::= VACUUM vinto */
168087{sqlite3Vacuum(pParse,0,yymsp[0].minor.yy528);}
168088 break;
168089 case 248: /* cmd ::= VACUUM nm vinto */
168090{sqlite3Vacuum(pParse,&yymsp[-1].minor.yy0,yymsp[0].minor.yy528);}
168091 break;
168092 case 251: /* cmd ::= PRAGMA nm dbnm */
168093{sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
168094 break;
168095 case 252: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
168096{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
168097 break;
168098 case 253: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
168099{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
168100 break;
168101 case 254: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
168102{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
168103 break;
168104 case 255: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
168105{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
168106 break;
168107 case 258: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
168108{
168109 Token all;
168110 all.z = yymsp[-3].minor.yy0.z;
168111 all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
168112 sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy33, &all);
168113}
168114 break;
168115 case 259: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
168116{
168117 sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy394, yymsp[-4].minor.yy180.a, yymsp[-4].minor.yy180.b, yymsp[-2].minor.yy131, yymsp[0].minor.yy528, yymsp[-10].minor.yy394, yymsp[-8].minor.yy394);
168118 yymsp[-10].minor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0); /*A-overwrites-T*/
168119}
168120 break;
168121 case 260: /* trigger_time ::= BEFORE|AFTER */
168122{ yymsp[0].minor.yy394 = yymsp[0].major; /*A-overwrites-X*/ }
168123 break;
168124 case 261: /* trigger_time ::= INSTEAD OF */
168125{ yymsp[-1].minor.yy394 = TK_INSTEAD65;}
168126 break;
168127 case 262: /* trigger_time ::= */
168128{ yymsp[1].minor.yy394 = TK_BEFORE33; }
168129 break;
168130 case 263: /* trigger_event ::= DELETE|INSERT */
168131 case 264: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==264);
168132{yymsp[0].minor.yy180.a = yymsp[0].major; /*A-overwrites-X*/ yymsp[0].minor.yy180.b = 0;}
168133 break;
168134 case 265: /* trigger_event ::= UPDATE OF idlist */
168135{yymsp[-2].minor.yy180.a = TK_UPDATE129; yymsp[-2].minor.yy180.b = yymsp[0].minor.yy254;}
168136 break;
168137 case 266: /* when_clause ::= */
168138 case 285: /* key_opt ::= */ yytestcase(yyruleno==285);
168139{ yymsp[1].minor.yy528 = 0; }
168140 break;
168141 case 267: /* when_clause ::= WHEN expr */
168142 case 286: /* key_opt ::= KEY expr */ yytestcase(yyruleno==286);
168143{ yymsp[-1].minor.yy528 = yymsp[0].minor.yy528; }
168144 break;
168145 case 268: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
168146{
168147 assert( yymsp[-2].minor.yy33!=0 )((void) (0));
168148 yymsp[-2].minor.yy33->pLast->pNext = yymsp[-1].minor.yy33;
168149 yymsp[-2].minor.yy33->pLast = yymsp[-1].minor.yy33;
168150}
168151 break;
168152 case 269: /* trigger_cmd_list ::= trigger_cmd SEMI */
168153{
168154 assert( yymsp[-1].minor.yy33!=0 )((void) (0));
168155 yymsp[-1].minor.yy33->pLast = yymsp[-1].minor.yy33;
168156}
168157 break;
168158 case 270: /* trnm ::= nm DOT nm */
168159{
168160 yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;
168161 sqlite3ErrorMsg(pParse,
168162 "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
168163 "statements within triggers");
168164}
168165 break;
168166 case 271: /* tridxby ::= INDEXED BY nm */
168167{
168168 sqlite3ErrorMsg(pParse,
168169 "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
168170 "within triggers");
168171}
168172 break;
168173 case 272: /* tridxby ::= NOT INDEXED */
168174{
168175 sqlite3ErrorMsg(pParse,
168176 "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
168177 "within triggers");
168178}
168179 break;
168180 case 273: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt */
168181{yylhsminor.yy33 = sqlite3TriggerUpdateStep(pParse, &yymsp[-6].minor.yy0, yymsp[-2].minor.yy131, yymsp[-3].minor.yy322, yymsp[-1].minor.yy528, yymsp[-7].minor.yy394, yymsp[-8].minor.yy0.z, yymsp[0].minor.yy522);}
168182 yymsp[-8].minor.yy33 = yylhsminor.yy33;
168183 break;
168184 case 274: /* trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
168185{
168186 yylhsminor.yy33 = sqlite3TriggerInsertStep(pParse,&yymsp[-4].minor.yy0,yymsp[-3].minor.yy254,yymsp[-2].minor.yy47,yymsp[-6].minor.yy394,yymsp[-1].minor.yy444,yymsp[-7].minor.yy522,yymsp[0].minor.yy522);/*yylhsminor.yy33-overwrites-yymsp[-6].minor.yy394*/
168187}
168188 yymsp[-7].minor.yy33 = yylhsminor.yy33;
168189 break;
168190 case 275: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
168191{yylhsminor.yy33 = sqlite3TriggerDeleteStep(pParse, &yymsp[-3].minor.yy0, yymsp[-1].minor.yy528, yymsp[-5].minor.yy0.z, yymsp[0].minor.yy522);}
168192 yymsp[-5].minor.yy33 = yylhsminor.yy33;
168193 break;
168194 case 276: /* trigger_cmd ::= scanpt select scanpt */
168195{yylhsminor.yy33 = sqlite3TriggerSelectStep(pParse->db, yymsp[-1].minor.yy47, yymsp[-2].minor.yy522, yymsp[0].minor.yy522); /*yylhsminor.yy33-overwrites-yymsp[-1].minor.yy47*/}
168196 yymsp[-2].minor.yy33 = yylhsminor.yy33;
168197 break;
168198 case 277: /* expr ::= RAISE LP IGNORE RP */
168199{
168200 yymsp[-3].minor.yy528 = sqlite3PExpr(pParse, TK_RAISE71, 0, 0);
168201 if( yymsp[-3].minor.yy528 ){
168202 yymsp[-3].minor.yy528->affExpr = OE_Ignore4;
168203 }
168204}
168205 break;
168206 case 278: /* expr ::= RAISE LP raisetype COMMA nm RP */
168207{
168208 yymsp[-5].minor.yy528 = sqlite3ExprAlloc(pParse->db, TK_RAISE71, &yymsp[-1].minor.yy0, 1);
168209 if( yymsp[-5].minor.yy528 ) {
168210 yymsp[-5].minor.yy528->affExpr = (char)yymsp[-3].minor.yy394;
168211 }
168212}
168213 break;
168214 case 279: /* raisetype ::= ROLLBACK */
168215{yymsp[0].minor.yy394 = OE_Rollback1;}
168216 break;
168217 case 281: /* raisetype ::= FAIL */
168218{yymsp[0].minor.yy394 = OE_Fail3;}
168219 break;
168220 case 282: /* cmd ::= DROP TRIGGER ifexists fullname */
168221{
168222 sqlite3DropTrigger(pParse,yymsp[0].minor.yy131,yymsp[-1].minor.yy394);
168223}
168224 break;
168225 case 283: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
168226{
168227 sqlite3Attach(pParse, yymsp[-3].minor.yy528, yymsp[-1].minor.yy528, yymsp[0].minor.yy528);
168228}
168229 break;
168230 case 284: /* cmd ::= DETACH database_kw_opt expr */
168231{
168232 sqlite3Detach(pParse, yymsp[0].minor.yy528);
168233}
168234 break;
168235 case 287: /* cmd ::= REINDEX */
168236{sqlite3Reindex(pParse, 0, 0);}
168237 break;
168238 case 288: /* cmd ::= REINDEX nm dbnm */
168239{sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
168240 break;
168241 case 289: /* cmd ::= ANALYZE */
168242{sqlite3Analyze(pParse, 0, 0);}
168243 break;
168244 case 290: /* cmd ::= ANALYZE nm dbnm */
168245{sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
168246 break;
168247 case 291: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
168248{
168249 sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy131,&yymsp[0].minor.yy0);
168250}
168251 break;
168252 case 292: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
168253{
168254 yymsp[-1].minor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-1].minor.yy0.z) + pParse->sLastToken.n;
168255 sqlite3AlterFinishAddColumn(pParse, &yymsp[-1].minor.yy0);
168256}
168257 break;
168258 case 293: /* cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm */
168259{
168260 sqlite3AlterDropColumn(pParse, yymsp[-3].minor.yy131, &yymsp[0].minor.yy0);
168261}
168262 break;
168263 case 294: /* add_column_fullname ::= fullname */
168264{
168265 disableLookaside(pParse);
168266 sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy131);
168267}
168268 break;
168269 case 295: /* cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
168270{
168271 sqlite3AlterRenameColumn(pParse, yymsp[-5].minor.yy131, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0);
168272}
168273 break;
168274 case 296: /* cmd ::= create_vtab */
168275{sqlite3VtabFinishParse(pParse,0);}
168276 break;
168277 case 297: /* cmd ::= create_vtab LP vtabarglist RP */
168278{sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);}
168279 break;
168280 case 298: /* create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
168281{
168282 sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, yymsp[-4].minor.yy394);
168283}
168284 break;
168285 case 299: /* vtabarg ::= */
168286{sqlite3VtabArgInit(pParse);}
168287 break;
168288 case 300: /* vtabargtoken ::= ANY */
168289 case 301: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==301);
168290 case 302: /* lp ::= LP */ yytestcase(yyruleno==302);
168291{sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
168292 break;
168293 case 303: /* with ::= WITH wqlist */
168294 case 304: /* with ::= WITH RECURSIVE wqlist */ yytestcase(yyruleno==304);
168295{ sqlite3WithPush(pParse, yymsp[0].minor.yy521, 1); }
168296 break;
168297 case 305: /* wqas ::= AS */
168298{yymsp[0].minor.yy516 = M10d_Any1;}
168299 break;
168300 case 306: /* wqas ::= AS MATERIALIZED */
168301{yymsp[-1].minor.yy516 = M10d_Yes0;}
168302 break;
168303 case 307: /* wqas ::= AS NOT MATERIALIZED */
168304{yymsp[-2].minor.yy516 = M10d_No2;}
168305 break;
168306 case 308: /* wqitem ::= nm eidlist_opt wqas LP select RP */
168307{
168308 yymsp[-5].minor.yy385 = sqlite3CteNew(pParse, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy322, yymsp[-1].minor.yy47, yymsp[-3].minor.yy516); /*A-overwrites-X*/
168309}
168310 break;
168311 case 309: /* wqlist ::= wqitem */
168312{
168313 yymsp[0].minor.yy521 = sqlite3WithAdd(pParse, 0, yymsp[0].minor.yy385); /*A-overwrites-X*/
168314}
168315 break;
168316 case 310: /* wqlist ::= wqlist COMMA wqitem */
168317{
168318 yymsp[-2].minor.yy521 = sqlite3WithAdd(pParse, yymsp[-2].minor.yy521, yymsp[0].minor.yy385);
168319}
168320 break;
168321 case 311: /* windowdefn_list ::= windowdefn */
168322{ yylhsminor.yy41 = yymsp[0].minor.yy41; }
168323 yymsp[0].minor.yy41 = yylhsminor.yy41;
168324 break;
168325 case 312: /* windowdefn_list ::= windowdefn_list COMMA windowdefn */
168326{
168327 assert( yymsp[0].minor.yy41!=0 )((void) (0));
168328 sqlite3WindowChain(pParse, yymsp[0].minor.yy41, yymsp[-2].minor.yy41);
168329 yymsp[0].minor.yy41->pNextWin = yymsp[-2].minor.yy41;
168330 yylhsminor.yy41 = yymsp[0].minor.yy41;
168331}
168332 yymsp[-2].minor.yy41 = yylhsminor.yy41;
168333 break;
168334 case 313: /* windowdefn ::= nm AS LP window RP */
168335{
168336 if( ALWAYS(yymsp[-1].minor.yy41)(yymsp[-1].minor.yy41) ){
168337 yymsp[-1].minor.yy41->zName = sqlite3DbStrNDup(pParse->db, yymsp[-4].minor.yy0.z, yymsp[-4].minor.yy0.n);
168338 }
168339 yylhsminor.yy41 = yymsp[-1].minor.yy41;
168340}
168341 yymsp[-4].minor.yy41 = yylhsminor.yy41;
168342 break;
168343 case 314: /* window ::= PARTITION BY nexprlist orderby_opt frame_opt */
168344{
168345 yymsp[-4].minor.yy41 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy41, yymsp[-2].minor.yy322, yymsp[-1].minor.yy322, 0);
168346}
168347 break;
168348 case 315: /* window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
168349{
168350 yylhsminor.yy41 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy41, yymsp[-2].minor.yy322, yymsp[-1].minor.yy322, &yymsp[-5].minor.yy0);
168351}
168352 yymsp[-5].minor.yy41 = yylhsminor.yy41;
168353 break;
168354 case 316: /* window ::= ORDER BY sortlist frame_opt */
168355{
168356 yymsp[-3].minor.yy41 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy41, 0, yymsp[-1].minor.yy322, 0);
168357}
168358 break;
168359 case 317: /* window ::= nm ORDER BY sortlist frame_opt */
168360{
168361 yylhsminor.yy41 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy41, 0, yymsp[-1].minor.yy322, &yymsp[-4].minor.yy0);
168362}
168363 yymsp[-4].minor.yy41 = yylhsminor.yy41;
168364 break;
168365 case 318: /* window ::= frame_opt */
168366 case 337: /* filter_over ::= over_clause */ yytestcase(yyruleno==337);
168367{
168368 yylhsminor.yy41 = yymsp[0].minor.yy41;
168369}
168370 yymsp[0].minor.yy41 = yylhsminor.yy41;
168371 break;
168372 case 319: /* window ::= nm frame_opt */
168373{
168374 yylhsminor.yy41 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy41, 0, 0, &yymsp[-1].minor.yy0);
168375}
168376 yymsp[-1].minor.yy41 = yylhsminor.yy41;
168377 break;
168378 case 320: /* frame_opt ::= */
168379{
168380 yymsp[1].minor.yy41 = sqlite3WindowAlloc(pParse, 0, TK_UNBOUNDED90, 0, TK_CURRENT85, 0, 0);
168381}
168382 break;
168383 case 321: /* frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
168384{
168385 yylhsminor.yy41 = sqlite3WindowAlloc(pParse, yymsp[-2].minor.yy394, yymsp[-1].minor.yy595.eType, yymsp[-1].minor.yy595.pExpr, TK_CURRENT85, 0, yymsp[0].minor.yy516);
168386}
168387 yymsp[-2].minor.yy41 = yylhsminor.yy41;
168388 break;
168389 case 322: /* frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
168390{
168391 yylhsminor.yy41 = sqlite3WindowAlloc(pParse, yymsp[-5].minor.yy394, yymsp[-3].minor.yy595.eType, yymsp[-3].minor.yy595.pExpr, yymsp[-1].minor.yy595.eType, yymsp[-1].minor.yy595.pExpr, yymsp[0].minor.yy516);
168392}
168393 yymsp[-5].minor.yy41 = yylhsminor.yy41;
168394 break;
168395 case 324: /* frame_bound_s ::= frame_bound */
168396 case 326: /* frame_bound_e ::= frame_bound */ yytestcase(yyruleno==326);
168397{yylhsminor.yy595 = yymsp[0].minor.yy595;}
168398 yymsp[0].minor.yy595 = yylhsminor.yy595;
168399 break;
168400 case 325: /* frame_bound_s ::= UNBOUNDED PRECEDING */
168401 case 327: /* frame_bound_e ::= UNBOUNDED FOLLOWING */ yytestcase(yyruleno==327);
168402 case 329: /* frame_bound ::= CURRENT ROW */ yytestcase(yyruleno==329);
168403{yylhsminor.yy595.eType = yymsp[-1].major; yylhsminor.yy595.pExpr = 0;}
168404 yymsp[-1].minor.yy595 = yylhsminor.yy595;
168405 break;
168406 case 328: /* frame_bound ::= expr PRECEDING|FOLLOWING */
168407{yylhsminor.yy595.eType = yymsp[0].major; yylhsminor.yy595.pExpr = yymsp[-1].minor.yy528;}
168408 yymsp[-1].minor.yy595 = yylhsminor.yy595;
168409 break;
168410 case 330: /* frame_exclude_opt ::= */
168411{yymsp[1].minor.yy516 = 0;}
168412 break;
168413 case 331: /* frame_exclude_opt ::= EXCLUDE frame_exclude */
168414{yymsp[-1].minor.yy516 = yymsp[0].minor.yy516;}
168415 break;
168416 case 332: /* frame_exclude ::= NO OTHERS */
168417 case 333: /* frame_exclude ::= CURRENT ROW */ yytestcase(yyruleno==333);
168418{yymsp[-1].minor.yy516 = yymsp[-1].major; /*A-overwrites-X*/}
168419 break;
168420 case 334: /* frame_exclude ::= GROUP|TIES */
168421{yymsp[0].minor.yy516 = yymsp[0].major; /*A-overwrites-X*/}
168422 break;
168423 case 335: /* window_clause ::= WINDOW windowdefn_list */
168424{ yymsp[-1].minor.yy41 = yymsp[0].minor.yy41; }
168425 break;
168426 case 336: /* filter_over ::= filter_clause over_clause */
168427{
168428 if( yymsp[0].minor.yy41 ){
168429 yymsp[0].minor.yy41->pFilter = yymsp[-1].minor.yy528;
168430 }else{
168431 sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy528);
168432 }
168433 yylhsminor.yy41 = yymsp[0].minor.yy41;
168434}
168435 yymsp[-1].minor.yy41 = yylhsminor.yy41;
168436 break;
168437 case 338: /* filter_over ::= filter_clause */
168438{
168439 yylhsminor.yy41 = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
168440 if( yylhsminor.yy41 ){
168441 yylhsminor.yy41->eFrmType = TK_FILTER166;
168442 yylhsminor.yy41->pFilter = yymsp[0].minor.yy528;
168443 }else{
168444 sqlite3ExprDelete(pParse->db, yymsp[0].minor.yy528);
168445 }
168446}
168447 yymsp[0].minor.yy41 = yylhsminor.yy41;
168448 break;
168449 case 339: /* over_clause ::= OVER LP window RP */
168450{
168451 yymsp[-3].minor.yy41 = yymsp[-1].minor.yy41;
168452 assert( yymsp[-3].minor.yy41!=0 )((void) (0));
168453}
168454 break;
168455 case 340: /* over_clause ::= OVER nm */
168456{
168457 yymsp[-1].minor.yy41 = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
168458 if( yymsp[-1].minor.yy41 ){
168459 yymsp[-1].minor.yy41->zName = sqlite3DbStrNDup(pParse->db, yymsp[0].minor.yy0.z, yymsp[0].minor.yy0.n);
168460 }
168461}
168462 break;
168463 case 341: /* filter_clause ::= FILTER LP WHERE expr RP */
168464{ yymsp[-4].minor.yy528 = yymsp[-1].minor.yy528; }
168465 break;
168466 default:
168467 /* (342) input ::= cmdlist */ yytestcase(yyruleno==342);
168468 /* (343) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==343);
168469 /* (344) cmdlist ::= ecmd (OPTIMIZED OUT) */ assert(yyruleno!=344)((void) (0));
168470 /* (345) ecmd ::= SEMI */ yytestcase(yyruleno==345);
168471 /* (346) ecmd ::= cmdx SEMI */ yytestcase(yyruleno==346);
168472 /* (347) ecmd ::= explain cmdx SEMI (NEVER REDUCES) */ assert(yyruleno!=347)((void) (0));
168473 /* (348) trans_opt ::= */ yytestcase(yyruleno==348);
168474 /* (349) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==349);
168475 /* (350) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==350);
168476 /* (351) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==351);
168477 /* (352) savepoint_opt ::= */ yytestcase(yyruleno==352);
168478 /* (353) cmd ::= create_table create_table_args */ yytestcase(yyruleno==353);
168479 /* (354) table_option_set ::= table_option (OPTIMIZED OUT) */ assert(yyruleno!=354)((void) (0));
168480 /* (355) columnlist ::= columnlist COMMA columnname carglist */ yytestcase(yyruleno==355);
168481 /* (356) columnlist ::= columnname carglist */ yytestcase(yyruleno==356);
168482 /* (357) nm ::= ID|INDEXED */ yytestcase(yyruleno==357);
168483 /* (358) nm ::= STRING */ yytestcase(yyruleno==358);
168484 /* (359) nm ::= JOIN_KW */ yytestcase(yyruleno==359);
168485 /* (360) typetoken ::= typename */ yytestcase(yyruleno==360);
168486 /* (361) typename ::= ID|STRING */ yytestcase(yyruleno==361);
168487 /* (362) signed ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=362)((void) (0));
168488 /* (363) signed ::= minus_num (OPTIMIZED OUT) */ assert(yyruleno!=363)((void) (0));
168489 /* (364) carglist ::= carglist ccons */ yytestcase(yyruleno==364);
168490 /* (365) carglist ::= */ yytestcase(yyruleno==365);
168491 /* (366) ccons ::= NULL onconf */ yytestcase(yyruleno==366);
168492 /* (367) ccons ::= GENERATED ALWAYS AS generated */ yytestcase(yyruleno==367);
168493 /* (368) ccons ::= AS generated */ yytestcase(yyruleno==368);
168494 /* (369) conslist_opt ::= COMMA conslist */ yytestcase(yyruleno==369);
168495 /* (370) conslist ::= conslist tconscomma tcons */ yytestcase(yyruleno==370);
168496 /* (371) conslist ::= tcons (OPTIMIZED OUT) */ assert(yyruleno!=371)((void) (0));
168497 /* (372) tconscomma ::= */ yytestcase(yyruleno==372);
168498 /* (373) defer_subclause_opt ::= defer_subclause (OPTIMIZED OUT) */ assert(yyruleno!=373)((void) (0));
168499 /* (374) resolvetype ::= raisetype (OPTIMIZED OUT) */ assert(yyruleno!=374)((void) (0));
168500 /* (375) selectnowith ::= oneselect (OPTIMIZED OUT) */ assert(yyruleno!=375)((void) (0));
168501 /* (376) oneselect ::= values */ yytestcase(yyruleno==376);
168502 /* (377) sclp ::= selcollist COMMA */ yytestcase(yyruleno==377);
168503 /* (378) as ::= ID|STRING */ yytestcase(yyruleno==378);
168504 /* (379) indexed_opt ::= indexed_by (OPTIMIZED OUT) */ assert(yyruleno!=379)((void) (0));
168505 /* (380) returning ::= */ yytestcase(yyruleno==380);
168506 /* (381) expr ::= term (OPTIMIZED OUT) */ assert(yyruleno!=381)((void) (0));
168507 /* (382) likeop ::= LIKE_KW|MATCH */ yytestcase(yyruleno==382);
168508 /* (383) exprlist ::= nexprlist */ yytestcase(yyruleno==383);
168509 /* (384) nmnum ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=384)((void) (0));
168510 /* (385) nmnum ::= nm (OPTIMIZED OUT) */ assert(yyruleno!=385)((void) (0));
168511 /* (386) nmnum ::= ON */ yytestcase(yyruleno==386);
168512 /* (387) nmnum ::= DELETE */ yytestcase(yyruleno==387);
168513 /* (388) nmnum ::= DEFAULT */ yytestcase(yyruleno==388);
168514 /* (389) plus_num ::= INTEGER|FLOAT */ yytestcase(yyruleno==389);
168515 /* (390) foreach_clause ::= */ yytestcase(yyruleno==390);
168516 /* (391) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==391);
168517 /* (392) trnm ::= nm */ yytestcase(yyruleno==392);
168518 /* (393) tridxby ::= */ yytestcase(yyruleno==393);
168519 /* (394) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==394);
168520 /* (395) database_kw_opt ::= */ yytestcase(yyruleno==395);
168521 /* (396) kwcolumn_opt ::= */ yytestcase(yyruleno==396);
168522 /* (397) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==397);
168523 /* (398) vtabarglist ::= vtabarg */ yytestcase(yyruleno==398);
168524 /* (399) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==399);
168525 /* (400) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==400);
168526 /* (401) anylist ::= */ yytestcase(yyruleno==401);
168527 /* (402) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==402);
168528 /* (403) anylist ::= anylist ANY */ yytestcase(yyruleno==403);
168529 /* (404) with ::= */ yytestcase(yyruleno==404);
168530 break;
168531/********** End reduce actions ************************************************/
168532 };
168533 assert( yyruleno<sizeof(yyRuleInfoLhs)/sizeof(yyRuleInfoLhs[0]) )((void) (0));
168534 yygoto = yyRuleInfoLhs[yyruleno];
168535 yysize = yyRuleInfoNRhs[yyruleno];
168536 yyact = yy_find_reduce_action(yymsp[yysize].stateno,(YYCODETYPEunsigned short int)yygoto);
168537
168538 /* There are no SHIFTREDUCE actions on nonterminals because the table
168539 ** generator has simplified them to pure REDUCE actions. */
168540 assert( !(yyact>YY_MAX_SHIFT && yyact<=YY_MAX_SHIFTREDUCE) )((void) (0));
168541
168542 /* It is not possible for a REDUCE to be followed by an error */
168543 assert( yyact!=YY_ERROR_ACTION )((void) (0));
168544
168545 yymsp += yysize+1;
168546 yypParser->yytos = yymsp;
168547 yymsp->stateno = (YYACTIONTYPEunsigned short int)yyact;
168548 yymsp->major = (YYCODETYPEunsigned short int)yygoto;
168549 yyTraceShift(yypParser, yyact, "... then shift");
168550 return yyact;
168551}
168552
168553/*
168554** The following code executes when the parse fails
168555*/
168556#ifndef YYNOERRORRECOVERY1
168557static void yy_parse_failed(
168558 yyParser *yypParser /* The parser */
168559){
168560 sqlite3ParserARG_FETCH
168561 sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse;
168562#ifndef NDEBUG1
168563 if( yyTraceFILE ){
168564 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
168565 }
168566#endif
168567 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
168568 /* Here code is inserted which will be executed whenever the
168569 ** parser fails */
168570/************ Begin %parse_failure code ***************************************/
168571/************ End %parse_failure code *****************************************/
168572 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
168573 sqlite3ParserCTX_STOREyypParser->pParse=pParse;
168574}
168575#endif /* YYNOERRORRECOVERY */
168576
168577/*
168578** The following code executes when a syntax error first occurs.
168579*/
168580static void yy_syntax_error(
168581 yyParser *yypParser, /* The parser */
168582 int yymajor, /* The major type of the error token */
168583 sqlite3ParserTOKENTYPEToken yyminor /* The minor type of the error token */
168584){
168585 sqlite3ParserARG_FETCH
168586 sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse;
168587#define TOKENyyminor yyminor
168588/************ Begin %syntax_error code ****************************************/
168589
168590 UNUSED_PARAMETER(yymajor)(void)(yymajor); /* Silence some compiler warnings */
168591 if( TOKENyyminor.z[0] ){
168592 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKENyyminor);
168593 }else{
168594 sqlite3ErrorMsg(pParse, "incomplete input");
168595 }
168596/************ End %syntax_error code ******************************************/
168597 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
168598 sqlite3ParserCTX_STOREyypParser->pParse=pParse;
168599}
168600
168601/*
168602** The following is executed when the parser accepts
168603*/
168604static void yy_accept(
168605 yyParser *yypParser /* The parser */
168606){
168607 sqlite3ParserARG_FETCH
168608 sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse;
168609#ifndef NDEBUG1
168610 if( yyTraceFILE ){
168611 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
168612 }
168613#endif
168614#ifndef YYNOERRORRECOVERY1
168615 yypParser->yyerrcnt = -1;
168616#endif
168617 assert( yypParser->yytos==yypParser->yystack )((void) (0));
168618 /* Here code is inserted which will be executed whenever the
168619 ** parser accepts */
168620/*********** Begin %parse_accept code *****************************************/
168621/*********** End %parse_accept code *******************************************/
168622 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
168623 sqlite3ParserCTX_STOREyypParser->pParse=pParse;
168624}
168625
168626/* The main parser program.
168627** The first argument is a pointer to a structure obtained from
168628** "sqlite3ParserAlloc" which describes the current state of the parser.
168629** The second argument is the major token number. The third is
168630** the minor token. The fourth optional argument is whatever the
168631** user wants (and specified in the grammar) and is available for
168632** use by the action routines.
168633**
168634** Inputs:
168635** <ul>
168636** <li> A pointer to the parser (an opaque structure.)
168637** <li> The major token number.
168638** <li> The minor token number.
168639** <li> An option argument of a grammar-specified type.
168640** </ul>
168641**
168642** Outputs:
168643** None.
168644*/
168645SQLITE_PRIVATEstatic void sqlite3Parser(
168646 void *yyp, /* The parser */
168647 int yymajor, /* The major token code number */
168648 sqlite3ParserTOKENTYPEToken yyminor /* The value for the token */
168649 sqlite3ParserARG_PDECL /* Optional %extra_argument parameter */
168650){
168651 YYMINORTYPE yyminorunion;
168652 YYACTIONTYPEunsigned short int yyact; /* The parser action. */
168653#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY1)
168654 int yyendofinput; /* True if we are at the end of input */
168655#endif
168656#ifdef YYERRORSYMBOL
168657 int yyerrorhit = 0; /* True if yymajor has invoked an error */
168658#endif
168659 yyParser *yypParser = (yyParser*)yyp; /* The parser */
168660 sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse;
168661 sqlite3ParserARG_STORE
168662
168663 assert( yypParser->yytos!=0 )((void) (0));
168664#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY1)
168665 yyendofinput = (yymajor==0);
168666#endif
168667
168668 yyact = yypParser->yytos->stateno;
168669#ifndef NDEBUG1
168670 if( yyTraceFILE ){
168671 if( yyact < YY_MIN_REDUCE1243 ){
168672 fprintf(yyTraceFILE,"%sInput '%s' in state %d\n",
168673 yyTracePrompt,yyTokenName[yymajor],yyact);
168674 }else{
168675 fprintf(yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
168676 yyTracePrompt,yyTokenName[yymajor],yyact-YY_MIN_REDUCE1243);
168677 }
168678 }
168679#endif
168680
168681 while(1){ /* Exit by "break" */
168682 assert( yypParser->yytos>=yypParser->yystack )((void) (0));
168683 assert( yyact==yypParser->yytos->stateno )((void) (0));
168684 yyact = yy_find_shift_action((YYCODETYPEunsigned short int)yymajor,yyact);
168685 if( yyact >= YY_MIN_REDUCE1243 ){
168686 unsigned int yyruleno = yyact - YY_MIN_REDUCE1243; /* Reduce by this rule */
168687#ifndef NDEBUG1
168688 assert( yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) )((void) (0));
168689 if( yyTraceFILE ){
168690 int yysize = yyRuleInfoNRhs[yyruleno];
168691 if( yysize ){
168692 fprintf(yyTraceFILE, "%sReduce %d [%s]%s, pop back to state %d.\n",
168693 yyTracePrompt,
168694 yyruleno, yyRuleName[yyruleno],
168695 yyruleno<YYNRULE_WITH_ACTION342 ? "" : " without external action",
168696 yypParser->yytos[yysize].stateno);
168697 }else{
168698 fprintf(yyTraceFILE, "%sReduce %d [%s]%s.\n",
168699 yyTracePrompt, yyruleno, yyRuleName[yyruleno],
168700 yyruleno<YYNRULE_WITH_ACTION342 ? "" : " without external action");
168701 }
168702 }
168703#endif /* NDEBUG */
168704
168705 /* Check that the stack is large enough to grow by a single entry
168706 ** if the RHS of the rule is empty. This ensures that there is room
168707 ** enough on the stack to push the LHS value */
168708 if( yyRuleInfoNRhs[yyruleno]==0 ){
168709#ifdef YYTRACKMAXSTACKDEPTH
168710 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
168711 yypParser->yyhwm++;
168712 assert( yypParser->yyhwm ==((void) (0))
168713 (int)(yypParser->yytos - yypParser->yystack))((void) (0));
168714 }
168715#endif
168716#if YYSTACKDEPTH100>0
168717 if( yypParser->yytos>=yypParser->yystackEnd ){
168718 yyStackOverflow(yypParser);
168719 break;
168720 }
168721#else
168722 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz-1] ){
168723 if( yyGrowStack(yypParser) ){
168724 yyStackOverflow(yypParser);
168725 break;
168726 }
168727 }
168728#endif
168729 }
168730 yyact = yy_reduce(yypParser,yyruleno,yymajor,yyminor sqlite3ParserCTX_PARAM,pParse);
168731 }else if( yyact <= YY_MAX_SHIFTREDUCE1239 ){
168732 yy_shift(yypParser,yyact,(YYCODETYPEunsigned short int)yymajor,yyminor);
168733#ifndef YYNOERRORRECOVERY1
168734 yypParser->yyerrcnt--;
168735#endif
168736 break;
168737 }else if( yyact==YY_ACCEPT_ACTION1241 ){
168738 yypParser->yytos--;
168739 yy_accept(yypParser);
168740 return;
168741 }else{
168742 assert( yyact == YY_ERROR_ACTION )((void) (0));
168743 yyminorunion.yy0 = yyminor;
168744#ifdef YYERRORSYMBOL
168745 int yymx;
168746#endif
168747#ifndef NDEBUG1
168748 if( yyTraceFILE ){
168749 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
168750 }
168751#endif
168752#ifdef YYERRORSYMBOL
168753 /* A syntax error has occurred.
168754 ** The response to an error depends upon whether or not the
168755 ** grammar defines an error token "ERROR".
168756 **
168757 ** This is what we do if the grammar does define ERROR:
168758 **
168759 ** * Call the %syntax_error function.
168760 **
168761 ** * Begin popping the stack until we enter a state where
168762 ** it is legal to shift the error symbol, then shift
168763 ** the error symbol.
168764 **
168765 ** * Set the error count to three.
168766 **
168767 ** * Begin accepting and shifting new tokens. No new error
168768 ** processing will occur until three tokens have been
168769 ** shifted successfully.
168770 **
168771 */
168772 if( yypParser->yyerrcnt<0 ){
168773 yy_syntax_error(yypParser,yymajor,yyminor);
168774 }
168775 yymx = yypParser->yytos->major;
168776 if( yymx==YYERRORSYMBOL || yyerrorhit ){
168777#ifndef NDEBUG1
168778 if( yyTraceFILE ){
168779 fprintf(yyTraceFILE,"%sDiscard input token %s\n",
168780 yyTracePrompt,yyTokenName[yymajor]);
168781 }
168782#endif
168783 yy_destructor(yypParser, (YYCODETYPEunsigned short int)yymajor, &yyminorunion);
168784 yymajor = YYNOCODE319;
168785 }else{
168786 while( yypParser->yytos > yypParser->yystack ){
168787 yyact = yy_find_reduce_action(yypParser->yytos->stateno,
168788 YYERRORSYMBOL);
168789 if( yyact<=YY_MAX_SHIFTREDUCE1239 ) break;
168790 yy_pop_parser_stack(yypParser);
168791 }
168792 if( yypParser->yytos <= yypParser->yystack || yymajor==0 ){
168793 yy_destructor(yypParser,(YYCODETYPEunsigned short int)yymajor,&yyminorunion);
168794 yy_parse_failed(yypParser);
168795#ifndef YYNOERRORRECOVERY1
168796 yypParser->yyerrcnt = -1;
168797#endif
168798 yymajor = YYNOCODE319;
168799 }else if( yymx!=YYERRORSYMBOL ){
168800 yy_shift(yypParser,yyact,YYERRORSYMBOL,yyminor);
168801 }
168802 }
168803 yypParser->yyerrcnt = 3;
168804 yyerrorhit = 1;
168805 if( yymajor==YYNOCODE319 ) break;
168806 yyact = yypParser->yytos->stateno;
168807#elif defined(YYNOERRORRECOVERY1)
168808 /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
168809 ** do any kind of error recovery. Instead, simply invoke the syntax
168810 ** error routine and continue going as if nothing had happened.
168811 **
168812 ** Applications can set this macro (for example inside %include) if
168813 ** they intend to abandon the parse upon the first syntax error seen.
168814 */
168815 yy_syntax_error(yypParser,yymajor, yyminor);
168816 yy_destructor(yypParser,(YYCODETYPEunsigned short int)yymajor,&yyminorunion);
168817 break;
168818#else /* YYERRORSYMBOL is not defined */
168819 /* This is what we do if the grammar does not define ERROR:
168820 **
168821 ** * Report an error message, and throw away the input token.
168822 **
168823 ** * If the input token is $, then fail the parse.
168824 **
168825 ** As before, subsequent error messages are suppressed until
168826 ** three input tokens have been successfully shifted.
168827 */
168828 if( yypParser->yyerrcnt<=0 ){
168829 yy_syntax_error(yypParser,yymajor, yyminor);
168830 }
168831 yypParser->yyerrcnt = 3;
168832 yy_destructor(yypParser,(YYCODETYPEunsigned short int)yymajor,&yyminorunion);
168833 if( yyendofinput ){
168834 yy_parse_failed(yypParser);
168835#ifndef YYNOERRORRECOVERY1
168836 yypParser->yyerrcnt = -1;
168837#endif
168838 }
168839 break;
168840#endif
168841 }
168842 }
168843#ifndef NDEBUG1
168844 if( yyTraceFILE ){
168845 yyStackEntry *i;
168846 char cDiv = '[';
168847 fprintf(yyTraceFILE,"%sReturn. Stack=",yyTracePrompt);
168848 for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){
168849 fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]);
168850 cDiv = ' ';
168851 }
168852 fprintf(yyTraceFILE,"]\n");
168853 }
168854#endif
168855 return;
168856}
168857
168858/*
168859** Return the fallback token corresponding to canonical token iToken, or
168860** 0 if iToken has no fallback.
168861*/
168862SQLITE_PRIVATEstatic int sqlite3ParserFallback(int iToken){
168863#ifdef YYFALLBACK1
168864 assert( iToken<(int)(sizeof(yyFallback)/sizeof(yyFallback[0])) )((void) (0));
168865 return yyFallback[iToken];
168866#else
168867 (void)iToken;
168868 return 0;
168869#endif
168870}
168871
168872/************** End of parse.c ***********************************************/
168873/************** Begin file tokenize.c ****************************************/
168874/*
168875** 2001 September 15
168876**
168877** The author disclaims copyright to this source code. In place of
168878** a legal notice, here is a blessing:
168879**
168880** May you do good and not evil.
168881** May you find forgiveness for yourself and forgive others.
168882** May you share freely, never taking more than you give.
168883**
168884*************************************************************************
168885** An tokenizer for SQL
168886**
168887** This file contains C code that splits an SQL input string up into
168888** individual tokens and sends those tokens one-by-one over to the
168889** parser for analysis.
168890*/
168891/* #include "sqliteInt.h" */
168892/* #include <stdlib.h> */
168893
168894/* Character classes for tokenizing
168895**
168896** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented
168897** using a lookup table, whereas a switch() directly on c uses a binary search.
168898** The lookup table is much faster. To maximize speed, and to ensure that
168899** a lookup table is used, all of the classes need to be small integers and
168900** all of them need to be used within the switch.
168901*/
168902#define CC_X0 0 /* The letter 'x', or start of BLOB literal */
168903#define CC_KYWD01 1 /* First letter of a keyword */
168904#define CC_KYWD2 2 /* Alphabetics or '_'. Usable in a keyword */
168905#define CC_DIGIT3 3 /* Digits */
168906#define CC_DOLLAR4 4 /* '$' */
168907#define CC_VARALPHA5 5 /* '@', '#', ':'. Alphabetic SQL variables */
168908#define CC_VARNUM6 6 /* '?'. Numeric SQL variables */
168909#define CC_SPACE7 7 /* Space characters */
168910#define CC_QUOTE8 8 /* '"', '\'', or '`'. String literals, quoted ids */
168911#define CC_QUOTE29 9 /* '['. [...] style quoted ids */
168912#define CC_PIPE10 10 /* '|'. Bitwise OR or concatenate */
168913#define CC_MINUS11 11 /* '-'. Minus or SQL-style comment */
168914#define CC_LT12 12 /* '<'. Part of < or <= or <> */
168915#define CC_GT13 13 /* '>'. Part of > or >= */
168916#define CC_EQ14 14 /* '='. Part of = or == */
168917#define CC_BANG15 15 /* '!'. Part of != */
168918#define CC_SLASH16 16 /* '/'. / or c-style comment */
168919#define CC_LP17 17 /* '(' */
168920#define CC_RP18 18 /* ')' */
168921#define CC_SEMI19 19 /* ';' */
168922#define CC_PLUS20 20 /* '+' */
168923#define CC_STAR21 21 /* '*' */
168924#define CC_PERCENT22 22 /* '%' */
168925#define CC_COMMA23 23 /* ',' */
168926#define CC_AND24 24 /* '&' */
168927#define CC_TILDA25 25 /* '~' */
168928#define CC_DOT26 26 /* '.' */
168929#define CC_ID27 27 /* unicode characters usable in IDs */
168930#define CC_ILLEGAL28 28 /* Illegal character */
168931#define CC_NUL29 29 /* 0x00 */
168932#define CC_BOM30 30 /* First byte of UTF8 BOM: 0xEF 0xBB 0xBF */
168933
168934static const unsigned char aiClass[] = {
168935#ifdef SQLITE_ASCII1
168936/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
168937/* 0x */ 29, 28, 28, 28, 28, 28, 28, 28, 28, 7, 7, 28, 7, 7, 28, 28,
168938/* 1x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
168939/* 2x */ 7, 15, 8, 5, 4, 22, 24, 8, 17, 18, 21, 20, 23, 11, 26, 16,
168940/* 3x */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 19, 12, 14, 13, 6,
168941/* 4x */ 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
168942/* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 2, 9, 28, 28, 28, 2,
168943/* 6x */ 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
168944/* 7x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 2, 28, 10, 28, 25, 28,
168945/* 8x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
168946/* 9x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
168947/* Ax */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
168948/* Bx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
168949/* Cx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
168950/* Dx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
168951/* Ex */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 30,
168952/* Fx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27
168953#endif
168954#ifdef SQLITE_EBCDIC
168955/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
168956/* 0x */ 29, 28, 28, 28, 28, 7, 28, 28, 28, 28, 28, 28, 7, 7, 28, 28,
168957/* 1x */ 28, 28, 28, 28, 28, 7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
168958/* 2x */ 28, 28, 28, 28, 28, 7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
168959/* 3x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
168960/* 4x */ 7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 26, 12, 17, 20, 10,
168961/* 5x */ 24, 28, 28, 28, 28, 28, 28, 28, 28, 28, 15, 4, 21, 18, 19, 28,
168962/* 6x */ 11, 16, 28, 28, 28, 28, 28, 28, 28, 28, 28, 23, 22, 2, 13, 6,
168963/* 7x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 8, 5, 5, 5, 8, 14, 8,
168964/* 8x */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28,
168965/* 9x */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28,
168966/* Ax */ 28, 25, 1, 1, 1, 1, 1, 0, 2, 2, 28, 28, 28, 28, 28, 28,
168967/* Bx */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 9, 28, 28, 28, 28, 28,
168968/* Cx */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28,
168969/* Dx */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28,
168970/* Ex */ 28, 28, 1, 1, 1, 1, 1, 0, 2, 2, 28, 28, 28, 28, 28, 28,
168971/* Fx */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 28, 28, 28, 28, 28, 28,
168972#endif
168973};
168974
168975/*
168976** The charMap() macro maps alphabetic characters (only) into their
168977** lower-case ASCII equivalent. On ASCII machines, this is just
168978** an upper-to-lower case map. On EBCDIC machines we also need
168979** to adjust the encoding. The mapping is only valid for alphabetics
168980** which are the only characters for which this feature is used.
168981**
168982** Used by keywordhash.h
168983*/
168984#ifdef SQLITE_ASCII1
168985# define charMap(X)sqlite3UpperToLower[(unsigned char)X] sqlite3UpperToLower[(unsigned char)X]
168986#endif
168987#ifdef SQLITE_EBCDIC
168988# define charMap(X)sqlite3UpperToLower[(unsigned char)X] ebcdicToAscii[(unsigned char)X]
168989const unsigned char ebcdicToAscii[] = {
168990/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
168991 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
168992 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
168993 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
168994 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */
168995 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */
168996 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */
168997 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */
168998 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */
168999 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */
169000 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */
169001 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */
169002 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
169003 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */
169004 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */
169005 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */
169006 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */
169007};
169008#endif
169009
169010/*
169011** The sqlite3KeywordCode function looks up an identifier to determine if
169012** it is a keyword. If it is a keyword, the token code of that keyword is
169013** returned. If the input is not a keyword, TK_ID is returned.
169014**
169015** The implementation of this routine was generated by a program,
169016** mkkeywordhash.c, located in the tool subdirectory of the distribution.
169017** The output of the mkkeywordhash.c program is written into a file
169018** named keywordhash.h and then included into this source file by
169019** the #include below.
169020*/
169021/************** Include keywordhash.h in the middle of tokenize.c ************/
169022/************** Begin file keywordhash.h *************************************/
169023/***** This file contains automatically generated code ******
169024**
169025** The code in this file has been automatically generated by
169026**
169027** sqlite/tool/mkkeywordhash.c
169028**
169029** The code in this file implements a function that determines whether
169030** or not a given identifier is really an SQL keyword. The same thing
169031** might be implemented more directly using a hand-written hash table.
169032** But by using this automatically generated code, the size of the code
169033** is substantially reduced. This is important for embedded applications
169034** on platforms with limited memory.
169035*/
169036/* Hash score: 231 */
169037/* zKWText[] encodes 1007 bytes of keyword text in 667 bytes */
169038/* REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT */
169039/* ABLEFTHENDEFERRABLELSEXCLUDELETEMPORARYISNULLSAVEPOINTERSECT */
169040/* IESNOTNULLIKEXCEPTRANSACTIONATURALTERAISEXCLUSIVEXISTS */
169041/* CONSTRAINTOFFSETRIGGERANGENERATEDETACHAVINGLOBEGINNEREFERENCES */
169042/* UNIQUERYWITHOUTERELEASEATTACHBETWEENOTHINGROUPSCASCADEFAULT */
169043/* CASECOLLATECREATECURRENT_DATEIMMEDIATEJOINSERTMATCHPLANALYZE */
169044/* PRAGMATERIALIZEDEFERREDISTINCTUPDATEVALUESVIRTUALWAYSWHENWHERE */
169045/* CURSIVEABORTAFTERENAMEANDROPARTITIONAUTOINCREMENTCASTCOLUMN */
169046/* COMMITCONFLICTCROSSCURRENT_TIMESTAMPRECEDINGFAILASTFILTER */
169047/* EPLACEFIRSTFOLLOWINGFROMFULLIMITIFORDERESTRICTOTHERSOVER */
169048/* ETURNINGRIGHTROLLBACKROWSUNBOUNDEDUNIONUSINGVACUUMVIEWINDOWBY */
169049/* INITIALLYPRIMARY */
169050static const char zKWText[666] = {
169051 'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H',
169052 'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G',
169053 'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A',
169054 'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F',
169055 'E','R','R','A','B','L','E','L','S','E','X','C','L','U','D','E','L','E',
169056 'T','E','M','P','O','R','A','R','Y','I','S','N','U','L','L','S','A','V',
169057 'E','P','O','I','N','T','E','R','S','E','C','T','I','E','S','N','O','T',
169058 'N','U','L','L','I','K','E','X','C','E','P','T','R','A','N','S','A','C',
169059 'T','I','O','N','A','T','U','R','A','L','T','E','R','A','I','S','E','X',
169060 'C','L','U','S','I','V','E','X','I','S','T','S','C','O','N','S','T','R',
169061 'A','I','N','T','O','F','F','S','E','T','R','I','G','G','E','R','A','N',
169062 'G','E','N','E','R','A','T','E','D','E','T','A','C','H','A','V','I','N',
169063 'G','L','O','B','E','G','I','N','N','E','R','E','F','E','R','E','N','C',
169064 'E','S','U','N','I','Q','U','E','R','Y','W','I','T','H','O','U','T','E',
169065 'R','E','L','E','A','S','E','A','T','T','A','C','H','B','E','T','W','E',
169066 'E','N','O','T','H','I','N','G','R','O','U','P','S','C','A','S','C','A',
169067 'D','E','F','A','U','L','T','C','A','S','E','C','O','L','L','A','T','E',
169068 'C','R','E','A','T','E','C','U','R','R','E','N','T','_','D','A','T','E',
169069 'I','M','M','E','D','I','A','T','E','J','O','I','N','S','E','R','T','M',
169070 'A','T','C','H','P','L','A','N','A','L','Y','Z','E','P','R','A','G','M',
169071 'A','T','E','R','I','A','L','I','Z','E','D','E','F','E','R','R','E','D',
169072 'I','S','T','I','N','C','T','U','P','D','A','T','E','V','A','L','U','E',
169073 'S','V','I','R','T','U','A','L','W','A','Y','S','W','H','E','N','W','H',
169074 'E','R','E','C','U','R','S','I','V','E','A','B','O','R','T','A','F','T',
169075 'E','R','E','N','A','M','E','A','N','D','R','O','P','A','R','T','I','T',
169076 'I','O','N','A','U','T','O','I','N','C','R','E','M','E','N','T','C','A',
169077 'S','T','C','O','L','U','M','N','C','O','M','M','I','T','C','O','N','F',
169078 'L','I','C','T','C','R','O','S','S','C','U','R','R','E','N','T','_','T',
169079 'I','M','E','S','T','A','M','P','R','E','C','E','D','I','N','G','F','A',
169080 'I','L','A','S','T','F','I','L','T','E','R','E','P','L','A','C','E','F',
169081 'I','R','S','T','F','O','L','L','O','W','I','N','G','F','R','O','M','F',
169082 'U','L','L','I','M','I','T','I','F','O','R','D','E','R','E','S','T','R',
169083 'I','C','T','O','T','H','E','R','S','O','V','E','R','E','T','U','R','N',
169084 'I','N','G','R','I','G','H','T','R','O','L','L','B','A','C','K','R','O',
169085 'W','S','U','N','B','O','U','N','D','E','D','U','N','I','O','N','U','S',
169086 'I','N','G','V','A','C','U','U','M','V','I','E','W','I','N','D','O','W',
169087 'B','Y','I','N','I','T','I','A','L','L','Y','P','R','I','M','A','R','Y',
169088};
169089/* aKWHash[i] is the hash value for the i-th keyword */
169090static const unsigned char aKWHash[127] = {
169091 84, 92, 134, 82, 105, 29, 0, 0, 94, 0, 85, 72, 0,
169092 53, 35, 86, 15, 0, 42, 97, 54, 89, 135, 19, 0, 0,
169093 140, 0, 40, 129, 0, 22, 107, 0, 9, 0, 0, 123, 80,
169094 0, 78, 6, 0, 65, 103, 147, 0, 136, 115, 0, 0, 48,
169095 0, 90, 24, 0, 17, 0, 27, 70, 23, 26, 5, 60, 142,
169096 110, 122, 0, 73, 91, 71, 145, 61, 120, 74, 0, 49, 0,
169097 11, 41, 0, 113, 0, 0, 0, 109, 10, 111, 116, 125, 14,
169098 50, 124, 0, 100, 0, 18, 121, 144, 56, 130, 139, 88, 83,
169099 37, 30, 126, 0, 0, 108, 51, 131, 128, 0, 34, 0, 0,
169100 132, 0, 98, 38, 39, 0, 20, 45, 117, 93,
169101};
169102/* aKWNext[] forms the hash collision chain. If aKWHash[i]==0
169103** then the i-th keyword has no more hash collisions. Otherwise,
169104** the next keyword with the same hash is aKWHash[i]-1. */
169105static const unsigned char aKWNext[147] = {
169106 0, 0, 0, 0, 4, 0, 43, 0, 0, 106, 114, 0, 0,
169107 0, 2, 0, 0, 143, 0, 0, 0, 13, 0, 0, 0, 0,
169108 141, 0, 0, 119, 52, 0, 0, 137, 12, 0, 0, 62, 0,
169109 138, 0, 133, 0, 0, 36, 0, 0, 28, 77, 0, 0, 0,
169110 0, 59, 0, 47, 0, 0, 0, 0, 0, 0, 0, 0, 0,
169111 0, 69, 0, 0, 0, 0, 0, 146, 3, 0, 58, 0, 1,
169112 75, 0, 0, 0, 31, 0, 0, 0, 0, 0, 127, 0, 104,
169113 0, 64, 66, 63, 0, 0, 0, 0, 0, 46, 0, 16, 8,
169114 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 101, 0,
169115 112, 21, 7, 67, 0, 79, 96, 118, 0, 0, 68, 0, 0,
169116 99, 44, 0, 55, 0, 76, 0, 95, 32, 33, 57, 25, 0,
169117 102, 0, 0, 87,
169118};
169119/* aKWLen[i] is the length (in bytes) of the i-th keyword */
169120static const unsigned char aKWLen[147] = {
169121 7, 7, 5, 4, 6, 4, 5, 3, 6, 7, 3, 6, 6,
169122 7, 7, 3, 8, 2, 6, 5, 4, 4, 3, 10, 4, 7,
169123 6, 9, 4, 2, 6, 5, 9, 9, 4, 7, 3, 2, 4,
169124 4, 6, 11, 6, 2, 7, 5, 5, 9, 6, 10, 4, 6,
169125 2, 3, 7, 5, 9, 6, 6, 4, 5, 5, 10, 6, 5,
169126 7, 4, 5, 7, 6, 7, 7, 6, 5, 7, 3, 7, 4,
169127 7, 6, 12, 9, 4, 6, 5, 4, 7, 6, 12, 8, 8,
169128 2, 6, 6, 7, 6, 4, 5, 9, 5, 5, 6, 3, 4,
169129 9, 13, 2, 2, 4, 6, 6, 8, 5, 17, 12, 7, 9,
169130 4, 4, 6, 7, 5, 9, 4, 4, 5, 2, 5, 8, 6,
169131 4, 9, 5, 8, 4, 3, 9, 5, 5, 6, 4, 6, 2,
169132 2, 9, 3, 7,
169133};
169134/* aKWOffset[i] is the index into zKWText[] of the start of
169135** the text for the i-th keyword. */
169136static const unsigned short int aKWOffset[147] = {
169137 0, 2, 2, 8, 9, 14, 16, 20, 23, 25, 25, 29, 33,
169138 36, 41, 46, 48, 53, 54, 59, 62, 65, 67, 69, 78, 81,
169139 86, 90, 90, 94, 99, 101, 105, 111, 119, 123, 123, 123, 126,
169140 129, 132, 137, 142, 146, 147, 152, 156, 160, 168, 174, 181, 184,
169141 184, 187, 189, 195, 198, 206, 211, 216, 219, 222, 226, 236, 239,
169142 244, 244, 248, 252, 259, 265, 271, 277, 277, 283, 284, 288, 295,
169143 299, 306, 312, 324, 333, 335, 341, 346, 348, 355, 359, 370, 377,
169144 378, 385, 391, 397, 402, 408, 412, 415, 424, 429, 433, 439, 441,
169145 444, 453, 455, 457, 466, 470, 476, 482, 490, 495, 495, 495, 511,
169146 520, 523, 527, 532, 539, 544, 553, 557, 560, 565, 567, 571, 579,
169147 585, 588, 597, 602, 610, 610, 614, 623, 628, 633, 639, 642, 645,
169148 648, 650, 655, 659,
169149};
169150/* aKWCode[i] is the parser symbol code for the i-th keyword */
169151static const unsigned char aKWCode[147] = {
169152 TK_REINDEX98, TK_INDEXED116, TK_INDEX161, TK_DESC39, TK_ESCAPE58,
169153 TK_EACH41, TK_CHECK124, TK_KEY67, TK_BEFORE33, TK_FOREIGN132,
169154 TK_FOR62, TK_IGNORE63, TK_LIKE_KW47, TK_EXPLAIN2, TK_INSTEAD65,
169155 TK_ADD163, TK_DATABASE38, TK_AS24, TK_SELECT138, TK_TABLE16,
169156 TK_JOIN_KW118, TK_THEN159, TK_END11, TK_DEFERRABLE131, TK_ELSE160,
169157 TK_EXCLUDE91, TK_DELETE128, TK_TEMP21, TK_TEMP21, TK_OR43,
169158 TK_ISNULL50, TK_NULLS82, TK_SAVEPOINT13, TK_INTERSECT137, TK_TIES94,
169159 TK_NOTNULL51, TK_NOT19, TK_NO66, TK_NULL121, TK_LIKE_KW47,
169160 TK_EXCEPT136, TK_TRANSACTION6,TK_ACTION28, TK_ON115, TK_JOIN_KW118,
169161 TK_ALTER162, TK_RAISE71, TK_EXCLUSIVE9, TK_EXISTS20, TK_CONSTRAINT119,
169162 TK_INTO151, TK_OFFSET69, TK_OF68, TK_SET130, TK_TRIGGER77,
169163 TK_RANGE89, TK_GENERATED95, TK_DETACH40, TK_HAVING147, TK_LIKE_KW47,
169164 TK_BEGIN5, TK_JOIN_KW118, TK_REFERENCES125, TK_UNIQUE123, TK_QUERY3,
169165 TK_WITHOUT26, TK_WITH81, TK_JOIN_KW118, TK_RELEASE14, TK_ATTACH32,
169166 TK_BETWEEN48, TK_NOTHING152, TK_GROUPS92, TK_GROUP146, TK_CASCADE35,
169167 TK_ASC31, TK_DEFAULT120, TK_CASE157, TK_COLLATE113, TK_CREATE17,
169168 TK_CTIME_KW100, TK_IMMEDIATE8, TK_JOIN143, TK_INSERT127, TK_MATCH46,
169169 TK_PLAN4, TK_ANALYZE30, TK_PRAGMA70, TK_MATERIALIZED97, TK_DEFERRED7,
169170 TK_DISTINCT140, TK_IS45, TK_UPDATE129, TK_VALUES139, TK_VIRTUAL80,
169171 TK_ALWAYS96, TK_WHEN158, TK_WHERE149, TK_RECURSIVE72, TK_ABORT27,
169172 TK_AFTER29, TK_RENAME99, TK_AND44, TK_DROP133, TK_PARTITION87,
169173 TK_AUTOINCR126, TK_TO15, TK_IN49, TK_CAST36, TK_COLUMNKW60,
169174 TK_COMMIT10, TK_CONFLICT37, TK_JOIN_KW118, TK_CTIME_KW100, TK_CTIME_KW100,
169175 TK_CURRENT85, TK_PRECEDING88, TK_FAIL42, TK_LAST84, TK_FILTER166,
169176 TK_REPLACE73, TK_FIRST83, TK_FOLLOWING86, TK_FROM142, TK_JOIN_KW118,
169177 TK_LIMIT148, TK_IF18, TK_ORDER145, TK_RESTRICT74, TK_OTHERS93,
169178 TK_OVER165, TK_RETURNING150, TK_JOIN_KW118, TK_ROLLBACK12, TK_ROWS76,
169179 TK_ROW75, TK_UNBOUNDED90, TK_UNION134, TK_USING144, TK_VACUUM78,
169180 TK_VIEW79, TK_WINDOW164, TK_DO61, TK_BY34, TK_INITIALLY64,
169181 TK_ALL135, TK_PRIMARY122,
169182};
169183/* Hash table decoded:
169184** 0: INSERT
169185** 1: IS
169186** 2: ROLLBACK TRIGGER
169187** 3: IMMEDIATE
169188** 4: PARTITION
169189** 5: TEMP
169190** 6:
169191** 7:
169192** 8: VALUES WITHOUT
169193** 9:
169194** 10: MATCH
169195** 11: NOTHING
169196** 12:
169197** 13: OF
169198** 14: TIES IGNORE
169199** 15: PLAN
169200** 16: INSTEAD INDEXED
169201** 17:
169202** 18: TRANSACTION RIGHT
169203** 19: WHEN
169204** 20: SET HAVING
169205** 21: MATERIALIZED IF
169206** 22: ROWS
169207** 23: SELECT
169208** 24:
169209** 25:
169210** 26: VACUUM SAVEPOINT
169211** 27:
169212** 28: LIKE UNION VIRTUAL REFERENCES
169213** 29: RESTRICT
169214** 30:
169215** 31: THEN REGEXP
169216** 32: TO
169217** 33:
169218** 34: BEFORE
169219** 35:
169220** 36:
169221** 37: FOLLOWING COLLATE CASCADE
169222** 38: CREATE
169223** 39:
169224** 40: CASE REINDEX
169225** 41: EACH
169226** 42:
169227** 43: QUERY
169228** 44: AND ADD
169229** 45: PRIMARY ANALYZE
169230** 46:
169231** 47: ROW ASC DETACH
169232** 48: CURRENT_TIME CURRENT_DATE
169233** 49:
169234** 50:
169235** 51: EXCLUSIVE TEMPORARY
169236** 52:
169237** 53: DEFERRED
169238** 54: DEFERRABLE
169239** 55:
169240** 56: DATABASE
169241** 57:
169242** 58: DELETE VIEW GENERATED
169243** 59: ATTACH
169244** 60: END
169245** 61: EXCLUDE
169246** 62: ESCAPE DESC
169247** 63: GLOB
169248** 64: WINDOW ELSE
169249** 65: COLUMN
169250** 66: FIRST
169251** 67:
169252** 68: GROUPS ALL
169253** 69: DISTINCT DROP KEY
169254** 70: BETWEEN
169255** 71: INITIALLY
169256** 72: BEGIN
169257** 73: FILTER CHECK ACTION
169258** 74: GROUP INDEX
169259** 75:
169260** 76: EXISTS DEFAULT
169261** 77:
169262** 78: FOR CURRENT_TIMESTAMP
169263** 79: EXCEPT
169264** 80:
169265** 81: CROSS
169266** 82:
169267** 83:
169268** 84:
169269** 85: CAST
169270** 86: FOREIGN AUTOINCREMENT
169271** 87: COMMIT
169272** 88: CURRENT AFTER ALTER
169273** 89: FULL FAIL CONFLICT
169274** 90: EXPLAIN
169275** 91: CONSTRAINT
169276** 92: FROM ALWAYS
169277** 93:
169278** 94: ABORT
169279** 95:
169280** 96: AS DO
169281** 97: REPLACE WITH RELEASE
169282** 98: BY RENAME
169283** 99: RANGE RAISE
169284** 100: OTHERS
169285** 101: USING NULLS
169286** 102: PRAGMA
169287** 103: JOIN ISNULL OFFSET
169288** 104: NOT
169289** 105: OR LAST LEFT
169290** 106: LIMIT
169291** 107:
169292** 108:
169293** 109: IN
169294** 110: INTO
169295** 111: OVER RECURSIVE
169296** 112: ORDER OUTER
169297** 113:
169298** 114: INTERSECT UNBOUNDED
169299** 115:
169300** 116:
169301** 117: RETURNING ON
169302** 118:
169303** 119: WHERE
169304** 120: NO INNER
169305** 121: NULL
169306** 122:
169307** 123: TABLE
169308** 124: NATURAL NOTNULL
169309** 125: PRECEDING
169310** 126: UPDATE UNIQUE
169311*/
169312/* Check to see if z[0..n-1] is a keyword. If it is, write the
169313** parser symbol code for that keyword into *pType. Always
169314** return the integer n (the length of the token). */
169315static int keywordCode(const char *z, int n, int *pType){
169316 int i, j;
169317 const char *zKW;
169318 if( n>=2 ){
169319 i = ((charMap(z[0])sqlite3UpperToLower[(unsigned char)z[0]]*4) ^ (charMap(z[n-1])sqlite3UpperToLower[(unsigned char)z[n-1]]*3) ^ n*1) % 127;
169320 for(i=((int)aKWHash[i])-1; i>=0; i=((int)aKWNext[i])-1){
169321 if( aKWLen[i]!=n ) continue;
169322 zKW = &zKWText[aKWOffset[i]];
169323#ifdef SQLITE_ASCII1
169324 if( (z[0]&~0x20)!=zKW[0] ) continue;
169325 if( (z[1]&~0x20)!=zKW[1] ) continue;
169326 j = 2;
169327 while( j<n && (z[j]&~0x20)==zKW[j] ){ j++; }
169328#endif
169329#ifdef SQLITE_EBCDIC
169330 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;
169331 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;
169332 j = 2;
169333 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++; }
169334#endif
169335 if( j<n ) continue;
169336 testcase( i==0 ); /* REINDEX */
169337 testcase( i==1 ); /* INDEXED */
169338 testcase( i==2 ); /* INDEX */
169339 testcase( i==3 ); /* DESC */
169340 testcase( i==4 ); /* ESCAPE */
169341 testcase( i==5 ); /* EACH */
169342 testcase( i==6 ); /* CHECK */
169343 testcase( i==7 ); /* KEY */
169344 testcase( i==8 ); /* BEFORE */
169345 testcase( i==9 ); /* FOREIGN */
169346 testcase( i==10 ); /* FOR */
169347 testcase( i==11 ); /* IGNORE */
169348 testcase( i==12 ); /* REGEXP */
169349 testcase( i==13 ); /* EXPLAIN */
169350 testcase( i==14 ); /* INSTEAD */
169351 testcase( i==15 ); /* ADD */
169352 testcase( i==16 ); /* DATABASE */
169353 testcase( i==17 ); /* AS */
169354 testcase( i==18 ); /* SELECT */
169355 testcase( i==19 ); /* TABLE */
169356 testcase( i==20 ); /* LEFT */
169357 testcase( i==21 ); /* THEN */
169358 testcase( i==22 ); /* END */
169359 testcase( i==23 ); /* DEFERRABLE */
169360 testcase( i==24 ); /* ELSE */
169361 testcase( i==25 ); /* EXCLUDE */
169362 testcase( i==26 ); /* DELETE */
169363 testcase( i==27 ); /* TEMPORARY */
169364 testcase( i==28 ); /* TEMP */
169365 testcase( i==29 ); /* OR */
169366 testcase( i==30 ); /* ISNULL */
169367 testcase( i==31 ); /* NULLS */
169368 testcase( i==32 ); /* SAVEPOINT */
169369 testcase( i==33 ); /* INTERSECT */
169370 testcase( i==34 ); /* TIES */
169371 testcase( i==35 ); /* NOTNULL */
169372 testcase( i==36 ); /* NOT */
169373 testcase( i==37 ); /* NO */
169374 testcase( i==38 ); /* NULL */
169375 testcase( i==39 ); /* LIKE */
169376 testcase( i==40 ); /* EXCEPT */
169377 testcase( i==41 ); /* TRANSACTION */
169378 testcase( i==42 ); /* ACTION */
169379 testcase( i==43 ); /* ON */
169380 testcase( i==44 ); /* NATURAL */
169381 testcase( i==45 ); /* ALTER */
169382 testcase( i==46 ); /* RAISE */
169383 testcase( i==47 ); /* EXCLUSIVE */
169384 testcase( i==48 ); /* EXISTS */
169385 testcase( i==49 ); /* CONSTRAINT */
169386 testcase( i==50 ); /* INTO */
169387 testcase( i==51 ); /* OFFSET */
169388 testcase( i==52 ); /* OF */
169389 testcase( i==53 ); /* SET */
169390 testcase( i==54 ); /* TRIGGER */
169391 testcase( i==55 ); /* RANGE */
169392 testcase( i==56 ); /* GENERATED */
169393 testcase( i==57 ); /* DETACH */
169394 testcase( i==58 ); /* HAVING */
169395 testcase( i==59 ); /* GLOB */
169396 testcase( i==60 ); /* BEGIN */
169397 testcase( i==61 ); /* INNER */
169398 testcase( i==62 ); /* REFERENCES */
169399 testcase( i==63 ); /* UNIQUE */
169400 testcase( i==64 ); /* QUERY */
169401 testcase( i==65 ); /* WITHOUT */
169402 testcase( i==66 ); /* WITH */
169403 testcase( i==67 ); /* OUTER */
169404 testcase( i==68 ); /* RELEASE */
169405 testcase( i==69 ); /* ATTACH */
169406 testcase( i==70 ); /* BETWEEN */
169407 testcase( i==71 ); /* NOTHING */
169408 testcase( i==72 ); /* GROUPS */
169409 testcase( i==73 ); /* GROUP */
169410 testcase( i==74 ); /* CASCADE */
169411 testcase( i==75 ); /* ASC */
169412 testcase( i==76 ); /* DEFAULT */
169413 testcase( i==77 ); /* CASE */
169414 testcase( i==78 ); /* COLLATE */
169415 testcase( i==79 ); /* CREATE */
169416 testcase( i==80 ); /* CURRENT_DATE */
169417 testcase( i==81 ); /* IMMEDIATE */
169418 testcase( i==82 ); /* JOIN */
169419 testcase( i==83 ); /* INSERT */
169420 testcase( i==84 ); /* MATCH */
169421 testcase( i==85 ); /* PLAN */
169422 testcase( i==86 ); /* ANALYZE */
169423 testcase( i==87 ); /* PRAGMA */
169424 testcase( i==88 ); /* MATERIALIZED */
169425 testcase( i==89 ); /* DEFERRED */
169426 testcase( i==90 ); /* DISTINCT */
169427 testcase( i==91 ); /* IS */
169428 testcase( i==92 ); /* UPDATE */
169429 testcase( i==93 ); /* VALUES */
169430 testcase( i==94 ); /* VIRTUAL */
169431 testcase( i==95 ); /* ALWAYS */
169432 testcase( i==96 ); /* WHEN */
169433 testcase( i==97 ); /* WHERE */
169434 testcase( i==98 ); /* RECURSIVE */
169435 testcase( i==99 ); /* ABORT */
169436 testcase( i==100 ); /* AFTER */
169437 testcase( i==101 ); /* RENAME */
169438 testcase( i==102 ); /* AND */
169439 testcase( i==103 ); /* DROP */
169440 testcase( i==104 ); /* PARTITION */
169441 testcase( i==105 ); /* AUTOINCREMENT */
169442 testcase( i==106 ); /* TO */
169443 testcase( i==107 ); /* IN */
169444 testcase( i==108 ); /* CAST */
169445 testcase( i==109 ); /* COLUMN */
169446 testcase( i==110 ); /* COMMIT */
169447 testcase( i==111 ); /* CONFLICT */
169448 testcase( i==112 ); /* CROSS */
169449 testcase( i==113 ); /* CURRENT_TIMESTAMP */
169450 testcase( i==114 ); /* CURRENT_TIME */
169451 testcase( i==115 ); /* CURRENT */
169452 testcase( i==116 ); /* PRECEDING */
169453 testcase( i==117 ); /* FAIL */
169454 testcase( i==118 ); /* LAST */
169455 testcase( i==119 ); /* FILTER */
169456 testcase( i==120 ); /* REPLACE */
169457 testcase( i==121 ); /* FIRST */
169458 testcase( i==122 ); /* FOLLOWING */
169459 testcase( i==123 ); /* FROM */
169460 testcase( i==124 ); /* FULL */
169461 testcase( i==125 ); /* LIMIT */
169462 testcase( i==126 ); /* IF */
169463 testcase( i==127 ); /* ORDER */
169464 testcase( i==128 ); /* RESTRICT */
169465 testcase( i==129 ); /* OTHERS */
169466 testcase( i==130 ); /* OVER */
169467 testcase( i==131 ); /* RETURNING */
169468 testcase( i==132 ); /* RIGHT */
169469 testcase( i==133 ); /* ROLLBACK */
169470 testcase( i==134 ); /* ROWS */
169471 testcase( i==135 ); /* ROW */
169472 testcase( i==136 ); /* UNBOUNDED */
169473 testcase( i==137 ); /* UNION */
169474 testcase( i==138 ); /* USING */
169475 testcase( i==139 ); /* VACUUM */
169476 testcase( i==140 ); /* VIEW */
169477 testcase( i==141 ); /* WINDOW */
169478 testcase( i==142 ); /* DO */
169479 testcase( i==143 ); /* BY */
169480 testcase( i==144 ); /* INITIALLY */
169481 testcase( i==145 ); /* ALL */
169482 testcase( i==146 ); /* PRIMARY */
169483 *pType = aKWCode[i];
169484 break;
169485 }
169486 }
169487 return n;
169488}
169489SQLITE_PRIVATEstatic int sqlite3KeywordCode(const unsigned char *z, int n){
169490 int id = TK_ID59;
169491 keywordCode((char*)z, n, &id);
169492 return id;
169493}
169494#define SQLITE_N_KEYWORD147 147
169495SQLITE_API int sqlite3_keyword_name(int i,const char **pzName,int *pnName){
169496 if( i<0 || i>=SQLITE_N_KEYWORD147 ) return SQLITE_ERROR1;
169497 *pzName = zKWText + aKWOffset[i];
169498 *pnName = aKWLen[i];
169499 return SQLITE_OK0;
169500}
169501SQLITE_API int sqlite3_keyword_count(void){ return SQLITE_N_KEYWORD147; }
169502SQLITE_API int sqlite3_keyword_check(const char *zName, int nName){
169503 return TK_ID59!=sqlite3KeywordCode((const u8*)zName, nName);
169504}
169505
169506/************** End of keywordhash.h *****************************************/
169507/************** Continuing where we left off in tokenize.c *******************/
169508
169509
169510/*
169511** If X is a character that can be used in an identifier then
169512** IdChar(X) will be true. Otherwise it is false.
169513**
169514** For ASCII, any character with the high-order bit set is
169515** allowed in an identifier. For 7-bit characters,
169516** sqlite3IsIdChar[X] must be 1.
169517**
169518** For EBCDIC, the rules are more complex but have the same
169519** end result.
169520**
169521** Ticket #1066. the SQL standard does not allow '$' in the
169522** middle of identifiers. But many SQL implementations do.
169523** SQLite will allow '$' in identifiers for compatibility.
169524** But the feature is undocumented.
169525*/
169526#ifdef SQLITE_ASCII1
169527#define IdChar(C)((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
169528#endif
169529#ifdef SQLITE_EBCDIC
169530SQLITE_PRIVATEstatic const char sqlite3IsEbcdicIdChar[] = {
169531/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
169532 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */
169533 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */
169534 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */
169535 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */
169536 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */
169537 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */
169538 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */
169539 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
169540 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */
169541 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */
169542 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
169543 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
169544};
169545#define IdChar(C)((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
169546#endif
169547
169548/* Make the IdChar function accessible from ctime.c and alter.c */
169549SQLITE_PRIVATEstatic int sqlite3IsIdChar(u8 c){ return IdChar(c)((sqlite3CtypeMap[(unsigned char)c]&0x46)!=0); }
169550
169551#ifndef SQLITE_OMIT_WINDOWFUNC
169552/*
169553** Return the id of the next token in string (*pz). Before returning, set
169554** (*pz) to point to the byte following the parsed token.
169555*/
169556static int getToken(const unsigned char **pz){
169557 const unsigned char *z = *pz;
169558 int t; /* Token type to return */
169559 do {
169560 z += sqlite3GetToken(z, &t);
169561 }while( t==TK_SPACE183 );
169562 if( t==TK_ID59
169563 || t==TK_STRING117
169564 || t==TK_JOIN_KW118
169565 || t==TK_WINDOW164
169566 || t==TK_OVER165
169567 || sqlite3ParserFallback(t)==TK_ID59
169568 ){
169569 t = TK_ID59;
169570 }
169571 *pz = z;
169572 return t;
169573}
169574
169575/*
169576** The following three functions are called immediately after the tokenizer
169577** reads the keywords WINDOW, OVER and FILTER, respectively, to determine
169578** whether the token should be treated as a keyword or an SQL identifier.
169579** This cannot be handled by the usual lemon %fallback method, due to
169580** the ambiguity in some constructions. e.g.
169581**
169582** SELECT sum(x) OVER ...
169583**
169584** In the above, "OVER" might be a keyword, or it might be an alias for the
169585** sum(x) expression. If a "%fallback ID OVER" directive were added to
169586** grammar, then SQLite would always treat "OVER" as an alias, making it
169587** impossible to call a window-function without a FILTER clause.
169588**
169589** WINDOW is treated as a keyword if:
169590**
169591** * the following token is an identifier, or a keyword that can fallback
169592** to being an identifier, and
169593** * the token after than one is TK_AS.
169594**
169595** OVER is a keyword if:
169596**
169597** * the previous token was TK_RP, and
169598** * the next token is either TK_LP or an identifier.
169599**
169600** FILTER is a keyword if:
169601**
169602** * the previous token was TK_RP, and
169603** * the next token is TK_LP.
169604*/
169605static int analyzeWindowKeyword(const unsigned char *z){
169606 int t;
169607 t = getToken(&z);
169608 if( t!=TK_ID59 ) return TK_ID59;
169609 t = getToken(&z);
169610 if( t!=TK_AS24 ) return TK_ID59;
169611 return TK_WINDOW164;
169612}
169613static int analyzeOverKeyword(const unsigned char *z, int lastToken){
169614 if( lastToken==TK_RP23 ){
169615 int t = getToken(&z);
169616 if( t==TK_LP22 || t==TK_ID59 ) return TK_OVER165;
169617 }
169618 return TK_ID59;
169619}
169620static int analyzeFilterKeyword(const unsigned char *z, int lastToken){
169621 if( lastToken==TK_RP23 && getToken(&z)==TK_LP22 ){
169622 return TK_FILTER166;
169623 }
169624 return TK_ID59;
169625}
169626#endif /* SQLITE_OMIT_WINDOWFUNC */
169627
169628/*
169629** Return the length (in bytes) of the token that begins at z[0].
169630** Store the token type in *tokenType before returning.
169631*/
169632SQLITE_PRIVATEstatic int sqlite3GetToken(const unsigned char *z, int *tokenType){
169633 int i, c;
169634 switch( aiClass[*z] ){ /* Switch on the character-class of the first byte
169635 ** of the token. See the comment on the CC_ defines
169636 ** above. */
169637 case CC_SPACE7: {
169638 testcase( z[0]==' ' );
169639 testcase( z[0]=='\t' );
169640 testcase( z[0]=='\n' );
169641 testcase( z[0]=='\f' );
169642 testcase( z[0]=='\r' );
169643 for(i=1; sqlite3Isspace(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x01); i++){}
169644 *tokenType = TK_SPACE183;
169645 return i;
169646 }
169647 case CC_MINUS11: {
169648 if( z[1]=='-' ){
169649 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
169650 *tokenType = TK_SPACE183; /* IMP: R-22934-25134 */
169651 return i;
169652 }else if( z[1]=='>' ){
169653 *tokenType = TK_PTR112;
169654 return 2 + (z[2]=='>');
169655 }
169656 *tokenType = TK_MINUS107;
169657 return 1;
169658 }
169659 case CC_LP17: {
169660 *tokenType = TK_LP22;
169661 return 1;
169662 }
169663 case CC_RP18: {
169664 *tokenType = TK_RP23;
169665 return 1;
169666 }
169667 case CC_SEMI19: {
169668 *tokenType = TK_SEMI1;
169669 return 1;
169670 }
169671 case CC_PLUS20: {
169672 *tokenType = TK_PLUS106;
169673 return 1;
169674 }
169675 case CC_STAR21: {
169676 *tokenType = TK_STAR108;
169677 return 1;
169678 }
169679 case CC_SLASH16: {
169680 if( z[1]!='*' || z[2]==0 ){
169681 *tokenType = TK_SLASH109;
169682 return 1;
169683 }
169684 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
169685 if( c ) i++;
169686 *tokenType = TK_SPACE183; /* IMP: R-22934-25134 */
169687 return i;
169688 }
169689 case CC_PERCENT22: {
169690 *tokenType = TK_REM110;
169691 return 1;
169692 }
169693 case CC_EQ14: {
169694 *tokenType = TK_EQ53;
169695 return 1 + (z[1]=='=');
169696 }
169697 case CC_LT12: {
169698 if( (c=z[1])=='=' ){
169699 *tokenType = TK_LE55;
169700 return 2;
169701 }else if( c=='>' ){
169702 *tokenType = TK_NE52;
169703 return 2;
169704 }else if( c=='<' ){
169705 *tokenType = TK_LSHIFT104;
169706 return 2;
169707 }else{
169708 *tokenType = TK_LT56;
169709 return 1;
169710 }
169711 }
169712 case CC_GT13: {
169713 if( (c=z[1])=='=' ){
169714 *tokenType = TK_GE57;
169715 return 2;
169716 }else if( c=='>' ){
169717 *tokenType = TK_RSHIFT105;
169718 return 2;
169719 }else{
169720 *tokenType = TK_GT54;
169721 return 1;
169722 }
169723 }
169724 case CC_BANG15: {
169725 if( z[1]!='=' ){
169726 *tokenType = TK_ILLEGAL184;
169727 return 1;
169728 }else{
169729 *tokenType = TK_NE52;
169730 return 2;
169731 }
169732 }
169733 case CC_PIPE10: {
169734 if( z[1]!='|' ){
169735 *tokenType = TK_BITOR103;
169736 return 1;
169737 }else{
169738 *tokenType = TK_CONCAT111;
169739 return 2;
169740 }
169741 }
169742 case CC_COMMA23: {
169743 *tokenType = TK_COMMA25;
169744 return 1;
169745 }
169746 case CC_AND24: {
169747 *tokenType = TK_BITAND102;
169748 return 1;
169749 }
169750 case CC_TILDA25: {
169751 *tokenType = TK_BITNOT114;
169752 return 1;
169753 }
169754 case CC_QUOTE8: {
169755 int delim = z[0];
169756 testcase( delim=='`' );
169757 testcase( delim=='\'' );
169758 testcase( delim=='"' );
169759 for(i=1; (c=z[i])!=0; i++){
169760 if( c==delim ){
169761 if( z[i+1]==delim ){
169762 i++;
169763 }else{
169764 break;
169765 }
169766 }
169767 }
169768 if( c=='\'' ){
169769 *tokenType = TK_STRING117;
169770 return i+1;
169771 }else if( c!=0 ){
169772 *tokenType = TK_ID59;
169773 return i+1;
169774 }else{
169775 *tokenType = TK_ILLEGAL184;
169776 return i;
169777 }
169778 }
169779 case CC_DOT26: {
169780#ifndef SQLITE_OMIT_FLOATING_POINT
169781 if( !sqlite3Isdigit(z[1])(sqlite3CtypeMap[(unsigned char)(z[1])]&0x04) )
169782#endif
169783 {
169784 *tokenType = TK_DOT141;
169785 return 1;
169786 }
169787 /* If the next character is a digit, this is a floating point
169788 ** number that begins with ".". Fall thru into the next case */
169789 /* no break */ deliberate_fall_through
169790 }
169791 case CC_DIGIT3: {
169792 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
169793 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
169794 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
169795 testcase( z[0]=='9' );
169796 *tokenType = TK_INTEGER155;
169797#ifndef SQLITE_OMIT_HEX_INTEGER
169798 if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2])(sqlite3CtypeMap[(unsigned char)(z[2])]&0x08) ){
169799 for(i=3; sqlite3Isxdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x08); i++){}
169800 return i;
169801 }
169802#endif
169803 for(i=0; sqlite3Isdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x04); i++){}
169804#ifndef SQLITE_OMIT_FLOATING_POINT
169805 if( z[i]=='.' ){
169806 i++;
169807 while( sqlite3Isdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x04) ){ i++; }
169808 *tokenType = TK_FLOAT153;
169809 }
169810 if( (z[i]=='e' || z[i]=='E') &&
169811 ( sqlite3Isdigit(z[i+1])(sqlite3CtypeMap[(unsigned char)(z[i+1])]&0x04)
169812 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2])(sqlite3CtypeMap[(unsigned char)(z[i+2])]&0x04))
169813 )
169814 ){
169815 i += 2;
169816 while( sqlite3Isdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x04) ){ i++; }
169817 *tokenType = TK_FLOAT153;
169818 }
169819#endif
169820 while( IdChar(z[i])((sqlite3CtypeMap[(unsigned char)z[i]]&0x46)!=0) ){
169821 *tokenType = TK_ILLEGAL184;
169822 i++;
169823 }
169824 return i;
169825 }
169826 case CC_QUOTE29: {
169827 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
169828 *tokenType = c==']' ? TK_ID59 : TK_ILLEGAL184;
169829 return i;
169830 }
169831 case CC_VARNUM6: {
169832 *tokenType = TK_VARIABLE156;
169833 for(i=1; sqlite3Isdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x04); i++){}
169834 return i;
169835 }
169836 case CC_DOLLAR4:
169837 case CC_VARALPHA5: {
169838 int n = 0;
169839 testcase( z[0]=='$' ); testcase( z[0]=='@' );
169840 testcase( z[0]==':' ); testcase( z[0]=='#' );
169841 *tokenType = TK_VARIABLE156;
169842 for(i=1; (c=z[i])!=0; i++){
169843 if( IdChar(c)((sqlite3CtypeMap[(unsigned char)c]&0x46)!=0) ){
169844 n++;
169845#ifndef SQLITE_OMIT_TCL_VARIABLE
169846 }else if( c=='(' && n>0 ){
169847 do{
169848 i++;
169849 }while( (c=z[i])!=0 && !sqlite3Isspace(c)(sqlite3CtypeMap[(unsigned char)(c)]&0x01) && c!=')' );
169850 if( c==')' ){
169851 i++;
169852 }else{
169853 *tokenType = TK_ILLEGAL184;
169854 }
169855 break;
169856 }else if( c==':' && z[i+1]==':' ){
169857 i++;
169858#endif
169859 }else{
169860 break;
169861 }
169862 }
169863 if( n==0 ) *tokenType = TK_ILLEGAL184;
169864 return i;
169865 }
169866 case CC_KYWD01: {
169867 for(i=1; aiClass[z[i]]<=CC_KYWD2; i++){}
169868 if( IdChar(z[i])((sqlite3CtypeMap[(unsigned char)z[i]]&0x46)!=0) ){
169869 /* This token started out using characters that can appear in keywords,
169870 ** but z[i] is a character not allowed within keywords, so this must
169871 ** be an identifier instead */
169872 i++;
169873 break;
169874 }
169875 *tokenType = TK_ID59;
169876 return keywordCode((char*)z, i, tokenType);
169877 }
169878 case CC_X0: {
169879#ifndef SQLITE_OMIT_BLOB_LITERAL
169880 testcase( z[0]=='x' ); testcase( z[0]=='X' );
169881 if( z[1]=='\'' ){
169882 *tokenType = TK_BLOB154;
169883 for(i=2; sqlite3Isxdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x08); i++){}
169884 if( z[i]!='\'' || i%2 ){
169885 *tokenType = TK_ILLEGAL184;
169886 while( z[i] && z[i]!='\'' ){ i++; }
169887 }
169888 if( z[i] ) i++;
169889 return i;
169890 }
169891#endif
169892 /* If it is not a BLOB literal, then it must be an ID, since no
169893 ** SQL keywords start with the letter 'x'. Fall through */
169894 /* no break */ deliberate_fall_through
169895 }
169896 case CC_KYWD2:
169897 case CC_ID27: {
169898 i = 1;
169899 break;
169900 }
169901 case CC_BOM30: {
169902 if( z[1]==0xbb && z[2]==0xbf ){
169903 *tokenType = TK_SPACE183;
169904 return 3;
169905 }
169906 i = 1;
169907 break;
169908 }
169909 case CC_NUL29: {
169910 *tokenType = TK_ILLEGAL184;
169911 return 0;
169912 }
169913 default: {
169914 *tokenType = TK_ILLEGAL184;
169915 return 1;
169916 }
169917 }
169918 while( IdChar(z[i])((sqlite3CtypeMap[(unsigned char)z[i]]&0x46)!=0) ){ i++; }
169919 *tokenType = TK_ID59;
169920 return i;
169921}
169922
169923/*
169924** Run the parser on the given SQL string.
169925*/
169926SQLITE_PRIVATEstatic int sqlite3RunParser(Parse *pParse, const char *zSql){
169927 int nErr = 0; /* Number of errors encountered */
169928 void *pEngine; /* The LEMON-generated LALR(1) parser */
169929 int n = 0; /* Length of the next token token */
169930 int tokenType; /* type of the next token */
169931 int lastTokenParsed = -1; /* type of the previous token */
169932 sqlite3 *db = pParse->db; /* The database connection */
169933 int mxSqlLen; /* Max length of an SQL string */
169934 Parse *pParentParse = 0; /* Outer parse context, if any */
169935#ifdef sqlite3Parser_ENGINEALWAYSONSTACK1
169936 yyParser sEngine; /* Space to hold the Lemon-generated Parser object */
169937#endif
169938 VVA_ONLY( u8 startedWithOom = db->mallocFailed );
169939
169940 assert( zSql!=0 )((void) (0));
169941 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH1];
169942 if( db->nVdbeActive==0 ){
169943 AtomicStore(&db->u1.isInterrupted, 0)__atomic_store_n((&db->u1.isInterrupted),(0),0);
169944 }
169945 pParse->rc = SQLITE_OK0;
169946 pParse->zTail = zSql;
169947#ifdef SQLITE_DEBUG
169948 if( db->flags & SQLITE_ParserTrace ){
169949 printf("parser: [[[%s]]]\n", zSql);
169950 sqlite3ParserTrace(stdoutstdout, "parser: ");
169951 }else{
169952 sqlite3ParserTrace(0, 0);
169953 }
169954#endif
169955#ifdef sqlite3Parser_ENGINEALWAYSONSTACK1
169956 pEngine = &sEngine;
169957 sqlite3ParserInit(pEngine, pParse);
169958#else
169959 pEngine = sqlite3ParserAlloc(sqlite3Malloc, pParse);
169960 if( pEngine==0 ){
169961 sqlite3OomFault(db);
169962 return SQLITE_NOMEM_BKPT7;
169963 }
169964#endif
169965 assert( pParse->pNewTable==0 )((void) (0));
169966 assert( pParse->pNewTrigger==0 )((void) (0));
169967 assert( pParse->nVar==0 )((void) (0));
169968 assert( pParse->pVList==0 )((void) (0));
169969 pParentParse = db->pParse;
169970 db->pParse = pParse;
169971 while( 1 ){
169972 n = sqlite3GetToken((u8*)zSql, &tokenType);
169973 mxSqlLen -= n;
169974 if( mxSqlLen<0 ){
169975 pParse->rc = SQLITE_TOOBIG18;
169976 pParse->nErr++;
169977 break;
169978 }
169979#ifndef SQLITE_OMIT_WINDOWFUNC
169980 if( tokenType>=TK_WINDOW164 ){
169981 assert( tokenType==TK_SPACE || tokenType==TK_OVER || tokenType==TK_FILTER((void) (0))
169982 || tokenType==TK_ILLEGAL || tokenType==TK_WINDOW((void) (0))
169983 )((void) (0));
169984#else
169985 if( tokenType>=TK_SPACE183 ){
169986 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL )((void) (0));
169987#endif /* SQLITE_OMIT_WINDOWFUNC */
169988 if( AtomicLoad(&db->u1.isInterrupted)__atomic_load_n((&db->u1.isInterrupted),0) ){
169989 pParse->rc = SQLITE_INTERRUPT9;
169990 pParse->nErr++;
169991 break;
169992 }
169993 if( tokenType==TK_SPACE183 ){
169994 zSql += n;
169995 continue;
169996 }
169997 if( zSql[0]==0 ){
169998 /* Upon reaching the end of input, call the parser two more times
169999 ** with tokens TK_SEMI and 0, in that order. */
170000 if( lastTokenParsed==TK_SEMI1 ){
170001 tokenType = 0;
170002 }else if( lastTokenParsed==0 ){
170003 break;
170004 }else{
170005 tokenType = TK_SEMI1;
170006 }
170007 n = 0;
170008#ifndef SQLITE_OMIT_WINDOWFUNC
170009 }else if( tokenType==TK_WINDOW164 ){
170010 assert( n==6 )((void) (0));
170011 tokenType = analyzeWindowKeyword((const u8*)&zSql[6]);
170012 }else if( tokenType==TK_OVER165 ){
170013 assert( n==4 )((void) (0));
170014 tokenType = analyzeOverKeyword((const u8*)&zSql[4], lastTokenParsed);
170015 }else if( tokenType==TK_FILTER166 ){
170016 assert( n==6 )((void) (0));
170017 tokenType = analyzeFilterKeyword((const u8*)&zSql[6], lastTokenParsed);
170018#endif /* SQLITE_OMIT_WINDOWFUNC */
170019 }else{
170020 Token x;
170021 x.z = zSql;
170022 x.n = n;
170023 sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"", &x);
170024 break;
170025 }
170026 }
170027 pParse->sLastToken.z = zSql;
170028 pParse->sLastToken.n = n;
170029 sqlite3Parser(pEngine, tokenType, pParse->sLastToken);
170030 lastTokenParsed = tokenType;
170031 zSql += n;
170032 assert( db->mallocFailed==0 || pParse->rc!=SQLITE_OK || startedWithOom )((void) (0));
170033 if( pParse->rc!=SQLITE_OK0 ) break;
170034 }
170035 assert( nErr==0 )((void) (0));
170036#ifdef YYTRACKMAXSTACKDEPTH
170037 sqlite3_mutex_enter(sqlite3MallocMutex());
170038 sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK6,
170039 sqlite3ParserStackPeak(pEngine)
170040 );
170041 sqlite3_mutex_leave(sqlite3MallocMutex());
170042#endif /* YYDEBUG */
170043#ifdef sqlite3Parser_ENGINEALWAYSONSTACK1
170044 sqlite3ParserFinalize(pEngine);
170045#else
170046 sqlite3ParserFree(pEngine, sqlite3_free);
170047#endif
170048 if( db->mallocFailed ){
170049 pParse->rc = SQLITE_NOMEM_BKPT7;
170050 }
170051 if( pParse->zErrMsg || (pParse->rc!=SQLITE_OK0 && pParse->rc!=SQLITE_DONE101) ){
170052 if( pParse->zErrMsg==0 ){
170053 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
170054 }
170055 sqlite3_log(pParse->rc, "%s in \"%s\"", pParse->zErrMsg, pParse->zTail);
170056 nErr++;
170057 }
170058 pParse->zTail = zSql;
170059#ifndef SQLITE_OMIT_VIRTUALTABLE
170060 sqlite3_free(pParse->apVtabLock);
170061#endif
170062
170063 if( pParse->pNewTable && !IN_SPECIAL_PARSE(pParse->eParseMode!=0) ){
170064 /* If the pParse->declareVtab flag is set, do not delete any table
170065 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
170066 ** will take responsibility for freeing the Table structure.
170067 */
170068 sqlite3DeleteTable(db, pParse->pNewTable);
170069 }
170070 if( pParse->pNewTrigger && !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
170071 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
170072 }
170073 if( pParse->pVList ) sqlite3DbFreeNN(db, pParse->pVList);
170074 db->pParse = pParentParse;
170075 assert( nErr==0 || pParse->rc!=SQLITE_OK )((void) (0));
170076 return nErr;
170077}
170078
170079
170080#ifdef SQLITE_ENABLE_NORMALIZE
170081/*
170082** Insert a single space character into pStr if the current string
170083** ends with an identifier
170084*/
170085static void addSpaceSeparator(sqlite3_str *pStr){
170086 if( pStr->nChar && sqlite3IsIdChar(pStr->zText[pStr->nChar-1]) ){
170087 sqlite3_str_append(pStr, " ", 1);
170088 }
170089}
170090
170091/*
170092** Compute a normalization of the SQL given by zSql[0..nSql-1]. Return
170093** the normalization in space obtained from sqlite3DbMalloc(). Or return
170094** NULL if anything goes wrong or if zSql is NULL.
170095*/
170096SQLITE_PRIVATEstatic char *sqlite3Normalize(
170097 Vdbe *pVdbe, /* VM being reprepared */
170098 const char *zSql /* The original SQL string */
170099){
170100 sqlite3 *db; /* The database connection */
170101 int i; /* Next unread byte of zSql[] */
170102 int n; /* length of current token */
170103 int tokenType; /* type of current token */
170104 int prevType = 0; /* Previous non-whitespace token */
170105 int nParen; /* Number of nested levels of parentheses */
170106 int iStartIN; /* Start of RHS of IN operator in z[] */
170107 int nParenAtIN; /* Value of nParent at start of RHS of IN operator */
170108 u32 j; /* Bytes of normalized SQL generated so far */
170109 sqlite3_str *pStr; /* The normalized SQL string under construction */
170110
170111 db = sqlite3VdbeDb(pVdbe);
170112 tokenType = -1;
170113 nParen = iStartIN = nParenAtIN = 0;
170114 pStr = sqlite3_str_new(db);
170115 assert( pStr!=0 )((void) (0)); /* sqlite3_str_new() never returns NULL */
170116 for(i=0; zSql[i] && pStr->accError==0; i+=n){
170117 if( tokenType!=TK_SPACE183 ){
170118 prevType = tokenType;
170119 }
170120 n = sqlite3GetToken((unsigned char*)zSql+i, &tokenType);
170121 if( NEVER(n<=0)(n<=0) ) break;
170122 switch( tokenType ){
170123 case TK_SPACE183: {
170124 break;
170125 }
170126 case TK_NULL121: {
170127 if( prevType==TK_IS45 || prevType==TK_NOT19 ){
170128 sqlite3_str_append(pStr, " NULL", 5);
170129 break;
170130 }
170131 /* Fall through */
170132 }
170133 case TK_STRING117:
170134 case TK_INTEGER155:
170135 case TK_FLOAT153:
170136 case TK_VARIABLE156:
170137 case TK_BLOB154: {
170138 sqlite3_str_append(pStr, "?", 1);
170139 break;
170140 }
170141 case TK_LP22: {
170142 nParen++;
170143 if( prevType==TK_IN49 ){
170144 iStartIN = pStr->nChar;
170145 nParenAtIN = nParen;
170146 }
170147 sqlite3_str_append(pStr, "(", 1);
170148 break;
170149 }
170150 case TK_RP23: {
170151 if( iStartIN>0 && nParen==nParenAtIN ){
170152 assert( pStr->nChar>=(u32)iStartIN )((void) (0));
170153 pStr->nChar = iStartIN+1;
170154 sqlite3_str_append(pStr, "?,?,?", 5);
170155 iStartIN = 0;
170156 }
170157 nParen--;
170158 sqlite3_str_append(pStr, ")", 1);
170159 break;
170160 }
170161 case TK_ID59: {
170162 iStartIN = 0;
170163 j = pStr->nChar;
170164 if( sqlite3Isquote(zSql[i])(sqlite3CtypeMap[(unsigned char)(zSql[i])]&0x80) ){
170165 char *zId = sqlite3DbStrNDup(db, zSql+i, n);
170166 int nId;
170167 int eType = 0;
170168 if( zId==0 ) break;
170169 sqlite3Dequote(zId);
170170 if( zSql[i]=='"' && sqlite3VdbeUsesDoubleQuotedString(pVdbe, zId) ){
170171 sqlite3_str_append(pStr, "?", 1);
170172 sqlite3DbFree(db, zId);
170173 break;
170174 }
170175 nId = sqlite3Strlen30(zId);
170176 if( sqlite3GetToken((u8*)zId, &eType)==nId && eType==TK_ID59 ){
170177 addSpaceSeparator(pStr);
170178 sqlite3_str_append(pStr, zId, nId);
170179 }else{
170180 sqlite3_str_appendf(pStr, "\"%w\"", zId);
170181 }
170182 sqlite3DbFree(db, zId);
170183 }else{
170184 addSpaceSeparator(pStr);
170185 sqlite3_str_append(pStr, zSql+i, n);
170186 }
170187 while( j<pStr->nChar ){
170188 pStr->zText[j] = sqlite3Tolower(pStr->zText[j])(sqlite3UpperToLower[(unsigned char)(pStr->zText[j])]);
170189 j++;
170190 }
170191 break;
170192 }
170193 case TK_SELECT138: {
170194 iStartIN = 0;
170195 /* fall through */
170196 }
170197 default: {
170198 if( sqlite3IsIdChar(zSql[i]) ) addSpaceSeparator(pStr);
170199 j = pStr->nChar;
170200 sqlite3_str_append(pStr, zSql+i, n);
170201 while( j<pStr->nChar ){
170202 pStr->zText[j] = sqlite3Toupper(pStr->zText[j])((pStr->zText[j])&~(sqlite3CtypeMap[(unsigned char)(pStr
->zText[j])]&0x20))
;
170203 j++;
170204 }
170205 break;
170206 }
170207 }
170208 }
170209 if( tokenType!=TK_SEMI1 ) sqlite3_str_append(pStr, ";", 1);
170210 return sqlite3_str_finish(pStr);
170211}
170212#endif /* SQLITE_ENABLE_NORMALIZE */
170213
170214/************** End of tokenize.c ********************************************/
170215/************** Begin file complete.c ****************************************/
170216/*
170217** 2001 September 15
170218**
170219** The author disclaims copyright to this source code. In place of
170220** a legal notice, here is a blessing:
170221**
170222** May you do good and not evil.
170223** May you find forgiveness for yourself and forgive others.
170224** May you share freely, never taking more than you give.
170225**
170226*************************************************************************
170227** An tokenizer for SQL
170228**
170229** This file contains C code that implements the sqlite3_complete() API.
170230** This code used to be part of the tokenizer.c source file. But by
170231** separating it out, the code will be automatically omitted from
170232** static links that do not use it.
170233*/
170234/* #include "sqliteInt.h" */
170235#ifndef SQLITE_OMIT_COMPLETE
170236
170237/*
170238** This is defined in tokenize.c. We just have to import the definition.
170239*/
170240#ifndef SQLITE_AMALGAMATION1
170241#ifdef SQLITE_ASCII1
170242#define IdChar(C)((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
170243#endif
170244#ifdef SQLITE_EBCDIC
170245SQLITE_PRIVATEstatic const char sqlite3IsEbcdicIdChar[];
170246#define IdChar(C)((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
170247#endif
170248#endif /* SQLITE_AMALGAMATION */
170249
170250
170251/*
170252** Token types used by the sqlite3_complete() routine. See the header
170253** comments on that procedure for additional information.
170254*/
170255#define tkSEMI0 0
170256#define tkWS1 1
170257#define tkOTHER2 2
170258#ifndef SQLITE_OMIT_TRIGGER
170259#define tkEXPLAIN3 3
170260#define tkCREATE4 4
170261#define tkTEMP5 5
170262#define tkTRIGGER6 6
170263#define tkEND7 7
170264#endif
170265
170266/*
170267** Return TRUE if the given SQL string ends in a semicolon.
170268**
170269** Special handling is require for CREATE TRIGGER statements.
170270** Whenever the CREATE TRIGGER keywords are seen, the statement
170271** must end with ";END;".
170272**
170273** This implementation uses a state machine with 8 states:
170274**
170275** (0) INVALID We have not yet seen a non-whitespace character.
170276**
170277** (1) START At the beginning or end of an SQL statement. This routine
170278** returns 1 if it ends in the START state and 0 if it ends
170279** in any other state.
170280**
170281** (2) NORMAL We are in the middle of statement which ends with a single
170282** semicolon.
170283**
170284** (3) EXPLAIN The keyword EXPLAIN has been seen at the beginning of
170285** a statement.
170286**
170287** (4) CREATE The keyword CREATE has been seen at the beginning of a
170288** statement, possibly preceded by EXPLAIN and/or followed by
170289** TEMP or TEMPORARY
170290**
170291** (5) TRIGGER We are in the middle of a trigger definition that must be
170292** ended by a semicolon, the keyword END, and another semicolon.
170293**
170294** (6) SEMI We've seen the first semicolon in the ";END;" that occurs at
170295** the end of a trigger definition.
170296**
170297** (7) END We've seen the ";END" of the ";END;" that occurs at the end
170298** of a trigger definition.
170299**
170300** Transitions between states above are determined by tokens extracted
170301** from the input. The following tokens are significant:
170302**
170303** (0) tkSEMI A semicolon.
170304** (1) tkWS Whitespace.
170305** (2) tkOTHER Any other SQL token.
170306** (3) tkEXPLAIN The "explain" keyword.
170307** (4) tkCREATE The "create" keyword.
170308** (5) tkTEMP The "temp" or "temporary" keyword.
170309** (6) tkTRIGGER The "trigger" keyword.
170310** (7) tkEND The "end" keyword.
170311**
170312** Whitespace never causes a state transition and is always ignored.
170313** This means that a SQL string of all whitespace is invalid.
170314**
170315** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
170316** to recognize the end of a trigger can be omitted. All we have to do
170317** is look for a semicolon that is not part of an string or comment.
170318*/
170319SQLITE_API int sqlite3_complete(const char *zSql){
170320 u8 state = 0; /* Current state, using numbers defined in header comment */
170321 u8 token; /* Value of the next token */
170322
170323#ifndef SQLITE_OMIT_TRIGGER
170324 /* A complex statement machine used to detect the end of a CREATE TRIGGER
170325 ** statement. This is the normal case.
170326 */
170327 static const u8 trans[8][8] = {
170328 /* Token: */
170329 /* State: ** SEMI WS OTHER EXPLAIN CREATE TEMP TRIGGER END */
170330 /* 0 INVALID: */ { 1, 0, 2, 3, 4, 2, 2, 2, },
170331 /* 1 START: */ { 1, 1, 2, 3, 4, 2, 2, 2, },
170332 /* 2 NORMAL: */ { 1, 2, 2, 2, 2, 2, 2, 2, },
170333 /* 3 EXPLAIN: */ { 1, 3, 3, 2, 4, 2, 2, 2, },
170334 /* 4 CREATE: */ { 1, 4, 2, 2, 2, 4, 5, 2, },
170335 /* 5 TRIGGER: */ { 6, 5, 5, 5, 5, 5, 5, 5, },
170336 /* 6 SEMI: */ { 6, 6, 5, 5, 5, 5, 5, 7, },
170337 /* 7 END: */ { 1, 7, 5, 5, 5, 5, 5, 5, },
170338 };
170339#else
170340 /* If triggers are not supported by this compile then the statement machine
170341 ** used to detect the end of a statement is much simpler
170342 */
170343 static const u8 trans[3][3] = {
170344 /* Token: */
170345 /* State: ** SEMI WS OTHER */
170346 /* 0 INVALID: */ { 1, 0, 2, },
170347 /* 1 START: */ { 1, 1, 2, },
170348 /* 2 NORMAL: */ { 1, 2, 2, },
170349 };
170350#endif /* SQLITE_OMIT_TRIGGER */
170351
170352#ifdef SQLITE_ENABLE_API_ARMOR1
170353 if( zSql==0 ){
170354 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(170354);
170355 return 0;
170356 }
170357#endif
170358
170359 while( *zSql ){
170360 switch( *zSql ){
170361 case ';': { /* A semicolon */
170362 token = tkSEMI0;
170363 break;
170364 }
170365 case ' ':
170366 case '\r':
170367 case '\t':
170368 case '\n':
170369 case '\f': { /* White space is ignored */
170370 token = tkWS1;
170371 break;
170372 }
170373 case '/': { /* C-style comments */
170374 if( zSql[1]!='*' ){
170375 token = tkOTHER2;
170376 break;
170377 }
170378 zSql += 2;
170379 while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
170380 if( zSql[0]==0 ) return 0;
170381 zSql++;
170382 token = tkWS1;
170383 break;
170384 }
170385 case '-': { /* SQL-style comments from "--" to end of line */
170386 if( zSql[1]!='-' ){
170387 token = tkOTHER2;
170388 break;
170389 }
170390 while( *zSql && *zSql!='\n' ){ zSql++; }
170391 if( *zSql==0 ) return state==1;
170392 token = tkWS1;
170393 break;
170394 }
170395 case '[': { /* Microsoft-style identifiers in [...] */
170396 zSql++;
170397 while( *zSql && *zSql!=']' ){ zSql++; }
170398 if( *zSql==0 ) return 0;
170399 token = tkOTHER2;
170400 break;
170401 }
170402 case '`': /* Grave-accent quoted symbols used by MySQL */
170403 case '"': /* single- and double-quoted strings */
170404 case '\'': {
170405 int c = *zSql;
170406 zSql++;
170407 while( *zSql && *zSql!=c ){ zSql++; }
170408 if( *zSql==0 ) return 0;
170409 token = tkOTHER2;
170410 break;
170411 }
170412 default: {
170413#ifdef SQLITE_EBCDIC
170414 unsigned char c;
170415#endif
170416 if( IdChar((u8)*zSql)((sqlite3CtypeMap[(unsigned char)(u8)*zSql]&0x46)!=0) ){
170417 /* Keywords and unquoted identifiers */
170418 int nId;
170419 for(nId=1; IdChar(zSql[nId])((sqlite3CtypeMap[(unsigned char)zSql[nId]]&0x46)!=0); nId++){}
170420#ifdef SQLITE_OMIT_TRIGGER
170421 token = tkOTHER2;
170422#else
170423 switch( *zSql ){
170424 case 'c': case 'C': {
170425 if( nId==6 && sqlite3StrNICmpsqlite3_strnicmp(zSql, "create", 6)==0 ){
170426 token = tkCREATE4;
170427 }else{
170428 token = tkOTHER2;
170429 }
170430 break;
170431 }
170432 case 't': case 'T': {
170433 if( nId==7 && sqlite3StrNICmpsqlite3_strnicmp(zSql, "trigger", 7)==0 ){
170434 token = tkTRIGGER6;
170435 }else if( nId==4 && sqlite3StrNICmpsqlite3_strnicmp(zSql, "temp", 4)==0 ){
170436 token = tkTEMP5;
170437 }else if( nId==9 && sqlite3StrNICmpsqlite3_strnicmp(zSql, "temporary", 9)==0 ){
170438 token = tkTEMP5;
170439 }else{
170440 token = tkOTHER2;
170441 }
170442 break;
170443 }
170444 case 'e': case 'E': {
170445 if( nId==3 && sqlite3StrNICmpsqlite3_strnicmp(zSql, "end", 3)==0 ){
170446 token = tkEND7;
170447 }else
170448#ifndef SQLITE_OMIT_EXPLAIN
170449 if( nId==7 && sqlite3StrNICmpsqlite3_strnicmp(zSql, "explain", 7)==0 ){
170450 token = tkEXPLAIN3;
170451 }else
170452#endif
170453 {
170454 token = tkOTHER2;
170455 }
170456 break;
170457 }
170458 default: {
170459 token = tkOTHER2;
170460 break;
170461 }
170462 }
170463#endif /* SQLITE_OMIT_TRIGGER */
170464 zSql += nId-1;
170465 }else{
170466 /* Operators and special symbols */
170467 token = tkOTHER2;
170468 }
170469 break;
170470 }
170471 }
170472 state = trans[state][token];
170473 zSql++;
170474 }
170475 return state==1;
170476}
170477
170478#ifndef SQLITE_OMIT_UTF16
170479/*
170480** This routine is the same as the sqlite3_complete() routine described
170481** above, except that the parameter is required to be UTF-16 encoded, not
170482** UTF-8.
170483*/
170484SQLITE_API int sqlite3_complete16(const void *zSql){
170485 sqlite3_value *pVal;
170486 char const *zSql8;
170487 int rc;
170488
170489#ifndef SQLITE_OMIT_AUTOINIT
170490 rc = sqlite3_initialize();
170491 if( rc ) return rc;
170492#endif
170493 pVal = sqlite3ValueNew(0);
170494 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE2, SQLITE_STATIC((sqlite3_destructor_type)0));
170495 zSql8 = sqlite3ValueText(pVal, SQLITE_UTF81);
170496 if( zSql8 ){
170497 rc = sqlite3_complete(zSql8);
170498 }else{
170499 rc = SQLITE_NOMEM_BKPT7;
170500 }
170501 sqlite3ValueFree(pVal);
170502 return rc & 0xff;
170503}
170504#endif /* SQLITE_OMIT_UTF16 */
170505#endif /* SQLITE_OMIT_COMPLETE */
170506
170507/************** End of complete.c ********************************************/
170508/************** Begin file main.c ********************************************/
170509/*
170510** 2001 September 15
170511**
170512** The author disclaims copyright to this source code. In place of
170513** a legal notice, here is a blessing:
170514**
170515** May you do good and not evil.
170516** May you find forgiveness for yourself and forgive others.
170517** May you share freely, never taking more than you give.
170518**
170519*************************************************************************
170520** Main file for the SQLite library. The routines in this file
170521** implement the programmer interface to the library. Routines in
170522** other files are for internal use by SQLite and should not be
170523** accessed by users of the library.
170524*/
170525/* #include "sqliteInt.h" */
170526
170527#ifdef SQLITE_ENABLE_FTS31
170528/************** Include fts3.h in the middle of main.c ***********************/
170529/************** Begin file fts3.h ********************************************/
170530/*
170531** 2006 Oct 10
170532**
170533** The author disclaims copyright to this source code. In place of
170534** a legal notice, here is a blessing:
170535**
170536** May you do good and not evil.
170537** May you find forgiveness for yourself and forgive others.
170538** May you share freely, never taking more than you give.
170539**
170540******************************************************************************
170541**
170542** This header file is used by programs that want to link against the
170543** FTS3 library. All it does is declare the sqlite3Fts3Init() interface.
170544*/
170545/* #include "sqlite3.h" */
170546
170547#if 0
170548extern "C" {
170549#endif /* __cplusplus */
170550
170551SQLITE_PRIVATEstatic int sqlite3Fts3Init(sqlite3 *db);
170552
170553#if 0
170554} /* extern "C" */
170555#endif /* __cplusplus */
170556
170557/************** End of fts3.h ************************************************/
170558/************** Continuing where we left off in main.c ***********************/
170559#endif
170560#ifdef SQLITE_ENABLE_RTREE1
170561/************** Include rtree.h in the middle of main.c **********************/
170562/************** Begin file rtree.h *******************************************/
170563/*
170564** 2008 May 26
170565**
170566** The author disclaims copyright to this source code. In place of
170567** a legal notice, here is a blessing:
170568**
170569** May you do good and not evil.
170570** May you find forgiveness for yourself and forgive others.
170571** May you share freely, never taking more than you give.
170572**
170573******************************************************************************
170574**
170575** This header file is used by programs that want to link against the
170576** RTREE library. All it does is declare the sqlite3RtreeInit() interface.
170577*/
170578/* #include "sqlite3.h" */
170579
170580#ifdef SQLITE_OMIT_VIRTUALTABLE
170581# undef SQLITE_ENABLE_RTREE1
170582#endif
170583
170584#if 0
170585extern "C" {
170586#endif /* __cplusplus */
170587
170588SQLITE_PRIVATEstatic int sqlite3RtreeInit(sqlite3 *db);
170589
170590#if 0
170591} /* extern "C" */
170592#endif /* __cplusplus */
170593
170594/************** End of rtree.h ***********************************************/
170595/************** Continuing where we left off in main.c ***********************/
170596#endif
170597#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS)
170598/************** Include sqliteicu.h in the middle of main.c ******************/
170599/************** Begin file sqliteicu.h ***************************************/
170600/*
170601** 2008 May 26
170602**
170603** The author disclaims copyright to this source code. In place of
170604** a legal notice, here is a blessing:
170605**
170606** May you do good and not evil.
170607** May you find forgiveness for yourself and forgive others.
170608** May you share freely, never taking more than you give.
170609**
170610******************************************************************************
170611**
170612** This header file is used by programs that want to link against the
170613** ICU extension. All it does is declare the sqlite3IcuInit() interface.
170614*/
170615/* #include "sqlite3.h" */
170616
170617#if 0
170618extern "C" {
170619#endif /* __cplusplus */
170620
170621SQLITE_PRIVATEstatic int sqlite3IcuInit(sqlite3 *db);
170622
170623#if 0
170624} /* extern "C" */
170625#endif /* __cplusplus */
170626
170627/************** End of sqliteicu.h *******************************************/
170628/************** Continuing where we left off in main.c ***********************/
170629#endif
170630
170631/*
170632** This is an extension initializer that is a no-op and always
170633** succeeds, except that it fails if the fault-simulation is set
170634** to 500.
170635*/
170636static int sqlite3TestExtInit(sqlite3 *db){
170637 (void)db;
170638 return sqlite3FaultSim(500);
170639}
170640
170641
170642/*
170643** Forward declarations of external module initializer functions
170644** for modules that need them.
170645*/
170646#ifdef SQLITE_ENABLE_FTS1
170647SQLITE_PRIVATEstatic int sqlite3Fts1Init(sqlite3*);
170648#endif
170649#ifdef SQLITE_ENABLE_FTS2
170650SQLITE_PRIVATEstatic int sqlite3Fts2Init(sqlite3*);
170651#endif
170652#ifdef SQLITE_ENABLE_FTS51
170653SQLITE_PRIVATEstatic int sqlite3Fts5Init(sqlite3*);
170654#endif
170655#ifdef SQLITE_ENABLE_STMTVTAB
170656SQLITE_PRIVATEstatic int sqlite3StmtVtabInit(sqlite3*);
170657#endif
170658
170659/*
170660** An array of pointers to extension initializer functions for
170661** built-in extensions.
170662*/
170663static int (*const sqlite3BuiltinExtensions[])(sqlite3*) = {
170664#ifdef SQLITE_ENABLE_FTS1
170665 sqlite3Fts1Init,
170666#endif
170667#ifdef SQLITE_ENABLE_FTS2
170668 sqlite3Fts2Init,
170669#endif
170670#ifdef SQLITE_ENABLE_FTS31
170671 sqlite3Fts3Init,
170672#endif
170673#ifdef SQLITE_ENABLE_FTS51
170674 sqlite3Fts5Init,
170675#endif
170676#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS)
170677 sqlite3IcuInit,
170678#endif
170679#ifdef SQLITE_ENABLE_RTREE1
170680 sqlite3RtreeInit,
170681#endif
170682#ifdef SQLITE_ENABLE_DBPAGE_VTAB
170683 sqlite3DbpageRegister,
170684#endif
170685#ifdef SQLITE_ENABLE_DBSTAT_VTAB1
170686 sqlite3DbstatRegister,
170687#endif
170688 sqlite3TestExtInit,
170689#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
170690 sqlite3JsonTableFunctions,
170691#endif
170692#ifdef SQLITE_ENABLE_STMTVTAB
170693 sqlite3StmtVtabInit,
170694#endif
170695#ifdef SQLITE_ENABLE_BYTECODE_VTAB
170696 sqlite3VdbeBytecodeVtabInit,
170697#endif
170698};
170699
170700#ifndef SQLITE_AMALGAMATION1
170701/* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
170702** contains the text of SQLITE_VERSION macro.
170703*/
170704SQLITE_API const char sqlite3_version[] = SQLITE_VERSION"3.39.2";
170705#endif
170706
170707/* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
170708** a pointer to the to the sqlite3_version[] string constant.
170709*/
170710SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; }
170711
170712/* IMPLEMENTATION-OF: R-25063-23286 The sqlite3_sourceid() function returns a
170713** pointer to a string constant whose value is the same as the
170714** SQLITE_SOURCE_ID C preprocessor macro. Except if SQLite is built using
170715** an edited copy of the amalgamation, then the last four characters of
170716** the hash might be different from SQLITE_SOURCE_ID.
170717*/
170718/* SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } */
170719
170720/* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
170721** returns an integer equal to SQLITE_VERSION_NUMBER.
170722*/
170723SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER3039002; }
170724
170725/* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
170726** zero if and only if SQLite was compiled with mutexing code omitted due to
170727** the SQLITE_THREADSAFE compile-time option being set to 0.
170728*/
170729SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE2; }
170730
170731/*
170732** When compiling the test fixture or with debugging enabled (on Win32),
170733** this variable being set to non-zero will cause OSTRACE macros to emit
170734** extra diagnostic information.
170735*/
170736#ifdef SQLITE_HAVE_OS_TRACE
170737# ifndef SQLITE_DEBUG_OS_TRACE
170738# define SQLITE_DEBUG_OS_TRACE 0
170739# endif
170740 int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
170741#endif
170742
170743#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
170744/*
170745** If the following function pointer is not NULL and if
170746** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
170747** I/O active are written using this function. These messages
170748** are intended for debugging activity only.
170749*/
170750SQLITE_API void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...) = 0;
170751#endif
170752
170753/*
170754** If the following global variable points to a string which is the
170755** name of a directory, then that directory will be used to store
170756** temporary files.
170757**
170758** See also the "PRAGMA temp_store_directory" SQL command.
170759*/
170760SQLITE_API char *sqlite3_temp_directory = 0;
170761
170762/*
170763** If the following global variable points to a string which is the
170764** name of a directory, then that directory will be used to store
170765** all database files specified with a relative pathname.
170766**
170767** See also the "PRAGMA data_store_directory" SQL command.
170768*/
170769SQLITE_API char *sqlite3_data_directory = 0;
170770
170771/*
170772** Initialize SQLite.
170773**
170774** This routine must be called to initialize the memory allocation,
170775** VFS, and mutex subsystems prior to doing any serious work with
170776** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT
170777** this routine will be called automatically by key routines such as
170778** sqlite3_open().
170779**
170780** This routine is a no-op except on its very first call for the process,
170781** or for the first call after a call to sqlite3_shutdown.
170782**
170783** The first thread to call this routine runs the initialization to
170784** completion. If subsequent threads call this routine before the first
170785** thread has finished the initialization process, then the subsequent
170786** threads must block until the first thread finishes with the initialization.
170787**
170788** The first thread might call this routine recursively. Recursive
170789** calls to this routine should not block, of course. Otherwise the
170790** initialization process would never complete.
170791**
170792** Let X be the first thread to enter this routine. Let Y be some other
170793** thread. Then while the initial invocation of this routine by X is
170794** incomplete, it is required that:
170795**
170796** * Calls to this routine from Y must block until the outer-most
170797** call by X completes.
170798**
170799** * Recursive calls to this routine from thread X return immediately
170800** without blocking.
170801*/
170802SQLITE_API int sqlite3_initialize(void){
170803 MUTEX_LOGIC( sqlite3_mutex *pMainMtx; )sqlite3_mutex *pMainMtx; /* The main static mutex */
170804 int rc; /* Result code */
170805#ifdef SQLITE_EXTRA_INIT
170806 int bRunExtraInit = 0; /* Extra initialization needed */
170807#endif
170808
170809#ifdef SQLITE_OMIT_WSD
170810 rc = sqlite3_wsd_init(4096, 24);
170811 if( rc!=SQLITE_OK0 ){
170812 return rc;
170813 }
170814#endif
170815
170816 /* If the following assert() fails on some obscure processor/compiler
170817 ** combination, the work-around is to set the correct pointer
170818 ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
170819 assert( SQLITE_PTRSIZE==sizeof(char*) )((void) (0));
170820
170821 /* If SQLite is already completely initialized, then this call
170822 ** to sqlite3_initialize() should be a no-op. But the initialization
170823 ** must be complete. So isInit must not be set until the very end
170824 ** of this routine.
170825 */
170826 if( sqlite3GlobalConfigsqlite3Config.isInit ){
170827 sqlite3MemoryBarrier();
170828 return SQLITE_OK0;
170829 }
170830
170831 /* Make sure the mutex subsystem is initialized. If unable to
170832 ** initialize the mutex subsystem, return early with the error.
170833 ** If the system is so sick that we are unable to allocate a mutex,
170834 ** there is not much SQLite is going to be able to do.
170835 **
170836 ** The mutex subsystem must take care of serializing its own
170837 ** initialization.
170838 */
170839 rc = sqlite3MutexInit();
170840 if( rc ) return rc;
170841
170842 /* Initialize the malloc() system and the recursive pInitMutex mutex.
170843 ** This operation is protected by the STATIC_MAIN mutex. Note that
170844 ** MutexAlloc() is called for a static mutex prior to initializing the
170845 ** malloc subsystem - this implies that the allocation of a static
170846 ** mutex must not require support from the malloc subsystem.
170847 */
170848 MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )pMainMtx = sqlite3MutexAlloc(2);
170849 sqlite3_mutex_enter(pMainMtx);
170850 sqlite3GlobalConfigsqlite3Config.isMutexInit = 1;
170851 if( !sqlite3GlobalConfigsqlite3Config.isMallocInit ){
170852 rc = sqlite3MallocInit();
170853 }
170854 if( rc==SQLITE_OK0 ){
170855 sqlite3GlobalConfigsqlite3Config.isMallocInit = 1;
170856 if( !sqlite3GlobalConfigsqlite3Config.pInitMutex ){
170857 sqlite3GlobalConfigsqlite3Config.pInitMutex =
170858 sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE1);
170859 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex && !sqlite3GlobalConfigsqlite3Config.pInitMutex ){
170860 rc = SQLITE_NOMEM_BKPT7;
170861 }
170862 }
170863 }
170864 if( rc==SQLITE_OK0 ){
170865 sqlite3GlobalConfigsqlite3Config.nRefInitMutex++;
170866 }
170867 sqlite3_mutex_leave(pMainMtx);
170868
170869 /* If rc is not SQLITE_OK at this point, then either the malloc
170870 ** subsystem could not be initialized or the system failed to allocate
170871 ** the pInitMutex mutex. Return an error in either case. */
170872 if( rc!=SQLITE_OK0 ){
170873 return rc;
170874 }
170875
170876 /* Do the rest of the initialization under the recursive mutex so
170877 ** that we will be able to handle recursive calls into
170878 ** sqlite3_initialize(). The recursive calls normally come through
170879 ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
170880 ** recursive calls might also be possible.
170881 **
170882 ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
170883 ** to the xInit method, so the xInit method need not be threadsafe.
170884 **
170885 ** The following mutex is what serializes access to the appdef pcache xInit
170886 ** methods. The sqlite3_pcache_methods.xInit() all is embedded in the
170887 ** call to sqlite3PcacheInitialize().
170888 */
170889 sqlite3_mutex_enter(sqlite3GlobalConfigsqlite3Config.pInitMutex);
170890 if( sqlite3GlobalConfigsqlite3Config.isInit==0 && sqlite3GlobalConfigsqlite3Config.inProgress==0 ){
170891 sqlite3GlobalConfigsqlite3Config.inProgress = 1;
170892#ifdef SQLITE_ENABLE_SQLLOG
170893 {
170894 extern void sqlite3_init_sqllog(void);
170895 sqlite3_init_sqllog();
170896 }
170897#endif
170898 memset(&sqlite3BuiltinFunctions, 0, sizeof(sqlite3BuiltinFunctions));
170899 sqlite3RegisterBuiltinFunctions();
170900 if( sqlite3GlobalConfigsqlite3Config.isPCacheInit==0 ){
170901 rc = sqlite3PcacheInitialize();
170902 }
170903 if( rc==SQLITE_OK0 ){
170904 sqlite3GlobalConfigsqlite3Config.isPCacheInit = 1;
170905 rc = sqlite3OsInit();
170906 }
170907#ifndef SQLITE_OMIT_DESERIALIZE
170908 if( rc==SQLITE_OK0 ){
170909 rc = sqlite3MemdbInit();
170910 }
170911#endif
170912 if( rc==SQLITE_OK0 ){
170913 sqlite3PCacheBufferSetup( sqlite3GlobalConfigsqlite3Config.pPage,
170914 sqlite3GlobalConfigsqlite3Config.szPage, sqlite3GlobalConfigsqlite3Config.nPage);
170915 sqlite3MemoryBarrier();
170916 sqlite3GlobalConfigsqlite3Config.isInit = 1;
170917#ifdef SQLITE_EXTRA_INIT
170918 bRunExtraInit = 1;
170919#endif
170920 }
170921 sqlite3GlobalConfigsqlite3Config.inProgress = 0;
170922 }
170923 sqlite3_mutex_leave(sqlite3GlobalConfigsqlite3Config.pInitMutex);
170924
170925 /* Go back under the static mutex and clean up the recursive
170926 ** mutex to prevent a resource leak.
170927 */
170928 sqlite3_mutex_enter(pMainMtx);
170929 sqlite3GlobalConfigsqlite3Config.nRefInitMutex--;
170930 if( sqlite3GlobalConfigsqlite3Config.nRefInitMutex<=0 ){
170931 assert( sqlite3GlobalConfig.nRefInitMutex==0 )((void) (0));
170932 sqlite3_mutex_free(sqlite3GlobalConfigsqlite3Config.pInitMutex);
170933 sqlite3GlobalConfigsqlite3Config.pInitMutex = 0;
170934 }
170935 sqlite3_mutex_leave(pMainMtx);
170936
170937 /* The following is just a sanity check to make sure SQLite has
170938 ** been compiled correctly. It is important to run this code, but
170939 ** we don't want to run it too often and soak up CPU cycles for no
170940 ** reason. So we run it once during initialization.
170941 */
170942#ifndef NDEBUG1
170943#ifndef SQLITE_OMIT_FLOATING_POINT
170944 /* This section of code's only "output" is via assert() statements. */
170945 if( rc==SQLITE_OK0 ){
170946 u64 x = (((u64)1)<<63)-1;
170947 double y;
170948 assert(sizeof(x)==8)((void) (0));
170949 assert(sizeof(x)==sizeof(y))((void) (0));
170950 memcpy(&y, &x, 8);
170951 assert( sqlite3IsNaN(y) )((void) (0));
170952 }
170953#endif
170954#endif
170955
170956 /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
170957 ** compile-time option.
170958 */
170959#ifdef SQLITE_EXTRA_INIT
170960 if( bRunExtraInit ){
170961 int SQLITE_EXTRA_INIT(const char*);
170962 rc = SQLITE_EXTRA_INIT(0);
170963 }
170964#endif
170965
170966 return rc;
170967}
170968
170969/*
170970** Undo the effects of sqlite3_initialize(). Must not be called while
170971** there are outstanding database connections or memory allocations or
170972** while any part of SQLite is otherwise in use in any thread. This
170973** routine is not threadsafe. But it is safe to invoke this routine
170974** on when SQLite is already shut down. If SQLite is already shut down
170975** when this routine is invoked, then this routine is a harmless no-op.
170976*/
170977SQLITE_API int sqlite3_shutdown(void){
170978#ifdef SQLITE_OMIT_WSD
170979 int rc = sqlite3_wsd_init(4096, 24);
170980 if( rc!=SQLITE_OK0 ){
170981 return rc;
170982 }
170983#endif
170984
170985 if( sqlite3GlobalConfigsqlite3Config.isInit ){
170986#ifdef SQLITE_EXTRA_SHUTDOWN
170987 void SQLITE_EXTRA_SHUTDOWN(void);
170988 SQLITE_EXTRA_SHUTDOWN();
170989#endif
170990 sqlite3_os_end();
170991 sqlite3_reset_auto_extension();
170992 sqlite3GlobalConfigsqlite3Config.isInit = 0;
170993 }
170994 if( sqlite3GlobalConfigsqlite3Config.isPCacheInit ){
170995 sqlite3PcacheShutdown();
170996 sqlite3GlobalConfigsqlite3Config.isPCacheInit = 0;
170997 }
170998 if( sqlite3GlobalConfigsqlite3Config.isMallocInit ){
170999 sqlite3MallocEnd();
171000 sqlite3GlobalConfigsqlite3Config.isMallocInit = 0;
171001
171002#ifndef SQLITE_OMIT_SHUTDOWN_DIRECTORIES
171003 /* The heap subsystem has now been shutdown and these values are supposed
171004 ** to be NULL or point to memory that was obtained from sqlite3_malloc(),
171005 ** which would rely on that heap subsystem; therefore, make sure these
171006 ** values cannot refer to heap memory that was just invalidated when the
171007 ** heap subsystem was shutdown. This is only done if the current call to
171008 ** this function resulted in the heap subsystem actually being shutdown.
171009 */
171010 sqlite3_data_directory = 0;
171011 sqlite3_temp_directory = 0;
171012#endif
171013 }
171014 if( sqlite3GlobalConfigsqlite3Config.isMutexInit ){
171015 sqlite3MutexEnd();
171016 sqlite3GlobalConfigsqlite3Config.isMutexInit = 0;
171017 }
171018
171019 return SQLITE_OK0;
171020}
171021
171022/*
171023** This API allows applications to modify the global configuration of
171024** the SQLite library at run-time.
171025**
171026** This routine should only be called when there are no outstanding
171027** database connections or memory allocations. This routine is not
171028** threadsafe. Failure to heed these warnings can lead to unpredictable
171029** behavior.
171030*/
171031SQLITE_API int sqlite3_config(int op, ...){
171032 va_list ap;
171033 int rc = SQLITE_OK0;
171034
171035 /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
171036 ** the SQLite library is in use. */
171037 if( sqlite3GlobalConfigsqlite3Config.isInit ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(171037);
171038
171039 va_start(ap, op)__builtin_va_start(ap, op);
171040 switch( op ){
171041
171042 /* Mutex configuration options are only available in a threadsafe
171043 ** compile.
171044 */
171045#if defined(SQLITE_THREADSAFE2) && SQLITE_THREADSAFE2>0 /* IMP: R-54466-46756 */
171046 case SQLITE_CONFIG_SINGLETHREAD1: {
171047 /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to
171048 ** Single-thread. */
171049 sqlite3GlobalConfigsqlite3Config.bCoreMutex = 0; /* Disable mutex on core */
171050 sqlite3GlobalConfigsqlite3Config.bFullMutex = 0; /* Disable mutex on connections */
171051 break;
171052 }
171053#endif
171054#if defined(SQLITE_THREADSAFE2) && SQLITE_THREADSAFE2>0 /* IMP: R-20520-54086 */
171055 case SQLITE_CONFIG_MULTITHREAD2: {
171056 /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to
171057 ** Multi-thread. */
171058 sqlite3GlobalConfigsqlite3Config.bCoreMutex = 1; /* Enable mutex on core */
171059 sqlite3GlobalConfigsqlite3Config.bFullMutex = 0; /* Disable mutex on connections */
171060 break;
171061 }
171062#endif
171063#if defined(SQLITE_THREADSAFE2) && SQLITE_THREADSAFE2>0 /* IMP: R-59593-21810 */
171064 case SQLITE_CONFIG_SERIALIZED3: {
171065 /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to
171066 ** Serialized. */
171067 sqlite3GlobalConfigsqlite3Config.bCoreMutex = 1; /* Enable mutex on core */
171068 sqlite3GlobalConfigsqlite3Config.bFullMutex = 1; /* Enable mutex on connections */
171069 break;
171070 }
171071#endif
171072#if defined(SQLITE_THREADSAFE2) && SQLITE_THREADSAFE2>0 /* IMP: R-63666-48755 */
171073 case SQLITE_CONFIG_MUTEX10: {
171074 /* Specify an alternative mutex implementation */
171075 sqlite3GlobalConfigsqlite3Config.mutex = *va_arg(ap, sqlite3_mutex_methods*)__builtin_va_arg(ap, sqlite3_mutex_methods*);
171076 break;
171077 }
171078#endif
171079#if defined(SQLITE_THREADSAFE2) && SQLITE_THREADSAFE2>0 /* IMP: R-14450-37597 */
171080 case SQLITE_CONFIG_GETMUTEX11: {
171081 /* Retrieve the current mutex implementation */
171082 *va_arg(ap, sqlite3_mutex_methods*)__builtin_va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfigsqlite3Config.mutex;
171083 break;
171084 }
171085#endif
171086
171087 case SQLITE_CONFIG_MALLOC4: {
171088 /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a
171089 ** single argument which is a pointer to an instance of the
171090 ** sqlite3_mem_methods structure. The argument specifies alternative
171091 ** low-level memory allocation routines to be used in place of the memory
171092 ** allocation routines built into SQLite. */
171093 sqlite3GlobalConfigsqlite3Config.m = *va_arg(ap, sqlite3_mem_methods*)__builtin_va_arg(ap, sqlite3_mem_methods*);
171094 break;
171095 }
171096 case SQLITE_CONFIG_GETMALLOC5: {
171097 /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a
171098 ** single argument which is a pointer to an instance of the
171099 ** sqlite3_mem_methods structure. The sqlite3_mem_methods structure is
171100 ** filled with the currently defined memory allocation routines. */
171101 if( sqlite3GlobalConfigsqlite3Config.m.xMalloc==0 ) sqlite3MemSetDefault();
171102 *va_arg(ap, sqlite3_mem_methods*)__builtin_va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfigsqlite3Config.m;
171103 break;
171104 }
171105 case SQLITE_CONFIG_MEMSTATUS9: {
171106 /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
171107 ** single argument of type int, interpreted as a boolean, which enables
171108 ** or disables the collection of memory allocation statistics. */
171109 sqlite3GlobalConfigsqlite3Config.bMemstat = va_arg(ap, int)__builtin_va_arg(ap, int);
171110 break;
171111 }
171112 case SQLITE_CONFIG_SMALL_MALLOC27: {
171113 sqlite3GlobalConfigsqlite3Config.bSmallMalloc = va_arg(ap, int)__builtin_va_arg(ap, int);
171114 break;
171115 }
171116 case SQLITE_CONFIG_PAGECACHE7: {
171117 /* EVIDENCE-OF: R-18761-36601 There are three arguments to
171118 ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem),
171119 ** the size of each page cache line (sz), and the number of cache lines
171120 ** (N). */
171121 sqlite3GlobalConfigsqlite3Config.pPage = va_arg(ap, void*)__builtin_va_arg(ap, void*);
171122 sqlite3GlobalConfigsqlite3Config.szPage = va_arg(ap, int)__builtin_va_arg(ap, int);
171123 sqlite3GlobalConfigsqlite3Config.nPage = va_arg(ap, int)__builtin_va_arg(ap, int);
171124 break;
171125 }
171126 case SQLITE_CONFIG_PCACHE_HDRSZ24: {
171127 /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
171128 ** a single parameter which is a pointer to an integer and writes into
171129 ** that integer the number of extra bytes per page required for each page
171130 ** in SQLITE_CONFIG_PAGECACHE. */
171131 *va_arg(ap, int*)__builtin_va_arg(ap, int*) =
171132 sqlite3HeaderSizeBtree() +
171133 sqlite3HeaderSizePcache() +
171134 sqlite3HeaderSizePcache1();
171135 break;
171136 }
171137
171138 case SQLITE_CONFIG_PCACHE14: {
171139 /* no-op */
171140 break;
171141 }
171142 case SQLITE_CONFIG_GETPCACHE15: {
171143 /* now an error */
171144 rc = SQLITE_ERROR1;
171145 break;
171146 }
171147
171148 case SQLITE_CONFIG_PCACHE218: {
171149 /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
171150 ** single argument which is a pointer to an sqlite3_pcache_methods2
171151 ** object. This object specifies the interface to a custom page cache
171152 ** implementation. */
171153 sqlite3GlobalConfigsqlite3Config.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*)__builtin_va_arg(ap, sqlite3_pcache_methods2*);
171154 break;
171155 }
171156 case SQLITE_CONFIG_GETPCACHE219: {
171157 /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
171158 ** single argument which is a pointer to an sqlite3_pcache_methods2
171159 ** object. SQLite copies of the current page cache implementation into
171160 ** that object. */
171161 if( sqlite3GlobalConfigsqlite3Config.pcache2.xInit==0 ){
171162 sqlite3PCacheSetDefault();
171163 }
171164 *va_arg(ap, sqlite3_pcache_methods2*)__builtin_va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfigsqlite3Config.pcache2;
171165 break;
171166 }
171167
171168/* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only
171169** available if SQLite is compiled with either SQLITE_ENABLE_MEMSYS3 or
171170** SQLITE_ENABLE_MEMSYS5 and returns SQLITE_ERROR if invoked otherwise. */
171171#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
171172 case SQLITE_CONFIG_HEAP8: {
171173 /* EVIDENCE-OF: R-19854-42126 There are three arguments to
171174 ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the
171175 ** number of bytes in the memory buffer, and the minimum allocation size.
171176 */
171177 sqlite3GlobalConfigsqlite3Config.pHeap = va_arg(ap, void*)__builtin_va_arg(ap, void*);
171178 sqlite3GlobalConfigsqlite3Config.nHeap = va_arg(ap, int)__builtin_va_arg(ap, int);
171179 sqlite3GlobalConfigsqlite3Config.mnReq = va_arg(ap, int)__builtin_va_arg(ap, int);
171180
171181 if( sqlite3GlobalConfigsqlite3Config.mnReq<1 ){
171182 sqlite3GlobalConfigsqlite3Config.mnReq = 1;
171183 }else if( sqlite3GlobalConfigsqlite3Config.mnReq>(1<<12) ){
171184 /* cap min request size at 2^12 */
171185 sqlite3GlobalConfigsqlite3Config.mnReq = (1<<12);
171186 }
171187
171188 if( sqlite3GlobalConfigsqlite3Config.pHeap==0 ){
171189 /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer)
171190 ** is NULL, then SQLite reverts to using its default memory allocator
171191 ** (the system malloc() implementation), undoing any prior invocation of
171192 ** SQLITE_CONFIG_MALLOC.
171193 **
171194 ** Setting sqlite3GlobalConfig.m to all zeros will cause malloc to
171195 ** revert to its default implementation when sqlite3_initialize() is run
171196 */
171197 memset(&sqlite3GlobalConfigsqlite3Config.m, 0, sizeof(sqlite3GlobalConfigsqlite3Config.m));
171198 }else{
171199 /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the
171200 ** alternative memory allocator is engaged to handle all of SQLites
171201 ** memory allocation needs. */
171202#ifdef SQLITE_ENABLE_MEMSYS3
171203 sqlite3GlobalConfigsqlite3Config.m = *sqlite3MemGetMemsys3();
171204#endif
171205#ifdef SQLITE_ENABLE_MEMSYS5
171206 sqlite3GlobalConfigsqlite3Config.m = *sqlite3MemGetMemsys5();
171207#endif
171208 }
171209 break;
171210 }
171211#endif
171212
171213 case SQLITE_CONFIG_LOOKASIDE13: {
171214 sqlite3GlobalConfigsqlite3Config.szLookaside = va_arg(ap, int)__builtin_va_arg(ap, int);
171215 sqlite3GlobalConfigsqlite3Config.nLookaside = va_arg(ap, int)__builtin_va_arg(ap, int);
171216 break;
171217 }
171218
171219 /* Record a pointer to the logger function and its first argument.
171220 ** The default is NULL. Logging is disabled if the function pointer is
171221 ** NULL.
171222 */
171223 case SQLITE_CONFIG_LOG16: {
171224 /* MSVC is picky about pulling func ptrs from va lists.
171225 ** http://support.microsoft.com/kb/47961
171226 ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));
171227 */
171228 typedef void(*LOGFUNC_t)(void*,int,const char*);
171229 sqlite3GlobalConfigsqlite3Config.xLog = va_arg(ap, LOGFUNC_t)__builtin_va_arg(ap, LOGFUNC_t);
171230 sqlite3GlobalConfigsqlite3Config.pLogArg = va_arg(ap, void*)__builtin_va_arg(ap, void*);
171231 break;
171232 }
171233
171234 /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames
171235 ** can be changed at start-time using the
171236 ** sqlite3_config(SQLITE_CONFIG_URI,1) or
171237 ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls.
171238 */
171239 case SQLITE_CONFIG_URI17: {
171240 /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single
171241 ** argument of type int. If non-zero, then URI handling is globally
171242 ** enabled. If the parameter is zero, then URI handling is globally
171243 ** disabled. */
171244 sqlite3GlobalConfigsqlite3Config.bOpenUri = va_arg(ap, int)__builtin_va_arg(ap, int);
171245 break;
171246 }
171247
171248 case SQLITE_CONFIG_COVERING_INDEX_SCAN20: {
171249 /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN
171250 ** option takes a single integer argument which is interpreted as a
171251 ** boolean in order to enable or disable the use of covering indices for
171252 ** full table scans in the query optimizer. */
171253 sqlite3GlobalConfigsqlite3Config.bUseCis = va_arg(ap, int)__builtin_va_arg(ap, int);
171254 break;
171255 }
171256
171257#ifdef SQLITE_ENABLE_SQLLOG
171258 case SQLITE_CONFIG_SQLLOG21: {
171259 typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int);
171260 sqlite3GlobalConfigsqlite3Config.xSqllog = va_arg(ap, SQLLOGFUNC_t)__builtin_va_arg(ap, SQLLOGFUNC_t);
171261 sqlite3GlobalConfigsqlite3Config.pSqllogArg = va_arg(ap, void *)__builtin_va_arg(ap, void *);
171262 break;
171263 }
171264#endif
171265
171266 case SQLITE_CONFIG_MMAP_SIZE22: {
171267 /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit
171268 ** integer (sqlite3_int64) values that are the default mmap size limit
171269 ** (the default setting for PRAGMA mmap_size) and the maximum allowed
171270 ** mmap size limit. */
171271 sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64)__builtin_va_arg(ap, sqlite3_int64);
171272 sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64)__builtin_va_arg(ap, sqlite3_int64);
171273 /* EVIDENCE-OF: R-53367-43190 If either argument to this option is
171274 ** negative, then that argument is changed to its compile-time default.
171275 **
171276 ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be
171277 ** silently truncated if necessary so that it does not exceed the
171278 ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE
171279 ** compile-time option.
171280 */
171281 if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE20971520 ){
171282 mxMmap = SQLITE_MAX_MMAP_SIZE20971520;
171283 }
171284 if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE0;
171285 if( szMmap>mxMmap) szMmap = mxMmap;
171286 sqlite3GlobalConfigsqlite3Config.mxMmap = mxMmap;
171287 sqlite3GlobalConfigsqlite3Config.szMmap = szMmap;
171288 break;
171289 }
171290
171291#if SQLITE_OS_WIN0 && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */
171292 case SQLITE_CONFIG_WIN32_HEAPSIZE23: {
171293 /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit
171294 ** unsigned integer value that specifies the maximum size of the created
171295 ** heap. */
171296 sqlite3GlobalConfigsqlite3Config.nHeap = va_arg(ap, int)__builtin_va_arg(ap, int);
171297 break;
171298 }
171299#endif
171300
171301 case SQLITE_CONFIG_PMASZ25: {
171302 sqlite3GlobalConfigsqlite3Config.szPma = va_arg(ap, unsigned int)__builtin_va_arg(ap, unsigned int);
171303 break;
171304 }
171305
171306 case SQLITE_CONFIG_STMTJRNL_SPILL26: {
171307 sqlite3GlobalConfigsqlite3Config.nStmtSpill = va_arg(ap, int)__builtin_va_arg(ap, int);
171308 break;
171309 }
171310
171311#ifdef SQLITE_ENABLE_SORTER_REFERENCES
171312 case SQLITE_CONFIG_SORTERREF_SIZE28: {
171313 int iVal = va_arg(ap, int)__builtin_va_arg(ap, int);
171314 if( iVal<0 ){
171315 iVal = SQLITE_DEFAULT_SORTERREF_SIZE0x7fffffff;
171316 }
171317 sqlite3GlobalConfigsqlite3Config.szSorterRef = (u32)iVal;
171318 break;
171319 }
171320#endif /* SQLITE_ENABLE_SORTER_REFERENCES */
171321
171322#ifndef SQLITE_OMIT_DESERIALIZE
171323 case SQLITE_CONFIG_MEMDB_MAXSIZE29: {
171324 sqlite3GlobalConfigsqlite3Config.mxMemdbSize = va_arg(ap, sqlite3_int64)__builtin_va_arg(ap, sqlite3_int64);
171325 break;
171326 }
171327#endif /* SQLITE_OMIT_DESERIALIZE */
171328
171329 default: {
171330 rc = SQLITE_ERROR1;
171331 break;
171332 }
171333 }
171334 va_end(ap)__builtin_va_end(ap);
171335 return rc;
171336}
171337
171338/*
171339** Set up the lookaside buffers for a database connection.
171340** Return SQLITE_OK on success.
171341** If lookaside is already active, return SQLITE_BUSY.
171342**
171343** The sz parameter is the number of bytes in each lookaside slot.
171344** The cnt parameter is the number of slots. If pStart is NULL the
171345** space for the lookaside memory is obtained from sqlite3_malloc().
171346** If pStart is not NULL then it is sz*cnt bytes of memory to use for
171347** the lookaside memory.
171348*/
171349static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
171350#ifndef SQLITE_OMIT_LOOKASIDE
171351 void *pStart;
171352 sqlite3_int64 szAlloc = sz*(sqlite3_int64)cnt;
171353 int nBig; /* Number of full-size slots */
171354 int nSm; /* Number smaller LOOKASIDE_SMALL-byte slots */
171355
171356 if( sqlite3LookasideUsed(db,0)>0 ){
171357 return SQLITE_BUSY5;
171358 }
171359 /* Free any existing lookaside buffer for this handle before
171360 ** allocating a new one so we don't have to have space for
171361 ** both at the same time.
171362 */
171363 if( db->lookaside.bMalloced ){
171364 sqlite3_free(db->lookaside.pStart);
171365 }
171366 /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger
171367 ** than a pointer to be useful.
171368 */
171369 sz = ROUNDDOWN8(sz)((sz)&~7); /* IMP: R-33038-09382 */
171370 if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
171371 if( cnt<0 ) cnt = 0;
171372 if( sz==0 || cnt==0 ){
171373 sz = 0;
171374 pStart = 0;
171375 }else if( pBuf==0 ){
171376 sqlite3BeginBenignMalloc();
171377 pStart = sqlite3Malloc( szAlloc ); /* IMP: R-61949-35727 */
171378 sqlite3EndBenignMalloc();
171379 if( pStart ) szAlloc = sqlite3MallocSize(pStart);
171380 }else{
171381 pStart = pBuf;
171382 }
171383#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
171384 if( sz>=LOOKASIDE_SMALL128*3 ){
171385 nBig = szAlloc/(3*LOOKASIDE_SMALL128+sz);
171386 nSm = (szAlloc - sz*nBig)/LOOKASIDE_SMALL128;
171387 }else if( sz>=LOOKASIDE_SMALL128*2 ){
171388 nBig = szAlloc/(LOOKASIDE_SMALL128+sz);
171389 nSm = (szAlloc - sz*nBig)/LOOKASIDE_SMALL128;
171390 }else
171391#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
171392 if( sz>0 ){
171393 nBig = szAlloc/sz;
171394 nSm = 0;
171395 }else{
171396 nBig = nSm = 0;
171397 }
171398 db->lookaside.pStart = pStart;
171399 db->lookaside.pInit = 0;
171400 db->lookaside.pFree = 0;
171401 db->lookaside.sz = (u16)sz;
171402 db->lookaside.szTrue = (u16)sz;
171403 if( pStart ){
171404 int i;
171405 LookasideSlot *p;
171406 assert( sz > (int)sizeof(LookasideSlot*) )((void) (0));
171407 p = (LookasideSlot*)pStart;
171408 for(i=0; i<nBig; i++){
171409 p->pNext = db->lookaside.pInit;
171410 db->lookaside.pInit = p;
171411 p = (LookasideSlot*)&((u8*)p)[sz];
171412 }
171413#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
171414 db->lookaside.pSmallInit = 0;
171415 db->lookaside.pSmallFree = 0;
171416 db->lookaside.pMiddle = p;
171417 for(i=0; i<nSm; i++){
171418 p->pNext = db->lookaside.pSmallInit;
171419 db->lookaside.pSmallInit = p;
171420 p = (LookasideSlot*)&((u8*)p)[LOOKASIDE_SMALL128];
171421 }
171422#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
171423 assert( ((uptr)p)<=szAlloc + (uptr)pStart )((void) (0));
171424 db->lookaside.pEnd = p;
171425 db->lookaside.bDisable = 0;
171426 db->lookaside.bMalloced = pBuf==0 ?1:0;
171427 db->lookaside.nSlot = nBig+nSm;
171428 }else{
171429 db->lookaside.pStart = db;
171430#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
171431 db->lookaside.pSmallInit = 0;
171432 db->lookaside.pSmallFree = 0;
171433 db->lookaside.pMiddle = db;
171434#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
171435 db->lookaside.pEnd = db;
171436 db->lookaside.bDisable = 1;
171437 db->lookaside.sz = 0;
171438 db->lookaside.bMalloced = 0;
171439 db->lookaside.nSlot = 0;
171440 }
171441 assert( sqlite3LookasideUsed(db,0)==0 )((void) (0));
171442#endif /* SQLITE_OMIT_LOOKASIDE */
171443 return SQLITE_OK0;
171444}
171445
171446/*
171447** Return the mutex associated with a database connection.
171448*/
171449SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
171450#ifdef SQLITE_ENABLE_API_ARMOR1
171451 if( !sqlite3SafetyCheckOk(db) ){
171452 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(171452);
171453 return 0;
171454 }
171455#endif
171456 return db->mutex;
171457}
171458
171459/*
171460** Free up as much memory as we can from the given database
171461** connection.
171462*/
171463SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){
171464 int i;
171465
171466#ifdef SQLITE_ENABLE_API_ARMOR1
171467 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(171467);
171468#endif
171469 sqlite3_mutex_enter(db->mutex);
171470 sqlite3BtreeEnterAll(db);
171471 for(i=0; i<db->nDb; i++){
171472 Btree *pBt = db->aDb[i].pBt;
171473 if( pBt ){
171474 Pager *pPager = sqlite3BtreePager(pBt);
171475 sqlite3PagerShrink(pPager);
171476 }
171477 }
171478 sqlite3BtreeLeaveAll(db);
171479 sqlite3_mutex_leave(db->mutex);
171480 return SQLITE_OK0;
171481}
171482
171483/*
171484** Flush any dirty pages in the pager-cache for any attached database
171485** to disk.
171486*/
171487SQLITE_API int sqlite3_db_cacheflush(sqlite3 *db){
171488 int i;
171489 int rc = SQLITE_OK0;
171490 int bSeenBusy = 0;
171491
171492#ifdef SQLITE_ENABLE_API_ARMOR1
171493 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(171493);
171494#endif
171495 sqlite3_mutex_enter(db->mutex);
171496 sqlite3BtreeEnterAll(db);
171497 for(i=0; rc==SQLITE_OK0 && i<db->nDb; i++){
171498 Btree *pBt = db->aDb[i].pBt;
171499 if( pBt && sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE2 ){
171500 Pager *pPager = sqlite3BtreePager(pBt);
171501 rc = sqlite3PagerFlush(pPager);
171502 if( rc==SQLITE_BUSY5 ){
171503 bSeenBusy = 1;
171504 rc = SQLITE_OK0;
171505 }
171506 }
171507 }
171508 sqlite3BtreeLeaveAll(db);
171509 sqlite3_mutex_leave(db->mutex);
171510 return ((rc==SQLITE_OK0 && bSeenBusy) ? SQLITE_BUSY5 : rc);
171511}
171512
171513/*
171514** Configuration settings for an individual database connection
171515*/
171516SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
171517 va_list ap;
171518 int rc;
171519 va_start(ap, op)__builtin_va_start(ap, op);
171520 switch( op ){
171521 case SQLITE_DBCONFIG_MAINDBNAME1000: {
171522 /* IMP: R-06824-28531 */
171523 /* IMP: R-36257-52125 */
171524 db->aDb[0].zDbSName = va_arg(ap,char*)__builtin_va_arg(ap, char*);
171525 rc = SQLITE_OK0;
171526 break;
171527 }
171528 case SQLITE_DBCONFIG_LOOKASIDE1001: {
171529 void *pBuf = va_arg(ap, void*)__builtin_va_arg(ap, void*); /* IMP: R-26835-10964 */
171530 int sz = va_arg(ap, int)__builtin_va_arg(ap, int); /* IMP: R-47871-25994 */
171531 int cnt = va_arg(ap, int)__builtin_va_arg(ap, int); /* IMP: R-04460-53386 */
171532 rc = setupLookaside(db, pBuf, sz, cnt);
171533 break;
171534 }
171535 default: {
171536 static const struct {
171537 int op; /* The opcode */
171538 u32 mask; /* Mask of the bit in sqlite3.flags to set/clear */
171539 } aFlagOp[] = {
171540 { SQLITE_DBCONFIG_ENABLE_FKEY1002, SQLITE_ForeignKeys0x00004000 },
171541 { SQLITE_DBCONFIG_ENABLE_TRIGGER1003, SQLITE_EnableTrigger0x00040000 },
171542 { SQLITE_DBCONFIG_ENABLE_VIEW1015, SQLITE_EnableView0x80000000 },
171543 { SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER1004, SQLITE_Fts3Tokenizer0x00400000 },
171544 { SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION1005, SQLITE_LoadExtension0x00010000 },
171545 { SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE1006, SQLITE_NoCkptOnClose0x00000800 },
171546 { SQLITE_DBCONFIG_ENABLE_QPSG1007, SQLITE_EnableQPSG0x00800000 },
171547 { SQLITE_DBCONFIG_TRIGGER_EQP1008, SQLITE_TriggerEQP0x01000000 },
171548 { SQLITE_DBCONFIG_RESET_DATABASE1009, SQLITE_ResetDatabase0x02000000 },
171549 { SQLITE_DBCONFIG_DEFENSIVE1010, SQLITE_Defensive0x10000000 },
171550 { SQLITE_DBCONFIG_WRITABLE_SCHEMA1011, SQLITE_WriteSchema0x00000001|
171551 SQLITE_NoSchemaError0x08000000 },
171552 { SQLITE_DBCONFIG_LEGACY_ALTER_TABLE1012, SQLITE_LegacyAlter0x04000000 },
171553 { SQLITE_DBCONFIG_DQS_DDL1014, SQLITE_DqsDDL0x20000000 },
171554 { SQLITE_DBCONFIG_DQS_DML1013, SQLITE_DqsDML0x40000000 },
171555 { SQLITE_DBCONFIG_LEGACY_FILE_FORMAT1016, SQLITE_LegacyFileFmt0x00000002 },
171556 { SQLITE_DBCONFIG_TRUSTED_SCHEMA1017, SQLITE_TrustedSchema0x00000080 },
171557 };
171558 unsigned int i;
171559 rc = SQLITE_ERROR1; /* IMP: R-42790-23372 */
171560 for(i=0; i<ArraySize(aFlagOp)((int)(sizeof(aFlagOp)/sizeof(aFlagOp[0]))); i++){
171561 if( aFlagOp[i].op==op ){
171562 int onoff = va_arg(ap, int)__builtin_va_arg(ap, int);
171563 int *pRes = va_arg(ap, int*)__builtin_va_arg(ap, int*);
171564 u64 oldFlags = db->flags;
171565 if( onoff>0 ){
171566 db->flags |= aFlagOp[i].mask;
171567 }else if( onoff==0 ){
171568 db->flags &= ~(u64)aFlagOp[i].mask;
171569 }
171570 if( oldFlags!=db->flags ){
171571 sqlite3ExpirePreparedStatements(db, 0);
171572 }
171573 if( pRes ){
171574 *pRes = (db->flags & aFlagOp[i].mask)!=0;
171575 }
171576 rc = SQLITE_OK0;
171577 break;
171578 }
171579 }
171580 break;
171581 }
171582 }
171583 va_end(ap)__builtin_va_end(ap);
171584 return rc;
171585}
171586
171587/*
171588** This is the default collating function named "BINARY" which is always
171589** available.
171590*/
171591static int binCollFunc(
171592 void *NotUsed,
171593 int nKey1, const void *pKey1,
171594 int nKey2, const void *pKey2
171595){
171596 int rc, n;
171597 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
171598 n = nKey1<nKey2 ? nKey1 : nKey2;
171599 /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares
171600 ** strings byte by byte using the memcmp() function from the standard C
171601 ** library. */
171602 assert( pKey1 && pKey2 )((void) (0));
171603 rc = memcmp(pKey1, pKey2, n);
171604 if( rc==0 ){
171605 rc = nKey1 - nKey2;
171606 }
171607 return rc;
171608}
171609
171610/*
171611** This is the collating function named "RTRIM" which is always
171612** available. Ignore trailing spaces.
171613*/
171614static int rtrimCollFunc(
171615 void *pUser,
171616 int nKey1, const void *pKey1,
171617 int nKey2, const void *pKey2
171618){
171619 const u8 *pK1 = (const u8*)pKey1;
171620 const u8 *pK2 = (const u8*)pKey2;
171621 while( nKey1 && pK1[nKey1-1]==' ' ) nKey1--;
171622 while( nKey2 && pK2[nKey2-1]==' ' ) nKey2--;
171623 return binCollFunc(pUser, nKey1, pKey1, nKey2, pKey2);
171624}
171625
171626/*
171627** Return true if CollSeq is the default built-in BINARY.
171628*/
171629SQLITE_PRIVATEstatic int sqlite3IsBinary(const CollSeq *p){
171630 assert( p==0 || p->xCmp!=binCollFunc || strcmp(p->zName,"BINARY")==0 )((void) (0));
171631 return p==0 || p->xCmp==binCollFunc;
171632}
171633
171634/*
171635** Another built-in collating sequence: NOCASE.
171636**
171637** This collating sequence is intended to be used for "case independent
171638** comparison". SQLite's knowledge of upper and lower case equivalents
171639** extends only to the 26 characters used in the English language.
171640**
171641** At the moment there is only a UTF-8 implementation.
171642*/
171643static int nocaseCollatingFunc(
171644 void *NotUsed,
171645 int nKey1, const void *pKey1,
171646 int nKey2, const void *pKey2
171647){
171648 int r = sqlite3StrNICmpsqlite3_strnicmp(
171649 (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
171650 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
171651 if( 0==r ){
171652 r = nKey1-nKey2;
171653 }
171654 return r;
171655}
171656
171657/*
171658** Return the ROWID of the most recent insert
171659*/
171660SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
171661#ifdef SQLITE_ENABLE_API_ARMOR1
171662 if( !sqlite3SafetyCheckOk(db) ){
171663 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(171663);
171664 return 0;
171665 }
171666#endif
171667 return db->lastRowid;
171668}
171669
171670/*
171671** Set the value returned by the sqlite3_last_insert_rowid() API function.
171672*/
171673SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3 *db, sqlite3_int64 iRowid){
171674#ifdef SQLITE_ENABLE_API_ARMOR1
171675 if( !sqlite3SafetyCheckOk(db) ){
171676 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(171676);
171677 return;
171678 }
171679#endif
171680 sqlite3_mutex_enter(db->mutex);
171681 db->lastRowid = iRowid;
171682 sqlite3_mutex_leave(db->mutex);
171683}
171684
171685/*
171686** Return the number of changes in the most recent call to sqlite3_exec().
171687*/
171688SQLITE_API sqlite3_int64 sqlite3_changes64(sqlite3 *db){
171689#ifdef SQLITE_ENABLE_API_ARMOR1
171690 if( !sqlite3SafetyCheckOk(db) ){
171691 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(171691);
171692 return 0;
171693 }
171694#endif
171695 return db->nChange;
171696}
171697SQLITE_API int sqlite3_changes(sqlite3 *db){
171698 return (int)sqlite3_changes64(db);
171699}
171700
171701/*
171702** Return the number of changes since the database handle was opened.
171703*/
171704SQLITE_API sqlite3_int64 sqlite3_total_changes64(sqlite3 *db){
171705#ifdef SQLITE_ENABLE_API_ARMOR1
171706 if( !sqlite3SafetyCheckOk(db) ){
171707 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(171707);
171708 return 0;
171709 }
171710#endif
171711 return db->nTotalChange;
171712}
171713SQLITE_API int sqlite3_total_changes(sqlite3 *db){
171714 return (int)sqlite3_total_changes64(db);
171715}
171716
171717/*
171718** Close all open savepoints. This function only manipulates fields of the
171719** database handle object, it does not close any savepoints that may be open
171720** at the b-tree/pager level.
171721*/
171722SQLITE_PRIVATEstatic void sqlite3CloseSavepoints(sqlite3 *db){
171723 while( db->pSavepoint ){
171724 Savepoint *pTmp = db->pSavepoint;
171725 db->pSavepoint = pTmp->pNext;
171726 sqlite3DbFree(db, pTmp);
171727 }
171728 db->nSavepoint = 0;
171729 db->nStatement = 0;
171730 db->isTransactionSavepoint = 0;
171731}
171732
171733/*
171734** Invoke the destructor function associated with FuncDef p, if any. Except,
171735** if this is not the last copy of the function, do not invoke it. Multiple
171736** copies of a single function are created when create_function() is called
171737** with SQLITE_ANY as the encoding.
171738*/
171739static void functionDestroy(sqlite3 *db, FuncDef *p){
171740 FuncDestructor *pDestructor;
171741 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 )((void) (0));
171742 pDestructor = p->u.pDestructor;
171743 if( pDestructor ){
171744 pDestructor->nRef--;
171745 if( pDestructor->nRef==0 ){
171746 pDestructor->xDestroy(pDestructor->pUserData);
171747 sqlite3DbFree(db, pDestructor);
171748 }
171749 }
171750}
171751
171752/*
171753** Disconnect all sqlite3_vtab objects that belong to database connection
171754** db. This is called when db is being closed.
171755*/
171756static void disconnectAllVtab(sqlite3 *db){
171757#ifndef SQLITE_OMIT_VIRTUALTABLE
171758 int i;
171759 HashElem *p;
171760 sqlite3BtreeEnterAll(db);
171761 for(i=0; i<db->nDb; i++){
171762 Schema *pSchema = db->aDb[i].pSchema;
171763 if( pSchema ){
171764 for(p=sqliteHashFirst(&pSchema->tblHash)((&pSchema->tblHash)->first); p; p=sqliteHashNext(p)((p)->next)){
171765 Table *pTab = (Table *)sqliteHashData(p)((p)->data);
171766 if( IsVirtual(pTab)((pTab)->eTabType==1) ) sqlite3VtabDisconnect(db, pTab);
171767 }
171768 }
171769 }
171770 for(p=sqliteHashFirst(&db->aModule)((&db->aModule)->first); p; p=sqliteHashNext(p)((p)->next)){
171771 Module *pMod = (Module *)sqliteHashData(p)((p)->data);
171772 if( pMod->pEpoTab ){
171773 sqlite3VtabDisconnect(db, pMod->pEpoTab);
171774 }
171775 }
171776 sqlite3VtabUnlockList(db);
171777 sqlite3BtreeLeaveAll(db);
171778#else
171779 UNUSED_PARAMETER(db)(void)(db);
171780#endif
171781}
171782
171783/*
171784** Return TRUE if database connection db has unfinalized prepared
171785** statements or unfinished sqlite3_backup objects.
171786*/
171787static int connectionIsBusy(sqlite3 *db){
171788 int j;
171789 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
171790 if( db->pVdbe ) return 1;
171791 for(j=0; j<db->nDb; j++){
171792 Btree *pBt = db->aDb[j].pBt;
171793 if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1;
171794 }
171795 return 0;
171796}
171797
171798/*
171799** Close an existing SQLite database
171800*/
171801static int sqlite3Close(sqlite3 *db, int forceZombie){
171802 if( !db ){
171803 /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or
171804 ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */
171805 return SQLITE_OK0;
171806 }
171807 if( !sqlite3SafetyCheckSickOrOk(db) ){
171808 return SQLITE_MISUSE_BKPTsqlite3MisuseError(171808);
171809 }
171810 sqlite3_mutex_enter(db->mutex);
171811 if( db->mTrace & SQLITE_TRACE_CLOSE0x08 ){
171812 db->trace.xV2(SQLITE_TRACE_CLOSE0x08, db->pTraceArg, db, 0);
171813 }
171814
171815 /* Force xDisconnect calls on all virtual tables */
171816 disconnectAllVtab(db);
171817
171818 /* If a transaction is open, the disconnectAllVtab() call above
171819 ** will not have called the xDisconnect() method on any virtual
171820 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
171821 ** call will do so. We need to do this before the check for active
171822 ** SQL statements below, as the v-table implementation may be storing
171823 ** some prepared statements internally.
171824 */
171825 sqlite3VtabRollback(db);
171826
171827 /* Legacy behavior (sqlite3_close() behavior) is to return
171828 ** SQLITE_BUSY if the connection can not be closed immediately.
171829 */
171830 if( !forceZombie && connectionIsBusy(db) ){
171831 sqlite3ErrorWithMsg(db, SQLITE_BUSY5, "unable to close due to unfinalized "
171832 "statements or unfinished backups");
171833 sqlite3_mutex_leave(db->mutex);
171834 return SQLITE_BUSY5;
171835 }
171836
171837#ifdef SQLITE_ENABLE_SQLLOG
171838 if( sqlite3GlobalConfigsqlite3Config.xSqllog ){
171839 /* Closing the handle. Fourth parameter is passed the value 2. */
171840 sqlite3GlobalConfigsqlite3Config.xSqllog(sqlite3GlobalConfigsqlite3Config.pSqllogArg, db, 0, 2);
171841 }
171842#endif
171843
171844 /* Convert the connection into a zombie and then close it.
171845 */
171846 db->eOpenState = SQLITE_STATE_ZOMBIE0xa7;
171847 sqlite3LeaveMutexAndCloseZombie(db);
171848 return SQLITE_OK0;
171849}
171850
171851/*
171852** Return the transaction state for a single databse, or the maximum
171853** transaction state over all attached databases if zSchema is null.
171854*/
171855SQLITE_API int sqlite3_txn_state(sqlite3 *db, const char *zSchema){
171856 int iDb, nDb;
171857 int iTxn = -1;
171858#ifdef SQLITE_ENABLE_API_ARMOR1
171859 if( !sqlite3SafetyCheckOk(db) ){
171860 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(171860);
171861 return -1;
171862 }
171863#endif
171864 sqlite3_mutex_enter(db->mutex);
171865 if( zSchema ){
171866 nDb = iDb = sqlite3FindDbName(db, zSchema);
171867 if( iDb<0 ) nDb--;
171868 }else{
171869 iDb = 0;
171870 nDb = db->nDb-1;
171871 }
171872 for(; iDb<=nDb; iDb++){
171873 Btree *pBt = db->aDb[iDb].pBt;
171874 int x = pBt!=0 ? sqlite3BtreeTxnState(pBt) : SQLITE_TXN_NONE0;
171875 if( x>iTxn ) iTxn = x;
171876 }
171877 sqlite3_mutex_leave(db->mutex);
171878 return iTxn;
171879}
171880
171881/*
171882** Two variations on the public interface for closing a database
171883** connection. The sqlite3_close() version returns SQLITE_BUSY and
171884** leaves the connection open if there are unfinalized prepared
171885** statements or unfinished sqlite3_backups. The sqlite3_close_v2()
171886** version forces the connection to become a zombie if there are
171887** unclosed resources, and arranges for deallocation when the last
171888** prepare statement or sqlite3_backup closes.
171889*/
171890SQLITE_API int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); }
171891SQLITE_API int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); }
171892
171893
171894/*
171895** Close the mutex on database connection db.
171896**
171897** Furthermore, if database connection db is a zombie (meaning that there
171898** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and
171899** every sqlite3_stmt has now been finalized and every sqlite3_backup has
171900** finished, then free all resources.
171901*/
171902SQLITE_PRIVATEstatic void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
171903 HashElem *i; /* Hash table iterator */
171904 int j;
171905
171906 /* If there are outstanding sqlite3_stmt or sqlite3_backup objects
171907 ** or if the connection has not yet been closed by sqlite3_close_v2(),
171908 ** then just leave the mutex and return.
171909 */
171910 if( db->eOpenState!=SQLITE_STATE_ZOMBIE0xa7 || connectionIsBusy(db) ){
171911 sqlite3_mutex_leave(db->mutex);
171912 return;
171913 }
171914
171915 /* If we reach this point, it means that the database connection has
171916 ** closed all sqlite3_stmt and sqlite3_backup objects and has been
171917 ** passed to sqlite3_close (meaning that it is a zombie). Therefore,
171918 ** go ahead and free all resources.
171919 */
171920
171921 /* If a transaction is open, roll it back. This also ensures that if
171922 ** any database schemas have been modified by an uncommitted transaction
171923 ** they are reset. And that the required b-tree mutex is held to make
171924 ** the pager rollback and schema reset an atomic operation. */
171925 sqlite3RollbackAll(db, SQLITE_OK0);
171926
171927 /* Free any outstanding Savepoint structures. */
171928 sqlite3CloseSavepoints(db);
171929
171930 /* Close all database connections */
171931 for(j=0; j<db->nDb; j++){
171932 struct Db *pDb = &db->aDb[j];
171933 if( pDb->pBt ){
171934 sqlite3BtreeClose(pDb->pBt);
171935 pDb->pBt = 0;
171936 if( j!=1 ){
171937 pDb->pSchema = 0;
171938 }
171939 }
171940 }
171941 /* Clear the TEMP schema separately and last */
171942 if( db->aDb[1].pSchema ){
171943 sqlite3SchemaClear(db->aDb[1].pSchema);
171944 }
171945 sqlite3VtabUnlockList(db);
171946
171947 /* Free up the array of auxiliary databases */
171948 sqlite3CollapseDatabaseArray(db);
171949 assert( db->nDb<=2 )((void) (0));
171950 assert( db->aDb==db->aDbStatic )((void) (0));
171951
171952 /* Tell the code in notify.c that the connection no longer holds any
171953 ** locks and does not require any further unlock-notify callbacks.
171954 */
171955 sqlite3ConnectionClosed(db);
171956
171957 for(i=sqliteHashFirst(&db->aFunc)((&db->aFunc)->first); i; i=sqliteHashNext(i)((i)->next)){
171958 FuncDef *pNext, *p;
171959 p = sqliteHashData(i)((i)->data);
171960 do{
171961 functionDestroy(db, p);
171962 pNext = p->pNext;
171963 sqlite3DbFree(db, p);
171964 p = pNext;
171965 }while( p );
171966 }
171967 sqlite3HashClear(&db->aFunc);
171968 for(i=sqliteHashFirst(&db->aCollSeq)((&db->aCollSeq)->first); i; i=sqliteHashNext(i)((i)->next)){
171969 CollSeq *pColl = (CollSeq *)sqliteHashData(i)((i)->data);
171970 /* Invoke any destructors registered for collation sequence user data. */
171971 for(j=0; j<3; j++){
171972 if( pColl[j].xDel ){
171973 pColl[j].xDel(pColl[j].pUser);
171974 }
171975 }
171976 sqlite3DbFree(db, pColl);
171977 }
171978 sqlite3HashClear(&db->aCollSeq);
171979#ifndef SQLITE_OMIT_VIRTUALTABLE
171980 for(i=sqliteHashFirst(&db->aModule)((&db->aModule)->first); i; i=sqliteHashNext(i)((i)->next)){
171981 Module *pMod = (Module *)sqliteHashData(i)((i)->data);
171982 sqlite3VtabEponymousTableClear(db, pMod);
171983 sqlite3VtabModuleUnref(db, pMod);
171984 }
171985 sqlite3HashClear(&db->aModule);
171986#endif
171987
171988 sqlite3Error(db, SQLITE_OK0); /* Deallocates any cached error strings. */
171989 sqlite3ValueFree(db->pErr);
171990 sqlite3CloseExtensions(db);
171991#if SQLITE_USER_AUTHENTICATION
171992 sqlite3_free(db->auth.zAuthUser);
171993 sqlite3_free(db->auth.zAuthPW);
171994#endif
171995
171996 db->eOpenState = SQLITE_STATE_ERROR0xd5;
171997
171998 /* The temp-database schema is allocated differently from the other schema
171999 ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
172000 ** So it needs to be freed here. Todo: Why not roll the temp schema into
172001 ** the same sqliteMalloc() as the one that allocates the database
172002 ** structure?
172003 */
172004 sqlite3DbFree(db, db->aDb[1].pSchema);
172005 if( db->xAutovacDestr ){
172006 db->xAutovacDestr(db->pAutovacPagesArg);
172007 }
172008 sqlite3_mutex_leave(db->mutex);
172009 db->eOpenState = SQLITE_STATE_CLOSED0xce;
172010 sqlite3_mutex_free(db->mutex);
172011 assert( sqlite3LookasideUsed(db,0)==0 )((void) (0));
172012 if( db->lookaside.bMalloced ){
172013 sqlite3_free(db->lookaside.pStart);
172014 }
172015 sqlite3_free(db);
172016}
172017
172018/*
172019** Rollback all database files. If tripCode is not SQLITE_OK, then
172020** any write cursors are invalidated ("tripped" - as in "tripping a circuit
172021** breaker") and made to return tripCode if there are any further
172022** attempts to use that cursor. Read cursors remain open and valid
172023** but are "saved" in case the table pages are moved around.
172024*/
172025SQLITE_PRIVATEstatic void sqlite3RollbackAll(sqlite3 *db, int tripCode){
172026 int i;
172027 int inTrans = 0;
172028 int schemaChange;
172029 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
172030 sqlite3BeginBenignMalloc();
172031
172032 /* Obtain all b-tree mutexes before making any calls to BtreeRollback().
172033 ** This is important in case the transaction being rolled back has
172034 ** modified the database schema. If the b-tree mutexes are not taken
172035 ** here, then another shared-cache connection might sneak in between
172036 ** the database rollback and schema reset, which can cause false
172037 ** corruption reports in some cases. */
172038 sqlite3BtreeEnterAll(db);
172039 schemaChange = (db->mDbFlags & DBFLAG_SchemaChange0x0001)!=0 && db->init.busy==0;
172040
172041 for(i=0; i<db->nDb; i++){
172042 Btree *p = db->aDb[i].pBt;
172043 if( p ){
172044 if( sqlite3BtreeTxnState(p)==SQLITE_TXN_WRITE2 ){
172045 inTrans = 1;
172046 }
172047 sqlite3BtreeRollback(p, tripCode, !schemaChange);
172048 }
172049 }
172050 sqlite3VtabRollback(db);
172051 sqlite3EndBenignMalloc();
172052
172053 if( schemaChange ){
172054 sqlite3ExpirePreparedStatements(db, 0);
172055 sqlite3ResetAllSchemasOfConnection(db);
172056 }
172057 sqlite3BtreeLeaveAll(db);
172058
172059 /* Any deferred constraint violations have now been resolved. */
172060 db->nDeferredCons = 0;
172061 db->nDeferredImmCons = 0;
172062 db->flags &= ~(u64)(SQLITE_DeferFKs0x00080000|SQLITE_CorruptRdOnly((u64)(0x00002)<<32));
172063
172064 /* If one has been configured, invoke the rollback-hook callback */
172065 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
172066 db->xRollbackCallback(db->pRollbackArg);
172067 }
172068}
172069
172070/*
172071** Return a static string containing the name corresponding to the error code
172072** specified in the argument.
172073*/
172074#if defined(SQLITE_NEED_ERR_NAME)
172075SQLITE_PRIVATEstatic const char *sqlite3ErrName(int rc){
172076 const char *zName = 0;
172077 int i, origRc = rc;
172078 for(i=0; i<2 && zName==0; i++, rc &= 0xff){
172079 switch( rc ){
172080 case SQLITE_OK0: zName = "SQLITE_OK"; break;
172081 case SQLITE_ERROR1: zName = "SQLITE_ERROR"; break;
172082 case SQLITE_ERROR_SNAPSHOT(1 | (3<<8)): zName = "SQLITE_ERROR_SNAPSHOT"; break;
172083 case SQLITE_INTERNAL2: zName = "SQLITE_INTERNAL"; break;
172084 case SQLITE_PERM3: zName = "SQLITE_PERM"; break;
172085 case SQLITE_ABORT4: zName = "SQLITE_ABORT"; break;
172086 case SQLITE_ABORT_ROLLBACK(4 | (2<<8)): zName = "SQLITE_ABORT_ROLLBACK"; break;
172087 case SQLITE_BUSY5: zName = "SQLITE_BUSY"; break;
172088 case SQLITE_BUSY_RECOVERY(5 | (1<<8)): zName = "SQLITE_BUSY_RECOVERY"; break;
172089 case SQLITE_BUSY_SNAPSHOT(5 | (2<<8)): zName = "SQLITE_BUSY_SNAPSHOT"; break;
172090 case SQLITE_LOCKED6: zName = "SQLITE_LOCKED"; break;
172091 case SQLITE_LOCKED_SHAREDCACHE(6 | (1<<8)): zName = "SQLITE_LOCKED_SHAREDCACHE";break;
172092 case SQLITE_NOMEM7: zName = "SQLITE_NOMEM"; break;
172093 case SQLITE_READONLY8: zName = "SQLITE_READONLY"; break;
172094 case SQLITE_READONLY_RECOVERY(8 | (1<<8)): zName = "SQLITE_READONLY_RECOVERY"; break;
172095 case SQLITE_READONLY_CANTINIT(8 | (5<<8)): zName = "SQLITE_READONLY_CANTINIT"; break;
172096 case SQLITE_READONLY_ROLLBACK(8 | (3<<8)): zName = "SQLITE_READONLY_ROLLBACK"; break;
172097 case SQLITE_READONLY_DBMOVED(8 | (4<<8)): zName = "SQLITE_READONLY_DBMOVED"; break;
172098 case SQLITE_READONLY_DIRECTORY(8 | (6<<8)): zName = "SQLITE_READONLY_DIRECTORY";break;
172099 case SQLITE_INTERRUPT9: zName = "SQLITE_INTERRUPT"; break;
172100 case SQLITE_IOERR10: zName = "SQLITE_IOERR"; break;
172101 case SQLITE_IOERR_READ(10 | (1<<8)): zName = "SQLITE_IOERR_READ"; break;
172102 case SQLITE_IOERR_SHORT_READ(10 | (2<<8)): zName = "SQLITE_IOERR_SHORT_READ"; break;
172103 case SQLITE_IOERR_WRITE(10 | (3<<8)): zName = "SQLITE_IOERR_WRITE"; break;
172104 case SQLITE_IOERR_FSYNC(10 | (4<<8)): zName = "SQLITE_IOERR_FSYNC"; break;
172105 case SQLITE_IOERR_DIR_FSYNC(10 | (5<<8)): zName = "SQLITE_IOERR_DIR_FSYNC"; break;
172106 case SQLITE_IOERR_TRUNCATE(10 | (6<<8)): zName = "SQLITE_IOERR_TRUNCATE"; break;
172107 case SQLITE_IOERR_FSTAT(10 | (7<<8)): zName = "SQLITE_IOERR_FSTAT"; break;
172108 case SQLITE_IOERR_UNLOCK(10 | (8<<8)): zName = "SQLITE_IOERR_UNLOCK"; break;
172109 case SQLITE_IOERR_RDLOCK(10 | (9<<8)): zName = "SQLITE_IOERR_RDLOCK"; break;
172110 case SQLITE_IOERR_DELETE(10 | (10<<8)): zName = "SQLITE_IOERR_DELETE"; break;
172111 case SQLITE_IOERR_NOMEM(10 | (12<<8)): zName = "SQLITE_IOERR_NOMEM"; break;
172112 case SQLITE_IOERR_ACCESS(10 | (13<<8)): zName = "SQLITE_IOERR_ACCESS"; break;
172113 case SQLITE_IOERR_CHECKRESERVEDLOCK(10 | (14<<8)):
172114 zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
172115 case SQLITE_IOERR_LOCK(10 | (15<<8)): zName = "SQLITE_IOERR_LOCK"; break;
172116 case SQLITE_IOERR_CLOSE(10 | (16<<8)): zName = "SQLITE_IOERR_CLOSE"; break;
172117 case SQLITE_IOERR_DIR_CLOSE(10 | (17<<8)): zName = "SQLITE_IOERR_DIR_CLOSE"; break;
172118 case SQLITE_IOERR_SHMOPEN(10 | (18<<8)): zName = "SQLITE_IOERR_SHMOPEN"; break;
172119 case SQLITE_IOERR_SHMSIZE(10 | (19<<8)): zName = "SQLITE_IOERR_SHMSIZE"; break;
172120 case SQLITE_IOERR_SHMLOCK(10 | (20<<8)): zName = "SQLITE_IOERR_SHMLOCK"; break;
172121 case SQLITE_IOERR_SHMMAP(10 | (21<<8)): zName = "SQLITE_IOERR_SHMMAP"; break;
172122 case SQLITE_IOERR_SEEK(10 | (22<<8)): zName = "SQLITE_IOERR_SEEK"; break;
172123 case SQLITE_IOERR_DELETE_NOENT(10 | (23<<8)): zName = "SQLITE_IOERR_DELETE_NOENT";break;
172124 case SQLITE_IOERR_MMAP(10 | (24<<8)): zName = "SQLITE_IOERR_MMAP"; break;
172125 case SQLITE_IOERR_GETTEMPPATH(10 | (25<<8)): zName = "SQLITE_IOERR_GETTEMPPATH"; break;
172126 case SQLITE_IOERR_CONVPATH(10 | (26<<8)): zName = "SQLITE_IOERR_CONVPATH"; break;
172127 case SQLITE_CORRUPT11: zName = "SQLITE_CORRUPT"; break;
172128 case SQLITE_CORRUPT_VTAB(11 | (1<<8)): zName = "SQLITE_CORRUPT_VTAB"; break;
172129 case SQLITE_NOTFOUND12: zName = "SQLITE_NOTFOUND"; break;
172130 case SQLITE_FULL13: zName = "SQLITE_FULL"; break;
172131 case SQLITE_CANTOPEN14: zName = "SQLITE_CANTOPEN"; break;
172132 case SQLITE_CANTOPEN_NOTEMPDIR(14 | (1<<8)): zName = "SQLITE_CANTOPEN_NOTEMPDIR";break;
172133 case SQLITE_CANTOPEN_ISDIR(14 | (2<<8)): zName = "SQLITE_CANTOPEN_ISDIR"; break;
172134 case SQLITE_CANTOPEN_FULLPATH(14 | (3<<8)): zName = "SQLITE_CANTOPEN_FULLPATH"; break;
172135 case SQLITE_CANTOPEN_CONVPATH(14 | (4<<8)): zName = "SQLITE_CANTOPEN_CONVPATH"; break;
172136 case SQLITE_CANTOPEN_SYMLINK(14 | (6<<8)): zName = "SQLITE_CANTOPEN_SYMLINK"; break;
172137 case SQLITE_PROTOCOL15: zName = "SQLITE_PROTOCOL"; break;
172138 case SQLITE_EMPTY16: zName = "SQLITE_EMPTY"; break;
172139 case SQLITE_SCHEMA17: zName = "SQLITE_SCHEMA"; break;
172140 case SQLITE_TOOBIG18: zName = "SQLITE_TOOBIG"; break;
172141 case SQLITE_CONSTRAINT19: zName = "SQLITE_CONSTRAINT"; break;
172142 case SQLITE_CONSTRAINT_UNIQUE(19 | (8<<8)): zName = "SQLITE_CONSTRAINT_UNIQUE"; break;
172143 case SQLITE_CONSTRAINT_TRIGGER(19 | (7<<8)): zName = "SQLITE_CONSTRAINT_TRIGGER";break;
172144 case SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8)):
172145 zName = "SQLITE_CONSTRAINT_FOREIGNKEY"; break;
172146 case SQLITE_CONSTRAINT_CHECK(19 | (1<<8)): zName = "SQLITE_CONSTRAINT_CHECK"; break;
172147 case SQLITE_CONSTRAINT_PRIMARYKEY(19 | (6<<8)):
172148 zName = "SQLITE_CONSTRAINT_PRIMARYKEY"; break;
172149 case SQLITE_CONSTRAINT_NOTNULL(19 | (5<<8)): zName = "SQLITE_CONSTRAINT_NOTNULL";break;
172150 case SQLITE_CONSTRAINT_COMMITHOOK(19 | (2<<8)):
172151 zName = "SQLITE_CONSTRAINT_COMMITHOOK"; break;
172152 case SQLITE_CONSTRAINT_VTAB(19 | (9<<8)): zName = "SQLITE_CONSTRAINT_VTAB"; break;
172153 case SQLITE_CONSTRAINT_FUNCTION(19 | (4<<8)):
172154 zName = "SQLITE_CONSTRAINT_FUNCTION"; break;
172155 case SQLITE_CONSTRAINT_ROWID(19 |(10<<8)): zName = "SQLITE_CONSTRAINT_ROWID"; break;
172156 case SQLITE_MISMATCH20: zName = "SQLITE_MISMATCH"; break;
172157 case SQLITE_MISUSE21: zName = "SQLITE_MISUSE"; break;
172158 case SQLITE_NOLFS22: zName = "SQLITE_NOLFS"; break;
172159 case SQLITE_AUTH23: zName = "SQLITE_AUTH"; break;
172160 case SQLITE_FORMAT24: zName = "SQLITE_FORMAT"; break;
172161 case SQLITE_RANGE25: zName = "SQLITE_RANGE"; break;
172162 case SQLITE_NOTADB26: zName = "SQLITE_NOTADB"; break;
172163 case SQLITE_ROW100: zName = "SQLITE_ROW"; break;
172164 case SQLITE_NOTICE27: zName = "SQLITE_NOTICE"; break;
172165 case SQLITE_NOTICE_RECOVER_WAL(27 | (1<<8)): zName = "SQLITE_NOTICE_RECOVER_WAL";break;
172166 case SQLITE_NOTICE_RECOVER_ROLLBACK(27 | (2<<8)):
172167 zName = "SQLITE_NOTICE_RECOVER_ROLLBACK"; break;
172168 case SQLITE_WARNING28: zName = "SQLITE_WARNING"; break;
172169 case SQLITE_WARNING_AUTOINDEX(28 | (1<<8)): zName = "SQLITE_WARNING_AUTOINDEX"; break;
172170 case SQLITE_DONE101: zName = "SQLITE_DONE"; break;
172171 }
172172 }
172173 if( zName==0 ){
172174 static char zBuf[50];
172175 sqlite3_snprintf(sizeof(zBuf), zBuf, "SQLITE_UNKNOWN(%d)", origRc);
172176 zName = zBuf;
172177 }
172178 return zName;
172179}
172180#endif
172181
172182/*
172183** Return a static string that describes the kind of error specified in the
172184** argument.
172185*/
172186SQLITE_PRIVATEstatic const char *sqlite3ErrStr(int rc){
172187 static const char* const aMsg[] = {
172188 /* SQLITE_OK */ "not an error",
172189 /* SQLITE_ERROR */ "SQL logic error",
172190 /* SQLITE_INTERNAL */ 0,
172191 /* SQLITE_PERM */ "access permission denied",
172192 /* SQLITE_ABORT */ "query aborted",
172193 /* SQLITE_BUSY */ "database is locked",
172194 /* SQLITE_LOCKED */ "database table is locked",
172195 /* SQLITE_NOMEM */ "out of memory",
172196 /* SQLITE_READONLY */ "attempt to write a readonly database",
172197 /* SQLITE_INTERRUPT */ "interrupted",
172198 /* SQLITE_IOERR */ "disk I/O error",
172199 /* SQLITE_CORRUPT */ "database disk image is malformed",
172200 /* SQLITE_NOTFOUND */ "unknown operation",
172201 /* SQLITE_FULL */ "database or disk is full",
172202 /* SQLITE_CANTOPEN */ "unable to open database file",
172203 /* SQLITE_PROTOCOL */ "locking protocol",
172204 /* SQLITE_EMPTY */ 0,
172205 /* SQLITE_SCHEMA */ "database schema has changed",
172206 /* SQLITE_TOOBIG */ "string or blob too big",
172207 /* SQLITE_CONSTRAINT */ "constraint failed",
172208 /* SQLITE_MISMATCH */ "datatype mismatch",
172209 /* SQLITE_MISUSE */ "bad parameter or other API misuse",
172210#ifdef SQLITE_DISABLE_LFS
172211 /* SQLITE_NOLFS */ "large file support is disabled",
172212#else
172213 /* SQLITE_NOLFS */ 0,
172214#endif
172215 /* SQLITE_AUTH */ "authorization denied",
172216 /* SQLITE_FORMAT */ 0,
172217 /* SQLITE_RANGE */ "column index out of range",
172218 /* SQLITE_NOTADB */ "file is not a database",
172219 /* SQLITE_NOTICE */ "notification message",
172220 /* SQLITE_WARNING */ "warning message",
172221 };
172222 const char *zErr = "unknown error";
172223 switch( rc ){
172224 case SQLITE_ABORT_ROLLBACK(4 | (2<<8)): {
172225 zErr = "abort due to ROLLBACK";
172226 break;
172227 }
172228 case SQLITE_ROW100: {
172229 zErr = "another row available";
172230 break;
172231 }
172232 case SQLITE_DONE101: {
172233 zErr = "no more rows available";
172234 break;
172235 }
172236 default: {
172237 rc &= 0xff;
172238 if( ALWAYS(rc>=0)(rc>=0) && rc<ArraySize(aMsg)((int)(sizeof(aMsg)/sizeof(aMsg[0]))) && aMsg[rc]!=0 ){
172239 zErr = aMsg[rc];
172240 }
172241 break;
172242 }
172243 }
172244 return zErr;
172245}
172246
172247/*
172248** This routine implements a busy callback that sleeps and tries
172249** again until a timeout value is reached. The timeout value is
172250** an integer number of milliseconds passed in as the first
172251** argument.
172252**
172253** Return non-zero to retry the lock. Return zero to stop trying
172254** and cause SQLite to return SQLITE_BUSY.
172255*/
172256static int sqliteDefaultBusyCallback(
172257 void *ptr, /* Database connection */
172258 int count /* Number of times table has been busy */
172259){
172260#if SQLITE_OS_WIN0 || HAVE_USLEEP1
172261 /* This case is for systems that have support for sleeping for fractions of
172262 ** a second. Examples: All windows systems, unix systems with usleep() */
172263 static const u8 delays[] =
172264 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
172265 static const u8 totals[] =
172266 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
172267# define NDELAY((int)(sizeof(delays)/sizeof(delays[0]))) ArraySize(delays)((int)(sizeof(delays)/sizeof(delays[0])))
172268 sqlite3 *db = (sqlite3 *)ptr;
172269 int tmout = db->busyTimeout;
172270 int delay, prior;
172271
172272 assert( count>=0 )((void) (0));
172273 if( count < NDELAY((int)(sizeof(delays)/sizeof(delays[0]))) ){
172274 delay = delays[count];
172275 prior = totals[count];
172276 }else{
172277 delay = delays[NDELAY((int)(sizeof(delays)/sizeof(delays[0])))-1];
172278 prior = totals[NDELAY((int)(sizeof(delays)/sizeof(delays[0])))-1] + delay*(count-(NDELAY((int)(sizeof(delays)/sizeof(delays[0])))-1));
172279 }
172280 if( prior + delay > tmout ){
172281 delay = tmout - prior;
172282 if( delay<=0 ) return 0;
172283 }
172284 sqlite3OsSleep(db->pVfs, delay*1000);
172285 return 1;
172286#else
172287 /* This case for unix systems that lack usleep() support. Sleeping
172288 ** must be done in increments of whole seconds */
172289 sqlite3 *db = (sqlite3 *)ptr;
172290 int tmout = ((sqlite3 *)ptr)->busyTimeout;
172291 if( (count+1)*1000 > tmout ){
172292 return 0;
172293 }
172294 sqlite3OsSleep(db->pVfs, 1000000);
172295 return 1;
172296#endif
172297}
172298
172299/*
172300** Invoke the given busy handler.
172301**
172302** This routine is called when an operation failed to acquire a
172303** lock on VFS file pFile.
172304**
172305** If this routine returns non-zero, the lock is retried. If it
172306** returns 0, the operation aborts with an SQLITE_BUSY error.
172307*/
172308SQLITE_PRIVATEstatic int sqlite3InvokeBusyHandler(BusyHandler *p){
172309 int rc;
172310 if( p->xBusyHandler==0 || p->nBusy<0 ) return 0;
172311 rc = p->xBusyHandler(p->pBusyArg, p->nBusy);
172312 if( rc==0 ){
172313 p->nBusy = -1;
172314 }else{
172315 p->nBusy++;
172316 }
172317 return rc;
172318}
172319
172320/*
172321** This routine sets the busy callback for an Sqlite database to the
172322** given callback function with the given argument.
172323*/
172324SQLITE_API int sqlite3_busy_handler(
172325 sqlite3 *db,
172326 int (*xBusy)(void*,int),
172327 void *pArg
172328){
172329#ifdef SQLITE_ENABLE_API_ARMOR1
172330 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(172330);
172331#endif
172332 sqlite3_mutex_enter(db->mutex);
172333 db->busyHandler.xBusyHandler = xBusy;
172334 db->busyHandler.pBusyArg = pArg;
172335 db->busyHandler.nBusy = 0;
172336 db->busyTimeout = 0;
172337 sqlite3_mutex_leave(db->mutex);
172338 return SQLITE_OK0;
172339}
172340
172341#ifndef SQLITE_OMIT_PROGRESS_CALLBACK1
172342/*
172343** This routine sets the progress callback for an Sqlite database to the
172344** given callback function with the given argument. The progress callback will
172345** be invoked every nOps opcodes.
172346*/
172347SQLITE_API void sqlite3_progress_handler(
172348 sqlite3 *db,
172349 int nOps,
172350 int (*xProgress)(void*),
172351 void *pArg
172352){
172353#ifdef SQLITE_ENABLE_API_ARMOR1
172354 if( !sqlite3SafetyCheckOk(db) ){
172355 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(172355);
172356 return;
172357 }
172358#endif
172359 sqlite3_mutex_enter(db->mutex);
172360 if( nOps>0 ){
172361 db->xProgress = xProgress;
172362 db->nProgressOps = (unsigned)nOps;
172363 db->pProgressArg = pArg;
172364 }else{
172365 db->xProgress = 0;
172366 db->nProgressOps = 0;
172367 db->pProgressArg = 0;
172368 }
172369 sqlite3_mutex_leave(db->mutex);
172370}
172371#endif
172372
172373
172374/*
172375** This routine installs a default busy handler that waits for the
172376** specified number of milliseconds before returning 0.
172377*/
172378SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
172379#ifdef SQLITE_ENABLE_API_ARMOR1
172380 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(172380);
172381#endif
172382 if( ms>0 ){
172383 sqlite3_busy_handler(db, (int(*)(void*,int))sqliteDefaultBusyCallback,
172384 (void*)db);
172385 db->busyTimeout = ms;
172386 }else{
172387 sqlite3_busy_handler(db, 0, 0);
172388 }
172389 return SQLITE_OK0;
172390}
172391
172392/*
172393** Cause any pending operation to stop at its earliest opportunity.
172394*/
172395SQLITE_API void sqlite3_interrupt(sqlite3 *db){
172396#ifdef SQLITE_ENABLE_API_ARMOR1
172397 if( !sqlite3SafetyCheckOk(db) && (db==0 || db->eOpenState!=SQLITE_STATE_ZOMBIE0xa7) ){
172398 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(172398);
172399 return;
172400 }
172401#endif
172402 AtomicStore(&db->u1.isInterrupted, 1)__atomic_store_n((&db->u1.isInterrupted),(1),0);
172403}
172404
172405
172406/*
172407** This function is exactly the same as sqlite3_create_function(), except
172408** that it is designed to be called by internal code. The difference is
172409** that if a malloc() fails in sqlite3_create_function(), an error code
172410** is returned and the mallocFailed flag cleared.
172411*/
172412SQLITE_PRIVATEstatic int sqlite3CreateFunc(
172413 sqlite3 *db,
172414 const char *zFunctionName,
172415 int nArg,
172416 int enc,
172417 void *pUserData,
172418 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
172419 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
172420 void (*xFinal)(sqlite3_context*),
172421 void (*xValue)(sqlite3_context*),
172422 void (*xInverse)(sqlite3_context*,int,sqlite3_value **),
172423 FuncDestructor *pDestructor
172424){
172425 FuncDef *p;
172426 int extraFlags;
172427
172428 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
172429 assert( xValue==0 || xSFunc==0 )((void) (0));
172430 if( zFunctionName==0 /* Must have a valid name */
172431 || (xSFunc!=0 && xFinal!=0) /* Not both xSFunc and xFinal */
172432 || ((xFinal==0)!=(xStep==0)) /* Both or neither of xFinal and xStep */
172433 || ((xValue==0)!=(xInverse==0)) /* Both or neither of xValue, xInverse */
172434 || (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG127)
172435 || (255<sqlite3Strlen30(zFunctionName))
172436 ){
172437 return SQLITE_MISUSE_BKPTsqlite3MisuseError(172437);
172438 }
172439
172440 assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC )((void) (0));
172441 assert( SQLITE_FUNC_DIRECT==SQLITE_DIRECTONLY )((void) (0));
172442 extraFlags = enc & (SQLITE_DETERMINISTIC0x000000800|SQLITE_DIRECTONLY0x000080000|
172443 SQLITE_SUBTYPE0x000100000|SQLITE_INNOCUOUS0x000200000);
172444 enc &= (SQLITE_FUNC_ENCMASK0x0003|SQLITE_ANY5);
172445
172446 /* The SQLITE_INNOCUOUS flag is the same bit as SQLITE_FUNC_UNSAFE. But
172447 ** the meaning is inverted. So flip the bit. */
172448 assert( SQLITE_FUNC_UNSAFE==SQLITE_INNOCUOUS )((void) (0));
172449 extraFlags ^= SQLITE_FUNC_UNSAFE0x00200000;
172450
172451
172452#ifndef SQLITE_OMIT_UTF16
172453 /* If SQLITE_UTF16 is specified as the encoding type, transform this
172454 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
172455 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
172456 **
172457 ** If SQLITE_ANY is specified, add three versions of the function
172458 ** to the hash table.
172459 */
172460 switch( enc ){
172461 case SQLITE_UTF164:
172462 enc = SQLITE_UTF16NATIVE2;
172463 break;
172464 case SQLITE_ANY5: {
172465 int rc;
172466 rc = sqlite3CreateFunc(db, zFunctionName, nArg,
172467 (SQLITE_UTF81|extraFlags)^SQLITE_FUNC_UNSAFE0x00200000,
172468 pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor);
172469 if( rc==SQLITE_OK0 ){
172470 rc = sqlite3CreateFunc(db, zFunctionName, nArg,
172471 (SQLITE_UTF16LE2|extraFlags)^SQLITE_FUNC_UNSAFE0x00200000,
172472 pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor);
172473 }
172474 if( rc!=SQLITE_OK0 ){
172475 return rc;
172476 }
172477 enc = SQLITE_UTF16BE3;
172478 break;
172479 }
172480 case SQLITE_UTF81:
172481 case SQLITE_UTF16LE2:
172482 case SQLITE_UTF16BE3:
172483 break;
172484 default:
172485 enc = SQLITE_UTF81;
172486 break;
172487 }
172488#else
172489 enc = SQLITE_UTF81;
172490#endif
172491
172492 /* Check if an existing function is being overridden or deleted. If so,
172493 ** and there are active VMs, then return SQLITE_BUSY. If a function
172494 ** is being overridden/deleted but there are no active VMs, allow the
172495 ** operation to continue but invalidate all precompiled statements.
172496 */
172497 p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 0);
172498 if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK0x0003)==(u32)enc && p->nArg==nArg ){
172499 if( db->nVdbeActive ){
172500 sqlite3ErrorWithMsg(db, SQLITE_BUSY5,
172501 "unable to delete/modify user-function due to active statements");
172502 assert( !db->mallocFailed )((void) (0));
172503 return SQLITE_BUSY5;
172504 }else{
172505 sqlite3ExpirePreparedStatements(db, 0);
172506 }
172507 }else if( xSFunc==0 && xFinal==0 ){
172508 /* Trying to delete a function that does not exist. This is a no-op.
172509 ** https://sqlite.org/forum/forumpost/726219164b */
172510 return SQLITE_OK0;
172511 }
172512
172513 p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 1);
172514 assert(p || db->mallocFailed)((void) (0));
172515 if( !p ){
172516 return SQLITE_NOMEM_BKPT7;
172517 }
172518
172519 /* If an older version of the function with a configured destructor is
172520 ** being replaced invoke the destructor function here. */
172521 functionDestroy(db, p);
172522
172523 if( pDestructor ){
172524 pDestructor->nRef++;
172525 }
172526 p->u.pDestructor = pDestructor;
172527 p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK0x0003) | extraFlags;
172528 testcase( p->funcFlags & SQLITE_DETERMINISTIC );
172529 testcase( p->funcFlags & SQLITE_DIRECTONLY );
172530 p->xSFunc = xSFunc ? xSFunc : xStep;
172531 p->xFinalize = xFinal;
172532 p->xValue = xValue;
172533 p->xInverse = xInverse;
172534 p->pUserData = pUserData;
172535 p->nArg = (u16)nArg;
172536 return SQLITE_OK0;
172537}
172538
172539/*
172540** Worker function used by utf-8 APIs that create new functions:
172541**
172542** sqlite3_create_function()
172543** sqlite3_create_function_v2()
172544** sqlite3_create_window_function()
172545*/
172546static int createFunctionApi(
172547 sqlite3 *db,
172548 const char *zFunc,
172549 int nArg,
172550 int enc,
172551 void *p,
172552 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**),
172553 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
172554 void (*xFinal)(sqlite3_context*),
172555 void (*xValue)(sqlite3_context*),
172556 void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
172557 void(*xDestroy)(void*)
172558){
172559 int rc = SQLITE_ERROR1;
172560 FuncDestructor *pArg = 0;
172561
172562#ifdef SQLITE_ENABLE_API_ARMOR1
172563 if( !sqlite3SafetyCheckOk(db) ){
172564 return SQLITE_MISUSE_BKPTsqlite3MisuseError(172564);
172565 }
172566#endif
172567 sqlite3_mutex_enter(db->mutex);
172568 if( xDestroy ){
172569 pArg = (FuncDestructor *)sqlite3Malloc(sizeof(FuncDestructor));
172570 if( !pArg ){
172571 sqlite3OomFault(db);
172572 xDestroy(p);
172573 goto out;
172574 }
172575 pArg->nRef = 0;
172576 pArg->xDestroy = xDestroy;
172577 pArg->pUserData = p;
172578 }
172579 rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p,
172580 xSFunc, xStep, xFinal, xValue, xInverse, pArg
172581 );
172582 if( pArg && pArg->nRef==0 ){
172583 assert( rc!=SQLITE_OK || (xStep==0 && xFinal==0) )((void) (0));
172584 xDestroy(p);
172585 sqlite3_free(pArg);
172586 }
172587
172588 out:
172589 rc = sqlite3ApiExit(db, rc);
172590 sqlite3_mutex_leave(db->mutex);
172591 return rc;
172592}
172593
172594/*
172595** Create new user functions.
172596*/
172597SQLITE_API int sqlite3_create_function(
172598 sqlite3 *db,
172599 const char *zFunc,
172600 int nArg,
172601 int enc,
172602 void *p,
172603 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
172604 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
172605 void (*xFinal)(sqlite3_context*)
172606){
172607 return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep,
172608 xFinal, 0, 0, 0);
172609}
172610SQLITE_API int sqlite3_create_function_v2(
172611 sqlite3 *db,
172612 const char *zFunc,
172613 int nArg,
172614 int enc,
172615 void *p,
172616 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
172617 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
172618 void (*xFinal)(sqlite3_context*),
172619 void (*xDestroy)(void *)
172620){
172621 return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep,
172622 xFinal, 0, 0, xDestroy);
172623}
172624SQLITE_API int sqlite3_create_window_function(
172625 sqlite3 *db,
172626 const char *zFunc,
172627 int nArg,
172628 int enc,
172629 void *p,
172630 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
172631 void (*xFinal)(sqlite3_context*),
172632 void (*xValue)(sqlite3_context*),
172633 void (*xInverse)(sqlite3_context*,int,sqlite3_value **),
172634 void (*xDestroy)(void *)
172635){
172636 return createFunctionApi(db, zFunc, nArg, enc, p, 0, xStep,
172637 xFinal, xValue, xInverse, xDestroy);
172638}
172639
172640#ifndef SQLITE_OMIT_UTF16
172641SQLITE_API int sqlite3_create_function16(
172642 sqlite3 *db,
172643 const void *zFunctionName,
172644 int nArg,
172645 int eTextRep,
172646 void *p,
172647 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**),
172648 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
172649 void (*xFinal)(sqlite3_context*)
172650){
172651 int rc;
172652 char *zFunc8;
172653
172654#ifdef SQLITE_ENABLE_API_ARMOR1
172655 if( !sqlite3SafetyCheckOk(db) || zFunctionName==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(172655);
172656#endif
172657 sqlite3_mutex_enter(db->mutex);
172658 assert( !db->mallocFailed )((void) (0));
172659 zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE2);
172660 rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xSFunc,xStep,xFinal,0,0,0);
172661 sqlite3DbFree(db, zFunc8);
172662 rc = sqlite3ApiExit(db, rc);
172663 sqlite3_mutex_leave(db->mutex);
172664 return rc;
172665}
172666#endif
172667
172668
172669/*
172670** The following is the implementation of an SQL function that always
172671** fails with an error message stating that the function is used in the
172672** wrong context. The sqlite3_overload_function() API might construct
172673** SQL function that use this routine so that the functions will exist
172674** for name resolution but are actually overloaded by the xFindFunction
172675** method of virtual tables.
172676*/
172677static void sqlite3InvalidFunction(
172678 sqlite3_context *context, /* The function calling context */
172679 int NotUsed, /* Number of arguments to the function */
172680 sqlite3_value **NotUsed2 /* Value of each argument */
172681){
172682 const char *zName = (const char*)sqlite3_user_data(context);
172683 char *zErr;
172684 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
172685 zErr = sqlite3_mprintf(
172686 "unable to use function %s in the requested context", zName);
172687 sqlite3_result_error(context, zErr, -1);
172688 sqlite3_free(zErr);
172689}
172690
172691/*
172692** Declare that a function has been overloaded by a virtual table.
172693**
172694** If the function already exists as a regular global function, then
172695** this routine is a no-op. If the function does not exist, then create
172696** a new one that always throws a run-time error.
172697**
172698** When virtual tables intend to provide an overloaded function, they
172699** should call this routine to make sure the global function exists.
172700** A global function must exist in order for name resolution to work
172701** properly.
172702*/
172703SQLITE_API int sqlite3_overload_function(
172704 sqlite3 *db,
172705 const char *zName,
172706 int nArg
172707){
172708 int rc;
172709 char *zCopy;
172710
172711#ifdef SQLITE_ENABLE_API_ARMOR1
172712 if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
172713 return SQLITE_MISUSE_BKPTsqlite3MisuseError(172713);
172714 }
172715#endif
172716 sqlite3_mutex_enter(db->mutex);
172717 rc = sqlite3FindFunction(db, zName, nArg, SQLITE_UTF81, 0)!=0;
172718 sqlite3_mutex_leave(db->mutex);
172719 if( rc ) return SQLITE_OK0;
172720 zCopy = sqlite3_mprintf(zName);
172721 if( zCopy==0 ) return SQLITE_NOMEM7;
172722 return sqlite3_create_function_v2(db, zName, nArg, SQLITE_UTF81,
172723 zCopy, sqlite3InvalidFunction, 0, 0, sqlite3_free);
172724}
172725
172726#ifndef SQLITE_OMIT_TRACE
172727/*
172728** Register a trace function. The pArg from the previously registered trace
172729** is returned.
172730**
172731** A NULL trace function means that no tracing is executes. A non-NULL
172732** trace is a pointer to a function that is invoked at the start of each
172733** SQL statement.
172734*/
172735#ifndef SQLITE_OMIT_DEPRECATED1
172736SQLITE_API void *sqlite3_trace(sqlite3 *db, void(*xTrace)(void*,const char*), void *pArg){
172737 void *pOld;
172738
172739#ifdef SQLITE_ENABLE_API_ARMOR1
172740 if( !sqlite3SafetyCheckOk(db) ){
172741 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(172741);
172742 return 0;
172743 }
172744#endif
172745 sqlite3_mutex_enter(db->mutex);
172746 pOld = db->pTraceArg;
172747 db->mTrace = xTrace ? SQLITE_TRACE_LEGACY0 : 0;
172748 db->trace.xLegacy = xTrace;
172749 db->pTraceArg = pArg;
172750 sqlite3_mutex_leave(db->mutex);
172751 return pOld;
172752}
172753#endif /* SQLITE_OMIT_DEPRECATED */
172754
172755/* Register a trace callback using the version-2 interface.
172756*/
172757SQLITE_API int sqlite3_trace_v2(
172758 sqlite3 *db, /* Trace this connection */
172759 unsigned mTrace, /* Mask of events to be traced */
172760 int(*xTrace)(unsigned,void*,void*,void*), /* Callback to invoke */
172761 void *pArg /* Context */
172762){
172763#ifdef SQLITE_ENABLE_API_ARMOR1
172764 if( !sqlite3SafetyCheckOk(db) ){
172765 return SQLITE_MISUSE_BKPTsqlite3MisuseError(172765);
172766 }
172767#endif
172768 sqlite3_mutex_enter(db->mutex);
172769 if( mTrace==0 ) xTrace = 0;
172770 if( xTrace==0 ) mTrace = 0;
172771 db->mTrace = mTrace;
172772 db->trace.xV2 = xTrace;
172773 db->pTraceArg = pArg;
172774 sqlite3_mutex_leave(db->mutex);
172775 return SQLITE_OK0;
172776}
172777
172778#ifndef SQLITE_OMIT_DEPRECATED1
172779/*
172780** Register a profile function. The pArg from the previously registered
172781** profile function is returned.
172782**
172783** A NULL profile function means that no profiling is executes. A non-NULL
172784** profile is a pointer to a function that is invoked at the conclusion of
172785** each SQL statement that is run.
172786*/
172787SQLITE_API void *sqlite3_profile(
172788 sqlite3 *db,
172789 void (*xProfile)(void*,const char*,sqlite_uint64),
172790 void *pArg
172791){
172792 void *pOld;
172793
172794#ifdef SQLITE_ENABLE_API_ARMOR1
172795 if( !sqlite3SafetyCheckOk(db) ){
172796 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(172796);
172797 return 0;
172798 }
172799#endif
172800 sqlite3_mutex_enter(db->mutex);
172801 pOld = db->pProfileArg;
172802 db->xProfile = xProfile;
172803 db->pProfileArg = pArg;
172804 db->mTrace &= SQLITE_TRACE_NONLEGACY_MASK0x0f;
172805 if( db->xProfile ) db->mTrace |= SQLITE_TRACE_XPROFILE0;
172806 sqlite3_mutex_leave(db->mutex);
172807 return pOld;
172808}
172809#endif /* SQLITE_OMIT_DEPRECATED */
172810#endif /* SQLITE_OMIT_TRACE */
172811
172812/*
172813** Register a function to be invoked when a transaction commits.
172814** If the invoked function returns non-zero, then the commit becomes a
172815** rollback.
172816*/
172817SQLITE_API void *sqlite3_commit_hook(
172818 sqlite3 *db, /* Attach the hook to this database */
172819 int (*xCallback)(void*), /* Function to invoke on each commit */
172820 void *pArg /* Argument to the function */
172821){
172822 void *pOld;
172823
172824#ifdef SQLITE_ENABLE_API_ARMOR1
172825 if( !sqlite3SafetyCheckOk(db) ){
172826 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(172826);
172827 return 0;
172828 }
172829#endif
172830 sqlite3_mutex_enter(db->mutex);
172831 pOld = db->pCommitArg;
172832 db->xCommitCallback = xCallback;
172833 db->pCommitArg = pArg;
172834 sqlite3_mutex_leave(db->mutex);
172835 return pOld;
172836}
172837
172838/*
172839** Register a callback to be invoked each time a row is updated,
172840** inserted or deleted using this database connection.
172841*/
172842SQLITE_API void *sqlite3_update_hook(
172843 sqlite3 *db, /* Attach the hook to this database */
172844 void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
172845 void *pArg /* Argument to the function */
172846){
172847 void *pRet;
172848
172849#ifdef SQLITE_ENABLE_API_ARMOR1
172850 if( !sqlite3SafetyCheckOk(db) ){
172851 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(172851);
172852 return 0;
172853 }
172854#endif
172855 sqlite3_mutex_enter(db->mutex);
172856 pRet = db->pUpdateArg;
172857 db->xUpdateCallback = xCallback;
172858 db->pUpdateArg = pArg;
172859 sqlite3_mutex_leave(db->mutex);
172860 return pRet;
172861}
172862
172863/*
172864** Register a callback to be invoked each time a transaction is rolled
172865** back by this database connection.
172866*/
172867SQLITE_API void *sqlite3_rollback_hook(
172868 sqlite3 *db, /* Attach the hook to this database */
172869 void (*xCallback)(void*), /* Callback function */
172870 void *pArg /* Argument to the function */
172871){
172872 void *pRet;
172873
172874#ifdef SQLITE_ENABLE_API_ARMOR1
172875 if( !sqlite3SafetyCheckOk(db) ){
172876 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(172876);
172877 return 0;
172878 }
172879#endif
172880 sqlite3_mutex_enter(db->mutex);
172881 pRet = db->pRollbackArg;
172882 db->xRollbackCallback = xCallback;
172883 db->pRollbackArg = pArg;
172884 sqlite3_mutex_leave(db->mutex);
172885 return pRet;
172886}
172887
172888#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
172889/*
172890** Register a callback to be invoked each time a row is updated,
172891** inserted or deleted using this database connection.
172892*/
172893SQLITE_API void *sqlite3_preupdate_hook(
172894 sqlite3 *db, /* Attach the hook to this database */
172895 void(*xCallback)( /* Callback function */
172896 void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64),
172897 void *pArg /* First callback argument */
172898){
172899 void *pRet;
172900 sqlite3_mutex_enter(db->mutex);
172901 pRet = db->pPreUpdateArg;
172902 db->xPreUpdateCallback = xCallback;
172903 db->pPreUpdateArg = pArg;
172904 sqlite3_mutex_leave(db->mutex);
172905 return pRet;
172906}
172907#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
172908
172909/*
172910** Register a function to be invoked prior to each autovacuum that
172911** determines the number of pages to vacuum.
172912*/
172913SQLITE_API int sqlite3_autovacuum_pages(
172914 sqlite3 *db, /* Attach the hook to this database */
172915 unsigned int (*xCallback)(void*,const char*,u32,u32,u32),
172916 void *pArg, /* Argument to the function */
172917 void (*xDestructor)(void*) /* Destructor for pArg */
172918){
172919#ifdef SQLITE_ENABLE_API_ARMOR1
172920 if( !sqlite3SafetyCheckOk(db) ){
172921 if( xDestructor ) xDestructor(pArg);
172922 return SQLITE_MISUSE_BKPTsqlite3MisuseError(172922);
172923 }
172924#endif
172925 sqlite3_mutex_enter(db->mutex);
172926 if( db->xAutovacDestr ){
172927 db->xAutovacDestr(db->pAutovacPagesArg);
172928 }
172929 db->xAutovacPages = xCallback;
172930 db->pAutovacPagesArg = pArg;
172931 db->xAutovacDestr = xDestructor;
172932 sqlite3_mutex_leave(db->mutex);
172933 return SQLITE_OK0;
172934}
172935
172936
172937#ifndef SQLITE_OMIT_WAL
172938/*
172939** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
172940** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
172941** is greater than sqlite3.pWalArg cast to an integer (the value configured by
172942** wal_autocheckpoint()).
172943*/
172944SQLITE_PRIVATEstatic int sqlite3WalDefaultHook(
172945 void *pClientData, /* Argument */
172946 sqlite3 *db, /* Connection */
172947 const char *zDb, /* Database */
172948 int nFrame /* Size of WAL */
172949){
172950 if( nFrame>=SQLITE_PTR_TO_INT(pClientData)((int)(long int)(pClientData)) ){
172951 sqlite3BeginBenignMalloc();
172952 sqlite3_wal_checkpoint(db, zDb);
172953 sqlite3EndBenignMalloc();
172954 }
172955 return SQLITE_OK0;
172956}
172957#endif /* SQLITE_OMIT_WAL */
172958
172959/*
172960** Configure an sqlite3_wal_hook() callback to automatically checkpoint
172961** a database after committing a transaction if there are nFrame or
172962** more frames in the log file. Passing zero or a negative value as the
172963** nFrame parameter disables automatic checkpoints entirely.
172964**
172965** The callback registered by this function replaces any existing callback
172966** registered using sqlite3_wal_hook(). Likewise, registering a callback
172967** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
172968** configured by this function.
172969*/
172970SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
172971#ifdef SQLITE_OMIT_WAL
172972 UNUSED_PARAMETER(db)(void)(db);
172973 UNUSED_PARAMETER(nFrame)(void)(nFrame);
172974#else
172975#ifdef SQLITE_ENABLE_API_ARMOR1
172976 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(172976);
172977#endif
172978 if( nFrame>0 ){
172979 sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame)((void*)(long int)(nFrame)));
172980 }else{
172981 sqlite3_wal_hook(db, 0, 0);
172982 }
172983#endif
172984 return SQLITE_OK0;
172985}
172986
172987/*
172988** Register a callback to be invoked each time a transaction is written
172989** into the write-ahead-log by this database connection.
172990*/
172991SQLITE_API void *sqlite3_wal_hook(
172992 sqlite3 *db, /* Attach the hook to this db handle */
172993 int(*xCallback)(void *, sqlite3*, const char*, int),
172994 void *pArg /* First argument passed to xCallback() */
172995){
172996#ifndef SQLITE_OMIT_WAL
172997 void *pRet;
172998#ifdef SQLITE_ENABLE_API_ARMOR1
172999 if( !sqlite3SafetyCheckOk(db) ){
173000 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(173000);
173001 return 0;
173002 }
173003#endif
173004 sqlite3_mutex_enter(db->mutex);
173005 pRet = db->pWalArg;
173006 db->xWalCallback = xCallback;
173007 db->pWalArg = pArg;
173008 sqlite3_mutex_leave(db->mutex);
173009 return pRet;
173010#else
173011 return 0;
173012#endif
173013}
173014
173015/*
173016** Checkpoint database zDb.
173017*/
173018SQLITE_API int sqlite3_wal_checkpoint_v2(
173019 sqlite3 *db, /* Database handle */
173020 const char *zDb, /* Name of attached database (or NULL) */
173021 int eMode, /* SQLITE_CHECKPOINT_* value */
173022 int *pnLog, /* OUT: Size of WAL log in frames */
173023 int *pnCkpt /* OUT: Total number of frames checkpointed */
173024){
173025#ifdef SQLITE_OMIT_WAL
173026 return SQLITE_OK0;
173027#else
173028 int rc; /* Return code */
173029 int iDb; /* Schema to checkpoint */
173030
173031#ifdef SQLITE_ENABLE_API_ARMOR1
173032 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(173032);
173033#endif
173034
173035 /* Initialize the output variables to -1 in case an error occurs. */
173036 if( pnLog ) *pnLog = -1;
173037 if( pnCkpt ) *pnCkpt = -1;
173038
173039 assert( SQLITE_CHECKPOINT_PASSIVE==0 )((void) (0));
173040 assert( SQLITE_CHECKPOINT_FULL==1 )((void) (0));
173041 assert( SQLITE_CHECKPOINT_RESTART==2 )((void) (0));
173042 assert( SQLITE_CHECKPOINT_TRUNCATE==3 )((void) (0));
173043 if( eMode<SQLITE_CHECKPOINT_PASSIVE0 || eMode>SQLITE_CHECKPOINT_TRUNCATE3 ){
173044 /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint
173045 ** mode: */
173046 return SQLITE_MISUSE21;
173047 }
173048
173049 sqlite3_mutex_enter(db->mutex);
173050 if( zDb && zDb[0] ){
173051 iDb = sqlite3FindDbName(db, zDb);
173052 }else{
173053 iDb = SQLITE_MAX_DB(10 +2); /* This means process all schemas */
173054 }
173055 if( iDb<0 ){
173056 rc = SQLITE_ERROR1;
173057 sqlite3ErrorWithMsg(db, SQLITE_ERROR1, "unknown database: %s", zDb);
173058 }else{
173059 db->busyHandler.nBusy = 0;
173060 rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
173061 sqlite3Error(db, rc);
173062 }
173063 rc = sqlite3ApiExit(db, rc);
173064
173065 /* If there are no active statements, clear the interrupt flag at this
173066 ** point. */
173067 if( db->nVdbeActive==0 ){
173068 AtomicStore(&db->u1.isInterrupted, 0)__atomic_store_n((&db->u1.isInterrupted),(0),0);
173069 }
173070
173071 sqlite3_mutex_leave(db->mutex);
173072 return rc;
173073#endif
173074}
173075
173076
173077/*
173078** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
173079** to contains a zero-length string, all attached databases are
173080** checkpointed.
173081*/
173082SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
173083 /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to
173084 ** sqlite3_wal_checkpoint_v2(D,X,SQLITE_CHECKPOINT_PASSIVE,0,0). */
173085 return sqlite3_wal_checkpoint_v2(db,zDb,SQLITE_CHECKPOINT_PASSIVE0,0,0);
173086}
173087
173088#ifndef SQLITE_OMIT_WAL
173089/*
173090** Run a checkpoint on database iDb. This is a no-op if database iDb is
173091** not currently open in WAL mode.
173092**
173093** If a transaction is open on the database being checkpointed, this
173094** function returns SQLITE_LOCKED and a checkpoint is not attempted. If
173095** an error occurs while running the checkpoint, an SQLite error code is
173096** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
173097**
173098** The mutex on database handle db should be held by the caller. The mutex
173099** associated with the specific b-tree being checkpointed is taken by
173100** this function while the checkpoint is running.
173101**
173102** If iDb is passed SQLITE_MAX_DB then all attached databases are
173103** checkpointed. If an error is encountered it is returned immediately -
173104** no attempt is made to checkpoint any remaining databases.
173105**
173106** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL, RESTART
173107** or TRUNCATE.
173108*/
173109SQLITE_PRIVATEstatic int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
173110 int rc = SQLITE_OK0; /* Return code */
173111 int i; /* Used to iterate through attached dbs */
173112 int bBusy = 0; /* True if SQLITE_BUSY has been encountered */
173113
173114 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
173115 assert( !pnLog || *pnLog==-1 )((void) (0));
173116 assert( !pnCkpt || *pnCkpt==-1 )((void) (0));
173117 testcase( iDb==SQLITE_MAX_ATTACHED ); /* See forum post a006d86f72 */
173118 testcase( iDb==SQLITE_MAX_DB );
173119
173120 for(i=0; i<db->nDb && rc==SQLITE_OK0; i++){
173121 if( i==iDb || iDb==SQLITE_MAX_DB(10 +2) ){
173122 rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
173123 pnLog = 0;
173124 pnCkpt = 0;
173125 if( rc==SQLITE_BUSY5 ){
173126 bBusy = 1;
173127 rc = SQLITE_OK0;
173128 }
173129 }
173130 }
173131
173132 return (rc==SQLITE_OK0 && bBusy) ? SQLITE_BUSY5 : rc;
173133}
173134#endif /* SQLITE_OMIT_WAL */
173135
173136/*
173137** This function returns true if main-memory should be used instead of
173138** a temporary file for transient pager files and statement journals.
173139** The value returned depends on the value of db->temp_store (runtime
173140** parameter) and the compile time value of SQLITE_TEMP_STORE. The
173141** following table describes the relationship between these two values
173142** and this functions return value.
173143**
173144** SQLITE_TEMP_STORE db->temp_store Location of temporary database
173145** ----------------- -------------- ------------------------------
173146** 0 any file (return 0)
173147** 1 1 file (return 0)
173148** 1 2 memory (return 1)
173149** 1 0 file (return 0)
173150** 2 1 file (return 0)
173151** 2 2 memory (return 1)
173152** 2 0 memory (return 1)
173153** 3 any memory (return 1)
173154*/
173155SQLITE_PRIVATEstatic int sqlite3TempInMemory(const sqlite3 *db){
173156#if SQLITE_TEMP_STORE1==1
173157 return ( db->temp_store==2 );
173158#endif
173159#if SQLITE_TEMP_STORE1==2
173160 return ( db->temp_store!=1 );
173161#endif
173162#if SQLITE_TEMP_STORE1==3
173163 UNUSED_PARAMETER(db)(void)(db);
173164 return 1;
173165#endif
173166#if SQLITE_TEMP_STORE1<1 || SQLITE_TEMP_STORE1>3
173167 UNUSED_PARAMETER(db)(void)(db);
173168 return 0;
173169#endif
173170}
173171
173172/*
173173** Return UTF-8 encoded English language explanation of the most recent
173174** error.
173175*/
173176SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){
173177 const char *z;
173178 if( !db ){
173179 return sqlite3ErrStr(SQLITE_NOMEM_BKPT7);
173180 }
173181 if( !sqlite3SafetyCheckSickOrOk(db) ){
173182 return sqlite3ErrStr(SQLITE_MISUSE_BKPTsqlite3MisuseError(173182));
173183 }
173184 sqlite3_mutex_enter(db->mutex);
173185 if( db->mallocFailed ){
173186 z = sqlite3ErrStr(SQLITE_NOMEM_BKPT7);
173187 }else{
173188 testcase( db->pErr==0 );
173189 z = db->errCode ? (char*)sqlite3_value_text(db->pErr) : 0;
173190 assert( !db->mallocFailed )((void) (0));
173191 if( z==0 ){
173192 z = sqlite3ErrStr(db->errCode);
173193 }
173194 }
173195 sqlite3_mutex_leave(db->mutex);
173196 return z;
173197}
173198
173199/*
173200** Return the byte offset of the most recent error
173201*/
173202SQLITE_API int sqlite3_error_offset(sqlite3 *db){
173203 int iOffset = -1;
173204 if( db && sqlite3SafetyCheckSickOrOk(db) && db->errCode ){
173205 sqlite3_mutex_enter(db->mutex);
173206 iOffset = db->errByteOffset;
173207 sqlite3_mutex_leave(db->mutex);
173208 }
173209 return iOffset;
173210}
173211
173212#ifndef SQLITE_OMIT_UTF16
173213/*
173214** Return UTF-16 encoded English language explanation of the most recent
173215** error.
173216*/
173217SQLITE_API const void *sqlite3_errmsg16(sqlite3 *db){
173218 static const u16 outOfMem[] = {
173219 'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
173220 };
173221 static const u16 misuse[] = {
173222 'b', 'a', 'd', ' ', 'p', 'a', 'r', 'a', 'm', 'e', 't', 'e', 'r', ' ',
173223 'o', 'r', ' ', 'o', 't', 'h', 'e', 'r', ' ', 'A', 'P', 'I', ' ',
173224 'm', 'i', 's', 'u', 's', 'e', 0
173225 };
173226
173227 const void *z;
173228 if( !db ){
173229 return (void *)outOfMem;
173230 }
173231 if( !sqlite3SafetyCheckSickOrOk(db) ){
173232 return (void *)misuse;
173233 }
173234 sqlite3_mutex_enter(db->mutex);
173235 if( db->mallocFailed ){
173236 z = (void *)outOfMem;
173237 }else{
173238 z = sqlite3_value_text16(db->pErr);
173239 if( z==0 ){
173240 sqlite3ErrorWithMsg(db, db->errCode, sqlite3ErrStr(db->errCode));
173241 z = sqlite3_value_text16(db->pErr);
173242 }
173243 /* A malloc() may have failed within the call to sqlite3_value_text16()
173244 ** above. If this is the case, then the db->mallocFailed flag needs to
173245 ** be cleared before returning. Do this directly, instead of via
173246 ** sqlite3ApiExit(), to avoid setting the database handle error message.
173247 */
173248 sqlite3OomClear(db);
173249 }
173250 sqlite3_mutex_leave(db->mutex);
173251 return z;
173252}
173253#endif /* SQLITE_OMIT_UTF16 */
173254
173255/*
173256** Return the most recent error code generated by an SQLite routine. If NULL is
173257** passed to this function, we assume a malloc() failed during sqlite3_open().
173258*/
173259SQLITE_API int sqlite3_errcode(sqlite3 *db){
173260 if( db && !sqlite3SafetyCheckSickOrOk(db) ){
173261 return SQLITE_MISUSE_BKPTsqlite3MisuseError(173261);
173262 }
173263 if( !db || db->mallocFailed ){
173264 return SQLITE_NOMEM_BKPT7;
173265 }
173266 return db->errCode & db->errMask;
173267}
173268SQLITE_API int sqlite3_extended_errcode(sqlite3 *db){
173269 if( db && !sqlite3SafetyCheckSickOrOk(db) ){
173270 return SQLITE_MISUSE_BKPTsqlite3MisuseError(173270);
173271 }
173272 if( !db || db->mallocFailed ){
173273 return SQLITE_NOMEM_BKPT7;
173274 }
173275 return db->errCode;
173276}
173277SQLITE_API int sqlite3_system_errno(sqlite3 *db){
173278 return db ? db->iSysErrno : 0;
173279}
173280
173281/*
173282** Return a string that describes the kind of error specified in the
173283** argument. For now, this simply calls the internal sqlite3ErrStr()
173284** function.
173285*/
173286SQLITE_API const char *sqlite3_errstr(int rc){
173287 return sqlite3ErrStr(rc);
173288}
173289
173290/*
173291** Create a new collating function for database "db". The name is zName
173292** and the encoding is enc.
173293*/
173294static int createCollation(
173295 sqlite3* db,
173296 const char *zName,
173297 u8 enc,
173298 void* pCtx,
173299 int(*xCompare)(void*,int,const void*,int,const void*),
173300 void(*xDel)(void*)
173301){
173302 CollSeq *pColl;
173303 int enc2;
173304
173305 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
173306
173307 /* If SQLITE_UTF16 is specified as the encoding type, transform this
173308 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
173309 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
173310 */
173311 enc2 = enc;
173312 testcase( enc2==SQLITE_UTF16 );
173313 testcase( enc2==SQLITE_UTF16_ALIGNED );
173314 if( enc2==SQLITE_UTF164 || enc2==SQLITE_UTF16_ALIGNED8 ){
173315 enc2 = SQLITE_UTF16NATIVE2;
173316 }
173317 if( enc2<SQLITE_UTF81 || enc2>SQLITE_UTF16BE3 ){
173318 return SQLITE_MISUSE_BKPTsqlite3MisuseError(173318);
173319 }
173320
173321 /* Check if this call is removing or replacing an existing collation
173322 ** sequence. If so, and there are active VMs, return busy. If there
173323 ** are no active VMs, invalidate any pre-compiled statements.
173324 */
173325 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
173326 if( pColl && pColl->xCmp ){
173327 if( db->nVdbeActive ){
173328 sqlite3ErrorWithMsg(db, SQLITE_BUSY5,
173329 "unable to delete/modify collation sequence due to active statements");
173330 return SQLITE_BUSY5;
173331 }
173332 sqlite3ExpirePreparedStatements(db, 0);
173333
173334 /* If collation sequence pColl was created directly by a call to
173335 ** sqlite3_create_collation, and not generated by synthCollSeq(),
173336 ** then any copies made by synthCollSeq() need to be invalidated.
173337 ** Also, collation destructor - CollSeq.xDel() - function may need
173338 ** to be called.
173339 */
173340 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED8)==enc2 ){
173341 CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName);
173342 int j;
173343 for(j=0; j<3; j++){
173344 CollSeq *p = &aColl[j];
173345 if( p->enc==pColl->enc ){
173346 if( p->xDel ){
173347 p->xDel(p->pUser);
173348 }
173349 p->xCmp = 0;
173350 }
173351 }
173352 }
173353 }
173354
173355 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
173356 if( pColl==0 ) return SQLITE_NOMEM_BKPT7;
173357 pColl->xCmp = xCompare;
173358 pColl->pUser = pCtx;
173359 pColl->xDel = xDel;
173360 pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED8));
173361 sqlite3Error(db, SQLITE_OK0);
173362 return SQLITE_OK0;
173363}
173364
173365
173366/*
173367** This array defines hard upper bounds on limit values. The
173368** initializer must be kept in sync with the SQLITE_LIMIT_*
173369** #defines in sqlite3.h.
173370*/
173371static const int aHardLimit[] = {
173372 SQLITE_MAX_LENGTH2147483645,
173373 SQLITE_MAX_SQL_LENGTH1000000000,
173374 SQLITE_MAX_COLUMN2000,
173375 SQLITE_MAX_EXPR_DEPTH1000,
173376 SQLITE_MAX_COMPOUND_SELECT500,
173377 SQLITE_MAX_VDBE_OP250000000,
173378 SQLITE_MAX_FUNCTION_ARG127,
173379 SQLITE_MAX_ATTACHED10,
173380 SQLITE_MAX_LIKE_PATTERN_LENGTH50000,
173381 SQLITE_MAX_VARIABLE_NUMBER500000, /* IMP: R-38091-32352 */
173382 SQLITE_MAX_TRIGGER_DEPTH1000,
173383 SQLITE_MAX_WORKER_THREADS8,
173384};
173385
173386/*
173387** Make sure the hard limits are set to reasonable values
173388*/
173389#if SQLITE_MAX_LENGTH2147483645<100
173390# error SQLITE_MAX_LENGTH2147483645 must be at least 100
173391#endif
173392#if SQLITE_MAX_SQL_LENGTH1000000000<100
173393# error SQLITE_MAX_SQL_LENGTH1000000000 must be at least 100
173394#endif
173395#if SQLITE_MAX_SQL_LENGTH1000000000>SQLITE_MAX_LENGTH2147483645
173396# error SQLITE_MAX_SQL_LENGTH1000000000 must not be greater than SQLITE_MAX_LENGTH2147483645
173397#endif
173398#if SQLITE_MAX_COMPOUND_SELECT500<2
173399# error SQLITE_MAX_COMPOUND_SELECT500 must be at least 2
173400#endif
173401#if SQLITE_MAX_VDBE_OP250000000<40
173402# error SQLITE_MAX_VDBE_OP250000000 must be at least 40
173403#endif
173404#if SQLITE_MAX_FUNCTION_ARG127<0 || SQLITE_MAX_FUNCTION_ARG127>127
173405# error SQLITE_MAX_FUNCTION_ARG127 must be between 0 and 127
173406#endif
173407#if SQLITE_MAX_ATTACHED10<0 || SQLITE_MAX_ATTACHED10>125
173408# error SQLITE_MAX_ATTACHED10 must be between 0 and 125
173409#endif
173410#if SQLITE_MAX_LIKE_PATTERN_LENGTH50000<1
173411# error SQLITE_MAX_LIKE_PATTERN_LENGTH50000 must be at least 1
173412#endif
173413#if SQLITE_MAX_COLUMN2000>32767
173414# error SQLITE_MAX_COLUMN2000 must not exceed 32767
173415#endif
173416#if SQLITE_MAX_TRIGGER_DEPTH1000<1
173417# error SQLITE_MAX_TRIGGER_DEPTH1000 must be at least 1
173418#endif
173419#if SQLITE_MAX_WORKER_THREADS8<0 || SQLITE_MAX_WORKER_THREADS8>50
173420# error SQLITE_MAX_WORKER_THREADS8 must be between 0 and 50
173421#endif
173422
173423
173424/*
173425** Change the value of a limit. Report the old value.
173426** If an invalid limit index is supplied, report -1.
173427** Make no changes but still report the old value if the
173428** new limit is negative.
173429**
173430** A new lower limit does not shrink existing constructs.
173431** It merely prevents new constructs that exceed the limit
173432** from forming.
173433*/
173434SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
173435 int oldLimit;
173436
173437#ifdef SQLITE_ENABLE_API_ARMOR1
173438 if( !sqlite3SafetyCheckOk(db) ){
173439 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(173439);
173440 return -1;
173441 }
173442#endif
173443
173444 /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
173445 ** there is a hard upper bound set at compile-time by a C preprocessor
173446 ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
173447 ** "_MAX_".)
173448 */
173449 assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH )((void) (0));
173450 assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH )((void) (0));
173451 assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN )((void) (0));
173452 assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH )((void) (0));
173453 assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT)((void) (0));
173454 assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP )((void) (0));
173455 assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG )((void) (0));
173456 assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED )((void) (0));
173457 assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]==((void) (0))
173458 SQLITE_MAX_LIKE_PATTERN_LENGTH )((void) (0));
173459 assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER)((void) (0));
173460 assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH )((void) (0));
173461 assert( aHardLimit[SQLITE_LIMIT_WORKER_THREADS]==SQLITE_MAX_WORKER_THREADS )((void) (0));
173462 assert( SQLITE_LIMIT_WORKER_THREADS==(SQLITE_N_LIMIT-1) )((void) (0));
173463
173464
173465 if( limitId<0 || limitId>=SQLITE_N_LIMIT(11 +1) ){
173466 return -1;
173467 }
173468 oldLimit = db->aLimit[limitId];
173469 if( newLimit>=0 ){ /* IMP: R-52476-28732 */
173470 if( newLimit>aHardLimit[limitId] ){
173471 newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */
173472 }else if( newLimit<1 && limitId==SQLITE_LIMIT_LENGTH0 ){
173473 newLimit = 1;
173474 }
173475 db->aLimit[limitId] = newLimit;
173476 }
173477 return oldLimit; /* IMP: R-53341-35419 */
173478}
173479
173480/*
173481** This function is used to parse both URIs and non-URI filenames passed by the
173482** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
173483** URIs specified as part of ATTACH statements.
173484**
173485** The first argument to this function is the name of the VFS to use (or
173486** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx"
173487** query parameter. The second argument contains the URI (or non-URI filename)
173488** itself. When this function is called the *pFlags variable should contain
173489** the default flags to open the database handle with. The value stored in
173490** *pFlags may be updated before returning if the URI filename contains
173491** "cache=xxx" or "mode=xxx" query parameters.
173492**
173493** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to
173494** the VFS that should be used to open the database file. *pzFile is set to
173495** point to a buffer containing the name of the file to open. The value
173496** stored in *pzFile is a database name acceptable to sqlite3_uri_parameter()
173497** and is in the same format as names created using sqlite3_create_filename().
173498** The caller must invoke sqlite3_free_filename() (not sqlite3_free()!) on
173499** the value returned in *pzFile to avoid a memory leak.
173500**
173501** If an error occurs, then an SQLite error code is returned and *pzErrMsg
173502** may be set to point to a buffer containing an English language error
173503** message. It is the responsibility of the caller to eventually release
173504** this buffer by calling sqlite3_free().
173505*/
173506SQLITE_PRIVATEstatic int sqlite3ParseUri(
173507 const char *zDefaultVfs, /* VFS to use if no "vfs=xxx" query option */
173508 const char *zUri, /* Nul-terminated URI to parse */
173509 unsigned int *pFlags, /* IN/OUT: SQLITE_OPEN_XXX flags */
173510 sqlite3_vfs **ppVfs, /* OUT: VFS to use */
173511 char **pzFile, /* OUT: Filename component of URI */
173512 char **pzErrMsg /* OUT: Error message (if rc!=SQLITE_OK) */
173513){
173514 int rc = SQLITE_OK0;
173515 unsigned int flags = *pFlags;
173516 const char *zVfs = zDefaultVfs;
173517 char *zFile;
173518 char c;
173519 int nUri = sqlite3Strlen30(zUri);
173520
173521 assert( *pzErrMsg==0 )((void) (0));
173522
173523 if( ((flags & SQLITE_OPEN_URI0x00000040) /* IMP: R-48725-32206 */
173524 || sqlite3GlobalConfigsqlite3Config.bOpenUri) /* IMP: R-51689-46548 */
173525 && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
173526 ){
173527 char *zOpt;
173528 int eState; /* Parser state when parsing URI */
173529 int iIn; /* Input character index */
173530 int iOut = 0; /* Output character index */
173531 u64 nByte = nUri+8; /* Bytes of space to allocate */
173532
173533 /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen
173534 ** method that there may be extra parameters following the file-name. */
173535 flags |= SQLITE_OPEN_URI0x00000040;
173536
173537 for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
173538 zFile = sqlite3_malloc64(nByte);
173539 if( !zFile ) return SQLITE_NOMEM_BKPT7;
173540
173541 memset(zFile, 0, 4); /* 4-byte of 0x00 is the start of DB name marker */
173542 zFile += 4;
173543
173544 iIn = 5;
173545#ifdef SQLITE_ALLOW_URI_AUTHORITY
173546 if( strncmp(zUri+5, "///", 3)==0 ){
173547 iIn = 7;
173548 /* The following condition causes URIs with five leading / characters
173549 ** like file://///host/path to be converted into UNCs like //host/path.
173550 ** The correct URI for that UNC has only two or four leading / characters
173551 ** file://host/path or file:////host/path. But 5 leading slashes is a
173552 ** common error, we are told, so we handle it as a special case. */
173553 if( strncmp(zUri+7, "///", 3)==0 ){ iIn++; }
173554 }else if( strncmp(zUri+5, "//localhost/", 12)==0 ){
173555 iIn = 16;
173556 }
173557#else
173558 /* Discard the scheme and authority segments of the URI. */
173559 if( zUri[5]=='/' && zUri[6]=='/' ){
173560 iIn = 7;
173561 while( zUri[iIn] && zUri[iIn]!='/' ) iIn++;
173562 if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){
173563 *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s",
173564 iIn-7, &zUri[7]);
173565 rc = SQLITE_ERROR1;
173566 goto parse_uri_out;
173567 }
173568 }
173569#endif
173570
173571 /* Copy the filename and any query parameters into the zFile buffer.
173572 ** Decode %HH escape codes along the way.
173573 **
173574 ** Within this loop, variable eState may be set to 0, 1 or 2, depending
173575 ** on the parsing context. As follows:
173576 **
173577 ** 0: Parsing file-name.
173578 ** 1: Parsing name section of a name=value query parameter.
173579 ** 2: Parsing value section of a name=value query parameter.
173580 */
173581 eState = 0;
173582 while( (c = zUri[iIn])!=0 && c!='#' ){
173583 iIn++;
173584 if( c=='%'
173585 && sqlite3Isxdigit(zUri[iIn])(sqlite3CtypeMap[(unsigned char)(zUri[iIn])]&0x08)
173586 && sqlite3Isxdigit(zUri[iIn+1])(sqlite3CtypeMap[(unsigned char)(zUri[iIn+1])]&0x08)
173587 ){
173588 int octet = (sqlite3HexToInt(zUri[iIn++]) << 4);
173589 octet += sqlite3HexToInt(zUri[iIn++]);
173590
173591 assert( octet>=0 && octet<256 )((void) (0));
173592 if( octet==0 ){
173593#ifndef SQLITE_ENABLE_URI_00_ERROR
173594 /* This branch is taken when "%00" appears within the URI. In this
173595 ** case we ignore all text in the remainder of the path, name or
173596 ** value currently being parsed. So ignore the current character
173597 ** and skip to the next "?", "=" or "&", as appropriate. */
173598 while( (c = zUri[iIn])!=0 && c!='#'
173599 && (eState!=0 || c!='?')
173600 && (eState!=1 || (c!='=' && c!='&'))
173601 && (eState!=2 || c!='&')
173602 ){
173603 iIn++;
173604 }
173605 continue;
173606#else
173607 /* If ENABLE_URI_00_ERROR is defined, "%00" in a URI is an error. */
173608 *pzErrMsg = sqlite3_mprintf("unexpected %%00 in uri");
173609 rc = SQLITE_ERROR1;
173610 goto parse_uri_out;
173611#endif
173612 }
173613 c = octet;
173614 }else if( eState==1 && (c=='&' || c=='=') ){
173615 if( zFile[iOut-1]==0 ){
173616 /* An empty option name. Ignore this option altogether. */
173617 while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
173618 continue;
173619 }
173620 if( c=='&' ){
173621 zFile[iOut++] = '\0';
173622 }else{
173623 eState = 2;
173624 }
173625 c = 0;
173626 }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){
173627 c = 0;
173628 eState = 1;
173629 }
173630 zFile[iOut++] = c;
173631 }
173632 if( eState==1 ) zFile[iOut++] = '\0';
173633 memset(zFile+iOut, 0, 4); /* end-of-options + empty journal filenames */
173634
173635 /* Check if there were any options specified that should be interpreted
173636 ** here. Options that are interpreted here include "vfs" and those that
173637 ** correspond to flags that may be passed to the sqlite3_open_v2()
173638 ** method. */
173639 zOpt = &zFile[sqlite3Strlen30(zFile)+1];
173640 while( zOpt[0] ){
173641 int nOpt = sqlite3Strlen30(zOpt);
173642 char *zVal = &zOpt[nOpt+1];
173643 int nVal = sqlite3Strlen30(zVal);
173644
173645 if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){
173646 zVfs = zVal;
173647 }else{
173648 struct OpenMode {
173649 const char *z;
173650 int mode;
173651 } *aMode = 0;
173652 char *zModeType = 0;
173653 int mask = 0;
173654 int limit = 0;
173655
173656 if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){
173657 static struct OpenMode aCacheMode[] = {
173658 { "shared", SQLITE_OPEN_SHAREDCACHE0x00020000 },
173659 { "private", SQLITE_OPEN_PRIVATECACHE0x00040000 },
173660 { 0, 0 }
173661 };
173662
173663 mask = SQLITE_OPEN_SHAREDCACHE0x00020000|SQLITE_OPEN_PRIVATECACHE0x00040000;
173664 aMode = aCacheMode;
173665 limit = mask;
173666 zModeType = "cache";
173667 }
173668 if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){
173669 static struct OpenMode aOpenMode[] = {
173670 { "ro", SQLITE_OPEN_READONLY0x00000001 },
173671 { "rw", SQLITE_OPEN_READWRITE0x00000002 },
173672 { "rwc", SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004 },
173673 { "memory", SQLITE_OPEN_MEMORY0x00000080 },
173674 { 0, 0 }
173675 };
173676
173677 mask = SQLITE_OPEN_READONLY0x00000001 | SQLITE_OPEN_READWRITE0x00000002
173678 | SQLITE_OPEN_CREATE0x00000004 | SQLITE_OPEN_MEMORY0x00000080;
173679 aMode = aOpenMode;
173680 limit = mask & flags;
173681 zModeType = "access";
173682 }
173683
173684 if( aMode ){
173685 int i;
173686 int mode = 0;
173687 for(i=0; aMode[i].z; i++){
173688 const char *z = aMode[i].z;
173689 if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){
173690 mode = aMode[i].mode;
173691 break;
173692 }
173693 }
173694 if( mode==0 ){
173695 *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal);
173696 rc = SQLITE_ERROR1;
173697 goto parse_uri_out;
173698 }
173699 if( (mode & ~SQLITE_OPEN_MEMORY0x00000080)>limit ){
173700 *pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s",
173701 zModeType, zVal);
173702 rc = SQLITE_PERM3;
173703 goto parse_uri_out;
173704 }
173705 flags = (flags & ~mask) | mode;
173706 }
173707 }
173708
173709 zOpt = &zVal[nVal+1];
173710 }
173711
173712 }else{
173713 zFile = sqlite3_malloc64(nUri+8);
173714 if( !zFile ) return SQLITE_NOMEM_BKPT7;
173715 memset(zFile, 0, 4);
173716 zFile += 4;
173717 if( nUri ){
173718 memcpy(zFile, zUri, nUri);
173719 }
173720 memset(zFile+nUri, 0, 4);
173721 flags &= ~SQLITE_OPEN_URI0x00000040;
173722 }
173723
173724 *ppVfs = sqlite3_vfs_find(zVfs);
173725 if( *ppVfs==0 ){
173726 *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs);
173727 rc = SQLITE_ERROR1;
173728 }
173729 parse_uri_out:
173730 if( rc!=SQLITE_OK0 ){
173731 sqlite3_free_filename(zFile);
173732 zFile = 0;
173733 }
173734 *pFlags = flags;
173735 *pzFile = zFile;
173736 return rc;
173737}
173738
173739/*
173740** This routine does the core work of extracting URI parameters from a
173741** database filename for the sqlite3_uri_parameter() interface.
173742*/
173743static const char *uriParameter(const char *zFilename, const char *zParam){
173744 zFilename += sqlite3Strlen30(zFilename) + 1;
173745 while( ALWAYS(zFilename!=0)(zFilename!=0) && zFilename[0] ){
173746 int x = strcmp(zFilename, zParam);
173747 zFilename += sqlite3Strlen30(zFilename) + 1;
173748 if( x==0 ) return zFilename;
173749 zFilename += sqlite3Strlen30(zFilename) + 1;
173750 }
173751 return 0;
173752}
173753
173754
173755
173756/*
173757** This routine does the work of opening a database on behalf of
173758** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
173759** is UTF-8 encoded.
173760*/
173761static int openDatabase(
173762 const char *zFilename, /* Database filename UTF-8 encoded */
173763 sqlite3 **ppDb, /* OUT: Returned database handle */
173764 unsigned int flags, /* Operational flags */
173765 const char *zVfs /* Name of the VFS to use */
173766){
173767 sqlite3 *db; /* Store allocated handle here */
173768 int rc; /* Return code */
173769 int isThreadsafe; /* True for threadsafe connections */
173770 char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */
173771 char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */
173772 int i; /* Loop counter */
173773
173774#ifdef SQLITE_ENABLE_API_ARMOR1
173775 if( ppDb==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(173775);
173776#endif
173777 *ppDb = 0;
173778#ifndef SQLITE_OMIT_AUTOINIT
173779 rc = sqlite3_initialize();
173780 if( rc ) return rc;
173781#endif
173782
173783 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex==0 ){
173784 isThreadsafe = 0;
173785 }else if( flags & SQLITE_OPEN_NOMUTEX0x00008000 ){
173786 isThreadsafe = 0;
173787 }else if( flags & SQLITE_OPEN_FULLMUTEX0x00010000 ){
173788 isThreadsafe = 1;
173789 }else{
173790 isThreadsafe = sqlite3GlobalConfigsqlite3Config.bFullMutex;
173791 }
173792
173793 if( flags & SQLITE_OPEN_PRIVATECACHE0x00040000 ){
173794 flags &= ~SQLITE_OPEN_SHAREDCACHE0x00020000;
173795 }else if( sqlite3GlobalConfigsqlite3Config.sharedCacheEnabled ){
173796 flags |= SQLITE_OPEN_SHAREDCACHE0x00020000;
173797 }
173798
173799 /* Remove harmful bits from the flags parameter
173800 **
173801 ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
173802 ** dealt with in the previous code block. Besides these, the only
173803 ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
173804 ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE,
173805 ** SQLITE_OPEN_PRIVATECACHE, SQLITE_OPEN_EXRESCODE, and some reserved
173806 ** bits. Silently mask off all other flags.
173807 */
173808 flags &= ~( SQLITE_OPEN_DELETEONCLOSE0x00000008 |
173809 SQLITE_OPEN_EXCLUSIVE0x00000010 |
173810 SQLITE_OPEN_MAIN_DB0x00000100 |
173811 SQLITE_OPEN_TEMP_DB0x00000200 |
173812 SQLITE_OPEN_TRANSIENT_DB0x00000400 |
173813 SQLITE_OPEN_MAIN_JOURNAL0x00000800 |
173814 SQLITE_OPEN_TEMP_JOURNAL0x00001000 |
173815 SQLITE_OPEN_SUBJOURNAL0x00002000 |
173816 SQLITE_OPEN_SUPER_JOURNAL0x00004000 |
173817 SQLITE_OPEN_NOMUTEX0x00008000 |
173818 SQLITE_OPEN_FULLMUTEX0x00010000 |
173819 SQLITE_OPEN_WAL0x00080000
173820 );
173821
173822 /* Allocate the sqlite data structure */
173823 db = sqlite3MallocZero( sizeof(sqlite3) );
173824 if( db==0 ) goto opendb_out;
173825 if( isThreadsafe
173826#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
173827 || sqlite3GlobalConfigsqlite3Config.bCoreMutex
173828#endif
173829 ){
173830 db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE1);
173831 if( db->mutex==0 ){
173832 sqlite3_free(db);
173833 db = 0;
173834 goto opendb_out;
173835 }
173836 if( isThreadsafe==0 ){
173837 sqlite3MutexWarnOnContention(db->mutex);
173838 }
173839 }
173840 sqlite3_mutex_enter(db->mutex);
173841 db->errMask = (flags & SQLITE_OPEN_EXRESCODE0x02000000)!=0 ? 0xffffffff : 0xff;
173842 db->nDb = 2;
173843 db->eOpenState = SQLITE_STATE_BUSY0x6d;
173844 db->aDb = db->aDbStatic;
173845 db->lookaside.bDisable = 1;
173846 db->lookaside.sz = 0;
173847
173848 assert( sizeof(db->aLimit)==sizeof(aHardLimit) )((void) (0));
173849 memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
173850 db->aLimit[SQLITE_LIMIT_WORKER_THREADS11] = SQLITE_DEFAULT_WORKER_THREADS0;
173851 db->autoCommit = 1;
173852 db->nextAutovac = -1;
173853 db->szMmap = sqlite3GlobalConfigsqlite3Config.szMmap;
173854 db->nextPagesize = 0;
173855 db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */
173856#ifdef SQLITE_ENABLE_SORTER_MMAP
173857 /* Beginning with version 3.37.0, using the VFS xFetch() API to memory-map
173858 ** the temporary files used to do external sorts (see code in vdbesort.c)
173859 ** is disabled. It can still be used either by defining
173860 ** SQLITE_ENABLE_SORTER_MMAP at compile time or by using the
173861 ** SQLITE_TESTCTRL_SORTER_MMAP test-control at runtime. */
173862 db->nMaxSorterMmap = 0x7FFFFFFF;
173863#endif
173864 db->flags |= SQLITE_ShortColNames0x00000040
173865 | SQLITE_EnableTrigger0x00040000
173866 | SQLITE_EnableView0x80000000
173867 | SQLITE_CacheSpill0x00000020
173868#if !defined(SQLITE_TRUSTED_SCHEMA) || SQLITE_TRUSTED_SCHEMA+0!=0
173869 | SQLITE_TrustedSchema0x00000080
173870#endif
173871/* The SQLITE_DQS compile-time option determines the default settings
173872** for SQLITE_DBCONFIG_DQS_DDL and SQLITE_DBCONFIG_DQS_DML.
173873**
173874** SQLITE_DQS SQLITE_DBCONFIG_DQS_DDL SQLITE_DBCONFIG_DQS_DML
173875** ---------- ----------------------- -----------------------
173876** undefined on on
173877** 3 on on
173878** 2 on off
173879** 1 off on
173880** 0 off off
173881**
173882** Legacy behavior is 3 (double-quoted string literals are allowed anywhere)
173883** and so that is the default. But developers are encouranged to use
173884** -DSQLITE_DQS=0 (best) or -DSQLITE_DQS=1 (second choice) if possible.
173885*/
173886#if !defined(SQLITE_DQS0)
173887# define SQLITE_DQS0 3
173888#endif
173889#if (SQLITE_DQS0&1)==1
173890 | SQLITE_DqsDML0x40000000
173891#endif
173892#if (SQLITE_DQS0&2)==2
173893 | SQLITE_DqsDDL0x20000000
173894#endif
173895
173896#if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX
173897 | SQLITE_AutoIndex0x00008000
173898#endif
173899#if SQLITE_DEFAULT_CKPTFULLFSYNC1
173900 | SQLITE_CkptFullFSync0x00000010
173901#endif
173902#if SQLITE_DEFAULT_FILE_FORMAT4<4
173903 | SQLITE_LegacyFileFmt0x00000002
173904#endif
173905#ifdef SQLITE_ENABLE_LOAD_EXTENSION
173906 | SQLITE_LoadExtension0x00010000
173907#endif
173908#if SQLITE_DEFAULT_RECURSIVE_TRIGGERS0
173909 | SQLITE_RecTriggers0x00002000
173910#endif
173911#if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS
173912 | SQLITE_ForeignKeys0x00004000
173913#endif
173914#if defined(SQLITE_REVERSE_UNORDERED_SELECTS)
173915 | SQLITE_ReverseOrder0x00001000
173916#endif
173917#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
173918 | SQLITE_CellSizeCk0x00200000
173919#endif
173920#if defined(SQLITE_ENABLE_FTS3_TOKENIZER1)
173921 | SQLITE_Fts3Tokenizer0x00400000
173922#endif
173923#if defined(SQLITE_ENABLE_QPSG)
173924 | SQLITE_EnableQPSG0x00800000
173925#endif
173926#if defined(SQLITE_DEFAULT_DEFENSIVE)
173927 | SQLITE_Defensive0x10000000
173928#endif
173929#if defined(SQLITE_DEFAULT_LEGACY_ALTER_TABLE)
173930 | SQLITE_LegacyAlter0x04000000
173931#endif
173932 ;
173933 sqlite3HashInit(&db->aCollSeq);
173934#ifndef SQLITE_OMIT_VIRTUALTABLE
173935 sqlite3HashInit(&db->aModule);
173936#endif
173937
173938 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
173939 ** and UTF-16, so add a version for each to avoid any unnecessary
173940 ** conversions. The only error that can occur here is a malloc() failure.
173941 **
173942 ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating
173943 ** functions:
173944 */
173945 createCollation(db, sqlite3StrBINARY, SQLITE_UTF81, 0, binCollFunc, 0);
173946 createCollation(db, sqlite3StrBINARY, SQLITE_UTF16BE3, 0, binCollFunc, 0);
173947 createCollation(db, sqlite3StrBINARY, SQLITE_UTF16LE2, 0, binCollFunc, 0);
173948 createCollation(db, "NOCASE", SQLITE_UTF81, 0, nocaseCollatingFunc, 0);
173949 createCollation(db, "RTRIM", SQLITE_UTF81, 0, rtrimCollFunc, 0);
173950 if( db->mallocFailed ){
173951 goto opendb_out;
173952 }
173953
173954 /* Parse the filename/URI argument
173955 **
173956 ** Only allow sensible combinations of bits in the flags argument.
173957 ** Throw an error if any non-sense combination is used. If we
173958 ** do not block illegal combinations here, it could trigger
173959 ** assert() statements in deeper layers. Sensible combinations
173960 ** are:
173961 **
173962 ** 1: SQLITE_OPEN_READONLY
173963 ** 2: SQLITE_OPEN_READWRITE
173964 ** 6: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
173965 */
173966 db->openFlags = flags;
173967 assert( SQLITE_OPEN_READONLY == 0x01 )((void) (0));
173968 assert( SQLITE_OPEN_READWRITE == 0x02 )((void) (0));
173969 assert( SQLITE_OPEN_CREATE == 0x04 )((void) (0));
173970 testcase( (1<<(flags&7))==0x02 ); /* READONLY */
173971 testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
173972 testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
173973 if( ((1<<(flags&7)) & 0x46)==0 ){
173974 rc = SQLITE_MISUSE_BKPTsqlite3MisuseError(173974); /* IMP: R-18321-05872 */
173975 }else{
173976 rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
173977 }
173978 if( rc!=SQLITE_OK0 ){
173979 if( rc==SQLITE_NOMEM7 ) sqlite3OomFault(db);
173980 sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
173981 sqlite3_free(zErrMsg);
173982 goto opendb_out;
173983 }
173984
173985 /* Open the backend database driver */
173986 rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
173987 flags | SQLITE_OPEN_MAIN_DB0x00000100);
173988 if( rc!=SQLITE_OK0 ){
173989 if( rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
173990 rc = SQLITE_NOMEM_BKPT7;
173991 }
173992 sqlite3Error(db, rc);
173993 goto opendb_out;
173994 }
173995 sqlite3BtreeEnter(db->aDb[0].pBt);
173996 db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
173997 if( !db->mallocFailed ){
173998 sqlite3SetTextEncoding(db, SCHEMA_ENC(db)((db)->aDb[0].pSchema->enc));
173999 }
174000 sqlite3BtreeLeave(db->aDb[0].pBt);
174001 db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
174002
174003 /* The default safety_level for the main database is FULL; for the temp
174004 ** database it is OFF. This matches the pager layer defaults.
174005 */
174006 db->aDb[0].zDbSName = "main";
174007 db->aDb[0].safety_level = SQLITE_DEFAULT_SYNCHRONOUS2+1;
174008 db->aDb[1].zDbSName = "temp";
174009 db->aDb[1].safety_level = PAGER_SYNCHRONOUS_OFF0x01;
174010
174011 db->eOpenState = SQLITE_STATE_OPEN0x76;
174012 if( db->mallocFailed ){
174013 goto opendb_out;
174014 }
174015
174016 /* Register all built-in functions, but do not attempt to read the
174017 ** database schema yet. This is delayed until the first time the database
174018 ** is accessed.
174019 */
174020 sqlite3Error(db, SQLITE_OK0);
174021 sqlite3RegisterPerConnectionBuiltinFunctions(db);
174022 rc = sqlite3_errcode(db);
174023
174024
174025 /* Load compiled-in extensions */
174026 for(i=0; rc==SQLITE_OK0 && i<ArraySize(sqlite3BuiltinExtensions)((int)(sizeof(sqlite3BuiltinExtensions)/sizeof(sqlite3BuiltinExtensions
[0])))
; i++){
174027 rc = sqlite3BuiltinExtensions[i](db);
174028 }
174029
174030 /* Load automatic extensions - extensions that have been registered
174031 ** using the sqlite3_automatic_extension() API.
174032 */
174033 if( rc==SQLITE_OK0 ){
174034 sqlite3AutoLoadExtensions(db);
174035 rc = sqlite3_errcode(db);
174036 if( rc!=SQLITE_OK0 ){
174037 goto opendb_out;
174038 }
174039 }
174040
174041#ifdef SQLITE_ENABLE_INTERNAL_FUNCTIONS
174042 /* Testing use only!!! The -DSQLITE_ENABLE_INTERNAL_FUNCTIONS=1 compile-time
174043 ** option gives access to internal functions by default.
174044 ** Testing use only!!! */
174045 db->mDbFlags |= DBFLAG_InternalFunc0x0020;
174046#endif
174047
174048 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
174049 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
174050 ** mode. Doing nothing at all also makes NORMAL the default.
174051 */
174052#ifdef SQLITE_DEFAULT_LOCKING_MODE
174053 db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
174054 sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
174055 SQLITE_DEFAULT_LOCKING_MODE);
174056#endif
174057
174058 if( rc ) sqlite3Error(db, rc);
174059
174060 /* Enable the lookaside-malloc subsystem */
174061 setupLookaside(db, 0, sqlite3GlobalConfigsqlite3Config.szLookaside,
174062 sqlite3GlobalConfigsqlite3Config.nLookaside);
174063
174064 sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT1000);
174065
174066opendb_out:
174067 if( db ){
174068 assert( db->mutex!=0 || isThreadsafe==0((void) (0))
174069 || sqlite3GlobalConfig.bFullMutex==0 )((void) (0));
174070 sqlite3_mutex_leave(db->mutex);
174071 }
174072 rc = sqlite3_errcode(db);
174073 assert( db!=0 || (rc&0xff)==SQLITE_NOMEM )((void) (0));
174074 if( (rc&0xff)==SQLITE_NOMEM7 ){
174075 sqlite3_close(db);
174076 db = 0;
174077 }else if( rc!=SQLITE_OK0 ){
174078 db->eOpenState = SQLITE_STATE_SICK0xba;
174079 }
174080 *ppDb = db;
174081#ifdef SQLITE_ENABLE_SQLLOG
174082 if( sqlite3GlobalConfigsqlite3Config.xSqllog ){
174083 /* Opening a db handle. Fourth parameter is passed 0. */
174084 void *pArg = sqlite3GlobalConfigsqlite3Config.pSqllogArg;
174085 sqlite3GlobalConfigsqlite3Config.xSqllog(pArg, db, zFilename, 0);
174086 }
174087#endif
174088 sqlite3_free_filename(zOpen);
174089 return rc;
174090}
174091
174092
174093/*
174094** Open a new database handle.
174095*/
174096SQLITE_API int sqlite3_open(
174097 const char *zFilename,
174098 sqlite3 **ppDb
174099){
174100 return openDatabase(zFilename, ppDb,
174101 SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004, 0);
174102}
174103SQLITE_API int sqlite3_open_v2(
174104 const char *filename, /* Database filename (UTF-8) */
174105 sqlite3 **ppDb, /* OUT: SQLite db handle */
174106 int flags, /* Flags */
174107 const char *zVfs /* Name of VFS module to use */
174108){
174109 return openDatabase(filename, ppDb, (unsigned int)flags, zVfs);
174110}
174111
174112#ifndef SQLITE_OMIT_UTF16
174113/*
174114** Open a new database handle.
174115*/
174116SQLITE_API int sqlite3_open16(
174117 const void *zFilename,
174118 sqlite3 **ppDb
174119){
174120 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
174121 sqlite3_value *pVal;
174122 int rc;
174123
174124#ifdef SQLITE_ENABLE_API_ARMOR1
174125 if( ppDb==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(174125);
174126#endif
174127 *ppDb = 0;
174128#ifndef SQLITE_OMIT_AUTOINIT
174129 rc = sqlite3_initialize();
174130 if( rc ) return rc;
174131#endif
174132 if( zFilename==0 ) zFilename = "\000\000";
174133 pVal = sqlite3ValueNew(0);
174134 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE2, SQLITE_STATIC((sqlite3_destructor_type)0));
174135 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF81);
174136 if( zFilename8 ){
174137 rc = openDatabase(zFilename8, ppDb,
174138 SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004, 0);
174139 assert( *ppDb || rc==SQLITE_NOMEM )((void) (0));
174140 if( rc==SQLITE_OK0 && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded)(((*ppDb)->aDb[0].pSchema->schemaFlags&(0x0001))==(
0x0001))
){
174141 SCHEMA_ENC(*ppDb)((*ppDb)->aDb[0].pSchema->enc) = ENC(*ppDb)((*ppDb)->enc) = SQLITE_UTF16NATIVE2;
174142 }
174143 }else{
174144 rc = SQLITE_NOMEM_BKPT7;
174145 }
174146 sqlite3ValueFree(pVal);
174147
174148 return rc & 0xff;
174149}
174150#endif /* SQLITE_OMIT_UTF16 */
174151
174152/*
174153** Register a new collation sequence with the database handle db.
174154*/
174155SQLITE_API int sqlite3_create_collation(
174156 sqlite3* db,
174157 const char *zName,
174158 int enc,
174159 void* pCtx,
174160 int(*xCompare)(void*,int,const void*,int,const void*)
174161){
174162 return sqlite3_create_collation_v2(db, zName, enc, pCtx, xCompare, 0);
174163}
174164
174165/*
174166** Register a new collation sequence with the database handle db.
174167*/
174168SQLITE_API int sqlite3_create_collation_v2(
174169 sqlite3* db,
174170 const char *zName,
174171 int enc,
174172 void* pCtx,
174173 int(*xCompare)(void*,int,const void*,int,const void*),
174174 void(*xDel)(void*)
174175){
174176 int rc;
174177
174178#ifdef SQLITE_ENABLE_API_ARMOR1
174179 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(174179);
174180#endif
174181 sqlite3_mutex_enter(db->mutex);
174182 assert( !db->mallocFailed )((void) (0));
174183 rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel);
174184 rc = sqlite3ApiExit(db, rc);
174185 sqlite3_mutex_leave(db->mutex);
174186 return rc;
174187}
174188
174189#ifndef SQLITE_OMIT_UTF16
174190/*
174191** Register a new collation sequence with the database handle db.
174192*/
174193SQLITE_API int sqlite3_create_collation16(
174194 sqlite3* db,
174195 const void *zName,
174196 int enc,
174197 void* pCtx,
174198 int(*xCompare)(void*,int,const void*,int,const void*)
174199){
174200 int rc = SQLITE_OK0;
174201 char *zName8;
174202
174203#ifdef SQLITE_ENABLE_API_ARMOR1
174204 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(174204);
174205#endif
174206 sqlite3_mutex_enter(db->mutex);
174207 assert( !db->mallocFailed )((void) (0));
174208 zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE2);
174209 if( zName8 ){
174210 rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0);
174211 sqlite3DbFree(db, zName8);
174212 }
174213 rc = sqlite3ApiExit(db, rc);
174214 sqlite3_mutex_leave(db->mutex);
174215 return rc;
174216}
174217#endif /* SQLITE_OMIT_UTF16 */
174218
174219/*
174220** Register a collation sequence factory callback with the database handle
174221** db. Replace any previously installed collation sequence factory.
174222*/
174223SQLITE_API int sqlite3_collation_needed(
174224 sqlite3 *db,
174225 void *pCollNeededArg,
174226 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
174227){
174228#ifdef SQLITE_ENABLE_API_ARMOR1
174229 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(174229);
174230#endif
174231 sqlite3_mutex_enter(db->mutex);
174232 db->xCollNeeded = xCollNeeded;
174233 db->xCollNeeded16 = 0;
174234 db->pCollNeededArg = pCollNeededArg;
174235 sqlite3_mutex_leave(db->mutex);
174236 return SQLITE_OK0;
174237}
174238
174239#ifndef SQLITE_OMIT_UTF16
174240/*
174241** Register a collation sequence factory callback with the database handle
174242** db. Replace any previously installed collation sequence factory.
174243*/
174244SQLITE_API int sqlite3_collation_needed16(
174245 sqlite3 *db,
174246 void *pCollNeededArg,
174247 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
174248){
174249#ifdef SQLITE_ENABLE_API_ARMOR1
174250 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(174250);
174251#endif
174252 sqlite3_mutex_enter(db->mutex);
174253 db->xCollNeeded = 0;
174254 db->xCollNeeded16 = xCollNeeded16;
174255 db->pCollNeededArg = pCollNeededArg;
174256 sqlite3_mutex_leave(db->mutex);
174257 return SQLITE_OK0;
174258}
174259#endif /* SQLITE_OMIT_UTF16 */
174260
174261#ifndef SQLITE_OMIT_DEPRECATED1
174262/*
174263** This function is now an anachronism. It used to be used to recover from a
174264** malloc() failure, but SQLite now does this automatically.
174265*/
174266SQLITE_API int sqlite3_global_recover(void){
174267 return SQLITE_OK0;
174268}
174269#endif
174270
174271/*
174272** Test to see whether or not the database connection is in autocommit
174273** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on
174274** by default. Autocommit is disabled by a BEGIN statement and reenabled
174275** by the next COMMIT or ROLLBACK.
174276*/
174277SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
174278#ifdef SQLITE_ENABLE_API_ARMOR1
174279 if( !sqlite3SafetyCheckOk(db) ){
174280 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(174280);
174281 return 0;
174282 }
174283#endif
174284 return db->autoCommit;
174285}
174286
174287/*
174288** The following routines are substitutes for constants SQLITE_CORRUPT,
174289** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_NOMEM and possibly other error
174290** constants. They serve two purposes:
174291**
174292** 1. Serve as a convenient place to set a breakpoint in a debugger
174293** to detect when version error conditions occurs.
174294**
174295** 2. Invoke sqlite3_log() to provide the source code location where
174296** a low-level error is first detected.
174297*/
174298SQLITE_PRIVATEstatic int sqlite3ReportError(int iErr, int lineno, const char *zType){
174299 sqlite3_log(iErr, "%s at line %d of [%.10s]",
174300 zType, lineno, 20+sqlite3_sourceid());
174301 return iErr;
174302}
174303SQLITE_PRIVATEstatic int sqlite3CorruptError(int lineno){
174304 testcase( sqlite3GlobalConfig.xLog!=0 );
174305 return sqlite3ReportError(SQLITE_CORRUPT11, lineno, "database corruption");
174306}
174307SQLITE_PRIVATEstatic int sqlite3MisuseError(int lineno){
174308 testcase( sqlite3GlobalConfig.xLog!=0 );
174309 return sqlite3ReportError(SQLITE_MISUSE21, lineno, "misuse");
174310}
174311SQLITE_PRIVATEstatic int sqlite3CantopenError(int lineno){
174312 testcase( sqlite3GlobalConfig.xLog!=0 );
174313 return sqlite3ReportError(SQLITE_CANTOPEN14, lineno, "cannot open file");
174314}
174315#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_CORRUPT_PGNO)
174316SQLITE_PRIVATEstatic int sqlite3CorruptPgnoError(int lineno, Pgno pgno){
174317 char zMsg[100];
174318 sqlite3_snprintf(sizeof(zMsg), zMsg, "database corruption page %d", pgno);
174319 testcase( sqlite3GlobalConfig.xLog!=0 );
174320 return sqlite3ReportError(SQLITE_CORRUPT11, lineno, zMsg);
174321}
174322#endif
174323#ifdef SQLITE_DEBUG
174324SQLITE_PRIVATEstatic int sqlite3NomemError(int lineno){
174325 testcase( sqlite3GlobalConfig.xLog!=0 );
174326 return sqlite3ReportError(SQLITE_NOMEM7, lineno, "OOM");
174327}
174328SQLITE_PRIVATEstatic int sqlite3IoerrnomemError(int lineno){
174329 testcase( sqlite3GlobalConfig.xLog!=0 );
174330 return sqlite3ReportError(SQLITE_IOERR_NOMEM(10 | (12<<8)), lineno, "I/O OOM error");
174331}
174332#endif
174333
174334#ifndef SQLITE_OMIT_DEPRECATED1
174335/*
174336** This is a convenience routine that makes sure that all thread-specific
174337** data for this thread has been deallocated.
174338**
174339** SQLite no longer uses thread-specific data so this routine is now a
174340** no-op. It is retained for historical compatibility.
174341*/
174342SQLITE_API void sqlite3_thread_cleanup(void){
174343}
174344#endif
174345
174346/*
174347** Return meta information about a specific column of a database table.
174348** See comment in sqlite3.h (sqlite.h.in) for details.
174349*/
174350SQLITE_API int sqlite3_table_column_metadata(
174351 sqlite3 *db, /* Connection handle */
174352 const char *zDbName, /* Database name or NULL */
174353 const char *zTableName, /* Table name */
174354 const char *zColumnName, /* Column name */
174355 char const **pzDataType, /* OUTPUT: Declared data type */
174356 char const **pzCollSeq, /* OUTPUT: Collation sequence name */
174357 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
174358 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
174359 int *pAutoinc /* OUTPUT: True if column is auto-increment */
174360){
174361 int rc;
174362 char *zErrMsg = 0;
174363 Table *pTab = 0;
174364 Column *pCol = 0;
174365 int iCol = 0;
174366 char const *zDataType = 0;
174367 char const *zCollSeq = 0;
174368 int notnull = 0;
174369 int primarykey = 0;
174370 int autoinc = 0;
174371
174372
174373#ifdef SQLITE_ENABLE_API_ARMOR1
174374 if( !sqlite3SafetyCheckOk(db) || zTableName==0 ){
174375 return SQLITE_MISUSE_BKPTsqlite3MisuseError(174375);
174376 }
174377#endif
174378
174379 /* Ensure the database schema has been loaded */
174380 sqlite3_mutex_enter(db->mutex);
174381 sqlite3BtreeEnterAll(db);
174382 rc = sqlite3Init(db, &zErrMsg);
174383 if( SQLITE_OK0!=rc ){
174384 goto error_out;
174385 }
174386
174387 /* Locate the table in question */
174388 pTab = sqlite3FindTable(db, zTableName, zDbName);
174389 if( !pTab || IsView(pTab)((pTab)->eTabType==2) ){
174390 pTab = 0;
174391 goto error_out;
174392 }
174393
174394 /* Find the column for which info is requested */
174395 if( zColumnName==0 ){
174396 /* Query for existance of table only */
174397 }else{
174398 for(iCol=0; iCol<pTab->nCol; iCol++){
174399 pCol = &pTab->aCol[iCol];
174400 if( 0==sqlite3StrICmp(pCol->zCnName, zColumnName) ){
174401 break;
174402 }
174403 }
174404 if( iCol==pTab->nCol ){
174405 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) && sqlite3IsRowid(zColumnName) ){
174406 iCol = pTab->iPKey;
174407 pCol = iCol>=0 ? &pTab->aCol[iCol] : 0;
174408 }else{
174409 pTab = 0;
174410 goto error_out;
174411 }
174412 }
174413 }
174414
174415 /* The following block stores the meta information that will be returned
174416 ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
174417 ** and autoinc. At this point there are two possibilities:
174418 **
174419 ** 1. The specified column name was rowid", "oid" or "_rowid_"
174420 ** and there is no explicitly declared IPK column.
174421 **
174422 ** 2. The table is not a view and the column name identified an
174423 ** explicitly declared column. Copy meta information from *pCol.
174424 */
174425 if( pCol ){
174426 zDataType = sqlite3ColumnType(pCol,0);
174427 zCollSeq = sqlite3ColumnColl(pCol);
174428 notnull = pCol->notNull!=0;
174429 primarykey = (pCol->colFlags & COLFLAG_PRIMKEY0x0001)!=0;
174430 autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement0x00000008)!=0;
174431 }else{
174432 zDataType = "INTEGER";
174433 primarykey = 1;
174434 }
174435 if( !zCollSeq ){
174436 zCollSeq = sqlite3StrBINARY;
174437 }
174438
174439error_out:
174440 sqlite3BtreeLeaveAll(db);
174441
174442 /* Whether the function call succeeded or failed, set the output parameters
174443 ** to whatever their local counterparts contain. If an error did occur,
174444 ** this has the effect of zeroing all output parameters.
174445 */
174446 if( pzDataType ) *pzDataType = zDataType;
174447 if( pzCollSeq ) *pzCollSeq = zCollSeq;
174448 if( pNotNull ) *pNotNull = notnull;
174449 if( pPrimaryKey ) *pPrimaryKey = primarykey;
174450 if( pAutoinc ) *pAutoinc = autoinc;
174451
174452 if( SQLITE_OK0==rc && !pTab ){
174453 sqlite3DbFree(db, zErrMsg);
174454 zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
174455 zColumnName);
174456 rc = SQLITE_ERROR1;
174457 }
174458 sqlite3ErrorWithMsg(db, rc, (zErrMsg?"%s":0), zErrMsg);
174459 sqlite3DbFree(db, zErrMsg);
174460 rc = sqlite3ApiExit(db, rc);
174461 sqlite3_mutex_leave(db->mutex);
174462 return rc;
174463}
174464
174465/*
174466** Sleep for a little while. Return the amount of time slept.
174467*/
174468SQLITE_API int sqlite3_sleep(int ms){
174469 sqlite3_vfs *pVfs;
174470 int rc;
174471 pVfs = sqlite3_vfs_find(0);
174472 if( pVfs==0 ) return 0;
174473
174474 /* This function works in milliseconds, but the underlying OsSleep()
174475 ** API uses microseconds. Hence the 1000's.
174476 */
174477 rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
174478 return rc;
174479}
174480
174481/*
174482** Enable or disable the extended result codes.
174483*/
174484SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
174485#ifdef SQLITE_ENABLE_API_ARMOR1
174486 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(174486);
174487#endif
174488 sqlite3_mutex_enter(db->mutex);
174489 db->errMask = onoff ? 0xffffffff : 0xff;
174490 sqlite3_mutex_leave(db->mutex);
174491 return SQLITE_OK0;
174492}
174493
174494/*
174495** Invoke the xFileControl method on a particular database.
174496*/
174497SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
174498 int rc = SQLITE_ERROR1;
174499 Btree *pBtree;
174500
174501#ifdef SQLITE_ENABLE_API_ARMOR1
174502 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(174502);
174503#endif
174504 sqlite3_mutex_enter(db->mutex);
174505 pBtree = sqlite3DbNameToBtree(db, zDbName);
174506 if( pBtree ){
174507 Pager *pPager;
174508 sqlite3_file *fd;
174509 sqlite3BtreeEnter(pBtree);
174510 pPager = sqlite3BtreePager(pBtree);
174511 assert( pPager!=0 )((void) (0));
174512 fd = sqlite3PagerFile(pPager);
174513 assert( fd!=0 )((void) (0));
174514 if( op==SQLITE_FCNTL_FILE_POINTER7 ){
174515 *(sqlite3_file**)pArg = fd;
174516 rc = SQLITE_OK0;
174517 }else if( op==SQLITE_FCNTL_VFS_POINTER27 ){
174518 *(sqlite3_vfs**)pArg = sqlite3PagerVfs(pPager);
174519 rc = SQLITE_OK0;
174520 }else if( op==SQLITE_FCNTL_JOURNAL_POINTER28 ){
174521 *(sqlite3_file**)pArg = sqlite3PagerJrnlFile(pPager);
174522 rc = SQLITE_OK0;
174523 }else if( op==SQLITE_FCNTL_DATA_VERSION35 ){
174524 *(unsigned int*)pArg = sqlite3PagerDataVersion(pPager);
174525 rc = SQLITE_OK0;
174526 }else if( op==SQLITE_FCNTL_RESERVE_BYTES38 ){
174527 int iNew = *(int*)pArg;
174528 *(int*)pArg = sqlite3BtreeGetRequestedReserve(pBtree);
174529 if( iNew>=0 && iNew<=255 ){
174530 sqlite3BtreeSetPageSize(pBtree, 0, iNew, 0);
174531 }
174532 rc = SQLITE_OK0;
174533 }else{
174534 int nSave = db->busyHandler.nBusy;
174535 rc = sqlite3OsFileControl(fd, op, pArg);
174536 db->busyHandler.nBusy = nSave;
174537 }
174538 sqlite3BtreeLeave(pBtree);
174539 }
174540 sqlite3_mutex_leave(db->mutex);
174541 return rc;
174542}
174543
174544/*
174545** Interface to the testing logic.
174546*/
174547SQLITE_API int sqlite3_test_control(int op, ...){
174548 int rc = 0;
174549#ifdef SQLITE_UNTESTABLE
174550 UNUSED_PARAMETER(op)(void)(op);
174551#else
174552 va_list ap;
174553 va_start(ap, op)__builtin_va_start(ap, op);
174554 switch( op ){
174555
174556 /*
174557 ** Save the current state of the PRNG.
174558 */
174559 case SQLITE_TESTCTRL_PRNG_SAVE5: {
174560 sqlite3PrngSaveState();
174561 break;
174562 }
174563
174564 /*
174565 ** Restore the state of the PRNG to the last state saved using
174566 ** PRNG_SAVE. If PRNG_SAVE has never before been called, then
174567 ** this verb acts like PRNG_RESET.
174568 */
174569 case SQLITE_TESTCTRL_PRNG_RESTORE6: {
174570 sqlite3PrngRestoreState();
174571 break;
174572 }
174573
174574 /* sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, int x, sqlite3 *db);
174575 **
174576 ** Control the seed for the pseudo-random number generator (PRNG) that
174577 ** is built into SQLite. Cases:
174578 **
174579 ** x!=0 && db!=0 Seed the PRNG to the current value of the
174580 ** schema cookie in the main database for db, or
174581 ** x if the schema cookie is zero. This case
174582 ** is convenient to use with database fuzzers
174583 ** as it allows the fuzzer some control over the
174584 ** the PRNG seed.
174585 **
174586 ** x!=0 && db==0 Seed the PRNG to the value of x.
174587 **
174588 ** x==0 && db==0 Revert to default behavior of using the
174589 ** xRandomness method on the primary VFS.
174590 **
174591 ** This test-control also resets the PRNG so that the new seed will
174592 ** be used for the next call to sqlite3_randomness().
174593 */
174594#ifndef SQLITE_OMIT_WSD
174595 case SQLITE_TESTCTRL_PRNG_SEED28: {
174596 int x = va_arg(ap, int)__builtin_va_arg(ap, int);
174597 int y;
174598 sqlite3 *db = va_arg(ap, sqlite3*)__builtin_va_arg(ap, sqlite3*);
174599 assert( db==0 || db->aDb[0].pSchema!=0 )((void) (0));
174600 if( db && (y = db->aDb[0].pSchema->schema_cookie)!=0 ){ x = y; }
174601 sqlite3Config.iPrngSeed = x;
174602 sqlite3_randomness(0,0);
174603 break;
174604 }
174605#endif
174606
174607 /*
174608 ** sqlite3_test_control(BITVEC_TEST, size, program)
174609 **
174610 ** Run a test against a Bitvec object of size. The program argument
174611 ** is an array of integers that defines the test. Return -1 on a
174612 ** memory allocation error, 0 on success, or non-zero for an error.
174613 ** See the sqlite3BitvecBuiltinTest() for additional information.
174614 */
174615 case SQLITE_TESTCTRL_BITVEC_TEST8: {
174616 int sz = va_arg(ap, int)__builtin_va_arg(ap, int);
174617 int *aProg = va_arg(ap, int*)__builtin_va_arg(ap, int*);
174618 rc = sqlite3BitvecBuiltinTest(sz, aProg);
174619 break;
174620 }
174621
174622 /*
174623 ** sqlite3_test_control(FAULT_INSTALL, xCallback)
174624 **
174625 ** Arrange to invoke xCallback() whenever sqlite3FaultSim() is called,
174626 ** if xCallback is not NULL.
174627 **
174628 ** As a test of the fault simulator mechanism itself, sqlite3FaultSim(0)
174629 ** is called immediately after installing the new callback and the return
174630 ** value from sqlite3FaultSim(0) becomes the return from
174631 ** sqlite3_test_control().
174632 */
174633 case SQLITE_TESTCTRL_FAULT_INSTALL9: {
174634 /* A bug in MSVC prevents it from understanding pointers to functions
174635 ** types in the second argument to va_arg(). Work around the problem
174636 ** using a typedef.
174637 ** http://support.microsoft.com/kb/47961 <-- dead hyperlink
174638 ** Search at http://web.archive.org/ to find the 2015-03-16 archive
174639 ** of the link above to see the original text.
174640 ** sqlite3GlobalConfig.xTestCallback = va_arg(ap, int(*)(int));
174641 */
174642 typedef int(*sqlite3FaultFuncType)(int);
174643 sqlite3GlobalConfigsqlite3Config.xTestCallback = va_arg(ap, sqlite3FaultFuncType)__builtin_va_arg(ap, sqlite3FaultFuncType);
174644 rc = sqlite3FaultSim(0);
174645 break;
174646 }
174647
174648 /*
174649 ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
174650 **
174651 ** Register hooks to call to indicate which malloc() failures
174652 ** are benign.
174653 */
174654 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS10: {
174655 typedef void (*void_function)(void);
174656 void_function xBenignBegin;
174657 void_function xBenignEnd;
174658 xBenignBegin = va_arg(ap, void_function)__builtin_va_arg(ap, void_function);
174659 xBenignEnd = va_arg(ap, void_function)__builtin_va_arg(ap, void_function);
174660 sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
174661 break;
174662 }
174663
174664 /*
174665 ** sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
174666 **
174667 ** Set the PENDING byte to the value in the argument, if X>0.
174668 ** Make no changes if X==0. Return the value of the pending byte
174669 ** as it existing before this routine was called.
174670 **
174671 ** IMPORTANT: Changing the PENDING byte from 0x40000000 results in
174672 ** an incompatible database file format. Changing the PENDING byte
174673 ** while any database connection is open results in undefined and
174674 ** deleterious behavior.
174675 */
174676 case SQLITE_TESTCTRL_PENDING_BYTE11: {
174677 rc = PENDING_BYTEsqlite3PendingByte;
174678#ifndef SQLITE_OMIT_WSD
174679 {
174680 unsigned int newVal = va_arg(ap, unsigned int)__builtin_va_arg(ap, unsigned int);
174681 if( newVal ) sqlite3PendingByte = newVal;
174682 }
174683#endif
174684 break;
174685 }
174686
174687 /*
174688 ** sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
174689 **
174690 ** This action provides a run-time test to see whether or not
174691 ** assert() was enabled at compile-time. If X is true and assert()
174692 ** is enabled, then the return value is true. If X is true and
174693 ** assert() is disabled, then the return value is zero. If X is
174694 ** false and assert() is enabled, then the assertion fires and the
174695 ** process aborts. If X is false and assert() is disabled, then the
174696 ** return value is zero.
174697 */
174698 case SQLITE_TESTCTRL_ASSERT12: {
174699 volatile int x = 0;
174700 assert( /*side-effects-ok*/ (x = va_arg(ap,int))!=0 )((void) (0));
174701 rc = x;
174702#if defined(SQLITE_DEBUG)
174703 /* Invoke these debugging routines so that the compiler does not
174704 ** issue "defined but not used" warnings. */
174705 if( x==9999 ){
174706 sqlite3ShowExpr(0);
174707 sqlite3ShowExpr(0);
174708 sqlite3ShowExprList(0);
174709 sqlite3ShowIdList(0);
174710 sqlite3ShowSrcList(0);
174711 sqlite3ShowWith(0);
174712 sqlite3ShowUpsert(0);
174713 sqlite3ShowTriggerStep(0);
174714 sqlite3ShowTriggerStepList(0);
174715 sqlite3ShowTrigger(0);
174716 sqlite3ShowTriggerList(0);
174717#ifndef SQLITE_OMIT_WINDOWFUNC
174718 sqlite3ShowWindow(0);
174719 sqlite3ShowWinFunc(0);
174720#endif
174721 sqlite3ShowSelect(0);
174722 }
174723#endif
174724 break;
174725 }
174726
174727
174728 /*
174729 ** sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
174730 **
174731 ** This action provides a run-time test to see how the ALWAYS and
174732 ** NEVER macros were defined at compile-time.
174733 **
174734 ** The return value is ALWAYS(X) if X is true, or 0 if X is false.
174735 **
174736 ** The recommended test is X==2. If the return value is 2, that means
174737 ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
174738 ** default setting. If the return value is 1, then ALWAYS() is either
174739 ** hard-coded to true or else it asserts if its argument is false.
174740 ** The first behavior (hard-coded to true) is the case if
174741 ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
174742 ** behavior (assert if the argument to ALWAYS() is false) is the case if
174743 ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
174744 **
174745 ** The run-time test procedure might look something like this:
174746 **
174747 ** if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
174748 ** // ALWAYS() and NEVER() are no-op pass-through macros
174749 ** }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
174750 ** // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
174751 ** }else{
174752 ** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0.
174753 ** }
174754 */
174755 case SQLITE_TESTCTRL_ALWAYS13: {
174756 int x = va_arg(ap,int)__builtin_va_arg(ap, int);
174757 rc = x ? ALWAYS(x)(x) : 0;
174758 break;
174759 }
174760
174761 /*
174762 ** sqlite3_test_control(SQLITE_TESTCTRL_BYTEORDER);
174763 **
174764 ** The integer returned reveals the byte-order of the computer on which
174765 ** SQLite is running:
174766 **
174767 ** 1 big-endian, determined at run-time
174768 ** 10 little-endian, determined at run-time
174769 ** 432101 big-endian, determined at compile-time
174770 ** 123410 little-endian, determined at compile-time
174771 */
174772 case SQLITE_TESTCTRL_BYTEORDER22: {
174773 rc = SQLITE_BYTEORDER1234*100 + SQLITE_LITTLEENDIAN1*10 + SQLITE_BIGENDIAN0;
174774 break;
174775 }
174776
174777 /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
174778 **
174779 ** Enable or disable various optimizations for testing purposes. The
174780 ** argument N is a bitmask of optimizations to be disabled. For normal
174781 ** operation N should be 0. The idea is that a test program (like the
174782 ** SQL Logic Test or SLT test module) can run the same SQL multiple times
174783 ** with various optimizations disabled to verify that the same answer
174784 ** is obtained in every case.
174785 */
174786 case SQLITE_TESTCTRL_OPTIMIZATIONS15: {
174787 sqlite3 *db = va_arg(ap, sqlite3*)__builtin_va_arg(ap, sqlite3*);
174788 db->dbOptFlags = va_arg(ap, u32)__builtin_va_arg(ap, u32);
174789 break;
174790 }
174791
174792 /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, onoff, xAlt);
174793 **
174794 ** If parameter onoff is 1, subsequent calls to localtime() fail.
174795 ** If 2, then invoke xAlt() instead of localtime(). If 0, normal
174796 ** processing.
174797 **
174798 ** xAlt arguments are void pointers, but they really want to be:
174799 **
174800 ** int xAlt(const time_t*, struct tm*);
174801 **
174802 ** xAlt should write results in to struct tm object of its 2nd argument
174803 ** and return zero on success, or return non-zero on failure.
174804 */
174805 case SQLITE_TESTCTRL_LOCALTIME_FAULT18: {
174806 sqlite3GlobalConfigsqlite3Config.bLocaltimeFault = va_arg(ap, int)__builtin_va_arg(ap, int);
174807 if( sqlite3GlobalConfigsqlite3Config.bLocaltimeFault==2 ){
174808 typedef int(*sqlite3LocaltimeType)(const void*,void*);
174809 sqlite3GlobalConfigsqlite3Config.xAltLocaltime = va_arg(ap, sqlite3LocaltimeType)__builtin_va_arg(ap, sqlite3LocaltimeType);
174810 }else{
174811 sqlite3GlobalConfigsqlite3Config.xAltLocaltime = 0;
174812 }
174813 break;
174814 }
174815
174816 /* sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, sqlite3*);
174817 **
174818 ** Toggle the ability to use internal functions on or off for
174819 ** the database connection given in the argument.
174820 */
174821 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS17: {
174822 sqlite3 *db = va_arg(ap, sqlite3*)__builtin_va_arg(ap, sqlite3*);
174823 db->mDbFlags ^= DBFLAG_InternalFunc0x0020;
174824 break;
174825 }
174826
174827 /* sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int);
174828 **
174829 ** Set or clear a flag that indicates that the database file is always well-
174830 ** formed and never corrupt. This flag is clear by default, indicating that
174831 ** database files might have arbitrary corruption. Setting the flag during
174832 ** testing causes certain assert() statements in the code to be activated
174833 ** that demonstrat invariants on well-formed database files.
174834 */
174835 case SQLITE_TESTCTRL_NEVER_CORRUPT20: {
174836 sqlite3GlobalConfigsqlite3Config.neverCorrupt = va_arg(ap, int)__builtin_va_arg(ap, int);
174837 break;
174838 }
174839
174840 /* sqlite3_test_control(SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS, int);
174841 **
174842 ** Set or clear a flag that causes SQLite to verify that type, name,
174843 ** and tbl_name fields of the sqlite_schema table. This is normally
174844 ** on, but it is sometimes useful to turn it off for testing.
174845 **
174846 ** 2020-07-22: Disabling EXTRA_SCHEMA_CHECKS also disables the
174847 ** verification of rootpage numbers when parsing the schema. This
174848 ** is useful to make it easier to reach strange internal error states
174849 ** during testing. The EXTRA_SCHEMA_CHECKS setting is always enabled
174850 ** in production.
174851 */
174852 case SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS29: {
174853 sqlite3GlobalConfigsqlite3Config.bExtraSchemaChecks = va_arg(ap, int)__builtin_va_arg(ap, int);
174854 break;
174855 }
174856
174857 /* Set the threshold at which OP_Once counters reset back to zero.
174858 ** By default this is 0x7ffffffe (over 2 billion), but that value is
174859 ** too big to test in a reasonable amount of time, so this control is
174860 ** provided to set a small and easily reachable reset value.
174861 */
174862 case SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD19: {
174863 sqlite3GlobalConfigsqlite3Config.iOnceResetThreshold = va_arg(ap, int)__builtin_va_arg(ap, int);
174864 break;
174865 }
174866
174867 /* sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, xCallback, ptr);
174868 **
174869 ** Set the VDBE coverage callback function to xCallback with context
174870 ** pointer ptr.
174871 */
174872 case SQLITE_TESTCTRL_VDBE_COVERAGE21: {
174873#ifdef SQLITE_VDBE_COVERAGE
174874 typedef void (*branch_callback)(void*,unsigned int,
174875 unsigned char,unsigned char);
174876 sqlite3GlobalConfigsqlite3Config.xVdbeBranch = va_arg(ap,branch_callback)__builtin_va_arg(ap, branch_callback);
174877 sqlite3GlobalConfigsqlite3Config.pVdbeBranchArg = va_arg(ap,void*)__builtin_va_arg(ap, void*);
174878#endif
174879 break;
174880 }
174881
174882 /* sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, nMax); */
174883 case SQLITE_TESTCTRL_SORTER_MMAP24: {
174884 sqlite3 *db = va_arg(ap, sqlite3*)__builtin_va_arg(ap, sqlite3*);
174885 db->nMaxSorterMmap = va_arg(ap, int)__builtin_va_arg(ap, int);
174886 break;
174887 }
174888
174889 /* sqlite3_test_control(SQLITE_TESTCTRL_ISINIT);
174890 **
174891 ** Return SQLITE_OK if SQLite has been initialized and SQLITE_ERROR if
174892 ** not.
174893 */
174894 case SQLITE_TESTCTRL_ISINIT23: {
174895 if( sqlite3GlobalConfigsqlite3Config.isInit==0 ) rc = SQLITE_ERROR1;
174896 break;
174897 }
174898
174899 /* sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, dbName, onOff, tnum);
174900 **
174901 ** This test control is used to create imposter tables. "db" is a pointer
174902 ** to the database connection. dbName is the database name (ex: "main" or
174903 ** "temp") which will receive the imposter. "onOff" turns imposter mode on
174904 ** or off. "tnum" is the root page of the b-tree to which the imposter
174905 ** table should connect.
174906 **
174907 ** Enable imposter mode only when the schema has already been parsed. Then
174908 ** run a single CREATE TABLE statement to construct the imposter table in
174909 ** the parsed schema. Then turn imposter mode back off again.
174910 **
174911 ** If onOff==0 and tnum>0 then reset the schema for all databases, causing
174912 ** the schema to be reparsed the next time it is needed. This has the
174913 ** effect of erasing all imposter tables.
174914 */
174915 case SQLITE_TESTCTRL_IMPOSTER25: {
174916 sqlite3 *db = va_arg(ap, sqlite3*)__builtin_va_arg(ap, sqlite3*);
174917 int iDb;
174918 sqlite3_mutex_enter(db->mutex);
174919 iDb = sqlite3FindDbName(db, va_arg(ap,const char*)__builtin_va_arg(ap, const char*));
174920 if( iDb>=0 ){
174921 db->init.iDb = iDb;
174922 db->init.busy = db->init.imposterTable = va_arg(ap,int)__builtin_va_arg(ap, int);
174923 db->init.newTnum = va_arg(ap,int)__builtin_va_arg(ap, int);
174924 if( db->init.busy==0 && db->init.newTnum>0 ){
174925 sqlite3ResetAllSchemasOfConnection(db);
174926 }
174927 }
174928 sqlite3_mutex_leave(db->mutex);
174929 break;
174930 }
174931
174932#if defined(YYCOVERAGE)
174933 /* sqlite3_test_control(SQLITE_TESTCTRL_PARSER_COVERAGE, FILE *out)
174934 **
174935 ** This test control (only available when SQLite is compiled with
174936 ** -DYYCOVERAGE) writes a report onto "out" that shows all
174937 ** state/lookahead combinations in the parser state machine
174938 ** which are never exercised. If any state is missed, make the
174939 ** return code SQLITE_ERROR.
174940 */
174941 case SQLITE_TESTCTRL_PARSER_COVERAGE26: {
174942 FILE *out = va_arg(ap, FILE*)__builtin_va_arg(ap, FILE*);
174943 if( sqlite3ParserCoverage(out) ) rc = SQLITE_ERROR1;
174944 break;
174945 }
174946#endif /* defined(YYCOVERAGE) */
174947
174948 /* sqlite3_test_control(SQLITE_TESTCTRL_RESULT_INTREAL, sqlite3_context*);
174949 **
174950 ** This test-control causes the most recent sqlite3_result_int64() value
174951 ** to be interpreted as a MEM_IntReal instead of as an MEM_Int. Normally,
174952 ** MEM_IntReal values only arise during an INSERT operation of integer
174953 ** values into a REAL column, so they can be challenging to test. This
174954 ** test-control enables us to write an intreal() SQL function that can
174955 ** inject an intreal() value at arbitrary places in an SQL statement,
174956 ** for testing purposes.
174957 */
174958 case SQLITE_TESTCTRL_RESULT_INTREAL27: {
174959 sqlite3_context *pCtx = va_arg(ap, sqlite3_context*)__builtin_va_arg(ap, sqlite3_context*);
174960 sqlite3ResultIntReal(pCtx);
174961 break;
174962 }
174963
174964 /* sqlite3_test_control(SQLITE_TESTCTRL_SEEK_COUNT,
174965 ** sqlite3 *db, // Database connection
174966 ** u64 *pnSeek // Write seek count here
174967 ** );
174968 **
174969 ** This test-control queries the seek-counter on the "main" database
174970 ** file. The seek-counter is written into *pnSeek and is then reset.
174971 ** The seek-count is only available if compiled with SQLITE_DEBUG.
174972 */
174973 case SQLITE_TESTCTRL_SEEK_COUNT30: {
174974 sqlite3 *db = va_arg(ap, sqlite3*)__builtin_va_arg(ap, sqlite3*);
174975 u64 *pn = va_arg(ap, sqlite3_uint64*)__builtin_va_arg(ap, sqlite3_uint64*);
174976 *pn = sqlite3BtreeSeekCount(db->aDb->pBt)0;
174977 (void)db; /* Silence harmless unused variable warning */
174978 break;
174979 }
174980
174981 /* sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, op, ptr)
174982 **
174983 ** "ptr" is a pointer to a u32.
174984 **
174985 ** op==0 Store the current sqlite3TreeTrace in *ptr
174986 ** op==1 Set sqlite3TreeTrace to the value *ptr
174987 ** op==3 Store the current sqlite3WhereTrace in *ptr
174988 ** op==3 Set sqlite3WhereTrace to the value *ptr
174989 */
174990 case SQLITE_TESTCTRL_TRACEFLAGS31: {
174991 int opTrace = va_arg(ap, int)__builtin_va_arg(ap, int);
174992 u32 *ptr = va_arg(ap, u32*)__builtin_va_arg(ap, u32*);
174993 switch( opTrace ){
174994 case 0: *ptr = sqlite3TreeTrace; break;
174995 case 1: sqlite3TreeTrace = *ptr; break;
174996 case 2: *ptr = sqlite3WhereTrace; break;
174997 case 3: sqlite3WhereTrace = *ptr; break;
174998 }
174999 break;
175000 }
175001
175002 /* sqlite3_test_control(SQLITE_TESTCTRL_LOGEST,
175003 ** double fIn, // Input value
175004 ** int *pLogEst, // sqlite3LogEstFromDouble(fIn)
175005 ** u64 *pInt, // sqlite3LogEstToInt(*pLogEst)
175006 ** int *pLogEst2 // sqlite3LogEst(*pInt)
175007 ** );
175008 **
175009 ** Test access for the LogEst conversion routines.
175010 */
175011 case SQLITE_TESTCTRL_LOGEST33: {
175012 double rIn = va_arg(ap, double)__builtin_va_arg(ap, double);
175013 LogEst rLogEst = sqlite3LogEstFromDouble(rIn);
175014 int *pI1 = va_arg(ap,int*)__builtin_va_arg(ap, int*);
175015 u64 *pU64 = va_arg(ap,u64*)__builtin_va_arg(ap, u64*);
175016 int *pI2 = va_arg(ap,int*)__builtin_va_arg(ap, int*);
175017 *pI1 = rLogEst;
175018 *pU64 = sqlite3LogEstToInt(rLogEst);
175019 *pI2 = sqlite3LogEst(*pU64);
175020 break;
175021 }
175022
175023
175024#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_WSD)
175025 /* sqlite3_test_control(SQLITE_TESTCTRL_TUNE, id, *piValue)
175026 **
175027 ** If "id" is an integer between 1 and SQLITE_NTUNE then set the value
175028 ** of the id-th tuning parameter to *piValue. If "id" is between -1
175029 ** and -SQLITE_NTUNE, then write the current value of the (-id)-th
175030 ** tuning parameter into *piValue.
175031 **
175032 ** Tuning parameters are for use during transient development builds,
175033 ** to help find the best values for constants in the query planner.
175034 ** Access tuning parameters using the Tuning(ID) macro. Set the
175035 ** parameters in the CLI using ".testctrl tune ID VALUE".
175036 **
175037 ** Transient use only. Tuning parameters should not be used in
175038 ** checked-in code.
175039 */
175040 case SQLITE_TESTCTRL_TUNE32: {
175041 int id = va_arg(ap, int)__builtin_va_arg(ap, int);
175042 int *piValue = va_arg(ap, int*)__builtin_va_arg(ap, int*);
175043 if( id>0 && id<=SQLITE_NTUNE6 ){
175044 Tuning(id)0 = *piValue;
175045 }else if( id<0 && id>=-SQLITE_NTUNE6 ){
175046 *piValue = Tuning(-id)0;
175047 }else{
175048 rc = SQLITE_NOTFOUND12;
175049 }
175050 break;
175051 }
175052#endif
175053 }
175054 va_end(ap)__builtin_va_end(ap);
175055#endif /* SQLITE_UNTESTABLE */
175056 return rc;
175057}
175058
175059/*
175060** The Pager stores the Database filename, Journal filename, and WAL filename
175061** consecutively in memory, in that order. The database filename is prefixed
175062** by four zero bytes. Locate the start of the database filename by searching
175063** backwards for the first byte following four consecutive zero bytes.
175064**
175065** This only works if the filename passed in was obtained from the Pager.
175066*/
175067static const char *databaseName(const char *zName){
175068 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
175069 zName--;
175070 }
175071 return zName;
175072}
175073
175074/*
175075** Append text z[] to the end of p[]. Return a pointer to the first
175076** character after then zero terminator on the new text in p[].
175077*/
175078static char *appendText(char *p, const char *z){
175079 size_t n = strlen(z);
175080 memcpy(p, z, n+1);
175081 return p+n+1;
175082}
175083
175084/*
175085** Allocate memory to hold names for a database, journal file, WAL file,
175086** and query parameters. The pointer returned is valid for use by
175087** sqlite3_filename_database() and sqlite3_uri_parameter() and related
175088** functions.
175089**
175090** Memory layout must be compatible with that generated by the pager
175091** and expected by sqlite3_uri_parameter() and databaseName().
175092*/
175093SQLITE_API char *sqlite3_create_filename(
175094 const char *zDatabase,
175095 const char *zJournal,
175096 const char *zWal,
175097 int nParam,
175098 const char **azParam
175099){
175100 sqlite3_int64 nByte;
175101 int i;
175102 char *pResult, *p;
175103 nByte = strlen(zDatabase) + strlen(zJournal) + strlen(zWal) + 10;
175104 for(i=0; i<nParam*2; i++){
175105 nByte += strlen(azParam[i])+1;
175106 }
175107 pResult = p = sqlite3_malloc64( nByte );
175108 if( p==0 ) return 0;
175109 memset(p, 0, 4);
175110 p += 4;
175111 p = appendText(p, zDatabase);
175112 for(i=0; i<nParam*2; i++){
175113 p = appendText(p, azParam[i]);
175114 }
175115 *(p++) = 0;
175116 p = appendText(p, zJournal);
175117 p = appendText(p, zWal);
175118 *(p++) = 0;
175119 *(p++) = 0;
175120 assert( (sqlite3_int64)(p - pResult)==nByte )((void) (0));
175121 return pResult + 4;
175122}
175123
175124/*
175125** Free memory obtained from sqlite3_create_filename(). It is a severe
175126** error to call this routine with any parameter other than a pointer
175127** previously obtained from sqlite3_create_filename() or a NULL pointer.
175128*/
175129SQLITE_API void sqlite3_free_filename(char *p){
175130 if( p==0 ) return;
175131 p = (char*)databaseName(p);
175132 sqlite3_free(p - 4);
175133}
175134
175135
175136/*
175137** This is a utility routine, useful to VFS implementations, that checks
175138** to see if a database file was a URI that contained a specific query
175139** parameter, and if so obtains the value of the query parameter.
175140**
175141** The zFilename argument is the filename pointer passed into the xOpen()
175142** method of a VFS implementation. The zParam argument is the name of the
175143** query parameter we seek. This routine returns the value of the zParam
175144** parameter if it exists. If the parameter does not exist, this routine
175145** returns a NULL pointer.
175146*/
175147SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
175148 if( zFilename==0 || zParam==0 ) return 0;
175149 zFilename = databaseName(zFilename);
175150 return uriParameter(zFilename, zParam);
175151}
175152
175153/*
175154** Return a pointer to the name of Nth query parameter of the filename.
175155*/
175156SQLITE_API const char *sqlite3_uri_key(const char *zFilename, int N){
175157 if( zFilename==0 || N<0 ) return 0;
175158 zFilename = databaseName(zFilename);
175159 zFilename += sqlite3Strlen30(zFilename) + 1;
175160 while( ALWAYS(zFilename)(zFilename) && zFilename[0] && (N--)>0 ){
175161 zFilename += sqlite3Strlen30(zFilename) + 1;
175162 zFilename += sqlite3Strlen30(zFilename) + 1;
175163 }
175164 return zFilename[0] ? zFilename : 0;
175165}
175166
175167/*
175168** Return a boolean value for a query parameter.
175169*/
175170SQLITE_API int sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){
175171 const char *z = sqlite3_uri_parameter(zFilename, zParam);
175172 bDflt = bDflt!=0;
175173 return z ? sqlite3GetBoolean(z, bDflt) : bDflt;
175174}
175175
175176/*
175177** Return a 64-bit integer value for a query parameter.
175178*/
175179SQLITE_API sqlite3_int64 sqlite3_uri_int64(
175180 const char *zFilename, /* Filename as passed to xOpen */
175181 const char *zParam, /* URI parameter sought */
175182 sqlite3_int64 bDflt /* return if parameter is missing */
175183){
175184 const char *z = sqlite3_uri_parameter(zFilename, zParam);
175185 sqlite3_int64 v;
175186 if( z && sqlite3DecOrHexToI64(z, &v)==0 ){
175187 bDflt = v;
175188 }
175189 return bDflt;
175190}
175191
175192/*
175193** Translate a filename that was handed to a VFS routine into the corresponding
175194** database, journal, or WAL file.
175195**
175196** It is an error to pass this routine a filename string that was not
175197** passed into the VFS from the SQLite core. Doing so is similar to
175198** passing free() a pointer that was not obtained from malloc() - it is
175199** an error that we cannot easily detect but that will likely cause memory
175200** corruption.
175201*/
175202SQLITE_API const char *sqlite3_filename_database(const char *zFilename){
175203 if( zFilename==0 ) return 0;
175204 return databaseName(zFilename);
175205}
175206SQLITE_API const char *sqlite3_filename_journal(const char *zFilename){
175207 if( zFilename==0 ) return 0;
175208 zFilename = databaseName(zFilename);
175209 zFilename += sqlite3Strlen30(zFilename) + 1;
175210 while( ALWAYS(zFilename)(zFilename) && zFilename[0] ){
175211 zFilename += sqlite3Strlen30(zFilename) + 1;
175212 zFilename += sqlite3Strlen30(zFilename) + 1;
175213 }
175214 return zFilename + 1;
175215}
175216SQLITE_API const char *sqlite3_filename_wal(const char *zFilename){
175217#ifdef SQLITE_OMIT_WAL
175218 return 0;
175219#else
175220 zFilename = sqlite3_filename_journal(zFilename);
175221 if( zFilename ) zFilename += sqlite3Strlen30(zFilename) + 1;
175222 return zFilename;
175223#endif
175224}
175225
175226/*
175227** Return the Btree pointer identified by zDbName. Return NULL if not found.
175228*/
175229SQLITE_PRIVATEstatic Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){
175230 int iDb = zDbName ? sqlite3FindDbName(db, zDbName) : 0;
175231 return iDb<0 ? 0 : db->aDb[iDb].pBt;
175232}
175233
175234/*
175235** Return the name of the N-th database schema. Return NULL if N is out
175236** of range.
175237*/
175238SQLITE_API const char *sqlite3_db_name(sqlite3 *db, int N){
175239#ifdef SQLITE_ENABLE_API_ARMOR1
175240 if( !sqlite3SafetyCheckOk(db) ){
175241 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(175241);
175242 return 0;
175243 }
175244#endif
175245 if( N<0 || N>=db->nDb ){
175246 return 0;
175247 }else{
175248 return db->aDb[N].zDbSName;
175249 }
175250}
175251
175252/*
175253** Return the filename of the database associated with a database
175254** connection.
175255*/
175256SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
175257 Btree *pBt;
175258#ifdef SQLITE_ENABLE_API_ARMOR1
175259 if( !sqlite3SafetyCheckOk(db) ){
175260 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(175260);
175261 return 0;
175262 }
175263#endif
175264 pBt = sqlite3DbNameToBtree(db, zDbName);
175265 return pBt ? sqlite3BtreeGetFilename(pBt) : 0;
175266}
175267
175268/*
175269** Return 1 if database is read-only or 0 if read/write. Return -1 if
175270** no such database exists.
175271*/
175272SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
175273 Btree *pBt;
175274#ifdef SQLITE_ENABLE_API_ARMOR1
175275 if( !sqlite3SafetyCheckOk(db) ){
175276 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(175276);
175277 return -1;
175278 }
175279#endif
175280 pBt = sqlite3DbNameToBtree(db, zDbName);
175281 return pBt ? sqlite3BtreeIsReadonly(pBt) : -1;
175282}
175283
175284#ifdef SQLITE_ENABLE_SNAPSHOT1
175285/*
175286** Obtain a snapshot handle for the snapshot of database zDb currently
175287** being read by handle db.
175288*/
175289SQLITE_API int sqlite3_snapshot_get(
175290 sqlite3 *db,
175291 const char *zDb,
175292 sqlite3_snapshot **ppSnapshot
175293){
175294 int rc = SQLITE_ERROR1;
175295#ifndef SQLITE_OMIT_WAL
175296
175297#ifdef SQLITE_ENABLE_API_ARMOR1
175298 if( !sqlite3SafetyCheckOk(db) ){
175299 return SQLITE_MISUSE_BKPTsqlite3MisuseError(175299);
175300 }
175301#endif
175302 sqlite3_mutex_enter(db->mutex);
175303
175304 if( db->autoCommit==0 ){
175305 int iDb = sqlite3FindDbName(db, zDb);
175306 if( iDb==0 || iDb>1 ){
175307 Btree *pBt = db->aDb[iDb].pBt;
175308 if( SQLITE_TXN_WRITE2!=sqlite3BtreeTxnState(pBt) ){
175309 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
175310 if( rc==SQLITE_OK0 ){
175311 rc = sqlite3PagerSnapshotGet(sqlite3BtreePager(pBt), ppSnapshot);
175312 }
175313 }
175314 }
175315 }
175316
175317 sqlite3_mutex_leave(db->mutex);
175318#endif /* SQLITE_OMIT_WAL */
175319 return rc;
175320}
175321
175322/*
175323** Open a read-transaction on the snapshot idendified by pSnapshot.
175324*/
175325SQLITE_API int sqlite3_snapshot_open(
175326 sqlite3 *db,
175327 const char *zDb,
175328 sqlite3_snapshot *pSnapshot
175329){
175330 int rc = SQLITE_ERROR1;
175331#ifndef SQLITE_OMIT_WAL
175332
175333#ifdef SQLITE_ENABLE_API_ARMOR1
175334 if( !sqlite3SafetyCheckOk(db) ){
175335 return SQLITE_MISUSE_BKPTsqlite3MisuseError(175335);
175336 }
175337#endif
175338 sqlite3_mutex_enter(db->mutex);
175339 if( db->autoCommit==0 ){
175340 int iDb;
175341 iDb = sqlite3FindDbName(db, zDb);
175342 if( iDb==0 || iDb>1 ){
175343 Btree *pBt = db->aDb[iDb].pBt;
175344 if( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_WRITE2 ){
175345 Pager *pPager = sqlite3BtreePager(pBt);
175346 int bUnlock = 0;
175347 if( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_NONE0 ){
175348 if( db->nVdbeActive==0 ){
175349 rc = sqlite3PagerSnapshotCheck(pPager, pSnapshot);
175350 if( rc==SQLITE_OK0 ){
175351 bUnlock = 1;
175352 rc = sqlite3BtreeCommit(pBt);
175353 }
175354 }
175355 }else{
175356 rc = SQLITE_OK0;
175357 }
175358 if( rc==SQLITE_OK0 ){
175359 rc = sqlite3PagerSnapshotOpen(pPager, pSnapshot);
175360 }
175361 if( rc==SQLITE_OK0 ){
175362 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
175363 sqlite3PagerSnapshotOpen(pPager, 0);
175364 }
175365 if( bUnlock ){
175366 sqlite3PagerSnapshotUnlock(pPager);
175367 }
175368 }
175369 }
175370 }
175371
175372 sqlite3_mutex_leave(db->mutex);
175373#endif /* SQLITE_OMIT_WAL */
175374 return rc;
175375}
175376
175377/*
175378** Recover as many snapshots as possible from the wal file associated with
175379** schema zDb of database db.
175380*/
175381SQLITE_API int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb){
175382 int rc = SQLITE_ERROR1;
175383 int iDb;
175384#ifndef SQLITE_OMIT_WAL
175385
175386#ifdef SQLITE_ENABLE_API_ARMOR1
175387 if( !sqlite3SafetyCheckOk(db) ){
175388 return SQLITE_MISUSE_BKPTsqlite3MisuseError(175388);
175389 }
175390#endif
175391
175392 sqlite3_mutex_enter(db->mutex);
175393 iDb = sqlite3FindDbName(db, zDb);
175394 if( iDb==0 || iDb>1 ){
175395 Btree *pBt = db->aDb[iDb].pBt;
175396 if( SQLITE_TXN_NONE0==sqlite3BtreeTxnState(pBt) ){
175397 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
175398 if( rc==SQLITE_OK0 ){
175399 rc = sqlite3PagerSnapshotRecover(sqlite3BtreePager(pBt));
175400 sqlite3BtreeCommit(pBt);
175401 }
175402 }
175403 }
175404 sqlite3_mutex_leave(db->mutex);
175405#endif /* SQLITE_OMIT_WAL */
175406 return rc;
175407}
175408
175409/*
175410** Free a snapshot handle obtained from sqlite3_snapshot_get().
175411*/
175412SQLITE_API void sqlite3_snapshot_free(sqlite3_snapshot *pSnapshot){
175413 sqlite3_free(pSnapshot);
175414}
175415#endif /* SQLITE_ENABLE_SNAPSHOT */
175416
175417#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
175418/*
175419** Given the name of a compile-time option, return true if that option
175420** was used and false if not.
175421**
175422** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
175423** is not required for a match.
175424*/
175425SQLITE_API int sqlite3_compileoption_used(const char *zOptName){
175426 int i, n;
175427 int nOpt;
175428 const char **azCompileOpt;
175429
175430#if SQLITE_ENABLE_API_ARMOR1
175431 if( zOptName==0 ){
175432 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(175432);
175433 return 0;
175434 }
175435#endif
175436
175437 azCompileOpt = sqlite3CompileOptions(&nOpt);
175438
175439 if( sqlite3StrNICmpsqlite3_strnicmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
175440 n = sqlite3Strlen30(zOptName);
175441
175442 /* Since nOpt is normally in single digits, a linear search is
175443 ** adequate. No need for a binary search. */
175444 for(i=0; i<nOpt; i++){
175445 if( sqlite3StrNICmpsqlite3_strnicmp(zOptName, azCompileOpt[i], n)==0
175446 && sqlite3IsIdChar((unsigned char)azCompileOpt[i][n])==0
175447 ){
175448 return 1;
175449 }
175450 }
175451 return 0;
175452}
175453
175454/*
175455** Return the N-th compile-time option string. If N is out of range,
175456** return a NULL pointer.
175457*/
175458SQLITE_API const char *sqlite3_compileoption_get(int N){
175459 int nOpt;
175460 const char **azCompileOpt;
175461 azCompileOpt = sqlite3CompileOptions(&nOpt);
175462 if( N>=0 && N<nOpt ){
175463 return azCompileOpt[N];
175464 }
175465 return 0;
175466}
175467#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
175468
175469/************** End of main.c ************************************************/
175470/************** Begin file notify.c ******************************************/
175471/*
175472** 2009 March 3
175473**
175474** The author disclaims copyright to this source code. In place of
175475** a legal notice, here is a blessing:
175476**
175477** May you do good and not evil.
175478** May you find forgiveness for yourself and forgive others.
175479** May you share freely, never taking more than you give.
175480**
175481*************************************************************************
175482**
175483** This file contains the implementation of the sqlite3_unlock_notify()
175484** API method and its associated functionality.
175485*/
175486/* #include "sqliteInt.h" */
175487/* #include "btreeInt.h" */
175488
175489/* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
175490#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
175491
175492/*
175493** Public interfaces:
175494**
175495** sqlite3ConnectionBlocked()
175496** sqlite3ConnectionUnlocked()
175497** sqlite3ConnectionClosed()
175498** sqlite3_unlock_notify()
175499*/
175500
175501#define assertMutexHeld() \
175502 assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN)) )((void) (0))
175503
175504/*
175505** Head of a linked list of all sqlite3 objects created by this process
175506** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection
175507** is not NULL. This variable may only accessed while the STATIC_MAIN
175508** mutex is held.
175509*/
175510static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0;
175511
175512#ifndef NDEBUG1
175513/*
175514** This function is a complex assert() that verifies the following
175515** properties of the blocked connections list:
175516**
175517** 1) Each entry in the list has a non-NULL value for either
175518** pUnlockConnection or pBlockingConnection, or both.
175519**
175520** 2) All entries in the list that share a common value for
175521** xUnlockNotify are grouped together.
175522**
175523** 3) If the argument db is not NULL, then none of the entries in the
175524** blocked connections list have pUnlockConnection or pBlockingConnection
175525** set to db. This is used when closing connection db.
175526*/
175527static void checkListProperties(sqlite3 *db){
175528 sqlite3 *p;
175529 for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
175530 int seen = 0;
175531 sqlite3 *p2;
175532
175533 /* Verify property (1) */
175534 assert( p->pUnlockConnection || p->pBlockingConnection )((void) (0));
175535
175536 /* Verify property (2) */
175537 for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
175538 if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
175539 assert( p2->xUnlockNotify==p->xUnlockNotify || !seen )((void) (0));
175540 assert( db==0 || p->pUnlockConnection!=db )((void) (0));
175541 assert( db==0 || p->pBlockingConnection!=db )((void) (0));
175542 }
175543 }
175544}
175545#else
175546# define checkListProperties(x)
175547#endif
175548
175549/*
175550** Remove connection db from the blocked connections list. If connection
175551** db is not currently a part of the list, this function is a no-op.
175552*/
175553static void removeFromBlockedList(sqlite3 *db){
175554 sqlite3 **pp;
175555 assertMutexHeld();
175556 for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
175557 if( *pp==db ){
175558 *pp = (*pp)->pNextBlocked;
175559 break;
175560 }
175561 }
175562}
175563
175564/*
175565** Add connection db to the blocked connections list. It is assumed
175566** that it is not already a part of the list.
175567*/
175568static void addToBlockedList(sqlite3 *db){
175569 sqlite3 **pp;
175570 assertMutexHeld();
175571 for(
175572 pp=&sqlite3BlockedList;
175573 *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify;
175574 pp=&(*pp)->pNextBlocked
175575 );
175576 db->pNextBlocked = *pp;
175577 *pp = db;
175578}
175579
175580/*
175581** Obtain the STATIC_MAIN mutex.
175582*/
175583static void enterMutex(void){
175584 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2));
175585 checkListProperties(0);
175586}
175587
175588/*
175589** Release the STATIC_MAIN mutex.
175590*/
175591static void leaveMutex(void){
175592 assertMutexHeld();
175593 checkListProperties(0);
175594 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2));
175595}
175596
175597/*
175598** Register an unlock-notify callback.
175599**
175600** This is called after connection "db" has attempted some operation
175601** but has received an SQLITE_LOCKED error because another connection
175602** (call it pOther) in the same process was busy using the same shared
175603** cache. pOther is found by looking at db->pBlockingConnection.
175604**
175605** If there is no blocking connection, the callback is invoked immediately,
175606** before this routine returns.
175607**
175608** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate
175609** a deadlock.
175610**
175611** Otherwise, make arrangements to invoke xNotify when pOther drops
175612** its locks.
175613**
175614** Each call to this routine overrides any prior callbacks registered
175615** on the same "db". If xNotify==0 then any prior callbacks are immediately
175616** cancelled.
175617*/
175618SQLITE_API int sqlite3_unlock_notify(
175619 sqlite3 *db,
175620 void (*xNotify)(void **, int),
175621 void *pArg
175622){
175623 int rc = SQLITE_OK0;
175624
175625 sqlite3_mutex_enter(db->mutex);
175626 enterMutex();
175627
175628 if( xNotify==0 ){
175629 removeFromBlockedList(db);
175630 db->pBlockingConnection = 0;
175631 db->pUnlockConnection = 0;
175632 db->xUnlockNotify = 0;
175633 db->pUnlockArg = 0;
175634 }else if( 0==db->pBlockingConnection ){
175635 /* The blocking transaction has been concluded. Or there never was a
175636 ** blocking transaction. In either case, invoke the notify callback
175637 ** immediately.
175638 */
175639 xNotify(&pArg, 1);
175640 }else{
175641 sqlite3 *p;
175642
175643 for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
175644 if( p ){
175645 rc = SQLITE_LOCKED6; /* Deadlock detected. */
175646 }else{
175647 db->pUnlockConnection = db->pBlockingConnection;
175648 db->xUnlockNotify = xNotify;
175649 db->pUnlockArg = pArg;
175650 removeFromBlockedList(db);
175651 addToBlockedList(db);
175652 }
175653 }
175654
175655 leaveMutex();
175656 assert( !db->mallocFailed )((void) (0));
175657 sqlite3ErrorWithMsg(db, rc, (rc?"database is deadlocked":0));
175658 sqlite3_mutex_leave(db->mutex);
175659 return rc;
175660}
175661
175662/*
175663** This function is called while stepping or preparing a statement
175664** associated with connection db. The operation will return SQLITE_LOCKED
175665** to the user because it requires a lock that will not be available
175666** until connection pBlocker concludes its current transaction.
175667*/
175668SQLITE_PRIVATEstatic void sqlite3ConnectionBlocked(sqlite3 *db, sqlite3 *pBlocker){
175669 enterMutex();
175670 if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
175671 addToBlockedList(db);
175672 }
175673 db->pBlockingConnection = pBlocker;
175674 leaveMutex();
175675}
175676
175677/*
175678** This function is called when
175679** the transaction opened by database db has just finished. Locks held
175680** by database connection db have been released.
175681**
175682** This function loops through each entry in the blocked connections
175683** list and does the following:
175684**
175685** 1) If the sqlite3.pBlockingConnection member of a list entry is
175686** set to db, then set pBlockingConnection=0.
175687**
175688** 2) If the sqlite3.pUnlockConnection member of a list entry is
175689** set to db, then invoke the configured unlock-notify callback and
175690** set pUnlockConnection=0.
175691**
175692** 3) If the two steps above mean that pBlockingConnection==0 and
175693** pUnlockConnection==0, remove the entry from the blocked connections
175694** list.
175695*/
175696SQLITE_PRIVATEstatic void sqlite3ConnectionUnlocked(sqlite3 *db){
175697 void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
175698 int nArg = 0; /* Number of entries in aArg[] */
175699 sqlite3 **pp; /* Iterator variable */
175700 void **aArg; /* Arguments to the unlock callback */
175701 void **aDyn = 0; /* Dynamically allocated space for aArg[] */
175702 void *aStatic[16]; /* Starter space for aArg[]. No malloc required */
175703
175704 aArg = aStatic;
175705 enterMutex(); /* Enter STATIC_MAIN mutex */
175706
175707 /* This loop runs once for each entry in the blocked-connections list. */
175708 for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
175709 sqlite3 *p = *pp;
175710
175711 /* Step 1. */
175712 if( p->pBlockingConnection==db ){
175713 p->pBlockingConnection = 0;
175714 }
175715
175716 /* Step 2. */
175717 if( p->pUnlockConnection==db ){
175718 assert( p->xUnlockNotify )((void) (0));
175719 if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
175720 xUnlockNotify(aArg, nArg);
175721 nArg = 0;
175722 }
175723
175724 sqlite3BeginBenignMalloc();
175725 assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) )((void) (0));
175726 assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn )((void) (0));
175727 if( (!aDyn && nArg==(int)ArraySize(aStatic)((int)(sizeof(aStatic)/sizeof(aStatic[0]))))
175728 || (aDyn && nArg==(int)(sqlite3MallocSize(aDyn)/sizeof(void*)))
175729 ){
175730 /* The aArg[] array needs to grow. */
175731 void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2);
175732 if( pNew ){
175733 memcpy(pNew, aArg, nArg*sizeof(void *));
175734 sqlite3_free(aDyn);
175735 aDyn = aArg = pNew;
175736 }else{
175737 /* This occurs when the array of context pointers that need to
175738 ** be passed to the unlock-notify callback is larger than the
175739 ** aStatic[] array allocated on the stack and the attempt to
175740 ** allocate a larger array from the heap has failed.
175741 **
175742 ** This is a difficult situation to handle. Returning an error
175743 ** code to the caller is insufficient, as even if an error code
175744 ** is returned the transaction on connection db will still be
175745 ** closed and the unlock-notify callbacks on blocked connections
175746 ** will go unissued. This might cause the application to wait
175747 ** indefinitely for an unlock-notify callback that will never
175748 ** arrive.
175749 **
175750 ** Instead, invoke the unlock-notify callback with the context
175751 ** array already accumulated. We can then clear the array and
175752 ** begin accumulating any further context pointers without
175753 ** requiring any dynamic allocation. This is sub-optimal because
175754 ** it means that instead of one callback with a large array of
175755 ** context pointers the application will receive two or more
175756 ** callbacks with smaller arrays of context pointers, which will
175757 ** reduce the applications ability to prioritize multiple
175758 ** connections. But it is the best that can be done under the
175759 ** circumstances.
175760 */
175761 xUnlockNotify(aArg, nArg);
175762 nArg = 0;
175763 }
175764 }
175765 sqlite3EndBenignMalloc();
175766
175767 aArg[nArg++] = p->pUnlockArg;
175768 xUnlockNotify = p->xUnlockNotify;
175769 p->pUnlockConnection = 0;
175770 p->xUnlockNotify = 0;
175771 p->pUnlockArg = 0;
175772 }
175773
175774 /* Step 3. */
175775 if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
175776 /* Remove connection p from the blocked connections list. */
175777 *pp = p->pNextBlocked;
175778 p->pNextBlocked = 0;
175779 }else{
175780 pp = &p->pNextBlocked;
175781 }
175782 }
175783
175784 if( nArg!=0 ){
175785 xUnlockNotify(aArg, nArg);
175786 }
175787 sqlite3_free(aDyn);
175788 leaveMutex(); /* Leave STATIC_MAIN mutex */
175789}
175790
175791/*
175792** This is called when the database connection passed as an argument is
175793** being closed. The connection is removed from the blocked list.
175794*/
175795SQLITE_PRIVATEstatic void sqlite3ConnectionClosed(sqlite3 *db){
175796 sqlite3ConnectionUnlocked(db);
175797 enterMutex();
175798 removeFromBlockedList(db);
175799 checkListProperties(db);
175800 leaveMutex();
175801}
175802#endif
175803
175804/************** End of notify.c **********************************************/
175805/************** Begin file fts3.c ********************************************/
175806/*
175807** 2006 Oct 10
175808**
175809** The author disclaims copyright to this source code. In place of
175810** a legal notice, here is a blessing:
175811**
175812** May you do good and not evil.
175813** May you find forgiveness for yourself and forgive others.
175814** May you share freely, never taking more than you give.
175815**
175816******************************************************************************
175817**
175818** This is an SQLite module implementing full-text search.
175819*/
175820
175821/*
175822** The code in this file is only compiled if:
175823**
175824** * The FTS3 module is being built as an extension
175825** (in which case SQLITE_CORE is not defined), or
175826**
175827** * The FTS3 module is being built into the core of
175828** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
175829*/
175830
175831/* The full-text index is stored in a series of b+tree (-like)
175832** structures called segments which map terms to doclists. The
175833** structures are like b+trees in layout, but are constructed from the
175834** bottom up in optimal fashion and are not updatable. Since trees
175835** are built from the bottom up, things will be described from the
175836** bottom up.
175837**
175838**
175839**** Varints ****
175840** The basic unit of encoding is a variable-length integer called a
175841** varint. We encode variable-length integers in little-endian order
175842** using seven bits * per byte as follows:
175843**
175844** KEY:
175845** A = 0xxxxxxx 7 bits of data and one flag bit
175846** B = 1xxxxxxx 7 bits of data and one flag bit
175847**
175848** 7 bits - A
175849** 14 bits - BA
175850** 21 bits - BBA
175851** and so on.
175852**
175853** This is similar in concept to how sqlite encodes "varints" but
175854** the encoding is not the same. SQLite varints are big-endian
175855** are are limited to 9 bytes in length whereas FTS3 varints are
175856** little-endian and can be up to 10 bytes in length (in theory).
175857**
175858** Example encodings:
175859**
175860** 1: 0x01
175861** 127: 0x7f
175862** 128: 0x81 0x00
175863**
175864**
175865**** Document lists ****
175866** A doclist (document list) holds a docid-sorted list of hits for a
175867** given term. Doclists hold docids and associated token positions.
175868** A docid is the unique integer identifier for a single document.
175869** A position is the index of a word within the document. The first
175870** word of the document has a position of 0.
175871**
175872** FTS3 used to optionally store character offsets using a compile-time
175873** option. But that functionality is no longer supported.
175874**
175875** A doclist is stored like this:
175876**
175877** array {
175878** varint docid; (delta from previous doclist)
175879** array { (position list for column 0)
175880** varint position; (2 more than the delta from previous position)
175881** }
175882** array {
175883** varint POS_COLUMN; (marks start of position list for new column)
175884** varint column; (index of new column)
175885** array {
175886** varint position; (2 more than the delta from previous position)
175887** }
175888** }
175889** varint POS_END; (marks end of positions for this document.
175890** }
175891**
175892** Here, array { X } means zero or more occurrences of X, adjacent in
175893** memory. A "position" is an index of a token in the token stream
175894** generated by the tokenizer. Note that POS_END and POS_COLUMN occur
175895** in the same logical place as the position element, and act as sentinals
175896** ending a position list array. POS_END is 0. POS_COLUMN is 1.
175897** The positions numbers are not stored literally but rather as two more
175898** than the difference from the prior position, or the just the position plus
175899** 2 for the first position. Example:
175900**
175901** label: A B C D E F G H I J K
175902** value: 123 5 9 1 1 14 35 0 234 72 0
175903**
175904** The 123 value is the first docid. For column zero in this document
175905** there are two matches at positions 3 and 10 (5-2 and 9-2+3). The 1
175906** at D signals the start of a new column; the 1 at E indicates that the
175907** new column is column number 1. There are two positions at 12 and 45
175908** (14-2 and 35-2+12). The 0 at H indicate the end-of-document. The
175909** 234 at I is the delta to next docid (357). It has one position 70
175910** (72-2) and then terminates with the 0 at K.
175911**
175912** A "position-list" is the list of positions for multiple columns for
175913** a single docid. A "column-list" is the set of positions for a single
175914** column. Hence, a position-list consists of one or more column-lists,
175915** a document record consists of a docid followed by a position-list and
175916** a doclist consists of one or more document records.
175917**
175918** A bare doclist omits the position information, becoming an
175919** array of varint-encoded docids.
175920**
175921**** Segment leaf nodes ****
175922** Segment leaf nodes store terms and doclists, ordered by term. Leaf
175923** nodes are written using LeafWriter, and read using LeafReader (to
175924** iterate through a single leaf node's data) and LeavesReader (to
175925** iterate through a segment's entire leaf layer). Leaf nodes have
175926** the format:
175927**
175928** varint iHeight; (height from leaf level, always 0)
175929** varint nTerm; (length of first term)
175930** char pTerm[nTerm]; (content of first term)
175931** varint nDoclist; (length of term's associated doclist)
175932** char pDoclist[nDoclist]; (content of doclist)
175933** array {
175934** (further terms are delta-encoded)
175935** varint nPrefix; (length of prefix shared with previous term)
175936** varint nSuffix; (length of unshared suffix)
175937** char pTermSuffix[nSuffix];(unshared suffix of next term)
175938** varint nDoclist; (length of term's associated doclist)
175939** char pDoclist[nDoclist]; (content of doclist)
175940** }
175941**
175942** Here, array { X } means zero or more occurrences of X, adjacent in
175943** memory.
175944**
175945** Leaf nodes are broken into blocks which are stored contiguously in
175946** the %_segments table in sorted order. This means that when the end
175947** of a node is reached, the next term is in the node with the next
175948** greater node id.
175949**
175950** New data is spilled to a new leaf node when the current node
175951** exceeds LEAF_MAX bytes (default 2048). New data which itself is
175952** larger than STANDALONE_MIN (default 1024) is placed in a standalone
175953** node (a leaf node with a single term and doclist). The goal of
175954** these settings is to pack together groups of small doclists while
175955** making it efficient to directly access large doclists. The
175956** assumption is that large doclists represent terms which are more
175957** likely to be query targets.
175958**
175959** TODO(shess) It may be useful for blocking decisions to be more
175960** dynamic. For instance, it may make more sense to have a 2.5k leaf
175961** node rather than splitting into 2k and .5k nodes. My intuition is
175962** that this might extend through 2x or 4x the pagesize.
175963**
175964**
175965**** Segment interior nodes ****
175966** Segment interior nodes store blockids for subtree nodes and terms
175967** to describe what data is stored by the each subtree. Interior
175968** nodes are written using InteriorWriter, and read using
175969** InteriorReader. InteriorWriters are created as needed when
175970** SegmentWriter creates new leaf nodes, or when an interior node
175971** itself grows too big and must be split. The format of interior
175972** nodes:
175973**
175974** varint iHeight; (height from leaf level, always >0)
175975** varint iBlockid; (block id of node's leftmost subtree)
175976** optional {
175977** varint nTerm; (length of first term)
175978** char pTerm[nTerm]; (content of first term)
175979** array {
175980** (further terms are delta-encoded)
175981** varint nPrefix; (length of shared prefix with previous term)
175982** varint nSuffix; (length of unshared suffix)
175983** char pTermSuffix[nSuffix]; (unshared suffix of next term)
175984** }
175985** }
175986**
175987** Here, optional { X } means an optional element, while array { X }
175988** means zero or more occurrences of X, adjacent in memory.
175989**
175990** An interior node encodes n terms separating n+1 subtrees. The
175991** subtree blocks are contiguous, so only the first subtree's blockid
175992** is encoded. The subtree at iBlockid will contain all terms less
175993** than the first term encoded (or all terms if no term is encoded).
175994** Otherwise, for terms greater than or equal to pTerm[i] but less
175995** than pTerm[i+1], the subtree for that term will be rooted at
175996** iBlockid+i. Interior nodes only store enough term data to
175997** distinguish adjacent children (if the rightmost term of the left
175998** child is "something", and the leftmost term of the right child is
175999** "wicked", only "w" is stored).
176000**
176001** New data is spilled to a new interior node at the same height when
176002** the current node exceeds INTERIOR_MAX bytes (default 2048).
176003** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing
176004** interior nodes and making the tree too skinny. The interior nodes
176005** at a given height are naturally tracked by interior nodes at
176006** height+1, and so on.
176007**
176008**
176009**** Segment directory ****
176010** The segment directory in table %_segdir stores meta-information for
176011** merging and deleting segments, and also the root node of the
176012** segment's tree.
176013**
176014** The root node is the top node of the segment's tree after encoding
176015** the entire segment, restricted to ROOT_MAX bytes (default 1024).
176016** This could be either a leaf node or an interior node. If the top
176017** node requires more than ROOT_MAX bytes, it is flushed to %_segments
176018** and a new root interior node is generated (which should always fit
176019** within ROOT_MAX because it only needs space for 2 varints, the
176020** height and the blockid of the previous root).
176021**
176022** The meta-information in the segment directory is:
176023** level - segment level (see below)
176024** idx - index within level
176025** - (level,idx uniquely identify a segment)
176026** start_block - first leaf node
176027** leaves_end_block - last leaf node
176028** end_block - last block (including interior nodes)
176029** root - contents of root node
176030**
176031** If the root node is a leaf node, then start_block,
176032** leaves_end_block, and end_block are all 0.
176033**
176034**
176035**** Segment merging ****
176036** To amortize update costs, segments are grouped into levels and
176037** merged in batches. Each increase in level represents exponentially
176038** more documents.
176039**
176040** New documents (actually, document updates) are tokenized and
176041** written individually (using LeafWriter) to a level 0 segment, with
176042** incrementing idx. When idx reaches MERGE_COUNT (default 16), all
176043** level 0 segments are merged into a single level 1 segment. Level 1
176044** is populated like level 0, and eventually MERGE_COUNT level 1
176045** segments are merged to a single level 2 segment (representing
176046** MERGE_COUNT^2 updates), and so on.
176047**
176048** A segment merge traverses all segments at a given level in
176049** parallel, performing a straightforward sorted merge. Since segment
176050** leaf nodes are written in to the %_segments table in order, this
176051** merge traverses the underlying sqlite disk structures efficiently.
176052** After the merge, all segment blocks from the merged level are
176053** deleted.
176054**
176055** MERGE_COUNT controls how often we merge segments. 16 seems to be
176056** somewhat of a sweet spot for insertion performance. 32 and 64 show
176057** very similar performance numbers to 16 on insertion, though they're
176058** a tiny bit slower (perhaps due to more overhead in merge-time
176059** sorting). 8 is about 20% slower than 16, 4 about 50% slower than
176060** 16, 2 about 66% slower than 16.
176061**
176062** At query time, high MERGE_COUNT increases the number of segments
176063** which need to be scanned and merged. For instance, with 100k docs
176064** inserted:
176065**
176066** MERGE_COUNT segments
176067** 16 25
176068** 8 12
176069** 4 10
176070** 2 6
176071**
176072** This appears to have only a moderate impact on queries for very
176073** frequent terms (which are somewhat dominated by segment merge
176074** costs), and infrequent and non-existent terms still seem to be fast
176075** even with many segments.
176076**
176077** TODO(shess) That said, it would be nice to have a better query-side
176078** argument for MERGE_COUNT of 16. Also, it is possible/likely that
176079** optimizations to things like doclist merging will swing the sweet
176080** spot around.
176081**
176082**
176083**
176084**** Handling of deletions and updates ****
176085** Since we're using a segmented structure, with no docid-oriented
176086** index into the term index, we clearly cannot simply update the term
176087** index when a document is deleted or updated. For deletions, we
176088** write an empty doclist (varint(docid) varint(POS_END)), for updates
176089** we simply write the new doclist. Segment merges overwrite older
176090** data for a particular docid with newer data, so deletes or updates
176091** will eventually overtake the earlier data and knock it out. The
176092** query logic likewise merges doclists so that newer data knocks out
176093** older data.
176094*/
176095
176096/************** Include fts3Int.h in the middle of fts3.c ********************/
176097/************** Begin file fts3Int.h *****************************************/
176098/*
176099** 2009 Nov 12
176100**
176101** The author disclaims copyright to this source code. In place of
176102** a legal notice, here is a blessing:
176103**
176104** May you do good and not evil.
176105** May you find forgiveness for yourself and forgive others.
176106** May you share freely, never taking more than you give.
176107**
176108******************************************************************************
176109**
176110*/
176111#ifndef _FTSINT_H
176112#define _FTSINT_H
176113
176114#if !defined(NDEBUG1) && !defined(SQLITE_DEBUG)
176115# define NDEBUG1 1
176116#endif
176117
176118/* FTS3/FTS4 require virtual tables */
176119#ifdef SQLITE_OMIT_VIRTUALTABLE
176120# undef SQLITE_ENABLE_FTS31
176121# undef SQLITE_ENABLE_FTS41
176122#endif
176123
176124/*
176125** FTS4 is really an extension for FTS3. It is enabled using the
176126** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also all
176127** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
176128*/
176129#if defined(SQLITE_ENABLE_FTS41) && !defined(SQLITE_ENABLE_FTS31)
176130# define SQLITE_ENABLE_FTS31
176131#endif
176132
176133#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
176134
176135/* If not building as part of the core, include sqlite3ext.h. */
176136#ifndef SQLITE_CORE1
176137/* # include "sqlite3ext.h" */
176138SQLITE_EXTENSION_INIT3
176139#endif
176140
176141/* #include "sqlite3.h" */
176142/************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/
176143/************** Begin file fts3_tokenizer.h **********************************/
176144/*
176145** 2006 July 10
176146**
176147** The author disclaims copyright to this source code.
176148**
176149*************************************************************************
176150** Defines the interface to tokenizers used by fulltext-search. There
176151** are three basic components:
176152**
176153** sqlite3_tokenizer_module is a singleton defining the tokenizer
176154** interface functions. This is essentially the class structure for
176155** tokenizers.
176156**
176157** sqlite3_tokenizer is used to define a particular tokenizer, perhaps
176158** including customization information defined at creation time.
176159**
176160** sqlite3_tokenizer_cursor is generated by a tokenizer to generate
176161** tokens from a particular input.
176162*/
176163#ifndef _FTS3_TOKENIZER_H_
176164#define _FTS3_TOKENIZER_H_
176165
176166/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
176167** If tokenizers are to be allowed to call sqlite3_*() functions, then
176168** we will need a way to register the API consistently.
176169*/
176170/* #include "sqlite3.h" */
176171
176172/*
176173** Structures used by the tokenizer interface. When a new tokenizer
176174** implementation is registered, the caller provides a pointer to
176175** an sqlite3_tokenizer_module containing pointers to the callback
176176** functions that make up an implementation.
176177**
176178** When an fts3 table is created, it passes any arguments passed to
176179** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the
176180** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer
176181** implementation. The xCreate() function in turn returns an
176182** sqlite3_tokenizer structure representing the specific tokenizer to
176183** be used for the fts3 table (customized by the tokenizer clause arguments).
176184**
176185** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen()
176186** method is called. It returns an sqlite3_tokenizer_cursor object
176187** that may be used to tokenize a specific input buffer based on
176188** the tokenization rules supplied by a specific sqlite3_tokenizer
176189** object.
176190*/
176191typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module;
176192typedef struct sqlite3_tokenizer sqlite3_tokenizer;
176193typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor;
176194
176195struct sqlite3_tokenizer_module {
176196
176197 /*
176198 ** Structure version. Should always be set to 0 or 1.
176199 */
176200 int iVersion;
176201
176202 /*
176203 ** Create a new tokenizer. The values in the argv[] array are the
176204 ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL
176205 ** TABLE statement that created the fts3 table. For example, if
176206 ** the following SQL is executed:
176207 **
176208 ** CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
176209 **
176210 ** then argc is set to 2, and the argv[] array contains pointers
176211 ** to the strings "arg1" and "arg2".
176212 **
176213 ** This method should return either SQLITE_OK (0), or an SQLite error
176214 ** code. If SQLITE_OK is returned, then *ppTokenizer should be set
176215 ** to point at the newly created tokenizer structure. The generic
176216 ** sqlite3_tokenizer.pModule variable should not be initialized by
176217 ** this callback. The caller will do so.
176218 */
176219 int (*xCreate)(
176220 int argc, /* Size of argv array */
176221 const char *const*argv, /* Tokenizer argument strings */
176222 sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */
176223 );
176224
176225 /*
176226 ** Destroy an existing tokenizer. The fts3 module calls this method
176227 ** exactly once for each successful call to xCreate().
176228 */
176229 int (*xDestroy)(sqlite3_tokenizer *pTokenizer);
176230
176231 /*
176232 ** Create a tokenizer cursor to tokenize an input buffer. The caller
176233 ** is responsible for ensuring that the input buffer remains valid
176234 ** until the cursor is closed (using the xClose() method).
176235 */
176236 int (*xOpen)(
176237 sqlite3_tokenizer *pTokenizer, /* Tokenizer object */
176238 const char *pInput, int nBytes, /* Input buffer */
176239 sqlite3_tokenizer_cursor **ppCursor /* OUT: Created tokenizer cursor */
176240 );
176241
176242 /*
176243 ** Destroy an existing tokenizer cursor. The fts3 module calls this
176244 ** method exactly once for each successful call to xOpen().
176245 */
176246 int (*xClose)(sqlite3_tokenizer_cursor *pCursor);
176247
176248 /*
176249 ** Retrieve the next token from the tokenizer cursor pCursor. This
176250 ** method should either return SQLITE_OK and set the values of the
176251 ** "OUT" variables identified below, or SQLITE_DONE to indicate that
176252 ** the end of the buffer has been reached, or an SQLite error code.
176253 **
176254 ** *ppToken should be set to point at a buffer containing the
176255 ** normalized version of the token (i.e. after any case-folding and/or
176256 ** stemming has been performed). *pnBytes should be set to the length
176257 ** of this buffer in bytes. The input text that generated the token is
176258 ** identified by the byte offsets returned in *piStartOffset and
176259 ** *piEndOffset. *piStartOffset should be set to the index of the first
176260 ** byte of the token in the input buffer. *piEndOffset should be set
176261 ** to the index of the first byte just past the end of the token in
176262 ** the input buffer.
176263 **
176264 ** The buffer *ppToken is set to point at is managed by the tokenizer
176265 ** implementation. It is only required to be valid until the next call
176266 ** to xNext() or xClose().
176267 */
176268 /* TODO(shess) current implementation requires pInput to be
176269 ** nul-terminated. This should either be fixed, or pInput/nBytes
176270 ** should be converted to zInput.
176271 */
176272 int (*xNext)(
176273 sqlite3_tokenizer_cursor *pCursor, /* Tokenizer cursor */
176274 const char **ppToken, int *pnBytes, /* OUT: Normalized text for token */
176275 int *piStartOffset, /* OUT: Byte offset of token in input buffer */
176276 int *piEndOffset, /* OUT: Byte offset of end of token in input buffer */
176277 int *piPosition /* OUT: Number of tokens returned before this one */
176278 );
176279
176280 /***********************************************************************
176281 ** Methods below this point are only available if iVersion>=1.
176282 */
176283
176284 /*
176285 ** Configure the language id of a tokenizer cursor.
176286 */
176287 int (*xLanguageid)(sqlite3_tokenizer_cursor *pCsr, int iLangid);
176288};
176289
176290struct sqlite3_tokenizer {
176291 const sqlite3_tokenizer_module *pModule; /* The module for this tokenizer */
176292 /* Tokenizer implementations will typically add additional fields */
176293};
176294
176295struct sqlite3_tokenizer_cursor {
176296 sqlite3_tokenizer *pTokenizer; /* Tokenizer for this cursor. */
176297 /* Tokenizer implementations will typically add additional fields */
176298};
176299
176300int fts3_global_term_cnt(int iTerm, int iCol);
176301int fts3_term_cnt(int iTerm, int iCol);
176302
176303
176304#endif /* _FTS3_TOKENIZER_H_ */
176305
176306/************** End of fts3_tokenizer.h **************************************/
176307/************** Continuing where we left off in fts3Int.h ********************/
176308/************** Include fts3_hash.h in the middle of fts3Int.h ***************/
176309/************** Begin file fts3_hash.h ***************************************/
176310/*
176311** 2001 September 22
176312**
176313** The author disclaims copyright to this source code. In place of
176314** a legal notice, here is a blessing:
176315**
176316** May you do good and not evil.
176317** May you find forgiveness for yourself and forgive others.
176318** May you share freely, never taking more than you give.
176319**
176320*************************************************************************
176321** This is the header file for the generic hash-table implementation
176322** used in SQLite. We've modified it slightly to serve as a standalone
176323** hash table implementation for the full-text indexing module.
176324**
176325*/
176326#ifndef _FTS3_HASH_H_
176327#define _FTS3_HASH_H_
176328
176329/* Forward declarations of structures. */
176330typedef struct Fts3Hash Fts3Hash;
176331typedef struct Fts3HashElem Fts3HashElem;
176332
176333/* A complete hash table is an instance of the following structure.
176334** The internals of this structure are intended to be opaque -- client
176335** code should not attempt to access or modify the fields of this structure
176336** directly. Change this structure only by using the routines below.
176337** However, many of the "procedures" and "functions" for modifying and
176338** accessing this structure are really macros, so we can't really make
176339** this structure opaque.
176340*/
176341struct Fts3Hash {
176342 char keyClass; /* HASH_INT, _POINTER, _STRING, _BINARY */
176343 char copyKey; /* True if copy of key made on insert */
176344 int count; /* Number of entries in this table */
176345 Fts3HashElem *first; /* The first element of the array */
176346 int htsize; /* Number of buckets in the hash table */
176347 struct _fts3ht { /* the hash table */
176348 int count; /* Number of entries with this hash */
176349 Fts3HashElem *chain; /* Pointer to first entry with this hash */
176350 } *ht;
176351};
176352
176353/* Each element in the hash table is an instance of the following
176354** structure. All elements are stored on a single doubly-linked list.
176355**
176356** Again, this structure is intended to be opaque, but it can't really
176357** be opaque because it is used by macros.
176358*/
176359struct Fts3HashElem {
176360 Fts3HashElem *next, *prev; /* Next and previous elements in the table */
176361 void *data; /* Data associated with this element */
176362 void *pKey; int nKey; /* Key associated with this element */
176363};
176364
176365/*
176366** There are 2 different modes of operation for a hash table:
176367**
176368** FTS3_HASH_STRING pKey points to a string that is nKey bytes long
176369** (including the null-terminator, if any). Case
176370** is respected in comparisons.
176371**
176372** FTS3_HASH_BINARY pKey points to binary data nKey bytes long.
176373** memcmp() is used to compare keys.
176374**
176375** A copy of the key is made if the copyKey parameter to fts3HashInit is 1.
176376*/
176377#define FTS3_HASH_STRING1 1
176378#define FTS3_HASH_BINARY2 2
176379
176380/*
176381** Access routines. To delete, insert a NULL pointer.
176382*/
176383SQLITE_PRIVATEstatic void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey);
176384SQLITE_PRIVATEstatic void *sqlite3Fts3HashInsert(Fts3Hash*, const void *pKey, int nKey, void *pData);
176385SQLITE_PRIVATEstatic void *sqlite3Fts3HashFind(const Fts3Hash*, const void *pKey, int nKey);
176386SQLITE_PRIVATEstatic void sqlite3Fts3HashClear(Fts3Hash*);
176387SQLITE_PRIVATEstatic Fts3HashElem *sqlite3Fts3HashFindElem(const Fts3Hash *, const void *, int);
176388
176389/*
176390** Shorthand for the functions above
176391*/
176392#define fts3HashInitsqlite3Fts3HashInit sqlite3Fts3HashInit
176393#define fts3HashInsertsqlite3Fts3HashInsert sqlite3Fts3HashInsert
176394#define fts3HashFindsqlite3Fts3HashFind sqlite3Fts3HashFind
176395#define fts3HashClearsqlite3Fts3HashClear sqlite3Fts3HashClear
176396#define fts3HashFindElemsqlite3Fts3HashFindElem sqlite3Fts3HashFindElem
176397
176398/*
176399** Macros for looping over all elements of a hash table. The idiom is
176400** like this:
176401**
176402** Fts3Hash h;
176403** Fts3HashElem *p;
176404** ...
176405** for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){
176406** SomeStructure *pData = fts3HashData(p);
176407** // do something with pData
176408** }
176409*/
176410#define fts3HashFirst(H)((H)->first) ((H)->first)
176411#define fts3HashNext(E)((E)->next) ((E)->next)
176412#define fts3HashData(E)((E)->data) ((E)->data)
176413#define fts3HashKey(E)((E)->pKey) ((E)->pKey)
176414#define fts3HashKeysize(E)((E)->nKey) ((E)->nKey)
176415
176416/*
176417** Number of entries in a hash table
176418*/
176419#define fts3HashCount(H)((H)->count) ((H)->count)
176420
176421#endif /* _FTS3_HASH_H_ */
176422
176423/************** End of fts3_hash.h *******************************************/
176424/************** Continuing where we left off in fts3Int.h ********************/
176425
176426/*
176427** This constant determines the maximum depth of an FTS expression tree
176428** that the library will create and use. FTS uses recursion to perform
176429** various operations on the query tree, so the disadvantage of a large
176430** limit is that it may allow very large queries to use large amounts
176431** of stack space (perhaps causing a stack overflow).
176432*/
176433#ifndef SQLITE_FTS3_MAX_EXPR_DEPTH12
176434# define SQLITE_FTS3_MAX_EXPR_DEPTH12 12
176435#endif
176436
176437
176438/*
176439** This constant controls how often segments are merged. Once there are
176440** FTS3_MERGE_COUNT segments of level N, they are merged into a single
176441** segment of level N+1.
176442*/
176443#define FTS3_MERGE_COUNT16 16
176444
176445/*
176446** This is the maximum amount of data (in bytes) to store in the
176447** Fts3Table.pendingTerms hash table. Normally, the hash table is
176448** populated as documents are inserted/updated/deleted in a transaction
176449** and used to create a new segment when the transaction is committed.
176450** However if this limit is reached midway through a transaction, a new
176451** segment is created and the hash table cleared immediately.
176452*/
176453#define FTS3_MAX_PENDING_DATA(1*1024*1024) (1*1024*1024)
176454
176455/*
176456** Macro to return the number of elements in an array. SQLite has a
176457** similar macro called ArraySize(). Use a different name to avoid
176458** a collision when building an amalgamation with built-in FTS3.
176459*/
176460#define SizeofArray(X)((int)(sizeof(X)/sizeof(X[0]))) ((int)(sizeof(X)/sizeof(X[0])))
176461
176462
176463#ifndef MIN
176464# define MIN(x,y)((x)<(y)?(x):(y)) ((x)<(y)?(x):(y))
176465#endif
176466#ifndef MAX
176467# define MAX(x,y)((x)>(y)?(x):(y)) ((x)>(y)?(x):(y))
176468#endif
176469
176470/*
176471** Maximum length of a varint encoded integer. The varint format is different
176472** from that used by SQLite, so the maximum length is 10, not 9.
176473*/
176474#define FTS3_VARINT_MAX10 10
176475
176476#define FTS3_BUFFER_PADDING8 8
176477
176478/*
176479** FTS4 virtual tables may maintain multiple indexes - one index of all terms
176480** in the document set and zero or more prefix indexes. All indexes are stored
176481** as one or more b+-trees in the %_segments and %_segdir tables.
176482**
176483** It is possible to determine which index a b+-tree belongs to based on the
176484** value stored in the "%_segdir.level" column. Given this value L, the index
176485** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with
176486** level values between 0 and 1023 (inclusive) belong to index 0, all levels
176487** between 1024 and 2047 to index 1, and so on.
176488**
176489** It is considered impossible for an index to use more than 1024 levels. In
176490** theory though this may happen, but only after at least
176491** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables.
176492*/
176493#define FTS3_SEGDIR_MAXLEVEL1024 1024
176494#define FTS3_SEGDIR_MAXLEVEL_STR"1024" "1024"
176495
176496/*
176497** The testcase() macro is only used by the amalgamation. If undefined,
176498** make it a no-op.
176499*/
176500#ifndef testcase
176501# define testcase(X)
176502#endif
176503
176504/*
176505** Terminator values for position-lists and column-lists.
176506*/
176507#define POS_COLUMN(1) (1) /* Column-list terminator */
176508#define POS_END(0) (0) /* Position-list terminator */
176509
176510/*
176511** The assert_fts3_nc() macro is similar to the assert() macro, except that it
176512** is used for assert() conditions that are true only if it can be
176513** guranteed that the database is not corrupt.
176514*/
176515#ifdef SQLITE_DEBUG
176516SQLITE_API extern int sqlite3_fts3_may_be_corrupt;
176517# define assert_fts3_nc(x)((void) (0)) assert(sqlite3_fts3_may_be_corrupt || (x))((void) (0))
176518#else
176519# define assert_fts3_nc(x)((void) (0)) assert(x)((void) (0))
176520#endif
176521
176522/*
176523** This section provides definitions to allow the
176524** FTS3 extension to be compiled outside of the
176525** amalgamation.
176526*/
176527#ifndef SQLITE_AMALGAMATION1
176528/*
176529** Macros indicating that conditional expressions are always true or
176530** false.
176531*/
176532#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
176533# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
176534#endif
176535#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
176536# define ALWAYS(X)(X) (1)
176537# define NEVER(X)(X) (0)
176538#elif !defined(NDEBUG1)
176539# define ALWAYS(X)(X) ((X)?1:(assert(0)((void) (0)),0))
176540# define NEVER(X)(X) ((X)?(assert(0)((void) (0)),1):0)
176541#else
176542# define ALWAYS(X)(X) (X)
176543# define NEVER(X)(X) (X)
176544#endif
176545
176546/*
176547** Internal types used by SQLite.
176548*/
176549typedef unsigned char u8; /* 1-byte (or larger) unsigned integer */
176550typedef short int i16; /* 2-byte (or larger) signed integer */
176551typedef unsigned int u32; /* 4-byte unsigned integer */
176552typedef sqlite3_uint64 u64; /* 8-byte unsigned integer */
176553typedef sqlite3_int64 i64; /* 8-byte signed integer */
176554
176555/*
176556** Macro used to suppress compiler warnings for unused parameters.
176557*/
176558#define UNUSED_PARAMETER(x)(void)(x) (void)(x)
176559
176560/*
176561** Activate assert() only if SQLITE_TEST is enabled.
176562*/
176563#if !defined(NDEBUG1) && !defined(SQLITE_DEBUG)
176564# define NDEBUG1 1
176565#endif
176566
176567/*
176568** The TESTONLY macro is used to enclose variable declarations or
176569** other bits of code that are needed to support the arguments
176570** within testcase() and assert() macros.
176571*/
176572#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
176573# define TESTONLY(X) X
176574#else
176575# define TESTONLY(X)
176576#endif
176577
176578#define LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) (0xffffffff|(((i64)0x7fffffff)<<32))
176579#define SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) (((i64)-1) - LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)))
176580
176581#define deliberate_fall_through
176582
176583#endif /* SQLITE_AMALGAMATION */
176584
176585#ifdef SQLITE_DEBUG
176586SQLITE_PRIVATEstatic int sqlite3Fts3Corrupt(void);
176587# define FTS_CORRUPT_VTAB(11 | (1<<8)) sqlite3Fts3Corrupt()
176588#else
176589# define FTS_CORRUPT_VTAB(11 | (1<<8)) SQLITE_CORRUPT_VTAB(11 | (1<<8))
176590#endif
176591
176592typedef struct Fts3Table Fts3Table;
176593typedef struct Fts3Cursor Fts3Cursor;
176594typedef struct Fts3Expr Fts3Expr;
176595typedef struct Fts3Phrase Fts3Phrase;
176596typedef struct Fts3PhraseToken Fts3PhraseToken;
176597
176598typedef struct Fts3Doclist Fts3Doclist;
176599typedef struct Fts3SegFilter Fts3SegFilter;
176600typedef struct Fts3DeferredToken Fts3DeferredToken;
176601typedef struct Fts3SegReader Fts3SegReader;
176602typedef struct Fts3MultiSegReader Fts3MultiSegReader;
176603
176604typedef struct MatchinfoBuffer MatchinfoBuffer;
176605
176606/*
176607** A connection to a fulltext index is an instance of the following
176608** structure. The xCreate and xConnect methods create an instance
176609** of this structure and xDestroy and xDisconnect free that instance.
176610** All other methods receive a pointer to the structure as one of their
176611** arguments.
176612*/
176613struct Fts3Table {
176614 sqlite3_vtab base; /* Base class used by SQLite core */
176615 sqlite3 *db; /* The database connection */
176616 const char *zDb; /* logical database name */
176617 const char *zName; /* virtual table name */
176618 int nColumn; /* number of named columns in virtual table */
176619 char **azColumn; /* column names. malloced */
176620 u8 *abNotindexed; /* True for 'notindexed' columns */
176621 sqlite3_tokenizer *pTokenizer; /* tokenizer for inserts and queries */
176622 char *zContentTbl; /* content=xxx option, or NULL */
176623 char *zLanguageid; /* languageid=xxx option, or NULL */
176624 int nAutoincrmerge; /* Value configured by 'automerge' */
176625 u32 nLeafAdd; /* Number of leaf blocks added this trans */
176626 int bLock; /* Used to prevent recursive content= tbls */
176627
176628 /* Precompiled statements used by the implementation. Each of these
176629 ** statements is run and reset within a single virtual table API call.
176630 */
176631 sqlite3_stmt *aStmt[40];
176632 sqlite3_stmt *pSeekStmt; /* Cache for fts3CursorSeekStmt() */
176633
176634 char *zReadExprlist;
176635 char *zWriteExprlist;
176636
176637 int nNodeSize; /* Soft limit for node size */
176638 u8 bFts4; /* True for FTS4, false for FTS3 */
176639 u8 bHasStat; /* True if %_stat table exists (2==unknown) */
176640 u8 bHasDocsize; /* True if %_docsize table exists */
176641 u8 bDescIdx; /* True if doclists are in reverse order */
176642 u8 bIgnoreSavepoint; /* True to ignore xSavepoint invocations */
176643 int nPgsz; /* Page size for host database */
176644 char *zSegmentsTbl; /* Name of %_segments table */
176645 sqlite3_blob *pSegments; /* Blob handle open on %_segments table */
176646
176647 /*
176648 ** The following array of hash tables is used to buffer pending index
176649 ** updates during transactions. All pending updates buffered at any one
176650 ** time must share a common language-id (see the FTS4 langid= feature).
176651 ** The current language id is stored in variable iPrevLangid.
176652 **
176653 ** A single FTS4 table may have multiple full-text indexes. For each index
176654 ** there is an entry in the aIndex[] array. Index 0 is an index of all the
176655 ** terms that appear in the document set. Each subsequent index in aIndex[]
176656 ** is an index of prefixes of a specific length.
176657 **
176658 ** Variable nPendingData contains an estimate the memory consumed by the
176659 ** pending data structures, including hash table overhead, but not including
176660 ** malloc overhead. When nPendingData exceeds nMaxPendingData, all hash
176661 ** tables are flushed to disk. Variable iPrevDocid is the docid of the most
176662 ** recently inserted record.
176663 */
176664 int nIndex; /* Size of aIndex[] */
176665 struct Fts3Index {
176666 int nPrefix; /* Prefix length (0 for main terms index) */
176667 Fts3Hash hPending; /* Pending terms table for this index */
176668 } *aIndex;
176669 int nMaxPendingData; /* Max pending data before flush to disk */
176670 int nPendingData; /* Current bytes of pending data */
176671 sqlite_int64 iPrevDocid; /* Docid of most recently inserted document */
176672 int iPrevLangid; /* Langid of recently inserted document */
176673 int bPrevDelete; /* True if last operation was a delete */
176674
176675#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
176676 /* State variables used for validating that the transaction control
176677 ** methods of the virtual table are called at appropriate times. These
176678 ** values do not contribute to FTS functionality; they are used for
176679 ** verifying the operation of the SQLite core.
176680 */
176681 int inTransaction; /* True after xBegin but before xCommit/xRollback */
176682 int mxSavepoint; /* Largest valid xSavepoint integer */
176683#endif
176684
176685#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
176686 /* True to disable the incremental doclist optimization. This is controled
176687 ** by special insert command 'test-no-incr-doclist'. */
176688 int bNoIncrDoclist;
176689
176690 /* Number of segments in a level */
176691 int nMergeCount;
176692#endif
176693};
176694
176695/* Macro to find the number of segments to merge */
176696#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
176697# define MergeCount(P)16 ((P)->nMergeCount)
176698#else
176699# define MergeCount(P)16 FTS3_MERGE_COUNT16
176700#endif
176701
176702/*
176703** When the core wants to read from the virtual table, it creates a
176704** virtual table cursor (an instance of the following structure) using
176705** the xOpen method. Cursors are destroyed using the xClose method.
176706*/
176707struct Fts3Cursor {
176708 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
176709 i16 eSearch; /* Search strategy (see below) */
176710 u8 isEof; /* True if at End Of Results */
176711 u8 isRequireSeek; /* True if must seek pStmt to %_content row */
176712 u8 bSeekStmt; /* True if pStmt is a seek */
176713 sqlite3_stmt *pStmt; /* Prepared statement in use by the cursor */
176714 Fts3Expr *pExpr; /* Parsed MATCH query string */
176715 int iLangid; /* Language being queried for */
176716 int nPhrase; /* Number of matchable phrases in query */
176717 Fts3DeferredToken *pDeferred; /* Deferred search tokens, if any */
176718 sqlite3_int64 iPrevId; /* Previous id read from aDoclist */
176719 char *pNextId; /* Pointer into the body of aDoclist */
176720 char *aDoclist; /* List of docids for full-text queries */
176721 int nDoclist; /* Size of buffer at aDoclist */
176722 u8 bDesc; /* True to sort in descending order */
176723 int eEvalmode; /* An FTS3_EVAL_XX constant */
176724 int nRowAvg; /* Average size of database rows, in pages */
176725 sqlite3_int64 nDoc; /* Documents in table */
176726 i64 iMinDocid; /* Minimum docid to return */
176727 i64 iMaxDocid; /* Maximum docid to return */
176728 int isMatchinfoNeeded; /* True when aMatchinfo[] needs filling in */
176729 MatchinfoBuffer *pMIBuffer; /* Buffer for matchinfo data */
176730};
176731
176732#define FTS3_EVAL_FILTER0 0
176733#define FTS3_EVAL_NEXT1 1
176734#define FTS3_EVAL_MATCHINFO2 2
176735
176736/*
176737** The Fts3Cursor.eSearch member is always set to one of the following.
176738** Actualy, Fts3Cursor.eSearch can be greater than or equal to
176739** FTS3_FULLTEXT_SEARCH. If so, then Fts3Cursor.eSearch - 2 is the index
176740** of the column to be searched. For example, in
176741**
176742** CREATE VIRTUAL TABLE ex1 USING fts3(a,b,c,d);
176743** SELECT docid FROM ex1 WHERE b MATCH 'one two three';
176744**
176745** Because the LHS of the MATCH operator is 2nd column "b",
176746** Fts3Cursor.eSearch will be set to FTS3_FULLTEXT_SEARCH+1. (+0 for a,
176747** +1 for b, +2 for c, +3 for d.) If the LHS of MATCH were "ex1"
176748** indicating that all columns should be searched,
176749** then eSearch would be set to FTS3_FULLTEXT_SEARCH+4.
176750*/
176751#define FTS3_FULLSCAN_SEARCH0 0 /* Linear scan of %_content table */
176752#define FTS3_DOCID_SEARCH1 1 /* Lookup by rowid on %_content table */
176753#define FTS3_FULLTEXT_SEARCH2 2 /* Full-text index search */
176754
176755/*
176756** The lower 16-bits of the sqlite3_index_info.idxNum value set by
176757** the xBestIndex() method contains the Fts3Cursor.eSearch value described
176758** above. The upper 16-bits contain a combination of the following
176759** bits, used to describe extra constraints on full-text searches.
176760*/
176761#define FTS3_HAVE_LANGID0x00010000 0x00010000 /* languageid=? */
176762#define FTS3_HAVE_DOCID_GE0x00020000 0x00020000 /* docid>=? */
176763#define FTS3_HAVE_DOCID_LE0x00040000 0x00040000 /* docid<=? */
176764
176765struct Fts3Doclist {
176766 char *aAll; /* Array containing doclist (or NULL) */
176767 int nAll; /* Size of a[] in bytes */
176768 char *pNextDocid; /* Pointer to next docid */
176769
176770 sqlite3_int64 iDocid; /* Current docid (if pList!=0) */
176771 int bFreeList; /* True if pList should be sqlite3_free()d */
176772 char *pList; /* Pointer to position list following iDocid */
176773 int nList; /* Length of position list */
176774};
176775
176776/*
176777** A "phrase" is a sequence of one or more tokens that must match in
176778** sequence. A single token is the base case and the most common case.
176779** For a sequence of tokens contained in double-quotes (i.e. "one two three")
176780** nToken will be the number of tokens in the string.
176781*/
176782struct Fts3PhraseToken {
176783 char *z; /* Text of the token */
176784 int n; /* Number of bytes in buffer z */
176785 int isPrefix; /* True if token ends with a "*" character */
176786 int bFirst; /* True if token must appear at position 0 */
176787
176788 /* Variables above this point are populated when the expression is
176789 ** parsed (by code in fts3_expr.c). Below this point the variables are
176790 ** used when evaluating the expression. */
176791 Fts3DeferredToken *pDeferred; /* Deferred token object for this token */
176792 Fts3MultiSegReader *pSegcsr; /* Segment-reader for this token */
176793};
176794
176795struct Fts3Phrase {
176796 /* Cache of doclist for this phrase. */
176797 Fts3Doclist doclist;
176798 int bIncr; /* True if doclist is loaded incrementally */
176799 int iDoclistToken;
176800
176801 /* Used by sqlite3Fts3EvalPhrasePoslist() if this is a descendent of an
176802 ** OR condition. */
176803 char *pOrPoslist;
176804 i64 iOrDocid;
176805
176806 /* Variables below this point are populated by fts3_expr.c when parsing
176807 ** a MATCH expression. Everything above is part of the evaluation phase.
176808 */
176809 int nToken; /* Number of tokens in the phrase */
176810 int iColumn; /* Index of column this phrase must match */
176811 Fts3PhraseToken aToken[1]; /* One entry for each token in the phrase */
176812};
176813
176814/*
176815** A tree of these objects forms the RHS of a MATCH operator.
176816**
176817** If Fts3Expr.eType is FTSQUERY_PHRASE and isLoaded is true, then aDoclist
176818** points to a malloced buffer, size nDoclist bytes, containing the results
176819** of this phrase query in FTS3 doclist format. As usual, the initial
176820** "Length" field found in doclists stored on disk is omitted from this
176821** buffer.
176822**
176823** Variable aMI is used only for FTSQUERY_NEAR nodes to store the global
176824** matchinfo data. If it is not NULL, it points to an array of size nCol*3,
176825** where nCol is the number of columns in the queried FTS table. The array
176826** is populated as follows:
176827**
176828** aMI[iCol*3 + 0] = Undefined
176829** aMI[iCol*3 + 1] = Number of occurrences
176830** aMI[iCol*3 + 2] = Number of rows containing at least one instance
176831**
176832** The aMI array is allocated using sqlite3_malloc(). It should be freed
176833** when the expression node is.
176834*/
176835struct Fts3Expr {
176836 int eType; /* One of the FTSQUERY_XXX values defined below */
176837 int nNear; /* Valid if eType==FTSQUERY_NEAR */
176838 Fts3Expr *pParent; /* pParent->pLeft==this or pParent->pRight==this */
176839 Fts3Expr *pLeft; /* Left operand */
176840 Fts3Expr *pRight; /* Right operand */
176841 Fts3Phrase *pPhrase; /* Valid if eType==FTSQUERY_PHRASE */
176842
176843 /* The following are used by the fts3_eval.c module. */
176844 sqlite3_int64 iDocid; /* Current docid */
176845 u8 bEof; /* True this expression is at EOF already */
176846 u8 bStart; /* True if iDocid is valid */
176847 u8 bDeferred; /* True if this expression is entirely deferred */
176848
176849 /* The following are used by the fts3_snippet.c module. */
176850 int iPhrase; /* Index of this phrase in matchinfo() results */
176851 u32 *aMI; /* See above */
176852};
176853
176854/*
176855** Candidate values for Fts3Query.eType. Note that the order of the first
176856** four values is in order of precedence when parsing expressions. For
176857** example, the following:
176858**
176859** "a OR b AND c NOT d NEAR e"
176860**
176861** is equivalent to:
176862**
176863** "a OR (b AND (c NOT (d NEAR e)))"
176864*/
176865#define FTSQUERY_NEAR1 1
176866#define FTSQUERY_NOT2 2
176867#define FTSQUERY_AND3 3
176868#define FTSQUERY_OR4 4
176869#define FTSQUERY_PHRASE5 5
176870
176871
176872/* fts3_write.c */
176873SQLITE_PRIVATEstatic int sqlite3Fts3UpdateMethod(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*);
176874SQLITE_PRIVATEstatic int sqlite3Fts3PendingTermsFlush(Fts3Table *);
176875SQLITE_PRIVATEstatic void sqlite3Fts3PendingTermsClear(Fts3Table *);
176876SQLITE_PRIVATEstatic int sqlite3Fts3Optimize(Fts3Table *);
176877SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderNew(int, int, sqlite3_int64,
176878 sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**);
176879SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderPending(
176880 Fts3Table*,int,const char*,int,int,Fts3SegReader**);
176881SQLITE_PRIVATEstatic void sqlite3Fts3SegReaderFree(Fts3SegReader *);
176882SQLITE_PRIVATEstatic int sqlite3Fts3AllSegdirs(Fts3Table*, int, int, int, sqlite3_stmt **);
176883SQLITE_PRIVATEstatic int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*, int*);
176884
176885SQLITE_PRIVATEstatic int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
176886SQLITE_PRIVATEstatic int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
176887
176888#ifndef SQLITE_DISABLE_FTS4_DEFERRED
176889SQLITE_PRIVATEstatic void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
176890SQLITE_PRIVATEstatic int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
176891SQLITE_PRIVATEstatic int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
176892SQLITE_PRIVATEstatic void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
176893SQLITE_PRIVATEstatic int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
176894#else
176895# define sqlite3Fts3FreeDeferredTokens(x)
176896# define sqlite3Fts3DeferToken(x,y,z) SQLITE_OK0
176897# define sqlite3Fts3CacheDeferredDoclists(x) SQLITE_OK0
176898# define sqlite3Fts3FreeDeferredDoclists(x)
176899# define sqlite3Fts3DeferredTokenList(x,y,z) SQLITE_OK0
176900#endif
176901
176902SQLITE_PRIVATEstatic void sqlite3Fts3SegmentsClose(Fts3Table *);
176903SQLITE_PRIVATEstatic int sqlite3Fts3MaxLevel(Fts3Table *, int *);
176904
176905/* Special values interpreted by sqlite3SegReaderCursor() */
176906#define FTS3_SEGCURSOR_PENDING-1 -1
176907#define FTS3_SEGCURSOR_ALL-2 -2
176908
176909SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3MultiSegReader*, Fts3SegFilter*);
176910SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3MultiSegReader *);
176911SQLITE_PRIVATEstatic void sqlite3Fts3SegReaderFinish(Fts3MultiSegReader *);
176912
176913SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderCursor(Fts3Table *,
176914 int, int, int, const char *, int, int, int, Fts3MultiSegReader *);
176915
176916/* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
176917#define FTS3_SEGMENT_REQUIRE_POS0x00000001 0x00000001
176918#define FTS3_SEGMENT_IGNORE_EMPTY0x00000002 0x00000002
176919#define FTS3_SEGMENT_COLUMN_FILTER0x00000004 0x00000004
176920#define FTS3_SEGMENT_PREFIX0x00000008 0x00000008
176921#define FTS3_SEGMENT_SCAN0x00000010 0x00000010
176922#define FTS3_SEGMENT_FIRST0x00000020 0x00000020
176923
176924/* Type passed as 4th argument to SegmentReaderIterate() */
176925struct Fts3SegFilter {
176926 const char *zTerm;
176927 int nTerm;
176928 int iCol;
176929 int flags;
176930};
176931
176932struct Fts3MultiSegReader {
176933 /* Used internally by sqlite3Fts3SegReaderXXX() calls */
176934 Fts3SegReader **apSegment; /* Array of Fts3SegReader objects */
176935 int nSegment; /* Size of apSegment array */
176936 int nAdvance; /* How many seg-readers to advance */
176937 Fts3SegFilter *pFilter; /* Pointer to filter object */
176938 char *aBuffer; /* Buffer to merge doclists in */
176939 int nBuffer; /* Allocated size of aBuffer[] in bytes */
176940
176941 int iColFilter; /* If >=0, filter for this column */
176942 int bRestart;
176943
176944 /* Used by fts3.c only. */
176945 int nCost; /* Cost of running iterator */
176946 int bLookup; /* True if a lookup of a single entry. */
176947
176948 /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */
176949 char *zTerm; /* Pointer to term buffer */
176950 int nTerm; /* Size of zTerm in bytes */
176951 char *aDoclist; /* Pointer to doclist buffer */
176952 int nDoclist; /* Size of aDoclist[] in bytes */
176953};
176954
176955SQLITE_PRIVATEstatic int sqlite3Fts3Incrmerge(Fts3Table*,int,int);
176956
176957#define fts3GetVarint32(p, piVal)( (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, piVal) : (
*piVal=*(u8*)(p), 1) )
( \
176958 (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, piVal) : (*piVal=*(u8*)(p), 1) \
176959)
176960
176961/* fts3.c */
176962SQLITE_PRIVATEstatic void sqlite3Fts3ErrMsg(char**,const char*,...);
176963SQLITE_PRIVATEstatic int sqlite3Fts3PutVarint(char *, sqlite3_int64);
176964SQLITE_PRIVATEstatic int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
176965SQLITE_PRIVATEstatic int sqlite3Fts3GetVarintU(const char *, sqlite_uint64 *);
176966SQLITE_PRIVATEstatic int sqlite3Fts3GetVarintBounded(const char*,const char*,sqlite3_int64*);
176967SQLITE_PRIVATEstatic int sqlite3Fts3GetVarint32(const char *, int *);
176968SQLITE_PRIVATEstatic int sqlite3Fts3VarintLen(sqlite3_uint64);
176969SQLITE_PRIVATEstatic void sqlite3Fts3Dequote(char *);
176970SQLITE_PRIVATEstatic void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
176971SQLITE_PRIVATEstatic int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
176972SQLITE_PRIVATEstatic int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *);
176973SQLITE_PRIVATEstatic void sqlite3Fts3CreateStatTable(int*, Fts3Table*);
176974SQLITE_PRIVATEstatic int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc);
176975SQLITE_PRIVATEstatic int sqlite3Fts3ReadInt(const char *z, int *pnOut);
176976
176977/* fts3_tokenizer.c */
176978SQLITE_PRIVATEstatic const char *sqlite3Fts3NextToken(const char *, int *);
176979SQLITE_PRIVATEstatic int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
176980SQLITE_PRIVATEstatic int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *,
176981 sqlite3_tokenizer **, char **
176982);
176983SQLITE_PRIVATEstatic int sqlite3Fts3IsIdChar(char);
176984
176985/* fts3_snippet.c */
176986SQLITE_PRIVATEstatic void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
176987SQLITE_PRIVATEstatic void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
176988 const char *, const char *, int, int
176989);
176990SQLITE_PRIVATEstatic void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
176991SQLITE_PRIVATEstatic void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p);
176992
176993/* fts3_expr.c */
176994SQLITE_PRIVATEstatic int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
176995 char **, int, int, int, const char *, int, Fts3Expr **, char **
176996);
176997SQLITE_PRIVATEstatic void sqlite3Fts3ExprFree(Fts3Expr *);
176998#ifdef SQLITE_TEST
176999SQLITE_PRIVATEstatic int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash*);
177000SQLITE_PRIVATEstatic int sqlite3Fts3InitTerm(sqlite3 *db);
177001#endif
177002SQLITE_PRIVATEstatic void *sqlite3Fts3MallocZero(i64 nByte);
177003
177004SQLITE_PRIVATEstatic int sqlite3Fts3OpenTokenizer(sqlite3_tokenizer *, int, const char *, int,
177005 sqlite3_tokenizer_cursor **
177006);
177007
177008/* fts3_aux.c */
177009SQLITE_PRIVATEstatic int sqlite3Fts3InitAux(sqlite3 *db);
177010
177011SQLITE_PRIVATEstatic void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *);
177012
177013SQLITE_PRIVATEstatic int sqlite3Fts3MsrIncrStart(
177014 Fts3Table*, Fts3MultiSegReader*, int, const char*, int);
177015SQLITE_PRIVATEstatic int sqlite3Fts3MsrIncrNext(
177016 Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
177017SQLITE_PRIVATEstatic int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **);
177018SQLITE_PRIVATEstatic int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
177019SQLITE_PRIVATEstatic int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
177020
177021/* fts3_tokenize_vtab.c */
177022SQLITE_PRIVATEstatic int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *, void(*xDestroy)(void*));
177023
177024/* fts3_unicode2.c (functions generated by parsing unicode text files) */
177025#ifndef SQLITE_DISABLE_FTS3_UNICODE
177026SQLITE_PRIVATEstatic int sqlite3FtsUnicodeFold(int, int);
177027SQLITE_PRIVATEstatic int sqlite3FtsUnicodeIsalnum(int);
177028SQLITE_PRIVATEstatic int sqlite3FtsUnicodeIsdiacritic(int);
177029#endif
177030
177031#endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */
177032#endif /* _FTSINT_H */
177033
177034/************** End of fts3Int.h *********************************************/
177035/************** Continuing where we left off in fts3.c ***********************/
177036#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
177037
177038#if defined(SQLITE_ENABLE_FTS31) && !defined(SQLITE_CORE1)
177039# define SQLITE_CORE1 1
177040#endif
177041
177042/* #include <assert.h> */
177043/* #include <stdlib.h> */
177044/* #include <stddef.h> */
177045/* #include <stdio.h> */
177046/* #include <string.h> */
177047/* #include <stdarg.h> */
177048
177049/* #include "fts3.h" */
177050#ifndef SQLITE_CORE1
177051/* # include "sqlite3ext.h" */
177052 SQLITE_EXTENSION_INIT1
177053#endif
177054
177055typedef struct Fts3HashWrapper Fts3HashWrapper;
177056struct Fts3HashWrapper {
177057 Fts3Hash hash; /* Hash table */
177058 int nRef; /* Number of pointers to this object */
177059};
177060
177061static int fts3EvalNext(Fts3Cursor *pCsr);
177062static int fts3EvalStart(Fts3Cursor *pCsr);
177063static int fts3TermSegReaderCursor(
177064 Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **);
177065
177066/*
177067** This variable is set to false when running tests for which the on disk
177068** structures should not be corrupt. Otherwise, true. If it is false, extra
177069** assert() conditions in the fts3 code are activated - conditions that are
177070** only true if it is guaranteed that the fts3 database is not corrupt.
177071*/
177072#ifdef SQLITE_DEBUG
177073SQLITE_API int sqlite3_fts3_may_be_corrupt = 1;
177074#endif
177075
177076/*
177077** Write a 64-bit variable-length integer to memory starting at p[0].
177078** The length of data written will be between 1 and FTS3_VARINT_MAX bytes.
177079** The number of bytes written is returned.
177080*/
177081SQLITE_PRIVATEstatic int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
177082 unsigned char *q = (unsigned char *) p;
177083 sqlite_uint64 vu = v;
177084 do{
177085 *q++ = (unsigned char) ((vu & 0x7f) | 0x80);
177086 vu >>= 7;
177087 }while( vu!=0 );
177088 q[-1] &= 0x7f; /* turn off high bit in final byte */
177089 assert( q - (unsigned char *)p <= FTS3_VARINT_MAX )((void) (0));
177090 return (int) (q - (unsigned char *)p);
177091}
177092
177093#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; }
\
177094 v = (v & mask1) | ( (*(const unsigned char*)(ptr++)) << shift ); \
177095 if( (v & mask2)==0 ){ var = v; return ret; }
177096#define GETVARINT_INIT(v, ptr, shift, mask1, mask2, var, ret)v = (*ptr++); if( (v & mask2)==0 ){ var = v; return ret; } \
177097 v = (*ptr++); \
177098 if( (v & mask2)==0 ){ var = v; return ret; }
177099
177100SQLITE_PRIVATEstatic int sqlite3Fts3GetVarintU(const char *pBuf, sqlite_uint64 *v){
177101 const unsigned char *p = (const unsigned char*)pBuf;
177102 const unsigned char *pStart = p;
177103 u32 a;
177104 u64 b;
177105 int shift;
177106
177107 GETVARINT_INIT(a, p, 0, 0x00, 0x80, *v, 1)a = (*p++); if( (a & 0x80)==0 ){ *v = a; return 1; };
177108 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; }
;
177109 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; }
;
177110 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; }
;
177111 b = (a & 0x0FFFFFFF );
177112
177113 for(shift=28; shift<=63; shift+=7){
177114 u64 c = *p++;
177115 b += (c&0x7F) << shift;
177116 if( (c & 0x80)==0 ) break;
177117 }
177118 *v = b;
177119 return (int)(p - pStart);
177120}
177121
177122/*
177123** Read a 64-bit variable-length integer from memory starting at p[0].
177124** Return the number of bytes read, or 0 on error.
177125** The value is stored in *v.
177126*/
177127SQLITE_PRIVATEstatic int sqlite3Fts3GetVarint(const char *pBuf, sqlite_int64 *v){
177128 return sqlite3Fts3GetVarintU(pBuf, (sqlite3_uint64*)v);
177129}
177130
177131/*
177132** Read a 64-bit variable-length integer from memory starting at p[0] and
177133** not extending past pEnd[-1].
177134** Return the number of bytes read, or 0 on error.
177135** The value is stored in *v.
177136*/
177137SQLITE_PRIVATEstatic int sqlite3Fts3GetVarintBounded(
177138 const char *pBuf,
177139 const char *pEnd,
177140 sqlite_int64 *v
177141){
177142 const unsigned char *p = (const unsigned char*)pBuf;
177143 const unsigned char *pStart = p;
177144 const unsigned char *pX = (const unsigned char*)pEnd;
177145 u64 b = 0;
177146 int shift;
177147 for(shift=0; shift<=63; shift+=7){
177148 u64 c = p<pX ? *p : 0;
177149 p++;
177150 b += (c&0x7F) << shift;
177151 if( (c & 0x80)==0 ) break;
177152 }
177153 *v = b;
177154 return (int)(p - pStart);
177155}
177156
177157/*
177158** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to
177159** a non-negative 32-bit integer before it is returned.
177160*/
177161SQLITE_PRIVATEstatic int sqlite3Fts3GetVarint32(const char *p, int *pi){
177162 const unsigned char *ptr = (const unsigned char*)p;
177163 u32 a;
177164
177165#ifndef fts3GetVarint32
177166 GETVARINT_INIT(a, ptr, 0, 0x00, 0x80, *pi, 1)a = (*ptr++); if( (a & 0x80)==0 ){ *pi = a; return 1; };
177167#else
177168 a = (*ptr++);
177169 assert( a & 0x80 )((void) (0));
177170#endif
177171
177172 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; }
;
177173 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; }
;
177174 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; }
;
177175 a = (a & 0x0FFFFFFF );
177176 *pi = (int)(a | ((u32)(*ptr & 0x07) << 28));
177177 assert( 0==(a & 0x80000000) )((void) (0));
177178 assert( *pi>=0 )((void) (0));
177179 return 5;
177180}
177181
177182/*
177183** Return the number of bytes required to encode v as a varint
177184*/
177185SQLITE_PRIVATEstatic int sqlite3Fts3VarintLen(sqlite3_uint64 v){
177186 int i = 0;
177187 do{
177188 i++;
177189 v >>= 7;
177190 }while( v!=0 );
177191 return i;
177192}
177193
177194/*
177195** Convert an SQL-style quoted string into a normal string by removing
177196** the quote characters. The conversion is done in-place. If the
177197** input does not begin with a quote character, then this routine
177198** is a no-op.
177199**
177200** Examples:
177201**
177202** "abc" becomes abc
177203** 'xyz' becomes xyz
177204** [pqr] becomes pqr
177205** `mno` becomes mno
177206**
177207*/
177208SQLITE_PRIVATEstatic void sqlite3Fts3Dequote(char *z){
177209 char quote; /* Quote character (if any ) */
177210
177211 quote = z[0];
177212 if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
177213 int iIn = 1; /* Index of next byte to read from input */
177214 int iOut = 0; /* Index of next byte to write to output */
177215
177216 /* If the first byte was a '[', then the close-quote character is a ']' */
177217 if( quote=='[' ) quote = ']';
177218
177219 while( z[iIn] ){
177220 if( z[iIn]==quote ){
177221 if( z[iIn+1]!=quote ) break;
177222 z[iOut++] = quote;
177223 iIn += 2;
177224 }else{
177225 z[iOut++] = z[iIn++];
177226 }
177227 }
177228 z[iOut] = '\0';
177229 }
177230}
177231
177232/*
177233** Read a single varint from the doclist at *pp and advance *pp to point
177234** to the first byte past the end of the varint. Add the value of the varint
177235** to *pVal.
177236*/
177237static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){
177238 sqlite3_int64 iVal;
177239 *pp += sqlite3Fts3GetVarint(*pp, &iVal);
177240 *pVal += iVal;
177241}
177242
177243/*
177244** When this function is called, *pp points to the first byte following a
177245** varint that is part of a doclist (or position-list, or any other list
177246** of varints). This function moves *pp to point to the start of that varint,
177247** and sets *pVal by the varint value.
177248**
177249** Argument pStart points to the first byte of the doclist that the
177250** varint is part of.
177251*/
177252static void fts3GetReverseVarint(
177253 char **pp,
177254 char *pStart,
177255 sqlite3_int64 *pVal
177256){
177257 sqlite3_int64 iVal;
177258 char *p;
177259
177260 /* Pointer p now points at the first byte past the varint we are
177261 ** interested in. So, unless the doclist is corrupt, the 0x80 bit is
177262 ** clear on character p[-1]. */
177263 for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
177264 p++;
177265 *pp = p;
177266
177267 sqlite3Fts3GetVarint(p, &iVal);
177268 *pVal = iVal;
177269}
177270
177271/*
177272** The xDisconnect() virtual table method.
177273*/
177274static int fts3DisconnectMethod(sqlite3_vtab *pVtab){
177275 Fts3Table *p = (Fts3Table *)pVtab;
177276 int i;
177277
177278 assert( p->nPendingData==0 )((void) (0));
177279 assert( p->pSegments==0 )((void) (0));
177280
177281 /* Free any prepared statements held */
177282 sqlite3_finalize(p->pSeekStmt);
177283 for(i=0; i<SizeofArray(p->aStmt)((int)(sizeof(p->aStmt)/sizeof(p->aStmt[0]))); i++){
177284 sqlite3_finalize(p->aStmt[i]);
177285 }
177286 sqlite3_free(p->zSegmentsTbl);
177287 sqlite3_free(p->zReadExprlist);
177288 sqlite3_free(p->zWriteExprlist);
177289 sqlite3_free(p->zContentTbl);
177290 sqlite3_free(p->zLanguageid);
177291
177292 /* Invoke the tokenizer destructor to free the tokenizer. */
177293 p->pTokenizer->pModule->xDestroy(p->pTokenizer);
177294
177295 sqlite3_free(p);
177296 return SQLITE_OK0;
177297}
177298
177299/*
177300** Write an error message into *pzErr
177301*/
177302SQLITE_PRIVATEstatic void sqlite3Fts3ErrMsg(char **pzErr, const char *zFormat, ...){
177303 va_list ap;
177304 sqlite3_free(*pzErr);
177305 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
177306 *pzErr = sqlite3_vmprintf(zFormat, ap);
177307 va_end(ap)__builtin_va_end(ap);
177308}
177309
177310/*
177311** Construct one or more SQL statements from the format string given
177312** and then evaluate those statements. The success code is written
177313** into *pRc.
177314**
177315** If *pRc is initially non-zero then this routine is a no-op.
177316*/
177317static void fts3DbExec(
177318 int *pRc, /* Success code */
177319 sqlite3 *db, /* Database in which to run SQL */
177320 const char *zFormat, /* Format string for SQL */
177321 ... /* Arguments to the format string */
177322){
177323 va_list ap;
177324 char *zSql;
177325 if( *pRc ) return;
177326 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
177327 zSql = sqlite3_vmprintf(zFormat, ap);
177328 va_end(ap)__builtin_va_end(ap);
177329 if( zSql==0 ){
177330 *pRc = SQLITE_NOMEM7;
177331 }else{
177332 *pRc = sqlite3_exec(db, zSql, 0, 0, 0);
177333 sqlite3_free(zSql);
177334 }
177335}
177336
177337/*
177338** The xDestroy() virtual table method.
177339*/
177340static int fts3DestroyMethod(sqlite3_vtab *pVtab){
177341 Fts3Table *p = (Fts3Table *)pVtab;
177342 int rc = SQLITE_OK0; /* Return code */
177343 const char *zDb = p->zDb; /* Name of database (e.g. "main", "temp") */
177344 sqlite3 *db = p->db; /* Database handle */
177345
177346 /* Drop the shadow tables */
177347 fts3DbExec(&rc, db,
177348 "DROP TABLE IF EXISTS %Q.'%q_segments';"
177349 "DROP TABLE IF EXISTS %Q.'%q_segdir';"
177350 "DROP TABLE IF EXISTS %Q.'%q_docsize';"
177351 "DROP TABLE IF EXISTS %Q.'%q_stat';"
177352 "%s DROP TABLE IF EXISTS %Q.'%q_content';",
177353 zDb, p->zName,
177354 zDb, p->zName,
177355 zDb, p->zName,
177356 zDb, p->zName,
177357 (p->zContentTbl ? "--" : ""), zDb,p->zName
177358 );
177359
177360 /* If everything has worked, invoke fts3DisconnectMethod() to free the
177361 ** memory associated with the Fts3Table structure and return SQLITE_OK.
177362 ** Otherwise, return an SQLite error code.
177363 */
177364 return (rc==SQLITE_OK0 ? fts3DisconnectMethod(pVtab) : rc);
177365}
177366
177367
177368/*
177369** Invoke sqlite3_declare_vtab() to declare the schema for the FTS3 table
177370** passed as the first argument. This is done as part of the xConnect()
177371** and xCreate() methods.
177372**
177373** If *pRc is non-zero when this function is called, it is a no-op.
177374** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
177375** before returning.
177376*/
177377static void fts3DeclareVtab(int *pRc, Fts3Table *p){
177378 if( *pRc==SQLITE_OK0 ){
177379 int i; /* Iterator variable */
177380 int rc; /* Return code */
177381 char *zSql; /* SQL statement passed to declare_vtab() */
177382 char *zCols; /* List of user defined columns */
177383 const char *zLanguageid;
177384
177385 zLanguageid = (p->zLanguageid ? p->zLanguageid : "__langid");
177386 sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT1, 1);
177387
177388 /* Create a list of user columns for the virtual table */
177389 zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]);
177390 for(i=1; zCols && i<p->nColumn; i++){
177391 zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]);
177392 }
177393
177394 /* Create the whole "CREATE TABLE" statement to pass to SQLite */
177395 zSql = sqlite3_mprintf(
177396 "CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN, %Q HIDDEN)",
177397 zCols, p->zName, zLanguageid
177398 );
177399 if( !zCols || !zSql ){
177400 rc = SQLITE_NOMEM7;
177401 }else{
177402 rc = sqlite3_declare_vtab(p->db, zSql);
177403 }
177404
177405 sqlite3_free(zSql);
177406 sqlite3_free(zCols);
177407 *pRc = rc;
177408 }
177409}
177410
177411/*
177412** Create the %_stat table if it does not already exist.
177413*/
177414SQLITE_PRIVATEstatic void sqlite3Fts3CreateStatTable(int *pRc, Fts3Table *p){
177415 fts3DbExec(pRc, p->db,
177416 "CREATE TABLE IF NOT EXISTS %Q.'%q_stat'"
177417 "(id INTEGER PRIMARY KEY, value BLOB);",
177418 p->zDb, p->zName
177419 );
177420 if( (*pRc)==SQLITE_OK0 ) p->bHasStat = 1;
177421}
177422
177423/*
177424** Create the backing store tables (%_content, %_segments and %_segdir)
177425** required by the FTS3 table passed as the only argument. This is done
177426** as part of the vtab xCreate() method.
177427**
177428** If the p->bHasDocsize boolean is true (indicating that this is an
177429** FTS4 table, not an FTS3 table) then also create the %_docsize and
177430** %_stat tables required by FTS4.
177431*/
177432static int fts3CreateTables(Fts3Table *p){
177433 int rc = SQLITE_OK0; /* Return code */
177434 int i; /* Iterator variable */
177435 sqlite3 *db = p->db; /* The database connection */
177436
177437 if( p->zContentTbl==0 ){
177438 const char *zLanguageid = p->zLanguageid;
177439 char *zContentCols; /* Columns of %_content table */
177440
177441 /* Create a list of user columns for the content table */
177442 zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY");
177443 for(i=0; zContentCols && i<p->nColumn; i++){
177444 char *z = p->azColumn[i];
177445 zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z);
177446 }
177447 if( zLanguageid && zContentCols ){
177448 zContentCols = sqlite3_mprintf("%z, langid", zContentCols, zLanguageid);
177449 }
177450 if( zContentCols==0 ) rc = SQLITE_NOMEM7;
177451
177452 /* Create the content table */
177453 fts3DbExec(&rc, db,
177454 "CREATE TABLE %Q.'%q_content'(%s)",
177455 p->zDb, p->zName, zContentCols
177456 );
177457 sqlite3_free(zContentCols);
177458 }
177459
177460 /* Create other tables */
177461 fts3DbExec(&rc, db,
177462 "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);",
177463 p->zDb, p->zName
177464 );
177465 fts3DbExec(&rc, db,
177466 "CREATE TABLE %Q.'%q_segdir'("
177467 "level INTEGER,"
177468 "idx INTEGER,"
177469 "start_block INTEGER,"
177470 "leaves_end_block INTEGER,"
177471 "end_block INTEGER,"
177472 "root BLOB,"
177473 "PRIMARY KEY(level, idx)"
177474 ");",
177475 p->zDb, p->zName
177476 );
177477 if( p->bHasDocsize ){
177478 fts3DbExec(&rc, db,
177479 "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
177480 p->zDb, p->zName
177481 );
177482 }
177483 assert( p->bHasStat==p->bFts4 )((void) (0));
177484 if( p->bHasStat ){
177485 sqlite3Fts3CreateStatTable(&rc, p);
177486 }
177487 return rc;
177488}
177489
177490/*
177491** Store the current database page-size in bytes in p->nPgsz.
177492**
177493** If *pRc is non-zero when this function is called, it is a no-op.
177494** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
177495** before returning.
177496*/
177497static void fts3DatabasePageSize(int *pRc, Fts3Table *p){
177498 if( *pRc==SQLITE_OK0 ){
177499 int rc; /* Return code */
177500 char *zSql; /* SQL text "PRAGMA %Q.page_size" */
177501 sqlite3_stmt *pStmt; /* Compiled "PRAGMA %Q.page_size" statement */
177502
177503 zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb);
177504 if( !zSql ){
177505 rc = SQLITE_NOMEM7;
177506 }else{
177507 rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
177508 if( rc==SQLITE_OK0 ){
177509 sqlite3_step(pStmt);
177510 p->nPgsz = sqlite3_column_int(pStmt, 0);
177511 rc = sqlite3_finalize(pStmt);
177512 }else if( rc==SQLITE_AUTH23 ){
177513 p->nPgsz = 1024;
177514 rc = SQLITE_OK0;
177515 }
177516 }
177517 assert( p->nPgsz>0 || rc!=SQLITE_OK )((void) (0));
177518 sqlite3_free(zSql);
177519 *pRc = rc;
177520 }
177521}
177522
177523/*
177524** "Special" FTS4 arguments are column specifications of the following form:
177525**
177526** <key> = <value>
177527**
177528** There may not be whitespace surrounding the "=" character. The <value>
177529** term may be quoted, but the <key> may not.
177530*/
177531static int fts3IsSpecialColumn(
177532 const char *z,
177533 int *pnKey,
177534 char **pzValue
177535){
177536 char *zValue;
177537 const char *zCsr = z;
177538
177539 while( *zCsr!='=' ){
177540 if( *zCsr=='\0' ) return 0;
177541 zCsr++;
177542 }
177543
177544 *pnKey = (int)(zCsr-z);
177545 zValue = sqlite3_mprintf("%s", &zCsr[1]);
177546 if( zValue ){
177547 sqlite3Fts3Dequote(zValue);
177548 }
177549 *pzValue = zValue;
177550 return 1;
177551}
177552
177553/*
177554** Append the output of a printf() style formatting to an existing string.
177555*/
177556static void fts3Appendf(
177557 int *pRc, /* IN/OUT: Error code */
177558 char **pz, /* IN/OUT: Pointer to string buffer */
177559 const char *zFormat, /* Printf format string to append */
177560 ... /* Arguments for printf format string */
177561){
177562 if( *pRc==SQLITE_OK0 ){
177563 va_list ap;
177564 char *z;
177565 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
177566 z = sqlite3_vmprintf(zFormat, ap);
177567 va_end(ap)__builtin_va_end(ap);
177568 if( z && *pz ){
177569 char *z2 = sqlite3_mprintf("%s%s", *pz, z);
177570 sqlite3_free(z);
177571 z = z2;
177572 }
177573 if( z==0 ) *pRc = SQLITE_NOMEM7;
177574 sqlite3_free(*pz);
177575 *pz = z;
177576 }
177577}
177578
177579/*
177580** Return a copy of input string zInput enclosed in double-quotes (") and
177581** with all double quote characters escaped. For example:
177582**
177583** fts3QuoteId("un \"zip\"") -> "un \"\"zip\"\""
177584**
177585** The pointer returned points to memory obtained from sqlite3_malloc(). It
177586** is the callers responsibility to call sqlite3_free() to release this
177587** memory.
177588*/
177589static char *fts3QuoteId(char const *zInput){
177590 sqlite3_int64 nRet;
177591 char *zRet;
177592 nRet = 2 + (int)strlen(zInput)*2 + 1;
177593 zRet = sqlite3_malloc64(nRet);
177594 if( zRet ){
177595 int i;
177596 char *z = zRet;
177597 *(z++) = '"';
177598 for(i=0; zInput[i]; i++){
177599 if( zInput[i]=='"' ) *(z++) = '"';
177600 *(z++) = zInput[i];
177601 }
177602 *(z++) = '"';
177603 *(z++) = '\0';
177604 }
177605 return zRet;
177606}
177607
177608/*
177609** Return a list of comma separated SQL expressions and a FROM clause that
177610** could be used in a SELECT statement such as the following:
177611**
177612** SELECT <list of expressions> FROM %_content AS x ...
177613**
177614** to return the docid, followed by each column of text data in order
177615** from left to write. If parameter zFunc is not NULL, then instead of
177616** being returned directly each column of text data is passed to an SQL
177617** function named zFunc first. For example, if zFunc is "unzip" and the
177618** table has the three user-defined columns "a", "b", and "c", the following
177619** string is returned:
177620**
177621** "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c') FROM %_content AS x"
177622**
177623** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
177624** is the responsibility of the caller to eventually free it.
177625**
177626** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
177627** a NULL pointer is returned). Otherwise, if an OOM error is encountered
177628** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
177629** no error occurs, *pRc is left unmodified.
177630*/
177631static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){
177632 char *zRet = 0;
177633 char *zFree = 0;
177634 char *zFunction;
177635 int i;
177636
177637 if( p->zContentTbl==0 ){
177638 if( !zFunc ){
177639 zFunction = "";
177640 }else{
177641 zFree = zFunction = fts3QuoteId(zFunc);
177642 }
177643 fts3Appendf(pRc, &zRet, "docid");
177644 for(i=0; i<p->nColumn; i++){
177645 fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
177646 }
177647 if( p->zLanguageid ){
177648 fts3Appendf(pRc, &zRet, ", x.%Q", "langid");
177649 }
177650 sqlite3_free(zFree);
177651 }else{
177652 fts3Appendf(pRc, &zRet, "rowid");
177653 for(i=0; i<p->nColumn; i++){
177654 fts3Appendf(pRc, &zRet, ", x.'%q'", p->azColumn[i]);
177655 }
177656 if( p->zLanguageid ){
177657 fts3Appendf(pRc, &zRet, ", x.%Q", p->zLanguageid);
177658 }
177659 }
177660 fts3Appendf(pRc, &zRet, " FROM '%q'.'%q%s' AS x",
177661 p->zDb,
177662 (p->zContentTbl ? p->zContentTbl : p->zName),
177663 (p->zContentTbl ? "" : "_content")
177664 );
177665 return zRet;
177666}
177667
177668/*
177669** Return a list of N comma separated question marks, where N is the number
177670** of columns in the %_content table (one for the docid plus one for each
177671** user-defined text column).
177672**
177673** If argument zFunc is not NULL, then all but the first question mark
177674** is preceded by zFunc and an open bracket, and followed by a closed
177675** bracket. For example, if zFunc is "zip" and the FTS3 table has three
177676** user-defined text columns, the following string is returned:
177677**
177678** "?, zip(?), zip(?), zip(?)"
177679**
177680** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
177681** is the responsibility of the caller to eventually free it.
177682**
177683** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
177684** a NULL pointer is returned). Otherwise, if an OOM error is encountered
177685** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
177686** no error occurs, *pRc is left unmodified.
177687*/
177688static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){
177689 char *zRet = 0;
177690 char *zFree = 0;
177691 char *zFunction;
177692 int i;
177693
177694 if( !zFunc ){
177695 zFunction = "";
177696 }else{
177697 zFree = zFunction = fts3QuoteId(zFunc);
177698 }
177699 fts3Appendf(pRc, &zRet, "?");
177700 for(i=0; i<p->nColumn; i++){
177701 fts3Appendf(pRc, &zRet, ",%s(?)", zFunction);
177702 }
177703 if( p->zLanguageid ){
177704 fts3Appendf(pRc, &zRet, ", ?");
177705 }
177706 sqlite3_free(zFree);
177707 return zRet;
177708}
177709
177710/*
177711** Buffer z contains a positive integer value encoded as utf-8 text.
177712** Decode this value and store it in *pnOut, returning the number of bytes
177713** consumed. If an overflow error occurs return a negative value.
177714*/
177715SQLITE_PRIVATEstatic int sqlite3Fts3ReadInt(const char *z, int *pnOut){
177716 u64 iVal = 0;
177717 int i;
177718 for(i=0; z[i]>='0' && z[i]<='9'; i++){
177719 iVal = iVal*10 + (z[i] - '0');
177720 if( iVal>0x7FFFFFFF ) return -1;
177721 }
177722 *pnOut = (int)iVal;
177723 return i;
177724}
177725
177726/*
177727** This function interprets the string at (*pp) as a non-negative integer
177728** value. It reads the integer and sets *pnOut to the value read, then
177729** sets *pp to point to the byte immediately following the last byte of
177730** the integer value.
177731**
177732** Only decimal digits ('0'..'9') may be part of an integer value.
177733**
177734** If *pp does not being with a decimal digit SQLITE_ERROR is returned and
177735** the output value undefined. Otherwise SQLITE_OK is returned.
177736**
177737** This function is used when parsing the "prefix=" FTS4 parameter.
177738*/
177739static int fts3GobbleInt(const char **pp, int *pnOut){
177740 const int MAX_NPREFIX = 10000000;
177741 int nInt = 0; /* Output value */
177742 int nByte;
177743 nByte = sqlite3Fts3ReadInt(*pp, &nInt);
177744 if( nInt>MAX_NPREFIX ){
177745 nInt = 0;
177746 }
177747 if( nByte==0 ){
177748 return SQLITE_ERROR1;
177749 }
177750 *pnOut = nInt;
177751 *pp += nByte;
177752 return SQLITE_OK0;
177753}
177754
177755/*
177756** This function is called to allocate an array of Fts3Index structures
177757** representing the indexes maintained by the current FTS table. FTS tables
177758** always maintain the main "terms" index, but may also maintain one or
177759** more "prefix" indexes, depending on the value of the "prefix=" parameter
177760** (if any) specified as part of the CREATE VIRTUAL TABLE statement.
177761**
177762** Argument zParam is passed the value of the "prefix=" option if one was
177763** specified, or NULL otherwise.
177764**
177765** If no error occurs, SQLITE_OK is returned and *apIndex set to point to
177766** the allocated array. *pnIndex is set to the number of elements in the
177767** array. If an error does occur, an SQLite error code is returned.
177768**
177769** Regardless of whether or not an error is returned, it is the responsibility
177770** of the caller to call sqlite3_free() on the output array to free it.
177771*/
177772static int fts3PrefixParameter(
177773 const char *zParam, /* ABC in prefix=ABC parameter to parse */
177774 int *pnIndex, /* OUT: size of *apIndex[] array */
177775 struct Fts3Index **apIndex /* OUT: Array of indexes for this table */
177776){
177777 struct Fts3Index *aIndex; /* Allocated array */
177778 int nIndex = 1; /* Number of entries in array */
177779
177780 if( zParam && zParam[0] ){
177781 const char *p;
177782 nIndex++;
177783 for(p=zParam; *p; p++){
177784 if( *p==',' ) nIndex++;
177785 }
177786 }
177787
177788 aIndex = sqlite3_malloc64(sizeof(struct Fts3Index) * nIndex);
177789 *apIndex = aIndex;
177790 if( !aIndex ){
177791 return SQLITE_NOMEM7;
177792 }
177793
177794 memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex);
177795 if( zParam ){
177796 const char *p = zParam;
177797 int i;
177798 for(i=1; i<nIndex; i++){
177799 int nPrefix = 0;
177800 if( fts3GobbleInt(&p, &nPrefix) ) return SQLITE_ERROR1;
177801 assert( nPrefix>=0 )((void) (0));
177802 if( nPrefix==0 ){
177803 nIndex--;
177804 i--;
177805 }else{
177806 aIndex[i].nPrefix = nPrefix;
177807 }
177808 p++;
177809 }
177810 }
177811
177812 *pnIndex = nIndex;
177813 return SQLITE_OK0;
177814}
177815
177816/*
177817** This function is called when initializing an FTS4 table that uses the
177818** content=xxx option. It determines the number of and names of the columns
177819** of the new FTS4 table.
177820**
177821** The third argument passed to this function is the value passed to the
177822** config=xxx option (i.e. "xxx"). This function queries the database for
177823** a table of that name. If found, the output variables are populated
177824** as follows:
177825**
177826** *pnCol: Set to the number of columns table xxx has,
177827**
177828** *pnStr: Set to the total amount of space required to store a copy
177829** of each columns name, including the nul-terminator.
177830**
177831** *pazCol: Set to point to an array of *pnCol strings. Each string is
177832** the name of the corresponding column in table xxx. The array
177833** and its contents are allocated using a single allocation. It
177834** is the responsibility of the caller to free this allocation
177835** by eventually passing the *pazCol value to sqlite3_free().
177836**
177837** If the table cannot be found, an error code is returned and the output
177838** variables are undefined. Or, if an OOM is encountered, SQLITE_NOMEM is
177839** returned (and the output variables are undefined).
177840*/
177841static int fts3ContentColumns(
177842 sqlite3 *db, /* Database handle */
177843 const char *zDb, /* Name of db (i.e. "main", "temp" etc.) */
177844 const char *zTbl, /* Name of content table */
177845 const char ***pazCol, /* OUT: Malloc'd array of column names */
177846 int *pnCol, /* OUT: Size of array *pazCol */
177847 int *pnStr, /* OUT: Bytes of string content */
177848 char **pzErr /* OUT: error message */
177849){
177850 int rc = SQLITE_OK0; /* Return code */
177851 char *zSql; /* "SELECT *" statement on zTbl */
177852 sqlite3_stmt *pStmt = 0; /* Compiled version of zSql */
177853
177854 zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zTbl);
177855 if( !zSql ){
177856 rc = SQLITE_NOMEM7;
177857 }else{
177858 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
177859 if( rc!=SQLITE_OK0 ){
177860 sqlite3Fts3ErrMsg(pzErr, "%s", sqlite3_errmsg(db));
177861 }
177862 }
177863 sqlite3_free(zSql);
177864
177865 if( rc==SQLITE_OK0 ){
177866 const char **azCol; /* Output array */
177867 sqlite3_int64 nStr = 0; /* Size of all column names (incl. 0x00) */
177868 int nCol; /* Number of table columns */
177869 int i; /* Used to iterate through columns */
177870
177871 /* Loop through the returned columns. Set nStr to the number of bytes of
177872 ** space required to store a copy of each column name, including the
177873 ** nul-terminator byte. */
177874 nCol = sqlite3_column_count(pStmt);
177875 for(i=0; i<nCol; i++){
177876 const char *zCol = sqlite3_column_name(pStmt, i);
177877 nStr += strlen(zCol) + 1;
177878 }
177879
177880 /* Allocate and populate the array to return. */
177881 azCol = (const char **)sqlite3_malloc64(sizeof(char *) * nCol + nStr);
177882 if( azCol==0 ){
177883 rc = SQLITE_NOMEM7;
177884 }else{
177885 char *p = (char *)&azCol[nCol];
177886 for(i=0; i<nCol; i++){
177887 const char *zCol = sqlite3_column_name(pStmt, i);
177888 int n = (int)strlen(zCol)+1;
177889 memcpy(p, zCol, n);
177890 azCol[i] = p;
177891 p += n;
177892 }
177893 }
177894 sqlite3_finalize(pStmt);
177895
177896 /* Set the output variables. */
177897 *pnCol = nCol;
177898 *pnStr = nStr;
177899 *pazCol = azCol;
177900 }
177901
177902 return rc;
177903}
177904
177905/*
177906** This function is the implementation of both the xConnect and xCreate
177907** methods of the FTS3 virtual table.
177908**
177909** The argv[] array contains the following:
177910**
177911** argv[0] -> module name ("fts3" or "fts4")
177912** argv[1] -> database name
177913** argv[2] -> table name
177914** argv[...] -> "column name" and other module argument fields.
177915*/
177916static int fts3InitVtab(
177917 int isCreate, /* True for xCreate, false for xConnect */
177918 sqlite3 *db, /* The SQLite database connection */
177919 void *pAux, /* Hash table containing tokenizers */
177920 int argc, /* Number of elements in argv array */
177921 const char * const *argv, /* xCreate/xConnect argument array */
177922 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
177923 char **pzErr /* Write any error message here */
177924){
177925 Fts3Hash *pHash = &((Fts3HashWrapper*)pAux)->hash;
177926 Fts3Table *p = 0; /* Pointer to allocated vtab */
177927 int rc = SQLITE_OK0; /* Return code */
177928 int i; /* Iterator variable */
177929 sqlite3_int64 nByte; /* Size of allocation used for *p */
177930 int iCol; /* Column index */
177931 int nString = 0; /* Bytes required to hold all column names */
177932 int nCol = 0; /* Number of columns in the FTS table */
177933 char *zCsr; /* Space for holding column names */
177934 int nDb; /* Bytes required to hold database name */
177935 int nName; /* Bytes required to hold table name */
177936 int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
177937 const char **aCol; /* Array of column names */
177938 sqlite3_tokenizer *pTokenizer = 0; /* Tokenizer for this table */
177939
177940 int nIndex = 0; /* Size of aIndex[] array */
177941 struct Fts3Index *aIndex = 0; /* Array of indexes for this table */
177942
177943 /* The results of parsing supported FTS4 key=value options: */
177944 int bNoDocsize = 0; /* True to omit %_docsize table */
177945 int bDescIdx = 0; /* True to store descending indexes */
177946 char *zPrefix = 0; /* Prefix parameter value (or NULL) */
177947 char *zCompress = 0; /* compress=? parameter (or NULL) */
177948 char *zUncompress = 0; /* uncompress=? parameter (or NULL) */
177949 char *zContent = 0; /* content=? parameter (or NULL) */
177950 char *zLanguageid = 0; /* languageid=? parameter (or NULL) */
177951 char **azNotindexed = 0; /* The set of notindexed= columns */
177952 int nNotindexed = 0; /* Size of azNotindexed[] array */
177953
177954 assert( strlen(argv[0])==4 )((void) (0));
177955 assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)((void) (0))
177956 || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)((void) (0))
177957 )((void) (0));
177958
177959 nDb = (int)strlen(argv[1]) + 1;
177960 nName = (int)strlen(argv[2]) + 1;
177961
177962 nByte = sizeof(const char *) * (argc-2);
177963 aCol = (const char **)sqlite3_malloc64(nByte);
177964 if( aCol ){
177965 memset((void*)aCol, 0, nByte);
177966 azNotindexed = (char **)sqlite3_malloc64(nByte);
177967 }
177968 if( azNotindexed ){
177969 memset(azNotindexed, 0, nByte);
177970 }
177971 if( !aCol || !azNotindexed ){
177972 rc = SQLITE_NOMEM7;
177973 goto fts3_init_out;
177974 }
177975
177976 /* Loop through all of the arguments passed by the user to the FTS3/4
177977 ** module (i.e. all the column names and special arguments). This loop
177978 ** does the following:
177979 **
177980 ** + Figures out the number of columns the FTSX table will have, and
177981 ** the number of bytes of space that must be allocated to store copies
177982 ** of the column names.
177983 **
177984 ** + If there is a tokenizer specification included in the arguments,
177985 ** initializes the tokenizer pTokenizer.
177986 */
177987 for(i=3; rc==SQLITE_OK0 && i<argc; i++){
177988 char const *z = argv[i];
177989 int nKey;
177990 char *zVal;
177991
177992 /* Check if this is a tokenizer specification */
177993 if( !pTokenizer
177994 && strlen(z)>8
177995 && 0==sqlite3_strnicmp(z, "tokenize", 8)
177996 && 0==sqlite3Fts3IsIdChar(z[8])
177997 ){
177998 rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr);
177999 }
178000
178001 /* Check if it is an FTS4 special argument. */
178002 else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){
178003 struct Fts4Option {
178004 const char *zOpt;
178005 int nOpt;
178006 } aFts4Opt[] = {
178007 { "matchinfo", 9 }, /* 0 -> MATCHINFO */
178008 { "prefix", 6 }, /* 1 -> PREFIX */
178009 { "compress", 8 }, /* 2 -> COMPRESS */
178010 { "uncompress", 10 }, /* 3 -> UNCOMPRESS */
178011 { "order", 5 }, /* 4 -> ORDER */
178012 { "content", 7 }, /* 5 -> CONTENT */
178013 { "languageid", 10 }, /* 6 -> LANGUAGEID */
178014 { "notindexed", 10 } /* 7 -> NOTINDEXED */
178015 };
178016
178017 int iOpt;
178018 if( !zVal ){
178019 rc = SQLITE_NOMEM7;
178020 }else{
178021 for(iOpt=0; iOpt<SizeofArray(aFts4Opt)((int)(sizeof(aFts4Opt)/sizeof(aFts4Opt[0]))); iOpt++){
178022 struct Fts4Option *pOp = &aFts4Opt[iOpt];
178023 if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){
178024 break;
178025 }
178026 }
178027 switch( iOpt ){
178028 case 0: /* MATCHINFO */
178029 if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){
178030 sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo: %s", zVal);
178031 rc = SQLITE_ERROR1;
178032 }
178033 bNoDocsize = 1;
178034 break;
178035
178036 case 1: /* PREFIX */
178037 sqlite3_free(zPrefix);
178038 zPrefix = zVal;
178039 zVal = 0;
178040 break;
178041
178042 case 2: /* COMPRESS */
178043 sqlite3_free(zCompress);
178044 zCompress = zVal;
178045 zVal = 0;
178046 break;
178047
178048 case 3: /* UNCOMPRESS */
178049 sqlite3_free(zUncompress);
178050 zUncompress = zVal;
178051 zVal = 0;
178052 break;
178053
178054 case 4: /* ORDER */
178055 if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3))
178056 && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4))
178057 ){
178058 sqlite3Fts3ErrMsg(pzErr, "unrecognized order: %s", zVal);
178059 rc = SQLITE_ERROR1;
178060 }
178061 bDescIdx = (zVal[0]=='d' || zVal[0]=='D');
178062 break;
178063
178064 case 5: /* CONTENT */
178065 sqlite3_free(zContent);
178066 zContent = zVal;
178067 zVal = 0;
178068 break;
178069
178070 case 6: /* LANGUAGEID */
178071 assert( iOpt==6 )((void) (0));
178072 sqlite3_free(zLanguageid);
178073 zLanguageid = zVal;
178074 zVal = 0;
178075 break;
178076
178077 case 7: /* NOTINDEXED */
178078 azNotindexed[nNotindexed++] = zVal;
178079 zVal = 0;
178080 break;
178081
178082 default:
178083 assert( iOpt==SizeofArray(aFts4Opt) )((void) (0));
178084 sqlite3Fts3ErrMsg(pzErr, "unrecognized parameter: %s", z);
178085 rc = SQLITE_ERROR1;
178086 break;
178087 }
178088 sqlite3_free(zVal);
178089 }
178090 }
178091
178092 /* Otherwise, the argument is a column name. */
178093 else {
178094 nString += (int)(strlen(z) + 1);
178095 aCol[nCol++] = z;
178096 }
178097 }
178098
178099 /* If a content=xxx option was specified, the following:
178100 **
178101 ** 1. Ignore any compress= and uncompress= options.
178102 **
178103 ** 2. If no column names were specified as part of the CREATE VIRTUAL
178104 ** TABLE statement, use all columns from the content table.
178105 */
178106 if( rc==SQLITE_OK0 && zContent ){
178107 sqlite3_free(zCompress);
178108 sqlite3_free(zUncompress);
178109 zCompress = 0;
178110 zUncompress = 0;
178111 if( nCol==0 ){
178112 sqlite3_free((void*)aCol);
178113 aCol = 0;
178114 rc = fts3ContentColumns(db, argv[1], zContent,&aCol,&nCol,&nString,pzErr);
178115
178116 /* If a languageid= option was specified, remove the language id
178117 ** column from the aCol[] array. */
178118 if( rc==SQLITE_OK0 && zLanguageid ){
178119 int j;
178120 for(j=0; j<nCol; j++){
178121 if( sqlite3_stricmp(zLanguageid, aCol[j])==0 ){
178122 int k;
178123 for(k=j; k<nCol; k++) aCol[k] = aCol[k+1];
178124 nCol--;
178125 break;
178126 }
178127 }
178128 }
178129 }
178130 }
178131 if( rc!=SQLITE_OK0 ) goto fts3_init_out;
178132
178133 if( nCol==0 ){
178134 assert( nString==0 )((void) (0));
178135 aCol[0] = "content";
178136 nString = 8;
178137 nCol = 1;
178138 }
178139
178140 if( pTokenizer==0 ){
178141 rc = sqlite3Fts3InitTokenizer(pHash, "simple", &pTokenizer, pzErr);
178142 if( rc!=SQLITE_OK0 ) goto fts3_init_out;
178143 }
178144 assert( pTokenizer )((void) (0));
178145
178146 rc = fts3PrefixParameter(zPrefix, &nIndex, &aIndex);
178147 if( rc==SQLITE_ERROR1 ){
178148 assert( zPrefix )((void) (0));
178149 sqlite3Fts3ErrMsg(pzErr, "error parsing prefix parameter: %s", zPrefix);
178150 }
178151 if( rc!=SQLITE_OK0 ) goto fts3_init_out;
178152
178153 /* Allocate and populate the Fts3Table structure. */
178154 nByte = sizeof(Fts3Table) + /* Fts3Table */
178155 nCol * sizeof(char *) + /* azColumn */
178156 nIndex * sizeof(struct Fts3Index) + /* aIndex */
178157 nCol * sizeof(u8) + /* abNotindexed */
178158 nName + /* zName */
178159 nDb + /* zDb */
178160 nString; /* Space for azColumn strings */
178161 p = (Fts3Table*)sqlite3_malloc64(nByte);
178162 if( p==0 ){
178163 rc = SQLITE_NOMEM7;
178164 goto fts3_init_out;
178165 }
178166 memset(p, 0, nByte);
178167 p->db = db;
178168 p->nColumn = nCol;
178169 p->nPendingData = 0;
178170 p->azColumn = (char **)&p[1];
178171 p->pTokenizer = pTokenizer;
178172 p->nMaxPendingData = FTS3_MAX_PENDING_DATA(1*1024*1024);
178173 p->bHasDocsize = (isFts4 && bNoDocsize==0);
178174 p->bHasStat = (u8)isFts4;
178175 p->bFts4 = (u8)isFts4;
178176 p->bDescIdx = (u8)bDescIdx;
178177 p->nAutoincrmerge = 0xff; /* 0xff means setting unknown */
178178 p->zContentTbl = zContent;
178179 p->zLanguageid = zLanguageid;
178180 zContent = 0;
178181 zLanguageid = 0;
178182 TESTONLY( p->inTransaction = -1 );
178183 TESTONLY( p->mxSavepoint = -1 );
178184
178185 p->aIndex = (struct Fts3Index *)&p->azColumn[nCol];
178186 memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex);
178187 p->nIndex = nIndex;
178188 for(i=0; i<nIndex; i++){
178189 fts3HashInitsqlite3Fts3HashInit(&p->aIndex[i].hPending, FTS3_HASH_STRING1, 1);
178190 }
178191 p->abNotindexed = (u8 *)&p->aIndex[nIndex];
178192
178193 /* Fill in the zName and zDb fields of the vtab structure. */
178194 zCsr = (char *)&p->abNotindexed[nCol];
178195 p->zName = zCsr;
178196 memcpy(zCsr, argv[2], nName);
178197 zCsr += nName;
178198 p->zDb = zCsr;
178199 memcpy(zCsr, argv[1], nDb);
178200 zCsr += nDb;
178201
178202 /* Fill in the azColumn array */
178203 for(iCol=0; iCol<nCol; iCol++){
178204 char *z;
178205 int n = 0;
178206 z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
178207 if( n>0 ){
178208 memcpy(zCsr, z, n);
178209 }
178210 zCsr[n] = '\0';
178211 sqlite3Fts3Dequote(zCsr);
178212 p->azColumn[iCol] = zCsr;
178213 zCsr += n+1;
178214 assert( zCsr <= &((char *)p)[nByte] )((void) (0));
178215 }
178216
178217 /* Fill in the abNotindexed array */
178218 for(iCol=0; iCol<nCol; iCol++){
178219 int n = (int)strlen(p->azColumn[iCol]);
178220 for(i=0; i<nNotindexed; i++){
178221 char *zNot = azNotindexed[i];
178222 if( zNot && n==(int)strlen(zNot)
178223 && 0==sqlite3_strnicmp(p->azColumn[iCol], zNot, n)
178224 ){
178225 p->abNotindexed[iCol] = 1;
178226 sqlite3_free(zNot);
178227 azNotindexed[i] = 0;
178228 }
178229 }
178230 }
178231 for(i=0; i<nNotindexed; i++){
178232 if( azNotindexed[i] ){
178233 sqlite3Fts3ErrMsg(pzErr, "no such column: %s", azNotindexed[i]);
178234 rc = SQLITE_ERROR1;
178235 }
178236 }
178237
178238 if( rc==SQLITE_OK0 && (zCompress==0)!=(zUncompress==0) ){
178239 char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
178240 rc = SQLITE_ERROR1;
178241 sqlite3Fts3ErrMsg(pzErr, "missing %s parameter in fts4 constructor", zMiss);
178242 }
178243 p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
178244 p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
178245 if( rc!=SQLITE_OK0 ) goto fts3_init_out;
178246
178247 /* If this is an xCreate call, create the underlying tables in the
178248 ** database. TODO: For xConnect(), it could verify that said tables exist.
178249 */
178250 if( isCreate ){
178251 rc = fts3CreateTables(p);
178252 }
178253
178254 /* Check to see if a legacy fts3 table has been "upgraded" by the
178255 ** addition of a %_stat table so that it can use incremental merge.
178256 */
178257 if( !isFts4 && !isCreate ){
178258 p->bHasStat = 2;
178259 }
178260
178261 /* Figure out the page-size for the database. This is required in order to
178262 ** estimate the cost of loading large doclists from the database. */
178263 fts3DatabasePageSize(&rc, p);
178264 p->nNodeSize = p->nPgsz-35;
178265
178266#if defined(SQLITE_DEBUG)||defined(SQLITE_TEST)
178267 p->nMergeCount = FTS3_MERGE_COUNT16;
178268#endif
178269
178270 /* Declare the table schema to SQLite. */
178271 fts3DeclareVtab(&rc, p);
178272
178273fts3_init_out:
178274 sqlite3_free(zPrefix);
178275 sqlite3_free(aIndex);
178276 sqlite3_free(zCompress);
178277 sqlite3_free(zUncompress);
178278 sqlite3_free(zContent);
178279 sqlite3_free(zLanguageid);
178280 for(i=0; i<nNotindexed; i++) sqlite3_free(azNotindexed[i]);
178281 sqlite3_free((void *)aCol);
178282 sqlite3_free((void *)azNotindexed);
178283 if( rc!=SQLITE_OK0 ){
178284 if( p ){
178285 fts3DisconnectMethod((sqlite3_vtab *)p);
178286 }else if( pTokenizer ){
178287 pTokenizer->pModule->xDestroy(pTokenizer);
178288 }
178289 }else{
178290 assert( p->pSegments==0 )((void) (0));
178291 *ppVTab = &p->base;
178292 }
178293 return rc;
178294}
178295
178296/*
178297** The xConnect() and xCreate() methods for the virtual table. All the
178298** work is done in function fts3InitVtab().
178299*/
178300static int fts3ConnectMethod(
178301 sqlite3 *db, /* Database connection */
178302 void *pAux, /* Pointer to tokenizer hash table */
178303 int argc, /* Number of elements in argv array */
178304 const char * const *argv, /* xCreate/xConnect argument array */
178305 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
178306 char **pzErr /* OUT: sqlite3_malloc'd error message */
178307){
178308 return fts3InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
178309}
178310static int fts3CreateMethod(
178311 sqlite3 *db, /* Database connection */
178312 void *pAux, /* Pointer to tokenizer hash table */
178313 int argc, /* Number of elements in argv array */
178314 const char * const *argv, /* xCreate/xConnect argument array */
178315 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
178316 char **pzErr /* OUT: sqlite3_malloc'd error message */
178317){
178318 return fts3InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
178319}
178320
178321/*
178322** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
178323** extension is currently being used by a version of SQLite too old to
178324** support estimatedRows. In that case this function is a no-op.
178325*/
178326static void fts3SetEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
178327#if SQLITE_VERSION_NUMBER3039002>=3008002
178328 if( sqlite3_libversion_number()>=3008002 ){
178329 pIdxInfo->estimatedRows = nRow;
178330 }
178331#endif
178332}
178333
178334/*
178335** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
178336** extension is currently being used by a version of SQLite too old to
178337** support index-info flags. In that case this function is a no-op.
178338*/
178339static void fts3SetUniqueFlag(sqlite3_index_info *pIdxInfo){
178340#if SQLITE_VERSION_NUMBER3039002>=3008012
178341 if( sqlite3_libversion_number()>=3008012 ){
178342 pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE1;
178343 }
178344#endif
178345}
178346
178347/*
178348** Implementation of the xBestIndex method for FTS3 tables. There
178349** are three possible strategies, in order of preference:
178350**
178351** 1. Direct lookup by rowid or docid.
178352** 2. Full-text search using a MATCH operator on a non-docid column.
178353** 3. Linear scan of %_content table.
178354*/
178355static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
178356 Fts3Table *p = (Fts3Table *)pVTab;
178357 int i; /* Iterator variable */
178358 int iCons = -1; /* Index of constraint to use */
178359
178360 int iLangidCons = -1; /* Index of langid=x constraint, if present */
178361 int iDocidGe = -1; /* Index of docid>=x constraint, if present */
178362 int iDocidLe = -1; /* Index of docid<=x constraint, if present */
178363 int iIdx;
178364
178365 if( p->bLock ){
178366 return SQLITE_ERROR1;
178367 }
178368
178369 /* By default use a full table scan. This is an expensive option,
178370 ** so search through the constraints to see if a more efficient
178371 ** strategy is possible.
178372 */
178373 pInfo->idxNum = FTS3_FULLSCAN_SEARCH0;
178374 pInfo->estimatedCost = 5000000;
178375 for(i=0; i<pInfo->nConstraint; i++){
178376 int bDocid; /* True if this constraint is on docid */
178377 struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
178378 if( pCons->usable==0 ){
178379 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH64 ){
178380 /* There exists an unusable MATCH constraint. This means that if
178381 ** the planner does elect to use the results of this call as part
178382 ** of the overall query plan the user will see an "unable to use
178383 ** function MATCH in the requested context" error. To discourage
178384 ** this, return a very high cost here. */
178385 pInfo->idxNum = FTS3_FULLSCAN_SEARCH0;
178386 pInfo->estimatedCost = 1e50;
178387 fts3SetEstimatedRows(pInfo, ((sqlite3_int64)1) << 50);
178388 return SQLITE_OK0;
178389 }
178390 continue;
178391 }
178392
178393 bDocid = (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1);
178394
178395 /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */
178396 if( iCons<0 && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ2 && bDocid ){
178397 pInfo->idxNum = FTS3_DOCID_SEARCH1;
178398 pInfo->estimatedCost = 1.0;
178399 iCons = i;
178400 }
178401
178402 /* A MATCH constraint. Use a full-text search.
178403 **
178404 ** If there is more than one MATCH constraint available, use the first
178405 ** one encountered. If there is both a MATCH constraint and a direct
178406 ** rowid/docid lookup, prefer the MATCH strategy. This is done even
178407 ** though the rowid/docid lookup is faster than a MATCH query, selecting
178408 ** it would lead to an "unable to use function MATCH in the requested
178409 ** context" error.
178410 */
178411 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH64
178412 && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
178413 ){
178414 pInfo->idxNum = FTS3_FULLTEXT_SEARCH2 + pCons->iColumn;
178415 pInfo->estimatedCost = 2.0;
178416 iCons = i;
178417 }
178418
178419 /* Equality constraint on the langid column */
178420 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ2
178421 && pCons->iColumn==p->nColumn + 2
178422 ){
178423 iLangidCons = i;
178424 }
178425
178426 if( bDocid ){
178427 switch( pCons->op ){
178428 case SQLITE_INDEX_CONSTRAINT_GE32:
178429 case SQLITE_INDEX_CONSTRAINT_GT4:
178430 iDocidGe = i;
178431 break;
178432
178433 case SQLITE_INDEX_CONSTRAINT_LE8:
178434 case SQLITE_INDEX_CONSTRAINT_LT16:
178435 iDocidLe = i;
178436 break;
178437 }
178438 }
178439 }
178440
178441 /* If using a docid=? or rowid=? strategy, set the UNIQUE flag. */
178442 if( pInfo->idxNum==FTS3_DOCID_SEARCH1 ) fts3SetUniqueFlag(pInfo);
178443
178444 iIdx = 1;
178445 if( iCons>=0 ){
178446 pInfo->aConstraintUsage[iCons].argvIndex = iIdx++;
178447 pInfo->aConstraintUsage[iCons].omit = 1;
178448 }
178449 if( iLangidCons>=0 ){
178450 pInfo->idxNum |= FTS3_HAVE_LANGID0x00010000;
178451 pInfo->aConstraintUsage[iLangidCons].argvIndex = iIdx++;
178452 }
178453 if( iDocidGe>=0 ){
178454 pInfo->idxNum |= FTS3_HAVE_DOCID_GE0x00020000;
178455 pInfo->aConstraintUsage[iDocidGe].argvIndex = iIdx++;
178456 }
178457 if( iDocidLe>=0 ){
178458 pInfo->idxNum |= FTS3_HAVE_DOCID_LE0x00040000;
178459 pInfo->aConstraintUsage[iDocidLe].argvIndex = iIdx++;
178460 }
178461
178462 /* Regardless of the strategy selected, FTS can deliver rows in rowid (or
178463 ** docid) order. Both ascending and descending are possible.
178464 */
178465 if( pInfo->nOrderBy==1 ){
178466 struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0];
178467 if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){
178468 if( pOrder->desc ){
178469 pInfo->idxStr = "DESC";
178470 }else{
178471 pInfo->idxStr = "ASC";
178472 }
178473 pInfo->orderByConsumed = 1;
178474 }
178475 }
178476
178477 assert( p->pSegments==0 )((void) (0));
178478 return SQLITE_OK0;
178479}
178480
178481/*
178482** Implementation of xOpen method.
178483*/
178484static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
178485 sqlite3_vtab_cursor *pCsr; /* Allocated cursor */
178486
178487 UNUSED_PARAMETER(pVTab)(void)(pVTab);
178488
178489 /* Allocate a buffer large enough for an Fts3Cursor structure. If the
178490 ** allocation succeeds, zero it and return SQLITE_OK. Otherwise,
178491 ** if the allocation fails, return SQLITE_NOMEM.
178492 */
178493 *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor));
178494 if( !pCsr ){
178495 return SQLITE_NOMEM7;
178496 }
178497 memset(pCsr, 0, sizeof(Fts3Cursor));
178498 return SQLITE_OK0;
178499}
178500
178501/*
178502** Finalize the statement handle at pCsr->pStmt.
178503**
178504** Or, if that statement handle is one created by fts3CursorSeekStmt(),
178505** and the Fts3Table.pSeekStmt slot is currently NULL, save the statement
178506** pointer there instead of finalizing it.
178507*/
178508static void fts3CursorFinalizeStmt(Fts3Cursor *pCsr){
178509 if( pCsr->bSeekStmt ){
178510 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
178511 if( p->pSeekStmt==0 ){
178512 p->pSeekStmt = pCsr->pStmt;
178513 sqlite3_reset(pCsr->pStmt);
178514 pCsr->pStmt = 0;
178515 }
178516 pCsr->bSeekStmt = 0;
178517 }
178518 sqlite3_finalize(pCsr->pStmt);
178519}
178520
178521/*
178522** Free all resources currently held by the cursor passed as the only
178523** argument.
178524*/
178525static void fts3ClearCursor(Fts3Cursor *pCsr){
178526 fts3CursorFinalizeStmt(pCsr);
178527 sqlite3Fts3FreeDeferredTokens(pCsr);
178528 sqlite3_free(pCsr->aDoclist);
178529 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
178530 sqlite3Fts3ExprFree(pCsr->pExpr);
178531 memset(&(&pCsr->base)[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
178532}
178533
178534/*
178535** Close the cursor. For additional information see the documentation
178536** on the xClose method of the virtual table interface.
178537*/
178538static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
178539 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
178540 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 )((void) (0));
178541 fts3ClearCursor(pCsr);
178542 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 )((void) (0));
178543 sqlite3_free(pCsr);
178544 return SQLITE_OK0;
178545}
178546
178547/*
178548** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
178549** compose and prepare an SQL statement of the form:
178550**
178551** "SELECT <columns> FROM %_content WHERE rowid = ?"
178552**
178553** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
178554** it. If an error occurs, return an SQLite error code.
178555*/
178556static int fts3CursorSeekStmt(Fts3Cursor *pCsr){
178557 int rc = SQLITE_OK0;
178558 if( pCsr->pStmt==0 ){
178559 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
178560 char *zSql;
178561 if( p->pSeekStmt ){
178562 pCsr->pStmt = p->pSeekStmt;
178563 p->pSeekStmt = 0;
178564 }else{
178565 zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
178566 if( !zSql ) return SQLITE_NOMEM7;
178567 p->bLock++;
178568 rc = sqlite3_prepare_v3(
178569 p->db, zSql,-1,SQLITE_PREPARE_PERSISTENT0x01,&pCsr->pStmt,0
178570 );
178571 p->bLock--;
178572 sqlite3_free(zSql);
178573 }
178574 if( rc==SQLITE_OK0 ) pCsr->bSeekStmt = 1;
178575 }
178576 return rc;
178577}
178578
178579/*
178580** Position the pCsr->pStmt statement so that it is on the row
178581** of the %_content table that contains the last match. Return
178582** SQLITE_OK on success.
178583*/
178584static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
178585 int rc = SQLITE_OK0;
178586 if( pCsr->isRequireSeek ){
178587 rc = fts3CursorSeekStmt(pCsr);
178588 if( rc==SQLITE_OK0 ){
178589 Fts3Table *pTab = (Fts3Table*)pCsr->base.pVtab;
178590 pTab->bLock++;
178591 sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
178592 pCsr->isRequireSeek = 0;
178593 if( SQLITE_ROW100==sqlite3_step(pCsr->pStmt) ){
178594 pTab->bLock--;
178595 return SQLITE_OK0;
178596 }else{
178597 pTab->bLock--;
178598 rc = sqlite3_reset(pCsr->pStmt);
178599 if( rc==SQLITE_OK0 && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){
178600 /* If no row was found and no error has occurred, then the %_content
178601 ** table is missing a row that is present in the full-text index.
178602 ** The data structures are corrupt. */
178603 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
178604 pCsr->isEof = 1;
178605 }
178606 }
178607 }
178608 }
178609
178610 if( rc!=SQLITE_OK0 && pContext ){
178611 sqlite3_result_error_code(pContext, rc);
178612 }
178613 return rc;
178614}
178615
178616/*
178617** This function is used to process a single interior node when searching
178618** a b-tree for a term or term prefix. The node data is passed to this
178619** function via the zNode/nNode parameters. The term to search for is
178620** passed in zTerm/nTerm.
178621**
178622** If piFirst is not NULL, then this function sets *piFirst to the blockid
178623** of the child node that heads the sub-tree that may contain the term.
178624**
178625** If piLast is not NULL, then *piLast is set to the right-most child node
178626** that heads a sub-tree that may contain a term for which zTerm/nTerm is
178627** a prefix.
178628**
178629** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
178630*/
178631static int fts3ScanInteriorNode(
178632 const char *zTerm, /* Term to select leaves for */
178633 int nTerm, /* Size of term zTerm in bytes */
178634 const char *zNode, /* Buffer containing segment interior node */
178635 int nNode, /* Size of buffer at zNode */
178636 sqlite3_int64 *piFirst, /* OUT: Selected child node */
178637 sqlite3_int64 *piLast /* OUT: Selected child node */
178638){
178639 int rc = SQLITE_OK0; /* Return code */
178640 const char *zCsr = zNode; /* Cursor to iterate through node */
178641 const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
178642 char *zBuffer = 0; /* Buffer to load terms into */
178643 i64 nAlloc = 0; /* Size of allocated buffer */
178644 int isFirstTerm = 1; /* True when processing first term on page */
178645 u64 iChild; /* Block id of child node to descend to */
178646 int nBuffer = 0; /* Total term size */
178647
178648 /* Skip over the 'height' varint that occurs at the start of every
178649 ** interior node. Then load the blockid of the left-child of the b-tree
178650 ** node into variable iChild.
178651 **
178652 ** Even if the data structure on disk is corrupted, this (reading two
178653 ** varints from the buffer) does not risk an overread. If zNode is a
178654 ** root node, then the buffer comes from a SELECT statement. SQLite does
178655 ** not make this guarantee explicitly, but in practice there are always
178656 ** either more than 20 bytes of allocated space following the nNode bytes of
178657 ** contents, or two zero bytes. Or, if the node is read from the %_segments
178658 ** table, then there are always 20 bytes of zeroed padding following the
178659 ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details).
178660 */
178661 zCsr += sqlite3Fts3GetVarintU(zCsr, &iChild);
178662 zCsr += sqlite3Fts3GetVarintU(zCsr, &iChild);
178663 if( zCsr>zEnd ){
178664 return FTS_CORRUPT_VTAB(11 | (1<<8));
178665 }
178666
178667 while( zCsr<zEnd && (piFirst || piLast) ){
178668 int cmp; /* memcmp() result */
178669 int nSuffix; /* Size of term suffix */
178670 int nPrefix = 0; /* Size of term prefix */
178671
178672 /* Load the next term on the node into zBuffer. Use realloc() to expand
178673 ** the size of zBuffer if required. */
178674 if( !isFirstTerm ){
178675 zCsr += fts3GetVarint32(zCsr, &nPrefix)( (*(u8*)(zCsr)&0x80) ? sqlite3Fts3GetVarint32(zCsr, &
nPrefix) : (*&nPrefix=*(u8*)(zCsr), 1) )
;
178676 if( nPrefix>nBuffer ){
178677 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
178678 goto finish_scan;
178679 }
178680 }
178681 isFirstTerm = 0;
178682 zCsr += fts3GetVarint32(zCsr, &nSuffix)( (*(u8*)(zCsr)&0x80) ? sqlite3Fts3GetVarint32(zCsr, &
nSuffix) : (*&nSuffix=*(u8*)(zCsr), 1) )
;
178683
178684 assert( nPrefix>=0 && nSuffix>=0 )((void) (0));
178685 if( nPrefix>zCsr-zNode || nSuffix>zEnd-zCsr || nSuffix==0 ){
178686 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
178687 goto finish_scan;
178688 }
178689 if( (i64)nPrefix+nSuffix>nAlloc ){
178690 char *zNew;
178691 nAlloc = ((i64)nPrefix+nSuffix) * 2;
178692 zNew = (char *)sqlite3_realloc64(zBuffer, nAlloc);
178693 if( !zNew ){
178694 rc = SQLITE_NOMEM7;
178695 goto finish_scan;
178696 }
178697 zBuffer = zNew;
178698 }
178699 assert( zBuffer )((void) (0));
178700 memcpy(&zBuffer[nPrefix], zCsr, nSuffix);
178701 nBuffer = nPrefix + nSuffix;
178702 zCsr += nSuffix;
178703
178704 /* Compare the term we are searching for with the term just loaded from
178705 ** the interior node. If the specified term is greater than or equal
178706 ** to the term from the interior node, then all terms on the sub-tree
178707 ** headed by node iChild are smaller than zTerm. No need to search
178708 ** iChild.
178709 **
178710 ** If the interior node term is larger than the specified term, then
178711 ** the tree headed by iChild may contain the specified term.
178712 */
178713 cmp = memcmp(zTerm, zBuffer, (nBuffer>nTerm ? nTerm : nBuffer));
178714 if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){
178715 *piFirst = (i64)iChild;
178716 piFirst = 0;
178717 }
178718
178719 if( piLast && cmp<0 ){
178720 *piLast = (i64)iChild;
178721 piLast = 0;
178722 }
178723
178724 iChild++;
178725 };
178726
178727 if( piFirst ) *piFirst = (i64)iChild;
178728 if( piLast ) *piLast = (i64)iChild;
178729
178730 finish_scan:
178731 sqlite3_free(zBuffer);
178732 return rc;
178733}
178734
178735
178736/*
178737** The buffer pointed to by argument zNode (size nNode bytes) contains an
178738** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes)
178739** contains a term. This function searches the sub-tree headed by the zNode
178740** node for the range of leaf nodes that may contain the specified term
178741** or terms for which the specified term is a prefix.
178742**
178743** If piLeaf is not NULL, then *piLeaf is set to the blockid of the
178744** left-most leaf node in the tree that may contain the specified term.
178745** If piLeaf2 is not NULL, then *piLeaf2 is set to the blockid of the
178746** right-most leaf node that may contain a term for which the specified
178747** term is a prefix.
178748**
178749** It is possible that the range of returned leaf nodes does not contain
178750** the specified term or any terms for which it is a prefix. However, if the
178751** segment does contain any such terms, they are stored within the identified
178752** range. Because this function only inspects interior segment nodes (and
178753** never loads leaf nodes into memory), it is not possible to be sure.
178754**
178755** If an error occurs, an error code other than SQLITE_OK is returned.
178756*/
178757static int fts3SelectLeaf(
178758 Fts3Table *p, /* Virtual table handle */
178759 const char *zTerm, /* Term to select leaves for */
178760 int nTerm, /* Size of term zTerm in bytes */
178761 const char *zNode, /* Buffer containing segment interior node */
178762 int nNode, /* Size of buffer at zNode */
178763 sqlite3_int64 *piLeaf, /* Selected leaf node */
178764 sqlite3_int64 *piLeaf2 /* Selected leaf node */
178765){
178766 int rc = SQLITE_OK0; /* Return code */
178767 int iHeight; /* Height of this node in tree */
178768
178769 assert( piLeaf || piLeaf2 )((void) (0));
178770
178771 fts3GetVarint32(zNode, &iHeight)( (*(u8*)(zNode)&0x80) ? sqlite3Fts3GetVarint32(zNode, &
iHeight) : (*&iHeight=*(u8*)(zNode), 1) )
;
178772 rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2);
178773 assert_fts3_nc( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) )((void) (0));
178774
178775 if( rc==SQLITE_OK0 && iHeight>1 ){
178776 char *zBlob = 0; /* Blob read from %_segments table */
178777 int nBlob = 0; /* Size of zBlob in bytes */
178778
178779 if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){
178780 rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob, 0);
178781 if( rc==SQLITE_OK0 ){
178782 rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, 0);
178783 }
178784 sqlite3_free(zBlob);
178785 piLeaf = 0;
178786 zBlob = 0;
178787 }
178788
178789 if( rc==SQLITE_OK0 ){
178790 rc = sqlite3Fts3ReadBlock(p, piLeaf?*piLeaf:*piLeaf2, &zBlob, &nBlob, 0);
178791 }
178792 if( rc==SQLITE_OK0 ){
178793 int iNewHeight = 0;
178794 fts3GetVarint32(zBlob, &iNewHeight)( (*(u8*)(zBlob)&0x80) ? sqlite3Fts3GetVarint32(zBlob, &
iNewHeight) : (*&iNewHeight=*(u8*)(zBlob), 1) )
;
178795 if( iNewHeight>=iHeight ){
178796 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
178797 }else{
178798 rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2);
178799 }
178800 }
178801 sqlite3_free(zBlob);
178802 }
178803
178804 return rc;
178805}
178806
178807/*
178808** This function is used to create delta-encoded serialized lists of FTS3
178809** varints. Each call to this function appends a single varint to a list.
178810*/
178811static void fts3PutDeltaVarint(
178812 char **pp, /* IN/OUT: Output pointer */
178813 sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */
178814 sqlite3_int64 iVal /* Write this value to the list */
178815){
178816 assert_fts3_nc( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) )((void) (0));
178817 *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev);
178818 *piPrev = iVal;
178819}
178820
178821/*
178822** When this function is called, *ppPoslist is assumed to point to the
178823** start of a position-list. After it returns, *ppPoslist points to the
178824** first byte after the position-list.
178825**
178826** A position list is list of positions (delta encoded) and columns for
178827** a single document record of a doclist. So, in other words, this
178828** routine advances *ppPoslist so that it points to the next docid in
178829** the doclist, or to the first byte past the end of the doclist.
178830**
178831** If pp is not NULL, then the contents of the position list are copied
178832** to *pp. *pp is set to point to the first byte past the last byte copied
178833** before this function returns.
178834*/
178835static void fts3PoslistCopy(char **pp, char **ppPoslist){
178836 char *pEnd = *ppPoslist;
178837 char c = 0;
178838
178839 /* The end of a position list is marked by a zero encoded as an FTS3
178840 ** varint. A single POS_END (0) byte. Except, if the 0 byte is preceded by
178841 ** a byte with the 0x80 bit set, then it is not a varint 0, but the tail
178842 ** of some other, multi-byte, value.
178843 **
178844 ** The following while-loop moves pEnd to point to the first byte that is not
178845 ** immediately preceded by a byte with the 0x80 bit set. Then increments
178846 ** pEnd once more so that it points to the byte immediately following the
178847 ** last byte in the position-list.
178848 */
178849 while( *pEnd | c ){
178850 c = *pEnd++ & 0x80;
178851 testcase( c!=0 && (*pEnd)==0 );
178852 }
178853 pEnd++; /* Advance past the POS_END terminator byte */
178854
178855 if( pp ){
178856 int n = (int)(pEnd - *ppPoslist);
178857 char *p = *pp;
178858 memcpy(p, *ppPoslist, n);
178859 p += n;
178860 *pp = p;
178861 }
178862 *ppPoslist = pEnd;
178863}
178864
178865/*
178866** When this function is called, *ppPoslist is assumed to point to the
178867** start of a column-list. After it returns, *ppPoslist points to the
178868** to the terminator (POS_COLUMN or POS_END) byte of the column-list.
178869**
178870** A column-list is list of delta-encoded positions for a single column
178871** within a single document within a doclist.
178872**
178873** The column-list is terminated either by a POS_COLUMN varint (1) or
178874** a POS_END varint (0). This routine leaves *ppPoslist pointing to
178875** the POS_COLUMN or POS_END that terminates the column-list.
178876**
178877** If pp is not NULL, then the contents of the column-list are copied
178878** to *pp. *pp is set to point to the first byte past the last byte copied
178879** before this function returns. The POS_COLUMN or POS_END terminator
178880** is not copied into *pp.
178881*/
178882static void fts3ColumnlistCopy(char **pp, char **ppPoslist){
178883 char *pEnd = *ppPoslist;
178884 char c = 0;
178885
178886 /* A column-list is terminated by either a 0x01 or 0x00 byte that is
178887 ** not part of a multi-byte varint.
178888 */
178889 while( 0xFE & (*pEnd | c) ){
178890 c = *pEnd++ & 0x80;
178891 testcase( c!=0 && ((*pEnd)&0xfe)==0 );
178892 }
178893 if( pp ){
178894 int n = (int)(pEnd - *ppPoslist);
178895 char *p = *pp;
178896 memcpy(p, *ppPoslist, n);
178897 p += n;
178898 *pp = p;
178899 }
178900 *ppPoslist = pEnd;
178901}
178902
178903/*
178904** Value used to signify the end of an position-list. This must be
178905** as large or larger than any value that might appear on the
178906** position-list, even a position list that has been corrupted.
178907*/
178908#define POSITION_LIST_END(0xffffffff|(((i64)0x7fffffff)<<32)) LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32))
178909
178910/*
178911** This function is used to help parse position-lists. When this function is
178912** called, *pp may point to the start of the next varint in the position-list
178913** being parsed, or it may point to 1 byte past the end of the position-list
178914** (in which case **pp will be a terminator bytes POS_END (0) or
178915** (1)).
178916**
178917** If *pp points past the end of the current position-list, set *pi to
178918** POSITION_LIST_END and return. Otherwise, read the next varint from *pp,
178919** increment the current value of *pi by the value read, and set *pp to
178920** point to the next value before returning.
178921**
178922** Before calling this routine *pi must be initialized to the value of
178923** the previous position, or zero if we are reading the first position
178924** in the position-list. Because positions are delta-encoded, the value
178925** of the previous position is needed in order to compute the value of
178926** the next position.
178927*/
178928static void fts3ReadNextPos(
178929 char **pp, /* IN/OUT: Pointer into position-list buffer */
178930 sqlite3_int64 *pi /* IN/OUT: Value read from position-list */
178931){
178932 if( (**pp)&0xFE ){
178933 int iVal;
178934 *pp += fts3GetVarint32((*pp), &iVal)( (*(u8*)((*pp))&0x80) ? sqlite3Fts3GetVarint32((*pp), &
iVal) : (*&iVal=*(u8*)((*pp)), 1) )
;
178935 *pi += iVal;
178936 *pi -= 2;
178937 }else{
178938 *pi = POSITION_LIST_END(0xffffffff|(((i64)0x7fffffff)<<32));
178939 }
178940}
178941
178942/*
178943** If parameter iCol is not 0, write an POS_COLUMN (1) byte followed by
178944** the value of iCol encoded as a varint to *pp. This will start a new
178945** column list.
178946**
178947** Set *pp to point to the byte just after the last byte written before
178948** returning (do not modify it if iCol==0). Return the total number of bytes
178949** written (0 if iCol==0).
178950*/
178951static int fts3PutColNumber(char **pp, int iCol){
178952 int n = 0; /* Number of bytes written */
178953 if( iCol ){
178954 char *p = *pp; /* Output pointer */
178955 n = 1 + sqlite3Fts3PutVarint(&p[1], iCol);
178956 *p = 0x01;
178957 *pp = &p[n];
178958 }
178959 return n;
178960}
178961
178962/*
178963** Compute the union of two position lists. The output written
178964** into *pp contains all positions of both *pp1 and *pp2 in sorted
178965** order and with any duplicates removed. All pointers are
178966** updated appropriately. The caller is responsible for insuring
178967** that there is enough space in *pp to hold the complete output.
178968*/
178969static int fts3PoslistMerge(
178970 char **pp, /* Output buffer */
178971 char **pp1, /* Left input list */
178972 char **pp2 /* Right input list */
178973){
178974 char *p = *pp;
178975 char *p1 = *pp1;
178976 char *p2 = *pp2;
178977
178978 while( *p1 || *p2 ){
178979 int iCol1; /* The current column index in pp1 */
178980 int iCol2; /* The current column index in pp2 */
178981
178982 if( *p1==POS_COLUMN(1) ){
178983 fts3GetVarint32(&p1[1], &iCol1)( (*(u8*)(&p1[1])&0x80) ? sqlite3Fts3GetVarint32(&
p1[1], &iCol1) : (*&iCol1=*(u8*)(&p1[1]), 1) )
;
178984 if( iCol1==0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
178985 }
178986 else if( *p1==POS_END(0) ) iCol1 = 0x7fffffff;
178987 else iCol1 = 0;
178988
178989 if( *p2==POS_COLUMN(1) ){
178990 fts3GetVarint32(&p2[1], &iCol2)( (*(u8*)(&p2[1])&0x80) ? sqlite3Fts3GetVarint32(&
p2[1], &iCol2) : (*&iCol2=*(u8*)(&p2[1]), 1) )
;
178991 if( iCol2==0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
178992 }
178993 else if( *p2==POS_END(0) ) iCol2 = 0x7fffffff;
178994 else iCol2 = 0;
178995
178996 if( iCol1==iCol2 ){
178997 sqlite3_int64 i1 = 0; /* Last position from pp1 */
178998 sqlite3_int64 i2 = 0; /* Last position from pp2 */
178999 sqlite3_int64 iPrev = 0;
179000 int n = fts3PutColNumber(&p, iCol1);
179001 p1 += n;
179002 p2 += n;
179003
179004 /* At this point, both p1 and p2 point to the start of column-lists
179005 ** for the same column (the column with index iCol1 and iCol2).
179006 ** A column-list is a list of non-negative delta-encoded varints, each
179007 ** incremented by 2 before being stored. Each list is terminated by a
179008 ** POS_END (0) or POS_COLUMN (1). The following block merges the two lists
179009 ** and writes the results to buffer p. p is left pointing to the byte
179010 ** after the list written. No terminator (POS_END or POS_COLUMN) is
179011 ** written to the output.
179012 */
179013 fts3GetDeltaVarint(&p1, &i1);
179014 fts3GetDeltaVarint(&p2, &i2);
179015 if( i1<2 || i2<2 ){
179016 break;
179017 }
179018 do {
179019 fts3PutDeltaVarint(&p, &iPrev, (i1<i2) ? i1 : i2);
179020 iPrev -= 2;
179021 if( i1==i2 ){
179022 fts3ReadNextPos(&p1, &i1);
179023 fts3ReadNextPos(&p2, &i2);
179024 }else if( i1<i2 ){
179025 fts3ReadNextPos(&p1, &i1);
179026 }else{
179027 fts3ReadNextPos(&p2, &i2);
179028 }
179029 }while( i1!=POSITION_LIST_END(0xffffffff|(((i64)0x7fffffff)<<32)) || i2!=POSITION_LIST_END(0xffffffff|(((i64)0x7fffffff)<<32)) );
179030 }else if( iCol1<iCol2 ){
179031 p1 += fts3PutColNumber(&p, iCol1);
179032 fts3ColumnlistCopy(&p, &p1);
179033 }else{
179034 p2 += fts3PutColNumber(&p, iCol2);
179035 fts3ColumnlistCopy(&p, &p2);
179036 }
179037 }
179038
179039 *p++ = POS_END(0);
179040 *pp = p;
179041 *pp1 = p1 + 1;
179042 *pp2 = p2 + 1;
179043 return SQLITE_OK0;
179044}
179045
179046/*
179047** This function is used to merge two position lists into one. When it is
179048** called, *pp1 and *pp2 must both point to position lists. A position-list is
179049** the part of a doclist that follows each document id. For example, if a row
179050** contains:
179051**
179052** 'a b c'|'x y z'|'a b b a'
179053**
179054** Then the position list for this row for token 'b' would consist of:
179055**
179056** 0x02 0x01 0x02 0x03 0x03 0x00
179057**
179058** When this function returns, both *pp1 and *pp2 are left pointing to the
179059** byte following the 0x00 terminator of their respective position lists.
179060**
179061** If isSaveLeft is 0, an entry is added to the output position list for
179062** each position in *pp2 for which there exists one or more positions in
179063** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e.
179064** when the *pp1 token appears before the *pp2 token, but not more than nToken
179065** slots before it.
179066**
179067** e.g. nToken==1 searches for adjacent positions.
179068*/
179069static int fts3PoslistPhraseMerge(
179070 char **pp, /* IN/OUT: Preallocated output buffer */
179071 int nToken, /* Maximum difference in token positions */
179072 int isSaveLeft, /* Save the left position */
179073 int isExact, /* If *pp1 is exactly nTokens before *pp2 */
179074 char **pp1, /* IN/OUT: Left input list */
179075 char **pp2 /* IN/OUT: Right input list */
179076){
179077 char *p = *pp;
179078 char *p1 = *pp1;
179079 char *p2 = *pp2;
179080 int iCol1 = 0;
179081 int iCol2 = 0;
179082
179083 /* Never set both isSaveLeft and isExact for the same invocation. */
179084 assert( isSaveLeft==0 || isExact==0 )((void) (0));
179085
179086 assert_fts3_nc( p!=0 && *p1!=0 && *p2!=0 )((void) (0));
179087 if( *p1==POS_COLUMN(1) ){
179088 p1++;
179089 p1 += fts3GetVarint32(p1, &iCol1)( (*(u8*)(p1)&0x80) ? sqlite3Fts3GetVarint32(p1, &iCol1
) : (*&iCol1=*(u8*)(p1), 1) )
;
179090 }
179091 if( *p2==POS_COLUMN(1) ){
179092 p2++;
179093 p2 += fts3GetVarint32(p2, &iCol2)( (*(u8*)(p2)&0x80) ? sqlite3Fts3GetVarint32(p2, &iCol2
) : (*&iCol2=*(u8*)(p2), 1) )
;
179094 }
179095
179096 while( 1 ){
179097 if( iCol1==iCol2 ){
179098 char *pSave = p;
179099 sqlite3_int64 iPrev = 0;
179100 sqlite3_int64 iPos1 = 0;
179101 sqlite3_int64 iPos2 = 0;
179102
179103 if( iCol1 ){
179104 *p++ = POS_COLUMN(1);
179105 p += sqlite3Fts3PutVarint(p, iCol1);
179106 }
179107
179108 fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
179109 fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
179110 if( iPos1<0 || iPos2<0 ) break;
179111
179112 while( 1 ){
179113 if( iPos2==iPos1+nToken
179114 || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken)
179115 ){
179116 sqlite3_int64 iSave;
179117 iSave = isSaveLeft ? iPos1 : iPos2;
179118 fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2;
179119 pSave = 0;
179120 assert( p )((void) (0));
179121 }
179122 if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){
179123 if( (*p2&0xFE)==0 ) break;
179124 fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
179125 }else{
179126 if( (*p1&0xFE)==0 ) break;
179127 fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
179128 }
179129 }
179130
179131 if( pSave ){
179132 assert( pp && p )((void) (0));
179133 p = pSave;
179134 }
179135
179136 fts3ColumnlistCopy(0, &p1);
179137 fts3ColumnlistCopy(0, &p2);
179138 assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 )((void) (0));
179139 if( 0==*p1 || 0==*p2 ) break;
179140
179141 p1++;
179142 p1 += fts3GetVarint32(p1, &iCol1)( (*(u8*)(p1)&0x80) ? sqlite3Fts3GetVarint32(p1, &iCol1
) : (*&iCol1=*(u8*)(p1), 1) )
;
179143 p2++;
179144 p2 += fts3GetVarint32(p2, &iCol2)( (*(u8*)(p2)&0x80) ? sqlite3Fts3GetVarint32(p2, &iCol2
) : (*&iCol2=*(u8*)(p2), 1) )
;
179145 }
179146
179147 /* Advance pointer p1 or p2 (whichever corresponds to the smaller of
179148 ** iCol1 and iCol2) so that it points to either the 0x00 that marks the
179149 ** end of the position list, or the 0x01 that precedes the next
179150 ** column-number in the position list.
179151 */
179152 else if( iCol1<iCol2 ){
179153 fts3ColumnlistCopy(0, &p1);
179154 if( 0==*p1 ) break;
179155 p1++;
179156 p1 += fts3GetVarint32(p1, &iCol1)( (*(u8*)(p1)&0x80) ? sqlite3Fts3GetVarint32(p1, &iCol1
) : (*&iCol1=*(u8*)(p1), 1) )
;
179157 }else{
179158 fts3ColumnlistCopy(0, &p2);
179159 if( 0==*p2 ) break;
179160 p2++;
179161 p2 += fts3GetVarint32(p2, &iCol2)( (*(u8*)(p2)&0x80) ? sqlite3Fts3GetVarint32(p2, &iCol2
) : (*&iCol2=*(u8*)(p2), 1) )
;
179162 }
179163 }
179164
179165 fts3PoslistCopy(0, &p2);
179166 fts3PoslistCopy(0, &p1);
179167 *pp1 = p1;
179168 *pp2 = p2;
179169 if( *pp==p ){
179170 return 0;
179171 }
179172 *p++ = 0x00;
179173 *pp = p;
179174 return 1;
179175}
179176
179177/*
179178** Merge two position-lists as required by the NEAR operator. The argument
179179** position lists correspond to the left and right phrases of an expression
179180** like:
179181**
179182** "phrase 1" NEAR "phrase number 2"
179183**
179184** Position list *pp1 corresponds to the left-hand side of the NEAR
179185** expression and *pp2 to the right. As usual, the indexes in the position
179186** lists are the offsets of the last token in each phrase (tokens "1" and "2"
179187** in the example above).
179188**
179189** The output position list - written to *pp - is a copy of *pp2 with those
179190** entries that are not sufficiently NEAR entries in *pp1 removed.
179191*/
179192static int fts3PoslistNearMerge(
179193 char **pp, /* Output buffer */
179194 char *aTmp, /* Temporary buffer space */
179195 int nRight, /* Maximum difference in token positions */
179196 int nLeft, /* Maximum difference in token positions */
179197 char **pp1, /* IN/OUT: Left input list */
179198 char **pp2 /* IN/OUT: Right input list */
179199){
179200 char *p1 = *pp1;
179201 char *p2 = *pp2;
179202
179203 char *pTmp1 = aTmp;
179204 char *pTmp2;
179205 char *aTmp2;
179206 int res = 1;
179207
179208 fts3PoslistPhraseMerge(&pTmp1, nRight, 0, 0, pp1, pp2);
179209 aTmp2 = pTmp2 = pTmp1;
179210 *pp1 = p1;
179211 *pp2 = p2;
179212 fts3PoslistPhraseMerge(&pTmp2, nLeft, 1, 0, pp2, pp1);
179213 if( pTmp1!=aTmp && pTmp2!=aTmp2 ){
179214 fts3PoslistMerge(pp, &aTmp, &aTmp2);
179215 }else if( pTmp1!=aTmp ){
179216 fts3PoslistCopy(pp, &aTmp);
179217 }else if( pTmp2!=aTmp2 ){
179218 fts3PoslistCopy(pp, &aTmp2);
179219 }else{
179220 res = 0;
179221 }
179222
179223 return res;
179224}
179225
179226/*
179227** An instance of this function is used to merge together the (potentially
179228** large number of) doclists for each term that matches a prefix query.
179229** See function fts3TermSelectMerge() for details.
179230*/
179231typedef struct TermSelect TermSelect;
179232struct TermSelect {
179233 char *aaOutput[16]; /* Malloc'd output buffers */
179234 int anOutput[16]; /* Size each output buffer in bytes */
179235};
179236
179237/*
179238** This function is used to read a single varint from a buffer. Parameter
179239** pEnd points 1 byte past the end of the buffer. When this function is
179240** called, if *pp points to pEnd or greater, then the end of the buffer
179241** has been reached. In this case *pp is set to 0 and the function returns.
179242**
179243** If *pp does not point to or past pEnd, then a single varint is read
179244** from *pp. *pp is then set to point 1 byte past the end of the read varint.
179245**
179246** If bDescIdx is false, the value read is added to *pVal before returning.
179247** If it is true, the value read is subtracted from *pVal before this
179248** function returns.
179249*/
179250static void fts3GetDeltaVarint3(
179251 char **pp, /* IN/OUT: Point to read varint from */
179252 char *pEnd, /* End of buffer */
179253 int bDescIdx, /* True if docids are descending */
179254 sqlite3_int64 *pVal /* IN/OUT: Integer value */
179255){
179256 if( *pp>=pEnd ){
179257 *pp = 0;
179258 }else{
179259 u64 iVal;
179260 *pp += sqlite3Fts3GetVarintU(*pp, &iVal);
179261 if( bDescIdx ){
179262 *pVal = (i64)((u64)*pVal - iVal);
179263 }else{
179264 *pVal = (i64)((u64)*pVal + iVal);
179265 }
179266 }
179267}
179268
179269/*
179270** This function is used to write a single varint to a buffer. The varint
179271** is written to *pp. Before returning, *pp is set to point 1 byte past the
179272** end of the value written.
179273**
179274** If *pbFirst is zero when this function is called, the value written to
179275** the buffer is that of parameter iVal.
179276**
179277** If *pbFirst is non-zero when this function is called, then the value
179278** written is either (iVal-*piPrev) (if bDescIdx is zero) or (*piPrev-iVal)
179279** (if bDescIdx is non-zero).
179280**
179281** Before returning, this function always sets *pbFirst to 1 and *piPrev
179282** to the value of parameter iVal.
179283*/
179284static void fts3PutDeltaVarint3(
179285 char **pp, /* IN/OUT: Output pointer */
179286 int bDescIdx, /* True for descending docids */
179287 sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */
179288 int *pbFirst, /* IN/OUT: True after first int written */
179289 sqlite3_int64 iVal /* Write this value to the list */
179290){
179291 sqlite3_uint64 iWrite;
179292 if( bDescIdx==0 || *pbFirst==0 ){
179293 assert_fts3_nc( *pbFirst==0 || iVal>=*piPrev )((void) (0));
179294 iWrite = (u64)iVal - (u64)*piPrev;
179295 }else{
179296 assert_fts3_nc( *piPrev>=iVal )((void) (0));
179297 iWrite = (u64)*piPrev - (u64)iVal;
179298 }
179299 assert( *pbFirst || *piPrev==0 )((void) (0));
179300 assert_fts3_nc( *pbFirst==0 || iWrite>0 )((void) (0));
179301 *pp += sqlite3Fts3PutVarint(*pp, iWrite);
179302 *piPrev = iVal;
179303 *pbFirst = 1;
179304}
179305
179306
179307/*
179308** This macro is used by various functions that merge doclists. The two
179309** arguments are 64-bit docid values. If the value of the stack variable
179310** bDescDoclist is 0 when this macro is invoked, then it returns (i1-i2).
179311** Otherwise, (i2-i1).
179312**
179313** Using this makes it easier to write code that can merge doclists that are
179314** sorted in either ascending or descending order.
179315*/
179316/* #define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i64)((u64)i1-i2)) */
179317#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)))
179318
179319/*
179320** This function does an "OR" merge of two doclists (output contains all
179321** positions contained in either argument doclist). If the docids in the
179322** input doclists are sorted in ascending order, parameter bDescDoclist
179323** should be false. If they are sorted in ascending order, it should be
179324** passed a non-zero value.
179325**
179326** If no error occurs, *paOut is set to point at an sqlite3_malloc'd buffer
179327** containing the output doclist and SQLITE_OK is returned. In this case
179328** *pnOut is set to the number of bytes in the output doclist.
179329**
179330** If an error occurs, an SQLite error code is returned. The output values
179331** are undefined in this case.
179332*/
179333static int fts3DoclistOrMerge(
179334 int bDescDoclist, /* True if arguments are desc */
179335 char *a1, int n1, /* First doclist */
179336 char *a2, int n2, /* Second doclist */
179337 char **paOut, int *pnOut /* OUT: Malloc'd doclist */
179338){
179339 int rc = SQLITE_OK0;
179340 sqlite3_int64 i1 = 0;
179341 sqlite3_int64 i2 = 0;
179342 sqlite3_int64 iPrev = 0;
179343 char *pEnd1 = &a1[n1];
179344 char *pEnd2 = &a2[n2];
179345 char *p1 = a1;
179346 char *p2 = a2;
179347 char *p;
179348 char *aOut;
179349 int bFirstOut = 0;
179350
179351 *paOut = 0;
179352 *pnOut = 0;
179353
179354 /* Allocate space for the output. Both the input and output doclists
179355 ** are delta encoded. If they are in ascending order (bDescDoclist==0),
179356 ** then the first docid in each list is simply encoded as a varint. For
179357 ** each subsequent docid, the varint stored is the difference between the
179358 ** current and previous docid (a positive number - since the list is in
179359 ** ascending order).
179360 **
179361 ** The first docid written to the output is therefore encoded using the
179362 ** same number of bytes as it is in whichever of the input lists it is
179363 ** read from. And each subsequent docid read from the same input list
179364 ** consumes either the same or less bytes as it did in the input (since
179365 ** the difference between it and the previous value in the output must
179366 ** be a positive value less than or equal to the delta value read from
179367 ** the input list). The same argument applies to all but the first docid
179368 ** read from the 'other' list. And to the contents of all position lists
179369 ** that will be copied and merged from the input to the output.
179370 **
179371 ** However, if the first docid copied to the output is a negative number,
179372 ** then the encoding of the first docid from the 'other' input list may
179373 ** be larger in the output than it was in the input (since the delta value
179374 ** may be a larger positive integer than the actual docid).
179375 **
179376 ** The space required to store the output is therefore the sum of the
179377 ** sizes of the two inputs, plus enough space for exactly one of the input
179378 ** docids to grow.
179379 **
179380 ** A symetric argument may be made if the doclists are in descending
179381 ** order.
179382 */
179383 aOut = sqlite3_malloc64((i64)n1+n2+FTS3_VARINT_MAX10-1+FTS3_BUFFER_PADDING8);
179384 if( !aOut ) return SQLITE_NOMEM7;
179385
179386 p = aOut;
179387 fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
179388 fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
179389 while( p1 || p2 ){
179390 sqlite3_int64 iDiff = DOCID_CMP(i1, i2)((bDescDoclist?-1:1) * (i1>i2?1:((i1==i2)?0:-1)));
179391
179392 if( p2 && p1 && iDiff==0 ){
179393 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
179394 rc = fts3PoslistMerge(&p, &p1, &p2);
179395 if( rc ) break;
179396 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
179397 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
179398 }else if( !p2 || (p1 && iDiff<0) ){
179399 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
179400 fts3PoslistCopy(&p, &p1);
179401 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
179402 }else{
179403 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i2);
179404 fts3PoslistCopy(&p, &p2);
179405 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
179406 }
179407
179408 assert( (p-aOut)<=((p1?(p1-a1):n1)+(p2?(p2-a2):n2)+FTS3_VARINT_MAX-1) )((void) (0));
179409 }
179410
179411 if( rc!=SQLITE_OK0 ){
179412 sqlite3_free(aOut);
179413 p = aOut = 0;
179414 }else{
179415 assert( (p-aOut)<=n1+n2+FTS3_VARINT_MAX-1 )((void) (0));
179416 memset(&aOut[(p-aOut)], 0, FTS3_BUFFER_PADDING8);
179417 }
179418 *paOut = aOut;
179419 *pnOut = (int)(p-aOut);
179420 return rc;
179421}
179422
179423/*
179424** This function does a "phrase" merge of two doclists. In a phrase merge,
179425** the output contains a copy of each position from the right-hand input
179426** doclist for which there is a position in the left-hand input doclist
179427** exactly nDist tokens before it.
179428**
179429** If the docids in the input doclists are sorted in ascending order,
179430** parameter bDescDoclist should be false. If they are sorted in ascending
179431** order, it should be passed a non-zero value.
179432**
179433** The right-hand input doclist is overwritten by this function.
179434*/
179435static int fts3DoclistPhraseMerge(
179436 int bDescDoclist, /* True if arguments are desc */
179437 int nDist, /* Distance from left to right (1=adjacent) */
179438 char *aLeft, int nLeft, /* Left doclist */
179439 char **paRight, int *pnRight /* IN/OUT: Right/output doclist */
179440){
179441 sqlite3_int64 i1 = 0;
179442 sqlite3_int64 i2 = 0;
179443 sqlite3_int64 iPrev = 0;
179444 char *aRight = *paRight;
179445 char *pEnd1 = &aLeft[nLeft];
179446 char *pEnd2 = &aRight[*pnRight];
179447 char *p1 = aLeft;
179448 char *p2 = aRight;
179449 char *p;
179450 int bFirstOut = 0;
179451 char *aOut;
179452
179453 assert( nDist>0 )((void) (0));
179454 if( bDescDoclist ){
179455 aOut = sqlite3_malloc64((sqlite3_int64)*pnRight + FTS3_VARINT_MAX10);
179456 if( aOut==0 ) return SQLITE_NOMEM7;
179457 }else{
179458 aOut = aRight;
179459 }
179460 p = aOut;
179461
179462 fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
179463 fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
179464
179465 while( p1 && p2 ){
179466 sqlite3_int64 iDiff = DOCID_CMP(i1, i2)((bDescDoclist?-1:1) * (i1>i2?1:((i1==i2)?0:-1)));
179467 if( iDiff==0 ){
179468 char *pSave = p;
179469 sqlite3_int64 iPrevSave = iPrev;
179470 int bFirstOutSave = bFirstOut;
179471
179472 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
179473 if( 0==fts3PoslistPhraseMerge(&p, nDist, 0, 1, &p1, &p2) ){
179474 p = pSave;
179475 iPrev = iPrevSave;
179476 bFirstOut = bFirstOutSave;
179477 }
179478 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
179479 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
179480 }else if( iDiff<0 ){
179481 fts3PoslistCopy(0, &p1);
179482 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
179483 }else{
179484 fts3PoslistCopy(0, &p2);
179485 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
179486 }
179487 }
179488
179489 *pnRight = (int)(p - aOut);
179490 if( bDescDoclist ){
179491 sqlite3_free(aRight);
179492 *paRight = aOut;
179493 }
179494
179495 return SQLITE_OK0;
179496}
179497
179498/*
179499** Argument pList points to a position list nList bytes in size. This
179500** function checks to see if the position list contains any entries for
179501** a token in position 0 (of any column). If so, it writes argument iDelta
179502** to the output buffer pOut, followed by a position list consisting only
179503** of the entries from pList at position 0, and terminated by an 0x00 byte.
179504** The value returned is the number of bytes written to pOut (if any).
179505*/
179506SQLITE_PRIVATEstatic int sqlite3Fts3FirstFilter(
179507 sqlite3_int64 iDelta, /* Varint that may be written to pOut */
179508 char *pList, /* Position list (no 0x00 term) */
179509 int nList, /* Size of pList in bytes */
179510 char *pOut /* Write output here */
179511){
179512 int nOut = 0;
179513 int bWritten = 0; /* True once iDelta has been written */
179514 char *p = pList;
179515 char *pEnd = &pList[nList];
179516
179517 if( *p!=0x01 ){
179518 if( *p==0x02 ){
179519 nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
179520 pOut[nOut++] = 0x02;
179521 bWritten = 1;
179522 }
179523 fts3ColumnlistCopy(0, &p);
179524 }
179525
179526 while( p<pEnd ){
179527 sqlite3_int64 iCol;
179528 p++;
179529 p += sqlite3Fts3GetVarint(p, &iCol);
179530 if( *p==0x02 ){
179531 if( bWritten==0 ){
179532 nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
179533 bWritten = 1;
179534 }
179535 pOut[nOut++] = 0x01;
179536 nOut += sqlite3Fts3PutVarint(&pOut[nOut], iCol);
179537 pOut[nOut++] = 0x02;
179538 }
179539 fts3ColumnlistCopy(0, &p);
179540 }
179541 if( bWritten ){
179542 pOut[nOut++] = 0x00;
179543 }
179544
179545 return nOut;
179546}
179547
179548
179549/*
179550** Merge all doclists in the TermSelect.aaOutput[] array into a single
179551** doclist stored in TermSelect.aaOutput[0]. If successful, delete all
179552** other doclists (except the aaOutput[0] one) and return SQLITE_OK.
179553**
179554** If an OOM error occurs, return SQLITE_NOMEM. In this case it is
179555** the responsibility of the caller to free any doclists left in the
179556** TermSelect.aaOutput[] array.
179557*/
179558static int fts3TermSelectFinishMerge(Fts3Table *p, TermSelect *pTS){
179559 char *aOut = 0;
179560 int nOut = 0;
179561 int i;
179562
179563 /* Loop through the doclists in the aaOutput[] array. Merge them all
179564 ** into a single doclist.
179565 */
179566 for(i=0; i<SizeofArray(pTS->aaOutput)((int)(sizeof(pTS->aaOutput)/sizeof(pTS->aaOutput[0]))); i++){
179567 if( pTS->aaOutput[i] ){
179568 if( !aOut ){
179569 aOut = pTS->aaOutput[i];
179570 nOut = pTS->anOutput[i];
179571 pTS->aaOutput[i] = 0;
179572 }else{
179573 int nNew;
179574 char *aNew;
179575
179576 int rc = fts3DoclistOrMerge(p->bDescIdx,
179577 pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, &aNew, &nNew
179578 );
179579 if( rc!=SQLITE_OK0 ){
179580 sqlite3_free(aOut);
179581 return rc;
179582 }
179583
179584 sqlite3_free(pTS->aaOutput[i]);
179585 sqlite3_free(aOut);
179586 pTS->aaOutput[i] = 0;
179587 aOut = aNew;
179588 nOut = nNew;
179589 }
179590 }
179591 }
179592
179593 pTS->aaOutput[0] = aOut;
179594 pTS->anOutput[0] = nOut;
179595 return SQLITE_OK0;
179596}
179597
179598/*
179599** Merge the doclist aDoclist/nDoclist into the TermSelect object passed
179600** as the first argument. The merge is an "OR" merge (see function
179601** fts3DoclistOrMerge() for details).
179602**
179603** This function is called with the doclist for each term that matches
179604** a queried prefix. It merges all these doclists into one, the doclist
179605** for the specified prefix. Since there can be a very large number of
179606** doclists to merge, the merging is done pair-wise using the TermSelect
179607** object.
179608**
179609** This function returns SQLITE_OK if the merge is successful, or an
179610** SQLite error code (SQLITE_NOMEM) if an error occurs.
179611*/
179612static int fts3TermSelectMerge(
179613 Fts3Table *p, /* FTS table handle */
179614 TermSelect *pTS, /* TermSelect object to merge into */
179615 char *aDoclist, /* Pointer to doclist */
179616 int nDoclist /* Size of aDoclist in bytes */
179617){
179618 if( pTS->aaOutput[0]==0 ){
179619 /* If this is the first term selected, copy the doclist to the output
179620 ** buffer using memcpy().
179621 **
179622 ** Add FTS3_VARINT_MAX bytes of unused space to the end of the
179623 ** allocation. This is so as to ensure that the buffer is big enough
179624 ** to hold the current doclist AND'd with any other doclist. If the
179625 ** doclists are stored in order=ASC order, this padding would not be
179626 ** required (since the size of [doclistA AND doclistB] is always less
179627 ** than or equal to the size of [doclistA] in that case). But this is
179628 ** not true for order=DESC. For example, a doclist containing (1, -1)
179629 ** may be smaller than (-1), as in the first example the -1 may be stored
179630 ** as a single-byte delta, whereas in the second it must be stored as a
179631 ** FTS3_VARINT_MAX byte varint.
179632 **
179633 ** Similar padding is added in the fts3DoclistOrMerge() function.
179634 */
179635 pTS->aaOutput[0] = sqlite3_malloc(nDoclist + FTS3_VARINT_MAX10 + 1);
179636 pTS->anOutput[0] = nDoclist;
179637 if( pTS->aaOutput[0] ){
179638 memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
179639 memset(&pTS->aaOutput[0][nDoclist], 0, FTS3_VARINT_MAX10);
179640 }else{
179641 return SQLITE_NOMEM7;
179642 }
179643 }else{
179644 char *aMerge = aDoclist;
179645 int nMerge = nDoclist;
179646 int iOut;
179647
179648 for(iOut=0; iOut<SizeofArray(pTS->aaOutput)((int)(sizeof(pTS->aaOutput)/sizeof(pTS->aaOutput[0]))); iOut++){
179649 if( pTS->aaOutput[iOut]==0 ){
179650 assert( iOut>0 )((void) (0));
179651 pTS->aaOutput[iOut] = aMerge;
179652 pTS->anOutput[iOut] = nMerge;
179653 break;
179654 }else{
179655 char *aNew;
179656 int nNew;
179657
179658 int rc = fts3DoclistOrMerge(p->bDescIdx, aMerge, nMerge,
179659 pTS->aaOutput[iOut], pTS->anOutput[iOut], &aNew, &nNew
179660 );
179661 if( rc!=SQLITE_OK0 ){
179662 if( aMerge!=aDoclist ) sqlite3_free(aMerge);
179663 return rc;
179664 }
179665
179666 if( aMerge!=aDoclist ) sqlite3_free(aMerge);
179667 sqlite3_free(pTS->aaOutput[iOut]);
179668 pTS->aaOutput[iOut] = 0;
179669
179670 aMerge = aNew;
179671 nMerge = nNew;
179672 if( (iOut+1)==SizeofArray(pTS->aaOutput)((int)(sizeof(pTS->aaOutput)/sizeof(pTS->aaOutput[0]))) ){
179673 pTS->aaOutput[iOut] = aMerge;
179674 pTS->anOutput[iOut] = nMerge;
179675 }
179676 }
179677 }
179678 }
179679 return SQLITE_OK0;
179680}
179681
179682/*
179683** Append SegReader object pNew to the end of the pCsr->apSegment[] array.
179684*/
179685static int fts3SegReaderCursorAppend(
179686 Fts3MultiSegReader *pCsr,
179687 Fts3SegReader *pNew
179688){
179689 if( (pCsr->nSegment%16)==0 ){
179690 Fts3SegReader **apNew;
179691 sqlite3_int64 nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
179692 apNew = (Fts3SegReader **)sqlite3_realloc64(pCsr->apSegment, nByte);
179693 if( !apNew ){
179694 sqlite3Fts3SegReaderFree(pNew);
179695 return SQLITE_NOMEM7;
179696 }
179697 pCsr->apSegment = apNew;
179698 }
179699 pCsr->apSegment[pCsr->nSegment++] = pNew;
179700 return SQLITE_OK0;
179701}
179702
179703/*
179704** Add seg-reader objects to the Fts3MultiSegReader object passed as the
179705** 8th argument.
179706**
179707** This function returns SQLITE_OK if successful, or an SQLite error code
179708** otherwise.
179709*/
179710static int fts3SegReaderCursor(
179711 Fts3Table *p, /* FTS3 table handle */
179712 int iLangid, /* Language id */
179713 int iIndex, /* Index to search (from 0 to p->nIndex-1) */
179714 int iLevel, /* Level of segments to scan */
179715 const char *zTerm, /* Term to query for */
179716 int nTerm, /* Size of zTerm in bytes */
179717 int isPrefix, /* True for a prefix search */
179718 int isScan, /* True to scan from zTerm to EOF */
179719 Fts3MultiSegReader *pCsr /* Cursor object to populate */
179720){
179721 int rc = SQLITE_OK0; /* Error code */
179722 sqlite3_stmt *pStmt = 0; /* Statement to iterate through segments */
179723 int rc2; /* Result of sqlite3_reset() */
179724
179725 /* If iLevel is less than 0 and this is not a scan, include a seg-reader
179726 ** for the pending-terms. If this is a scan, then this call must be being
179727 ** made by an fts4aux module, not an FTS table. In this case calling
179728 ** Fts3SegReaderPending might segfault, as the data structures used by
179729 ** fts4aux are not completely populated. So it's easiest to filter these
179730 ** calls out here. */
179731 if( iLevel<0 && p->aIndex && p->iPrevLangid==iLangid ){
179732 Fts3SegReader *pSeg = 0;
179733 rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix||isScan, &pSeg);
179734 if( rc==SQLITE_OK0 && pSeg ){
179735 rc = fts3SegReaderCursorAppend(pCsr, pSeg);
179736 }
179737 }
179738
179739 if( iLevel!=FTS3_SEGCURSOR_PENDING-1 ){
179740 if( rc==SQLITE_OK0 ){
179741 rc = sqlite3Fts3AllSegdirs(p, iLangid, iIndex, iLevel, &pStmt);
179742 }
179743
179744 while( rc==SQLITE_OK0 && SQLITE_ROW100==(rc = sqlite3_step(pStmt)) ){
179745 Fts3SegReader *pSeg = 0;
179746
179747 /* Read the values returned by the SELECT into local variables. */
179748 sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1);
179749 sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2);
179750 sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3);
179751 int nRoot = sqlite3_column_bytes(pStmt, 4);
179752 char const *zRoot = sqlite3_column_blob(pStmt, 4);
179753
179754 /* If zTerm is not NULL, and this segment is not stored entirely on its
179755 ** root node, the range of leaves scanned can be reduced. Do this. */
179756 if( iStartBlock && zTerm && zRoot ){
179757 sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0);
179758 rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi);
179759 if( rc!=SQLITE_OK0 ) goto finished;
179760 if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock;
179761 }
179762
179763 rc = sqlite3Fts3SegReaderNew(pCsr->nSegment+1,
179764 (isPrefix==0 && isScan==0),
179765 iStartBlock, iLeavesEndBlock,
179766 iEndBlock, zRoot, nRoot, &pSeg
179767 );
179768 if( rc!=SQLITE_OK0 ) goto finished;
179769 rc = fts3SegReaderCursorAppend(pCsr, pSeg);
179770 }
179771 }
179772
179773 finished:
179774 rc2 = sqlite3_reset(pStmt);
179775 if( rc==SQLITE_DONE101 ) rc = rc2;
179776
179777 return rc;
179778}
179779
179780/*
179781** Set up a cursor object for iterating through a full-text index or a
179782** single level therein.
179783*/
179784SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderCursor(
179785 Fts3Table *p, /* FTS3 table handle */
179786 int iLangid, /* Language-id to search */
179787 int iIndex, /* Index to search (from 0 to p->nIndex-1) */
179788 int iLevel, /* Level of segments to scan */
179789 const char *zTerm, /* Term to query for */
179790 int nTerm, /* Size of zTerm in bytes */
179791 int isPrefix, /* True for a prefix search */
179792 int isScan, /* True to scan from zTerm to EOF */
179793 Fts3MultiSegReader *pCsr /* Cursor object to populate */
179794){
179795 assert( iIndex>=0 && iIndex<p->nIndex )((void) (0));
179796 assert( iLevel==FTS3_SEGCURSOR_ALL((void) (0))
179797 || iLevel==FTS3_SEGCURSOR_PENDING((void) (0))
179798 || iLevel>=0((void) (0))
179799 )((void) (0));
179800 assert( iLevel<FTS3_SEGDIR_MAXLEVEL )((void) (0));
179801 assert( FTS3_SEGCURSOR_ALL<0 && FTS3_SEGCURSOR_PENDING<0 )((void) (0));
179802 assert( isPrefix==0 || isScan==0 )((void) (0));
179803
179804 memset(pCsr, 0, sizeof(Fts3MultiSegReader));
179805 return fts3SegReaderCursor(
179806 p, iLangid, iIndex, iLevel, zTerm, nTerm, isPrefix, isScan, pCsr
179807 );
179808}
179809
179810/*
179811** In addition to its current configuration, have the Fts3MultiSegReader
179812** passed as the 4th argument also scan the doclist for term zTerm/nTerm.
179813**
179814** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
179815*/
179816static int fts3SegReaderCursorAddZero(
179817 Fts3Table *p, /* FTS virtual table handle */
179818 int iLangid,
179819 const char *zTerm, /* Term to scan doclist of */
179820 int nTerm, /* Number of bytes in zTerm */
179821 Fts3MultiSegReader *pCsr /* Fts3MultiSegReader to modify */
179822){
179823 return fts3SegReaderCursor(p,
179824 iLangid, 0, FTS3_SEGCURSOR_ALL-2, zTerm, nTerm, 0, 0,pCsr
179825 );
179826}
179827
179828/*
179829** Open an Fts3MultiSegReader to scan the doclist for term zTerm/nTerm. Or,
179830** if isPrefix is true, to scan the doclist for all terms for which
179831** zTerm/nTerm is a prefix. If successful, return SQLITE_OK and write
179832** a pointer to the new Fts3MultiSegReader to *ppSegcsr. Otherwise, return
179833** an SQLite error code.
179834**
179835** It is the responsibility of the caller to free this object by eventually
179836** passing it to fts3SegReaderCursorFree()
179837**
179838** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
179839** Output parameter *ppSegcsr is set to 0 if an error occurs.
179840*/
179841static int fts3TermSegReaderCursor(
179842 Fts3Cursor *pCsr, /* Virtual table cursor handle */
179843 const char *zTerm, /* Term to query for */
179844 int nTerm, /* Size of zTerm in bytes */
179845 int isPrefix, /* True for a prefix search */
179846 Fts3MultiSegReader **ppSegcsr /* OUT: Allocated seg-reader cursor */
179847){
179848 Fts3MultiSegReader *pSegcsr; /* Object to allocate and return */
179849 int rc = SQLITE_NOMEM7; /* Return code */
179850
179851 pSegcsr = sqlite3_malloc(sizeof(Fts3MultiSegReader));
179852 if( pSegcsr ){
179853 int i;
179854 int bFound = 0; /* True once an index has been found */
179855 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
179856
179857 if( isPrefix ){
179858 for(i=1; bFound==0 && i<p->nIndex; i++){
179859 if( p->aIndex[i].nPrefix==nTerm ){
179860 bFound = 1;
179861 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
179862 i, FTS3_SEGCURSOR_ALL-2, zTerm, nTerm, 0, 0, pSegcsr
179863 );
179864 pSegcsr->bLookup = 1;
179865 }
179866 }
179867
179868 for(i=1; bFound==0 && i<p->nIndex; i++){
179869 if( p->aIndex[i].nPrefix==nTerm+1 ){
179870 bFound = 1;
179871 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
179872 i, FTS3_SEGCURSOR_ALL-2, zTerm, nTerm, 1, 0, pSegcsr
179873 );
179874 if( rc==SQLITE_OK0 ){
179875 rc = fts3SegReaderCursorAddZero(
179876 p, pCsr->iLangid, zTerm, nTerm, pSegcsr
179877 );
179878 }
179879 }
179880 }
179881 }
179882
179883 if( bFound==0 ){
179884 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
179885 0, FTS3_SEGCURSOR_ALL-2, zTerm, nTerm, isPrefix, 0, pSegcsr
179886 );
179887 pSegcsr->bLookup = !isPrefix;
179888 }
179889 }
179890
179891 *ppSegcsr = pSegcsr;
179892 return rc;
179893}
179894
179895/*
179896** Free an Fts3MultiSegReader allocated by fts3TermSegReaderCursor().
179897*/
179898static void fts3SegReaderCursorFree(Fts3MultiSegReader *pSegcsr){
179899 sqlite3Fts3SegReaderFinish(pSegcsr);
179900 sqlite3_free(pSegcsr);
179901}
179902
179903/*
179904** This function retrieves the doclist for the specified term (or term
179905** prefix) from the database.
179906*/
179907static int fts3TermSelect(
179908 Fts3Table *p, /* Virtual table handle */
179909 Fts3PhraseToken *pTok, /* Token to query for */
179910 int iColumn, /* Column to query (or -ve for all columns) */
179911 int *pnOut, /* OUT: Size of buffer at *ppOut */
179912 char **ppOut /* OUT: Malloced result buffer */
179913){
179914 int rc; /* Return code */
179915 Fts3MultiSegReader *pSegcsr; /* Seg-reader cursor for this term */
179916 TermSelect tsc; /* Object for pair-wise doclist merging */
179917 Fts3SegFilter filter; /* Segment term filter configuration */
179918
179919 pSegcsr = pTok->pSegcsr;
179920 memset(&tsc, 0, sizeof(TermSelect));
179921
179922 filter.flags = FTS3_SEGMENT_IGNORE_EMPTY0x00000002 | FTS3_SEGMENT_REQUIRE_POS0x00000001
179923 | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX0x00000008 : 0)
179924 | (pTok->bFirst ? FTS3_SEGMENT_FIRST0x00000020 : 0)
179925 | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER0x00000004 : 0);
179926 filter.iCol = iColumn;
179927 filter.zTerm = pTok->z;
179928 filter.nTerm = pTok->n;
179929
179930 rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter);
179931 while( SQLITE_OK0==rc
179932 && SQLITE_ROW100==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr))
179933 ){
179934 rc = fts3TermSelectMerge(p, &tsc, pSegcsr->aDoclist, pSegcsr->nDoclist);
179935 }
179936
179937 if( rc==SQLITE_OK0 ){
179938 rc = fts3TermSelectFinishMerge(p, &tsc);
179939 }
179940 if( rc==SQLITE_OK0 ){
179941 *ppOut = tsc.aaOutput[0];
179942 *pnOut = tsc.anOutput[0];
179943 }else{
179944 int i;
179945 for(i=0; i<SizeofArray(tsc.aaOutput)((int)(sizeof(tsc.aaOutput)/sizeof(tsc.aaOutput[0]))); i++){
179946 sqlite3_free(tsc.aaOutput[i]);
179947 }
179948 }
179949
179950 fts3SegReaderCursorFree(pSegcsr);
179951 pTok->pSegcsr = 0;
179952 return rc;
179953}
179954
179955/*
179956** This function counts the total number of docids in the doclist stored
179957** in buffer aList[], size nList bytes.
179958**
179959** If the isPoslist argument is true, then it is assumed that the doclist
179960** contains a position-list following each docid. Otherwise, it is assumed
179961** that the doclist is simply a list of docids stored as delta encoded
179962** varints.
179963*/
179964static int fts3DoclistCountDocids(char *aList, int nList){
179965 int nDoc = 0; /* Return value */
179966 if( aList ){
179967 char *aEnd = &aList[nList]; /* Pointer to one byte after EOF */
179968 char *p = aList; /* Cursor */
179969 while( p<aEnd ){
179970 nDoc++;
179971 while( (*p++)&0x80 ); /* Skip docid varint */
179972 fts3PoslistCopy(0, &p); /* Skip over position list */
179973 }
179974 }
179975
179976 return nDoc;
179977}
179978
179979/*
179980** Advance the cursor to the next row in the %_content table that
179981** matches the search criteria. For a MATCH search, this will be
179982** the next row that matches. For a full-table scan, this will be
179983** simply the next row in the %_content table. For a docid lookup,
179984** this routine simply sets the EOF flag.
179985**
179986** Return SQLITE_OK if nothing goes wrong. SQLITE_OK is returned
179987** even if we reach end-of-file. The fts3EofMethod() will be called
179988** subsequently to determine whether or not an EOF was hit.
179989*/
179990static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){
179991 int rc;
179992 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
179993 if( pCsr->eSearch==FTS3_DOCID_SEARCH1 || pCsr->eSearch==FTS3_FULLSCAN_SEARCH0 ){
179994 Fts3Table *pTab = (Fts3Table*)pCursor->pVtab;
179995 pTab->bLock++;
179996 if( SQLITE_ROW100!=sqlite3_step(pCsr->pStmt) ){
179997 pCsr->isEof = 1;
179998 rc = sqlite3_reset(pCsr->pStmt);
179999 }else{
180000 pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0);
180001 rc = SQLITE_OK0;
180002 }
180003 pTab->bLock--;
180004 }else{
180005 rc = fts3EvalNext((Fts3Cursor *)pCursor);
180006 }
180007 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 )((void) (0));
180008 return rc;
180009}
180010
180011/*
180012** If the numeric type of argument pVal is "integer", then return it
180013** converted to a 64-bit signed integer. Otherwise, return a copy of
180014** the second parameter, iDefault.
180015*/
180016static sqlite3_int64 fts3DocidRange(sqlite3_value *pVal, i64 iDefault){
180017 if( pVal ){
180018 int eType = sqlite3_value_numeric_type(pVal);
180019 if( eType==SQLITE_INTEGER1 ){
180020 return sqlite3_value_int64(pVal);
180021 }
180022 }
180023 return iDefault;
180024}
180025
180026/*
180027** This is the xFilter interface for the virtual table. See
180028** the virtual table xFilter method documentation for additional
180029** information.
180030**
180031** If idxNum==FTS3_FULLSCAN_SEARCH then do a full table scan against
180032** the %_content table.
180033**
180034** If idxNum==FTS3_DOCID_SEARCH then do a docid lookup for a single entry
180035** in the %_content table.
180036**
180037** If idxNum>=FTS3_FULLTEXT_SEARCH then use the full text index. The
180038** column on the left-hand side of the MATCH operator is column
180039** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed. argv[0] is the right-hand
180040** side of the MATCH operator.
180041*/
180042static int fts3FilterMethod(
180043 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
180044 int idxNum, /* Strategy index */
180045 const char *idxStr, /* Unused */
180046 int nVal, /* Number of elements in apVal */
180047 sqlite3_value **apVal /* Arguments for the indexing scheme */
180048){
180049 int rc = SQLITE_OK0;
180050 char *zSql; /* SQL statement used to access %_content */
180051 int eSearch;
180052 Fts3Table *p = (Fts3Table *)pCursor->pVtab;
180053 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
180054
180055 sqlite3_value *pCons = 0; /* The MATCH or rowid constraint, if any */
180056 sqlite3_value *pLangid = 0; /* The "langid = ?" constraint, if any */
180057 sqlite3_value *pDocidGe = 0; /* The "docid >= ?" constraint, if any */
180058 sqlite3_value *pDocidLe = 0; /* The "docid <= ?" constraint, if any */
180059 int iIdx;
180060
180061 UNUSED_PARAMETER(idxStr)(void)(idxStr);
180062 UNUSED_PARAMETER(nVal)(void)(nVal);
180063
180064 if( p->bLock ){
180065 return SQLITE_ERROR1;
180066 }
180067
180068 eSearch = (idxNum & 0x0000FFFF);
180069 assert( eSearch>=0 && eSearch<=(FTS3_FULLTEXT_SEARCH+p->nColumn) )((void) (0));
180070 assert( p->pSegments==0 )((void) (0));
180071
180072 /* Collect arguments into local variables */
180073 iIdx = 0;
180074 if( eSearch!=FTS3_FULLSCAN_SEARCH0 ) pCons = apVal[iIdx++];
180075 if( idxNum & FTS3_HAVE_LANGID0x00010000 ) pLangid = apVal[iIdx++];
180076 if( idxNum & FTS3_HAVE_DOCID_GE0x00020000 ) pDocidGe = apVal[iIdx++];
180077 if( idxNum & FTS3_HAVE_DOCID_LE0x00040000 ) pDocidLe = apVal[iIdx++];
180078 assert( iIdx==nVal )((void) (0));
180079
180080 /* In case the cursor has been used before, clear it now. */
180081 fts3ClearCursor(pCsr);
180082
180083 /* Set the lower and upper bounds on docids to return */
180084 pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))));
180085 pCsr->iMaxDocid = fts3DocidRange(pDocidLe, LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)));
180086
180087 if( idxStr ){
180088 pCsr->bDesc = (idxStr[0]=='D');
180089 }else{
180090 pCsr->bDesc = p->bDescIdx;
180091 }
180092 pCsr->eSearch = (i16)eSearch;
180093
180094 if( eSearch!=FTS3_DOCID_SEARCH1 && eSearch!=FTS3_FULLSCAN_SEARCH0 ){
180095 int iCol = eSearch-FTS3_FULLTEXT_SEARCH2;
180096 const char *zQuery = (const char *)sqlite3_value_text(pCons);
180097
180098 if( zQuery==0 && sqlite3_value_type(pCons)!=SQLITE_NULL5 ){
180099 return SQLITE_NOMEM7;
180100 }
180101
180102 pCsr->iLangid = 0;
180103 if( pLangid ) pCsr->iLangid = sqlite3_value_int(pLangid);
180104
180105 assert( p->base.zErrMsg==0 )((void) (0));
180106 rc = sqlite3Fts3ExprParse(p->pTokenizer, pCsr->iLangid,
180107 p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr,
180108 &p->base.zErrMsg
180109 );
180110 if( rc!=SQLITE_OK0 ){
180111 return rc;
180112 }
180113
180114 rc = fts3EvalStart(pCsr);
180115 sqlite3Fts3SegmentsClose(p);
180116 if( rc!=SQLITE_OK0 ) return rc;
180117 pCsr->pNextId = pCsr->aDoclist;
180118 pCsr->iPrevId = 0;
180119 }
180120
180121 /* Compile a SELECT statement for this cursor. For a full-table-scan, the
180122 ** statement loops through all rows of the %_content table. For a
180123 ** full-text query or docid lookup, the statement retrieves a single
180124 ** row by docid.
180125 */
180126 if( eSearch==FTS3_FULLSCAN_SEARCH0 ){
180127 if( pDocidGe || pDocidLe ){
180128 zSql = sqlite3_mprintf(
180129 "SELECT %s WHERE rowid BETWEEN %lld AND %lld ORDER BY rowid %s",
180130 p->zReadExprlist, pCsr->iMinDocid, pCsr->iMaxDocid,
180131 (pCsr->bDesc ? "DESC" : "ASC")
180132 );
180133 }else{
180134 zSql = sqlite3_mprintf("SELECT %s ORDER BY rowid %s",
180135 p->zReadExprlist, (pCsr->bDesc ? "DESC" : "ASC")
180136 );
180137 }
180138 if( zSql ){
180139 p->bLock++;
180140 rc = sqlite3_prepare_v3(
180141 p->db,zSql,-1,SQLITE_PREPARE_PERSISTENT0x01,&pCsr->pStmt,0
180142 );
180143 p->bLock--;
180144 sqlite3_free(zSql);
180145 }else{
180146 rc = SQLITE_NOMEM7;
180147 }
180148 }else if( eSearch==FTS3_DOCID_SEARCH1 ){
180149 rc = fts3CursorSeekStmt(pCsr);
180150 if( rc==SQLITE_OK0 ){
180151 rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons);
180152 }
180153 }
180154 if( rc!=SQLITE_OK0 ) return rc;
180155
180156 return fts3NextMethod(pCursor);
180157}
180158
180159/*
180160** This is the xEof method of the virtual table. SQLite calls this
180161** routine to find out if it has reached the end of a result set.
180162*/
180163static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){
180164 Fts3Cursor *pCsr = (Fts3Cursor*)pCursor;
180165 if( pCsr->isEof ){
180166 fts3ClearCursor(pCsr);
180167 pCsr->isEof = 1;
180168 }
180169 return pCsr->isEof;
180170}
180171
180172/*
180173** This is the xRowid method. The SQLite core calls this routine to
180174** retrieve the rowid for the current row of the result set. fts3
180175** exposes %_content.docid as the rowid for the virtual table. The
180176** rowid should be written to *pRowid.
180177*/
180178static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
180179 Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
180180 *pRowid = pCsr->iPrevId;
180181 return SQLITE_OK0;
180182}
180183
180184/*
180185** This is the xColumn method, called by SQLite to request a value from
180186** the row that the supplied cursor currently points to.
180187**
180188** If:
180189**
180190** (iCol < p->nColumn) -> The value of the iCol'th user column.
180191** (iCol == p->nColumn) -> Magic column with the same name as the table.
180192** (iCol == p->nColumn+1) -> Docid column
180193** (iCol == p->nColumn+2) -> Langid column
180194*/
180195static int fts3ColumnMethod(
180196 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
180197 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
180198 int iCol /* Index of column to read value from */
180199){
180200 int rc = SQLITE_OK0; /* Return Code */
180201 Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
180202 Fts3Table *p = (Fts3Table *)pCursor->pVtab;
180203
180204 /* The column value supplied by SQLite must be in range. */
180205 assert( iCol>=0 && iCol<=p->nColumn+2 )((void) (0));
180206
180207 switch( iCol-p->nColumn ){
180208 case 0:
180209 /* The special 'table-name' column */
180210 sqlite3_result_pointer(pCtx, pCsr, "fts3cursor", 0);
180211 break;
180212
180213 case 1:
180214 /* The docid column */
180215 sqlite3_result_int64(pCtx, pCsr->iPrevId);
180216 break;
180217
180218 case 2:
180219 if( pCsr->pExpr ){
180220 sqlite3_result_int64(pCtx, pCsr->iLangid);
180221 break;
180222 }else if( p->zLanguageid==0 ){
180223 sqlite3_result_int(pCtx, 0);
180224 break;
180225 }else{
180226 iCol = p->nColumn;
180227 /* no break */ deliberate_fall_through
180228 }
180229
180230 default:
180231 /* A user column. Or, if this is a full-table scan, possibly the
180232 ** language-id column. Seek the cursor. */
180233 rc = fts3CursorSeek(0, pCsr);
180234 if( rc==SQLITE_OK0 && sqlite3_data_count(pCsr->pStmt)-1>iCol ){
180235 sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1));
180236 }
180237 break;
180238 }
180239
180240 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 )((void) (0));
180241 return rc;
180242}
180243
180244/*
180245** This function is the implementation of the xUpdate callback used by
180246** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
180247** inserted, updated or deleted.
180248*/
180249static int fts3UpdateMethod(
180250 sqlite3_vtab *pVtab, /* Virtual table handle */
180251 int nArg, /* Size of argument array */
180252 sqlite3_value **apVal, /* Array of arguments */
180253 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
180254){
180255 return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid);
180256}
180257
180258/*
180259** Implementation of xSync() method. Flush the contents of the pending-terms
180260** hash-table to the database.
180261*/
180262static int fts3SyncMethod(sqlite3_vtab *pVtab){
180263
180264 /* Following an incremental-merge operation, assuming that the input
180265 ** segments are not completely consumed (the usual case), they are updated
180266 ** in place to remove the entries that have already been merged. This
180267 ** involves updating the leaf block that contains the smallest unmerged
180268 ** entry and each block (if any) between the leaf and the root node. So
180269 ** if the height of the input segment b-trees is N, and input segments
180270 ** are merged eight at a time, updating the input segments at the end
180271 ** of an incremental-merge requires writing (8*(1+N)) blocks. N is usually
180272 ** small - often between 0 and 2. So the overhead of the incremental
180273 ** merge is somewhere between 8 and 24 blocks. To avoid this overhead
180274 ** dwarfing the actual productive work accomplished, the incremental merge
180275 ** is only attempted if it will write at least 64 leaf blocks. Hence
180276 ** nMinMerge.
180277 **
180278 ** Of course, updating the input segments also involves deleting a bunch
180279 ** of blocks from the segments table. But this is not considered overhead
180280 ** as it would also be required by a crisis-merge that used the same input
180281 ** segments.
180282 */
180283 const u32 nMinMerge = 64; /* Minimum amount of incr-merge work to do */
180284
180285 Fts3Table *p = (Fts3Table*)pVtab;
180286 int rc;
180287 i64 iLastRowid = sqlite3_last_insert_rowid(p->db);
180288
180289 rc = sqlite3Fts3PendingTermsFlush(p);
180290 if( rc==SQLITE_OK0
180291 && p->nLeafAdd>(nMinMerge/16)
180292 && p->nAutoincrmerge && p->nAutoincrmerge!=0xff
180293 ){
180294 int mxLevel = 0; /* Maximum relative level value in db */
180295 int A; /* Incr-merge parameter A */
180296
180297 rc = sqlite3Fts3MaxLevel(p, &mxLevel);
180298 assert( rc==SQLITE_OK || mxLevel==0 )((void) (0));
180299 A = p->nLeafAdd * mxLevel;
180300 A += (A/2);
180301 if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge);
180302 }
180303 sqlite3Fts3SegmentsClose(p);
180304 sqlite3_set_last_insert_rowid(p->db, iLastRowid);
180305 return rc;
180306}
180307
180308/*
180309** If it is currently unknown whether or not the FTS table has an %_stat
180310** table (if p->bHasStat==2), attempt to determine this (set p->bHasStat
180311** to 0 or 1). Return SQLITE_OK if successful, or an SQLite error code
180312** if an error occurs.
180313*/
180314static int fts3SetHasStat(Fts3Table *p){
180315 int rc = SQLITE_OK0;
180316 if( p->bHasStat==2 ){
180317 char *zTbl = sqlite3_mprintf("%s_stat", p->zName);
180318 if( zTbl ){
180319 int res = sqlite3_table_column_metadata(p->db, p->zDb, zTbl, 0,0,0,0,0,0);
180320 sqlite3_free(zTbl);
180321 p->bHasStat = (res==SQLITE_OK0);
180322 }else{
180323 rc = SQLITE_NOMEM7;
180324 }
180325 }
180326 return rc;
180327}
180328
180329/*
180330** Implementation of xBegin() method.
180331*/
180332static int fts3BeginMethod(sqlite3_vtab *pVtab){
180333 Fts3Table *p = (Fts3Table*)pVtab;
180334 int rc;
180335 UNUSED_PARAMETER(pVtab)(void)(pVtab);
180336 assert( p->pSegments==0 )((void) (0));
180337 assert( p->nPendingData==0 )((void) (0));
180338 assert( p->inTransaction!=1 )((void) (0));
180339 p->nLeafAdd = 0;
180340 rc = fts3SetHasStat(p);
180341#ifdef SQLITE_DEBUG
180342 if( rc==SQLITE_OK0 ){
180343 p->inTransaction = 1;
180344 p->mxSavepoint = -1;
180345 }
180346#endif
180347 return rc;
180348}
180349
180350/*
180351** Implementation of xCommit() method. This is a no-op. The contents of
180352** the pending-terms hash-table have already been flushed into the database
180353** by fts3SyncMethod().
180354*/
180355static int fts3CommitMethod(sqlite3_vtab *pVtab){
180356 TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
180357 UNUSED_PARAMETER(pVtab)(void)(pVtab);
180358 assert( p->nPendingData==0 )((void) (0));
180359 assert( p->inTransaction!=0 )((void) (0));
180360 assert( p->pSegments==0 )((void) (0));
180361 TESTONLY( p->inTransaction = 0 );
180362 TESTONLY( p->mxSavepoint = -1; );
180363 return SQLITE_OK0;
180364}
180365
180366/*
180367** Implementation of xRollback(). Discard the contents of the pending-terms
180368** hash-table. Any changes made to the database are reverted by SQLite.
180369*/
180370static int fts3RollbackMethod(sqlite3_vtab *pVtab){
180371 Fts3Table *p = (Fts3Table*)pVtab;
180372 sqlite3Fts3PendingTermsClear(p);
180373 assert( p->inTransaction!=0 )((void) (0));
180374 TESTONLY( p->inTransaction = 0 );
180375 TESTONLY( p->mxSavepoint = -1; );
180376 return SQLITE_OK0;
180377}
180378
180379/*
180380** When called, *ppPoslist must point to the byte immediately following the
180381** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function
180382** moves *ppPoslist so that it instead points to the first byte of the
180383** same position list.
180384*/
180385static void fts3ReversePoslist(char *pStart, char **ppPoslist){
180386 char *p = &(*ppPoslist)[-2];
180387 char c = 0;
180388
180389 /* Skip backwards passed any trailing 0x00 bytes added by NearTrim() */
180390 while( p>pStart && (c=*p--)==0 );
180391
180392 /* Search backwards for a varint with value zero (the end of the previous
180393 ** poslist). This is an 0x00 byte preceded by some byte that does not
180394 ** have the 0x80 bit set. */
180395 while( p>pStart && (*p & 0x80) | c ){
180396 c = *p--;
180397 }
180398 assert( p==pStart || c==0 )((void) (0));
180399
180400 /* At this point p points to that preceding byte without the 0x80 bit
180401 ** set. So to find the start of the poslist, skip forward 2 bytes then
180402 ** over a varint.
180403 **
180404 ** Normally. The other case is that p==pStart and the poslist to return
180405 ** is the first in the doclist. In this case do not skip forward 2 bytes.
180406 ** The second part of the if condition (c==0 && *ppPoslist>&p[2])
180407 ** is required for cases where the first byte of a doclist and the
180408 ** doclist is empty. For example, if the first docid is 10, a doclist
180409 ** that begins with:
180410 **
180411 ** 0x0A 0x00 <next docid delta varint>
180412 */
180413 if( p>pStart || (c==0 && *ppPoslist>&p[2]) ){ p = &p[2]; }
180414 while( *p++&0x80 );
180415 *ppPoslist = p;
180416}
180417
180418/*
180419** Helper function used by the implementation of the overloaded snippet(),
180420** offsets() and optimize() SQL functions.
180421**
180422** If the value passed as the third argument is a blob of size
180423** sizeof(Fts3Cursor*), then the blob contents are copied to the
180424** output variable *ppCsr and SQLITE_OK is returned. Otherwise, an error
180425** message is written to context pContext and SQLITE_ERROR returned. The
180426** string passed via zFunc is used as part of the error message.
180427*/
180428static int fts3FunctionArg(
180429 sqlite3_context *pContext, /* SQL function call context */
180430 const char *zFunc, /* Function name */
180431 sqlite3_value *pVal, /* argv[0] passed to function */
180432 Fts3Cursor **ppCsr /* OUT: Store cursor handle here */
180433){
180434 int rc;
180435 *ppCsr = (Fts3Cursor*)sqlite3_value_pointer(pVal, "fts3cursor");
180436 if( (*ppCsr)!=0 ){
180437 rc = SQLITE_OK0;
180438 }else{
180439 char *zErr = sqlite3_mprintf("illegal first argument to %s", zFunc);
180440 sqlite3_result_error(pContext, zErr, -1);
180441 sqlite3_free(zErr);
180442 rc = SQLITE_ERROR1;
180443 }
180444 return rc;
180445}
180446
180447/*
180448** Implementation of the snippet() function for FTS3
180449*/
180450static void fts3SnippetFunc(
180451 sqlite3_context *pContext, /* SQLite function call context */
180452 int nVal, /* Size of apVal[] array */
180453 sqlite3_value **apVal /* Array of arguments */
180454){
180455 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
180456 const char *zStart = "<b>";
180457 const char *zEnd = "</b>";
180458 const char *zEllipsis = "<b>...</b>";
180459 int iCol = -1;
180460 int nToken = 15; /* Default number of tokens in snippet */
180461
180462 /* There must be at least one argument passed to this function (otherwise
180463 ** the non-overloaded version would have been called instead of this one).
180464 */
180465 assert( nVal>=1 )((void) (0));
180466
180467 if( nVal>6 ){
180468 sqlite3_result_error(pContext,
180469 "wrong number of arguments to function snippet()", -1);
180470 return;
180471 }
180472 if( fts3FunctionArg(pContext, "snippet", apVal[0], &pCsr) ) return;
180473
180474 switch( nVal ){
180475 case 6: nToken = sqlite3_value_int(apVal[5]);
180476 /* no break */ deliberate_fall_through
180477 case 5: iCol = sqlite3_value_int(apVal[4]);
180478 /* no break */ deliberate_fall_through
180479 case 4: zEllipsis = (const char*)sqlite3_value_text(apVal[3]);
180480 /* no break */ deliberate_fall_through
180481 case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]);
180482 /* no break */ deliberate_fall_through
180483 case 2: zStart = (const char*)sqlite3_value_text(apVal[1]);
180484 }
180485 if( !zEllipsis || !zEnd || !zStart ){
180486 sqlite3_result_error_nomem(pContext);
180487 }else if( nToken==0 ){
180488 sqlite3_result_text(pContext, "", -1, SQLITE_STATIC((sqlite3_destructor_type)0));
180489 }else if( SQLITE_OK0==fts3CursorSeek(pContext, pCsr) ){
180490 sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken);
180491 }
180492}
180493
180494/*
180495** Implementation of the offsets() function for FTS3
180496*/
180497static void fts3OffsetsFunc(
180498 sqlite3_context *pContext, /* SQLite function call context */
180499 int nVal, /* Size of argument array */
180500 sqlite3_value **apVal /* Array of arguments */
180501){
180502 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
180503
180504 UNUSED_PARAMETER(nVal)(void)(nVal);
180505
180506 assert( nVal==1 )((void) (0));
180507 if( fts3FunctionArg(pContext, "offsets", apVal[0], &pCsr) ) return;
180508 assert( pCsr )((void) (0));
180509 if( SQLITE_OK0==fts3CursorSeek(pContext, pCsr) ){
180510 sqlite3Fts3Offsets(pContext, pCsr);
180511 }
180512}
180513
180514/*
180515** Implementation of the special optimize() function for FTS3. This
180516** function merges all segments in the database to a single segment.
180517** Example usage is:
180518**
180519** SELECT optimize(t) FROM t LIMIT 1;
180520**
180521** where 't' is the name of an FTS3 table.
180522*/
180523static void fts3OptimizeFunc(
180524 sqlite3_context *pContext, /* SQLite function call context */
180525 int nVal, /* Size of argument array */
180526 sqlite3_value **apVal /* Array of arguments */
180527){
180528 int rc; /* Return code */
180529 Fts3Table *p; /* Virtual table handle */
180530 Fts3Cursor *pCursor; /* Cursor handle passed through apVal[0] */
180531
180532 UNUSED_PARAMETER(nVal)(void)(nVal);
180533
180534 assert( nVal==1 )((void) (0));
180535 if( fts3FunctionArg(pContext, "optimize", apVal[0], &pCursor) ) return;
180536 p = (Fts3Table *)pCursor->base.pVtab;
180537 assert( p )((void) (0));
180538
180539 rc = sqlite3Fts3Optimize(p);
180540
180541 switch( rc ){
180542 case SQLITE_OK0:
180543 sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC((sqlite3_destructor_type)0));
180544 break;
180545 case SQLITE_DONE101:
180546 sqlite3_result_text(pContext, "Index already optimal", -1, SQLITE_STATIC((sqlite3_destructor_type)0));
180547 break;
180548 default:
180549 sqlite3_result_error_code(pContext, rc);
180550 break;
180551 }
180552}
180553
180554/*
180555** Implementation of the matchinfo() function for FTS3
180556*/
180557static void fts3MatchinfoFunc(
180558 sqlite3_context *pContext, /* SQLite function call context */
180559 int nVal, /* Size of argument array */
180560 sqlite3_value **apVal /* Array of arguments */
180561){
180562 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
180563 assert( nVal==1 || nVal==2 )((void) (0));
180564 if( SQLITE_OK0==fts3FunctionArg(pContext, "matchinfo", apVal[0], &pCsr) ){
180565 const char *zArg = 0;
180566 if( nVal>1 ){
180567 zArg = (const char *)sqlite3_value_text(apVal[1]);
180568 }
180569 sqlite3Fts3Matchinfo(pContext, pCsr, zArg);
180570 }
180571}
180572
180573/*
180574** This routine implements the xFindFunction method for the FTS3
180575** virtual table.
180576*/
180577static int fts3FindFunctionMethod(
180578 sqlite3_vtab *pVtab, /* Virtual table handle */
180579 int nArg, /* Number of SQL function arguments */
180580 const char *zName, /* Name of SQL function */
180581 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
180582 void **ppArg /* Unused */
180583){
180584 struct Overloaded {
180585 const char *zName;
180586 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
180587 } aOverload[] = {
180588 { "snippet", fts3SnippetFunc },
180589 { "offsets", fts3OffsetsFunc },
180590 { "optimize", fts3OptimizeFunc },
180591 { "matchinfo", fts3MatchinfoFunc },
180592 };
180593 int i; /* Iterator variable */
180594
180595 UNUSED_PARAMETER(pVtab)(void)(pVtab);
180596 UNUSED_PARAMETER(nArg)(void)(nArg);
180597 UNUSED_PARAMETER(ppArg)(void)(ppArg);
180598
180599 for(i=0; i<SizeofArray(aOverload)((int)(sizeof(aOverload)/sizeof(aOverload[0]))); i++){
180600 if( strcmp(zName, aOverload[i].zName)==0 ){
180601 *pxFunc = aOverload[i].xFunc;
180602 return 1;
180603 }
180604 }
180605
180606 /* No function of the specified name was found. Return 0. */
180607 return 0;
180608}
180609
180610/*
180611** Implementation of FTS3 xRename method. Rename an fts3 table.
180612*/
180613static int fts3RenameMethod(
180614 sqlite3_vtab *pVtab, /* Virtual table handle */
180615 const char *zName /* New name of table */
180616){
180617 Fts3Table *p = (Fts3Table *)pVtab;
180618 sqlite3 *db = p->db; /* Database connection */
180619 int rc; /* Return Code */
180620
180621 /* At this point it must be known if the %_stat table exists or not.
180622 ** So bHasStat may not be 2. */
180623 rc = fts3SetHasStat(p);
180624
180625 /* As it happens, the pending terms table is always empty here. This is
180626 ** because an "ALTER TABLE RENAME TABLE" statement inside a transaction
180627 ** always opens a savepoint transaction. And the xSavepoint() method
180628 ** flushes the pending terms table. But leave the (no-op) call to
180629 ** PendingTermsFlush() in in case that changes.
180630 */
180631 assert( p->nPendingData==0 )((void) (0));
180632 if( rc==SQLITE_OK0 ){
180633 rc = sqlite3Fts3PendingTermsFlush(p);
180634 }
180635
180636 if( p->zContentTbl==0 ){
180637 fts3DbExec(&rc, db,
180638 "ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';",
180639 p->zDb, p->zName, zName
180640 );
180641 }
180642
180643 if( p->bHasDocsize ){
180644 fts3DbExec(&rc, db,
180645 "ALTER TABLE %Q.'%q_docsize' RENAME TO '%q_docsize';",
180646 p->zDb, p->zName, zName
180647 );
180648 }
180649 if( p->bHasStat ){
180650 fts3DbExec(&rc, db,
180651 "ALTER TABLE %Q.'%q_stat' RENAME TO '%q_stat';",
180652 p->zDb, p->zName, zName
180653 );
180654 }
180655 fts3DbExec(&rc, db,
180656 "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';",
180657 p->zDb, p->zName, zName
180658 );
180659 fts3DbExec(&rc, db,
180660 "ALTER TABLE %Q.'%q_segdir' RENAME TO '%q_segdir';",
180661 p->zDb, p->zName, zName
180662 );
180663 return rc;
180664}
180665
180666/*
180667** The xSavepoint() method.
180668**
180669** Flush the contents of the pending-terms table to disk.
180670*/
180671static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
180672 int rc = SQLITE_OK0;
180673 UNUSED_PARAMETER(iSavepoint)(void)(iSavepoint);
180674 assert( ((Fts3Table *)pVtab)->inTransaction )((void) (0));
180675 assert( ((Fts3Table *)pVtab)->mxSavepoint <= iSavepoint )((void) (0));
180676 TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint );
180677 if( ((Fts3Table *)pVtab)->bIgnoreSavepoint==0 ){
180678 rc = fts3SyncMethod(pVtab);
180679 }
180680 return rc;
180681}
180682
180683/*
180684** The xRelease() method.
180685**
180686** This is a no-op.
180687*/
180688static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
180689 TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
180690 UNUSED_PARAMETER(iSavepoint)(void)(iSavepoint);
180691 UNUSED_PARAMETER(pVtab)(void)(pVtab);
180692 assert( p->inTransaction )((void) (0));
180693 assert( p->mxSavepoint >= iSavepoint )((void) (0));
180694 TESTONLY( p->mxSavepoint = iSavepoint-1 );
180695 return SQLITE_OK0;
180696}
180697
180698/*
180699** The xRollbackTo() method.
180700**
180701** Discard the contents of the pending terms table.
180702*/
180703static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
180704 Fts3Table *p = (Fts3Table*)pVtab;
180705 UNUSED_PARAMETER(iSavepoint)(void)(iSavepoint);
180706 assert( p->inTransaction )((void) (0));
180707 TESTONLY( p->mxSavepoint = iSavepoint );
180708 sqlite3Fts3PendingTermsClear(p);
180709 return SQLITE_OK0;
180710}
180711
180712/*
180713** Return true if zName is the extension on one of the shadow tables used
180714** by this module.
180715*/
180716static int fts3ShadowName(const char *zName){
180717 static const char *azName[] = {
180718 "content", "docsize", "segdir", "segments", "stat",
180719 };
180720 unsigned int i;
180721 for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
180722 if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
180723 }
180724 return 0;
180725}
180726
180727static const sqlite3_module fts3Module = {
180728 /* iVersion */ 3,
180729 /* xCreate */ fts3CreateMethod,
180730 /* xConnect */ fts3ConnectMethod,
180731 /* xBestIndex */ fts3BestIndexMethod,
180732 /* xDisconnect */ fts3DisconnectMethod,
180733 /* xDestroy */ fts3DestroyMethod,
180734 /* xOpen */ fts3OpenMethod,
180735 /* xClose */ fts3CloseMethod,
180736 /* xFilter */ fts3FilterMethod,
180737 /* xNext */ fts3NextMethod,
180738 /* xEof */ fts3EofMethod,
180739 /* xColumn */ fts3ColumnMethod,
180740 /* xRowid */ fts3RowidMethod,
180741 /* xUpdate */ fts3UpdateMethod,
180742 /* xBegin */ fts3BeginMethod,
180743 /* xSync */ fts3SyncMethod,
180744 /* xCommit */ fts3CommitMethod,
180745 /* xRollback */ fts3RollbackMethod,
180746 /* xFindFunction */ fts3FindFunctionMethod,
180747 /* xRename */ fts3RenameMethod,
180748 /* xSavepoint */ fts3SavepointMethod,
180749 /* xRelease */ fts3ReleaseMethod,
180750 /* xRollbackTo */ fts3RollbackToMethod,
180751 /* xShadowName */ fts3ShadowName,
180752};
180753
180754/*
180755** This function is registered as the module destructor (called when an
180756** FTS3 enabled database connection is closed). It frees the memory
180757** allocated for the tokenizer hash table.
180758*/
180759static void hashDestroy(void *p){
180760 Fts3HashWrapper *pHash = (Fts3HashWrapper *)p;
180761 pHash->nRef--;
180762 if( pHash->nRef<=0 ){
180763 sqlite3Fts3HashClear(&pHash->hash);
180764 sqlite3_free(pHash);
180765 }
180766}
180767
180768/*
180769** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are
180770** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c
180771** respectively. The following three forward declarations are for functions
180772** declared in these files used to retrieve the respective implementations.
180773**
180774** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
180775** to by the argument to point to the "simple" tokenizer implementation.
180776** And so on.
180777*/
180778SQLITE_PRIVATEstatic void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
180779SQLITE_PRIVATEstatic void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
180780#ifndef SQLITE_DISABLE_FTS3_UNICODE
180781SQLITE_PRIVATEstatic void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const**ppModule);
180782#endif
180783#ifdef SQLITE_ENABLE_ICU
180784SQLITE_PRIVATEstatic void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
180785#endif
180786
180787/*
180788** Initialize the fts3 extension. If this extension is built as part
180789** of the sqlite library, then this function is called directly by
180790** SQLite. If fts3 is built as a dynamically loadable extension, this
180791** function is called by the sqlite3_extension_init() entry point.
180792*/
180793SQLITE_PRIVATEstatic int sqlite3Fts3Init(sqlite3 *db){
180794 int rc = SQLITE_OK0;
180795 Fts3HashWrapper *pHash = 0;
180796 const sqlite3_tokenizer_module *pSimple = 0;
180797 const sqlite3_tokenizer_module *pPorter = 0;
180798#ifndef SQLITE_DISABLE_FTS3_UNICODE
180799 const sqlite3_tokenizer_module *pUnicode = 0;
180800#endif
180801
180802#ifdef SQLITE_ENABLE_ICU
180803 const sqlite3_tokenizer_module *pIcu = 0;
180804 sqlite3Fts3IcuTokenizerModule(&pIcu);
180805#endif
180806
180807#ifndef SQLITE_DISABLE_FTS3_UNICODE
180808 sqlite3Fts3UnicodeTokenizer(&pUnicode);
180809#endif
180810
180811#ifdef SQLITE_TEST
180812 rc = sqlite3Fts3InitTerm(db);
180813 if( rc!=SQLITE_OK0 ) return rc;
180814#endif
180815
180816 rc = sqlite3Fts3InitAux(db);
180817 if( rc!=SQLITE_OK0 ) return rc;
180818
180819 sqlite3Fts3SimpleTokenizerModule(&pSimple);
180820 sqlite3Fts3PorterTokenizerModule(&pPorter);
180821
180822 /* Allocate and initialize the hash-table used to store tokenizers. */
180823 pHash = sqlite3_malloc(sizeof(Fts3HashWrapper));
180824 if( !pHash ){
180825 rc = SQLITE_NOMEM7;
180826 }else{
180827 sqlite3Fts3HashInit(&pHash->hash, FTS3_HASH_STRING1, 1);
180828 pHash->nRef = 0;
180829 }
180830
180831 /* Load the built-in tokenizers into the hash table */
180832 if( rc==SQLITE_OK0 ){
180833 if( sqlite3Fts3HashInsert(&pHash->hash, "simple", 7, (void *)pSimple)
180834 || sqlite3Fts3HashInsert(&pHash->hash, "porter", 7, (void *)pPorter)
180835
180836#ifndef SQLITE_DISABLE_FTS3_UNICODE
180837 || sqlite3Fts3HashInsert(&pHash->hash, "unicode61", 10, (void *)pUnicode)
180838#endif
180839#ifdef SQLITE_ENABLE_ICU
180840 || (pIcu && sqlite3Fts3HashInsert(&pHash->hash, "icu", 4, (void *)pIcu))
180841#endif
180842 ){
180843 rc = SQLITE_NOMEM7;
180844 }
180845 }
180846
180847#ifdef SQLITE_TEST
180848 if( rc==SQLITE_OK0 ){
180849 rc = sqlite3Fts3ExprInitTestInterface(db, &pHash->hash);
180850 }
180851#endif
180852
180853 /* Create the virtual table wrapper around the hash-table and overload
180854 ** the four scalar functions. If this is successful, register the
180855 ** module with sqlite.
180856 */
180857 if( SQLITE_OK0==rc
180858 && SQLITE_OK0==(rc=sqlite3Fts3InitHashTable(db,&pHash->hash,"fts3_tokenizer"))
180859 && SQLITE_OK0==(rc = sqlite3_overload_function(db, "snippet", -1))
180860 && SQLITE_OK0==(rc = sqlite3_overload_function(db, "offsets", 1))
180861 && SQLITE_OK0==(rc = sqlite3_overload_function(db, "matchinfo", 1))
180862 && SQLITE_OK0==(rc = sqlite3_overload_function(db, "matchinfo", 2))
180863 && SQLITE_OK0==(rc = sqlite3_overload_function(db, "optimize", 1))
180864 ){
180865 pHash->nRef++;
180866 rc = sqlite3_create_module_v2(
180867 db, "fts3", &fts3Module, (void *)pHash, hashDestroy
180868 );
180869 if( rc==SQLITE_OK0 ){
180870 pHash->nRef++;
180871 rc = sqlite3_create_module_v2(
180872 db, "fts4", &fts3Module, (void *)pHash, hashDestroy
180873 );
180874 }
180875 if( rc==SQLITE_OK0 ){
180876 pHash->nRef++;
180877 rc = sqlite3Fts3InitTok(db, (void *)pHash, hashDestroy);
180878 }
180879 return rc;
180880 }
180881
180882
180883 /* An error has occurred. Delete the hash table and return the error code. */
180884 assert( rc!=SQLITE_OK )((void) (0));
180885 if( pHash ){
180886 sqlite3Fts3HashClear(&pHash->hash);
180887 sqlite3_free(pHash);
180888 }
180889 return rc;
180890}
180891
180892/*
180893** Allocate an Fts3MultiSegReader for each token in the expression headed
180894** by pExpr.
180895**
180896** An Fts3SegReader object is a cursor that can seek or scan a range of
180897** entries within a single segment b-tree. An Fts3MultiSegReader uses multiple
180898** Fts3SegReader objects internally to provide an interface to seek or scan
180899** within the union of all segments of a b-tree. Hence the name.
180900**
180901** If the allocated Fts3MultiSegReader just seeks to a single entry in a
180902** segment b-tree (if the term is not a prefix or it is a prefix for which
180903** there exists prefix b-tree of the right length) then it may be traversed
180904** and merged incrementally. Otherwise, it has to be merged into an in-memory
180905** doclist and then traversed.
180906*/
180907static void fts3EvalAllocateReaders(
180908 Fts3Cursor *pCsr, /* FTS cursor handle */
180909 Fts3Expr *pExpr, /* Allocate readers for this expression */
180910 int *pnToken, /* OUT: Total number of tokens in phrase. */
180911 int *pnOr, /* OUT: Total number of OR nodes in expr. */
180912 int *pRc /* IN/OUT: Error code */
180913){
180914 if( pExpr && SQLITE_OK0==*pRc ){
180915 if( pExpr->eType==FTSQUERY_PHRASE5 ){
180916 int i;
180917 int nToken = pExpr->pPhrase->nToken;
180918 *pnToken += nToken;
180919 for(i=0; i<nToken; i++){
180920 Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i];
180921 int rc = fts3TermSegReaderCursor(pCsr,
180922 pToken->z, pToken->n, pToken->isPrefix, &pToken->pSegcsr
180923 );
180924 if( rc!=SQLITE_OK0 ){
180925 *pRc = rc;
180926 return;
180927 }
180928 }
180929 assert( pExpr->pPhrase->iDoclistToken==0 )((void) (0));
180930 pExpr->pPhrase->iDoclistToken = -1;
180931 }else{
180932 *pnOr += (pExpr->eType==FTSQUERY_OR4);
180933 fts3EvalAllocateReaders(pCsr, pExpr->pLeft, pnToken, pnOr, pRc);
180934 fts3EvalAllocateReaders(pCsr, pExpr->pRight, pnToken, pnOr, pRc);
180935 }
180936 }
180937}
180938
180939/*
180940** Arguments pList/nList contain the doclist for token iToken of phrase p.
180941** It is merged into the main doclist stored in p->doclist.aAll/nAll.
180942**
180943** This function assumes that pList points to a buffer allocated using
180944** sqlite3_malloc(). This function takes responsibility for eventually
180945** freeing the buffer.
180946**
180947** SQLITE_OK is returned if successful, or SQLITE_NOMEM if an error occurs.
180948*/
180949static int fts3EvalPhraseMergeToken(
180950 Fts3Table *pTab, /* FTS Table pointer */
180951 Fts3Phrase *p, /* Phrase to merge pList/nList into */
180952 int iToken, /* Token pList/nList corresponds to */
180953 char *pList, /* Pointer to doclist */
180954 int nList /* Number of bytes in pList */
180955){
180956 int rc = SQLITE_OK0;
180957 assert( iToken!=p->iDoclistToken )((void) (0));
180958
180959 if( pList==0 ){
180960 sqlite3_free(p->doclist.aAll);
180961 p->doclist.aAll = 0;
180962 p->doclist.nAll = 0;
180963 }
180964
180965 else if( p->iDoclistToken<0 ){
180966 p->doclist.aAll = pList;
180967 p->doclist.nAll = nList;
180968 }
180969
180970 else if( p->doclist.aAll==0 ){
180971 sqlite3_free(pList);
180972 }
180973
180974 else {
180975 char *pLeft;
180976 char *pRight;
180977 int nLeft;
180978 int nRight;
180979 int nDiff;
180980
180981 if( p->iDoclistToken<iToken ){
180982 pLeft = p->doclist.aAll;
180983 nLeft = p->doclist.nAll;
180984 pRight = pList;
180985 nRight = nList;
180986 nDiff = iToken - p->iDoclistToken;
180987 }else{
180988 pRight = p->doclist.aAll;
180989 nRight = p->doclist.nAll;
180990 pLeft = pList;
180991 nLeft = nList;
180992 nDiff = p->iDoclistToken - iToken;
180993 }
180994
180995 rc = fts3DoclistPhraseMerge(
180996 pTab->bDescIdx, nDiff, pLeft, nLeft, &pRight, &nRight
180997 );
180998 sqlite3_free(pLeft);
180999 p->doclist.aAll = pRight;
181000 p->doclist.nAll = nRight;
181001 }
181002
181003 if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
181004 return rc;
181005}
181006
181007/*
181008** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist
181009** does not take deferred tokens into account.
181010**
181011** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
181012*/
181013static int fts3EvalPhraseLoad(
181014 Fts3Cursor *pCsr, /* FTS Cursor handle */
181015 Fts3Phrase *p /* Phrase object */
181016){
181017 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
181018 int iToken;
181019 int rc = SQLITE_OK0;
181020
181021 for(iToken=0; rc==SQLITE_OK0 && iToken<p->nToken; iToken++){
181022 Fts3PhraseToken *pToken = &p->aToken[iToken];
181023 assert( pToken->pDeferred==0 || pToken->pSegcsr==0 )((void) (0));
181024
181025 if( pToken->pSegcsr ){
181026 int nThis = 0;
181027 char *pThis = 0;
181028 rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis);
181029 if( rc==SQLITE_OK0 ){
181030 rc = fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
181031 }
181032 }
181033 assert( pToken->pSegcsr==0 )((void) (0));
181034 }
181035
181036 return rc;
181037}
181038
181039#ifndef SQLITE_DISABLE_FTS4_DEFERRED
181040/*
181041** This function is called on each phrase after the position lists for
181042** any deferred tokens have been loaded into memory. It updates the phrases
181043** current position list to include only those positions that are really
181044** instances of the phrase (after considering deferred tokens). If this
181045** means that the phrase does not appear in the current row, doclist.pList
181046** and doclist.nList are both zeroed.
181047**
181048** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
181049*/
181050static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
181051 int iToken; /* Used to iterate through phrase tokens */
181052 char *aPoslist = 0; /* Position list for deferred tokens */
181053 int nPoslist = 0; /* Number of bytes in aPoslist */
181054 int iPrev = -1; /* Token number of previous deferred token */
181055 char *aFree = (pPhrase->doclist.bFreeList ? pPhrase->doclist.pList : 0);
181056
181057 for(iToken=0; iToken<pPhrase->nToken; iToken++){
181058 Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
181059 Fts3DeferredToken *pDeferred = pToken->pDeferred;
181060
181061 if( pDeferred ){
181062 char *pList;
181063 int nList;
181064 int rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList);
181065 if( rc!=SQLITE_OK0 ) return rc;
181066
181067 if( pList==0 ){
181068 sqlite3_free(aPoslist);
181069 sqlite3_free(aFree);
181070 pPhrase->doclist.pList = 0;
181071 pPhrase->doclist.nList = 0;
181072 return SQLITE_OK0;
181073
181074 }else if( aPoslist==0 ){
181075 aPoslist = pList;
181076 nPoslist = nList;
181077
181078 }else{
181079 char *aOut = pList;
181080 char *p1 = aPoslist;
181081 char *p2 = aOut;
181082
181083 assert( iPrev>=0 )((void) (0));
181084 fts3PoslistPhraseMerge(&aOut, iToken-iPrev, 0, 1, &p1, &p2);
181085 sqlite3_free(aPoslist);
181086 aPoslist = pList;
181087 nPoslist = (int)(aOut - aPoslist);
181088 if( nPoslist==0 ){
181089 sqlite3_free(aPoslist);
181090 sqlite3_free(aFree);
181091 pPhrase->doclist.pList = 0;
181092 pPhrase->doclist.nList = 0;
181093 return SQLITE_OK0;
181094 }
181095 }
181096 iPrev = iToken;
181097 }
181098 }
181099
181100 if( iPrev>=0 ){
181101 int nMaxUndeferred = pPhrase->iDoclistToken;
181102 if( nMaxUndeferred<0 ){
181103 pPhrase->doclist.pList = aPoslist;
181104 pPhrase->doclist.nList = nPoslist;
181105 pPhrase->doclist.iDocid = pCsr->iPrevId;
181106 pPhrase->doclist.bFreeList = 1;
181107 }else{
181108 int nDistance;
181109 char *p1;
181110 char *p2;
181111 char *aOut;
181112
181113 if( nMaxUndeferred>iPrev ){
181114 p1 = aPoslist;
181115 p2 = pPhrase->doclist.pList;
181116 nDistance = nMaxUndeferred - iPrev;
181117 }else{
181118 p1 = pPhrase->doclist.pList;
181119 p2 = aPoslist;
181120 nDistance = iPrev - nMaxUndeferred;
181121 }
181122
181123 aOut = (char *)sqlite3_malloc(nPoslist+8);
181124 if( !aOut ){
181125 sqlite3_free(aPoslist);
181126 return SQLITE_NOMEM7;
181127 }
181128
181129 pPhrase->doclist.pList = aOut;
181130 assert( p1 && p2 )((void) (0));
181131 if( fts3PoslistPhraseMerge(&aOut, nDistance, 0, 1, &p1, &p2) ){
181132 pPhrase->doclist.bFreeList = 1;
181133 pPhrase->doclist.nList = (int)(aOut - pPhrase->doclist.pList);
181134 }else{
181135 sqlite3_free(aOut);
181136 pPhrase->doclist.pList = 0;
181137 pPhrase->doclist.nList = 0;
181138 }
181139 sqlite3_free(aPoslist);
181140 }
181141 }
181142
181143 if( pPhrase->doclist.pList!=aFree ) sqlite3_free(aFree);
181144 return SQLITE_OK0;
181145}
181146#endif /* SQLITE_DISABLE_FTS4_DEFERRED */
181147
181148/*
181149** Maximum number of tokens a phrase may have to be considered for the
181150** incremental doclists strategy.
181151*/
181152#define MAX_INCR_PHRASE_TOKENS4 4
181153
181154/*
181155** This function is called for each Fts3Phrase in a full-text query
181156** expression to initialize the mechanism for returning rows. Once this
181157** function has been called successfully on an Fts3Phrase, it may be
181158** used with fts3EvalPhraseNext() to iterate through the matching docids.
181159**
181160** If parameter bOptOk is true, then the phrase may (or may not) use the
181161** incremental loading strategy. Otherwise, the entire doclist is loaded into
181162** memory within this call.
181163**
181164** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
181165*/
181166static int fts3EvalPhraseStart(Fts3Cursor *pCsr, int bOptOk, Fts3Phrase *p){
181167 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
181168 int rc = SQLITE_OK0; /* Error code */
181169 int i;
181170
181171 /* Determine if doclists may be loaded from disk incrementally. This is
181172 ** possible if the bOptOk argument is true, the FTS doclists will be
181173 ** scanned in forward order, and the phrase consists of
181174 ** MAX_INCR_PHRASE_TOKENS or fewer tokens, none of which are are "^first"
181175 ** tokens or prefix tokens that cannot use a prefix-index. */
181176 int bHaveIncr = 0;
181177 int bIncrOk = (bOptOk
181178 && pCsr->bDesc==pTab->bDescIdx
181179 && p->nToken<=MAX_INCR_PHRASE_TOKENS4 && p->nToken>0
181180#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
181181 && pTab->bNoIncrDoclist==0
181182#endif
181183 );
181184 for(i=0; bIncrOk==1 && i<p->nToken; i++){
181185 Fts3PhraseToken *pToken = &p->aToken[i];
181186 if( pToken->bFirst || (pToken->pSegcsr!=0 && !pToken->pSegcsr->bLookup) ){
181187 bIncrOk = 0;
181188 }
181189 if( pToken->pSegcsr ) bHaveIncr = 1;
181190 }
181191
181192 if( bIncrOk && bHaveIncr ){
181193 /* Use the incremental approach. */
181194 int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn);
181195 for(i=0; rc==SQLITE_OK0 && i<p->nToken; i++){
181196 Fts3PhraseToken *pToken = &p->aToken[i];
181197 Fts3MultiSegReader *pSegcsr = pToken->pSegcsr;
181198 if( pSegcsr ){
181199 rc = sqlite3Fts3MsrIncrStart(pTab, pSegcsr, iCol, pToken->z, pToken->n);
181200 }
181201 }
181202 p->bIncr = 1;
181203 }else{
181204 /* Load the full doclist for the phrase into memory. */
181205 rc = fts3EvalPhraseLoad(pCsr, p);
181206 p->bIncr = 0;
181207 }
181208
181209 assert( rc!=SQLITE_OK || p->nToken<1 || p->aToken[0].pSegcsr==0 || p->bIncr )((void) (0));
181210 return rc;
181211}
181212
181213/*
181214** This function is used to iterate backwards (from the end to start)
181215** through doclists. It is used by this module to iterate through phrase
181216** doclists in reverse and by the fts3_write.c module to iterate through
181217** pending-terms lists when writing to databases with "order=desc".
181218**
181219** The doclist may be sorted in ascending (parameter bDescIdx==0) or
181220** descending (parameter bDescIdx==1) order of docid. Regardless, this
181221** function iterates from the end of the doclist to the beginning.
181222*/
181223SQLITE_PRIVATEstatic void sqlite3Fts3DoclistPrev(
181224 int bDescIdx, /* True if the doclist is desc */
181225 char *aDoclist, /* Pointer to entire doclist */
181226 int nDoclist, /* Length of aDoclist in bytes */
181227 char **ppIter, /* IN/OUT: Iterator pointer */
181228 sqlite3_int64 *piDocid, /* IN/OUT: Docid pointer */
181229 int *pnList, /* OUT: List length pointer */
181230 u8 *pbEof /* OUT: End-of-file flag */
181231){
181232 char *p = *ppIter;
181233
181234 assert( nDoclist>0 )((void) (0));
181235 assert( *pbEof==0 )((void) (0));
181236 assert_fts3_nc( p || *piDocid==0 )((void) (0));
181237 assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) )((void) (0));
181238
181239 if( p==0 ){
181240 sqlite3_int64 iDocid = 0;
181241 char *pNext = 0;
181242 char *pDocid = aDoclist;
181243 char *pEnd = &aDoclist[nDoclist];
181244 int iMul = 1;
181245
181246 while( pDocid<pEnd ){
181247 sqlite3_int64 iDelta;
181248 pDocid += sqlite3Fts3GetVarint(pDocid, &iDelta);
181249 iDocid += (iMul * iDelta);
181250 pNext = pDocid;
181251 fts3PoslistCopy(0, &pDocid);
181252 while( pDocid<pEnd && *pDocid==0 ) pDocid++;
181253 iMul = (bDescIdx ? -1 : 1);
181254 }
181255
181256 *pnList = (int)(pEnd - pNext);
181257 *ppIter = pNext;
181258 *piDocid = iDocid;
181259 }else{
181260 int iMul = (bDescIdx ? -1 : 1);
181261 sqlite3_int64 iDelta;
181262 fts3GetReverseVarint(&p, aDoclist, &iDelta);
181263 *piDocid -= (iMul * iDelta);
181264
181265 if( p==aDoclist ){
181266 *pbEof = 1;
181267 }else{
181268 char *pSave = p;
181269 fts3ReversePoslist(aDoclist, &p);
181270 *pnList = (int)(pSave - p);
181271 }
181272 *ppIter = p;
181273 }
181274}
181275
181276/*
181277** Iterate forwards through a doclist.
181278*/
181279SQLITE_PRIVATEstatic void sqlite3Fts3DoclistNext(
181280 int bDescIdx, /* True if the doclist is desc */
181281 char *aDoclist, /* Pointer to entire doclist */
181282 int nDoclist, /* Length of aDoclist in bytes */
181283 char **ppIter, /* IN/OUT: Iterator pointer */
181284 sqlite3_int64 *piDocid, /* IN/OUT: Docid pointer */
181285 u8 *pbEof /* OUT: End-of-file flag */
181286){
181287 char *p = *ppIter;
181288
181289 assert( nDoclist>0 )((void) (0));
181290 assert( *pbEof==0 )((void) (0));
181291 assert_fts3_nc( p || *piDocid==0 )((void) (0));
181292 assert( !p || (p>=aDoclist && p<=&aDoclist[nDoclist]) )((void) (0));
181293
181294 if( p==0 ){
181295 p = aDoclist;
181296 p += sqlite3Fts3GetVarint(p, piDocid);
181297 }else{
181298 fts3PoslistCopy(0, &p);
181299 while( p<&aDoclist[nDoclist] && *p==0 ) p++;
181300 if( p>=&aDoclist[nDoclist] ){
181301 *pbEof = 1;
181302 }else{
181303 sqlite3_int64 iVar;
181304 p += sqlite3Fts3GetVarint(p, &iVar);
181305 *piDocid += ((bDescIdx ? -1 : 1) * iVar);
181306 }
181307 }
181308
181309 *ppIter = p;
181310}
181311
181312/*
181313** Advance the iterator pDL to the next entry in pDL->aAll/nAll. Set *pbEof
181314** to true if EOF is reached.
181315*/
181316static void fts3EvalDlPhraseNext(
181317 Fts3Table *pTab,
181318 Fts3Doclist *pDL,
181319 u8 *pbEof
181320){
181321 char *pIter; /* Used to iterate through aAll */
181322 char *pEnd; /* 1 byte past end of aAll */
181323
181324 if( pDL->pNextDocid ){
181325 pIter = pDL->pNextDocid;
181326 assert( pDL->aAll!=0 || pIter==0 )((void) (0));
181327 }else{
181328 pIter = pDL->aAll;
181329 }
181330
181331 if( pIter==0 || pIter>=(pEnd = pDL->aAll + pDL->nAll) ){
181332 /* We have already reached the end of this doclist. EOF. */
181333 *pbEof = 1;
181334 }else{
181335 sqlite3_int64 iDelta;
181336 pIter += sqlite3Fts3GetVarint(pIter, &iDelta);
181337 if( pTab->bDescIdx==0 || pDL->pNextDocid==0 ){
181338 pDL->iDocid += iDelta;
181339 }else{
181340 pDL->iDocid -= iDelta;
181341 }
181342 pDL->pList = pIter;
181343 fts3PoslistCopy(0, &pIter);
181344 pDL->nList = (int)(pIter - pDL->pList);
181345
181346 /* pIter now points just past the 0x00 that terminates the position-
181347 ** list for document pDL->iDocid. However, if this position-list was
181348 ** edited in place by fts3EvalNearTrim(), then pIter may not actually
181349 ** point to the start of the next docid value. The following line deals
181350 ** with this case by advancing pIter past the zero-padding added by
181351 ** fts3EvalNearTrim(). */
181352 while( pIter<pEnd && *pIter==0 ) pIter++;
181353
181354 pDL->pNextDocid = pIter;
181355 assert( pIter>=&pDL->aAll[pDL->nAll] || *pIter )((void) (0));
181356 *pbEof = 0;
181357 }
181358}
181359
181360/*
181361** Helper type used by fts3EvalIncrPhraseNext() and incrPhraseTokenNext().
181362*/
181363typedef struct TokenDoclist TokenDoclist;
181364struct TokenDoclist {
181365 int bIgnore;
181366 sqlite3_int64 iDocid;
181367 char *pList;
181368 int nList;
181369};
181370
181371/*
181372** Token pToken is an incrementally loaded token that is part of a
181373** multi-token phrase. Advance it to the next matching document in the
181374** database and populate output variable *p with the details of the new
181375** entry. Or, if the iterator has reached EOF, set *pbEof to true.
181376**
181377** If an error occurs, return an SQLite error code. Otherwise, return
181378** SQLITE_OK.
181379*/
181380static int incrPhraseTokenNext(
181381 Fts3Table *pTab, /* Virtual table handle */
181382 Fts3Phrase *pPhrase, /* Phrase to advance token of */
181383 int iToken, /* Specific token to advance */
181384 TokenDoclist *p, /* OUT: Docid and doclist for new entry */
181385 u8 *pbEof /* OUT: True if iterator is at EOF */
181386){
181387 int rc = SQLITE_OK0;
181388
181389 if( pPhrase->iDoclistToken==iToken ){
181390 assert( p->bIgnore==0 )((void) (0));
181391 assert( pPhrase->aToken[iToken].pSegcsr==0 )((void) (0));
181392 fts3EvalDlPhraseNext(pTab, &pPhrase->doclist, pbEof);
181393 p->pList = pPhrase->doclist.pList;
181394 p->nList = pPhrase->doclist.nList;
181395 p->iDocid = pPhrase->doclist.iDocid;
181396 }else{
181397 Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
181398 assert( pToken->pDeferred==0 )((void) (0));
181399 assert( pToken->pSegcsr || pPhrase->iDoclistToken>=0 )((void) (0));
181400 if( pToken->pSegcsr ){
181401 assert( p->bIgnore==0 )((void) (0));
181402 rc = sqlite3Fts3MsrIncrNext(
181403 pTab, pToken->pSegcsr, &p->iDocid, &p->pList, &p->nList
181404 );
181405 if( p->pList==0 ) *pbEof = 1;
181406 }else{
181407 p->bIgnore = 1;
181408 }
181409 }
181410
181411 return rc;
181412}
181413
181414
181415/*
181416** The phrase iterator passed as the second argument:
181417**
181418** * features at least one token that uses an incremental doclist, and
181419**
181420** * does not contain any deferred tokens.
181421**
181422** Advance it to the next matching documnent in the database and populate
181423** the Fts3Doclist.pList and nList fields.
181424**
181425** If there is no "next" entry and no error occurs, then *pbEof is set to
181426** 1 before returning. Otherwise, if no error occurs and the iterator is
181427** successfully advanced, *pbEof is set to 0.
181428**
181429** If an error occurs, return an SQLite error code. Otherwise, return
181430** SQLITE_OK.
181431*/
181432static int fts3EvalIncrPhraseNext(
181433 Fts3Cursor *pCsr, /* FTS Cursor handle */
181434 Fts3Phrase *p, /* Phrase object to advance to next docid */
181435 u8 *pbEof /* OUT: Set to 1 if EOF */
181436){
181437 int rc = SQLITE_OK0;
181438 Fts3Doclist *pDL = &p->doclist;
181439 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
181440 u8 bEof = 0;
181441
181442 /* This is only called if it is guaranteed that the phrase has at least
181443 ** one incremental token. In which case the bIncr flag is set. */
181444 assert( p->bIncr==1 )((void) (0));
181445
181446 if( p->nToken==1 ){
181447 rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr,
181448 &pDL->iDocid, &pDL->pList, &pDL->nList
181449 );
181450 if( pDL->pList==0 ) bEof = 1;
181451 }else{
181452 int bDescDoclist = pCsr->bDesc;
181453 struct TokenDoclist a[MAX_INCR_PHRASE_TOKENS4];
181454
181455 memset(a, 0, sizeof(a));
181456 assert( p->nToken<=MAX_INCR_PHRASE_TOKENS )((void) (0));
181457 assert( p->iDoclistToken<MAX_INCR_PHRASE_TOKENS )((void) (0));
181458
181459 while( bEof==0 ){
181460 int bMaxSet = 0;
181461 sqlite3_int64 iMax = 0; /* Largest docid for all iterators */
181462 int i; /* Used to iterate through tokens */
181463
181464 /* Advance the iterator for each token in the phrase once. */
181465 for(i=0; rc==SQLITE_OK0 && i<p->nToken && bEof==0; i++){
181466 rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
181467 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) ){
181468 iMax = a[i].iDocid;
181469 bMaxSet = 1;
181470 }
181471 }
181472 assert( rc!=SQLITE_OK || (p->nToken>=1 && a[p->nToken-1].bIgnore==0) )((void) (0));
181473 assert( rc!=SQLITE_OK || bMaxSet )((void) (0));
181474
181475 /* Keep advancing iterators until they all point to the same document */
181476 for(i=0; i<p->nToken; i++){
181477 while( rc==SQLITE_OK0 && bEof==0
181478 && 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
181479 ){
181480 rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
181481 if( DOCID_CMP(a[i].iDocid, iMax)((bDescDoclist?-1:1) * (a[i].iDocid>iMax?1:((a[i].iDocid==
iMax)?0:-1)))
>0 ){
181482 iMax = a[i].iDocid;
181483 i = 0;
181484 }
181485 }
181486 }
181487
181488 /* Check if the current entries really are a phrase match */
181489 if( bEof==0 ){
181490 int nList = 0;
181491 int nByte = a[p->nToken-1].nList;
181492 char *aDoclist = sqlite3_malloc(nByte+FTS3_BUFFER_PADDING8);
181493 if( !aDoclist ) return SQLITE_NOMEM7;
181494 memcpy(aDoclist, a[p->nToken-1].pList, nByte+1);
181495 memset(&aDoclist[nByte], 0, FTS3_BUFFER_PADDING8);
181496
181497 for(i=0; i<(p->nToken-1); i++){
181498 if( a[i].bIgnore==0 ){
181499 char *pL = a[i].pList;
181500 char *pR = aDoclist;
181501 char *pOut = aDoclist;
181502 int nDist = p->nToken-1-i;
181503 int res = fts3PoslistPhraseMerge(&pOut, nDist, 0, 1, &pL, &pR);
181504 if( res==0 ) break;
181505 nList = (int)(pOut - aDoclist);
181506 }
181507 }
181508 if( i==(p->nToken-1) ){
181509 pDL->iDocid = iMax;
181510 pDL->pList = aDoclist;
181511 pDL->nList = nList;
181512 pDL->bFreeList = 1;
181513 break;
181514 }
181515 sqlite3_free(aDoclist);
181516 }
181517 }
181518 }
181519
181520 *pbEof = bEof;
181521 return rc;
181522}
181523
181524/*
181525** Attempt to move the phrase iterator to point to the next matching docid.
181526** If an error occurs, return an SQLite error code. Otherwise, return
181527** SQLITE_OK.
181528**
181529** If there is no "next" entry and no error occurs, then *pbEof is set to
181530** 1 before returning. Otherwise, if no error occurs and the iterator is
181531** successfully advanced, *pbEof is set to 0.
181532*/
181533static int fts3EvalPhraseNext(
181534 Fts3Cursor *pCsr, /* FTS Cursor handle */
181535 Fts3Phrase *p, /* Phrase object to advance to next docid */
181536 u8 *pbEof /* OUT: Set to 1 if EOF */
181537){
181538 int rc = SQLITE_OK0;
181539 Fts3Doclist *pDL = &p->doclist;
181540 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
181541
181542 if( p->bIncr ){
181543 rc = fts3EvalIncrPhraseNext(pCsr, p, pbEof);
181544 }else if( pCsr->bDesc!=pTab->bDescIdx && pDL->nAll ){
181545 sqlite3Fts3DoclistPrev(pTab->bDescIdx, pDL->aAll, pDL->nAll,
181546 &pDL->pNextDocid, &pDL->iDocid, &pDL->nList, pbEof
181547 );
181548 pDL->pList = pDL->pNextDocid;
181549 }else{
181550 fts3EvalDlPhraseNext(pTab, pDL, pbEof);
181551 }
181552
181553 return rc;
181554}
181555
181556/*
181557**
181558** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
181559** Otherwise, fts3EvalPhraseStart() is called on all phrases within the
181560** expression. Also the Fts3Expr.bDeferred variable is set to true for any
181561** expressions for which all descendent tokens are deferred.
181562**
181563** If parameter bOptOk is zero, then it is guaranteed that the
181564** Fts3Phrase.doclist.aAll/nAll variables contain the entire doclist for
181565** each phrase in the expression (subject to deferred token processing).
181566** Or, if bOptOk is non-zero, then one or more tokens within the expression
181567** may be loaded incrementally, meaning doclist.aAll/nAll is not available.
181568**
181569** If an error occurs within this function, *pRc is set to an SQLite error
181570** code before returning.
181571*/
181572static void fts3EvalStartReaders(
181573 Fts3Cursor *pCsr, /* FTS Cursor handle */
181574 Fts3Expr *pExpr, /* Expression to initialize phrases in */
181575 int *pRc /* IN/OUT: Error code */
181576){
181577 if( pExpr && SQLITE_OK0==*pRc ){
181578 if( pExpr->eType==FTSQUERY_PHRASE5 ){
181579 int nToken = pExpr->pPhrase->nToken;
181580 if( nToken ){
181581 int i;
181582 for(i=0; i<nToken; i++){
181583 if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break;
181584 }
181585 pExpr->bDeferred = (i==nToken);
181586 }
181587 *pRc = fts3EvalPhraseStart(pCsr, 1, pExpr->pPhrase);
181588 }else{
181589 fts3EvalStartReaders(pCsr, pExpr->pLeft, pRc);
181590 fts3EvalStartReaders(pCsr, pExpr->pRight, pRc);
181591 pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred);
181592 }
181593 }
181594}
181595
181596/*
181597** An array of the following structures is assembled as part of the process
181598** of selecting tokens to defer before the query starts executing (as part
181599** of the xFilter() method). There is one element in the array for each
181600** token in the FTS expression.
181601**
181602** Tokens are divided into AND/NEAR clusters. All tokens in a cluster belong
181603** to phrases that are connected only by AND and NEAR operators (not OR or
181604** NOT). When determining tokens to defer, each AND/NEAR cluster is considered
181605** separately. The root of a tokens AND/NEAR cluster is stored in
181606** Fts3TokenAndCost.pRoot.
181607*/
181608typedef struct Fts3TokenAndCost Fts3TokenAndCost;
181609struct Fts3TokenAndCost {
181610 Fts3Phrase *pPhrase; /* The phrase the token belongs to */
181611 int iToken; /* Position of token in phrase */
181612 Fts3PhraseToken *pToken; /* The token itself */
181613 Fts3Expr *pRoot; /* Root of NEAR/AND cluster */
181614 int nOvfl; /* Number of overflow pages to load doclist */
181615 int iCol; /* The column the token must match */
181616};
181617
181618/*
181619** This function is used to populate an allocated Fts3TokenAndCost array.
181620**
181621** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
181622** Otherwise, if an error occurs during execution, *pRc is set to an
181623** SQLite error code.
181624*/
181625static void fts3EvalTokenCosts(
181626 Fts3Cursor *pCsr, /* FTS Cursor handle */
181627 Fts3Expr *pRoot, /* Root of current AND/NEAR cluster */
181628 Fts3Expr *pExpr, /* Expression to consider */
181629 Fts3TokenAndCost **ppTC, /* Write new entries to *(*ppTC)++ */
181630 Fts3Expr ***ppOr, /* Write new OR root to *(*ppOr)++ */
181631 int *pRc /* IN/OUT: Error code */
181632){
181633 if( *pRc==SQLITE_OK0 ){
181634 if( pExpr->eType==FTSQUERY_PHRASE5 ){
181635 Fts3Phrase *pPhrase = pExpr->pPhrase;
181636 int i;
181637 for(i=0; *pRc==SQLITE_OK0 && i<pPhrase->nToken; i++){
181638 Fts3TokenAndCost *pTC = (*ppTC)++;
181639 pTC->pPhrase = pPhrase;
181640 pTC->iToken = i;
181641 pTC->pRoot = pRoot;
181642 pTC->pToken = &pPhrase->aToken[i];
181643 pTC->iCol = pPhrase->iColumn;
181644 *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl);
181645 }
181646 }else if( pExpr->eType!=FTSQUERY_NOT2 ){
181647 assert( pExpr->eType==FTSQUERY_OR((void) (0))
181648 || pExpr->eType==FTSQUERY_AND((void) (0))
181649 || pExpr->eType==FTSQUERY_NEAR((void) (0))
181650 )((void) (0));
181651 assert( pExpr->pLeft && pExpr->pRight )((void) (0));
181652 if( pExpr->eType==FTSQUERY_OR4 ){
181653 pRoot = pExpr->pLeft;
181654 **ppOr = pRoot;
181655 (*ppOr)++;
181656 }
181657 fts3EvalTokenCosts(pCsr, pRoot, pExpr->pLeft, ppTC, ppOr, pRc);
181658 if( pExpr->eType==FTSQUERY_OR4 ){
181659 pRoot = pExpr->pRight;
181660 **ppOr = pRoot;
181661 (*ppOr)++;
181662 }
181663 fts3EvalTokenCosts(pCsr, pRoot, pExpr->pRight, ppTC, ppOr, pRc);
181664 }
181665 }
181666}
181667
181668/*
181669** Determine the average document (row) size in pages. If successful,
181670** write this value to *pnPage and return SQLITE_OK. Otherwise, return
181671** an SQLite error code.
181672**
181673** The average document size in pages is calculated by first calculating
181674** determining the average size in bytes, B. If B is less than the amount
181675** of data that will fit on a single leaf page of an intkey table in
181676** this database, then the average docsize is 1. Otherwise, it is 1 plus
181677** the number of overflow pages consumed by a record B bytes in size.
181678*/
181679static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){
181680 int rc = SQLITE_OK0;
181681 if( pCsr->nRowAvg==0 ){
181682 /* The average document size, which is required to calculate the cost
181683 ** of each doclist, has not yet been determined. Read the required
181684 ** data from the %_stat table to calculate it.
181685 **
181686 ** Entry 0 of the %_stat table is a blob containing (nCol+1) FTS3
181687 ** varints, where nCol is the number of columns in the FTS3 table.
181688 ** The first varint is the number of documents currently stored in
181689 ** the table. The following nCol varints contain the total amount of
181690 ** data stored in all rows of each column of the table, from left
181691 ** to right.
181692 */
181693 Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
181694 sqlite3_stmt *pStmt;
181695 sqlite3_int64 nDoc = 0;
181696 sqlite3_int64 nByte = 0;
181697 const char *pEnd;
181698 const char *a;
181699
181700 rc = sqlite3Fts3SelectDoctotal(p, &pStmt);
181701 if( rc!=SQLITE_OK0 ) return rc;
181702 a = sqlite3_column_blob(pStmt, 0);
181703 testcase( a==0 ); /* If %_stat.value set to X'' */
181704 if( a ){
181705 pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
181706 a += sqlite3Fts3GetVarintBounded(a, pEnd, &nDoc);
181707 while( a<pEnd ){
181708 a += sqlite3Fts3GetVarintBounded(a, pEnd, &nByte);
181709 }
181710 }
181711 if( nDoc==0 || nByte==0 ){
181712 sqlite3_reset(pStmt);
181713 return FTS_CORRUPT_VTAB(11 | (1<<8));
181714 }
181715
181716 pCsr->nDoc = nDoc;
181717 pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
181718 assert( pCsr->nRowAvg>0 )((void) (0));
181719 rc = sqlite3_reset(pStmt);
181720 }
181721
181722 *pnPage = pCsr->nRowAvg;
181723 return rc;
181724}
181725
181726/*
181727** This function is called to select the tokens (if any) that will be
181728** deferred. The array aTC[] has already been populated when this is
181729** called.
181730**
181731** This function is called once for each AND/NEAR cluster in the
181732** expression. Each invocation determines which tokens to defer within
181733** the cluster with root node pRoot. See comments above the definition
181734** of struct Fts3TokenAndCost for more details.
181735**
181736** If no error occurs, SQLITE_OK is returned and sqlite3Fts3DeferToken()
181737** called on each token to defer. Otherwise, an SQLite error code is
181738** returned.
181739*/
181740static int fts3EvalSelectDeferred(
181741 Fts3Cursor *pCsr, /* FTS Cursor handle */
181742 Fts3Expr *pRoot, /* Consider tokens with this root node */
181743 Fts3TokenAndCost *aTC, /* Array of expression tokens and costs */
181744 int nTC /* Number of entries in aTC[] */
181745){
181746 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
181747 int nDocSize = 0; /* Number of pages per doc loaded */
181748 int rc = SQLITE_OK0; /* Return code */
181749 int ii; /* Iterator variable for various purposes */
181750 int nOvfl = 0; /* Total overflow pages used by doclists */
181751 int nToken = 0; /* Total number of tokens in cluster */
181752
181753 int nMinEst = 0; /* The minimum count for any phrase so far. */
181754 int nLoad4 = 1; /* (Phrases that will be loaded)^4. */
181755
181756 /* Tokens are never deferred for FTS tables created using the content=xxx
181757 ** option. The reason being that it is not guaranteed that the content
181758 ** table actually contains the same data as the index. To prevent this from
181759 ** causing any problems, the deferred token optimization is completely
181760 ** disabled for content=xxx tables. */
181761 if( pTab->zContentTbl ){
181762 return SQLITE_OK0;
181763 }
181764
181765 /* Count the tokens in this AND/NEAR cluster. If none of the doclists
181766 ** associated with the tokens spill onto overflow pages, or if there is
181767 ** only 1 token, exit early. No tokens to defer in this case. */
181768 for(ii=0; ii<nTC; ii++){
181769 if( aTC[ii].pRoot==pRoot ){
181770 nOvfl += aTC[ii].nOvfl;
181771 nToken++;
181772 }
181773 }
181774 if( nOvfl==0 || nToken<2 ) return SQLITE_OK0;
181775
181776 /* Obtain the average docsize (in pages). */
181777 rc = fts3EvalAverageDocsize(pCsr, &nDocSize);
181778 assert( rc!=SQLITE_OK || nDocSize>0 )((void) (0));
181779
181780
181781 /* Iterate through all tokens in this AND/NEAR cluster, in ascending order
181782 ** of the number of overflow pages that will be loaded by the pager layer
181783 ** to retrieve the entire doclist for the token from the full-text index.
181784 ** Load the doclists for tokens that are either:
181785 **
181786 ** a. The cheapest token in the entire query (i.e. the one visited by the
181787 ** first iteration of this loop), or
181788 **
181789 ** b. Part of a multi-token phrase.
181790 **
181791 ** After each token doclist is loaded, merge it with the others from the
181792 ** same phrase and count the number of documents that the merged doclist
181793 ** contains. Set variable "nMinEst" to the smallest number of documents in
181794 ** any phrase doclist for which 1 or more token doclists have been loaded.
181795 ** Let nOther be the number of other phrases for which it is certain that
181796 ** one or more tokens will not be deferred.
181797 **
181798 ** Then, for each token, defer it if loading the doclist would result in
181799 ** loading N or more overflow pages into memory, where N is computed as:
181800 **
181801 ** (nMinEst + 4^nOther - 1) / (4^nOther)
181802 */
181803 for(ii=0; ii<nToken && rc==SQLITE_OK0; ii++){
181804 int iTC; /* Used to iterate through aTC[] array. */
181805 Fts3TokenAndCost *pTC = 0; /* Set to cheapest remaining token. */
181806
181807 /* Set pTC to point to the cheapest remaining token. */
181808 for(iTC=0; iTC<nTC; iTC++){
181809 if( aTC[iTC].pToken && aTC[iTC].pRoot==pRoot
181810 && (!pTC || aTC[iTC].nOvfl<pTC->nOvfl)
181811 ){
181812 pTC = &aTC[iTC];
181813 }
181814 }
181815 assert( pTC )((void) (0));
181816
181817 if( ii && pTC->nOvfl>=((nMinEst+(nLoad4/4)-1)/(nLoad4/4))*nDocSize ){
181818 /* The number of overflow pages to load for this (and therefore all
181819 ** subsequent) tokens is greater than the estimated number of pages
181820 ** that will be loaded if all subsequent tokens are deferred.
181821 */
181822 Fts3PhraseToken *pToken = pTC->pToken;
181823 rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol);
181824 fts3SegReaderCursorFree(pToken->pSegcsr);
181825 pToken->pSegcsr = 0;
181826 }else{
181827 /* Set nLoad4 to the value of (4^nOther) for the next iteration of the
181828 ** for-loop. Except, limit the value to 2^24 to prevent it from
181829 ** overflowing the 32-bit integer it is stored in. */
181830 if( ii<12 ) nLoad4 = nLoad4*4;
181831
181832 if( ii==0 || (pTC->pPhrase->nToken>1 && ii!=nToken-1) ){
181833 /* Either this is the cheapest token in the entire query, or it is
181834 ** part of a multi-token phrase. Either way, the entire doclist will
181835 ** (eventually) be loaded into memory. It may as well be now. */
181836 Fts3PhraseToken *pToken = pTC->pToken;
181837 int nList = 0;
181838 char *pList = 0;
181839 rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList);
181840 assert( rc==SQLITE_OK || pList==0 )((void) (0));
181841 if( rc==SQLITE_OK0 ){
181842 rc = fts3EvalPhraseMergeToken(
181843 pTab, pTC->pPhrase, pTC->iToken,pList,nList
181844 );
181845 }
181846 if( rc==SQLITE_OK0 ){
181847 int nCount;
181848 nCount = fts3DoclistCountDocids(
181849 pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll
181850 );
181851 if( ii==0 || nCount<nMinEst ) nMinEst = nCount;
181852 }
181853 }
181854 }
181855 pTC->pToken = 0;
181856 }
181857
181858 return rc;
181859}
181860
181861/*
181862** This function is called from within the xFilter method. It initializes
181863** the full-text query currently stored in pCsr->pExpr. To iterate through
181864** the results of a query, the caller does:
181865**
181866** fts3EvalStart(pCsr);
181867** while( 1 ){
181868** fts3EvalNext(pCsr);
181869** if( pCsr->bEof ) break;
181870** ... return row pCsr->iPrevId to the caller ...
181871** }
181872*/
181873static int fts3EvalStart(Fts3Cursor *pCsr){
181874 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
181875 int rc = SQLITE_OK0;
181876 int nToken = 0;
181877 int nOr = 0;
181878
181879 /* Allocate a MultiSegReader for each token in the expression. */
181880 fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc);
181881
181882 /* Determine which, if any, tokens in the expression should be deferred. */
181883#ifndef SQLITE_DISABLE_FTS4_DEFERRED
181884 if( rc==SQLITE_OK0 && nToken>1 && pTab->bFts4 ){
181885 Fts3TokenAndCost *aTC;
181886 aTC = (Fts3TokenAndCost *)sqlite3_malloc64(
181887 sizeof(Fts3TokenAndCost) * nToken
181888 + sizeof(Fts3Expr *) * nOr * 2
181889 );
181890
181891 if( !aTC ){
181892 rc = SQLITE_NOMEM7;
181893 }else{
181894 Fts3Expr **apOr = (Fts3Expr **)&aTC[nToken];
181895 int ii;
181896 Fts3TokenAndCost *pTC = aTC;
181897 Fts3Expr **ppOr = apOr;
181898
181899 fts3EvalTokenCosts(pCsr, 0, pCsr->pExpr, &pTC, &ppOr, &rc);
181900 nToken = (int)(pTC-aTC);
181901 nOr = (int)(ppOr-apOr);
181902
181903 if( rc==SQLITE_OK0 ){
181904 rc = fts3EvalSelectDeferred(pCsr, 0, aTC, nToken);
181905 for(ii=0; rc==SQLITE_OK0 && ii<nOr; ii++){
181906 rc = fts3EvalSelectDeferred(pCsr, apOr[ii], aTC, nToken);
181907 }
181908 }
181909
181910 sqlite3_free(aTC);
181911 }
181912 }
181913#endif
181914
181915 fts3EvalStartReaders(pCsr, pCsr->pExpr, &rc);
181916 return rc;
181917}
181918
181919/*
181920** Invalidate the current position list for phrase pPhrase.
181921*/
181922static void fts3EvalInvalidatePoslist(Fts3Phrase *pPhrase){
181923 if( pPhrase->doclist.bFreeList ){
181924 sqlite3_free(pPhrase->doclist.pList);
181925 }
181926 pPhrase->doclist.pList = 0;
181927 pPhrase->doclist.nList = 0;
181928 pPhrase->doclist.bFreeList = 0;
181929}
181930
181931/*
181932** This function is called to edit the position list associated with
181933** the phrase object passed as the fifth argument according to a NEAR
181934** condition. For example:
181935**
181936** abc NEAR/5 "def ghi"
181937**
181938** Parameter nNear is passed the NEAR distance of the expression (5 in
181939** the example above). When this function is called, *paPoslist points to
181940** the position list, and *pnToken is the number of phrase tokens in the
181941** phrase on the other side of the NEAR operator to pPhrase. For example,
181942** if pPhrase refers to the "def ghi" phrase, then *paPoslist points to
181943** the position list associated with phrase "abc".
181944**
181945** All positions in the pPhrase position list that are not sufficiently
181946** close to a position in the *paPoslist position list are removed. If this
181947** leaves 0 positions, zero is returned. Otherwise, non-zero.
181948**
181949** Before returning, *paPoslist is set to point to the position lsit
181950** associated with pPhrase. And *pnToken is set to the number of tokens in
181951** pPhrase.
181952*/
181953static int fts3EvalNearTrim(
181954 int nNear, /* NEAR distance. As in "NEAR/nNear". */
181955 char *aTmp, /* Temporary space to use */
181956 char **paPoslist, /* IN/OUT: Position list */
181957 int *pnToken, /* IN/OUT: Tokens in phrase of *paPoslist */
181958 Fts3Phrase *pPhrase /* The phrase object to trim the doclist of */
181959){
181960 int nParam1 = nNear + pPhrase->nToken;
181961 int nParam2 = nNear + *pnToken;
181962 int nNew;
181963 char *p2;
181964 char *pOut;
181965 int res;
181966
181967 assert( pPhrase->doclist.pList )((void) (0));
181968
181969 p2 = pOut = pPhrase->doclist.pList;
181970 res = fts3PoslistNearMerge(
181971 &pOut, aTmp, nParam1, nParam2, paPoslist, &p2
181972 );
181973 if( res ){
181974 nNew = (int)(pOut - pPhrase->doclist.pList) - 1;
181975 assert_fts3_nc( nNew<=pPhrase->doclist.nList && nNew>0 )((void) (0));
181976 if( nNew>=0 && nNew<=pPhrase->doclist.nList ){
181977 assert( pPhrase->doclist.pList[nNew]=='\0' )((void) (0));
181978 memset(&pPhrase->doclist.pList[nNew], 0, pPhrase->doclist.nList - nNew);
181979 pPhrase->doclist.nList = nNew;
181980 }
181981 *paPoslist = pPhrase->doclist.pList;
181982 *pnToken = pPhrase->nToken;
181983 }
181984
181985 return res;
181986}
181987
181988/*
181989** This function is a no-op if *pRc is other than SQLITE_OK when it is called.
181990** Otherwise, it advances the expression passed as the second argument to
181991** point to the next matching row in the database. Expressions iterate through
181992** matching rows in docid order. Ascending order if Fts3Cursor.bDesc is zero,
181993** or descending if it is non-zero.
181994**
181995** If an error occurs, *pRc is set to an SQLite error code. Otherwise, if
181996** successful, the following variables in pExpr are set:
181997**
181998** Fts3Expr.bEof (non-zero if EOF - there is no next row)
181999** Fts3Expr.iDocid (valid if bEof==0. The docid of the next row)
182000**
182001** If the expression is of type FTSQUERY_PHRASE, and the expression is not
182002** at EOF, then the following variables are populated with the position list
182003** for the phrase for the visited row:
182004**
182005** FTs3Expr.pPhrase->doclist.nList (length of pList in bytes)
182006** FTs3Expr.pPhrase->doclist.pList (pointer to position list)
182007**
182008** It says above that this function advances the expression to the next
182009** matching row. This is usually true, but there are the following exceptions:
182010**
182011** 1. Deferred tokens are not taken into account. If a phrase consists
182012** entirely of deferred tokens, it is assumed to match every row in
182013** the db. In this case the position-list is not populated at all.
182014**
182015** Or, if a phrase contains one or more deferred tokens and one or
182016** more non-deferred tokens, then the expression is advanced to the
182017** next possible match, considering only non-deferred tokens. In other
182018** words, if the phrase is "A B C", and "B" is deferred, the expression
182019** is advanced to the next row that contains an instance of "A * C",
182020** where "*" may match any single token. The position list in this case
182021** is populated as for "A * C" before returning.
182022**
182023** 2. NEAR is treated as AND. If the expression is "x NEAR y", it is
182024** advanced to point to the next row that matches "x AND y".
182025**
182026** See sqlite3Fts3EvalTestDeferred() for details on testing if a row is
182027** really a match, taking into account deferred tokens and NEAR operators.
182028*/
182029static void fts3EvalNextRow(
182030 Fts3Cursor *pCsr, /* FTS Cursor handle */
182031 Fts3Expr *pExpr, /* Expr. to advance to next matching row */
182032 int *pRc /* IN/OUT: Error code */
182033){
182034 if( *pRc==SQLITE_OK0 ){
182035 int bDescDoclist = pCsr->bDesc; /* Used by DOCID_CMP() macro */
182036 assert( pExpr->bEof==0 )((void) (0));
182037 pExpr->bStart = 1;
182038
182039 switch( pExpr->eType ){
182040 case FTSQUERY_NEAR1:
182041 case FTSQUERY_AND3: {
182042 Fts3Expr *pLeft = pExpr->pLeft;
182043 Fts3Expr *pRight = pExpr->pRight;
182044 assert( !pLeft->bDeferred || !pRight->bDeferred )((void) (0));
182045
182046 if( pLeft->bDeferred ){
182047 /* LHS is entirely deferred. So we assume it matches every row.
182048 ** Advance the RHS iterator to find the next row visited. */
182049 fts3EvalNextRow(pCsr, pRight, pRc);
182050 pExpr->iDocid = pRight->iDocid;
182051 pExpr->bEof = pRight->bEof;
182052 }else if( pRight->bDeferred ){
182053 /* RHS is entirely deferred. So we assume it matches every row.
182054 ** Advance the LHS iterator to find the next row visited. */
182055 fts3EvalNextRow(pCsr, pLeft, pRc);
182056 pExpr->iDocid = pLeft->iDocid;
182057 pExpr->bEof = pLeft->bEof;
182058 }else{
182059 /* Neither the RHS or LHS are deferred. */
182060 fts3EvalNextRow(pCsr, pLeft, pRc);
182061 fts3EvalNextRow(pCsr, pRight, pRc);
182062 while( !pLeft->bEof && !pRight->bEof && *pRc==SQLITE_OK0 ){
182063 sqlite3_int64 iDiff = DOCID_CMP(pLeft->iDocid, pRight->iDocid)((bDescDoclist?-1:1) * (pLeft->iDocid>pRight->iDocid
?1:((pLeft->iDocid==pRight->iDocid)?0:-1)))
;
182064 if( iDiff==0 ) break;
182065 if( iDiff<0 ){
182066 fts3EvalNextRow(pCsr, pLeft, pRc);
182067 }else{
182068 fts3EvalNextRow(pCsr, pRight, pRc);
182069 }
182070 }
182071 pExpr->iDocid = pLeft->iDocid;
182072 pExpr->bEof = (pLeft->bEof || pRight->bEof);
182073 if( pExpr->eType==FTSQUERY_NEAR1 && pExpr->bEof ){
182074 assert( pRight->eType==FTSQUERY_PHRASE )((void) (0));
182075 if( pRight->pPhrase->doclist.aAll ){
182076 Fts3Doclist *pDl = &pRight->pPhrase->doclist;
182077 while( *pRc==SQLITE_OK0 && pRight->bEof==0 ){
182078 memset(pDl->pList, 0, pDl->nList);
182079 fts3EvalNextRow(pCsr, pRight, pRc);
182080 }
182081 }
182082 if( pLeft->pPhrase && pLeft->pPhrase->doclist.aAll ){
182083 Fts3Doclist *pDl = &pLeft->pPhrase->doclist;
182084 while( *pRc==SQLITE_OK0 && pLeft->bEof==0 ){
182085 memset(pDl->pList, 0, pDl->nList);
182086 fts3EvalNextRow(pCsr, pLeft, pRc);
182087 }
182088 }
182089 pRight->bEof = pLeft->bEof = 1;
182090 }
182091 }
182092 break;
182093 }
182094
182095 case FTSQUERY_OR4: {
182096 Fts3Expr *pLeft = pExpr->pLeft;
182097 Fts3Expr *pRight = pExpr->pRight;
182098 sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid)((bDescDoclist?-1:1) * (pLeft->iDocid>pRight->iDocid
?1:((pLeft->iDocid==pRight->iDocid)?0:-1)))
;
182099
182100 assert_fts3_nc( pLeft->bStart || pLeft->iDocid==pRight->iDocid )((void) (0));
182101 assert_fts3_nc( pRight->bStart || pLeft->iDocid==pRight->iDocid )((void) (0));
182102
182103 if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
182104 fts3EvalNextRow(pCsr, pLeft, pRc);
182105 }else if( pLeft->bEof || iCmp>0 ){
182106 fts3EvalNextRow(pCsr, pRight, pRc);
182107 }else{
182108 fts3EvalNextRow(pCsr, pLeft, pRc);
182109 fts3EvalNextRow(pCsr, pRight, pRc);
182110 }
182111
182112 pExpr->bEof = (pLeft->bEof && pRight->bEof);
182113 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid)((bDescDoclist?-1:1) * (pLeft->iDocid>pRight->iDocid
?1:((pLeft->iDocid==pRight->iDocid)?0:-1)))
;
182114 if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
182115 pExpr->iDocid = pLeft->iDocid;
182116 }else{
182117 pExpr->iDocid = pRight->iDocid;
182118 }
182119
182120 break;
182121 }
182122
182123 case FTSQUERY_NOT2: {
182124 Fts3Expr *pLeft = pExpr->pLeft;
182125 Fts3Expr *pRight = pExpr->pRight;
182126
182127 if( pRight->bStart==0 ){
182128 fts3EvalNextRow(pCsr, pRight, pRc);
182129 assert( *pRc!=SQLITE_OK || pRight->bStart )((void) (0));
182130 }
182131
182132 fts3EvalNextRow(pCsr, pLeft, pRc);
182133 if( pLeft->bEof==0 ){
182134 while( !*pRc
182135 && !pRight->bEof
182136 && DOCID_CMP(pLeft->iDocid, pRight->iDocid)((bDescDoclist?-1:1) * (pLeft->iDocid>pRight->iDocid
?1:((pLeft->iDocid==pRight->iDocid)?0:-1)))
>0
182137 ){
182138 fts3EvalNextRow(pCsr, pRight, pRc);
182139 }
182140 }
182141 pExpr->iDocid = pLeft->iDocid;
182142 pExpr->bEof = pLeft->bEof;
182143 break;
182144 }
182145
182146 default: {
182147 Fts3Phrase *pPhrase = pExpr->pPhrase;
182148 fts3EvalInvalidatePoslist(pPhrase);
182149 *pRc = fts3EvalPhraseNext(pCsr, pPhrase, &pExpr->bEof);
182150 pExpr->iDocid = pPhrase->doclist.iDocid;
182151 break;
182152 }
182153 }
182154 }
182155}
182156
182157/*
182158** If *pRc is not SQLITE_OK, or if pExpr is not the root node of a NEAR
182159** cluster, then this function returns 1 immediately.
182160**
182161** Otherwise, it checks if the current row really does match the NEAR
182162** expression, using the data currently stored in the position lists
182163** (Fts3Expr->pPhrase.doclist.pList/nList) for each phrase in the expression.
182164**
182165** If the current row is a match, the position list associated with each
182166** phrase in the NEAR expression is edited in place to contain only those
182167** phrase instances sufficiently close to their peers to satisfy all NEAR
182168** constraints. In this case it returns 1. If the NEAR expression does not
182169** match the current row, 0 is returned. The position lists may or may not
182170** be edited if 0 is returned.
182171*/
182172static int fts3EvalNearTest(Fts3Expr *pExpr, int *pRc){
182173 int res = 1;
182174
182175 /* The following block runs if pExpr is the root of a NEAR query.
182176 ** For example, the query:
182177 **
182178 ** "w" NEAR "x" NEAR "y" NEAR "z"
182179 **
182180 ** which is represented in tree form as:
182181 **
182182 ** |
182183 ** +--NEAR--+ <-- root of NEAR query
182184 ** | |
182185 ** +--NEAR--+ "z"
182186 ** | |
182187 ** +--NEAR--+ "y"
182188 ** | |
182189 ** "w" "x"
182190 **
182191 ** The right-hand child of a NEAR node is always a phrase. The
182192 ** left-hand child may be either a phrase or a NEAR node. There are
182193 ** no exceptions to this - it's the way the parser in fts3_expr.c works.
182194 */
182195 if( *pRc==SQLITE_OK0
182196 && pExpr->eType==FTSQUERY_NEAR1
182197 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR1)
182198 ){
182199 Fts3Expr *p;
182200 sqlite3_int64 nTmp = 0; /* Bytes of temp space */
182201 char *aTmp; /* Temp space for PoslistNearMerge() */
182202
182203 /* Allocate temporary working space. */
182204 for(p=pExpr; p->pLeft; p=p->pLeft){
182205 assert( p->pRight->pPhrase->doclist.nList>0 )((void) (0));
182206 nTmp += p->pRight->pPhrase->doclist.nList;
182207 }
182208 nTmp += p->pPhrase->doclist.nList;
182209 aTmp = sqlite3_malloc64(nTmp*2);
182210 if( !aTmp ){
182211 *pRc = SQLITE_NOMEM7;
182212 res = 0;
182213 }else{
182214 char *aPoslist = p->pPhrase->doclist.pList;
182215 int nToken = p->pPhrase->nToken;
182216
182217 for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR1; p=p->pParent){
182218 Fts3Phrase *pPhrase = p->pRight->pPhrase;
182219 int nNear = p->nNear;
182220 res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
182221 }
182222
182223 aPoslist = pExpr->pRight->pPhrase->doclist.pList;
182224 nToken = pExpr->pRight->pPhrase->nToken;
182225 for(p=pExpr->pLeft; p && res; p=p->pLeft){
182226 int nNear;
182227 Fts3Phrase *pPhrase;
182228 assert( p->pParent && p->pParent->pLeft==p )((void) (0));
182229 nNear = p->pParent->nNear;
182230 pPhrase = (
182231 p->eType==FTSQUERY_NEAR1 ? p->pRight->pPhrase : p->pPhrase
182232 );
182233 res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
182234 }
182235 }
182236
182237 sqlite3_free(aTmp);
182238 }
182239
182240 return res;
182241}
182242
182243/*
182244** This function is a helper function for sqlite3Fts3EvalTestDeferred().
182245** Assuming no error occurs or has occurred, It returns non-zero if the
182246** expression passed as the second argument matches the row that pCsr
182247** currently points to, or zero if it does not.
182248**
182249** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
182250** If an error occurs during execution of this function, *pRc is set to
182251** the appropriate SQLite error code. In this case the returned value is
182252** undefined.
182253*/
182254static int fts3EvalTestExpr(
182255 Fts3Cursor *pCsr, /* FTS cursor handle */
182256 Fts3Expr *pExpr, /* Expr to test. May or may not be root. */
182257 int *pRc /* IN/OUT: Error code */
182258){
182259 int bHit = 1; /* Return value */
182260 if( *pRc==SQLITE_OK0 ){
182261 switch( pExpr->eType ){
182262 case FTSQUERY_NEAR1:
182263 case FTSQUERY_AND3:
182264 bHit = (
182265 fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
182266 && fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
182267 && fts3EvalNearTest(pExpr, pRc)
182268 );
182269
182270 /* If the NEAR expression does not match any rows, zero the doclist for
182271 ** all phrases involved in the NEAR. This is because the snippet(),
182272 ** offsets() and matchinfo() functions are not supposed to recognize
182273 ** any instances of phrases that are part of unmatched NEAR queries.
182274 ** For example if this expression:
182275 **
182276 ** ... MATCH 'a OR (b NEAR c)'
182277 **
182278 ** is matched against a row containing:
182279 **
182280 ** 'a b d e'
182281 **
182282 ** then any snippet() should ony highlight the "a" term, not the "b"
182283 ** (as "b" is part of a non-matching NEAR clause).
182284 */
182285 if( bHit==0
182286 && pExpr->eType==FTSQUERY_NEAR1
182287 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR1)
182288 ){
182289 Fts3Expr *p;
182290 for(p=pExpr; p->pPhrase==0; p=p->pLeft){
182291 if( p->pRight->iDocid==pCsr->iPrevId ){
182292 fts3EvalInvalidatePoslist(p->pRight->pPhrase);
182293 }
182294 }
182295 if( p->iDocid==pCsr->iPrevId ){
182296 fts3EvalInvalidatePoslist(p->pPhrase);
182297 }
182298 }
182299
182300 break;
182301
182302 case FTSQUERY_OR4: {
182303 int bHit1 = fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc);
182304 int bHit2 = fts3EvalTestExpr(pCsr, pExpr->pRight, pRc);
182305 bHit = bHit1 || bHit2;
182306 break;
182307 }
182308
182309 case FTSQUERY_NOT2:
182310 bHit = (
182311 fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
182312 && !fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
182313 );
182314 break;
182315
182316 default: {
182317#ifndef SQLITE_DISABLE_FTS4_DEFERRED
182318 if( pCsr->pDeferred && (pExpr->bDeferred || (
182319 pExpr->iDocid==pCsr->iPrevId && pExpr->pPhrase->doclist.pList
182320 ))){
182321 Fts3Phrase *pPhrase = pExpr->pPhrase;
182322 if( pExpr->bDeferred ){
182323 fts3EvalInvalidatePoslist(pPhrase);
182324 }
182325 *pRc = fts3EvalDeferredPhrase(pCsr, pPhrase);
182326 bHit = (pPhrase->doclist.pList!=0);
182327 pExpr->iDocid = pCsr->iPrevId;
182328 }else
182329#endif
182330 {
182331 bHit = (
182332 pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId
182333 && pExpr->pPhrase->doclist.nList>0
182334 );
182335 }
182336 break;
182337 }
182338 }
182339 }
182340 return bHit;
182341}
182342
182343/*
182344** This function is called as the second part of each xNext operation when
182345** iterating through the results of a full-text query. At this point the
182346** cursor points to a row that matches the query expression, with the
182347** following caveats:
182348**
182349** * Up until this point, "NEAR" operators in the expression have been
182350** treated as "AND".
182351**
182352** * Deferred tokens have not yet been considered.
182353**
182354** If *pRc is not SQLITE_OK when this function is called, it immediately
182355** returns 0. Otherwise, it tests whether or not after considering NEAR
182356** operators and deferred tokens the current row is still a match for the
182357** expression. It returns 1 if both of the following are true:
182358**
182359** 1. *pRc is SQLITE_OK when this function returns, and
182360**
182361** 2. After scanning the current FTS table row for the deferred tokens,
182362** it is determined that the row does *not* match the query.
182363**
182364** Or, if no error occurs and it seems the current row does match the FTS
182365** query, return 0.
182366*/
182367SQLITE_PRIVATEstatic int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc){
182368 int rc = *pRc;
182369 int bMiss = 0;
182370 if( rc==SQLITE_OK0 ){
182371
182372 /* If there are one or more deferred tokens, load the current row into
182373 ** memory and scan it to determine the position list for each deferred
182374 ** token. Then, see if this row is really a match, considering deferred
182375 ** tokens and NEAR operators (neither of which were taken into account
182376 ** earlier, by fts3EvalNextRow()).
182377 */
182378 if( pCsr->pDeferred ){
182379 rc = fts3CursorSeek(0, pCsr);
182380 if( rc==SQLITE_OK0 ){
182381 rc = sqlite3Fts3CacheDeferredDoclists(pCsr);
182382 }
182383 }
182384 bMiss = (0==fts3EvalTestExpr(pCsr, pCsr->pExpr, &rc));
182385
182386 /* Free the position-lists accumulated for each deferred token above. */
182387 sqlite3Fts3FreeDeferredDoclists(pCsr);
182388 *pRc = rc;
182389 }
182390 return (rc==SQLITE_OK0 && bMiss);
182391}
182392
182393/*
182394** Advance to the next document that matches the FTS expression in
182395** Fts3Cursor.pExpr.
182396*/
182397static int fts3EvalNext(Fts3Cursor *pCsr){
182398 int rc = SQLITE_OK0; /* Return Code */
182399 Fts3Expr *pExpr = pCsr->pExpr;
182400 assert( pCsr->isEof==0 )((void) (0));
182401 if( pExpr==0 ){
182402 pCsr->isEof = 1;
182403 }else{
182404 do {
182405 if( pCsr->isRequireSeek==0 ){
182406 sqlite3_reset(pCsr->pStmt);
182407 }
182408 assert( sqlite3_data_count(pCsr->pStmt)==0 )((void) (0));
182409 fts3EvalNextRow(pCsr, pExpr, &rc);
182410 pCsr->isEof = pExpr->bEof;
182411 pCsr->isRequireSeek = 1;
182412 pCsr->isMatchinfoNeeded = 1;
182413 pCsr->iPrevId = pExpr->iDocid;
182414 }while( pCsr->isEof==0 && sqlite3Fts3EvalTestDeferred(pCsr, &rc) );
182415 }
182416
182417 /* Check if the cursor is past the end of the docid range specified
182418 ** by Fts3Cursor.iMinDocid/iMaxDocid. If so, set the EOF flag. */
182419 if( rc==SQLITE_OK0 && (
182420 (pCsr->bDesc==0 && pCsr->iPrevId>pCsr->iMaxDocid)
182421 || (pCsr->bDesc!=0 && pCsr->iPrevId<pCsr->iMinDocid)
182422 )){
182423 pCsr->isEof = 1;
182424 }
182425
182426 return rc;
182427}
182428
182429/*
182430** Restart interation for expression pExpr so that the next call to
182431** fts3EvalNext() visits the first row. Do not allow incremental
182432** loading or merging of phrase doclists for this iteration.
182433**
182434** If *pRc is other than SQLITE_OK when this function is called, it is
182435** a no-op. If an error occurs within this function, *pRc is set to an
182436** SQLite error code before returning.
182437*/
182438static void fts3EvalRestart(
182439 Fts3Cursor *pCsr,
182440 Fts3Expr *pExpr,
182441 int *pRc
182442){
182443 if( pExpr && *pRc==SQLITE_OK0 ){
182444 Fts3Phrase *pPhrase = pExpr->pPhrase;
182445
182446 if( pPhrase ){
182447 fts3EvalInvalidatePoslist(pPhrase);
182448 if( pPhrase->bIncr ){
182449 int i;
182450 for(i=0; i<pPhrase->nToken; i++){
182451 Fts3PhraseToken *pToken = &pPhrase->aToken[i];
182452 assert( pToken->pDeferred==0 )((void) (0));
182453 if( pToken->pSegcsr ){
182454 sqlite3Fts3MsrIncrRestart(pToken->pSegcsr);
182455 }
182456 }
182457 *pRc = fts3EvalPhraseStart(pCsr, 0, pPhrase);
182458 }
182459 pPhrase->doclist.pNextDocid = 0;
182460 pPhrase->doclist.iDocid = 0;
182461 pPhrase->pOrPoslist = 0;
182462 }
182463
182464 pExpr->iDocid = 0;
182465 pExpr->bEof = 0;
182466 pExpr->bStart = 0;
182467
182468 fts3EvalRestart(pCsr, pExpr->pLeft, pRc);
182469 fts3EvalRestart(pCsr, pExpr->pRight, pRc);
182470 }
182471}
182472
182473/*
182474** After allocating the Fts3Expr.aMI[] array for each phrase in the
182475** expression rooted at pExpr, the cursor iterates through all rows matched
182476** by pExpr, calling this function for each row. This function increments
182477** the values in Fts3Expr.aMI[] according to the position-list currently
182478** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase
182479** expression nodes.
182480*/
182481static void fts3EvalUpdateCounts(Fts3Expr *pExpr, int nCol){
182482 if( pExpr ){
182483 Fts3Phrase *pPhrase = pExpr->pPhrase;
182484 if( pPhrase && pPhrase->doclist.pList ){
182485 int iCol = 0;
182486 char *p = pPhrase->doclist.pList;
182487
182488 do{
182489 u8 c = 0;
182490 int iCnt = 0;
182491 while( 0xFE & (*p | c) ){
182492 if( (c&0x80)==0 ) iCnt++;
182493 c = *p++ & 0x80;
182494 }
182495
182496 /* aMI[iCol*3 + 1] = Number of occurrences
182497 ** aMI[iCol*3 + 2] = Number of rows containing at least one instance
182498 */
182499 pExpr->aMI[iCol*3 + 1] += iCnt;
182500 pExpr->aMI[iCol*3 + 2] += (iCnt>0);
182501 if( *p==0x00 ) break;
182502 p++;
182503 p += fts3GetVarint32(p, &iCol)( (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, &iCol)
: (*&iCol=*(u8*)(p), 1) )
;
182504 }while( iCol<nCol );
182505 }
182506
182507 fts3EvalUpdateCounts(pExpr->pLeft, nCol);
182508 fts3EvalUpdateCounts(pExpr->pRight, nCol);
182509 }
182510}
182511
182512/*
182513** Expression pExpr must be of type FTSQUERY_PHRASE.
182514**
182515** If it is not already allocated and populated, this function allocates and
182516** populates the Fts3Expr.aMI[] array for expression pExpr. If pExpr is part
182517** of a NEAR expression, then it also allocates and populates the same array
182518** for all other phrases that are part of the NEAR expression.
182519**
182520** SQLITE_OK is returned if the aMI[] array is successfully allocated and
182521** populated. Otherwise, if an error occurs, an SQLite error code is returned.
182522*/
182523static int fts3EvalGatherStats(
182524 Fts3Cursor *pCsr, /* Cursor object */
182525 Fts3Expr *pExpr /* FTSQUERY_PHRASE expression */
182526){
182527 int rc = SQLITE_OK0; /* Return code */
182528
182529 assert( pExpr->eType==FTSQUERY_PHRASE )((void) (0));
182530 if( pExpr->aMI==0 ){
182531 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
182532 Fts3Expr *pRoot; /* Root of NEAR expression */
182533 Fts3Expr *p; /* Iterator used for several purposes */
182534
182535 sqlite3_int64 iPrevId = pCsr->iPrevId;
182536 sqlite3_int64 iDocid;
182537 u8 bEof;
182538
182539 /* Find the root of the NEAR expression */
182540 pRoot = pExpr;
182541 while( pRoot->pParent && pRoot->pParent->eType==FTSQUERY_NEAR1 ){
182542 pRoot = pRoot->pParent;
182543 }
182544 iDocid = pRoot->iDocid;
182545 bEof = pRoot->bEof;
182546 assert( pRoot->bStart )((void) (0));
182547
182548 /* Allocate space for the aMSI[] array of each FTSQUERY_PHRASE node */
182549 for(p=pRoot; p; p=p->pLeft){
182550 Fts3Expr *pE = (p->eType==FTSQUERY_PHRASE5?p:p->pRight);
182551 assert( pE->aMI==0 )((void) (0));
182552 pE->aMI = (u32 *)sqlite3_malloc64(pTab->nColumn * 3 * sizeof(u32));
182553 if( !pE->aMI ) return SQLITE_NOMEM7;
182554 memset(pE->aMI, 0, pTab->nColumn * 3 * sizeof(u32));
182555 }
182556
182557 fts3EvalRestart(pCsr, pRoot, &rc);
182558
182559 while( pCsr->isEof==0 && rc==SQLITE_OK0 ){
182560
182561 do {
182562 /* Ensure the %_content statement is reset. */
182563 if( pCsr->isRequireSeek==0 ) sqlite3_reset(pCsr->pStmt);
182564 assert( sqlite3_data_count(pCsr->pStmt)==0 )((void) (0));
182565
182566 /* Advance to the next document */
182567 fts3EvalNextRow(pCsr, pRoot, &rc);
182568 pCsr->isEof = pRoot->bEof;
182569 pCsr->isRequireSeek = 1;
182570 pCsr->isMatchinfoNeeded = 1;
182571 pCsr->iPrevId = pRoot->iDocid;
182572 }while( pCsr->isEof==0
182573 && pRoot->eType==FTSQUERY_NEAR1
182574 && sqlite3Fts3EvalTestDeferred(pCsr, &rc)
182575 );
182576
182577 if( rc==SQLITE_OK0 && pCsr->isEof==0 ){
182578 fts3EvalUpdateCounts(pRoot, pTab->nColumn);
182579 }
182580 }
182581
182582 pCsr->isEof = 0;
182583 pCsr->iPrevId = iPrevId;
182584
182585 if( bEof ){
182586 pRoot->bEof = bEof;
182587 }else{
182588 /* Caution: pRoot may iterate through docids in ascending or descending
182589 ** order. For this reason, even though it seems more defensive, the
182590 ** do loop can not be written:
182591 **
182592 ** do {...} while( pRoot->iDocid<iDocid && rc==SQLITE_OK );
182593 */
182594 fts3EvalRestart(pCsr, pRoot, &rc);
182595 do {
182596 fts3EvalNextRow(pCsr, pRoot, &rc);
182597 assert_fts3_nc( pRoot->bEof==0 )((void) (0));
182598 if( pRoot->bEof ) rc = FTS_CORRUPT_VTAB(11 | (1<<8));
182599 }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK0 );
182600 }
182601 }
182602 return rc;
182603}
182604
182605/*
182606** This function is used by the matchinfo() module to query a phrase
182607** expression node for the following information:
182608**
182609** 1. The total number of occurrences of the phrase in each column of
182610** the FTS table (considering all rows), and
182611**
182612** 2. For each column, the number of rows in the table for which the
182613** column contains at least one instance of the phrase.
182614**
182615** If no error occurs, SQLITE_OK is returned and the values for each column
182616** written into the array aiOut as follows:
182617**
182618** aiOut[iCol*3 + 1] = Number of occurrences
182619** aiOut[iCol*3 + 2] = Number of rows containing at least one instance
182620**
182621** Caveats:
182622**
182623** * If a phrase consists entirely of deferred tokens, then all output
182624** values are set to the number of documents in the table. In other
182625** words we assume that very common tokens occur exactly once in each
182626** column of each row of the table.
182627**
182628** * If a phrase contains some deferred tokens (and some non-deferred
182629** tokens), count the potential occurrence identified by considering
182630** the non-deferred tokens instead of actual phrase occurrences.
182631**
182632** * If the phrase is part of a NEAR expression, then only phrase instances
182633** that meet the NEAR constraint are included in the counts.
182634*/
182635SQLITE_PRIVATEstatic int sqlite3Fts3EvalPhraseStats(
182636 Fts3Cursor *pCsr, /* FTS cursor handle */
182637 Fts3Expr *pExpr, /* Phrase expression */
182638 u32 *aiOut /* Array to write results into (see above) */
182639){
182640 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
182641 int rc = SQLITE_OK0;
182642 int iCol;
182643
182644 if( pExpr->bDeferred && pExpr->pParent->eType!=FTSQUERY_NEAR1 ){
182645 assert( pCsr->nDoc>0 )((void) (0));
182646 for(iCol=0; iCol<pTab->nColumn; iCol++){
182647 aiOut[iCol*3 + 1] = (u32)pCsr->nDoc;
182648 aiOut[iCol*3 + 2] = (u32)pCsr->nDoc;
182649 }
182650 }else{
182651 rc = fts3EvalGatherStats(pCsr, pExpr);
182652 if( rc==SQLITE_OK0 ){
182653 assert( pExpr->aMI )((void) (0));
182654 for(iCol=0; iCol<pTab->nColumn; iCol++){
182655 aiOut[iCol*3 + 1] = pExpr->aMI[iCol*3 + 1];
182656 aiOut[iCol*3 + 2] = pExpr->aMI[iCol*3 + 2];
182657 }
182658 }
182659 }
182660
182661 return rc;
182662}
182663
182664/*
182665** The expression pExpr passed as the second argument to this function
182666** must be of type FTSQUERY_PHRASE.
182667**
182668** The returned value is either NULL or a pointer to a buffer containing
182669** a position-list indicating the occurrences of the phrase in column iCol
182670** of the current row.
182671**
182672** More specifically, the returned buffer contains 1 varint for each
182673** occurrence of the phrase in the column, stored using the normal (delta+2)
182674** compression and is terminated by either an 0x01 or 0x00 byte. For example,
182675** if the requested column contains "a b X c d X X" and the position-list
182676** for 'X' is requested, the buffer returned may contain:
182677**
182678** 0x04 0x05 0x03 0x01 or 0x04 0x05 0x03 0x00
182679**
182680** This function works regardless of whether or not the phrase is deferred,
182681** incremental, or neither.
182682*/
182683SQLITE_PRIVATEstatic int sqlite3Fts3EvalPhrasePoslist(
182684 Fts3Cursor *pCsr, /* FTS3 cursor object */
182685 Fts3Expr *pExpr, /* Phrase to return doclist for */
182686 int iCol, /* Column to return position list for */
182687 char **ppOut /* OUT: Pointer to position list */
182688){
182689 Fts3Phrase *pPhrase = pExpr->pPhrase;
182690 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
182691 char *pIter;
182692 int iThis;
182693 sqlite3_int64 iDocid;
182694
182695 /* If this phrase is applies specifically to some column other than
182696 ** column iCol, return a NULL pointer. */
182697 *ppOut = 0;
182698 assert( iCol>=0 && iCol<pTab->nColumn )((void) (0));
182699 if( (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol) ){
182700 return SQLITE_OK0;
182701 }
182702
182703 iDocid = pExpr->iDocid;
182704 pIter = pPhrase->doclist.pList;
182705 if( iDocid!=pCsr->iPrevId || pExpr->bEof ){
182706 int rc = SQLITE_OK0;
182707 int bDescDoclist = pTab->bDescIdx; /* For DOCID_CMP macro */
182708 int bOr = 0;
182709 u8 bTreeEof = 0;
182710 Fts3Expr *p; /* Used to iterate from pExpr to root */
182711 Fts3Expr *pNear; /* Most senior NEAR ancestor (or pExpr) */
182712 int bMatch;
182713
182714 /* Check if this phrase descends from an OR expression node. If not,
182715 ** return NULL. Otherwise, the entry that corresponds to docid
182716 ** pCsr->iPrevId may lie earlier in the doclist buffer. Or, if the
182717 ** tree that the node is part of has been marked as EOF, but the node
182718 ** itself is not EOF, then it may point to an earlier entry. */
182719 pNear = pExpr;
182720 for(p=pExpr->pParent; p; p=p->pParent){
182721 if( p->eType==FTSQUERY_OR4 ) bOr = 1;
182722 if( p->eType==FTSQUERY_NEAR1 ) pNear = p;
182723 if( p->bEof ) bTreeEof = 1;
182724 }
182725 if( bOr==0 ) return SQLITE_OK0;
182726
182727 /* This is the descendent of an OR node. In this case we cannot use
182728 ** an incremental phrase. Load the entire doclist for the phrase
182729 ** into memory in this case. */
182730 if( pPhrase->bIncr ){
182731 int bEofSave = pNear->bEof;
182732 fts3EvalRestart(pCsr, pNear, &rc);
182733 while( rc==SQLITE_OK0 && !pNear->bEof ){
182734 fts3EvalNextRow(pCsr, pNear, &rc);
182735 if( bEofSave==0 && pNear->iDocid==iDocid ) break;
182736 }
182737 assert( rc!=SQLITE_OK || pPhrase->bIncr==0 )((void) (0));
182738 if( rc==SQLITE_OK0 && pNear->bEof!=bEofSave ){
182739 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
182740 }
182741 }
182742 if( bTreeEof ){
182743 while( rc==SQLITE_OK0 && !pNear->bEof ){
182744 fts3EvalNextRow(pCsr, pNear, &rc);
182745 }
182746 }
182747 if( rc!=SQLITE_OK0 ) return rc;
182748
182749 bMatch = 1;
182750 for(p=pNear; p; p=p->pLeft){
182751 u8 bEof = 0;
182752 Fts3Expr *pTest = p;
182753 Fts3Phrase *pPh;
182754 assert( pTest->eType==FTSQUERY_NEAR || pTest->eType==FTSQUERY_PHRASE )((void) (0));
182755 if( pTest->eType==FTSQUERY_NEAR1 ) pTest = pTest->pRight;
182756 assert( pTest->eType==FTSQUERY_PHRASE )((void) (0));
182757 pPh = pTest->pPhrase;
182758
182759 pIter = pPh->pOrPoslist;
182760 iDocid = pPh->iOrDocid;
182761 if( pCsr->bDesc==bDescDoclist ){
182762 bEof = !pPh->doclist.nAll ||
182763 (pIter >= (pPh->doclist.aAll + pPh->doclist.nAll));
182764 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)((bDescDoclist?-1:1) * (iDocid>pCsr->iPrevId?1:((iDocid
==pCsr->iPrevId)?0:-1)))
<0 ) && bEof==0 ){
182765 sqlite3Fts3DoclistNext(
182766 bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
182767 &pIter, &iDocid, &bEof
182768 );
182769 }
182770 }else{
182771 bEof = !pPh->doclist.nAll || (pIter && pIter<=pPh->doclist.aAll);
182772 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)((bDescDoclist?-1:1) * (iDocid>pCsr->iPrevId?1:((iDocid
==pCsr->iPrevId)?0:-1)))
>0 ) && bEof==0 ){
182773 int dummy;
182774 sqlite3Fts3DoclistPrev(
182775 bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
182776 &pIter, &iDocid, &dummy, &bEof
182777 );
182778 }
182779 }
182780 pPh->pOrPoslist = pIter;
182781 pPh->iOrDocid = iDocid;
182782 if( bEof || iDocid!=pCsr->iPrevId ) bMatch = 0;
182783 }
182784
182785 if( bMatch ){
182786 pIter = pPhrase->pOrPoslist;
182787 }else{
182788 pIter = 0;
182789 }
182790 }
182791 if( pIter==0 ) return SQLITE_OK0;
182792
182793 if( *pIter==0x01 ){
182794 pIter++;
182795 pIter += fts3GetVarint32(pIter, &iThis)( (*(u8*)(pIter)&0x80) ? sqlite3Fts3GetVarint32(pIter, &
iThis) : (*&iThis=*(u8*)(pIter), 1) )
;
182796 }else{
182797 iThis = 0;
182798 }
182799 while( iThis<iCol ){
182800 fts3ColumnlistCopy(0, &pIter);
182801 if( *pIter==0x00 ) return SQLITE_OK0;
182802 pIter++;
182803 pIter += fts3GetVarint32(pIter, &iThis)( (*(u8*)(pIter)&0x80) ? sqlite3Fts3GetVarint32(pIter, &
iThis) : (*&iThis=*(u8*)(pIter), 1) )
;
182804 }
182805 if( *pIter==0x00 ){
182806 pIter = 0;
182807 }
182808
182809 *ppOut = ((iCol==iThis)?pIter:0);
182810 return SQLITE_OK0;
182811}
182812
182813/*
182814** Free all components of the Fts3Phrase structure that were allocated by
182815** the eval module. Specifically, this means to free:
182816**
182817** * the contents of pPhrase->doclist, and
182818** * any Fts3MultiSegReader objects held by phrase tokens.
182819*/
182820SQLITE_PRIVATEstatic void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *pPhrase){
182821 if( pPhrase ){
182822 int i;
182823 sqlite3_free(pPhrase->doclist.aAll);
182824 fts3EvalInvalidatePoslist(pPhrase);
182825 memset(&pPhrase->doclist, 0, sizeof(Fts3Doclist));
182826 for(i=0; i<pPhrase->nToken; i++){
182827 fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr);
182828 pPhrase->aToken[i].pSegcsr = 0;
182829 }
182830 }
182831}
182832
182833
182834/*
182835** Return SQLITE_CORRUPT_VTAB.
182836*/
182837#ifdef SQLITE_DEBUG
182838SQLITE_PRIVATEstatic int sqlite3Fts3Corrupt(){
182839 return SQLITE_CORRUPT_VTAB(11 | (1<<8));
182840}
182841#endif
182842
182843#if !SQLITE_CORE1
182844/*
182845** Initialize API pointer table, if required.
182846*/
182847#ifdef _WIN32
182848__declspec(dllexport)
182849#endif
182850SQLITE_API int sqlite3_fts3_init(
182851 sqlite3 *db,
182852 char **pzErrMsg,
182853 const sqlite3_api_routines *pApi
182854){
182855 SQLITE_EXTENSION_INIT2(pApi)(void)pApi;
182856 return sqlite3Fts3Init(db);
182857}
182858#endif
182859
182860#endif
182861
182862/************** End of fts3.c ************************************************/
182863/************** Begin file fts3_aux.c ****************************************/
182864/*
182865** 2011 Jan 27
182866**
182867** The author disclaims copyright to this source code. In place of
182868** a legal notice, here is a blessing:
182869**
182870** May you do good and not evil.
182871** May you find forgiveness for yourself and forgive others.
182872** May you share freely, never taking more than you give.
182873**
182874******************************************************************************
182875**
182876*/
182877/* #include "fts3Int.h" */
182878#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
182879
182880/* #include <string.h> */
182881/* #include <assert.h> */
182882
182883typedef struct Fts3auxTable Fts3auxTable;
182884typedef struct Fts3auxCursor Fts3auxCursor;
182885
182886struct Fts3auxTable {
182887 sqlite3_vtab base; /* Base class used by SQLite core */
182888 Fts3Table *pFts3Tab;
182889};
182890
182891struct Fts3auxCursor {
182892 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
182893 Fts3MultiSegReader csr; /* Must be right after "base" */
182894 Fts3SegFilter filter;
182895 char *zStop;
182896 int nStop; /* Byte-length of string zStop */
182897 int iLangid; /* Language id to query */
182898 int isEof; /* True if cursor is at EOF */
182899 sqlite3_int64 iRowid; /* Current rowid */
182900
182901 int iCol; /* Current value of 'col' column */
182902 int nStat; /* Size of aStat[] array */
182903 struct Fts3auxColstats {
182904 sqlite3_int64 nDoc; /* 'documents' values for current csr row */
182905 sqlite3_int64 nOcc; /* 'occurrences' values for current csr row */
182906 } *aStat;
182907};
182908
182909/*
182910** Schema of the terms table.
182911*/
182912#define FTS3_AUX_SCHEMA"CREATE TABLE x(term, col, documents, occurrences, languageid HIDDEN)" \
182913 "CREATE TABLE x(term, col, documents, occurrences, languageid HIDDEN)"
182914
182915/*
182916** This function does all the work for both the xConnect and xCreate methods.
182917** These tables have no persistent representation of their own, so xConnect
182918** and xCreate are identical operations.
182919*/
182920static int fts3auxConnectMethod(
182921 sqlite3 *db, /* Database connection */
182922 void *pUnused, /* Unused */
182923 int argc, /* Number of elements in argv array */
182924 const char * const *argv, /* xCreate/xConnect argument array */
182925 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
182926 char **pzErr /* OUT: sqlite3_malloc'd error message */
182927){
182928 char const *zDb; /* Name of database (e.g. "main") */
182929 char const *zFts3; /* Name of fts3 table */
182930 int nDb; /* Result of strlen(zDb) */
182931 int nFts3; /* Result of strlen(zFts3) */
182932 sqlite3_int64 nByte; /* Bytes of space to allocate here */
182933 int rc; /* value returned by declare_vtab() */
182934 Fts3auxTable *p; /* Virtual table object to return */
182935
182936 UNUSED_PARAMETER(pUnused)(void)(pUnused);
182937
182938 /* The user should invoke this in one of two forms:
182939 **
182940 ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table);
182941 ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table-db, fts4-table);
182942 */
182943 if( argc!=4 && argc!=5 ) goto bad_args;
182944
182945 zDb = argv[1];
182946 nDb = (int)strlen(zDb);
182947 if( argc==5 ){
182948 if( nDb==4 && 0==sqlite3_strnicmp("temp", zDb, 4) ){
182949 zDb = argv[3];
182950 nDb = (int)strlen(zDb);
182951 zFts3 = argv[4];
182952 }else{
182953 goto bad_args;
182954 }
182955 }else{
182956 zFts3 = argv[3];
182957 }
182958 nFts3 = (int)strlen(zFts3);
182959
182960 rc = sqlite3_declare_vtab(db, FTS3_AUX_SCHEMA"CREATE TABLE x(term, col, documents, occurrences, languageid HIDDEN)");
182961 if( rc!=SQLITE_OK0 ) return rc;
182962
182963 nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2;
182964 p = (Fts3auxTable *)sqlite3_malloc64(nByte);
182965 if( !p ) return SQLITE_NOMEM7;
182966 memset(p, 0, nByte);
182967
182968 p->pFts3Tab = (Fts3Table *)&p[1];
182969 p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
182970 p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
182971 p->pFts3Tab->db = db;
182972 p->pFts3Tab->nIndex = 1;
182973
182974 memcpy((char *)p->pFts3Tab->zDb, zDb, nDb);
182975 memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
182976 sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
182977
182978 *ppVtab = (sqlite3_vtab *)p;
182979 return SQLITE_OK0;
182980
182981 bad_args:
182982 sqlite3Fts3ErrMsg(pzErr, "invalid arguments to fts4aux constructor");
182983 return SQLITE_ERROR1;
182984}
182985
182986/*
182987** This function does the work for both the xDisconnect and xDestroy methods.
182988** These tables have no persistent representation of their own, so xDisconnect
182989** and xDestroy are identical operations.
182990*/
182991static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){
182992 Fts3auxTable *p = (Fts3auxTable *)pVtab;
182993 Fts3Table *pFts3 = p->pFts3Tab;
182994 int i;
182995
182996 /* Free any prepared statements held */
182997 for(i=0; i<SizeofArray(pFts3->aStmt)((int)(sizeof(pFts3->aStmt)/sizeof(pFts3->aStmt[0]))); i++){
182998 sqlite3_finalize(pFts3->aStmt[i]);
182999 }
183000 sqlite3_free(pFts3->zSegmentsTbl);
183001 sqlite3_free(p);
183002 return SQLITE_OK0;
183003}
183004
183005#define FTS4AUX_EQ_CONSTRAINT1 1
183006#define FTS4AUX_GE_CONSTRAINT2 2
183007#define FTS4AUX_LE_CONSTRAINT4 4
183008
183009/*
183010** xBestIndex - Analyze a WHERE and ORDER BY clause.
183011*/
183012static int fts3auxBestIndexMethod(
183013 sqlite3_vtab *pVTab,
183014 sqlite3_index_info *pInfo
183015){
183016 int i;
183017 int iEq = -1;
183018 int iGe = -1;
183019 int iLe = -1;
183020 int iLangid = -1;
183021 int iNext = 1; /* Next free argvIndex value */
183022
183023 UNUSED_PARAMETER(pVTab)(void)(pVTab);
183024
183025 /* This vtab delivers always results in "ORDER BY term ASC" order. */
183026 if( pInfo->nOrderBy==1
183027 && pInfo->aOrderBy[0].iColumn==0
183028 && pInfo->aOrderBy[0].desc==0
183029 ){
183030 pInfo->orderByConsumed = 1;
183031 }
183032
183033 /* Search for equality and range constraints on the "term" column.
183034 ** And equality constraints on the hidden "languageid" column. */
183035 for(i=0; i<pInfo->nConstraint; i++){
183036 if( pInfo->aConstraint[i].usable ){
183037 int op = pInfo->aConstraint[i].op;
183038 int iCol = pInfo->aConstraint[i].iColumn;
183039
183040 if( iCol==0 ){
183041 if( op==SQLITE_INDEX_CONSTRAINT_EQ2 ) iEq = i;
183042 if( op==SQLITE_INDEX_CONSTRAINT_LT16 ) iLe = i;
183043 if( op==SQLITE_INDEX_CONSTRAINT_LE8 ) iLe = i;
183044 if( op==SQLITE_INDEX_CONSTRAINT_GT4 ) iGe = i;
183045 if( op==SQLITE_INDEX_CONSTRAINT_GE32 ) iGe = i;
183046 }
183047 if( iCol==4 ){
183048 if( op==SQLITE_INDEX_CONSTRAINT_EQ2 ) iLangid = i;
183049 }
183050 }
183051 }
183052
183053 if( iEq>=0 ){
183054 pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT1;
183055 pInfo->aConstraintUsage[iEq].argvIndex = iNext++;
183056 pInfo->estimatedCost = 5;
183057 }else{
183058 pInfo->idxNum = 0;
183059 pInfo->estimatedCost = 20000;
183060 if( iGe>=0 ){
183061 pInfo->idxNum += FTS4AUX_GE_CONSTRAINT2;
183062 pInfo->aConstraintUsage[iGe].argvIndex = iNext++;
183063 pInfo->estimatedCost /= 2;
183064 }
183065 if( iLe>=0 ){
183066 pInfo->idxNum += FTS4AUX_LE_CONSTRAINT4;
183067 pInfo->aConstraintUsage[iLe].argvIndex = iNext++;
183068 pInfo->estimatedCost /= 2;
183069 }
183070 }
183071 if( iLangid>=0 ){
183072 pInfo->aConstraintUsage[iLangid].argvIndex = iNext++;
183073 pInfo->estimatedCost--;
183074 }
183075
183076 return SQLITE_OK0;
183077}
183078
183079/*
183080** xOpen - Open a cursor.
183081*/
183082static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
183083 Fts3auxCursor *pCsr; /* Pointer to cursor object to return */
183084
183085 UNUSED_PARAMETER(pVTab)(void)(pVTab);
183086
183087 pCsr = (Fts3auxCursor *)sqlite3_malloc(sizeof(Fts3auxCursor));
183088 if( !pCsr ) return SQLITE_NOMEM7;
183089 memset(pCsr, 0, sizeof(Fts3auxCursor));
183090
183091 *ppCsr = (sqlite3_vtab_cursor *)pCsr;
183092 return SQLITE_OK0;
183093}
183094
183095/*
183096** xClose - Close a cursor.
183097*/
183098static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){
183099 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
183100 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
183101
183102 sqlite3Fts3SegmentsClose(pFts3);
183103 sqlite3Fts3SegReaderFinish(&pCsr->csr);
183104 sqlite3_free((void *)pCsr->filter.zTerm);
183105 sqlite3_free(pCsr->zStop);
183106 sqlite3_free(pCsr->aStat);
183107 sqlite3_free(pCsr);
183108 return SQLITE_OK0;
183109}
183110
183111static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){
183112 if( nSize>pCsr->nStat ){
183113 struct Fts3auxColstats *aNew;
183114 aNew = (struct Fts3auxColstats *)sqlite3_realloc64(pCsr->aStat,
183115 sizeof(struct Fts3auxColstats) * nSize
183116 );
183117 if( aNew==0 ) return SQLITE_NOMEM7;
183118 memset(&aNew[pCsr->nStat], 0,
183119 sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
183120 );
183121 pCsr->aStat = aNew;
183122 pCsr->nStat = nSize;
183123 }
183124 return SQLITE_OK0;
183125}
183126
183127/*
183128** xNext - Advance the cursor to the next row, if any.
183129*/
183130static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){
183131 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
183132 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
183133 int rc;
183134
183135 /* Increment our pretend rowid value. */
183136 pCsr->iRowid++;
183137
183138 for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
183139 if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK0;
183140 }
183141
183142 rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
183143 if( rc==SQLITE_ROW100 ){
183144 int i = 0;
183145 int nDoclist = pCsr->csr.nDoclist;
183146 char *aDoclist = pCsr->csr.aDoclist;
183147 int iCol;
183148
183149 int eState = 0;
183150
183151 if( pCsr->zStop ){
183152 int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
183153 int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n);
183154 if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
183155 pCsr->isEof = 1;
183156 return SQLITE_OK0;
183157 }
183158 }
183159
183160 if( fts3auxGrowStatArray(pCsr, 2) ) return SQLITE_NOMEM7;
183161 memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat);
183162 iCol = 0;
183163 rc = SQLITE_OK0;
183164
183165 while( i<nDoclist ){
183166 sqlite3_int64 v = 0;
183167
183168 i += sqlite3Fts3GetVarint(&aDoclist[i], &v);
183169 switch( eState ){
183170 /* State 0. In this state the integer just read was a docid. */
183171 case 0:
183172 pCsr->aStat[0].nDoc++;
183173 eState = 1;
183174 iCol = 0;
183175 break;
183176
183177 /* State 1. In this state we are expecting either a 1, indicating
183178 ** that the following integer will be a column number, or the
183179 ** start of a position list for column 0.
183180 **
183181 ** The only difference between state 1 and state 2 is that if the
183182 ** integer encountered in state 1 is not 0 or 1, then we need to
183183 ** increment the column 0 "nDoc" count for this term.
183184 */
183185 case 1:
183186 assert( iCol==0 )((void) (0));
183187 if( v>1 ){
183188 pCsr->aStat[1].nDoc++;
183189 }
183190 eState = 2;
183191 /* fall through */
183192
183193 case 2:
183194 if( v==0 ){ /* 0x00. Next integer will be a docid. */
183195 eState = 0;
183196 }else if( v==1 ){ /* 0x01. Next integer will be a column number. */
183197 eState = 3;
183198 }else{ /* 2 or greater. A position. */
183199 pCsr->aStat[iCol+1].nOcc++;
183200 pCsr->aStat[0].nOcc++;
183201 }
183202 break;
183203
183204 /* State 3. The integer just read is a column number. */
183205 default: assert( eState==3 )((void) (0));
183206 iCol = (int)v;
183207 if( iCol<1 ){
183208 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
183209 break;
183210 }
183211 if( fts3auxGrowStatArray(pCsr, iCol+2) ) return SQLITE_NOMEM7;
183212 pCsr->aStat[iCol+1].nDoc++;
183213 eState = 2;
183214 break;
183215 }
183216 }
183217
183218 pCsr->iCol = 0;
183219 }else{
183220 pCsr->isEof = 1;
183221 }
183222 return rc;
183223}
183224
183225/*
183226** xFilter - Initialize a cursor to point at the start of its data.
183227*/
183228static int fts3auxFilterMethod(
183229 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
183230 int idxNum, /* Strategy index */
183231 const char *idxStr, /* Unused */
183232 int nVal, /* Number of elements in apVal */
183233 sqlite3_value **apVal /* Arguments for the indexing scheme */
183234){
183235 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
183236 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
183237 int rc;
183238 int isScan = 0;
183239 int iLangVal = 0; /* Language id to query */
183240
183241 int iEq = -1; /* Index of term=? value in apVal */
183242 int iGe = -1; /* Index of term>=? value in apVal */
183243 int iLe = -1; /* Index of term<=? value in apVal */
183244 int iLangid = -1; /* Index of languageid=? value in apVal */
183245 int iNext = 0;
183246
183247 UNUSED_PARAMETER(nVal)(void)(nVal);
183248 UNUSED_PARAMETER(idxStr)(void)(idxStr);
183249
183250 assert( idxStr==0 )((void) (0));
183251 assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0((void) (0))
183252 || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT((void) (0))
183253 || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT)((void) (0))
183254 )((void) (0));
183255
183256 if( idxNum==FTS4AUX_EQ_CONSTRAINT1 ){
183257 iEq = iNext++;
183258 }else{
183259 isScan = 1;
183260 if( idxNum & FTS4AUX_GE_CONSTRAINT2 ){
183261 iGe = iNext++;
183262 }
183263 if( idxNum & FTS4AUX_LE_CONSTRAINT4 ){
183264 iLe = iNext++;
183265 }
183266 }
183267 if( iNext<nVal ){
183268 iLangid = iNext++;
183269 }
183270
183271 /* In case this cursor is being reused, close and zero it. */
183272 testcase(pCsr->filter.zTerm);
183273 sqlite3Fts3SegReaderFinish(&pCsr->csr);
183274 sqlite3_free((void *)pCsr->filter.zTerm);
183275 sqlite3_free(pCsr->aStat);
183276 sqlite3_free(pCsr->zStop);
183277 memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
183278
183279 pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS0x00000001|FTS3_SEGMENT_IGNORE_EMPTY0x00000002;
183280 if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN0x00000010;
183281
183282 if( iEq>=0 || iGe>=0 ){
183283 const unsigned char *zStr = sqlite3_value_text(apVal[0]);
183284 assert( (iEq==0 && iGe==-1) || (iEq==-1 && iGe==0) )((void) (0));
183285 if( zStr ){
183286 pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr);
183287 if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM7;
183288 pCsr->filter.nTerm = (int)strlen(pCsr->filter.zTerm);
183289 }
183290 }
183291
183292 if( iLe>=0 ){
183293 pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iLe]));
183294 if( pCsr->zStop==0 ) return SQLITE_NOMEM7;
183295 pCsr->nStop = (int)strlen(pCsr->zStop);
183296 }
183297
183298 if( iLangid>=0 ){
183299 iLangVal = sqlite3_value_int(apVal[iLangid]);
183300
183301 /* If the user specified a negative value for the languageid, use zero
183302 ** instead. This works, as the "languageid=?" constraint will also
183303 ** be tested by the VDBE layer. The test will always be false (since
183304 ** this module will not return a row with a negative languageid), and
183305 ** so the overall query will return zero rows. */
183306 if( iLangVal<0 ) iLangVal = 0;
183307 }
183308 pCsr->iLangid = iLangVal;
183309
183310 rc = sqlite3Fts3SegReaderCursor(pFts3, iLangVal, 0, FTS3_SEGCURSOR_ALL-2,
183311 pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr
183312 );
183313 if( rc==SQLITE_OK0 ){
183314 rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
183315 }
183316
183317 if( rc==SQLITE_OK0 ) rc = fts3auxNextMethod(pCursor);
183318 return rc;
183319}
183320
183321/*
183322** xEof - Return true if the cursor is at EOF, or false otherwise.
183323*/
183324static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){
183325 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
183326 return pCsr->isEof;
183327}
183328
183329/*
183330** xColumn - Return a column value.
183331*/
183332static int fts3auxColumnMethod(
183333 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
183334 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
183335 int iCol /* Index of column to read value from */
183336){
183337 Fts3auxCursor *p = (Fts3auxCursor *)pCursor;
183338
183339 assert( p->isEof==0 )((void) (0));
183340 switch( iCol ){
183341 case 0: /* term */
183342 sqlite3_result_text(pCtx, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
183343 break;
183344
183345 case 1: /* col */
183346 if( p->iCol ){
183347 sqlite3_result_int(pCtx, p->iCol-1);
183348 }else{
183349 sqlite3_result_text(pCtx, "*", -1, SQLITE_STATIC((sqlite3_destructor_type)0));
183350 }
183351 break;
183352
183353 case 2: /* documents */
183354 sqlite3_result_int64(pCtx, p->aStat[p->iCol].nDoc);
183355 break;
183356
183357 case 3: /* occurrences */
183358 sqlite3_result_int64(pCtx, p->aStat[p->iCol].nOcc);
183359 break;
183360
183361 default: /* languageid */
183362 assert( iCol==4 )((void) (0));
183363 sqlite3_result_int(pCtx, p->iLangid);
183364 break;
183365 }
183366
183367 return SQLITE_OK0;
183368}
183369
183370/*
183371** xRowid - Return the current rowid for the cursor.
183372*/
183373static int fts3auxRowidMethod(
183374 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
183375 sqlite_int64 *pRowid /* OUT: Rowid value */
183376){
183377 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
183378 *pRowid = pCsr->iRowid;
183379 return SQLITE_OK0;
183380}
183381
183382/*
183383** Register the fts3aux module with database connection db. Return SQLITE_OK
183384** if successful or an error code if sqlite3_create_module() fails.
183385*/
183386SQLITE_PRIVATEstatic int sqlite3Fts3InitAux(sqlite3 *db){
183387 static const sqlite3_module fts3aux_module = {
183388 0, /* iVersion */
183389 fts3auxConnectMethod, /* xCreate */
183390 fts3auxConnectMethod, /* xConnect */
183391 fts3auxBestIndexMethod, /* xBestIndex */
183392 fts3auxDisconnectMethod, /* xDisconnect */
183393 fts3auxDisconnectMethod, /* xDestroy */
183394 fts3auxOpenMethod, /* xOpen */
183395 fts3auxCloseMethod, /* xClose */
183396 fts3auxFilterMethod, /* xFilter */
183397 fts3auxNextMethod, /* xNext */
183398 fts3auxEofMethod, /* xEof */
183399 fts3auxColumnMethod, /* xColumn */
183400 fts3auxRowidMethod, /* xRowid */
183401 0, /* xUpdate */
183402 0, /* xBegin */
183403 0, /* xSync */
183404 0, /* xCommit */
183405 0, /* xRollback */
183406 0, /* xFindFunction */
183407 0, /* xRename */
183408 0, /* xSavepoint */
183409 0, /* xRelease */
183410 0, /* xRollbackTo */
183411 0 /* xShadowName */
183412 };
183413 int rc; /* Return code */
183414
183415 rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0);
183416 return rc;
183417}
183418
183419#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
183420
183421/************** End of fts3_aux.c ********************************************/
183422/************** Begin file fts3_expr.c ***************************************/
183423/*
183424** 2008 Nov 28
183425**
183426** The author disclaims copyright to this source code. In place of
183427** a legal notice, here is a blessing:
183428**
183429** May you do good and not evil.
183430** May you find forgiveness for yourself and forgive others.
183431** May you share freely, never taking more than you give.
183432**
183433******************************************************************************
183434**
183435** This module contains code that implements a parser for fts3 query strings
183436** (the right-hand argument to the MATCH operator). Because the supported
183437** syntax is relatively simple, the whole tokenizer/parser system is
183438** hand-coded.
183439*/
183440/* #include "fts3Int.h" */
183441#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
183442
183443/*
183444** By default, this module parses the legacy syntax that has been
183445** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS
183446** is defined, then it uses the new syntax. The differences between
183447** the new and the old syntaxes are:
183448**
183449** a) The new syntax supports parenthesis. The old does not.
183450**
183451** b) The new syntax supports the AND and NOT operators. The old does not.
183452**
183453** c) The old syntax supports the "-" token qualifier. This is not
183454** supported by the new syntax (it is replaced by the NOT operator).
183455**
183456** d) When using the old syntax, the OR operator has a greater precedence
183457** than an implicit AND. When using the new, both implicity and explicit
183458** AND operators have a higher precedence than OR.
183459**
183460** If compiled with SQLITE_TEST defined, then this module exports the
183461** symbol "int sqlite3_fts3_enable_parentheses". Setting this variable
183462** to zero causes the module to use the old syntax. If it is set to
183463** non-zero the new syntax is activated. This is so both syntaxes can
183464** be tested using a single build of testfixture.
183465**
183466** The following describes the syntax supported by the fts3 MATCH
183467** operator in a similar format to that used by the lemon parser
183468** generator. This module does not use actually lemon, it uses a
183469** custom parser.
183470**
183471** query ::= andexpr (OR andexpr)*.
183472**
183473** andexpr ::= notexpr (AND? notexpr)*.
183474**
183475** notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*.
183476** notexpr ::= LP query RP.
183477**
183478** nearexpr ::= phrase (NEAR distance_opt nearexpr)*.
183479**
183480** distance_opt ::= .
183481** distance_opt ::= / INTEGER.
183482**
183483** phrase ::= TOKEN.
183484** phrase ::= COLUMN:TOKEN.
183485** phrase ::= "TOKEN TOKEN TOKEN...".
183486*/
183487
183488#ifdef SQLITE_TEST
183489SQLITE_API int sqlite3_fts3_enable_parentheses1 = 0;
183490#else
183491# ifdef SQLITE_ENABLE_FTS3_PARENTHESIS1
183492# define sqlite3_fts3_enable_parentheses1 1
183493# else
183494# define sqlite3_fts3_enable_parentheses1 0
183495# endif
183496#endif
183497
183498/*
183499** Default span for NEAR operators.
183500*/
183501#define SQLITE_FTS3_DEFAULT_NEAR_PARAM10 10
183502
183503/* #include <string.h> */
183504/* #include <assert.h> */
183505
183506/*
183507** isNot:
183508** This variable is used by function getNextNode(). When getNextNode() is
183509** called, it sets ParseContext.isNot to true if the 'next node' is a
183510** FTSQUERY_PHRASE with a unary "-" attached to it. i.e. "mysql" in the
183511** FTS3 query "sqlite -mysql". Otherwise, ParseContext.isNot is set to
183512** zero.
183513*/
183514typedef struct ParseContext ParseContext;
183515struct ParseContext {
183516 sqlite3_tokenizer *pTokenizer; /* Tokenizer module */
183517 int iLangid; /* Language id used with tokenizer */
183518 const char **azCol; /* Array of column names for fts3 table */
183519 int bFts4; /* True to allow FTS4-only syntax */
183520 int nCol; /* Number of entries in azCol[] */
183521 int iDefaultCol; /* Default column to query */
183522 int isNot; /* True if getNextNode() sees a unary - */
183523 sqlite3_context *pCtx; /* Write error message here */
183524 int nNest; /* Number of nested brackets */
183525};
183526
183527/*
183528** This function is equivalent to the standard isspace() function.
183529**
183530** The standard isspace() can be awkward to use safely, because although it
183531** is defined to accept an argument of type int, its behavior when passed
183532** an integer that falls outside of the range of the unsigned char type
183533** is undefined (and sometimes, "undefined" means segfault). This wrapper
183534** is defined to accept an argument of type char, and always returns 0 for
183535** any values that fall outside of the range of the unsigned char type (i.e.
183536** negative values).
183537*/
183538static int fts3isspace(char c){
183539 return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
183540}
183541
183542/*
183543** Allocate nByte bytes of memory using sqlite3_malloc(). If successful,
183544** zero the memory before returning a pointer to it. If unsuccessful,
183545** return NULL.
183546*/
183547SQLITE_PRIVATEstatic void *sqlite3Fts3MallocZero(sqlite3_int64 nByte){
183548 void *pRet = sqlite3_malloc64(nByte);
183549 if( pRet ) memset(pRet, 0, nByte);
183550 return pRet;
183551}
183552
183553SQLITE_PRIVATEstatic int sqlite3Fts3OpenTokenizer(
183554 sqlite3_tokenizer *pTokenizer,
183555 int iLangid,
183556 const char *z,
183557 int n,
183558 sqlite3_tokenizer_cursor **ppCsr
183559){
183560 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
183561 sqlite3_tokenizer_cursor *pCsr = 0;
183562 int rc;
183563
183564 rc = pModule->xOpen(pTokenizer, z, n, &pCsr);
183565 assert( rc==SQLITE_OK || pCsr==0 )((void) (0));
183566 if( rc==SQLITE_OK0 ){
183567 pCsr->pTokenizer = pTokenizer;
183568 if( pModule->iVersion>=1 ){
183569 rc = pModule->xLanguageid(pCsr, iLangid);
183570 if( rc!=SQLITE_OK0 ){
183571 pModule->xClose(pCsr);
183572 pCsr = 0;
183573 }
183574 }
183575 }
183576 *ppCsr = pCsr;
183577 return rc;
183578}
183579
183580/*
183581** Function getNextNode(), which is called by fts3ExprParse(), may itself
183582** call fts3ExprParse(). So this forward declaration is required.
183583*/
183584static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *);
183585
183586/*
183587** Extract the next token from buffer z (length n) using the tokenizer
183588** and other information (column names etc.) in pParse. Create an Fts3Expr
183589** structure of type FTSQUERY_PHRASE containing a phrase consisting of this
183590** single token and set *ppExpr to point to it. If the end of the buffer is
183591** reached before a token is found, set *ppExpr to zero. It is the
183592** responsibility of the caller to eventually deallocate the allocated
183593** Fts3Expr structure (if any) by passing it to sqlite3_free().
183594**
183595** Return SQLITE_OK if successful, or SQLITE_NOMEM if a memory allocation
183596** fails.
183597*/
183598static int getNextToken(
183599 ParseContext *pParse, /* fts3 query parse context */
183600 int iCol, /* Value for Fts3Phrase.iColumn */
183601 const char *z, int n, /* Input string */
183602 Fts3Expr **ppExpr, /* OUT: expression */
183603 int *pnConsumed /* OUT: Number of bytes consumed */
183604){
183605 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
183606 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
183607 int rc;
183608 sqlite3_tokenizer_cursor *pCursor;
183609 Fts3Expr *pRet = 0;
183610 int i = 0;
183611
183612 /* Set variable i to the maximum number of bytes of input to tokenize. */
183613 for(i=0; i<n; i++){
183614 if( sqlite3_fts3_enable_parentheses1 && (z[i]=='(' || z[i]==')') ) break;
183615 if( z[i]=='"' ) break;
183616 }
183617
183618 *pnConsumed = i;
183619 rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, i, &pCursor);
183620 if( rc==SQLITE_OK0 ){
183621 const char *zToken;
183622 int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0;
183623 sqlite3_int64 nByte; /* total space to allocate */
183624
183625 rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
183626 if( rc==SQLITE_OK0 ){
183627 nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken;
183628 pRet = (Fts3Expr *)sqlite3Fts3MallocZero(nByte);
183629 if( !pRet ){
183630 rc = SQLITE_NOMEM7;
183631 }else{
183632 pRet->eType = FTSQUERY_PHRASE5;
183633 pRet->pPhrase = (Fts3Phrase *)&pRet[1];
183634 pRet->pPhrase->nToken = 1;
183635 pRet->pPhrase->iColumn = iCol;
183636 pRet->pPhrase->aToken[0].n = nToken;
183637 pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1];
183638 memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken);
183639
183640 if( iEnd<n && z[iEnd]=='*' ){
183641 pRet->pPhrase->aToken[0].isPrefix = 1;
183642 iEnd++;
183643 }
183644
183645 while( 1 ){
183646 if( !sqlite3_fts3_enable_parentheses1
183647 && iStart>0 && z[iStart-1]=='-'
183648 ){
183649 pParse->isNot = 1;
183650 iStart--;
183651 }else if( pParse->bFts4 && iStart>0 && z[iStart-1]=='^' ){
183652 pRet->pPhrase->aToken[0].bFirst = 1;
183653 iStart--;
183654 }else{
183655 break;
183656 }
183657 }
183658
183659 }
183660 *pnConsumed = iEnd;
183661 }else if( i && rc==SQLITE_DONE101 ){
183662 rc = SQLITE_OK0;
183663 }
183664
183665 pModule->xClose(pCursor);
183666 }
183667
183668 *ppExpr = pRet;
183669 return rc;
183670}
183671
183672
183673/*
183674** Enlarge a memory allocation. If an out-of-memory allocation occurs,
183675** then free the old allocation.
183676*/
183677static void *fts3ReallocOrFree(void *pOrig, sqlite3_int64 nNew){
183678 void *pRet = sqlite3_realloc64(pOrig, nNew);
183679 if( !pRet ){
183680 sqlite3_free(pOrig);
183681 }
183682 return pRet;
183683}
183684
183685/*
183686** Buffer zInput, length nInput, contains the contents of a quoted string
183687** that appeared as part of an fts3 query expression. Neither quote character
183688** is included in the buffer. This function attempts to tokenize the entire
183689** input buffer and create an Fts3Expr structure of type FTSQUERY_PHRASE
183690** containing the results.
183691**
183692** If successful, SQLITE_OK is returned and *ppExpr set to point at the
183693** allocated Fts3Expr structure. Otherwise, either SQLITE_NOMEM (out of memory
183694** error) or SQLITE_ERROR (tokenization error) is returned and *ppExpr set
183695** to 0.
183696*/
183697static int getNextString(
183698 ParseContext *pParse, /* fts3 query parse context */
183699 const char *zInput, int nInput, /* Input string */
183700 Fts3Expr **ppExpr /* OUT: expression */
183701){
183702 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
183703 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
183704 int rc;
183705 Fts3Expr *p = 0;
183706 sqlite3_tokenizer_cursor *pCursor = 0;
183707 char *zTemp = 0;
183708 int nTemp = 0;
183709
183710 const int nSpace = sizeof(Fts3Expr) + sizeof(Fts3Phrase);
183711 int nToken = 0;
183712
183713 /* The final Fts3Expr data structure, including the Fts3Phrase,
183714 ** Fts3PhraseToken structures token buffers are all stored as a single
183715 ** allocation so that the expression can be freed with a single call to
183716 ** sqlite3_free(). Setting this up requires a two pass approach.
183717 **
183718 ** The first pass, in the block below, uses a tokenizer cursor to iterate
183719 ** through the tokens in the expression. This pass uses fts3ReallocOrFree()
183720 ** to assemble data in two dynamic buffers:
183721 **
183722 ** Buffer p: Points to the Fts3Expr structure, followed by the Fts3Phrase
183723 ** structure, followed by the array of Fts3PhraseToken
183724 ** structures. This pass only populates the Fts3PhraseToken array.
183725 **
183726 ** Buffer zTemp: Contains copies of all tokens.
183727 **
183728 ** The second pass, in the block that begins "if( rc==SQLITE_DONE )" below,
183729 ** appends buffer zTemp to buffer p, and fills in the Fts3Expr and Fts3Phrase
183730 ** structures.
183731 */
183732 rc = sqlite3Fts3OpenTokenizer(
183733 pTokenizer, pParse->iLangid, zInput, nInput, &pCursor);
183734 if( rc==SQLITE_OK0 ){
183735 int ii;
183736 for(ii=0; rc==SQLITE_OK0; ii++){
183737 const char *zByte;
183738 int nByte = 0, iBegin = 0, iEnd = 0, iPos = 0;
183739 rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
183740 if( rc==SQLITE_OK0 ){
183741 Fts3PhraseToken *pToken;
183742
183743 p = fts3ReallocOrFree(p, nSpace + ii*sizeof(Fts3PhraseToken));
183744 if( !p ) goto no_mem;
183745
183746 zTemp = fts3ReallocOrFree(zTemp, nTemp + nByte);
183747 if( !zTemp ) goto no_mem;
183748
183749 assert( nToken==ii )((void) (0));
183750 pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii];
183751 memset(pToken, 0, sizeof(Fts3PhraseToken));
183752
183753 memcpy(&zTemp[nTemp], zByte, nByte);
183754 nTemp += nByte;
183755
183756 pToken->n = nByte;
183757 pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*');
183758 pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^');
183759 nToken = ii+1;
183760 }
183761 }
183762
183763 pModule->xClose(pCursor);
183764 pCursor = 0;
183765 }
183766
183767 if( rc==SQLITE_DONE101 ){
183768 int jj;
183769 char *zBuf = 0;
183770
183771 p = fts3ReallocOrFree(p, nSpace + nToken*sizeof(Fts3PhraseToken) + nTemp);
183772 if( !p ) goto no_mem;
183773 memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p);
183774 p->eType = FTSQUERY_PHRASE5;
183775 p->pPhrase = (Fts3Phrase *)&p[1];
183776 p->pPhrase->iColumn = pParse->iDefaultCol;
183777 p->pPhrase->nToken = nToken;
183778
183779 zBuf = (char *)&p->pPhrase->aToken[nToken];
183780 if( zTemp ){
183781 memcpy(zBuf, zTemp, nTemp);
183782 sqlite3_free(zTemp);
183783 }else{
183784 assert( nTemp==0 )((void) (0));
183785 }
183786
183787 for(jj=0; jj<p->pPhrase->nToken; jj++){
183788 p->pPhrase->aToken[jj].z = zBuf;
183789 zBuf += p->pPhrase->aToken[jj].n;
183790 }
183791 rc = SQLITE_OK0;
183792 }
183793
183794 *ppExpr = p;
183795 return rc;
183796no_mem:
183797
183798 if( pCursor ){
183799 pModule->xClose(pCursor);
183800 }
183801 sqlite3_free(zTemp);
183802 sqlite3_free(p);
183803 *ppExpr = 0;
183804 return SQLITE_NOMEM7;
183805}
183806
183807/*
183808** The output variable *ppExpr is populated with an allocated Fts3Expr
183809** structure, or set to 0 if the end of the input buffer is reached.
183810**
183811** Returns an SQLite error code. SQLITE_OK if everything works, SQLITE_NOMEM
183812** if a malloc failure occurs, or SQLITE_ERROR if a parse error is encountered.
183813** If SQLITE_ERROR is returned, pContext is populated with an error message.
183814*/
183815static int getNextNode(
183816 ParseContext *pParse, /* fts3 query parse context */
183817 const char *z, int n, /* Input string */
183818 Fts3Expr **ppExpr, /* OUT: expression */
183819 int *pnConsumed /* OUT: Number of bytes consumed */
183820){
183821 static const struct Fts3Keyword {
183822 char *z; /* Keyword text */
183823 unsigned char n; /* Length of the keyword */
183824 unsigned char parenOnly; /* Only valid in paren mode */
183825 unsigned char eType; /* Keyword code */
183826 } aKeyword[] = {
183827 { "OR" , 2, 0, FTSQUERY_OR4 },
183828 { "AND", 3, 1, FTSQUERY_AND3 },
183829 { "NOT", 3, 1, FTSQUERY_NOT2 },
183830 { "NEAR", 4, 0, FTSQUERY_NEAR1 }
183831 };
183832 int ii;
183833 int iCol;
183834 int iColLen;
183835 int rc;
183836 Fts3Expr *pRet = 0;
183837
183838 const char *zInput = z;
183839 int nInput = n;
183840
183841 pParse->isNot = 0;
183842
183843 /* Skip over any whitespace before checking for a keyword, an open or
183844 ** close bracket, or a quoted string.
183845 */
183846 while( nInput>0 && fts3isspace(*zInput) ){
183847 nInput--;
183848 zInput++;
183849 }
183850 if( nInput==0 ){
183851 return SQLITE_DONE101;
183852 }
183853
183854 /* See if we are dealing with a keyword. */
183855 for(ii=0; ii<(int)(sizeof(aKeyword)/sizeof(struct Fts3Keyword)); ii++){
183856 const struct Fts3Keyword *pKey = &aKeyword[ii];
183857
183858 if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses1)!=0 ){
183859 continue;
183860 }
183861
183862 if( nInput>=pKey->n && 0==memcmp(zInput, pKey->z, pKey->n) ){
183863 int nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM10;
183864 int nKey = pKey->n;
183865 char cNext;
183866
183867 /* If this is a "NEAR" keyword, check for an explicit nearness. */
183868 if( pKey->eType==FTSQUERY_NEAR1 ){
183869 assert( nKey==4 )((void) (0));
183870 if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){
183871 nKey += 1+sqlite3Fts3ReadInt(&zInput[nKey+1], &nNear);
183872 }
183873 }
183874
183875 /* At this point this is probably a keyword. But for that to be true,
183876 ** the next byte must contain either whitespace, an open or close
183877 ** parenthesis, a quote character, or EOF.
183878 */
183879 cNext = zInput[nKey];
183880 if( fts3isspace(cNext)
183881 || cNext=='"' || cNext=='(' || cNext==')' || cNext==0
183882 ){
183883 pRet = (Fts3Expr *)sqlite3Fts3MallocZero(sizeof(Fts3Expr));
183884 if( !pRet ){
183885 return SQLITE_NOMEM7;
183886 }
183887 pRet->eType = pKey->eType;
183888 pRet->nNear = nNear;
183889 *ppExpr = pRet;
183890 *pnConsumed = (int)((zInput - z) + nKey);
183891 return SQLITE_OK0;
183892 }
183893
183894 /* Turns out that wasn't a keyword after all. This happens if the
183895 ** user has supplied a token such as "ORacle". Continue.
183896 */
183897 }
183898 }
183899
183900 /* See if we are dealing with a quoted phrase. If this is the case, then
183901 ** search for the closing quote and pass the whole string to getNextString()
183902 ** for processing. This is easy to do, as fts3 has no syntax for escaping
183903 ** a quote character embedded in a string.
183904 */
183905 if( *zInput=='"' ){
183906 for(ii=1; ii<nInput && zInput[ii]!='"'; ii++);
183907 *pnConsumed = (int)((zInput - z) + ii + 1);
183908 if( ii==nInput ){
183909 return SQLITE_ERROR1;
183910 }
183911 return getNextString(pParse, &zInput[1], ii-1, ppExpr);
183912 }
183913
183914 if( sqlite3_fts3_enable_parentheses1 ){
183915 if( *zInput=='(' ){
183916 int nConsumed = 0;
183917 pParse->nNest++;
183918#if !defined(SQLITE_MAX_EXPR_DEPTH1000)
183919 if( pParse->nNest>1000 ) return SQLITE_ERROR1;
183920#elif SQLITE_MAX_EXPR_DEPTH1000>0
183921 if( pParse->nNest>SQLITE_MAX_EXPR_DEPTH1000 ) return SQLITE_ERROR1;
183922#endif
183923 rc = fts3ExprParse(pParse, zInput+1, nInput-1, ppExpr, &nConsumed);
183924 *pnConsumed = (int)(zInput - z) + 1 + nConsumed;
183925 return rc;
183926 }else if( *zInput==')' ){
183927 pParse->nNest--;
183928 *pnConsumed = (int)((zInput - z) + 1);
183929 *ppExpr = 0;
183930 return SQLITE_DONE101;
183931 }
183932 }
183933
183934 /* If control flows to this point, this must be a regular token, or
183935 ** the end of the input. Read a regular token using the sqlite3_tokenizer
183936 ** interface. Before doing so, figure out if there is an explicit
183937 ** column specifier for the token.
183938 **
183939 ** TODO: Strangely, it is not possible to associate a column specifier
183940 ** with a quoted phrase, only with a single token. Not sure if this was
183941 ** an implementation artifact or an intentional decision when fts3 was
183942 ** first implemented. Whichever it was, this module duplicates the
183943 ** limitation.
183944 */
183945 iCol = pParse->iDefaultCol;
183946 iColLen = 0;
183947 for(ii=0; ii<pParse->nCol; ii++){
183948 const char *zStr = pParse->azCol[ii];
183949 int nStr = (int)strlen(zStr);
183950 if( nInput>nStr && zInput[nStr]==':'
183951 && sqlite3_strnicmp(zStr, zInput, nStr)==0
183952 ){
183953 iCol = ii;
183954 iColLen = (int)((zInput - z) + nStr + 1);
183955 break;
183956 }
183957 }
183958 rc = getNextToken(pParse, iCol, &z[iColLen], n-iColLen, ppExpr, pnConsumed);
183959 *pnConsumed += iColLen;
183960 return rc;
183961}
183962
183963/*
183964** The argument is an Fts3Expr structure for a binary operator (any type
183965** except an FTSQUERY_PHRASE). Return an integer value representing the
183966** precedence of the operator. Lower values have a higher precedence (i.e.
183967** group more tightly). For example, in the C language, the == operator
183968** groups more tightly than ||, and would therefore have a higher precedence.
183969**
183970** When using the new fts3 query syntax (when SQLITE_ENABLE_FTS3_PARENTHESIS
183971** is defined), the order of the operators in precedence from highest to
183972** lowest is:
183973**
183974** NEAR
183975** NOT
183976** AND (including implicit ANDs)
183977** OR
183978**
183979** Note that when using the old query syntax, the OR operator has a higher
183980** precedence than the AND operator.
183981*/
183982static int opPrecedence(Fts3Expr *p){
183983 assert( p->eType!=FTSQUERY_PHRASE )((void) (0));
183984 if( sqlite3_fts3_enable_parentheses1 ){
183985 return p->eType;
183986 }else if( p->eType==FTSQUERY_NEAR1 ){
183987 return 1;
183988 }else if( p->eType==FTSQUERY_OR4 ){
183989 return 2;
183990 }
183991 assert( p->eType==FTSQUERY_AND )((void) (0));
183992 return 3;
183993}
183994
183995/*
183996** Argument ppHead contains a pointer to the current head of a query
183997** expression tree being parsed. pPrev is the expression node most recently
183998** inserted into the tree. This function adds pNew, which is always a binary
183999** operator node, into the expression tree based on the relative precedence
184000** of pNew and the existing nodes of the tree. This may result in the head
184001** of the tree changing, in which case *ppHead is set to the new root node.
184002*/
184003static void insertBinaryOperator(
184004 Fts3Expr **ppHead, /* Pointer to the root node of a tree */
184005 Fts3Expr *pPrev, /* Node most recently inserted into the tree */
184006 Fts3Expr *pNew /* New binary node to insert into expression tree */
184007){
184008 Fts3Expr *pSplit = pPrev;
184009 while( pSplit->pParent && opPrecedence(pSplit->pParent)<=opPrecedence(pNew) ){
184010 pSplit = pSplit->pParent;
184011 }
184012
184013 if( pSplit->pParent ){
184014 assert( pSplit->pParent->pRight==pSplit )((void) (0));
184015 pSplit->pParent->pRight = pNew;
184016 pNew->pParent = pSplit->pParent;
184017 }else{
184018 *ppHead = pNew;
184019 }
184020 pNew->pLeft = pSplit;
184021 pSplit->pParent = pNew;
184022}
184023
184024/*
184025** Parse the fts3 query expression found in buffer z, length n. This function
184026** returns either when the end of the buffer is reached or an unmatched
184027** closing bracket - ')' - is encountered.
184028**
184029** If successful, SQLITE_OK is returned, *ppExpr is set to point to the
184030** parsed form of the expression and *pnConsumed is set to the number of
184031** bytes read from buffer z. Otherwise, *ppExpr is set to 0 and SQLITE_NOMEM
184032** (out of memory error) or SQLITE_ERROR (parse error) is returned.
184033*/
184034static int fts3ExprParse(
184035 ParseContext *pParse, /* fts3 query parse context */
184036 const char *z, int n, /* Text of MATCH query */
184037 Fts3Expr **ppExpr, /* OUT: Parsed query structure */
184038 int *pnConsumed /* OUT: Number of bytes consumed */
184039){
184040 Fts3Expr *pRet = 0;
184041 Fts3Expr *pPrev = 0;
184042 Fts3Expr *pNotBranch = 0; /* Only used in legacy parse mode */
184043 int nIn = n;
184044 const char *zIn = z;
184045 int rc = SQLITE_OK0;
184046 int isRequirePhrase = 1;
184047
184048 while( rc==SQLITE_OK0 ){
184049 Fts3Expr *p = 0;
184050 int nByte = 0;
184051
184052 rc = getNextNode(pParse, zIn, nIn, &p, &nByte);
184053 assert( nByte>0 || (rc!=SQLITE_OK && p==0) )((void) (0));
184054 if( rc==SQLITE_OK0 ){
184055 if( p ){
184056 int isPhrase;
184057
184058 if( !sqlite3_fts3_enable_parentheses1
184059 && p->eType==FTSQUERY_PHRASE5 && pParse->isNot
184060 ){
184061 /* Create an implicit NOT operator. */
184062 Fts3Expr *pNot = sqlite3Fts3MallocZero(sizeof(Fts3Expr));
184063 if( !pNot ){
184064 sqlite3Fts3ExprFree(p);
184065 rc = SQLITE_NOMEM7;
184066 goto exprparse_out;
184067 }
184068 pNot->eType = FTSQUERY_NOT2;
184069 pNot->pRight = p;
184070 p->pParent = pNot;
184071 if( pNotBranch ){
184072 pNot->pLeft = pNotBranch;
184073 pNotBranch->pParent = pNot;
184074 }
184075 pNotBranch = pNot;
184076 p = pPrev;
184077 }else{
184078 int eType = p->eType;
184079 isPhrase = (eType==FTSQUERY_PHRASE5 || p->pLeft);
184080
184081 /* The isRequirePhrase variable is set to true if a phrase or
184082 ** an expression contained in parenthesis is required. If a
184083 ** binary operator (AND, OR, NOT or NEAR) is encounted when
184084 ** isRequirePhrase is set, this is a syntax error.
184085 */
184086 if( !isPhrase && isRequirePhrase ){
184087 sqlite3Fts3ExprFree(p);
184088 rc = SQLITE_ERROR1;
184089 goto exprparse_out;
184090 }
184091
184092 if( isPhrase && !isRequirePhrase ){
184093 /* Insert an implicit AND operator. */
184094 Fts3Expr *pAnd;
184095 assert( pRet && pPrev )((void) (0));
184096 pAnd = sqlite3Fts3MallocZero(sizeof(Fts3Expr));
184097 if( !pAnd ){
184098 sqlite3Fts3ExprFree(p);
184099 rc = SQLITE_NOMEM7;
184100 goto exprparse_out;
184101 }
184102 pAnd->eType = FTSQUERY_AND3;
184103 insertBinaryOperator(&pRet, pPrev, pAnd);
184104 pPrev = pAnd;
184105 }
184106
184107 /* This test catches attempts to make either operand of a NEAR
184108 ** operator something other than a phrase. For example, either of
184109 ** the following:
184110 **
184111 ** (bracketed expression) NEAR phrase
184112 ** phrase NEAR (bracketed expression)
184113 **
184114 ** Return an error in either case.
184115 */
184116 if( pPrev && (
184117 (eType==FTSQUERY_NEAR1 && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE5)
184118 || (eType!=FTSQUERY_PHRASE5 && isPhrase && pPrev->eType==FTSQUERY_NEAR1)
184119 )){
184120 sqlite3Fts3ExprFree(p);
184121 rc = SQLITE_ERROR1;
184122 goto exprparse_out;
184123 }
184124
184125 if( isPhrase ){
184126 if( pRet ){
184127 assert( pPrev && pPrev->pLeft && pPrev->pRight==0 )((void) (0));
184128 pPrev->pRight = p;
184129 p->pParent = pPrev;
184130 }else{
184131 pRet = p;
184132 }
184133 }else{
184134 insertBinaryOperator(&pRet, pPrev, p);
184135 }
184136 isRequirePhrase = !isPhrase;
184137 }
184138 pPrev = p;
184139 }
184140 assert( nByte>0 )((void) (0));
184141 }
184142 assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) )((void) (0));
184143 nIn -= nByte;
184144 zIn += nByte;
184145 }
184146
184147 if( rc==SQLITE_DONE101 && pRet && isRequirePhrase ){
184148 rc = SQLITE_ERROR1;
184149 }
184150
184151 if( rc==SQLITE_DONE101 ){
184152 rc = SQLITE_OK0;
184153 if( !sqlite3_fts3_enable_parentheses1 && pNotBranch ){
184154 if( !pRet ){
184155 rc = SQLITE_ERROR1;
184156 }else{
184157 Fts3Expr *pIter = pNotBranch;
184158 while( pIter->pLeft ){
184159 pIter = pIter->pLeft;
184160 }
184161 pIter->pLeft = pRet;
184162 pRet->pParent = pIter;
184163 pRet = pNotBranch;
184164 }
184165 }
184166 }
184167 *pnConsumed = n - nIn;
184168
184169exprparse_out:
184170 if( rc!=SQLITE_OK0 ){
184171 sqlite3Fts3ExprFree(pRet);
184172 sqlite3Fts3ExprFree(pNotBranch);
184173 pRet = 0;
184174 }
184175 *ppExpr = pRet;
184176 return rc;
184177}
184178
184179/*
184180** Return SQLITE_ERROR if the maximum depth of the expression tree passed
184181** as the only argument is more than nMaxDepth.
184182*/
184183static int fts3ExprCheckDepth(Fts3Expr *p, int nMaxDepth){
184184 int rc = SQLITE_OK0;
184185 if( p ){
184186 if( nMaxDepth<0 ){
184187 rc = SQLITE_TOOBIG18;
184188 }else{
184189 rc = fts3ExprCheckDepth(p->pLeft, nMaxDepth-1);
184190 if( rc==SQLITE_OK0 ){
184191 rc = fts3ExprCheckDepth(p->pRight, nMaxDepth-1);
184192 }
184193 }
184194 }
184195 return rc;
184196}
184197
184198/*
184199** This function attempts to transform the expression tree at (*pp) to
184200** an equivalent but more balanced form. The tree is modified in place.
184201** If successful, SQLITE_OK is returned and (*pp) set to point to the
184202** new root expression node.
184203**
184204** nMaxDepth is the maximum allowable depth of the balanced sub-tree.
184205**
184206** Otherwise, if an error occurs, an SQLite error code is returned and
184207** expression (*pp) freed.
184208*/
184209static int fts3ExprBalance(Fts3Expr **pp, int nMaxDepth){
184210 int rc = SQLITE_OK0; /* Return code */
184211 Fts3Expr *pRoot = *pp; /* Initial root node */
184212 Fts3Expr *pFree = 0; /* List of free nodes. Linked by pParent. */
184213 int eType = pRoot->eType; /* Type of node in this tree */
184214
184215 if( nMaxDepth==0 ){
184216 rc = SQLITE_ERROR1;
184217 }
184218
184219 if( rc==SQLITE_OK0 ){
184220 if( (eType==FTSQUERY_AND3 || eType==FTSQUERY_OR4) ){
184221 Fts3Expr **apLeaf;
184222 apLeaf = (Fts3Expr **)sqlite3_malloc64(sizeof(Fts3Expr *) * nMaxDepth);
184223 if( 0==apLeaf ){
184224 rc = SQLITE_NOMEM7;
184225 }else{
184226 memset(apLeaf, 0, sizeof(Fts3Expr *) * nMaxDepth);
184227 }
184228
184229 if( rc==SQLITE_OK0 ){
184230 int i;
184231 Fts3Expr *p;
184232
184233 /* Set $p to point to the left-most leaf in the tree of eType nodes. */
184234 for(p=pRoot; p->eType==eType; p=p->pLeft){
184235 assert( p->pParent==0 || p->pParent->pLeft==p )((void) (0));
184236 assert( p->pLeft && p->pRight )((void) (0));
184237 }
184238
184239 /* This loop runs once for each leaf in the tree of eType nodes. */
184240 while( 1 ){
184241 int iLvl;
184242 Fts3Expr *pParent = p->pParent; /* Current parent of p */
184243
184244 assert( pParent==0 || pParent->pLeft==p )((void) (0));
184245 p->pParent = 0;
184246 if( pParent ){
184247 pParent->pLeft = 0;
184248 }else{
184249 pRoot = 0;
184250 }
184251 rc = fts3ExprBalance(&p, nMaxDepth-1);
184252 if( rc!=SQLITE_OK0 ) break;
184253
184254 for(iLvl=0; p && iLvl<nMaxDepth; iLvl++){
184255 if( apLeaf[iLvl]==0 ){
184256 apLeaf[iLvl] = p;
184257 p = 0;
184258 }else{
184259 assert( pFree )((void) (0));
184260 pFree->pLeft = apLeaf[iLvl];
184261 pFree->pRight = p;
184262 pFree->pLeft->pParent = pFree;
184263 pFree->pRight->pParent = pFree;
184264
184265 p = pFree;
184266 pFree = pFree->pParent;
184267 p->pParent = 0;
184268 apLeaf[iLvl] = 0;
184269 }
184270 }
184271 if( p ){
184272 sqlite3Fts3ExprFree(p);
184273 rc = SQLITE_TOOBIG18;
184274 break;
184275 }
184276
184277 /* If that was the last leaf node, break out of the loop */
184278 if( pParent==0 ) break;
184279
184280 /* Set $p to point to the next leaf in the tree of eType nodes */
184281 for(p=pParent->pRight; p->eType==eType; p=p->pLeft);
184282
184283 /* Remove pParent from the original tree. */
184284 assert( pParent->pParent==0 || pParent->pParent->pLeft==pParent )((void) (0));
184285 pParent->pRight->pParent = pParent->pParent;
184286 if( pParent->pParent ){
184287 pParent->pParent->pLeft = pParent->pRight;
184288 }else{
184289 assert( pParent==pRoot )((void) (0));
184290 pRoot = pParent->pRight;
184291 }
184292
184293 /* Link pParent into the free node list. It will be used as an
184294 ** internal node of the new tree. */
184295 pParent->pParent = pFree;
184296 pFree = pParent;
184297 }
184298
184299 if( rc==SQLITE_OK0 ){
184300 p = 0;
184301 for(i=0; i<nMaxDepth; i++){
184302 if( apLeaf[i] ){
184303 if( p==0 ){
184304 p = apLeaf[i];
184305 p->pParent = 0;
184306 }else{
184307 assert( pFree!=0 )((void) (0));
184308 pFree->pRight = p;
184309 pFree->pLeft = apLeaf[i];
184310 pFree->pLeft->pParent = pFree;
184311 pFree->pRight->pParent = pFree;
184312
184313 p = pFree;
184314 pFree = pFree->pParent;
184315 p->pParent = 0;
184316 }
184317 }
184318 }
184319 pRoot = p;
184320 }else{
184321 /* An error occurred. Delete the contents of the apLeaf[] array
184322 ** and pFree list. Everything else is cleaned up by the call to
184323 ** sqlite3Fts3ExprFree(pRoot) below. */
184324 Fts3Expr *pDel;
184325 for(i=0; i<nMaxDepth; i++){
184326 sqlite3Fts3ExprFree(apLeaf[i]);
184327 }
184328 while( (pDel=pFree)!=0 ){
184329 pFree = pDel->pParent;
184330 sqlite3_free(pDel);
184331 }
184332 }
184333
184334 assert( pFree==0 )((void) (0));
184335 sqlite3_free( apLeaf );
184336 }
184337 }else if( eType==FTSQUERY_NOT2 ){
184338 Fts3Expr *pLeft = pRoot->pLeft;
184339 Fts3Expr *pRight = pRoot->pRight;
184340
184341 pRoot->pLeft = 0;
184342 pRoot->pRight = 0;
184343 pLeft->pParent = 0;
184344 pRight->pParent = 0;
184345
184346 rc = fts3ExprBalance(&pLeft, nMaxDepth-1);
184347 if( rc==SQLITE_OK0 ){
184348 rc = fts3ExprBalance(&pRight, nMaxDepth-1);
184349 }
184350
184351 if( rc!=SQLITE_OK0 ){
184352 sqlite3Fts3ExprFree(pRight);
184353 sqlite3Fts3ExprFree(pLeft);
184354 }else{
184355 assert( pLeft && pRight )((void) (0));
184356 pRoot->pLeft = pLeft;
184357 pLeft->pParent = pRoot;
184358 pRoot->pRight = pRight;
184359 pRight->pParent = pRoot;
184360 }
184361 }
184362 }
184363
184364 if( rc!=SQLITE_OK0 ){
184365 sqlite3Fts3ExprFree(pRoot);
184366 pRoot = 0;
184367 }
184368 *pp = pRoot;
184369 return rc;
184370}
184371
184372/*
184373** This function is similar to sqlite3Fts3ExprParse(), with the following
184374** differences:
184375**
184376** 1. It does not do expression rebalancing.
184377** 2. It does not check that the expression does not exceed the
184378** maximum allowable depth.
184379** 3. Even if it fails, *ppExpr may still be set to point to an
184380** expression tree. It should be deleted using sqlite3Fts3ExprFree()
184381** in this case.
184382*/
184383static int fts3ExprParseUnbalanced(
184384 sqlite3_tokenizer *pTokenizer, /* Tokenizer module */
184385 int iLangid, /* Language id for tokenizer */
184386 char **azCol, /* Array of column names for fts3 table */
184387 int bFts4, /* True to allow FTS4-only syntax */
184388 int nCol, /* Number of entries in azCol[] */
184389 int iDefaultCol, /* Default column to query */
184390 const char *z, int n, /* Text of MATCH query */
184391 Fts3Expr **ppExpr /* OUT: Parsed query structure */
184392){
184393 int nParsed;
184394 int rc;
184395 ParseContext sParse;
184396
184397 memset(&sParse, 0, sizeof(ParseContext));
184398 sParse.pTokenizer = pTokenizer;
184399 sParse.iLangid = iLangid;
184400 sParse.azCol = (const char **)azCol;
184401 sParse.nCol = nCol;
184402 sParse.iDefaultCol = iDefaultCol;
184403 sParse.bFts4 = bFts4;
184404 if( z==0 ){
184405 *ppExpr = 0;
184406 return SQLITE_OK0;
184407 }
184408 if( n<0 ){
184409 n = (int)strlen(z);
184410 }
184411 rc = fts3ExprParse(&sParse, z, n, ppExpr, &nParsed);
184412 assert( rc==SQLITE_OK || *ppExpr==0 )((void) (0));
184413
184414 /* Check for mismatched parenthesis */
184415 if( rc==SQLITE_OK0 && sParse.nNest ){
184416 rc = SQLITE_ERROR1;
184417 }
184418
184419 return rc;
184420}
184421
184422/*
184423** Parameters z and n contain a pointer to and length of a buffer containing
184424** an fts3 query expression, respectively. This function attempts to parse the
184425** query expression and create a tree of Fts3Expr structures representing the
184426** parsed expression. If successful, *ppExpr is set to point to the head
184427** of the parsed expression tree and SQLITE_OK is returned. If an error
184428** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
184429** error) is returned and *ppExpr is set to 0.
184430**
184431** If parameter n is a negative number, then z is assumed to point to a
184432** nul-terminated string and the length is determined using strlen().
184433**
184434** The first parameter, pTokenizer, is passed the fts3 tokenizer module to
184435** use to normalize query tokens while parsing the expression. The azCol[]
184436** array, which is assumed to contain nCol entries, should contain the names
184437** of each column in the target fts3 table, in order from left to right.
184438** Column names must be nul-terminated strings.
184439**
184440** The iDefaultCol parameter should be passed the index of the table column
184441** that appears on the left-hand-side of the MATCH operator (the default
184442** column to match against for tokens for which a column name is not explicitly
184443** specified as part of the query string), or -1 if tokens may by default
184444** match any table column.
184445*/
184446SQLITE_PRIVATEstatic int sqlite3Fts3ExprParse(
184447 sqlite3_tokenizer *pTokenizer, /* Tokenizer module */
184448 int iLangid, /* Language id for tokenizer */
184449 char **azCol, /* Array of column names for fts3 table */
184450 int bFts4, /* True to allow FTS4-only syntax */
184451 int nCol, /* Number of entries in azCol[] */
184452 int iDefaultCol, /* Default column to query */
184453 const char *z, int n, /* Text of MATCH query */
184454 Fts3Expr **ppExpr, /* OUT: Parsed query structure */
184455 char **pzErr /* OUT: Error message (sqlite3_malloc) */
184456){
184457 int rc = fts3ExprParseUnbalanced(
184458 pTokenizer, iLangid, azCol, bFts4, nCol, iDefaultCol, z, n, ppExpr
184459 );
184460
184461 /* Rebalance the expression. And check that its depth does not exceed
184462 ** SQLITE_FTS3_MAX_EXPR_DEPTH. */
184463 if( rc==SQLITE_OK0 && *ppExpr ){
184464 rc = fts3ExprBalance(ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH12);
184465 if( rc==SQLITE_OK0 ){
184466 rc = fts3ExprCheckDepth(*ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH12);
184467 }
184468 }
184469
184470 if( rc!=SQLITE_OK0 ){
184471 sqlite3Fts3ExprFree(*ppExpr);
184472 *ppExpr = 0;
184473 if( rc==SQLITE_TOOBIG18 ){
184474 sqlite3Fts3ErrMsg(pzErr,
184475 "FTS expression tree is too large (maximum depth %d)",
184476 SQLITE_FTS3_MAX_EXPR_DEPTH12
184477 );
184478 rc = SQLITE_ERROR1;
184479 }else if( rc==SQLITE_ERROR1 ){
184480 sqlite3Fts3ErrMsg(pzErr, "malformed MATCH expression: [%s]", z);
184481 }
184482 }
184483
184484 return rc;
184485}
184486
184487/*
184488** Free a single node of an expression tree.
184489*/
184490static void fts3FreeExprNode(Fts3Expr *p){
184491 assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 )((void) (0));
184492 sqlite3Fts3EvalPhraseCleanup(p->pPhrase);
184493 sqlite3_free(p->aMI);
184494 sqlite3_free(p);
184495}
184496
184497/*
184498** Free a parsed fts3 query expression allocated by sqlite3Fts3ExprParse().
184499**
184500** This function would be simpler if it recursively called itself. But
184501** that would mean passing a sufficiently large expression to ExprParse()
184502** could cause a stack overflow.
184503*/
184504SQLITE_PRIVATEstatic void sqlite3Fts3ExprFree(Fts3Expr *pDel){
184505 Fts3Expr *p;
184506 assert( pDel==0 || pDel->pParent==0 )((void) (0));
184507 for(p=pDel; p && (p->pLeft||p->pRight); p=(p->pLeft ? p->pLeft : p->pRight)){
184508 assert( p->pParent==0 || p==p->pParent->pRight || p==p->pParent->pLeft )((void) (0));
184509 }
184510 while( p ){
184511 Fts3Expr *pParent = p->pParent;
184512 fts3FreeExprNode(p);
184513 if( pParent && p==pParent->pLeft && pParent->pRight ){
184514 p = pParent->pRight;
184515 while( p && (p->pLeft || p->pRight) ){
184516 assert( p==p->pParent->pRight || p==p->pParent->pLeft )((void) (0));
184517 p = (p->pLeft ? p->pLeft : p->pRight);
184518 }
184519 }else{
184520 p = pParent;
184521 }
184522 }
184523}
184524
184525/****************************************************************************
184526*****************************************************************************
184527** Everything after this point is just test code.
184528*/
184529
184530#ifdef SQLITE_TEST
184531
184532/* #include <stdio.h> */
184533
184534/*
184535** Return a pointer to a buffer containing a text representation of the
184536** expression passed as the first argument. The buffer is obtained from
184537** sqlite3_malloc(). It is the responsibility of the caller to use
184538** sqlite3_free() to release the memory. If an OOM condition is encountered,
184539** NULL is returned.
184540**
184541** If the second argument is not NULL, then its contents are prepended to
184542** the returned expression text and then freed using sqlite3_free().
184543*/
184544static char *exprToString(Fts3Expr *pExpr, char *zBuf){
184545 if( pExpr==0 ){
184546 return sqlite3_mprintf("");
184547 }
184548 switch( pExpr->eType ){
184549 case FTSQUERY_PHRASE5: {
184550 Fts3Phrase *pPhrase = pExpr->pPhrase;
184551 int i;
184552 zBuf = sqlite3_mprintf(
184553 "%zPHRASE %d 0", zBuf, pPhrase->iColumn);
184554 for(i=0; zBuf && i<pPhrase->nToken; i++){
184555 zBuf = sqlite3_mprintf("%z %.*s%s", zBuf,
184556 pPhrase->aToken[i].n, pPhrase->aToken[i].z,
184557 (pPhrase->aToken[i].isPrefix?"+":"")
184558 );
184559 }
184560 return zBuf;
184561 }
184562
184563 case FTSQUERY_NEAR1:
184564 zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear);
184565 break;
184566 case FTSQUERY_NOT2:
184567 zBuf = sqlite3_mprintf("%zNOT ", zBuf);
184568 break;
184569 case FTSQUERY_AND3:
184570 zBuf = sqlite3_mprintf("%zAND ", zBuf);
184571 break;
184572 case FTSQUERY_OR4:
184573 zBuf = sqlite3_mprintf("%zOR ", zBuf);
184574 break;
184575 }
184576
184577 if( zBuf ) zBuf = sqlite3_mprintf("%z{", zBuf);
184578 if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf);
184579 if( zBuf ) zBuf = sqlite3_mprintf("%z} {", zBuf);
184580
184581 if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf);
184582 if( zBuf ) zBuf = sqlite3_mprintf("%z}", zBuf);
184583
184584 return zBuf;
184585}
184586
184587/*
184588** This is the implementation of a scalar SQL function used to test the
184589** expression parser. It should be called as follows:
184590**
184591** fts3_exprtest(<tokenizer>, <expr>, <column 1>, ...);
184592**
184593** The first argument, <tokenizer>, is the name of the fts3 tokenizer used
184594** to parse the query expression (see README.tokenizers). The second argument
184595** is the query expression to parse. Each subsequent argument is the name
184596** of a column of the fts3 table that the query expression may refer to.
184597** For example:
184598**
184599** SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2');
184600*/
184601static void fts3ExprTestCommon(
184602 int bRebalance,
184603 sqlite3_context *context,
184604 int argc,
184605 sqlite3_value **argv
184606){
184607 sqlite3_tokenizer *pTokenizer = 0;
184608 int rc;
184609 char **azCol = 0;
184610 const char *zExpr;
184611 int nExpr;
184612 int nCol;
184613 int ii;
184614 Fts3Expr *pExpr;
184615 char *zBuf = 0;
184616 Fts3Hash *pHash = (Fts3Hash*)sqlite3_user_data(context);
184617 const char *zTokenizer = 0;
184618 char *zErr = 0;
184619
184620 if( argc<3 ){
184621 sqlite3_result_error(context,
184622 "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
184623 );
184624 return;
184625 }
184626
184627 zTokenizer = (const char*)sqlite3_value_text(argv[0]);
184628 rc = sqlite3Fts3InitTokenizer(pHash, zTokenizer, &pTokenizer, &zErr);
184629 if( rc!=SQLITE_OK0 ){
184630 if( rc==SQLITE_NOMEM7 ){
184631 sqlite3_result_error_nomem(context);
184632 }else{
184633 sqlite3_result_error(context, zErr, -1);
184634 }
184635 sqlite3_free(zErr);
184636 return;
184637 }
184638
184639 zExpr = (const char *)sqlite3_value_text(argv[1]);
184640 nExpr = sqlite3_value_bytes(argv[1]);
184641 nCol = argc-2;
184642 azCol = (char **)sqlite3_malloc64(nCol*sizeof(char *));
184643 if( !azCol ){
184644 sqlite3_result_error_nomem(context);
184645 goto exprtest_out;
184646 }
184647 for(ii=0; ii<nCol; ii++){
184648 azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
184649 }
184650
184651 if( bRebalance ){
184652 char *zDummy = 0;
184653 rc = sqlite3Fts3ExprParse(
184654 pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr, &zDummy
184655 );
184656 assert( rc==SQLITE_OK || pExpr==0 )((void) (0));
184657 sqlite3_free(zDummy);
184658 }else{
184659 rc = fts3ExprParseUnbalanced(
184660 pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr
184661 );
184662 }
184663
184664 if( rc!=SQLITE_OK0 && rc!=SQLITE_NOMEM7 ){
184665 sqlite3Fts3ExprFree(pExpr);
184666 sqlite3_result_error(context, "Error parsing expression", -1);
184667 }else if( rc==SQLITE_NOMEM7 || !(zBuf = exprToString(pExpr, 0)) ){
184668 sqlite3_result_error_nomem(context);
184669 }else{
184670 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
184671 sqlite3_free(zBuf);
184672 }
184673
184674 sqlite3Fts3ExprFree(pExpr);
184675
184676exprtest_out:
184677 if( pTokenizer ){
184678 rc = pTokenizer->pModule->xDestroy(pTokenizer);
184679 }
184680 sqlite3_free(azCol);
184681}
184682
184683static void fts3ExprTest(
184684 sqlite3_context *context,
184685 int argc,
184686 sqlite3_value **argv
184687){
184688 fts3ExprTestCommon(0, context, argc, argv);
184689}
184690static void fts3ExprTestRebalance(
184691 sqlite3_context *context,
184692 int argc,
184693 sqlite3_value **argv
184694){
184695 fts3ExprTestCommon(1, context, argc, argv);
184696}
184697
184698/*
184699** Register the query expression parser test function fts3_exprtest()
184700** with database connection db.
184701*/
184702SQLITE_PRIVATEstatic int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash *pHash){
184703 int rc = sqlite3_create_function(
184704 db, "fts3_exprtest", -1, SQLITE_UTF81, (void*)pHash, fts3ExprTest, 0, 0
184705 );
184706 if( rc==SQLITE_OK0 ){
184707 rc = sqlite3_create_function(db, "fts3_exprtest_rebalance",
184708 -1, SQLITE_UTF81, (void*)pHash, fts3ExprTestRebalance, 0, 0
184709 );
184710 }
184711 return rc;
184712}
184713
184714#endif
184715#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
184716
184717/************** End of fts3_expr.c *******************************************/
184718/************** Begin file fts3_hash.c ***************************************/
184719/*
184720** 2001 September 22
184721**
184722** The author disclaims copyright to this source code. In place of
184723** a legal notice, here is a blessing:
184724**
184725** May you do good and not evil.
184726** May you find forgiveness for yourself and forgive others.
184727** May you share freely, never taking more than you give.
184728**
184729*************************************************************************
184730** This is the implementation of generic hash-tables used in SQLite.
184731** We've modified it slightly to serve as a standalone hash table
184732** implementation for the full-text indexing module.
184733*/
184734
184735/*
184736** The code in this file is only compiled if:
184737**
184738** * The FTS3 module is being built as an extension
184739** (in which case SQLITE_CORE is not defined), or
184740**
184741** * The FTS3 module is being built into the core of
184742** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
184743*/
184744/* #include "fts3Int.h" */
184745#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
184746
184747/* #include <assert.h> */
184748/* #include <stdlib.h> */
184749/* #include <string.h> */
184750
184751/* #include "fts3_hash.h" */
184752
184753/*
184754** Malloc and Free functions
184755*/
184756static void *fts3HashMalloc(sqlite3_int64 n){
184757 void *p = sqlite3_malloc64(n);
184758 if( p ){
184759 memset(p, 0, n);
184760 }
184761 return p;
184762}
184763static void fts3HashFree(void *p){
184764 sqlite3_free(p);
184765}
184766
184767/* Turn bulk memory into a hash table object by initializing the
184768** fields of the Hash structure.
184769**
184770** "pNew" is a pointer to the hash table that is to be initialized.
184771** keyClass is one of the constants
184772** FTS3_HASH_BINARY or FTS3_HASH_STRING. The value of keyClass
184773** determines what kind of key the hash table will use. "copyKey" is
184774** true if the hash table should make its own private copy of keys and
184775** false if it should just use the supplied pointer.
184776*/
184777SQLITE_PRIVATEstatic void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey){
184778 assert( pNew!=0 )((void) (0));
184779 assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY )((void) (0));
184780 pNew->keyClass = keyClass;
184781 pNew->copyKey = copyKey;
184782 pNew->first = 0;
184783 pNew->count = 0;
184784 pNew->htsize = 0;
184785 pNew->ht = 0;
184786}
184787
184788/* Remove all entries from a hash table. Reclaim all memory.
184789** Call this routine to delete a hash table or to reset a hash table
184790** to the empty state.
184791*/
184792SQLITE_PRIVATEstatic void sqlite3Fts3HashClear(Fts3Hash *pH){
184793 Fts3HashElem *elem; /* For looping over all elements of the table */
184794
184795 assert( pH!=0 )((void) (0));
184796 elem = pH->first;
184797 pH->first = 0;
184798 fts3HashFree(pH->ht);
184799 pH->ht = 0;
184800 pH->htsize = 0;
184801 while( elem ){
184802 Fts3HashElem *next_elem = elem->next;
184803 if( pH->copyKey && elem->pKey ){
184804 fts3HashFree(elem->pKey);
184805 }
184806 fts3HashFree(elem);
184807 elem = next_elem;
184808 }
184809 pH->count = 0;
184810}
184811
184812/*
184813** Hash and comparison functions when the mode is FTS3_HASH_STRING
184814*/
184815static int fts3StrHash(const void *pKey, int nKey){
184816 const char *z = (const char *)pKey;
184817 unsigned h = 0;
184818 if( nKey<=0 ) nKey = (int) strlen(z);
184819 while( nKey > 0 ){
184820 h = (h<<3) ^ h ^ *z++;
184821 nKey--;
184822 }
184823 return (int)(h & 0x7fffffff);
184824}
184825static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
184826 if( n1!=n2 ) return 1;
184827 return strncmp((const char*)pKey1,(const char*)pKey2,n1);
184828}
184829
184830/*
184831** Hash and comparison functions when the mode is FTS3_HASH_BINARY
184832*/
184833static int fts3BinHash(const void *pKey, int nKey){
184834 int h = 0;
184835 const char *z = (const char *)pKey;
184836 while( nKey-- > 0 ){
184837 h = (h<<3) ^ h ^ *(z++);
184838 }
184839 return h & 0x7fffffff;
184840}
184841static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){
184842 if( n1!=n2 ) return 1;
184843 return memcmp(pKey1,pKey2,n1);
184844}
184845
184846/*
184847** Return a pointer to the appropriate hash function given the key class.
184848**
184849** The C syntax in this function definition may be unfamilar to some
184850** programmers, so we provide the following additional explanation:
184851**
184852** The name of the function is "ftsHashFunction". The function takes a
184853** single parameter "keyClass". The return value of ftsHashFunction()
184854** is a pointer to another function. Specifically, the return value
184855** of ftsHashFunction() is a pointer to a function that takes two parameters
184856** with types "const void*" and "int" and returns an "int".
184857*/
184858static int (*ftsHashFunction(int keyClass))(const void*,int){
184859 if( keyClass==FTS3_HASH_STRING1 ){
184860 return &fts3StrHash;
184861 }else{
184862 assert( keyClass==FTS3_HASH_BINARY )((void) (0));
184863 return &fts3BinHash;
184864 }
184865}
184866
184867/*
184868** Return a pointer to the appropriate hash function given the key class.
184869**
184870** For help in interpreted the obscure C code in the function definition,
184871** see the header comment on the previous function.
184872*/
184873static int (*ftsCompareFunction(int keyClass))(const void*,int,const void*,int){
184874 if( keyClass==FTS3_HASH_STRING1 ){
184875 return &fts3StrCompare;
184876 }else{
184877 assert( keyClass==FTS3_HASH_BINARY )((void) (0));
184878 return &fts3BinCompare;
184879 }
184880}
184881
184882/* Link an element into the hash table
184883*/
184884static void fts3HashInsertElement(
184885 Fts3Hash *pH, /* The complete hash table */
184886 struct _fts3ht *pEntry, /* The entry into which pNew is inserted */
184887 Fts3HashElem *pNew /* The element to be inserted */
184888){
184889 Fts3HashElem *pHead; /* First element already in pEntry */
184890 pHead = pEntry->chain;
184891 if( pHead ){
184892 pNew->next = pHead;
184893 pNew->prev = pHead->prev;
184894 if( pHead->prev ){ pHead->prev->next = pNew; }
184895 else { pH->first = pNew; }
184896 pHead->prev = pNew;
184897 }else{
184898 pNew->next = pH->first;
184899 if( pH->first ){ pH->first->prev = pNew; }
184900 pNew->prev = 0;
184901 pH->first = pNew;
184902 }
184903 pEntry->count++;
184904 pEntry->chain = pNew;
184905}
184906
184907
184908/* Resize the hash table so that it cantains "new_size" buckets.
184909** "new_size" must be a power of 2. The hash table might fail
184910** to resize if sqliteMalloc() fails.
184911**
184912** Return non-zero if a memory allocation error occurs.
184913*/
184914static int fts3Rehash(Fts3Hash *pH, int new_size){
184915 struct _fts3ht *new_ht; /* The new hash table */
184916 Fts3HashElem *elem, *next_elem; /* For looping over existing elements */
184917 int (*xHash)(const void*,int); /* The hash function */
184918
184919 assert( (new_size & (new_size-1))==0 )((void) (0));
184920 new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) );
184921 if( new_ht==0 ) return 1;
184922 fts3HashFree(pH->ht);
184923 pH->ht = new_ht;
184924 pH->htsize = new_size;
184925 xHash = ftsHashFunction(pH->keyClass);
184926 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
184927 int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
184928 next_elem = elem->next;
184929 fts3HashInsertElement(pH, &new_ht[h], elem);
184930 }
184931 return 0;
184932}
184933
184934/* This function (for internal use only) locates an element in an
184935** hash table that matches the given key. The hash for this key has
184936** already been computed and is passed as the 4th parameter.
184937*/
184938static Fts3HashElem *fts3FindElementByHash(
184939 const Fts3Hash *pH, /* The pH to be searched */
184940 const void *pKey, /* The key we are searching for */
184941 int nKey,
184942 int h /* The hash for this key. */
184943){
184944 Fts3HashElem *elem; /* Used to loop thru the element list */
184945 int count; /* Number of elements left to test */
184946 int (*xCompare)(const void*,int,const void*,int); /* comparison function */
184947
184948 if( pH->ht ){
184949 struct _fts3ht *pEntry = &pH->ht[h];
184950 elem = pEntry->chain;
184951 count = pEntry->count;
184952 xCompare = ftsCompareFunction(pH->keyClass);
184953 while( count-- && elem ){
184954 if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
184955 return elem;
184956 }
184957 elem = elem->next;
184958 }
184959 }
184960 return 0;
184961}
184962
184963/* Remove a single entry from the hash table given a pointer to that
184964** element and a hash on the element's key.
184965*/
184966static void fts3RemoveElementByHash(
184967 Fts3Hash *pH, /* The pH containing "elem" */
184968 Fts3HashElem* elem, /* The element to be removed from the pH */
184969 int h /* Hash value for the element */
184970){
184971 struct _fts3ht *pEntry;
184972 if( elem->prev ){
184973 elem->prev->next = elem->next;
184974 }else{
184975 pH->first = elem->next;
184976 }
184977 if( elem->next ){
184978 elem->next->prev = elem->prev;
184979 }
184980 pEntry = &pH->ht[h];
184981 if( pEntry->chain==elem ){
184982 pEntry->chain = elem->next;
184983 }
184984 pEntry->count--;
184985 if( pEntry->count<=0 ){
184986 pEntry->chain = 0;
184987 }
184988 if( pH->copyKey && elem->pKey ){
184989 fts3HashFree(elem->pKey);
184990 }
184991 fts3HashFree( elem );
184992 pH->count--;
184993 if( pH->count<=0 ){
184994 assert( pH->first==0 )((void) (0));
184995 assert( pH->count==0 )((void) (0));
184996 fts3HashClearsqlite3Fts3HashClear(pH);
184997 }
184998}
184999
185000SQLITE_PRIVATEstatic Fts3HashElem *sqlite3Fts3HashFindElem(
185001 const Fts3Hash *pH,
185002 const void *pKey,
185003 int nKey
185004){
185005 int h; /* A hash on key */
185006 int (*xHash)(const void*,int); /* The hash function */
185007
185008 if( pH==0 || pH->ht==0 ) return 0;
185009 xHash = ftsHashFunction(pH->keyClass);
185010 assert( xHash!=0 )((void) (0));
185011 h = (*xHash)(pKey,nKey);
185012 assert( (pH->htsize & (pH->htsize-1))==0 )((void) (0));
185013 return fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1));
185014}
185015
185016/*
185017** Attempt to locate an element of the hash table pH with a key
185018** that matches pKey,nKey. Return the data for this element if it is
185019** found, or NULL if there is no match.
185020*/
185021SQLITE_PRIVATEstatic void *sqlite3Fts3HashFind(const Fts3Hash *pH, const void *pKey, int nKey){
185022 Fts3HashElem *pElem; /* The element that matches key (if any) */
185023
185024 pElem = sqlite3Fts3HashFindElem(pH, pKey, nKey);
185025 return pElem ? pElem->data : 0;
185026}
185027
185028/* Insert an element into the hash table pH. The key is pKey,nKey
185029** and the data is "data".
185030**
185031** If no element exists with a matching key, then a new
185032** element is created. A copy of the key is made if the copyKey
185033** flag is set. NULL is returned.
185034**
185035** If another element already exists with the same key, then the
185036** new data replaces the old data and the old data is returned.
185037** The key is not copied in this instance. If a malloc fails, then
185038** the new data is returned and the hash table is unchanged.
185039**
185040** If the "data" parameter to this function is NULL, then the
185041** element corresponding to "key" is removed from the hash table.
185042*/
185043SQLITE_PRIVATEstatic void *sqlite3Fts3HashInsert(
185044 Fts3Hash *pH, /* The hash table to insert into */
185045 const void *pKey, /* The key */
185046 int nKey, /* Number of bytes in the key */
185047 void *data /* The data */
185048){
185049 int hraw; /* Raw hash value of the key */
185050 int h; /* the hash of the key modulo hash table size */
185051 Fts3HashElem *elem; /* Used to loop thru the element list */
185052 Fts3HashElem *new_elem; /* New element added to the pH */
185053 int (*xHash)(const void*,int); /* The hash function */
185054
185055 assert( pH!=0 )((void) (0));
185056 xHash = ftsHashFunction(pH->keyClass);
185057 assert( xHash!=0 )((void) (0));
185058 hraw = (*xHash)(pKey, nKey);
185059 assert( (pH->htsize & (pH->htsize-1))==0 )((void) (0));
185060 h = hraw & (pH->htsize-1);
185061 elem = fts3FindElementByHash(pH,pKey,nKey,h);
185062 if( elem ){
185063 void *old_data = elem->data;
185064 if( data==0 ){
185065 fts3RemoveElementByHash(pH,elem,h);
185066 }else{
185067 elem->data = data;
185068 }
185069 return old_data;
185070 }
185071 if( data==0 ) return 0;
185072 if( (pH->htsize==0 && fts3Rehash(pH,8))
185073 || (pH->count>=pH->htsize && fts3Rehash(pH, pH->htsize*2))
185074 ){
185075 pH->count = 0;
185076 return data;
185077 }
185078 assert( pH->htsize>0 )((void) (0));
185079 new_elem = (Fts3HashElem*)fts3HashMalloc( sizeof(Fts3HashElem) );
185080 if( new_elem==0 ) return data;
185081 if( pH->copyKey && pKey!=0 ){
185082 new_elem->pKey = fts3HashMalloc( nKey );
185083 if( new_elem->pKey==0 ){
185084 fts3HashFree(new_elem);
185085 return data;
185086 }
185087 memcpy((void*)new_elem->pKey, pKey, nKey);
185088 }else{
185089 new_elem->pKey = (void*)pKey;
185090 }
185091 new_elem->nKey = nKey;
185092 pH->count++;
185093 assert( pH->htsize>0 )((void) (0));
185094 assert( (pH->htsize & (pH->htsize-1))==0 )((void) (0));
185095 h = hraw & (pH->htsize-1);
185096 fts3HashInsertElement(pH, &pH->ht[h], new_elem);
185097 new_elem->data = data;
185098 return 0;
185099}
185100
185101#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
185102
185103/************** End of fts3_hash.c *******************************************/
185104/************** Begin file fts3_porter.c *************************************/
185105/*
185106** 2006 September 30
185107**
185108** The author disclaims copyright to this source code. In place of
185109** a legal notice, here is a blessing:
185110**
185111** May you do good and not evil.
185112** May you find forgiveness for yourself and forgive others.
185113** May you share freely, never taking more than you give.
185114**
185115*************************************************************************
185116** Implementation of the full-text-search tokenizer that implements
185117** a Porter stemmer.
185118*/
185119
185120/*
185121** The code in this file is only compiled if:
185122**
185123** * The FTS3 module is being built as an extension
185124** (in which case SQLITE_CORE is not defined), or
185125**
185126** * The FTS3 module is being built into the core of
185127** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
185128*/
185129/* #include "fts3Int.h" */
185130#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
185131
185132/* #include <assert.h> */
185133/* #include <stdlib.h> */
185134/* #include <stdio.h> */
185135/* #include <string.h> */
185136
185137/* #include "fts3_tokenizer.h" */
185138
185139/*
185140** Class derived from sqlite3_tokenizer
185141*/
185142typedef struct porter_tokenizer {
185143 sqlite3_tokenizer base; /* Base class */
185144} porter_tokenizer;
185145
185146/*
185147** Class derived from sqlite3_tokenizer_cursor
185148*/
185149typedef struct porter_tokenizer_cursor {
185150 sqlite3_tokenizer_cursor base;
185151 const char *zInput; /* input we are tokenizing */
185152 int nInput; /* size of the input */
185153 int iOffset; /* current position in zInput */
185154 int iToken; /* index of next token to be returned */
185155 char *zToken; /* storage for current token */
185156 int nAllocated; /* space allocated to zToken buffer */
185157} porter_tokenizer_cursor;
185158
185159
185160/*
185161** Create a new tokenizer instance.
185162*/
185163static int porterCreate(
185164 int argc, const char * const *argv,
185165 sqlite3_tokenizer **ppTokenizer
185166){
185167 porter_tokenizer *t;
185168
185169 UNUSED_PARAMETER(argc)(void)(argc);
185170 UNUSED_PARAMETER(argv)(void)(argv);
185171
185172 t = (porter_tokenizer *) sqlite3_malloc(sizeof(*t));
185173 if( t==NULL((void*)0) ) return SQLITE_NOMEM7;
185174 memset(t, 0, sizeof(*t));
185175 *ppTokenizer = &t->base;
185176 return SQLITE_OK0;
185177}
185178
185179/*
185180** Destroy a tokenizer
185181*/
185182static int porterDestroy(sqlite3_tokenizer *pTokenizer){
185183 sqlite3_free(pTokenizer);
185184 return SQLITE_OK0;
185185}
185186
185187/*
185188** Prepare to begin tokenizing a particular string. The input
185189** string to be tokenized is zInput[0..nInput-1]. A cursor
185190** used to incrementally tokenize this string is returned in
185191** *ppCursor.
185192*/
185193static int porterOpen(
185194 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
185195 const char *zInput, int nInput, /* String to be tokenized */
185196 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
185197){
185198 porter_tokenizer_cursor *c;
185199
185200 UNUSED_PARAMETER(pTokenizer)(void)(pTokenizer);
185201
185202 c = (porter_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
185203 if( c==NULL((void*)0) ) return SQLITE_NOMEM7;
185204
185205 c->zInput = zInput;
185206 if( zInput==0 ){
185207 c->nInput = 0;
185208 }else if( nInput<0 ){
185209 c->nInput = (int)strlen(zInput);
185210 }else{
185211 c->nInput = nInput;
185212 }
185213 c->iOffset = 0; /* start tokenizing at the beginning */
185214 c->iToken = 0;
185215 c->zToken = NULL((void*)0); /* no space allocated, yet. */
185216 c->nAllocated = 0;
185217
185218 *ppCursor = &c->base;
185219 return SQLITE_OK0;
185220}
185221
185222/*
185223** Close a tokenization cursor previously opened by a call to
185224** porterOpen() above.
185225*/
185226static int porterClose(sqlite3_tokenizer_cursor *pCursor){
185227 porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
185228 sqlite3_free(c->zToken);
185229 sqlite3_free(c);
185230 return SQLITE_OK0;
185231}
185232/*
185233** Vowel or consonant
185234*/
185235static const char cType[] = {
185236 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
185237 1, 1, 1, 2, 1
185238};
185239
185240/*
185241** isConsonant() and isVowel() determine if their first character in
185242** the string they point to is a consonant or a vowel, according
185243** to Porter ruls.
185244**
185245** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'.
185246** 'Y' is a consonant unless it follows another consonant,
185247** in which case it is a vowel.
185248**
185249** In these routine, the letters are in reverse order. So the 'y' rule
185250** is that 'y' is a consonant unless it is followed by another
185251** consonent.
185252*/
185253static int isVowel(const char*);
185254static int isConsonant(const char *z){
185255 int j;
185256 char x = *z;
185257 if( x==0 ) return 0;
185258 assert( x>='a' && x<='z' )((void) (0));
185259 j = cType[x-'a'];
185260 if( j<2 ) return j;
185261 return z[1]==0 || isVowel(z + 1);
185262}
185263static int isVowel(const char *z){
185264 int j;
185265 char x = *z;
185266 if( x==0 ) return 0;
185267 assert( x>='a' && x<='z' )((void) (0));
185268 j = cType[x-'a'];
185269 if( j<2 ) return 1-j;
185270 return isConsonant(z + 1);
185271}
185272
185273/*
185274** Let any sequence of one or more vowels be represented by V and let
185275** C be sequence of one or more consonants. Then every word can be
185276** represented as:
185277**
185278** [C] (VC){m} [V]
185279**
185280** In prose: A word is an optional consonant followed by zero or
185281** vowel-consonant pairs followed by an optional vowel. "m" is the
185282** number of vowel consonant pairs. This routine computes the value
185283** of m for the first i bytes of a word.
185284**
185285** Return true if the m-value for z is 1 or more. In other words,
185286** return true if z contains at least one vowel that is followed
185287** by a consonant.
185288**
185289** In this routine z[] is in reverse order. So we are really looking
185290** for an instance of a consonant followed by a vowel.
185291*/
185292static int m_gt_0(const char *z){
185293 while( isVowel(z) ){ z++; }
185294 if( *z==0 ) return 0;
185295 while( isConsonant(z) ){ z++; }
185296 return *z!=0;
185297}
185298
185299/* Like mgt0 above except we are looking for a value of m which is
185300** exactly 1
185301*/
185302static int m_eq_1(const char *z){
185303 while( isVowel(z) ){ z++; }
185304 if( *z==0 ) return 0;
185305 while( isConsonant(z) ){ z++; }
185306 if( *z==0 ) return 0;
185307 while( isVowel(z) ){ z++; }
185308 if( *z==0 ) return 1;
185309 while( isConsonant(z) ){ z++; }
185310 return *z==0;
185311}
185312
185313/* Like mgt0 above except we are looking for a value of m>1 instead
185314** or m>0
185315*/
185316static int m_gt_1(const char *z){
185317 while( isVowel(z) ){ z++; }
185318 if( *z==0 ) return 0;
185319 while( isConsonant(z) ){ z++; }
185320 if( *z==0 ) return 0;
185321 while( isVowel(z) ){ z++; }
185322 if( *z==0 ) return 0;
185323 while( isConsonant(z) ){ z++; }
185324 return *z!=0;
185325}
185326
185327/*
185328** Return TRUE if there is a vowel anywhere within z[0..n-1]
185329*/
185330static int hasVowel(const char *z){
185331 while( isConsonant(z) ){ z++; }
185332 return *z!=0;
185333}
185334
185335/*
185336** Return TRUE if the word ends in a double consonant.
185337**
185338** The text is reversed here. So we are really looking at
185339** the first two characters of z[].
185340*/
185341static int doubleConsonant(const char *z){
185342 return isConsonant(z) && z[0]==z[1];
185343}
185344
185345/*
185346** Return TRUE if the word ends with three letters which
185347** are consonant-vowel-consonent and where the final consonant
185348** is not 'w', 'x', or 'y'.
185349**
185350** The word is reversed here. So we are really checking the
185351** first three letters and the first one cannot be in [wxy].
185352*/
185353static int star_oh(const char *z){
185354 return
185355 isConsonant(z) &&
185356 z[0]!='w' && z[0]!='x' && z[0]!='y' &&
185357 isVowel(z+1) &&
185358 isConsonant(z+2);
185359}
185360
185361/*
185362** If the word ends with zFrom and xCond() is true for the stem
185363** of the word that preceeds the zFrom ending, then change the
185364** ending to zTo.
185365**
185366** The input word *pz and zFrom are both in reverse order. zTo
185367** is in normal order.
185368**
185369** Return TRUE if zFrom matches. Return FALSE if zFrom does not
185370** match. Not that TRUE is returned even if xCond() fails and
185371** no substitution occurs.
185372*/
185373static int stem(
185374 char **pz, /* The word being stemmed (Reversed) */
185375 const char *zFrom, /* If the ending matches this... (Reversed) */
185376 const char *zTo, /* ... change the ending to this (not reversed) */
185377 int (*xCond)(const char*) /* Condition that must be true */
185378){
185379 char *z = *pz;
185380 while( *zFrom && *zFrom==*z ){ z++; zFrom++; }
185381 if( *zFrom!=0 ) return 0;
185382 if( xCond && !xCond(z) ) return 1;
185383 while( *zTo ){
185384 *(--z) = *(zTo++);
185385 }
185386 *pz = z;
185387 return 1;
185388}
185389
185390/*
185391** This is the fallback stemmer used when the porter stemmer is
185392** inappropriate. The input word is copied into the output with
185393** US-ASCII case folding. If the input word is too long (more
185394** than 20 bytes if it contains no digits or more than 6 bytes if
185395** it contains digits) then word is truncated to 20 or 6 bytes
185396** by taking 10 or 3 bytes from the beginning and end.
185397*/
185398static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
185399 int i, mx, j;
185400 int hasDigit = 0;
185401 for(i=0; i<nIn; i++){
185402 char c = zIn[i];
185403 if( c>='A' && c<='Z' ){
185404 zOut[i] = c - 'A' + 'a';
185405 }else{
185406 if( c>='0' && c<='9' ) hasDigit = 1;
185407 zOut[i] = c;
185408 }
185409 }
185410 mx = hasDigit ? 3 : 10;
185411 if( nIn>mx*2 ){
185412 for(j=mx, i=nIn-mx; i<nIn; i++, j++){
185413 zOut[j] = zOut[i];
185414 }
185415 i = j;
185416 }
185417 zOut[i] = 0;
185418 *pnOut = i;
185419}
185420
185421
185422/*
185423** Stem the input word zIn[0..nIn-1]. Store the output in zOut.
185424** zOut is at least big enough to hold nIn bytes. Write the actual
185425** size of the output word (exclusive of the '\0' terminator) into *pnOut.
185426**
185427** Any upper-case characters in the US-ASCII character set ([A-Z])
185428** are converted to lower case. Upper-case UTF characters are
185429** unchanged.
185430**
185431** Words that are longer than about 20 bytes are stemmed by retaining
185432** a few bytes from the beginning and the end of the word. If the
185433** word contains digits, 3 bytes are taken from the beginning and
185434** 3 bytes from the end. For long words without digits, 10 bytes
185435** are taken from each end. US-ASCII case folding still applies.
185436**
185437** If the input word contains not digits but does characters not
185438** in [a-zA-Z] then no stemming is attempted and this routine just
185439** copies the input into the input into the output with US-ASCII
185440** case folding.
185441**
185442** Stemming never increases the length of the word. So there is
185443** no chance of overflowing the zOut buffer.
185444*/
185445static void porter_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
185446 int i, j;
185447 char zReverse[28];
185448 char *z, *z2;
185449 if( nIn<3 || nIn>=(int)sizeof(zReverse)-7 ){
185450 /* The word is too big or too small for the porter stemmer.
185451 ** Fallback to the copy stemmer */
185452 copy_stemmer(zIn, nIn, zOut, pnOut);
185453 return;
185454 }
185455 for(i=0, j=sizeof(zReverse)-6; i<nIn; i++, j--){
185456 char c = zIn[i];
185457 if( c>='A' && c<='Z' ){
185458 zReverse[j] = c + 'a' - 'A';
185459 }else if( c>='a' && c<='z' ){
185460 zReverse[j] = c;
185461 }else{
185462 /* The use of a character not in [a-zA-Z] means that we fallback
185463 ** to the copy stemmer */
185464 copy_stemmer(zIn, nIn, zOut, pnOut);
185465 return;
185466 }
185467 }
185468 memset(&zReverse[sizeof(zReverse)-5], 0, 5);
185469 z = &zReverse[j+1];
185470
185471
185472 /* Step 1a */
185473 if( z[0]=='s' ){
185474 if(
185475 !stem(&z, "sess", "ss", 0) &&
185476 !stem(&z, "sei", "i", 0) &&
185477 !stem(&z, "ss", "ss", 0)
185478 ){
185479 z++;
185480 }
185481 }
185482
185483 /* Step 1b */
185484 z2 = z;
185485 if( stem(&z, "dee", "ee", m_gt_0) ){
185486 /* Do nothing. The work was all in the test */
185487 }else if(
185488 (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel))
185489 && z!=z2
185490 ){
185491 if( stem(&z, "ta", "ate", 0) ||
185492 stem(&z, "lb", "ble", 0) ||
185493 stem(&z, "zi", "ize", 0) ){
185494 /* Do nothing. The work was all in the test */
185495 }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){
185496 z++;
185497 }else if( m_eq_1(z) && star_oh(z) ){
185498 *(--z) = 'e';
185499 }
185500 }
185501
185502 /* Step 1c */
185503 if( z[0]=='y' && hasVowel(z+1) ){
185504 z[0] = 'i';
185505 }
185506
185507 /* Step 2 */
185508 switch( z[1] ){
185509 case 'a':
185510 if( !stem(&z, "lanoita", "ate", m_gt_0) ){
185511 stem(&z, "lanoit", "tion", m_gt_0);
185512 }
185513 break;
185514 case 'c':
185515 if( !stem(&z, "icne", "ence", m_gt_0) ){
185516 stem(&z, "icna", "ance", m_gt_0);
185517 }
185518 break;
185519 case 'e':
185520 stem(&z, "rezi", "ize", m_gt_0);
185521 break;
185522 case 'g':
185523 stem(&z, "igol", "log", m_gt_0);
185524 break;
185525 case 'l':
185526 if( !stem(&z, "ilb", "ble", m_gt_0)
185527 && !stem(&z, "illa", "al", m_gt_0)
185528 && !stem(&z, "iltne", "ent", m_gt_0)
185529 && !stem(&z, "ile", "e", m_gt_0)
185530 ){
185531 stem(&z, "ilsuo", "ous", m_gt_0);
185532 }
185533 break;
185534 case 'o':
185535 if( !stem(&z, "noitazi", "ize", m_gt_0)
185536 && !stem(&z, "noita", "ate", m_gt_0)
185537 ){
185538 stem(&z, "rota", "ate", m_gt_0);
185539 }
185540 break;
185541 case 's':
185542 if( !stem(&z, "msila", "al", m_gt_0)
185543 && !stem(&z, "ssenevi", "ive", m_gt_0)
185544 && !stem(&z, "ssenluf", "ful", m_gt_0)
185545 ){
185546 stem(&z, "ssensuo", "ous", m_gt_0);
185547 }
185548 break;
185549 case 't':
185550 if( !stem(&z, "itila", "al", m_gt_0)
185551 && !stem(&z, "itivi", "ive", m_gt_0)
185552 ){
185553 stem(&z, "itilib", "ble", m_gt_0);
185554 }
185555 break;
185556 }
185557
185558 /* Step 3 */
185559 switch( z[0] ){
185560 case 'e':
185561 if( !stem(&z, "etaci", "ic", m_gt_0)
185562 && !stem(&z, "evita", "", m_gt_0)
185563 ){
185564 stem(&z, "ezila", "al", m_gt_0);
185565 }
185566 break;
185567 case 'i':
185568 stem(&z, "itici", "ic", m_gt_0);
185569 break;
185570 case 'l':
185571 if( !stem(&z, "laci", "ic", m_gt_0) ){
185572 stem(&z, "luf", "", m_gt_0);
185573 }
185574 break;
185575 case 's':
185576 stem(&z, "ssen", "", m_gt_0);
185577 break;
185578 }
185579
185580 /* Step 4 */
185581 switch( z[1] ){
185582 case 'a':
185583 if( z[0]=='l' && m_gt_1(z+2) ){
185584 z += 2;
185585 }
185586 break;
185587 case 'c':
185588 if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e') && m_gt_1(z+4) ){
185589 z += 4;
185590 }
185591 break;
185592 case 'e':
185593 if( z[0]=='r' && m_gt_1(z+2) ){
185594 z += 2;
185595 }
185596 break;
185597 case 'i':
185598 if( z[0]=='c' && m_gt_1(z+2) ){
185599 z += 2;
185600 }
185601 break;
185602 case 'l':
185603 if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){
185604 z += 4;
185605 }
185606 break;
185607 case 'n':
185608 if( z[0]=='t' ){
185609 if( z[2]=='a' ){
185610 if( m_gt_1(z+3) ){
185611 z += 3;
185612 }
185613 }else if( z[2]=='e' ){
185614 if( !stem(&z, "tneme", "", m_gt_1)
185615 && !stem(&z, "tnem", "", m_gt_1)
185616 ){
185617 stem(&z, "tne", "", m_gt_1);
185618 }
185619 }
185620 }
185621 break;
185622 case 'o':
185623 if( z[0]=='u' ){
185624 if( m_gt_1(z+2) ){
185625 z += 2;
185626 }
185627 }else if( z[3]=='s' || z[3]=='t' ){
185628 stem(&z, "noi", "", m_gt_1);
185629 }
185630 break;
185631 case 's':
185632 if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){
185633 z += 3;
185634 }
185635 break;
185636 case 't':
185637 if( !stem(&z, "eta", "", m_gt_1) ){
185638 stem(&z, "iti", "", m_gt_1);
185639 }
185640 break;
185641 case 'u':
185642 if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){
185643 z += 3;
185644 }
185645 break;
185646 case 'v':
185647 case 'z':
185648 if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){
185649 z += 3;
185650 }
185651 break;
185652 }
185653
185654 /* Step 5a */
185655 if( z[0]=='e' ){
185656 if( m_gt_1(z+1) ){
185657 z++;
185658 }else if( m_eq_1(z+1) && !star_oh(z+1) ){
185659 z++;
185660 }
185661 }
185662
185663 /* Step 5b */
185664 if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){
185665 z++;
185666 }
185667
185668 /* z[] is now the stemmed word in reverse order. Flip it back
185669 ** around into forward order and return.
185670 */
185671 *pnOut = i = (int)strlen(z);
185672 zOut[i] = 0;
185673 while( *z ){
185674 zOut[--i] = *(z++);
185675 }
185676}
185677
185678/*
185679** Characters that can be part of a token. We assume any character
185680** whose value is greater than 0x80 (any UTF character) can be
185681** part of a token. In other words, delimiters all must have
185682** values of 0x7f or lower.
185683*/
185684static const char porterIdChar[] = {
185685/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
185686 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
185687 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
185688 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
185689 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
185690 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
185691};
185692#define isDelim(C)(((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar
[ch-0x30]))
(((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30]))
185693
185694/*
185695** Extract the next token from a tokenization cursor. The cursor must
185696** have been opened by a prior call to porterOpen().
185697*/
185698static int porterNext(
185699 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by porterOpen */
185700 const char **pzToken, /* OUT: *pzToken is the token text */
185701 int *pnBytes, /* OUT: Number of bytes in token */
185702 int *piStartOffset, /* OUT: Starting offset of token */
185703 int *piEndOffset, /* OUT: Ending offset of token */
185704 int *piPosition /* OUT: Position integer of token */
185705){
185706 porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
185707 const char *z = c->zInput;
185708
185709 while( c->iOffset<c->nInput ){
185710 int iStartOffset, ch;
185711
185712 /* Scan past delimiter characters */
185713 while( c->iOffset<c->nInput && isDelim(z[c->iOffset])(((ch=z[c->iOffset])&0x80)==0 && (ch<0x30 ||
!porterIdChar[ch-0x30]))
){
185714 c->iOffset++;
185715 }
185716
185717 /* Count non-delimiter characters. */
185718 iStartOffset = c->iOffset;
185719 while( c->iOffset<c->nInput && !isDelim(z[c->iOffset])(((ch=z[c->iOffset])&0x80)==0 && (ch<0x30 ||
!porterIdChar[ch-0x30]))
){
185720 c->iOffset++;
185721 }
185722
185723 if( c->iOffset>iStartOffset ){
185724 int n = c->iOffset-iStartOffset;
185725 if( n>c->nAllocated ){
185726 char *pNew;
185727 c->nAllocated = n+20;
185728 pNew = sqlite3_realloc(c->zToken, c->nAllocated);
185729 if( !pNew ) return SQLITE_NOMEM7;
185730 c->zToken = pNew;
185731 }
185732 porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
185733 *pzToken = c->zToken;
185734 *piStartOffset = iStartOffset;
185735 *piEndOffset = c->iOffset;
185736 *piPosition = c->iToken++;
185737 return SQLITE_OK0;
185738 }
185739 }
185740 return SQLITE_DONE101;
185741}
185742
185743/*
185744** The set of routines that implement the porter-stemmer tokenizer
185745*/
185746static const sqlite3_tokenizer_module porterTokenizerModule = {
185747 0,
185748 porterCreate,
185749 porterDestroy,
185750 porterOpen,
185751 porterClose,
185752 porterNext,
185753 0
185754};
185755
185756/*
185757** Allocate a new porter tokenizer. Return a pointer to the new
185758** tokenizer in *ppModule
185759*/
185760SQLITE_PRIVATEstatic void sqlite3Fts3PorterTokenizerModule(
185761 sqlite3_tokenizer_module const**ppModule
185762){
185763 *ppModule = &porterTokenizerModule;
185764}
185765
185766#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
185767
185768/************** End of fts3_porter.c *****************************************/
185769/************** Begin file fts3_tokenizer.c **********************************/
185770/*
185771** 2007 June 22
185772**
185773** The author disclaims copyright to this source code. In place of
185774** a legal notice, here is a blessing:
185775**
185776** May you do good and not evil.
185777** May you find forgiveness for yourself and forgive others.
185778** May you share freely, never taking more than you give.
185779**
185780******************************************************************************
185781**
185782** This is part of an SQLite module implementing full-text search.
185783** This particular file implements the generic tokenizer interface.
185784*/
185785
185786/*
185787** The code in this file is only compiled if:
185788**
185789** * The FTS3 module is being built as an extension
185790** (in which case SQLITE_CORE is not defined), or
185791**
185792** * The FTS3 module is being built into the core of
185793** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
185794*/
185795/* #include "fts3Int.h" */
185796#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
185797
185798/* #include <assert.h> */
185799/* #include <string.h> */
185800
185801/*
185802** Return true if the two-argument version of fts3_tokenizer()
185803** has been activated via a prior call to sqlite3_db_config(db,
185804** SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
185805*/
185806static int fts3TokenizerEnabled(sqlite3_context *context){
185807 sqlite3 *db = sqlite3_context_db_handle(context);
185808 int isEnabled = 0;
185809 sqlite3_db_config(db,SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER1004,-1,&isEnabled);
185810 return isEnabled;
185811}
185812
185813/*
185814** Implementation of the SQL scalar function for accessing the underlying
185815** hash table. This function may be called as follows:
185816**
185817** SELECT <function-name>(<key-name>);
185818** SELECT <function-name>(<key-name>, <pointer>);
185819**
185820** where <function-name> is the name passed as the second argument
185821** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer').
185822**
185823** If the <pointer> argument is specified, it must be a blob value
185824** containing a pointer to be stored as the hash data corresponding
185825** to the string <key-name>. If <pointer> is not specified, then
185826** the string <key-name> must already exist in the has table. Otherwise,
185827** an error is returned.
185828**
185829** Whether or not the <pointer> argument is specified, the value returned
185830** is a blob containing the pointer stored as the hash data corresponding
185831** to string <key-name> (after the hash-table is updated, if applicable).
185832*/
185833static void fts3TokenizerFunc(
185834 sqlite3_context *context,
185835 int argc,
185836 sqlite3_value **argv
185837){
185838 Fts3Hash *pHash;
185839 void *pPtr = 0;
185840 const unsigned char *zName;
185841 int nName;
185842
185843 assert( argc==1 || argc==2 )((void) (0));
185844
185845 pHash = (Fts3Hash *)sqlite3_user_data(context);
185846
185847 zName = sqlite3_value_text(argv[0]);
185848 nName = sqlite3_value_bytes(argv[0])+1;
185849
185850 if( argc==2 ){
185851 if( fts3TokenizerEnabled(context) || sqlite3_value_frombind(argv[1]) ){
185852 void *pOld;
185853 int n = sqlite3_value_bytes(argv[1]);
185854 if( zName==0 || n!=sizeof(pPtr) ){
185855 sqlite3_result_error(context, "argument type mismatch", -1);
185856 return;
185857 }
185858 pPtr = *(void **)sqlite3_value_blob(argv[1]);
185859 pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
185860 if( pOld==pPtr ){
185861 sqlite3_result_error(context, "out of memory", -1);
185862 }
185863 }else{
185864 sqlite3_result_error(context, "fts3tokenize disabled", -1);
185865 return;
185866 }
185867 }else{
185868 if( zName ){
185869 pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
185870 }
185871 if( !pPtr ){
185872 char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
185873 sqlite3_result_error(context, zErr, -1);
185874 sqlite3_free(zErr);
185875 return;
185876 }
185877 }
185878 if( fts3TokenizerEnabled(context) || sqlite3_value_frombind(argv[0]) ){
185879 sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
185880 }
185881}
185882
185883SQLITE_PRIVATEstatic int sqlite3Fts3IsIdChar(char c){
185884 static const char isFtsIdChar[] = {
185885 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
185886 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
185887 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
185888 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
185889 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
185890 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
185891 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
185892 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
185893 };
185894 return (c&0x80 || isFtsIdChar[(int)(c)]);
185895}
185896
185897SQLITE_PRIVATEstatic const char *sqlite3Fts3NextToken(const char *zStr, int *pn){
185898 const char *z1;
185899 const char *z2 = 0;
185900
185901 /* Find the start of the next token. */
185902 z1 = zStr;
185903 while( z2==0 ){
185904 char c = *z1;
185905 switch( c ){
185906 case '\0': return 0; /* No more tokens here */
185907 case '\'':
185908 case '"':
185909 case '`': {
185910 z2 = z1;
185911 while( *++z2 && (*z2!=c || *++z2==c) );
185912 break;
185913 }
185914 case '[':
185915 z2 = &z1[1];
185916 while( *z2 && z2[0]!=']' ) z2++;
185917 if( *z2 ) z2++;
185918 break;
185919
185920 default:
185921 if( sqlite3Fts3IsIdChar(*z1) ){
185922 z2 = &z1[1];
185923 while( sqlite3Fts3IsIdChar(*z2) ) z2++;
185924 }else{
185925 z1++;
185926 }
185927 }
185928 }
185929
185930 *pn = (int)(z2-z1);
185931 return z1;
185932}
185933
185934SQLITE_PRIVATEstatic int sqlite3Fts3InitTokenizer(
185935 Fts3Hash *pHash, /* Tokenizer hash table */
185936 const char *zArg, /* Tokenizer name */
185937 sqlite3_tokenizer **ppTok, /* OUT: Tokenizer (if applicable) */
185938 char **pzErr /* OUT: Set to malloced error message */
185939){
185940 int rc;
185941 char *z = (char *)zArg;
185942 int n = 0;
185943 char *zCopy;
185944 char *zEnd; /* Pointer to nul-term of zCopy */
185945 sqlite3_tokenizer_module *m;
185946
185947 zCopy = sqlite3_mprintf("%s", zArg);
185948 if( !zCopy ) return SQLITE_NOMEM7;
185949 zEnd = &zCopy[strlen(zCopy)];
185950
185951 z = (char *)sqlite3Fts3NextToken(zCopy, &n);
185952 if( z==0 ){
185953 assert( n==0 )((void) (0));
185954 z = zCopy;
185955 }
185956 z[n] = '\0';
185957 sqlite3Fts3Dequote(z);
185958
185959 m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1);
185960 if( !m ){
185961 sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", z);
185962 rc = SQLITE_ERROR1;
185963 }else{
185964 char const **aArg = 0;
185965 int iArg = 0;
185966 z = &z[n+1];
185967 while( z<zEnd && (NULL((void*)0)!=(z = (char *)sqlite3Fts3NextToken(z, &n))) ){
185968 sqlite3_int64 nNew = sizeof(char *)*(iArg+1);
185969 char const **aNew = (const char **)sqlite3_realloc64((void *)aArg, nNew);
185970 if( !aNew ){
185971 sqlite3_free(zCopy);
185972 sqlite3_free((void *)aArg);
185973 return SQLITE_NOMEM7;
185974 }
185975 aArg = aNew;
185976 aArg[iArg++] = z;
185977 z[n] = '\0';
185978 sqlite3Fts3Dequote(z);
185979 z = &z[n+1];
185980 }
185981 rc = m->xCreate(iArg, aArg, ppTok);
185982 assert( rc!=SQLITE_OK || *ppTok )((void) (0));
185983 if( rc!=SQLITE_OK0 ){
185984 sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer");
185985 }else{
185986 (*ppTok)->pModule = m;
185987 }
185988 sqlite3_free((void *)aArg);
185989 }
185990
185991 sqlite3_free(zCopy);
185992 return rc;
185993}
185994
185995
185996#ifdef SQLITE_TEST
185997
185998#if defined(INCLUDE_SQLITE_TCL_H)
185999# include "sqlite_tcl.h"
186000#else
186001# include "tcl.h"
186002#endif
186003/* #include <string.h> */
186004
186005/*
186006** Implementation of a special SQL scalar function for testing tokenizers
186007** designed to be used in concert with the Tcl testing framework. This
186008** function must be called with two or more arguments:
186009**
186010** SELECT <function-name>(<key-name>, ..., <input-string>);
186011**
186012** where <function-name> is the name passed as the second argument
186013** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
186014** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
186015**
186016** The return value is a string that may be interpreted as a Tcl
186017** list. For each token in the <input-string>, three elements are
186018** added to the returned list. The first is the token position, the
186019** second is the token text (folded, stemmed, etc.) and the third is the
186020** substring of <input-string> associated with the token. For example,
186021** using the built-in "simple" tokenizer:
186022**
186023** SELECT fts_tokenizer_test('simple', 'I don't see how');
186024**
186025** will return the string:
186026**
186027** "{0 i I 1 dont don't 2 see see 3 how how}"
186028**
186029*/
186030static void testFunc(
186031 sqlite3_context *context,
186032 int argc,
186033 sqlite3_value **argv
186034){
186035 Fts3Hash *pHash;
186036 sqlite3_tokenizer_module *p;
186037 sqlite3_tokenizer *pTokenizer = 0;
186038 sqlite3_tokenizer_cursor *pCsr = 0;
186039
186040 const char *zErr = 0;
186041
186042 const char *zName;
186043 int nName;
186044 const char *zInput;
186045 int nInput;
186046
186047 const char *azArg[64];
186048
186049 const char *zToken;
186050 int nToken = 0;
186051 int iStart = 0;
186052 int iEnd = 0;
186053 int iPos = 0;
186054 int i;
186055
186056 Tcl_Obj *pRet;
186057
186058 if( argc<2 ){
186059 sqlite3_result_error(context, "insufficient arguments", -1);
186060 return;
186061 }
186062
186063 nName = sqlite3_value_bytes(argv[0]);
186064 zName = (const char *)sqlite3_value_text(argv[0]);
186065 nInput = sqlite3_value_bytes(argv[argc-1]);
186066 zInput = (const char *)sqlite3_value_text(argv[argc-1]);
186067
186068 pHash = (Fts3Hash *)sqlite3_user_data(context);
186069 p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
186070
186071 if( !p ){
186072 char *zErr2 = sqlite3_mprintf("unknown tokenizer: %s", zName);
186073 sqlite3_result_error(context, zErr2, -1);
186074 sqlite3_free(zErr2);
186075 return;
186076 }
186077
186078 pRet = Tcl_NewObj();
186079 Tcl_IncrRefCount(pRet);
186080
186081 for(i=1; i<argc-1; i++){
186082 azArg[i-1] = (const char *)sqlite3_value_text(argv[i]);
186083 }
186084
186085 if( SQLITE_OK0!=p->xCreate(argc-2, azArg, &pTokenizer) ){
186086 zErr = "error in xCreate()";
186087 goto finish;
186088 }
186089 pTokenizer->pModule = p;
186090 if( sqlite3Fts3OpenTokenizer(pTokenizer, 0, zInput, nInput, &pCsr) ){
186091 zErr = "error in xOpen()";
186092 goto finish;
186093 }
186094
186095 while( SQLITE_OK0==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
186096 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos));
186097 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
186098 zToken = &zInput[iStart];
186099 nToken = iEnd-iStart;
186100 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
186101 }
186102
186103 if( SQLITE_OK0!=p->xClose(pCsr) ){
186104 zErr = "error in xClose()";
186105 goto finish;
186106 }
186107 if( SQLITE_OK0!=p->xDestroy(pTokenizer) ){
186108 zErr = "error in xDestroy()";
186109 goto finish;
186110 }
186111
186112finish:
186113 if( zErr ){
186114 sqlite3_result_error(context, zErr, -1);
186115 }else{
186116 sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
186117 }
186118 Tcl_DecrRefCount(pRet);
186119}
186120
186121static
186122int registerTokenizer(
186123 sqlite3 *db,
186124 char *zName,
186125 const sqlite3_tokenizer_module *p
186126){
186127 int rc;
186128 sqlite3_stmt *pStmt;
186129 const char zSql[] = "SELECT fts3_tokenizer(?, ?)";
186130
186131 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
186132 if( rc!=SQLITE_OK0 ){
186133 return rc;
186134 }
186135
186136 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
186137 sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC((sqlite3_destructor_type)0));
186138 sqlite3_step(pStmt);
186139
186140 return sqlite3_finalize(pStmt);
186141}
186142
186143
186144static
186145int queryTokenizer(
186146 sqlite3 *db,
186147 char *zName,
186148 const sqlite3_tokenizer_module **pp
186149){
186150 int rc;
186151 sqlite3_stmt *pStmt;
186152 const char zSql[] = "SELECT fts3_tokenizer(?)";
186153
186154 *pp = 0;
186155 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
186156 if( rc!=SQLITE_OK0 ){
186157 return rc;
186158 }
186159
186160 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
186161 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
186162 if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB4
186163 && sqlite3_column_bytes(pStmt, 0)==sizeof(*pp)
186164 ){
186165 memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
186166 }
186167 }
186168
186169 return sqlite3_finalize(pStmt);
186170}
186171
186172SQLITE_PRIVATEstatic void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
186173
186174/*
186175** Implementation of the scalar function fts3_tokenizer_internal_test().
186176** This function is used for testing only, it is not included in the
186177** build unless SQLITE_TEST is defined.
186178**
186179** The purpose of this is to test that the fts3_tokenizer() function
186180** can be used as designed by the C-code in the queryTokenizer and
186181** registerTokenizer() functions above. These two functions are repeated
186182** in the README.tokenizer file as an example, so it is important to
186183** test them.
186184**
186185** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar
186186** function with no arguments. An assert() will fail if a problem is
186187** detected. i.e.:
186188**
186189** SELECT fts3_tokenizer_internal_test();
186190**
186191*/
186192static void intTestFunc(
186193 sqlite3_context *context,
186194 int argc,
186195 sqlite3_value **argv
186196){
186197 int rc;
186198 const sqlite3_tokenizer_module *p1;
186199 const sqlite3_tokenizer_module *p2;
186200 sqlite3 *db = (sqlite3 *)sqlite3_user_data(context);
186201
186202 UNUSED_PARAMETER(argc)(void)(argc);
186203 UNUSED_PARAMETER(argv)(void)(argv);
186204
186205 /* Test the query function */
186206 sqlite3Fts3SimpleTokenizerModule(&p1);
186207 rc = queryTokenizer(db, "simple", &p2);
186208 assert( rc==SQLITE_OK )((void) (0));
186209 assert( p1==p2 )((void) (0));
186210 rc = queryTokenizer(db, "nosuchtokenizer", &p2);
186211 assert( rc==SQLITE_ERROR )((void) (0));
186212 assert( p2==0 )((void) (0));
186213 assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") )((void) (0));
186214
186215 /* Test the storage function */
186216 if( fts3TokenizerEnabled(context) ){
186217 rc = registerTokenizer(db, "nosuchtokenizer", p1);
186218 assert( rc==SQLITE_OK )((void) (0));
186219 rc = queryTokenizer(db, "nosuchtokenizer", &p2);
186220 assert( rc==SQLITE_OK )((void) (0));
186221 assert( p2==p1 )((void) (0));
186222 }
186223
186224 sqlite3_result_text(context, "ok", -1, SQLITE_STATIC((sqlite3_destructor_type)0));
186225}
186226
186227#endif
186228
186229/*
186230** Set up SQL objects in database db used to access the contents of
186231** the hash table pointed to by argument pHash. The hash table must
186232** been initialized to use string keys, and to take a private copy
186233** of the key when a value is inserted. i.e. by a call similar to:
186234**
186235** sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
186236**
186237** This function adds a scalar function (see header comment above
186238** fts3TokenizerFunc() in this file for details) and, if ENABLE_TABLE is
186239** defined at compilation time, a temporary virtual table (see header
186240** comment above struct HashTableVtab) to the database schema. Both
186241** provide read/write access to the contents of *pHash.
186242**
186243** The third argument to this function, zName, is used as the name
186244** of both the scalar and, if created, the virtual table.
186245*/
186246SQLITE_PRIVATEstatic int sqlite3Fts3InitHashTable(
186247 sqlite3 *db,
186248 Fts3Hash *pHash,
186249 const char *zName
186250){
186251 int rc = SQLITE_OK0;
186252 void *p = (void *)pHash;
186253 const int any = SQLITE_UTF81|SQLITE_DIRECTONLY0x000080000;
186254
186255#ifdef SQLITE_TEST
186256 char *zTest = 0;
186257 char *zTest2 = 0;
186258 void *pdb = (void *)db;
186259 zTest = sqlite3_mprintf("%s_test", zName);
186260 zTest2 = sqlite3_mprintf("%s_internal_test", zName);
186261 if( !zTest || !zTest2 ){
186262 rc = SQLITE_NOMEM7;
186263 }
186264#endif
186265
186266 if( SQLITE_OK0==rc ){
186267 rc = sqlite3_create_function(db, zName, 1, any, p, fts3TokenizerFunc, 0, 0);
186268 }
186269 if( SQLITE_OK0==rc ){
186270 rc = sqlite3_create_function(db, zName, 2, any, p, fts3TokenizerFunc, 0, 0);
186271 }
186272#ifdef SQLITE_TEST
186273 if( SQLITE_OK0==rc ){
186274 rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0);
186275 }
186276 if( SQLITE_OK0==rc ){
186277 rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
186278 }
186279#endif
186280
186281#ifdef SQLITE_TEST
186282 sqlite3_free(zTest);
186283 sqlite3_free(zTest2);
186284#endif
186285
186286 return rc;
186287}
186288
186289#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
186290
186291/************** End of fts3_tokenizer.c **************************************/
186292/************** Begin file fts3_tokenizer1.c *********************************/
186293/*
186294** 2006 Oct 10
186295**
186296** The author disclaims copyright to this source code. In place of
186297** a legal notice, here is a blessing:
186298**
186299** May you do good and not evil.
186300** May you find forgiveness for yourself and forgive others.
186301** May you share freely, never taking more than you give.
186302**
186303******************************************************************************
186304**
186305** Implementation of the "simple" full-text-search tokenizer.
186306*/
186307
186308/*
186309** The code in this file is only compiled if:
186310**
186311** * The FTS3 module is being built as an extension
186312** (in which case SQLITE_CORE is not defined), or
186313**
186314** * The FTS3 module is being built into the core of
186315** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
186316*/
186317/* #include "fts3Int.h" */
186318#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
186319
186320/* #include <assert.h> */
186321/* #include <stdlib.h> */
186322/* #include <stdio.h> */
186323/* #include <string.h> */
186324
186325/* #include "fts3_tokenizer.h" */
186326
186327typedef struct simple_tokenizer {
186328 sqlite3_tokenizer base;
186329 char delim[128]; /* flag ASCII delimiters */
186330} simple_tokenizer;
186331
186332typedef struct simple_tokenizer_cursor {
186333 sqlite3_tokenizer_cursor base;
186334 const char *pInput; /* input we are tokenizing */
186335 int nBytes; /* size of the input */
186336 int iOffset; /* current position in pInput */
186337 int iToken; /* index of next token to be returned */
186338 char *pToken; /* storage for current token */
186339 int nTokenAllocated; /* space allocated to zToken buffer */
186340} simple_tokenizer_cursor;
186341
186342
186343static int simpleDelim(simple_tokenizer *t, unsigned char c){
186344 return c<0x80 && t->delim[c];
186345}
186346static int fts3_isalnum(int x){
186347 return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z');
186348}
186349
186350/*
186351** Create a new tokenizer instance.
186352*/
186353static int simpleCreate(
186354 int argc, const char * const *argv,
186355 sqlite3_tokenizer **ppTokenizer
186356){
186357 simple_tokenizer *t;
186358
186359 t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t));
186360 if( t==NULL((void*)0) ) return SQLITE_NOMEM7;
186361 memset(t, 0, sizeof(*t));
186362
186363 /* TODO(shess) Delimiters need to remain the same from run to run,
186364 ** else we need to reindex. One solution would be a meta-table to
186365 ** track such information in the database, then we'd only want this
186366 ** information on the initial create.
186367 */
186368 if( argc>1 ){
186369 int i, n = (int)strlen(argv[1]);
186370 for(i=0; i<n; i++){
186371 unsigned char ch = argv[1][i];
186372 /* We explicitly don't support UTF-8 delimiters for now. */
186373 if( ch>=0x80 ){
186374 sqlite3_free(t);
186375 return SQLITE_ERROR1;
186376 }
186377 t->delim[ch] = 1;
186378 }
186379 } else {
186380 /* Mark non-alphanumeric ASCII characters as delimiters */
186381 int i;
186382 for(i=1; i<0x80; i++){
186383 t->delim[i] = !fts3_isalnum(i) ? -1 : 0;
186384 }
186385 }
186386
186387 *ppTokenizer = &t->base;
186388 return SQLITE_OK0;
186389}
186390
186391/*
186392** Destroy a tokenizer
186393*/
186394static int simpleDestroy(sqlite3_tokenizer *pTokenizer){
186395 sqlite3_free(pTokenizer);
186396 return SQLITE_OK0;
186397}
186398
186399/*
186400** Prepare to begin tokenizing a particular string. The input
186401** string to be tokenized is pInput[0..nBytes-1]. A cursor
186402** used to incrementally tokenize this string is returned in
186403** *ppCursor.
186404*/
186405static int simpleOpen(
186406 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
186407 const char *pInput, int nBytes, /* String to be tokenized */
186408 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
186409){
186410 simple_tokenizer_cursor *c;
186411
186412 UNUSED_PARAMETER(pTokenizer)(void)(pTokenizer);
186413
186414 c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
186415 if( c==NULL((void*)0) ) return SQLITE_NOMEM7;
186416
186417 c->pInput = pInput;
186418 if( pInput==0 ){
186419 c->nBytes = 0;
186420 }else if( nBytes<0 ){
186421 c->nBytes = (int)strlen(pInput);
186422 }else{
186423 c->nBytes = nBytes;
186424 }
186425 c->iOffset = 0; /* start tokenizing at the beginning */
186426 c->iToken = 0;
186427 c->pToken = NULL((void*)0); /* no space allocated, yet. */
186428 c->nTokenAllocated = 0;
186429
186430 *ppCursor = &c->base;
186431 return SQLITE_OK0;
186432}
186433
186434/*
186435** Close a tokenization cursor previously opened by a call to
186436** simpleOpen() above.
186437*/
186438static int simpleClose(sqlite3_tokenizer_cursor *pCursor){
186439 simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
186440 sqlite3_free(c->pToken);
186441 sqlite3_free(c);
186442 return SQLITE_OK0;
186443}
186444
186445/*
186446** Extract the next token from a tokenization cursor. The cursor must
186447** have been opened by a prior call to simpleOpen().
186448*/
186449static int simpleNext(
186450 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */
186451 const char **ppToken, /* OUT: *ppToken is the token text */
186452 int *pnBytes, /* OUT: Number of bytes in token */
186453 int *piStartOffset, /* OUT: Starting offset of token */
186454 int *piEndOffset, /* OUT: Ending offset of token */
186455 int *piPosition /* OUT: Position integer of token */
186456){
186457 simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
186458 simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
186459 unsigned char *p = (unsigned char *)c->pInput;
186460
186461 while( c->iOffset<c->nBytes ){
186462 int iStartOffset;
186463
186464 /* Scan past delimiter characters */
186465 while( c->iOffset<c->nBytes && simpleDelim(t, p[c->iOffset]) ){
186466 c->iOffset++;
186467 }
186468
186469 /* Count non-delimiter characters. */
186470 iStartOffset = c->iOffset;
186471 while( c->iOffset<c->nBytes && !simpleDelim(t, p[c->iOffset]) ){
186472 c->iOffset++;
186473 }
186474
186475 if( c->iOffset>iStartOffset ){
186476 int i, n = c->iOffset-iStartOffset;
186477 if( n>c->nTokenAllocated ){
186478 char *pNew;
186479 c->nTokenAllocated = n+20;
186480 pNew = sqlite3_realloc(c->pToken, c->nTokenAllocated);
186481 if( !pNew ) return SQLITE_NOMEM7;
186482 c->pToken = pNew;
186483 }
186484 for(i=0; i<n; i++){
186485 /* TODO(shess) This needs expansion to handle UTF-8
186486 ** case-insensitivity.
186487 */
186488 unsigned char ch = p[iStartOffset+i];
186489 c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch);
186490 }
186491 *ppToken = c->pToken;
186492 *pnBytes = n;
186493 *piStartOffset = iStartOffset;
186494 *piEndOffset = c->iOffset;
186495 *piPosition = c->iToken++;
186496
186497 return SQLITE_OK0;
186498 }
186499 }
186500 return SQLITE_DONE101;
186501}
186502
186503/*
186504** The set of routines that implement the simple tokenizer
186505*/
186506static const sqlite3_tokenizer_module simpleTokenizerModule = {
186507 0,
186508 simpleCreate,
186509 simpleDestroy,
186510 simpleOpen,
186511 simpleClose,
186512 simpleNext,
186513 0,
186514};
186515
186516/*
186517** Allocate a new simple tokenizer. Return a pointer to the new
186518** tokenizer in *ppModule
186519*/
186520SQLITE_PRIVATEstatic void sqlite3Fts3SimpleTokenizerModule(
186521 sqlite3_tokenizer_module const**ppModule
186522){
186523 *ppModule = &simpleTokenizerModule;
186524}
186525
186526#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
186527
186528/************** End of fts3_tokenizer1.c *************************************/
186529/************** Begin file fts3_tokenize_vtab.c ******************************/
186530/*
186531** 2013 Apr 22
186532**
186533** The author disclaims copyright to this source code. In place of
186534** a legal notice, here is a blessing:
186535**
186536** May you do good and not evil.
186537** May you find forgiveness for yourself and forgive others.
186538** May you share freely, never taking more than you give.
186539**
186540******************************************************************************
186541**
186542** This file contains code for the "fts3tokenize" virtual table module.
186543** An fts3tokenize virtual table is created as follows:
186544**
186545** CREATE VIRTUAL TABLE <tbl> USING fts3tokenize(
186546** <tokenizer-name>, <arg-1>, ...
186547** );
186548**
186549** The table created has the following schema:
186550**
186551** CREATE TABLE <tbl>(input, token, start, end, position)
186552**
186553** When queried, the query must include a WHERE clause of type:
186554**
186555** input = <string>
186556**
186557** The virtual table module tokenizes this <string>, using the FTS3
186558** tokenizer specified by the arguments to the CREATE VIRTUAL TABLE
186559** statement and returns one row for each token in the result. With
186560** fields set as follows:
186561**
186562** input: Always set to a copy of <string>
186563** token: A token from the input.
186564** start: Byte offset of the token within the input <string>.
186565** end: Byte offset of the byte immediately following the end of the
186566** token within the input string.
186567** pos: Token offset of token within input.
186568**
186569*/
186570/* #include "fts3Int.h" */
186571#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
186572
186573/* #include <string.h> */
186574/* #include <assert.h> */
186575
186576typedef struct Fts3tokTable Fts3tokTable;
186577typedef struct Fts3tokCursor Fts3tokCursor;
186578
186579/*
186580** Virtual table structure.
186581*/
186582struct Fts3tokTable {
186583 sqlite3_vtab base; /* Base class used by SQLite core */
186584 const sqlite3_tokenizer_module *pMod;
186585 sqlite3_tokenizer *pTok;
186586};
186587
186588/*
186589** Virtual table cursor structure.
186590*/
186591struct Fts3tokCursor {
186592 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
186593 char *zInput; /* Input string */
186594 sqlite3_tokenizer_cursor *pCsr; /* Cursor to iterate through zInput */
186595 int iRowid; /* Current 'rowid' value */
186596 const char *zToken; /* Current 'token' value */
186597 int nToken; /* Size of zToken in bytes */
186598 int iStart; /* Current 'start' value */
186599 int iEnd; /* Current 'end' value */
186600 int iPos; /* Current 'pos' value */
186601};
186602
186603/*
186604** Query FTS for the tokenizer implementation named zName.
186605*/
186606static int fts3tokQueryTokenizer(
186607 Fts3Hash *pHash,
186608 const char *zName,
186609 const sqlite3_tokenizer_module **pp,
186610 char **pzErr
186611){
186612 sqlite3_tokenizer_module *p;
186613 int nName = (int)strlen(zName);
186614
186615 p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
186616 if( !p ){
186617 sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", zName);
186618 return SQLITE_ERROR1;
186619 }
186620
186621 *pp = p;
186622 return SQLITE_OK0;
186623}
186624
186625/*
186626** The second argument, argv[], is an array of pointers to nul-terminated
186627** strings. This function makes a copy of the array and strings into a
186628** single block of memory. It then dequotes any of the strings that appear
186629** to be quoted.
186630**
186631** If successful, output parameter *pazDequote is set to point at the
186632** array of dequoted strings and SQLITE_OK is returned. The caller is
186633** responsible for eventually calling sqlite3_free() to free the array
186634** in this case. Or, if an error occurs, an SQLite error code is returned.
186635** The final value of *pazDequote is undefined in this case.
186636*/
186637static int fts3tokDequoteArray(
186638 int argc, /* Number of elements in argv[] */
186639 const char * const *argv, /* Input array */
186640 char ***pazDequote /* Output array */
186641){
186642 int rc = SQLITE_OK0; /* Return code */
186643 if( argc==0 ){
186644 *pazDequote = 0;
186645 }else{
186646 int i;
186647 int nByte = 0;
186648 char **azDequote;
186649
186650 for(i=0; i<argc; i++){
186651 nByte += (int)(strlen(argv[i]) + 1);
186652 }
186653
186654 *pazDequote = azDequote = sqlite3_malloc64(sizeof(char *)*argc + nByte);
186655 if( azDequote==0 ){
186656 rc = SQLITE_NOMEM7;
186657 }else{
186658 char *pSpace = (char *)&azDequote[argc];
186659 for(i=0; i<argc; i++){
186660 int n = (int)strlen(argv[i]);
186661 azDequote[i] = pSpace;
186662 memcpy(pSpace, argv[i], n+1);
186663 sqlite3Fts3Dequote(pSpace);
186664 pSpace += (n+1);
186665 }
186666 }
186667 }
186668
186669 return rc;
186670}
186671
186672/*
186673** Schema of the tokenizer table.
186674*/
186675#define FTS3_TOK_SCHEMA"CREATE TABLE x(input, token, start, end, position)" "CREATE TABLE x(input, token, start, end, position)"
186676
186677/*
186678** This function does all the work for both the xConnect and xCreate methods.
186679** These tables have no persistent representation of their own, so xConnect
186680** and xCreate are identical operations.
186681**
186682** argv[0]: module name
186683** argv[1]: database name
186684** argv[2]: table name
186685** argv[3]: first argument (tokenizer name)
186686*/
186687static int fts3tokConnectMethod(
186688 sqlite3 *db, /* Database connection */
186689 void *pHash, /* Hash table of tokenizers */
186690 int argc, /* Number of elements in argv array */
186691 const char * const *argv, /* xCreate/xConnect argument array */
186692 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
186693 char **pzErr /* OUT: sqlite3_malloc'd error message */
186694){
186695 Fts3tokTable *pTab = 0;
186696 const sqlite3_tokenizer_module *pMod = 0;
186697 sqlite3_tokenizer *pTok = 0;
186698 int rc;
186699 char **azDequote = 0;
186700 int nDequote;
186701
186702 rc = sqlite3_declare_vtab(db, FTS3_TOK_SCHEMA"CREATE TABLE x(input, token, start, end, position)");
186703 if( rc!=SQLITE_OK0 ) return rc;
186704
186705 nDequote = argc-3;
186706 rc = fts3tokDequoteArray(nDequote, &argv[3], &azDequote);
186707
186708 if( rc==SQLITE_OK0 ){
186709 const char *zModule;
186710 if( nDequote<1 ){
186711 zModule = "simple";
186712 }else{
186713 zModule = azDequote[0];
186714 }
186715 rc = fts3tokQueryTokenizer((Fts3Hash*)pHash, zModule, &pMod, pzErr);
186716 }
186717
186718 assert( (rc==SQLITE_OK)==(pMod!=0) )((void) (0));
186719 if( rc==SQLITE_OK0 ){
186720 const char * const *azArg = 0;
186721 if( nDequote>1 ) azArg = (const char * const *)&azDequote[1];
186722 rc = pMod->xCreate((nDequote>1 ? nDequote-1 : 0), azArg, &pTok);
186723 }
186724
186725 if( rc==SQLITE_OK0 ){
186726 pTab = (Fts3tokTable *)sqlite3_malloc(sizeof(Fts3tokTable));
186727 if( pTab==0 ){
186728 rc = SQLITE_NOMEM7;
186729 }
186730 }
186731
186732 if( rc==SQLITE_OK0 ){
186733 memset(pTab, 0, sizeof(Fts3tokTable));
186734 pTab->pMod = pMod;
186735 pTab->pTok = pTok;
186736 *ppVtab = &pTab->base;
186737 }else{
186738 if( pTok ){
186739 pMod->xDestroy(pTok);
186740 }
186741 }
186742
186743 sqlite3_free(azDequote);
186744 return rc;
186745}
186746
186747/*
186748** This function does the work for both the xDisconnect and xDestroy methods.
186749** These tables have no persistent representation of their own, so xDisconnect
186750** and xDestroy are identical operations.
186751*/
186752static int fts3tokDisconnectMethod(sqlite3_vtab *pVtab){
186753 Fts3tokTable *pTab = (Fts3tokTable *)pVtab;
186754
186755 pTab->pMod->xDestroy(pTab->pTok);
186756 sqlite3_free(pTab);
186757 return SQLITE_OK0;
186758}
186759
186760/*
186761** xBestIndex - Analyze a WHERE and ORDER BY clause.
186762*/
186763static int fts3tokBestIndexMethod(
186764 sqlite3_vtab *pVTab,
186765 sqlite3_index_info *pInfo
186766){
186767 int i;
186768 UNUSED_PARAMETER(pVTab)(void)(pVTab);
186769
186770 for(i=0; i<pInfo->nConstraint; i++){
186771 if( pInfo->aConstraint[i].usable
186772 && pInfo->aConstraint[i].iColumn==0
186773 && pInfo->aConstraint[i].op==SQLITE_INDEX_CONSTRAINT_EQ2
186774 ){
186775 pInfo->idxNum = 1;
186776 pInfo->aConstraintUsage[i].argvIndex = 1;
186777 pInfo->aConstraintUsage[i].omit = 1;
186778 pInfo->estimatedCost = 1;
186779 return SQLITE_OK0;
186780 }
186781 }
186782
186783 pInfo->idxNum = 0;
186784 assert( pInfo->estimatedCost>1000000.0 )((void) (0));
186785
186786 return SQLITE_OK0;
186787}
186788
186789/*
186790** xOpen - Open a cursor.
186791*/
186792static int fts3tokOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
186793 Fts3tokCursor *pCsr;
186794 UNUSED_PARAMETER(pVTab)(void)(pVTab);
186795
186796 pCsr = (Fts3tokCursor *)sqlite3_malloc(sizeof(Fts3tokCursor));
186797 if( pCsr==0 ){
186798 return SQLITE_NOMEM7;
186799 }
186800 memset(pCsr, 0, sizeof(Fts3tokCursor));
186801
186802 *ppCsr = (sqlite3_vtab_cursor *)pCsr;
186803 return SQLITE_OK0;
186804}
186805
186806/*
186807** Reset the tokenizer cursor passed as the only argument. As if it had
186808** just been returned by fts3tokOpenMethod().
186809*/
186810static void fts3tokResetCursor(Fts3tokCursor *pCsr){
186811 if( pCsr->pCsr ){
186812 Fts3tokTable *pTab = (Fts3tokTable *)(pCsr->base.pVtab);
186813 pTab->pMod->xClose(pCsr->pCsr);
186814 pCsr->pCsr = 0;
186815 }
186816 sqlite3_free(pCsr->zInput);
186817 pCsr->zInput = 0;
186818 pCsr->zToken = 0;
186819 pCsr->nToken = 0;
186820 pCsr->iStart = 0;
186821 pCsr->iEnd = 0;
186822 pCsr->iPos = 0;
186823 pCsr->iRowid = 0;
186824}
186825
186826/*
186827** xClose - Close a cursor.
186828*/
186829static int fts3tokCloseMethod(sqlite3_vtab_cursor *pCursor){
186830 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
186831
186832 fts3tokResetCursor(pCsr);
186833 sqlite3_free(pCsr);
186834 return SQLITE_OK0;
186835}
186836
186837/*
186838** xNext - Advance the cursor to the next row, if any.
186839*/
186840static int fts3tokNextMethod(sqlite3_vtab_cursor *pCursor){
186841 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
186842 Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
186843 int rc; /* Return code */
186844
186845 pCsr->iRowid++;
186846 rc = pTab->pMod->xNext(pCsr->pCsr,
186847 &pCsr->zToken, &pCsr->nToken,
186848 &pCsr->iStart, &pCsr->iEnd, &pCsr->iPos
186849 );
186850
186851 if( rc!=SQLITE_OK0 ){
186852 fts3tokResetCursor(pCsr);
186853 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
186854 }
186855
186856 return rc;
186857}
186858
186859/*
186860** xFilter - Initialize a cursor to point at the start of its data.
186861*/
186862static int fts3tokFilterMethod(
186863 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
186864 int idxNum, /* Strategy index */
186865 const char *idxStr, /* Unused */
186866 int nVal, /* Number of elements in apVal */
186867 sqlite3_value **apVal /* Arguments for the indexing scheme */
186868){
186869 int rc = SQLITE_ERROR1;
186870 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
186871 Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
186872 UNUSED_PARAMETER(idxStr)(void)(idxStr);
186873 UNUSED_PARAMETER(nVal)(void)(nVal);
186874
186875 fts3tokResetCursor(pCsr);
186876 if( idxNum==1 ){
186877 const char *zByte = (const char *)sqlite3_value_text(apVal[0]);
186878 int nByte = sqlite3_value_bytes(apVal[0]);
186879 pCsr->zInput = sqlite3_malloc64(nByte+1);
186880 if( pCsr->zInput==0 ){
186881 rc = SQLITE_NOMEM7;
186882 }else{
186883 if( nByte>0 ) memcpy(pCsr->zInput, zByte, nByte);
186884 pCsr->zInput[nByte] = 0;
186885 rc = pTab->pMod->xOpen(pTab->pTok, pCsr->zInput, nByte, &pCsr->pCsr);
186886 if( rc==SQLITE_OK0 ){
186887 pCsr->pCsr->pTokenizer = pTab->pTok;
186888 }
186889 }
186890 }
186891
186892 if( rc!=SQLITE_OK0 ) return rc;
186893 return fts3tokNextMethod(pCursor);
186894}
186895
186896/*
186897** xEof - Return true if the cursor is at EOF, or false otherwise.
186898*/
186899static int fts3tokEofMethod(sqlite3_vtab_cursor *pCursor){
186900 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
186901 return (pCsr->zToken==0);
186902}
186903
186904/*
186905** xColumn - Return a column value.
186906*/
186907static int fts3tokColumnMethod(
186908 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
186909 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
186910 int iCol /* Index of column to read value from */
186911){
186912 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
186913
186914 /* CREATE TABLE x(input, token, start, end, position) */
186915 switch( iCol ){
186916 case 0:
186917 sqlite3_result_text(pCtx, pCsr->zInput, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
186918 break;
186919 case 1:
186920 sqlite3_result_text(pCtx, pCsr->zToken, pCsr->nToken, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
186921 break;
186922 case 2:
186923 sqlite3_result_int(pCtx, pCsr->iStart);
186924 break;
186925 case 3:
186926 sqlite3_result_int(pCtx, pCsr->iEnd);
186927 break;
186928 default:
186929 assert( iCol==4 )((void) (0));
186930 sqlite3_result_int(pCtx, pCsr->iPos);
186931 break;
186932 }
186933 return SQLITE_OK0;
186934}
186935
186936/*
186937** xRowid - Return the current rowid for the cursor.
186938*/
186939static int fts3tokRowidMethod(
186940 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
186941 sqlite_int64 *pRowid /* OUT: Rowid value */
186942){
186943 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
186944 *pRowid = (sqlite3_int64)pCsr->iRowid;
186945 return SQLITE_OK0;
186946}
186947
186948/*
186949** Register the fts3tok module with database connection db. Return SQLITE_OK
186950** if successful or an error code if sqlite3_create_module() fails.
186951*/
186952SQLITE_PRIVATEstatic int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash, void(*xDestroy)(void*)){
186953 static const sqlite3_module fts3tok_module = {
186954 0, /* iVersion */
186955 fts3tokConnectMethod, /* xCreate */
186956 fts3tokConnectMethod, /* xConnect */
186957 fts3tokBestIndexMethod, /* xBestIndex */
186958 fts3tokDisconnectMethod, /* xDisconnect */
186959 fts3tokDisconnectMethod, /* xDestroy */
186960 fts3tokOpenMethod, /* xOpen */
186961 fts3tokCloseMethod, /* xClose */
186962 fts3tokFilterMethod, /* xFilter */
186963 fts3tokNextMethod, /* xNext */
186964 fts3tokEofMethod, /* xEof */
186965 fts3tokColumnMethod, /* xColumn */
186966 fts3tokRowidMethod, /* xRowid */
186967 0, /* xUpdate */
186968 0, /* xBegin */
186969 0, /* xSync */
186970 0, /* xCommit */
186971 0, /* xRollback */
186972 0, /* xFindFunction */
186973 0, /* xRename */
186974 0, /* xSavepoint */
186975 0, /* xRelease */
186976 0, /* xRollbackTo */
186977 0 /* xShadowName */
186978 };
186979 int rc; /* Return code */
186980
186981 rc = sqlite3_create_module_v2(
186982 db, "fts3tokenize", &fts3tok_module, (void*)pHash, xDestroy
186983 );
186984 return rc;
186985}
186986
186987#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
186988
186989/************** End of fts3_tokenize_vtab.c **********************************/
186990/************** Begin file fts3_write.c **************************************/
186991/*
186992** 2009 Oct 23
186993**
186994** The author disclaims copyright to this source code. In place of
186995** a legal notice, here is a blessing:
186996**
186997** May you do good and not evil.
186998** May you find forgiveness for yourself and forgive others.
186999** May you share freely, never taking more than you give.
187000**
187001******************************************************************************
187002**
187003** This file is part of the SQLite FTS3 extension module. Specifically,
187004** this file contains code to insert, update and delete rows from FTS3
187005** tables. It also contains code to merge FTS3 b-tree segments. Some
187006** of the sub-routines used to merge segments are also used by the query
187007** code in fts3.c.
187008*/
187009
187010/* #include "fts3Int.h" */
187011#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
187012
187013/* #include <string.h> */
187014/* #include <assert.h> */
187015/* #include <stdlib.h> */
187016/* #include <stdio.h> */
187017
187018#define FTS_MAX_APPENDABLE_HEIGHT16 16
187019
187020/*
187021** When full-text index nodes are loaded from disk, the buffer that they
187022** are loaded into has the following number of bytes of padding at the end
187023** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
187024** of 920 bytes is allocated for it.
187025**
187026** This means that if we have a pointer into a buffer containing node data,
187027** it is always safe to read up to two varints from it without risking an
187028** overread, even if the node data is corrupted.
187029*/
187030#define FTS3_NODE_PADDING(10*2) (FTS3_VARINT_MAX10*2)
187031
187032/*
187033** Under certain circumstances, b-tree nodes (doclists) can be loaded into
187034** memory incrementally instead of all at once. This can be a big performance
187035** win (reduced IO and CPU) if SQLite stops calling the virtual table xNext()
187036** method before retrieving all query results (as may happen, for example,
187037** if a query has a LIMIT clause).
187038**
187039** Incremental loading is used for b-tree nodes FTS3_NODE_CHUNK_THRESHOLD
187040** bytes and larger. Nodes are loaded in chunks of FTS3_NODE_CHUNKSIZE bytes.
187041** The code is written so that the hard lower-limit for each of these values
187042** is 1. Clearly such small values would be inefficient, but can be useful
187043** for testing purposes.
187044**
187045** If this module is built with SQLITE_TEST defined, these constants may
187046** be overridden at runtime for testing purposes. File fts3_test.c contains
187047** a Tcl interface to read and write the values.
187048*/
187049#ifdef SQLITE_TEST
187050int test_fts3_node_chunksize = (4*1024);
187051int test_fts3_node_chunk_threshold = (4*1024)*4;
187052# define FTS3_NODE_CHUNKSIZE(4*1024) test_fts3_node_chunksize
187053# define FTS3_NODE_CHUNK_THRESHOLD((4*1024)*4) test_fts3_node_chunk_threshold
187054#else
187055# define FTS3_NODE_CHUNKSIZE(4*1024) (4*1024)
187056# define FTS3_NODE_CHUNK_THRESHOLD((4*1024)*4) (FTS3_NODE_CHUNKSIZE(4*1024)*4)
187057#endif
187058
187059/*
187060** The values that may be meaningfully bound to the :1 parameter in
187061** statements SQL_REPLACE_STAT and SQL_SELECT_STAT.
187062*/
187063#define FTS_STAT_DOCTOTAL0 0
187064#define FTS_STAT_INCRMERGEHINT1 1
187065#define FTS_STAT_AUTOINCRMERGE2 2
187066
187067/*
187068** If FTS_LOG_MERGES is defined, call sqlite3_log() to report each automatic
187069** and incremental merge operation that takes place. This is used for
187070** debugging FTS only, it should not usually be turned on in production
187071** systems.
187072*/
187073#ifdef FTS3_LOG_MERGES
187074static void fts3LogMerge(int nMerge, sqlite3_int64 iAbsLevel){
187075 sqlite3_log(SQLITE_OK0, "%d-way merge from level %d", nMerge, (int)iAbsLevel);
187076}
187077#else
187078#define fts3LogMerge(x, y)
187079#endif
187080
187081
187082typedef struct PendingList PendingList;
187083typedef struct SegmentNode SegmentNode;
187084typedef struct SegmentWriter SegmentWriter;
187085
187086/*
187087** An instance of the following data structure is used to build doclists
187088** incrementally. See function fts3PendingListAppend() for details.
187089*/
187090struct PendingList {
187091 int nData;
187092 char *aData;
187093 int nSpace;
187094 sqlite3_int64 iLastDocid;
187095 sqlite3_int64 iLastCol;
187096 sqlite3_int64 iLastPos;
187097};
187098
187099
187100/*
187101** Each cursor has a (possibly empty) linked list of the following objects.
187102*/
187103struct Fts3DeferredToken {
187104 Fts3PhraseToken *pToken; /* Pointer to corresponding expr token */
187105 int iCol; /* Column token must occur in */
187106 Fts3DeferredToken *pNext; /* Next in list of deferred tokens */
187107 PendingList *pList; /* Doclist is assembled here */
187108};
187109
187110/*
187111** An instance of this structure is used to iterate through the terms on
187112** a contiguous set of segment b-tree leaf nodes. Although the details of
187113** this structure are only manipulated by code in this file, opaque handles
187114** of type Fts3SegReader* are also used by code in fts3.c to iterate through
187115** terms when querying the full-text index. See functions:
187116**
187117** sqlite3Fts3SegReaderNew()
187118** sqlite3Fts3SegReaderFree()
187119** sqlite3Fts3SegReaderIterate()
187120**
187121** Methods used to manipulate Fts3SegReader structures:
187122**
187123** fts3SegReaderNext()
187124** fts3SegReaderFirstDocid()
187125** fts3SegReaderNextDocid()
187126*/
187127struct Fts3SegReader {
187128 int iIdx; /* Index within level, or 0x7FFFFFFF for PT */
187129 u8 bLookup; /* True for a lookup only */
187130 u8 rootOnly; /* True for a root-only reader */
187131
187132 sqlite3_int64 iStartBlock; /* Rowid of first leaf block to traverse */
187133 sqlite3_int64 iLeafEndBlock; /* Rowid of final leaf block to traverse */
187134 sqlite3_int64 iEndBlock; /* Rowid of final block in segment (or 0) */
187135 sqlite3_int64 iCurrentBlock; /* Current leaf block (or 0) */
187136
187137 char *aNode; /* Pointer to node data (or NULL) */
187138 int nNode; /* Size of buffer at aNode (or 0) */
187139 int nPopulate; /* If >0, bytes of buffer aNode[] loaded */
187140 sqlite3_blob *pBlob; /* If not NULL, blob handle to read node */
187141
187142 Fts3HashElem **ppNextElem;
187143
187144 /* Variables set by fts3SegReaderNext(). These may be read directly
187145 ** by the caller. They are valid from the time SegmentReaderNew() returns
187146 ** until SegmentReaderNext() returns something other than SQLITE_OK
187147 ** (i.e. SQLITE_DONE).
187148 */
187149 int nTerm; /* Number of bytes in current term */
187150 char *zTerm; /* Pointer to current term */
187151 int nTermAlloc; /* Allocated size of zTerm buffer */
187152 char *aDoclist; /* Pointer to doclist of current entry */
187153 int nDoclist; /* Size of doclist in current entry */
187154
187155 /* The following variables are used by fts3SegReaderNextDocid() to iterate
187156 ** through the current doclist (aDoclist/nDoclist).
187157 */
187158 char *pOffsetList;
187159 int nOffsetList; /* For descending pending seg-readers only */
187160 sqlite3_int64 iDocid;
187161};
187162
187163#define fts3SegReaderIsPending(p)((p)->ppNextElem!=0) ((p)->ppNextElem!=0)
187164#define fts3SegReaderIsRootOnly(p)((p)->rootOnly!=0) ((p)->rootOnly!=0)
187165
187166/*
187167** An instance of this structure is used to create a segment b-tree in the
187168** database. The internal details of this type are only accessed by the
187169** following functions:
187170**
187171** fts3SegWriterAdd()
187172** fts3SegWriterFlush()
187173** fts3SegWriterFree()
187174*/
187175struct SegmentWriter {
187176 SegmentNode *pTree; /* Pointer to interior tree structure */
187177 sqlite3_int64 iFirst; /* First slot in %_segments written */
187178 sqlite3_int64 iFree; /* Next free slot in %_segments */
187179 char *zTerm; /* Pointer to previous term buffer */
187180 int nTerm; /* Number of bytes in zTerm */
187181 int nMalloc; /* Size of malloc'd buffer at zMalloc */
187182 char *zMalloc; /* Malloc'd space (possibly) used for zTerm */
187183 int nSize; /* Size of allocation at aData */
187184 int nData; /* Bytes of data in aData */
187185 char *aData; /* Pointer to block from malloc() */
187186 i64 nLeafData; /* Number of bytes of leaf data written */
187187};
187188
187189/*
187190** Type SegmentNode is used by the following three functions to create
187191** the interior part of the segment b+-tree structures (everything except
187192** the leaf nodes). These functions and type are only ever used by code
187193** within the fts3SegWriterXXX() family of functions described above.
187194**
187195** fts3NodeAddTerm()
187196** fts3NodeWrite()
187197** fts3NodeFree()
187198**
187199** When a b+tree is written to the database (either as a result of a merge
187200** or the pending-terms table being flushed), leaves are written into the
187201** database file as soon as they are completely populated. The interior of
187202** the tree is assembled in memory and written out only once all leaves have
187203** been populated and stored. This is Ok, as the b+-tree fanout is usually
187204** very large, meaning that the interior of the tree consumes relatively
187205** little memory.
187206*/
187207struct SegmentNode {
187208 SegmentNode *pParent; /* Parent node (or NULL for root node) */
187209 SegmentNode *pRight; /* Pointer to right-sibling */
187210 SegmentNode *pLeftmost; /* Pointer to left-most node of this depth */
187211 int nEntry; /* Number of terms written to node so far */
187212 char *zTerm; /* Pointer to previous term buffer */
187213 int nTerm; /* Number of bytes in zTerm */
187214 int nMalloc; /* Size of malloc'd buffer at zMalloc */
187215 char *zMalloc; /* Malloc'd space (possibly) used for zTerm */
187216 int nData; /* Bytes of valid data so far */
187217 char *aData; /* Node data */
187218};
187219
187220/*
187221** Valid values for the second argument to fts3SqlStmt().
187222*/
187223#define SQL_DELETE_CONTENT0 0
187224#define SQL_IS_EMPTY1 1
187225#define SQL_DELETE_ALL_CONTENT2 2
187226#define SQL_DELETE_ALL_SEGMENTS3 3
187227#define SQL_DELETE_ALL_SEGDIR4 4
187228#define SQL_DELETE_ALL_DOCSIZE5 5
187229#define SQL_DELETE_ALL_STAT6 6
187230#define SQL_SELECT_CONTENT_BY_ROWID7 7
187231#define SQL_NEXT_SEGMENT_INDEX8 8
187232#define SQL_INSERT_SEGMENTS9 9
187233#define SQL_NEXT_SEGMENTS_ID10 10
187234#define SQL_INSERT_SEGDIR11 11
187235#define SQL_SELECT_LEVEL12 12
187236#define SQL_SELECT_LEVEL_RANGE13 13
187237#define SQL_SELECT_LEVEL_COUNT14 14
187238#define SQL_SELECT_SEGDIR_MAX_LEVEL15 15
187239#define SQL_DELETE_SEGDIR_LEVEL16 16
187240#define SQL_DELETE_SEGMENTS_RANGE17 17
187241#define SQL_CONTENT_INSERT18 18
187242#define SQL_DELETE_DOCSIZE19 19
187243#define SQL_REPLACE_DOCSIZE20 20
187244#define SQL_SELECT_DOCSIZE21 21
187245#define SQL_SELECT_STAT22 22
187246#define SQL_REPLACE_STAT23 23
187247
187248#define SQL_SELECT_ALL_PREFIX_LEVEL24 24
187249#define SQL_DELETE_ALL_TERMS_SEGDIR25 25
187250#define SQL_DELETE_SEGDIR_RANGE26 26
187251#define SQL_SELECT_ALL_LANGID27 27
187252#define SQL_FIND_MERGE_LEVEL28 28
187253#define SQL_MAX_LEAF_NODE_ESTIMATE29 29
187254#define SQL_DELETE_SEGDIR_ENTRY30 30
187255#define SQL_SHIFT_SEGDIR_ENTRY31 31
187256#define SQL_SELECT_SEGDIR32 32
187257#define SQL_CHOMP_SEGDIR33 33
187258#define SQL_SEGMENT_IS_APPENDABLE34 34
187259#define SQL_SELECT_INDEXES35 35
187260#define SQL_SELECT_MXLEVEL36 36
187261
187262#define SQL_SELECT_LEVEL_RANGE237 37
187263#define SQL_UPDATE_LEVEL_IDX38 38
187264#define SQL_UPDATE_LEVEL39 39
187265
187266/*
187267** This function is used to obtain an SQLite prepared statement handle
187268** for the statement identified by the second argument. If successful,
187269** *pp is set to the requested statement handle and SQLITE_OK returned.
187270** Otherwise, an SQLite error code is returned and *pp is set to 0.
187271**
187272** If argument apVal is not NULL, then it must point to an array with
187273** at least as many entries as the requested statement has bound
187274** parameters. The values are bound to the statements parameters before
187275** returning.
187276*/
187277static int fts3SqlStmt(
187278 Fts3Table *p, /* Virtual table handle */
187279 int eStmt, /* One of the SQL_XXX constants above */
187280 sqlite3_stmt **pp, /* OUT: Statement handle */
187281 sqlite3_value **apVal /* Values to bind to statement */
187282){
187283 const char *azSql[] = {
187284/* 0 */ "DELETE FROM %Q.'%q_content' WHERE rowid = ?",
187285/* 1 */ "SELECT NOT EXISTS(SELECT docid FROM %Q.'%q_content' WHERE rowid!=?)",
187286/* 2 */ "DELETE FROM %Q.'%q_content'",
187287/* 3 */ "DELETE FROM %Q.'%q_segments'",
187288/* 4 */ "DELETE FROM %Q.'%q_segdir'",
187289/* 5 */ "DELETE FROM %Q.'%q_docsize'",
187290/* 6 */ "DELETE FROM %Q.'%q_stat'",
187291/* 7 */ "SELECT %s WHERE rowid=?",
187292/* 8 */ "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
187293/* 9 */ "REPLACE INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
187294/* 10 */ "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
187295/* 11 */ "REPLACE INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
187296
187297 /* Return segments in order from oldest to newest.*/
187298/* 12 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
187299 "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC",
187300/* 13 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
187301 "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?"
187302 "ORDER BY level DESC, idx ASC",
187303
187304/* 14 */ "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?",
187305/* 15 */ "SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
187306
187307/* 16 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
187308/* 17 */ "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
187309/* 18 */ "INSERT INTO %Q.'%q_content' VALUES(%s)",
187310/* 19 */ "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
187311/* 20 */ "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
187312/* 21 */ "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
187313/* 22 */ "SELECT value FROM %Q.'%q_stat' WHERE id=?",
187314/* 23 */ "REPLACE INTO %Q.'%q_stat' VALUES(?,?)",
187315/* 24 */ "",
187316/* 25 */ "",
187317
187318/* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
187319/* 27 */ "SELECT ? UNION SELECT level / (1024 * ?) FROM %Q.'%q_segdir'",
187320
187321/* This statement is used to determine which level to read the input from
187322** when performing an incremental merge. It returns the absolute level number
187323** of the oldest level in the db that contains at least ? segments. Or,
187324** if no level in the FTS index contains more than ? segments, the statement
187325** returns zero rows. */
187326/* 28 */ "SELECT level, count(*) AS cnt FROM %Q.'%q_segdir' "
187327 " GROUP BY level HAVING cnt>=?"
187328 " ORDER BY (level %% 1024) ASC, 2 DESC LIMIT 1",
187329
187330/* Estimate the upper limit on the number of leaf nodes in a new segment
187331** created by merging the oldest :2 segments from absolute level :1. See
187332** function sqlite3Fts3Incrmerge() for details. */
187333/* 29 */ "SELECT 2 * total(1 + leaves_end_block - start_block) "
187334 " FROM (SELECT * FROM %Q.'%q_segdir' "
187335 " WHERE level = ? ORDER BY idx ASC LIMIT ?"
187336 " )",
187337
187338/* SQL_DELETE_SEGDIR_ENTRY
187339** Delete the %_segdir entry on absolute level :1 with index :2. */
187340/* 30 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
187341
187342/* SQL_SHIFT_SEGDIR_ENTRY
187343** Modify the idx value for the segment with idx=:3 on absolute level :2
187344** to :1. */
187345/* 31 */ "UPDATE %Q.'%q_segdir' SET idx = ? WHERE level=? AND idx=?",
187346
187347/* SQL_SELECT_SEGDIR
187348** Read a single entry from the %_segdir table. The entry from absolute
187349** level :1 with index value :2. */
187350/* 32 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
187351 "FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
187352
187353/* SQL_CHOMP_SEGDIR
187354** Update the start_block (:1) and root (:2) fields of the %_segdir
187355** entry located on absolute level :3 with index :4. */
187356/* 33 */ "UPDATE %Q.'%q_segdir' SET start_block = ?, root = ?"
187357 "WHERE level = ? AND idx = ?",
187358
187359/* SQL_SEGMENT_IS_APPENDABLE
187360** Return a single row if the segment with end_block=? is appendable. Or
187361** no rows otherwise. */
187362/* 34 */ "SELECT 1 FROM %Q.'%q_segments' WHERE blockid=? AND block IS NULL",
187363
187364/* SQL_SELECT_INDEXES
187365** Return the list of valid segment indexes for absolute level ? */
187366/* 35 */ "SELECT idx FROM %Q.'%q_segdir' WHERE level=? ORDER BY 1 ASC",
187367
187368/* SQL_SELECT_MXLEVEL
187369** Return the largest relative level in the FTS index or indexes. */
187370/* 36 */ "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'",
187371
187372 /* Return segments in order from oldest to newest.*/
187373/* 37 */ "SELECT level, idx, end_block "
187374 "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ? "
187375 "ORDER BY level DESC, idx ASC",
187376
187377 /* Update statements used while promoting segments */
187378/* 38 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=-1,idx=? "
187379 "WHERE level=? AND idx=?",
187380/* 39 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=? WHERE level=-1"
187381
187382 };
187383 int rc = SQLITE_OK0;
187384 sqlite3_stmt *pStmt;
187385
187386 assert( SizeofArray(azSql)==SizeofArray(p->aStmt) )((void) (0));
187387 assert( eStmt<SizeofArray(azSql) && eStmt>=0 )((void) (0));
187388
187389 pStmt = p->aStmt[eStmt];
187390 if( !pStmt ){
187391 int f = SQLITE_PREPARE_PERSISTENT0x01|SQLITE_PREPARE_NO_VTAB0x04;
187392 char *zSql;
187393 if( eStmt==SQL_CONTENT_INSERT18 ){
187394 zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
187395 }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID7 ){
187396 f &= ~SQLITE_PREPARE_NO_VTAB0x04;
187397 zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist);
187398 }else{
187399 zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
187400 }
187401 if( !zSql ){
187402 rc = SQLITE_NOMEM7;
187403 }else{
187404 rc = sqlite3_prepare_v3(p->db, zSql, -1, f, &pStmt, NULL((void*)0));
187405 sqlite3_free(zSql);
187406 assert( rc==SQLITE_OK || pStmt==0 )((void) (0));
187407 p->aStmt[eStmt] = pStmt;
187408 }
187409 }
187410 if( apVal ){
187411 int i;
187412 int nParam = sqlite3_bind_parameter_count(pStmt);
187413 for(i=0; rc==SQLITE_OK0 && i<nParam; i++){
187414 rc = sqlite3_bind_value(pStmt, i+1, apVal[i]);
187415 }
187416 }
187417 *pp = pStmt;
187418 return rc;
187419}
187420
187421
187422static int fts3SelectDocsize(
187423 Fts3Table *pTab, /* FTS3 table handle */
187424 sqlite3_int64 iDocid, /* Docid to bind for SQL_SELECT_DOCSIZE */
187425 sqlite3_stmt **ppStmt /* OUT: Statement handle */
187426){
187427 sqlite3_stmt *pStmt = 0; /* Statement requested from fts3SqlStmt() */
187428 int rc; /* Return code */
187429
187430 rc = fts3SqlStmt(pTab, SQL_SELECT_DOCSIZE21, &pStmt, 0);
187431 if( rc==SQLITE_OK0 ){
187432 sqlite3_bind_int64(pStmt, 1, iDocid);
187433 rc = sqlite3_step(pStmt);
187434 if( rc!=SQLITE_ROW100 || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB4 ){
187435 rc = sqlite3_reset(pStmt);
187436 if( rc==SQLITE_OK0 ) rc = FTS_CORRUPT_VTAB(11 | (1<<8));
187437 pStmt = 0;
187438 }else{
187439 rc = SQLITE_OK0;
187440 }
187441 }
187442
187443 *ppStmt = pStmt;
187444 return rc;
187445}
187446
187447SQLITE_PRIVATEstatic int sqlite3Fts3SelectDoctotal(
187448 Fts3Table *pTab, /* Fts3 table handle */
187449 sqlite3_stmt **ppStmt /* OUT: Statement handle */
187450){
187451 sqlite3_stmt *pStmt = 0;
187452 int rc;
187453 rc = fts3SqlStmt(pTab, SQL_SELECT_STAT22, &pStmt, 0);
187454 if( rc==SQLITE_OK0 ){
187455 sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL0);
187456 if( sqlite3_step(pStmt)!=SQLITE_ROW100
187457 || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB4
187458 ){
187459 rc = sqlite3_reset(pStmt);
187460 if( rc==SQLITE_OK0 ) rc = FTS_CORRUPT_VTAB(11 | (1<<8));
187461 pStmt = 0;
187462 }
187463 }
187464 *ppStmt = pStmt;
187465 return rc;
187466}
187467
187468SQLITE_PRIVATEstatic int sqlite3Fts3SelectDocsize(
187469 Fts3Table *pTab, /* Fts3 table handle */
187470 sqlite3_int64 iDocid, /* Docid to read size data for */
187471 sqlite3_stmt **ppStmt /* OUT: Statement handle */
187472){
187473 return fts3SelectDocsize(pTab, iDocid, ppStmt);
187474}
187475
187476/*
187477** Similar to fts3SqlStmt(). Except, after binding the parameters in
187478** array apVal[] to the SQL statement identified by eStmt, the statement
187479** is executed.
187480**
187481** Returns SQLITE_OK if the statement is successfully executed, or an
187482** SQLite error code otherwise.
187483*/
187484static void fts3SqlExec(
187485 int *pRC, /* Result code */
187486 Fts3Table *p, /* The FTS3 table */
187487 int eStmt, /* Index of statement to evaluate */
187488 sqlite3_value **apVal /* Parameters to bind */
187489){
187490 sqlite3_stmt *pStmt;
187491 int rc;
187492 if( *pRC ) return;
187493 rc = fts3SqlStmt(p, eStmt, &pStmt, apVal);
187494 if( rc==SQLITE_OK0 ){
187495 sqlite3_step(pStmt);
187496 rc = sqlite3_reset(pStmt);
187497 }
187498 *pRC = rc;
187499}
187500
187501
187502/*
187503** This function ensures that the caller has obtained an exclusive
187504** shared-cache table-lock on the %_segdir table. This is required before
187505** writing data to the fts3 table. If this lock is not acquired first, then
187506** the caller may end up attempting to take this lock as part of committing
187507** a transaction, causing SQLite to return SQLITE_LOCKED or
187508** LOCKED_SHAREDCACHEto a COMMIT command.
187509**
187510** It is best to avoid this because if FTS3 returns any error when
187511** committing a transaction, the whole transaction will be rolled back.
187512** And this is not what users expect when they get SQLITE_LOCKED_SHAREDCACHE.
187513** It can still happen if the user locks the underlying tables directly
187514** instead of accessing them via FTS.
187515*/
187516static int fts3Writelock(Fts3Table *p){
187517 int rc = SQLITE_OK0;
187518
187519 if( p->nPendingData==0 ){
187520 sqlite3_stmt *pStmt;
187521 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL16, &pStmt, 0);
187522 if( rc==SQLITE_OK0 ){
187523 sqlite3_bind_null(pStmt, 1);
187524 sqlite3_step(pStmt);
187525 rc = sqlite3_reset(pStmt);
187526 }
187527 }
187528
187529 return rc;
187530}
187531
187532/*
187533** FTS maintains a separate indexes for each language-id (a 32-bit integer).
187534** Within each language id, a separate index is maintained to store the
187535** document terms, and each configured prefix size (configured the FTS
187536** "prefix=" option). And each index consists of multiple levels ("relative
187537** levels").
187538**
187539** All three of these values (the language id, the specific index and the
187540** level within the index) are encoded in 64-bit integer values stored
187541** in the %_segdir table on disk. This function is used to convert three
187542** separate component values into the single 64-bit integer value that
187543** can be used to query the %_segdir table.
187544**
187545** Specifically, each language-id/index combination is allocated 1024
187546** 64-bit integer level values ("absolute levels"). The main terms index
187547** for language-id 0 is allocate values 0-1023. The first prefix index
187548** (if any) for language-id 0 is allocated values 1024-2047. And so on.
187549** Language 1 indexes are allocated immediately following language 0.
187550**
187551** So, for a system with nPrefix prefix indexes configured, the block of
187552** absolute levels that corresponds to language-id iLangid and index
187553** iIndex starts at absolute level ((iLangid * (nPrefix+1) + iIndex) * 1024).
187554*/
187555static sqlite3_int64 getAbsoluteLevel(
187556 Fts3Table *p, /* FTS3 table handle */
187557 int iLangid, /* Language id */
187558 int iIndex, /* Index in p->aIndex[] */
187559 int iLevel /* Level of segments */
187560){
187561 sqlite3_int64 iBase; /* First absolute level for iLangid/iIndex */
187562 assert_fts3_nc( iLangid>=0 )((void) (0));
187563 assert( p->nIndex>0 )((void) (0));
187564 assert( iIndex>=0 && iIndex<p->nIndex )((void) (0));
187565
187566 iBase = ((sqlite3_int64)iLangid * p->nIndex + iIndex) * FTS3_SEGDIR_MAXLEVEL1024;
187567 return iBase + iLevel;
187568}
187569
187570/*
187571** Set *ppStmt to a statement handle that may be used to iterate through
187572** all rows in the %_segdir table, from oldest to newest. If successful,
187573** return SQLITE_OK. If an error occurs while preparing the statement,
187574** return an SQLite error code.
187575**
187576** There is only ever one instance of this SQL statement compiled for
187577** each FTS3 table.
187578**
187579** The statement returns the following columns from the %_segdir table:
187580**
187581** 0: idx
187582** 1: start_block
187583** 2: leaves_end_block
187584** 3: end_block
187585** 4: root
187586*/
187587SQLITE_PRIVATEstatic int sqlite3Fts3AllSegdirs(
187588 Fts3Table *p, /* FTS3 table */
187589 int iLangid, /* Language being queried */
187590 int iIndex, /* Index for p->aIndex[] */
187591 int iLevel, /* Level to select (relative level) */
187592 sqlite3_stmt **ppStmt /* OUT: Compiled statement */
187593){
187594 int rc;
187595 sqlite3_stmt *pStmt = 0;
187596
187597 assert( iLevel==FTS3_SEGCURSOR_ALL || iLevel>=0 )((void) (0));
187598 assert( iLevel<FTS3_SEGDIR_MAXLEVEL )((void) (0));
187599 assert( iIndex>=0 && iIndex<p->nIndex )((void) (0));
187600
187601 if( iLevel<0 ){
187602 /* "SELECT * FROM %_segdir WHERE level BETWEEN ? AND ? ORDER BY ..." */
187603 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE13, &pStmt, 0);
187604 if( rc==SQLITE_OK0 ){
187605 sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
187606 sqlite3_bind_int64(pStmt, 2,
187607 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL1024-1)
187608 );
187609 }
187610 }else{
187611 /* "SELECT * FROM %_segdir WHERE level = ? ORDER BY ..." */
187612 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL12, &pStmt, 0);
187613 if( rc==SQLITE_OK0 ){
187614 sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex,iLevel));
187615 }
187616 }
187617 *ppStmt = pStmt;
187618 return rc;
187619}
187620
187621
187622/*
187623** Append a single varint to a PendingList buffer. SQLITE_OK is returned
187624** if successful, or an SQLite error code otherwise.
187625**
187626** This function also serves to allocate the PendingList structure itself.
187627** For example, to create a new PendingList structure containing two
187628** varints:
187629**
187630** PendingList *p = 0;
187631** fts3PendingListAppendVarint(&p, 1);
187632** fts3PendingListAppendVarint(&p, 2);
187633*/
187634static int fts3PendingListAppendVarint(
187635 PendingList **pp, /* IN/OUT: Pointer to PendingList struct */
187636 sqlite3_int64 i /* Value to append to data */
187637){
187638 PendingList *p = *pp;
187639
187640 /* Allocate or grow the PendingList as required. */
187641 if( !p ){
187642 p = sqlite3_malloc(sizeof(*p) + 100);
187643 if( !p ){
187644 return SQLITE_NOMEM7;
187645 }
187646 p->nSpace = 100;
187647 p->aData = (char *)&p[1];
187648 p->nData = 0;
187649 }
187650 else if( p->nData+FTS3_VARINT_MAX10+1>p->nSpace ){
187651 int nNew = p->nSpace * 2;
187652 p = sqlite3_realloc(p, sizeof(*p) + nNew);
187653 if( !p ){
187654 sqlite3_free(*pp);
187655 *pp = 0;
187656 return SQLITE_NOMEM7;
187657 }
187658 p->nSpace = nNew;
187659 p->aData = (char *)&p[1];
187660 }
187661
187662 /* Append the new serialized varint to the end of the list. */
187663 p->nData += sqlite3Fts3PutVarint(&p->aData[p->nData], i);
187664 p->aData[p->nData] = '\0';
187665 *pp = p;
187666 return SQLITE_OK0;
187667}
187668
187669/*
187670** Add a docid/column/position entry to a PendingList structure. Non-zero
187671** is returned if the structure is sqlite3_realloced as part of adding
187672** the entry. Otherwise, zero.
187673**
187674** If an OOM error occurs, *pRc is set to SQLITE_NOMEM before returning.
187675** Zero is always returned in this case. Otherwise, if no OOM error occurs,
187676** it is set to SQLITE_OK.
187677*/
187678static int fts3PendingListAppend(
187679 PendingList **pp, /* IN/OUT: PendingList structure */
187680 sqlite3_int64 iDocid, /* Docid for entry to add */
187681 sqlite3_int64 iCol, /* Column for entry to add */
187682 sqlite3_int64 iPos, /* Position of term for entry to add */
187683 int *pRc /* OUT: Return code */
187684){
187685 PendingList *p = *pp;
187686 int rc = SQLITE_OK0;
187687
187688 assert( !p || p->iLastDocid<=iDocid )((void) (0));
187689
187690 if( !p || p->iLastDocid!=iDocid ){
187691 u64 iDelta = (u64)iDocid - (u64)(p ? p->iLastDocid : 0);
187692 if( p ){
187693 assert( p->nData<p->nSpace )((void) (0));
187694 assert( p->aData[p->nData]==0 )((void) (0));
187695 p->nData++;
187696 }
187697 if( SQLITE_OK0!=(rc = fts3PendingListAppendVarint(&p, iDelta)) ){
187698 goto pendinglistappend_out;
187699 }
187700 p->iLastCol = -1;
187701 p->iLastPos = 0;
187702 p->iLastDocid = iDocid;
187703 }
187704 if( iCol>0 && p->iLastCol!=iCol ){
187705 if( SQLITE_OK0!=(rc = fts3PendingListAppendVarint(&p, 1))
187706 || SQLITE_OK0!=(rc = fts3PendingListAppendVarint(&p, iCol))
187707 ){
187708 goto pendinglistappend_out;
187709 }
187710 p->iLastCol = iCol;
187711 p->iLastPos = 0;
187712 }
187713 if( iCol>=0 ){
187714 assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) )((void) (0));
187715 rc = fts3PendingListAppendVarint(&p, 2+iPos-p->iLastPos);
187716 if( rc==SQLITE_OK0 ){
187717 p->iLastPos = iPos;
187718 }
187719 }
187720
187721 pendinglistappend_out:
187722 *pRc = rc;
187723 if( p!=*pp ){
187724 *pp = p;
187725 return 1;
187726 }
187727 return 0;
187728}
187729
187730/*
187731** Free a PendingList object allocated by fts3PendingListAppend().
187732*/
187733static void fts3PendingListDelete(PendingList *pList){
187734 sqlite3_free(pList);
187735}
187736
187737/*
187738** Add an entry to one of the pending-terms hash tables.
187739*/
187740static int fts3PendingTermsAddOne(
187741 Fts3Table *p,
187742 int iCol,
187743 int iPos,
187744 Fts3Hash *pHash, /* Pending terms hash table to add entry to */
187745 const char *zToken,
187746 int nToken
187747){
187748 PendingList *pList;
187749 int rc = SQLITE_OK0;
187750
187751 pList = (PendingList *)fts3HashFindsqlite3Fts3HashFind(pHash, zToken, nToken);
187752 if( pList ){
187753 p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem));
187754 }
187755 if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){
187756 if( pList==fts3HashInsertsqlite3Fts3HashInsert(pHash, zToken, nToken, pList) ){
187757 /* Malloc failed while inserting the new entry. This can only
187758 ** happen if there was no previous entry for this token.
187759 */
187760 assert( 0==fts3HashFind(pHash, zToken, nToken) )((void) (0));
187761 sqlite3_free(pList);
187762 rc = SQLITE_NOMEM7;
187763 }
187764 }
187765 if( rc==SQLITE_OK0 ){
187766 p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem));
187767 }
187768 return rc;
187769}
187770
187771/*
187772** Tokenize the nul-terminated string zText and add all tokens to the
187773** pending-terms hash-table. The docid used is that currently stored in
187774** p->iPrevDocid, and the column is specified by argument iCol.
187775**
187776** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
187777*/
187778static int fts3PendingTermsAdd(
187779 Fts3Table *p, /* Table into which text will be inserted */
187780 int iLangid, /* Language id to use */
187781 const char *zText, /* Text of document to be inserted */
187782 int iCol, /* Column into which text is being inserted */
187783 u32 *pnWord /* IN/OUT: Incr. by number tokens inserted */
187784){
187785 int rc;
187786 int iStart = 0;
187787 int iEnd = 0;
187788 int iPos = 0;
187789 int nWord = 0;
187790
187791 char const *zToken;
187792 int nToken = 0;
187793
187794 sqlite3_tokenizer *pTokenizer = p->pTokenizer;
187795 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
187796 sqlite3_tokenizer_cursor *pCsr;
187797 int (*xNext)(sqlite3_tokenizer_cursor *pCursor,
187798 const char**,int*,int*,int*,int*);
187799
187800 assert( pTokenizer && pModule )((void) (0));
187801
187802 /* If the user has inserted a NULL value, this function may be called with
187803 ** zText==0. In this case, add zero token entries to the hash table and
187804 ** return early. */
187805 if( zText==0 ){
187806 *pnWord = 0;
187807 return SQLITE_OK0;
187808 }
187809
187810 rc = sqlite3Fts3OpenTokenizer(pTokenizer, iLangid, zText, -1, &pCsr);
187811 if( rc!=SQLITE_OK0 ){
187812 return rc;
187813 }
187814
187815 xNext = pModule->xNext;
187816 while( SQLITE_OK0==rc
187817 && SQLITE_OK0==(rc = xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos))
187818 ){
187819 int i;
187820 if( iPos>=nWord ) nWord = iPos+1;
187821
187822 /* Positions cannot be negative; we use -1 as a terminator internally.
187823 ** Tokens must have a non-zero length.
187824 */
187825 if( iPos<0 || !zToken || nToken<=0 ){
187826 rc = SQLITE_ERROR1;
187827 break;
187828 }
187829
187830 /* Add the term to the terms index */
187831 rc = fts3PendingTermsAddOne(
187832 p, iCol, iPos, &p->aIndex[0].hPending, zToken, nToken
187833 );
187834
187835 /* Add the term to each of the prefix indexes that it is not too
187836 ** short for. */
187837 for(i=1; rc==SQLITE_OK0 && i<p->nIndex; i++){
187838 struct Fts3Index *pIndex = &p->aIndex[i];
187839 if( nToken<pIndex->nPrefix ) continue;
187840 rc = fts3PendingTermsAddOne(
187841 p, iCol, iPos, &pIndex->hPending, zToken, pIndex->nPrefix
187842 );
187843 }
187844 }
187845
187846 pModule->xClose(pCsr);
187847 *pnWord += nWord;
187848 return (rc==SQLITE_DONE101 ? SQLITE_OK0 : rc);
187849}
187850
187851/*
187852** Calling this function indicates that subsequent calls to
187853** fts3PendingTermsAdd() are to add term/position-list pairs for the
187854** contents of the document with docid iDocid.
187855*/
187856static int fts3PendingTermsDocid(
187857 Fts3Table *p, /* Full-text table handle */
187858 int bDelete, /* True if this op is a delete */
187859 int iLangid, /* Language id of row being written */
187860 sqlite_int64 iDocid /* Docid of row being written */
187861){
187862 assert( iLangid>=0 )((void) (0));
187863 assert( bDelete==1 || bDelete==0 )((void) (0));
187864
187865 /* TODO(shess) Explore whether partially flushing the buffer on
187866 ** forced-flush would provide better performance. I suspect that if
187867 ** we ordered the doclists by size and flushed the largest until the
187868 ** buffer was half empty, that would let the less frequent terms
187869 ** generate longer doclists.
187870 */
187871 if( iDocid<p->iPrevDocid
187872 || (iDocid==p->iPrevDocid && p->bPrevDelete==0)
187873 || p->iPrevLangid!=iLangid
187874 || p->nPendingData>p->nMaxPendingData
187875 ){
187876 int rc = sqlite3Fts3PendingTermsFlush(p);
187877 if( rc!=SQLITE_OK0 ) return rc;
187878 }
187879 p->iPrevDocid = iDocid;
187880 p->iPrevLangid = iLangid;
187881 p->bPrevDelete = bDelete;
187882 return SQLITE_OK0;
187883}
187884
187885/*
187886** Discard the contents of the pending-terms hash tables.
187887*/
187888SQLITE_PRIVATEstatic void sqlite3Fts3PendingTermsClear(Fts3Table *p){
187889 int i;
187890 for(i=0; i<p->nIndex; i++){
187891 Fts3HashElem *pElem;
187892 Fts3Hash *pHash = &p->aIndex[i].hPending;
187893 for(pElem=fts3HashFirst(pHash)((pHash)->first); pElem; pElem=fts3HashNext(pElem)((pElem)->next)){
187894 PendingList *pList = (PendingList *)fts3HashData(pElem)((pElem)->data);
187895 fts3PendingListDelete(pList);
187896 }
187897 fts3HashClearsqlite3Fts3HashClear(pHash);
187898 }
187899 p->nPendingData = 0;
187900}
187901
187902/*
187903** This function is called by the xUpdate() method as part of an INSERT
187904** operation. It adds entries for each term in the new record to the
187905** pendingTerms hash table.
187906**
187907** Argument apVal is the same as the similarly named argument passed to
187908** fts3InsertData(). Parameter iDocid is the docid of the new row.
187909*/
187910static int fts3InsertTerms(
187911 Fts3Table *p,
187912 int iLangid,
187913 sqlite3_value **apVal,
187914 u32 *aSz
187915){
187916 int i; /* Iterator variable */
187917 for(i=2; i<p->nColumn+2; i++){
187918 int iCol = i-2;
187919 if( p->abNotindexed[iCol]==0 ){
187920 const char *zText = (const char *)sqlite3_value_text(apVal[i]);
187921 int rc = fts3PendingTermsAdd(p, iLangid, zText, iCol, &aSz[iCol]);
187922 if( rc!=SQLITE_OK0 ){
187923 return rc;
187924 }
187925 aSz[p->nColumn] += sqlite3_value_bytes(apVal[i]);
187926 }
187927 }
187928 return SQLITE_OK0;
187929}
187930
187931/*
187932** This function is called by the xUpdate() method for an INSERT operation.
187933** The apVal parameter is passed a copy of the apVal argument passed by
187934** SQLite to the xUpdate() method. i.e:
187935**
187936** apVal[0] Not used for INSERT.
187937** apVal[1] rowid
187938** apVal[2] Left-most user-defined column
187939** ...
187940** apVal[p->nColumn+1] Right-most user-defined column
187941** apVal[p->nColumn+2] Hidden column with same name as table
187942** apVal[p->nColumn+3] Hidden "docid" column (alias for rowid)
187943** apVal[p->nColumn+4] Hidden languageid column
187944*/
187945static int fts3InsertData(
187946 Fts3Table *p, /* Full-text table */
187947 sqlite3_value **apVal, /* Array of values to insert */
187948 sqlite3_int64 *piDocid /* OUT: Docid for row just inserted */
187949){
187950 int rc; /* Return code */
187951 sqlite3_stmt *pContentInsert; /* INSERT INTO %_content VALUES(...) */
187952
187953 if( p->zContentTbl ){
187954 sqlite3_value *pRowid = apVal[p->nColumn+3];
187955 if( sqlite3_value_type(pRowid)==SQLITE_NULL5 ){
187956 pRowid = apVal[1];
187957 }
187958 if( sqlite3_value_type(pRowid)!=SQLITE_INTEGER1 ){
187959 return SQLITE_CONSTRAINT19;
187960 }
187961 *piDocid = sqlite3_value_int64(pRowid);
187962 return SQLITE_OK0;
187963 }
187964
187965 /* Locate the statement handle used to insert data into the %_content
187966 ** table. The SQL for this statement is:
187967 **
187968 ** INSERT INTO %_content VALUES(?, ?, ?, ...)
187969 **
187970 ** The statement features N '?' variables, where N is the number of user
187971 ** defined columns in the FTS3 table, plus one for the docid field.
187972 */
187973 rc = fts3SqlStmt(p, SQL_CONTENT_INSERT18, &pContentInsert, &apVal[1]);
187974 if( rc==SQLITE_OK0 && p->zLanguageid ){
187975 rc = sqlite3_bind_int(
187976 pContentInsert, p->nColumn+2,
187977 sqlite3_value_int(apVal[p->nColumn+4])
187978 );
187979 }
187980 if( rc!=SQLITE_OK0 ) return rc;
187981
187982 /* There is a quirk here. The users INSERT statement may have specified
187983 ** a value for the "rowid" field, for the "docid" field, or for both.
187984 ** Which is a problem, since "rowid" and "docid" are aliases for the
187985 ** same value. For example:
187986 **
187987 ** INSERT INTO fts3tbl(rowid, docid) VALUES(1, 2);
187988 **
187989 ** In FTS3, this is an error. It is an error to specify non-NULL values
187990 ** for both docid and some other rowid alias.
187991 */
187992 if( SQLITE_NULL5!=sqlite3_value_type(apVal[3+p->nColumn]) ){
187993 if( SQLITE_NULL5==sqlite3_value_type(apVal[0])
187994 && SQLITE_NULL5!=sqlite3_value_type(apVal[1])
187995 ){
187996 /* A rowid/docid conflict. */
187997 return SQLITE_ERROR1;
187998 }
187999 rc = sqlite3_bind_value(pContentInsert, 1, apVal[3+p->nColumn]);
188000 if( rc!=SQLITE_OK0 ) return rc;
188001 }
188002
188003 /* Execute the statement to insert the record. Set *piDocid to the
188004 ** new docid value.
188005 */
188006 sqlite3_step(pContentInsert);
188007 rc = sqlite3_reset(pContentInsert);
188008
188009 *piDocid = sqlite3_last_insert_rowid(p->db);
188010 return rc;
188011}
188012
188013
188014
188015/*
188016** Remove all data from the FTS3 table. Clear the hash table containing
188017** pending terms.
188018*/
188019static int fts3DeleteAll(Fts3Table *p, int bContent){
188020 int rc = SQLITE_OK0; /* Return code */
188021
188022 /* Discard the contents of the pending-terms hash table. */
188023 sqlite3Fts3PendingTermsClear(p);
188024
188025 /* Delete everything from the shadow tables. Except, leave %_content as
188026 ** is if bContent is false. */
188027 assert( p->zContentTbl==0 || bContent==0 )((void) (0));
188028 if( bContent ) fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT2, 0);
188029 fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGMENTS3, 0);
188030 fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR4, 0);
188031 if( p->bHasDocsize ){
188032 fts3SqlExec(&rc, p, SQL_DELETE_ALL_DOCSIZE5, 0);
188033 }
188034 if( p->bHasStat ){
188035 fts3SqlExec(&rc, p, SQL_DELETE_ALL_STAT6, 0);
188036 }
188037 return rc;
188038}
188039
188040/*
188041**
188042*/
188043static int langidFromSelect(Fts3Table *p, sqlite3_stmt *pSelect){
188044 int iLangid = 0;
188045 if( p->zLanguageid ) iLangid = sqlite3_column_int(pSelect, p->nColumn+1);
188046 return iLangid;
188047}
188048
188049/*
188050** The first element in the apVal[] array is assumed to contain the docid
188051** (an integer) of a row about to be deleted. Remove all terms from the
188052** full-text index.
188053*/
188054static void fts3DeleteTerms(
188055 int *pRC, /* Result code */
188056 Fts3Table *p, /* The FTS table to delete from */
188057 sqlite3_value *pRowid, /* The docid to be deleted */
188058 u32 *aSz, /* Sizes of deleted document written here */
188059 int *pbFound /* OUT: Set to true if row really does exist */
188060){
188061 int rc;
188062 sqlite3_stmt *pSelect;
188063
188064 assert( *pbFound==0 )((void) (0));
188065 if( *pRC ) return;
188066 rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID7, &pSelect, &pRowid);
188067 if( rc==SQLITE_OK0 ){
188068 if( SQLITE_ROW100==sqlite3_step(pSelect) ){
188069 int i;
188070 int iLangid = langidFromSelect(p, pSelect);
188071 i64 iDocid = sqlite3_column_int64(pSelect, 0);
188072 rc = fts3PendingTermsDocid(p, 1, iLangid, iDocid);
188073 for(i=1; rc==SQLITE_OK0 && i<=p->nColumn; i++){
188074 int iCol = i-1;
188075 if( p->abNotindexed[iCol]==0 ){
188076 const char *zText = (const char *)sqlite3_column_text(pSelect, i);
188077 rc = fts3PendingTermsAdd(p, iLangid, zText, -1, &aSz[iCol]);
188078 aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i);
188079 }
188080 }
188081 if( rc!=SQLITE_OK0 ){
188082 sqlite3_reset(pSelect);
188083 *pRC = rc;
188084 return;
188085 }
188086 *pbFound = 1;
188087 }
188088 rc = sqlite3_reset(pSelect);
188089 }else{
188090 sqlite3_reset(pSelect);
188091 }
188092 *pRC = rc;
188093}
188094
188095/*
188096** Forward declaration to account for the circular dependency between
188097** functions fts3SegmentMerge() and fts3AllocateSegdirIdx().
188098*/
188099static int fts3SegmentMerge(Fts3Table *, int, int, int);
188100
188101/*
188102** This function allocates a new level iLevel index in the segdir table.
188103** Usually, indexes are allocated within a level sequentially starting
188104** with 0, so the allocated index is one greater than the value returned
188105** by:
188106**
188107** SELECT max(idx) FROM %_segdir WHERE level = :iLevel
188108**
188109** However, if there are already FTS3_MERGE_COUNT indexes at the requested
188110** level, they are merged into a single level (iLevel+1) segment and the
188111** allocated index is 0.
188112**
188113** If successful, *piIdx is set to the allocated index slot and SQLITE_OK
188114** returned. Otherwise, an SQLite error code is returned.
188115*/
188116static int fts3AllocateSegdirIdx(
188117 Fts3Table *p,
188118 int iLangid, /* Language id */
188119 int iIndex, /* Index for p->aIndex */
188120 int iLevel,
188121 int *piIdx
188122){
188123 int rc; /* Return Code */
188124 sqlite3_stmt *pNextIdx; /* Query for next idx at level iLevel */
188125 int iNext = 0; /* Result of query pNextIdx */
188126
188127 assert( iLangid>=0 )((void) (0));
188128 assert( p->nIndex>=1 )((void) (0));
188129
188130 /* Set variable iNext to the next available segdir index at level iLevel. */
188131 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX8, &pNextIdx, 0);
188132 if( rc==SQLITE_OK0 ){
188133 sqlite3_bind_int64(
188134 pNextIdx, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
188135 );
188136 if( SQLITE_ROW100==sqlite3_step(pNextIdx) ){
188137 iNext = sqlite3_column_int(pNextIdx, 0);
188138 }
188139 rc = sqlite3_reset(pNextIdx);
188140 }
188141
188142 if( rc==SQLITE_OK0 ){
188143 /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already
188144 ** full, merge all segments in level iLevel into a single iLevel+1
188145 ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
188146 ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
188147 */
188148 if( iNext>=MergeCount(p)16 ){
188149 fts3LogMerge(16, getAbsoluteLevel(p, iLangid, iIndex, iLevel));
188150 rc = fts3SegmentMerge(p, iLangid, iIndex, iLevel);
188151 *piIdx = 0;
188152 }else{
188153 *piIdx = iNext;
188154 }
188155 }
188156
188157 return rc;
188158}
188159
188160/*
188161** The %_segments table is declared as follows:
188162**
188163** CREATE TABLE %_segments(blockid INTEGER PRIMARY KEY, block BLOB)
188164**
188165** This function reads data from a single row of the %_segments table. The
188166** specific row is identified by the iBlockid parameter. If paBlob is not
188167** NULL, then a buffer is allocated using sqlite3_malloc() and populated
188168** with the contents of the blob stored in the "block" column of the
188169** identified table row is. Whether or not paBlob is NULL, *pnBlob is set
188170** to the size of the blob in bytes before returning.
188171**
188172** If an error occurs, or the table does not contain the specified row,
188173** an SQLite error code is returned. Otherwise, SQLITE_OK is returned. If
188174** paBlob is non-NULL, then it is the responsibility of the caller to
188175** eventually free the returned buffer.
188176**
188177** This function may leave an open sqlite3_blob* handle in the
188178** Fts3Table.pSegments variable. This handle is reused by subsequent calls
188179** to this function. The handle may be closed by calling the
188180** sqlite3Fts3SegmentsClose() function. Reusing a blob handle is a handy
188181** performance improvement, but the blob handle should always be closed
188182** before control is returned to the user (to prevent a lock being held
188183** on the database file for longer than necessary). Thus, any virtual table
188184** method (xFilter etc.) that may directly or indirectly call this function
188185** must call sqlite3Fts3SegmentsClose() before returning.
188186*/
188187SQLITE_PRIVATEstatic int sqlite3Fts3ReadBlock(
188188 Fts3Table *p, /* FTS3 table handle */
188189 sqlite3_int64 iBlockid, /* Access the row with blockid=$iBlockid */
188190 char **paBlob, /* OUT: Blob data in malloc'd buffer */
188191 int *pnBlob, /* OUT: Size of blob data */
188192 int *pnLoad /* OUT: Bytes actually loaded */
188193){
188194 int rc; /* Return code */
188195
188196 /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
188197 assert( pnBlob )((void) (0));
188198
188199 if( p->pSegments ){
188200 rc = sqlite3_blob_reopen(p->pSegments, iBlockid);
188201 }else{
188202 if( 0==p->zSegmentsTbl ){
188203 p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName);
188204 if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM7;
188205 }
188206 rc = sqlite3_blob_open(
188207 p->db, p->zDb, p->zSegmentsTbl, "block", iBlockid, 0, &p->pSegments
188208 );
188209 }
188210
188211 if( rc==SQLITE_OK0 ){
188212 int nByte = sqlite3_blob_bytes(p->pSegments);
188213 *pnBlob = nByte;
188214 if( paBlob ){
188215 char *aByte = sqlite3_malloc(nByte + FTS3_NODE_PADDING(10*2));
188216 if( !aByte ){
188217 rc = SQLITE_NOMEM7;
188218 }else{
188219 if( pnLoad && nByte>(FTS3_NODE_CHUNK_THRESHOLD((4*1024)*4)) ){
188220 nByte = FTS3_NODE_CHUNKSIZE(4*1024);
188221 *pnLoad = nByte;
188222 }
188223 rc = sqlite3_blob_read(p->pSegments, aByte, nByte, 0);
188224 memset(&aByte[nByte], 0, FTS3_NODE_PADDING(10*2));
188225 if( rc!=SQLITE_OK0 ){
188226 sqlite3_free(aByte);
188227 aByte = 0;
188228 }
188229 }
188230 *paBlob = aByte;
188231 }
188232 }else if( rc==SQLITE_ERROR1 ){
188233 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
188234 }
188235
188236 return rc;
188237}
188238
188239/*
188240** Close the blob handle at p->pSegments, if it is open. See comments above
188241** the sqlite3Fts3ReadBlock() function for details.
188242*/
188243SQLITE_PRIVATEstatic void sqlite3Fts3SegmentsClose(Fts3Table *p){
188244 sqlite3_blob_close(p->pSegments);
188245 p->pSegments = 0;
188246}
188247
188248static int fts3SegReaderIncrRead(Fts3SegReader *pReader){
188249 int nRead; /* Number of bytes to read */
188250 int rc; /* Return code */
188251
188252 nRead = MIN(pReader->nNode - pReader->nPopulate, FTS3_NODE_CHUNKSIZE)((pReader->nNode - pReader->nPopulate)<((4*1024))?(pReader
->nNode - pReader->nPopulate):((4*1024)))
;
188253 rc = sqlite3_blob_read(
188254 pReader->pBlob,
188255 &pReader->aNode[pReader->nPopulate],
188256 nRead,
188257 pReader->nPopulate
188258 );
188259
188260 if( rc==SQLITE_OK0 ){
188261 pReader->nPopulate += nRead;
188262 memset(&pReader->aNode[pReader->nPopulate], 0, FTS3_NODE_PADDING(10*2));
188263 if( pReader->nPopulate==pReader->nNode ){
188264 sqlite3_blob_close(pReader->pBlob);
188265 pReader->pBlob = 0;
188266 pReader->nPopulate = 0;
188267 }
188268 }
188269 return rc;
188270}
188271
188272static int fts3SegReaderRequire(Fts3SegReader *pReader, char *pFrom, int nByte){
188273 int rc = SQLITE_OK0;
188274 assert( !pReader->pBlob((void) (0))
188275 || (pFrom>=pReader->aNode && pFrom<&pReader->aNode[pReader->nNode])((void) (0))
188276 )((void) (0));
188277 while( pReader->pBlob && rc==SQLITE_OK0
188278 && (pFrom - pReader->aNode + nByte)>pReader->nPopulate
188279 ){
188280 rc = fts3SegReaderIncrRead(pReader);
188281 }
188282 return rc;
188283}
188284
188285/*
188286** Set an Fts3SegReader cursor to point at EOF.
188287*/
188288static void fts3SegReaderSetEof(Fts3SegReader *pSeg){
188289 if( !fts3SegReaderIsRootOnly(pSeg)((pSeg)->rootOnly!=0) ){
188290 sqlite3_free(pSeg->aNode);
188291 sqlite3_blob_close(pSeg->pBlob);
188292 pSeg->pBlob = 0;
188293 }
188294 pSeg->aNode = 0;
188295}
188296
188297/*
188298** Move the iterator passed as the first argument to the next term in the
188299** segment. If successful, SQLITE_OK is returned. If there is no next term,
188300** SQLITE_DONE. Otherwise, an SQLite error code.
188301*/
188302static int fts3SegReaderNext(
188303 Fts3Table *p,
188304 Fts3SegReader *pReader,
188305 int bIncr
188306){
188307 int rc; /* Return code of various sub-routines */
188308 char *pNext; /* Cursor variable */
188309 int nPrefix; /* Number of bytes in term prefix */
188310 int nSuffix; /* Number of bytes in term suffix */
188311
188312 if( !pReader->aDoclist ){
188313 pNext = pReader->aNode;
188314 }else{
188315 pNext = &pReader->aDoclist[pReader->nDoclist];
188316 }
188317
188318 if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){
188319
188320 if( fts3SegReaderIsPending(pReader)((pReader)->ppNextElem!=0) ){
188321 Fts3HashElem *pElem = *(pReader->ppNextElem);
188322 sqlite3_free(pReader->aNode);
188323 pReader->aNode = 0;
188324 if( pElem ){
188325 char *aCopy;
188326 PendingList *pList = (PendingList *)fts3HashData(pElem)((pElem)->data);
188327 int nCopy = pList->nData+1;
188328
188329 int nTerm = fts3HashKeysize(pElem)((pElem)->nKey);
188330 if( (nTerm+1)>pReader->nTermAlloc ){
188331 sqlite3_free(pReader->zTerm);
188332 pReader->zTerm = (char*)sqlite3_malloc((nTerm+1)*2);
188333 if( !pReader->zTerm ) return SQLITE_NOMEM7;
188334 pReader->nTermAlloc = (nTerm+1)*2;
188335 }
188336 memcpy(pReader->zTerm, fts3HashKey(pElem)((pElem)->pKey), nTerm);
188337 pReader->zTerm[nTerm] = '\0';
188338 pReader->nTerm = nTerm;
188339
188340 aCopy = (char*)sqlite3_malloc(nCopy);
188341 if( !aCopy ) return SQLITE_NOMEM7;
188342 memcpy(aCopy, pList->aData, nCopy);
188343 pReader->nNode = pReader->nDoclist = nCopy;
188344 pReader->aNode = pReader->aDoclist = aCopy;
188345 pReader->ppNextElem++;
188346 assert( pReader->aNode )((void) (0));
188347 }
188348 return SQLITE_OK0;
188349 }
188350
188351 fts3SegReaderSetEof(pReader);
188352
188353 /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf
188354 ** blocks have already been traversed. */
188355#ifdef CORRUPT_DB(sqlite3Config.neverCorrupt==0)
188356 assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock || CORRUPT_DB )((void) (0));
188357#endif
188358 if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
188359 return SQLITE_OK0;
188360 }
188361
188362 rc = sqlite3Fts3ReadBlock(
188363 p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode,
188364 (bIncr ? &pReader->nPopulate : 0)
188365 );
188366 if( rc!=SQLITE_OK0 ) return rc;
188367 assert( pReader->pBlob==0 )((void) (0));
188368 if( bIncr && pReader->nPopulate<pReader->nNode ){
188369 pReader->pBlob = p->pSegments;
188370 p->pSegments = 0;
188371 }
188372 pNext = pReader->aNode;
188373 }
188374
188375 assert( !fts3SegReaderIsPending(pReader) )((void) (0));
188376
188377 rc = fts3SegReaderRequire(pReader, pNext, FTS3_VARINT_MAX10*2);
188378 if( rc!=SQLITE_OK0 ) return rc;
188379
188380 /* Because of the FTS3_NODE_PADDING bytes of padding, the following is
188381 ** safe (no risk of overread) even if the node data is corrupted. */
188382 pNext += fts3GetVarint32(pNext, &nPrefix)( (*(u8*)(pNext)&0x80) ? sqlite3Fts3GetVarint32(pNext, &
nPrefix) : (*&nPrefix=*(u8*)(pNext), 1) )
;
188383 pNext += fts3GetVarint32(pNext, &nSuffix)( (*(u8*)(pNext)&0x80) ? sqlite3Fts3GetVarint32(pNext, &
nSuffix) : (*&nSuffix=*(u8*)(pNext), 1) )
;
188384 if( nSuffix<=0
188385 || (&pReader->aNode[pReader->nNode] - pNext)<nSuffix
188386 || nPrefix>pReader->nTerm
188387 ){
188388 return FTS_CORRUPT_VTAB(11 | (1<<8));
188389 }
188390
188391 /* Both nPrefix and nSuffix were read by fts3GetVarint32() and so are
188392 ** between 0 and 0x7FFFFFFF. But the sum of the two may cause integer
188393 ** overflow - hence the (i64) casts. */
188394 if( (i64)nPrefix+nSuffix>(i64)pReader->nTermAlloc ){
188395 i64 nNew = ((i64)nPrefix+nSuffix)*2;
188396 char *zNew = sqlite3_realloc64(pReader->zTerm, nNew);
188397 if( !zNew ){
188398 return SQLITE_NOMEM7;
188399 }
188400 pReader->zTerm = zNew;
188401 pReader->nTermAlloc = nNew;
188402 }
188403
188404 rc = fts3SegReaderRequire(pReader, pNext, nSuffix+FTS3_VARINT_MAX10);
188405 if( rc!=SQLITE_OK0 ) return rc;
188406
188407 memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix);
188408 pReader->nTerm = nPrefix+nSuffix;
188409 pNext += nSuffix;
188410 pNext += fts3GetVarint32(pNext, &pReader->nDoclist)( (*(u8*)(pNext)&0x80) ? sqlite3Fts3GetVarint32(pNext, &
pReader->nDoclist) : (*&pReader->nDoclist=*(u8*)(pNext
), 1) )
;
188411 pReader->aDoclist = pNext;
188412 pReader->pOffsetList = 0;
188413
188414 /* Check that the doclist does not appear to extend past the end of the
188415 ** b-tree node. And that the final byte of the doclist is 0x00. If either
188416 ** of these statements is untrue, then the data structure is corrupt.
188417 */
188418 if( pReader->nDoclist > pReader->nNode-(pReader->aDoclist-pReader->aNode)
188419 || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
188420 || pReader->nDoclist==0
188421 ){
188422 return FTS_CORRUPT_VTAB(11 | (1<<8));
188423 }
188424 return SQLITE_OK0;
188425}
188426
188427/*
188428** Set the SegReader to point to the first docid in the doclist associated
188429** with the current term.
188430*/
188431static int fts3SegReaderFirstDocid(Fts3Table *pTab, Fts3SegReader *pReader){
188432 int rc = SQLITE_OK0;
188433 assert( pReader->aDoclist )((void) (0));
188434 assert( !pReader->pOffsetList )((void) (0));
188435 if( pTab->bDescIdx && fts3SegReaderIsPending(pReader)((pReader)->ppNextElem!=0) ){
188436 u8 bEof = 0;
188437 pReader->iDocid = 0;
188438 pReader->nOffsetList = 0;
188439 sqlite3Fts3DoclistPrev(0,
188440 pReader->aDoclist, pReader->nDoclist, &pReader->pOffsetList,
188441 &pReader->iDocid, &pReader->nOffsetList, &bEof
188442 );
188443 }else{
188444 rc = fts3SegReaderRequire(pReader, pReader->aDoclist, FTS3_VARINT_MAX10);
188445 if( rc==SQLITE_OK0 ){
188446 int n = sqlite3Fts3GetVarint(pReader->aDoclist, &pReader->iDocid);
188447 pReader->pOffsetList = &pReader->aDoclist[n];
188448 }
188449 }
188450 return rc;
188451}
188452
188453/*
188454** Advance the SegReader to point to the next docid in the doclist
188455** associated with the current term.
188456**
188457** If arguments ppOffsetList and pnOffsetList are not NULL, then
188458** *ppOffsetList is set to point to the first column-offset list
188459** in the doclist entry (i.e. immediately past the docid varint).
188460** *pnOffsetList is set to the length of the set of column-offset
188461** lists, not including the nul-terminator byte. For example:
188462*/
188463static int fts3SegReaderNextDocid(
188464 Fts3Table *pTab,
188465 Fts3SegReader *pReader, /* Reader to advance to next docid */
188466 char **ppOffsetList, /* OUT: Pointer to current position-list */
188467 int *pnOffsetList /* OUT: Length of *ppOffsetList in bytes */
188468){
188469 int rc = SQLITE_OK0;
188470 char *p = pReader->pOffsetList;
188471 char c = 0;
188472
188473 assert( p )((void) (0));
188474
188475 if( pTab->bDescIdx && fts3SegReaderIsPending(pReader)((pReader)->ppNextElem!=0) ){
188476 /* A pending-terms seg-reader for an FTS4 table that uses order=desc.
188477 ** Pending-terms doclists are always built up in ascending order, so
188478 ** we have to iterate through them backwards here. */
188479 u8 bEof = 0;
188480 if( ppOffsetList ){
188481 *ppOffsetList = pReader->pOffsetList;
188482 *pnOffsetList = pReader->nOffsetList - 1;
188483 }
188484 sqlite3Fts3DoclistPrev(0,
188485 pReader->aDoclist, pReader->nDoclist, &p, &pReader->iDocid,
188486 &pReader->nOffsetList, &bEof
188487 );
188488 if( bEof ){
188489 pReader->pOffsetList = 0;
188490 }else{
188491 pReader->pOffsetList = p;
188492 }
188493 }else{
188494 char *pEnd = &pReader->aDoclist[pReader->nDoclist];
188495
188496 /* Pointer p currently points at the first byte of an offset list. The
188497 ** following block advances it to point one byte past the end of
188498 ** the same offset list. */
188499 while( 1 ){
188500
188501 /* The following line of code (and the "p++" below the while() loop) is
188502 ** normally all that is required to move pointer p to the desired
188503 ** position. The exception is if this node is being loaded from disk
188504 ** incrementally and pointer "p" now points to the first byte past
188505 ** the populated part of pReader->aNode[].
188506 */
188507 while( *p | c ) c = *p++ & 0x80;
188508 assert( *p==0 )((void) (0));
188509
188510 if( pReader->pBlob==0 || p<&pReader->aNode[pReader->nPopulate] ) break;
188511 rc = fts3SegReaderIncrRead(pReader);
188512 if( rc!=SQLITE_OK0 ) return rc;
188513 }
188514 p++;
188515
188516 /* If required, populate the output variables with a pointer to and the
188517 ** size of the previous offset-list.
188518 */
188519 if( ppOffsetList ){
188520 *ppOffsetList = pReader->pOffsetList;
188521 *pnOffsetList = (int)(p - pReader->pOffsetList - 1);
188522 }
188523
188524 /* List may have been edited in place by fts3EvalNearTrim() */
188525 while( p<pEnd && *p==0 ) p++;
188526
188527 /* If there are no more entries in the doclist, set pOffsetList to
188528 ** NULL. Otherwise, set Fts3SegReader.iDocid to the next docid and
188529 ** Fts3SegReader.pOffsetList to point to the next offset list before
188530 ** returning.
188531 */
188532 if( p>=pEnd ){
188533 pReader->pOffsetList = 0;
188534 }else{
188535 rc = fts3SegReaderRequire(pReader, p, FTS3_VARINT_MAX10);
188536 if( rc==SQLITE_OK0 ){
188537 u64 iDelta;
188538 pReader->pOffsetList = p + sqlite3Fts3GetVarintU(p, &iDelta);
188539 if( pTab->bDescIdx ){
188540 pReader->iDocid = (i64)((u64)pReader->iDocid - iDelta);
188541 }else{
188542 pReader->iDocid = (i64)((u64)pReader->iDocid + iDelta);
188543 }
188544 }
188545 }
188546 }
188547
188548 return rc;
188549}
188550
188551
188552SQLITE_PRIVATEstatic int sqlite3Fts3MsrOvfl(
188553 Fts3Cursor *pCsr,
188554 Fts3MultiSegReader *pMsr,
188555 int *pnOvfl
188556){
188557 Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
188558 int nOvfl = 0;
188559 int ii;
188560 int rc = SQLITE_OK0;
188561 int pgsz = p->nPgsz;
188562
188563 assert( p->bFts4 )((void) (0));
188564 assert( pgsz>0 )((void) (0));
188565
188566 for(ii=0; rc==SQLITE_OK0 && ii<pMsr->nSegment; ii++){
188567 Fts3SegReader *pReader = pMsr->apSegment[ii];
188568 if( !fts3SegReaderIsPending(pReader)((pReader)->ppNextElem!=0)
188569 && !fts3SegReaderIsRootOnly(pReader)((pReader)->rootOnly!=0)
188570 ){
188571 sqlite3_int64 jj;
188572 for(jj=pReader->iStartBlock; jj<=pReader->iLeafEndBlock; jj++){
188573 int nBlob;
188574 rc = sqlite3Fts3ReadBlock(p, jj, 0, &nBlob, 0);
188575 if( rc!=SQLITE_OK0 ) break;
188576 if( (nBlob+35)>pgsz ){
188577 nOvfl += (nBlob + 34)/pgsz;
188578 }
188579 }
188580 }
188581 }
188582 *pnOvfl = nOvfl;
188583 return rc;
188584}
188585
188586/*
188587** Free all allocations associated with the iterator passed as the
188588** second argument.
188589*/
188590SQLITE_PRIVATEstatic void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){
188591 if( pReader ){
188592 sqlite3_free(pReader->zTerm);
188593 if( !fts3SegReaderIsRootOnly(pReader)((pReader)->rootOnly!=0) ){
188594 sqlite3_free(pReader->aNode);
188595 }
188596 sqlite3_blob_close(pReader->pBlob);
188597 }
188598 sqlite3_free(pReader);
188599}
188600
188601/*
188602** Allocate a new SegReader object.
188603*/
188604SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderNew(
188605 int iAge, /* Segment "age". */
188606 int bLookup, /* True for a lookup only */
188607 sqlite3_int64 iStartLeaf, /* First leaf to traverse */
188608 sqlite3_int64 iEndLeaf, /* Final leaf to traverse */
188609 sqlite3_int64 iEndBlock, /* Final block of segment */
188610 const char *zRoot, /* Buffer containing root node */
188611 int nRoot, /* Size of buffer containing root node */
188612 Fts3SegReader **ppReader /* OUT: Allocated Fts3SegReader */
188613){
188614 Fts3SegReader *pReader; /* Newly allocated SegReader object */
188615 int nExtra = 0; /* Bytes to allocate segment root node */
188616
188617 assert( zRoot!=0 || nRoot==0 )((void) (0));
188618#ifdef CORRUPT_DB(sqlite3Config.neverCorrupt==0)
188619 assert( zRoot!=0 || CORRUPT_DB )((void) (0));
188620#endif
188621
188622 if( iStartLeaf==0 ){
188623 if( iEndLeaf!=0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
188624 nExtra = nRoot + FTS3_NODE_PADDING(10*2);
188625 }
188626
188627 pReader = (Fts3SegReader *)sqlite3_malloc(sizeof(Fts3SegReader) + nExtra);
188628 if( !pReader ){
188629 return SQLITE_NOMEM7;
188630 }
188631 memset(pReader, 0, sizeof(Fts3SegReader));
188632 pReader->iIdx = iAge;
188633 pReader->bLookup = bLookup!=0;
188634 pReader->iStartBlock = iStartLeaf;
188635 pReader->iLeafEndBlock = iEndLeaf;
188636 pReader->iEndBlock = iEndBlock;
188637
188638 if( nExtra ){
188639 /* The entire segment is stored in the root node. */
188640 pReader->aNode = (char *)&pReader[1];
188641 pReader->rootOnly = 1;
188642 pReader->nNode = nRoot;
188643 if( nRoot ) memcpy(pReader->aNode, zRoot, nRoot);
188644 memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING(10*2));
188645 }else{
188646 pReader->iCurrentBlock = iStartLeaf-1;
188647 }
188648 *ppReader = pReader;
188649 return SQLITE_OK0;
188650}
188651
188652/*
188653** This is a comparison function used as a qsort() callback when sorting
188654** an array of pending terms by term. This occurs as part of flushing
188655** the contents of the pending-terms hash table to the database.
188656*/
188657static int SQLITE_CDECL fts3CompareElemByTerm(
188658 const void *lhs,
188659 const void *rhs
188660){
188661 char *z1 = fts3HashKey(*(Fts3HashElem **)lhs)((*(Fts3HashElem **)lhs)->pKey);
188662 char *z2 = fts3HashKey(*(Fts3HashElem **)rhs)((*(Fts3HashElem **)rhs)->pKey);
188663 int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs)((*(Fts3HashElem **)lhs)->nKey);
188664 int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs)((*(Fts3HashElem **)rhs)->nKey);
188665
188666 int n = (n1<n2 ? n1 : n2);
188667 int c = memcmp(z1, z2, n);
188668 if( c==0 ){
188669 c = n1 - n2;
188670 }
188671 return c;
188672}
188673
188674/*
188675** This function is used to allocate an Fts3SegReader that iterates through
188676** a subset of the terms stored in the Fts3Table.pendingTerms array.
188677**
188678** If the isPrefixIter parameter is zero, then the returned SegReader iterates
188679** through each term in the pending-terms table. Or, if isPrefixIter is
188680** non-zero, it iterates through each term and its prefixes. For example, if
188681** the pending terms hash table contains the terms "sqlite", "mysql" and
188682** "firebird", then the iterator visits the following 'terms' (in the order
188683** shown):
188684**
188685** f fi fir fire fireb firebi firebir firebird
188686** m my mys mysq mysql
188687** s sq sql sqli sqlit sqlite
188688**
188689** Whereas if isPrefixIter is zero, the terms visited are:
188690**
188691** firebird mysql sqlite
188692*/
188693SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderPending(
188694 Fts3Table *p, /* Virtual table handle */
188695 int iIndex, /* Index for p->aIndex */
188696 const char *zTerm, /* Term to search for */
188697 int nTerm, /* Size of buffer zTerm */
188698 int bPrefix, /* True for a prefix iterator */
188699 Fts3SegReader **ppReader /* OUT: SegReader for pending-terms */
188700){
188701 Fts3SegReader *pReader = 0; /* Fts3SegReader object to return */
188702 Fts3HashElem *pE; /* Iterator variable */
188703 Fts3HashElem **aElem = 0; /* Array of term hash entries to scan */
188704 int nElem = 0; /* Size of array at aElem */
188705 int rc = SQLITE_OK0; /* Return Code */
188706 Fts3Hash *pHash;
188707
188708 pHash = &p->aIndex[iIndex].hPending;
188709 if( bPrefix ){
188710 int nAlloc = 0; /* Size of allocated array at aElem */
188711
188712 for(pE=fts3HashFirst(pHash)((pHash)->first); pE; pE=fts3HashNext(pE)((pE)->next)){
188713 char *zKey = (char *)fts3HashKey(pE)((pE)->pKey);
188714 int nKey = fts3HashKeysize(pE)((pE)->nKey);
188715 if( nTerm==0 || (nKey>=nTerm && 0==memcmp(zKey, zTerm, nTerm)) ){
188716 if( nElem==nAlloc ){
188717 Fts3HashElem **aElem2;
188718 nAlloc += 16;
188719 aElem2 = (Fts3HashElem **)sqlite3_realloc(
188720 aElem, nAlloc*sizeof(Fts3HashElem *)
188721 );
188722 if( !aElem2 ){
188723 rc = SQLITE_NOMEM7;
188724 nElem = 0;
188725 break;
188726 }
188727 aElem = aElem2;
188728 }
188729
188730 aElem[nElem++] = pE;
188731 }
188732 }
188733
188734 /* If more than one term matches the prefix, sort the Fts3HashElem
188735 ** objects in term order using qsort(). This uses the same comparison
188736 ** callback as is used when flushing terms to disk.
188737 */
188738 if( nElem>1 ){
188739 qsort(aElem, nElem, sizeof(Fts3HashElem *), fts3CompareElemByTerm);
188740 }
188741
188742 }else{
188743 /* The query is a simple term lookup that matches at most one term in
188744 ** the index. All that is required is a straight hash-lookup.
188745 **
188746 ** Because the stack address of pE may be accessed via the aElem pointer
188747 ** below, the "Fts3HashElem *pE" must be declared so that it is valid
188748 ** within this entire function, not just this "else{...}" block.
188749 */
188750 pE = fts3HashFindElemsqlite3Fts3HashFindElem(pHash, zTerm, nTerm);
188751 if( pE ){
188752 aElem = &pE;
188753 nElem = 1;
188754 }
188755 }
188756
188757 if( nElem>0 ){
188758 sqlite3_int64 nByte;
188759 nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *);
188760 pReader = (Fts3SegReader *)sqlite3_malloc64(nByte);
188761 if( !pReader ){
188762 rc = SQLITE_NOMEM7;
188763 }else{
188764 memset(pReader, 0, nByte);
188765 pReader->iIdx = 0x7FFFFFFF;
188766 pReader->ppNextElem = (Fts3HashElem **)&pReader[1];
188767 memcpy(pReader->ppNextElem, aElem, nElem*sizeof(Fts3HashElem *));
188768 }
188769 }
188770
188771 if( bPrefix ){
188772 sqlite3_free(aElem);
188773 }
188774 *ppReader = pReader;
188775 return rc;
188776}
188777
188778/*
188779** Compare the entries pointed to by two Fts3SegReader structures.
188780** Comparison is as follows:
188781**
188782** 1) EOF is greater than not EOF.
188783**
188784** 2) The current terms (if any) are compared using memcmp(). If one
188785** term is a prefix of another, the longer term is considered the
188786** larger.
188787**
188788** 3) By segment age. An older segment is considered larger.
188789*/
188790static int fts3SegReaderCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
188791 int rc;
188792 if( pLhs->aNode && pRhs->aNode ){
188793 int rc2 = pLhs->nTerm - pRhs->nTerm;
188794 if( rc2<0 ){
188795 rc = memcmp(pLhs->zTerm, pRhs->zTerm, pLhs->nTerm);
188796 }else{
188797 rc = memcmp(pLhs->zTerm, pRhs->zTerm, pRhs->nTerm);
188798 }
188799 if( rc==0 ){
188800 rc = rc2;
188801 }
188802 }else{
188803 rc = (pLhs->aNode==0) - (pRhs->aNode==0);
188804 }
188805 if( rc==0 ){
188806 rc = pRhs->iIdx - pLhs->iIdx;
188807 }
188808 assert_fts3_nc( rc!=0 )((void) (0));
188809 return rc;
188810}
188811
188812/*
188813** A different comparison function for SegReader structures. In this
188814** version, it is assumed that each SegReader points to an entry in
188815** a doclist for identical terms. Comparison is made as follows:
188816**
188817** 1) EOF (end of doclist in this case) is greater than not EOF.
188818**
188819** 2) By current docid.
188820**
188821** 3) By segment age. An older segment is considered larger.
188822*/
188823static int fts3SegReaderDoclistCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
188824 int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
188825 if( rc==0 ){
188826 if( pLhs->iDocid==pRhs->iDocid ){
188827 rc = pRhs->iIdx - pLhs->iIdx;
188828 }else{
188829 rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1;
188830 }
188831 }
188832 assert( pLhs->aNode && pRhs->aNode )((void) (0));
188833 return rc;
188834}
188835static int fts3SegReaderDoclistCmpRev(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
188836 int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
188837 if( rc==0 ){
188838 if( pLhs->iDocid==pRhs->iDocid ){
188839 rc = pRhs->iIdx - pLhs->iIdx;
188840 }else{
188841 rc = (pLhs->iDocid < pRhs->iDocid) ? 1 : -1;
188842 }
188843 }
188844 assert( pLhs->aNode && pRhs->aNode )((void) (0));
188845 return rc;
188846}
188847
188848/*
188849** Compare the term that the Fts3SegReader object passed as the first argument
188850** points to with the term specified by arguments zTerm and nTerm.
188851**
188852** If the pSeg iterator is already at EOF, return 0. Otherwise, return
188853** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are
188854** equal, or +ve if the pSeg term is greater than zTerm/nTerm.
188855*/
188856static int fts3SegReaderTermCmp(
188857 Fts3SegReader *pSeg, /* Segment reader object */
188858 const char *zTerm, /* Term to compare to */
188859 int nTerm /* Size of term zTerm in bytes */
188860){
188861 int res = 0;
188862 if( pSeg->aNode ){
188863 if( pSeg->nTerm>nTerm ){
188864 res = memcmp(pSeg->zTerm, zTerm, nTerm);
188865 }else{
188866 res = memcmp(pSeg->zTerm, zTerm, pSeg->nTerm);
188867 }
188868 if( res==0 ){
188869 res = pSeg->nTerm-nTerm;
188870 }
188871 }
188872 return res;
188873}
188874
188875/*
188876** Argument apSegment is an array of nSegment elements. It is known that
188877** the final (nSegment-nSuspect) members are already in sorted order
188878** (according to the comparison function provided). This function shuffles
188879** the array around until all entries are in sorted order.
188880*/
188881static void fts3SegReaderSort(
188882 Fts3SegReader **apSegment, /* Array to sort entries of */
188883 int nSegment, /* Size of apSegment array */
188884 int nSuspect, /* Unsorted entry count */
188885 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) /* Comparison function */
188886){
188887 int i; /* Iterator variable */
188888
188889 assert( nSuspect<=nSegment )((void) (0));
188890
188891 if( nSuspect==nSegment ) nSuspect--;
188892 for(i=nSuspect-1; i>=0; i--){
188893 int j;
188894 for(j=i; j<(nSegment-1); j++){
188895 Fts3SegReader *pTmp;
188896 if( xCmp(apSegment[j], apSegment[j+1])<0 ) break;
188897 pTmp = apSegment[j+1];
188898 apSegment[j+1] = apSegment[j];
188899 apSegment[j] = pTmp;
188900 }
188901 }
188902
188903#ifndef NDEBUG1
188904 /* Check that the list really is sorted now. */
188905 for(i=0; i<(nSuspect-1); i++){
188906 assert( xCmp(apSegment[i], apSegment[i+1])<0 )((void) (0));
188907 }
188908#endif
188909}
188910
188911/*
188912** Insert a record into the %_segments table.
188913*/
188914static int fts3WriteSegment(
188915 Fts3Table *p, /* Virtual table handle */
188916 sqlite3_int64 iBlock, /* Block id for new block */
188917 char *z, /* Pointer to buffer containing block data */
188918 int n /* Size of buffer z in bytes */
188919){
188920 sqlite3_stmt *pStmt;
188921 int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS9, &pStmt, 0);
188922 if( rc==SQLITE_OK0 ){
188923 sqlite3_bind_int64(pStmt, 1, iBlock);
188924 sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC((sqlite3_destructor_type)0));
188925 sqlite3_step(pStmt);
188926 rc = sqlite3_reset(pStmt);
188927 sqlite3_bind_null(pStmt, 2);
188928 }
188929 return rc;
188930}
188931
188932/*
188933** Find the largest relative level number in the table. If successful, set
188934** *pnMax to this value and return SQLITE_OK. Otherwise, if an error occurs,
188935** set *pnMax to zero and return an SQLite error code.
188936*/
188937SQLITE_PRIVATEstatic int sqlite3Fts3MaxLevel(Fts3Table *p, int *pnMax){
188938 int rc;
188939 int mxLevel = 0;
188940 sqlite3_stmt *pStmt = 0;
188941
188942 rc = fts3SqlStmt(p, SQL_SELECT_MXLEVEL36, &pStmt, 0);
188943 if( rc==SQLITE_OK0 ){
188944 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
188945 mxLevel = sqlite3_column_int(pStmt, 0);
188946 }
188947 rc = sqlite3_reset(pStmt);
188948 }
188949 *pnMax = mxLevel;
188950 return rc;
188951}
188952
188953/*
188954** Insert a record into the %_segdir table.
188955*/
188956static int fts3WriteSegdir(
188957 Fts3Table *p, /* Virtual table handle */
188958 sqlite3_int64 iLevel, /* Value for "level" field (absolute level) */
188959 int iIdx, /* Value for "idx" field */
188960 sqlite3_int64 iStartBlock, /* Value for "start_block" field */
188961 sqlite3_int64 iLeafEndBlock, /* Value for "leaves_end_block" field */
188962 sqlite3_int64 iEndBlock, /* Value for "end_block" field */
188963 sqlite3_int64 nLeafData, /* Bytes of leaf data in segment */
188964 char *zRoot, /* Blob value for "root" field */
188965 int nRoot /* Number of bytes in buffer zRoot */
188966){
188967 sqlite3_stmt *pStmt;
188968 int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR11, &pStmt, 0);
188969 if( rc==SQLITE_OK0 ){
188970 sqlite3_bind_int64(pStmt, 1, iLevel);
188971 sqlite3_bind_int(pStmt, 2, iIdx);
188972 sqlite3_bind_int64(pStmt, 3, iStartBlock);
188973 sqlite3_bind_int64(pStmt, 4, iLeafEndBlock);
188974 if( nLeafData==0 ){
188975 sqlite3_bind_int64(pStmt, 5, iEndBlock);
188976 }else{
188977 char *zEnd = sqlite3_mprintf("%lld %lld", iEndBlock, nLeafData);
188978 if( !zEnd ) return SQLITE_NOMEM7;
188979 sqlite3_bind_text(pStmt, 5, zEnd, -1, sqlite3_free);
188980 }
188981 sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC((sqlite3_destructor_type)0));
188982 sqlite3_step(pStmt);
188983 rc = sqlite3_reset(pStmt);
188984 sqlite3_bind_null(pStmt, 6);
188985 }
188986 return rc;
188987}
188988
188989/*
188990** Return the size of the common prefix (if any) shared by zPrev and
188991** zNext, in bytes. For example,
188992**
188993** fts3PrefixCompress("abc", 3, "abcdef", 6) // returns 3
188994** fts3PrefixCompress("abX", 3, "abcdef", 6) // returns 2
188995** fts3PrefixCompress("abX", 3, "Xbcdef", 6) // returns 0
188996*/
188997static int fts3PrefixCompress(
188998 const char *zPrev, /* Buffer containing previous term */
188999 int nPrev, /* Size of buffer zPrev in bytes */
189000 const char *zNext, /* Buffer containing next term */
189001 int nNext /* Size of buffer zNext in bytes */
189002){
189003 int n;
189004 for(n=0; n<nPrev && n<nNext && zPrev[n]==zNext[n]; n++);
189005 assert_fts3_nc( n<nNext )((void) (0));
189006 return n;
189007}
189008
189009/*
189010** Add term zTerm to the SegmentNode. It is guaranteed that zTerm is larger
189011** (according to memcmp) than the previous term.
189012*/
189013static int fts3NodeAddTerm(
189014 Fts3Table *p, /* Virtual table handle */
189015 SegmentNode **ppTree, /* IN/OUT: SegmentNode handle */
189016 int isCopyTerm, /* True if zTerm/nTerm is transient */
189017 const char *zTerm, /* Pointer to buffer containing term */
189018 int nTerm /* Size of term in bytes */
189019){
189020 SegmentNode *pTree = *ppTree;
189021 int rc;
189022 SegmentNode *pNew;
189023
189024 /* First try to append the term to the current node. Return early if
189025 ** this is possible.
189026 */
189027 if( pTree ){
189028 int nData = pTree->nData; /* Current size of node in bytes */
189029 int nReq = nData; /* Required space after adding zTerm */
189030 int nPrefix; /* Number of bytes of prefix compression */
189031 int nSuffix; /* Suffix length */
189032
189033 nPrefix = fts3PrefixCompress(pTree->zTerm, pTree->nTerm, zTerm, nTerm);
189034 nSuffix = nTerm-nPrefix;
189035
189036 /* If nSuffix is zero or less, then zTerm/nTerm must be a prefix of
189037 ** pWriter->zTerm/pWriter->nTerm. i.e. must be equal to or less than when
189038 ** compared with BINARY collation. This indicates corruption. */
189039 if( nSuffix<=0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
189040
189041 nReq += sqlite3Fts3VarintLen(nPrefix)+sqlite3Fts3VarintLen(nSuffix)+nSuffix;
189042 if( nReq<=p->nNodeSize || !pTree->zTerm ){
189043
189044 if( nReq>p->nNodeSize ){
189045 /* An unusual case: this is the first term to be added to the node
189046 ** and the static node buffer (p->nNodeSize bytes) is not large
189047 ** enough. Use a separately malloced buffer instead This wastes
189048 ** p->nNodeSize bytes, but since this scenario only comes about when
189049 ** the database contain two terms that share a prefix of almost 2KB,
189050 ** this is not expected to be a serious problem.
189051 */
189052 assert( pTree->aData==(char *)&pTree[1] )((void) (0));
189053 pTree->aData = (char *)sqlite3_malloc(nReq);
189054 if( !pTree->aData ){
189055 return SQLITE_NOMEM7;
189056 }
189057 }
189058
189059 if( pTree->zTerm ){
189060 /* There is no prefix-length field for first term in a node */
189061 nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nPrefix);
189062 }
189063
189064 nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nSuffix);
189065 memcpy(&pTree->aData[nData], &zTerm[nPrefix], nSuffix);
189066 pTree->nData = nData + nSuffix;
189067 pTree->nEntry++;
189068
189069 if( isCopyTerm ){
189070 if( pTree->nMalloc<nTerm ){
189071 char *zNew = sqlite3_realloc(pTree->zMalloc, nTerm*2);
189072 if( !zNew ){
189073 return SQLITE_NOMEM7;
189074 }
189075 pTree->nMalloc = nTerm*2;
189076 pTree->zMalloc = zNew;
189077 }
189078 pTree->zTerm = pTree->zMalloc;
189079 memcpy(pTree->zTerm, zTerm, nTerm);
189080 pTree->nTerm = nTerm;
189081 }else{
189082 pTree->zTerm = (char *)zTerm;
189083 pTree->nTerm = nTerm;
189084 }
189085 return SQLITE_OK0;
189086 }
189087 }
189088
189089 /* If control flows to here, it was not possible to append zTerm to the
189090 ** current node. Create a new node (a right-sibling of the current node).
189091 ** If this is the first node in the tree, the term is added to it.
189092 **
189093 ** Otherwise, the term is not added to the new node, it is left empty for
189094 ** now. Instead, the term is inserted into the parent of pTree. If pTree
189095 ** has no parent, one is created here.
189096 */
189097 pNew = (SegmentNode *)sqlite3_malloc(sizeof(SegmentNode) + p->nNodeSize);
189098 if( !pNew ){
189099 return SQLITE_NOMEM7;
189100 }
189101 memset(pNew, 0, sizeof(SegmentNode));
189102 pNew->nData = 1 + FTS3_VARINT_MAX10;
189103 pNew->aData = (char *)&pNew[1];
189104
189105 if( pTree ){
189106 SegmentNode *pParent = pTree->pParent;
189107 rc = fts3NodeAddTerm(p, &pParent, isCopyTerm, zTerm, nTerm);
189108 if( pTree->pParent==0 ){
189109 pTree->pParent = pParent;
189110 }
189111 pTree->pRight = pNew;
189112 pNew->pLeftmost = pTree->pLeftmost;
189113 pNew->pParent = pParent;
189114 pNew->zMalloc = pTree->zMalloc;
189115 pNew->nMalloc = pTree->nMalloc;
189116 pTree->zMalloc = 0;
189117 }else{
189118 pNew->pLeftmost = pNew;
189119 rc = fts3NodeAddTerm(p, &pNew, isCopyTerm, zTerm, nTerm);
189120 }
189121
189122 *ppTree = pNew;
189123 return rc;
189124}
189125
189126/*
189127** Helper function for fts3NodeWrite().
189128*/
189129static int fts3TreeFinishNode(
189130 SegmentNode *pTree,
189131 int iHeight,
189132 sqlite3_int64 iLeftChild
189133){
189134 int nStart;
189135 assert( iHeight>=1 && iHeight<128 )((void) (0));
189136 nStart = FTS3_VARINT_MAX10 - sqlite3Fts3VarintLen(iLeftChild);
189137 pTree->aData[nStart] = (char)iHeight;
189138 sqlite3Fts3PutVarint(&pTree->aData[nStart+1], iLeftChild);
189139 return nStart;
189140}
189141
189142/*
189143** Write the buffer for the segment node pTree and all of its peers to the
189144** database. Then call this function recursively to write the parent of
189145** pTree and its peers to the database.
189146**
189147** Except, if pTree is a root node, do not write it to the database. Instead,
189148** set output variables *paRoot and *pnRoot to contain the root node.
189149**
189150** If successful, SQLITE_OK is returned and output variable *piLast is
189151** set to the largest blockid written to the database (or zero if no
189152** blocks were written to the db). Otherwise, an SQLite error code is
189153** returned.
189154*/
189155static int fts3NodeWrite(
189156 Fts3Table *p, /* Virtual table handle */
189157 SegmentNode *pTree, /* SegmentNode handle */
189158 int iHeight, /* Height of this node in tree */
189159 sqlite3_int64 iLeaf, /* Block id of first leaf node */
189160 sqlite3_int64 iFree, /* Block id of next free slot in %_segments */
189161 sqlite3_int64 *piLast, /* OUT: Block id of last entry written */
189162 char **paRoot, /* OUT: Data for root node */
189163 int *pnRoot /* OUT: Size of root node in bytes */
189164){
189165 int rc = SQLITE_OK0;
189166
189167 if( !pTree->pParent ){
189168 /* Root node of the tree. */
189169 int nStart = fts3TreeFinishNode(pTree, iHeight, iLeaf);
189170 *piLast = iFree-1;
189171 *pnRoot = pTree->nData - nStart;
189172 *paRoot = &pTree->aData[nStart];
189173 }else{
189174 SegmentNode *pIter;
189175 sqlite3_int64 iNextFree = iFree;
189176 sqlite3_int64 iNextLeaf = iLeaf;
189177 for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK0; pIter=pIter->pRight){
189178 int nStart = fts3TreeFinishNode(pIter, iHeight, iNextLeaf);
189179 int nWrite = pIter->nData - nStart;
189180
189181 rc = fts3WriteSegment(p, iNextFree, &pIter->aData[nStart], nWrite);
189182 iNextFree++;
189183 iNextLeaf += (pIter->nEntry+1);
189184 }
189185 if( rc==SQLITE_OK0 ){
189186 assert( iNextLeaf==iFree )((void) (0));
189187 rc = fts3NodeWrite(
189188 p, pTree->pParent, iHeight+1, iFree, iNextFree, piLast, paRoot, pnRoot
189189 );
189190 }
189191 }
189192
189193 return rc;
189194}
189195
189196/*
189197** Free all memory allocations associated with the tree pTree.
189198*/
189199static void fts3NodeFree(SegmentNode *pTree){
189200 if( pTree ){
189201 SegmentNode *p = pTree->pLeftmost;
189202 fts3NodeFree(p->pParent);
189203 while( p ){
189204 SegmentNode *pRight = p->pRight;
189205 if( p->aData!=(char *)&p[1] ){
189206 sqlite3_free(p->aData);
189207 }
189208 assert( pRight==0 || p->zMalloc==0 )((void) (0));
189209 sqlite3_free(p->zMalloc);
189210 sqlite3_free(p);
189211 p = pRight;
189212 }
189213 }
189214}
189215
189216/*
189217** Add a term to the segment being constructed by the SegmentWriter object
189218** *ppWriter. When adding the first term to a segment, *ppWriter should
189219** be passed NULL. This function will allocate a new SegmentWriter object
189220** and return it via the input/output variable *ppWriter in this case.
189221**
189222** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
189223*/
189224static int fts3SegWriterAdd(
189225 Fts3Table *p, /* Virtual table handle */
189226 SegmentWriter **ppWriter, /* IN/OUT: SegmentWriter handle */
189227 int isCopyTerm, /* True if buffer zTerm must be copied */
189228 const char *zTerm, /* Pointer to buffer containing term */
189229 int nTerm, /* Size of term in bytes */
189230 const char *aDoclist, /* Pointer to buffer containing doclist */
189231 int nDoclist /* Size of doclist in bytes */
189232){
189233 int nPrefix; /* Size of term prefix in bytes */
189234 int nSuffix; /* Size of term suffix in bytes */
189235 int nReq; /* Number of bytes required on leaf page */
189236 int nData;
189237 SegmentWriter *pWriter = *ppWriter;
189238
189239 if( !pWriter ){
189240 int rc;
189241 sqlite3_stmt *pStmt;
189242
189243 /* Allocate the SegmentWriter structure */
189244 pWriter = (SegmentWriter *)sqlite3_malloc(sizeof(SegmentWriter));
189245 if( !pWriter ) return SQLITE_NOMEM7;
189246 memset(pWriter, 0, sizeof(SegmentWriter));
189247 *ppWriter = pWriter;
189248
189249 /* Allocate a buffer in which to accumulate data */
189250 pWriter->aData = (char *)sqlite3_malloc(p->nNodeSize);
189251 if( !pWriter->aData ) return SQLITE_NOMEM7;
189252 pWriter->nSize = p->nNodeSize;
189253
189254 /* Find the next free blockid in the %_segments table */
189255 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID10, &pStmt, 0);
189256 if( rc!=SQLITE_OK0 ) return rc;
189257 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
189258 pWriter->iFree = sqlite3_column_int64(pStmt, 0);
189259 pWriter->iFirst = pWriter->iFree;
189260 }
189261 rc = sqlite3_reset(pStmt);
189262 if( rc!=SQLITE_OK0 ) return rc;
189263 }
189264 nData = pWriter->nData;
189265
189266 nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm);
189267 nSuffix = nTerm-nPrefix;
189268
189269 /* If nSuffix is zero or less, then zTerm/nTerm must be a prefix of
189270 ** pWriter->zTerm/pWriter->nTerm. i.e. must be equal to or less than when
189271 ** compared with BINARY collation. This indicates corruption. */
189272 if( nSuffix<=0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
189273
189274 /* Figure out how many bytes are required by this new entry */
189275 nReq = sqlite3Fts3VarintLen(nPrefix) + /* varint containing prefix size */
189276 sqlite3Fts3VarintLen(nSuffix) + /* varint containing suffix size */
189277 nSuffix + /* Term suffix */
189278 sqlite3Fts3VarintLen(nDoclist) + /* Size of doclist */
189279 nDoclist; /* Doclist data */
189280
189281 if( nData>0 && nData+nReq>p->nNodeSize ){
189282 int rc;
189283
189284 /* The current leaf node is full. Write it out to the database. */
189285 if( pWriter->iFree==LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) ) return FTS_CORRUPT_VTAB(11 | (1<<8));
189286 rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData);
189287 if( rc!=SQLITE_OK0 ) return rc;
189288 p->nLeafAdd++;
189289
189290 /* Add the current term to the interior node tree. The term added to
189291 ** the interior tree must:
189292 **
189293 ** a) be greater than the largest term on the leaf node just written
189294 ** to the database (still available in pWriter->zTerm), and
189295 **
189296 ** b) be less than or equal to the term about to be added to the new
189297 ** leaf node (zTerm/nTerm).
189298 **
189299 ** In other words, it must be the prefix of zTerm 1 byte longer than
189300 ** the common prefix (if any) of zTerm and pWriter->zTerm.
189301 */
189302 assert( nPrefix<nTerm )((void) (0));
189303 rc = fts3NodeAddTerm(p, &pWriter->pTree, isCopyTerm, zTerm, nPrefix+1);
189304 if( rc!=SQLITE_OK0 ) return rc;
189305
189306 nData = 0;
189307 pWriter->nTerm = 0;
189308
189309 nPrefix = 0;
189310 nSuffix = nTerm;
189311 nReq = 1 + /* varint containing prefix size */
189312 sqlite3Fts3VarintLen(nTerm) + /* varint containing suffix size */
189313 nTerm + /* Term suffix */
189314 sqlite3Fts3VarintLen(nDoclist) + /* Size of doclist */
189315 nDoclist; /* Doclist data */
189316 }
189317
189318 /* Increase the total number of bytes written to account for the new entry. */
189319 pWriter->nLeafData += nReq;
189320
189321 /* If the buffer currently allocated is too small for this entry, realloc
189322 ** the buffer to make it large enough.
189323 */
189324 if( nReq>pWriter->nSize ){
189325 char *aNew = sqlite3_realloc(pWriter->aData, nReq);
189326 if( !aNew ) return SQLITE_NOMEM7;
189327 pWriter->aData = aNew;
189328 pWriter->nSize = nReq;
189329 }
189330 assert( nData+nReq<=pWriter->nSize )((void) (0));
189331
189332 /* Append the prefix-compressed term and doclist to the buffer. */
189333 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix);
189334 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix);
189335 assert( nSuffix>0 )((void) (0));
189336 memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix);
189337 nData += nSuffix;
189338 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist);
189339 assert( nDoclist>0 )((void) (0));
189340 memcpy(&pWriter->aData[nData], aDoclist, nDoclist);
189341 pWriter->nData = nData + nDoclist;
189342
189343 /* Save the current term so that it can be used to prefix-compress the next.
189344 ** If the isCopyTerm parameter is true, then the buffer pointed to by
189345 ** zTerm is transient, so take a copy of the term data. Otherwise, just
189346 ** store a copy of the pointer.
189347 */
189348 if( isCopyTerm ){
189349 if( nTerm>pWriter->nMalloc ){
189350 char *zNew = sqlite3_realloc(pWriter->zMalloc, nTerm*2);
189351 if( !zNew ){
189352 return SQLITE_NOMEM7;
189353 }
189354 pWriter->nMalloc = nTerm*2;
189355 pWriter->zMalloc = zNew;
189356 pWriter->zTerm = zNew;
189357 }
189358 assert( pWriter->zTerm==pWriter->zMalloc )((void) (0));
189359 assert( nTerm>0 )((void) (0));
189360 memcpy(pWriter->zTerm, zTerm, nTerm);
189361 }else{
189362 pWriter->zTerm = (char *)zTerm;
189363 }
189364 pWriter->nTerm = nTerm;
189365
189366 return SQLITE_OK0;
189367}
189368
189369/*
189370** Flush all data associated with the SegmentWriter object pWriter to the
189371** database. This function must be called after all terms have been added
189372** to the segment using fts3SegWriterAdd(). If successful, SQLITE_OK is
189373** returned. Otherwise, an SQLite error code.
189374*/
189375static int fts3SegWriterFlush(
189376 Fts3Table *p, /* Virtual table handle */
189377 SegmentWriter *pWriter, /* SegmentWriter to flush to the db */
189378 sqlite3_int64 iLevel, /* Value for 'level' column of %_segdir */
189379 int iIdx /* Value for 'idx' column of %_segdir */
189380){
189381 int rc; /* Return code */
189382 if( pWriter->pTree ){
189383 sqlite3_int64 iLast = 0; /* Largest block id written to database */
189384 sqlite3_int64 iLastLeaf; /* Largest leaf block id written to db */
189385 char *zRoot = NULL((void*)0); /* Pointer to buffer containing root node */
189386 int nRoot = 0; /* Size of buffer zRoot */
189387
189388 iLastLeaf = pWriter->iFree;
189389 rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, pWriter->nData);
189390 if( rc==SQLITE_OK0 ){
189391 rc = fts3NodeWrite(p, pWriter->pTree, 1,
189392 pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
189393 }
189394 if( rc==SQLITE_OK0 ){
189395 rc = fts3WriteSegdir(p, iLevel, iIdx,
189396 pWriter->iFirst, iLastLeaf, iLast, pWriter->nLeafData, zRoot, nRoot);
189397 }
189398 }else{
189399 /* The entire tree fits on the root node. Write it to the segdir table. */
189400 rc = fts3WriteSegdir(p, iLevel, iIdx,
189401 0, 0, 0, pWriter->nLeafData, pWriter->aData, pWriter->nData);
189402 }
189403 p->nLeafAdd++;
189404 return rc;
189405}
189406
189407/*
189408** Release all memory held by the SegmentWriter object passed as the
189409** first argument.
189410*/
189411static void fts3SegWriterFree(SegmentWriter *pWriter){
189412 if( pWriter ){
189413 sqlite3_free(pWriter->aData);
189414 sqlite3_free(pWriter->zMalloc);
189415 fts3NodeFree(pWriter->pTree);
189416 sqlite3_free(pWriter);
189417 }
189418}
189419
189420/*
189421** The first value in the apVal[] array is assumed to contain an integer.
189422** This function tests if there exist any documents with docid values that
189423** are different from that integer. i.e. if deleting the document with docid
189424** pRowid would mean the FTS3 table were empty.
189425**
189426** If successful, *pisEmpty is set to true if the table is empty except for
189427** document pRowid, or false otherwise, and SQLITE_OK is returned. If an
189428** error occurs, an SQLite error code is returned.
189429*/
189430static int fts3IsEmpty(Fts3Table *p, sqlite3_value *pRowid, int *pisEmpty){
189431 sqlite3_stmt *pStmt;
189432 int rc;
189433 if( p->zContentTbl ){
189434 /* If using the content=xxx option, assume the table is never empty */
189435 *pisEmpty = 0;
189436 rc = SQLITE_OK0;
189437 }else{
189438 rc = fts3SqlStmt(p, SQL_IS_EMPTY1, &pStmt, &pRowid);
189439 if( rc==SQLITE_OK0 ){
189440 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
189441 *pisEmpty = sqlite3_column_int(pStmt, 0);
189442 }
189443 rc = sqlite3_reset(pStmt);
189444 }
189445 }
189446 return rc;
189447}
189448
189449/*
189450** Set *pnMax to the largest segment level in the database for the index
189451** iIndex.
189452**
189453** Segment levels are stored in the 'level' column of the %_segdir table.
189454**
189455** Return SQLITE_OK if successful, or an SQLite error code if not.
189456*/
189457static int fts3SegmentMaxLevel(
189458 Fts3Table *p,
189459 int iLangid,
189460 int iIndex,
189461 sqlite3_int64 *pnMax
189462){
189463 sqlite3_stmt *pStmt;
189464 int rc;
189465 assert( iIndex>=0 && iIndex<p->nIndex )((void) (0));
189466
189467 /* Set pStmt to the compiled version of:
189468 **
189469 ** SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
189470 **
189471 ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
189472 */
189473 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL15, &pStmt, 0);
189474 if( rc!=SQLITE_OK0 ) return rc;
189475 sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
189476 sqlite3_bind_int64(pStmt, 2,
189477 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL1024-1)
189478 );
189479 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
189480 *pnMax = sqlite3_column_int64(pStmt, 0);
189481 }
189482 return sqlite3_reset(pStmt);
189483}
189484
189485/*
189486** iAbsLevel is an absolute level that may be assumed to exist within
189487** the database. This function checks if it is the largest level number
189488** within its index. Assuming no error occurs, *pbMax is set to 1 if
189489** iAbsLevel is indeed the largest level, or 0 otherwise, and SQLITE_OK
189490** is returned. If an error occurs, an error code is returned and the
189491** final value of *pbMax is undefined.
189492*/
189493static int fts3SegmentIsMaxLevel(Fts3Table *p, i64 iAbsLevel, int *pbMax){
189494
189495 /* Set pStmt to the compiled version of:
189496 **
189497 ** SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
189498 **
189499 ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
189500 */
189501 sqlite3_stmt *pStmt;
189502 int rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL15, &pStmt, 0);
189503 if( rc!=SQLITE_OK0 ) return rc;
189504 sqlite3_bind_int64(pStmt, 1, iAbsLevel+1);
189505 sqlite3_bind_int64(pStmt, 2,
189506 (((u64)iAbsLevel/FTS3_SEGDIR_MAXLEVEL1024)+1) * FTS3_SEGDIR_MAXLEVEL1024
189507 );
189508
189509 *pbMax = 0;
189510 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
189511 *pbMax = sqlite3_column_type(pStmt, 0)==SQLITE_NULL5;
189512 }
189513 return sqlite3_reset(pStmt);
189514}
189515
189516/*
189517** Delete all entries in the %_segments table associated with the segment
189518** opened with seg-reader pSeg. This function does not affect the contents
189519** of the %_segdir table.
189520*/
189521static int fts3DeleteSegment(
189522 Fts3Table *p, /* FTS table handle */
189523 Fts3SegReader *pSeg /* Segment to delete */
189524){
189525 int rc = SQLITE_OK0; /* Return code */
189526 if( pSeg->iStartBlock ){
189527 sqlite3_stmt *pDelete; /* SQL statement to delete rows */
189528 rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE17, &pDelete, 0);
189529 if( rc==SQLITE_OK0 ){
189530 sqlite3_bind_int64(pDelete, 1, pSeg->iStartBlock);
189531 sqlite3_bind_int64(pDelete, 2, pSeg->iEndBlock);
189532 sqlite3_step(pDelete);
189533 rc = sqlite3_reset(pDelete);
189534 }
189535 }
189536 return rc;
189537}
189538
189539/*
189540** This function is used after merging multiple segments into a single large
189541** segment to delete the old, now redundant, segment b-trees. Specifically,
189542** it:
189543**
189544** 1) Deletes all %_segments entries for the segments associated with
189545** each of the SegReader objects in the array passed as the third
189546** argument, and
189547**
189548** 2) deletes all %_segdir entries with level iLevel, or all %_segdir
189549** entries regardless of level if (iLevel<0).
189550**
189551** SQLITE_OK is returned if successful, otherwise an SQLite error code.
189552*/
189553static int fts3DeleteSegdir(
189554 Fts3Table *p, /* Virtual table handle */
189555 int iLangid, /* Language id */
189556 int iIndex, /* Index for p->aIndex */
189557 int iLevel, /* Level of %_segdir entries to delete */
189558 Fts3SegReader **apSegment, /* Array of SegReader objects */
189559 int nReader /* Size of array apSegment */
189560){
189561 int rc = SQLITE_OK0; /* Return Code */
189562 int i; /* Iterator variable */
189563 sqlite3_stmt *pDelete = 0; /* SQL statement to delete rows */
189564
189565 for(i=0; rc==SQLITE_OK0 && i<nReader; i++){
189566 rc = fts3DeleteSegment(p, apSegment[i]);
189567 }
189568 if( rc!=SQLITE_OK0 ){
189569 return rc;
189570 }
189571
189572 assert( iLevel>=0 || iLevel==FTS3_SEGCURSOR_ALL )((void) (0));
189573 if( iLevel==FTS3_SEGCURSOR_ALL-2 ){
189574 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_RANGE26, &pDelete, 0);
189575 if( rc==SQLITE_OK0 ){
189576 sqlite3_bind_int64(pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
189577 sqlite3_bind_int64(pDelete, 2,
189578 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL1024-1)
189579 );
189580 }
189581 }else{
189582 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL16, &pDelete, 0);
189583 if( rc==SQLITE_OK0 ){
189584 sqlite3_bind_int64(
189585 pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
189586 );
189587 }
189588 }
189589
189590 if( rc==SQLITE_OK0 ){
189591 sqlite3_step(pDelete);
189592 rc = sqlite3_reset(pDelete);
189593 }
189594
189595 return rc;
189596}
189597
189598/*
189599** When this function is called, buffer *ppList (size *pnList bytes) contains
189600** a position list that may (or may not) feature multiple columns. This
189601** function adjusts the pointer *ppList and the length *pnList so that they
189602** identify the subset of the position list that corresponds to column iCol.
189603**
189604** If there are no entries in the input position list for column iCol, then
189605** *pnList is set to zero before returning.
189606**
189607** If parameter bZero is non-zero, then any part of the input list following
189608** the end of the output list is zeroed before returning.
189609*/
189610static void fts3ColumnFilter(
189611 int iCol, /* Column to filter on */
189612 int bZero, /* Zero out anything following *ppList */
189613 char **ppList, /* IN/OUT: Pointer to position list */
189614 int *pnList /* IN/OUT: Size of buffer *ppList in bytes */
189615){
189616 char *pList = *ppList;
189617 int nList = *pnList;
189618 char *pEnd = &pList[nList];
189619 int iCurrent = 0;
189620 char *p = pList;
189621
189622 assert( iCol>=0 )((void) (0));
189623 while( 1 ){
189624 char c = 0;
189625 while( p<pEnd && (c | *p)&0xFE ) c = *p++ & 0x80;
189626
189627 if( iCol==iCurrent ){
189628 nList = (int)(p - pList);
189629 break;
189630 }
189631
189632 nList -= (int)(p - pList);
189633 pList = p;
189634 if( nList<=0 ){
189635 break;
189636 }
189637 p = &pList[1];
189638 p += fts3GetVarint32(p, &iCurrent)( (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, &iCurrent
) : (*&iCurrent=*(u8*)(p), 1) )
;
189639 }
189640
189641 if( bZero && (pEnd - &pList[nList])>0){
189642 memset(&pList[nList], 0, pEnd - &pList[nList]);
189643 }
189644 *ppList = pList;
189645 *pnList = nList;
189646}
189647
189648/*
189649** Cache data in the Fts3MultiSegReader.aBuffer[] buffer (overwriting any
189650** existing data). Grow the buffer if required.
189651**
189652** If successful, return SQLITE_OK. Otherwise, if an OOM error is encountered
189653** trying to resize the buffer, return SQLITE_NOMEM.
189654*/
189655static int fts3MsrBufferData(
189656 Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
189657 char *pList,
189658 int nList
189659){
189660 if( nList>pMsr->nBuffer ){
189661 char *pNew;
189662 pMsr->nBuffer = nList*2;
189663 pNew = (char *)sqlite3_realloc(pMsr->aBuffer, pMsr->nBuffer);
189664 if( !pNew ) return SQLITE_NOMEM7;
189665 pMsr->aBuffer = pNew;
189666 }
189667
189668 assert( nList>0 )((void) (0));
189669 memcpy(pMsr->aBuffer, pList, nList);
189670 return SQLITE_OK0;
189671}
189672
189673SQLITE_PRIVATEstatic int sqlite3Fts3MsrIncrNext(
189674 Fts3Table *p, /* Virtual table handle */
189675 Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
189676 sqlite3_int64 *piDocid, /* OUT: Docid value */
189677 char **paPoslist, /* OUT: Pointer to position list */
189678 int *pnPoslist /* OUT: Size of position list in bytes */
189679){
189680 int nMerge = pMsr->nAdvance;
189681 Fts3SegReader **apSegment = pMsr->apSegment;
189682 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
189683 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
189684 );
189685
189686 if( nMerge==0 ){
189687 *paPoslist = 0;
189688 return SQLITE_OK0;
189689 }
189690
189691 while( 1 ){
189692 Fts3SegReader *pSeg;
189693 pSeg = pMsr->apSegment[0];
189694
189695 if( pSeg->pOffsetList==0 ){
189696 *paPoslist = 0;
189697 break;
189698 }else{
189699 int rc;
189700 char *pList;
189701 int nList;
189702 int j;
189703 sqlite3_int64 iDocid = apSegment[0]->iDocid;
189704
189705 rc = fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
189706 j = 1;
189707 while( rc==SQLITE_OK0
189708 && j<nMerge
189709 && apSegment[j]->pOffsetList
189710 && apSegment[j]->iDocid==iDocid
189711 ){
189712 rc = fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
189713 j++;
189714 }
189715 if( rc!=SQLITE_OK0 ) return rc;
189716 fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp);
189717
189718 if( nList>0 && fts3SegReaderIsPending(apSegment[0])((apSegment[0])->ppNextElem!=0) ){
189719 rc = fts3MsrBufferData(pMsr, pList, nList+1);
189720 if( rc!=SQLITE_OK0 ) return rc;
189721 assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 )((void) (0));
189722 pList = pMsr->aBuffer;
189723 }
189724
189725 if( pMsr->iColFilter>=0 ){
189726 fts3ColumnFilter(pMsr->iColFilter, 1, &pList, &nList);
189727 }
189728
189729 if( nList>0 ){
189730 *paPoslist = pList;
189731 *piDocid = iDocid;
189732 *pnPoslist = nList;
189733 break;
189734 }
189735 }
189736 }
189737
189738 return SQLITE_OK0;
189739}
189740
189741static int fts3SegReaderStart(
189742 Fts3Table *p, /* Virtual table handle */
189743 Fts3MultiSegReader *pCsr, /* Cursor object */
189744 const char *zTerm, /* Term searched for (or NULL) */
189745 int nTerm /* Length of zTerm in bytes */
189746){
189747 int i;
189748 int nSeg = pCsr->nSegment;
189749
189750 /* If the Fts3SegFilter defines a specific term (or term prefix) to search
189751 ** for, then advance each segment iterator until it points to a term of
189752 ** equal or greater value than the specified term. This prevents many
189753 ** unnecessary merge/sort operations for the case where single segment
189754 ** b-tree leaf nodes contain more than one term.
189755 */
189756 for(i=0; pCsr->bRestart==0 && i<pCsr->nSegment; i++){
189757 int res = 0;
189758 Fts3SegReader *pSeg = pCsr->apSegment[i];
189759 do {
189760 int rc = fts3SegReaderNext(p, pSeg, 0);
189761 if( rc!=SQLITE_OK0 ) return rc;
189762 }while( zTerm && (res = fts3SegReaderTermCmp(pSeg, zTerm, nTerm))<0 );
189763
189764 if( pSeg->bLookup && res!=0 ){
189765 fts3SegReaderSetEof(pSeg);
189766 }
189767 }
189768 fts3SegReaderSort(pCsr->apSegment, nSeg, nSeg, fts3SegReaderCmp);
189769
189770 return SQLITE_OK0;
189771}
189772
189773SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderStart(
189774 Fts3Table *p, /* Virtual table handle */
189775 Fts3MultiSegReader *pCsr, /* Cursor object */
189776 Fts3SegFilter *pFilter /* Restrictions on range of iteration */
189777){
189778 pCsr->pFilter = pFilter;
189779 return fts3SegReaderStart(p, pCsr, pFilter->zTerm, pFilter->nTerm);
189780}
189781
189782SQLITE_PRIVATEstatic int sqlite3Fts3MsrIncrStart(
189783 Fts3Table *p, /* Virtual table handle */
189784 Fts3MultiSegReader *pCsr, /* Cursor object */
189785 int iCol, /* Column to match on. */
189786 const char *zTerm, /* Term to iterate through a doclist for */
189787 int nTerm /* Number of bytes in zTerm */
189788){
189789 int i;
189790 int rc;
189791 int nSegment = pCsr->nSegment;
189792 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
189793 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
189794 );
189795
189796 assert( pCsr->pFilter==0 )((void) (0));
189797 assert( zTerm && nTerm>0 )((void) (0));
189798
189799 /* Advance each segment iterator until it points to the term zTerm/nTerm. */
189800 rc = fts3SegReaderStart(p, pCsr, zTerm, nTerm);
189801 if( rc!=SQLITE_OK0 ) return rc;
189802
189803 /* Determine how many of the segments actually point to zTerm/nTerm. */
189804 for(i=0; i<nSegment; i++){
189805 Fts3SegReader *pSeg = pCsr->apSegment[i];
189806 if( !pSeg->aNode || fts3SegReaderTermCmp(pSeg, zTerm, nTerm) ){
189807 break;
189808 }
189809 }
189810 pCsr->nAdvance = i;
189811
189812 /* Advance each of the segments to point to the first docid. */
189813 for(i=0; i<pCsr->nAdvance; i++){
189814 rc = fts3SegReaderFirstDocid(p, pCsr->apSegment[i]);
189815 if( rc!=SQLITE_OK0 ) return rc;
189816 }
189817 fts3SegReaderSort(pCsr->apSegment, i, i, xCmp);
189818
189819 assert( iCol<0 || iCol<p->nColumn )((void) (0));
189820 pCsr->iColFilter = iCol;
189821
189822 return SQLITE_OK0;
189823}
189824
189825/*
189826** This function is called on a MultiSegReader that has been started using
189827** sqlite3Fts3MsrIncrStart(). One or more calls to MsrIncrNext() may also
189828** have been made. Calling this function puts the MultiSegReader in such
189829** a state that if the next two calls are:
189830**
189831** sqlite3Fts3SegReaderStart()
189832** sqlite3Fts3SegReaderStep()
189833**
189834** then the entire doclist for the term is available in
189835** MultiSegReader.aDoclist/nDoclist.
189836*/
189837SQLITE_PRIVATEstatic int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr){
189838 int i; /* Used to iterate through segment-readers */
189839
189840 assert( pCsr->zTerm==0 )((void) (0));
189841 assert( pCsr->nTerm==0 )((void) (0));
189842 assert( pCsr->aDoclist==0 )((void) (0));
189843 assert( pCsr->nDoclist==0 )((void) (0));
189844
189845 pCsr->nAdvance = 0;
189846 pCsr->bRestart = 1;
189847 for(i=0; i<pCsr->nSegment; i++){
189848 pCsr->apSegment[i]->pOffsetList = 0;
189849 pCsr->apSegment[i]->nOffsetList = 0;
189850 pCsr->apSegment[i]->iDocid = 0;
189851 }
189852
189853 return SQLITE_OK0;
189854}
189855
189856static int fts3GrowSegReaderBuffer(Fts3MultiSegReader *pCsr, int nReq){
189857 if( nReq>pCsr->nBuffer ){
189858 char *aNew;
189859 pCsr->nBuffer = nReq*2;
189860 aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer);
189861 if( !aNew ){
189862 return SQLITE_NOMEM7;
189863 }
189864 pCsr->aBuffer = aNew;
189865 }
189866 return SQLITE_OK0;
189867}
189868
189869
189870SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderStep(
189871 Fts3Table *p, /* Virtual table handle */
189872 Fts3MultiSegReader *pCsr /* Cursor object */
189873){
189874 int rc = SQLITE_OK0;
189875
189876 int isIgnoreEmpty = (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY0x00000002);
189877 int isRequirePos = (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS0x00000001);
189878 int isColFilter = (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER0x00000004);
189879 int isPrefix = (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX0x00000008);
189880 int isScan = (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN0x00000010);
189881 int isFirst = (pCsr->pFilter->flags & FTS3_SEGMENT_FIRST0x00000020);
189882
189883 Fts3SegReader **apSegment = pCsr->apSegment;
189884 int nSegment = pCsr->nSegment;
189885 Fts3SegFilter *pFilter = pCsr->pFilter;
189886 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
189887 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
189888 );
189889
189890 if( pCsr->nSegment==0 ) return SQLITE_OK0;
189891
189892 do {
189893 int nMerge;
189894 int i;
189895
189896 /* Advance the first pCsr->nAdvance entries in the apSegment[] array
189897 ** forward. Then sort the list in order of current term again.
189898 */
189899 for(i=0; i<pCsr->nAdvance; i++){
189900 Fts3SegReader *pSeg = apSegment[i];
189901 if( pSeg->bLookup ){
189902 fts3SegReaderSetEof(pSeg);
189903 }else{
189904 rc = fts3SegReaderNext(p, pSeg, 0);
189905 }
189906 if( rc!=SQLITE_OK0 ) return rc;
189907 }
189908 fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp);
189909 pCsr->nAdvance = 0;
189910
189911 /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
189912 assert( rc==SQLITE_OK )((void) (0));
189913 if( apSegment[0]->aNode==0 ) break;
189914
189915 pCsr->nTerm = apSegment[0]->nTerm;
189916 pCsr->zTerm = apSegment[0]->zTerm;
189917
189918 /* If this is a prefix-search, and if the term that apSegment[0] points
189919 ** to does not share a suffix with pFilter->zTerm/nTerm, then all
189920 ** required callbacks have been made. In this case exit early.
189921 **
189922 ** Similarly, if this is a search for an exact match, and the first term
189923 ** of segment apSegment[0] is not a match, exit early.
189924 */
189925 if( pFilter->zTerm && !isScan ){
189926 if( pCsr->nTerm<pFilter->nTerm
189927 || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
189928 || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm)
189929 ){
189930 break;
189931 }
189932 }
189933
189934 nMerge = 1;
189935 while( nMerge<nSegment
189936 && apSegment[nMerge]->aNode
189937 && apSegment[nMerge]->nTerm==pCsr->nTerm
189938 && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm)
189939 ){
189940 nMerge++;
189941 }
189942
189943 assert( isIgnoreEmpty || (isRequirePos && !isColFilter) )((void) (0));
189944 if( nMerge==1
189945 && !isIgnoreEmpty
189946 && !isFirst
189947 && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])((apSegment[0])->ppNextElem!=0)==0)
189948 ){
189949 pCsr->nDoclist = apSegment[0]->nDoclist;
189950 if( fts3SegReaderIsPending(apSegment[0])((apSegment[0])->ppNextElem!=0) ){
189951 rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, pCsr->nDoclist);
189952 pCsr->aDoclist = pCsr->aBuffer;
189953 }else{
189954 pCsr->aDoclist = apSegment[0]->aDoclist;
189955 }
189956 if( rc==SQLITE_OK0 ) rc = SQLITE_ROW100;
189957 }else{
189958 int nDoclist = 0; /* Size of doclist */
189959 sqlite3_int64 iPrev = 0; /* Previous docid stored in doclist */
189960
189961 /* The current term of the first nMerge entries in the array
189962 ** of Fts3SegReader objects is the same. The doclists must be merged
189963 ** and a single term returned with the merged doclist.
189964 */
189965 for(i=0; i<nMerge; i++){
189966 fts3SegReaderFirstDocid(p, apSegment[i]);
189967 }
189968 fts3SegReaderSort(apSegment, nMerge, nMerge, xCmp);
189969 while( apSegment[0]->pOffsetList ){
189970 int j; /* Number of segments that share a docid */
189971 char *pList = 0;
189972 int nList = 0;
189973 int nByte;
189974 sqlite3_int64 iDocid = apSegment[0]->iDocid;
189975 fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
189976 j = 1;
189977 while( j<nMerge
189978 && apSegment[j]->pOffsetList
189979 && apSegment[j]->iDocid==iDocid
189980 ){
189981 fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
189982 j++;
189983 }
189984
189985 if( isColFilter ){
189986 fts3ColumnFilter(pFilter->iCol, 0, &pList, &nList);
189987 }
189988
189989 if( !isIgnoreEmpty || nList>0 ){
189990
189991 /* Calculate the 'docid' delta value to write into the merged
189992 ** doclist. */
189993 sqlite3_int64 iDelta;
189994 if( p->bDescIdx && nDoclist>0 ){
189995 if( iPrev<=iDocid ) return FTS_CORRUPT_VTAB(11 | (1<<8));
189996 iDelta = (i64)((u64)iPrev - (u64)iDocid);
189997 }else{
189998 if( nDoclist>0 && iPrev>=iDocid ) return FTS_CORRUPT_VTAB(11 | (1<<8));
189999 iDelta = (i64)((u64)iDocid - (u64)iPrev);
190000 }
190001
190002 nByte = sqlite3Fts3VarintLen(iDelta) + (isRequirePos?nList+1:0);
190003
190004 rc = fts3GrowSegReaderBuffer(pCsr, nByte+nDoclist+FTS3_NODE_PADDING(10*2));
190005 if( rc ) return rc;
190006
190007 if( isFirst ){
190008 char *a = &pCsr->aBuffer[nDoclist];
190009 int nWrite;
190010
190011 nWrite = sqlite3Fts3FirstFilter(iDelta, pList, nList, a);
190012 if( nWrite ){
190013 iPrev = iDocid;
190014 nDoclist += nWrite;
190015 }
190016 }else{
190017 nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta);
190018 iPrev = iDocid;
190019 if( isRequirePos ){
190020 memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
190021 nDoclist += nList;
190022 pCsr->aBuffer[nDoclist++] = '\0';
190023 }
190024 }
190025 }
190026
190027 fts3SegReaderSort(apSegment, nMerge, j, xCmp);
190028 }
190029 if( nDoclist>0 ){
190030 rc = fts3GrowSegReaderBuffer(pCsr, nDoclist+FTS3_NODE_PADDING(10*2));
190031 if( rc ) return rc;
190032 memset(&pCsr->aBuffer[nDoclist], 0, FTS3_NODE_PADDING(10*2));
190033 pCsr->aDoclist = pCsr->aBuffer;
190034 pCsr->nDoclist = nDoclist;
190035 rc = SQLITE_ROW100;
190036 }
190037 }
190038 pCsr->nAdvance = nMerge;
190039 }while( rc==SQLITE_OK0 );
190040
190041 return rc;
190042}
190043
190044
190045SQLITE_PRIVATEstatic void sqlite3Fts3SegReaderFinish(
190046 Fts3MultiSegReader *pCsr /* Cursor object */
190047){
190048 if( pCsr ){
190049 int i;
190050 for(i=0; i<pCsr->nSegment; i++){
190051 sqlite3Fts3SegReaderFree(pCsr->apSegment[i]);
190052 }
190053 sqlite3_free(pCsr->apSegment);
190054 sqlite3_free(pCsr->aBuffer);
190055
190056 pCsr->nSegment = 0;
190057 pCsr->apSegment = 0;
190058 pCsr->aBuffer = 0;
190059 }
190060}
190061
190062/*
190063** Decode the "end_block" field, selected by column iCol of the SELECT
190064** statement passed as the first argument.
190065**
190066** The "end_block" field may contain either an integer, or a text field
190067** containing the text representation of two non-negative integers separated
190068** by one or more space (0x20) characters. In the first case, set *piEndBlock
190069** to the integer value and *pnByte to zero before returning. In the second,
190070** set *piEndBlock to the first value and *pnByte to the second.
190071*/
190072static void fts3ReadEndBlockField(
190073 sqlite3_stmt *pStmt,
190074 int iCol,
190075 i64 *piEndBlock,
190076 i64 *pnByte
190077){
190078 const unsigned char *zText = sqlite3_column_text(pStmt, iCol);
190079 if( zText ){
190080 int i;
190081 int iMul = 1;
190082 u64 iVal = 0;
190083 for(i=0; zText[i]>='0' && zText[i]<='9'; i++){
190084 iVal = iVal*10 + (zText[i] - '0');
190085 }
190086 *piEndBlock = (i64)iVal;
190087 while( zText[i]==' ' ) i++;
190088 iVal = 0;
190089 if( zText[i]=='-' ){
190090 i++;
190091 iMul = -1;
190092 }
190093 for(/* no-op */; zText[i]>='0' && zText[i]<='9'; i++){
190094 iVal = iVal*10 + (zText[i] - '0');
190095 }
190096 *pnByte = ((i64)iVal * (i64)iMul);
190097 }
190098}
190099
190100
190101/*
190102** A segment of size nByte bytes has just been written to absolute level
190103** iAbsLevel. Promote any segments that should be promoted as a result.
190104*/
190105static int fts3PromoteSegments(
190106 Fts3Table *p, /* FTS table handle */
190107 sqlite3_int64 iAbsLevel, /* Absolute level just updated */
190108 sqlite3_int64 nByte /* Size of new segment at iAbsLevel */
190109){
190110 int rc = SQLITE_OK0;
190111 sqlite3_stmt *pRange;
190112
190113 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE237, &pRange, 0);
190114
190115 if( rc==SQLITE_OK0 ){
190116 int bOk = 0;
190117 i64 iLast = (iAbsLevel/FTS3_SEGDIR_MAXLEVEL1024 + 1) * FTS3_SEGDIR_MAXLEVEL1024 - 1;
190118 i64 nLimit = (nByte*3)/2;
190119
190120 /* Loop through all entries in the %_segdir table corresponding to
190121 ** segments in this index on levels greater than iAbsLevel. If there is
190122 ** at least one such segment, and it is possible to determine that all
190123 ** such segments are smaller than nLimit bytes in size, they will be
190124 ** promoted to level iAbsLevel. */
190125 sqlite3_bind_int64(pRange, 1, iAbsLevel+1);
190126 sqlite3_bind_int64(pRange, 2, iLast);
190127 while( SQLITE_ROW100==sqlite3_step(pRange) ){
190128 i64 nSize = 0, dummy;
190129 fts3ReadEndBlockField(pRange, 2, &dummy, &nSize);
190130 if( nSize<=0 || nSize>nLimit ){
190131 /* If nSize==0, then the %_segdir.end_block field does not not
190132 ** contain a size value. This happens if it was written by an
190133 ** old version of FTS. In this case it is not possible to determine
190134 ** the size of the segment, and so segment promotion does not
190135 ** take place. */
190136 bOk = 0;
190137 break;
190138 }
190139 bOk = 1;
190140 }
190141 rc = sqlite3_reset(pRange);
190142
190143 if( bOk ){
190144 int iIdx = 0;
190145 sqlite3_stmt *pUpdate1 = 0;
190146 sqlite3_stmt *pUpdate2 = 0;
190147
190148 if( rc==SQLITE_OK0 ){
190149 rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL_IDX38, &pUpdate1, 0);
190150 }
190151 if( rc==SQLITE_OK0 ){
190152 rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL39, &pUpdate2, 0);
190153 }
190154
190155 if( rc==SQLITE_OK0 ){
190156
190157 /* Loop through all %_segdir entries for segments in this index with
190158 ** levels equal to or greater than iAbsLevel. As each entry is visited,
190159 ** updated it to set (level = -1) and (idx = N), where N is 0 for the
190160 ** oldest segment in the range, 1 for the next oldest, and so on.
190161 **
190162 ** In other words, move all segments being promoted to level -1,
190163 ** setting the "idx" fields as appropriate to keep them in the same
190164 ** order. The contents of level -1 (which is never used, except
190165 ** transiently here), will be moved back to level iAbsLevel below. */
190166 sqlite3_bind_int64(pRange, 1, iAbsLevel);
190167 while( SQLITE_ROW100==sqlite3_step(pRange) ){
190168 sqlite3_bind_int(pUpdate1, 1, iIdx++);
190169 sqlite3_bind_int(pUpdate1, 2, sqlite3_column_int(pRange, 0));
190170 sqlite3_bind_int(pUpdate1, 3, sqlite3_column_int(pRange, 1));
190171 sqlite3_step(pUpdate1);
190172 rc = sqlite3_reset(pUpdate1);
190173 if( rc!=SQLITE_OK0 ){
190174 sqlite3_reset(pRange);
190175 break;
190176 }
190177 }
190178 }
190179 if( rc==SQLITE_OK0 ){
190180 rc = sqlite3_reset(pRange);
190181 }
190182
190183 /* Move level -1 to level iAbsLevel */
190184 if( rc==SQLITE_OK0 ){
190185 sqlite3_bind_int64(pUpdate2, 1, iAbsLevel);
190186 sqlite3_step(pUpdate2);
190187 rc = sqlite3_reset(pUpdate2);
190188 }
190189 }
190190 }
190191
190192
190193 return rc;
190194}
190195
190196/*
190197** Merge all level iLevel segments in the database into a single
190198** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
190199** single segment with a level equal to the numerically largest level
190200** currently present in the database.
190201**
190202** If this function is called with iLevel<0, but there is only one
190203** segment in the database, SQLITE_DONE is returned immediately.
190204** Otherwise, if successful, SQLITE_OK is returned. If an error occurs,
190205** an SQLite error code is returned.
190206*/
190207static int fts3SegmentMerge(
190208 Fts3Table *p,
190209 int iLangid, /* Language id to merge */
190210 int iIndex, /* Index in p->aIndex[] to merge */
190211 int iLevel /* Level to merge */
190212){
190213 int rc; /* Return code */
190214 int iIdx = 0; /* Index of new segment */
190215 sqlite3_int64 iNewLevel = 0; /* Level/index to create new segment at */
190216 SegmentWriter *pWriter = 0; /* Used to write the new, merged, segment */
190217 Fts3SegFilter filter; /* Segment term filter condition */
190218 Fts3MultiSegReader csr; /* Cursor to iterate through level(s) */
190219 int bIgnoreEmpty = 0; /* True to ignore empty segments */
190220 i64 iMaxLevel = 0; /* Max level number for this index/langid */
190221
190222 assert( iLevel==FTS3_SEGCURSOR_ALL((void) (0))
190223 || iLevel==FTS3_SEGCURSOR_PENDING((void) (0))
190224 || iLevel>=0((void) (0))
190225 )((void) (0));
190226 assert( iLevel<FTS3_SEGDIR_MAXLEVEL )((void) (0));
190227 assert( iIndex>=0 && iIndex<p->nIndex )((void) (0));
190228
190229 rc = sqlite3Fts3SegReaderCursor(p, iLangid, iIndex, iLevel, 0, 0, 1, 0, &csr);
190230 if( rc!=SQLITE_OK0 || csr.nSegment==0 ) goto finished;
190231
190232 if( iLevel!=FTS3_SEGCURSOR_PENDING-1 ){
190233 rc = fts3SegmentMaxLevel(p, iLangid, iIndex, &iMaxLevel);
190234 if( rc!=SQLITE_OK0 ) goto finished;
190235 }
190236
190237 if( iLevel==FTS3_SEGCURSOR_ALL-2 ){
190238 /* This call is to merge all segments in the database to a single
190239 ** segment. The level of the new segment is equal to the numerically
190240 ** greatest segment level currently present in the database for this
190241 ** index. The idx of the new segment is always 0. */
190242 if( csr.nSegment==1 && 0==fts3SegReaderIsPending(csr.apSegment[0])((csr.apSegment[0])->ppNextElem!=0) ){
190243 rc = SQLITE_DONE101;
190244 goto finished;
190245 }
190246 iNewLevel = iMaxLevel;
190247 bIgnoreEmpty = 1;
190248
190249 }else{
190250 /* This call is to merge all segments at level iLevel. find the next
190251 ** available segment index at level iLevel+1. The call to
190252 ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to
190253 ** a single iLevel+2 segment if necessary. */
190254 assert( FTS3_SEGCURSOR_PENDING==-1 )((void) (0));
190255 iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, iLevel+1);
190256 rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, iLevel+1, &iIdx);
190257 bIgnoreEmpty = (iLevel!=FTS3_SEGCURSOR_PENDING-1) && (iNewLevel>iMaxLevel);
190258 }
190259 if( rc!=SQLITE_OK0 ) goto finished;
190260
190261 assert( csr.nSegment>0 )((void) (0));
190262 assert_fts3_nc( iNewLevel>=getAbsoluteLevel(p, iLangid, iIndex, 0) )((void) (0));
190263 assert_fts3_nc(((void) (0))
190264 iNewLevel<getAbsoluteLevel(p, iLangid, iIndex,FTS3_SEGDIR_MAXLEVEL)((void) (0))
190265 )((void) (0));
190266
190267 memset(&filter, 0, sizeof(Fts3SegFilter));
190268 filter.flags = FTS3_SEGMENT_REQUIRE_POS0x00000001;
190269 filter.flags |= (bIgnoreEmpty ? FTS3_SEGMENT_IGNORE_EMPTY0x00000002 : 0);
190270
190271 rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
190272 while( SQLITE_OK0==rc ){
190273 rc = sqlite3Fts3SegReaderStep(p, &csr);
190274 if( rc!=SQLITE_ROW100 ) break;
190275 rc = fts3SegWriterAdd(p, &pWriter, 1,
190276 csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
190277 }
190278 if( rc!=SQLITE_OK0 ) goto finished;
190279 assert_fts3_nc( pWriter || bIgnoreEmpty )((void) (0));
190280
190281 if( iLevel!=FTS3_SEGCURSOR_PENDING-1 ){
190282 rc = fts3DeleteSegdir(
190283 p, iLangid, iIndex, iLevel, csr.apSegment, csr.nSegment
190284 );
190285 if( rc!=SQLITE_OK0 ) goto finished;
190286 }
190287 if( pWriter ){
190288 rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
190289 if( rc==SQLITE_OK0 ){
190290 if( iLevel==FTS3_SEGCURSOR_PENDING-1 || iNewLevel<iMaxLevel ){
190291 rc = fts3PromoteSegments(p, iNewLevel, pWriter->nLeafData);
190292 }
190293 }
190294 }
190295
190296 finished:
190297 fts3SegWriterFree(pWriter);
190298 sqlite3Fts3SegReaderFinish(&csr);
190299 return rc;
190300}
190301
190302
190303/*
190304** Flush the contents of pendingTerms to level 0 segments.
190305*/
190306SQLITE_PRIVATEstatic int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
190307 int rc = SQLITE_OK0;
190308 int i;
190309
190310 for(i=0; rc==SQLITE_OK0 && i<p->nIndex; i++){
190311 rc = fts3SegmentMerge(p, p->iPrevLangid, i, FTS3_SEGCURSOR_PENDING-1);
190312 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
190313 }
190314 sqlite3Fts3PendingTermsClear(p);
190315
190316 /* Determine the auto-incr-merge setting if unknown. If enabled,
190317 ** estimate the number of leaf blocks of content to be written
190318 */
190319 if( rc==SQLITE_OK0 && p->bHasStat
190320 && p->nAutoincrmerge==0xff && p->nLeafAdd>0
190321 ){
190322 sqlite3_stmt *pStmt = 0;
190323 rc = fts3SqlStmt(p, SQL_SELECT_STAT22, &pStmt, 0);
190324 if( rc==SQLITE_OK0 ){
190325 sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE2);
190326 rc = sqlite3_step(pStmt);
190327 if( rc==SQLITE_ROW100 ){
190328 p->nAutoincrmerge = sqlite3_column_int(pStmt, 0);
190329 if( p->nAutoincrmerge==1 ) p->nAutoincrmerge = 8;
190330 }else if( rc==SQLITE_DONE101 ){
190331 p->nAutoincrmerge = 0;
190332 }
190333 rc = sqlite3_reset(pStmt);
190334 }
190335 }
190336 return rc;
190337}
190338
190339/*
190340** Encode N integers as varints into a blob.
190341*/
190342static void fts3EncodeIntArray(
190343 int N, /* The number of integers to encode */
190344 u32 *a, /* The integer values */
190345 char *zBuf, /* Write the BLOB here */
190346 int *pNBuf /* Write number of bytes if zBuf[] used here */
190347){
190348 int i, j;
190349 for(i=j=0; i<N; i++){
190350 j += sqlite3Fts3PutVarint(&zBuf[j], (sqlite3_int64)a[i]);
190351 }
190352 *pNBuf = j;
190353}
190354
190355/*
190356** Decode a blob of varints into N integers
190357*/
190358static void fts3DecodeIntArray(
190359 int N, /* The number of integers to decode */
190360 u32 *a, /* Write the integer values */
190361 const char *zBuf, /* The BLOB containing the varints */
190362 int nBuf /* size of the BLOB */
190363){
190364 int i = 0;
190365 if( nBuf && (zBuf[nBuf-1]&0x80)==0 ){
190366 int j;
190367 for(i=j=0; i<N && j<nBuf; i++){
190368 sqlite3_int64 x;
190369 j += sqlite3Fts3GetVarint(&zBuf[j], &x);
190370 a[i] = (u32)(x & 0xffffffff);
190371 }
190372 }
190373 while( i<N ) a[i++] = 0;
190374}
190375
190376/*
190377** Insert the sizes (in tokens) for each column of the document
190378** with docid equal to p->iPrevDocid. The sizes are encoded as
190379** a blob of varints.
190380*/
190381static void fts3InsertDocsize(
190382 int *pRC, /* Result code */
190383 Fts3Table *p, /* Table into which to insert */
190384 u32 *aSz /* Sizes of each column, in tokens */
190385){
190386 char *pBlob; /* The BLOB encoding of the document size */
190387 int nBlob; /* Number of bytes in the BLOB */
190388 sqlite3_stmt *pStmt; /* Statement used to insert the encoding */
190389 int rc; /* Result code from subfunctions */
190390
190391 if( *pRC ) return;
190392 pBlob = sqlite3_malloc64( 10*(sqlite3_int64)p->nColumn );
190393 if( pBlob==0 ){
190394 *pRC = SQLITE_NOMEM7;
190395 return;
190396 }
190397 fts3EncodeIntArray(p->nColumn, aSz, pBlob, &nBlob);
190398 rc = fts3SqlStmt(p, SQL_REPLACE_DOCSIZE20, &pStmt, 0);
190399 if( rc ){
190400 sqlite3_free(pBlob);
190401 *pRC = rc;
190402 return;
190403 }
190404 sqlite3_bind_int64(pStmt, 1, p->iPrevDocid);
190405 sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, sqlite3_free);
190406 sqlite3_step(pStmt);
190407 *pRC = sqlite3_reset(pStmt);
190408}
190409
190410/*
190411** Record 0 of the %_stat table contains a blob consisting of N varints,
190412** where N is the number of user defined columns in the fts3 table plus
190413** two. If nCol is the number of user defined columns, then values of the
190414** varints are set as follows:
190415**
190416** Varint 0: Total number of rows in the table.
190417**
190418** Varint 1..nCol: For each column, the total number of tokens stored in
190419** the column for all rows of the table.
190420**
190421** Varint 1+nCol: The total size, in bytes, of all text values in all
190422** columns of all rows of the table.
190423**
190424*/
190425static void fts3UpdateDocTotals(
190426 int *pRC, /* The result code */
190427 Fts3Table *p, /* Table being updated */
190428 u32 *aSzIns, /* Size increases */
190429 u32 *aSzDel, /* Size decreases */
190430 int nChng /* Change in the number of documents */
190431){
190432 char *pBlob; /* Storage for BLOB written into %_stat */
190433 int nBlob; /* Size of BLOB written into %_stat */
190434 u32 *a; /* Array of integers that becomes the BLOB */
190435 sqlite3_stmt *pStmt; /* Statement for reading and writing */
190436 int i; /* Loop counter */
190437 int rc; /* Result code from subfunctions */
190438
190439 const int nStat = p->nColumn+2;
190440
190441 if( *pRC ) return;
190442 a = sqlite3_malloc64( (sizeof(u32)+10)*(sqlite3_int64)nStat );
190443 if( a==0 ){
190444 *pRC = SQLITE_NOMEM7;
190445 return;
190446 }
190447 pBlob = (char*)&a[nStat];
190448 rc = fts3SqlStmt(p, SQL_SELECT_STAT22, &pStmt, 0);
190449 if( rc ){
190450 sqlite3_free(a);
190451 *pRC = rc;
190452 return;
190453 }
190454 sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL0);
190455 if( sqlite3_step(pStmt)==SQLITE_ROW100 ){
190456 fts3DecodeIntArray(nStat, a,
190457 sqlite3_column_blob(pStmt, 0),
190458 sqlite3_column_bytes(pStmt, 0));
190459 }else{
190460 memset(a, 0, sizeof(u32)*(nStat) );
190461 }
190462 rc = sqlite3_reset(pStmt);
190463 if( rc!=SQLITE_OK0 ){
190464 sqlite3_free(a);
190465 *pRC = rc;
190466 return;
190467 }
190468 if( nChng<0 && a[0]<(u32)(-nChng) ){
190469 a[0] = 0;
190470 }else{
190471 a[0] += nChng;
190472 }
190473 for(i=0; i<p->nColumn+1; i++){
190474 u32 x = a[i+1];
190475 if( x+aSzIns[i] < aSzDel[i] ){
190476 x = 0;
190477 }else{
190478 x = x + aSzIns[i] - aSzDel[i];
190479 }
190480 a[i+1] = x;
190481 }
190482 fts3EncodeIntArray(nStat, a, pBlob, &nBlob);
190483 rc = fts3SqlStmt(p, SQL_REPLACE_STAT23, &pStmt, 0);
190484 if( rc ){
190485 sqlite3_free(a);
190486 *pRC = rc;
190487 return;
190488 }
190489 sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL0);
190490 sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC((sqlite3_destructor_type)0));
190491 sqlite3_step(pStmt);
190492 *pRC = sqlite3_reset(pStmt);
190493 sqlite3_bind_null(pStmt, 2);
190494 sqlite3_free(a);
190495}
190496
190497/*
190498** Merge the entire database so that there is one segment for each
190499** iIndex/iLangid combination.
190500*/
190501static int fts3DoOptimize(Fts3Table *p, int bReturnDone){
190502 int bSeenDone = 0;
190503 int rc;
190504 sqlite3_stmt *pAllLangid = 0;
190505
190506 rc = sqlite3Fts3PendingTermsFlush(p);
190507 if( rc==SQLITE_OK0 ){
190508 rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID27, &pAllLangid, 0);
190509 }
190510 if( rc==SQLITE_OK0 ){
190511 int rc2;
190512 sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
190513 sqlite3_bind_int(pAllLangid, 2, p->nIndex);
190514 while( sqlite3_step(pAllLangid)==SQLITE_ROW100 ){
190515 int i;
190516 int iLangid = sqlite3_column_int(pAllLangid, 0);
190517 for(i=0; rc==SQLITE_OK0 && i<p->nIndex; i++){
190518 rc = fts3SegmentMerge(p, iLangid, i, FTS3_SEGCURSOR_ALL-2);
190519 if( rc==SQLITE_DONE101 ){
190520 bSeenDone = 1;
190521 rc = SQLITE_OK0;
190522 }
190523 }
190524 }
190525 rc2 = sqlite3_reset(pAllLangid);
190526 if( rc==SQLITE_OK0 ) rc = rc2;
190527 }
190528
190529 sqlite3Fts3SegmentsClose(p);
190530
190531 return (rc==SQLITE_OK0 && bReturnDone && bSeenDone) ? SQLITE_DONE101 : rc;
190532}
190533
190534/*
190535** This function is called when the user executes the following statement:
190536**
190537** INSERT INTO <tbl>(<tbl>) VALUES('rebuild');
190538**
190539** The entire FTS index is discarded and rebuilt. If the table is one
190540** created using the content=xxx option, then the new index is based on
190541** the current contents of the xxx table. Otherwise, it is rebuilt based
190542** on the contents of the %_content table.
190543*/
190544static int fts3DoRebuild(Fts3Table *p){
190545 int rc; /* Return Code */
190546
190547 rc = fts3DeleteAll(p, 0);
190548 if( rc==SQLITE_OK0 ){
190549 u32 *aSz = 0;
190550 u32 *aSzIns = 0;
190551 u32 *aSzDel = 0;
190552 sqlite3_stmt *pStmt = 0;
190553 int nEntry = 0;
190554
190555 /* Compose and prepare an SQL statement to loop through the content table */
190556 char *zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
190557 if( !zSql ){
190558 rc = SQLITE_NOMEM7;
190559 }else{
190560 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
190561 sqlite3_free(zSql);
190562 }
190563
190564 if( rc==SQLITE_OK0 ){
190565 sqlite3_int64 nByte = sizeof(u32) * ((sqlite3_int64)p->nColumn+1)*3;
190566 aSz = (u32 *)sqlite3_malloc64(nByte);
190567 if( aSz==0 ){
190568 rc = SQLITE_NOMEM7;
190569 }else{
190570 memset(aSz, 0, nByte);
190571 aSzIns = &aSz[p->nColumn+1];
190572 aSzDel = &aSzIns[p->nColumn+1];
190573 }
190574 }
190575
190576 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pStmt) ){
190577 int iCol;
190578 int iLangid = langidFromSelect(p, pStmt);
190579 rc = fts3PendingTermsDocid(p, 0, iLangid, sqlite3_column_int64(pStmt, 0));
190580 memset(aSz, 0, sizeof(aSz[0]) * (p->nColumn+1));
190581 for(iCol=0; rc==SQLITE_OK0 && iCol<p->nColumn; iCol++){
190582 if( p->abNotindexed[iCol]==0 ){
190583 const char *z = (const char *) sqlite3_column_text(pStmt, iCol+1);
190584 rc = fts3PendingTermsAdd(p, iLangid, z, iCol, &aSz[iCol]);
190585 aSz[p->nColumn] += sqlite3_column_bytes(pStmt, iCol+1);
190586 }
190587 }
190588 if( p->bHasDocsize ){
190589 fts3InsertDocsize(&rc, p, aSz);
190590 }
190591 if( rc!=SQLITE_OK0 ){
190592 sqlite3_finalize(pStmt);
190593 pStmt = 0;
190594 }else{
190595 nEntry++;
190596 for(iCol=0; iCol<=p->nColumn; iCol++){
190597 aSzIns[iCol] += aSz[iCol];
190598 }
190599 }
190600 }
190601 if( p->bFts4 ){
190602 fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nEntry);
190603 }
190604 sqlite3_free(aSz);
190605
190606 if( pStmt ){
190607 int rc2 = sqlite3_finalize(pStmt);
190608 if( rc==SQLITE_OK0 ){
190609 rc = rc2;
190610 }
190611 }
190612 }
190613
190614 return rc;
190615}
190616
190617
190618/*
190619** This function opens a cursor used to read the input data for an
190620** incremental merge operation. Specifically, it opens a cursor to scan
190621** the oldest nSeg segments (idx=0 through idx=(nSeg-1)) in absolute
190622** level iAbsLevel.
190623*/
190624static int fts3IncrmergeCsr(
190625 Fts3Table *p, /* FTS3 table handle */
190626 sqlite3_int64 iAbsLevel, /* Absolute level to open */
190627 int nSeg, /* Number of segments to merge */
190628 Fts3MultiSegReader *pCsr /* Cursor object to populate */
190629){
190630 int rc; /* Return Code */
190631 sqlite3_stmt *pStmt = 0; /* Statement used to read %_segdir entry */
190632 sqlite3_int64 nByte; /* Bytes allocated at pCsr->apSegment[] */
190633
190634 /* Allocate space for the Fts3MultiSegReader.aCsr[] array */
190635 memset(pCsr, 0, sizeof(*pCsr));
190636 nByte = sizeof(Fts3SegReader *) * nSeg;
190637 pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc64(nByte);
190638
190639 if( pCsr->apSegment==0 ){
190640 rc = SQLITE_NOMEM7;
190641 }else{
190642 memset(pCsr->apSegment, 0, nByte);
190643 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL12, &pStmt, 0);
190644 }
190645 if( rc==SQLITE_OK0 ){
190646 int i;
190647 int rc2;
190648 sqlite3_bind_int64(pStmt, 1, iAbsLevel);
190649 assert( pCsr->nSegment==0 )((void) (0));
190650 for(i=0; rc==SQLITE_OK0 && sqlite3_step(pStmt)==SQLITE_ROW100 && i<nSeg; i++){
190651 rc = sqlite3Fts3SegReaderNew(i, 0,
190652 sqlite3_column_int64(pStmt, 1), /* segdir.start_block */
190653 sqlite3_column_int64(pStmt, 2), /* segdir.leaves_end_block */
190654 sqlite3_column_int64(pStmt, 3), /* segdir.end_block */
190655 sqlite3_column_blob(pStmt, 4), /* segdir.root */
190656 sqlite3_column_bytes(pStmt, 4), /* segdir.root */
190657 &pCsr->apSegment[i]
190658 );
190659 pCsr->nSegment++;
190660 }
190661 rc2 = sqlite3_reset(pStmt);
190662 if( rc==SQLITE_OK0 ) rc = rc2;
190663 }
190664
190665 return rc;
190666}
190667
190668typedef struct IncrmergeWriter IncrmergeWriter;
190669typedef struct NodeWriter NodeWriter;
190670typedef struct Blob Blob;
190671typedef struct NodeReader NodeReader;
190672
190673/*
190674** An instance of the following structure is used as a dynamic buffer
190675** to build up nodes or other blobs of data in.
190676**
190677** The function blobGrowBuffer() is used to extend the allocation.
190678*/
190679struct Blob {
190680 char *a; /* Pointer to allocation */
190681 int n; /* Number of valid bytes of data in a[] */
190682 int nAlloc; /* Allocated size of a[] (nAlloc>=n) */
190683};
190684
190685/*
190686** This structure is used to build up buffers containing segment b-tree
190687** nodes (blocks).
190688*/
190689struct NodeWriter {
190690 sqlite3_int64 iBlock; /* Current block id */
190691 Blob key; /* Last key written to the current block */
190692 Blob block; /* Current block image */
190693};
190694
190695/*
190696** An object of this type contains the state required to create or append
190697** to an appendable b-tree segment.
190698*/
190699struct IncrmergeWriter {
190700 int nLeafEst; /* Space allocated for leaf blocks */
190701 int nWork; /* Number of leaf pages flushed */
190702 sqlite3_int64 iAbsLevel; /* Absolute level of input segments */
190703 int iIdx; /* Index of *output* segment in iAbsLevel+1 */
190704 sqlite3_int64 iStart; /* Block number of first allocated block */
190705 sqlite3_int64 iEnd; /* Block number of last allocated block */
190706 sqlite3_int64 nLeafData; /* Bytes of leaf page data so far */
190707 u8 bNoLeafData; /* If true, store 0 for segment size */
190708 NodeWriter aNodeWriter[FTS_MAX_APPENDABLE_HEIGHT16];
190709};
190710
190711/*
190712** An object of the following type is used to read data from a single
190713** FTS segment node. See the following functions:
190714**
190715** nodeReaderInit()
190716** nodeReaderNext()
190717** nodeReaderRelease()
190718*/
190719struct NodeReader {
190720 const char *aNode;
190721 int nNode;
190722 int iOff; /* Current offset within aNode[] */
190723
190724 /* Output variables. Containing the current node entry. */
190725 sqlite3_int64 iChild; /* Pointer to child node */
190726 Blob term; /* Current term */
190727 const char *aDoclist; /* Pointer to doclist */
190728 int nDoclist; /* Size of doclist in bytes */
190729};
190730
190731/*
190732** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
190733** Otherwise, if the allocation at pBlob->a is not already at least nMin
190734** bytes in size, extend (realloc) it to be so.
190735**
190736** If an OOM error occurs, set *pRc to SQLITE_NOMEM and leave pBlob->a
190737** unmodified. Otherwise, if the allocation succeeds, update pBlob->nAlloc
190738** to reflect the new size of the pBlob->a[] buffer.
190739*/
190740static void blobGrowBuffer(Blob *pBlob, int nMin, int *pRc){
190741 if( *pRc==SQLITE_OK0 && nMin>pBlob->nAlloc ){
190742 int nAlloc = nMin;
190743 char *a = (char *)sqlite3_realloc(pBlob->a, nAlloc);
190744 if( a ){
190745 pBlob->nAlloc = nAlloc;
190746 pBlob->a = a;
190747 }else{
190748 *pRc = SQLITE_NOMEM7;
190749 }
190750 }
190751}
190752
190753/*
190754** Attempt to advance the node-reader object passed as the first argument to
190755** the next entry on the node.
190756**
190757** Return an error code if an error occurs (SQLITE_NOMEM is possible).
190758** Otherwise return SQLITE_OK. If there is no next entry on the node
190759** (e.g. because the current entry is the last) set NodeReader->aNode to
190760** NULL to indicate EOF. Otherwise, populate the NodeReader structure output
190761** variables for the new entry.
190762*/
190763static int nodeReaderNext(NodeReader *p){
190764 int bFirst = (p->term.n==0); /* True for first term on the node */
190765 int nPrefix = 0; /* Bytes to copy from previous term */
190766 int nSuffix = 0; /* Bytes to append to the prefix */
190767 int rc = SQLITE_OK0; /* Return code */
190768
190769 assert( p->aNode )((void) (0));
190770 if( p->iChild && bFirst==0 ) p->iChild++;
190771 if( p->iOff>=p->nNode ){
190772 /* EOF */
190773 p->aNode = 0;
190774 }else{
190775 if( bFirst==0 ){
190776 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) )
;
190777 }
190778 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) )
;
190779
190780 if( nPrefix>p->term.n || nSuffix>p->nNode-p->iOff || nSuffix==0 ){
190781 return FTS_CORRUPT_VTAB(11 | (1<<8));
190782 }
190783 blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc);
190784 if( rc==SQLITE_OK0 && ALWAYS(p->term.a!=0)(p->term.a!=0) ){
190785 memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix);
190786 p->term.n = nPrefix+nSuffix;
190787 p->iOff += nSuffix;
190788 if( p->iChild==0 ){
190789 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) )
;
190790 if( (p->nNode-p->iOff)<p->nDoclist ){
190791 return FTS_CORRUPT_VTAB(11 | (1<<8));
190792 }
190793 p->aDoclist = &p->aNode[p->iOff];
190794 p->iOff += p->nDoclist;
190795 }
190796 }
190797 }
190798
190799 assert_fts3_nc( p->iOff<=p->nNode )((void) (0));
190800 return rc;
190801}
190802
190803/*
190804** Release all dynamic resources held by node-reader object *p.
190805*/
190806static void nodeReaderRelease(NodeReader *p){
190807 sqlite3_free(p->term.a);
190808}
190809
190810/*
190811** Initialize a node-reader object to read the node in buffer aNode/nNode.
190812**
190813** If successful, SQLITE_OK is returned and the NodeReader object set to
190814** point to the first entry on the node (if any). Otherwise, an SQLite
190815** error code is returned.
190816*/
190817static int nodeReaderInit(NodeReader *p, const char *aNode, int nNode){
190818 memset(p, 0, sizeof(NodeReader));
190819 p->aNode = aNode;
190820 p->nNode = nNode;
190821
190822 /* Figure out if this is a leaf or an internal node. */
190823 if( aNode && aNode[0] ){
190824 /* An internal node. */
190825 p->iOff = 1 + sqlite3Fts3GetVarint(&p->aNode[1], &p->iChild);
190826 }else{
190827 p->iOff = 1;
190828 }
190829
190830 return aNode ? nodeReaderNext(p) : SQLITE_OK0;
190831}
190832
190833/*
190834** This function is called while writing an FTS segment each time a leaf o
190835** node is finished and written to disk. The key (zTerm/nTerm) is guaranteed
190836** to be greater than the largest key on the node just written, but smaller
190837** than or equal to the first key that will be written to the next leaf
190838** node.
190839**
190840** The block id of the leaf node just written to disk may be found in
190841** (pWriter->aNodeWriter[0].iBlock) when this function is called.
190842*/
190843static int fts3IncrmergePush(
190844 Fts3Table *p, /* Fts3 table handle */
190845 IncrmergeWriter *pWriter, /* Writer object */
190846 const char *zTerm, /* Term to write to internal node */
190847 int nTerm /* Bytes at zTerm */
190848){
190849 sqlite3_int64 iPtr = pWriter->aNodeWriter[0].iBlock;
190850 int iLayer;
190851
190852 assert( nTerm>0 )((void) (0));
190853 for(iLayer=1; ALWAYS(iLayer<FTS_MAX_APPENDABLE_HEIGHT)(iLayer<16); iLayer++){
190854 sqlite3_int64 iNextPtr = 0;
190855 NodeWriter *pNode = &pWriter->aNodeWriter[iLayer];
190856 int rc = SQLITE_OK0;
190857 int nPrefix;
190858 int nSuffix;
190859 int nSpace;
190860
190861 /* Figure out how much space the key will consume if it is written to
190862 ** the current node of layer iLayer. Due to the prefix compression,
190863 ** the space required changes depending on which node the key is to
190864 ** be added to. */
190865 nPrefix = fts3PrefixCompress(pNode->key.a, pNode->key.n, zTerm, nTerm);
190866 nSuffix = nTerm - nPrefix;
190867 if(nSuffix<=0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
190868 nSpace = sqlite3Fts3VarintLen(nPrefix);
190869 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
190870
190871 if( pNode->key.n==0 || (pNode->block.n + nSpace)<=p->nNodeSize ){
190872 /* If the current node of layer iLayer contains zero keys, or if adding
190873 ** the key to it will not cause it to grow to larger than nNodeSize
190874 ** bytes in size, write the key here. */
190875
190876 Blob *pBlk = &pNode->block;
190877 if( pBlk->n==0 ){
190878 blobGrowBuffer(pBlk, p->nNodeSize, &rc);
190879 if( rc==SQLITE_OK0 ){
190880 pBlk->a[0] = (char)iLayer;
190881 pBlk->n = 1 + sqlite3Fts3PutVarint(&pBlk->a[1], iPtr);
190882 }
190883 }
190884 blobGrowBuffer(pBlk, pBlk->n + nSpace, &rc);
190885 blobGrowBuffer(&pNode->key, nTerm, &rc);
190886
190887 if( rc==SQLITE_OK0 ){
190888 if( pNode->key.n ){
190889 pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nPrefix);
190890 }
190891 pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nSuffix);
190892 assert( nPrefix+nSuffix<=nTerm )((void) (0));
190893 assert( nPrefix>=0 )((void) (0));
190894 memcpy(&pBlk->a[pBlk->n], &zTerm[nPrefix], nSuffix);
190895 pBlk->n += nSuffix;
190896
190897 memcpy(pNode->key.a, zTerm, nTerm);
190898 pNode->key.n = nTerm;
190899 }
190900 }else{
190901 /* Otherwise, flush the current node of layer iLayer to disk.
190902 ** Then allocate a new, empty sibling node. The key will be written
190903 ** into the parent of this node. */
190904 rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
190905
190906 assert( pNode->block.nAlloc>=p->nNodeSize )((void) (0));
190907 pNode->block.a[0] = (char)iLayer;
190908 pNode->block.n = 1 + sqlite3Fts3PutVarint(&pNode->block.a[1], iPtr+1);
190909
190910 iNextPtr = pNode->iBlock;
190911 pNode->iBlock++;
190912 pNode->key.n = 0;
190913 }
190914
190915 if( rc!=SQLITE_OK0 || iNextPtr==0 ) return rc;
190916 iPtr = iNextPtr;
190917 }
190918
190919 assert( 0 )((void) (0));
190920 return 0;
190921}
190922
190923/*
190924** Append a term and (optionally) doclist to the FTS segment node currently
190925** stored in blob *pNode. The node need not contain any terms, but the
190926** header must be written before this function is called.
190927**
190928** A node header is a single 0x00 byte for a leaf node, or a height varint
190929** followed by the left-hand-child varint for an internal node.
190930**
190931** The term to be appended is passed via arguments zTerm/nTerm. For a
190932** leaf node, the doclist is passed as aDoclist/nDoclist. For an internal
190933** node, both aDoclist and nDoclist must be passed 0.
190934**
190935** If the size of the value in blob pPrev is zero, then this is the first
190936** term written to the node. Otherwise, pPrev contains a copy of the
190937** previous term. Before this function returns, it is updated to contain a
190938** copy of zTerm/nTerm.
190939**
190940** It is assumed that the buffer associated with pNode is already large
190941** enough to accommodate the new entry. The buffer associated with pPrev
190942** is extended by this function if requrired.
190943**
190944** If an error (i.e. OOM condition) occurs, an SQLite error code is
190945** returned. Otherwise, SQLITE_OK.
190946*/
190947static int fts3AppendToNode(
190948 Blob *pNode, /* Current node image to append to */
190949 Blob *pPrev, /* Buffer containing previous term written */
190950 const char *zTerm, /* New term to write */
190951 int nTerm, /* Size of zTerm in bytes */
190952 const char *aDoclist, /* Doclist (or NULL) to write */
190953 int nDoclist /* Size of aDoclist in bytes */
190954){
190955 int rc = SQLITE_OK0; /* Return code */
190956 int bFirst = (pPrev->n==0); /* True if this is the first term written */
190957 int nPrefix; /* Size of term prefix in bytes */
190958 int nSuffix; /* Size of term suffix in bytes */
190959
190960 /* Node must have already been started. There must be a doclist for a
190961 ** leaf node, and there must not be a doclist for an internal node. */
190962 assert( pNode->n>0 )((void) (0));
190963 assert_fts3_nc( (pNode->a[0]=='\0')==(aDoclist!=0) )((void) (0));
190964
190965 blobGrowBuffer(pPrev, nTerm, &rc);
190966 if( rc!=SQLITE_OK0 ) return rc;
190967
190968 nPrefix = fts3PrefixCompress(pPrev->a, pPrev->n, zTerm, nTerm);
190969 nSuffix = nTerm - nPrefix;
190970 if( nSuffix<=0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
190971 memcpy(pPrev->a, zTerm, nTerm);
190972 pPrev->n = nTerm;
190973
190974 if( bFirst==0 ){
190975 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nPrefix);
190976 }
190977 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nSuffix);
190978 memcpy(&pNode->a[pNode->n], &zTerm[nPrefix], nSuffix);
190979 pNode->n += nSuffix;
190980
190981 if( aDoclist ){
190982 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nDoclist);
190983 memcpy(&pNode->a[pNode->n], aDoclist, nDoclist);
190984 pNode->n += nDoclist;
190985 }
190986
190987 assert( pNode->n<=pNode->nAlloc )((void) (0));
190988
190989 return SQLITE_OK0;
190990}
190991
190992/*
190993** Append the current term and doclist pointed to by cursor pCsr to the
190994** appendable b-tree segment opened for writing by pWriter.
190995**
190996** Return SQLITE_OK if successful, or an SQLite error code otherwise.
190997*/
190998static int fts3IncrmergeAppend(
190999 Fts3Table *p, /* Fts3 table handle */
191000 IncrmergeWriter *pWriter, /* Writer object */
191001 Fts3MultiSegReader *pCsr /* Cursor containing term and doclist */
191002){
191003 const char *zTerm = pCsr->zTerm;
191004 int nTerm = pCsr->nTerm;
191005 const char *aDoclist = pCsr->aDoclist;
191006 int nDoclist = pCsr->nDoclist;
191007 int rc = SQLITE_OK0; /* Return code */
191008 int nSpace; /* Total space in bytes required on leaf */
191009 int nPrefix; /* Size of prefix shared with previous term */
191010 int nSuffix; /* Size of suffix (nTerm - nPrefix) */
191011 NodeWriter *pLeaf; /* Object used to write leaf nodes */
191012
191013 pLeaf = &pWriter->aNodeWriter[0];
191014 nPrefix = fts3PrefixCompress(pLeaf->key.a, pLeaf->key.n, zTerm, nTerm);
191015 nSuffix = nTerm - nPrefix;
191016 if(nSuffix<=0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
191017
191018 nSpace = sqlite3Fts3VarintLen(nPrefix);
191019 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
191020 nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
191021
191022 /* If the current block is not empty, and if adding this term/doclist
191023 ** to the current block would make it larger than Fts3Table.nNodeSize
191024 ** bytes, write this block out to the database. */
191025 if( pLeaf->block.n>0 && (pLeaf->block.n + nSpace)>p->nNodeSize ){
191026 rc = fts3WriteSegment(p, pLeaf->iBlock, pLeaf->block.a, pLeaf->block.n);
191027 pWriter->nWork++;
191028
191029 /* Add the current term to the parent node. The term added to the
191030 ** parent must:
191031 **
191032 ** a) be greater than the largest term on the leaf node just written
191033 ** to the database (still available in pLeaf->key), and
191034 **
191035 ** b) be less than or equal to the term about to be added to the new
191036 ** leaf node (zTerm/nTerm).
191037 **
191038 ** In other words, it must be the prefix of zTerm 1 byte longer than
191039 ** the common prefix (if any) of zTerm and pWriter->zTerm.
191040 */
191041 if( rc==SQLITE_OK0 ){
191042 rc = fts3IncrmergePush(p, pWriter, zTerm, nPrefix+1);
191043 }
191044
191045 /* Advance to the next output block */
191046 pLeaf->iBlock++;
191047 pLeaf->key.n = 0;
191048 pLeaf->block.n = 0;
191049
191050 nSuffix = nTerm;
191051 nSpace = 1;
191052 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
191053 nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
191054 }
191055
191056 pWriter->nLeafData += nSpace;
191057 blobGrowBuffer(&pLeaf->block, pLeaf->block.n + nSpace, &rc);
191058 if( rc==SQLITE_OK0 ){
191059 if( pLeaf->block.n==0 ){
191060 pLeaf->block.n = 1;
191061 pLeaf->block.a[0] = '\0';
191062 }
191063 rc = fts3AppendToNode(
191064 &pLeaf->block, &pLeaf->key, zTerm, nTerm, aDoclist, nDoclist
191065 );
191066 }
191067
191068 return rc;
191069}
191070
191071/*
191072** This function is called to release all dynamic resources held by the
191073** merge-writer object pWriter, and if no error has occurred, to flush
191074** all outstanding node buffers held by pWriter to disk.
191075**
191076** If *pRc is not SQLITE_OK when this function is called, then no attempt
191077** is made to write any data to disk. Instead, this function serves only
191078** to release outstanding resources.
191079**
191080** Otherwise, if *pRc is initially SQLITE_OK and an error occurs while
191081** flushing buffers to disk, *pRc is set to an SQLite error code before
191082** returning.
191083*/
191084static void fts3IncrmergeRelease(
191085 Fts3Table *p, /* FTS3 table handle */
191086 IncrmergeWriter *pWriter, /* Merge-writer object */
191087 int *pRc /* IN/OUT: Error code */
191088){
191089 int i; /* Used to iterate through non-root layers */
191090 int iRoot; /* Index of root in pWriter->aNodeWriter */
191091 NodeWriter *pRoot; /* NodeWriter for root node */
191092 int rc = *pRc; /* Error code */
191093
191094 /* Set iRoot to the index in pWriter->aNodeWriter[] of the output segment
191095 ** root node. If the segment fits entirely on a single leaf node, iRoot
191096 ** will be set to 0. If the root node is the parent of the leaves, iRoot
191097 ** will be 1. And so on. */
191098 for(iRoot=FTS_MAX_APPENDABLE_HEIGHT16-1; iRoot>=0; iRoot--){
191099 NodeWriter *pNode = &pWriter->aNodeWriter[iRoot];
191100 if( pNode->block.n>0 ) break;
191101 assert( *pRc || pNode->block.nAlloc==0 )((void) (0));
191102 assert( *pRc || pNode->key.nAlloc==0 )((void) (0));
191103 sqlite3_free(pNode->block.a);
191104 sqlite3_free(pNode->key.a);
191105 }
191106
191107 /* Empty output segment. This is a no-op. */
191108 if( iRoot<0 ) return;
191109
191110 /* The entire output segment fits on a single node. Normally, this means
191111 ** the node would be stored as a blob in the "root" column of the %_segdir
191112 ** table. However, this is not permitted in this case. The problem is that
191113 ** space has already been reserved in the %_segments table, and so the
191114 ** start_block and end_block fields of the %_segdir table must be populated.
191115 ** And, by design or by accident, released versions of FTS cannot handle
191116 ** segments that fit entirely on the root node with start_block!=0.
191117 **
191118 ** Instead, create a synthetic root node that contains nothing but a
191119 ** pointer to the single content node. So that the segment consists of a
191120 ** single leaf and a single interior (root) node.
191121 **
191122 ** Todo: Better might be to defer allocating space in the %_segments
191123 ** table until we are sure it is needed.
191124 */
191125 if( iRoot==0 ){
191126 Blob *pBlock = &pWriter->aNodeWriter[1].block;
191127 blobGrowBuffer(pBlock, 1 + FTS3_VARINT_MAX10, &rc);
191128 if( rc==SQLITE_OK0 ){
191129 pBlock->a[0] = 0x01;
191130 pBlock->n = 1 + sqlite3Fts3PutVarint(
191131 &pBlock->a[1], pWriter->aNodeWriter[0].iBlock
191132 );
191133 }
191134 iRoot = 1;
191135 }
191136 pRoot = &pWriter->aNodeWriter[iRoot];
191137
191138 /* Flush all currently outstanding nodes to disk. */
191139 for(i=0; i<iRoot; i++){
191140 NodeWriter *pNode = &pWriter->aNodeWriter[i];
191141 if( pNode->block.n>0 && rc==SQLITE_OK0 ){
191142 rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
191143 }
191144 sqlite3_free(pNode->block.a);
191145 sqlite3_free(pNode->key.a);
191146 }
191147
191148 /* Write the %_segdir record. */
191149 if( rc==SQLITE_OK0 ){
191150 rc = fts3WriteSegdir(p,
191151 pWriter->iAbsLevel+1, /* level */
191152 pWriter->iIdx, /* idx */
191153 pWriter->iStart, /* start_block */
191154 pWriter->aNodeWriter[0].iBlock, /* leaves_end_block */
191155 pWriter->iEnd, /* end_block */
191156 (pWriter->bNoLeafData==0 ? pWriter->nLeafData : 0), /* end_block */
191157 pRoot->block.a, pRoot->block.n /* root */
191158 );
191159 }
191160 sqlite3_free(pRoot->block.a);
191161 sqlite3_free(pRoot->key.a);
191162
191163 *pRc = rc;
191164}
191165
191166/*
191167** Compare the term in buffer zLhs (size in bytes nLhs) with that in
191168** zRhs (size in bytes nRhs) using memcmp. If one term is a prefix of
191169** the other, it is considered to be smaller than the other.
191170**
191171** Return -ve if zLhs is smaller than zRhs, 0 if it is equal, or +ve
191172** if it is greater.
191173*/
191174static int fts3TermCmp(
191175 const char *zLhs, int nLhs, /* LHS of comparison */
191176 const char *zRhs, int nRhs /* RHS of comparison */
191177){
191178 int nCmp = MIN(nLhs, nRhs)((nLhs)<(nRhs)?(nLhs):(nRhs));
191179 int res;
191180
191181 if( nCmp && ALWAYS(zLhs)(zLhs) && ALWAYS(zRhs)(zRhs) ){
191182 res = memcmp(zLhs, zRhs, nCmp);
191183 }else{
191184 res = 0;
191185 }
191186 if( res==0 ) res = nLhs - nRhs;
191187
191188 return res;
191189}
191190
191191
191192/*
191193** Query to see if the entry in the %_segments table with blockid iEnd is
191194** NULL. If no error occurs and the entry is NULL, set *pbRes 1 before
191195** returning. Otherwise, set *pbRes to 0.
191196**
191197** Or, if an error occurs while querying the database, return an SQLite
191198** error code. The final value of *pbRes is undefined in this case.
191199**
191200** This is used to test if a segment is an "appendable" segment. If it
191201** is, then a NULL entry has been inserted into the %_segments table
191202** with blockid %_segdir.end_block.
191203*/
191204static int fts3IsAppendable(Fts3Table *p, sqlite3_int64 iEnd, int *pbRes){
191205 int bRes = 0; /* Result to set *pbRes to */
191206 sqlite3_stmt *pCheck = 0; /* Statement to query database with */
191207 int rc; /* Return code */
191208
191209 rc = fts3SqlStmt(p, SQL_SEGMENT_IS_APPENDABLE34, &pCheck, 0);
191210 if( rc==SQLITE_OK0 ){
191211 sqlite3_bind_int64(pCheck, 1, iEnd);
191212 if( SQLITE_ROW100==sqlite3_step(pCheck) ) bRes = 1;
191213 rc = sqlite3_reset(pCheck);
191214 }
191215
191216 *pbRes = bRes;
191217 return rc;
191218}
191219
191220/*
191221** This function is called when initializing an incremental-merge operation.
191222** It checks if the existing segment with index value iIdx at absolute level
191223** (iAbsLevel+1) can be appended to by the incremental merge. If it can, the
191224** merge-writer object *pWriter is initialized to write to it.
191225**
191226** An existing segment can be appended to by an incremental merge if:
191227**
191228** * It was initially created as an appendable segment (with all required
191229** space pre-allocated), and
191230**
191231** * The first key read from the input (arguments zKey and nKey) is
191232** greater than the largest key currently stored in the potential
191233** output segment.
191234*/
191235static int fts3IncrmergeLoad(
191236 Fts3Table *p, /* Fts3 table handle */
191237 sqlite3_int64 iAbsLevel, /* Absolute level of input segments */
191238 int iIdx, /* Index of candidate output segment */
191239 const char *zKey, /* First key to write */
191240 int nKey, /* Number of bytes in nKey */
191241 IncrmergeWriter *pWriter /* Populate this object */
191242){
191243 int rc; /* Return code */
191244 sqlite3_stmt *pSelect = 0; /* SELECT to read %_segdir entry */
191245
191246 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR32, &pSelect, 0);
191247 if( rc==SQLITE_OK0 ){
191248 sqlite3_int64 iStart = 0; /* Value of %_segdir.start_block */
191249 sqlite3_int64 iLeafEnd = 0; /* Value of %_segdir.leaves_end_block */
191250 sqlite3_int64 iEnd = 0; /* Value of %_segdir.end_block */
191251 const char *aRoot = 0; /* Pointer to %_segdir.root buffer */
191252 int nRoot = 0; /* Size of aRoot[] in bytes */
191253 int rc2; /* Return code from sqlite3_reset() */
191254 int bAppendable = 0; /* Set to true if segment is appendable */
191255
191256 /* Read the %_segdir entry for index iIdx absolute level (iAbsLevel+1) */
191257 sqlite3_bind_int64(pSelect, 1, iAbsLevel+1);
191258 sqlite3_bind_int(pSelect, 2, iIdx);
191259 if( sqlite3_step(pSelect)==SQLITE_ROW100 ){
191260 iStart = sqlite3_column_int64(pSelect, 1);
191261 iLeafEnd = sqlite3_column_int64(pSelect, 2);
191262 fts3ReadEndBlockField(pSelect, 3, &iEnd, &pWriter->nLeafData);
191263 if( pWriter->nLeafData<0 ){
191264 pWriter->nLeafData = pWriter->nLeafData * -1;
191265 }
191266 pWriter->bNoLeafData = (pWriter->nLeafData==0);
191267 nRoot = sqlite3_column_bytes(pSelect, 4);
191268 aRoot = sqlite3_column_blob(pSelect, 4);
191269 if( aRoot==0 ){
191270 sqlite3_reset(pSelect);
191271 return nRoot ? SQLITE_NOMEM7 : FTS_CORRUPT_VTAB(11 | (1<<8));
191272 }
191273 }else{
191274 return sqlite3_reset(pSelect);
191275 }
191276
191277 /* Check for the zero-length marker in the %_segments table */
191278 rc = fts3IsAppendable(p, iEnd, &bAppendable);
191279
191280 /* Check that zKey/nKey is larger than the largest key the candidate */
191281 if( rc==SQLITE_OK0 && bAppendable ){
191282 char *aLeaf = 0;
191283 int nLeaf = 0;
191284
191285 rc = sqlite3Fts3ReadBlock(p, iLeafEnd, &aLeaf, &nLeaf, 0);
191286 if( rc==SQLITE_OK0 ){
191287 NodeReader reader;
191288 for(rc = nodeReaderInit(&reader, aLeaf, nLeaf);
191289 rc==SQLITE_OK0 && reader.aNode;
191290 rc = nodeReaderNext(&reader)
191291 ){
191292 assert( reader.aNode )((void) (0));
191293 }
191294 if( fts3TermCmp(zKey, nKey, reader.term.a, reader.term.n)<=0 ){
191295 bAppendable = 0;
191296 }
191297 nodeReaderRelease(&reader);
191298 }
191299 sqlite3_free(aLeaf);
191300 }
191301
191302 if( rc==SQLITE_OK0 && bAppendable ){
191303 /* It is possible to append to this segment. Set up the IncrmergeWriter
191304 ** object to do so. */
191305 int i;
191306 int nHeight = (int)aRoot[0];
191307 NodeWriter *pNode;
191308 if( nHeight<1 || nHeight>=FTS_MAX_APPENDABLE_HEIGHT16 ){
191309 sqlite3_reset(pSelect);
191310 return FTS_CORRUPT_VTAB(11 | (1<<8));
191311 }
191312
191313 pWriter->nLeafEst = (int)((iEnd - iStart) + 1)/FTS_MAX_APPENDABLE_HEIGHT16;
191314 pWriter->iStart = iStart;
191315 pWriter->iEnd = iEnd;
191316 pWriter->iAbsLevel = iAbsLevel;
191317 pWriter->iIdx = iIdx;
191318
191319 for(i=nHeight+1; i<FTS_MAX_APPENDABLE_HEIGHT16; i++){
191320 pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
191321 }
191322
191323 pNode = &pWriter->aNodeWriter[nHeight];
191324 pNode->iBlock = pWriter->iStart + pWriter->nLeafEst*nHeight;
191325 blobGrowBuffer(&pNode->block,
191326 MAX(nRoot, p->nNodeSize)((nRoot)>(p->nNodeSize)?(nRoot):(p->nNodeSize))+FTS3_NODE_PADDING(10*2), &rc
191327 );
191328 if( rc==SQLITE_OK0 ){
191329 memcpy(pNode->block.a, aRoot, nRoot);
191330 pNode->block.n = nRoot;
191331 memset(&pNode->block.a[nRoot], 0, FTS3_NODE_PADDING(10*2));
191332 }
191333
191334 for(i=nHeight; i>=0 && rc==SQLITE_OK0; i--){
191335 NodeReader reader;
191336 pNode = &pWriter->aNodeWriter[i];
191337
191338 if( pNode->block.a){
191339 rc = nodeReaderInit(&reader, pNode->block.a, pNode->block.n);
191340 while( reader.aNode && rc==SQLITE_OK0 ) rc = nodeReaderNext(&reader);
191341 blobGrowBuffer(&pNode->key, reader.term.n, &rc);
191342 if( rc==SQLITE_OK0 ){
191343 assert_fts3_nc( reader.term.n>0 || reader.aNode==0 )((void) (0));
191344 if( reader.term.n>0 ){
191345 memcpy(pNode->key.a, reader.term.a, reader.term.n);
191346 }
191347 pNode->key.n = reader.term.n;
191348 if( i>0 ){
191349 char *aBlock = 0;
191350 int nBlock = 0;
191351 pNode = &pWriter->aNodeWriter[i-1];
191352 pNode->iBlock = reader.iChild;
191353 rc = sqlite3Fts3ReadBlock(p, reader.iChild, &aBlock, &nBlock,0);
191354 blobGrowBuffer(&pNode->block,
191355 MAX(nBlock, p->nNodeSize)((nBlock)>(p->nNodeSize)?(nBlock):(p->nNodeSize))+FTS3_NODE_PADDING(10*2), &rc
191356 );
191357 if( rc==SQLITE_OK0 ){
191358 memcpy(pNode->block.a, aBlock, nBlock);
191359 pNode->block.n = nBlock;
191360 memset(&pNode->block.a[nBlock], 0, FTS3_NODE_PADDING(10*2));
191361 }
191362 sqlite3_free(aBlock);
191363 }
191364 }
191365 }
191366 nodeReaderRelease(&reader);
191367 }
191368 }
191369
191370 rc2 = sqlite3_reset(pSelect);
191371 if( rc==SQLITE_OK0 ) rc = rc2;
191372 }
191373
191374 return rc;
191375}
191376
191377/*
191378** Determine the largest segment index value that exists within absolute
191379** level iAbsLevel+1. If no error occurs, set *piIdx to this value plus
191380** one before returning SQLITE_OK. Or, if there are no segments at all
191381** within level iAbsLevel, set *piIdx to zero.
191382**
191383** If an error occurs, return an SQLite error code. The final value of
191384** *piIdx is undefined in this case.
191385*/
191386static int fts3IncrmergeOutputIdx(
191387 Fts3Table *p, /* FTS Table handle */
191388 sqlite3_int64 iAbsLevel, /* Absolute index of input segments */
191389 int *piIdx /* OUT: Next free index at iAbsLevel+1 */
191390){
191391 int rc;
191392 sqlite3_stmt *pOutputIdx = 0; /* SQL used to find output index */
191393
191394 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX8, &pOutputIdx, 0);
191395 if( rc==SQLITE_OK0 ){
191396 sqlite3_bind_int64(pOutputIdx, 1, iAbsLevel+1);
191397 sqlite3_step(pOutputIdx);
191398 *piIdx = sqlite3_column_int(pOutputIdx, 0);
191399 rc = sqlite3_reset(pOutputIdx);
191400 }
191401
191402 return rc;
191403}
191404
191405/*
191406** Allocate an appendable output segment on absolute level iAbsLevel+1
191407** with idx value iIdx.
191408**
191409** In the %_segdir table, a segment is defined by the values in three
191410** columns:
191411**
191412** start_block
191413** leaves_end_block
191414** end_block
191415**
191416** When an appendable segment is allocated, it is estimated that the
191417** maximum number of leaf blocks that may be required is the sum of the
191418** number of leaf blocks consumed by the input segments, plus the number
191419** of input segments, multiplied by two. This value is stored in stack
191420** variable nLeafEst.
191421**
191422** A total of 16*nLeafEst blocks are allocated when an appendable segment
191423** is created ((1 + end_block - start_block)==16*nLeafEst). The contiguous
191424** array of leaf nodes starts at the first block allocated. The array
191425** of interior nodes that are parents of the leaf nodes start at block
191426** (start_block + (1 + end_block - start_block) / 16). And so on.
191427**
191428** In the actual code below, the value "16" is replaced with the
191429** pre-processor macro FTS_MAX_APPENDABLE_HEIGHT.
191430*/
191431static int fts3IncrmergeWriter(
191432 Fts3Table *p, /* Fts3 table handle */
191433 sqlite3_int64 iAbsLevel, /* Absolute level of input segments */
191434 int iIdx, /* Index of new output segment */
191435 Fts3MultiSegReader *pCsr, /* Cursor that data will be read from */
191436 IncrmergeWriter *pWriter /* Populate this object */
191437){
191438 int rc; /* Return Code */
191439 int i; /* Iterator variable */
191440 int nLeafEst = 0; /* Blocks allocated for leaf nodes */
191441 sqlite3_stmt *pLeafEst = 0; /* SQL used to determine nLeafEst */
191442 sqlite3_stmt *pFirstBlock = 0; /* SQL used to determine first block */
191443
191444 /* Calculate nLeafEst. */
191445 rc = fts3SqlStmt(p, SQL_MAX_LEAF_NODE_ESTIMATE29, &pLeafEst, 0);
191446 if( rc==SQLITE_OK0 ){
191447 sqlite3_bind_int64(pLeafEst, 1, iAbsLevel);
191448 sqlite3_bind_int64(pLeafEst, 2, pCsr->nSegment);
191449 if( SQLITE_ROW100==sqlite3_step(pLeafEst) ){
191450 nLeafEst = sqlite3_column_int(pLeafEst, 0);
191451 }
191452 rc = sqlite3_reset(pLeafEst);
191453 }
191454 if( rc!=SQLITE_OK0 ) return rc;
191455
191456 /* Calculate the first block to use in the output segment */
191457 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID10, &pFirstBlock, 0);
191458 if( rc==SQLITE_OK0 ){
191459 if( SQLITE_ROW100==sqlite3_step(pFirstBlock) ){
191460 pWriter->iStart = sqlite3_column_int64(pFirstBlock, 0);
191461 pWriter->iEnd = pWriter->iStart - 1;
191462 pWriter->iEnd += nLeafEst * FTS_MAX_APPENDABLE_HEIGHT16;
191463 }
191464 rc = sqlite3_reset(pFirstBlock);
191465 }
191466 if( rc!=SQLITE_OK0 ) return rc;
191467
191468 /* Insert the marker in the %_segments table to make sure nobody tries
191469 ** to steal the space just allocated. This is also used to identify
191470 ** appendable segments. */
191471 rc = fts3WriteSegment(p, pWriter->iEnd, 0, 0);
191472 if( rc!=SQLITE_OK0 ) return rc;
191473
191474 pWriter->iAbsLevel = iAbsLevel;
191475 pWriter->nLeafEst = nLeafEst;
191476 pWriter->iIdx = iIdx;
191477
191478 /* Set up the array of NodeWriter objects */
191479 for(i=0; i<FTS_MAX_APPENDABLE_HEIGHT16; i++){
191480 pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
191481 }
191482 return SQLITE_OK0;
191483}
191484
191485/*
191486** Remove an entry from the %_segdir table. This involves running the
191487** following two statements:
191488**
191489** DELETE FROM %_segdir WHERE level = :iAbsLevel AND idx = :iIdx
191490** UPDATE %_segdir SET idx = idx - 1 WHERE level = :iAbsLevel AND idx > :iIdx
191491**
191492** The DELETE statement removes the specific %_segdir level. The UPDATE
191493** statement ensures that the remaining segments have contiguously allocated
191494** idx values.
191495*/
191496static int fts3RemoveSegdirEntry(
191497 Fts3Table *p, /* FTS3 table handle */
191498 sqlite3_int64 iAbsLevel, /* Absolute level to delete from */
191499 int iIdx /* Index of %_segdir entry to delete */
191500){
191501 int rc; /* Return code */
191502 sqlite3_stmt *pDelete = 0; /* DELETE statement */
191503
191504 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_ENTRY30, &pDelete, 0);
191505 if( rc==SQLITE_OK0 ){
191506 sqlite3_bind_int64(pDelete, 1, iAbsLevel);
191507 sqlite3_bind_int(pDelete, 2, iIdx);
191508 sqlite3_step(pDelete);
191509 rc = sqlite3_reset(pDelete);
191510 }
191511
191512 return rc;
191513}
191514
191515/*
191516** One or more segments have just been removed from absolute level iAbsLevel.
191517** Update the 'idx' values of the remaining segments in the level so that
191518** the idx values are a contiguous sequence starting from 0.
191519*/
191520static int fts3RepackSegdirLevel(
191521 Fts3Table *p, /* FTS3 table handle */
191522 sqlite3_int64 iAbsLevel /* Absolute level to repack */
191523){
191524 int rc; /* Return code */
191525 int *aIdx = 0; /* Array of remaining idx values */
191526 int nIdx = 0; /* Valid entries in aIdx[] */
191527 int nAlloc = 0; /* Allocated size of aIdx[] */
191528 int i; /* Iterator variable */
191529 sqlite3_stmt *pSelect = 0; /* Select statement to read idx values */
191530 sqlite3_stmt *pUpdate = 0; /* Update statement to modify idx values */
191531
191532 rc = fts3SqlStmt(p, SQL_SELECT_INDEXES35, &pSelect, 0);
191533 if( rc==SQLITE_OK0 ){
191534 int rc2;
191535 sqlite3_bind_int64(pSelect, 1, iAbsLevel);
191536 while( SQLITE_ROW100==sqlite3_step(pSelect) ){
191537 if( nIdx>=nAlloc ){
191538 int *aNew;
191539 nAlloc += 16;
191540 aNew = sqlite3_realloc(aIdx, nAlloc*sizeof(int));
191541 if( !aNew ){
191542 rc = SQLITE_NOMEM7;
191543 break;
191544 }
191545 aIdx = aNew;
191546 }
191547 aIdx[nIdx++] = sqlite3_column_int(pSelect, 0);
191548 }
191549 rc2 = sqlite3_reset(pSelect);
191550 if( rc==SQLITE_OK0 ) rc = rc2;
191551 }
191552
191553 if( rc==SQLITE_OK0 ){
191554 rc = fts3SqlStmt(p, SQL_SHIFT_SEGDIR_ENTRY31, &pUpdate, 0);
191555 }
191556 if( rc==SQLITE_OK0 ){
191557 sqlite3_bind_int64(pUpdate, 2, iAbsLevel);
191558 }
191559
191560 assert( p->bIgnoreSavepoint==0 )((void) (0));
191561 p->bIgnoreSavepoint = 1;
191562 for(i=0; rc==SQLITE_OK0 && i<nIdx; i++){
191563 if( aIdx[i]!=i ){
191564 sqlite3_bind_int(pUpdate, 3, aIdx[i]);
191565 sqlite3_bind_int(pUpdate, 1, i);
191566 sqlite3_step(pUpdate);
191567 rc = sqlite3_reset(pUpdate);
191568 }
191569 }
191570 p->bIgnoreSavepoint = 0;
191571
191572 sqlite3_free(aIdx);
191573 return rc;
191574}
191575
191576static void fts3StartNode(Blob *pNode, int iHeight, sqlite3_int64 iChild){
191577 pNode->a[0] = (char)iHeight;
191578 if( iChild ){
191579 assert( pNode->nAlloc>=1+sqlite3Fts3VarintLen(iChild) )((void) (0));
191580 pNode->n = 1 + sqlite3Fts3PutVarint(&pNode->a[1], iChild);
191581 }else{
191582 assert( pNode->nAlloc>=1 )((void) (0));
191583 pNode->n = 1;
191584 }
191585}
191586
191587/*
191588** The first two arguments are a pointer to and the size of a segment b-tree
191589** node. The node may be a leaf or an internal node.
191590**
191591** This function creates a new node image in blob object *pNew by copying
191592** all terms that are greater than or equal to zTerm/nTerm (for leaf nodes)
191593** or greater than zTerm/nTerm (for internal nodes) from aNode/nNode.
191594*/
191595static int fts3TruncateNode(
191596 const char *aNode, /* Current node image */
191597 int nNode, /* Size of aNode in bytes */
191598 Blob *pNew, /* OUT: Write new node image here */
191599 const char *zTerm, /* Omit all terms smaller than this */
191600 int nTerm, /* Size of zTerm in bytes */
191601 sqlite3_int64 *piBlock /* OUT: Block number in next layer down */
191602){
191603 NodeReader reader; /* Reader object */
191604 Blob prev = {0, 0, 0}; /* Previous term written to new node */
191605 int rc = SQLITE_OK0; /* Return code */
191606 int bLeaf; /* True for a leaf node */
191607
191608 if( nNode<1 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
191609 bLeaf = aNode[0]=='\0';
191610
191611 /* Allocate required output space */
191612 blobGrowBuffer(pNew, nNode, &rc);
191613 if( rc!=SQLITE_OK0 ) return rc;
191614 pNew->n = 0;
191615
191616 /* Populate new node buffer */
191617 for(rc = nodeReaderInit(&reader, aNode, nNode);
191618 rc==SQLITE_OK0 && reader.aNode;
191619 rc = nodeReaderNext(&reader)
191620 ){
191621 if( pNew->n==0 ){
191622 int res = fts3TermCmp(reader.term.a, reader.term.n, zTerm, nTerm);
191623 if( res<0 || (bLeaf==0 && res==0) ) continue;
191624 fts3StartNode(pNew, (int)aNode[0], reader.iChild);
191625 *piBlock = reader.iChild;
191626 }
191627 rc = fts3AppendToNode(
191628 pNew, &prev, reader.term.a, reader.term.n,
191629 reader.aDoclist, reader.nDoclist
191630 );
191631 if( rc!=SQLITE_OK0 ) break;
191632 }
191633 if( pNew->n==0 ){
191634 fts3StartNode(pNew, (int)aNode[0], reader.iChild);
191635 *piBlock = reader.iChild;
191636 }
191637 assert( pNew->n<=pNew->nAlloc )((void) (0));
191638
191639 nodeReaderRelease(&reader);
191640 sqlite3_free(prev.a);
191641 return rc;
191642}
191643
191644/*
191645** Remove all terms smaller than zTerm/nTerm from segment iIdx in absolute
191646** level iAbsLevel. This may involve deleting entries from the %_segments
191647** table, and modifying existing entries in both the %_segments and %_segdir
191648** tables.
191649**
191650** SQLITE_OK is returned if the segment is updated successfully. Or an
191651** SQLite error code otherwise.
191652*/
191653static int fts3TruncateSegment(
191654 Fts3Table *p, /* FTS3 table handle */
191655 sqlite3_int64 iAbsLevel, /* Absolute level of segment to modify */
191656 int iIdx, /* Index within level of segment to modify */
191657 const char *zTerm, /* Remove terms smaller than this */
191658 int nTerm /* Number of bytes in buffer zTerm */
191659){
191660 int rc = SQLITE_OK0; /* Return code */
191661 Blob root = {0,0,0}; /* New root page image */
191662 Blob block = {0,0,0}; /* Buffer used for any other block */
191663 sqlite3_int64 iBlock = 0; /* Block id */
191664 sqlite3_int64 iNewStart = 0; /* New value for iStartBlock */
191665 sqlite3_int64 iOldStart = 0; /* Old value for iStartBlock */
191666 sqlite3_stmt *pFetch = 0; /* Statement used to fetch segdir */
191667
191668 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR32, &pFetch, 0);
191669 if( rc==SQLITE_OK0 ){
191670 int rc2; /* sqlite3_reset() return code */
191671 sqlite3_bind_int64(pFetch, 1, iAbsLevel);
191672 sqlite3_bind_int(pFetch, 2, iIdx);
191673 if( SQLITE_ROW100==sqlite3_step(pFetch) ){
191674 const char *aRoot = sqlite3_column_blob(pFetch, 4);
191675 int nRoot = sqlite3_column_bytes(pFetch, 4);
191676 iOldStart = sqlite3_column_int64(pFetch, 1);
191677 rc = fts3TruncateNode(aRoot, nRoot, &root, zTerm, nTerm, &iBlock);
191678 }
191679 rc2 = sqlite3_reset(pFetch);
191680 if( rc==SQLITE_OK0 ) rc = rc2;
191681 }
191682
191683 while( rc==SQLITE_OK0 && iBlock ){
191684 char *aBlock = 0;
191685 int nBlock = 0;
191686 iNewStart = iBlock;
191687
191688 rc = sqlite3Fts3ReadBlock(p, iBlock, &aBlock, &nBlock, 0);
191689 if( rc==SQLITE_OK0 ){
191690 rc = fts3TruncateNode(aBlock, nBlock, &block, zTerm, nTerm, &iBlock);
191691 }
191692 if( rc==SQLITE_OK0 ){
191693 rc = fts3WriteSegment(p, iNewStart, block.a, block.n);
191694 }
191695 sqlite3_free(aBlock);
191696 }
191697
191698 /* Variable iNewStart now contains the first valid leaf node. */
191699 if( rc==SQLITE_OK0 && iNewStart ){
191700 sqlite3_stmt *pDel = 0;
191701 rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE17, &pDel, 0);
191702 if( rc==SQLITE_OK0 ){
191703 sqlite3_bind_int64(pDel, 1, iOldStart);
191704 sqlite3_bind_int64(pDel, 2, iNewStart-1);
191705 sqlite3_step(pDel);
191706 rc = sqlite3_reset(pDel);
191707 }
191708 }
191709
191710 if( rc==SQLITE_OK0 ){
191711 sqlite3_stmt *pChomp = 0;
191712 rc = fts3SqlStmt(p, SQL_CHOMP_SEGDIR33, &pChomp, 0);
191713 if( rc==SQLITE_OK0 ){
191714 sqlite3_bind_int64(pChomp, 1, iNewStart);
191715 sqlite3_bind_blob(pChomp, 2, root.a, root.n, SQLITE_STATIC((sqlite3_destructor_type)0));
191716 sqlite3_bind_int64(pChomp, 3, iAbsLevel);
191717 sqlite3_bind_int(pChomp, 4, iIdx);
191718 sqlite3_step(pChomp);
191719 rc = sqlite3_reset(pChomp);
191720 sqlite3_bind_null(pChomp, 2);
191721 }
191722 }
191723
191724 sqlite3_free(root.a);
191725 sqlite3_free(block.a);
191726 return rc;
191727}
191728
191729/*
191730** This function is called after an incrmental-merge operation has run to
191731** merge (or partially merge) two or more segments from absolute level
191732** iAbsLevel.
191733**
191734** Each input segment is either removed from the db completely (if all of
191735** its data was copied to the output segment by the incrmerge operation)
191736** or modified in place so that it no longer contains those entries that
191737** have been duplicated in the output segment.
191738*/
191739static int fts3IncrmergeChomp(
191740 Fts3Table *p, /* FTS table handle */
191741 sqlite3_int64 iAbsLevel, /* Absolute level containing segments */
191742 Fts3MultiSegReader *pCsr, /* Chomp all segments opened by this cursor */
191743 int *pnRem /* Number of segments not deleted */
191744){
191745 int i;
191746 int nRem = 0;
191747 int rc = SQLITE_OK0;
191748
191749 for(i=pCsr->nSegment-1; i>=0 && rc==SQLITE_OK0; i--){
191750 Fts3SegReader *pSeg = 0;
191751 int j;
191752
191753 /* Find the Fts3SegReader object with Fts3SegReader.iIdx==i. It is hiding
191754 ** somewhere in the pCsr->apSegment[] array. */
191755 for(j=0; ALWAYS(j<pCsr->nSegment)(j<pCsr->nSegment); j++){
191756 pSeg = pCsr->apSegment[j];
191757 if( pSeg->iIdx==i ) break;
191758 }
191759 assert( j<pCsr->nSegment && pSeg->iIdx==i )((void) (0));
191760
191761 if( pSeg->aNode==0 ){
191762 /* Seg-reader is at EOF. Remove the entire input segment. */
191763 rc = fts3DeleteSegment(p, pSeg);
191764 if( rc==SQLITE_OK0 ){
191765 rc = fts3RemoveSegdirEntry(p, iAbsLevel, pSeg->iIdx);
191766 }
191767 *pnRem = 0;
191768 }else{
191769 /* The incremental merge did not copy all the data from this
191770 ** segment to the upper level. The segment is modified in place
191771 ** so that it contains no keys smaller than zTerm/nTerm. */
191772 const char *zTerm = pSeg->zTerm;
191773 int nTerm = pSeg->nTerm;
191774 rc = fts3TruncateSegment(p, iAbsLevel, pSeg->iIdx, zTerm, nTerm);
191775 nRem++;
191776 }
191777 }
191778
191779 if( rc==SQLITE_OK0 && nRem!=pCsr->nSegment ){
191780 rc = fts3RepackSegdirLevel(p, iAbsLevel);
191781 }
191782
191783 *pnRem = nRem;
191784 return rc;
191785}
191786
191787/*
191788** Store an incr-merge hint in the database.
191789*/
191790static int fts3IncrmergeHintStore(Fts3Table *p, Blob *pHint){
191791 sqlite3_stmt *pReplace = 0;
191792 int rc; /* Return code */
191793
191794 rc = fts3SqlStmt(p, SQL_REPLACE_STAT23, &pReplace, 0);
191795 if( rc==SQLITE_OK0 ){
191796 sqlite3_bind_int(pReplace, 1, FTS_STAT_INCRMERGEHINT1);
191797 sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC((sqlite3_destructor_type)0));
191798 sqlite3_step(pReplace);
191799 rc = sqlite3_reset(pReplace);
191800 sqlite3_bind_null(pReplace, 2);
191801 }
191802
191803 return rc;
191804}
191805
191806/*
191807** Load an incr-merge hint from the database. The incr-merge hint, if one
191808** exists, is stored in the rowid==1 row of the %_stat table.
191809**
191810** If successful, populate blob *pHint with the value read from the %_stat
191811** table and return SQLITE_OK. Otherwise, if an error occurs, return an
191812** SQLite error code.
191813*/
191814static int fts3IncrmergeHintLoad(Fts3Table *p, Blob *pHint){
191815 sqlite3_stmt *pSelect = 0;
191816 int rc;
191817
191818 pHint->n = 0;
191819 rc = fts3SqlStmt(p, SQL_SELECT_STAT22, &pSelect, 0);
191820 if( rc==SQLITE_OK0 ){
191821 int rc2;
191822 sqlite3_bind_int(pSelect, 1, FTS_STAT_INCRMERGEHINT1);
191823 if( SQLITE_ROW100==sqlite3_step(pSelect) ){
191824 const char *aHint = sqlite3_column_blob(pSelect, 0);
191825 int nHint = sqlite3_column_bytes(pSelect, 0);
191826 if( aHint ){
191827 blobGrowBuffer(pHint, nHint, &rc);
191828 if( rc==SQLITE_OK0 ){
191829 if( ALWAYS(pHint->a!=0)(pHint->a!=0) ) memcpy(pHint->a, aHint, nHint);
191830 pHint->n = nHint;
191831 }
191832 }
191833 }
191834 rc2 = sqlite3_reset(pSelect);
191835 if( rc==SQLITE_OK0 ) rc = rc2;
191836 }
191837
191838 return rc;
191839}
191840
191841/*
191842** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
191843** Otherwise, append an entry to the hint stored in blob *pHint. Each entry
191844** consists of two varints, the absolute level number of the input segments
191845** and the number of input segments.
191846**
191847** If successful, leave *pRc set to SQLITE_OK and return. If an error occurs,
191848** set *pRc to an SQLite error code before returning.
191849*/
191850static void fts3IncrmergeHintPush(
191851 Blob *pHint, /* Hint blob to append to */
191852 i64 iAbsLevel, /* First varint to store in hint */
191853 int nInput, /* Second varint to store in hint */
191854 int *pRc /* IN/OUT: Error code */
191855){
191856 blobGrowBuffer(pHint, pHint->n + 2*FTS3_VARINT_MAX10, pRc);
191857 if( *pRc==SQLITE_OK0 ){
191858 pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], iAbsLevel);
191859 pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], (i64)nInput);
191860 }
191861}
191862
191863/*
191864** Read the last entry (most recently pushed) from the hint blob *pHint
191865** and then remove the entry. Write the two values read to *piAbsLevel and
191866** *pnInput before returning.
191867**
191868** If no error occurs, return SQLITE_OK. If the hint blob in *pHint does
191869** not contain at least two valid varints, return SQLITE_CORRUPT_VTAB.
191870*/
191871static int fts3IncrmergeHintPop(Blob *pHint, i64 *piAbsLevel, int *pnInput){
191872 const int nHint = pHint->n;
191873 int i;
191874
191875 i = pHint->n-1;
191876 if( (pHint->a[i] & 0x80) ) return FTS_CORRUPT_VTAB(11 | (1<<8));
191877 while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
191878 if( i==0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
191879 i--;
191880 while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
191881
191882 pHint->n = i;
191883 i += sqlite3Fts3GetVarint(&pHint->a[i], piAbsLevel);
191884 i += fts3GetVarint32(&pHint->a[i], pnInput)( (*(u8*)(&pHint->a[i])&0x80) ? sqlite3Fts3GetVarint32
(&pHint->a[i], pnInput) : (*pnInput=*(u8*)(&pHint->
a[i]), 1) )
;
191885 assert( i<=nHint )((void) (0));
191886 if( i!=nHint ) return FTS_CORRUPT_VTAB(11 | (1<<8));
191887
191888 return SQLITE_OK0;
191889}
191890
191891
191892/*
191893** Attempt an incremental merge that writes nMerge leaf blocks.
191894**
191895** Incremental merges happen nMin segments at a time. The segments
191896** to be merged are the nMin oldest segments (the ones with the smallest
191897** values for the _segdir.idx field) in the highest level that contains
191898** at least nMin segments. Multiple merges might occur in an attempt to
191899** write the quota of nMerge leaf blocks.
191900*/
191901SQLITE_PRIVATEstatic int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
191902 int rc; /* Return code */
191903 int nRem = nMerge; /* Number of leaf pages yet to be written */
191904 Fts3MultiSegReader *pCsr; /* Cursor used to read input data */
191905 Fts3SegFilter *pFilter; /* Filter used with cursor pCsr */
191906 IncrmergeWriter *pWriter; /* Writer object */
191907 int nSeg = 0; /* Number of input segments */
191908 sqlite3_int64 iAbsLevel = 0; /* Absolute level number to work on */
191909 Blob hint = {0, 0, 0}; /* Hint read from %_stat table */
191910 int bDirtyHint = 0; /* True if blob 'hint' has been modified */
191911
191912 /* Allocate space for the cursor, filter and writer objects */
191913 const int nAlloc = sizeof(*pCsr) + sizeof(*pFilter) + sizeof(*pWriter);
191914 pWriter = (IncrmergeWriter *)sqlite3_malloc(nAlloc);
191915 if( !pWriter ) return SQLITE_NOMEM7;
191916 pFilter = (Fts3SegFilter *)&pWriter[1];
191917 pCsr = (Fts3MultiSegReader *)&pFilter[1];
191918
191919 rc = fts3IncrmergeHintLoad(p, &hint);
191920 while( rc==SQLITE_OK0 && nRem>0 ){
191921 const i64 nMod = FTS3_SEGDIR_MAXLEVEL1024 * p->nIndex;
191922 sqlite3_stmt *pFindLevel = 0; /* SQL used to determine iAbsLevel */
191923 int bUseHint = 0; /* True if attempting to append */
191924 int iIdx = 0; /* Largest idx in level (iAbsLevel+1) */
191925
191926 /* Search the %_segdir table for the absolute level with the smallest
191927 ** relative level number that contains at least nMin segments, if any.
191928 ** If one is found, set iAbsLevel to the absolute level number and
191929 ** nSeg to nMin. If no level with at least nMin segments can be found,
191930 ** set nSeg to -1.
191931 */
191932 rc = fts3SqlStmt(p, SQL_FIND_MERGE_LEVEL28, &pFindLevel, 0);
191933 sqlite3_bind_int(pFindLevel, 1, MAX(2, nMin)((2)>(nMin)?(2):(nMin)));
191934 if( sqlite3_step(pFindLevel)==SQLITE_ROW100 ){
191935 iAbsLevel = sqlite3_column_int64(pFindLevel, 0);
191936 nSeg = sqlite3_column_int(pFindLevel, 1);
191937 assert( nSeg>=2 )((void) (0));
191938 }else{
191939 nSeg = -1;
191940 }
191941 rc = sqlite3_reset(pFindLevel);
191942
191943 /* If the hint read from the %_stat table is not empty, check if the
191944 ** last entry in it specifies a relative level smaller than or equal
191945 ** to the level identified by the block above (if any). If so, this
191946 ** iteration of the loop will work on merging at the hinted level.
191947 */
191948 if( rc==SQLITE_OK0 && hint.n ){
191949 int nHint = hint.n;
191950 sqlite3_int64 iHintAbsLevel = 0; /* Hint level */
191951 int nHintSeg = 0; /* Hint number of segments */
191952
191953 rc = fts3IncrmergeHintPop(&hint, &iHintAbsLevel, &nHintSeg);
191954 if( nSeg<0 || (iAbsLevel % nMod) >= (iHintAbsLevel % nMod) ){
191955 /* Based on the scan in the block above, it is known that there
191956 ** are no levels with a relative level smaller than that of
191957 ** iAbsLevel with more than nSeg segments, or if nSeg is -1,
191958 ** no levels with more than nMin segments. Use this to limit the
191959 ** value of nHintSeg to avoid a large memory allocation in case the
191960 ** merge-hint is corrupt*/
191961 iAbsLevel = iHintAbsLevel;
191962 nSeg = MIN(MAX(nMin,nSeg), nHintSeg)((((nMin)>(nSeg)?(nMin):(nSeg)))<(nHintSeg)?(((nMin)>
(nSeg)?(nMin):(nSeg))):(nHintSeg))
;
191963 bUseHint = 1;
191964 bDirtyHint = 1;
191965 }else{
191966 /* This undoes the effect of the HintPop() above - so that no entry
191967 ** is removed from the hint blob. */
191968 hint.n = nHint;
191969 }
191970 }
191971
191972 /* If nSeg is less that zero, then there is no level with at least
191973 ** nMin segments and no hint in the %_stat table. No work to do.
191974 ** Exit early in this case. */
191975 if( nSeg<=0 ) break;
191976
191977 assert( nMod<=0x7FFFFFFF )((void) (0));
191978 if( iAbsLevel<0 || iAbsLevel>(nMod<<32) ){
191979 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
191980 break;
191981 }
191982
191983 /* Open a cursor to iterate through the contents of the oldest nSeg
191984 ** indexes of absolute level iAbsLevel. If this cursor is opened using
191985 ** the 'hint' parameters, it is possible that there are less than nSeg
191986 ** segments available in level iAbsLevel. In this case, no work is
191987 ** done on iAbsLevel - fall through to the next iteration of the loop
191988 ** to start work on some other level. */
191989 memset(pWriter, 0, nAlloc);
191990 pFilter->flags = FTS3_SEGMENT_REQUIRE_POS0x00000001;
191991
191992 if( rc==SQLITE_OK0 ){
191993 rc = fts3IncrmergeOutputIdx(p, iAbsLevel, &iIdx);
191994 assert( bUseHint==1 || bUseHint==0 )((void) (0));
191995 if( iIdx==0 || (bUseHint && iIdx==1) ){
191996 int bIgnore = 0;
191997 rc = fts3SegmentIsMaxLevel(p, iAbsLevel+1, &bIgnore);
191998 if( bIgnore ){
191999 pFilter->flags |= FTS3_SEGMENT_IGNORE_EMPTY0x00000002;
192000 }
192001 }
192002 }
192003
192004 if( rc==SQLITE_OK0 ){
192005 rc = fts3IncrmergeCsr(p, iAbsLevel, nSeg, pCsr);
192006 }
192007 if( SQLITE_OK0==rc && pCsr->nSegment==nSeg
192008 && SQLITE_OK0==(rc = sqlite3Fts3SegReaderStart(p, pCsr, pFilter))
192009 ){
192010 int bEmpty = 0;
192011 rc = sqlite3Fts3SegReaderStep(p, pCsr);
192012 if( rc==SQLITE_OK0 ){
192013 bEmpty = 1;
192014 }else if( rc!=SQLITE_ROW100 ){
192015 sqlite3Fts3SegReaderFinish(pCsr);
192016 break;
192017 }
192018 if( bUseHint && iIdx>0 ){
192019 const char *zKey = pCsr->zTerm;
192020 int nKey = pCsr->nTerm;
192021 rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx-1, zKey, nKey, pWriter);
192022 }else{
192023 rc = fts3IncrmergeWriter(p, iAbsLevel, iIdx, pCsr, pWriter);
192024 }
192025
192026 if( rc==SQLITE_OK0 && pWriter->nLeafEst ){
192027 fts3LogMerge(nSeg, iAbsLevel);
192028 if( bEmpty==0 ){
192029 do {
192030 rc = fts3IncrmergeAppend(p, pWriter, pCsr);
192031 if( rc==SQLITE_OK0 ) rc = sqlite3Fts3SegReaderStep(p, pCsr);
192032 if( pWriter->nWork>=nRem && rc==SQLITE_ROW100 ) rc = SQLITE_OK0;
192033 }while( rc==SQLITE_ROW100 );
192034 }
192035
192036 /* Update or delete the input segments */
192037 if( rc==SQLITE_OK0 ){
192038 nRem -= (1 + pWriter->nWork);
192039 rc = fts3IncrmergeChomp(p, iAbsLevel, pCsr, &nSeg);
192040 if( nSeg!=0 ){
192041 bDirtyHint = 1;
192042 fts3IncrmergeHintPush(&hint, iAbsLevel, nSeg, &rc);
192043 }
192044 }
192045 }
192046
192047 if( nSeg!=0 ){
192048 pWriter->nLeafData = pWriter->nLeafData * -1;
192049 }
192050 fts3IncrmergeRelease(p, pWriter, &rc);
192051 if( nSeg==0 && pWriter->bNoLeafData==0 ){
192052 fts3PromoteSegments(p, iAbsLevel+1, pWriter->nLeafData);
192053 }
192054 }
192055
192056 sqlite3Fts3SegReaderFinish(pCsr);
192057 }
192058
192059 /* Write the hint values into the %_stat table for the next incr-merger */
192060 if( bDirtyHint && rc==SQLITE_OK0 ){
192061 rc = fts3IncrmergeHintStore(p, &hint);
192062 }
192063
192064 sqlite3_free(pWriter);
192065 sqlite3_free(hint.a);
192066 return rc;
192067}
192068
192069/*
192070** Convert the text beginning at *pz into an integer and return
192071** its value. Advance *pz to point to the first character past
192072** the integer.
192073**
192074** This function used for parameters to merge= and incrmerge=
192075** commands.
192076*/
192077static int fts3Getint(const char **pz){
192078 const char *z = *pz;
192079 int i = 0;
192080 while( (*z)>='0' && (*z)<='9' && i<214748363 ) i = 10*i + *(z++) - '0';
192081 *pz = z;
192082 return i;
192083}
192084
192085/*
192086** Process statements of the form:
192087**
192088** INSERT INTO table(table) VALUES('merge=A,B');
192089**
192090** A and B are integers that decode to be the number of leaf pages
192091** written for the merge, and the minimum number of segments on a level
192092** before it will be selected for a merge, respectively.
192093*/
192094static int fts3DoIncrmerge(
192095 Fts3Table *p, /* FTS3 table handle */
192096 const char *zParam /* Nul-terminated string containing "A,B" */
192097){
192098 int rc;
192099 int nMin = (MergeCount(p)16 / 2);
192100 int nMerge = 0;
192101 const char *z = zParam;
192102
192103 /* Read the first integer value */
192104 nMerge = fts3Getint(&z);
192105
192106 /* If the first integer value is followed by a ',', read the second
192107 ** integer value. */
192108 if( z[0]==',' && z[1]!='\0' ){
192109 z++;
192110 nMin = fts3Getint(&z);
192111 }
192112
192113 if( z[0]!='\0' || nMin<2 ){
192114 rc = SQLITE_ERROR1;
192115 }else{
192116 rc = SQLITE_OK0;
192117 if( !p->bHasStat ){
192118 assert( p->bFts4==0 )((void) (0));
192119 sqlite3Fts3CreateStatTable(&rc, p);
192120 }
192121 if( rc==SQLITE_OK0 ){
192122 rc = sqlite3Fts3Incrmerge(p, nMerge, nMin);
192123 }
192124 sqlite3Fts3SegmentsClose(p);
192125 }
192126 return rc;
192127}
192128
192129/*
192130** Process statements of the form:
192131**
192132** INSERT INTO table(table) VALUES('automerge=X');
192133**
192134** where X is an integer. X==0 means to turn automerge off. X!=0 means
192135** turn it on. The setting is persistent.
192136*/
192137static int fts3DoAutoincrmerge(
192138 Fts3Table *p, /* FTS3 table handle */
192139 const char *zParam /* Nul-terminated string containing boolean */
192140){
192141 int rc = SQLITE_OK0;
192142 sqlite3_stmt *pStmt = 0;
192143 p->nAutoincrmerge = fts3Getint(&zParam);
192144 if( p->nAutoincrmerge==1 || p->nAutoincrmerge>MergeCount(p)16 ){
192145 p->nAutoincrmerge = 8;
192146 }
192147 if( !p->bHasStat ){
192148 assert( p->bFts4==0 )((void) (0));
192149 sqlite3Fts3CreateStatTable(&rc, p);
192150 if( rc ) return rc;
192151 }
192152 rc = fts3SqlStmt(p, SQL_REPLACE_STAT23, &pStmt, 0);
192153 if( rc ) return rc;
192154 sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE2);
192155 sqlite3_bind_int(pStmt, 2, p->nAutoincrmerge);
192156 sqlite3_step(pStmt);
192157 rc = sqlite3_reset(pStmt);
192158 return rc;
192159}
192160
192161/*
192162** Return a 64-bit checksum for the FTS index entry specified by the
192163** arguments to this function.
192164*/
192165static u64 fts3ChecksumEntry(
192166 const char *zTerm, /* Pointer to buffer containing term */
192167 int nTerm, /* Size of zTerm in bytes */
192168 int iLangid, /* Language id for current row */
192169 int iIndex, /* Index (0..Fts3Table.nIndex-1) */
192170 i64 iDocid, /* Docid for current row. */
192171 int iCol, /* Column number */
192172 int iPos /* Position */
192173){
192174 int i;
192175 u64 ret = (u64)iDocid;
192176
192177 ret += (ret<<3) + iLangid;
192178 ret += (ret<<3) + iIndex;
192179 ret += (ret<<3) + iCol;
192180 ret += (ret<<3) + iPos;
192181 for(i=0; i<nTerm; i++) ret += (ret<<3) + zTerm[i];
192182
192183 return ret;
192184}
192185
192186/*
192187** Return a checksum of all entries in the FTS index that correspond to
192188** language id iLangid. The checksum is calculated by XORing the checksums
192189** of each individual entry (see fts3ChecksumEntry()) together.
192190**
192191** If successful, the checksum value is returned and *pRc set to SQLITE_OK.
192192** Otherwise, if an error occurs, *pRc is set to an SQLite error code. The
192193** return value is undefined in this case.
192194*/
192195static u64 fts3ChecksumIndex(
192196 Fts3Table *p, /* FTS3 table handle */
192197 int iLangid, /* Language id to return cksum for */
192198 int iIndex, /* Index to cksum (0..p->nIndex-1) */
192199 int *pRc /* OUT: Return code */
192200){
192201 Fts3SegFilter filter;
192202 Fts3MultiSegReader csr;
192203 int rc;
192204 u64 cksum = 0;
192205
192206 assert( *pRc==SQLITE_OK )((void) (0));
192207
192208 memset(&filter, 0, sizeof(filter));
192209 memset(&csr, 0, sizeof(csr));
192210 filter.flags = FTS3_SEGMENT_REQUIRE_POS0x00000001|FTS3_SEGMENT_IGNORE_EMPTY0x00000002;
192211 filter.flags |= FTS3_SEGMENT_SCAN0x00000010;
192212
192213 rc = sqlite3Fts3SegReaderCursor(
192214 p, iLangid, iIndex, FTS3_SEGCURSOR_ALL-2, 0, 0, 0, 1,&csr
192215 );
192216 if( rc==SQLITE_OK0 ){
192217 rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
192218 }
192219
192220 if( rc==SQLITE_OK0 ){
192221 while( SQLITE_ROW100==(rc = sqlite3Fts3SegReaderStep(p, &csr)) ){
192222 char *pCsr = csr.aDoclist;
192223 char *pEnd = &pCsr[csr.nDoclist];
192224
192225 i64 iDocid = 0;
192226 i64 iCol = 0;
192227 u64 iPos = 0;
192228
192229 pCsr += sqlite3Fts3GetVarint(pCsr, &iDocid);
192230 while( pCsr<pEnd ){
192231 u64 iVal = 0;
192232 pCsr += sqlite3Fts3GetVarintU(pCsr, &iVal);
192233 if( pCsr<pEnd ){
192234 if( iVal==0 || iVal==1 ){
192235 iCol = 0;
192236 iPos = 0;
192237 if( iVal ){
192238 pCsr += sqlite3Fts3GetVarint(pCsr, &iCol);
192239 }else{
192240 pCsr += sqlite3Fts3GetVarintU(pCsr, &iVal);
192241 if( p->bDescIdx ){
192242 iDocid = (i64)((u64)iDocid - iVal);
192243 }else{
192244 iDocid = (i64)((u64)iDocid + iVal);
192245 }
192246 }
192247 }else{
192248 iPos += (iVal - 2);
192249 cksum = cksum ^ fts3ChecksumEntry(
192250 csr.zTerm, csr.nTerm, iLangid, iIndex, iDocid,
192251 (int)iCol, (int)iPos
192252 );
192253 }
192254 }
192255 }
192256 }
192257 }
192258 sqlite3Fts3SegReaderFinish(&csr);
192259
192260 *pRc = rc;
192261 return cksum;
192262}
192263
192264/*
192265** Check if the contents of the FTS index match the current contents of the
192266** content table. If no error occurs and the contents do match, set *pbOk
192267** to true and return SQLITE_OK. Or if the contents do not match, set *pbOk
192268** to false before returning.
192269**
192270** If an error occurs (e.g. an OOM or IO error), return an SQLite error
192271** code. The final value of *pbOk is undefined in this case.
192272*/
192273static int fts3IntegrityCheck(Fts3Table *p, int *pbOk){
192274 int rc = SQLITE_OK0; /* Return code */
192275 u64 cksum1 = 0; /* Checksum based on FTS index contents */
192276 u64 cksum2 = 0; /* Checksum based on %_content contents */
192277 sqlite3_stmt *pAllLangid = 0; /* Statement to return all language-ids */
192278
192279 /* This block calculates the checksum according to the FTS index. */
192280 rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID27, &pAllLangid, 0);
192281 if( rc==SQLITE_OK0 ){
192282 int rc2;
192283 sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
192284 sqlite3_bind_int(pAllLangid, 2, p->nIndex);
192285 while( rc==SQLITE_OK0 && sqlite3_step(pAllLangid)==SQLITE_ROW100 ){
192286 int iLangid = sqlite3_column_int(pAllLangid, 0);
192287 int i;
192288 for(i=0; i<p->nIndex; i++){
192289 cksum1 = cksum1 ^ fts3ChecksumIndex(p, iLangid, i, &rc);
192290 }
192291 }
192292 rc2 = sqlite3_reset(pAllLangid);
192293 if( rc==SQLITE_OK0 ) rc = rc2;
192294 }
192295
192296 /* This block calculates the checksum according to the %_content table */
192297 if( rc==SQLITE_OK0 ){
192298 sqlite3_tokenizer_module const *pModule = p->pTokenizer->pModule;
192299 sqlite3_stmt *pStmt = 0;
192300 char *zSql;
192301
192302 zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
192303 if( !zSql ){
192304 rc = SQLITE_NOMEM7;
192305 }else{
192306 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
192307 sqlite3_free(zSql);
192308 }
192309
192310 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pStmt) ){
192311 i64 iDocid = sqlite3_column_int64(pStmt, 0);
192312 int iLang = langidFromSelect(p, pStmt);
192313 int iCol;
192314
192315 for(iCol=0; rc==SQLITE_OK0 && iCol<p->nColumn; iCol++){
192316 if( p->abNotindexed[iCol]==0 ){
192317 const char *zText = (const char *)sqlite3_column_text(pStmt, iCol+1);
192318 sqlite3_tokenizer_cursor *pT = 0;
192319
192320 rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, -1, &pT);
192321 while( rc==SQLITE_OK0 ){
192322 char const *zToken; /* Buffer containing token */
192323 int nToken = 0; /* Number of bytes in token */
192324 int iDum1 = 0, iDum2 = 0; /* Dummy variables */
192325 int iPos = 0; /* Position of token in zText */
192326
192327 rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
192328 if( rc==SQLITE_OK0 ){
192329 int i;
192330 cksum2 = cksum2 ^ fts3ChecksumEntry(
192331 zToken, nToken, iLang, 0, iDocid, iCol, iPos
192332 );
192333 for(i=1; i<p->nIndex; i++){
192334 if( p->aIndex[i].nPrefix<=nToken ){
192335 cksum2 = cksum2 ^ fts3ChecksumEntry(
192336 zToken, p->aIndex[i].nPrefix, iLang, i, iDocid, iCol, iPos
192337 );
192338 }
192339 }
192340 }
192341 }
192342 if( pT ) pModule->xClose(pT);
192343 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
192344 }
192345 }
192346 }
192347
192348 sqlite3_finalize(pStmt);
192349 }
192350
192351 *pbOk = (cksum1==cksum2);
192352 return rc;
192353}
192354
192355/*
192356** Run the integrity-check. If no error occurs and the current contents of
192357** the FTS index are correct, return SQLITE_OK. Or, if the contents of the
192358** FTS index are incorrect, return SQLITE_CORRUPT_VTAB.
192359**
192360** Or, if an error (e.g. an OOM or IO error) occurs, return an SQLite
192361** error code.
192362**
192363** The integrity-check works as follows. For each token and indexed token
192364** prefix in the document set, a 64-bit checksum is calculated (by code
192365** in fts3ChecksumEntry()) based on the following:
192366**
192367** + The index number (0 for the main index, 1 for the first prefix
192368** index etc.),
192369** + The token (or token prefix) text itself,
192370** + The language-id of the row it appears in,
192371** + The docid of the row it appears in,
192372** + The column it appears in, and
192373** + The tokens position within that column.
192374**
192375** The checksums for all entries in the index are XORed together to create
192376** a single checksum for the entire index.
192377**
192378** The integrity-check code calculates the same checksum in two ways:
192379**
192380** 1. By scanning the contents of the FTS index, and
192381** 2. By scanning and tokenizing the content table.
192382**
192383** If the two checksums are identical, the integrity-check is deemed to have
192384** passed.
192385*/
192386static int fts3DoIntegrityCheck(
192387 Fts3Table *p /* FTS3 table handle */
192388){
192389 int rc;
192390 int bOk = 0;
192391 rc = fts3IntegrityCheck(p, &bOk);
192392 if( rc==SQLITE_OK0 && bOk==0 ) rc = FTS_CORRUPT_VTAB(11 | (1<<8));
192393 return rc;
192394}
192395
192396/*
192397** Handle a 'special' INSERT of the form:
192398**
192399** "INSERT INTO tbl(tbl) VALUES(<expr>)"
192400**
192401** Argument pVal contains the result of <expr>. Currently the only
192402** meaningful value to insert is the text 'optimize'.
192403*/
192404static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
192405 int rc = SQLITE_ERROR1; /* Return Code */
192406 const char *zVal = (const char *)sqlite3_value_text(pVal);
192407 int nVal = sqlite3_value_bytes(pVal);
192408
192409 if( !zVal ){
192410 return SQLITE_NOMEM7;
192411 }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
192412 rc = fts3DoOptimize(p, 0);
192413 }else if( nVal==7 && 0==sqlite3_strnicmp(zVal, "rebuild", 7) ){
192414 rc = fts3DoRebuild(p);
192415 }else if( nVal==15 && 0==sqlite3_strnicmp(zVal, "integrity-check", 15) ){
192416 rc = fts3DoIntegrityCheck(p);
192417 }else if( nVal>6 && 0==sqlite3_strnicmp(zVal, "merge=", 6) ){
192418 rc = fts3DoIncrmerge(p, &zVal[6]);
192419 }else if( nVal>10 && 0==sqlite3_strnicmp(zVal, "automerge=", 10) ){
192420 rc = fts3DoAutoincrmerge(p, &zVal[10]);
192421#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
192422 }else{
192423 int v;
192424 if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
192425 v = atoi(&zVal[9]);
192426 if( v>=24 && v<=p->nPgsz-35 ) p->nNodeSize = v;
192427 rc = SQLITE_OK0;
192428 }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
192429 v = atoi(&zVal[11]);
192430 if( v>=64 && v<=FTS3_MAX_PENDING_DATA(1*1024*1024) ) p->nMaxPendingData = v;
192431 rc = SQLITE_OK0;
192432 }else if( nVal>21 && 0==sqlite3_strnicmp(zVal,"test-no-incr-doclist=",21) ){
192433 p->bNoIncrDoclist = atoi(&zVal[21]);
192434 rc = SQLITE_OK0;
192435 }else if( nVal>11 && 0==sqlite3_strnicmp(zVal,"mergecount=",11) ){
192436 v = atoi(&zVal[11]);
192437 if( v>=4 && v<=FTS3_MERGE_COUNT16 && (v&1)==0 ) p->nMergeCount = v;
192438 rc = SQLITE_OK0;
192439 }
192440#endif
192441 }
192442 return rc;
192443}
192444
192445#ifndef SQLITE_DISABLE_FTS4_DEFERRED
192446/*
192447** Delete all cached deferred doclists. Deferred doclists are cached
192448** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function.
192449*/
192450SQLITE_PRIVATEstatic void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *pCsr){
192451 Fts3DeferredToken *pDef;
192452 for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){
192453 fts3PendingListDelete(pDef->pList);
192454 pDef->pList = 0;
192455 }
192456}
192457
192458/*
192459** Free all entries in the pCsr->pDeffered list. Entries are added to
192460** this list using sqlite3Fts3DeferToken().
192461*/
192462SQLITE_PRIVATEstatic void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *pCsr){
192463 Fts3DeferredToken *pDef;
192464 Fts3DeferredToken *pNext;
192465 for(pDef=pCsr->pDeferred; pDef; pDef=pNext){
192466 pNext = pDef->pNext;
192467 fts3PendingListDelete(pDef->pList);
192468 sqlite3_free(pDef);
192469 }
192470 pCsr->pDeferred = 0;
192471}
192472
192473/*
192474** Generate deferred-doclists for all tokens in the pCsr->pDeferred list
192475** based on the row that pCsr currently points to.
192476**
192477** A deferred-doclist is like any other doclist with position information
192478** included, except that it only contains entries for a single row of the
192479** table, not for all rows.
192480*/
192481SQLITE_PRIVATEstatic int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *pCsr){
192482 int rc = SQLITE_OK0; /* Return code */
192483 if( pCsr->pDeferred ){
192484 int i; /* Used to iterate through table columns */
192485 sqlite3_int64 iDocid; /* Docid of the row pCsr points to */
192486 Fts3DeferredToken *pDef; /* Used to iterate through deferred tokens */
192487
192488 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
192489 sqlite3_tokenizer *pT = p->pTokenizer;
192490 sqlite3_tokenizer_module const *pModule = pT->pModule;
192491
192492 assert( pCsr->isRequireSeek==0 )((void) (0));
192493 iDocid = sqlite3_column_int64(pCsr->pStmt, 0);
192494
192495 for(i=0; i<p->nColumn && rc==SQLITE_OK0; i++){
192496 if( p->abNotindexed[i]==0 ){
192497 const char *zText = (const char *)sqlite3_column_text(pCsr->pStmt, i+1);
192498 sqlite3_tokenizer_cursor *pTC = 0;
192499
192500 rc = sqlite3Fts3OpenTokenizer(pT, pCsr->iLangid, zText, -1, &pTC);
192501 while( rc==SQLITE_OK0 ){
192502 char const *zToken; /* Buffer containing token */
192503 int nToken = 0; /* Number of bytes in token */
192504 int iDum1 = 0, iDum2 = 0; /* Dummy variables */
192505 int iPos = 0; /* Position of token in zText */
192506
192507 rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
192508 for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK0; pDef=pDef->pNext){
192509 Fts3PhraseToken *pPT = pDef->pToken;
192510 if( (pDef->iCol>=p->nColumn || pDef->iCol==i)
192511 && (pPT->bFirst==0 || iPos==0)
192512 && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken))
192513 && (0==memcmp(zToken, pPT->z, pPT->n))
192514 ){
192515 fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc);
192516 }
192517 }
192518 }
192519 if( pTC ) pModule->xClose(pTC);
192520 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
192521 }
192522 }
192523
192524 for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK0; pDef=pDef->pNext){
192525 if( pDef->pList ){
192526 rc = fts3PendingListAppendVarint(&pDef->pList, 0);
192527 }
192528 }
192529 }
192530
192531 return rc;
192532}
192533
192534SQLITE_PRIVATEstatic int sqlite3Fts3DeferredTokenList(
192535 Fts3DeferredToken *p,
192536 char **ppData,
192537 int *pnData
192538){
192539 char *pRet;
192540 int nSkip;
192541 sqlite3_int64 dummy;
192542
192543 *ppData = 0;
192544 *pnData = 0;
192545
192546 if( p->pList==0 ){
192547 return SQLITE_OK0;
192548 }
192549
192550 pRet = (char *)sqlite3_malloc(p->pList->nData);
192551 if( !pRet ) return SQLITE_NOMEM7;
192552
192553 nSkip = sqlite3Fts3GetVarint(p->pList->aData, &dummy);
192554 *pnData = p->pList->nData - nSkip;
192555 *ppData = pRet;
192556
192557 memcpy(pRet, &p->pList->aData[nSkip], *pnData);
192558 return SQLITE_OK0;
192559}
192560
192561/*
192562** Add an entry for token pToken to the pCsr->pDeferred list.
192563*/
192564SQLITE_PRIVATEstatic int sqlite3Fts3DeferToken(
192565 Fts3Cursor *pCsr, /* Fts3 table cursor */
192566 Fts3PhraseToken *pToken, /* Token to defer */
192567 int iCol /* Column that token must appear in (or -1) */
192568){
192569 Fts3DeferredToken *pDeferred;
192570 pDeferred = sqlite3_malloc(sizeof(*pDeferred));
192571 if( !pDeferred ){
192572 return SQLITE_NOMEM7;
192573 }
192574 memset(pDeferred, 0, sizeof(*pDeferred));
192575 pDeferred->pToken = pToken;
192576 pDeferred->pNext = pCsr->pDeferred;
192577 pDeferred->iCol = iCol;
192578 pCsr->pDeferred = pDeferred;
192579
192580 assert( pToken->pDeferred==0 )((void) (0));
192581 pToken->pDeferred = pDeferred;
192582
192583 return SQLITE_OK0;
192584}
192585#endif
192586
192587/*
192588** SQLite value pRowid contains the rowid of a row that may or may not be
192589** present in the FTS3 table. If it is, delete it and adjust the contents
192590** of subsiduary data structures accordingly.
192591*/
192592static int fts3DeleteByRowid(
192593 Fts3Table *p,
192594 sqlite3_value *pRowid,
192595 int *pnChng, /* IN/OUT: Decrement if row is deleted */
192596 u32 *aSzDel
192597){
192598 int rc = SQLITE_OK0; /* Return code */
192599 int bFound = 0; /* True if *pRowid really is in the table */
192600
192601 fts3DeleteTerms(&rc, p, pRowid, aSzDel, &bFound);
192602 if( bFound && rc==SQLITE_OK0 ){
192603 int isEmpty = 0; /* Deleting *pRowid leaves the table empty */
192604 rc = fts3IsEmpty(p, pRowid, &isEmpty);
192605 if( rc==SQLITE_OK0 ){
192606 if( isEmpty ){
192607 /* Deleting this row means the whole table is empty. In this case
192608 ** delete the contents of all three tables and throw away any
192609 ** data in the pendingTerms hash table. */
192610 rc = fts3DeleteAll(p, 1);
192611 *pnChng = 0;
192612 memset(aSzDel, 0, sizeof(u32) * (p->nColumn+1) * 2);
192613 }else{
192614 *pnChng = *pnChng - 1;
192615 if( p->zContentTbl==0 ){
192616 fts3SqlExec(&rc, p, SQL_DELETE_CONTENT0, &pRowid);
192617 }
192618 if( p->bHasDocsize ){
192619 fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE19, &pRowid);
192620 }
192621 }
192622 }
192623 }
192624
192625 return rc;
192626}
192627
192628/*
192629** This function does the work for the xUpdate method of FTS3 virtual
192630** tables. The schema of the virtual table being:
192631**
192632** CREATE TABLE <table name>(
192633** <user columns>,
192634** <table name> HIDDEN,
192635** docid HIDDEN,
192636** <langid> HIDDEN
192637** );
192638**
192639**
192640*/
192641SQLITE_PRIVATEstatic int sqlite3Fts3UpdateMethod(
192642 sqlite3_vtab *pVtab, /* FTS3 vtab object */
192643 int nArg, /* Size of argument array */
192644 sqlite3_value **apVal, /* Array of arguments */
192645 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
192646){
192647 Fts3Table *p = (Fts3Table *)pVtab;
192648 int rc = SQLITE_OK0; /* Return Code */
192649 u32 *aSzIns = 0; /* Sizes of inserted documents */
192650 u32 *aSzDel = 0; /* Sizes of deleted documents */
192651 int nChng = 0; /* Net change in number of documents */
192652 int bInsertDone = 0;
192653
192654 /* At this point it must be known if the %_stat table exists or not.
192655 ** So bHasStat may not be 2. */
192656 assert( p->bHasStat==0 || p->bHasStat==1 )((void) (0));
192657
192658 assert( p->pSegments==0 )((void) (0));
192659 assert(((void) (0))
192660 nArg==1 /* DELETE operations */((void) (0))
192661 || nArg==(2 + p->nColumn + 3) /* INSERT or UPDATE operations */((void) (0))
192662 )((void) (0));
192663
192664 /* Check for a "special" INSERT operation. One of the form:
192665 **
192666 ** INSERT INTO xyz(xyz) VALUES('command');
192667 */
192668 if( nArg>1
192669 && sqlite3_value_type(apVal[0])==SQLITE_NULL5
192670 && sqlite3_value_type(apVal[p->nColumn+2])!=SQLITE_NULL5
192671 ){
192672 rc = fts3SpecialInsert(p, apVal[p->nColumn+2]);
192673 goto update_out;
192674 }
192675
192676 if( nArg>1 && sqlite3_value_int(apVal[2 + p->nColumn + 2])<0 ){
192677 rc = SQLITE_CONSTRAINT19;
192678 goto update_out;
192679 }
192680
192681 /* Allocate space to hold the change in document sizes */
192682 aSzDel = sqlite3_malloc64(sizeof(aSzDel[0])*((sqlite3_int64)p->nColumn+1)*2);
192683 if( aSzDel==0 ){
192684 rc = SQLITE_NOMEM7;
192685 goto update_out;
192686 }
192687 aSzIns = &aSzDel[p->nColumn+1];
192688 memset(aSzDel, 0, sizeof(aSzDel[0])*(p->nColumn+1)*2);
192689
192690 rc = fts3Writelock(p);
192691 if( rc!=SQLITE_OK0 ) goto update_out;
192692
192693 /* If this is an INSERT operation, or an UPDATE that modifies the rowid
192694 ** value, then this operation requires constraint handling.
192695 **
192696 ** If the on-conflict mode is REPLACE, this means that the existing row
192697 ** should be deleted from the database before inserting the new row. Or,
192698 ** if the on-conflict mode is other than REPLACE, then this method must
192699 ** detect the conflict and return SQLITE_CONSTRAINT before beginning to
192700 ** modify the database file.
192701 */
192702 if( nArg>1 && p->zContentTbl==0 ){
192703 /* Find the value object that holds the new rowid value. */
192704 sqlite3_value *pNewRowid = apVal[3+p->nColumn];
192705 if( sqlite3_value_type(pNewRowid)==SQLITE_NULL5 ){
192706 pNewRowid = apVal[1];
192707 }
192708
192709 if( sqlite3_value_type(pNewRowid)!=SQLITE_NULL5 && (
192710 sqlite3_value_type(apVal[0])==SQLITE_NULL5
192711 || sqlite3_value_int64(apVal[0])!=sqlite3_value_int64(pNewRowid)
192712 )){
192713 /* The new rowid is not NULL (in this case the rowid will be
192714 ** automatically assigned and there is no chance of a conflict), and
192715 ** the statement is either an INSERT or an UPDATE that modifies the
192716 ** rowid column. So if the conflict mode is REPLACE, then delete any
192717 ** existing row with rowid=pNewRowid.
192718 **
192719 ** Or, if the conflict mode is not REPLACE, insert the new record into
192720 ** the %_content table. If we hit the duplicate rowid constraint (or any
192721 ** other error) while doing so, return immediately.
192722 **
192723 ** This branch may also run if pNewRowid contains a value that cannot
192724 ** be losslessly converted to an integer. In this case, the eventual
192725 ** call to fts3InsertData() (either just below or further on in this
192726 ** function) will return SQLITE_MISMATCH. If fts3DeleteByRowid is
192727 ** invoked, it will delete zero rows (since no row will have
192728 ** docid=$pNewRowid if $pNewRowid is not an integer value).
192729 */
192730 if( sqlite3_vtab_on_conflict(p->db)==SQLITE_REPLACE5 ){
192731 rc = fts3DeleteByRowid(p, pNewRowid, &nChng, aSzDel);
192732 }else{
192733 rc = fts3InsertData(p, apVal, pRowid);
192734 bInsertDone = 1;
192735 }
192736 }
192737 }
192738 if( rc!=SQLITE_OK0 ){
192739 goto update_out;
192740 }
192741
192742 /* If this is a DELETE or UPDATE operation, remove the old record. */
192743 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL5 ){
192744 assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER )((void) (0));
192745 rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
192746 }
192747
192748 /* If this is an INSERT or UPDATE operation, insert the new record. */
192749 if( nArg>1 && rc==SQLITE_OK0 ){
192750 int iLangid = sqlite3_value_int(apVal[2 + p->nColumn + 2]);
192751 if( bInsertDone==0 ){
192752 rc = fts3InsertData(p, apVal, pRowid);
192753 if( rc==SQLITE_CONSTRAINT19 && p->zContentTbl==0 ){
192754 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
192755 }
192756 }
192757 if( rc==SQLITE_OK0 ){
192758 rc = fts3PendingTermsDocid(p, 0, iLangid, *pRowid);
192759 }
192760 if( rc==SQLITE_OK0 ){
192761 assert( p->iPrevDocid==*pRowid )((void) (0));
192762 rc = fts3InsertTerms(p, iLangid, apVal, aSzIns);
192763 }
192764 if( p->bHasDocsize ){
192765 fts3InsertDocsize(&rc, p, aSzIns);
192766 }
192767 nChng++;
192768 }
192769
192770 if( p->bFts4 ){
192771 fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nChng);
192772 }
192773
192774 update_out:
192775 sqlite3_free(aSzDel);
192776 sqlite3Fts3SegmentsClose(p);
192777 return rc;
192778}
192779
192780/*
192781** Flush any data in the pending-terms hash table to disk. If successful,
192782** merge all segments in the database (including the new segment, if
192783** there was any data to flush) into a single segment.
192784*/
192785SQLITE_PRIVATEstatic int sqlite3Fts3Optimize(Fts3Table *p){
192786 int rc;
192787 rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
192788 if( rc==SQLITE_OK0 ){
192789 rc = fts3DoOptimize(p, 1);
192790 if( rc==SQLITE_OK0 || rc==SQLITE_DONE101 ){
192791 int rc2 = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
192792 if( rc2!=SQLITE_OK0 ) rc = rc2;
192793 }else{
192794 sqlite3_exec(p->db, "ROLLBACK TO fts3", 0, 0, 0);
192795 sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
192796 }
192797 }
192798 sqlite3Fts3SegmentsClose(p);
192799 return rc;
192800}
192801
192802#endif
192803
192804/************** End of fts3_write.c ******************************************/
192805/************** Begin file fts3_snippet.c ************************************/
192806/*
192807** 2009 Oct 23
192808**
192809** The author disclaims copyright to this source code. In place of
192810** a legal notice, here is a blessing:
192811**
192812** May you do good and not evil.
192813** May you find forgiveness for yourself and forgive others.
192814** May you share freely, never taking more than you give.
192815**
192816******************************************************************************
192817*/
192818
192819/* #include "fts3Int.h" */
192820#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
192821
192822/* #include <string.h> */
192823/* #include <assert.h> */
192824
192825#ifndef SQLITE_AMALGAMATION1
192826typedef sqlite3_int64 i64;
192827#endif
192828
192829/*
192830** Characters that may appear in the second argument to matchinfo().
192831*/
192832#define FTS3_MATCHINFO_NPHRASE'p' 'p' /* 1 value */
192833#define FTS3_MATCHINFO_NCOL'c' 'c' /* 1 value */
192834#define FTS3_MATCHINFO_NDOC'n' 'n' /* 1 value */
192835#define FTS3_MATCHINFO_AVGLENGTH'a' 'a' /* nCol values */
192836#define FTS3_MATCHINFO_LENGTH'l' 'l' /* nCol values */
192837#define FTS3_MATCHINFO_LCS's' 's' /* nCol values */
192838#define FTS3_MATCHINFO_HITS'x' 'x' /* 3*nCol*nPhrase values */
192839#define FTS3_MATCHINFO_LHITS'y' 'y' /* nCol*nPhrase values */
192840#define FTS3_MATCHINFO_LHITS_BM'b' 'b' /* nCol*nPhrase values */
192841
192842/*
192843** The default value for the second argument to matchinfo().
192844*/
192845#define FTS3_MATCHINFO_DEFAULT"pcx" "pcx"
192846
192847
192848/*
192849** Used as an fts3ExprIterate() context when loading phrase doclists to
192850** Fts3Expr.aDoclist[]/nDoclist.
192851*/
192852typedef struct LoadDoclistCtx LoadDoclistCtx;
192853struct LoadDoclistCtx {
192854 Fts3Cursor *pCsr; /* FTS3 Cursor */
192855 int nPhrase; /* Number of phrases seen so far */
192856 int nToken; /* Number of tokens seen so far */
192857};
192858
192859/*
192860** The following types are used as part of the implementation of the
192861** fts3BestSnippet() routine.
192862*/
192863typedef struct SnippetIter SnippetIter;
192864typedef struct SnippetPhrase SnippetPhrase;
192865typedef struct SnippetFragment SnippetFragment;
192866
192867struct SnippetIter {
192868 Fts3Cursor *pCsr; /* Cursor snippet is being generated from */
192869 int iCol; /* Extract snippet from this column */
192870 int nSnippet; /* Requested snippet length (in tokens) */
192871 int nPhrase; /* Number of phrases in query */
192872 SnippetPhrase *aPhrase; /* Array of size nPhrase */
192873 int iCurrent; /* First token of current snippet */
192874};
192875
192876struct SnippetPhrase {
192877 int nToken; /* Number of tokens in phrase */
192878 char *pList; /* Pointer to start of phrase position list */
192879 i64 iHead; /* Next value in position list */
192880 char *pHead; /* Position list data following iHead */
192881 i64 iTail; /* Next value in trailing position list */
192882 char *pTail; /* Position list data following iTail */
192883};
192884
192885struct SnippetFragment {
192886 int iCol; /* Column snippet is extracted from */
192887 int iPos; /* Index of first token in snippet */
192888 u64 covered; /* Mask of query phrases covered */
192889 u64 hlmask; /* Mask of snippet terms to highlight */
192890};
192891
192892/*
192893** This type is used as an fts3ExprIterate() context object while
192894** accumulating the data returned by the matchinfo() function.
192895*/
192896typedef struct MatchInfo MatchInfo;
192897struct MatchInfo {
192898 Fts3Cursor *pCursor; /* FTS3 Cursor */
192899 int nCol; /* Number of columns in table */
192900 int nPhrase; /* Number of matchable phrases in query */
192901 sqlite3_int64 nDoc; /* Number of docs in database */
192902 char flag;
192903 u32 *aMatchinfo; /* Pre-allocated buffer */
192904};
192905
192906/*
192907** An instance of this structure is used to manage a pair of buffers, each
192908** (nElem * sizeof(u32)) bytes in size. See the MatchinfoBuffer code below
192909** for details.
192910*/
192911struct MatchinfoBuffer {
192912 u8 aRef[3];
192913 int nElem;
192914 int bGlobal; /* Set if global data is loaded */
192915 char *zMatchinfo;
192916 u32 aMatchinfo[1];
192917};
192918
192919
192920/*
192921** The snippet() and offsets() functions both return text values. An instance
192922** of the following structure is used to accumulate those values while the
192923** functions are running. See fts3StringAppend() for details.
192924*/
192925typedef struct StrBuffer StrBuffer;
192926struct StrBuffer {
192927 char *z; /* Pointer to buffer containing string */
192928 int n; /* Length of z in bytes (excl. nul-term) */
192929 int nAlloc; /* Allocated size of buffer z in bytes */
192930};
192931
192932
192933/*************************************************************************
192934** Start of MatchinfoBuffer code.
192935*/
192936
192937/*
192938** Allocate a two-slot MatchinfoBuffer object.
192939*/
192940static MatchinfoBuffer *fts3MIBufferNew(size_t nElem, const char *zMatchinfo){
192941 MatchinfoBuffer *pRet;
192942 sqlite3_int64 nByte = sizeof(u32) * (2*(sqlite3_int64)nElem + 1)
192943 + sizeof(MatchinfoBuffer);
192944 sqlite3_int64 nStr = strlen(zMatchinfo);
192945
192946 pRet = sqlite3Fts3MallocZero(nByte + nStr+1);
192947 if( pRet ){
192948 pRet->aMatchinfo[0] = (u8*)(&pRet->aMatchinfo[1]) - (u8*)pRet;
192949 pRet->aMatchinfo[1+nElem] = pRet->aMatchinfo[0]
192950 + sizeof(u32)*((int)nElem+1);
192951 pRet->nElem = (int)nElem;
192952 pRet->zMatchinfo = ((char*)pRet) + nByte;
192953 memcpy(pRet->zMatchinfo, zMatchinfo, nStr+1);
192954 pRet->aRef[0] = 1;
192955 }
192956
192957 return pRet;
192958}
192959
192960static void fts3MIBufferFree(void *p){
192961 MatchinfoBuffer *pBuf = (MatchinfoBuffer*)((u8*)p - ((u32*)p)[-1]);
192962
192963 assert( (u32*)p==&pBuf->aMatchinfo[1]((void) (0))
192964 || (u32*)p==&pBuf->aMatchinfo[pBuf->nElem+2]((void) (0))
192965 )((void) (0));
192966 if( (u32*)p==&pBuf->aMatchinfo[1] ){
192967 pBuf->aRef[1] = 0;
192968 }else{
192969 pBuf->aRef[2] = 0;
192970 }
192971
192972 if( pBuf->aRef[0]==0 && pBuf->aRef[1]==0 && pBuf->aRef[2]==0 ){
192973 sqlite3_free(pBuf);
192974 }
192975}
192976
192977static void (*fts3MIBufferAlloc(MatchinfoBuffer *p, u32 **paOut))(void*){
192978 void (*xRet)(void*) = 0;
192979 u32 *aOut = 0;
192980
192981 if( p->aRef[1]==0 ){
192982 p->aRef[1] = 1;
192983 aOut = &p->aMatchinfo[1];
192984 xRet = fts3MIBufferFree;
192985 }
192986 else if( p->aRef[2]==0 ){
192987 p->aRef[2] = 1;
192988 aOut = &p->aMatchinfo[p->nElem+2];
192989 xRet = fts3MIBufferFree;
192990 }else{
192991 aOut = (u32*)sqlite3_malloc64(p->nElem * sizeof(u32));
192992 if( aOut ){
192993 xRet = sqlite3_free;
192994 if( p->bGlobal ) memcpy(aOut, &p->aMatchinfo[1], p->nElem*sizeof(u32));
192995 }
192996 }
192997
192998 *paOut = aOut;
192999 return xRet;
193000}
193001
193002static void fts3MIBufferSetGlobal(MatchinfoBuffer *p){
193003 p->bGlobal = 1;
193004 memcpy(&p->aMatchinfo[2+p->nElem], &p->aMatchinfo[1], p->nElem*sizeof(u32));
193005}
193006
193007/*
193008** Free a MatchinfoBuffer object allocated using fts3MIBufferNew()
193009*/
193010SQLITE_PRIVATEstatic void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p){
193011 if( p ){
193012 assert( p->aRef[0]==1 )((void) (0));
193013 p->aRef[0] = 0;
193014 if( p->aRef[0]==0 && p->aRef[1]==0 && p->aRef[2]==0 ){
193015 sqlite3_free(p);
193016 }
193017 }
193018}
193019
193020/*
193021** End of MatchinfoBuffer code.
193022*************************************************************************/
193023
193024
193025/*
193026** This function is used to help iterate through a position-list. A position
193027** list is a list of unique integers, sorted from smallest to largest. Each
193028** element of the list is represented by an FTS3 varint that takes the value
193029** of the difference between the current element and the previous one plus
193030** two. For example, to store the position-list:
193031**
193032** 4 9 113
193033**
193034** the three varints:
193035**
193036** 6 7 106
193037**
193038** are encoded.
193039**
193040** When this function is called, *pp points to the start of an element of
193041** the list. *piPos contains the value of the previous entry in the list.
193042** After it returns, *piPos contains the value of the next element of the
193043** list and *pp is advanced to the following varint.
193044*/
193045static void fts3GetDeltaPosition(char **pp, i64 *piPos){
193046 int iVal;
193047 *pp += fts3GetVarint32(*pp, &iVal)( (*(u8*)(*pp)&0x80) ? sqlite3Fts3GetVarint32(*pp, &iVal
) : (*&iVal=*(u8*)(*pp), 1) )
;
193048 *piPos += (iVal-2);
193049}
193050
193051/*
193052** Helper function for fts3ExprIterate() (see below).
193053*/
193054static int fts3ExprIterate2(
193055 Fts3Expr *pExpr, /* Expression to iterate phrases of */
193056 int *piPhrase, /* Pointer to phrase counter */
193057 int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */
193058 void *pCtx /* Second argument to pass to callback */
193059){
193060 int rc; /* Return code */
193061 int eType = pExpr->eType; /* Type of expression node pExpr */
193062
193063 if( eType!=FTSQUERY_PHRASE5 ){
193064 assert( pExpr->pLeft && pExpr->pRight )((void) (0));
193065 rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx);
193066 if( rc==SQLITE_OK0 && eType!=FTSQUERY_NOT2 ){
193067 rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx);
193068 }
193069 }else{
193070 rc = x(pExpr, *piPhrase, pCtx);
193071 (*piPhrase)++;
193072 }
193073 return rc;
193074}
193075
193076/*
193077** Iterate through all phrase nodes in an FTS3 query, except those that
193078** are part of a sub-tree that is the right-hand-side of a NOT operator.
193079** For each phrase node found, the supplied callback function is invoked.
193080**
193081** If the callback function returns anything other than SQLITE_OK,
193082** the iteration is abandoned and the error code returned immediately.
193083** Otherwise, SQLITE_OK is returned after a callback has been made for
193084** all eligible phrase nodes.
193085*/
193086static int fts3ExprIterate(
193087 Fts3Expr *pExpr, /* Expression to iterate phrases of */
193088 int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */
193089 void *pCtx /* Second argument to pass to callback */
193090){
193091 int iPhrase = 0; /* Variable used as the phrase counter */
193092 return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx);
193093}
193094
193095
193096/*
193097** This is an fts3ExprIterate() callback used while loading the doclists
193098** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
193099** fts3ExprLoadDoclists().
193100*/
193101static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
193102 int rc = SQLITE_OK0;
193103 Fts3Phrase *pPhrase = pExpr->pPhrase;
193104 LoadDoclistCtx *p = (LoadDoclistCtx *)ctx;
193105
193106 UNUSED_PARAMETER(iPhrase)(void)(iPhrase);
193107
193108 p->nPhrase++;
193109 p->nToken += pPhrase->nToken;
193110
193111 return rc;
193112}
193113
193114/*
193115** Load the doclists for each phrase in the query associated with FTS3 cursor
193116** pCsr.
193117**
193118** If pnPhrase is not NULL, then *pnPhrase is set to the number of matchable
193119** phrases in the expression (all phrases except those directly or
193120** indirectly descended from the right-hand-side of a NOT operator). If
193121** pnToken is not NULL, then it is set to the number of tokens in all
193122** matchable phrases of the expression.
193123*/
193124static int fts3ExprLoadDoclists(
193125 Fts3Cursor *pCsr, /* Fts3 cursor for current query */
193126 int *pnPhrase, /* OUT: Number of phrases in query */
193127 int *pnToken /* OUT: Number of tokens in query */
193128){
193129 int rc; /* Return Code */
193130 LoadDoclistCtx sCtx = {0,0,0}; /* Context for fts3ExprIterate() */
193131 sCtx.pCsr = pCsr;
193132 rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb, (void *)&sCtx);
193133 if( pnPhrase ) *pnPhrase = sCtx.nPhrase;
193134 if( pnToken ) *pnToken = sCtx.nToken;
193135 return rc;
193136}
193137
193138static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
193139 (*(int *)ctx)++;
193140 pExpr->iPhrase = iPhrase;
193141 return SQLITE_OK0;
193142}
193143static int fts3ExprPhraseCount(Fts3Expr *pExpr){
193144 int nPhrase = 0;
193145 (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase);
193146 return nPhrase;
193147}
193148
193149/*
193150** Advance the position list iterator specified by the first two
193151** arguments so that it points to the first element with a value greater
193152** than or equal to parameter iNext.
193153*/
193154static void fts3SnippetAdvance(char **ppIter, i64 *piIter, int iNext){
193155 char *pIter = *ppIter;
193156 if( pIter ){
193157 i64 iIter = *piIter;
193158
193159 while( iIter<iNext ){
193160 if( 0==(*pIter & 0xFE) ){
193161 iIter = -1;
193162 pIter = 0;
193163 break;
193164 }
193165 fts3GetDeltaPosition(&pIter, &iIter);
193166 }
193167
193168 *piIter = iIter;
193169 *ppIter = pIter;
193170 }
193171}
193172
193173/*
193174** Advance the snippet iterator to the next candidate snippet.
193175*/
193176static int fts3SnippetNextCandidate(SnippetIter *pIter){
193177 int i; /* Loop counter */
193178
193179 if( pIter->iCurrent<0 ){
193180 /* The SnippetIter object has just been initialized. The first snippet
193181 ** candidate always starts at offset 0 (even if this candidate has a
193182 ** score of 0.0).
193183 */
193184 pIter->iCurrent = 0;
193185
193186 /* Advance the 'head' iterator of each phrase to the first offset that
193187 ** is greater than or equal to (iNext+nSnippet).
193188 */
193189 for(i=0; i<pIter->nPhrase; i++){
193190 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
193191 fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, pIter->nSnippet);
193192 }
193193 }else{
193194 int iStart;
193195 int iEnd = 0x7FFFFFFF;
193196
193197 for(i=0; i<pIter->nPhrase; i++){
193198 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
193199 if( pPhrase->pHead && pPhrase->iHead<iEnd ){
193200 iEnd = pPhrase->iHead;
193201 }
193202 }
193203 if( iEnd==0x7FFFFFFF ){
193204 return 1;
193205 }
193206
193207 pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1;
193208 for(i=0; i<pIter->nPhrase; i++){
193209 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
193210 fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, iEnd+1);
193211 fts3SnippetAdvance(&pPhrase->pTail, &pPhrase->iTail, iStart);
193212 }
193213 }
193214
193215 return 0;
193216}
193217
193218/*
193219** Retrieve information about the current candidate snippet of snippet
193220** iterator pIter.
193221*/
193222static void fts3SnippetDetails(
193223 SnippetIter *pIter, /* Snippet iterator */
193224 u64 mCovered, /* Bitmask of phrases already covered */
193225 int *piToken, /* OUT: First token of proposed snippet */
193226 int *piScore, /* OUT: "Score" for this snippet */
193227 u64 *pmCover, /* OUT: Bitmask of phrases covered */
193228 u64 *pmHighlight /* OUT: Bitmask of terms to highlight */
193229){
193230 int iStart = pIter->iCurrent; /* First token of snippet */
193231 int iScore = 0; /* Score of this snippet */
193232 int i; /* Loop counter */
193233 u64 mCover = 0; /* Mask of phrases covered by this snippet */
193234 u64 mHighlight = 0; /* Mask of tokens to highlight in snippet */
193235
193236 for(i=0; i<pIter->nPhrase; i++){
193237 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
193238 if( pPhrase->pTail ){
193239 char *pCsr = pPhrase->pTail;
193240 i64 iCsr = pPhrase->iTail;
193241
193242 while( iCsr<(iStart+pIter->nSnippet) && iCsr>=iStart ){
193243 int j;
193244 u64 mPhrase = (u64)1 << (i%64);
193245 u64 mPos = (u64)1 << (iCsr - iStart);
193246 assert( iCsr>=iStart && (iCsr - iStart)<=64 )((void) (0));
193247 assert( i>=0 )((void) (0));
193248 if( (mCover|mCovered)&mPhrase ){
193249 iScore++;
193250 }else{
193251 iScore += 1000;
193252 }
193253 mCover |= mPhrase;
193254
193255 for(j=0; j<pPhrase->nToken; j++){
193256 mHighlight |= (mPos>>j);
193257 }
193258
193259 if( 0==(*pCsr & 0x0FE) ) break;
193260 fts3GetDeltaPosition(&pCsr, &iCsr);
193261 }
193262 }
193263 }
193264
193265 /* Set the output variables before returning. */
193266 *piToken = iStart;
193267 *piScore = iScore;
193268 *pmCover = mCover;
193269 *pmHighlight = mHighlight;
193270}
193271
193272/*
193273** This function is an fts3ExprIterate() callback used by fts3BestSnippet().
193274** Each invocation populates an element of the SnippetIter.aPhrase[] array.
193275*/
193276static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){
193277 SnippetIter *p = (SnippetIter *)ctx;
193278 SnippetPhrase *pPhrase = &p->aPhrase[iPhrase];
193279 char *pCsr;
193280 int rc;
193281
193282 pPhrase->nToken = pExpr->pPhrase->nToken;
193283 rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pCsr);
193284 assert( rc==SQLITE_OK || pCsr==0 )((void) (0));
193285 if( pCsr ){
193286 i64 iFirst = 0;
193287 pPhrase->pList = pCsr;
193288 fts3GetDeltaPosition(&pCsr, &iFirst);
193289 if( iFirst<0 ){
193290 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
193291 }else{
193292 pPhrase->pHead = pCsr;
193293 pPhrase->pTail = pCsr;
193294 pPhrase->iHead = iFirst;
193295 pPhrase->iTail = iFirst;
193296 }
193297 }else{
193298 assert( rc!=SQLITE_OK || (((void) (0))
193299 pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0((void) (0))
193300 ))((void) (0));
193301 }
193302
193303 return rc;
193304}
193305
193306/*
193307** Select the fragment of text consisting of nFragment contiguous tokens
193308** from column iCol that represent the "best" snippet. The best snippet
193309** is the snippet with the highest score, where scores are calculated
193310** by adding:
193311**
193312** (a) +1 point for each occurrence of a matchable phrase in the snippet.
193313**
193314** (b) +1000 points for the first occurrence of each matchable phrase in
193315** the snippet for which the corresponding mCovered bit is not set.
193316**
193317** The selected snippet parameters are stored in structure *pFragment before
193318** returning. The score of the selected snippet is stored in *piScore
193319** before returning.
193320*/
193321static int fts3BestSnippet(
193322 int nSnippet, /* Desired snippet length */
193323 Fts3Cursor *pCsr, /* Cursor to create snippet for */
193324 int iCol, /* Index of column to create snippet from */
193325 u64 mCovered, /* Mask of phrases already covered */
193326 u64 *pmSeen, /* IN/OUT: Mask of phrases seen */
193327 SnippetFragment *pFragment, /* OUT: Best snippet found */
193328 int *piScore /* OUT: Score of snippet pFragment */
193329){
193330 int rc; /* Return Code */
193331 int nList; /* Number of phrases in expression */
193332 SnippetIter sIter; /* Iterates through snippet candidates */
193333 sqlite3_int64 nByte; /* Number of bytes of space to allocate */
193334 int iBestScore = -1; /* Best snippet score found so far */
193335 int i; /* Loop counter */
193336
193337 memset(&sIter, 0, sizeof(sIter));
193338
193339 /* Iterate through the phrases in the expression to count them. The same
193340 ** callback makes sure the doclists are loaded for each phrase.
193341 */
193342 rc = fts3ExprLoadDoclists(pCsr, &nList, 0);
193343 if( rc!=SQLITE_OK0 ){
193344 return rc;
193345 }
193346
193347 /* Now that it is known how many phrases there are, allocate and zero
193348 ** the required space using malloc().
193349 */
193350 nByte = sizeof(SnippetPhrase) * nList;
193351 sIter.aPhrase = (SnippetPhrase *)sqlite3Fts3MallocZero(nByte);
193352 if( !sIter.aPhrase ){
193353 return SQLITE_NOMEM7;
193354 }
193355
193356 /* Initialize the contents of the SnippetIter object. Then iterate through
193357 ** the set of phrases in the expression to populate the aPhrase[] array.
193358 */
193359 sIter.pCsr = pCsr;
193360 sIter.iCol = iCol;
193361 sIter.nSnippet = nSnippet;
193362 sIter.nPhrase = nList;
193363 sIter.iCurrent = -1;
193364 rc = fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void*)&sIter);
193365 if( rc==SQLITE_OK0 ){
193366
193367 /* Set the *pmSeen output variable. */
193368 for(i=0; i<nList; i++){
193369 if( sIter.aPhrase[i].pHead ){
193370 *pmSeen |= (u64)1 << (i%64);
193371 }
193372 }
193373
193374 /* Loop through all candidate snippets. Store the best snippet in
193375 ** *pFragment. Store its associated 'score' in iBestScore.
193376 */
193377 pFragment->iCol = iCol;
193378 while( !fts3SnippetNextCandidate(&sIter) ){
193379 int iPos;
193380 int iScore;
193381 u64 mCover;
193382 u64 mHighlite;
193383 fts3SnippetDetails(&sIter, mCovered, &iPos, &iScore, &mCover,&mHighlite);
193384 assert( iScore>=0 )((void) (0));
193385 if( iScore>iBestScore ){
193386 pFragment->iPos = iPos;
193387 pFragment->hlmask = mHighlite;
193388 pFragment->covered = mCover;
193389 iBestScore = iScore;
193390 }
193391 }
193392
193393 *piScore = iBestScore;
193394 }
193395 sqlite3_free(sIter.aPhrase);
193396 return rc;
193397}
193398
193399
193400/*
193401** Append a string to the string-buffer passed as the first argument.
193402**
193403** If nAppend is negative, then the length of the string zAppend is
193404** determined using strlen().
193405*/
193406static int fts3StringAppend(
193407 StrBuffer *pStr, /* Buffer to append to */
193408 const char *zAppend, /* Pointer to data to append to buffer */
193409 int nAppend /* Size of zAppend in bytes (or -1) */
193410){
193411 if( nAppend<0 ){
193412 nAppend = (int)strlen(zAppend);
193413 }
193414
193415 /* If there is insufficient space allocated at StrBuffer.z, use realloc()
193416 ** to grow the buffer until so that it is big enough to accomadate the
193417 ** appended data.
193418 */
193419 if( pStr->n+nAppend+1>=pStr->nAlloc ){
193420 sqlite3_int64 nAlloc = pStr->nAlloc+(sqlite3_int64)nAppend+100;
193421 char *zNew = sqlite3_realloc64(pStr->z, nAlloc);
193422 if( !zNew ){
193423 return SQLITE_NOMEM7;
193424 }
193425 pStr->z = zNew;
193426 pStr->nAlloc = nAlloc;
193427 }
193428 assert( pStr->z!=0 && (pStr->nAlloc >= pStr->n+nAppend+1) )((void) (0));
193429
193430 /* Append the data to the string buffer. */
193431 memcpy(&pStr->z[pStr->n], zAppend, nAppend);
193432 pStr->n += nAppend;
193433 pStr->z[pStr->n] = '\0';
193434
193435 return SQLITE_OK0;
193436}
193437
193438/*
193439** The fts3BestSnippet() function often selects snippets that end with a
193440** query term. That is, the final term of the snippet is always a term
193441** that requires highlighting. For example, if 'X' is a highlighted term
193442** and '.' is a non-highlighted term, BestSnippet() may select:
193443**
193444** ........X.....X
193445**
193446** This function "shifts" the beginning of the snippet forward in the
193447** document so that there are approximately the same number of
193448** non-highlighted terms to the right of the final highlighted term as there
193449** are to the left of the first highlighted term. For example, to this:
193450**
193451** ....X.....X....
193452**
193453** This is done as part of extracting the snippet text, not when selecting
193454** the snippet. Snippet selection is done based on doclists only, so there
193455** is no way for fts3BestSnippet() to know whether or not the document
193456** actually contains terms that follow the final highlighted term.
193457*/
193458static int fts3SnippetShift(
193459 Fts3Table *pTab, /* FTS3 table snippet comes from */
193460 int iLangid, /* Language id to use in tokenizing */
193461 int nSnippet, /* Number of tokens desired for snippet */
193462 const char *zDoc, /* Document text to extract snippet from */
193463 int nDoc, /* Size of buffer zDoc in bytes */
193464 int *piPos, /* IN/OUT: First token of snippet */
193465 u64 *pHlmask /* IN/OUT: Mask of tokens to highlight */
193466){
193467 u64 hlmask = *pHlmask; /* Local copy of initial highlight-mask */
193468
193469 if( hlmask ){
193470 int nLeft; /* Tokens to the left of first highlight */
193471 int nRight; /* Tokens to the right of last highlight */
193472 int nDesired; /* Ideal number of tokens to shift forward */
193473
193474 for(nLeft=0; !(hlmask & ((u64)1 << nLeft)); nLeft++);
193475 for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++);
193476 assert( (nSnippet-1-nRight)<=63 && (nSnippet-1-nRight)>=0 )((void) (0));
193477 nDesired = (nLeft-nRight)/2;
193478
193479 /* Ideally, the start of the snippet should be pushed forward in the
193480 ** document nDesired tokens. This block checks if there are actually
193481 ** nDesired tokens to the right of the snippet. If so, *piPos and
193482 ** *pHlMask are updated to shift the snippet nDesired tokens to the
193483 ** right. Otherwise, the snippet is shifted by the number of tokens
193484 ** available.
193485 */
193486 if( nDesired>0 ){
193487 int nShift; /* Number of tokens to shift snippet by */
193488 int iCurrent = 0; /* Token counter */
193489 int rc; /* Return Code */
193490 sqlite3_tokenizer_module *pMod;
193491 sqlite3_tokenizer_cursor *pC;
193492 pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
193493
193494 /* Open a cursor on zDoc/nDoc. Check if there are (nSnippet+nDesired)
193495 ** or more tokens in zDoc/nDoc.
193496 */
193497 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, iLangid, zDoc, nDoc, &pC);
193498 if( rc!=SQLITE_OK0 ){
193499 return rc;
193500 }
193501 while( rc==SQLITE_OK0 && iCurrent<(nSnippet+nDesired) ){
193502 const char *ZDUMMY; int DUMMY1 = 0, DUMMY2 = 0, DUMMY3 = 0;
193503 rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent);
193504 }
193505 pMod->xClose(pC);
193506 if( rc!=SQLITE_OK0 && rc!=SQLITE_DONE101 ){ return rc; }
193507
193508 nShift = (rc==SQLITE_DONE101)+iCurrent-nSnippet;
193509 assert( nShift<=nDesired )((void) (0));
193510 if( nShift>0 ){
193511 *piPos += nShift;
193512 *pHlmask = hlmask >> nShift;
193513 }
193514 }
193515 }
193516 return SQLITE_OK0;
193517}
193518
193519/*
193520** Extract the snippet text for fragment pFragment from cursor pCsr and
193521** append it to string buffer pOut.
193522*/
193523static int fts3SnippetText(
193524 Fts3Cursor *pCsr, /* FTS3 Cursor */
193525 SnippetFragment *pFragment, /* Snippet to extract */
193526 int iFragment, /* Fragment number */
193527 int isLast, /* True for final fragment in snippet */
193528 int nSnippet, /* Number of tokens in extracted snippet */
193529 const char *zOpen, /* String inserted before highlighted term */
193530 const char *zClose, /* String inserted after highlighted term */
193531 const char *zEllipsis, /* String inserted between snippets */
193532 StrBuffer *pOut /* Write output here */
193533){
193534 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
193535 int rc; /* Return code */
193536 const char *zDoc; /* Document text to extract snippet from */
193537 int nDoc; /* Size of zDoc in bytes */
193538 int iCurrent = 0; /* Current token number of document */
193539 int iEnd = 0; /* Byte offset of end of current token */
193540 int isShiftDone = 0; /* True after snippet is shifted */
193541 int iPos = pFragment->iPos; /* First token of snippet */
193542 u64 hlmask = pFragment->hlmask; /* Highlight-mask for snippet */
193543 int iCol = pFragment->iCol+1; /* Query column to extract text from */
193544 sqlite3_tokenizer_module *pMod; /* Tokenizer module methods object */
193545 sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor open on zDoc/nDoc */
193546
193547 zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol);
193548 if( zDoc==0 ){
193549 if( sqlite3_column_type(pCsr->pStmt, iCol)!=SQLITE_NULL5 ){
193550 return SQLITE_NOMEM7;
193551 }
193552 return SQLITE_OK0;
193553 }
193554 nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol);
193555
193556 /* Open a token cursor on the document. */
193557 pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
193558 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid, zDoc,nDoc,&pC);
193559 if( rc!=SQLITE_OK0 ){
193560 return rc;
193561 }
193562
193563 while( rc==SQLITE_OK0 ){
193564 const char *ZDUMMY; /* Dummy argument used with tokenizer */
193565 int DUMMY1 = -1; /* Dummy argument used with tokenizer */
193566 int iBegin = 0; /* Offset in zDoc of start of token */
193567 int iFin = 0; /* Offset in zDoc of end of token */
193568 int isHighlight = 0; /* True for highlighted terms */
193569
193570 /* Variable DUMMY1 is initialized to a negative value above. Elsewhere
193571 ** in the FTS code the variable that the third argument to xNext points to
193572 ** is initialized to zero before the first (*but not necessarily
193573 ** subsequent*) call to xNext(). This is done for a particular application
193574 ** that needs to know whether or not the tokenizer is being used for
193575 ** snippet generation or for some other purpose.
193576 **
193577 ** Extreme care is required when writing code to depend on this
193578 ** initialization. It is not a documented part of the tokenizer interface.
193579 ** If a tokenizer is used directly by any code outside of FTS, this
193580 ** convention might not be respected. */
193581 rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent);
193582 if( rc!=SQLITE_OK0 ){
193583 if( rc==SQLITE_DONE101 ){
193584 /* Special case - the last token of the snippet is also the last token
193585 ** of the column. Append any punctuation that occurred between the end
193586 ** of the previous token and the end of the document to the output.
193587 ** Then break out of the loop. */
193588 rc = fts3StringAppend(pOut, &zDoc[iEnd], -1);
193589 }
193590 break;
193591 }
193592 if( iCurrent<iPos ){ continue; }
193593
193594 if( !isShiftDone ){
193595 int n = nDoc - iBegin;
193596 rc = fts3SnippetShift(
193597 pTab, pCsr->iLangid, nSnippet, &zDoc[iBegin], n, &iPos, &hlmask
193598 );
193599 isShiftDone = 1;
193600
193601 /* Now that the shift has been done, check if the initial "..." are
193602 ** required. They are required if (a) this is not the first fragment,
193603 ** or (b) this fragment does not begin at position 0 of its column.
193604 */
193605 if( rc==SQLITE_OK0 ){
193606 if( iPos>0 || iFragment>0 ){
193607 rc = fts3StringAppend(pOut, zEllipsis, -1);
193608 }else if( iBegin ){
193609 rc = fts3StringAppend(pOut, zDoc, iBegin);
193610 }
193611 }
193612 if( rc!=SQLITE_OK0 || iCurrent<iPos ) continue;
193613 }
193614
193615 if( iCurrent>=(iPos+nSnippet) ){
193616 if( isLast ){
193617 rc = fts3StringAppend(pOut, zEllipsis, -1);
193618 }
193619 break;
193620 }
193621
193622 /* Set isHighlight to true if this term should be highlighted. */
193623 isHighlight = (hlmask & ((u64)1 << (iCurrent-iPos)))!=0;
193624
193625 if( iCurrent>iPos ) rc = fts3StringAppend(pOut, &zDoc[iEnd], iBegin-iEnd);
193626 if( rc==SQLITE_OK0 && isHighlight ) rc = fts3StringAppend(pOut, zOpen, -1);
193627 if( rc==SQLITE_OK0 ) rc = fts3StringAppend(pOut, &zDoc[iBegin], iFin-iBegin);
193628 if( rc==SQLITE_OK0 && isHighlight ) rc = fts3StringAppend(pOut, zClose, -1);
193629
193630 iEnd = iFin;
193631 }
193632
193633 pMod->xClose(pC);
193634 return rc;
193635}
193636
193637
193638/*
193639** This function is used to count the entries in a column-list (a
193640** delta-encoded list of term offsets within a single column of a single
193641** row). When this function is called, *ppCollist should point to the
193642** beginning of the first varint in the column-list (the varint that
193643** contains the position of the first matching term in the column data).
193644** Before returning, *ppCollist is set to point to the first byte after
193645** the last varint in the column-list (either the 0x00 signifying the end
193646** of the position-list, or the 0x01 that precedes the column number of
193647** the next column in the position-list).
193648**
193649** The number of elements in the column-list is returned.
193650*/
193651static int fts3ColumnlistCount(char **ppCollist){
193652 char *pEnd = *ppCollist;
193653 char c = 0;
193654 int nEntry = 0;
193655
193656 /* A column-list is terminated by either a 0x01 or 0x00. */
193657 while( 0xFE & (*pEnd | c) ){
193658 c = *pEnd++ & 0x80;
193659 if( !c ) nEntry++;
193660 }
193661
193662 *ppCollist = pEnd;
193663 return nEntry;
193664}
193665
193666/*
193667** This function gathers 'y' or 'b' data for a single phrase.
193668*/
193669static int fts3ExprLHits(
193670 Fts3Expr *pExpr, /* Phrase expression node */
193671 MatchInfo *p /* Matchinfo context */
193672){
193673 Fts3Table *pTab = (Fts3Table *)p->pCursor->base.pVtab;
193674 int iStart;
193675 Fts3Phrase *pPhrase = pExpr->pPhrase;
193676 char *pIter = pPhrase->doclist.pList;
193677 int iCol = 0;
193678
193679 assert( p->flag==FTS3_MATCHINFO_LHITS_BM || p->flag==FTS3_MATCHINFO_LHITS )((void) (0));
193680 if( p->flag==FTS3_MATCHINFO_LHITS'y' ){
193681 iStart = pExpr->iPhrase * p->nCol;
193682 }else{
193683 iStart = pExpr->iPhrase * ((p->nCol + 31) / 32);
193684 }
193685
193686 if( pIter ) while( 1 ){
193687 int nHit = fts3ColumnlistCount(&pIter);
193688 if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){
193689 if( p->flag==FTS3_MATCHINFO_LHITS'y' ){
193690 p->aMatchinfo[iStart + iCol] = (u32)nHit;
193691 }else if( nHit ){
193692 p->aMatchinfo[iStart + (iCol+1)/32] |= (1 << (iCol&0x1F));
193693 }
193694 }
193695 assert( *pIter==0x00 || *pIter==0x01 )((void) (0));
193696 if( *pIter!=0x01 ) break;
193697 pIter++;
193698 pIter += fts3GetVarint32(pIter, &iCol)( (*(u8*)(pIter)&0x80) ? sqlite3Fts3GetVarint32(pIter, &
iCol) : (*&iCol=*(u8*)(pIter), 1) )
;
193699 if( iCol>=p->nCol ) return FTS_CORRUPT_VTAB(11 | (1<<8));
193700 }
193701 return SQLITE_OK0;
193702}
193703
193704/*
193705** Gather the results for matchinfo directives 'y' and 'b'.
193706*/
193707static int fts3ExprLHitGather(
193708 Fts3Expr *pExpr,
193709 MatchInfo *p
193710){
193711 int rc = SQLITE_OK0;
193712 assert( (pExpr->pLeft==0)==(pExpr->pRight==0) )((void) (0));
193713 if( pExpr->bEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){
193714 if( pExpr->pLeft ){
193715 rc = fts3ExprLHitGather(pExpr->pLeft, p);
193716 if( rc==SQLITE_OK0 ) rc = fts3ExprLHitGather(pExpr->pRight, p);
193717 }else{
193718 rc = fts3ExprLHits(pExpr, p);
193719 }
193720 }
193721 return rc;
193722}
193723
193724/*
193725** fts3ExprIterate() callback used to collect the "global" matchinfo stats
193726** for a single query.
193727**
193728** fts3ExprIterate() callback to load the 'global' elements of a
193729** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements
193730** of the matchinfo array that are constant for all rows returned by the
193731** current query.
193732**
193733** Argument pCtx is actually a pointer to a struct of type MatchInfo. This
193734** function populates Matchinfo.aMatchinfo[] as follows:
193735**
193736** for(iCol=0; iCol<nCol; iCol++){
193737** aMatchinfo[3*iPhrase*nCol + 3*iCol + 1] = X;
193738** aMatchinfo[3*iPhrase*nCol + 3*iCol + 2] = Y;
193739** }
193740**
193741** where X is the number of matches for phrase iPhrase is column iCol of all
193742** rows of the table. Y is the number of rows for which column iCol contains
193743** at least one instance of phrase iPhrase.
193744**
193745** If the phrase pExpr consists entirely of deferred tokens, then all X and
193746** Y values are set to nDoc, where nDoc is the number of documents in the
193747** file system. This is done because the full-text index doclist is required
193748** to calculate these values properly, and the full-text index doclist is
193749** not available for deferred tokens.
193750*/
193751static int fts3ExprGlobalHitsCb(
193752 Fts3Expr *pExpr, /* Phrase expression node */
193753 int iPhrase, /* Phrase number (numbered from zero) */
193754 void *pCtx /* Pointer to MatchInfo structure */
193755){
193756 MatchInfo *p = (MatchInfo *)pCtx;
193757 return sqlite3Fts3EvalPhraseStats(
193758 p->pCursor, pExpr, &p->aMatchinfo[3*iPhrase*p->nCol]
193759 );
193760}
193761
193762/*
193763** fts3ExprIterate() callback used to collect the "local" part of the
193764** FTS3_MATCHINFO_HITS array. The local stats are those elements of the
193765** array that are different for each row returned by the query.
193766*/
193767static int fts3ExprLocalHitsCb(
193768 Fts3Expr *pExpr, /* Phrase expression node */
193769 int iPhrase, /* Phrase number */
193770 void *pCtx /* Pointer to MatchInfo structure */
193771){
193772 int rc = SQLITE_OK0;
193773 MatchInfo *p = (MatchInfo *)pCtx;
193774 int iStart = iPhrase * p->nCol * 3;
193775 int i;
193776
193777 for(i=0; i<p->nCol && rc==SQLITE_OK0; i++){
193778 char *pCsr;
193779 rc = sqlite3Fts3EvalPhrasePoslist(p->pCursor, pExpr, i, &pCsr);
193780 if( pCsr ){
193781 p->aMatchinfo[iStart+i*3] = fts3ColumnlistCount(&pCsr);
193782 }else{
193783 p->aMatchinfo[iStart+i*3] = 0;
193784 }
193785 }
193786
193787 return rc;
193788}
193789
193790static int fts3MatchinfoCheck(
193791 Fts3Table *pTab,
193792 char cArg,
193793 char **pzErr
193794){
193795 if( (cArg==FTS3_MATCHINFO_NPHRASE'p')
193796 || (cArg==FTS3_MATCHINFO_NCOL'c')
193797 || (cArg==FTS3_MATCHINFO_NDOC'n' && pTab->bFts4)
193798 || (cArg==FTS3_MATCHINFO_AVGLENGTH'a' && pTab->bFts4)
193799 || (cArg==FTS3_MATCHINFO_LENGTH'l' && pTab->bHasDocsize)
193800 || (cArg==FTS3_MATCHINFO_LCS's')
193801 || (cArg==FTS3_MATCHINFO_HITS'x')
193802 || (cArg==FTS3_MATCHINFO_LHITS'y')
193803 || (cArg==FTS3_MATCHINFO_LHITS_BM'b')
193804 ){
193805 return SQLITE_OK0;
193806 }
193807 sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo request: %c", cArg);
193808 return SQLITE_ERROR1;
193809}
193810
193811static size_t fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
193812 size_t nVal; /* Number of integers output by cArg */
193813
193814 switch( cArg ){
193815 case FTS3_MATCHINFO_NDOC'n':
193816 case FTS3_MATCHINFO_NPHRASE'p':
193817 case FTS3_MATCHINFO_NCOL'c':
193818 nVal = 1;
193819 break;
193820
193821 case FTS3_MATCHINFO_AVGLENGTH'a':
193822 case FTS3_MATCHINFO_LENGTH'l':
193823 case FTS3_MATCHINFO_LCS's':
193824 nVal = pInfo->nCol;
193825 break;
193826
193827 case FTS3_MATCHINFO_LHITS'y':
193828 nVal = pInfo->nCol * pInfo->nPhrase;
193829 break;
193830
193831 case FTS3_MATCHINFO_LHITS_BM'b':
193832 nVal = pInfo->nPhrase * ((pInfo->nCol + 31) / 32);
193833 break;
193834
193835 default:
193836 assert( cArg==FTS3_MATCHINFO_HITS )((void) (0));
193837 nVal = pInfo->nCol * pInfo->nPhrase * 3;
193838 break;
193839 }
193840
193841 return nVal;
193842}
193843
193844static int fts3MatchinfoSelectDoctotal(
193845 Fts3Table *pTab,
193846 sqlite3_stmt **ppStmt,
193847 sqlite3_int64 *pnDoc,
193848 const char **paLen,
193849 const char **ppEnd
193850){
193851 sqlite3_stmt *pStmt;
193852 const char *a;
193853 const char *pEnd;
193854 sqlite3_int64 nDoc;
193855 int n;
193856
193857
193858 if( !*ppStmt ){
193859 int rc = sqlite3Fts3SelectDoctotal(pTab, ppStmt);
193860 if( rc!=SQLITE_OK0 ) return rc;
193861 }
193862 pStmt = *ppStmt;
193863 assert( sqlite3_data_count(pStmt)==1 )((void) (0));
193864
193865 n = sqlite3_column_bytes(pStmt, 0);
193866 a = sqlite3_column_blob(pStmt, 0);
193867 if( a==0 ){
193868 return FTS_CORRUPT_VTAB(11 | (1<<8));
193869 }
193870 pEnd = a + n;
193871 a += sqlite3Fts3GetVarintBounded(a, pEnd, &nDoc);
193872 if( nDoc<=0 || a>pEnd ){
193873 return FTS_CORRUPT_VTAB(11 | (1<<8));
193874 }
193875 *pnDoc = nDoc;
193876
193877 if( paLen ) *paLen = a;
193878 if( ppEnd ) *ppEnd = pEnd;
193879 return SQLITE_OK0;
193880}
193881
193882/*
193883** An instance of the following structure is used to store state while
193884** iterating through a multi-column position-list corresponding to the
193885** hits for a single phrase on a single row in order to calculate the
193886** values for a matchinfo() FTS3_MATCHINFO_LCS request.
193887*/
193888typedef struct LcsIterator LcsIterator;
193889struct LcsIterator {
193890 Fts3Expr *pExpr; /* Pointer to phrase expression */
193891 int iPosOffset; /* Tokens count up to end of this phrase */
193892 char *pRead; /* Cursor used to iterate through aDoclist */
193893 int iPos; /* Current position */
193894};
193895
193896/*
193897** If LcsIterator.iCol is set to the following value, the iterator has
193898** finished iterating through all offsets for all columns.
193899*/
193900#define LCS_ITERATOR_FINISHED0x7FFFFFFF; 0x7FFFFFFF;
193901
193902static int fts3MatchinfoLcsCb(
193903 Fts3Expr *pExpr, /* Phrase expression node */
193904 int iPhrase, /* Phrase number (numbered from zero) */
193905 void *pCtx /* Pointer to MatchInfo structure */
193906){
193907 LcsIterator *aIter = (LcsIterator *)pCtx;
193908 aIter[iPhrase].pExpr = pExpr;
193909 return SQLITE_OK0;
193910}
193911
193912/*
193913** Advance the iterator passed as an argument to the next position. Return
193914** 1 if the iterator is at EOF or if it now points to the start of the
193915** position list for the next column.
193916*/
193917static int fts3LcsIteratorAdvance(LcsIterator *pIter){
193918 char *pRead;
193919 sqlite3_int64 iRead;
193920 int rc = 0;
193921
193922 if( NEVER(pIter==0)(pIter==0) ) return 1;
193923 pRead = pIter->pRead;
193924 pRead += sqlite3Fts3GetVarint(pRead, &iRead);
193925 if( iRead==0 || iRead==1 ){
193926 pRead = 0;
193927 rc = 1;
193928 }else{
193929 pIter->iPos += (int)(iRead-2);
193930 }
193931
193932 pIter->pRead = pRead;
193933 return rc;
193934}
193935
193936/*
193937** This function implements the FTS3_MATCHINFO_LCS matchinfo() flag.
193938**
193939** If the call is successful, the longest-common-substring lengths for each
193940** column are written into the first nCol elements of the pInfo->aMatchinfo[]
193941** array before returning. SQLITE_OK is returned in this case.
193942**
193943** Otherwise, if an error occurs, an SQLite error code is returned and the
193944** data written to the first nCol elements of pInfo->aMatchinfo[] is
193945** undefined.
193946*/
193947static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){
193948 LcsIterator *aIter;
193949 int i;
193950 int iCol;
193951 int nToken = 0;
193952 int rc = SQLITE_OK0;
193953
193954 /* Allocate and populate the array of LcsIterator objects. The array
193955 ** contains one element for each matchable phrase in the query.
193956 **/
193957 aIter = sqlite3Fts3MallocZero(sizeof(LcsIterator) * pCsr->nPhrase);
193958 if( !aIter ) return SQLITE_NOMEM7;
193959 (void)fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter);
193960
193961 for(i=0; i<pInfo->nPhrase; i++){
193962 LcsIterator *pIter = &aIter[i];
193963 nToken -= pIter->pExpr->pPhrase->nToken;
193964 pIter->iPosOffset = nToken;
193965 }
193966
193967 for(iCol=0; iCol<pInfo->nCol; iCol++){
193968 int nLcs = 0; /* LCS value for this column */
193969 int nLive = 0; /* Number of iterators in aIter not at EOF */
193970
193971 for(i=0; i<pInfo->nPhrase; i++){
193972 LcsIterator *pIt = &aIter[i];
193973 rc = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol, &pIt->pRead);
193974 if( rc!=SQLITE_OK0 ) goto matchinfo_lcs_out;
193975 if( pIt->pRead ){
193976 pIt->iPos = pIt->iPosOffset;
193977 fts3LcsIteratorAdvance(pIt);
193978 if( pIt->pRead==0 ){
193979 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
193980 goto matchinfo_lcs_out;
193981 }
193982 nLive++;
193983 }
193984 }
193985
193986 while( nLive>0 ){
193987 LcsIterator *pAdv = 0; /* The iterator to advance by one position */
193988 int nThisLcs = 0; /* LCS for the current iterator positions */
193989
193990 for(i=0; i<pInfo->nPhrase; i++){
193991 LcsIterator *pIter = &aIter[i];
193992 if( pIter->pRead==0 ){
193993 /* This iterator is already at EOF for this column. */
193994 nThisLcs = 0;
193995 }else{
193996 if( pAdv==0 || pIter->iPos<pAdv->iPos ){
193997 pAdv = pIter;
193998 }
193999 if( nThisLcs==0 || pIter->iPos==pIter[-1].iPos ){
194000 nThisLcs++;
194001 }else{
194002 nThisLcs = 1;
194003 }
194004 if( nThisLcs>nLcs ) nLcs = nThisLcs;
194005 }
194006 }
194007 if( fts3LcsIteratorAdvance(pAdv) ) nLive--;
194008 }
194009
194010 pInfo->aMatchinfo[iCol] = nLcs;
194011 }
194012
194013 matchinfo_lcs_out:
194014 sqlite3_free(aIter);
194015 return rc;
194016}
194017
194018/*
194019** Populate the buffer pInfo->aMatchinfo[] with an array of integers to
194020** be returned by the matchinfo() function. Argument zArg contains the
194021** format string passed as the second argument to matchinfo (or the
194022** default value "pcx" if no second argument was specified). The format
194023** string has already been validated and the pInfo->aMatchinfo[] array
194024** is guaranteed to be large enough for the output.
194025**
194026** If bGlobal is true, then populate all fields of the matchinfo() output.
194027** If it is false, then assume that those fields that do not change between
194028** rows (i.e. FTS3_MATCHINFO_NPHRASE, NCOL, NDOC, AVGLENGTH and part of HITS)
194029** have already been populated.
194030**
194031** Return SQLITE_OK if successful, or an SQLite error code if an error
194032** occurs. If a value other than SQLITE_OK is returned, the state the
194033** pInfo->aMatchinfo[] buffer is left in is undefined.
194034*/
194035static int fts3MatchinfoValues(
194036 Fts3Cursor *pCsr, /* FTS3 cursor object */
194037 int bGlobal, /* True to grab the global stats */
194038 MatchInfo *pInfo, /* Matchinfo context object */
194039 const char *zArg /* Matchinfo format string */
194040){
194041 int rc = SQLITE_OK0;
194042 int i;
194043 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
194044 sqlite3_stmt *pSelect = 0;
194045
194046 for(i=0; rc==SQLITE_OK0 && zArg[i]; i++){
194047 pInfo->flag = zArg[i];
194048 switch( zArg[i] ){
194049 case FTS3_MATCHINFO_NPHRASE'p':
194050 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
194051 break;
194052
194053 case FTS3_MATCHINFO_NCOL'c':
194054 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol;
194055 break;
194056
194057 case FTS3_MATCHINFO_NDOC'n':
194058 if( bGlobal ){
194059 sqlite3_int64 nDoc = 0;
194060 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, 0, 0);
194061 pInfo->aMatchinfo[0] = (u32)nDoc;
194062 }
194063 break;
194064
194065 case FTS3_MATCHINFO_AVGLENGTH'a':
194066 if( bGlobal ){
194067 sqlite3_int64 nDoc; /* Number of rows in table */
194068 const char *a; /* Aggregate column length array */
194069 const char *pEnd; /* First byte past end of length array */
194070
194071 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a, &pEnd);
194072 if( rc==SQLITE_OK0 ){
194073 int iCol;
194074 for(iCol=0; iCol<pInfo->nCol; iCol++){
194075 u32 iVal;
194076 sqlite3_int64 nToken;
194077 a += sqlite3Fts3GetVarint(a, &nToken);
194078 if( a>pEnd ){
194079 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
194080 break;
194081 }
194082 iVal = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc);
194083 pInfo->aMatchinfo[iCol] = iVal;
194084 }
194085 }
194086 }
194087 break;
194088
194089 case FTS3_MATCHINFO_LENGTH'l': {
194090 sqlite3_stmt *pSelectDocsize = 0;
194091 rc = sqlite3Fts3SelectDocsize(pTab, pCsr->iPrevId, &pSelectDocsize);
194092 if( rc==SQLITE_OK0 ){
194093 int iCol;
194094 const char *a = sqlite3_column_blob(pSelectDocsize, 0);
194095 const char *pEnd = a + sqlite3_column_bytes(pSelectDocsize, 0);
194096 for(iCol=0; iCol<pInfo->nCol; iCol++){
194097 sqlite3_int64 nToken;
194098 a += sqlite3Fts3GetVarintBounded(a, pEnd, &nToken);
194099 if( a>pEnd ){
194100 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
194101 break;
194102 }
194103 pInfo->aMatchinfo[iCol] = (u32)nToken;
194104 }
194105 }
194106 sqlite3_reset(pSelectDocsize);
194107 break;
194108 }
194109
194110 case FTS3_MATCHINFO_LCS's':
194111 rc = fts3ExprLoadDoclists(pCsr, 0, 0);
194112 if( rc==SQLITE_OK0 ){
194113 rc = fts3MatchinfoLcs(pCsr, pInfo);
194114 }
194115 break;
194116
194117 case FTS3_MATCHINFO_LHITS_BM'b':
194118 case FTS3_MATCHINFO_LHITS'y': {
194119 size_t nZero = fts3MatchinfoSize(pInfo, zArg[i]) * sizeof(u32);
194120 memset(pInfo->aMatchinfo, 0, nZero);
194121 rc = fts3ExprLHitGather(pCsr->pExpr, pInfo);
194122 break;
194123 }
194124
194125 default: {
194126 Fts3Expr *pExpr;
194127 assert( zArg[i]==FTS3_MATCHINFO_HITS )((void) (0));
194128 pExpr = pCsr->pExpr;
194129 rc = fts3ExprLoadDoclists(pCsr, 0, 0);
194130 if( rc!=SQLITE_OK0 ) break;
194131 if( bGlobal ){
194132 if( pCsr->pDeferred ){
194133 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc,0,0);
194134 if( rc!=SQLITE_OK0 ) break;
194135 }
194136 rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo);
194137 sqlite3Fts3EvalTestDeferred(pCsr, &rc);
194138 if( rc!=SQLITE_OK0 ) break;
194139 }
194140 (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo);
194141 break;
194142 }
194143 }
194144
194145 pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, zArg[i]);
194146 }
194147
194148 sqlite3_reset(pSelect);
194149 return rc;
194150}
194151
194152
194153/*
194154** Populate pCsr->aMatchinfo[] with data for the current row. The
194155** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
194156*/
194157static void fts3GetMatchinfo(
194158 sqlite3_context *pCtx, /* Return results here */
194159 Fts3Cursor *pCsr, /* FTS3 Cursor object */
194160 const char *zArg /* Second argument to matchinfo() function */
194161){
194162 MatchInfo sInfo;
194163 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
194164 int rc = SQLITE_OK0;
194165 int bGlobal = 0; /* Collect 'global' stats as well as local */
194166
194167 u32 *aOut = 0;
194168 void (*xDestroyOut)(void*) = 0;
194169
194170 memset(&sInfo, 0, sizeof(MatchInfo));
194171 sInfo.pCursor = pCsr;
194172 sInfo.nCol = pTab->nColumn;
194173
194174 /* If there is cached matchinfo() data, but the format string for the
194175 ** cache does not match the format string for this request, discard
194176 ** the cached data. */
194177 if( pCsr->pMIBuffer && strcmp(pCsr->pMIBuffer->zMatchinfo, zArg) ){
194178 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
194179 pCsr->pMIBuffer = 0;
194180 }
194181
194182 /* If Fts3Cursor.pMIBuffer is NULL, then this is the first time the
194183 ** matchinfo function has been called for this query. In this case
194184 ** allocate the array used to accumulate the matchinfo data and
194185 ** initialize those elements that are constant for every row.
194186 */
194187 if( pCsr->pMIBuffer==0 ){
194188 size_t nMatchinfo = 0; /* Number of u32 elements in match-info */
194189 int i; /* Used to iterate through zArg */
194190
194191 /* Determine the number of phrases in the query */
194192 pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr);
194193 sInfo.nPhrase = pCsr->nPhrase;
194194
194195 /* Determine the number of integers in the buffer returned by this call. */
194196 for(i=0; zArg[i]; i++){
194197 char *zErr = 0;
194198 if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
194199 sqlite3_result_error(pCtx, zErr, -1);
194200 sqlite3_free(zErr);
194201 return;
194202 }
194203 nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]);
194204 }
194205
194206 /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */
194207 pCsr->pMIBuffer = fts3MIBufferNew(nMatchinfo, zArg);
194208 if( !pCsr->pMIBuffer ) rc = SQLITE_NOMEM7;
194209
194210 pCsr->isMatchinfoNeeded = 1;
194211 bGlobal = 1;
194212 }
194213
194214 if( rc==SQLITE_OK0 ){
194215 xDestroyOut = fts3MIBufferAlloc(pCsr->pMIBuffer, &aOut);
194216 if( xDestroyOut==0 ){
194217 rc = SQLITE_NOMEM7;
194218 }
194219 }
194220
194221 if( rc==SQLITE_OK0 ){
194222 sInfo.aMatchinfo = aOut;
194223 sInfo.nPhrase = pCsr->nPhrase;
194224 rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg);
194225 if( bGlobal ){
194226 fts3MIBufferSetGlobal(pCsr->pMIBuffer);
194227 }
194228 }
194229
194230 if( rc!=SQLITE_OK0 ){
194231 sqlite3_result_error_code(pCtx, rc);
194232 if( xDestroyOut ) xDestroyOut(aOut);
194233 }else{
194234 int n = pCsr->pMIBuffer->nElem * sizeof(u32);
194235 sqlite3_result_blob(pCtx, aOut, n, xDestroyOut);
194236 }
194237}
194238
194239/*
194240** Implementation of snippet() function.
194241*/
194242SQLITE_PRIVATEstatic void sqlite3Fts3Snippet(
194243 sqlite3_context *pCtx, /* SQLite function call context */
194244 Fts3Cursor *pCsr, /* Cursor object */
194245 const char *zStart, /* Snippet start text - "<b>" */
194246 const char *zEnd, /* Snippet end text - "</b>" */
194247 const char *zEllipsis, /* Snippet ellipsis text - "<b>...</b>" */
194248 int iCol, /* Extract snippet from this column */
194249 int nToken /* Approximate number of tokens in snippet */
194250){
194251 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
194252 int rc = SQLITE_OK0;
194253 int i;
194254 StrBuffer res = {0, 0, 0};
194255
194256 /* The returned text includes up to four fragments of text extracted from
194257 ** the data in the current row. The first iteration of the for(...) loop
194258 ** below attempts to locate a single fragment of text nToken tokens in
194259 ** size that contains at least one instance of all phrases in the query
194260 ** expression that appear in the current row. If such a fragment of text
194261 ** cannot be found, the second iteration of the loop attempts to locate
194262 ** a pair of fragments, and so on.
194263 */
194264 int nSnippet = 0; /* Number of fragments in this snippet */
194265 SnippetFragment aSnippet[4]; /* Maximum of 4 fragments per snippet */
194266 int nFToken = -1; /* Number of tokens in each fragment */
194267
194268 if( !pCsr->pExpr ){
194269 sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC((sqlite3_destructor_type)0));
194270 return;
194271 }
194272
194273 /* Limit the snippet length to 64 tokens. */
194274 if( nToken<-64 ) nToken = -64;
194275 if( nToken>+64 ) nToken = +64;
194276
194277 for(nSnippet=1; 1; nSnippet++){
194278
194279 int iSnip; /* Loop counter 0..nSnippet-1 */
194280 u64 mCovered = 0; /* Bitmask of phrases covered by snippet */
194281 u64 mSeen = 0; /* Bitmask of phrases seen by BestSnippet() */
194282
194283 if( nToken>=0 ){
194284 nFToken = (nToken+nSnippet-1) / nSnippet;
194285 }else{
194286 nFToken = -1 * nToken;
194287 }
194288
194289 for(iSnip=0; iSnip<nSnippet; iSnip++){
194290 int iBestScore = -1; /* Best score of columns checked so far */
194291 int iRead; /* Used to iterate through columns */
194292 SnippetFragment *pFragment = &aSnippet[iSnip];
194293
194294 memset(pFragment, 0, sizeof(*pFragment));
194295
194296 /* Loop through all columns of the table being considered for snippets.
194297 ** If the iCol argument to this function was negative, this means all
194298 ** columns of the FTS3 table. Otherwise, only column iCol is considered.
194299 */
194300 for(iRead=0; iRead<pTab->nColumn; iRead++){
194301 SnippetFragment sF = {0, 0, 0, 0};
194302 int iS = 0;
194303 if( iCol>=0 && iRead!=iCol ) continue;
194304
194305 /* Find the best snippet of nFToken tokens in column iRead. */
194306 rc = fts3BestSnippet(nFToken, pCsr, iRead, mCovered, &mSeen, &sF, &iS);
194307 if( rc!=SQLITE_OK0 ){
194308 goto snippet_out;
194309 }
194310 if( iS>iBestScore ){
194311 *pFragment = sF;
194312 iBestScore = iS;
194313 }
194314 }
194315
194316 mCovered |= pFragment->covered;
194317 }
194318
194319 /* If all query phrases seen by fts3BestSnippet() are present in at least
194320 ** one of the nSnippet snippet fragments, break out of the loop.
194321 */
194322 assert( (mCovered&mSeen)==mCovered )((void) (0));
194323 if( mSeen==mCovered || nSnippet==SizeofArray(aSnippet)((int)(sizeof(aSnippet)/sizeof(aSnippet[0]))) ) break;
194324 }
194325
194326 assert( nFToken>0 )((void) (0));
194327
194328 for(i=0; i<nSnippet && rc==SQLITE_OK0; i++){
194329 rc = fts3SnippetText(pCsr, &aSnippet[i],
194330 i, (i==nSnippet-1), nFToken, zStart, zEnd, zEllipsis, &res
194331 );
194332 }
194333
194334 snippet_out:
194335 sqlite3Fts3SegmentsClose(pTab);
194336 if( rc!=SQLITE_OK0 ){
194337 sqlite3_result_error_code(pCtx, rc);
194338 sqlite3_free(res.z);
194339 }else{
194340 sqlite3_result_text(pCtx, res.z, -1, sqlite3_free);
194341 }
194342}
194343
194344
194345typedef struct TermOffset TermOffset;
194346typedef struct TermOffsetCtx TermOffsetCtx;
194347
194348struct TermOffset {
194349 char *pList; /* Position-list */
194350 i64 iPos; /* Position just read from pList */
194351 i64 iOff; /* Offset of this term from read positions */
194352};
194353
194354struct TermOffsetCtx {
194355 Fts3Cursor *pCsr;
194356 int iCol; /* Column of table to populate aTerm for */
194357 int iTerm;
194358 sqlite3_int64 iDocid;
194359 TermOffset *aTerm;
194360};
194361
194362/*
194363** This function is an fts3ExprIterate() callback used by sqlite3Fts3Offsets().
194364*/
194365static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){
194366 TermOffsetCtx *p = (TermOffsetCtx *)ctx;
194367 int nTerm; /* Number of tokens in phrase */
194368 int iTerm; /* For looping through nTerm phrase terms */
194369 char *pList; /* Pointer to position list for phrase */
194370 i64 iPos = 0; /* First position in position-list */
194371 int rc;
194372
194373 UNUSED_PARAMETER(iPhrase)(void)(iPhrase);
194374 rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pList);
194375 nTerm = pExpr->pPhrase->nToken;
194376 if( pList ){
194377 fts3GetDeltaPosition(&pList, &iPos);
194378 assert_fts3_nc( iPos>=0 )((void) (0));
194379 }
194380
194381 for(iTerm=0; iTerm<nTerm; iTerm++){
194382 TermOffset *pT = &p->aTerm[p->iTerm++];
194383 pT->iOff = nTerm-iTerm-1;
194384 pT->pList = pList;
194385 pT->iPos = iPos;
194386 }
194387
194388 return rc;
194389}
194390
194391/*
194392** Implementation of offsets() function.
194393*/
194394SQLITE_PRIVATEstatic void sqlite3Fts3Offsets(
194395 sqlite3_context *pCtx, /* SQLite function call context */
194396 Fts3Cursor *pCsr /* Cursor object */
194397){
194398 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
194399 sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule;
194400 int rc; /* Return Code */
194401 int nToken; /* Number of tokens in query */
194402 int iCol; /* Column currently being processed */
194403 StrBuffer res = {0, 0, 0}; /* Result string */
194404 TermOffsetCtx sCtx; /* Context for fts3ExprTermOffsetInit() */
194405
194406 if( !pCsr->pExpr ){
194407 sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC((sqlite3_destructor_type)0));
194408 return;
194409 }
194410
194411 memset(&sCtx, 0, sizeof(sCtx));
194412 assert( pCsr->isRequireSeek==0 )((void) (0));
194413
194414 /* Count the number of terms in the query */
194415 rc = fts3ExprLoadDoclists(pCsr, 0, &nToken);
194416 if( rc!=SQLITE_OK0 ) goto offsets_out;
194417
194418 /* Allocate the array of TermOffset iterators. */
194419 sCtx.aTerm = (TermOffset *)sqlite3Fts3MallocZero(sizeof(TermOffset)*nToken);
194420 if( 0==sCtx.aTerm ){
194421 rc = SQLITE_NOMEM7;
194422 goto offsets_out;
194423 }
194424 sCtx.iDocid = pCsr->iPrevId;
194425 sCtx.pCsr = pCsr;
194426
194427 /* Loop through the table columns, appending offset information to
194428 ** string-buffer res for each column.
194429 */
194430 for(iCol=0; iCol<pTab->nColumn; iCol++){
194431 sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor */
194432 const char *ZDUMMY; /* Dummy argument used with xNext() */
194433 int NDUMMY = 0; /* Dummy argument used with xNext() */
194434 int iStart = 0;
194435 int iEnd = 0;
194436 int iCurrent = 0;
194437 const char *zDoc;
194438 int nDoc;
194439
194440 /* Initialize the contents of sCtx.aTerm[] for column iCol. This
194441 ** operation may fail if the database contains corrupt records.
194442 */
194443 sCtx.iCol = iCol;
194444 sCtx.iTerm = 0;
194445 rc = fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void*)&sCtx);
194446 if( rc!=SQLITE_OK0 ) goto offsets_out;
194447
194448 /* Retreive the text stored in column iCol. If an SQL NULL is stored
194449 ** in column iCol, jump immediately to the next iteration of the loop.
194450 ** If an OOM occurs while retrieving the data (this can happen if SQLite
194451 ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM
194452 ** to the caller.
194453 */
194454 zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol+1);
194455 nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
194456 if( zDoc==0 ){
194457 if( sqlite3_column_type(pCsr->pStmt, iCol+1)==SQLITE_NULL5 ){
194458 continue;
194459 }
194460 rc = SQLITE_NOMEM7;
194461 goto offsets_out;
194462 }
194463
194464 /* Initialize a tokenizer iterator to iterate through column iCol. */
194465 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid,
194466 zDoc, nDoc, &pC
194467 );
194468 if( rc!=SQLITE_OK0 ) goto offsets_out;
194469
194470 rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
194471 while( rc==SQLITE_OK0 ){
194472 int i; /* Used to loop through terms */
194473 int iMinPos = 0x7FFFFFFF; /* Position of next token */
194474 TermOffset *pTerm = 0; /* TermOffset associated with next token */
194475
194476 for(i=0; i<nToken; i++){
194477 TermOffset *pT = &sCtx.aTerm[i];
194478 if( pT->pList && (pT->iPos-pT->iOff)<iMinPos ){
194479 iMinPos = pT->iPos-pT->iOff;
194480 pTerm = pT;
194481 }
194482 }
194483
194484 if( !pTerm ){
194485 /* All offsets for this column have been gathered. */
194486 rc = SQLITE_DONE101;
194487 }else{
194488 assert_fts3_nc( iCurrent<=iMinPos )((void) (0));
194489 if( 0==(0xFE&*pTerm->pList) ){
194490 pTerm->pList = 0;
194491 }else{
194492 fts3GetDeltaPosition(&pTerm->pList, &pTerm->iPos);
194493 }
194494 while( rc==SQLITE_OK0 && iCurrent<iMinPos ){
194495 rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
194496 }
194497 if( rc==SQLITE_OK0 ){
194498 char aBuffer[64];
194499 sqlite3_snprintf(sizeof(aBuffer), aBuffer,
194500 "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
194501 );
194502 rc = fts3StringAppend(&res, aBuffer, -1);
194503 }else if( rc==SQLITE_DONE101 && pTab->zContentTbl==0 ){
194504 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
194505 }
194506 }
194507 }
194508 if( rc==SQLITE_DONE101 ){
194509 rc = SQLITE_OK0;
194510 }
194511
194512 pMod->xClose(pC);
194513 if( rc!=SQLITE_OK0 ) goto offsets_out;
194514 }
194515
194516 offsets_out:
194517 sqlite3_free(sCtx.aTerm);
194518 assert( rc!=SQLITE_DONE )((void) (0));
194519 sqlite3Fts3SegmentsClose(pTab);
194520 if( rc!=SQLITE_OK0 ){
194521 sqlite3_result_error_code(pCtx, rc);
194522 sqlite3_free(res.z);
194523 }else{
194524 sqlite3_result_text(pCtx, res.z, res.n-1, sqlite3_free);
194525 }
194526 return;
194527}
194528
194529/*
194530** Implementation of matchinfo() function.
194531*/
194532SQLITE_PRIVATEstatic void sqlite3Fts3Matchinfo(
194533 sqlite3_context *pContext, /* Function call context */
194534 Fts3Cursor *pCsr, /* FTS3 table cursor */
194535 const char *zArg /* Second arg to matchinfo() function */
194536){
194537 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
194538 const char *zFormat;
194539
194540 if( zArg ){
194541 zFormat = zArg;
194542 }else{
194543 zFormat = FTS3_MATCHINFO_DEFAULT"pcx";
194544 }
194545
194546 if( !pCsr->pExpr ){
194547 sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC((sqlite3_destructor_type)0));
194548 return;
194549 }else{
194550 /* Retrieve matchinfo() data. */
194551 fts3GetMatchinfo(pContext, pCsr, zFormat);
194552 sqlite3Fts3SegmentsClose(pTab);
194553 }
194554}
194555
194556#endif
194557
194558/************** End of fts3_snippet.c ****************************************/
194559/************** Begin file fts3_unicode.c ************************************/
194560/*
194561** 2012 May 24
194562**
194563** The author disclaims copyright to this source code. In place of
194564** a legal notice, here is a blessing:
194565**
194566** May you do good and not evil.
194567** May you find forgiveness for yourself and forgive others.
194568** May you share freely, never taking more than you give.
194569**
194570******************************************************************************
194571**
194572** Implementation of the "unicode" full-text-search tokenizer.
194573*/
194574
194575#ifndef SQLITE_DISABLE_FTS3_UNICODE
194576
194577/* #include "fts3Int.h" */
194578#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
194579
194580/* #include <assert.h> */
194581/* #include <stdlib.h> */
194582/* #include <stdio.h> */
194583/* #include <string.h> */
194584
194585/* #include "fts3_tokenizer.h" */
194586
194587/*
194588** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
194589** from the sqlite3 source file utf.c. If this file is compiled as part
194590** of the amalgamation, they are not required.
194591*/
194592#ifndef SQLITE_AMALGAMATION1
194593
194594static const unsigned char sqlite3Utf8Trans1[] = {
194595 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
194596 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
194597 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
194598 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
194599 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
194600 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
194601 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
194602 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
194603};
194604
194605#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
; } }
\
194606 c = *(zIn++); \
194607 if( c>=0xc0 ){ \
194608 c = sqlite3Utf8Trans1[c-0xc0]; \
194609 while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \
194610 c = (c<<6) + (0x3f & *(zIn++)); \
194611 } \
194612 if( c<0x80 \
194613 || (c&0xFFFFF800)==0xD800 \
194614 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
194615 }
194616
194617#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); } }
{ \
194618 if( c<0x00080 ){ \
194619 *zOut++ = (u8)(c&0xFF); \
194620 } \
194621 else if( c<0x00800 ){ \
194622 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \
194623 *zOut++ = 0x80 + (u8)(c & 0x3F); \
194624 } \
194625 else if( c<0x10000 ){ \
194626 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \
194627 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
194628 *zOut++ = 0x80 + (u8)(c & 0x3F); \
194629 }else{ \
194630 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); \
194631 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); \
194632 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
194633 *zOut++ = 0x80 + (u8)(c & 0x3F); \
194634 } \
194635}
194636
194637#endif /* ifndef SQLITE_AMALGAMATION */
194638
194639typedef struct unicode_tokenizer unicode_tokenizer;
194640typedef struct unicode_cursor unicode_cursor;
194641
194642struct unicode_tokenizer {
194643 sqlite3_tokenizer base;
194644 int eRemoveDiacritic;
194645 int nException;
194646 int *aiException;
194647};
194648
194649struct unicode_cursor {
194650 sqlite3_tokenizer_cursor base;
194651 const unsigned char *aInput; /* Input text being tokenized */
194652 int nInput; /* Size of aInput[] in bytes */
194653 int iOff; /* Current offset within aInput[] */
194654 int iToken; /* Index of next token to be returned */
194655 char *zToken; /* storage for current token */
194656 int nAlloc; /* space allocated at zToken */
194657};
194658
194659
194660/*
194661** Destroy a tokenizer allocated by unicodeCreate().
194662*/
194663static int unicodeDestroy(sqlite3_tokenizer *pTokenizer){
194664 if( pTokenizer ){
194665 unicode_tokenizer *p = (unicode_tokenizer *)pTokenizer;
194666 sqlite3_free(p->aiException);
194667 sqlite3_free(p);
194668 }
194669 return SQLITE_OK0;
194670}
194671
194672/*
194673** As part of a tokenchars= or separators= option, the CREATE VIRTUAL TABLE
194674** statement has specified that the tokenizer for this table shall consider
194675** all characters in string zIn/nIn to be separators (if bAlnum==0) or
194676** token characters (if bAlnum==1).
194677**
194678** For each codepoint in the zIn/nIn string, this function checks if the
194679** sqlite3FtsUnicodeIsalnum() function already returns the desired result.
194680** If so, no action is taken. Otherwise, the codepoint is added to the
194681** unicode_tokenizer.aiException[] array. For the purposes of tokenization,
194682** the return value of sqlite3FtsUnicodeIsalnum() is inverted for all
194683** codepoints in the aiException[] array.
194684**
194685** If a standalone diacritic mark (one that sqlite3FtsUnicodeIsdiacritic()
194686** identifies as a diacritic) occurs in the zIn/nIn string it is ignored.
194687** It is not possible to change the behavior of the tokenizer with respect
194688** to these codepoints.
194689*/
194690static int unicodeAddExceptions(
194691 unicode_tokenizer *p, /* Tokenizer to add exceptions to */
194692 int bAlnum, /* Replace Isalnum() return value with this */
194693 const char *zIn, /* Array of characters to make exceptions */
194694 int nIn /* Length of z in bytes */
194695){
194696 const unsigned char *z = (const unsigned char *)zIn;
194697 const unsigned char *zTerm = &z[nIn];
194698 unsigned int iCode;
194699 int nEntry = 0;
194700
194701 assert( bAlnum==0 || bAlnum==1 )((void) (0));
194702
194703 while( z<zTerm ){
194704 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; } }
;
194705 assert( (sqlite3FtsUnicodeIsalnum((int)iCode) & 0xFFFFFFFE)==0 )((void) (0));
194706 if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum
194707 && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0
194708 ){
194709 nEntry++;
194710 }
194711 }
194712
194713 if( nEntry ){
194714 int *aNew; /* New aiException[] array */
194715 int nNew; /* Number of valid entries in array aNew[] */
194716
194717 aNew = sqlite3_realloc64(p->aiException,(p->nException+nEntry)*sizeof(int));
194718 if( aNew==0 ) return SQLITE_NOMEM7;
194719 nNew = p->nException;
194720
194721 z = (const unsigned char *)zIn;
194722 while( z<zTerm ){
194723 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; } }
;
194724 if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum
194725 && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0
194726 ){
194727 int i, j;
194728 for(i=0; i<nNew && aNew[i]<(int)iCode; i++);
194729 for(j=nNew; j>i; j--) aNew[j] = aNew[j-1];
194730 aNew[i] = (int)iCode;
194731 nNew++;
194732 }
194733 }
194734 p->aiException = aNew;
194735 p->nException = nNew;
194736 }
194737
194738 return SQLITE_OK0;
194739}
194740
194741/*
194742** Return true if the p->aiException[] array contains the value iCode.
194743*/
194744static int unicodeIsException(unicode_tokenizer *p, int iCode){
194745 if( p->nException>0 ){
194746 int *a = p->aiException;
194747 int iLo = 0;
194748 int iHi = p->nException-1;
194749
194750 while( iHi>=iLo ){
194751 int iTest = (iHi + iLo) / 2;
194752 if( iCode==a[iTest] ){
194753 return 1;
194754 }else if( iCode>a[iTest] ){
194755 iLo = iTest+1;
194756 }else{
194757 iHi = iTest-1;
194758 }
194759 }
194760 }
194761
194762 return 0;
194763}
194764
194765/*
194766** Return true if, for the purposes of tokenization, codepoint iCode is
194767** considered a token character (not a separator).
194768*/
194769static int unicodeIsAlnum(unicode_tokenizer *p, int iCode){
194770 assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 )((void) (0));
194771 return sqlite3FtsUnicodeIsalnum(iCode) ^ unicodeIsException(p, iCode);
194772}
194773
194774/*
194775** Create a new tokenizer instance.
194776*/
194777static int unicodeCreate(
194778 int nArg, /* Size of array argv[] */
194779 const char * const *azArg, /* Tokenizer creation arguments */
194780 sqlite3_tokenizer **pp /* OUT: New tokenizer handle */
194781){
194782 unicode_tokenizer *pNew; /* New tokenizer object */
194783 int i;
194784 int rc = SQLITE_OK0;
194785
194786 pNew = (unicode_tokenizer *) sqlite3_malloc(sizeof(unicode_tokenizer));
194787 if( pNew==NULL((void*)0) ) return SQLITE_NOMEM7;
194788 memset(pNew, 0, sizeof(unicode_tokenizer));
194789 pNew->eRemoveDiacritic = 1;
194790
194791 for(i=0; rc==SQLITE_OK0 && i<nArg; i++){
194792 const char *z = azArg[i];
194793 int n = (int)strlen(z);
194794
194795 if( n==19 && memcmp("remove_diacritics=1", z, 19)==0 ){
194796 pNew->eRemoveDiacritic = 1;
194797 }
194798 else if( n==19 && memcmp("remove_diacritics=0", z, 19)==0 ){
194799 pNew->eRemoveDiacritic = 0;
194800 }
194801 else if( n==19 && memcmp("remove_diacritics=2", z, 19)==0 ){
194802 pNew->eRemoveDiacritic = 2;
194803 }
194804 else if( n>=11 && memcmp("tokenchars=", z, 11)==0 ){
194805 rc = unicodeAddExceptions(pNew, 1, &z[11], n-11);
194806 }
194807 else if( n>=11 && memcmp("separators=", z, 11)==0 ){
194808 rc = unicodeAddExceptions(pNew, 0, &z[11], n-11);
194809 }
194810 else{
194811 /* Unrecognized argument */
194812 rc = SQLITE_ERROR1;
194813 }
194814 }
194815
194816 if( rc!=SQLITE_OK0 ){
194817 unicodeDestroy((sqlite3_tokenizer *)pNew);
194818 pNew = 0;
194819 }
194820 *pp = (sqlite3_tokenizer *)pNew;
194821 return rc;
194822}
194823
194824/*
194825** Prepare to begin tokenizing a particular string. The input
194826** string to be tokenized is pInput[0..nBytes-1]. A cursor
194827** used to incrementally tokenize this string is returned in
194828** *ppCursor.
194829*/
194830static int unicodeOpen(
194831 sqlite3_tokenizer *p, /* The tokenizer */
194832 const char *aInput, /* Input string */
194833 int nInput, /* Size of string aInput in bytes */
194834 sqlite3_tokenizer_cursor **pp /* OUT: New cursor object */
194835){
194836 unicode_cursor *pCsr;
194837
194838 pCsr = (unicode_cursor *)sqlite3_malloc(sizeof(unicode_cursor));
194839 if( pCsr==0 ){
194840 return SQLITE_NOMEM7;
194841 }
194842 memset(pCsr, 0, sizeof(unicode_cursor));
194843
194844 pCsr->aInput = (const unsigned char *)aInput;
194845 if( aInput==0 ){
194846 pCsr->nInput = 0;
194847 pCsr->aInput = (const unsigned char*)"";
194848 }else if( nInput<0 ){
194849 pCsr->nInput = (int)strlen(aInput);
194850 }else{
194851 pCsr->nInput = nInput;
194852 }
194853
194854 *pp = &pCsr->base;
194855 UNUSED_PARAMETER(p)(void)(p);
194856 return SQLITE_OK0;
194857}
194858
194859/*
194860** Close a tokenization cursor previously opened by a call to
194861** simpleOpen() above.
194862*/
194863static int unicodeClose(sqlite3_tokenizer_cursor *pCursor){
194864 unicode_cursor *pCsr = (unicode_cursor *) pCursor;
194865 sqlite3_free(pCsr->zToken);
194866 sqlite3_free(pCsr);
194867 return SQLITE_OK0;
194868}
194869
194870/*
194871** Extract the next token from a tokenization cursor. The cursor must
194872** have been opened by a prior call to simpleOpen().
194873*/
194874static int unicodeNext(
194875 sqlite3_tokenizer_cursor *pC, /* Cursor returned by simpleOpen */
194876 const char **paToken, /* OUT: Token text */
194877 int *pnToken, /* OUT: Number of bytes at *paToken */
194878 int *piStart, /* OUT: Starting offset of token */
194879 int *piEnd, /* OUT: Ending offset of token */
194880 int *piPos /* OUT: Position integer of token */
194881){
194882 unicode_cursor *pCsr = (unicode_cursor *)pC;
194883 unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
194884 unsigned int iCode = 0;
194885 char *zOut;
194886 const unsigned char *z = &pCsr->aInput[pCsr->iOff];
194887 const unsigned char *zStart = z;
194888 const unsigned char *zEnd;
194889 const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
194890
194891 /* Scan past any delimiter characters before the start of the next token.
194892 ** Return SQLITE_DONE early if this takes us all the way to the end of
194893 ** the input. */
194894 while( z<zTerm ){
194895 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; } }
;
194896 if( unicodeIsAlnum(p, (int)iCode) ) break;
194897 zStart = z;
194898 }
194899 if( zStart>=zTerm ) return SQLITE_DONE101;
194900
194901 zOut = pCsr->zToken;
194902 do {
194903 int iOut;
194904
194905 /* Grow the output buffer if required. */
194906 if( (zOut-pCsr->zToken)>=(pCsr->nAlloc-4) ){
194907 char *zNew = sqlite3_realloc64(pCsr->zToken, pCsr->nAlloc+64);
194908 if( !zNew ) return SQLITE_NOMEM7;
194909 zOut = &zNew[zOut - pCsr->zToken];
194910 pCsr->zToken = zNew;
194911 pCsr->nAlloc += 64;
194912 }
194913
194914 /* Write the folded case of the last character read to the output */
194915 zEnd = z;
194916 iOut = sqlite3FtsUnicodeFold((int)iCode, p->eRemoveDiacritic);
194917 if( iOut ){
194918 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); } }
;
194919 }
194920
194921 /* If the cursor is not at EOF, read the next character */
194922 if( z>=zTerm ) break;
194923 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; } }
;
194924 }while( unicodeIsAlnum(p, (int)iCode)
194925 || sqlite3FtsUnicodeIsdiacritic((int)iCode)
194926 );
194927
194928 /* Set the output variables and return. */
194929 pCsr->iOff = (int)(z - pCsr->aInput);
194930 *paToken = pCsr->zToken;
194931 *pnToken = (int)(zOut - pCsr->zToken);
194932 *piStart = (int)(zStart - pCsr->aInput);
194933 *piEnd = (int)(zEnd - pCsr->aInput);
194934 *piPos = pCsr->iToken++;
194935 return SQLITE_OK0;
194936}
194937
194938/*
194939** Set *ppModule to a pointer to the sqlite3_tokenizer_module
194940** structure for the unicode tokenizer.
194941*/
194942SQLITE_PRIVATEstatic void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const **ppModule){
194943 static const sqlite3_tokenizer_module module = {
194944 0,
194945 unicodeCreate,
194946 unicodeDestroy,
194947 unicodeOpen,
194948 unicodeClose,
194949 unicodeNext,
194950 0,
194951 };
194952 *ppModule = &module;
194953}
194954
194955#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
194956#endif /* ifndef SQLITE_DISABLE_FTS3_UNICODE */
194957
194958/************** End of fts3_unicode.c ****************************************/
194959/************** Begin file fts3_unicode2.c ***********************************/
194960/*
194961** 2012-05-25
194962**
194963** The author disclaims copyright to this source code. In place of
194964** a legal notice, here is a blessing:
194965**
194966** May you do good and not evil.
194967** May you find forgiveness for yourself and forgive others.
194968** May you share freely, never taking more than you give.
194969**
194970******************************************************************************
194971*/
194972
194973/*
194974** DO NOT EDIT THIS MACHINE GENERATED FILE.
194975*/
194976
194977#ifndef SQLITE_DISABLE_FTS3_UNICODE
194978#if defined(SQLITE_ENABLE_FTS31) || defined(SQLITE_ENABLE_FTS41)
194979
194980/* #include <assert.h> */
194981
194982/*
194983** Return true if the argument corresponds to a unicode codepoint
194984** classified as either a letter or a number. Otherwise false.
194985**
194986** The results are undefined if the value passed to this function
194987** is less than zero.
194988*/
194989SQLITE_PRIVATEstatic int sqlite3FtsUnicodeIsalnum(int c){
194990 /* Each unsigned integer in the following array corresponds to a contiguous
194991 ** range of unicode codepoints that are not either letters or numbers (i.e.
194992 ** codepoints for which this function should return 0).
194993 **
194994 ** The most significant 22 bits in each 32-bit value contain the first
194995 ** codepoint in the range. The least significant 10 bits are used to store
194996 ** the size of the range (always at least 1). In other words, the value
194997 ** ((C<<22) + N) represents a range of N codepoints starting with codepoint
194998 ** C. It is not possible to represent a range larger than 1023 codepoints
194999 ** using this format.
195000 */
195001 static const unsigned int aEntry[] = {
195002 0x00000030, 0x0000E807, 0x00016C06, 0x0001EC2F, 0x0002AC07,
195003 0x0002D001, 0x0002D803, 0x0002EC01, 0x0002FC01, 0x00035C01,
195004 0x0003DC01, 0x000B0804, 0x000B480E, 0x000B9407, 0x000BB401,
195005 0x000BBC81, 0x000DD401, 0x000DF801, 0x000E1002, 0x000E1C01,
195006 0x000FD801, 0x00120808, 0x00156806, 0x00162402, 0x00163C01,
195007 0x00164437, 0x0017CC02, 0x00180005, 0x00181816, 0x00187802,
195008 0x00192C15, 0x0019A804, 0x0019C001, 0x001B5001, 0x001B580F,
195009 0x001B9C07, 0x001BF402, 0x001C000E, 0x001C3C01, 0x001C4401,
195010 0x001CC01B, 0x001E980B, 0x001FAC09, 0x001FD804, 0x00205804,
195011 0x00206C09, 0x00209403, 0x0020A405, 0x0020C00F, 0x00216403,
195012 0x00217801, 0x0023901B, 0x00240004, 0x0024E803, 0x0024F812,
195013 0x00254407, 0x00258804, 0x0025C001, 0x00260403, 0x0026F001,
195014 0x0026F807, 0x00271C02, 0x00272C03, 0x00275C01, 0x00278802,
195015 0x0027C802, 0x0027E802, 0x00280403, 0x0028F001, 0x0028F805,
195016 0x00291C02, 0x00292C03, 0x00294401, 0x0029C002, 0x0029D401,
195017 0x002A0403, 0x002AF001, 0x002AF808, 0x002B1C03, 0x002B2C03,
195018 0x002B8802, 0x002BC002, 0x002C0403, 0x002CF001, 0x002CF807,
195019 0x002D1C02, 0x002D2C03, 0x002D5802, 0x002D8802, 0x002DC001,
195020 0x002E0801, 0x002EF805, 0x002F1803, 0x002F2804, 0x002F5C01,
195021 0x002FCC08, 0x00300403, 0x0030F807, 0x00311803, 0x00312804,
195022 0x00315402, 0x00318802, 0x0031FC01, 0x00320802, 0x0032F001,
195023 0x0032F807, 0x00331803, 0x00332804, 0x00335402, 0x00338802,
195024 0x00340802, 0x0034F807, 0x00351803, 0x00352804, 0x00355C01,
195025 0x00358802, 0x0035E401, 0x00360802, 0x00372801, 0x00373C06,
195026 0x00375801, 0x00376008, 0x0037C803, 0x0038C401, 0x0038D007,
195027 0x0038FC01, 0x00391C09, 0x00396802, 0x003AC401, 0x003AD006,
195028 0x003AEC02, 0x003B2006, 0x003C041F, 0x003CD00C, 0x003DC417,
195029 0x003E340B, 0x003E6424, 0x003EF80F, 0x003F380D, 0x0040AC14,
195030 0x00412806, 0x00415804, 0x00417803, 0x00418803, 0x00419C07,
195031 0x0041C404, 0x0042080C, 0x00423C01, 0x00426806, 0x0043EC01,
195032 0x004D740C, 0x004E400A, 0x00500001, 0x0059B402, 0x005A0001,
195033 0x005A6C02, 0x005BAC03, 0x005C4803, 0x005CC805, 0x005D4802,
195034 0x005DC802, 0x005ED023, 0x005F6004, 0x005F7401, 0x0060000F,
195035 0x0062A401, 0x0064800C, 0x0064C00C, 0x00650001, 0x00651002,
195036 0x0066C011, 0x00672002, 0x00677822, 0x00685C05, 0x00687802,
195037 0x0069540A, 0x0069801D, 0x0069FC01, 0x006A8007, 0x006AA006,
195038 0x006C0005, 0x006CD011, 0x006D6823, 0x006E0003, 0x006E840D,
195039 0x006F980E, 0x006FF004, 0x00709014, 0x0070EC05, 0x0071F802,
195040 0x00730008, 0x00734019, 0x0073B401, 0x0073C803, 0x00770027,
195041 0x0077F004, 0x007EF401, 0x007EFC03, 0x007F3403, 0x007F7403,
195042 0x007FB403, 0x007FF402, 0x00800065, 0x0081A806, 0x0081E805,
195043 0x00822805, 0x0082801A, 0x00834021, 0x00840002, 0x00840C04,
195044 0x00842002, 0x00845001, 0x00845803, 0x00847806, 0x00849401,
195045 0x00849C01, 0x0084A401, 0x0084B801, 0x0084E802, 0x00850005,
195046 0x00852804, 0x00853C01, 0x00864264, 0x00900027, 0x0091000B,
195047 0x0092704E, 0x00940200, 0x009C0475, 0x009E53B9, 0x00AD400A,
195048 0x00B39406, 0x00B3BC03, 0x00B3E404, 0x00B3F802, 0x00B5C001,
195049 0x00B5FC01, 0x00B7804F, 0x00B8C00C, 0x00BA001A, 0x00BA6C59,
195050 0x00BC00D6, 0x00BFC00C, 0x00C00005, 0x00C02019, 0x00C0A807,
195051 0x00C0D802, 0x00C0F403, 0x00C26404, 0x00C28001, 0x00C3EC01,
195052 0x00C64002, 0x00C6580A, 0x00C70024, 0x00C8001F, 0x00C8A81E,
195053 0x00C94001, 0x00C98020, 0x00CA2827, 0x00CB003F, 0x00CC0100,
195054 0x01370040, 0x02924037, 0x0293F802, 0x02983403, 0x0299BC10,
195055 0x029A7C01, 0x029BC008, 0x029C0017, 0x029C8002, 0x029E2402,
195056 0x02A00801, 0x02A01801, 0x02A02C01, 0x02A08C09, 0x02A0D804,
195057 0x02A1D004, 0x02A20002, 0x02A2D011, 0x02A33802, 0x02A38012,
195058 0x02A3E003, 0x02A4980A, 0x02A51C0D, 0x02A57C01, 0x02A60004,
195059 0x02A6CC1B, 0x02A77802, 0x02A8A40E, 0x02A90C01, 0x02A93002,
195060 0x02A97004, 0x02A9DC03, 0x02A9EC01, 0x02AAC001, 0x02AAC803,
195061 0x02AADC02, 0x02AAF802, 0x02AB0401, 0x02AB7802, 0x02ABAC07,
195062 0x02ABD402, 0x02AF8C0B, 0x03600001, 0x036DFC02, 0x036FFC02,
195063 0x037FFC01, 0x03EC7801, 0x03ECA401, 0x03EEC810, 0x03F4F802,
195064 0x03F7F002, 0x03F8001A, 0x03F88007, 0x03F8C023, 0x03F95013,
195065 0x03F9A004, 0x03FBFC01, 0x03FC040F, 0x03FC6807, 0x03FCEC06,
195066 0x03FD6C0B, 0x03FF8007, 0x03FFA007, 0x03FFE405, 0x04040003,
195067 0x0404DC09, 0x0405E411, 0x0406400C, 0x0407402E, 0x040E7C01,
195068 0x040F4001, 0x04215C01, 0x04247C01, 0x0424FC01, 0x04280403,
195069 0x04281402, 0x04283004, 0x0428E003, 0x0428FC01, 0x04294009,
195070 0x0429FC01, 0x042CE407, 0x04400003, 0x0440E016, 0x04420003,
195071 0x0442C012, 0x04440003, 0x04449C0E, 0x04450004, 0x04460003,
195072 0x0446CC0E, 0x04471404, 0x045AAC0D, 0x0491C004, 0x05BD442E,
195073 0x05BE3C04, 0x074000F6, 0x07440027, 0x0744A4B5, 0x07480046,
195074 0x074C0057, 0x075B0401, 0x075B6C01, 0x075BEC01, 0x075C5401,
195075 0x075CD401, 0x075D3C01, 0x075DBC01, 0x075E2401, 0x075EA401,
195076 0x075F0C01, 0x07BBC002, 0x07C0002C, 0x07C0C064, 0x07C2800F,
195077 0x07C2C40E, 0x07C3040F, 0x07C3440F, 0x07C4401F, 0x07C4C03C,
195078 0x07C5C02B, 0x07C7981D, 0x07C8402B, 0x07C90009, 0x07C94002,
195079 0x07CC0021, 0x07CCC006, 0x07CCDC46, 0x07CE0014, 0x07CE8025,
195080 0x07CF1805, 0x07CF8011, 0x07D0003F, 0x07D10001, 0x07D108B6,
195081 0x07D3E404, 0x07D4003E, 0x07D50004, 0x07D54018, 0x07D7EC46,
195082 0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401, 0x38008060,
195083 0x380400F0,
195084 };
195085 static const unsigned int aAscii[4] = {
195086 0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001,
195087 };
195088
195089 if( (unsigned int)c<128 ){
195090 return ( (aAscii[c >> 5] & ((unsigned int)1 << (c & 0x001F)))==0 );
195091 }else if( (unsigned int)c<(1<<22) ){
195092 unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
195093 int iRes = 0;
195094 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
195095 int iLo = 0;
195096 while( iHi>=iLo ){
195097 int iTest = (iHi + iLo) / 2;
195098 if( key >= aEntry[iTest] ){
195099 iRes = iTest;
195100 iLo = iTest+1;
195101 }else{
195102 iHi = iTest-1;
195103 }
195104 }
195105 assert( aEntry[0]<key )((void) (0));
195106 assert( key>=aEntry[iRes] )((void) (0));
195107 return (((unsigned int)c) >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
195108 }
195109 return 1;
195110}
195111
195112
195113/*
195114** If the argument is a codepoint corresponding to a lowercase letter
195115** in the ASCII range with a diacritic added, return the codepoint
195116** of the ASCII letter only. For example, if passed 235 - "LATIN
195117** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
195118** E"). The resuls of passing a codepoint that corresponds to an
195119** uppercase letter are undefined.
195120*/
195121static int remove_diacritic(int c, int bComplex){
195122 unsigned short aDia[] = {
195123 0, 1797, 1848, 1859, 1891, 1928, 1940, 1995,
195124 2024, 2040, 2060, 2110, 2168, 2206, 2264, 2286,
195125 2344, 2383, 2472, 2488, 2516, 2596, 2668, 2732,
195126 2782, 2842, 2894, 2954, 2984, 3000, 3028, 3336,
195127 3456, 3696, 3712, 3728, 3744, 3766, 3832, 3896,
195128 3912, 3928, 3944, 3968, 4008, 4040, 4056, 4106,
195129 4138, 4170, 4202, 4234, 4266, 4296, 4312, 4344,
195130 4408, 4424, 4442, 4472, 4488, 4504, 6148, 6198,
195131 6264, 6280, 6360, 6429, 6505, 6529, 61448, 61468,
195132 61512, 61534, 61592, 61610, 61642, 61672, 61688, 61704,
195133 61726, 61784, 61800, 61816, 61836, 61880, 61896, 61914,
195134 61948, 61998, 62062, 62122, 62154, 62184, 62200, 62218,
195135 62252, 62302, 62364, 62410, 62442, 62478, 62536, 62554,
195136 62584, 62604, 62640, 62648, 62656, 62664, 62730, 62766,
195137 62830, 62890, 62924, 62974, 63032, 63050, 63082, 63118,
195138 63182, 63242, 63274, 63310, 63368, 63390,
195139 };
195140#define HIBIT((unsigned char)0x80) ((unsigned char)0x80)
195141 unsigned char aChar[] = {
195142 '\0', 'a', 'c', 'e', 'i', 'n',
195143 'o', 'u', 'y', 'y', 'a', 'c',
195144 'd', 'e', 'e', 'g', 'h', 'i',
195145 'j', 'k', 'l', 'n', 'o', 'r',
195146 's', 't', 'u', 'u', 'w', 'y',
195147 'z', 'o', 'u', 'a', 'i', 'o',
195148 'u', 'u'|HIBIT((unsigned char)0x80), 'a'|HIBIT((unsigned char)0x80), 'g', 'k', 'o',
195149 'o'|HIBIT((unsigned char)0x80), 'j', 'g', 'n', 'a'|HIBIT((unsigned char)0x80), 'a',
195150 'e', 'i', 'o', 'r', 'u', 's',
195151 't', 'h', 'a', 'e', 'o'|HIBIT((unsigned char)0x80), 'o',
195152 'o'|HIBIT((unsigned char)0x80), 'y', '\0', '\0', '\0', '\0',
195153 '\0', '\0', '\0', '\0', 'a', 'b',
195154 'c'|HIBIT((unsigned char)0x80), 'd', 'd', 'e'|HIBIT((unsigned char)0x80), 'e', 'e'|HIBIT((unsigned char)0x80),
195155 'f', 'g', 'h', 'h', 'i', 'i'|HIBIT((unsigned char)0x80),
195156 'k', 'l', 'l'|HIBIT((unsigned char)0x80), 'l', 'm', 'n',
195157 'o'|HIBIT((unsigned char)0x80), 'p', 'r', 'r'|HIBIT((unsigned char)0x80), 'r', 's',
195158 's'|HIBIT((unsigned char)0x80), 't', 'u', 'u'|HIBIT((unsigned char)0x80), 'v', 'w',
195159 'w', 'x', 'y', 'z', 'h', 't',
195160 'w', 'y', 'a', 'a'|HIBIT((unsigned char)0x80), 'a'|HIBIT((unsigned char)0x80), 'a'|HIBIT((unsigned char)0x80),
195161 'e', 'e'|HIBIT((unsigned char)0x80), 'e'|HIBIT((unsigned char)0x80), 'i', 'o', 'o'|HIBIT((unsigned char)0x80),
195162 'o'|HIBIT((unsigned char)0x80), 'o'|HIBIT((unsigned char)0x80), 'u', 'u'|HIBIT((unsigned char)0x80), 'u'|HIBIT((unsigned char)0x80), 'y',
195163 };
195164
195165 unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
195166 int iRes = 0;
195167 int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
195168 int iLo = 0;
195169 while( iHi>=iLo ){
195170 int iTest = (iHi + iLo) / 2;
195171 if( key >= aDia[iTest] ){
195172 iRes = iTest;
195173 iLo = iTest+1;
195174 }else{
195175 iHi = iTest-1;
195176 }
195177 }
195178 assert( key>=aDia[iRes] )((void) (0));
195179 if( bComplex==0 && (aChar[iRes] & 0x80) ) return c;
195180 return (c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : ((int)aChar[iRes] & 0x7F);
195181}
195182
195183
195184/*
195185** Return true if the argument interpreted as a unicode codepoint
195186** is a diacritical modifier character.
195187*/
195188SQLITE_PRIVATEstatic int sqlite3FtsUnicodeIsdiacritic(int c){
195189 unsigned int mask0 = 0x08029FDF;
195190 unsigned int mask1 = 0x000361F8;
195191 if( c<768 || c>817 ) return 0;
195192 return (c < 768+32) ?
195193 (mask0 & ((unsigned int)1 << (c-768))) :
195194 (mask1 & ((unsigned int)1 << (c-768-32)));
195195}
195196
195197
195198/*
195199** Interpret the argument as a unicode codepoint. If the codepoint
195200** is an upper case character that has a lower case equivalent,
195201** return the codepoint corresponding to the lower case version.
195202** Otherwise, return a copy of the argument.
195203**
195204** The results are undefined if the value passed to this function
195205** is less than zero.
195206*/
195207SQLITE_PRIVATEstatic int sqlite3FtsUnicodeFold(int c, int eRemoveDiacritic){
195208 /* Each entry in the following array defines a rule for folding a range
195209 ** of codepoints to lower case. The rule applies to a range of nRange
195210 ** codepoints starting at codepoint iCode.
195211 **
195212 ** If the least significant bit in flags is clear, then the rule applies
195213 ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
195214 ** need to be folded). Or, if it is set, then the rule only applies to
195215 ** every second codepoint in the range, starting with codepoint C.
195216 **
195217 ** The 7 most significant bits in flags are an index into the aiOff[]
195218 ** array. If a specific codepoint C does require folding, then its lower
195219 ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
195220 **
195221 ** The contents of this array are generated by parsing the CaseFolding.txt
195222 ** file distributed as part of the "Unicode Character Database". See
195223 ** http://www.unicode.org for details.
195224 */
195225 static const struct TableEntry {
195226 unsigned short iCode;
195227 unsigned char flags;
195228 unsigned char nRange;
195229 } aEntry[] = {
195230 {65, 14, 26}, {181, 64, 1}, {192, 14, 23},
195231 {216, 14, 7}, {256, 1, 48}, {306, 1, 6},
195232 {313, 1, 16}, {330, 1, 46}, {376, 116, 1},
195233 {377, 1, 6}, {383, 104, 1}, {385, 50, 1},
195234 {386, 1, 4}, {390, 44, 1}, {391, 0, 1},
195235 {393, 42, 2}, {395, 0, 1}, {398, 32, 1},
195236 {399, 38, 1}, {400, 40, 1}, {401, 0, 1},
195237 {403, 42, 1}, {404, 46, 1}, {406, 52, 1},
195238 {407, 48, 1}, {408, 0, 1}, {412, 52, 1},
195239 {413, 54, 1}, {415, 56, 1}, {416, 1, 6},
195240 {422, 60, 1}, {423, 0, 1}, {425, 60, 1},
195241 {428, 0, 1}, {430, 60, 1}, {431, 0, 1},
195242 {433, 58, 2}, {435, 1, 4}, {439, 62, 1},
195243 {440, 0, 1}, {444, 0, 1}, {452, 2, 1},
195244 {453, 0, 1}, {455, 2, 1}, {456, 0, 1},
195245 {458, 2, 1}, {459, 1, 18}, {478, 1, 18},
195246 {497, 2, 1}, {498, 1, 4}, {502, 122, 1},
195247 {503, 134, 1}, {504, 1, 40}, {544, 110, 1},
195248 {546, 1, 18}, {570, 70, 1}, {571, 0, 1},
195249 {573, 108, 1}, {574, 68, 1}, {577, 0, 1},
195250 {579, 106, 1}, {580, 28, 1}, {581, 30, 1},
195251 {582, 1, 10}, {837, 36, 1}, {880, 1, 4},
195252 {886, 0, 1}, {902, 18, 1}, {904, 16, 3},
195253 {908, 26, 1}, {910, 24, 2}, {913, 14, 17},
195254 {931, 14, 9}, {962, 0, 1}, {975, 4, 1},
195255 {976, 140, 1}, {977, 142, 1}, {981, 146, 1},
195256 {982, 144, 1}, {984, 1, 24}, {1008, 136, 1},
195257 {1009, 138, 1}, {1012, 130, 1}, {1013, 128, 1},
195258 {1015, 0, 1}, {1017, 152, 1}, {1018, 0, 1},
195259 {1021, 110, 3}, {1024, 34, 16}, {1040, 14, 32},
195260 {1120, 1, 34}, {1162, 1, 54}, {1216, 6, 1},
195261 {1217, 1, 14}, {1232, 1, 88}, {1329, 22, 38},
195262 {4256, 66, 38}, {4295, 66, 1}, {4301, 66, 1},
195263 {7680, 1, 150}, {7835, 132, 1}, {7838, 96, 1},
195264 {7840, 1, 96}, {7944, 150, 8}, {7960, 150, 6},
195265 {7976, 150, 8}, {7992, 150, 8}, {8008, 150, 6},
195266 {8025, 151, 8}, {8040, 150, 8}, {8072, 150, 8},
195267 {8088, 150, 8}, {8104, 150, 8}, {8120, 150, 2},
195268 {8122, 126, 2}, {8124, 148, 1}, {8126, 100, 1},
195269 {8136, 124, 4}, {8140, 148, 1}, {8152, 150, 2},
195270 {8154, 120, 2}, {8168, 150, 2}, {8170, 118, 2},
195271 {8172, 152, 1}, {8184, 112, 2}, {8186, 114, 2},
195272 {8188, 148, 1}, {8486, 98, 1}, {8490, 92, 1},
195273 {8491, 94, 1}, {8498, 12, 1}, {8544, 8, 16},
195274 {8579, 0, 1}, {9398, 10, 26}, {11264, 22, 47},
195275 {11360, 0, 1}, {11362, 88, 1}, {11363, 102, 1},
195276 {11364, 90, 1}, {11367, 1, 6}, {11373, 84, 1},
195277 {11374, 86, 1}, {11375, 80, 1}, {11376, 82, 1},
195278 {11378, 0, 1}, {11381, 0, 1}, {11390, 78, 2},
195279 {11392, 1, 100}, {11499, 1, 4}, {11506, 0, 1},
195280 {42560, 1, 46}, {42624, 1, 24}, {42786, 1, 14},
195281 {42802, 1, 62}, {42873, 1, 4}, {42877, 76, 1},
195282 {42878, 1, 10}, {42891, 0, 1}, {42893, 74, 1},
195283 {42896, 1, 4}, {42912, 1, 10}, {42922, 72, 1},
195284 {65313, 14, 26},
195285 };
195286 static const unsigned short aiOff[] = {
195287 1, 2, 8, 15, 16, 26, 28, 32,
195288 37, 38, 40, 48, 63, 64, 69, 71,
195289 79, 80, 116, 202, 203, 205, 206, 207,
195290 209, 210, 211, 213, 214, 217, 218, 219,
195291 775, 7264, 10792, 10795, 23228, 23256, 30204, 54721,
195292 54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274,
195293 57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406,
195294 65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462,
195295 65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511,
195296 65514, 65521, 65527, 65528, 65529,
195297 };
195298
195299 int ret = c;
195300
195301 assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 )((void) (0));
195302
195303 if( c<128 ){
195304 if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
195305 }else if( c<65536 ){
195306 const struct TableEntry *p;
195307 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
195308 int iLo = 0;
195309 int iRes = -1;
195310
195311 assert( c>aEntry[0].iCode )((void) (0));
195312 while( iHi>=iLo ){
195313 int iTest = (iHi + iLo) / 2;
195314 int cmp = (c - aEntry[iTest].iCode);
195315 if( cmp>=0 ){
195316 iRes = iTest;
195317 iLo = iTest+1;
195318 }else{
195319 iHi = iTest-1;
195320 }
195321 }
195322
195323 assert( iRes>=0 && c>=aEntry[iRes].iCode )((void) (0));
195324 p = &aEntry[iRes];
195325 if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
195326 ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
195327 assert( ret>0 )((void) (0));
195328 }
195329
195330 if( eRemoveDiacritic ){
195331 ret = remove_diacritic(ret, eRemoveDiacritic==2);
195332 }
195333 }
195334
195335 else if( c>=66560 && c<66600 ){
195336 ret = c + 40;
195337 }
195338
195339 return ret;
195340}
195341#endif /* defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) */
195342#endif /* !defined(SQLITE_DISABLE_FTS3_UNICODE) */
195343
195344/************** End of fts3_unicode2.c ***************************************/
195345/************** Begin file json.c ********************************************/
195346/*
195347** 2015-08-12
195348**
195349** The author disclaims copyright to this source code. In place of
195350** a legal notice, here is a blessing:
195351**
195352** May you do good and not evil.
195353** May you find forgiveness for yourself and forgive others.
195354** May you share freely, never taking more than you give.
195355**
195356******************************************************************************
195357**
195358** This SQLite JSON functions.
195359**
195360** This file began as an extension in ext/misc/json1.c in 2015. That
195361** extension proved so useful that it has now been moved into the core.
195362**
195363** For the time being, all JSON is stored as pure text. (We might add
195364** a JSONB type in the future which stores a binary encoding of JSON in
195365** a BLOB, but there is no support for JSONB in the current implementation.
195366** This implementation parses JSON text at 250 MB/s, so it is hard to see
195367** how JSONB might improve on that.)
195368*/
195369#ifndef SQLITE_OMIT_JSON
195370/* #include "sqliteInt.h" */
195371
195372/*
195373** Growing our own isspace() routine this way is twice as fast as
195374** the library isspace() function, resulting in a 7% overall performance
195375** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
195376*/
195377static const char jsonIsSpace[] = {
195378 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
195379 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
195380 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
195381 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
195382 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
195383 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
195384 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
195385 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
195386 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
195387 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
195388 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
195389 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
195390 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
195391 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
195392 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
195393 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
195394};
195395#define fast_isspace(x)(jsonIsSpace[(unsigned char)x]) (jsonIsSpace[(unsigned char)x])
195396
195397#if !defined(SQLITE_DEBUG) && !defined(SQLITE_COVERAGE_TEST)
195398# define VVA(X)
195399#else
195400# define VVA(X) X
195401#endif
195402
195403/* Objects */
195404typedef struct JsonString JsonString;
195405typedef struct JsonNode JsonNode;
195406typedef struct JsonParse JsonParse;
195407
195408/* An instance of this object represents a JSON string
195409** under construction. Really, this is a generic string accumulator
195410** that can be and is used to create strings other than JSON.
195411*/
195412struct JsonString {
195413 sqlite3_context *pCtx; /* Function context - put error messages here */
195414 char *zBuf; /* Append JSON content here */
195415 u64 nAlloc; /* Bytes of storage available in zBuf[] */
195416 u64 nUsed; /* Bytes of zBuf[] currently used */
195417 u8 bStatic; /* True if zBuf is static space */
195418 u8 bErr; /* True if an error has been encountered */
195419 char zSpace[100]; /* Initial static space */
195420};
195421
195422/* JSON type values
195423*/
195424#define JSON_NULL0 0
195425#define JSON_TRUE1 1
195426#define JSON_FALSE2 2
195427#define JSON_INT3 3
195428#define JSON_REAL4 4
195429#define JSON_STRING5 5
195430#define JSON_ARRAY6 6
195431#define JSON_OBJECT7 7
195432
195433/* The "subtype" set for JSON values */
195434#define JSON_SUBTYPE74 74 /* Ascii for "J" */
195435
195436/*
195437** Names of the various JSON types:
195438*/
195439static const char * const jsonType[] = {
195440 "null", "true", "false", "integer", "real", "text", "array", "object"
195441};
195442
195443/* Bit values for the JsonNode.jnFlag field
195444*/
195445#define JNODE_RAW0x01 0x01 /* Content is raw, not JSON encoded */
195446#define JNODE_ESCAPE0x02 0x02 /* Content is text with \ escapes */
195447#define JNODE_REMOVE0x04 0x04 /* Do not output */
195448#define JNODE_REPLACE0x08 0x08 /* Replace with JsonNode.u.iReplace */
195449#define JNODE_PATCH0x10 0x10 /* Patch with JsonNode.u.pPatch */
195450#define JNODE_APPEND0x20 0x20 /* More ARRAY/OBJECT entries at u.iAppend */
195451#define JNODE_LABEL0x40 0x40 /* Is a label of an object */
195452
195453
195454/* A single node of parsed JSON
195455*/
195456struct JsonNode {
195457 u8 eType; /* One of the JSON_ type values */
195458 u8 jnFlags; /* JNODE flags */
195459 u8 eU; /* Which union element to use */
195460 u32 n; /* Bytes of content, or number of sub-nodes */
195461 union {
195462 const char *zJContent; /* 1: Content for INT, REAL, and STRING */
195463 u32 iAppend; /* 2: More terms for ARRAY and OBJECT */
195464 u32 iKey; /* 3: Key for ARRAY objects in json_tree() */
195465 u32 iReplace; /* 4: Replacement content for JNODE_REPLACE */
195466 JsonNode *pPatch; /* 5: Node chain of patch for JNODE_PATCH */
195467 } u;
195468};
195469
195470/* A completely parsed JSON string
195471*/
195472struct JsonParse {
195473 u32 nNode; /* Number of slots of aNode[] used */
195474 u32 nAlloc; /* Number of slots of aNode[] allocated */
195475 JsonNode *aNode; /* Array of nodes containing the parse */
195476 const char *zJson; /* Original JSON string */
195477 u32 *aUp; /* Index of parent of each node */
195478 u8 oom; /* Set to true if out of memory */
195479 u8 nErr; /* Number of errors seen */
195480 u16 iDepth; /* Nesting depth */
195481 int nJson; /* Length of the zJson string in bytes */
195482 u32 iHold; /* Replace cache line with the lowest iHold value */
195483};
195484
195485/*
195486** Maximum nesting depth of JSON for this implementation.
195487**
195488** This limit is needed to avoid a stack overflow in the recursive
195489** descent parser. A depth of 2000 is far deeper than any sane JSON
195490** should go.
195491*/
195492#define JSON_MAX_DEPTH2000 2000
195493
195494/**************************************************************************
195495** Utility routines for dealing with JsonString objects
195496**************************************************************************/
195497
195498/* Set the JsonString object to an empty string
195499*/
195500static void jsonZero(JsonString *p){
195501 p->zBuf = p->zSpace;
195502 p->nAlloc = sizeof(p->zSpace);
195503 p->nUsed = 0;
195504 p->bStatic = 1;
195505}
195506
195507/* Initialize the JsonString object
195508*/
195509static void jsonInit(JsonString *p, sqlite3_context *pCtx){
195510 p->pCtx = pCtx;
195511 p->bErr = 0;
195512 jsonZero(p);
195513}
195514
195515
195516/* Free all allocated memory and reset the JsonString object back to its
195517** initial state.
195518*/
195519static void jsonReset(JsonString *p){
195520 if( !p->bStatic ) sqlite3_free(p->zBuf);
195521 jsonZero(p);
195522}
195523
195524
195525/* Report an out-of-memory (OOM) condition
195526*/
195527static void jsonOom(JsonString *p){
195528 p->bErr = 1;
195529 sqlite3_result_error_nomem(p->pCtx);
195530 jsonReset(p);
195531}
195532
195533/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
195534** Return zero on success. Return non-zero on an OOM error
195535*/
195536static int jsonGrow(JsonString *p, u32 N){
195537 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
195538 char *zNew;
195539 if( p->bStatic ){
195540 if( p->bErr ) return 1;
195541 zNew = sqlite3_malloc64(nTotal);
195542 if( zNew==0 ){
195543 jsonOom(p);
195544 return SQLITE_NOMEM7;
195545 }
195546 memcpy(zNew, p->zBuf, (size_t)p->nUsed);
195547 p->zBuf = zNew;
195548 p->bStatic = 0;
195549 }else{
195550 zNew = sqlite3_realloc64(p->zBuf, nTotal);
195551 if( zNew==0 ){
195552 jsonOom(p);
195553 return SQLITE_NOMEM7;
195554 }
195555 p->zBuf = zNew;
195556 }
195557 p->nAlloc = nTotal;
195558 return SQLITE_OK0;
195559}
195560
195561/* Append N bytes from zIn onto the end of the JsonString string.
195562*/
195563static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
195564 if( N==0 ) return;
195565 if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
195566 memcpy(p->zBuf+p->nUsed, zIn, N);
195567 p->nUsed += N;
195568}
195569
195570/* Append formatted text (not to exceed N bytes) to the JsonString.
195571*/
195572static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
195573 va_list ap;
195574 if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return;
195575 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
195576 sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
195577 va_end(ap)__builtin_va_end(ap);
195578 p->nUsed += (int)strlen(p->zBuf+p->nUsed);
195579}
195580
195581/* Append a single character
195582*/
195583static void jsonAppendChar(JsonString *p, char c){
195584 if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
195585 p->zBuf[p->nUsed++] = c;
195586}
195587
195588/* Append a comma separator to the output buffer, if the previous
195589** character is not '[' or '{'.
195590*/
195591static void jsonAppendSeparator(JsonString *p){
195592 char c;
195593 if( p->nUsed==0 ) return;
195594 c = p->zBuf[p->nUsed-1];
195595 if( c!='[' && c!='{' ) jsonAppendChar(p, ',');
195596}
195597
195598/* Append the N-byte string in zIn to the end of the JsonString string
195599** under construction. Enclose the string in "..." and escape
195600** any double-quotes or backslash characters contained within the
195601** string.
195602*/
195603static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
195604 u32 i;
195605 if( zIn==0 || ((N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0) ) return;
195606 p->zBuf[p->nUsed++] = '"';
195607 for(i=0; i<N; i++){
195608 unsigned char c = ((unsigned const char*)zIn)[i];
195609 if( c=='"' || c=='\\' ){
195610 json_simple_escape:
195611 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
195612 p->zBuf[p->nUsed++] = '\\';
195613 }else if( c<=0x1f ){
195614 static const char aSpecial[] = {
195615 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
195616 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
195617 };
195618 assert( sizeof(aSpecial)==32 )((void) (0));
195619 assert( aSpecial['\b']=='b' )((void) (0));
195620 assert( aSpecial['\f']=='f' )((void) (0));
195621 assert( aSpecial['\n']=='n' )((void) (0));
195622 assert( aSpecial['\r']=='r' )((void) (0));
195623 assert( aSpecial['\t']=='t' )((void) (0));
195624 if( aSpecial[c] ){
195625 c = aSpecial[c];
195626 goto json_simple_escape;
195627 }
195628 if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return;
195629 p->zBuf[p->nUsed++] = '\\';
195630 p->zBuf[p->nUsed++] = 'u';
195631 p->zBuf[p->nUsed++] = '0';
195632 p->zBuf[p->nUsed++] = '0';
195633 p->zBuf[p->nUsed++] = '0' + (c>>4);
195634 c = "0123456789abcdef"[c&0xf];
195635 }
195636 p->zBuf[p->nUsed++] = c;
195637 }
195638 p->zBuf[p->nUsed++] = '"';
195639 assert( p->nUsed<p->nAlloc )((void) (0));
195640}
195641
195642/*
195643** Append a function parameter value to the JSON string under
195644** construction.
195645*/
195646static void jsonAppendValue(
195647 JsonString *p, /* Append to this JSON string */
195648 sqlite3_value *pValue /* Value to append */
195649){
195650 switch( sqlite3_value_type(pValue) ){
195651 case SQLITE_NULL5: {
195652 jsonAppendRaw(p, "null", 4);
195653 break;
195654 }
195655 case SQLITE_INTEGER1:
195656 case SQLITE_FLOAT2: {
195657 const char *z = (const char*)sqlite3_value_text(pValue);
195658 u32 n = (u32)sqlite3_value_bytes(pValue);
195659 jsonAppendRaw(p, z, n);
195660 break;
195661 }
195662 case SQLITE_TEXT3: {
195663 const char *z = (const char*)sqlite3_value_text(pValue);
195664 u32 n = (u32)sqlite3_value_bytes(pValue);
195665 if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE74 ){
195666 jsonAppendRaw(p, z, n);
195667 }else{
195668 jsonAppendString(p, z, n);
195669 }
195670 break;
195671 }
195672 default: {
195673 if( p->bErr==0 ){
195674 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
195675 p->bErr = 2;
195676 jsonReset(p);
195677 }
195678 break;
195679 }
195680 }
195681}
195682
195683
195684/* Make the JSON in p the result of the SQL function.
195685*/
195686static void jsonResult(JsonString *p){
195687 if( p->bErr==0 ){
195688 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
195689 p->bStatic ? SQLITE_TRANSIENT((sqlite3_destructor_type)-1) : sqlite3_free,
195690 SQLITE_UTF81);
195691 jsonZero(p);
195692 }
195693 assert( p->bStatic )((void) (0));
195694}
195695
195696/**************************************************************************
195697** Utility routines for dealing with JsonNode and JsonParse objects
195698**************************************************************************/
195699
195700/*
195701** Return the number of consecutive JsonNode slots need to represent
195702** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and
195703** OBJECT types, the number might be larger.
195704**
195705** Appended elements are not counted. The value returned is the number
195706** by which the JsonNode counter should increment in order to go to the
195707** next peer value.
195708*/
195709static u32 jsonNodeSize(JsonNode *pNode){
195710 return pNode->eType>=JSON_ARRAY6 ? pNode->n+1 : 1;
195711}
195712
195713/*
195714** Reclaim all memory allocated by a JsonParse object. But do not
195715** delete the JsonParse object itself.
195716*/
195717static void jsonParseReset(JsonParse *pParse){
195718 sqlite3_free(pParse->aNode);
195719 pParse->aNode = 0;
195720 pParse->nNode = 0;
195721 pParse->nAlloc = 0;
195722 sqlite3_free(pParse->aUp);
195723 pParse->aUp = 0;
195724}
195725
195726/*
195727** Free a JsonParse object that was obtained from sqlite3_malloc().
195728*/
195729static void jsonParseFree(JsonParse *pParse){
195730 jsonParseReset(pParse);
195731 sqlite3_free(pParse);
195732}
195733
195734/*
195735** Convert the JsonNode pNode into a pure JSON string and
195736** append to pOut. Subsubstructure is also included. Return
195737** the number of JsonNode objects that are encoded.
195738*/
195739static void jsonRenderNode(
195740 JsonNode *pNode, /* The node to render */
195741 JsonString *pOut, /* Write JSON here */
195742 sqlite3_value **aReplace /* Replacement values */
195743){
195744 assert( pNode!=0 )((void) (0));
195745 if( pNode->jnFlags & (JNODE_REPLACE0x08|JNODE_PATCH0x10) ){
195746 if( (pNode->jnFlags & JNODE_REPLACE0x08)!=0 && ALWAYS(aReplace!=0)(aReplace!=0) ){
195747 assert( pNode->eU==4 )((void) (0));
195748 jsonAppendValue(pOut, aReplace[pNode->u.iReplace]);
195749 return;
195750 }
195751 assert( pNode->eU==5 )((void) (0));
195752 pNode = pNode->u.pPatch;
195753 }
195754 switch( pNode->eType ){
195755 default: {
195756 assert( pNode->eType==JSON_NULL )((void) (0));
195757 jsonAppendRaw(pOut, "null", 4);
195758 break;
195759 }
195760 case JSON_TRUE1: {
195761 jsonAppendRaw(pOut, "true", 4);
195762 break;
195763 }
195764 case JSON_FALSE2: {
195765 jsonAppendRaw(pOut, "false", 5);
195766 break;
195767 }
195768 case JSON_STRING5: {
195769 if( pNode->jnFlags & JNODE_RAW0x01 ){
195770 assert( pNode->eU==1 )((void) (0));
195771 jsonAppendString(pOut, pNode->u.zJContent, pNode->n);
195772 break;
195773 }
195774 /* no break */ deliberate_fall_through
195775 }
195776 case JSON_REAL4:
195777 case JSON_INT3: {
195778 assert( pNode->eU==1 )((void) (0));
195779 jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
195780 break;
195781 }
195782 case JSON_ARRAY6: {
195783 u32 j = 1;
195784 jsonAppendChar(pOut, '[');
195785 for(;;){
195786 while( j<=pNode->n ){
195787 if( (pNode[j].jnFlags & JNODE_REMOVE0x04)==0 ){
195788 jsonAppendSeparator(pOut);
195789 jsonRenderNode(&pNode[j], pOut, aReplace);
195790 }
195791 j += jsonNodeSize(&pNode[j]);
195792 }
195793 if( (pNode->jnFlags & JNODE_APPEND0x20)==0 ) break;
195794 assert( pNode->eU==2 )((void) (0));
195795 pNode = &pNode[pNode->u.iAppend];
195796 j = 1;
195797 }
195798 jsonAppendChar(pOut, ']');
195799 break;
195800 }
195801 case JSON_OBJECT7: {
195802 u32 j = 1;
195803 jsonAppendChar(pOut, '{');
195804 for(;;){
195805 while( j<=pNode->n ){
195806 if( (pNode[j+1].jnFlags & JNODE_REMOVE0x04)==0 ){
195807 jsonAppendSeparator(pOut);
195808 jsonRenderNode(&pNode[j], pOut, aReplace);
195809 jsonAppendChar(pOut, ':');
195810 jsonRenderNode(&pNode[j+1], pOut, aReplace);
195811 }
195812 j += 1 + jsonNodeSize(&pNode[j+1]);
195813 }
195814 if( (pNode->jnFlags & JNODE_APPEND0x20)==0 ) break;
195815 assert( pNode->eU==2 )((void) (0));
195816 pNode = &pNode[pNode->u.iAppend];
195817 j = 1;
195818 }
195819 jsonAppendChar(pOut, '}');
195820 break;
195821 }
195822 }
195823}
195824
195825/*
195826** Return a JsonNode and all its descendents as a JSON string.
195827*/
195828static void jsonReturnJson(
195829 JsonNode *pNode, /* Node to return */
195830 sqlite3_context *pCtx, /* Return value for this function */
195831 sqlite3_value **aReplace /* Array of replacement values */
195832){
195833 JsonString s;
195834 jsonInit(&s, pCtx);
195835 jsonRenderNode(pNode, &s, aReplace);
195836 jsonResult(&s);
195837 sqlite3_result_subtype(pCtx, JSON_SUBTYPE74);
195838}
195839
195840/*
195841** Translate a single byte of Hex into an integer.
195842** This routine only works if h really is a valid hexadecimal
195843** character: 0..9a..fA..F
195844*/
195845static u8 jsonHexToInt(int h){
195846 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') )((void) (0));
195847#ifdef SQLITE_EBCDIC
195848 h += 9*(1&~(h>>4));
195849#else
195850 h += 9*(1&(h>>6));
195851#endif
195852 return (u8)(h & 0xf);
195853}
195854
195855/*
195856** Convert a 4-byte hex string into an integer
195857*/
195858static u32 jsonHexToInt4(const char *z){
195859 u32 v;
195860 assert( sqlite3Isxdigit(z[0]) )((void) (0));
195861 assert( sqlite3Isxdigit(z[1]) )((void) (0));
195862 assert( sqlite3Isxdigit(z[2]) )((void) (0));
195863 assert( sqlite3Isxdigit(z[3]) )((void) (0));
195864 v = (jsonHexToInt(z[0])<<12)
195865 + (jsonHexToInt(z[1])<<8)
195866 + (jsonHexToInt(z[2])<<4)
195867 + jsonHexToInt(z[3]);
195868 return v;
195869}
195870
195871/*
195872** Make the JsonNode the return value of the function.
195873*/
195874static void jsonReturn(
195875 JsonNode *pNode, /* Node to return */
195876 sqlite3_context *pCtx, /* Return value for this function */
195877 sqlite3_value **aReplace /* Array of replacement values */
195878){
195879 switch( pNode->eType ){
195880 default: {
195881 assert( pNode->eType==JSON_NULL )((void) (0));
195882 sqlite3_result_null(pCtx);
195883 break;
195884 }
195885 case JSON_TRUE1: {
195886 sqlite3_result_int(pCtx, 1);
195887 break;
195888 }
195889 case JSON_FALSE2: {
195890 sqlite3_result_int(pCtx, 0);
195891 break;
195892 }
195893 case JSON_INT3: {
195894 sqlite3_int64 i = 0;
195895 const char *z;
195896 assert( pNode->eU==1 )((void) (0));
195897 z = pNode->u.zJContent;
195898 if( z[0]=='-' ){ z++; }
195899 while( z[0]>='0' && z[0]<='9' ){
195900 unsigned v = *(z++) - '0';
195901 if( i>=LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32))/10 ){
195902 if( i>LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32))/10 ) goto int_as_real;
195903 if( z[0]>='0' && z[0]<='9' ) goto int_as_real;
195904 if( v==9 ) goto int_as_real;
195905 if( v==8 ){
195906 if( pNode->u.zJContent[0]=='-' ){
195907 sqlite3_result_int64(pCtx, SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))));
195908 goto int_done;
195909 }else{
195910 goto int_as_real;
195911 }
195912 }
195913 }
195914 i = i*10 + v;
195915 }
195916 if( pNode->u.zJContent[0]=='-' ){ i = -i; }
195917 sqlite3_result_int64(pCtx, i);
195918 int_done:
195919 break;
195920 int_as_real: ; /* no break */ deliberate_fall_through
195921 }
195922 case JSON_REAL4: {
195923 double r;
195924#ifdef SQLITE_AMALGAMATION1
195925 const char *z;
195926 assert( pNode->eU==1 )((void) (0));
195927 z = pNode->u.zJContent;
195928 sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF81);
195929#else
195930 assert( pNode->eU==1 )((void) (0));
195931 r = strtod(pNode->u.zJContent, 0);
195932#endif
195933 sqlite3_result_double(pCtx, r);
195934 break;
195935 }
195936 case JSON_STRING5: {
195937#if 0 /* Never happens because JNODE_RAW is only set by json_set(),
195938 ** json_insert() and json_replace() and those routines do not
195939 ** call jsonReturn() */
195940 if( pNode->jnFlags & JNODE_RAW0x01 ){
195941 assert( pNode->eU==1 )((void) (0));
195942 sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
195943 SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
195944 }else
195945#endif
195946 assert( (pNode->jnFlags & JNODE_RAW)==0 )((void) (0));
195947 if( (pNode->jnFlags & JNODE_ESCAPE0x02)==0 ){
195948 /* JSON formatted without any backslash-escapes */
195949 assert( pNode->eU==1 )((void) (0));
195950 sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
195951 SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
195952 }else{
195953 /* Translate JSON formatted string into raw text */
195954 u32 i;
195955 u32 n = pNode->n;
195956 const char *z;
195957 char *zOut;
195958 u32 j;
195959 assert( pNode->eU==1 )((void) (0));
195960 z = pNode->u.zJContent;
195961 zOut = sqlite3_malloc( n+1 );
195962 if( zOut==0 ){
195963 sqlite3_result_error_nomem(pCtx);
195964 break;
195965 }
195966 for(i=1, j=0; i<n-1; i++){
195967 char c = z[i];
195968 if( c!='\\' ){
195969 zOut[j++] = c;
195970 }else{
195971 c = z[++i];
195972 if( c=='u' ){
195973 u32 v = jsonHexToInt4(z+i+1);
195974 i += 4;
195975 if( v==0 ) break;
195976 if( v<=0x7f ){
195977 zOut[j++] = (char)v;
195978 }else if( v<=0x7ff ){
195979 zOut[j++] = (char)(0xc0 | (v>>6));
195980 zOut[j++] = 0x80 | (v&0x3f);
195981 }else{
195982 u32 vlo;
195983 if( (v&0xfc00)==0xd800
195984 && i<n-6
195985 && z[i+1]=='\\'
195986 && z[i+2]=='u'
195987 && ((vlo = jsonHexToInt4(z+i+3))&0xfc00)==0xdc00
195988 ){
195989 /* We have a surrogate pair */
195990 v = ((v&0x3ff)<<10) + (vlo&0x3ff) + 0x10000;
195991 i += 6;
195992 zOut[j++] = 0xf0 | (v>>18);
195993 zOut[j++] = 0x80 | ((v>>12)&0x3f);
195994 zOut[j++] = 0x80 | ((v>>6)&0x3f);
195995 zOut[j++] = 0x80 | (v&0x3f);
195996 }else{
195997 zOut[j++] = 0xe0 | (v>>12);
195998 zOut[j++] = 0x80 | ((v>>6)&0x3f);
195999 zOut[j++] = 0x80 | (v&0x3f);
196000 }
196001 }
196002 }else{
196003 if( c=='b' ){
196004 c = '\b';
196005 }else if( c=='f' ){
196006 c = '\f';
196007 }else if( c=='n' ){
196008 c = '\n';
196009 }else if( c=='r' ){
196010 c = '\r';
196011 }else if( c=='t' ){
196012 c = '\t';
196013 }
196014 zOut[j++] = c;
196015 }
196016 }
196017 }
196018 zOut[j] = 0;
196019 sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
196020 }
196021 break;
196022 }
196023 case JSON_ARRAY6:
196024 case JSON_OBJECT7: {
196025 jsonReturnJson(pNode, pCtx, aReplace);
196026 break;
196027 }
196028 }
196029}
196030
196031/* Forward reference */
196032static int jsonParseAddNode(JsonParse*,u32,u32,const char*);
196033
196034/*
196035** A macro to hint to the compiler that a function should not be
196036** inlined.
196037*/
196038#if defined(__GNUC__4)
196039# define JSON_NOINLINE__attribute__((noinline)) __attribute__((noinline))
196040#elif defined(_MSC_VER) && _MSC_VER>=1310
196041# define JSON_NOINLINE__attribute__((noinline)) __declspec(noinline)
196042#else
196043# define JSON_NOINLINE__attribute__((noinline))
196044#endif
196045
196046
196047static JSON_NOINLINE__attribute__((noinline)) int jsonParseAddNodeExpand(
196048 JsonParse *pParse, /* Append the node to this object */
196049 u32 eType, /* Node type */
196050 u32 n, /* Content size or sub-node count */
196051 const char *zContent /* Content */
196052){
196053 u32 nNew;
196054 JsonNode *pNew;
196055 assert( pParse->nNode>=pParse->nAlloc )((void) (0));
196056 if( pParse->oom ) return -1;
196057 nNew = pParse->nAlloc*2 + 10;
196058 pNew = sqlite3_realloc64(pParse->aNode, sizeof(JsonNode)*nNew);
196059 if( pNew==0 ){
196060 pParse->oom = 1;
196061 return -1;
196062 }
196063 pParse->nAlloc = nNew;
196064 pParse->aNode = pNew;
196065 assert( pParse->nNode<pParse->nAlloc )((void) (0));
196066 return jsonParseAddNode(pParse, eType, n, zContent);
196067}
196068
196069/*
196070** Create a new JsonNode instance based on the arguments and append that
196071** instance to the JsonParse. Return the index in pParse->aNode[] of the
196072** new node, or -1 if a memory allocation fails.
196073*/
196074static int jsonParseAddNode(
196075 JsonParse *pParse, /* Append the node to this object */
196076 u32 eType, /* Node type */
196077 u32 n, /* Content size or sub-node count */
196078 const char *zContent /* Content */
196079){
196080 JsonNode *p;
196081 if( pParse->aNode==0 || pParse->nNode>=pParse->nAlloc ){
196082 return jsonParseAddNodeExpand(pParse, eType, n, zContent);
196083 }
196084 p = &pParse->aNode[pParse->nNode];
196085 p->eType = (u8)eType;
196086 p->jnFlags = 0;
196087 VVA( p->eU = zContent ? 1 : 0 );
196088 p->n = n;
196089 p->u.zJContent = zContent;
196090 return pParse->nNode++;
196091}
196092
196093/*
196094** Return true if z[] begins with 4 (or more) hexadecimal digits
196095*/
196096static int jsonIs4Hex(const char *z){
196097 int i;
196098 for(i=0; i<4; i++) if( !sqlite3Isxdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x08) ) return 0;
196099 return 1;
196100}
196101
196102/*
196103** Parse a single JSON value which begins at pParse->zJson[i]. Return the
196104** index of the first character past the end of the value parsed.
196105**
196106** Return negative for a syntax error. Special cases: return -2 if the
196107** first non-whitespace character is '}' and return -3 if the first
196108** non-whitespace character is ']'.
196109*/
196110static int jsonParseValue(JsonParse *pParse, u32 i){
196111 char c;
196112 u32 j;
196113 int iThis;
196114 int x;
196115 JsonNode *pNode;
196116 const char *z = pParse->zJson;
196117 while( fast_isspace(z[i])(jsonIsSpace[(unsigned char)z[i]]) ){ i++; }
196118 if( (c = z[i])=='{' ){
196119 /* Parse object */
196120 iThis = jsonParseAddNode(pParse, JSON_OBJECT7, 0, 0);
196121 if( iThis<0 ) return -1;
196122 for(j=i+1;;j++){
196123 while( fast_isspace(z[j])(jsonIsSpace[(unsigned char)z[j]]) ){ j++; }
196124 if( ++pParse->iDepth > JSON_MAX_DEPTH2000 ) return -1;
196125 x = jsonParseValue(pParse, j);
196126 if( x<0 ){
196127 pParse->iDepth--;
196128 if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1;
196129 return -1;
196130 }
196131 if( pParse->oom ) return -1;
196132 pNode = &pParse->aNode[pParse->nNode-1];
196133 if( pNode->eType!=JSON_STRING5 ) return -1;
196134 pNode->jnFlags |= JNODE_LABEL0x40;
196135 j = x;
196136 while( fast_isspace(z[j])(jsonIsSpace[(unsigned char)z[j]]) ){ j++; }
196137 if( z[j]!=':' ) return -1;
196138 j++;
196139 x = jsonParseValue(pParse, j);
196140 pParse->iDepth--;
196141 if( x<0 ) return -1;
196142 j = x;
196143 while( fast_isspace(z[j])(jsonIsSpace[(unsigned char)z[j]]) ){ j++; }
196144 c = z[j];
196145 if( c==',' ) continue;
196146 if( c!='}' ) return -1;
196147 break;
196148 }
196149 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
196150 return j+1;
196151 }else if( c=='[' ){
196152 /* Parse array */
196153 iThis = jsonParseAddNode(pParse, JSON_ARRAY6, 0, 0);
196154 if( iThis<0 ) return -1;
196155 memset(&pParse->aNode[iThis].u, 0, sizeof(pParse->aNode[iThis].u));
196156 for(j=i+1;;j++){
196157 while( fast_isspace(z[j])(jsonIsSpace[(unsigned char)z[j]]) ){ j++; }
196158 if( ++pParse->iDepth > JSON_MAX_DEPTH2000 ) return -1;
196159 x = jsonParseValue(pParse, j);
196160 pParse->iDepth--;
196161 if( x<0 ){
196162 if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1;
196163 return -1;
196164 }
196165 j = x;
196166 while( fast_isspace(z[j])(jsonIsSpace[(unsigned char)z[j]]) ){ j++; }
196167 c = z[j];
196168 if( c==',' ) continue;
196169 if( c!=']' ) return -1;
196170 break;
196171 }
196172 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
196173 return j+1;
196174 }else if( c=='"' ){
196175 /* Parse string */
196176 u8 jnFlags = 0;
196177 j = i+1;
196178 for(;;){
196179 c = z[j];
196180 if( (c & ~0x1f)==0 ){
196181 /* Control characters are not allowed in strings */
196182 return -1;
196183 }
196184 if( c=='\\' ){
196185 c = z[++j];
196186 if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f'
196187 || c=='n' || c=='r' || c=='t'
196188 || (c=='u' && jsonIs4Hex(z+j+1)) ){
196189 jnFlags = JNODE_ESCAPE0x02;
196190 }else{
196191 return -1;
196192 }
196193 }else if( c=='"' ){
196194 break;
196195 }
196196 j++;
196197 }
196198 jsonParseAddNode(pParse, JSON_STRING5, j+1-i, &z[i]);
196199 if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
196200 return j+1;
196201 }else if( c=='n'
196202 && strncmp(z+i,"null",4)==0
196203 && !sqlite3Isalnum(z[i+4])(sqlite3CtypeMap[(unsigned char)(z[i+4])]&0x06) ){
196204 jsonParseAddNode(pParse, JSON_NULL0, 0, 0);
196205 return i+4;
196206 }else if( c=='t'
196207 && strncmp(z+i,"true",4)==0
196208 && !sqlite3Isalnum(z[i+4])(sqlite3CtypeMap[(unsigned char)(z[i+4])]&0x06) ){
196209 jsonParseAddNode(pParse, JSON_TRUE1, 0, 0);
196210 return i+4;
196211 }else if( c=='f'
196212 && strncmp(z+i,"false",5)==0
196213 && !sqlite3Isalnum(z[i+5])(sqlite3CtypeMap[(unsigned char)(z[i+5])]&0x06) ){
196214 jsonParseAddNode(pParse, JSON_FALSE2, 0, 0);
196215 return i+5;
196216 }else if( c=='-' || (c>='0' && c<='9') ){
196217 /* Parse number */
196218 u8 seenDP = 0;
196219 u8 seenE = 0;
196220 assert( '-' < '0' )((void) (0));
196221 if( c<='0' ){
196222 j = c=='-' ? i+1 : i;
196223 if( z[j]=='0' && z[j+1]>='0' && z[j+1]<='9' ) return -1;
196224 }
196225 j = i+1;
196226 for(;; j++){
196227 c = z[j];
196228 if( c>='0' && c<='9' ) continue;
196229 if( c=='.' ){
196230 if( z[j-1]=='-' ) return -1;
196231 if( seenDP ) return -1;
196232 seenDP = 1;
196233 continue;
196234 }
196235 if( c=='e' || c=='E' ){
196236 if( z[j-1]<'0' ) return -1;
196237 if( seenE ) return -1;
196238 seenDP = seenE = 1;
196239 c = z[j+1];
196240 if( c=='+' || c=='-' ){
196241 j++;
196242 c = z[j+1];
196243 }
196244 if( c<'0' || c>'9' ) return -1;
196245 continue;
196246 }
196247 break;
196248 }
196249 if( z[j-1]<'0' ) return -1;
196250 jsonParseAddNode(pParse, seenDP ? JSON_REAL4 : JSON_INT3,
196251 j - i, &z[i]);
196252 return j;
196253 }else if( c=='}' ){
196254 return -2; /* End of {...} */
196255 }else if( c==']' ){
196256 return -3; /* End of [...] */
196257 }else if( c==0 ){
196258 return 0; /* End of file */
196259 }else{
196260 return -1; /* Syntax error */
196261 }
196262}
196263
196264/*
196265** Parse a complete JSON string. Return 0 on success or non-zero if there
196266** are any errors. If an error occurs, free all memory associated with
196267** pParse.
196268**
196269** pParse is uninitialized when this routine is called.
196270*/
196271static int jsonParse(
196272 JsonParse *pParse, /* Initialize and fill this JsonParse object */
196273 sqlite3_context *pCtx, /* Report errors here */
196274 const char *zJson /* Input JSON text to be parsed */
196275){
196276 int i;
196277 memset(pParse, 0, sizeof(*pParse));
196278 if( zJson==0 ) return 1;
196279 pParse->zJson = zJson;
196280 i = jsonParseValue(pParse, 0);
196281 if( pParse->oom ) i = -1;
196282 if( i>0 ){
196283 assert( pParse->iDepth==0 )((void) (0));
196284 while( fast_isspace(zJson[i])(jsonIsSpace[(unsigned char)zJson[i]]) ) i++;
196285 if( zJson[i] ) i = -1;
196286 }
196287 if( i<=0 ){
196288 if( pCtx!=0 ){
196289 if( pParse->oom ){
196290 sqlite3_result_error_nomem(pCtx);
196291 }else{
196292 sqlite3_result_error(pCtx, "malformed JSON", -1);
196293 }
196294 }
196295 jsonParseReset(pParse);
196296 return 1;
196297 }
196298 return 0;
196299}
196300
196301/* Mark node i of pParse as being a child of iParent. Call recursively
196302** to fill in all the descendants of node i.
196303*/
196304static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){
196305 JsonNode *pNode = &pParse->aNode[i];
196306 u32 j;
196307 pParse->aUp[i] = iParent;
196308 switch( pNode->eType ){
196309 case JSON_ARRAY6: {
196310 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){
196311 jsonParseFillInParentage(pParse, i+j, i);
196312 }
196313 break;
196314 }
196315 case JSON_OBJECT7: {
196316 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){
196317 pParse->aUp[i+j] = i;
196318 jsonParseFillInParentage(pParse, i+j+1, i);
196319 }
196320 break;
196321 }
196322 default: {
196323 break;
196324 }
196325 }
196326}
196327
196328/*
196329** Compute the parentage of all nodes in a completed parse.
196330*/
196331static int jsonParseFindParents(JsonParse *pParse){
196332 u32 *aUp;
196333 assert( pParse->aUp==0 )((void) (0));
196334 aUp = pParse->aUp = sqlite3_malloc64( sizeof(u32)*pParse->nNode );
196335 if( aUp==0 ){
196336 pParse->oom = 1;
196337 return SQLITE_NOMEM7;
196338 }
196339 jsonParseFillInParentage(pParse, 0, 0);
196340 return SQLITE_OK0;
196341}
196342
196343/*
196344** Magic number used for the JSON parse cache in sqlite3_get_auxdata()
196345*/
196346#define JSON_CACHE_ID(-429938) (-429938) /* First cache entry */
196347#define JSON_CACHE_SZ4 4 /* Max number of cache entries */
196348
196349/*
196350** Obtain a complete parse of the JSON found in the first argument
196351** of the argv array. Use the sqlite3_get_auxdata() cache for this
196352** parse if it is available. If the cache is not available or if it
196353** is no longer valid, parse the JSON again and return the new parse,
196354** and also register the new parse so that it will be available for
196355** future sqlite3_get_auxdata() calls.
196356*/
196357static JsonParse *jsonParseCached(
196358 sqlite3_context *pCtx,
196359 sqlite3_value **argv,
196360 sqlite3_context *pErrCtx
196361){
196362 const char *zJson = (const char*)sqlite3_value_text(argv[0]);
196363 int nJson = sqlite3_value_bytes(argv[0]);
196364 JsonParse *p;
196365 JsonParse *pMatch = 0;
196366 int iKey;
196367 int iMinKey = 0;
196368 u32 iMinHold = 0xffffffff;
196369 u32 iMaxHold = 0;
196370 if( zJson==0 ) return 0;
196371 for(iKey=0; iKey<JSON_CACHE_SZ4; iKey++){
196372 p = (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID(-429938)+iKey);
196373 if( p==0 ){
196374 iMinKey = iKey;
196375 break;
196376 }
196377 if( pMatch==0
196378 && p->nJson==nJson
196379 && memcmp(p->zJson,zJson,nJson)==0
196380 ){
196381 p->nErr = 0;
196382 pMatch = p;
196383 }else if( p->iHold<iMinHold ){
196384 iMinHold = p->iHold;
196385 iMinKey = iKey;
196386 }
196387 if( p->iHold>iMaxHold ){
196388 iMaxHold = p->iHold;
196389 }
196390 }
196391 if( pMatch ){
196392 pMatch->nErr = 0;
196393 pMatch->iHold = iMaxHold+1;
196394 return pMatch;
196395 }
196396 p = sqlite3_malloc64( sizeof(*p) + nJson + 1 );
196397 if( p==0 ){
196398 sqlite3_result_error_nomem(pCtx);
196399 return 0;
196400 }
196401 memset(p, 0, sizeof(*p));
196402 p->zJson = (char*)&p[1];
196403 memcpy((char*)p->zJson, zJson, nJson+1);
196404 if( jsonParse(p, pErrCtx, p->zJson) ){
196405 sqlite3_free(p);
196406 return 0;
196407 }
196408 p->nJson = nJson;
196409 p->iHold = iMaxHold+1;
196410 sqlite3_set_auxdata(pCtx, JSON_CACHE_ID(-429938)+iMinKey, p,
196411 (void(*)(void*))jsonParseFree);
196412 return (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID(-429938)+iMinKey);
196413}
196414
196415/*
196416** Compare the OBJECT label at pNode against zKey,nKey. Return true on
196417** a match.
196418*/
196419static int jsonLabelCompare(JsonNode *pNode, const char *zKey, u32 nKey){
196420 assert( pNode->eU==1 )((void) (0));
196421 if( pNode->jnFlags & JNODE_RAW0x01 ){
196422 if( pNode->n!=nKey ) return 0;
196423 return strncmp(pNode->u.zJContent, zKey, nKey)==0;
196424 }else{
196425 if( pNode->n!=nKey+2 ) return 0;
196426 return strncmp(pNode->u.zJContent+1, zKey, nKey)==0;
196427 }
196428}
196429
196430/* forward declaration */
196431static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**);
196432
196433/*
196434** Search along zPath to find the node specified. Return a pointer
196435** to that node, or NULL if zPath is malformed or if there is no such
196436** node.
196437**
196438** If pApnd!=0, then try to append new nodes to complete zPath if it is
196439** possible to do so and if no existing node corresponds to zPath. If
196440** new nodes are appended *pApnd is set to 1.
196441*/
196442static JsonNode *jsonLookupStep(
196443 JsonParse *pParse, /* The JSON to search */
196444 u32 iRoot, /* Begin the search at this node */
196445 const char *zPath, /* The path to search */
196446 int *pApnd, /* Append nodes to complete path if not NULL */
196447 const char **pzErr /* Make *pzErr point to any syntax error in zPath */
196448){
196449 u32 i, j, nKey;
196450 const char *zKey;
196451 JsonNode *pRoot = &pParse->aNode[iRoot];
196452 if( zPath[0]==0 ) return pRoot;
196453 if( pRoot->jnFlags & JNODE_REPLACE0x08 ) return 0;
196454 if( zPath[0]=='.' ){
196455 if( pRoot->eType!=JSON_OBJECT7 ) return 0;
196456 zPath++;
196457 if( zPath[0]=='"' ){
196458 zKey = zPath + 1;
196459 for(i=1; zPath[i] && zPath[i]!='"'; i++){}
196460 nKey = i-1;
196461 if( zPath[i] ){
196462 i++;
196463 }else{
196464 *pzErr = zPath;
196465 return 0;
196466 }
196467 testcase( nKey==0 );
196468 }else{
196469 zKey = zPath;
196470 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
196471 nKey = i;
196472 if( nKey==0 ){
196473 *pzErr = zPath;
196474 return 0;
196475 }
196476 }
196477 j = 1;
196478 for(;;){
196479 while( j<=pRoot->n ){
196480 if( jsonLabelCompare(pRoot+j, zKey, nKey) ){
196481 return jsonLookupStep(pParse, iRoot+j+1, &zPath[i], pApnd, pzErr);
196482 }
196483 j++;
196484 j += jsonNodeSize(&pRoot[j]);
196485 }
196486 if( (pRoot->jnFlags & JNODE_APPEND0x20)==0 ) break;
196487 assert( pRoot->eU==2 )((void) (0));
196488 iRoot += pRoot->u.iAppend;
196489 pRoot = &pParse->aNode[iRoot];
196490 j = 1;
196491 }
196492 if( pApnd ){
196493 u32 iStart, iLabel;
196494 JsonNode *pNode;
196495 iStart = jsonParseAddNode(pParse, JSON_OBJECT7, 2, 0);
196496 iLabel = jsonParseAddNode(pParse, JSON_STRING5, nKey, zKey);
196497 zPath += i;
196498 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
196499 if( pParse->oom ) return 0;
196500 if( pNode ){
196501 pRoot = &pParse->aNode[iRoot];
196502 assert( pRoot->eU==0 )((void) (0));
196503 pRoot->u.iAppend = iStart - iRoot;
196504 pRoot->jnFlags |= JNODE_APPEND0x20;
196505 VVA( pRoot->eU = 2 );
196506 pParse->aNode[iLabel].jnFlags |= JNODE_RAW0x01;
196507 }
196508 return pNode;
196509 }
196510 }else if( zPath[0]=='[' ){
196511 i = 0;
196512 j = 1;
196513 while( sqlite3Isdigit(zPath[j])(sqlite3CtypeMap[(unsigned char)(zPath[j])]&0x04) ){
196514 i = i*10 + zPath[j] - '0';
196515 j++;
196516 }
196517 if( j<2 || zPath[j]!=']' ){
196518 if( zPath[1]=='#' ){
196519 JsonNode *pBase = pRoot;
196520 int iBase = iRoot;
196521 if( pRoot->eType!=JSON_ARRAY6 ) return 0;
196522 for(;;){
196523 while( j<=pBase->n ){
196524 if( (pBase[j].jnFlags & JNODE_REMOVE0x04)==0 ) i++;
196525 j += jsonNodeSize(&pBase[j]);
196526 }
196527 if( (pBase->jnFlags & JNODE_APPEND0x20)==0 ) break;
196528 assert( pBase->eU==2 )((void) (0));
196529 iBase += pBase->u.iAppend;
196530 pBase = &pParse->aNode[iBase];
196531 j = 1;
196532 }
196533 j = 2;
196534 if( zPath[2]=='-' && sqlite3Isdigit(zPath[3])(sqlite3CtypeMap[(unsigned char)(zPath[3])]&0x04) ){
196535 unsigned int x = 0;
196536 j = 3;
196537 do{
196538 x = x*10 + zPath[j] - '0';
196539 j++;
196540 }while( sqlite3Isdigit(zPath[j])(sqlite3CtypeMap[(unsigned char)(zPath[j])]&0x04) );
196541 if( x>i ) return 0;
196542 i -= x;
196543 }
196544 if( zPath[j]!=']' ){
196545 *pzErr = zPath;
196546 return 0;
196547 }
196548 }else{
196549 *pzErr = zPath;
196550 return 0;
196551 }
196552 }
196553 if( pRoot->eType!=JSON_ARRAY6 ) return 0;
196554 zPath += j + 1;
196555 j = 1;
196556 for(;;){
196557 while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE0x04)!=0) ){
196558 if( (pRoot[j].jnFlags & JNODE_REMOVE0x04)==0 ) i--;
196559 j += jsonNodeSize(&pRoot[j]);
196560 }
196561 if( (pRoot->jnFlags & JNODE_APPEND0x20)==0 ) break;
196562 assert( pRoot->eU==2 )((void) (0));
196563 iRoot += pRoot->u.iAppend;
196564 pRoot = &pParse->aNode[iRoot];
196565 j = 1;
196566 }
196567 if( j<=pRoot->n ){
196568 return jsonLookupStep(pParse, iRoot+j, zPath, pApnd, pzErr);
196569 }
196570 if( i==0 && pApnd ){
196571 u32 iStart;
196572 JsonNode *pNode;
196573 iStart = jsonParseAddNode(pParse, JSON_ARRAY6, 1, 0);
196574 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
196575 if( pParse->oom ) return 0;
196576 if( pNode ){
196577 pRoot = &pParse->aNode[iRoot];
196578 assert( pRoot->eU==0 )((void) (0));
196579 pRoot->u.iAppend = iStart - iRoot;
196580 pRoot->jnFlags |= JNODE_APPEND0x20;
196581 VVA( pRoot->eU = 2 );
196582 }
196583 return pNode;
196584 }
196585 }else{
196586 *pzErr = zPath;
196587 }
196588 return 0;
196589}
196590
196591/*
196592** Append content to pParse that will complete zPath. Return a pointer
196593** to the inserted node, or return NULL if the append fails.
196594*/
196595static JsonNode *jsonLookupAppend(
196596 JsonParse *pParse, /* Append content to the JSON parse */
196597 const char *zPath, /* Description of content to append */
196598 int *pApnd, /* Set this flag to 1 */
196599 const char **pzErr /* Make this point to any syntax error */
196600){
196601 *pApnd = 1;
196602 if( zPath[0]==0 ){
196603 jsonParseAddNode(pParse, JSON_NULL0, 0, 0);
196604 return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
196605 }
196606 if( zPath[0]=='.' ){
196607 jsonParseAddNode(pParse, JSON_OBJECT7, 0, 0);
196608 }else if( strncmp(zPath,"[0]",3)==0 ){
196609 jsonParseAddNode(pParse, JSON_ARRAY6, 0, 0);
196610 }else{
196611 return 0;
196612 }
196613 if( pParse->oom ) return 0;
196614 return jsonLookupStep(pParse, pParse->nNode-1, zPath, pApnd, pzErr);
196615}
196616
196617/*
196618** Return the text of a syntax error message on a JSON path. Space is
196619** obtained from sqlite3_malloc().
196620*/
196621static char *jsonPathSyntaxError(const char *zErr){
196622 return sqlite3_mprintf("JSON path error near '%q'", zErr);
196623}
196624
196625/*
196626** Do a node lookup using zPath. Return a pointer to the node on success.
196627** Return NULL if not found or if there is an error.
196628**
196629** On an error, write an error message into pCtx and increment the
196630** pParse->nErr counter.
196631**
196632** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if
196633** nodes are appended.
196634*/
196635static JsonNode *jsonLookup(
196636 JsonParse *pParse, /* The JSON to search */
196637 const char *zPath, /* The path to search */
196638 int *pApnd, /* Append nodes to complete path if not NULL */
196639 sqlite3_context *pCtx /* Report errors here, if not NULL */
196640){
196641 const char *zErr = 0;
196642 JsonNode *pNode = 0;
196643 char *zMsg;
196644
196645 if( zPath==0 ) return 0;
196646 if( zPath[0]!='$' ){
196647 zErr = zPath;
196648 goto lookup_err;
196649 }
196650 zPath++;
196651 pNode = jsonLookupStep(pParse, 0, zPath, pApnd, &zErr);
196652 if( zErr==0 ) return pNode;
196653
196654lookup_err:
196655 pParse->nErr++;
196656 assert( zErr!=0 && pCtx!=0 )((void) (0));
196657 zMsg = jsonPathSyntaxError(zErr);
196658 if( zMsg ){
196659 sqlite3_result_error(pCtx, zMsg, -1);
196660 sqlite3_free(zMsg);
196661 }else{
196662 sqlite3_result_error_nomem(pCtx);
196663 }
196664 return 0;
196665}
196666
196667
196668/*
196669** Report the wrong number of arguments for json_insert(), json_replace()
196670** or json_set().
196671*/
196672static void jsonWrongNumArgs(
196673 sqlite3_context *pCtx,
196674 const char *zFuncName
196675){
196676 char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
196677 zFuncName);
196678 sqlite3_result_error(pCtx, zMsg, -1);
196679 sqlite3_free(zMsg);
196680}
196681
196682/*
196683** Mark all NULL entries in the Object passed in as JNODE_REMOVE.
196684*/
196685static void jsonRemoveAllNulls(JsonNode *pNode){
196686 int i, n;
196687 assert( pNode->eType==JSON_OBJECT )((void) (0));
196688 n = pNode->n;
196689 for(i=2; i<=n; i += jsonNodeSize(&pNode[i])+1){
196690 switch( pNode[i].eType ){
196691 case JSON_NULL0:
196692 pNode[i].jnFlags |= JNODE_REMOVE0x04;
196693 break;
196694 case JSON_OBJECT7:
196695 jsonRemoveAllNulls(&pNode[i]);
196696 break;
196697 }
196698 }
196699}
196700
196701
196702/****************************************************************************
196703** SQL functions used for testing and debugging
196704****************************************************************************/
196705
196706#ifdef SQLITE_DEBUG
196707/*
196708** The json_parse(JSON) function returns a string which describes
196709** a parse of the JSON provided. Or it returns NULL if JSON is not
196710** well-formed.
196711*/
196712static void jsonParseFunc(
196713 sqlite3_context *ctx,
196714 int argc,
196715 sqlite3_value **argv
196716){
196717 JsonString s; /* Output string - not real JSON */
196718 JsonParse x; /* The parse */
196719 u32 i;
196720
196721 assert( argc==1 )((void) (0));
196722 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
196723 jsonParseFindParents(&x);
196724 jsonInit(&s, ctx);
196725 for(i=0; i<x.nNode; i++){
196726 const char *zType;
196727 if( x.aNode[i].jnFlags & JNODE_LABEL0x40 ){
196728 assert( x.aNode[i].eType==JSON_STRING )((void) (0));
196729 zType = "label";
196730 }else{
196731 zType = jsonType[x.aNode[i].eType];
196732 }
196733 jsonPrintf(100, &s,"node %3u: %7s n=%-4d up=%-4d",
196734 i, zType, x.aNode[i].n, x.aUp[i]);
196735 assert( x.aNode[i].eU==0 || x.aNode[i].eU==1 )((void) (0));
196736 if( x.aNode[i].u.zJContent!=0 ){
196737 assert( x.aNode[i].eU==1 )((void) (0));
196738 jsonAppendRaw(&s, " ", 1);
196739 jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
196740 }else{
196741 assert( x.aNode[i].eU==0 )((void) (0));
196742 }
196743 jsonAppendRaw(&s, "\n", 1);
196744 }
196745 jsonParseReset(&x);
196746 jsonResult(&s);
196747}
196748
196749/*
196750** The json_test1(JSON) function return true (1) if the input is JSON
196751** text generated by another json function. It returns (0) if the input
196752** is not known to be JSON.
196753*/
196754static void jsonTest1Func(
196755 sqlite3_context *ctx,
196756 int argc,
196757 sqlite3_value **argv
196758){
196759 UNUSED_PARAMETER(argc)(void)(argc);
196760 sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE74);
196761}
196762#endif /* SQLITE_DEBUG */
196763
196764/****************************************************************************
196765** Scalar SQL function implementations
196766****************************************************************************/
196767
196768/*
196769** Implementation of the json_QUOTE(VALUE) function. Return a JSON value
196770** corresponding to the SQL value input. Mostly this means putting
196771** double-quotes around strings and returning the unquoted string "null"
196772** when given a NULL input.
196773*/
196774static void jsonQuoteFunc(
196775 sqlite3_context *ctx,
196776 int argc,
196777 sqlite3_value **argv
196778){
196779 JsonString jx;
196780 UNUSED_PARAMETER(argc)(void)(argc);
196781
196782 jsonInit(&jx, ctx);
196783 jsonAppendValue(&jx, argv[0]);
196784 jsonResult(&jx);
196785 sqlite3_result_subtype(ctx, JSON_SUBTYPE74);
196786}
196787
196788/*
196789** Implementation of the json_array(VALUE,...) function. Return a JSON
196790** array that contains all values given in arguments. Or if any argument
196791** is a BLOB, throw an error.
196792*/
196793static void jsonArrayFunc(
196794 sqlite3_context *ctx,
196795 int argc,
196796 sqlite3_value **argv
196797){
196798 int i;
196799 JsonString jx;
196800
196801 jsonInit(&jx, ctx);
196802 jsonAppendChar(&jx, '[');
196803 for(i=0; i<argc; i++){
196804 jsonAppendSeparator(&jx);
196805 jsonAppendValue(&jx, argv[i]);
196806 }
196807 jsonAppendChar(&jx, ']');
196808 jsonResult(&jx);
196809 sqlite3_result_subtype(ctx, JSON_SUBTYPE74);
196810}
196811
196812
196813/*
196814** json_array_length(JSON)
196815** json_array_length(JSON, PATH)
196816**
196817** Return the number of elements in the top-level JSON array.
196818** Return 0 if the input is not a well-formed JSON array.
196819*/
196820static void jsonArrayLengthFunc(
196821 sqlite3_context *ctx,
196822 int argc,
196823 sqlite3_value **argv
196824){
196825 JsonParse *p; /* The parse */
196826 sqlite3_int64 n = 0;
196827 u32 i;
196828 JsonNode *pNode;
196829
196830 p = jsonParseCached(ctx, argv, ctx);
196831 if( p==0 ) return;
196832 assert( p->nNode )((void) (0));
196833 if( argc==2 ){
196834 const char *zPath = (const char*)sqlite3_value_text(argv[1]);
196835 pNode = jsonLookup(p, zPath, 0, ctx);
196836 }else{
196837 pNode = p->aNode;
196838 }
196839 if( pNode==0 ){
196840 return;
196841 }
196842 if( pNode->eType==JSON_ARRAY6 ){
196843 assert( (pNode->jnFlags & JNODE_APPEND)==0 )((void) (0));
196844 for(i=1; i<=pNode->n; n++){
196845 i += jsonNodeSize(&pNode[i]);
196846 }
196847 }
196848 sqlite3_result_int64(ctx, n);
196849}
196850
196851/*
196852** Bit values for the flags passed into jsonExtractFunc() or
196853** jsonSetFunc() via the user-data value.
196854*/
196855#define JSON_JSON0x01 0x01 /* Result is always JSON */
196856#define JSON_SQL0x02 0x02 /* Result is always SQL */
196857#define JSON_ABPATH0x03 0x03 /* Allow abbreviated JSON path specs */
196858#define JSON_ISSET0x04 0x04 /* json_set(), not json_insert() */
196859
196860/*
196861** json_extract(JSON, PATH, ...)
196862** "->"(JSON,PATH)
196863** "->>"(JSON,PATH)
196864**
196865** Return the element described by PATH. Return NULL if that PATH element
196866** is not found.
196867**
196868** If JSON_JSON is set or if more that one PATH argument is supplied then
196869** always return a JSON representation of the result. If JSON_SQL is set,
196870** then always return an SQL representation of the result. If neither flag
196871** is present and argc==2, then return JSON for objects and arrays and SQL
196872** for all other values.
196873**
196874** When multiple PATH arguments are supplied, the result is a JSON array
196875** containing the result of each PATH.
196876**
196877** Abbreviated JSON path expressions are allows if JSON_ABPATH, for
196878** compatibility with PG.
196879*/
196880static void jsonExtractFunc(
196881 sqlite3_context *ctx,
196882 int argc,
196883 sqlite3_value **argv
196884){
196885 JsonParse *p; /* The parse */
196886 JsonNode *pNode;
196887 const char *zPath;
196888 int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx))((int)(long int)(sqlite3_user_data(ctx)));
196889 JsonString jx;
196890
196891 if( argc<2 ) return;
196892 p = jsonParseCached(ctx, argv, ctx);
196893 if( p==0 ) return;
196894 if( argc==2 ){
196895 /* With a single PATH argument */
196896 zPath = (const char*)sqlite3_value_text(argv[1]);
196897 if( zPath==0 ) return;
196898 if( flags & JSON_ABPATH0x03 ){
196899 if( zPath[0]!='$' ){
196900 /* The -> and ->> operators accept abbreviated PATH arguments. This
196901 ** is mostly for compatibility with PostgreSQL, but also for
196902 ** convenience.
196903 **
196904 ** NUMBER ==> $[NUMBER] // PG compatible
196905 ** LABEL ==> $.LABEL // PG compatible
196906 ** [NUMBER] ==> $[NUMBER] // Not PG. Purely for convenience
196907 */
196908 jsonInit(&jx, ctx);
196909 if( sqlite3Isdigit(zPath[0])(sqlite3CtypeMap[(unsigned char)(zPath[0])]&0x04) ){
196910 jsonAppendRaw(&jx, "$[", 2);
196911 jsonAppendRaw(&jx, zPath, (int)strlen(zPath));
196912 jsonAppendRaw(&jx, "]", 2);
196913 }else{
196914 jsonAppendRaw(&jx, "$.", 1 + (zPath[0]!='['));
196915 jsonAppendRaw(&jx, zPath, (int)strlen(zPath));
196916 jsonAppendChar(&jx, 0);
196917 }
196918 pNode = jx.bErr ? 0 : jsonLookup(p, jx.zBuf, 0, ctx);
196919 jsonReset(&jx);
196920 }else{
196921 pNode = jsonLookup(p, zPath, 0, ctx);
196922 }
196923 if( pNode ){
196924 if( flags & JSON_JSON0x01 ){
196925 jsonReturnJson(pNode, ctx, 0);
196926 }else{
196927 jsonReturn(pNode, ctx, 0);
196928 sqlite3_result_subtype(ctx, 0);
196929 }
196930 }
196931 }else{
196932 pNode = jsonLookup(p, zPath, 0, ctx);
196933 if( p->nErr==0 && pNode ) jsonReturn(pNode, ctx, 0);
196934 }
196935 }else{
196936 /* Two or more PATH arguments results in a JSON array with each
196937 ** element of the array being the value selected by one of the PATHs */
196938 int i;
196939 jsonInit(&jx, ctx);
196940 jsonAppendChar(&jx, '[');
196941 for(i=1; i<argc; i++){
196942 zPath = (const char*)sqlite3_value_text(argv[i]);
196943 pNode = jsonLookup(p, zPath, 0, ctx);
196944 if( p->nErr ) break;
196945 jsonAppendSeparator(&jx);
196946 if( pNode ){
196947 jsonRenderNode(pNode, &jx, 0);
196948 }else{
196949 jsonAppendRaw(&jx, "null", 4);
196950 }
196951 }
196952 if( i==argc ){
196953 jsonAppendChar(&jx, ']');
196954 jsonResult(&jx);
196955 sqlite3_result_subtype(ctx, JSON_SUBTYPE74);
196956 }
196957 jsonReset(&jx);
196958 }
196959}
196960
196961/* This is the RFC 7396 MergePatch algorithm.
196962*/
196963static JsonNode *jsonMergePatch(
196964 JsonParse *pParse, /* The JSON parser that contains the TARGET */
196965 u32 iTarget, /* Node of the TARGET in pParse */
196966 JsonNode *pPatch /* The PATCH */
196967){
196968 u32 i, j;
196969 u32 iRoot;
196970 JsonNode *pTarget;
196971 if( pPatch->eType!=JSON_OBJECT7 ){
196972 return pPatch;
196973 }
196974 assert( iTarget<pParse->nNode )((void) (0));
196975 pTarget = &pParse->aNode[iTarget];
196976 assert( (pPatch->jnFlags & JNODE_APPEND)==0 )((void) (0));
196977 if( pTarget->eType!=JSON_OBJECT7 ){
196978 jsonRemoveAllNulls(pPatch);
196979 return pPatch;
196980 }
196981 iRoot = iTarget;
196982 for(i=1; i<pPatch->n; i += jsonNodeSize(&pPatch[i+1])+1){
196983 u32 nKey;
196984 const char *zKey;
196985 assert( pPatch[i].eType==JSON_STRING )((void) (0));
196986 assert( pPatch[i].jnFlags & JNODE_LABEL )((void) (0));
196987 assert( pPatch[i].eU==1 )((void) (0));
196988 nKey = pPatch[i].n;
196989 zKey = pPatch[i].u.zJContent;
196990 assert( (pPatch[i].jnFlags & JNODE_RAW)==0 )((void) (0));
196991 for(j=1; j<pTarget->n; j += jsonNodeSize(&pTarget[j+1])+1 ){
196992 assert( pTarget[j].eType==JSON_STRING )((void) (0));
196993 assert( pTarget[j].jnFlags & JNODE_LABEL )((void) (0));
196994 assert( (pPatch[i].jnFlags & JNODE_RAW)==0 )((void) (0));
196995 if( pTarget[j].n==nKey && strncmp(pTarget[j].u.zJContent,zKey,nKey)==0 ){
196996 if( pTarget[j+1].jnFlags & (JNODE_REMOVE0x04|JNODE_PATCH0x10) ) break;
196997 if( pPatch[i+1].eType==JSON_NULL0 ){
196998 pTarget[j+1].jnFlags |= JNODE_REMOVE0x04;
196999 }else{
197000 JsonNode *pNew = jsonMergePatch(pParse, iTarget+j+1, &pPatch[i+1]);
197001 if( pNew==0 ) return 0;
197002 pTarget = &pParse->aNode[iTarget];
197003 if( pNew!=&pTarget[j+1] ){
197004 assert( pTarget[j+1].eU==0((void) (0))
197005 || pTarget[j+1].eU==1((void) (0))
197006 || pTarget[j+1].eU==2 )((void) (0));
197007 testcase( pTarget[j+1].eU==1 );
197008 testcase( pTarget[j+1].eU==2 );
197009 VVA( pTarget[j+1].eU = 5 );
197010 pTarget[j+1].u.pPatch = pNew;
197011 pTarget[j+1].jnFlags |= JNODE_PATCH0x10;
197012 }
197013 }
197014 break;
197015 }
197016 }
197017 if( j>=pTarget->n && pPatch[i+1].eType!=JSON_NULL0 ){
197018 int iStart, iPatch;
197019 iStart = jsonParseAddNode(pParse, JSON_OBJECT7, 2, 0);
197020 jsonParseAddNode(pParse, JSON_STRING5, nKey, zKey);
197021 iPatch = jsonParseAddNode(pParse, JSON_TRUE1, 0, 0);
197022 if( pParse->oom ) return 0;
197023 jsonRemoveAllNulls(pPatch);
197024 pTarget = &pParse->aNode[iTarget];
197025 assert( pParse->aNode[iRoot].eU==0 || pParse->aNode[iRoot].eU==2 )((void) (0));
197026 testcase( pParse->aNode[iRoot].eU==2 );
197027 pParse->aNode[iRoot].jnFlags |= JNODE_APPEND0x20;
197028 VVA( pParse->aNode[iRoot].eU = 2 );
197029 pParse->aNode[iRoot].u.iAppend = iStart - iRoot;
197030 iRoot = iStart;
197031 assert( pParse->aNode[iPatch].eU==0 )((void) (0));
197032 VVA( pParse->aNode[iPatch].eU = 5 );
197033 pParse->aNode[iPatch].jnFlags |= JNODE_PATCH0x10;
197034 pParse->aNode[iPatch].u.pPatch = &pPatch[i+1];
197035 }
197036 }
197037 return pTarget;
197038}
197039
197040/*
197041** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON
197042** object that is the result of running the RFC 7396 MergePatch() algorithm
197043** on the two arguments.
197044*/
197045static void jsonPatchFunc(
197046 sqlite3_context *ctx,
197047 int argc,
197048 sqlite3_value **argv
197049){
197050 JsonParse x; /* The JSON that is being patched */
197051 JsonParse y; /* The patch */
197052 JsonNode *pResult; /* The result of the merge */
197053
197054 UNUSED_PARAMETER(argc)(void)(argc);
197055 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
197056 if( jsonParse(&y, ctx, (const char*)sqlite3_value_text(argv[1])) ){
197057 jsonParseReset(&x);
197058 return;
197059 }
197060 pResult = jsonMergePatch(&x, 0, y.aNode);
197061 assert( pResult!=0 || x.oom )((void) (0));
197062 if( pResult ){
197063 jsonReturnJson(pResult, ctx, 0);
197064 }else{
197065 sqlite3_result_error_nomem(ctx);
197066 }
197067 jsonParseReset(&x);
197068 jsonParseReset(&y);
197069}
197070
197071
197072/*
197073** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
197074** object that contains all name/value given in arguments. Or if any name
197075** is not a string or if any value is a BLOB, throw an error.
197076*/
197077static void jsonObjectFunc(
197078 sqlite3_context *ctx,
197079 int argc,
197080 sqlite3_value **argv
197081){
197082 int i;
197083 JsonString jx;
197084 const char *z;
197085 u32 n;
197086
197087 if( argc&1 ){
197088 sqlite3_result_error(ctx, "json_object() requires an even number "
197089 "of arguments", -1);
197090 return;
197091 }
197092 jsonInit(&jx, ctx);
197093 jsonAppendChar(&jx, '{');
197094 for(i=0; i<argc; i+=2){
197095 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT3 ){
197096 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
197097 jsonReset(&jx);
197098 return;
197099 }
197100 jsonAppendSeparator(&jx);
197101 z = (const char*)sqlite3_value_text(argv[i]);
197102 n = (u32)sqlite3_value_bytes(argv[i]);
197103 jsonAppendString(&jx, z, n);
197104 jsonAppendChar(&jx, ':');
197105 jsonAppendValue(&jx, argv[i+1]);
197106 }
197107 jsonAppendChar(&jx, '}');
197108 jsonResult(&jx);
197109 sqlite3_result_subtype(ctx, JSON_SUBTYPE74);
197110}
197111
197112
197113/*
197114** json_remove(JSON, PATH, ...)
197115**
197116** Remove the named elements from JSON and return the result. malformed
197117** JSON or PATH arguments result in an error.
197118*/
197119static void jsonRemoveFunc(
197120 sqlite3_context *ctx,
197121 int argc,
197122 sqlite3_value **argv
197123){
197124 JsonParse x; /* The parse */
197125 JsonNode *pNode;
197126 const char *zPath;
197127 u32 i;
197128
197129 if( argc<1 ) return;
197130 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
197131 assert( x.nNode )((void) (0));
197132 for(i=1; i<(u32)argc; i++){
197133 zPath = (const char*)sqlite3_value_text(argv[i]);
197134 if( zPath==0 ) goto remove_done;
197135 pNode = jsonLookup(&x, zPath, 0, ctx);
197136 if( x.nErr ) goto remove_done;
197137 if( pNode ) pNode->jnFlags |= JNODE_REMOVE0x04;
197138 }
197139 if( (x.aNode[0].jnFlags & JNODE_REMOVE0x04)==0 ){
197140 jsonReturnJson(x.aNode, ctx, 0);
197141 }
197142remove_done:
197143 jsonParseReset(&x);
197144}
197145
197146/*
197147** json_replace(JSON, PATH, VALUE, ...)
197148**
197149** Replace the value at PATH with VALUE. If PATH does not already exist,
197150** this routine is a no-op. If JSON or PATH is malformed, throw an error.
197151*/
197152static void jsonReplaceFunc(
197153 sqlite3_context *ctx,
197154 int argc,
197155 sqlite3_value **argv
197156){
197157 JsonParse x; /* The parse */
197158 JsonNode *pNode;
197159 const char *zPath;
197160 u32 i;
197161
197162 if( argc<1 ) return;
197163 if( (argc&1)==0 ) {
197164 jsonWrongNumArgs(ctx, "replace");
197165 return;
197166 }
197167 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
197168 assert( x.nNode )((void) (0));
197169 for(i=1; i<(u32)argc; i+=2){
197170 zPath = (const char*)sqlite3_value_text(argv[i]);
197171 pNode = jsonLookup(&x, zPath, 0, ctx);
197172 if( x.nErr ) goto replace_err;
197173 if( pNode ){
197174 assert( pNode->eU==0 || pNode->eU==1 || pNode->eU==4 )((void) (0));
197175 testcase( pNode->eU!=0 && pNode->eU!=1 );
197176 pNode->jnFlags |= (u8)JNODE_REPLACE0x08;
197177 VVA( pNode->eU = 4 );
197178 pNode->u.iReplace = i + 1;
197179 }
197180 }
197181 if( x.aNode[0].jnFlags & JNODE_REPLACE0x08 ){
197182 assert( x.aNode[0].eU==4 )((void) (0));
197183 sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
197184 }else{
197185 jsonReturnJson(x.aNode, ctx, argv);
197186 }
197187replace_err:
197188 jsonParseReset(&x);
197189}
197190
197191
197192/*
197193** json_set(JSON, PATH, VALUE, ...)
197194**
197195** Set the value at PATH to VALUE. Create the PATH if it does not already
197196** exist. Overwrite existing values that do exist.
197197** If JSON or PATH is malformed, throw an error.
197198**
197199** json_insert(JSON, PATH, VALUE, ...)
197200**
197201** Create PATH and initialize it to VALUE. If PATH already exists, this
197202** routine is a no-op. If JSON or PATH is malformed, throw an error.
197203*/
197204static void jsonSetFunc(
197205 sqlite3_context *ctx,
197206 int argc,
197207 sqlite3_value **argv
197208){
197209 JsonParse x; /* The parse */
197210 JsonNode *pNode;
197211 const char *zPath;
197212 u32 i;
197213 int bApnd;
197214 int bIsSet = sqlite3_user_data(ctx)!=0;
197215
197216 if( argc<1 ) return;
197217 if( (argc&1)==0 ) {
197218 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
197219 return;
197220 }
197221 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
197222 assert( x.nNode )((void) (0));
197223 for(i=1; i<(u32)argc; i+=2){
197224 zPath = (const char*)sqlite3_value_text(argv[i]);
197225 bApnd = 0;
197226 pNode = jsonLookup(&x, zPath, &bApnd, ctx);
197227 if( x.oom ){
197228 sqlite3_result_error_nomem(ctx);
197229 goto jsonSetDone;
197230 }else if( x.nErr ){
197231 goto jsonSetDone;
197232 }else if( pNode && (bApnd || bIsSet) ){
197233 testcase( pNode->eU!=0 && pNode->eU!=1 );
197234 assert( pNode->eU!=3 && pNode->eU!=5 )((void) (0));
197235 VVA( pNode->eU = 4 );
197236 pNode->jnFlags |= (u8)JNODE_REPLACE0x08;
197237 pNode->u.iReplace = i + 1;
197238 }
197239 }
197240 if( x.aNode[0].jnFlags & JNODE_REPLACE0x08 ){
197241 assert( x.aNode[0].eU==4 )((void) (0));
197242 sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
197243 }else{
197244 jsonReturnJson(x.aNode, ctx, argv);
197245 }
197246jsonSetDone:
197247 jsonParseReset(&x);
197248}
197249
197250/*
197251** json_type(JSON)
197252** json_type(JSON, PATH)
197253**
197254** Return the top-level "type" of a JSON string. json_type() raises an
197255** error if either the JSON or PATH inputs are not well-formed.
197256*/
197257static void jsonTypeFunc(
197258 sqlite3_context *ctx,
197259 int argc,
197260 sqlite3_value **argv
197261){
197262 JsonParse *p; /* The parse */
197263 const char *zPath;
197264 JsonNode *pNode;
197265
197266 p = jsonParseCached(ctx, argv, ctx);
197267 if( p==0 ) return;
197268 if( argc==2 ){
197269 zPath = (const char*)sqlite3_value_text(argv[1]);
197270 pNode = jsonLookup(p, zPath, 0, ctx);
197271 }else{
197272 pNode = p->aNode;
197273 }
197274 if( pNode ){
197275 sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC((sqlite3_destructor_type)0));
197276 }
197277}
197278
197279/*
197280** json_valid(JSON)
197281**
197282** Return 1 if JSON is a well-formed JSON string according to RFC-7159.
197283** Return 0 otherwise.
197284*/
197285static void jsonValidFunc(
197286 sqlite3_context *ctx,
197287 int argc,
197288 sqlite3_value **argv
197289){
197290 JsonParse *p; /* The parse */
197291 UNUSED_PARAMETER(argc)(void)(argc);
197292 p = jsonParseCached(ctx, argv, 0);
197293 sqlite3_result_int(ctx, p!=0);
197294}
197295
197296
197297/****************************************************************************
197298** Aggregate SQL function implementations
197299****************************************************************************/
197300/*
197301** json_group_array(VALUE)
197302**
197303** Return a JSON array composed of all values in the aggregate.
197304*/
197305static void jsonArrayStep(
197306 sqlite3_context *ctx,
197307 int argc,
197308 sqlite3_value **argv
197309){
197310 JsonString *pStr;
197311 UNUSED_PARAMETER(argc)(void)(argc);
197312 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
197313 if( pStr ){
197314 if( pStr->zBuf==0 ){
197315 jsonInit(pStr, ctx);
197316 jsonAppendChar(pStr, '[');
197317 }else if( pStr->nUsed>1 ){
197318 jsonAppendChar(pStr, ',');
197319 }
197320 pStr->pCtx = ctx;
197321 jsonAppendValue(pStr, argv[0]);
197322 }
197323}
197324static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
197325 JsonString *pStr;
197326 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
197327 if( pStr ){
197328 pStr->pCtx = ctx;
197329 jsonAppendChar(pStr, ']');
197330 if( pStr->bErr ){
197331 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
197332 assert( pStr->bStatic )((void) (0));
197333 }else if( isFinal ){
197334 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
197335 pStr->bStatic ? SQLITE_TRANSIENT((sqlite3_destructor_type)-1) : sqlite3_free);
197336 pStr->bStatic = 1;
197337 }else{
197338 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
197339 pStr->nUsed--;
197340 }
197341 }else{
197342 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC((sqlite3_destructor_type)0));
197343 }
197344 sqlite3_result_subtype(ctx, JSON_SUBTYPE74);
197345}
197346static void jsonArrayValue(sqlite3_context *ctx){
197347 jsonArrayCompute(ctx, 0);
197348}
197349static void jsonArrayFinal(sqlite3_context *ctx){
197350 jsonArrayCompute(ctx, 1);
197351}
197352
197353#ifndef SQLITE_OMIT_WINDOWFUNC
197354/*
197355** This method works for both json_group_array() and json_group_object().
197356** It works by removing the first element of the group by searching forward
197357** to the first comma (",") that is not within a string and deleting all
197358** text through that comma.
197359*/
197360static void jsonGroupInverse(
197361 sqlite3_context *ctx,
197362 int argc,
197363 sqlite3_value **argv
197364){
197365 unsigned int i;
197366 int inStr = 0;
197367 int nNest = 0;
197368 char *z;
197369 char c;
197370 JsonString *pStr;
197371 UNUSED_PARAMETER(argc)(void)(argc);
197372 UNUSED_PARAMETER(argv)(void)(argv);
197373 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
197374#ifdef NEVER
197375 /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
197376 ** always have been called to initalize it */
197377 if( NEVER(!pStr)(!pStr) ) return;
197378#endif
197379 z = pStr->zBuf;
197380 for(i=1; i<pStr->nUsed && ((c = z[i])!=',' || inStr || nNest); i++){
197381 if( c=='"' ){
197382 inStr = !inStr;
197383 }else if( c=='\\' ){
197384 i++;
197385 }else if( !inStr ){
197386 if( c=='{' || c=='[' ) nNest++;
197387 if( c=='}' || c==']' ) nNest--;
197388 }
197389 }
197390 if( i<pStr->nUsed ){
197391 pStr->nUsed -= i;
197392 memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1);
197393 z[pStr->nUsed] = 0;
197394 }else{
197395 pStr->nUsed = 1;
197396 }
197397}
197398#else
197399# define jsonGroupInverse 0
197400#endif
197401
197402
197403/*
197404** json_group_obj(NAME,VALUE)
197405**
197406** Return a JSON object composed of all names and values in the aggregate.
197407*/
197408static void jsonObjectStep(
197409 sqlite3_context *ctx,
197410 int argc,
197411 sqlite3_value **argv
197412){
197413 JsonString *pStr;
197414 const char *z;
197415 u32 n;
197416 UNUSED_PARAMETER(argc)(void)(argc);
197417 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
197418 if( pStr ){
197419 if( pStr->zBuf==0 ){
197420 jsonInit(pStr, ctx);
197421 jsonAppendChar(pStr, '{');
197422 }else if( pStr->nUsed>1 ){
197423 jsonAppendChar(pStr, ',');
197424 }
197425 pStr->pCtx = ctx;
197426 z = (const char*)sqlite3_value_text(argv[0]);
197427 n = (u32)sqlite3_value_bytes(argv[0]);
197428 jsonAppendString(pStr, z, n);
197429 jsonAppendChar(pStr, ':');
197430 jsonAppendValue(pStr, argv[1]);
197431 }
197432}
197433static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
197434 JsonString *pStr;
197435 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
197436 if( pStr ){
197437 jsonAppendChar(pStr, '}');
197438 if( pStr->bErr ){
197439 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
197440 assert( pStr->bStatic )((void) (0));
197441 }else if( isFinal ){
197442 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
197443 pStr->bStatic ? SQLITE_TRANSIENT((sqlite3_destructor_type)-1) : sqlite3_free);
197444 pStr->bStatic = 1;
197445 }else{
197446 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
197447 pStr->nUsed--;
197448 }
197449 }else{
197450 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC((sqlite3_destructor_type)0));
197451 }
197452 sqlite3_result_subtype(ctx, JSON_SUBTYPE74);
197453}
197454static void jsonObjectValue(sqlite3_context *ctx){
197455 jsonObjectCompute(ctx, 0);
197456}
197457static void jsonObjectFinal(sqlite3_context *ctx){
197458 jsonObjectCompute(ctx, 1);
197459}
197460
197461
197462
197463#ifndef SQLITE_OMIT_VIRTUALTABLE
197464/****************************************************************************
197465** The json_each virtual table
197466****************************************************************************/
197467typedef struct JsonEachCursor JsonEachCursor;
197468struct JsonEachCursor {
197469 sqlite3_vtab_cursor base; /* Base class - must be first */
197470 u32 iRowid; /* The rowid */
197471 u32 iBegin; /* The first node of the scan */
197472 u32 i; /* Index in sParse.aNode[] of current row */
197473 u32 iEnd; /* EOF when i equals or exceeds this value */
197474 u8 eType; /* Type of top-level element */
197475 u8 bRecursive; /* True for json_tree(). False for json_each() */
197476 char *zJson; /* Input JSON */
197477 char *zRoot; /* Path by which to filter zJson */
197478 JsonParse sParse; /* Parse of the input JSON */
197479};
197480
197481/* Constructor for the json_each virtual table */
197482static int jsonEachConnect(
197483 sqlite3 *db,
197484 void *pAux,
197485 int argc, const char *const*argv,
197486 sqlite3_vtab **ppVtab,
197487 char **pzErr
197488){
197489 sqlite3_vtab *pNew;
197490 int rc;
197491
197492/* Column numbers */
197493#define JEACH_KEY0 0
197494#define JEACH_VALUE1 1
197495#define JEACH_TYPE2 2
197496#define JEACH_ATOM3 3
197497#define JEACH_ID4 4
197498#define JEACH_PARENT5 5
197499#define JEACH_FULLKEY6 6
197500#define JEACH_PATH7 7
197501/* The xBestIndex method assumes that the JSON and ROOT columns are
197502** the last two columns in the table. Should this ever changes, be
197503** sure to update the xBestIndex method. */
197504#define JEACH_JSON8 8
197505#define JEACH_ROOT9 9
197506
197507 UNUSED_PARAMETER(pzErr)(void)(pzErr);
197508 UNUSED_PARAMETER(argv)(void)(argv);
197509 UNUSED_PARAMETER(argc)(void)(argc);
197510 UNUSED_PARAMETER(pAux)(void)(pAux);
197511 rc = sqlite3_declare_vtab(db,
197512 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
197513 "json HIDDEN,root HIDDEN)");
197514 if( rc==SQLITE_OK0 ){
197515 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
197516 if( pNew==0 ) return SQLITE_NOMEM7;
197517 memset(pNew, 0, sizeof(*pNew));
197518 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS2);
197519 }
197520 return rc;
197521}
197522
197523/* destructor for json_each virtual table */
197524static int jsonEachDisconnect(sqlite3_vtab *pVtab){
197525 sqlite3_free(pVtab);
197526 return SQLITE_OK0;
197527}
197528
197529/* constructor for a JsonEachCursor object for json_each(). */
197530static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
197531 JsonEachCursor *pCur;
197532
197533 UNUSED_PARAMETER(p)(void)(p);
197534 pCur = sqlite3_malloc( sizeof(*pCur) );
197535 if( pCur==0 ) return SQLITE_NOMEM7;
197536 memset(pCur, 0, sizeof(*pCur));
197537 *ppCursor = &pCur->base;
197538 return SQLITE_OK0;
197539}
197540
197541/* constructor for a JsonEachCursor object for json_tree(). */
197542static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
197543 int rc = jsonEachOpenEach(p, ppCursor);
197544 if( rc==SQLITE_OK0 ){
197545 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
197546 pCur->bRecursive = 1;
197547 }
197548 return rc;
197549}
197550
197551/* Reset a JsonEachCursor back to its original state. Free any memory
197552** held. */
197553static void jsonEachCursorReset(JsonEachCursor *p){
197554 sqlite3_free(p->zJson);
197555 sqlite3_free(p->zRoot);
197556 jsonParseReset(&p->sParse);
197557 p->iRowid = 0;
197558 p->i = 0;
197559 p->iEnd = 0;
197560 p->eType = 0;
197561 p->zJson = 0;
197562 p->zRoot = 0;
197563}
197564
197565/* Destructor for a jsonEachCursor object */
197566static int jsonEachClose(sqlite3_vtab_cursor *cur){
197567 JsonEachCursor *p = (JsonEachCursor*)cur;
197568 jsonEachCursorReset(p);
197569 sqlite3_free(cur);
197570 return SQLITE_OK0;
197571}
197572
197573/* Return TRUE if the jsonEachCursor object has been advanced off the end
197574** of the JSON object */
197575static int jsonEachEof(sqlite3_vtab_cursor *cur){
197576 JsonEachCursor *p = (JsonEachCursor*)cur;
197577 return p->i >= p->iEnd;
197578}
197579
197580/* Advance the cursor to the next element for json_tree() */
197581static int jsonEachNext(sqlite3_vtab_cursor *cur){
197582 JsonEachCursor *p = (JsonEachCursor*)cur;
197583 if( p->bRecursive ){
197584 if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL0x40 ) p->i++;
197585 p->i++;
197586 p->iRowid++;
197587 if( p->i<p->iEnd ){
197588 u32 iUp = p->sParse.aUp[p->i];
197589 JsonNode *pUp = &p->sParse.aNode[iUp];
197590 p->eType = pUp->eType;
197591 if( pUp->eType==JSON_ARRAY6 ){
197592 assert( pUp->eU==0 || pUp->eU==3 )((void) (0));
197593 testcase( pUp->eU==3 );
197594 VVA( pUp->eU = 3 );
197595 if( iUp==p->i-1 ){
197596 pUp->u.iKey = 0;
197597 }else{
197598 pUp->u.iKey++;
197599 }
197600 }
197601 }
197602 }else{
197603 switch( p->eType ){
197604 case JSON_ARRAY6: {
197605 p->i += jsonNodeSize(&p->sParse.aNode[p->i]);
197606 p->iRowid++;
197607 break;
197608 }
197609 case JSON_OBJECT7: {
197610 p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]);
197611 p->iRowid++;
197612 break;
197613 }
197614 default: {
197615 p->i = p->iEnd;
197616 break;
197617 }
197618 }
197619 }
197620 return SQLITE_OK0;
197621}
197622
197623/* Append an object label to the JSON Path being constructed
197624** in pStr.
197625*/
197626static void jsonAppendObjectPathElement(
197627 JsonString *pStr,
197628 JsonNode *pNode
197629){
197630 int jj, nn;
197631 const char *z;
197632 assert( pNode->eType==JSON_STRING )((void) (0));
197633 assert( pNode->jnFlags & JNODE_LABEL )((void) (0));
197634 assert( pNode->eU==1 )((void) (0));
197635 z = pNode->u.zJContent;
197636 nn = pNode->n;
197637 assert( nn>=2 )((void) (0));
197638 assert( z[0]=='"' )((void) (0));
197639 assert( z[nn-1]=='"' )((void) (0));
197640 if( nn>2 && sqlite3Isalpha(z[1])(sqlite3CtypeMap[(unsigned char)(z[1])]&0x02) ){
197641 for(jj=2; jj<nn-1 && sqlite3Isalnum(z[jj])(sqlite3CtypeMap[(unsigned char)(z[jj])]&0x06); jj++){}
197642 if( jj==nn-1 ){
197643 z++;
197644 nn -= 2;
197645 }
197646 }
197647 jsonPrintf(nn+2, pStr, ".%.*s", nn, z);
197648}
197649
197650/* Append the name of the path for element i to pStr
197651*/
197652static void jsonEachComputePath(
197653 JsonEachCursor *p, /* The cursor */
197654 JsonString *pStr, /* Write the path here */
197655 u32 i /* Path to this element */
197656){
197657 JsonNode *pNode, *pUp;
197658 u32 iUp;
197659 if( i==0 ){
197660 jsonAppendChar(pStr, '$');
197661 return;
197662 }
197663 iUp = p->sParse.aUp[i];
197664 jsonEachComputePath(p, pStr, iUp);
197665 pNode = &p->sParse.aNode[i];
197666 pUp = &p->sParse.aNode[iUp];
197667 if( pUp->eType==JSON_ARRAY6 ){
197668 assert( pUp->eU==3 || (pUp->eU==0 && pUp->u.iKey==0) )((void) (0));
197669 testcase( pUp->eU==0 );
197670 jsonPrintf(30, pStr, "[%d]", pUp->u.iKey);
197671 }else{
197672 assert( pUp->eType==JSON_OBJECT )((void) (0));
197673 if( (pNode->jnFlags & JNODE_LABEL0x40)==0 ) pNode--;
197674 jsonAppendObjectPathElement(pStr, pNode);
197675 }
197676}
197677
197678/* Return the value of a column */
197679static int jsonEachColumn(
197680 sqlite3_vtab_cursor *cur, /* The cursor */
197681 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
197682 int i /* Which column to return */
197683){
197684 JsonEachCursor *p = (JsonEachCursor*)cur;
197685 JsonNode *pThis = &p->sParse.aNode[p->i];
197686 switch( i ){
197687 case JEACH_KEY0: {
197688 if( p->i==0 ) break;
197689 if( p->eType==JSON_OBJECT7 ){
197690 jsonReturn(pThis, ctx, 0);
197691 }else if( p->eType==JSON_ARRAY6 ){
197692 u32 iKey;
197693 if( p->bRecursive ){
197694 if( p->iRowid==0 ) break;
197695 assert( p->sParse.aNode[p->sParse.aUp[p->i]].eU==3 )((void) (0));
197696 iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey;
197697 }else{
197698 iKey = p->iRowid;
197699 }
197700 sqlite3_result_int64(ctx, (sqlite3_int64)iKey);
197701 }
197702 break;
197703 }
197704 case JEACH_VALUE1: {
197705 if( pThis->jnFlags & JNODE_LABEL0x40 ) pThis++;
197706 jsonReturn(pThis, ctx, 0);
197707 break;
197708 }
197709 case JEACH_TYPE2: {
197710 if( pThis->jnFlags & JNODE_LABEL0x40 ) pThis++;
197711 sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC((sqlite3_destructor_type)0));
197712 break;
197713 }
197714 case JEACH_ATOM3: {
197715 if( pThis->jnFlags & JNODE_LABEL0x40 ) pThis++;
197716 if( pThis->eType>=JSON_ARRAY6 ) break;
197717 jsonReturn(pThis, ctx, 0);
197718 break;
197719 }
197720 case JEACH_ID4: {
197721 sqlite3_result_int64(ctx,
197722 (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL0x40)!=0));
197723 break;
197724 }
197725 case JEACH_PARENT5: {
197726 if( p->i>p->iBegin && p->bRecursive ){
197727 sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]);
197728 }
197729 break;
197730 }
197731 case JEACH_FULLKEY6: {
197732 JsonString x;
197733 jsonInit(&x, ctx);
197734 if( p->bRecursive ){
197735 jsonEachComputePath(p, &x, p->i);
197736 }else{
197737 if( p->zRoot ){
197738 jsonAppendRaw(&x, p->zRoot, (int)strlen(p->zRoot));
197739 }else{
197740 jsonAppendChar(&x, '$');
197741 }
197742 if( p->eType==JSON_ARRAY6 ){
197743 jsonPrintf(30, &x, "[%d]", p->iRowid);
197744 }else if( p->eType==JSON_OBJECT7 ){
197745 jsonAppendObjectPathElement(&x, pThis);
197746 }
197747 }
197748 jsonResult(&x);
197749 break;
197750 }
197751 case JEACH_PATH7: {
197752 if( p->bRecursive ){
197753 JsonString x;
197754 jsonInit(&x, ctx);
197755 jsonEachComputePath(p, &x, p->sParse.aUp[p->i]);
197756 jsonResult(&x);
197757 break;
197758 }
197759 /* For json_each() path and root are the same so fall through
197760 ** into the root case */
197761 /* no break */ deliberate_fall_through
197762 }
197763 default: {
197764 const char *zRoot = p->zRoot;
197765 if( zRoot==0 ) zRoot = "$";
197766 sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
197767 break;
197768 }
197769 case JEACH_JSON8: {
197770 assert( i==JEACH_JSON )((void) (0));
197771 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
197772 break;
197773 }
197774 }
197775 return SQLITE_OK0;
197776}
197777
197778/* Return the current rowid value */
197779static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
197780 JsonEachCursor *p = (JsonEachCursor*)cur;
197781 *pRowid = p->iRowid;
197782 return SQLITE_OK0;
197783}
197784
197785/* The query strategy is to look for an equality constraint on the json
197786** column. Without such a constraint, the table cannot operate. idxNum is
197787** 1 if the constraint is found, 3 if the constraint and zRoot are found,
197788** and 0 otherwise.
197789*/
197790static int jsonEachBestIndex(
197791 sqlite3_vtab *tab,
197792 sqlite3_index_info *pIdxInfo
197793){
197794 int i; /* Loop counter or computed array index */
197795 int aIdx[2]; /* Index of constraints for JSON and ROOT */
197796 int unusableMask = 0; /* Mask of unusable JSON and ROOT constraints */
197797 int idxMask = 0; /* Mask of usable == constraints JSON and ROOT */
197798 const struct sqlite3_index_constraint *pConstraint;
197799
197800 /* This implementation assumes that JSON and ROOT are the last two
197801 ** columns in the table */
197802 assert( JEACH_ROOT == JEACH_JSON+1 )((void) (0));
197803 UNUSED_PARAMETER(tab)(void)(tab);
197804 aIdx[0] = aIdx[1] = -1;
197805 pConstraint = pIdxInfo->aConstraint;
197806 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
197807 int iCol;
197808 int iMask;
197809 if( pConstraint->iColumn < JEACH_JSON8 ) continue;
197810 iCol = pConstraint->iColumn - JEACH_JSON8;
197811 assert( iCol==0 || iCol==1 )((void) (0));
197812 testcase( iCol==0 );
197813 iMask = 1 << iCol;
197814 if( pConstraint->usable==0 ){
197815 unusableMask |= iMask;
197816 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ2 ){
197817 aIdx[iCol] = i;
197818 idxMask |= iMask;
197819 }
197820 }
197821 if( (unusableMask & ~idxMask)!=0 ){
197822 /* If there are any unusable constraints on JSON or ROOT, then reject
197823 ** this entire plan */
197824 return SQLITE_CONSTRAINT19;
197825 }
197826 if( aIdx[0]<0 ){
197827 /* No JSON input. Leave estimatedCost at the huge value that it was
197828 ** initialized to to discourage the query planner from selecting this
197829 ** plan. */
197830 pIdxInfo->idxNum = 0;
197831 }else{
197832 pIdxInfo->estimatedCost = 1.0;
197833 i = aIdx[0];
197834 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
197835 pIdxInfo->aConstraintUsage[i].omit = 1;
197836 if( aIdx[1]<0 ){
197837 pIdxInfo->idxNum = 1; /* Only JSON supplied. Plan 1 */
197838 }else{
197839 i = aIdx[1];
197840 pIdxInfo->aConstraintUsage[i].argvIndex = 2;
197841 pIdxInfo->aConstraintUsage[i].omit = 1;
197842 pIdxInfo->idxNum = 3; /* Both JSON and ROOT are supplied. Plan 3 */
197843 }
197844 }
197845 return SQLITE_OK0;
197846}
197847
197848/* Start a search on a new JSON string */
197849static int jsonEachFilter(
197850 sqlite3_vtab_cursor *cur,
197851 int idxNum, const char *idxStr,
197852 int argc, sqlite3_value **argv
197853){
197854 JsonEachCursor *p = (JsonEachCursor*)cur;
197855 const char *z;
197856 const char *zRoot = 0;
197857 sqlite3_int64 n;
197858
197859 UNUSED_PARAMETER(idxStr)(void)(idxStr);
197860 UNUSED_PARAMETER(argc)(void)(argc);
197861 jsonEachCursorReset(p);
197862 if( idxNum==0 ) return SQLITE_OK0;
197863 z = (const char*)sqlite3_value_text(argv[0]);
197864 if( z==0 ) return SQLITE_OK0;
197865 n = sqlite3_value_bytes(argv[0]);
197866 p->zJson = sqlite3_malloc64( n+1 );
197867 if( p->zJson==0 ) return SQLITE_NOMEM7;
197868 memcpy(p->zJson, z, (size_t)n+1);
197869 if( jsonParse(&p->sParse, 0, p->zJson) ){
197870 int rc = SQLITE_NOMEM7;
197871 if( p->sParse.oom==0 ){
197872 sqlite3_free(cur->pVtab->zErrMsg);
197873 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
197874 if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR1;
197875 }
197876 jsonEachCursorReset(p);
197877 return rc;
197878 }else if( p->bRecursive && jsonParseFindParents(&p->sParse) ){
197879 jsonEachCursorReset(p);
197880 return SQLITE_NOMEM7;
197881 }else{
197882 JsonNode *pNode = 0;
197883 if( idxNum==3 ){
197884 const char *zErr = 0;
197885 zRoot = (const char*)sqlite3_value_text(argv[1]);
197886 if( zRoot==0 ) return SQLITE_OK0;
197887 n = sqlite3_value_bytes(argv[1]);
197888 p->zRoot = sqlite3_malloc64( n+1 );
197889 if( p->zRoot==0 ) return SQLITE_NOMEM7;
197890 memcpy(p->zRoot, zRoot, (size_t)n+1);
197891 if( zRoot[0]!='$' ){
197892 zErr = zRoot;
197893 }else{
197894 pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr);
197895 }
197896 if( zErr ){
197897 sqlite3_free(cur->pVtab->zErrMsg);
197898 cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
197899 jsonEachCursorReset(p);
197900 return cur->pVtab->zErrMsg ? SQLITE_ERROR1 : SQLITE_NOMEM7;
197901 }else if( pNode==0 ){
197902 return SQLITE_OK0;
197903 }
197904 }else{
197905 pNode = p->sParse.aNode;
197906 }
197907 p->iBegin = p->i = (int)(pNode - p->sParse.aNode);
197908 p->eType = pNode->eType;
197909 if( p->eType>=JSON_ARRAY6 ){
197910 assert( pNode->eU==0 )((void) (0));
197911 VVA( pNode->eU = 3 );
197912 pNode->u.iKey = 0;
197913 p->iEnd = p->i + pNode->n + 1;
197914 if( p->bRecursive ){
197915 p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType;
197916 if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL0x40)!=0 ){
197917 p->i--;
197918 }
197919 }else{
197920 p->i++;
197921 }
197922 }else{
197923 p->iEnd = p->i+1;
197924 }
197925 }
197926 return SQLITE_OK0;
197927}
197928
197929/* The methods of the json_each virtual table */
197930static sqlite3_module jsonEachModule = {
197931 0, /* iVersion */
197932 0, /* xCreate */
197933 jsonEachConnect, /* xConnect */
197934 jsonEachBestIndex, /* xBestIndex */
197935 jsonEachDisconnect, /* xDisconnect */
197936 0, /* xDestroy */
197937 jsonEachOpenEach, /* xOpen - open a cursor */
197938 jsonEachClose, /* xClose - close a cursor */
197939 jsonEachFilter, /* xFilter - configure scan constraints */
197940 jsonEachNext, /* xNext - advance a cursor */
197941 jsonEachEof, /* xEof - check for end of scan */
197942 jsonEachColumn, /* xColumn - read data */
197943 jsonEachRowid, /* xRowid - read data */
197944 0, /* xUpdate */
197945 0, /* xBegin */
197946 0, /* xSync */
197947 0, /* xCommit */
197948 0, /* xRollback */
197949 0, /* xFindMethod */
197950 0, /* xRename */
197951 0, /* xSavepoint */
197952 0, /* xRelease */
197953 0, /* xRollbackTo */
197954 0 /* xShadowName */
197955};
197956
197957/* The methods of the json_tree virtual table. */
197958static sqlite3_module jsonTreeModule = {
197959 0, /* iVersion */
197960 0, /* xCreate */
197961 jsonEachConnect, /* xConnect */
197962 jsonEachBestIndex, /* xBestIndex */
197963 jsonEachDisconnect, /* xDisconnect */
197964 0, /* xDestroy */
197965 jsonEachOpenTree, /* xOpen - open a cursor */
197966 jsonEachClose, /* xClose - close a cursor */
197967 jsonEachFilter, /* xFilter - configure scan constraints */
197968 jsonEachNext, /* xNext - advance a cursor */
197969 jsonEachEof, /* xEof - check for end of scan */
197970 jsonEachColumn, /* xColumn - read data */
197971 jsonEachRowid, /* xRowid - read data */
197972 0, /* xUpdate */
197973 0, /* xBegin */
197974 0, /* xSync */
197975 0, /* xCommit */
197976 0, /* xRollback */
197977 0, /* xFindMethod */
197978 0, /* xRename */
197979 0, /* xSavepoint */
197980 0, /* xRelease */
197981 0, /* xRollbackTo */
197982 0 /* xShadowName */
197983};
197984#endif /* SQLITE_OMIT_VIRTUALTABLE */
197985#endif /* !defined(SQLITE_OMIT_JSON) */
197986
197987/*
197988** Register JSON functions.
197989*/
197990SQLITE_PRIVATEstatic void sqlite3RegisterJsonFunctions(void){
197991#ifndef SQLITE_OMIT_JSON
197992 static FuncDef aJsonFunc[] = {
197993 JFUNCTION(json, 1, 0, jsonRemoveFunc){1, 0x00800000|0x000000800|0x000200000| 0x0800|1, ((void*)(long
int)(0)), 0, jsonRemoveFunc, 0, 0, 0, "json", {0} }
,
197994 JFUNCTION(json_array, -1, 0, jsonArrayFunc){-1, 0x00800000|0x000000800|0x000200000| 0x0800|1, ((void*)(long
int)(0)), 0, jsonArrayFunc, 0, 0, 0, "json_array", {0} }
,
197995 JFUNCTION(json_array_length, 1, 0, jsonArrayLengthFunc){1, 0x00800000|0x000000800|0x000200000| 0x0800|1, ((void*)(long
int)(0)), 0, jsonArrayLengthFunc, 0, 0, 0, "json_array_length"
, {0} }
,
197996 JFUNCTION(json_array_length, 2, 0, jsonArrayLengthFunc){2, 0x00800000|0x000000800|0x000200000| 0x0800|1, ((void*)(long
int)(0)), 0, jsonArrayLengthFunc, 0, 0, 0, "json_array_length"
, {0} }
,
197997 JFUNCTION(json_extract, -1, 0, jsonExtractFunc){-1, 0x00800000|0x000000800|0x000200000| 0x0800|1, ((void*)(long
int)(0)), 0, jsonExtractFunc, 0, 0, 0, "json_extract", {0} }
,
197998 JFUNCTION(->, 2, JSON_JSON, jsonExtractFunc){2, 0x00800000|0x000000800|0x000200000| 0x0800|1, ((void*)(long
int)(0x01)), 0, jsonExtractFunc, 0, 0, 0, "->", {0} }
,
197999 JFUNCTION(->>, 2, JSON_SQL, jsonExtractFunc){2, 0x00800000|0x000000800|0x000200000| 0x0800|1, ((void*)(long
int)(0x02)), 0, jsonExtractFunc, 0, 0, 0, "->>", {0} }
,
198000 JFUNCTION(json_insert, -1, 0, jsonSetFunc){-1, 0x00800000|0x000000800|0x000200000| 0x0800|1, ((void*)(long
int)(0)), 0, jsonSetFunc, 0, 0, 0, "json_insert", {0} }
,
198001 JFUNCTION(json_object, -1, 0, jsonObjectFunc){-1, 0x00800000|0x000000800|0x000200000| 0x0800|1, ((void*)(long
int)(0)), 0, jsonObjectFunc, 0, 0, 0, "json_object", {0} }
,
198002 JFUNCTION(json_patch, 2, 0, jsonPatchFunc){2, 0x00800000|0x000000800|0x000200000| 0x0800|1, ((void*)(long
int)(0)), 0, jsonPatchFunc, 0, 0, 0, "json_patch", {0} }
,
198003 JFUNCTION(json_quote, 1, 0, jsonQuoteFunc){1, 0x00800000|0x000000800|0x000200000| 0x0800|1, ((void*)(long
int)(0)), 0, jsonQuoteFunc, 0, 0, 0, "json_quote", {0} }
,
198004 JFUNCTION(json_remove, -1, 0, jsonRemoveFunc){-1, 0x00800000|0x000000800|0x000200000| 0x0800|1, ((void*)(long
int)(0)), 0, jsonRemoveFunc, 0, 0, 0, "json_remove", {0} }
,
198005 JFUNCTION(json_replace, -1, 0, jsonReplaceFunc){-1, 0x00800000|0x000000800|0x000200000| 0x0800|1, ((void*)(long
int)(0)), 0, jsonReplaceFunc, 0, 0, 0, "json_replace", {0} }
,
198006 JFUNCTION(json_set, -1, JSON_ISSET, jsonSetFunc){-1, 0x00800000|0x000000800|0x000200000| 0x0800|1, ((void*)(long
int)(0x04)), 0, jsonSetFunc, 0, 0, 0, "json_set", {0} }
,
198007 JFUNCTION(json_type, 1, 0, jsonTypeFunc){1, 0x00800000|0x000000800|0x000200000| 0x0800|1, ((void*)(long
int)(0)), 0, jsonTypeFunc, 0, 0, 0, "json_type", {0} }
,
198008 JFUNCTION(json_type, 2, 0, jsonTypeFunc){2, 0x00800000|0x000000800|0x000200000| 0x0800|1, ((void*)(long
int)(0)), 0, jsonTypeFunc, 0, 0, 0, "json_type", {0} }
,
198009 JFUNCTION(json_valid, 1, 0, jsonValidFunc){1, 0x00800000|0x000000800|0x000200000| 0x0800|1, ((void*)(long
int)(0)), 0, jsonValidFunc, 0, 0, 0, "json_valid", {0} }
,
198010#if SQLITE_DEBUG
198011 JFUNCTION(json_parse, 1, 0, jsonParseFunc){1, 0x00800000|0x000000800|0x000200000| 0x0800|1, ((void*)(long
int)(0)), 0, jsonParseFunc, 0, 0, 0, "json_parse", {0} }
,
198012 JFUNCTION(json_test1, 1, 0, jsonTest1Func){1, 0x00800000|0x000000800|0x000200000| 0x0800|1, ((void*)(long
int)(0)), 0, jsonTest1Func, 0, 0, 0, "json_test1", {0} }
,
198013#endif
198014 WAGGREGATE(json_group_array, 1, 0, 0,{1, 0x00800000|1|(0*0x0020)|0x000100000|1|0x000000800|0x000200000
, ((void*)(long int)(0)), 0, jsonArrayStep,jsonArrayFinal,jsonArrayValue
,jsonGroupInverse,"json_group_array", {0}}
198015 jsonArrayStep, jsonArrayFinal, jsonArrayValue, jsonGroupInverse,{1, 0x00800000|1|(0*0x0020)|0x000100000|1|0x000000800|0x000200000
, ((void*)(long int)(0)), 0, jsonArrayStep,jsonArrayFinal,jsonArrayValue
,jsonGroupInverse,"json_group_array", {0}}
198016 SQLITE_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS){1, 0x00800000|1|(0*0x0020)|0x000100000|1|0x000000800|0x000200000
, ((void*)(long int)(0)), 0, jsonArrayStep,jsonArrayFinal,jsonArrayValue
,jsonGroupInverse,"json_group_array", {0}}
,
198017 WAGGREGATE(json_group_object, 2, 0, 0,{2, 0x00800000|1|(0*0x0020)|0x000100000|1|0x000000800|0x000200000
, ((void*)(long int)(0)), 0, jsonObjectStep,jsonObjectFinal,jsonObjectValue
,jsonGroupInverse,"json_group_object", {0}}
198018 jsonObjectStep, jsonObjectFinal, jsonObjectValue, jsonGroupInverse,{2, 0x00800000|1|(0*0x0020)|0x000100000|1|0x000000800|0x000200000
, ((void*)(long int)(0)), 0, jsonObjectStep,jsonObjectFinal,jsonObjectValue
,jsonGroupInverse,"json_group_object", {0}}
198019 SQLITE_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS){2, 0x00800000|1|(0*0x0020)|0x000100000|1|0x000000800|0x000200000
, ((void*)(long int)(0)), 0, jsonObjectStep,jsonObjectFinal,jsonObjectValue
,jsonGroupInverse,"json_group_object", {0}}
198020 };
198021 sqlite3InsertBuiltinFuncs(aJsonFunc, ArraySize(aJsonFunc)((int)(sizeof(aJsonFunc)/sizeof(aJsonFunc[0]))));
198022#endif
198023}
198024
198025#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
198026/*
198027** Register the JSON table-valued functions
198028*/
198029SQLITE_PRIVATEstatic int sqlite3JsonTableFunctions(sqlite3 *db){
198030 int rc = SQLITE_OK0;
198031 static const struct {
198032 const char *zName;
198033 sqlite3_module *pModule;
198034 } aMod[] = {
198035 { "json_each", &jsonEachModule },
198036 { "json_tree", &jsonTreeModule },
198037 };
198038 unsigned int i;
198039 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK0; i++){
198040 rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
198041 }
198042 return rc;
198043}
198044#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON) */
198045
198046/************** End of json.c ************************************************/
198047/************** Begin file rtree.c *******************************************/
198048/*
198049** 2001 September 15
198050**
198051** The author disclaims copyright to this source code. In place of
198052** a legal notice, here is a blessing:
198053**
198054** May you do good and not evil.
198055** May you find forgiveness for yourself and forgive others.
198056** May you share freely, never taking more than you give.
198057**
198058*************************************************************************
198059** This file contains code for implementations of the r-tree and r*-tree
198060** algorithms packaged as an SQLite virtual table module.
198061*/
198062
198063/*
198064** Database Format of R-Tree Tables
198065** --------------------------------
198066**
198067** The data structure for a single virtual r-tree table is stored in three
198068** native SQLite tables declared as follows. In each case, the '%' character
198069** in the table name is replaced with the user-supplied name of the r-tree
198070** table.
198071**
198072** CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB)
198073** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
198074** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
198075**
198076** The data for each node of the r-tree structure is stored in the %_node
198077** table. For each node that is not the root node of the r-tree, there is
198078** an entry in the %_parent table associating the node with its parent.
198079** And for each row of data in the table, there is an entry in the %_rowid
198080** table that maps from the entries rowid to the id of the node that it
198081** is stored on. If the r-tree contains auxiliary columns, those are stored
198082** on the end of the %_rowid table.
198083**
198084** The root node of an r-tree always exists, even if the r-tree table is
198085** empty. The nodeno of the root node is always 1. All other nodes in the
198086** table must be the same size as the root node. The content of each node
198087** is formatted as follows:
198088**
198089** 1. If the node is the root node (node 1), then the first 2 bytes
198090** of the node contain the tree depth as a big-endian integer.
198091** For non-root nodes, the first 2 bytes are left unused.
198092**
198093** 2. The next 2 bytes contain the number of entries currently
198094** stored in the node.
198095**
198096** 3. The remainder of the node contains the node entries. Each entry
198097** consists of a single 8-byte integer followed by an even number
198098** of 4-byte coordinates. For leaf nodes the integer is the rowid
198099** of a record. For internal nodes it is the node number of a
198100** child page.
198101*/
198102
198103#if !defined(SQLITE_CORE1) \
198104 || (defined(SQLITE_ENABLE_RTREE1) && !defined(SQLITE_OMIT_VIRTUALTABLE))
198105
198106#ifndef SQLITE_CORE1
198107/* #include "sqlite3ext.h" */
198108 SQLITE_EXTENSION_INIT1
198109#else
198110/* #include "sqlite3.h" */
198111#endif
198112SQLITE_PRIVATEstatic int sqlite3GetToken(const unsigned char*,int*); /* In the SQLite core */
198113
198114/*
198115** If building separately, we will need some setup that is normally
198116** found in sqliteInt.h
198117*/
198118#if !defined(SQLITE_AMALGAMATION1)
198119#include "sqlite3rtree.h"
198120typedef sqlite3_int64 i64;
198121typedef sqlite3_uint64 u64;
198122typedef unsigned char u8;
198123typedef unsigned short u16;
198124typedef unsigned int u32;
198125#if !defined(NDEBUG1) && !defined(SQLITE_DEBUG)
198126# define NDEBUG1 1
198127#endif
198128#if defined(NDEBUG1) && defined(SQLITE_DEBUG)
198129# undef NDEBUG1
198130#endif
198131#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
198132# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
198133#endif
198134#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
198135# define ALWAYS(X)(X) (1)
198136# define NEVER(X)(X) (0)
198137#elif !defined(NDEBUG1)
198138# define ALWAYS(X)(X) ((X)?1:(assert(0)((void) (0)),0))
198139# define NEVER(X)(X) ((X)?(assert(0)((void) (0)),1):0)
198140#else
198141# define ALWAYS(X)(X) (X)
198142# define NEVER(X)(X) (X)
198143#endif
198144#endif /* !defined(SQLITE_AMALGAMATION) */
198145
198146/* #include <string.h> */
198147/* #include <stdio.h> */
198148/* #include <assert.h> */
198149/* #include <stdlib.h> */
198150
198151/* The following macro is used to suppress compiler warnings.
198152*/
198153#ifndef UNUSED_PARAMETER
198154# define UNUSED_PARAMETER(x)(void)(x) (void)(x)
198155#endif
198156
198157typedef struct Rtree Rtree;
198158typedef struct RtreeCursor RtreeCursor;
198159typedef struct RtreeNode RtreeNode;
198160typedef struct RtreeCell RtreeCell;
198161typedef struct RtreeConstraint RtreeConstraint;
198162typedef struct RtreeMatchArg RtreeMatchArg;
198163typedef struct RtreeGeomCallback RtreeGeomCallback;
198164typedef union RtreeCoord RtreeCoord;
198165typedef struct RtreeSearchPoint RtreeSearchPoint;
198166
198167/* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
198168#define RTREE_MAX_DIMENSIONS5 5
198169
198170/* Maximum number of auxiliary columns */
198171#define RTREE_MAX_AUX_COLUMN100 100
198172
198173/* Size of hash table Rtree.aHash. This hash table is not expected to
198174** ever contain very many entries, so a fixed number of buckets is
198175** used.
198176*/
198177#define HASHSIZE97 97
198178
198179/* The xBestIndex method of this virtual table requires an estimate of
198180** the number of rows in the virtual table to calculate the costs of
198181** various strategies. If possible, this estimate is loaded from the
198182** sqlite_stat1 table (with RTREE_MIN_ROWEST as a hard-coded minimum).
198183** Otherwise, if no sqlite_stat1 entry is available, use
198184** RTREE_DEFAULT_ROWEST.
198185*/
198186#define RTREE_DEFAULT_ROWEST1048576 1048576
198187#define RTREE_MIN_ROWEST100 100
198188
198189/*
198190** An rtree virtual-table object.
198191*/
198192struct Rtree {
198193 sqlite3_vtab base; /* Base class. Must be first */
198194 sqlite3 *db; /* Host database connection */
198195 int iNodeSize; /* Size in bytes of each node in the node table */
198196 u8 nDim; /* Number of dimensions */
198197 u8 nDim2; /* Twice the number of dimensions */
198198 u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
198199 u8 nBytesPerCell; /* Bytes consumed per cell */
198200 u8 inWrTrans; /* True if inside write transaction */
198201 u8 nAux; /* # of auxiliary columns in %_rowid */
198202#ifdef SQLITE_ENABLE_GEOPOLY
198203 u8 nAuxNotNull; /* Number of initial not-null aux columns */
198204#endif
198205#ifdef SQLITE_DEBUG
198206 u8 bCorrupt; /* Shadow table corruption detected */
198207#endif
198208 int iDepth; /* Current depth of the r-tree structure */
198209 char *zDb; /* Name of database containing r-tree table */
198210 char *zName; /* Name of r-tree table */
198211 u32 nBusy; /* Current number of users of this structure */
198212 i64 nRowEst; /* Estimated number of rows in this table */
198213 u32 nCursor; /* Number of open cursors */
198214 u32 nNodeRef; /* Number RtreeNodes with positive nRef */
198215 char *zReadAuxSql; /* SQL for statement to read aux data */
198216
198217 /* List of nodes removed during a CondenseTree operation. List is
198218 ** linked together via the pointer normally used for hash chains -
198219 ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
198220 ** headed by the node (leaf nodes have RtreeNode.iNode==0).
198221 */
198222 RtreeNode *pDeleted;
198223 int iReinsertHeight; /* Height of sub-trees Reinsert() has run on */
198224
198225 /* Blob I/O on xxx_node */
198226 sqlite3_blob *pNodeBlob;
198227
198228 /* Statements to read/write/delete a record from xxx_node */
198229 sqlite3_stmt *pWriteNode;
198230 sqlite3_stmt *pDeleteNode;
198231
198232 /* Statements to read/write/delete a record from xxx_rowid */
198233 sqlite3_stmt *pReadRowid;
198234 sqlite3_stmt *pWriteRowid;
198235 sqlite3_stmt *pDeleteRowid;
198236
198237 /* Statements to read/write/delete a record from xxx_parent */
198238 sqlite3_stmt *pReadParent;
198239 sqlite3_stmt *pWriteParent;
198240 sqlite3_stmt *pDeleteParent;
198241
198242 /* Statement for writing to the "aux:" fields, if there are any */
198243 sqlite3_stmt *pWriteAux;
198244
198245 RtreeNode *aHash[HASHSIZE97]; /* Hash table of in-memory nodes. */
198246};
198247
198248/* Possible values for Rtree.eCoordType: */
198249#define RTREE_COORD_REAL320 0
198250#define RTREE_COORD_INT321 1
198251
198252/*
198253** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will
198254** only deal with integer coordinates. No floating point operations
198255** will be done.
198256*/
198257#ifdef SQLITE_RTREE_INT_ONLY
198258 typedef sqlite3_int64 RtreeDValue; /* High accuracy coordinate */
198259 typedef int RtreeValue; /* Low accuracy coordinate */
198260# define RTREE_ZERO0.0 0
198261#else
198262 typedef double RtreeDValue; /* High accuracy coordinate */
198263 typedef float RtreeValue; /* Low accuracy coordinate */
198264# define RTREE_ZERO0.0 0.0
198265#endif
198266
198267/*
198268** Set the Rtree.bCorrupt flag
198269*/
198270#ifdef SQLITE_DEBUG
198271# define RTREE_IS_CORRUPT(X) ((X)->bCorrupt = 1)
198272#else
198273# define RTREE_IS_CORRUPT(X)
198274#endif
198275
198276/*
198277** When doing a search of an r-tree, instances of the following structure
198278** record intermediate results from the tree walk.
198279**
198280** The id is always a node-id. For iLevel>=1 the id is the node-id of
198281** the node that the RtreeSearchPoint represents. When iLevel==0, however,
198282** the id is of the parent node and the cell that RtreeSearchPoint
198283** represents is the iCell-th entry in the parent node.
198284*/
198285struct RtreeSearchPoint {
198286 RtreeDValue rScore; /* The score for this node. Smallest goes first. */
198287 sqlite3_int64 id; /* Node ID */
198288 u8 iLevel; /* 0=entries. 1=leaf node. 2+ for higher */
198289 u8 eWithin; /* PARTLY_WITHIN or FULLY_WITHIN */
198290 u8 iCell; /* Cell index within the node */
198291};
198292
198293/*
198294** The minimum number of cells allowed for a node is a third of the
198295** maximum. In Gutman's notation:
198296**
198297** m = M/3
198298**
198299** If an R*-tree "Reinsert" operation is required, the same number of
198300** cells are removed from the overfull node and reinserted into the tree.
198301*/
198302#define RTREE_MINCELLS(p)((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
198303#define RTREE_REINSERT(p)((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3) RTREE_MINCELLS(p)((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
198304#define RTREE_MAXCELLS51 51
198305
198306/*
198307** The smallest possible node-size is (512-64)==448 bytes. And the largest
198308** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates).
198309** Therefore all non-root nodes must contain at least 3 entries. Since
198310** 3^40 is greater than 2^64, an r-tree structure always has a depth of
198311** 40 or less.
198312*/
198313#define RTREE_MAX_DEPTH40 40
198314
198315
198316/*
198317** Number of entries in the cursor RtreeNode cache. The first entry is
198318** used to cache the RtreeNode for RtreeCursor.sPoint. The remaining
198319** entries cache the RtreeNode for the first elements of the priority queue.
198320*/
198321#define RTREE_CACHE_SZ5 5
198322
198323/*
198324** An rtree cursor object.
198325*/
198326struct RtreeCursor {
198327 sqlite3_vtab_cursor base; /* Base class. Must be first */
198328 u8 atEOF; /* True if at end of search */
198329 u8 bPoint; /* True if sPoint is valid */
198330 u8 bAuxValid; /* True if pReadAux is valid */
198331 int iStrategy; /* Copy of idxNum search parameter */
198332 int nConstraint; /* Number of entries in aConstraint */
198333 RtreeConstraint *aConstraint; /* Search constraints. */
198334 int nPointAlloc; /* Number of slots allocated for aPoint[] */
198335 int nPoint; /* Number of slots used in aPoint[] */
198336 int mxLevel; /* iLevel value for root of the tree */
198337 RtreeSearchPoint *aPoint; /* Priority queue for search points */
198338 sqlite3_stmt *pReadAux; /* Statement to read aux-data */
198339 RtreeSearchPoint sPoint; /* Cached next search point */
198340 RtreeNode *aNode[RTREE_CACHE_SZ5]; /* Rtree node cache */
198341 u32 anQueue[RTREE_MAX_DEPTH40+1]; /* Number of queued entries by iLevel */
198342};
198343
198344/* Return the Rtree of a RtreeCursor */
198345#define RTREE_OF_CURSOR(X)((Rtree*)((X)->base.pVtab)) ((Rtree*)((X)->base.pVtab))
198346
198347/*
198348** A coordinate can be either a floating point number or a integer. All
198349** coordinates within a single R-Tree are always of the same time.
198350*/
198351union RtreeCoord {
198352 RtreeValue f; /* Floating point value */
198353 int i; /* Integer value */
198354 u32 u; /* Unsigned for byte-order conversions */
198355};
198356
198357/*
198358** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
198359** formatted as a RtreeDValue (double or int64). This macro assumes that local
198360** variable pRtree points to the Rtree structure associated with the
198361** RtreeCoord.
198362*/
198363#ifdef SQLITE_RTREE_INT_ONLY
198364# define DCOORD(coord)( (pRtree->eCoordType==0) ? ((double)coord.f) : ((double)coord
.i) )
((RtreeDValue)coord.i)
198365#else
198366# define DCOORD(coord)( (pRtree->eCoordType==0) ? ((double)coord.f) : ((double)coord
.i) )
( \
198367 (pRtree->eCoordType==RTREE_COORD_REAL320) ? \
198368 ((double)coord.f) : \
198369 ((double)coord.i) \
198370 )
198371#endif
198372
198373/*
198374** A search constraint.
198375*/
198376struct RtreeConstraint {
198377 int iCoord; /* Index of constrained coordinate */
198378 int op; /* Constraining operation */
198379 union {
198380 RtreeDValue rValue; /* Constraint value. */
198381 int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*);
198382 int (*xQueryFunc)(sqlite3_rtree_query_info*);
198383 } u;
198384 sqlite3_rtree_query_info *pInfo; /* xGeom and xQueryFunc argument */
198385};
198386
198387/* Possible values for RtreeConstraint.op */
198388#define RTREE_EQ0x41 0x41 /* A */
198389#define RTREE_LE0x42 0x42 /* B */
198390#define RTREE_LT0x43 0x43 /* C */
198391#define RTREE_GE0x44 0x44 /* D */
198392#define RTREE_GT0x45 0x45 /* E */
198393#define RTREE_MATCH0x46 0x46 /* F: Old-style sqlite3_rtree_geometry_callback() */
198394#define RTREE_QUERY0x47 0x47 /* G: New-style sqlite3_rtree_query_callback() */
198395
198396/* Special operators available only on cursors. Needs to be consecutive
198397** with the normal values above, but must be less than RTREE_MATCH. These
198398** are used in the cursor for contraints such as x=NULL (RTREE_FALSE) or
198399** x<'xyz' (RTREE_TRUE) */
198400#define RTREE_TRUE0x3f 0x3f /* ? */
198401#define RTREE_FALSE0x40 0x40 /* @ */
198402
198403/*
198404** An rtree structure node.
198405*/
198406struct RtreeNode {
198407 RtreeNode *pParent; /* Parent node */
198408 i64 iNode; /* The node number */
198409 int nRef; /* Number of references to this node */
198410 int isDirty; /* True if the node needs to be written to disk */
198411 u8 *zData; /* Content of the node, as should be on disk */
198412 RtreeNode *pNext; /* Next node in this hash collision chain */
198413};
198414
198415/* Return the number of cells in a node */
198416#define NCELL(pNode)readInt16(&(pNode)->zData[2]) readInt16(&(pNode)->zData[2])
198417
198418/*
198419** A single cell from a node, deserialized
198420*/
198421struct RtreeCell {
198422 i64 iRowid; /* Node or entry ID */
198423 RtreeCoord aCoord[RTREE_MAX_DIMENSIONS5*2]; /* Bounding box coordinates */
198424};
198425
198426
198427/*
198428** This object becomes the sqlite3_user_data() for the SQL functions
198429** that are created by sqlite3_rtree_geometry_callback() and
198430** sqlite3_rtree_query_callback() and which appear on the right of MATCH
198431** operators in order to constrain a search.
198432**
198433** xGeom and xQueryFunc are the callback functions. Exactly one of
198434** xGeom and xQueryFunc fields is non-NULL, depending on whether the
198435** SQL function was created using sqlite3_rtree_geometry_callback() or
198436** sqlite3_rtree_query_callback().
198437**
198438** This object is deleted automatically by the destructor mechanism in
198439** sqlite3_create_function_v2().
198440*/
198441struct RtreeGeomCallback {
198442 int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
198443 int (*xQueryFunc)(sqlite3_rtree_query_info*);
198444 void (*xDestructor)(void*);
198445 void *pContext;
198446};
198447
198448/*
198449** An instance of this structure (in the form of a BLOB) is returned by
198450** the SQL functions that sqlite3_rtree_geometry_callback() and
198451** sqlite3_rtree_query_callback() create, and is read as the right-hand
198452** operand to the MATCH operator of an R-Tree.
198453*/
198454struct RtreeMatchArg {
198455 u32 iSize; /* Size of this object */
198456 RtreeGeomCallback cb; /* Info about the callback functions */
198457 int nParam; /* Number of parameters to the SQL function */
198458 sqlite3_value **apSqlParam; /* Original SQL parameter values */
198459 RtreeDValue aParam[1]; /* Values for parameters to the SQL function */
198460};
198461
198462#ifndef MAX
198463# define MAX(x,y)((x)>(y)?(x):(y)) ((x) < (y) ? (y) : (x))
198464#endif
198465#ifndef MIN
198466# define MIN(x,y)((x)<(y)?(x):(y)) ((x) > (y) ? (y) : (x))
198467#endif
198468
198469/* What version of GCC is being used. 0 means GCC is not being used .
198470** Note that the GCC_VERSION macro will also be set correctly when using
198471** clang, since clang works hard to be gcc compatible. So the gcc
198472** optimizations will also work when compiling with clang.
198473*/
198474#ifndef GCC_VERSION(4*1000000+2*1000+1)
198475#if defined(__GNUC__4) && !defined(SQLITE_DISABLE_INTRINSIC)
198476# define GCC_VERSION(4*1000000+2*1000+1) (__GNUC__4*1000000+__GNUC_MINOR__2*1000+__GNUC_PATCHLEVEL__1)
198477#else
198478# define GCC_VERSION(4*1000000+2*1000+1) 0
198479#endif
198480#endif
198481
198482/* The testcase() macro should already be defined in the amalgamation. If
198483** it is not, make it a no-op.
198484*/
198485#ifndef SQLITE_AMALGAMATION1
198486# if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_DEBUG)
198487 unsigned int sqlite3RtreeTestcase = 0;
198488# define testcase(X) if( X ){ sqlite3RtreeTestcase += __LINE__198488; }
198489# else
198490# define testcase(X)
198491# endif
198492#endif
198493
198494/*
198495** Make sure that the compiler intrinsics we desire are enabled when
198496** compiling with an appropriate version of MSVC unless prevented by
198497** the SQLITE_DISABLE_INTRINSIC define.
198498*/
198499#if !defined(SQLITE_DISABLE_INTRINSIC)
198500# if defined(_MSC_VER) && _MSC_VER>=1400
198501# if !defined(_WIN32_WCE)
198502/* # include <intrin.h> */
198503# pragma intrinsic(_byteswap_ulong)
198504# pragma intrinsic(_byteswap_uint64)
198505# else
198506/* # include <cmnintrin.h> */
198507# endif
198508# endif
198509#endif
198510
198511/*
198512** Macros to determine whether the machine is big or little endian,
198513** and whether or not that determination is run-time or compile-time.
198514**
198515** For best performance, an attempt is made to guess at the byte-order
198516** using C-preprocessor macros. If that is unsuccessful, or if
198517** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
198518** at run-time.
198519*/
198520#ifndef SQLITE_BYTEORDER1234
198521#if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
198522 defined(__x86_641) || defined(__x86_64__1) || defined(_M_X64) || \
198523 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
198524 defined(__arm__)
198525# define SQLITE_BYTEORDER1234 1234
198526#elif defined(sparc) || defined(__ppc__)
198527# define SQLITE_BYTEORDER1234 4321
198528#else
198529# define SQLITE_BYTEORDER1234 0 /* 0 means "unknown at compile-time" */
198530#endif
198531#endif
198532
198533
198534/* What version of MSVC is being used. 0 means MSVC is not being used */
198535#ifndef MSVC_VERSION0
198536#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
198537# define MSVC_VERSION0 _MSC_VER
198538#else
198539# define MSVC_VERSION0 0
198540#endif
198541#endif
198542
198543/*
198544** Functions to deserialize a 16 bit integer, 32 bit real number and
198545** 64 bit integer. The deserialized value is returned.
198546*/
198547static int readInt16(u8 *p){
198548 return (p[0]<<8) + p[1];
198549}
198550static void readCoord(u8 *p, RtreeCoord *pCoord){
198551 assert( ((((char*)p) - (char*)0)&3)==0 )((void) (0)); /* p is always 4-byte aligned */
198552#if SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
198553 pCoord->u = _byteswap_ulong(*(u32*)p);
198554#elif SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
198555 pCoord->u = __builtin_bswap32(*(u32*)p);
198556#elif SQLITE_BYTEORDER1234==4321
198557 pCoord->u = *(u32*)p;
198558#else
198559 pCoord->u = (
198560 (((u32)p[0]) << 24) +
198561 (((u32)p[1]) << 16) +
198562 (((u32)p[2]) << 8) +
198563 (((u32)p[3]) << 0)
198564 );
198565#endif
198566}
198567static i64 readInt64(u8 *p){
198568#if SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
198569 u64 x;
198570 memcpy(&x, p, 8);
198571 return (i64)_byteswap_uint64(x);
198572#elif SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
198573 u64 x;
198574 memcpy(&x, p, 8);
198575 return (i64)__builtin_bswap64(x);
198576#elif SQLITE_BYTEORDER1234==4321
198577 i64 x;
198578 memcpy(&x, p, 8);
198579 return x;
198580#else
198581 return (i64)(
198582 (((u64)p[0]) << 56) +
198583 (((u64)p[1]) << 48) +
198584 (((u64)p[2]) << 40) +
198585 (((u64)p[3]) << 32) +
198586 (((u64)p[4]) << 24) +
198587 (((u64)p[5]) << 16) +
198588 (((u64)p[6]) << 8) +
198589 (((u64)p[7]) << 0)
198590 );
198591#endif
198592}
198593
198594/*
198595** Functions to serialize a 16 bit integer, 32 bit real number and
198596** 64 bit integer. The value returned is the number of bytes written
198597** to the argument buffer (always 2, 4 and 8 respectively).
198598*/
198599static void writeInt16(u8 *p, int i){
198600 p[0] = (i>> 8)&0xFF;
198601 p[1] = (i>> 0)&0xFF;
198602}
198603static int writeCoord(u8 *p, RtreeCoord *pCoord){
198604 u32 i;
198605 assert( ((((char*)p) - (char*)0)&3)==0 )((void) (0)); /* p is always 4-byte aligned */
198606 assert( sizeof(RtreeCoord)==4 )((void) (0));
198607 assert( sizeof(u32)==4 )((void) (0));
198608#if SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
198609 i = __builtin_bswap32(pCoord->u);
198610 memcpy(p, &i, 4);
198611#elif SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
198612 i = _byteswap_ulong(pCoord->u);
198613 memcpy(p, &i, 4);
198614#elif SQLITE_BYTEORDER1234==4321
198615 i = pCoord->u;
198616 memcpy(p, &i, 4);
198617#else
198618 i = pCoord->u;
198619 p[0] = (i>>24)&0xFF;
198620 p[1] = (i>>16)&0xFF;
198621 p[2] = (i>> 8)&0xFF;
198622 p[3] = (i>> 0)&0xFF;
198623#endif
198624 return 4;
198625}
198626static int writeInt64(u8 *p, i64 i){
198627#if SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
198628 i = (i64)__builtin_bswap64((u64)i);
198629 memcpy(p, &i, 8);
198630#elif SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
198631 i = (i64)_byteswap_uint64((u64)i);
198632 memcpy(p, &i, 8);
198633#elif SQLITE_BYTEORDER1234==4321
198634 memcpy(p, &i, 8);
198635#else
198636 p[0] = (i>>56)&0xFF;
198637 p[1] = (i>>48)&0xFF;
198638 p[2] = (i>>40)&0xFF;
198639 p[3] = (i>>32)&0xFF;
198640 p[4] = (i>>24)&0xFF;
198641 p[5] = (i>>16)&0xFF;
198642 p[6] = (i>> 8)&0xFF;
198643 p[7] = (i>> 0)&0xFF;
198644#endif
198645 return 8;
198646}
198647
198648/*
198649** Increment the reference count of node p.
198650*/
198651static void nodeReference(RtreeNode *p){
198652 if( p ){
198653 assert( p->nRef>0 )((void) (0));
198654 p->nRef++;
198655 }
198656}
198657
198658/*
198659** Clear the content of node p (set all bytes to 0x00).
198660*/
198661static void nodeZero(Rtree *pRtree, RtreeNode *p){
198662 memset(&p->zData[2], 0, pRtree->iNodeSize-2);
198663 p->isDirty = 1;
198664}
198665
198666/*
198667** Given a node number iNode, return the corresponding key to use
198668** in the Rtree.aHash table.
198669*/
198670static unsigned int nodeHash(i64 iNode){
198671 return ((unsigned)iNode) % HASHSIZE97;
198672}
198673
198674/*
198675** Search the node hash table for node iNode. If found, return a pointer
198676** to it. Otherwise, return 0.
198677*/
198678static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){
198679 RtreeNode *p;
198680 for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext);
198681 return p;
198682}
198683
198684/*
198685** Add node pNode to the node hash table.
198686*/
198687static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){
198688 int iHash;
198689 assert( pNode->pNext==0 )((void) (0));
198690 iHash = nodeHash(pNode->iNode);
198691 pNode->pNext = pRtree->aHash[iHash];
198692 pRtree->aHash[iHash] = pNode;
198693}
198694
198695/*
198696** Remove node pNode from the node hash table.
198697*/
198698static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){
198699 RtreeNode **pp;
198700 if( pNode->iNode!=0 ){
198701 pp = &pRtree->aHash[nodeHash(pNode->iNode)];
198702 for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp)((void) (0)); }
198703 *pp = pNode->pNext;
198704 pNode->pNext = 0;
198705 }
198706}
198707
198708/*
198709** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0),
198710** indicating that node has not yet been assigned a node number. It is
198711** assigned a node number when nodeWrite() is called to write the
198712** node contents out to the database.
198713*/
198714static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){
198715 RtreeNode *pNode;
198716 pNode = (RtreeNode *)sqlite3_malloc64(sizeof(RtreeNode) + pRtree->iNodeSize);
198717 if( pNode ){
198718 memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize);
198719 pNode->zData = (u8 *)&pNode[1];
198720 pNode->nRef = 1;
198721 pRtree->nNodeRef++;
198722 pNode->pParent = pParent;
198723 pNode->isDirty = 1;
198724 nodeReference(pParent);
198725 }
198726 return pNode;
198727}
198728
198729/*
198730** Clear the Rtree.pNodeBlob object
198731*/
198732static void nodeBlobReset(Rtree *pRtree){
198733 if( pRtree->pNodeBlob && pRtree->inWrTrans==0 && pRtree->nCursor==0 ){
198734 sqlite3_blob *pBlob = pRtree->pNodeBlob;
198735 pRtree->pNodeBlob = 0;
198736 sqlite3_blob_close(pBlob);
198737 }
198738}
198739
198740/*
198741** Obtain a reference to an r-tree node.
198742*/
198743static int nodeAcquire(
198744 Rtree *pRtree, /* R-tree structure */
198745 i64 iNode, /* Node number to load */
198746 RtreeNode *pParent, /* Either the parent node or NULL */
198747 RtreeNode **ppNode /* OUT: Acquired node */
198748){
198749 int rc = SQLITE_OK0;
198750 RtreeNode *pNode = 0;
198751
198752 /* Check if the requested node is already in the hash table. If so,
198753 ** increase its reference count and return it.
198754 */
198755 if( (pNode = nodeHashLookup(pRtree, iNode))!=0 ){
198756 if( pParent && pParent!=pNode->pParent ){
198757 RTREE_IS_CORRUPT(pRtree);
198758 return SQLITE_CORRUPT_VTAB(11 | (1<<8));
198759 }
198760 pNode->nRef++;
198761 *ppNode = pNode;
198762 return SQLITE_OK0;
198763 }
198764
198765 if( pRtree->pNodeBlob ){
198766 sqlite3_blob *pBlob = pRtree->pNodeBlob;
198767 pRtree->pNodeBlob = 0;
198768 rc = sqlite3_blob_reopen(pBlob, iNode);
198769 pRtree->pNodeBlob = pBlob;
198770 if( rc ){
198771 nodeBlobReset(pRtree);
198772 if( rc==SQLITE_NOMEM7 ) return SQLITE_NOMEM7;
198773 }
198774 }
198775 if( pRtree->pNodeBlob==0 ){
198776 char *zTab = sqlite3_mprintf("%s_node", pRtree->zName);
198777 if( zTab==0 ) return SQLITE_NOMEM7;
198778 rc = sqlite3_blob_open(pRtree->db, pRtree->zDb, zTab, "data", iNode, 0,
198779 &pRtree->pNodeBlob);
198780 sqlite3_free(zTab);
198781 }
198782 if( rc ){
198783 nodeBlobReset(pRtree);
198784 *ppNode = 0;
198785 /* If unable to open an sqlite3_blob on the desired row, that can only
198786 ** be because the shadow tables hold erroneous data. */
198787 if( rc==SQLITE_ERROR1 ){
198788 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
198789 RTREE_IS_CORRUPT(pRtree);
198790 }
198791 }else if( pRtree->iNodeSize==sqlite3_blob_bytes(pRtree->pNodeBlob) ){
198792 pNode = (RtreeNode *)sqlite3_malloc64(sizeof(RtreeNode)+pRtree->iNodeSize);
198793 if( !pNode ){
198794 rc = SQLITE_NOMEM7;
198795 }else{
198796 pNode->pParent = pParent;
198797 pNode->zData = (u8 *)&pNode[1];
198798 pNode->nRef = 1;
198799 pRtree->nNodeRef++;
198800 pNode->iNode = iNode;
198801 pNode->isDirty = 0;
198802 pNode->pNext = 0;
198803 rc = sqlite3_blob_read(pRtree->pNodeBlob, pNode->zData,
198804 pRtree->iNodeSize, 0);
198805 }
198806 }
198807
198808 /* If the root node was just loaded, set pRtree->iDepth to the height
198809 ** of the r-tree structure. A height of zero means all data is stored on
198810 ** the root node. A height of one means the children of the root node
198811 ** are the leaves, and so on. If the depth as specified on the root node
198812 ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
198813 */
198814 if( rc==SQLITE_OK0 && pNode && iNode==1 ){
198815 pRtree->iDepth = readInt16(pNode->zData);
198816 if( pRtree->iDepth>RTREE_MAX_DEPTH40 ){
198817 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
198818 RTREE_IS_CORRUPT(pRtree);
198819 }
198820 }
198821
198822 /* If no error has occurred so far, check if the "number of entries"
198823 ** field on the node is too large. If so, set the return code to
198824 ** SQLITE_CORRUPT_VTAB.
198825 */
198826 if( pNode && rc==SQLITE_OK0 ){
198827 if( NCELL(pNode)readInt16(&(pNode)->zData[2])>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){
198828 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
198829 RTREE_IS_CORRUPT(pRtree);
198830 }
198831 }
198832
198833 if( rc==SQLITE_OK0 ){
198834 if( pNode!=0 ){
198835 nodeReference(pParent);
198836 nodeHashInsert(pRtree, pNode);
198837 }else{
198838 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
198839 RTREE_IS_CORRUPT(pRtree);
198840 }
198841 *ppNode = pNode;
198842 }else{
198843 if( pNode ){
198844 pRtree->nNodeRef--;
198845 sqlite3_free(pNode);
198846 }
198847 *ppNode = 0;
198848 }
198849
198850 return rc;
198851}
198852
198853/*
198854** Overwrite cell iCell of node pNode with the contents of pCell.
198855*/
198856static void nodeOverwriteCell(
198857 Rtree *pRtree, /* The overall R-Tree */
198858 RtreeNode *pNode, /* The node into which the cell is to be written */
198859 RtreeCell *pCell, /* The cell to write */
198860 int iCell /* Index into pNode into which pCell is written */
198861){
198862 int ii;
198863 u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
198864 p += writeInt64(p, pCell->iRowid);
198865 for(ii=0; ii<pRtree->nDim2; ii++){
198866 p += writeCoord(p, &pCell->aCoord[ii]);
198867 }
198868 pNode->isDirty = 1;
198869}
198870
198871/*
198872** Remove the cell with index iCell from node pNode.
198873*/
198874static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
198875 u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
198876 u8 *pSrc = &pDst[pRtree->nBytesPerCell];
198877 int nByte = (NCELL(pNode)readInt16(&(pNode)->zData[2]) - iCell - 1) * pRtree->nBytesPerCell;
198878 memmove(pDst, pSrc, nByte);
198879 writeInt16(&pNode->zData[2], NCELL(pNode)readInt16(&(pNode)->zData[2])-1);
198880 pNode->isDirty = 1;
198881}
198882
198883/*
198884** Insert the contents of cell pCell into node pNode. If the insert
198885** is successful, return SQLITE_OK.
198886**
198887** If there is not enough free space in pNode, return SQLITE_FULL.
198888*/
198889static int nodeInsertCell(
198890 Rtree *pRtree, /* The overall R-Tree */
198891 RtreeNode *pNode, /* Write new cell into this node */
198892 RtreeCell *pCell /* The cell to be inserted */
198893){
198894 int nCell; /* Current number of cells in pNode */
198895 int nMaxCell; /* Maximum number of cells for pNode */
198896
198897 nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
198898 nCell = NCELL(pNode)readInt16(&(pNode)->zData[2]);
198899
198900 assert( nCell<=nMaxCell )((void) (0));
198901 if( nCell<nMaxCell ){
198902 nodeOverwriteCell(pRtree, pNode, pCell, nCell);
198903 writeInt16(&pNode->zData[2], nCell+1);
198904 pNode->isDirty = 1;
198905 }
198906
198907 return (nCell==nMaxCell);
198908}
198909
198910/*
198911** If the node is dirty, write it out to the database.
198912*/
198913static int nodeWrite(Rtree *pRtree, RtreeNode *pNode){
198914 int rc = SQLITE_OK0;
198915 if( pNode->isDirty ){
198916 sqlite3_stmt *p = pRtree->pWriteNode;
198917 if( pNode->iNode ){
198918 sqlite3_bind_int64(p, 1, pNode->iNode);
198919 }else{
198920 sqlite3_bind_null(p, 1);
198921 }
198922 sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC((sqlite3_destructor_type)0));
198923 sqlite3_step(p);
198924 pNode->isDirty = 0;
198925 rc = sqlite3_reset(p);
198926 sqlite3_bind_null(p, 2);
198927 if( pNode->iNode==0 && rc==SQLITE_OK0 ){
198928 pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
198929 nodeHashInsert(pRtree, pNode);
198930 }
198931 }
198932 return rc;
198933}
198934
198935/*
198936** Release a reference to a node. If the node is dirty and the reference
198937** count drops to zero, the node data is written to the database.
198938*/
198939static int nodeRelease(Rtree *pRtree, RtreeNode *pNode){
198940 int rc = SQLITE_OK0;
198941 if( pNode ){
198942 assert( pNode->nRef>0 )((void) (0));
198943 assert( pRtree->nNodeRef>0 )((void) (0));
198944 pNode->nRef--;
198945 if( pNode->nRef==0 ){
198946 pRtree->nNodeRef--;
198947 if( pNode->iNode==1 ){
198948 pRtree->iDepth = -1;
198949 }
198950 if( pNode->pParent ){
198951 rc = nodeRelease(pRtree, pNode->pParent);
198952 }
198953 if( rc==SQLITE_OK0 ){
198954 rc = nodeWrite(pRtree, pNode);
198955 }
198956 nodeHashDelete(pRtree, pNode);
198957 sqlite3_free(pNode);
198958 }
198959 }
198960 return rc;
198961}
198962
198963/*
198964** Return the 64-bit integer value associated with cell iCell of
198965** node pNode. If pNode is a leaf node, this is a rowid. If it is
198966** an internal node, then the 64-bit integer is a child page number.
198967*/
198968static i64 nodeGetRowid(
198969 Rtree *pRtree, /* The overall R-Tree */
198970 RtreeNode *pNode, /* The node from which to extract the ID */
198971 int iCell /* The cell index from which to extract the ID */
198972){
198973 assert( iCell<NCELL(pNode) )((void) (0));
198974 return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
198975}
198976
198977/*
198978** Return coordinate iCoord from cell iCell in node pNode.
198979*/
198980static void nodeGetCoord(
198981 Rtree *pRtree, /* The overall R-Tree */
198982 RtreeNode *pNode, /* The node from which to extract a coordinate */
198983 int iCell, /* The index of the cell within the node */
198984 int iCoord, /* Which coordinate to extract */
198985 RtreeCoord *pCoord /* OUT: Space to write result to */
198986){
198987 readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
198988}
198989
198990/*
198991** Deserialize cell iCell of node pNode. Populate the structure pointed
198992** to by pCell with the results.
198993*/
198994static void nodeGetCell(
198995 Rtree *pRtree, /* The overall R-Tree */
198996 RtreeNode *pNode, /* The node containing the cell to be read */
198997 int iCell, /* Index of the cell within the node */
198998 RtreeCell *pCell /* OUT: Write the cell contents here */
198999){
199000 u8 *pData;
199001 RtreeCoord *pCoord;
199002 int ii = 0;
199003 pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
199004 pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell);
199005 pCoord = pCell->aCoord;
199006 do{
199007 readCoord(pData, &pCoord[ii]);
199008 readCoord(pData+4, &pCoord[ii+1]);
199009 pData += 8;
199010 ii += 2;
199011 }while( ii<pRtree->nDim2 );
199012}
199013
199014
199015/* Forward declaration for the function that does the work of
199016** the virtual table module xCreate() and xConnect() methods.
199017*/
199018static int rtreeInit(
199019 sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **, int
199020);
199021
199022/*
199023** Rtree virtual table module xCreate method.
199024*/
199025static int rtreeCreate(
199026 sqlite3 *db,
199027 void *pAux,
199028 int argc, const char *const*argv,
199029 sqlite3_vtab **ppVtab,
199030 char **pzErr
199031){
199032 return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
199033}
199034
199035/*
199036** Rtree virtual table module xConnect method.
199037*/
199038static int rtreeConnect(
199039 sqlite3 *db,
199040 void *pAux,
199041 int argc, const char *const*argv,
199042 sqlite3_vtab **ppVtab,
199043 char **pzErr
199044){
199045 return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
199046}
199047
199048/*
199049** Increment the r-tree reference count.
199050*/
199051static void rtreeReference(Rtree *pRtree){
199052 pRtree->nBusy++;
199053}
199054
199055/*
199056** Decrement the r-tree reference count. When the reference count reaches
199057** zero the structure is deleted.
199058*/
199059static void rtreeRelease(Rtree *pRtree){
199060 pRtree->nBusy--;
199061 if( pRtree->nBusy==0 ){
199062 pRtree->inWrTrans = 0;
199063 assert( pRtree->nCursor==0 )((void) (0));
199064 nodeBlobReset(pRtree);
199065 assert( pRtree->nNodeRef==0 || pRtree->bCorrupt )((void) (0));
199066 sqlite3_finalize(pRtree->pWriteNode);
199067 sqlite3_finalize(pRtree->pDeleteNode);
199068 sqlite3_finalize(pRtree->pReadRowid);
199069 sqlite3_finalize(pRtree->pWriteRowid);
199070 sqlite3_finalize(pRtree->pDeleteRowid);
199071 sqlite3_finalize(pRtree->pReadParent);
199072 sqlite3_finalize(pRtree->pWriteParent);
199073 sqlite3_finalize(pRtree->pDeleteParent);
199074 sqlite3_finalize(pRtree->pWriteAux);
199075 sqlite3_free(pRtree->zReadAuxSql);
199076 sqlite3_free(pRtree);
199077 }
199078}
199079
199080/*
199081** Rtree virtual table module xDisconnect method.
199082*/
199083static int rtreeDisconnect(sqlite3_vtab *pVtab){
199084 rtreeRelease((Rtree *)pVtab);
199085 return SQLITE_OK0;
199086}
199087
199088/*
199089** Rtree virtual table module xDestroy method.
199090*/
199091static int rtreeDestroy(sqlite3_vtab *pVtab){
199092 Rtree *pRtree = (Rtree *)pVtab;
199093 int rc;
199094 char *zCreate = sqlite3_mprintf(
199095 "DROP TABLE '%q'.'%q_node';"
199096 "DROP TABLE '%q'.'%q_rowid';"
199097 "DROP TABLE '%q'.'%q_parent';",
199098 pRtree->zDb, pRtree->zName,
199099 pRtree->zDb, pRtree->zName,
199100 pRtree->zDb, pRtree->zName
199101 );
199102 if( !zCreate ){
199103 rc = SQLITE_NOMEM7;
199104 }else{
199105 nodeBlobReset(pRtree);
199106 rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
199107 sqlite3_free(zCreate);
199108 }
199109 if( rc==SQLITE_OK0 ){
199110 rtreeRelease(pRtree);
199111 }
199112
199113 return rc;
199114}
199115
199116/*
199117** Rtree virtual table module xOpen method.
199118*/
199119static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
199120 int rc = SQLITE_NOMEM7;
199121 Rtree *pRtree = (Rtree *)pVTab;
199122 RtreeCursor *pCsr;
199123
199124 pCsr = (RtreeCursor *)sqlite3_malloc64(sizeof(RtreeCursor));
199125 if( pCsr ){
199126 memset(pCsr, 0, sizeof(RtreeCursor));
199127 pCsr->base.pVtab = pVTab;
199128 rc = SQLITE_OK0;
199129 pRtree->nCursor++;
199130 }
199131 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
199132
199133 return rc;
199134}
199135
199136
199137/*
199138** Reset a cursor back to its initial state.
199139*/
199140static void resetCursor(RtreeCursor *pCsr){
199141 Rtree *pRtree = (Rtree *)(pCsr->base.pVtab);
199142 int ii;
199143 sqlite3_stmt *pStmt;
199144 if( pCsr->aConstraint ){
199145 int i; /* Used to iterate through constraint array */
199146 for(i=0; i<pCsr->nConstraint; i++){
199147 sqlite3_rtree_query_info *pInfo = pCsr->aConstraint[i].pInfo;
199148 if( pInfo ){
199149 if( pInfo->xDelUser ) pInfo->xDelUser(pInfo->pUser);
199150 sqlite3_free(pInfo);
199151 }
199152 }
199153 sqlite3_free(pCsr->aConstraint);
199154 pCsr->aConstraint = 0;
199155 }
199156 for(ii=0; ii<RTREE_CACHE_SZ5; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
199157 sqlite3_free(pCsr->aPoint);
199158 pStmt = pCsr->pReadAux;
199159 memset(pCsr, 0, sizeof(RtreeCursor));
199160 pCsr->base.pVtab = (sqlite3_vtab*)pRtree;
199161 pCsr->pReadAux = pStmt;
199162
199163}
199164
199165/*
199166** Rtree virtual table module xClose method.
199167*/
199168static int rtreeClose(sqlite3_vtab_cursor *cur){
199169 Rtree *pRtree = (Rtree *)(cur->pVtab);
199170 RtreeCursor *pCsr = (RtreeCursor *)cur;
199171 assert( pRtree->nCursor>0 )((void) (0));
199172 resetCursor(pCsr);
199173 sqlite3_finalize(pCsr->pReadAux);
199174 sqlite3_free(pCsr);
199175 pRtree->nCursor--;
199176 nodeBlobReset(pRtree);
199177 return SQLITE_OK0;
199178}
199179
199180/*
199181** Rtree virtual table module xEof method.
199182**
199183** Return non-zero if the cursor does not currently point to a valid
199184** record (i.e if the scan has finished), or zero otherwise.
199185*/
199186static int rtreeEof(sqlite3_vtab_cursor *cur){
199187 RtreeCursor *pCsr = (RtreeCursor *)cur;
199188 return pCsr->atEOF;
199189}
199190
199191/*
199192** Convert raw bits from the on-disk RTree record into a coordinate value.
199193** The on-disk format is big-endian and needs to be converted for little-
199194** endian platforms. The on-disk record stores integer coordinates if
199195** eInt is true and it stores 32-bit floating point records if eInt is
199196** false. a[] is the four bytes of the on-disk record to be decoded.
199197** Store the results in "r".
199198**
199199** There are five versions of this macro. The last one is generic. The
199200** other four are various architectures-specific optimizations.
199201*/
199202#if SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
199203#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; }
{ \
199204 RtreeCoord c; /* Coordinate decoded */ \
199205 c.u = _byteswap_ulong(*(u32*)a); \
199206 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
199207}
199208#elif SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
199209#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; }
{ \
199210 RtreeCoord c; /* Coordinate decoded */ \
199211 c.u = __builtin_bswap32(*(u32*)a); \
199212 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
199213}
199214#elif SQLITE_BYTEORDER1234==1234
199215#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; }
{ \
199216 RtreeCoord c; /* Coordinate decoded */ \
199217 memcpy(&c.u,a,4); \
199218 c.u = ((c.u>>24)&0xff)|((c.u>>8)&0xff00)| \
199219 ((c.u&0xff)<<24)|((c.u&0xff00)<<8); \
199220 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
199221}
199222#elif SQLITE_BYTEORDER1234==4321
199223#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; }
{ \
199224 RtreeCoord c; /* Coordinate decoded */ \
199225 memcpy(&c.u,a,4); \
199226 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
199227}
199228#else
199229#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; }
{ \
199230 RtreeCoord c; /* Coordinate decoded */ \
199231 c.u = ((u32)a[0]<<24) + ((u32)a[1]<<16) \
199232 +((u32)a[2]<<8) + a[3]; \
199233 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
199234}
199235#endif
199236
199237/*
199238** Check the RTree node or entry given by pCellData and p against the MATCH
199239** constraint pConstraint.
199240*/
199241static int rtreeCallbackConstraint(
199242 RtreeConstraint *pConstraint, /* The constraint to test */
199243 int eInt, /* True if RTree holding integer coordinates */
199244 u8 *pCellData, /* Raw cell content */
199245 RtreeSearchPoint *pSearch, /* Container of this cell */
199246 sqlite3_rtree_dbl *prScore, /* OUT: score for the cell */
199247 int *peWithin /* OUT: visibility of the cell */
199248){
199249 sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */
199250 int nCoord = pInfo->nCoord; /* No. of coordinates */
199251 int rc; /* Callback return code */
199252 RtreeCoord c; /* Translator union */
199253 sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS5*2]; /* Decoded coordinates */
199254
199255 assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY )((void) (0));
199256 assert( nCoord==2 || nCoord==4 || nCoord==6 || nCoord==8 || nCoord==10 )((void) (0));
199257
199258 if( pConstraint->op==RTREE_QUERY0x47 && pSearch->iLevel==1 ){
199259 pInfo->iRowid = readInt64(pCellData);
199260 }
199261 pCellData += 8;
199262#ifndef SQLITE_RTREE_INT_ONLY
199263 if( eInt==0 ){
199264 switch( nCoord ){
199265 case 10: readCoord(pCellData+36, &c); aCoord[9] = c.f;
199266 readCoord(pCellData+32, &c); aCoord[8] = c.f;
199267 case 8: readCoord(pCellData+28, &c); aCoord[7] = c.f;
199268 readCoord(pCellData+24, &c); aCoord[6] = c.f;
199269 case 6: readCoord(pCellData+20, &c); aCoord[5] = c.f;
199270 readCoord(pCellData+16, &c); aCoord[4] = c.f;
199271 case 4: readCoord(pCellData+12, &c); aCoord[3] = c.f;
199272 readCoord(pCellData+8, &c); aCoord[2] = c.f;
199273 default: readCoord(pCellData+4, &c); aCoord[1] = c.f;
199274 readCoord(pCellData, &c); aCoord[0] = c.f;
199275 }
199276 }else
199277#endif
199278 {
199279 switch( nCoord ){
199280 case 10: readCoord(pCellData+36, &c); aCoord[9] = c.i;
199281 readCoord(pCellData+32, &c); aCoord[8] = c.i;
199282 case 8: readCoord(pCellData+28, &c); aCoord[7] = c.i;
199283 readCoord(pCellData+24, &c); aCoord[6] = c.i;
199284 case 6: readCoord(pCellData+20, &c); aCoord[5] = c.i;
199285 readCoord(pCellData+16, &c); aCoord[4] = c.i;
199286 case 4: readCoord(pCellData+12, &c); aCoord[3] = c.i;
199287 readCoord(pCellData+8, &c); aCoord[2] = c.i;
199288 default: readCoord(pCellData+4, &c); aCoord[1] = c.i;
199289 readCoord(pCellData, &c); aCoord[0] = c.i;
199290 }
199291 }
199292 if( pConstraint->op==RTREE_MATCH0x46 ){
199293 int eWithin = 0;
199294 rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo,
199295 nCoord, aCoord, &eWithin);
199296 if( eWithin==0 ) *peWithin = NOT_WITHIN0;
199297 *prScore = RTREE_ZERO0.0;
199298 }else{
199299 pInfo->aCoord = aCoord;
199300 pInfo->iLevel = pSearch->iLevel - 1;
199301 pInfo->rScore = pInfo->rParentScore = pSearch->rScore;
199302 pInfo->eWithin = pInfo->eParentWithin = pSearch->eWithin;
199303 rc = pConstraint->u.xQueryFunc(pInfo);
199304 if( pInfo->eWithin<*peWithin ) *peWithin = pInfo->eWithin;
199305 if( pInfo->rScore<*prScore || *prScore<RTREE_ZERO0.0 ){
199306 *prScore = pInfo->rScore;
199307 }
199308 }
199309 return rc;
199310}
199311
199312/*
199313** Check the internal RTree node given by pCellData against constraint p.
199314** If this constraint cannot be satisfied by any child within the node,
199315** set *peWithin to NOT_WITHIN.
199316*/
199317static void rtreeNonleafConstraint(
199318 RtreeConstraint *p, /* The constraint to test */
199319 int eInt, /* True if RTree holds integer coordinates */
199320 u8 *pCellData, /* Raw cell content as appears on disk */
199321 int *peWithin /* Adjust downward, as appropriate */
199322){
199323 sqlite3_rtree_dbl val; /* Coordinate value convert to a double */
199324
199325 /* p->iCoord might point to either a lower or upper bound coordinate
199326 ** in a coordinate pair. But make pCellData point to the lower bound.
199327 */
199328 pCellData += 8 + 4*(p->iCoord&0xfe);
199329
199330 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE((void) (0))
199331 || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_TRUE((void) (0))
199332 || p->op==RTREE_FALSE )((void) (0));
199333 assert( ((((char*)pCellData) - (char*)0)&3)==0 )((void) (0)); /* 4-byte aligned */
199334 switch( p->op ){
199335 case RTREE_TRUE0x3f: return; /* Always satisfied */
199336 case RTREE_FALSE0x40: break; /* Never satisfied */
199337 case RTREE_EQ0x41:
199338 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; }
;
199339 /* val now holds the lower bound of the coordinate pair */
199340 if( p->u.rValue>=val ){
199341 pCellData += 4;
199342 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; }
;
199343 /* val now holds the upper bound of the coordinate pair */
199344 if( p->u.rValue<=val ) return;
199345 }
199346 break;
199347 case RTREE_LE0x42:
199348 case RTREE_LT0x43:
199349 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; }
;
199350 /* val now holds the lower bound of the coordinate pair */
199351 if( p->u.rValue>=val ) return;
199352 break;
199353
199354 default:
199355 pCellData += 4;
199356 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; }
;
199357 /* val now holds the upper bound of the coordinate pair */
199358 if( p->u.rValue<=val ) return;
199359 break;
199360 }
199361 *peWithin = NOT_WITHIN0;
199362}
199363
199364/*
199365** Check the leaf RTree cell given by pCellData against constraint p.
199366** If this constraint is not satisfied, set *peWithin to NOT_WITHIN.
199367** If the constraint is satisfied, leave *peWithin unchanged.
199368**
199369** The constraint is of the form: xN op $val
199370**
199371** The op is given by p->op. The xN is p->iCoord-th coordinate in
199372** pCellData. $val is given by p->u.rValue.
199373*/
199374static void rtreeLeafConstraint(
199375 RtreeConstraint *p, /* The constraint to test */
199376 int eInt, /* True if RTree holds integer coordinates */
199377 u8 *pCellData, /* Raw cell content as appears on disk */
199378 int *peWithin /* Adjust downward, as appropriate */
199379){
199380 RtreeDValue xN; /* Coordinate value converted to a double */
199381
199382 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE((void) (0))
199383 || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_TRUE((void) (0))
199384 || p->op==RTREE_FALSE )((void) (0));
199385 pCellData += 8 + p->iCoord*4;
199386 assert( ((((char*)pCellData) - (char*)0)&3)==0 )((void) (0)); /* 4-byte aligned */
199387 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; }
;
199388 switch( p->op ){
199389 case RTREE_TRUE0x3f: return; /* Always satisfied */
199390 case RTREE_FALSE0x40: break; /* Never satisfied */
199391 case RTREE_LE0x42: if( xN <= p->u.rValue ) return; break;
199392 case RTREE_LT0x43: if( xN < p->u.rValue ) return; break;
199393 case RTREE_GE0x44: if( xN >= p->u.rValue ) return; break;
199394 case RTREE_GT0x45: if( xN > p->u.rValue ) return; break;
199395 default: if( xN == p->u.rValue ) return; break;
199396 }
199397 *peWithin = NOT_WITHIN0;
199398}
199399
199400/*
199401** One of the cells in node pNode is guaranteed to have a 64-bit
199402** integer value equal to iRowid. Return the index of this cell.
199403*/
199404static int nodeRowidIndex(
199405 Rtree *pRtree,
199406 RtreeNode *pNode,
199407 i64 iRowid,
199408 int *piIndex
199409){
199410 int ii;
199411 int nCell = NCELL(pNode)readInt16(&(pNode)->zData[2]);
199412 assert( nCell<200 )((void) (0));
199413 for(ii=0; ii<nCell; ii++){
199414 if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){
199415 *piIndex = ii;
199416 return SQLITE_OK0;
199417 }
199418 }
199419 RTREE_IS_CORRUPT(pRtree);
199420 return SQLITE_CORRUPT_VTAB(11 | (1<<8));
199421}
199422
199423/*
199424** Return the index of the cell containing a pointer to node pNode
199425** in its parent. If pNode is the root node, return -1.
199426*/
199427static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode, int *piIndex){
199428 RtreeNode *pParent = pNode->pParent;
199429 if( ALWAYS(pParent)(pParent) ){
199430 return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex);
199431 }else{
199432 *piIndex = -1;
199433 return SQLITE_OK0;
199434 }
199435}
199436
199437/*
199438** Compare two search points. Return negative, zero, or positive if the first
199439** is less than, equal to, or greater than the second.
199440**
199441** The rScore is the primary key. Smaller rScore values come first.
199442** If the rScore is a tie, then use iLevel as the tie breaker with smaller
199443** iLevel values coming first. In this way, if rScore is the same for all
199444** SearchPoints, then iLevel becomes the deciding factor and the result
199445** is a depth-first search, which is the desired default behavior.
199446*/
199447static int rtreeSearchPointCompare(
199448 const RtreeSearchPoint *pA,
199449 const RtreeSearchPoint *pB
199450){
199451 if( pA->rScore<pB->rScore ) return -1;
199452 if( pA->rScore>pB->rScore ) return +1;
199453 if( pA->iLevel<pB->iLevel ) return -1;
199454 if( pA->iLevel>pB->iLevel ) return +1;
199455 return 0;
199456}
199457
199458/*
199459** Interchange two search points in a cursor.
199460*/
199461static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){
199462 RtreeSearchPoint t = p->aPoint[i];
199463 assert( i<j )((void) (0));
199464 p->aPoint[i] = p->aPoint[j];
199465 p->aPoint[j] = t;
199466 i++; j++;
199467 if( i<RTREE_CACHE_SZ5 ){
199468 if( j>=RTREE_CACHE_SZ5 ){
199469 nodeRelease(RTREE_OF_CURSOR(p)((Rtree*)((p)->base.pVtab)), p->aNode[i]);
199470 p->aNode[i] = 0;
199471 }else{
199472 RtreeNode *pTemp = p->aNode[i];
199473 p->aNode[i] = p->aNode[j];
199474 p->aNode[j] = pTemp;
199475 }
199476 }
199477}
199478
199479/*
199480** Return the search point with the lowest current score.
199481*/
199482static RtreeSearchPoint *rtreeSearchPointFirst(RtreeCursor *pCur){
199483 return pCur->bPoint ? &pCur->sPoint : pCur->nPoint ? pCur->aPoint : 0;
199484}
199485
199486/*
199487** Get the RtreeNode for the search point with the lowest score.
199488*/
199489static RtreeNode *rtreeNodeOfFirstSearchPoint(RtreeCursor *pCur, int *pRC){
199490 sqlite3_int64 id;
199491 int ii = 1 - pCur->bPoint;
199492 assert( ii==0 || ii==1 )((void) (0));
199493 assert( pCur->bPoint || pCur->nPoint )((void) (0));
199494 if( pCur->aNode[ii]==0 ){
199495 assert( pRC!=0 )((void) (0));
199496 id = ii ? pCur->aPoint[0].id : pCur->sPoint.id;
199497 *pRC = nodeAcquire(RTREE_OF_CURSOR(pCur)((Rtree*)((pCur)->base.pVtab)), id, 0, &pCur->aNode[ii]);
199498 }
199499 return pCur->aNode[ii];
199500}
199501
199502/*
199503** Push a new element onto the priority queue
199504*/
199505static RtreeSearchPoint *rtreeEnqueue(
199506 RtreeCursor *pCur, /* The cursor */
199507 RtreeDValue rScore, /* Score for the new search point */
199508 u8 iLevel /* Level for the new search point */
199509){
199510 int i, j;
199511 RtreeSearchPoint *pNew;
199512 if( pCur->nPoint>=pCur->nPointAlloc ){
199513 int nNew = pCur->nPointAlloc*2 + 8;
199514 pNew = sqlite3_realloc64(pCur->aPoint, nNew*sizeof(pCur->aPoint[0]));
199515 if( pNew==0 ) return 0;
199516 pCur->aPoint = pNew;
199517 pCur->nPointAlloc = nNew;
199518 }
199519 i = pCur->nPoint++;
199520 pNew = pCur->aPoint + i;
199521 pNew->rScore = rScore;
199522 pNew->iLevel = iLevel;
199523 assert( iLevel<=RTREE_MAX_DEPTH )((void) (0));
199524 while( i>0 ){
199525 RtreeSearchPoint *pParent;
199526 j = (i-1)/2;
199527 pParent = pCur->aPoint + j;
199528 if( rtreeSearchPointCompare(pNew, pParent)>=0 ) break;
199529 rtreeSearchPointSwap(pCur, j, i);
199530 i = j;
199531 pNew = pParent;
199532 }
199533 return pNew;
199534}
199535
199536/*
199537** Allocate a new RtreeSearchPoint and return a pointer to it. Return
199538** NULL if malloc fails.
199539*/
199540static RtreeSearchPoint *rtreeSearchPointNew(
199541 RtreeCursor *pCur, /* The cursor */
199542 RtreeDValue rScore, /* Score for the new search point */
199543 u8 iLevel /* Level for the new search point */
199544){
199545 RtreeSearchPoint *pNew, *pFirst;
199546 pFirst = rtreeSearchPointFirst(pCur);
199547 pCur->anQueue[iLevel]++;
199548 if( pFirst==0
199549 || pFirst->rScore>rScore
199550 || (pFirst->rScore==rScore && pFirst->iLevel>iLevel)
199551 ){
199552 if( pCur->bPoint ){
199553 int ii;
199554 pNew = rtreeEnqueue(pCur, rScore, iLevel);
199555 if( pNew==0 ) return 0;
199556 ii = (int)(pNew - pCur->aPoint) + 1;
199557 assert( ii==1 )((void) (0));
199558 if( ALWAYS(ii<RTREE_CACHE_SZ)(ii<5) ){
199559 assert( pCur->aNode[ii]==0 )((void) (0));
199560 pCur->aNode[ii] = pCur->aNode[0];
199561 }else{
199562 nodeRelease(RTREE_OF_CURSOR(pCur)((Rtree*)((pCur)->base.pVtab)), pCur->aNode[0]);
199563 }
199564 pCur->aNode[0] = 0;
199565 *pNew = pCur->sPoint;
199566 }
199567 pCur->sPoint.rScore = rScore;
199568 pCur->sPoint.iLevel = iLevel;
199569 pCur->bPoint = 1;
199570 return &pCur->sPoint;
199571 }else{
199572 return rtreeEnqueue(pCur, rScore, iLevel);
199573 }
199574}
199575
199576#if 0
199577/* Tracing routines for the RtreeSearchPoint queue */
199578static void tracePoint(RtreeSearchPoint *p, int idx, RtreeCursor *pCur){
199579 if( idx<0 ){ printf(" s"); }else{ printf("%2d", idx); }
199580 printf(" %d.%05lld.%02d %g %d",
199581 p->iLevel, p->id, p->iCell, p->rScore, p->eWithin
199582 );
199583 idx++;
199584 if( idx<RTREE_CACHE_SZ5 ){
199585 printf(" %p\n", pCur->aNode[idx]);
199586 }else{
199587 printf("\n");
199588 }
199589}
199590static void traceQueue(RtreeCursor *pCur, const char *zPrefix){
199591 int ii;
199592 printf("=== %9s ", zPrefix);
199593 if( pCur->bPoint ){
199594 tracePoint(&pCur->sPoint, -1, pCur);
199595 }
199596 for(ii=0; ii<pCur->nPoint; ii++){
199597 if( ii>0 || pCur->bPoint ) printf(" ");
199598 tracePoint(&pCur->aPoint[ii], ii, pCur);
199599 }
199600}
199601# define RTREE_QUEUE_TRACE(A,B) traceQueue(A,B)
199602#else
199603# define RTREE_QUEUE_TRACE(A,B) /* no-op */
199604#endif
199605
199606/* Remove the search point with the lowest current score.
199607*/
199608static void rtreeSearchPointPop(RtreeCursor *p){
199609 int i, j, k, n;
199610 i = 1 - p->bPoint;
199611 assert( i==0 || i==1 )((void) (0));
199612 if( p->aNode[i] ){
199613 nodeRelease(RTREE_OF_CURSOR(p)((Rtree*)((p)->base.pVtab)), p->aNode[i]);
199614 p->aNode[i] = 0;
199615 }
199616 if( p->bPoint ){
199617 p->anQueue[p->sPoint.iLevel]--;
199618 p->bPoint = 0;
199619 }else if( ALWAYS(p->nPoint)(p->nPoint) ){
199620 p->anQueue[p->aPoint[0].iLevel]--;
199621 n = --p->nPoint;
199622 p->aPoint[0] = p->aPoint[n];
199623 if( n<RTREE_CACHE_SZ5-1 ){
199624 p->aNode[1] = p->aNode[n+1];
199625 p->aNode[n+1] = 0;
199626 }
199627 i = 0;
199628 while( (j = i*2+1)<n ){
199629 k = j+1;
199630 if( k<n && rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[j])<0 ){
199631 if( rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[i])<0 ){
199632 rtreeSearchPointSwap(p, i, k);
199633 i = k;
199634 }else{
199635 break;
199636 }
199637 }else{
199638 if( rtreeSearchPointCompare(&p->aPoint[j], &p->aPoint[i])<0 ){
199639 rtreeSearchPointSwap(p, i, j);
199640 i = j;
199641 }else{
199642 break;
199643 }
199644 }
199645 }
199646 }
199647}
199648
199649
199650/*
199651** Continue the search on cursor pCur until the front of the queue
199652** contains an entry suitable for returning as a result-set row,
199653** or until the RtreeSearchPoint queue is empty, indicating that the
199654** query has completed.
199655*/
199656static int rtreeStepToLeaf(RtreeCursor *pCur){
199657 RtreeSearchPoint *p;
199658 Rtree *pRtree = RTREE_OF_CURSOR(pCur)((Rtree*)((pCur)->base.pVtab));
199659 RtreeNode *pNode;
199660 int eWithin;
199661 int rc = SQLITE_OK0;
199662 int nCell;
199663 int nConstraint = pCur->nConstraint;
199664 int ii;
199665 int eInt;
199666 RtreeSearchPoint x;
199667
199668 eInt = pRtree->eCoordType==RTREE_COORD_INT321;
199669 while( (p = rtreeSearchPointFirst(pCur))!=0 && p->iLevel>0 ){
199670 u8 *pCellData;
199671 pNode = rtreeNodeOfFirstSearchPoint(pCur, &rc);
199672 if( rc ) return rc;
199673 nCell = NCELL(pNode)readInt16(&(pNode)->zData[2]);
199674 assert( nCell<200 )((void) (0));
199675 pCellData = pNode->zData + (4+pRtree->nBytesPerCell*p->iCell);
199676 while( p->iCell<nCell ){
199677 sqlite3_rtree_dbl rScore = (sqlite3_rtree_dbl)-1;
199678 eWithin = FULLY_WITHIN2;
199679 for(ii=0; ii<nConstraint; ii++){
199680 RtreeConstraint *pConstraint = pCur->aConstraint + ii;
199681 if( pConstraint->op>=RTREE_MATCH0x46 ){
199682 rc = rtreeCallbackConstraint(pConstraint, eInt, pCellData, p,
199683 &rScore, &eWithin);
199684 if( rc ) return rc;
199685 }else if( p->iLevel==1 ){
199686 rtreeLeafConstraint(pConstraint, eInt, pCellData, &eWithin);
199687 }else{
199688 rtreeNonleafConstraint(pConstraint, eInt, pCellData, &eWithin);
199689 }
199690 if( eWithin==NOT_WITHIN0 ){
199691 p->iCell++;
199692 pCellData += pRtree->nBytesPerCell;
199693 break;
199694 }
199695 }
199696 if( eWithin==NOT_WITHIN0 ) continue;
199697 p->iCell++;
199698 x.iLevel = p->iLevel - 1;
199699 if( x.iLevel ){
199700 x.id = readInt64(pCellData);
199701 for(ii=0; ii<pCur->nPoint; ii++){
199702 if( pCur->aPoint[ii].id==x.id ){
199703 RTREE_IS_CORRUPT(pRtree);
199704 return SQLITE_CORRUPT_VTAB(11 | (1<<8));
199705 }
199706 }
199707 x.iCell = 0;
199708 }else{
199709 x.id = p->id;
199710 x.iCell = p->iCell - 1;
199711 }
199712 if( p->iCell>=nCell ){
199713 RTREE_QUEUE_TRACE(pCur, "POP-S:");
199714 rtreeSearchPointPop(pCur);
199715 }
199716 if( rScore<RTREE_ZERO0.0 ) rScore = RTREE_ZERO0.0;
199717 p = rtreeSearchPointNew(pCur, rScore, x.iLevel);
199718 if( p==0 ) return SQLITE_NOMEM7;
199719 p->eWithin = (u8)eWithin;
199720 p->id = x.id;
199721 p->iCell = x.iCell;
199722 RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
199723 break;
199724 }
199725 if( p->iCell>=nCell ){
199726 RTREE_QUEUE_TRACE(pCur, "POP-Se:");
199727 rtreeSearchPointPop(pCur);
199728 }
199729 }
199730 pCur->atEOF = p==0;
199731 return SQLITE_OK0;
199732}
199733
199734/*
199735** Rtree virtual table module xNext method.
199736*/
199737static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
199738 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
199739 int rc = SQLITE_OK0;
199740
199741 /* Move to the next entry that matches the configured constraints. */
199742 RTREE_QUEUE_TRACE(pCsr, "POP-Nx:");
199743 if( pCsr->bAuxValid ){
199744 pCsr->bAuxValid = 0;
199745 sqlite3_reset(pCsr->pReadAux);
199746 }
199747 rtreeSearchPointPop(pCsr);
199748 rc = rtreeStepToLeaf(pCsr);
199749 return rc;
199750}
199751
199752/*
199753** Rtree virtual table module xRowid method.
199754*/
199755static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
199756 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
199757 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
199758 int rc = SQLITE_OK0;
199759 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
199760 if( rc==SQLITE_OK0 && ALWAYS(p)(p) ){
199761 *pRowid = nodeGetRowid(RTREE_OF_CURSOR(pCsr)((Rtree*)((pCsr)->base.pVtab)), pNode, p->iCell);
199762 }
199763 return rc;
199764}
199765
199766/*
199767** Rtree virtual table module xColumn method.
199768*/
199769static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
199770 Rtree *pRtree = (Rtree *)cur->pVtab;
199771 RtreeCursor *pCsr = (RtreeCursor *)cur;
199772 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
199773 RtreeCoord c;
199774 int rc = SQLITE_OK0;
199775 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
199776
199777 if( rc ) return rc;
199778 if( NEVER(p==0)(p==0) ) return SQLITE_OK0;
199779 if( i==0 ){
199780 sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
199781 }else if( i<=pRtree->nDim2 ){
199782 nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
199783#ifndef SQLITE_RTREE_INT_ONLY
199784 if( pRtree->eCoordType==RTREE_COORD_REAL320 ){
199785 sqlite3_result_double(ctx, c.f);
199786 }else
199787#endif
199788 {
199789 assert( pRtree->eCoordType==RTREE_COORD_INT32 )((void) (0));
199790 sqlite3_result_int(ctx, c.i);
199791 }
199792 }else{
199793 if( !pCsr->bAuxValid ){
199794 if( pCsr->pReadAux==0 ){
199795 rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0,
199796 &pCsr->pReadAux, 0);
199797 if( rc ) return rc;
199798 }
199799 sqlite3_bind_int64(pCsr->pReadAux, 1,
199800 nodeGetRowid(pRtree, pNode, p->iCell));
199801 rc = sqlite3_step(pCsr->pReadAux);
199802 if( rc==SQLITE_ROW100 ){
199803 pCsr->bAuxValid = 1;
199804 }else{
199805 sqlite3_reset(pCsr->pReadAux);
199806 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
199807 return rc;
199808 }
199809 }
199810 sqlite3_result_value(ctx,
199811 sqlite3_column_value(pCsr->pReadAux, i - pRtree->nDim2 + 1));
199812 }
199813 return SQLITE_OK0;
199814}
199815
199816/*
199817** Use nodeAcquire() to obtain the leaf node containing the record with
199818** rowid iRowid. If successful, set *ppLeaf to point to the node and
199819** return SQLITE_OK. If there is no such record in the table, set
199820** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf
199821** to zero and return an SQLite error code.
199822*/
199823static int findLeafNode(
199824 Rtree *pRtree, /* RTree to search */
199825 i64 iRowid, /* The rowid searching for */
199826 RtreeNode **ppLeaf, /* Write the node here */
199827 sqlite3_int64 *piNode /* Write the node-id here */
199828){
199829 int rc;
199830 *ppLeaf = 0;
199831 sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid);
199832 if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW100 ){
199833 i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0);
199834 if( piNode ) *piNode = iNode;
199835 rc = nodeAcquire(pRtree, iNode, 0, ppLeaf);
199836 sqlite3_reset(pRtree->pReadRowid);
199837 }else{
199838 rc = sqlite3_reset(pRtree->pReadRowid);
199839 }
199840 return rc;
199841}
199842
199843/*
199844** This function is called to configure the RtreeConstraint object passed
199845** as the second argument for a MATCH constraint. The value passed as the
199846** first argument to this function is the right-hand operand to the MATCH
199847** operator.
199848*/
199849static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
199850 RtreeMatchArg *pBlob, *pSrc; /* BLOB returned by geometry function */
199851 sqlite3_rtree_query_info *pInfo; /* Callback information */
199852
199853 pSrc = sqlite3_value_pointer(pValue, "RtreeMatchArg");
199854 if( pSrc==0 ) return SQLITE_ERROR1;
199855 pInfo = (sqlite3_rtree_query_info*)
199856 sqlite3_malloc64( sizeof(*pInfo)+pSrc->iSize );
199857 if( !pInfo ) return SQLITE_NOMEM7;
199858 memset(pInfo, 0, sizeof(*pInfo));
199859 pBlob = (RtreeMatchArg*)&pInfo[1];
199860 memcpy(pBlob, pSrc, pSrc->iSize);
199861 pInfo->pContext = pBlob->cb.pContext;
199862 pInfo->nParam = pBlob->nParam;
199863 pInfo->aParam = pBlob->aParam;
199864 pInfo->apSqlParam = pBlob->apSqlParam;
199865
199866 if( pBlob->cb.xGeom ){
199867 pCons->u.xGeom = pBlob->cb.xGeom;
199868 }else{
199869 pCons->op = RTREE_QUERY0x47;
199870 pCons->u.xQueryFunc = pBlob->cb.xQueryFunc;
199871 }
199872 pCons->pInfo = pInfo;
199873 return SQLITE_OK0;
199874}
199875
199876/*
199877** Rtree virtual table module xFilter method.
199878*/
199879static int rtreeFilter(
199880 sqlite3_vtab_cursor *pVtabCursor,
199881 int idxNum, const char *idxStr,
199882 int argc, sqlite3_value **argv
199883){
199884 Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
199885 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
199886 RtreeNode *pRoot = 0;
199887 int ii;
199888 int rc = SQLITE_OK0;
199889 int iCell = 0;
199890
199891 rtreeReference(pRtree);
199892
199893 /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
199894 resetCursor(pCsr);
199895
199896 pCsr->iStrategy = idxNum;
199897 if( idxNum==1 ){
199898 /* Special case - lookup by rowid. */
199899 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
199900 RtreeSearchPoint *p; /* Search point for the leaf */
199901 i64 iRowid = sqlite3_value_int64(argv[0]);
199902 i64 iNode = 0;
199903 int eType = sqlite3_value_numeric_type(argv[0]);
199904 if( eType==SQLITE_INTEGER1
199905 || (eType==SQLITE_FLOAT2 && sqlite3_value_double(argv[0])==iRowid)
199906 ){
199907 rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
199908 }else{
199909 rc = SQLITE_OK0;
199910 pLeaf = 0;
199911 }
199912 if( rc==SQLITE_OK0 && pLeaf!=0 ){
199913 p = rtreeSearchPointNew(pCsr, RTREE_ZERO0.0, 0);
199914 assert( p!=0 )((void) (0)); /* Always returns pCsr->sPoint */
199915 pCsr->aNode[0] = pLeaf;
199916 p->id = iNode;
199917 p->eWithin = PARTLY_WITHIN1;
199918 rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
199919 p->iCell = (u8)iCell;
199920 RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
199921 }else{
199922 pCsr->atEOF = 1;
199923 }
199924 }else{
199925 /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
199926 ** with the configured constraints.
199927 */
199928 rc = nodeAcquire(pRtree, 1, 0, &pRoot);
199929 if( rc==SQLITE_OK0 && argc>0 ){
199930 pCsr->aConstraint = sqlite3_malloc64(sizeof(RtreeConstraint)*argc);
199931 pCsr->nConstraint = argc;
199932 if( !pCsr->aConstraint ){
199933 rc = SQLITE_NOMEM7;
199934 }else{
199935 memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
199936 memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1));
199937 assert( (idxStr==0 && argc==0)((void) (0))
199938 || (idxStr && (int)strlen(idxStr)==argc*2) )((void) (0));
199939 for(ii=0; ii<argc; ii++){
199940 RtreeConstraint *p = &pCsr->aConstraint[ii];
199941 int eType = sqlite3_value_numeric_type(argv[ii]);
199942 p->op = idxStr[ii*2];
199943 p->iCoord = idxStr[ii*2+1]-'0';
199944 if( p->op>=RTREE_MATCH0x46 ){
199945 /* A MATCH operator. The right-hand-side must be a blob that
199946 ** can be cast into an RtreeMatchArg object. One created using
199947 ** an sqlite3_rtree_geometry_callback() SQL user function.
199948 */
199949 rc = deserializeGeometry(argv[ii], p);
199950 if( rc!=SQLITE_OK0 ){
199951 break;
199952 }
199953 p->pInfo->nCoord = pRtree->nDim2;
199954 p->pInfo->anQueue = pCsr->anQueue;
199955 p->pInfo->mxLevel = pRtree->iDepth + 1;
199956 }else if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
199957#ifdef SQLITE_RTREE_INT_ONLY
199958 p->u.rValue = sqlite3_value_int64(argv[ii]);
199959#else
199960 p->u.rValue = sqlite3_value_double(argv[ii]);
199961#endif
199962 }else{
199963 p->u.rValue = RTREE_ZERO0.0;
199964 if( eType==SQLITE_NULL5 ){
199965 p->op = RTREE_FALSE0x40;
199966 }else if( p->op==RTREE_LT0x43 || p->op==RTREE_LE0x42 ){
199967 p->op = RTREE_TRUE0x3f;
199968 }else{
199969 p->op = RTREE_FALSE0x40;
199970 }
199971 }
199972 }
199973 }
199974 }
199975 if( rc==SQLITE_OK0 ){
199976 RtreeSearchPoint *pNew;
199977 assert( pCsr->bPoint==0 )((void) (0)); /* Due to the resetCursor() call above */
199978 pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO0.0, (u8)(pRtree->iDepth+1));
199979 if( NEVER(pNew==0)(pNew==0) ){ /* Because pCsr->bPoint was FALSE */
199980 return SQLITE_NOMEM7;
199981 }
199982 pNew->id = 1;
199983 pNew->iCell = 0;
199984 pNew->eWithin = PARTLY_WITHIN1;
199985 assert( pCsr->bPoint==1 )((void) (0));
199986 pCsr->aNode[0] = pRoot;
199987 pRoot = 0;
199988 RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
199989 rc = rtreeStepToLeaf(pCsr);
199990 }
199991 }
199992
199993 nodeRelease(pRtree, pRoot);
199994 rtreeRelease(pRtree);
199995 return rc;
199996}
199997
199998/*
199999** Rtree virtual table module xBestIndex method. There are three
200000** table scan strategies to choose from (in order from most to
200001** least desirable):
200002**
200003** idxNum idxStr Strategy
200004** ------------------------------------------------
200005** 1 Unused Direct lookup by rowid.
200006** 2 See below R-tree query or full-table scan.
200007** ------------------------------------------------
200008**
200009** If strategy 1 is used, then idxStr is not meaningful. If strategy
200010** 2 is used, idxStr is formatted to contain 2 bytes for each
200011** constraint used. The first two bytes of idxStr correspond to
200012** the constraint in sqlite3_index_info.aConstraintUsage[] with
200013** (argvIndex==1) etc.
200014**
200015** The first of each pair of bytes in idxStr identifies the constraint
200016** operator as follows:
200017**
200018** Operator Byte Value
200019** ----------------------
200020** = 0x41 ('A')
200021** <= 0x42 ('B')
200022** < 0x43 ('C')
200023** >= 0x44 ('D')
200024** > 0x45 ('E')
200025** MATCH 0x46 ('F')
200026** ----------------------
200027**
200028** The second of each pair of bytes identifies the coordinate column
200029** to which the constraint applies. The leftmost coordinate column
200030** is 'a', the second from the left 'b' etc.
200031*/
200032static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
200033 Rtree *pRtree = (Rtree*)tab;
200034 int rc = SQLITE_OK0;
200035 int ii;
200036 int bMatch = 0; /* True if there exists a MATCH constraint */
200037 i64 nRow; /* Estimated rows returned by this scan */
200038
200039 int iIdx = 0;
200040 char zIdxStr[RTREE_MAX_DIMENSIONS5*8+1];
200041 memset(zIdxStr, 0, sizeof(zIdxStr));
200042
200043 /* Check if there exists a MATCH constraint - even an unusable one. If there
200044 ** is, do not consider the lookup-by-rowid plan as using such a plan would
200045 ** require the VDBE to evaluate the MATCH constraint, which is not currently
200046 ** possible. */
200047 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
200048 if( pIdxInfo->aConstraint[ii].op==SQLITE_INDEX_CONSTRAINT_MATCH64 ){
200049 bMatch = 1;
200050 }
200051 }
200052
200053 assert( pIdxInfo->idxStr==0 )((void) (0));
200054 for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
200055 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
200056
200057 if( bMatch==0 && p->usable
200058 && p->iColumn<=0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ2
200059 ){
200060 /* We have an equality constraint on the rowid. Use strategy 1. */
200061 int jj;
200062 for(jj=0; jj<ii; jj++){
200063 pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
200064 pIdxInfo->aConstraintUsage[jj].omit = 0;
200065 }
200066 pIdxInfo->idxNum = 1;
200067 pIdxInfo->aConstraintUsage[ii].argvIndex = 1;
200068 pIdxInfo->aConstraintUsage[jj].omit = 1;
200069
200070 /* This strategy involves a two rowid lookups on an B-Tree structures
200071 ** and then a linear search of an R-Tree node. This should be
200072 ** considered almost as quick as a direct rowid lookup (for which
200073 ** sqlite uses an internal cost of 0.0). It is expected to return
200074 ** a single row.
200075 */
200076 pIdxInfo->estimatedCost = 30.0;
200077 pIdxInfo->estimatedRows = 1;
200078 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE1;
200079 return SQLITE_OK0;
200080 }
200081
200082 if( p->usable
200083 && ((p->iColumn>0 && p->iColumn<=pRtree->nDim2)
200084 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH64)
200085 ){
200086 u8 op;
200087 switch( p->op ){
200088 case SQLITE_INDEX_CONSTRAINT_EQ2: op = RTREE_EQ0x41; break;
200089 case SQLITE_INDEX_CONSTRAINT_GT4: op = RTREE_GT0x45; break;
200090 case SQLITE_INDEX_CONSTRAINT_LE8: op = RTREE_LE0x42; break;
200091 case SQLITE_INDEX_CONSTRAINT_LT16: op = RTREE_LT0x43; break;
200092 case SQLITE_INDEX_CONSTRAINT_GE32: op = RTREE_GE0x44; break;
200093 case SQLITE_INDEX_CONSTRAINT_MATCH64: op = RTREE_MATCH0x46; break;
200094 default: op = 0; break;
200095 }
200096 if( op ){
200097 zIdxStr[iIdx++] = op;
200098 zIdxStr[iIdx++] = (char)(p->iColumn - 1 + '0');
200099 pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
200100 pIdxInfo->aConstraintUsage[ii].omit = 1;
200101 }
200102 }
200103 }
200104
200105 pIdxInfo->idxNum = 2;
200106 pIdxInfo->needToFreeIdxStr = 1;
200107 if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){
200108 return SQLITE_NOMEM7;
200109 }
200110
200111 nRow = pRtree->nRowEst >> (iIdx/2);
200112 pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
200113 pIdxInfo->estimatedRows = nRow;
200114
200115 return rc;
200116}
200117
200118/*
200119** Return the N-dimensional volumn of the cell stored in *p.
200120*/
200121static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
200122 RtreeDValue area = (RtreeDValue)1;
200123 assert( pRtree->nDim>=1 && pRtree->nDim<=5 )((void) (0));
200124#ifndef SQLITE_RTREE_INT_ONLY
200125 if( pRtree->eCoordType==RTREE_COORD_REAL320 ){
200126 switch( pRtree->nDim ){
200127 case 5: area = p->aCoord[9].f - p->aCoord[8].f;
200128 case 4: area *= p->aCoord[7].f - p->aCoord[6].f;
200129 case 3: area *= p->aCoord[5].f - p->aCoord[4].f;
200130 case 2: area *= p->aCoord[3].f - p->aCoord[2].f;
200131 default: area *= p->aCoord[1].f - p->aCoord[0].f;
200132 }
200133 }else
200134#endif
200135 {
200136 switch( pRtree->nDim ){
200137 case 5: area = (i64)p->aCoord[9].i - (i64)p->aCoord[8].i;
200138 case 4: area *= (i64)p->aCoord[7].i - (i64)p->aCoord[6].i;
200139 case 3: area *= (i64)p->aCoord[5].i - (i64)p->aCoord[4].i;
200140 case 2: area *= (i64)p->aCoord[3].i - (i64)p->aCoord[2].i;
200141 default: area *= (i64)p->aCoord[1].i - (i64)p->aCoord[0].i;
200142 }
200143 }
200144 return area;
200145}
200146
200147/*
200148** Return the margin length of cell p. The margin length is the sum
200149** of the objects size in each dimension.
200150*/
200151static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
200152 RtreeDValue margin = 0;
200153 int ii = pRtree->nDim2 - 2;
200154 do{
200155 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) )
);
200156 ii -= 2;
200157 }while( ii>=0 );
200158 return margin;
200159}
200160
200161/*
200162** Store the union of cells p1 and p2 in p1.
200163*/
200164static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
200165 int ii = 0;
200166 if( pRtree->eCoordType==RTREE_COORD_REAL320 ){
200167 do{
200168 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))
;
200169 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))
;
200170 ii += 2;
200171 }while( ii<pRtree->nDim2 );
200172 }else{
200173 do{
200174 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))
;
200175 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))
;
200176 ii += 2;
200177 }while( ii<pRtree->nDim2 );
200178 }
200179}
200180
200181/*
200182** Return true if the area covered by p2 is a subset of the area covered
200183** by p1. False otherwise.
200184*/
200185static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
200186 int ii;
200187 int isInt = (pRtree->eCoordType==RTREE_COORD_INT321);
200188 for(ii=0; ii<pRtree->nDim2; ii+=2){
200189 RtreeCoord *a1 = &p1->aCoord[ii];
200190 RtreeCoord *a2 = &p2->aCoord[ii];
200191 if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f))
200192 || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i))
200193 ){
200194 return 0;
200195 }
200196 }
200197 return 1;
200198}
200199
200200/*
200201** Return the amount cell p would grow by if it were unioned with pCell.
200202*/
200203static RtreeDValue cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
200204 RtreeDValue area;
200205 RtreeCell cell;
200206 memcpy(&cell, p, sizeof(RtreeCell));
200207 area = cellArea(pRtree, &cell);
200208 cellUnion(pRtree, &cell, pCell);
200209 return (cellArea(pRtree, &cell)-area);
200210}
200211
200212static RtreeDValue cellOverlap(
200213 Rtree *pRtree,
200214 RtreeCell *p,
200215 RtreeCell *aCell,
200216 int nCell
200217){
200218 int ii;
200219 RtreeDValue overlap = RTREE_ZERO0.0;
200220 for(ii=0; ii<nCell; ii++){
200221 int jj;
200222 RtreeDValue o = (RtreeDValue)1;
200223 for(jj=0; jj<pRtree->nDim2; jj+=2){
200224 RtreeDValue x1, x2;
200225 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) )))
;
200226 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) )))
;
200227 if( x2<x1 ){
200228 o = (RtreeDValue)0;
200229 break;
200230 }else{
200231 o = o * (x2-x1);
200232 }
200233 }
200234 overlap += o;
200235 }
200236 return overlap;
200237}
200238
200239
200240/*
200241** This function implements the ChooseLeaf algorithm from Gutman[84].
200242** ChooseSubTree in r*tree terminology.
200243*/
200244static int ChooseLeaf(
200245 Rtree *pRtree, /* Rtree table */
200246 RtreeCell *pCell, /* Cell to insert into rtree */
200247 int iHeight, /* Height of sub-tree rooted at pCell */
200248 RtreeNode **ppLeaf /* OUT: Selected leaf page */
200249){
200250 int rc;
200251 int ii;
200252 RtreeNode *pNode = 0;
200253 rc = nodeAcquire(pRtree, 1, 0, &pNode);
200254
200255 for(ii=0; rc==SQLITE_OK0 && ii<(pRtree->iDepth-iHeight); ii++){
200256 int iCell;
200257 sqlite3_int64 iBest = 0;
200258
200259 RtreeDValue fMinGrowth = RTREE_ZERO0.0;
200260 RtreeDValue fMinArea = RTREE_ZERO0.0;
200261
200262 int nCell = NCELL(pNode)readInt16(&(pNode)->zData[2]);
200263 RtreeCell cell;
200264 RtreeNode *pChild = 0;
200265
200266 RtreeCell *aCell = 0;
200267
200268 /* Select the child node which will be enlarged the least if pCell
200269 ** is inserted into it. Resolve ties by choosing the entry with
200270 ** the smallest area.
200271 */
200272 for(iCell=0; iCell<nCell; iCell++){
200273 int bBest = 0;
200274 RtreeDValue growth;
200275 RtreeDValue area;
200276 nodeGetCell(pRtree, pNode, iCell, &cell);
200277 growth = cellGrowth(pRtree, &cell, pCell);
200278 area = cellArea(pRtree, &cell);
200279 if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){
200280 bBest = 1;
200281 }
200282 if( bBest ){
200283 fMinGrowth = growth;
200284 fMinArea = area;
200285 iBest = cell.iRowid;
200286 }
200287 }
200288
200289 sqlite3_free(aCell);
200290 rc = nodeAcquire(pRtree, iBest, pNode, &pChild);
200291 nodeRelease(pRtree, pNode);
200292 pNode = pChild;
200293 }
200294
200295 *ppLeaf = pNode;
200296 return rc;
200297}
200298
200299/*
200300** A cell with the same content as pCell has just been inserted into
200301** the node pNode. This function updates the bounding box cells in
200302** all ancestor elements.
200303*/
200304static int AdjustTree(
200305 Rtree *pRtree, /* Rtree table */
200306 RtreeNode *pNode, /* Adjust ancestry of this node. */
200307 RtreeCell *pCell /* This cell was just inserted */
200308){
200309 RtreeNode *p = pNode;
200310 int cnt = 0;
200311 int rc;
200312 while( p->pParent ){
200313 RtreeNode *pParent = p->pParent;
200314 RtreeCell cell;
200315 int iCell;
200316
200317 cnt++;
200318 if( NEVER(cnt>100)(cnt>100) ){
200319 RTREE_IS_CORRUPT(pRtree);
200320 return SQLITE_CORRUPT_VTAB(11 | (1<<8));
200321 }
200322 rc = nodeParentIndex(pRtree, p, &iCell);
200323 if( NEVER(rc!=SQLITE_OK)(rc!=0) ){
200324 RTREE_IS_CORRUPT(pRtree);
200325 return SQLITE_CORRUPT_VTAB(11 | (1<<8));
200326 }
200327
200328 nodeGetCell(pRtree, pParent, iCell, &cell);
200329 if( !cellContains(pRtree, &cell, pCell) ){
200330 cellUnion(pRtree, &cell, pCell);
200331 nodeOverwriteCell(pRtree, pParent, &cell, iCell);
200332 }
200333
200334 p = pParent;
200335 }
200336 return SQLITE_OK0;
200337}
200338
200339/*
200340** Write mapping (iRowid->iNode) to the <rtree>_rowid table.
200341*/
200342static int rowidWrite(Rtree *pRtree, sqlite3_int64 iRowid, sqlite3_int64 iNode){
200343 sqlite3_bind_int64(pRtree->pWriteRowid, 1, iRowid);
200344 sqlite3_bind_int64(pRtree->pWriteRowid, 2, iNode);
200345 sqlite3_step(pRtree->pWriteRowid);
200346 return sqlite3_reset(pRtree->pWriteRowid);
200347}
200348
200349/*
200350** Write mapping (iNode->iPar) to the <rtree>_parent table.
200351*/
200352static int parentWrite(Rtree *pRtree, sqlite3_int64 iNode, sqlite3_int64 iPar){
200353 sqlite3_bind_int64(pRtree->pWriteParent, 1, iNode);
200354 sqlite3_bind_int64(pRtree->pWriteParent, 2, iPar);
200355 sqlite3_step(pRtree->pWriteParent);
200356 return sqlite3_reset(pRtree->pWriteParent);
200357}
200358
200359static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int);
200360
200361
200362/*
200363** Arguments aIdx, aDistance and aSpare all point to arrays of size
200364** nIdx. The aIdx array contains the set of integers from 0 to
200365** (nIdx-1) in no particular order. This function sorts the values
200366** in aIdx according to the indexed values in aDistance. For
200367** example, assuming the inputs:
200368**
200369** aIdx = { 0, 1, 2, 3 }
200370** aDistance = { 5.0, 2.0, 7.0, 6.0 }
200371**
200372** this function sets the aIdx array to contain:
200373**
200374** aIdx = { 0, 1, 2, 3 }
200375**
200376** The aSpare array is used as temporary working space by the
200377** sorting algorithm.
200378*/
200379static void SortByDistance(
200380 int *aIdx,
200381 int nIdx,
200382 RtreeDValue *aDistance,
200383 int *aSpare
200384){
200385 if( nIdx>1 ){
200386 int iLeft = 0;
200387 int iRight = 0;
200388
200389 int nLeft = nIdx/2;
200390 int nRight = nIdx-nLeft;
200391 int *aLeft = aIdx;
200392 int *aRight = &aIdx[nLeft];
200393
200394 SortByDistance(aLeft, nLeft, aDistance, aSpare);
200395 SortByDistance(aRight, nRight, aDistance, aSpare);
200396
200397 memcpy(aSpare, aLeft, sizeof(int)*nLeft);
200398 aLeft = aSpare;
200399
200400 while( iLeft<nLeft || iRight<nRight ){
200401 if( iLeft==nLeft ){
200402 aIdx[iLeft+iRight] = aRight[iRight];
200403 iRight++;
200404 }else if( iRight==nRight ){
200405 aIdx[iLeft+iRight] = aLeft[iLeft];
200406 iLeft++;
200407 }else{
200408 RtreeDValue fLeft = aDistance[aLeft[iLeft]];
200409 RtreeDValue fRight = aDistance[aRight[iRight]];
200410 if( fLeft<fRight ){
200411 aIdx[iLeft+iRight] = aLeft[iLeft];
200412 iLeft++;
200413 }else{
200414 aIdx[iLeft+iRight] = aRight[iRight];
200415 iRight++;
200416 }
200417 }
200418 }
200419
200420#if 0
200421 /* Check that the sort worked */
200422 {
200423 int jj;
200424 for(jj=1; jj<nIdx; jj++){
200425 RtreeDValue left = aDistance[aIdx[jj-1]];
200426 RtreeDValue right = aDistance[aIdx[jj]];
200427 assert( left<=right )((void) (0));
200428 }
200429 }
200430#endif
200431 }
200432}
200433
200434/*
200435** Arguments aIdx, aCell and aSpare all point to arrays of size
200436** nIdx. The aIdx array contains the set of integers from 0 to
200437** (nIdx-1) in no particular order. This function sorts the values
200438** in aIdx according to dimension iDim of the cells in aCell. The
200439** minimum value of dimension iDim is considered first, the
200440** maximum used to break ties.
200441**
200442** The aSpare array is used as temporary working space by the
200443** sorting algorithm.
200444*/
200445static void SortByDimension(
200446 Rtree *pRtree,
200447 int *aIdx,
200448 int nIdx,
200449 int iDim,
200450 RtreeCell *aCell,
200451 int *aSpare
200452){
200453 if( nIdx>1 ){
200454
200455 int iLeft = 0;
200456 int iRight = 0;
200457
200458 int nLeft = nIdx/2;
200459 int nRight = nIdx-nLeft;
200460 int *aLeft = aIdx;
200461 int *aRight = &aIdx[nLeft];
200462
200463 SortByDimension(pRtree, aLeft, nLeft, iDim, aCell, aSpare);
200464 SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare);
200465
200466 memcpy(aSpare, aLeft, sizeof(int)*nLeft);
200467 aLeft = aSpare;
200468 while( iLeft<nLeft || iRight<nRight ){
200469 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) )
;
200470 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) )
;
200471 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) )
;
200472 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) )
;
200473 if( (iLeft!=nLeft) && ((iRight==nRight)
200474 || (xleft1<xright1)
200475 || (xleft1==xright1 && xleft2<xright2)
200476 )){
200477 aIdx[iLeft+iRight] = aLeft[iLeft];
200478 iLeft++;
200479 }else{
200480 aIdx[iLeft+iRight] = aRight[iRight];
200481 iRight++;
200482 }
200483 }
200484
200485#if 0
200486 /* Check that the sort worked */
200487 {
200488 int jj;
200489 for(jj=1; jj<nIdx; jj++){
200490 RtreeDValue xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
200491 RtreeDValue xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
200492 RtreeDValue xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
200493 RtreeDValue xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
200494 assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) )((void) (0));
200495 }
200496 }
200497#endif
200498 }
200499}
200500
200501/*
200502** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
200503*/
200504static int splitNodeStartree(
200505 Rtree *pRtree,
200506 RtreeCell *aCell,
200507 int nCell,
200508 RtreeNode *pLeft,
200509 RtreeNode *pRight,
200510 RtreeCell *pBboxLeft,
200511 RtreeCell *pBboxRight
200512){
200513 int **aaSorted;
200514 int *aSpare;
200515 int ii;
200516
200517 int iBestDim = 0;
200518 int iBestSplit = 0;
200519 RtreeDValue fBestMargin = RTREE_ZERO0.0;
200520
200521 sqlite3_int64 nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
200522
200523 aaSorted = (int **)sqlite3_malloc64(nByte);
200524 if( !aaSorted ){
200525 return SQLITE_NOMEM7;
200526 }
200527
200528 aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell];
200529 memset(aaSorted, 0, nByte);
200530 for(ii=0; ii<pRtree->nDim; ii++){
200531 int jj;
200532 aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell];
200533 for(jj=0; jj<nCell; jj++){
200534 aaSorted[ii][jj] = jj;
200535 }
200536 SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
200537 }
200538
200539 for(ii=0; ii<pRtree->nDim; ii++){
200540 RtreeDValue margin = RTREE_ZERO0.0;
200541 RtreeDValue fBestOverlap = RTREE_ZERO0.0;
200542 RtreeDValue fBestArea = RTREE_ZERO0.0;
200543 int iBestLeft = 0;
200544 int nLeft;
200545
200546 for(
200547 nLeft=RTREE_MINCELLS(pRtree)((((pRtree)->iNodeSize-4)/(pRtree)->nBytesPerCell)/3);
200548 nLeft<=(nCell-RTREE_MINCELLS(pRtree)((((pRtree)->iNodeSize-4)/(pRtree)->nBytesPerCell)/3));
200549 nLeft++
200550 ){
200551 RtreeCell left;
200552 RtreeCell right;
200553 int kk;
200554 RtreeDValue overlap;
200555 RtreeDValue area;
200556
200557 memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell));
200558 memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
200559 for(kk=1; kk<(nCell-1); kk++){
200560 if( kk<nLeft ){
200561 cellUnion(pRtree, &left, &aCell[aaSorted[ii][kk]]);
200562 }else{
200563 cellUnion(pRtree, &right, &aCell[aaSorted[ii][kk]]);
200564 }
200565 }
200566 margin += cellMargin(pRtree, &left);
200567 margin += cellMargin(pRtree, &right);
200568 overlap = cellOverlap(pRtree, &left, &right, 1);
200569 area = cellArea(pRtree, &left) + cellArea(pRtree, &right);
200570 if( (nLeft==RTREE_MINCELLS(pRtree)((((pRtree)->iNodeSize-4)/(pRtree)->nBytesPerCell)/3))
200571 || (overlap<fBestOverlap)
200572 || (overlap==fBestOverlap && area<fBestArea)
200573 ){
200574 iBestLeft = nLeft;
200575 fBestOverlap = overlap;
200576 fBestArea = area;
200577 }
200578 }
200579
200580 if( ii==0 || margin<fBestMargin ){
200581 iBestDim = ii;
200582 fBestMargin = margin;
200583 iBestSplit = iBestLeft;
200584 }
200585 }
200586
200587 memcpy(pBboxLeft, &aCell[aaSorted[iBestDim][0]], sizeof(RtreeCell));
200588 memcpy(pBboxRight, &aCell[aaSorted[iBestDim][iBestSplit]], sizeof(RtreeCell));
200589 for(ii=0; ii<nCell; ii++){
200590 RtreeNode *pTarget = (ii<iBestSplit)?pLeft:pRight;
200591 RtreeCell *pBbox = (ii<iBestSplit)?pBboxLeft:pBboxRight;
200592 RtreeCell *pCell = &aCell[aaSorted[iBestDim][ii]];
200593 nodeInsertCell(pRtree, pTarget, pCell);
200594 cellUnion(pRtree, pBbox, pCell);
200595 }
200596
200597 sqlite3_free(aaSorted);
200598 return SQLITE_OK0;
200599}
200600
200601
200602static int updateMapping(
200603 Rtree *pRtree,
200604 i64 iRowid,
200605 RtreeNode *pNode,
200606 int iHeight
200607){
200608 int (*xSetMapping)(Rtree *, sqlite3_int64, sqlite3_int64);
200609 xSetMapping = ((iHeight==0)?rowidWrite:parentWrite);
200610 if( iHeight>0 ){
200611 RtreeNode *pChild = nodeHashLookup(pRtree, iRowid);
200612 RtreeNode *p;
200613 for(p=pNode; p; p=p->pParent){
200614 if( p==pChild ) return SQLITE_CORRUPT_VTAB(11 | (1<<8));
200615 }
200616 if( pChild ){
200617 nodeRelease(pRtree, pChild->pParent);
200618 nodeReference(pNode);
200619 pChild->pParent = pNode;
200620 }
200621 }
200622 if( NEVER(pNode==0)(pNode==0) ) return SQLITE_ERROR1;
200623 return xSetMapping(pRtree, iRowid, pNode->iNode);
200624}
200625
200626static int SplitNode(
200627 Rtree *pRtree,
200628 RtreeNode *pNode,
200629 RtreeCell *pCell,
200630 int iHeight
200631){
200632 int i;
200633 int newCellIsRight = 0;
200634
200635 int rc = SQLITE_OK0;
200636 int nCell = NCELL(pNode)readInt16(&(pNode)->zData[2]);
200637 RtreeCell *aCell;
200638 int *aiUsed;
200639
200640 RtreeNode *pLeft = 0;
200641 RtreeNode *pRight = 0;
200642
200643 RtreeCell leftbbox;
200644 RtreeCell rightbbox;
200645
200646 /* Allocate an array and populate it with a copy of pCell and
200647 ** all cells from node pLeft. Then zero the original node.
200648 */
200649 aCell = sqlite3_malloc64((sizeof(RtreeCell)+sizeof(int))*(nCell+1));
200650 if( !aCell ){
200651 rc = SQLITE_NOMEM7;
200652 goto splitnode_out;
200653 }
200654 aiUsed = (int *)&aCell[nCell+1];
200655 memset(aiUsed, 0, sizeof(int)*(nCell+1));
200656 for(i=0; i<nCell; i++){
200657 nodeGetCell(pRtree, pNode, i, &aCell[i]);
200658 }
200659 nodeZero(pRtree, pNode);
200660 memcpy(&aCell[nCell], pCell, sizeof(RtreeCell));
200661 nCell++;
200662
200663 if( pNode->iNode==1 ){
200664 pRight = nodeNew(pRtree, pNode);
200665 pLeft = nodeNew(pRtree, pNode);
200666 pRtree->iDepth++;
200667 pNode->isDirty = 1;
200668 writeInt16(pNode->zData, pRtree->iDepth);
200669 }else{
200670 pLeft = pNode;
200671 pRight = nodeNew(pRtree, pLeft->pParent);
200672 pLeft->nRef++;
200673 }
200674
200675 if( !pLeft || !pRight ){
200676 rc = SQLITE_NOMEM7;
200677 goto splitnode_out;
200678 }
200679
200680 memset(pLeft->zData, 0, pRtree->iNodeSize);
200681 memset(pRight->zData, 0, pRtree->iNodeSize);
200682
200683 rc = splitNodeStartree(pRtree, aCell, nCell, pLeft, pRight,
200684 &leftbbox, &rightbbox);
200685 if( rc!=SQLITE_OK0 ){
200686 goto splitnode_out;
200687 }
200688
200689 /* Ensure both child nodes have node numbers assigned to them by calling
200690 ** nodeWrite(). Node pRight always needs a node number, as it was created
200691 ** by nodeNew() above. But node pLeft sometimes already has a node number.
200692 ** In this case avoid the all to nodeWrite().
200693 */
200694 if( SQLITE_OK0!=(rc = nodeWrite(pRtree, pRight))
200695 || (0==pLeft->iNode && SQLITE_OK0!=(rc = nodeWrite(pRtree, pLeft)))
200696 ){
200697 goto splitnode_out;
200698 }
200699
200700 rightbbox.iRowid = pRight->iNode;
200701 leftbbox.iRowid = pLeft->iNode;
200702
200703 if( pNode->iNode==1 ){
200704 rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1);
200705 if( rc!=SQLITE_OK0 ){
200706 goto splitnode_out;
200707 }
200708 }else{
200709 RtreeNode *pParent = pLeft->pParent;
200710 int iCell;
200711 rc = nodeParentIndex(pRtree, pLeft, &iCell);
200712 if( ALWAYS(rc==SQLITE_OK)(rc==0) ){
200713 nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell);
200714 rc = AdjustTree(pRtree, pParent, &leftbbox);
200715 assert( rc==SQLITE_OK )((void) (0));
200716 }
200717 if( NEVER(rc!=SQLITE_OK)(rc!=0) ){
200718 goto splitnode_out;
200719 }
200720 }
200721 if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){
200722 goto splitnode_out;
200723 }
200724
200725 for(i=0; i<NCELL(pRight)readInt16(&(pRight)->zData[2]); i++){
200726 i64 iRowid = nodeGetRowid(pRtree, pRight, i);
200727 rc = updateMapping(pRtree, iRowid, pRight, iHeight);
200728 if( iRowid==pCell->iRowid ){
200729 newCellIsRight = 1;
200730 }
200731 if( rc!=SQLITE_OK0 ){
200732 goto splitnode_out;
200733 }
200734 }
200735 if( pNode->iNode==1 ){
200736 for(i=0; i<NCELL(pLeft)readInt16(&(pLeft)->zData[2]); i++){
200737 i64 iRowid = nodeGetRowid(pRtree, pLeft, i);
200738 rc = updateMapping(pRtree, iRowid, pLeft, iHeight);
200739 if( rc!=SQLITE_OK0 ){
200740 goto splitnode_out;
200741 }
200742 }
200743 }else if( newCellIsRight==0 ){
200744 rc = updateMapping(pRtree, pCell->iRowid, pLeft, iHeight);
200745 }
200746
200747 if( rc==SQLITE_OK0 ){
200748 rc = nodeRelease(pRtree, pRight);
200749 pRight = 0;
200750 }
200751 if( rc==SQLITE_OK0 ){
200752 rc = nodeRelease(pRtree, pLeft);
200753 pLeft = 0;
200754 }
200755
200756splitnode_out:
200757 nodeRelease(pRtree, pRight);
200758 nodeRelease(pRtree, pLeft);
200759 sqlite3_free(aCell);
200760 return rc;
200761}
200762
200763/*
200764** If node pLeaf is not the root of the r-tree and its pParent pointer is
200765** still NULL, load all ancestor nodes of pLeaf into memory and populate
200766** the pLeaf->pParent chain all the way up to the root node.
200767**
200768** This operation is required when a row is deleted (or updated - an update
200769** is implemented as a delete followed by an insert). SQLite provides the
200770** rowid of the row to delete, which can be used to find the leaf on which
200771** the entry resides (argument pLeaf). Once the leaf is located, this
200772** function is called to determine its ancestry.
200773*/
200774static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){
200775 int rc = SQLITE_OK0;
200776 RtreeNode *pChild = pLeaf;
200777 while( rc==SQLITE_OK0 && pChild->iNode!=1 && pChild->pParent==0 ){
200778 int rc2 = SQLITE_OK0; /* sqlite3_reset() return code */
200779 sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode);
200780 rc = sqlite3_step(pRtree->pReadParent);
200781 if( rc==SQLITE_ROW100 ){
200782 RtreeNode *pTest; /* Used to test for reference loops */
200783 i64 iNode; /* Node number of parent node */
200784
200785 /* Before setting pChild->pParent, test that we are not creating a
200786 ** loop of references (as we would if, say, pChild==pParent). We don't
200787 ** want to do this as it leads to a memory leak when trying to delete
200788 ** the referenced counted node structures.
200789 */
200790 iNode = sqlite3_column_int64(pRtree->pReadParent, 0);
200791 for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent);
200792 if( pTest==0 ){
200793 rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent);
200794 }
200795 }
200796 rc = sqlite3_reset(pRtree->pReadParent);
200797 if( rc==SQLITE_OK0 ) rc = rc2;
200798 if( rc==SQLITE_OK0 && !pChild->pParent ){
200799 RTREE_IS_CORRUPT(pRtree);
200800 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
200801 }
200802 pChild = pChild->pParent;
200803 }
200804 return rc;
200805}
200806
200807static int deleteCell(Rtree *, RtreeNode *, int, int);
200808
200809static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){
200810 int rc;
200811 int rc2;
200812 RtreeNode *pParent = 0;
200813 int iCell;
200814
200815 assert( pNode->nRef==1 )((void) (0));
200816
200817 /* Remove the entry in the parent cell. */
200818 rc = nodeParentIndex(pRtree, pNode, &iCell);
200819 if( rc==SQLITE_OK0 ){
200820 pParent = pNode->pParent;
200821 pNode->pParent = 0;
200822 rc = deleteCell(pRtree, pParent, iCell, iHeight+1);
200823 testcase( rc!=SQLITE_OK );
200824 }
200825 rc2 = nodeRelease(pRtree, pParent);
200826 if( rc==SQLITE_OK0 ){
200827 rc = rc2;
200828 }
200829 if( rc!=SQLITE_OK0 ){
200830 return rc;
200831 }
200832
200833 /* Remove the xxx_node entry. */
200834 sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode);
200835 sqlite3_step(pRtree->pDeleteNode);
200836 if( SQLITE_OK0!=(rc = sqlite3_reset(pRtree->pDeleteNode)) ){
200837 return rc;
200838 }
200839
200840 /* Remove the xxx_parent entry. */
200841 sqlite3_bind_int64(pRtree->pDeleteParent, 1, pNode->iNode);
200842 sqlite3_step(pRtree->pDeleteParent);
200843 if( SQLITE_OK0!=(rc = sqlite3_reset(pRtree->pDeleteParent)) ){
200844 return rc;
200845 }
200846
200847 /* Remove the node from the in-memory hash table and link it into
200848 ** the Rtree.pDeleted list. Its contents will be re-inserted later on.
200849 */
200850 nodeHashDelete(pRtree, pNode);
200851 pNode->iNode = iHeight;
200852 pNode->pNext = pRtree->pDeleted;
200853 pNode->nRef++;
200854 pRtree->pDeleted = pNode;
200855
200856 return SQLITE_OK0;
200857}
200858
200859static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){
200860 RtreeNode *pParent = pNode->pParent;
200861 int rc = SQLITE_OK0;
200862 if( pParent ){
200863 int ii;
200864 int nCell = NCELL(pNode)readInt16(&(pNode)->zData[2]);
200865 RtreeCell box; /* Bounding box for pNode */
200866 nodeGetCell(pRtree, pNode, 0, &box);
200867 for(ii=1; ii<nCell; ii++){
200868 RtreeCell cell;
200869 nodeGetCell(pRtree, pNode, ii, &cell);
200870 cellUnion(pRtree, &box, &cell);
200871 }
200872 box.iRowid = pNode->iNode;
200873 rc = nodeParentIndex(pRtree, pNode, &ii);
200874 if( rc==SQLITE_OK0 ){
200875 nodeOverwriteCell(pRtree, pParent, &box, ii);
200876 rc = fixBoundingBox(pRtree, pParent);
200877 }
200878 }
200879 return rc;
200880}
200881
200882/*
200883** Delete the cell at index iCell of node pNode. After removing the
200884** cell, adjust the r-tree data structure if required.
200885*/
200886static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){
200887 RtreeNode *pParent;
200888 int rc;
200889
200890 if( SQLITE_OK0!=(rc = fixLeafParent(pRtree, pNode)) ){
200891 return rc;
200892 }
200893
200894 /* Remove the cell from the node. This call just moves bytes around
200895 ** the in-memory node image, so it cannot fail.
200896 */
200897 nodeDeleteCell(pRtree, pNode, iCell);
200898
200899 /* If the node is not the tree root and now has less than the minimum
200900 ** number of cells, remove it from the tree. Otherwise, update the
200901 ** cell in the parent node so that it tightly contains the updated
200902 ** node.
200903 */
200904 pParent = pNode->pParent;
200905 assert( pParent || pNode->iNode==1 )((void) (0));
200906 if( pParent ){
200907 if( NCELL(pNode)readInt16(&(pNode)->zData[2])<RTREE_MINCELLS(pRtree)((((pRtree)->iNodeSize-4)/(pRtree)->nBytesPerCell)/3) ){
200908 rc = removeNode(pRtree, pNode, iHeight);
200909 }else{
200910 rc = fixBoundingBox(pRtree, pNode);
200911 }
200912 }
200913
200914 return rc;
200915}
200916
200917static int Reinsert(
200918 Rtree *pRtree,
200919 RtreeNode *pNode,
200920 RtreeCell *pCell,
200921 int iHeight
200922){
200923 int *aOrder;
200924 int *aSpare;
200925 RtreeCell *aCell;
200926 RtreeDValue *aDistance;
200927 int nCell;
200928 RtreeDValue aCenterCoord[RTREE_MAX_DIMENSIONS5];
200929 int iDim;
200930 int ii;
200931 int rc = SQLITE_OK0;
200932 int n;
200933
200934 memset(aCenterCoord, 0, sizeof(RtreeDValue)*RTREE_MAX_DIMENSIONS5);
200935
200936 nCell = NCELL(pNode)readInt16(&(pNode)->zData[2])+1;
200937 n = (nCell+1)&(~1);
200938
200939 /* Allocate the buffers used by this operation. The allocation is
200940 ** relinquished before this function returns.
200941 */
200942 aCell = (RtreeCell *)sqlite3_malloc64(n * (
200943 sizeof(RtreeCell) + /* aCell array */
200944 sizeof(int) + /* aOrder array */
200945 sizeof(int) + /* aSpare array */
200946 sizeof(RtreeDValue) /* aDistance array */
200947 ));
200948 if( !aCell ){
200949 return SQLITE_NOMEM7;
200950 }
200951 aOrder = (int *)&aCell[n];
200952 aSpare = (int *)&aOrder[n];
200953 aDistance = (RtreeDValue *)&aSpare[n];
200954
200955 for(ii=0; ii<nCell; ii++){
200956 if( ii==(nCell-1) ){
200957 memcpy(&aCell[ii], pCell, sizeof(RtreeCell));
200958 }else{
200959 nodeGetCell(pRtree, pNode, ii, &aCell[ii]);
200960 }
200961 aOrder[ii] = ii;
200962 for(iDim=0; iDim<pRtree->nDim; iDim++){
200963 aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2])( (pRtree->eCoordType==0) ? ((double)aCell[ii].aCoord[iDim
*2].f) : ((double)aCell[ii].aCoord[iDim*2].i) )
;
200964 aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2+1])( (pRtree->eCoordType==0) ? ((double)aCell[ii].aCoord[iDim
*2+1].f) : ((double)aCell[ii].aCoord[iDim*2+1].i) )
;
200965 }
200966 }
200967 for(iDim=0; iDim<pRtree->nDim; iDim++){
200968 aCenterCoord[iDim] = (aCenterCoord[iDim]/(nCell*(RtreeDValue)2));
200969 }
200970
200971 for(ii=0; ii<nCell; ii++){
200972 aDistance[ii] = RTREE_ZERO0.0;
200973 for(iDim=0; iDim<pRtree->nDim; iDim++){
200974 RtreeDValue coord = (DCOORD(aCell[ii].aCoord[iDim*2+1])( (pRtree->eCoordType==0) ? ((double)aCell[ii].aCoord[iDim
*2+1].f) : ((double)aCell[ii].aCoord[iDim*2+1].i) )
-
200975 DCOORD(aCell[ii].aCoord[iDim*2])( (pRtree->eCoordType==0) ? ((double)aCell[ii].aCoord[iDim
*2].f) : ((double)aCell[ii].aCoord[iDim*2].i) )
);
200976 aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
200977 }
200978 }
200979
200980 SortByDistance(aOrder, nCell, aDistance, aSpare);
200981 nodeZero(pRtree, pNode);
200982
200983 for(ii=0; rc==SQLITE_OK0 && ii<(nCell-(RTREE_MINCELLS(pRtree)((((pRtree)->iNodeSize-4)/(pRtree)->nBytesPerCell)/3)+1)); ii++){
200984 RtreeCell *p = &aCell[aOrder[ii]];
200985 nodeInsertCell(pRtree, pNode, p);
200986 if( p->iRowid==pCell->iRowid ){
200987 if( iHeight==0 ){
200988 rc = rowidWrite(pRtree, p->iRowid, pNode->iNode);
200989 }else{
200990 rc = parentWrite(pRtree, p->iRowid, pNode->iNode);
200991 }
200992 }
200993 }
200994 if( rc==SQLITE_OK0 ){
200995 rc = fixBoundingBox(pRtree, pNode);
200996 }
200997 for(; rc==SQLITE_OK0 && ii<nCell; ii++){
200998 /* Find a node to store this cell in. pNode->iNode currently contains
200999 ** the height of the sub-tree headed by the cell.
201000 */
201001 RtreeNode *pInsert;
201002 RtreeCell *p = &aCell[aOrder[ii]];
201003 rc = ChooseLeaf(pRtree, p, iHeight, &pInsert);
201004 if( rc==SQLITE_OK0 ){
201005 int rc2;
201006 rc = rtreeInsertCell(pRtree, pInsert, p, iHeight);
201007 rc2 = nodeRelease(pRtree, pInsert);
201008 if( rc==SQLITE_OK0 ){
201009 rc = rc2;
201010 }
201011 }
201012 }
201013
201014 sqlite3_free(aCell);
201015 return rc;
201016}
201017
201018/*
201019** Insert cell pCell into node pNode. Node pNode is the head of a
201020** subtree iHeight high (leaf nodes have iHeight==0).
201021*/
201022static int rtreeInsertCell(
201023 Rtree *pRtree,
201024 RtreeNode *pNode,
201025 RtreeCell *pCell,
201026 int iHeight
201027){
201028 int rc = SQLITE_OK0;
201029 if( iHeight>0 ){
201030 RtreeNode *pChild = nodeHashLookup(pRtree, pCell->iRowid);
201031 if( pChild ){
201032 nodeRelease(pRtree, pChild->pParent);
201033 nodeReference(pNode);
201034 pChild->pParent = pNode;
201035 }
201036 }
201037 if( nodeInsertCell(pRtree, pNode, pCell) ){
201038 if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){
201039 rc = SplitNode(pRtree, pNode, pCell, iHeight);
201040 }else{
201041 pRtree->iReinsertHeight = iHeight;
201042 rc = Reinsert(pRtree, pNode, pCell, iHeight);
201043 }
201044 }else{
201045 rc = AdjustTree(pRtree, pNode, pCell);
201046 if( ALWAYS(rc==SQLITE_OK)(rc==0) ){
201047 if( iHeight==0 ){
201048 rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode);
201049 }else{
201050 rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode);
201051 }
201052 }
201053 }
201054 return rc;
201055}
201056
201057static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){
201058 int ii;
201059 int rc = SQLITE_OK0;
201060 int nCell = NCELL(pNode)readInt16(&(pNode)->zData[2]);
201061
201062 for(ii=0; rc==SQLITE_OK0 && ii<nCell; ii++){
201063 RtreeNode *pInsert;
201064 RtreeCell cell;
201065 nodeGetCell(pRtree, pNode, ii, &cell);
201066
201067 /* Find a node to store this cell in. pNode->iNode currently contains
201068 ** the height of the sub-tree headed by the cell.
201069 */
201070 rc = ChooseLeaf(pRtree, &cell, (int)pNode->iNode, &pInsert);
201071 if( rc==SQLITE_OK0 ){
201072 int rc2;
201073 rc = rtreeInsertCell(pRtree, pInsert, &cell, (int)pNode->iNode);
201074 rc2 = nodeRelease(pRtree, pInsert);
201075 if( rc==SQLITE_OK0 ){
201076 rc = rc2;
201077 }
201078 }
201079 }
201080 return rc;
201081}
201082
201083/*
201084** Select a currently unused rowid for a new r-tree record.
201085*/
201086static int rtreeNewRowid(Rtree *pRtree, i64 *piRowid){
201087 int rc;
201088 sqlite3_bind_null(pRtree->pWriteRowid, 1);
201089 sqlite3_bind_null(pRtree->pWriteRowid, 2);
201090 sqlite3_step(pRtree->pWriteRowid);
201091 rc = sqlite3_reset(pRtree->pWriteRowid);
201092 *piRowid = sqlite3_last_insert_rowid(pRtree->db);
201093 return rc;
201094}
201095
201096/*
201097** Remove the entry with rowid=iDelete from the r-tree structure.
201098*/
201099static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
201100 int rc; /* Return code */
201101 RtreeNode *pLeaf = 0; /* Leaf node containing record iDelete */
201102 int iCell; /* Index of iDelete cell in pLeaf */
201103 RtreeNode *pRoot = 0; /* Root node of rtree structure */
201104
201105
201106 /* Obtain a reference to the root node to initialize Rtree.iDepth */
201107 rc = nodeAcquire(pRtree, 1, 0, &pRoot);
201108
201109 /* Obtain a reference to the leaf node that contains the entry
201110 ** about to be deleted.
201111 */
201112 if( rc==SQLITE_OK0 ){
201113 rc = findLeafNode(pRtree, iDelete, &pLeaf, 0);
201114 }
201115
201116#ifdef CORRUPT_DB(sqlite3Config.neverCorrupt==0)
201117 assert( pLeaf!=0 || rc!=SQLITE_OK || CORRUPT_DB )((void) (0));
201118#endif
201119
201120 /* Delete the cell in question from the leaf node. */
201121 if( rc==SQLITE_OK0 && pLeaf ){
201122 int rc2;
201123 rc = nodeRowidIndex(pRtree, pLeaf, iDelete, &iCell);
201124 if( rc==SQLITE_OK0 ){
201125 rc = deleteCell(pRtree, pLeaf, iCell, 0);
201126 }
201127 rc2 = nodeRelease(pRtree, pLeaf);
201128 if( rc==SQLITE_OK0 ){
201129 rc = rc2;
201130 }
201131 }
201132
201133 /* Delete the corresponding entry in the <rtree>_rowid table. */
201134 if( rc==SQLITE_OK0 ){
201135 sqlite3_bind_int64(pRtree->pDeleteRowid, 1, iDelete);
201136 sqlite3_step(pRtree->pDeleteRowid);
201137 rc = sqlite3_reset(pRtree->pDeleteRowid);
201138 }
201139
201140 /* Check if the root node now has exactly one child. If so, remove
201141 ** it, schedule the contents of the child for reinsertion and
201142 ** reduce the tree height by one.
201143 **
201144 ** This is equivalent to copying the contents of the child into
201145 ** the root node (the operation that Gutman's paper says to perform
201146 ** in this scenario).
201147 */
201148 if( rc==SQLITE_OK0 && pRtree->iDepth>0 && NCELL(pRoot)readInt16(&(pRoot)->zData[2])==1 ){
201149 int rc2;
201150 RtreeNode *pChild = 0;
201151 i64 iChild = nodeGetRowid(pRtree, pRoot, 0);
201152 rc = nodeAcquire(pRtree, iChild, pRoot, &pChild); /* tag-20210916a */
201153 if( rc==SQLITE_OK0 ){
201154 rc = removeNode(pRtree, pChild, pRtree->iDepth-1);
201155 }
201156 rc2 = nodeRelease(pRtree, pChild);
201157 if( rc==SQLITE_OK0 ) rc = rc2;
201158 if( rc==SQLITE_OK0 ){
201159 pRtree->iDepth--;
201160 writeInt16(pRoot->zData, pRtree->iDepth);
201161 pRoot->isDirty = 1;
201162 }
201163 }
201164
201165 /* Re-insert the contents of any underfull nodes removed from the tree. */
201166 for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
201167 if( rc==SQLITE_OK0 ){
201168 rc = reinsertNodeContent(pRtree, pLeaf);
201169 }
201170 pRtree->pDeleted = pLeaf->pNext;
201171 pRtree->nNodeRef--;
201172 sqlite3_free(pLeaf);
201173 }
201174
201175 /* Release the reference to the root node. */
201176 if( rc==SQLITE_OK0 ){
201177 rc = nodeRelease(pRtree, pRoot);
201178 }else{
201179 nodeRelease(pRtree, pRoot);
201180 }
201181
201182 return rc;
201183}
201184
201185/*
201186** Rounding constants for float->double conversion.
201187*/
201188#define RNDTOWARDS(1.0 - 1.0/8388608.0) (1.0 - 1.0/8388608.0) /* Round towards zero */
201189#define RNDAWAY(1.0 + 1.0/8388608.0) (1.0 + 1.0/8388608.0) /* Round away from zero */
201190
201191#if !defined(SQLITE_RTREE_INT_ONLY)
201192/*
201193** Convert an sqlite3_value into an RtreeValue (presumably a float)
201194** while taking care to round toward negative or positive, respectively.
201195*/
201196static RtreeValue rtreeValueDown(sqlite3_value *v){
201197 double d = sqlite3_value_double(v);
201198 float f = (float)d;
201199 if( f>d ){
201200 f = (float)(d*(d<0 ? RNDAWAY(1.0 + 1.0/8388608.0) : RNDTOWARDS(1.0 - 1.0/8388608.0)));
201201 }
201202 return f;
201203}
201204static RtreeValue rtreeValueUp(sqlite3_value *v){
201205 double d = sqlite3_value_double(v);
201206 float f = (float)d;
201207 if( f<d ){
201208 f = (float)(d*(d<0 ? RNDTOWARDS(1.0 - 1.0/8388608.0) : RNDAWAY(1.0 + 1.0/8388608.0)));
201209 }
201210 return f;
201211}
201212#endif /* !defined(SQLITE_RTREE_INT_ONLY) */
201213
201214/*
201215** A constraint has failed while inserting a row into an rtree table.
201216** Assuming no OOM error occurs, this function sets the error message
201217** (at pRtree->base.zErrMsg) to an appropriate value and returns
201218** SQLITE_CONSTRAINT.
201219**
201220** Parameter iCol is the index of the leftmost column involved in the
201221** constraint failure. If it is 0, then the constraint that failed is
201222** the unique constraint on the id column. Otherwise, it is the rtree
201223** (c1<=c2) constraint on columns iCol and iCol+1 that has failed.
201224**
201225** If an OOM occurs, SQLITE_NOMEM is returned instead of SQLITE_CONSTRAINT.
201226*/
201227static int rtreeConstraintError(Rtree *pRtree, int iCol){
201228 sqlite3_stmt *pStmt = 0;
201229 char *zSql;
201230 int rc;
201231
201232 assert( iCol==0 || iCol%2 )((void) (0));
201233 zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", pRtree->zDb, pRtree->zName);
201234 if( zSql ){
201235 rc = sqlite3_prepare_v2(pRtree->db, zSql, -1, &pStmt, 0);
201236 }else{
201237 rc = SQLITE_NOMEM7;
201238 }
201239 sqlite3_free(zSql);
201240
201241 if( rc==SQLITE_OK0 ){
201242 if( iCol==0 ){
201243 const char *zCol = sqlite3_column_name(pStmt, 0);
201244 pRtree->base.zErrMsg = sqlite3_mprintf(
201245 "UNIQUE constraint failed: %s.%s", pRtree->zName, zCol
201246 );
201247 }else{
201248 const char *zCol1 = sqlite3_column_name(pStmt, iCol);
201249 const char *zCol2 = sqlite3_column_name(pStmt, iCol+1);
201250 pRtree->base.zErrMsg = sqlite3_mprintf(
201251 "rtree constraint failed: %s.(%s<=%s)", pRtree->zName, zCol1, zCol2
201252 );
201253 }
201254 }
201255
201256 sqlite3_finalize(pStmt);
201257 return (rc==SQLITE_OK0 ? SQLITE_CONSTRAINT19 : rc);
201258}
201259
201260
201261
201262/*
201263** The xUpdate method for rtree module virtual tables.
201264*/
201265static int rtreeUpdate(
201266 sqlite3_vtab *pVtab,
201267 int nData,
201268 sqlite3_value **aData,
201269 sqlite_int64 *pRowid
201270){
201271 Rtree *pRtree = (Rtree *)pVtab;
201272 int rc = SQLITE_OK0;
201273 RtreeCell cell; /* New cell to insert if nData>1 */
201274 int bHaveRowid = 0; /* Set to 1 after new rowid is determined */
201275
201276 if( pRtree->nNodeRef ){
201277 /* Unable to write to the btree while another cursor is reading from it,
201278 ** since the write might do a rebalance which would disrupt the read
201279 ** cursor. */
201280 return SQLITE_LOCKED_VTAB(6 | (2<<8));
201281 }
201282 rtreeReference(pRtree);
201283 assert(nData>=1)((void) (0));
201284
201285 cell.iRowid = 0; /* Used only to suppress a compiler warning */
201286
201287 /* Constraint handling. A write operation on an r-tree table may return
201288 ** SQLITE_CONSTRAINT for two reasons:
201289 **
201290 ** 1. A duplicate rowid value, or
201291 ** 2. The supplied data violates the "x2>=x1" constraint.
201292 **
201293 ** In the first case, if the conflict-handling mode is REPLACE, then
201294 ** the conflicting row can be removed before proceeding. In the second
201295 ** case, SQLITE_CONSTRAINT must be returned regardless of the
201296 ** conflict-handling mode specified by the user.
201297 */
201298 if( nData>1 ){
201299 int ii;
201300 int nn = nData - 4;
201301
201302 if( nn > pRtree->nDim2 ) nn = pRtree->nDim2;
201303 /* Populate the cell.aCoord[] array. The first coordinate is aData[3].
201304 **
201305 ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
201306 ** with "column" that are interpreted as table constraints.
201307 ** Example: CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
201308 ** This problem was discovered after years of use, so we silently ignore
201309 ** these kinds of misdeclared tables to avoid breaking any legacy.
201310 */
201311
201312#ifndef SQLITE_RTREE_INT_ONLY
201313 if( pRtree->eCoordType==RTREE_COORD_REAL320 ){
201314 for(ii=0; ii<nn; ii+=2){
201315 cell.aCoord[ii].f = rtreeValueDown(aData[ii+3]);
201316 cell.aCoord[ii+1].f = rtreeValueUp(aData[ii+4]);
201317 if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
201318 rc = rtreeConstraintError(pRtree, ii+1);
201319 goto constraint;
201320 }
201321 }
201322 }else
201323#endif
201324 {
201325 for(ii=0; ii<nn; ii+=2){
201326 cell.aCoord[ii].i = sqlite3_value_int(aData[ii+3]);
201327 cell.aCoord[ii+1].i = sqlite3_value_int(aData[ii+4]);
201328 if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
201329 rc = rtreeConstraintError(pRtree, ii+1);
201330 goto constraint;
201331 }
201332 }
201333 }
201334
201335 /* If a rowid value was supplied, check if it is already present in
201336 ** the table. If so, the constraint has failed. */
201337 if( sqlite3_value_type(aData[2])!=SQLITE_NULL5 ){
201338 cell.iRowid = sqlite3_value_int64(aData[2]);
201339 if( sqlite3_value_type(aData[0])==SQLITE_NULL5
201340 || sqlite3_value_int64(aData[0])!=cell.iRowid
201341 ){
201342 int steprc;
201343 sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
201344 steprc = sqlite3_step(pRtree->pReadRowid);
201345 rc = sqlite3_reset(pRtree->pReadRowid);
201346 if( SQLITE_ROW100==steprc ){
201347 if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE5 ){
201348 rc = rtreeDeleteRowid(pRtree, cell.iRowid);
201349 }else{
201350 rc = rtreeConstraintError(pRtree, 0);
201351 goto constraint;
201352 }
201353 }
201354 }
201355 bHaveRowid = 1;
201356 }
201357 }
201358
201359 /* If aData[0] is not an SQL NULL value, it is the rowid of a
201360 ** record to delete from the r-tree table. The following block does
201361 ** just that.
201362 */
201363 if( sqlite3_value_type(aData[0])!=SQLITE_NULL5 ){
201364 rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(aData[0]));
201365 }
201366
201367 /* If the aData[] array contains more than one element, elements
201368 ** (aData[2]..aData[argc-1]) contain a new record to insert into
201369 ** the r-tree structure.
201370 */
201371 if( rc==SQLITE_OK0 && nData>1 ){
201372 /* Insert the new record into the r-tree */
201373 RtreeNode *pLeaf = 0;
201374
201375 /* Figure out the rowid of the new row. */
201376 if( bHaveRowid==0 ){
201377 rc = rtreeNewRowid(pRtree, &cell.iRowid);
201378 }
201379 *pRowid = cell.iRowid;
201380
201381 if( rc==SQLITE_OK0 ){
201382 rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
201383 }
201384 if( rc==SQLITE_OK0 ){
201385 int rc2;
201386 pRtree->iReinsertHeight = -1;
201387 rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
201388 rc2 = nodeRelease(pRtree, pLeaf);
201389 if( rc==SQLITE_OK0 ){
201390 rc = rc2;
201391 }
201392 }
201393 if( rc==SQLITE_OK0 && pRtree->nAux ){
201394 sqlite3_stmt *pUp = pRtree->pWriteAux;
201395 int jj;
201396 sqlite3_bind_int64(pUp, 1, *pRowid);
201397 for(jj=0; jj<pRtree->nAux; jj++){
201398 sqlite3_bind_value(pUp, jj+2, aData[pRtree->nDim2+3+jj]);
201399 }
201400 sqlite3_step(pUp);
201401 rc = sqlite3_reset(pUp);
201402 }
201403 }
201404
201405constraint:
201406 rtreeRelease(pRtree);
201407 return rc;
201408}
201409
201410/*
201411** Called when a transaction starts.
201412*/
201413static int rtreeBeginTransaction(sqlite3_vtab *pVtab){
201414 Rtree *pRtree = (Rtree *)pVtab;
201415 assert( pRtree->inWrTrans==0 )((void) (0));
201416 pRtree->inWrTrans++;
201417 return SQLITE_OK0;
201418}
201419
201420/*
201421** Called when a transaction completes (either by COMMIT or ROLLBACK).
201422** The sqlite3_blob object should be released at this point.
201423*/
201424static int rtreeEndTransaction(sqlite3_vtab *pVtab){
201425 Rtree *pRtree = (Rtree *)pVtab;
201426 pRtree->inWrTrans = 0;
201427 nodeBlobReset(pRtree);
201428 return SQLITE_OK0;
201429}
201430
201431/*
201432** The xRename method for rtree module virtual tables.
201433*/
201434static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
201435 Rtree *pRtree = (Rtree *)pVtab;
201436 int rc = SQLITE_NOMEM7;
201437 char *zSql = sqlite3_mprintf(
201438 "ALTER TABLE %Q.'%q_node' RENAME TO \"%w_node\";"
201439 "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";"
201440 "ALTER TABLE %Q.'%q_rowid' RENAME TO \"%w_rowid\";"
201441 , pRtree->zDb, pRtree->zName, zNewName
201442 , pRtree->zDb, pRtree->zName, zNewName
201443 , pRtree->zDb, pRtree->zName, zNewName
201444 );
201445 if( zSql ){
201446 nodeBlobReset(pRtree);
201447 rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
201448 sqlite3_free(zSql);
201449 }
201450 return rc;
201451}
201452
201453/*
201454** The xSavepoint method.
201455**
201456** This module does not need to do anything to support savepoints. However,
201457** it uses this hook to close any open blob handle. This is done because a
201458** DROP TABLE command - which fortunately always opens a savepoint - cannot
201459** succeed if there are any open blob handles. i.e. if the blob handle were
201460** not closed here, the following would fail:
201461**
201462** BEGIN;
201463** INSERT INTO rtree...
201464** DROP TABLE <tablename>; -- Would fail with SQLITE_LOCKED
201465** COMMIT;
201466*/
201467static int rtreeSavepoint(sqlite3_vtab *pVtab, int iSavepoint){
201468 Rtree *pRtree = (Rtree *)pVtab;
201469 u8 iwt = pRtree->inWrTrans;
201470 UNUSED_PARAMETER(iSavepoint)(void)(iSavepoint);
201471 pRtree->inWrTrans = 0;
201472 nodeBlobReset(pRtree);
201473 pRtree->inWrTrans = iwt;
201474 return SQLITE_OK0;
201475}
201476
201477/*
201478** This function populates the pRtree->nRowEst variable with an estimate
201479** of the number of rows in the virtual table. If possible, this is based
201480** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST.
201481*/
201482static int rtreeQueryStat1(sqlite3 *db, Rtree *pRtree){
201483 const char *zFmt = "SELECT stat FROM %Q.sqlite_stat1 WHERE tbl = '%q_rowid'";
201484 char *zSql;
201485 sqlite3_stmt *p;
201486 int rc;
201487 i64 nRow = RTREE_MIN_ROWEST100;
201488
201489 rc = sqlite3_table_column_metadata(
201490 db, pRtree->zDb, "sqlite_stat1",0,0,0,0,0,0
201491 );
201492 if( rc!=SQLITE_OK0 ){
201493 pRtree->nRowEst = RTREE_DEFAULT_ROWEST1048576;
201494 return rc==SQLITE_ERROR1 ? SQLITE_OK0 : rc;
201495 }
201496 zSql = sqlite3_mprintf(zFmt, pRtree->zDb, pRtree->zName);
201497 if( zSql==0 ){
201498 rc = SQLITE_NOMEM7;
201499 }else{
201500 rc = sqlite3_prepare_v2(db, zSql, -1, &p, 0);
201501 if( rc==SQLITE_OK0 ){
201502 if( sqlite3_step(p)==SQLITE_ROW100 ) nRow = sqlite3_column_int64(p, 0);
201503 rc = sqlite3_finalize(p);
201504 }
201505 sqlite3_free(zSql);
201506 }
201507 pRtree->nRowEst = MAX(nRow, RTREE_MIN_ROWEST)((nRow)>(100)?(nRow):(100));
201508 return rc;
201509}
201510
201511
201512/*
201513** Return true if zName is the extension on one of the shadow tables used
201514** by this module.
201515*/
201516static int rtreeShadowName(const char *zName){
201517 static const char *azName[] = {
201518 "node", "parent", "rowid"
201519 };
201520 unsigned int i;
201521 for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
201522 if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
201523 }
201524 return 0;
201525}
201526
201527static sqlite3_module rtreeModule = {
201528 3, /* iVersion */
201529 rtreeCreate, /* xCreate - create a table */
201530 rtreeConnect, /* xConnect - connect to an existing table */
201531 rtreeBestIndex, /* xBestIndex - Determine search strategy */
201532 rtreeDisconnect, /* xDisconnect - Disconnect from a table */
201533 rtreeDestroy, /* xDestroy - Drop a table */
201534 rtreeOpen, /* xOpen - open a cursor */
201535 rtreeClose, /* xClose - close a cursor */
201536 rtreeFilter, /* xFilter - configure scan constraints */
201537 rtreeNext, /* xNext - advance a cursor */
201538 rtreeEof, /* xEof */
201539 rtreeColumn, /* xColumn - read data */
201540 rtreeRowid, /* xRowid - read data */
201541 rtreeUpdate, /* xUpdate - write data */
201542 rtreeBeginTransaction, /* xBegin - begin transaction */
201543 rtreeEndTransaction, /* xSync - sync transaction */
201544 rtreeEndTransaction, /* xCommit - commit transaction */
201545 rtreeEndTransaction, /* xRollback - rollback transaction */
201546 0, /* xFindFunction - function overloading */
201547 rtreeRename, /* xRename - rename the table */
201548 rtreeSavepoint, /* xSavepoint */
201549 0, /* xRelease */
201550 0, /* xRollbackTo */
201551 rtreeShadowName /* xShadowName */
201552};
201553
201554static int rtreeSqlInit(
201555 Rtree *pRtree,
201556 sqlite3 *db,
201557 const char *zDb,
201558 const char *zPrefix,
201559 int isCreate
201560){
201561 int rc = SQLITE_OK0;
201562
201563 #define N_STATEMENT8 8
201564 static const char *azSql[N_STATEMENT8] = {
201565 /* Write the xxx_node table */
201566 "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(?1, ?2)",
201567 "DELETE FROM '%q'.'%q_node' WHERE nodeno = ?1",
201568
201569 /* Read and write the xxx_rowid table */
201570 "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = ?1",
201571 "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(?1, ?2)",
201572 "DELETE FROM '%q'.'%q_rowid' WHERE rowid = ?1",
201573
201574 /* Read and write the xxx_parent table */
201575 "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = ?1",
201576 "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(?1, ?2)",
201577 "DELETE FROM '%q'.'%q_parent' WHERE nodeno = ?1"
201578 };
201579 sqlite3_stmt **appStmt[N_STATEMENT8];
201580 int i;
201581 const int f = SQLITE_PREPARE_PERSISTENT0x01|SQLITE_PREPARE_NO_VTAB0x04;
201582
201583 pRtree->db = db;
201584
201585 if( isCreate ){
201586 char *zCreate;
201587 sqlite3_str *p = sqlite3_str_new(db);
201588 int ii;
201589 sqlite3_str_appendf(p,
201590 "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY,nodeno",
201591 zDb, zPrefix);
201592 for(ii=0; ii<pRtree->nAux; ii++){
201593 sqlite3_str_appendf(p,",a%d",ii);
201594 }
201595 sqlite3_str_appendf(p,
201596 ");CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY,data);",
201597 zDb, zPrefix);
201598 sqlite3_str_appendf(p,
201599 "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY,parentnode);",
201600 zDb, zPrefix);
201601 sqlite3_str_appendf(p,
201602 "INSERT INTO \"%w\".\"%w_node\"VALUES(1,zeroblob(%d))",
201603 zDb, zPrefix, pRtree->iNodeSize);
201604 zCreate = sqlite3_str_finish(p);
201605 if( !zCreate ){
201606 return SQLITE_NOMEM7;
201607 }
201608 rc = sqlite3_exec(db, zCreate, 0, 0, 0);
201609 sqlite3_free(zCreate);
201610 if( rc!=SQLITE_OK0 ){
201611 return rc;
201612 }
201613 }
201614
201615 appStmt[0] = &pRtree->pWriteNode;
201616 appStmt[1] = &pRtree->pDeleteNode;
201617 appStmt[2] = &pRtree->pReadRowid;
201618 appStmt[3] = &pRtree->pWriteRowid;
201619 appStmt[4] = &pRtree->pDeleteRowid;
201620 appStmt[5] = &pRtree->pReadParent;
201621 appStmt[6] = &pRtree->pWriteParent;
201622 appStmt[7] = &pRtree->pDeleteParent;
201623
201624 rc = rtreeQueryStat1(db, pRtree);
201625 for(i=0; i<N_STATEMENT8 && rc==SQLITE_OK0; i++){
201626 char *zSql;
201627 const char *zFormat;
201628 if( i!=3 || pRtree->nAux==0 ){
201629 zFormat = azSql[i];
201630 }else {
201631 /* An UPSERT is very slightly slower than REPLACE, but it is needed
201632 ** if there are auxiliary columns */
201633 zFormat = "INSERT INTO\"%w\".\"%w_rowid\"(rowid,nodeno)VALUES(?1,?2)"
201634 "ON CONFLICT(rowid)DO UPDATE SET nodeno=excluded.nodeno";
201635 }
201636 zSql = sqlite3_mprintf(zFormat, zDb, zPrefix);
201637 if( zSql ){
201638 rc = sqlite3_prepare_v3(db, zSql, -1, f, appStmt[i], 0);
201639 }else{
201640 rc = SQLITE_NOMEM7;
201641 }
201642 sqlite3_free(zSql);
201643 }
201644 if( pRtree->nAux ){
201645 pRtree->zReadAuxSql = sqlite3_mprintf(
201646 "SELECT * FROM \"%w\".\"%w_rowid\" WHERE rowid=?1",
201647 zDb, zPrefix);
201648 if( pRtree->zReadAuxSql==0 ){
201649 rc = SQLITE_NOMEM7;
201650 }else{
201651 sqlite3_str *p = sqlite3_str_new(db);
201652 int ii;
201653 char *zSql;
201654 sqlite3_str_appendf(p, "UPDATE \"%w\".\"%w_rowid\"SET ", zDb, zPrefix);
201655 for(ii=0; ii<pRtree->nAux; ii++){
201656 if( ii ) sqlite3_str_append(p, ",", 1);
201657#ifdef SQLITE_ENABLE_GEOPOLY
201658 if( ii<pRtree->nAuxNotNull ){
201659 sqlite3_str_appendf(p,"a%d=coalesce(?%d,a%d)",ii,ii+2,ii);
201660 }else
201661#endif
201662 {
201663 sqlite3_str_appendf(p,"a%d=?%d",ii,ii+2);
201664 }
201665 }
201666 sqlite3_str_appendf(p, " WHERE rowid=?1");
201667 zSql = sqlite3_str_finish(p);
201668 if( zSql==0 ){
201669 rc = SQLITE_NOMEM7;
201670 }else{
201671 rc = sqlite3_prepare_v3(db, zSql, -1, f, &pRtree->pWriteAux, 0);
201672 sqlite3_free(zSql);
201673 }
201674 }
201675 }
201676
201677 return rc;
201678}
201679
201680/*
201681** The second argument to this function contains the text of an SQL statement
201682** that returns a single integer value. The statement is compiled and executed
201683** using database connection db. If successful, the integer value returned
201684** is written to *piVal and SQLITE_OK returned. Otherwise, an SQLite error
201685** code is returned and the value of *piVal after returning is not defined.
201686*/
201687static int getIntFromStmt(sqlite3 *db, const char *zSql, int *piVal){
201688 int rc = SQLITE_NOMEM7;
201689 if( zSql ){
201690 sqlite3_stmt *pStmt = 0;
201691 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
201692 if( rc==SQLITE_OK0 ){
201693 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
201694 *piVal = sqlite3_column_int(pStmt, 0);
201695 }
201696 rc = sqlite3_finalize(pStmt);
201697 }
201698 }
201699 return rc;
201700}
201701
201702/*
201703** This function is called from within the xConnect() or xCreate() method to
201704** determine the node-size used by the rtree table being created or connected
201705** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned.
201706** Otherwise, an SQLite error code is returned.
201707**
201708** If this function is being called as part of an xConnect(), then the rtree
201709** table already exists. In this case the node-size is determined by inspecting
201710** the root node of the tree.
201711**
201712** Otherwise, for an xCreate(), use 64 bytes less than the database page-size.
201713** This ensures that each node is stored on a single database page. If the
201714** database page-size is so large that more than RTREE_MAXCELLS entries
201715** would fit in a single node, use a smaller node-size.
201716*/
201717static int getNodeSize(
201718 sqlite3 *db, /* Database handle */
201719 Rtree *pRtree, /* Rtree handle */
201720 int isCreate, /* True for xCreate, false for xConnect */
201721 char **pzErr /* OUT: Error message, if any */
201722){
201723 int rc;
201724 char *zSql;
201725 if( isCreate ){
201726 int iPageSize = 0;
201727 zSql = sqlite3_mprintf("PRAGMA %Q.page_size", pRtree->zDb);
201728 rc = getIntFromStmt(db, zSql, &iPageSize);
201729 if( rc==SQLITE_OK0 ){
201730 pRtree->iNodeSize = iPageSize-64;
201731 if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS51)<pRtree->iNodeSize ){
201732 pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS51;
201733 }
201734 }else{
201735 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
201736 }
201737 }else{
201738 zSql = sqlite3_mprintf(
201739 "SELECT length(data) FROM '%q'.'%q_node' WHERE nodeno = 1",
201740 pRtree->zDb, pRtree->zName
201741 );
201742 rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize);
201743 if( rc!=SQLITE_OK0 ){
201744 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
201745 }else if( pRtree->iNodeSize<(512-64) ){
201746 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
201747 RTREE_IS_CORRUPT(pRtree);
201748 *pzErr = sqlite3_mprintf("undersize RTree blobs in \"%q_node\"",
201749 pRtree->zName);
201750 }
201751 }
201752
201753 sqlite3_free(zSql);
201754 return rc;
201755}
201756
201757/*
201758** Return the length of a token
201759*/
201760static int rtreeTokenLength(const char *z){
201761 int dummy = 0;
201762 return sqlite3GetToken((const unsigned char*)z,&dummy);
201763}
201764
201765/*
201766** This function is the implementation of both the xConnect and xCreate
201767** methods of the r-tree virtual table.
201768**
201769** argv[0] -> module name
201770** argv[1] -> database name
201771** argv[2] -> table name
201772** argv[...] -> column names...
201773*/
201774static int rtreeInit(
201775 sqlite3 *db, /* Database connection */
201776 void *pAux, /* One of the RTREE_COORD_* constants */
201777 int argc, const char *const*argv, /* Parameters to CREATE TABLE statement */
201778 sqlite3_vtab **ppVtab, /* OUT: New virtual table */
201779 char **pzErr, /* OUT: Error message, if any */
201780 int isCreate /* True for xCreate, false for xConnect */
201781){
201782 int rc = SQLITE_OK0;
201783 Rtree *pRtree;
201784 int nDb; /* Length of string argv[1] */
201785 int nName; /* Length of string argv[2] */
201786 int eCoordType = (pAux ? RTREE_COORD_INT321 : RTREE_COORD_REAL320);
201787 sqlite3_str *pSql;
201788 char *zSql;
201789 int ii = 4;
201790 int iErr;
201791
201792 const char *aErrMsg[] = {
201793 0, /* 0 */
201794 "Wrong number of columns for an rtree table", /* 1 */
201795 "Too few columns for an rtree table", /* 2 */
201796 "Too many columns for an rtree table", /* 3 */
201797 "Auxiliary rtree columns must be last" /* 4 */
201798 };
201799
201800 assert( RTREE_MAX_AUX_COLUMN<256 )((void) (0)); /* Aux columns counted by a u8 */
201801 if( argc<6 || argc>RTREE_MAX_AUX_COLUMN100+3 ){
201802 *pzErr = sqlite3_mprintf("%s", aErrMsg[2 + (argc>=6)]);
201803 return SQLITE_ERROR1;
201804 }
201805
201806 sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT1, 1);
201807
201808 /* Allocate the sqlite3_vtab structure */
201809 nDb = (int)strlen(argv[1]);
201810 nName = (int)strlen(argv[2]);
201811 pRtree = (Rtree *)sqlite3_malloc64(sizeof(Rtree)+nDb+nName+2);
201812 if( !pRtree ){
201813 return SQLITE_NOMEM7;
201814 }
201815 memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
201816 pRtree->nBusy = 1;
201817 pRtree->base.pModule = &rtreeModule;
201818 pRtree->zDb = (char *)&pRtree[1];
201819 pRtree->zName = &pRtree->zDb[nDb+1];
201820 pRtree->eCoordType = (u8)eCoordType;
201821 memcpy(pRtree->zDb, argv[1], nDb);
201822 memcpy(pRtree->zName, argv[2], nName);
201823
201824
201825 /* Create/Connect to the underlying relational database schema. If
201826 ** that is successful, call sqlite3_declare_vtab() to configure
201827 ** the r-tree table schema.
201828 */
201829 pSql = sqlite3_str_new(db);
201830 sqlite3_str_appendf(pSql, "CREATE TABLE x(%.*s INT",
201831 rtreeTokenLength(argv[3]), argv[3]);
201832 for(ii=4; ii<argc; ii++){
201833 const char *zArg = argv[ii];
201834 if( zArg[0]=='+' ){
201835 pRtree->nAux++;
201836 sqlite3_str_appendf(pSql, ",%.*s", rtreeTokenLength(zArg+1), zArg+1);
201837 }else if( pRtree->nAux>0 ){
201838 break;
201839 }else{
201840 static const char *azFormat[] = {",%.*s REAL", ",%.*s INT"};
201841 pRtree->nDim2++;
201842 sqlite3_str_appendf(pSql, azFormat[eCoordType],
201843 rtreeTokenLength(zArg), zArg);
201844 }
201845 }
201846 sqlite3_str_appendf(pSql, ");");
201847 zSql = sqlite3_str_finish(pSql);
201848 if( !zSql ){
201849 rc = SQLITE_NOMEM7;
201850 }else if( ii<argc ){
201851 *pzErr = sqlite3_mprintf("%s", aErrMsg[4]);
201852 rc = SQLITE_ERROR1;
201853 }else if( SQLITE_OK0!=(rc = sqlite3_declare_vtab(db, zSql)) ){
201854 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
201855 }
201856 sqlite3_free(zSql);
201857 if( rc ) goto rtreeInit_fail;
201858 pRtree->nDim = pRtree->nDim2/2;
201859 if( pRtree->nDim<1 ){
201860 iErr = 2;
201861 }else if( pRtree->nDim2>RTREE_MAX_DIMENSIONS5*2 ){
201862 iErr = 3;
201863 }else if( pRtree->nDim2 % 2 ){
201864 iErr = 1;
201865 }else{
201866 iErr = 0;
201867 }
201868 if( iErr ){
201869 *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
201870 goto rtreeInit_fail;
201871 }
201872 pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
201873
201874 /* Figure out the node size to use. */
201875 rc = getNodeSize(db, pRtree, isCreate, pzErr);
201876 if( rc ) goto rtreeInit_fail;
201877 rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate);
201878 if( rc ){
201879 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
201880 goto rtreeInit_fail;
201881 }
201882
201883 *ppVtab = (sqlite3_vtab *)pRtree;
201884 return SQLITE_OK0;
201885
201886rtreeInit_fail:
201887 if( rc==SQLITE_OK0 ) rc = SQLITE_ERROR1;
201888 assert( *ppVtab==0 )((void) (0));
201889 assert( pRtree->nBusy==1 )((void) (0));
201890 rtreeRelease(pRtree);
201891 return rc;
201892}
201893
201894
201895/*
201896** Implementation of a scalar function that decodes r-tree nodes to
201897** human readable strings. This can be used for debugging and analysis.
201898**
201899** The scalar function takes two arguments: (1) the number of dimensions
201900** to the rtree (between 1 and 5, inclusive) and (2) a blob of data containing
201901** an r-tree node. For a two-dimensional r-tree structure called "rt", to
201902** deserialize all nodes, a statement like:
201903**
201904** SELECT rtreenode(2, data) FROM rt_node;
201905**
201906** The human readable string takes the form of a Tcl list with one
201907** entry for each cell in the r-tree node. Each entry is itself a
201908** list, containing the 8-byte rowid/pageno followed by the
201909** <num-dimension>*2 coordinates.
201910*/
201911static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
201912 RtreeNode node;
201913 Rtree tree;
201914 int ii;
201915 int nData;
201916 int errCode;
201917 sqlite3_str *pOut;
201918
201919 UNUSED_PARAMETER(nArg)(void)(nArg);
201920 memset(&node, 0, sizeof(RtreeNode));
201921 memset(&tree, 0, sizeof(Rtree));
201922 tree.nDim = (u8)sqlite3_value_int(apArg[0]);
201923 if( tree.nDim<1 || tree.nDim>5 ) return;
201924 tree.nDim2 = tree.nDim*2;
201925 tree.nBytesPerCell = 8 + 8 * tree.nDim;
201926 node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
201927 if( node.zData==0 ) return;
201928 nData = sqlite3_value_bytes(apArg[1]);
201929 if( nData<4 ) return;
201930 if( nData<NCELL(&node)readInt16(&(&node)->zData[2])*tree.nBytesPerCell ) return;
201931
201932 pOut = sqlite3_str_new(0);
201933 for(ii=0; ii<NCELL(&node)readInt16(&(&node)->zData[2]); ii++){
201934 RtreeCell cell;
201935 int jj;
201936
201937 nodeGetCell(&tree, &node, ii, &cell);
201938 if( ii>0 ) sqlite3_str_append(pOut, " ", 1);
201939 sqlite3_str_appendf(pOut, "{%lld", cell.iRowid);
201940 for(jj=0; jj<tree.nDim2; jj++){
201941#ifndef SQLITE_RTREE_INT_ONLY
201942 sqlite3_str_appendf(pOut, " %g", (double)cell.aCoord[jj].f);
201943#else
201944 sqlite3_str_appendf(pOut, " %d", cell.aCoord[jj].i);
201945#endif
201946 }
201947 sqlite3_str_append(pOut, "}", 1);
201948 }
201949 errCode = sqlite3_str_errcode(pOut);
201950 sqlite3_result_text(ctx, sqlite3_str_finish(pOut), -1, sqlite3_free);
201951 sqlite3_result_error_code(ctx, errCode);
201952}
201953
201954/* This routine implements an SQL function that returns the "depth" parameter
201955** from the front of a blob that is an r-tree node. For example:
201956**
201957** SELECT rtreedepth(data) FROM rt_node WHERE nodeno=1;
201958**
201959** The depth value is 0 for all nodes other than the root node, and the root
201960** node always has nodeno=1, so the example above is the primary use for this
201961** routine. This routine is intended for testing and analysis only.
201962*/
201963static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
201964 UNUSED_PARAMETER(nArg)(void)(nArg);
201965 if( sqlite3_value_type(apArg[0])!=SQLITE_BLOB4
201966 || sqlite3_value_bytes(apArg[0])<2
201967
201968 ){
201969 sqlite3_result_error(ctx, "Invalid argument to rtreedepth()", -1);
201970 }else{
201971 u8 *zBlob = (u8 *)sqlite3_value_blob(apArg[0]);
201972 if( zBlob ){
201973 sqlite3_result_int(ctx, readInt16(zBlob));
201974 }else{
201975 sqlite3_result_error_nomem(ctx);
201976 }
201977 }
201978}
201979
201980/*
201981** Context object passed between the various routines that make up the
201982** implementation of integrity-check function rtreecheck().
201983*/
201984typedef struct RtreeCheck RtreeCheck;
201985struct RtreeCheck {
201986 sqlite3 *db; /* Database handle */
201987 const char *zDb; /* Database containing rtree table */
201988 const char *zTab; /* Name of rtree table */
201989 int bInt; /* True for rtree_i32 table */
201990 int nDim; /* Number of dimensions for this rtree tbl */
201991 sqlite3_stmt *pGetNode; /* Statement used to retrieve nodes */
201992 sqlite3_stmt *aCheckMapping[2]; /* Statements to query %_parent/%_rowid */
201993 int nLeaf; /* Number of leaf cells in table */
201994 int nNonLeaf; /* Number of non-leaf cells in table */
201995 int rc; /* Return code */
201996 char *zReport; /* Message to report */
201997 int nErr; /* Number of lines in zReport */
201998};
201999
202000#define RTREE_CHECK_MAX_ERROR100 100
202001
202002/*
202003** Reset SQL statement pStmt. If the sqlite3_reset() call returns an error,
202004** and RtreeCheck.rc==SQLITE_OK, set RtreeCheck.rc to the error code.
202005*/
202006static void rtreeCheckReset(RtreeCheck *pCheck, sqlite3_stmt *pStmt){
202007 int rc = sqlite3_reset(pStmt);
202008 if( pCheck->rc==SQLITE_OK0 ) pCheck->rc = rc;
202009}
202010
202011/*
202012** The second and subsequent arguments to this function are a format string
202013** and printf style arguments. This function formats the string and attempts
202014** to compile it as an SQL statement.
202015**
202016** If successful, a pointer to the new SQL statement is returned. Otherwise,
202017** NULL is returned and an error code left in RtreeCheck.rc.
202018*/
202019static sqlite3_stmt *rtreeCheckPrepare(
202020 RtreeCheck *pCheck, /* RtreeCheck object */
202021 const char *zFmt, ... /* Format string and trailing args */
202022){
202023 va_list ap;
202024 char *z;
202025 sqlite3_stmt *pRet = 0;
202026
202027 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
202028 z = sqlite3_vmprintf(zFmt, ap);
202029
202030 if( pCheck->rc==SQLITE_OK0 ){
202031 if( z==0 ){
202032 pCheck->rc = SQLITE_NOMEM7;
202033 }else{
202034 pCheck->rc = sqlite3_prepare_v2(pCheck->db, z, -1, &pRet, 0);
202035 }
202036 }
202037
202038 sqlite3_free(z);
202039 va_end(ap)__builtin_va_end(ap);
202040 return pRet;
202041}
202042
202043/*
202044** The second and subsequent arguments to this function are a printf()
202045** style format string and arguments. This function formats the string and
202046** appends it to the report being accumuated in pCheck.
202047*/
202048static void rtreeCheckAppendMsg(RtreeCheck *pCheck, const char *zFmt, ...){
202049 va_list ap;
202050 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
202051 if( pCheck->rc==SQLITE_OK0 && pCheck->nErr<RTREE_CHECK_MAX_ERROR100 ){
202052 char *z = sqlite3_vmprintf(zFmt, ap);
202053 if( z==0 ){
202054 pCheck->rc = SQLITE_NOMEM7;
202055 }else{
202056 pCheck->zReport = sqlite3_mprintf("%z%s%z",
202057 pCheck->zReport, (pCheck->zReport ? "\n" : ""), z
202058 );
202059 if( pCheck->zReport==0 ){
202060 pCheck->rc = SQLITE_NOMEM7;
202061 }
202062 }
202063 pCheck->nErr++;
202064 }
202065 va_end(ap)__builtin_va_end(ap);
202066}
202067
202068/*
202069** This function is a no-op if there is already an error code stored
202070** in the RtreeCheck object indicated by the first argument. NULL is
202071** returned in this case.
202072**
202073** Otherwise, the contents of rtree table node iNode are loaded from
202074** the database and copied into a buffer obtained from sqlite3_malloc().
202075** If no error occurs, a pointer to the buffer is returned and (*pnNode)
202076** is set to the size of the buffer in bytes.
202077**
202078** Or, if an error does occur, NULL is returned and an error code left
202079** in the RtreeCheck object. The final value of *pnNode is undefined in
202080** this case.
202081*/
202082static u8 *rtreeCheckGetNode(RtreeCheck *pCheck, i64 iNode, int *pnNode){
202083 u8 *pRet = 0; /* Return value */
202084
202085 if( pCheck->rc==SQLITE_OK0 && pCheck->pGetNode==0 ){
202086 pCheck->pGetNode = rtreeCheckPrepare(pCheck,
202087 "SELECT data FROM %Q.'%q_node' WHERE nodeno=?",
202088 pCheck->zDb, pCheck->zTab
202089 );
202090 }
202091
202092 if( pCheck->rc==SQLITE_OK0 ){
202093 sqlite3_bind_int64(pCheck->pGetNode, 1, iNode);
202094 if( sqlite3_step(pCheck->pGetNode)==SQLITE_ROW100 ){
202095 int nNode = sqlite3_column_bytes(pCheck->pGetNode, 0);
202096 const u8 *pNode = (const u8*)sqlite3_column_blob(pCheck->pGetNode, 0);
202097 pRet = sqlite3_malloc64(nNode);
202098 if( pRet==0 ){
202099 pCheck->rc = SQLITE_NOMEM7;
202100 }else{
202101 memcpy(pRet, pNode, nNode);
202102 *pnNode = nNode;
202103 }
202104 }
202105 rtreeCheckReset(pCheck, pCheck->pGetNode);
202106 if( pCheck->rc==SQLITE_OK0 && pRet==0 ){
202107 rtreeCheckAppendMsg(pCheck, "Node %lld missing from database", iNode);
202108 }
202109 }
202110
202111 return pRet;
202112}
202113
202114/*
202115** This function is used to check that the %_parent (if bLeaf==0) or %_rowid
202116** (if bLeaf==1) table contains a specified entry. The schemas of the
202117** two tables are:
202118**
202119** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
202120** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
202121**
202122** In both cases, this function checks that there exists an entry with
202123** IPK value iKey and the second column set to iVal.
202124**
202125*/
202126static void rtreeCheckMapping(
202127 RtreeCheck *pCheck, /* RtreeCheck object */
202128 int bLeaf, /* True for a leaf cell, false for interior */
202129 i64 iKey, /* Key for mapping */
202130 i64 iVal /* Expected value for mapping */
202131){
202132 int rc;
202133 sqlite3_stmt *pStmt;
202134 const char *azSql[2] = {
202135 "SELECT parentnode FROM %Q.'%q_parent' WHERE nodeno=?1",
202136 "SELECT nodeno FROM %Q.'%q_rowid' WHERE rowid=?1"
202137 };
202138
202139 assert( bLeaf==0 || bLeaf==1 )((void) (0));
202140 if( pCheck->aCheckMapping[bLeaf]==0 ){
202141 pCheck->aCheckMapping[bLeaf] = rtreeCheckPrepare(pCheck,
202142 azSql[bLeaf], pCheck->zDb, pCheck->zTab
202143 );
202144 }
202145 if( pCheck->rc!=SQLITE_OK0 ) return;
202146
202147 pStmt = pCheck->aCheckMapping[bLeaf];
202148 sqlite3_bind_int64(pStmt, 1, iKey);
202149 rc = sqlite3_step(pStmt);
202150 if( rc==SQLITE_DONE101 ){
202151 rtreeCheckAppendMsg(pCheck, "Mapping (%lld -> %lld) missing from %s table",
202152 iKey, iVal, (bLeaf ? "%_rowid" : "%_parent")
202153 );
202154 }else if( rc==SQLITE_ROW100 ){
202155 i64 ii = sqlite3_column_int64(pStmt, 0);
202156 if( ii!=iVal ){
202157 rtreeCheckAppendMsg(pCheck,
202158 "Found (%lld -> %lld) in %s table, expected (%lld -> %lld)",
202159 iKey, ii, (bLeaf ? "%_rowid" : "%_parent"), iKey, iVal
202160 );
202161 }
202162 }
202163 rtreeCheckReset(pCheck, pStmt);
202164}
202165
202166/*
202167** Argument pCell points to an array of coordinates stored on an rtree page.
202168** This function checks that the coordinates are internally consistent (no
202169** x1>x2 conditions) and adds an error message to the RtreeCheck object
202170** if they are not.
202171**
202172** Additionally, if pParent is not NULL, then it is assumed to point to
202173** the array of coordinates on the parent page that bound the page
202174** containing pCell. In this case it is also verified that the two
202175** sets of coordinates are mutually consistent and an error message added
202176** to the RtreeCheck object if they are not.
202177*/
202178static void rtreeCheckCellCoord(
202179 RtreeCheck *pCheck,
202180 i64 iNode, /* Node id to use in error messages */
202181 int iCell, /* Cell number to use in error messages */
202182 u8 *pCell, /* Pointer to cell coordinates */
202183 u8 *pParent /* Pointer to parent coordinates */
202184){
202185 RtreeCoord c1, c2;
202186 RtreeCoord p1, p2;
202187 int i;
202188
202189 for(i=0; i<pCheck->nDim; i++){
202190 readCoord(&pCell[4*2*i], &c1);
202191 readCoord(&pCell[4*(2*i + 1)], &c2);
202192
202193 /* printf("%e, %e\n", c1.u.f, c2.u.f); */
202194 if( pCheck->bInt ? c1.i>c2.i : c1.f>c2.f ){
202195 rtreeCheckAppendMsg(pCheck,
202196 "Dimension %d of cell %d on node %lld is corrupt", i, iCell, iNode
202197 );
202198 }
202199
202200 if( pParent ){
202201 readCoord(&pParent[4*2*i], &p1);
202202 readCoord(&pParent[4*(2*i + 1)], &p2);
202203
202204 if( (pCheck->bInt ? c1.i<p1.i : c1.f<p1.f)
202205 || (pCheck->bInt ? c2.i>p2.i : c2.f>p2.f)
202206 ){
202207 rtreeCheckAppendMsg(pCheck,
202208 "Dimension %d of cell %d on node %lld is corrupt relative to parent"
202209 , i, iCell, iNode
202210 );
202211 }
202212 }
202213 }
202214}
202215
202216/*
202217** Run rtreecheck() checks on node iNode, which is at depth iDepth within
202218** the r-tree structure. Argument aParent points to the array of coordinates
202219** that bound node iNode on the parent node.
202220**
202221** If any problems are discovered, an error message is appended to the
202222** report accumulated in the RtreeCheck object.
202223*/
202224static void rtreeCheckNode(
202225 RtreeCheck *pCheck,
202226 int iDepth, /* Depth of iNode (0==leaf) */
202227 u8 *aParent, /* Buffer containing parent coords */
202228 i64 iNode /* Node to check */
202229){
202230 u8 *aNode = 0;
202231 int nNode = 0;
202232
202233 assert( iNode==1 || aParent!=0 )((void) (0));
202234 assert( pCheck->nDim>0 )((void) (0));
202235
202236 aNode = rtreeCheckGetNode(pCheck, iNode, &nNode);
202237 if( aNode ){
202238 if( nNode<4 ){
202239 rtreeCheckAppendMsg(pCheck,
202240 "Node %lld is too small (%d bytes)", iNode, nNode
202241 );
202242 }else{
202243 int nCell; /* Number of cells on page */
202244 int i; /* Used to iterate through cells */
202245 if( aParent==0 ){
202246 iDepth = readInt16(aNode);
202247 if( iDepth>RTREE_MAX_DEPTH40 ){
202248 rtreeCheckAppendMsg(pCheck, "Rtree depth out of range (%d)", iDepth);
202249 sqlite3_free(aNode);
202250 return;
202251 }
202252 }
202253 nCell = readInt16(&aNode[2]);
202254 if( (4 + nCell*(8 + pCheck->nDim*2*4))>nNode ){
202255 rtreeCheckAppendMsg(pCheck,
202256 "Node %lld is too small for cell count of %d (%d bytes)",
202257 iNode, nCell, nNode
202258 );
202259 }else{
202260 for(i=0; i<nCell; i++){
202261 u8 *pCell = &aNode[4 + i*(8 + pCheck->nDim*2*4)];
202262 i64 iVal = readInt64(pCell);
202263 rtreeCheckCellCoord(pCheck, iNode, i, &pCell[8], aParent);
202264
202265 if( iDepth>0 ){
202266 rtreeCheckMapping(pCheck, 0, iVal, iNode);
202267 rtreeCheckNode(pCheck, iDepth-1, &pCell[8], iVal);
202268 pCheck->nNonLeaf++;
202269 }else{
202270 rtreeCheckMapping(pCheck, 1, iVal, iNode);
202271 pCheck->nLeaf++;
202272 }
202273 }
202274 }
202275 }
202276 sqlite3_free(aNode);
202277 }
202278}
202279
202280/*
202281** The second argument to this function must be either "_rowid" or
202282** "_parent". This function checks that the number of entries in the
202283** %_rowid or %_parent table is exactly nExpect. If not, it adds
202284** an error message to the report in the RtreeCheck object indicated
202285** by the first argument.
202286*/
202287static void rtreeCheckCount(RtreeCheck *pCheck, const char *zTbl, i64 nExpect){
202288 if( pCheck->rc==SQLITE_OK0 ){
202289 sqlite3_stmt *pCount;
202290 pCount = rtreeCheckPrepare(pCheck, "SELECT count(*) FROM %Q.'%q%s'",
202291 pCheck->zDb, pCheck->zTab, zTbl
202292 );
202293 if( pCount ){
202294 if( sqlite3_step(pCount)==SQLITE_ROW100 ){
202295 i64 nActual = sqlite3_column_int64(pCount, 0);
202296 if( nActual!=nExpect ){
202297 rtreeCheckAppendMsg(pCheck, "Wrong number of entries in %%%s table"
202298 " - expected %lld, actual %lld" , zTbl, nExpect, nActual
202299 );
202300 }
202301 }
202302 pCheck->rc = sqlite3_finalize(pCount);
202303 }
202304 }
202305}
202306
202307/*
202308** This function does the bulk of the work for the rtree integrity-check.
202309** It is called by rtreecheck(), which is the SQL function implementation.
202310*/
202311static int rtreeCheckTable(
202312 sqlite3 *db, /* Database handle to access db through */
202313 const char *zDb, /* Name of db ("main", "temp" etc.) */
202314 const char *zTab, /* Name of rtree table to check */
202315 char **pzReport /* OUT: sqlite3_malloc'd report text */
202316){
202317 RtreeCheck check; /* Common context for various routines */
202318 sqlite3_stmt *pStmt = 0; /* Used to find column count of rtree table */
202319 int bEnd = 0; /* True if transaction should be closed */
202320 int nAux = 0; /* Number of extra columns. */
202321
202322 /* Initialize the context object */
202323 memset(&check, 0, sizeof(check));
202324 check.db = db;
202325 check.zDb = zDb;
202326 check.zTab = zTab;
202327
202328 /* If there is not already an open transaction, open one now. This is
202329 ** to ensure that the queries run as part of this integrity-check operate
202330 ** on a consistent snapshot. */
202331 if( sqlite3_get_autocommit(db) ){
202332 check.rc = sqlite3_exec(db, "BEGIN", 0, 0, 0);
202333 bEnd = 1;
202334 }
202335
202336 /* Find the number of auxiliary columns */
202337 if( check.rc==SQLITE_OK0 ){
202338 pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.'%q_rowid'", zDb, zTab);
202339 if( pStmt ){
202340 nAux = sqlite3_column_count(pStmt) - 2;
202341 sqlite3_finalize(pStmt);
202342 }else
202343 if( check.rc!=SQLITE_NOMEM7 ){
202344 check.rc = SQLITE_OK0;
202345 }
202346 }
202347
202348 /* Find number of dimensions in the rtree table. */
202349 pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.%Q", zDb, zTab);
202350 if( pStmt ){
202351 int rc;
202352 check.nDim = (sqlite3_column_count(pStmt) - 1 - nAux) / 2;
202353 if( check.nDim<1 ){
202354 rtreeCheckAppendMsg(&check, "Schema corrupt or not an rtree");
202355 }else if( SQLITE_ROW100==sqlite3_step(pStmt) ){
202356 check.bInt = (sqlite3_column_type(pStmt, 1)==SQLITE_INTEGER1);
202357 }
202358 rc = sqlite3_finalize(pStmt);
202359 if( rc!=SQLITE_CORRUPT11 ) check.rc = rc;
202360 }
202361
202362 /* Do the actual integrity-check */
202363 if( check.nDim>=1 ){
202364 if( check.rc==SQLITE_OK0 ){
202365 rtreeCheckNode(&check, 0, 0, 1);
202366 }
202367 rtreeCheckCount(&check, "_rowid", check.nLeaf);
202368 rtreeCheckCount(&check, "_parent", check.nNonLeaf);
202369 }
202370
202371 /* Finalize SQL statements used by the integrity-check */
202372 sqlite3_finalize(check.pGetNode);
202373 sqlite3_finalize(check.aCheckMapping[0]);
202374 sqlite3_finalize(check.aCheckMapping[1]);
202375
202376 /* If one was opened, close the transaction */
202377 if( bEnd ){
202378 int rc = sqlite3_exec(db, "END", 0, 0, 0);
202379 if( check.rc==SQLITE_OK0 ) check.rc = rc;
202380 }
202381 *pzReport = check.zReport;
202382 return check.rc;
202383}
202384
202385/*
202386** Usage:
202387**
202388** rtreecheck(<rtree-table>);
202389** rtreecheck(<database>, <rtree-table>);
202390**
202391** Invoking this SQL function runs an integrity-check on the named rtree
202392** table. The integrity-check verifies the following:
202393**
202394** 1. For each cell in the r-tree structure (%_node table), that:
202395**
202396** a) for each dimension, (coord1 <= coord2).
202397**
202398** b) unless the cell is on the root node, that the cell is bounded
202399** by the parent cell on the parent node.
202400**
202401** c) for leaf nodes, that there is an entry in the %_rowid
202402** table corresponding to the cell's rowid value that
202403** points to the correct node.
202404**
202405** d) for cells on non-leaf nodes, that there is an entry in the
202406** %_parent table mapping from the cell's child node to the
202407** node that it resides on.
202408**
202409** 2. That there are the same number of entries in the %_rowid table
202410** as there are leaf cells in the r-tree structure, and that there
202411** is a leaf cell that corresponds to each entry in the %_rowid table.
202412**
202413** 3. That there are the same number of entries in the %_parent table
202414** as there are non-leaf cells in the r-tree structure, and that
202415** there is a non-leaf cell that corresponds to each entry in the
202416** %_parent table.
202417*/
202418static void rtreecheck(
202419 sqlite3_context *ctx,
202420 int nArg,
202421 sqlite3_value **apArg
202422){
202423 if( nArg!=1 && nArg!=2 ){
202424 sqlite3_result_error(ctx,
202425 "wrong number of arguments to function rtreecheck()", -1
202426 );
202427 }else{
202428 int rc;
202429 char *zReport = 0;
202430 const char *zDb = (const char*)sqlite3_value_text(apArg[0]);
202431 const char *zTab;
202432 if( nArg==1 ){
202433 zTab = zDb;
202434 zDb = "main";
202435 }else{
202436 zTab = (const char*)sqlite3_value_text(apArg[1]);
202437 }
202438 rc = rtreeCheckTable(sqlite3_context_db_handle(ctx), zDb, zTab, &zReport);
202439 if( rc==SQLITE_OK0 ){
202440 sqlite3_result_text(ctx, zReport ? zReport : "ok", -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
202441 }else{
202442 sqlite3_result_error_code(ctx, rc);
202443 }
202444 sqlite3_free(zReport);
202445 }
202446}
202447
202448/* Conditionally include the geopoly code */
202449#ifdef SQLITE_ENABLE_GEOPOLY
202450/************** Include geopoly.c in the middle of rtree.c *******************/
202451/************** Begin file geopoly.c *****************************************/
202452/*
202453** 2018-05-25
202454**
202455** The author disclaims copyright to this source code. In place of
202456** a legal notice, here is a blessing:
202457**
202458** May you do good and not evil.
202459** May you find forgiveness for yourself and forgive others.
202460** May you share freely, never taking more than you give.
202461**
202462******************************************************************************
202463**
202464** This file implements an alternative R-Tree virtual table that
202465** uses polygons to express the boundaries of 2-dimensional objects.
202466**
202467** This file is #include-ed onto the end of "rtree.c" so that it has
202468** access to all of the R-Tree internals.
202469*/
202470/* #include <stdlib.h> */
202471
202472/* Enable -DGEOPOLY_ENABLE_DEBUG for debugging facilities */
202473#ifdef GEOPOLY_ENABLE_DEBUG
202474 static int geo_debug = 0;
202475# define GEODEBUG(X) if(geo_debug)printf X
202476#else
202477# define GEODEBUG(X)
202478#endif
202479
202480/* Character class routines */
202481#ifdef sqlite3Isdigit
202482 /* Use the SQLite core versions if this routine is part of the
202483 ** SQLite amalgamation */
202484# define safe_isdigit(x) sqlite3Isdigit(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x04)
202485# define safe_isalnum(x) sqlite3Isalnum(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x06)
202486# define safe_isxdigit(x) sqlite3Isxdigit(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x08)
202487#else
202488 /* Use the standard library for separate compilation */
202489#include <ctype.h> /* amalgamator: keep */
202490# define safe_isdigit(x) isdigit((unsigned char)(x))((*__ctype_b_loc ())[(int) (((unsigned char)(x)))] & (unsigned
short int) _ISdigit)
202491# define safe_isalnum(x) isalnum((unsigned char)(x))((*__ctype_b_loc ())[(int) (((unsigned char)(x)))] & (unsigned
short int) _ISalnum)
202492# define safe_isxdigit(x) isxdigit((unsigned char)(x))((*__ctype_b_loc ())[(int) (((unsigned char)(x)))] & (unsigned
short int) _ISxdigit)
202493#endif
202494
202495#ifndef JSON_NULL0 /* The following stuff repeats things found in json1 */
202496/*
202497** Growing our own isspace() routine this way is twice as fast as
202498** the library isspace() function.
202499*/
202500static const char geopolyIsSpace[] = {
202501 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
202502 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
202503 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
202504 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
202505 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
202506 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
202507 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
202508 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
202509 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
202510 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
202511 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
202512 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
202513 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
202514 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
202515 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
202516 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
202517};
202518#define fast_isspace(x)(jsonIsSpace[(unsigned char)x]) (geopolyIsSpace[(unsigned char)x])
202519#endif /* JSON NULL - back to original code */
202520
202521/* Compiler and version */
202522#ifndef GCC_VERSION(4*1000000+2*1000+1)
202523#if defined(__GNUC__4) && !defined(SQLITE_DISABLE_INTRINSIC)
202524# define GCC_VERSION(4*1000000+2*1000+1) (__GNUC__4*1000000+__GNUC_MINOR__2*1000+__GNUC_PATCHLEVEL__1)
202525#else
202526# define GCC_VERSION(4*1000000+2*1000+1) 0
202527#endif
202528#endif
202529#ifndef MSVC_VERSION0
202530#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
202531# define MSVC_VERSION0 _MSC_VER
202532#else
202533# define MSVC_VERSION0 0
202534#endif
202535#endif
202536
202537/* Datatype for coordinates
202538*/
202539typedef float GeoCoord;
202540
202541/*
202542** Internal representation of a polygon.
202543**
202544** The polygon consists of a sequence of vertexes. There is a line
202545** segment between each pair of vertexes, and one final segment from
202546** the last vertex back to the first. (This differs from the GeoJSON
202547** standard in which the final vertex is a repeat of the first.)
202548**
202549** The polygon follows the right-hand rule. The area to the right of
202550** each segment is "outside" and the area to the left is "inside".
202551**
202552** The on-disk representation consists of a 4-byte header followed by
202553** the values. The 4-byte header is:
202554**
202555** encoding (1 byte) 0=big-endian, 1=little-endian
202556** nvertex (3 bytes) Number of vertexes as a big-endian integer
202557**
202558** Enough space is allocated for 4 coordinates, to work around over-zealous
202559** warnings coming from some compiler (notably, clang). In reality, the size
202560** of each GeoPoly memory allocate is adjusted as necessary so that the
202561** GeoPoly.a[] array at the end is the appropriate size.
202562*/
202563typedef struct GeoPoly GeoPoly;
202564struct GeoPoly {
202565 int nVertex; /* Number of vertexes */
202566 unsigned char hdr[4]; /* Header for on-disk representation */
202567 GeoCoord a[8]; /* 2*nVertex values. X (longitude) first, then Y */
202568};
202569
202570/* The size of a memory allocation needed for a GeoPoly object sufficient
202571** to hold N coordinate pairs.
202572*/
202573#define GEOPOLY_SZ(N) (sizeof(GeoPoly) + sizeof(GeoCoord)*2*((N)-4))
202574
202575/* Macros to access coordinates of a GeoPoly.
202576** We have to use these macros, rather than just say p->a[i] in order
202577** to silence (incorrect) UBSAN warnings if the array index is too large.
202578*/
202579#define GeoX(P,I) (((GeoCoord*)(P)->a)[(I)*2])
202580#define GeoY(P,I) (((GeoCoord*)(P)->a)[(I)*2+1])
202581
202582
202583/*
202584** State of a parse of a GeoJSON input.
202585*/
202586typedef struct GeoParse GeoParse;
202587struct GeoParse {
202588 const unsigned char *z; /* Unparsed input */
202589 int nVertex; /* Number of vertexes in a[] */
202590 int nAlloc; /* Space allocated to a[] */
202591 int nErr; /* Number of errors encountered */
202592 GeoCoord *a; /* Array of vertexes. From sqlite3_malloc64() */
202593};
202594
202595/* Do a 4-byte byte swap */
202596static void geopolySwab32(unsigned char *a){
202597 unsigned char t = a[0];
202598 a[0] = a[3];
202599 a[3] = t;
202600 t = a[1];
202601 a[1] = a[2];
202602 a[2] = t;
202603}
202604
202605/* Skip whitespace. Return the next non-whitespace character. */
202606static char geopolySkipSpace(GeoParse *p){
202607 while( fast_isspace(p->z[0])(jsonIsSpace[(unsigned char)p->z[0]]) ) p->z++;
202608 return p->z[0];
202609}
202610
202611/* Parse out a number. Write the value into *pVal if pVal!=0.
202612** return non-zero on success and zero if the next token is not a number.
202613*/
202614static int geopolyParseNumber(GeoParse *p, GeoCoord *pVal){
202615 char c = geopolySkipSpace(p);
202616 const unsigned char *z = p->z;
202617 int j = 0;
202618 int seenDP = 0;
202619 int seenE = 0;
202620 if( c=='-' ){
202621 j = 1;
202622 c = z[j];
202623 }
202624 if( c=='0' && z[j+1]>='0' && z[j+1]<='9' ) return 0;
202625 for(;; j++){
202626 c = z[j];
202627 if( safe_isdigit(c) ) continue;
202628 if( c=='.' ){
202629 if( z[j-1]=='-' ) return 0;
202630 if( seenDP ) return 0;
202631 seenDP = 1;
202632 continue;
202633 }
202634 if( c=='e' || c=='E' ){
202635 if( z[j-1]<'0' ) return 0;
202636 if( seenE ) return -1;
202637 seenDP = seenE = 1;
202638 c = z[j+1];
202639 if( c=='+' || c=='-' ){
202640 j++;
202641 c = z[j+1];
202642 }
202643 if( c<'0' || c>'9' ) return 0;
202644 continue;
202645 }
202646 break;
202647 }
202648 if( z[j-1]<'0' ) return 0;
202649 if( pVal ){
202650#ifdef SQLITE_AMALGAMATION1
202651 /* The sqlite3AtoF() routine is much much faster than atof(), if it
202652 ** is available */
202653 double r;
202654 (void)sqlite3AtoF((const char*)p->z, &r, j, SQLITE_UTF81);
202655 *pVal = r;
202656#else
202657 *pVal = (GeoCoord)atof((const char*)p->z);
202658#endif
202659 }
202660 p->z += j;
202661 return 1;
202662}
202663
202664/*
202665** If the input is a well-formed JSON array of coordinates with at least
202666** four coordinates and where each coordinate is itself a two-value array,
202667** then convert the JSON into a GeoPoly object and return a pointer to
202668** that object.
202669**
202670** If any error occurs, return NULL.
202671*/
202672static GeoPoly *geopolyParseJson(const unsigned char *z, int *pRc){
202673 GeoParse s;
202674 int rc = SQLITE_OK0;
202675 memset(&s, 0, sizeof(s));
202676 s.z = z;
202677 if( geopolySkipSpace(&s)=='[' ){
202678 s.z++;
202679 while( geopolySkipSpace(&s)=='[' ){
202680 int ii = 0;
202681 char c;
202682 s.z++;
202683 if( s.nVertex>=s.nAlloc ){
202684 GeoCoord *aNew;
202685 s.nAlloc = s.nAlloc*2 + 16;
202686 aNew = sqlite3_realloc64(s.a, s.nAlloc*sizeof(GeoCoord)*2 );
202687 if( aNew==0 ){
202688 rc = SQLITE_NOMEM7;
202689 s.nErr++;
202690 break;
202691 }
202692 s.a = aNew;
202693 }
202694 while( geopolyParseNumber(&s, ii<=1 ? &s.a[s.nVertex*2+ii] : 0) ){
202695 ii++;
202696 if( ii==2 ) s.nVertex++;
202697 c = geopolySkipSpace(&s);
202698 s.z++;
202699 if( c==',' ) continue;
202700 if( c==']' && ii>=2 ) break;
202701 s.nErr++;
202702 rc = SQLITE_ERROR1;
202703 goto parse_json_err;
202704 }
202705 if( geopolySkipSpace(&s)==',' ){
202706 s.z++;
202707 continue;
202708 }
202709 break;
202710 }
202711 if( geopolySkipSpace(&s)==']'
202712 && s.nVertex>=4
202713 && s.a[0]==s.a[s.nVertex*2-2]
202714 && s.a[1]==s.a[s.nVertex*2-1]
202715 && (s.z++, geopolySkipSpace(&s)==0)
202716 ){
202717 GeoPoly *pOut;
202718 int x = 1;
202719 s.nVertex--; /* Remove the redundant vertex at the end */
202720 pOut = sqlite3_malloc64( GEOPOLY_SZ((sqlite3_int64)s.nVertex) );
202721 x = 1;
202722 if( pOut==0 ) goto parse_json_err;
202723 pOut->nVertex = s.nVertex;
202724 memcpy(pOut->a, s.a, s.nVertex*2*sizeof(GeoCoord));
202725 pOut->hdr[0] = *(unsigned char*)&x;
202726 pOut->hdr[1] = (s.nVertex>>16)&0xff;
202727 pOut->hdr[2] = (s.nVertex>>8)&0xff;
202728 pOut->hdr[3] = s.nVertex&0xff;
202729 sqlite3_free(s.a);
202730 if( pRc ) *pRc = SQLITE_OK0;
202731 return pOut;
202732 }else{
202733 s.nErr++;
202734 rc = SQLITE_ERROR1;
202735 }
202736 }
202737parse_json_err:
202738 if( pRc ) *pRc = rc;
202739 sqlite3_free(s.a);
202740 return 0;
202741}
202742
202743/*
202744** Given a function parameter, try to interpret it as a polygon, either
202745** in the binary format or JSON text. Compute a GeoPoly object and
202746** return a pointer to that object. Or if the input is not a well-formed
202747** polygon, put an error message in sqlite3_context and return NULL.
202748*/
202749static GeoPoly *geopolyFuncParam(
202750 sqlite3_context *pCtx, /* Context for error messages */
202751 sqlite3_value *pVal, /* The value to decode */
202752 int *pRc /* Write error here */
202753){
202754 GeoPoly *p = 0;
202755 int nByte;
202756 testcase( pCtx==0 );
202757 if( sqlite3_value_type(pVal)==SQLITE_BLOB4
202758 && (nByte = sqlite3_value_bytes(pVal))>=(4+6*sizeof(GeoCoord))
202759 ){
202760 const unsigned char *a = sqlite3_value_blob(pVal);
202761 int nVertex;
202762 if( a==0 ){
202763 if( pCtx ) sqlite3_result_error_nomem(pCtx);
202764 return 0;
202765 }
202766 nVertex = (a[1]<<16) + (a[2]<<8) + a[3];
202767 if( (a[0]==0 || a[0]==1)
202768 && (nVertex*2*sizeof(GeoCoord) + 4)==(unsigned int)nByte
202769 ){
202770 p = sqlite3_malloc64( sizeof(*p) + (nVertex-1)*2*sizeof(GeoCoord) );
202771 if( p==0 ){
202772 if( pRc ) *pRc = SQLITE_NOMEM7;
202773 if( pCtx ) sqlite3_result_error_nomem(pCtx);
202774 }else{
202775 int x = 1;
202776 p->nVertex = nVertex;
202777 memcpy(p->hdr, a, nByte);
202778 if( a[0] != *(unsigned char*)&x ){
202779 int ii;
202780 for(ii=0; ii<nVertex; ii++){
202781 geopolySwab32((unsigned char*)&GeoX(p,ii));
202782 geopolySwab32((unsigned char*)&GeoY(p,ii));
202783 }
202784 p->hdr[0] ^= 1;
202785 }
202786 }
202787 }
202788 if( pRc ) *pRc = SQLITE_OK0;
202789 return p;
202790 }else if( sqlite3_value_type(pVal)==SQLITE_TEXT3 ){
202791 const unsigned char *zJson = sqlite3_value_text(pVal);
202792 if( zJson==0 ){
202793 if( pRc ) *pRc = SQLITE_NOMEM7;
202794 return 0;
202795 }
202796 return geopolyParseJson(zJson, pRc);
202797 }else{
202798 if( pRc ) *pRc = SQLITE_ERROR1;
202799 return 0;
202800 }
202801}
202802
202803/*
202804** Implementation of the geopoly_blob(X) function.
202805**
202806** If the input is a well-formed Geopoly BLOB or JSON string
202807** then return the BLOB representation of the polygon. Otherwise
202808** return NULL.
202809*/
202810static void geopolyBlobFunc(
202811 sqlite3_context *context,
202812 int argc,
202813 sqlite3_value **argv
202814){
202815 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
202816 if( p ){
202817 sqlite3_result_blob(context, p->hdr,
202818 4+8*p->nVertex, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
202819 sqlite3_free(p);
202820 }
202821}
202822
202823/*
202824** SQL function: geopoly_json(X)
202825**
202826** Interpret X as a polygon and render it as a JSON array
202827** of coordinates. Or, if X is not a valid polygon, return NULL.
202828*/
202829static void geopolyJsonFunc(
202830 sqlite3_context *context,
202831 int argc,
202832 sqlite3_value **argv
202833){
202834 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
202835 if( p ){
202836 sqlite3 *db = sqlite3_context_db_handle(context);
202837 sqlite3_str *x = sqlite3_str_new(db);
202838 int i;
202839 sqlite3_str_append(x, "[", 1);
202840 for(i=0; i<p->nVertex; i++){
202841 sqlite3_str_appendf(x, "[%!g,%!g],", GeoX(p,i), GeoY(p,i));
202842 }
202843 sqlite3_str_appendf(x, "[%!g,%!g]]", GeoX(p,0), GeoY(p,0));
202844 sqlite3_result_text(context, sqlite3_str_finish(x), -1, sqlite3_free);
202845 sqlite3_free(p);
202846 }
202847}
202848
202849/*
202850** SQL function: geopoly_svg(X, ....)
202851**
202852** Interpret X as a polygon and render it as a SVG <polyline>.
202853** Additional arguments are added as attributes to the <polyline>.
202854*/
202855static void geopolySvgFunc(
202856 sqlite3_context *context,
202857 int argc,
202858 sqlite3_value **argv
202859){
202860 GeoPoly *p;
202861 if( argc<1 ) return;
202862 p = geopolyFuncParam(context, argv[0], 0);
202863 if( p ){
202864 sqlite3 *db = sqlite3_context_db_handle(context);
202865 sqlite3_str *x = sqlite3_str_new(db);
202866 int i;
202867 char cSep = '\'';
202868 sqlite3_str_appendf(x, "<polyline points=");
202869 for(i=0; i<p->nVertex; i++){
202870 sqlite3_str_appendf(x, "%c%g,%g", cSep, GeoX(p,i), GeoY(p,i));
202871 cSep = ' ';
202872 }
202873 sqlite3_str_appendf(x, " %g,%g'", GeoX(p,0), GeoY(p,0));
202874 for(i=1; i<argc; i++){
202875 const char *z = (const char*)sqlite3_value_text(argv[i]);
202876 if( z && z[0] ){
202877 sqlite3_str_appendf(x, " %s", z);
202878 }
202879 }
202880 sqlite3_str_appendf(x, "></polyline>");
202881 sqlite3_result_text(context, sqlite3_str_finish(x), -1, sqlite3_free);
202882 sqlite3_free(p);
202883 }
202884}
202885
202886/*
202887** SQL Function: geopoly_xform(poly, A, B, C, D, E, F)
202888**
202889** Transform and/or translate a polygon as follows:
202890**
202891** x1 = A*x0 + B*y0 + E
202892** y1 = C*x0 + D*y0 + F
202893**
202894** For a translation:
202895**
202896** geopoly_xform(poly, 1, 0, 0, 1, x-offset, y-offset)
202897**
202898** Rotate by R around the point (0,0):
202899**
202900** geopoly_xform(poly, cos(R), sin(R), -sin(R), cos(R), 0, 0)
202901*/
202902static void geopolyXformFunc(
202903 sqlite3_context *context,
202904 int argc,
202905 sqlite3_value **argv
202906){
202907 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
202908 double A = sqlite3_value_double(argv[1]);
202909 double B = sqlite3_value_double(argv[2]);
202910 double C = sqlite3_value_double(argv[3]);
202911 double D = sqlite3_value_double(argv[4]);
202912 double E = sqlite3_value_double(argv[5]);
202913 double F = sqlite3_value_double(argv[6]);
202914 GeoCoord x1, y1, x0, y0;
202915 int ii;
202916 if( p ){
202917 for(ii=0; ii<p->nVertex; ii++){
202918 x0 = GeoX(p,ii);
202919 y0 = GeoY(p,ii);
202920 x1 = (GeoCoord)(A*x0 + B*y0 + E);
202921 y1 = (GeoCoord)(C*x0 + D*y0 + F);
202922 GeoX(p,ii) = x1;
202923 GeoY(p,ii) = y1;
202924 }
202925 sqlite3_result_blob(context, p->hdr,
202926 4+8*p->nVertex, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
202927 sqlite3_free(p);
202928 }
202929}
202930
202931/*
202932** Compute the area enclosed by the polygon.
202933**
202934** This routine can also be used to detect polygons that rotate in
202935** the wrong direction. Polygons are suppose to be counter-clockwise (CCW).
202936** This routine returns a negative value for clockwise (CW) polygons.
202937*/
202938static double geopolyArea(GeoPoly *p){
202939 double rArea = 0.0;
202940 int ii;
202941 for(ii=0; ii<p->nVertex-1; ii++){
202942 rArea += (GeoX(p,ii) - GeoX(p,ii+1)) /* (x0 - x1) */
202943 * (GeoY(p,ii) + GeoY(p,ii+1)) /* (y0 + y1) */
202944 * 0.5;
202945 }
202946 rArea += (GeoX(p,ii) - GeoX(p,0)) /* (xN - x0) */
202947 * (GeoY(p,ii) + GeoY(p,0)) /* (yN + y0) */
202948 * 0.5;
202949 return rArea;
202950}
202951
202952/*
202953** Implementation of the geopoly_area(X) function.
202954**
202955** If the input is a well-formed Geopoly BLOB then return the area
202956** enclosed by the polygon. If the polygon circulates clockwise instead
202957** of counterclockwise (as it should) then return the negative of the
202958** enclosed area. Otherwise return NULL.
202959*/
202960static void geopolyAreaFunc(
202961 sqlite3_context *context,
202962 int argc,
202963 sqlite3_value **argv
202964){
202965 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
202966 if( p ){
202967 sqlite3_result_double(context, geopolyArea(p));
202968 sqlite3_free(p);
202969 }
202970}
202971
202972/*
202973** Implementation of the geopoly_ccw(X) function.
202974**
202975** If the rotation of polygon X is clockwise (incorrect) instead of
202976** counter-clockwise (the correct winding order according to RFC7946)
202977** then reverse the order of the vertexes in polygon X.
202978**
202979** In other words, this routine returns a CCW polygon regardless of the
202980** winding order of its input.
202981**
202982** Use this routine to sanitize historical inputs that that sometimes
202983** contain polygons that wind in the wrong direction.
202984*/
202985static void geopolyCcwFunc(
202986 sqlite3_context *context,
202987 int argc,
202988 sqlite3_value **argv
202989){
202990 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
202991 if( p ){
202992 if( geopolyArea(p)<0.0 ){
202993 int ii, jj;
202994 for(ii=1, jj=p->nVertex-1; ii<jj; ii++, jj--){
202995 GeoCoord t = GeoX(p,ii);
202996 GeoX(p,ii) = GeoX(p,jj);
202997 GeoX(p,jj) = t;
202998 t = GeoY(p,ii);
202999 GeoY(p,ii) = GeoY(p,jj);
203000 GeoY(p,jj) = t;
203001 }
203002 }
203003 sqlite3_result_blob(context, p->hdr,
203004 4+8*p->nVertex, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
203005 sqlite3_free(p);
203006 }
203007}
203008
203009#define GEOPOLY_PI 3.1415926535897932385
203010
203011/* Fast approximation for sine(X) for X between -0.5*pi and 2*pi
203012*/
203013static double geopolySine(double r){
203014 assert( r>=-0.5*GEOPOLY_PI && r<=2.0*GEOPOLY_PI )((void) (0));
203015 if( r>=1.5*GEOPOLY_PI ){
203016 r -= 2.0*GEOPOLY_PI;
203017 }
203018 if( r>=0.5*GEOPOLY_PI ){
203019 return -geopolySine(r-GEOPOLY_PI);
203020 }else{
203021 double r2 = r*r;
203022 double r3 = r2*r;
203023 double r5 = r3*r2;
203024 return 0.9996949*r - 0.1656700*r3 + 0.0075134*r5;
203025 }
203026}
203027
203028/*
203029** Function: geopoly_regular(X,Y,R,N)
203030**
203031** Construct a simple, convex, regular polygon centered at X, Y
203032** with circumradius R and with N sides.
203033*/
203034static void geopolyRegularFunc(
203035 sqlite3_context *context,
203036 int argc,
203037 sqlite3_value **argv
203038){
203039 double x = sqlite3_value_double(argv[0]);
203040 double y = sqlite3_value_double(argv[1]);
203041 double r = sqlite3_value_double(argv[2]);
203042 int n = sqlite3_value_int(argv[3]);
203043 int i;
203044 GeoPoly *p;
203045
203046 if( n<3 || r<=0.0 ) return;
203047 if( n>1000 ) n = 1000;
203048 p = sqlite3_malloc64( sizeof(*p) + (n-1)*2*sizeof(GeoCoord) );
203049 if( p==0 ){
203050 sqlite3_result_error_nomem(context);
203051 return;
203052 }
203053 i = 1;
203054 p->hdr[0] = *(unsigned char*)&i;
203055 p->hdr[1] = 0;
203056 p->hdr[2] = (n>>8)&0xff;
203057 p->hdr[3] = n&0xff;
203058 for(i=0; i<n; i++){
203059 double rAngle = 2.0*GEOPOLY_PI*i/n;
203060 GeoX(p,i) = x - r*geopolySine(rAngle-0.5*GEOPOLY_PI);
203061 GeoY(p,i) = y + r*geopolySine(rAngle);
203062 }
203063 sqlite3_result_blob(context, p->hdr, 4+8*n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
203064 sqlite3_free(p);
203065}
203066
203067/*
203068** If pPoly is a polygon, compute its bounding box. Then:
203069**
203070** (1) if aCoord!=0 store the bounding box in aCoord, returning NULL
203071** (2) otherwise, compute a GeoPoly for the bounding box and return the
203072** new GeoPoly
203073**
203074** If pPoly is NULL but aCoord is not NULL, then compute a new GeoPoly from
203075** the bounding box in aCoord and return a pointer to that GeoPoly.
203076*/
203077static GeoPoly *geopolyBBox(
203078 sqlite3_context *context, /* For recording the error */
203079 sqlite3_value *pPoly, /* The polygon */
203080 RtreeCoord *aCoord, /* Results here */
203081 int *pRc /* Error code here */
203082){
203083 GeoPoly *pOut = 0;
203084 GeoPoly *p;
203085 float mnX, mxX, mnY, mxY;
203086 if( pPoly==0 && aCoord!=0 ){
203087 p = 0;
203088 mnX = aCoord[0].f;
203089 mxX = aCoord[1].f;
203090 mnY = aCoord[2].f;
203091 mxY = aCoord[3].f;
203092 goto geopolyBboxFill;
203093 }else{
203094 p = geopolyFuncParam(context, pPoly, pRc);
203095 }
203096 if( p ){
203097 int ii;
203098 mnX = mxX = GeoX(p,0);
203099 mnY = mxY = GeoY(p,0);
203100 for(ii=1; ii<p->nVertex; ii++){
203101 double r = GeoX(p,ii);
203102 if( r<mnX ) mnX = (float)r;
203103 else if( r>mxX ) mxX = (float)r;
203104 r = GeoY(p,ii);
203105 if( r<mnY ) mnY = (float)r;
203106 else if( r>mxY ) mxY = (float)r;
203107 }
203108 if( pRc ) *pRc = SQLITE_OK0;
203109 if( aCoord==0 ){
203110 geopolyBboxFill:
203111 pOut = sqlite3_realloc64(p, GEOPOLY_SZ(4));
203112 if( pOut==0 ){
203113 sqlite3_free(p);
203114 if( context ) sqlite3_result_error_nomem(context);
203115 if( pRc ) *pRc = SQLITE_NOMEM7;
203116 return 0;
203117 }
203118 pOut->nVertex = 4;
203119 ii = 1;
203120 pOut->hdr[0] = *(unsigned char*)&ii;
203121 pOut->hdr[1] = 0;
203122 pOut->hdr[2] = 0;
203123 pOut->hdr[3] = 4;
203124 GeoX(pOut,0) = mnX;
203125 GeoY(pOut,0) = mnY;
203126 GeoX(pOut,1) = mxX;
203127 GeoY(pOut,1) = mnY;
203128 GeoX(pOut,2) = mxX;
203129 GeoY(pOut,2) = mxY;
203130 GeoX(pOut,3) = mnX;
203131 GeoY(pOut,3) = mxY;
203132 }else{
203133 sqlite3_free(p);
203134 aCoord[0].f = mnX;
203135 aCoord[1].f = mxX;
203136 aCoord[2].f = mnY;
203137 aCoord[3].f = mxY;
203138 }
203139 }else if( aCoord ){
203140 memset(aCoord, 0, sizeof(RtreeCoord)*4);
203141 }
203142 return pOut;
203143}
203144
203145/*
203146** Implementation of the geopoly_bbox(X) SQL function.
203147*/
203148static void geopolyBBoxFunc(
203149 sqlite3_context *context,
203150 int argc,
203151 sqlite3_value **argv
203152){
203153 GeoPoly *p = geopolyBBox(context, argv[0], 0, 0);
203154 if( p ){
203155 sqlite3_result_blob(context, p->hdr,
203156 4+8*p->nVertex, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
203157 sqlite3_free(p);
203158 }
203159}
203160
203161/*
203162** State vector for the geopoly_group_bbox() aggregate function.
203163*/
203164typedef struct GeoBBox GeoBBox;
203165struct GeoBBox {
203166 int isInit;
203167 RtreeCoord a[4];
203168};
203169
203170
203171/*
203172** Implementation of the geopoly_group_bbox(X) aggregate SQL function.
203173*/
203174static void geopolyBBoxStep(
203175 sqlite3_context *context,
203176 int argc,
203177 sqlite3_value **argv
203178){
203179 RtreeCoord a[4];
203180 int rc = SQLITE_OK0;
203181 (void)geopolyBBox(context, argv[0], a, &rc);
203182 if( rc==SQLITE_OK0 ){
203183 GeoBBox *pBBox;
203184 pBBox = (GeoBBox*)sqlite3_aggregate_context(context, sizeof(*pBBox));
203185 if( pBBox==0 ) return;
203186 if( pBBox->isInit==0 ){
203187 pBBox->isInit = 1;
203188 memcpy(pBBox->a, a, sizeof(RtreeCoord)*4);
203189 }else{
203190 if( a[0].f < pBBox->a[0].f ) pBBox->a[0] = a[0];
203191 if( a[1].f > pBBox->a[1].f ) pBBox->a[1] = a[1];
203192 if( a[2].f < pBBox->a[2].f ) pBBox->a[2] = a[2];
203193 if( a[3].f > pBBox->a[3].f ) pBBox->a[3] = a[3];
203194 }
203195 }
203196}
203197static void geopolyBBoxFinal(
203198 sqlite3_context *context
203199){
203200 GeoPoly *p;
203201 GeoBBox *pBBox;
203202 pBBox = (GeoBBox*)sqlite3_aggregate_context(context, 0);
203203 if( pBBox==0 ) return;
203204 p = geopolyBBox(context, 0, pBBox->a, 0);
203205 if( p ){
203206 sqlite3_result_blob(context, p->hdr,
203207 4+8*p->nVertex, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
203208 sqlite3_free(p);
203209 }
203210}
203211
203212
203213/*
203214** Determine if point (x0,y0) is beneath line segment (x1,y1)->(x2,y2).
203215** Returns:
203216**
203217** +2 x0,y0 is on the line segement
203218**
203219** +1 x0,y0 is beneath line segment
203220**
203221** 0 x0,y0 is not on or beneath the line segment or the line segment
203222** is vertical and x0,y0 is not on the line segment
203223**
203224** The left-most coordinate min(x1,x2) is not considered to be part of
203225** the line segment for the purposes of this analysis.
203226*/
203227static int pointBeneathLine(
203228 double x0, double y0,
203229 double x1, double y1,
203230 double x2, double y2
203231){
203232 double y;
203233 if( x0==x1 && y0==y1 ) return 2;
203234 if( x1<x2 ){
203235 if( x0<=x1 || x0>x2 ) return 0;
203236 }else if( x1>x2 ){
203237 if( x0<=x2 || x0>x1 ) return 0;
203238 }else{
203239 /* Vertical line segment */
203240 if( x0!=x1 ) return 0;
203241 if( y0<y1 && y0<y2 ) return 0;
203242 if( y0>y1 && y0>y2 ) return 0;
203243 return 2;
203244 }
203245 y = y1 + (y2-y1)*(x0-x1)/(x2-x1);
203246 if( y0==y ) return 2;
203247 if( y0<y ) return 1;
203248 return 0;
203249}
203250
203251/*
203252** SQL function: geopoly_contains_point(P,X,Y)
203253**
203254** Return +2 if point X,Y is within polygon P.
203255** Return +1 if point X,Y is on the polygon boundary.
203256** Return 0 if point X,Y is outside the polygon
203257*/
203258static void geopolyContainsPointFunc(
203259 sqlite3_context *context,
203260 int argc,
203261 sqlite3_value **argv
203262){
203263 GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
203264 double x0 = sqlite3_value_double(argv[1]);
203265 double y0 = sqlite3_value_double(argv[2]);
203266 int v = 0;
203267 int cnt = 0;
203268 int ii;
203269 if( p1==0 ) return;
203270 for(ii=0; ii<p1->nVertex-1; ii++){
203271 v = pointBeneathLine(x0,y0,GeoX(p1,ii), GeoY(p1,ii),
203272 GeoX(p1,ii+1),GeoY(p1,ii+1));
203273 if( v==2 ) break;
203274 cnt += v;
203275 }
203276 if( v!=2 ){
203277 v = pointBeneathLine(x0,y0,GeoX(p1,ii), GeoY(p1,ii),
203278 GeoX(p1,0), GeoY(p1,0));
203279 }
203280 if( v==2 ){
203281 sqlite3_result_int(context, 1);
203282 }else if( ((v+cnt)&1)==0 ){
203283 sqlite3_result_int(context, 0);
203284 }else{
203285 sqlite3_result_int(context, 2);
203286 }
203287 sqlite3_free(p1);
203288}
203289
203290/* Forward declaration */
203291static int geopolyOverlap(GeoPoly *p1, GeoPoly *p2);
203292
203293/*
203294** SQL function: geopoly_within(P1,P2)
203295**
203296** Return +2 if P1 and P2 are the same polygon
203297** Return +1 if P2 is contained within P1
203298** Return 0 if any part of P2 is on the outside of P1
203299**
203300*/
203301static void geopolyWithinFunc(
203302 sqlite3_context *context,
203303 int argc,
203304 sqlite3_value **argv
203305){
203306 GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
203307 GeoPoly *p2 = geopolyFuncParam(context, argv[1], 0);
203308 if( p1 && p2 ){
203309 int x = geopolyOverlap(p1, p2);
203310 if( x<0 ){
203311 sqlite3_result_error_nomem(context);
203312 }else{
203313 sqlite3_result_int(context, x==2 ? 1 : x==4 ? 2 : 0);
203314 }
203315 }
203316 sqlite3_free(p1);
203317 sqlite3_free(p2);
203318}
203319
203320/* Objects used by the overlap algorihm. */
203321typedef struct GeoEvent GeoEvent;
203322typedef struct GeoSegment GeoSegment;
203323typedef struct GeoOverlap GeoOverlap;
203324struct GeoEvent {
203325 double x; /* X coordinate at which event occurs */
203326 int eType; /* 0 for ADD, 1 for REMOVE */
203327 GeoSegment *pSeg; /* The segment to be added or removed */
203328 GeoEvent *pNext; /* Next event in the sorted list */
203329};
203330struct GeoSegment {
203331 double C, B; /* y = C*x + B */
203332 double y; /* Current y value */
203333 float y0; /* Initial y value */
203334 unsigned char side; /* 1 for p1, 2 for p2 */
203335 unsigned int idx; /* Which segment within the side */
203336 GeoSegment *pNext; /* Next segment in a list sorted by y */
203337};
203338struct GeoOverlap {
203339 GeoEvent *aEvent; /* Array of all events */
203340 GeoSegment *aSegment; /* Array of all segments */
203341 int nEvent; /* Number of events */
203342 int nSegment; /* Number of segments */
203343};
203344
203345/*
203346** Add a single segment and its associated events.
203347*/
203348static void geopolyAddOneSegment(
203349 GeoOverlap *p,
203350 GeoCoord x0,
203351 GeoCoord y0,
203352 GeoCoord x1,
203353 GeoCoord y1,
203354 unsigned char side,
203355 unsigned int idx
203356){
203357 GeoSegment *pSeg;
203358 GeoEvent *pEvent;
203359 if( x0==x1 ) return; /* Ignore vertical segments */
203360 if( x0>x1 ){
203361 GeoCoord t = x0;
203362 x0 = x1;
203363 x1 = t;
203364 t = y0;
203365 y0 = y1;
203366 y1 = t;
203367 }
203368 pSeg = p->aSegment + p->nSegment;
203369 p->nSegment++;
203370 pSeg->C = (y1-y0)/(x1-x0);
203371 pSeg->B = y1 - x1*pSeg->C;
203372 pSeg->y0 = y0;
203373 pSeg->side = side;
203374 pSeg->idx = idx;
203375 pEvent = p->aEvent + p->nEvent;
203376 p->nEvent++;
203377 pEvent->x = x0;
203378 pEvent->eType = 0;
203379 pEvent->pSeg = pSeg;
203380 pEvent = p->aEvent + p->nEvent;
203381 p->nEvent++;
203382 pEvent->x = x1;
203383 pEvent->eType = 1;
203384 pEvent->pSeg = pSeg;
203385}
203386
203387
203388
203389/*
203390** Insert all segments and events for polygon pPoly.
203391*/
203392static void geopolyAddSegments(
203393 GeoOverlap *p, /* Add segments to this Overlap object */
203394 GeoPoly *pPoly, /* Take all segments from this polygon */
203395 unsigned char side /* The side of pPoly */
203396){
203397 unsigned int i;
203398 GeoCoord *x;
203399 for(i=0; i<(unsigned)pPoly->nVertex-1; i++){
203400 x = &GeoX(pPoly,i);
203401 geopolyAddOneSegment(p, x[0], x[1], x[2], x[3], side, i);
203402 }
203403 x = &GeoX(pPoly,i);
203404 geopolyAddOneSegment(p, x[0], x[1], pPoly->a[0], pPoly->a[1], side, i);
203405}
203406
203407/*
203408** Merge two lists of sorted events by X coordinate
203409*/
203410static GeoEvent *geopolyEventMerge(GeoEvent *pLeft, GeoEvent *pRight){
203411 GeoEvent head, *pLast;
203412 head.pNext = 0;
203413 pLast = &head;
203414 while( pRight && pLeft ){
203415 if( pRight->x <= pLeft->x ){
203416 pLast->pNext = pRight;
203417 pLast = pRight;
203418 pRight = pRight->pNext;
203419 }else{
203420 pLast->pNext = pLeft;
203421 pLast = pLeft;
203422 pLeft = pLeft->pNext;
203423 }
203424 }
203425 pLast->pNext = pRight ? pRight : pLeft;
203426 return head.pNext;
203427}
203428
203429/*
203430** Sort an array of nEvent event objects into a list.
203431*/
203432static GeoEvent *geopolySortEventsByX(GeoEvent *aEvent, int nEvent){
203433 int mx = 0;
203434 int i, j;
203435 GeoEvent *p;
203436 GeoEvent *a[50];
203437 for(i=0; i<nEvent; i++){
203438 p = &aEvent[i];
203439 p->pNext = 0;
203440 for(j=0; j<mx && a[j]; j++){
203441 p = geopolyEventMerge(a[j], p);
203442 a[j] = 0;
203443 }
203444 a[j] = p;
203445 if( j>=mx ) mx = j+1;
203446 }
203447 p = 0;
203448 for(i=0; i<mx; i++){
203449 p = geopolyEventMerge(a[i], p);
203450 }
203451 return p;
203452}
203453
203454/*
203455** Merge two lists of sorted segments by Y, and then by C.
203456*/
203457static GeoSegment *geopolySegmentMerge(GeoSegment *pLeft, GeoSegment *pRight){
203458 GeoSegment head, *pLast;
203459 head.pNext = 0;
203460 pLast = &head;
203461 while( pRight && pLeft ){
203462 double r = pRight->y - pLeft->y;
203463 if( r==0.0 ) r = pRight->C - pLeft->C;
203464 if( r<0.0 ){
203465 pLast->pNext = pRight;
203466 pLast = pRight;
203467 pRight = pRight->pNext;
203468 }else{
203469 pLast->pNext = pLeft;
203470 pLast = pLeft;
203471 pLeft = pLeft->pNext;
203472 }
203473 }
203474 pLast->pNext = pRight ? pRight : pLeft;
203475 return head.pNext;
203476}
203477
203478/*
203479** Sort a list of GeoSegments in order of increasing Y and in the event of
203480** a tie, increasing C (slope).
203481*/
203482static GeoSegment *geopolySortSegmentsByYAndC(GeoSegment *pList){
203483 int mx = 0;
203484 int i;
203485 GeoSegment *p;
203486 GeoSegment *a[50];
203487 while( pList ){
203488 p = pList;
203489 pList = pList->pNext;
203490 p->pNext = 0;
203491 for(i=0; i<mx && a[i]; i++){
203492 p = geopolySegmentMerge(a[i], p);
203493 a[i] = 0;
203494 }
203495 a[i] = p;
203496 if( i>=mx ) mx = i+1;
203497 }
203498 p = 0;
203499 for(i=0; i<mx; i++){
203500 p = geopolySegmentMerge(a[i], p);
203501 }
203502 return p;
203503}
203504
203505/*
203506** Determine the overlap between two polygons
203507*/
203508static int geopolyOverlap(GeoPoly *p1, GeoPoly *p2){
203509 sqlite3_int64 nVertex = p1->nVertex + p2->nVertex + 2;
203510 GeoOverlap *p;
203511 sqlite3_int64 nByte;
203512 GeoEvent *pThisEvent;
203513 double rX;
203514 int rc = 0;
203515 int needSort = 0;
203516 GeoSegment *pActive = 0;
203517 GeoSegment *pSeg;
203518 unsigned char aOverlap[4];
203519
203520 nByte = sizeof(GeoEvent)*nVertex*2
203521 + sizeof(GeoSegment)*nVertex
203522 + sizeof(GeoOverlap);
203523 p = sqlite3_malloc64( nByte );
203524 if( p==0 ) return -1;
203525 p->aEvent = (GeoEvent*)&p[1];
203526 p->aSegment = (GeoSegment*)&p->aEvent[nVertex*2];
203527 p->nEvent = p->nSegment = 0;
203528 geopolyAddSegments(p, p1, 1);
203529 geopolyAddSegments(p, p2, 2);
203530 pThisEvent = geopolySortEventsByX(p->aEvent, p->nEvent);
203531 rX = pThisEvent && pThisEvent->x==0.0 ? -1.0 : 0.0;
203532 memset(aOverlap, 0, sizeof(aOverlap));
203533 while( pThisEvent ){
203534 if( pThisEvent->x!=rX ){
203535 GeoSegment *pPrev = 0;
203536 int iMask = 0;
203537 GEODEBUG(("Distinct X: %g\n", pThisEvent->x));
203538 rX = pThisEvent->x;
203539 if( needSort ){
203540 GEODEBUG(("SORT\n"));
203541 pActive = geopolySortSegmentsByYAndC(pActive);
203542 needSort = 0;
203543 }
203544 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
203545 if( pPrev ){
203546 if( pPrev->y!=pSeg->y ){
203547 GEODEBUG(("MASK: %d\n", iMask));
203548 aOverlap[iMask] = 1;
203549 }
203550 }
203551 iMask ^= pSeg->side;
203552 pPrev = pSeg;
203553 }
203554 pPrev = 0;
203555 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
203556 double y = pSeg->C*rX + pSeg->B;
203557 GEODEBUG(("Segment %d.%d %g->%g\n", pSeg->side, pSeg->idx, pSeg->y, y));
203558 pSeg->y = y;
203559 if( pPrev ){
203560 if( pPrev->y>pSeg->y && pPrev->side!=pSeg->side ){
203561 rc = 1;
203562 GEODEBUG(("Crossing: %d.%d and %d.%d\n",
203563 pPrev->side, pPrev->idx,
203564 pSeg->side, pSeg->idx));
203565 goto geopolyOverlapDone;
203566 }else if( pPrev->y!=pSeg->y ){
203567 GEODEBUG(("MASK: %d\n", iMask));
203568 aOverlap[iMask] = 1;
203569 }
203570 }
203571 iMask ^= pSeg->side;
203572 pPrev = pSeg;
203573 }
203574 }
203575 GEODEBUG(("%s %d.%d C=%g B=%g\n",
203576 pThisEvent->eType ? "RM " : "ADD",
203577 pThisEvent->pSeg->side, pThisEvent->pSeg->idx,
203578 pThisEvent->pSeg->C,
203579 pThisEvent->pSeg->B));
203580 if( pThisEvent->eType==0 ){
203581 /* Add a segment */
203582 pSeg = pThisEvent->pSeg;
203583 pSeg->y = pSeg->y0;
203584 pSeg->pNext = pActive;
203585 pActive = pSeg;
203586 needSort = 1;
203587 }else{
203588 /* Remove a segment */
203589 if( pActive==pThisEvent->pSeg ){
203590 pActive = ALWAYS(pActive)(pActive) ? pActive->pNext : 0;
203591 }else{
203592 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
203593 if( pSeg->pNext==pThisEvent->pSeg ){
203594 pSeg->pNext = ALWAYS(pSeg->pNext)(pSeg->pNext) ? pSeg->pNext->pNext : 0;
203595 break;
203596 }
203597 }
203598 }
203599 }
203600 pThisEvent = pThisEvent->pNext;
203601 }
203602 if( aOverlap[3]==0 ){
203603 rc = 0;
203604 }else if( aOverlap[1]!=0 && aOverlap[2]==0 ){
203605 rc = 3;
203606 }else if( aOverlap[1]==0 && aOverlap[2]!=0 ){
203607 rc = 2;
203608 }else if( aOverlap[1]==0 && aOverlap[2]==0 ){
203609 rc = 4;
203610 }else{
203611 rc = 1;
203612 }
203613
203614geopolyOverlapDone:
203615 sqlite3_free(p);
203616 return rc;
203617}
203618
203619/*
203620** SQL function: geopoly_overlap(P1,P2)
203621**
203622** Determine whether or not P1 and P2 overlap. Return value:
203623**
203624** 0 The two polygons are disjoint
203625** 1 They overlap
203626** 2 P1 is completely contained within P2
203627** 3 P2 is completely contained within P1
203628** 4 P1 and P2 are the same polygon
203629** NULL Either P1 or P2 or both are not valid polygons
203630*/
203631static void geopolyOverlapFunc(
203632 sqlite3_context *context,
203633 int argc,
203634 sqlite3_value **argv
203635){
203636 GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
203637 GeoPoly *p2 = geopolyFuncParam(context, argv[1], 0);
203638 if( p1 && p2 ){
203639 int x = geopolyOverlap(p1, p2);
203640 if( x<0 ){
203641 sqlite3_result_error_nomem(context);
203642 }else{
203643 sqlite3_result_int(context, x);
203644 }
203645 }
203646 sqlite3_free(p1);
203647 sqlite3_free(p2);
203648}
203649
203650/*
203651** Enable or disable debugging output
203652*/
203653static void geopolyDebugFunc(
203654 sqlite3_context *context,
203655 int argc,
203656 sqlite3_value **argv
203657){
203658#ifdef GEOPOLY_ENABLE_DEBUG
203659 geo_debug = sqlite3_value_int(argv[0]);
203660#endif
203661}
203662
203663/*
203664** This function is the implementation of both the xConnect and xCreate
203665** methods of the geopoly virtual table.
203666**
203667** argv[0] -> module name
203668** argv[1] -> database name
203669** argv[2] -> table name
203670** argv[...] -> column names...
203671*/
203672static int geopolyInit(
203673 sqlite3 *db, /* Database connection */
203674 void *pAux, /* One of the RTREE_COORD_* constants */
203675 int argc, const char *const*argv, /* Parameters to CREATE TABLE statement */
203676 sqlite3_vtab **ppVtab, /* OUT: New virtual table */
203677 char **pzErr, /* OUT: Error message, if any */
203678 int isCreate /* True for xCreate, false for xConnect */
203679){
203680 int rc = SQLITE_OK0;
203681 Rtree *pRtree;
203682 sqlite3_int64 nDb; /* Length of string argv[1] */
203683 sqlite3_int64 nName; /* Length of string argv[2] */
203684 sqlite3_str *pSql;
203685 char *zSql;
203686 int ii;
203687
203688 sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT1, 1);
203689
203690 /* Allocate the sqlite3_vtab structure */
203691 nDb = strlen(argv[1]);
203692 nName = strlen(argv[2]);
203693 pRtree = (Rtree *)sqlite3_malloc64(sizeof(Rtree)+nDb+nName+2);
203694 if( !pRtree ){
203695 return SQLITE_NOMEM7;
203696 }
203697 memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
203698 pRtree->nBusy = 1;
203699 pRtree->base.pModule = &rtreeModule;
203700 pRtree->zDb = (char *)&pRtree[1];
203701 pRtree->zName = &pRtree->zDb[nDb+1];
203702 pRtree->eCoordType = RTREE_COORD_REAL320;
203703 pRtree->nDim = 2;
203704 pRtree->nDim2 = 4;
203705 memcpy(pRtree->zDb, argv[1], nDb);
203706 memcpy(pRtree->zName, argv[2], nName);
203707
203708
203709 /* Create/Connect to the underlying relational database schema. If
203710 ** that is successful, call sqlite3_declare_vtab() to configure
203711 ** the r-tree table schema.
203712 */
203713 pSql = sqlite3_str_new(db);
203714 sqlite3_str_appendf(pSql, "CREATE TABLE x(_shape");
203715 pRtree->nAux = 1; /* Add one for _shape */
203716 pRtree->nAuxNotNull = 1; /* The _shape column is always not-null */
203717 for(ii=3; ii<argc; ii++){
203718 pRtree->nAux++;
203719 sqlite3_str_appendf(pSql, ",%s", argv[ii]);
203720 }
203721 sqlite3_str_appendf(pSql, ");");
203722 zSql = sqlite3_str_finish(pSql);
203723 if( !zSql ){
203724 rc = SQLITE_NOMEM7;
203725 }else if( SQLITE_OK0!=(rc = sqlite3_declare_vtab(db, zSql)) ){
203726 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
203727 }
203728 sqlite3_free(zSql);
203729 if( rc ) goto geopolyInit_fail;
203730 pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
203731
203732 /* Figure out the node size to use. */
203733 rc = getNodeSize(db, pRtree, isCreate, pzErr);
203734 if( rc ) goto geopolyInit_fail;
203735 rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate);
203736 if( rc ){
203737 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
203738 goto geopolyInit_fail;
203739 }
203740
203741 *ppVtab = (sqlite3_vtab *)pRtree;
203742 return SQLITE_OK0;
203743
203744geopolyInit_fail:
203745 if( rc==SQLITE_OK0 ) rc = SQLITE_ERROR1;
203746 assert( *ppVtab==0 )((void) (0));
203747 assert( pRtree->nBusy==1 )((void) (0));
203748 rtreeRelease(pRtree);
203749 return rc;
203750}
203751
203752
203753/*
203754** GEOPOLY virtual table module xCreate method.
203755*/
203756static int geopolyCreate(
203757 sqlite3 *db,
203758 void *pAux,
203759 int argc, const char *const*argv,
203760 sqlite3_vtab **ppVtab,
203761 char **pzErr
203762){
203763 return geopolyInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
203764}
203765
203766/*
203767** GEOPOLY virtual table module xConnect method.
203768*/
203769static int geopolyConnect(
203770 sqlite3 *db,
203771 void *pAux,
203772 int argc, const char *const*argv,
203773 sqlite3_vtab **ppVtab,
203774 char **pzErr
203775){
203776 return geopolyInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
203777}
203778
203779
203780/*
203781** GEOPOLY virtual table module xFilter method.
203782**
203783** Query plans:
203784**
203785** 1 rowid lookup
203786** 2 search for objects overlapping the same bounding box
203787** that contains polygon argv[0]
203788** 3 search for objects overlapping the same bounding box
203789** that contains polygon argv[0]
203790** 4 full table scan
203791*/
203792static int geopolyFilter(
203793 sqlite3_vtab_cursor *pVtabCursor, /* The cursor to initialize */
203794 int idxNum, /* Query plan */
203795 const char *idxStr, /* Not Used */
203796 int argc, sqlite3_value **argv /* Parameters to the query plan */
203797){
203798 Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
203799 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
203800 RtreeNode *pRoot = 0;
203801 int rc = SQLITE_OK0;
203802 int iCell = 0;
203803
203804 rtreeReference(pRtree);
203805
203806 /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
203807 resetCursor(pCsr);
203808
203809 pCsr->iStrategy = idxNum;
203810 if( idxNum==1 ){
203811 /* Special case - lookup by rowid. */
203812 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
203813 RtreeSearchPoint *p; /* Search point for the leaf */
203814 i64 iRowid = sqlite3_value_int64(argv[0]);
203815 i64 iNode = 0;
203816 rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
203817 if( rc==SQLITE_OK0 && pLeaf!=0 ){
203818 p = rtreeSearchPointNew(pCsr, RTREE_ZERO0.0, 0);
203819 assert( p!=0 )((void) (0)); /* Always returns pCsr->sPoint */
203820 pCsr->aNode[0] = pLeaf;
203821 p->id = iNode;
203822 p->eWithin = PARTLY_WITHIN1;
203823 rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
203824 p->iCell = (u8)iCell;
203825 RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
203826 }else{
203827 pCsr->atEOF = 1;
203828 }
203829 }else{
203830 /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
203831 ** with the configured constraints.
203832 */
203833 rc = nodeAcquire(pRtree, 1, 0, &pRoot);
203834 if( rc==SQLITE_OK0 && idxNum<=3 ){
203835 RtreeCoord bbox[4];
203836 RtreeConstraint *p;
203837 assert( argc==1 )((void) (0));
203838 assert( argv[0]!=0 )((void) (0));
203839 geopolyBBox(0, argv[0], bbox, &rc);
203840 if( rc ){
203841 goto geopoly_filter_end;
203842 }
203843 pCsr->aConstraint = p = sqlite3_malloc(sizeof(RtreeConstraint)*4);
203844 pCsr->nConstraint = 4;
203845 if( p==0 ){
203846 rc = SQLITE_NOMEM7;
203847 }else{
203848 memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*4);
203849 memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1));
203850 if( idxNum==2 ){
203851 /* Overlap query */
203852 p->op = 'B';
203853 p->iCoord = 0;
203854 p->u.rValue = bbox[1].f;
203855 p++;
203856 p->op = 'D';
203857 p->iCoord = 1;
203858 p->u.rValue = bbox[0].f;
203859 p++;
203860 p->op = 'B';
203861 p->iCoord = 2;
203862 p->u.rValue = bbox[3].f;
203863 p++;
203864 p->op = 'D';
203865 p->iCoord = 3;
203866 p->u.rValue = bbox[2].f;
203867 }else{
203868 /* Within query */
203869 p->op = 'D';
203870 p->iCoord = 0;
203871 p->u.rValue = bbox[0].f;
203872 p++;
203873 p->op = 'B';
203874 p->iCoord = 1;
203875 p->u.rValue = bbox[1].f;
203876 p++;
203877 p->op = 'D';
203878 p->iCoord = 2;
203879 p->u.rValue = bbox[2].f;
203880 p++;
203881 p->op = 'B';
203882 p->iCoord = 3;
203883 p->u.rValue = bbox[3].f;
203884 }
203885 }
203886 }
203887 if( rc==SQLITE_OK0 ){
203888 RtreeSearchPoint *pNew;
203889 pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO0.0, (u8)(pRtree->iDepth+1));
203890 if( pNew==0 ){
203891 rc = SQLITE_NOMEM7;
203892 goto geopoly_filter_end;
203893 }
203894 pNew->id = 1;
203895 pNew->iCell = 0;
203896 pNew->eWithin = PARTLY_WITHIN1;
203897 assert( pCsr->bPoint==1 )((void) (0));
203898 pCsr->aNode[0] = pRoot;
203899 pRoot = 0;
203900 RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
203901 rc = rtreeStepToLeaf(pCsr);
203902 }
203903 }
203904
203905geopoly_filter_end:
203906 nodeRelease(pRtree, pRoot);
203907 rtreeRelease(pRtree);
203908 return rc;
203909}
203910
203911/*
203912** Rtree virtual table module xBestIndex method. There are three
203913** table scan strategies to choose from (in order from most to
203914** least desirable):
203915**
203916** idxNum idxStr Strategy
203917** ------------------------------------------------
203918** 1 "rowid" Direct lookup by rowid.
203919** 2 "rtree" R-tree overlap query using geopoly_overlap()
203920** 3 "rtree" R-tree within query using geopoly_within()
203921** 4 "fullscan" full-table scan.
203922** ------------------------------------------------
203923*/
203924static int geopolyBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
203925 int ii;
203926 int iRowidTerm = -1;
203927 int iFuncTerm = -1;
203928 int idxNum = 0;
203929
203930 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
203931 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
203932 if( !p->usable ) continue;
203933 if( p->iColumn<0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ2 ){
203934 iRowidTerm = ii;
203935 break;
203936 }
203937 if( p->iColumn==0 && p->op>=SQLITE_INDEX_CONSTRAINT_FUNCTION150 ){
203938 /* p->op==SQLITE_INDEX_CONSTRAINT_FUNCTION for geopoly_overlap()
203939 ** p->op==(SQLITE_INDEX_CONTRAINT_FUNCTION+1) for geopoly_within().
203940 ** See geopolyFindFunction() */
203941 iFuncTerm = ii;
203942 idxNum = p->op - SQLITE_INDEX_CONSTRAINT_FUNCTION150 + 2;
203943 }
203944 }
203945
203946 if( iRowidTerm>=0 ){
203947 pIdxInfo->idxNum = 1;
203948 pIdxInfo->idxStr = "rowid";
203949 pIdxInfo->aConstraintUsage[iRowidTerm].argvIndex = 1;
203950 pIdxInfo->aConstraintUsage[iRowidTerm].omit = 1;
203951 pIdxInfo->estimatedCost = 30.0;
203952 pIdxInfo->estimatedRows = 1;
203953 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE1;
203954 return SQLITE_OK0;
203955 }
203956 if( iFuncTerm>=0 ){
203957 pIdxInfo->idxNum = idxNum;
203958 pIdxInfo->idxStr = "rtree";
203959 pIdxInfo->aConstraintUsage[iFuncTerm].argvIndex = 1;
203960 pIdxInfo->aConstraintUsage[iFuncTerm].omit = 0;
203961 pIdxInfo->estimatedCost = 300.0;
203962 pIdxInfo->estimatedRows = 10;
203963 return SQLITE_OK0;
203964 }
203965 pIdxInfo->idxNum = 4;
203966 pIdxInfo->idxStr = "fullscan";
203967 pIdxInfo->estimatedCost = 3000000.0;
203968 pIdxInfo->estimatedRows = 100000;
203969 return SQLITE_OK0;
203970}
203971
203972
203973/*
203974** GEOPOLY virtual table module xColumn method.
203975*/
203976static int geopolyColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
203977 Rtree *pRtree = (Rtree *)cur->pVtab;
203978 RtreeCursor *pCsr = (RtreeCursor *)cur;
203979 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
203980 int rc = SQLITE_OK0;
203981 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
203982
203983 if( rc ) return rc;
203984 if( p==0 ) return SQLITE_OK0;
203985 if( i==0 && sqlite3_vtab_nochange(ctx) ) return SQLITE_OK0;
203986 if( i<=pRtree->nAux ){
203987 if( !pCsr->bAuxValid ){
203988 if( pCsr->pReadAux==0 ){
203989 rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0,
203990 &pCsr->pReadAux, 0);
203991 if( rc ) return rc;
203992 }
203993 sqlite3_bind_int64(pCsr->pReadAux, 1,
203994 nodeGetRowid(pRtree, pNode, p->iCell));
203995 rc = sqlite3_step(pCsr->pReadAux);
203996 if( rc==SQLITE_ROW100 ){
203997 pCsr->bAuxValid = 1;
203998 }else{
203999 sqlite3_reset(pCsr->pReadAux);
204000 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
204001 return rc;
204002 }
204003 }
204004 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pReadAux, i+2));
204005 }
204006 return SQLITE_OK0;
204007}
204008
204009
204010/*
204011** The xUpdate method for GEOPOLY module virtual tables.
204012**
204013** For DELETE:
204014**
204015** argv[0] = the rowid to be deleted
204016**
204017** For INSERT:
204018**
204019** argv[0] = SQL NULL
204020** argv[1] = rowid to insert, or an SQL NULL to select automatically
204021** argv[2] = _shape column
204022** argv[3] = first application-defined column....
204023**
204024** For UPDATE:
204025**
204026** argv[0] = rowid to modify. Never NULL
204027** argv[1] = rowid after the change. Never NULL
204028** argv[2] = new value for _shape
204029** argv[3] = new value for first application-defined column....
204030*/
204031static int geopolyUpdate(
204032 sqlite3_vtab *pVtab,
204033 int nData,
204034 sqlite3_value **aData,
204035 sqlite_int64 *pRowid
204036){
204037 Rtree *pRtree = (Rtree *)pVtab;
204038 int rc = SQLITE_OK0;
204039 RtreeCell cell; /* New cell to insert if nData>1 */
204040 i64 oldRowid; /* The old rowid */
204041 int oldRowidValid; /* True if oldRowid is valid */
204042 i64 newRowid; /* The new rowid */
204043 int newRowidValid; /* True if newRowid is valid */
204044 int coordChange = 0; /* Change in coordinates */
204045
204046 if( pRtree->nNodeRef ){
204047 /* Unable to write to the btree while another cursor is reading from it,
204048 ** since the write might do a rebalance which would disrupt the read
204049 ** cursor. */
204050 return SQLITE_LOCKED_VTAB(6 | (2<<8));
204051 }
204052 rtreeReference(pRtree);
204053 assert(nData>=1)((void) (0));
204054
204055 oldRowidValid = sqlite3_value_type(aData[0])!=SQLITE_NULL5;;
204056 oldRowid = oldRowidValid ? sqlite3_value_int64(aData[0]) : 0;
204057 newRowidValid = nData>1 && sqlite3_value_type(aData[1])!=SQLITE_NULL5;
204058 newRowid = newRowidValid ? sqlite3_value_int64(aData[1]) : 0;
204059 cell.iRowid = newRowid;
204060
204061 if( nData>1 /* not a DELETE */
204062 && (!oldRowidValid /* INSERT */
204063 || !sqlite3_value_nochange(aData[2]) /* UPDATE _shape */
204064 || oldRowid!=newRowid) /* Rowid change */
204065 ){
204066 assert( aData[2]!=0 )((void) (0));
204067 geopolyBBox(0, aData[2], cell.aCoord, &rc);
204068 if( rc ){
204069 if( rc==SQLITE_ERROR1 ){
204070 pVtab->zErrMsg =
204071 sqlite3_mprintf("_shape does not contain a valid polygon");
204072 }
204073 goto geopoly_update_end;
204074 }
204075 coordChange = 1;
204076
204077 /* If a rowid value was supplied, check if it is already present in
204078 ** the table. If so, the constraint has failed. */
204079 if( newRowidValid && (!oldRowidValid || oldRowid!=newRowid) ){
204080 int steprc;
204081 sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
204082 steprc = sqlite3_step(pRtree->pReadRowid);
204083 rc = sqlite3_reset(pRtree->pReadRowid);
204084 if( SQLITE_ROW100==steprc ){
204085 if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE5 ){
204086 rc = rtreeDeleteRowid(pRtree, cell.iRowid);
204087 }else{
204088 rc = rtreeConstraintError(pRtree, 0);
204089 }
204090 }
204091 }
204092 }
204093
204094 /* If aData[0] is not an SQL NULL value, it is the rowid of a
204095 ** record to delete from the r-tree table. The following block does
204096 ** just that.
204097 */
204098 if( rc==SQLITE_OK0 && (nData==1 || (coordChange && oldRowidValid)) ){
204099 rc = rtreeDeleteRowid(pRtree, oldRowid);
204100 }
204101
204102 /* If the aData[] array contains more than one element, elements
204103 ** (aData[2]..aData[argc-1]) contain a new record to insert into
204104 ** the r-tree structure.
204105 */
204106 if( rc==SQLITE_OK0 && nData>1 && coordChange ){
204107 /* Insert the new record into the r-tree */
204108 RtreeNode *pLeaf = 0;
204109 if( !newRowidValid ){
204110 rc = rtreeNewRowid(pRtree, &cell.iRowid);
204111 }
204112 *pRowid = cell.iRowid;
204113 if( rc==SQLITE_OK0 ){
204114 rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
204115 }
204116 if( rc==SQLITE_OK0 ){
204117 int rc2;
204118 pRtree->iReinsertHeight = -1;
204119 rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
204120 rc2 = nodeRelease(pRtree, pLeaf);
204121 if( rc==SQLITE_OK0 ){
204122 rc = rc2;
204123 }
204124 }
204125 }
204126
204127 /* Change the data */
204128 if( rc==SQLITE_OK0 && nData>1 ){
204129 sqlite3_stmt *pUp = pRtree->pWriteAux;
204130 int jj;
204131 int nChange = 0;
204132 sqlite3_bind_int64(pUp, 1, cell.iRowid);
204133 assert( pRtree->nAux>=1 )((void) (0));
204134 if( sqlite3_value_nochange(aData[2]) ){
204135 sqlite3_bind_null(pUp, 2);
204136 }else{
204137 GeoPoly *p = 0;
204138 if( sqlite3_value_type(aData[2])==SQLITE_TEXT3
204139 && (p = geopolyFuncParam(0, aData[2], &rc))!=0
204140 && rc==SQLITE_OK0
204141 ){
204142 sqlite3_bind_blob(pUp, 2, p->hdr, 4+8*p->nVertex, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
204143 }else{
204144 sqlite3_bind_value(pUp, 2, aData[2]);
204145 }
204146 sqlite3_free(p);
204147 nChange = 1;
204148 }
204149 for(jj=1; jj<pRtree->nAux; jj++){
204150 nChange++;
204151 sqlite3_bind_value(pUp, jj+2, aData[jj+2]);
204152 }
204153 if( nChange ){
204154 sqlite3_step(pUp);
204155 rc = sqlite3_reset(pUp);
204156 }
204157 }
204158
204159geopoly_update_end:
204160 rtreeRelease(pRtree);
204161 return rc;
204162}
204163
204164/*
204165** Report that geopoly_overlap() is an overloaded function suitable
204166** for use in xBestIndex.
204167*/
204168static int geopolyFindFunction(
204169 sqlite3_vtab *pVtab,
204170 int nArg,
204171 const char *zName,
204172 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
204173 void **ppArg
204174){
204175 if( sqlite3_stricmp(zName, "geopoly_overlap")==0 ){
204176 *pxFunc = geopolyOverlapFunc;
204177 *ppArg = 0;
204178 return SQLITE_INDEX_CONSTRAINT_FUNCTION150;
204179 }
204180 if( sqlite3_stricmp(zName, "geopoly_within")==0 ){
204181 *pxFunc = geopolyWithinFunc;
204182 *ppArg = 0;
204183 return SQLITE_INDEX_CONSTRAINT_FUNCTION150+1;
204184 }
204185 return 0;
204186}
204187
204188
204189static sqlite3_module geopolyModule = {
204190 3, /* iVersion */
204191 geopolyCreate, /* xCreate - create a table */
204192 geopolyConnect, /* xConnect - connect to an existing table */
204193 geopolyBestIndex, /* xBestIndex - Determine search strategy */
204194 rtreeDisconnect, /* xDisconnect - Disconnect from a table */
204195 rtreeDestroy, /* xDestroy - Drop a table */
204196 rtreeOpen, /* xOpen - open a cursor */
204197 rtreeClose, /* xClose - close a cursor */
204198 geopolyFilter, /* xFilter - configure scan constraints */
204199 rtreeNext, /* xNext - advance a cursor */
204200 rtreeEof, /* xEof */
204201 geopolyColumn, /* xColumn - read data */
204202 rtreeRowid, /* xRowid - read data */
204203 geopolyUpdate, /* xUpdate - write data */
204204 rtreeBeginTransaction, /* xBegin - begin transaction */
204205 rtreeEndTransaction, /* xSync - sync transaction */
204206 rtreeEndTransaction, /* xCommit - commit transaction */
204207 rtreeEndTransaction, /* xRollback - rollback transaction */
204208 geopolyFindFunction, /* xFindFunction - function overloading */
204209 rtreeRename, /* xRename - rename the table */
204210 rtreeSavepoint, /* xSavepoint */
204211 0, /* xRelease */
204212 0, /* xRollbackTo */
204213 rtreeShadowName /* xShadowName */
204214};
204215
204216static int sqlite3_geopoly_init(sqlite3 *db){
204217 int rc = SQLITE_OK0;
204218 static const struct {
204219 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
204220 signed char nArg;
204221 unsigned char bPure;
204222 const char *zName;
204223 } aFunc[] = {
204224 { geopolyAreaFunc, 1, 1, "geopoly_area" },
204225 { geopolyBlobFunc, 1, 1, "geopoly_blob" },
204226 { geopolyJsonFunc, 1, 1, "geopoly_json" },
204227 { geopolySvgFunc, -1, 1, "geopoly_svg" },
204228 { geopolyWithinFunc, 2, 1, "geopoly_within" },
204229 { geopolyContainsPointFunc, 3, 1, "geopoly_contains_point" },
204230 { geopolyOverlapFunc, 2, 1, "geopoly_overlap" },
204231 { geopolyDebugFunc, 1, 0, "geopoly_debug" },
204232 { geopolyBBoxFunc, 1, 1, "geopoly_bbox" },
204233 { geopolyXformFunc, 7, 1, "geopoly_xform" },
204234 { geopolyRegularFunc, 4, 1, "geopoly_regular" },
204235 { geopolyCcwFunc, 1, 1, "geopoly_ccw" },
204236 };
204237 static const struct {
204238 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
204239 void (*xFinal)(sqlite3_context*);
204240 const char *zName;
204241 } aAgg[] = {
204242 { geopolyBBoxStep, geopolyBBoxFinal, "geopoly_group_bbox" },
204243 };
204244 int i;
204245 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK0; i++){
204246 int enc;
204247 if( aFunc[i].bPure ){
204248 enc = SQLITE_UTF81|SQLITE_DETERMINISTIC0x000000800|SQLITE_INNOCUOUS0x000200000;
204249 }else{
204250 enc = SQLITE_UTF81|SQLITE_DIRECTONLY0x000080000;
204251 }
204252 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
204253 enc, 0,
204254 aFunc[i].xFunc, 0, 0);
204255 }
204256 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK0; i++){
204257 rc = sqlite3_create_function(db, aAgg[i].zName, 1,
204258 SQLITE_UTF81|SQLITE_DETERMINISTIC0x000000800|SQLITE_INNOCUOUS0x000200000, 0,
204259 0, aAgg[i].xStep, aAgg[i].xFinal);
204260 }
204261 if( rc==SQLITE_OK0 ){
204262 rc = sqlite3_create_module_v2(db, "geopoly", &geopolyModule, 0, 0);
204263 }
204264 return rc;
204265}
204266
204267/************** End of geopoly.c *********************************************/
204268/************** Continuing where we left off in rtree.c **********************/
204269#endif
204270
204271/*
204272** Register the r-tree module with database handle db. This creates the
204273** virtual table module "rtree" and the debugging/analysis scalar
204274** function "rtreenode".
204275*/
204276SQLITE_PRIVATEstatic int sqlite3RtreeInit(sqlite3 *db){
204277 const int utf8 = SQLITE_UTF81;
204278 int rc;
204279
204280 rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
204281 if( rc==SQLITE_OK0 ){
204282 rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
204283 }
204284 if( rc==SQLITE_OK0 ){
204285 rc = sqlite3_create_function(db, "rtreecheck", -1, utf8, 0,rtreecheck, 0,0);
204286 }
204287 if( rc==SQLITE_OK0 ){
204288#ifdef SQLITE_RTREE_INT_ONLY
204289 void *c = (void *)RTREE_COORD_INT321;
204290#else
204291 void *c = (void *)RTREE_COORD_REAL320;
204292#endif
204293 rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
204294 }
204295 if( rc==SQLITE_OK0 ){
204296 void *c = (void *)RTREE_COORD_INT321;
204297 rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
204298 }
204299#ifdef SQLITE_ENABLE_GEOPOLY
204300 if( rc==SQLITE_OK0 ){
204301 rc = sqlite3_geopoly_init(db);
204302 }
204303#endif
204304
204305 return rc;
204306}
204307
204308/*
204309** This routine deletes the RtreeGeomCallback object that was attached
204310** one of the SQL functions create by sqlite3_rtree_geometry_callback()
204311** or sqlite3_rtree_query_callback(). In other words, this routine is the
204312** destructor for an RtreeGeomCallback objecct. This routine is called when
204313** the corresponding SQL function is deleted.
204314*/
204315static void rtreeFreeCallback(void *p){
204316 RtreeGeomCallback *pInfo = (RtreeGeomCallback*)p;
204317 if( pInfo->xDestructor ) pInfo->xDestructor(pInfo->pContext);
204318 sqlite3_free(p);
204319}
204320
204321/*
204322** This routine frees the BLOB that is returned by geomCallback().
204323*/
204324static void rtreeMatchArgFree(void *pArg){
204325 int i;
204326 RtreeMatchArg *p = (RtreeMatchArg*)pArg;
204327 for(i=0; i<p->nParam; i++){
204328 sqlite3_value_free(p->apSqlParam[i]);
204329 }
204330 sqlite3_free(p);
204331}
204332
204333/*
204334** Each call to sqlite3_rtree_geometry_callback() or
204335** sqlite3_rtree_query_callback() creates an ordinary SQLite
204336** scalar function that is implemented by this routine.
204337**
204338** All this function does is construct an RtreeMatchArg object that
204339** contains the geometry-checking callback routines and a list of
204340** parameters to this function, then return that RtreeMatchArg object
204341** as a BLOB.
204342**
204343** The R-Tree MATCH operator will read the returned BLOB, deserialize
204344** the RtreeMatchArg object, and use the RtreeMatchArg object to figure
204345** out which elements of the R-Tree should be returned by the query.
204346*/
204347static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
204348 RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
204349 RtreeMatchArg *pBlob;
204350 sqlite3_int64 nBlob;
204351 int memErr = 0;
204352
204353 nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue)
204354 + nArg*sizeof(sqlite3_value*);
204355 pBlob = (RtreeMatchArg *)sqlite3_malloc64(nBlob);
204356 if( !pBlob ){
204357 sqlite3_result_error_nomem(ctx);
204358 }else{
204359 int i;
204360 pBlob->iSize = nBlob;
204361 pBlob->cb = pGeomCtx[0];
204362 pBlob->apSqlParam = (sqlite3_value**)&pBlob->aParam[nArg];
204363 pBlob->nParam = nArg;
204364 for(i=0; i<nArg; i++){
204365 pBlob->apSqlParam[i] = sqlite3_value_dup(aArg[i]);
204366 if( pBlob->apSqlParam[i]==0 ) memErr = 1;
204367#ifdef SQLITE_RTREE_INT_ONLY
204368 pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
204369#else
204370 pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
204371#endif
204372 }
204373 if( memErr ){
204374 sqlite3_result_error_nomem(ctx);
204375 rtreeMatchArgFree(pBlob);
204376 }else{
204377 sqlite3_result_pointer(ctx, pBlob, "RtreeMatchArg", rtreeMatchArgFree);
204378 }
204379 }
204380}
204381
204382/*
204383** Register a new geometry function for use with the r-tree MATCH operator.
204384*/
204385SQLITE_API int sqlite3_rtree_geometry_callback(
204386 sqlite3 *db, /* Register SQL function on this connection */
204387 const char *zGeom, /* Name of the new SQL function */
204388 int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*), /* Callback */
204389 void *pContext /* Extra data associated with the callback */
204390){
204391 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
204392
204393 /* Allocate and populate the context object. */
204394 pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
204395 if( !pGeomCtx ) return SQLITE_NOMEM7;
204396 pGeomCtx->xGeom = xGeom;
204397 pGeomCtx->xQueryFunc = 0;
204398 pGeomCtx->xDestructor = 0;
204399 pGeomCtx->pContext = pContext;
204400 return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY5,
204401 (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback
204402 );
204403}
204404
204405/*
204406** Register a new 2nd-generation geometry function for use with the
204407** r-tree MATCH operator.
204408*/
204409SQLITE_API int sqlite3_rtree_query_callback(
204410 sqlite3 *db, /* Register SQL function on this connection */
204411 const char *zQueryFunc, /* Name of new SQL function */
204412 int (*xQueryFunc)(sqlite3_rtree_query_info*), /* Callback */
204413 void *pContext, /* Extra data passed into the callback */
204414 void (*xDestructor)(void*) /* Destructor for the extra data */
204415){
204416 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
204417
204418 /* Allocate and populate the context object. */
204419 pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
204420 if( !pGeomCtx ){
204421 if( xDestructor ) xDestructor(pContext);
204422 return SQLITE_NOMEM7;
204423 }
204424 pGeomCtx->xGeom = 0;
204425 pGeomCtx->xQueryFunc = xQueryFunc;
204426 pGeomCtx->xDestructor = xDestructor;
204427 pGeomCtx->pContext = pContext;
204428 return sqlite3_create_function_v2(db, zQueryFunc, -1, SQLITE_ANY5,
204429 (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback
204430 );
204431}
204432
204433#if !SQLITE_CORE1
204434#ifdef _WIN32
204435__declspec(dllexport)
204436#endif
204437SQLITE_API int sqlite3_rtree_init(
204438 sqlite3 *db,
204439 char **pzErrMsg,
204440 const sqlite3_api_routines *pApi
204441){
204442 SQLITE_EXTENSION_INIT2(pApi)(void)pApi;
204443 return sqlite3RtreeInit(db);
204444}
204445#endif
204446
204447#endif
204448
204449/************** End of rtree.c ***********************************************/
204450/************** Begin file icu.c *********************************************/
204451/*
204452** 2007 May 6
204453**
204454** The author disclaims copyright to this source code. In place of
204455** a legal notice, here is a blessing:
204456**
204457** May you do good and not evil.
204458** May you find forgiveness for yourself and forgive others.
204459** May you share freely, never taking more than you give.
204460**
204461*************************************************************************
204462** $Id: icu.c,v 1.7 2007/12/13 21:54:11 drh Exp $
204463**
204464** This file implements an integration between the ICU library
204465** ("International Components for Unicode", an open-source library
204466** for handling unicode data) and SQLite. The integration uses
204467** ICU to provide the following to SQLite:
204468**
204469** * An implementation of the SQL regexp() function (and hence REGEXP
204470** operator) using the ICU uregex_XX() APIs.
204471**
204472** * Implementations of the SQL scalar upper() and lower() functions
204473** for case mapping.
204474**
204475** * Integration of ICU and SQLite collation sequences.
204476**
204477** * An implementation of the LIKE operator that uses ICU to
204478** provide case-independent matching.
204479*/
204480
204481#if !defined(SQLITE_CORE1) \
204482 || defined(SQLITE_ENABLE_ICU) \
204483 || defined(SQLITE_ENABLE_ICU_COLLATIONS)
204484
204485/* Include ICU headers */
204486#include <unicode/utypes.h>
204487#include <unicode/uregex.h>
204488#include <unicode/ustring.h>
204489#include <unicode/ucol.h>
204490
204491/* #include <assert.h> */
204492
204493#ifndef SQLITE_CORE1
204494/* #include "sqlite3ext.h" */
204495 SQLITE_EXTENSION_INIT1
204496#else
204497/* #include "sqlite3.h" */
204498#endif
204499
204500/*
204501** This function is called when an ICU function called from within
204502** the implementation of an SQL scalar function returns an error.
204503**
204504** The scalar function context passed as the first argument is
204505** loaded with an error message based on the following two args.
204506*/
204507static void icuFunctionError(
204508 sqlite3_context *pCtx, /* SQLite scalar function context */
204509 const char *zName, /* Name of ICU function that failed */
204510 UErrorCode e /* Error code returned by ICU function */
204511){
204512 char zBuf[128];
204513 sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
204514 zBuf[127] = '\0';
204515 sqlite3_result_error(pCtx, zBuf, -1);
204516}
204517
204518#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_ICU)
204519
204520/*
204521** Maximum length (in bytes) of the pattern in a LIKE or GLOB
204522** operator.
204523*/
204524#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH50000
204525# define SQLITE_MAX_LIKE_PATTERN_LENGTH50000 50000
204526#endif
204527
204528/*
204529** Version of sqlite3_free() that is always a function, never a macro.
204530*/
204531static void xFree(void *p){
204532 sqlite3_free(p);
204533}
204534
204535/*
204536** This lookup table is used to help decode the first byte of
204537** a multi-byte UTF8 character. It is copied here from SQLite source
204538** code file utf8.c.
204539*/
204540static const unsigned char icuUtf8Trans1[] = {
204541 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
204542 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
204543 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
204544 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
204545 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
204546 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
204547 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
204548 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
204549};
204550
204551#define SQLITE_ICU_READ_UTF8(zIn, c) \
204552 c = *(zIn++); \
204553 if( c>=0xc0 ){ \
204554 c = icuUtf8Trans1[c-0xc0]; \
204555 while( (*zIn & 0xc0)==0x80 ){ \
204556 c = (c<<6) + (0x3f & *(zIn++)); \
204557 } \
204558 }
204559
204560#define SQLITE_ICU_SKIP_UTF8(zIn) \
204561 assert( *zIn )((void) (0)); \
204562 if( *(zIn++)>=0xc0 ){ \
204563 while( (*zIn & 0xc0)==0x80 ){zIn++;} \
204564 }
204565
204566
204567/*
204568** Compare two UTF-8 strings for equality where the first string is
204569** a "LIKE" expression. Return true (1) if they are the same and
204570** false (0) if they are different.
204571*/
204572static int icuLikeCompare(
204573 const uint8_t *zPattern, /* LIKE pattern */
204574 const uint8_t *zString, /* The UTF-8 string to compare against */
204575 const UChar32 uEsc /* The escape character */
204576){
204577 static const uint32_t MATCH_ONE = (uint32_t)'_';
204578 static const uint32_t MATCH_ALL = (uint32_t)'%';
204579
204580 int prevEscape = 0; /* True if the previous character was uEsc */
204581
204582 while( 1 ){
204583
204584 /* Read (and consume) the next character from the input pattern. */
204585 uint32_t uPattern;
204586 SQLITE_ICU_READ_UTF8(zPattern, uPattern);
204587 if( uPattern==0 ) break;
204588
204589 /* There are now 4 possibilities:
204590 **
204591 ** 1. uPattern is an unescaped match-all character "%",
204592 ** 2. uPattern is an unescaped match-one character "_",
204593 ** 3. uPattern is an unescaped escape character, or
204594 ** 4. uPattern is to be handled as an ordinary character
204595 */
204596 if( uPattern==MATCH_ALL && !prevEscape && uPattern!=(uint32_t)uEsc ){
204597 /* Case 1. */
204598 uint8_t c;
204599
204600 /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
204601 ** MATCH_ALL. For each MATCH_ONE, skip one character in the
204602 ** test string.
204603 */
204604 while( (c=*zPattern) == MATCH_ALL || c == MATCH_ONE ){
204605 if( c==MATCH_ONE ){
204606 if( *zString==0 ) return 0;
204607 SQLITE_ICU_SKIP_UTF8(zString);
204608 }
204609 zPattern++;
204610 }
204611
204612 if( *zPattern==0 ) return 1;
204613
204614 while( *zString ){
204615 if( icuLikeCompare(zPattern, zString, uEsc) ){
204616 return 1;
204617 }
204618 SQLITE_ICU_SKIP_UTF8(zString);
204619 }
204620 return 0;
204621
204622 }else if( uPattern==MATCH_ONE && !prevEscape && uPattern!=(uint32_t)uEsc ){
204623 /* Case 2. */
204624 if( *zString==0 ) return 0;
204625 SQLITE_ICU_SKIP_UTF8(zString);
204626
204627 }else if( uPattern==(uint32_t)uEsc && !prevEscape ){
204628 /* Case 3. */
204629 prevEscape = 1;
204630
204631 }else{
204632 /* Case 4. */
204633 uint32_t uString;
204634 SQLITE_ICU_READ_UTF8(zString, uString);
204635 uString = (uint32_t)u_foldCase((UChar32)uString, U_FOLD_CASE_DEFAULT);
204636 uPattern = (uint32_t)u_foldCase((UChar32)uPattern, U_FOLD_CASE_DEFAULT);
204637 if( uString!=uPattern ){
204638 return 0;
204639 }
204640 prevEscape = 0;
204641 }
204642 }
204643
204644 return *zString==0;
204645}
204646
204647/*
204648** Implementation of the like() SQL function. This function implements
204649** the build-in LIKE operator. The first argument to the function is the
204650** pattern and the second argument is the string. So, the SQL statements:
204651**
204652** A LIKE B
204653**
204654** is implemented as like(B, A). If there is an escape character E,
204655**
204656** A LIKE B ESCAPE E
204657**
204658** is mapped to like(B, A, E).
204659*/
204660static void icuLikeFunc(
204661 sqlite3_context *context,
204662 int argc,
204663 sqlite3_value **argv
204664){
204665 const unsigned char *zA = sqlite3_value_text(argv[0]);
204666 const unsigned char *zB = sqlite3_value_text(argv[1]);
204667 UChar32 uEsc = 0;
204668
204669 /* Limit the length of the LIKE or GLOB pattern to avoid problems
204670 ** of deep recursion and N*N behavior in patternCompare().
204671 */
204672 if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH50000 ){
204673 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
204674 return;
204675 }
204676
204677
204678 if( argc==3 ){
204679 /* The escape character string must consist of a single UTF-8 character.
204680 ** Otherwise, return an error.
204681 */
204682 int nE= sqlite3_value_bytes(argv[2]);
204683 const unsigned char *zE = sqlite3_value_text(argv[2]);
204684 int i = 0;
204685 if( zE==0 ) return;
204686 U8_NEXT(zE, i, nE, uEsc);
204687 if( i!=nE){
204688 sqlite3_result_error(context,
204689 "ESCAPE expression must be a single character", -1);
204690 return;
204691 }
204692 }
204693
204694 if( zA && zB ){
204695 sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc));
204696 }
204697}
204698
204699/*
204700** Function to delete compiled regexp objects. Registered as
204701** a destructor function with sqlite3_set_auxdata().
204702*/
204703static void icuRegexpDelete(void *p){
204704 URegularExpression *pExpr = (URegularExpression *)p;
204705 uregex_close(pExpr);
204706}
204707
204708/*
204709** Implementation of SQLite REGEXP operator. This scalar function takes
204710** two arguments. The first is a regular expression pattern to compile
204711** the second is a string to match against that pattern. If either
204712** argument is an SQL NULL, then NULL Is returned. Otherwise, the result
204713** is 1 if the string matches the pattern, or 0 otherwise.
204714**
204715** SQLite maps the regexp() function to the regexp() operator such
204716** that the following two are equivalent:
204717**
204718** zString REGEXP zPattern
204719** regexp(zPattern, zString)
204720**
204721** Uses the following ICU regexp APIs:
204722**
204723** uregex_open()
204724** uregex_matches()
204725** uregex_close()
204726*/
204727static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
204728 UErrorCode status = U_ZERO_ERROR;
204729 URegularExpression *pExpr;
204730 UBool res;
204731 const UChar *zString = sqlite3_value_text16(apArg[1]);
204732
204733 (void)nArg; /* Unused parameter */
204734
204735 /* If the left hand side of the regexp operator is NULL,
204736 ** then the result is also NULL.
204737 */
204738 if( !zString ){
204739 return;
204740 }
204741
204742 pExpr = sqlite3_get_auxdata(p, 0);
204743 if( !pExpr ){
204744 const UChar *zPattern = sqlite3_value_text16(apArg[0]);
204745 if( !zPattern ){
204746 return;
204747 }
204748 pExpr = uregex_open(zPattern, -1, 0, 0, &status);
204749
204750 if( U_SUCCESS(status) ){
204751 sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
204752 }else{
204753 assert(!pExpr)((void) (0));
204754 icuFunctionError(p, "uregex_open", status);
204755 return;
204756 }
204757 }
204758
204759 /* Configure the text that the regular expression operates on. */
204760 uregex_setText(pExpr, zString, -1, &status);
204761 if( !U_SUCCESS(status) ){
204762 icuFunctionError(p, "uregex_setText", status);
204763 return;
204764 }
204765
204766 /* Attempt the match */
204767 res = uregex_matches(pExpr, 0, &status);
204768 if( !U_SUCCESS(status) ){
204769 icuFunctionError(p, "uregex_matches", status);
204770 return;
204771 }
204772
204773 /* Set the text that the regular expression operates on to a NULL
204774 ** pointer. This is not really necessary, but it is tidier than
204775 ** leaving the regular expression object configured with an invalid
204776 ** pointer after this function returns.
204777 */
204778 uregex_setText(pExpr, 0, 0, &status);
204779
204780 /* Return 1 or 0. */
204781 sqlite3_result_int(p, res ? 1 : 0);
204782}
204783
204784/*
204785** Implementations of scalar functions for case mapping - upper() and
204786** lower(). Function upper() converts its input to upper-case (ABC).
204787** Function lower() converts to lower-case (abc).
204788**
204789** ICU provides two types of case mapping, "general" case mapping and
204790** "language specific". Refer to ICU documentation for the differences
204791** between the two.
204792**
204793** To utilise "general" case mapping, the upper() or lower() scalar
204794** functions are invoked with one argument:
204795**
204796** upper('ABC') -> 'abc'
204797** lower('abc') -> 'ABC'
204798**
204799** To access ICU "language specific" case mapping, upper() or lower()
204800** should be invoked with two arguments. The second argument is the name
204801** of the locale to use. Passing an empty string ("") or SQL NULL value
204802** as the second argument is the same as invoking the 1 argument version
204803** of upper() or lower().
204804**
204805** lower('I', 'en_us') -> 'i'
204806** lower('I', 'tr_tr') -> '\u131' (small dotless i)
204807**
204808** http://www.icu-project.org/userguide/posix.html#case_mappings
204809*/
204810static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
204811 const UChar *zInput; /* Pointer to input string */
204812 UChar *zOutput = 0; /* Pointer to output buffer */
204813 int nInput; /* Size of utf-16 input string in bytes */
204814 int nOut; /* Size of output buffer in bytes */
204815 int cnt;
204816 int bToUpper; /* True for toupper(), false for tolower() */
204817 UErrorCode status;
204818 const char *zLocale = 0;
204819
204820 assert(nArg==1 || nArg==2)((void) (0));
204821 bToUpper = (sqlite3_user_data(p)!=0);
204822 if( nArg==2 ){
204823 zLocale = (const char *)sqlite3_value_text(apArg[1]);
204824 }
204825
204826 zInput = sqlite3_value_text16(apArg[0]);
204827 if( !zInput ){
204828 return;
204829 }
204830 nOut = nInput = sqlite3_value_bytes16(apArg[0]);
204831 if( nOut==0 ){
204832 sqlite3_result_text16(p, "", 0, SQLITE_STATIC((sqlite3_destructor_type)0));
204833 return;
204834 }
204835
204836 for(cnt=0; cnt<2; cnt++){
204837 UChar *zNew = sqlite3_realloc(zOutput, nOut);
204838 if( zNew==0 ){
204839 sqlite3_free(zOutput);
204840 sqlite3_result_error_nomem(p);
204841 return;
204842 }
204843 zOutput = zNew;
204844 status = U_ZERO_ERROR;
204845 if( bToUpper ){
204846 nOut = 2*u_strToUpper(zOutput,nOut/2,zInput,nInput/2,zLocale,&status);
204847 }else{
204848 nOut = 2*u_strToLower(zOutput,nOut/2,zInput,nInput/2,zLocale,&status);
204849 }
204850
204851 if( U_SUCCESS(status) ){
204852 sqlite3_result_text16(p, zOutput, nOut, xFree);
204853 }else if( status==U_BUFFER_OVERFLOW_ERROR ){
204854 assert( cnt==0 )((void) (0));
204855 continue;
204856 }else{
204857 icuFunctionError(p, bToUpper ? "u_strToUpper" : "u_strToLower", status);
204858 }
204859 return;
204860 }
204861 assert( 0 )((void) (0)); /* Unreachable */
204862}
204863
204864#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) */
204865
204866/*
204867** Collation sequence destructor function. The pCtx argument points to
204868** a UCollator structure previously allocated using ucol_open().
204869*/
204870static void icuCollationDel(void *pCtx){
204871 UCollator *p = (UCollator *)pCtx;
204872 ucol_close(p);
204873}
204874
204875/*
204876** Collation sequence comparison function. The pCtx argument points to
204877** a UCollator structure previously allocated using ucol_open().
204878*/
204879static int icuCollationColl(
204880 void *pCtx,
204881 int nLeft,
204882 const void *zLeft,
204883 int nRight,
204884 const void *zRight
204885){
204886 UCollationResult res;
204887 UCollator *p = (UCollator *)pCtx;
204888 res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2);
204889 switch( res ){
204890 case UCOL_LESS: return -1;
204891 case UCOL_GREATER: return +1;
204892 case UCOL_EQUAL: return 0;
204893 }
204894 assert(!"Unexpected return value from ucol_strcoll()")((void) (0));
204895 return 0;
204896}
204897
204898/*
204899** Implementation of the scalar function icu_load_collation().
204900**
204901** This scalar function is used to add ICU collation based collation
204902** types to an SQLite database connection. It is intended to be called
204903** as follows:
204904**
204905** SELECT icu_load_collation(<locale>, <collation-name>);
204906**
204907** Where <locale> is a string containing an ICU locale identifier (i.e.
204908** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
204909** collation sequence to create.
204910*/
204911static void icuLoadCollation(
204912 sqlite3_context *p,
204913 int nArg,
204914 sqlite3_value **apArg
204915){
204916 sqlite3 *db = (sqlite3 *)sqlite3_user_data(p);
204917 UErrorCode status = U_ZERO_ERROR;
204918 const char *zLocale; /* Locale identifier - (eg. "jp_JP") */
204919 const char *zName; /* SQL Collation sequence name (eg. "japanese") */
204920 UCollator *pUCollator; /* ICU library collation object */
204921 int rc; /* Return code from sqlite3_create_collation_x() */
204922
204923 assert(nArg==2)((void) (0));
204924 (void)nArg; /* Unused parameter */
204925 zLocale = (const char *)sqlite3_value_text(apArg[0]);
204926 zName = (const char *)sqlite3_value_text(apArg[1]);
204927
204928 if( !zLocale || !zName ){
204929 return;
204930 }
204931
204932 pUCollator = ucol_open(zLocale, &status);
204933 if( !U_SUCCESS(status) ){
204934 icuFunctionError(p, "ucol_open", status);
204935 return;
204936 }
204937 assert(p)((void) (0));
204938
204939 rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF164, (void *)pUCollator,
204940 icuCollationColl, icuCollationDel
204941 );
204942 if( rc!=SQLITE_OK0 ){
204943 ucol_close(pUCollator);
204944 sqlite3_result_error(p, "Error registering collation function", -1);
204945 }
204946}
204947
204948/*
204949** Register the ICU extension functions with database db.
204950*/
204951SQLITE_PRIVATEstatic int sqlite3IcuInit(sqlite3 *db){
204952# define SQLITEICU_EXTRAFLAGS (SQLITE_DETERMINISTIC0x000000800|SQLITE_INNOCUOUS0x000200000)
204953 static const struct IcuScalar {
204954 const char *zName; /* Function name */
204955 unsigned char nArg; /* Number of arguments */
204956 unsigned int enc; /* Optimal text encoding */
204957 unsigned char iContext; /* sqlite3_user_data() context */
204958 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
204959 } scalars[] = {
204960 {"icu_load_collation",2,SQLITE_UTF81|SQLITE_DIRECTONLY0x000080000,1, icuLoadCollation},
204961#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_ICU)
204962 {"regexp", 2, SQLITE_ANY5|SQLITEICU_EXTRAFLAGS, 0, icuRegexpFunc},
204963 {"lower", 1, SQLITE_UTF164|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
204964 {"lower", 2, SQLITE_UTF164|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
204965 {"upper", 1, SQLITE_UTF164|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
204966 {"upper", 2, SQLITE_UTF164|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
204967 {"lower", 1, SQLITE_UTF81|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
204968 {"lower", 2, SQLITE_UTF81|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
204969 {"upper", 1, SQLITE_UTF81|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
204970 {"upper", 2, SQLITE_UTF81|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
204971 {"like", 2, SQLITE_UTF81|SQLITEICU_EXTRAFLAGS, 0, icuLikeFunc},
204972 {"like", 3, SQLITE_UTF81|SQLITEICU_EXTRAFLAGS, 0, icuLikeFunc},
204973#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) */
204974 };
204975 int rc = SQLITE_OK0;
204976 int i;
204977
204978 for(i=0; rc==SQLITE_OK0 && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
204979 const struct IcuScalar *p = &scalars[i];
204980 rc = sqlite3_create_function(
204981 db, p->zName, p->nArg, p->enc,
204982 p->iContext ? (void*)db : (void*)0,
204983 p->xFunc, 0, 0
204984 );
204985 }
204986
204987 return rc;
204988}
204989
204990#if !SQLITE_CORE1
204991#ifdef _WIN32
204992__declspec(dllexport)
204993#endif
204994SQLITE_API int sqlite3_icu_init(
204995 sqlite3 *db,
204996 char **pzErrMsg,
204997 const sqlite3_api_routines *pApi
204998){
204999 SQLITE_EXTENSION_INIT2(pApi)(void)pApi;
205000 return sqlite3IcuInit(db);
205001}
205002#endif
205003
205004#endif
205005
205006/************** End of icu.c *************************************************/
205007/************** Begin file fts3_icu.c ****************************************/
205008/*
205009** 2007 June 22
205010**
205011** The author disclaims copyright to this source code. In place of
205012** a legal notice, here is a blessing:
205013**
205014** May you do good and not evil.
205015** May you find forgiveness for yourself and forgive others.
205016** May you share freely, never taking more than you give.
205017**
205018*************************************************************************
205019** This file implements a tokenizer for fts3 based on the ICU library.
205020*/
205021/* #include "fts3Int.h" */
205022#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
205023#ifdef SQLITE_ENABLE_ICU
205024
205025/* #include <assert.h> */
205026/* #include <string.h> */
205027/* #include "fts3_tokenizer.h" */
205028
205029#include <unicode/ubrk.h>
205030/* #include <unicode/ucol.h> */
205031/* #include <unicode/ustring.h> */
205032#include <unicode/utf16.h>
205033
205034typedef struct IcuTokenizer IcuTokenizer;
205035typedef struct IcuCursor IcuCursor;
205036
205037struct IcuTokenizer {
205038 sqlite3_tokenizer base;
205039 char *zLocale;
205040};
205041
205042struct IcuCursor {
205043 sqlite3_tokenizer_cursor base;
205044
205045 UBreakIterator *pIter; /* ICU break-iterator object */
205046 int nChar; /* Number of UChar elements in pInput */
205047 UChar *aChar; /* Copy of input using utf-16 encoding */
205048 int *aOffset; /* Offsets of each character in utf-8 input */
205049
205050 int nBuffer;
205051 char *zBuffer;
205052
205053 int iToken;
205054};
205055
205056/*
205057** Create a new tokenizer instance.
205058*/
205059static int icuCreate(
205060 int argc, /* Number of entries in argv[] */
205061 const char * const *argv, /* Tokenizer creation arguments */
205062 sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */
205063){
205064 IcuTokenizer *p;
205065 int n = 0;
205066
205067 if( argc>0 ){
205068 n = strlen(argv[0])+1;
205069 }
205070 p = (IcuTokenizer *)sqlite3_malloc64(sizeof(IcuTokenizer)+n);
205071 if( !p ){
205072 return SQLITE_NOMEM7;
205073 }
205074 memset(p, 0, sizeof(IcuTokenizer));
205075
205076 if( n ){
205077 p->zLocale = (char *)&p[1];
205078 memcpy(p->zLocale, argv[0], n);
205079 }
205080
205081 *ppTokenizer = (sqlite3_tokenizer *)p;
205082
205083 return SQLITE_OK0;
205084}
205085
205086/*
205087** Destroy a tokenizer
205088*/
205089static int icuDestroy(sqlite3_tokenizer *pTokenizer){
205090 IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
205091 sqlite3_free(p);
205092 return SQLITE_OK0;
205093}
205094
205095/*
205096** Prepare to begin tokenizing a particular string. The input
205097** string to be tokenized is pInput[0..nBytes-1]. A cursor
205098** used to incrementally tokenize this string is returned in
205099** *ppCursor.
205100*/
205101static int icuOpen(
205102 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
205103 const char *zInput, /* Input string */
205104 int nInput, /* Length of zInput in bytes */
205105 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
205106){
205107 IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
205108 IcuCursor *pCsr;
205109
205110 const int32_t opt = U_FOLD_CASE_DEFAULT;
205111 UErrorCode status = U_ZERO_ERROR;
205112 int nChar;
205113
205114 UChar32 c;
205115 int iInput = 0;
205116 int iOut = 0;
205117
205118 *ppCursor = 0;
205119
205120 if( zInput==0 ){
205121 nInput = 0;
205122 zInput = "";
205123 }else if( nInput<0 ){
205124 nInput = strlen(zInput);
205125 }
205126 nChar = nInput+1;
205127 pCsr = (IcuCursor *)sqlite3_malloc64(
205128 sizeof(IcuCursor) + /* IcuCursor */
205129 ((nChar+3)&~3) * sizeof(UChar) + /* IcuCursor.aChar[] */
205130 (nChar+1) * sizeof(int) /* IcuCursor.aOffset[] */
205131 );
205132 if( !pCsr ){
205133 return SQLITE_NOMEM7;
205134 }
205135 memset(pCsr, 0, sizeof(IcuCursor));
205136 pCsr->aChar = (UChar *)&pCsr[1];
205137 pCsr->aOffset = (int *)&pCsr->aChar[(nChar+3)&~3];
205138
205139 pCsr->aOffset[iOut] = iInput;
205140 U8_NEXT(zInput, iInput, nInput, c);
205141 while( c>0 ){
205142 int isError = 0;
205143 c = u_foldCase(c, opt);
205144 U16_APPEND(pCsr->aChar, iOut, nChar, c, isError);
205145 if( isError ){
205146 sqlite3_free(pCsr);
205147 return SQLITE_ERROR1;
205148 }
205149 pCsr->aOffset[iOut] = iInput;
205150
205151 if( iInput<nInput ){
205152 U8_NEXT(zInput, iInput, nInput, c);
205153 }else{
205154 c = 0;
205155 }
205156 }
205157
205158 pCsr->pIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status);
205159 if( !U_SUCCESS(status) ){
205160 sqlite3_free(pCsr);
205161 return SQLITE_ERROR1;
205162 }
205163 pCsr->nChar = iOut;
205164
205165 ubrk_first(pCsr->pIter);
205166 *ppCursor = (sqlite3_tokenizer_cursor *)pCsr;
205167 return SQLITE_OK0;
205168}
205169
205170/*
205171** Close a tokenization cursor previously opened by a call to icuOpen().
205172*/
205173static int icuClose(sqlite3_tokenizer_cursor *pCursor){
205174 IcuCursor *pCsr = (IcuCursor *)pCursor;
205175 ubrk_close(pCsr->pIter);
205176 sqlite3_free(pCsr->zBuffer);
205177 sqlite3_free(pCsr);
205178 return SQLITE_OK0;
205179}
205180
205181/*
205182** Extract the next token from a tokenization cursor.
205183*/
205184static int icuNext(
205185 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */
205186 const char **ppToken, /* OUT: *ppToken is the token text */
205187 int *pnBytes, /* OUT: Number of bytes in token */
205188 int *piStartOffset, /* OUT: Starting offset of token */
205189 int *piEndOffset, /* OUT: Ending offset of token */
205190 int *piPosition /* OUT: Position integer of token */
205191){
205192 IcuCursor *pCsr = (IcuCursor *)pCursor;
205193
205194 int iStart = 0;
205195 int iEnd = 0;
205196 int nByte = 0;
205197
205198 while( iStart==iEnd ){
205199 UChar32 c;
205200
205201 iStart = ubrk_current(pCsr->pIter);
205202 iEnd = ubrk_next(pCsr->pIter);
205203 if( iEnd==UBRK_DONE ){
205204 return SQLITE_DONE101;
205205 }
205206
205207 while( iStart<iEnd ){
205208 int iWhite = iStart;
205209 U16_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
205210 if( u_isspace(c) ){
205211 iStart = iWhite;
205212 }else{
205213 break;
205214 }
205215 }
205216 assert(iStart<=iEnd)((void) (0));
205217 }
205218
205219 do {
205220 UErrorCode status = U_ZERO_ERROR;
205221 if( nByte ){
205222 char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte);
205223 if( !zNew ){
205224 return SQLITE_NOMEM7;
205225 }
205226 pCsr->zBuffer = zNew;
205227 pCsr->nBuffer = nByte;
205228 }
205229
205230 u_strToUTF8(
205231 pCsr->zBuffer, pCsr->nBuffer, &nByte, /* Output vars */
205232 &pCsr->aChar[iStart], iEnd-iStart, /* Input vars */
205233 &status /* Output success/failure */
205234 );
205235 } while( nByte>pCsr->nBuffer );
205236
205237 *ppToken = pCsr->zBuffer;
205238 *pnBytes = nByte;
205239 *piStartOffset = pCsr->aOffset[iStart];
205240 *piEndOffset = pCsr->aOffset[iEnd];
205241 *piPosition = pCsr->iToken++;
205242
205243 return SQLITE_OK0;
205244}
205245
205246/*
205247** The set of routines that implement the simple tokenizer
205248*/
205249static const sqlite3_tokenizer_module icuTokenizerModule = {
205250 0, /* iVersion */
205251 icuCreate, /* xCreate */
205252 icuDestroy, /* xCreate */
205253 icuOpen, /* xOpen */
205254 icuClose, /* xClose */
205255 icuNext, /* xNext */
205256 0, /* xLanguageid */
205257};
205258
205259/*
205260** Set *ppModule to point at the implementation of the ICU tokenizer.
205261*/
205262SQLITE_PRIVATEstatic void sqlite3Fts3IcuTokenizerModule(
205263 sqlite3_tokenizer_module const**ppModule
205264){
205265 *ppModule = &icuTokenizerModule;
205266}
205267
205268#endif /* defined(SQLITE_ENABLE_ICU) */
205269#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
205270
205271/************** End of fts3_icu.c ********************************************/
205272/************** Begin file sqlite3rbu.c **************************************/
205273/*
205274** 2014 August 30
205275**
205276** The author disclaims copyright to this source code. In place of
205277** a legal notice, here is a blessing:
205278**
205279** May you do good and not evil.
205280** May you find forgiveness for yourself and forgive others.
205281** May you share freely, never taking more than you give.
205282**
205283*************************************************************************
205284**
205285**
205286** OVERVIEW
205287**
205288** The RBU extension requires that the RBU update be packaged as an
205289** SQLite database. The tables it expects to find are described in
205290** sqlite3rbu.h. Essentially, for each table xyz in the target database
205291** that the user wishes to write to, a corresponding data_xyz table is
205292** created in the RBU database and populated with one row for each row to
205293** update, insert or delete from the target table.
205294**
205295** The update proceeds in three stages:
205296**
205297** 1) The database is updated. The modified database pages are written
205298** to a *-oal file. A *-oal file is just like a *-wal file, except
205299** that it is named "<database>-oal" instead of "<database>-wal".
205300** Because regular SQLite clients do not look for file named
205301** "<database>-oal", they go on using the original database in
205302** rollback mode while the *-oal file is being generated.
205303**
205304** During this stage RBU does not update the database by writing
205305** directly to the target tables. Instead it creates "imposter"
205306** tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses
205307** to update each b-tree individually. All updates required by each
205308** b-tree are completed before moving on to the next, and all
205309** updates are done in sorted key order.
205310**
205311** 2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
205312** location using a call to rename(2). Before doing this the RBU
205313** module takes an EXCLUSIVE lock on the database file, ensuring
205314** that there are no other active readers.
205315**
205316** Once the EXCLUSIVE lock is released, any other database readers
205317** detect the new *-wal file and read the database in wal mode. At
205318** this point they see the new version of the database - including
205319** the updates made as part of the RBU update.
205320**
205321** 3) The new *-wal file is checkpointed. This proceeds in the same way
205322** as a regular database checkpoint, except that a single frame is
205323** checkpointed each time sqlite3rbu_step() is called. If the RBU
205324** handle is closed before the entire *-wal file is checkpointed,
205325** the checkpoint progress is saved in the RBU database and the
205326** checkpoint can be resumed by another RBU client at some point in
205327** the future.
205328**
205329** POTENTIAL PROBLEMS
205330**
205331** The rename() call might not be portable. And RBU is not currently
205332** syncing the directory after renaming the file.
205333**
205334** When state is saved, any commit to the *-oal file and the commit to
205335** the RBU update database are not atomic. So if the power fails at the
205336** wrong moment they might get out of sync. As the main database will be
205337** committed before the RBU update database this will likely either just
205338** pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE
205339** constraint violations).
205340**
205341** If some client does modify the target database mid RBU update, or some
205342** other error occurs, the RBU extension will keep throwing errors. It's
205343** not really clear how to get out of this state. The system could just
205344** by delete the RBU update database and *-oal file and have the device
205345** download the update again and start over.
205346**
205347** At present, for an UPDATE, both the new.* and old.* records are
205348** collected in the rbu_xyz table. And for both UPDATEs and DELETEs all
205349** fields are collected. This means we're probably writing a lot more
205350** data to disk when saving the state of an ongoing update to the RBU
205351** update database than is strictly necessary.
205352**
205353*/
205354
205355/* #include <assert.h> */
205356/* #include <string.h> */
205357/* #include <stdio.h> */
205358
205359/* #include "sqlite3.h" */
205360
205361#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_RBU)
205362/************** Include sqlite3rbu.h in the middle of sqlite3rbu.c ***********/
205363/************** Begin file sqlite3rbu.h **************************************/
205364/*
205365** 2014 August 30
205366**
205367** The author disclaims copyright to this source code. In place of
205368** a legal notice, here is a blessing:
205369**
205370** May you do good and not evil.
205371** May you find forgiveness for yourself and forgive others.
205372** May you share freely, never taking more than you give.
205373**
205374*************************************************************************
205375**
205376** This file contains the public interface for the RBU extension.
205377*/
205378
205379/*
205380** SUMMARY
205381**
205382** Writing a transaction containing a large number of operations on
205383** b-tree indexes that are collectively larger than the available cache
205384** memory can be very inefficient.
205385**
205386** The problem is that in order to update a b-tree, the leaf page (at least)
205387** containing the entry being inserted or deleted must be modified. If the
205388** working set of leaves is larger than the available cache memory, then a
205389** single leaf that is modified more than once as part of the transaction
205390** may be loaded from or written to the persistent media multiple times.
205391** Additionally, because the index updates are likely to be applied in
205392** random order, access to pages within the database is also likely to be in
205393** random order, which is itself quite inefficient.
205394**
205395** One way to improve the situation is to sort the operations on each index
205396** by index key before applying them to the b-tree. This leads to an IO
205397** pattern that resembles a single linear scan through the index b-tree,
205398** and all but guarantees each modified leaf page is loaded and stored
205399** exactly once. SQLite uses this trick to improve the performance of
205400** CREATE INDEX commands. This extension allows it to be used to improve
205401** the performance of large transactions on existing databases.
205402**
205403** Additionally, this extension allows the work involved in writing the
205404** large transaction to be broken down into sub-transactions performed
205405** sequentially by separate processes. This is useful if the system cannot
205406** guarantee that a single update process will run for long enough to apply
205407** the entire update, for example because the update is being applied on a
205408** mobile device that is frequently rebooted. Even after the writer process
205409** has committed one or more sub-transactions, other database clients continue
205410** to read from the original database snapshot. In other words, partially
205411** applied transactions are not visible to other clients.
205412**
205413** "RBU" stands for "Resumable Bulk Update". As in a large database update
205414** transmitted via a wireless network to a mobile device. A transaction
205415** applied using this extension is hence refered to as an "RBU update".
205416**
205417**
205418** LIMITATIONS
205419**
205420** An "RBU update" transaction is subject to the following limitations:
205421**
205422** * The transaction must consist of INSERT, UPDATE and DELETE operations
205423** only.
205424**
205425** * INSERT statements may not use any default values.
205426**
205427** * UPDATE and DELETE statements must identify their target rows by
205428** non-NULL PRIMARY KEY values. Rows with NULL values stored in PRIMARY
205429** KEY fields may not be updated or deleted. If the table being written
205430** has no PRIMARY KEY, affected rows must be identified by rowid.
205431**
205432** * UPDATE statements may not modify PRIMARY KEY columns.
205433**
205434** * No triggers will be fired.
205435**
205436** * No foreign key violations are detected or reported.
205437**
205438** * CHECK constraints are not enforced.
205439**
205440** * No constraint handling mode except for "OR ROLLBACK" is supported.
205441**
205442**
205443** PREPARATION
205444**
205445** An "RBU update" is stored as a separate SQLite database. A database
205446** containing an RBU update is an "RBU database". For each table in the
205447** target database to be updated, the RBU database should contain a table
205448** named "data_<target name>" containing the same set of columns as the
205449** target table, and one more - "rbu_control". The data_% table should
205450** have no PRIMARY KEY or UNIQUE constraints, but each column should have
205451** the same type as the corresponding column in the target database.
205452** The "rbu_control" column should have no type at all. For example, if
205453** the target database contains:
205454**
205455** CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c UNIQUE);
205456**
205457** Then the RBU database should contain:
205458**
205459** CREATE TABLE data_t1(a INTEGER, b TEXT, c, rbu_control);
205460**
205461** The order of the columns in the data_% table does not matter.
205462**
205463** Instead of a regular table, the RBU database may also contain virtual
205464** tables or view named using the data_<target> naming scheme.
205465**
205466** Instead of the plain data_<target> naming scheme, RBU database tables
205467** may also be named data<integer>_<target>, where <integer> is any sequence
205468** of zero or more numeric characters (0-9). This can be significant because
205469** tables within the RBU database are always processed in order sorted by
205470** name. By judicious selection of the <integer> portion of the names
205471** of the RBU tables the user can therefore control the order in which they
205472** are processed. This can be useful, for example, to ensure that "external
205473** content" FTS4 tables are updated before their underlying content tables.
205474**
205475** If the target database table is a virtual table or a table that has no
205476** PRIMARY KEY declaration, the data_% table must also contain a column
205477** named "rbu_rowid". This column is mapped to the tables implicit primary
205478** key column - "rowid". Virtual tables for which the "rowid" column does
205479** not function like a primary key value cannot be updated using RBU. For
205480** example, if the target db contains either of the following:
205481**
205482** CREATE VIRTUAL TABLE x1 USING fts3(a, b);
205483** CREATE TABLE x1(a, b)
205484**
205485** then the RBU database should contain:
205486**
205487** CREATE TABLE data_x1(a, b, rbu_rowid, rbu_control);
205488**
205489** All non-hidden columns (i.e. all columns matched by "SELECT *") of the
205490** target table must be present in the input table. For virtual tables,
205491** hidden columns are optional - they are updated by RBU if present in
205492** the input table, or not otherwise. For example, to write to an fts4
205493** table with a hidden languageid column such as:
205494**
205495** CREATE VIRTUAL TABLE ft1 USING fts4(a, b, languageid='langid');
205496**
205497** Either of the following input table schemas may be used:
205498**
205499** CREATE TABLE data_ft1(a, b, langid, rbu_rowid, rbu_control);
205500** CREATE TABLE data_ft1(a, b, rbu_rowid, rbu_control);
205501**
205502** For each row to INSERT into the target database as part of the RBU
205503** update, the corresponding data_% table should contain a single record
205504** with the "rbu_control" column set to contain integer value 0. The
205505** other columns should be set to the values that make up the new record
205506** to insert.
205507**
205508** If the target database table has an INTEGER PRIMARY KEY, it is not
205509** possible to insert a NULL value into the IPK column. Attempting to
205510** do so results in an SQLITE_MISMATCH error.
205511**
205512** For each row to DELETE from the target database as part of the RBU
205513** update, the corresponding data_% table should contain a single record
205514** with the "rbu_control" column set to contain integer value 1. The
205515** real primary key values of the row to delete should be stored in the
205516** corresponding columns of the data_% table. The values stored in the
205517** other columns are not used.
205518**
205519** For each row to UPDATE from the target database as part of the RBU
205520** update, the corresponding data_% table should contain a single record
205521** with the "rbu_control" column set to contain a value of type text.
205522** The real primary key values identifying the row to update should be
205523** stored in the corresponding columns of the data_% table row, as should
205524** the new values of all columns being update. The text value in the
205525** "rbu_control" column must contain the same number of characters as
205526** there are columns in the target database table, and must consist entirely
205527** of 'x' and '.' characters (or in some special cases 'd' - see below). For
205528** each column that is being updated, the corresponding character is set to
205529** 'x'. For those that remain as they are, the corresponding character of the
205530** rbu_control value should be set to '.'. For example, given the tables
205531** above, the update statement:
205532**
205533** UPDATE t1 SET c = 'usa' WHERE a = 4;
205534**
205535** is represented by the data_t1 row created by:
205536**
205537** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..x');
205538**
205539** Instead of an 'x' character, characters of the rbu_control value specified
205540** for UPDATEs may also be set to 'd'. In this case, instead of updating the
205541** target table with the value stored in the corresponding data_% column, the
205542** user-defined SQL function "rbu_delta()" is invoked and the result stored in
205543** the target table column. rbu_delta() is invoked with two arguments - the
205544** original value currently stored in the target table column and the
205545** value specified in the data_xxx table.
205546**
205547** For example, this row:
205548**
205549** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..d');
205550**
205551** is similar to an UPDATE statement such as:
205552**
205553** UPDATE t1 SET c = rbu_delta(c, 'usa') WHERE a = 4;
205554**
205555** Finally, if an 'f' character appears in place of a 'd' or 's' in an
205556** ota_control string, the contents of the data_xxx table column is assumed
205557** to be a "fossil delta" - a patch to be applied to a blob value in the
205558** format used by the fossil source-code management system. In this case
205559** the existing value within the target database table must be of type BLOB.
205560** It is replaced by the result of applying the specified fossil delta to
205561** itself.
205562**
205563** If the target database table is a virtual table or a table with no PRIMARY
205564** KEY, the rbu_control value should not include a character corresponding
205565** to the rbu_rowid value. For example, this:
205566**
205567** INSERT INTO data_ft1(a, b, rbu_rowid, rbu_control)
205568** VALUES(NULL, 'usa', 12, '.x');
205569**
205570** causes a result similar to:
205571**
205572** UPDATE ft1 SET b = 'usa' WHERE rowid = 12;
205573**
205574** The data_xxx tables themselves should have no PRIMARY KEY declarations.
205575** However, RBU is more efficient if reading the rows in from each data_xxx
205576** table in "rowid" order is roughly the same as reading them sorted by
205577** the PRIMARY KEY of the corresponding target database table. In other
205578** words, rows should be sorted using the destination table PRIMARY KEY
205579** fields before they are inserted into the data_xxx tables.
205580**
205581** USAGE
205582**
205583** The API declared below allows an application to apply an RBU update
205584** stored on disk to an existing target database. Essentially, the
205585** application:
205586**
205587** 1) Opens an RBU handle using the sqlite3rbu_open() function.
205588**
205589** 2) Registers any required virtual table modules with the database
205590** handle returned by sqlite3rbu_db(). Also, if required, register
205591** the rbu_delta() implementation.
205592**
205593** 3) Calls the sqlite3rbu_step() function one or more times on
205594** the new handle. Each call to sqlite3rbu_step() performs a single
205595** b-tree operation, so thousands of calls may be required to apply
205596** a complete update.
205597**
205598** 4) Calls sqlite3rbu_close() to close the RBU update handle. If
205599** sqlite3rbu_step() has been called enough times to completely
205600** apply the update to the target database, then the RBU database
205601** is marked as fully applied. Otherwise, the state of the RBU
205602** update application is saved in the RBU database for later
205603** resumption.
205604**
205605** See comments below for more detail on APIs.
205606**
205607** If an update is only partially applied to the target database by the
205608** time sqlite3rbu_close() is called, various state information is saved
205609** within the RBU database. This allows subsequent processes to automatically
205610** resume the RBU update from where it left off.
205611**
205612** To remove all RBU extension state information, returning an RBU database
205613** to its original contents, it is sufficient to drop all tables that begin
205614** with the prefix "rbu_"
205615**
205616** DATABASE LOCKING
205617**
205618** An RBU update may not be applied to a database in WAL mode. Attempting
205619** to do so is an error (SQLITE_ERROR).
205620**
205621** While an RBU handle is open, a SHARED lock may be held on the target
205622** database file. This means it is possible for other clients to read the
205623** database, but not to write it.
205624**
205625** If an RBU update is started and then suspended before it is completed,
205626** then an external client writes to the database, then attempting to resume
205627** the suspended RBU update is also an error (SQLITE_BUSY).
205628*/
205629
205630#ifndef _SQLITE3RBU_H
205631#define _SQLITE3RBU_H
205632
205633/* #include "sqlite3.h" ** Required for error code definitions ** */
205634
205635#if 0
205636extern "C" {
205637#endif
205638
205639typedef struct sqlite3rbu sqlite3rbu;
205640
205641/*
205642** Open an RBU handle.
205643**
205644** Argument zTarget is the path to the target database. Argument zRbu is
205645** the path to the RBU database. Each call to this function must be matched
205646** by a call to sqlite3rbu_close(). When opening the databases, RBU passes
205647** the SQLITE_CONFIG_URI flag to sqlite3_open_v2(). So if either zTarget
205648** or zRbu begin with "file:", it will be interpreted as an SQLite
205649** database URI, not a regular file name.
205650**
205651** If the zState argument is passed a NULL value, the RBU extension stores
205652** the current state of the update (how many rows have been updated, which
205653** indexes are yet to be updated etc.) within the RBU database itself. This
205654** can be convenient, as it means that the RBU application does not need to
205655** organize removing a separate state file after the update is concluded.
205656** Or, if zState is non-NULL, it must be a path to a database file in which
205657** the RBU extension can store the state of the update.
205658**
205659** When resuming an RBU update, the zState argument must be passed the same
205660** value as when the RBU update was started.
205661**
205662** Once the RBU update is finished, the RBU extension does not
205663** automatically remove any zState database file, even if it created it.
205664**
205665** By default, RBU uses the default VFS to access the files on disk. To
205666** use a VFS other than the default, an SQLite "file:" URI containing a
205667** "vfs=..." option may be passed as the zTarget option.
205668**
205669** IMPORTANT NOTE FOR ZIPVFS USERS: The RBU extension works with all of
205670** SQLite's built-in VFSs, including the multiplexor VFS. However it does
205671** not work out of the box with zipvfs. Refer to the comment describing
205672** the zipvfs_create_vfs() API below for details on using RBU with zipvfs.
205673*/
205674SQLITE_API sqlite3rbu *sqlite3rbu_open(
205675 const char *zTarget,
205676 const char *zRbu,
205677 const char *zState
205678);
205679
205680/*
205681** Open an RBU handle to perform an RBU vacuum on database file zTarget.
205682** An RBU vacuum is similar to SQLite's built-in VACUUM command, except
205683** that it can be suspended and resumed like an RBU update.
205684**
205685** The second argument to this function identifies a database in which
205686** to store the state of the RBU vacuum operation if it is suspended. The
205687** first time sqlite3rbu_vacuum() is called, to start an RBU vacuum
205688** operation, the state database should either not exist or be empty
205689** (contain no tables). If an RBU vacuum is suspended by calling
205690** sqlite3rbu_close() on the RBU handle before sqlite3rbu_step() has
205691** returned SQLITE_DONE, the vacuum state is stored in the state database.
205692** The vacuum can be resumed by calling this function to open a new RBU
205693** handle specifying the same target and state databases.
205694**
205695** If the second argument passed to this function is NULL, then the
205696** name of the state database is "<database>-vacuum", where <database>
205697** is the name of the target database file. In this case, on UNIX, if the
205698** state database is not already present in the file-system, it is created
205699** with the same permissions as the target db is made.
205700**
205701** With an RBU vacuum, it is an SQLITE_MISUSE error if the name of the
205702** state database ends with "-vactmp". This name is reserved for internal
205703** use.
205704**
205705** This function does not delete the state database after an RBU vacuum
205706** is completed, even if it created it. However, if the call to
205707** sqlite3rbu_close() returns any value other than SQLITE_OK, the contents
205708** of the state tables within the state database are zeroed. This way,
205709** the next call to sqlite3rbu_vacuum() opens a handle that starts a
205710** new RBU vacuum operation.
205711**
205712** As with sqlite3rbu_open(), Zipvfs users should rever to the comment
205713** describing the sqlite3rbu_create_vfs() API function below for
205714** a description of the complications associated with using RBU with
205715** zipvfs databases.
205716*/
205717SQLITE_API sqlite3rbu *sqlite3rbu_vacuum(
205718 const char *zTarget,
205719 const char *zState
205720);
205721
205722/*
205723** Configure a limit for the amount of temp space that may be used by
205724** the RBU handle passed as the first argument. The new limit is specified
205725** in bytes by the second parameter. If it is positive, the limit is updated.
205726** If the second parameter to this function is passed zero, then the limit
205727** is removed entirely. If the second parameter is negative, the limit is
205728** not modified (this is useful for querying the current limit).
205729**
205730** In all cases the returned value is the current limit in bytes (zero
205731** indicates unlimited).
205732**
205733** If the temp space limit is exceeded during operation, an SQLITE_FULL
205734** error is returned.
205735*/
205736SQLITE_API sqlite3_int64 sqlite3rbu_temp_size_limit(sqlite3rbu*, sqlite3_int64);
205737
205738/*
205739** Return the current amount of temp file space, in bytes, currently used by
205740** the RBU handle passed as the only argument.
205741*/
205742SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu*);
205743
205744/*
205745** Internally, each RBU connection uses a separate SQLite database
205746** connection to access the target and rbu update databases. This
205747** API allows the application direct access to these database handles.
205748**
205749** The first argument passed to this function must be a valid, open, RBU
205750** handle. The second argument should be passed zero to access the target
205751** database handle, or non-zero to access the rbu update database handle.
205752** Accessing the underlying database handles may be useful in the
205753** following scenarios:
205754**
205755** * If any target tables are virtual tables, it may be necessary to
205756** call sqlite3_create_module() on the target database handle to
205757** register the required virtual table implementations.
205758**
205759** * If the data_xxx tables in the RBU source database are virtual
205760** tables, the application may need to call sqlite3_create_module() on
205761** the rbu update db handle to any required virtual table
205762** implementations.
205763**
205764** * If the application uses the "rbu_delta()" feature described above,
205765** it must use sqlite3_create_function() or similar to register the
205766** rbu_delta() implementation with the target database handle.
205767**
205768** If an error has occurred, either while opening or stepping the RBU object,
205769** this function may return NULL. The error code and message may be collected
205770** when sqlite3rbu_close() is called.
205771**
205772** Database handles returned by this function remain valid until the next
205773** call to any sqlite3rbu_xxx() function other than sqlite3rbu_db().
205774*/
205775SQLITE_API sqlite3 *sqlite3rbu_db(sqlite3rbu*, int bRbu);
205776
205777/*
205778** Do some work towards applying the RBU update to the target db.
205779**
205780** Return SQLITE_DONE if the update has been completely applied, or
205781** SQLITE_OK if no error occurs but there remains work to do to apply
205782** the RBU update. If an error does occur, some other error code is
205783** returned.
205784**
205785** Once a call to sqlite3rbu_step() has returned a value other than
205786** SQLITE_OK, all subsequent calls on the same RBU handle are no-ops
205787** that immediately return the same value.
205788*/
205789SQLITE_API int sqlite3rbu_step(sqlite3rbu *pRbu);
205790
205791/*
205792** Force RBU to save its state to disk.
205793**
205794** If a power failure or application crash occurs during an update, following
205795** system recovery RBU may resume the update from the point at which the state
205796** was last saved. In other words, from the most recent successful call to
205797** sqlite3rbu_close() or this function.
205798**
205799** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
205800*/
205801SQLITE_API int sqlite3rbu_savestate(sqlite3rbu *pRbu);
205802
205803/*
205804** Close an RBU handle.
205805**
205806** If the RBU update has been completely applied, mark the RBU database
205807** as fully applied. Otherwise, assuming no error has occurred, save the
205808** current state of the RBU update appliation to the RBU database.
205809**
205810** If an error has already occurred as part of an sqlite3rbu_step()
205811** or sqlite3rbu_open() call, or if one occurs within this function, an
205812** SQLite error code is returned. Additionally, if pzErrmsg is not NULL,
205813** *pzErrmsg may be set to point to a buffer containing a utf-8 formatted
205814** English language error message. It is the responsibility of the caller to
205815** eventually free any such buffer using sqlite3_free().
205816**
205817** Otherwise, if no error occurs, this function returns SQLITE_OK if the
205818** update has been partially applied, or SQLITE_DONE if it has been
205819** completely applied.
205820*/
205821SQLITE_API int sqlite3rbu_close(sqlite3rbu *pRbu, char **pzErrmsg);
205822
205823/*
205824** Return the total number of key-value operations (inserts, deletes or
205825** updates) that have been performed on the target database since the
205826** current RBU update was started.
205827*/
205828SQLITE_API sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu);
205829
205830/*
205831** Obtain permyriadage (permyriadage is to 10000 as percentage is to 100)
205832** progress indications for the two stages of an RBU update. This API may
205833** be useful for driving GUI progress indicators and similar.
205834**
205835** An RBU update is divided into two stages:
205836**
205837** * Stage 1, in which changes are accumulated in an oal/wal file, and
205838** * Stage 2, in which the contents of the wal file are copied into the
205839** main database.
205840**
205841** The update is visible to non-RBU clients during stage 2. During stage 1
205842** non-RBU reader clients may see the original database.
205843**
205844** If this API is called during stage 2 of the update, output variable
205845** (*pnOne) is set to 10000 to indicate that stage 1 has finished and (*pnTwo)
205846** to a value between 0 and 10000 to indicate the permyriadage progress of
205847** stage 2. A value of 5000 indicates that stage 2 is half finished,
205848** 9000 indicates that it is 90% finished, and so on.
205849**
205850** If this API is called during stage 1 of the update, output variable
205851** (*pnTwo) is set to 0 to indicate that stage 2 has not yet started. The
205852** value to which (*pnOne) is set depends on whether or not the RBU
205853** database contains an "rbu_count" table. The rbu_count table, if it
205854** exists, must contain the same columns as the following:
205855**
205856** CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
205857**
205858** There must be one row in the table for each source (data_xxx) table within
205859** the RBU database. The 'tbl' column should contain the name of the source
205860** table. The 'cnt' column should contain the number of rows within the
205861** source table.
205862**
205863** If the rbu_count table is present and populated correctly and this
205864** API is called during stage 1, the *pnOne output variable is set to the
205865** permyriadage progress of the same stage. If the rbu_count table does
205866** not exist, then (*pnOne) is set to -1 during stage 1. If the rbu_count
205867** table exists but is not correctly populated, the value of the *pnOne
205868** output variable during stage 1 is undefined.
205869*/
205870SQLITE_API void sqlite3rbu_bp_progress(sqlite3rbu *pRbu, int *pnOne, int*pnTwo);
205871
205872/*
205873** Obtain an indication as to the current stage of an RBU update or vacuum.
205874** This function always returns one of the SQLITE_RBU_STATE_XXX constants
205875** defined in this file. Return values should be interpreted as follows:
205876**
205877** SQLITE_RBU_STATE_OAL:
205878** RBU is currently building a *-oal file. The next call to sqlite3rbu_step()
205879** may either add further data to the *-oal file, or compute data that will
205880** be added by a subsequent call.
205881**
205882** SQLITE_RBU_STATE_MOVE:
205883** RBU has finished building the *-oal file. The next call to sqlite3rbu_step()
205884** will move the *-oal file to the equivalent *-wal path. If the current
205885** operation is an RBU update, then the updated version of the database
205886** file will become visible to ordinary SQLite clients following the next
205887** call to sqlite3rbu_step().
205888**
205889** SQLITE_RBU_STATE_CHECKPOINT:
205890** RBU is currently performing an incremental checkpoint. The next call to
205891** sqlite3rbu_step() will copy a page of data from the *-wal file into
205892** the target database file.
205893**
205894** SQLITE_RBU_STATE_DONE:
205895** The RBU operation has finished. Any subsequent calls to sqlite3rbu_step()
205896** will immediately return SQLITE_DONE.
205897**
205898** SQLITE_RBU_STATE_ERROR:
205899** An error has occurred. Any subsequent calls to sqlite3rbu_step() will
205900** immediately return the SQLite error code associated with the error.
205901*/
205902#define SQLITE_RBU_STATE_OAL 1
205903#define SQLITE_RBU_STATE_MOVE 2
205904#define SQLITE_RBU_STATE_CHECKPOINT 3
205905#define SQLITE_RBU_STATE_DONE 4
205906#define SQLITE_RBU_STATE_ERROR 5
205907
205908SQLITE_API int sqlite3rbu_state(sqlite3rbu *pRbu);
205909
205910/*
205911** Create an RBU VFS named zName that accesses the underlying file-system
205912** via existing VFS zParent. Or, if the zParent parameter is passed NULL,
205913** then the new RBU VFS uses the default system VFS to access the file-system.
205914** The new object is registered as a non-default VFS with SQLite before
205915** returning.
205916**
205917** Part of the RBU implementation uses a custom VFS object. Usually, this
205918** object is created and deleted automatically by RBU.
205919**
205920** The exception is for applications that also use zipvfs. In this case,
205921** the custom VFS must be explicitly created by the user before the RBU
205922** handle is opened. The RBU VFS should be installed so that the zipvfs
205923** VFS uses the RBU VFS, which in turn uses any other VFS layers in use
205924** (for example multiplexor) to access the file-system. For example,
205925** to assemble an RBU enabled VFS stack that uses both zipvfs and
205926** multiplexor (error checking omitted):
205927**
205928** // Create a VFS named "multiplex" (not the default).
205929** sqlite3_multiplex_initialize(0, 0);
205930**
205931** // Create an rbu VFS named "rbu" that uses multiplexor. If the
205932** // second argument were replaced with NULL, the "rbu" VFS would
205933** // access the file-system via the system default VFS, bypassing the
205934** // multiplexor.
205935** sqlite3rbu_create_vfs("rbu", "multiplex");
205936**
205937** // Create a zipvfs VFS named "zipvfs" that uses rbu.
205938** zipvfs_create_vfs_v3("zipvfs", "rbu", 0, xCompressorAlgorithmDetector);
205939**
205940** // Make zipvfs the default VFS.
205941** sqlite3_vfs_register(sqlite3_vfs_find("zipvfs"), 1);
205942**
205943** Because the default VFS created above includes a RBU functionality, it
205944** may be used by RBU clients. Attempting to use RBU with a zipvfs VFS stack
205945** that does not include the RBU layer results in an error.
205946**
205947** The overhead of adding the "rbu" VFS to the system is negligible for
205948** non-RBU users. There is no harm in an application accessing the
205949** file-system via "rbu" all the time, even if it only uses RBU functionality
205950** occasionally.
205951*/
205952SQLITE_API int sqlite3rbu_create_vfs(const char *zName, const char *zParent);
205953
205954/*
205955** Deregister and destroy an RBU vfs created by an earlier call to
205956** sqlite3rbu_create_vfs().
205957**
205958** VFS objects are not reference counted. If a VFS object is destroyed
205959** before all database handles that use it have been closed, the results
205960** are undefined.
205961*/
205962SQLITE_API void sqlite3rbu_destroy_vfs(const char *zName);
205963
205964#if 0
205965} /* end of the 'extern "C"' block */
205966#endif
205967
205968#endif /* _SQLITE3RBU_H */
205969
205970/************** End of sqlite3rbu.h ******************************************/
205971/************** Continuing where we left off in sqlite3rbu.c *****************/
205972
205973#if defined(_WIN32_WCE)
205974/* #include "windows.h" */
205975#endif
205976
205977/* Maximum number of prepared UPDATE statements held by this module */
205978#define SQLITE_RBU_UPDATE_CACHESIZE 16
205979
205980/* Delta checksums disabled by default. Compile with -DRBU_ENABLE_DELTA_CKSUM
205981** to enable checksum verification.
205982*/
205983#ifndef RBU_ENABLE_DELTA_CKSUM
205984# define RBU_ENABLE_DELTA_CKSUM 0
205985#endif
205986
205987/*
205988** Swap two objects of type TYPE.
205989*/
205990#if !defined(SQLITE_AMALGAMATION1)
205991# define SWAP(TYPE,A,B){TYPE t=A; A=B; B=t;} {TYPE t=A; A=B; B=t;}
205992#endif
205993
205994/*
205995** Name of the URI option that causes RBU to take an exclusive lock as
205996** part of the incremental checkpoint operation.
205997*/
205998#define RBU_EXCLUSIVE_CHECKPOINT "rbu_exclusive_checkpoint"
205999
206000
206001/*
206002** The rbu_state table is used to save the state of a partially applied
206003** update so that it can be resumed later. The table consists of integer
206004** keys mapped to values as follows:
206005**
206006** RBU_STATE_STAGE:
206007** May be set to integer values 1, 2, 4 or 5. As follows:
206008** 1: the *-rbu file is currently under construction.
206009** 2: the *-rbu file has been constructed, but not yet moved
206010** to the *-wal path.
206011** 4: the checkpoint is underway.
206012** 5: the rbu update has been checkpointed.
206013**
206014** RBU_STATE_TBL:
206015** Only valid if STAGE==1. The target database name of the table
206016** currently being written.
206017**
206018** RBU_STATE_IDX:
206019** Only valid if STAGE==1. The target database name of the index
206020** currently being written, or NULL if the main table is currently being
206021** updated.
206022**
206023** RBU_STATE_ROW:
206024** Only valid if STAGE==1. Number of rows already processed for the current
206025** table/index.
206026**
206027** RBU_STATE_PROGRESS:
206028** Trbul number of sqlite3rbu_step() calls made so far as part of this
206029** rbu update.
206030**
206031** RBU_STATE_CKPT:
206032** Valid if STAGE==4. The 64-bit checksum associated with the wal-index
206033** header created by recovering the *-wal file. This is used to detect
206034** cases when another client appends frames to the *-wal file in the
206035** middle of an incremental checkpoint (an incremental checkpoint cannot
206036** be continued if this happens).
206037**
206038** RBU_STATE_COOKIE:
206039** Valid if STAGE==1. The current change-counter cookie value in the
206040** target db file.
206041**
206042** RBU_STATE_OALSZ:
206043** Valid if STAGE==1. The size in bytes of the *-oal file.
206044**
206045** RBU_STATE_DATATBL:
206046** Only valid if STAGE==1. The RBU database name of the table
206047** currently being read.
206048*/
206049#define RBU_STATE_STAGE 1
206050#define RBU_STATE_TBL 2
206051#define RBU_STATE_IDX 3
206052#define RBU_STATE_ROW 4
206053#define RBU_STATE_PROGRESS 5
206054#define RBU_STATE_CKPT 6
206055#define RBU_STATE_COOKIE 7
206056#define RBU_STATE_OALSZ 8
206057#define RBU_STATE_PHASEONESTEP 9
206058#define RBU_STATE_DATATBL 10
206059
206060#define RBU_STAGE_OAL 1
206061#define RBU_STAGE_MOVE 2
206062#define RBU_STAGE_CAPTURE 3
206063#define RBU_STAGE_CKPT 4
206064#define RBU_STAGE_DONE 5
206065
206066
206067#define RBU_CREATE_STATE \
206068 "CREATE TABLE IF NOT EXISTS %s.rbu_state(k INTEGER PRIMARY KEY, v)"
206069
206070typedef struct RbuFrame RbuFrame;
206071typedef struct RbuObjIter RbuObjIter;
206072typedef struct RbuState RbuState;
206073typedef struct RbuSpan RbuSpan;
206074typedef struct rbu_vfs rbu_vfs;
206075typedef struct rbu_file rbu_file;
206076typedef struct RbuUpdateStmt RbuUpdateStmt;
206077
206078#if !defined(SQLITE_AMALGAMATION1)
206079typedef unsigned int u32;
206080typedef unsigned short u16;
206081typedef unsigned char u8;
206082typedef sqlite3_int64 i64;
206083#endif
206084
206085/*
206086** These values must match the values defined in wal.c for the equivalent
206087** locks. These are not magic numbers as they are part of the SQLite file
206088** format.
206089*/
206090#define WAL_LOCK_WRITE 0
206091#define WAL_LOCK_CKPT 1
206092#define WAL_LOCK_READ0 3
206093
206094#define SQLITE_FCNTL_RBUCNT 5149216
206095
206096/*
206097** A structure to store values read from the rbu_state table in memory.
206098*/
206099struct RbuState {
206100 int eStage;
206101 char *zTbl;
206102 char *zDataTbl;
206103 char *zIdx;
206104 i64 iWalCksum;
206105 int nRow;
206106 i64 nProgress;
206107 u32 iCookie;
206108 i64 iOalSz;
206109 i64 nPhaseOneStep;
206110};
206111
206112struct RbuUpdateStmt {
206113 char *zMask; /* Copy of update mask used with pUpdate */
206114 sqlite3_stmt *pUpdate; /* Last update statement (or NULL) */
206115 RbuUpdateStmt *pNext;
206116};
206117
206118struct RbuSpan {
206119 const char *zSpan;
206120 int nSpan;
206121};
206122
206123/*
206124** An iterator of this type is used to iterate through all objects in
206125** the target database that require updating. For each such table, the
206126** iterator visits, in order:
206127**
206128** * the table itself,
206129** * each index of the table (zero or more points to visit), and
206130** * a special "cleanup table" state.
206131**
206132** abIndexed:
206133** If the table has no indexes on it, abIndexed is set to NULL. Otherwise,
206134** it points to an array of flags nTblCol elements in size. The flag is
206135** set for each column that is either a part of the PK or a part of an
206136** index. Or clear otherwise.
206137**
206138** If there are one or more partial indexes on the table, all fields of
206139** this array set set to 1. This is because in that case, the module has
206140** no way to tell which fields will be required to add and remove entries
206141** from the partial indexes.
206142**
206143*/
206144struct RbuObjIter {
206145 sqlite3_stmt *pTblIter; /* Iterate through tables */
206146 sqlite3_stmt *pIdxIter; /* Index iterator */
206147 int nTblCol; /* Size of azTblCol[] array */
206148 char **azTblCol; /* Array of unquoted target column names */
206149 char **azTblType; /* Array of target column types */
206150 int *aiSrcOrder; /* src table col -> target table col */
206151 u8 *abTblPk; /* Array of flags, set on target PK columns */
206152 u8 *abNotNull; /* Array of flags, set on NOT NULL columns */
206153 u8 *abIndexed; /* Array of flags, set on indexed & PK cols */
206154 int eType; /* Table type - an RBU_PK_XXX value */
206155
206156 /* Output variables. zTbl==0 implies EOF. */
206157 int bCleanup; /* True in "cleanup" state */
206158 const char *zTbl; /* Name of target db table */
206159 const char *zDataTbl; /* Name of rbu db table (or null) */
206160 const char *zIdx; /* Name of target db index (or null) */
206161 int iTnum; /* Root page of current object */
206162 int iPkTnum; /* If eType==EXTERNAL, root of PK index */
206163 int bUnique; /* Current index is unique */
206164 int nIndex; /* Number of aux. indexes on table zTbl */
206165
206166 /* Statements created by rbuObjIterPrepareAll() */
206167 int nCol; /* Number of columns in current object */
206168 sqlite3_stmt *pSelect; /* Source data */
206169 sqlite3_stmt *pInsert; /* Statement for INSERT operations */
206170 sqlite3_stmt *pDelete; /* Statement for DELETE ops */
206171 sqlite3_stmt *pTmpInsert; /* Insert into rbu_tmp_$zDataTbl */
206172 int nIdxCol;
206173 RbuSpan *aIdxCol;
206174 char *zIdxSql;
206175
206176 /* Last UPDATE used (for PK b-tree updates only), or NULL. */
206177 RbuUpdateStmt *pRbuUpdate;
206178};
206179
206180/*
206181** Values for RbuObjIter.eType
206182**
206183** 0: Table does not exist (error)
206184** 1: Table has an implicit rowid.
206185** 2: Table has an explicit IPK column.
206186** 3: Table has an external PK index.
206187** 4: Table is WITHOUT ROWID.
206188** 5: Table is a virtual table.
206189*/
206190#define RBU_PK_NOTABLE 0
206191#define RBU_PK_NONE 1
206192#define RBU_PK_IPK 2
206193#define RBU_PK_EXTERNAL 3
206194#define RBU_PK_WITHOUT_ROWID 4
206195#define RBU_PK_VTAB 5
206196
206197
206198/*
206199** Within the RBU_STAGE_OAL stage, each call to sqlite3rbu_step() performs
206200** one of the following operations.
206201*/
206202#define RBU_INSERT 1 /* Insert on a main table b-tree */
206203#define RBU_DELETE 2 /* Delete a row from a main table b-tree */
206204#define RBU_REPLACE 3 /* Delete and then insert a row */
206205#define RBU_IDX_DELETE 4 /* Delete a row from an aux. index b-tree */
206206#define RBU_IDX_INSERT 5 /* Insert on an aux. index b-tree */
206207
206208#define RBU_UPDATE 6 /* Update a row in a main table b-tree */
206209
206210/*
206211** A single step of an incremental checkpoint - frame iWalFrame of the wal
206212** file should be copied to page iDbPage of the database file.
206213*/
206214struct RbuFrame {
206215 u32 iDbPage;
206216 u32 iWalFrame;
206217};
206218
206219/*
206220** RBU handle.
206221**
206222** nPhaseOneStep:
206223** If the RBU database contains an rbu_count table, this value is set to
206224** a running estimate of the number of b-tree operations required to
206225** finish populating the *-oal file. This allows the sqlite3_bp_progress()
206226** API to calculate the permyriadage progress of populating the *-oal file
206227** using the formula:
206228**
206229** permyriadage = (10000 * nProgress) / nPhaseOneStep
206230**
206231** nPhaseOneStep is initialized to the sum of:
206232**
206233** nRow * (nIndex + 1)
206234**
206235** for all source tables in the RBU database, where nRow is the number
206236** of rows in the source table and nIndex the number of indexes on the
206237** corresponding target database table.
206238**
206239** This estimate is accurate if the RBU update consists entirely of
206240** INSERT operations. However, it is inaccurate if:
206241**
206242** * the RBU update contains any UPDATE operations. If the PK specified
206243** for an UPDATE operation does not exist in the target table, then
206244** no b-tree operations are required on index b-trees. Or if the
206245** specified PK does exist, then (nIndex*2) such operations are
206246** required (one delete and one insert on each index b-tree).
206247**
206248** * the RBU update contains any DELETE operations for which the specified
206249** PK does not exist. In this case no operations are required on index
206250** b-trees.
206251**
206252** * the RBU update contains REPLACE operations. These are similar to
206253** UPDATE operations.
206254**
206255** nPhaseOneStep is updated to account for the conditions above during the
206256** first pass of each source table. The updated nPhaseOneStep value is
206257** stored in the rbu_state table if the RBU update is suspended.
206258*/
206259struct sqlite3rbu {
206260 int eStage; /* Value of RBU_STATE_STAGE field */
206261 sqlite3 *dbMain; /* target database handle */
206262 sqlite3 *dbRbu; /* rbu database handle */
206263 char *zTarget; /* Path to target db */
206264 char *zRbu; /* Path to rbu db */
206265 char *zState; /* Path to state db (or NULL if zRbu) */
206266 char zStateDb[5]; /* Db name for state ("stat" or "main") */
206267 int rc; /* Value returned by last rbu_step() call */
206268 char *zErrmsg; /* Error message if rc!=SQLITE_OK */
206269 int nStep; /* Rows processed for current object */
206270 int nProgress; /* Rows processed for all objects */
206271 RbuObjIter objiter; /* Iterator for skipping through tbl/idx */
206272 const char *zVfsName; /* Name of automatically created rbu vfs */
206273 rbu_file *pTargetFd; /* File handle open on target db */
206274 int nPagePerSector; /* Pages per sector for pTargetFd */
206275 i64 iOalSz;
206276 i64 nPhaseOneStep;
206277
206278 /* The following state variables are used as part of the incremental
206279 ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
206280 ** function rbuSetupCheckpoint() for details. */
206281 u32 iMaxFrame; /* Largest iWalFrame value in aFrame[] */
206282 u32 mLock;
206283 int nFrame; /* Entries in aFrame[] array */
206284 int nFrameAlloc; /* Allocated size of aFrame[] array */
206285 RbuFrame *aFrame;
206286 int pgsz;
206287 u8 *aBuf;
206288 i64 iWalCksum;
206289 i64 szTemp; /* Current size of all temp files in use */
206290 i64 szTempLimit; /* Total size limit for temp files */
206291
206292 /* Used in RBU vacuum mode only */
206293 int nRbu; /* Number of RBU VFS in the stack */
206294 rbu_file *pRbuFd; /* Fd for main db of dbRbu */
206295};
206296
206297/*
206298** An rbu VFS is implemented using an instance of this structure.
206299**
206300** Variable pRbu is only non-NULL for automatically created RBU VFS objects.
206301** It is NULL for RBU VFS objects created explicitly using
206302** sqlite3rbu_create_vfs(). It is used to track the total amount of temp
206303** space used by the RBU handle.
206304*/
206305struct rbu_vfs {
206306 sqlite3_vfs base; /* rbu VFS shim methods */
206307 sqlite3_vfs *pRealVfs; /* Underlying VFS */
206308 sqlite3_mutex *mutex; /* Mutex to protect pMain */
206309 sqlite3rbu *pRbu; /* Owner RBU object */
206310 rbu_file *pMain; /* List of main db files */
206311 rbu_file *pMainRbu; /* List of main db files with pRbu!=0 */
206312};
206313
206314/*
206315** Each file opened by an rbu VFS is represented by an instance of
206316** the following structure.
206317**
206318** If this is a temporary file (pRbu!=0 && flags&DELETE_ON_CLOSE), variable
206319** "sz" is set to the current size of the database file.
206320*/
206321struct rbu_file {
206322 sqlite3_file base; /* sqlite3_file methods */
206323 sqlite3_file *pReal; /* Underlying file handle */
206324 rbu_vfs *pRbuVfs; /* Pointer to the rbu_vfs object */
206325 sqlite3rbu *pRbu; /* Pointer to rbu object (rbu target only) */
206326 i64 sz; /* Size of file in bytes (temp only) */
206327
206328 int openFlags; /* Flags this file was opened with */
206329 u32 iCookie; /* Cookie value for main db files */
206330 u8 iWriteVer; /* "write-version" value for main db files */
206331 u8 bNolock; /* True to fail EXCLUSIVE locks */
206332
206333 int nShm; /* Number of entries in apShm[] array */
206334 char **apShm; /* Array of mmap'd *-shm regions */
206335 char *zDel; /* Delete this when closing file */
206336
206337 const char *zWal; /* Wal filename for this main db file */
206338 rbu_file *pWalFd; /* Wal file descriptor for this main db */
206339 rbu_file *pMainNext; /* Next MAIN_DB file */
206340 rbu_file *pMainRbuNext; /* Next MAIN_DB file with pRbu!=0 */
206341};
206342
206343/*
206344** True for an RBU vacuum handle, or false otherwise.
206345*/
206346#define rbuIsVacuum(p) ((p)->zTarget==0)
206347
206348
206349/*************************************************************************
206350** The following three functions, found below:
206351**
206352** rbuDeltaGetInt()
206353** rbuDeltaChecksum()
206354** rbuDeltaApply()
206355**
206356** are lifted from the fossil source code (http://fossil-scm.org). They
206357** are used to implement the scalar SQL function rbu_fossil_delta().
206358*/
206359
206360/*
206361** Read bytes from *pz and convert them into a positive integer. When
206362** finished, leave *pz pointing to the first character past the end of
206363** the integer. The *pLen parameter holds the length of the string
206364** in *pz and is decremented once for each character in the integer.
206365*/
206366static unsigned int rbuDeltaGetInt(const char **pz, int *pLen){
206367 static const signed char zValue[] = {
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, -1, -1, -1, -1,
206370 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
206371 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
206372 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
206373 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, 36,
206374 -1, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
206375 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, -1, -1, -1, 63, -1,
206376 };
206377 unsigned int v = 0;
206378 int c;
206379 unsigned char *z = (unsigned char*)*pz;
206380 unsigned char *zStart = z;
206381 while( (c = zValue[0x7f&*(z++)])>=0 ){
206382 v = (v<<6) + c;
206383 }
206384 z--;
206385 *pLen -= z - zStart;
206386 *pz = (char*)z;
206387 return v;
206388}
206389
206390#if RBU_ENABLE_DELTA_CKSUM
206391/*
206392** Compute a 32-bit checksum on the N-byte buffer. Return the result.
206393*/
206394static unsigned int rbuDeltaChecksum(const char *zIn, size_t N){
206395 const unsigned char *z = (const unsigned char *)zIn;
206396 unsigned sum0 = 0;
206397 unsigned sum1 = 0;
206398 unsigned sum2 = 0;
206399 unsigned sum3 = 0;
206400 while(N >= 16){
206401 sum0 += ((unsigned)z[0] + z[4] + z[8] + z[12]);
206402 sum1 += ((unsigned)z[1] + z[5] + z[9] + z[13]);
206403 sum2 += ((unsigned)z[2] + z[6] + z[10]+ z[14]);
206404 sum3 += ((unsigned)z[3] + z[7] + z[11]+ z[15]);
206405 z += 16;
206406 N -= 16;
206407 }
206408 while(N >= 4){
206409 sum0 += z[0];
206410 sum1 += z[1];
206411 sum2 += z[2];
206412 sum3 += z[3];
206413 z += 4;
206414 N -= 4;
206415 }
206416 sum3 += (sum2 << 8) + (sum1 << 16) + (sum0 << 24);
206417 switch(N){
206418 case 3: sum3 += (z[2] << 8);
206419 case 2: sum3 += (z[1] << 16);
206420 case 1: sum3 += (z[0] << 24);
206421 default: ;
206422 }
206423 return sum3;
206424}
206425#endif
206426
206427/*
206428** Apply a delta.
206429**
206430** The output buffer should be big enough to hold the whole output
206431** file and a NUL terminator at the end. The delta_output_size()
206432** routine will determine this size for you.
206433**
206434** The delta string should be null-terminated. But the delta string
206435** may contain embedded NUL characters (if the input and output are
206436** binary files) so we also have to pass in the length of the delta in
206437** the lenDelta parameter.
206438**
206439** This function returns the size of the output file in bytes (excluding
206440** the final NUL terminator character). Except, if the delta string is
206441** malformed or intended for use with a source file other than zSrc,
206442** then this routine returns -1.
206443**
206444** Refer to the delta_create() documentation above for a description
206445** of the delta file format.
206446*/
206447static int rbuDeltaApply(
206448 const char *zSrc, /* The source or pattern file */
206449 int lenSrc, /* Length of the source file */
206450 const char *zDelta, /* Delta to apply to the pattern */
206451 int lenDelta, /* Length of the delta */
206452 char *zOut /* Write the output into this preallocated buffer */
206453){
206454 unsigned int limit;
206455 unsigned int total = 0;
206456#if RBU_ENABLE_DELTA_CKSUM
206457 char *zOrigOut = zOut;
206458#endif
206459
206460 limit = rbuDeltaGetInt(&zDelta, &lenDelta);
206461 if( *zDelta!='\n' ){
206462 /* ERROR: size integer not terminated by "\n" */
206463 return -1;
206464 }
206465 zDelta++; lenDelta--;
206466 while( *zDelta && lenDelta>0 ){
206467 unsigned int cnt, ofst;
206468 cnt = rbuDeltaGetInt(&zDelta, &lenDelta);
206469 switch( zDelta[0] ){
206470 case '@': {
206471 zDelta++; lenDelta--;
206472 ofst = rbuDeltaGetInt(&zDelta, &lenDelta);
206473 if( lenDelta>0 && zDelta[0]!=',' ){
206474 /* ERROR: copy command not terminated by ',' */
206475 return -1;
206476 }
206477 zDelta++; lenDelta--;
206478 total += cnt;
206479 if( total>limit ){
206480 /* ERROR: copy exceeds output file size */
206481 return -1;
206482 }
206483 if( (int)(ofst+cnt) > lenSrc ){
206484 /* ERROR: copy extends past end of input */
206485 return -1;
206486 }
206487 memcpy(zOut, &zSrc[ofst], cnt);
206488 zOut += cnt;
206489 break;
206490 }
206491 case ':': {
206492 zDelta++; lenDelta--;
206493 total += cnt;
206494 if( total>limit ){
206495 /* ERROR: insert command gives an output larger than predicted */
206496 return -1;
206497 }
206498 if( (int)cnt>lenDelta ){
206499 /* ERROR: insert count exceeds size of delta */
206500 return -1;
206501 }
206502 memcpy(zOut, zDelta, cnt);
206503 zOut += cnt;
206504 zDelta += cnt;
206505 lenDelta -= cnt;
206506 break;
206507 }
206508 case ';': {
206509 zDelta++; lenDelta--;
206510 zOut[0] = 0;
206511#if RBU_ENABLE_DELTA_CKSUM
206512 if( cnt!=rbuDeltaChecksum(zOrigOut, total) ){
206513 /* ERROR: bad checksum */
206514 return -1;
206515 }
206516#endif
206517 if( total!=limit ){
206518 /* ERROR: generated size does not match predicted size */
206519 return -1;
206520 }
206521 return total;
206522 }
206523 default: {
206524 /* ERROR: unknown delta operator */
206525 return -1;
206526 }
206527 }
206528 }
206529 /* ERROR: unterminated delta */
206530 return -1;
206531}
206532
206533static int rbuDeltaOutputSize(const char *zDelta, int lenDelta){
206534 int size;
206535 size = rbuDeltaGetInt(&zDelta, &lenDelta);
206536 if( *zDelta!='\n' ){
206537 /* ERROR: size integer not terminated by "\n" */
206538 return -1;
206539 }
206540 return size;
206541}
206542
206543/*
206544** End of code taken from fossil.
206545*************************************************************************/
206546
206547/*
206548** Implementation of SQL scalar function rbu_fossil_delta().
206549**
206550** This function applies a fossil delta patch to a blob. Exactly two
206551** arguments must be passed to this function. The first is the blob to
206552** patch and the second the patch to apply. If no error occurs, this
206553** function returns the patched blob.
206554*/
206555static void rbuFossilDeltaFunc(
206556 sqlite3_context *context,
206557 int argc,
206558 sqlite3_value **argv
206559){
206560 const char *aDelta;
206561 int nDelta;
206562 const char *aOrig;
206563 int nOrig;
206564
206565 int nOut;
206566 int nOut2;
206567 char *aOut;
206568
206569 assert( argc==2 )((void) (0));
206570
206571 nOrig = sqlite3_value_bytes(argv[0]);
206572 aOrig = (const char*)sqlite3_value_blob(argv[0]);
206573 nDelta = sqlite3_value_bytes(argv[1]);
206574 aDelta = (const char*)sqlite3_value_blob(argv[1]);
206575
206576 /* Figure out the size of the output */
206577 nOut = rbuDeltaOutputSize(aDelta, nDelta);
206578 if( nOut<0 ){
206579 sqlite3_result_error(context, "corrupt fossil delta", -1);
206580 return;
206581 }
206582
206583 aOut = sqlite3_malloc(nOut+1);
206584 if( aOut==0 ){
206585 sqlite3_result_error_nomem(context);
206586 }else{
206587 nOut2 = rbuDeltaApply(aOrig, nOrig, aDelta, nDelta, aOut);
206588 if( nOut2!=nOut ){
206589 sqlite3_free(aOut);
206590 sqlite3_result_error(context, "corrupt fossil delta", -1);
206591 }else{
206592 sqlite3_result_blob(context, aOut, nOut, sqlite3_free);
206593 }
206594 }
206595}
206596
206597
206598/*
206599** Prepare the SQL statement in buffer zSql against database handle db.
206600** If successful, set *ppStmt to point to the new statement and return
206601** SQLITE_OK.
206602**
206603** Otherwise, if an error does occur, set *ppStmt to NULL and return
206604** an SQLite error code. Additionally, set output variable *pzErrmsg to
206605** point to a buffer containing an error message. It is the responsibility
206606** of the caller to (eventually) free this buffer using sqlite3_free().
206607*/
206608static int prepareAndCollectError(
206609 sqlite3 *db,
206610 sqlite3_stmt **ppStmt,
206611 char **pzErrmsg,
206612 const char *zSql
206613){
206614 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
206615 if( rc!=SQLITE_OK0 ){
206616 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
206617 *ppStmt = 0;
206618 }
206619 return rc;
206620}
206621
206622/*
206623** Reset the SQL statement passed as the first argument. Return a copy
206624** of the value returned by sqlite3_reset().
206625**
206626** If an error has occurred, then set *pzErrmsg to point to a buffer
206627** containing an error message. It is the responsibility of the caller
206628** to eventually free this buffer using sqlite3_free().
206629*/
206630static int resetAndCollectError(sqlite3_stmt *pStmt, char **pzErrmsg){
206631 int rc = sqlite3_reset(pStmt);
206632 if( rc!=SQLITE_OK0 ){
206633 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(sqlite3_db_handle(pStmt)));
206634 }
206635 return rc;
206636}
206637
206638/*
206639** Unless it is NULL, argument zSql points to a buffer allocated using
206640** sqlite3_malloc containing an SQL statement. This function prepares the SQL
206641** statement against database db and frees the buffer. If statement
206642** compilation is successful, *ppStmt is set to point to the new statement
206643** handle and SQLITE_OK is returned.
206644**
206645** Otherwise, if an error occurs, *ppStmt is set to NULL and an error code
206646** returned. In this case, *pzErrmsg may also be set to point to an error
206647** message. It is the responsibility of the caller to free this error message
206648** buffer using sqlite3_free().
206649**
206650** If argument zSql is NULL, this function assumes that an OOM has occurred.
206651** In this case SQLITE_NOMEM is returned and *ppStmt set to NULL.
206652*/
206653static int prepareFreeAndCollectError(
206654 sqlite3 *db,
206655 sqlite3_stmt **ppStmt,
206656 char **pzErrmsg,
206657 char *zSql
206658){
206659 int rc;
206660 assert( *pzErrmsg==0 )((void) (0));
206661 if( zSql==0 ){
206662 rc = SQLITE_NOMEM7;
206663 *ppStmt = 0;
206664 }else{
206665 rc = prepareAndCollectError(db, ppStmt, pzErrmsg, zSql);
206666 sqlite3_free(zSql);
206667 }
206668 return rc;
206669}
206670
206671/*
206672** Free the RbuObjIter.azTblCol[] and RbuObjIter.abTblPk[] arrays allocated
206673** by an earlier call to rbuObjIterCacheTableInfo().
206674*/
206675static void rbuObjIterFreeCols(RbuObjIter *pIter){
206676 int i;
206677 for(i=0; i<pIter->nTblCol; i++){
206678 sqlite3_free(pIter->azTblCol[i]);
206679 sqlite3_free(pIter->azTblType[i]);
206680 }
206681 sqlite3_free(pIter->azTblCol);
206682 pIter->azTblCol = 0;
206683 pIter->azTblType = 0;
206684 pIter->aiSrcOrder = 0;
206685 pIter->abTblPk = 0;
206686 pIter->abNotNull = 0;
206687 pIter->nTblCol = 0;
206688 pIter->eType = 0; /* Invalid value */
206689}
206690
206691/*
206692** Finalize all statements and free all allocations that are specific to
206693** the current object (table/index pair).
206694*/
206695static void rbuObjIterClearStatements(RbuObjIter *pIter){
206696 RbuUpdateStmt *pUp;
206697
206698 sqlite3_finalize(pIter->pSelect);
206699 sqlite3_finalize(pIter->pInsert);
206700 sqlite3_finalize(pIter->pDelete);
206701 sqlite3_finalize(pIter->pTmpInsert);
206702 pUp = pIter->pRbuUpdate;
206703 while( pUp ){
206704 RbuUpdateStmt *pTmp = pUp->pNext;
206705 sqlite3_finalize(pUp->pUpdate);
206706 sqlite3_free(pUp);
206707 pUp = pTmp;
206708 }
206709 sqlite3_free(pIter->aIdxCol);
206710 sqlite3_free(pIter->zIdxSql);
206711
206712 pIter->pSelect = 0;
206713 pIter->pInsert = 0;
206714 pIter->pDelete = 0;
206715 pIter->pRbuUpdate = 0;
206716 pIter->pTmpInsert = 0;
206717 pIter->nCol = 0;
206718 pIter->nIdxCol = 0;
206719 pIter->aIdxCol = 0;
206720 pIter->zIdxSql = 0;
206721}
206722
206723/*
206724** Clean up any resources allocated as part of the iterator object passed
206725** as the only argument.
206726*/
206727static void rbuObjIterFinalize(RbuObjIter *pIter){
206728 rbuObjIterClearStatements(pIter);
206729 sqlite3_finalize(pIter->pTblIter);
206730 sqlite3_finalize(pIter->pIdxIter);
206731 rbuObjIterFreeCols(pIter);
206732 memset(pIter, 0, sizeof(RbuObjIter));
206733}
206734
206735/*
206736** Advance the iterator to the next position.
206737**
206738** If no error occurs, SQLITE_OK is returned and the iterator is left
206739** pointing to the next entry. Otherwise, an error code and message is
206740** left in the RBU handle passed as the first argument. A copy of the
206741** error code is returned.
206742*/
206743static int rbuObjIterNext(sqlite3rbu *p, RbuObjIter *pIter){
206744 int rc = p->rc;
206745 if( rc==SQLITE_OK0 ){
206746
206747 /* Free any SQLite statements used while processing the previous object */
206748 rbuObjIterClearStatements(pIter);
206749 if( pIter->zIdx==0 ){
206750 rc = sqlite3_exec(p->dbMain,
206751 "DROP TRIGGER IF EXISTS temp.rbu_insert_tr;"
206752 "DROP TRIGGER IF EXISTS temp.rbu_update1_tr;"
206753 "DROP TRIGGER IF EXISTS temp.rbu_update2_tr;"
206754 "DROP TRIGGER IF EXISTS temp.rbu_delete_tr;"
206755 , 0, 0, &p->zErrmsg
206756 );
206757 }
206758
206759 if( rc==SQLITE_OK0 ){
206760 if( pIter->bCleanup ){
206761 rbuObjIterFreeCols(pIter);
206762 pIter->bCleanup = 0;
206763 rc = sqlite3_step(pIter->pTblIter);
206764 if( rc!=SQLITE_ROW100 ){
206765 rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
206766 pIter->zTbl = 0;
206767 }else{
206768 pIter->zTbl = (const char*)sqlite3_column_text(pIter->pTblIter, 0);
206769 pIter->zDataTbl = (const char*)sqlite3_column_text(pIter->pTblIter,1);
206770 rc = (pIter->zDataTbl && pIter->zTbl) ? SQLITE_OK0 : SQLITE_NOMEM7;
206771 }
206772 }else{
206773 if( pIter->zIdx==0 ){
206774 sqlite3_stmt *pIdx = pIter->pIdxIter;
206775 rc = sqlite3_bind_text(pIdx, 1, pIter->zTbl, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
206776 }
206777 if( rc==SQLITE_OK0 ){
206778 rc = sqlite3_step(pIter->pIdxIter);
206779 if( rc!=SQLITE_ROW100 ){
206780 rc = resetAndCollectError(pIter->pIdxIter, &p->zErrmsg);
206781 pIter->bCleanup = 1;
206782 pIter->zIdx = 0;
206783 }else{
206784 pIter->zIdx = (const char*)sqlite3_column_text(pIter->pIdxIter, 0);
206785 pIter->iTnum = sqlite3_column_int(pIter->pIdxIter, 1);
206786 pIter->bUnique = sqlite3_column_int(pIter->pIdxIter, 2);
206787 rc = pIter->zIdx ? SQLITE_OK0 : SQLITE_NOMEM7;
206788 }
206789 }
206790 }
206791 }
206792 }
206793
206794 if( rc!=SQLITE_OK0 ){
206795 rbuObjIterFinalize(pIter);
206796 p->rc = rc;
206797 }
206798 return rc;
206799}
206800
206801
206802/*
206803** The implementation of the rbu_target_name() SQL function. This function
206804** accepts one or two arguments. The first argument is the name of a table -
206805** the name of a table in the RBU database. The second, if it is present, is 1
206806** for a view or 0 for a table.
206807**
206808** For a non-vacuum RBU handle, if the table name matches the pattern:
206809**
206810** data[0-9]_<name>
206811**
206812** where <name> is any sequence of 1 or more characters, <name> is returned.
206813** Otherwise, if the only argument does not match the above pattern, an SQL
206814** NULL is returned.
206815**
206816** "data_t1" -> "t1"
206817** "data0123_t2" -> "t2"
206818** "dataAB_t3" -> NULL
206819**
206820** For an rbu vacuum handle, a copy of the first argument is returned if
206821** the second argument is either missing or 0 (not a view).
206822*/
206823static void rbuTargetNameFunc(
206824 sqlite3_context *pCtx,
206825 int argc,
206826 sqlite3_value **argv
206827){
206828 sqlite3rbu *p = sqlite3_user_data(pCtx);
206829 const char *zIn;
206830 assert( argc==1 || argc==2 )((void) (0));
206831
206832 zIn = (const char*)sqlite3_value_text(argv[0]);
206833 if( zIn ){
206834 if( rbuIsVacuum(p) ){
206835 assert( argc==2 || argc==1 )((void) (0));
206836 if( argc==1 || 0==sqlite3_value_int(argv[1]) ){
206837 sqlite3_result_text(pCtx, zIn, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
206838 }
206839 }else{
206840 if( strlen(zIn)>4 && memcmp("data", zIn, 4)==0 ){
206841 int i;
206842 for(i=4; zIn[i]>='0' && zIn[i]<='9'; i++);
206843 if( zIn[i]=='_' && zIn[i+1] ){
206844 sqlite3_result_text(pCtx, &zIn[i+1], -1, SQLITE_STATIC((sqlite3_destructor_type)0));
206845 }
206846 }
206847 }
206848 }
206849}
206850
206851/*
206852** Initialize the iterator structure passed as the second argument.
206853**
206854** If no error occurs, SQLITE_OK is returned and the iterator is left
206855** pointing to the first entry. Otherwise, an error code and message is
206856** left in the RBU handle passed as the first argument. A copy of the
206857** error code is returned.
206858*/
206859static int rbuObjIterFirst(sqlite3rbu *p, RbuObjIter *pIter){
206860 int rc;
206861 memset(pIter, 0, sizeof(RbuObjIter));
206862
206863 rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pTblIter, &p->zErrmsg,
206864 sqlite3_mprintf(
206865 "SELECT rbu_target_name(name, type='view') AS target, name "
206866 "FROM sqlite_schema "
206867 "WHERE type IN ('table', 'view') AND target IS NOT NULL "
206868 " %s "
206869 "ORDER BY name"
206870 , rbuIsVacuum(p) ? "AND rootpage!=0 AND rootpage IS NOT NULL" : ""));
206871
206872 if( rc==SQLITE_OK0 ){
206873 rc = prepareAndCollectError(p->dbMain, &pIter->pIdxIter, &p->zErrmsg,
206874 "SELECT name, rootpage, sql IS NULL OR substr(8, 6)=='UNIQUE' "
206875 " FROM main.sqlite_schema "
206876 " WHERE type='index' AND tbl_name = ?"
206877 );
206878 }
206879
206880 pIter->bCleanup = 1;
206881 p->rc = rc;
206882 return rbuObjIterNext(p, pIter);
206883}
206884
206885/*
206886** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
206887** an error code is stored in the RBU handle passed as the first argument.
206888**
206889** If an error has already occurred (p->rc is already set to something other
206890** than SQLITE_OK), then this function returns NULL without modifying the
206891** stored error code. In this case it still calls sqlite3_free() on any
206892** printf() parameters associated with %z conversions.
206893*/
206894static char *rbuMPrintf(sqlite3rbu *p, const char *zFmt, ...){
206895 char *zSql = 0;
206896 va_list ap;
206897 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
206898 zSql = sqlite3_vmprintf(zFmt, ap);
206899 if( p->rc==SQLITE_OK0 ){
206900 if( zSql==0 ) p->rc = SQLITE_NOMEM7;
206901 }else{
206902 sqlite3_free(zSql);
206903 zSql = 0;
206904 }
206905 va_end(ap)__builtin_va_end(ap);
206906 return zSql;
206907}
206908
206909/*
206910** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
206911** arguments are the usual subsitution values. This function performs
206912** the printf() style substitutions and executes the result as an SQL
206913** statement on the RBU handles database.
206914**
206915** If an error occurs, an error code and error message is stored in the
206916** RBU handle. If an error has already occurred when this function is
206917** called, it is a no-op.
206918*/
206919static int rbuMPrintfExec(sqlite3rbu *p, sqlite3 *db, const char *zFmt, ...){
206920 va_list ap;
206921 char *zSql;
206922 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
206923 zSql = sqlite3_vmprintf(zFmt, ap);
206924 if( p->rc==SQLITE_OK0 ){
206925 if( zSql==0 ){
206926 p->rc = SQLITE_NOMEM7;
206927 }else{
206928 p->rc = sqlite3_exec(db, zSql, 0, 0, &p->zErrmsg);
206929 }
206930 }
206931 sqlite3_free(zSql);
206932 va_end(ap)__builtin_va_end(ap);
206933 return p->rc;
206934}
206935
206936/*
206937** Attempt to allocate and return a pointer to a zeroed block of nByte
206938** bytes.
206939**
206940** If an error (i.e. an OOM condition) occurs, return NULL and leave an
206941** error code in the rbu handle passed as the first argument. Or, if an
206942** error has already occurred when this function is called, return NULL
206943** immediately without attempting the allocation or modifying the stored
206944** error code.
206945*/
206946static void *rbuMalloc(sqlite3rbu *p, sqlite3_int64 nByte){
206947 void *pRet = 0;
206948 if( p->rc==SQLITE_OK0 ){
206949 assert( nByte>0 )((void) (0));
206950 pRet = sqlite3_malloc64(nByte);
206951 if( pRet==0 ){
206952 p->rc = SQLITE_NOMEM7;
206953 }else{
206954 memset(pRet, 0, nByte);
206955 }
206956 }
206957 return pRet;
206958}
206959
206960
206961/*
206962** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
206963** there is room for at least nCol elements. If an OOM occurs, store an
206964** error code in the RBU handle passed as the first argument.
206965*/
206966static void rbuAllocateIterArrays(sqlite3rbu *p, RbuObjIter *pIter, int nCol){
206967 sqlite3_int64 nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol;
206968 char **azNew;
206969
206970 azNew = (char**)rbuMalloc(p, nByte);
206971 if( azNew ){
206972 pIter->azTblCol = azNew;
206973 pIter->azTblType = &azNew[nCol];
206974 pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
206975 pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
206976 pIter->abNotNull = (u8*)&pIter->abTblPk[nCol];
206977 pIter->abIndexed = (u8*)&pIter->abNotNull[nCol];
206978 }
206979}
206980
206981/*
206982** The first argument must be a nul-terminated string. This function
206983** returns a copy of the string in memory obtained from sqlite3_malloc().
206984** It is the responsibility of the caller to eventually free this memory
206985** using sqlite3_free().
206986**
206987** If an OOM condition is encountered when attempting to allocate memory,
206988** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
206989** if the allocation succeeds, (*pRc) is left unchanged.
206990*/
206991static char *rbuStrndup(const char *zStr, int *pRc){
206992 char *zRet = 0;
206993
206994 if( *pRc==SQLITE_OK0 ){
206995 if( zStr ){
206996 size_t nCopy = strlen(zStr) + 1;
206997 zRet = (char*)sqlite3_malloc64(nCopy);
206998 if( zRet ){
206999 memcpy(zRet, zStr, nCopy);
207000 }else{
207001 *pRc = SQLITE_NOMEM7;
207002 }
207003 }
207004 }
207005
207006 return zRet;
207007}
207008
207009/*
207010** Finalize the statement passed as the second argument.
207011**
207012** If the sqlite3_finalize() call indicates that an error occurs, and the
207013** rbu handle error code is not already set, set the error code and error
207014** message accordingly.
207015*/
207016static void rbuFinalize(sqlite3rbu *p, sqlite3_stmt *pStmt){
207017 sqlite3 *db = sqlite3_db_handle(pStmt);
207018 int rc = sqlite3_finalize(pStmt);
207019 if( p->rc==SQLITE_OK0 && rc!=SQLITE_OK0 ){
207020 p->rc = rc;
207021 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
207022 }
207023}
207024
207025/* Determine the type of a table.
207026**
207027** peType is of type (int*), a pointer to an output parameter of type
207028** (int). This call sets the output parameter as follows, depending
207029** on the type of the table specified by parameters dbName and zTbl.
207030**
207031** RBU_PK_NOTABLE: No such table.
207032** RBU_PK_NONE: Table has an implicit rowid.
207033** RBU_PK_IPK: Table has an explicit IPK column.
207034** RBU_PK_EXTERNAL: Table has an external PK index.
207035** RBU_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
207036** RBU_PK_VTAB: Table is a virtual table.
207037**
207038** Argument *piPk is also of type (int*), and also points to an output
207039** parameter. Unless the table has an external primary key index
207040** (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
207041** if the table does have an external primary key index, then *piPk
207042** is set to the root page number of the primary key index before
207043** returning.
207044**
207045** ALGORITHM:
207046**
207047** if( no entry exists in sqlite_schema ){
207048** return RBU_PK_NOTABLE
207049** }else if( sql for the entry starts with "CREATE VIRTUAL" ){
207050** return RBU_PK_VTAB
207051** }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
207052** if( the index that is the pk exists in sqlite_schema ){
207053** *piPK = rootpage of that index.
207054** return RBU_PK_EXTERNAL
207055** }else{
207056** return RBU_PK_WITHOUT_ROWID
207057** }
207058** }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
207059** return RBU_PK_IPK
207060** }else{
207061** return RBU_PK_NONE
207062** }
207063*/
207064static void rbuTableType(
207065 sqlite3rbu *p,
207066 const char *zTab,
207067 int *peType,
207068 int *piTnum,
207069 int *piPk
207070){
207071 /*
207072 ** 0) SELECT count(*) FROM sqlite_schema where name=%Q AND IsVirtual(%Q)
207073 ** 1) PRAGMA index_list = ?
207074 ** 2) SELECT count(*) FROM sqlite_schema where name=%Q
207075 ** 3) PRAGMA table_info = ?
207076 */
207077 sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
207078
207079 *peType = RBU_PK_NOTABLE;
207080 *piPk = 0;
207081
207082 assert( p->rc==SQLITE_OK )((void) (0));
207083 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg,
207084 sqlite3_mprintf(
207085 "SELECT "
207086 " (sql COLLATE nocase BETWEEN 'CREATE VIRTUAL' AND 'CREATE VIRTUAM'),"
207087 " rootpage"
207088 " FROM sqlite_schema"
207089 " WHERE name=%Q", zTab
207090 ));
207091 if( p->rc!=SQLITE_OK0 || sqlite3_step(aStmt[0])!=SQLITE_ROW100 ){
207092 /* Either an error, or no such table. */
207093 goto rbuTableType_end;
207094 }
207095 if( sqlite3_column_int(aStmt[0], 0) ){
207096 *peType = RBU_PK_VTAB; /* virtual table */
207097 goto rbuTableType_end;
207098 }
207099 *piTnum = sqlite3_column_int(aStmt[0], 1);
207100
207101 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg,
207102 sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
207103 );
207104 if( p->rc ) goto rbuTableType_end;
207105 while( sqlite3_step(aStmt[1])==SQLITE_ROW100 ){
207106 const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
207107 const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
207108 if( zOrig && zIdx && zOrig[0]=='p' ){
207109 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg,
207110 sqlite3_mprintf(
207111 "SELECT rootpage FROM sqlite_schema WHERE name = %Q", zIdx
207112 ));
207113 if( p->rc==SQLITE_OK0 ){
207114 if( sqlite3_step(aStmt[2])==SQLITE_ROW100 ){
207115 *piPk = sqlite3_column_int(aStmt[2], 0);
207116 *peType = RBU_PK_EXTERNAL;
207117 }else{
207118 *peType = RBU_PK_WITHOUT_ROWID;
207119 }
207120 }
207121 goto rbuTableType_end;
207122 }
207123 }
207124
207125 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg,
207126 sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
207127 );
207128 if( p->rc==SQLITE_OK0 ){
207129 while( sqlite3_step(aStmt[3])==SQLITE_ROW100 ){
207130 if( sqlite3_column_int(aStmt[3],5)>0 ){
207131 *peType = RBU_PK_IPK; /* explicit IPK column */
207132 goto rbuTableType_end;
207133 }
207134 }
207135 *peType = RBU_PK_NONE;
207136 }
207137
207138rbuTableType_end: {
207139 unsigned int i;
207140 for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
207141 rbuFinalize(p, aStmt[i]);
207142 }
207143 }
207144}
207145
207146/*
207147** This is a helper function for rbuObjIterCacheTableInfo(). It populates
207148** the pIter->abIndexed[] array.
207149*/
207150static void rbuObjIterCacheIndexedCols(sqlite3rbu *p, RbuObjIter *pIter){
207151 sqlite3_stmt *pList = 0;
207152 int bIndex = 0;
207153
207154 if( p->rc==SQLITE_OK0 ){
207155 memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
207156 p->rc = prepareFreeAndCollectError(p->dbMain, &pList, &p->zErrmsg,
207157 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
207158 );
207159 }
207160
207161 pIter->nIndex = 0;
207162 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pList) ){
207163 const char *zIdx = (const char*)sqlite3_column_text(pList, 1);
207164 int bPartial = sqlite3_column_int(pList, 4);
207165 sqlite3_stmt *pXInfo = 0;
207166 if( zIdx==0 ) break;
207167 if( bPartial ){
207168 memset(pIter->abIndexed, 0x01, sizeof(u8)*pIter->nTblCol);
207169 }
207170 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
207171 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
207172 );
207173 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pXInfo) ){
207174 int iCid = sqlite3_column_int(pXInfo, 1);
207175 if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
207176 if( iCid==-2 ){
207177 memset(pIter->abIndexed, 0x01, sizeof(u8)*pIter->nTblCol);
207178 }
207179 }
207180 rbuFinalize(p, pXInfo);
207181 bIndex = 1;
207182 pIter->nIndex++;
207183 }
207184
207185 if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
207186 /* "PRAGMA index_list" includes the main PK b-tree */
207187 pIter->nIndex--;
207188 }
207189
207190 rbuFinalize(p, pList);
207191 if( bIndex==0 ) pIter->abIndexed = 0;
207192}
207193
207194
207195/*
207196** If they are not already populated, populate the pIter->azTblCol[],
207197** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
207198** the table (not index) that the iterator currently points to.
207199**
207200** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
207201** an error does occur, an error code and error message are also left in
207202** the RBU handle.
207203*/
207204static int rbuObjIterCacheTableInfo(sqlite3rbu *p, RbuObjIter *pIter){
207205 if( pIter->azTblCol==0 ){
207206 sqlite3_stmt *pStmt = 0;
207207 int nCol = 0;
207208 int i; /* for() loop iterator variable */
207209 int bRbuRowid = 0; /* If input table has column "rbu_rowid" */
207210 int iOrder = 0;
207211 int iTnum = 0;
207212
207213 /* Figure out the type of table this step will deal with. */
207214 assert( pIter->eType==0 )((void) (0));
207215 rbuTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
207216 if( p->rc==SQLITE_OK0 && pIter->eType==RBU_PK_NOTABLE ){
207217 p->rc = SQLITE_ERROR1;
207218 p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
207219 }
207220 if( p->rc ) return p->rc;
207221 if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
207222
207223 assert( pIter->eType==RBU_PK_NONE || pIter->eType==RBU_PK_IPK((void) (0))
207224 || pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_WITHOUT_ROWID((void) (0))
207225 || pIter->eType==RBU_PK_VTAB((void) (0))
207226 )((void) (0));
207227
207228 /* Populate the azTblCol[] and nTblCol variables based on the columns
207229 ** of the input table. Ignore any input table columns that begin with
207230 ** "rbu_". */
207231 p->rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
207232 sqlite3_mprintf("SELECT * FROM '%q'", pIter->zDataTbl)
207233 );
207234 if( p->rc==SQLITE_OK0 ){
207235 nCol = sqlite3_column_count(pStmt);
207236 rbuAllocateIterArrays(p, pIter, nCol);
207237 }
207238 for(i=0; p->rc==SQLITE_OK0 && i<nCol; i++){
207239 const char *zName = (const char*)sqlite3_column_name(pStmt, i);
207240 if( sqlite3_strnicmp("rbu_", zName, 4) ){
207241 char *zCopy = rbuStrndup(zName, &p->rc);
207242 pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
207243 pIter->azTblCol[pIter->nTblCol++] = zCopy;
207244 }
207245 else if( 0==sqlite3_stricmp("rbu_rowid", zName) ){
207246 bRbuRowid = 1;
207247 }
207248 }
207249 sqlite3_finalize(pStmt);
207250 pStmt = 0;
207251
207252 if( p->rc==SQLITE_OK0
207253 && rbuIsVacuum(p)==0
207254 && bRbuRowid!=(pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
207255 ){
207256 p->rc = SQLITE_ERROR1;
207257 p->zErrmsg = sqlite3_mprintf(
207258 "table %q %s rbu_rowid column", pIter->zDataTbl,
207259 (bRbuRowid ? "may not have" : "requires")
207260 );
207261 }
207262
207263 /* Check that all non-HIDDEN columns in the destination table are also
207264 ** present in the input table. Populate the abTblPk[], azTblType[] and
207265 ** aiTblOrder[] arrays at the same time. */
207266 if( p->rc==SQLITE_OK0 ){
207267 p->rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
207268 sqlite3_mprintf("PRAGMA table_info(%Q)", pIter->zTbl)
207269 );
207270 }
207271 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pStmt) ){
207272 const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
207273 if( zName==0 ) break; /* An OOM - finalize() below returns S_NOMEM */
207274 for(i=iOrder; i<pIter->nTblCol; i++){
207275 if( 0==strcmp(zName, pIter->azTblCol[i]) ) break;
207276 }
207277 if( i==pIter->nTblCol ){
207278 p->rc = SQLITE_ERROR1;
207279 p->zErrmsg = sqlite3_mprintf("column missing from %q: %s",
207280 pIter->zDataTbl, zName
207281 );
207282 }else{
207283 int iPk = sqlite3_column_int(pStmt, 5);
207284 int bNotNull = sqlite3_column_int(pStmt, 3);
207285 const char *zType = (const char*)sqlite3_column_text(pStmt, 2);
207286
207287 if( i!=iOrder ){
207288 SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]){int t=pIter->aiSrcOrder[i]; pIter->aiSrcOrder[i]=pIter
->aiSrcOrder[iOrder]; pIter->aiSrcOrder[iOrder]=t;}
;
207289 SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]){char* t=pIter->azTblCol[i]; pIter->azTblCol[i]=pIter->
azTblCol[iOrder]; pIter->azTblCol[iOrder]=t;}
;
207290 }
207291
207292 pIter->azTblType[iOrder] = rbuStrndup(zType, &p->rc);
207293 assert( iPk>=0 )((void) (0));
207294 pIter->abTblPk[iOrder] = (u8)iPk;
207295 pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
207296 iOrder++;
207297 }
207298 }
207299
207300 rbuFinalize(p, pStmt);
207301 rbuObjIterCacheIndexedCols(p, pIter);
207302 assert( pIter->eType!=RBU_PK_VTAB || pIter->abIndexed==0 )((void) (0));
207303 assert( pIter->eType!=RBU_PK_VTAB || pIter->nIndex==0 )((void) (0));
207304 }
207305
207306 return p->rc;
207307}
207308
207309/*
207310** This function constructs and returns a pointer to a nul-terminated
207311** string containing some SQL clause or list based on one or more of the
207312** column names currently stored in the pIter->azTblCol[] array.
207313*/
207314static char *rbuObjIterGetCollist(
207315 sqlite3rbu *p, /* RBU object */
207316 RbuObjIter *pIter /* Object iterator for column names */
207317){
207318 char *zList = 0;
207319 const char *zSep = "";
207320 int i;
207321 for(i=0; i<pIter->nTblCol; i++){
207322 const char *z = pIter->azTblCol[i];
207323 zList = rbuMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
207324 zSep = ", ";
207325 }
207326 return zList;
207327}
207328
207329/*
207330** Return a comma separated list of the quoted PRIMARY KEY column names,
207331** in order, for the current table. Before each column name, add the text
207332** zPre. After each column name, add the zPost text. Use zSeparator as
207333** the separator text (usually ", ").
207334*/
207335static char *rbuObjIterGetPkList(
207336 sqlite3rbu *p, /* RBU object */
207337 RbuObjIter *pIter, /* Object iterator for column names */
207338 const char *zPre, /* Before each quoted column name */
207339 const char *zSeparator, /* Separator to use between columns */
207340 const char *zPost /* After each quoted column name */
207341){
207342 int iPk = 1;
207343 char *zRet = 0;
207344 const char *zSep = "";
207345 while( 1 ){
207346 int i;
207347 for(i=0; i<pIter->nTblCol; i++){
207348 if( (int)pIter->abTblPk[i]==iPk ){
207349 const char *zCol = pIter->azTblCol[i];
207350 zRet = rbuMPrintf(p, "%z%s%s\"%w\"%s", zRet, zSep, zPre, zCol, zPost);
207351 zSep = zSeparator;
207352 break;
207353 }
207354 }
207355 if( i==pIter->nTblCol ) break;
207356 iPk++;
207357 }
207358 return zRet;
207359}
207360
207361/*
207362** This function is called as part of restarting an RBU vacuum within
207363** stage 1 of the process (while the *-oal file is being built) while
207364** updating a table (not an index). The table may be a rowid table or
207365** a WITHOUT ROWID table. It queries the target database to find the
207366** largest key that has already been written to the target table and
207367** constructs a WHERE clause that can be used to extract the remaining
207368** rows from the source table. For a rowid table, the WHERE clause
207369** is of the form:
207370**
207371** "WHERE _rowid_ > ?"
207372**
207373** and for WITHOUT ROWID tables:
207374**
207375** "WHERE (key1, key2) > (?, ?)"
207376**
207377** Instead of "?" placeholders, the actual WHERE clauses created by
207378** this function contain literal SQL values.
207379*/
207380static char *rbuVacuumTableStart(
207381 sqlite3rbu *p, /* RBU handle */
207382 RbuObjIter *pIter, /* RBU iterator object */
207383 int bRowid, /* True for a rowid table */
207384 const char *zWrite /* Target table name prefix */
207385){
207386 sqlite3_stmt *pMax = 0;
207387 char *zRet = 0;
207388 if( bRowid ){
207389 p->rc = prepareFreeAndCollectError(p->dbMain, &pMax, &p->zErrmsg,
207390 sqlite3_mprintf(
207391 "SELECT max(_rowid_) FROM \"%s%w\"", zWrite, pIter->zTbl
207392 )
207393 );
207394 if( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pMax) ){
207395 sqlite3_int64 iMax = sqlite3_column_int64(pMax, 0);
207396 zRet = rbuMPrintf(p, " WHERE _rowid_ > %lld ", iMax);
207397 }
207398 rbuFinalize(p, pMax);
207399 }else{
207400 char *zOrder = rbuObjIterGetPkList(p, pIter, "", ", ", " DESC");
207401 char *zSelect = rbuObjIterGetPkList(p, pIter, "quote(", "||','||", ")");
207402 char *zList = rbuObjIterGetPkList(p, pIter, "", ", ", "");
207403
207404 if( p->rc==SQLITE_OK0 ){
207405 p->rc = prepareFreeAndCollectError(p->dbMain, &pMax, &p->zErrmsg,
207406 sqlite3_mprintf(
207407 "SELECT %s FROM \"%s%w\" ORDER BY %s LIMIT 1",
207408 zSelect, zWrite, pIter->zTbl, zOrder
207409 )
207410 );
207411 if( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pMax) ){
207412 const char *zVal = (const char*)sqlite3_column_text(pMax, 0);
207413 zRet = rbuMPrintf(p, " WHERE (%s) > (%s) ", zList, zVal);
207414 }
207415 rbuFinalize(p, pMax);
207416 }
207417
207418 sqlite3_free(zOrder);
207419 sqlite3_free(zSelect);
207420 sqlite3_free(zList);
207421 }
207422 return zRet;
207423}
207424
207425/*
207426** This function is called as part of restating an RBU vacuum when the
207427** current operation is writing content to an index. If possible, it
207428** queries the target index b-tree for the largest key already written to
207429** it, then composes and returns an expression that can be used in a WHERE
207430** clause to select the remaining required rows from the source table.
207431** It is only possible to return such an expression if:
207432**
207433** * The index contains no DESC columns, and
207434** * The last key written to the index before the operation was
207435** suspended does not contain any NULL values.
207436**
207437** The expression is of the form:
207438**
207439** (index-field1, index-field2, ...) > (?, ?, ...)
207440**
207441** except that the "?" placeholders are replaced with literal values.
207442**
207443** If the expression cannot be created, NULL is returned. In this case,
207444** the caller has to use an OFFSET clause to extract only the required
207445** rows from the sourct table, just as it does for an RBU update operation.
207446*/
207447static char *rbuVacuumIndexStart(
207448 sqlite3rbu *p, /* RBU handle */
207449 RbuObjIter *pIter /* RBU iterator object */
207450){
207451 char *zOrder = 0;
207452 char *zLhs = 0;
207453 char *zSelect = 0;
207454 char *zVector = 0;
207455 char *zRet = 0;
207456 int bFailed = 0;
207457 const char *zSep = "";
207458 int iCol = 0;
207459 sqlite3_stmt *pXInfo = 0;
207460
207461 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
207462 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
207463 );
207464 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pXInfo) ){
207465 int iCid = sqlite3_column_int(pXInfo, 1);
207466 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
207467 const char *zCol;
207468 if( sqlite3_column_int(pXInfo, 3) ){
207469 bFailed = 1;
207470 break;
207471 }
207472
207473 if( iCid<0 ){
207474 if( pIter->eType==RBU_PK_IPK ){
207475 int i;
207476 for(i=0; pIter->abTblPk[i]==0; i++);
207477 assert( i<pIter->nTblCol )((void) (0));
207478 zCol = pIter->azTblCol[i];
207479 }else{
207480 zCol = "_rowid_";
207481 }
207482 }else{
207483 zCol = pIter->azTblCol[iCid];
207484 }
207485
207486 zLhs = rbuMPrintf(p, "%z%s \"%w\" COLLATE %Q",
207487 zLhs, zSep, zCol, zCollate
207488 );
207489 zOrder = rbuMPrintf(p, "%z%s \"rbu_imp_%d%w\" COLLATE %Q DESC",
207490 zOrder, zSep, iCol, zCol, zCollate
207491 );
207492 zSelect = rbuMPrintf(p, "%z%s quote(\"rbu_imp_%d%w\")",
207493 zSelect, zSep, iCol, zCol
207494 );
207495 zSep = ", ";
207496 iCol++;
207497 }
207498 rbuFinalize(p, pXInfo);
207499 if( bFailed ) goto index_start_out;
207500
207501 if( p->rc==SQLITE_OK0 ){
207502 sqlite3_stmt *pSel = 0;
207503
207504 p->rc = prepareFreeAndCollectError(p->dbMain, &pSel, &p->zErrmsg,
207505 sqlite3_mprintf("SELECT %s FROM \"rbu_imp_%w\" ORDER BY %s LIMIT 1",
207506 zSelect, pIter->zTbl, zOrder
207507 )
207508 );
207509 if( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pSel) ){
207510 zSep = "";
207511 for(iCol=0; iCol<pIter->nCol; iCol++){
207512 const char *zQuoted = (const char*)sqlite3_column_text(pSel, iCol);
207513 if( zQuoted==0 ){
207514 p->rc = SQLITE_NOMEM7;
207515 }else if( zQuoted[0]=='N' ){
207516 bFailed = 1;
207517 break;
207518 }
207519 zVector = rbuMPrintf(p, "%z%s%s", zVector, zSep, zQuoted);
207520 zSep = ", ";
207521 }
207522
207523 if( !bFailed ){
207524 zRet = rbuMPrintf(p, "(%s) > (%s)", zLhs, zVector);
207525 }
207526 }
207527 rbuFinalize(p, pSel);
207528 }
207529
207530 index_start_out:
207531 sqlite3_free(zOrder);
207532 sqlite3_free(zSelect);
207533 sqlite3_free(zVector);
207534 sqlite3_free(zLhs);
207535 return zRet;
207536}
207537
207538/*
207539** This function is used to create a SELECT list (the list of SQL
207540** expressions that follows a SELECT keyword) for a SELECT statement
207541** used to read from an data_xxx or rbu_tmp_xxx table while updating the
207542** index object currently indicated by the iterator object passed as the
207543** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
207544** to obtain the required information.
207545**
207546** If the index is of the following form:
207547**
207548** CREATE INDEX i1 ON t1(c, b COLLATE nocase);
207549**
207550** and "t1" is a table with an explicit INTEGER PRIMARY KEY column
207551** "ipk", the returned string is:
207552**
207553** "`c` COLLATE 'BINARY', `b` COLLATE 'NOCASE', `ipk` COLLATE 'BINARY'"
207554**
207555** As well as the returned string, three other malloc'd strings are
207556** returned via output parameters. As follows:
207557**
207558** pzImposterCols: ...
207559** pzImposterPk: ...
207560** pzWhere: ...
207561*/
207562static char *rbuObjIterGetIndexCols(
207563 sqlite3rbu *p, /* RBU object */
207564 RbuObjIter *pIter, /* Object iterator for column names */
207565 char **pzImposterCols, /* OUT: Columns for imposter table */
207566 char **pzImposterPk, /* OUT: Imposter PK clause */
207567 char **pzWhere, /* OUT: WHERE clause */
207568 int *pnBind /* OUT: Trbul number of columns */
207569){
207570 int rc = p->rc; /* Error code */
207571 int rc2; /* sqlite3_finalize() return code */
207572 char *zRet = 0; /* String to return */
207573 char *zImpCols = 0; /* String to return via *pzImposterCols */
207574 char *zImpPK = 0; /* String to return via *pzImposterPK */
207575 char *zWhere = 0; /* String to return via *pzWhere */
207576 int nBind = 0; /* Value to return via *pnBind */
207577 const char *zCom = ""; /* Set to ", " later on */
207578 const char *zAnd = ""; /* Set to " AND " later on */
207579 sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = ? */
207580
207581 if( rc==SQLITE_OK0 ){
207582 assert( p->zErrmsg==0 )((void) (0));
207583 rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
207584 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
207585 );
207586 }
207587
207588 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pXInfo) ){
207589 int iCid = sqlite3_column_int(pXInfo, 1);
207590 int bDesc = sqlite3_column_int(pXInfo, 3);
207591 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
207592 const char *zCol = 0;
207593 const char *zType;
207594
207595 if( iCid==-2 ){
207596 int iSeq = sqlite3_column_int(pXInfo, 0);
207597 zRet = sqlite3_mprintf("%z%s(%.*s) COLLATE %Q", zRet, zCom,
207598 pIter->aIdxCol[iSeq].nSpan, pIter->aIdxCol[iSeq].zSpan, zCollate
207599 );
207600 zType = "";
207601 }else {
207602 if( iCid<0 ){
207603 /* An integer primary key. If the table has an explicit IPK, use
207604 ** its name. Otherwise, use "rbu_rowid". */
207605 if( pIter->eType==RBU_PK_IPK ){
207606 int i;
207607 for(i=0; pIter->abTblPk[i]==0; i++);
207608 assert( i<pIter->nTblCol )((void) (0));
207609 zCol = pIter->azTblCol[i];
207610 }else if( rbuIsVacuum(p) ){
207611 zCol = "_rowid_";
207612 }else{
207613 zCol = "rbu_rowid";
207614 }
207615 zType = "INTEGER";
207616 }else{
207617 zCol = pIter->azTblCol[iCid];
207618 zType = pIter->azTblType[iCid];
207619 }
207620 zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom,zCol,zCollate);
207621 }
207622
207623 if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
207624 const char *zOrder = (bDesc ? " DESC" : "");
207625 zImpPK = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\"%s",
207626 zImpPK, zCom, nBind, zCol, zOrder
207627 );
207628 }
207629 zImpCols = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\" %s COLLATE %Q",
207630 zImpCols, zCom, nBind, zCol, zType, zCollate
207631 );
207632 zWhere = sqlite3_mprintf(
207633 "%z%s\"rbu_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
207634 );
207635 if( zRet==0 || zImpPK==0 || zImpCols==0 || zWhere==0 ) rc = SQLITE_NOMEM7;
207636 zCom = ", ";
207637 zAnd = " AND ";
207638 nBind++;
207639 }
207640
207641 rc2 = sqlite3_finalize(pXInfo);
207642 if( rc==SQLITE_OK0 ) rc = rc2;
207643
207644 if( rc!=SQLITE_OK0 ){
207645 sqlite3_free(zRet);
207646 sqlite3_free(zImpCols);
207647 sqlite3_free(zImpPK);
207648 sqlite3_free(zWhere);
207649 zRet = 0;
207650 zImpCols = 0;
207651 zImpPK = 0;
207652 zWhere = 0;
207653 p->rc = rc;
207654 }
207655
207656 *pzImposterCols = zImpCols;
207657 *pzImposterPk = zImpPK;
207658 *pzWhere = zWhere;
207659 *pnBind = nBind;
207660 return zRet;
207661}
207662
207663/*
207664** Assuming the current table columns are "a", "b" and "c", and the zObj
207665** paramter is passed "old", return a string of the form:
207666**
207667** "old.a, old.b, old.b"
207668**
207669** With the column names escaped.
207670**
207671** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append
207672** the text ", old._rowid_" to the returned value.
207673*/
207674static char *rbuObjIterGetOldlist(
207675 sqlite3rbu *p,
207676 RbuObjIter *pIter,
207677 const char *zObj
207678){
207679 char *zList = 0;
207680 if( p->rc==SQLITE_OK0 && pIter->abIndexed ){
207681 const char *zS = "";
207682 int i;
207683 for(i=0; i<pIter->nTblCol; i++){
207684 if( pIter->abIndexed[i] ){
207685 const char *zCol = pIter->azTblCol[i];
207686 zList = sqlite3_mprintf("%z%s%s.\"%w\"", zList, zS, zObj, zCol);
207687 }else{
207688 zList = sqlite3_mprintf("%z%sNULL", zList, zS);
207689 }
207690 zS = ", ";
207691 if( zList==0 ){
207692 p->rc = SQLITE_NOMEM7;
207693 break;
207694 }
207695 }
207696
207697 /* For a table with implicit rowids, append "old._rowid_" to the list. */
207698 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
207699 zList = rbuMPrintf(p, "%z, %s._rowid_", zList, zObj);
207700 }
207701 }
207702 return zList;
207703}
207704
207705/*
207706** Return an expression that can be used in a WHERE clause to match the
207707** primary key of the current table. For example, if the table is:
207708**
207709** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c));
207710**
207711** Return the string:
207712**
207713** "b = ?1 AND c = ?2"
207714*/
207715static char *rbuObjIterGetWhere(
207716 sqlite3rbu *p,
207717 RbuObjIter *pIter
207718){
207719 char *zList = 0;
207720 if( pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE ){
207721 zList = rbuMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
207722 }else if( pIter->eType==RBU_PK_EXTERNAL ){
207723 const char *zSep = "";
207724 int i;
207725 for(i=0; i<pIter->nTblCol; i++){
207726 if( pIter->abTblPk[i] ){
207727 zList = rbuMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
207728 zSep = " AND ";
207729 }
207730 }
207731 zList = rbuMPrintf(p,
207732 "_rowid_ = (SELECT id FROM rbu_imposter2 WHERE %z)", zList
207733 );
207734
207735 }else{
207736 const char *zSep = "";
207737 int i;
207738 for(i=0; i<pIter->nTblCol; i++){
207739 if( pIter->abTblPk[i] ){
207740 const char *zCol = pIter->azTblCol[i];
207741 zList = rbuMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
207742 zSep = " AND ";
207743 }
207744 }
207745 }
207746 return zList;
207747}
207748
207749/*
207750** The SELECT statement iterating through the keys for the current object
207751** (p->objiter.pSelect) currently points to a valid row. However, there
207752** is something wrong with the rbu_control value in the rbu_control value
207753** stored in the (p->nCol+1)'th column. Set the error code and error message
207754** of the RBU handle to something reflecting this.
207755*/
207756static void rbuBadControlError(sqlite3rbu *p){
207757 p->rc = SQLITE_ERROR1;
207758 p->zErrmsg = sqlite3_mprintf("invalid rbu_control value");
207759}
207760
207761
207762/*
207763** Return a nul-terminated string containing the comma separated list of
207764** assignments that should be included following the "SET" keyword of
207765** an UPDATE statement used to update the table object that the iterator
207766** passed as the second argument currently points to if the rbu_control
207767** column of the data_xxx table entry is set to zMask.
207768**
207769** The memory for the returned string is obtained from sqlite3_malloc().
207770** It is the responsibility of the caller to eventually free it using
207771** sqlite3_free().
207772**
207773** If an OOM error is encountered when allocating space for the new
207774** string, an error code is left in the rbu handle passed as the first
207775** argument and NULL is returned. Or, if an error has already occurred
207776** when this function is called, NULL is returned immediately, without
207777** attempting the allocation or modifying the stored error code.
207778*/
207779static char *rbuObjIterGetSetlist(
207780 sqlite3rbu *p,
207781 RbuObjIter *pIter,
207782 const char *zMask
207783){
207784 char *zList = 0;
207785 if( p->rc==SQLITE_OK0 ){
207786 int i;
207787
207788 if( (int)strlen(zMask)!=pIter->nTblCol ){
207789 rbuBadControlError(p);
207790 }else{
207791 const char *zSep = "";
207792 for(i=0; i<pIter->nTblCol; i++){
207793 char c = zMask[pIter->aiSrcOrder[i]];
207794 if( c=='x' ){
207795 zList = rbuMPrintf(p, "%z%s\"%w\"=?%d",
207796 zList, zSep, pIter->azTblCol[i], i+1
207797 );
207798 zSep = ", ";
207799 }
207800 else if( c=='d' ){
207801 zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_delta(\"%w\", ?%d)",
207802 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
207803 );
207804 zSep = ", ";
207805 }
207806 else if( c=='f' ){
207807 zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_fossil_delta(\"%w\", ?%d)",
207808 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
207809 );
207810 zSep = ", ";
207811 }
207812 }
207813 }
207814 }
207815 return zList;
207816}
207817
207818/*
207819** Return a nul-terminated string consisting of nByte comma separated
207820** "?" expressions. For example, if nByte is 3, return a pointer to
207821** a buffer containing the string "?,?,?".
207822**
207823** The memory for the returned string is obtained from sqlite3_malloc().
207824** It is the responsibility of the caller to eventually free it using
207825** sqlite3_free().
207826**
207827** If an OOM error is encountered when allocating space for the new
207828** string, an error code is left in the rbu handle passed as the first
207829** argument and NULL is returned. Or, if an error has already occurred
207830** when this function is called, NULL is returned immediately, without
207831** attempting the allocation or modifying the stored error code.
207832*/
207833static char *rbuObjIterGetBindlist(sqlite3rbu *p, int nBind){
207834 char *zRet = 0;
207835 sqlite3_int64 nByte = 2*(sqlite3_int64)nBind + 1;
207836
207837 zRet = (char*)rbuMalloc(p, nByte);
207838 if( zRet ){
207839 int i;
207840 for(i=0; i<nBind; i++){
207841 zRet[i*2] = '?';
207842 zRet[i*2+1] = (i+1==nBind) ? '\0' : ',';
207843 }
207844 }
207845 return zRet;
207846}
207847
207848/*
207849** The iterator currently points to a table (not index) of type
207850** RBU_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
207851** declaration for the corresponding imposter table. For example,
207852** if the iterator points to a table created as:
207853**
207854** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
207855**
207856** this function returns:
207857**
207858** PRIMARY KEY("b", "a" DESC)
207859*/
207860static char *rbuWithoutRowidPK(sqlite3rbu *p, RbuObjIter *pIter){
207861 char *z = 0;
207862 assert( pIter->zIdx==0 )((void) (0));
207863 if( p->rc==SQLITE_OK0 ){
207864 const char *zSep = "PRIMARY KEY(";
207865 sqlite3_stmt *pXList = 0; /* PRAGMA index_list = (pIter->zTbl) */
207866 sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = <pk-index> */
207867
207868 p->rc = prepareFreeAndCollectError(p->dbMain, &pXList, &p->zErrmsg,
207869 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
207870 );
207871 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pXList) ){
207872 const char *zOrig = (const char*)sqlite3_column_text(pXList,3);
207873 if( zOrig && strcmp(zOrig, "pk")==0 ){
207874 const char *zIdx = (const char*)sqlite3_column_text(pXList,1);
207875 if( zIdx ){
207876 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
207877 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
207878 );
207879 }
207880 break;
207881 }
207882 }
207883 rbuFinalize(p, pXList);
207884
207885 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pXInfo) ){
207886 if( sqlite3_column_int(pXInfo, 5) ){
207887 /* int iCid = sqlite3_column_int(pXInfo, 0); */
207888 const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
207889 const char *zDesc = sqlite3_column_int(pXInfo, 3) ? " DESC" : "";
207890 z = rbuMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
207891 zSep = ", ";
207892 }
207893 }
207894 z = rbuMPrintf(p, "%z)", z);
207895 rbuFinalize(p, pXInfo);
207896 }
207897 return z;
207898}
207899
207900/*
207901** This function creates the second imposter table used when writing to
207902** a table b-tree where the table has an external primary key. If the
207903** iterator passed as the second argument does not currently point to
207904** a table (not index) with an external primary key, this function is a
207905** no-op.
207906**
207907** Assuming the iterator does point to a table with an external PK, this
207908** function creates a WITHOUT ROWID imposter table named "rbu_imposter2"
207909** used to access that PK index. For example, if the target table is
207910** declared as follows:
207911**
207912** CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
207913**
207914** then the imposter table schema is:
207915**
207916** CREATE TABLE rbu_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
207917**
207918*/
207919static void rbuCreateImposterTable2(sqlite3rbu *p, RbuObjIter *pIter){
207920 if( p->rc==SQLITE_OK0 && pIter->eType==RBU_PK_EXTERNAL ){
207921 int tnum = pIter->iPkTnum; /* Root page of PK index */
207922 sqlite3_stmt *pQuery = 0; /* SELECT name ... WHERE rootpage = $tnum */
207923 const char *zIdx = 0; /* Name of PK index */
207924 sqlite3_stmt *pXInfo = 0; /* PRAGMA main.index_xinfo = $zIdx */
207925 const char *zComma = "";
207926 char *zCols = 0; /* Used to build up list of table cols */
207927 char *zPk = 0; /* Used to build up table PK declaration */
207928
207929 /* Figure out the name of the primary key index for the current table.
207930 ** This is needed for the argument to "PRAGMA index_xinfo". Set
207931 ** zIdx to point to a nul-terminated string containing this name. */
207932 p->rc = prepareAndCollectError(p->dbMain, &pQuery, &p->zErrmsg,
207933 "SELECT name FROM sqlite_schema WHERE rootpage = ?"
207934 );
207935 if( p->rc==SQLITE_OK0 ){
207936 sqlite3_bind_int(pQuery, 1, tnum);
207937 if( SQLITE_ROW100==sqlite3_step(pQuery) ){
207938 zIdx = (const char*)sqlite3_column_text(pQuery, 0);
207939 }
207940 }
207941 if( zIdx ){
207942 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
207943 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
207944 );
207945 }
207946 rbuFinalize(p, pQuery);
207947
207948 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pXInfo) ){
207949 int bKey = sqlite3_column_int(pXInfo, 5);
207950 if( bKey ){
207951 int iCid = sqlite3_column_int(pXInfo, 1);
207952 int bDesc = sqlite3_column_int(pXInfo, 3);
207953 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
207954 zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %Q", zCols, zComma,
207955 iCid, pIter->azTblType[iCid], zCollate
207956 );
207957 zPk = rbuMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
207958 zComma = ", ";
207959 }
207960 }
207961 zCols = rbuMPrintf(p, "%z, id INTEGER", zCols);
207962 rbuFinalize(p, pXInfo);
207963
207964 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 1, tnum);
207965 rbuMPrintfExec(p, p->dbMain,
207966 "CREATE TABLE rbu_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
207967 zCols, zPk
207968 );
207969 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 0, 0);
207970 }
207971}
207972
207973/*
207974** If an error has already occurred when this function is called, it
207975** immediately returns zero (without doing any work). Or, if an error
207976** occurs during the execution of this function, it sets the error code
207977** in the sqlite3rbu object indicated by the first argument and returns
207978** zero.
207979**
207980** The iterator passed as the second argument is guaranteed to point to
207981** a table (not an index) when this function is called. This function
207982** attempts to create any imposter table required to write to the main
207983** table b-tree of the table before returning. Non-zero is returned if
207984** an imposter table are created, or zero otherwise.
207985**
207986** An imposter table is required in all cases except RBU_PK_VTAB. Only
207987** virtual tables are written to directly. The imposter table has the
207988** same schema as the actual target table (less any UNIQUE constraints).
207989** More precisely, the "same schema" means the same columns, types,
207990** collation sequences. For tables that do not have an external PRIMARY
207991** KEY, it also means the same PRIMARY KEY declaration.
207992*/
207993static void rbuCreateImposterTable(sqlite3rbu *p, RbuObjIter *pIter){
207994 if( p->rc==SQLITE_OK0 && pIter->eType!=RBU_PK_VTAB ){
207995 int tnum = pIter->iTnum;
207996 const char *zComma = "";
207997 char *zSql = 0;
207998 int iCol;
207999 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 0, 1);
208000
208001 for(iCol=0; p->rc==SQLITE_OK0 && iCol<pIter->nTblCol; iCol++){
208002 const char *zPk = "";
208003 const char *zCol = pIter->azTblCol[iCol];
208004 const char *zColl = 0;
208005
208006 p->rc = sqlite3_table_column_metadata(
208007 p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
208008 );
208009
208010 if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){
208011 /* If the target table column is an "INTEGER PRIMARY KEY", add
208012 ** "PRIMARY KEY" to the imposter table column declaration. */
208013 zPk = "PRIMARY KEY ";
208014 }
208015 zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %Q%s",
208016 zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
208017 (pIter->abNotNull[iCol] ? " NOT NULL" : "")
208018 );
208019 zComma = ", ";
208020 }
208021
208022 if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
208023 char *zPk = rbuWithoutRowidPK(p, pIter);
208024 if( zPk ){
208025 zSql = rbuMPrintf(p, "%z, %z", zSql, zPk);
208026 }
208027 }
208028
208029 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 1, tnum);
208030 rbuMPrintfExec(p, p->dbMain, "CREATE TABLE \"rbu_imp_%w\"(%z)%s",
208031 pIter->zTbl, zSql,
208032 (pIter->eType==RBU_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
208033 );
208034 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 0, 0);
208035 }
208036}
208037
208038/*
208039** Prepare a statement used to insert rows into the "rbu_tmp_xxx" table.
208040** Specifically a statement of the form:
208041**
208042** INSERT INTO rbu_tmp_xxx VALUES(?, ?, ? ...);
208043**
208044** The number of bound variables is equal to the number of columns in
208045** the target table, plus one (for the rbu_control column), plus one more
208046** (for the rbu_rowid column) if the target table is an implicit IPK or
208047** virtual table.
208048*/
208049static void rbuObjIterPrepareTmpInsert(
208050 sqlite3rbu *p,
208051 RbuObjIter *pIter,
208052 const char *zCollist,
208053 const char *zRbuRowid
208054){
208055 int bRbuRowid = (pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE);
208056 char *zBind = rbuObjIterGetBindlist(p, pIter->nTblCol + 1 + bRbuRowid);
208057 if( zBind ){
208058 assert( pIter->pTmpInsert==0 )((void) (0));
208059 p->rc = prepareFreeAndCollectError(
208060 p->dbRbu, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
208061 "INSERT INTO %s.'rbu_tmp_%q'(rbu_control,%s%s) VALUES(%z)",
208062 p->zStateDb, pIter->zDataTbl, zCollist, zRbuRowid, zBind
208063 ));
208064 }
208065}
208066
208067static void rbuTmpInsertFunc(
208068 sqlite3_context *pCtx,
208069 int nVal,
208070 sqlite3_value **apVal
208071){
208072 sqlite3rbu *p = sqlite3_user_data(pCtx);
208073 int rc = SQLITE_OK0;
208074 int i;
208075
208076 assert( sqlite3_value_int(apVal[0])!=0((void) (0))
208077 || p->objiter.eType==RBU_PK_EXTERNAL((void) (0))
208078 || p->objiter.eType==RBU_PK_NONE((void) (0))
208079 )((void) (0));
208080 if( sqlite3_value_int(apVal[0])!=0 ){
208081 p->nPhaseOneStep += p->objiter.nIndex;
208082 }
208083
208084 for(i=0; rc==SQLITE_OK0 && i<nVal; i++){
208085 rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
208086 }
208087 if( rc==SQLITE_OK0 ){
208088 sqlite3_step(p->objiter.pTmpInsert);
208089 rc = sqlite3_reset(p->objiter.pTmpInsert);
208090 }
208091
208092 if( rc!=SQLITE_OK0 ){
208093 sqlite3_result_error_code(pCtx, rc);
208094 }
208095}
208096
208097static char *rbuObjIterGetIndexWhere(sqlite3rbu *p, RbuObjIter *pIter){
208098 sqlite3_stmt *pStmt = 0;
208099 int rc = p->rc;
208100 char *zRet = 0;
208101
208102 assert( pIter->zIdxSql==0 && pIter->nIdxCol==0 && pIter->aIdxCol==0 )((void) (0));
208103
208104 if( rc==SQLITE_OK0 ){
208105 rc = prepareAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
208106 "SELECT trim(sql) FROM sqlite_schema WHERE type='index' AND name=?"
208107 );
208108 }
208109 if( rc==SQLITE_OK0 ){
208110 int rc2;
208111 rc = sqlite3_bind_text(pStmt, 1, pIter->zIdx, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
208112 if( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pStmt) ){
208113 char *zSql = (char*)sqlite3_column_text(pStmt, 0);
208114 if( zSql ){
208115 pIter->zIdxSql = zSql = rbuStrndup(zSql, &rc);
208116 }
208117 if( zSql ){
208118 int nParen = 0; /* Number of open parenthesis */
208119 int i;
208120 int iIdxCol = 0;
208121 int nIdxAlloc = 0;
208122 for(i=0; zSql[i]; i++){
208123 char c = zSql[i];
208124
208125 /* If necessary, grow the pIter->aIdxCol[] array */
208126 if( iIdxCol==nIdxAlloc ){
208127 RbuSpan *aIdxCol = (RbuSpan*)sqlite3_realloc(
208128 pIter->aIdxCol, (nIdxAlloc+16)*sizeof(RbuSpan)
208129 );
208130 if( aIdxCol==0 ){
208131 rc = SQLITE_NOMEM7;
208132 break;
208133 }
208134 pIter->aIdxCol = aIdxCol;
208135 nIdxAlloc += 16;
208136 }
208137
208138 if( c=='(' ){
208139 if( nParen==0 ){
208140 assert( iIdxCol==0 )((void) (0));
208141 pIter->aIdxCol[0].zSpan = &zSql[i+1];
208142 }
208143 nParen++;
208144 }
208145 else if( c==')' ){
208146 nParen--;
208147 if( nParen==0 ){
208148 int nSpan = &zSql[i] - pIter->aIdxCol[iIdxCol].zSpan;
208149 pIter->aIdxCol[iIdxCol++].nSpan = nSpan;
208150 i++;
208151 break;
208152 }
208153 }else if( c==',' && nParen==1 ){
208154 int nSpan = &zSql[i] - pIter->aIdxCol[iIdxCol].zSpan;
208155 pIter->aIdxCol[iIdxCol++].nSpan = nSpan;
208156 pIter->aIdxCol[iIdxCol].zSpan = &zSql[i+1];
208157 }else if( c=='"' || c=='\'' || c=='`' ){
208158 for(i++; 1; i++){
208159 if( zSql[i]==c ){
208160 if( zSql[i+1]!=c ) break;
208161 i++;
208162 }
208163 }
208164 }else if( c=='[' ){
208165 for(i++; 1; i++){
208166 if( zSql[i]==']' ) break;
208167 }
208168 }else if( c=='-' && zSql[i+1]=='-' ){
208169 for(i=i+2; zSql[i] && zSql[i]!='\n'; i++);
208170 if( zSql[i]=='\0' ) break;
208171 }else if( c=='/' && zSql[i+1]=='*' ){
208172 for(i=i+2; zSql[i] && (zSql[i]!='*' || zSql[i+1]!='/'); i++);
208173 if( zSql[i]=='\0' ) break;
208174 i++;
208175 }
208176 }
208177 if( zSql[i] ){
208178 zRet = rbuStrndup(&zSql[i], &rc);
208179 }
208180 pIter->nIdxCol = iIdxCol;
208181 }
208182 }
208183
208184 rc2 = sqlite3_finalize(pStmt);
208185 if( rc==SQLITE_OK0 ) rc = rc2;
208186 }
208187
208188 p->rc = rc;
208189 return zRet;
208190}
208191
208192/*
208193** Ensure that the SQLite statement handles required to update the
208194** target database object currently indicated by the iterator passed
208195** as the second argument are available.
208196*/
208197static int rbuObjIterPrepareAll(
208198 sqlite3rbu *p,
208199 RbuObjIter *pIter,
208200 int nOffset /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
208201){
208202 assert( pIter->bCleanup==0 )((void) (0));
208203 if( pIter->pSelect==0 && rbuObjIterCacheTableInfo(p, pIter)==SQLITE_OK0 ){
208204 const int tnum = pIter->iTnum;
208205 char *zCollist = 0; /* List of indexed columns */
208206 char **pz = &p->zErrmsg;
208207 const char *zIdx = pIter->zIdx;
208208 char *zLimit = 0;
208209
208210 if( nOffset ){
208211 zLimit = sqlite3_mprintf(" LIMIT -1 OFFSET %d", nOffset);
208212 if( !zLimit ) p->rc = SQLITE_NOMEM7;
208213 }
208214
208215 if( zIdx ){
208216 const char *zTbl = pIter->zTbl;
208217 char *zImposterCols = 0; /* Columns for imposter table */
208218 char *zImposterPK = 0; /* Primary key declaration for imposter */
208219 char *zWhere = 0; /* WHERE clause on PK columns */
208220 char *zBind = 0;
208221 char *zPart = 0;
208222 int nBind = 0;
208223
208224 assert( pIter->eType!=RBU_PK_VTAB )((void) (0));
208225 zPart = rbuObjIterGetIndexWhere(p, pIter);
208226 zCollist = rbuObjIterGetIndexCols(
208227 p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
208228 );
208229 zBind = rbuObjIterGetBindlist(p, nBind);
208230
208231 /* Create the imposter table used to write to this index. */
208232 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 0, 1);
208233 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 1,tnum);
208234 rbuMPrintfExec(p, p->dbMain,
208235 "CREATE TABLE \"rbu_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
208236 zTbl, zImposterCols, zImposterPK
208237 );
208238 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 0, 0);
208239
208240 /* Create the statement to insert index entries */
208241 pIter->nCol = nBind;
208242 if( p->rc==SQLITE_OK0 ){
208243 p->rc = prepareFreeAndCollectError(
208244 p->dbMain, &pIter->pInsert, &p->zErrmsg,
208245 sqlite3_mprintf("INSERT INTO \"rbu_imp_%w\" VALUES(%s)", zTbl, zBind)
208246 );
208247 }
208248
208249 /* And to delete index entries */
208250 if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK0 ){
208251 p->rc = prepareFreeAndCollectError(
208252 p->dbMain, &pIter->pDelete, &p->zErrmsg,
208253 sqlite3_mprintf("DELETE FROM \"rbu_imp_%w\" WHERE %s", zTbl, zWhere)
208254 );
208255 }
208256
208257 /* Create the SELECT statement to read keys in sorted order */
208258 if( p->rc==SQLITE_OK0 ){
208259 char *zSql;
208260 if( rbuIsVacuum(p) ){
208261 char *zStart = 0;
208262 if( nOffset ){
208263 zStart = rbuVacuumIndexStart(p, pIter);
208264 if( zStart ){
208265 sqlite3_free(zLimit);
208266 zLimit = 0;
208267 }
208268 }
208269
208270 zSql = sqlite3_mprintf(
208271 "SELECT %s, 0 AS rbu_control FROM '%q' %s %s %s ORDER BY %s%s",
208272 zCollist,
208273 pIter->zDataTbl,
208274 zPart,
208275 (zStart ? (zPart ? "AND" : "WHERE") : ""), zStart,
208276 zCollist, zLimit
208277 );
208278 sqlite3_free(zStart);
208279 }else
208280
208281 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
208282 zSql = sqlite3_mprintf(
208283 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' %s ORDER BY %s%s",
208284 zCollist, p->zStateDb, pIter->zDataTbl,
208285 zPart, zCollist, zLimit
208286 );
208287 }else{
208288 zSql = sqlite3_mprintf(
208289 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' %s "
208290 "UNION ALL "
208291 "SELECT %s, rbu_control FROM '%q' "
208292 "%s %s typeof(rbu_control)='integer' AND rbu_control!=1 "
208293 "ORDER BY %s%s",
208294 zCollist, p->zStateDb, pIter->zDataTbl, zPart,
208295 zCollist, pIter->zDataTbl,
208296 zPart,
208297 (zPart ? "AND" : "WHERE"),
208298 zCollist, zLimit
208299 );
208300 }
208301 if( p->rc==SQLITE_OK0 ){
208302 p->rc = prepareFreeAndCollectError(p->dbRbu,&pIter->pSelect,pz,zSql);
208303 }else{
208304 sqlite3_free(zSql);
208305 }
208306 }
208307
208308 sqlite3_free(zImposterCols);
208309 sqlite3_free(zImposterPK);
208310 sqlite3_free(zWhere);
208311 sqlite3_free(zBind);
208312 sqlite3_free(zPart);
208313 }else{
208314 int bRbuRowid = (pIter->eType==RBU_PK_VTAB)
208315 ||(pIter->eType==RBU_PK_NONE)
208316 ||(pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p));
208317 const char *zTbl = pIter->zTbl; /* Table this step applies to */
208318 const char *zWrite; /* Imposter table name */
208319
208320 char *zBindings = rbuObjIterGetBindlist(p, pIter->nTblCol + bRbuRowid);
208321 char *zWhere = rbuObjIterGetWhere(p, pIter);
208322 char *zOldlist = rbuObjIterGetOldlist(p, pIter, "old");
208323 char *zNewlist = rbuObjIterGetOldlist(p, pIter, "new");
208324
208325 zCollist = rbuObjIterGetCollist(p, pIter);
208326 pIter->nCol = pIter->nTblCol;
208327
208328 /* Create the imposter table or tables (if required). */
208329 rbuCreateImposterTable(p, pIter);
208330 rbuCreateImposterTable2(p, pIter);
208331 zWrite = (pIter->eType==RBU_PK_VTAB ? "" : "rbu_imp_");
208332
208333 /* Create the INSERT statement to write to the target PK b-tree */
208334 if( p->rc==SQLITE_OK0 ){
208335 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
208336 sqlite3_mprintf(
208337 "INSERT INTO \"%s%w\"(%s%s) VALUES(%s)",
208338 zWrite, zTbl, zCollist, (bRbuRowid ? ", _rowid_" : ""), zBindings
208339 )
208340 );
208341 }
208342
208343 /* Create the DELETE statement to write to the target PK b-tree.
208344 ** Because it only performs INSERT operations, this is not required for
208345 ** an rbu vacuum handle. */
208346 if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK0 ){
208347 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pDelete, pz,
208348 sqlite3_mprintf(
208349 "DELETE FROM \"%s%w\" WHERE %s", zWrite, zTbl, zWhere
208350 )
208351 );
208352 }
208353
208354 if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
208355 const char *zRbuRowid = "";
208356 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
208357 zRbuRowid = ", rbu_rowid";
208358 }
208359
208360 /* Create the rbu_tmp_xxx table and the triggers to populate it. */
208361 rbuMPrintfExec(p, p->dbRbu,
208362 "CREATE TABLE IF NOT EXISTS %s.'rbu_tmp_%q' AS "
208363 "SELECT *%s FROM '%q' WHERE 0;"
208364 , p->zStateDb, pIter->zDataTbl
208365 , (pIter->eType==RBU_PK_EXTERNAL ? ", 0 AS rbu_rowid" : "")
208366 , pIter->zDataTbl
208367 );
208368
208369 rbuMPrintfExec(p, p->dbMain,
208370 "CREATE TEMP TRIGGER rbu_delete_tr BEFORE DELETE ON \"%s%w\" "
208371 "BEGIN "
208372 " SELECT rbu_tmp_insert(3, %s);"
208373 "END;"
208374
208375 "CREATE TEMP TRIGGER rbu_update1_tr BEFORE UPDATE ON \"%s%w\" "
208376 "BEGIN "
208377 " SELECT rbu_tmp_insert(3, %s);"
208378 "END;"
208379
208380 "CREATE TEMP TRIGGER rbu_update2_tr AFTER UPDATE ON \"%s%w\" "
208381 "BEGIN "
208382 " SELECT rbu_tmp_insert(4, %s);"
208383 "END;",
208384 zWrite, zTbl, zOldlist,
208385 zWrite, zTbl, zOldlist,
208386 zWrite, zTbl, zNewlist
208387 );
208388
208389 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
208390 rbuMPrintfExec(p, p->dbMain,
208391 "CREATE TEMP TRIGGER rbu_insert_tr AFTER INSERT ON \"%s%w\" "
208392 "BEGIN "
208393 " SELECT rbu_tmp_insert(0, %s);"
208394 "END;",
208395 zWrite, zTbl, zNewlist
208396 );
208397 }
208398
208399 rbuObjIterPrepareTmpInsert(p, pIter, zCollist, zRbuRowid);
208400 }
208401
208402 /* Create the SELECT statement to read keys from data_xxx */
208403 if( p->rc==SQLITE_OK0 ){
208404 const char *zRbuRowid = "";
208405 char *zStart = 0;
208406 char *zOrder = 0;
208407 if( bRbuRowid ){
208408 zRbuRowid = rbuIsVacuum(p) ? ",_rowid_ " : ",rbu_rowid";
208409 }
208410
208411 if( rbuIsVacuum(p) ){
208412 if( nOffset ){
208413 zStart = rbuVacuumTableStart(p, pIter, bRbuRowid, zWrite);
208414 if( zStart ){
208415 sqlite3_free(zLimit);
208416 zLimit = 0;
208417 }
208418 }
208419 if( bRbuRowid ){
208420 zOrder = rbuMPrintf(p, "_rowid_");
208421 }else{
208422 zOrder = rbuObjIterGetPkList(p, pIter, "", ", ", "");
208423 }
208424 }
208425
208426 if( p->rc==SQLITE_OK0 ){
208427 p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz,
208428 sqlite3_mprintf(
208429 "SELECT %s,%s rbu_control%s FROM '%q'%s %s %s %s",
208430 zCollist,
208431 (rbuIsVacuum(p) ? "0 AS " : ""),
208432 zRbuRowid,
208433 pIter->zDataTbl, (zStart ? zStart : ""),
208434 (zOrder ? "ORDER BY" : ""), zOrder,
208435 zLimit
208436 )
208437 );
208438 }
208439 sqlite3_free(zStart);
208440 sqlite3_free(zOrder);
208441 }
208442
208443 sqlite3_free(zWhere);
208444 sqlite3_free(zOldlist);
208445 sqlite3_free(zNewlist);
208446 sqlite3_free(zBindings);
208447 }
208448 sqlite3_free(zCollist);
208449 sqlite3_free(zLimit);
208450 }
208451
208452 return p->rc;
208453}
208454
208455/*
208456** Set output variable *ppStmt to point to an UPDATE statement that may
208457** be used to update the imposter table for the main table b-tree of the
208458** table object that pIter currently points to, assuming that the
208459** rbu_control column of the data_xyz table contains zMask.
208460**
208461** If the zMask string does not specify any columns to update, then this
208462** is not an error. Output variable *ppStmt is set to NULL in this case.
208463*/
208464static int rbuGetUpdateStmt(
208465 sqlite3rbu *p, /* RBU handle */
208466 RbuObjIter *pIter, /* Object iterator */
208467 const char *zMask, /* rbu_control value ('x.x.') */
208468 sqlite3_stmt **ppStmt /* OUT: UPDATE statement handle */
208469){
208470 RbuUpdateStmt **pp;
208471 RbuUpdateStmt *pUp = 0;
208472 int nUp = 0;
208473
208474 /* In case an error occurs */
208475 *ppStmt = 0;
208476
208477 /* Search for an existing statement. If one is found, shift it to the front
208478 ** of the LRU queue and return immediately. Otherwise, leave nUp pointing
208479 ** to the number of statements currently in the cache and pUp to the
208480 ** last object in the list. */
208481 for(pp=&pIter->pRbuUpdate; *pp; pp=&((*pp)->pNext)){
208482 pUp = *pp;
208483 if( strcmp(pUp->zMask, zMask)==0 ){
208484 *pp = pUp->pNext;
208485 pUp->pNext = pIter->pRbuUpdate;
208486 pIter->pRbuUpdate = pUp;
208487 *ppStmt = pUp->pUpdate;
208488 return SQLITE_OK0;
208489 }
208490 nUp++;
208491 }
208492 assert( pUp==0 || pUp->pNext==0 )((void) (0));
208493
208494 if( nUp>=SQLITE_RBU_UPDATE_CACHESIZE ){
208495 for(pp=&pIter->pRbuUpdate; *pp!=pUp; pp=&((*pp)->pNext));
208496 *pp = 0;
208497 sqlite3_finalize(pUp->pUpdate);
208498 pUp->pUpdate = 0;
208499 }else{
208500 pUp = (RbuUpdateStmt*)rbuMalloc(p, sizeof(RbuUpdateStmt)+pIter->nTblCol+1);
208501 }
208502
208503 if( pUp ){
208504 char *zWhere = rbuObjIterGetWhere(p, pIter);
208505 char *zSet = rbuObjIterGetSetlist(p, pIter, zMask);
208506 char *zUpdate = 0;
208507
208508 pUp->zMask = (char*)&pUp[1];
208509 memcpy(pUp->zMask, zMask, pIter->nTblCol);
208510 pUp->pNext = pIter->pRbuUpdate;
208511 pIter->pRbuUpdate = pUp;
208512
208513 if( zSet ){
208514 const char *zPrefix = "";
208515
208516 if( pIter->eType!=RBU_PK_VTAB ) zPrefix = "rbu_imp_";
208517 zUpdate = sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s",
208518 zPrefix, pIter->zTbl, zSet, zWhere
208519 );
208520 p->rc = prepareFreeAndCollectError(
208521 p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate
208522 );
208523 *ppStmt = pUp->pUpdate;
208524 }
208525 sqlite3_free(zWhere);
208526 sqlite3_free(zSet);
208527 }
208528
208529 return p->rc;
208530}
208531
208532static sqlite3 *rbuOpenDbhandle(
208533 sqlite3rbu *p,
208534 const char *zName,
208535 int bUseVfs
208536){
208537 sqlite3 *db = 0;
208538 if( p->rc==SQLITE_OK0 ){
208539 const int flags = SQLITE_OPEN_READWRITE0x00000002|SQLITE_OPEN_CREATE0x00000004|SQLITE_OPEN_URI0x00000040;
208540 p->rc = sqlite3_open_v2(zName, &db, flags, bUseVfs ? p->zVfsName : 0);
208541 if( p->rc ){
208542 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
208543 sqlite3_close(db);
208544 db = 0;
208545 }
208546 }
208547 return db;
208548}
208549
208550/*
208551** Free an RbuState object allocated by rbuLoadState().
208552*/
208553static void rbuFreeState(RbuState *p){
208554 if( p ){
208555 sqlite3_free(p->zTbl);
208556 sqlite3_free(p->zDataTbl);
208557 sqlite3_free(p->zIdx);
208558 sqlite3_free(p);
208559 }
208560}
208561
208562/*
208563** Allocate an RbuState object and load the contents of the rbu_state
208564** table into it. Return a pointer to the new object. It is the
208565** responsibility of the caller to eventually free the object using
208566** sqlite3_free().
208567**
208568** If an error occurs, leave an error code and message in the rbu handle
208569** and return NULL.
208570*/
208571static RbuState *rbuLoadState(sqlite3rbu *p){
208572 RbuState *pRet = 0;
208573 sqlite3_stmt *pStmt = 0;
208574 int rc;
208575 int rc2;
208576
208577 pRet = (RbuState*)rbuMalloc(p, sizeof(RbuState));
208578 if( pRet==0 ) return 0;
208579
208580 rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
208581 sqlite3_mprintf("SELECT k, v FROM %s.rbu_state", p->zStateDb)
208582 );
208583 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pStmt) ){
208584 switch( sqlite3_column_int(pStmt, 0) ){
208585 case RBU_STATE_STAGE:
208586 pRet->eStage = sqlite3_column_int(pStmt, 1);
208587 if( pRet->eStage!=RBU_STAGE_OAL
208588 && pRet->eStage!=RBU_STAGE_MOVE
208589 && pRet->eStage!=RBU_STAGE_CKPT
208590 ){
208591 p->rc = SQLITE_CORRUPT11;
208592 }
208593 break;
208594
208595 case RBU_STATE_TBL:
208596 pRet->zTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
208597 break;
208598
208599 case RBU_STATE_IDX:
208600 pRet->zIdx = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
208601 break;
208602
208603 case RBU_STATE_ROW:
208604 pRet->nRow = sqlite3_column_int(pStmt, 1);
208605 break;
208606
208607 case RBU_STATE_PROGRESS:
208608 pRet->nProgress = sqlite3_column_int64(pStmt, 1);
208609 break;
208610
208611 case RBU_STATE_CKPT:
208612 pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
208613 break;
208614
208615 case RBU_STATE_COOKIE:
208616 pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
208617 break;
208618
208619 case RBU_STATE_OALSZ:
208620 pRet->iOalSz = sqlite3_column_int64(pStmt, 1);
208621 break;
208622
208623 case RBU_STATE_PHASEONESTEP:
208624 pRet->nPhaseOneStep = sqlite3_column_int64(pStmt, 1);
208625 break;
208626
208627 case RBU_STATE_DATATBL:
208628 pRet->zDataTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
208629 break;
208630
208631 default:
208632 rc = SQLITE_CORRUPT11;
208633 break;
208634 }
208635 }
208636 rc2 = sqlite3_finalize(pStmt);
208637 if( rc==SQLITE_OK0 ) rc = rc2;
208638
208639 p->rc = rc;
208640 return pRet;
208641}
208642
208643
208644/*
208645** Open the database handle and attach the RBU database as "rbu". If an
208646** error occurs, leave an error code and message in the RBU handle.
208647**
208648** If argument dbMain is not NULL, then it is a database handle already
208649** open on the target database. Use this handle instead of opening a new
208650** one.
208651*/
208652static void rbuOpenDatabase(sqlite3rbu *p, sqlite3 *dbMain, int *pbRetry){
208653 assert( p->rc || (p->dbMain==0 && p->dbRbu==0) )((void) (0));
208654 assert( p->rc || rbuIsVacuum(p) || p->zTarget!=0 )((void) (0));
208655 assert( dbMain==0 || rbuIsVacuum(p)==0 )((void) (0));
208656
208657 /* Open the RBU database */
208658 p->dbRbu = rbuOpenDbhandle(p, p->zRbu, 1);
208659 p->dbMain = dbMain;
208660
208661 if( p->rc==SQLITE_OK0 && rbuIsVacuum(p) ){
208662 sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
208663 if( p->zState==0 ){
208664 const char *zFile = sqlite3_db_filename(p->dbRbu, "main");
208665 p->zState = rbuMPrintf(p, "file://%s-vacuum?modeof=%s", zFile, zFile);
208666 }
208667 }
208668
208669 /* If using separate RBU and state databases, attach the state database to
208670 ** the RBU db handle now. */
208671 if( p->zState ){
208672 rbuMPrintfExec(p, p->dbRbu, "ATTACH %Q AS stat", p->zState);
208673 memcpy(p->zStateDb, "stat", 4);
208674 }else{
208675 memcpy(p->zStateDb, "main", 4);
208676 }
208677
208678#if 0
208679 if( p->rc==SQLITE_OK0 && rbuIsVacuum(p) ){
208680 p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, 0);
208681 }
208682#endif
208683
208684 /* If it has not already been created, create the rbu_state table */
208685 rbuMPrintfExec(p, p->dbRbu, RBU_CREATE_STATE, p->zStateDb);
208686
208687#if 0
208688 if( rbuIsVacuum(p) ){
208689 if( p->rc==SQLITE_OK0 ){
208690 int rc2;
208691 int bOk = 0;
208692 sqlite3_stmt *pCnt = 0;
208693 p->rc = prepareAndCollectError(p->dbRbu, &pCnt, &p->zErrmsg,
208694 "SELECT count(*) FROM stat.sqlite_schema"
208695 );
208696 if( p->rc==SQLITE_OK0
208697 && sqlite3_step(pCnt)==SQLITE_ROW100
208698 && 1==sqlite3_column_int(pCnt, 0)
208699 ){
208700 bOk = 1;
208701 }
208702 rc2 = sqlite3_finalize(pCnt);
208703 if( p->rc==SQLITE_OK0 ) p->rc = rc2;
208704
208705 if( p->rc==SQLITE_OK0 && bOk==0 ){
208706 p->rc = SQLITE_ERROR1;
208707 p->zErrmsg = sqlite3_mprintf("invalid state database");
208708 }
208709
208710 if( p->rc==SQLITE_OK0 ){
208711 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
208712 }
208713 }
208714 }
208715#endif
208716
208717 if( p->rc==SQLITE_OK0 && rbuIsVacuum(p) ){
208718 int bOpen = 0;
208719 int rc;
208720 p->nRbu = 0;
208721 p->pRbuFd = 0;
208722 rc = sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
208723 if( rc!=SQLITE_NOTFOUND12 ) p->rc = rc;
208724 if( p->eStage>=RBU_STAGE_MOVE ){
208725 bOpen = 1;
208726 }else{
208727 RbuState *pState = rbuLoadState(p);
208728 if( pState ){
208729 bOpen = (pState->eStage>=RBU_STAGE_MOVE);
208730 rbuFreeState(pState);
208731 }
208732 }
208733 if( bOpen ) p->dbMain = rbuOpenDbhandle(p, p->zRbu, p->nRbu<=1);
208734 }
208735
208736 p->eStage = 0;
208737 if( p->rc==SQLITE_OK0 && p->dbMain==0 ){
208738 if( !rbuIsVacuum(p) ){
208739 p->dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
208740 }else if( p->pRbuFd->pWalFd ){
208741 if( pbRetry ){
208742 p->pRbuFd->bNolock = 0;
208743 sqlite3_close(p->dbRbu);
208744 sqlite3_close(p->dbMain);
208745 p->dbMain = 0;
208746 p->dbRbu = 0;
208747 *pbRetry = 1;
208748 return;
208749 }
208750 p->rc = SQLITE_ERROR1;
208751 p->zErrmsg = sqlite3_mprintf("cannot vacuum wal mode database");
208752 }else{
208753 char *zTarget;
208754 char *zExtra = 0;
208755 if( strlen(p->zRbu)>=5 && 0==memcmp("file:", p->zRbu, 5) ){
208756 zExtra = &p->zRbu[5];
208757 while( *zExtra ){
208758 if( *zExtra++=='?' ) break;
208759 }
208760 if( *zExtra=='\0' ) zExtra = 0;
208761 }
208762
208763 zTarget = sqlite3_mprintf("file:%s-vactmp?rbu_memory=1%s%s",
208764 sqlite3_db_filename(p->dbRbu, "main"),
208765 (zExtra==0 ? "" : "&"), (zExtra==0 ? "" : zExtra)
208766 );
208767
208768 if( zTarget==0 ){
208769 p->rc = SQLITE_NOMEM7;
208770 return;
208771 }
208772 p->dbMain = rbuOpenDbhandle(p, zTarget, p->nRbu<=1);
208773 sqlite3_free(zTarget);
208774 }
208775 }
208776
208777 if( p->rc==SQLITE_OK0 ){
208778 p->rc = sqlite3_create_function(p->dbMain,
208779 "rbu_tmp_insert", -1, SQLITE_UTF81, (void*)p, rbuTmpInsertFunc, 0, 0
208780 );
208781 }
208782
208783 if( p->rc==SQLITE_OK0 ){
208784 p->rc = sqlite3_create_function(p->dbMain,
208785 "rbu_fossil_delta", 2, SQLITE_UTF81, 0, rbuFossilDeltaFunc, 0, 0
208786 );
208787 }
208788
208789 if( p->rc==SQLITE_OK0 ){
208790 p->rc = sqlite3_create_function(p->dbRbu,
208791 "rbu_target_name", -1, SQLITE_UTF81, (void*)p, rbuTargetNameFunc, 0, 0
208792 );
208793 }
208794
208795 if( p->rc==SQLITE_OK0 ){
208796 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU26, (void*)p);
208797 }
208798 rbuMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_schema");
208799
208800 /* Mark the database file just opened as an RBU target database. If
208801 ** this call returns SQLITE_NOTFOUND, then the RBU vfs is not in use.
208802 ** This is an error. */
208803 if( p->rc==SQLITE_OK0 ){
208804 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU26, (void*)p);
208805 }
208806
208807 if( p->rc==SQLITE_NOTFOUND12 ){
208808 p->rc = SQLITE_ERROR1;
208809 p->zErrmsg = sqlite3_mprintf("rbu vfs not found");
208810 }
208811}
208812
208813/*
208814** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
208815** It is a no-op unless SQLITE_ENABLE_8_3_NAMES is defined.
208816**
208817** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
208818** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
208819** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
208820** three characters, then shorten the suffix on z[] to be the last three
208821** characters of the original suffix.
208822**
208823** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
208824** do the suffix shortening regardless of URI parameter.
208825**
208826** Examples:
208827**
208828** test.db-journal => test.nal
208829** test.db-wal => test.wal
208830** test.db-shm => test.shm
208831** test.db-mj7f3319fa => test.9fa
208832*/
208833static void rbuFileSuffix3(const char *zBase, char *z){
208834#ifdef SQLITE_ENABLE_8_3_NAMES
208835#if SQLITE_ENABLE_8_3_NAMES<2
208836 if( sqlite3_uri_boolean(zBase, "8_3_names", 0) )
208837#endif
208838 {
208839 int i, sz;
208840 sz = (int)strlen(z)&0xffffff;
208841 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
208842 if( z[i]=='.' && sz>i+4 ) memmove(&z[i+1], &z[sz-3], 4);
208843 }
208844#endif
208845}
208846
208847/*
208848** Return the current wal-index header checksum for the target database
208849** as a 64-bit integer.
208850**
208851** The checksum is store in the first page of xShmMap memory as an 8-byte
208852** blob starting at byte offset 40.
208853*/
208854static i64 rbuShmChecksum(sqlite3rbu *p){
208855 i64 iRet = 0;
208856 if( p->rc==SQLITE_OK0 ){
208857 sqlite3_file *pDb = p->pTargetFd->pReal;
208858 u32 volatile *ptr;
208859 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
208860 if( p->rc==SQLITE_OK0 ){
208861 iRet = ((i64)ptr[10] << 32) + ptr[11];
208862 }
208863 }
208864 return iRet;
208865}
208866
208867/*
208868** This function is called as part of initializing or reinitializing an
208869** incremental checkpoint.
208870**
208871** It populates the sqlite3rbu.aFrame[] array with the set of
208872** (wal frame -> db page) copy operations required to checkpoint the
208873** current wal file, and obtains the set of shm locks required to safely
208874** perform the copy operations directly on the file-system.
208875**
208876** If argument pState is not NULL, then the incremental checkpoint is
208877** being resumed. In this case, if the checksum of the wal-index-header
208878** following recovery is not the same as the checksum saved in the RbuState
208879** object, then the rbu handle is set to DONE state. This occurs if some
208880** other client appends a transaction to the wal file in the middle of
208881** an incremental checkpoint.
208882*/
208883static void rbuSetupCheckpoint(sqlite3rbu *p, RbuState *pState){
208884
208885 /* If pState is NULL, then the wal file may not have been opened and
208886 ** recovered. Running a read-statement here to ensure that doing so
208887 ** does not interfere with the "capture" process below. */
208888 if( pState==0 ){
208889 p->eStage = 0;
208890 if( p->rc==SQLITE_OK0 ){
208891 p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_schema", 0, 0, 0);
208892 }
208893 }
208894
208895 /* Assuming no error has occurred, run a "restart" checkpoint with the
208896 ** sqlite3rbu.eStage variable set to CAPTURE. This turns on the following
208897 ** special behaviour in the rbu VFS:
208898 **
208899 ** * If the exclusive shm WRITER or READ0 lock cannot be obtained,
208900 ** the checkpoint fails with SQLITE_BUSY (normally SQLite would
208901 ** proceed with running a passive checkpoint instead of failing).
208902 **
208903 ** * Attempts to read from the *-wal file or write to the database file
208904 ** do not perform any IO. Instead, the frame/page combinations that
208905 ** would be read/written are recorded in the sqlite3rbu.aFrame[]
208906 ** array.
208907 **
208908 ** * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER,
208909 ** READ0 and CHECKPOINT locks taken as part of the checkpoint are
208910 ** no-ops. These locks will not be released until the connection
208911 ** is closed.
208912 **
208913 ** * Attempting to xSync() the database file causes an SQLITE_INTERNAL
208914 ** error.
208915 **
208916 ** As a result, unless an error (i.e. OOM or SQLITE_BUSY) occurs, the
208917 ** checkpoint below fails with SQLITE_INTERNAL, and leaves the aFrame[]
208918 ** array populated with a set of (frame -> page) mappings. Because the
208919 ** WRITER, CHECKPOINT and READ0 locks are still held, it is safe to copy
208920 ** data from the wal file into the database file according to the
208921 ** contents of aFrame[].
208922 */
208923 if( p->rc==SQLITE_OK0 ){
208924 int rc2;
208925 p->eStage = RBU_STAGE_CAPTURE;
208926 rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
208927 if( rc2!=SQLITE_INTERNAL2 ) p->rc = rc2;
208928 }
208929
208930 if( p->rc==SQLITE_OK0 && p->nFrame>0 ){
208931 p->eStage = RBU_STAGE_CKPT;
208932 p->nStep = (pState ? pState->nRow : 0);
208933 p->aBuf = rbuMalloc(p, p->pgsz);
208934 p->iWalCksum = rbuShmChecksum(p);
208935 }
208936
208937 if( p->rc==SQLITE_OK0 ){
208938 if( p->nFrame==0 || (pState && pState->iWalCksum!=p->iWalCksum) ){
208939 p->rc = SQLITE_DONE101;
208940 p->eStage = RBU_STAGE_DONE;
208941 }else{
208942 int nSectorSize;
208943 sqlite3_file *pDb = p->pTargetFd->pReal;
208944 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
208945 assert( p->nPagePerSector==0 )((void) (0));
208946 nSectorSize = pDb->pMethods->xSectorSize(pDb);
208947 if( nSectorSize>p->pgsz ){
208948 p->nPagePerSector = nSectorSize / p->pgsz;
208949 }else{
208950 p->nPagePerSector = 1;
208951 }
208952
208953 /* Call xSync() on the wal file. This causes SQLite to sync the
208954 ** directory in which the target database and the wal file reside, in
208955 ** case it has not been synced since the rename() call in
208956 ** rbuMoveOalFile(). */
208957 p->rc = pWal->pMethods->xSync(pWal, SQLITE_SYNC_NORMAL0x00002);
208958 }
208959 }
208960}
208961
208962/*
208963** Called when iAmt bytes are read from offset iOff of the wal file while
208964** the rbu object is in capture mode. Record the frame number of the frame
208965** being read in the aFrame[] array.
208966*/
208967static int rbuCaptureWalRead(sqlite3rbu *pRbu, i64 iOff, int iAmt){
208968 const u32 mReq = (1<<WAL_LOCK_WRITE)|(1<<WAL_LOCK_CKPT)|(1<<WAL_LOCK_READ0);
208969 u32 iFrame;
208970
208971 if( pRbu->mLock!=mReq ){
208972 pRbu->rc = SQLITE_BUSY5;
208973 return SQLITE_INTERNAL2;
208974 }
208975
208976 pRbu->pgsz = iAmt;
208977 if( pRbu->nFrame==pRbu->nFrameAlloc ){
208978 int nNew = (pRbu->nFrameAlloc ? pRbu->nFrameAlloc : 64) * 2;
208979 RbuFrame *aNew;
208980 aNew = (RbuFrame*)sqlite3_realloc64(pRbu->aFrame, nNew * sizeof(RbuFrame));
208981 if( aNew==0 ) return SQLITE_NOMEM7;
208982 pRbu->aFrame = aNew;
208983 pRbu->nFrameAlloc = nNew;
208984 }
208985
208986 iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
208987 if( pRbu->iMaxFrame<iFrame ) pRbu->iMaxFrame = iFrame;
208988 pRbu->aFrame[pRbu->nFrame].iWalFrame = iFrame;
208989 pRbu->aFrame[pRbu->nFrame].iDbPage = 0;
208990 pRbu->nFrame++;
208991 return SQLITE_OK0;
208992}
208993
208994/*
208995** Called when a page of data is written to offset iOff of the database
208996** file while the rbu handle is in capture mode. Record the page number
208997** of the page being written in the aFrame[] array.
208998*/
208999static int rbuCaptureDbWrite(sqlite3rbu *pRbu, i64 iOff){
209000 pRbu->aFrame[pRbu->nFrame-1].iDbPage = (u32)(iOff / pRbu->pgsz) + 1;
209001 return SQLITE_OK0;
209002}
209003
209004/*
209005** This is called as part of an incremental checkpoint operation. Copy
209006** a single frame of data from the wal file into the database file, as
209007** indicated by the RbuFrame object.
209008*/
209009static void rbuCheckpointFrame(sqlite3rbu *p, RbuFrame *pFrame){
209010 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
209011 sqlite3_file *pDb = p->pTargetFd->pReal;
209012 i64 iOff;
209013
209014 assert( p->rc==SQLITE_OK )((void) (0));
209015 iOff = (i64)(pFrame->iWalFrame-1) * (p->pgsz + 24) + 32 + 24;
209016 p->rc = pWal->pMethods->xRead(pWal, p->aBuf, p->pgsz, iOff);
209017 if( p->rc ) return;
209018
209019 iOff = (i64)(pFrame->iDbPage-1) * p->pgsz;
209020 p->rc = pDb->pMethods->xWrite(pDb, p->aBuf, p->pgsz, iOff);
209021}
209022
209023
209024/*
209025** Take an EXCLUSIVE lock on the database file. Return SQLITE_OK if
209026** successful, or an SQLite error code otherwise.
209027*/
209028static int rbuLockDatabase(sqlite3 *db){
209029 int rc = SQLITE_OK0;
209030 sqlite3_file *fd = 0;
209031 sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER7, &fd);
209032
209033 if( fd->pMethods ){
209034 rc = fd->pMethods->xLock(fd, SQLITE_LOCK_SHARED1);
209035 if( rc==SQLITE_OK0 ){
209036 rc = fd->pMethods->xLock(fd, SQLITE_LOCK_EXCLUSIVE4);
209037 }
209038 }
209039 return rc;
209040}
209041
209042/*
209043** Return true if the database handle passed as the only argument
209044** was opened with the rbu_exclusive_checkpoint=1 URI parameter
209045** specified. Or false otherwise.
209046*/
209047static int rbuExclusiveCheckpoint(sqlite3 *db){
209048 const char *zUri = sqlite3_db_filename(db, 0);
209049 return sqlite3_uri_boolean(zUri, RBU_EXCLUSIVE_CHECKPOINT, 0);
209050}
209051
209052#if defined(_WIN32_WCE)
209053static LPWSTR rbuWinUtf8ToUnicode(const char *zFilename){
209054 int nChar;
209055 LPWSTR zWideFilename;
209056
209057 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL((void*)0), 0);
209058 if( nChar==0 ){
209059 return 0;
209060 }
209061 zWideFilename = sqlite3_malloc64( nChar*sizeof(zWideFilename[0]) );
209062 if( zWideFilename==0 ){
209063 return 0;
209064 }
209065 memset(zWideFilename, 0, nChar*sizeof(zWideFilename[0]));
209066 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
209067 nChar);
209068 if( nChar==0 ){
209069 sqlite3_free(zWideFilename);
209070 zWideFilename = 0;
209071 }
209072 return zWideFilename;
209073}
209074#endif
209075
209076/*
209077** The RBU handle is currently in RBU_STAGE_OAL state, with a SHARED lock
209078** on the database file. This proc moves the *-oal file to the *-wal path,
209079** then reopens the database file (this time in vanilla, non-oal, WAL mode).
209080** If an error occurs, leave an error code and error message in the rbu
209081** handle.
209082*/
209083static void rbuMoveOalFile(sqlite3rbu *p){
209084 const char *zBase = sqlite3_db_filename(p->dbMain, "main");
209085 const char *zMove = zBase;
209086 char *zOal;
209087 char *zWal;
209088
209089 if( rbuIsVacuum(p) ){
209090 zMove = sqlite3_db_filename(p->dbRbu, "main");
209091 }
209092 zOal = sqlite3_mprintf("%s-oal", zMove);
209093 zWal = sqlite3_mprintf("%s-wal", zMove);
209094
209095 assert( p->eStage==RBU_STAGE_MOVE )((void) (0));
209096 assert( p->rc==SQLITE_OK && p->zErrmsg==0 )((void) (0));
209097 if( zWal==0 || zOal==0 ){
209098 p->rc = SQLITE_NOMEM7;
209099 }else{
209100 /* Move the *-oal file to *-wal. At this point connection p->db is
209101 ** holding a SHARED lock on the target database file (because it is
209102 ** in WAL mode). So no other connection may be writing the db.
209103 **
209104 ** In order to ensure that there are no database readers, an EXCLUSIVE
209105 ** lock is obtained here before the *-oal is moved to *-wal.
209106 */
209107 sqlite3 *dbMain = 0;
209108 rbuFileSuffix3(zBase, zWal);
209109 rbuFileSuffix3(zBase, zOal);
209110
209111 /* Re-open the databases. */
209112 rbuObjIterFinalize(&p->objiter);
209113 sqlite3_close(p->dbRbu);
209114 sqlite3_close(p->dbMain);
209115 p->dbMain = 0;
209116 p->dbRbu = 0;
209117
209118 dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
209119 if( dbMain ){
209120 assert( p->rc==SQLITE_OK )((void) (0));
209121 p->rc = rbuLockDatabase(dbMain);
209122 }
209123
209124 if( p->rc==SQLITE_OK0 ){
209125#if defined(_WIN32_WCE)
209126 {
209127 LPWSTR zWideOal;
209128 LPWSTR zWideWal;
209129
209130 zWideOal = rbuWinUtf8ToUnicode(zOal);
209131 if( zWideOal ){
209132 zWideWal = rbuWinUtf8ToUnicode(zWal);
209133 if( zWideWal ){
209134 if( MoveFileW(zWideOal, zWideWal) ){
209135 p->rc = SQLITE_OK0;
209136 }else{
209137 p->rc = SQLITE_IOERR10;
209138 }
209139 sqlite3_free(zWideWal);
209140 }else{
209141 p->rc = SQLITE_IOERR_NOMEM(10 | (12<<8));
209142 }
209143 sqlite3_free(zWideOal);
209144 }else{
209145 p->rc = SQLITE_IOERR_NOMEM(10 | (12<<8));
209146 }
209147 }
209148#else
209149 p->rc = rename(zOal, zWal) ? SQLITE_IOERR10 : SQLITE_OK0;
209150#endif
209151 }
209152
209153 if( p->rc!=SQLITE_OK0
209154 || rbuIsVacuum(p)
209155 || rbuExclusiveCheckpoint(dbMain)==0
209156 ){
209157 sqlite3_close(dbMain);
209158 dbMain = 0;
209159 }
209160
209161 if( p->rc==SQLITE_OK0 ){
209162 rbuOpenDatabase(p, dbMain, 0);
209163 rbuSetupCheckpoint(p, 0);
209164 }
209165 }
209166
209167 sqlite3_free(zWal);
209168 sqlite3_free(zOal);
209169}
209170
209171/*
209172** The SELECT statement iterating through the keys for the current object
209173** (p->objiter.pSelect) currently points to a valid row. This function
209174** determines the type of operation requested by this row and returns
209175** one of the following values to indicate the result:
209176**
209177** * RBU_INSERT
209178** * RBU_DELETE
209179** * RBU_IDX_DELETE
209180** * RBU_UPDATE
209181**
209182** If RBU_UPDATE is returned, then output variable *pzMask is set to
209183** point to the text value indicating the columns to update.
209184**
209185** If the rbu_control field contains an invalid value, an error code and
209186** message are left in the RBU handle and zero returned.
209187*/
209188static int rbuStepType(sqlite3rbu *p, const char **pzMask){
209189 int iCol = p->objiter.nCol; /* Index of rbu_control column */
209190 int res = 0; /* Return value */
209191
209192 switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
209193 case SQLITE_INTEGER1: {
209194 int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
209195 switch( iVal ){
209196 case 0: res = RBU_INSERT; break;
209197 case 1: res = RBU_DELETE; break;
209198 case 2: res = RBU_REPLACE; break;
209199 case 3: res = RBU_IDX_DELETE; break;
209200 case 4: res = RBU_IDX_INSERT; break;
209201 }
209202 break;
209203 }
209204
209205 case SQLITE_TEXT3: {
209206 const unsigned char *z = sqlite3_column_text(p->objiter.pSelect, iCol);
209207 if( z==0 ){
209208 p->rc = SQLITE_NOMEM7;
209209 }else{
209210 *pzMask = (const char*)z;
209211 }
209212 res = RBU_UPDATE;
209213
209214 break;
209215 }
209216
209217 default:
209218 break;
209219 }
209220
209221 if( res==0 ){
209222 rbuBadControlError(p);
209223 }
209224 return res;
209225}
209226
209227#ifdef SQLITE_DEBUG
209228/*
209229** Assert that column iCol of statement pStmt is named zName.
209230*/
209231static void assertColumnName(sqlite3_stmt *pStmt, int iCol, const char *zName){
209232 const char *zCol = sqlite3_column_name(pStmt, iCol);
209233 assert( 0==sqlite3_stricmp(zName, zCol) )((void) (0));
209234}
209235#else
209236# define assertColumnName(x,y,z)
209237#endif
209238
209239/*
209240** Argument eType must be one of RBU_INSERT, RBU_DELETE, RBU_IDX_INSERT or
209241** RBU_IDX_DELETE. This function performs the work of a single
209242** sqlite3rbu_step() call for the type of operation specified by eType.
209243*/
209244static void rbuStepOneOp(sqlite3rbu *p, int eType){
209245 RbuObjIter *pIter = &p->objiter;
209246 sqlite3_value *pVal;
209247 sqlite3_stmt *pWriter;
209248 int i;
209249
209250 assert( p->rc==SQLITE_OK )((void) (0));
209251 assert( eType!=RBU_DELETE || pIter->zIdx==0 )((void) (0));
209252 assert( eType==RBU_DELETE || eType==RBU_IDX_DELETE((void) (0))
209253 || eType==RBU_INSERT || eType==RBU_IDX_INSERT((void) (0))
209254 )((void) (0));
209255
209256 /* If this is a delete, decrement nPhaseOneStep by nIndex. If the DELETE
209257 ** statement below does actually delete a row, nPhaseOneStep will be
209258 ** incremented by the same amount when SQL function rbu_tmp_insert()
209259 ** is invoked by the trigger. */
209260 if( eType==RBU_DELETE ){
209261 p->nPhaseOneStep -= p->objiter.nIndex;
209262 }
209263
209264 if( eType==RBU_IDX_DELETE || eType==RBU_DELETE ){
209265 pWriter = pIter->pDelete;
209266 }else{
209267 pWriter = pIter->pInsert;
209268 }
209269
209270 for(i=0; i<pIter->nCol; i++){
209271 /* If this is an INSERT into a table b-tree and the table has an
209272 ** explicit INTEGER PRIMARY KEY, check that this is not an attempt
209273 ** to write a NULL into the IPK column. That is not permitted. */
209274 if( eType==RBU_INSERT
209275 && pIter->zIdx==0 && pIter->eType==RBU_PK_IPK && pIter->abTblPk[i]
209276 && sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL5
209277 ){
209278 p->rc = SQLITE_MISMATCH20;
209279 p->zErrmsg = sqlite3_mprintf("datatype mismatch");
209280 return;
209281 }
209282
209283 if( eType==RBU_DELETE && pIter->abTblPk[i]==0 ){
209284 continue;
209285 }
209286
209287 pVal = sqlite3_column_value(pIter->pSelect, i);
209288 p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
209289 if( p->rc ) return;
209290 }
209291 if( pIter->zIdx==0 ){
209292 if( pIter->eType==RBU_PK_VTAB
209293 || pIter->eType==RBU_PK_NONE
209294 || (pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p))
209295 ){
209296 /* For a virtual table, or a table with no primary key, the
209297 ** SELECT statement is:
209298 **
209299 ** SELECT <cols>, rbu_control, rbu_rowid FROM ....
209300 **
209301 ** Hence column_value(pIter->nCol+1).
209302 */
209303 assertColumnName(pIter->pSelect, pIter->nCol+1,
209304 rbuIsVacuum(p) ? "rowid" : "rbu_rowid"
209305 );
209306 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
209307 p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
209308 }
209309 }
209310 if( p->rc==SQLITE_OK0 ){
209311 sqlite3_step(pWriter);
209312 p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
209313 }
209314}
209315
209316/*
209317** This function does the work for an sqlite3rbu_step() call.
209318**
209319** The object-iterator (p->objiter) currently points to a valid object,
209320** and the input cursor (p->objiter.pSelect) currently points to a valid
209321** input row. Perform whatever processing is required and return.
209322**
209323** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
209324** and message is left in the RBU handle and a copy of the error code
209325** returned.
209326*/
209327static int rbuStep(sqlite3rbu *p){
209328 RbuObjIter *pIter = &p->objiter;
209329 const char *zMask = 0;
209330 int eType = rbuStepType(p, &zMask);
209331
209332 if( eType ){
209333 assert( eType==RBU_INSERT || eType==RBU_DELETE((void) (0))
209334 || eType==RBU_REPLACE || eType==RBU_IDX_DELETE((void) (0))
209335 || eType==RBU_IDX_INSERT || eType==RBU_UPDATE((void) (0))
209336 )((void) (0));
209337 assert( eType!=RBU_UPDATE || pIter->zIdx==0 )((void) (0));
209338
209339 if( pIter->zIdx==0 && (eType==RBU_IDX_DELETE || eType==RBU_IDX_INSERT) ){
209340 rbuBadControlError(p);
209341 }
209342 else if( eType==RBU_REPLACE ){
209343 if( pIter->zIdx==0 ){
209344 p->nPhaseOneStep += p->objiter.nIndex;
209345 rbuStepOneOp(p, RBU_DELETE);
209346 }
209347 if( p->rc==SQLITE_OK0 ) rbuStepOneOp(p, RBU_INSERT);
209348 }
209349 else if( eType!=RBU_UPDATE ){
209350 rbuStepOneOp(p, eType);
209351 }
209352 else{
209353 sqlite3_value *pVal;
209354 sqlite3_stmt *pUpdate = 0;
209355 assert( eType==RBU_UPDATE )((void) (0));
209356 p->nPhaseOneStep -= p->objiter.nIndex;
209357 rbuGetUpdateStmt(p, pIter, zMask, &pUpdate);
209358 if( pUpdate ){
209359 int i;
209360 for(i=0; p->rc==SQLITE_OK0 && i<pIter->nCol; i++){
209361 char c = zMask[pIter->aiSrcOrder[i]];
209362 pVal = sqlite3_column_value(pIter->pSelect, i);
209363 if( pIter->abTblPk[i] || c!='.' ){
209364 p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
209365 }
209366 }
209367 if( p->rc==SQLITE_OK0
209368 && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
209369 ){
209370 /* Bind the rbu_rowid value to column _rowid_ */
209371 assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
209372 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
209373 p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
209374 }
209375 if( p->rc==SQLITE_OK0 ){
209376 sqlite3_step(pUpdate);
209377 p->rc = resetAndCollectError(pUpdate, &p->zErrmsg);
209378 }
209379 }
209380 }
209381 }
209382 return p->rc;
209383}
209384
209385/*
209386** Increment the schema cookie of the main database opened by p->dbMain.
209387**
209388** Or, if this is an RBU vacuum, set the schema cookie of the main db
209389** opened by p->dbMain to one more than the schema cookie of the main
209390** db opened by p->dbRbu.
209391*/
209392static void rbuIncrSchemaCookie(sqlite3rbu *p){
209393 if( p->rc==SQLITE_OK0 ){
209394 sqlite3 *dbread = (rbuIsVacuum(p) ? p->dbRbu : p->dbMain);
209395 int iCookie = 1000000;
209396 sqlite3_stmt *pStmt;
209397
209398 p->rc = prepareAndCollectError(dbread, &pStmt, &p->zErrmsg,
209399 "PRAGMA schema_version"
209400 );
209401 if( p->rc==SQLITE_OK0 ){
209402 /* Coverage: it may be that this sqlite3_step() cannot fail. There
209403 ** is already a transaction open, so the prepared statement cannot
209404 ** throw an SQLITE_SCHEMA exception. The only database page the
209405 ** statement reads is page 1, which is guaranteed to be in the cache.
209406 ** And no memory allocations are required. */
209407 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
209408 iCookie = sqlite3_column_int(pStmt, 0);
209409 }
209410 rbuFinalize(p, pStmt);
209411 }
209412 if( p->rc==SQLITE_OK0 ){
209413 rbuMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
209414 }
209415 }
209416}
209417
209418/*
209419** Update the contents of the rbu_state table within the rbu database. The
209420** value stored in the RBU_STATE_STAGE column is eStage. All other values
209421** are determined by inspecting the rbu handle passed as the first argument.
209422*/
209423static void rbuSaveState(sqlite3rbu *p, int eStage){
209424 if( p->rc==SQLITE_OK0 || p->rc==SQLITE_DONE101 ){
209425 sqlite3_stmt *pInsert = 0;
209426 rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
209427 int rc;
209428
209429 assert( p->zErrmsg==0 )((void) (0));
209430 rc = prepareFreeAndCollectError(p->dbRbu, &pInsert, &p->zErrmsg,
209431 sqlite3_mprintf(
209432 "INSERT OR REPLACE INTO %s.rbu_state(k, v) VALUES "
209433 "(%d, %d), "
209434 "(%d, %Q), "
209435 "(%d, %Q), "
209436 "(%d, %d), "
209437 "(%d, %d), "
209438 "(%d, %lld), "
209439 "(%d, %lld), "
209440 "(%d, %lld), "
209441 "(%d, %lld), "
209442 "(%d, %Q) ",
209443 p->zStateDb,
209444 RBU_STATE_STAGE, eStage,
209445 RBU_STATE_TBL, p->objiter.zTbl,
209446 RBU_STATE_IDX, p->objiter.zIdx,
209447 RBU_STATE_ROW, p->nStep,
209448 RBU_STATE_PROGRESS, p->nProgress,
209449 RBU_STATE_CKPT, p->iWalCksum,
209450 RBU_STATE_COOKIE, (i64)pFd->iCookie,
209451 RBU_STATE_OALSZ, p->iOalSz,
209452 RBU_STATE_PHASEONESTEP, p->nPhaseOneStep,
209453 RBU_STATE_DATATBL, p->objiter.zDataTbl
209454 )
209455 );
209456 assert( pInsert==0 || rc==SQLITE_OK )((void) (0));
209457
209458 if( rc==SQLITE_OK0 ){
209459 sqlite3_step(pInsert);
209460 rc = sqlite3_finalize(pInsert);
209461 }
209462 if( rc!=SQLITE_OK0 ) p->rc = rc;
209463 }
209464}
209465
209466
209467/*
209468** The second argument passed to this function is the name of a PRAGMA
209469** setting - "page_size", "auto_vacuum", "user_version" or "application_id".
209470** This function executes the following on sqlite3rbu.dbRbu:
209471**
209472** "PRAGMA main.$zPragma"
209473**
209474** where $zPragma is the string passed as the second argument, then
209475** on sqlite3rbu.dbMain:
209476**
209477** "PRAGMA main.$zPragma = $val"
209478**
209479** where $val is the value returned by the first PRAGMA invocation.
209480**
209481** In short, it copies the value of the specified PRAGMA setting from
209482** dbRbu to dbMain.
209483*/
209484static void rbuCopyPragma(sqlite3rbu *p, const char *zPragma){
209485 if( p->rc==SQLITE_OK0 ){
209486 sqlite3_stmt *pPragma = 0;
209487 p->rc = prepareFreeAndCollectError(p->dbRbu, &pPragma, &p->zErrmsg,
209488 sqlite3_mprintf("PRAGMA main.%s", zPragma)
209489 );
209490 if( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pPragma) ){
209491 p->rc = rbuMPrintfExec(p, p->dbMain, "PRAGMA main.%s = %d",
209492 zPragma, sqlite3_column_int(pPragma, 0)
209493 );
209494 }
209495 rbuFinalize(p, pPragma);
209496 }
209497}
209498
209499/*
209500** The RBU handle passed as the only argument has just been opened and
209501** the state database is empty. If this RBU handle was opened for an
209502** RBU vacuum operation, create the schema in the target db.
209503*/
209504static void rbuCreateTargetSchema(sqlite3rbu *p){
209505 sqlite3_stmt *pSql = 0;
209506 sqlite3_stmt *pInsert = 0;
209507
209508 assert( rbuIsVacuum(p) )((void) (0));
209509 p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=1", 0,0, &p->zErrmsg);
209510 if( p->rc==SQLITE_OK0 ){
209511 p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
209512 "SELECT sql FROM sqlite_schema WHERE sql!='' AND rootpage!=0"
209513 " AND name!='sqlite_sequence' "
209514 " ORDER BY type DESC"
209515 );
209516 }
209517
209518 while( p->rc==SQLITE_OK0 && sqlite3_step(pSql)==SQLITE_ROW100 ){
209519 const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
209520 p->rc = sqlite3_exec(p->dbMain, zSql, 0, 0, &p->zErrmsg);
209521 }
209522 rbuFinalize(p, pSql);
209523 if( p->rc!=SQLITE_OK0 ) return;
209524
209525 if( p->rc==SQLITE_OK0 ){
209526 p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
209527 "SELECT * FROM sqlite_schema WHERE rootpage=0 OR rootpage IS NULL"
209528 );
209529 }
209530
209531 if( p->rc==SQLITE_OK0 ){
209532 p->rc = prepareAndCollectError(p->dbMain, &pInsert, &p->zErrmsg,
209533 "INSERT INTO sqlite_schema VALUES(?,?,?,?,?)"
209534 );
209535 }
209536
209537 while( p->rc==SQLITE_OK0 && sqlite3_step(pSql)==SQLITE_ROW100 ){
209538 int i;
209539 for(i=0; i<5; i++){
209540 sqlite3_bind_value(pInsert, i+1, sqlite3_column_value(pSql, i));
209541 }
209542 sqlite3_step(pInsert);
209543 p->rc = sqlite3_reset(pInsert);
209544 }
209545 if( p->rc==SQLITE_OK0 ){
209546 p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=0",0,0,&p->zErrmsg);
209547 }
209548
209549 rbuFinalize(p, pSql);
209550 rbuFinalize(p, pInsert);
209551}
209552
209553/*
209554** Step the RBU object.
209555*/
209556SQLITE_API int sqlite3rbu_step(sqlite3rbu *p){
209557 if( p ){
209558 switch( p->eStage ){
209559 case RBU_STAGE_OAL: {
209560 RbuObjIter *pIter = &p->objiter;
209561
209562 /* If this is an RBU vacuum operation and the state table was empty
209563 ** when this handle was opened, create the target database schema. */
209564 if( rbuIsVacuum(p) && p->nProgress==0 && p->rc==SQLITE_OK0 ){
209565 rbuCreateTargetSchema(p);
209566 rbuCopyPragma(p, "user_version");
209567 rbuCopyPragma(p, "application_id");
209568 }
209569
209570 while( p->rc==SQLITE_OK0 && pIter->zTbl ){
209571
209572 if( pIter->bCleanup ){
209573 /* Clean up the rbu_tmp_xxx table for the previous table. It
209574 ** cannot be dropped as there are currently active SQL statements.
209575 ** But the contents can be deleted. */
209576 if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
209577 rbuMPrintfExec(p, p->dbRbu,
209578 "DELETE FROM %s.'rbu_tmp_%q'", p->zStateDb, pIter->zDataTbl
209579 );
209580 }
209581 }else{
209582 rbuObjIterPrepareAll(p, pIter, 0);
209583
209584 /* Advance to the next row to process. */
209585 if( p->rc==SQLITE_OK0 ){
209586 int rc = sqlite3_step(pIter->pSelect);
209587 if( rc==SQLITE_ROW100 ){
209588 p->nProgress++;
209589 p->nStep++;
209590 return rbuStep(p);
209591 }
209592 p->rc = sqlite3_reset(pIter->pSelect);
209593 p->nStep = 0;
209594 }
209595 }
209596
209597 rbuObjIterNext(p, pIter);
209598 }
209599
209600 if( p->rc==SQLITE_OK0 ){
209601 assert( pIter->zTbl==0 )((void) (0));
209602 rbuSaveState(p, RBU_STAGE_MOVE);
209603 rbuIncrSchemaCookie(p);
209604 if( p->rc==SQLITE_OK0 ){
209605 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
209606 }
209607 if( p->rc==SQLITE_OK0 ){
209608 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
209609 }
209610 p->eStage = RBU_STAGE_MOVE;
209611 }
209612 break;
209613 }
209614
209615 case RBU_STAGE_MOVE: {
209616 if( p->rc==SQLITE_OK0 ){
209617 rbuMoveOalFile(p);
209618 p->nProgress++;
209619 }
209620 break;
209621 }
209622
209623 case RBU_STAGE_CKPT: {
209624 if( p->rc==SQLITE_OK0 ){
209625 if( p->nStep>=p->nFrame ){
209626 sqlite3_file *pDb = p->pTargetFd->pReal;
209627
209628 /* Sync the db file */
209629 p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL0x00002);
209630
209631 /* Update nBackfill */
209632 if( p->rc==SQLITE_OK0 ){
209633 void volatile *ptr;
209634 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, &ptr);
209635 if( p->rc==SQLITE_OK0 ){
209636 ((u32 volatile*)ptr)[24] = p->iMaxFrame;
209637 }
209638 }
209639
209640 if( p->rc==SQLITE_OK0 ){
209641 p->eStage = RBU_STAGE_DONE;
209642 p->rc = SQLITE_DONE101;
209643 }
209644 }else{
209645 /* At one point the following block copied a single frame from the
209646 ** wal file to the database file. So that one call to sqlite3rbu_step()
209647 ** checkpointed a single frame.
209648 **
209649 ** However, if the sector-size is larger than the page-size, and the
209650 ** application calls sqlite3rbu_savestate() or close() immediately
209651 ** after this step, then rbu_step() again, then a power failure occurs,
209652 ** then the database page written here may be damaged. Work around
209653 ** this by checkpointing frames until the next page in the aFrame[]
209654 ** lies on a different disk sector to the current one. */
209655 u32 iSector;
209656 do{
209657 RbuFrame *pFrame = &p->aFrame[p->nStep];
209658 iSector = (pFrame->iDbPage-1) / p->nPagePerSector;
209659 rbuCheckpointFrame(p, pFrame);
209660 p->nStep++;
209661 }while( p->nStep<p->nFrame
209662 && iSector==((p->aFrame[p->nStep].iDbPage-1) / p->nPagePerSector)
209663 && p->rc==SQLITE_OK0
209664 );
209665 }
209666 p->nProgress++;
209667 }
209668 break;
209669 }
209670
209671 default:
209672 break;
209673 }
209674 return p->rc;
209675 }else{
209676 return SQLITE_NOMEM7;
209677 }
209678}
209679
209680/*
209681** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
209682** otherwise. Either or both argument may be NULL. Two NULL values are
209683** considered equal, and NULL is considered distinct from all other values.
209684*/
209685static int rbuStrCompare(const char *z1, const char *z2){
209686 if( z1==0 && z2==0 ) return 0;
209687 if( z1==0 || z2==0 ) return 1;
209688 return (sqlite3_stricmp(z1, z2)!=0);
209689}
209690
209691/*
209692** This function is called as part of sqlite3rbu_open() when initializing
209693** an rbu handle in OAL stage. If the rbu update has not started (i.e.
209694** the rbu_state table was empty) it is a no-op. Otherwise, it arranges
209695** things so that the next call to sqlite3rbu_step() continues on from
209696** where the previous rbu handle left off.
209697**
209698** If an error occurs, an error code and error message are left in the
209699** rbu handle passed as the first argument.
209700*/
209701static void rbuSetupOal(sqlite3rbu *p, RbuState *pState){
209702 assert( p->rc==SQLITE_OK )((void) (0));
209703 if( pState->zTbl ){
209704 RbuObjIter *pIter = &p->objiter;
209705 int rc = SQLITE_OK0;
209706
209707 while( rc==SQLITE_OK0 && pIter->zTbl && (pIter->bCleanup
209708 || rbuStrCompare(pIter->zIdx, pState->zIdx)
209709 || (pState->zDataTbl==0 && rbuStrCompare(pIter->zTbl, pState->zTbl))
209710 || (pState->zDataTbl && rbuStrCompare(pIter->zDataTbl, pState->zDataTbl))
209711 )){
209712 rc = rbuObjIterNext(p, pIter);
209713 }
209714
209715 if( rc==SQLITE_OK0 && !pIter->zTbl ){
209716 rc = SQLITE_ERROR1;
209717 p->zErrmsg = sqlite3_mprintf("rbu_state mismatch error");
209718 }
209719
209720 if( rc==SQLITE_OK0 ){
209721 p->nStep = pState->nRow;
209722 rc = rbuObjIterPrepareAll(p, &p->objiter, p->nStep);
209723 }
209724
209725 p->rc = rc;
209726 }
209727}
209728
209729/*
209730** If there is a "*-oal" file in the file-system corresponding to the
209731** target database in the file-system, delete it. If an error occurs,
209732** leave an error code and error message in the rbu handle.
209733*/
209734static void rbuDeleteOalFile(sqlite3rbu *p){
209735 char *zOal = rbuMPrintf(p, "%s-oal", p->zTarget);
209736 if( zOal ){
209737 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
209738 assert( pVfs && p->rc==SQLITE_OK && p->zErrmsg==0 )((void) (0));
209739 pVfs->xDelete(pVfs, zOal, 0);
209740 sqlite3_free(zOal);
209741 }
209742}
209743
209744/*
209745** Allocate a private rbu VFS for the rbu handle passed as the only
209746** argument. This VFS will be used unless the call to sqlite3rbu_open()
209747** specified a URI with a vfs=? option in place of a target database
209748** file name.
209749*/
209750static void rbuCreateVfs(sqlite3rbu *p){
209751 int rnd;
209752 char zRnd[64];
209753
209754 assert( p->rc==SQLITE_OK )((void) (0));
209755 sqlite3_randomness(sizeof(int), (void*)&rnd);
209756 sqlite3_snprintf(sizeof(zRnd), zRnd, "rbu_vfs_%d", rnd);
209757 p->rc = sqlite3rbu_create_vfs(zRnd, 0);
209758 if( p->rc==SQLITE_OK0 ){
209759 sqlite3_vfs *pVfs = sqlite3_vfs_find(zRnd);
209760 assert( pVfs )((void) (0));
209761 p->zVfsName = pVfs->zName;
209762 ((rbu_vfs*)pVfs)->pRbu = p;
209763 }
209764}
209765
209766/*
209767** Destroy the private VFS created for the rbu handle passed as the only
209768** argument by an earlier call to rbuCreateVfs().
209769*/
209770static void rbuDeleteVfs(sqlite3rbu *p){
209771 if( p->zVfsName ){
209772 sqlite3rbu_destroy_vfs(p->zVfsName);
209773 p->zVfsName = 0;
209774 }
209775}
209776
209777/*
209778** This user-defined SQL function is invoked with a single argument - the
209779** name of a table expected to appear in the target database. It returns
209780** the number of auxilliary indexes on the table.
209781*/
209782static void rbuIndexCntFunc(
209783 sqlite3_context *pCtx,
209784 int nVal,
209785 sqlite3_value **apVal
209786){
209787 sqlite3rbu *p = (sqlite3rbu*)sqlite3_user_data(pCtx);
209788 sqlite3_stmt *pStmt = 0;
209789 char *zErrmsg = 0;
209790 int rc;
209791 sqlite3 *db = (rbuIsVacuum(p) ? p->dbRbu : p->dbMain);
209792
209793 assert( nVal==1 )((void) (0));
209794
209795 rc = prepareFreeAndCollectError(db, &pStmt, &zErrmsg,
209796 sqlite3_mprintf("SELECT count(*) FROM sqlite_schema "
209797 "WHERE type='index' AND tbl_name = %Q", sqlite3_value_text(apVal[0]))
209798 );
209799 if( rc!=SQLITE_OK0 ){
209800 sqlite3_result_error(pCtx, zErrmsg, -1);
209801 }else{
209802 int nIndex = 0;
209803 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
209804 nIndex = sqlite3_column_int(pStmt, 0);
209805 }
209806 rc = sqlite3_finalize(pStmt);
209807 if( rc==SQLITE_OK0 ){
209808 sqlite3_result_int(pCtx, nIndex);
209809 }else{
209810 sqlite3_result_error(pCtx, sqlite3_errmsg(db), -1);
209811 }
209812 }
209813
209814 sqlite3_free(zErrmsg);
209815}
209816
209817/*
209818** If the RBU database contains the rbu_count table, use it to initialize
209819** the sqlite3rbu.nPhaseOneStep variable. The schema of the rbu_count table
209820** is assumed to contain the same columns as:
209821**
209822** CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
209823**
209824** There should be one row in the table for each data_xxx table in the
209825** database. The 'tbl' column should contain the name of a data_xxx table,
209826** and the cnt column the number of rows it contains.
209827**
209828** sqlite3rbu.nPhaseOneStep is initialized to the sum of (1 + nIndex) * cnt
209829** for all rows in the rbu_count table, where nIndex is the number of
209830** indexes on the corresponding target database table.
209831*/
209832static void rbuInitPhaseOneSteps(sqlite3rbu *p){
209833 if( p->rc==SQLITE_OK0 ){
209834 sqlite3_stmt *pStmt = 0;
209835 int bExists = 0; /* True if rbu_count exists */
209836
209837 p->nPhaseOneStep = -1;
209838
209839 p->rc = sqlite3_create_function(p->dbRbu,
209840 "rbu_index_cnt", 1, SQLITE_UTF81, (void*)p, rbuIndexCntFunc, 0, 0
209841 );
209842
209843 /* Check for the rbu_count table. If it does not exist, or if an error
209844 ** occurs, nPhaseOneStep will be left set to -1. */
209845 if( p->rc==SQLITE_OK0 ){
209846 p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
209847 "SELECT 1 FROM sqlite_schema WHERE tbl_name = 'rbu_count'"
209848 );
209849 }
209850 if( p->rc==SQLITE_OK0 ){
209851 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
209852 bExists = 1;
209853 }
209854 p->rc = sqlite3_finalize(pStmt);
209855 }
209856
209857 if( p->rc==SQLITE_OK0 && bExists ){
209858 p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
209859 "SELECT sum(cnt * (1 + rbu_index_cnt(rbu_target_name(tbl))))"
209860 "FROM rbu_count"
209861 );
209862 if( p->rc==SQLITE_OK0 ){
209863 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
209864 p->nPhaseOneStep = sqlite3_column_int64(pStmt, 0);
209865 }
209866 p->rc = sqlite3_finalize(pStmt);
209867 }
209868 }
209869 }
209870}
209871
209872
209873static sqlite3rbu *openRbuHandle(
209874 const char *zTarget,
209875 const char *zRbu,
209876 const char *zState
209877){
209878 sqlite3rbu *p;
209879 size_t nTarget = zTarget ? strlen(zTarget) : 0;
209880 size_t nRbu = strlen(zRbu);
209881 size_t nByte = sizeof(sqlite3rbu) + nTarget+1 + nRbu+1;
209882
209883 p = (sqlite3rbu*)sqlite3_malloc64(nByte);
209884 if( p ){
209885 RbuState *pState = 0;
209886
209887 /* Create the custom VFS. */
209888 memset(p, 0, sizeof(sqlite3rbu));
209889 rbuCreateVfs(p);
209890
209891 /* Open the target, RBU and state databases */
209892 if( p->rc==SQLITE_OK0 ){
209893 char *pCsr = (char*)&p[1];
209894 int bRetry = 0;
209895 if( zTarget ){
209896 p->zTarget = pCsr;
209897 memcpy(p->zTarget, zTarget, nTarget+1);
209898 pCsr += nTarget+1;
209899 }
209900 p->zRbu = pCsr;
209901 memcpy(p->zRbu, zRbu, nRbu+1);
209902 pCsr += nRbu+1;
209903 if( zState ){
209904 p->zState = rbuMPrintf(p, "%s", zState);
209905 }
209906
209907 /* If the first attempt to open the database file fails and the bRetry
209908 ** flag it set, this means that the db was not opened because it seemed
209909 ** to be a wal-mode db. But, this may have happened due to an earlier
209910 ** RBU vacuum operation leaving an old wal file in the directory.
209911 ** If this is the case, it will have been checkpointed and deleted
209912 ** when the handle was closed and a second attempt to open the
209913 ** database may succeed. */
209914 rbuOpenDatabase(p, 0, &bRetry);
209915 if( bRetry ){
209916 rbuOpenDatabase(p, 0, 0);
209917 }
209918 }
209919
209920 if( p->rc==SQLITE_OK0 ){
209921 pState = rbuLoadState(p);
209922 assert( pState || p->rc!=SQLITE_OK )((void) (0));
209923 if( p->rc==SQLITE_OK0 ){
209924
209925 if( pState->eStage==0 ){
209926 rbuDeleteOalFile(p);
209927 rbuInitPhaseOneSteps(p);
209928 p->eStage = RBU_STAGE_OAL;
209929 }else{
209930 p->eStage = pState->eStage;
209931 p->nPhaseOneStep = pState->nPhaseOneStep;
209932 }
209933 p->nProgress = pState->nProgress;
209934 p->iOalSz = pState->iOalSz;
209935 }
209936 }
209937 assert( p->rc!=SQLITE_OK || p->eStage!=0 )((void) (0));
209938
209939 if( p->rc==SQLITE_OK0 && p->pTargetFd->pWalFd ){
209940 if( p->eStage==RBU_STAGE_OAL ){
209941 p->rc = SQLITE_ERROR1;
209942 p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
209943 }else if( p->eStage==RBU_STAGE_MOVE ){
209944 p->eStage = RBU_STAGE_CKPT;
209945 p->nStep = 0;
209946 }
209947 }
209948
209949 if( p->rc==SQLITE_OK0
209950 && (p->eStage==RBU_STAGE_OAL || p->eStage==RBU_STAGE_MOVE)
209951 && pState->eStage!=0
209952 ){
209953 rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
209954 if( pFd->iCookie!=pState->iCookie ){
209955 /* At this point (pTargetFd->iCookie) contains the value of the
209956 ** change-counter cookie (the thing that gets incremented when a
209957 ** transaction is committed in rollback mode) currently stored on
209958 ** page 1 of the database file. */
209959 p->rc = SQLITE_BUSY5;
209960 p->zErrmsg = sqlite3_mprintf("database modified during rbu %s",
209961 (rbuIsVacuum(p) ? "vacuum" : "update")
209962 );
209963 }
209964 }
209965
209966 if( p->rc==SQLITE_OK0 ){
209967 if( p->eStage==RBU_STAGE_OAL ){
209968 sqlite3 *db = p->dbMain;
209969 p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, &p->zErrmsg);
209970
209971 /* Point the object iterator at the first object */
209972 if( p->rc==SQLITE_OK0 ){
209973 p->rc = rbuObjIterFirst(p, &p->objiter);
209974 }
209975
209976 /* If the RBU database contains no data_xxx tables, declare the RBU
209977 ** update finished. */
209978 if( p->rc==SQLITE_OK0 && p->objiter.zTbl==0 ){
209979 p->rc = SQLITE_DONE101;
209980 p->eStage = RBU_STAGE_DONE;
209981 }else{
209982 if( p->rc==SQLITE_OK0 && pState->eStage==0 && rbuIsVacuum(p) ){
209983 rbuCopyPragma(p, "page_size");
209984 rbuCopyPragma(p, "auto_vacuum");
209985 }
209986
209987 /* Open transactions both databases. The *-oal file is opened or
209988 ** created at this point. */
209989 if( p->rc==SQLITE_OK0 ){
209990 p->rc = sqlite3_exec(db, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
209991 }
209992
209993 /* Check if the main database is a zipvfs db. If it is, set the upper
209994 ** level pager to use "journal_mode=off". This prevents it from
209995 ** generating a large journal using a temp file. */
209996 if( p->rc==SQLITE_OK0 ){
209997 int frc = sqlite3_file_control(db, "main", SQLITE_FCNTL_ZIPVFS25, 0);
209998 if( frc==SQLITE_OK0 ){
209999 p->rc = sqlite3_exec(
210000 db, "PRAGMA journal_mode=off",0,0,&p->zErrmsg);
210001 }
210002 }
210003
210004 if( p->rc==SQLITE_OK0 ){
210005 rbuSetupOal(p, pState);
210006 }
210007 }
210008 }else if( p->eStage==RBU_STAGE_MOVE ){
210009 /* no-op */
210010 }else if( p->eStage==RBU_STAGE_CKPT ){
210011 if( !rbuIsVacuum(p) && rbuExclusiveCheckpoint(p->dbMain) ){
210012 /* If the rbu_exclusive_checkpoint=1 URI parameter was specified
210013 ** and an incremental checkpoint is being resumed, attempt an
210014 ** exclusive lock on the db file. If this fails, so be it. */
210015 p->eStage = RBU_STAGE_DONE;
210016 rbuLockDatabase(p->dbMain);
210017 p->eStage = RBU_STAGE_CKPT;
210018 }
210019 rbuSetupCheckpoint(p, pState);
210020 }else if( p->eStage==RBU_STAGE_DONE ){
210021 p->rc = SQLITE_DONE101;
210022 }else{
210023 p->rc = SQLITE_CORRUPT11;
210024 }
210025 }
210026
210027 rbuFreeState(pState);
210028 }
210029
210030 return p;
210031}
210032
210033/*
210034** Allocate and return an RBU handle with all fields zeroed except for the
210035** error code, which is set to SQLITE_MISUSE.
210036*/
210037static sqlite3rbu *rbuMisuseError(void){
210038 sqlite3rbu *pRet;
210039 pRet = sqlite3_malloc64(sizeof(sqlite3rbu));
210040 if( pRet ){
210041 memset(pRet, 0, sizeof(sqlite3rbu));
210042 pRet->rc = SQLITE_MISUSE21;
210043 }
210044 return pRet;
210045}
210046
210047/*
210048** Open and return a new RBU handle.
210049*/
210050SQLITE_API sqlite3rbu *sqlite3rbu_open(
210051 const char *zTarget,
210052 const char *zRbu,
210053 const char *zState
210054){
210055 if( zTarget==0 || zRbu==0 ){ return rbuMisuseError(); }
210056 return openRbuHandle(zTarget, zRbu, zState);
210057}
210058
210059/*
210060** Open a handle to begin or resume an RBU VACUUM operation.
210061*/
210062SQLITE_API sqlite3rbu *sqlite3rbu_vacuum(
210063 const char *zTarget,
210064 const char *zState
210065){
210066 if( zTarget==0 ){ return rbuMisuseError(); }
210067 if( zState ){
210068 int n = strlen(zState);
210069 if( n>=7 && 0==memcmp("-vactmp", &zState[n-7], 7) ){
210070 return rbuMisuseError();
210071 }
210072 }
210073 /* TODO: Check that both arguments are non-NULL */
210074 return openRbuHandle(0, zTarget, zState);
210075}
210076
210077/*
210078** Return the database handle used by pRbu.
210079*/
210080SQLITE_API sqlite3 *sqlite3rbu_db(sqlite3rbu *pRbu, int bRbu){
210081 sqlite3 *db = 0;
210082 if( pRbu ){
210083 db = (bRbu ? pRbu->dbRbu : pRbu->dbMain);
210084 }
210085 return db;
210086}
210087
210088
210089/*
210090** If the error code currently stored in the RBU handle is SQLITE_CONSTRAINT,
210091** then edit any error message string so as to remove all occurrences of
210092** the pattern "rbu_imp_[0-9]*".
210093*/
210094static void rbuEditErrmsg(sqlite3rbu *p){
210095 if( p->rc==SQLITE_CONSTRAINT19 && p->zErrmsg ){
210096 unsigned int i;
210097 size_t nErrmsg = strlen(p->zErrmsg);
210098 for(i=0; i<(nErrmsg-8); i++){
210099 if( memcmp(&p->zErrmsg[i], "rbu_imp_", 8)==0 ){
210100 int nDel = 8;
210101 while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
210102 memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
210103 nErrmsg -= nDel;
210104 }
210105 }
210106 }
210107}
210108
210109/*
210110** Close the RBU handle.
210111*/
210112SQLITE_API int sqlite3rbu_close(sqlite3rbu *p, char **pzErrmsg){
210113 int rc;
210114 if( p ){
210115
210116 /* Commit the transaction to the *-oal file. */
210117 if( p->rc==SQLITE_OK0 && p->eStage==RBU_STAGE_OAL ){
210118 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
210119 }
210120
210121 /* Sync the db file if currently doing an incremental checkpoint */
210122 if( p->rc==SQLITE_OK0 && p->eStage==RBU_STAGE_CKPT ){
210123 sqlite3_file *pDb = p->pTargetFd->pReal;
210124 p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL0x00002);
210125 }
210126
210127 rbuSaveState(p, p->eStage);
210128
210129 if( p->rc==SQLITE_OK0 && p->eStage==RBU_STAGE_OAL ){
210130 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
210131 }
210132
210133 /* Close any open statement handles. */
210134 rbuObjIterFinalize(&p->objiter);
210135
210136 /* If this is an RBU vacuum handle and the vacuum has either finished
210137 ** successfully or encountered an error, delete the contents of the
210138 ** state table. This causes the next call to sqlite3rbu_vacuum()
210139 ** specifying the current target and state databases to start a new
210140 ** vacuum from scratch. */
210141 if( rbuIsVacuum(p) && p->rc!=SQLITE_OK0 && p->dbRbu ){
210142 int rc2 = sqlite3_exec(p->dbRbu, "DELETE FROM stat.rbu_state", 0, 0, 0);
210143 if( p->rc==SQLITE_DONE101 && rc2!=SQLITE_OK0 ) p->rc = rc2;
210144 }
210145
210146 /* Close the open database handle and VFS object. */
210147 sqlite3_close(p->dbRbu);
210148 sqlite3_close(p->dbMain);
210149 assert( p->szTemp==0 )((void) (0));
210150 rbuDeleteVfs(p);
210151 sqlite3_free(p->aBuf);
210152 sqlite3_free(p->aFrame);
210153
210154 rbuEditErrmsg(p);
210155 rc = p->rc;
210156 if( pzErrmsg ){
210157 *pzErrmsg = p->zErrmsg;
210158 }else{
210159 sqlite3_free(p->zErrmsg);
210160 }
210161 sqlite3_free(p->zState);
210162 sqlite3_free(p);
210163 }else{
210164 rc = SQLITE_NOMEM7;
210165 *pzErrmsg = 0;
210166 }
210167 return rc;
210168}
210169
210170/*
210171** Return the total number of key-value operations (inserts, deletes or
210172** updates) that have been performed on the target database since the
210173** current RBU update was started.
210174*/
210175SQLITE_API sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu){
210176 return pRbu->nProgress;
210177}
210178
210179/*
210180** Return permyriadage progress indications for the two main stages of
210181** an RBU update.
210182*/
210183SQLITE_API void sqlite3rbu_bp_progress(sqlite3rbu *p, int *pnOne, int *pnTwo){
210184 const int MAX_PROGRESS = 10000;
210185 switch( p->eStage ){
210186 case RBU_STAGE_OAL:
210187 if( p->nPhaseOneStep>0 ){
210188 *pnOne = (int)(MAX_PROGRESS * (i64)p->nProgress/(i64)p->nPhaseOneStep);
210189 }else{
210190 *pnOne = -1;
210191 }
210192 *pnTwo = 0;
210193 break;
210194
210195 case RBU_STAGE_MOVE:
210196 *pnOne = MAX_PROGRESS;
210197 *pnTwo = 0;
210198 break;
210199
210200 case RBU_STAGE_CKPT:
210201 *pnOne = MAX_PROGRESS;
210202 *pnTwo = (int)(MAX_PROGRESS * (i64)p->nStep / (i64)p->nFrame);
210203 break;
210204
210205 case RBU_STAGE_DONE:
210206 *pnOne = MAX_PROGRESS;
210207 *pnTwo = MAX_PROGRESS;
210208 break;
210209
210210 default:
210211 assert( 0 )((void) (0));
210212 }
210213}
210214
210215/*
210216** Return the current state of the RBU vacuum or update operation.
210217*/
210218SQLITE_API int sqlite3rbu_state(sqlite3rbu *p){
210219 int aRes[] = {
210220 0, SQLITE_RBU_STATE_OAL, SQLITE_RBU_STATE_MOVE,
210221 0, SQLITE_RBU_STATE_CHECKPOINT, SQLITE_RBU_STATE_DONE
210222 };
210223
210224 assert( RBU_STAGE_OAL==1 )((void) (0));
210225 assert( RBU_STAGE_MOVE==2 )((void) (0));
210226 assert( RBU_STAGE_CKPT==4 )((void) (0));
210227 assert( RBU_STAGE_DONE==5 )((void) (0));
210228 assert( aRes[RBU_STAGE_OAL]==SQLITE_RBU_STATE_OAL )((void) (0));
210229 assert( aRes[RBU_STAGE_MOVE]==SQLITE_RBU_STATE_MOVE )((void) (0));
210230 assert( aRes[RBU_STAGE_CKPT]==SQLITE_RBU_STATE_CHECKPOINT )((void) (0));
210231 assert( aRes[RBU_STAGE_DONE]==SQLITE_RBU_STATE_DONE )((void) (0));
210232
210233 if( p->rc!=SQLITE_OK0 && p->rc!=SQLITE_DONE101 ){
210234 return SQLITE_RBU_STATE_ERROR;
210235 }else{
210236 assert( p->rc!=SQLITE_DONE || p->eStage==RBU_STAGE_DONE )((void) (0));
210237 assert( p->eStage==RBU_STAGE_OAL((void) (0))
210238 || p->eStage==RBU_STAGE_MOVE((void) (0))
210239 || p->eStage==RBU_STAGE_CKPT((void) (0))
210240 || p->eStage==RBU_STAGE_DONE((void) (0))
210241 )((void) (0));
210242 return aRes[p->eStage];
210243 }
210244}
210245
210246SQLITE_API int sqlite3rbu_savestate(sqlite3rbu *p){
210247 int rc = p->rc;
210248 if( rc==SQLITE_DONE101 ) return SQLITE_OK0;
210249
210250 assert( p->eStage>=RBU_STAGE_OAL && p->eStage<=RBU_STAGE_DONE )((void) (0));
210251 if( p->eStage==RBU_STAGE_OAL ){
210252 assert( rc!=SQLITE_DONE )((void) (0));
210253 if( rc==SQLITE_OK0 ) rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, 0);
210254 }
210255
210256 /* Sync the db file */
210257 if( rc==SQLITE_OK0 && p->eStage==RBU_STAGE_CKPT ){
210258 sqlite3_file *pDb = p->pTargetFd->pReal;
210259 rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL0x00002);
210260 }
210261
210262 p->rc = rc;
210263 rbuSaveState(p, p->eStage);
210264 rc = p->rc;
210265
210266 if( p->eStage==RBU_STAGE_OAL ){
210267 assert( rc!=SQLITE_DONE )((void) (0));
210268 if( rc==SQLITE_OK0 ) rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
210269 if( rc==SQLITE_OK0 ){
210270 const char *zBegin = rbuIsVacuum(p) ? "BEGIN" : "BEGIN IMMEDIATE";
210271 rc = sqlite3_exec(p->dbRbu, zBegin, 0, 0, 0);
210272 }
210273 if( rc==SQLITE_OK0 ) rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0,0);
210274 }
210275
210276 p->rc = rc;
210277 return rc;
210278}
210279
210280/**************************************************************************
210281** Beginning of RBU VFS shim methods. The VFS shim modifies the behaviour
210282** of a standard VFS in the following ways:
210283**
210284** 1. Whenever the first page of a main database file is read or
210285** written, the value of the change-counter cookie is stored in
210286** rbu_file.iCookie. Similarly, the value of the "write-version"
210287** database header field is stored in rbu_file.iWriteVer. This ensures
210288** that the values are always trustworthy within an open transaction.
210289**
210290** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (rbu_file.pWalFd)
210291** member variable of the associated database file descriptor is set
210292** to point to the new file. A mutex protected linked list of all main
210293** db fds opened using a particular RBU VFS is maintained at
210294** rbu_vfs.pMain to facilitate this.
210295**
210296** 3. Using a new file-control "SQLITE_FCNTL_RBU", a main db rbu_file
210297** object can be marked as the target database of an RBU update. This
210298** turns on the following extra special behaviour:
210299**
210300** 3a. If xAccess() is called to check if there exists a *-wal file
210301** associated with an RBU target database currently in RBU_STAGE_OAL
210302** stage (preparing the *-oal file), the following special handling
210303** applies:
210304**
210305** * if the *-wal file does exist, return SQLITE_CANTOPEN. An RBU
210306** target database may not be in wal mode already.
210307**
210308** * if the *-wal file does not exist, set the output parameter to
210309** non-zero (to tell SQLite that it does exist) anyway.
210310**
210311** Then, when xOpen() is called to open the *-wal file associated with
210312** the RBU target in RBU_STAGE_OAL stage, instead of opening the *-wal
210313** file, the rbu vfs opens the corresponding *-oal file instead.
210314**
210315** 3b. The *-shm pages returned by xShmMap() for a target db file in
210316** RBU_STAGE_OAL mode are actually stored in heap memory. This is to
210317** avoid creating a *-shm file on disk. Additionally, xShmLock() calls
210318** are no-ops on target database files in RBU_STAGE_OAL mode. This is
210319** because assert() statements in some VFS implementations fail if
210320** xShmLock() is called before xShmMap().
210321**
210322** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
210323** mode except RBU_STAGE_DONE (all work completed and checkpointed), it
210324** fails with an SQLITE_BUSY error. This is to stop RBU connections
210325** from automatically checkpointing a *-wal (or *-oal) file from within
210326** sqlite3_close().
210327**
210328** 3d. In RBU_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
210329** all xWrite() calls on the target database file perform no IO.
210330** Instead the frame and page numbers that would be read and written
210331** are recorded. Additionally, successful attempts to obtain exclusive
210332** xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target
210333** database file are recorded. xShmLock() calls to unlock the same
210334** locks are no-ops (so that once obtained, these locks are never
210335** relinquished). Finally, calls to xSync() on the target database
210336** file fail with SQLITE_INTERNAL errors.
210337*/
210338
210339static void rbuUnlockShm(rbu_file *p){
210340 assert( p->openFlags & SQLITE_OPEN_MAIN_DB )((void) (0));
210341 if( p->pRbu ){
210342 int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
210343 int i;
210344 for(i=0; i<SQLITE_SHM_NLOCK8;i++){
210345 if( (1<<i) & p->pRbu->mLock ){
210346 xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK1|SQLITE_SHM_EXCLUSIVE8);
210347 }
210348 }
210349 p->pRbu->mLock = 0;
210350 }
210351}
210352
210353/*
210354*/
210355static int rbuUpdateTempSize(rbu_file *pFd, sqlite3_int64 nNew){
210356 sqlite3rbu *pRbu = pFd->pRbu;
210357 i64 nDiff = nNew - pFd->sz;
210358 pRbu->szTemp += nDiff;
210359 pFd->sz = nNew;
210360 assert( pRbu->szTemp>=0 )((void) (0));
210361 if( pRbu->szTempLimit && pRbu->szTemp>pRbu->szTempLimit ) return SQLITE_FULL13;
210362 return SQLITE_OK0;
210363}
210364
210365/*
210366** Add an item to the main-db lists, if it is not already present.
210367**
210368** There are two main-db lists. One for all file descriptors, and one
210369** for all file descriptors with rbu_file.pDb!=0. If the argument has
210370** rbu_file.pDb!=0, then it is assumed to already be present on the
210371** main list and is only added to the pDb!=0 list.
210372*/
210373static void rbuMainlistAdd(rbu_file *p){
210374 rbu_vfs *pRbuVfs = p->pRbuVfs;
210375 rbu_file *pIter;
210376 assert( (p->openFlags & SQLITE_OPEN_MAIN_DB) )((void) (0));
210377 sqlite3_mutex_enter(pRbuVfs->mutex);
210378 if( p->pRbu==0 ){
210379 for(pIter=pRbuVfs->pMain; pIter; pIter=pIter->pMainNext);
210380 p->pMainNext = pRbuVfs->pMain;
210381 pRbuVfs->pMain = p;
210382 }else{
210383 for(pIter=pRbuVfs->pMainRbu; pIter && pIter!=p; pIter=pIter->pMainRbuNext){}
210384 if( pIter==0 ){
210385 p->pMainRbuNext = pRbuVfs->pMainRbu;
210386 pRbuVfs->pMainRbu = p;
210387 }
210388 }
210389 sqlite3_mutex_leave(pRbuVfs->mutex);
210390}
210391
210392/*
210393** Remove an item from the main-db lists.
210394*/
210395static void rbuMainlistRemove(rbu_file *p){
210396 rbu_file **pp;
210397 sqlite3_mutex_enter(p->pRbuVfs->mutex);
210398 for(pp=&p->pRbuVfs->pMain; *pp && *pp!=p; pp=&((*pp)->pMainNext)){}
210399 if( *pp ) *pp = p->pMainNext;
210400 p->pMainNext = 0;
210401 for(pp=&p->pRbuVfs->pMainRbu; *pp && *pp!=p; pp=&((*pp)->pMainRbuNext)){}
210402 if( *pp ) *pp = p->pMainRbuNext;
210403 p->pMainRbuNext = 0;
210404 sqlite3_mutex_leave(p->pRbuVfs->mutex);
210405}
210406
210407/*
210408** Given that zWal points to a buffer containing a wal file name passed to
210409** either the xOpen() or xAccess() VFS method, search the main-db list for
210410** a file-handle opened by the same database connection on the corresponding
210411** database file.
210412**
210413** If parameter bRbu is true, only search for file-descriptors with
210414** rbu_file.pDb!=0.
210415*/
210416static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal, int bRbu){
210417 rbu_file *pDb;
210418 sqlite3_mutex_enter(pRbuVfs->mutex);
210419 if( bRbu ){
210420 for(pDb=pRbuVfs->pMainRbu; pDb && pDb->zWal!=zWal; pDb=pDb->pMainRbuNext){}
210421 }else{
210422 for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext){}
210423 }
210424 sqlite3_mutex_leave(pRbuVfs->mutex);
210425 return pDb;
210426}
210427
210428/*
210429** Close an rbu file.
210430*/
210431static int rbuVfsClose(sqlite3_file *pFile){
210432 rbu_file *p = (rbu_file*)pFile;
210433 int rc;
210434 int i;
210435
210436 /* Free the contents of the apShm[] array. And the array itself. */
210437 for(i=0; i<p->nShm; i++){
210438 sqlite3_free(p->apShm[i]);
210439 }
210440 sqlite3_free(p->apShm);
210441 p->apShm = 0;
210442 sqlite3_free(p->zDel);
210443
210444 if( p->openFlags & SQLITE_OPEN_MAIN_DB0x00000100 ){
210445 rbuMainlistRemove(p);
210446 rbuUnlockShm(p);
210447 p->pReal->pMethods->xShmUnmap(p->pReal, 0);
210448 }
210449 else if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE0x00000008) && p->pRbu ){
210450 rbuUpdateTempSize(p, 0);
210451 }
210452 assert( p->pMainNext==0 && p->pRbuVfs->pMain!=p )((void) (0));
210453
210454 /* Close the underlying file handle */
210455 rc = p->pReal->pMethods->xClose(p->pReal);
210456 return rc;
210457}
210458
210459
210460/*
210461** Read and return an unsigned 32-bit big-endian integer from the buffer
210462** passed as the only argument.
210463*/
210464static u32 rbuGetU32(u8 *aBuf){
210465 return ((u32)aBuf[0] << 24)
210466 + ((u32)aBuf[1] << 16)
210467 + ((u32)aBuf[2] << 8)
210468 + ((u32)aBuf[3]);
210469}
210470
210471/*
210472** Write an unsigned 32-bit value in big-endian format to the supplied
210473** buffer.
210474*/
210475static void rbuPutU32(u8 *aBuf, u32 iVal){
210476 aBuf[0] = (iVal >> 24) & 0xFF;
210477 aBuf[1] = (iVal >> 16) & 0xFF;
210478 aBuf[2] = (iVal >> 8) & 0xFF;
210479 aBuf[3] = (iVal >> 0) & 0xFF;
210480}
210481
210482static void rbuPutU16(u8 *aBuf, u16 iVal){
210483 aBuf[0] = (iVal >> 8) & 0xFF;
210484 aBuf[1] = (iVal >> 0) & 0xFF;
210485}
210486
210487/*
210488** Read data from an rbuVfs-file.
210489*/
210490static int rbuVfsRead(
210491 sqlite3_file *pFile,
210492 void *zBuf,
210493 int iAmt,
210494 sqlite_int64 iOfst
210495){
210496 rbu_file *p = (rbu_file*)pFile;
210497 sqlite3rbu *pRbu = p->pRbu;
210498 int rc;
210499
210500 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
210501 assert( p->openFlags & SQLITE_OPEN_WAL )((void) (0));
210502 rc = rbuCaptureWalRead(p->pRbu, iOfst, iAmt);
210503 }else{
210504 if( pRbu && pRbu->eStage==RBU_STAGE_OAL
210505 && (p->openFlags & SQLITE_OPEN_WAL0x00080000)
210506 && iOfst>=pRbu->iOalSz
210507 ){
210508 rc = SQLITE_OK0;
210509 memset(zBuf, 0, iAmt);
210510 }else{
210511 rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
210512#if 1
210513 /* If this is being called to read the first page of the target
210514 ** database as part of an rbu vacuum operation, synthesize the
210515 ** contents of the first page if it does not yet exist. Otherwise,
210516 ** SQLite will not check for a *-wal file. */
210517 if( pRbu && rbuIsVacuum(pRbu)
210518 && rc==SQLITE_IOERR_SHORT_READ(10 | (2<<8)) && iOfst==0
210519 && (p->openFlags & SQLITE_OPEN_MAIN_DB0x00000100)
210520 && pRbu->rc==SQLITE_OK0
210521 ){
210522 sqlite3_file *pFd = (sqlite3_file*)pRbu->pRbuFd;
210523 rc = pFd->pMethods->xRead(pFd, zBuf, iAmt, iOfst);
210524 if( rc==SQLITE_OK0 ){
210525 u8 *aBuf = (u8*)zBuf;
210526 u32 iRoot = rbuGetU32(&aBuf[52]) ? 1 : 0;
210527 rbuPutU32(&aBuf[52], iRoot); /* largest root page number */
210528 rbuPutU32(&aBuf[36], 0); /* number of free pages */
210529 rbuPutU32(&aBuf[32], 0); /* first page on free list trunk */
210530 rbuPutU32(&aBuf[28], 1); /* size of db file in pages */
210531 rbuPutU32(&aBuf[24], pRbu->pRbuFd->iCookie+1); /* Change counter */
210532
210533 if( iAmt>100 ){
210534 memset(&aBuf[100], 0, iAmt-100);
210535 rbuPutU16(&aBuf[105], iAmt & 0xFFFF);
210536 aBuf[100] = 0x0D;
210537 }
210538 }
210539 }
210540#endif
210541 }
210542 if( rc==SQLITE_OK0 && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB0x00000100) ){
210543 /* These look like magic numbers. But they are stable, as they are part
210544 ** of the definition of the SQLite file format, which may not change. */
210545 u8 *pBuf = (u8*)zBuf;
210546 p->iCookie = rbuGetU32(&pBuf[24]);
210547 p->iWriteVer = pBuf[19];
210548 }
210549 }
210550 return rc;
210551}
210552
210553/*
210554** Write data to an rbuVfs-file.
210555*/
210556static int rbuVfsWrite(
210557 sqlite3_file *pFile,
210558 const void *zBuf,
210559 int iAmt,
210560 sqlite_int64 iOfst
210561){
210562 rbu_file *p = (rbu_file*)pFile;
210563 sqlite3rbu *pRbu = p->pRbu;
210564 int rc;
210565
210566 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
210567 assert( p->openFlags & SQLITE_OPEN_MAIN_DB )((void) (0));
210568 rc = rbuCaptureDbWrite(p->pRbu, iOfst);
210569 }else{
210570 if( pRbu ){
210571 if( pRbu->eStage==RBU_STAGE_OAL
210572 && (p->openFlags & SQLITE_OPEN_WAL0x00080000)
210573 && iOfst>=pRbu->iOalSz
210574 ){
210575 pRbu->iOalSz = iAmt + iOfst;
210576 }else if( p->openFlags & SQLITE_OPEN_DELETEONCLOSE0x00000008 ){
210577 i64 szNew = iAmt+iOfst;
210578 if( szNew>p->sz ){
210579 rc = rbuUpdateTempSize(p, szNew);
210580 if( rc!=SQLITE_OK0 ) return rc;
210581 }
210582 }
210583 }
210584 rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
210585 if( rc==SQLITE_OK0 && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB0x00000100) ){
210586 /* These look like magic numbers. But they are stable, as they are part
210587 ** of the definition of the SQLite file format, which may not change. */
210588 u8 *pBuf = (u8*)zBuf;
210589 p->iCookie = rbuGetU32(&pBuf[24]);
210590 p->iWriteVer = pBuf[19];
210591 }
210592 }
210593 return rc;
210594}
210595
210596/*
210597** Truncate an rbuVfs-file.
210598*/
210599static int rbuVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
210600 rbu_file *p = (rbu_file*)pFile;
210601 if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE0x00000008) && p->pRbu ){
210602 int rc = rbuUpdateTempSize(p, size);
210603 if( rc!=SQLITE_OK0 ) return rc;
210604 }
210605 return p->pReal->pMethods->xTruncate(p->pReal, size);
210606}
210607
210608/*
210609** Sync an rbuVfs-file.
210610*/
210611static int rbuVfsSync(sqlite3_file *pFile, int flags){
210612 rbu_file *p = (rbu_file *)pFile;
210613 if( p->pRbu && p->pRbu->eStage==RBU_STAGE_CAPTURE ){
210614 if( p->openFlags & SQLITE_OPEN_MAIN_DB0x00000100 ){
210615 return SQLITE_INTERNAL2;
210616 }
210617 return SQLITE_OK0;
210618 }
210619 return p->pReal->pMethods->xSync(p->pReal, flags);
210620}
210621
210622/*
210623** Return the current file-size of an rbuVfs-file.
210624*/
210625static int rbuVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
210626 rbu_file *p = (rbu_file *)pFile;
210627 int rc;
210628 rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
210629
210630 /* If this is an RBU vacuum operation and this is the target database,
210631 ** pretend that it has at least one page. Otherwise, SQLite will not
210632 ** check for the existance of a *-wal file. rbuVfsRead() contains
210633 ** similar logic. */
210634 if( rc==SQLITE_OK0 && *pSize==0
210635 && p->pRbu && rbuIsVacuum(p->pRbu)
210636 && (p->openFlags & SQLITE_OPEN_MAIN_DB0x00000100)
210637 ){
210638 *pSize = 1024;
210639 }
210640 return rc;
210641}
210642
210643/*
210644** Lock an rbuVfs-file.
210645*/
210646static int rbuVfsLock(sqlite3_file *pFile, int eLock){
210647 rbu_file *p = (rbu_file*)pFile;
210648 sqlite3rbu *pRbu = p->pRbu;
210649 int rc = SQLITE_OK0;
210650
210651 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) )((void) (0));
210652 if( eLock==SQLITE_LOCK_EXCLUSIVE4
210653 && (p->bNolock || (pRbu && pRbu->eStage!=RBU_STAGE_DONE))
210654 ){
210655 /* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this
210656 ** prevents it from checkpointing the database from sqlite3_close(). */
210657 rc = SQLITE_BUSY5;
210658 }else{
210659 rc = p->pReal->pMethods->xLock(p->pReal, eLock);
210660 }
210661
210662 return rc;
210663}
210664
210665/*
210666** Unlock an rbuVfs-file.
210667*/
210668static int rbuVfsUnlock(sqlite3_file *pFile, int eLock){
210669 rbu_file *p = (rbu_file *)pFile;
210670 return p->pReal->pMethods->xUnlock(p->pReal, eLock);
210671}
210672
210673/*
210674** Check if another file-handle holds a RESERVED lock on an rbuVfs-file.
210675*/
210676static int rbuVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
210677 rbu_file *p = (rbu_file *)pFile;
210678 return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
210679}
210680
210681/*
210682** File control method. For custom operations on an rbuVfs-file.
210683*/
210684static int rbuVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
210685 rbu_file *p = (rbu_file *)pFile;
210686 int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
210687 int rc;
210688
210689 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB)((void) (0))
210690 || p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL)((void) (0))
210691 )((void) (0));
210692 if( op==SQLITE_FCNTL_RBU26 ){
210693 sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
210694
210695 /* First try to find another RBU vfs lower down in the vfs stack. If
210696 ** one is found, this vfs will operate in pass-through mode. The lower
210697 ** level vfs will do the special RBU handling. */
210698 rc = xControl(p->pReal, op, pArg);
210699
210700 if( rc==SQLITE_NOTFOUND12 ){
210701 /* Now search for a zipvfs instance lower down in the VFS stack. If
210702 ** one is found, this is an error. */
210703 void *dummy = 0;
210704 rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS25, &dummy);
210705 if( rc==SQLITE_OK0 ){
210706 rc = SQLITE_ERROR1;
210707 pRbu->zErrmsg = sqlite3_mprintf("rbu/zipvfs setup error");
210708 }else if( rc==SQLITE_NOTFOUND12 ){
210709 pRbu->pTargetFd = p;
210710 p->pRbu = pRbu;
210711 rbuMainlistAdd(p);
210712 if( p->pWalFd ) p->pWalFd->pRbu = pRbu;
210713 rc = SQLITE_OK0;
210714 }
210715 }
210716 return rc;
210717 }
210718 else if( op==SQLITE_FCNTL_RBUCNT ){
210719 sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
210720 pRbu->nRbu++;
210721 pRbu->pRbuFd = p;
210722 p->bNolock = 1;
210723 }
210724
210725 rc = xControl(p->pReal, op, pArg);
210726 if( rc==SQLITE_OK0 && op==SQLITE_FCNTL_VFSNAME12 ){
210727 rbu_vfs *pRbuVfs = p->pRbuVfs;
210728 char *zIn = *(char**)pArg;
210729 char *zOut = sqlite3_mprintf("rbu(%s)/%z", pRbuVfs->base.zName, zIn);
210730 *(char**)pArg = zOut;
210731 if( zOut==0 ) rc = SQLITE_NOMEM7;
210732 }
210733
210734 return rc;
210735}
210736
210737/*
210738** Return the sector-size in bytes for an rbuVfs-file.
210739*/
210740static int rbuVfsSectorSize(sqlite3_file *pFile){
210741 rbu_file *p = (rbu_file *)pFile;
210742 return p->pReal->pMethods->xSectorSize(p->pReal);
210743}
210744
210745/*
210746** Return the device characteristic flags supported by an rbuVfs-file.
210747*/
210748static int rbuVfsDeviceCharacteristics(sqlite3_file *pFile){
210749 rbu_file *p = (rbu_file *)pFile;
210750 return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
210751}
210752
210753/*
210754** Take or release a shared-memory lock.
210755*/
210756static int rbuVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
210757 rbu_file *p = (rbu_file*)pFile;
210758 sqlite3rbu *pRbu = p->pRbu;
210759 int rc = SQLITE_OK0;
210760
210761#ifdef SQLITE_AMALGAMATION1
210762 assert( WAL_CKPT_LOCK==1 )((void) (0));
210763#endif
210764
210765 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) )((void) (0));
210766 if( pRbu && (
210767 pRbu->eStage==RBU_STAGE_OAL
210768 || pRbu->eStage==RBU_STAGE_MOVE
210769 || pRbu->eStage==RBU_STAGE_DONE
210770 )){
210771 /* Prevent SQLite from taking a shm-lock on the target file when it
210772 ** is supplying heap memory to the upper layer in place of *-shm
210773 ** segments. */
210774 if( ofst==WAL_LOCK_CKPT && n==1 ) rc = SQLITE_BUSY5;
210775 }else{
210776 int bCapture = 0;
210777 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
210778 bCapture = 1;
210779 }
210780 if( bCapture==0 || 0==(flags & SQLITE_SHM_UNLOCK1) ){
210781 rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
210782 if( bCapture && rc==SQLITE_OK0 ){
210783 pRbu->mLock |= ((1<<n) - 1) << ofst;
210784 }
210785 }
210786 }
210787
210788 return rc;
210789}
210790
210791/*
210792** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
210793*/
210794static int rbuVfsShmMap(
210795 sqlite3_file *pFile,
210796 int iRegion,
210797 int szRegion,
210798 int isWrite,
210799 void volatile **pp
210800){
210801 rbu_file *p = (rbu_file*)pFile;
210802 int rc = SQLITE_OK0;
210803 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
210804
210805 /* If not in RBU_STAGE_OAL, allow this call to pass through. Or, if this
210806 ** rbu is in the RBU_STAGE_OAL state, use heap memory for *-shm space
210807 ** instead of a file on disk. */
210808 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) )((void) (0));
210809 if( eStage==RBU_STAGE_OAL ){
210810 sqlite3_int64 nByte = (iRegion+1) * sizeof(char*);
210811 char **apNew = (char**)sqlite3_realloc64(p->apShm, nByte);
210812
210813 /* This is an RBU connection that uses its own heap memory for the
210814 ** pages of the *-shm file. Since no other process can have run
210815 ** recovery, the connection must request *-shm pages in order
210816 ** from start to finish. */
210817 assert( iRegion==p->nShm )((void) (0));
210818 if( apNew==0 ){
210819 rc = SQLITE_NOMEM7;
210820 }else{
210821 memset(&apNew[p->nShm], 0, sizeof(char*) * (1 + iRegion - p->nShm));
210822 p->apShm = apNew;
210823 p->nShm = iRegion+1;
210824 }
210825
210826 if( rc==SQLITE_OK0 ){
210827 char *pNew = (char*)sqlite3_malloc64(szRegion);
210828 if( pNew==0 ){
210829 rc = SQLITE_NOMEM7;
210830 }else{
210831 memset(pNew, 0, szRegion);
210832 p->apShm[iRegion] = pNew;
210833 }
210834 }
210835
210836 if( rc==SQLITE_OK0 ){
210837 *pp = p->apShm[iRegion];
210838 }else{
210839 *pp = 0;
210840 }
210841 }else{
210842 assert( p->apShm==0 )((void) (0));
210843 rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
210844 }
210845
210846 return rc;
210847}
210848
210849/*
210850** Memory barrier.
210851*/
210852static void rbuVfsShmBarrier(sqlite3_file *pFile){
210853 rbu_file *p = (rbu_file *)pFile;
210854 p->pReal->pMethods->xShmBarrier(p->pReal);
210855}
210856
210857/*
210858** The xShmUnmap method.
210859*/
210860static int rbuVfsShmUnmap(sqlite3_file *pFile, int delFlag){
210861 rbu_file *p = (rbu_file*)pFile;
210862 int rc = SQLITE_OK0;
210863 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
210864
210865 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) )((void) (0));
210866 if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
210867 /* no-op */
210868 }else{
210869 /* Release the checkpointer and writer locks */
210870 rbuUnlockShm(p);
210871 rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
210872 }
210873 return rc;
210874}
210875
210876/*
210877** Open an rbu file handle.
210878*/
210879static int rbuVfsOpen(
210880 sqlite3_vfs *pVfs,
210881 const char *zName,
210882 sqlite3_file *pFile,
210883 int flags,
210884 int *pOutFlags
210885){
210886 static sqlite3_io_methods rbuvfs_io_methods = {
210887 2, /* iVersion */
210888 rbuVfsClose, /* xClose */
210889 rbuVfsRead, /* xRead */
210890 rbuVfsWrite, /* xWrite */
210891 rbuVfsTruncate, /* xTruncate */
210892 rbuVfsSync, /* xSync */
210893 rbuVfsFileSize, /* xFileSize */
210894 rbuVfsLock, /* xLock */
210895 rbuVfsUnlock, /* xUnlock */
210896 rbuVfsCheckReservedLock, /* xCheckReservedLock */
210897 rbuVfsFileControl, /* xFileControl */
210898 rbuVfsSectorSize, /* xSectorSize */
210899 rbuVfsDeviceCharacteristics, /* xDeviceCharacteristics */
210900 rbuVfsShmMap, /* xShmMap */
210901 rbuVfsShmLock, /* xShmLock */
210902 rbuVfsShmBarrier, /* xShmBarrier */
210903 rbuVfsShmUnmap, /* xShmUnmap */
210904 0, 0 /* xFetch, xUnfetch */
210905 };
210906 rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
210907 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
210908 rbu_file *pFd = (rbu_file *)pFile;
210909 int rc = SQLITE_OK0;
210910 const char *zOpen = zName;
210911 int oflags = flags;
210912
210913 memset(pFd, 0, sizeof(rbu_file));
210914 pFd->pReal = (sqlite3_file*)&pFd[1];
210915 pFd->pRbuVfs = pRbuVfs;
210916 pFd->openFlags = flags;
210917 if( zName ){
210918 if( flags & SQLITE_OPEN_MAIN_DB0x00000100 ){
210919 /* A main database has just been opened. The following block sets
210920 ** (pFd->zWal) to point to a buffer owned by SQLite that contains
210921 ** the name of the *-wal file this db connection will use. SQLite
210922 ** happens to pass a pointer to this buffer when using xAccess()
210923 ** or xOpen() to operate on the *-wal file. */
210924 pFd->zWal = sqlite3_filename_wal(zName);
210925 }
210926 else if( flags & SQLITE_OPEN_WAL0x00080000 ){
210927 rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName, 0);
210928 if( pDb ){
210929 if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
210930 /* This call is to open a *-wal file. Intead, open the *-oal. */
210931 size_t nOpen;
210932 if( rbuIsVacuum(pDb->pRbu) ){
210933 zOpen = sqlite3_db_filename(pDb->pRbu->dbRbu, "main");
210934 zOpen = sqlite3_filename_wal(zOpen);
210935 }
210936 nOpen = strlen(zOpen);
210937 ((char*)zOpen)[nOpen-3] = 'o';
210938 pFd->pRbu = pDb->pRbu;
210939 }
210940 pDb->pWalFd = pFd;
210941 }
210942 }
210943 }else{
210944 pFd->pRbu = pRbuVfs->pRbu;
210945 }
210946
210947 if( oflags & SQLITE_OPEN_MAIN_DB0x00000100
210948 && sqlite3_uri_boolean(zName, "rbu_memory", 0)
210949 ){
210950 assert( oflags & SQLITE_OPEN_MAIN_DB )((void) (0));
210951 oflags = SQLITE_OPEN_TEMP_DB0x00000200 | SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004 |
210952 SQLITE_OPEN_EXCLUSIVE0x00000010 | SQLITE_OPEN_DELETEONCLOSE0x00000008;
210953 zOpen = 0;
210954 }
210955
210956 if( rc==SQLITE_OK0 ){
210957 rc = pRealVfs->xOpen(pRealVfs, zOpen, pFd->pReal, oflags, pOutFlags);
210958 }
210959 if( pFd->pReal->pMethods ){
210960 /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
210961 ** pointer and, if the file is a main database file, link it into the
210962 ** mutex protected linked list of all such files. */
210963 pFile->pMethods = &rbuvfs_io_methods;
210964 if( flags & SQLITE_OPEN_MAIN_DB0x00000100 ){
210965 rbuMainlistAdd(pFd);
210966 }
210967 }else{
210968 sqlite3_free(pFd->zDel);
210969 }
210970
210971 return rc;
210972}
210973
210974/*
210975** Delete the file located at zPath.
210976*/
210977static int rbuVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
210978 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
210979 return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
210980}
210981
210982/*
210983** Test for access permissions. Return true if the requested permission
210984** is available, or false otherwise.
210985*/
210986static int rbuVfsAccess(
210987 sqlite3_vfs *pVfs,
210988 const char *zPath,
210989 int flags,
210990 int *pResOut
210991){
210992 rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
210993 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
210994 int rc;
210995
210996 rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
210997
210998 /* If this call is to check if a *-wal file associated with an RBU target
210999 ** database connection exists, and the RBU update is in RBU_STAGE_OAL,
211000 ** the following special handling is activated:
211001 **
211002 ** a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
211003 ** ensures that the RBU extension never tries to update a database
211004 ** in wal mode, even if the first page of the database file has
211005 ** been damaged.
211006 **
211007 ** b) if the *-wal file does not exist, claim that it does anyway,
211008 ** causing SQLite to call xOpen() to open it. This call will also
211009 ** be intercepted (see the rbuVfsOpen() function) and the *-oal
211010 ** file opened instead.
211011 */
211012 if( rc==SQLITE_OK0 && flags==SQLITE_ACCESS_EXISTS0 ){
211013 rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath, 1);
211014 if( pDb && pDb->pRbu->eStage==RBU_STAGE_OAL ){
211015 assert( pDb->pRbu )((void) (0));
211016 if( *pResOut ){
211017 rc = SQLITE_CANTOPEN14;
211018 }else{
211019 sqlite3_int64 sz = 0;
211020 rc = rbuVfsFileSize(&pDb->base, &sz);
211021 *pResOut = (sz>0);
211022 }
211023 }
211024 }
211025
211026 return rc;
211027}
211028
211029/*
211030** Populate buffer zOut with the full canonical pathname corresponding
211031** to the pathname in zPath. zOut is guaranteed to point to a buffer
211032** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
211033*/
211034static int rbuVfsFullPathname(
211035 sqlite3_vfs *pVfs,
211036 const char *zPath,
211037 int nOut,
211038 char *zOut
211039){
211040 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
211041 return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
211042}
211043
211044#ifndef SQLITE_OMIT_LOAD_EXTENSION1
211045/*
211046** Open the dynamic library located at zPath and return a handle.
211047*/
211048static void *rbuVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
211049 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
211050 return pRealVfs->xDlOpen(pRealVfs, zPath);
211051}
211052
211053/*
211054** Populate the buffer zErrMsg (size nByte bytes) with a human readable
211055** utf-8 string describing the most recent error encountered associated
211056** with dynamic libraries.
211057*/
211058static void rbuVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
211059 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
211060 pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
211061}
211062
211063/*
211064** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
211065*/
211066static void (*rbuVfsDlSym(
211067 sqlite3_vfs *pVfs,
211068 void *pArg,
211069 const char *zSym
211070))(void){
211071 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
211072 return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
211073}
211074
211075/*
211076** Close the dynamic library handle pHandle.
211077*/
211078static void rbuVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
211079 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
211080 pRealVfs->xDlClose(pRealVfs, pHandle);
211081}
211082#endif /* SQLITE_OMIT_LOAD_EXTENSION */
211083
211084/*
211085** Populate the buffer pointed to by zBufOut with nByte bytes of
211086** random data.
211087*/
211088static int rbuVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
211089 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
211090 return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
211091}
211092
211093/*
211094** Sleep for nMicro microseconds. Return the number of microseconds
211095** actually slept.
211096*/
211097static int rbuVfsSleep(sqlite3_vfs *pVfs, int nMicro){
211098 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
211099 return pRealVfs->xSleep(pRealVfs, nMicro);
211100}
211101
211102/*
211103** Return the current time as a Julian Day number in *pTimeOut.
211104*/
211105static int rbuVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
211106 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
211107 return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
211108}
211109
211110/*
211111** No-op.
211112*/
211113static int rbuVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
211114 return 0;
211115}
211116
211117/*
211118** Deregister and destroy an RBU vfs created by an earlier call to
211119** sqlite3rbu_create_vfs().
211120*/
211121SQLITE_API void sqlite3rbu_destroy_vfs(const char *zName){
211122 sqlite3_vfs *pVfs = sqlite3_vfs_find(zName);
211123 if( pVfs && pVfs->xOpen==rbuVfsOpen ){
211124 sqlite3_mutex_free(((rbu_vfs*)pVfs)->mutex);
211125 sqlite3_vfs_unregister(pVfs);
211126 sqlite3_free(pVfs);
211127 }
211128}
211129
211130/*
211131** Create an RBU VFS named zName that accesses the underlying file-system
211132** via existing VFS zParent. The new object is registered as a non-default
211133** VFS with SQLite before returning.
211134*/
211135SQLITE_API int sqlite3rbu_create_vfs(const char *zName, const char *zParent){
211136
211137 /* Template for VFS */
211138 static sqlite3_vfs vfs_template = {
211139 1, /* iVersion */
211140 0, /* szOsFile */
211141 0, /* mxPathname */
211142 0, /* pNext */
211143 0, /* zName */
211144 0, /* pAppData */
211145 rbuVfsOpen, /* xOpen */
211146 rbuVfsDelete, /* xDelete */
211147 rbuVfsAccess, /* xAccess */
211148 rbuVfsFullPathname, /* xFullPathname */
211149
211150#ifndef SQLITE_OMIT_LOAD_EXTENSION1
211151 rbuVfsDlOpen, /* xDlOpen */
211152 rbuVfsDlError, /* xDlError */
211153 rbuVfsDlSym, /* xDlSym */
211154 rbuVfsDlClose, /* xDlClose */
211155#else
211156 0, 0, 0, 0,
211157#endif
211158
211159 rbuVfsRandomness, /* xRandomness */
211160 rbuVfsSleep, /* xSleep */
211161 rbuVfsCurrentTime, /* xCurrentTime */
211162 rbuVfsGetLastError, /* xGetLastError */
211163 0, /* xCurrentTimeInt64 (version 2) */
211164 0, 0, 0 /* Unimplemented version 3 methods */
211165 };
211166
211167 rbu_vfs *pNew = 0; /* Newly allocated VFS */
211168 int rc = SQLITE_OK0;
211169 size_t nName;
211170 size_t nByte;
211171
211172 nName = strlen(zName);
211173 nByte = sizeof(rbu_vfs) + nName + 1;
211174 pNew = (rbu_vfs*)sqlite3_malloc64(nByte);
211175 if( pNew==0 ){
211176 rc = SQLITE_NOMEM7;
211177 }else{
211178 sqlite3_vfs *pParent; /* Parent VFS */
211179 memset(pNew, 0, nByte);
211180 pParent = sqlite3_vfs_find(zParent);
211181 if( pParent==0 ){
211182 rc = SQLITE_NOTFOUND12;
211183 }else{
211184 char *zSpace;
211185 memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
211186 pNew->base.mxPathname = pParent->mxPathname;
211187 pNew->base.szOsFile = sizeof(rbu_file) + pParent->szOsFile;
211188 pNew->pRealVfs = pParent;
211189 pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
211190 memcpy(zSpace, zName, nName);
211191
211192 /* Allocate the mutex and register the new VFS (not as the default) */
211193 pNew->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE1);
211194 if( pNew->mutex==0 ){
211195 rc = SQLITE_NOMEM7;
211196 }else{
211197 rc = sqlite3_vfs_register(&pNew->base, 0);
211198 }
211199 }
211200
211201 if( rc!=SQLITE_OK0 ){
211202 sqlite3_mutex_free(pNew->mutex);
211203 sqlite3_free(pNew);
211204 }
211205 }
211206
211207 return rc;
211208}
211209
211210/*
211211** Configure the aggregate temp file size limit for this RBU handle.
211212*/
211213SQLITE_API sqlite3_int64 sqlite3rbu_temp_size_limit(sqlite3rbu *pRbu, sqlite3_int64 n){
211214 if( n>=0 ){
211215 pRbu->szTempLimit = n;
211216 }
211217 return pRbu->szTempLimit;
211218}
211219
211220SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu *pRbu){
211221 return pRbu->szTemp;
211222}
211223
211224
211225/**************************************************************************/
211226
211227#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU) */
211228
211229/************** End of sqlite3rbu.c ******************************************/
211230/************** Begin file dbstat.c ******************************************/
211231/*
211232** 2010 July 12
211233**
211234** The author disclaims copyright to this source code. In place of
211235** a legal notice, here is a blessing:
211236**
211237** May you do good and not evil.
211238** May you find forgiveness for yourself and forgive others.
211239** May you share freely, never taking more than you give.
211240**
211241******************************************************************************
211242**
211243** This file contains an implementation of the "dbstat" virtual table.
211244**
211245** The dbstat virtual table is used to extract low-level storage
211246** information from an SQLite database in order to implement the
211247** "sqlite3_analyzer" utility. See the ../tool/spaceanal.tcl script
211248** for an example implementation.
211249**
211250** Additional information is available on the "dbstat.html" page of the
211251** official SQLite documentation.
211252*/
211253
211254/* #include "sqliteInt.h" ** Requires access to internal data structures ** */
211255#if (defined(SQLITE_ENABLE_DBSTAT_VTAB1) || defined(SQLITE_TEST)) \
211256 && !defined(SQLITE_OMIT_VIRTUALTABLE)
211257
211258/*
211259** The pager and btree modules arrange objects in memory so that there are
211260** always approximately 200 bytes of addressable memory following each page
211261** buffer. This way small buffer overreads caused by corrupt database pages
211262** do not cause undefined behaviour. This module pads each page buffer
211263** by the following number of bytes for the same purpose.
211264*/
211265#define DBSTAT_PAGE_PADDING_BYTES256 256
211266
211267/*
211268** Page paths:
211269**
211270** The value of the 'path' column describes the path taken from the
211271** root-node of the b-tree structure to each page. The value of the
211272** root-node path is '/'.
211273**
211274** The value of the path for the left-most child page of the root of
211275** a b-tree is '/000/'. (Btrees store content ordered from left to right
211276** so the pages to the left have smaller keys than the pages to the right.)
211277** The next to left-most child of the root page is
211278** '/001', and so on, each sibling page identified by a 3-digit hex
211279** value. The children of the 451st left-most sibling have paths such
211280** as '/1c2/000/, '/1c2/001/' etc.
211281**
211282** Overflow pages are specified by appending a '+' character and a
211283** six-digit hexadecimal value to the path to the cell they are linked
211284** from. For example, the three overflow pages in a chain linked from
211285** the left-most cell of the 450th child of the root page are identified
211286** by the paths:
211287**
211288** '/1c2/000+000000' // First page in overflow chain
211289** '/1c2/000+000001' // Second page in overflow chain
211290** '/1c2/000+000002' // Third page in overflow chain
211291**
211292** If the paths are sorted using the BINARY collation sequence, then
211293** the overflow pages associated with a cell will appear earlier in the
211294** sort-order than its child page:
211295**
211296** '/1c2/000/' // Left-most child of 451st child of root
211297*/
211298static const char zDbstatSchema[] =
211299 "CREATE TABLE x("
211300 " name TEXT," /* 0 Name of table or index */
211301 " path TEXT," /* 1 Path to page from root (NULL for agg) */
211302 " pageno INTEGER," /* 2 Page number (page count for aggregates) */
211303 " pagetype TEXT," /* 3 'internal', 'leaf', 'overflow', or NULL */
211304 " ncell INTEGER," /* 4 Cells on page (0 for overflow) */
211305 " payload INTEGER," /* 5 Bytes of payload on this page */
211306 " unused INTEGER," /* 6 Bytes of unused space on this page */
211307 " mx_payload INTEGER," /* 7 Largest payload size of all cells */
211308 " pgoffset INTEGER," /* 8 Offset of page in file (NULL for agg) */
211309 " pgsize INTEGER," /* 9 Size of the page (sum for aggregate) */
211310 " schema TEXT HIDDEN," /* 10 Database schema being analyzed */
211311 " aggregate BOOLEAN HIDDEN" /* 11 aggregate info for each table */
211312 ")"
211313;
211314
211315/* Forward reference to data structured used in this module */
211316typedef struct StatTable StatTable;
211317typedef struct StatCursor StatCursor;
211318typedef struct StatPage StatPage;
211319typedef struct StatCell StatCell;
211320
211321/* Size information for a single cell within a btree page */
211322struct StatCell {
211323 int nLocal; /* Bytes of local payload */
211324 u32 iChildPg; /* Child node (or 0 if this is a leaf) */
211325 int nOvfl; /* Entries in aOvfl[] */
211326 u32 *aOvfl; /* Array of overflow page numbers */
211327 int nLastOvfl; /* Bytes of payload on final overflow page */
211328 int iOvfl; /* Iterates through aOvfl[] */
211329};
211330
211331/* Size information for a single btree page */
211332struct StatPage {
211333 u32 iPgno; /* Page number */
211334 u8 *aPg; /* Page buffer from sqlite3_malloc() */
211335 int iCell; /* Current cell */
211336 char *zPath; /* Path to this page */
211337
211338 /* Variables populated by statDecodePage(): */
211339 u8 flags; /* Copy of flags byte */
211340 int nCell; /* Number of cells on page */
211341 int nUnused; /* Number of unused bytes on page */
211342 StatCell *aCell; /* Array of parsed cells */
211343 u32 iRightChildPg; /* Right-child page number (or 0) */
211344 int nMxPayload; /* Largest payload of any cell on the page */
211345};
211346
211347/* The cursor for scanning the dbstat virtual table */
211348struct StatCursor {
211349 sqlite3_vtab_cursor base; /* base class. MUST BE FIRST! */
211350 sqlite3_stmt *pStmt; /* Iterates through set of root pages */
211351 u8 isEof; /* After pStmt has returned SQLITE_DONE */
211352 u8 isAgg; /* Aggregate results for each table */
211353 int iDb; /* Schema used for this query */
211354
211355 StatPage aPage[32]; /* Pages in path to current page */
211356 int iPage; /* Current entry in aPage[] */
211357
211358 /* Values to return. */
211359 u32 iPageno; /* Value of 'pageno' column */
211360 char *zName; /* Value of 'name' column */
211361 char *zPath; /* Value of 'path' column */
211362 char *zPagetype; /* Value of 'pagetype' column */
211363 int nPage; /* Number of pages in current btree */
211364 int nCell; /* Value of 'ncell' column */
211365 int nMxPayload; /* Value of 'mx_payload' column */
211366 i64 nUnused; /* Value of 'unused' column */
211367 i64 nPayload; /* Value of 'payload' column */
211368 i64 iOffset; /* Value of 'pgOffset' column */
211369 i64 szPage; /* Value of 'pgSize' column */
211370};
211371
211372/* An instance of the DBSTAT virtual table */
211373struct StatTable {
211374 sqlite3_vtab base; /* base class. MUST BE FIRST! */
211375 sqlite3 *db; /* Database connection that owns this vtab */
211376 int iDb; /* Index of database to analyze */
211377};
211378
211379#ifndef get2byte
211380# define get2byte(x)((x)[0]<<8 | (x)[1]) ((x)[0]<<8 | (x)[1])
211381#endif
211382
211383/*
211384** Connect to or create a new DBSTAT virtual table.
211385*/
211386static int statConnect(
211387 sqlite3 *db,
211388 void *pAux,
211389 int argc, const char *const*argv,
211390 sqlite3_vtab **ppVtab,
211391 char **pzErr
211392){
211393 StatTable *pTab = 0;
211394 int rc = SQLITE_OK0;
211395 int iDb;
211396
211397 if( argc>=4 ){
211398 Token nm;
211399 sqlite3TokenInit(&nm, (char*)argv[3]);
211400 iDb = sqlite3FindDb(db, &nm);
211401 if( iDb<0 ){
211402 *pzErr = sqlite3_mprintf("no such database: %s", argv[3]);
211403 return SQLITE_ERROR1;
211404 }
211405 }else{
211406 iDb = 0;
211407 }
211408 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY3);
211409 rc = sqlite3_declare_vtab(db, zDbstatSchema);
211410 if( rc==SQLITE_OK0 ){
211411 pTab = (StatTable *)sqlite3_malloc64(sizeof(StatTable));
211412 if( pTab==0 ) rc = SQLITE_NOMEM_BKPT7;
211413 }
211414
211415 assert( rc==SQLITE_OK || pTab==0 )((void) (0));
211416 if( rc==SQLITE_OK0 ){
211417 memset(pTab, 0, sizeof(StatTable));
211418 pTab->db = db;
211419 pTab->iDb = iDb;
211420 }
211421
211422 *ppVtab = (sqlite3_vtab*)pTab;
211423 return rc;
211424}
211425
211426/*
211427** Disconnect from or destroy the DBSTAT virtual table.
211428*/
211429static int statDisconnect(sqlite3_vtab *pVtab){
211430 sqlite3_free(pVtab);
211431 return SQLITE_OK0;
211432}
211433
211434/*
211435** Compute the best query strategy and return the result in idxNum.
211436**
211437** idxNum-Bit Meaning
211438** ---------- ----------------------------------------------
211439** 0x01 There is a schema=? term in the WHERE clause
211440** 0x02 There is a name=? term in the WHERE clause
211441** 0x04 There is an aggregate=? term in the WHERE clause
211442** 0x08 Output should be ordered by name and path
211443*/
211444static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
211445 int i;
211446 int iSchema = -1;
211447 int iName = -1;
211448 int iAgg = -1;
211449
211450 /* Look for a valid schema=? constraint. If found, change the idxNum to
211451 ** 1 and request the value of that constraint be sent to xFilter. And
211452 ** lower the cost estimate to encourage the constrained version to be
211453 ** used.
211454 */
211455 for(i=0; i<pIdxInfo->nConstraint; i++){
211456 if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ2 ) continue;
211457 if( pIdxInfo->aConstraint[i].usable==0 ){
211458 /* Force DBSTAT table should always be the right-most table in a join */
211459 return SQLITE_CONSTRAINT19;
211460 }
211461 switch( pIdxInfo->aConstraint[i].iColumn ){
211462 case 0: { /* name */
211463 iName = i;
211464 break;
211465 }
211466 case 10: { /* schema */
211467 iSchema = i;
211468 break;
211469 }
211470 case 11: { /* aggregate */
211471 iAgg = i;
211472 break;
211473 }
211474 }
211475 }
211476 i = 0;
211477 if( iSchema>=0 ){
211478 pIdxInfo->aConstraintUsage[iSchema].argvIndex = ++i;
211479 pIdxInfo->aConstraintUsage[iSchema].omit = 1;
211480 pIdxInfo->idxNum |= 0x01;
211481 }
211482 if( iName>=0 ){
211483 pIdxInfo->aConstraintUsage[iName].argvIndex = ++i;
211484 pIdxInfo->idxNum |= 0x02;
211485 }
211486 if( iAgg>=0 ){
211487 pIdxInfo->aConstraintUsage[iAgg].argvIndex = ++i;
211488 pIdxInfo->idxNum |= 0x04;
211489 }
211490 pIdxInfo->estimatedCost = 1.0;
211491
211492 /* Records are always returned in ascending order of (name, path).
211493 ** If this will satisfy the client, set the orderByConsumed flag so that
211494 ** SQLite does not do an external sort.
211495 */
211496 if( ( pIdxInfo->nOrderBy==1
211497 && pIdxInfo->aOrderBy[0].iColumn==0
211498 && pIdxInfo->aOrderBy[0].desc==0
211499 ) ||
211500 ( pIdxInfo->nOrderBy==2
211501 && pIdxInfo->aOrderBy[0].iColumn==0
211502 && pIdxInfo->aOrderBy[0].desc==0
211503 && pIdxInfo->aOrderBy[1].iColumn==1
211504 && pIdxInfo->aOrderBy[1].desc==0
211505 )
211506 ){
211507 pIdxInfo->orderByConsumed = 1;
211508 pIdxInfo->idxNum |= 0x08;
211509 }
211510
211511 return SQLITE_OK0;
211512}
211513
211514/*
211515** Open a new DBSTAT cursor.
211516*/
211517static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
211518 StatTable *pTab = (StatTable *)pVTab;
211519 StatCursor *pCsr;
211520
211521 pCsr = (StatCursor *)sqlite3_malloc64(sizeof(StatCursor));
211522 if( pCsr==0 ){
211523 return SQLITE_NOMEM_BKPT7;
211524 }else{
211525 memset(pCsr, 0, sizeof(StatCursor));
211526 pCsr->base.pVtab = pVTab;
211527 pCsr->iDb = pTab->iDb;
211528 }
211529
211530 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
211531 return SQLITE_OK0;
211532}
211533
211534static void statClearCells(StatPage *p){
211535 int i;
211536 if( p->aCell ){
211537 for(i=0; i<p->nCell; i++){
211538 sqlite3_free(p->aCell[i].aOvfl);
211539 }
211540 sqlite3_free(p->aCell);
211541 }
211542 p->nCell = 0;
211543 p->aCell = 0;
211544}
211545
211546static void statClearPage(StatPage *p){
211547 u8 *aPg = p->aPg;
211548 statClearCells(p);
211549 sqlite3_free(p->zPath);
211550 memset(p, 0, sizeof(StatPage));
211551 p->aPg = aPg;
211552}
211553
211554static void statResetCsr(StatCursor *pCsr){
211555 int i;
211556 /* In some circumstances, specifically if an OOM has occurred, the call
211557 ** to sqlite3_reset() may cause the pager to be reset (emptied). It is
211558 ** important that statClearPage() is called to free any page refs before
211559 ** this happens. dbsqlfuzz 9ed3e4e3816219d3509d711636c38542bf3f40b1. */
211560 for(i=0; i<ArraySize(pCsr->aPage)((int)(sizeof(pCsr->aPage)/sizeof(pCsr->aPage[0]))); i++){
211561 statClearPage(&pCsr->aPage[i]);
211562 sqlite3_free(pCsr->aPage[i].aPg);
211563 pCsr->aPage[i].aPg = 0;
211564 }
211565 sqlite3_reset(pCsr->pStmt);
211566 pCsr->iPage = 0;
211567 sqlite3_free(pCsr->zPath);
211568 pCsr->zPath = 0;
211569 pCsr->isEof = 0;
211570}
211571
211572/* Resize the space-used counters inside of the cursor */
211573static void statResetCounts(StatCursor *pCsr){
211574 pCsr->nCell = 0;
211575 pCsr->nMxPayload = 0;
211576 pCsr->nUnused = 0;
211577 pCsr->nPayload = 0;
211578 pCsr->szPage = 0;
211579 pCsr->nPage = 0;
211580}
211581
211582/*
211583** Close a DBSTAT cursor.
211584*/
211585static int statClose(sqlite3_vtab_cursor *pCursor){
211586 StatCursor *pCsr = (StatCursor *)pCursor;
211587 statResetCsr(pCsr);
211588 sqlite3_finalize(pCsr->pStmt);
211589 sqlite3_free(pCsr);
211590 return SQLITE_OK0;
211591}
211592
211593/*
211594** For a single cell on a btree page, compute the number of bytes of
211595** content (payload) stored on that page. That is to say, compute the
211596** number of bytes of content not found on overflow pages.
211597*/
211598static int getLocalPayload(
211599 int nUsable, /* Usable bytes per page */
211600 u8 flags, /* Page flags */
211601 int nTotal /* Total record (payload) size */
211602){
211603 int nLocal;
211604 int nMinLocal;
211605 int nMaxLocal;
211606
211607 if( flags==0x0D ){ /* Table leaf node */
211608 nMinLocal = (nUsable - 12) * 32 / 255 - 23;
211609 nMaxLocal = nUsable - 35;
211610 }else{ /* Index interior and leaf nodes */
211611 nMinLocal = (nUsable - 12) * 32 / 255 - 23;
211612 nMaxLocal = (nUsable - 12) * 64 / 255 - 23;
211613 }
211614
211615 nLocal = nMinLocal + (nTotal - nMinLocal) % (nUsable - 4);
211616 if( nLocal>nMaxLocal ) nLocal = nMinLocal;
211617 return nLocal;
211618}
211619
211620/* Populate the StatPage object with information about the all
211621** cells found on the page currently under analysis.
211622*/
211623static int statDecodePage(Btree *pBt, StatPage *p){
211624 int nUnused;
211625 int iOff;
211626 int nHdr;
211627 int isLeaf;
211628 int szPage;
211629
211630 u8 *aData = p->aPg;
211631 u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0];
211632
211633 p->flags = aHdr[0];
211634 if( p->flags==0x0A || p->flags==0x0D ){
211635 isLeaf = 1;
211636 nHdr = 8;
211637 }else if( p->flags==0x05 || p->flags==0x02 ){
211638 isLeaf = 0;
211639 nHdr = 12;
211640 }else{
211641 goto statPageIsCorrupt;
211642 }
211643 if( p->iPgno==1 ) nHdr += 100;
211644 p->nCell = get2byte(&aHdr[3])((&aHdr[3])[0]<<8 | (&aHdr[3])[1]);
211645 p->nMxPayload = 0;
211646 szPage = sqlite3BtreeGetPageSize(pBt);
211647
211648 nUnused = get2byte(&aHdr[5])((&aHdr[5])[0]<<8 | (&aHdr[5])[1]) - nHdr - 2*p->nCell;
211649 nUnused += (int)aHdr[7];
211650 iOff = get2byte(&aHdr[1])((&aHdr[1])[0]<<8 | (&aHdr[1])[1]);
211651 while( iOff ){
211652 int iNext;
211653 if( iOff>=szPage ) goto statPageIsCorrupt;
211654 nUnused += get2byte(&aData[iOff+2])((&aData[iOff+2])[0]<<8 | (&aData[iOff+2])[1]);
211655 iNext = get2byte(&aData[iOff])((&aData[iOff])[0]<<8 | (&aData[iOff])[1]);
211656 if( iNext<iOff+4 && iNext>0 ) goto statPageIsCorrupt;
211657 iOff = iNext;
211658 }
211659 p->nUnused = nUnused;
211660 p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]);
211661
211662 if( p->nCell ){
211663 int i; /* Used to iterate through cells */
211664 int nUsable; /* Usable bytes per page */
211665
211666 sqlite3BtreeEnter(pBt);
211667 nUsable = szPage - sqlite3BtreeGetReserveNoMutex(pBt);
211668 sqlite3BtreeLeave(pBt);
211669 p->aCell = sqlite3_malloc64((p->nCell+1) * sizeof(StatCell));
211670 if( p->aCell==0 ) return SQLITE_NOMEM_BKPT7;
211671 memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell));
211672
211673 for(i=0; i<p->nCell; i++){
211674 StatCell *pCell = &p->aCell[i];
211675
211676 iOff = get2byte(&aData[nHdr+i*2])((&aData[nHdr+i*2])[0]<<8 | (&aData[nHdr+i*2])[
1])
;
211677 if( iOff<nHdr || iOff>=szPage ) goto statPageIsCorrupt;
211678 if( !isLeaf ){
211679 pCell->iChildPg = sqlite3Get4byte(&aData[iOff]);
211680 iOff += 4;
211681 }
211682 if( p->flags==0x05 ){
211683 /* A table interior node. nPayload==0. */
211684 }else{
211685 u32 nPayload; /* Bytes of payload total (local+overflow) */
211686 int nLocal; /* Bytes of payload stored locally */
211687 iOff += getVarint32(&aData[iOff], nPayload)(u8)((*(&aData[iOff])<(u8)0x80)?((nPayload)=(u32)*(&
aData[iOff])),1:sqlite3GetVarint32((&aData[iOff]),(u32 *)
&(nPayload)))
;
211688 if( p->flags==0x0D ){
211689 u64 dummy;
211690 iOff += sqlite3GetVarint(&aData[iOff], &dummy);
211691 }
211692 if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload;
211693 nLocal = getLocalPayload(nUsable, p->flags, nPayload);
211694 if( nLocal<0 ) goto statPageIsCorrupt;
211695 pCell->nLocal = nLocal;
211696 assert( nPayload>=(u32)nLocal )((void) (0));
211697 assert( nLocal<=(nUsable-35) )((void) (0));
211698 if( nPayload>(u32)nLocal ){
211699 int j;
211700 int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
211701 if( iOff+nLocal+4>nUsable || nPayload>0x7fffffff ){
211702 goto statPageIsCorrupt;
211703 }
211704 pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
211705 pCell->nOvfl = nOvfl;
211706 pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl);
211707 if( pCell->aOvfl==0 ) return SQLITE_NOMEM_BKPT7;
211708 pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
211709 for(j=1; j<nOvfl; j++){
211710 int rc;
211711 u32 iPrev = pCell->aOvfl[j-1];
211712 DbPage *pPg = 0;
211713 rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg, 0);
211714 if( rc!=SQLITE_OK0 ){
211715 assert( pPg==0 )((void) (0));
211716 return rc;
211717 }
211718 pCell->aOvfl[j] = sqlite3Get4byte(sqlite3PagerGetData(pPg));
211719 sqlite3PagerUnref(pPg);
211720 }
211721 }
211722 }
211723 }
211724 }
211725
211726 return SQLITE_OK0;
211727
211728statPageIsCorrupt:
211729 p->flags = 0;
211730 statClearCells(p);
211731 return SQLITE_OK0;
211732}
211733
211734/*
211735** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on
211736** the current value of pCsr->iPageno.
211737*/
211738static void statSizeAndOffset(StatCursor *pCsr){
211739 StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab;
211740 Btree *pBt = pTab->db->aDb[pTab->iDb].pBt;
211741 Pager *pPager = sqlite3BtreePager(pBt);
211742 sqlite3_file *fd;
211743 sqlite3_int64 x[2];
211744
211745 /* If connected to a ZIPVFS backend, find the page size and
211746 ** offset from ZIPVFS.
211747 */
211748 fd = sqlite3PagerFile(pPager);
211749 x[0] = pCsr->iPageno;
211750 if( sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK0 ){
211751 pCsr->iOffset = x[0];
211752 pCsr->szPage += x[1];
211753 }else{
211754 /* Not ZIPVFS: The default page size and offset */
211755 pCsr->szPage += sqlite3BtreeGetPageSize(pBt);
211756 pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1);
211757 }
211758}
211759
211760/*
211761** Load a copy of the page data for page iPg into the buffer belonging
211762** to page object pPg. Allocate the buffer if necessary. Return SQLITE_OK
211763** if successful, or an SQLite error code otherwise.
211764*/
211765static int statGetPage(
211766 Btree *pBt, /* Load page from this b-tree */
211767 u32 iPg, /* Page number to load */
211768 StatPage *pPg /* Load page into this object */
211769){
211770 int pgsz = sqlite3BtreeGetPageSize(pBt);
211771 DbPage *pDbPage = 0;
211772 int rc;
211773
211774 if( pPg->aPg==0 ){
211775 pPg->aPg = (u8*)sqlite3_malloc(pgsz + DBSTAT_PAGE_PADDING_BYTES256);
211776 if( pPg->aPg==0 ){
211777 return SQLITE_NOMEM_BKPT7;
211778 }
211779 memset(&pPg->aPg[pgsz], 0, DBSTAT_PAGE_PADDING_BYTES256);
211780 }
211781
211782 rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPg, &pDbPage, 0);
211783 if( rc==SQLITE_OK0 ){
211784 const u8 *a = sqlite3PagerGetData(pDbPage);
211785 memcpy(pPg->aPg, a, pgsz);
211786 sqlite3PagerUnref(pDbPage);
211787 }
211788
211789 return rc;
211790}
211791
211792/*
211793** Move a DBSTAT cursor to the next entry. Normally, the next
211794** entry will be the next page, but in aggregated mode (pCsr->isAgg!=0),
211795** the next entry is the next btree.
211796*/
211797static int statNext(sqlite3_vtab_cursor *pCursor){
211798 int rc;
211799 int nPayload;
211800 char *z;
211801 StatCursor *pCsr = (StatCursor *)pCursor;
211802 StatTable *pTab = (StatTable *)pCursor->pVtab;
211803 Btree *pBt = pTab->db->aDb[pCsr->iDb].pBt;
211804 Pager *pPager = sqlite3BtreePager(pBt);
211805
211806 sqlite3_free(pCsr->zPath);
211807 pCsr->zPath = 0;
211808
211809statNextRestart:
211810 if( pCsr->iPage<0 ){
211811 /* Start measuring space on the next btree */
211812 statResetCounts(pCsr);
211813 rc = sqlite3_step(pCsr->pStmt);
211814 if( rc==SQLITE_ROW100 ){
211815 int nPage;
211816 u32 iRoot = (u32)sqlite3_column_int64(pCsr->pStmt, 1);
211817 sqlite3PagerPagecount(pPager, &nPage);
211818 if( nPage==0 ){
211819 pCsr->isEof = 1;
211820 return sqlite3_reset(pCsr->pStmt);
211821 }
211822 rc = statGetPage(pBt, iRoot, &pCsr->aPage[0]);
211823 pCsr->aPage[0].iPgno = iRoot;
211824 pCsr->aPage[0].iCell = 0;
211825 if( !pCsr->isAgg ){
211826 pCsr->aPage[0].zPath = z = sqlite3_mprintf("/");
211827 if( z==0 ) rc = SQLITE_NOMEM_BKPT7;
211828 }
211829 pCsr->iPage = 0;
211830 pCsr->nPage = 1;
211831 }else{
211832 pCsr->isEof = 1;
211833 return sqlite3_reset(pCsr->pStmt);
211834 }
211835 }else{
211836 /* Continue analyzing the btree previously started */
211837 StatPage *p = &pCsr->aPage[pCsr->iPage];
211838 if( !pCsr->isAgg ) statResetCounts(pCsr);
211839 while( p->iCell<p->nCell ){
211840 StatCell *pCell = &p->aCell[p->iCell];
211841 while( pCell->iOvfl<pCell->nOvfl ){
211842 int nUsable, iOvfl;
211843 sqlite3BtreeEnter(pBt);
211844 nUsable = sqlite3BtreeGetPageSize(pBt) -
211845 sqlite3BtreeGetReserveNoMutex(pBt);
211846 sqlite3BtreeLeave(pBt);
211847 pCsr->nPage++;
211848 statSizeAndOffset(pCsr);
211849 if( pCell->iOvfl<pCell->nOvfl-1 ){
211850 pCsr->nPayload += nUsable - 4;
211851 }else{
211852 pCsr->nPayload += pCell->nLastOvfl;
211853 pCsr->nUnused += nUsable - 4 - pCell->nLastOvfl;
211854 }
211855 iOvfl = pCell->iOvfl;
211856 pCell->iOvfl++;
211857 if( !pCsr->isAgg ){
211858 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
211859 pCsr->iPageno = pCell->aOvfl[iOvfl];
211860 pCsr->zPagetype = "overflow";
211861 pCsr->zPath = z = sqlite3_mprintf(
211862 "%s%.3x+%.6x", p->zPath, p->iCell, iOvfl
211863 );
211864 return z==0 ? SQLITE_NOMEM_BKPT7 : SQLITE_OK0;
211865 }
211866 }
211867 if( p->iRightChildPg ) break;
211868 p->iCell++;
211869 }
211870
211871 if( !p->iRightChildPg || p->iCell>p->nCell ){
211872 statClearPage(p);
211873 pCsr->iPage--;
211874 if( pCsr->isAgg && pCsr->iPage<0 ){
211875 /* label-statNext-done: When computing aggregate space usage over
211876 ** an entire btree, this is the exit point from this function */
211877 return SQLITE_OK0;
211878 }
211879 goto statNextRestart; /* Tail recursion */
211880 }
211881 pCsr->iPage++;
211882 if( pCsr->iPage>=ArraySize(pCsr->aPage)((int)(sizeof(pCsr->aPage)/sizeof(pCsr->aPage[0]))) ){
211883 statResetCsr(pCsr);
211884 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(211884);
211885 }
211886 assert( p==&pCsr->aPage[pCsr->iPage-1] )((void) (0));
211887
211888 if( p->iCell==p->nCell ){
211889 p[1].iPgno = p->iRightChildPg;
211890 }else{
211891 p[1].iPgno = p->aCell[p->iCell].iChildPg;
211892 }
211893 rc = statGetPage(pBt, p[1].iPgno, &p[1]);
211894 pCsr->nPage++;
211895 p[1].iCell = 0;
211896 if( !pCsr->isAgg ){
211897 p[1].zPath = z = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell);
211898 if( z==0 ) rc = SQLITE_NOMEM_BKPT7;
211899 }
211900 p->iCell++;
211901 }
211902
211903
211904 /* Populate the StatCursor fields with the values to be returned
211905 ** by the xColumn() and xRowid() methods.
211906 */
211907 if( rc==SQLITE_OK0 ){
211908 int i;
211909 StatPage *p = &pCsr->aPage[pCsr->iPage];
211910 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
211911 pCsr->iPageno = p->iPgno;
211912
211913 rc = statDecodePage(pBt, p);
211914 if( rc==SQLITE_OK0 ){
211915 statSizeAndOffset(pCsr);
211916
211917 switch( p->flags ){
211918 case 0x05: /* table internal */
211919 case 0x02: /* index internal */
211920 pCsr->zPagetype = "internal";
211921 break;
211922 case 0x0D: /* table leaf */
211923 case 0x0A: /* index leaf */
211924 pCsr->zPagetype = "leaf";
211925 break;
211926 default:
211927 pCsr->zPagetype = "corrupted";
211928 break;
211929 }
211930 pCsr->nCell += p->nCell;
211931 pCsr->nUnused += p->nUnused;
211932 if( p->nMxPayload>pCsr->nMxPayload ) pCsr->nMxPayload = p->nMxPayload;
211933 if( !pCsr->isAgg ){
211934 pCsr->zPath = z = sqlite3_mprintf("%s", p->zPath);
211935 if( z==0 ) rc = SQLITE_NOMEM_BKPT7;
211936 }
211937 nPayload = 0;
211938 for(i=0; i<p->nCell; i++){
211939 nPayload += p->aCell[i].nLocal;
211940 }
211941 pCsr->nPayload += nPayload;
211942
211943 /* If computing aggregate space usage by btree, continue with the
211944 ** next page. The loop will exit via the return at label-statNext-done
211945 */
211946 if( pCsr->isAgg ) goto statNextRestart;
211947 }
211948 }
211949
211950 return rc;
211951}
211952
211953static int statEof(sqlite3_vtab_cursor *pCursor){
211954 StatCursor *pCsr = (StatCursor *)pCursor;
211955 return pCsr->isEof;
211956}
211957
211958/* Initialize a cursor according to the query plan idxNum using the
211959** arguments in argv[0]. See statBestIndex() for a description of the
211960** meaning of the bits in idxNum.
211961*/
211962static int statFilter(
211963 sqlite3_vtab_cursor *pCursor,
211964 int idxNum, const char *idxStr,
211965 int argc, sqlite3_value **argv
211966){
211967 StatCursor *pCsr = (StatCursor *)pCursor;
211968 StatTable *pTab = (StatTable*)(pCursor->pVtab);
211969 sqlite3_str *pSql; /* Query of btrees to analyze */
211970 char *zSql; /* String value of pSql */
211971 int iArg = 0; /* Count of argv[] parameters used so far */
211972 int rc = SQLITE_OK0; /* Result of this operation */
211973 const char *zName = 0; /* Only provide analysis of this table */
211974
211975 statResetCsr(pCsr);
211976 sqlite3_finalize(pCsr->pStmt);
211977 pCsr->pStmt = 0;
211978 if( idxNum & 0x01 ){
211979 /* schema=? constraint is present. Get its value */
211980 const char *zDbase = (const char*)sqlite3_value_text(argv[iArg++]);
211981 pCsr->iDb = sqlite3FindDbName(pTab->db, zDbase);
211982 if( pCsr->iDb<0 ){
211983 pCsr->iDb = 0;
211984 pCsr->isEof = 1;
211985 return SQLITE_OK0;
211986 }
211987 }else{
211988 pCsr->iDb = pTab->iDb;
211989 }
211990 if( idxNum & 0x02 ){
211991 /* name=? constraint is present */
211992 zName = (const char*)sqlite3_value_text(argv[iArg++]);
211993 }
211994 if( idxNum & 0x04 ){
211995 /* aggregate=? constraint is present */
211996 pCsr->isAgg = sqlite3_value_double(argv[iArg++])!=0.0;
211997 }else{
211998 pCsr->isAgg = 0;
211999 }
212000 pSql = sqlite3_str_new(pTab->db);
212001 sqlite3_str_appendf(pSql,
212002 "SELECT * FROM ("
212003 "SELECT 'sqlite_schema' AS name,1 AS rootpage,'table' AS type"
212004 " UNION ALL "
212005 "SELECT name,rootpage,type"
212006 " FROM \"%w\".sqlite_schema WHERE rootpage!=0)",
212007 pTab->db->aDb[pCsr->iDb].zDbSName);
212008 if( zName ){
212009 sqlite3_str_appendf(pSql, "WHERE name=%Q", zName);
212010 }
212011 if( idxNum & 0x08 ){
212012 sqlite3_str_appendf(pSql, " ORDER BY name");
212013 }
212014 zSql = sqlite3_str_finish(pSql);
212015 if( zSql==0 ){
212016 return SQLITE_NOMEM_BKPT7;
212017 }else{
212018 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
212019 sqlite3_free(zSql);
212020 }
212021
212022 if( rc==SQLITE_OK0 ){
212023 pCsr->iPage = -1;
212024 rc = statNext(pCursor);
212025 }
212026 return rc;
212027}
212028
212029static int statColumn(
212030 sqlite3_vtab_cursor *pCursor,
212031 sqlite3_context *ctx,
212032 int i
212033){
212034 StatCursor *pCsr = (StatCursor *)pCursor;
212035 switch( i ){
212036 case 0: /* name */
212037 sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
212038 break;
212039 case 1: /* path */
212040 if( !pCsr->isAgg ){
212041 sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
212042 }
212043 break;
212044 case 2: /* pageno */
212045 if( pCsr->isAgg ){
212046 sqlite3_result_int64(ctx, pCsr->nPage);
212047 }else{
212048 sqlite3_result_int64(ctx, pCsr->iPageno);
212049 }
212050 break;
212051 case 3: /* pagetype */
212052 if( !pCsr->isAgg ){
212053 sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
212054 }
212055 break;
212056 case 4: /* ncell */
212057 sqlite3_result_int(ctx, pCsr->nCell);
212058 break;
212059 case 5: /* payload */
212060 sqlite3_result_int(ctx, pCsr->nPayload);
212061 break;
212062 case 6: /* unused */
212063 sqlite3_result_int(ctx, pCsr->nUnused);
212064 break;
212065 case 7: /* mx_payload */
212066 sqlite3_result_int(ctx, pCsr->nMxPayload);
212067 break;
212068 case 8: /* pgoffset */
212069 if( !pCsr->isAgg ){
212070 sqlite3_result_int64(ctx, pCsr->iOffset);
212071 }
212072 break;
212073 case 9: /* pgsize */
212074 sqlite3_result_int(ctx, pCsr->szPage);
212075 break;
212076 case 10: { /* schema */
212077 sqlite3 *db = sqlite3_context_db_handle(ctx);
212078 int iDb = pCsr->iDb;
212079 sqlite3_result_text(ctx, db->aDb[iDb].zDbSName, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
212080 break;
212081 }
212082 default: { /* aggregate */
212083 sqlite3_result_int(ctx, pCsr->isAgg);
212084 break;
212085 }
212086 }
212087 return SQLITE_OK0;
212088}
212089
212090static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
212091 StatCursor *pCsr = (StatCursor *)pCursor;
212092 *pRowid = pCsr->iPageno;
212093 return SQLITE_OK0;
212094}
212095
212096/*
212097** Invoke this routine to register the "dbstat" virtual table module
212098*/
212099SQLITE_PRIVATEstatic int sqlite3DbstatRegister(sqlite3 *db){
212100 static sqlite3_module dbstat_module = {
212101 0, /* iVersion */
212102 statConnect, /* xCreate */
212103 statConnect, /* xConnect */
212104 statBestIndex, /* xBestIndex */
212105 statDisconnect, /* xDisconnect */
212106 statDisconnect, /* xDestroy */
212107 statOpen, /* xOpen - open a cursor */
212108 statClose, /* xClose - close a cursor */
212109 statFilter, /* xFilter - configure scan constraints */
212110 statNext, /* xNext - advance a cursor */
212111 statEof, /* xEof - check for end of scan */
212112 statColumn, /* xColumn - read data */
212113 statRowid, /* xRowid - read data */
212114 0, /* xUpdate */
212115 0, /* xBegin */
212116 0, /* xSync */
212117 0, /* xCommit */
212118 0, /* xRollback */
212119 0, /* xFindMethod */
212120 0, /* xRename */
212121 0, /* xSavepoint */
212122 0, /* xRelease */
212123 0, /* xRollbackTo */
212124 0 /* xShadowName */
212125 };
212126 return sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
212127}
212128#elif defined(SQLITE_ENABLE_DBSTAT_VTAB1)
212129SQLITE_PRIVATEstatic int sqlite3DbstatRegister(sqlite3 *db){ return SQLITE_OK0; }
212130#endif /* SQLITE_ENABLE_DBSTAT_VTAB */
212131
212132/************** End of dbstat.c **********************************************/
212133/************** Begin file dbpage.c ******************************************/
212134/*
212135** 2017-10-11
212136**
212137** The author disclaims copyright to this source code. In place of
212138** a legal notice, here is a blessing:
212139**
212140** May you do good and not evil.
212141** May you find forgiveness for yourself and forgive others.
212142** May you share freely, never taking more than you give.
212143**
212144******************************************************************************
212145**
212146** This file contains an implementation of the "sqlite_dbpage" virtual table.
212147**
212148** The sqlite_dbpage virtual table is used to read or write whole raw
212149** pages of the database file. The pager interface is used so that
212150** uncommitted changes and changes recorded in the WAL file are correctly
212151** retrieved.
212152**
212153** Usage example:
212154**
212155** SELECT data FROM sqlite_dbpage('aux1') WHERE pgno=123;
212156**
212157** This is an eponymous virtual table so it does not need to be created before
212158** use. The optional argument to the sqlite_dbpage() table name is the
212159** schema for the database file that is to be read. The default schema is
212160** "main".
212161**
212162** The data field of sqlite_dbpage table can be updated. The new
212163** value must be a BLOB which is the correct page size, otherwise the
212164** update fails. Rows may not be deleted or inserted.
212165*/
212166
212167/* #include "sqliteInt.h" ** Requires access to internal data structures ** */
212168#if (defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)) \
212169 && !defined(SQLITE_OMIT_VIRTUALTABLE)
212170
212171typedef struct DbpageTable DbpageTable;
212172typedef struct DbpageCursor DbpageCursor;
212173
212174struct DbpageCursor {
212175 sqlite3_vtab_cursor base; /* Base class. Must be first */
212176 int pgno; /* Current page number */
212177 int mxPgno; /* Last page to visit on this scan */
212178 Pager *pPager; /* Pager being read/written */
212179 DbPage *pPage1; /* Page 1 of the database */
212180 int iDb; /* Index of database to analyze */
212181 int szPage; /* Size of each page in bytes */
212182};
212183
212184struct DbpageTable {
212185 sqlite3_vtab base; /* Base class. Must be first */
212186 sqlite3 *db; /* The database */
212187};
212188
212189/* Columns */
212190#define DBPAGE_COLUMN_PGNO 0
212191#define DBPAGE_COLUMN_DATA 1
212192#define DBPAGE_COLUMN_SCHEMA 2
212193
212194
212195
212196/*
212197** Connect to or create a dbpagevfs virtual table.
212198*/
212199static int dbpageConnect(
212200 sqlite3 *db,
212201 void *pAux,
212202 int argc, const char *const*argv,
212203 sqlite3_vtab **ppVtab,
212204 char **pzErr
212205){
212206 DbpageTable *pTab = 0;
212207 int rc = SQLITE_OK0;
212208
212209 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY3);
212210 rc = sqlite3_declare_vtab(db,
212211 "CREATE TABLE x(pgno INTEGER PRIMARY KEY, data BLOB, schema HIDDEN)");
212212 if( rc==SQLITE_OK0 ){
212213 pTab = (DbpageTable *)sqlite3_malloc64(sizeof(DbpageTable));
212214 if( pTab==0 ) rc = SQLITE_NOMEM_BKPT7;
212215 }
212216
212217 assert( rc==SQLITE_OK || pTab==0 )((void) (0));
212218 if( rc==SQLITE_OK0 ){
212219 memset(pTab, 0, sizeof(DbpageTable));
212220 pTab->db = db;
212221 }
212222
212223 *ppVtab = (sqlite3_vtab*)pTab;
212224 return rc;
212225}
212226
212227/*
212228** Disconnect from or destroy a dbpagevfs virtual table.
212229*/
212230static int dbpageDisconnect(sqlite3_vtab *pVtab){
212231 sqlite3_free(pVtab);
212232 return SQLITE_OK0;
212233}
212234
212235/*
212236** idxNum:
212237**
212238** 0 schema=main, full table scan
212239** 1 schema=main, pgno=?1
212240** 2 schema=?1, full table scan
212241** 3 schema=?1, pgno=?2
212242*/
212243static int dbpageBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
212244 int i;
212245 int iPlan = 0;
212246
212247 /* If there is a schema= constraint, it must be honored. Report a
212248 ** ridiculously large estimated cost if the schema= constraint is
212249 ** unavailable
212250 */
212251 for(i=0; i<pIdxInfo->nConstraint; i++){
212252 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
212253 if( p->iColumn!=DBPAGE_COLUMN_SCHEMA ) continue;
212254 if( p->op!=SQLITE_INDEX_CONSTRAINT_EQ2 ) continue;
212255 if( !p->usable ){
212256 /* No solution. */
212257 return SQLITE_CONSTRAINT19;
212258 }
212259 iPlan = 2;
212260 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
212261 pIdxInfo->aConstraintUsage[i].omit = 1;
212262 break;
212263 }
212264
212265 /* If we reach this point, it means that either there is no schema=
212266 ** constraint (in which case we use the "main" schema) or else the
212267 ** schema constraint was accepted. Lower the estimated cost accordingly
212268 */
212269 pIdxInfo->estimatedCost = 1.0e6;
212270
212271 /* Check for constraints against pgno */
212272 for(i=0; i<pIdxInfo->nConstraint; i++){
212273 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
212274 if( p->usable && p->iColumn<=0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ2 ){
212275 pIdxInfo->estimatedRows = 1;
212276 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE1;
212277 pIdxInfo->estimatedCost = 1.0;
212278 pIdxInfo->aConstraintUsage[i].argvIndex = iPlan ? 2 : 1;
212279 pIdxInfo->aConstraintUsage[i].omit = 1;
212280 iPlan |= 1;
212281 break;
212282 }
212283 }
212284 pIdxInfo->idxNum = iPlan;
212285
212286 if( pIdxInfo->nOrderBy>=1
212287 && pIdxInfo->aOrderBy[0].iColumn<=0
212288 && pIdxInfo->aOrderBy[0].desc==0
212289 ){
212290 pIdxInfo->orderByConsumed = 1;
212291 }
212292 sqlite3VtabUsesAllSchemas(pIdxInfo);
212293 return SQLITE_OK0;
212294}
212295
212296/*
212297** Open a new dbpagevfs cursor.
212298*/
212299static int dbpageOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
212300 DbpageCursor *pCsr;
212301
212302 pCsr = (DbpageCursor *)sqlite3_malloc64(sizeof(DbpageCursor));
212303 if( pCsr==0 ){
212304 return SQLITE_NOMEM_BKPT7;
212305 }else{
212306 memset(pCsr, 0, sizeof(DbpageCursor));
212307 pCsr->base.pVtab = pVTab;
212308 pCsr->pgno = -1;
212309 }
212310
212311 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
212312 return SQLITE_OK0;
212313}
212314
212315/*
212316** Close a dbpagevfs cursor.
212317*/
212318static int dbpageClose(sqlite3_vtab_cursor *pCursor){
212319 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
212320 if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
212321 sqlite3_free(pCsr);
212322 return SQLITE_OK0;
212323}
212324
212325/*
212326** Move a dbpagevfs cursor to the next entry in the file.
212327*/
212328static int dbpageNext(sqlite3_vtab_cursor *pCursor){
212329 int rc = SQLITE_OK0;
212330 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
212331 pCsr->pgno++;
212332 return rc;
212333}
212334
212335static int dbpageEof(sqlite3_vtab_cursor *pCursor){
212336 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
212337 return pCsr->pgno > pCsr->mxPgno;
212338}
212339
212340/*
212341** idxNum:
212342**
212343** 0 schema=main, full table scan
212344** 1 schema=main, pgno=?1
212345** 2 schema=?1, full table scan
212346** 3 schema=?1, pgno=?2
212347**
212348** idxStr is not used
212349*/
212350static int dbpageFilter(
212351 sqlite3_vtab_cursor *pCursor,
212352 int idxNum, const char *idxStr,
212353 int argc, sqlite3_value **argv
212354){
212355 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
212356 DbpageTable *pTab = (DbpageTable *)pCursor->pVtab;
212357 int rc;
212358 sqlite3 *db = pTab->db;
212359 Btree *pBt;
212360
212361 /* Default setting is no rows of result */
212362 pCsr->pgno = 1;
212363 pCsr->mxPgno = 0;
212364
212365 if( idxNum & 2 ){
212366 const char *zSchema;
212367 assert( argc>=1 )((void) (0));
212368 zSchema = (const char*)sqlite3_value_text(argv[0]);
212369 pCsr->iDb = sqlite3FindDbName(db, zSchema);
212370 if( pCsr->iDb<0 ) return SQLITE_OK0;
212371 }else{
212372 pCsr->iDb = 0;
212373 }
212374 pBt = db->aDb[pCsr->iDb].pBt;
212375 if( pBt==0 ) return SQLITE_OK0;
212376 pCsr->pPager = sqlite3BtreePager(pBt);
212377 pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
212378 pCsr->mxPgno = sqlite3BtreeLastPage(pBt);
212379 if( idxNum & 1 ){
212380 assert( argc>(idxNum>>1) )((void) (0));
212381 pCsr->pgno = sqlite3_value_int(argv[idxNum>>1]);
212382 if( pCsr->pgno<1 || pCsr->pgno>pCsr->mxPgno ){
212383 pCsr->pgno = 1;
212384 pCsr->mxPgno = 0;
212385 }else{
212386 pCsr->mxPgno = pCsr->pgno;
212387 }
212388 }else{
212389 assert( pCsr->pgno==1 )((void) (0));
212390 }
212391 if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
212392 rc = sqlite3PagerGet(pCsr->pPager, 1, &pCsr->pPage1, 0);
212393 return rc;
212394}
212395
212396static int dbpageColumn(
212397 sqlite3_vtab_cursor *pCursor,
212398 sqlite3_context *ctx,
212399 int i
212400){
212401 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
212402 int rc = SQLITE_OK0;
212403 switch( i ){
212404 case 0: { /* pgno */
212405 sqlite3_result_int(ctx, pCsr->pgno);
212406 break;
212407 }
212408 case 1: { /* data */
212409 DbPage *pDbPage = 0;
212410 rc = sqlite3PagerGet(pCsr->pPager, pCsr->pgno, (DbPage**)&pDbPage, 0);
212411 if( rc==SQLITE_OK0 ){
212412 sqlite3_result_blob(ctx, sqlite3PagerGetData(pDbPage), pCsr->szPage,
212413 SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
212414 }
212415 sqlite3PagerUnref(pDbPage);
212416 break;
212417 }
212418 default: { /* schema */
212419 sqlite3 *db = sqlite3_context_db_handle(ctx);
212420 sqlite3_result_text(ctx, db->aDb[pCsr->iDb].zDbSName, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
212421 break;
212422 }
212423 }
212424 return SQLITE_OK0;
212425}
212426
212427static int dbpageRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
212428 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
212429 *pRowid = pCsr->pgno;
212430 return SQLITE_OK0;
212431}
212432
212433static int dbpageUpdate(
212434 sqlite3_vtab *pVtab,
212435 int argc,
212436 sqlite3_value **argv,
212437 sqlite_int64 *pRowid
212438){
212439 DbpageTable *pTab = (DbpageTable *)pVtab;
212440 Pgno pgno;
212441 DbPage *pDbPage = 0;
212442 int rc = SQLITE_OK0;
212443 char *zErr = 0;
212444 const char *zSchema;
212445 int iDb;
212446 Btree *pBt;
212447 Pager *pPager;
212448 int szPage;
212449
212450 if( pTab->db->flags & SQLITE_Defensive0x10000000 ){
212451 zErr = "read-only";
212452 goto update_fail;
212453 }
212454 if( argc==1 ){
212455 zErr = "cannot delete";
212456 goto update_fail;
212457 }
212458 pgno = sqlite3_value_int(argv[0]);
212459 if( (Pgno)sqlite3_value_int(argv[1])!=pgno ){
212460 zErr = "cannot insert";
212461 goto update_fail;
212462 }
212463 zSchema = (const char*)sqlite3_value_text(argv[4]);
212464 iDb = zSchema ? sqlite3FindDbName(pTab->db, zSchema) : -1;
212465 if( iDb<0 ){
212466 zErr = "no such schema";
212467 goto update_fail;
212468 }
212469 pBt = pTab->db->aDb[iDb].pBt;
212470 if( pgno<1 || pBt==0 || pgno>sqlite3BtreeLastPage(pBt) ){
212471 zErr = "bad page number";
212472 goto update_fail;
212473 }
212474 szPage = sqlite3BtreeGetPageSize(pBt);
212475 if( sqlite3_value_type(argv[3])!=SQLITE_BLOB4
212476 || sqlite3_value_bytes(argv[3])!=szPage
212477 ){
212478 zErr = "bad page value";
212479 goto update_fail;
212480 }
212481 pPager = sqlite3BtreePager(pBt);
212482 rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pDbPage, 0);
212483 if( rc==SQLITE_OK0 ){
212484 rc = sqlite3PagerWrite(pDbPage);
212485 if( rc==SQLITE_OK0 ){
212486 memcpy(sqlite3PagerGetData(pDbPage),
212487 sqlite3_value_blob(argv[3]),
212488 szPage);
212489 }
212490 }
212491 sqlite3PagerUnref(pDbPage);
212492 return rc;
212493
212494update_fail:
212495 sqlite3_free(pVtab->zErrMsg);
212496 pVtab->zErrMsg = sqlite3_mprintf("%s", zErr);
212497 return SQLITE_ERROR1;
212498}
212499
212500/* Since we do not know in advance which database files will be
212501** written by the sqlite_dbpage virtual table, start a write transaction
212502** on them all.
212503*/
212504static int dbpageBegin(sqlite3_vtab *pVtab){
212505 DbpageTable *pTab = (DbpageTable *)pVtab;
212506 sqlite3 *db = pTab->db;
212507 int i;
212508 for(i=0; i<db->nDb; i++){
212509 Btree *pBt = db->aDb[i].pBt;
212510 if( pBt ) sqlite3BtreeBeginTrans(pBt, 1, 0);
212511 }
212512 return SQLITE_OK0;
212513}
212514
212515
212516/*
212517** Invoke this routine to register the "dbpage" virtual table module
212518*/
212519SQLITE_PRIVATEstatic int sqlite3DbpageRegister(sqlite3 *db){
212520 static sqlite3_module dbpage_module = {
212521 0, /* iVersion */
212522 dbpageConnect, /* xCreate */
212523 dbpageConnect, /* xConnect */
212524 dbpageBestIndex, /* xBestIndex */
212525 dbpageDisconnect, /* xDisconnect */
212526 dbpageDisconnect, /* xDestroy */
212527 dbpageOpen, /* xOpen - open a cursor */
212528 dbpageClose, /* xClose - close a cursor */
212529 dbpageFilter, /* xFilter - configure scan constraints */
212530 dbpageNext, /* xNext - advance a cursor */
212531 dbpageEof, /* xEof - check for end of scan */
212532 dbpageColumn, /* xColumn - read data */
212533 dbpageRowid, /* xRowid - read data */
212534 dbpageUpdate, /* xUpdate */
212535 dbpageBegin, /* xBegin */
212536 0, /* xSync */
212537 0, /* xCommit */
212538 0, /* xRollback */
212539 0, /* xFindMethod */
212540 0, /* xRename */
212541 0, /* xSavepoint */
212542 0, /* xRelease */
212543 0, /* xRollbackTo */
212544 0 /* xShadowName */
212545 };
212546 return sqlite3_create_module(db, "sqlite_dbpage", &dbpage_module, 0);
212547}
212548#elif defined(SQLITE_ENABLE_DBPAGE_VTAB)
212549SQLITE_PRIVATEstatic int sqlite3DbpageRegister(sqlite3 *db){ return SQLITE_OK0; }
212550#endif /* SQLITE_ENABLE_DBSTAT_VTAB */
212551
212552/************** End of dbpage.c **********************************************/
212553/************** Begin file sqlite3session.c **********************************/
212554
212555#if defined(SQLITE_ENABLE_SESSION1) && defined(SQLITE_ENABLE_PREUPDATE_HOOK1)
212556/* #include "sqlite3session.h" */
212557/* #include <assert.h> */
212558/* #include <string.h> */
212559
212560#ifndef SQLITE_AMALGAMATION1
212561/* # include "sqliteInt.h" */
212562/* # include "vdbeInt.h" */
212563#endif
212564
212565typedef struct SessionTable SessionTable;
212566typedef struct SessionChange SessionChange;
212567typedef struct SessionBuffer SessionBuffer;
212568typedef struct SessionInput SessionInput;
212569
212570/*
212571** Minimum chunk size used by streaming versions of functions.
212572*/
212573#ifndef SESSIONS_STRM_CHUNK_SIZE1024
212574# ifdef SQLITE_TEST
212575# define SESSIONS_STRM_CHUNK_SIZE1024 64
212576# else
212577# define SESSIONS_STRM_CHUNK_SIZE1024 1024
212578# endif
212579#endif
212580
212581static int sessions_strm_chunk_size = SESSIONS_STRM_CHUNK_SIZE1024;
212582
212583typedef struct SessionHook SessionHook;
212584struct SessionHook {
212585 void *pCtx;
212586 int (*xOld)(void*,int,sqlite3_value**);
212587 int (*xNew)(void*,int,sqlite3_value**);
212588 int (*xCount)(void*);
212589 int (*xDepth)(void*);
212590};
212591
212592/*
212593** Session handle structure.
212594*/
212595struct sqlite3_session {
212596 sqlite3 *db; /* Database handle session is attached to */
212597 char *zDb; /* Name of database session is attached to */
212598 int bEnableSize; /* True if changeset_size() enabled */
212599 int bEnable; /* True if currently recording */
212600 int bIndirect; /* True if all changes are indirect */
212601 int bAutoAttach; /* True to auto-attach tables */
212602 int rc; /* Non-zero if an error has occurred */
212603 void *pFilterCtx; /* First argument to pass to xTableFilter */
212604 int (*xTableFilter)(void *pCtx, const char *zTab);
212605 i64 nMalloc; /* Number of bytes of data allocated */
212606 i64 nMaxChangesetSize;
212607 sqlite3_value *pZeroBlob; /* Value containing X'' */
212608 sqlite3_session *pNext; /* Next session object on same db. */
212609 SessionTable *pTable; /* List of attached tables */
212610 SessionHook hook; /* APIs to grab new and old data with */
212611};
212612
212613/*
212614** Instances of this structure are used to build strings or binary records.
212615*/
212616struct SessionBuffer {
212617 u8 *aBuf; /* Pointer to changeset buffer */
212618 int nBuf; /* Size of buffer aBuf */
212619 int nAlloc; /* Size of allocation containing aBuf */
212620};
212621
212622/*
212623** An object of this type is used internally as an abstraction for
212624** input data. Input data may be supplied either as a single large buffer
212625** (e.g. sqlite3changeset_start()) or using a stream function (e.g.
212626** sqlite3changeset_start_strm()).
212627*/
212628struct SessionInput {
212629 int bNoDiscard; /* If true, do not discard in InputBuffer() */
212630 int iCurrent; /* Offset in aData[] of current change */
212631 int iNext; /* Offset in aData[] of next change */
212632 u8 *aData; /* Pointer to buffer containing changeset */
212633 int nData; /* Number of bytes in aData */
212634
212635 SessionBuffer buf; /* Current read buffer */
212636 int (*xInput)(void*, void*, int*); /* Input stream call (or NULL) */
212637 void *pIn; /* First argument to xInput */
212638 int bEof; /* Set to true after xInput finished */
212639};
212640
212641/*
212642** Structure for changeset iterators.
212643*/
212644struct sqlite3_changeset_iter {
212645 SessionInput in; /* Input buffer or stream */
212646 SessionBuffer tblhdr; /* Buffer to hold apValue/zTab/abPK/ */
212647 int bPatchset; /* True if this is a patchset */
212648 int bInvert; /* True to invert changeset */
212649 int bSkipEmpty; /* Skip noop UPDATE changes */
212650 int rc; /* Iterator error code */
212651 sqlite3_stmt *pConflict; /* Points to conflicting row, if any */
212652 char *zTab; /* Current table */
212653 int nCol; /* Number of columns in zTab */
212654 int op; /* Current operation */
212655 int bIndirect; /* True if current change was indirect */
212656 u8 *abPK; /* Primary key array */
212657 sqlite3_value **apValue; /* old.* and new.* values */
212658};
212659
212660/*
212661** Each session object maintains a set of the following structures, one
212662** for each table the session object is monitoring. The structures are
212663** stored in a linked list starting at sqlite3_session.pTable.
212664**
212665** The keys of the SessionTable.aChange[] hash table are all rows that have
212666** been modified in any way since the session object was attached to the
212667** table.
212668**
212669** The data associated with each hash-table entry is a structure containing
212670** a subset of the initial values that the modified row contained at the
212671** start of the session. Or no initial values if the row was inserted.
212672*/
212673struct SessionTable {
212674 SessionTable *pNext;
212675 char *zName; /* Local name of table */
212676 int nCol; /* Number of columns in table zName */
212677 int bStat1; /* True if this is sqlite_stat1 */
212678 const char **azCol; /* Column names */
212679 u8 *abPK; /* Array of primary key flags */
212680 int nEntry; /* Total number of entries in hash table */
212681 int nChange; /* Size of apChange[] array */
212682 SessionChange **apChange; /* Hash table buckets */
212683};
212684
212685/*
212686** RECORD FORMAT:
212687**
212688** The following record format is similar to (but not compatible with) that
212689** used in SQLite database files. This format is used as part of the
212690** change-set binary format, and so must be architecture independent.
212691**
212692** Unlike the SQLite database record format, each field is self-contained -
212693** there is no separation of header and data. Each field begins with a
212694** single byte describing its type, as follows:
212695**
212696** 0x00: Undefined value.
212697** 0x01: Integer value.
212698** 0x02: Real value.
212699** 0x03: Text value.
212700** 0x04: Blob value.
212701** 0x05: SQL NULL value.
212702**
212703** Note that the above match the definitions of SQLITE_INTEGER, SQLITE_TEXT
212704** and so on in sqlite3.h. For undefined and NULL values, the field consists
212705** only of the single type byte. For other types of values, the type byte
212706** is followed by:
212707**
212708** Text values:
212709** A varint containing the number of bytes in the value (encoded using
212710** UTF-8). Followed by a buffer containing the UTF-8 representation
212711** of the text value. There is no nul terminator.
212712**
212713** Blob values:
212714** A varint containing the number of bytes in the value, followed by
212715** a buffer containing the value itself.
212716**
212717** Integer values:
212718** An 8-byte big-endian integer value.
212719**
212720** Real values:
212721** An 8-byte big-endian IEEE 754-2008 real value.
212722**
212723** Varint values are encoded in the same way as varints in the SQLite
212724** record format.
212725**
212726** CHANGESET FORMAT:
212727**
212728** A changeset is a collection of DELETE, UPDATE and INSERT operations on
212729** one or more tables. Operations on a single table are grouped together,
212730** but may occur in any order (i.e. deletes, updates and inserts are all
212731** mixed together).
212732**
212733** Each group of changes begins with a table header:
212734**
212735** 1 byte: Constant 0x54 (capital 'T')
212736** Varint: Number of columns in the table.
212737** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
212738** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
212739**
212740** Followed by one or more changes to the table.
212741**
212742** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
212743** 1 byte: The "indirect-change" flag.
212744** old.* record: (delete and update only)
212745** new.* record: (insert and update only)
212746**
212747** The "old.*" and "new.*" records, if present, are N field records in the
212748** format described above under "RECORD FORMAT", where N is the number of
212749** columns in the table. The i'th field of each record is associated with
212750** the i'th column of the table, counting from left to right in the order
212751** in which columns were declared in the CREATE TABLE statement.
212752**
212753** The new.* record that is part of each INSERT change contains the values
212754** that make up the new row. Similarly, the old.* record that is part of each
212755** DELETE change contains the values that made up the row that was deleted
212756** from the database. In the changeset format, the records that are part
212757** of INSERT or DELETE changes never contain any undefined (type byte 0x00)
212758** fields.
212759**
212760** Within the old.* record associated with an UPDATE change, all fields
212761** associated with table columns that are not PRIMARY KEY columns and are
212762** not modified by the UPDATE change are set to "undefined". Other fields
212763** are set to the values that made up the row before the UPDATE that the
212764** change records took place. Within the new.* record, fields associated
212765** with table columns modified by the UPDATE change contain the new
212766** values. Fields associated with table columns that are not modified
212767** are set to "undefined".
212768**
212769** PATCHSET FORMAT:
212770**
212771** A patchset is also a collection of changes. It is similar to a changeset,
212772** but leaves undefined those fields that are not useful if no conflict
212773** resolution is required when applying the changeset.
212774**
212775** Each group of changes begins with a table header:
212776**
212777** 1 byte: Constant 0x50 (capital 'P')
212778** Varint: Number of columns in the table.
212779** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
212780** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
212781**
212782** Followed by one or more changes to the table.
212783**
212784** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
212785** 1 byte: The "indirect-change" flag.
212786** single record: (PK fields for DELETE, PK and modified fields for UPDATE,
212787** full record for INSERT).
212788**
212789** As in the changeset format, each field of the single record that is part
212790** of a patchset change is associated with the correspondingly positioned
212791** table column, counting from left to right within the CREATE TABLE
212792** statement.
212793**
212794** For a DELETE change, all fields within the record except those associated
212795** with PRIMARY KEY columns are omitted. The PRIMARY KEY fields contain the
212796** values identifying the row to delete.
212797**
212798** For an UPDATE change, all fields except those associated with PRIMARY KEY
212799** columns and columns that are modified by the UPDATE are set to "undefined".
212800** PRIMARY KEY fields contain the values identifying the table row to update,
212801** and fields associated with modified columns contain the new column values.
212802**
212803** The records associated with INSERT changes are in the same format as for
212804** changesets. It is not possible for a record associated with an INSERT
212805** change to contain a field set to "undefined".
212806**
212807** REBASE BLOB FORMAT:
212808**
212809** A rebase blob may be output by sqlite3changeset_apply_v2() and its
212810** streaming equivalent for use with the sqlite3_rebaser APIs to rebase
212811** existing changesets. A rebase blob contains one entry for each conflict
212812** resolved using either the OMIT or REPLACE strategies within the apply_v2()
212813** call.
212814**
212815** The format used for a rebase blob is very similar to that used for
212816** changesets. All entries related to a single table are grouped together.
212817**
212818** Each group of entries begins with a table header in changeset format:
212819**
212820** 1 byte: Constant 0x54 (capital 'T')
212821** Varint: Number of columns in the table.
212822** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
212823** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
212824**
212825** Followed by one or more entries associated with the table.
212826**
212827** 1 byte: Either SQLITE_INSERT (0x12), DELETE (0x09).
212828** 1 byte: Flag. 0x01 for REPLACE, 0x00 for OMIT.
212829** record: (in the record format defined above).
212830**
212831** In a rebase blob, the first field is set to SQLITE_INSERT if the change
212832** that caused the conflict was an INSERT or UPDATE, or to SQLITE_DELETE if
212833** it was a DELETE. The second field is set to 0x01 if the conflict
212834** resolution strategy was REPLACE, or 0x00 if it was OMIT.
212835**
212836** If the change that caused the conflict was a DELETE, then the single
212837** record is a copy of the old.* record from the original changeset. If it
212838** was an INSERT, then the single record is a copy of the new.* record. If
212839** the conflicting change was an UPDATE, then the single record is a copy
212840** of the new.* record with the PK fields filled in based on the original
212841** old.* record.
212842*/
212843
212844/*
212845** For each row modified during a session, there exists a single instance of
212846** this structure stored in a SessionTable.aChange[] hash table.
212847*/
212848struct SessionChange {
212849 u8 op; /* One of UPDATE, DELETE, INSERT */
212850 u8 bIndirect; /* True if this change is "indirect" */
212851 int nMaxSize; /* Max size of eventual changeset record */
212852 int nRecord; /* Number of bytes in buffer aRecord[] */
212853 u8 *aRecord; /* Buffer containing old.* record */
212854 SessionChange *pNext; /* For hash-table collisions */
212855};
212856
212857/*
212858** Write a varint with value iVal into the buffer at aBuf. Return the
212859** number of bytes written.
212860*/
212861static int sessionVarintPut(u8 *aBuf, int iVal){
212862 return putVarint32(aBuf, iVal)(u8)(((u32)(iVal)<(u32)0x80)?(*(aBuf)=(unsigned char)(iVal
)),1: sqlite3PutVarint((aBuf),(iVal)))
;
212863}
212864
212865/*
212866** Return the number of bytes required to store value iVal as a varint.
212867*/
212868static int sessionVarintLen(int iVal){
212869 return sqlite3VarintLen(iVal);
212870}
212871
212872/*
212873** Read a varint value from aBuf[] into *piVal. Return the number of
212874** bytes read.
212875*/
212876static int sessionVarintGet(u8 *aBuf, int *piVal){
212877 return getVarint32(aBuf, *piVal)(u8)((*(aBuf)<(u8)0x80)?((*piVal)=(u32)*(aBuf)),1:sqlite3GetVarint32
((aBuf),(u32 *)&(*piVal)))
;
212878}
212879
212880/* Load an unaligned and unsigned 32-bit integer */
212881#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])
212882
212883/*
212884** Read a 64-bit big-endian integer value from buffer aRec[]. Return
212885** the value read.
212886*/
212887static sqlite3_int64 sessionGetI64(u8 *aRec){
212888 u64 x = SESSION_UINT32(aRec)(((u32)(aRec)[0]<<24)|((aRec)[1]<<16)|((aRec)[2]<<
8)|(aRec)[3])
;
212889 u32 y = SESSION_UINT32(aRec+4)(((u32)(aRec+4)[0]<<24)|((aRec+4)[1]<<16)|((aRec+
4)[2]<<8)|(aRec+4)[3])
;
212890 x = (x<<32) + y;
212891 return (sqlite3_int64)x;
212892}
212893
212894/*
212895** Write a 64-bit big-endian integer value to the buffer aBuf[].
212896*/
212897static void sessionPutI64(u8 *aBuf, sqlite3_int64 i){
212898 aBuf[0] = (i>>56) & 0xFF;
212899 aBuf[1] = (i>>48) & 0xFF;
212900 aBuf[2] = (i>>40) & 0xFF;
212901 aBuf[3] = (i>>32) & 0xFF;
212902 aBuf[4] = (i>>24) & 0xFF;
212903 aBuf[5] = (i>>16) & 0xFF;
212904 aBuf[6] = (i>> 8) & 0xFF;
212905 aBuf[7] = (i>> 0) & 0xFF;
212906}
212907
212908/*
212909** This function is used to serialize the contents of value pValue (see
212910** comment titled "RECORD FORMAT" above).
212911**
212912** If it is non-NULL, the serialized form of the value is written to
212913** buffer aBuf. *pnWrite is set to the number of bytes written before
212914** returning. Or, if aBuf is NULL, the only thing this function does is
212915** set *pnWrite.
212916**
212917** If no error occurs, SQLITE_OK is returned. Or, if an OOM error occurs
212918** within a call to sqlite3_value_text() (may fail if the db is utf-16))
212919** SQLITE_NOMEM is returned.
212920*/
212921static int sessionSerializeValue(
212922 u8 *aBuf, /* If non-NULL, write serialized value here */
212923 sqlite3_value *pValue, /* Value to serialize */
212924 sqlite3_int64 *pnWrite /* IN/OUT: Increment by bytes written */
212925){
212926 int nByte; /* Size of serialized value in bytes */
212927
212928 if( pValue ){
212929 int eType; /* Value type (SQLITE_NULL, TEXT etc.) */
212930
212931 eType = sqlite3_value_type(pValue);
212932 if( aBuf ) aBuf[0] = eType;
212933
212934 switch( eType ){
212935 case SQLITE_NULL5:
212936 nByte = 1;
212937 break;
212938
212939 case SQLITE_INTEGER1:
212940 case SQLITE_FLOAT2:
212941 if( aBuf ){
212942 /* TODO: SQLite does something special to deal with mixed-endian
212943 ** floating point values (e.g. ARM7). This code probably should
212944 ** too. */
212945 u64 i;
212946 if( eType==SQLITE_INTEGER1 ){
212947 i = (u64)sqlite3_value_int64(pValue);
212948 }else{
212949 double r;
212950 assert( sizeof(double)==8 && sizeof(u64)==8 )((void) (0));
212951 r = sqlite3_value_double(pValue);
212952 memcpy(&i, &r, 8);
212953 }
212954 sessionPutI64(&aBuf[1], i);
212955 }
212956 nByte = 9;
212957 break;
212958
212959 default: {
212960 u8 *z;
212961 int n;
212962 int nVarint;
212963
212964 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB )((void) (0));
212965 if( eType==SQLITE_TEXT3 ){
212966 z = (u8 *)sqlite3_value_text(pValue);
212967 }else{
212968 z = (u8 *)sqlite3_value_blob(pValue);
212969 }
212970 n = sqlite3_value_bytes(pValue);
212971 if( z==0 && (eType!=SQLITE_BLOB4 || n>0) ) return SQLITE_NOMEM7;
212972 nVarint = sessionVarintLen(n);
212973
212974 if( aBuf ){
212975 sessionVarintPut(&aBuf[1], n);
212976 if( n>0 ) memcpy(&aBuf[nVarint + 1], z, n);
212977 }
212978
212979 nByte = 1 + nVarint + n;
212980 break;
212981 }
212982 }
212983 }else{
212984 nByte = 1;
212985 if( aBuf ) aBuf[0] = '\0';
212986 }
212987
212988 if( pnWrite ) *pnWrite += nByte;
212989 return SQLITE_OK0;
212990}
212991
212992/*
212993** Allocate and return a pointer to a buffer nByte bytes in size. If
212994** pSession is not NULL, increase the sqlite3_session.nMalloc variable
212995** by the number of bytes allocated.
212996*/
212997static void *sessionMalloc64(sqlite3_session *pSession, i64 nByte){
212998 void *pRet = sqlite3_malloc64(nByte);
212999 if( pSession ) pSession->nMalloc += sqlite3_msize(pRet);
213000 return pRet;
213001}
213002
213003/*
213004** Free buffer pFree, which must have been allocated by an earlier
213005** call to sessionMalloc64(). If pSession is not NULL, decrease the
213006** sqlite3_session.nMalloc counter by the number of bytes freed.
213007*/
213008static void sessionFree(sqlite3_session *pSession, void *pFree){
213009 if( pSession ) pSession->nMalloc -= sqlite3_msize(pFree);
213010 sqlite3_free(pFree);
213011}
213012
213013/*
213014** This macro is used to calculate hash key values for data structures. In
213015** order to use this macro, the entire data structure must be represented
213016** as a series of unsigned integers. In order to calculate a hash-key value
213017** for a data structure represented as three such integers, the macro may
213018** then be used as follows:
213019**
213020** int hash_key_value;
213021** hash_key_value = HASH_APPEND(0, <value 1>);
213022** hash_key_value = HASH_APPEND(hash_key_value, <value 2>);
213023** hash_key_value = HASH_APPEND(hash_key_value, <value 3>);
213024**
213025** In practice, the data structures this macro is used for are the primary
213026** key values of modified rows.
213027*/
213028#define HASH_APPEND(hash, add)((hash) << 3) ^ (hash) ^ (unsigned int)(add) ((hash) << 3) ^ (hash) ^ (unsigned int)(add)
213029
213030/*
213031** Append the hash of the 64-bit integer passed as the second argument to the
213032** hash-key value passed as the first. Return the new hash-key value.
213033*/
213034static unsigned int sessionHashAppendI64(unsigned int h, i64 i){
213035 h = HASH_APPEND(h, i & 0xFFFFFFFF)((h) << 3) ^ (h) ^ (unsigned int)(i & 0xFFFFFFFF);
213036 return HASH_APPEND(h, (i>>32)&0xFFFFFFFF)((h) << 3) ^ (h) ^ (unsigned int)((i>>32)&0xFFFFFFFF
)
;
213037}
213038
213039/*
213040** Append the hash of the blob passed via the second and third arguments to
213041** the hash-key value passed as the first. Return the new hash-key value.
213042*/
213043static unsigned int sessionHashAppendBlob(unsigned int h, int n, const u8 *z){
213044 int i;
213045 for(i=0; i<n; i++) h = HASH_APPEND(h, z[i])((h) << 3) ^ (h) ^ (unsigned int)(z[i]);
213046 return h;
213047}
213048
213049/*
213050** Append the hash of the data type passed as the second argument to the
213051** hash-key value passed as the first. Return the new hash-key value.
213052*/
213053static unsigned int sessionHashAppendType(unsigned int h, int eType){
213054 return HASH_APPEND(h, eType)((h) << 3) ^ (h) ^ (unsigned int)(eType);
213055}
213056
213057/*
213058** This function may only be called from within a pre-update callback.
213059** It calculates a hash based on the primary key values of the old.* or
213060** new.* row currently available and, assuming no error occurs, writes it to
213061** *piHash before returning. If the primary key contains one or more NULL
213062** values, *pbNullPK is set to true before returning.
213063**
213064** If an error occurs, an SQLite error code is returned and the final values
213065** of *piHash asn *pbNullPK are undefined. Otherwise, SQLITE_OK is returned
213066** and the output variables are set as described above.
213067*/
213068static int sessionPreupdateHash(
213069 sqlite3_session *pSession, /* Session object that owns pTab */
213070 SessionTable *pTab, /* Session table handle */
213071 int bNew, /* True to hash the new.* PK */
213072 int *piHash, /* OUT: Hash value */
213073 int *pbNullPK /* OUT: True if there are NULL values in PK */
213074){
213075 unsigned int h = 0; /* Hash value to return */
213076 int i; /* Used to iterate through columns */
213077
213078 assert( *pbNullPK==0 )((void) (0));
213079 assert( pTab->nCol==pSession->hook.xCount(pSession->hook.pCtx) )((void) (0));
213080 for(i=0; i<pTab->nCol; i++){
213081 if( pTab->abPK[i] ){
213082 int rc;
213083 int eType;
213084 sqlite3_value *pVal;
213085
213086 if( bNew ){
213087 rc = pSession->hook.xNew(pSession->hook.pCtx, i, &pVal);
213088 }else{
213089 rc = pSession->hook.xOld(pSession->hook.pCtx, i, &pVal);
213090 }
213091 if( rc!=SQLITE_OK0 ) return rc;
213092
213093 eType = sqlite3_value_type(pVal);
213094 h = sessionHashAppendType(h, eType);
213095 if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
213096 i64 iVal;
213097 if( eType==SQLITE_INTEGER1 ){
213098 iVal = sqlite3_value_int64(pVal);
213099 }else{
213100 double rVal = sqlite3_value_double(pVal);
213101 assert( sizeof(iVal)==8 && sizeof(rVal)==8 )((void) (0));
213102 memcpy(&iVal, &rVal, 8);
213103 }
213104 h = sessionHashAppendI64(h, iVal);
213105 }else if( eType==SQLITE_TEXT3 || eType==SQLITE_BLOB4 ){
213106 const u8 *z;
213107 int n;
213108 if( eType==SQLITE_TEXT3 ){
213109 z = (const u8 *)sqlite3_value_text(pVal);
213110 }else{
213111 z = (const u8 *)sqlite3_value_blob(pVal);
213112 }
213113 n = sqlite3_value_bytes(pVal);
213114 if( !z && (eType!=SQLITE_BLOB4 || n>0) ) return SQLITE_NOMEM7;
213115 h = sessionHashAppendBlob(h, n, z);
213116 }else{
213117 assert( eType==SQLITE_NULL )((void) (0));
213118 assert( pTab->bStat1==0 || i!=1 )((void) (0));
213119 *pbNullPK = 1;
213120 }
213121 }
213122 }
213123
213124 *piHash = (h % pTab->nChange);
213125 return SQLITE_OK0;
213126}
213127
213128/*
213129** The buffer that the argument points to contains a serialized SQL value.
213130** Return the number of bytes of space occupied by the value (including
213131** the type byte).
213132*/
213133static int sessionSerialLen(u8 *a){
213134 int e = *a;
213135 int n;
213136 if( e==0 || e==0xFF ) return 1;
213137 if( e==SQLITE_NULL5 ) return 1;
213138 if( e==SQLITE_INTEGER1 || e==SQLITE_FLOAT2 ) return 9;
213139 return sessionVarintGet(&a[1], &n) + 1 + n;
213140}
213141
213142/*
213143** Based on the primary key values stored in change aRecord, calculate a
213144** hash key. Assume the has table has nBucket buckets. The hash keys
213145** calculated by this function are compatible with those calculated by
213146** sessionPreupdateHash().
213147**
213148** The bPkOnly argument is non-zero if the record at aRecord[] is from
213149** a patchset DELETE. In this case the non-PK fields are omitted entirely.
213150*/
213151static unsigned int sessionChangeHash(
213152 SessionTable *pTab, /* Table handle */
213153 int bPkOnly, /* Record consists of PK fields only */
213154 u8 *aRecord, /* Change record */
213155 int nBucket /* Assume this many buckets in hash table */
213156){
213157 unsigned int h = 0; /* Value to return */
213158 int i; /* Used to iterate through columns */
213159 u8 *a = aRecord; /* Used to iterate through change record */
213160
213161 for(i=0; i<pTab->nCol; i++){
213162 int eType = *a;
213163 int isPK = pTab->abPK[i];
213164 if( bPkOnly && isPK==0 ) continue;
213165
213166 /* It is not possible for eType to be SQLITE_NULL here. The session
213167 ** module does not record changes for rows with NULL values stored in
213168 ** primary key columns. */
213169 assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT((void) (0))
213170 || eType==SQLITE_TEXT || eType==SQLITE_BLOB((void) (0))
213171 || eType==SQLITE_NULL || eType==0((void) (0))
213172 )((void) (0));
213173 assert( !isPK || (eType!=0 && eType!=SQLITE_NULL) )((void) (0));
213174
213175 if( isPK ){
213176 a++;
213177 h = sessionHashAppendType(h, eType);
213178 if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
213179 h = sessionHashAppendI64(h, sessionGetI64(a));
213180 a += 8;
213181 }else{
213182 int n;
213183 a += sessionVarintGet(a, &n);
213184 h = sessionHashAppendBlob(h, n, a);
213185 a += n;
213186 }
213187 }else{
213188 a += sessionSerialLen(a);
213189 }
213190 }
213191 return (h % nBucket);
213192}
213193
213194/*
213195** Arguments aLeft and aRight are pointers to change records for table pTab.
213196** This function returns true if the two records apply to the same row (i.e.
213197** have the same values stored in the primary key columns), or false
213198** otherwise.
213199*/
213200static int sessionChangeEqual(
213201 SessionTable *pTab, /* Table used for PK definition */
213202 int bLeftPkOnly, /* True if aLeft[] contains PK fields only */
213203 u8 *aLeft, /* Change record */
213204 int bRightPkOnly, /* True if aRight[] contains PK fields only */
213205 u8 *aRight /* Change record */
213206){
213207 u8 *a1 = aLeft; /* Cursor to iterate through aLeft */
213208 u8 *a2 = aRight; /* Cursor to iterate through aRight */
213209 int iCol; /* Used to iterate through table columns */
213210
213211 for(iCol=0; iCol<pTab->nCol; iCol++){
213212 if( pTab->abPK[iCol] ){
213213 int n1 = sessionSerialLen(a1);
213214 int n2 = sessionSerialLen(a2);
213215
213216 if( n1!=n2 || memcmp(a1, a2, n1) ){
213217 return 0;
213218 }
213219 a1 += n1;
213220 a2 += n2;
213221 }else{
213222 if( bLeftPkOnly==0 ) a1 += sessionSerialLen(a1);
213223 if( bRightPkOnly==0 ) a2 += sessionSerialLen(a2);
213224 }
213225 }
213226
213227 return 1;
213228}
213229
213230/*
213231** Arguments aLeft and aRight both point to buffers containing change
213232** records with nCol columns. This function "merges" the two records into
213233** a single records which is written to the buffer at *paOut. *paOut is
213234** then set to point to one byte after the last byte written before
213235** returning.
213236**
213237** The merging of records is done as follows: For each column, if the
213238** aRight record contains a value for the column, copy the value from
213239** their. Otherwise, if aLeft contains a value, copy it. If neither
213240** record contains a value for a given column, then neither does the
213241** output record.
213242*/
213243static void sessionMergeRecord(
213244 u8 **paOut,
213245 int nCol,
213246 u8 *aLeft,
213247 u8 *aRight
213248){
213249 u8 *a1 = aLeft; /* Cursor used to iterate through aLeft */
213250 u8 *a2 = aRight; /* Cursor used to iterate through aRight */
213251 u8 *aOut = *paOut; /* Output cursor */
213252 int iCol; /* Used to iterate from 0 to nCol */
213253
213254 for(iCol=0; iCol<nCol; iCol++){
213255 int n1 = sessionSerialLen(a1);
213256 int n2 = sessionSerialLen(a2);
213257 if( *a2 ){
213258 memcpy(aOut, a2, n2);
213259 aOut += n2;
213260 }else{
213261 memcpy(aOut, a1, n1);
213262 aOut += n1;
213263 }
213264 a1 += n1;
213265 a2 += n2;
213266 }
213267
213268 *paOut = aOut;
213269}
213270
213271/*
213272** This is a helper function used by sessionMergeUpdate().
213273**
213274** When this function is called, both *paOne and *paTwo point to a value
213275** within a change record. Before it returns, both have been advanced so
213276** as to point to the next value in the record.
213277**
213278** If, when this function is called, *paTwo points to a valid value (i.e.
213279** *paTwo[0] is not 0x00 - the "no value" placeholder), a copy of the *paTwo
213280** pointer is returned and *pnVal is set to the number of bytes in the
213281** serialized value. Otherwise, a copy of *paOne is returned and *pnVal
213282** set to the number of bytes in the value at *paOne. If *paOne points
213283** to the "no value" placeholder, *pnVal is set to 1. In other words:
213284**
213285** if( *paTwo is valid ) return *paTwo;
213286** return *paOne;
213287**
213288*/
213289static u8 *sessionMergeValue(
213290 u8 **paOne, /* IN/OUT: Left-hand buffer pointer */
213291 u8 **paTwo, /* IN/OUT: Right-hand buffer pointer */
213292 int *pnVal /* OUT: Bytes in returned value */
213293){
213294 u8 *a1 = *paOne;
213295 u8 *a2 = *paTwo;
213296 u8 *pRet = 0;
213297 int n1;
213298
213299 assert( a1 )((void) (0));
213300 if( a2 ){
213301 int n2 = sessionSerialLen(a2);
213302 if( *a2 ){
213303 *pnVal = n2;
213304 pRet = a2;
213305 }
213306 *paTwo = &a2[n2];
213307 }
213308
213309 n1 = sessionSerialLen(a1);
213310 if( pRet==0 ){
213311 *pnVal = n1;
213312 pRet = a1;
213313 }
213314 *paOne = &a1[n1];
213315
213316 return pRet;
213317}
213318
213319/*
213320** This function is used by changeset_concat() to merge two UPDATE changes
213321** on the same row.
213322*/
213323static int sessionMergeUpdate(
213324 u8 **paOut, /* IN/OUT: Pointer to output buffer */
213325 SessionTable *pTab, /* Table change pertains to */
213326 int bPatchset, /* True if records are patchset records */
213327 u8 *aOldRecord1, /* old.* record for first change */
213328 u8 *aOldRecord2, /* old.* record for second change */
213329 u8 *aNewRecord1, /* new.* record for first change */
213330 u8 *aNewRecord2 /* new.* record for second change */
213331){
213332 u8 *aOld1 = aOldRecord1;
213333 u8 *aOld2 = aOldRecord2;
213334 u8 *aNew1 = aNewRecord1;
213335 u8 *aNew2 = aNewRecord2;
213336
213337 u8 *aOut = *paOut;
213338 int i;
213339
213340 if( bPatchset==0 ){
213341 int bRequired = 0;
213342
213343 assert( aOldRecord1 && aNewRecord1 )((void) (0));
213344
213345 /* Write the old.* vector first. */
213346 for(i=0; i<pTab->nCol; i++){
213347 int nOld;
213348 u8 *aOld;
213349 int nNew;
213350 u8 *aNew;
213351
213352 aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
213353 aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
213354 if( pTab->abPK[i] || nOld!=nNew || memcmp(aOld, aNew, nNew) ){
213355 if( pTab->abPK[i]==0 ) bRequired = 1;
213356 memcpy(aOut, aOld, nOld);
213357 aOut += nOld;
213358 }else{
213359 *(aOut++) = '\0';
213360 }
213361 }
213362
213363 if( !bRequired ) return 0;
213364 }
213365
213366 /* Write the new.* vector */
213367 aOld1 = aOldRecord1;
213368 aOld2 = aOldRecord2;
213369 aNew1 = aNewRecord1;
213370 aNew2 = aNewRecord2;
213371 for(i=0; i<pTab->nCol; i++){
213372 int nOld;
213373 u8 *aOld;
213374 int nNew;
213375 u8 *aNew;
213376
213377 aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
213378 aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
213379 if( bPatchset==0
213380 && (pTab->abPK[i] || (nOld==nNew && 0==memcmp(aOld, aNew, nNew)))
213381 ){
213382 *(aOut++) = '\0';
213383 }else{
213384 memcpy(aOut, aNew, nNew);
213385 aOut += nNew;
213386 }
213387 }
213388
213389 *paOut = aOut;
213390 return 1;
213391}
213392
213393/*
213394** This function is only called from within a pre-update-hook callback.
213395** It determines if the current pre-update-hook change affects the same row
213396** as the change stored in argument pChange. If so, it returns true. Otherwise
213397** if the pre-update-hook does not affect the same row as pChange, it returns
213398** false.
213399*/
213400static int sessionPreupdateEqual(
213401 sqlite3_session *pSession, /* Session object that owns SessionTable */
213402 SessionTable *pTab, /* Table associated with change */
213403 SessionChange *pChange, /* Change to compare to */
213404 int op /* Current pre-update operation */
213405){
213406 int iCol; /* Used to iterate through columns */
213407 u8 *a = pChange->aRecord; /* Cursor used to scan change record */
213408
213409 assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE )((void) (0));
213410 for(iCol=0; iCol<pTab->nCol; iCol++){
213411 if( !pTab->abPK[iCol] ){
213412 a += sessionSerialLen(a);
213413 }else{
213414 sqlite3_value *pVal; /* Value returned by preupdate_new/old */
213415 int rc; /* Error code from preupdate_new/old */
213416 int eType = *a++; /* Type of value from change record */
213417
213418 /* The following calls to preupdate_new() and preupdate_old() can not
213419 ** fail. This is because they cache their return values, and by the
213420 ** time control flows to here they have already been called once from
213421 ** within sessionPreupdateHash(). The first two asserts below verify
213422 ** this (that the method has already been called). */
213423 if( op==SQLITE_INSERT18 ){
213424 /* assert( db->pPreUpdate->pNewUnpacked || db->pPreUpdate->aNew ); */
213425 rc = pSession->hook.xNew(pSession->hook.pCtx, iCol, &pVal);
213426 }else{
213427 /* assert( db->pPreUpdate->pUnpacked ); */
213428 rc = pSession->hook.xOld(pSession->hook.pCtx, iCol, &pVal);
213429 }
213430 assert( rc==SQLITE_OK )((void) (0));
213431 if( sqlite3_value_type(pVal)!=eType ) return 0;
213432
213433 /* A SessionChange object never has a NULL value in a PK column */
213434 assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT((void) (0))
213435 || eType==SQLITE_BLOB || eType==SQLITE_TEXT((void) (0))
213436 )((void) (0));
213437
213438 if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
213439 i64 iVal = sessionGetI64(a);
213440 a += 8;
213441 if( eType==SQLITE_INTEGER1 ){
213442 if( sqlite3_value_int64(pVal)!=iVal ) return 0;
213443 }else{
213444 double rVal;
213445 assert( sizeof(iVal)==8 && sizeof(rVal)==8 )((void) (0));
213446 memcpy(&rVal, &iVal, 8);
213447 if( sqlite3_value_double(pVal)!=rVal ) return 0;
213448 }
213449 }else{
213450 int n;
213451 const u8 *z;
213452 a += sessionVarintGet(a, &n);
213453 if( sqlite3_value_bytes(pVal)!=n ) return 0;
213454 if( eType==SQLITE_TEXT3 ){
213455 z = sqlite3_value_text(pVal);
213456 }else{
213457 z = sqlite3_value_blob(pVal);
213458 }
213459 if( n>0 && memcmp(a, z, n) ) return 0;
213460 a += n;
213461 }
213462 }
213463 }
213464
213465 return 1;
213466}
213467
213468/*
213469** If required, grow the hash table used to store changes on table pTab
213470** (part of the session pSession). If a fatal OOM error occurs, set the
213471** session object to failed and return SQLITE_ERROR. Otherwise, return
213472** SQLITE_OK.
213473**
213474** It is possible that a non-fatal OOM error occurs in this function. In
213475** that case the hash-table does not grow, but SQLITE_OK is returned anyway.
213476** Growing the hash table in this case is a performance optimization only,
213477** it is not required for correct operation.
213478*/
213479static int sessionGrowHash(
213480 sqlite3_session *pSession, /* For memory accounting. May be NULL */
213481 int bPatchset,
213482 SessionTable *pTab
213483){
213484 if( pTab->nChange==0 || pTab->nEntry>=(pTab->nChange/2) ){
213485 int i;
213486 SessionChange **apNew;
213487 sqlite3_int64 nNew = 2*(sqlite3_int64)(pTab->nChange ? pTab->nChange : 128);
213488
213489 apNew = (SessionChange**)sessionMalloc64(
213490 pSession, sizeof(SessionChange*) * nNew
213491 );
213492 if( apNew==0 ){
213493 if( pTab->nChange==0 ){
213494 return SQLITE_ERROR1;
213495 }
213496 return SQLITE_OK0;
213497 }
213498 memset(apNew, 0, sizeof(SessionChange *) * nNew);
213499
213500 for(i=0; i<pTab->nChange; i++){
213501 SessionChange *p;
213502 SessionChange *pNext;
213503 for(p=pTab->apChange[i]; p; p=pNext){
213504 int bPkOnly = (p->op==SQLITE_DELETE9 && bPatchset);
213505 int iHash = sessionChangeHash(pTab, bPkOnly, p->aRecord, nNew);
213506 pNext = p->pNext;
213507 p->pNext = apNew[iHash];
213508 apNew[iHash] = p;
213509 }
213510 }
213511
213512 sessionFree(pSession, pTab->apChange);
213513 pTab->nChange = nNew;
213514 pTab->apChange = apNew;
213515 }
213516
213517 return SQLITE_OK0;
213518}
213519
213520/*
213521** This function queries the database for the names of the columns of table
213522** zThis, in schema zDb.
213523**
213524** Otherwise, if they are not NULL, variable *pnCol is set to the number
213525** of columns in the database table and variable *pzTab is set to point to a
213526** nul-terminated copy of the table name. *pazCol (if not NULL) is set to
213527** point to an array of pointers to column names. And *pabPK (again, if not
213528** NULL) is set to point to an array of booleans - true if the corresponding
213529** column is part of the primary key.
213530**
213531** For example, if the table is declared as:
213532**
213533** CREATE TABLE tbl1(w, x, y, z, PRIMARY KEY(w, z));
213534**
213535** Then the four output variables are populated as follows:
213536**
213537** *pnCol = 4
213538** *pzTab = "tbl1"
213539** *pazCol = {"w", "x", "y", "z"}
213540** *pabPK = {1, 0, 0, 1}
213541**
213542** All returned buffers are part of the same single allocation, which must
213543** be freed using sqlite3_free() by the caller
213544*/
213545static int sessionTableInfo(
213546 sqlite3_session *pSession, /* For memory accounting. May be NULL */
213547 sqlite3 *db, /* Database connection */
213548 const char *zDb, /* Name of attached database (e.g. "main") */
213549 const char *zThis, /* Table name */
213550 int *pnCol, /* OUT: number of columns */
213551 const char **pzTab, /* OUT: Copy of zThis */
213552 const char ***pazCol, /* OUT: Array of column names for table */
213553 u8 **pabPK /* OUT: Array of booleans - true for PK col */
213554){
213555 char *zPragma;
213556 sqlite3_stmt *pStmt;
213557 int rc;
213558 sqlite3_int64 nByte;
213559 int nDbCol = 0;
213560 int nThis;
213561 int i;
213562 u8 *pAlloc = 0;
213563 char **azCol = 0;
213564 u8 *abPK = 0;
213565
213566 assert( pazCol && pabPK )((void) (0));
213567
213568 nThis = sqlite3Strlen30(zThis);
213569 if( nThis==12 && 0==sqlite3_stricmp("sqlite_stat1", zThis) ){
213570 rc = sqlite3_table_column_metadata(db, zDb, zThis, 0, 0, 0, 0, 0, 0);
213571 if( rc==SQLITE_OK0 ){
213572 /* For sqlite_stat1, pretend that (tbl,idx) is the PRIMARY KEY. */
213573 zPragma = sqlite3_mprintf(
213574 "SELECT 0, 'tbl', '', 0, '', 1 UNION ALL "
213575 "SELECT 1, 'idx', '', 0, '', 2 UNION ALL "
213576 "SELECT 2, 'stat', '', 0, '', 0"
213577 );
213578 }else if( rc==SQLITE_ERROR1 ){
213579 zPragma = sqlite3_mprintf("");
213580 }else{
213581 *pazCol = 0;
213582 *pabPK = 0;
213583 *pnCol = 0;
213584 if( pzTab ) *pzTab = 0;
213585 return rc;
213586 }
213587 }else{
213588 zPragma = sqlite3_mprintf("PRAGMA '%q'.table_info('%q')", zDb, zThis);
213589 }
213590 if( !zPragma ){
213591 *pazCol = 0;
213592 *pabPK = 0;
213593 *pnCol = 0;
213594 if( pzTab ) *pzTab = 0;
213595 return SQLITE_NOMEM7;
213596 }
213597
213598 rc = sqlite3_prepare_v2(db, zPragma, -1, &pStmt, 0);
213599 sqlite3_free(zPragma);
213600 if( rc!=SQLITE_OK0 ){
213601 *pazCol = 0;
213602 *pabPK = 0;
213603 *pnCol = 0;
213604 if( pzTab ) *pzTab = 0;
213605 return rc;
213606 }
213607
213608 nByte = nThis + 1;
213609 while( SQLITE_ROW100==sqlite3_step(pStmt) ){
213610 nByte += sqlite3_column_bytes(pStmt, 1);
213611 nDbCol++;
213612 }
213613 rc = sqlite3_reset(pStmt);
213614
213615 if( rc==SQLITE_OK0 ){
213616 nByte += nDbCol * (sizeof(const char *) + sizeof(u8) + 1);
213617 pAlloc = sessionMalloc64(pSession, nByte);
213618 if( pAlloc==0 ){
213619 rc = SQLITE_NOMEM7;
213620 }
213621 }
213622 if( rc==SQLITE_OK0 ){
213623 azCol = (char **)pAlloc;
213624 pAlloc = (u8 *)&azCol[nDbCol];
213625 abPK = (u8 *)pAlloc;
213626 pAlloc = &abPK[nDbCol];
213627 if( pzTab ){
213628 memcpy(pAlloc, zThis, nThis+1);
213629 *pzTab = (char *)pAlloc;
213630 pAlloc += nThis+1;
213631 }
213632
213633 i = 0;
213634 while( SQLITE_ROW100==sqlite3_step(pStmt) ){
213635 int nName = sqlite3_column_bytes(pStmt, 1);
213636 const unsigned char *zName = sqlite3_column_text(pStmt, 1);
213637 if( zName==0 ) break;
213638 memcpy(pAlloc, zName, nName+1);
213639 azCol[i] = (char *)pAlloc;
213640 pAlloc += nName+1;
213641 abPK[i] = sqlite3_column_int(pStmt, 5);
213642 i++;
213643 }
213644 rc = sqlite3_reset(pStmt);
213645
213646 }
213647
213648 /* If successful, populate the output variables. Otherwise, zero them and
213649 ** free any allocation made. An error code will be returned in this case.
213650 */
213651 if( rc==SQLITE_OK0 ){
213652 *pazCol = (const char **)azCol;
213653 *pabPK = abPK;
213654 *pnCol = nDbCol;
213655 }else{
213656 *pazCol = 0;
213657 *pabPK = 0;
213658 *pnCol = 0;
213659 if( pzTab ) *pzTab = 0;
213660 sessionFree(pSession, azCol);
213661 }
213662 sqlite3_finalize(pStmt);
213663 return rc;
213664}
213665
213666/*
213667** This function is only called from within a pre-update handler for a
213668** write to table pTab, part of session pSession. If this is the first
213669** write to this table, initalize the SessionTable.nCol, azCol[] and
213670** abPK[] arrays accordingly.
213671**
213672** If an error occurs, an error code is stored in sqlite3_session.rc and
213673** non-zero returned. Or, if no error occurs but the table has no primary
213674** key, sqlite3_session.rc is left set to SQLITE_OK and non-zero returned to
213675** indicate that updates on this table should be ignored. SessionTable.abPK
213676** is set to NULL in this case.
213677*/
213678static int sessionInitTable(sqlite3_session *pSession, SessionTable *pTab){
213679 if( pTab->nCol==0 ){
213680 u8 *abPK;
213681 assert( pTab->azCol==0 || pTab->abPK==0 )((void) (0));
213682 pSession->rc = sessionTableInfo(pSession, pSession->db, pSession->zDb,
213683 pTab->zName, &pTab->nCol, 0, &pTab->azCol, &abPK
213684 );
213685 if( pSession->rc==SQLITE_OK0 ){
213686 int i;
213687 for(i=0; i<pTab->nCol; i++){
213688 if( abPK[i] ){
213689 pTab->abPK = abPK;
213690 break;
213691 }
213692 }
213693 if( 0==sqlite3_stricmp("sqlite_stat1", pTab->zName) ){
213694 pTab->bStat1 = 1;
213695 }
213696
213697 if( pSession->bEnableSize ){
213698 pSession->nMaxChangesetSize += (
213699 1 + sessionVarintLen(pTab->nCol) + pTab->nCol + strlen(pTab->zName)+1
213700 );
213701 }
213702 }
213703 }
213704 return (pSession->rc || pTab->abPK==0);
213705}
213706
213707/*
213708** Versions of the four methods in object SessionHook for use with the
213709** sqlite_stat1 table. The purpose of this is to substitute a zero-length
213710** blob each time a NULL value is read from the "idx" column of the
213711** sqlite_stat1 table.
213712*/
213713typedef struct SessionStat1Ctx SessionStat1Ctx;
213714struct SessionStat1Ctx {
213715 SessionHook hook;
213716 sqlite3_session *pSession;
213717};
213718static int sessionStat1Old(void *pCtx, int iCol, sqlite3_value **ppVal){
213719 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
213720 sqlite3_value *pVal = 0;
213721 int rc = p->hook.xOld(p->hook.pCtx, iCol, &pVal);
213722 if( rc==SQLITE_OK0 && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL5 ){
213723 pVal = p->pSession->pZeroBlob;
213724 }
213725 *ppVal = pVal;
213726 return rc;
213727}
213728static int sessionStat1New(void *pCtx, int iCol, sqlite3_value **ppVal){
213729 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
213730 sqlite3_value *pVal = 0;
213731 int rc = p->hook.xNew(p->hook.pCtx, iCol, &pVal);
213732 if( rc==SQLITE_OK0 && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL5 ){
213733 pVal = p->pSession->pZeroBlob;
213734 }
213735 *ppVal = pVal;
213736 return rc;
213737}
213738static int sessionStat1Count(void *pCtx){
213739 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
213740 return p->hook.xCount(p->hook.pCtx);
213741}
213742static int sessionStat1Depth(void *pCtx){
213743 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
213744 return p->hook.xDepth(p->hook.pCtx);
213745}
213746
213747static int sessionUpdateMaxSize(
213748 int op,
213749 sqlite3_session *pSession, /* Session object pTab is attached to */
213750 SessionTable *pTab, /* Table that change applies to */
213751 SessionChange *pC /* Update pC->nMaxSize */
213752){
213753 i64 nNew = 2;
213754 if( pC->op==SQLITE_INSERT18 ){
213755 if( op!=SQLITE_DELETE9 ){
213756 int ii;
213757 for(ii=0; ii<pTab->nCol; ii++){
213758 sqlite3_value *p = 0;
213759 pSession->hook.xNew(pSession->hook.pCtx, ii, &p);
213760 sessionSerializeValue(0, p, &nNew);
213761 }
213762 }
213763 }else if( op==SQLITE_DELETE9 ){
213764 nNew += pC->nRecord;
213765 if( sqlite3_preupdate_blobwrite(pSession->db)>=0 ){
213766 nNew += pC->nRecord;
213767 }
213768 }else{
213769 int ii;
213770 u8 *pCsr = pC->aRecord;
213771 for(ii=0; ii<pTab->nCol; ii++){
213772 int bChanged = 1;
213773 int nOld = 0;
213774 int eType;
213775 sqlite3_value *p = 0;
213776 pSession->hook.xNew(pSession->hook.pCtx, ii, &p);
213777 if( p==0 ){
213778 return SQLITE_NOMEM7;
213779 }
213780
213781 eType = *pCsr++;
213782 switch( eType ){
213783 case SQLITE_NULL5:
213784 bChanged = sqlite3_value_type(p)!=SQLITE_NULL5;
213785 break;
213786
213787 case SQLITE_FLOAT2:
213788 case SQLITE_INTEGER1: {
213789 if( eType==sqlite3_value_type(p) ){
213790 sqlite3_int64 iVal = sessionGetI64(pCsr);
213791 if( eType==SQLITE_INTEGER1 ){
213792 bChanged = (iVal!=sqlite3_value_int64(p));
213793 }else{
213794 double dVal;
213795 memcpy(&dVal, &iVal, 8);
213796 bChanged = (dVal!=sqlite3_value_double(p));
213797 }
213798 }
213799 nOld = 8;
213800 pCsr += 8;
213801 break;
213802 }
213803
213804 default: {
213805 int nByte;
213806 nOld = sessionVarintGet(pCsr, &nByte);
213807 pCsr += nOld;
213808 nOld += nByte;
213809 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB )((void) (0));
213810 if( eType==sqlite3_value_type(p)
213811 && nByte==sqlite3_value_bytes(p)
213812 && (nByte==0 || 0==memcmp(pCsr, sqlite3_value_blob(p), nByte))
213813 ){
213814 bChanged = 0;
213815 }
213816 pCsr += nByte;
213817 break;
213818 }
213819 }
213820
213821 if( bChanged && pTab->abPK[ii] ){
213822 nNew = pC->nRecord + 2;
213823 break;
213824 }
213825
213826 if( bChanged ){
213827 nNew += 1 + nOld;
213828 sessionSerializeValue(0, p, &nNew);
213829 }else if( pTab->abPK[ii] ){
213830 nNew += 2 + nOld;
213831 }else{
213832 nNew += 2;
213833 }
213834 }
213835 }
213836
213837 if( nNew>pC->nMaxSize ){
213838 int nIncr = nNew - pC->nMaxSize;
213839 pC->nMaxSize = nNew;
213840 pSession->nMaxChangesetSize += nIncr;
213841 }
213842 return SQLITE_OK0;
213843}
213844
213845/*
213846** This function is only called from with a pre-update-hook reporting a
213847** change on table pTab (attached to session pSession). The type of change
213848** (UPDATE, INSERT, DELETE) is specified by the first argument.
213849**
213850** Unless one is already present or an error occurs, an entry is added
213851** to the changed-rows hash table associated with table pTab.
213852*/
213853static void sessionPreupdateOneChange(
213854 int op, /* One of SQLITE_UPDATE, INSERT, DELETE */
213855 sqlite3_session *pSession, /* Session object pTab is attached to */
213856 SessionTable *pTab /* Table that change applies to */
213857){
213858 int iHash;
213859 int bNull = 0;
213860 int rc = SQLITE_OK0;
213861 SessionStat1Ctx stat1 = {{0,0,0,0,0},0};
213862
213863 if( pSession->rc ) return;
213864
213865 /* Load table details if required */
213866 if( sessionInitTable(pSession, pTab) ) return;
213867
213868 /* Check the number of columns in this xPreUpdate call matches the
213869 ** number of columns in the table. */
213870 if( pTab->nCol!=pSession->hook.xCount(pSession->hook.pCtx) ){
213871 pSession->rc = SQLITE_SCHEMA17;
213872 return;
213873 }
213874
213875 /* Grow the hash table if required */
213876 if( sessionGrowHash(pSession, 0, pTab) ){
213877 pSession->rc = SQLITE_NOMEM7;
213878 return;
213879 }
213880
213881 if( pTab->bStat1 ){
213882 stat1.hook = pSession->hook;
213883 stat1.pSession = pSession;
213884 pSession->hook.pCtx = (void*)&stat1;
213885 pSession->hook.xNew = sessionStat1New;
213886 pSession->hook.xOld = sessionStat1Old;
213887 pSession->hook.xCount = sessionStat1Count;
213888 pSession->hook.xDepth = sessionStat1Depth;
213889 if( pSession->pZeroBlob==0 ){
213890 sqlite3_value *p = sqlite3ValueNew(0);
213891 if( p==0 ){
213892 rc = SQLITE_NOMEM7;
213893 goto error_out;
213894 }
213895 sqlite3ValueSetStr(p, 0, "", 0, SQLITE_STATIC((sqlite3_destructor_type)0));
213896 pSession->pZeroBlob = p;
213897 }
213898 }
213899
213900 /* Calculate the hash-key for this change. If the primary key of the row
213901 ** includes a NULL value, exit early. Such changes are ignored by the
213902 ** session module. */
213903 rc = sessionPreupdateHash(pSession, pTab, op==SQLITE_INSERT18, &iHash, &bNull);
213904 if( rc!=SQLITE_OK0 ) goto error_out;
213905
213906 if( bNull==0 ){
213907 /* Search the hash table for an existing record for this row. */
213908 SessionChange *pC;
213909 for(pC=pTab->apChange[iHash]; pC; pC=pC->pNext){
213910 if( sessionPreupdateEqual(pSession, pTab, pC, op) ) break;
213911 }
213912
213913 if( pC==0 ){
213914 /* Create a new change object containing all the old values (if
213915 ** this is an SQLITE_UPDATE or SQLITE_DELETE), or just the PK
213916 ** values (if this is an INSERT). */
213917 sqlite3_int64 nByte; /* Number of bytes to allocate */
213918 int i; /* Used to iterate through columns */
213919
213920 assert( rc==SQLITE_OK )((void) (0));
213921 pTab->nEntry++;
213922
213923 /* Figure out how large an allocation is required */
213924 nByte = sizeof(SessionChange);
213925 for(i=0; i<pTab->nCol; i++){
213926 sqlite3_value *p = 0;
213927 if( op!=SQLITE_INSERT18 ){
213928 TESTONLY(int trc = ) pSession->hook.xOld(pSession->hook.pCtx, i, &p);
213929 assert( trc==SQLITE_OK )((void) (0));
213930 }else if( pTab->abPK[i] ){
213931 TESTONLY(int trc = ) pSession->hook.xNew(pSession->hook.pCtx, i, &p);
213932 assert( trc==SQLITE_OK )((void) (0));
213933 }
213934
213935 /* This may fail if SQLite value p contains a utf-16 string that must
213936 ** be converted to utf-8 and an OOM error occurs while doing so. */
213937 rc = sessionSerializeValue(0, p, &nByte);
213938 if( rc!=SQLITE_OK0 ) goto error_out;
213939 }
213940
213941 /* Allocate the change object */
213942 pC = (SessionChange *)sessionMalloc64(pSession, nByte);
213943 if( !pC ){
213944 rc = SQLITE_NOMEM7;
213945 goto error_out;
213946 }else{
213947 memset(pC, 0, sizeof(SessionChange));
213948 pC->aRecord = (u8 *)&pC[1];
213949 }
213950
213951 /* Populate the change object. None of the preupdate_old(),
213952 ** preupdate_new() or SerializeValue() calls below may fail as all
213953 ** required values and encodings have already been cached in memory.
213954 ** It is not possible for an OOM to occur in this block. */
213955 nByte = 0;
213956 for(i=0; i<pTab->nCol; i++){
213957 sqlite3_value *p = 0;
213958 if( op!=SQLITE_INSERT18 ){
213959 pSession->hook.xOld(pSession->hook.pCtx, i, &p);
213960 }else if( pTab->abPK[i] ){
213961 pSession->hook.xNew(pSession->hook.pCtx, i, &p);
213962 }
213963 sessionSerializeValue(&pC->aRecord[nByte], p, &nByte);
213964 }
213965
213966 /* Add the change to the hash-table */
213967 if( pSession->bIndirect || pSession->hook.xDepth(pSession->hook.pCtx) ){
213968 pC->bIndirect = 1;
213969 }
213970 pC->nRecord = nByte;
213971 pC->op = op;
213972 pC->pNext = pTab->apChange[iHash];
213973 pTab->apChange[iHash] = pC;
213974
213975 }else if( pC->bIndirect ){
213976 /* If the existing change is considered "indirect", but this current
213977 ** change is "direct", mark the change object as direct. */
213978 if( pSession->hook.xDepth(pSession->hook.pCtx)==0
213979 && pSession->bIndirect==0
213980 ){
213981 pC->bIndirect = 0;
213982 }
213983 }
213984
213985 assert( rc==SQLITE_OK )((void) (0));
213986 if( pSession->bEnableSize ){
213987 rc = sessionUpdateMaxSize(op, pSession, pTab, pC);
213988 }
213989 }
213990
213991
213992 /* If an error has occurred, mark the session object as failed. */
213993 error_out:
213994 if( pTab->bStat1 ){
213995 pSession->hook = stat1.hook;
213996 }
213997 if( rc!=SQLITE_OK0 ){
213998 pSession->rc = rc;
213999 }
214000}
214001
214002static int sessionFindTable(
214003 sqlite3_session *pSession,
214004 const char *zName,
214005 SessionTable **ppTab
214006){
214007 int rc = SQLITE_OK0;
214008 int nName = sqlite3Strlen30(zName);
214009 SessionTable *pRet;
214010
214011 /* Search for an existing table */
214012 for(pRet=pSession->pTable; pRet; pRet=pRet->pNext){
214013 if( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) ) break;
214014 }
214015
214016 if( pRet==0 && pSession->bAutoAttach ){
214017 /* If there is a table-filter configured, invoke it. If it returns 0,
214018 ** do not automatically add the new table. */
214019 if( pSession->xTableFilter==0
214020 || pSession->xTableFilter(pSession->pFilterCtx, zName)
214021 ){
214022 rc = sqlite3session_attach(pSession, zName);
214023 if( rc==SQLITE_OK0 ){
214024 pRet = pSession->pTable;
214025 while( ALWAYS(pRet)(pRet) && pRet->pNext ){
214026 pRet = pRet->pNext;
214027 }
214028 assert( pRet!=0 )((void) (0));
214029 assert( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) )((void) (0));
214030 }
214031 }
214032 }
214033
214034 assert( rc==SQLITE_OK || pRet==0 )((void) (0));
214035 *ppTab = pRet;
214036 return rc;
214037}
214038
214039/*
214040** The 'pre-update' hook registered by this module with SQLite databases.
214041*/
214042static void xPreUpdate(
214043 void *pCtx, /* Copy of third arg to preupdate_hook() */
214044 sqlite3 *db, /* Database handle */
214045 int op, /* SQLITE_UPDATE, DELETE or INSERT */
214046 char const *zDb, /* Database name */
214047 char const *zName, /* Table name */
214048 sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */
214049 sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */
214050){
214051 sqlite3_session *pSession;
214052 int nDb = sqlite3Strlen30(zDb);
214053
214054 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
214055
214056 for(pSession=(sqlite3_session *)pCtx; pSession; pSession=pSession->pNext){
214057 SessionTable *pTab;
214058
214059 /* If this session is attached to a different database ("main", "temp"
214060 ** etc.), or if it is not currently enabled, there is nothing to do. Skip
214061 ** to the next session object attached to this database. */
214062 if( pSession->bEnable==0 ) continue;
214063 if( pSession->rc ) continue;
214064 if( sqlite3_strnicmp(zDb, pSession->zDb, nDb+1) ) continue;
214065
214066 pSession->rc = sessionFindTable(pSession, zName, &pTab);
214067 if( pTab ){
214068 assert( pSession->rc==SQLITE_OK )((void) (0));
214069 sessionPreupdateOneChange(op, pSession, pTab);
214070 if( op==SQLITE_UPDATE23 ){
214071 sessionPreupdateOneChange(SQLITE_INSERT18, pSession, pTab);
214072 }
214073 }
214074 }
214075}
214076
214077/*
214078** The pre-update hook implementations.
214079*/
214080static int sessionPreupdateOld(void *pCtx, int iVal, sqlite3_value **ppVal){
214081 return sqlite3_preupdate_old((sqlite3*)pCtx, iVal, ppVal);
214082}
214083static int sessionPreupdateNew(void *pCtx, int iVal, sqlite3_value **ppVal){
214084 return sqlite3_preupdate_new((sqlite3*)pCtx, iVal, ppVal);
214085}
214086static int sessionPreupdateCount(void *pCtx){
214087 return sqlite3_preupdate_count((sqlite3*)pCtx);
214088}
214089static int sessionPreupdateDepth(void *pCtx){
214090 return sqlite3_preupdate_depth((sqlite3*)pCtx);
214091}
214092
214093/*
214094** Install the pre-update hooks on the session object passed as the only
214095** argument.
214096*/
214097static void sessionPreupdateHooks(
214098 sqlite3_session *pSession
214099){
214100 pSession->hook.pCtx = (void*)pSession->db;
214101 pSession->hook.xOld = sessionPreupdateOld;
214102 pSession->hook.xNew = sessionPreupdateNew;
214103 pSession->hook.xCount = sessionPreupdateCount;
214104 pSession->hook.xDepth = sessionPreupdateDepth;
214105}
214106
214107typedef struct SessionDiffCtx SessionDiffCtx;
214108struct SessionDiffCtx {
214109 sqlite3_stmt *pStmt;
214110 int nOldOff;
214111};
214112
214113/*
214114** The diff hook implementations.
214115*/
214116static int sessionDiffOld(void *pCtx, int iVal, sqlite3_value **ppVal){
214117 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
214118 *ppVal = sqlite3_column_value(p->pStmt, iVal+p->nOldOff);
214119 return SQLITE_OK0;
214120}
214121static int sessionDiffNew(void *pCtx, int iVal, sqlite3_value **ppVal){
214122 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
214123 *ppVal = sqlite3_column_value(p->pStmt, iVal);
214124 return SQLITE_OK0;
214125}
214126static int sessionDiffCount(void *pCtx){
214127 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
214128 return p->nOldOff ? p->nOldOff : sqlite3_column_count(p->pStmt);
214129}
214130static int sessionDiffDepth(void *pCtx){
214131 return 0;
214132}
214133
214134/*
214135** Install the diff hooks on the session object passed as the only
214136** argument.
214137*/
214138static void sessionDiffHooks(
214139 sqlite3_session *pSession,
214140 SessionDiffCtx *pDiffCtx
214141){
214142 pSession->hook.pCtx = (void*)pDiffCtx;
214143 pSession->hook.xOld = sessionDiffOld;
214144 pSession->hook.xNew = sessionDiffNew;
214145 pSession->hook.xCount = sessionDiffCount;
214146 pSession->hook.xDepth = sessionDiffDepth;
214147}
214148
214149static char *sessionExprComparePK(
214150 int nCol,
214151 const char *zDb1, const char *zDb2,
214152 const char *zTab,
214153 const char **azCol, u8 *abPK
214154){
214155 int i;
214156 const char *zSep = "";
214157 char *zRet = 0;
214158
214159 for(i=0; i<nCol; i++){
214160 if( abPK[i] ){
214161 zRet = sqlite3_mprintf("%z%s\"%w\".\"%w\".\"%w\"=\"%w\".\"%w\".\"%w\"",
214162 zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
214163 );
214164 zSep = " AND ";
214165 if( zRet==0 ) break;
214166 }
214167 }
214168
214169 return zRet;
214170}
214171
214172static char *sessionExprCompareOther(
214173 int nCol,
214174 const char *zDb1, const char *zDb2,
214175 const char *zTab,
214176 const char **azCol, u8 *abPK
214177){
214178 int i;
214179 const char *zSep = "";
214180 char *zRet = 0;
214181 int bHave = 0;
214182
214183 for(i=0; i<nCol; i++){
214184 if( abPK[i]==0 ){
214185 bHave = 1;
214186 zRet = sqlite3_mprintf(
214187 "%z%s\"%w\".\"%w\".\"%w\" IS NOT \"%w\".\"%w\".\"%w\"",
214188 zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
214189 );
214190 zSep = " OR ";
214191 if( zRet==0 ) break;
214192 }
214193 }
214194
214195 if( bHave==0 ){
214196 assert( zRet==0 )((void) (0));
214197 zRet = sqlite3_mprintf("0");
214198 }
214199
214200 return zRet;
214201}
214202
214203static char *sessionSelectFindNew(
214204 int nCol,
214205 const char *zDb1, /* Pick rows in this db only */
214206 const char *zDb2, /* But not in this one */
214207 const char *zTbl, /* Table name */
214208 const char *zExpr
214209){
214210 char *zRet = sqlite3_mprintf(
214211 "SELECT * FROM \"%w\".\"%w\" WHERE NOT EXISTS ("
214212 " SELECT 1 FROM \"%w\".\"%w\" WHERE %s"
214213 ")",
214214 zDb1, zTbl, zDb2, zTbl, zExpr
214215 );
214216 return zRet;
214217}
214218
214219static int sessionDiffFindNew(
214220 int op,
214221 sqlite3_session *pSession,
214222 SessionTable *pTab,
214223 const char *zDb1,
214224 const char *zDb2,
214225 char *zExpr
214226){
214227 int rc = SQLITE_OK0;
214228 char *zStmt = sessionSelectFindNew(pTab->nCol, zDb1, zDb2, pTab->zName,zExpr);
214229
214230 if( zStmt==0 ){
214231 rc = SQLITE_NOMEM7;
214232 }else{
214233 sqlite3_stmt *pStmt;
214234 rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
214235 if( rc==SQLITE_OK0 ){
214236 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
214237 pDiffCtx->pStmt = pStmt;
214238 pDiffCtx->nOldOff = 0;
214239 while( SQLITE_ROW100==sqlite3_step(pStmt) ){
214240 sessionPreupdateOneChange(op, pSession, pTab);
214241 }
214242 rc = sqlite3_finalize(pStmt);
214243 }
214244 sqlite3_free(zStmt);
214245 }
214246
214247 return rc;
214248}
214249
214250static int sessionDiffFindModified(
214251 sqlite3_session *pSession,
214252 SessionTable *pTab,
214253 const char *zFrom,
214254 const char *zExpr
214255){
214256 int rc = SQLITE_OK0;
214257
214258 char *zExpr2 = sessionExprCompareOther(pTab->nCol,
214259 pSession->zDb, zFrom, pTab->zName, pTab->azCol, pTab->abPK
214260 );
214261 if( zExpr2==0 ){
214262 rc = SQLITE_NOMEM7;
214263 }else{
214264 char *zStmt = sqlite3_mprintf(
214265 "SELECT * FROM \"%w\".\"%w\", \"%w\".\"%w\" WHERE %s AND (%z)",
214266 pSession->zDb, pTab->zName, zFrom, pTab->zName, zExpr, zExpr2
214267 );
214268 if( zStmt==0 ){
214269 rc = SQLITE_NOMEM7;
214270 }else{
214271 sqlite3_stmt *pStmt;
214272 rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
214273
214274 if( rc==SQLITE_OK0 ){
214275 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
214276 pDiffCtx->pStmt = pStmt;
214277 pDiffCtx->nOldOff = pTab->nCol;
214278 while( SQLITE_ROW100==sqlite3_step(pStmt) ){
214279 sessionPreupdateOneChange(SQLITE_UPDATE23, pSession, pTab);
214280 }
214281 rc = sqlite3_finalize(pStmt);
214282 }
214283 sqlite3_free(zStmt);
214284 }
214285 }
214286
214287 return rc;
214288}
214289
214290SQLITE_API int sqlite3session_diff(
214291 sqlite3_session *pSession,
214292 const char *zFrom,
214293 const char *zTbl,
214294 char **pzErrMsg
214295){
214296 const char *zDb = pSession->zDb;
214297 int rc = pSession->rc;
214298 SessionDiffCtx d;
214299
214300 memset(&d, 0, sizeof(d));
214301 sessionDiffHooks(pSession, &d);
214302
214303 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
214304 if( pzErrMsg ) *pzErrMsg = 0;
214305 if( rc==SQLITE_OK0 ){
214306 char *zExpr = 0;
214307 sqlite3 *db = pSession->db;
214308 SessionTable *pTo; /* Table zTbl */
214309
214310 /* Locate and if necessary initialize the target table object */
214311 rc = sessionFindTable(pSession, zTbl, &pTo);
214312 if( pTo==0 ) goto diff_out;
214313 if( sessionInitTable(pSession, pTo) ){
214314 rc = pSession->rc;
214315 goto diff_out;
214316 }
214317
214318 /* Check the table schemas match */
214319 if( rc==SQLITE_OK0 ){
214320 int bHasPk = 0;
214321 int bMismatch = 0;
214322 int nCol; /* Columns in zFrom.zTbl */
214323 u8 *abPK;
214324 const char **azCol = 0;
214325 rc = sessionTableInfo(0, db, zFrom, zTbl, &nCol, 0, &azCol, &abPK);
214326 if( rc==SQLITE_OK0 ){
214327 if( pTo->nCol!=nCol ){
214328 bMismatch = 1;
214329 }else{
214330 int i;
214331 for(i=0; i<nCol; i++){
214332 if( pTo->abPK[i]!=abPK[i] ) bMismatch = 1;
214333 if( sqlite3_stricmp(azCol[i], pTo->azCol[i]) ) bMismatch = 1;
214334 if( abPK[i] ) bHasPk = 1;
214335 }
214336 }
214337 }
214338 sqlite3_free((char*)azCol);
214339 if( bMismatch ){
214340 if( pzErrMsg ){
214341 *pzErrMsg = sqlite3_mprintf("table schemas do not match");
214342 }
214343 rc = SQLITE_SCHEMA17;
214344 }
214345 if( bHasPk==0 ){
214346 /* Ignore tables with no primary keys */
214347 goto diff_out;
214348 }
214349 }
214350
214351 if( rc==SQLITE_OK0 ){
214352 zExpr = sessionExprComparePK(pTo->nCol,
214353 zDb, zFrom, pTo->zName, pTo->azCol, pTo->abPK
214354 );
214355 }
214356
214357 /* Find new rows */
214358 if( rc==SQLITE_OK0 ){
214359 rc = sessionDiffFindNew(SQLITE_INSERT18, pSession, pTo, zDb, zFrom, zExpr);
214360 }
214361
214362 /* Find old rows */
214363 if( rc==SQLITE_OK0 ){
214364 rc = sessionDiffFindNew(SQLITE_DELETE9, pSession, pTo, zFrom, zDb, zExpr);
214365 }
214366
214367 /* Find modified rows */
214368 if( rc==SQLITE_OK0 ){
214369 rc = sessionDiffFindModified(pSession, pTo, zFrom, zExpr);
214370 }
214371
214372 sqlite3_free(zExpr);
214373 }
214374
214375 diff_out:
214376 sessionPreupdateHooks(pSession);
214377 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
214378 return rc;
214379}
214380
214381/*
214382** Create a session object. This session object will record changes to
214383** database zDb attached to connection db.
214384*/
214385SQLITE_API int sqlite3session_create(
214386 sqlite3 *db, /* Database handle */
214387 const char *zDb, /* Name of db (e.g. "main") */
214388 sqlite3_session **ppSession /* OUT: New session object */
214389){
214390 sqlite3_session *pNew; /* Newly allocated session object */
214391 sqlite3_session *pOld; /* Session object already attached to db */
214392 int nDb = sqlite3Strlen30(zDb); /* Length of zDb in bytes */
214393
214394 /* Zero the output value in case an error occurs. */
214395 *ppSession = 0;
214396
214397 /* Allocate and populate the new session object. */
214398 pNew = (sqlite3_session *)sqlite3_malloc64(sizeof(sqlite3_session) + nDb + 1);
214399 if( !pNew ) return SQLITE_NOMEM7;
214400 memset(pNew, 0, sizeof(sqlite3_session));
214401 pNew->db = db;
214402 pNew->zDb = (char *)&pNew[1];
214403 pNew->bEnable = 1;
214404 memcpy(pNew->zDb, zDb, nDb+1);
214405 sessionPreupdateHooks(pNew);
214406
214407 /* Add the new session object to the linked list of session objects
214408 ** attached to database handle $db. Do this under the cover of the db
214409 ** handle mutex. */
214410 sqlite3_mutex_enter(sqlite3_db_mutex(db));
214411 pOld = (sqlite3_session*)sqlite3_preupdate_hook(db, xPreUpdate, (void*)pNew);
214412 pNew->pNext = pOld;
214413 sqlite3_mutex_leave(sqlite3_db_mutex(db));
214414
214415 *ppSession = pNew;
214416 return SQLITE_OK0;
214417}
214418
214419/*
214420** Free the list of table objects passed as the first argument. The contents
214421** of the changed-rows hash tables are also deleted.
214422*/
214423static void sessionDeleteTable(sqlite3_session *pSession, SessionTable *pList){
214424 SessionTable *pNext;
214425 SessionTable *pTab;
214426
214427 for(pTab=pList; pTab; pTab=pNext){
214428 int i;
214429 pNext = pTab->pNext;
214430 for(i=0; i<pTab->nChange; i++){
214431 SessionChange *p;
214432 SessionChange *pNextChange;
214433 for(p=pTab->apChange[i]; p; p=pNextChange){
214434 pNextChange = p->pNext;
214435 sessionFree(pSession, p);
214436 }
214437 }
214438 sessionFree(pSession, (char*)pTab->azCol); /* cast works around VC++ bug */
214439 sessionFree(pSession, pTab->apChange);
214440 sessionFree(pSession, pTab);
214441 }
214442}
214443
214444/*
214445** Delete a session object previously allocated using sqlite3session_create().
214446*/
214447SQLITE_API void sqlite3session_delete(sqlite3_session *pSession){
214448 sqlite3 *db = pSession->db;
214449 sqlite3_session *pHead;
214450 sqlite3_session **pp;
214451
214452 /* Unlink the session from the linked list of sessions attached to the
214453 ** database handle. Hold the db mutex while doing so. */
214454 sqlite3_mutex_enter(sqlite3_db_mutex(db));
214455 pHead = (sqlite3_session*)sqlite3_preupdate_hook(db, 0, 0);
214456 for(pp=&pHead; ALWAYS((*pp)!=0)((*pp)!=0); pp=&((*pp)->pNext)){
214457 if( (*pp)==pSession ){
214458 *pp = (*pp)->pNext;
214459 if( pHead ) sqlite3_preupdate_hook(db, xPreUpdate, (void*)pHead);
214460 break;
214461 }
214462 }
214463 sqlite3_mutex_leave(sqlite3_db_mutex(db));
214464 sqlite3ValueFree(pSession->pZeroBlob);
214465
214466 /* Delete all attached table objects. And the contents of their
214467 ** associated hash-tables. */
214468 sessionDeleteTable(pSession, pSession->pTable);
214469
214470 /* Assert that all allocations have been freed and then free the
214471 ** session object itself. */
214472 assert( pSession->nMalloc==0 )((void) (0));
214473 sqlite3_free(pSession);
214474}
214475
214476/*
214477** Set a table filter on a Session Object.
214478*/
214479SQLITE_API void sqlite3session_table_filter(
214480 sqlite3_session *pSession,
214481 int(*xFilter)(void*, const char*),
214482 void *pCtx /* First argument passed to xFilter */
214483){
214484 pSession->bAutoAttach = 1;
214485 pSession->pFilterCtx = pCtx;
214486 pSession->xTableFilter = xFilter;
214487}
214488
214489/*
214490** Attach a table to a session. All subsequent changes made to the table
214491** while the session object is enabled will be recorded.
214492**
214493** Only tables that have a PRIMARY KEY defined may be attached. It does
214494** not matter if the PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias)
214495** or not.
214496*/
214497SQLITE_API int sqlite3session_attach(
214498 sqlite3_session *pSession, /* Session object */
214499 const char *zName /* Table name */
214500){
214501 int rc = SQLITE_OK0;
214502 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
214503
214504 if( !zName ){
214505 pSession->bAutoAttach = 1;
214506 }else{
214507 SessionTable *pTab; /* New table object (if required) */
214508 int nName; /* Number of bytes in string zName */
214509
214510 /* First search for an existing entry. If one is found, this call is
214511 ** a no-op. Return early. */
214512 nName = sqlite3Strlen30(zName);
214513 for(pTab=pSession->pTable; pTab; pTab=pTab->pNext){
214514 if( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ) break;
214515 }
214516
214517 if( !pTab ){
214518 /* Allocate new SessionTable object. */
214519 int nByte = sizeof(SessionTable) + nName + 1;
214520 pTab = (SessionTable*)sessionMalloc64(pSession, nByte);
214521 if( !pTab ){
214522 rc = SQLITE_NOMEM7;
214523 }else{
214524 /* Populate the new SessionTable object and link it into the list.
214525 ** The new object must be linked onto the end of the list, not
214526 ** simply added to the start of it in order to ensure that tables
214527 ** appear in the correct order when a changeset or patchset is
214528 ** eventually generated. */
214529 SessionTable **ppTab;
214530 memset(pTab, 0, sizeof(SessionTable));
214531 pTab->zName = (char *)&pTab[1];
214532 memcpy(pTab->zName, zName, nName+1);
214533 for(ppTab=&pSession->pTable; *ppTab; ppTab=&(*ppTab)->pNext);
214534 *ppTab = pTab;
214535 }
214536 }
214537 }
214538
214539 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
214540 return rc;
214541}
214542
214543/*
214544** Ensure that there is room in the buffer to append nByte bytes of data.
214545** If not, use sqlite3_realloc() to grow the buffer so that there is.
214546**
214547** If successful, return zero. Otherwise, if an OOM condition is encountered,
214548** set *pRc to SQLITE_NOMEM and return non-zero.
214549*/
214550static int sessionBufferGrow(SessionBuffer *p, i64 nByte, int *pRc){
214551#define SESSION_MAX_BUFFER_SZ(0x7FFFFF00 - 1) (0x7FFFFF00 - 1)
214552 i64 nReq = p->nBuf + nByte;
214553 if( *pRc==SQLITE_OK0 && nReq>p->nAlloc ){
214554 u8 *aNew;
214555 i64 nNew = p->nAlloc ? p->nAlloc : 128;
214556
214557 do {
214558 nNew = nNew*2;
214559 }while( nNew<nReq );
214560
214561 /* The value of SESSION_MAX_BUFFER_SZ is copied from the implementation
214562 ** of sqlite3_realloc64(). Allocations greater than this size in bytes
214563 ** always fail. It is used here to ensure that this routine can always
214564 ** allocate up to this limit - instead of up to the largest power of
214565 ** two smaller than the limit. */
214566 if( nNew>SESSION_MAX_BUFFER_SZ(0x7FFFFF00 - 1) ){
214567 nNew = SESSION_MAX_BUFFER_SZ(0x7FFFFF00 - 1);
214568 if( nNew<nReq ){
214569 *pRc = SQLITE_NOMEM7;
214570 return 1;
214571 }
214572 }
214573
214574 aNew = (u8 *)sqlite3_realloc64(p->aBuf, nNew);
214575 if( 0==aNew ){
214576 *pRc = SQLITE_NOMEM7;
214577 }else{
214578 p->aBuf = aNew;
214579 p->nAlloc = nNew;
214580 }
214581 }
214582 return (*pRc!=SQLITE_OK0);
214583}
214584
214585/*
214586** Append the value passed as the second argument to the buffer passed
214587** as the first.
214588**
214589** This function is a no-op if *pRc is non-zero when it is called.
214590** Otherwise, if an error occurs, *pRc is set to an SQLite error code
214591** before returning.
214592*/
214593static void sessionAppendValue(SessionBuffer *p, sqlite3_value *pVal, int *pRc){
214594 int rc = *pRc;
214595 if( rc==SQLITE_OK0 ){
214596 sqlite3_int64 nByte = 0;
214597 rc = sessionSerializeValue(0, pVal, &nByte);
214598 sessionBufferGrow(p, nByte, &rc);
214599 if( rc==SQLITE_OK0 ){
214600 rc = sessionSerializeValue(&p->aBuf[p->nBuf], pVal, 0);
214601 p->nBuf += nByte;
214602 }else{
214603 *pRc = rc;
214604 }
214605 }
214606}
214607
214608/*
214609** This function is a no-op if *pRc is other than SQLITE_OK when it is
214610** called. Otherwise, append a single byte to the buffer.
214611**
214612** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
214613** returning.
214614*/
214615static void sessionAppendByte(SessionBuffer *p, u8 v, int *pRc){
214616 if( 0==sessionBufferGrow(p, 1, pRc) ){
214617 p->aBuf[p->nBuf++] = v;
214618 }
214619}
214620
214621/*
214622** This function is a no-op if *pRc is other than SQLITE_OK when it is
214623** called. Otherwise, append a single varint to the buffer.
214624**
214625** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
214626** returning.
214627*/
214628static void sessionAppendVarint(SessionBuffer *p, int v, int *pRc){
214629 if( 0==sessionBufferGrow(p, 9, pRc) ){
214630 p->nBuf += sessionVarintPut(&p->aBuf[p->nBuf], v);
214631 }
214632}
214633
214634/*
214635** This function is a no-op if *pRc is other than SQLITE_OK when it is
214636** called. Otherwise, append a blob of data to the buffer.
214637**
214638** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
214639** returning.
214640*/
214641static void sessionAppendBlob(
214642 SessionBuffer *p,
214643 const u8 *aBlob,
214644 int nBlob,
214645 int *pRc
214646){
214647 if( nBlob>0 && 0==sessionBufferGrow(p, nBlob, pRc) ){
214648 memcpy(&p->aBuf[p->nBuf], aBlob, nBlob);
214649 p->nBuf += nBlob;
214650 }
214651}
214652
214653/*
214654** This function is a no-op if *pRc is other than SQLITE_OK when it is
214655** called. Otherwise, append a string to the buffer. All bytes in the string
214656** up to (but not including) the nul-terminator are written to the buffer.
214657**
214658** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
214659** returning.
214660*/
214661static void sessionAppendStr(
214662 SessionBuffer *p,
214663 const char *zStr,
214664 int *pRc
214665){
214666 int nStr = sqlite3Strlen30(zStr);
214667 if( 0==sessionBufferGrow(p, nStr, pRc) ){
214668 memcpy(&p->aBuf[p->nBuf], zStr, nStr);
214669 p->nBuf += nStr;
214670 }
214671}
214672
214673/*
214674** This function is a no-op if *pRc is other than SQLITE_OK when it is
214675** called. Otherwise, append the string representation of integer iVal
214676** to the buffer. No nul-terminator is written.
214677**
214678** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
214679** returning.
214680*/
214681static void sessionAppendInteger(
214682 SessionBuffer *p, /* Buffer to append to */
214683 int iVal, /* Value to write the string rep. of */
214684 int *pRc /* IN/OUT: Error code */
214685){
214686 char aBuf[24];
214687 sqlite3_snprintf(sizeof(aBuf)-1, aBuf, "%d", iVal);
214688 sessionAppendStr(p, aBuf, pRc);
214689}
214690
214691/*
214692** This function is a no-op if *pRc is other than SQLITE_OK when it is
214693** called. Otherwise, append the string zStr enclosed in quotes (") and
214694** with any embedded quote characters escaped to the buffer. No
214695** nul-terminator byte is written.
214696**
214697** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
214698** returning.
214699*/
214700static void sessionAppendIdent(
214701 SessionBuffer *p, /* Buffer to a append to */
214702 const char *zStr, /* String to quote, escape and append */
214703 int *pRc /* IN/OUT: Error code */
214704){
214705 int nStr = sqlite3Strlen30(zStr)*2 + 2 + 1;
214706 if( 0==sessionBufferGrow(p, nStr, pRc) ){
214707 char *zOut = (char *)&p->aBuf[p->nBuf];
214708 const char *zIn = zStr;
214709 *zOut++ = '"';
214710 while( *zIn ){
214711 if( *zIn=='"' ) *zOut++ = '"';
214712 *zOut++ = *(zIn++);
214713 }
214714 *zOut++ = '"';
214715 p->nBuf = (int)((u8 *)zOut - p->aBuf);
214716 }
214717}
214718
214719/*
214720** This function is a no-op if *pRc is other than SQLITE_OK when it is
214721** called. Otherwse, it appends the serialized version of the value stored
214722** in column iCol of the row that SQL statement pStmt currently points
214723** to to the buffer.
214724*/
214725static void sessionAppendCol(
214726 SessionBuffer *p, /* Buffer to append to */
214727 sqlite3_stmt *pStmt, /* Handle pointing to row containing value */
214728 int iCol, /* Column to read value from */
214729 int *pRc /* IN/OUT: Error code */
214730){
214731 if( *pRc==SQLITE_OK0 ){
214732 int eType = sqlite3_column_type(pStmt, iCol);
214733 sessionAppendByte(p, (u8)eType, pRc);
214734 if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
214735 sqlite3_int64 i;
214736 u8 aBuf[8];
214737 if( eType==SQLITE_INTEGER1 ){
214738 i = sqlite3_column_int64(pStmt, iCol);
214739 }else{
214740 double r = sqlite3_column_double(pStmt, iCol);
214741 memcpy(&i, &r, 8);
214742 }
214743 sessionPutI64(aBuf, i);
214744 sessionAppendBlob(p, aBuf, 8, pRc);
214745 }
214746 if( eType==SQLITE_BLOB4 || eType==SQLITE_TEXT3 ){
214747 u8 *z;
214748 int nByte;
214749 if( eType==SQLITE_BLOB4 ){
214750 z = (u8 *)sqlite3_column_blob(pStmt, iCol);
214751 }else{
214752 z = (u8 *)sqlite3_column_text(pStmt, iCol);
214753 }
214754 nByte = sqlite3_column_bytes(pStmt, iCol);
214755 if( z || (eType==SQLITE_BLOB4 && nByte==0) ){
214756 sessionAppendVarint(p, nByte, pRc);
214757 sessionAppendBlob(p, z, nByte, pRc);
214758 }else{
214759 *pRc = SQLITE_NOMEM7;
214760 }
214761 }
214762 }
214763}
214764
214765/*
214766**
214767** This function appends an update change to the buffer (see the comments
214768** under "CHANGESET FORMAT" at the top of the file). An update change
214769** consists of:
214770**
214771** 1 byte: SQLITE_UPDATE (0x17)
214772** n bytes: old.* record (see RECORD FORMAT)
214773** m bytes: new.* record (see RECORD FORMAT)
214774**
214775** The SessionChange object passed as the third argument contains the
214776** values that were stored in the row when the session began (the old.*
214777** values). The statement handle passed as the second argument points
214778** at the current version of the row (the new.* values).
214779**
214780** If all of the old.* values are equal to their corresponding new.* value
214781** (i.e. nothing has changed), then no data at all is appended to the buffer.
214782**
214783** Otherwise, the old.* record contains all primary key values and the
214784** original values of any fields that have been modified. The new.* record
214785** contains the new values of only those fields that have been modified.
214786*/
214787static int sessionAppendUpdate(
214788 SessionBuffer *pBuf, /* Buffer to append to */
214789 int bPatchset, /* True for "patchset", 0 for "changeset" */
214790 sqlite3_stmt *pStmt, /* Statement handle pointing at new row */
214791 SessionChange *p, /* Object containing old values */
214792 u8 *abPK /* Boolean array - true for PK columns */
214793){
214794 int rc = SQLITE_OK0;
214795 SessionBuffer buf2 = {0,0,0}; /* Buffer to accumulate new.* record in */
214796 int bNoop = 1; /* Set to zero if any values are modified */
214797 int nRewind = pBuf->nBuf; /* Set to zero if any values are modified */
214798 int i; /* Used to iterate through columns */
214799 u8 *pCsr = p->aRecord; /* Used to iterate through old.* values */
214800
214801 assert( abPK!=0 )((void) (0));
214802 sessionAppendByte(pBuf, SQLITE_UPDATE23, &rc);
214803 sessionAppendByte(pBuf, p->bIndirect, &rc);
214804 for(i=0; i<sqlite3_column_count(pStmt); i++){
214805 int bChanged = 0;
214806 int nAdvance;
214807 int eType = *pCsr;
214808 switch( eType ){
214809 case SQLITE_NULL5:
214810 nAdvance = 1;
214811 if( sqlite3_column_type(pStmt, i)!=SQLITE_NULL5 ){
214812 bChanged = 1;
214813 }
214814 break;
214815
214816 case SQLITE_FLOAT2:
214817 case SQLITE_INTEGER1: {
214818 nAdvance = 9;
214819 if( eType==sqlite3_column_type(pStmt, i) ){
214820 sqlite3_int64 iVal = sessionGetI64(&pCsr[1]);
214821 if( eType==SQLITE_INTEGER1 ){
214822 if( iVal==sqlite3_column_int64(pStmt, i) ) break;
214823 }else{
214824 double dVal;
214825 memcpy(&dVal, &iVal, 8);
214826 if( dVal==sqlite3_column_double(pStmt, i) ) break;
214827 }
214828 }
214829 bChanged = 1;
214830 break;
214831 }
214832
214833 default: {
214834 int n;
214835 int nHdr = 1 + sessionVarintGet(&pCsr[1], &n);
214836 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB )((void) (0));
214837 nAdvance = nHdr + n;
214838 if( eType==sqlite3_column_type(pStmt, i)
214839 && n==sqlite3_column_bytes(pStmt, i)
214840 && (n==0 || 0==memcmp(&pCsr[nHdr], sqlite3_column_blob(pStmt, i), n))
214841 ){
214842 break;
214843 }
214844 bChanged = 1;
214845 }
214846 }
214847
214848 /* If at least one field has been modified, this is not a no-op. */
214849 if( bChanged ) bNoop = 0;
214850
214851 /* Add a field to the old.* record. This is omitted if this modules is
214852 ** currently generating a patchset. */
214853 if( bPatchset==0 ){
214854 if( bChanged || abPK[i] ){
214855 sessionAppendBlob(pBuf, pCsr, nAdvance, &rc);
214856 }else{
214857 sessionAppendByte(pBuf, 0, &rc);
214858 }
214859 }
214860
214861 /* Add a field to the new.* record. Or the only record if currently
214862 ** generating a patchset. */
214863 if( bChanged || (bPatchset && abPK[i]) ){
214864 sessionAppendCol(&buf2, pStmt, i, &rc);
214865 }else{
214866 sessionAppendByte(&buf2, 0, &rc);
214867 }
214868
214869 pCsr += nAdvance;
214870 }
214871
214872 if( bNoop ){
214873 pBuf->nBuf = nRewind;
214874 }else{
214875 sessionAppendBlob(pBuf, buf2.aBuf, buf2.nBuf, &rc);
214876 }
214877 sqlite3_free(buf2.aBuf);
214878
214879 return rc;
214880}
214881
214882/*
214883** Append a DELETE change to the buffer passed as the first argument. Use
214884** the changeset format if argument bPatchset is zero, or the patchset
214885** format otherwise.
214886*/
214887static int sessionAppendDelete(
214888 SessionBuffer *pBuf, /* Buffer to append to */
214889 int bPatchset, /* True for "patchset", 0 for "changeset" */
214890 SessionChange *p, /* Object containing old values */
214891 int nCol, /* Number of columns in table */
214892 u8 *abPK /* Boolean array - true for PK columns */
214893){
214894 int rc = SQLITE_OK0;
214895
214896 sessionAppendByte(pBuf, SQLITE_DELETE9, &rc);
214897 sessionAppendByte(pBuf, p->bIndirect, &rc);
214898
214899 if( bPatchset==0 ){
214900 sessionAppendBlob(pBuf, p->aRecord, p->nRecord, &rc);
214901 }else{
214902 int i;
214903 u8 *a = p->aRecord;
214904 for(i=0; i<nCol; i++){
214905 u8 *pStart = a;
214906 int eType = *a++;
214907
214908 switch( eType ){
214909 case 0:
214910 case SQLITE_NULL5:
214911 assert( abPK[i]==0 )((void) (0));
214912 break;
214913
214914 case SQLITE_FLOAT2:
214915 case SQLITE_INTEGER1:
214916 a += 8;
214917 break;
214918
214919 default: {
214920 int n;
214921 a += sessionVarintGet(a, &n);
214922 a += n;
214923 break;
214924 }
214925 }
214926 if( abPK[i] ){
214927 sessionAppendBlob(pBuf, pStart, (int)(a-pStart), &rc);
214928 }
214929 }
214930 assert( (a - p->aRecord)==p->nRecord )((void) (0));
214931 }
214932
214933 return rc;
214934}
214935
214936/*
214937** Formulate and prepare a SELECT statement to retrieve a row from table
214938** zTab in database zDb based on its primary key. i.e.
214939**
214940** SELECT * FROM zDb.zTab WHERE pk1 = ? AND pk2 = ? AND ...
214941*/
214942static int sessionSelectStmt(
214943 sqlite3 *db, /* Database handle */
214944 const char *zDb, /* Database name */
214945 const char *zTab, /* Table name */
214946 int nCol, /* Number of columns in table */
214947 const char **azCol, /* Names of table columns */
214948 u8 *abPK, /* PRIMARY KEY array */
214949 sqlite3_stmt **ppStmt /* OUT: Prepared SELECT statement */
214950){
214951 int rc = SQLITE_OK0;
214952 char *zSql = 0;
214953 int nSql = -1;
214954
214955 if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){
214956 zSql = sqlite3_mprintf(
214957 "SELECT tbl, ?2, stat FROM %Q.sqlite_stat1 WHERE tbl IS ?1 AND "
214958 "idx IS (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", zDb
214959 );
214960 if( zSql==0 ) rc = SQLITE_NOMEM7;
214961 }else{
214962 int i;
214963 const char *zSep = "";
214964 SessionBuffer buf = {0, 0, 0};
214965
214966 sessionAppendStr(&buf, "SELECT * FROM ", &rc);
214967 sessionAppendIdent(&buf, zDb, &rc);
214968 sessionAppendStr(&buf, ".", &rc);
214969 sessionAppendIdent(&buf, zTab, &rc);
214970 sessionAppendStr(&buf, " WHERE ", &rc);
214971 for(i=0; i<nCol; i++){
214972 if( abPK[i] ){
214973 sessionAppendStr(&buf, zSep, &rc);
214974 sessionAppendIdent(&buf, azCol[i], &rc);
214975 sessionAppendStr(&buf, " IS ?", &rc);
214976 sessionAppendInteger(&buf, i+1, &rc);
214977 zSep = " AND ";
214978 }
214979 }
214980 zSql = (char*)buf.aBuf;
214981 nSql = buf.nBuf;
214982 }
214983
214984 if( rc==SQLITE_OK0 ){
214985 rc = sqlite3_prepare_v2(db, zSql, nSql, ppStmt, 0);
214986 }
214987 sqlite3_free(zSql);
214988 return rc;
214989}
214990
214991/*
214992** Bind the PRIMARY KEY values from the change passed in argument pChange
214993** to the SELECT statement passed as the first argument. The SELECT statement
214994** is as prepared by function sessionSelectStmt().
214995**
214996** Return SQLITE_OK if all PK values are successfully bound, or an SQLite
214997** error code (e.g. SQLITE_NOMEM) otherwise.
214998*/
214999static int sessionSelectBind(
215000 sqlite3_stmt *pSelect, /* SELECT from sessionSelectStmt() */
215001 int nCol, /* Number of columns in table */
215002 u8 *abPK, /* PRIMARY KEY array */
215003 SessionChange *pChange /* Change structure */
215004){
215005 int i;
215006 int rc = SQLITE_OK0;
215007 u8 *a = pChange->aRecord;
215008
215009 for(i=0; i<nCol && rc==SQLITE_OK0; i++){
215010 int eType = *a++;
215011
215012 switch( eType ){
215013 case 0:
215014 case SQLITE_NULL5:
215015 assert( abPK[i]==0 )((void) (0));
215016 break;
215017
215018 case SQLITE_INTEGER1: {
215019 if( abPK[i] ){
215020 i64 iVal = sessionGetI64(a);
215021 rc = sqlite3_bind_int64(pSelect, i+1, iVal);
215022 }
215023 a += 8;
215024 break;
215025 }
215026
215027 case SQLITE_FLOAT2: {
215028 if( abPK[i] ){
215029 double rVal;
215030 i64 iVal = sessionGetI64(a);
215031 memcpy(&rVal, &iVal, 8);
215032 rc = sqlite3_bind_double(pSelect, i+1, rVal);
215033 }
215034 a += 8;
215035 break;
215036 }
215037
215038 case SQLITE_TEXT3: {
215039 int n;
215040 a += sessionVarintGet(a, &n);
215041 if( abPK[i] ){
215042 rc = sqlite3_bind_text(pSelect, i+1, (char *)a, n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
215043 }
215044 a += n;
215045 break;
215046 }
215047
215048 default: {
215049 int n;
215050 assert( eType==SQLITE_BLOB )((void) (0));
215051 a += sessionVarintGet(a, &n);
215052 if( abPK[i] ){
215053 rc = sqlite3_bind_blob(pSelect, i+1, a, n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
215054 }
215055 a += n;
215056 break;
215057 }
215058 }
215059 }
215060
215061 return rc;
215062}
215063
215064/*
215065** This function is a no-op if *pRc is set to other than SQLITE_OK when it
215066** is called. Otherwise, append a serialized table header (part of the binary
215067** changeset format) to buffer *pBuf. If an error occurs, set *pRc to an
215068** SQLite error code before returning.
215069*/
215070static void sessionAppendTableHdr(
215071 SessionBuffer *pBuf, /* Append header to this buffer */
215072 int bPatchset, /* Use the patchset format if true */
215073 SessionTable *pTab, /* Table object to append header for */
215074 int *pRc /* IN/OUT: Error code */
215075){
215076 /* Write a table header */
215077 sessionAppendByte(pBuf, (bPatchset ? 'P' : 'T'), pRc);
215078 sessionAppendVarint(pBuf, pTab->nCol, pRc);
215079 sessionAppendBlob(pBuf, pTab->abPK, pTab->nCol, pRc);
215080 sessionAppendBlob(pBuf, (u8 *)pTab->zName, (int)strlen(pTab->zName)+1, pRc);
215081}
215082
215083/*
215084** Generate either a changeset (if argument bPatchset is zero) or a patchset
215085** (if it is non-zero) based on the current contents of the session object
215086** passed as the first argument.
215087**
215088** If no error occurs, SQLITE_OK is returned and the new changeset/patchset
215089** stored in output variables *pnChangeset and *ppChangeset. Or, if an error
215090** occurs, an SQLite error code is returned and both output variables set
215091** to 0.
215092*/
215093static int sessionGenerateChangeset(
215094 sqlite3_session *pSession, /* Session object */
215095 int bPatchset, /* True for patchset, false for changeset */
215096 int (*xOutput)(void *pOut, const void *pData, int nData),
215097 void *pOut, /* First argument for xOutput */
215098 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
215099 void **ppChangeset /* OUT: Buffer containing changeset */
215100){
215101 sqlite3 *db = pSession->db; /* Source database handle */
215102 SessionTable *pTab; /* Used to iterate through attached tables */
215103 SessionBuffer buf = {0,0,0}; /* Buffer in which to accumlate changeset */
215104 int rc; /* Return code */
215105
215106 assert( xOutput==0 || (pnChangeset==0 && ppChangeset==0) )((void) (0));
215107 assert( xOutput!=0 || (pnChangeset!=0 && ppChangeset!=0) )((void) (0));
215108
215109 /* Zero the output variables in case an error occurs. If this session
215110 ** object is already in the error state (sqlite3_session.rc != SQLITE_OK),
215111 ** this call will be a no-op. */
215112 if( xOutput==0 ){
215113 assert( pnChangeset!=0 && ppChangeset!=0 )((void) (0));
215114 *pnChangeset = 0;
215115 *ppChangeset = 0;
215116 }
215117
215118 if( pSession->rc ) return pSession->rc;
215119 rc = sqlite3_exec(pSession->db, "SAVEPOINT changeset", 0, 0, 0);
215120 if( rc!=SQLITE_OK0 ) return rc;
215121
215122 sqlite3_mutex_enter(sqlite3_db_mutex(db));
215123
215124 for(pTab=pSession->pTable; rc==SQLITE_OK0 && pTab; pTab=pTab->pNext){
215125 if( pTab->nEntry ){
215126 const char *zName = pTab->zName;
215127 int nCol = 0; /* Number of columns in table */
215128 u8 *abPK = 0; /* Primary key array */
215129 const char **azCol = 0; /* Table columns */
215130 int i; /* Used to iterate through hash buckets */
215131 sqlite3_stmt *pSel = 0; /* SELECT statement to query table pTab */
215132 int nRewind = buf.nBuf; /* Initial size of write buffer */
215133 int nNoop; /* Size of buffer after writing tbl header */
215134
215135 /* Check the table schema is still Ok. */
215136 rc = sessionTableInfo(0, db, pSession->zDb, zName, &nCol, 0,&azCol,&abPK);
215137 if( !rc && (pTab->nCol!=nCol || memcmp(abPK, pTab->abPK, nCol)) ){
215138 rc = SQLITE_SCHEMA17;
215139 }
215140
215141 /* Write a table header */
215142 sessionAppendTableHdr(&buf, bPatchset, pTab, &rc);
215143
215144 /* Build and compile a statement to execute: */
215145 if( rc==SQLITE_OK0 ){
215146 rc = sessionSelectStmt(
215147 db, pSession->zDb, zName, nCol, azCol, abPK, &pSel);
215148 }
215149
215150 nNoop = buf.nBuf;
215151 for(i=0; i<pTab->nChange && rc==SQLITE_OK0; i++){
215152 SessionChange *p; /* Used to iterate through changes */
215153
215154 for(p=pTab->apChange[i]; rc==SQLITE_OK0 && p; p=p->pNext){
215155 rc = sessionSelectBind(pSel, nCol, abPK, p);
215156 if( rc!=SQLITE_OK0 ) continue;
215157 if( sqlite3_step(pSel)==SQLITE_ROW100 ){
215158 if( p->op==SQLITE_INSERT18 ){
215159 int iCol;
215160 sessionAppendByte(&buf, SQLITE_INSERT18, &rc);
215161 sessionAppendByte(&buf, p->bIndirect, &rc);
215162 for(iCol=0; iCol<nCol; iCol++){
215163 sessionAppendCol(&buf, pSel, iCol, &rc);
215164 }
215165 }else{
215166 assert( abPK!=0 )((void) (0)); /* Because sessionSelectStmt() returned ok */
215167 rc = sessionAppendUpdate(&buf, bPatchset, pSel, p, abPK);
215168 }
215169 }else if( p->op!=SQLITE_INSERT18 ){
215170 rc = sessionAppendDelete(&buf, bPatchset, p, nCol, abPK);
215171 }
215172 if( rc==SQLITE_OK0 ){
215173 rc = sqlite3_reset(pSel);
215174 }
215175
215176 /* If the buffer is now larger than sessions_strm_chunk_size, pass
215177 ** its contents to the xOutput() callback. */
215178 if( xOutput
215179 && rc==SQLITE_OK0
215180 && buf.nBuf>nNoop
215181 && buf.nBuf>sessions_strm_chunk_size
215182 ){
215183 rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
215184 nNoop = -1;
215185 buf.nBuf = 0;
215186 }
215187
215188 }
215189 }
215190
215191 sqlite3_finalize(pSel);
215192 if( buf.nBuf==nNoop ){
215193 buf.nBuf = nRewind;
215194 }
215195 sqlite3_free((char*)azCol); /* cast works around VC++ bug */
215196 }
215197 }
215198
215199 if( rc==SQLITE_OK0 ){
215200 if( xOutput==0 ){
215201 *pnChangeset = buf.nBuf;
215202 *ppChangeset = buf.aBuf;
215203 buf.aBuf = 0;
215204 }else if( buf.nBuf>0 ){
215205 rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
215206 }
215207 }
215208
215209 sqlite3_free(buf.aBuf);
215210 sqlite3_exec(db, "RELEASE changeset", 0, 0, 0);
215211 sqlite3_mutex_leave(sqlite3_db_mutex(db));
215212 return rc;
215213}
215214
215215/*
215216** Obtain a changeset object containing all changes recorded by the
215217** session object passed as the first argument.
215218**
215219** It is the responsibility of the caller to eventually free the buffer
215220** using sqlite3_free().
215221*/
215222SQLITE_API int sqlite3session_changeset(
215223 sqlite3_session *pSession, /* Session object */
215224 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
215225 void **ppChangeset /* OUT: Buffer containing changeset */
215226){
215227 int rc;
215228
215229 if( pnChangeset==0 || ppChangeset==0 ) return SQLITE_MISUSE21;
215230 rc = sessionGenerateChangeset(pSession, 0, 0, 0, pnChangeset,ppChangeset);
215231 assert( rc || pnChangeset==0((void) (0))
215232 || pSession->bEnableSize==0 || *pnChangeset<=pSession->nMaxChangesetSize((void) (0))
215233 )((void) (0));
215234 return rc;
215235}
215236
215237/*
215238** Streaming version of sqlite3session_changeset().
215239*/
215240SQLITE_API int sqlite3session_changeset_strm(
215241 sqlite3_session *pSession,
215242 int (*xOutput)(void *pOut, const void *pData, int nData),
215243 void *pOut
215244){
215245 if( xOutput==0 ) return SQLITE_MISUSE21;
215246 return sessionGenerateChangeset(pSession, 0, xOutput, pOut, 0, 0);
215247}
215248
215249/*
215250** Streaming version of sqlite3session_patchset().
215251*/
215252SQLITE_API int sqlite3session_patchset_strm(
215253 sqlite3_session *pSession,
215254 int (*xOutput)(void *pOut, const void *pData, int nData),
215255 void *pOut
215256){
215257 if( xOutput==0 ) return SQLITE_MISUSE21;
215258 return sessionGenerateChangeset(pSession, 1, xOutput, pOut, 0, 0);
215259}
215260
215261/*
215262** Obtain a patchset object containing all changes recorded by the
215263** session object passed as the first argument.
215264**
215265** It is the responsibility of the caller to eventually free the buffer
215266** using sqlite3_free().
215267*/
215268SQLITE_API int sqlite3session_patchset(
215269 sqlite3_session *pSession, /* Session object */
215270 int *pnPatchset, /* OUT: Size of buffer at *ppChangeset */
215271 void **ppPatchset /* OUT: Buffer containing changeset */
215272){
215273 if( pnPatchset==0 || ppPatchset==0 ) return SQLITE_MISUSE21;
215274 return sessionGenerateChangeset(pSession, 1, 0, 0, pnPatchset, ppPatchset);
215275}
215276
215277/*
215278** Enable or disable the session object passed as the first argument.
215279*/
215280SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable){
215281 int ret;
215282 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
215283 if( bEnable>=0 ){
215284 pSession->bEnable = bEnable;
215285 }
215286 ret = pSession->bEnable;
215287 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
215288 return ret;
215289}
215290
215291/*
215292** Enable or disable the session object passed as the first argument.
215293*/
215294SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect){
215295 int ret;
215296 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
215297 if( bIndirect>=0 ){
215298 pSession->bIndirect = bIndirect;
215299 }
215300 ret = pSession->bIndirect;
215301 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
215302 return ret;
215303}
215304
215305/*
215306** Return true if there have been no changes to monitored tables recorded
215307** by the session object passed as the only argument.
215308*/
215309SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession){
215310 int ret = 0;
215311 SessionTable *pTab;
215312
215313 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
215314 for(pTab=pSession->pTable; pTab && ret==0; pTab=pTab->pNext){
215315 ret = (pTab->nEntry>0);
215316 }
215317 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
215318
215319 return (ret==0);
215320}
215321
215322/*
215323** Return the amount of heap memory in use.
215324*/
215325SQLITE_API sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession){
215326 return pSession->nMalloc;
215327}
215328
215329/*
215330** Configure the session object passed as the first argument.
215331*/
215332SQLITE_API int sqlite3session_object_config(sqlite3_session *pSession, int op, void *pArg){
215333 int rc = SQLITE_OK0;
215334 switch( op ){
215335 case SQLITE_SESSION_OBJCONFIG_SIZE1: {
215336 int iArg = *(int*)pArg;
215337 if( iArg>=0 ){
215338 if( pSession->pTable ){
215339 rc = SQLITE_MISUSE21;
215340 }else{
215341 pSession->bEnableSize = (iArg!=0);
215342 }
215343 }
215344 *(int*)pArg = pSession->bEnableSize;
215345 break;
215346 }
215347
215348 default:
215349 rc = SQLITE_MISUSE21;
215350 }
215351
215352 return rc;
215353}
215354
215355/*
215356** Return the maximum size of sqlite3session_changeset() output.
215357*/
215358SQLITE_API sqlite3_int64 sqlite3session_changeset_size(sqlite3_session *pSession){
215359 return pSession->nMaxChangesetSize;
215360}
215361
215362/*
215363** Do the work for either sqlite3changeset_start() or start_strm().
215364*/
215365static int sessionChangesetStart(
215366 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
215367 int (*xInput)(void *pIn, void *pData, int *pnData),
215368 void *pIn,
215369 int nChangeset, /* Size of buffer pChangeset in bytes */
215370 void *pChangeset, /* Pointer to buffer containing changeset */
215371 int bInvert, /* True to invert changeset */
215372 int bSkipEmpty /* True to skip empty UPDATE changes */
215373){
215374 sqlite3_changeset_iter *pRet; /* Iterator to return */
215375 int nByte; /* Number of bytes to allocate for iterator */
215376
215377 assert( xInput==0 || (pChangeset==0 && nChangeset==0) )((void) (0));
215378
215379 /* Zero the output variable in case an error occurs. */
215380 *pp = 0;
215381
215382 /* Allocate and initialize the iterator structure. */
215383 nByte = sizeof(sqlite3_changeset_iter);
215384 pRet = (sqlite3_changeset_iter *)sqlite3_malloc(nByte);
215385 if( !pRet ) return SQLITE_NOMEM7;
215386 memset(pRet, 0, sizeof(sqlite3_changeset_iter));
215387 pRet->in.aData = (u8 *)pChangeset;
215388 pRet->in.nData = nChangeset;
215389 pRet->in.xInput = xInput;
215390 pRet->in.pIn = pIn;
215391 pRet->in.bEof = (xInput ? 0 : 1);
215392 pRet->bInvert = bInvert;
215393 pRet->bSkipEmpty = bSkipEmpty;
215394
215395 /* Populate the output variable and return success. */
215396 *pp = pRet;
215397 return SQLITE_OK0;
215398}
215399
215400/*
215401** Create an iterator used to iterate through the contents of a changeset.
215402*/
215403SQLITE_API int sqlite3changeset_start(
215404 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
215405 int nChangeset, /* Size of buffer pChangeset in bytes */
215406 void *pChangeset /* Pointer to buffer containing changeset */
215407){
215408 return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, 0, 0);
215409}
215410SQLITE_API int sqlite3changeset_start_v2(
215411 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
215412 int nChangeset, /* Size of buffer pChangeset in bytes */
215413 void *pChangeset, /* Pointer to buffer containing changeset */
215414 int flags
215415){
215416 int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT0x0002);
215417 return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, bInvert, 0);
215418}
215419
215420/*
215421** Streaming version of sqlite3changeset_start().
215422*/
215423SQLITE_API int sqlite3changeset_start_strm(
215424 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
215425 int (*xInput)(void *pIn, void *pData, int *pnData),
215426 void *pIn
215427){
215428 return sessionChangesetStart(pp, xInput, pIn, 0, 0, 0, 0);
215429}
215430SQLITE_API int sqlite3changeset_start_v2_strm(
215431 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
215432 int (*xInput)(void *pIn, void *pData, int *pnData),
215433 void *pIn,
215434 int flags
215435){
215436 int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT0x0002);
215437 return sessionChangesetStart(pp, xInput, pIn, 0, 0, bInvert, 0);
215438}
215439
215440/*
215441** If the SessionInput object passed as the only argument is a streaming
215442** object and the buffer is full, discard some data to free up space.
215443*/
215444static void sessionDiscardData(SessionInput *pIn){
215445 if( pIn->xInput && pIn->iNext>=sessions_strm_chunk_size ){
215446 int nMove = pIn->buf.nBuf - pIn->iNext;
215447 assert( nMove>=0 )((void) (0));
215448 if( nMove>0 ){
215449 memmove(pIn->buf.aBuf, &pIn->buf.aBuf[pIn->iNext], nMove);
215450 }
215451 pIn->buf.nBuf -= pIn->iNext;
215452 pIn->iNext = 0;
215453 pIn->nData = pIn->buf.nBuf;
215454 }
215455}
215456
215457/*
215458** Ensure that there are at least nByte bytes available in the buffer. Or,
215459** if there are not nByte bytes remaining in the input, that all available
215460** data is in the buffer.
215461**
215462** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
215463*/
215464static int sessionInputBuffer(SessionInput *pIn, int nByte){
215465 int rc = SQLITE_OK0;
215466 if( pIn->xInput ){
215467 while( !pIn->bEof && (pIn->iNext+nByte)>=pIn->nData && rc==SQLITE_OK0 ){
215468 int nNew = sessions_strm_chunk_size;
215469
215470 if( pIn->bNoDiscard==0 ) sessionDiscardData(pIn);
215471 if( SQLITE_OK0==sessionBufferGrow(&pIn->buf, nNew, &rc) ){
215472 rc = pIn->xInput(pIn->pIn, &pIn->buf.aBuf[pIn->buf.nBuf], &nNew);
215473 if( nNew==0 ){
215474 pIn->bEof = 1;
215475 }else{
215476 pIn->buf.nBuf += nNew;
215477 }
215478 }
215479
215480 pIn->aData = pIn->buf.aBuf;
215481 pIn->nData = pIn->buf.nBuf;
215482 }
215483 }
215484 return rc;
215485}
215486
215487/*
215488** When this function is called, *ppRec points to the start of a record
215489** that contains nCol values. This function advances the pointer *ppRec
215490** until it points to the byte immediately following that record.
215491*/
215492static void sessionSkipRecord(
215493 u8 **ppRec, /* IN/OUT: Record pointer */
215494 int nCol /* Number of values in record */
215495){
215496 u8 *aRec = *ppRec;
215497 int i;
215498 for(i=0; i<nCol; i++){
215499 int eType = *aRec++;
215500 if( eType==SQLITE_TEXT3 || eType==SQLITE_BLOB4 ){
215501 int nByte;
215502 aRec += sessionVarintGet((u8*)aRec, &nByte);
215503 aRec += nByte;
215504 }else if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
215505 aRec += 8;
215506 }
215507 }
215508
215509 *ppRec = aRec;
215510}
215511
215512/*
215513** This function sets the value of the sqlite3_value object passed as the
215514** first argument to a copy of the string or blob held in the aData[]
215515** buffer. SQLITE_OK is returned if successful, or SQLITE_NOMEM if an OOM
215516** error occurs.
215517*/
215518static int sessionValueSetStr(
215519 sqlite3_value *pVal, /* Set the value of this object */
215520 u8 *aData, /* Buffer containing string or blob data */
215521 int nData, /* Size of buffer aData[] in bytes */
215522 u8 enc /* String encoding (0 for blobs) */
215523){
215524 /* In theory this code could just pass SQLITE_TRANSIENT as the final
215525 ** argument to sqlite3ValueSetStr() and have the copy created
215526 ** automatically. But doing so makes it difficult to detect any OOM
215527 ** error. Hence the code to create the copy externally. */
215528 u8 *aCopy = sqlite3_malloc64((sqlite3_int64)nData+1);
215529 if( aCopy==0 ) return SQLITE_NOMEM7;
215530 memcpy(aCopy, aData, nData);
215531 sqlite3ValueSetStr(pVal, nData, (char*)aCopy, enc, sqlite3_free);
215532 return SQLITE_OK0;
215533}
215534
215535/*
215536** Deserialize a single record from a buffer in memory. See "RECORD FORMAT"
215537** for details.
215538**
215539** When this function is called, *paChange points to the start of the record
215540** to deserialize. Assuming no error occurs, *paChange is set to point to
215541** one byte after the end of the same record before this function returns.
215542** If the argument abPK is NULL, then the record contains nCol values. Or,
215543** if abPK is other than NULL, then the record contains only the PK fields
215544** (in other words, it is a patchset DELETE record).
215545**
215546** If successful, each element of the apOut[] array (allocated by the caller)
215547** is set to point to an sqlite3_value object containing the value read
215548** from the corresponding position in the record. If that value is not
215549** included in the record (i.e. because the record is part of an UPDATE change
215550** and the field was not modified), the corresponding element of apOut[] is
215551** set to NULL.
215552**
215553** It is the responsibility of the caller to free all sqlite_value structures
215554** using sqlite3_free().
215555**
215556** If an error occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
215557** The apOut[] array may have been partially populated in this case.
215558*/
215559static int sessionReadRecord(
215560 SessionInput *pIn, /* Input data */
215561 int nCol, /* Number of values in record */
215562 u8 *abPK, /* Array of primary key flags, or NULL */
215563 sqlite3_value **apOut, /* Write values to this array */
215564 int *pbEmpty
215565){
215566 int i; /* Used to iterate through columns */
215567 int rc = SQLITE_OK0;
215568
215569 assert( pbEmpty==0 || *pbEmpty==0 )((void) (0));
215570 if( pbEmpty ) *pbEmpty = 1;
215571 for(i=0; i<nCol && rc==SQLITE_OK0; i++){
215572 int eType = 0; /* Type of value (SQLITE_NULL, TEXT etc.) */
215573 if( abPK && abPK[i]==0 ) continue;
215574 rc = sessionInputBuffer(pIn, 9);
215575 if( rc==SQLITE_OK0 ){
215576 if( pIn->iNext>=pIn->nData ){
215577 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(215577);
215578 }else{
215579 eType = pIn->aData[pIn->iNext++];
215580 assert( apOut[i]==0 )((void) (0));
215581 if( eType ){
215582 if( pbEmpty ) *pbEmpty = 0;
215583 apOut[i] = sqlite3ValueNew(0);
215584 if( !apOut[i] ) rc = SQLITE_NOMEM7;
215585 }
215586 }
215587 }
215588
215589 if( rc==SQLITE_OK0 ){
215590 u8 *aVal = &pIn->aData[pIn->iNext];
215591 if( eType==SQLITE_TEXT3 || eType==SQLITE_BLOB4 ){
215592 int nByte;
215593 pIn->iNext += sessionVarintGet(aVal, &nByte);
215594 rc = sessionInputBuffer(pIn, nByte);
215595 if( rc==SQLITE_OK0 ){
215596 if( nByte<0 || nByte>pIn->nData-pIn->iNext ){
215597 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(215597);
215598 }else{
215599 u8 enc = (eType==SQLITE_TEXT3 ? SQLITE_UTF81 : 0);
215600 rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
215601 pIn->iNext += nByte;
215602 }
215603 }
215604 }
215605 if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
215606 sqlite3_int64 v = sessionGetI64(aVal);
215607 if( eType==SQLITE_INTEGER1 ){
215608 sqlite3VdbeMemSetInt64(apOut[i], v);
215609 }else{
215610 double d;
215611 memcpy(&d, &v, 8);
215612 sqlite3VdbeMemSetDouble(apOut[i], d);
215613 }
215614 pIn->iNext += 8;
215615 }
215616 }
215617 }
215618
215619 return rc;
215620}
215621
215622/*
215623** The input pointer currently points to the second byte of a table-header.
215624** Specifically, to the following:
215625**
215626** + number of columns in table (varint)
215627** + array of PK flags (1 byte per column),
215628** + table name (nul terminated).
215629**
215630** This function ensures that all of the above is present in the input
215631** buffer (i.e. that it can be accessed without any calls to xInput()).
215632** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
215633** The input pointer is not moved.
215634*/
215635static int sessionChangesetBufferTblhdr(SessionInput *pIn, int *pnByte){
215636 int rc = SQLITE_OK0;
215637 int nCol = 0;
215638 int nRead = 0;
215639
215640 rc = sessionInputBuffer(pIn, 9);
215641 if( rc==SQLITE_OK0 ){
215642 nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
215643 /* The hard upper limit for the number of columns in an SQLite
215644 ** database table is, according to sqliteLimit.h, 32676. So
215645 ** consider any table-header that purports to have more than 65536
215646 ** columns to be corrupt. This is convenient because otherwise,
215647 ** if the (nCol>65536) condition below were omitted, a sufficiently
215648 ** large value for nCol may cause nRead to wrap around and become
215649 ** negative. Leading to a crash. */
215650 if( nCol<0 || nCol>65536 ){
215651 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(215651);
215652 }else{
215653 rc = sessionInputBuffer(pIn, nRead+nCol+100);
215654 nRead += nCol;
215655 }
215656 }
215657
215658 while( rc==SQLITE_OK0 ){
215659 while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
215660 nRead++;
215661 }
215662 if( (pIn->iNext + nRead)<pIn->nData ) break;
215663 rc = sessionInputBuffer(pIn, nRead + 100);
215664 }
215665 *pnByte = nRead+1;
215666 return rc;
215667}
215668
215669/*
215670** The input pointer currently points to the first byte of the first field
215671** of a record consisting of nCol columns. This function ensures the entire
215672** record is buffered. It does not move the input pointer.
215673**
215674** If successful, SQLITE_OK is returned and *pnByte is set to the size of
215675** the record in bytes. Otherwise, an SQLite error code is returned. The
215676** final value of *pnByte is undefined in this case.
215677*/
215678static int sessionChangesetBufferRecord(
215679 SessionInput *pIn, /* Input data */
215680 int nCol, /* Number of columns in record */
215681 int *pnByte /* OUT: Size of record in bytes */
215682){
215683 int rc = SQLITE_OK0;
215684 int nByte = 0;
215685 int i;
215686 for(i=0; rc==SQLITE_OK0 && i<nCol; i++){
215687 int eType;
215688 rc = sessionInputBuffer(pIn, nByte + 10);
215689 if( rc==SQLITE_OK0 ){
215690 eType = pIn->aData[pIn->iNext + nByte++];
215691 if( eType==SQLITE_TEXT3 || eType==SQLITE_BLOB4 ){
215692 int n;
215693 nByte += sessionVarintGet(&pIn->aData[pIn->iNext+nByte], &n);
215694 nByte += n;
215695 rc = sessionInputBuffer(pIn, nByte);
215696 }else if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
215697 nByte += 8;
215698 }
215699 }
215700 }
215701 *pnByte = nByte;
215702 return rc;
215703}
215704
215705/*
215706** The input pointer currently points to the second byte of a table-header.
215707** Specifically, to the following:
215708**
215709** + number of columns in table (varint)
215710** + array of PK flags (1 byte per column),
215711** + table name (nul terminated).
215712**
215713** This function decodes the table-header and populates the p->nCol,
215714** p->zTab and p->abPK[] variables accordingly. The p->apValue[] array is
215715** also allocated or resized according to the new value of p->nCol. The
215716** input pointer is left pointing to the byte following the table header.
215717**
215718** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code
215719** is returned and the final values of the various fields enumerated above
215720** are undefined.
215721*/
215722static int sessionChangesetReadTblhdr(sqlite3_changeset_iter *p){
215723 int rc;
215724 int nCopy;
215725 assert( p->rc==SQLITE_OK )((void) (0));
215726
215727 rc = sessionChangesetBufferTblhdr(&p->in, &nCopy);
215728 if( rc==SQLITE_OK0 ){
215729 int nByte;
215730 int nVarint;
215731 nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol);
215732 if( p->nCol>0 ){
215733 nCopy -= nVarint;
215734 p->in.iNext += nVarint;
215735 nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
215736 p->tblhdr.nBuf = 0;
215737 sessionBufferGrow(&p->tblhdr, nByte, &rc);
215738 }else{
215739 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(215739);
215740 }
215741 }
215742
215743 if( rc==SQLITE_OK0 ){
215744 size_t iPK = sizeof(sqlite3_value*)*p->nCol*2;
215745 memset(p->tblhdr.aBuf, 0, iPK);
215746 memcpy(&p->tblhdr.aBuf[iPK], &p->in.aData[p->in.iNext], nCopy);
215747 p->in.iNext += nCopy;
215748 }
215749
215750 p->apValue = (sqlite3_value**)p->tblhdr.aBuf;
215751 if( p->apValue==0 ){
215752 p->abPK = 0;
215753 p->zTab = 0;
215754 }else{
215755 p->abPK = (u8*)&p->apValue[p->nCol*2];
215756 p->zTab = p->abPK ? (char*)&p->abPK[p->nCol] : 0;
215757 }
215758 return (p->rc = rc);
215759}
215760
215761/*
215762** Advance the changeset iterator to the next change. The differences between
215763** this function and sessionChangesetNext() are that
215764**
215765** * If pbEmpty is not NULL and the change is a no-op UPDATE (an UPDATE
215766** that modifies no columns), this function sets (*pbEmpty) to 1.
215767**
215768** * If the iterator is configured to skip no-op UPDATEs,
215769** sessionChangesetNext() does that. This function does not.
215770*/
215771static int sessionChangesetNextOne(
215772 sqlite3_changeset_iter *p, /* Changeset iterator */
215773 u8 **paRec, /* If non-NULL, store record pointer here */
215774 int *pnRec, /* If non-NULL, store size of record here */
215775 int *pbNew, /* If non-NULL, true if new table */
215776 int *pbEmpty
215777){
215778 int i;
215779 u8 op;
215780
215781 assert( (paRec==0 && pnRec==0) || (paRec && pnRec) )((void) (0));
215782 assert( pbEmpty==0 || *pbEmpty==0 )((void) (0));
215783
215784 /* If the iterator is in the error-state, return immediately. */
215785 if( p->rc!=SQLITE_OK0 ) return p->rc;
215786
215787 /* Free the current contents of p->apValue[], if any. */
215788 if( p->apValue ){
215789 for(i=0; i<p->nCol*2; i++){
215790 sqlite3ValueFree(p->apValue[i]);
215791 }
215792 memset(p->apValue, 0, sizeof(sqlite3_value*)*p->nCol*2);
215793 }
215794
215795 /* Make sure the buffer contains at least 10 bytes of input data, or all
215796 ** remaining data if there are less than 10 bytes available. This is
215797 ** sufficient either for the 'T' or 'P' byte and the varint that follows
215798 ** it, or for the two single byte values otherwise. */
215799 p->rc = sessionInputBuffer(&p->in, 2);
215800 if( p->rc!=SQLITE_OK0 ) return p->rc;
215801
215802 /* If the iterator is already at the end of the changeset, return DONE. */
215803 if( p->in.iNext>=p->in.nData ){
215804 return SQLITE_DONE101;
215805 }
215806
215807 sessionDiscardData(&p->in);
215808 p->in.iCurrent = p->in.iNext;
215809
215810 op = p->in.aData[p->in.iNext++];
215811 while( op=='T' || op=='P' ){
215812 if( pbNew ) *pbNew = 1;
215813 p->bPatchset = (op=='P');
215814 if( sessionChangesetReadTblhdr(p) ) return p->rc;
215815 if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
215816 p->in.iCurrent = p->in.iNext;
215817 if( p->in.iNext>=p->in.nData ) return SQLITE_DONE101;
215818 op = p->in.aData[p->in.iNext++];
215819 }
215820
215821 if( p->zTab==0 || (p->bPatchset && p->bInvert) ){
215822 /* The first record in the changeset is not a table header. Must be a
215823 ** corrupt changeset. */
215824 assert( p->in.iNext==1 || p->zTab )((void) (0));
215825 return (p->rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(215825));
215826 }
215827
215828 p->op = op;
215829 p->bIndirect = p->in.aData[p->in.iNext++];
215830 if( p->op!=SQLITE_UPDATE23 && p->op!=SQLITE_DELETE9 && p->op!=SQLITE_INSERT18 ){
215831 return (p->rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(215831));
215832 }
215833
215834 if( paRec ){
215835 int nVal; /* Number of values to buffer */
215836 if( p->bPatchset==0 && op==SQLITE_UPDATE23 ){
215837 nVal = p->nCol * 2;
215838 }else if( p->bPatchset && op==SQLITE_DELETE9 ){
215839 nVal = 0;
215840 for(i=0; i<p->nCol; i++) if( p->abPK[i] ) nVal++;
215841 }else{
215842 nVal = p->nCol;
215843 }
215844 p->rc = sessionChangesetBufferRecord(&p->in, nVal, pnRec);
215845 if( p->rc!=SQLITE_OK0 ) return p->rc;
215846 *paRec = &p->in.aData[p->in.iNext];
215847 p->in.iNext += *pnRec;
215848 }else{
215849 sqlite3_value **apOld = (p->bInvert ? &p->apValue[p->nCol] : p->apValue);
215850 sqlite3_value **apNew = (p->bInvert ? p->apValue : &p->apValue[p->nCol]);
215851
215852 /* If this is an UPDATE or DELETE, read the old.* record. */
215853 if( p->op!=SQLITE_INSERT18 && (p->bPatchset==0 || p->op==SQLITE_DELETE9) ){
215854 u8 *abPK = p->bPatchset ? p->abPK : 0;
215855 p->rc = sessionReadRecord(&p->in, p->nCol, abPK, apOld, 0);
215856 if( p->rc!=SQLITE_OK0 ) return p->rc;
215857 }
215858
215859 /* If this is an INSERT or UPDATE, read the new.* record. */
215860 if( p->op!=SQLITE_DELETE9 ){
215861 p->rc = sessionReadRecord(&p->in, p->nCol, 0, apNew, pbEmpty);
215862 if( p->rc!=SQLITE_OK0 ) return p->rc;
215863 }
215864
215865 if( (p->bPatchset || p->bInvert) && p->op==SQLITE_UPDATE23 ){
215866 /* If this is an UPDATE that is part of a patchset, then all PK and
215867 ** modified fields are present in the new.* record. The old.* record
215868 ** is currently completely empty. This block shifts the PK fields from
215869 ** new.* to old.*, to accommodate the code that reads these arrays. */
215870 for(i=0; i<p->nCol; i++){
215871 assert( p->bPatchset==0 || p->apValue[i]==0 )((void) (0));
215872 if( p->abPK[i] ){
215873 assert( p->apValue[i]==0 )((void) (0));
215874 p->apValue[i] = p->apValue[i+p->nCol];
215875 if( p->apValue[i]==0 ) return (p->rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(215875));
215876 p->apValue[i+p->nCol] = 0;
215877 }
215878 }
215879 }else if( p->bInvert ){
215880 if( p->op==SQLITE_INSERT18 ) p->op = SQLITE_DELETE9;
215881 else if( p->op==SQLITE_DELETE9 ) p->op = SQLITE_INSERT18;
215882 }
215883 }
215884
215885 return SQLITE_ROW100;
215886}
215887
215888/*
215889** Advance the changeset iterator to the next change.
215890**
215891** If both paRec and pnRec are NULL, then this function works like the public
215892** API sqlite3changeset_next(). If SQLITE_ROW is returned, then the
215893** sqlite3changeset_new() and old() APIs may be used to query for values.
215894**
215895** Otherwise, if paRec and pnRec are not NULL, then a pointer to the change
215896** record is written to *paRec before returning and the number of bytes in
215897** the record to *pnRec.
215898**
215899** Either way, this function returns SQLITE_ROW if the iterator is
215900** successfully advanced to the next change in the changeset, an SQLite
215901** error code if an error occurs, or SQLITE_DONE if there are no further
215902** changes in the changeset.
215903*/
215904static int sessionChangesetNext(
215905 sqlite3_changeset_iter *p, /* Changeset iterator */
215906 u8 **paRec, /* If non-NULL, store record pointer here */
215907 int *pnRec, /* If non-NULL, store size of record here */
215908 int *pbNew /* If non-NULL, true if new table */
215909){
215910 int bEmpty;
215911 int rc;
215912 do {
215913 bEmpty = 0;
215914 rc = sessionChangesetNextOne(p, paRec, pnRec, pbNew, &bEmpty);
215915 }while( rc==SQLITE_ROW100 && p->bSkipEmpty && bEmpty);
215916 return rc;
215917}
215918
215919/*
215920** Advance an iterator created by sqlite3changeset_start() to the next
215921** change in the changeset. This function may return SQLITE_ROW, SQLITE_DONE
215922** or SQLITE_CORRUPT.
215923**
215924** This function may not be called on iterators passed to a conflict handler
215925** callback by changeset_apply().
215926*/
215927SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *p){
215928 return sessionChangesetNext(p, 0, 0, 0);
215929}
215930
215931/*
215932** The following function extracts information on the current change
215933** from a changeset iterator. It may only be called after changeset_next()
215934** has returned SQLITE_ROW.
215935*/
215936SQLITE_API int sqlite3changeset_op(
215937 sqlite3_changeset_iter *pIter, /* Iterator handle */
215938 const char **pzTab, /* OUT: Pointer to table name */
215939 int *pnCol, /* OUT: Number of columns in table */
215940 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
215941 int *pbIndirect /* OUT: True if change is indirect */
215942){
215943 *pOp = pIter->op;
215944 *pnCol = pIter->nCol;
215945 *pzTab = pIter->zTab;
215946 if( pbIndirect ) *pbIndirect = pIter->bIndirect;
215947 return SQLITE_OK0;
215948}
215949
215950/*
215951** Return information regarding the PRIMARY KEY and number of columns in
215952** the database table affected by the change that pIter currently points
215953** to. This function may only be called after changeset_next() returns
215954** SQLITE_ROW.
215955*/
215956SQLITE_API int sqlite3changeset_pk(
215957 sqlite3_changeset_iter *pIter, /* Iterator object */
215958 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
215959 int *pnCol /* OUT: Number of entries in output array */
215960){
215961 *pabPK = pIter->abPK;
215962 if( pnCol ) *pnCol = pIter->nCol;
215963 return SQLITE_OK0;
215964}
215965
215966/*
215967** This function may only be called while the iterator is pointing to an
215968** SQLITE_UPDATE or SQLITE_DELETE change (see sqlite3changeset_op()).
215969** Otherwise, SQLITE_MISUSE is returned.
215970**
215971** It sets *ppValue to point to an sqlite3_value structure containing the
215972** iVal'th value in the old.* record. Or, if that particular value is not
215973** included in the record (because the change is an UPDATE and the field
215974** was not modified and is not a PK column), set *ppValue to NULL.
215975**
215976** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
215977** not modified. Otherwise, SQLITE_OK.
215978*/
215979SQLITE_API int sqlite3changeset_old(
215980 sqlite3_changeset_iter *pIter, /* Changeset iterator */
215981 int iVal, /* Index of old.* value to retrieve */
215982 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
215983){
215984 if( pIter->op!=SQLITE_UPDATE23 && pIter->op!=SQLITE_DELETE9 ){
215985 return SQLITE_MISUSE21;
215986 }
215987 if( iVal<0 || iVal>=pIter->nCol ){
215988 return SQLITE_RANGE25;
215989 }
215990 *ppValue = pIter->apValue[iVal];
215991 return SQLITE_OK0;
215992}
215993
215994/*
215995** This function may only be called while the iterator is pointing to an
215996** SQLITE_UPDATE or SQLITE_INSERT change (see sqlite3changeset_op()).
215997** Otherwise, SQLITE_MISUSE is returned.
215998**
215999** It sets *ppValue to point to an sqlite3_value structure containing the
216000** iVal'th value in the new.* record. Or, if that particular value is not
216001** included in the record (because the change is an UPDATE and the field
216002** was not modified), set *ppValue to NULL.
216003**
216004** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
216005** not modified. Otherwise, SQLITE_OK.
216006*/
216007SQLITE_API int sqlite3changeset_new(
216008 sqlite3_changeset_iter *pIter, /* Changeset iterator */
216009 int iVal, /* Index of new.* value to retrieve */
216010 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
216011){
216012 if( pIter->op!=SQLITE_UPDATE23 && pIter->op!=SQLITE_INSERT18 ){
216013 return SQLITE_MISUSE21;
216014 }
216015 if( iVal<0 || iVal>=pIter->nCol ){
216016 return SQLITE_RANGE25;
216017 }
216018 *ppValue = pIter->apValue[pIter->nCol+iVal];
216019 return SQLITE_OK0;
216020}
216021
216022/*
216023** The following two macros are used internally. They are similar to the
216024** sqlite3changeset_new() and sqlite3changeset_old() functions, except that
216025** they omit all error checking and return a pointer to the requested value.
216026*/
216027#define sessionChangesetNew(pIter, iVal)(pIter)->apValue[(pIter)->nCol+(iVal)] (pIter)->apValue[(pIter)->nCol+(iVal)]
216028#define sessionChangesetOld(pIter, iVal)(pIter)->apValue[(iVal)] (pIter)->apValue[(iVal)]
216029
216030/*
216031** This function may only be called with a changeset iterator that has been
216032** passed to an SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT
216033** conflict-handler function. Otherwise, SQLITE_MISUSE is returned.
216034**
216035** If successful, *ppValue is set to point to an sqlite3_value structure
216036** containing the iVal'th value of the conflicting record.
216037**
216038** If value iVal is out-of-range or some other error occurs, an SQLite error
216039** code is returned. Otherwise, SQLITE_OK.
216040*/
216041SQLITE_API int sqlite3changeset_conflict(
216042 sqlite3_changeset_iter *pIter, /* Changeset iterator */
216043 int iVal, /* Index of conflict record value to fetch */
216044 sqlite3_value **ppValue /* OUT: Value from conflicting row */
216045){
216046 if( !pIter->pConflict ){
216047 return SQLITE_MISUSE21;
216048 }
216049 if( iVal<0 || iVal>=pIter->nCol ){
216050 return SQLITE_RANGE25;
216051 }
216052 *ppValue = sqlite3_column_value(pIter->pConflict, iVal);
216053 return SQLITE_OK0;
216054}
216055
216056/*
216057** This function may only be called with an iterator passed to an
216058** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
216059** it sets the output variable to the total number of known foreign key
216060** violations in the destination database and returns SQLITE_OK.
216061**
216062** In all other cases this function returns SQLITE_MISUSE.
216063*/
216064SQLITE_API int sqlite3changeset_fk_conflicts(
216065 sqlite3_changeset_iter *pIter, /* Changeset iterator */
216066 int *pnOut /* OUT: Number of FK violations */
216067){
216068 if( pIter->pConflict || pIter->apValue ){
216069 return SQLITE_MISUSE21;
216070 }
216071 *pnOut = pIter->nCol;
216072 return SQLITE_OK0;
216073}
216074
216075
216076/*
216077** Finalize an iterator allocated with sqlite3changeset_start().
216078**
216079** This function may not be called on iterators passed to a conflict handler
216080** callback by changeset_apply().
216081*/
216082SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *p){
216083 int rc = SQLITE_OK0;
216084 if( p ){
216085 int i; /* Used to iterate through p->apValue[] */
216086 rc = p->rc;
216087 if( p->apValue ){
216088 for(i=0; i<p->nCol*2; i++) sqlite3ValueFree(p->apValue[i]);
216089 }
216090 sqlite3_free(p->tblhdr.aBuf);
216091 sqlite3_free(p->in.buf.aBuf);
216092 sqlite3_free(p);
216093 }
216094 return rc;
216095}
216096
216097static int sessionChangesetInvert(
216098 SessionInput *pInput, /* Input changeset */
216099 int (*xOutput)(void *pOut, const void *pData, int nData),
216100 void *pOut,
216101 int *pnInverted, /* OUT: Number of bytes in output changeset */
216102 void **ppInverted /* OUT: Inverse of pChangeset */
216103){
216104 int rc = SQLITE_OK0; /* Return value */
216105 SessionBuffer sOut; /* Output buffer */
216106 int nCol = 0; /* Number of cols in current table */
216107 u8 *abPK = 0; /* PK array for current table */
216108 sqlite3_value **apVal = 0; /* Space for values for UPDATE inversion */
216109 SessionBuffer sPK = {0, 0, 0}; /* PK array for current table */
216110
216111 /* Initialize the output buffer */
216112 memset(&sOut, 0, sizeof(SessionBuffer));
216113
216114 /* Zero the output variables in case an error occurs. */
216115 if( ppInverted ){
216116 *ppInverted = 0;
216117 *pnInverted = 0;
216118 }
216119
216120 while( 1 ){
216121 u8 eType;
216122
216123 /* Test for EOF. */
216124 if( (rc = sessionInputBuffer(pInput, 2)) ) goto finished_invert;
216125 if( pInput->iNext>=pInput->nData ) break;
216126 eType = pInput->aData[pInput->iNext];
216127
216128 switch( eType ){
216129 case 'T': {
216130 /* A 'table' record consists of:
216131 **
216132 ** * A constant 'T' character,
216133 ** * Number of columns in said table (a varint),
216134 ** * An array of nCol bytes (sPK),
216135 ** * A nul-terminated table name.
216136 */
216137 int nByte;
216138 int nVar;
216139 pInput->iNext++;
216140 if( (rc = sessionChangesetBufferTblhdr(pInput, &nByte)) ){
216141 goto finished_invert;
216142 }
216143 nVar = sessionVarintGet(&pInput->aData[pInput->iNext], &nCol);
216144 sPK.nBuf = 0;
216145 sessionAppendBlob(&sPK, &pInput->aData[pInput->iNext+nVar], nCol, &rc);
216146 sessionAppendByte(&sOut, eType, &rc);
216147 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
216148 if( rc ) goto finished_invert;
216149
216150 pInput->iNext += nByte;
216151 sqlite3_free(apVal);
216152 apVal = 0;
216153 abPK = sPK.aBuf;
216154 break;
216155 }
216156
216157 case SQLITE_INSERT18:
216158 case SQLITE_DELETE9: {
216159 int nByte;
216160 int bIndirect = pInput->aData[pInput->iNext+1];
216161 int eType2 = (eType==SQLITE_DELETE9 ? SQLITE_INSERT18 : SQLITE_DELETE9);
216162 pInput->iNext += 2;
216163 assert( rc==SQLITE_OK )((void) (0));
216164 rc = sessionChangesetBufferRecord(pInput, nCol, &nByte);
216165 sessionAppendByte(&sOut, eType2, &rc);
216166 sessionAppendByte(&sOut, bIndirect, &rc);
216167 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
216168 pInput->iNext += nByte;
216169 if( rc ) goto finished_invert;
216170 break;
216171 }
216172
216173 case SQLITE_UPDATE23: {
216174 int iCol;
216175
216176 if( 0==apVal ){
216177 apVal = (sqlite3_value **)sqlite3_malloc64(sizeof(apVal[0])*nCol*2);
216178 if( 0==apVal ){
216179 rc = SQLITE_NOMEM7;
216180 goto finished_invert;
216181 }
216182 memset(apVal, 0, sizeof(apVal[0])*nCol*2);
216183 }
216184
216185 /* Write the header for the new UPDATE change. Same as the original. */
216186 sessionAppendByte(&sOut, eType, &rc);
216187 sessionAppendByte(&sOut, pInput->aData[pInput->iNext+1], &rc);
216188
216189 /* Read the old.* and new.* records for the update change. */
216190 pInput->iNext += 2;
216191 rc = sessionReadRecord(pInput, nCol, 0, &apVal[0], 0);
216192 if( rc==SQLITE_OK0 ){
216193 rc = sessionReadRecord(pInput, nCol, 0, &apVal[nCol], 0);
216194 }
216195
216196 /* Write the new old.* record. Consists of the PK columns from the
216197 ** original old.* record, and the other values from the original
216198 ** new.* record. */
216199 for(iCol=0; iCol<nCol; iCol++){
216200 sqlite3_value *pVal = apVal[iCol + (abPK[iCol] ? 0 : nCol)];
216201 sessionAppendValue(&sOut, pVal, &rc);
216202 }
216203
216204 /* Write the new new.* record. Consists of a copy of all values
216205 ** from the original old.* record, except for the PK columns, which
216206 ** are set to "undefined". */
216207 for(iCol=0; iCol<nCol; iCol++){
216208 sqlite3_value *pVal = (abPK[iCol] ? 0 : apVal[iCol]);
216209 sessionAppendValue(&sOut, pVal, &rc);
216210 }
216211
216212 for(iCol=0; iCol<nCol*2; iCol++){
216213 sqlite3ValueFree(apVal[iCol]);
216214 }
216215 memset(apVal, 0, sizeof(apVal[0])*nCol*2);
216216 if( rc!=SQLITE_OK0 ){
216217 goto finished_invert;
216218 }
216219
216220 break;
216221 }
216222
216223 default:
216224 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(216224);
216225 goto finished_invert;
216226 }
216227
216228 assert( rc==SQLITE_OK )((void) (0));
216229 if( xOutput && sOut.nBuf>=sessions_strm_chunk_size ){
216230 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
216231 sOut.nBuf = 0;
216232 if( rc!=SQLITE_OK0 ) goto finished_invert;
216233 }
216234 }
216235
216236 assert( rc==SQLITE_OK )((void) (0));
216237 if( pnInverted && ALWAYS(ppInverted)(ppInverted) ){
216238 *pnInverted = sOut.nBuf;
216239 *ppInverted = sOut.aBuf;
216240 sOut.aBuf = 0;
216241 }else if( sOut.nBuf>0 && ALWAYS(xOutput!=0)(xOutput!=0) ){
216242 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
216243 }
216244
216245 finished_invert:
216246 sqlite3_free(sOut.aBuf);
216247 sqlite3_free(apVal);
216248 sqlite3_free(sPK.aBuf);
216249 return rc;
216250}
216251
216252
216253/*
216254** Invert a changeset object.
216255*/
216256SQLITE_API int sqlite3changeset_invert(
216257 int nChangeset, /* Number of bytes in input */
216258 const void *pChangeset, /* Input changeset */
216259 int *pnInverted, /* OUT: Number of bytes in output changeset */
216260 void **ppInverted /* OUT: Inverse of pChangeset */
216261){
216262 SessionInput sInput;
216263
216264 /* Set up the input stream */
216265 memset(&sInput, 0, sizeof(SessionInput));
216266 sInput.nData = nChangeset;
216267 sInput.aData = (u8*)pChangeset;
216268
216269 return sessionChangesetInvert(&sInput, 0, 0, pnInverted, ppInverted);
216270}
216271
216272/*
216273** Streaming version of sqlite3changeset_invert().
216274*/
216275SQLITE_API int sqlite3changeset_invert_strm(
216276 int (*xInput)(void *pIn, void *pData, int *pnData),
216277 void *pIn,
216278 int (*xOutput)(void *pOut, const void *pData, int nData),
216279 void *pOut
216280){
216281 SessionInput sInput;
216282 int rc;
216283
216284 /* Set up the input stream */
216285 memset(&sInput, 0, sizeof(SessionInput));
216286 sInput.xInput = xInput;
216287 sInput.pIn = pIn;
216288
216289 rc = sessionChangesetInvert(&sInput, xOutput, pOut, 0, 0);
216290 sqlite3_free(sInput.buf.aBuf);
216291 return rc;
216292}
216293
216294
216295typedef struct SessionUpdate SessionUpdate;
216296struct SessionUpdate {
216297 sqlite3_stmt *pStmt;
216298 u32 *aMask;
216299 SessionUpdate *pNext;
216300};
216301
216302typedef struct SessionApplyCtx SessionApplyCtx;
216303struct SessionApplyCtx {
216304 sqlite3 *db;
216305 sqlite3_stmt *pDelete; /* DELETE statement */
216306 sqlite3_stmt *pInsert; /* INSERT statement */
216307 sqlite3_stmt *pSelect; /* SELECT statement */
216308 int nCol; /* Size of azCol[] and abPK[] arrays */
216309 const char **azCol; /* Array of column names */
216310 u8 *abPK; /* Boolean array - true if column is in PK */
216311 u32 *aUpdateMask; /* Used by sessionUpdateFind */
216312 SessionUpdate *pUp;
216313 int bStat1; /* True if table is sqlite_stat1 */
216314 int bDeferConstraints; /* True to defer constraints */
216315 int bInvertConstraints; /* Invert when iterating constraints buffer */
216316 SessionBuffer constraints; /* Deferred constraints are stored here */
216317 SessionBuffer rebase; /* Rebase information (if any) here */
216318 u8 bRebaseStarted; /* If table header is already in rebase */
216319 u8 bRebase; /* True to collect rebase information */
216320};
216321
216322/* Number of prepared UPDATE statements to cache. */
216323#define SESSION_UPDATE_CACHE_SZ12 12
216324
216325/*
216326** Find a prepared UPDATE statement suitable for the UPDATE step currently
216327** being visited by the iterator. The UPDATE is of the form:
216328**
216329** UPDATE tbl SET col = ?, col2 = ? WHERE pk1 IS ? AND pk2 IS ?
216330*/
216331static int sessionUpdateFind(
216332 sqlite3_changeset_iter *pIter,
216333 SessionApplyCtx *p,
216334 int bPatchset,
216335 sqlite3_stmt **ppStmt
216336){
216337 int rc = SQLITE_OK0;
216338 SessionUpdate *pUp = 0;
216339 int nCol = pIter->nCol;
216340 int nU32 = (pIter->nCol+33)/32;
216341 int ii;
216342
216343 if( p->aUpdateMask==0 ){
216344 p->aUpdateMask = sqlite3_malloc(nU32*sizeof(u32));
216345 if( p->aUpdateMask==0 ){
216346 rc = SQLITE_NOMEM7;
216347 }
216348 }
216349
216350 if( rc==SQLITE_OK0 ){
216351 memset(p->aUpdateMask, 0, nU32*sizeof(u32));
216352 rc = SQLITE_CORRUPT11;
216353 for(ii=0; ii<pIter->nCol; ii++){
216354 if( sessionChangesetNew(pIter, ii)(pIter)->apValue[(pIter)->nCol+(ii)] ){
216355 p->aUpdateMask[ii/32] |= (1<<(ii%32));
216356 rc = SQLITE_OK0;
216357 }
216358 }
216359 }
216360
216361 if( rc==SQLITE_OK0 ){
216362 if( bPatchset ) p->aUpdateMask[nCol/32] |= (1<<(nCol%32));
216363
216364 if( p->pUp ){
216365 int nUp = 0;
216366 SessionUpdate **pp = &p->pUp;
216367 while( 1 ){
216368 nUp++;
216369 if( 0==memcmp(p->aUpdateMask, (*pp)->aMask, nU32*sizeof(u32)) ){
216370 pUp = *pp;
216371 *pp = pUp->pNext;
216372 pUp->pNext = p->pUp;
216373 p->pUp = pUp;
216374 break;
216375 }
216376
216377 if( (*pp)->pNext ){
216378 pp = &(*pp)->pNext;
216379 }else{
216380 if( nUp>=SESSION_UPDATE_CACHE_SZ12 ){
216381 sqlite3_finalize((*pp)->pStmt);
216382 sqlite3_free(*pp);
216383 *pp = 0;
216384 }
216385 break;
216386 }
216387 }
216388 }
216389
216390 if( pUp==0 ){
216391 int nByte = sizeof(SessionUpdate) * nU32*sizeof(u32);
216392 int bStat1 = (sqlite3_stricmp(pIter->zTab, "sqlite_stat1")==0);
216393 pUp = (SessionUpdate*)sqlite3_malloc(nByte);
216394 if( pUp==0 ){
216395 rc = SQLITE_NOMEM7;
216396 }else{
216397 const char *zSep = "";
216398 SessionBuffer buf;
216399
216400 memset(&buf, 0, sizeof(buf));
216401 pUp->aMask = (u32*)&pUp[1];
216402 memcpy(pUp->aMask, p->aUpdateMask, nU32*sizeof(u32));
216403
216404 sessionAppendStr(&buf, "UPDATE main.", &rc);
216405 sessionAppendIdent(&buf, pIter->zTab, &rc);
216406 sessionAppendStr(&buf, " SET ", &rc);
216407
216408 /* Create the assignments part of the UPDATE */
216409 for(ii=0; ii<pIter->nCol; ii++){
216410 if( p->abPK[ii]==0 && sessionChangesetNew(pIter, ii)(pIter)->apValue[(pIter)->nCol+(ii)] ){
216411 sessionAppendStr(&buf, zSep, &rc);
216412 sessionAppendIdent(&buf, p->azCol[ii], &rc);
216413 sessionAppendStr(&buf, " = ?", &rc);
216414 sessionAppendInteger(&buf, ii*2+1, &rc);
216415 zSep = ", ";
216416 }
216417 }
216418
216419 /* Create the WHERE clause part of the UPDATE */
216420 zSep = "";
216421 sessionAppendStr(&buf, " WHERE ", &rc);
216422 for(ii=0; ii<pIter->nCol; ii++){
216423 if( p->abPK[ii] || (bPatchset==0 && sessionChangesetOld(pIter, ii)(pIter)->apValue[(ii)]) ){
216424 sessionAppendStr(&buf, zSep, &rc);
216425 if( bStat1 && ii==1 ){
216426 assert( sqlite3_stricmp(p->azCol[ii], "idx")==0 )((void) (0));
216427 sessionAppendStr(&buf,
216428 "idx IS CASE "
216429 "WHEN length(?4)=0 AND typeof(?4)='blob' THEN NULL "
216430 "ELSE ?4 END ", &rc
216431 );
216432 }else{
216433 sessionAppendIdent(&buf, p->azCol[ii], &rc);
216434 sessionAppendStr(&buf, " IS ?", &rc);
216435 sessionAppendInteger(&buf, ii*2+2, &rc);
216436 }
216437 zSep = " AND ";
216438 }
216439 }
216440
216441 if( rc==SQLITE_OK0 ){
216442 char *zSql = (char*)buf.aBuf;
216443 rc = sqlite3_prepare_v2(p->db, zSql, buf.nBuf, &pUp->pStmt, 0);
216444 }
216445
216446 if( rc!=SQLITE_OK0 ){
216447 sqlite3_free(pUp);
216448 pUp = 0;
216449 }else{
216450 pUp->pNext = p->pUp;
216451 p->pUp = pUp;
216452 }
216453 sqlite3_free(buf.aBuf);
216454 }
216455 }
216456 }
216457
216458 assert( (rc==SQLITE_OK)==(pUp!=0) )((void) (0));
216459 if( pUp ){
216460 *ppStmt = pUp->pStmt;
216461 }else{
216462 *ppStmt = 0;
216463 }
216464 return rc;
216465}
216466
216467/*
216468** Free all cached UPDATE statements.
216469*/
216470static void sessionUpdateFree(SessionApplyCtx *p){
216471 SessionUpdate *pUp;
216472 SessionUpdate *pNext;
216473 for(pUp=p->pUp; pUp; pUp=pNext){
216474 pNext = pUp->pNext;
216475 sqlite3_finalize(pUp->pStmt);
216476 sqlite3_free(pUp);
216477 }
216478 p->pUp = 0;
216479 sqlite3_free(p->aUpdateMask);
216480 p->aUpdateMask = 0;
216481}
216482
216483/*
216484** Formulate a statement to DELETE a row from database db. Assuming a table
216485** structure like this:
216486**
216487** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
216488**
216489** The DELETE statement looks like this:
216490**
216491** DELETE FROM x WHERE a = :1 AND c = :3 AND (:5 OR b IS :2 AND d IS :4)
216492**
216493** Variable :5 (nCol+1) is a boolean. It should be set to 0 if we require
216494** matching b and d values, or 1 otherwise. The second case comes up if the
216495** conflict handler is invoked with NOTFOUND and returns CHANGESET_REPLACE.
216496**
216497** If successful, SQLITE_OK is returned and SessionApplyCtx.pDelete is left
216498** pointing to the prepared version of the SQL statement.
216499*/
216500static int sessionDeleteRow(
216501 sqlite3 *db, /* Database handle */
216502 const char *zTab, /* Table name */
216503 SessionApplyCtx *p /* Session changeset-apply context */
216504){
216505 int i;
216506 const char *zSep = "";
216507 int rc = SQLITE_OK0;
216508 SessionBuffer buf = {0, 0, 0};
216509 int nPk = 0;
216510
216511 sessionAppendStr(&buf, "DELETE FROM main.", &rc);
216512 sessionAppendIdent(&buf, zTab, &rc);
216513 sessionAppendStr(&buf, " WHERE ", &rc);
216514
216515 for(i=0; i<p->nCol; i++){
216516 if( p->abPK[i] ){
216517 nPk++;
216518 sessionAppendStr(&buf, zSep, &rc);
216519 sessionAppendIdent(&buf, p->azCol[i], &rc);
216520 sessionAppendStr(&buf, " = ?", &rc);
216521 sessionAppendInteger(&buf, i+1, &rc);
216522 zSep = " AND ";
216523 }
216524 }
216525
216526 if( nPk<p->nCol ){
216527 sessionAppendStr(&buf, " AND (?", &rc);
216528 sessionAppendInteger(&buf, p->nCol+1, &rc);
216529 sessionAppendStr(&buf, " OR ", &rc);
216530
216531 zSep = "";
216532 for(i=0; i<p->nCol; i++){
216533 if( !p->abPK[i] ){
216534 sessionAppendStr(&buf, zSep, &rc);
216535 sessionAppendIdent(&buf, p->azCol[i], &rc);
216536 sessionAppendStr(&buf, " IS ?", &rc);
216537 sessionAppendInteger(&buf, i+1, &rc);
216538 zSep = "AND ";
216539 }
216540 }
216541 sessionAppendStr(&buf, ")", &rc);
216542 }
216543
216544 if( rc==SQLITE_OK0 ){
216545 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pDelete, 0);
216546 }
216547 sqlite3_free(buf.aBuf);
216548
216549 return rc;
216550}
216551
216552/*
216553** Formulate and prepare an SQL statement to query table zTab by primary
216554** key. Assuming the following table structure:
216555**
216556** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
216557**
216558** The SELECT statement looks like this:
216559**
216560** SELECT * FROM x WHERE a = ?1 AND c = ?3
216561**
216562** If successful, SQLITE_OK is returned and SessionApplyCtx.pSelect is left
216563** pointing to the prepared version of the SQL statement.
216564*/
216565static int sessionSelectRow(
216566 sqlite3 *db, /* Database handle */
216567 const char *zTab, /* Table name */
216568 SessionApplyCtx *p /* Session changeset-apply context */
216569){
216570 return sessionSelectStmt(
216571 db, "main", zTab, p->nCol, p->azCol, p->abPK, &p->pSelect);
216572}
216573
216574/*
216575** Formulate and prepare an INSERT statement to add a record to table zTab.
216576** For example:
216577**
216578** INSERT INTO main."zTab" VALUES(?1, ?2, ?3 ...);
216579**
216580** If successful, SQLITE_OK is returned and SessionApplyCtx.pInsert is left
216581** pointing to the prepared version of the SQL statement.
216582*/
216583static int sessionInsertRow(
216584 sqlite3 *db, /* Database handle */
216585 const char *zTab, /* Table name */
216586 SessionApplyCtx *p /* Session changeset-apply context */
216587){
216588 int rc = SQLITE_OK0;
216589 int i;
216590 SessionBuffer buf = {0, 0, 0};
216591
216592 sessionAppendStr(&buf, "INSERT INTO main.", &rc);
216593 sessionAppendIdent(&buf, zTab, &rc);
216594 sessionAppendStr(&buf, "(", &rc);
216595 for(i=0; i<p->nCol; i++){
216596 if( i!=0 ) sessionAppendStr(&buf, ", ", &rc);
216597 sessionAppendIdent(&buf, p->azCol[i], &rc);
216598 }
216599
216600 sessionAppendStr(&buf, ") VALUES(?", &rc);
216601 for(i=1; i<p->nCol; i++){
216602 sessionAppendStr(&buf, ", ?", &rc);
216603 }
216604 sessionAppendStr(&buf, ")", &rc);
216605
216606 if( rc==SQLITE_OK0 ){
216607 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pInsert, 0);
216608 }
216609 sqlite3_free(buf.aBuf);
216610 return rc;
216611}
216612
216613static int sessionPrepare(sqlite3 *db, sqlite3_stmt **pp, const char *zSql){
216614 return sqlite3_prepare_v2(db, zSql, -1, pp, 0);
216615}
216616
216617/*
216618** Prepare statements for applying changes to the sqlite_stat1 table.
216619** These are similar to those created by sessionSelectRow(),
216620** sessionInsertRow(), sessionUpdateRow() and sessionDeleteRow() for
216621** other tables.
216622*/
216623static int sessionStat1Sql(sqlite3 *db, SessionApplyCtx *p){
216624 int rc = sessionSelectRow(db, "sqlite_stat1", p);
216625 if( rc==SQLITE_OK0 ){
216626 rc = sessionPrepare(db, &p->pInsert,
216627 "INSERT INTO main.sqlite_stat1 VALUES(?1, "
216628 "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END, "
216629 "?3)"
216630 );
216631 }
216632 if( rc==SQLITE_OK0 ){
216633 rc = sessionPrepare(db, &p->pDelete,
216634 "DELETE FROM main.sqlite_stat1 WHERE tbl=?1 AND idx IS "
216635 "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END "
216636 "AND (?4 OR stat IS ?3)"
216637 );
216638 }
216639 return rc;
216640}
216641
216642/*
216643** A wrapper around sqlite3_bind_value() that detects an extra problem.
216644** See comments in the body of this function for details.
216645*/
216646static int sessionBindValue(
216647 sqlite3_stmt *pStmt, /* Statement to bind value to */
216648 int i, /* Parameter number to bind to */
216649 sqlite3_value *pVal /* Value to bind */
216650){
216651 int eType = sqlite3_value_type(pVal);
216652 /* COVERAGE: The (pVal->z==0) branch is never true using current versions
216653 ** of SQLite. If a malloc fails in an sqlite3_value_xxx() function, either
216654 ** the (pVal->z) variable remains as it was or the type of the value is
216655 ** set to SQLITE_NULL. */
216656 if( (eType==SQLITE_TEXT3 || eType==SQLITE_BLOB4) && pVal->z==0 ){
216657 /* This condition occurs when an earlier OOM in a call to
216658 ** sqlite3_value_text() or sqlite3_value_blob() (perhaps from within
216659 ** a conflict-handler) has zeroed the pVal->z pointer. Return NOMEM. */
216660 return SQLITE_NOMEM7;
216661 }
216662 return sqlite3_bind_value(pStmt, i, pVal);
216663}
216664
216665/*
216666** Iterator pIter must point to an SQLITE_INSERT entry. This function
216667** transfers new.* values from the current iterator entry to statement
216668** pStmt. The table being inserted into has nCol columns.
216669**
216670** New.* value $i from the iterator is bound to variable ($i+1) of
216671** statement pStmt. If parameter abPK is NULL, all values from 0 to (nCol-1)
216672** are transfered to the statement. Otherwise, if abPK is not NULL, it points
216673** to an array nCol elements in size. In this case only those values for
216674** which abPK[$i] is true are read from the iterator and bound to the
216675** statement.
216676**
216677** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK.
216678*/
216679static int sessionBindRow(
216680 sqlite3_changeset_iter *pIter, /* Iterator to read values from */
216681 int(*xValue)(sqlite3_changeset_iter *, int, sqlite3_value **),
216682 int nCol, /* Number of columns */
216683 u8 *abPK, /* If not NULL, bind only if true */
216684 sqlite3_stmt *pStmt /* Bind values to this statement */
216685){
216686 int i;
216687 int rc = SQLITE_OK0;
216688
216689 /* Neither sqlite3changeset_old or sqlite3changeset_new can fail if the
216690 ** argument iterator points to a suitable entry. Make sure that xValue
216691 ** is one of these to guarantee that it is safe to ignore the return
216692 ** in the code below. */
216693 assert( xValue==sqlite3changeset_old || xValue==sqlite3changeset_new )((void) (0));
216694
216695 for(i=0; rc==SQLITE_OK0 && i<nCol; i++){
216696 if( !abPK || abPK[i] ){
216697 sqlite3_value *pVal = 0;
216698 (void)xValue(pIter, i, &pVal);
216699 if( pVal==0 ){
216700 /* The value in the changeset was "undefined". This indicates a
216701 ** corrupt changeset blob. */
216702 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(216702);
216703 }else{
216704 rc = sessionBindValue(pStmt, i+1, pVal);
216705 }
216706 }
216707 }
216708 return rc;
216709}
216710
216711/*
216712** SQL statement pSelect is as generated by the sessionSelectRow() function.
216713** This function binds the primary key values from the change that changeset
216714** iterator pIter points to to the SELECT and attempts to seek to the table
216715** entry. If a row is found, the SELECT statement left pointing at the row
216716** and SQLITE_ROW is returned. Otherwise, if no row is found and no error
216717** has occured, the statement is reset and SQLITE_OK is returned. If an
216718** error occurs, the statement is reset and an SQLite error code is returned.
216719**
216720** If this function returns SQLITE_ROW, the caller must eventually reset()
216721** statement pSelect. If any other value is returned, the statement does
216722** not require a reset().
216723**
216724** If the iterator currently points to an INSERT record, bind values from the
216725** new.* record to the SELECT statement. Or, if it points to a DELETE or
216726** UPDATE, bind values from the old.* record.
216727*/
216728static int sessionSeekToRow(
216729 sqlite3 *db, /* Database handle */
216730 sqlite3_changeset_iter *pIter, /* Changeset iterator */
216731 u8 *abPK, /* Primary key flags array */
216732 sqlite3_stmt *pSelect /* SELECT statement from sessionSelectRow() */
216733){
216734 int rc; /* Return code */
216735 int nCol; /* Number of columns in table */
216736 int op; /* Changset operation (SQLITE_UPDATE etc.) */
216737 const char *zDummy; /* Unused */
216738
216739 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
216740 rc = sessionBindRow(pIter,
216741 op==SQLITE_INSERT18 ? sqlite3changeset_new : sqlite3changeset_old,
216742 nCol, abPK, pSelect
216743 );
216744
216745 if( rc==SQLITE_OK0 ){
216746 rc = sqlite3_step(pSelect);
216747 if( rc!=SQLITE_ROW100 ) rc = sqlite3_reset(pSelect);
216748 }
216749
216750 return rc;
216751}
216752
216753/*
216754** This function is called from within sqlite3changeset_apply_v2() when
216755** a conflict is encountered and resolved using conflict resolution
216756** mode eType (either SQLITE_CHANGESET_OMIT or SQLITE_CHANGESET_REPLACE)..
216757** It adds a conflict resolution record to the buffer in
216758** SessionApplyCtx.rebase, which will eventually be returned to the caller
216759** of apply_v2() as the "rebase" buffer.
216760**
216761** Return SQLITE_OK if successful, or an SQLite error code otherwise.
216762*/
216763static int sessionRebaseAdd(
216764 SessionApplyCtx *p, /* Apply context */
216765 int eType, /* Conflict resolution (OMIT or REPLACE) */
216766 sqlite3_changeset_iter *pIter /* Iterator pointing at current change */
216767){
216768 int rc = SQLITE_OK0;
216769 if( p->bRebase ){
216770 int i;
216771 int eOp = pIter->op;
216772 if( p->bRebaseStarted==0 ){
216773 /* Append a table-header to the rebase buffer */
216774 const char *zTab = pIter->zTab;
216775 sessionAppendByte(&p->rebase, 'T', &rc);
216776 sessionAppendVarint(&p->rebase, p->nCol, &rc);
216777 sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc);
216778 sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc);
216779 p->bRebaseStarted = 1;
216780 }
216781
216782 assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT )((void) (0));
216783 assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE )((void) (0));
216784
216785 sessionAppendByte(&p->rebase,
216786 (eOp==SQLITE_DELETE9 ? SQLITE_DELETE9 : SQLITE_INSERT18), &rc
216787 );
216788 sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE1), &rc);
216789 for(i=0; i<p->nCol; i++){
216790 sqlite3_value *pVal = 0;
216791 if( eOp==SQLITE_DELETE9 || (eOp==SQLITE_UPDATE23 && p->abPK[i]) ){
216792 sqlite3changeset_old(pIter, i, &pVal);
216793 }else{
216794 sqlite3changeset_new(pIter, i, &pVal);
216795 }
216796 sessionAppendValue(&p->rebase, pVal, &rc);
216797 }
216798 }
216799 return rc;
216800}
216801
216802/*
216803** Invoke the conflict handler for the change that the changeset iterator
216804** currently points to.
216805**
216806** Argument eType must be either CHANGESET_DATA or CHANGESET_CONFLICT.
216807** If argument pbReplace is NULL, then the type of conflict handler invoked
216808** depends solely on eType, as follows:
216809**
216810** eType value Value passed to xConflict
216811** -------------------------------------------------
216812** CHANGESET_DATA CHANGESET_NOTFOUND
216813** CHANGESET_CONFLICT CHANGESET_CONSTRAINT
216814**
216815** Or, if pbReplace is not NULL, then an attempt is made to find an existing
216816** record with the same primary key as the record about to be deleted, updated
216817** or inserted. If such a record can be found, it is available to the conflict
216818** handler as the "conflicting" record. In this case the type of conflict
216819** handler invoked is as follows:
216820**
216821** eType value PK Record found? Value passed to xConflict
216822** ----------------------------------------------------------------
216823** CHANGESET_DATA Yes CHANGESET_DATA
216824** CHANGESET_DATA No CHANGESET_NOTFOUND
216825** CHANGESET_CONFLICT Yes CHANGESET_CONFLICT
216826** CHANGESET_CONFLICT No CHANGESET_CONSTRAINT
216827**
216828** If pbReplace is not NULL, and a record with a matching PK is found, and
216829** the conflict handler function returns SQLITE_CHANGESET_REPLACE, *pbReplace
216830** is set to non-zero before returning SQLITE_OK.
216831**
216832** If the conflict handler returns SQLITE_CHANGESET_ABORT, SQLITE_ABORT is
216833** returned. Or, if the conflict handler returns an invalid value,
216834** SQLITE_MISUSE. If the conflict handler returns SQLITE_CHANGESET_OMIT,
216835** this function returns SQLITE_OK.
216836*/
216837static int sessionConflictHandler(
216838 int eType, /* Either CHANGESET_DATA or CONFLICT */
216839 SessionApplyCtx *p, /* changeset_apply() context */
216840 sqlite3_changeset_iter *pIter, /* Changeset iterator */
216841 int(*xConflict)(void *, int, sqlite3_changeset_iter*),
216842 void *pCtx, /* First argument for conflict handler */
216843 int *pbReplace /* OUT: Set to true if PK row is found */
216844){
216845 int res = 0; /* Value returned by conflict handler */
216846 int rc;
216847 int nCol;
216848 int op;
216849 const char *zDummy;
216850
216851 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
216852
216853 assert( eType==SQLITE_CHANGESET_CONFLICT || eType==SQLITE_CHANGESET_DATA )((void) (0));
216854 assert( SQLITE_CHANGESET_CONFLICT+1==SQLITE_CHANGESET_CONSTRAINT )((void) (0));
216855 assert( SQLITE_CHANGESET_DATA+1==SQLITE_CHANGESET_NOTFOUND )((void) (0));
216856
216857 /* Bind the new.* PRIMARY KEY values to the SELECT statement. */
216858 if( pbReplace ){
216859 rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect);
216860 }else{
216861 rc = SQLITE_OK0;
216862 }
216863
216864 if( rc==SQLITE_ROW100 ){
216865 /* There exists another row with the new.* primary key. */
216866 pIter->pConflict = p->pSelect;
216867 res = xConflict(pCtx, eType, pIter);
216868 pIter->pConflict = 0;
216869 rc = sqlite3_reset(p->pSelect);
216870 }else if( rc==SQLITE_OK0 ){
216871 if( p->bDeferConstraints && eType==SQLITE_CHANGESET_CONFLICT3 ){
216872 /* Instead of invoking the conflict handler, append the change blob
216873 ** to the SessionApplyCtx.constraints buffer. */
216874 u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent];
216875 int nBlob = pIter->in.iNext - pIter->in.iCurrent;
216876 sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc);
216877 return SQLITE_OK0;
216878 }else{
216879 /* No other row with the new.* primary key. */
216880 res = xConflict(pCtx, eType+1, pIter);
216881 if( res==SQLITE_CHANGESET_REPLACE1 ) rc = SQLITE_MISUSE21;
216882 }
216883 }
216884
216885 if( rc==SQLITE_OK0 ){
216886 switch( res ){
216887 case SQLITE_CHANGESET_REPLACE1:
216888 assert( pbReplace )((void) (0));
216889 *pbReplace = 1;
216890 break;
216891
216892 case SQLITE_CHANGESET_OMIT0:
216893 break;
216894
216895 case SQLITE_CHANGESET_ABORT2:
216896 rc = SQLITE_ABORT4;
216897 break;
216898
216899 default:
216900 rc = SQLITE_MISUSE21;
216901 break;
216902 }
216903 if( rc==SQLITE_OK0 ){
216904 rc = sessionRebaseAdd(p, res, pIter);
216905 }
216906 }
216907
216908 return rc;
216909}
216910
216911/*
216912** Attempt to apply the change that the iterator passed as the first argument
216913** currently points to to the database. If a conflict is encountered, invoke
216914** the conflict handler callback.
216915**
216916** If argument pbRetry is NULL, then ignore any CHANGESET_DATA conflict. If
216917** one is encountered, update or delete the row with the matching primary key
216918** instead. Or, if pbRetry is not NULL and a CHANGESET_DATA conflict occurs,
216919** invoke the conflict handler. If it returns CHANGESET_REPLACE, set *pbRetry
216920** to true before returning. In this case the caller will invoke this function
216921** again, this time with pbRetry set to NULL.
216922**
216923** If argument pbReplace is NULL and a CHANGESET_CONFLICT conflict is
216924** encountered invoke the conflict handler with CHANGESET_CONSTRAINT instead.
216925** Or, if pbReplace is not NULL, invoke it with CHANGESET_CONFLICT. If such
216926** an invocation returns SQLITE_CHANGESET_REPLACE, set *pbReplace to true
216927** before retrying. In this case the caller attempts to remove the conflicting
216928** row before invoking this function again, this time with pbReplace set
216929** to NULL.
216930**
216931** If any conflict handler returns SQLITE_CHANGESET_ABORT, this function
216932** returns SQLITE_ABORT. Otherwise, if no error occurs, SQLITE_OK is
216933** returned.
216934*/
216935static int sessionApplyOneOp(
216936 sqlite3_changeset_iter *pIter, /* Changeset iterator */
216937 SessionApplyCtx *p, /* changeset_apply() context */
216938 int(*xConflict)(void *, int, sqlite3_changeset_iter *),
216939 void *pCtx, /* First argument for the conflict handler */
216940 int *pbReplace, /* OUT: True to remove PK row and retry */
216941 int *pbRetry /* OUT: True to retry. */
216942){
216943 const char *zDummy;
216944 int op;
216945 int nCol;
216946 int rc = SQLITE_OK0;
216947
216948 assert( p->pDelete && p->pInsert && p->pSelect )((void) (0));
216949 assert( p->azCol && p->abPK )((void) (0));
216950 assert( !pbReplace || *pbReplace==0 )((void) (0));
216951
216952 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
216953
216954 if( op==SQLITE_DELETE9 ){
216955
216956 /* Bind values to the DELETE statement. If conflict handling is required,
216957 ** bind values for all columns and set bound variable (nCol+1) to true.
216958 ** Or, if conflict handling is not required, bind just the PK column
216959 ** values and, if it exists, set (nCol+1) to false. Conflict handling
216960 ** is not required if:
216961 **
216962 ** * this is a patchset, or
216963 ** * (pbRetry==0), or
216964 ** * all columns of the table are PK columns (in this case there is
216965 ** no (nCol+1) variable to bind to).
216966 */
216967 u8 *abPK = (pIter->bPatchset ? p->abPK : 0);
216968 rc = sessionBindRow(pIter, sqlite3changeset_old, nCol, abPK, p->pDelete);
216969 if( rc==SQLITE_OK0 && sqlite3_bind_parameter_count(p->pDelete)>nCol ){
216970 rc = sqlite3_bind_int(p->pDelete, nCol+1, (pbRetry==0 || abPK));
216971 }
216972 if( rc!=SQLITE_OK0 ) return rc;
216973
216974 sqlite3_step(p->pDelete);
216975 rc = sqlite3_reset(p->pDelete);
216976 if( rc==SQLITE_OK0 && sqlite3_changes(p->db)==0 ){
216977 rc = sessionConflictHandler(
216978 SQLITE_CHANGESET_DATA1, p, pIter, xConflict, pCtx, pbRetry
216979 );
216980 }else if( (rc&0xff)==SQLITE_CONSTRAINT19 ){
216981 rc = sessionConflictHandler(
216982 SQLITE_CHANGESET_CONFLICT3, p, pIter, xConflict, pCtx, 0
216983 );
216984 }
216985
216986 }else if( op==SQLITE_UPDATE23 ){
216987 int i;
216988 sqlite3_stmt *pUp = 0;
216989 int bPatchset = (pbRetry==0 || pIter->bPatchset);
216990
216991 rc = sessionUpdateFind(pIter, p, bPatchset, &pUp);
216992
216993 /* Bind values to the UPDATE statement. */
216994 for(i=0; rc==SQLITE_OK0 && i<nCol; i++){
216995 sqlite3_value *pOld = sessionChangesetOld(pIter, i)(pIter)->apValue[(i)];
216996 sqlite3_value *pNew = sessionChangesetNew(pIter, i)(pIter)->apValue[(pIter)->nCol+(i)];
216997 if( p->abPK[i] || (bPatchset==0 && pOld) ){
216998 rc = sessionBindValue(pUp, i*2+2, pOld);
216999 }
217000 if( rc==SQLITE_OK0 && pNew ){
217001 rc = sessionBindValue(pUp, i*2+1, pNew);
217002 }
217003 }
217004 if( rc!=SQLITE_OK0 ) return rc;
217005
217006 /* Attempt the UPDATE. In the case of a NOTFOUND or DATA conflict,
217007 ** the result will be SQLITE_OK with 0 rows modified. */
217008 sqlite3_step(pUp);
217009 rc = sqlite3_reset(pUp);
217010
217011 if( rc==SQLITE_OK0 && sqlite3_changes(p->db)==0 ){
217012 /* A NOTFOUND or DATA error. Search the table to see if it contains
217013 ** a row with a matching primary key. If so, this is a DATA conflict.
217014 ** Otherwise, if there is no primary key match, it is a NOTFOUND. */
217015
217016 rc = sessionConflictHandler(
217017 SQLITE_CHANGESET_DATA1, p, pIter, xConflict, pCtx, pbRetry
217018 );
217019
217020 }else if( (rc&0xff)==SQLITE_CONSTRAINT19 ){
217021 /* This is always a CONSTRAINT conflict. */
217022 rc = sessionConflictHandler(
217023 SQLITE_CHANGESET_CONFLICT3, p, pIter, xConflict, pCtx, 0
217024 );
217025 }
217026
217027 }else{
217028 assert( op==SQLITE_INSERT )((void) (0));
217029 if( p->bStat1 ){
217030 /* Check if there is a conflicting row. For sqlite_stat1, this needs
217031 ** to be done using a SELECT, as there is no PRIMARY KEY in the
217032 ** database schema to throw an exception if a duplicate is inserted. */
217033 rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect);
217034 if( rc==SQLITE_ROW100 ){
217035 rc = SQLITE_CONSTRAINT19;
217036 sqlite3_reset(p->pSelect);
217037 }
217038 }
217039
217040 if( rc==SQLITE_OK0 ){
217041 rc = sessionBindRow(pIter, sqlite3changeset_new, nCol, 0, p->pInsert);
217042 if( rc!=SQLITE_OK0 ) return rc;
217043
217044 sqlite3_step(p->pInsert);
217045 rc = sqlite3_reset(p->pInsert);
217046 }
217047
217048 if( (rc&0xff)==SQLITE_CONSTRAINT19 ){
217049 rc = sessionConflictHandler(
217050 SQLITE_CHANGESET_CONFLICT3, p, pIter, xConflict, pCtx, pbReplace
217051 );
217052 }
217053 }
217054
217055 return rc;
217056}
217057
217058/*
217059** Attempt to apply the change that the iterator passed as the first argument
217060** currently points to to the database. If a conflict is encountered, invoke
217061** the conflict handler callback.
217062**
217063** The difference between this function and sessionApplyOne() is that this
217064** function handles the case where the conflict-handler is invoked and
217065** returns SQLITE_CHANGESET_REPLACE - indicating that the change should be
217066** retried in some manner.
217067*/
217068static int sessionApplyOneWithRetry(
217069 sqlite3 *db, /* Apply change to "main" db of this handle */
217070 sqlite3_changeset_iter *pIter, /* Changeset iterator to read change from */
217071 SessionApplyCtx *pApply, /* Apply context */
217072 int(*xConflict)(void*, int, sqlite3_changeset_iter*),
217073 void *pCtx /* First argument passed to xConflict */
217074){
217075 int bReplace = 0;
217076 int bRetry = 0;
217077 int rc;
217078
217079 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, &bReplace, &bRetry);
217080 if( rc==SQLITE_OK0 ){
217081 /* If the bRetry flag is set, the change has not been applied due to an
217082 ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and
217083 ** a row with the correct PK is present in the db, but one or more other
217084 ** fields do not contain the expected values) and the conflict handler
217085 ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation,
217086 ** but pass NULL as the final argument so that sessionApplyOneOp() ignores
217087 ** the SQLITE_CHANGESET_DATA problem. */
217088 if( bRetry ){
217089 assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE )((void) (0));
217090 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
217091 }
217092
217093 /* If the bReplace flag is set, the change is an INSERT that has not
217094 ** been performed because the database already contains a row with the
217095 ** specified primary key and the conflict handler returned
217096 ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row
217097 ** before reattempting the INSERT. */
217098 else if( bReplace ){
217099 assert( pIter->op==SQLITE_INSERT )((void) (0));
217100 rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
217101 if( rc==SQLITE_OK0 ){
217102 rc = sessionBindRow(pIter,
217103 sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
217104 sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
217105 }
217106 if( rc==SQLITE_OK0 ){
217107 sqlite3_step(pApply->pDelete);
217108 rc = sqlite3_reset(pApply->pDelete);
217109 }
217110 if( rc==SQLITE_OK0 ){
217111 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
217112 }
217113 if( rc==SQLITE_OK0 ){
217114 rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
217115 }
217116 }
217117 }
217118
217119 return rc;
217120}
217121
217122/*
217123** Retry the changes accumulated in the pApply->constraints buffer.
217124*/
217125static int sessionRetryConstraints(
217126 sqlite3 *db,
217127 int bPatchset,
217128 const char *zTab,
217129 SessionApplyCtx *pApply,
217130 int(*xConflict)(void*, int, sqlite3_changeset_iter*),
217131 void *pCtx /* First argument passed to xConflict */
217132){
217133 int rc = SQLITE_OK0;
217134
217135 while( pApply->constraints.nBuf ){
217136 sqlite3_changeset_iter *pIter2 = 0;
217137 SessionBuffer cons = pApply->constraints;
217138 memset(&pApply->constraints, 0, sizeof(SessionBuffer));
217139
217140 rc = sessionChangesetStart(
217141 &pIter2, 0, 0, cons.nBuf, cons.aBuf, pApply->bInvertConstraints, 1
217142 );
217143 if( rc==SQLITE_OK0 ){
217144 size_t nByte = 2*pApply->nCol*sizeof(sqlite3_value*);
217145 int rc2;
217146 pIter2->bPatchset = bPatchset;
217147 pIter2->zTab = (char*)zTab;
217148 pIter2->nCol = pApply->nCol;
217149 pIter2->abPK = pApply->abPK;
217150 sessionBufferGrow(&pIter2->tblhdr, nByte, &rc);
217151 pIter2->apValue = (sqlite3_value**)pIter2->tblhdr.aBuf;
217152 if( rc==SQLITE_OK0 ) memset(pIter2->apValue, 0, nByte);
217153
217154 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3changeset_next(pIter2) ){
217155 rc = sessionApplyOneWithRetry(db, pIter2, pApply, xConflict, pCtx);
217156 }
217157
217158 rc2 = sqlite3changeset_finalize(pIter2);
217159 if( rc==SQLITE_OK0 ) rc = rc2;
217160 }
217161 assert( pApply->bDeferConstraints || pApply->constraints.nBuf==0 )((void) (0));
217162
217163 sqlite3_free(cons.aBuf);
217164 if( rc!=SQLITE_OK0 ) break;
217165 if( pApply->constraints.nBuf>=cons.nBuf ){
217166 /* No progress was made on the last round. */
217167 pApply->bDeferConstraints = 0;
217168 }
217169 }
217170
217171 return rc;
217172}
217173
217174/*
217175** Argument pIter is a changeset iterator that has been initialized, but
217176** not yet passed to sqlite3changeset_next(). This function applies the
217177** changeset to the main database attached to handle "db". The supplied
217178** conflict handler callback is invoked to resolve any conflicts encountered
217179** while applying the change.
217180*/
217181static int sessionChangesetApply(
217182 sqlite3 *db, /* Apply change to "main" db of this handle */
217183 sqlite3_changeset_iter *pIter, /* Changeset to apply */
217184 int(*xFilter)(
217185 void *pCtx, /* Copy of sixth arg to _apply() */
217186 const char *zTab /* Table name */
217187 ),
217188 int(*xConflict)(
217189 void *pCtx, /* Copy of fifth arg to _apply() */
217190 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
217191 sqlite3_changeset_iter *p /* Handle describing change and conflict */
217192 ),
217193 void *pCtx, /* First argument passed to xConflict */
217194 void **ppRebase, int *pnRebase, /* OUT: Rebase information */
217195 int flags /* SESSION_APPLY_XXX flags */
217196){
217197 int schemaMismatch = 0;
217198 int rc = SQLITE_OK0; /* Return code */
217199 const char *zTab = 0; /* Name of current table */
217200 int nTab = 0; /* Result of sqlite3Strlen30(zTab) */
217201 SessionApplyCtx sApply; /* changeset_apply() context object */
217202 int bPatchset;
217203
217204 assert( xConflict!=0 )((void) (0));
217205
217206 pIter->in.bNoDiscard = 1;
217207 memset(&sApply, 0, sizeof(sApply));
217208 sApply.bRebase = (ppRebase && pnRebase);
217209 sApply.bInvertConstraints = !!(flags & SQLITE_CHANGESETAPPLY_INVERT0x0002);
217210 sqlite3_mutex_enter(sqlite3_db_mutex(db));
217211 if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT0x0001)==0 ){
217212 rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0);
217213 }
217214 if( rc==SQLITE_OK0 ){
217215 rc = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 1", 0, 0, 0);
217216 }
217217 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3changeset_next(pIter) ){
217218 int nCol;
217219 int op;
217220 const char *zNew;
217221
217222 sqlite3changeset_op(pIter, &zNew, &nCol, &op, 0);
217223
217224 if( zTab==0 || sqlite3_strnicmp(zNew, zTab, nTab+1) ){
217225 u8 *abPK;
217226
217227 rc = sessionRetryConstraints(
217228 db, pIter->bPatchset, zTab, &sApply, xConflict, pCtx
217229 );
217230 if( rc!=SQLITE_OK0 ) break;
217231
217232 sessionUpdateFree(&sApply);
217233 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
217234 sqlite3_finalize(sApply.pDelete);
217235 sqlite3_finalize(sApply.pInsert);
217236 sqlite3_finalize(sApply.pSelect);
217237 sApply.db = db;
217238 sApply.pDelete = 0;
217239 sApply.pInsert = 0;
217240 sApply.pSelect = 0;
217241 sApply.nCol = 0;
217242 sApply.azCol = 0;
217243 sApply.abPK = 0;
217244 sApply.bStat1 = 0;
217245 sApply.bDeferConstraints = 1;
217246 sApply.bRebaseStarted = 0;
217247 memset(&sApply.constraints, 0, sizeof(SessionBuffer));
217248
217249 /* If an xFilter() callback was specified, invoke it now. If the
217250 ** xFilter callback returns zero, skip this table. If it returns
217251 ** non-zero, proceed. */
217252 schemaMismatch = (xFilter && (0==xFilter(pCtx, zNew)));
217253 if( schemaMismatch ){
217254 zTab = sqlite3_mprintf("%s", zNew);
217255 if( zTab==0 ){
217256 rc = SQLITE_NOMEM7;
217257 break;
217258 }
217259 nTab = (int)strlen(zTab);
217260 sApply.azCol = (const char **)zTab;
217261 }else{
217262 int nMinCol = 0;
217263 int i;
217264
217265 sqlite3changeset_pk(pIter, &abPK, 0);
217266 rc = sessionTableInfo(0,
217267 db, "main", zNew, &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK
217268 );
217269 if( rc!=SQLITE_OK0 ) break;
217270 for(i=0; i<sApply.nCol; i++){
217271 if( sApply.abPK[i] ) nMinCol = i+1;
217272 }
217273
217274 if( sApply.nCol==0 ){
217275 schemaMismatch = 1;
217276 sqlite3_log(SQLITE_SCHEMA17,
217277 "sqlite3changeset_apply(): no such table: %s", zTab
217278 );
217279 }
217280 else if( sApply.nCol<nCol ){
217281 schemaMismatch = 1;
217282 sqlite3_log(SQLITE_SCHEMA17,
217283 "sqlite3changeset_apply(): table %s has %d columns, "
217284 "expected %d or more",
217285 zTab, sApply.nCol, nCol
217286 );
217287 }
217288 else if( nCol<nMinCol || memcmp(sApply.abPK, abPK, nCol)!=0 ){
217289 schemaMismatch = 1;
217290 sqlite3_log(SQLITE_SCHEMA17, "sqlite3changeset_apply(): "
217291 "primary key mismatch for table %s", zTab
217292 );
217293 }
217294 else{
217295 sApply.nCol = nCol;
217296 if( 0==sqlite3_stricmp(zTab, "sqlite_stat1") ){
217297 if( (rc = sessionStat1Sql(db, &sApply) ) ){
217298 break;
217299 }
217300 sApply.bStat1 = 1;
217301 }else{
217302 if( (rc = sessionSelectRow(db, zTab, &sApply))
217303 || (rc = sessionDeleteRow(db, zTab, &sApply))
217304 || (rc = sessionInsertRow(db, zTab, &sApply))
217305 ){
217306 break;
217307 }
217308 sApply.bStat1 = 0;
217309 }
217310 }
217311 nTab = sqlite3Strlen30(zTab);
217312 }
217313 }
217314
217315 /* If there is a schema mismatch on the current table, proceed to the
217316 ** next change. A log message has already been issued. */
217317 if( schemaMismatch ) continue;
217318
217319 rc = sessionApplyOneWithRetry(db, pIter, &sApply, xConflict, pCtx);
217320 }
217321
217322 bPatchset = pIter->bPatchset;
217323 if( rc==SQLITE_OK0 ){
217324 rc = sqlite3changeset_finalize(pIter);
217325 }else{
217326 sqlite3changeset_finalize(pIter);
217327 }
217328
217329 if( rc==SQLITE_OK0 ){
217330 rc = sessionRetryConstraints(db, bPatchset, zTab, &sApply, xConflict, pCtx);
217331 }
217332
217333 if( rc==SQLITE_OK0 ){
217334 int nFk, notUsed;
217335 sqlite3_db_status(db, SQLITE_DBSTATUS_DEFERRED_FKS10, &nFk, &notUsed, 0);
217336 if( nFk!=0 ){
217337 int res = SQLITE_CHANGESET_ABORT2;
217338 sqlite3_changeset_iter sIter;
217339 memset(&sIter, 0, sizeof(sIter));
217340 sIter.nCol = nFk;
217341 res = xConflict(pCtx, SQLITE_CHANGESET_FOREIGN_KEY5, &sIter);
217342 if( res!=SQLITE_CHANGESET_OMIT0 ){
217343 rc = SQLITE_CONSTRAINT19;
217344 }
217345 }
217346 }
217347 sqlite3_exec(db, "PRAGMA defer_foreign_keys = 0", 0, 0, 0);
217348
217349 if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT0x0001)==0 ){
217350 if( rc==SQLITE_OK0 ){
217351 rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
217352 }else{
217353 sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
217354 sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
217355 }
217356 }
217357
217358 assert( sApply.bRebase || sApply.rebase.nBuf==0 )((void) (0));
217359 if( rc==SQLITE_OK0 && bPatchset==0 && sApply.bRebase ){
217360 *ppRebase = (void*)sApply.rebase.aBuf;
217361 *pnRebase = sApply.rebase.nBuf;
217362 sApply.rebase.aBuf = 0;
217363 }
217364 sessionUpdateFree(&sApply);
217365 sqlite3_finalize(sApply.pInsert);
217366 sqlite3_finalize(sApply.pDelete);
217367 sqlite3_finalize(sApply.pSelect);
217368 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
217369 sqlite3_free((char*)sApply.constraints.aBuf);
217370 sqlite3_free((char*)sApply.rebase.aBuf);
217371 sqlite3_mutex_leave(sqlite3_db_mutex(db));
217372 return rc;
217373}
217374
217375/*
217376** Apply the changeset passed via pChangeset/nChangeset to the main
217377** database attached to handle "db".
217378*/
217379SQLITE_API int sqlite3changeset_apply_v2(
217380 sqlite3 *db, /* Apply change to "main" db of this handle */
217381 int nChangeset, /* Size of changeset in bytes */
217382 void *pChangeset, /* Changeset blob */
217383 int(*xFilter)(
217384 void *pCtx, /* Copy of sixth arg to _apply() */
217385 const char *zTab /* Table name */
217386 ),
217387 int(*xConflict)(
217388 void *pCtx, /* Copy of sixth arg to _apply() */
217389 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
217390 sqlite3_changeset_iter *p /* Handle describing change and conflict */
217391 ),
217392 void *pCtx, /* First argument passed to xConflict */
217393 void **ppRebase, int *pnRebase,
217394 int flags
217395){
217396 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
217397 int bInv = !!(flags & SQLITE_CHANGESETAPPLY_INVERT0x0002);
217398 int rc = sessionChangesetStart(&pIter, 0, 0, nChangeset, pChangeset, bInv, 1);
217399 if( rc==SQLITE_OK0 ){
217400 rc = sessionChangesetApply(
217401 db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
217402 );
217403 }
217404 return rc;
217405}
217406
217407/*
217408** Apply the changeset passed via pChangeset/nChangeset to the main database
217409** attached to handle "db". Invoke the supplied conflict handler callback
217410** to resolve any conflicts encountered while applying the change.
217411*/
217412SQLITE_API int sqlite3changeset_apply(
217413 sqlite3 *db, /* Apply change to "main" db of this handle */
217414 int nChangeset, /* Size of changeset in bytes */
217415 void *pChangeset, /* Changeset blob */
217416 int(*xFilter)(
217417 void *pCtx, /* Copy of sixth arg to _apply() */
217418 const char *zTab /* Table name */
217419 ),
217420 int(*xConflict)(
217421 void *pCtx, /* Copy of fifth arg to _apply() */
217422 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
217423 sqlite3_changeset_iter *p /* Handle describing change and conflict */
217424 ),
217425 void *pCtx /* First argument passed to xConflict */
217426){
217427 return sqlite3changeset_apply_v2(
217428 db, nChangeset, pChangeset, xFilter, xConflict, pCtx, 0, 0, 0
217429 );
217430}
217431
217432/*
217433** Apply the changeset passed via xInput/pIn to the main database
217434** attached to handle "db". Invoke the supplied conflict handler callback
217435** to resolve any conflicts encountered while applying the change.
217436*/
217437SQLITE_API int sqlite3changeset_apply_v2_strm(
217438 sqlite3 *db, /* Apply change to "main" db of this handle */
217439 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
217440 void *pIn, /* First arg for xInput */
217441 int(*xFilter)(
217442 void *pCtx, /* Copy of sixth arg to _apply() */
217443 const char *zTab /* Table name */
217444 ),
217445 int(*xConflict)(
217446 void *pCtx, /* Copy of sixth arg to _apply() */
217447 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
217448 sqlite3_changeset_iter *p /* Handle describing change and conflict */
217449 ),
217450 void *pCtx, /* First argument passed to xConflict */
217451 void **ppRebase, int *pnRebase,
217452 int flags
217453){
217454 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
217455 int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT0x0002);
217456 int rc = sessionChangesetStart(&pIter, xInput, pIn, 0, 0, bInverse, 1);
217457 if( rc==SQLITE_OK0 ){
217458 rc = sessionChangesetApply(
217459 db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
217460 );
217461 }
217462 return rc;
217463}
217464SQLITE_API int sqlite3changeset_apply_strm(
217465 sqlite3 *db, /* Apply change to "main" db of this handle */
217466 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
217467 void *pIn, /* First arg for xInput */
217468 int(*xFilter)(
217469 void *pCtx, /* Copy of sixth arg to _apply() */
217470 const char *zTab /* Table name */
217471 ),
217472 int(*xConflict)(
217473 void *pCtx, /* Copy of sixth arg to _apply() */
217474 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
217475 sqlite3_changeset_iter *p /* Handle describing change and conflict */
217476 ),
217477 void *pCtx /* First argument passed to xConflict */
217478){
217479 return sqlite3changeset_apply_v2_strm(
217480 db, xInput, pIn, xFilter, xConflict, pCtx, 0, 0, 0
217481 );
217482}
217483
217484/*
217485** sqlite3_changegroup handle.
217486*/
217487struct sqlite3_changegroup {
217488 int rc; /* Error code */
217489 int bPatch; /* True to accumulate patchsets */
217490 SessionTable *pList; /* List of tables in current patch */
217491};
217492
217493/*
217494** This function is called to merge two changes to the same row together as
217495** part of an sqlite3changeset_concat() operation. A new change object is
217496** allocated and a pointer to it stored in *ppNew.
217497*/
217498static int sessionChangeMerge(
217499 SessionTable *pTab, /* Table structure */
217500 int bRebase, /* True for a rebase hash-table */
217501 int bPatchset, /* True for patchsets */
217502 SessionChange *pExist, /* Existing change */
217503 int op2, /* Second change operation */
217504 int bIndirect, /* True if second change is indirect */
217505 u8 *aRec, /* Second change record */
217506 int nRec, /* Number of bytes in aRec */
217507 SessionChange **ppNew /* OUT: Merged change */
217508){
217509 SessionChange *pNew = 0;
217510 int rc = SQLITE_OK0;
217511
217512 if( !pExist ){
217513 pNew = (SessionChange *)sqlite3_malloc64(sizeof(SessionChange) + nRec);
217514 if( !pNew ){
217515 return SQLITE_NOMEM7;
217516 }
217517 memset(pNew, 0, sizeof(SessionChange));
217518 pNew->op = op2;
217519 pNew->bIndirect = bIndirect;
217520 pNew->aRecord = (u8*)&pNew[1];
217521 if( bIndirect==0 || bRebase==0 ){
217522 pNew->nRecord = nRec;
217523 memcpy(pNew->aRecord, aRec, nRec);
217524 }else{
217525 int i;
217526 u8 *pIn = aRec;
217527 u8 *pOut = pNew->aRecord;
217528 for(i=0; i<pTab->nCol; i++){
217529 int nIn = sessionSerialLen(pIn);
217530 if( *pIn==0 ){
217531 *pOut++ = 0;
217532 }else if( pTab->abPK[i]==0 ){
217533 *pOut++ = 0xFF;
217534 }else{
217535 memcpy(pOut, pIn, nIn);
217536 pOut += nIn;
217537 }
217538 pIn += nIn;
217539 }
217540 pNew->nRecord = pOut - pNew->aRecord;
217541 }
217542 }else if( bRebase ){
217543 if( pExist->op==SQLITE_DELETE9 && pExist->bIndirect ){
217544 *ppNew = pExist;
217545 }else{
217546 sqlite3_int64 nByte = nRec + pExist->nRecord + sizeof(SessionChange);
217547 pNew = (SessionChange*)sqlite3_malloc64(nByte);
217548 if( pNew==0 ){
217549 rc = SQLITE_NOMEM7;
217550 }else{
217551 int i;
217552 u8 *a1 = pExist->aRecord;
217553 u8 *a2 = aRec;
217554 u8 *pOut;
217555
217556 memset(pNew, 0, nByte);
217557 pNew->bIndirect = bIndirect || pExist->bIndirect;
217558 pNew->op = op2;
217559 pOut = pNew->aRecord = (u8*)&pNew[1];
217560
217561 for(i=0; i<pTab->nCol; i++){
217562 int n1 = sessionSerialLen(a1);
217563 int n2 = sessionSerialLen(a2);
217564 if( *a1==0xFF || (pTab->abPK[i]==0 && bIndirect) ){
217565 *pOut++ = 0xFF;
217566 }else if( *a2==0 ){
217567 memcpy(pOut, a1, n1);
217568 pOut += n1;
217569 }else{
217570 memcpy(pOut, a2, n2);
217571 pOut += n2;
217572 }
217573 a1 += n1;
217574 a2 += n2;
217575 }
217576 pNew->nRecord = pOut - pNew->aRecord;
217577 }
217578 sqlite3_free(pExist);
217579 }
217580 }else{
217581 int op1 = pExist->op;
217582
217583 /*
217584 ** op1=INSERT, op2=INSERT -> Unsupported. Discard op2.
217585 ** op1=INSERT, op2=UPDATE -> INSERT.
217586 ** op1=INSERT, op2=DELETE -> (none)
217587 **
217588 ** op1=UPDATE, op2=INSERT -> Unsupported. Discard op2.
217589 ** op1=UPDATE, op2=UPDATE -> UPDATE.
217590 ** op1=UPDATE, op2=DELETE -> DELETE.
217591 **
217592 ** op1=DELETE, op2=INSERT -> UPDATE.
217593 ** op1=DELETE, op2=UPDATE -> Unsupported. Discard op2.
217594 ** op1=DELETE, op2=DELETE -> Unsupported. Discard op2.
217595 */
217596 if( (op1==SQLITE_INSERT18 && op2==SQLITE_INSERT18)
217597 || (op1==SQLITE_UPDATE23 && op2==SQLITE_INSERT18)
217598 || (op1==SQLITE_DELETE9 && op2==SQLITE_UPDATE23)
217599 || (op1==SQLITE_DELETE9 && op2==SQLITE_DELETE9)
217600 ){
217601 pNew = pExist;
217602 }else if( op1==SQLITE_INSERT18 && op2==SQLITE_DELETE9 ){
217603 sqlite3_free(pExist);
217604 assert( pNew==0 )((void) (0));
217605 }else{
217606 u8 *aExist = pExist->aRecord;
217607 sqlite3_int64 nByte;
217608 u8 *aCsr;
217609
217610 /* Allocate a new SessionChange object. Ensure that the aRecord[]
217611 ** buffer of the new object is large enough to hold any record that
217612 ** may be generated by combining the input records. */
217613 nByte = sizeof(SessionChange) + pExist->nRecord + nRec;
217614 pNew = (SessionChange *)sqlite3_malloc64(nByte);
217615 if( !pNew ){
217616 sqlite3_free(pExist);
217617 return SQLITE_NOMEM7;
217618 }
217619 memset(pNew, 0, sizeof(SessionChange));
217620 pNew->bIndirect = (bIndirect && pExist->bIndirect);
217621 aCsr = pNew->aRecord = (u8 *)&pNew[1];
217622
217623 if( op1==SQLITE_INSERT18 ){ /* INSERT + UPDATE */
217624 u8 *a1 = aRec;
217625 assert( op2==SQLITE_UPDATE )((void) (0));
217626 pNew->op = SQLITE_INSERT18;
217627 if( bPatchset==0 ) sessionSkipRecord(&a1, pTab->nCol);
217628 sessionMergeRecord(&aCsr, pTab->nCol, aExist, a1);
217629 }else if( op1==SQLITE_DELETE9 ){ /* DELETE + INSERT */
217630 assert( op2==SQLITE_INSERT )((void) (0));
217631 pNew->op = SQLITE_UPDATE23;
217632 if( bPatchset ){
217633 memcpy(aCsr, aRec, nRec);
217634 aCsr += nRec;
217635 }else{
217636 if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aExist, 0,aRec,0) ){
217637 sqlite3_free(pNew);
217638 pNew = 0;
217639 }
217640 }
217641 }else if( op2==SQLITE_UPDATE23 ){ /* UPDATE + UPDATE */
217642 u8 *a1 = aExist;
217643 u8 *a2 = aRec;
217644 assert( op1==SQLITE_UPDATE )((void) (0));
217645 if( bPatchset==0 ){
217646 sessionSkipRecord(&a1, pTab->nCol);
217647 sessionSkipRecord(&a2, pTab->nCol);
217648 }
217649 pNew->op = SQLITE_UPDATE23;
217650 if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aRec, aExist,a1,a2) ){
217651 sqlite3_free(pNew);
217652 pNew = 0;
217653 }
217654 }else{ /* UPDATE + DELETE */
217655 assert( op1==SQLITE_UPDATE && op2==SQLITE_DELETE )((void) (0));
217656 pNew->op = SQLITE_DELETE9;
217657 if( bPatchset ){
217658 memcpy(aCsr, aRec, nRec);
217659 aCsr += nRec;
217660 }else{
217661 sessionMergeRecord(&aCsr, pTab->nCol, aRec, aExist);
217662 }
217663 }
217664
217665 if( pNew ){
217666 pNew->nRecord = (int)(aCsr - pNew->aRecord);
217667 }
217668 sqlite3_free(pExist);
217669 }
217670 }
217671
217672 *ppNew = pNew;
217673 return rc;
217674}
217675
217676/*
217677** Add all changes in the changeset traversed by the iterator passed as
217678** the first argument to the changegroup hash tables.
217679*/
217680static int sessionChangesetToHash(
217681 sqlite3_changeset_iter *pIter, /* Iterator to read from */
217682 sqlite3_changegroup *pGrp, /* Changegroup object to add changeset to */
217683 int bRebase /* True if hash table is for rebasing */
217684){
217685 u8 *aRec;
217686 int nRec;
217687 int rc = SQLITE_OK0;
217688 SessionTable *pTab = 0;
217689
217690 while( SQLITE_ROW100==sessionChangesetNext(pIter, &aRec, &nRec, 0) ){
217691 const char *zNew;
217692 int nCol;
217693 int op;
217694 int iHash;
217695 int bIndirect;
217696 SessionChange *pChange;
217697 SessionChange *pExist = 0;
217698 SessionChange **pp;
217699
217700 if( pGrp->pList==0 ){
217701 pGrp->bPatch = pIter->bPatchset;
217702 }else if( pIter->bPatchset!=pGrp->bPatch ){
217703 rc = SQLITE_ERROR1;
217704 break;
217705 }
217706
217707 sqlite3changeset_op(pIter, &zNew, &nCol, &op, &bIndirect);
217708 if( !pTab || sqlite3_stricmp(zNew, pTab->zName) ){
217709 /* Search the list for a matching table */
217710 int nNew = (int)strlen(zNew);
217711 u8 *abPK;
217712
217713 sqlite3changeset_pk(pIter, &abPK, 0);
217714 for(pTab = pGrp->pList; pTab; pTab=pTab->pNext){
217715 if( 0==sqlite3_strnicmp(pTab->zName, zNew, nNew+1) ) break;
217716 }
217717 if( !pTab ){
217718 SessionTable **ppTab;
217719
217720 pTab = sqlite3_malloc64(sizeof(SessionTable) + nCol + nNew+1);
217721 if( !pTab ){
217722 rc = SQLITE_NOMEM7;
217723 break;
217724 }
217725 memset(pTab, 0, sizeof(SessionTable));
217726 pTab->nCol = nCol;
217727 pTab->abPK = (u8*)&pTab[1];
217728 memcpy(pTab->abPK, abPK, nCol);
217729 pTab->zName = (char*)&pTab->abPK[nCol];
217730 memcpy(pTab->zName, zNew, nNew+1);
217731
217732 /* The new object must be linked on to the end of the list, not
217733 ** simply added to the start of it. This is to ensure that the
217734 ** tables within the output of sqlite3changegroup_output() are in
217735 ** the right order. */
217736 for(ppTab=&pGrp->pList; *ppTab; ppTab=&(*ppTab)->pNext);
217737 *ppTab = pTab;
217738 }else if( pTab->nCol!=nCol || memcmp(pTab->abPK, abPK, nCol) ){
217739 rc = SQLITE_SCHEMA17;
217740 break;
217741 }
217742 }
217743
217744 if( sessionGrowHash(0, pIter->bPatchset, pTab) ){
217745 rc = SQLITE_NOMEM7;
217746 break;
217747 }
217748 iHash = sessionChangeHash(
217749 pTab, (pIter->bPatchset && op==SQLITE_DELETE9), aRec, pTab->nChange
217750 );
217751
217752 /* Search for existing entry. If found, remove it from the hash table.
217753 ** Code below may link it back in.
217754 */
217755 for(pp=&pTab->apChange[iHash]; *pp; pp=&(*pp)->pNext){
217756 int bPkOnly1 = 0;
217757 int bPkOnly2 = 0;
217758 if( pIter->bPatchset ){
217759 bPkOnly1 = (*pp)->op==SQLITE_DELETE9;
217760 bPkOnly2 = op==SQLITE_DELETE9;
217761 }
217762 if( sessionChangeEqual(pTab, bPkOnly1, (*pp)->aRecord, bPkOnly2, aRec) ){
217763 pExist = *pp;
217764 *pp = (*pp)->pNext;
217765 pTab->nEntry--;
217766 break;
217767 }
217768 }
217769
217770 rc = sessionChangeMerge(pTab, bRebase,
217771 pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
217772 );
217773 if( rc ) break;
217774 if( pChange ){
217775 pChange->pNext = pTab->apChange[iHash];
217776 pTab->apChange[iHash] = pChange;
217777 pTab->nEntry++;
217778 }
217779 }
217780
217781 if( rc==SQLITE_OK0 ) rc = pIter->rc;
217782 return rc;
217783}
217784
217785/*
217786** Serialize a changeset (or patchset) based on all changesets (or patchsets)
217787** added to the changegroup object passed as the first argument.
217788**
217789** If xOutput is not NULL, then the changeset/patchset is returned to the
217790** user via one or more calls to xOutput, as with the other streaming
217791** interfaces.
217792**
217793** Or, if xOutput is NULL, then (*ppOut) is populated with a pointer to a
217794** buffer containing the output changeset before this function returns. In
217795** this case (*pnOut) is set to the size of the output buffer in bytes. It
217796** is the responsibility of the caller to free the output buffer using
217797** sqlite3_free() when it is no longer required.
217798**
217799** If successful, SQLITE_OK is returned. Or, if an error occurs, an SQLite
217800** error code. If an error occurs and xOutput is NULL, (*ppOut) and (*pnOut)
217801** are both set to 0 before returning.
217802*/
217803static int sessionChangegroupOutput(
217804 sqlite3_changegroup *pGrp,
217805 int (*xOutput)(void *pOut, const void *pData, int nData),
217806 void *pOut,
217807 int *pnOut,
217808 void **ppOut
217809){
217810 int rc = SQLITE_OK0;
217811 SessionBuffer buf = {0, 0, 0};
217812 SessionTable *pTab;
217813 assert( xOutput==0 || (ppOut==0 && pnOut==0) )((void) (0));
217814
217815 /* Create the serialized output changeset based on the contents of the
217816 ** hash tables attached to the SessionTable objects in list p->pList.
217817 */
217818 for(pTab=pGrp->pList; rc==SQLITE_OK0 && pTab; pTab=pTab->pNext){
217819 int i;
217820 if( pTab->nEntry==0 ) continue;
217821
217822 sessionAppendTableHdr(&buf, pGrp->bPatch, pTab, &rc);
217823 for(i=0; i<pTab->nChange; i++){
217824 SessionChange *p;
217825 for(p=pTab->apChange[i]; p; p=p->pNext){
217826 sessionAppendByte(&buf, p->op, &rc);
217827 sessionAppendByte(&buf, p->bIndirect, &rc);
217828 sessionAppendBlob(&buf, p->aRecord, p->nRecord, &rc);
217829 if( rc==SQLITE_OK0 && xOutput && buf.nBuf>=sessions_strm_chunk_size ){
217830 rc = xOutput(pOut, buf.aBuf, buf.nBuf);
217831 buf.nBuf = 0;
217832 }
217833 }
217834 }
217835 }
217836
217837 if( rc==SQLITE_OK0 ){
217838 if( xOutput ){
217839 if( buf.nBuf>0 ) rc = xOutput(pOut, buf.aBuf, buf.nBuf);
217840 }else if( ppOut ){
217841 *ppOut = buf.aBuf;
217842 if( pnOut ) *pnOut = buf.nBuf;
217843 buf.aBuf = 0;
217844 }
217845 }
217846 sqlite3_free(buf.aBuf);
217847
217848 return rc;
217849}
217850
217851/*
217852** Allocate a new, empty, sqlite3_changegroup.
217853*/
217854SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp){
217855 int rc = SQLITE_OK0; /* Return code */
217856 sqlite3_changegroup *p; /* New object */
217857 p = (sqlite3_changegroup*)sqlite3_malloc(sizeof(sqlite3_changegroup));
217858 if( p==0 ){
217859 rc = SQLITE_NOMEM7;
217860 }else{
217861 memset(p, 0, sizeof(sqlite3_changegroup));
217862 }
217863 *pp = p;
217864 return rc;
217865}
217866
217867/*
217868** Add the changeset currently stored in buffer pData, size nData bytes,
217869** to changeset-group p.
217870*/
217871SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup *pGrp, int nData, void *pData){
217872 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
217873 int rc; /* Return code */
217874
217875 rc = sqlite3changeset_start(&pIter, nData, pData);
217876 if( rc==SQLITE_OK0 ){
217877 rc = sessionChangesetToHash(pIter, pGrp, 0);
217878 }
217879 sqlite3changeset_finalize(pIter);
217880 return rc;
217881}
217882
217883/*
217884** Obtain a buffer containing a changeset representing the concatenation
217885** of all changesets added to the group so far.
217886*/
217887SQLITE_API int sqlite3changegroup_output(
217888 sqlite3_changegroup *pGrp,
217889 int *pnData,
217890 void **ppData
217891){
217892 return sessionChangegroupOutput(pGrp, 0, 0, pnData, ppData);
217893}
217894
217895/*
217896** Streaming versions of changegroup_add().
217897*/
217898SQLITE_API int sqlite3changegroup_add_strm(
217899 sqlite3_changegroup *pGrp,
217900 int (*xInput)(void *pIn, void *pData, int *pnData),
217901 void *pIn
217902){
217903 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
217904 int rc; /* Return code */
217905
217906 rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
217907 if( rc==SQLITE_OK0 ){
217908 rc = sessionChangesetToHash(pIter, pGrp, 0);
217909 }
217910 sqlite3changeset_finalize(pIter);
217911 return rc;
217912}
217913
217914/*
217915** Streaming versions of changegroup_output().
217916*/
217917SQLITE_API int sqlite3changegroup_output_strm(
217918 sqlite3_changegroup *pGrp,
217919 int (*xOutput)(void *pOut, const void *pData, int nData),
217920 void *pOut
217921){
217922 return sessionChangegroupOutput(pGrp, xOutput, pOut, 0, 0);
217923}
217924
217925/*
217926** Delete a changegroup object.
217927*/
217928SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){
217929 if( pGrp ){
217930 sessionDeleteTable(0, pGrp->pList);
217931 sqlite3_free(pGrp);
217932 }
217933}
217934
217935/*
217936** Combine two changesets together.
217937*/
217938SQLITE_API int sqlite3changeset_concat(
217939 int nLeft, /* Number of bytes in lhs input */
217940 void *pLeft, /* Lhs input changeset */
217941 int nRight /* Number of bytes in rhs input */,
217942 void *pRight, /* Rhs input changeset */
217943 int *pnOut, /* OUT: Number of bytes in output changeset */
217944 void **ppOut /* OUT: changeset (left <concat> right) */
217945){
217946 sqlite3_changegroup *pGrp;
217947 int rc;
217948
217949 rc = sqlite3changegroup_new(&pGrp);
217950 if( rc==SQLITE_OK0 ){
217951 rc = sqlite3changegroup_add(pGrp, nLeft, pLeft);
217952 }
217953 if( rc==SQLITE_OK0 ){
217954 rc = sqlite3changegroup_add(pGrp, nRight, pRight);
217955 }
217956 if( rc==SQLITE_OK0 ){
217957 rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
217958 }
217959 sqlite3changegroup_delete(pGrp);
217960
217961 return rc;
217962}
217963
217964/*
217965** Streaming version of sqlite3changeset_concat().
217966*/
217967SQLITE_API int sqlite3changeset_concat_strm(
217968 int (*xInputA)(void *pIn, void *pData, int *pnData),
217969 void *pInA,
217970 int (*xInputB)(void *pIn, void *pData, int *pnData),
217971 void *pInB,
217972 int (*xOutput)(void *pOut, const void *pData, int nData),
217973 void *pOut
217974){
217975 sqlite3_changegroup *pGrp;
217976 int rc;
217977
217978 rc = sqlite3changegroup_new(&pGrp);
217979 if( rc==SQLITE_OK0 ){
217980 rc = sqlite3changegroup_add_strm(pGrp, xInputA, pInA);
217981 }
217982 if( rc==SQLITE_OK0 ){
217983 rc = sqlite3changegroup_add_strm(pGrp, xInputB, pInB);
217984 }
217985 if( rc==SQLITE_OK0 ){
217986 rc = sqlite3changegroup_output_strm(pGrp, xOutput, pOut);
217987 }
217988 sqlite3changegroup_delete(pGrp);
217989
217990 return rc;
217991}
217992
217993/*
217994** Changeset rebaser handle.
217995*/
217996struct sqlite3_rebaser {
217997 sqlite3_changegroup grp; /* Hash table */
217998};
217999
218000/*
218001** Buffers a1 and a2 must both contain a sessions module record nCol
218002** fields in size. This function appends an nCol sessions module
218003** record to buffer pBuf that is a copy of a1, except that for
218004** each field that is undefined in a1[], swap in the field from a2[].
218005*/
218006static void sessionAppendRecordMerge(
218007 SessionBuffer *pBuf, /* Buffer to append to */
218008 int nCol, /* Number of columns in each record */
218009 u8 *a1, int n1, /* Record 1 */
218010 u8 *a2, int n2, /* Record 2 */
218011 int *pRc /* IN/OUT: error code */
218012){
218013 sessionBufferGrow(pBuf, n1+n2, pRc);
218014 if( *pRc==SQLITE_OK0 ){
218015 int i;
218016 u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
218017 for(i=0; i<nCol; i++){
218018 int nn1 = sessionSerialLen(a1);
218019 int nn2 = sessionSerialLen(a2);
218020 if( *a1==0 || *a1==0xFF ){
218021 memcpy(pOut, a2, nn2);
218022 pOut += nn2;
218023 }else{
218024 memcpy(pOut, a1, nn1);
218025 pOut += nn1;
218026 }
218027 a1 += nn1;
218028 a2 += nn2;
218029 }
218030
218031 pBuf->nBuf = pOut-pBuf->aBuf;
218032 assert( pBuf->nBuf<=pBuf->nAlloc )((void) (0));
218033 }
218034}
218035
218036/*
218037** This function is called when rebasing a local UPDATE change against one
218038** or more remote UPDATE changes. The aRec/nRec buffer contains the current
218039** old.* and new.* records for the change. The rebase buffer (a single
218040** record) is in aChange/nChange. The rebased change is appended to buffer
218041** pBuf.
218042**
218043** Rebasing the UPDATE involves:
218044**
218045** * Removing any changes to fields for which the corresponding field
218046** in the rebase buffer is set to "replaced" (type 0xFF). If this
218047** means the UPDATE change updates no fields, nothing is appended
218048** to the output buffer.
218049**
218050** * For each field modified by the local change for which the
218051** corresponding field in the rebase buffer is not "undefined" (0x00)
218052** or "replaced" (0xFF), the old.* value is replaced by the value
218053** in the rebase buffer.
218054*/
218055static void sessionAppendPartialUpdate(
218056 SessionBuffer *pBuf, /* Append record here */
218057 sqlite3_changeset_iter *pIter, /* Iterator pointed at local change */
218058 u8 *aRec, int nRec, /* Local change */
218059 u8 *aChange, int nChange, /* Record to rebase against */
218060 int *pRc /* IN/OUT: Return Code */
218061){
218062 sessionBufferGrow(pBuf, 2+nRec+nChange, pRc);
218063 if( *pRc==SQLITE_OK0 ){
218064 int bData = 0;
218065 u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
218066 int i;
218067 u8 *a1 = aRec;
218068 u8 *a2 = aChange;
218069
218070 *pOut++ = SQLITE_UPDATE23;
218071 *pOut++ = pIter->bIndirect;
218072 for(i=0; i<pIter->nCol; i++){
218073 int n1 = sessionSerialLen(a1);
218074 int n2 = sessionSerialLen(a2);
218075 if( pIter->abPK[i] || a2[0]==0 ){
218076 if( !pIter->abPK[i] && a1[0] ) bData = 1;
218077 memcpy(pOut, a1, n1);
218078 pOut += n1;
218079 }else if( a2[0]!=0xFF ){
218080 bData = 1;
218081 memcpy(pOut, a2, n2);
218082 pOut += n2;
218083 }else{
218084 *pOut++ = '\0';
218085 }
218086 a1 += n1;
218087 a2 += n2;
218088 }
218089 if( bData ){
218090 a2 = aChange;
218091 for(i=0; i<pIter->nCol; i++){
218092 int n1 = sessionSerialLen(a1);
218093 int n2 = sessionSerialLen(a2);
218094 if( pIter->abPK[i] || a2[0]!=0xFF ){
218095 memcpy(pOut, a1, n1);
218096 pOut += n1;
218097 }else{
218098 *pOut++ = '\0';
218099 }
218100 a1 += n1;
218101 a2 += n2;
218102 }
218103 pBuf->nBuf = (pOut - pBuf->aBuf);
218104 }
218105 }
218106}
218107
218108/*
218109** pIter is configured to iterate through a changeset. This function rebases
218110** that changeset according to the current configuration of the rebaser
218111** object passed as the first argument. If no error occurs and argument xOutput
218112** is not NULL, then the changeset is returned to the caller by invoking
218113** xOutput zero or more times and SQLITE_OK returned. Or, if xOutput is NULL,
218114** then (*ppOut) is set to point to a buffer containing the rebased changeset
218115** before this function returns. In this case (*pnOut) is set to the size of
218116** the buffer in bytes. It is the responsibility of the caller to eventually
218117** free the (*ppOut) buffer using sqlite3_free().
218118**
218119** If an error occurs, an SQLite error code is returned. If ppOut and
218120** pnOut are not NULL, then the two output parameters are set to 0 before
218121** returning.
218122*/
218123static int sessionRebase(
218124 sqlite3_rebaser *p, /* Rebaser hash table */
218125 sqlite3_changeset_iter *pIter, /* Input data */
218126 int (*xOutput)(void *pOut, const void *pData, int nData),
218127 void *pOut, /* Context for xOutput callback */
218128 int *pnOut, /* OUT: Number of bytes in output changeset */
218129 void **ppOut /* OUT: Inverse of pChangeset */
218130){
218131 int rc = SQLITE_OK0;
218132 u8 *aRec = 0;
218133 int nRec = 0;
218134 int bNew = 0;
218135 SessionTable *pTab = 0;
218136 SessionBuffer sOut = {0,0,0};
218137
218138 while( SQLITE_ROW100==sessionChangesetNext(pIter, &aRec, &nRec, &bNew) ){
218139 SessionChange *pChange = 0;
218140 int bDone = 0;
218141
218142 if( bNew ){
218143 const char *zTab = pIter->zTab;
218144 for(pTab=p->grp.pList; pTab; pTab=pTab->pNext){
218145 if( 0==sqlite3_stricmp(pTab->zName, zTab) ) break;
218146 }
218147 bNew = 0;
218148
218149 /* A patchset may not be rebased */
218150 if( pIter->bPatchset ){
218151 rc = SQLITE_ERROR1;
218152 }
218153
218154 /* Append a table header to the output for this new table */
218155 sessionAppendByte(&sOut, pIter->bPatchset ? 'P' : 'T', &rc);
218156 sessionAppendVarint(&sOut, pIter->nCol, &rc);
218157 sessionAppendBlob(&sOut, pIter->abPK, pIter->nCol, &rc);
218158 sessionAppendBlob(&sOut,(u8*)pIter->zTab,(int)strlen(pIter->zTab)+1,&rc);
218159 }
218160
218161 if( pTab && rc==SQLITE_OK0 ){
218162 int iHash = sessionChangeHash(pTab, 0, aRec, pTab->nChange);
218163
218164 for(pChange=pTab->apChange[iHash]; pChange; pChange=pChange->pNext){
218165 if( sessionChangeEqual(pTab, 0, aRec, 0, pChange->aRecord) ){
218166 break;
218167 }
218168 }
218169 }
218170
218171 if( pChange ){
218172 assert( pChange->op==SQLITE_DELETE || pChange->op==SQLITE_INSERT )((void) (0));
218173 switch( pIter->op ){
218174 case SQLITE_INSERT18:
218175 if( pChange->op==SQLITE_INSERT18 ){
218176 bDone = 1;
218177 if( pChange->bIndirect==0 ){
218178 sessionAppendByte(&sOut, SQLITE_UPDATE23, &rc);
218179 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
218180 sessionAppendBlob(&sOut, pChange->aRecord, pChange->nRecord, &rc);
218181 sessionAppendBlob(&sOut, aRec, nRec, &rc);
218182 }
218183 }
218184 break;
218185
218186 case SQLITE_UPDATE23:
218187 bDone = 1;
218188 if( pChange->op==SQLITE_DELETE9 ){
218189 if( pChange->bIndirect==0 ){
218190 u8 *pCsr = aRec;
218191 sessionSkipRecord(&pCsr, pIter->nCol);
218192 sessionAppendByte(&sOut, SQLITE_INSERT18, &rc);
218193 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
218194 sessionAppendRecordMerge(&sOut, pIter->nCol,
218195 pCsr, nRec-(pCsr-aRec),
218196 pChange->aRecord, pChange->nRecord, &rc
218197 );
218198 }
218199 }else{
218200 sessionAppendPartialUpdate(&sOut, pIter,
218201 aRec, nRec, pChange->aRecord, pChange->nRecord, &rc
218202 );
218203 }
218204 break;
218205
218206 default:
218207 assert( pIter->op==SQLITE_DELETE )((void) (0));
218208 bDone = 1;
218209 if( pChange->op==SQLITE_INSERT18 ){
218210 sessionAppendByte(&sOut, SQLITE_DELETE9, &rc);
218211 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
218212 sessionAppendRecordMerge(&sOut, pIter->nCol,
218213 pChange->aRecord, pChange->nRecord, aRec, nRec, &rc
218214 );
218215 }
218216 break;
218217 }
218218 }
218219
218220 if( bDone==0 ){
218221 sessionAppendByte(&sOut, pIter->op, &rc);
218222 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
218223 sessionAppendBlob(&sOut, aRec, nRec, &rc);
218224 }
218225 if( rc==SQLITE_OK0 && xOutput && sOut.nBuf>sessions_strm_chunk_size ){
218226 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
218227 sOut.nBuf = 0;
218228 }
218229 if( rc ) break;
218230 }
218231
218232 if( rc!=SQLITE_OK0 ){
218233 sqlite3_free(sOut.aBuf);
218234 memset(&sOut, 0, sizeof(sOut));
218235 }
218236
218237 if( rc==SQLITE_OK0 ){
218238 if( xOutput ){
218239 if( sOut.nBuf>0 ){
218240 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
218241 }
218242 }else if( ppOut ){
218243 *ppOut = (void*)sOut.aBuf;
218244 *pnOut = sOut.nBuf;
218245 sOut.aBuf = 0;
218246 }
218247 }
218248 sqlite3_free(sOut.aBuf);
218249 return rc;
218250}
218251
218252/*
218253** Create a new rebaser object.
218254*/
218255SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew){
218256 int rc = SQLITE_OK0;
218257 sqlite3_rebaser *pNew;
218258
218259 pNew = sqlite3_malloc(sizeof(sqlite3_rebaser));
218260 if( pNew==0 ){
218261 rc = SQLITE_NOMEM7;
218262 }else{
218263 memset(pNew, 0, sizeof(sqlite3_rebaser));
218264 }
218265 *ppNew = pNew;
218266 return rc;
218267}
218268
218269/*
218270** Call this one or more times to configure a rebaser.
218271*/
218272SQLITE_API int sqlite3rebaser_configure(
218273 sqlite3_rebaser *p,
218274 int nRebase, const void *pRebase
218275){
218276 sqlite3_changeset_iter *pIter = 0; /* Iterator opened on pData/nData */
218277 int rc; /* Return code */
218278 rc = sqlite3changeset_start(&pIter, nRebase, (void*)pRebase);
218279 if( rc==SQLITE_OK0 ){
218280 rc = sessionChangesetToHash(pIter, &p->grp, 1);
218281 }
218282 sqlite3changeset_finalize(pIter);
218283 return rc;
218284}
218285
218286/*
218287** Rebase a changeset according to current rebaser configuration
218288*/
218289SQLITE_API int sqlite3rebaser_rebase(
218290 sqlite3_rebaser *p,
218291 int nIn, const void *pIn,
218292 int *pnOut, void **ppOut
218293){
218294 sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */
218295 int rc = sqlite3changeset_start(&pIter, nIn, (void*)pIn);
218296
218297 if( rc==SQLITE_OK0 ){
218298 rc = sessionRebase(p, pIter, 0, 0, pnOut, ppOut);
218299 sqlite3changeset_finalize(pIter);
218300 }
218301
218302 return rc;
218303}
218304
218305/*
218306** Rebase a changeset according to current rebaser configuration
218307*/
218308SQLITE_API int sqlite3rebaser_rebase_strm(
218309 sqlite3_rebaser *p,
218310 int (*xInput)(void *pIn, void *pData, int *pnData),
218311 void *pIn,
218312 int (*xOutput)(void *pOut, const void *pData, int nData),
218313 void *pOut
218314){
218315 sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */
218316 int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
218317
218318 if( rc==SQLITE_OK0 ){
218319 rc = sessionRebase(p, pIter, xOutput, pOut, 0, 0);
218320 sqlite3changeset_finalize(pIter);
218321 }
218322
218323 return rc;
218324}
218325
218326/*
218327** Destroy a rebaser object
218328*/
218329SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p){
218330 if( p ){
218331 sessionDeleteTable(0, p->grp.pList);
218332 sqlite3_free(p);
218333 }
218334}
218335
218336/*
218337** Global configuration
218338*/
218339SQLITE_API int sqlite3session_config(int op, void *pArg){
218340 int rc = SQLITE_OK0;
218341 switch( op ){
218342 case SQLITE_SESSION_CONFIG_STRMSIZE1: {
218343 int *pInt = (int*)pArg;
218344 if( *pInt>0 ){
218345 sessions_strm_chunk_size = *pInt;
218346 }
218347 *pInt = sessions_strm_chunk_size;
218348 break;
218349 }
218350 default:
218351 rc = SQLITE_MISUSE21;
218352 break;
218353 }
218354 return rc;
218355}
218356
218357#endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */
218358
218359/************** End of sqlite3session.c **************************************/
218360/************** Begin file fts5.c ********************************************/
218361
218362
218363#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS51)
218364
218365#if !defined(NDEBUG1) && !defined(SQLITE_DEBUG)
218366# define NDEBUG1 1
218367#endif
218368#if defined(NDEBUG1) && defined(SQLITE_DEBUG)
218369# undef NDEBUG1
218370#endif
218371
218372/*
218373** 2014 May 31
218374**
218375** The author disclaims copyright to this source code. In place of
218376** a legal notice, here is a blessing:
218377**
218378** May you do good and not evil.
218379** May you find forgiveness for yourself and forgive others.
218380** May you share freely, never taking more than you give.
218381**
218382******************************************************************************
218383**
218384** Interfaces to extend FTS5. Using the interfaces defined in this file,
218385** FTS5 may be extended with:
218386**
218387** * custom tokenizers, and
218388** * custom auxiliary functions.
218389*/
218390
218391
218392#ifndef _FTS5_H
218393#define _FTS5_H
218394
218395/* #include "sqlite3.h" */
218396
218397#if 0
218398extern "C" {
218399#endif
218400
218401/*************************************************************************
218402** CUSTOM AUXILIARY FUNCTIONS
218403**
218404** Virtual table implementations may overload SQL functions by implementing
218405** the sqlite3_module.xFindFunction() method.
218406*/
218407
218408typedef struct Fts5ExtensionApi Fts5ExtensionApi;
218409typedef struct Fts5Context Fts5Context;
218410typedef struct Fts5PhraseIter Fts5PhraseIter;
218411
218412typedef void (*fts5_extension_function)(
218413 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
218414 Fts5Context *pFts, /* First arg to pass to pApi functions */
218415 sqlite3_context *pCtx, /* Context for returning result/error */
218416 int nVal, /* Number of values in apVal[] array */
218417 sqlite3_value **apVal /* Array of trailing arguments */
218418);
218419
218420struct Fts5PhraseIter {
218421 const unsigned char *a;
218422 const unsigned char *b;
218423};
218424
218425/*
218426** EXTENSION API FUNCTIONS
218427**
218428** xUserData(pFts):
218429** Return a copy of the context pointer the extension function was
218430** registered with.
218431**
218432** xColumnTotalSize(pFts, iCol, pnToken):
218433** If parameter iCol is less than zero, set output variable *pnToken
218434** to the total number of tokens in the FTS5 table. Or, if iCol is
218435** non-negative but less than the number of columns in the table, return
218436** the total number of tokens in column iCol, considering all rows in
218437** the FTS5 table.
218438**
218439** If parameter iCol is greater than or equal to the number of columns
218440** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
218441** an OOM condition or IO error), an appropriate SQLite error code is
218442** returned.
218443**
218444** xColumnCount(pFts):
218445** Return the number of columns in the table.
218446**
218447** xColumnSize(pFts, iCol, pnToken):
218448** If parameter iCol is less than zero, set output variable *pnToken
218449** to the total number of tokens in the current row. Or, if iCol is
218450** non-negative but less than the number of columns in the table, set
218451** *pnToken to the number of tokens in column iCol of the current row.
218452**
218453** If parameter iCol is greater than or equal to the number of columns
218454** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
218455** an OOM condition or IO error), an appropriate SQLite error code is
218456** returned.
218457**
218458** This function may be quite inefficient if used with an FTS5 table
218459** created with the "columnsize=0" option.
218460**
218461** xColumnText:
218462** This function attempts to retrieve the text of column iCol of the
218463** current document. If successful, (*pz) is set to point to a buffer
218464** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
218465** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
218466** if an error occurs, an SQLite error code is returned and the final values
218467** of (*pz) and (*pn) are undefined.
218468**
218469** xPhraseCount:
218470** Returns the number of phrases in the current query expression.
218471**
218472** xPhraseSize:
218473** Returns the number of tokens in phrase iPhrase of the query. Phrases
218474** are numbered starting from zero.
218475**
218476** xInstCount:
218477** Set *pnInst to the total number of occurrences of all phrases within
218478** the query within the current row. Return SQLITE_OK if successful, or
218479** an error code (i.e. SQLITE_NOMEM) if an error occurs.
218480**
218481** This API can be quite slow if used with an FTS5 table created with the
218482** "detail=none" or "detail=column" option. If the FTS5 table is created
218483** with either "detail=none" or "detail=column" and "content=" option
218484** (i.e. if it is a contentless table), then this API always returns 0.
218485**
218486** xInst:
218487** Query for the details of phrase match iIdx within the current row.
218488** Phrase matches are numbered starting from zero, so the iIdx argument
218489** should be greater than or equal to zero and smaller than the value
218490** output by xInstCount().
218491**
218492** Usually, output parameter *piPhrase is set to the phrase number, *piCol
218493** to the column in which it occurs and *piOff the token offset of the
218494** first token of the phrase. Returns SQLITE_OK if successful, or an error
218495** code (i.e. SQLITE_NOMEM) if an error occurs.
218496**
218497** This API can be quite slow if used with an FTS5 table created with the
218498** "detail=none" or "detail=column" option.
218499**
218500** xRowid:
218501** Returns the rowid of the current row.
218502**
218503** xTokenize:
218504** Tokenize text using the tokenizer belonging to the FTS5 table.
218505**
218506** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
218507** This API function is used to query the FTS table for phrase iPhrase
218508** of the current query. Specifically, a query equivalent to:
218509**
218510** ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
218511**
218512** with $p set to a phrase equivalent to the phrase iPhrase of the
218513** current query is executed. Any column filter that applies to
218514** phrase iPhrase of the current query is included in $p. For each
218515** row visited, the callback function passed as the fourth argument
218516** is invoked. The context and API objects passed to the callback
218517** function may be used to access the properties of each matched row.
218518** Invoking Api.xUserData() returns a copy of the pointer passed as
218519** the third argument to pUserData.
218520**
218521** If the callback function returns any value other than SQLITE_OK, the
218522** query is abandoned and the xQueryPhrase function returns immediately.
218523** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
218524** Otherwise, the error code is propagated upwards.
218525**
218526** If the query runs to completion without incident, SQLITE_OK is returned.
218527** Or, if some error occurs before the query completes or is aborted by
218528** the callback, an SQLite error code is returned.
218529**
218530**
218531** xSetAuxdata(pFts5, pAux, xDelete)
218532**
218533** Save the pointer passed as the second argument as the extension function's
218534** "auxiliary data". The pointer may then be retrieved by the current or any
218535** future invocation of the same fts5 extension function made as part of
218536** the same MATCH query using the xGetAuxdata() API.
218537**
218538** Each extension function is allocated a single auxiliary data slot for
218539** each FTS query (MATCH expression). If the extension function is invoked
218540** more than once for a single FTS query, then all invocations share a
218541** single auxiliary data context.
218542**
218543** If there is already an auxiliary data pointer when this function is
218544** invoked, then it is replaced by the new pointer. If an xDelete callback
218545** was specified along with the original pointer, it is invoked at this
218546** point.
218547**
218548** The xDelete callback, if one is specified, is also invoked on the
218549** auxiliary data pointer after the FTS5 query has finished.
218550**
218551** If an error (e.g. an OOM condition) occurs within this function,
218552** the auxiliary data is set to NULL and an error code returned. If the
218553** xDelete parameter was not NULL, it is invoked on the auxiliary data
218554** pointer before returning.
218555**
218556**
218557** xGetAuxdata(pFts5, bClear)
218558**
218559** Returns the current auxiliary data pointer for the fts5 extension
218560** function. See the xSetAuxdata() method for details.
218561**
218562** If the bClear argument is non-zero, then the auxiliary data is cleared
218563** (set to NULL) before this function returns. In this case the xDelete,
218564** if any, is not invoked.
218565**
218566**
218567** xRowCount(pFts5, pnRow)
218568**
218569** This function is used to retrieve the total number of rows in the table.
218570** In other words, the same value that would be returned by:
218571**
218572** SELECT count(*) FROM ftstable;
218573**
218574** xPhraseFirst()
218575** This function is used, along with type Fts5PhraseIter and the xPhraseNext
218576** method, to iterate through all instances of a single query phrase within
218577** the current row. This is the same information as is accessible via the
218578** xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient
218579** to use, this API may be faster under some circumstances. To iterate
218580** through instances of phrase iPhrase, use the following code:
218581**
218582** Fts5PhraseIter iter;
218583** int iCol, iOff;
218584** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
218585** iCol>=0;
218586** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
218587** ){
218588** // An instance of phrase iPhrase at offset iOff of column iCol
218589** }
218590**
218591** The Fts5PhraseIter structure is defined above. Applications should not
218592** modify this structure directly - it should only be used as shown above
218593** with the xPhraseFirst() and xPhraseNext() API methods (and by
218594** xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below).
218595**
218596** This API can be quite slow if used with an FTS5 table created with the
218597** "detail=none" or "detail=column" option. If the FTS5 table is created
218598** with either "detail=none" or "detail=column" and "content=" option
218599** (i.e. if it is a contentless table), then this API always iterates
218600** through an empty set (all calls to xPhraseFirst() set iCol to -1).
218601**
218602** xPhraseNext()
218603** See xPhraseFirst above.
218604**
218605** xPhraseFirstColumn()
218606** This function and xPhraseNextColumn() are similar to the xPhraseFirst()
218607** and xPhraseNext() APIs described above. The difference is that instead
218608** of iterating through all instances of a phrase in the current row, these
218609** APIs are used to iterate through the set of columns in the current row
218610** that contain one or more instances of a specified phrase. For example:
218611**
218612** Fts5PhraseIter iter;
218613** int iCol;
218614** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
218615** iCol>=0;
218616** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
218617** ){
218618** // Column iCol contains at least one instance of phrase iPhrase
218619** }
218620**
218621** This API can be quite slow if used with an FTS5 table created with the
218622** "detail=none" option. If the FTS5 table is created with either
218623** "detail=none" "content=" option (i.e. if it is a contentless table),
218624** then this API always iterates through an empty set (all calls to
218625** xPhraseFirstColumn() set iCol to -1).
218626**
218627** The information accessed using this API and its companion
218628** xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext
218629** (or xInst/xInstCount). The chief advantage of this API is that it is
218630** significantly more efficient than those alternatives when used with
218631** "detail=column" tables.
218632**
218633** xPhraseNextColumn()
218634** See xPhraseFirstColumn above.
218635*/
218636struct Fts5ExtensionApi {
218637 int iVersion; /* Currently always set to 3 */
218638
218639 void *(*xUserData)(Fts5Context*);
218640
218641 int (*xColumnCount)(Fts5Context*);
218642 int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
218643 int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
218644
218645 int (*xTokenize)(Fts5Context*,
218646 const char *pText, int nText, /* Text to tokenize */
218647 void *pCtx, /* Context passed to xToken() */
218648 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
218649 );
218650
218651 int (*xPhraseCount)(Fts5Context*);
218652 int (*xPhraseSize)(Fts5Context*, int iPhrase);
218653
218654 int (*xInstCount)(Fts5Context*, int *pnInst);
218655 int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
218656
218657 sqlite3_int64 (*xRowid)(Fts5Context*);
218658 int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
218659 int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
218660
218661 int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
218662 int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
218663 );
218664 int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
218665 void *(*xGetAuxdata)(Fts5Context*, int bClear);
218666
218667 int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
218668 void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
218669
218670 int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
218671 void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
218672};
218673
218674/*
218675** CUSTOM AUXILIARY FUNCTIONS
218676*************************************************************************/
218677
218678/*************************************************************************
218679** CUSTOM TOKENIZERS
218680**
218681** Applications may also register custom tokenizer types. A tokenizer
218682** is registered by providing fts5 with a populated instance of the
218683** following structure. All structure methods must be defined, setting
218684** any member of the fts5_tokenizer struct to NULL leads to undefined
218685** behaviour. The structure methods are expected to function as follows:
218686**
218687** xCreate:
218688** This function is used to allocate and initialize a tokenizer instance.
218689** A tokenizer instance is required to actually tokenize text.
218690**
218691** The first argument passed to this function is a copy of the (void*)
218692** pointer provided by the application when the fts5_tokenizer object
218693** was registered with FTS5 (the third argument to xCreateTokenizer()).
218694** The second and third arguments are an array of nul-terminated strings
218695** containing the tokenizer arguments, if any, specified following the
218696** tokenizer name as part of the CREATE VIRTUAL TABLE statement used
218697** to create the FTS5 table.
218698**
218699** The final argument is an output variable. If successful, (*ppOut)
218700** should be set to point to the new tokenizer handle and SQLITE_OK
218701** returned. If an error occurs, some value other than SQLITE_OK should
218702** be returned. In this case, fts5 assumes that the final value of *ppOut
218703** is undefined.
218704**
218705** xDelete:
218706** This function is invoked to delete a tokenizer handle previously
218707** allocated using xCreate(). Fts5 guarantees that this function will
218708** be invoked exactly once for each successful call to xCreate().
218709**
218710** xTokenize:
218711** This function is expected to tokenize the nText byte string indicated
218712** by argument pText. pText may or may not be nul-terminated. The first
218713** argument passed to this function is a pointer to an Fts5Tokenizer object
218714** returned by an earlier call to xCreate().
218715**
218716** The second argument indicates the reason that FTS5 is requesting
218717** tokenization of the supplied text. This is always one of the following
218718** four values:
218719**
218720** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
218721** or removed from the FTS table. The tokenizer is being invoked to
218722** determine the set of tokens to add to (or delete from) the
218723** FTS index.
218724**
218725** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
218726** against the FTS index. The tokenizer is being called to tokenize
218727** a bareword or quoted string specified as part of the query.
218728**
218729** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
218730** FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is
218731** followed by a "*" character, indicating that the last token
218732** returned by the tokenizer will be treated as a token prefix.
218733**
218734** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
218735** satisfy an fts5_api.xTokenize() request made by an auxiliary
218736** function. Or an fts5_api.xColumnSize() request made by the same
218737** on a columnsize=0 database.
218738** </ul>
218739**
218740** For each token in the input string, the supplied callback xToken() must
218741** be invoked. The first argument to it should be a copy of the pointer
218742** passed as the second argument to xTokenize(). The third and fourth
218743** arguments are a pointer to a buffer containing the token text, and the
218744** size of the token in bytes. The 4th and 5th arguments are the byte offsets
218745** of the first byte of and first byte immediately following the text from
218746** which the token is derived within the input.
218747**
218748** The second argument passed to the xToken() callback ("tflags") should
218749** normally be set to 0. The exception is if the tokenizer supports
218750** synonyms. In this case see the discussion below for details.
218751**
218752** FTS5 assumes the xToken() callback is invoked for each token in the
218753** order that they occur within the input text.
218754**
218755** If an xToken() callback returns any value other than SQLITE_OK, then
218756** the tokenization should be abandoned and the xTokenize() method should
218757** immediately return a copy of the xToken() return value. Or, if the
218758** input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally,
218759** if an error occurs with the xTokenize() implementation itself, it
218760** may abandon the tokenization and return any error code other than
218761** SQLITE_OK or SQLITE_DONE.
218762**
218763** SYNONYM SUPPORT
218764**
218765** Custom tokenizers may also support synonyms. Consider a case in which a
218766** user wishes to query for a phrase such as "first place". Using the
218767** built-in tokenizers, the FTS5 query 'first + place' will match instances
218768** of "first place" within the document set, but not alternative forms
218769** such as "1st place". In some applications, it would be better to match
218770** all instances of "first place" or "1st place" regardless of which form
218771** the user specified in the MATCH query text.
218772**
218773** There are several ways to approach this in FTS5:
218774**
218775** <ol><li> By mapping all synonyms to a single token. In this case, using
218776** the above example, this means that the tokenizer returns the
218777** same token for inputs "first" and "1st". Say that token is in
218778** fact "first", so that when the user inserts the document "I won
218779** 1st place" entries are added to the index for tokens "i", "won",
218780** "first" and "place". If the user then queries for '1st + place',
218781** the tokenizer substitutes "first" for "1st" and the query works
218782** as expected.
218783**
218784** <li> By querying the index for all synonyms of each query term
218785** separately. In this case, when tokenizing query text, the
218786** tokenizer may provide multiple synonyms for a single term
218787** within the document. FTS5 then queries the index for each
218788** synonym individually. For example, faced with the query:
218789**
218790** <codeblock>
218791** ... MATCH 'first place'</codeblock>
218792**
218793** the tokenizer offers both "1st" and "first" as synonyms for the
218794** first token in the MATCH query and FTS5 effectively runs a query
218795** similar to:
218796**
218797** <codeblock>
218798** ... MATCH '(first OR 1st) place'</codeblock>
218799**
218800** except that, for the purposes of auxiliary functions, the query
218801** still appears to contain just two phrases - "(first OR 1st)"
218802** being treated as a single phrase.
218803**
218804** <li> By adding multiple synonyms for a single term to the FTS index.
218805** Using this method, when tokenizing document text, the tokenizer
218806** provides multiple synonyms for each token. So that when a
218807** document such as "I won first place" is tokenized, entries are
218808** added to the FTS index for "i", "won", "first", "1st" and
218809** "place".
218810**
218811** This way, even if the tokenizer does not provide synonyms
218812** when tokenizing query text (it should not - to do so would be
218813** inefficient), it doesn't matter if the user queries for
218814** 'first + place' or '1st + place', as there are entries in the
218815** FTS index corresponding to both forms of the first token.
218816** </ol>
218817**
218818** Whether it is parsing document or query text, any call to xToken that
218819** specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
218820** is considered to supply a synonym for the previous token. For example,
218821** when parsing the document "I won first place", a tokenizer that supports
218822** synonyms would call xToken() 5 times, as follows:
218823**
218824** <codeblock>
218825** xToken(pCtx, 0, "i", 1, 0, 1);
218826** xToken(pCtx, 0, "won", 3, 2, 5);
218827** xToken(pCtx, 0, "first", 5, 6, 11);
218828** xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3, 6, 11);
218829** xToken(pCtx, 0, "place", 5, 12, 17);
218830**</codeblock>
218831**
218832** It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time
218833** xToken() is called. Multiple synonyms may be specified for a single token
218834** by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence.
218835** There is no limit to the number of synonyms that may be provided for a
218836** single token.
218837**
218838** In many cases, method (1) above is the best approach. It does not add
218839** extra data to the FTS index or require FTS5 to query for multiple terms,
218840** so it is efficient in terms of disk space and query speed. However, it
218841** does not support prefix queries very well. If, as suggested above, the
218842** token "first" is substituted for "1st" by the tokenizer, then the query:
218843**
218844** <codeblock>
218845** ... MATCH '1s*'</codeblock>
218846**
218847** will not match documents that contain the token "1st" (as the tokenizer
218848** will probably not map "1s" to any prefix of "first").
218849**
218850** For full prefix support, method (3) may be preferred. In this case,
218851** because the index contains entries for both "first" and "1st", prefix
218852** queries such as 'fi*' or '1s*' will match correctly. However, because
218853** extra entries are added to the FTS index, this method uses more space
218854** within the database.
218855**
218856** Method (2) offers a midpoint between (1) and (3). Using this method,
218857** a query such as '1s*' will match documents that contain the literal
218858** token "1st", but not "first" (assuming the tokenizer is not able to
218859** provide synonyms for prefixes). However, a non-prefix query like '1st'
218860** will match against "1st" and "first". This method does not require
218861** extra disk space, as no extra entries are added to the FTS index.
218862** On the other hand, it may require more CPU cycles to run MATCH queries,
218863** as separate queries of the FTS index are required for each synonym.
218864**
218865** When using methods (2) or (3), it is important that the tokenizer only
218866** provide synonyms when tokenizing document text (method (2)) or query
218867** text (method (3)), not both. Doing so will not cause any errors, but is
218868** inefficient.
218869*/
218870typedef struct Fts5Tokenizer Fts5Tokenizer;
218871typedef struct fts5_tokenizer fts5_tokenizer;
218872struct fts5_tokenizer {
218873 int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
218874 void (*xDelete)(Fts5Tokenizer*);
218875 int (*xTokenize)(Fts5Tokenizer*,
218876 void *pCtx,
218877 int flags, /* Mask of FTS5_TOKENIZE_* flags */
218878 const char *pText, int nText,
218879 int (*xToken)(
218880 void *pCtx, /* Copy of 2nd argument to xTokenize() */
218881 int tflags, /* Mask of FTS5_TOKEN_* flags */
218882 const char *pToken, /* Pointer to buffer containing token */
218883 int nToken, /* Size of token in bytes */
218884 int iStart, /* Byte offset of token within input text */
218885 int iEnd /* Byte offset of end of token within input text */
218886 )
218887 );
218888};
218889
218890/* Flags that may be passed as the third argument to xTokenize() */
218891#define FTS5_TOKENIZE_QUERY0x0001 0x0001
218892#define FTS5_TOKENIZE_PREFIX0x0002 0x0002
218893#define FTS5_TOKENIZE_DOCUMENT0x0004 0x0004
218894#define FTS5_TOKENIZE_AUX0x0008 0x0008
218895
218896/* Flags that may be passed by the tokenizer implementation back to FTS5
218897** as the third argument to the supplied xToken callback. */
218898#define FTS5_TOKEN_COLOCATED0x0001 0x0001 /* Same position as prev. token */
218899
218900/*
218901** END OF CUSTOM TOKENIZERS
218902*************************************************************************/
218903
218904/*************************************************************************
218905** FTS5 EXTENSION REGISTRATION API
218906*/
218907typedef struct fts5_api fts5_api;
218908struct fts5_api {
218909 int iVersion; /* Currently always set to 2 */
218910
218911 /* Create a new tokenizer */
218912 int (*xCreateTokenizer)(
218913 fts5_api *pApi,
218914 const char *zName,
218915 void *pContext,
218916 fts5_tokenizer *pTokenizer,
218917 void (*xDestroy)(void*)
218918 );
218919
218920 /* Find an existing tokenizer */
218921 int (*xFindTokenizer)(
218922 fts5_api *pApi,
218923 const char *zName,
218924 void **ppContext,
218925 fts5_tokenizer *pTokenizer
218926 );
218927
218928 /* Create a new auxiliary function */
218929 int (*xCreateFunction)(
218930 fts5_api *pApi,
218931 const char *zName,
218932 void *pContext,
218933 fts5_extension_function xFunction,
218934 void (*xDestroy)(void*)
218935 );
218936};
218937
218938/*
218939** END OF REGISTRATION API
218940*************************************************************************/
218941
218942#if 0
218943} /* end of the 'extern "C"' block */
218944#endif
218945
218946#endif /* _FTS5_H */
218947
218948/*
218949** 2014 May 31
218950**
218951** The author disclaims copyright to this source code. In place of
218952** a legal notice, here is a blessing:
218953**
218954** May you do good and not evil.
218955** May you find forgiveness for yourself and forgive others.
218956** May you share freely, never taking more than you give.
218957**
218958******************************************************************************
218959**
218960*/
218961#ifndef _FTS5INT_H
218962#define _FTS5INT_H
218963
218964/* #include "fts5.h" */
218965/* #include "sqlite3ext.h" */
218966SQLITE_EXTENSION_INIT1
218967
218968/* #include <string.h> */
218969/* #include <assert.h> */
218970
218971#ifndef SQLITE_AMALGAMATION1
218972
218973typedef unsigned char u8;
218974typedef unsigned int u32;
218975typedef unsigned short u16;
218976typedef short i16;
218977typedef sqlite3_int64 i64;
218978typedef sqlite3_uint64 u64;
218979
218980#ifndef ArraySize
218981# define ArraySize(x)((int)(sizeof(x)/sizeof(x[0]))) ((int)(sizeof(x) / sizeof(x[0])))
218982#endif
218983
218984#define testcase(x)
218985
218986#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
218987# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
218988#endif
218989#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
218990# define ALWAYS(X)(X) (1)
218991# define NEVER(X)(X) (0)
218992#elif !defined(NDEBUG1)
218993# define ALWAYS(X)(X) ((X)?1:(assert(0)((void) (0)),0))
218994# define NEVER(X)(X) ((X)?(assert(0)((void) (0)),1):0)
218995#else
218996# define ALWAYS(X)(X) (X)
218997# define NEVER(X)(X) (X)
218998#endif
218999
219000#define MIN(x,y)((x)<(y)?(x):(y)) (((x) < (y)) ? (x) : (y))
219001#define MAX(x,y)((x)>(y)?(x):(y)) (((x) > (y)) ? (x) : (y))
219002
219003/*
219004** Constants for the largest and smallest possible 64-bit signed integers.
219005*/
219006# define LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) (0xffffffff|(((i64)0x7fffffff)<<32))
219007# define SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) (((i64)-1) - LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)))
219008
219009#endif
219010
219011/* Truncate very long tokens to this many bytes. Hard limit is
219012** (65536-1-1-4-9)==65521 bytes. The limiting factor is the 16-bit offset
219013** field that occurs at the start of each leaf page (see fts5_index.c). */
219014#define FTS5_MAX_TOKEN_SIZE32768 32768
219015
219016/*
219017** Maximum number of prefix indexes on single FTS5 table. This must be
219018** less than 32. If it is set to anything large than that, an #error
219019** directive in fts5_index.c will cause the build to fail.
219020*/
219021#define FTS5_MAX_PREFIX_INDEXES31 31
219022
219023/*
219024** Maximum segments permitted in a single index
219025*/
219026#define FTS5_MAX_SEGMENT2000 2000
219027
219028#define FTS5_DEFAULT_NEARDIST10 10
219029#define FTS5_DEFAULT_RANK"bm25" "bm25"
219030
219031/* Name of rank and rowid columns */
219032#define FTS5_RANK_NAME"rank" "rank"
219033#define FTS5_ROWID_NAME"rowid" "rowid"
219034
219035#ifdef SQLITE_DEBUG
219036# define FTS5_CORRUPT(11 | (1<<8)) sqlite3Fts5Corrupt()
219037static int sqlite3Fts5Corrupt(void);
219038#else
219039# define FTS5_CORRUPT(11 | (1<<8)) SQLITE_CORRUPT_VTAB(11 | (1<<8))
219040#endif
219041
219042/*
219043** The assert_nc() macro is similar to the assert() macro, except that it
219044** is used for assert() conditions that are true only if it can be
219045** guranteed that the database is not corrupt.
219046*/
219047#ifdef SQLITE_DEBUG
219048SQLITE_API extern int sqlite3_fts5_may_be_corrupt;
219049# define assert_nc(x)((void) (0)) assert(sqlite3_fts5_may_be_corrupt || (x))((void) (0))
219050#else
219051# define assert_nc(x)((void) (0)) assert(x)((void) (0))
219052#endif
219053
219054/*
219055** A version of memcmp() that does not cause asan errors if one of the pointer
219056** parameters is NULL and the number of bytes to compare is zero.
219057*/
219058#define fts5Memcmp(s1, s2, n)((n)<=0 ? 0 : memcmp((s1), (s2), (n))) ((n)<=0 ? 0 : memcmp((s1), (s2), (n)))
219059
219060/* Mark a function parameter as unused, to suppress nuisance compiler
219061** warnings. */
219062#ifndef UNUSED_PARAM
219063# define UNUSED_PARAM(X)(void)(X) (void)(X)
219064#endif
219065
219066#ifndef UNUSED_PARAM2
219067# define UNUSED_PARAM2(X, Y)(void)(X), (void)(Y) (void)(X), (void)(Y)
219068#endif
219069
219070typedef struct Fts5Global Fts5Global;
219071typedef struct Fts5Colset Fts5Colset;
219072
219073/* If a NEAR() clump or phrase may only match a specific set of columns,
219074** then an object of the following type is used to record the set of columns.
219075** Each entry in the aiCol[] array is a column that may be matched.
219076**
219077** This object is used by fts5_expr.c and fts5_index.c.
219078*/
219079struct Fts5Colset {
219080 int nCol;
219081 int aiCol[1];
219082};
219083
219084
219085
219086/**************************************************************************
219087** Interface to code in fts5_config.c. fts5_config.c contains contains code
219088** to parse the arguments passed to the CREATE VIRTUAL TABLE statement.
219089*/
219090
219091typedef struct Fts5Config Fts5Config;
219092
219093/*
219094** An instance of the following structure encodes all information that can
219095** be gleaned from the CREATE VIRTUAL TABLE statement.
219096**
219097** And all information loaded from the %_config table.
219098**
219099** nAutomerge:
219100** The minimum number of segments that an auto-merge operation should
219101** attempt to merge together. A value of 1 sets the object to use the
219102** compile time default. Zero disables auto-merge altogether.
219103**
219104** zContent:
219105**
219106** zContentRowid:
219107** The value of the content_rowid= option, if one was specified. Or
219108** the string "rowid" otherwise. This text is not quoted - if it is
219109** used as part of an SQL statement it needs to be quoted appropriately.
219110**
219111** zContentExprlist:
219112**
219113** pzErrmsg:
219114** This exists in order to allow the fts5_index.c module to return a
219115** decent error message if it encounters a file-format version it does
219116** not understand.
219117**
219118** bColumnsize:
219119** True if the %_docsize table is created.
219120**
219121** bPrefixIndex:
219122** This is only used for debugging. If set to false, any prefix indexes
219123** are ignored. This value is configured using:
219124**
219125** INSERT INTO tbl(tbl, rank) VALUES('prefix-index', $bPrefixIndex);
219126**
219127*/
219128struct Fts5Config {
219129 sqlite3 *db; /* Database handle */
219130 char *zDb; /* Database holding FTS index (e.g. "main") */
219131 char *zName; /* Name of FTS index */
219132 int nCol; /* Number of columns */
219133 char **azCol; /* Column names */
219134 u8 *abUnindexed; /* True for unindexed columns */
219135 int nPrefix; /* Number of prefix indexes */
219136 int *aPrefix; /* Sizes in bytes of nPrefix prefix indexes */
219137 int eContent; /* An FTS5_CONTENT value */
219138 char *zContent; /* content table */
219139 char *zContentRowid; /* "content_rowid=" option value */
219140 int bColumnsize; /* "columnsize=" option value (dflt==1) */
219141 int eDetail; /* FTS5_DETAIL_XXX value */
219142 char *zContentExprlist;
219143 Fts5Tokenizer *pTok;
219144 fts5_tokenizer *pTokApi;
219145 int bLock; /* True when table is preparing statement */
219146 int ePattern; /* FTS_PATTERN_XXX constant */
219147
219148 /* Values loaded from the %_config table */
219149 int iCookie; /* Incremented when %_config is modified */
219150 int pgsz; /* Approximate page size used in %_data */
219151 int nAutomerge; /* 'automerge' setting */
219152 int nCrisisMerge; /* Maximum allowed segments per level */
219153 int nUsermerge; /* 'usermerge' setting */
219154 int nHashSize; /* Bytes of memory for in-memory hash */
219155 char *zRank; /* Name of rank function */
219156 char *zRankArgs; /* Arguments to rank function */
219157
219158 /* If non-NULL, points to sqlite3_vtab.base.zErrmsg. Often NULL. */
219159 char **pzErrmsg;
219160
219161#ifdef SQLITE_DEBUG
219162 int bPrefixIndex; /* True to use prefix-indexes */
219163#endif
219164};
219165
219166/* Current expected value of %_config table 'version' field */
219167#define FTS5_CURRENT_VERSION4 4
219168
219169#define FTS5_CONTENT_NORMAL0 0
219170#define FTS5_CONTENT_NONE1 1
219171#define FTS5_CONTENT_EXTERNAL2 2
219172
219173#define FTS5_DETAIL_FULL0 0
219174#define FTS5_DETAIL_NONE1 1
219175#define FTS5_DETAIL_COLUMNS2 2
219176
219177#define FTS5_PATTERN_NONE0 0
219178#define FTS5_PATTERN_LIKE65 65 /* matches SQLITE_INDEX_CONSTRAINT_LIKE */
219179#define FTS5_PATTERN_GLOB66 66 /* matches SQLITE_INDEX_CONSTRAINT_GLOB */
219180
219181static int sqlite3Fts5ConfigParse(
219182 Fts5Global*, sqlite3*, int, const char **, Fts5Config**, char**
219183);
219184static void sqlite3Fts5ConfigFree(Fts5Config*);
219185
219186static int sqlite3Fts5ConfigDeclareVtab(Fts5Config *pConfig);
219187
219188static int sqlite3Fts5Tokenize(
219189 Fts5Config *pConfig, /* FTS5 Configuration object */
219190 int flags, /* FTS5_TOKENIZE_* flags */
219191 const char *pText, int nText, /* Text to tokenize */
219192 void *pCtx, /* Context passed to xToken() */
219193 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
219194);
219195
219196static void sqlite3Fts5Dequote(char *z);
219197
219198/* Load the contents of the %_config table */
219199static int sqlite3Fts5ConfigLoad(Fts5Config*, int);
219200
219201/* Set the value of a single config attribute */
219202static int sqlite3Fts5ConfigSetValue(Fts5Config*, const char*, sqlite3_value*, int*);
219203
219204static int sqlite3Fts5ConfigParseRank(const char*, char**, char**);
219205
219206/*
219207** End of interface to code in fts5_config.c.
219208**************************************************************************/
219209
219210/**************************************************************************
219211** Interface to code in fts5_buffer.c.
219212*/
219213
219214/*
219215** Buffer object for the incremental building of string data.
219216*/
219217typedef struct Fts5Buffer Fts5Buffer;
219218struct Fts5Buffer {
219219 u8 *p;
219220 int n;
219221 int nSpace;
219222};
219223
219224static int sqlite3Fts5BufferSize(int*, Fts5Buffer*, u32);
219225static void sqlite3Fts5BufferAppendVarint(int*, Fts5Buffer*, i64);
219226static void sqlite3Fts5BufferAppendBlob(int*, Fts5Buffer*, u32, const u8*);
219227static void sqlite3Fts5BufferAppendString(int *, Fts5Buffer*, const char*);
219228static void sqlite3Fts5BufferFree(Fts5Buffer*);
219229static void sqlite3Fts5BufferZero(Fts5Buffer*);
219230static void sqlite3Fts5BufferSet(int*, Fts5Buffer*, int, const u8*);
219231static void sqlite3Fts5BufferAppendPrintf(int *, Fts5Buffer*, char *zFmt, ...);
219232
219233static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...);
219234
219235#define fts5BufferZero(x)sqlite3Fts5BufferZero(x) sqlite3Fts5BufferZero(x)
219236#define fts5BufferAppendVarint(a,b,c)sqlite3Fts5BufferAppendVarint(a,b,c) sqlite3Fts5BufferAppendVarint(a,b,c)
219237#define fts5BufferFree(a)sqlite3Fts5BufferFree(a) sqlite3Fts5BufferFree(a)
219238#define fts5BufferAppendBlob(a,b,c,d)sqlite3Fts5BufferAppendBlob(a,b,c,d) sqlite3Fts5BufferAppendBlob(a,b,c,d)
219239#define fts5BufferSet(a,b,c,d)sqlite3Fts5BufferSet(a,b,c,d) sqlite3Fts5BufferSet(a,b,c,d)
219240
219241#define fts5BufferGrow(pRc,pBuf,nn)( (u32)((pBuf)->n) + (u32)(nn) <= (u32)((pBuf)->nSpace
) ? 0 : sqlite3Fts5BufferSize((pRc),(pBuf),(nn)+(pBuf)->n)
)
( \
219242 (u32)((pBuf)->n) + (u32)(nn) <= (u32)((pBuf)->nSpace) ? 0 : \
219243 sqlite3Fts5BufferSize((pRc),(pBuf),(nn)+(pBuf)->n) \
219244)
219245
219246/* Write and decode big-endian 32-bit integer values */
219247static void sqlite3Fts5Put32(u8*, int);
219248static int sqlite3Fts5Get32(const u8*);
219249
219250#define FTS5_POS2COLUMN(iPos)(int)(iPos >> 32) (int)(iPos >> 32)
219251#define FTS5_POS2OFFSET(iPos)(int)(iPos & 0x7FFFFFFF) (int)(iPos & 0x7FFFFFFF)
219252
219253typedef struct Fts5PoslistReader Fts5PoslistReader;
219254struct Fts5PoslistReader {
219255 /* Variables used only by sqlite3Fts5PoslistIterXXX() functions. */
219256 const u8 *a; /* Position list to iterate through */
219257 int n; /* Size of buffer at a[] in bytes */
219258 int i; /* Current offset in a[] */
219259
219260 u8 bFlag; /* For client use (any custom purpose) */
219261
219262 /* Output variables */
219263 u8 bEof; /* Set to true at EOF */
219264 i64 iPos; /* (iCol<<32) + iPos */
219265};
219266static int sqlite3Fts5PoslistReaderInit(
219267 const u8 *a, int n, /* Poslist buffer to iterate through */
219268 Fts5PoslistReader *pIter /* Iterator object to initialize */
219269);
219270static int sqlite3Fts5PoslistReaderNext(Fts5PoslistReader*);
219271
219272typedef struct Fts5PoslistWriter Fts5PoslistWriter;
219273struct Fts5PoslistWriter {
219274 i64 iPrev;
219275};
219276static int sqlite3Fts5PoslistWriterAppend(Fts5Buffer*, Fts5PoslistWriter*, i64);
219277static void sqlite3Fts5PoslistSafeAppend(Fts5Buffer*, i64*, i64);
219278
219279static int sqlite3Fts5PoslistNext64(
219280 const u8 *a, int n, /* Buffer containing poslist */
219281 int *pi, /* IN/OUT: Offset within a[] */
219282 i64 *piOff /* IN/OUT: Current offset */
219283);
219284
219285/* Malloc utility */
219286static void *sqlite3Fts5MallocZero(int *pRc, sqlite3_int64 nByte);
219287static char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn);
219288
219289/* Character set tests (like isspace(), isalpha() etc.) */
219290static int sqlite3Fts5IsBareword(char t);
219291
219292
219293/* Bucket of terms object used by the integrity-check in offsets=0 mode. */
219294typedef struct Fts5Termset Fts5Termset;
219295static int sqlite3Fts5TermsetNew(Fts5Termset**);
219296static int sqlite3Fts5TermsetAdd(Fts5Termset*, int, const char*, int, int *pbPresent);
219297static void sqlite3Fts5TermsetFree(Fts5Termset*);
219298
219299/*
219300** End of interface to code in fts5_buffer.c.
219301**************************************************************************/
219302
219303/**************************************************************************
219304** Interface to code in fts5_index.c. fts5_index.c contains contains code
219305** to access the data stored in the %_data table.
219306*/
219307
219308typedef struct Fts5Index Fts5Index;
219309typedef struct Fts5IndexIter Fts5IndexIter;
219310
219311struct Fts5IndexIter {
219312 i64 iRowid;
219313 const u8 *pData;
219314 int nData;
219315 u8 bEof;
219316};
219317
219318#define sqlite3Fts5IterEof(x)((x)->bEof) ((x)->bEof)
219319
219320/*
219321** Values used as part of the flags argument passed to IndexQuery().
219322*/
219323#define FTS5INDEX_QUERY_PREFIX0x0001 0x0001 /* Prefix query */
219324#define FTS5INDEX_QUERY_DESC0x0002 0x0002 /* Docs in descending rowid order */
219325#define FTS5INDEX_QUERY_TEST_NOIDX0x0004 0x0004 /* Do not use prefix index */
219326#define FTS5INDEX_QUERY_SCAN0x0008 0x0008 /* Scan query (fts5vocab) */
219327
219328/* The following are used internally by the fts5_index.c module. They are
219329** defined here only to make it easier to avoid clashes with the flags
219330** above. */
219331#define FTS5INDEX_QUERY_SKIPEMPTY0x0010 0x0010
219332#define FTS5INDEX_QUERY_NOOUTPUT0x0020 0x0020
219333
219334/*
219335** Create/destroy an Fts5Index object.
219336*/
219337static int sqlite3Fts5IndexOpen(Fts5Config *pConfig, int bCreate, Fts5Index**, char**);
219338static int sqlite3Fts5IndexClose(Fts5Index *p);
219339
219340/*
219341** Return a simple checksum value based on the arguments.
219342*/
219343static u64 sqlite3Fts5IndexEntryCksum(
219344 i64 iRowid,
219345 int iCol,
219346 int iPos,
219347 int iIdx,
219348 const char *pTerm,
219349 int nTerm
219350);
219351
219352/*
219353** Argument p points to a buffer containing utf-8 text that is n bytes in
219354** size. Return the number of bytes in the nChar character prefix of the
219355** buffer, or 0 if there are less than nChar characters in total.
219356*/
219357static int sqlite3Fts5IndexCharlenToBytelen(
219358 const char *p,
219359 int nByte,
219360 int nChar
219361);
219362
219363/*
219364** Open a new iterator to iterate though all rowids that match the
219365** specified token or token prefix.
219366*/
219367static int sqlite3Fts5IndexQuery(
219368 Fts5Index *p, /* FTS index to query */
219369 const char *pToken, int nToken, /* Token (or prefix) to query for */
219370 int flags, /* Mask of FTS5INDEX_QUERY_X flags */
219371 Fts5Colset *pColset, /* Match these columns only */
219372 Fts5IndexIter **ppIter /* OUT: New iterator object */
219373);
219374
219375/*
219376** The various operations on open token or token prefix iterators opened
219377** using sqlite3Fts5IndexQuery().
219378*/
219379static int sqlite3Fts5IterNext(Fts5IndexIter*);
219380static int sqlite3Fts5IterNextFrom(Fts5IndexIter*, i64 iMatch);
219381
219382/*
219383** Close an iterator opened by sqlite3Fts5IndexQuery().
219384*/
219385static void sqlite3Fts5IterClose(Fts5IndexIter*);
219386
219387/*
219388** Close the reader blob handle, if it is open.
219389*/
219390static void sqlite3Fts5IndexCloseReader(Fts5Index*);
219391
219392/*
219393** This interface is used by the fts5vocab module.
219394*/
219395static const char *sqlite3Fts5IterTerm(Fts5IndexIter*, int*);
219396static int sqlite3Fts5IterNextScan(Fts5IndexIter*);
219397static void *sqlite3Fts5StructureRef(Fts5Index*);
219398static void sqlite3Fts5StructureRelease(void*);
219399static int sqlite3Fts5StructureTest(Fts5Index*, void*);
219400
219401
219402/*
219403** Insert or remove data to or from the index. Each time a document is
219404** added to or removed from the index, this function is called one or more
219405** times.
219406**
219407** For an insert, it must be called once for each token in the new document.
219408** If the operation is a delete, it must be called (at least) once for each
219409** unique token in the document with an iCol value less than zero. The iPos
219410** argument is ignored for a delete.
219411*/
219412static int sqlite3Fts5IndexWrite(
219413 Fts5Index *p, /* Index to write to */
219414 int iCol, /* Column token appears in (-ve -> delete) */
219415 int iPos, /* Position of token within column */
219416 const char *pToken, int nToken /* Token to add or remove to or from index */
219417);
219418
219419/*
219420** Indicate that subsequent calls to sqlite3Fts5IndexWrite() pertain to
219421** document iDocid.
219422*/
219423static int sqlite3Fts5IndexBeginWrite(
219424 Fts5Index *p, /* Index to write to */
219425 int bDelete, /* True if current operation is a delete */
219426 i64 iDocid /* Docid to add or remove data from */
219427);
219428
219429/*
219430** Flush any data stored in the in-memory hash tables to the database.
219431** Also close any open blob handles.
219432*/
219433static int sqlite3Fts5IndexSync(Fts5Index *p);
219434
219435/*
219436** Discard any data stored in the in-memory hash tables. Do not write it
219437** to the database. Additionally, assume that the contents of the %_data
219438** table may have changed on disk. So any in-memory caches of %_data
219439** records must be invalidated.
219440*/
219441static int sqlite3Fts5IndexRollback(Fts5Index *p);
219442
219443/*
219444** Get or set the "averages" values.
219445*/
219446static int sqlite3Fts5IndexGetAverages(Fts5Index *p, i64 *pnRow, i64 *anSize);
219447static int sqlite3Fts5IndexSetAverages(Fts5Index *p, const u8*, int);
219448
219449/*
219450** Functions called by the storage module as part of integrity-check.
219451*/
219452static int sqlite3Fts5IndexIntegrityCheck(Fts5Index*, u64 cksum, int bUseCksum);
219453
219454/*
219455** Called during virtual module initialization to register UDF
219456** fts5_decode() with SQLite
219457*/
219458static int sqlite3Fts5IndexInit(sqlite3*);
219459
219460static int sqlite3Fts5IndexSetCookie(Fts5Index*, int);
219461
219462/*
219463** Return the total number of entries read from the %_data table by
219464** this connection since it was created.
219465*/
219466static int sqlite3Fts5IndexReads(Fts5Index *p);
219467
219468static int sqlite3Fts5IndexReinit(Fts5Index *p);
219469static int sqlite3Fts5IndexOptimize(Fts5Index *p);
219470static int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge);
219471static int sqlite3Fts5IndexReset(Fts5Index *p);
219472
219473static int sqlite3Fts5IndexLoadConfig(Fts5Index *p);
219474
219475/*
219476** End of interface to code in fts5_index.c.
219477**************************************************************************/
219478
219479/**************************************************************************
219480** Interface to code in fts5_varint.c.
219481*/
219482static int sqlite3Fts5GetVarint32(const unsigned char *p, u32 *v);
219483static int sqlite3Fts5GetVarintLen(u32 iVal);
219484static u8 sqlite3Fts5GetVarint(const unsigned char*, u64*);
219485static int sqlite3Fts5PutVarint(unsigned char *p, u64 v);
219486
219487#define fts5GetVarint32(a,b)sqlite3Fts5GetVarint32(a,(u32*)&b) sqlite3Fts5GetVarint32(a,(u32*)&b)
219488#define fts5GetVarintsqlite3Fts5GetVarint sqlite3Fts5GetVarint
219489
219490#define fts5FastGetVarint32(a, iOff, nVal){ nVal = (a)[iOff++]; if( nVal & 0x80 ){ iOff--; iOff += sqlite3Fts5GetVarint32
(&(a)[iOff],(u32*)&nVal); } }
{ \
219491 nVal = (a)[iOff++]; \
219492 if( nVal & 0x80 ){ \
219493 iOff--; \
219494 iOff += fts5GetVarint32(&(a)[iOff], nVal)sqlite3Fts5GetVarint32(&(a)[iOff],(u32*)&nVal); \
219495 } \
219496}
219497
219498
219499/*
219500** End of interface to code in fts5_varint.c.
219501**************************************************************************/
219502
219503
219504/**************************************************************************
219505** Interface to code in fts5_main.c.
219506*/
219507
219508/*
219509** Virtual-table object.
219510*/
219511typedef struct Fts5Table Fts5Table;
219512struct Fts5Table {
219513 sqlite3_vtab base; /* Base class used by SQLite core */
219514 Fts5Config *pConfig; /* Virtual table configuration */
219515 Fts5Index *pIndex; /* Full-text index */
219516};
219517
219518static int sqlite3Fts5GetTokenizer(
219519 Fts5Global*,
219520 const char **azArg,
219521 int nArg,
219522 Fts5Config*,
219523 char **pzErr
219524);
219525
219526static Fts5Table *sqlite3Fts5TableFromCsrid(Fts5Global*, i64);
219527
219528static int sqlite3Fts5FlushToDisk(Fts5Table*);
219529
219530/*
219531** End of interface to code in fts5.c.
219532**************************************************************************/
219533
219534/**************************************************************************
219535** Interface to code in fts5_hash.c.
219536*/
219537typedef struct Fts5Hash Fts5Hash;
219538
219539/*
219540** Create a hash table, free a hash table.
219541*/
219542static int sqlite3Fts5HashNew(Fts5Config*, Fts5Hash**, int *pnSize);
219543static void sqlite3Fts5HashFree(Fts5Hash*);
219544
219545static int sqlite3Fts5HashWrite(
219546 Fts5Hash*,
219547 i64 iRowid, /* Rowid for this entry */
219548 int iCol, /* Column token appears in (-ve -> delete) */
219549 int iPos, /* Position of token within column */
219550 char bByte,
219551 const char *pToken, int nToken /* Token to add or remove to or from index */
219552);
219553
219554/*
219555** Empty (but do not delete) a hash table.
219556*/
219557static void sqlite3Fts5HashClear(Fts5Hash*);
219558
219559static int sqlite3Fts5HashQuery(
219560 Fts5Hash*, /* Hash table to query */
219561 int nPre,
219562 const char *pTerm, int nTerm, /* Query term */
219563 void **ppObj, /* OUT: Pointer to doclist for pTerm */
219564 int *pnDoclist /* OUT: Size of doclist in bytes */
219565);
219566
219567static int sqlite3Fts5HashScanInit(
219568 Fts5Hash*, /* Hash table to query */
219569 const char *pTerm, int nTerm /* Query prefix */
219570);
219571static void sqlite3Fts5HashScanNext(Fts5Hash*);
219572static int sqlite3Fts5HashScanEof(Fts5Hash*);
219573static void sqlite3Fts5HashScanEntry(Fts5Hash *,
219574 const char **pzTerm, /* OUT: term (nul-terminated) */
219575 const u8 **ppDoclist, /* OUT: pointer to doclist */
219576 int *pnDoclist /* OUT: size of doclist in bytes */
219577);
219578
219579
219580/*
219581** End of interface to code in fts5_hash.c.
219582**************************************************************************/
219583
219584/**************************************************************************
219585** Interface to code in fts5_storage.c. fts5_storage.c contains contains
219586** code to access the data stored in the %_content and %_docsize tables.
219587*/
219588
219589#define FTS5_STMT_SCAN_ASC0 0 /* SELECT rowid, * FROM ... ORDER BY 1 ASC */
219590#define FTS5_STMT_SCAN_DESC1 1 /* SELECT rowid, * FROM ... ORDER BY 1 DESC */
219591#define FTS5_STMT_LOOKUP2 2 /* SELECT rowid, * FROM ... WHERE rowid=? */
219592
219593typedef struct Fts5Storage Fts5Storage;
219594
219595static int sqlite3Fts5StorageOpen(Fts5Config*, Fts5Index*, int, Fts5Storage**, char**);
219596static int sqlite3Fts5StorageClose(Fts5Storage *p);
219597static int sqlite3Fts5StorageRename(Fts5Storage*, const char *zName);
219598
219599static int sqlite3Fts5DropAll(Fts5Config*);
219600static int sqlite3Fts5CreateTable(Fts5Config*, const char*, const char*, int, char **);
219601
219602static int sqlite3Fts5StorageDelete(Fts5Storage *p, i64, sqlite3_value**);
219603static int sqlite3Fts5StorageContentInsert(Fts5Storage *p, sqlite3_value**, i64*);
219604static int sqlite3Fts5StorageIndexInsert(Fts5Storage *p, sqlite3_value**, i64);
219605
219606static int sqlite3Fts5StorageIntegrity(Fts5Storage *p, int iArg);
219607
219608static int sqlite3Fts5StorageStmt(Fts5Storage *p, int eStmt, sqlite3_stmt**, char**);
219609static void sqlite3Fts5StorageStmtRelease(Fts5Storage *p, int eStmt, sqlite3_stmt*);
219610
219611static int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol);
219612static int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnAvg);
219613static int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow);
219614
219615static int sqlite3Fts5StorageSync(Fts5Storage *p);
219616static int sqlite3Fts5StorageRollback(Fts5Storage *p);
219617
219618static int sqlite3Fts5StorageConfigValue(
219619 Fts5Storage *p, const char*, sqlite3_value*, int
219620);
219621
219622static int sqlite3Fts5StorageDeleteAll(Fts5Storage *p);
219623static int sqlite3Fts5StorageRebuild(Fts5Storage *p);
219624static int sqlite3Fts5StorageOptimize(Fts5Storage *p);
219625static int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge);
219626static int sqlite3Fts5StorageReset(Fts5Storage *p);
219627
219628/*
219629** End of interface to code in fts5_storage.c.
219630**************************************************************************/
219631
219632
219633/**************************************************************************
219634** Interface to code in fts5_expr.c.
219635*/
219636typedef struct Fts5Expr Fts5Expr;
219637typedef struct Fts5ExprNode Fts5ExprNode;
219638typedef struct Fts5Parse Fts5Parse;
219639typedef struct Fts5Token Fts5Token;
219640typedef struct Fts5ExprPhrase Fts5ExprPhrase;
219641typedef struct Fts5ExprNearset Fts5ExprNearset;
219642
219643struct Fts5Token {
219644 const char *p; /* Token text (not NULL terminated) */
219645 int n; /* Size of buffer p in bytes */
219646};
219647
219648/* Parse a MATCH expression. */
219649static int sqlite3Fts5ExprNew(
219650 Fts5Config *pConfig,
219651 int bPhraseToAnd,
219652 int iCol, /* Column on LHS of MATCH operator */
219653 const char *zExpr,
219654 Fts5Expr **ppNew,
219655 char **pzErr
219656);
219657static int sqlite3Fts5ExprPattern(
219658 Fts5Config *pConfig,
219659 int bGlob,
219660 int iCol,
219661 const char *zText,
219662 Fts5Expr **pp
219663);
219664
219665/*
219666** for(rc = sqlite3Fts5ExprFirst(pExpr, pIdx, bDesc);
219667** rc==SQLITE_OK && 0==sqlite3Fts5ExprEof(pExpr);
219668** rc = sqlite3Fts5ExprNext(pExpr)
219669** ){
219670** // The document with rowid iRowid matches the expression!
219671** i64 iRowid = sqlite3Fts5ExprRowid(pExpr);
219672** }
219673*/
219674static int sqlite3Fts5ExprFirst(Fts5Expr*, Fts5Index *pIdx, i64 iMin, int bDesc);
219675static int sqlite3Fts5ExprNext(Fts5Expr*, i64 iMax);
219676static int sqlite3Fts5ExprEof(Fts5Expr*);
219677static i64 sqlite3Fts5ExprRowid(Fts5Expr*);
219678
219679static void sqlite3Fts5ExprFree(Fts5Expr*);
219680static int sqlite3Fts5ExprAnd(Fts5Expr **pp1, Fts5Expr *p2);
219681
219682/* Called during startup to register a UDF with SQLite */
219683static int sqlite3Fts5ExprInit(Fts5Global*, sqlite3*);
219684
219685static int sqlite3Fts5ExprPhraseCount(Fts5Expr*);
219686static int sqlite3Fts5ExprPhraseSize(Fts5Expr*, int iPhrase);
219687static int sqlite3Fts5ExprPoslist(Fts5Expr*, int, const u8 **);
219688
219689typedef struct Fts5PoslistPopulator Fts5PoslistPopulator;
219690static Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr*, int);
219691static int sqlite3Fts5ExprPopulatePoslists(
219692 Fts5Config*, Fts5Expr*, Fts5PoslistPopulator*, int, const char*, int
219693);
219694static void sqlite3Fts5ExprCheckPoslists(Fts5Expr*, i64);
219695
219696static int sqlite3Fts5ExprClonePhrase(Fts5Expr*, int, Fts5Expr**);
219697
219698static int sqlite3Fts5ExprPhraseCollist(Fts5Expr *, int, const u8 **, int *);
219699
219700/*******************************************
219701** The fts5_expr.c API above this point is used by the other hand-written
219702** C code in this module. The interfaces below this point are called by
219703** the parser code in fts5parse.y. */
219704
219705static void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...);
219706
219707static Fts5ExprNode *sqlite3Fts5ParseNode(
219708 Fts5Parse *pParse,
219709 int eType,
219710 Fts5ExprNode *pLeft,
219711 Fts5ExprNode *pRight,
219712 Fts5ExprNearset *pNear
219713);
219714
219715static Fts5ExprNode *sqlite3Fts5ParseImplicitAnd(
219716 Fts5Parse *pParse,
219717 Fts5ExprNode *pLeft,
219718 Fts5ExprNode *pRight
219719);
219720
219721static Fts5ExprPhrase *sqlite3Fts5ParseTerm(
219722 Fts5Parse *pParse,
219723 Fts5ExprPhrase *pPhrase,
219724 Fts5Token *pToken,
219725 int bPrefix
219726);
219727
219728static void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase*);
219729
219730static Fts5ExprNearset *sqlite3Fts5ParseNearset(
219731 Fts5Parse*,
219732 Fts5ExprNearset*,
219733 Fts5ExprPhrase*
219734);
219735
219736static Fts5Colset *sqlite3Fts5ParseColset(
219737 Fts5Parse*,
219738 Fts5Colset*,
219739 Fts5Token *
219740);
219741
219742static void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase*);
219743static void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset*);
219744static void sqlite3Fts5ParseNodeFree(Fts5ExprNode*);
219745
219746static void sqlite3Fts5ParseSetDistance(Fts5Parse*, Fts5ExprNearset*, Fts5Token*);
219747static void sqlite3Fts5ParseSetColset(Fts5Parse*, Fts5ExprNode*, Fts5Colset*);
219748static Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse*, Fts5Colset*);
219749static void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p);
219750static void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token*);
219751
219752/*
219753** End of interface to code in fts5_expr.c.
219754**************************************************************************/
219755
219756
219757
219758/**************************************************************************
219759** Interface to code in fts5_aux.c.
219760*/
219761
219762static int sqlite3Fts5AuxInit(fts5_api*);
219763/*
219764** End of interface to code in fts5_aux.c.
219765**************************************************************************/
219766
219767/**************************************************************************
219768** Interface to code in fts5_tokenizer.c.
219769*/
219770
219771static int sqlite3Fts5TokenizerInit(fts5_api*);
219772static int sqlite3Fts5TokenizerPattern(
219773 int (*xCreate)(void*, const char**, int, Fts5Tokenizer**),
219774 Fts5Tokenizer *pTok
219775);
219776/*
219777** End of interface to code in fts5_tokenizer.c.
219778**************************************************************************/
219779
219780/**************************************************************************
219781** Interface to code in fts5_vocab.c.
219782*/
219783
219784static int sqlite3Fts5VocabInit(Fts5Global*, sqlite3*);
219785
219786/*
219787** End of interface to code in fts5_vocab.c.
219788**************************************************************************/
219789
219790
219791/**************************************************************************
219792** Interface to automatically generated code in fts5_unicode2.c.
219793*/
219794static int sqlite3Fts5UnicodeIsdiacritic(int c);
219795static int sqlite3Fts5UnicodeFold(int c, int bRemoveDiacritic);
219796
219797static int sqlite3Fts5UnicodeCatParse(const char*, u8*);
219798static int sqlite3Fts5UnicodeCategory(u32 iCode);
219799static void sqlite3Fts5UnicodeAscii(u8*, u8*);
219800/*
219801** End of interface to code in fts5_unicode2.c.
219802**************************************************************************/
219803
219804#endif
219805
219806#define FTS5_OR1 1
219807#define FTS5_AND2 2
219808#define FTS5_NOT3 3
219809#define FTS5_TERM4 4
219810#define FTS5_COLON5 5
219811#define FTS5_MINUS6 6
219812#define FTS5_LCP7 7
219813#define FTS5_RCP8 8
219814#define FTS5_STRING9 9
219815#define FTS5_LP10 10
219816#define FTS5_RP11 11
219817#define FTS5_CARET12 12
219818#define FTS5_COMMA13 13
219819#define FTS5_PLUS14 14
219820#define FTS5_STAR15 15
219821
219822/* This file is automatically generated by Lemon from input grammar
219823** source file "fts5parse.y". */
219824/*
219825** 2000-05-29
219826**
219827** The author disclaims copyright to this source code. In place of
219828** a legal notice, here is a blessing:
219829**
219830** May you do good and not evil.
219831** May you find forgiveness for yourself and forgive others.
219832** May you share freely, never taking more than you give.
219833**
219834*************************************************************************
219835** Driver template for the LEMON parser generator.
219836**
219837** The "lemon" program processes an LALR(1) input grammar file, then uses
219838** this template to construct a parser. The "lemon" program inserts text
219839** at each "%%" line. Also, any "P-a-r-s-e" identifer prefix (without the
219840** interstitial "-" characters) contained in this template is changed into
219841** the value of the %name directive from the grammar. Otherwise, the content
219842** of this template is copied straight through into the generate parser
219843** source file.
219844**
219845** The following is the concatenation of all %include directives from the
219846** input grammar file:
219847*/
219848/************ Begin %include sections from the grammar ************************/
219849
219850/* #include "fts5Int.h" */
219851/* #include "fts5parse.h" */
219852
219853/*
219854** Disable all error recovery processing in the parser push-down
219855** automaton.
219856*/
219857#define fts5YYNOERRORRECOVERY1 1
219858
219859/*
219860** Make fts5yytestcase() the same as testcase()
219861*/
219862#define fts5yytestcase(X) testcase(X)
219863
219864/*
219865** Indicate that sqlite3ParserFree() will never be called with a null
219866** pointer.
219867*/
219868#define fts5YYPARSEFREENOTNULL1 1
219869
219870/*
219871** Alternative datatype for the argument to the malloc() routine passed
219872** into sqlite3ParserAlloc(). The default is size_t.
219873*/
219874#define fts5YYMALLOCARGTYPEu64 u64
219875
219876/**************** End of %include directives **********************************/
219877/* These constants specify the various numeric values for terminal symbols.
219878***************** Begin token definitions *************************************/
219879#ifndef FTS5_OR1
219880#define FTS5_OR1 1
219881#define FTS5_AND2 2
219882#define FTS5_NOT3 3
219883#define FTS5_TERM4 4
219884#define FTS5_COLON5 5
219885#define FTS5_MINUS6 6
219886#define FTS5_LCP7 7
219887#define FTS5_RCP8 8
219888#define FTS5_STRING9 9
219889#define FTS5_LP10 10
219890#define FTS5_RP11 11
219891#define FTS5_CARET12 12
219892#define FTS5_COMMA13 13
219893#define FTS5_PLUS14 14
219894#define FTS5_STAR15 15
219895#endif
219896/**************** End token definitions ***************************************/
219897
219898/* The next sections is a series of control #defines.
219899** various aspects of the generated parser.
219900** fts5YYCODETYPE is the data type used to store the integer codes
219901** that represent terminal and non-terminal symbols.
219902** "unsigned char" is used if there are fewer than
219903** 256 symbols. Larger types otherwise.
219904** fts5YYNOCODE is a number of type fts5YYCODETYPE that is not used for
219905** any terminal or nonterminal symbol.
219906** fts5YYFALLBACK If defined, this indicates that one or more tokens
219907** (also known as: "terminal symbols") have fall-back
219908** values which should be used if the original symbol
219909** would not parse. This permits keywords to sometimes
219910** be used as identifiers, for example.
219911** fts5YYACTIONTYPE is the data type used for "action codes" - numbers
219912** that indicate what to do in response to the next
219913** token.
219914** sqlite3Fts5ParserFTS5TOKENTYPE is the data type used for minor type for terminal
219915** symbols. Background: A "minor type" is a semantic
219916** value associated with a terminal or non-terminal
219917** symbols. For example, for an "ID" terminal symbol,
219918** the minor type might be the name of the identifier.
219919** Each non-terminal can have a different minor type.
219920** Terminal symbols all have the same minor type, though.
219921** This macros defines the minor type for terminal
219922** symbols.
219923** fts5YYMINORTYPE is the data type used for all minor types.
219924** This is typically a union of many types, one of
219925** which is sqlite3Fts5ParserFTS5TOKENTYPE. The entry in the union
219926** for terminal symbols is called "fts5yy0".
219927** fts5YYSTACKDEPTH is the maximum depth of the parser's stack. If
219928** zero the stack is dynamically sized using realloc()
219929** sqlite3Fts5ParserARG_SDECL A static variable declaration for the %extra_argument
219930** sqlite3Fts5ParserARG_PDECL A parameter declaration for the %extra_argument
219931** sqlite3Fts5ParserARG_PARAM Code to pass %extra_argument as a subroutine parameter
219932** sqlite3Fts5ParserARG_STORE Code to store %extra_argument into fts5yypParser
219933** sqlite3Fts5ParserARG_FETCH Code to extract %extra_argument from fts5yypParser
219934** sqlite3Fts5ParserCTX_* As sqlite3Fts5ParserARG_ except for %extra_context
219935** fts5YYERRORSYMBOL is the code number of the error symbol. If not
219936** defined, then do no error processing.
219937** fts5YYNSTATE the combined number of states.
219938** fts5YYNRULE the number of rules in the grammar
219939** fts5YYNFTS5TOKEN Number of terminal symbols
219940** fts5YY_MAX_SHIFT Maximum value for shift actions
219941** fts5YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
219942** fts5YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
219943** fts5YY_ERROR_ACTION The fts5yy_action[] code for syntax error
219944** fts5YY_ACCEPT_ACTION The fts5yy_action[] code for accept
219945** fts5YY_NO_ACTION The fts5yy_action[] code for no-op
219946** fts5YY_MIN_REDUCE Minimum value for reduce actions
219947** fts5YY_MAX_REDUCE Maximum value for reduce actions
219948*/
219949#ifndef INTERFACE1
219950# define INTERFACE1 1
219951#endif
219952/************* Begin control #defines *****************************************/
219953#define fts5YYCODETYPEunsigned char unsigned char
219954#define fts5YYNOCODE27 27
219955#define fts5YYACTIONTYPEunsigned char unsigned char
219956#define sqlite3Fts5ParserFTS5TOKENTYPEFts5Token Fts5Token
219957typedef union {
219958 int fts5yyinit;
219959 sqlite3Fts5ParserFTS5TOKENTYPEFts5Token fts5yy0;
219960 int fts5yy4;
219961 Fts5Colset* fts5yy11;
219962 Fts5ExprNode* fts5yy24;
219963 Fts5ExprNearset* fts5yy46;
219964 Fts5ExprPhrase* fts5yy53;
219965} fts5YYMINORTYPE;
219966#ifndef fts5YYSTACKDEPTH100
219967#define fts5YYSTACKDEPTH100 100
219968#endif
219969#define sqlite3Fts5ParserARG_SDECLFts5Parse *pParse; Fts5Parse *pParse;
219970#define sqlite3Fts5ParserARG_PDECL,Fts5Parse *pParse ,Fts5Parse *pParse
219971#define sqlite3Fts5ParserARG_PARAM,pParse ,pParse
219972#define sqlite3Fts5ParserARG_FETCHFts5Parse *pParse=fts5yypParser->pParse; Fts5Parse *pParse=fts5yypParser->pParse;
219973#define sqlite3Fts5ParserARG_STOREfts5yypParser->pParse=pParse; fts5yypParser->pParse=pParse;
219974#define sqlite3Fts5ParserCTX_SDECL
219975#define sqlite3Fts5ParserCTX_PDECL
219976#define sqlite3Fts5ParserCTX_PARAM
219977#define sqlite3Fts5ParserCTX_FETCH
219978#define sqlite3Fts5ParserCTX_STORE
219979#define fts5YYNSTATE35 35
219980#define fts5YYNRULE28 28
219981#define fts5YYNRULE_WITH_ACTION28 28
219982#define fts5YYNFTS5TOKEN16 16
219983#define fts5YY_MAX_SHIFT34 34
219984#define fts5YY_MIN_SHIFTREDUCE52 52
219985#define fts5YY_MAX_SHIFTREDUCE79 79
219986#define fts5YY_ERROR_ACTION80 80
219987#define fts5YY_ACCEPT_ACTION81 81
219988#define fts5YY_NO_ACTION82 82
219989#define fts5YY_MIN_REDUCE83 83
219990#define fts5YY_MAX_REDUCE110 110
219991/************* End control #defines *******************************************/
219992#define fts5YY_NLOOKAHEAD((int)(sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0]))) ((int)(sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0])))
219993
219994/* Define the fts5yytestcase() macro to be a no-op if is not already defined
219995** otherwise.
219996**
219997** Applications can choose to define fts5yytestcase() in the %include section
219998** to a macro that can assist in verifying code coverage. For production
219999** code the fts5yytestcase() macro should be turned off. But it is useful
220000** for testing.
220001*/
220002#ifndef fts5yytestcase
220003# define fts5yytestcase(X)
220004#endif
220005
220006
220007/* Next are the tables used to determine what action to take based on the
220008** current state and lookahead token. These tables are used to implement
220009** functions that take a state number and lookahead value and return an
220010** action integer.
220011**
220012** Suppose the action integer is N. Then the action is determined as
220013** follows
220014**
220015** 0 <= N <= fts5YY_MAX_SHIFT Shift N. That is, push the lookahead
220016** token onto the stack and goto state N.
220017**
220018** N between fts5YY_MIN_SHIFTREDUCE Shift to an arbitrary state then
220019** and fts5YY_MAX_SHIFTREDUCE reduce by rule N-fts5YY_MIN_SHIFTREDUCE.
220020**
220021** N == fts5YY_ERROR_ACTION A syntax error has occurred.
220022**
220023** N == fts5YY_ACCEPT_ACTION The parser accepts its input.
220024**
220025** N == fts5YY_NO_ACTION No such action. Denotes unused
220026** slots in the fts5yy_action[] table.
220027**
220028** N between fts5YY_MIN_REDUCE Reduce by rule N-fts5YY_MIN_REDUCE
220029** and fts5YY_MAX_REDUCE
220030**
220031** The action table is constructed as a single large table named fts5yy_action[].
220032** Given state S and lookahead X, the action is computed as either:
220033**
220034** (A) N = fts5yy_action[ fts5yy_shift_ofst[S] + X ]
220035** (B) N = fts5yy_default[S]
220036**
220037** The (A) formula is preferred. The B formula is used instead if
220038** fts5yy_lookahead[fts5yy_shift_ofst[S]+X] is not equal to X.
220039**
220040** The formulas above are for computing the action when the lookahead is
220041** a terminal symbol. If the lookahead is a non-terminal (as occurs after
220042** a reduce action) then the fts5yy_reduce_ofst[] array is used in place of
220043** the fts5yy_shift_ofst[] array.
220044**
220045** The following are the tables generated in this section:
220046**
220047** fts5yy_action[] A single table containing all actions.
220048** fts5yy_lookahead[] A table containing the lookahead for each entry in
220049** fts5yy_action. Used to detect hash collisions.
220050** fts5yy_shift_ofst[] For each state, the offset into fts5yy_action for
220051** shifting terminals.
220052** fts5yy_reduce_ofst[] For each state, the offset into fts5yy_action for
220053** shifting non-terminals after a reduce.
220054** fts5yy_default[] Default action for each state.
220055**
220056*********** Begin parsing tables **********************************************/
220057#define fts5YY_ACTTAB_COUNT(105) (105)
220058static const fts5YYACTIONTYPEunsigned char fts5yy_action[] = {
220059 /* 0 */ 81, 20, 96, 6, 28, 99, 98, 26, 26, 18,
220060 /* 10 */ 96, 6, 28, 17, 98, 56, 26, 19, 96, 6,
220061 /* 20 */ 28, 14, 98, 14, 26, 31, 92, 96, 6, 28,
220062 /* 30 */ 108, 98, 25, 26, 21, 96, 6, 28, 78, 98,
220063 /* 40 */ 58, 26, 29, 96, 6, 28, 107, 98, 22, 26,
220064 /* 50 */ 24, 16, 12, 11, 1, 13, 13, 24, 16, 23,
220065 /* 60 */ 11, 33, 34, 13, 97, 8, 27, 32, 98, 7,
220066 /* 70 */ 26, 3, 4, 5, 3, 4, 5, 3, 83, 4,
220067 /* 80 */ 5, 3, 63, 5, 3, 62, 12, 2, 86, 13,
220068 /* 90 */ 9, 30, 10, 10, 54, 57, 75, 78, 78, 53,
220069 /* 100 */ 57, 15, 82, 82, 71,
220070};
220071static const fts5YYCODETYPEunsigned char fts5yy_lookahead[] = {
220072 /* 0 */ 16, 17, 18, 19, 20, 22, 22, 24, 24, 17,
220073 /* 10 */ 18, 19, 20, 7, 22, 9, 24, 17, 18, 19,
220074 /* 20 */ 20, 9, 22, 9, 24, 13, 17, 18, 19, 20,
220075 /* 30 */ 26, 22, 24, 24, 17, 18, 19, 20, 15, 22,
220076 /* 40 */ 9, 24, 17, 18, 19, 20, 26, 22, 21, 24,
220077 /* 50 */ 6, 7, 9, 9, 10, 12, 12, 6, 7, 21,
220078 /* 60 */ 9, 24, 25, 12, 18, 5, 20, 14, 22, 5,
220079 /* 70 */ 24, 3, 1, 2, 3, 1, 2, 3, 0, 1,
220080 /* 80 */ 2, 3, 11, 2, 3, 11, 9, 10, 5, 12,
220081 /* 90 */ 23, 24, 10, 10, 8, 9, 9, 15, 15, 8,
220082 /* 100 */ 9, 9, 27, 27, 11, 27, 27, 27, 27, 27,
220083 /* 110 */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
220084 /* 120 */ 27,
220085};
220086#define fts5YY_SHIFT_COUNT(34) (34)
220087#define fts5YY_SHIFT_MIN(0) (0)
220088#define fts5YY_SHIFT_MAX(93) (93)
220089static const unsigned char fts5yy_shift_ofst[] = {
220090 /* 0 */ 44, 44, 44, 44, 44, 44, 51, 77, 43, 12,
220091 /* 10 */ 14, 83, 82, 14, 23, 23, 31, 31, 71, 74,
220092 /* 20 */ 78, 81, 86, 91, 6, 53, 53, 60, 64, 68,
220093 /* 30 */ 53, 87, 92, 53, 93,
220094};
220095#define fts5YY_REDUCE_COUNT(17) (17)
220096#define fts5YY_REDUCE_MIN(-17) (-17)
220097#define fts5YY_REDUCE_MAX(67) (67)
220098static const signed char fts5yy_reduce_ofst[] = {
220099 /* 0 */ -16, -8, 0, 9, 17, 25, 46, -17, -17, 37,
220100 /* 10 */ 67, 4, 4, 8, 4, 20, 27, 38,
220101};
220102static const fts5YYACTIONTYPEunsigned char fts5yy_default[] = {
220103 /* 0 */ 80, 80, 80, 80, 80, 80, 95, 80, 80, 105,
220104 /* 10 */ 80, 110, 110, 80, 110, 110, 80, 80, 80, 80,
220105 /* 20 */ 80, 91, 80, 80, 80, 101, 100, 80, 80, 90,
220106 /* 30 */ 103, 80, 80, 104, 80,
220107};
220108/********** End of lemon-generated parsing tables *****************************/
220109
220110/* The next table maps tokens (terminal symbols) into fallback tokens.
220111** If a construct like the following:
220112**
220113** %fallback ID X Y Z.
220114**
220115** appears in the grammar, then ID becomes a fallback token for X, Y,
220116** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
220117** but it does not parse, the type of the token is changed to ID and
220118** the parse is retried before an error is thrown.
220119**
220120** This feature can be used, for example, to cause some keywords in a language
220121** to revert to identifiers if they keyword does not apply in the context where
220122** it appears.
220123*/
220124#ifdef fts5YYFALLBACK
220125static const fts5YYCODETYPEunsigned char fts5yyFallback[] = {
220126};
220127#endif /* fts5YYFALLBACK */
220128
220129/* The following structure represents a single element of the
220130** parser's stack. Information stored includes:
220131**
220132** + The state number for the parser at this level of the stack.
220133**
220134** + The value of the token stored at this level of the stack.
220135** (In other words, the "major" token.)
220136**
220137** + The semantic value stored at this level of the stack. This is
220138** the information used by the action routines in the grammar.
220139** It is sometimes called the "minor" token.
220140**
220141** After the "shift" half of a SHIFTREDUCE action, the stateno field
220142** actually contains the reduce action for the second half of the
220143** SHIFTREDUCE.
220144*/
220145struct fts5yyStackEntry {
220146 fts5YYACTIONTYPEunsigned char stateno; /* The state-number, or reduce action in SHIFTREDUCE */
220147 fts5YYCODETYPEunsigned char major; /* The major token value. This is the code
220148 ** number for the token at this stack level */
220149 fts5YYMINORTYPE minor; /* The user-supplied minor token value. This
220150 ** is the value of the token */
220151};
220152typedef struct fts5yyStackEntry fts5yyStackEntry;
220153
220154/* The state of the parser is completely contained in an instance of
220155** the following structure */
220156struct fts5yyParser {
220157 fts5yyStackEntry *fts5yytos; /* Pointer to top element of the stack */
220158#ifdef fts5YYTRACKMAXSTACKDEPTH
220159 int fts5yyhwm; /* High-water mark of the stack */
220160#endif
220161#ifndef fts5YYNOERRORRECOVERY1
220162 int fts5yyerrcnt; /* Shifts left before out of the error */
220163#endif
220164 sqlite3Fts5ParserARG_SDECLFts5Parse *pParse; /* A place to hold %extra_argument */
220165 sqlite3Fts5ParserCTX_SDECL /* A place to hold %extra_context */
220166#if fts5YYSTACKDEPTH100<=0
220167 int fts5yystksz; /* Current side of the stack */
220168 fts5yyStackEntry *fts5yystack; /* The parser's stack */
220169 fts5yyStackEntry fts5yystk0; /* First stack entry */
220170#else
220171 fts5yyStackEntry fts5yystack[fts5YYSTACKDEPTH100]; /* The parser's stack */
220172 fts5yyStackEntry *fts5yystackEnd; /* Last entry in the stack */
220173#endif
220174};
220175typedef struct fts5yyParser fts5yyParser;
220176
220177/* #include <assert.h> */
220178#ifndef NDEBUG1
220179/* #include <stdio.h> */
220180static FILE *fts5yyTraceFILE = 0;
220181static char *fts5yyTracePrompt = 0;
220182#endif /* NDEBUG */
220183
220184#ifndef NDEBUG1
220185/*
220186** Turn parser tracing on by giving a stream to which to write the trace
220187** and a prompt to preface each trace message. Tracing is turned off
220188** by making either argument NULL
220189**
220190** Inputs:
220191** <ul>
220192** <li> A FILE* to which trace output should be written.
220193** If NULL, then tracing is turned off.
220194** <li> A prefix string written at the beginning of every
220195** line of trace output. If NULL, then tracing is
220196** turned off.
220197** </ul>
220198**
220199** Outputs:
220200** None.
220201*/
220202static void sqlite3Fts5ParserTrace(FILE *TraceFILE, char *zTracePrompt){
220203 fts5yyTraceFILE = TraceFILE;
220204 fts5yyTracePrompt = zTracePrompt;
220205 if( fts5yyTraceFILE==0 ) fts5yyTracePrompt = 0;
220206 else if( fts5yyTracePrompt==0 ) fts5yyTraceFILE = 0;
220207}
220208#endif /* NDEBUG */
220209
220210#if defined(fts5YYCOVERAGE) || !defined(NDEBUG1)
220211/* For tracing shifts, the names of all terminals and nonterminals
220212** are required. The following table supplies these names */
220213static const char *const fts5yyTokenName[] = {
220214 /* 0 */ "$",
220215 /* 1 */ "OR",
220216 /* 2 */ "AND",
220217 /* 3 */ "NOT",
220218 /* 4 */ "TERM",
220219 /* 5 */ "COLON",
220220 /* 6 */ "MINUS",
220221 /* 7 */ "LCP",
220222 /* 8 */ "RCP",
220223 /* 9 */ "STRING",
220224 /* 10 */ "LP",
220225 /* 11 */ "RP",
220226 /* 12 */ "CARET",
220227 /* 13 */ "COMMA",
220228 /* 14 */ "PLUS",
220229 /* 15 */ "STAR",
220230 /* 16 */ "input",
220231 /* 17 */ "expr",
220232 /* 18 */ "cnearset",
220233 /* 19 */ "exprlist",
220234 /* 20 */ "colset",
220235 /* 21 */ "colsetlist",
220236 /* 22 */ "nearset",
220237 /* 23 */ "nearphrases",
220238 /* 24 */ "phrase",
220239 /* 25 */ "neardist_opt",
220240 /* 26 */ "star_opt",
220241};
220242#endif /* defined(fts5YYCOVERAGE) || !defined(NDEBUG) */
220243
220244#ifndef NDEBUG1
220245/* For tracing reduce actions, the names of all rules are required.
220246*/
220247static const char *const fts5yyRuleName[] = {
220248 /* 0 */ "input ::= expr",
220249 /* 1 */ "colset ::= MINUS LCP colsetlist RCP",
220250 /* 2 */ "colset ::= LCP colsetlist RCP",
220251 /* 3 */ "colset ::= STRING",
220252 /* 4 */ "colset ::= MINUS STRING",
220253 /* 5 */ "colsetlist ::= colsetlist STRING",
220254 /* 6 */ "colsetlist ::= STRING",
220255 /* 7 */ "expr ::= expr AND expr",
220256 /* 8 */ "expr ::= expr OR expr",
220257 /* 9 */ "expr ::= expr NOT expr",
220258 /* 10 */ "expr ::= colset COLON LP expr RP",
220259 /* 11 */ "expr ::= LP expr RP",
220260 /* 12 */ "expr ::= exprlist",
220261 /* 13 */ "exprlist ::= cnearset",
220262 /* 14 */ "exprlist ::= exprlist cnearset",
220263 /* 15 */ "cnearset ::= nearset",
220264 /* 16 */ "cnearset ::= colset COLON nearset",
220265 /* 17 */ "nearset ::= phrase",
220266 /* 18 */ "nearset ::= CARET phrase",
220267 /* 19 */ "nearset ::= STRING LP nearphrases neardist_opt RP",
220268 /* 20 */ "nearphrases ::= phrase",
220269 /* 21 */ "nearphrases ::= nearphrases phrase",
220270 /* 22 */ "neardist_opt ::=",
220271 /* 23 */ "neardist_opt ::= COMMA STRING",
220272 /* 24 */ "phrase ::= phrase PLUS STRING star_opt",
220273 /* 25 */ "phrase ::= STRING star_opt",
220274 /* 26 */ "star_opt ::= STAR",
220275 /* 27 */ "star_opt ::=",
220276};
220277#endif /* NDEBUG */
220278
220279
220280#if fts5YYSTACKDEPTH100<=0
220281/*
220282** Try to increase the size of the parser stack. Return the number
220283** of errors. Return 0 on success.
220284*/
220285static int fts5yyGrowStack(fts5yyParser *p){
220286 int newSize;
220287 int idx;
220288 fts5yyStackEntry *pNew;
220289
220290 newSize = p->fts5yystksz*2 + 100;
220291 idx = p->fts5yytos ? (int)(p->fts5yytos - p->fts5yystack) : 0;
220292 if( p->fts5yystack==&p->fts5yystk0 ){
220293 pNew = malloc(newSize*sizeof(pNew[0]));
220294 if( pNew ) pNew[0] = p->fts5yystk0;
220295 }else{
220296 pNew = realloc(p->fts5yystack, newSize*sizeof(pNew[0]));
220297 }
220298 if( pNew ){
220299 p->fts5yystack = pNew;
220300 p->fts5yytos = &p->fts5yystack[idx];
220301#ifndef NDEBUG1
220302 if( fts5yyTraceFILE ){
220303 fprintf(fts5yyTraceFILE,"%sStack grows from %d to %d entries.\n",
220304 fts5yyTracePrompt, p->fts5yystksz, newSize);
220305 }
220306#endif
220307 p->fts5yystksz = newSize;
220308 }
220309 return pNew==0;
220310}
220311#endif
220312
220313/* Datatype of the argument to the memory allocated passed as the
220314** second argument to sqlite3Fts5ParserAlloc() below. This can be changed by
220315** putting an appropriate #define in the %include section of the input
220316** grammar.
220317*/
220318#ifndef fts5YYMALLOCARGTYPEu64
220319# define fts5YYMALLOCARGTYPEu64 size_t
220320#endif
220321
220322/* Initialize a new parser that has already been allocated.
220323*/
220324static void sqlite3Fts5ParserInit(void *fts5yypRawParser sqlite3Fts5ParserCTX_PDECL){
220325 fts5yyParser *fts5yypParser = (fts5yyParser*)fts5yypRawParser;
220326 sqlite3Fts5ParserCTX_STORE
220327#ifdef fts5YYTRACKMAXSTACKDEPTH
220328 fts5yypParser->fts5yyhwm = 0;
220329#endif
220330#if fts5YYSTACKDEPTH100<=0
220331 fts5yypParser->fts5yytos = NULL((void*)0);
220332 fts5yypParser->fts5yystack = NULL((void*)0);
220333 fts5yypParser->fts5yystksz = 0;
220334 if( fts5yyGrowStack(fts5yypParser) ){
220335 fts5yypParser->fts5yystack = &fts5yypParser->fts5yystk0;
220336 fts5yypParser->fts5yystksz = 1;
220337 }
220338#endif
220339#ifndef fts5YYNOERRORRECOVERY1
220340 fts5yypParser->fts5yyerrcnt = -1;
220341#endif
220342 fts5yypParser->fts5yytos = fts5yypParser->fts5yystack;
220343 fts5yypParser->fts5yystack[0].stateno = 0;
220344 fts5yypParser->fts5yystack[0].major = 0;
220345#if fts5YYSTACKDEPTH100>0
220346 fts5yypParser->fts5yystackEnd = &fts5yypParser->fts5yystack[fts5YYSTACKDEPTH100-1];
220347#endif
220348}
220349
220350#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
220351/*
220352** This function allocates a new parser.
220353** The only argument is a pointer to a function which works like
220354** malloc.
220355**
220356** Inputs:
220357** A pointer to the function used to allocate memory.
220358**
220359** Outputs:
220360** A pointer to a parser. This pointer is used in subsequent calls
220361** to sqlite3Fts5Parser and sqlite3Fts5ParserFree.
220362*/
220363static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(fts5YYMALLOCARGTYPEu64) sqlite3Fts5ParserCTX_PDECL){
220364 fts5yyParser *fts5yypParser;
220365 fts5yypParser = (fts5yyParser*)(*mallocProc)( (fts5YYMALLOCARGTYPEu64)sizeof(fts5yyParser) );
220366 if( fts5yypParser ){
220367 sqlite3Fts5ParserCTX_STORE
220368 sqlite3Fts5ParserInit(fts5yypParser sqlite3Fts5ParserCTX_PARAM);
220369 }
220370 return (void*)fts5yypParser;
220371}
220372#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
220373
220374
220375/* The following function deletes the "minor type" or semantic value
220376** associated with a symbol. The symbol can be either a terminal
220377** or nonterminal. "fts5yymajor" is the symbol code, and "fts5yypminor" is
220378** a pointer to the value to be deleted. The code used to do the
220379** deletions is derived from the %destructor and/or %token_destructor
220380** directives of the input grammar.
220381*/
220382static void fts5yy_destructor(
220383 fts5yyParser *fts5yypParser, /* The parser */
220384 fts5YYCODETYPEunsigned char fts5yymajor, /* Type code for object to destroy */
220385 fts5YYMINORTYPE *fts5yypminor /* The object to be destroyed */
220386){
220387 sqlite3Fts5ParserARG_FETCHFts5Parse *pParse=fts5yypParser->pParse;
220388 sqlite3Fts5ParserCTX_FETCH
220389 switch( fts5yymajor ){
220390 /* Here is inserted the actions which take place when a
220391 ** terminal or non-terminal is destroyed. This can happen
220392 ** when the symbol is popped from the stack during a
220393 ** reduce or during error processing or when a parser is
220394 ** being destroyed before it is finished parsing.
220395 **
220396 ** Note: during a reduce, the only symbols destroyed are those
220397 ** which appear on the RHS of the rule, but which are *not* used
220398 ** inside the C code.
220399 */
220400/********* Begin destructor definitions ***************************************/
220401 case 16: /* input */
220402{
220403 (void)pParse;
220404}
220405 break;
220406 case 17: /* expr */
220407 case 18: /* cnearset */
220408 case 19: /* exprlist */
220409{
220410 sqlite3Fts5ParseNodeFree((fts5yypminor->fts5yy24));
220411}
220412 break;
220413 case 20: /* colset */
220414 case 21: /* colsetlist */
220415{
220416 sqlite3_free((fts5yypminor->fts5yy11));
220417}
220418 break;
220419 case 22: /* nearset */
220420 case 23: /* nearphrases */
220421{
220422 sqlite3Fts5ParseNearsetFree((fts5yypminor->fts5yy46));
220423}
220424 break;
220425 case 24: /* phrase */
220426{
220427 sqlite3Fts5ParsePhraseFree((fts5yypminor->fts5yy53));
220428}
220429 break;
220430/********* End destructor definitions *****************************************/
220431 default: break; /* If no destructor action specified: do nothing */
220432 }
220433}
220434
220435/*
220436** Pop the parser's stack once.
220437**
220438** If there is a destructor routine associated with the token which
220439** is popped from the stack, then call it.
220440*/
220441static void fts5yy_pop_parser_stack(fts5yyParser *pParser){
220442 fts5yyStackEntry *fts5yytos;
220443 assert( pParser->fts5yytos!=0 )((void) (0));
220444 assert( pParser->fts5yytos > pParser->fts5yystack )((void) (0));
220445 fts5yytos = pParser->fts5yytos--;
220446#ifndef NDEBUG1
220447 if( fts5yyTraceFILE ){
220448 fprintf(fts5yyTraceFILE,"%sPopping %s\n",
220449 fts5yyTracePrompt,
220450 fts5yyTokenName[fts5yytos->major]);
220451 }
220452#endif
220453 fts5yy_destructor(pParser, fts5yytos->major, &fts5yytos->minor);
220454}
220455
220456/*
220457** Clear all secondary memory allocations from the parser
220458*/
220459static void sqlite3Fts5ParserFinalize(void *p){
220460 fts5yyParser *pParser = (fts5yyParser*)p;
220461 while( pParser->fts5yytos>pParser->fts5yystack ) fts5yy_pop_parser_stack(pParser);
220462#if fts5YYSTACKDEPTH100<=0
220463 if( pParser->fts5yystack!=&pParser->fts5yystk0 ) free(pParser->fts5yystack);
220464#endif
220465}
220466
220467#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
220468/*
220469** Deallocate and destroy a parser. Destructors are called for
220470** all stack elements before shutting the parser down.
220471**
220472** If the fts5YYPARSEFREENEVERNULL macro exists (for example because it
220473** is defined in a %include section of the input grammar) then it is
220474** assumed that the input pointer is never NULL.
220475*/
220476static void sqlite3Fts5ParserFree(
220477 void *p, /* The parser to be deleted */
220478 void (*freeProc)(void*) /* Function used to reclaim memory */
220479){
220480#ifndef fts5YYPARSEFREENEVERNULL
220481 if( p==0 ) return;
220482#endif
220483 sqlite3Fts5ParserFinalize(p);
220484 (*freeProc)(p);
220485}
220486#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
220487
220488/*
220489** Return the peak depth of the stack for a parser.
220490*/
220491#ifdef fts5YYTRACKMAXSTACKDEPTH
220492static int sqlite3Fts5ParserStackPeak(void *p){
220493 fts5yyParser *pParser = (fts5yyParser*)p;
220494 return pParser->fts5yyhwm;
220495}
220496#endif
220497
220498/* This array of booleans keeps track of the parser statement
220499** coverage. The element fts5yycoverage[X][Y] is set when the parser
220500** is in state X and has a lookahead token Y. In a well-tested
220501** systems, every element of this matrix should end up being set.
220502*/
220503#if defined(fts5YYCOVERAGE)
220504static unsigned char fts5yycoverage[fts5YYNSTATE35][fts5YYNFTS5TOKEN16];
220505#endif
220506
220507/*
220508** Write into out a description of every state/lookahead combination that
220509**
220510** (1) has not been used by the parser, and
220511** (2) is not a syntax error.
220512**
220513** Return the number of missed state/lookahead combinations.
220514*/
220515#if defined(fts5YYCOVERAGE)
220516static int sqlite3Fts5ParserCoverage(FILE *out){
220517 int stateno, iLookAhead, i;
220518 int nMissed = 0;
220519 for(stateno=0; stateno<fts5YYNSTATE35; stateno++){
220520 i = fts5yy_shift_ofst[stateno];
220521 for(iLookAhead=0; iLookAhead<fts5YYNFTS5TOKEN16; iLookAhead++){
220522 if( fts5yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
220523 if( fts5yycoverage[stateno][iLookAhead]==0 ) nMissed++;
220524 if( out ){
220525 fprintf(out,"State %d lookahead %s %s\n", stateno,
220526 fts5yyTokenName[iLookAhead],
220527 fts5yycoverage[stateno][iLookAhead] ? "ok" : "missed");
220528 }
220529 }
220530 }
220531 return nMissed;
220532}
220533#endif
220534
220535/*
220536** Find the appropriate action for a parser given the terminal
220537** look-ahead token iLookAhead.
220538*/
220539static fts5YYACTIONTYPEunsigned char fts5yy_find_shift_action(
220540 fts5YYCODETYPEunsigned char iLookAhead, /* The look-ahead token */
220541 fts5YYACTIONTYPEunsigned char stateno /* Current state number */
220542){
220543 int i;
220544
220545 if( stateno>fts5YY_MAX_SHIFT34 ) return stateno;
220546 assert( stateno <= fts5YY_SHIFT_COUNT )((void) (0));
220547#if defined(fts5YYCOVERAGE)
220548 fts5yycoverage[stateno][iLookAhead] = 1;
220549#endif
220550 do{
220551 i = fts5yy_shift_ofst[stateno];
220552 assert( i>=0 )((void) (0));
220553 assert( i<=fts5YY_ACTTAB_COUNT )((void) (0));
220554 assert( i+fts5YYNFTS5TOKEN<=(int)fts5YY_NLOOKAHEAD )((void) (0));
220555 assert( iLookAhead!=fts5YYNOCODE )((void) (0));
220556 assert( iLookAhead < fts5YYNFTS5TOKEN )((void) (0));
220557 i += iLookAhead;
220558 assert( i<(int)fts5YY_NLOOKAHEAD )((void) (0));
220559 if( fts5yy_lookahead[i]!=iLookAhead ){
220560#ifdef fts5YYFALLBACK
220561 fts5YYCODETYPEunsigned char iFallback; /* Fallback token */
220562 assert( iLookAhead<sizeof(fts5yyFallback)/sizeof(fts5yyFallback[0]) )((void) (0));
220563 iFallback = fts5yyFallback[iLookAhead];
220564 if( iFallback!=0 ){
220565#ifndef NDEBUG1
220566 if( fts5yyTraceFILE ){
220567 fprintf(fts5yyTraceFILE, "%sFALLBACK %s => %s\n",
220568 fts5yyTracePrompt, fts5yyTokenName[iLookAhead], fts5yyTokenName[iFallback]);
220569 }
220570#endif
220571 assert( fts5yyFallback[iFallback]==0 )((void) (0)); /* Fallback loop must terminate */
220572 iLookAhead = iFallback;
220573 continue;
220574 }
220575#endif
220576#ifdef fts5YYWILDCARD
220577 {
220578 int j = i - iLookAhead + fts5YYWILDCARD;
220579 assert( j<(int)(sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0])) )((void) (0));
220580 if( fts5yy_lookahead[j]==fts5YYWILDCARD && iLookAhead>0 ){
220581#ifndef NDEBUG1
220582 if( fts5yyTraceFILE ){
220583 fprintf(fts5yyTraceFILE, "%sWILDCARD %s => %s\n",
220584 fts5yyTracePrompt, fts5yyTokenName[iLookAhead],
220585 fts5yyTokenName[fts5YYWILDCARD]);
220586 }
220587#endif /* NDEBUG */
220588 return fts5yy_action[j];
220589 }
220590 }
220591#endif /* fts5YYWILDCARD */
220592 return fts5yy_default[stateno];
220593 }else{
220594 assert( i>=0 && i<(int)(sizeof(fts5yy_action)/sizeof(fts5yy_action[0])) )((void) (0));
220595 return fts5yy_action[i];
220596 }
220597 }while(1);
220598}
220599
220600/*
220601** Find the appropriate action for a parser given the non-terminal
220602** look-ahead token iLookAhead.
220603*/
220604static fts5YYACTIONTYPEunsigned char fts5yy_find_reduce_action(
220605 fts5YYACTIONTYPEunsigned char stateno, /* Current state number */
220606 fts5YYCODETYPEunsigned char iLookAhead /* The look-ahead token */
220607){
220608 int i;
220609#ifdef fts5YYERRORSYMBOL
220610 if( stateno>fts5YY_REDUCE_COUNT(17) ){
220611 return fts5yy_default[stateno];
220612 }
220613#else
220614 assert( stateno<=fts5YY_REDUCE_COUNT )((void) (0));
220615#endif
220616 i = fts5yy_reduce_ofst[stateno];
220617 assert( iLookAhead!=fts5YYNOCODE )((void) (0));
220618 i += iLookAhead;
220619#ifdef fts5YYERRORSYMBOL
220620 if( i<0 || i>=fts5YY_ACTTAB_COUNT(105) || fts5yy_lookahead[i]!=iLookAhead ){
220621 return fts5yy_default[stateno];
220622 }
220623#else
220624 assert( i>=0 && i<fts5YY_ACTTAB_COUNT )((void) (0));
220625 assert( fts5yy_lookahead[i]==iLookAhead )((void) (0));
220626#endif
220627 return fts5yy_action[i];
220628}
220629
220630/*
220631** The following routine is called if the stack overflows.
220632*/
220633static void fts5yyStackOverflow(fts5yyParser *fts5yypParser){
220634 sqlite3Fts5ParserARG_FETCHFts5Parse *pParse=fts5yypParser->pParse;
220635 sqlite3Fts5ParserCTX_FETCH
220636#ifndef NDEBUG1
220637 if( fts5yyTraceFILE ){
220638 fprintf(fts5yyTraceFILE,"%sStack Overflow!\n",fts5yyTracePrompt);
220639 }
220640#endif
220641 while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(fts5yypParser);
220642 /* Here code is inserted which will execute if the parser
220643 ** stack every overflows */
220644/******** Begin %stack_overflow code ******************************************/
220645
220646 sqlite3Fts5ParseError(pParse, "fts5: parser stack overflow");
220647/******** End %stack_overflow code ********************************************/
220648 sqlite3Fts5ParserARG_STOREfts5yypParser->pParse=pParse; /* Suppress warning about unused %extra_argument var */
220649 sqlite3Fts5ParserCTX_STORE
220650}
220651
220652/*
220653** Print tracing information for a SHIFT action
220654*/
220655#ifndef NDEBUG1
220656static void fts5yyTraceShift(fts5yyParser *fts5yypParser, int fts5yyNewState, const char *zTag){
220657 if( fts5yyTraceFILE ){
220658 if( fts5yyNewState<fts5YYNSTATE35 ){
220659 fprintf(fts5yyTraceFILE,"%s%s '%s', go to state %d\n",
220660 fts5yyTracePrompt, zTag, fts5yyTokenName[fts5yypParser->fts5yytos->major],
220661 fts5yyNewState);
220662 }else{
220663 fprintf(fts5yyTraceFILE,"%s%s '%s', pending reduce %d\n",
220664 fts5yyTracePrompt, zTag, fts5yyTokenName[fts5yypParser->fts5yytos->major],
220665 fts5yyNewState - fts5YY_MIN_REDUCE83);
220666 }
220667 }
220668}
220669#else
220670# define fts5yyTraceShift(X,Y,Z)
220671#endif
220672
220673/*
220674** Perform a shift action.
220675*/
220676static void fts5yy_shift(
220677 fts5yyParser *fts5yypParser, /* The parser to be shifted */
220678 fts5YYACTIONTYPEunsigned char fts5yyNewState, /* The new state to shift in */
220679 fts5YYCODETYPEunsigned char fts5yyMajor, /* The major token to shift in */
220680 sqlite3Fts5ParserFTS5TOKENTYPEFts5Token fts5yyMinor /* The minor token to shift in */
220681){
220682 fts5yyStackEntry *fts5yytos;
220683 fts5yypParser->fts5yytos++;
220684#ifdef fts5YYTRACKMAXSTACKDEPTH
220685 if( (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack)>fts5yypParser->fts5yyhwm ){
220686 fts5yypParser->fts5yyhwm++;
220687 assert( fts5yypParser->fts5yyhwm == (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack) )((void) (0));
220688 }
220689#endif
220690#if fts5YYSTACKDEPTH100>0
220691 if( fts5yypParser->fts5yytos>fts5yypParser->fts5yystackEnd ){
220692 fts5yypParser->fts5yytos--;
220693 fts5yyStackOverflow(fts5yypParser);
220694 return;
220695 }
220696#else
220697 if( fts5yypParser->fts5yytos>=&fts5yypParser->fts5yystack[fts5yypParser->fts5yystksz] ){
220698 if( fts5yyGrowStack(fts5yypParser) ){
220699 fts5yypParser->fts5yytos--;
220700 fts5yyStackOverflow(fts5yypParser);
220701 return;
220702 }
220703 }
220704#endif
220705 if( fts5yyNewState > fts5YY_MAX_SHIFT34 ){
220706 fts5yyNewState += fts5YY_MIN_REDUCE83 - fts5YY_MIN_SHIFTREDUCE52;
220707 }
220708 fts5yytos = fts5yypParser->fts5yytos;
220709 fts5yytos->stateno = fts5yyNewState;
220710 fts5yytos->major = fts5yyMajor;
220711 fts5yytos->minor.fts5yy0 = fts5yyMinor;
220712 fts5yyTraceShift(fts5yypParser, fts5yyNewState, "Shift");
220713}
220714
220715/* For rule J, fts5yyRuleInfoLhs[J] contains the symbol on the left-hand side
220716** of that rule */
220717static const fts5YYCODETYPEunsigned char fts5yyRuleInfoLhs[] = {
220718 16, /* (0) input ::= expr */
220719 20, /* (1) colset ::= MINUS LCP colsetlist RCP */
220720 20, /* (2) colset ::= LCP colsetlist RCP */
220721 20, /* (3) colset ::= STRING */
220722 20, /* (4) colset ::= MINUS STRING */
220723 21, /* (5) colsetlist ::= colsetlist STRING */
220724 21, /* (6) colsetlist ::= STRING */
220725 17, /* (7) expr ::= expr AND expr */
220726 17, /* (8) expr ::= expr OR expr */
220727 17, /* (9) expr ::= expr NOT expr */
220728 17, /* (10) expr ::= colset COLON LP expr RP */
220729 17, /* (11) expr ::= LP expr RP */
220730 17, /* (12) expr ::= exprlist */
220731 19, /* (13) exprlist ::= cnearset */
220732 19, /* (14) exprlist ::= exprlist cnearset */
220733 18, /* (15) cnearset ::= nearset */
220734 18, /* (16) cnearset ::= colset COLON nearset */
220735 22, /* (17) nearset ::= phrase */
220736 22, /* (18) nearset ::= CARET phrase */
220737 22, /* (19) nearset ::= STRING LP nearphrases neardist_opt RP */
220738 23, /* (20) nearphrases ::= phrase */
220739 23, /* (21) nearphrases ::= nearphrases phrase */
220740 25, /* (22) neardist_opt ::= */
220741 25, /* (23) neardist_opt ::= COMMA STRING */
220742 24, /* (24) phrase ::= phrase PLUS STRING star_opt */
220743 24, /* (25) phrase ::= STRING star_opt */
220744 26, /* (26) star_opt ::= STAR */
220745 26, /* (27) star_opt ::= */
220746};
220747
220748/* For rule J, fts5yyRuleInfoNRhs[J] contains the negative of the number
220749** of symbols on the right-hand side of that rule. */
220750static const signed char fts5yyRuleInfoNRhs[] = {
220751 -1, /* (0) input ::= expr */
220752 -4, /* (1) colset ::= MINUS LCP colsetlist RCP */
220753 -3, /* (2) colset ::= LCP colsetlist RCP */
220754 -1, /* (3) colset ::= STRING */
220755 -2, /* (4) colset ::= MINUS STRING */
220756 -2, /* (5) colsetlist ::= colsetlist STRING */
220757 -1, /* (6) colsetlist ::= STRING */
220758 -3, /* (7) expr ::= expr AND expr */
220759 -3, /* (8) expr ::= expr OR expr */
220760 -3, /* (9) expr ::= expr NOT expr */
220761 -5, /* (10) expr ::= colset COLON LP expr RP */
220762 -3, /* (11) expr ::= LP expr RP */
220763 -1, /* (12) expr ::= exprlist */
220764 -1, /* (13) exprlist ::= cnearset */
220765 -2, /* (14) exprlist ::= exprlist cnearset */
220766 -1, /* (15) cnearset ::= nearset */
220767 -3, /* (16) cnearset ::= colset COLON nearset */
220768 -1, /* (17) nearset ::= phrase */
220769 -2, /* (18) nearset ::= CARET phrase */
220770 -5, /* (19) nearset ::= STRING LP nearphrases neardist_opt RP */
220771 -1, /* (20) nearphrases ::= phrase */
220772 -2, /* (21) nearphrases ::= nearphrases phrase */
220773 0, /* (22) neardist_opt ::= */
220774 -2, /* (23) neardist_opt ::= COMMA STRING */
220775 -4, /* (24) phrase ::= phrase PLUS STRING star_opt */
220776 -2, /* (25) phrase ::= STRING star_opt */
220777 -1, /* (26) star_opt ::= STAR */
220778 0, /* (27) star_opt ::= */
220779};
220780
220781static void fts5yy_accept(fts5yyParser*); /* Forward Declaration */
220782
220783/*
220784** Perform a reduce action and the shift that must immediately
220785** follow the reduce.
220786**
220787** The fts5yyLookahead and fts5yyLookaheadToken parameters provide reduce actions
220788** access to the lookahead token (if any). The fts5yyLookahead will be fts5YYNOCODE
220789** if the lookahead token has already been consumed. As this procedure is
220790** only called from one place, optimizing compilers will in-line it, which
220791** means that the extra parameters have no performance impact.
220792*/
220793static fts5YYACTIONTYPEunsigned char fts5yy_reduce(
220794 fts5yyParser *fts5yypParser, /* The parser */
220795 unsigned int fts5yyruleno, /* Number of the rule by which to reduce */
220796 int fts5yyLookahead, /* Lookahead token, or fts5YYNOCODE if none */
220797 sqlite3Fts5ParserFTS5TOKENTYPEFts5Token fts5yyLookaheadToken /* Value of the lookahead token */
220798 sqlite3Fts5ParserCTX_PDECL /* %extra_context */
220799){
220800 int fts5yygoto; /* The next state */
220801 fts5YYACTIONTYPEunsigned char fts5yyact; /* The next action */
220802 fts5yyStackEntry *fts5yymsp; /* The top of the parser's stack */
220803 int fts5yysize; /* Amount to pop the stack */
220804 sqlite3Fts5ParserARG_FETCHFts5Parse *pParse=fts5yypParser->pParse;
220805 (void)fts5yyLookahead;
220806 (void)fts5yyLookaheadToken;
220807 fts5yymsp = fts5yypParser->fts5yytos;
220808
220809 switch( fts5yyruleno ){
220810 /* Beginning here are the reduction cases. A typical example
220811 ** follows:
220812 ** case 0:
220813 ** #line <lineno> <grammarfile>
220814 ** { ... } // User supplied code
220815 ** #line <lineno> <thisfile>
220816 ** break;
220817 */
220818/********** Begin reduce actions **********************************************/
220819 fts5YYMINORTYPE fts5yylhsminor;
220820 case 0: /* input ::= expr */
220821{ sqlite3Fts5ParseFinished(pParse, fts5yymsp[0].minor.fts5yy24); }
220822 break;
220823 case 1: /* colset ::= MINUS LCP colsetlist RCP */
220824{
220825 fts5yymsp[-3].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
220826}
220827 break;
220828 case 2: /* colset ::= LCP colsetlist RCP */
220829{ fts5yymsp[-2].minor.fts5yy11 = fts5yymsp[-1].minor.fts5yy11; }
220830 break;
220831 case 3: /* colset ::= STRING */
220832{
220833 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
220834}
220835 fts5yymsp[0].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
220836 break;
220837 case 4: /* colset ::= MINUS STRING */
220838{
220839 fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
220840 fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
220841}
220842 break;
220843 case 5: /* colsetlist ::= colsetlist STRING */
220844{
220845 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, fts5yymsp[-1].minor.fts5yy11, &fts5yymsp[0].minor.fts5yy0); }
220846 fts5yymsp[-1].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
220847 break;
220848 case 6: /* colsetlist ::= STRING */
220849{
220850 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
220851}
220852 fts5yymsp[0].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
220853 break;
220854 case 7: /* expr ::= expr AND expr */
220855{
220856 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_AND2, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
220857}
220858 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
220859 break;
220860 case 8: /* expr ::= expr OR expr */
220861{
220862 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_OR1, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
220863}
220864 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
220865 break;
220866 case 9: /* expr ::= expr NOT expr */
220867{
220868 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_NOT3, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
220869}
220870 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
220871 break;
220872 case 10: /* expr ::= colset COLON LP expr RP */
220873{
220874 sqlite3Fts5ParseSetColset(pParse, fts5yymsp[-1].minor.fts5yy24, fts5yymsp[-4].minor.fts5yy11);
220875 fts5yylhsminor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;
220876}
220877 fts5yymsp[-4].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
220878 break;
220879 case 11: /* expr ::= LP expr RP */
220880{fts5yymsp[-2].minor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;}
220881 break;
220882 case 12: /* expr ::= exprlist */
220883 case 13: /* exprlist ::= cnearset */ fts5yytestcase(fts5yyruleno==13);
220884{fts5yylhsminor.fts5yy24 = fts5yymsp[0].minor.fts5yy24;}
220885 fts5yymsp[0].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
220886 break;
220887 case 14: /* exprlist ::= exprlist cnearset */
220888{
220889 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseImplicitAnd(pParse, fts5yymsp[-1].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24);
220890}
220891 fts5yymsp[-1].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
220892 break;
220893 case 15: /* cnearset ::= nearset */
220894{
220895 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_STRING9, 0, 0, fts5yymsp[0].minor.fts5yy46);
220896}
220897 fts5yymsp[0].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
220898 break;
220899 case 16: /* cnearset ::= colset COLON nearset */
220900{
220901 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_STRING9, 0, 0, fts5yymsp[0].minor.fts5yy46);
220902 sqlite3Fts5ParseSetColset(pParse, fts5yylhsminor.fts5yy24, fts5yymsp[-2].minor.fts5yy11);
220903}
220904 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
220905 break;
220906 case 17: /* nearset ::= phrase */
220907{ fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53); }
220908 fts5yymsp[0].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
220909 break;
220910 case 18: /* nearset ::= CARET phrase */
220911{
220912 sqlite3Fts5ParseSetCaret(fts5yymsp[0].minor.fts5yy53);
220913 fts5yymsp[-1].minor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53);
220914}
220915 break;
220916 case 19: /* nearset ::= STRING LP nearphrases neardist_opt RP */
220917{
220918 sqlite3Fts5ParseNear(pParse, &fts5yymsp[-4].minor.fts5yy0);
220919 sqlite3Fts5ParseSetDistance(pParse, fts5yymsp[-2].minor.fts5yy46, &fts5yymsp[-1].minor.fts5yy0);
220920 fts5yylhsminor.fts5yy46 = fts5yymsp[-2].minor.fts5yy46;
220921}
220922 fts5yymsp[-4].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
220923 break;
220924 case 20: /* nearphrases ::= phrase */
220925{
220926 fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53);
220927}
220928 fts5yymsp[0].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
220929 break;
220930 case 21: /* nearphrases ::= nearphrases phrase */
220931{
220932 fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, fts5yymsp[-1].minor.fts5yy46, fts5yymsp[0].minor.fts5yy53);
220933}
220934 fts5yymsp[-1].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
220935 break;
220936 case 22: /* neardist_opt ::= */
220937{ fts5yymsp[1].minor.fts5yy0.p = 0; fts5yymsp[1].minor.fts5yy0.n = 0; }
220938 break;
220939 case 23: /* neardist_opt ::= COMMA STRING */
220940{ fts5yymsp[-1].minor.fts5yy0 = fts5yymsp[0].minor.fts5yy0; }
220941 break;
220942 case 24: /* phrase ::= phrase PLUS STRING star_opt */
220943{
220944 fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, fts5yymsp[-3].minor.fts5yy53, &fts5yymsp[-1].minor.fts5yy0, fts5yymsp[0].minor.fts5yy4);
220945}
220946 fts5yymsp[-3].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
220947 break;
220948 case 25: /* phrase ::= STRING star_opt */
220949{
220950 fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, 0, &fts5yymsp[-1].minor.fts5yy0, fts5yymsp[0].minor.fts5yy4);
220951}
220952 fts5yymsp[-1].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
220953 break;
220954 case 26: /* star_opt ::= STAR */
220955{ fts5yymsp[0].minor.fts5yy4 = 1; }
220956 break;
220957 case 27: /* star_opt ::= */
220958{ fts5yymsp[1].minor.fts5yy4 = 0; }
220959 break;
220960 default:
220961 break;
220962/********** End reduce actions ************************************************/
220963 };
220964 assert( fts5yyruleno<sizeof(fts5yyRuleInfoLhs)/sizeof(fts5yyRuleInfoLhs[0]) )((void) (0));
220965 fts5yygoto = fts5yyRuleInfoLhs[fts5yyruleno];
220966 fts5yysize = fts5yyRuleInfoNRhs[fts5yyruleno];
220967 fts5yyact = fts5yy_find_reduce_action(fts5yymsp[fts5yysize].stateno,(fts5YYCODETYPEunsigned char)fts5yygoto);
220968
220969 /* There are no SHIFTREDUCE actions on nonterminals because the table
220970 ** generator has simplified them to pure REDUCE actions. */
220971 assert( !(fts5yyact>fts5YY_MAX_SHIFT && fts5yyact<=fts5YY_MAX_SHIFTREDUCE) )((void) (0));
220972
220973 /* It is not possible for a REDUCE to be followed by an error */
220974 assert( fts5yyact!=fts5YY_ERROR_ACTION )((void) (0));
220975
220976 fts5yymsp += fts5yysize+1;
220977 fts5yypParser->fts5yytos = fts5yymsp;
220978 fts5yymsp->stateno = (fts5YYACTIONTYPEunsigned char)fts5yyact;
220979 fts5yymsp->major = (fts5YYCODETYPEunsigned char)fts5yygoto;
220980 fts5yyTraceShift(fts5yypParser, fts5yyact, "... then shift");
220981 return fts5yyact;
220982}
220983
220984/*
220985** The following code executes when the parse fails
220986*/
220987#ifndef fts5YYNOERRORRECOVERY1
220988static void fts5yy_parse_failed(
220989 fts5yyParser *fts5yypParser /* The parser */
220990){
220991 sqlite3Fts5ParserARG_FETCHFts5Parse *pParse=fts5yypParser->pParse;
220992 sqlite3Fts5ParserCTX_FETCH
220993#ifndef NDEBUG1
220994 if( fts5yyTraceFILE ){
220995 fprintf(fts5yyTraceFILE,"%sFail!\n",fts5yyTracePrompt);
220996 }
220997#endif
220998 while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(fts5yypParser);
220999 /* Here code is inserted which will be executed whenever the
221000 ** parser fails */
221001/************ Begin %parse_failure code ***************************************/
221002/************ End %parse_failure code *****************************************/
221003 sqlite3Fts5ParserARG_STOREfts5yypParser->pParse=pParse; /* Suppress warning about unused %extra_argument variable */
221004 sqlite3Fts5ParserCTX_STORE
221005}
221006#endif /* fts5YYNOERRORRECOVERY */
221007
221008/*
221009** The following code executes when a syntax error first occurs.
221010*/
221011static void fts5yy_syntax_error(
221012 fts5yyParser *fts5yypParser, /* The parser */
221013 int fts5yymajor, /* The major type of the error token */
221014 sqlite3Fts5ParserFTS5TOKENTYPEFts5Token fts5yyminor /* The minor type of the error token */
221015){
221016 sqlite3Fts5ParserARG_FETCHFts5Parse *pParse=fts5yypParser->pParse;
221017 sqlite3Fts5ParserCTX_FETCH
221018#define FTS5TOKENfts5yyminor fts5yyminor
221019/************ Begin %syntax_error code ****************************************/
221020
221021 UNUSED_PARAM(fts5yymajor)(void)(fts5yymajor); /* Silence a compiler warning */
221022 sqlite3Fts5ParseError(
221023 pParse, "fts5: syntax error near \"%.*s\"",FTS5TOKENfts5yyminor.n,FTS5TOKENfts5yyminor.p
221024 );
221025/************ End %syntax_error code ******************************************/
221026 sqlite3Fts5ParserARG_STOREfts5yypParser->pParse=pParse; /* Suppress warning about unused %extra_argument variable */
221027 sqlite3Fts5ParserCTX_STORE
221028}
221029
221030/*
221031** The following is executed when the parser accepts
221032*/
221033static void fts5yy_accept(
221034 fts5yyParser *fts5yypParser /* The parser */
221035){
221036 sqlite3Fts5ParserARG_FETCHFts5Parse *pParse=fts5yypParser->pParse;
221037 sqlite3Fts5ParserCTX_FETCH
221038#ifndef NDEBUG1
221039 if( fts5yyTraceFILE ){
221040 fprintf(fts5yyTraceFILE,"%sAccept!\n",fts5yyTracePrompt);
221041 }
221042#endif
221043#ifndef fts5YYNOERRORRECOVERY1
221044 fts5yypParser->fts5yyerrcnt = -1;
221045#endif
221046 assert( fts5yypParser->fts5yytos==fts5yypParser->fts5yystack )((void) (0));
221047 /* Here code is inserted which will be executed whenever the
221048 ** parser accepts */
221049/*********** Begin %parse_accept code *****************************************/
221050/*********** End %parse_accept code *******************************************/
221051 sqlite3Fts5ParserARG_STOREfts5yypParser->pParse=pParse; /* Suppress warning about unused %extra_argument variable */
221052 sqlite3Fts5ParserCTX_STORE
221053}
221054
221055/* The main parser program.
221056** The first argument is a pointer to a structure obtained from
221057** "sqlite3Fts5ParserAlloc" which describes the current state of the parser.
221058** The second argument is the major token number. The third is
221059** the minor token. The fourth optional argument is whatever the
221060** user wants (and specified in the grammar) and is available for
221061** use by the action routines.
221062**
221063** Inputs:
221064** <ul>
221065** <li> A pointer to the parser (an opaque structure.)
221066** <li> The major token number.
221067** <li> The minor token number.
221068** <li> An option argument of a grammar-specified type.
221069** </ul>
221070**
221071** Outputs:
221072** None.
221073*/
221074static void sqlite3Fts5Parser(
221075 void *fts5yyp, /* The parser */
221076 int fts5yymajor, /* The major token code number */
221077 sqlite3Fts5ParserFTS5TOKENTYPEFts5Token fts5yyminor /* The value for the token */
221078 sqlite3Fts5ParserARG_PDECL,Fts5Parse *pParse /* Optional %extra_argument parameter */
221079){
221080 fts5YYMINORTYPE fts5yyminorunion;
221081 fts5YYACTIONTYPEunsigned char fts5yyact; /* The parser action. */
221082#if !defined(fts5YYERRORSYMBOL) && !defined(fts5YYNOERRORRECOVERY1)
221083 int fts5yyendofinput; /* True if we are at the end of input */
221084#endif
221085#ifdef fts5YYERRORSYMBOL
221086 int fts5yyerrorhit = 0; /* True if fts5yymajor has invoked an error */
221087#endif
221088 fts5yyParser *fts5yypParser = (fts5yyParser*)fts5yyp; /* The parser */
221089 sqlite3Fts5ParserCTX_FETCH
221090 sqlite3Fts5ParserARG_STOREfts5yypParser->pParse=pParse;
221091
221092 assert( fts5yypParser->fts5yytos!=0 )((void) (0));
221093#if !defined(fts5YYERRORSYMBOL) && !defined(fts5YYNOERRORRECOVERY1)
221094 fts5yyendofinput = (fts5yymajor==0);
221095#endif
221096
221097 fts5yyact = fts5yypParser->fts5yytos->stateno;
221098#ifndef NDEBUG1
221099 if( fts5yyTraceFILE ){
221100 if( fts5yyact < fts5YY_MIN_REDUCE83 ){
221101 fprintf(fts5yyTraceFILE,"%sInput '%s' in state %d\n",
221102 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor],fts5yyact);
221103 }else{
221104 fprintf(fts5yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
221105 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor],fts5yyact-fts5YY_MIN_REDUCE83);
221106 }
221107 }
221108#endif
221109
221110 while(1){ /* Exit by "break" */
221111 assert( fts5yypParser->fts5yytos>=fts5yypParser->fts5yystack )((void) (0));
221112 assert( fts5yyact==fts5yypParser->fts5yytos->stateno )((void) (0));
221113 fts5yyact = fts5yy_find_shift_action((fts5YYCODETYPEunsigned char)fts5yymajor,fts5yyact);
221114 if( fts5yyact >= fts5YY_MIN_REDUCE83 ){
221115 unsigned int fts5yyruleno = fts5yyact - fts5YY_MIN_REDUCE83; /* Reduce by this rule */
221116#ifndef NDEBUG1
221117 assert( fts5yyruleno<(int)(sizeof(fts5yyRuleName)/sizeof(fts5yyRuleName[0])) )((void) (0));
221118 if( fts5yyTraceFILE ){
221119 int fts5yysize = fts5yyRuleInfoNRhs[fts5yyruleno];
221120 if( fts5yysize ){
221121 fprintf(fts5yyTraceFILE, "%sReduce %d [%s]%s, pop back to state %d.\n",
221122 fts5yyTracePrompt,
221123 fts5yyruleno, fts5yyRuleName[fts5yyruleno],
221124 fts5yyruleno<fts5YYNRULE_WITH_ACTION28 ? "" : " without external action",
221125 fts5yypParser->fts5yytos[fts5yysize].stateno);
221126 }else{
221127 fprintf(fts5yyTraceFILE, "%sReduce %d [%s]%s.\n",
221128 fts5yyTracePrompt, fts5yyruleno, fts5yyRuleName[fts5yyruleno],
221129 fts5yyruleno<fts5YYNRULE_WITH_ACTION28 ? "" : " without external action");
221130 }
221131 }
221132#endif /* NDEBUG */
221133
221134 /* Check that the stack is large enough to grow by a single entry
221135 ** if the RHS of the rule is empty. This ensures that there is room
221136 ** enough on the stack to push the LHS value */
221137 if( fts5yyRuleInfoNRhs[fts5yyruleno]==0 ){
221138#ifdef fts5YYTRACKMAXSTACKDEPTH
221139 if( (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack)>fts5yypParser->fts5yyhwm ){
221140 fts5yypParser->fts5yyhwm++;
221141 assert( fts5yypParser->fts5yyhwm ==((void) (0))
221142 (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack))((void) (0));
221143 }
221144#endif
221145#if fts5YYSTACKDEPTH100>0
221146 if( fts5yypParser->fts5yytos>=fts5yypParser->fts5yystackEnd ){
221147 fts5yyStackOverflow(fts5yypParser);
221148 break;
221149 }
221150#else
221151 if( fts5yypParser->fts5yytos>=&fts5yypParser->fts5yystack[fts5yypParser->fts5yystksz-1] ){
221152 if( fts5yyGrowStack(fts5yypParser) ){
221153 fts5yyStackOverflow(fts5yypParser);
221154 break;
221155 }
221156 }
221157#endif
221158 }
221159 fts5yyact = fts5yy_reduce(fts5yypParser,fts5yyruleno,fts5yymajor,fts5yyminor sqlite3Fts5ParserCTX_PARAM);
221160 }else if( fts5yyact <= fts5YY_MAX_SHIFTREDUCE79 ){
221161 fts5yy_shift(fts5yypParser,fts5yyact,(fts5YYCODETYPEunsigned char)fts5yymajor,fts5yyminor);
221162#ifndef fts5YYNOERRORRECOVERY1
221163 fts5yypParser->fts5yyerrcnt--;
221164#endif
221165 break;
221166 }else if( fts5yyact==fts5YY_ACCEPT_ACTION81 ){
221167 fts5yypParser->fts5yytos--;
221168 fts5yy_accept(fts5yypParser);
221169 return;
221170 }else{
221171 assert( fts5yyact == fts5YY_ERROR_ACTION )((void) (0));
221172 fts5yyminorunion.fts5yy0 = fts5yyminor;
221173#ifdef fts5YYERRORSYMBOL
221174 int fts5yymx;
221175#endif
221176#ifndef NDEBUG1
221177 if( fts5yyTraceFILE ){
221178 fprintf(fts5yyTraceFILE,"%sSyntax Error!\n",fts5yyTracePrompt);
221179 }
221180#endif
221181#ifdef fts5YYERRORSYMBOL
221182 /* A syntax error has occurred.
221183 ** The response to an error depends upon whether or not the
221184 ** grammar defines an error token "ERROR".
221185 **
221186 ** This is what we do if the grammar does define ERROR:
221187 **
221188 ** * Call the %syntax_error function.
221189 **
221190 ** * Begin popping the stack until we enter a state where
221191 ** it is legal to shift the error symbol, then shift
221192 ** the error symbol.
221193 **
221194 ** * Set the error count to three.
221195 **
221196 ** * Begin accepting and shifting new tokens. No new error
221197 ** processing will occur until three tokens have been
221198 ** shifted successfully.
221199 **
221200 */
221201 if( fts5yypParser->fts5yyerrcnt<0 ){
221202 fts5yy_syntax_error(fts5yypParser,fts5yymajor,fts5yyminor);
221203 }
221204 fts5yymx = fts5yypParser->fts5yytos->major;
221205 if( fts5yymx==fts5YYERRORSYMBOL || fts5yyerrorhit ){
221206#ifndef NDEBUG1
221207 if( fts5yyTraceFILE ){
221208 fprintf(fts5yyTraceFILE,"%sDiscard input token %s\n",
221209 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor]);
221210 }
221211#endif
221212 fts5yy_destructor(fts5yypParser, (fts5YYCODETYPEunsigned char)fts5yymajor, &fts5yyminorunion);
221213 fts5yymajor = fts5YYNOCODE27;
221214 }else{
221215 while( fts5yypParser->fts5yytos > fts5yypParser->fts5yystack ){
221216 fts5yyact = fts5yy_find_reduce_action(fts5yypParser->fts5yytos->stateno,
221217 fts5YYERRORSYMBOL);
221218 if( fts5yyact<=fts5YY_MAX_SHIFTREDUCE79 ) break;
221219 fts5yy_pop_parser_stack(fts5yypParser);
221220 }
221221 if( fts5yypParser->fts5yytos <= fts5yypParser->fts5yystack || fts5yymajor==0 ){
221222 fts5yy_destructor(fts5yypParser,(fts5YYCODETYPEunsigned char)fts5yymajor,&fts5yyminorunion);
221223 fts5yy_parse_failed(fts5yypParser);
221224#ifndef fts5YYNOERRORRECOVERY1
221225 fts5yypParser->fts5yyerrcnt = -1;
221226#endif
221227 fts5yymajor = fts5YYNOCODE27;
221228 }else if( fts5yymx!=fts5YYERRORSYMBOL ){
221229 fts5yy_shift(fts5yypParser,fts5yyact,fts5YYERRORSYMBOL,fts5yyminor);
221230 }
221231 }
221232 fts5yypParser->fts5yyerrcnt = 3;
221233 fts5yyerrorhit = 1;
221234 if( fts5yymajor==fts5YYNOCODE27 ) break;
221235 fts5yyact = fts5yypParser->fts5yytos->stateno;
221236#elif defined(fts5YYNOERRORRECOVERY1)
221237 /* If the fts5YYNOERRORRECOVERY macro is defined, then do not attempt to
221238 ** do any kind of error recovery. Instead, simply invoke the syntax
221239 ** error routine and continue going as if nothing had happened.
221240 **
221241 ** Applications can set this macro (for example inside %include) if
221242 ** they intend to abandon the parse upon the first syntax error seen.
221243 */
221244 fts5yy_syntax_error(fts5yypParser,fts5yymajor, fts5yyminor);
221245 fts5yy_destructor(fts5yypParser,(fts5YYCODETYPEunsigned char)fts5yymajor,&fts5yyminorunion);
221246 break;
221247#else /* fts5YYERRORSYMBOL is not defined */
221248 /* This is what we do if the grammar does not define ERROR:
221249 **
221250 ** * Report an error message, and throw away the input token.
221251 **
221252 ** * If the input token is $, then fail the parse.
221253 **
221254 ** As before, subsequent error messages are suppressed until
221255 ** three input tokens have been successfully shifted.
221256 */
221257 if( fts5yypParser->fts5yyerrcnt<=0 ){
221258 fts5yy_syntax_error(fts5yypParser,fts5yymajor, fts5yyminor);
221259 }
221260 fts5yypParser->fts5yyerrcnt = 3;
221261 fts5yy_destructor(fts5yypParser,(fts5YYCODETYPEunsigned char)fts5yymajor,&fts5yyminorunion);
221262 if( fts5yyendofinput ){
221263 fts5yy_parse_failed(fts5yypParser);
221264#ifndef fts5YYNOERRORRECOVERY1
221265 fts5yypParser->fts5yyerrcnt = -1;
221266#endif
221267 }
221268 break;
221269#endif
221270 }
221271 }
221272#ifndef NDEBUG1
221273 if( fts5yyTraceFILE ){
221274 fts5yyStackEntry *i;
221275 char cDiv = '[';
221276 fprintf(fts5yyTraceFILE,"%sReturn. Stack=",fts5yyTracePrompt);
221277 for(i=&fts5yypParser->fts5yystack[1]; i<=fts5yypParser->fts5yytos; i++){
221278 fprintf(fts5yyTraceFILE,"%c%s", cDiv, fts5yyTokenName[i->major]);
221279 cDiv = ' ';
221280 }
221281 fprintf(fts5yyTraceFILE,"]\n");
221282 }
221283#endif
221284 return;
221285}
221286
221287/*
221288** Return the fallback token corresponding to canonical token iToken, or
221289** 0 if iToken has no fallback.
221290*/
221291static int sqlite3Fts5ParserFallback(int iToken){
221292#ifdef fts5YYFALLBACK
221293 assert( iToken<(int)(sizeof(fts5yyFallback)/sizeof(fts5yyFallback[0])) )((void) (0));
221294 return fts5yyFallback[iToken];
221295#else
221296 (void)iToken;
221297 return 0;
221298#endif
221299}
221300
221301/*
221302** 2014 May 31
221303**
221304** The author disclaims copyright to this source code. In place of
221305** a legal notice, here is a blessing:
221306**
221307** May you do good and not evil.
221308** May you find forgiveness for yourself and forgive others.
221309** May you share freely, never taking more than you give.
221310**
221311******************************************************************************
221312*/
221313
221314
221315/* #include "fts5Int.h" */
221316#include <math.h> /* amalgamator: keep */
221317
221318/*
221319** Object used to iterate through all "coalesced phrase instances" in
221320** a single column of the current row. If the phrase instances in the
221321** column being considered do not overlap, this object simply iterates
221322** through them. Or, if they do overlap (share one or more tokens in
221323** common), each set of overlapping instances is treated as a single
221324** match. See documentation for the highlight() auxiliary function for
221325** details.
221326**
221327** Usage is:
221328**
221329** for(rc = fts5CInstIterNext(pApi, pFts, iCol, &iter);
221330** (rc==SQLITE_OK && 0==fts5CInstIterEof(&iter);
221331** rc = fts5CInstIterNext(&iter)
221332** ){
221333** printf("instance starts at %d, ends at %d\n", iter.iStart, iter.iEnd);
221334** }
221335**
221336*/
221337typedef struct CInstIter CInstIter;
221338struct CInstIter {
221339 const Fts5ExtensionApi *pApi; /* API offered by current FTS version */
221340 Fts5Context *pFts; /* First arg to pass to pApi functions */
221341 int iCol; /* Column to search */
221342 int iInst; /* Next phrase instance index */
221343 int nInst; /* Total number of phrase instances */
221344
221345 /* Output variables */
221346 int iStart; /* First token in coalesced phrase instance */
221347 int iEnd; /* Last token in coalesced phrase instance */
221348};
221349
221350/*
221351** Advance the iterator to the next coalesced phrase instance. Return
221352** an SQLite error code if an error occurs, or SQLITE_OK otherwise.
221353*/
221354static int fts5CInstIterNext(CInstIter *pIter){
221355 int rc = SQLITE_OK0;
221356 pIter->iStart = -1;
221357 pIter->iEnd = -1;
221358
221359 while( rc==SQLITE_OK0 && pIter->iInst<pIter->nInst ){
221360 int ip; int ic; int io;
221361 rc = pIter->pApi->xInst(pIter->pFts, pIter->iInst, &ip, &ic, &io);
221362 if( rc==SQLITE_OK0 ){
221363 if( ic==pIter->iCol ){
221364 int iEnd = io - 1 + pIter->pApi->xPhraseSize(pIter->pFts, ip);
221365 if( pIter->iStart<0 ){
221366 pIter->iStart = io;
221367 pIter->iEnd = iEnd;
221368 }else if( io<=pIter->iEnd ){
221369 if( iEnd>pIter->iEnd ) pIter->iEnd = iEnd;
221370 }else{
221371 break;
221372 }
221373 }
221374 pIter->iInst++;
221375 }
221376 }
221377
221378 return rc;
221379}
221380
221381/*
221382** Initialize the iterator object indicated by the final parameter to
221383** iterate through coalesced phrase instances in column iCol.
221384*/
221385static int fts5CInstIterInit(
221386 const Fts5ExtensionApi *pApi,
221387 Fts5Context *pFts,
221388 int iCol,
221389 CInstIter *pIter
221390){
221391 int rc;
221392
221393 memset(pIter, 0, sizeof(CInstIter));
221394 pIter->pApi = pApi;
221395 pIter->pFts = pFts;
221396 pIter->iCol = iCol;
221397 rc = pApi->xInstCount(pFts, &pIter->nInst);
221398
221399 if( rc==SQLITE_OK0 ){
221400 rc = fts5CInstIterNext(pIter);
221401 }
221402
221403 return rc;
221404}
221405
221406
221407
221408/*************************************************************************
221409** Start of highlight() implementation.
221410*/
221411typedef struct HighlightContext HighlightContext;
221412struct HighlightContext {
221413 CInstIter iter; /* Coalesced Instance Iterator */
221414 int iPos; /* Current token offset in zIn[] */
221415 int iRangeStart; /* First token to include */
221416 int iRangeEnd; /* If non-zero, last token to include */
221417 const char *zOpen; /* Opening highlight */
221418 const char *zClose; /* Closing highlight */
221419 const char *zIn; /* Input text */
221420 int nIn; /* Size of input text in bytes */
221421 int iOff; /* Current offset within zIn[] */
221422 char *zOut; /* Output value */
221423};
221424
221425/*
221426** Append text to the HighlightContext output string - p->zOut. Argument
221427** z points to a buffer containing n bytes of text to append. If n is
221428** negative, everything up until the first '\0' is appended to the output.
221429**
221430** If *pRc is set to any value other than SQLITE_OK when this function is
221431** called, it is a no-op. If an error (i.e. an OOM condition) is encountered,
221432** *pRc is set to an error code before returning.
221433*/
221434static void fts5HighlightAppend(
221435 int *pRc,
221436 HighlightContext *p,
221437 const char *z, int n
221438){
221439 if( *pRc==SQLITE_OK0 && z ){
221440 if( n<0 ) n = (int)strlen(z);
221441 p->zOut = sqlite3_mprintf("%z%.*s", p->zOut, n, z);
221442 if( p->zOut==0 ) *pRc = SQLITE_NOMEM7;
221443 }
221444}
221445
221446/*
221447** Tokenizer callback used by implementation of highlight() function.
221448*/
221449static int fts5HighlightCb(
221450 void *pContext, /* Pointer to HighlightContext object */
221451 int tflags, /* Mask of FTS5_TOKEN_* flags */
221452 const char *pToken, /* Buffer containing token */
221453 int nToken, /* Size of token in bytes */
221454 int iStartOff, /* Start offset of token */
221455 int iEndOff /* End offset of token */
221456){
221457 HighlightContext *p = (HighlightContext*)pContext;
221458 int rc = SQLITE_OK0;
221459 int iPos;
221460
221461 UNUSED_PARAM2(pToken, nToken)(void)(pToken), (void)(nToken);
221462
221463 if( tflags & FTS5_TOKEN_COLOCATED0x0001 ) return SQLITE_OK0;
221464 iPos = p->iPos++;
221465
221466 if( p->iRangeEnd>0 ){
221467 if( iPos<p->iRangeStart || iPos>p->iRangeEnd ) return SQLITE_OK0;
221468 if( p->iRangeStart && iPos==p->iRangeStart ) p->iOff = iStartOff;
221469 }
221470
221471 if( iPos==p->iter.iStart ){
221472 fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iStartOff - p->iOff);
221473 fts5HighlightAppend(&rc, p, p->zOpen, -1);
221474 p->iOff = iStartOff;
221475 }
221476
221477 if( iPos==p->iter.iEnd ){
221478 if( p->iRangeEnd && p->iter.iStart<p->iRangeStart ){
221479 fts5HighlightAppend(&rc, p, p->zOpen, -1);
221480 }
221481 fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff);
221482 fts5HighlightAppend(&rc, p, p->zClose, -1);
221483 p->iOff = iEndOff;
221484 if( rc==SQLITE_OK0 ){
221485 rc = fts5CInstIterNext(&p->iter);
221486 }
221487 }
221488
221489 if( p->iRangeEnd>0 && iPos==p->iRangeEnd ){
221490 fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff);
221491 p->iOff = iEndOff;
221492 if( iPos>=p->iter.iStart && iPos<p->iter.iEnd ){
221493 fts5HighlightAppend(&rc, p, p->zClose, -1);
221494 }
221495 }
221496
221497 return rc;
221498}
221499
221500/*
221501** Implementation of highlight() function.
221502*/
221503static void fts5HighlightFunction(
221504 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
221505 Fts5Context *pFts, /* First arg to pass to pApi functions */
221506 sqlite3_context *pCtx, /* Context for returning result/error */
221507 int nVal, /* Number of values in apVal[] array */
221508 sqlite3_value **apVal /* Array of trailing arguments */
221509){
221510 HighlightContext ctx;
221511 int rc;
221512 int iCol;
221513
221514 if( nVal!=3 ){
221515 const char *zErr = "wrong number of arguments to function highlight()";
221516 sqlite3_result_error(pCtx, zErr, -1);
221517 return;
221518 }
221519
221520 iCol = sqlite3_value_int(apVal[0]);
221521 memset(&ctx, 0, sizeof(HighlightContext));
221522 ctx.zOpen = (const char*)sqlite3_value_text(apVal[1]);
221523 ctx.zClose = (const char*)sqlite3_value_text(apVal[2]);
221524 rc = pApi->xColumnText(pFts, iCol, &ctx.zIn, &ctx.nIn);
221525
221526 if( ctx.zIn ){
221527 if( rc==SQLITE_OK0 ){
221528 rc = fts5CInstIterInit(pApi, pFts, iCol, &ctx.iter);
221529 }
221530
221531 if( rc==SQLITE_OK0 ){
221532 rc = pApi->xTokenize(pFts, ctx.zIn, ctx.nIn, (void*)&ctx,fts5HighlightCb);
221533 }
221534 fts5HighlightAppend(&rc, &ctx, &ctx.zIn[ctx.iOff], ctx.nIn - ctx.iOff);
221535
221536 if( rc==SQLITE_OK0 ){
221537 sqlite3_result_text(pCtx, (const char*)ctx.zOut, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
221538 }
221539 sqlite3_free(ctx.zOut);
221540 }
221541 if( rc!=SQLITE_OK0 ){
221542 sqlite3_result_error_code(pCtx, rc);
221543 }
221544}
221545/*
221546** End of highlight() implementation.
221547**************************************************************************/
221548
221549/*
221550** Context object passed to the fts5SentenceFinderCb() function.
221551*/
221552typedef struct Fts5SFinder Fts5SFinder;
221553struct Fts5SFinder {
221554 int iPos; /* Current token position */
221555 int nFirstAlloc; /* Allocated size of aFirst[] */
221556 int nFirst; /* Number of entries in aFirst[] */
221557 int *aFirst; /* Array of first token in each sentence */
221558 const char *zDoc; /* Document being tokenized */
221559};
221560
221561/*
221562** Add an entry to the Fts5SFinder.aFirst[] array. Grow the array if
221563** necessary. Return SQLITE_OK if successful, or SQLITE_NOMEM if an
221564** error occurs.
221565*/
221566static int fts5SentenceFinderAdd(Fts5SFinder *p, int iAdd){
221567 if( p->nFirstAlloc==p->nFirst ){
221568 int nNew = p->nFirstAlloc ? p->nFirstAlloc*2 : 64;
221569 int *aNew;
221570
221571 aNew = (int*)sqlite3_realloc64(p->aFirst, nNew*sizeof(int));
221572 if( aNew==0 ) return SQLITE_NOMEM7;
221573 p->aFirst = aNew;
221574 p->nFirstAlloc = nNew;
221575 }
221576 p->aFirst[p->nFirst++] = iAdd;
221577 return SQLITE_OK0;
221578}
221579
221580/*
221581** This function is an xTokenize() callback used by the auxiliary snippet()
221582** function. Its job is to identify tokens that are the first in a sentence.
221583** For each such token, an entry is added to the SFinder.aFirst[] array.
221584*/
221585static int fts5SentenceFinderCb(
221586 void *pContext, /* Pointer to HighlightContext object */
221587 int tflags, /* Mask of FTS5_TOKEN_* flags */
221588 const char *pToken, /* Buffer containing token */
221589 int nToken, /* Size of token in bytes */
221590 int iStartOff, /* Start offset of token */
221591 int iEndOff /* End offset of token */
221592){
221593 int rc = SQLITE_OK0;
221594
221595 UNUSED_PARAM2(pToken, nToken)(void)(pToken), (void)(nToken);
221596 UNUSED_PARAM(iEndOff)(void)(iEndOff);
221597
221598 if( (tflags & FTS5_TOKEN_COLOCATED0x0001)==0 ){
221599 Fts5SFinder *p = (Fts5SFinder*)pContext;
221600 if( p->iPos>0 ){
221601 int i;
221602 char c = 0;
221603 for(i=iStartOff-1; i>=0; i--){
221604 c = p->zDoc[i];
221605 if( c!=' ' && c!='\t' && c!='\n' && c!='\r' ) break;
221606 }
221607 if( i!=iStartOff-1 && (c=='.' || c==':') ){
221608 rc = fts5SentenceFinderAdd(p, p->iPos);
221609 }
221610 }else{
221611 rc = fts5SentenceFinderAdd(p, 0);
221612 }
221613 p->iPos++;
221614 }
221615 return rc;
221616}
221617
221618static int fts5SnippetScore(
221619 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
221620 Fts5Context *pFts, /* First arg to pass to pApi functions */
221621 int nDocsize, /* Size of column in tokens */
221622 unsigned char *aSeen, /* Array with one element per query phrase */
221623 int iCol, /* Column to score */
221624 int iPos, /* Starting offset to score */
221625 int nToken, /* Max tokens per snippet */
221626 int *pnScore, /* OUT: Score */
221627 int *piPos /* OUT: Adjusted offset */
221628){
221629 int rc;
221630 int i;
221631 int ip = 0;
221632 int ic = 0;
221633 int iOff = 0;
221634 int iFirst = -1;
221635 int nInst;
221636 int nScore = 0;
221637 int iLast = 0;
221638 sqlite3_int64 iEnd = (sqlite3_int64)iPos + nToken;
221639
221640 rc = pApi->xInstCount(pFts, &nInst);
221641 for(i=0; i<nInst && rc==SQLITE_OK0; i++){
221642 rc = pApi->xInst(pFts, i, &ip, &ic, &iOff);
221643 if( rc==SQLITE_OK0 && ic==iCol && iOff>=iPos && iOff<iEnd ){
221644 nScore += (aSeen[ip] ? 1 : 1000);
221645 aSeen[ip] = 1;
221646 if( iFirst<0 ) iFirst = iOff;
221647 iLast = iOff + pApi->xPhraseSize(pFts, ip);
221648 }
221649 }
221650
221651 *pnScore = nScore;
221652 if( piPos ){
221653 sqlite3_int64 iAdj = iFirst - (nToken - (iLast-iFirst)) / 2;
221654 if( (iAdj+nToken)>nDocsize ) iAdj = nDocsize - nToken;
221655 if( iAdj<0 ) iAdj = 0;
221656 *piPos = (int)iAdj;
221657 }
221658
221659 return rc;
221660}
221661
221662/*
221663** Return the value in pVal interpreted as utf-8 text. Except, if pVal
221664** contains a NULL value, return a pointer to a static string zero
221665** bytes in length instead of a NULL pointer.
221666*/
221667static const char *fts5ValueToText(sqlite3_value *pVal){
221668 const char *zRet = (const char*)sqlite3_value_text(pVal);
221669 return zRet ? zRet : "";
221670}
221671
221672/*
221673** Implementation of snippet() function.
221674*/
221675static void fts5SnippetFunction(
221676 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
221677 Fts5Context *pFts, /* First arg to pass to pApi functions */
221678 sqlite3_context *pCtx, /* Context for returning result/error */
221679 int nVal, /* Number of values in apVal[] array */
221680 sqlite3_value **apVal /* Array of trailing arguments */
221681){
221682 HighlightContext ctx;
221683 int rc = SQLITE_OK0; /* Return code */
221684 int iCol; /* 1st argument to snippet() */
221685 const char *zEllips; /* 4th argument to snippet() */
221686 int nToken; /* 5th argument to snippet() */
221687 int nInst = 0; /* Number of instance matches this row */
221688 int i; /* Used to iterate through instances */
221689 int nPhrase; /* Number of phrases in query */
221690 unsigned char *aSeen; /* Array of "seen instance" flags */
221691 int iBestCol; /* Column containing best snippet */
221692 int iBestStart = 0; /* First token of best snippet */
221693 int nBestScore = 0; /* Score of best snippet */
221694 int nColSize = 0; /* Total size of iBestCol in tokens */
221695 Fts5SFinder sFinder; /* Used to find the beginnings of sentences */
221696 int nCol;
221697
221698 if( nVal!=5 ){
221699 const char *zErr = "wrong number of arguments to function snippet()";
221700 sqlite3_result_error(pCtx, zErr, -1);
221701 return;
221702 }
221703
221704 nCol = pApi->xColumnCount(pFts);
221705 memset(&ctx, 0, sizeof(HighlightContext));
221706 iCol = sqlite3_value_int(apVal[0]);
221707 ctx.zOpen = fts5ValueToText(apVal[1]);
221708 ctx.zClose = fts5ValueToText(apVal[2]);
221709 zEllips = fts5ValueToText(apVal[3]);
221710 nToken = sqlite3_value_int(apVal[4]);
221711
221712 iBestCol = (iCol>=0 ? iCol : 0);
221713 nPhrase = pApi->xPhraseCount(pFts);
221714 aSeen = sqlite3_malloc(nPhrase);
221715 if( aSeen==0 ){
221716 rc = SQLITE_NOMEM7;
221717 }
221718 if( rc==SQLITE_OK0 ){
221719 rc = pApi->xInstCount(pFts, &nInst);
221720 }
221721
221722 memset(&sFinder, 0, sizeof(Fts5SFinder));
221723 for(i=0; i<nCol; i++){
221724 if( iCol<0 || iCol==i ){
221725 int nDoc;
221726 int nDocsize;
221727 int ii;
221728 sFinder.iPos = 0;
221729 sFinder.nFirst = 0;
221730 rc = pApi->xColumnText(pFts, i, &sFinder.zDoc, &nDoc);
221731 if( rc!=SQLITE_OK0 ) break;
221732 rc = pApi->xTokenize(pFts,
221733 sFinder.zDoc, nDoc, (void*)&sFinder,fts5SentenceFinderCb
221734 );
221735 if( rc!=SQLITE_OK0 ) break;
221736 rc = pApi->xColumnSize(pFts, i, &nDocsize);
221737 if( rc!=SQLITE_OK0 ) break;
221738
221739 for(ii=0; rc==SQLITE_OK0 && ii<nInst; ii++){
221740 int ip, ic, io;
221741 int iAdj;
221742 int nScore;
221743 int jj;
221744
221745 rc = pApi->xInst(pFts, ii, &ip, &ic, &io);
221746 if( ic!=i ) continue;
221747 if( io>nDocsize ) rc = FTS5_CORRUPT(11 | (1<<8));
221748 if( rc!=SQLITE_OK0 ) continue;
221749 memset(aSeen, 0, nPhrase);
221750 rc = fts5SnippetScore(pApi, pFts, nDocsize, aSeen, i,
221751 io, nToken, &nScore, &iAdj
221752 );
221753 if( rc==SQLITE_OK0 && nScore>nBestScore ){
221754 nBestScore = nScore;
221755 iBestCol = i;
221756 iBestStart = iAdj;
221757 nColSize = nDocsize;
221758 }
221759
221760 if( rc==SQLITE_OK0 && sFinder.nFirst && nDocsize>nToken ){
221761 for(jj=0; jj<(sFinder.nFirst-1); jj++){
221762 if( sFinder.aFirst[jj+1]>io ) break;
221763 }
221764
221765 if( sFinder.aFirst[jj]<io ){
221766 memset(aSeen, 0, nPhrase);
221767 rc = fts5SnippetScore(pApi, pFts, nDocsize, aSeen, i,
221768 sFinder.aFirst[jj], nToken, &nScore, 0
221769 );
221770
221771 nScore += (sFinder.aFirst[jj]==0 ? 120 : 100);
221772 if( rc==SQLITE_OK0 && nScore>nBestScore ){
221773 nBestScore = nScore;
221774 iBestCol = i;
221775 iBestStart = sFinder.aFirst[jj];
221776 nColSize = nDocsize;
221777 }
221778 }
221779 }
221780 }
221781 }
221782 }
221783
221784 if( rc==SQLITE_OK0 ){
221785 rc = pApi->xColumnText(pFts, iBestCol, &ctx.zIn, &ctx.nIn);
221786 }
221787 if( rc==SQLITE_OK0 && nColSize==0 ){
221788 rc = pApi->xColumnSize(pFts, iBestCol, &nColSize);
221789 }
221790 if( ctx.zIn ){
221791 if( rc==SQLITE_OK0 ){
221792 rc = fts5CInstIterInit(pApi, pFts, iBestCol, &ctx.iter);
221793 }
221794
221795 ctx.iRangeStart = iBestStart;
221796 ctx.iRangeEnd = iBestStart + nToken - 1;
221797
221798 if( iBestStart>0 ){
221799 fts5HighlightAppend(&rc, &ctx, zEllips, -1);
221800 }
221801
221802 /* Advance iterator ctx.iter so that it points to the first coalesced
221803 ** phrase instance at or following position iBestStart. */
221804 while( ctx.iter.iStart>=0 && ctx.iter.iStart<iBestStart && rc==SQLITE_OK0 ){
221805 rc = fts5CInstIterNext(&ctx.iter);
221806 }
221807
221808 if( rc==SQLITE_OK0 ){
221809 rc = pApi->xTokenize(pFts, ctx.zIn, ctx.nIn, (void*)&ctx,fts5HighlightCb);
221810 }
221811 if( ctx.iRangeEnd>=(nColSize-1) ){
221812 fts5HighlightAppend(&rc, &ctx, &ctx.zIn[ctx.iOff], ctx.nIn - ctx.iOff);
221813 }else{
221814 fts5HighlightAppend(&rc, &ctx, zEllips, -1);
221815 }
221816 }
221817 if( rc==SQLITE_OK0 ){
221818 sqlite3_result_text(pCtx, (const char*)ctx.zOut, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
221819 }else{
221820 sqlite3_result_error_code(pCtx, rc);
221821 }
221822 sqlite3_free(ctx.zOut);
221823 sqlite3_free(aSeen);
221824 sqlite3_free(sFinder.aFirst);
221825}
221826
221827/************************************************************************/
221828
221829/*
221830** The first time the bm25() function is called for a query, an instance
221831** of the following structure is allocated and populated.
221832*/
221833typedef struct Fts5Bm25Data Fts5Bm25Data;
221834struct Fts5Bm25Data {
221835 int nPhrase; /* Number of phrases in query */
221836 double avgdl; /* Average number of tokens in each row */
221837 double *aIDF; /* IDF for each phrase */
221838 double *aFreq; /* Array used to calculate phrase freq. */
221839};
221840
221841/*
221842** Callback used by fts5Bm25GetData() to count the number of rows in the
221843** table matched by each individual phrase within the query.
221844*/
221845static int fts5CountCb(
221846 const Fts5ExtensionApi *pApi,
221847 Fts5Context *pFts,
221848 void *pUserData /* Pointer to sqlite3_int64 variable */
221849){
221850 sqlite3_int64 *pn = (sqlite3_int64*)pUserData;
221851 UNUSED_PARAM2(pApi, pFts)(void)(pApi), (void)(pFts);
221852 (*pn)++;
221853 return SQLITE_OK0;
221854}
221855
221856/*
221857** Set *ppData to point to the Fts5Bm25Data object for the current query.
221858** If the object has not already been allocated, allocate and populate it
221859** now.
221860*/
221861static int fts5Bm25GetData(
221862 const Fts5ExtensionApi *pApi,
221863 Fts5Context *pFts,
221864 Fts5Bm25Data **ppData /* OUT: bm25-data object for this query */
221865){
221866 int rc = SQLITE_OK0; /* Return code */
221867 Fts5Bm25Data *p; /* Object to return */
221868
221869 p = (Fts5Bm25Data*)pApi->xGetAuxdata(pFts, 0);
221870 if( p==0 ){
221871 int nPhrase; /* Number of phrases in query */
221872 sqlite3_int64 nRow = 0; /* Number of rows in table */
221873 sqlite3_int64 nToken = 0; /* Number of tokens in table */
221874 sqlite3_int64 nByte; /* Bytes of space to allocate */
221875 int i;
221876
221877 /* Allocate the Fts5Bm25Data object */
221878 nPhrase = pApi->xPhraseCount(pFts);
221879 nByte = sizeof(Fts5Bm25Data) + nPhrase*2*sizeof(double);
221880 p = (Fts5Bm25Data*)sqlite3_malloc64(nByte);
221881 if( p==0 ){
221882 rc = SQLITE_NOMEM7;
221883 }else{
221884 memset(p, 0, (size_t)nByte);
221885 p->nPhrase = nPhrase;
221886 p->aIDF = (double*)&p[1];
221887 p->aFreq = &p->aIDF[nPhrase];
221888 }
221889
221890 /* Calculate the average document length for this FTS5 table */
221891 if( rc==SQLITE_OK0 ) rc = pApi->xRowCount(pFts, &nRow);
221892 assert( rc!=SQLITE_OK || nRow>0 )((void) (0));
221893 if( rc==SQLITE_OK0 ) rc = pApi->xColumnTotalSize(pFts, -1, &nToken);
221894 if( rc==SQLITE_OK0 ) p->avgdl = (double)nToken / (double)nRow;
221895
221896 /* Calculate an IDF for each phrase in the query */
221897 for(i=0; rc==SQLITE_OK0 && i<nPhrase; i++){
221898 sqlite3_int64 nHit = 0;
221899 rc = pApi->xQueryPhrase(pFts, i, (void*)&nHit, fts5CountCb);
221900 if( rc==SQLITE_OK0 ){
221901 /* Calculate the IDF (Inverse Document Frequency) for phrase i.
221902 ** This is done using the standard BM25 formula as found on wikipedia:
221903 **
221904 ** IDF = log( (N - nHit + 0.5) / (nHit + 0.5) )
221905 **
221906 ** where "N" is the total number of documents in the set and nHit
221907 ** is the number that contain at least one instance of the phrase
221908 ** under consideration.
221909 **
221910 ** The problem with this is that if (N < 2*nHit), the IDF is
221911 ** negative. Which is undesirable. So the mimimum allowable IDF is
221912 ** (1e-6) - roughly the same as a term that appears in just over
221913 ** half of set of 5,000,000 documents. */
221914 double idf = log( (nRow - nHit + 0.5) / (nHit + 0.5) );
221915 if( idf<=0.0 ) idf = 1e-6;
221916 p->aIDF[i] = idf;
221917 }
221918 }
221919
221920 if( rc!=SQLITE_OK0 ){
221921 sqlite3_free(p);
221922 }else{
221923 rc = pApi->xSetAuxdata(pFts, p, sqlite3_free);
221924 }
221925 if( rc!=SQLITE_OK0 ) p = 0;
221926 }
221927 *ppData = p;
221928 return rc;
221929}
221930
221931/*
221932** Implementation of bm25() function.
221933*/
221934static void fts5Bm25Function(
221935 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
221936 Fts5Context *pFts, /* First arg to pass to pApi functions */
221937 sqlite3_context *pCtx, /* Context for returning result/error */
221938 int nVal, /* Number of values in apVal[] array */
221939 sqlite3_value **apVal /* Array of trailing arguments */
221940){
221941 const double k1 = 1.2; /* Constant "k1" from BM25 formula */
221942 const double b = 0.75; /* Constant "b" from BM25 formula */
221943 int rc; /* Error code */
221944 double score = 0.0; /* SQL function return value */
221945 Fts5Bm25Data *pData; /* Values allocated/calculated once only */
221946 int i; /* Iterator variable */
221947 int nInst = 0; /* Value returned by xInstCount() */
221948 double D = 0.0; /* Total number of tokens in row */
221949 double *aFreq = 0; /* Array of phrase freq. for current row */
221950
221951 /* Calculate the phrase frequency (symbol "f(qi,D)" in the documentation)
221952 ** for each phrase in the query for the current row. */
221953 rc = fts5Bm25GetData(pApi, pFts, &pData);
221954 if( rc==SQLITE_OK0 ){
221955 aFreq = pData->aFreq;
221956 memset(aFreq, 0, sizeof(double) * pData->nPhrase);
221957 rc = pApi->xInstCount(pFts, &nInst);
221958 }
221959 for(i=0; rc==SQLITE_OK0 && i<nInst; i++){
221960 int ip; int ic; int io;
221961 rc = pApi->xInst(pFts, i, &ip, &ic, &io);
221962 if( rc==SQLITE_OK0 ){
221963 double w = (nVal > ic) ? sqlite3_value_double(apVal[ic]) : 1.0;
221964 aFreq[ip] += w;
221965 }
221966 }
221967
221968 /* Figure out the total size of the current row in tokens. */
221969 if( rc==SQLITE_OK0 ){
221970 int nTok;
221971 rc = pApi->xColumnSize(pFts, -1, &nTok);
221972 D = (double)nTok;
221973 }
221974
221975 /* Determine and return the BM25 score for the current row. Or, if an
221976 ** error has occurred, throw an exception. */
221977 if( rc==SQLITE_OK0 ){
221978 for(i=0; i<pData->nPhrase; i++){
221979 score += pData->aIDF[i] * (
221980 ( aFreq[i] * (k1 + 1.0) ) /
221981 ( aFreq[i] + k1 * (1 - b + b * D / pData->avgdl) )
221982 );
221983 }
221984 sqlite3_result_double(pCtx, -1.0 * score);
221985 }else{
221986 sqlite3_result_error_code(pCtx, rc);
221987 }
221988}
221989
221990static int sqlite3Fts5AuxInit(fts5_api *pApi){
221991 struct Builtin {
221992 const char *zFunc; /* Function name (nul-terminated) */
221993 void *pUserData; /* User-data pointer */
221994 fts5_extension_function xFunc;/* Callback function */
221995 void (*xDestroy)(void*); /* Destructor function */
221996 } aBuiltin [] = {
221997 { "snippet", 0, fts5SnippetFunction, 0 },
221998 { "highlight", 0, fts5HighlightFunction, 0 },
221999 { "bm25", 0, fts5Bm25Function, 0 },
222000 };
222001 int rc = SQLITE_OK0; /* Return code */
222002 int i; /* To iterate through builtin functions */
222003
222004 for(i=0; rc==SQLITE_OK0 && i<ArraySize(aBuiltin)((int)(sizeof(aBuiltin)/sizeof(aBuiltin[0]))); i++){
222005 rc = pApi->xCreateFunction(pApi,
222006 aBuiltin[i].zFunc,
222007 aBuiltin[i].pUserData,
222008 aBuiltin[i].xFunc,
222009 aBuiltin[i].xDestroy
222010 );
222011 }
222012
222013 return rc;
222014}
222015
222016/*
222017** 2014 May 31
222018**
222019** The author disclaims copyright to this source code. In place of
222020** a legal notice, here is a blessing:
222021**
222022** May you do good and not evil.
222023** May you find forgiveness for yourself and forgive others.
222024** May you share freely, never taking more than you give.
222025**
222026******************************************************************************
222027*/
222028
222029
222030
222031/* #include "fts5Int.h" */
222032
222033static int sqlite3Fts5BufferSize(int *pRc, Fts5Buffer *pBuf, u32 nByte){
222034 if( (u32)pBuf->nSpace<nByte ){
222035 u64 nNew = pBuf->nSpace ? pBuf->nSpace : 64;
222036 u8 *pNew;
222037 while( nNew<nByte ){
222038 nNew = nNew * 2;
222039 }
222040 pNew = sqlite3_realloc64(pBuf->p, nNew);
222041 if( pNew==0 ){
222042 *pRc = SQLITE_NOMEM7;
222043 return 1;
222044 }else{
222045 pBuf->nSpace = (int)nNew;
222046 pBuf->p = pNew;
222047 }
222048 }
222049 return 0;
222050}
222051
222052
222053/*
222054** Encode value iVal as an SQLite varint and append it to the buffer object
222055** pBuf. If an OOM error occurs, set the error code in p.
222056*/
222057static void sqlite3Fts5BufferAppendVarint(int *pRc, Fts5Buffer *pBuf, i64 iVal){
222058 if( fts5BufferGrow(pRc, pBuf, 9)( (u32)((pBuf)->n) + (u32)(9) <= (u32)((pBuf)->nSpace
) ? 0 : sqlite3Fts5BufferSize((pRc),(pBuf),(9)+(pBuf)->n) )
) return;
222059 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iVal);
222060}
222061
222062static void sqlite3Fts5Put32(u8 *aBuf, int iVal){
222063 aBuf[0] = (iVal>>24) & 0x00FF;
222064 aBuf[1] = (iVal>>16) & 0x00FF;
222065 aBuf[2] = (iVal>> 8) & 0x00FF;
222066 aBuf[3] = (iVal>> 0) & 0x00FF;
222067}
222068
222069static int sqlite3Fts5Get32(const u8 *aBuf){
222070 return (int)((((u32)aBuf[0])<<24) + (aBuf[1]<<16) + (aBuf[2]<<8) + aBuf[3]);
222071}
222072
222073/*
222074** Append buffer nData/pData to buffer pBuf. If an OOM error occurs, set
222075** the error code in p. If an error has already occurred when this function
222076** is called, it is a no-op.
222077*/
222078static void sqlite3Fts5BufferAppendBlob(
222079 int *pRc,
222080 Fts5Buffer *pBuf,
222081 u32 nData,
222082 const u8 *pData
222083){
222084 if( nData ){
222085 if( fts5BufferGrow(pRc, pBuf, nData)( (u32)((pBuf)->n) + (u32)(nData) <= (u32)((pBuf)->nSpace
) ? 0 : sqlite3Fts5BufferSize((pRc),(pBuf),(nData)+(pBuf)->
n) )
) return;
222086 memcpy(&pBuf->p[pBuf->n], pData, nData);
222087 pBuf->n += nData;
222088 }
222089}
222090
222091/*
222092** Append the nul-terminated string zStr to the buffer pBuf. This function
222093** ensures that the byte following the buffer data is set to 0x00, even
222094** though this byte is not included in the pBuf->n count.
222095*/
222096static void sqlite3Fts5BufferAppendString(
222097 int *pRc,
222098 Fts5Buffer *pBuf,
222099 const char *zStr
222100){
222101 int nStr = (int)strlen(zStr);
222102 sqlite3Fts5BufferAppendBlob(pRc, pBuf, nStr+1, (const u8*)zStr);
222103 pBuf->n--;
222104}
222105
222106/*
222107** Argument zFmt is a printf() style format string. This function performs
222108** the printf() style processing, then appends the results to buffer pBuf.
222109**
222110** Like sqlite3Fts5BufferAppendString(), this function ensures that the byte
222111** following the buffer data is set to 0x00, even though this byte is not
222112** included in the pBuf->n count.
222113*/
222114static void sqlite3Fts5BufferAppendPrintf(
222115 int *pRc,
222116 Fts5Buffer *pBuf,
222117 char *zFmt, ...
222118){
222119 if( *pRc==SQLITE_OK0 ){
222120 char *zTmp;
222121 va_list ap;
222122 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
222123 zTmp = sqlite3_vmprintf(zFmt, ap);
222124 va_end(ap)__builtin_va_end(ap);
222125
222126 if( zTmp==0 ){
222127 *pRc = SQLITE_NOMEM7;
222128 }else{
222129 sqlite3Fts5BufferAppendString(pRc, pBuf, zTmp);
222130 sqlite3_free(zTmp);
222131 }
222132 }
222133}
222134
222135static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...){
222136 char *zRet = 0;
222137 if( *pRc==SQLITE_OK0 ){
222138 va_list ap;
222139 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
222140 zRet = sqlite3_vmprintf(zFmt, ap);
222141 va_end(ap)__builtin_va_end(ap);
222142 if( zRet==0 ){
222143 *pRc = SQLITE_NOMEM7;
222144 }
222145 }
222146 return zRet;
222147}
222148
222149
222150/*
222151** Free any buffer allocated by pBuf. Zero the structure before returning.
222152*/
222153static void sqlite3Fts5BufferFree(Fts5Buffer *pBuf){
222154 sqlite3_free(pBuf->p);
222155 memset(pBuf, 0, sizeof(Fts5Buffer));
222156}
222157
222158/*
222159** Zero the contents of the buffer object. But do not free the associated
222160** memory allocation.
222161*/
222162static void sqlite3Fts5BufferZero(Fts5Buffer *pBuf){
222163 pBuf->n = 0;
222164}
222165
222166/*
222167** Set the buffer to contain nData/pData. If an OOM error occurs, leave an
222168** the error code in p. If an error has already occurred when this function
222169** is called, it is a no-op.
222170*/
222171static void sqlite3Fts5BufferSet(
222172 int *pRc,
222173 Fts5Buffer *pBuf,
222174 int nData,
222175 const u8 *pData
222176){
222177 pBuf->n = 0;
222178 sqlite3Fts5BufferAppendBlob(pRc, pBuf, nData, pData);
222179}
222180
222181static int sqlite3Fts5PoslistNext64(
222182 const u8 *a, int n, /* Buffer containing poslist */
222183 int *pi, /* IN/OUT: Offset within a[] */
222184 i64 *piOff /* IN/OUT: Current offset */
222185){
222186 int i = *pi;
222187 if( i>=n ){
222188 /* EOF */
222189 *piOff = -1;
222190 return 1;
222191 }else{
222192 i64 iOff = *piOff;
222193 u32 iVal;
222194 fts5FastGetVarint32(a, i, iVal){ iVal = (a)[i++]; if( iVal & 0x80 ){ i--; i += sqlite3Fts5GetVarint32
(&(a)[i],(u32*)&iVal); } }
;
222195 if( iVal<=1 ){
222196 if( iVal==0 ){
222197 *pi = i;
222198 return 0;
222199 }
222200 fts5FastGetVarint32(a, i, iVal){ iVal = (a)[i++]; if( iVal & 0x80 ){ i--; i += sqlite3Fts5GetVarint32
(&(a)[i],(u32*)&iVal); } }
;
222201 iOff = ((i64)iVal) << 32;
222202 assert( iOff>=0 )((void) (0));
222203 fts5FastGetVarint32(a, i, iVal){ iVal = (a)[i++]; if( iVal & 0x80 ){ i--; i += sqlite3Fts5GetVarint32
(&(a)[i],(u32*)&iVal); } }
;
222204 if( iVal<2 ){
222205 /* This is a corrupt record. So stop parsing it here. */
222206 *piOff = -1;
222207 return 1;
222208 }
222209 *piOff = iOff + ((iVal-2) & 0x7FFFFFFF);
222210 }else{
222211 *piOff = (iOff & (i64)0x7FFFFFFF<<32)+((iOff + (iVal-2)) & 0x7FFFFFFF);
222212 }
222213 *pi = i;
222214 assert_nc( *piOff>=iOff )((void) (0));
222215 return 0;
222216 }
222217}
222218
222219
222220/*
222221** Advance the iterator object passed as the only argument. Return true
222222** if the iterator reaches EOF, or false otherwise.
222223*/
222224static int sqlite3Fts5PoslistReaderNext(Fts5PoslistReader *pIter){
222225 if( sqlite3Fts5PoslistNext64(pIter->a, pIter->n, &pIter->i, &pIter->iPos) ){
222226 pIter->bEof = 1;
222227 }
222228 return pIter->bEof;
222229}
222230
222231static int sqlite3Fts5PoslistReaderInit(
222232 const u8 *a, int n, /* Poslist buffer to iterate through */
222233 Fts5PoslistReader *pIter /* Iterator object to initialize */
222234){
222235 memset(pIter, 0, sizeof(*pIter));
222236 pIter->a = a;
222237 pIter->n = n;
222238 sqlite3Fts5PoslistReaderNext(pIter);
222239 return pIter->bEof;
222240}
222241
222242/*
222243** Append position iPos to the position list being accumulated in buffer
222244** pBuf, which must be already be large enough to hold the new data.
222245** The previous position written to this list is *piPrev. *piPrev is set
222246** to iPos before returning.
222247*/
222248static void sqlite3Fts5PoslistSafeAppend(
222249 Fts5Buffer *pBuf,
222250 i64 *piPrev,
222251 i64 iPos
222252){
222253 if( iPos>=*piPrev ){
222254 static const i64 colmask = ((i64)(0x7FFFFFFF)) << 32;
222255 if( (iPos & colmask) != (*piPrev & colmask) ){
222256 pBuf->p[pBuf->n++] = 1;
222257 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos>>32));
222258 *piPrev = (iPos & colmask);
222259 }
222260 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos-*piPrev)+2);
222261 *piPrev = iPos;
222262 }
222263}
222264
222265static int sqlite3Fts5PoslistWriterAppend(
222266 Fts5Buffer *pBuf,
222267 Fts5PoslistWriter *pWriter,
222268 i64 iPos
222269){
222270 int rc = 0; /* Initialized only to suppress erroneous warning from Clang */
222271 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;
222272 sqlite3Fts5PoslistSafeAppend(pBuf, &pWriter->iPrev, iPos);
222273 return SQLITE_OK0;
222274}
222275
222276static void *sqlite3Fts5MallocZero(int *pRc, sqlite3_int64 nByte){
222277 void *pRet = 0;
222278 if( *pRc==SQLITE_OK0 ){
222279 pRet = sqlite3_malloc64(nByte);
222280 if( pRet==0 ){
222281 if( nByte>0 ) *pRc = SQLITE_NOMEM7;
222282 }else{
222283 memset(pRet, 0, (size_t)nByte);
222284 }
222285 }
222286 return pRet;
222287}
222288
222289/*
222290** Return a nul-terminated copy of the string indicated by pIn. If nIn
222291** is non-negative, then it is the length of the string in bytes. Otherwise,
222292** the length of the string is determined using strlen().
222293**
222294** It is the responsibility of the caller to eventually free the returned
222295** buffer using sqlite3_free(). If an OOM error occurs, NULL is returned.
222296*/
222297static char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn){
222298 char *zRet = 0;
222299 if( *pRc==SQLITE_OK0 ){
222300 if( nIn<0 ){
222301 nIn = (int)strlen(pIn);
222302 }
222303 zRet = (char*)sqlite3_malloc(nIn+1);
222304 if( zRet ){
222305 memcpy(zRet, pIn, nIn);
222306 zRet[nIn] = '\0';
222307 }else{
222308 *pRc = SQLITE_NOMEM7;
222309 }
222310 }
222311 return zRet;
222312}
222313
222314
222315/*
222316** Return true if character 't' may be part of an FTS5 bareword, or false
222317** otherwise. Characters that may be part of barewords:
222318**
222319** * All non-ASCII characters,
222320** * The 52 upper and lower case ASCII characters, and
222321** * The 10 integer ASCII characters.
222322** * The underscore character "_" (0x5F).
222323** * The unicode "subsitute" character (0x1A).
222324*/
222325static int sqlite3Fts5IsBareword(char t){
222326 u8 aBareword[128] = {
222327 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 .. 0x0F */
222328 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, /* 0x10 .. 0x1F */
222329 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 .. 0x2F */
222330 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 0x30 .. 0x3F */
222331 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 .. 0x4F */
222332 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 0x50 .. 0x5F */
222333 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 .. 0x6F */
222334 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 /* 0x70 .. 0x7F */
222335 };
222336
222337 return (t & 0x80) || aBareword[(int)t];
222338}
222339
222340
222341/*************************************************************************
222342*/
222343typedef struct Fts5TermsetEntry Fts5TermsetEntry;
222344struct Fts5TermsetEntry {
222345 char *pTerm;
222346 int nTerm;
222347 int iIdx; /* Index (main or aPrefix[] entry) */
222348 Fts5TermsetEntry *pNext;
222349};
222350
222351struct Fts5Termset {
222352 Fts5TermsetEntry *apHash[512];
222353};
222354
222355static int sqlite3Fts5TermsetNew(Fts5Termset **pp){
222356 int rc = SQLITE_OK0;
222357 *pp = sqlite3Fts5MallocZero(&rc, sizeof(Fts5Termset));
222358 return rc;
222359}
222360
222361static int sqlite3Fts5TermsetAdd(
222362 Fts5Termset *p,
222363 int iIdx,
222364 const char *pTerm, int nTerm,
222365 int *pbPresent
222366){
222367 int rc = SQLITE_OK0;
222368 *pbPresent = 0;
222369 if( p ){
222370 int i;
222371 u32 hash = 13;
222372 Fts5TermsetEntry *pEntry;
222373
222374 /* Calculate a hash value for this term. This is the same hash checksum
222375 ** used by the fts5_hash.c module. This is not important for correct
222376 ** operation of the module, but is necessary to ensure that some tests
222377 ** designed to produce hash table collisions really do work. */
222378 for(i=nTerm-1; i>=0; i--){
222379 hash = (hash << 3) ^ hash ^ pTerm[i];
222380 }
222381 hash = (hash << 3) ^ hash ^ iIdx;
222382 hash = hash % ArraySize(p->apHash)((int)(sizeof(p->apHash)/sizeof(p->apHash[0])));
222383
222384 for(pEntry=p->apHash[hash]; pEntry; pEntry=pEntry->pNext){
222385 if( pEntry->iIdx==iIdx
222386 && pEntry->nTerm==nTerm
222387 && memcmp(pEntry->pTerm, pTerm, nTerm)==0
222388 ){
222389 *pbPresent = 1;
222390 break;
222391 }
222392 }
222393
222394 if( pEntry==0 ){
222395 pEntry = sqlite3Fts5MallocZero(&rc, sizeof(Fts5TermsetEntry) + nTerm);
222396 if( pEntry ){
222397 pEntry->pTerm = (char*)&pEntry[1];
222398 pEntry->nTerm = nTerm;
222399 pEntry->iIdx = iIdx;
222400 memcpy(pEntry->pTerm, pTerm, nTerm);
222401 pEntry->pNext = p->apHash[hash];
222402 p->apHash[hash] = pEntry;
222403 }
222404 }
222405 }
222406
222407 return rc;
222408}
222409
222410static void sqlite3Fts5TermsetFree(Fts5Termset *p){
222411 if( p ){
222412 u32 i;
222413 for(i=0; i<ArraySize(p->apHash)((int)(sizeof(p->apHash)/sizeof(p->apHash[0]))); i++){
222414 Fts5TermsetEntry *pEntry = p->apHash[i];
222415 while( pEntry ){
222416 Fts5TermsetEntry *pDel = pEntry;
222417 pEntry = pEntry->pNext;
222418 sqlite3_free(pDel);
222419 }
222420 }
222421 sqlite3_free(p);
222422 }
222423}
222424
222425/*
222426** 2014 Jun 09
222427**
222428** The author disclaims copyright to this source code. In place of
222429** a legal notice, here is a blessing:
222430**
222431** May you do good and not evil.
222432** May you find forgiveness for yourself and forgive others.
222433** May you share freely, never taking more than you give.
222434**
222435******************************************************************************
222436**
222437** This is an SQLite module implementing full-text search.
222438*/
222439
222440
222441/* #include "fts5Int.h" */
222442
222443#define FTS5_DEFAULT_PAGE_SIZE4050 4050
222444#define FTS5_DEFAULT_AUTOMERGE4 4
222445#define FTS5_DEFAULT_USERMERGE4 4
222446#define FTS5_DEFAULT_CRISISMERGE16 16
222447#define FTS5_DEFAULT_HASHSIZE(1024*1024) (1024*1024)
222448
222449/* Maximum allowed page size */
222450#define FTS5_MAX_PAGE_SIZE(64*1024) (64*1024)
222451
222452static int fts5_iswhitespace(char x){
222453 return (x==' ');
222454}
222455
222456static int fts5_isopenquote(char x){
222457 return (x=='"' || x=='\'' || x=='[' || x=='`');
222458}
222459
222460/*
222461** Argument pIn points to a character that is part of a nul-terminated
222462** string. Return a pointer to the first character following *pIn in
222463** the string that is not a white-space character.
222464*/
222465static const char *fts5ConfigSkipWhitespace(const char *pIn){
222466 const char *p = pIn;
222467 if( p ){
222468 while( fts5_iswhitespace(*p) ){ p++; }
222469 }
222470 return p;
222471}
222472
222473/*
222474** Argument pIn points to a character that is part of a nul-terminated
222475** string. Return a pointer to the first character following *pIn in
222476** the string that is not a "bareword" character.
222477*/
222478static const char *fts5ConfigSkipBareword(const char *pIn){
222479 const char *p = pIn;
222480 while ( sqlite3Fts5IsBareword(*p) ) p++;
222481 if( p==pIn ) p = 0;
222482 return p;
222483}
222484
222485static int fts5_isdigit(char a){
222486 return (a>='0' && a<='9');
222487}
222488
222489
222490
222491static const char *fts5ConfigSkipLiteral(const char *pIn){
222492 const char *p = pIn;
222493 switch( *p ){
222494 case 'n': case 'N':
222495 if( sqlite3_strnicmp("null", p, 4)==0 ){
222496 p = &p[4];
222497 }else{
222498 p = 0;
222499 }
222500 break;
222501
222502 case 'x': case 'X':
222503 p++;
222504 if( *p=='\'' ){
222505 p++;
222506 while( (*p>='a' && *p<='f')
222507 || (*p>='A' && *p<='F')
222508 || (*p>='0' && *p<='9')
222509 ){
222510 p++;
222511 }
222512 if( *p=='\'' && 0==((p-pIn)%2) ){
222513 p++;
222514 }else{
222515 p = 0;
222516 }
222517 }else{
222518 p = 0;
222519 }
222520 break;
222521
222522 case '\'':
222523 p++;
222524 while( p ){
222525 if( *p=='\'' ){
222526 p++;
222527 if( *p!='\'' ) break;
222528 }
222529 p++;
222530 if( *p==0 ) p = 0;
222531 }
222532 break;
222533
222534 default:
222535 /* maybe a number */
222536 if( *p=='+' || *p=='-' ) p++;
222537 while( fts5_isdigit(*p) ) p++;
222538
222539 /* At this point, if the literal was an integer, the parse is
222540 ** finished. Or, if it is a floating point value, it may continue
222541 ** with either a decimal point or an 'E' character. */
222542 if( *p=='.' && fts5_isdigit(p[1]) ){
222543 p += 2;
222544 while( fts5_isdigit(*p) ) p++;
222545 }
222546 if( p==pIn ) p = 0;
222547
222548 break;
222549 }
222550
222551 return p;
222552}
222553
222554/*
222555** The first character of the string pointed to by argument z is guaranteed
222556** to be an open-quote character (see function fts5_isopenquote()).
222557**
222558** This function searches for the corresponding close-quote character within
222559** the string and, if found, dequotes the string in place and adds a new
222560** nul-terminator byte.
222561**
222562** If the close-quote is found, the value returned is the byte offset of
222563** the character immediately following it. Or, if the close-quote is not
222564** found, -1 is returned. If -1 is returned, the buffer is left in an
222565** undefined state.
222566*/
222567static int fts5Dequote(char *z){
222568 char q;
222569 int iIn = 1;
222570 int iOut = 0;
222571 q = z[0];
222572
222573 /* Set stack variable q to the close-quote character */
222574 assert( q=='[' || q=='\'' || q=='"' || q=='`' )((void) (0));
222575 if( q=='[' ) q = ']';
222576
222577 while( z[iIn] ){
222578 if( z[iIn]==q ){
222579 if( z[iIn+1]!=q ){
222580 /* Character iIn was the close quote. */
222581 iIn++;
222582 break;
222583 }else{
222584 /* Character iIn and iIn+1 form an escaped quote character. Skip
222585 ** the input cursor past both and copy a single quote character
222586 ** to the output buffer. */
222587 iIn += 2;
222588 z[iOut++] = q;
222589 }
222590 }else{
222591 z[iOut++] = z[iIn++];
222592 }
222593 }
222594
222595 z[iOut] = '\0';
222596 return iIn;
222597}
222598
222599/*
222600** Convert an SQL-style quoted string into a normal string by removing
222601** the quote characters. The conversion is done in-place. If the
222602** input does not begin with a quote character, then this routine
222603** is a no-op.
222604**
222605** Examples:
222606**
222607** "abc" becomes abc
222608** 'xyz' becomes xyz
222609** [pqr] becomes pqr
222610** `mno` becomes mno
222611*/
222612static void sqlite3Fts5Dequote(char *z){
222613 char quote; /* Quote character (if any ) */
222614
222615 assert( 0==fts5_iswhitespace(z[0]) )((void) (0));
222616 quote = z[0];
222617 if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
222618 fts5Dequote(z);
222619 }
222620}
222621
222622
222623struct Fts5Enum {
222624 const char *zName;
222625 int eVal;
222626};
222627typedef struct Fts5Enum Fts5Enum;
222628
222629static int fts5ConfigSetEnum(
222630 const Fts5Enum *aEnum,
222631 const char *zEnum,
222632 int *peVal
222633){
222634 int nEnum = (int)strlen(zEnum);
222635 int i;
222636 int iVal = -1;
222637
222638 for(i=0; aEnum[i].zName; i++){
222639 if( sqlite3_strnicmp(aEnum[i].zName, zEnum, nEnum)==0 ){
222640 if( iVal>=0 ) return SQLITE_ERROR1;
222641 iVal = aEnum[i].eVal;
222642 }
222643 }
222644
222645 *peVal = iVal;
222646 return iVal<0 ? SQLITE_ERROR1 : SQLITE_OK0;
222647}
222648
222649/*
222650** Parse a "special" CREATE VIRTUAL TABLE directive and update
222651** configuration object pConfig as appropriate.
222652**
222653** If successful, object pConfig is updated and SQLITE_OK returned. If
222654** an error occurs, an SQLite error code is returned and an error message
222655** may be left in *pzErr. It is the responsibility of the caller to
222656** eventually free any such error message using sqlite3_free().
222657*/
222658static int fts5ConfigParseSpecial(
222659 Fts5Global *pGlobal,
222660 Fts5Config *pConfig, /* Configuration object to update */
222661 const char *zCmd, /* Special command to parse */
222662 const char *zArg, /* Argument to parse */
222663 char **pzErr /* OUT: Error message */
222664){
222665 int rc = SQLITE_OK0;
222666 int nCmd = (int)strlen(zCmd);
222667 if( sqlite3_strnicmp("prefix", zCmd, nCmd)==0 ){
222668 const int nByte = sizeof(int) * FTS5_MAX_PREFIX_INDEXES31;
222669 const char *p;
222670 int bFirst = 1;
222671 if( pConfig->aPrefix==0 ){
222672 pConfig->aPrefix = sqlite3Fts5MallocZero(&rc, nByte);
222673 if( rc ) return rc;
222674 }
222675
222676 p = zArg;
222677 while( 1 ){
222678 int nPre = 0;
222679
222680 while( p[0]==' ' ) p++;
222681 if( bFirst==0 && p[0]==',' ){
222682 p++;
222683 while( p[0]==' ' ) p++;
222684 }else if( p[0]=='\0' ){
222685 break;
222686 }
222687 if( p[0]<'0' || p[0]>'9' ){
222688 *pzErr = sqlite3_mprintf("malformed prefix=... directive");
222689 rc = SQLITE_ERROR1;
222690 break;
222691 }
222692
222693 if( pConfig->nPrefix==FTS5_MAX_PREFIX_INDEXES31 ){
222694 *pzErr = sqlite3_mprintf(
222695 "too many prefix indexes (max %d)", FTS5_MAX_PREFIX_INDEXES31
222696 );
222697 rc = SQLITE_ERROR1;
222698 break;
222699 }
222700
222701 while( p[0]>='0' && p[0]<='9' && nPre<1000 ){
222702 nPre = nPre*10 + (p[0] - '0');
222703 p++;
222704 }
222705
222706 if( nPre<=0 || nPre>=1000 ){
222707 *pzErr = sqlite3_mprintf("prefix length out of range (max 999)");
222708 rc = SQLITE_ERROR1;
222709 break;
222710 }
222711
222712 pConfig->aPrefix[pConfig->nPrefix] = nPre;
222713 pConfig->nPrefix++;
222714 bFirst = 0;
222715 }
222716 assert( pConfig->nPrefix<=FTS5_MAX_PREFIX_INDEXES )((void) (0));
222717 return rc;
222718 }
222719
222720 if( sqlite3_strnicmp("tokenize", zCmd, nCmd)==0 ){
222721 const char *p = (const char*)zArg;
222722 sqlite3_int64 nArg = strlen(zArg) + 1;
222723 char **azArg = sqlite3Fts5MallocZero(&rc, sizeof(char*) * nArg);
222724 char *pDel = sqlite3Fts5MallocZero(&rc, nArg * 2);
222725 char *pSpace = pDel;
222726
222727 if( azArg && pSpace ){
222728 if( pConfig->pTok ){
222729 *pzErr = sqlite3_mprintf("multiple tokenize=... directives");
222730 rc = SQLITE_ERROR1;
222731 }else{
222732 for(nArg=0; p && *p; nArg++){
222733 const char *p2 = fts5ConfigSkipWhitespace(p);
222734 if( *p2=='\'' ){
222735 p = fts5ConfigSkipLiteral(p2);
222736 }else{
222737 p = fts5ConfigSkipBareword(p2);
222738 }
222739 if( p ){
222740 memcpy(pSpace, p2, p-p2);
222741 azArg[nArg] = pSpace;
222742 sqlite3Fts5Dequote(pSpace);
222743 pSpace += (p - p2) + 1;
222744 p = fts5ConfigSkipWhitespace(p);
222745 }
222746 }
222747 if( p==0 ){
222748 *pzErr = sqlite3_mprintf("parse error in tokenize directive");
222749 rc = SQLITE_ERROR1;
222750 }else{
222751 rc = sqlite3Fts5GetTokenizer(pGlobal,
222752 (const char**)azArg, (int)nArg, pConfig,
222753 pzErr
222754 );
222755 }
222756 }
222757 }
222758
222759 sqlite3_free(azArg);
222760 sqlite3_free(pDel);
222761 return rc;
222762 }
222763
222764 if( sqlite3_strnicmp("content", zCmd, nCmd)==0 ){
222765 if( pConfig->eContent!=FTS5_CONTENT_NORMAL0 ){
222766 *pzErr = sqlite3_mprintf("multiple content=... directives");
222767 rc = SQLITE_ERROR1;
222768 }else{
222769 if( zArg[0] ){
222770 pConfig->eContent = FTS5_CONTENT_EXTERNAL2;
222771 pConfig->zContent = sqlite3Fts5Mprintf(&rc, "%Q.%Q", pConfig->zDb,zArg);
222772 }else{
222773 pConfig->eContent = FTS5_CONTENT_NONE1;
222774 }
222775 }
222776 return rc;
222777 }
222778
222779 if( sqlite3_strnicmp("content_rowid", zCmd, nCmd)==0 ){
222780 if( pConfig->zContentRowid ){
222781 *pzErr = sqlite3_mprintf("multiple content_rowid=... directives");
222782 rc = SQLITE_ERROR1;
222783 }else{
222784 pConfig->zContentRowid = sqlite3Fts5Strndup(&rc, zArg, -1);
222785 }
222786 return rc;
222787 }
222788
222789 if( sqlite3_strnicmp("columnsize", zCmd, nCmd)==0 ){
222790 if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){
222791 *pzErr = sqlite3_mprintf("malformed columnsize=... directive");
222792 rc = SQLITE_ERROR1;
222793 }else{
222794 pConfig->bColumnsize = (zArg[0]=='1');
222795 }
222796 return rc;
222797 }
222798
222799 if( sqlite3_strnicmp("detail", zCmd, nCmd)==0 ){
222800 const Fts5Enum aDetail[] = {
222801 { "none", FTS5_DETAIL_NONE1 },
222802 { "full", FTS5_DETAIL_FULL0 },
222803 { "columns", FTS5_DETAIL_COLUMNS2 },
222804 { 0, 0 }
222805 };
222806
222807 if( (rc = fts5ConfigSetEnum(aDetail, zArg, &pConfig->eDetail)) ){
222808 *pzErr = sqlite3_mprintf("malformed detail=... directive");
222809 }
222810 return rc;
222811 }
222812
222813 *pzErr = sqlite3_mprintf("unrecognized option: \"%.*s\"", nCmd, zCmd);
222814 return SQLITE_ERROR1;
222815}
222816
222817/*
222818** Allocate an instance of the default tokenizer ("simple") at
222819** Fts5Config.pTokenizer. Return SQLITE_OK if successful, or an SQLite error
222820** code if an error occurs.
222821*/
222822static int fts5ConfigDefaultTokenizer(Fts5Global *pGlobal, Fts5Config *pConfig){
222823 assert( pConfig->pTok==0 && pConfig->pTokApi==0 )((void) (0));
222824 return sqlite3Fts5GetTokenizer(pGlobal, 0, 0, pConfig, 0);
222825}
222826
222827/*
222828** Gobble up the first bareword or quoted word from the input buffer zIn.
222829** Return a pointer to the character immediately following the last in
222830** the gobbled word if successful, or a NULL pointer otherwise (failed
222831** to find close-quote character).
222832**
222833** Before returning, set pzOut to point to a new buffer containing a
222834** nul-terminated, dequoted copy of the gobbled word. If the word was
222835** quoted, *pbQuoted is also set to 1 before returning.
222836**
222837** If *pRc is other than SQLITE_OK when this function is called, it is
222838** a no-op (NULL is returned). Otherwise, if an OOM occurs within this
222839** function, *pRc is set to SQLITE_NOMEM before returning. *pRc is *not*
222840** set if a parse error (failed to find close quote) occurs.
222841*/
222842static const char *fts5ConfigGobbleWord(
222843 int *pRc, /* IN/OUT: Error code */
222844 const char *zIn, /* Buffer to gobble string/bareword from */
222845 char **pzOut, /* OUT: malloc'd buffer containing str/bw */
222846 int *pbQuoted /* OUT: Set to true if dequoting required */
222847){
222848 const char *zRet = 0;
222849
222850 sqlite3_int64 nIn = strlen(zIn);
222851 char *zOut = sqlite3_malloc64(nIn+1);
222852
222853 assert( *pRc==SQLITE_OK )((void) (0));
222854 *pbQuoted = 0;
222855 *pzOut = 0;
222856
222857 if( zOut==0 ){
222858 *pRc = SQLITE_NOMEM7;
222859 }else{
222860 memcpy(zOut, zIn, (size_t)(nIn+1));
222861 if( fts5_isopenquote(zOut[0]) ){
222862 int ii = fts5Dequote(zOut);
222863 zRet = &zIn[ii];
222864 *pbQuoted = 1;
222865 }else{
222866 zRet = fts5ConfigSkipBareword(zIn);
222867 if( zRet ){
222868 zOut[zRet-zIn] = '\0';
222869 }
222870 }
222871 }
222872
222873 if( zRet==0 ){
222874 sqlite3_free(zOut);
222875 }else{
222876 *pzOut = zOut;
222877 }
222878
222879 return zRet;
222880}
222881
222882static int fts5ConfigParseColumn(
222883 Fts5Config *p,
222884 char *zCol,
222885 char *zArg,
222886 char **pzErr
222887){
222888 int rc = SQLITE_OK0;
222889 if( 0==sqlite3_stricmp(zCol, FTS5_RANK_NAME"rank")
222890 || 0==sqlite3_stricmp(zCol, FTS5_ROWID_NAME"rowid")
222891 ){
222892 *pzErr = sqlite3_mprintf("reserved fts5 column name: %s", zCol);
222893 rc = SQLITE_ERROR1;
222894 }else if( zArg ){
222895 if( 0==sqlite3_stricmp(zArg, "unindexed") ){
222896 p->abUnindexed[p->nCol] = 1;
222897 }else{
222898 *pzErr = sqlite3_mprintf("unrecognized column option: %s", zArg);
222899 rc = SQLITE_ERROR1;
222900 }
222901 }
222902
222903 p->azCol[p->nCol++] = zCol;
222904 return rc;
222905}
222906
222907/*
222908** Populate the Fts5Config.zContentExprlist string.
222909*/
222910static int fts5ConfigMakeExprlist(Fts5Config *p){
222911 int i;
222912 int rc = SQLITE_OK0;
222913 Fts5Buffer buf = {0, 0, 0};
222914
222915 sqlite3Fts5BufferAppendPrintf(&rc, &buf, "T.%Q", p->zContentRowid);
222916 if( p->eContent!=FTS5_CONTENT_NONE1 ){
222917 for(i=0; i<p->nCol; i++){
222918 if( p->eContent==FTS5_CONTENT_EXTERNAL2 ){
222919 sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.%Q", p->azCol[i]);
222920 }else{
222921 sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.c%d", i);
222922 }
222923 }
222924 }
222925
222926 assert( p->zContentExprlist==0 )((void) (0));
222927 p->zContentExprlist = (char*)buf.p;
222928 return rc;
222929}
222930
222931/*
222932** Arguments nArg/azArg contain the string arguments passed to the xCreate
222933** or xConnect method of the virtual table. This function attempts to
222934** allocate an instance of Fts5Config containing the results of parsing
222935** those arguments.
222936**
222937** If successful, SQLITE_OK is returned and *ppOut is set to point to the
222938** new Fts5Config object. If an error occurs, an SQLite error code is
222939** returned, *ppOut is set to NULL and an error message may be left in
222940** *pzErr. It is the responsibility of the caller to eventually free any
222941** such error message using sqlite3_free().
222942*/
222943static int sqlite3Fts5ConfigParse(
222944 Fts5Global *pGlobal,
222945 sqlite3 *db,
222946 int nArg, /* Number of arguments */
222947 const char **azArg, /* Array of nArg CREATE VIRTUAL TABLE args */
222948 Fts5Config **ppOut, /* OUT: Results of parse */
222949 char **pzErr /* OUT: Error message */
222950){
222951 int rc = SQLITE_OK0; /* Return code */
222952 Fts5Config *pRet; /* New object to return */
222953 int i;
222954 sqlite3_int64 nByte;
222955
222956 *ppOut = pRet = (Fts5Config*)sqlite3_malloc(sizeof(Fts5Config));
222957 if( pRet==0 ) return SQLITE_NOMEM7;
222958 memset(pRet, 0, sizeof(Fts5Config));
222959 pRet->db = db;
222960 pRet->iCookie = -1;
222961
222962 nByte = nArg * (sizeof(char*) + sizeof(u8));
222963 pRet->azCol = (char**)sqlite3Fts5MallocZero(&rc, nByte);
222964 pRet->abUnindexed = pRet->azCol ? (u8*)&pRet->azCol[nArg] : 0;
222965 pRet->zDb = sqlite3Fts5Strndup(&rc, azArg[1], -1);
222966 pRet->zName = sqlite3Fts5Strndup(&rc, azArg[2], -1);
222967 pRet->bColumnsize = 1;
222968 pRet->eDetail = FTS5_DETAIL_FULL0;
222969#ifdef SQLITE_DEBUG
222970 pRet->bPrefixIndex = 1;
222971#endif
222972 if( rc==SQLITE_OK0 && sqlite3_stricmp(pRet->zName, FTS5_RANK_NAME"rank")==0 ){
222973 *pzErr = sqlite3_mprintf("reserved fts5 table name: %s", pRet->zName);
222974 rc = SQLITE_ERROR1;
222975 }
222976
222977 for(i=3; rc==SQLITE_OK0 && i<nArg; i++){
222978 const char *zOrig = azArg[i];
222979 const char *z;
222980 char *zOne = 0;
222981 char *zTwo = 0;
222982 int bOption = 0;
222983 int bMustBeCol = 0;
222984
222985 z = fts5ConfigGobbleWord(&rc, zOrig, &zOne, &bMustBeCol);
222986 z = fts5ConfigSkipWhitespace(z);
222987 if( z && *z=='=' ){
222988 bOption = 1;
222989 assert( zOne!=0 )((void) (0));
222990 z++;
222991 if( bMustBeCol ) z = 0;
222992 }
222993 z = fts5ConfigSkipWhitespace(z);
222994 if( z && z[0] ){
222995 int bDummy;
222996 z = fts5ConfigGobbleWord(&rc, z, &zTwo, &bDummy);
222997 if( z && z[0] ) z = 0;
222998 }
222999
223000 if( rc==SQLITE_OK0 ){
223001 if( z==0 ){
223002 *pzErr = sqlite3_mprintf("parse error in \"%s\"", zOrig);
223003 rc = SQLITE_ERROR1;
223004 }else{
223005 if( bOption ){
223006 rc = fts5ConfigParseSpecial(pGlobal, pRet,
223007 ALWAYS(zOne)(zOne)?zOne:"",
223008 zTwo?zTwo:"",
223009 pzErr
223010 );
223011 }else{
223012 rc = fts5ConfigParseColumn(pRet, zOne, zTwo, pzErr);
223013 zOne = 0;
223014 }
223015 }
223016 }
223017
223018 sqlite3_free(zOne);
223019 sqlite3_free(zTwo);
223020 }
223021
223022 /* If a tokenizer= option was successfully parsed, the tokenizer has
223023 ** already been allocated. Otherwise, allocate an instance of the default
223024 ** tokenizer (unicode61) now. */
223025 if( rc==SQLITE_OK0 && pRet->pTok==0 ){
223026 rc = fts5ConfigDefaultTokenizer(pGlobal, pRet);
223027 }
223028
223029 /* If no zContent option was specified, fill in the default values. */
223030 if( rc==SQLITE_OK0 && pRet->zContent==0 ){
223031 const char *zTail = 0;
223032 assert( pRet->eContent==FTS5_CONTENT_NORMAL((void) (0))
223033 || pRet->eContent==FTS5_CONTENT_NONE((void) (0))
223034 )((void) (0));
223035 if( pRet->eContent==FTS5_CONTENT_NORMAL0 ){
223036 zTail = "content";
223037 }else if( pRet->bColumnsize ){
223038 zTail = "docsize";
223039 }
223040
223041 if( zTail ){
223042 pRet->zContent = sqlite3Fts5Mprintf(
223043 &rc, "%Q.'%q_%s'", pRet->zDb, pRet->zName, zTail
223044 );
223045 }
223046 }
223047
223048 if( rc==SQLITE_OK0 && pRet->zContentRowid==0 ){
223049 pRet->zContentRowid = sqlite3Fts5Strndup(&rc, "rowid", -1);
223050 }
223051
223052 /* Formulate the zContentExprlist text */
223053 if( rc==SQLITE_OK0 ){
223054 rc = fts5ConfigMakeExprlist(pRet);
223055 }
223056
223057 if( rc!=SQLITE_OK0 ){
223058 sqlite3Fts5ConfigFree(pRet);
223059 *ppOut = 0;
223060 }
223061 return rc;
223062}
223063
223064/*
223065** Free the configuration object passed as the only argument.
223066*/
223067static void sqlite3Fts5ConfigFree(Fts5Config *pConfig){
223068 if( pConfig ){
223069 int i;
223070 if( pConfig->pTok ){
223071 pConfig->pTokApi->xDelete(pConfig->pTok);
223072 }
223073 sqlite3_free(pConfig->zDb);
223074 sqlite3_free(pConfig->zName);
223075 for(i=0; i<pConfig->nCol; i++){
223076 sqlite3_free(pConfig->azCol[i]);
223077 }
223078 sqlite3_free(pConfig->azCol);
223079 sqlite3_free(pConfig->aPrefix);
223080 sqlite3_free(pConfig->zRank);
223081 sqlite3_free(pConfig->zRankArgs);
223082 sqlite3_free(pConfig->zContent);
223083 sqlite3_free(pConfig->zContentRowid);
223084 sqlite3_free(pConfig->zContentExprlist);
223085 sqlite3_free(pConfig);
223086 }
223087}
223088
223089/*
223090** Call sqlite3_declare_vtab() based on the contents of the configuration
223091** object passed as the only argument. Return SQLITE_OK if successful, or
223092** an SQLite error code if an error occurs.
223093*/
223094static int sqlite3Fts5ConfigDeclareVtab(Fts5Config *pConfig){
223095 int i;
223096 int rc = SQLITE_OK0;
223097 char *zSql;
223098
223099 zSql = sqlite3Fts5Mprintf(&rc, "CREATE TABLE x(");
223100 for(i=0; zSql && i<pConfig->nCol; i++){
223101 const char *zSep = (i==0?"":", ");
223102 zSql = sqlite3Fts5Mprintf(&rc, "%z%s%Q", zSql, zSep, pConfig->azCol[i]);
223103 }
223104 zSql = sqlite3Fts5Mprintf(&rc, "%z, %Q HIDDEN, %s HIDDEN)",
223105 zSql, pConfig->zName, FTS5_RANK_NAME"rank"
223106 );
223107
223108 assert( zSql || rc==SQLITE_NOMEM )((void) (0));
223109 if( zSql ){
223110 rc = sqlite3_declare_vtab(pConfig->db, zSql);
223111 sqlite3_free(zSql);
223112 }
223113
223114 return rc;
223115}
223116
223117/*
223118** Tokenize the text passed via the second and third arguments.
223119**
223120** The callback is invoked once for each token in the input text. The
223121** arguments passed to it are, in order:
223122**
223123** void *pCtx // Copy of 4th argument to sqlite3Fts5Tokenize()
223124** const char *pToken // Pointer to buffer containing token
223125** int nToken // Size of token in bytes
223126** int iStart // Byte offset of start of token within input text
223127** int iEnd // Byte offset of end of token within input text
223128** int iPos // Position of token in input (first token is 0)
223129**
223130** If the callback returns a non-zero value the tokenization is abandoned
223131** and no further callbacks are issued.
223132**
223133** This function returns SQLITE_OK if successful or an SQLite error code
223134** if an error occurs. If the tokenization was abandoned early because
223135** the callback returned SQLITE_DONE, this is not an error and this function
223136** still returns SQLITE_OK. Or, if the tokenization was abandoned early
223137** because the callback returned another non-zero value, it is assumed
223138** to be an SQLite error code and returned to the caller.
223139*/
223140static int sqlite3Fts5Tokenize(
223141 Fts5Config *pConfig, /* FTS5 Configuration object */
223142 int flags, /* FTS5_TOKENIZE_* flags */
223143 const char *pText, int nText, /* Text to tokenize */
223144 void *pCtx, /* Context passed to xToken() */
223145 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
223146){
223147 if( pText==0 ) return SQLITE_OK0;
223148 return pConfig->pTokApi->xTokenize(
223149 pConfig->pTok, pCtx, flags, pText, nText, xToken
223150 );
223151}
223152
223153/*
223154** Argument pIn points to the first character in what is expected to be
223155** a comma-separated list of SQL literals followed by a ')' character.
223156** If it actually is this, return a pointer to the ')'. Otherwise, return
223157** NULL to indicate a parse error.
223158*/
223159static const char *fts5ConfigSkipArgs(const char *pIn){
223160 const char *p = pIn;
223161
223162 while( 1 ){
223163 p = fts5ConfigSkipWhitespace(p);
223164 p = fts5ConfigSkipLiteral(p);
223165 p = fts5ConfigSkipWhitespace(p);
223166 if( p==0 || *p==')' ) break;
223167 if( *p!=',' ){
223168 p = 0;
223169 break;
223170 }
223171 p++;
223172 }
223173
223174 return p;
223175}
223176
223177/*
223178** Parameter zIn contains a rank() function specification. The format of
223179** this is:
223180**
223181** + Bareword (function name)
223182** + Open parenthesis - "("
223183** + Zero or more SQL literals in a comma separated list
223184** + Close parenthesis - ")"
223185*/
223186static int sqlite3Fts5ConfigParseRank(
223187 const char *zIn, /* Input string */
223188 char **pzRank, /* OUT: Rank function name */
223189 char **pzRankArgs /* OUT: Rank function arguments */
223190){
223191 const char *p = zIn;
223192 const char *pRank;
223193 char *zRank = 0;
223194 char *zRankArgs = 0;
223195 int rc = SQLITE_OK0;
223196
223197 *pzRank = 0;
223198 *pzRankArgs = 0;
223199
223200 if( p==0 ){
223201 rc = SQLITE_ERROR1;
223202 }else{
223203 p = fts5ConfigSkipWhitespace(p);
223204 pRank = p;
223205 p = fts5ConfigSkipBareword(p);
223206
223207 if( p ){
223208 zRank = sqlite3Fts5MallocZero(&rc, 1 + p - pRank);
223209 if( zRank ) memcpy(zRank, pRank, p-pRank);
223210 }else{
223211 rc = SQLITE_ERROR1;
223212 }
223213
223214 if( rc==SQLITE_OK0 ){
223215 p = fts5ConfigSkipWhitespace(p);
223216 if( *p!='(' ) rc = SQLITE_ERROR1;
223217 p++;
223218 }
223219 if( rc==SQLITE_OK0 ){
223220 const char *pArgs;
223221 p = fts5ConfigSkipWhitespace(p);
223222 pArgs = p;
223223 if( *p!=')' ){
223224 p = fts5ConfigSkipArgs(p);
223225 if( p==0 ){
223226 rc = SQLITE_ERROR1;
223227 }else{
223228 zRankArgs = sqlite3Fts5MallocZero(&rc, 1 + p - pArgs);
223229 if( zRankArgs ) memcpy(zRankArgs, pArgs, p-pArgs);
223230 }
223231 }
223232 }
223233 }
223234
223235 if( rc!=SQLITE_OK0 ){
223236 sqlite3_free(zRank);
223237 assert( zRankArgs==0 )((void) (0));
223238 }else{
223239 *pzRank = zRank;
223240 *pzRankArgs = zRankArgs;
223241 }
223242 return rc;
223243}
223244
223245static int sqlite3Fts5ConfigSetValue(
223246 Fts5Config *pConfig,
223247 const char *zKey,
223248 sqlite3_value *pVal,
223249 int *pbBadkey
223250){
223251 int rc = SQLITE_OK0;
223252
223253 if( 0==sqlite3_stricmp(zKey, "pgsz") ){
223254 int pgsz = 0;
223255 if( SQLITE_INTEGER1==sqlite3_value_numeric_type(pVal) ){
223256 pgsz = sqlite3_value_int(pVal);
223257 }
223258 if( pgsz<32 || pgsz>FTS5_MAX_PAGE_SIZE(64*1024) ){
223259 *pbBadkey = 1;
223260 }else{
223261 pConfig->pgsz = pgsz;
223262 }
223263 }
223264
223265 else if( 0==sqlite3_stricmp(zKey, "hashsize") ){
223266 int nHashSize = -1;
223267 if( SQLITE_INTEGER1==sqlite3_value_numeric_type(pVal) ){
223268 nHashSize = sqlite3_value_int(pVal);
223269 }
223270 if( nHashSize<=0 ){
223271 *pbBadkey = 1;
223272 }else{
223273 pConfig->nHashSize = nHashSize;
223274 }
223275 }
223276
223277 else if( 0==sqlite3_stricmp(zKey, "automerge") ){
223278 int nAutomerge = -1;
223279 if( SQLITE_INTEGER1==sqlite3_value_numeric_type(pVal) ){
223280 nAutomerge = sqlite3_value_int(pVal);
223281 }
223282 if( nAutomerge<0 || nAutomerge>64 ){
223283 *pbBadkey = 1;
223284 }else{
223285 if( nAutomerge==1 ) nAutomerge = FTS5_DEFAULT_AUTOMERGE4;
223286 pConfig->nAutomerge = nAutomerge;
223287 }
223288 }
223289
223290 else if( 0==sqlite3_stricmp(zKey, "usermerge") ){
223291 int nUsermerge = -1;
223292 if( SQLITE_INTEGER1==sqlite3_value_numeric_type(pVal) ){
223293 nUsermerge = sqlite3_value_int(pVal);
223294 }
223295 if( nUsermerge<2 || nUsermerge>16 ){
223296 *pbBadkey = 1;
223297 }else{
223298 pConfig->nUsermerge = nUsermerge;
223299 }
223300 }
223301
223302 else if( 0==sqlite3_stricmp(zKey, "crisismerge") ){
223303 int nCrisisMerge = -1;
223304 if( SQLITE_INTEGER1==sqlite3_value_numeric_type(pVal) ){
223305 nCrisisMerge = sqlite3_value_int(pVal);
223306 }
223307 if( nCrisisMerge<0 ){
223308 *pbBadkey = 1;
223309 }else{
223310 if( nCrisisMerge<=1 ) nCrisisMerge = FTS5_DEFAULT_CRISISMERGE16;
223311 if( nCrisisMerge>=FTS5_MAX_SEGMENT2000 ) nCrisisMerge = FTS5_MAX_SEGMENT2000-1;
223312 pConfig->nCrisisMerge = nCrisisMerge;
223313 }
223314 }
223315
223316 else if( 0==sqlite3_stricmp(zKey, "rank") ){
223317 const char *zIn = (const char*)sqlite3_value_text(pVal);
223318 char *zRank;
223319 char *zRankArgs;
223320 rc = sqlite3Fts5ConfigParseRank(zIn, &zRank, &zRankArgs);
223321 if( rc==SQLITE_OK0 ){
223322 sqlite3_free(pConfig->zRank);
223323 sqlite3_free(pConfig->zRankArgs);
223324 pConfig->zRank = zRank;
223325 pConfig->zRankArgs = zRankArgs;
223326 }else if( rc==SQLITE_ERROR1 ){
223327 rc = SQLITE_OK0;
223328 *pbBadkey = 1;
223329 }
223330 }else{
223331 *pbBadkey = 1;
223332 }
223333 return rc;
223334}
223335
223336/*
223337** Load the contents of the %_config table into memory.
223338*/
223339static int sqlite3Fts5ConfigLoad(Fts5Config *pConfig, int iCookie){
223340 const char *zSelect = "SELECT k, v FROM %Q.'%q_config'";
223341 char *zSql;
223342 sqlite3_stmt *p = 0;
223343 int rc = SQLITE_OK0;
223344 int iVersion = 0;
223345
223346 /* Set default values */
223347 pConfig->pgsz = FTS5_DEFAULT_PAGE_SIZE4050;
223348 pConfig->nAutomerge = FTS5_DEFAULT_AUTOMERGE4;
223349 pConfig->nUsermerge = FTS5_DEFAULT_USERMERGE4;
223350 pConfig->nCrisisMerge = FTS5_DEFAULT_CRISISMERGE16;
223351 pConfig->nHashSize = FTS5_DEFAULT_HASHSIZE(1024*1024);
223352
223353 zSql = sqlite3Fts5Mprintf(&rc, zSelect, pConfig->zDb, pConfig->zName);
223354 if( zSql ){
223355 rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &p, 0);
223356 sqlite3_free(zSql);
223357 }
223358
223359 assert( rc==SQLITE_OK || p==0 )((void) (0));
223360 if( rc==SQLITE_OK0 ){
223361 while( SQLITE_ROW100==sqlite3_step(p) ){
223362 const char *zK = (const char*)sqlite3_column_text(p, 0);
223363 sqlite3_value *pVal = sqlite3_column_value(p, 1);
223364 if( 0==sqlite3_stricmp(zK, "version") ){
223365 iVersion = sqlite3_value_int(pVal);
223366 }else{
223367 int bDummy = 0;
223368 sqlite3Fts5ConfigSetValue(pConfig, zK, pVal, &bDummy);
223369 }
223370 }
223371 rc = sqlite3_finalize(p);
223372 }
223373
223374 if( rc==SQLITE_OK0 && iVersion!=FTS5_CURRENT_VERSION4 ){
223375 rc = SQLITE_ERROR1;
223376 if( pConfig->pzErrmsg ){
223377 assert( 0==*pConfig->pzErrmsg )((void) (0));
223378 *pConfig->pzErrmsg = sqlite3_mprintf(
223379 "invalid fts5 file format (found %d, expected %d) - run 'rebuild'",
223380 iVersion, FTS5_CURRENT_VERSION4
223381 );
223382 }
223383 }
223384
223385 if( rc==SQLITE_OK0 ){
223386 pConfig->iCookie = iCookie;
223387 }
223388 return rc;
223389}
223390
223391/*
223392** 2014 May 31
223393**
223394** The author disclaims copyright to this source code. In place of
223395** a legal notice, here is a blessing:
223396**
223397** May you do good and not evil.
223398** May you find forgiveness for yourself and forgive others.
223399** May you share freely, never taking more than you give.
223400**
223401******************************************************************************
223402**
223403*/
223404
223405
223406
223407/* #include "fts5Int.h" */
223408/* #include "fts5parse.h" */
223409
223410/*
223411** All token types in the generated fts5parse.h file are greater than 0.
223412*/
223413#define FTS5_EOF0 0
223414
223415#define FTS5_LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) (0xffffffff|(((i64)0x7fffffff)<<32))
223416
223417typedef struct Fts5ExprTerm Fts5ExprTerm;
223418
223419/*
223420** Functions generated by lemon from fts5parse.y.
223421*/
223422static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(u64));
223423static void sqlite3Fts5ParserFree(void*, void (*freeProc)(void*));
223424static void sqlite3Fts5Parser(void*, int, Fts5Token, Fts5Parse*);
223425#ifndef NDEBUG1
223426/* #include <stdio.h> */
223427static void sqlite3Fts5ParserTrace(FILE*, char*);
223428#endif
223429static int sqlite3Fts5ParserFallback(int);
223430
223431
223432struct Fts5Expr {
223433 Fts5Index *pIndex;
223434 Fts5Config *pConfig;
223435 Fts5ExprNode *pRoot;
223436 int bDesc; /* Iterate in descending rowid order */
223437 int nPhrase; /* Number of phrases in expression */
223438 Fts5ExprPhrase **apExprPhrase; /* Pointers to phrase objects */
223439};
223440
223441/*
223442** eType:
223443** Expression node type. Always one of:
223444**
223445** FTS5_AND (nChild, apChild valid)
223446** FTS5_OR (nChild, apChild valid)
223447** FTS5_NOT (nChild, apChild valid)
223448** FTS5_STRING (pNear valid)
223449** FTS5_TERM (pNear valid)
223450*/
223451struct Fts5ExprNode {
223452 int eType; /* Node type */
223453 int bEof; /* True at EOF */
223454 int bNomatch; /* True if entry is not a match */
223455
223456 /* Next method for this node. */
223457 int (*xNext)(Fts5Expr*, Fts5ExprNode*, int, i64);
223458
223459 i64 iRowid; /* Current rowid */
223460 Fts5ExprNearset *pNear; /* For FTS5_STRING - cluster of phrases */
223461
223462 /* Child nodes. For a NOT node, this array always contains 2 entries. For
223463 ** AND or OR nodes, it contains 2 or more entries. */
223464 int nChild; /* Number of child nodes */
223465 Fts5ExprNode *apChild[1]; /* Array of child nodes */
223466};
223467
223468#define Fts5NodeIsString(p)((p)->eType==4 || (p)->eType==9) ((p)->eType==FTS5_TERM4 || (p)->eType==FTS5_STRING9)
223469
223470/*
223471** Invoke the xNext method of an Fts5ExprNode object. This macro should be
223472** used as if it has the same signature as the xNext() methods themselves.
223473*/
223474#define fts5ExprNodeNext(a,b,c,d)(b)->xNext((a), (b), (c), (d)) (b)->xNext((a), (b), (c), (d))
223475
223476/*
223477** An instance of the following structure represents a single search term
223478** or term prefix.
223479*/
223480struct Fts5ExprTerm {
223481 u8 bPrefix; /* True for a prefix term */
223482 u8 bFirst; /* True if token must be first in column */
223483 char *zTerm; /* nul-terminated term */
223484 Fts5IndexIter *pIter; /* Iterator for this term */
223485 Fts5ExprTerm *pSynonym; /* Pointer to first in list of synonyms */
223486};
223487
223488/*
223489** A phrase. One or more terms that must appear in a contiguous sequence
223490** within a document for it to match.
223491*/
223492struct Fts5ExprPhrase {
223493 Fts5ExprNode *pNode; /* FTS5_STRING node this phrase is part of */
223494 Fts5Buffer poslist; /* Current position list */
223495 int nTerm; /* Number of entries in aTerm[] */
223496 Fts5ExprTerm aTerm[1]; /* Terms that make up this phrase */
223497};
223498
223499/*
223500** One or more phrases that must appear within a certain token distance of
223501** each other within each matching document.
223502*/
223503struct Fts5ExprNearset {
223504 int nNear; /* NEAR parameter */
223505 Fts5Colset *pColset; /* Columns to search (NULL -> all columns) */
223506 int nPhrase; /* Number of entries in aPhrase[] array */
223507 Fts5ExprPhrase *apPhrase[1]; /* Array of phrase pointers */
223508};
223509
223510
223511/*
223512** Parse context.
223513*/
223514struct Fts5Parse {
223515 Fts5Config *pConfig;
223516 char *zErr;
223517 int rc;
223518 int nPhrase; /* Size of apPhrase array */
223519 Fts5ExprPhrase **apPhrase; /* Array of all phrases */
223520 Fts5ExprNode *pExpr; /* Result of a successful parse */
223521 int bPhraseToAnd; /* Convert "a+b" to "a AND b" */
223522};
223523
223524static void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...){
223525 va_list ap;
223526 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
223527 if( pParse->rc==SQLITE_OK0 ){
223528 assert( pParse->zErr==0 )((void) (0));
223529 pParse->zErr = sqlite3_vmprintf(zFmt, ap);
223530 pParse->rc = SQLITE_ERROR1;
223531 }
223532 va_end(ap)__builtin_va_end(ap);
223533}
223534
223535static int fts5ExprIsspace(char t){
223536 return t==' ' || t=='\t' || t=='\n' || t=='\r';
223537}
223538
223539/*
223540** Read the first token from the nul-terminated string at *pz.
223541*/
223542static int fts5ExprGetToken(
223543 Fts5Parse *pParse,
223544 const char **pz, /* IN/OUT: Pointer into buffer */
223545 Fts5Token *pToken
223546){
223547 const char *z = *pz;
223548 int tok;
223549
223550 /* Skip past any whitespace */
223551 while( fts5ExprIsspace(*z) ) z++;
223552
223553 pToken->p = z;
223554 pToken->n = 1;
223555 switch( *z ){
223556 case '(': tok = FTS5_LP10; break;
223557 case ')': tok = FTS5_RP11; break;
223558 case '{': tok = FTS5_LCP7; break;
223559 case '}': tok = FTS5_RCP8; break;
223560 case ':': tok = FTS5_COLON5; break;
223561 case ',': tok = FTS5_COMMA13; break;
223562 case '+': tok = FTS5_PLUS14; break;
223563 case '*': tok = FTS5_STAR15; break;
223564 case '-': tok = FTS5_MINUS6; break;
223565 case '^': tok = FTS5_CARET12; break;
223566 case '\0': tok = FTS5_EOF0; break;
223567
223568 case '"': {
223569 const char *z2;
223570 tok = FTS5_STRING9;
223571
223572 for(z2=&z[1]; 1; z2++){
223573 if( z2[0]=='"' ){
223574 z2++;
223575 if( z2[0]!='"' ) break;
223576 }
223577 if( z2[0]=='\0' ){
223578 sqlite3Fts5ParseError(pParse, "unterminated string");
223579 return FTS5_EOF0;
223580 }
223581 }
223582 pToken->n = (z2 - z);
223583 break;
223584 }
223585
223586 default: {
223587 const char *z2;
223588 if( sqlite3Fts5IsBareword(z[0])==0 ){
223589 sqlite3Fts5ParseError(pParse, "fts5: syntax error near \"%.1s\"", z);
223590 return FTS5_EOF0;
223591 }
223592 tok = FTS5_STRING9;
223593 for(z2=&z[1]; sqlite3Fts5IsBareword(*z2); z2++);
223594 pToken->n = (z2 - z);
223595 if( pToken->n==2 && memcmp(pToken->p, "OR", 2)==0 ) tok = FTS5_OR1;
223596 if( pToken->n==3 && memcmp(pToken->p, "NOT", 3)==0 ) tok = FTS5_NOT3;
223597 if( pToken->n==3 && memcmp(pToken->p, "AND", 3)==0 ) tok = FTS5_AND2;
223598 break;
223599 }
223600 }
223601
223602 *pz = &pToken->p[pToken->n];
223603 return tok;
223604}
223605
223606static void *fts5ParseAlloc(u64 t){ return sqlite3_malloc64((sqlite3_int64)t);}
223607static void fts5ParseFree(void *p){ sqlite3_free(p); }
223608
223609static int sqlite3Fts5ExprNew(
223610 Fts5Config *pConfig, /* FTS5 Configuration */
223611 int bPhraseToAnd,
223612 int iCol,
223613 const char *zExpr, /* Expression text */
223614 Fts5Expr **ppNew,
223615 char **pzErr
223616){
223617 Fts5Parse sParse;
223618 Fts5Token token;
223619 const char *z = zExpr;
223620 int t; /* Next token type */
223621 void *pEngine;
223622 Fts5Expr *pNew;
223623
223624 *ppNew = 0;
223625 *pzErr = 0;
223626 memset(&sParse, 0, sizeof(sParse));
223627 sParse.bPhraseToAnd = bPhraseToAnd;
223628 pEngine = sqlite3Fts5ParserAlloc(fts5ParseAlloc);
223629 if( pEngine==0 ){ return SQLITE_NOMEM7; }
223630 sParse.pConfig = pConfig;
223631
223632 do {
223633 t = fts5ExprGetToken(&sParse, &z, &token);
223634 sqlite3Fts5Parser(pEngine, t, token, &sParse);
223635 }while( sParse.rc==SQLITE_OK0 && t!=FTS5_EOF0 );
223636 sqlite3Fts5ParserFree(pEngine, fts5ParseFree);
223637
223638 /* If the LHS of the MATCH expression was a user column, apply the
223639 ** implicit column-filter. */
223640 if( iCol<pConfig->nCol && sParse.pExpr && sParse.rc==SQLITE_OK0 ){
223641 int n = sizeof(Fts5Colset);
223642 Fts5Colset *pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&sParse.rc, n);
223643 if( pColset ){
223644 pColset->nCol = 1;
223645 pColset->aiCol[0] = iCol;
223646 sqlite3Fts5ParseSetColset(&sParse, sParse.pExpr, pColset);
223647 }
223648 }
223649
223650 assert( sParse.rc!=SQLITE_OK || sParse.zErr==0 )((void) (0));
223651 if( sParse.rc==SQLITE_OK0 ){
223652 *ppNew = pNew = sqlite3_malloc(sizeof(Fts5Expr));
223653 if( pNew==0 ){
223654 sParse.rc = SQLITE_NOMEM7;
223655 sqlite3Fts5ParseNodeFree(sParse.pExpr);
223656 }else{
223657 if( !sParse.pExpr ){
223658 const int nByte = sizeof(Fts5ExprNode);
223659 pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&sParse.rc, nByte);
223660 if( pNew->pRoot ){
223661 pNew->pRoot->bEof = 1;
223662 }
223663 }else{
223664 pNew->pRoot = sParse.pExpr;
223665 }
223666 pNew->pIndex = 0;
223667 pNew->pConfig = pConfig;
223668 pNew->apExprPhrase = sParse.apPhrase;
223669 pNew->nPhrase = sParse.nPhrase;
223670 pNew->bDesc = 0;
223671 sParse.apPhrase = 0;
223672 }
223673 }else{
223674 sqlite3Fts5ParseNodeFree(sParse.pExpr);
223675 }
223676
223677 sqlite3_free(sParse.apPhrase);
223678 *pzErr = sParse.zErr;
223679 return sParse.rc;
223680}
223681
223682/*
223683** This function is only called when using the special 'trigram' tokenizer.
223684** Argument zText contains the text of a LIKE or GLOB pattern matched
223685** against column iCol. This function creates and compiles an FTS5 MATCH
223686** expression that will match a superset of the rows matched by the LIKE or
223687** GLOB. If successful, SQLITE_OK is returned. Otherwise, an SQLite error
223688** code.
223689*/
223690static int sqlite3Fts5ExprPattern(
223691 Fts5Config *pConfig, int bGlob, int iCol, const char *zText, Fts5Expr **pp
223692){
223693 i64 nText = strlen(zText);
223694 char *zExpr = (char*)sqlite3_malloc64(nText*4 + 1);
223695 int rc = SQLITE_OK0;
223696
223697 if( zExpr==0 ){
223698 rc = SQLITE_NOMEM7;
223699 }else{
223700 char aSpec[3];
223701 int iOut = 0;
223702 int i = 0;
223703 int iFirst = 0;
223704
223705 if( bGlob==0 ){
223706 aSpec[0] = '_';
223707 aSpec[1] = '%';
223708 aSpec[2] = 0;
223709 }else{
223710 aSpec[0] = '*';
223711 aSpec[1] = '?';
223712 aSpec[2] = '[';
223713 }
223714
223715 while( i<=nText ){
223716 if( i==nText
223717 || zText[i]==aSpec[0] || zText[i]==aSpec[1] || zText[i]==aSpec[2]
223718 ){
223719 if( i-iFirst>=3 ){
223720 int jj;
223721 zExpr[iOut++] = '"';
223722 for(jj=iFirst; jj<i; jj++){
223723 zExpr[iOut++] = zText[jj];
223724 if( zText[jj]=='"' ) zExpr[iOut++] = '"';
223725 }
223726 zExpr[iOut++] = '"';
223727 zExpr[iOut++] = ' ';
223728 }
223729 if( zText[i]==aSpec[2] ){
223730 i += 2;
223731 if( zText[i-1]=='^' ) i++;
223732 while( i<nText && zText[i]!=']' ) i++;
223733 }
223734 iFirst = i+1;
223735 }
223736 i++;
223737 }
223738 if( iOut>0 ){
223739 int bAnd = 0;
223740 if( pConfig->eDetail!=FTS5_DETAIL_FULL0 ){
223741 bAnd = 1;
223742 if( pConfig->eDetail==FTS5_DETAIL_NONE1 ){
223743 iCol = pConfig->nCol;
223744 }
223745 }
223746 zExpr[iOut] = '\0';
223747 rc = sqlite3Fts5ExprNew(pConfig, bAnd, iCol, zExpr, pp,pConfig->pzErrmsg);
223748 }else{
223749 *pp = 0;
223750 }
223751 sqlite3_free(zExpr);
223752 }
223753
223754 return rc;
223755}
223756
223757/*
223758** Free the expression node object passed as the only argument.
223759*/
223760static void sqlite3Fts5ParseNodeFree(Fts5ExprNode *p){
223761 if( p ){
223762 int i;
223763 for(i=0; i<p->nChild; i++){
223764 sqlite3Fts5ParseNodeFree(p->apChild[i]);
223765 }
223766 sqlite3Fts5ParseNearsetFree(p->pNear);
223767 sqlite3_free(p);
223768 }
223769}
223770
223771/*
223772** Free the expression object passed as the only argument.
223773*/
223774static void sqlite3Fts5ExprFree(Fts5Expr *p){
223775 if( p ){
223776 sqlite3Fts5ParseNodeFree(p->pRoot);
223777 sqlite3_free(p->apExprPhrase);
223778 sqlite3_free(p);
223779 }
223780}
223781
223782static int sqlite3Fts5ExprAnd(Fts5Expr **pp1, Fts5Expr *p2){
223783 Fts5Parse sParse;
223784 memset(&sParse, 0, sizeof(sParse));
223785
223786 if( *pp1 ){
223787 Fts5Expr *p1 = *pp1;
223788 int nPhrase = p1->nPhrase + p2->nPhrase;
223789
223790 p1->pRoot = sqlite3Fts5ParseNode(&sParse, FTS5_AND2, p1->pRoot, p2->pRoot,0);
223791 p2->pRoot = 0;
223792
223793 if( sParse.rc==SQLITE_OK0 ){
223794 Fts5ExprPhrase **ap = (Fts5ExprPhrase**)sqlite3_realloc(
223795 p1->apExprPhrase, nPhrase * sizeof(Fts5ExprPhrase*)
223796 );
223797 if( ap==0 ){
223798 sParse.rc = SQLITE_NOMEM7;
223799 }else{
223800 int i;
223801 memmove(&ap[p2->nPhrase], ap, p1->nPhrase*sizeof(Fts5ExprPhrase*));
223802 for(i=0; i<p2->nPhrase; i++){
223803 ap[i] = p2->apExprPhrase[i];
223804 }
223805 p1->nPhrase = nPhrase;
223806 p1->apExprPhrase = ap;
223807 }
223808 }
223809 sqlite3_free(p2->apExprPhrase);
223810 sqlite3_free(p2);
223811 }else{
223812 *pp1 = p2;
223813 }
223814
223815 return sParse.rc;
223816}
223817
223818/*
223819** Argument pTerm must be a synonym iterator. Return the current rowid
223820** that it points to.
223821*/
223822static i64 fts5ExprSynonymRowid(Fts5ExprTerm *pTerm, int bDesc, int *pbEof){
223823 i64 iRet = 0;
223824 int bRetValid = 0;
223825 Fts5ExprTerm *p;
223826
223827 assert( pTerm )((void) (0));
223828 assert( pTerm->pSynonym )((void) (0));
223829 assert( bDesc==0 || bDesc==1 )((void) (0));
223830 for(p=pTerm; p; p=p->pSynonym){
223831 if( 0==sqlite3Fts5IterEof(p->pIter)((p->pIter)->bEof) ){
223832 i64 iRowid = p->pIter->iRowid;
223833 if( bRetValid==0 || (bDesc!=(iRowid<iRet)) ){
223834 iRet = iRowid;
223835 bRetValid = 1;
223836 }
223837 }
223838 }
223839
223840 if( pbEof && bRetValid==0 ) *pbEof = 1;
223841 return iRet;
223842}
223843
223844/*
223845** Argument pTerm must be a synonym iterator.
223846*/
223847static int fts5ExprSynonymList(
223848 Fts5ExprTerm *pTerm,
223849 i64 iRowid,
223850 Fts5Buffer *pBuf, /* Use this buffer for space if required */
223851 u8 **pa, int *pn
223852){
223853 Fts5PoslistReader aStatic[4];
223854 Fts5PoslistReader *aIter = aStatic;
223855 int nIter = 0;
223856 int nAlloc = 4;
223857 int rc = SQLITE_OK0;
223858 Fts5ExprTerm *p;
223859
223860 assert( pTerm->pSynonym )((void) (0));
223861 for(p=pTerm; p; p=p->pSynonym){
223862 Fts5IndexIter *pIter = p->pIter;
223863 if( sqlite3Fts5IterEof(pIter)((pIter)->bEof)==0 && pIter->iRowid==iRowid ){
223864 if( pIter->nData==0 ) continue;
223865 if( nIter==nAlloc ){
223866 sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * nAlloc * 2;
223867 Fts5PoslistReader *aNew = (Fts5PoslistReader*)sqlite3_malloc64(nByte);
223868 if( aNew==0 ){
223869 rc = SQLITE_NOMEM7;
223870 goto synonym_poslist_out;
223871 }
223872 memcpy(aNew, aIter, sizeof(Fts5PoslistReader) * nIter);
223873 nAlloc = nAlloc*2;
223874 if( aIter!=aStatic ) sqlite3_free(aIter);
223875 aIter = aNew;
223876 }
223877 sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &aIter[nIter]);
223878 assert( aIter[nIter].bEof==0 )((void) (0));
223879 nIter++;
223880 }
223881 }
223882
223883 if( nIter==1 ){
223884 *pa = (u8*)aIter[0].a;
223885 *pn = aIter[0].n;
223886 }else{
223887 Fts5PoslistWriter writer = {0};
223888 i64 iPrev = -1;
223889 fts5BufferZero(pBuf)sqlite3Fts5BufferZero(pBuf);
223890 while( 1 ){
223891 int i;
223892 i64 iMin = FTS5_LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
223893 for(i=0; i<nIter; i++){
223894 if( aIter[i].bEof==0 ){
223895 if( aIter[i].iPos==iPrev ){
223896 if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) continue;
223897 }
223898 if( aIter[i].iPos<iMin ){
223899 iMin = aIter[i].iPos;
223900 }
223901 }
223902 }
223903 if( iMin==FTS5_LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) || rc!=SQLITE_OK0 ) break;
223904 rc = sqlite3Fts5PoslistWriterAppend(pBuf, &writer, iMin);
223905 iPrev = iMin;
223906 }
223907 if( rc==SQLITE_OK0 ){
223908 *pa = pBuf->p;
223909 *pn = pBuf->n;
223910 }
223911 }
223912
223913 synonym_poslist_out:
223914 if( aIter!=aStatic ) sqlite3_free(aIter);
223915 return rc;
223916}
223917
223918
223919/*
223920** All individual term iterators in pPhrase are guaranteed to be valid and
223921** pointing to the same rowid when this function is called. This function
223922** checks if the current rowid really is a match, and if so populates
223923** the pPhrase->poslist buffer accordingly. Output parameter *pbMatch
223924** is set to true if this is really a match, or false otherwise.
223925**
223926** SQLITE_OK is returned if an error occurs, or an SQLite error code
223927** otherwise. It is not considered an error code if the current rowid is
223928** not a match.
223929*/
223930static int fts5ExprPhraseIsMatch(
223931 Fts5ExprNode *pNode, /* Node pPhrase belongs to */
223932 Fts5ExprPhrase *pPhrase, /* Phrase object to initialize */
223933 int *pbMatch /* OUT: Set to true if really a match */
223934){
223935 Fts5PoslistWriter writer = {0};
223936 Fts5PoslistReader aStatic[4];
223937 Fts5PoslistReader *aIter = aStatic;
223938 int i;
223939 int rc = SQLITE_OK0;
223940 int bFirst = pPhrase->aTerm[0].bFirst;
223941
223942 fts5BufferZero(&pPhrase->poslist)sqlite3Fts5BufferZero(&pPhrase->poslist);
223943
223944 /* If the aStatic[] array is not large enough, allocate a large array
223945 ** using sqlite3_malloc(). This approach could be improved upon. */
223946 if( pPhrase->nTerm>ArraySize(aStatic)((int)(sizeof(aStatic)/sizeof(aStatic[0]))) ){
223947 sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * pPhrase->nTerm;
223948 aIter = (Fts5PoslistReader*)sqlite3_malloc64(nByte);
223949 if( !aIter ) return SQLITE_NOMEM7;
223950 }
223951 memset(aIter, 0, sizeof(Fts5PoslistReader) * pPhrase->nTerm);
223952
223953 /* Initialize a term iterator for each term in the phrase */
223954 for(i=0; i<pPhrase->nTerm; i++){
223955 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
223956 int n = 0;
223957 int bFlag = 0;
223958 u8 *a = 0;
223959 if( pTerm->pSynonym ){
223960 Fts5Buffer buf = {0, 0, 0};
223961 rc = fts5ExprSynonymList(pTerm, pNode->iRowid, &buf, &a, &n);
223962 if( rc ){
223963 sqlite3_free(a);
223964 goto ismatch_out;
223965 }
223966 if( a==buf.p ) bFlag = 1;
223967 }else{
223968 a = (u8*)pTerm->pIter->pData;
223969 n = pTerm->pIter->nData;
223970 }
223971 sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]);
223972 aIter[i].bFlag = (u8)bFlag;
223973 if( aIter[i].bEof ) goto ismatch_out;
223974 }
223975
223976 while( 1 ){
223977 int bMatch;
223978 i64 iPos = aIter[0].iPos;
223979 do {
223980 bMatch = 1;
223981 for(i=0; i<pPhrase->nTerm; i++){
223982 Fts5PoslistReader *pPos = &aIter[i];
223983 i64 iAdj = iPos + i;
223984 if( pPos->iPos!=iAdj ){
223985 bMatch = 0;
223986 while( pPos->iPos<iAdj ){
223987 if( sqlite3Fts5PoslistReaderNext(pPos) ) goto ismatch_out;
223988 }
223989 if( pPos->iPos>iAdj ) iPos = pPos->iPos-i;
223990 }
223991 }
223992 }while( bMatch==0 );
223993
223994 /* Append position iPos to the output */
223995 if( bFirst==0 || FTS5_POS2OFFSET(iPos)(int)(iPos & 0x7FFFFFFF)==0 ){
223996 rc = sqlite3Fts5PoslistWriterAppend(&pPhrase->poslist, &writer, iPos);
223997 if( rc!=SQLITE_OK0 ) goto ismatch_out;
223998 }
223999
224000 for(i=0; i<pPhrase->nTerm; i++){
224001 if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) goto ismatch_out;
224002 }
224003 }
224004
224005 ismatch_out:
224006 *pbMatch = (pPhrase->poslist.n>0);
224007 for(i=0; i<pPhrase->nTerm; i++){
224008 if( aIter[i].bFlag ) sqlite3_free((u8*)aIter[i].a);
224009 }
224010 if( aIter!=aStatic ) sqlite3_free(aIter);
224011 return rc;
224012}
224013
224014typedef struct Fts5LookaheadReader Fts5LookaheadReader;
224015struct Fts5LookaheadReader {
224016 const u8 *a; /* Buffer containing position list */
224017 int n; /* Size of buffer a[] in bytes */
224018 int i; /* Current offset in position list */
224019 i64 iPos; /* Current position */
224020 i64 iLookahead; /* Next position */
224021};
224022
224023#define FTS5_LOOKAHEAD_EOF(((i64)1) << 62) (((i64)1) << 62)
224024
224025static int fts5LookaheadReaderNext(Fts5LookaheadReader *p){
224026 p->iPos = p->iLookahead;
224027 if( sqlite3Fts5PoslistNext64(p->a, p->n, &p->i, &p->iLookahead) ){
224028 p->iLookahead = FTS5_LOOKAHEAD_EOF(((i64)1) << 62);
224029 }
224030 return (p->iPos==FTS5_LOOKAHEAD_EOF(((i64)1) << 62));
224031}
224032
224033static int fts5LookaheadReaderInit(
224034 const u8 *a, int n, /* Buffer to read position list from */
224035 Fts5LookaheadReader *p /* Iterator object to initialize */
224036){
224037 memset(p, 0, sizeof(Fts5LookaheadReader));
224038 p->a = a;
224039 p->n = n;
224040 fts5LookaheadReaderNext(p);
224041 return fts5LookaheadReaderNext(p);
224042}
224043
224044typedef struct Fts5NearTrimmer Fts5NearTrimmer;
224045struct Fts5NearTrimmer {
224046 Fts5LookaheadReader reader; /* Input iterator */
224047 Fts5PoslistWriter writer; /* Writer context */
224048 Fts5Buffer *pOut; /* Output poslist */
224049};
224050
224051/*
224052** The near-set object passed as the first argument contains more than
224053** one phrase. All phrases currently point to the same row. The
224054** Fts5ExprPhrase.poslist buffers are populated accordingly. This function
224055** tests if the current row contains instances of each phrase sufficiently
224056** close together to meet the NEAR constraint. Non-zero is returned if it
224057** does, or zero otherwise.
224058**
224059** If in/out parameter (*pRc) is set to other than SQLITE_OK when this
224060** function is called, it is a no-op. Or, if an error (e.g. SQLITE_NOMEM)
224061** occurs within this function (*pRc) is set accordingly before returning.
224062** The return value is undefined in both these cases.
224063**
224064** If no error occurs and non-zero (a match) is returned, the position-list
224065** of each phrase object is edited to contain only those entries that
224066** meet the constraint before returning.
224067*/
224068static int fts5ExprNearIsMatch(int *pRc, Fts5ExprNearset *pNear){
224069 Fts5NearTrimmer aStatic[4];
224070 Fts5NearTrimmer *a = aStatic;
224071 Fts5ExprPhrase **apPhrase = pNear->apPhrase;
224072
224073 int i;
224074 int rc = *pRc;
224075 int bMatch;
224076
224077 assert( pNear->nPhrase>1 )((void) (0));
224078
224079 /* If the aStatic[] array is not large enough, allocate a large array
224080 ** using sqlite3_malloc(). This approach could be improved upon. */
224081 if( pNear->nPhrase>ArraySize(aStatic)((int)(sizeof(aStatic)/sizeof(aStatic[0]))) ){
224082 sqlite3_int64 nByte = sizeof(Fts5NearTrimmer) * pNear->nPhrase;
224083 a = (Fts5NearTrimmer*)sqlite3Fts5MallocZero(&rc, nByte);
224084 }else{
224085 memset(aStatic, 0, sizeof(aStatic));
224086 }
224087 if( rc!=SQLITE_OK0 ){
224088 *pRc = rc;
224089 return 0;
224090 }
224091
224092 /* Initialize a lookahead iterator for each phrase. After passing the
224093 ** buffer and buffer size to the lookaside-reader init function, zero
224094 ** the phrase poslist buffer. The new poslist for the phrase (containing
224095 ** the same entries as the original with some entries removed on account
224096 ** of the NEAR constraint) is written over the original even as it is
224097 ** being read. This is safe as the entries for the new poslist are a
224098 ** subset of the old, so it is not possible for data yet to be read to
224099 ** be overwritten. */
224100 for(i=0; i<pNear->nPhrase; i++){
224101 Fts5Buffer *pPoslist = &apPhrase[i]->poslist;
224102 fts5LookaheadReaderInit(pPoslist->p, pPoslist->n, &a[i].reader);
224103 pPoslist->n = 0;
224104 a[i].pOut = pPoslist;
224105 }
224106
224107 while( 1 ){
224108 int iAdv;
224109 i64 iMin;
224110 i64 iMax;
224111
224112 /* This block advances the phrase iterators until they point to a set of
224113 ** entries that together comprise a match. */
224114 iMax = a[0].reader.iPos;
224115 do {
224116 bMatch = 1;
224117 for(i=0; i<pNear->nPhrase; i++){
224118 Fts5LookaheadReader *pPos = &a[i].reader;
224119 iMin = iMax - pNear->apPhrase[i]->nTerm - pNear->nNear;
224120 if( pPos->iPos<iMin || pPos->iPos>iMax ){
224121 bMatch = 0;
224122 while( pPos->iPos<iMin ){
224123 if( fts5LookaheadReaderNext(pPos) ) goto ismatch_out;
224124 }
224125 if( pPos->iPos>iMax ) iMax = pPos->iPos;
224126 }
224127 }
224128 }while( bMatch==0 );
224129
224130 /* Add an entry to each output position list */
224131 for(i=0; i<pNear->nPhrase; i++){
224132 i64 iPos = a[i].reader.iPos;
224133 Fts5PoslistWriter *pWriter = &a[i].writer;
224134 if( a[i].pOut->n==0 || iPos!=pWriter->iPrev ){
224135 sqlite3Fts5PoslistWriterAppend(a[i].pOut, pWriter, iPos);
224136 }
224137 }
224138
224139 iAdv = 0;
224140 iMin = a[0].reader.iLookahead;
224141 for(i=0; i<pNear->nPhrase; i++){
224142 if( a[i].reader.iLookahead < iMin ){
224143 iMin = a[i].reader.iLookahead;
224144 iAdv = i;
224145 }
224146 }
224147 if( fts5LookaheadReaderNext(&a[iAdv].reader) ) goto ismatch_out;
224148 }
224149
224150 ismatch_out: {
224151 int bRet = a[0].pOut->n>0;
224152 *pRc = rc;
224153 if( a!=aStatic ) sqlite3_free(a);
224154 return bRet;
224155 }
224156}
224157
224158/*
224159** Advance iterator pIter until it points to a value equal to or laster
224160** than the initial value of *piLast. If this means the iterator points
224161** to a value laster than *piLast, update *piLast to the new lastest value.
224162**
224163** If the iterator reaches EOF, set *pbEof to true before returning. If
224164** an error occurs, set *pRc to an error code. If either *pbEof or *pRc
224165** are set, return a non-zero value. Otherwise, return zero.
224166*/
224167static int fts5ExprAdvanceto(
224168 Fts5IndexIter *pIter, /* Iterator to advance */
224169 int bDesc, /* True if iterator is "rowid DESC" */
224170 i64 *piLast, /* IN/OUT: Lastest rowid seen so far */
224171 int *pRc, /* OUT: Error code */
224172 int *pbEof /* OUT: Set to true if EOF */
224173){
224174 i64 iLast = *piLast;
224175 i64 iRowid;
224176
224177 iRowid = pIter->iRowid;
224178 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
224179 int rc = sqlite3Fts5IterNextFrom(pIter, iLast);
224180 if( rc || sqlite3Fts5IterEof(pIter)((pIter)->bEof) ){
224181 *pRc = rc;
224182 *pbEof = 1;
224183 return 1;
224184 }
224185 iRowid = pIter->iRowid;
224186 assert( (bDesc==0 && iRowid>=iLast) || (bDesc==1 && iRowid<=iLast) )((void) (0));
224187 }
224188 *piLast = iRowid;
224189
224190 return 0;
224191}
224192
224193static int fts5ExprSynonymAdvanceto(
224194 Fts5ExprTerm *pTerm, /* Term iterator to advance */
224195 int bDesc, /* True if iterator is "rowid DESC" */
224196 i64 *piLast, /* IN/OUT: Lastest rowid seen so far */
224197 int *pRc /* OUT: Error code */
224198){
224199 int rc = SQLITE_OK0;
224200 i64 iLast = *piLast;
224201 Fts5ExprTerm *p;
224202 int bEof = 0;
224203
224204 for(p=pTerm; rc==SQLITE_OK0 && p; p=p->pSynonym){
224205 if( sqlite3Fts5IterEof(p->pIter)((p->pIter)->bEof)==0 ){
224206 i64 iRowid = p->pIter->iRowid;
224207 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
224208 rc = sqlite3Fts5IterNextFrom(p->pIter, iLast);
224209 }
224210 }
224211 }
224212
224213 if( rc!=SQLITE_OK0 ){
224214 *pRc = rc;
224215 bEof = 1;
224216 }else{
224217 *piLast = fts5ExprSynonymRowid(pTerm, bDesc, &bEof);
224218 }
224219 return bEof;
224220}
224221
224222
224223static int fts5ExprNearTest(
224224 int *pRc,
224225 Fts5Expr *pExpr, /* Expression that pNear is a part of */
224226 Fts5ExprNode *pNode /* The "NEAR" node (FTS5_STRING) */
224227){
224228 Fts5ExprNearset *pNear = pNode->pNear;
224229 int rc = *pRc;
224230
224231 if( pExpr->pConfig->eDetail!=FTS5_DETAIL_FULL0 ){
224232 Fts5ExprTerm *pTerm;
224233 Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
224234 pPhrase->poslist.n = 0;
224235 for(pTerm=&pPhrase->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
224236 Fts5IndexIter *pIter = pTerm->pIter;
224237 if( sqlite3Fts5IterEof(pIter)((pIter)->bEof)==0 ){
224238 if( pIter->iRowid==pNode->iRowid && pIter->nData>0 ){
224239 pPhrase->poslist.n = 1;
224240 }
224241 }
224242 }
224243 return pPhrase->poslist.n;
224244 }else{
224245 int i;
224246
224247 /* Check that each phrase in the nearset matches the current row.
224248 ** Populate the pPhrase->poslist buffers at the same time. If any
224249 ** phrase is not a match, break out of the loop early. */
224250 for(i=0; rc==SQLITE_OK0 && i<pNear->nPhrase; i++){
224251 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
224252 if( pPhrase->nTerm>1 || pPhrase->aTerm[0].pSynonym
224253 || pNear->pColset || pPhrase->aTerm[0].bFirst
224254 ){
224255 int bMatch = 0;
224256 rc = fts5ExprPhraseIsMatch(pNode, pPhrase, &bMatch);
224257 if( bMatch==0 ) break;
224258 }else{
224259 Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
224260 fts5BufferSet(&rc, &pPhrase->poslist, pIter->nData, pIter->pData)sqlite3Fts5BufferSet(&rc,&pPhrase->poslist,pIter->
nData,pIter->pData)
;
224261 }
224262 }
224263
224264 *pRc = rc;
224265 if( i==pNear->nPhrase && (i==1 || fts5ExprNearIsMatch(pRc, pNear)) ){
224266 return 1;
224267 }
224268 return 0;
224269 }
224270}
224271
224272
224273/*
224274** Initialize all term iterators in the pNear object. If any term is found
224275** to match no documents at all, return immediately without initializing any
224276** further iterators.
224277**
224278** If an error occurs, return an SQLite error code. Otherwise, return
224279** SQLITE_OK. It is not considered an error if some term matches zero
224280** documents.
224281*/
224282static int fts5ExprNearInitAll(
224283 Fts5Expr *pExpr,
224284 Fts5ExprNode *pNode
224285){
224286 Fts5ExprNearset *pNear = pNode->pNear;
224287 int i;
224288
224289 assert( pNode->bNomatch==0 )((void) (0));
224290 for(i=0; i<pNear->nPhrase; i++){
224291 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
224292 if( pPhrase->nTerm==0 ){
224293 pNode->bEof = 1;
224294 return SQLITE_OK0;
224295 }else{
224296 int j;
224297 for(j=0; j<pPhrase->nTerm; j++){
224298 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
224299 Fts5ExprTerm *p;
224300 int bHit = 0;
224301
224302 for(p=pTerm; p; p=p->pSynonym){
224303 int rc;
224304 if( p->pIter ){
224305 sqlite3Fts5IterClose(p->pIter);
224306 p->pIter = 0;
224307 }
224308 rc = sqlite3Fts5IndexQuery(
224309 pExpr->pIndex, p->zTerm, (int)strlen(p->zTerm),
224310 (pTerm->bPrefix ? FTS5INDEX_QUERY_PREFIX0x0001 : 0) |
224311 (pExpr->bDesc ? FTS5INDEX_QUERY_DESC0x0002 : 0),
224312 pNear->pColset,
224313 &p->pIter
224314 );
224315 assert( (rc==SQLITE_OK)==(p->pIter!=0) )((void) (0));
224316 if( rc!=SQLITE_OK0 ) return rc;
224317 if( 0==sqlite3Fts5IterEof(p->pIter)((p->pIter)->bEof) ){
224318 bHit = 1;
224319 }
224320 }
224321
224322 if( bHit==0 ){
224323 pNode->bEof = 1;
224324 return SQLITE_OK0;
224325 }
224326 }
224327 }
224328 }
224329
224330 pNode->bEof = 0;
224331 return SQLITE_OK0;
224332}
224333
224334/*
224335** If pExpr is an ASC iterator, this function returns a value with the
224336** same sign as:
224337**
224338** (iLhs - iRhs)
224339**
224340** Otherwise, if this is a DESC iterator, the opposite is returned:
224341**
224342** (iRhs - iLhs)
224343*/
224344static int fts5RowidCmp(
224345 Fts5Expr *pExpr,
224346 i64 iLhs,
224347 i64 iRhs
224348){
224349 assert( pExpr->bDesc==0 || pExpr->bDesc==1 )((void) (0));
224350 if( pExpr->bDesc==0 ){
224351 if( iLhs<iRhs ) return -1;
224352 return (iLhs > iRhs);
224353 }else{
224354 if( iLhs>iRhs ) return -1;
224355 return (iLhs < iRhs);
224356 }
224357}
224358
224359static void fts5ExprSetEof(Fts5ExprNode *pNode){
224360 int i;
224361 pNode->bEof = 1;
224362 pNode->bNomatch = 0;
224363 for(i=0; i<pNode->nChild; i++){
224364 fts5ExprSetEof(pNode->apChild[i]);
224365 }
224366}
224367
224368static void fts5ExprNodeZeroPoslist(Fts5ExprNode *pNode){
224369 if( pNode->eType==FTS5_STRING9 || pNode->eType==FTS5_TERM4 ){
224370 Fts5ExprNearset *pNear = pNode->pNear;
224371 int i;
224372 for(i=0; i<pNear->nPhrase; i++){
224373 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
224374 pPhrase->poslist.n = 0;
224375 }
224376 }else{
224377 int i;
224378 for(i=0; i<pNode->nChild; i++){
224379 fts5ExprNodeZeroPoslist(pNode->apChild[i]);
224380 }
224381 }
224382}
224383
224384
224385
224386/*
224387** Compare the values currently indicated by the two nodes as follows:
224388**
224389** res = (*p1) - (*p2)
224390**
224391** Nodes that point to values that come later in the iteration order are
224392** considered to be larger. Nodes at EOF are the largest of all.
224393**
224394** This means that if the iteration order is ASC, then numerically larger
224395** rowids are considered larger. Or if it is the default DESC, numerically
224396** smaller rowids are larger.
224397*/
224398static int fts5NodeCompare(
224399 Fts5Expr *pExpr,
224400 Fts5ExprNode *p1,
224401 Fts5ExprNode *p2
224402){
224403 if( p2->bEof ) return -1;
224404 if( p1->bEof ) return +1;
224405 return fts5RowidCmp(pExpr, p1->iRowid, p2->iRowid);
224406}
224407
224408/*
224409** All individual term iterators in pNear are guaranteed to be valid when
224410** this function is called. This function checks if all term iterators
224411** point to the same rowid, and if not, advances them until they do.
224412** If an EOF is reached before this happens, *pbEof is set to true before
224413** returning.
224414**
224415** SQLITE_OK is returned if an error occurs, or an SQLite error code
224416** otherwise. It is not considered an error code if an iterator reaches
224417** EOF.
224418*/
224419static int fts5ExprNodeTest_STRING(
224420 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
224421 Fts5ExprNode *pNode
224422){
224423 Fts5ExprNearset *pNear = pNode->pNear;
224424 Fts5ExprPhrase *pLeft = pNear->apPhrase[0];
224425 int rc = SQLITE_OK0;
224426 i64 iLast; /* Lastest rowid any iterator points to */
224427 int i, j; /* Phrase and token index, respectively */
224428 int bMatch; /* True if all terms are at the same rowid */
224429 const int bDesc = pExpr->bDesc;
224430
224431 /* Check that this node should not be FTS5_TERM */
224432 assert( pNear->nPhrase>1((void) (0))
224433 || pNear->apPhrase[0]->nTerm>1((void) (0))
224434 || pNear->apPhrase[0]->aTerm[0].pSynonym((void) (0))
224435 || pNear->apPhrase[0]->aTerm[0].bFirst((void) (0))
224436 )((void) (0));
224437
224438 /* Initialize iLast, the "lastest" rowid any iterator points to. If the
224439 ** iterator skips through rowids in the default ascending order, this means
224440 ** the maximum rowid. Or, if the iterator is "ORDER BY rowid DESC", then it
224441 ** means the minimum rowid. */
224442 if( pLeft->aTerm[0].pSynonym ){
224443 iLast = fts5ExprSynonymRowid(&pLeft->aTerm[0], bDesc, 0);
224444 }else{
224445 iLast = pLeft->aTerm[0].pIter->iRowid;
224446 }
224447
224448 do {
224449 bMatch = 1;
224450 for(i=0; i<pNear->nPhrase; i++){
224451 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
224452 for(j=0; j<pPhrase->nTerm; j++){
224453 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
224454 if( pTerm->pSynonym ){
224455 i64 iRowid = fts5ExprSynonymRowid(pTerm, bDesc, 0);
224456 if( iRowid==iLast ) continue;
224457 bMatch = 0;
224458 if( fts5ExprSynonymAdvanceto(pTerm, bDesc, &iLast, &rc) ){
224459 pNode->bNomatch = 0;
224460 pNode->bEof = 1;
224461 return rc;
224462 }
224463 }else{
224464 Fts5IndexIter *pIter = pPhrase->aTerm[j].pIter;
224465 if( pIter->iRowid==iLast || pIter->bEof ) continue;
224466 bMatch = 0;
224467 if( fts5ExprAdvanceto(pIter, bDesc, &iLast, &rc, &pNode->bEof) ){
224468 return rc;
224469 }
224470 }
224471 }
224472 }
224473 }while( bMatch==0 );
224474
224475 pNode->iRowid = iLast;
224476 pNode->bNomatch = ((0==fts5ExprNearTest(&rc, pExpr, pNode)) && rc==SQLITE_OK0);
224477 assert( pNode->bEof==0 || pNode->bNomatch==0 )((void) (0));
224478
224479 return rc;
224480}
224481
224482/*
224483** Advance the first term iterator in the first phrase of pNear. Set output
224484** variable *pbEof to true if it reaches EOF or if an error occurs.
224485**
224486** Return SQLITE_OK if successful, or an SQLite error code if an error
224487** occurs.
224488*/
224489static int fts5ExprNodeNext_STRING(
224490 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
224491 Fts5ExprNode *pNode, /* FTS5_STRING or FTS5_TERM node */
224492 int bFromValid,
224493 i64 iFrom
224494){
224495 Fts5ExprTerm *pTerm = &pNode->pNear->apPhrase[0]->aTerm[0];
224496 int rc = SQLITE_OK0;
224497
224498 pNode->bNomatch = 0;
224499 if( pTerm->pSynonym ){
224500 int bEof = 1;
224501 Fts5ExprTerm *p;
224502
224503 /* Find the firstest rowid any synonym points to. */
224504 i64 iRowid = fts5ExprSynonymRowid(pTerm, pExpr->bDesc, 0);
224505
224506 /* Advance each iterator that currently points to iRowid. Or, if iFrom
224507 ** is valid - each iterator that points to a rowid before iFrom. */
224508 for(p=pTerm; p; p=p->pSynonym){
224509 if( sqlite3Fts5IterEof(p->pIter)((p->pIter)->bEof)==0 ){
224510 i64 ii = p->pIter->iRowid;
224511 if( ii==iRowid
224512 || (bFromValid && ii!=iFrom && (ii>iFrom)==pExpr->bDesc)
224513 ){
224514 if( bFromValid ){
224515 rc = sqlite3Fts5IterNextFrom(p->pIter, iFrom);
224516 }else{
224517 rc = sqlite3Fts5IterNext(p->pIter);
224518 }
224519 if( rc!=SQLITE_OK0 ) break;
224520 if( sqlite3Fts5IterEof(p->pIter)((p->pIter)->bEof)==0 ){
224521 bEof = 0;
224522 }
224523 }else{
224524 bEof = 0;
224525 }
224526 }
224527 }
224528
224529 /* Set the EOF flag if either all synonym iterators are at EOF or an
224530 ** error has occurred. */
224531 pNode->bEof = (rc || bEof);
224532 }else{
224533 Fts5IndexIter *pIter = pTerm->pIter;
224534
224535 assert( Fts5NodeIsString(pNode) )((void) (0));
224536 if( bFromValid ){
224537 rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
224538 }else{
224539 rc = sqlite3Fts5IterNext(pIter);
224540 }
224541
224542 pNode->bEof = (rc || sqlite3Fts5IterEof(pIter)((pIter)->bEof));
224543 }
224544
224545 if( pNode->bEof==0 ){
224546 assert( rc==SQLITE_OK )((void) (0));
224547 rc = fts5ExprNodeTest_STRING(pExpr, pNode);
224548 }
224549
224550 return rc;
224551}
224552
224553
224554static int fts5ExprNodeTest_TERM(
224555 Fts5Expr *pExpr, /* Expression that pNear is a part of */
224556 Fts5ExprNode *pNode /* The "NEAR" node (FTS5_TERM) */
224557){
224558 /* As this "NEAR" object is actually a single phrase that consists
224559 ** of a single term only, grab pointers into the poslist managed by the
224560 ** fts5_index.c iterator object. This is much faster than synthesizing
224561 ** a new poslist the way we have to for more complicated phrase or NEAR
224562 ** expressions. */
224563 Fts5ExprPhrase *pPhrase = pNode->pNear->apPhrase[0];
224564 Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
224565
224566 assert( pNode->eType==FTS5_TERM )((void) (0));
224567 assert( pNode->pNear->nPhrase==1 && pPhrase->nTerm==1 )((void) (0));
224568 assert( pPhrase->aTerm[0].pSynonym==0 )((void) (0));
224569
224570 pPhrase->poslist.n = pIter->nData;
224571 if( pExpr->pConfig->eDetail==FTS5_DETAIL_FULL0 ){
224572 pPhrase->poslist.p = (u8*)pIter->pData;
224573 }
224574 pNode->iRowid = pIter->iRowid;
224575 pNode->bNomatch = (pPhrase->poslist.n==0);
224576 return SQLITE_OK0;
224577}
224578
224579/*
224580** xNext() method for a node of type FTS5_TERM.
224581*/
224582static int fts5ExprNodeNext_TERM(
224583 Fts5Expr *pExpr,
224584 Fts5ExprNode *pNode,
224585 int bFromValid,
224586 i64 iFrom
224587){
224588 int rc;
224589 Fts5IndexIter *pIter = pNode->pNear->apPhrase[0]->aTerm[0].pIter;
224590
224591 assert( pNode->bEof==0 )((void) (0));
224592 if( bFromValid ){
224593 rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
224594 }else{
224595 rc = sqlite3Fts5IterNext(pIter);
224596 }
224597 if( rc==SQLITE_OK0 && sqlite3Fts5IterEof(pIter)((pIter)->bEof)==0 ){
224598 rc = fts5ExprNodeTest_TERM(pExpr, pNode);
224599 }else{
224600 pNode->bEof = 1;
224601 pNode->bNomatch = 0;
224602 }
224603 return rc;
224604}
224605
224606static void fts5ExprNodeTest_OR(
224607 Fts5Expr *pExpr, /* Expression of which pNode is a part */
224608 Fts5ExprNode *pNode /* Expression node to test */
224609){
224610 Fts5ExprNode *pNext = pNode->apChild[0];
224611 int i;
224612
224613 for(i=1; i<pNode->nChild; i++){
224614 Fts5ExprNode *pChild = pNode->apChild[i];
224615 int cmp = fts5NodeCompare(pExpr, pNext, pChild);
224616 if( cmp>0 || (cmp==0 && pChild->bNomatch==0) ){
224617 pNext = pChild;
224618 }
224619 }
224620 pNode->iRowid = pNext->iRowid;
224621 pNode->bEof = pNext->bEof;
224622 pNode->bNomatch = pNext->bNomatch;
224623}
224624
224625static int fts5ExprNodeNext_OR(
224626 Fts5Expr *pExpr,
224627 Fts5ExprNode *pNode,
224628 int bFromValid,
224629 i64 iFrom
224630){
224631 int i;
224632 i64 iLast = pNode->iRowid;
224633
224634 for(i=0; i<pNode->nChild; i++){
224635 Fts5ExprNode *p1 = pNode->apChild[i];
224636 assert( p1->bEof || fts5RowidCmp(pExpr, p1->iRowid, iLast)>=0 )((void) (0));
224637 if( p1->bEof==0 ){
224638 if( (p1->iRowid==iLast)
224639 || (bFromValid && fts5RowidCmp(pExpr, p1->iRowid, iFrom)<0)
224640 ){
224641 int rc = fts5ExprNodeNext(pExpr, p1, bFromValid, iFrom)(p1)->xNext((pExpr), (p1), (bFromValid), (iFrom));
224642 if( rc!=SQLITE_OK0 ){
224643 pNode->bNomatch = 0;
224644 return rc;
224645 }
224646 }
224647 }
224648 }
224649
224650 fts5ExprNodeTest_OR(pExpr, pNode);
224651 return SQLITE_OK0;
224652}
224653
224654/*
224655** Argument pNode is an FTS5_AND node.
224656*/
224657static int fts5ExprNodeTest_AND(
224658 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
224659 Fts5ExprNode *pAnd /* FTS5_AND node to advance */
224660){
224661 int iChild;
224662 i64 iLast = pAnd->iRowid;
224663 int rc = SQLITE_OK0;
224664 int bMatch;
224665
224666 assert( pAnd->bEof==0 )((void) (0));
224667 do {
224668 pAnd->bNomatch = 0;
224669 bMatch = 1;
224670 for(iChild=0; iChild<pAnd->nChild; iChild++){
224671 Fts5ExprNode *pChild = pAnd->apChild[iChild];
224672 int cmp = fts5RowidCmp(pExpr, iLast, pChild->iRowid);
224673 if( cmp>0 ){
224674 /* Advance pChild until it points to iLast or laster */
224675 rc = fts5ExprNodeNext(pExpr, pChild, 1, iLast)(pChild)->xNext((pExpr), (pChild), (1), (iLast));
224676 if( rc!=SQLITE_OK0 ){
224677 pAnd->bNomatch = 0;
224678 return rc;
224679 }
224680 }
224681
224682 /* If the child node is now at EOF, so is the parent AND node. Otherwise,
224683 ** the child node is guaranteed to have advanced at least as far as
224684 ** rowid iLast. So if it is not at exactly iLast, pChild->iRowid is the
224685 ** new lastest rowid seen so far. */
224686 assert( pChild->bEof || fts5RowidCmp(pExpr, iLast, pChild->iRowid)<=0 )((void) (0));
224687 if( pChild->bEof ){
224688 fts5ExprSetEof(pAnd);
224689 bMatch = 1;
224690 break;
224691 }else if( iLast!=pChild->iRowid ){
224692 bMatch = 0;
224693 iLast = pChild->iRowid;
224694 }
224695
224696 if( pChild->bNomatch ){
224697 pAnd->bNomatch = 1;
224698 }
224699 }
224700 }while( bMatch==0 );
224701
224702 if( pAnd->bNomatch && pAnd!=pExpr->pRoot ){
224703 fts5ExprNodeZeroPoslist(pAnd);
224704 }
224705 pAnd->iRowid = iLast;
224706 return SQLITE_OK0;
224707}
224708
224709static int fts5ExprNodeNext_AND(
224710 Fts5Expr *pExpr,
224711 Fts5ExprNode *pNode,
224712 int bFromValid,
224713 i64 iFrom
224714){
224715 int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom)(pNode->apChild[0])->xNext((pExpr), (pNode->apChild[
0]), (bFromValid), (iFrom))
;
224716 if( rc==SQLITE_OK0 ){
224717 rc = fts5ExprNodeTest_AND(pExpr, pNode);
224718 }else{
224719 pNode->bNomatch = 0;
224720 }
224721 return rc;
224722}
224723
224724static int fts5ExprNodeTest_NOT(
224725 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
224726 Fts5ExprNode *pNode /* FTS5_NOT node to advance */
224727){
224728 int rc = SQLITE_OK0;
224729 Fts5ExprNode *p1 = pNode->apChild[0];
224730 Fts5ExprNode *p2 = pNode->apChild[1];
224731 assert( pNode->nChild==2 )((void) (0));
224732
224733 while( rc==SQLITE_OK0 && p1->bEof==0 ){
224734 int cmp = fts5NodeCompare(pExpr, p1, p2);
224735 if( cmp>0 ){
224736 rc = fts5ExprNodeNext(pExpr, p2, 1, p1->iRowid)(p2)->xNext((pExpr), (p2), (1), (p1->iRowid));
224737 cmp = fts5NodeCompare(pExpr, p1, p2);
224738 }
224739 assert( rc!=SQLITE_OK || cmp<=0 )((void) (0));
224740 if( cmp || p2->bNomatch ) break;
224741 rc = fts5ExprNodeNext(pExpr, p1, 0, 0)(p1)->xNext((pExpr), (p1), (0), (0));
224742 }
224743 pNode->bEof = p1->bEof;
224744 pNode->bNomatch = p1->bNomatch;
224745 pNode->iRowid = p1->iRowid;
224746 if( p1->bEof ){
224747 fts5ExprNodeZeroPoslist(p2);
224748 }
224749 return rc;
224750}
224751
224752static int fts5ExprNodeNext_NOT(
224753 Fts5Expr *pExpr,
224754 Fts5ExprNode *pNode,
224755 int bFromValid,
224756 i64 iFrom
224757){
224758 int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom)(pNode->apChild[0])->xNext((pExpr), (pNode->apChild[
0]), (bFromValid), (iFrom))
;
224759 if( rc==SQLITE_OK0 ){
224760 rc = fts5ExprNodeTest_NOT(pExpr, pNode);
224761 }
224762 if( rc!=SQLITE_OK0 ){
224763 pNode->bNomatch = 0;
224764 }
224765 return rc;
224766}
224767
224768/*
224769** If pNode currently points to a match, this function returns SQLITE_OK
224770** without modifying it. Otherwise, pNode is advanced until it does point
224771** to a match or EOF is reached.
224772*/
224773static int fts5ExprNodeTest(
224774 Fts5Expr *pExpr, /* Expression of which pNode is a part */
224775 Fts5ExprNode *pNode /* Expression node to test */
224776){
224777 int rc = SQLITE_OK0;
224778 if( pNode->bEof==0 ){
224779 switch( pNode->eType ){
224780
224781 case FTS5_STRING9: {
224782 rc = fts5ExprNodeTest_STRING(pExpr, pNode);
224783 break;
224784 }
224785
224786 case FTS5_TERM4: {
224787 rc = fts5ExprNodeTest_TERM(pExpr, pNode);
224788 break;
224789 }
224790
224791 case FTS5_AND2: {
224792 rc = fts5ExprNodeTest_AND(pExpr, pNode);
224793 break;
224794 }
224795
224796 case FTS5_OR1: {
224797 fts5ExprNodeTest_OR(pExpr, pNode);
224798 break;
224799 }
224800
224801 default: assert( pNode->eType==FTS5_NOT )((void) (0)); {
224802 rc = fts5ExprNodeTest_NOT(pExpr, pNode);
224803 break;
224804 }
224805 }
224806 }
224807 return rc;
224808}
224809
224810
224811/*
224812** Set node pNode, which is part of expression pExpr, to point to the first
224813** match. If there are no matches, set the Node.bEof flag to indicate EOF.
224814**
224815** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
224816** It is not an error if there are no matches.
224817*/
224818static int fts5ExprNodeFirst(Fts5Expr *pExpr, Fts5ExprNode *pNode){
224819 int rc = SQLITE_OK0;
224820 pNode->bEof = 0;
224821 pNode->bNomatch = 0;
224822
224823 if( Fts5NodeIsString(pNode)((pNode)->eType==4 || (pNode)->eType==9) ){
224824 /* Initialize all term iterators in the NEAR object. */
224825 rc = fts5ExprNearInitAll(pExpr, pNode);
224826 }else if( pNode->xNext==0 ){
224827 pNode->bEof = 1;
224828 }else{
224829 int i;
224830 int nEof = 0;
224831 for(i=0; i<pNode->nChild && rc==SQLITE_OK0; i++){
224832 Fts5ExprNode *pChild = pNode->apChild[i];
224833 rc = fts5ExprNodeFirst(pExpr, pNode->apChild[i]);
224834 assert( pChild->bEof==0 || pChild->bEof==1 )((void) (0));
224835 nEof += pChild->bEof;
224836 }
224837 pNode->iRowid = pNode->apChild[0]->iRowid;
224838
224839 switch( pNode->eType ){
224840 case FTS5_AND2:
224841 if( nEof>0 ) fts5ExprSetEof(pNode);
224842 break;
224843
224844 case FTS5_OR1:
224845 if( pNode->nChild==nEof ) fts5ExprSetEof(pNode);
224846 break;
224847
224848 default:
224849 assert( pNode->eType==FTS5_NOT )((void) (0));
224850 pNode->bEof = pNode->apChild[0]->bEof;
224851 break;
224852 }
224853 }
224854
224855 if( rc==SQLITE_OK0 ){
224856 rc = fts5ExprNodeTest(pExpr, pNode);
224857 }
224858 return rc;
224859}
224860
224861
224862/*
224863** Begin iterating through the set of documents in index pIdx matched by
224864** the MATCH expression passed as the first argument. If the "bDesc"
224865** parameter is passed a non-zero value, iteration is in descending rowid
224866** order. Or, if it is zero, in ascending order.
224867**
224868** If iterating in ascending rowid order (bDesc==0), the first document
224869** visited is that with the smallest rowid that is larger than or equal
224870** to parameter iFirst. Or, if iterating in ascending order (bDesc==1),
224871** then the first document visited must have a rowid smaller than or
224872** equal to iFirst.
224873**
224874** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
224875** is not considered an error if the query does not match any documents.
224876*/
224877static int sqlite3Fts5ExprFirst(Fts5Expr *p, Fts5Index *pIdx, i64 iFirst, int bDesc){
224878 Fts5ExprNode *pRoot = p->pRoot;
224879 int rc; /* Return code */
224880
224881 p->pIndex = pIdx;
224882 p->bDesc = bDesc;
224883 rc = fts5ExprNodeFirst(p, pRoot);
224884
224885 /* If not at EOF but the current rowid occurs earlier than iFirst in
224886 ** the iteration order, move to document iFirst or later. */
224887 if( rc==SQLITE_OK0
224888 && 0==pRoot->bEof
224889 && fts5RowidCmp(p, pRoot->iRowid, iFirst)<0
224890 ){
224891 rc = fts5ExprNodeNext(p, pRoot, 1, iFirst)(pRoot)->xNext((p), (pRoot), (1), (iFirst));
224892 }
224893
224894 /* If the iterator is not at a real match, skip forward until it is. */
224895 while( pRoot->bNomatch && rc==SQLITE_OK0 ){
224896 assert( pRoot->bEof==0 )((void) (0));
224897 rc = fts5ExprNodeNext(p, pRoot, 0, 0)(pRoot)->xNext((p), (pRoot), (0), (0));
224898 }
224899 return rc;
224900}
224901
224902/*
224903** Move to the next document
224904**
224905** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
224906** is not considered an error if the query does not match any documents.
224907*/
224908static int sqlite3Fts5ExprNext(Fts5Expr *p, i64 iLast){
224909 int rc;
224910 Fts5ExprNode *pRoot = p->pRoot;
224911 assert( pRoot->bEof==0 && pRoot->bNomatch==0 )((void) (0));
224912 do {
224913 rc = fts5ExprNodeNext(p, pRoot, 0, 0)(pRoot)->xNext((p), (pRoot), (0), (0));
224914 assert( pRoot->bNomatch==0 || (rc==SQLITE_OK && pRoot->bEof==0) )((void) (0));
224915 }while( pRoot->bNomatch );
224916 if( fts5RowidCmp(p, pRoot->iRowid, iLast)>0 ){
224917 pRoot->bEof = 1;
224918 }
224919 return rc;
224920}
224921
224922static int sqlite3Fts5ExprEof(Fts5Expr *p){
224923 return p->pRoot->bEof;
224924}
224925
224926static i64 sqlite3Fts5ExprRowid(Fts5Expr *p){
224927 return p->pRoot->iRowid;
224928}
224929
224930static int fts5ParseStringFromToken(Fts5Token *pToken, char **pz){
224931 int rc = SQLITE_OK0;
224932 *pz = sqlite3Fts5Strndup(&rc, pToken->p, pToken->n);
224933 return rc;
224934}
224935
224936/*
224937** Free the phrase object passed as the only argument.
224938*/
224939static void fts5ExprPhraseFree(Fts5ExprPhrase *pPhrase){
224940 if( pPhrase ){
224941 int i;
224942 for(i=0; i<pPhrase->nTerm; i++){
224943 Fts5ExprTerm *pSyn;
224944 Fts5ExprTerm *pNext;
224945 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
224946 sqlite3_free(pTerm->zTerm);
224947 sqlite3Fts5IterClose(pTerm->pIter);
224948 for(pSyn=pTerm->pSynonym; pSyn; pSyn=pNext){
224949 pNext = pSyn->pSynonym;
224950 sqlite3Fts5IterClose(pSyn->pIter);
224951 fts5BufferFree((Fts5Buffer*)&pSyn[1])sqlite3Fts5BufferFree((Fts5Buffer*)&pSyn[1]);
224952 sqlite3_free(pSyn);
224953 }
224954 }
224955 if( pPhrase->poslist.nSpace>0 ) fts5BufferFree(&pPhrase->poslist)sqlite3Fts5BufferFree(&pPhrase->poslist);
224956 sqlite3_free(pPhrase);
224957 }
224958}
224959
224960/*
224961** Set the "bFirst" flag on the first token of the phrase passed as the
224962** only argument.
224963*/
224964static void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase *pPhrase){
224965 if( pPhrase && pPhrase->nTerm ){
224966 pPhrase->aTerm[0].bFirst = 1;
224967 }
224968}
224969
224970/*
224971** If argument pNear is NULL, then a new Fts5ExprNearset object is allocated
224972** and populated with pPhrase. Or, if pNear is not NULL, phrase pPhrase is
224973** appended to it and the results returned.
224974**
224975** If an OOM error occurs, both the pNear and pPhrase objects are freed and
224976** NULL returned.
224977*/
224978static Fts5ExprNearset *sqlite3Fts5ParseNearset(
224979 Fts5Parse *pParse, /* Parse context */
224980 Fts5ExprNearset *pNear, /* Existing nearset, or NULL */
224981 Fts5ExprPhrase *pPhrase /* Recently parsed phrase */
224982){
224983 const int SZALLOC = 8;
224984 Fts5ExprNearset *pRet = 0;
224985
224986 if( pParse->rc==SQLITE_OK0 ){
224987 if( pPhrase==0 ){
224988 return pNear;
224989 }
224990 if( pNear==0 ){
224991 sqlite3_int64 nByte;
224992 nByte = sizeof(Fts5ExprNearset) + SZALLOC * sizeof(Fts5ExprPhrase*);
224993 pRet = sqlite3_malloc64(nByte);
224994 if( pRet==0 ){
224995 pParse->rc = SQLITE_NOMEM7;
224996 }else{
224997 memset(pRet, 0, (size_t)nByte);
224998 }
224999 }else if( (pNear->nPhrase % SZALLOC)==0 ){
225000 int nNew = pNear->nPhrase + SZALLOC;
225001 sqlite3_int64 nByte;
225002
225003 nByte = sizeof(Fts5ExprNearset) + nNew * sizeof(Fts5ExprPhrase*);
225004 pRet = (Fts5ExprNearset*)sqlite3_realloc64(pNear, nByte);
225005 if( pRet==0 ){
225006 pParse->rc = SQLITE_NOMEM7;
225007 }
225008 }else{
225009 pRet = pNear;
225010 }
225011 }
225012
225013 if( pRet==0 ){
225014 assert( pParse->rc!=SQLITE_OK )((void) (0));
225015 sqlite3Fts5ParseNearsetFree(pNear);
225016 sqlite3Fts5ParsePhraseFree(pPhrase);
225017 }else{
225018 if( pRet->nPhrase>0 ){
225019 Fts5ExprPhrase *pLast = pRet->apPhrase[pRet->nPhrase-1];
225020 assert( pParse!=0 )((void) (0));
225021 assert( pParse->apPhrase!=0 )((void) (0));
225022 assert( pParse->nPhrase>=2 )((void) (0));
225023 assert( pLast==pParse->apPhrase[pParse->nPhrase-2] )((void) (0));
225024 if( pPhrase->nTerm==0 ){
225025 fts5ExprPhraseFree(pPhrase);
225026 pRet->nPhrase--;
225027 pParse->nPhrase--;
225028 pPhrase = pLast;
225029 }else if( pLast->nTerm==0 ){
225030 fts5ExprPhraseFree(pLast);
225031 pParse->apPhrase[pParse->nPhrase-2] = pPhrase;
225032 pParse->nPhrase--;
225033 pRet->nPhrase--;
225034 }
225035 }
225036 pRet->apPhrase[pRet->nPhrase++] = pPhrase;
225037 }
225038 return pRet;
225039}
225040
225041typedef struct TokenCtx TokenCtx;
225042struct TokenCtx {
225043 Fts5ExprPhrase *pPhrase;
225044 int rc;
225045};
225046
225047/*
225048** Callback for tokenizing terms used by ParseTerm().
225049*/
225050static int fts5ParseTokenize(
225051 void *pContext, /* Pointer to Fts5InsertCtx object */
225052 int tflags, /* Mask of FTS5_TOKEN_* flags */
225053 const char *pToken, /* Buffer containing token */
225054 int nToken, /* Size of token in bytes */
225055 int iUnused1, /* Start offset of token */
225056 int iUnused2 /* End offset of token */
225057){
225058 int rc = SQLITE_OK0;
225059 const int SZALLOC = 8;
225060 TokenCtx *pCtx = (TokenCtx*)pContext;
225061 Fts5ExprPhrase *pPhrase = pCtx->pPhrase;
225062
225063 UNUSED_PARAM2(iUnused1, iUnused2)(void)(iUnused1), (void)(iUnused2);
225064
225065 /* If an error has already occurred, this is a no-op */
225066 if( pCtx->rc!=SQLITE_OK0 ) return pCtx->rc;
225067 if( nToken>FTS5_MAX_TOKEN_SIZE32768 ) nToken = FTS5_MAX_TOKEN_SIZE32768;
225068
225069 if( pPhrase && pPhrase->nTerm>0 && (tflags & FTS5_TOKEN_COLOCATED0x0001) ){
225070 Fts5ExprTerm *pSyn;
225071 sqlite3_int64 nByte = sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer) + nToken+1;
225072 pSyn = (Fts5ExprTerm*)sqlite3_malloc64(nByte);
225073 if( pSyn==0 ){
225074 rc = SQLITE_NOMEM7;
225075 }else{
225076 memset(pSyn, 0, (size_t)nByte);
225077 pSyn->zTerm = ((char*)pSyn) + sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer);
225078 memcpy(pSyn->zTerm, pToken, nToken);
225079 pSyn->pSynonym = pPhrase->aTerm[pPhrase->nTerm-1].pSynonym;
225080 pPhrase->aTerm[pPhrase->nTerm-1].pSynonym = pSyn;
225081 }
225082 }else{
225083 Fts5ExprTerm *pTerm;
225084 if( pPhrase==0 || (pPhrase->nTerm % SZALLOC)==0 ){
225085 Fts5ExprPhrase *pNew;
225086 int nNew = SZALLOC + (pPhrase ? pPhrase->nTerm : 0);
225087
225088 pNew = (Fts5ExprPhrase*)sqlite3_realloc64(pPhrase,
225089 sizeof(Fts5ExprPhrase) + sizeof(Fts5ExprTerm) * nNew
225090 );
225091 if( pNew==0 ){
225092 rc = SQLITE_NOMEM7;
225093 }else{
225094 if( pPhrase==0 ) memset(pNew, 0, sizeof(Fts5ExprPhrase));
225095 pCtx->pPhrase = pPhrase = pNew;
225096 pNew->nTerm = nNew - SZALLOC;
225097 }
225098 }
225099
225100 if( rc==SQLITE_OK0 ){
225101 pTerm = &pPhrase->aTerm[pPhrase->nTerm++];
225102 memset(pTerm, 0, sizeof(Fts5ExprTerm));
225103 pTerm->zTerm = sqlite3Fts5Strndup(&rc, pToken, nToken);
225104 }
225105 }
225106
225107 pCtx->rc = rc;
225108 return rc;
225109}
225110
225111
225112/*
225113** Free the phrase object passed as the only argument.
225114*/
225115static void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase *pPhrase){
225116 fts5ExprPhraseFree(pPhrase);
225117}
225118
225119/*
225120** Free the phrase object passed as the second argument.
225121*/
225122static void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset *pNear){
225123 if( pNear ){
225124 int i;
225125 for(i=0; i<pNear->nPhrase; i++){
225126 fts5ExprPhraseFree(pNear->apPhrase[i]);
225127 }
225128 sqlite3_free(pNear->pColset);
225129 sqlite3_free(pNear);
225130 }
225131}
225132
225133static void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p){
225134 assert( pParse->pExpr==0 )((void) (0));
225135 pParse->pExpr = p;
225136}
225137
225138static int parseGrowPhraseArray(Fts5Parse *pParse){
225139 if( (pParse->nPhrase % 8)==0 ){
225140 sqlite3_int64 nByte = sizeof(Fts5ExprPhrase*) * (pParse->nPhrase + 8);
225141 Fts5ExprPhrase **apNew;
225142 apNew = (Fts5ExprPhrase**)sqlite3_realloc64(pParse->apPhrase, nByte);
225143 if( apNew==0 ){
225144 pParse->rc = SQLITE_NOMEM7;
225145 return SQLITE_NOMEM7;
225146 }
225147 pParse->apPhrase = apNew;
225148 }
225149 return SQLITE_OK0;
225150}
225151
225152/*
225153** This function is called by the parser to process a string token. The
225154** string may or may not be quoted. In any case it is tokenized and a
225155** phrase object consisting of all tokens returned.
225156*/
225157static Fts5ExprPhrase *sqlite3Fts5ParseTerm(
225158 Fts5Parse *pParse, /* Parse context */
225159 Fts5ExprPhrase *pAppend, /* Phrase to append to */
225160 Fts5Token *pToken, /* String to tokenize */
225161 int bPrefix /* True if there is a trailing "*" */
225162){
225163 Fts5Config *pConfig = pParse->pConfig;
225164 TokenCtx sCtx; /* Context object passed to callback */
225165 int rc; /* Tokenize return code */
225166 char *z = 0;
225167
225168 memset(&sCtx, 0, sizeof(TokenCtx));
225169 sCtx.pPhrase = pAppend;
225170
225171 rc = fts5ParseStringFromToken(pToken, &z);
225172 if( rc==SQLITE_OK0 ){
225173 int flags = FTS5_TOKENIZE_QUERY0x0001 | (bPrefix ? FTS5_TOKENIZE_PREFIX0x0002 : 0);
225174 int n;
225175 sqlite3Fts5Dequote(z);
225176 n = (int)strlen(z);
225177 rc = sqlite3Fts5Tokenize(pConfig, flags, z, n, &sCtx, fts5ParseTokenize);
225178 }
225179 sqlite3_free(z);
225180 if( rc || (rc = sCtx.rc) ){
225181 pParse->rc = rc;
225182 fts5ExprPhraseFree(sCtx.pPhrase);
225183 sCtx.pPhrase = 0;
225184 }else{
225185
225186 if( pAppend==0 ){
225187 if( parseGrowPhraseArray(pParse) ){
225188 fts5ExprPhraseFree(sCtx.pPhrase);
225189 return 0;
225190 }
225191 pParse->nPhrase++;
225192 }
225193
225194 if( sCtx.pPhrase==0 ){
225195 /* This happens when parsing a token or quoted phrase that contains
225196 ** no token characters at all. (e.g ... MATCH '""'). */
225197 sCtx.pPhrase = sqlite3Fts5MallocZero(&pParse->rc, sizeof(Fts5ExprPhrase));
225198 }else if( sCtx.pPhrase->nTerm ){
225199 sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = (u8)bPrefix;
225200 }
225201 pParse->apPhrase[pParse->nPhrase-1] = sCtx.pPhrase;
225202 }
225203
225204 return sCtx.pPhrase;
225205}
225206
225207/*
225208** Create a new FTS5 expression by cloning phrase iPhrase of the
225209** expression passed as the second argument.
225210*/
225211static int sqlite3Fts5ExprClonePhrase(
225212 Fts5Expr *pExpr,
225213 int iPhrase,
225214 Fts5Expr **ppNew
225215){
225216 int rc = SQLITE_OK0; /* Return code */
225217 Fts5ExprPhrase *pOrig; /* The phrase extracted from pExpr */
225218 Fts5Expr *pNew = 0; /* Expression to return via *ppNew */
225219 TokenCtx sCtx = {0,0}; /* Context object for fts5ParseTokenize */
225220
225221 pOrig = pExpr->apExprPhrase[iPhrase];
225222 pNew = (Fts5Expr*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Expr));
225223 if( rc==SQLITE_OK0 ){
225224 pNew->apExprPhrase = (Fts5ExprPhrase**)sqlite3Fts5MallocZero(&rc,
225225 sizeof(Fts5ExprPhrase*));
225226 }
225227 if( rc==SQLITE_OK0 ){
225228 pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&rc,
225229 sizeof(Fts5ExprNode));
225230 }
225231 if( rc==SQLITE_OK0 ){
225232 pNew->pRoot->pNear = (Fts5ExprNearset*)sqlite3Fts5MallocZero(&rc,
225233 sizeof(Fts5ExprNearset) + sizeof(Fts5ExprPhrase*));
225234 }
225235 if( rc==SQLITE_OK0 ){
225236 Fts5Colset *pColsetOrig = pOrig->pNode->pNear->pColset;
225237 if( pColsetOrig ){
225238 sqlite3_int64 nByte;
225239 Fts5Colset *pColset;
225240 nByte = sizeof(Fts5Colset) + (pColsetOrig->nCol-1) * sizeof(int);
225241 pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&rc, nByte);
225242 if( pColset ){
225243 memcpy(pColset, pColsetOrig, (size_t)nByte);
225244 }
225245 pNew->pRoot->pNear->pColset = pColset;
225246 }
225247 }
225248
225249 if( pOrig->nTerm ){
225250 int i; /* Used to iterate through phrase terms */
225251 for(i=0; rc==SQLITE_OK0 && i<pOrig->nTerm; i++){
225252 int tflags = 0;
225253 Fts5ExprTerm *p;
225254 for(p=&pOrig->aTerm[i]; p && rc==SQLITE_OK0; p=p->pSynonym){
225255 const char *zTerm = p->zTerm;
225256 rc = fts5ParseTokenize((void*)&sCtx, tflags, zTerm, (int)strlen(zTerm),
225257 0, 0);
225258 tflags = FTS5_TOKEN_COLOCATED0x0001;
225259 }
225260 if( rc==SQLITE_OK0 ){
225261 sCtx.pPhrase->aTerm[i].bPrefix = pOrig->aTerm[i].bPrefix;
225262 sCtx.pPhrase->aTerm[i].bFirst = pOrig->aTerm[i].bFirst;
225263 }
225264 }
225265 }else{
225266 /* This happens when parsing a token or quoted phrase that contains
225267 ** no token characters at all. (e.g ... MATCH '""'). */
225268 sCtx.pPhrase = sqlite3Fts5MallocZero(&rc, sizeof(Fts5ExprPhrase));
225269 }
225270
225271 if( rc==SQLITE_OK0 && ALWAYS(sCtx.pPhrase)(sCtx.pPhrase) ){
225272 /* All the allocations succeeded. Put the expression object together. */
225273 pNew->pIndex = pExpr->pIndex;
225274 pNew->pConfig = pExpr->pConfig;
225275 pNew->nPhrase = 1;
225276 pNew->apExprPhrase[0] = sCtx.pPhrase;
225277 pNew->pRoot->pNear->apPhrase[0] = sCtx.pPhrase;
225278 pNew->pRoot->pNear->nPhrase = 1;
225279 sCtx.pPhrase->pNode = pNew->pRoot;
225280
225281 if( pOrig->nTerm==1
225282 && pOrig->aTerm[0].pSynonym==0
225283 && pOrig->aTerm[0].bFirst==0
225284 ){
225285 pNew->pRoot->eType = FTS5_TERM4;
225286 pNew->pRoot->xNext = fts5ExprNodeNext_TERM;
225287 }else{
225288 pNew->pRoot->eType = FTS5_STRING9;
225289 pNew->pRoot->xNext = fts5ExprNodeNext_STRING;
225290 }
225291 }else{
225292 sqlite3Fts5ExprFree(pNew);
225293 fts5ExprPhraseFree(sCtx.pPhrase);
225294 pNew = 0;
225295 }
225296
225297 *ppNew = pNew;
225298 return rc;
225299}
225300
225301
225302/*
225303** Token pTok has appeared in a MATCH expression where the NEAR operator
225304** is expected. If token pTok does not contain "NEAR", store an error
225305** in the pParse object.
225306*/
225307static void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token *pTok){
225308 if( pTok->n!=4 || memcmp("NEAR", pTok->p, 4) ){
225309 sqlite3Fts5ParseError(
225310 pParse, "fts5: syntax error near \"%.*s\"", pTok->n, pTok->p
225311 );
225312 }
225313}
225314
225315static void sqlite3Fts5ParseSetDistance(
225316 Fts5Parse *pParse,
225317 Fts5ExprNearset *pNear,
225318 Fts5Token *p
225319){
225320 if( pNear ){
225321 int nNear = 0;
225322 int i;
225323 if( p->n ){
225324 for(i=0; i<p->n; i++){
225325 char c = (char)p->p[i];
225326 if( c<'0' || c>'9' ){
225327 sqlite3Fts5ParseError(
225328 pParse, "expected integer, got \"%.*s\"", p->n, p->p
225329 );
225330 return;
225331 }
225332 nNear = nNear * 10 + (p->p[i] - '0');
225333 }
225334 }else{
225335 nNear = FTS5_DEFAULT_NEARDIST10;
225336 }
225337 pNear->nNear = nNear;
225338 }
225339}
225340
225341/*
225342** The second argument passed to this function may be NULL, or it may be
225343** an existing Fts5Colset object. This function returns a pointer to
225344** a new colset object containing the contents of (p) with new value column
225345** number iCol appended.
225346**
225347** If an OOM error occurs, store an error code in pParse and return NULL.
225348** The old colset object (if any) is not freed in this case.
225349*/
225350static Fts5Colset *fts5ParseColset(
225351 Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */
225352 Fts5Colset *p, /* Existing colset object */
225353 int iCol /* New column to add to colset object */
225354){
225355 int nCol = p ? p->nCol : 0; /* Num. columns already in colset object */
225356 Fts5Colset *pNew; /* New colset object to return */
225357
225358 assert( pParse->rc==SQLITE_OK )((void) (0));
225359 assert( iCol>=0 && iCol<pParse->pConfig->nCol )((void) (0));
225360
225361 pNew = sqlite3_realloc64(p, sizeof(Fts5Colset) + sizeof(int)*nCol);
225362 if( pNew==0 ){
225363 pParse->rc = SQLITE_NOMEM7;
225364 }else{
225365 int *aiCol = pNew->aiCol;
225366 int i, j;
225367 for(i=0; i<nCol; i++){
225368 if( aiCol[i]==iCol ) return pNew;
225369 if( aiCol[i]>iCol ) break;
225370 }
225371 for(j=nCol; j>i; j--){
225372 aiCol[j] = aiCol[j-1];
225373 }
225374 aiCol[i] = iCol;
225375 pNew->nCol = nCol+1;
225376
225377#ifndef NDEBUG1
225378 /* Check that the array is in order and contains no duplicate entries. */
225379 for(i=1; i<pNew->nCol; i++) assert( pNew->aiCol[i]>pNew->aiCol[i-1] )((void) (0));
225380#endif
225381 }
225382
225383 return pNew;
225384}
225385
225386/*
225387** Allocate and return an Fts5Colset object specifying the inverse of
225388** the colset passed as the second argument. Free the colset passed
225389** as the second argument before returning.
225390*/
225391static Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse *pParse, Fts5Colset *p){
225392 Fts5Colset *pRet;
225393 int nCol = pParse->pConfig->nCol;
225394
225395 pRet = (Fts5Colset*)sqlite3Fts5MallocZero(&pParse->rc,
225396 sizeof(Fts5Colset) + sizeof(int)*nCol
225397 );
225398 if( pRet ){
225399 int i;
225400 int iOld = 0;
225401 for(i=0; i<nCol; i++){
225402 if( iOld>=p->nCol || p->aiCol[iOld]!=i ){
225403 pRet->aiCol[pRet->nCol++] = i;
225404 }else{
225405 iOld++;
225406 }
225407 }
225408 }
225409
225410 sqlite3_free(p);
225411 return pRet;
225412}
225413
225414static Fts5Colset *sqlite3Fts5ParseColset(
225415 Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */
225416 Fts5Colset *pColset, /* Existing colset object */
225417 Fts5Token *p
225418){
225419 Fts5Colset *pRet = 0;
225420 int iCol;
225421 char *z; /* Dequoted copy of token p */
225422
225423 z = sqlite3Fts5Strndup(&pParse->rc, p->p, p->n);
225424 if( pParse->rc==SQLITE_OK0 ){
225425 Fts5Config *pConfig = pParse->pConfig;
225426 sqlite3Fts5Dequote(z);
225427 for(iCol=0; iCol<pConfig->nCol; iCol++){
225428 if( 0==sqlite3_stricmp(pConfig->azCol[iCol], z) ) break;
225429 }
225430 if( iCol==pConfig->nCol ){
225431 sqlite3Fts5ParseError(pParse, "no such column: %s", z);
225432 }else{
225433 pRet = fts5ParseColset(pParse, pColset, iCol);
225434 }
225435 sqlite3_free(z);
225436 }
225437
225438 if( pRet==0 ){
225439 assert( pParse->rc!=SQLITE_OK )((void) (0));
225440 sqlite3_free(pColset);
225441 }
225442
225443 return pRet;
225444}
225445
225446/*
225447** If argument pOrig is NULL, or if (*pRc) is set to anything other than
225448** SQLITE_OK when this function is called, NULL is returned.
225449**
225450** Otherwise, a copy of (*pOrig) is made into memory obtained from
225451** sqlite3Fts5MallocZero() and a pointer to it returned. If the allocation
225452** fails, (*pRc) is set to SQLITE_NOMEM and NULL is returned.
225453*/
225454static Fts5Colset *fts5CloneColset(int *pRc, Fts5Colset *pOrig){
225455 Fts5Colset *pRet;
225456 if( pOrig ){
225457 sqlite3_int64 nByte = sizeof(Fts5Colset) + (pOrig->nCol-1) * sizeof(int);
225458 pRet = (Fts5Colset*)sqlite3Fts5MallocZero(pRc, nByte);
225459 if( pRet ){
225460 memcpy(pRet, pOrig, (size_t)nByte);
225461 }
225462 }else{
225463 pRet = 0;
225464 }
225465 return pRet;
225466}
225467
225468/*
225469** Remove from colset pColset any columns that are not also in colset pMerge.
225470*/
225471static void fts5MergeColset(Fts5Colset *pColset, Fts5Colset *pMerge){
225472 int iIn = 0; /* Next input in pColset */
225473 int iMerge = 0; /* Next input in pMerge */
225474 int iOut = 0; /* Next output slot in pColset */
225475
225476 while( iIn<pColset->nCol && iMerge<pMerge->nCol ){
225477 int iDiff = pColset->aiCol[iIn] - pMerge->aiCol[iMerge];
225478 if( iDiff==0 ){
225479 pColset->aiCol[iOut++] = pMerge->aiCol[iMerge];
225480 iMerge++;
225481 iIn++;
225482 }else if( iDiff>0 ){
225483 iMerge++;
225484 }else{
225485 iIn++;
225486 }
225487 }
225488 pColset->nCol = iOut;
225489}
225490
225491/*
225492** Recursively apply colset pColset to expression node pNode and all of
225493** its decendents. If (*ppFree) is not NULL, it contains a spare copy
225494** of pColset. This function may use the spare copy and set (*ppFree) to
225495** zero, or it may create copies of pColset using fts5CloneColset().
225496*/
225497static void fts5ParseSetColset(
225498 Fts5Parse *pParse,
225499 Fts5ExprNode *pNode,
225500 Fts5Colset *pColset,
225501 Fts5Colset **ppFree
225502){
225503 if( pParse->rc==SQLITE_OK0 ){
225504 assert( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING((void) (0))
225505 || pNode->eType==FTS5_AND || pNode->eType==FTS5_OR((void) (0))
225506 || pNode->eType==FTS5_NOT || pNode->eType==FTS5_EOF((void) (0))
225507 )((void) (0));
225508 if( pNode->eType==FTS5_STRING9 || pNode->eType==FTS5_TERM4 ){
225509 Fts5ExprNearset *pNear = pNode->pNear;
225510 if( pNear->pColset ){
225511 fts5MergeColset(pNear->pColset, pColset);
225512 if( pNear->pColset->nCol==0 ){
225513 pNode->eType = FTS5_EOF0;
225514 pNode->xNext = 0;
225515 }
225516 }else if( *ppFree ){
225517 pNear->pColset = pColset;
225518 *ppFree = 0;
225519 }else{
225520 pNear->pColset = fts5CloneColset(&pParse->rc, pColset);
225521 }
225522 }else{
225523 int i;
225524 assert( pNode->eType!=FTS5_EOF || pNode->nChild==0 )((void) (0));
225525 for(i=0; i<pNode->nChild; i++){
225526 fts5ParseSetColset(pParse, pNode->apChild[i], pColset, ppFree);
225527 }
225528 }
225529 }
225530}
225531
225532/*
225533** Apply colset pColset to expression node pExpr and all of its descendents.
225534*/
225535static void sqlite3Fts5ParseSetColset(
225536 Fts5Parse *pParse,
225537 Fts5ExprNode *pExpr,
225538 Fts5Colset *pColset
225539){
225540 Fts5Colset *pFree = pColset;
225541 if( pParse->pConfig->eDetail==FTS5_DETAIL_NONE1 ){
225542 sqlite3Fts5ParseError(pParse,
225543 "fts5: column queries are not supported (detail=none)"
225544 );
225545 }else{
225546 fts5ParseSetColset(pParse, pExpr, pColset, &pFree);
225547 }
225548 sqlite3_free(pFree);
225549}
225550
225551static void fts5ExprAssignXNext(Fts5ExprNode *pNode){
225552 switch( pNode->eType ){
225553 case FTS5_STRING9: {
225554 Fts5ExprNearset *pNear = pNode->pNear;
225555 if( pNear->nPhrase==1 && pNear->apPhrase[0]->nTerm==1
225556 && pNear->apPhrase[0]->aTerm[0].pSynonym==0
225557 && pNear->apPhrase[0]->aTerm[0].bFirst==0
225558 ){
225559 pNode->eType = FTS5_TERM4;
225560 pNode->xNext = fts5ExprNodeNext_TERM;
225561 }else{
225562 pNode->xNext = fts5ExprNodeNext_STRING;
225563 }
225564 break;
225565 };
225566
225567 case FTS5_OR1: {
225568 pNode->xNext = fts5ExprNodeNext_OR;
225569 break;
225570 };
225571
225572 case FTS5_AND2: {
225573 pNode->xNext = fts5ExprNodeNext_AND;
225574 break;
225575 };
225576
225577 default: assert( pNode->eType==FTS5_NOT )((void) (0)); {
225578 pNode->xNext = fts5ExprNodeNext_NOT;
225579 break;
225580 };
225581 }
225582}
225583
225584static void fts5ExprAddChildren(Fts5ExprNode *p, Fts5ExprNode *pSub){
225585 if( p->eType!=FTS5_NOT3 && pSub->eType==p->eType ){
225586 int nByte = sizeof(Fts5ExprNode*) * pSub->nChild;
225587 memcpy(&p->apChild[p->nChild], pSub->apChild, nByte);
225588 p->nChild += pSub->nChild;
225589 sqlite3_free(pSub);
225590 }else{
225591 p->apChild[p->nChild++] = pSub;
225592 }
225593}
225594
225595/*
225596** This function is used when parsing LIKE or GLOB patterns against
225597** trigram indexes that specify either detail=column or detail=none.
225598** It converts a phrase:
225599**
225600** abc + def + ghi
225601**
225602** into an AND tree:
225603**
225604** abc AND def AND ghi
225605*/
225606static Fts5ExprNode *fts5ParsePhraseToAnd(
225607 Fts5Parse *pParse,
225608 Fts5ExprNearset *pNear
225609){
225610 int nTerm = pNear->apPhrase[0]->nTerm;
225611 int ii;
225612 int nByte;
225613 Fts5ExprNode *pRet;
225614
225615 assert( pNear->nPhrase==1 )((void) (0));
225616 assert( pParse->bPhraseToAnd )((void) (0));
225617
225618 nByte = sizeof(Fts5ExprNode) + nTerm*sizeof(Fts5ExprNode*);
225619 pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte);
225620 if( pRet ){
225621 pRet->eType = FTS5_AND2;
225622 pRet->nChild = nTerm;
225623 fts5ExprAssignXNext(pRet);
225624 pParse->nPhrase--;
225625 for(ii=0; ii<nTerm; ii++){
225626 Fts5ExprPhrase *pPhrase = (Fts5ExprPhrase*)sqlite3Fts5MallocZero(
225627 &pParse->rc, sizeof(Fts5ExprPhrase)
225628 );
225629 if( pPhrase ){
225630 if( parseGrowPhraseArray(pParse) ){
225631 fts5ExprPhraseFree(pPhrase);
225632 }else{
225633 pParse->apPhrase[pParse->nPhrase++] = pPhrase;
225634 pPhrase->nTerm = 1;
225635 pPhrase->aTerm[0].zTerm = sqlite3Fts5Strndup(
225636 &pParse->rc, pNear->apPhrase[0]->aTerm[ii].zTerm, -1
225637 );
225638 pRet->apChild[ii] = sqlite3Fts5ParseNode(pParse, FTS5_STRING9,
225639 0, 0, sqlite3Fts5ParseNearset(pParse, 0, pPhrase)
225640 );
225641 }
225642 }
225643 }
225644
225645 if( pParse->rc ){
225646 sqlite3Fts5ParseNodeFree(pRet);
225647 pRet = 0;
225648 }else{
225649 sqlite3Fts5ParseNearsetFree(pNear);
225650 }
225651 }
225652
225653 return pRet;
225654}
225655
225656/*
225657** Allocate and return a new expression object. If anything goes wrong (i.e.
225658** OOM error), leave an error code in pParse and return NULL.
225659*/
225660static Fts5ExprNode *sqlite3Fts5ParseNode(
225661 Fts5Parse *pParse, /* Parse context */
225662 int eType, /* FTS5_STRING, AND, OR or NOT */
225663 Fts5ExprNode *pLeft, /* Left hand child expression */
225664 Fts5ExprNode *pRight, /* Right hand child expression */
225665 Fts5ExprNearset *pNear /* For STRING expressions, the near cluster */
225666){
225667 Fts5ExprNode *pRet = 0;
225668
225669 if( pParse->rc==SQLITE_OK0 ){
225670 int nChild = 0; /* Number of children of returned node */
225671 sqlite3_int64 nByte; /* Bytes of space to allocate for this node */
225672
225673 assert( (eType!=FTS5_STRING && !pNear)((void) (0))
225674 || (eType==FTS5_STRING && !pLeft && !pRight)((void) (0))
225675 )((void) (0));
225676 if( eType==FTS5_STRING9 && pNear==0 ) return 0;
225677 if( eType!=FTS5_STRING9 && pLeft==0 ) return pRight;
225678 if( eType!=FTS5_STRING9 && pRight==0 ) return pLeft;
225679
225680 if( eType==FTS5_STRING9
225681 && pParse->bPhraseToAnd
225682 && pNear->apPhrase[0]->nTerm>1
225683 ){
225684 pRet = fts5ParsePhraseToAnd(pParse, pNear);
225685 }else{
225686 if( eType==FTS5_NOT3 ){
225687 nChild = 2;
225688 }else if( eType==FTS5_AND2 || eType==FTS5_OR1 ){
225689 nChild = 2;
225690 if( pLeft->eType==eType ) nChild += pLeft->nChild-1;
225691 if( pRight->eType==eType ) nChild += pRight->nChild-1;
225692 }
225693
225694 nByte = sizeof(Fts5ExprNode) + sizeof(Fts5ExprNode*)*(nChild-1);
225695 pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte);
225696
225697 if( pRet ){
225698 pRet->eType = eType;
225699 pRet->pNear = pNear;
225700 fts5ExprAssignXNext(pRet);
225701 if( eType==FTS5_STRING9 ){
225702 int iPhrase;
225703 for(iPhrase=0; iPhrase<pNear->nPhrase; iPhrase++){
225704 pNear->apPhrase[iPhrase]->pNode = pRet;
225705 if( pNear->apPhrase[iPhrase]->nTerm==0 ){
225706 pRet->xNext = 0;
225707 pRet->eType = FTS5_EOF0;
225708 }
225709 }
225710
225711 if( pParse->pConfig->eDetail!=FTS5_DETAIL_FULL0 ){
225712 Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
225713 if( pNear->nPhrase!=1
225714 || pPhrase->nTerm>1
225715 || (pPhrase->nTerm>0 && pPhrase->aTerm[0].bFirst)
225716 ){
225717 sqlite3Fts5ParseError(pParse,
225718 "fts5: %s queries are not supported (detail!=full)",
225719 pNear->nPhrase==1 ? "phrase": "NEAR"
225720 );
225721 sqlite3_free(pRet);
225722 pRet = 0;
225723 }
225724 }
225725 }else{
225726 fts5ExprAddChildren(pRet, pLeft);
225727 fts5ExprAddChildren(pRet, pRight);
225728 }
225729 }
225730 }
225731 }
225732
225733 if( pRet==0 ){
225734 assert( pParse->rc!=SQLITE_OK )((void) (0));
225735 sqlite3Fts5ParseNodeFree(pLeft);
225736 sqlite3Fts5ParseNodeFree(pRight);
225737 sqlite3Fts5ParseNearsetFree(pNear);
225738 }
225739 return pRet;
225740}
225741
225742static Fts5ExprNode *sqlite3Fts5ParseImplicitAnd(
225743 Fts5Parse *pParse, /* Parse context */
225744 Fts5ExprNode *pLeft, /* Left hand child expression */
225745 Fts5ExprNode *pRight /* Right hand child expression */
225746){
225747 Fts5ExprNode *pRet = 0;
225748 Fts5ExprNode *pPrev;
225749
225750 if( pParse->rc ){
225751 sqlite3Fts5ParseNodeFree(pLeft);
225752 sqlite3Fts5ParseNodeFree(pRight);
225753 }else{
225754
225755 assert( pLeft->eType==FTS5_STRING((void) (0))
225756 || pLeft->eType==FTS5_TERM((void) (0))
225757 || pLeft->eType==FTS5_EOF((void) (0))
225758 || pLeft->eType==FTS5_AND((void) (0))
225759 )((void) (0));
225760 assert( pRight->eType==FTS5_STRING((void) (0))
225761 || pRight->eType==FTS5_TERM((void) (0))
225762 || pRight->eType==FTS5_EOF((void) (0))
225763 )((void) (0));
225764
225765 if( pLeft->eType==FTS5_AND2 ){
225766 pPrev = pLeft->apChild[pLeft->nChild-1];
225767 }else{
225768 pPrev = pLeft;
225769 }
225770 assert( pPrev->eType==FTS5_STRING((void) (0))
225771 || pPrev->eType==FTS5_TERM((void) (0))
225772 || pPrev->eType==FTS5_EOF((void) (0))
225773 )((void) (0));
225774
225775 if( pRight->eType==FTS5_EOF0 ){
225776 assert( pParse->apPhrase[pParse->nPhrase-1]==pRight->pNear->apPhrase[0] )((void) (0));
225777 sqlite3Fts5ParseNodeFree(pRight);
225778 pRet = pLeft;
225779 pParse->nPhrase--;
225780 }
225781 else if( pPrev->eType==FTS5_EOF0 ){
225782 Fts5ExprPhrase **ap;
225783
225784 if( pPrev==pLeft ){
225785 pRet = pRight;
225786 }else{
225787 pLeft->apChild[pLeft->nChild-1] = pRight;
225788 pRet = pLeft;
225789 }
225790
225791 ap = &pParse->apPhrase[pParse->nPhrase-1-pRight->pNear->nPhrase];
225792 assert( ap[0]==pPrev->pNear->apPhrase[0] )((void) (0));
225793 memmove(ap, &ap[1], sizeof(Fts5ExprPhrase*)*pRight->pNear->nPhrase);
225794 pParse->nPhrase--;
225795
225796 sqlite3Fts5ParseNodeFree(pPrev);
225797 }
225798 else{
225799 pRet = sqlite3Fts5ParseNode(pParse, FTS5_AND2, pLeft, pRight, 0);
225800 }
225801 }
225802
225803 return pRet;
225804}
225805
225806#ifdef SQLITE_TEST
225807static char *fts5ExprTermPrint(Fts5ExprTerm *pTerm){
225808 sqlite3_int64 nByte = 0;
225809 Fts5ExprTerm *p;
225810 char *zQuoted;
225811
225812 /* Determine the maximum amount of space required. */
225813 for(p=pTerm; p; p=p->pSynonym){
225814 nByte += (int)strlen(pTerm->zTerm) * 2 + 3 + 2;
225815 }
225816 zQuoted = sqlite3_malloc64(nByte);
225817
225818 if( zQuoted ){
225819 int i = 0;
225820 for(p=pTerm; p; p=p->pSynonym){
225821 char *zIn = p->zTerm;
225822 zQuoted[i++] = '"';
225823 while( *zIn ){
225824 if( *zIn=='"' ) zQuoted[i++] = '"';
225825 zQuoted[i++] = *zIn++;
225826 }
225827 zQuoted[i++] = '"';
225828 if( p->pSynonym ) zQuoted[i++] = '|';
225829 }
225830 if( pTerm->bPrefix ){
225831 zQuoted[i++] = ' ';
225832 zQuoted[i++] = '*';
225833 }
225834 zQuoted[i++] = '\0';
225835 }
225836 return zQuoted;
225837}
225838
225839static char *fts5PrintfAppend(char *zApp, const char *zFmt, ...){
225840 char *zNew;
225841 va_list ap;
225842 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
225843 zNew = sqlite3_vmprintf(zFmt, ap);
225844 va_end(ap)__builtin_va_end(ap);
225845 if( zApp && zNew ){
225846 char *zNew2 = sqlite3_mprintf("%s%s", zApp, zNew);
225847 sqlite3_free(zNew);
225848 zNew = zNew2;
225849 }
225850 sqlite3_free(zApp);
225851 return zNew;
225852}
225853
225854/*
225855** Compose a tcl-readable representation of expression pExpr. Return a
225856** pointer to a buffer containing that representation. It is the
225857** responsibility of the caller to at some point free the buffer using
225858** sqlite3_free().
225859*/
225860static char *fts5ExprPrintTcl(
225861 Fts5Config *pConfig,
225862 const char *zNearsetCmd,
225863 Fts5ExprNode *pExpr
225864){
225865 char *zRet = 0;
225866 if( pExpr->eType==FTS5_STRING9 || pExpr->eType==FTS5_TERM4 ){
225867 Fts5ExprNearset *pNear = pExpr->pNear;
225868 int i;
225869 int iTerm;
225870
225871 zRet = fts5PrintfAppend(zRet, "%s ", zNearsetCmd);
225872 if( zRet==0 ) return 0;
225873 if( pNear->pColset ){
225874 int *aiCol = pNear->pColset->aiCol;
225875 int nCol = pNear->pColset->nCol;
225876 if( nCol==1 ){
225877 zRet = fts5PrintfAppend(zRet, "-col %d ", aiCol[0]);
225878 }else{
225879 zRet = fts5PrintfAppend(zRet, "-col {%d", aiCol[0]);
225880 for(i=1; i<pNear->pColset->nCol; i++){
225881 zRet = fts5PrintfAppend(zRet, " %d", aiCol[i]);
225882 }
225883 zRet = fts5PrintfAppend(zRet, "} ");
225884 }
225885 if( zRet==0 ) return 0;
225886 }
225887
225888 if( pNear->nPhrase>1 ){
225889 zRet = fts5PrintfAppend(zRet, "-near %d ", pNear->nNear);
225890 if( zRet==0 ) return 0;
225891 }
225892
225893 zRet = fts5PrintfAppend(zRet, "--");
225894 if( zRet==0 ) return 0;
225895
225896 for(i=0; i<pNear->nPhrase; i++){
225897 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
225898
225899 zRet = fts5PrintfAppend(zRet, " {");
225900 for(iTerm=0; zRet && iTerm<pPhrase->nTerm; iTerm++){
225901 char *zTerm = pPhrase->aTerm[iTerm].zTerm;
225902 zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" ", zTerm);
225903 if( pPhrase->aTerm[iTerm].bPrefix ){
225904 zRet = fts5PrintfAppend(zRet, "*");
225905 }
225906 }
225907
225908 if( zRet ) zRet = fts5PrintfAppend(zRet, "}");
225909 if( zRet==0 ) return 0;
225910 }
225911
225912 }else{
225913 char const *zOp = 0;
225914 int i;
225915 switch( pExpr->eType ){
225916 case FTS5_AND2: zOp = "AND"; break;
225917 case FTS5_NOT3: zOp = "NOT"; break;
225918 default:
225919 assert( pExpr->eType==FTS5_OR )((void) (0));
225920 zOp = "OR";
225921 break;
225922 }
225923
225924 zRet = sqlite3_mprintf("%s", zOp);
225925 for(i=0; zRet && i<pExpr->nChild; i++){
225926 char *z = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->apChild[i]);
225927 if( !z ){
225928 sqlite3_free(zRet);
225929 zRet = 0;
225930 }else{
225931 zRet = fts5PrintfAppend(zRet, " [%z]", z);
225932 }
225933 }
225934 }
225935
225936 return zRet;
225937}
225938
225939static char *fts5ExprPrint(Fts5Config *pConfig, Fts5ExprNode *pExpr){
225940 char *zRet = 0;
225941 if( pExpr->eType==0 ){
225942 return sqlite3_mprintf("\"\"");
225943 }else
225944 if( pExpr->eType==FTS5_STRING9 || pExpr->eType==FTS5_TERM4 ){
225945 Fts5ExprNearset *pNear = pExpr->pNear;
225946 int i;
225947 int iTerm;
225948
225949 if( pNear->pColset ){
225950 int ii;
225951 Fts5Colset *pColset = pNear->pColset;
225952 if( pColset->nCol>1 ) zRet = fts5PrintfAppend(zRet, "{");
225953 for(ii=0; ii<pColset->nCol; ii++){
225954 zRet = fts5PrintfAppend(zRet, "%s%s",
225955 pConfig->azCol[pColset->aiCol[ii]], ii==pColset->nCol-1 ? "" : " "
225956 );
225957 }
225958 if( zRet ){
225959 zRet = fts5PrintfAppend(zRet, "%s : ", pColset->nCol>1 ? "}" : "");
225960 }
225961 if( zRet==0 ) return 0;
225962 }
225963
225964 if( pNear->nPhrase>1 ){
225965 zRet = fts5PrintfAppend(zRet, "NEAR(");
225966 if( zRet==0 ) return 0;
225967 }
225968
225969 for(i=0; i<pNear->nPhrase; i++){
225970 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
225971 if( i!=0 ){
225972 zRet = fts5PrintfAppend(zRet, " ");
225973 if( zRet==0 ) return 0;
225974 }
225975 for(iTerm=0; iTerm<pPhrase->nTerm; iTerm++){
225976 char *zTerm = fts5ExprTermPrint(&pPhrase->aTerm[iTerm]);
225977 if( zTerm ){
225978 zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" + ", zTerm);
225979 sqlite3_free(zTerm);
225980 }
225981 if( zTerm==0 || zRet==0 ){
225982 sqlite3_free(zRet);
225983 return 0;
225984 }
225985 }
225986 }
225987
225988 if( pNear->nPhrase>1 ){
225989 zRet = fts5PrintfAppend(zRet, ", %d)", pNear->nNear);
225990 if( zRet==0 ) return 0;
225991 }
225992
225993 }else{
225994 char const *zOp = 0;
225995 int i;
225996
225997 switch( pExpr->eType ){
225998 case FTS5_AND2: zOp = " AND "; break;
225999 case FTS5_NOT3: zOp = " NOT "; break;
226000 default:
226001 assert( pExpr->eType==FTS5_OR )((void) (0));
226002 zOp = " OR ";
226003 break;
226004 }
226005
226006 for(i=0; i<pExpr->nChild; i++){
226007 char *z = fts5ExprPrint(pConfig, pExpr->apChild[i]);
226008 if( z==0 ){
226009 sqlite3_free(zRet);
226010 zRet = 0;
226011 }else{
226012 int e = pExpr->apChild[i]->eType;
226013 int b = (e!=FTS5_STRING9 && e!=FTS5_TERM4 && e!=FTS5_EOF0);
226014 zRet = fts5PrintfAppend(zRet, "%s%s%z%s",
226015 (i==0 ? "" : zOp),
226016 (b?"(":""), z, (b?")":"")
226017 );
226018 }
226019 if( zRet==0 ) break;
226020 }
226021 }
226022
226023 return zRet;
226024}
226025
226026/*
226027** The implementation of user-defined scalar functions fts5_expr() (bTcl==0)
226028** and fts5_expr_tcl() (bTcl!=0).
226029*/
226030static void fts5ExprFunction(
226031 sqlite3_context *pCtx, /* Function call context */
226032 int nArg, /* Number of args */
226033 sqlite3_value **apVal, /* Function arguments */
226034 int bTcl
226035){
226036 Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx);
226037 sqlite3 *db = sqlite3_context_db_handle(pCtx);
226038 const char *zExpr = 0;
226039 char *zErr = 0;
226040 Fts5Expr *pExpr = 0;
226041 int rc;
226042 int i;
226043
226044 const char **azConfig; /* Array of arguments for Fts5Config */
226045 const char *zNearsetCmd = "nearset";
226046 int nConfig; /* Size of azConfig[] */
226047 Fts5Config *pConfig = 0;
226048 int iArg = 1;
226049
226050 if( nArg<1 ){
226051 zErr = sqlite3_mprintf("wrong number of arguments to function %s",
226052 bTcl ? "fts5_expr_tcl" : "fts5_expr"
226053 );
226054 sqlite3_result_error(pCtx, zErr, -1);
226055 sqlite3_free(zErr);
226056 return;
226057 }
226058
226059 if( bTcl && nArg>1 ){
226060 zNearsetCmd = (const char*)sqlite3_value_text(apVal[1]);
226061 iArg = 2;
226062 }
226063
226064 nConfig = 3 + (nArg-iArg);
226065 azConfig = (const char**)sqlite3_malloc64(sizeof(char*) * nConfig);
226066 if( azConfig==0 ){
226067 sqlite3_result_error_nomem(pCtx);
226068 return;
226069 }
226070 azConfig[0] = 0;
226071 azConfig[1] = "main";
226072 azConfig[2] = "tbl";
226073 for(i=3; iArg<nArg; iArg++){
226074 const char *z = (const char*)sqlite3_value_text(apVal[iArg]);
226075 azConfig[i++] = (z ? z : "");
226076 }
226077
226078 zExpr = (const char*)sqlite3_value_text(apVal[0]);
226079 if( zExpr==0 ) zExpr = "";
226080
226081 rc = sqlite3Fts5ConfigParse(pGlobal, db, nConfig, azConfig, &pConfig, &zErr);
226082 if( rc==SQLITE_OK0 ){
226083 rc = sqlite3Fts5ExprNew(pConfig, 0, pConfig->nCol, zExpr, &pExpr, &zErr);
226084 }
226085 if( rc==SQLITE_OK0 ){
226086 char *zText;
226087 if( pExpr->pRoot->xNext==0 ){
226088 zText = sqlite3_mprintf("");
226089 }else if( bTcl ){
226090 zText = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->pRoot);
226091 }else{
226092 zText = fts5ExprPrint(pConfig, pExpr->pRoot);
226093 }
226094 if( zText==0 ){
226095 rc = SQLITE_NOMEM7;
226096 }else{
226097 sqlite3_result_text(pCtx, zText, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
226098 sqlite3_free(zText);
226099 }
226100 }
226101
226102 if( rc!=SQLITE_OK0 ){
226103 if( zErr ){
226104 sqlite3_result_error(pCtx, zErr, -1);
226105 sqlite3_free(zErr);
226106 }else{
226107 sqlite3_result_error_code(pCtx, rc);
226108 }
226109 }
226110 sqlite3_free((void *)azConfig);
226111 sqlite3Fts5ConfigFree(pConfig);
226112 sqlite3Fts5ExprFree(pExpr);
226113}
226114
226115static void fts5ExprFunctionHr(
226116 sqlite3_context *pCtx, /* Function call context */
226117 int nArg, /* Number of args */
226118 sqlite3_value **apVal /* Function arguments */
226119){
226120 fts5ExprFunction(pCtx, nArg, apVal, 0);
226121}
226122static void fts5ExprFunctionTcl(
226123 sqlite3_context *pCtx, /* Function call context */
226124 int nArg, /* Number of args */
226125 sqlite3_value **apVal /* Function arguments */
226126){
226127 fts5ExprFunction(pCtx, nArg, apVal, 1);
226128}
226129
226130/*
226131** The implementation of an SQLite user-defined-function that accepts a
226132** single integer as an argument. If the integer is an alpha-numeric
226133** unicode code point, 1 is returned. Otherwise 0.
226134*/
226135static void fts5ExprIsAlnum(
226136 sqlite3_context *pCtx, /* Function call context */
226137 int nArg, /* Number of args */
226138 sqlite3_value **apVal /* Function arguments */
226139){
226140 int iCode;
226141 u8 aArr[32];
226142 if( nArg!=1 ){
226143 sqlite3_result_error(pCtx,
226144 "wrong number of arguments to function fts5_isalnum", -1
226145 );
226146 return;
226147 }
226148 memset(aArr, 0, sizeof(aArr));
226149 sqlite3Fts5UnicodeCatParse("L*", aArr);
226150 sqlite3Fts5UnicodeCatParse("N*", aArr);
226151 sqlite3Fts5UnicodeCatParse("Co", aArr);
226152 iCode = sqlite3_value_int(apVal[0]);
226153 sqlite3_result_int(pCtx, aArr[sqlite3Fts5UnicodeCategory((u32)iCode)]);
226154}
226155
226156static void fts5ExprFold(
226157 sqlite3_context *pCtx, /* Function call context */
226158 int nArg, /* Number of args */
226159 sqlite3_value **apVal /* Function arguments */
226160){
226161 if( nArg!=1 && nArg!=2 ){
226162 sqlite3_result_error(pCtx,
226163 "wrong number of arguments to function fts5_fold", -1
226164 );
226165 }else{
226166 int iCode;
226167 int bRemoveDiacritics = 0;
226168 iCode = sqlite3_value_int(apVal[0]);
226169 if( nArg==2 ) bRemoveDiacritics = sqlite3_value_int(apVal[1]);
226170 sqlite3_result_int(pCtx, sqlite3Fts5UnicodeFold(iCode, bRemoveDiacritics));
226171 }
226172}
226173#endif /* ifdef SQLITE_TEST */
226174
226175/*
226176** This is called during initialization to register the fts5_expr() scalar
226177** UDF with the SQLite handle passed as the only argument.
226178*/
226179static int sqlite3Fts5ExprInit(Fts5Global *pGlobal, sqlite3 *db){
226180#ifdef SQLITE_TEST
226181 struct Fts5ExprFunc {
226182 const char *z;
226183 void (*x)(sqlite3_context*,int,sqlite3_value**);
226184 } aFunc[] = {
226185 { "fts5_expr", fts5ExprFunctionHr },
226186 { "fts5_expr_tcl", fts5ExprFunctionTcl },
226187 { "fts5_isalnum", fts5ExprIsAlnum },
226188 { "fts5_fold", fts5ExprFold },
226189 };
226190 int i;
226191 int rc = SQLITE_OK0;
226192 void *pCtx = (void*)pGlobal;
226193
226194 for(i=0; rc==SQLITE_OK0 && i<ArraySize(aFunc)((int)(sizeof(aFunc)/sizeof(aFunc[0]))); i++){
226195 struct Fts5ExprFunc *p = &aFunc[i];
226196 rc = sqlite3_create_function(db, p->z, -1, SQLITE_UTF81, pCtx, p->x, 0, 0);
226197 }
226198#else
226199 int rc = SQLITE_OK0;
226200 UNUSED_PARAM2(pGlobal,db)(void)(pGlobal), (void)(db);
226201#endif
226202
226203 /* Avoid warnings indicating that sqlite3Fts5ParserTrace() and
226204 ** sqlite3Fts5ParserFallback() are unused */
226205#ifndef NDEBUG1
226206 (void)sqlite3Fts5ParserTrace;
226207#endif
226208 (void)sqlite3Fts5ParserFallback;
226209
226210 return rc;
226211}
226212
226213/*
226214** Return the number of phrases in expression pExpr.
226215*/
226216static int sqlite3Fts5ExprPhraseCount(Fts5Expr *pExpr){
226217 return (pExpr ? pExpr->nPhrase : 0);
226218}
226219
226220/*
226221** Return the number of terms in the iPhrase'th phrase in pExpr.
226222*/
226223static int sqlite3Fts5ExprPhraseSize(Fts5Expr *pExpr, int iPhrase){
226224 if( iPhrase<0 || iPhrase>=pExpr->nPhrase ) return 0;
226225 return pExpr->apExprPhrase[iPhrase]->nTerm;
226226}
226227
226228/*
226229** This function is used to access the current position list for phrase
226230** iPhrase.
226231*/
226232static int sqlite3Fts5ExprPoslist(Fts5Expr *pExpr, int iPhrase, const u8 **pa){
226233 int nRet;
226234 Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
226235 Fts5ExprNode *pNode = pPhrase->pNode;
226236 if( pNode->bEof==0 && pNode->iRowid==pExpr->pRoot->iRowid ){
226237 *pa = pPhrase->poslist.p;
226238 nRet = pPhrase->poslist.n;
226239 }else{
226240 *pa = 0;
226241 nRet = 0;
226242 }
226243 return nRet;
226244}
226245
226246struct Fts5PoslistPopulator {
226247 Fts5PoslistWriter writer;
226248 int bOk; /* True if ok to populate */
226249 int bMiss;
226250};
226251
226252/*
226253** Clear the position lists associated with all phrases in the expression
226254** passed as the first argument. Argument bLive is true if the expression
226255** might be pointing to a real entry, otherwise it has just been reset.
226256**
226257** At present this function is only used for detail=col and detail=none
226258** fts5 tables. This implies that all phrases must be at most 1 token
226259** in size, as phrase matches are not supported without detail=full.
226260*/
226261static Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr *pExpr, int bLive){
226262 Fts5PoslistPopulator *pRet;
226263 pRet = sqlite3_malloc64(sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
226264 if( pRet ){
226265 int i;
226266 memset(pRet, 0, sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
226267 for(i=0; i<pExpr->nPhrase; i++){
226268 Fts5Buffer *pBuf = &pExpr->apExprPhrase[i]->poslist;
226269 Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
226270 assert( pExpr->apExprPhrase[i]->nTerm<=1 )((void) (0));
226271 if( bLive &&
226272 (pBuf->n==0 || pNode->iRowid!=pExpr->pRoot->iRowid || pNode->bEof)
226273 ){
226274 pRet[i].bMiss = 1;
226275 }else{
226276 pBuf->n = 0;
226277 }
226278 }
226279 }
226280 return pRet;
226281}
226282
226283struct Fts5ExprCtx {
226284 Fts5Expr *pExpr;
226285 Fts5PoslistPopulator *aPopulator;
226286 i64 iOff;
226287};
226288typedef struct Fts5ExprCtx Fts5ExprCtx;
226289
226290/*
226291** TODO: Make this more efficient!
226292*/
226293static int fts5ExprColsetTest(Fts5Colset *pColset, int iCol){
226294 int i;
226295 for(i=0; i<pColset->nCol; i++){
226296 if( pColset->aiCol[i]==iCol ) return 1;
226297 }
226298 return 0;
226299}
226300
226301static int fts5ExprPopulatePoslistsCb(
226302 void *pCtx, /* Copy of 2nd argument to xTokenize() */
226303 int tflags, /* Mask of FTS5_TOKEN_* flags */
226304 const char *pToken, /* Pointer to buffer containing token */
226305 int nToken, /* Size of token in bytes */
226306 int iUnused1, /* Byte offset of token within input text */
226307 int iUnused2 /* Byte offset of end of token within input text */
226308){
226309 Fts5ExprCtx *p = (Fts5ExprCtx*)pCtx;
226310 Fts5Expr *pExpr = p->pExpr;
226311 int i;
226312
226313 UNUSED_PARAM2(iUnused1, iUnused2)(void)(iUnused1), (void)(iUnused2);
226314
226315 if( nToken>FTS5_MAX_TOKEN_SIZE32768 ) nToken = FTS5_MAX_TOKEN_SIZE32768;
226316 if( (tflags & FTS5_TOKEN_COLOCATED0x0001)==0 ) p->iOff++;
226317 for(i=0; i<pExpr->nPhrase; i++){
226318 Fts5ExprTerm *pTerm;
226319 if( p->aPopulator[i].bOk==0 ) continue;
226320 for(pTerm=&pExpr->apExprPhrase[i]->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
226321 int nTerm = (int)strlen(pTerm->zTerm);
226322 if( (nTerm==nToken || (nTerm<nToken && pTerm->bPrefix))
226323 && memcmp(pTerm->zTerm, pToken, nTerm)==0
226324 ){
226325 int rc = sqlite3Fts5PoslistWriterAppend(
226326 &pExpr->apExprPhrase[i]->poslist, &p->aPopulator[i].writer, p->iOff
226327 );
226328 if( rc ) return rc;
226329 break;
226330 }
226331 }
226332 }
226333 return SQLITE_OK0;
226334}
226335
226336static int sqlite3Fts5ExprPopulatePoslists(
226337 Fts5Config *pConfig,
226338 Fts5Expr *pExpr,
226339 Fts5PoslistPopulator *aPopulator,
226340 int iCol,
226341 const char *z, int n
226342){
226343 int i;
226344 Fts5ExprCtx sCtx;
226345 sCtx.pExpr = pExpr;
226346 sCtx.aPopulator = aPopulator;
226347 sCtx.iOff = (((i64)iCol) << 32) - 1;
226348
226349 for(i=0; i<pExpr->nPhrase; i++){
226350 Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
226351 Fts5Colset *pColset = pNode->pNear->pColset;
226352 if( (pColset && 0==fts5ExprColsetTest(pColset, iCol))
226353 || aPopulator[i].bMiss
226354 ){
226355 aPopulator[i].bOk = 0;
226356 }else{
226357 aPopulator[i].bOk = 1;
226358 }
226359 }
226360
226361 return sqlite3Fts5Tokenize(pConfig,
226362 FTS5_TOKENIZE_DOCUMENT0x0004, z, n, (void*)&sCtx, fts5ExprPopulatePoslistsCb
226363 );
226364}
226365
226366static void fts5ExprClearPoslists(Fts5ExprNode *pNode){
226367 if( pNode->eType==FTS5_TERM4 || pNode->eType==FTS5_STRING9 ){
226368 pNode->pNear->apPhrase[0]->poslist.n = 0;
226369 }else{
226370 int i;
226371 for(i=0; i<pNode->nChild; i++){
226372 fts5ExprClearPoslists(pNode->apChild[i]);
226373 }
226374 }
226375}
226376
226377static int fts5ExprCheckPoslists(Fts5ExprNode *pNode, i64 iRowid){
226378 pNode->iRowid = iRowid;
226379 pNode->bEof = 0;
226380 switch( pNode->eType ){
226381 case FTS5_TERM4:
226382 case FTS5_STRING9:
226383 return (pNode->pNear->apPhrase[0]->poslist.n>0);
226384
226385 case FTS5_AND2: {
226386 int i;
226387 for(i=0; i<pNode->nChild; i++){
226388 if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid)==0 ){
226389 fts5ExprClearPoslists(pNode);
226390 return 0;
226391 }
226392 }
226393 break;
226394 }
226395
226396 case FTS5_OR1: {
226397 int i;
226398 int bRet = 0;
226399 for(i=0; i<pNode->nChild; i++){
226400 if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid) ){
226401 bRet = 1;
226402 }
226403 }
226404 return bRet;
226405 }
226406
226407 default: {
226408 assert( pNode->eType==FTS5_NOT )((void) (0));
226409 if( 0==fts5ExprCheckPoslists(pNode->apChild[0], iRowid)
226410 || 0!=fts5ExprCheckPoslists(pNode->apChild[1], iRowid)
226411 ){
226412 fts5ExprClearPoslists(pNode);
226413 return 0;
226414 }
226415 break;
226416 }
226417 }
226418 return 1;
226419}
226420
226421static void sqlite3Fts5ExprCheckPoslists(Fts5Expr *pExpr, i64 iRowid){
226422 fts5ExprCheckPoslists(pExpr->pRoot, iRowid);
226423}
226424
226425/*
226426** This function is only called for detail=columns tables.
226427*/
226428static int sqlite3Fts5ExprPhraseCollist(
226429 Fts5Expr *pExpr,
226430 int iPhrase,
226431 const u8 **ppCollist,
226432 int *pnCollist
226433){
226434 Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
226435 Fts5ExprNode *pNode = pPhrase->pNode;
226436 int rc = SQLITE_OK0;
226437
226438 assert( iPhrase>=0 && iPhrase<pExpr->nPhrase )((void) (0));
226439 assert( pExpr->pConfig->eDetail==FTS5_DETAIL_COLUMNS )((void) (0));
226440
226441 if( pNode->bEof==0
226442 && pNode->iRowid==pExpr->pRoot->iRowid
226443 && pPhrase->poslist.n>0
226444 ){
226445 Fts5ExprTerm *pTerm = &pPhrase->aTerm[0];
226446 if( pTerm->pSynonym ){
226447 Fts5Buffer *pBuf = (Fts5Buffer*)&pTerm->pSynonym[1];
226448 rc = fts5ExprSynonymList(
226449 pTerm, pNode->iRowid, pBuf, (u8**)ppCollist, pnCollist
226450 );
226451 }else{
226452 *ppCollist = pPhrase->aTerm[0].pIter->pData;
226453 *pnCollist = pPhrase->aTerm[0].pIter->nData;
226454 }
226455 }else{
226456 *ppCollist = 0;
226457 *pnCollist = 0;
226458 }
226459
226460 return rc;
226461}
226462
226463/*
226464** 2014 August 11
226465**
226466** The author disclaims copyright to this source code. In place of
226467** a legal notice, here is a blessing:
226468**
226469** May you do good and not evil.
226470** May you find forgiveness for yourself and forgive others.
226471** May you share freely, never taking more than you give.
226472**
226473******************************************************************************
226474**
226475*/
226476
226477
226478
226479/* #include "fts5Int.h" */
226480
226481typedef struct Fts5HashEntry Fts5HashEntry;
226482
226483/*
226484** This file contains the implementation of an in-memory hash table used
226485** to accumuluate "term -> doclist" content before it is flused to a level-0
226486** segment.
226487*/
226488
226489
226490struct Fts5Hash {
226491 int eDetail; /* Copy of Fts5Config.eDetail */
226492 int *pnByte; /* Pointer to bytes counter */
226493 int nEntry; /* Number of entries currently in hash */
226494 int nSlot; /* Size of aSlot[] array */
226495 Fts5HashEntry *pScan; /* Current ordered scan item */
226496 Fts5HashEntry **aSlot; /* Array of hash slots */
226497};
226498
226499/*
226500** Each entry in the hash table is represented by an object of the
226501** following type. Each object, its key (a nul-terminated string) and
226502** its current data are stored in a single memory allocation. The
226503** key immediately follows the object in memory. The position list
226504** data immediately follows the key data in memory.
226505**
226506** The data that follows the key is in a similar, but not identical format
226507** to the doclist data stored in the database. It is:
226508**
226509** * Rowid, as a varint
226510** * Position list, without 0x00 terminator.
226511** * Size of previous position list and rowid, as a 4 byte
226512** big-endian integer.
226513**
226514** iRowidOff:
226515** Offset of last rowid written to data area. Relative to first byte of
226516** structure.
226517**
226518** nData:
226519** Bytes of data written since iRowidOff.
226520*/
226521struct Fts5HashEntry {
226522 Fts5HashEntry *pHashNext; /* Next hash entry with same hash-key */
226523 Fts5HashEntry *pScanNext; /* Next entry in sorted order */
226524
226525 int nAlloc; /* Total size of allocation */
226526 int iSzPoslist; /* Offset of space for 4-byte poslist size */
226527 int nData; /* Total bytes of data (incl. structure) */
226528 int nKey; /* Length of key in bytes */
226529 u8 bDel; /* Set delete-flag @ iSzPoslist */
226530 u8 bContent; /* Set content-flag (detail=none mode) */
226531 i16 iCol; /* Column of last value written */
226532 int iPos; /* Position of last value written */
226533 i64 iRowid; /* Rowid of last value written */
226534};
226535
226536/*
226537** Eqivalent to:
226538**
226539** char *fts5EntryKey(Fts5HashEntry *pEntry){ return zKey; }
226540*/
226541#define fts5EntryKey(p)( ((char *)(&(p)[1])) ) ( ((char *)(&(p)[1])) )
226542
226543
226544/*
226545** Allocate a new hash table.
226546*/
226547static int sqlite3Fts5HashNew(Fts5Config *pConfig, Fts5Hash **ppNew, int *pnByte){
226548 int rc = SQLITE_OK0;
226549 Fts5Hash *pNew;
226550
226551 *ppNew = pNew = (Fts5Hash*)sqlite3_malloc(sizeof(Fts5Hash));
226552 if( pNew==0 ){
226553 rc = SQLITE_NOMEM7;
226554 }else{
226555 sqlite3_int64 nByte;
226556 memset(pNew, 0, sizeof(Fts5Hash));
226557 pNew->pnByte = pnByte;
226558 pNew->eDetail = pConfig->eDetail;
226559
226560 pNew->nSlot = 1024;
226561 nByte = sizeof(Fts5HashEntry*) * pNew->nSlot;
226562 pNew->aSlot = (Fts5HashEntry**)sqlite3_malloc64(nByte);
226563 if( pNew->aSlot==0 ){
226564 sqlite3_free(pNew);
226565 *ppNew = 0;
226566 rc = SQLITE_NOMEM7;
226567 }else{
226568 memset(pNew->aSlot, 0, (size_t)nByte);
226569 }
226570 }
226571 return rc;
226572}
226573
226574/*
226575** Free a hash table object.
226576*/
226577static void sqlite3Fts5HashFree(Fts5Hash *pHash){
226578 if( pHash ){
226579 sqlite3Fts5HashClear(pHash);
226580 sqlite3_free(pHash->aSlot);
226581 sqlite3_free(pHash);
226582 }
226583}
226584
226585/*
226586** Empty (but do not delete) a hash table.
226587*/
226588static void sqlite3Fts5HashClear(Fts5Hash *pHash){
226589 int i;
226590 for(i=0; i<pHash->nSlot; i++){
226591 Fts5HashEntry *pNext;
226592 Fts5HashEntry *pSlot;
226593 for(pSlot=pHash->aSlot[i]; pSlot; pSlot=pNext){
226594 pNext = pSlot->pHashNext;
226595 sqlite3_free(pSlot);
226596 }
226597 }
226598 memset(pHash->aSlot, 0, pHash->nSlot * sizeof(Fts5HashEntry*));
226599 pHash->nEntry = 0;
226600}
226601
226602static unsigned int fts5HashKey(int nSlot, const u8 *p, int n){
226603 int i;
226604 unsigned int h = 13;
226605 for(i=n-1; i>=0; i--){
226606 h = (h << 3) ^ h ^ p[i];
226607 }
226608 return (h % nSlot);
226609}
226610
226611static unsigned int fts5HashKey2(int nSlot, u8 b, const u8 *p, int n){
226612 int i;
226613 unsigned int h = 13;
226614 for(i=n-1; i>=0; i--){
226615 h = (h << 3) ^ h ^ p[i];
226616 }
226617 h = (h << 3) ^ h ^ b;
226618 return (h % nSlot);
226619}
226620
226621/*
226622** Resize the hash table by doubling the number of slots.
226623*/
226624static int fts5HashResize(Fts5Hash *pHash){
226625 int nNew = pHash->nSlot*2;
226626 int i;
226627 Fts5HashEntry **apNew;
226628 Fts5HashEntry **apOld = pHash->aSlot;
226629
226630 apNew = (Fts5HashEntry**)sqlite3_malloc64(nNew*sizeof(Fts5HashEntry*));
226631 if( !apNew ) return SQLITE_NOMEM7;
226632 memset(apNew, 0, nNew*sizeof(Fts5HashEntry*));
226633
226634 for(i=0; i<pHash->nSlot; i++){
226635 while( apOld[i] ){
226636 unsigned int iHash;
226637 Fts5HashEntry *p = apOld[i];
226638 apOld[i] = p->pHashNext;
226639 iHash = fts5HashKey(nNew, (u8*)fts5EntryKey(p)( ((char *)(&(p)[1])) ),
226640 (int)strlen(fts5EntryKey(p)( ((char *)(&(p)[1])) )));
226641 p->pHashNext = apNew[iHash];
226642 apNew[iHash] = p;
226643 }
226644 }
226645
226646 sqlite3_free(apOld);
226647 pHash->nSlot = nNew;
226648 pHash->aSlot = apNew;
226649 return SQLITE_OK0;
226650}
226651
226652static int fts5HashAddPoslistSize(
226653 Fts5Hash *pHash,
226654 Fts5HashEntry *p,
226655 Fts5HashEntry *p2
226656){
226657 int nRet = 0;
226658 if( p->iSzPoslist ){
226659 u8 *pPtr = p2 ? (u8*)p2 : (u8*)p;
226660 int nData = p->nData;
226661 if( pHash->eDetail==FTS5_DETAIL_NONE1 ){
226662 assert( nData==p->iSzPoslist )((void) (0));
226663 if( p->bDel ){
226664 pPtr[nData++] = 0x00;
226665 if( p->bContent ){
226666 pPtr[nData++] = 0x00;
226667 }
226668 }
226669 }else{
226670 int nSz = (nData - p->iSzPoslist - 1); /* Size in bytes */
226671 int nPos = nSz*2 + p->bDel; /* Value of nPos field */
226672
226673 assert( p->bDel==0 || p->bDel==1 )((void) (0));
226674 if( nPos<=127 ){
226675 pPtr[p->iSzPoslist] = (u8)nPos;
226676 }else{
226677 int nByte = sqlite3Fts5GetVarintLen((u32)nPos);
226678 memmove(&pPtr[p->iSzPoslist + nByte], &pPtr[p->iSzPoslist + 1], nSz);
226679 sqlite3Fts5PutVarint(&pPtr[p->iSzPoslist], nPos);
226680 nData += (nByte-1);
226681 }
226682 }
226683
226684 nRet = nData - p->nData;
226685 if( p2==0 ){
226686 p->iSzPoslist = 0;
226687 p->bDel = 0;
226688 p->bContent = 0;
226689 p->nData = nData;
226690 }
226691 }
226692 return nRet;
226693}
226694
226695/*
226696** Add an entry to the in-memory hash table. The key is the concatenation
226697** of bByte and (pToken/nToken). The value is (iRowid/iCol/iPos).
226698**
226699** (bByte || pToken) -> (iRowid,iCol,iPos)
226700**
226701** Or, if iCol is negative, then the value is a delete marker.
226702*/
226703static int sqlite3Fts5HashWrite(
226704 Fts5Hash *pHash,
226705 i64 iRowid, /* Rowid for this entry */
226706 int iCol, /* Column token appears in (-ve -> delete) */
226707 int iPos, /* Position of token within column */
226708 char bByte, /* First byte of token */
226709 const char *pToken, int nToken /* Token to add or remove to or from index */
226710){
226711 unsigned int iHash;
226712 Fts5HashEntry *p;
226713 u8 *pPtr;
226714 int nIncr = 0; /* Amount to increment (*pHash->pnByte) by */
226715 int bNew; /* If non-delete entry should be written */
226716
226717 bNew = (pHash->eDetail==FTS5_DETAIL_FULL0);
226718
226719 /* Attempt to locate an existing hash entry */
226720 iHash = fts5HashKey2(pHash->nSlot, (u8)bByte, (const u8*)pToken, nToken);
226721 for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){
226722 char *zKey = fts5EntryKey(p)( ((char *)(&(p)[1])) );
226723 if( zKey[0]==bByte
226724 && p->nKey==nToken
226725 && memcmp(&zKey[1], pToken, nToken)==0
226726 ){
226727 break;
226728 }
226729 }
226730
226731 /* If an existing hash entry cannot be found, create a new one. */
226732 if( p==0 ){
226733 /* Figure out how much space to allocate */
226734 char *zKey;
226735 sqlite3_int64 nByte = sizeof(Fts5HashEntry) + (nToken+1) + 1 + 64;
226736 if( nByte<128 ) nByte = 128;
226737
226738 /* Grow the Fts5Hash.aSlot[] array if necessary. */
226739 if( (pHash->nEntry*2)>=pHash->nSlot ){
226740 int rc = fts5HashResize(pHash);
226741 if( rc!=SQLITE_OK0 ) return rc;
226742 iHash = fts5HashKey2(pHash->nSlot, (u8)bByte, (const u8*)pToken, nToken);
226743 }
226744
226745 /* Allocate new Fts5HashEntry and add it to the hash table. */
226746 p = (Fts5HashEntry*)sqlite3_malloc64(nByte);
226747 if( !p ) return SQLITE_NOMEM7;
226748 memset(p, 0, sizeof(Fts5HashEntry));
226749 p->nAlloc = (int)nByte;
226750 zKey = fts5EntryKey(p)( ((char *)(&(p)[1])) );
226751 zKey[0] = bByte;
226752 memcpy(&zKey[1], pToken, nToken);
226753 assert( iHash==fts5HashKey(pHash->nSlot, (u8*)zKey, nToken+1) )((void) (0));
226754 p->nKey = nToken;
226755 zKey[nToken+1] = '\0';
226756 p->nData = nToken+1 + 1 + sizeof(Fts5HashEntry);
226757 p->pHashNext = pHash->aSlot[iHash];
226758 pHash->aSlot[iHash] = p;
226759 pHash->nEntry++;
226760
226761 /* Add the first rowid field to the hash-entry */
226762 p->nData += sqlite3Fts5PutVarint(&((u8*)p)[p->nData], iRowid);
226763 p->iRowid = iRowid;
226764
226765 p->iSzPoslist = p->nData;
226766 if( pHash->eDetail!=FTS5_DETAIL_NONE1 ){
226767 p->nData += 1;
226768 p->iCol = (pHash->eDetail==FTS5_DETAIL_FULL0 ? 0 : -1);
226769 }
226770
226771 }else{
226772
226773 /* Appending to an existing hash-entry. Check that there is enough
226774 ** space to append the largest possible new entry. Worst case scenario
226775 ** is:
226776 **
226777 ** + 9 bytes for a new rowid,
226778 ** + 4 byte reserved for the "poslist size" varint.
226779 ** + 1 byte for a "new column" byte,
226780 ** + 3 bytes for a new column number (16-bit max) as a varint,
226781 ** + 5 bytes for the new position offset (32-bit max).
226782 */
226783 if( (p->nAlloc - p->nData) < (9 + 4 + 1 + 3 + 5) ){
226784 sqlite3_int64 nNew = p->nAlloc * 2;
226785 Fts5HashEntry *pNew;
226786 Fts5HashEntry **pp;
226787 pNew = (Fts5HashEntry*)sqlite3_realloc64(p, nNew);
226788 if( pNew==0 ) return SQLITE_NOMEM7;
226789 pNew->nAlloc = (int)nNew;
226790 for(pp=&pHash->aSlot[iHash]; *pp!=p; pp=&(*pp)->pHashNext);
226791 *pp = pNew;
226792 p = pNew;
226793 }
226794 nIncr -= p->nData;
226795 }
226796 assert( (p->nAlloc - p->nData) >= (9 + 4 + 1 + 3 + 5) )((void) (0));
226797
226798 pPtr = (u8*)p;
226799
226800 /* If this is a new rowid, append the 4-byte size field for the previous
226801 ** entry, and the new rowid for this entry. */
226802 if( iRowid!=p->iRowid ){
226803 u64 iDiff = (u64)iRowid - (u64)p->iRowid;
226804 fts5HashAddPoslistSize(pHash, p, 0);
226805 p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iDiff);
226806 p->iRowid = iRowid;
226807 bNew = 1;
226808 p->iSzPoslist = p->nData;
226809 if( pHash->eDetail!=FTS5_DETAIL_NONE1 ){
226810 p->nData += 1;
226811 p->iCol = (pHash->eDetail==FTS5_DETAIL_FULL0 ? 0 : -1);
226812 p->iPos = 0;
226813 }
226814 }
226815
226816 if( iCol>=0 ){
226817 if( pHash->eDetail==FTS5_DETAIL_NONE1 ){
226818 p->bContent = 1;
226819 }else{
226820 /* Append a new column value, if necessary */
226821 assert_nc( iCol>=p->iCol )((void) (0));
226822 if( iCol!=p->iCol ){
226823 if( pHash->eDetail==FTS5_DETAIL_FULL0 ){
226824 pPtr[p->nData++] = 0x01;
226825 p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iCol);
226826 p->iCol = (i16)iCol;
226827 p->iPos = 0;
226828 }else{
226829 bNew = 1;
226830 p->iCol = (i16)(iPos = iCol);
226831 }
226832 }
226833
226834 /* Append the new position offset, if necessary */
226835 if( bNew ){
226836 p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iPos - p->iPos + 2);
226837 p->iPos = iPos;
226838 }
226839 }
226840 }else{
226841 /* This is a delete. Set the delete flag. */
226842 p->bDel = 1;
226843 }
226844
226845 nIncr += p->nData;
226846 *pHash->pnByte += nIncr;
226847 return SQLITE_OK0;
226848}
226849
226850
226851/*
226852** Arguments pLeft and pRight point to linked-lists of hash-entry objects,
226853** each sorted in key order. This function merges the two lists into a
226854** single list and returns a pointer to its first element.
226855*/
226856static Fts5HashEntry *fts5HashEntryMerge(
226857 Fts5HashEntry *pLeft,
226858 Fts5HashEntry *pRight
226859){
226860 Fts5HashEntry *p1 = pLeft;
226861 Fts5HashEntry *p2 = pRight;
226862 Fts5HashEntry *pRet = 0;
226863 Fts5HashEntry **ppOut = &pRet;
226864
226865 while( p1 || p2 ){
226866 if( p1==0 ){
226867 *ppOut = p2;
226868 p2 = 0;
226869 }else if( p2==0 ){
226870 *ppOut = p1;
226871 p1 = 0;
226872 }else{
226873 int i = 0;
226874 char *zKey1 = fts5EntryKey(p1)( ((char *)(&(p1)[1])) );
226875 char *zKey2 = fts5EntryKey(p2)( ((char *)(&(p2)[1])) );
226876 while( zKey1[i]==zKey2[i] ) i++;
226877
226878 if( ((u8)zKey1[i])>((u8)zKey2[i]) ){
226879 /* p2 is smaller */
226880 *ppOut = p2;
226881 ppOut = &p2->pScanNext;
226882 p2 = p2->pScanNext;
226883 }else{
226884 /* p1 is smaller */
226885 *ppOut = p1;
226886 ppOut = &p1->pScanNext;
226887 p1 = p1->pScanNext;
226888 }
226889 *ppOut = 0;
226890 }
226891 }
226892
226893 return pRet;
226894}
226895
226896/*
226897** Extract all tokens from hash table iHash and link them into a list
226898** in sorted order. The hash table is cleared before returning. It is
226899** the responsibility of the caller to free the elements of the returned
226900** list.
226901*/
226902static int fts5HashEntrySort(
226903 Fts5Hash *pHash,
226904 const char *pTerm, int nTerm, /* Query prefix, if any */
226905 Fts5HashEntry **ppSorted
226906){
226907 const int nMergeSlot = 32;
226908 Fts5HashEntry **ap;
226909 Fts5HashEntry *pList;
226910 int iSlot;
226911 int i;
226912
226913 *ppSorted = 0;
226914 ap = sqlite3_malloc64(sizeof(Fts5HashEntry*) * nMergeSlot);
226915 if( !ap ) return SQLITE_NOMEM7;
226916 memset(ap, 0, sizeof(Fts5HashEntry*) * nMergeSlot);
226917
226918 for(iSlot=0; iSlot<pHash->nSlot; iSlot++){
226919 Fts5HashEntry *pIter;
226920 for(pIter=pHash->aSlot[iSlot]; pIter; pIter=pIter->pHashNext){
226921 if( pTerm==0
226922 || (pIter->nKey+1>=nTerm && 0==memcmp(fts5EntryKey(pIter)( ((char *)(&(pIter)[1])) ), pTerm, nTerm))
226923 ){
226924 Fts5HashEntry *pEntry = pIter;
226925 pEntry->pScanNext = 0;
226926 for(i=0; ap[i]; i++){
226927 pEntry = fts5HashEntryMerge(pEntry, ap[i]);
226928 ap[i] = 0;
226929 }
226930 ap[i] = pEntry;
226931 }
226932 }
226933 }
226934
226935 pList = 0;
226936 for(i=0; i<nMergeSlot; i++){
226937 pList = fts5HashEntryMerge(pList, ap[i]);
226938 }
226939
226940 pHash->nEntry = 0;
226941 sqlite3_free(ap);
226942 *ppSorted = pList;
226943 return SQLITE_OK0;
226944}
226945
226946/*
226947** Query the hash table for a doclist associated with term pTerm/nTerm.
226948*/
226949static int sqlite3Fts5HashQuery(
226950 Fts5Hash *pHash, /* Hash table to query */
226951 int nPre,
226952 const char *pTerm, int nTerm, /* Query term */
226953 void **ppOut, /* OUT: Pointer to new object */
226954 int *pnDoclist /* OUT: Size of doclist in bytes */
226955){
226956 unsigned int iHash = fts5HashKey(pHash->nSlot, (const u8*)pTerm, nTerm);
226957 char *zKey = 0;
226958 Fts5HashEntry *p;
226959
226960 for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){
226961 zKey = fts5EntryKey(p)( ((char *)(&(p)[1])) );
226962 assert( p->nKey+1==(int)strlen(zKey) )((void) (0));
226963 if( nTerm==p->nKey+1 && memcmp(zKey, pTerm, nTerm)==0 ) break;
226964 }
226965
226966 if( p ){
226967 int nHashPre = sizeof(Fts5HashEntry) + nTerm + 1;
226968 int nList = p->nData - nHashPre;
226969 u8 *pRet = (u8*)(*ppOut = sqlite3_malloc64(nPre + nList + 10));
226970 if( pRet ){
226971 Fts5HashEntry *pFaux = (Fts5HashEntry*)&pRet[nPre-nHashPre];
226972 memcpy(&pRet[nPre], &((u8*)p)[nHashPre], nList);
226973 nList += fts5HashAddPoslistSize(pHash, p, pFaux);
226974 *pnDoclist = nList;
226975 }else{
226976 *pnDoclist = 0;
226977 return SQLITE_NOMEM7;
226978 }
226979 }else{
226980 *ppOut = 0;
226981 *pnDoclist = 0;
226982 }
226983
226984 return SQLITE_OK0;
226985}
226986
226987static int sqlite3Fts5HashScanInit(
226988 Fts5Hash *p, /* Hash table to query */
226989 const char *pTerm, int nTerm /* Query prefix */
226990){
226991 return fts5HashEntrySort(p, pTerm, nTerm, &p->pScan);
226992}
226993
226994static void sqlite3Fts5HashScanNext(Fts5Hash *p){
226995 assert( !sqlite3Fts5HashScanEof(p) )((void) (0));
226996 p->pScan = p->pScan->pScanNext;
226997}
226998
226999static int sqlite3Fts5HashScanEof(Fts5Hash *p){
227000 return (p->pScan==0);
227001}
227002
227003static void sqlite3Fts5HashScanEntry(
227004 Fts5Hash *pHash,
227005 const char **pzTerm, /* OUT: term (nul-terminated) */
227006 const u8 **ppDoclist, /* OUT: pointer to doclist */
227007 int *pnDoclist /* OUT: size of doclist in bytes */
227008){
227009 Fts5HashEntry *p;
227010 if( (p = pHash->pScan) ){
227011 char *zKey = fts5EntryKey(p)( ((char *)(&(p)[1])) );
227012 int nTerm = (int)strlen(zKey);
227013 fts5HashAddPoslistSize(pHash, p, 0);
227014 *pzTerm = zKey;
227015 *ppDoclist = (const u8*)&zKey[nTerm+1];
227016 *pnDoclist = p->nData - (sizeof(Fts5HashEntry) + nTerm + 1);
227017 }else{
227018 *pzTerm = 0;
227019 *ppDoclist = 0;
227020 *pnDoclist = 0;
227021 }
227022}
227023
227024/*
227025** 2014 May 31
227026**
227027** The author disclaims copyright to this source code. In place of
227028** a legal notice, here is a blessing:
227029**
227030** May you do good and not evil.
227031** May you find forgiveness for yourself and forgive others.
227032** May you share freely, never taking more than you give.
227033**
227034******************************************************************************
227035**
227036** Low level access to the FTS index stored in the database file. The
227037** routines in this file file implement all read and write access to the
227038** %_data table. Other parts of the system access this functionality via
227039** the interface defined in fts5Int.h.
227040*/
227041
227042
227043/* #include "fts5Int.h" */
227044
227045/*
227046** Overview:
227047**
227048** The %_data table contains all the FTS indexes for an FTS5 virtual table.
227049** As well as the main term index, there may be up to 31 prefix indexes.
227050** The format is similar to FTS3/4, except that:
227051**
227052** * all segment b-tree leaf data is stored in fixed size page records
227053** (e.g. 1000 bytes). A single doclist may span multiple pages. Care is
227054** taken to ensure it is possible to iterate in either direction through
227055** the entries in a doclist, or to seek to a specific entry within a
227056** doclist, without loading it into memory.
227057**
227058** * large doclists that span many pages have associated "doclist index"
227059** records that contain a copy of the first rowid on each page spanned by
227060** the doclist. This is used to speed up seek operations, and merges of
227061** large doclists with very small doclists.
227062**
227063** * extra fields in the "structure record" record the state of ongoing
227064** incremental merge operations.
227065**
227066*/
227067
227068
227069#define FTS5_OPT_WORK_UNIT1000 1000 /* Number of leaf pages per optimize step */
227070#define FTS5_WORK_UNIT64 64 /* Number of leaf pages in unit of work */
227071
227072#define FTS5_MIN_DLIDX_SIZE4 4 /* Add dlidx if this many empty pages */
227073
227074#define FTS5_MAIN_PREFIX'0' '0'
227075
227076#if FTS5_MAX_PREFIX_INDEXES31 > 31
227077# error "FTS5_MAX_PREFIX_INDEXES is too large"
227078#endif
227079
227080/*
227081** Details:
227082**
227083** The %_data table managed by this module,
227084**
227085** CREATE TABLE %_data(id INTEGER PRIMARY KEY, block BLOB);
227086**
227087** , contains the following 5 types of records. See the comments surrounding
227088** the FTS5_*_ROWID macros below for a description of how %_data rowids are
227089** assigned to each fo them.
227090**
227091** 1. Structure Records:
227092**
227093** The set of segments that make up an index - the index structure - are
227094** recorded in a single record within the %_data table. The record consists
227095** of a single 32-bit configuration cookie value followed by a list of
227096** SQLite varints. If the FTS table features more than one index (because
227097** there are one or more prefix indexes), it is guaranteed that all share
227098** the same cookie value.
227099**
227100** Immediately following the configuration cookie, the record begins with
227101** three varints:
227102**
227103** + number of levels,
227104** + total number of segments on all levels,
227105** + value of write counter.
227106**
227107** Then, for each level from 0 to nMax:
227108**
227109** + number of input segments in ongoing merge.
227110** + total number of segments in level.
227111** + for each segment from oldest to newest:
227112** + segment id (always > 0)
227113** + first leaf page number (often 1, always greater than 0)
227114** + final leaf page number
227115**
227116** 2. The Averages Record:
227117**
227118** A single record within the %_data table. The data is a list of varints.
227119** The first value is the number of rows in the index. Then, for each column
227120** from left to right, the total number of tokens in the column for all
227121** rows of the table.
227122**
227123** 3. Segment leaves:
227124**
227125** TERM/DOCLIST FORMAT:
227126**
227127** Most of each segment leaf is taken up by term/doclist data. The
227128** general format of term/doclist, starting with the first term
227129** on the leaf page, is:
227130**
227131** varint : size of first term
227132** blob: first term data
227133** doclist: first doclist
227134** zero-or-more {
227135** varint: number of bytes in common with previous term
227136** varint: number of bytes of new term data (nNew)
227137** blob: nNew bytes of new term data
227138** doclist: next doclist
227139** }
227140**
227141** doclist format:
227142**
227143** varint: first rowid
227144** poslist: first poslist
227145** zero-or-more {
227146** varint: rowid delta (always > 0)
227147** poslist: next poslist
227148** }
227149**
227150** poslist format:
227151**
227152** varint: size of poslist in bytes multiplied by 2, not including
227153** this field. Plus 1 if this entry carries the "delete" flag.
227154** collist: collist for column 0
227155** zero-or-more {
227156** 0x01 byte
227157** varint: column number (I)
227158** collist: collist for column I
227159** }
227160**
227161** collist format:
227162**
227163** varint: first offset + 2
227164** zero-or-more {
227165** varint: offset delta + 2
227166** }
227167**
227168** PAGE FORMAT
227169**
227170** Each leaf page begins with a 4-byte header containing 2 16-bit
227171** unsigned integer fields in big-endian format. They are:
227172**
227173** * The byte offset of the first rowid on the page, if it exists
227174** and occurs before the first term (otherwise 0).
227175**
227176** * The byte offset of the start of the page footer. If the page
227177** footer is 0 bytes in size, then this field is the same as the
227178** size of the leaf page in bytes.
227179**
227180** The page footer consists of a single varint for each term located
227181** on the page. Each varint is the byte offset of the current term
227182** within the page, delta-compressed against the previous value. In
227183** other words, the first varint in the footer is the byte offset of
227184** the first term, the second is the byte offset of the second less that
227185** of the first, and so on.
227186**
227187** The term/doclist format described above is accurate if the entire
227188** term/doclist data fits on a single leaf page. If this is not the case,
227189** the format is changed in two ways:
227190**
227191** + if the first rowid on a page occurs before the first term, it
227192** is stored as a literal value:
227193**
227194** varint: first rowid
227195**
227196** + the first term on each page is stored in the same way as the
227197** very first term of the segment:
227198**
227199** varint : size of first term
227200** blob: first term data
227201**
227202** 5. Segment doclist indexes:
227203**
227204** Doclist indexes are themselves b-trees, however they usually consist of
227205** a single leaf record only. The format of each doclist index leaf page
227206** is:
227207**
227208** * Flags byte. Bits are:
227209** 0x01: Clear if leaf is also the root page, otherwise set.
227210**
227211** * Page number of fts index leaf page. As a varint.
227212**
227213** * First rowid on page indicated by previous field. As a varint.
227214**
227215** * A list of varints, one for each subsequent termless page. A
227216** positive delta if the termless page contains at least one rowid,
227217** or an 0x00 byte otherwise.
227218**
227219** Internal doclist index nodes are:
227220**
227221** * Flags byte. Bits are:
227222** 0x01: Clear for root page, otherwise set.
227223**
227224** * Page number of first child page. As a varint.
227225**
227226** * Copy of first rowid on page indicated by previous field. As a varint.
227227**
227228** * A list of delta-encoded varints - the first rowid on each subsequent
227229** child page.
227230**
227231*/
227232
227233/*
227234** Rowids for the averages and structure records in the %_data table.
227235*/
227236#define FTS5_AVERAGES_ROWID1 1 /* Rowid used for the averages record */
227237#define FTS5_STRUCTURE_ROWID10 10 /* The structure record */
227238
227239/*
227240** Macros determining the rowids used by segment leaves and dlidx leaves
227241** and nodes. All nodes and leaves are stored in the %_data table with large
227242** positive rowids.
227243**
227244** Each segment has a unique non-zero 16-bit id.
227245**
227246** The rowid for each segment leaf is found by passing the segment id and
227247** the leaf page number to the FTS5_SEGMENT_ROWID macro. Leaves are numbered
227248** sequentially starting from 1.
227249*/
227250#define FTS5_DATA_ID_B16 16 /* Max seg id number 65535 */
227251#define FTS5_DATA_DLI_B1 1 /* Doclist-index flag (1 bit) */
227252#define FTS5_DATA_HEIGHT_B5 5 /* Max dlidx tree height of 32 */
227253#define FTS5_DATA_PAGE_B31 31 /* Max page number of 2147483648 */
227254
227255#define fts5_dri(segid, dlidx, height, pgno)( ((i64)(segid) << (31 +5 +1)) + ((i64)(dlidx) <<
(31 + 5)) + ((i64)(height) << (31)) + ((i64)(pgno)) )
( \
227256 ((i64)(segid) << (FTS5_DATA_PAGE_B31+FTS5_DATA_HEIGHT_B5+FTS5_DATA_DLI_B1)) + \
227257 ((i64)(dlidx) << (FTS5_DATA_PAGE_B31 + FTS5_DATA_HEIGHT_B5)) + \
227258 ((i64)(height) << (FTS5_DATA_PAGE_B31)) + \
227259 ((i64)(pgno)) \
227260)
227261
227262#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)) )
227263#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)) )
227264
227265#ifdef SQLITE_DEBUG
227266static int sqlite3Fts5Corrupt() { return SQLITE_CORRUPT_VTAB(11 | (1<<8)); }
227267#endif
227268
227269
227270/*
227271** Each time a blob is read from the %_data table, it is padded with this
227272** many zero bytes. This makes it easier to decode the various record formats
227273** without overreading if the records are corrupt.
227274*/
227275#define FTS5_DATA_ZERO_PADDING8 8
227276#define FTS5_DATA_PADDING20 20
227277
227278typedef struct Fts5Data Fts5Data;
227279typedef struct Fts5DlidxIter Fts5DlidxIter;
227280typedef struct Fts5DlidxLvl Fts5DlidxLvl;
227281typedef struct Fts5DlidxWriter Fts5DlidxWriter;
227282typedef struct Fts5Iter Fts5Iter;
227283typedef struct Fts5PageWriter Fts5PageWriter;
227284typedef struct Fts5SegIter Fts5SegIter;
227285typedef struct Fts5DoclistIter Fts5DoclistIter;
227286typedef struct Fts5SegWriter Fts5SegWriter;
227287typedef struct Fts5Structure Fts5Structure;
227288typedef struct Fts5StructureLevel Fts5StructureLevel;
227289typedef struct Fts5StructureSegment Fts5StructureSegment;
227290
227291struct Fts5Data {
227292 u8 *p; /* Pointer to buffer containing record */
227293 int nn; /* Size of record in bytes */
227294 int szLeaf; /* Size of leaf without page-index */
227295};
227296
227297/*
227298** One object per %_data table.
227299*/
227300struct Fts5Index {
227301 Fts5Config *pConfig; /* Virtual table configuration */
227302 char *zDataTbl; /* Name of %_data table */
227303 int nWorkUnit; /* Leaf pages in a "unit" of work */
227304
227305 /*
227306 ** Variables related to the accumulation of tokens and doclists within the
227307 ** in-memory hash tables before they are flushed to disk.
227308 */
227309 Fts5Hash *pHash; /* Hash table for in-memory data */
227310 int nPendingData; /* Current bytes of pending data */
227311 i64 iWriteRowid; /* Rowid for current doc being written */
227312 int bDelete; /* Current write is a delete */
227313
227314 /* Error state. */
227315 int rc; /* Current error code */
227316
227317 /* State used by the fts5DataXXX() functions. */
227318 sqlite3_blob *pReader; /* RO incr-blob open on %_data table */
227319 sqlite3_stmt *pWriter; /* "INSERT ... %_data VALUES(?,?)" */
227320 sqlite3_stmt *pDeleter; /* "DELETE FROM %_data ... id>=? AND id<=?" */
227321 sqlite3_stmt *pIdxWriter; /* "INSERT ... %_idx VALUES(?,?,?,?)" */
227322 sqlite3_stmt *pIdxDeleter; /* "DELETE FROM %_idx WHERE segid=?" */
227323 sqlite3_stmt *pIdxSelect;
227324 int nRead; /* Total number of blocks read */
227325
227326 sqlite3_stmt *pDataVersion;
227327 i64 iStructVersion; /* data_version when pStruct read */
227328 Fts5Structure *pStruct; /* Current db structure (or NULL) */
227329};
227330
227331struct Fts5DoclistIter {
227332 u8 *aEof; /* Pointer to 1 byte past end of doclist */
227333
227334 /* Output variables. aPoslist==0 at EOF */
227335 i64 iRowid;
227336 u8 *aPoslist;
227337 int nPoslist;
227338 int nSize;
227339};
227340
227341/*
227342** The contents of the "structure" record for each index are represented
227343** using an Fts5Structure record in memory. Which uses instances of the
227344** other Fts5StructureXXX types as components.
227345*/
227346struct Fts5StructureSegment {
227347 int iSegid; /* Segment id */
227348 int pgnoFirst; /* First leaf page number in segment */
227349 int pgnoLast; /* Last leaf page number in segment */
227350};
227351struct Fts5StructureLevel {
227352 int nMerge; /* Number of segments in incr-merge */
227353 int nSeg; /* Total number of segments on level */
227354 Fts5StructureSegment *aSeg; /* Array of segments. aSeg[0] is oldest. */
227355};
227356struct Fts5Structure {
227357 int nRef; /* Object reference count */
227358 u64 nWriteCounter; /* Total leaves written to level 0 */
227359 int nSegment; /* Total segments in this structure */
227360 int nLevel; /* Number of levels in this index */
227361 Fts5StructureLevel aLevel[1]; /* Array of nLevel level objects */
227362};
227363
227364/*
227365** An object of type Fts5SegWriter is used to write to segments.
227366*/
227367struct Fts5PageWriter {
227368 int pgno; /* Page number for this page */
227369 int iPrevPgidx; /* Previous value written into pgidx */
227370 Fts5Buffer buf; /* Buffer containing leaf data */
227371 Fts5Buffer pgidx; /* Buffer containing page-index */
227372 Fts5Buffer term; /* Buffer containing previous term on page */
227373};
227374struct Fts5DlidxWriter {
227375 int pgno; /* Page number for this page */
227376 int bPrevValid; /* True if iPrev is valid */
227377 i64 iPrev; /* Previous rowid value written to page */
227378 Fts5Buffer buf; /* Buffer containing page data */
227379};
227380struct Fts5SegWriter {
227381 int iSegid; /* Segid to write to */
227382 Fts5PageWriter writer; /* PageWriter object */
227383 i64 iPrevRowid; /* Previous rowid written to current leaf */
227384 u8 bFirstRowidInDoclist; /* True if next rowid is first in doclist */
227385 u8 bFirstRowidInPage; /* True if next rowid is first in page */
227386 /* TODO1: Can use (writer.pgidx.n==0) instead of bFirstTermInPage */
227387 u8 bFirstTermInPage; /* True if next term will be first in leaf */
227388 int nLeafWritten; /* Number of leaf pages written */
227389 int nEmpty; /* Number of contiguous term-less nodes */
227390
227391 int nDlidx; /* Allocated size of aDlidx[] array */
227392 Fts5DlidxWriter *aDlidx; /* Array of Fts5DlidxWriter objects */
227393
227394 /* Values to insert into the %_idx table */
227395 Fts5Buffer btterm; /* Next term to insert into %_idx table */
227396 int iBtPage; /* Page number corresponding to btterm */
227397};
227398
227399typedef struct Fts5CResult Fts5CResult;
227400struct Fts5CResult {
227401 u16 iFirst; /* aSeg[] index of firstest iterator */
227402 u8 bTermEq; /* True if the terms are equal */
227403};
227404
227405/*
227406** Object for iterating through a single segment, visiting each term/rowid
227407** pair in the segment.
227408**
227409** pSeg:
227410** The segment to iterate through.
227411**
227412** iLeafPgno:
227413** Current leaf page number within segment.
227414**
227415** iLeafOffset:
227416** Byte offset within the current leaf that is the first byte of the
227417** position list data (one byte passed the position-list size field).
227418** rowid field of the current entry. Usually this is the size field of the
227419** position list data. The exception is if the rowid for the current entry
227420** is the last thing on the leaf page.
227421**
227422** pLeaf:
227423** Buffer containing current leaf page data. Set to NULL at EOF.
227424**
227425** iTermLeafPgno, iTermLeafOffset:
227426** Leaf page number containing the last term read from the segment. And
227427** the offset immediately following the term data.
227428**
227429** flags:
227430** Mask of FTS5_SEGITER_XXX values. Interpreted as follows:
227431**
227432** FTS5_SEGITER_ONETERM:
227433** If set, set the iterator to point to EOF after the current doclist
227434** has been exhausted. Do not proceed to the next term in the segment.
227435**
227436** FTS5_SEGITER_REVERSE:
227437** This flag is only ever set if FTS5_SEGITER_ONETERM is also set. If
227438** it is set, iterate through rowid in descending order instead of the
227439** default ascending order.
227440**
227441** iRowidOffset/nRowidOffset/aRowidOffset:
227442** These are used if the FTS5_SEGITER_REVERSE flag is set.
227443**
227444** For each rowid on the page corresponding to the current term, the
227445** corresponding aRowidOffset[] entry is set to the byte offset of the
227446** start of the "position-list-size" field within the page.
227447**
227448** iTermIdx:
227449** Index of current term on iTermLeafPgno.
227450*/
227451struct Fts5SegIter {
227452 Fts5StructureSegment *pSeg; /* Segment to iterate through */
227453 int flags; /* Mask of configuration flags */
227454 int iLeafPgno; /* Current leaf page number */
227455 Fts5Data *pLeaf; /* Current leaf data */
227456 Fts5Data *pNextLeaf; /* Leaf page (iLeafPgno+1) */
227457 i64 iLeafOffset; /* Byte offset within current leaf */
227458
227459 /* Next method */
227460 void (*xNext)(Fts5Index*, Fts5SegIter*, int*);
227461
227462 /* The page and offset from which the current term was read. The offset
227463 ** is the offset of the first rowid in the current doclist. */
227464 int iTermLeafPgno;
227465 int iTermLeafOffset;
227466
227467 int iPgidxOff; /* Next offset in pgidx */
227468 int iEndofDoclist;
227469
227470 /* The following are only used if the FTS5_SEGITER_REVERSE flag is set. */
227471 int iRowidOffset; /* Current entry in aRowidOffset[] */
227472 int nRowidOffset; /* Allocated size of aRowidOffset[] array */
227473 int *aRowidOffset; /* Array of offset to rowid fields */
227474
227475 Fts5DlidxIter *pDlidx; /* If there is a doclist-index */
227476
227477 /* Variables populated based on current entry. */
227478 Fts5Buffer term; /* Current term */
227479 i64 iRowid; /* Current rowid */
227480 int nPos; /* Number of bytes in current position list */
227481 u8 bDel; /* True if the delete flag is set */
227482};
227483
227484/*
227485** Argument is a pointer to an Fts5Data structure that contains a
227486** leaf page.
227487*/
227488#define ASSERT_SZLEAF_OK(x)((void) (0)) assert( \((void) (0))
227489 (x)->szLeaf==(x)->nn || (x)->szLeaf==fts5GetU16(&(x)->p[2]) \((void) (0))
227490)((void) (0))
227491
227492#define FTS5_SEGITER_ONETERM0x01 0x01
227493#define FTS5_SEGITER_REVERSE0x02 0x02
227494
227495/*
227496** Argument is a pointer to an Fts5Data structure that contains a leaf
227497** page. This macro evaluates to true if the leaf contains no terms, or
227498** false if it contains at least one term.
227499*/
227500#define fts5LeafIsTermless(x)((x)->szLeaf >= (x)->nn) ((x)->szLeaf >= (x)->nn)
227501
227502#define fts5LeafTermOff(x, i)(fts5GetU16(&(x)->p[(x)->szLeaf + (i)*2])) (fts5GetU16(&(x)->p[(x)->szLeaf + (i)*2]))
227503
227504#define fts5LeafFirstRowidOff(x)(fts5GetU16((x)->p)) (fts5GetU16((x)->p))
227505
227506/*
227507** Object for iterating through the merged results of one or more segments,
227508** visiting each term/rowid pair in the merged data.
227509**
227510** nSeg is always a power of two greater than or equal to the number of
227511** segments that this object is merging data from. Both the aSeg[] and
227512** aFirst[] arrays are sized at nSeg entries. The aSeg[] array is padded
227513** with zeroed objects - these are handled as if they were iterators opened
227514** on empty segments.
227515**
227516** The results of comparing segments aSeg[N] and aSeg[N+1], where N is an
227517** even number, is stored in aFirst[(nSeg+N)/2]. The "result" of the
227518** comparison in this context is the index of the iterator that currently
227519** points to the smaller term/rowid combination. Iterators at EOF are
227520** considered to be greater than all other iterators.
227521**
227522** aFirst[1] contains the index in aSeg[] of the iterator that points to
227523** the smallest key overall. aFirst[0] is unused.
227524**
227525** poslist:
227526** Used by sqlite3Fts5IterPoslist() when the poslist needs to be buffered.
227527** There is no way to tell if this is populated or not.
227528*/
227529struct Fts5Iter {
227530 Fts5IndexIter base; /* Base class containing output vars */
227531
227532 Fts5Index *pIndex; /* Index that owns this iterator */
227533 Fts5Buffer poslist; /* Buffer containing current poslist */
227534 Fts5Colset *pColset; /* Restrict matches to these columns */
227535
227536 /* Invoked to set output variables. */
227537 void (*xSetOutputs)(Fts5Iter*, Fts5SegIter*);
227538
227539 int nSeg; /* Size of aSeg[] array */
227540 int bRev; /* True to iterate in reverse order */
227541 u8 bSkipEmpty; /* True to skip deleted entries */
227542
227543 i64 iSwitchRowid; /* Firstest rowid of other than aFirst[1] */
227544 Fts5CResult *aFirst; /* Current merge state (see above) */
227545 Fts5SegIter aSeg[1]; /* Array of segment iterators */
227546};
227547
227548
227549/*
227550** An instance of the following type is used to iterate through the contents
227551** of a doclist-index record.
227552**
227553** pData:
227554** Record containing the doclist-index data.
227555**
227556** bEof:
227557** Set to true once iterator has reached EOF.
227558**
227559** iOff:
227560** Set to the current offset within record pData.
227561*/
227562struct Fts5DlidxLvl {
227563 Fts5Data *pData; /* Data for current page of this level */
227564 int iOff; /* Current offset into pData */
227565 int bEof; /* At EOF already */
227566 int iFirstOff; /* Used by reverse iterators */
227567
227568 /* Output variables */
227569 int iLeafPgno; /* Page number of current leaf page */
227570 i64 iRowid; /* First rowid on leaf iLeafPgno */
227571};
227572struct Fts5DlidxIter {
227573 int nLvl;
227574 int iSegid;
227575 Fts5DlidxLvl aLvl[1];
227576};
227577
227578static void fts5PutU16(u8 *aOut, u16 iVal){
227579 aOut[0] = (iVal>>8);
227580 aOut[1] = (iVal&0xFF);
227581}
227582
227583static u16 fts5GetU16(const u8 *aIn){
227584 return ((u16)aIn[0] << 8) + aIn[1];
227585}
227586
227587/*
227588** Allocate and return a buffer at least nByte bytes in size.
227589**
227590** If an OOM error is encountered, return NULL and set the error code in
227591** the Fts5Index handle passed as the first argument.
227592*/
227593static void *fts5IdxMalloc(Fts5Index *p, sqlite3_int64 nByte){
227594 return sqlite3Fts5MallocZero(&p->rc, nByte);
227595}
227596
227597/*
227598** Compare the contents of the pLeft buffer with the pRight/nRight blob.
227599**
227600** Return -ve if pLeft is smaller than pRight, 0 if they are equal or
227601** +ve if pRight is smaller than pLeft. In other words:
227602**
227603** res = *pLeft - *pRight
227604*/
227605#ifdef SQLITE_DEBUG
227606static int fts5BufferCompareBlob(
227607 Fts5Buffer *pLeft, /* Left hand side of comparison */
227608 const u8 *pRight, int nRight /* Right hand side of comparison */
227609){
227610 int nCmp = MIN(pLeft->n, nRight)((pLeft->n)<(nRight)?(pLeft->n):(nRight));
227611 int res = memcmp(pLeft->p, pRight, nCmp);
227612 return (res==0 ? (pLeft->n - nRight) : res);
227613}
227614#endif
227615
227616/*
227617** Compare the contents of the two buffers using memcmp(). If one buffer
227618** is a prefix of the other, it is considered the lesser.
227619**
227620** Return -ve if pLeft is smaller than pRight, 0 if they are equal or
227621** +ve if pRight is smaller than pLeft. In other words:
227622**
227623** res = *pLeft - *pRight
227624*/
227625static int fts5BufferCompare(Fts5Buffer *pLeft, Fts5Buffer *pRight){
227626 int nCmp, res;
227627 nCmp = MIN(pLeft->n, pRight->n)((pLeft->n)<(pRight->n)?(pLeft->n):(pRight->n)
)
;
227628 assert( nCmp<=0 || pLeft->p!=0 )((void) (0));
227629 assert( nCmp<=0 || pRight->p!=0 )((void) (0));
227630 res = fts5Memcmp(pLeft->p, pRight->p, nCmp)((nCmp)<=0 ? 0 : memcmp((pLeft->p), (pRight->p), (nCmp
)))
;
227631 return (res==0 ? (pLeft->n - pRight->n) : res);
227632}
227633
227634static int fts5LeafFirstTermOff(Fts5Data *pLeaf){
227635 int ret;
227636 fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf], ret)sqlite3Fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf],(u32
*)&ret)
;
227637 return ret;
227638}
227639
227640/*
227641** Close the read-only blob handle, if it is open.
227642*/
227643static void sqlite3Fts5IndexCloseReader(Fts5Index *p){
227644 if( p->pReader ){
227645 sqlite3_blob *pReader = p->pReader;
227646 p->pReader = 0;
227647 sqlite3_blob_close(pReader);
227648 }
227649}
227650
227651/*
227652** Retrieve a record from the %_data table.
227653**
227654** If an error occurs, NULL is returned and an error left in the
227655** Fts5Index object.
227656*/
227657static Fts5Data *fts5DataRead(Fts5Index *p, i64 iRowid){
227658 Fts5Data *pRet = 0;
227659 if( p->rc==SQLITE_OK0 ){
227660 int rc = SQLITE_OK0;
227661
227662 if( p->pReader ){
227663 /* This call may return SQLITE_ABORT if there has been a savepoint
227664 ** rollback since it was last used. In this case a new blob handle
227665 ** is required. */
227666 sqlite3_blob *pBlob = p->pReader;
227667 p->pReader = 0;
227668 rc = sqlite3_blob_reopen(pBlob, iRowid);
227669 assert( p->pReader==0 )((void) (0));
227670 p->pReader = pBlob;
227671 if( rc!=SQLITE_OK0 ){
227672 sqlite3Fts5IndexCloseReader(p);
227673 }
227674 if( rc==SQLITE_ABORT4 ) rc = SQLITE_OK0;
227675 }
227676
227677 /* If the blob handle is not open at this point, open it and seek
227678 ** to the requested entry. */
227679 if( p->pReader==0 && rc==SQLITE_OK0 ){
227680 Fts5Config *pConfig = p->pConfig;
227681 rc = sqlite3_blob_open(pConfig->db,
227682 pConfig->zDb, p->zDataTbl, "block", iRowid, 0, &p->pReader
227683 );
227684 }
227685
227686 /* If either of the sqlite3_blob_open() or sqlite3_blob_reopen() calls
227687 ** above returned SQLITE_ERROR, return SQLITE_CORRUPT_VTAB instead.
227688 ** All the reasons those functions might return SQLITE_ERROR - missing
227689 ** table, missing row, non-blob/text in block column - indicate
227690 ** backing store corruption. */
227691 if( rc==SQLITE_ERROR1 ) rc = FTS5_CORRUPT(11 | (1<<8));
227692
227693 if( rc==SQLITE_OK0 ){
227694 u8 *aOut = 0; /* Read blob data into this buffer */
227695 int nByte = sqlite3_blob_bytes(p->pReader);
227696 sqlite3_int64 nAlloc = sizeof(Fts5Data) + nByte + FTS5_DATA_PADDING20;
227697 pRet = (Fts5Data*)sqlite3_malloc64(nAlloc);
227698 if( pRet ){
227699 pRet->nn = nByte;
227700 aOut = pRet->p = (u8*)&pRet[1];
227701 }else{
227702 rc = SQLITE_NOMEM7;
227703 }
227704
227705 if( rc==SQLITE_OK0 ){
227706 rc = sqlite3_blob_read(p->pReader, aOut, nByte, 0);
227707 }
227708 if( rc!=SQLITE_OK0 ){
227709 sqlite3_free(pRet);
227710 pRet = 0;
227711 }else{
227712 /* TODO1: Fix this */
227713 pRet->p[nByte] = 0x00;
227714 pRet->p[nByte+1] = 0x00;
227715 pRet->szLeaf = fts5GetU16(&pRet->p[2]);
227716 }
227717 }
227718 p->rc = rc;
227719 p->nRead++;
227720 }
227721
227722 assert( (pRet==0)==(p->rc!=SQLITE_OK) )((void) (0));
227723 return pRet;
227724}
227725
227726
227727/*
227728** Release a reference to data record returned by an earlier call to
227729** fts5DataRead().
227730*/
227731static void fts5DataRelease(Fts5Data *pData){
227732 sqlite3_free(pData);
227733}
227734
227735static Fts5Data *fts5LeafRead(Fts5Index *p, i64 iRowid){
227736 Fts5Data *pRet = fts5DataRead(p, iRowid);
227737 if( pRet ){
227738 if( pRet->nn<4 || pRet->szLeaf>pRet->nn ){
227739 p->rc = FTS5_CORRUPT(11 | (1<<8));
227740 fts5DataRelease(pRet);
227741 pRet = 0;
227742 }
227743 }
227744 return pRet;
227745}
227746
227747static int fts5IndexPrepareStmt(
227748 Fts5Index *p,
227749 sqlite3_stmt **ppStmt,
227750 char *zSql
227751){
227752 if( p->rc==SQLITE_OK0 ){
227753 if( zSql ){
227754 p->rc = sqlite3_prepare_v3(p->pConfig->db, zSql, -1,
227755 SQLITE_PREPARE_PERSISTENT0x01|SQLITE_PREPARE_NO_VTAB0x04,
227756 ppStmt, 0);
227757 }else{
227758 p->rc = SQLITE_NOMEM7;
227759 }
227760 }
227761 sqlite3_free(zSql);
227762 return p->rc;
227763}
227764
227765
227766/*
227767** INSERT OR REPLACE a record into the %_data table.
227768*/
227769static void fts5DataWrite(Fts5Index *p, i64 iRowid, const u8 *pData, int nData){
227770 if( p->rc!=SQLITE_OK0 ) return;
227771
227772 if( p->pWriter==0 ){
227773 Fts5Config *pConfig = p->pConfig;
227774 fts5IndexPrepareStmt(p, &p->pWriter, sqlite3_mprintf(
227775 "REPLACE INTO '%q'.'%q_data'(id, block) VALUES(?,?)",
227776 pConfig->zDb, pConfig->zName
227777 ));
227778 if( p->rc ) return;
227779 }
227780
227781 sqlite3_bind_int64(p->pWriter, 1, iRowid);
227782 sqlite3_bind_blob(p->pWriter, 2, pData, nData, SQLITE_STATIC((sqlite3_destructor_type)0));
227783 sqlite3_step(p->pWriter);
227784 p->rc = sqlite3_reset(p->pWriter);
227785 sqlite3_bind_null(p->pWriter, 2);
227786}
227787
227788/*
227789** Execute the following SQL:
227790**
227791** DELETE FROM %_data WHERE id BETWEEN $iFirst AND $iLast
227792*/
227793static void fts5DataDelete(Fts5Index *p, i64 iFirst, i64 iLast){
227794 if( p->rc!=SQLITE_OK0 ) return;
227795
227796 if( p->pDeleter==0 ){
227797 Fts5Config *pConfig = p->pConfig;
227798 char *zSql = sqlite3_mprintf(
227799 "DELETE FROM '%q'.'%q_data' WHERE id>=? AND id<=?",
227800 pConfig->zDb, pConfig->zName
227801 );
227802 if( fts5IndexPrepareStmt(p, &p->pDeleter, zSql) ) return;
227803 }
227804
227805 sqlite3_bind_int64(p->pDeleter, 1, iFirst);
227806 sqlite3_bind_int64(p->pDeleter, 2, iLast);
227807 sqlite3_step(p->pDeleter);
227808 p->rc = sqlite3_reset(p->pDeleter);
227809}
227810
227811/*
227812** Remove all records associated with segment iSegid.
227813*/
227814static void fts5DataRemoveSegment(Fts5Index *p, int iSegid){
227815 i64 iFirst = FTS5_SEGMENT_ROWID(iSegid, 0)( ((i64)(iSegid) << (31 +5 +1)) + ((i64)(0) << (31
+ 5)) + ((i64)(0) << (31)) + ((i64)(0)) )
;
227816 i64 iLast = FTS5_SEGMENT_ROWID(iSegid+1, 0)( ((i64)(iSegid+1) << (31 +5 +1)) + ((i64)(0) << (
31 + 5)) + ((i64)(0) << (31)) + ((i64)(0)) )
-1;
227817 fts5DataDelete(p, iFirst, iLast);
227818 if( p->pIdxDeleter==0 ){
227819 Fts5Config *pConfig = p->pConfig;
227820 fts5IndexPrepareStmt(p, &p->pIdxDeleter, sqlite3_mprintf(
227821 "DELETE FROM '%q'.'%q_idx' WHERE segid=?",
227822 pConfig->zDb, pConfig->zName
227823 ));
227824 }
227825 if( p->rc==SQLITE_OK0 ){
227826 sqlite3_bind_int(p->pIdxDeleter, 1, iSegid);
227827 sqlite3_step(p->pIdxDeleter);
227828 p->rc = sqlite3_reset(p->pIdxDeleter);
227829 }
227830}
227831
227832/*
227833** Release a reference to an Fts5Structure object returned by an earlier
227834** call to fts5StructureRead() or fts5StructureDecode().
227835*/
227836static void fts5StructureRelease(Fts5Structure *pStruct){
227837 if( pStruct && 0>=(--pStruct->nRef) ){
227838 int i;
227839 assert( pStruct->nRef==0 )((void) (0));
227840 for(i=0; i<pStruct->nLevel; i++){
227841 sqlite3_free(pStruct->aLevel[i].aSeg);
227842 }
227843 sqlite3_free(pStruct);
227844 }
227845}
227846
227847static void fts5StructureRef(Fts5Structure *pStruct){
227848 pStruct->nRef++;
227849}
227850
227851static void *sqlite3Fts5StructureRef(Fts5Index *p){
227852 fts5StructureRef(p->pStruct);
227853 return (void*)p->pStruct;
227854}
227855static void sqlite3Fts5StructureRelease(void *p){
227856 if( p ){
227857 fts5StructureRelease((Fts5Structure*)p);
227858 }
227859}
227860static int sqlite3Fts5StructureTest(Fts5Index *p, void *pStruct){
227861 if( p->pStruct!=(Fts5Structure*)pStruct ){
227862 return SQLITE_ABORT4;
227863 }
227864 return SQLITE_OK0;
227865}
227866
227867/*
227868** Ensure that structure object (*pp) is writable.
227869**
227870** This function is a no-op if (*pRc) is not SQLITE_OK when it is called. If
227871** an error occurs, (*pRc) is set to an SQLite error code before returning.
227872*/
227873static void fts5StructureMakeWritable(int *pRc, Fts5Structure **pp){
227874 Fts5Structure *p = *pp;
227875 if( *pRc==SQLITE_OK0 && p->nRef>1 ){
227876 i64 nByte = sizeof(Fts5Structure)+(p->nLevel-1)*sizeof(Fts5StructureLevel);
227877 Fts5Structure *pNew;
227878 pNew = (Fts5Structure*)sqlite3Fts5MallocZero(pRc, nByte);
227879 if( pNew ){
227880 int i;
227881 memcpy(pNew, p, nByte);
227882 for(i=0; i<p->nLevel; i++) pNew->aLevel[i].aSeg = 0;
227883 for(i=0; i<p->nLevel; i++){
227884 Fts5StructureLevel *pLvl = &pNew->aLevel[i];
227885 nByte = sizeof(Fts5StructureSegment) * pNew->aLevel[i].nSeg;
227886 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(pRc, nByte);
227887 if( pLvl->aSeg==0 ){
227888 for(i=0; i<p->nLevel; i++){
227889 sqlite3_free(pNew->aLevel[i].aSeg);
227890 }
227891 sqlite3_free(pNew);
227892 return;
227893 }
227894 memcpy(pLvl->aSeg, p->aLevel[i].aSeg, nByte);
227895 }
227896 p->nRef--;
227897 pNew->nRef = 1;
227898 }
227899 *pp = pNew;
227900 }
227901}
227902
227903/*
227904** Deserialize and return the structure record currently stored in serialized
227905** form within buffer pData/nData.
227906**
227907** The Fts5Structure.aLevel[] and each Fts5StructureLevel.aSeg[] array
227908** are over-allocated by one slot. This allows the structure contents
227909** to be more easily edited.
227910**
227911** If an error occurs, *ppOut is set to NULL and an SQLite error code
227912** returned. Otherwise, *ppOut is set to point to the new object and
227913** SQLITE_OK returned.
227914*/
227915static int fts5StructureDecode(
227916 const u8 *pData, /* Buffer containing serialized structure */
227917 int nData, /* Size of buffer pData in bytes */
227918 int *piCookie, /* Configuration cookie value */
227919 Fts5Structure **ppOut /* OUT: Deserialized object */
227920){
227921 int rc = SQLITE_OK0;
227922 int i = 0;
227923 int iLvl;
227924 int nLevel = 0;
227925 int nSegment = 0;
227926 sqlite3_int64 nByte; /* Bytes of space to allocate at pRet */
227927 Fts5Structure *pRet = 0; /* Structure object to return */
227928
227929 /* Grab the cookie value */
227930 if( piCookie ) *piCookie = sqlite3Fts5Get32(pData);
227931 i = 4;
227932
227933 /* Read the total number of levels and segments from the start of the
227934 ** structure record. */
227935 i += fts5GetVarint32(&pData[i], nLevel)sqlite3Fts5GetVarint32(&pData[i],(u32*)&nLevel);
227936 i += fts5GetVarint32(&pData[i], nSegment)sqlite3Fts5GetVarint32(&pData[i],(u32*)&nSegment);
227937 if( nLevel>FTS5_MAX_SEGMENT2000 || nLevel<0
227938 || nSegment>FTS5_MAX_SEGMENT2000 || nSegment<0
227939 ){
227940 return FTS5_CORRUPT(11 | (1<<8));
227941 }
227942 nByte = (
227943 sizeof(Fts5Structure) + /* Main structure */
227944 sizeof(Fts5StructureLevel) * (nLevel-1) /* aLevel[] array */
227945 );
227946 pRet = (Fts5Structure*)sqlite3Fts5MallocZero(&rc, nByte);
227947
227948 if( pRet ){
227949 pRet->nRef = 1;
227950 pRet->nLevel = nLevel;
227951 pRet->nSegment = nSegment;
227952 i += sqlite3Fts5GetVarint(&pData[i], &pRet->nWriteCounter);
227953
227954 for(iLvl=0; rc==SQLITE_OK0 && iLvl<nLevel; iLvl++){
227955 Fts5StructureLevel *pLvl = &pRet->aLevel[iLvl];
227956 int nTotal = 0;
227957 int iSeg;
227958
227959 if( i>=nData ){
227960 rc = FTS5_CORRUPT(11 | (1<<8));
227961 }else{
227962 i += fts5GetVarint32(&pData[i], pLvl->nMerge)sqlite3Fts5GetVarint32(&pData[i],(u32*)&pLvl->nMerge
)
;
227963 i += fts5GetVarint32(&pData[i], nTotal)sqlite3Fts5GetVarint32(&pData[i],(u32*)&nTotal);
227964 if( nTotal<pLvl->nMerge ) rc = FTS5_CORRUPT(11 | (1<<8));
227965 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&rc,
227966 nTotal * sizeof(Fts5StructureSegment)
227967 );
227968 nSegment -= nTotal;
227969 }
227970
227971 if( rc==SQLITE_OK0 ){
227972 pLvl->nSeg = nTotal;
227973 for(iSeg=0; iSeg<nTotal; iSeg++){
227974 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
227975 if( i>=nData ){
227976 rc = FTS5_CORRUPT(11 | (1<<8));
227977 break;
227978 }
227979 i += fts5GetVarint32(&pData[i], pSeg->iSegid)sqlite3Fts5GetVarint32(&pData[i],(u32*)&pSeg->iSegid
)
;
227980 i += fts5GetVarint32(&pData[i], pSeg->pgnoFirst)sqlite3Fts5GetVarint32(&pData[i],(u32*)&pSeg->pgnoFirst
)
;
227981 i += fts5GetVarint32(&pData[i], pSeg->pgnoLast)sqlite3Fts5GetVarint32(&pData[i],(u32*)&pSeg->pgnoLast
)
;
227982 if( pSeg->pgnoLast<pSeg->pgnoFirst ){
227983 rc = FTS5_CORRUPT(11 | (1<<8));
227984 break;
227985 }
227986 }
227987 if( iLvl>0 && pLvl[-1].nMerge && nTotal==0 ) rc = FTS5_CORRUPT(11 | (1<<8));
227988 if( iLvl==nLevel-1 && pLvl->nMerge ) rc = FTS5_CORRUPT(11 | (1<<8));
227989 }
227990 }
227991 if( nSegment!=0 && rc==SQLITE_OK0 ) rc = FTS5_CORRUPT(11 | (1<<8));
227992
227993 if( rc!=SQLITE_OK0 ){
227994 fts5StructureRelease(pRet);
227995 pRet = 0;
227996 }
227997 }
227998
227999 *ppOut = pRet;
228000 return rc;
228001}
228002
228003/*
228004** Add a level to the Fts5Structure.aLevel[] array of structure object
228005** (*ppStruct).
228006*/
228007static void fts5StructureAddLevel(int *pRc, Fts5Structure **ppStruct){
228008 fts5StructureMakeWritable(pRc, ppStruct);
228009 if( *pRc==SQLITE_OK0 ){
228010 Fts5Structure *pStruct = *ppStruct;
228011 int nLevel = pStruct->nLevel;
228012 sqlite3_int64 nByte = (
228013 sizeof(Fts5Structure) + /* Main structure */
228014 sizeof(Fts5StructureLevel) * (nLevel+1) /* aLevel[] array */
228015 );
228016
228017 pStruct = sqlite3_realloc64(pStruct, nByte);
228018 if( pStruct ){
228019 memset(&pStruct->aLevel[nLevel], 0, sizeof(Fts5StructureLevel));
228020 pStruct->nLevel++;
228021 *ppStruct = pStruct;
228022 }else{
228023 *pRc = SQLITE_NOMEM7;
228024 }
228025 }
228026}
228027
228028/*
228029** Extend level iLvl so that there is room for at least nExtra more
228030** segments.
228031*/
228032static void fts5StructureExtendLevel(
228033 int *pRc,
228034 Fts5Structure *pStruct,
228035 int iLvl,
228036 int nExtra,
228037 int bInsert
228038){
228039 if( *pRc==SQLITE_OK0 ){
228040 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
228041 Fts5StructureSegment *aNew;
228042 sqlite3_int64 nByte;
228043
228044 nByte = (pLvl->nSeg + nExtra) * sizeof(Fts5StructureSegment);
228045 aNew = sqlite3_realloc64(pLvl->aSeg, nByte);
228046 if( aNew ){
228047 if( bInsert==0 ){
228048 memset(&aNew[pLvl->nSeg], 0, sizeof(Fts5StructureSegment) * nExtra);
228049 }else{
228050 int nMove = pLvl->nSeg * sizeof(Fts5StructureSegment);
228051 memmove(&aNew[nExtra], aNew, nMove);
228052 memset(aNew, 0, sizeof(Fts5StructureSegment) * nExtra);
228053 }
228054 pLvl->aSeg = aNew;
228055 }else{
228056 *pRc = SQLITE_NOMEM7;
228057 }
228058 }
228059}
228060
228061static Fts5Structure *fts5StructureReadUncached(Fts5Index *p){
228062 Fts5Structure *pRet = 0;
228063 Fts5Config *pConfig = p->pConfig;
228064 int iCookie; /* Configuration cookie */
228065 Fts5Data *pData;
228066
228067 pData = fts5DataRead(p, FTS5_STRUCTURE_ROWID10);
228068 if( p->rc==SQLITE_OK0 ){
228069 /* TODO: Do we need this if the leaf-index is appended? Probably... */
228070 memset(&pData->p[pData->nn], 0, FTS5_DATA_PADDING20);
228071 p->rc = fts5StructureDecode(pData->p, pData->nn, &iCookie, &pRet);
228072 if( p->rc==SQLITE_OK0 && (pConfig->pgsz==0 || pConfig->iCookie!=iCookie) ){
228073 p->rc = sqlite3Fts5ConfigLoad(pConfig, iCookie);
228074 }
228075 fts5DataRelease(pData);
228076 if( p->rc!=SQLITE_OK0 ){
228077 fts5StructureRelease(pRet);
228078 pRet = 0;
228079 }
228080 }
228081
228082 return pRet;
228083}
228084
228085static i64 fts5IndexDataVersion(Fts5Index *p){
228086 i64 iVersion = 0;
228087
228088 if( p->rc==SQLITE_OK0 ){
228089 if( p->pDataVersion==0 ){
228090 p->rc = fts5IndexPrepareStmt(p, &p->pDataVersion,
228091 sqlite3_mprintf("PRAGMA %Q.data_version", p->pConfig->zDb)
228092 );
228093 if( p->rc ) return 0;
228094 }
228095
228096 if( SQLITE_ROW100==sqlite3_step(p->pDataVersion) ){
228097 iVersion = sqlite3_column_int64(p->pDataVersion, 0);
228098 }
228099 p->rc = sqlite3_reset(p->pDataVersion);
228100 }
228101
228102 return iVersion;
228103}
228104
228105/*
228106** Read, deserialize and return the structure record.
228107**
228108** The Fts5Structure.aLevel[] and each Fts5StructureLevel.aSeg[] array
228109** are over-allocated as described for function fts5StructureDecode()
228110** above.
228111**
228112** If an error occurs, NULL is returned and an error code left in the
228113** Fts5Index handle. If an error has already occurred when this function
228114** is called, it is a no-op.
228115*/
228116static Fts5Structure *fts5StructureRead(Fts5Index *p){
228117
228118 if( p->pStruct==0 ){
228119 p->iStructVersion = fts5IndexDataVersion(p);
228120 if( p->rc==SQLITE_OK0 ){
228121 p->pStruct = fts5StructureReadUncached(p);
228122 }
228123 }
228124
228125#if 0
228126 else{
228127 Fts5Structure *pTest = fts5StructureReadUncached(p);
228128 if( pTest ){
228129 int i, j;
228130 assert_nc( p->pStruct->nSegment==pTest->nSegment )((void) (0));
228131 assert_nc( p->pStruct->nLevel==pTest->nLevel )((void) (0));
228132 for(i=0; i<pTest->nLevel; i++){
228133 assert_nc( p->pStruct->aLevel[i].nMerge==pTest->aLevel[i].nMerge )((void) (0));
228134 assert_nc( p->pStruct->aLevel[i].nSeg==pTest->aLevel[i].nSeg )((void) (0));
228135 for(j=0; j<pTest->aLevel[i].nSeg; j++){
228136 Fts5StructureSegment *p1 = &pTest->aLevel[i].aSeg[j];
228137 Fts5StructureSegment *p2 = &p->pStruct->aLevel[i].aSeg[j];
228138 assert_nc( p1->iSegid==p2->iSegid )((void) (0));
228139 assert_nc( p1->pgnoFirst==p2->pgnoFirst )((void) (0));
228140 assert_nc( p1->pgnoLast==p2->pgnoLast )((void) (0));
228141 }
228142 }
228143 fts5StructureRelease(pTest);
228144 }
228145 }
228146#endif
228147
228148 if( p->rc!=SQLITE_OK0 ) return 0;
228149 assert( p->iStructVersion!=0 )((void) (0));
228150 assert( p->pStruct!=0 )((void) (0));
228151 fts5StructureRef(p->pStruct);
228152 return p->pStruct;
228153}
228154
228155static void fts5StructureInvalidate(Fts5Index *p){
228156 if( p->pStruct ){
228157 fts5StructureRelease(p->pStruct);
228158 p->pStruct = 0;
228159 }
228160}
228161
228162/*
228163** Return the total number of segments in index structure pStruct. This
228164** function is only ever used as part of assert() conditions.
228165*/
228166#ifdef SQLITE_DEBUG
228167static int fts5StructureCountSegments(Fts5Structure *pStruct){
228168 int nSegment = 0; /* Total number of segments */
228169 if( pStruct ){
228170 int iLvl; /* Used to iterate through levels */
228171 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
228172 nSegment += pStruct->aLevel[iLvl].nSeg;
228173 }
228174 }
228175
228176 return nSegment;
228177}
228178#endif
228179
228180#define fts5BufferSafeAppendBlob(pBuf, pBlob, nBlob){ ((void) (0)); memcpy(&(pBuf)->p[(pBuf)->n], pBlob
, nBlob); (pBuf)->n += nBlob; }
{ \
228181 assert( (pBuf)->nSpace>=((pBuf)->n+nBlob) )((void) (0)); \
228182 memcpy(&(pBuf)->p[(pBuf)->n], pBlob, nBlob); \
228183 (pBuf)->n += nBlob; \
228184}
228185
228186#define fts5BufferSafeAppendVarint(pBuf, iVal){ (pBuf)->n += sqlite3Fts5PutVarint(&(pBuf)->p[(pBuf
)->n], (iVal)); ((void) (0)); }
{ \
228187 (pBuf)->n += sqlite3Fts5PutVarint(&(pBuf)->p[(pBuf)->n], (iVal)); \
228188 assert( (pBuf)->nSpace>=(pBuf)->n )((void) (0)); \
228189}
228190
228191
228192/*
228193** Serialize and store the "structure" record.
228194**
228195** If an error occurs, leave an error code in the Fts5Index object. If an
228196** error has already occurred, this function is a no-op.
228197*/
228198static void fts5StructureWrite(Fts5Index *p, Fts5Structure *pStruct){
228199 if( p->rc==SQLITE_OK0 ){
228200 Fts5Buffer buf; /* Buffer to serialize record into */
228201 int iLvl; /* Used to iterate through levels */
228202 int iCookie; /* Cookie value to store */
228203
228204 assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) )((void) (0));
228205 memset(&buf, 0, sizeof(Fts5Buffer));
228206
228207 /* Append the current configuration cookie */
228208 iCookie = p->pConfig->iCookie;
228209 if( iCookie<0 ) iCookie = 0;
228210
228211 if( 0==sqlite3Fts5BufferSize(&p->rc, &buf, 4+9+9+9) ){
228212 sqlite3Fts5Put32(buf.p, iCookie);
228213 buf.n = 4;
228214 fts5BufferSafeAppendVarint(&buf, pStruct->nLevel){ (&buf)->n += sqlite3Fts5PutVarint(&(&buf)->
p[(&buf)->n], (pStruct->nLevel)); ((void) (0)); }
;
228215 fts5BufferSafeAppendVarint(&buf, pStruct->nSegment){ (&buf)->n += sqlite3Fts5PutVarint(&(&buf)->
p[(&buf)->n], (pStruct->nSegment)); ((void) (0)); }
;
228216 fts5BufferSafeAppendVarint(&buf, (i64)pStruct->nWriteCounter){ (&buf)->n += sqlite3Fts5PutVarint(&(&buf)->
p[(&buf)->n], ((i64)pStruct->nWriteCounter)); ((void
) (0)); }
;
228217 }
228218
228219 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
228220 int iSeg; /* Used to iterate through segments */
228221 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
228222 fts5BufferAppendVarint(&p->rc, &buf, pLvl->nMerge)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,pLvl->
nMerge)
;
228223 fts5BufferAppendVarint(&p->rc, &buf, pLvl->nSeg)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,pLvl->
nSeg)
;
228224 assert( pLvl->nMerge<=pLvl->nSeg )((void) (0));
228225
228226 for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
228227 fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].iSegid)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,pLvl->
aSeg[iSeg].iSegid)
;
228228 fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].pgnoFirst)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,pLvl->
aSeg[iSeg].pgnoFirst)
;
228229 fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].pgnoLast)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,pLvl->
aSeg[iSeg].pgnoLast)
;
228230 }
228231 }
228232
228233 fts5DataWrite(p, FTS5_STRUCTURE_ROWID10, buf.p, buf.n);
228234 fts5BufferFree(&buf)sqlite3Fts5BufferFree(&buf);
228235 }
228236}
228237
228238#if 0
228239static void fts5DebugStructure(int*,Fts5Buffer*,Fts5Structure*);
228240static void fts5PrintStructure(const char *zCaption, Fts5Structure *pStruct){
228241 int rc = SQLITE_OK0;
228242 Fts5Buffer buf;
228243 memset(&buf, 0, sizeof(buf));
228244 fts5DebugStructure(&rc, &buf, pStruct);
228245 fprintf(stdoutstdout, "%s: %s\n", zCaption, buf.p);
228246 fflush(stdoutstdout);
228247 fts5BufferFree(&buf)sqlite3Fts5BufferFree(&buf);
228248}
228249#else
228250# define fts5PrintStructure(x,y)
228251#endif
228252
228253static int fts5SegmentSize(Fts5StructureSegment *pSeg){
228254 return 1 + pSeg->pgnoLast - pSeg->pgnoFirst;
228255}
228256
228257/*
228258** Return a copy of index structure pStruct. Except, promote as many
228259** segments as possible to level iPromote. If an OOM occurs, NULL is
228260** returned.
228261*/
228262static void fts5StructurePromoteTo(
228263 Fts5Index *p,
228264 int iPromote,
228265 int szPromote,
228266 Fts5Structure *pStruct
228267){
228268 int il, is;
228269 Fts5StructureLevel *pOut = &pStruct->aLevel[iPromote];
228270
228271 if( pOut->nMerge==0 ){
228272 for(il=iPromote+1; il<pStruct->nLevel; il++){
228273 Fts5StructureLevel *pLvl = &pStruct->aLevel[il];
228274 if( pLvl->nMerge ) return;
228275 for(is=pLvl->nSeg-1; is>=0; is--){
228276 int sz = fts5SegmentSize(&pLvl->aSeg[is]);
228277 if( sz>szPromote ) return;
228278 fts5StructureExtendLevel(&p->rc, pStruct, iPromote, 1, 1);
228279 if( p->rc ) return;
228280 memcpy(pOut->aSeg, &pLvl->aSeg[is], sizeof(Fts5StructureSegment));
228281 pOut->nSeg++;
228282 pLvl->nSeg--;
228283 }
228284 }
228285 }
228286}
228287
228288/*
228289** A new segment has just been written to level iLvl of index structure
228290** pStruct. This function determines if any segments should be promoted
228291** as a result. Segments are promoted in two scenarios:
228292**
228293** a) If the segment just written is smaller than one or more segments
228294** within the previous populated level, it is promoted to the previous
228295** populated level.
228296**
228297** b) If the segment just written is larger than the newest segment on
228298** the next populated level, then that segment, and any other adjacent
228299** segments that are also smaller than the one just written, are
228300** promoted.
228301**
228302** If one or more segments are promoted, the structure object is updated
228303** to reflect this.
228304*/
228305static void fts5StructurePromote(
228306 Fts5Index *p, /* FTS5 backend object */
228307 int iLvl, /* Index level just updated */
228308 Fts5Structure *pStruct /* Index structure */
228309){
228310 if( p->rc==SQLITE_OK0 ){
228311 int iTst;
228312 int iPromote = -1;
228313 int szPromote = 0; /* Promote anything this size or smaller */
228314 Fts5StructureSegment *pSeg; /* Segment just written */
228315 int szSeg; /* Size of segment just written */
228316 int nSeg = pStruct->aLevel[iLvl].nSeg;
228317
228318 if( nSeg==0 ) return;
228319 pSeg = &pStruct->aLevel[iLvl].aSeg[pStruct->aLevel[iLvl].nSeg-1];
228320 szSeg = (1 + pSeg->pgnoLast - pSeg->pgnoFirst);
228321
228322 /* Check for condition (a) */
228323 for(iTst=iLvl-1; iTst>=0 && pStruct->aLevel[iTst].nSeg==0; iTst--);
228324 if( iTst>=0 ){
228325 int i;
228326 int szMax = 0;
228327 Fts5StructureLevel *pTst = &pStruct->aLevel[iTst];
228328 assert( pTst->nMerge==0 )((void) (0));
228329 for(i=0; i<pTst->nSeg; i++){
228330 int sz = pTst->aSeg[i].pgnoLast - pTst->aSeg[i].pgnoFirst + 1;
228331 if( sz>szMax ) szMax = sz;
228332 }
228333 if( szMax>=szSeg ){
228334 /* Condition (a) is true. Promote the newest segment on level
228335 ** iLvl to level iTst. */
228336 iPromote = iTst;
228337 szPromote = szMax;
228338 }
228339 }
228340
228341 /* If condition (a) is not met, assume (b) is true. StructurePromoteTo()
228342 ** is a no-op if it is not. */
228343 if( iPromote<0 ){
228344 iPromote = iLvl;
228345 szPromote = szSeg;
228346 }
228347 fts5StructurePromoteTo(p, iPromote, szPromote, pStruct);
228348 }
228349}
228350
228351
228352/*
228353** Advance the iterator passed as the only argument. If the end of the
228354** doclist-index page is reached, return non-zero.
228355*/
228356static int fts5DlidxLvlNext(Fts5DlidxLvl *pLvl){
228357 Fts5Data *pData = pLvl->pData;
228358
228359 if( pLvl->iOff==0 ){
228360 assert( pLvl->bEof==0 )((void) (0));
228361 pLvl->iOff = 1;
228362 pLvl->iOff += fts5GetVarint32(&pData->p[1], pLvl->iLeafPgno)sqlite3Fts5GetVarint32(&pData->p[1],(u32*)&pLvl->
iLeafPgno)
;
228363 pLvl->iOff += fts5GetVarintsqlite3Fts5GetVarint(&pData->p[pLvl->iOff], (u64*)&pLvl->iRowid);
228364 pLvl->iFirstOff = pLvl->iOff;
228365 }else{
228366 int iOff;
228367 for(iOff=pLvl->iOff; iOff<pData->nn; iOff++){
228368 if( pData->p[iOff] ) break;
228369 }
228370
228371 if( iOff<pData->nn ){
228372 i64 iVal;
228373 pLvl->iLeafPgno += (iOff - pLvl->iOff) + 1;
228374 iOff += fts5GetVarintsqlite3Fts5GetVarint(&pData->p[iOff], (u64*)&iVal);
228375 pLvl->iRowid += iVal;
228376 pLvl->iOff = iOff;
228377 }else{
228378 pLvl->bEof = 1;
228379 }
228380 }
228381
228382 return pLvl->bEof;
228383}
228384
228385/*
228386** Advance the iterator passed as the only argument.
228387*/
228388static int fts5DlidxIterNextR(Fts5Index *p, Fts5DlidxIter *pIter, int iLvl){
228389 Fts5DlidxLvl *pLvl = &pIter->aLvl[iLvl];
228390
228391 assert( iLvl<pIter->nLvl )((void) (0));
228392 if( fts5DlidxLvlNext(pLvl) ){
228393 if( (iLvl+1) < pIter->nLvl ){
228394 fts5DlidxIterNextR(p, pIter, iLvl+1);
228395 if( pLvl[1].bEof==0 ){
228396 fts5DataRelease(pLvl->pData);
228397 memset(pLvl, 0, sizeof(Fts5DlidxLvl));
228398 pLvl->pData = fts5DataRead(p,
228399 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
)) )
228400 );
228401 if( pLvl->pData ) fts5DlidxLvlNext(pLvl);
228402 }
228403 }
228404 }
228405
228406 return pIter->aLvl[0].bEof;
228407}
228408static int fts5DlidxIterNext(Fts5Index *p, Fts5DlidxIter *pIter){
228409 return fts5DlidxIterNextR(p, pIter, 0);
228410}
228411
228412/*
228413** The iterator passed as the first argument has the following fields set
228414** as follows. This function sets up the rest of the iterator so that it
228415** points to the first rowid in the doclist-index.
228416**
228417** pData:
228418** pointer to doclist-index record,
228419**
228420** When this function is called pIter->iLeafPgno is the page number the
228421** doclist is associated with (the one featuring the term).
228422*/
228423static int fts5DlidxIterFirst(Fts5DlidxIter *pIter){
228424 int i;
228425 for(i=0; i<pIter->nLvl; i++){
228426 fts5DlidxLvlNext(&pIter->aLvl[i]);
228427 }
228428 return pIter->aLvl[0].bEof;
228429}
228430
228431
228432static int fts5DlidxIterEof(Fts5Index *p, Fts5DlidxIter *pIter){
228433 return p->rc!=SQLITE_OK0 || pIter->aLvl[0].bEof;
228434}
228435
228436static void fts5DlidxIterLast(Fts5Index *p, Fts5DlidxIter *pIter){
228437 int i;
228438
228439 /* Advance each level to the last entry on the last page */
228440 for(i=pIter->nLvl-1; p->rc==SQLITE_OK0 && i>=0; i--){
228441 Fts5DlidxLvl *pLvl = &pIter->aLvl[i];
228442 while( fts5DlidxLvlNext(pLvl)==0 );
228443 pLvl->bEof = 0;
228444
228445 if( i>0 ){
228446 Fts5DlidxLvl *pChild = &pLvl[-1];
228447 fts5DataRelease(pChild->pData);
228448 memset(pChild, 0, sizeof(Fts5DlidxLvl));
228449 pChild->pData = fts5DataRead(p,
228450 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
)) )
228451 );
228452 }
228453 }
228454}
228455
228456/*
228457** Move the iterator passed as the only argument to the previous entry.
228458*/
228459static int fts5DlidxLvlPrev(Fts5DlidxLvl *pLvl){
228460 int iOff = pLvl->iOff;
228461
228462 assert( pLvl->bEof==0 )((void) (0));
228463 if( iOff<=pLvl->iFirstOff ){
228464 pLvl->bEof = 1;
228465 }else{
228466 u8 *a = pLvl->pData->p;
228467 i64 iVal;
228468 int iLimit;
228469 int ii;
228470 int nZero = 0;
228471
228472 /* Currently iOff points to the first byte of a varint. This block
228473 ** decrements iOff until it points to the first byte of the previous
228474 ** varint. Taking care not to read any memory locations that occur
228475 ** before the buffer in memory. */
228476 iLimit = (iOff>9 ? iOff-9 : 0);
228477 for(iOff--; iOff>iLimit; iOff--){
228478 if( (a[iOff-1] & 0x80)==0 ) break;
228479 }
228480
228481 fts5GetVarintsqlite3Fts5GetVarint(&a[iOff], (u64*)&iVal);
228482 pLvl->iRowid -= iVal;
228483 pLvl->iLeafPgno--;
228484
228485 /* Skip backwards past any 0x00 varints. */
228486 for(ii=iOff-1; ii>=pLvl->iFirstOff && a[ii]==0x00; ii--){
228487 nZero++;
228488 }
228489 if( ii>=pLvl->iFirstOff && (a[ii] & 0x80) ){
228490 /* The byte immediately before the last 0x00 byte has the 0x80 bit
228491 ** set. So the last 0x00 is only a varint 0 if there are 8 more 0x80
228492 ** bytes before a[ii]. */
228493 int bZero = 0; /* True if last 0x00 counts */
228494 if( (ii-8)>=pLvl->iFirstOff ){
228495 int j;
228496 for(j=1; j<=8 && (a[ii-j] & 0x80); j++);
228497 bZero = (j>8);
228498 }
228499 if( bZero==0 ) nZero--;
228500 }
228501 pLvl->iLeafPgno -= nZero;
228502 pLvl->iOff = iOff - nZero;
228503 }
228504
228505 return pLvl->bEof;
228506}
228507
228508static int fts5DlidxIterPrevR(Fts5Index *p, Fts5DlidxIter *pIter, int iLvl){
228509 Fts5DlidxLvl *pLvl = &pIter->aLvl[iLvl];
228510
228511 assert( iLvl<pIter->nLvl )((void) (0));
228512 if( fts5DlidxLvlPrev(pLvl) ){
228513 if( (iLvl+1) < pIter->nLvl ){
228514 fts5DlidxIterPrevR(p, pIter, iLvl+1);
228515 if( pLvl[1].bEof==0 ){
228516 fts5DataRelease(pLvl->pData);
228517 memset(pLvl, 0, sizeof(Fts5DlidxLvl));
228518 pLvl->pData = fts5DataRead(p,
228519 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
)) )
228520 );
228521 if( pLvl->pData ){
228522 while( fts5DlidxLvlNext(pLvl)==0 );
228523 pLvl->bEof = 0;
228524 }
228525 }
228526 }
228527 }
228528
228529 return pIter->aLvl[0].bEof;
228530}
228531static int fts5DlidxIterPrev(Fts5Index *p, Fts5DlidxIter *pIter){
228532 return fts5DlidxIterPrevR(p, pIter, 0);
228533}
228534
228535/*
228536** Free a doclist-index iterator object allocated by fts5DlidxIterInit().
228537*/
228538static void fts5DlidxIterFree(Fts5DlidxIter *pIter){
228539 if( pIter ){
228540 int i;
228541 for(i=0; i<pIter->nLvl; i++){
228542 fts5DataRelease(pIter->aLvl[i].pData);
228543 }
228544 sqlite3_free(pIter);
228545 }
228546}
228547
228548static Fts5DlidxIter *fts5DlidxIterInit(
228549 Fts5Index *p, /* Fts5 Backend to iterate within */
228550 int bRev, /* True for ORDER BY ASC */
228551 int iSegid, /* Segment id */
228552 int iLeafPg /* Leaf page number to load dlidx for */
228553){
228554 Fts5DlidxIter *pIter = 0;
228555 int i;
228556 int bDone = 0;
228557
228558 for(i=0; p->rc==SQLITE_OK0 && bDone==0; i++){
228559 sqlite3_int64 nByte = sizeof(Fts5DlidxIter) + i * sizeof(Fts5DlidxLvl);
228560 Fts5DlidxIter *pNew;
228561
228562 pNew = (Fts5DlidxIter*)sqlite3_realloc64(pIter, nByte);
228563 if( pNew==0 ){
228564 p->rc = SQLITE_NOMEM7;
228565 }else{
228566 i64 iRowid = FTS5_DLIDX_ROWID(iSegid, i, iLeafPg)( ((i64)(iSegid) << (31 +5 +1)) + ((i64)(1) << (31
+ 5)) + ((i64)(i) << (31)) + ((i64)(iLeafPg)) )
;
228567 Fts5DlidxLvl *pLvl = &pNew->aLvl[i];
228568 pIter = pNew;
228569 memset(pLvl, 0, sizeof(Fts5DlidxLvl));
228570 pLvl->pData = fts5DataRead(p, iRowid);
228571 if( pLvl->pData && (pLvl->pData->p[0] & 0x0001)==0 ){
228572 bDone = 1;
228573 }
228574 pIter->nLvl = i+1;
228575 }
228576 }
228577
228578 if( p->rc==SQLITE_OK0 ){
228579 pIter->iSegid = iSegid;
228580 if( bRev==0 ){
228581 fts5DlidxIterFirst(pIter);
228582 }else{
228583 fts5DlidxIterLast(p, pIter);
228584 }
228585 }
228586
228587 if( p->rc!=SQLITE_OK0 ){
228588 fts5DlidxIterFree(pIter);
228589 pIter = 0;
228590 }
228591
228592 return pIter;
228593}
228594
228595static i64 fts5DlidxIterRowid(Fts5DlidxIter *pIter){
228596 return pIter->aLvl[0].iRowid;
228597}
228598static int fts5DlidxIterPgno(Fts5DlidxIter *pIter){
228599 return pIter->aLvl[0].iLeafPgno;
228600}
228601
228602/*
228603** Load the next leaf page into the segment iterator.
228604*/
228605static void fts5SegIterNextPage(
228606 Fts5Index *p, /* FTS5 backend object */
228607 Fts5SegIter *pIter /* Iterator to advance to next page */
228608){
228609 Fts5Data *pLeaf;
228610 Fts5StructureSegment *pSeg = pIter->pSeg;
228611 fts5DataRelease(pIter->pLeaf);
228612 pIter->iLeafPgno++;
228613 if( pIter->pNextLeaf ){
228614 pIter->pLeaf = pIter->pNextLeaf;
228615 pIter->pNextLeaf = 0;
228616 }else if( pIter->iLeafPgno<=pSeg->pgnoLast ){
228617 pIter->pLeaf = fts5LeafRead(p,
228618 FTS5_SEGMENT_ROWID(pSeg->iSegid, pIter->iLeafPgno)( ((i64)(pSeg->iSegid) << (31 +5 +1)) + ((i64)(0) <<
(31 + 5)) + ((i64)(0) << (31)) + ((i64)(pIter->iLeafPgno
)) )
228619 );
228620 }else{
228621 pIter->pLeaf = 0;
228622 }
228623 pLeaf = pIter->pLeaf;
228624
228625 if( pLeaf ){
228626 pIter->iPgidxOff = pLeaf->szLeaf;
228627 if( fts5LeafIsTermless(pLeaf)((pLeaf)->szLeaf >= (pLeaf)->nn) ){
228628 pIter->iEndofDoclist = pLeaf->nn+1;
228629 }else{
228630 pIter->iPgidxOff += fts5GetVarint32(&pLeaf->p[pIter->iPgidxOff],sqlite3Fts5GetVarint32(&pLeaf->p[pIter->iPgidxOff],
(u32*)&pIter->iEndofDoclist)
228631 pIter->iEndofDoclistsqlite3Fts5GetVarint32(&pLeaf->p[pIter->iPgidxOff],
(u32*)&pIter->iEndofDoclist)
228632 )sqlite3Fts5GetVarint32(&pLeaf->p[pIter->iPgidxOff],
(u32*)&pIter->iEndofDoclist)
;
228633 }
228634 }
228635}
228636
228637/*
228638** Argument p points to a buffer containing a varint to be interpreted as a
228639** position list size field. Read the varint and return the number of bytes
228640** read. Before returning, set *pnSz to the number of bytes in the position
228641** list, and *pbDel to true if the delete flag is set, or false otherwise.
228642*/
228643static int fts5GetPoslistSize(const u8 *p, int *pnSz, int *pbDel){
228644 int nSz;
228645 int n = 0;
228646 fts5FastGetVarint32(p, n, nSz){ nSz = (p)[n++]; if( nSz & 0x80 ){ n--; n += sqlite3Fts5GetVarint32
(&(p)[n],(u32*)&nSz); } }
;
228647 assert_nc( nSz>=0 )((void) (0));
228648 *pnSz = nSz/2;
228649 *pbDel = nSz & 0x0001;
228650 return n;
228651}
228652
228653/*
228654** Fts5SegIter.iLeafOffset currently points to the first byte of a
228655** position-list size field. Read the value of the field and store it
228656** in the following variables:
228657**
228658** Fts5SegIter.nPos
228659** Fts5SegIter.bDel
228660**
228661** Leave Fts5SegIter.iLeafOffset pointing to the first byte of the
228662** position list content (if any).
228663*/
228664static void fts5SegIterLoadNPos(Fts5Index *p, Fts5SegIter *pIter){
228665 if( p->rc==SQLITE_OK0 ){
228666 int iOff = pIter->iLeafOffset; /* Offset to read at */
228667 ASSERT_SZLEAF_OK(pIter->pLeaf)((void) (0));
228668 if( p->pConfig->eDetail==FTS5_DETAIL_NONE1 ){
228669 int iEod = MIN(pIter->iEndofDoclist, pIter->pLeaf->szLeaf)((pIter->iEndofDoclist)<(pIter->pLeaf->szLeaf)?(pIter
->iEndofDoclist):(pIter->pLeaf->szLeaf))
;
228670 pIter->bDel = 0;
228671 pIter->nPos = 1;
228672 if( iOff<iEod && pIter->pLeaf->p[iOff]==0 ){
228673 pIter->bDel = 1;
228674 iOff++;
228675 if( iOff<iEod && pIter->pLeaf->p[iOff]==0 ){
228676 pIter->nPos = 1;
228677 iOff++;
228678 }else{
228679 pIter->nPos = 0;
228680 }
228681 }
228682 }else{
228683 int nSz;
228684 fts5FastGetVarint32(pIter->pLeaf->p, iOff, nSz){ nSz = (pIter->pLeaf->p)[iOff++]; if( nSz & 0x80 )
{ iOff--; iOff += sqlite3Fts5GetVarint32(&(pIter->pLeaf
->p)[iOff],(u32*)&nSz); } }
;
228685 pIter->bDel = (nSz & 0x0001);
228686 pIter->nPos = nSz>>1;
228687 assert_nc( pIter->nPos>=0 )((void) (0));
228688 }
228689 pIter->iLeafOffset = iOff;
228690 }
228691}
228692
228693static void fts5SegIterLoadRowid(Fts5Index *p, Fts5SegIter *pIter){
228694 u8 *a = pIter->pLeaf->p; /* Buffer to read data from */
228695 i64 iOff = pIter->iLeafOffset;
228696
228697 ASSERT_SZLEAF_OK(pIter->pLeaf)((void) (0));
228698 if( iOff>=pIter->pLeaf->szLeaf ){
228699 fts5SegIterNextPage(p, pIter);
228700 if( pIter->pLeaf==0 ){
228701 if( p->rc==SQLITE_OK0 ) p->rc = FTS5_CORRUPT(11 | (1<<8));
228702 return;
228703 }
228704 iOff = 4;
228705 a = pIter->pLeaf->p;
228706 }
228707 iOff += sqlite3Fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
228708 pIter->iLeafOffset = iOff;
228709}
228710
228711/*
228712** Fts5SegIter.iLeafOffset currently points to the first byte of the
228713** "nSuffix" field of a term. Function parameter nKeep contains the value
228714** of the "nPrefix" field (if there was one - it is passed 0 if this is
228715** the first term in the segment).
228716**
228717** This function populates:
228718**
228719** Fts5SegIter.term
228720** Fts5SegIter.rowid
228721**
228722** accordingly and leaves (Fts5SegIter.iLeafOffset) set to the content of
228723** the first position list. The position list belonging to document
228724** (Fts5SegIter.iRowid).
228725*/
228726static void fts5SegIterLoadTerm(Fts5Index *p, Fts5SegIter *pIter, int nKeep){
228727 u8 *a = pIter->pLeaf->p; /* Buffer to read data from */
228728 i64 iOff = pIter->iLeafOffset; /* Offset to read at */
228729 int nNew; /* Bytes of new data */
228730
228731 iOff += fts5GetVarint32(&a[iOff], nNew)sqlite3Fts5GetVarint32(&a[iOff],(u32*)&nNew);
228732 if( iOff+nNew>pIter->pLeaf->szLeaf || nKeep>pIter->term.n || nNew==0 ){
228733 p->rc = FTS5_CORRUPT(11 | (1<<8));
228734 return;
228735 }
228736 pIter->term.n = nKeep;
228737 fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff])sqlite3Fts5BufferAppendBlob(&p->rc,&pIter->term
,nNew,&a[iOff])
;
228738 assert( pIter->term.n<=pIter->term.nSpace )((void) (0));
228739 iOff += nNew;
228740 pIter->iTermLeafOffset = iOff;
228741 pIter->iTermLeafPgno = pIter->iLeafPgno;
228742 pIter->iLeafOffset = iOff;
228743
228744 if( pIter->iPgidxOff>=pIter->pLeaf->nn ){
228745 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
228746 }else{
228747 int nExtra;
228748 pIter->iPgidxOff += fts5GetVarint32(&a[pIter->iPgidxOff], nExtra)sqlite3Fts5GetVarint32(&a[pIter->iPgidxOff],(u32*)&
nExtra)
;
228749 pIter->iEndofDoclist += nExtra;
228750 }
228751
228752 fts5SegIterLoadRowid(p, pIter);
228753}
228754
228755static void fts5SegIterNext(Fts5Index*, Fts5SegIter*, int*);
228756static void fts5SegIterNext_Reverse(Fts5Index*, Fts5SegIter*, int*);
228757static void fts5SegIterNext_None(Fts5Index*, Fts5SegIter*, int*);
228758
228759static void fts5SegIterSetNext(Fts5Index *p, Fts5SegIter *pIter){
228760 if( pIter->flags & FTS5_SEGITER_REVERSE0x02 ){
228761 pIter->xNext = fts5SegIterNext_Reverse;
228762 }else if( p->pConfig->eDetail==FTS5_DETAIL_NONE1 ){
228763 pIter->xNext = fts5SegIterNext_None;
228764 }else{
228765 pIter->xNext = fts5SegIterNext;
228766 }
228767}
228768
228769/*
228770** Initialize the iterator object pIter to iterate through the entries in
228771** segment pSeg. The iterator is left pointing to the first entry when
228772** this function returns.
228773**
228774** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
228775** an error has already occurred when this function is called, it is a no-op.
228776*/
228777static void fts5SegIterInit(
228778 Fts5Index *p, /* FTS index object */
228779 Fts5StructureSegment *pSeg, /* Description of segment */
228780 Fts5SegIter *pIter /* Object to populate */
228781){
228782 if( pSeg->pgnoFirst==0 ){
228783 /* This happens if the segment is being used as an input to an incremental
228784 ** merge and all data has already been "trimmed". See function
228785 ** fts5TrimSegments() for details. In this case leave the iterator empty.
228786 ** The caller will see the (pIter->pLeaf==0) and assume the iterator is
228787 ** at EOF already. */
228788 assert( pIter->pLeaf==0 )((void) (0));
228789 return;
228790 }
228791
228792 if( p->rc==SQLITE_OK0 ){
228793 memset(pIter, 0, sizeof(*pIter));
228794 fts5SegIterSetNext(p, pIter);
228795 pIter->pSeg = pSeg;
228796 pIter->iLeafPgno = pSeg->pgnoFirst-1;
228797 fts5SegIterNextPage(p, pIter);
228798 }
228799
228800 if( p->rc==SQLITE_OK0 ){
228801 pIter->iLeafOffset = 4;
228802 assert( pIter->pLeaf!=0 )((void) (0));
228803 assert_nc( pIter->pLeaf->nn>4 )((void) (0));
228804 assert_nc( fts5LeafFirstTermOff(pIter->pLeaf)==4 )((void) (0));
228805 pIter->iPgidxOff = pIter->pLeaf->szLeaf+1;
228806 fts5SegIterLoadTerm(p, pIter, 0);
228807 fts5SegIterLoadNPos(p, pIter);
228808 }
228809}
228810
228811/*
228812** This function is only ever called on iterators created by calls to
228813** Fts5IndexQuery() with the FTS5INDEX_QUERY_DESC flag set.
228814**
228815** The iterator is in an unusual state when this function is called: the
228816** Fts5SegIter.iLeafOffset variable is set to the offset of the start of
228817** the position-list size field for the first relevant rowid on the page.
228818** Fts5SegIter.rowid is set, but nPos and bDel are not.
228819**
228820** This function advances the iterator so that it points to the last
228821** relevant rowid on the page and, if necessary, initializes the
228822** aRowidOffset[] and iRowidOffset variables. At this point the iterator
228823** is in its regular state - Fts5SegIter.iLeafOffset points to the first
228824** byte of the position list content associated with said rowid.
228825*/
228826static void fts5SegIterReverseInitPage(Fts5Index *p, Fts5SegIter *pIter){
228827 int eDetail = p->pConfig->eDetail;
228828 int n = pIter->pLeaf->szLeaf;
228829 int i = pIter->iLeafOffset;
228830 u8 *a = pIter->pLeaf->p;
228831 int iRowidOffset = 0;
228832
228833 if( n>pIter->iEndofDoclist ){
228834 n = pIter->iEndofDoclist;
228835 }
228836
228837 ASSERT_SZLEAF_OK(pIter->pLeaf)((void) (0));
228838 while( 1 ){
228839 u64 iDelta = 0;
228840
228841 if( eDetail==FTS5_DETAIL_NONE1 ){
228842 /* todo */
228843 if( i<n && a[i]==0 ){
228844 i++;
228845 if( i<n && a[i]==0 ) i++;
228846 }
228847 }else{
228848 int nPos;
228849 int bDummy;
228850 i += fts5GetPoslistSize(&a[i], &nPos, &bDummy);
228851 i += nPos;
228852 }
228853 if( i>=n ) break;
228854 i += fts5GetVarintsqlite3Fts5GetVarint(&a[i], &iDelta);
228855 pIter->iRowid += iDelta;
228856
228857 /* If necessary, grow the pIter->aRowidOffset[] array. */
228858 if( iRowidOffset>=pIter->nRowidOffset ){
228859 int nNew = pIter->nRowidOffset + 8;
228860 int *aNew = (int*)sqlite3_realloc64(pIter->aRowidOffset,nNew*sizeof(int));
228861 if( aNew==0 ){
228862 p->rc = SQLITE_NOMEM7;
228863 break;
228864 }
228865 pIter->aRowidOffset = aNew;
228866 pIter->nRowidOffset = nNew;
228867 }
228868
228869 pIter->aRowidOffset[iRowidOffset++] = pIter->iLeafOffset;
228870 pIter->iLeafOffset = i;
228871 }
228872 pIter->iRowidOffset = iRowidOffset;
228873 fts5SegIterLoadNPos(p, pIter);
228874}
228875
228876/*
228877**
228878*/
228879static void fts5SegIterReverseNewPage(Fts5Index *p, Fts5SegIter *pIter){
228880 assert( pIter->flags & FTS5_SEGITER_REVERSE )((void) (0));
228881 assert( pIter->flags & FTS5_SEGITER_ONETERM )((void) (0));
228882
228883 fts5DataRelease(pIter->pLeaf);
228884 pIter->pLeaf = 0;
228885 while( p->rc==SQLITE_OK0 && pIter->iLeafPgno>pIter->iTermLeafPgno ){
228886 Fts5Data *pNew;
228887 pIter->iLeafPgno--;
228888 pNew = fts5DataRead(p, FTS5_SEGMENT_ROWID(( ((i64)(pIter->pSeg->iSegid) << (31 +5 +1)) + ((
i64)(0) << (31 + 5)) + ((i64)(0) << (31)) + ((i64
)(pIter->iLeafPgno)) )
228889 pIter->pSeg->iSegid, pIter->iLeafPgno( ((i64)(pIter->pSeg->iSegid) << (31 +5 +1)) + ((
i64)(0) << (31 + 5)) + ((i64)(0) << (31)) + ((i64
)(pIter->iLeafPgno)) )
228890 )( ((i64)(pIter->pSeg->iSegid) << (31 +5 +1)) + ((
i64)(0) << (31 + 5)) + ((i64)(0) << (31)) + ((i64
)(pIter->iLeafPgno)) )
);
228891 if( pNew ){
228892 /* iTermLeafOffset may be equal to szLeaf if the term is the last
228893 ** thing on the page - i.e. the first rowid is on the following page.
228894 ** In this case leave pIter->pLeaf==0, this iterator is at EOF. */
228895 if( pIter->iLeafPgno==pIter->iTermLeafPgno ){
228896 assert( pIter->pLeaf==0 )((void) (0));
228897 if( pIter->iTermLeafOffset<pNew->szLeaf ){
228898 pIter->pLeaf = pNew;
228899 pIter->iLeafOffset = pIter->iTermLeafOffset;
228900 }
228901 }else{
228902 int iRowidOff;
228903 iRowidOff = fts5LeafFirstRowidOff(pNew)(fts5GetU16((pNew)->p));
228904 if( iRowidOff ){
228905 if( iRowidOff>=pNew->szLeaf ){
228906 p->rc = FTS5_CORRUPT(11 | (1<<8));
228907 }else{
228908 pIter->pLeaf = pNew;
228909 pIter->iLeafOffset = iRowidOff;
228910 }
228911 }
228912 }
228913
228914 if( pIter->pLeaf ){
228915 u8 *a = &pIter->pLeaf->p[pIter->iLeafOffset];
228916 pIter->iLeafOffset += fts5GetVarintsqlite3Fts5GetVarint(a, (u64*)&pIter->iRowid);
228917 break;
228918 }else{
228919 fts5DataRelease(pNew);
228920 }
228921 }
228922 }
228923
228924 if( pIter->pLeaf ){
228925 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
228926 fts5SegIterReverseInitPage(p, pIter);
228927 }
228928}
228929
228930/*
228931** Return true if the iterator passed as the second argument currently
228932** points to a delete marker. A delete marker is an entry with a 0 byte
228933** position-list.
228934*/
228935static int fts5MultiIterIsEmpty(Fts5Index *p, Fts5Iter *pIter){
228936 Fts5SegIter *pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
228937 return (p->rc==SQLITE_OK0 && pSeg->pLeaf && pSeg->nPos==0);
228938}
228939
228940/*
228941** Advance iterator pIter to the next entry.
228942**
228943** This version of fts5SegIterNext() is only used by reverse iterators.
228944*/
228945static void fts5SegIterNext_Reverse(
228946 Fts5Index *p, /* FTS5 backend object */
228947 Fts5SegIter *pIter, /* Iterator to advance */
228948 int *pbUnused /* Unused */
228949){
228950 assert( pIter->flags & FTS5_SEGITER_REVERSE )((void) (0));
228951 assert( pIter->pNextLeaf==0 )((void) (0));
228952 UNUSED_PARAM(pbUnused)(void)(pbUnused);
228953
228954 if( pIter->iRowidOffset>0 ){
228955 u8 *a = pIter->pLeaf->p;
228956 int iOff;
228957 u64 iDelta;
228958
228959 pIter->iRowidOffset--;
228960 pIter->iLeafOffset = pIter->aRowidOffset[pIter->iRowidOffset];
228961 fts5SegIterLoadNPos(p, pIter);
228962 iOff = pIter->iLeafOffset;
228963 if( p->pConfig->eDetail!=FTS5_DETAIL_NONE1 ){
228964 iOff += pIter->nPos;
228965 }
228966 fts5GetVarintsqlite3Fts5GetVarint(&a[iOff], &iDelta);
228967 pIter->iRowid -= iDelta;
228968 }else{
228969 fts5SegIterReverseNewPage(p, pIter);
228970 }
228971}
228972
228973/*
228974** Advance iterator pIter to the next entry.
228975**
228976** This version of fts5SegIterNext() is only used if detail=none and the
228977** iterator is not a reverse direction iterator.
228978*/
228979static void fts5SegIterNext_None(
228980 Fts5Index *p, /* FTS5 backend object */
228981 Fts5SegIter *pIter, /* Iterator to advance */
228982 int *pbNewTerm /* OUT: Set for new term */
228983){
228984 int iOff;
228985
228986 assert( p->rc==SQLITE_OK )((void) (0));
228987 assert( (pIter->flags & FTS5_SEGITER_REVERSE)==0 )((void) (0));
228988 assert( p->pConfig->eDetail==FTS5_DETAIL_NONE )((void) (0));
228989
228990 ASSERT_SZLEAF_OK(pIter->pLeaf)((void) (0));
228991 iOff = pIter->iLeafOffset;
228992
228993 /* Next entry is on the next page */
228994 if( pIter->pSeg && iOff>=pIter->pLeaf->szLeaf ){
228995 fts5SegIterNextPage(p, pIter);
228996 if( p->rc || pIter->pLeaf==0 ) return;
228997 pIter->iRowid = 0;
228998 iOff = 4;
228999 }
229000
229001 if( iOff<pIter->iEndofDoclist ){
229002 /* Next entry is on the current page */
229003 i64 iDelta;
229004 iOff += sqlite3Fts5GetVarint(&pIter->pLeaf->p[iOff], (u64*)&iDelta);
229005 pIter->iLeafOffset = iOff;
229006 pIter->iRowid += iDelta;
229007 }else if( (pIter->flags & FTS5_SEGITER_ONETERM0x01)==0 ){
229008 if( pIter->pSeg ){
229009 int nKeep = 0;
229010 if( iOff!=fts5LeafFirstTermOff(pIter->pLeaf) ){
229011 iOff += fts5GetVarint32(&pIter->pLeaf->p[iOff], nKeep)sqlite3Fts5GetVarint32(&pIter->pLeaf->p[iOff],(u32*
)&nKeep)
;
229012 }
229013 pIter->iLeafOffset = iOff;
229014 fts5SegIterLoadTerm(p, pIter, nKeep);
229015 }else{
229016 const u8 *pList = 0;
229017 const char *zTerm = 0;
229018 int nList;
229019 sqlite3Fts5HashScanNext(p->pHash);
229020 sqlite3Fts5HashScanEntry(p->pHash, &zTerm, &pList, &nList);
229021 if( pList==0 ) goto next_none_eof;
229022 pIter->pLeaf->p = (u8*)pList;
229023 pIter->pLeaf->nn = nList;
229024 pIter->pLeaf->szLeaf = nList;
229025 pIter->iEndofDoclist = nList;
229026 sqlite3Fts5BufferSet(&p->rc,&pIter->term, (int)strlen(zTerm), (u8*)zTerm);
229027 pIter->iLeafOffset = fts5GetVarintsqlite3Fts5GetVarint(pList, (u64*)&pIter->iRowid);
229028 }
229029
229030 if( pbNewTerm ) *pbNewTerm = 1;
229031 }else{
229032 goto next_none_eof;
229033 }
229034
229035 fts5SegIterLoadNPos(p, pIter);
229036
229037 return;
229038 next_none_eof:
229039 fts5DataRelease(pIter->pLeaf);
229040 pIter->pLeaf = 0;
229041}
229042
229043
229044/*
229045** Advance iterator pIter to the next entry.
229046**
229047** If an error occurs, Fts5Index.rc is set to an appropriate error code. It
229048** is not considered an error if the iterator reaches EOF. If an error has
229049** already occurred when this function is called, it is a no-op.
229050*/
229051static void fts5SegIterNext(
229052 Fts5Index *p, /* FTS5 backend object */
229053 Fts5SegIter *pIter, /* Iterator to advance */
229054 int *pbNewTerm /* OUT: Set for new term */
229055){
229056 Fts5Data *pLeaf = pIter->pLeaf;
229057 int iOff;
229058 int bNewTerm = 0;
229059 int nKeep = 0;
229060 u8 *a;
229061 int n;
229062
229063 assert( pbNewTerm==0 || *pbNewTerm==0 )((void) (0));
229064 assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE )((void) (0));
229065
229066 /* Search for the end of the position list within the current page. */
229067 a = pLeaf->p;
229068 n = pLeaf->szLeaf;
229069
229070 ASSERT_SZLEAF_OK(pLeaf)((void) (0));
229071 iOff = pIter->iLeafOffset + pIter->nPos;
229072
229073 if( iOff<n ){
229074 /* The next entry is on the current page. */
229075 assert_nc( iOff<=pIter->iEndofDoclist )((void) (0));
229076 if( iOff>=pIter->iEndofDoclist ){
229077 bNewTerm = 1;
229078 if( iOff!=fts5LeafFirstTermOff(pLeaf) ){
229079 iOff += fts5GetVarint32(&a[iOff], nKeep)sqlite3Fts5GetVarint32(&a[iOff],(u32*)&nKeep);
229080 }
229081 }else{
229082 u64 iDelta;
229083 iOff += sqlite3Fts5GetVarint(&a[iOff], &iDelta);
229084 pIter->iRowid += iDelta;
229085 assert_nc( iDelta>0 )((void) (0));
229086 }
229087 pIter->iLeafOffset = iOff;
229088
229089 }else if( pIter->pSeg==0 ){
229090 const u8 *pList = 0;
229091 const char *zTerm = 0;
229092 int nList = 0;
229093 assert( (pIter->flags & FTS5_SEGITER_ONETERM) || pbNewTerm )((void) (0));
229094 if( 0==(pIter->flags & FTS5_SEGITER_ONETERM0x01) ){
229095 sqlite3Fts5HashScanNext(p->pHash);
229096 sqlite3Fts5HashScanEntry(p->pHash, &zTerm, &pList, &nList);
229097 }
229098 if( pList==0 ){
229099 fts5DataRelease(pIter->pLeaf);
229100 pIter->pLeaf = 0;
229101 }else{
229102 pIter->pLeaf->p = (u8*)pList;
229103 pIter->pLeaf->nn = nList;
229104 pIter->pLeaf->szLeaf = nList;
229105 pIter->iEndofDoclist = nList+1;
229106 sqlite3Fts5BufferSet(&p->rc, &pIter->term, (int)strlen(zTerm),
229107 (u8*)zTerm);
229108 pIter->iLeafOffset = fts5GetVarintsqlite3Fts5GetVarint(pList, (u64*)&pIter->iRowid);
229109 *pbNewTerm = 1;
229110 }
229111 }else{
229112 iOff = 0;
229113 /* Next entry is not on the current page */
229114 while( iOff==0 ){
229115 fts5SegIterNextPage(p, pIter);
229116 pLeaf = pIter->pLeaf;
229117 if( pLeaf==0 ) break;
229118 ASSERT_SZLEAF_OK(pLeaf)((void) (0));
229119 if( (iOff = fts5LeafFirstRowidOff(pLeaf)(fts5GetU16((pLeaf)->p))) && iOff<pLeaf->szLeaf ){
229120 iOff += sqlite3Fts5GetVarint(&pLeaf->p[iOff], (u64*)&pIter->iRowid);
229121 pIter->iLeafOffset = iOff;
229122
229123 if( pLeaf->nn>pLeaf->szLeaf ){
229124 pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32(sqlite3Fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf],(u32
*)&pIter->iEndofDoclist)
229125 &pLeaf->p[pLeaf->szLeaf], pIter->iEndofDoclistsqlite3Fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf],(u32
*)&pIter->iEndofDoclist)
229126 )sqlite3Fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf],(u32
*)&pIter->iEndofDoclist)
;
229127 }
229128 }
229129 else if( pLeaf->nn>pLeaf->szLeaf ){
229130 pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32(sqlite3Fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf],(u32
*)&iOff)
229131 &pLeaf->p[pLeaf->szLeaf], iOffsqlite3Fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf],(u32
*)&iOff)
229132 )sqlite3Fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf],(u32
*)&iOff)
;
229133 pIter->iLeafOffset = iOff;
229134 pIter->iEndofDoclist = iOff;
229135 bNewTerm = 1;
229136 }
229137 assert_nc( iOff<pLeaf->szLeaf )((void) (0));
229138 if( iOff>pLeaf->szLeaf ){
229139 p->rc = FTS5_CORRUPT(11 | (1<<8));
229140 return;
229141 }
229142 }
229143 }
229144
229145 /* Check if the iterator is now at EOF. If so, return early. */
229146 if( pIter->pLeaf ){
229147 if( bNewTerm ){
229148 if( pIter->flags & FTS5_SEGITER_ONETERM0x01 ){
229149 fts5DataRelease(pIter->pLeaf);
229150 pIter->pLeaf = 0;
229151 }else{
229152 fts5SegIterLoadTerm(p, pIter, nKeep);
229153 fts5SegIterLoadNPos(p, pIter);
229154 if( pbNewTerm ) *pbNewTerm = 1;
229155 }
229156 }else{
229157 /* The following could be done by calling fts5SegIterLoadNPos(). But
229158 ** this block is particularly performance critical, so equivalent
229159 ** code is inlined. */
229160 int nSz;
229161 assert_nc( pIter->iLeafOffset<=pIter->pLeaf->nn )((void) (0));
229162 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); } }
;
229163 pIter->bDel = (nSz & 0x0001);
229164 pIter->nPos = nSz>>1;
229165 assert_nc( pIter->nPos>=0 )((void) (0));
229166 }
229167 }
229168}
229169
229170#define SWAPVAL(T, a, b){ T tmp; tmp=a; a=b; b=tmp; } { T tmp; tmp=a; a=b; b=tmp; }
229171
229172#define fts5IndexSkipVarint(a, iOff){ int iEnd = iOff+9; while( (a[iOff++] & 0x80) &&
iOff<iEnd ); }
{ \
229173 int iEnd = iOff+9; \
229174 while( (a[iOff++] & 0x80) && iOff<iEnd ); \
229175}
229176
229177/*
229178** Iterator pIter currently points to the first rowid in a doclist. This
229179** function sets the iterator up so that iterates in reverse order through
229180** the doclist.
229181*/
229182static void fts5SegIterReverse(Fts5Index *p, Fts5SegIter *pIter){
229183 Fts5DlidxIter *pDlidx = pIter->pDlidx;
229184 Fts5Data *pLast = 0;
229185 int pgnoLast = 0;
229186
229187 if( pDlidx ){
229188 int iSegid = pIter->pSeg->iSegid;
229189 pgnoLast = fts5DlidxIterPgno(pDlidx);
229190 pLast = fts5LeafRead(p, FTS5_SEGMENT_ROWID(iSegid, pgnoLast)( ((i64)(iSegid) << (31 +5 +1)) + ((i64)(0) << (31
+ 5)) + ((i64)(0) << (31)) + ((i64)(pgnoLast)) )
);
229191 }else{
229192 Fts5Data *pLeaf = pIter->pLeaf; /* Current leaf data */
229193
229194 /* Currently, Fts5SegIter.iLeafOffset points to the first byte of
229195 ** position-list content for the current rowid. Back it up so that it
229196 ** points to the start of the position-list size field. */
229197 int iPoslist;
229198 if( pIter->iTermLeafPgno==pIter->iLeafPgno ){
229199 iPoslist = pIter->iTermLeafOffset;
229200 }else{
229201 iPoslist = 4;
229202 }
229203 fts5IndexSkipVarint(pLeaf->p, iPoslist){ int iEnd = iPoslist+9; while( (pLeaf->p[iPoslist++] &
0x80) && iPoslist<iEnd ); }
;
229204 pIter->iLeafOffset = iPoslist;
229205
229206 /* If this condition is true then the largest rowid for the current
229207 ** term may not be stored on the current page. So search forward to
229208 ** see where said rowid really is. */
229209 if( pIter->iEndofDoclist>=pLeaf->szLeaf ){
229210 int pgno;
229211 Fts5StructureSegment *pSeg = pIter->pSeg;
229212
229213 /* The last rowid in the doclist may not be on the current page. Search
229214 ** forward to find the page containing the last rowid. */
229215 for(pgno=pIter->iLeafPgno+1; !p->rc && pgno<=pSeg->pgnoLast; pgno++){
229216 i64 iAbs = FTS5_SEGMENT_ROWID(pSeg->iSegid, pgno)( ((i64)(pSeg->iSegid) << (31 +5 +1)) + ((i64)(0) <<
(31 + 5)) + ((i64)(0) << (31)) + ((i64)(pgno)) )
;
229217 Fts5Data *pNew = fts5LeafRead(p, iAbs);
229218 if( pNew ){
229219 int iRowid, bTermless;
229220 iRowid = fts5LeafFirstRowidOff(pNew)(fts5GetU16((pNew)->p));
229221 bTermless = fts5LeafIsTermless(pNew)((pNew)->szLeaf >= (pNew)->nn);
229222 if( iRowid ){
229223 SWAPVAL(Fts5Data*, pNew, pLast){ Fts5Data* tmp; tmp=pNew; pNew=pLast; pLast=tmp; };
229224 pgnoLast = pgno;
229225 }
229226 fts5DataRelease(pNew);
229227 if( bTermless==0 ) break;
229228 }
229229 }
229230 }
229231 }
229232
229233 /* If pLast is NULL at this point, then the last rowid for this doclist
229234 ** lies on the page currently indicated by the iterator. In this case
229235 ** pIter->iLeafOffset is already set to point to the position-list size
229236 ** field associated with the first relevant rowid on the page.
229237 **
229238 ** Or, if pLast is non-NULL, then it is the page that contains the last
229239 ** rowid. In this case configure the iterator so that it points to the
229240 ** first rowid on this page.
229241 */
229242 if( pLast ){
229243 int iOff;
229244 fts5DataRelease(pIter->pLeaf);
229245 pIter->pLeaf = pLast;
229246 pIter->iLeafPgno = pgnoLast;
229247 iOff = fts5LeafFirstRowidOff(pLast)(fts5GetU16((pLast)->p));
229248 if( iOff>pLast->szLeaf ){
229249 p->rc = FTS5_CORRUPT(11 | (1<<8));
229250 return;
229251 }
229252 iOff += fts5GetVarintsqlite3Fts5GetVarint(&pLast->p[iOff], (u64*)&pIter->iRowid);
229253 pIter->iLeafOffset = iOff;
229254
229255 if( fts5LeafIsTermless(pLast)((pLast)->szLeaf >= (pLast)->nn) ){
229256 pIter->iEndofDoclist = pLast->nn+1;
229257 }else{
229258 pIter->iEndofDoclist = fts5LeafFirstTermOff(pLast);
229259 }
229260 }
229261
229262 fts5SegIterReverseInitPage(p, pIter);
229263}
229264
229265/*
229266** Iterator pIter currently points to the first rowid of a doclist.
229267** There is a doclist-index associated with the final term on the current
229268** page. If the current term is the last term on the page, load the
229269** doclist-index from disk and initialize an iterator at (pIter->pDlidx).
229270*/
229271static void fts5SegIterLoadDlidx(Fts5Index *p, Fts5SegIter *pIter){
229272 int iSeg = pIter->pSeg->iSegid;
229273 int bRev = (pIter->flags & FTS5_SEGITER_REVERSE0x02);
229274 Fts5Data *pLeaf = pIter->pLeaf; /* Current leaf data */
229275
229276 assert( pIter->flags & FTS5_SEGITER_ONETERM )((void) (0));
229277 assert( pIter->pDlidx==0 )((void) (0));
229278
229279 /* Check if the current doclist ends on this page. If it does, return
229280 ** early without loading the doclist-index (as it belongs to a different
229281 ** term. */
229282 if( pIter->iTermLeafPgno==pIter->iLeafPgno
229283 && pIter->iEndofDoclist<pLeaf->szLeaf
229284 ){
229285 return;
229286 }
229287
229288 pIter->pDlidx = fts5DlidxIterInit(p, bRev, iSeg, pIter->iTermLeafPgno);
229289}
229290
229291/*
229292** The iterator object passed as the second argument currently contains
229293** no valid values except for the Fts5SegIter.pLeaf member variable. This
229294** function searches the leaf page for a term matching (pTerm/nTerm).
229295**
229296** If the specified term is found on the page, then the iterator is left
229297** pointing to it. If argument bGe is zero and the term is not found,
229298** the iterator is left pointing at EOF.
229299**
229300** If bGe is non-zero and the specified term is not found, then the
229301** iterator is left pointing to the smallest term in the segment that
229302** is larger than the specified term, even if this term is not on the
229303** current page.
229304*/
229305static void fts5LeafSeek(
229306 Fts5Index *p, /* Leave any error code here */
229307 int bGe, /* True for a >= search */
229308 Fts5SegIter *pIter, /* Iterator to seek */
229309 const u8 *pTerm, int nTerm /* Term to search for */
229310){
229311 u32 iOff;
229312 const u8 *a = pIter->pLeaf->p;
229313 u32 n = (u32)pIter->pLeaf->nn;
229314
229315 u32 nMatch = 0;
229316 u32 nKeep = 0;
229317 u32 nNew = 0;
229318 u32 iTermOff;
229319 u32 iPgidx; /* Current offset in pgidx */
229320 int bEndOfPage = 0;
229321
229322 assert( p->rc==SQLITE_OK )((void) (0));
229323
229324 iPgidx = (u32)pIter->pLeaf->szLeaf;
229325 iPgidx += fts5GetVarint32(&a[iPgidx], iTermOff)sqlite3Fts5GetVarint32(&a[iPgidx],(u32*)&iTermOff);
229326 iOff = iTermOff;
229327 if( iOff>n ){
229328 p->rc = FTS5_CORRUPT(11 | (1<<8));
229329 return;
229330 }
229331
229332 while( 1 ){
229333
229334 /* Figure out how many new bytes are in this term */
229335 fts5FastGetVarint32(a, iOff, nNew){ nNew = (a)[iOff++]; if( nNew & 0x80 ){ iOff--; iOff += sqlite3Fts5GetVarint32
(&(a)[iOff],(u32*)&nNew); } }
;
229336 if( nKeep<nMatch ){
229337 goto search_failed;
229338 }
229339
229340 assert( nKeep>=nMatch )((void) (0));
229341 if( nKeep==nMatch ){
229342 u32 nCmp;
229343 u32 i;
229344 nCmp = (u32)MIN(nNew, nTerm-nMatch)((nNew)<(nTerm-nMatch)?(nNew):(nTerm-nMatch));
229345 for(i=0; i<nCmp; i++){
229346 if( a[iOff+i]!=pTerm[nMatch+i] ) break;
229347 }
229348 nMatch += i;
229349
229350 if( (u32)nTerm==nMatch ){
229351 if( i==nNew ){
229352 goto search_success;
229353 }else{
229354 goto search_failed;
229355 }
229356 }else if( i<nNew && a[iOff+i]>pTerm[nMatch] ){
229357 goto search_failed;
229358 }
229359 }
229360
229361 if( iPgidx>=n ){
229362 bEndOfPage = 1;
229363 break;
229364 }
229365
229366 iPgidx += fts5GetVarint32(&a[iPgidx], nKeep)sqlite3Fts5GetVarint32(&a[iPgidx],(u32*)&nKeep);
229367 iTermOff += nKeep;
229368 iOff = iTermOff;
229369
229370 if( iOff>=n ){
229371 p->rc = FTS5_CORRUPT(11 | (1<<8));
229372 return;
229373 }
229374
229375 /* Read the nKeep field of the next term. */
229376 fts5FastGetVarint32(a, iOff, nKeep){ nKeep = (a)[iOff++]; if( nKeep & 0x80 ){ iOff--; iOff +=
sqlite3Fts5GetVarint32(&(a)[iOff],(u32*)&nKeep); } }
;
229377 }
229378
229379 search_failed:
229380 if( bGe==0 ){
229381 fts5DataRelease(pIter->pLeaf);
229382 pIter->pLeaf = 0;
229383 return;
229384 }else if( bEndOfPage ){
229385 do {
229386 fts5SegIterNextPage(p, pIter);
229387 if( pIter->pLeaf==0 ) return;
229388 a = pIter->pLeaf->p;
229389 if( fts5LeafIsTermless(pIter->pLeaf)((pIter->pLeaf)->szLeaf >= (pIter->pLeaf)->nn)==0 ){
229390 iPgidx = (u32)pIter->pLeaf->szLeaf;
229391 iPgidx += fts5GetVarint32(&pIter->pLeaf->p[iPgidx], iOff)sqlite3Fts5GetVarint32(&pIter->pLeaf->p[iPgidx],(u32
*)&iOff)
;
229392 if( iOff<4 || (i64)iOff>=pIter->pLeaf->szLeaf ){
229393 p->rc = FTS5_CORRUPT(11 | (1<<8));
229394 return;
229395 }else{
229396 nKeep = 0;
229397 iTermOff = iOff;
229398 n = (u32)pIter->pLeaf->nn;
229399 iOff += fts5GetVarint32(&a[iOff], nNew)sqlite3Fts5GetVarint32(&a[iOff],(u32*)&nNew);
229400 break;
229401 }
229402 }
229403 }while( 1 );
229404 }
229405
229406 search_success:
229407 if( (i64)iOff+nNew>n || nNew<1 ){
229408 p->rc = FTS5_CORRUPT(11 | (1<<8));
229409 return;
229410 }
229411 pIter->iLeafOffset = iOff + nNew;
229412 pIter->iTermLeafOffset = pIter->iLeafOffset;
229413 pIter->iTermLeafPgno = pIter->iLeafPgno;
229414
229415 fts5BufferSet(&p->rc, &pIter->term, nKeep, pTerm)sqlite3Fts5BufferSet(&p->rc,&pIter->term,nKeep,
pTerm)
;
229416 fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff])sqlite3Fts5BufferAppendBlob(&p->rc,&pIter->term
,nNew,&a[iOff])
;
229417
229418 if( iPgidx>=n ){
229419 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
229420 }else{
229421 int nExtra;
229422 iPgidx += fts5GetVarint32(&a[iPgidx], nExtra)sqlite3Fts5GetVarint32(&a[iPgidx],(u32*)&nExtra);
229423 pIter->iEndofDoclist = iTermOff + nExtra;
229424 }
229425 pIter->iPgidxOff = iPgidx;
229426
229427 fts5SegIterLoadRowid(p, pIter);
229428 fts5SegIterLoadNPos(p, pIter);
229429}
229430
229431static sqlite3_stmt *fts5IdxSelectStmt(Fts5Index *p){
229432 if( p->pIdxSelect==0 ){
229433 Fts5Config *pConfig = p->pConfig;
229434 fts5IndexPrepareStmt(p, &p->pIdxSelect, sqlite3_mprintf(
229435 "SELECT pgno FROM '%q'.'%q_idx' WHERE "
229436 "segid=? AND term<=? ORDER BY term DESC LIMIT 1",
229437 pConfig->zDb, pConfig->zName
229438 ));
229439 }
229440 return p->pIdxSelect;
229441}
229442
229443/*
229444** Initialize the object pIter to point to term pTerm/nTerm within segment
229445** pSeg. If there is no such term in the index, the iterator is set to EOF.
229446**
229447** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
229448** an error has already occurred when this function is called, it is a no-op.
229449*/
229450static void fts5SegIterSeekInit(
229451 Fts5Index *p, /* FTS5 backend */
229452 const u8 *pTerm, int nTerm, /* Term to seek to */
229453 int flags, /* Mask of FTS5INDEX_XXX flags */
229454 Fts5StructureSegment *pSeg, /* Description of segment */
229455 Fts5SegIter *pIter /* Object to populate */
229456){
229457 int iPg = 1;
229458 int bGe = (flags & FTS5INDEX_QUERY_SCAN0x0008);
229459 int bDlidx = 0; /* True if there is a doclist-index */
229460 sqlite3_stmt *pIdxSelect = 0;
229461
229462 assert( bGe==0 || (flags & FTS5INDEX_QUERY_DESC)==0 )((void) (0));
229463 assert( pTerm && nTerm )((void) (0));
229464 memset(pIter, 0, sizeof(*pIter));
229465 pIter->pSeg = pSeg;
229466
229467 /* This block sets stack variable iPg to the leaf page number that may
229468 ** contain term (pTerm/nTerm), if it is present in the segment. */
229469 pIdxSelect = fts5IdxSelectStmt(p);
229470 if( p->rc ) return;
229471 sqlite3_bind_int(pIdxSelect, 1, pSeg->iSegid);
229472 sqlite3_bind_blob(pIdxSelect, 2, pTerm, nTerm, SQLITE_STATIC((sqlite3_destructor_type)0));
229473 if( SQLITE_ROW100==sqlite3_step(pIdxSelect) ){
229474 i64 val = sqlite3_column_int(pIdxSelect, 0);
229475 iPg = (int)(val>>1);
229476 bDlidx = (val & 0x0001);
229477 }
229478 p->rc = sqlite3_reset(pIdxSelect);
229479 sqlite3_bind_null(pIdxSelect, 2);
229480
229481 if( iPg<pSeg->pgnoFirst ){
229482 iPg = pSeg->pgnoFirst;
229483 bDlidx = 0;
229484 }
229485
229486 pIter->iLeafPgno = iPg - 1;
229487 fts5SegIterNextPage(p, pIter);
229488
229489 if( pIter->pLeaf ){
229490 fts5LeafSeek(p, bGe, pIter, pTerm, nTerm);
229491 }
229492
229493 if( p->rc==SQLITE_OK0 && bGe==0 ){
229494 pIter->flags |= FTS5_SEGITER_ONETERM0x01;
229495 if( pIter->pLeaf ){
229496 if( flags & FTS5INDEX_QUERY_DESC0x0002 ){
229497 pIter->flags |= FTS5_SEGITER_REVERSE0x02;
229498 }
229499 if( bDlidx ){
229500 fts5SegIterLoadDlidx(p, pIter);
229501 }
229502 if( flags & FTS5INDEX_QUERY_DESC0x0002 ){
229503 fts5SegIterReverse(p, pIter);
229504 }
229505 }
229506 }
229507
229508 fts5SegIterSetNext(p, pIter);
229509
229510 /* Either:
229511 **
229512 ** 1) an error has occurred, or
229513 ** 2) the iterator points to EOF, or
229514 ** 3) the iterator points to an entry with term (pTerm/nTerm), or
229515 ** 4) the FTS5INDEX_QUERY_SCAN flag was set and the iterator points
229516 ** to an entry with a term greater than or equal to (pTerm/nTerm).
229517 */
229518 assert_nc( p->rc!=SQLITE_OK /* 1 */((void) (0))
229519 || pIter->pLeaf==0 /* 2 */((void) (0))
229520 || fts5BufferCompareBlob(&pIter->term, pTerm, nTerm)==0 /* 3 */((void) (0))
229521 || (bGe && fts5BufferCompareBlob(&pIter->term, pTerm, nTerm)>0) /* 4 */((void) (0))
229522 )((void) (0));
229523}
229524
229525/*
229526** Initialize the object pIter to point to term pTerm/nTerm within the
229527** in-memory hash table. If there is no such term in the hash-table, the
229528** iterator is set to EOF.
229529**
229530** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
229531** an error has already occurred when this function is called, it is a no-op.
229532*/
229533static void fts5SegIterHashInit(
229534 Fts5Index *p, /* FTS5 backend */
229535 const u8 *pTerm, int nTerm, /* Term to seek to */
229536 int flags, /* Mask of FTS5INDEX_XXX flags */
229537 Fts5SegIter *pIter /* Object to populate */
229538){
229539 int nList = 0;
229540 const u8 *z = 0;
229541 int n = 0;
229542 Fts5Data *pLeaf = 0;
229543
229544 assert( p->pHash )((void) (0));
229545 assert( p->rc==SQLITE_OK )((void) (0));
229546
229547 if( pTerm==0 || (flags & FTS5INDEX_QUERY_SCAN0x0008) ){
229548 const u8 *pList = 0;
229549
229550 p->rc = sqlite3Fts5HashScanInit(p->pHash, (const char*)pTerm, nTerm);
229551 sqlite3Fts5HashScanEntry(p->pHash, (const char**)&z, &pList, &nList);
229552 n = (z ? (int)strlen((const char*)z) : 0);
229553 if( pList ){
229554 pLeaf = fts5IdxMalloc(p, sizeof(Fts5Data));
229555 if( pLeaf ){
229556 pLeaf->p = (u8*)pList;
229557 }
229558 }
229559 }else{
229560 p->rc = sqlite3Fts5HashQuery(p->pHash, sizeof(Fts5Data),
229561 (const char*)pTerm, nTerm, (void**)&pLeaf, &nList
229562 );
229563 if( pLeaf ){
229564 pLeaf->p = (u8*)&pLeaf[1];
229565 }
229566 z = pTerm;
229567 n = nTerm;
229568 pIter->flags |= FTS5_SEGITER_ONETERM0x01;
229569 }
229570
229571 if( pLeaf ){
229572 sqlite3Fts5BufferSet(&p->rc, &pIter->term, n, z);
229573 pLeaf->nn = pLeaf->szLeaf = nList;
229574 pIter->pLeaf = pLeaf;
229575 pIter->iLeafOffset = fts5GetVarintsqlite3Fts5GetVarint(pLeaf->p, (u64*)&pIter->iRowid);
229576 pIter->iEndofDoclist = pLeaf->nn;
229577
229578 if( flags & FTS5INDEX_QUERY_DESC0x0002 ){
229579 pIter->flags |= FTS5_SEGITER_REVERSE0x02;
229580 fts5SegIterReverseInitPage(p, pIter);
229581 }else{
229582 fts5SegIterLoadNPos(p, pIter);
229583 }
229584 }
229585
229586 fts5SegIterSetNext(p, pIter);
229587}
229588
229589/*
229590** Zero the iterator passed as the only argument.
229591*/
229592static void fts5SegIterClear(Fts5SegIter *pIter){
229593 fts5BufferFree(&pIter->term)sqlite3Fts5BufferFree(&pIter->term);
229594 fts5DataRelease(pIter->pLeaf);
229595 fts5DataRelease(pIter->pNextLeaf);
229596 fts5DlidxIterFree(pIter->pDlidx);
229597 sqlite3_free(pIter->aRowidOffset);
229598 memset(pIter, 0, sizeof(Fts5SegIter));
229599}
229600
229601#ifdef SQLITE_DEBUG
229602
229603/*
229604** This function is used as part of the big assert() procedure implemented by
229605** fts5AssertMultiIterSetup(). It ensures that the result currently stored
229606** in *pRes is the correct result of comparing the current positions of the
229607** two iterators.
229608*/
229609static void fts5AssertComparisonResult(
229610 Fts5Iter *pIter,
229611 Fts5SegIter *p1,
229612 Fts5SegIter *p2,
229613 Fts5CResult *pRes
229614){
229615 int i1 = p1 - pIter->aSeg;
229616 int i2 = p2 - pIter->aSeg;
229617
229618 if( p1->pLeaf || p2->pLeaf ){
229619 if( p1->pLeaf==0 ){
229620 assert( pRes->iFirst==i2 )((void) (0));
229621 }else if( p2->pLeaf==0 ){
229622 assert( pRes->iFirst==i1 )((void) (0));
229623 }else{
229624 int nMin = MIN(p1->term.n, p2->term.n)((p1->term.n)<(p2->term.n)?(p1->term.n):(p2->term
.n))
;
229625 int res = fts5Memcmp(p1->term.p, p2->term.p, nMin)((nMin)<=0 ? 0 : memcmp((p1->term.p), (p2->term.p), (
nMin)))
;
229626 if( res==0 ) res = p1->term.n - p2->term.n;
229627
229628 if( res==0 ){
229629 assert( pRes->bTermEq==1 )((void) (0));
229630 assert( p1->iRowid!=p2->iRowid )((void) (0));
229631 res = ((p1->iRowid > p2->iRowid)==pIter->bRev) ? -1 : 1;
229632 }else{
229633 assert( pRes->bTermEq==0 )((void) (0));
229634 }
229635
229636 if( res<0 ){
229637 assert( pRes->iFirst==i1 )((void) (0));
229638 }else{
229639 assert( pRes->iFirst==i2 )((void) (0));
229640 }
229641 }
229642 }
229643}
229644
229645/*
229646** This function is a no-op unless SQLITE_DEBUG is defined when this module
229647** is compiled. In that case, this function is essentially an assert()
229648** statement used to verify that the contents of the pIter->aFirst[] array
229649** are correct.
229650*/
229651static void fts5AssertMultiIterSetup(Fts5Index *p, Fts5Iter *pIter){
229652 if( p->rc==SQLITE_OK0 ){
229653 Fts5SegIter *pFirst = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
229654 int i;
229655
229656 assert( (pFirst->pLeaf==0)==pIter->base.bEof )((void) (0));
229657
229658 /* Check that pIter->iSwitchRowid is set correctly. */
229659 for(i=0; i<pIter->nSeg; i++){
229660 Fts5SegIter *p1 = &pIter->aSeg[i];
229661 assert( p1==pFirst((void) (0))
229662 || p1->pLeaf==0((void) (0))
229663 || fts5BufferCompare(&pFirst->term, &p1->term)((void) (0))
229664 || p1->iRowid==pIter->iSwitchRowid((void) (0))
229665 || (p1->iRowid<pIter->iSwitchRowid)==pIter->bRev((void) (0))
229666 )((void) (0));
229667 }
229668
229669 for(i=0; i<pIter->nSeg; i+=2){
229670 Fts5SegIter *p1 = &pIter->aSeg[i];
229671 Fts5SegIter *p2 = &pIter->aSeg[i+1];
229672 Fts5CResult *pRes = &pIter->aFirst[(pIter->nSeg + i) / 2];
229673 fts5AssertComparisonResult(pIter, p1, p2, pRes);
229674 }
229675
229676 for(i=1; i<(pIter->nSeg / 2); i+=2){
229677 Fts5SegIter *p1 = &pIter->aSeg[ pIter->aFirst[i*2].iFirst ];
229678 Fts5SegIter *p2 = &pIter->aSeg[ pIter->aFirst[i*2+1].iFirst ];
229679 Fts5CResult *pRes = &pIter->aFirst[i];
229680 fts5AssertComparisonResult(pIter, p1, p2, pRes);
229681 }
229682 }
229683}
229684#else
229685# define fts5AssertMultiIterSetup(x,y)
229686#endif
229687
229688/*
229689** Do the comparison necessary to populate pIter->aFirst[iOut].
229690**
229691** If the returned value is non-zero, then it is the index of an entry
229692** in the pIter->aSeg[] array that is (a) not at EOF, and (b) pointing
229693** to a key that is a duplicate of another, higher priority,
229694** segment-iterator in the pSeg->aSeg[] array.
229695*/
229696static int fts5MultiIterDoCompare(Fts5Iter *pIter, int iOut){
229697 int i1; /* Index of left-hand Fts5SegIter */
229698 int i2; /* Index of right-hand Fts5SegIter */
229699 int iRes;
229700 Fts5SegIter *p1; /* Left-hand Fts5SegIter */
229701 Fts5SegIter *p2; /* Right-hand Fts5SegIter */
229702 Fts5CResult *pRes = &pIter->aFirst[iOut];
229703
229704 assert( iOut<pIter->nSeg && iOut>0 )((void) (0));
229705 assert( pIter->bRev==0 || pIter->bRev==1 )((void) (0));
229706
229707 if( iOut>=(pIter->nSeg/2) ){
229708 i1 = (iOut - pIter->nSeg/2) * 2;
229709 i2 = i1 + 1;
229710 }else{
229711 i1 = pIter->aFirst[iOut*2].iFirst;
229712 i2 = pIter->aFirst[iOut*2+1].iFirst;
229713 }
229714 p1 = &pIter->aSeg[i1];
229715 p2 = &pIter->aSeg[i2];
229716
229717 pRes->bTermEq = 0;
229718 if( p1->pLeaf==0 ){ /* If p1 is at EOF */
229719 iRes = i2;
229720 }else if( p2->pLeaf==0 ){ /* If p2 is at EOF */
229721 iRes = i1;
229722 }else{
229723 int res = fts5BufferCompare(&p1->term, &p2->term);
229724 if( res==0 ){
229725 assert_nc( i2>i1 )((void) (0));
229726 assert_nc( i2!=0 )((void) (0));
229727 pRes->bTermEq = 1;
229728 if( p1->iRowid==p2->iRowid ){
229729 p1->bDel = p2->bDel;
229730 return i2;
229731 }
229732 res = ((p1->iRowid > p2->iRowid)==pIter->bRev) ? -1 : +1;
229733 }
229734 assert( res!=0 )((void) (0));
229735 if( res<0 ){
229736 iRes = i1;
229737 }else{
229738 iRes = i2;
229739 }
229740 }
229741
229742 pRes->iFirst = (u16)iRes;
229743 return 0;
229744}
229745
229746/*
229747** Move the seg-iter so that it points to the first rowid on page iLeafPgno.
229748** It is an error if leaf iLeafPgno does not exist or contains no rowids.
229749*/
229750static void fts5SegIterGotoPage(
229751 Fts5Index *p, /* FTS5 backend object */
229752 Fts5SegIter *pIter, /* Iterator to advance */
229753 int iLeafPgno
229754){
229755 assert( iLeafPgno>pIter->iLeafPgno )((void) (0));
229756
229757 if( iLeafPgno>pIter->pSeg->pgnoLast ){
229758 p->rc = FTS5_CORRUPT(11 | (1<<8));
229759 }else{
229760 fts5DataRelease(pIter->pNextLeaf);
229761 pIter->pNextLeaf = 0;
229762 pIter->iLeafPgno = iLeafPgno-1;
229763 fts5SegIterNextPage(p, pIter);
229764 assert( p->rc!=SQLITE_OK || pIter->iLeafPgno==iLeafPgno )((void) (0));
229765
229766 if( p->rc==SQLITE_OK0 && ALWAYS(pIter->pLeaf!=0)(pIter->pLeaf!=0) ){
229767 int iOff;
229768 u8 *a = pIter->pLeaf->p;
229769 int n = pIter->pLeaf->szLeaf;
229770
229771 iOff = fts5LeafFirstRowidOff(pIter->pLeaf)(fts5GetU16((pIter->pLeaf)->p));
229772 if( iOff<4 || iOff>=n ){
229773 p->rc = FTS5_CORRUPT(11 | (1<<8));
229774 }else{
229775 iOff += fts5GetVarintsqlite3Fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
229776 pIter->iLeafOffset = iOff;
229777 fts5SegIterLoadNPos(p, pIter);
229778 }
229779 }
229780 }
229781}
229782
229783/*
229784** Advance the iterator passed as the second argument until it is at or
229785** past rowid iFrom. Regardless of the value of iFrom, the iterator is
229786** always advanced at least once.
229787*/
229788static void fts5SegIterNextFrom(
229789 Fts5Index *p, /* FTS5 backend object */
229790 Fts5SegIter *pIter, /* Iterator to advance */
229791 i64 iMatch /* Advance iterator at least this far */
229792){
229793 int bRev = (pIter->flags & FTS5_SEGITER_REVERSE0x02);
229794 Fts5DlidxIter *pDlidx = pIter->pDlidx;
229795 int iLeafPgno = pIter->iLeafPgno;
229796 int bMove = 1;
229797
229798 assert( pIter->flags & FTS5_SEGITER_ONETERM )((void) (0));
229799 assert( pIter->pDlidx )((void) (0));
229800 assert( pIter->pLeaf )((void) (0));
229801
229802 if( bRev==0 ){
229803 while( !fts5DlidxIterEof(p, pDlidx) && iMatch>fts5DlidxIterRowid(pDlidx) ){
229804 iLeafPgno = fts5DlidxIterPgno(pDlidx);
229805 fts5DlidxIterNext(p, pDlidx);
229806 }
229807 assert_nc( iLeafPgno>=pIter->iLeafPgno || p->rc )((void) (0));
229808 if( iLeafPgno>pIter->iLeafPgno ){
229809 fts5SegIterGotoPage(p, pIter, iLeafPgno);
229810 bMove = 0;
229811 }
229812 }else{
229813 assert( pIter->pNextLeaf==0 )((void) (0));
229814 assert( iMatch<pIter->iRowid )((void) (0));
229815 while( !fts5DlidxIterEof(p, pDlidx) && iMatch<fts5DlidxIterRowid(pDlidx) ){
229816 fts5DlidxIterPrev(p, pDlidx);
229817 }
229818 iLeafPgno = fts5DlidxIterPgno(pDlidx);
229819
229820 assert( fts5DlidxIterEof(p, pDlidx) || iLeafPgno<=pIter->iLeafPgno )((void) (0));
229821
229822 if( iLeafPgno<pIter->iLeafPgno ){
229823 pIter->iLeafPgno = iLeafPgno+1;
229824 fts5SegIterReverseNewPage(p, pIter);
229825 bMove = 0;
229826 }
229827 }
229828
229829 do{
229830 if( bMove && p->rc==SQLITE_OK0 ) pIter->xNext(p, pIter, 0);
229831 if( pIter->pLeaf==0 ) break;
229832 if( bRev==0 && pIter->iRowid>=iMatch ) break;
229833 if( bRev!=0 && pIter->iRowid<=iMatch ) break;
229834 bMove = 1;
229835 }while( p->rc==SQLITE_OK0 );
229836}
229837
229838
229839/*
229840** Free the iterator object passed as the second argument.
229841*/
229842static void fts5MultiIterFree(Fts5Iter *pIter){
229843 if( pIter ){
229844 int i;
229845 for(i=0; i<pIter->nSeg; i++){
229846 fts5SegIterClear(&pIter->aSeg[i]);
229847 }
229848 fts5BufferFree(&pIter->poslist)sqlite3Fts5BufferFree(&pIter->poslist);
229849 sqlite3_free(pIter);
229850 }
229851}
229852
229853static void fts5MultiIterAdvanced(
229854 Fts5Index *p, /* FTS5 backend to iterate within */
229855 Fts5Iter *pIter, /* Iterator to update aFirst[] array for */
229856 int iChanged, /* Index of sub-iterator just advanced */
229857 int iMinset /* Minimum entry in aFirst[] to set */
229858){
229859 int i;
229860 for(i=(pIter->nSeg+iChanged)/2; i>=iMinset && p->rc==SQLITE_OK0; i=i/2){
229861 int iEq;
229862 if( (iEq = fts5MultiIterDoCompare(pIter, i)) ){
229863 Fts5SegIter *pSeg = &pIter->aSeg[iEq];
229864 assert( p->rc==SQLITE_OK )((void) (0));
229865 pSeg->xNext(p, pSeg, 0);
229866 i = pIter->nSeg + iEq;
229867 }
229868 }
229869}
229870
229871/*
229872** Sub-iterator iChanged of iterator pIter has just been advanced. It still
229873** points to the same term though - just a different rowid. This function
229874** attempts to update the contents of the pIter->aFirst[] accordingly.
229875** If it does so successfully, 0 is returned. Otherwise 1.
229876**
229877** If non-zero is returned, the caller should call fts5MultiIterAdvanced()
229878** on the iterator instead. That function does the same as this one, except
229879** that it deals with more complicated cases as well.
229880*/
229881static int fts5MultiIterAdvanceRowid(
229882 Fts5Iter *pIter, /* Iterator to update aFirst[] array for */
229883 int iChanged, /* Index of sub-iterator just advanced */
229884 Fts5SegIter **ppFirst
229885){
229886 Fts5SegIter *pNew = &pIter->aSeg[iChanged];
229887
229888 if( pNew->iRowid==pIter->iSwitchRowid
229889 || (pNew->iRowid<pIter->iSwitchRowid)==pIter->bRev
229890 ){
229891 int i;
229892 Fts5SegIter *pOther = &pIter->aSeg[iChanged ^ 0x0001];
229893 pIter->iSwitchRowid = pIter->bRev ? SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) : LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
229894 for(i=(pIter->nSeg+iChanged)/2; 1; i=i/2){
229895 Fts5CResult *pRes = &pIter->aFirst[i];
229896
229897 assert( pNew->pLeaf )((void) (0));
229898 assert( pRes->bTermEq==0 || pOther->pLeaf )((void) (0));
229899
229900 if( pRes->bTermEq ){
229901 if( pNew->iRowid==pOther->iRowid ){
229902 return 1;
229903 }else if( (pOther->iRowid>pNew->iRowid)==pIter->bRev ){
229904 pIter->iSwitchRowid = pOther->iRowid;
229905 pNew = pOther;
229906 }else if( (pOther->iRowid>pIter->iSwitchRowid)==pIter->bRev ){
229907 pIter->iSwitchRowid = pOther->iRowid;
229908 }
229909 }
229910 pRes->iFirst = (u16)(pNew - pIter->aSeg);
229911 if( i==1 ) break;
229912
229913 pOther = &pIter->aSeg[ pIter->aFirst[i ^ 0x0001].iFirst ];
229914 }
229915 }
229916
229917 *ppFirst = pNew;
229918 return 0;
229919}
229920
229921/*
229922** Set the pIter->bEof variable based on the state of the sub-iterators.
229923*/
229924static void fts5MultiIterSetEof(Fts5Iter *pIter){
229925 Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
229926 pIter->base.bEof = pSeg->pLeaf==0;
229927 pIter->iSwitchRowid = pSeg->iRowid;
229928}
229929
229930/*
229931** Move the iterator to the next entry.
229932**
229933** If an error occurs, an error code is left in Fts5Index.rc. It is not
229934** considered an error if the iterator reaches EOF, or if it is already at
229935** EOF when this function is called.
229936*/
229937static void fts5MultiIterNext(
229938 Fts5Index *p,
229939 Fts5Iter *pIter,
229940 int bFrom, /* True if argument iFrom is valid */
229941 i64 iFrom /* Advance at least as far as this */
229942){
229943 int bUseFrom = bFrom;
229944 assert( pIter->base.bEof==0 )((void) (0));
229945 while( p->rc==SQLITE_OK0 ){
229946 int iFirst = pIter->aFirst[1].iFirst;
229947 int bNewTerm = 0;
229948 Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
229949 assert( p->rc==SQLITE_OK )((void) (0));
229950 if( bUseFrom && pSeg->pDlidx ){
229951 fts5SegIterNextFrom(p, pSeg, iFrom);
229952 }else{
229953 pSeg->xNext(p, pSeg, &bNewTerm);
229954 }
229955
229956 if( pSeg->pLeaf==0 || bNewTerm
229957 || fts5MultiIterAdvanceRowid(pIter, iFirst, &pSeg)
229958 ){
229959 fts5MultiIterAdvanced(p, pIter, iFirst, 1);
229960 fts5MultiIterSetEof(pIter);
229961 pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
229962 if( pSeg->pLeaf==0 ) return;
229963 }
229964
229965 fts5AssertMultiIterSetup(p, pIter);
229966 assert( pSeg==&pIter->aSeg[pIter->aFirst[1].iFirst] && pSeg->pLeaf )((void) (0));
229967 if( pIter->bSkipEmpty==0 || pSeg->nPos ){
229968 pIter->xSetOutputs(pIter, pSeg);
229969 return;
229970 }
229971 bUseFrom = 0;
229972 }
229973}
229974
229975static void fts5MultiIterNext2(
229976 Fts5Index *p,
229977 Fts5Iter *pIter,
229978 int *pbNewTerm /* OUT: True if *might* be new term */
229979){
229980 assert( pIter->bSkipEmpty )((void) (0));
229981 if( p->rc==SQLITE_OK0 ){
229982 *pbNewTerm = 0;
229983 do{
229984 int iFirst = pIter->aFirst[1].iFirst;
229985 Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
229986 int bNewTerm = 0;
229987
229988 assert( p->rc==SQLITE_OK )((void) (0));
229989 pSeg->xNext(p, pSeg, &bNewTerm);
229990 if( pSeg->pLeaf==0 || bNewTerm
229991 || fts5MultiIterAdvanceRowid(pIter, iFirst, &pSeg)
229992 ){
229993 fts5MultiIterAdvanced(p, pIter, iFirst, 1);
229994 fts5MultiIterSetEof(pIter);
229995 *pbNewTerm = 1;
229996 }
229997 fts5AssertMultiIterSetup(p, pIter);
229998
229999 }while( fts5MultiIterIsEmpty(p, pIter) );
230000 }
230001}
230002
230003static void fts5IterSetOutputs_Noop(Fts5Iter *pUnused1, Fts5SegIter *pUnused2){
230004 UNUSED_PARAM2(pUnused1, pUnused2)(void)(pUnused1), (void)(pUnused2);
230005}
230006
230007static Fts5Iter *fts5MultiIterAlloc(
230008 Fts5Index *p, /* FTS5 backend to iterate within */
230009 int nSeg
230010){
230011 Fts5Iter *pNew;
230012 int nSlot; /* Power of two >= nSeg */
230013
230014 for(nSlot=2; nSlot<nSeg; nSlot=nSlot*2);
230015 pNew = fts5IdxMalloc(p,
230016 sizeof(Fts5Iter) + /* pNew */
230017 sizeof(Fts5SegIter) * (nSlot-1) + /* pNew->aSeg[] */
230018 sizeof(Fts5CResult) * nSlot /* pNew->aFirst[] */
230019 );
230020 if( pNew ){
230021 pNew->nSeg = nSlot;
230022 pNew->aFirst = (Fts5CResult*)&pNew->aSeg[nSlot];
230023 pNew->pIndex = p;
230024 pNew->xSetOutputs = fts5IterSetOutputs_Noop;
230025 }
230026 return pNew;
230027}
230028
230029static void fts5PoslistCallback(
230030 Fts5Index *pUnused,
230031 void *pContext,
230032 const u8 *pChunk, int nChunk
230033){
230034 UNUSED_PARAM(pUnused)(void)(pUnused);
230035 assert_nc( nChunk>=0 )((void) (0));
230036 if( nChunk>0 ){
230037 fts5BufferSafeAppendBlob((Fts5Buffer*)pContext, pChunk, nChunk){ ((void) (0)); memcpy(&((Fts5Buffer*)pContext)->p[((Fts5Buffer
*)pContext)->n], pChunk, nChunk); ((Fts5Buffer*)pContext)->
n += nChunk; }
;
230038 }
230039}
230040
230041typedef struct PoslistCallbackCtx PoslistCallbackCtx;
230042struct PoslistCallbackCtx {
230043 Fts5Buffer *pBuf; /* Append to this buffer */
230044 Fts5Colset *pColset; /* Restrict matches to this column */
230045 int eState; /* See above */
230046};
230047
230048typedef struct PoslistOffsetsCtx PoslistOffsetsCtx;
230049struct PoslistOffsetsCtx {
230050 Fts5Buffer *pBuf; /* Append to this buffer */
230051 Fts5Colset *pColset; /* Restrict matches to this column */
230052 int iRead;
230053 int iWrite;
230054};
230055
230056/*
230057** TODO: Make this more efficient!
230058*/
230059static int fts5IndexColsetTest(Fts5Colset *pColset, int iCol){
230060 int i;
230061 for(i=0; i<pColset->nCol; i++){
230062 if( pColset->aiCol[i]==iCol ) return 1;
230063 }
230064 return 0;
230065}
230066
230067static void fts5PoslistOffsetsCallback(
230068 Fts5Index *pUnused,
230069 void *pContext,
230070 const u8 *pChunk, int nChunk
230071){
230072 PoslistOffsetsCtx *pCtx = (PoslistOffsetsCtx*)pContext;
230073 UNUSED_PARAM(pUnused)(void)(pUnused);
230074 assert_nc( nChunk>=0 )((void) (0));
230075 if( nChunk>0 ){
230076 int i = 0;
230077 while( i<nChunk ){
230078 int iVal;
230079 i += fts5GetVarint32(&pChunk[i], iVal)sqlite3Fts5GetVarint32(&pChunk[i],(u32*)&iVal);
230080 iVal += pCtx->iRead - 2;
230081 pCtx->iRead = iVal;
230082 if( fts5IndexColsetTest(pCtx->pColset, iVal) ){
230083 fts5BufferSafeAppendVarint(pCtx->pBuf, iVal + 2 - pCtx->iWrite){ (pCtx->pBuf)->n += sqlite3Fts5PutVarint(&(pCtx->
pBuf)->p[(pCtx->pBuf)->n], (iVal + 2 - pCtx->iWrite
)); ((void) (0)); }
;
230084 pCtx->iWrite = iVal;
230085 }
230086 }
230087 }
230088}
230089
230090static void fts5PoslistFilterCallback(
230091 Fts5Index *pUnused,
230092 void *pContext,
230093 const u8 *pChunk, int nChunk
230094){
230095 PoslistCallbackCtx *pCtx = (PoslistCallbackCtx*)pContext;
230096 UNUSED_PARAM(pUnused)(void)(pUnused);
230097 assert_nc( nChunk>=0 )((void) (0));
230098 if( nChunk>0 ){
230099 /* Search through to find the first varint with value 1. This is the
230100 ** start of the next columns hits. */
230101 int i = 0;
230102 int iStart = 0;
230103
230104 if( pCtx->eState==2 ){
230105 int iCol;
230106 fts5FastGetVarint32(pChunk, i, iCol){ iCol = (pChunk)[i++]; if( iCol & 0x80 ){ i--; i += sqlite3Fts5GetVarint32
(&(pChunk)[i],(u32*)&iCol); } }
;
230107 if( fts5IndexColsetTest(pCtx->pColset, iCol) ){
230108 pCtx->eState = 1;
230109 fts5BufferSafeAppendVarint(pCtx->pBuf, 1){ (pCtx->pBuf)->n += sqlite3Fts5PutVarint(&(pCtx->
pBuf)->p[(pCtx->pBuf)->n], (1)); ((void) (0)); }
;
230110 }else{
230111 pCtx->eState = 0;
230112 }
230113 }
230114
230115 do {
230116 while( i<nChunk && pChunk[i]!=0x01 ){
230117 while( pChunk[i] & 0x80 ) i++;
230118 i++;
230119 }
230120 if( pCtx->eState ){
230121 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; }
;
230122 }
230123 if( i<nChunk ){
230124 int iCol;
230125 iStart = i;
230126 i++;
230127 if( i>=nChunk ){
230128 pCtx->eState = 2;
230129 }else{
230130 fts5FastGetVarint32(pChunk, i, iCol){ iCol = (pChunk)[i++]; if( iCol & 0x80 ){ i--; i += sqlite3Fts5GetVarint32
(&(pChunk)[i],(u32*)&iCol); } }
;
230131 pCtx->eState = fts5IndexColsetTest(pCtx->pColset, iCol);
230132 if( pCtx->eState ){
230133 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; }
;
230134 iStart = i;
230135 }
230136 }
230137 }
230138 }while( i<nChunk );
230139 }
230140}
230141
230142static void fts5ChunkIterate(
230143 Fts5Index *p, /* Index object */
230144 Fts5SegIter *pSeg, /* Poslist of this iterator */
230145 void *pCtx, /* Context pointer for xChunk callback */
230146 void (*xChunk)(Fts5Index*, void*, const u8*, int)
230147){
230148 int nRem = pSeg->nPos; /* Number of bytes still to come */
230149 Fts5Data *pData = 0;
230150 u8 *pChunk = &pSeg->pLeaf->p[pSeg->iLeafOffset];
230151 int nChunk = MIN(nRem, pSeg->pLeaf->szLeaf - pSeg->iLeafOffset)((nRem)<(pSeg->pLeaf->szLeaf - pSeg->iLeafOffset)
?(nRem):(pSeg->pLeaf->szLeaf - pSeg->iLeafOffset))
;
230152 int pgno = pSeg->iLeafPgno;
230153 int pgnoSave = 0;
230154
230155 /* This function does not work with detail=none databases. */
230156 assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE )((void) (0));
230157
230158 if( (pSeg->flags & FTS5_SEGITER_REVERSE0x02)==0 ){
230159 pgnoSave = pgno+1;
230160 }
230161
230162 while( 1 ){
230163 xChunk(p, pCtx, pChunk, nChunk);
230164 nRem -= nChunk;
230165 fts5DataRelease(pData);
230166 if( nRem<=0 ){
230167 break;
230168 }else if( pSeg->pSeg==0 ){
230169 p->rc = FTS5_CORRUPT(11 | (1<<8));
230170 return;
230171 }else{
230172 pgno++;
230173 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
)) )
);
230174 if( pData==0 ) break;
230175 pChunk = &pData->p[4];
230176 nChunk = MIN(nRem, pData->szLeaf - 4)((nRem)<(pData->szLeaf - 4)?(nRem):(pData->szLeaf - 4
))
;
230177 if( pgno==pgnoSave ){
230178 assert( pSeg->pNextLeaf==0 )((void) (0));
230179 pSeg->pNextLeaf = pData;
230180 pData = 0;
230181 }
230182 }
230183 }
230184}
230185
230186/*
230187** Iterator pIter currently points to a valid entry (not EOF). This
230188** function appends the position list data for the current entry to
230189** buffer pBuf. It does not make a copy of the position-list size
230190** field.
230191*/
230192static void fts5SegiterPoslist(
230193 Fts5Index *p,
230194 Fts5SegIter *pSeg,
230195 Fts5Colset *pColset,
230196 Fts5Buffer *pBuf
230197){
230198 assert( pBuf!=0 )((void) (0));
230199 assert( pSeg!=0 )((void) (0));
230200 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) )
){
230201 assert( pBuf->p!=0 )((void) (0));
230202 assert( pBuf->nSpace >= pBuf->n+pSeg->nPos+FTS5_DATA_ZERO_PADDING )((void) (0));
230203 memset(&pBuf->p[pBuf->n+pSeg->nPos], 0, FTS5_DATA_ZERO_PADDING8);
230204 if( pColset==0 ){
230205 fts5ChunkIterate(p, pSeg, (void*)pBuf, fts5PoslistCallback);
230206 }else{
230207 if( p->pConfig->eDetail==FTS5_DETAIL_FULL0 ){
230208 PoslistCallbackCtx sCtx;
230209 sCtx.pBuf = pBuf;
230210 sCtx.pColset = pColset;
230211 sCtx.eState = fts5IndexColsetTest(pColset, 0);
230212 assert( sCtx.eState==0 || sCtx.eState==1 )((void) (0));
230213 fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistFilterCallback);
230214 }else{
230215 PoslistOffsetsCtx sCtx;
230216 memset(&sCtx, 0, sizeof(sCtx));
230217 sCtx.pBuf = pBuf;
230218 sCtx.pColset = pColset;
230219 fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistOffsetsCallback);
230220 }
230221 }
230222 }
230223}
230224
230225/*
230226** Parameter pPos points to a buffer containing a position list, size nPos.
230227** This function filters it according to pColset (which must be non-NULL)
230228** and sets pIter->base.pData/nData to point to the new position list.
230229** If memory is required for the new position list, use buffer pIter->poslist.
230230** Or, if the new position list is a contiguous subset of the input, set
230231** pIter->base.pData/nData to point directly to it.
230232**
230233** This function is a no-op if *pRc is other than SQLITE_OK when it is
230234** called. If an OOM error is encountered, *pRc is set to SQLITE_NOMEM
230235** before returning.
230236*/
230237static void fts5IndexExtractColset(
230238 int *pRc,
230239 Fts5Colset *pColset, /* Colset to filter on */
230240 const u8 *pPos, int nPos, /* Position list */
230241 Fts5Iter *pIter
230242){
230243 if( *pRc==SQLITE_OK0 ){
230244 const u8 *p = pPos;
230245 const u8 *aCopy = p;
230246 const u8 *pEnd = &p[nPos]; /* One byte past end of position list */
230247 int i = 0;
230248 int iCurrent = 0;
230249
230250 if( pColset->nCol>1 && sqlite3Fts5BufferSize(pRc, &pIter->poslist, nPos) ){
230251 return;
230252 }
230253
230254 while( 1 ){
230255 while( pColset->aiCol[i]<iCurrent ){
230256 i++;
230257 if( i==pColset->nCol ){
230258 pIter->base.pData = pIter->poslist.p;
230259 pIter->base.nData = pIter->poslist.n;
230260 return;
230261 }
230262 }
230263
230264 /* Advance pointer p until it points to pEnd or an 0x01 byte that is
230265 ** not part of a varint */
230266 while( p<pEnd && *p!=0x01 ){
230267 while( *p++ & 0x80 );
230268 }
230269
230270 if( pColset->aiCol[i]==iCurrent ){
230271 if( pColset->nCol==1 ){
230272 pIter->base.pData = aCopy;
230273 pIter->base.nData = p-aCopy;
230274 return;
230275 }
230276 fts5BufferSafeAppendBlob(&pIter->poslist, aCopy, p-aCopy){ ((void) (0)); memcpy(&(&pIter->poslist)->p[(&
pIter->poslist)->n], aCopy, p-aCopy); (&pIter->poslist
)->n += p-aCopy; }
;
230277 }
230278 if( p>=pEnd ){
230279 pIter->base.pData = pIter->poslist.p;
230280 pIter->base.nData = pIter->poslist.n;
230281 return;
230282 }
230283 aCopy = p++;
230284 iCurrent = *p++;
230285 if( iCurrent & 0x80 ){
230286 p--;
230287 p += fts5GetVarint32(p, iCurrent)sqlite3Fts5GetVarint32(p,(u32*)&iCurrent);
230288 }
230289 }
230290 }
230291
230292}
230293
230294/*
230295** xSetOutputs callback used by detail=none tables.
230296*/
230297static void fts5IterSetOutputs_None(Fts5Iter *pIter, Fts5SegIter *pSeg){
230298 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_NONE )((void) (0));
230299 pIter->base.iRowid = pSeg->iRowid;
230300 pIter->base.nData = pSeg->nPos;
230301}
230302
230303/*
230304** xSetOutputs callback used by detail=full and detail=col tables when no
230305** column filters are specified.
230306*/
230307static void fts5IterSetOutputs_Nocolset(Fts5Iter *pIter, Fts5SegIter *pSeg){
230308 pIter->base.iRowid = pSeg->iRowid;
230309 pIter->base.nData = pSeg->nPos;
230310
230311 assert( pIter->pIndex->pConfig->eDetail!=FTS5_DETAIL_NONE )((void) (0));
230312 assert( pIter->pColset==0 )((void) (0));
230313
230314 if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
230315 /* All data is stored on the current page. Populate the output
230316 ** variables to point into the body of the page object. */
230317 pIter->base.pData = &pSeg->pLeaf->p[pSeg->iLeafOffset];
230318 }else{
230319 /* The data is distributed over two or more pages. Copy it into the
230320 ** Fts5Iter.poslist buffer and then set the output pointer to point
230321 ** to this buffer. */
230322 fts5BufferZero(&pIter->poslist)sqlite3Fts5BufferZero(&pIter->poslist);
230323 fts5SegiterPoslist(pIter->pIndex, pSeg, 0, &pIter->poslist);
230324 pIter->base.pData = pIter->poslist.p;
230325 }
230326}
230327
230328/*
230329** xSetOutputs callback used when the Fts5Colset object has nCol==0 (match
230330** against no columns at all).
230331*/
230332static void fts5IterSetOutputs_ZeroColset(Fts5Iter *pIter, Fts5SegIter *pSeg){
230333 UNUSED_PARAM(pSeg)(void)(pSeg);
230334 pIter->base.nData = 0;
230335}
230336
230337/*
230338** xSetOutputs callback used by detail=col when there is a column filter
230339** and there are 100 or more columns. Also called as a fallback from
230340** fts5IterSetOutputs_Col100 if the column-list spans more than one page.
230341*/
230342static void fts5IterSetOutputs_Col(Fts5Iter *pIter, Fts5SegIter *pSeg){
230343 fts5BufferZero(&pIter->poslist)sqlite3Fts5BufferZero(&pIter->poslist);
230344 fts5SegiterPoslist(pIter->pIndex, pSeg, pIter->pColset, &pIter->poslist);
230345 pIter->base.iRowid = pSeg->iRowid;
230346 pIter->base.pData = pIter->poslist.p;
230347 pIter->base.nData = pIter->poslist.n;
230348}
230349
230350/*
230351** xSetOutputs callback used when:
230352**
230353** * detail=col,
230354** * there is a column filter, and
230355** * the table contains 100 or fewer columns.
230356**
230357** The last point is to ensure all column numbers are stored as
230358** single-byte varints.
230359*/
230360static void fts5IterSetOutputs_Col100(Fts5Iter *pIter, Fts5SegIter *pSeg){
230361
230362 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_COLUMNS )((void) (0));
230363 assert( pIter->pColset )((void) (0));
230364
230365 if( pSeg->iLeafOffset+pSeg->nPos>pSeg->pLeaf->szLeaf ){
230366 fts5IterSetOutputs_Col(pIter, pSeg);
230367 }else{
230368 u8 *a = (u8*)&pSeg->pLeaf->p[pSeg->iLeafOffset];
230369 u8 *pEnd = (u8*)&a[pSeg->nPos];
230370 int iPrev = 0;
230371 int *aiCol = pIter->pColset->aiCol;
230372 int *aiColEnd = &aiCol[pIter->pColset->nCol];
230373
230374 u8 *aOut = pIter->poslist.p;
230375 int iPrevOut = 0;
230376
230377 pIter->base.iRowid = pSeg->iRowid;
230378
230379 while( a<pEnd ){
230380 iPrev += (int)a++[0] - 2;
230381 while( *aiCol<iPrev ){
230382 aiCol++;
230383 if( aiCol==aiColEnd ) goto setoutputs_col_out;
230384 }
230385 if( *aiCol==iPrev ){
230386 *aOut++ = (u8)((iPrev - iPrevOut) + 2);
230387 iPrevOut = iPrev;
230388 }
230389 }
230390
230391setoutputs_col_out:
230392 pIter->base.pData = pIter->poslist.p;
230393 pIter->base.nData = aOut - pIter->poslist.p;
230394 }
230395}
230396
230397/*
230398** xSetOutputs callback used by detail=full when there is a column filter.
230399*/
230400static void fts5IterSetOutputs_Full(Fts5Iter *pIter, Fts5SegIter *pSeg){
230401 Fts5Colset *pColset = pIter->pColset;
230402 pIter->base.iRowid = pSeg->iRowid;
230403
230404 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_FULL )((void) (0));
230405 assert( pColset )((void) (0));
230406
230407 if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
230408 /* All data is stored on the current page. Populate the output
230409 ** variables to point into the body of the page object. */
230410 const u8 *a = &pSeg->pLeaf->p[pSeg->iLeafOffset];
230411 int *pRc = &pIter->pIndex->rc;
230412 fts5BufferZero(&pIter->poslist)sqlite3Fts5BufferZero(&pIter->poslist);
230413 fts5IndexExtractColset(pRc, pColset, a, pSeg->nPos, pIter);
230414 }else{
230415 /* The data is distributed over two or more pages. Copy it into the
230416 ** Fts5Iter.poslist buffer and then set the output pointer to point
230417 ** to this buffer. */
230418 fts5BufferZero(&pIter->poslist)sqlite3Fts5BufferZero(&pIter->poslist);
230419 fts5SegiterPoslist(pIter->pIndex, pSeg, pColset, &pIter->poslist);
230420 pIter->base.pData = pIter->poslist.p;
230421 pIter->base.nData = pIter->poslist.n;
230422 }
230423}
230424
230425static void fts5IterSetOutputCb(int *pRc, Fts5Iter *pIter){
230426 assert( pIter!=0 || (*pRc)!=SQLITE_OK )((void) (0));
230427 if( *pRc==SQLITE_OK0 ){
230428 Fts5Config *pConfig = pIter->pIndex->pConfig;
230429 if( pConfig->eDetail==FTS5_DETAIL_NONE1 ){
230430 pIter->xSetOutputs = fts5IterSetOutputs_None;
230431 }
230432
230433 else if( pIter->pColset==0 ){
230434 pIter->xSetOutputs = fts5IterSetOutputs_Nocolset;
230435 }
230436
230437 else if( pIter->pColset->nCol==0 ){
230438 pIter->xSetOutputs = fts5IterSetOutputs_ZeroColset;
230439 }
230440
230441 else if( pConfig->eDetail==FTS5_DETAIL_FULL0 ){
230442 pIter->xSetOutputs = fts5IterSetOutputs_Full;
230443 }
230444
230445 else{
230446 assert( pConfig->eDetail==FTS5_DETAIL_COLUMNS )((void) (0));
230447 if( pConfig->nCol<=100 ){
230448 pIter->xSetOutputs = fts5IterSetOutputs_Col100;
230449 sqlite3Fts5BufferSize(pRc, &pIter->poslist, pConfig->nCol);
230450 }else{
230451 pIter->xSetOutputs = fts5IterSetOutputs_Col;
230452 }
230453 }
230454 }
230455}
230456
230457
230458/*
230459** Allocate a new Fts5Iter object.
230460**
230461** The new object will be used to iterate through data in structure pStruct.
230462** If iLevel is -ve, then all data in all segments is merged. Or, if iLevel
230463** is zero or greater, data from the first nSegment segments on level iLevel
230464** is merged.
230465**
230466** The iterator initially points to the first term/rowid entry in the
230467** iterated data.
230468*/
230469static void fts5MultiIterNew(
230470 Fts5Index *p, /* FTS5 backend to iterate within */
230471 Fts5Structure *pStruct, /* Structure of specific index */
230472 int flags, /* FTS5INDEX_QUERY_XXX flags */
230473 Fts5Colset *pColset, /* Colset to filter on (or NULL) */
230474 const u8 *pTerm, int nTerm, /* Term to seek to (or NULL/0) */
230475 int iLevel, /* Level to iterate (-1 for all) */
230476 int nSegment, /* Number of segments to merge (iLevel>=0) */
230477 Fts5Iter **ppOut /* New object */
230478){
230479 int nSeg = 0; /* Number of segment-iters in use */
230480 int iIter = 0; /* */
230481 int iSeg; /* Used to iterate through segments */
230482 Fts5StructureLevel *pLvl;
230483 Fts5Iter *pNew;
230484
230485 assert( (pTerm==0 && nTerm==0) || iLevel<0 )((void) (0));
230486
230487 /* Allocate space for the new multi-seg-iterator. */
230488 if( p->rc==SQLITE_OK0 ){
230489 if( iLevel<0 ){
230490 assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) )((void) (0));
230491 nSeg = pStruct->nSegment;
230492 nSeg += (p->pHash ? 1 : 0);
230493 }else{
230494 nSeg = MIN(pStruct->aLevel[iLevel].nSeg, nSegment)((pStruct->aLevel[iLevel].nSeg)<(nSegment)?(pStruct->
aLevel[iLevel].nSeg):(nSegment))
;
230495 }
230496 }
230497 *ppOut = pNew = fts5MultiIterAlloc(p, nSeg);
230498 if( pNew==0 ){
230499 assert( p->rc!=SQLITE_OK )((void) (0));
230500 goto fts5MultiIterNew_post_check;
230501 }
230502 pNew->bRev = (0!=(flags & FTS5INDEX_QUERY_DESC0x0002));
230503 pNew->bSkipEmpty = (0!=(flags & FTS5INDEX_QUERY_SKIPEMPTY0x0010));
230504 pNew->pColset = pColset;
230505 if( (flags & FTS5INDEX_QUERY_NOOUTPUT0x0020)==0 ){
230506 fts5IterSetOutputCb(&p->rc, pNew);
230507 }
230508
230509 /* Initialize each of the component segment iterators. */
230510 if( p->rc==SQLITE_OK0 ){
230511 if( iLevel<0 ){
230512 Fts5StructureLevel *pEnd = &pStruct->aLevel[pStruct->nLevel];
230513 if( p->pHash ){
230514 /* Add a segment iterator for the current contents of the hash table. */
230515 Fts5SegIter *pIter = &pNew->aSeg[iIter++];
230516 fts5SegIterHashInit(p, pTerm, nTerm, flags, pIter);
230517 }
230518 for(pLvl=&pStruct->aLevel[0]; pLvl<pEnd; pLvl++){
230519 for(iSeg=pLvl->nSeg-1; iSeg>=0; iSeg--){
230520 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
230521 Fts5SegIter *pIter = &pNew->aSeg[iIter++];
230522 if( pTerm==0 ){
230523 fts5SegIterInit(p, pSeg, pIter);
230524 }else{
230525 fts5SegIterSeekInit(p, pTerm, nTerm, flags, pSeg, pIter);
230526 }
230527 }
230528 }
230529 }else{
230530 pLvl = &pStruct->aLevel[iLevel];
230531 for(iSeg=nSeg-1; iSeg>=0; iSeg--){
230532 fts5SegIterInit(p, &pLvl->aSeg[iSeg], &pNew->aSeg[iIter++]);
230533 }
230534 }
230535 assert( iIter==nSeg )((void) (0));
230536 }
230537
230538 /* If the above was successful, each component iterators now points
230539 ** to the first entry in its segment. In this case initialize the
230540 ** aFirst[] array. Or, if an error has occurred, free the iterator
230541 ** object and set the output variable to NULL. */
230542 if( p->rc==SQLITE_OK0 ){
230543 for(iIter=pNew->nSeg-1; iIter>0; iIter--){
230544 int iEq;
230545 if( (iEq = fts5MultiIterDoCompare(pNew, iIter)) ){
230546 Fts5SegIter *pSeg = &pNew->aSeg[iEq];
230547 if( p->rc==SQLITE_OK0 ) pSeg->xNext(p, pSeg, 0);
230548 fts5MultiIterAdvanced(p, pNew, iEq, iIter);
230549 }
230550 }
230551 fts5MultiIterSetEof(pNew);
230552 fts5AssertMultiIterSetup(p, pNew);
230553
230554 if( pNew->bSkipEmpty && fts5MultiIterIsEmpty(p, pNew) ){
230555 fts5MultiIterNext(p, pNew, 0, 0);
230556 }else if( pNew->base.bEof==0 ){
230557 Fts5SegIter *pSeg = &pNew->aSeg[pNew->aFirst[1].iFirst];
230558 pNew->xSetOutputs(pNew, pSeg);
230559 }
230560
230561 }else{
230562 fts5MultiIterFree(pNew);
230563 *ppOut = 0;
230564 }
230565
230566fts5MultiIterNew_post_check:
230567 assert( (*ppOut)!=0 || p->rc!=SQLITE_OK )((void) (0));
230568 return;
230569}
230570
230571/*
230572** Create an Fts5Iter that iterates through the doclist provided
230573** as the second argument.
230574*/
230575static void fts5MultiIterNew2(
230576 Fts5Index *p, /* FTS5 backend to iterate within */
230577 Fts5Data *pData, /* Doclist to iterate through */
230578 int bDesc, /* True for descending rowid order */
230579 Fts5Iter **ppOut /* New object */
230580){
230581 Fts5Iter *pNew;
230582 pNew = fts5MultiIterAlloc(p, 2);
230583 if( pNew ){
230584 Fts5SegIter *pIter = &pNew->aSeg[1];
230585
230586 pIter->flags = FTS5_SEGITER_ONETERM0x01;
230587 if( pData->szLeaf>0 ){
230588 pIter->pLeaf = pData;
230589 pIter->iLeafOffset = fts5GetVarintsqlite3Fts5GetVarint(pData->p, (u64*)&pIter->iRowid);
230590 pIter->iEndofDoclist = pData->nn;
230591 pNew->aFirst[1].iFirst = 1;
230592 if( bDesc ){
230593 pNew->bRev = 1;
230594 pIter->flags |= FTS5_SEGITER_REVERSE0x02;
230595 fts5SegIterReverseInitPage(p, pIter);
230596 }else{
230597 fts5SegIterLoadNPos(p, pIter);
230598 }
230599 pData = 0;
230600 }else{
230601 pNew->base.bEof = 1;
230602 }
230603 fts5SegIterSetNext(p, pIter);
230604
230605 *ppOut = pNew;
230606 }
230607
230608 fts5DataRelease(pData);
230609}
230610
230611/*
230612** Return true if the iterator is at EOF or if an error has occurred.
230613** False otherwise.
230614*/
230615static int fts5MultiIterEof(Fts5Index *p, Fts5Iter *pIter){
230616 assert( pIter!=0 || p->rc!=SQLITE_OK )((void) (0));
230617 assert( p->rc!=SQLITE_OK((void) (0))
230618 || (pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf==0)==pIter->base.bEof((void) (0))
230619 )((void) (0));
230620 return (p->rc || pIter->base.bEof);
230621}
230622
230623/*
230624** Return the rowid of the entry that the iterator currently points
230625** to. If the iterator points to EOF when this function is called the
230626** results are undefined.
230627*/
230628static i64 fts5MultiIterRowid(Fts5Iter *pIter){
230629 assert( pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf )((void) (0));
230630 return pIter->aSeg[ pIter->aFirst[1].iFirst ].iRowid;
230631}
230632
230633/*
230634** Move the iterator to the next entry at or following iMatch.
230635*/
230636static void fts5MultiIterNextFrom(
230637 Fts5Index *p,
230638 Fts5Iter *pIter,
230639 i64 iMatch
230640){
230641 while( 1 ){
230642 i64 iRowid;
230643 fts5MultiIterNext(p, pIter, 1, iMatch);
230644 if( fts5MultiIterEof(p, pIter) ) break;
230645 iRowid = fts5MultiIterRowid(pIter);
230646 if( pIter->bRev==0 && iRowid>=iMatch ) break;
230647 if( pIter->bRev!=0 && iRowid<=iMatch ) break;
230648 }
230649}
230650
230651/*
230652** Return a pointer to a buffer containing the term associated with the
230653** entry that the iterator currently points to.
230654*/
230655static const u8 *fts5MultiIterTerm(Fts5Iter *pIter, int *pn){
230656 Fts5SegIter *p = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
230657 *pn = p->term.n;
230658 return p->term.p;
230659}
230660
230661/*
230662** Allocate a new segment-id for the structure pStruct. The new segment
230663** id must be between 1 and 65335 inclusive, and must not be used by
230664** any currently existing segment. If a free segment id cannot be found,
230665** SQLITE_FULL is returned.
230666**
230667** If an error has already occurred, this function is a no-op. 0 is
230668** returned in this case.
230669*/
230670static int fts5AllocateSegid(Fts5Index *p, Fts5Structure *pStruct){
230671 int iSegid = 0;
230672
230673 if( p->rc==SQLITE_OK0 ){
230674 if( pStruct->nSegment>=FTS5_MAX_SEGMENT2000 ){
230675 p->rc = SQLITE_FULL13;
230676 }else{
230677 /* FTS5_MAX_SEGMENT is currently defined as 2000. So the following
230678 ** array is 63 elements, or 252 bytes, in size. */
230679 u32 aUsed[(FTS5_MAX_SEGMENT2000+31) / 32];
230680 int iLvl, iSeg;
230681 int i;
230682 u32 mask;
230683 memset(aUsed, 0, sizeof(aUsed));
230684 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
230685 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
230686 int iId = pStruct->aLevel[iLvl].aSeg[iSeg].iSegid;
230687 if( iId<=FTS5_MAX_SEGMENT2000 && iId>0 ){
230688 aUsed[(iId-1) / 32] |= (u32)1 << ((iId-1) % 32);
230689 }
230690 }
230691 }
230692
230693 for(i=0; aUsed[i]==0xFFFFFFFF; i++);
230694 mask = aUsed[i];
230695 for(iSegid=0; mask & ((u32)1 << iSegid); iSegid++);
230696 iSegid += 1 + i*32;
230697
230698#ifdef SQLITE_DEBUG
230699 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
230700 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
230701 assert_nc( iSegid!=pStruct->aLevel[iLvl].aSeg[iSeg].iSegid )((void) (0));
230702 }
230703 }
230704 assert_nc( iSegid>0 && iSegid<=FTS5_MAX_SEGMENT )((void) (0));
230705
230706 {
230707 sqlite3_stmt *pIdxSelect = fts5IdxSelectStmt(p);
230708 if( p->rc==SQLITE_OK0 ){
230709 u8 aBlob[2] = {0xff, 0xff};
230710 sqlite3_bind_int(pIdxSelect, 1, iSegid);
230711 sqlite3_bind_blob(pIdxSelect, 2, aBlob, 2, SQLITE_STATIC((sqlite3_destructor_type)0));
230712 assert_nc( sqlite3_step(pIdxSelect)!=SQLITE_ROW )((void) (0));
230713 p->rc = sqlite3_reset(pIdxSelect);
230714 sqlite3_bind_null(pIdxSelect, 2);
230715 }
230716 }
230717#endif
230718 }
230719 }
230720
230721 return iSegid;
230722}
230723
230724/*
230725** Discard all data currently cached in the hash-tables.
230726*/
230727static void fts5IndexDiscardData(Fts5Index *p){
230728 assert( p->pHash || p->nPendingData==0 )((void) (0));
230729 if( p->pHash ){
230730 sqlite3Fts5HashClear(p->pHash);
230731 p->nPendingData = 0;
230732 }
230733}
230734
230735/*
230736** Return the size of the prefix, in bytes, that buffer
230737** (pNew/<length-unknown>) shares with buffer (pOld/nOld).
230738**
230739** Buffer (pNew/<length-unknown>) is guaranteed to be greater
230740** than buffer (pOld/nOld).
230741*/
230742static int fts5PrefixCompress(int nOld, const u8 *pOld, const u8 *pNew){
230743 int i;
230744 for(i=0; i<nOld; i++){
230745 if( pOld[i]!=pNew[i] ) break;
230746 }
230747 return i;
230748}
230749
230750static void fts5WriteDlidxClear(
230751 Fts5Index *p,
230752 Fts5SegWriter *pWriter,
230753 int bFlush /* If true, write dlidx to disk */
230754){
230755 int i;
230756 assert( bFlush==0 || (pWriter->nDlidx>0 && pWriter->aDlidx[0].buf.n>0) )((void) (0));
230757 for(i=0; i<pWriter->nDlidx; i++){
230758 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[i];
230759 if( pDlidx->buf.n==0 ) break;
230760 if( bFlush ){
230761 assert( pDlidx->pgno!=0 )((void) (0));
230762 fts5DataWrite(p,
230763 FTS5_DLIDX_ROWID(pWriter->iSegid, i, pDlidx->pgno)( ((i64)(pWriter->iSegid) << (31 +5 +1)) + ((i64)(1)
<< (31 + 5)) + ((i64)(i) << (31)) + ((i64)(pDlidx
->pgno)) )
,
230764 pDlidx->buf.p, pDlidx->buf.n
230765 );
230766 }
230767 sqlite3Fts5BufferZero(&pDlidx->buf);
230768 pDlidx->bPrevValid = 0;
230769 }
230770}
230771
230772/*
230773** Grow the pWriter->aDlidx[] array to at least nLvl elements in size.
230774** Any new array elements are zeroed before returning.
230775*/
230776static int fts5WriteDlidxGrow(
230777 Fts5Index *p,
230778 Fts5SegWriter *pWriter,
230779 int nLvl
230780){
230781 if( p->rc==SQLITE_OK0 && nLvl>=pWriter->nDlidx ){
230782 Fts5DlidxWriter *aDlidx = (Fts5DlidxWriter*)sqlite3_realloc64(
230783 pWriter->aDlidx, sizeof(Fts5DlidxWriter) * nLvl
230784 );
230785 if( aDlidx==0 ){
230786 p->rc = SQLITE_NOMEM7;
230787 }else{
230788 size_t nByte = sizeof(Fts5DlidxWriter) * (nLvl - pWriter->nDlidx);
230789 memset(&aDlidx[pWriter->nDlidx], 0, nByte);
230790 pWriter->aDlidx = aDlidx;
230791 pWriter->nDlidx = nLvl;
230792 }
230793 }
230794 return p->rc;
230795}
230796
230797/*
230798** If the current doclist-index accumulating in pWriter->aDlidx[] is large
230799** enough, flush it to disk and return 1. Otherwise discard it and return
230800** zero.
230801*/
230802static int fts5WriteFlushDlidx(Fts5Index *p, Fts5SegWriter *pWriter){
230803 int bFlag = 0;
230804
230805 /* If there were FTS5_MIN_DLIDX_SIZE or more empty leaf pages written
230806 ** to the database, also write the doclist-index to disk. */
230807 if( pWriter->aDlidx[0].buf.n>0 && pWriter->nEmpty>=FTS5_MIN_DLIDX_SIZE4 ){
230808 bFlag = 1;
230809 }
230810 fts5WriteDlidxClear(p, pWriter, bFlag);
230811 pWriter->nEmpty = 0;
230812 return bFlag;
230813}
230814
230815/*
230816** This function is called whenever processing of the doclist for the
230817** last term on leaf page (pWriter->iBtPage) is completed.
230818**
230819** The doclist-index for that term is currently stored in-memory within the
230820** Fts5SegWriter.aDlidx[] array. If it is large enough, this function
230821** writes it out to disk. Or, if it is too small to bother with, discards
230822** it.
230823**
230824** Fts5SegWriter.btterm currently contains the first term on page iBtPage.
230825*/
230826static void fts5WriteFlushBtree(Fts5Index *p, Fts5SegWriter *pWriter){
230827 int bFlag;
230828
230829 assert( pWriter->iBtPage || pWriter->nEmpty==0 )((void) (0));
230830 if( pWriter->iBtPage==0 ) return;
230831 bFlag = fts5WriteFlushDlidx(p, pWriter);
230832
230833 if( p->rc==SQLITE_OK0 ){
230834 const char *z = (pWriter->btterm.n>0?(const char*)pWriter->btterm.p:"");
230835 /* The following was already done in fts5WriteInit(): */
230836 /* sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid); */
230837 sqlite3_bind_blob(p->pIdxWriter, 2, z, pWriter->btterm.n, SQLITE_STATIC((sqlite3_destructor_type)0));
230838 sqlite3_bind_int64(p->pIdxWriter, 3, bFlag + ((i64)pWriter->iBtPage<<1));
230839 sqlite3_step(p->pIdxWriter);
230840 p->rc = sqlite3_reset(p->pIdxWriter);
230841 sqlite3_bind_null(p->pIdxWriter, 2);
230842 }
230843 pWriter->iBtPage = 0;
230844}
230845
230846/*
230847** This is called once for each leaf page except the first that contains
230848** at least one term. Argument (nTerm/pTerm) is the split-key - a term that
230849** is larger than all terms written to earlier leaves, and equal to or
230850** smaller than the first term on the new leaf.
230851**
230852** If an error occurs, an error code is left in Fts5Index.rc. If an error
230853** has already occurred when this function is called, it is a no-op.
230854*/
230855static void fts5WriteBtreeTerm(
230856 Fts5Index *p, /* FTS5 backend object */
230857 Fts5SegWriter *pWriter, /* Writer object */
230858 int nTerm, const u8 *pTerm /* First term on new page */
230859){
230860 fts5WriteFlushBtree(p, pWriter);
230861 if( p->rc==SQLITE_OK0 ){
230862 fts5BufferSet(&p->rc, &pWriter->btterm, nTerm, pTerm)sqlite3Fts5BufferSet(&p->rc,&pWriter->btterm,nTerm
,pTerm)
;
230863 pWriter->iBtPage = pWriter->writer.pgno;
230864 }
230865}
230866
230867/*
230868** This function is called when flushing a leaf page that contains no
230869** terms at all to disk.
230870*/
230871static void fts5WriteBtreeNoTerm(
230872 Fts5Index *p, /* FTS5 backend object */
230873 Fts5SegWriter *pWriter /* Writer object */
230874){
230875 /* If there were no rowids on the leaf page either and the doclist-index
230876 ** has already been started, append an 0x00 byte to it. */
230877 if( pWriter->bFirstRowidInPage && pWriter->aDlidx[0].buf.n>0 ){
230878 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[0];
230879 assert( pDlidx->bPrevValid )((void) (0));
230880 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, 0);
230881 }
230882
230883 /* Increment the "number of sequential leaves without a term" counter. */
230884 pWriter->nEmpty++;
230885}
230886
230887static i64 fts5DlidxExtractFirstRowid(Fts5Buffer *pBuf){
230888 i64 iRowid;
230889 int iOff;
230890
230891 iOff = 1 + fts5GetVarintsqlite3Fts5GetVarint(&pBuf->p[1], (u64*)&iRowid);
230892 fts5GetVarintsqlite3Fts5GetVarint(&pBuf->p[iOff], (u64*)&iRowid);
230893 return iRowid;
230894}
230895
230896/*
230897** Rowid iRowid has just been appended to the current leaf page. It is the
230898** first on the page. This function appends an appropriate entry to the current
230899** doclist-index.
230900*/
230901static void fts5WriteDlidxAppend(
230902 Fts5Index *p,
230903 Fts5SegWriter *pWriter,
230904 i64 iRowid
230905){
230906 int i;
230907 int bDone = 0;
230908
230909 for(i=0; p->rc==SQLITE_OK0 && bDone==0; i++){
230910 i64 iVal;
230911 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[i];
230912
230913 if( pDlidx->buf.n>=p->pConfig->pgsz ){
230914 /* The current doclist-index page is full. Write it to disk and push
230915 ** a copy of iRowid (which will become the first rowid on the next
230916 ** doclist-index leaf page) up into the next level of the b-tree
230917 ** hierarchy. If the node being flushed is currently the root node,
230918 ** also push its first rowid upwards. */
230919 pDlidx->buf.p[0] = 0x01; /* Not the root node */
230920 fts5DataWrite(p,
230921 FTS5_DLIDX_ROWID(pWriter->iSegid, i, pDlidx->pgno)( ((i64)(pWriter->iSegid) << (31 +5 +1)) + ((i64)(1)
<< (31 + 5)) + ((i64)(i) << (31)) + ((i64)(pDlidx
->pgno)) )
,
230922 pDlidx->buf.p, pDlidx->buf.n
230923 );
230924 fts5WriteDlidxGrow(p, pWriter, i+2);
230925 pDlidx = &pWriter->aDlidx[i];
230926 if( p->rc==SQLITE_OK0 && pDlidx[1].buf.n==0 ){
230927 i64 iFirst = fts5DlidxExtractFirstRowid(&pDlidx->buf);
230928
230929 /* This was the root node. Push its first rowid up to the new root. */
230930 pDlidx[1].pgno = pDlidx->pgno;
230931 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, 0);
230932 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, pDlidx->pgno);
230933 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, iFirst);
230934 pDlidx[1].bPrevValid = 1;
230935 pDlidx[1].iPrev = iFirst;
230936 }
230937
230938 sqlite3Fts5BufferZero(&pDlidx->buf);
230939 pDlidx->bPrevValid = 0;
230940 pDlidx->pgno++;
230941 }else{
230942 bDone = 1;
230943 }
230944
230945 if( pDlidx->bPrevValid ){
230946 iVal = iRowid - pDlidx->iPrev;
230947 }else{
230948 i64 iPgno = (i==0 ? pWriter->writer.pgno : pDlidx[-1].pgno);
230949 assert( pDlidx->buf.n==0 )((void) (0));
230950 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, !bDone);
230951 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, iPgno);
230952 iVal = iRowid;
230953 }
230954
230955 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, iVal);
230956 pDlidx->bPrevValid = 1;
230957 pDlidx->iPrev = iRowid;
230958 }
230959}
230960
230961static void fts5WriteFlushLeaf(Fts5Index *p, Fts5SegWriter *pWriter){
230962 static const u8 zero[] = { 0x00, 0x00, 0x00, 0x00 };
230963 Fts5PageWriter *pPage = &pWriter->writer;
230964 i64 iRowid;
230965
230966 assert( (pPage->pgidx.n==0)==(pWriter->bFirstTermInPage) )((void) (0));
230967
230968 /* Set the szLeaf header field. */
230969 assert( 0==fts5GetU16(&pPage->buf.p[2]) )((void) (0));
230970 fts5PutU16(&pPage->buf.p[2], (u16)pPage->buf.n);
230971
230972 if( pWriter->bFirstTermInPage ){
230973 /* No term was written to this page. */
230974 assert( pPage->pgidx.n==0 )((void) (0));
230975 fts5WriteBtreeNoTerm(p, pWriter);
230976 }else{
230977 /* Append the pgidx to the page buffer. Set the szLeaf header field. */
230978 fts5BufferAppendBlob(&p->rc, &pPage->buf, pPage->pgidx.n, pPage->pgidx.p)sqlite3Fts5BufferAppendBlob(&p->rc,&pPage->buf,
pPage->pgidx.n,pPage->pgidx.p)
;
230979 }
230980
230981 /* Write the page out to disk */
230982 iRowid = FTS5_SEGMENT_ROWID(pWriter->iSegid, pPage->pgno)( ((i64)(pWriter->iSegid) << (31 +5 +1)) + ((i64)(0)
<< (31 + 5)) + ((i64)(0) << (31)) + ((i64)(pPage
->pgno)) )
;
230983 fts5DataWrite(p, iRowid, pPage->buf.p, pPage->buf.n);
230984
230985 /* Initialize the next page. */
230986 fts5BufferZero(&pPage->buf)sqlite3Fts5BufferZero(&pPage->buf);
230987 fts5BufferZero(&pPage->pgidx)sqlite3Fts5BufferZero(&pPage->pgidx);
230988 fts5BufferAppendBlob(&p->rc, &pPage->buf, 4, zero)sqlite3Fts5BufferAppendBlob(&p->rc,&pPage->buf,
4,zero)
;
230989 pPage->iPrevPgidx = 0;
230990 pPage->pgno++;
230991
230992 /* Increase the leaves written counter */
230993 pWriter->nLeafWritten++;
230994
230995 /* The new leaf holds no terms or rowids */
230996 pWriter->bFirstTermInPage = 1;
230997 pWriter->bFirstRowidInPage = 1;
230998}
230999
231000/*
231001** Append term pTerm/nTerm to the segment being written by the writer passed
231002** as the second argument.
231003**
231004** If an error occurs, set the Fts5Index.rc error code. If an error has
231005** already occurred, this function is a no-op.
231006*/
231007static void fts5WriteAppendTerm(
231008 Fts5Index *p,
231009 Fts5SegWriter *pWriter,
231010 int nTerm, const u8 *pTerm
231011){
231012 int nPrefix; /* Bytes of prefix compression for term */
231013 Fts5PageWriter *pPage = &pWriter->writer;
231014 Fts5Buffer *pPgidx = &pWriter->writer.pgidx;
231015 int nMin = MIN(pPage->term.n, nTerm)((pPage->term.n)<(nTerm)?(pPage->term.n):(nTerm));
231016
231017 assert( p->rc==SQLITE_OK )((void) (0));
231018 assert( pPage->buf.n>=4 )((void) (0));
231019 assert( pPage->buf.n>4 || pWriter->bFirstTermInPage )((void) (0));
231020
231021 /* If the current leaf page is full, flush it to disk. */
231022 if( (pPage->buf.n + pPgidx->n + nTerm + 2)>=p->pConfig->pgsz ){
231023 if( pPage->buf.n>4 ){
231024 fts5WriteFlushLeaf(p, pWriter);
231025 if( p->rc!=SQLITE_OK0 ) return;
231026 }
231027 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) )
;
231028 }
231029
231030 /* TODO1: Updating pgidx here. */
231031 pPgidx->n += sqlite3Fts5PutVarint(
231032 &pPgidx->p[pPgidx->n], pPage->buf.n - pPage->iPrevPgidx
231033 );
231034 pPage->iPrevPgidx = pPage->buf.n;
231035#if 0
231036 fts5PutU16(&pPgidx->p[pPgidx->n], pPage->buf.n);
231037 pPgidx->n += 2;
231038#endif
231039
231040 if( pWriter->bFirstTermInPage ){
231041 nPrefix = 0;
231042 if( pPage->pgno!=1 ){
231043 /* This is the first term on a leaf that is not the leftmost leaf in
231044 ** the segment b-tree. In this case it is necessary to add a term to
231045 ** the b-tree hierarchy that is (a) larger than the largest term
231046 ** already written to the segment and (b) smaller than or equal to
231047 ** this term. In other words, a prefix of (pTerm/nTerm) that is one
231048 ** byte longer than the longest prefix (pTerm/nTerm) shares with the
231049 ** previous term.
231050 **
231051 ** Usually, the previous term is available in pPage->term. The exception
231052 ** is if this is the first term written in an incremental-merge step.
231053 ** In this case the previous term is not available, so just write a
231054 ** copy of (pTerm/nTerm) into the parent node. This is slightly
231055 ** inefficient, but still correct. */
231056 int n = nTerm;
231057 if( pPage->term.n ){
231058 n = 1 + fts5PrefixCompress(nMin, pPage->term.p, pTerm);
231059 }
231060 fts5WriteBtreeTerm(p, pWriter, n, pTerm);
231061 if( p->rc!=SQLITE_OK0 ) return;
231062 pPage = &pWriter->writer;
231063 }
231064 }else{
231065 nPrefix = fts5PrefixCompress(nMin, pPage->term.p, pTerm);
231066 fts5BufferAppendVarint(&p->rc, &pPage->buf, nPrefix)sqlite3Fts5BufferAppendVarint(&p->rc,&pPage->buf
,nPrefix)
;
231067 }
231068
231069 /* Append the number of bytes of new data, then the term data itself
231070 ** to the page. */
231071 fts5BufferAppendVarint(&p->rc, &pPage->buf, nTerm - nPrefix)sqlite3Fts5BufferAppendVarint(&p->rc,&pPage->buf
,nTerm - nPrefix)
;
231072 fts5BufferAppendBlob(&p->rc, &pPage->buf, nTerm - nPrefix, &pTerm[nPrefix])sqlite3Fts5BufferAppendBlob(&p->rc,&pPage->buf,
nTerm - nPrefix,&pTerm[nPrefix])
;
231073
231074 /* Update the Fts5PageWriter.term field. */
231075 fts5BufferSet(&p->rc, &pPage->term, nTerm, pTerm)sqlite3Fts5BufferSet(&p->rc,&pPage->term,nTerm,
pTerm)
;
231076 pWriter->bFirstTermInPage = 0;
231077
231078 pWriter->bFirstRowidInPage = 0;
231079 pWriter->bFirstRowidInDoclist = 1;
231080
231081 assert( p->rc || (pWriter->nDlidx>0 && pWriter->aDlidx[0].buf.n==0) )((void) (0));
231082 pWriter->aDlidx[0].pgno = pPage->pgno;
231083}
231084
231085/*
231086** Append a rowid and position-list size field to the writers output.
231087*/
231088static void fts5WriteAppendRowid(
231089 Fts5Index *p,
231090 Fts5SegWriter *pWriter,
231091 i64 iRowid
231092){
231093 if( p->rc==SQLITE_OK0 ){
231094 Fts5PageWriter *pPage = &pWriter->writer;
231095
231096 if( (pPage->buf.n + pPage->pgidx.n)>=p->pConfig->pgsz ){
231097 fts5WriteFlushLeaf(p, pWriter);
231098 }
231099
231100 /* If this is to be the first rowid written to the page, set the
231101 ** rowid-pointer in the page-header. Also append a value to the dlidx
231102 ** buffer, in case a doclist-index is required. */
231103 if( pWriter->bFirstRowidInPage ){
231104 fts5PutU16(pPage->buf.p, (u16)pPage->buf.n);
231105 fts5WriteDlidxAppend(p, pWriter, iRowid);
231106 }
231107
231108 /* Write the rowid. */
231109 if( pWriter->bFirstRowidInDoclist || pWriter->bFirstRowidInPage ){
231110 fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid)sqlite3Fts5BufferAppendVarint(&p->rc,&pPage->buf
,iRowid)
;
231111 }else{
231112 assert_nc( p->rc || iRowid>pWriter->iPrevRowid )((void) (0));
231113 fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid - pWriter->iPrevRowid)sqlite3Fts5BufferAppendVarint(&p->rc,&pPage->buf
,iRowid - pWriter->iPrevRowid)
;
231114 }
231115 pWriter->iPrevRowid = iRowid;
231116 pWriter->bFirstRowidInDoclist = 0;
231117 pWriter->bFirstRowidInPage = 0;
231118 }
231119}
231120
231121static void fts5WriteAppendPoslistData(
231122 Fts5Index *p,
231123 Fts5SegWriter *pWriter,
231124 const u8 *aData,
231125 int nData
231126){
231127 Fts5PageWriter *pPage = &pWriter->writer;
231128 const u8 *a = aData;
231129 int n = nData;
231130
231131 assert( p->pConfig->pgsz>0 )((void) (0));
231132 while( p->rc==SQLITE_OK0
231133 && (pPage->buf.n + pPage->pgidx.n + n)>=p->pConfig->pgsz
231134 ){
231135 int nReq = p->pConfig->pgsz - pPage->buf.n - pPage->pgidx.n;
231136 int nCopy = 0;
231137 while( nCopy<nReq ){
231138 i64 dummy;
231139 nCopy += fts5GetVarintsqlite3Fts5GetVarint(&a[nCopy], (u64*)&dummy);
231140 }
231141 fts5BufferAppendBlob(&p->rc, &pPage->buf, nCopy, a)sqlite3Fts5BufferAppendBlob(&p->rc,&pPage->buf,
nCopy,a)
;
231142 a += nCopy;
231143 n -= nCopy;
231144 fts5WriteFlushLeaf(p, pWriter);
231145 }
231146 if( n>0 ){
231147 fts5BufferAppendBlob(&p->rc, &pPage->buf, n, a)sqlite3Fts5BufferAppendBlob(&p->rc,&pPage->buf,
n,a)
;
231148 }
231149}
231150
231151/*
231152** Flush any data cached by the writer object to the database. Free any
231153** allocations associated with the writer.
231154*/
231155static void fts5WriteFinish(
231156 Fts5Index *p,
231157 Fts5SegWriter *pWriter, /* Writer object */
231158 int *pnLeaf /* OUT: Number of leaf pages in b-tree */
231159){
231160 int i;
231161 Fts5PageWriter *pLeaf = &pWriter->writer;
231162 if( p->rc==SQLITE_OK0 ){
231163 assert( pLeaf->pgno>=1 )((void) (0));
231164 if( pLeaf->buf.n>4 ){
231165 fts5WriteFlushLeaf(p, pWriter);
231166 }
231167 *pnLeaf = pLeaf->pgno-1;
231168 if( pLeaf->pgno>1 ){
231169 fts5WriteFlushBtree(p, pWriter);
231170 }
231171 }
231172 fts5BufferFree(&pLeaf->term)sqlite3Fts5BufferFree(&pLeaf->term);
231173 fts5BufferFree(&pLeaf->buf)sqlite3Fts5BufferFree(&pLeaf->buf);
231174 fts5BufferFree(&pLeaf->pgidx)sqlite3Fts5BufferFree(&pLeaf->pgidx);
231175 fts5BufferFree(&pWriter->btterm)sqlite3Fts5BufferFree(&pWriter->btterm);
231176
231177 for(i=0; i<pWriter->nDlidx; i++){
231178 sqlite3Fts5BufferFree(&pWriter->aDlidx[i].buf);
231179 }
231180 sqlite3_free(pWriter->aDlidx);
231181}
231182
231183static void fts5WriteInit(
231184 Fts5Index *p,
231185 Fts5SegWriter *pWriter,
231186 int iSegid
231187){
231188 const int nBuffer = p->pConfig->pgsz + FTS5_DATA_PADDING20;
231189
231190 memset(pWriter, 0, sizeof(Fts5SegWriter));
231191 pWriter->iSegid = iSegid;
231192
231193 fts5WriteDlidxGrow(p, pWriter, 1);
231194 pWriter->writer.pgno = 1;
231195 pWriter->bFirstTermInPage = 1;
231196 pWriter->iBtPage = 1;
231197
231198 assert( pWriter->writer.buf.n==0 )((void) (0));
231199 assert( pWriter->writer.pgidx.n==0 )((void) (0));
231200
231201 /* Grow the two buffers to pgsz + padding bytes in size. */
231202 sqlite3Fts5BufferSize(&p->rc, &pWriter->writer.pgidx, nBuffer);
231203 sqlite3Fts5BufferSize(&p->rc, &pWriter->writer.buf, nBuffer);
231204
231205 if( p->pIdxWriter==0 ){
231206 Fts5Config *pConfig = p->pConfig;
231207 fts5IndexPrepareStmt(p, &p->pIdxWriter, sqlite3_mprintf(
231208 "INSERT INTO '%q'.'%q_idx'(segid,term,pgno) VALUES(?,?,?)",
231209 pConfig->zDb, pConfig->zName
231210 ));
231211 }
231212
231213 if( p->rc==SQLITE_OK0 ){
231214 /* Initialize the 4-byte leaf-page header to 0x00. */
231215 memset(pWriter->writer.buf.p, 0, 4);
231216 pWriter->writer.buf.n = 4;
231217
231218 /* Bind the current output segment id to the index-writer. This is an
231219 ** optimization over binding the same value over and over as rows are
231220 ** inserted into %_idx by the current writer. */
231221 sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid);
231222 }
231223}
231224
231225/*
231226** Iterator pIter was used to iterate through the input segments of on an
231227** incremental merge operation. This function is called if the incremental
231228** merge step has finished but the input has not been completely exhausted.
231229*/
231230static void fts5TrimSegments(Fts5Index *p, Fts5Iter *pIter){
231231 int i;
231232 Fts5Buffer buf;
231233 memset(&buf, 0, sizeof(Fts5Buffer));
231234 for(i=0; i<pIter->nSeg && p->rc==SQLITE_OK0; i++){
231235 Fts5SegIter *pSeg = &pIter->aSeg[i];
231236 if( pSeg->pSeg==0 ){
231237 /* no-op */
231238 }else if( pSeg->pLeaf==0 ){
231239 /* All keys from this input segment have been transfered to the output.
231240 ** Set both the first and last page-numbers to 0 to indicate that the
231241 ** segment is now empty. */
231242 pSeg->pSeg->pgnoLast = 0;
231243 pSeg->pSeg->pgnoFirst = 0;
231244 }else{
231245 int iOff = pSeg->iTermLeafOffset; /* Offset on new first leaf page */
231246 i64 iLeafRowid;
231247 Fts5Data *pData;
231248 int iId = pSeg->pSeg->iSegid;
231249 u8 aHdr[4] = {0x00, 0x00, 0x00, 0x00};
231250
231251 iLeafRowid = FTS5_SEGMENT_ROWID(iId, pSeg->iTermLeafPgno)( ((i64)(iId) << (31 +5 +1)) + ((i64)(0) << (31 +
5)) + ((i64)(0) << (31)) + ((i64)(pSeg->iTermLeafPgno
)) )
;
231252 pData = fts5LeafRead(p, iLeafRowid);
231253 if( pData ){
231254 if( iOff>pData->szLeaf ){
231255 /* This can occur if the pages that the segments occupy overlap - if
231256 ** a single page has been assigned to more than one segment. In
231257 ** this case a prior iteration of this loop may have corrupted the
231258 ** segment currently being trimmed. */
231259 p->rc = FTS5_CORRUPT(11 | (1<<8));
231260 }else{
231261 fts5BufferZero(&buf)sqlite3Fts5BufferZero(&buf);
231262 fts5BufferGrow(&p->rc, &buf, pData->nn)( (u32)((&buf)->n) + (u32)(pData->nn) <= (u32)((
&buf)->nSpace) ? 0 : sqlite3Fts5BufferSize((&p->
rc),(&buf),(pData->nn)+(&buf)->n) )
;
231263 fts5BufferAppendBlob(&p->rc, &buf, sizeof(aHdr), aHdr)sqlite3Fts5BufferAppendBlob(&p->rc,&buf,sizeof(aHdr
),aHdr)
;
231264 fts5BufferAppendVarint(&p->rc, &buf, pSeg->term.n)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,pSeg->
term.n)
;
231265 fts5BufferAppendBlob(&p->rc, &buf, pSeg->term.n, pSeg->term.p)sqlite3Fts5BufferAppendBlob(&p->rc,&buf,pSeg->term
.n,pSeg->term.p)
;
231266 fts5BufferAppendBlob(&p->rc, &buf, pData->szLeaf-iOff,&pData->p[iOff])sqlite3Fts5BufferAppendBlob(&p->rc,&buf,pData->
szLeaf-iOff,&pData->p[iOff])
;
231267 if( p->rc==SQLITE_OK0 ){
231268 /* Set the szLeaf field */
231269 fts5PutU16(&buf.p[2], (u16)buf.n);
231270 }
231271
231272 /* Set up the new page-index array */
231273 fts5BufferAppendVarint(&p->rc, &buf, 4)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,4);
231274 if( pSeg->iLeafPgno==pSeg->iTermLeafPgno
231275 && pSeg->iEndofDoclist<pData->szLeaf
231276 && pSeg->iPgidxOff<=pData->nn
231277 ){
231278 int nDiff = pData->szLeaf - pSeg->iEndofDoclist;
231279 fts5BufferAppendVarint(&p->rc, &buf, buf.n - 1 - nDiff - 4)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,buf.n - 1
- nDiff - 4)
;
231280 fts5BufferAppendBlob(&p->rc, &buf,sqlite3Fts5BufferAppendBlob(&p->rc,&buf,pData->
nn - pSeg->iPgidxOff,&pData->p[pSeg->iPgidxOff])
231281 pData->nn - pSeg->iPgidxOff, &pData->p[pSeg->iPgidxOff]sqlite3Fts5BufferAppendBlob(&p->rc,&buf,pData->
nn - pSeg->iPgidxOff,&pData->p[pSeg->iPgidxOff])
231282 )sqlite3Fts5BufferAppendBlob(&p->rc,&buf,pData->
nn - pSeg->iPgidxOff,&pData->p[pSeg->iPgidxOff])
;
231283 }
231284
231285 pSeg->pSeg->pgnoFirst = pSeg->iTermLeafPgno;
231286 fts5DataDelete(p, FTS5_SEGMENT_ROWID(iId, 1)( ((i64)(iId) << (31 +5 +1)) + ((i64)(0) << (31 +
5)) + ((i64)(0) << (31)) + ((i64)(1)) )
, iLeafRowid);
231287 fts5DataWrite(p, iLeafRowid, buf.p, buf.n);
231288 }
231289 fts5DataRelease(pData);
231290 }
231291 }
231292 }
231293 fts5BufferFree(&buf)sqlite3Fts5BufferFree(&buf);
231294}
231295
231296static void fts5MergeChunkCallback(
231297 Fts5Index *p,
231298 void *pCtx,
231299 const u8 *pChunk, int nChunk
231300){
231301 Fts5SegWriter *pWriter = (Fts5SegWriter*)pCtx;
231302 fts5WriteAppendPoslistData(p, pWriter, pChunk, nChunk);
231303}
231304
231305/*
231306**
231307*/
231308static void fts5IndexMergeLevel(
231309 Fts5Index *p, /* FTS5 backend object */
231310 Fts5Structure **ppStruct, /* IN/OUT: Stucture of index */
231311 int iLvl, /* Level to read input from */
231312 int *pnRem /* Write up to this many output leaves */
231313){
231314 Fts5Structure *pStruct = *ppStruct;
231315 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
231316 Fts5StructureLevel *pLvlOut;
231317 Fts5Iter *pIter = 0; /* Iterator to read input data */
231318 int nRem = pnRem ? *pnRem : 0; /* Output leaf pages left to write */
231319 int nInput; /* Number of input segments */
231320 Fts5SegWriter writer; /* Writer object */
231321 Fts5StructureSegment *pSeg; /* Output segment */
231322 Fts5Buffer term;
231323 int bOldest; /* True if the output segment is the oldest */
231324 int eDetail = p->pConfig->eDetail;
231325 const int flags = FTS5INDEX_QUERY_NOOUTPUT0x0020;
231326 int bTermWritten = 0; /* True if current term already output */
231327
231328 assert( iLvl<pStruct->nLevel )((void) (0));
231329 assert( pLvl->nMerge<=pLvl->nSeg )((void) (0));
231330
231331 memset(&writer, 0, sizeof(Fts5SegWriter));
231332 memset(&term, 0, sizeof(Fts5Buffer));
231333 if( pLvl->nMerge ){
231334 pLvlOut = &pStruct->aLevel[iLvl+1];
231335 assert( pLvlOut->nSeg>0 )((void) (0));
231336 nInput = pLvl->nMerge;
231337 pSeg = &pLvlOut->aSeg[pLvlOut->nSeg-1];
231338
231339 fts5WriteInit(p, &writer, pSeg->iSegid);
231340 writer.writer.pgno = pSeg->pgnoLast+1;
231341 writer.iBtPage = 0;
231342 }else{
231343 int iSegid = fts5AllocateSegid(p, pStruct);
231344
231345 /* Extend the Fts5Structure object as required to ensure the output
231346 ** segment exists. */
231347 if( iLvl==pStruct->nLevel-1 ){
231348 fts5StructureAddLevel(&p->rc, ppStruct);
231349 pStruct = *ppStruct;
231350 }
231351 fts5StructureExtendLevel(&p->rc, pStruct, iLvl+1, 1, 0);
231352 if( p->rc ) return;
231353 pLvl = &pStruct->aLevel[iLvl];
231354 pLvlOut = &pStruct->aLevel[iLvl+1];
231355
231356 fts5WriteInit(p, &writer, iSegid);
231357
231358 /* Add the new segment to the output level */
231359 pSeg = &pLvlOut->aSeg[pLvlOut->nSeg];
231360 pLvlOut->nSeg++;
231361 pSeg->pgnoFirst = 1;
231362 pSeg->iSegid = iSegid;
231363 pStruct->nSegment++;
231364
231365 /* Read input from all segments in the input level */
231366 nInput = pLvl->nSeg;
231367 }
231368 bOldest = (pLvlOut->nSeg==1 && pStruct->nLevel==iLvl+2);
231369
231370 assert( iLvl>=0 )((void) (0));
231371 for(fts5MultiIterNew(p, pStruct, flags, 0, 0, 0, iLvl, nInput, &pIter);
231372 fts5MultiIterEof(p, pIter)==0;
231373 fts5MultiIterNext(p, pIter, 0, 0)
231374 ){
231375 Fts5SegIter *pSegIter = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
231376 int nPos; /* position-list size field value */
231377 int nTerm;
231378 const u8 *pTerm;
231379
231380 pTerm = fts5MultiIterTerm(pIter, &nTerm);
231381 if( nTerm!=term.n || fts5Memcmp(pTerm, term.p, nTerm)((nTerm)<=0 ? 0 : memcmp((pTerm), (term.p), (nTerm))) ){
231382 if( pnRem && writer.nLeafWritten>nRem ){
231383 break;
231384 }
231385 fts5BufferSet(&p->rc, &term, nTerm, pTerm)sqlite3Fts5BufferSet(&p->rc,&term,nTerm,pTerm);
231386 bTermWritten =0;
231387 }
231388
231389 /* Check for key annihilation. */
231390 if( pSegIter->nPos==0 && (bOldest || pSegIter->bDel==0) ) continue;
231391
231392 if( p->rc==SQLITE_OK0 && bTermWritten==0 ){
231393 /* This is a new term. Append a term to the output segment. */
231394 fts5WriteAppendTerm(p, &writer, nTerm, pTerm);
231395 bTermWritten = 1;
231396 }
231397
231398 /* Append the rowid to the output */
231399 /* WRITEPOSLISTSIZE */
231400 fts5WriteAppendRowid(p, &writer, fts5MultiIterRowid(pIter));
231401
231402 if( eDetail==FTS5_DETAIL_NONE1 ){
231403 if( pSegIter->bDel ){
231404 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, 0)sqlite3Fts5BufferAppendVarint(&p->rc,&writer.writer
.buf,0)
;
231405 if( pSegIter->nPos>0 ){
231406 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, 0)sqlite3Fts5BufferAppendVarint(&p->rc,&writer.writer
.buf,0)
;
231407 }
231408 }
231409 }else{
231410 /* Append the position-list data to the output */
231411 nPos = pSegIter->nPos*2 + pSegIter->bDel;
231412 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, nPos)sqlite3Fts5BufferAppendVarint(&p->rc,&writer.writer
.buf,nPos)
;
231413 fts5ChunkIterate(p, pSegIter, (void*)&writer, fts5MergeChunkCallback);
231414 }
231415 }
231416
231417 /* Flush the last leaf page to disk. Set the output segment b-tree height
231418 ** and last leaf page number at the same time. */
231419 fts5WriteFinish(p, &writer, &pSeg->pgnoLast);
231420
231421 assert( pIter!=0 || p->rc!=SQLITE_OK )((void) (0));
231422 if( fts5MultiIterEof(p, pIter) ){
231423 int i;
231424
231425 /* Remove the redundant segments from the %_data table */
231426 for(i=0; i<nInput; i++){
231427 fts5DataRemoveSegment(p, pLvl->aSeg[i].iSegid);
231428 }
231429
231430 /* Remove the redundant segments from the input level */
231431 if( pLvl->nSeg!=nInput ){
231432 int nMove = (pLvl->nSeg - nInput) * sizeof(Fts5StructureSegment);
231433 memmove(pLvl->aSeg, &pLvl->aSeg[nInput], nMove);
231434 }
231435 pStruct->nSegment -= nInput;
231436 pLvl->nSeg -= nInput;
231437 pLvl->nMerge = 0;
231438 if( pSeg->pgnoLast==0 ){
231439 pLvlOut->nSeg--;
231440 pStruct->nSegment--;
231441 }
231442 }else{
231443 assert( pSeg->pgnoLast>0 )((void) (0));
231444 fts5TrimSegments(p, pIter);
231445 pLvl->nMerge = nInput;
231446 }
231447
231448 fts5MultiIterFree(pIter);
231449 fts5BufferFree(&term)sqlite3Fts5BufferFree(&term);
231450 if( pnRem ) *pnRem -= writer.nLeafWritten;
231451}
231452
231453/*
231454** Do up to nPg pages of automerge work on the index.
231455**
231456** Return true if any changes were actually made, or false otherwise.
231457*/
231458static int fts5IndexMerge(
231459 Fts5Index *p, /* FTS5 backend object */
231460 Fts5Structure **ppStruct, /* IN/OUT: Current structure of index */
231461 int nPg, /* Pages of work to do */
231462 int nMin /* Minimum number of segments to merge */
231463){
231464 int nRem = nPg;
231465 int bRet = 0;
231466 Fts5Structure *pStruct = *ppStruct;
231467 while( nRem>0 && p->rc==SQLITE_OK0 ){
231468 int iLvl; /* To iterate through levels */
231469 int iBestLvl = 0; /* Level offering the most input segments */
231470 int nBest = 0; /* Number of input segments on best level */
231471
231472 /* Set iBestLvl to the level to read input segments from. */
231473 assert( pStruct->nLevel>0 )((void) (0));
231474 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
231475 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
231476 if( pLvl->nMerge ){
231477 if( pLvl->nMerge>nBest ){
231478 iBestLvl = iLvl;
231479 nBest = pLvl->nMerge;
231480 }
231481 break;
231482 }
231483 if( pLvl->nSeg>nBest ){
231484 nBest = pLvl->nSeg;
231485 iBestLvl = iLvl;
231486 }
231487 }
231488
231489 /* If nBest is still 0, then the index must be empty. */
231490#ifdef SQLITE_DEBUG
231491 for(iLvl=0; nBest==0 && iLvl<pStruct->nLevel; iLvl++){
231492 assert( pStruct->aLevel[iLvl].nSeg==0 )((void) (0));
231493 }
231494#endif
231495
231496 if( nBest<nMin && pStruct->aLevel[iBestLvl].nMerge==0 ){
231497 break;
231498 }
231499 bRet = 1;
231500 fts5IndexMergeLevel(p, &pStruct, iBestLvl, &nRem);
231501 if( p->rc==SQLITE_OK0 && pStruct->aLevel[iBestLvl].nMerge==0 ){
231502 fts5StructurePromote(p, iBestLvl+1, pStruct);
231503 }
231504 }
231505 *ppStruct = pStruct;
231506 return bRet;
231507}
231508
231509/*
231510** A total of nLeaf leaf pages of data has just been flushed to a level-0
231511** segment. This function updates the write-counter accordingly and, if
231512** necessary, performs incremental merge work.
231513**
231514** If an error occurs, set the Fts5Index.rc error code. If an error has
231515** already occurred, this function is a no-op.
231516*/
231517static void fts5IndexAutomerge(
231518 Fts5Index *p, /* FTS5 backend object */
231519 Fts5Structure **ppStruct, /* IN/OUT: Current structure of index */
231520 int nLeaf /* Number of output leaves just written */
231521){
231522 if( p->rc==SQLITE_OK0 && p->pConfig->nAutomerge>0 && ALWAYS((*ppStruct)!=0)((*ppStruct)!=0) ){
231523 Fts5Structure *pStruct = *ppStruct;
231524 u64 nWrite; /* Initial value of write-counter */
231525 int nWork; /* Number of work-quanta to perform */
231526 int nRem; /* Number of leaf pages left to write */
231527
231528 /* Update the write-counter. While doing so, set nWork. */
231529 nWrite = pStruct->nWriteCounter;
231530 nWork = (int)(((nWrite + nLeaf) / p->nWorkUnit) - (nWrite / p->nWorkUnit));
231531 pStruct->nWriteCounter += nLeaf;
231532 nRem = (int)(p->nWorkUnit * nWork * pStruct->nLevel);
231533
231534 fts5IndexMerge(p, ppStruct, nRem, p->pConfig->nAutomerge);
231535 }
231536}
231537
231538static void fts5IndexCrisismerge(
231539 Fts5Index *p, /* FTS5 backend object */
231540 Fts5Structure **ppStruct /* IN/OUT: Current structure of index */
231541){
231542 const int nCrisis = p->pConfig->nCrisisMerge;
231543 Fts5Structure *pStruct = *ppStruct;
231544 int iLvl = 0;
231545
231546 assert( p->rc!=SQLITE_OK || pStruct->nLevel>0 )((void) (0));
231547 while( p->rc==SQLITE_OK0 && pStruct->aLevel[iLvl].nSeg>=nCrisis ){
231548 fts5IndexMergeLevel(p, &pStruct, iLvl, 0);
231549 assert( p->rc!=SQLITE_OK || pStruct->nLevel>(iLvl+1) )((void) (0));
231550 fts5StructurePromote(p, iLvl+1, pStruct);
231551 iLvl++;
231552 }
231553 *ppStruct = pStruct;
231554}
231555
231556static int fts5IndexReturn(Fts5Index *p){
231557 int rc = p->rc;
231558 p->rc = SQLITE_OK0;
231559 return rc;
231560}
231561
231562typedef struct Fts5FlushCtx Fts5FlushCtx;
231563struct Fts5FlushCtx {
231564 Fts5Index *pIdx;
231565 Fts5SegWriter writer;
231566};
231567
231568/*
231569** Buffer aBuf[] contains a list of varints, all small enough to fit
231570** in a 32-bit integer. Return the size of the largest prefix of this
231571** list nMax bytes or less in size.
231572*/
231573static int fts5PoslistPrefix(const u8 *aBuf, int nMax){
231574 int ret;
231575 u32 dummy;
231576 ret = fts5GetVarint32(aBuf, dummy)sqlite3Fts5GetVarint32(aBuf,(u32*)&dummy);
231577 if( ret<nMax ){
231578 while( 1 ){
231579 int i = fts5GetVarint32(&aBuf[ret], dummy)sqlite3Fts5GetVarint32(&aBuf[ret],(u32*)&dummy);
231580 if( (ret + i) > nMax ) break;
231581 ret += i;
231582 }
231583 }
231584 return ret;
231585}
231586
231587/*
231588** Flush the contents of in-memory hash table iHash to a new level-0
231589** segment on disk. Also update the corresponding structure record.
231590**
231591** If an error occurs, set the Fts5Index.rc error code. If an error has
231592** already occurred, this function is a no-op.
231593*/
231594static void fts5FlushOneHash(Fts5Index *p){
231595 Fts5Hash *pHash = p->pHash;
231596 Fts5Structure *pStruct;
231597 int iSegid;
231598 int pgnoLast = 0; /* Last leaf page number in segment */
231599
231600 /* Obtain a reference to the index structure and allocate a new segment-id
231601 ** for the new level-0 segment. */
231602 pStruct = fts5StructureRead(p);
231603 iSegid = fts5AllocateSegid(p, pStruct);
231604 fts5StructureInvalidate(p);
231605
231606 if( iSegid ){
231607 const int pgsz = p->pConfig->pgsz;
231608 int eDetail = p->pConfig->eDetail;
231609 Fts5StructureSegment *pSeg; /* New segment within pStruct */
231610 Fts5Buffer *pBuf; /* Buffer in which to assemble leaf page */
231611 Fts5Buffer *pPgidx; /* Buffer in which to assemble pgidx */
231612
231613 Fts5SegWriter writer;
231614 fts5WriteInit(p, &writer, iSegid);
231615
231616 pBuf = &writer.writer.buf;
231617 pPgidx = &writer.writer.pgidx;
231618
231619 /* fts5WriteInit() should have initialized the buffers to (most likely)
231620 ** the maximum space required. */
231621 assert( p->rc || pBuf->nSpace>=(pgsz + FTS5_DATA_PADDING) )((void) (0));
231622 assert( p->rc || pPgidx->nSpace>=(pgsz + FTS5_DATA_PADDING) )((void) (0));
231623
231624 /* Begin scanning through hash table entries. This loop runs once for each
231625 ** term/doclist currently stored within the hash table. */
231626 if( p->rc==SQLITE_OK0 ){
231627 p->rc = sqlite3Fts5HashScanInit(pHash, 0, 0);
231628 }
231629 while( p->rc==SQLITE_OK0 && 0==sqlite3Fts5HashScanEof(pHash) ){
231630 const char *zTerm; /* Buffer containing term */
231631 const u8 *pDoclist; /* Pointer to doclist for this term */
231632 int nDoclist; /* Size of doclist in bytes */
231633
231634 /* Write the term for this entry to disk. */
231635 sqlite3Fts5HashScanEntry(pHash, &zTerm, &pDoclist, &nDoclist);
231636 fts5WriteAppendTerm(p, &writer, (int)strlen(zTerm), (const u8*)zTerm);
231637 if( p->rc!=SQLITE_OK0 ) break;
231638
231639 assert( writer.bFirstRowidInPage==0 )((void) (0));
231640 if( pgsz>=(pBuf->n + pPgidx->n + nDoclist + 1) ){
231641 /* The entire doclist will fit on the current leaf. */
231642 fts5BufferSafeAppendBlob(pBuf, pDoclist, nDoclist){ ((void) (0)); memcpy(&(pBuf)->p[(pBuf)->n], pDoclist
, nDoclist); (pBuf)->n += nDoclist; }
;
231643 }else{
231644 i64 iRowid = 0;
231645 u64 iDelta = 0;
231646 int iOff = 0;
231647
231648 /* The entire doclist will not fit on this leaf. The following
231649 ** loop iterates through the poslists that make up the current
231650 ** doclist. */
231651 while( p->rc==SQLITE_OK0 && iOff<nDoclist ){
231652 iOff += fts5GetVarintsqlite3Fts5GetVarint(&pDoclist[iOff], &iDelta);
231653 iRowid += iDelta;
231654
231655 if( writer.bFirstRowidInPage ){
231656 fts5PutU16(&pBuf->p[0], (u16)pBuf->n); /* first rowid on page */
231657 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iRowid);
231658 writer.bFirstRowidInPage = 0;
231659 fts5WriteDlidxAppend(p, &writer, iRowid);
231660 if( p->rc!=SQLITE_OK0 ) break;
231661 }else{
231662 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iDelta);
231663 }
231664 assert( pBuf->n<=pBuf->nSpace )((void) (0));
231665
231666 if( eDetail==FTS5_DETAIL_NONE1 ){
231667 if( iOff<nDoclist && pDoclist[iOff]==0 ){
231668 pBuf->p[pBuf->n++] = 0;
231669 iOff++;
231670 if( iOff<nDoclist && pDoclist[iOff]==0 ){
231671 pBuf->p[pBuf->n++] = 0;
231672 iOff++;
231673 }
231674 }
231675 if( (pBuf->n + pPgidx->n)>=pgsz ){
231676 fts5WriteFlushLeaf(p, &writer);
231677 }
231678 }else{
231679 int bDummy;
231680 int nPos;
231681 int nCopy = fts5GetPoslistSize(&pDoclist[iOff], &nPos, &bDummy);
231682 nCopy += nPos;
231683 if( (pBuf->n + pPgidx->n + nCopy) <= pgsz ){
231684 /* The entire poslist will fit on the current leaf. So copy
231685 ** it in one go. */
231686 fts5BufferSafeAppendBlob(pBuf, &pDoclist[iOff], nCopy){ ((void) (0)); memcpy(&(pBuf)->p[(pBuf)->n], &
pDoclist[iOff], nCopy); (pBuf)->n += nCopy; }
;
231687 }else{
231688 /* The entire poslist will not fit on this leaf. So it needs
231689 ** to be broken into sections. The only qualification being
231690 ** that each varint must be stored contiguously. */
231691 const u8 *pPoslist = &pDoclist[iOff];
231692 int iPos = 0;
231693 while( p->rc==SQLITE_OK0 ){
231694 int nSpace = pgsz - pBuf->n - pPgidx->n;
231695 int n = 0;
231696 if( (nCopy - iPos)<=nSpace ){
231697 n = nCopy - iPos;
231698 }else{
231699 n = fts5PoslistPrefix(&pPoslist[iPos], nSpace);
231700 }
231701 assert( n>0 )((void) (0));
231702 fts5BufferSafeAppendBlob(pBuf, &pPoslist[iPos], n){ ((void) (0)); memcpy(&(pBuf)->p[(pBuf)->n], &
pPoslist[iPos], n); (pBuf)->n += n; }
;
231703 iPos += n;
231704 if( (pBuf->n + pPgidx->n)>=pgsz ){
231705 fts5WriteFlushLeaf(p, &writer);
231706 }
231707 if( iPos>=nCopy ) break;
231708 }
231709 }
231710 iOff += nCopy;
231711 }
231712 }
231713 }
231714
231715 /* TODO2: Doclist terminator written here. */
231716 /* pBuf->p[pBuf->n++] = '\0'; */
231717 assert( pBuf->n<=pBuf->nSpace )((void) (0));
231718 if( p->rc==SQLITE_OK0 ) sqlite3Fts5HashScanNext(pHash);
231719 }
231720 sqlite3Fts5HashClear(pHash);
231721 fts5WriteFinish(p, &writer, &pgnoLast);
231722
231723 /* Update the Fts5Structure. It is written back to the database by the
231724 ** fts5StructureRelease() call below. */
231725 if( pStruct->nLevel==0 ){
231726 fts5StructureAddLevel(&p->rc, &pStruct);
231727 }
231728 fts5StructureExtendLevel(&p->rc, pStruct, 0, 1, 0);
231729 if( p->rc==SQLITE_OK0 ){
231730 pSeg = &pStruct->aLevel[0].aSeg[ pStruct->aLevel[0].nSeg++ ];
231731 pSeg->iSegid = iSegid;
231732 pSeg->pgnoFirst = 1;
231733 pSeg->pgnoLast = pgnoLast;
231734 pStruct->nSegment++;
231735 }
231736 fts5StructurePromote(p, 0, pStruct);
231737 }
231738
231739 fts5IndexAutomerge(p, &pStruct, pgnoLast);
231740 fts5IndexCrisismerge(p, &pStruct);
231741 fts5StructureWrite(p, pStruct);
231742 fts5StructureRelease(pStruct);
231743}
231744
231745/*
231746** Flush any data stored in the in-memory hash tables to the database.
231747*/
231748static void fts5IndexFlush(Fts5Index *p){
231749 /* Unless it is empty, flush the hash table to disk */
231750 if( p->nPendingData ){
231751 assert( p->pHash )((void) (0));
231752 p->nPendingData = 0;
231753 fts5FlushOneHash(p);
231754 }
231755}
231756
231757static Fts5Structure *fts5IndexOptimizeStruct(
231758 Fts5Index *p,
231759 Fts5Structure *pStruct
231760){
231761 Fts5Structure *pNew = 0;
231762 sqlite3_int64 nByte = sizeof(Fts5Structure);
231763 int nSeg = pStruct->nSegment;
231764 int i;
231765
231766 /* Figure out if this structure requires optimization. A structure does
231767 ** not require optimization if either:
231768 **
231769 ** + it consists of fewer than two segments, or
231770 ** + all segments are on the same level, or
231771 ** + all segments except one are currently inputs to a merge operation.
231772 **
231773 ** In the first case, return NULL. In the second, increment the ref-count
231774 ** on *pStruct and return a copy of the pointer to it.
231775 */
231776 if( nSeg<2 ) return 0;
231777 for(i=0; i<pStruct->nLevel; i++){
231778 int nThis = pStruct->aLevel[i].nSeg;
231779 if( nThis==nSeg || (nThis==nSeg-1 && pStruct->aLevel[i].nMerge==nThis) ){
231780 fts5StructureRef(pStruct);
231781 return pStruct;
231782 }
231783 assert( pStruct->aLevel[i].nMerge<=nThis )((void) (0));
231784 }
231785
231786 nByte += (pStruct->nLevel+1) * sizeof(Fts5StructureLevel);
231787 pNew = (Fts5Structure*)sqlite3Fts5MallocZero(&p->rc, nByte);
231788
231789 if( pNew ){
231790 Fts5StructureLevel *pLvl;
231791 nByte = nSeg * sizeof(Fts5StructureSegment);
231792 pNew->nLevel = pStruct->nLevel+1;
231793 pNew->nRef = 1;
231794 pNew->nWriteCounter = pStruct->nWriteCounter;
231795 pLvl = &pNew->aLevel[pStruct->nLevel];
231796 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&p->rc, nByte);
231797 if( pLvl->aSeg ){
231798 int iLvl, iSeg;
231799 int iSegOut = 0;
231800 /* Iterate through all segments, from oldest to newest. Add them to
231801 ** the new Fts5Level object so that pLvl->aSeg[0] is the oldest
231802 ** segment in the data structure. */
231803 for(iLvl=pStruct->nLevel-1; iLvl>=0; iLvl--){
231804 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
231805 pLvl->aSeg[iSegOut] = pStruct->aLevel[iLvl].aSeg[iSeg];
231806 iSegOut++;
231807 }
231808 }
231809 pNew->nSegment = pLvl->nSeg = nSeg;
231810 }else{
231811 sqlite3_free(pNew);
231812 pNew = 0;
231813 }
231814 }
231815
231816 return pNew;
231817}
231818
231819static int sqlite3Fts5IndexOptimize(Fts5Index *p){
231820 Fts5Structure *pStruct;
231821 Fts5Structure *pNew = 0;
231822
231823 assert( p->rc==SQLITE_OK )((void) (0));
231824 fts5IndexFlush(p);
231825 pStruct = fts5StructureRead(p);
231826 fts5StructureInvalidate(p);
231827
231828 if( pStruct ){
231829 pNew = fts5IndexOptimizeStruct(p, pStruct);
231830 }
231831 fts5StructureRelease(pStruct);
231832
231833 assert( pNew==0 || pNew->nSegment>0 )((void) (0));
231834 if( pNew ){
231835 int iLvl;
231836 for(iLvl=0; pNew->aLevel[iLvl].nSeg==0; iLvl++){}
231837 while( p->rc==SQLITE_OK0 && pNew->aLevel[iLvl].nSeg>0 ){
231838 int nRem = FTS5_OPT_WORK_UNIT1000;
231839 fts5IndexMergeLevel(p, &pNew, iLvl, &nRem);
231840 }
231841
231842 fts5StructureWrite(p, pNew);
231843 fts5StructureRelease(pNew);
231844 }
231845
231846 return fts5IndexReturn(p);
231847}
231848
231849/*
231850** This is called to implement the special "VALUES('merge', $nMerge)"
231851** INSERT command.
231852*/
231853static int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge){
231854 Fts5Structure *pStruct = fts5StructureRead(p);
231855 if( pStruct ){
231856 int nMin = p->pConfig->nUsermerge;
231857 fts5StructureInvalidate(p);
231858 if( nMerge<0 ){
231859 Fts5Structure *pNew = fts5IndexOptimizeStruct(p, pStruct);
231860 fts5StructureRelease(pStruct);
231861 pStruct = pNew;
231862 nMin = 2;
231863 nMerge = nMerge*-1;
231864 }
231865 if( pStruct && pStruct->nLevel ){
231866 if( fts5IndexMerge(p, &pStruct, nMerge, nMin) ){
231867 fts5StructureWrite(p, pStruct);
231868 }
231869 }
231870 fts5StructureRelease(pStruct);
231871 }
231872 return fts5IndexReturn(p);
231873}
231874
231875static void fts5AppendRowid(
231876 Fts5Index *p,
231877 i64 iDelta,
231878 Fts5Iter *pUnused,
231879 Fts5Buffer *pBuf
231880){
231881 UNUSED_PARAM(pUnused)(void)(pUnused);
231882 fts5BufferAppendVarint(&p->rc, pBuf, iDelta)sqlite3Fts5BufferAppendVarint(&p->rc,pBuf,iDelta);
231883}
231884
231885static void fts5AppendPoslist(
231886 Fts5Index *p,
231887 i64 iDelta,
231888 Fts5Iter *pMulti,
231889 Fts5Buffer *pBuf
231890){
231891 int nData = pMulti->base.nData;
231892 int nByte = nData + 9 + 9 + FTS5_DATA_ZERO_PADDING8;
231893 assert( nData>0 )((void) (0));
231894 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) )
){
231895 fts5BufferSafeAppendVarint(pBuf, iDelta){ (pBuf)->n += sqlite3Fts5PutVarint(&(pBuf)->p[(pBuf
)->n], (iDelta)); ((void) (0)); }
;
231896 fts5BufferSafeAppendVarint(pBuf, nData*2){ (pBuf)->n += sqlite3Fts5PutVarint(&(pBuf)->p[(pBuf
)->n], (nData*2)); ((void) (0)); }
;
231897 fts5BufferSafeAppendBlob(pBuf, pMulti->base.pData, nData){ ((void) (0)); memcpy(&(pBuf)->p[(pBuf)->n], pMulti
->base.pData, nData); (pBuf)->n += nData; }
;
231898 memset(&pBuf->p[pBuf->n], 0, FTS5_DATA_ZERO_PADDING8);
231899 }
231900}
231901
231902
231903static void fts5DoclistIterNext(Fts5DoclistIter *pIter){
231904 u8 *p = pIter->aPoslist + pIter->nSize + pIter->nPoslist;
231905
231906 assert( pIter->aPoslist || (p==0 && pIter->aPoslist==0) )((void) (0));
231907 if( p>=pIter->aEof ){
231908 pIter->aPoslist = 0;
231909 }else{
231910 i64 iDelta;
231911
231912 p += fts5GetVarintsqlite3Fts5GetVarint(p, (u64*)&iDelta);
231913 pIter->iRowid += iDelta;
231914
231915 /* Read position list size */
231916 if( p[0] & 0x80 ){
231917 int nPos;
231918 pIter->nSize = fts5GetVarint32(p, nPos)sqlite3Fts5GetVarint32(p,(u32*)&nPos);
231919 pIter->nPoslist = (nPos>>1);
231920 }else{
231921 pIter->nPoslist = ((int)(p[0])) >> 1;
231922 pIter->nSize = 1;
231923 }
231924
231925 pIter->aPoslist = p;
231926 if( &pIter->aPoslist[pIter->nPoslist]>pIter->aEof ){
231927 pIter->aPoslist = 0;
231928 }
231929 }
231930}
231931
231932static void fts5DoclistIterInit(
231933 Fts5Buffer *pBuf,
231934 Fts5DoclistIter *pIter
231935){
231936 memset(pIter, 0, sizeof(*pIter));
231937 if( pBuf->n>0 ){
231938 pIter->aPoslist = pBuf->p;
231939 pIter->aEof = &pBuf->p[pBuf->n];
231940 fts5DoclistIterNext(pIter);
231941 }
231942}
231943
231944#if 0
231945/*
231946** Append a doclist to buffer pBuf.
231947**
231948** This function assumes that space within the buffer has already been
231949** allocated.
231950*/
231951static void fts5MergeAppendDocid({ ((void) (0)); { ((Fts5Buffer *pBuf))->n += sqlite3Fts5PutVarint
(&((Fts5Buffer *pBuf))->p[((Fts5Buffer *pBuf))->n],
((i64 iRowid) - (i64 *piLastRowid))); ((void) (0)); }; (i64 *
piLastRowid) = (i64 iRowid); }
231952 Fts5Buffer *pBuf, /* Buffer to write to */{ ((void) (0)); { ((Fts5Buffer *pBuf))->n += sqlite3Fts5PutVarint
(&((Fts5Buffer *pBuf))->p[((Fts5Buffer *pBuf))->n],
((i64 iRowid) - (i64 *piLastRowid))); ((void) (0)); }; (i64 *
piLastRowid) = (i64 iRowid); }
231953 i64 *piLastRowid, /* IN/OUT: Previous rowid written (if any) */{ ((void) (0)); { ((Fts5Buffer *pBuf))->n += sqlite3Fts5PutVarint
(&((Fts5Buffer *pBuf))->p[((Fts5Buffer *pBuf))->n],
((i64 iRowid) - (i64 *piLastRowid))); ((void) (0)); }; (i64 *
piLastRowid) = (i64 iRowid); }
231954 i64 iRowid /* Rowid to append */{ ((void) (0)); { ((Fts5Buffer *pBuf))->n += sqlite3Fts5PutVarint
(&((Fts5Buffer *pBuf))->p[((Fts5Buffer *pBuf))->n],
((i64 iRowid) - (i64 *piLastRowid))); ((void) (0)); }; (i64 *
piLastRowid) = (i64 iRowid); }
231955){ ((void) (0)); { ((Fts5Buffer *pBuf))->n += sqlite3Fts5PutVarint
(&((Fts5Buffer *pBuf))->p[((Fts5Buffer *pBuf))->n],
((i64 iRowid) - (i64 *piLastRowid))); ((void) (0)); }; (i64 *
piLastRowid) = (i64 iRowid); }
{
231956 assert( pBuf->n!=0 || (*piLastRowid)==0 )((void) (0));
231957 fts5BufferSafeAppendVarint(pBuf, iRowid - *piLastRowid){ (pBuf)->n += sqlite3Fts5PutVarint(&(pBuf)->p[(pBuf
)->n], (iRowid - *piLastRowid)); ((void) (0)); }
;
231958 *piLastRowid = iRowid;
231959}
231960#endif
231961
231962#define fts5MergeAppendDocid(pBuf, iLastRowid, iRowid){ ((void) (0)); { ((pBuf))->n += sqlite3Fts5PutVarint(&
((pBuf))->p[((pBuf))->n], ((iRowid) - (iLastRowid))); (
(void) (0)); }; (iLastRowid) = (iRowid); }
{ \
231963 assert( (pBuf)->n!=0 || (iLastRowid)==0 )((void) (0)); \
231964 fts5BufferSafeAppendVarint((pBuf), (iRowid) - (iLastRowid)){ ((pBuf))->n += sqlite3Fts5PutVarint(&((pBuf))->p[
((pBuf))->n], ((iRowid) - (iLastRowid))); ((void) (0)); }
; \
231965 (iLastRowid) = (iRowid); \
231966}
231967
231968/*
231969** Swap the contents of buffer *p1 with that of *p2.
231970*/
231971static void fts5BufferSwap(Fts5Buffer *p1, Fts5Buffer *p2){
231972 Fts5Buffer tmp = *p1;
231973 *p1 = *p2;
231974 *p2 = tmp;
231975}
231976
231977static void fts5NextRowid(Fts5Buffer *pBuf, int *piOff, i64 *piRowid){
231978 int i = *piOff;
231979 if( i>=pBuf->n ){
231980 *piOff = -1;
231981 }else{
231982 u64 iVal;
231983 *piOff = i + sqlite3Fts5GetVarint(&pBuf->p[i], &iVal);
231984 *piRowid += iVal;
231985 }
231986}
231987
231988/*
231989** This is the equivalent of fts5MergePrefixLists() for detail=none mode.
231990** In this case the buffers consist of a delta-encoded list of rowids only.
231991*/
231992static void fts5MergeRowidLists(
231993 Fts5Index *p, /* FTS5 backend object */
231994 Fts5Buffer *p1, /* First list to merge */
231995 int nBuf, /* Number of entries in apBuf[] */
231996 Fts5Buffer *aBuf /* Array of other lists to merge into p1 */
231997){
231998 int i1 = 0;
231999 int i2 = 0;
232000 i64 iRowid1 = 0;
232001 i64 iRowid2 = 0;
232002 i64 iOut = 0;
232003 Fts5Buffer *p2 = &aBuf[0];
232004 Fts5Buffer out;
232005
232006 (void)nBuf;
232007 memset(&out, 0, sizeof(out));
232008 assert( nBuf==1 )((void) (0));
232009 sqlite3Fts5BufferSize(&p->rc, &out, p1->n + p2->n);
232010 if( p->rc ) return;
232011
232012 fts5NextRowid(p1, &i1, &iRowid1);
232013 fts5NextRowid(p2, &i2, &iRowid2);
232014 while( i1>=0 || i2>=0 ){
232015 if( i1>=0 && (i2<0 || iRowid1<iRowid2) ){
232016 assert( iOut==0 || iRowid1>iOut )((void) (0));
232017 fts5BufferSafeAppendVarint(&out, iRowid1 - iOut){ (&out)->n += sqlite3Fts5PutVarint(&(&out)->
p[(&out)->n], (iRowid1 - iOut)); ((void) (0)); }
;
232018 iOut = iRowid1;
232019 fts5NextRowid(p1, &i1, &iRowid1);
232020 }else{
232021 assert( iOut==0 || iRowid2>iOut )((void) (0));
232022 fts5BufferSafeAppendVarint(&out, iRowid2 - iOut){ (&out)->n += sqlite3Fts5PutVarint(&(&out)->
p[(&out)->n], (iRowid2 - iOut)); ((void) (0)); }
;
232023 iOut = iRowid2;
232024 if( i1>=0 && iRowid1==iRowid2 ){
232025 fts5NextRowid(p1, &i1, &iRowid1);
232026 }
232027 fts5NextRowid(p2, &i2, &iRowid2);
232028 }
232029 }
232030
232031 fts5BufferSwap(&out, p1);
232032 fts5BufferFree(&out)sqlite3Fts5BufferFree(&out);
232033}
232034
232035typedef struct PrefixMerger PrefixMerger;
232036struct PrefixMerger {
232037 Fts5DoclistIter iter; /* Doclist iterator */
232038 i64 iPos; /* For iterating through a position list */
232039 int iOff;
232040 u8 *aPos;
232041 PrefixMerger *pNext; /* Next in docid/poslist order */
232042};
232043
232044static void fts5PrefixMergerInsertByRowid(
232045 PrefixMerger **ppHead,
232046 PrefixMerger *p
232047){
232048 if( p->iter.aPoslist ){
232049 PrefixMerger **pp = ppHead;
232050 while( *pp && p->iter.iRowid>(*pp)->iter.iRowid ){
232051 pp = &(*pp)->pNext;
232052 }
232053 p->pNext = *pp;
232054 *pp = p;
232055 }
232056}
232057
232058static void fts5PrefixMergerInsertByPosition(
232059 PrefixMerger **ppHead,
232060 PrefixMerger *p
232061){
232062 if( p->iPos>=0 ){
232063 PrefixMerger **pp = ppHead;
232064 while( *pp && p->iPos>(*pp)->iPos ){
232065 pp = &(*pp)->pNext;
232066 }
232067 p->pNext = *pp;
232068 *pp = p;
232069 }
232070}
232071
232072
232073/*
232074** Array aBuf[] contains nBuf doclists. These are all merged in with the
232075** doclist in buffer p1.
232076*/
232077static void fts5MergePrefixLists(
232078 Fts5Index *p, /* FTS5 backend object */
232079 Fts5Buffer *p1, /* First list to merge */
232080 int nBuf, /* Number of buffers in array aBuf[] */
232081 Fts5Buffer *aBuf /* Other lists to merge in */
232082){
232083#define fts5PrefixMergerNextPosition(p)sqlite3Fts5PoslistNext64((p)->aPos,(p)->iter.nPoslist,&
(p)->iOff,&(p)->iPos)
\
232084 sqlite3Fts5PoslistNext64((p)->aPos,(p)->iter.nPoslist,&(p)->iOff,&(p)->iPos)
232085#define FTS5_MERGE_NLIST16 16
232086 PrefixMerger aMerger[FTS5_MERGE_NLIST16];
232087 PrefixMerger *pHead = 0;
232088 int i;
232089 int nOut = 0;
232090 Fts5Buffer out = {0, 0, 0};
232091 Fts5Buffer tmp = {0, 0, 0};
232092 i64 iLastRowid = 0;
232093
232094 /* Initialize a doclist-iterator for each input buffer. Arrange them in
232095 ** a linked-list starting at pHead in ascending order of rowid. Avoid
232096 ** linking any iterators already at EOF into the linked list at all. */
232097 assert( nBuf+1<=sizeof(aMerger)/sizeof(aMerger[0]) )((void) (0));
232098 memset(aMerger, 0, sizeof(PrefixMerger)*(nBuf+1));
232099 pHead = &aMerger[nBuf];
232100 fts5DoclistIterInit(p1, &pHead->iter);
232101 for(i=0; i<nBuf; i++){
232102 fts5DoclistIterInit(&aBuf[i], &aMerger[i].iter);
232103 fts5PrefixMergerInsertByRowid(&pHead, &aMerger[i]);
232104 nOut += aBuf[i].n;
232105 }
232106 if( nOut==0 ) return;
232107 nOut += p1->n + 9 + 10*nBuf;
232108
232109 /* The maximum size of the output is equal to the sum of the
232110 ** input sizes + 1 varint (9 bytes). The extra varint is because if the
232111 ** first rowid in one input is a large negative number, and the first in
232112 ** the other a non-negative number, the delta for the non-negative
232113 ** number will be larger on disk than the literal integer value
232114 ** was.
232115 **
232116 ** Or, if the input position-lists are corrupt, then the output might
232117 ** include up to (nBuf+1) extra 10-byte positions created by interpreting -1
232118 ** (the value PoslistNext64() uses for EOF) as a position and appending
232119 ** it to the output. This can happen at most once for each input
232120 ** position-list, hence (nBuf+1) 10 byte paddings. */
232121 if( sqlite3Fts5BufferSize(&p->rc, &out, nOut) ) return;
232122
232123 while( pHead ){
232124 fts5MergeAppendDocid(&out, iLastRowid, pHead->iter.iRowid){ ((void) (0)); { ((&out))->n += sqlite3Fts5PutVarint(
&((&out))->p[((&out))->n], ((pHead->iter
.iRowid) - (iLastRowid))); ((void) (0)); }; (iLastRowid) = (pHead
->iter.iRowid); }
;
232125
232126 if( pHead->pNext && iLastRowid==pHead->pNext->iter.iRowid ){
232127 /* Merge data from two or more poslists */
232128 i64 iPrev = 0;
232129 int nTmp = FTS5_DATA_ZERO_PADDING8;
232130 int nMerge = 0;
232131 PrefixMerger *pSave = pHead;
232132 PrefixMerger *pThis = 0;
232133 int nTail = 0;
232134
232135 pHead = 0;
232136 while( pSave && pSave->iter.iRowid==iLastRowid ){
232137 PrefixMerger *pNext = pSave->pNext;
232138 pSave->iOff = 0;
232139 pSave->iPos = 0;
232140 pSave->aPos = &pSave->iter.aPoslist[pSave->iter.nSize];
232141 fts5PrefixMergerNextPosition(pSave)sqlite3Fts5PoslistNext64((pSave)->aPos,(pSave)->iter.nPoslist
,&(pSave)->iOff,&(pSave)->iPos)
;
232142 nTmp += pSave->iter.nPoslist + 10;
232143 nMerge++;
232144 fts5PrefixMergerInsertByPosition(&pHead, pSave);
232145 pSave = pNext;
232146 }
232147
232148 if( pHead==0 || pHead->pNext==0 ){
232149 p->rc = FTS5_CORRUPT(11 | (1<<8));
232150 break;
232151 }
232152
232153 /* See the earlier comment in this function for an explanation of why
232154 ** corrupt input position lists might cause the output to consume
232155 ** at most nMerge*10 bytes of unexpected space. */
232156 if( sqlite3Fts5BufferSize(&p->rc, &tmp, nTmp+nMerge*10) ){
232157 break;
232158 }
232159 fts5BufferZero(&tmp)sqlite3Fts5BufferZero(&tmp);
232160
232161 pThis = pHead;
232162 pHead = pThis->pNext;
232163 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, pThis->iPos);
232164 fts5PrefixMergerNextPosition(pThis)sqlite3Fts5PoslistNext64((pThis)->aPos,(pThis)->iter.nPoslist
,&(pThis)->iOff,&(pThis)->iPos)
;
232165 fts5PrefixMergerInsertByPosition(&pHead, pThis);
232166
232167 while( pHead->pNext ){
232168 pThis = pHead;
232169 if( pThis->iPos!=iPrev ){
232170 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, pThis->iPos);
232171 }
232172 fts5PrefixMergerNextPosition(pThis)sqlite3Fts5PoslistNext64((pThis)->aPos,(pThis)->iter.nPoslist
,&(pThis)->iOff,&(pThis)->iPos)
;
232173 pHead = pThis->pNext;
232174 fts5PrefixMergerInsertByPosition(&pHead, pThis);
232175 }
232176
232177 if( pHead->iPos!=iPrev ){
232178 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, pHead->iPos);
232179 }
232180 nTail = pHead->iter.nPoslist - pHead->iOff;
232181
232182 /* WRITEPOSLISTSIZE */
232183 assert_nc( tmp.n+nTail<=nTmp )((void) (0));
232184 assert( tmp.n+nTail<=nTmp+nMerge*10 )((void) (0));
232185 if( tmp.n+nTail>nTmp-FTS5_DATA_ZERO_PADDING8 ){
232186 if( p->rc==SQLITE_OK0 ) p->rc = FTS5_CORRUPT(11 | (1<<8));
232187 break;
232188 }
232189 fts5BufferSafeAppendVarint(&out, (tmp.n+nTail) * 2){ (&out)->n += sqlite3Fts5PutVarint(&(&out)->
p[(&out)->n], ((tmp.n+nTail) * 2)); ((void) (0)); }
;
232190 fts5BufferSafeAppendBlob(&out, tmp.p, tmp.n){ ((void) (0)); memcpy(&(&out)->p[(&out)->n
], tmp.p, tmp.n); (&out)->n += tmp.n; }
;
232191 if( nTail>0 ){
232192 fts5BufferSafeAppendBlob(&out, &pHead->aPos[pHead->iOff], nTail){ ((void) (0)); memcpy(&(&out)->p[(&out)->n
], &pHead->aPos[pHead->iOff], nTail); (&out)->
n += nTail; }
;
232193 }
232194
232195 pHead = pSave;
232196 for(i=0; i<nBuf+1; i++){
232197 PrefixMerger *pX = &aMerger[i];
232198 if( pX->iter.aPoslist && pX->iter.iRowid==iLastRowid ){
232199 fts5DoclistIterNext(&pX->iter);
232200 fts5PrefixMergerInsertByRowid(&pHead, pX);
232201 }
232202 }
232203
232204 }else{
232205 /* Copy poslist from pHead to output */
232206 PrefixMerger *pThis = pHead;
232207 Fts5DoclistIter *pI = &pThis->iter;
232208 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; }
;
232209 fts5DoclistIterNext(pI);
232210 pHead = pThis->pNext;
232211 fts5PrefixMergerInsertByRowid(&pHead, pThis);
232212 }
232213 }
232214
232215 fts5BufferFree(p1)sqlite3Fts5BufferFree(p1);
232216 fts5BufferFree(&tmp)sqlite3Fts5BufferFree(&tmp);
232217 memset(&out.p[out.n], 0, FTS5_DATA_ZERO_PADDING8);
232218 *p1 = out;
232219}
232220
232221static void fts5SetupPrefixIter(
232222 Fts5Index *p, /* Index to read from */
232223 int bDesc, /* True for "ORDER BY rowid DESC" */
232224 int iIdx, /* Index to scan for data */
232225 u8 *pToken, /* Buffer containing prefix to match */
232226 int nToken, /* Size of buffer pToken in bytes */
232227 Fts5Colset *pColset, /* Restrict matches to these columns */
232228 Fts5Iter **ppIter /* OUT: New iterator */
232229){
232230 Fts5Structure *pStruct;
232231 Fts5Buffer *aBuf;
232232 int nBuf = 32;
232233 int nMerge = 1;
232234
232235 void (*xMerge)(Fts5Index*, Fts5Buffer*, int, Fts5Buffer*);
232236 void (*xAppend)(Fts5Index*, i64, Fts5Iter*, Fts5Buffer*);
232237 if( p->pConfig->eDetail==FTS5_DETAIL_NONE1 ){
232238 xMerge = fts5MergeRowidLists;
232239 xAppend = fts5AppendRowid;
232240 }else{
232241 nMerge = FTS5_MERGE_NLIST16-1;
232242 nBuf = nMerge*8; /* Sufficient to merge (16^8)==(2^32) lists */
232243 xMerge = fts5MergePrefixLists;
232244 xAppend = fts5AppendPoslist;
232245 }
232246
232247 aBuf = (Fts5Buffer*)fts5IdxMalloc(p, sizeof(Fts5Buffer)*nBuf);
232248 pStruct = fts5StructureRead(p);
232249
232250 if( aBuf && pStruct ){
232251 const int flags = FTS5INDEX_QUERY_SCAN0x0008
232252 | FTS5INDEX_QUERY_SKIPEMPTY0x0010
232253 | FTS5INDEX_QUERY_NOOUTPUT0x0020;
232254 int i;
232255 i64 iLastRowid = 0;
232256 Fts5Iter *p1 = 0; /* Iterator used to gather data from index */
232257 Fts5Data *pData;
232258 Fts5Buffer doclist;
232259 int bNewTerm = 1;
232260
232261 memset(&doclist, 0, sizeof(doclist));
232262 if( iIdx!=0 ){
232263 int dummy = 0;
232264 const int f2 = FTS5INDEX_QUERY_SKIPEMPTY0x0010|FTS5INDEX_QUERY_NOOUTPUT0x0020;
232265 pToken[0] = FTS5_MAIN_PREFIX'0';
232266 fts5MultiIterNew(p, pStruct, f2, pColset, pToken, nToken, -1, 0, &p1);
232267 fts5IterSetOutputCb(&p->rc, p1);
232268 for(;
232269 fts5MultiIterEof(p, p1)==0;
232270 fts5MultiIterNext2(p, p1, &dummy)
232271 ){
232272 Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ];
232273 p1->xSetOutputs(p1, pSeg);
232274 if( p1->base.nData ){
232275 xAppend(p, p1->base.iRowid-iLastRowid, p1, &doclist);
232276 iLastRowid = p1->base.iRowid;
232277 }
232278 }
232279 fts5MultiIterFree(p1);
232280 }
232281
232282 pToken[0] = FTS5_MAIN_PREFIX'0' + iIdx;
232283 fts5MultiIterNew(p, pStruct, flags, pColset, pToken, nToken, -1, 0, &p1);
232284 fts5IterSetOutputCb(&p->rc, p1);
232285 for( /* no-op */ ;
232286 fts5MultiIterEof(p, p1)==0;
232287 fts5MultiIterNext2(p, p1, &bNewTerm)
232288 ){
232289 Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ];
232290 int nTerm = pSeg->term.n;
232291 const u8 *pTerm = pSeg->term.p;
232292 p1->xSetOutputs(p1, pSeg);
232293
232294 assert_nc( memcmp(pToken, pTerm, MIN(nToken, nTerm))<=0 )((void) (0));
232295 if( bNewTerm ){
232296 if( nTerm<nToken || memcmp(pToken, pTerm, nToken) ) break;
232297 }
232298
232299 if( p1->base.nData==0 ) continue;
232300
232301 if( p1->base.iRowid<=iLastRowid && doclist.n>0 ){
232302 for(i=0; p->rc==SQLITE_OK0 && doclist.n; i++){
232303 int i1 = i*nMerge;
232304 int iStore;
232305 assert( i1+nMerge<=nBuf )((void) (0));
232306 for(iStore=i1; iStore<i1+nMerge; iStore++){
232307 if( aBuf[iStore].n==0 ){
232308 fts5BufferSwap(&doclist, &aBuf[iStore]);
232309 fts5BufferZero(&doclist)sqlite3Fts5BufferZero(&doclist);
232310 break;
232311 }
232312 }
232313 if( iStore==i1+nMerge ){
232314 xMerge(p, &doclist, nMerge, &aBuf[i1]);
232315 for(iStore=i1; iStore<i1+nMerge; iStore++){
232316 fts5BufferZero(&aBuf[iStore])sqlite3Fts5BufferZero(&aBuf[iStore]);
232317 }
232318 }
232319 }
232320 iLastRowid = 0;
232321 }
232322
232323 xAppend(p, p1->base.iRowid-iLastRowid, p1, &doclist);
232324 iLastRowid = p1->base.iRowid;
232325 }
232326
232327 assert( (nBuf%nMerge)==0 )((void) (0));
232328 for(i=0; i<nBuf; i+=nMerge){
232329 int iFree;
232330 if( p->rc==SQLITE_OK0 ){
232331 xMerge(p, &doclist, nMerge, &aBuf[i]);
232332 }
232333 for(iFree=i; iFree<i+nMerge; iFree++){
232334 fts5BufferFree(&aBuf[iFree])sqlite3Fts5BufferFree(&aBuf[iFree]);
232335 }
232336 }
232337 fts5MultiIterFree(p1);
232338
232339 pData = fts5IdxMalloc(p, sizeof(Fts5Data)+doclist.n+FTS5_DATA_ZERO_PADDING8);
232340 if( pData ){
232341 pData->p = (u8*)&pData[1];
232342 pData->nn = pData->szLeaf = doclist.n;
232343 if( doclist.n ) memcpy(pData->p, doclist.p, doclist.n);
232344 fts5MultiIterNew2(p, pData, bDesc, ppIter);
232345 }
232346 fts5BufferFree(&doclist)sqlite3Fts5BufferFree(&doclist);
232347 }
232348
232349 fts5StructureRelease(pStruct);
232350 sqlite3_free(aBuf);
232351}
232352
232353
232354/*
232355** Indicate that all subsequent calls to sqlite3Fts5IndexWrite() pertain
232356** to the document with rowid iRowid.
232357*/
232358static int sqlite3Fts5IndexBeginWrite(Fts5Index *p, int bDelete, i64 iRowid){
232359 assert( p->rc==SQLITE_OK )((void) (0));
232360
232361 /* Allocate the hash table if it has not already been allocated */
232362 if( p->pHash==0 ){
232363 p->rc = sqlite3Fts5HashNew(p->pConfig, &p->pHash, &p->nPendingData);
232364 }
232365
232366 /* Flush the hash table to disk if required */
232367 if( iRowid<p->iWriteRowid
232368 || (iRowid==p->iWriteRowid && p->bDelete==0)
232369 || (p->nPendingData > p->pConfig->nHashSize)
232370 ){
232371 fts5IndexFlush(p);
232372 }
232373
232374 p->iWriteRowid = iRowid;
232375 p->bDelete = bDelete;
232376 return fts5IndexReturn(p);
232377}
232378
232379/*
232380** Commit data to disk.
232381*/
232382static int sqlite3Fts5IndexSync(Fts5Index *p){
232383 assert( p->rc==SQLITE_OK )((void) (0));
232384 fts5IndexFlush(p);
232385 sqlite3Fts5IndexCloseReader(p);
232386 return fts5IndexReturn(p);
232387}
232388
232389/*
232390** Discard any data stored in the in-memory hash tables. Do not write it
232391** to the database. Additionally, assume that the contents of the %_data
232392** table may have changed on disk. So any in-memory caches of %_data
232393** records must be invalidated.
232394*/
232395static int sqlite3Fts5IndexRollback(Fts5Index *p){
232396 sqlite3Fts5IndexCloseReader(p);
232397 fts5IndexDiscardData(p);
232398 fts5StructureInvalidate(p);
232399 /* assert( p->rc==SQLITE_OK ); */
232400 return SQLITE_OK0;
232401}
232402
232403/*
232404** The %_data table is completely empty when this function is called. This
232405** function populates it with the initial structure objects for each index,
232406** and the initial version of the "averages" record (a zero-byte blob).
232407*/
232408static int sqlite3Fts5IndexReinit(Fts5Index *p){
232409 Fts5Structure s;
232410 fts5StructureInvalidate(p);
232411 fts5IndexDiscardData(p);
232412 memset(&s, 0, sizeof(Fts5Structure));
232413 fts5DataWrite(p, FTS5_AVERAGES_ROWID1, (const u8*)"", 0);
232414 fts5StructureWrite(p, &s);
232415 return fts5IndexReturn(p);
232416}
232417
232418/*
232419** Open a new Fts5Index handle. If the bCreate argument is true, create
232420** and initialize the underlying %_data table.
232421**
232422** If successful, set *pp to point to the new object and return SQLITE_OK.
232423** Otherwise, set *pp to NULL and return an SQLite error code.
232424*/
232425static int sqlite3Fts5IndexOpen(
232426 Fts5Config *pConfig,
232427 int bCreate,
232428 Fts5Index **pp,
232429 char **pzErr
232430){
232431 int rc = SQLITE_OK0;
232432 Fts5Index *p; /* New object */
232433
232434 *pp = p = (Fts5Index*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Index));
232435 if( rc==SQLITE_OK0 ){
232436 p->pConfig = pConfig;
232437 p->nWorkUnit = FTS5_WORK_UNIT64;
232438 p->zDataTbl = sqlite3Fts5Mprintf(&rc, "%s_data", pConfig->zName);
232439 if( p->zDataTbl && bCreate ){
232440 rc = sqlite3Fts5CreateTable(
232441 pConfig, "data", "id INTEGER PRIMARY KEY, block BLOB", 0, pzErr
232442 );
232443 if( rc==SQLITE_OK0 ){
232444 rc = sqlite3Fts5CreateTable(pConfig, "idx",
232445 "segid, term, pgno, PRIMARY KEY(segid, term)",
232446 1, pzErr
232447 );
232448 }
232449 if( rc==SQLITE_OK0 ){
232450 rc = sqlite3Fts5IndexReinit(p);
232451 }
232452 }
232453 }
232454
232455 assert( rc!=SQLITE_OK || p->rc==SQLITE_OK )((void) (0));
232456 if( rc ){
232457 sqlite3Fts5IndexClose(p);
232458 *pp = 0;
232459 }
232460 return rc;
232461}
232462
232463/*
232464** Close a handle opened by an earlier call to sqlite3Fts5IndexOpen().
232465*/
232466static int sqlite3Fts5IndexClose(Fts5Index *p){
232467 int rc = SQLITE_OK0;
232468 if( p ){
232469 assert( p->pReader==0 )((void) (0));
232470 fts5StructureInvalidate(p);
232471 sqlite3_finalize(p->pWriter);
232472 sqlite3_finalize(p->pDeleter);
232473 sqlite3_finalize(p->pIdxWriter);
232474 sqlite3_finalize(p->pIdxDeleter);
232475 sqlite3_finalize(p->pIdxSelect);
232476 sqlite3_finalize(p->pDataVersion);
232477 sqlite3Fts5HashFree(p->pHash);
232478 sqlite3_free(p->zDataTbl);
232479 sqlite3_free(p);
232480 }
232481 return rc;
232482}
232483
232484/*
232485** Argument p points to a buffer containing utf-8 text that is n bytes in
232486** size. Return the number of bytes in the nChar character prefix of the
232487** buffer, or 0 if there are less than nChar characters in total.
232488*/
232489static int sqlite3Fts5IndexCharlenToBytelen(
232490 const char *p,
232491 int nByte,
232492 int nChar
232493){
232494 int n = 0;
232495 int i;
232496 for(i=0; i<nChar; i++){
232497 if( n>=nByte ) return 0; /* Input contains fewer than nChar chars */
232498 if( (unsigned char)p[n++]>=0xc0 ){
232499 if( n>=nByte ) return 0;
232500 while( (p[n] & 0xc0)==0x80 ){
232501 n++;
232502 if( n>=nByte ){
232503 if( i+1==nChar ) break;
232504 return 0;
232505 }
232506 }
232507 }
232508 }
232509 return n;
232510}
232511
232512/*
232513** pIn is a UTF-8 encoded string, nIn bytes in size. Return the number of
232514** unicode characters in the string.
232515*/
232516static int fts5IndexCharlen(const char *pIn, int nIn){
232517 int nChar = 0;
232518 int i = 0;
232519 while( i<nIn ){
232520 if( (unsigned char)pIn[i++]>=0xc0 ){
232521 while( i<nIn && (pIn[i] & 0xc0)==0x80 ) i++;
232522 }
232523 nChar++;
232524 }
232525 return nChar;
232526}
232527
232528/*
232529** Insert or remove data to or from the index. Each time a document is
232530** added to or removed from the index, this function is called one or more
232531** times.
232532**
232533** For an insert, it must be called once for each token in the new document.
232534** If the operation is a delete, it must be called (at least) once for each
232535** unique token in the document with an iCol value less than zero. The iPos
232536** argument is ignored for a delete.
232537*/
232538static int sqlite3Fts5IndexWrite(
232539 Fts5Index *p, /* Index to write to */
232540 int iCol, /* Column token appears in (-ve -> delete) */
232541 int iPos, /* Position of token within column */
232542 const char *pToken, int nToken /* Token to add or remove to or from index */
232543){
232544 int i; /* Used to iterate through indexes */
232545 int rc = SQLITE_OK0; /* Return code */
232546 Fts5Config *pConfig = p->pConfig;
232547
232548 assert( p->rc==SQLITE_OK )((void) (0));
232549 assert( (iCol<0)==p->bDelete )((void) (0));
232550
232551 /* Add the entry to the main terms index. */
232552 rc = sqlite3Fts5HashWrite(
232553 p->pHash, p->iWriteRowid, iCol, iPos, FTS5_MAIN_PREFIX'0', pToken, nToken
232554 );
232555
232556 for(i=0; i<pConfig->nPrefix && rc==SQLITE_OK0; i++){
232557 const int nChar = pConfig->aPrefix[i];
232558 int nByte = sqlite3Fts5IndexCharlenToBytelen(pToken, nToken, nChar);
232559 if( nByte ){
232560 rc = sqlite3Fts5HashWrite(p->pHash,
232561 p->iWriteRowid, iCol, iPos, (char)(FTS5_MAIN_PREFIX'0'+i+1), pToken,
232562 nByte
232563 );
232564 }
232565 }
232566
232567 return rc;
232568}
232569
232570/*
232571** Open a new iterator to iterate though all rowid that match the
232572** specified token or token prefix.
232573*/
232574static int sqlite3Fts5IndexQuery(
232575 Fts5Index *p, /* FTS index to query */
232576 const char *pToken, int nToken, /* Token (or prefix) to query for */
232577 int flags, /* Mask of FTS5INDEX_QUERY_X flags */
232578 Fts5Colset *pColset, /* Match these columns only */
232579 Fts5IndexIter **ppIter /* OUT: New iterator object */
232580){
232581 Fts5Config *pConfig = p->pConfig;
232582 Fts5Iter *pRet = 0;
232583 Fts5Buffer buf = {0, 0, 0};
232584
232585 /* If the QUERY_SCAN flag is set, all other flags must be clear. */
232586 assert( (flags & FTS5INDEX_QUERY_SCAN)==0 || flags==FTS5INDEX_QUERY_SCAN )((void) (0));
232587
232588 if( sqlite3Fts5BufferSize(&p->rc, &buf, nToken+1)==0 ){
232589 int iIdx = 0; /* Index to search */
232590 int iPrefixIdx = 0; /* +1 prefix index */
232591 if( nToken>0 ) memcpy(&buf.p[1], pToken, nToken);
232592
232593 /* Figure out which index to search and set iIdx accordingly. If this
232594 ** is a prefix query for which there is no prefix index, set iIdx to
232595 ** greater than pConfig->nPrefix to indicate that the query will be
232596 ** satisfied by scanning multiple terms in the main index.
232597 **
232598 ** If the QUERY_TEST_NOIDX flag was specified, then this must be a
232599 ** prefix-query. Instead of using a prefix-index (if one exists),
232600 ** evaluate the prefix query using the main FTS index. This is used
232601 ** for internal sanity checking by the integrity-check in debug
232602 ** mode only. */
232603#ifdef SQLITE_DEBUG
232604 if( pConfig->bPrefixIndex==0 || (flags & FTS5INDEX_QUERY_TEST_NOIDX0x0004) ){
232605 assert( flags & FTS5INDEX_QUERY_PREFIX )((void) (0));
232606 iIdx = 1+pConfig->nPrefix;
232607 }else
232608#endif
232609 if( flags & FTS5INDEX_QUERY_PREFIX0x0001 ){
232610 int nChar = fts5IndexCharlen(pToken, nToken);
232611 for(iIdx=1; iIdx<=pConfig->nPrefix; iIdx++){
232612 int nIdxChar = pConfig->aPrefix[iIdx-1];
232613 if( nIdxChar==nChar ) break;
232614 if( nIdxChar==nChar+1 ) iPrefixIdx = iIdx;
232615 }
232616 }
232617
232618 if( iIdx<=pConfig->nPrefix ){
232619 /* Straight index lookup */
232620 Fts5Structure *pStruct = fts5StructureRead(p);
232621 buf.p[0] = (u8)(FTS5_MAIN_PREFIX'0' + iIdx);
232622 if( pStruct ){
232623 fts5MultiIterNew(p, pStruct, flags | FTS5INDEX_QUERY_SKIPEMPTY0x0010,
232624 pColset, buf.p, nToken+1, -1, 0, &pRet
232625 );
232626 fts5StructureRelease(pStruct);
232627 }
232628 }else{
232629 /* Scan multiple terms in the main index */
232630 int bDesc = (flags & FTS5INDEX_QUERY_DESC0x0002)!=0;
232631 fts5SetupPrefixIter(p, bDesc, iPrefixIdx, buf.p, nToken+1, pColset,&pRet);
232632 if( pRet==0 ){
232633 assert( p->rc!=SQLITE_OK )((void) (0));
232634 }else{
232635 assert( pRet->pColset==0 )((void) (0));
232636 fts5IterSetOutputCb(&p->rc, pRet);
232637 if( p->rc==SQLITE_OK0 ){
232638 Fts5SegIter *pSeg = &pRet->aSeg[pRet->aFirst[1].iFirst];
232639 if( pSeg->pLeaf ) pRet->xSetOutputs(pRet, pSeg);
232640 }
232641 }
232642 }
232643
232644 if( p->rc ){
232645 sqlite3Fts5IterClose((Fts5IndexIter*)pRet);
232646 pRet = 0;
232647 sqlite3Fts5IndexCloseReader(p);
232648 }
232649
232650 *ppIter = (Fts5IndexIter*)pRet;
232651 sqlite3Fts5BufferFree(&buf);
232652 }
232653 return fts5IndexReturn(p);
232654}
232655
232656/*
232657** Return true if the iterator passed as the only argument is at EOF.
232658*/
232659/*
232660** Move to the next matching rowid.
232661*/
232662static int sqlite3Fts5IterNext(Fts5IndexIter *pIndexIter){
232663 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
232664 assert( pIter->pIndex->rc==SQLITE_OK )((void) (0));
232665 fts5MultiIterNext(pIter->pIndex, pIter, 0, 0);
232666 return fts5IndexReturn(pIter->pIndex);
232667}
232668
232669/*
232670** Move to the next matching term/rowid. Used by the fts5vocab module.
232671*/
232672static int sqlite3Fts5IterNextScan(Fts5IndexIter *pIndexIter){
232673 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
232674 Fts5Index *p = pIter->pIndex;
232675
232676 assert( pIter->pIndex->rc==SQLITE_OK )((void) (0));
232677
232678 fts5MultiIterNext(p, pIter, 0, 0);
232679 if( p->rc==SQLITE_OK0 ){
232680 Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
232681 if( pSeg->pLeaf && pSeg->term.p[0]!=FTS5_MAIN_PREFIX'0' ){
232682 fts5DataRelease(pSeg->pLeaf);
232683 pSeg->pLeaf = 0;
232684 pIter->base.bEof = 1;
232685 }
232686 }
232687
232688 return fts5IndexReturn(pIter->pIndex);
232689}
232690
232691/*
232692** Move to the next matching rowid that occurs at or after iMatch. The
232693** definition of "at or after" depends on whether this iterator iterates
232694** in ascending or descending rowid order.
232695*/
232696static int sqlite3Fts5IterNextFrom(Fts5IndexIter *pIndexIter, i64 iMatch){
232697 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
232698 fts5MultiIterNextFrom(pIter->pIndex, pIter, iMatch);
232699 return fts5IndexReturn(pIter->pIndex);
232700}
232701
232702/*
232703** Return the current term.
232704*/
232705static const char *sqlite3Fts5IterTerm(Fts5IndexIter *pIndexIter, int *pn){
232706 int n;
232707 const char *z = (const char*)fts5MultiIterTerm((Fts5Iter*)pIndexIter, &n);
232708 assert_nc( z || n<=1 )((void) (0));
232709 *pn = n-1;
232710 return (z ? &z[1] : 0);
232711}
232712
232713/*
232714** Close an iterator opened by an earlier call to sqlite3Fts5IndexQuery().
232715*/
232716static void sqlite3Fts5IterClose(Fts5IndexIter *pIndexIter){
232717 if( pIndexIter ){
232718 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
232719 Fts5Index *pIndex = pIter->pIndex;
232720 fts5MultiIterFree(pIter);
232721 sqlite3Fts5IndexCloseReader(pIndex);
232722 }
232723}
232724
232725/*
232726** Read and decode the "averages" record from the database.
232727**
232728** Parameter anSize must point to an array of size nCol, where nCol is
232729** the number of user defined columns in the FTS table.
232730*/
232731static int sqlite3Fts5IndexGetAverages(Fts5Index *p, i64 *pnRow, i64 *anSize){
232732 int nCol = p->pConfig->nCol;
232733 Fts5Data *pData;
232734
232735 *pnRow = 0;
232736 memset(anSize, 0, sizeof(i64) * nCol);
232737 pData = fts5DataRead(p, FTS5_AVERAGES_ROWID1);
232738 if( p->rc==SQLITE_OK0 && pData->nn ){
232739 int i = 0;
232740 int iCol;
232741 i += fts5GetVarintsqlite3Fts5GetVarint(&pData->p[i], (u64*)pnRow);
232742 for(iCol=0; i<pData->nn && iCol<nCol; iCol++){
232743 i += fts5GetVarintsqlite3Fts5GetVarint(&pData->p[i], (u64*)&anSize[iCol]);
232744 }
232745 }
232746
232747 fts5DataRelease(pData);
232748 return fts5IndexReturn(p);
232749}
232750
232751/*
232752** Replace the current "averages" record with the contents of the buffer
232753** supplied as the second argument.
232754*/
232755static int sqlite3Fts5IndexSetAverages(Fts5Index *p, const u8 *pData, int nData){
232756 assert( p->rc==SQLITE_OK )((void) (0));
232757 fts5DataWrite(p, FTS5_AVERAGES_ROWID1, pData, nData);
232758 return fts5IndexReturn(p);
232759}
232760
232761/*
232762** Return the total number of blocks this module has read from the %_data
232763** table since it was created.
232764*/
232765static int sqlite3Fts5IndexReads(Fts5Index *p){
232766 return p->nRead;
232767}
232768
232769/*
232770** Set the 32-bit cookie value stored at the start of all structure
232771** records to the value passed as the second argument.
232772**
232773** Return SQLITE_OK if successful, or an SQLite error code if an error
232774** occurs.
232775*/
232776static int sqlite3Fts5IndexSetCookie(Fts5Index *p, int iNew){
232777 int rc; /* Return code */
232778 Fts5Config *pConfig = p->pConfig; /* Configuration object */
232779 u8 aCookie[4]; /* Binary representation of iNew */
232780 sqlite3_blob *pBlob = 0;
232781
232782 assert( p->rc==SQLITE_OK )((void) (0));
232783 sqlite3Fts5Put32(aCookie, iNew);
232784
232785 rc = sqlite3_blob_open(pConfig->db, pConfig->zDb, p->zDataTbl,
232786 "block", FTS5_STRUCTURE_ROWID10, 1, &pBlob
232787 );
232788 if( rc==SQLITE_OK0 ){
232789 sqlite3_blob_write(pBlob, aCookie, 4, 0);
232790 rc = sqlite3_blob_close(pBlob);
232791 }
232792
232793 return rc;
232794}
232795
232796static int sqlite3Fts5IndexLoadConfig(Fts5Index *p){
232797 Fts5Structure *pStruct;
232798 pStruct = fts5StructureRead(p);
232799 fts5StructureRelease(pStruct);
232800 return fts5IndexReturn(p);
232801}
232802
232803
232804/*************************************************************************
232805**************************************************************************
232806** Below this point is the implementation of the integrity-check
232807** functionality.
232808*/
232809
232810/*
232811** Return a simple checksum value based on the arguments.
232812*/
232813static u64 sqlite3Fts5IndexEntryCksum(
232814 i64 iRowid,
232815 int iCol,
232816 int iPos,
232817 int iIdx,
232818 const char *pTerm,
232819 int nTerm
232820){
232821 int i;
232822 u64 ret = iRowid;
232823 ret += (ret<<3) + iCol;
232824 ret += (ret<<3) + iPos;
232825 if( iIdx>=0 ) ret += (ret<<3) + (FTS5_MAIN_PREFIX'0' + iIdx);
232826 for(i=0; i<nTerm; i++) ret += (ret<<3) + pTerm[i];
232827 return ret;
232828}
232829
232830#ifdef SQLITE_DEBUG
232831/*
232832** This function is purely an internal test. It does not contribute to
232833** FTS functionality, or even the integrity-check, in any way.
232834**
232835** Instead, it tests that the same set of pgno/rowid combinations are
232836** visited regardless of whether the doclist-index identified by parameters
232837** iSegid/iLeaf is iterated in forwards or reverse order.
232838*/
232839static void fts5TestDlidxReverse(
232840 Fts5Index *p,
232841 int iSegid, /* Segment id to load from */
232842 int iLeaf /* Load doclist-index for this leaf */
232843){
232844 Fts5DlidxIter *pDlidx = 0;
232845 u64 cksum1 = 13;
232846 u64 cksum2 = 13;
232847
232848 for(pDlidx=fts5DlidxIterInit(p, 0, iSegid, iLeaf);
232849 fts5DlidxIterEof(p, pDlidx)==0;
232850 fts5DlidxIterNext(p, pDlidx)
232851 ){
232852 i64 iRowid = fts5DlidxIterRowid(pDlidx);
232853 int pgno = fts5DlidxIterPgno(pDlidx);
232854 assert( pgno>iLeaf )((void) (0));
232855 cksum1 += iRowid + ((i64)pgno<<32);
232856 }
232857 fts5DlidxIterFree(pDlidx);
232858 pDlidx = 0;
232859
232860 for(pDlidx=fts5DlidxIterInit(p, 1, iSegid, iLeaf);
232861 fts5DlidxIterEof(p, pDlidx)==0;
232862 fts5DlidxIterPrev(p, pDlidx)
232863 ){
232864 i64 iRowid = fts5DlidxIterRowid(pDlidx);
232865 int pgno = fts5DlidxIterPgno(pDlidx);
232866 assert( fts5DlidxIterPgno(pDlidx)>iLeaf )((void) (0));
232867 cksum2 += iRowid + ((i64)pgno<<32);
232868 }
232869 fts5DlidxIterFree(pDlidx);
232870 pDlidx = 0;
232871
232872 if( p->rc==SQLITE_OK0 && cksum1!=cksum2 ) p->rc = FTS5_CORRUPT(11 | (1<<8));
232873}
232874
232875static int fts5QueryCksum(
232876 Fts5Index *p, /* Fts5 index object */
232877 int iIdx,
232878 const char *z, /* Index key to query for */
232879 int n, /* Size of index key in bytes */
232880 int flags, /* Flags for Fts5IndexQuery */
232881 u64 *pCksum /* IN/OUT: Checksum value */
232882){
232883 int eDetail = p->pConfig->eDetail;
232884 u64 cksum = *pCksum;
232885 Fts5IndexIter *pIter = 0;
232886 int rc = sqlite3Fts5IndexQuery(p, z, n, flags, 0, &pIter);
232887
232888 while( rc==SQLITE_OK0 && ALWAYS(pIter!=0)(pIter!=0) && 0==sqlite3Fts5IterEof(pIter)((pIter)->bEof) ){
232889 i64 rowid = pIter->iRowid;
232890
232891 if( eDetail==FTS5_DETAIL_NONE1 ){
232892 cksum ^= sqlite3Fts5IndexEntryCksum(rowid, 0, 0, iIdx, z, n);
232893 }else{
232894 Fts5PoslistReader sReader;
232895 for(sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &sReader);
232896 sReader.bEof==0;
232897 sqlite3Fts5PoslistReaderNext(&sReader)
232898 ){
232899 int iCol = FTS5_POS2COLUMN(sReader.iPos)(int)(sReader.iPos >> 32);
232900 int iOff = FTS5_POS2OFFSET(sReader.iPos)(int)(sReader.iPos & 0x7FFFFFFF);
232901 cksum ^= sqlite3Fts5IndexEntryCksum(rowid, iCol, iOff, iIdx, z, n);
232902 }
232903 }
232904 if( rc==SQLITE_OK0 ){
232905 rc = sqlite3Fts5IterNext(pIter);
232906 }
232907 }
232908 sqlite3Fts5IterClose(pIter);
232909
232910 *pCksum = cksum;
232911 return rc;
232912}
232913
232914/*
232915** Check if buffer z[], size n bytes, contains as series of valid utf-8
232916** encoded codepoints. If so, return 0. Otherwise, if the buffer does not
232917** contain valid utf-8, return non-zero.
232918*/
232919static int fts5TestUtf8(const char *z, int n){
232920 int i = 0;
232921 assert_nc( n>0 )((void) (0));
232922 while( i<n ){
232923 if( (z[i] & 0x80)==0x00 ){
232924 i++;
232925 }else
232926 if( (z[i] & 0xE0)==0xC0 ){
232927 if( i+1>=n || (z[i+1] & 0xC0)!=0x80 ) return 1;
232928 i += 2;
232929 }else
232930 if( (z[i] & 0xF0)==0xE0 ){
232931 if( i+2>=n || (z[i+1] & 0xC0)!=0x80 || (z[i+2] & 0xC0)!=0x80 ) return 1;
232932 i += 3;
232933 }else
232934 if( (z[i] & 0xF8)==0xF0 ){
232935 if( i+3>=n || (z[i+1] & 0xC0)!=0x80 || (z[i+2] & 0xC0)!=0x80 ) return 1;
232936 if( (z[i+2] & 0xC0)!=0x80 ) return 1;
232937 i += 3;
232938 }else{
232939 return 1;
232940 }
232941 }
232942
232943 return 0;
232944}
232945
232946/*
232947** This function is also purely an internal test. It does not contribute to
232948** FTS functionality, or even the integrity-check, in any way.
232949*/
232950static void fts5TestTerm(
232951 Fts5Index *p,
232952 Fts5Buffer *pPrev, /* Previous term */
232953 const char *z, int n, /* Possibly new term to test */
232954 u64 expected,
232955 u64 *pCksum
232956){
232957 int rc = p->rc;
232958 if( pPrev->n==0 ){
232959 fts5BufferSet(&rc, pPrev, n, (const u8*)z)sqlite3Fts5BufferSet(&rc,pPrev,n,(const u8*)z);
232960 }else
232961 if( rc==SQLITE_OK0 && (pPrev->n!=n || memcmp(pPrev->p, z, n)) ){
232962 u64 cksum3 = *pCksum;
232963 const char *zTerm = (const char*)&pPrev->p[1]; /* term sans prefix-byte */
232964 int nTerm = pPrev->n-1; /* Size of zTerm in bytes */
232965 int iIdx = (pPrev->p[0] - FTS5_MAIN_PREFIX'0');
232966 int flags = (iIdx==0 ? 0 : FTS5INDEX_QUERY_PREFIX0x0001);
232967 u64 ck1 = 0;
232968 u64 ck2 = 0;
232969
232970 /* Check that the results returned for ASC and DESC queries are
232971 ** the same. If not, call this corruption. */
232972 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, flags, &ck1);
232973 if( rc==SQLITE_OK0 ){
232974 int f = flags|FTS5INDEX_QUERY_DESC0x0002;
232975 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
232976 }
232977 if( rc==SQLITE_OK0 && ck1!=ck2 ) rc = FTS5_CORRUPT(11 | (1<<8));
232978
232979 /* If this is a prefix query, check that the results returned if the
232980 ** the index is disabled are the same. In both ASC and DESC order.
232981 **
232982 ** This check may only be performed if the hash table is empty. This
232983 ** is because the hash table only supports a single scan query at
232984 ** a time, and the multi-iter loop from which this function is called
232985 ** is already performing such a scan.
232986 **
232987 ** Also only do this if buffer zTerm contains nTerm bytes of valid
232988 ** utf-8. Otherwise, the last part of the buffer contents might contain
232989 ** a non-utf-8 sequence that happens to be a prefix of a valid utf-8
232990 ** character stored in the main fts index, which will cause the
232991 ** test to fail. */
232992 if( p->nPendingData==0 && 0==fts5TestUtf8(zTerm, nTerm) ){
232993 if( iIdx>0 && rc==SQLITE_OK0 ){
232994 int f = flags|FTS5INDEX_QUERY_TEST_NOIDX0x0004;
232995 ck2 = 0;
232996 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
232997 if( rc==SQLITE_OK0 && ck1!=ck2 ) rc = FTS5_CORRUPT(11 | (1<<8));
232998 }
232999 if( iIdx>0 && rc==SQLITE_OK0 ){
233000 int f = flags|FTS5INDEX_QUERY_TEST_NOIDX0x0004|FTS5INDEX_QUERY_DESC0x0002;
233001 ck2 = 0;
233002 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
233003 if( rc==SQLITE_OK0 && ck1!=ck2 ) rc = FTS5_CORRUPT(11 | (1<<8));
233004 }
233005 }
233006
233007 cksum3 ^= ck1;
233008 fts5BufferSet(&rc, pPrev, n, (const u8*)z)sqlite3Fts5BufferSet(&rc,pPrev,n,(const u8*)z);
233009
233010 if( rc==SQLITE_OK0 && cksum3!=expected ){
233011 rc = FTS5_CORRUPT(11 | (1<<8));
233012 }
233013 *pCksum = cksum3;
233014 }
233015 p->rc = rc;
233016}
233017
233018#else
233019# define fts5TestDlidxReverse(x,y,z)
233020# define fts5TestTerm(u,v,w,x,y,z)
233021#endif
233022
233023/*
233024** Check that:
233025**
233026** 1) All leaves of pSeg between iFirst and iLast (inclusive) exist and
233027** contain zero terms.
233028** 2) All leaves of pSeg between iNoRowid and iLast (inclusive) exist and
233029** contain zero rowids.
233030*/
233031static void fts5IndexIntegrityCheckEmpty(
233032 Fts5Index *p,
233033 Fts5StructureSegment *pSeg, /* Segment to check internal consistency */
233034 int iFirst,
233035 int iNoRowid,
233036 int iLast
233037){
233038 int i;
233039
233040 /* Now check that the iter.nEmpty leaves following the current leaf
233041 ** (a) exist and (b) contain no terms. */
233042 for(i=iFirst; p->rc==SQLITE_OK0 && i<=iLast; i++){
233043 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)) )
);
233044 if( pLeaf ){
233045 if( !fts5LeafIsTermless(pLeaf)((pLeaf)->szLeaf >= (pLeaf)->nn) ) p->rc = FTS5_CORRUPT(11 | (1<<8));
233046 if( i>=iNoRowid && 0!=fts5LeafFirstRowidOff(pLeaf)(fts5GetU16((pLeaf)->p)) ) p->rc = FTS5_CORRUPT(11 | (1<<8));
233047 }
233048 fts5DataRelease(pLeaf);
233049 }
233050}
233051
233052static void fts5IntegrityCheckPgidx(Fts5Index *p, Fts5Data *pLeaf){
233053 int iTermOff = 0;
233054 int ii;
233055
233056 Fts5Buffer buf1 = {0,0,0};
233057 Fts5Buffer buf2 = {0,0,0};
233058
233059 ii = pLeaf->szLeaf;
233060 while( ii<pLeaf->nn && p->rc==SQLITE_OK0 ){
233061 int res;
233062 int iOff;
233063 int nIncr;
233064
233065 ii += fts5GetVarint32(&pLeaf->p[ii], nIncr)sqlite3Fts5GetVarint32(&pLeaf->p[ii],(u32*)&nIncr);
233066 iTermOff += nIncr;
233067 iOff = iTermOff;
233068
233069 if( iOff>=pLeaf->szLeaf ){
233070 p->rc = FTS5_CORRUPT(11 | (1<<8));
233071 }else if( iTermOff==nIncr ){
233072 int nByte;
233073 iOff += fts5GetVarint32(&pLeaf->p[iOff], nByte)sqlite3Fts5GetVarint32(&pLeaf->p[iOff],(u32*)&nByte
)
;
233074 if( (iOff+nByte)>pLeaf->szLeaf ){
233075 p->rc = FTS5_CORRUPT(11 | (1<<8));
233076 }else{
233077 fts5BufferSet(&p->rc, &buf1, nByte, &pLeaf->p[iOff])sqlite3Fts5BufferSet(&p->rc,&buf1,nByte,&pLeaf
->p[iOff])
;
233078 }
233079 }else{
233080 int nKeep, nByte;
233081 iOff += fts5GetVarint32(&pLeaf->p[iOff], nKeep)sqlite3Fts5GetVarint32(&pLeaf->p[iOff],(u32*)&nKeep
)
;
233082 iOff += fts5GetVarint32(&pLeaf->p[iOff], nByte)sqlite3Fts5GetVarint32(&pLeaf->p[iOff],(u32*)&nByte
)
;
233083 if( nKeep>buf1.n || (iOff+nByte)>pLeaf->szLeaf ){
233084 p->rc = FTS5_CORRUPT(11 | (1<<8));
233085 }else{
233086 buf1.n = nKeep;
233087 fts5BufferAppendBlob(&p->rc, &buf1, nByte, &pLeaf->p[iOff])sqlite3Fts5BufferAppendBlob(&p->rc,&buf1,nByte,&
pLeaf->p[iOff])
;
233088 }
233089
233090 if( p->rc==SQLITE_OK0 ){
233091 res = fts5BufferCompare(&buf1, &buf2);
233092 if( res<=0 ) p->rc = FTS5_CORRUPT(11 | (1<<8));
233093 }
233094 }
233095 fts5BufferSet(&p->rc, &buf2, buf1.n, buf1.p)sqlite3Fts5BufferSet(&p->rc,&buf2,buf1.n,buf1.p);
233096 }
233097
233098 fts5BufferFree(&buf1)sqlite3Fts5BufferFree(&buf1);
233099 fts5BufferFree(&buf2)sqlite3Fts5BufferFree(&buf2);
233100}
233101
233102static void fts5IndexIntegrityCheckSegment(
233103 Fts5Index *p, /* FTS5 backend object */
233104 Fts5StructureSegment *pSeg /* Segment to check internal consistency */
233105){
233106 Fts5Config *pConfig = p->pConfig;
233107 sqlite3_stmt *pStmt = 0;
233108 int rc2;
233109 int iIdxPrevLeaf = pSeg->pgnoFirst-1;
233110 int iDlidxPrevLeaf = pSeg->pgnoLast;
233111
233112 if( pSeg->pgnoFirst==0 ) return;
233113
233114 fts5IndexPrepareStmt(p, &pStmt, sqlite3_mprintf(
233115 "SELECT segid, term, (pgno>>1), (pgno&1) FROM %Q.'%q_idx' WHERE segid=%d "
233116 "ORDER BY 1, 2",
233117 pConfig->zDb, pConfig->zName, pSeg->iSegid
233118 ));
233119
233120 /* Iterate through the b-tree hierarchy. */
233121 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pStmt) ){
233122 i64 iRow; /* Rowid for this leaf */
233123 Fts5Data *pLeaf; /* Data for this leaf */
233124
233125 const char *zIdxTerm = (const char*)sqlite3_column_blob(pStmt, 1);
233126 int nIdxTerm = sqlite3_column_bytes(pStmt, 1);
233127 int iIdxLeaf = sqlite3_column_int(pStmt, 2);
233128 int bIdxDlidx = sqlite3_column_int(pStmt, 3);
233129
233130 /* If the leaf in question has already been trimmed from the segment,
233131 ** ignore this b-tree entry. Otherwise, load it into memory. */
233132 if( iIdxLeaf<pSeg->pgnoFirst ) continue;
233133 iRow = FTS5_SEGMENT_ROWID(pSeg->iSegid, iIdxLeaf)( ((i64)(pSeg->iSegid) << (31 +5 +1)) + ((i64)(0) <<
(31 + 5)) + ((i64)(0) << (31)) + ((i64)(iIdxLeaf)) )
;
233134 pLeaf = fts5LeafRead(p, iRow);
233135 if( pLeaf==0 ) break;
233136
233137 /* Check that the leaf contains at least one term, and that it is equal
233138 ** to or larger than the split-key in zIdxTerm. Also check that if there
233139 ** is also a rowid pointer within the leaf page header, it points to a
233140 ** location before the term. */
233141 if( pLeaf->nn<=pLeaf->szLeaf ){
233142 p->rc = FTS5_CORRUPT(11 | (1<<8));
233143 }else{
233144 int iOff; /* Offset of first term on leaf */
233145 int iRowidOff; /* Offset of first rowid on leaf */
233146 int nTerm; /* Size of term on leaf in bytes */
233147 int res; /* Comparison of term and split-key */
233148
233149 iOff = fts5LeafFirstTermOff(pLeaf);
233150 iRowidOff = fts5LeafFirstRowidOff(pLeaf)(fts5GetU16((pLeaf)->p));
233151 if( iRowidOff>=iOff || iOff>=pLeaf->szLeaf ){
233152 p->rc = FTS5_CORRUPT(11 | (1<<8));
233153 }else{
233154 iOff += fts5GetVarint32(&pLeaf->p[iOff], nTerm)sqlite3Fts5GetVarint32(&pLeaf->p[iOff],(u32*)&nTerm
)
;
233155 res = fts5Memcmp(&pLeaf->p[iOff], zIdxTerm, MIN(nTerm, nIdxTerm))((((nTerm)<(nIdxTerm)?(nTerm):(nIdxTerm)))<=0 ? 0 : memcmp
((&pLeaf->p[iOff]), (zIdxTerm), (((nTerm)<(nIdxTerm
)?(nTerm):(nIdxTerm)))))
;
233156 if( res==0 ) res = nTerm - nIdxTerm;
233157 if( res<0 ) p->rc = FTS5_CORRUPT(11 | (1<<8));
233158 }
233159
233160 fts5IntegrityCheckPgidx(p, pLeaf);
233161 }
233162 fts5DataRelease(pLeaf);
233163 if( p->rc ) break;
233164
233165 /* Now check that the iter.nEmpty leaves following the current leaf
233166 ** (a) exist and (b) contain no terms. */
233167 fts5IndexIntegrityCheckEmpty(
233168 p, pSeg, iIdxPrevLeaf+1, iDlidxPrevLeaf+1, iIdxLeaf-1
233169 );
233170 if( p->rc ) break;
233171
233172 /* If there is a doclist-index, check that it looks right. */
233173 if( bIdxDlidx ){
233174 Fts5DlidxIter *pDlidx = 0; /* For iterating through doclist index */
233175 int iPrevLeaf = iIdxLeaf;
233176 int iSegid = pSeg->iSegid;
233177 int iPg = 0;
233178 i64 iKey;
233179
233180 for(pDlidx=fts5DlidxIterInit(p, 0, iSegid, iIdxLeaf);
233181 fts5DlidxIterEof(p, pDlidx)==0;
233182 fts5DlidxIterNext(p, pDlidx)
233183 ){
233184
233185 /* Check any rowid-less pages that occur before the current leaf. */
233186 for(iPg=iPrevLeaf+1; iPg<fts5DlidxIterPgno(pDlidx); iPg++){
233187 iKey = FTS5_SEGMENT_ROWID(iSegid, iPg)( ((i64)(iSegid) << (31 +5 +1)) + ((i64)(0) << (31
+ 5)) + ((i64)(0) << (31)) + ((i64)(iPg)) )
;
233188 pLeaf = fts5DataRead(p, iKey);
233189 if( pLeaf ){
233190 if( fts5LeafFirstRowidOff(pLeaf)(fts5GetU16((pLeaf)->p))!=0 ) p->rc = FTS5_CORRUPT(11 | (1<<8));
233191 fts5DataRelease(pLeaf);
233192 }
233193 }
233194 iPrevLeaf = fts5DlidxIterPgno(pDlidx);
233195
233196 /* Check that the leaf page indicated by the iterator really does
233197 ** contain the rowid suggested by the same. */
233198 iKey = FTS5_SEGMENT_ROWID(iSegid, iPrevLeaf)( ((i64)(iSegid) << (31 +5 +1)) + ((i64)(0) << (31
+ 5)) + ((i64)(0) << (31)) + ((i64)(iPrevLeaf)) )
;
233199 pLeaf = fts5DataRead(p, iKey);
233200 if( pLeaf ){
233201 i64 iRowid;
233202 int iRowidOff = fts5LeafFirstRowidOff(pLeaf)(fts5GetU16((pLeaf)->p));
233203 ASSERT_SZLEAF_OK(pLeaf)((void) (0));
233204 if( iRowidOff>=pLeaf->szLeaf ){
233205 p->rc = FTS5_CORRUPT(11 | (1<<8));
233206 }else{
233207 fts5GetVarintsqlite3Fts5GetVarint(&pLeaf->p[iRowidOff], (u64*)&iRowid);
233208 if( iRowid!=fts5DlidxIterRowid(pDlidx) ) p->rc = FTS5_CORRUPT(11 | (1<<8));
233209 }
233210 fts5DataRelease(pLeaf);
233211 }
233212 }
233213
233214 iDlidxPrevLeaf = iPg;
233215 fts5DlidxIterFree(pDlidx);
233216 fts5TestDlidxReverse(p, iSegid, iIdxLeaf);
233217 }else{
233218 iDlidxPrevLeaf = pSeg->pgnoLast;
233219 /* TODO: Check there is no doclist index */
233220 }
233221
233222 iIdxPrevLeaf = iIdxLeaf;
233223 }
233224
233225 rc2 = sqlite3_finalize(pStmt);
233226 if( p->rc==SQLITE_OK0 ) p->rc = rc2;
233227
233228 /* Page iter.iLeaf must now be the rightmost leaf-page in the segment */
233229#if 0
233230 if( p->rc==SQLITE_OK0 && iter.iLeaf!=pSeg->pgnoLast ){
233231 p->rc = FTS5_CORRUPT(11 | (1<<8));
233232 }
233233#endif
233234}
233235
233236
233237/*
233238** Run internal checks to ensure that the FTS index (a) is internally
233239** consistent and (b) contains entries for which the XOR of the checksums
233240** as calculated by sqlite3Fts5IndexEntryCksum() is cksum.
233241**
233242** Return SQLITE_CORRUPT if any of the internal checks fail, or if the
233243** checksum does not match. Return SQLITE_OK if all checks pass without
233244** error, or some other SQLite error code if another error (e.g. OOM)
233245** occurs.
233246*/
233247static int sqlite3Fts5IndexIntegrityCheck(Fts5Index *p, u64 cksum, int bUseCksum){
233248 int eDetail = p->pConfig->eDetail;
233249 u64 cksum2 = 0; /* Checksum based on contents of indexes */
233250 Fts5Buffer poslist = {0,0,0}; /* Buffer used to hold a poslist */
233251 Fts5Iter *pIter; /* Used to iterate through entire index */
233252 Fts5Structure *pStruct; /* Index structure */
233253 int iLvl, iSeg;
233254
233255#ifdef SQLITE_DEBUG
233256 /* Used by extra internal tests only run if NDEBUG is not defined */
233257 u64 cksum3 = 0; /* Checksum based on contents of indexes */
233258 Fts5Buffer term = {0,0,0}; /* Buffer used to hold most recent term */
233259#endif
233260 const int flags = FTS5INDEX_QUERY_NOOUTPUT0x0020;
233261
233262 /* Load the FTS index structure */
233263 pStruct = fts5StructureRead(p);
233264 if( pStruct==0 ){
233265 assert( p->rc!=SQLITE_OK )((void) (0));
233266 return fts5IndexReturn(p);
233267 }
233268
233269 /* Check that the internal nodes of each segment match the leaves */
233270 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
233271 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
233272 Fts5StructureSegment *pSeg = &pStruct->aLevel[iLvl].aSeg[iSeg];
233273 fts5IndexIntegrityCheckSegment(p, pSeg);
233274 }
233275 }
233276
233277 /* The cksum argument passed to this function is a checksum calculated
233278 ** based on all expected entries in the FTS index (including prefix index
233279 ** entries). This block checks that a checksum calculated based on the
233280 ** actual contents of FTS index is identical.
233281 **
233282 ** Two versions of the same checksum are calculated. The first (stack
233283 ** variable cksum2) based on entries extracted from the full-text index
233284 ** while doing a linear scan of each individual index in turn.
233285 **
233286 ** As each term visited by the linear scans, a separate query for the
233287 ** same term is performed. cksum3 is calculated based on the entries
233288 ** extracted by these queries.
233289 */
233290 for(fts5MultiIterNew(p, pStruct, flags, 0, 0, 0, -1, 0, &pIter);
233291 fts5MultiIterEof(p, pIter)==0;
233292 fts5MultiIterNext(p, pIter, 0, 0)
233293 ){
233294 int n; /* Size of term in bytes */
233295 i64 iPos = 0; /* Position read from poslist */
233296 int iOff = 0; /* Offset within poslist */
233297 i64 iRowid = fts5MultiIterRowid(pIter);
233298 char *z = (char*)fts5MultiIterTerm(pIter, &n);
233299
233300 /* If this is a new term, query for it. Update cksum3 with the results. */
233301 fts5TestTerm(p, &term, z, n, cksum2, &cksum3);
233302
233303 if( eDetail==FTS5_DETAIL_NONE1 ){
233304 if( 0==fts5MultiIterIsEmpty(p, pIter) ){
233305 cksum2 ^= sqlite3Fts5IndexEntryCksum(iRowid, 0, 0, -1, z, n);
233306 }
233307 }else{
233308 poslist.n = 0;
233309 fts5SegiterPoslist(p, &pIter->aSeg[pIter->aFirst[1].iFirst], 0, &poslist);
233310 fts5BufferAppendBlob(&p->rc, &poslist, 4, (const u8*)"\0\0\0\0")sqlite3Fts5BufferAppendBlob(&p->rc,&poslist,4,(const
u8*)"\0\0\0\0")
;
233311 while( 0==sqlite3Fts5PoslistNext64(poslist.p, poslist.n, &iOff, &iPos) ){
233312 int iCol = FTS5_POS2COLUMN(iPos)(int)(iPos >> 32);
233313 int iTokOff = FTS5_POS2OFFSET(iPos)(int)(iPos & 0x7FFFFFFF);
233314 cksum2 ^= sqlite3Fts5IndexEntryCksum(iRowid, iCol, iTokOff, -1, z, n);
233315 }
233316 }
233317 }
233318 fts5TestTerm(p, &term, 0, 0, cksum2, &cksum3);
233319
233320 fts5MultiIterFree(pIter);
233321 if( p->rc==SQLITE_OK0 && bUseCksum && cksum!=cksum2 ) p->rc = FTS5_CORRUPT(11 | (1<<8));
233322
233323 fts5StructureRelease(pStruct);
233324#ifdef SQLITE_DEBUG
233325 fts5BufferFree(&term)sqlite3Fts5BufferFree(&term);
233326#endif
233327 fts5BufferFree(&poslist)sqlite3Fts5BufferFree(&poslist);
233328 return fts5IndexReturn(p);
233329}
233330
233331/*************************************************************************
233332**************************************************************************
233333** Below this point is the implementation of the fts5_decode() scalar
233334** function only.
233335*/
233336
233337#ifdef SQLITE_TEST
233338/*
233339** Decode a segment-data rowid from the %_data table. This function is
233340** the opposite of macro FTS5_SEGMENT_ROWID().
233341*/
233342static void fts5DecodeRowid(
233343 i64 iRowid, /* Rowid from %_data table */
233344 int *piSegid, /* OUT: Segment id */
233345 int *pbDlidx, /* OUT: Dlidx flag */
233346 int *piHeight, /* OUT: Height */
233347 int *piPgno /* OUT: Page number */
233348){
233349 *piPgno = (int)(iRowid & (((i64)1 << FTS5_DATA_PAGE_B31) - 1));
233350 iRowid >>= FTS5_DATA_PAGE_B31;
233351
233352 *piHeight = (int)(iRowid & (((i64)1 << FTS5_DATA_HEIGHT_B5) - 1));
233353 iRowid >>= FTS5_DATA_HEIGHT_B5;
233354
233355 *pbDlidx = (int)(iRowid & 0x0001);
233356 iRowid >>= FTS5_DATA_DLI_B1;
233357
233358 *piSegid = (int)(iRowid & (((i64)1 << FTS5_DATA_ID_B16) - 1));
233359}
233360#endif /* SQLITE_TEST */
233361
233362#ifdef SQLITE_TEST
233363static void fts5DebugRowid(int *pRc, Fts5Buffer *pBuf, i64 iKey){
233364 int iSegid, iHeight, iPgno, bDlidx; /* Rowid compenents */
233365 fts5DecodeRowid(iKey, &iSegid, &bDlidx, &iHeight, &iPgno);
233366
233367 if( iSegid==0 ){
233368 if( iKey==FTS5_AVERAGES_ROWID1 ){
233369 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{averages} ");
233370 }else{
233371 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{structure}");
233372 }
233373 }
233374 else{
233375 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{%ssegid=%d h=%d pgno=%d}",
233376 bDlidx ? "dlidx " : "", iSegid, iHeight, iPgno
233377 );
233378 }
233379}
233380#endif /* SQLITE_TEST */
233381
233382#ifdef SQLITE_TEST
233383static void fts5DebugStructure(
233384 int *pRc, /* IN/OUT: error code */
233385 Fts5Buffer *pBuf,
233386 Fts5Structure *p
233387){
233388 int iLvl, iSeg; /* Iterate through levels, segments */
233389
233390 for(iLvl=0; iLvl<p->nLevel; iLvl++){
233391 Fts5StructureLevel *pLvl = &p->aLevel[iLvl];
233392 sqlite3Fts5BufferAppendPrintf(pRc, pBuf,
233393 " {lvl=%d nMerge=%d nSeg=%d", iLvl, pLvl->nMerge, pLvl->nSeg
233394 );
233395 for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
233396 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
233397 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " {id=%d leaves=%d..%d}",
233398 pSeg->iSegid, pSeg->pgnoFirst, pSeg->pgnoLast
233399 );
233400 }
233401 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "}");
233402 }
233403}
233404#endif /* SQLITE_TEST */
233405
233406#ifdef SQLITE_TEST
233407/*
233408** This is part of the fts5_decode() debugging aid.
233409**
233410** Arguments pBlob/nBlob contain a serialized Fts5Structure object. This
233411** function appends a human-readable representation of the same object
233412** to the buffer passed as the second argument.
233413*/
233414static void fts5DecodeStructure(
233415 int *pRc, /* IN/OUT: error code */
233416 Fts5Buffer *pBuf,
233417 const u8 *pBlob, int nBlob
233418){
233419 int rc; /* Return code */
233420 Fts5Structure *p = 0; /* Decoded structure object */
233421
233422 rc = fts5StructureDecode(pBlob, nBlob, 0, &p);
233423 if( rc!=SQLITE_OK0 ){
233424 *pRc = rc;
233425 return;
233426 }
233427
233428 fts5DebugStructure(pRc, pBuf, p);
233429 fts5StructureRelease(p);
233430}
233431#endif /* SQLITE_TEST */
233432
233433#ifdef SQLITE_TEST
233434/*
233435** This is part of the fts5_decode() debugging aid.
233436**
233437** Arguments pBlob/nBlob contain an "averages" record. This function
233438** appends a human-readable representation of record to the buffer passed
233439** as the second argument.
233440*/
233441static void fts5DecodeAverages(
233442 int *pRc, /* IN/OUT: error code */
233443 Fts5Buffer *pBuf,
233444 const u8 *pBlob, int nBlob
233445){
233446 int i = 0;
233447 const char *zSpace = "";
233448
233449 while( i<nBlob ){
233450 u64 iVal;
233451 i += sqlite3Fts5GetVarint(&pBlob[i], &iVal);
233452 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "%s%d", zSpace, (int)iVal);
233453 zSpace = " ";
233454 }
233455}
233456#endif /* SQLITE_TEST */
233457
233458#ifdef SQLITE_TEST
233459/*
233460** Buffer (a/n) is assumed to contain a list of serialized varints. Read
233461** each varint and append its string representation to buffer pBuf. Return
233462** after either the input buffer is exhausted or a 0 value is read.
233463**
233464** The return value is the number of bytes read from the input buffer.
233465*/
233466static int fts5DecodePoslist(int *pRc, Fts5Buffer *pBuf, const u8 *a, int n){
233467 int iOff = 0;
233468 while( iOff<n ){
233469 int iVal;
233470 iOff += fts5GetVarint32(&a[iOff], iVal)sqlite3Fts5GetVarint32(&a[iOff],(u32*)&iVal);
233471 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " %d", iVal);
233472 }
233473 return iOff;
233474}
233475#endif /* SQLITE_TEST */
233476
233477#ifdef SQLITE_TEST
233478/*
233479** The start of buffer (a/n) contains the start of a doclist. The doclist
233480** may or may not finish within the buffer. This function appends a text
233481** representation of the part of the doclist that is present to buffer
233482** pBuf.
233483**
233484** The return value is the number of bytes read from the input buffer.
233485*/
233486static int fts5DecodeDoclist(int *pRc, Fts5Buffer *pBuf, const u8 *a, int n){
233487 i64 iDocid = 0;
233488 int iOff = 0;
233489
233490 if( n>0 ){
233491 iOff = sqlite3Fts5GetVarint(a, (u64*)&iDocid);
233492 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " id=%lld", iDocid);
233493 }
233494 while( iOff<n ){
233495 int nPos;
233496 int bDel;
233497 iOff += fts5GetPoslistSize(&a[iOff], &nPos, &bDel);
233498 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " nPos=%d%s", nPos, bDel?"*":"");
233499 iOff += fts5DecodePoslist(pRc, pBuf, &a[iOff], MIN(n-iOff, nPos)((n-iOff)<(nPos)?(n-iOff):(nPos)));
233500 if( iOff<n ){
233501 i64 iDelta;
233502 iOff += sqlite3Fts5GetVarint(&a[iOff], (u64*)&iDelta);
233503 iDocid += iDelta;
233504 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " id=%lld", iDocid);
233505 }
233506 }
233507
233508 return iOff;
233509}
233510#endif /* SQLITE_TEST */
233511
233512#ifdef SQLITE_TEST
233513/*
233514** This function is part of the fts5_decode() debugging function. It is
233515** only ever used with detail=none tables.
233516**
233517** Buffer (pData/nData) contains a doclist in the format used by detail=none
233518** tables. This function appends a human-readable version of that list to
233519** buffer pBuf.
233520**
233521** If *pRc is other than SQLITE_OK when this function is called, it is a
233522** no-op. If an OOM or other error occurs within this function, *pRc is
233523** set to an SQLite error code before returning. The final state of buffer
233524** pBuf is undefined in this case.
233525*/
233526static void fts5DecodeRowidList(
233527 int *pRc, /* IN/OUT: Error code */
233528 Fts5Buffer *pBuf, /* Buffer to append text to */
233529 const u8 *pData, int nData /* Data to decode list-of-rowids from */
233530){
233531 int i = 0;
233532 i64 iRowid = 0;
233533
233534 while( i<nData ){
233535 const char *zApp = "";
233536 u64 iVal;
233537 i += sqlite3Fts5GetVarint(&pData[i], &iVal);
233538 iRowid += iVal;
233539
233540 if( i<nData && pData[i]==0x00 ){
233541 i++;
233542 if( i<nData && pData[i]==0x00 ){
233543 i++;
233544 zApp = "+";
233545 }else{
233546 zApp = "*";
233547 }
233548 }
233549
233550 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " %lld%s", iRowid, zApp);
233551 }
233552}
233553#endif /* SQLITE_TEST */
233554
233555#ifdef SQLITE_TEST
233556/*
233557** The implementation of user-defined scalar function fts5_decode().
233558*/
233559static void fts5DecodeFunction(
233560 sqlite3_context *pCtx, /* Function call context */
233561 int nArg, /* Number of args (always 2) */
233562 sqlite3_value **apVal /* Function arguments */
233563){
233564 i64 iRowid; /* Rowid for record being decoded */
233565 int iSegid,iHeight,iPgno,bDlidx;/* Rowid components */
233566 const u8 *aBlob; int n; /* Record to decode */
233567 u8 *a = 0;
233568 Fts5Buffer s; /* Build up text to return here */
233569 int rc = SQLITE_OK0; /* Return code */
233570 sqlite3_int64 nSpace = 0;
233571 int eDetailNone = (sqlite3_user_data(pCtx)!=0);
233572
233573 assert( nArg==2 )((void) (0));
233574 UNUSED_PARAM(nArg)(void)(nArg);
233575 memset(&s, 0, sizeof(Fts5Buffer));
233576 iRowid = sqlite3_value_int64(apVal[0]);
233577
233578 /* Make a copy of the second argument (a blob) in aBlob[]. The aBlob[]
233579 ** copy is followed by FTS5_DATA_ZERO_PADDING 0x00 bytes, which prevents
233580 ** buffer overreads even if the record is corrupt. */
233581 n = sqlite3_value_bytes(apVal[1]);
233582 aBlob = sqlite3_value_blob(apVal[1]);
233583 nSpace = n + FTS5_DATA_ZERO_PADDING8;
233584 a = (u8*)sqlite3Fts5MallocZero(&rc, nSpace);
233585 if( a==0 ) goto decode_out;
233586 if( n>0 ) memcpy(a, aBlob, n);
233587
233588 fts5DecodeRowid(iRowid, &iSegid, &bDlidx, &iHeight, &iPgno);
233589
233590 fts5DebugRowid(&rc, &s, iRowid);
233591 if( bDlidx ){
233592 Fts5Data dlidx;
233593 Fts5DlidxLvl lvl;
233594
233595 dlidx.p = a;
233596 dlidx.nn = n;
233597
233598 memset(&lvl, 0, sizeof(Fts5DlidxLvl));
233599 lvl.pData = &dlidx;
233600 lvl.iLeafPgno = iPgno;
233601
233602 for(fts5DlidxLvlNext(&lvl); lvl.bEof==0; fts5DlidxLvlNext(&lvl)){
233603 sqlite3Fts5BufferAppendPrintf(&rc, &s,
233604 " %d(%lld)", lvl.iLeafPgno, lvl.iRowid
233605 );
233606 }
233607 }else if( iSegid==0 ){
233608 if( iRowid==FTS5_AVERAGES_ROWID1 ){
233609 fts5DecodeAverages(&rc, &s, a, n);
233610 }else{
233611 fts5DecodeStructure(&rc, &s, a, n);
233612 }
233613 }else if( eDetailNone ){
233614 Fts5Buffer term; /* Current term read from page */
233615 int szLeaf;
233616 int iPgidxOff = szLeaf = fts5GetU16(&a[2]);
233617 int iTermOff;
233618 int nKeep = 0;
233619 int iOff;
233620
233621 memset(&term, 0, sizeof(Fts5Buffer));
233622
233623 /* Decode any entries that occur before the first term. */
233624 if( szLeaf<n ){
233625 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], iTermOff)sqlite3Fts5GetVarint32(&a[iPgidxOff],(u32*)&iTermOff);
233626 }else{
233627 iTermOff = szLeaf;
233628 }
233629 fts5DecodeRowidList(&rc, &s, &a[4], iTermOff-4);
233630
233631 iOff = iTermOff;
233632 while( iOff<szLeaf ){
233633 int nAppend;
233634
233635 /* Read the term data for the next term*/
233636 iOff += fts5GetVarint32(&a[iOff], nAppend)sqlite3Fts5GetVarint32(&a[iOff],(u32*)&nAppend);
233637 term.n = nKeep;
233638 fts5BufferAppendBlob(&rc, &term, nAppend, &a[iOff])sqlite3Fts5BufferAppendBlob(&rc,&term,nAppend,&a[
iOff])
;
233639 sqlite3Fts5BufferAppendPrintf(
233640 &rc, &s, " term=%.*s", term.n, (const char*)term.p
233641 );
233642 iOff += nAppend;
233643
233644 /* Figure out where the doclist for this term ends */
233645 if( iPgidxOff<n ){
233646 int nIncr;
233647 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], nIncr)sqlite3Fts5GetVarint32(&a[iPgidxOff],(u32*)&nIncr);
233648 iTermOff += nIncr;
233649 }else{
233650 iTermOff = szLeaf;
233651 }
233652
233653 fts5DecodeRowidList(&rc, &s, &a[iOff], iTermOff-iOff);
233654 iOff = iTermOff;
233655 if( iOff<szLeaf ){
233656 iOff += fts5GetVarint32(&a[iOff], nKeep)sqlite3Fts5GetVarint32(&a[iOff],(u32*)&nKeep);
233657 }
233658 }
233659
233660 fts5BufferFree(&term)sqlite3Fts5BufferFree(&term);
233661 }else{
233662 Fts5Buffer term; /* Current term read from page */
233663 int szLeaf; /* Offset of pgidx in a[] */
233664 int iPgidxOff;
233665 int iPgidxPrev = 0; /* Previous value read from pgidx */
233666 int iTermOff = 0;
233667 int iRowidOff = 0;
233668 int iOff;
233669 int nDoclist;
233670
233671 memset(&term, 0, sizeof(Fts5Buffer));
233672
233673 if( n<4 ){
233674 sqlite3Fts5BufferSet(&rc, &s, 7, (const u8*)"corrupt");
233675 goto decode_out;
233676 }else{
233677 iRowidOff = fts5GetU16(&a[0]);
233678 iPgidxOff = szLeaf = fts5GetU16(&a[2]);
233679 if( iPgidxOff<n ){
233680 fts5GetVarint32(&a[iPgidxOff], iTermOff)sqlite3Fts5GetVarint32(&a[iPgidxOff],(u32*)&iTermOff);
233681 }else if( iPgidxOff>n ){
233682 rc = FTS5_CORRUPT(11 | (1<<8));
233683 goto decode_out;
233684 }
233685 }
233686
233687 /* Decode the position list tail at the start of the page */
233688 if( iRowidOff!=0 ){
233689 iOff = iRowidOff;
233690 }else if( iTermOff!=0 ){
233691 iOff = iTermOff;
233692 }else{
233693 iOff = szLeaf;
233694 }
233695 if( iOff>n ){
233696 rc = FTS5_CORRUPT(11 | (1<<8));
233697 goto decode_out;
233698 }
233699 fts5DecodePoslist(&rc, &s, &a[4], iOff-4);
233700
233701 /* Decode any more doclist data that appears on the page before the
233702 ** first term. */
233703 nDoclist = (iTermOff ? iTermOff : szLeaf) - iOff;
233704 if( nDoclist+iOff>n ){
233705 rc = FTS5_CORRUPT(11 | (1<<8));
233706 goto decode_out;
233707 }
233708 fts5DecodeDoclist(&rc, &s, &a[iOff], nDoclist);
233709
233710 while( iPgidxOff<n && rc==SQLITE_OK0 ){
233711 int bFirst = (iPgidxOff==szLeaf); /* True for first term on page */
233712 int nByte; /* Bytes of data */
233713 int iEnd;
233714
233715 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], nByte)sqlite3Fts5GetVarint32(&a[iPgidxOff],(u32*)&nByte);
233716 iPgidxPrev += nByte;
233717 iOff = iPgidxPrev;
233718
233719 if( iPgidxOff<n ){
233720 fts5GetVarint32(&a[iPgidxOff], nByte)sqlite3Fts5GetVarint32(&a[iPgidxOff],(u32*)&nByte);
233721 iEnd = iPgidxPrev + nByte;
233722 }else{
233723 iEnd = szLeaf;
233724 }
233725 if( iEnd>szLeaf ){
233726 rc = FTS5_CORRUPT(11 | (1<<8));
233727 break;
233728 }
233729
233730 if( bFirst==0 ){
233731 iOff += fts5GetVarint32(&a[iOff], nByte)sqlite3Fts5GetVarint32(&a[iOff],(u32*)&nByte);
233732 if( nByte>term.n ){
233733 rc = FTS5_CORRUPT(11 | (1<<8));
233734 break;
233735 }
233736 term.n = nByte;
233737 }
233738 iOff += fts5GetVarint32(&a[iOff], nByte)sqlite3Fts5GetVarint32(&a[iOff],(u32*)&nByte);
233739 if( iOff+nByte>n ){
233740 rc = FTS5_CORRUPT(11 | (1<<8));
233741 break;
233742 }
233743 fts5BufferAppendBlob(&rc, &term, nByte, &a[iOff])sqlite3Fts5BufferAppendBlob(&rc,&term,nByte,&a[iOff
])
;
233744 iOff += nByte;
233745
233746 sqlite3Fts5BufferAppendPrintf(
233747 &rc, &s, " term=%.*s", term.n, (const char*)term.p
233748 );
233749 iOff += fts5DecodeDoclist(&rc, &s, &a[iOff], iEnd-iOff);
233750 }
233751
233752 fts5BufferFree(&term)sqlite3Fts5BufferFree(&term);
233753 }
233754
233755 decode_out:
233756 sqlite3_free(a);
233757 if( rc==SQLITE_OK0 ){
233758 sqlite3_result_text(pCtx, (const char*)s.p, s.n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
233759 }else{
233760 sqlite3_result_error_code(pCtx, rc);
233761 }
233762 fts5BufferFree(&s)sqlite3Fts5BufferFree(&s);
233763}
233764#endif /* SQLITE_TEST */
233765
233766#ifdef SQLITE_TEST
233767/*
233768** The implementation of user-defined scalar function fts5_rowid().
233769*/
233770static void fts5RowidFunction(
233771 sqlite3_context *pCtx, /* Function call context */
233772 int nArg, /* Number of args (always 2) */
233773 sqlite3_value **apVal /* Function arguments */
233774){
233775 const char *zArg;
233776 if( nArg==0 ){
233777 sqlite3_result_error(pCtx, "should be: fts5_rowid(subject, ....)", -1);
233778 }else{
233779 zArg = (const char*)sqlite3_value_text(apVal[0]);
233780 if( 0==sqlite3_stricmp(zArg, "segment") ){
233781 i64 iRowid;
233782 int segid, pgno;
233783 if( nArg!=3 ){
233784 sqlite3_result_error(pCtx,
233785 "should be: fts5_rowid('segment', segid, pgno))", -1
233786 );
233787 }else{
233788 segid = sqlite3_value_int(apVal[1]);
233789 pgno = sqlite3_value_int(apVal[2]);
233790 iRowid = FTS5_SEGMENT_ROWID(segid, pgno)( ((i64)(segid) << (31 +5 +1)) + ((i64)(0) << (31
+ 5)) + ((i64)(0) << (31)) + ((i64)(pgno)) )
;
233791 sqlite3_result_int64(pCtx, iRowid);
233792 }
233793 }else{
233794 sqlite3_result_error(pCtx,
233795 "first arg to fts5_rowid() must be 'segment'" , -1
233796 );
233797 }
233798 }
233799}
233800#endif /* SQLITE_TEST */
233801
233802/*
233803** This is called as part of registering the FTS5 module with database
233804** connection db. It registers several user-defined scalar functions useful
233805** with FTS5.
233806**
233807** If successful, SQLITE_OK is returned. If an error occurs, some other
233808** SQLite error code is returned instead.
233809*/
233810static int sqlite3Fts5IndexInit(sqlite3 *db){
233811#ifdef SQLITE_TEST
233812 int rc = sqlite3_create_function(
233813 db, "fts5_decode", 2, SQLITE_UTF81, 0, fts5DecodeFunction, 0, 0
233814 );
233815
233816 if( rc==SQLITE_OK0 ){
233817 rc = sqlite3_create_function(
233818 db, "fts5_decode_none", 2,
233819 SQLITE_UTF81, (void*)db, fts5DecodeFunction, 0, 0
233820 );
233821 }
233822
233823 if( rc==SQLITE_OK0 ){
233824 rc = sqlite3_create_function(
233825 db, "fts5_rowid", -1, SQLITE_UTF81, 0, fts5RowidFunction, 0, 0
233826 );
233827 }
233828 return rc;
233829#else
233830 return SQLITE_OK0;
233831 UNUSED_PARAM(db)(void)(db);
233832#endif
233833}
233834
233835
233836static int sqlite3Fts5IndexReset(Fts5Index *p){
233837 assert( p->pStruct==0 || p->iStructVersion!=0 )((void) (0));
233838 if( fts5IndexDataVersion(p)!=p->iStructVersion ){
233839 fts5StructureInvalidate(p);
233840 }
233841 return fts5IndexReturn(p);
233842}
233843
233844/*
233845** 2014 Jun 09
233846**
233847** The author disclaims copyright to this source code. In place of
233848** a legal notice, here is a blessing:
233849**
233850** May you do good and not evil.
233851** May you find forgiveness for yourself and forgive others.
233852** May you share freely, never taking more than you give.
233853**
233854******************************************************************************
233855**
233856** This is an SQLite module implementing full-text search.
233857*/
233858
233859
233860/* #include "fts5Int.h" */
233861
233862/*
233863** This variable is set to false when running tests for which the on disk
233864** structures should not be corrupt. Otherwise, true. If it is false, extra
233865** assert() conditions in the fts5 code are activated - conditions that are
233866** only true if it is guaranteed that the fts5 database is not corrupt.
233867*/
233868#ifdef SQLITE_DEBUG
233869SQLITE_API int sqlite3_fts5_may_be_corrupt = 1;
233870#endif
233871
233872
233873typedef struct Fts5Auxdata Fts5Auxdata;
233874typedef struct Fts5Auxiliary Fts5Auxiliary;
233875typedef struct Fts5Cursor Fts5Cursor;
233876typedef struct Fts5FullTable Fts5FullTable;
233877typedef struct Fts5Sorter Fts5Sorter;
233878typedef struct Fts5TokenizerModule Fts5TokenizerModule;
233879
233880/*
233881** NOTES ON TRANSACTIONS:
233882**
233883** SQLite invokes the following virtual table methods as transactions are
233884** opened and closed by the user:
233885**
233886** xBegin(): Start of a new transaction.
233887** xSync(): Initial part of two-phase commit.
233888** xCommit(): Final part of two-phase commit.
233889** xRollback(): Rollback the transaction.
233890**
233891** Anything that is required as part of a commit that may fail is performed
233892** in the xSync() callback. Current versions of SQLite ignore any errors
233893** returned by xCommit().
233894**
233895** And as sub-transactions are opened/closed:
233896**
233897** xSavepoint(int S): Open savepoint S.
233898** xRelease(int S): Commit and close savepoint S.
233899** xRollbackTo(int S): Rollback to start of savepoint S.
233900**
233901** During a write-transaction the fts5_index.c module may cache some data
233902** in-memory. It is flushed to disk whenever xSync(), xRelease() or
233903** xSavepoint() is called. And discarded whenever xRollback() or xRollbackTo()
233904** is called.
233905**
233906** Additionally, if SQLITE_DEBUG is defined, an instance of the following
233907** structure is used to record the current transaction state. This information
233908** is not required, but it is used in the assert() statements executed by
233909** function fts5CheckTransactionState() (see below).
233910*/
233911struct Fts5TransactionState {
233912 int eState; /* 0==closed, 1==open, 2==synced */
233913 int iSavepoint; /* Number of open savepoints (0 -> none) */
233914};
233915
233916/*
233917** A single object of this type is allocated when the FTS5 module is
233918** registered with a database handle. It is used to store pointers to
233919** all registered FTS5 extensions - tokenizers and auxiliary functions.
233920*/
233921struct Fts5Global {
233922 fts5_api api; /* User visible part of object (see fts5.h) */
233923 sqlite3 *db; /* Associated database connection */
233924 i64 iNextId; /* Used to allocate unique cursor ids */
233925 Fts5Auxiliary *pAux; /* First in list of all aux. functions */
233926 Fts5TokenizerModule *pTok; /* First in list of all tokenizer modules */
233927 Fts5TokenizerModule *pDfltTok; /* Default tokenizer module */
233928 Fts5Cursor *pCsr; /* First in list of all open cursors */
233929};
233930
233931/*
233932** Each auxiliary function registered with the FTS5 module is represented
233933** by an object of the following type. All such objects are stored as part
233934** of the Fts5Global.pAux list.
233935*/
233936struct Fts5Auxiliary {
233937 Fts5Global *pGlobal; /* Global context for this function */
233938 char *zFunc; /* Function name (nul-terminated) */
233939 void *pUserData; /* User-data pointer */
233940 fts5_extension_function xFunc; /* Callback function */
233941 void (*xDestroy)(void*); /* Destructor function */
233942 Fts5Auxiliary *pNext; /* Next registered auxiliary function */
233943};
233944
233945/*
233946** Each tokenizer module registered with the FTS5 module is represented
233947** by an object of the following type. All such objects are stored as part
233948** of the Fts5Global.pTok list.
233949*/
233950struct Fts5TokenizerModule {
233951 char *zName; /* Name of tokenizer */
233952 void *pUserData; /* User pointer passed to xCreate() */
233953 fts5_tokenizer x; /* Tokenizer functions */
233954 void (*xDestroy)(void*); /* Destructor function */
233955 Fts5TokenizerModule *pNext; /* Next registered tokenizer module */
233956};
233957
233958struct Fts5FullTable {
233959 Fts5Table p; /* Public class members from fts5Int.h */
233960 Fts5Storage *pStorage; /* Document store */
233961 Fts5Global *pGlobal; /* Global (connection wide) data */
233962 Fts5Cursor *pSortCsr; /* Sort data from this cursor */
233963#ifdef SQLITE_DEBUG
233964 struct Fts5TransactionState ts;
233965#endif
233966};
233967
233968struct Fts5MatchPhrase {
233969 Fts5Buffer *pPoslist; /* Pointer to current poslist */
233970 int nTerm; /* Size of phrase in terms */
233971};
233972
233973/*
233974** pStmt:
233975** SELECT rowid, <fts> FROM <fts> ORDER BY +rank;
233976**
233977** aIdx[]:
233978** There is one entry in the aIdx[] array for each phrase in the query,
233979** the value of which is the offset within aPoslist[] following the last
233980** byte of the position list for the corresponding phrase.
233981*/
233982struct Fts5Sorter {
233983 sqlite3_stmt *pStmt;
233984 i64 iRowid; /* Current rowid */
233985 const u8 *aPoslist; /* Position lists for current row */
233986 int nIdx; /* Number of entries in aIdx[] */
233987 int aIdx[1]; /* Offsets into aPoslist for current row */
233988};
233989
233990
233991/*
233992** Virtual-table cursor object.
233993**
233994** iSpecial:
233995** If this is a 'special' query (refer to function fts5SpecialMatch()),
233996** then this variable contains the result of the query.
233997**
233998** iFirstRowid, iLastRowid:
233999** These variables are only used for FTS5_PLAN_MATCH cursors. Assuming the
234000** cursor iterates in ascending order of rowids, iFirstRowid is the lower
234001** limit of rowids to return, and iLastRowid the upper. In other words, the
234002** WHERE clause in the user's query might have been:
234003**
234004** <tbl> MATCH <expr> AND rowid BETWEEN $iFirstRowid AND $iLastRowid
234005**
234006** If the cursor iterates in descending order of rowid, iFirstRowid
234007** is the upper limit (i.e. the "first" rowid visited) and iLastRowid
234008** the lower.
234009*/
234010struct Fts5Cursor {
234011 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
234012 Fts5Cursor *pNext; /* Next cursor in Fts5Cursor.pCsr list */
234013 int *aColumnSize; /* Values for xColumnSize() */
234014 i64 iCsrId; /* Cursor id */
234015
234016 /* Zero from this point onwards on cursor reset */
234017 int ePlan; /* FTS5_PLAN_XXX value */
234018 int bDesc; /* True for "ORDER BY rowid DESC" queries */
234019 i64 iFirstRowid; /* Return no rowids earlier than this */
234020 i64 iLastRowid; /* Return no rowids later than this */
234021 sqlite3_stmt *pStmt; /* Statement used to read %_content */
234022 Fts5Expr *pExpr; /* Expression for MATCH queries */
234023 Fts5Sorter *pSorter; /* Sorter for "ORDER BY rank" queries */
234024 int csrflags; /* Mask of cursor flags (see below) */
234025 i64 iSpecial; /* Result of special query */
234026
234027 /* "rank" function. Populated on demand from vtab.xColumn(). */
234028 char *zRank; /* Custom rank function */
234029 char *zRankArgs; /* Custom rank function args */
234030 Fts5Auxiliary *pRank; /* Rank callback (or NULL) */
234031 int nRankArg; /* Number of trailing arguments for rank() */
234032 sqlite3_value **apRankArg; /* Array of trailing arguments */
234033 sqlite3_stmt *pRankArgStmt; /* Origin of objects in apRankArg[] */
234034
234035 /* Auxiliary data storage */
234036 Fts5Auxiliary *pAux; /* Currently executing extension function */
234037 Fts5Auxdata *pAuxdata; /* First in linked list of saved aux-data */
234038
234039 /* Cache used by auxiliary functions xInst() and xInstCount() */
234040 Fts5PoslistReader *aInstIter; /* One for each phrase */
234041 int nInstAlloc; /* Size of aInst[] array (entries / 3) */
234042 int nInstCount; /* Number of phrase instances */
234043 int *aInst; /* 3 integers per phrase instance */
234044};
234045
234046/*
234047** Bits that make up the "idxNum" parameter passed indirectly by
234048** xBestIndex() to xFilter().
234049*/
234050#define FTS5_BI_MATCH0x0001 0x0001 /* <tbl> MATCH ? */
234051#define FTS5_BI_RANK0x0002 0x0002 /* rank MATCH ? */
234052#define FTS5_BI_ROWID_EQ0x0004 0x0004 /* rowid == ? */
234053#define FTS5_BI_ROWID_LE0x0008 0x0008 /* rowid <= ? */
234054#define FTS5_BI_ROWID_GE0x0010 0x0010 /* rowid >= ? */
234055
234056#define FTS5_BI_ORDER_RANK0x0020 0x0020
234057#define FTS5_BI_ORDER_ROWID0x0040 0x0040
234058#define FTS5_BI_ORDER_DESC0x0080 0x0080
234059
234060/*
234061** Values for Fts5Cursor.csrflags
234062*/
234063#define FTS5CSR_EOF0x01 0x01
234064#define FTS5CSR_REQUIRE_CONTENT0x02 0x02
234065#define FTS5CSR_REQUIRE_DOCSIZE0x04 0x04
234066#define FTS5CSR_REQUIRE_INST0x08 0x08
234067#define FTS5CSR_FREE_ZRANK0x10 0x10
234068#define FTS5CSR_REQUIRE_RESEEK0x20 0x20
234069#define FTS5CSR_REQUIRE_POSLIST0x40 0x40
234070
234071#define BitFlagAllTest(x,y)(((x) & (y))==(y)) (((x) & (y))==(y))
234072#define BitFlagTest(x,y)(((x) & (y))!=0) (((x) & (y))!=0)
234073
234074
234075/*
234076** Macros to Set(), Clear() and Test() cursor flags.
234077*/
234078#define CsrFlagSet(pCsr, flag)((pCsr)->csrflags |= (flag)) ((pCsr)->csrflags |= (flag))
234079#define CsrFlagClear(pCsr, flag)((pCsr)->csrflags &= ~(flag)) ((pCsr)->csrflags &= ~(flag))
234080#define CsrFlagTest(pCsr, flag)((pCsr)->csrflags & (flag)) ((pCsr)->csrflags & (flag))
234081
234082struct Fts5Auxdata {
234083 Fts5Auxiliary *pAux; /* Extension to which this belongs */
234084 void *pPtr; /* Pointer value */
234085 void(*xDelete)(void*); /* Destructor */
234086 Fts5Auxdata *pNext; /* Next object in linked list */
234087};
234088
234089#ifdef SQLITE_DEBUG
234090#define FTS5_BEGIN 1
234091#define FTS5_SYNC 2
234092#define FTS5_COMMIT 3
234093#define FTS5_ROLLBACK 4
234094#define FTS5_SAVEPOINT 5
234095#define FTS5_RELEASE 6
234096#define FTS5_ROLLBACKTO 7
234097static void fts5CheckTransactionState(Fts5FullTable *p, int op, int iSavepoint){
234098 switch( op ){
234099 case FTS5_BEGIN:
234100 assert( p->ts.eState==0 )((void) (0));
234101 p->ts.eState = 1;
234102 p->ts.iSavepoint = -1;
234103 break;
234104
234105 case FTS5_SYNC:
234106 assert( p->ts.eState==1 )((void) (0));
234107 p->ts.eState = 2;
234108 break;
234109
234110 case FTS5_COMMIT:
234111 assert( p->ts.eState==2 )((void) (0));
234112 p->ts.eState = 0;
234113 break;
234114
234115 case FTS5_ROLLBACK:
234116 assert( p->ts.eState==1 || p->ts.eState==2 || p->ts.eState==0 )((void) (0));
234117 p->ts.eState = 0;
234118 break;
234119
234120 case FTS5_SAVEPOINT:
234121 assert( p->ts.eState==1 )((void) (0));
234122 assert( iSavepoint>=0 )((void) (0));
234123 assert( iSavepoint>=p->ts.iSavepoint )((void) (0));
234124 p->ts.iSavepoint = iSavepoint;
234125 break;
234126
234127 case FTS5_RELEASE:
234128 assert( p->ts.eState==1 )((void) (0));
234129 assert( iSavepoint>=0 )((void) (0));
234130 assert( iSavepoint<=p->ts.iSavepoint )((void) (0));
234131 p->ts.iSavepoint = iSavepoint-1;
234132 break;
234133
234134 case FTS5_ROLLBACKTO:
234135 assert( p->ts.eState==1 )((void) (0));
234136 assert( iSavepoint>=-1 )((void) (0));
234137 /* The following assert() can fail if another vtab strikes an error
234138 ** within an xSavepoint() call then SQLite calls xRollbackTo() - without
234139 ** having called xSavepoint() on this vtab. */
234140 /* assert( iSavepoint<=p->ts.iSavepoint ); */
234141 p->ts.iSavepoint = iSavepoint;
234142 break;
234143 }
234144}
234145#else
234146# define fts5CheckTransactionState(x,y,z)
234147#endif
234148
234149/*
234150** Return true if pTab is a contentless table.
234151*/
234152static int fts5IsContentless(Fts5FullTable *pTab){
234153 return pTab->p.pConfig->eContent==FTS5_CONTENT_NONE1;
234154}
234155
234156/*
234157** Delete a virtual table handle allocated by fts5InitVtab().
234158*/
234159static void fts5FreeVtab(Fts5FullTable *pTab){
234160 if( pTab ){
234161 sqlite3Fts5IndexClose(pTab->p.pIndex);
234162 sqlite3Fts5StorageClose(pTab->pStorage);
234163 sqlite3Fts5ConfigFree(pTab->p.pConfig);
234164 sqlite3_free(pTab);
234165 }
234166}
234167
234168/*
234169** The xDisconnect() virtual table method.
234170*/
234171static int fts5DisconnectMethod(sqlite3_vtab *pVtab){
234172 fts5FreeVtab((Fts5FullTable*)pVtab);
234173 return SQLITE_OK0;
234174}
234175
234176/*
234177** The xDestroy() virtual table method.
234178*/
234179static int fts5DestroyMethod(sqlite3_vtab *pVtab){
234180 Fts5Table *pTab = (Fts5Table*)pVtab;
234181 int rc = sqlite3Fts5DropAll(pTab->pConfig);
234182 if( rc==SQLITE_OK0 ){
234183 fts5FreeVtab((Fts5FullTable*)pVtab);
234184 }
234185 return rc;
234186}
234187
234188/*
234189** This function is the implementation of both the xConnect and xCreate
234190** methods of the FTS3 virtual table.
234191**
234192** The argv[] array contains the following:
234193**
234194** argv[0] -> module name ("fts5")
234195** argv[1] -> database name
234196** argv[2] -> table name
234197** argv[...] -> "column name" and other module argument fields.
234198*/
234199static int fts5InitVtab(
234200 int bCreate, /* True for xCreate, false for xConnect */
234201 sqlite3 *db, /* The SQLite database connection */
234202 void *pAux, /* Hash table containing tokenizers */
234203 int argc, /* Number of elements in argv array */
234204 const char * const *argv, /* xCreate/xConnect argument array */
234205 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
234206 char **pzErr /* Write any error message here */
234207){
234208 Fts5Global *pGlobal = (Fts5Global*)pAux;
234209 const char **azConfig = (const char**)argv;
234210 int rc = SQLITE_OK0; /* Return code */
234211 Fts5Config *pConfig = 0; /* Results of parsing argc/argv */
234212 Fts5FullTable *pTab = 0; /* New virtual table object */
234213
234214 /* Allocate the new vtab object and parse the configuration */
234215 pTab = (Fts5FullTable*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5FullTable));
234216 if( rc==SQLITE_OK0 ){
234217 rc = sqlite3Fts5ConfigParse(pGlobal, db, argc, azConfig, &pConfig, pzErr);
234218 assert( (rc==SQLITE_OK && *pzErr==0) || pConfig==0 )((void) (0));
234219 }
234220 if( rc==SQLITE_OK0 ){
234221 pTab->p.pConfig = pConfig;
234222 pTab->pGlobal = pGlobal;
234223 }
234224
234225 /* Open the index sub-system */
234226 if( rc==SQLITE_OK0 ){
234227 rc = sqlite3Fts5IndexOpen(pConfig, bCreate, &pTab->p.pIndex, pzErr);
234228 }
234229
234230 /* Open the storage sub-system */
234231 if( rc==SQLITE_OK0 ){
234232 rc = sqlite3Fts5StorageOpen(
234233 pConfig, pTab->p.pIndex, bCreate, &pTab->pStorage, pzErr
234234 );
234235 }
234236
234237 /* Call sqlite3_declare_vtab() */
234238 if( rc==SQLITE_OK0 ){
234239 rc = sqlite3Fts5ConfigDeclareVtab(pConfig);
234240 }
234241
234242 /* Load the initial configuration */
234243 if( rc==SQLITE_OK0 ){
234244 assert( pConfig->pzErrmsg==0 )((void) (0));
234245 pConfig->pzErrmsg = pzErr;
234246 rc = sqlite3Fts5IndexLoadConfig(pTab->p.pIndex);
234247 sqlite3Fts5IndexRollback(pTab->p.pIndex);
234248 pConfig->pzErrmsg = 0;
234249 }
234250
234251 if( rc!=SQLITE_OK0 ){
234252 fts5FreeVtab(pTab);
234253 pTab = 0;
234254 }else if( bCreate ){
234255 fts5CheckTransactionState(pTab, FTS5_BEGIN, 0);
234256 }
234257 *ppVTab = (sqlite3_vtab*)pTab;
234258 return rc;
234259}
234260
234261/*
234262** The xConnect() and xCreate() methods for the virtual table. All the
234263** work is done in function fts5InitVtab().
234264*/
234265static int fts5ConnectMethod(
234266 sqlite3 *db, /* Database connection */
234267 void *pAux, /* Pointer to tokenizer hash table */
234268 int argc, /* Number of elements in argv array */
234269 const char * const *argv, /* xCreate/xConnect argument array */
234270 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
234271 char **pzErr /* OUT: sqlite3_malloc'd error message */
234272){
234273 return fts5InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
234274}
234275static int fts5CreateMethod(
234276 sqlite3 *db, /* Database connection */
234277 void *pAux, /* Pointer to tokenizer hash table */
234278 int argc, /* Number of elements in argv array */
234279 const char * const *argv, /* xCreate/xConnect argument array */
234280 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
234281 char **pzErr /* OUT: sqlite3_malloc'd error message */
234282){
234283 return fts5InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
234284}
234285
234286/*
234287** The different query plans.
234288*/
234289#define FTS5_PLAN_MATCH1 1 /* (<tbl> MATCH ?) */
234290#define FTS5_PLAN_SOURCE2 2 /* A source cursor for SORTED_MATCH */
234291#define FTS5_PLAN_SPECIAL3 3 /* An internal query */
234292#define FTS5_PLAN_SORTED_MATCH4 4 /* (<tbl> MATCH ? ORDER BY rank) */
234293#define FTS5_PLAN_SCAN5 5 /* No usable constraint */
234294#define FTS5_PLAN_ROWID6 6 /* (rowid = ?) */
234295
234296/*
234297** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
234298** extension is currently being used by a version of SQLite too old to
234299** support index-info flags. In that case this function is a no-op.
234300*/
234301static void fts5SetUniqueFlag(sqlite3_index_info *pIdxInfo){
234302#if SQLITE_VERSION_NUMBER3039002>=3008012
234303#ifndef SQLITE_CORE1
234304 if( sqlite3_libversion_number()>=3008012 )
234305#endif
234306 {
234307 pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE1;
234308 }
234309#endif
234310}
234311
234312static int fts5UsePatternMatch(
234313 Fts5Config *pConfig,
234314 struct sqlite3_index_constraint *p
234315){
234316 assert( FTS5_PATTERN_GLOB==SQLITE_INDEX_CONSTRAINT_GLOB )((void) (0));
234317 assert( FTS5_PATTERN_LIKE==SQLITE_INDEX_CONSTRAINT_LIKE )((void) (0));
234318 if( pConfig->ePattern==FTS5_PATTERN_GLOB66 && p->op==FTS5_PATTERN_GLOB66 ){
234319 return 1;
234320 }
234321 if( pConfig->ePattern==FTS5_PATTERN_LIKE65
234322 && (p->op==FTS5_PATTERN_LIKE65 || p->op==FTS5_PATTERN_GLOB66)
234323 ){
234324 return 1;
234325 }
234326 return 0;
234327}
234328
234329/*
234330** Implementation of the xBestIndex method for FTS5 tables. Within the
234331** WHERE constraint, it searches for the following:
234332**
234333** 1. A MATCH constraint against the table column.
234334** 2. A MATCH constraint against the "rank" column.
234335** 3. A MATCH constraint against some other column.
234336** 4. An == constraint against the rowid column.
234337** 5. A < or <= constraint against the rowid column.
234338** 6. A > or >= constraint against the rowid column.
234339**
234340** Within the ORDER BY, the following are supported:
234341**
234342** 5. ORDER BY rank [ASC|DESC]
234343** 6. ORDER BY rowid [ASC|DESC]
234344**
234345** Information for the xFilter call is passed via both the idxNum and
234346** idxStr variables. Specifically, idxNum is a bitmask of the following
234347** flags used to encode the ORDER BY clause:
234348**
234349** FTS5_BI_ORDER_RANK
234350** FTS5_BI_ORDER_ROWID
234351** FTS5_BI_ORDER_DESC
234352**
234353** idxStr is used to encode data from the WHERE clause. For each argument
234354** passed to the xFilter method, the following is appended to idxStr:
234355**
234356** Match against table column: "m"
234357** Match against rank column: "r"
234358** Match against other column: "M<column-number>"
234359** LIKE against other column: "L<column-number>"
234360** GLOB against other column: "G<column-number>"
234361** Equality constraint against the rowid: "="
234362** A < or <= against the rowid: "<"
234363** A > or >= against the rowid: ">"
234364**
234365** This function ensures that there is at most one "r" or "=". And that if
234366** there exists an "=" then there is no "<" or ">".
234367**
234368** Costs are assigned as follows:
234369**
234370** a) If an unusable MATCH operator is present in the WHERE clause, the
234371** cost is unconditionally set to 1e50 (a really big number).
234372**
234373** a) If a MATCH operator is present, the cost depends on the other
234374** constraints also present. As follows:
234375**
234376** * No other constraints: cost=1000.0
234377** * One rowid range constraint: cost=750.0
234378** * Both rowid range constraints: cost=500.0
234379** * An == rowid constraint: cost=100.0
234380**
234381** b) Otherwise, if there is no MATCH:
234382**
234383** * No other constraints: cost=1000000.0
234384** * One rowid range constraint: cost=750000.0
234385** * Both rowid range constraints: cost=250000.0
234386** * An == rowid constraint: cost=10.0
234387**
234388** Costs are not modified by the ORDER BY clause.
234389*/
234390static int fts5BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
234391 Fts5Table *pTab = (Fts5Table*)pVTab;
234392 Fts5Config *pConfig = pTab->pConfig;
234393 const int nCol = pConfig->nCol;
234394 int idxFlags = 0; /* Parameter passed through to xFilter() */
234395 int i;
234396
234397 char *idxStr;
234398 int iIdxStr = 0;
234399 int iCons = 0;
234400
234401 int bSeenEq = 0;
234402 int bSeenGt = 0;
234403 int bSeenLt = 0;
234404 int bSeenMatch = 0;
234405 int bSeenRank = 0;
234406
234407
234408 assert( SQLITE_INDEX_CONSTRAINT_EQ<SQLITE_INDEX_CONSTRAINT_MATCH )((void) (0));
234409 assert( SQLITE_INDEX_CONSTRAINT_GT<SQLITE_INDEX_CONSTRAINT_MATCH )((void) (0));
234410 assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH )((void) (0));
234411 assert( SQLITE_INDEX_CONSTRAINT_GE<SQLITE_INDEX_CONSTRAINT_MATCH )((void) (0));
234412 assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH )((void) (0));
234413
234414 if( pConfig->bLock ){
234415 pTab->base.zErrMsg = sqlite3_mprintf(
234416 "recursively defined fts5 content table"
234417 );
234418 return SQLITE_ERROR1;
234419 }
234420
234421 idxStr = (char*)sqlite3_malloc(pInfo->nConstraint * 8 + 1);
234422 if( idxStr==0 ) return SQLITE_NOMEM7;
234423 pInfo->idxStr = idxStr;
234424 pInfo->needToFreeIdxStr = 1;
234425
234426 for(i=0; i<pInfo->nConstraint; i++){
234427 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
234428 int iCol = p->iColumn;
234429 if( p->op==SQLITE_INDEX_CONSTRAINT_MATCH64
234430 || (p->op==SQLITE_INDEX_CONSTRAINT_EQ2 && iCol>=nCol)
234431 ){
234432 /* A MATCH operator or equivalent */
234433 if( p->usable==0 || iCol<0 ){
234434 /* As there exists an unusable MATCH constraint this is an
234435 ** unusable plan. Set a prohibitively high cost. */
234436 pInfo->estimatedCost = 1e50;
234437 assert( iIdxStr < pInfo->nConstraint*6 + 1 )((void) (0));
234438 idxStr[iIdxStr] = 0;
234439 return SQLITE_OK0;
234440 }else{
234441 if( iCol==nCol+1 ){
234442 if( bSeenRank ) continue;
234443 idxStr[iIdxStr++] = 'r';
234444 bSeenRank = 1;
234445 }else if( iCol>=0 ){
234446 bSeenMatch = 1;
234447 idxStr[iIdxStr++] = 'M';
234448 sqlite3_snprintf(6, &idxStr[iIdxStr], "%d", iCol);
234449 idxStr += strlen(&idxStr[iIdxStr]);
234450 assert( idxStr[iIdxStr]=='\0' )((void) (0));
234451 }
234452 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
234453 pInfo->aConstraintUsage[i].omit = 1;
234454 }
234455 }else if( p->usable ){
234456 if( iCol>=0 && iCol<nCol && fts5UsePatternMatch(pConfig, p) ){
234457 assert( p->op==FTS5_PATTERN_LIKE || p->op==FTS5_PATTERN_GLOB )((void) (0));
234458 idxStr[iIdxStr++] = p->op==FTS5_PATTERN_LIKE65 ? 'L' : 'G';
234459 sqlite3_snprintf(6, &idxStr[iIdxStr], "%d", iCol);
234460 idxStr += strlen(&idxStr[iIdxStr]);
234461 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
234462 assert( idxStr[iIdxStr]=='\0' )((void) (0));
234463 }else if( bSeenEq==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ2 && iCol<0 ){
234464 idxStr[iIdxStr++] = '=';
234465 bSeenEq = 1;
234466 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
234467 }
234468 }
234469 }
234470
234471 if( bSeenEq==0 ){
234472 for(i=0; i<pInfo->nConstraint; i++){
234473 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
234474 if( p->iColumn<0 && p->usable ){
234475 int op = p->op;
234476 if( op==SQLITE_INDEX_CONSTRAINT_LT16 || op==SQLITE_INDEX_CONSTRAINT_LE8 ){
234477 if( bSeenLt ) continue;
234478 idxStr[iIdxStr++] = '<';
234479 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
234480 bSeenLt = 1;
234481 }else
234482 if( op==SQLITE_INDEX_CONSTRAINT_GT4 || op==SQLITE_INDEX_CONSTRAINT_GE32 ){
234483 if( bSeenGt ) continue;
234484 idxStr[iIdxStr++] = '>';
234485 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
234486 bSeenGt = 1;
234487 }
234488 }
234489 }
234490 }
234491 idxStr[iIdxStr] = '\0';
234492
234493 /* Set idxFlags flags for the ORDER BY clause */
234494 if( pInfo->nOrderBy==1 ){
234495 int iSort = pInfo->aOrderBy[0].iColumn;
234496 if( iSort==(pConfig->nCol+1) && bSeenMatch ){
234497 idxFlags |= FTS5_BI_ORDER_RANK0x0020;
234498 }else if( iSort==-1 ){
234499 idxFlags |= FTS5_BI_ORDER_ROWID0x0040;
234500 }
234501 if( BitFlagTest(idxFlags, FTS5_BI_ORDER_RANK|FTS5_BI_ORDER_ROWID)(((idxFlags) & (0x0020|0x0040))!=0) ){
234502 pInfo->orderByConsumed = 1;
234503 if( pInfo->aOrderBy[0].desc ){
234504 idxFlags |= FTS5_BI_ORDER_DESC0x0080;
234505 }
234506 }
234507 }
234508
234509 /* Calculate the estimated cost based on the flags set in idxFlags. */
234510 if( bSeenEq ){
234511 pInfo->estimatedCost = bSeenMatch ? 100.0 : 10.0;
234512 if( bSeenMatch==0 ) fts5SetUniqueFlag(pInfo);
234513 }else if( bSeenLt && bSeenGt ){
234514 pInfo->estimatedCost = bSeenMatch ? 500.0 : 250000.0;
234515 }else if( bSeenLt || bSeenGt ){
234516 pInfo->estimatedCost = bSeenMatch ? 750.0 : 750000.0;
234517 }else{
234518 pInfo->estimatedCost = bSeenMatch ? 1000.0 : 1000000.0;
234519 }
234520
234521 pInfo->idxNum = idxFlags;
234522 return SQLITE_OK0;
234523}
234524
234525static int fts5NewTransaction(Fts5FullTable *pTab){
234526 Fts5Cursor *pCsr;
234527 for(pCsr=pTab->pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
234528 if( pCsr->base.pVtab==(sqlite3_vtab*)pTab ) return SQLITE_OK0;
234529 }
234530 return sqlite3Fts5StorageReset(pTab->pStorage);
234531}
234532
234533/*
234534** Implementation of xOpen method.
234535*/
234536static int fts5OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
234537 Fts5FullTable *pTab = (Fts5FullTable*)pVTab;
234538 Fts5Config *pConfig = pTab->p.pConfig;
234539 Fts5Cursor *pCsr = 0; /* New cursor object */
234540 sqlite3_int64 nByte; /* Bytes of space to allocate */
234541 int rc; /* Return code */
234542
234543 rc = fts5NewTransaction(pTab);
234544 if( rc==SQLITE_OK0 ){
234545 nByte = sizeof(Fts5Cursor) + pConfig->nCol * sizeof(int);
234546 pCsr = (Fts5Cursor*)sqlite3_malloc64(nByte);
234547 if( pCsr ){
234548 Fts5Global *pGlobal = pTab->pGlobal;
234549 memset(pCsr, 0, (size_t)nByte);
234550 pCsr->aColumnSize = (int*)&pCsr[1];
234551 pCsr->pNext = pGlobal->pCsr;
234552 pGlobal->pCsr = pCsr;
234553 pCsr->iCsrId = ++pGlobal->iNextId;
234554 }else{
234555 rc = SQLITE_NOMEM7;
234556 }
234557 }
234558 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
234559 return rc;
234560}
234561
234562static int fts5StmtType(Fts5Cursor *pCsr){
234563 if( pCsr->ePlan==FTS5_PLAN_SCAN5 ){
234564 return (pCsr->bDesc) ? FTS5_STMT_SCAN_DESC1 : FTS5_STMT_SCAN_ASC0;
234565 }
234566 return FTS5_STMT_LOOKUP2;
234567}
234568
234569/*
234570** This function is called after the cursor passed as the only argument
234571** is moved to point at a different row. It clears all cached data
234572** specific to the previous row stored by the cursor object.
234573*/
234574static void fts5CsrNewrow(Fts5Cursor *pCsr){
234575 CsrFlagSet(pCsr,((pCsr)->csrflags |= (0x02 | 0x04 | 0x08 | 0x40))
234576 FTS5CSR_REQUIRE_CONTENT((pCsr)->csrflags |= (0x02 | 0x04 | 0x08 | 0x40))
234577 | FTS5CSR_REQUIRE_DOCSIZE((pCsr)->csrflags |= (0x02 | 0x04 | 0x08 | 0x40))
234578 | FTS5CSR_REQUIRE_INST((pCsr)->csrflags |= (0x02 | 0x04 | 0x08 | 0x40))
234579 | FTS5CSR_REQUIRE_POSLIST((pCsr)->csrflags |= (0x02 | 0x04 | 0x08 | 0x40))
234580 )((pCsr)->csrflags |= (0x02 | 0x04 | 0x08 | 0x40));
234581}
234582
234583static void fts5FreeCursorComponents(Fts5Cursor *pCsr){
234584 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
234585 Fts5Auxdata *pData;
234586 Fts5Auxdata *pNext;
234587
234588 sqlite3_free(pCsr->aInstIter);
234589 sqlite3_free(pCsr->aInst);
234590 if( pCsr->pStmt ){
234591 int eStmt = fts5StmtType(pCsr);
234592 sqlite3Fts5StorageStmtRelease(pTab->pStorage, eStmt, pCsr->pStmt);
234593 }
234594 if( pCsr->pSorter ){
234595 Fts5Sorter *pSorter = pCsr->pSorter;
234596 sqlite3_finalize(pSorter->pStmt);
234597 sqlite3_free(pSorter);
234598 }
234599
234600 if( pCsr->ePlan!=FTS5_PLAN_SOURCE2 ){
234601 sqlite3Fts5ExprFree(pCsr->pExpr);
234602 }
234603
234604 for(pData=pCsr->pAuxdata; pData; pData=pNext){
234605 pNext = pData->pNext;
234606 if( pData->xDelete ) pData->xDelete(pData->pPtr);
234607 sqlite3_free(pData);
234608 }
234609
234610 sqlite3_finalize(pCsr->pRankArgStmt);
234611 sqlite3_free(pCsr->apRankArg);
234612
234613 if( CsrFlagTest(pCsr, FTS5CSR_FREE_ZRANK)((pCsr)->csrflags & (0x10)) ){
234614 sqlite3_free(pCsr->zRank);
234615 sqlite3_free(pCsr->zRankArgs);
234616 }
234617
234618 sqlite3Fts5IndexCloseReader(pTab->p.pIndex);
234619 memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan - (u8*)pCsr));
234620}
234621
234622
234623/*
234624** Close the cursor. For additional information see the documentation
234625** on the xClose method of the virtual table interface.
234626*/
234627static int fts5CloseMethod(sqlite3_vtab_cursor *pCursor){
234628 if( pCursor ){
234629 Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
234630 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
234631 Fts5Cursor **pp;
234632
234633 fts5FreeCursorComponents(pCsr);
234634 /* Remove the cursor from the Fts5Global.pCsr list */
234635 for(pp=&pTab->pGlobal->pCsr; (*pp)!=pCsr; pp=&(*pp)->pNext);
234636 *pp = pCsr->pNext;
234637
234638 sqlite3_free(pCsr);
234639 }
234640 return SQLITE_OK0;
234641}
234642
234643static int fts5SorterNext(Fts5Cursor *pCsr){
234644 Fts5Sorter *pSorter = pCsr->pSorter;
234645 int rc;
234646
234647 rc = sqlite3_step(pSorter->pStmt);
234648 if( rc==SQLITE_DONE101 ){
234649 rc = SQLITE_OK0;
234650 CsrFlagSet(pCsr, FTS5CSR_EOF|FTS5CSR_REQUIRE_CONTENT)((pCsr)->csrflags |= (0x01|0x02));
234651 }else if( rc==SQLITE_ROW100 ){
234652 const u8 *a;
234653 const u8 *aBlob;
234654 int nBlob;
234655 int i;
234656 int iOff = 0;
234657 rc = SQLITE_OK0;
234658
234659 pSorter->iRowid = sqlite3_column_int64(pSorter->pStmt, 0);
234660 nBlob = sqlite3_column_bytes(pSorter->pStmt, 1);
234661 aBlob = a = sqlite3_column_blob(pSorter->pStmt, 1);
234662
234663 /* nBlob==0 in detail=none mode. */
234664 if( nBlob>0 ){
234665 for(i=0; i<(pSorter->nIdx-1); i++){
234666 int iVal;
234667 a += fts5GetVarint32(a, iVal)sqlite3Fts5GetVarint32(a,(u32*)&iVal);
234668 iOff += iVal;
234669 pSorter->aIdx[i] = iOff;
234670 }
234671 pSorter->aIdx[i] = &aBlob[nBlob] - a;
234672 pSorter->aPoslist = a;
234673 }
234674
234675 fts5CsrNewrow(pCsr);
234676 }
234677
234678 return rc;
234679}
234680
234681
234682/*
234683** Set the FTS5CSR_REQUIRE_RESEEK flag on all FTS5_PLAN_MATCH cursors
234684** open on table pTab.
234685*/
234686static void fts5TripCursors(Fts5FullTable *pTab){
234687 Fts5Cursor *pCsr;
234688 for(pCsr=pTab->pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
234689 if( pCsr->ePlan==FTS5_PLAN_MATCH1
234690 && pCsr->base.pVtab==(sqlite3_vtab*)pTab
234691 ){
234692 CsrFlagSet(pCsr, FTS5CSR_REQUIRE_RESEEK)((pCsr)->csrflags |= (0x20));
234693 }
234694 }
234695}
234696
234697/*
234698** If the REQUIRE_RESEEK flag is set on the cursor passed as the first
234699** argument, close and reopen all Fts5IndexIter iterators that the cursor
234700** is using. Then attempt to move the cursor to a rowid equal to or laster
234701** (in the cursors sort order - ASC or DESC) than the current rowid.
234702**
234703** If the new rowid is not equal to the old, set output parameter *pbSkip
234704** to 1 before returning. Otherwise, leave it unchanged.
234705**
234706** Return SQLITE_OK if successful or if no reseek was required, or an
234707** error code if an error occurred.
234708*/
234709static int fts5CursorReseek(Fts5Cursor *pCsr, int *pbSkip){
234710 int rc = SQLITE_OK0;
234711 assert( *pbSkip==0 )((void) (0));
234712 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_RESEEK)((pCsr)->csrflags & (0x20)) ){
234713 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
234714 int bDesc = pCsr->bDesc;
234715 i64 iRowid = sqlite3Fts5ExprRowid(pCsr->pExpr);
234716
234717 rc = sqlite3Fts5ExprFirst(pCsr->pExpr, pTab->p.pIndex, iRowid, bDesc);
234718 if( rc==SQLITE_OK0 && iRowid!=sqlite3Fts5ExprRowid(pCsr->pExpr) ){
234719 *pbSkip = 1;
234720 }
234721
234722 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_RESEEK)((pCsr)->csrflags &= ~(0x20));
234723 fts5CsrNewrow(pCsr);
234724 if( sqlite3Fts5ExprEof(pCsr->pExpr) ){
234725 CsrFlagSet(pCsr, FTS5CSR_EOF)((pCsr)->csrflags |= (0x01));
234726 *pbSkip = 1;
234727 }
234728 }
234729 return rc;
234730}
234731
234732
234733/*
234734** Advance the cursor to the next row in the table that matches the
234735** search criteria.
234736**
234737** Return SQLITE_OK if nothing goes wrong. SQLITE_OK is returned
234738** even if we reach end-of-file. The fts5EofMethod() will be called
234739** subsequently to determine whether or not an EOF was hit.
234740*/
234741static int fts5NextMethod(sqlite3_vtab_cursor *pCursor){
234742 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
234743 int rc;
234744
234745 assert( (pCsr->ePlan<3)==((void) (0))
234746 (pCsr->ePlan==FTS5_PLAN_MATCH || pCsr->ePlan==FTS5_PLAN_SOURCE)((void) (0))
234747 )((void) (0));
234748 assert( !CsrFlagTest(pCsr, FTS5CSR_EOF) )((void) (0));
234749
234750 if( pCsr->ePlan<3 ){
234751 int bSkip = 0;
234752 if( (rc = fts5CursorReseek(pCsr, &bSkip)) || bSkip ) return rc;
234753 rc = sqlite3Fts5ExprNext(pCsr->pExpr, pCsr->iLastRowid);
234754 CsrFlagSet(pCsr, sqlite3Fts5ExprEof(pCsr->pExpr))((pCsr)->csrflags |= (sqlite3Fts5ExprEof(pCsr->pExpr)));
234755 fts5CsrNewrow(pCsr);
234756 }else{
234757 switch( pCsr->ePlan ){
234758 case FTS5_PLAN_SPECIAL3: {
234759 CsrFlagSet(pCsr, FTS5CSR_EOF)((pCsr)->csrflags |= (0x01));
234760 rc = SQLITE_OK0;
234761 break;
234762 }
234763
234764 case FTS5_PLAN_SORTED_MATCH4: {
234765 rc = fts5SorterNext(pCsr);
234766 break;
234767 }
234768
234769 default: {
234770 Fts5Config *pConfig = ((Fts5Table*)pCursor->pVtab)->pConfig;
234771 pConfig->bLock++;
234772 rc = sqlite3_step(pCsr->pStmt);
234773 pConfig->bLock--;
234774 if( rc!=SQLITE_ROW100 ){
234775 CsrFlagSet(pCsr, FTS5CSR_EOF)((pCsr)->csrflags |= (0x01));
234776 rc = sqlite3_reset(pCsr->pStmt);
234777 if( rc!=SQLITE_OK0 ){
234778 pCursor->pVtab->zErrMsg = sqlite3_mprintf(
234779 "%s", sqlite3_errmsg(pConfig->db)
234780 );
234781 }
234782 }else{
234783 rc = SQLITE_OK0;
234784 }
234785 break;
234786 }
234787 }
234788 }
234789
234790 return rc;
234791}
234792
234793
234794static int fts5PrepareStatement(
234795 sqlite3_stmt **ppStmt,
234796 Fts5Config *pConfig,
234797 const char *zFmt,
234798 ...
234799){
234800 sqlite3_stmt *pRet = 0;
234801 int rc;
234802 char *zSql;
234803 va_list ap;
234804
234805 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
234806 zSql = sqlite3_vmprintf(zFmt, ap);
234807 if( zSql==0 ){
234808 rc = SQLITE_NOMEM7;
234809 }else{
234810 rc = sqlite3_prepare_v3(pConfig->db, zSql, -1,
234811 SQLITE_PREPARE_PERSISTENT0x01, &pRet, 0);
234812 if( rc!=SQLITE_OK0 ){
234813 *pConfig->pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(pConfig->db));
234814 }
234815 sqlite3_free(zSql);
234816 }
234817
234818 va_end(ap)__builtin_va_end(ap);
234819 *ppStmt = pRet;
234820 return rc;
234821}
234822
234823static int fts5CursorFirstSorted(
234824 Fts5FullTable *pTab,
234825 Fts5Cursor *pCsr,
234826 int bDesc
234827){
234828 Fts5Config *pConfig = pTab->p.pConfig;
234829 Fts5Sorter *pSorter;
234830 int nPhrase;
234831 sqlite3_int64 nByte;
234832 int rc;
234833 const char *zRank = pCsr->zRank;
234834 const char *zRankArgs = pCsr->zRankArgs;
234835
234836 nPhrase = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
234837 nByte = sizeof(Fts5Sorter) + sizeof(int) * (nPhrase-1);
234838 pSorter = (Fts5Sorter*)sqlite3_malloc64(nByte);
234839 if( pSorter==0 ) return SQLITE_NOMEM7;
234840 memset(pSorter, 0, (size_t)nByte);
234841 pSorter->nIdx = nPhrase;
234842
234843 /* TODO: It would be better to have some system for reusing statement
234844 ** handles here, rather than preparing a new one for each query. But that
234845 ** is not possible as SQLite reference counts the virtual table objects.
234846 ** And since the statement required here reads from this very virtual
234847 ** table, saving it creates a circular reference.
234848 **
234849 ** If SQLite a built-in statement cache, this wouldn't be a problem. */
234850 rc = fts5PrepareStatement(&pSorter->pStmt, pConfig,
234851 "SELECT rowid, rank FROM %Q.%Q ORDER BY %s(\"%w\"%s%s) %s",
234852 pConfig->zDb, pConfig->zName, zRank, pConfig->zName,
234853 (zRankArgs ? ", " : ""),
234854 (zRankArgs ? zRankArgs : ""),
234855 bDesc ? "DESC" : "ASC"
234856 );
234857
234858 pCsr->pSorter = pSorter;
234859 if( rc==SQLITE_OK0 ){
234860 assert( pTab->pSortCsr==0 )((void) (0));
234861 pTab->pSortCsr = pCsr;
234862 rc = fts5SorterNext(pCsr);
234863 pTab->pSortCsr = 0;
234864 }
234865
234866 if( rc!=SQLITE_OK0 ){
234867 sqlite3_finalize(pSorter->pStmt);
234868 sqlite3_free(pSorter);
234869 pCsr->pSorter = 0;
234870 }
234871
234872 return rc;
234873}
234874
234875static int fts5CursorFirst(Fts5FullTable *pTab, Fts5Cursor *pCsr, int bDesc){
234876 int rc;
234877 Fts5Expr *pExpr = pCsr->pExpr;
234878 rc = sqlite3Fts5ExprFirst(pExpr, pTab->p.pIndex, pCsr->iFirstRowid, bDesc);
234879 if( sqlite3Fts5ExprEof(pExpr) ){
234880 CsrFlagSet(pCsr, FTS5CSR_EOF)((pCsr)->csrflags |= (0x01));
234881 }
234882 fts5CsrNewrow(pCsr);
234883 return rc;
234884}
234885
234886/*
234887** Process a "special" query. A special query is identified as one with a
234888** MATCH expression that begins with a '*' character. The remainder of
234889** the text passed to the MATCH operator are used as the special query
234890** parameters.
234891*/
234892static int fts5SpecialMatch(
234893 Fts5FullTable *pTab,
234894 Fts5Cursor *pCsr,
234895 const char *zQuery
234896){
234897 int rc = SQLITE_OK0; /* Return code */
234898 const char *z = zQuery; /* Special query text */
234899 int n; /* Number of bytes in text at z */
234900
234901 while( z[0]==' ' ) z++;
234902 for(n=0; z[n] && z[n]!=' '; n++);
234903
234904 assert( pTab->p.base.zErrMsg==0 )((void) (0));
234905 pCsr->ePlan = FTS5_PLAN_SPECIAL3;
234906
234907 if( n==5 && 0==sqlite3_strnicmp("reads", z, n) ){
234908 pCsr->iSpecial = sqlite3Fts5IndexReads(pTab->p.pIndex);
234909 }
234910 else if( n==2 && 0==sqlite3_strnicmp("id", z, n) ){
234911 pCsr->iSpecial = pCsr->iCsrId;
234912 }
234913 else{
234914 /* An unrecognized directive. Return an error message. */
234915 pTab->p.base.zErrMsg = sqlite3_mprintf("unknown special query: %.*s", n, z);
234916 rc = SQLITE_ERROR1;
234917 }
234918
234919 return rc;
234920}
234921
234922/*
234923** Search for an auxiliary function named zName that can be used with table
234924** pTab. If one is found, return a pointer to the corresponding Fts5Auxiliary
234925** structure. Otherwise, if no such function exists, return NULL.
234926*/
234927static Fts5Auxiliary *fts5FindAuxiliary(Fts5FullTable *pTab, const char *zName){
234928 Fts5Auxiliary *pAux;
234929
234930 for(pAux=pTab->pGlobal->pAux; pAux; pAux=pAux->pNext){
234931 if( sqlite3_stricmp(zName, pAux->zFunc)==0 ) return pAux;
234932 }
234933
234934 /* No function of the specified name was found. Return 0. */
234935 return 0;
234936}
234937
234938
234939static int fts5FindRankFunction(Fts5Cursor *pCsr){
234940 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
234941 Fts5Config *pConfig = pTab->p.pConfig;
234942 int rc = SQLITE_OK0;
234943 Fts5Auxiliary *pAux = 0;
234944 const char *zRank = pCsr->zRank;
234945 const char *zRankArgs = pCsr->zRankArgs;
234946
234947 if( zRankArgs ){
234948 char *zSql = sqlite3Fts5Mprintf(&rc, "SELECT %s", zRankArgs);
234949 if( zSql ){
234950 sqlite3_stmt *pStmt = 0;
234951 rc = sqlite3_prepare_v3(pConfig->db, zSql, -1,
234952 SQLITE_PREPARE_PERSISTENT0x01, &pStmt, 0);
234953 sqlite3_free(zSql);
234954 assert( rc==SQLITE_OK || pCsr->pRankArgStmt==0 )((void) (0));
234955 if( rc==SQLITE_OK0 ){
234956 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
234957 sqlite3_int64 nByte;
234958 pCsr->nRankArg = sqlite3_column_count(pStmt);
234959 nByte = sizeof(sqlite3_value*)*pCsr->nRankArg;
234960 pCsr->apRankArg = (sqlite3_value**)sqlite3Fts5MallocZero(&rc, nByte);
234961 if( rc==SQLITE_OK0 ){
234962 int i;
234963 for(i=0; i<pCsr->nRankArg; i++){
234964 pCsr->apRankArg[i] = sqlite3_column_value(pStmt, i);
234965 }
234966 }
234967 pCsr->pRankArgStmt = pStmt;
234968 }else{
234969 rc = sqlite3_finalize(pStmt);
234970 assert( rc!=SQLITE_OK )((void) (0));
234971 }
234972 }
234973 }
234974 }
234975
234976 if( rc==SQLITE_OK0 ){
234977 pAux = fts5FindAuxiliary(pTab, zRank);
234978 if( pAux==0 ){
234979 assert( pTab->p.base.zErrMsg==0 )((void) (0));
234980 pTab->p.base.zErrMsg = sqlite3_mprintf("no such function: %s", zRank);
234981 rc = SQLITE_ERROR1;
234982 }
234983 }
234984
234985 pCsr->pRank = pAux;
234986 return rc;
234987}
234988
234989
234990static int fts5CursorParseRank(
234991 Fts5Config *pConfig,
234992 Fts5Cursor *pCsr,
234993 sqlite3_value *pRank
234994){
234995 int rc = SQLITE_OK0;
234996 if( pRank ){
234997 const char *z = (const char*)sqlite3_value_text(pRank);
234998 char *zRank = 0;
234999 char *zRankArgs = 0;
235000
235001 if( z==0 ){
235002 if( sqlite3_value_type(pRank)==SQLITE_NULL5 ) rc = SQLITE_ERROR1;
235003 }else{
235004 rc = sqlite3Fts5ConfigParseRank(z, &zRank, &zRankArgs);
235005 }
235006 if( rc==SQLITE_OK0 ){
235007 pCsr->zRank = zRank;
235008 pCsr->zRankArgs = zRankArgs;
235009 CsrFlagSet(pCsr, FTS5CSR_FREE_ZRANK)((pCsr)->csrflags |= (0x10));
235010 }else if( rc==SQLITE_ERROR1 ){
235011 pCsr->base.pVtab->zErrMsg = sqlite3_mprintf(
235012 "parse error in rank function: %s", z
235013 );
235014 }
235015 }else{
235016 if( pConfig->zRank ){
235017 pCsr->zRank = (char*)pConfig->zRank;
235018 pCsr->zRankArgs = (char*)pConfig->zRankArgs;
235019 }else{
235020 pCsr->zRank = (char*)FTS5_DEFAULT_RANK"bm25";
235021 pCsr->zRankArgs = 0;
235022 }
235023 }
235024 return rc;
235025}
235026
235027static i64 fts5GetRowidLimit(sqlite3_value *pVal, i64 iDefault){
235028 if( pVal ){
235029 int eType = sqlite3_value_numeric_type(pVal);
235030 if( eType==SQLITE_INTEGER1 ){
235031 return sqlite3_value_int64(pVal);
235032 }
235033 }
235034 return iDefault;
235035}
235036
235037/*
235038** This is the xFilter interface for the virtual table. See
235039** the virtual table xFilter method documentation for additional
235040** information.
235041**
235042** There are three possible query strategies:
235043**
235044** 1. Full-text search using a MATCH operator.
235045** 2. A by-rowid lookup.
235046** 3. A full-table scan.
235047*/
235048static int fts5FilterMethod(
235049 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
235050 int idxNum, /* Strategy index */
235051 const char *idxStr, /* Unused */
235052 int nVal, /* Number of elements in apVal */
235053 sqlite3_value **apVal /* Arguments for the indexing scheme */
235054){
235055 Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
235056 Fts5Config *pConfig = pTab->p.pConfig;
235057 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
235058 int rc = SQLITE_OK0; /* Error code */
235059 int bDesc; /* True if ORDER BY [rank|rowid] DESC */
235060 int bOrderByRank; /* True if ORDER BY rank */
235061 sqlite3_value *pRank = 0; /* rank MATCH ? expression (or NULL) */
235062 sqlite3_value *pRowidEq = 0; /* rowid = ? expression (or NULL) */
235063 sqlite3_value *pRowidLe = 0; /* rowid <= ? expression (or NULL) */
235064 sqlite3_value *pRowidGe = 0; /* rowid >= ? expression (or NULL) */
235065 int iCol; /* Column on LHS of MATCH operator */
235066 char **pzErrmsg = pConfig->pzErrmsg;
235067 int i;
235068 int iIdxStr = 0;
235069 Fts5Expr *pExpr = 0;
235070
235071 if( pConfig->bLock ){
235072 pTab->p.base.zErrMsg = sqlite3_mprintf(
235073 "recursively defined fts5 content table"
235074 );
235075 return SQLITE_ERROR1;
235076 }
235077
235078 if( pCsr->ePlan ){
235079 fts5FreeCursorComponents(pCsr);
235080 memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan-(u8*)pCsr));
235081 }
235082
235083 assert( pCsr->pStmt==0 )((void) (0));
235084 assert( pCsr->pExpr==0 )((void) (0));
235085 assert( pCsr->csrflags==0 )((void) (0));
235086 assert( pCsr->pRank==0 )((void) (0));
235087 assert( pCsr->zRank==0 )((void) (0));
235088 assert( pCsr->zRankArgs==0 )((void) (0));
235089 assert( pTab->pSortCsr==0 || nVal==0 )((void) (0));
235090
235091 assert( pzErrmsg==0 || pzErrmsg==&pTab->p.base.zErrMsg )((void) (0));
235092 pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
235093
235094 /* Decode the arguments passed through to this function. */
235095 for(i=0; i<nVal; i++){
235096 switch( idxStr[iIdxStr++] ){
235097 case 'r':
235098 pRank = apVal[i];
235099 break;
235100 case 'M': {
235101 const char *zText = (const char*)sqlite3_value_text(apVal[i]);
235102 if( zText==0 ) zText = "";
235103 iCol = 0;
235104 do{
235105 iCol = iCol*10 + (idxStr[iIdxStr]-'0');
235106 iIdxStr++;
235107 }while( idxStr[iIdxStr]>='0' && idxStr[iIdxStr]<='9' );
235108
235109 if( zText[0]=='*' ){
235110 /* The user has issued a query of the form "MATCH '*...'". This
235111 ** indicates that the MATCH expression is not a full text query,
235112 ** but a request for an internal parameter. */
235113 rc = fts5SpecialMatch(pTab, pCsr, &zText[1]);
235114 goto filter_out;
235115 }else{
235116 char **pzErr = &pTab->p.base.zErrMsg;
235117 rc = sqlite3Fts5ExprNew(pConfig, 0, iCol, zText, &pExpr, pzErr);
235118 if( rc==SQLITE_OK0 ){
235119 rc = sqlite3Fts5ExprAnd(&pCsr->pExpr, pExpr);
235120 pExpr = 0;
235121 }
235122 if( rc!=SQLITE_OK0 ) goto filter_out;
235123 }
235124
235125 break;
235126 }
235127 case 'L':
235128 case 'G': {
235129 int bGlob = (idxStr[iIdxStr-1]=='G');
235130 const char *zText = (const char*)sqlite3_value_text(apVal[i]);
235131 iCol = 0;
235132 do{
235133 iCol = iCol*10 + (idxStr[iIdxStr]-'0');
235134 iIdxStr++;
235135 }while( idxStr[iIdxStr]>='0' && idxStr[iIdxStr]<='9' );
235136 if( zText ){
235137 rc = sqlite3Fts5ExprPattern(pConfig, bGlob, iCol, zText, &pExpr);
235138 }
235139 if( rc==SQLITE_OK0 ){
235140 rc = sqlite3Fts5ExprAnd(&pCsr->pExpr, pExpr);
235141 pExpr = 0;
235142 }
235143 if( rc!=SQLITE_OK0 ) goto filter_out;
235144 break;
235145 }
235146 case '=':
235147 pRowidEq = apVal[i];
235148 break;
235149 case '<':
235150 pRowidLe = apVal[i];
235151 break;
235152 default: assert( idxStr[iIdxStr-1]=='>' )((void) (0));
235153 pRowidGe = apVal[i];
235154 break;
235155 }
235156 }
235157 bOrderByRank = ((idxNum & FTS5_BI_ORDER_RANK0x0020) ? 1 : 0);
235158 pCsr->bDesc = bDesc = ((idxNum & FTS5_BI_ORDER_DESC0x0080) ? 1 : 0);
235159
235160 /* Set the cursor upper and lower rowid limits. Only some strategies
235161 ** actually use them. This is ok, as the xBestIndex() method leaves the
235162 ** sqlite3_index_constraint.omit flag clear for range constraints
235163 ** on the rowid field. */
235164 if( pRowidEq ){
235165 pRowidLe = pRowidGe = pRowidEq;
235166 }
235167 if( bDesc ){
235168 pCsr->iFirstRowid = fts5GetRowidLimit(pRowidLe, LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)));
235169 pCsr->iLastRowid = fts5GetRowidLimit(pRowidGe, SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))));
235170 }else{
235171 pCsr->iLastRowid = fts5GetRowidLimit(pRowidLe, LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)));
235172 pCsr->iFirstRowid = fts5GetRowidLimit(pRowidGe, SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))));
235173 }
235174
235175 if( pTab->pSortCsr ){
235176 /* If pSortCsr is non-NULL, then this call is being made as part of
235177 ** processing for a "... MATCH <expr> ORDER BY rank" query (ePlan is
235178 ** set to FTS5_PLAN_SORTED_MATCH). pSortCsr is the cursor that will
235179 ** return results to the user for this query. The current cursor
235180 ** (pCursor) is used to execute the query issued by function
235181 ** fts5CursorFirstSorted() above. */
235182 assert( pRowidEq==0 && pRowidLe==0 && pRowidGe==0 && pRank==0 )((void) (0));
235183 assert( nVal==0 && bOrderByRank==0 && bDesc==0 )((void) (0));
235184 assert( pCsr->iLastRowid==LARGEST_INT64 )((void) (0));
235185 assert( pCsr->iFirstRowid==SMALLEST_INT64 )((void) (0));
235186 if( pTab->pSortCsr->bDesc ){
235187 pCsr->iLastRowid = pTab->pSortCsr->iFirstRowid;
235188 pCsr->iFirstRowid = pTab->pSortCsr->iLastRowid;
235189 }else{
235190 pCsr->iLastRowid = pTab->pSortCsr->iLastRowid;
235191 pCsr->iFirstRowid = pTab->pSortCsr->iFirstRowid;
235192 }
235193 pCsr->ePlan = FTS5_PLAN_SOURCE2;
235194 pCsr->pExpr = pTab->pSortCsr->pExpr;
235195 rc = fts5CursorFirst(pTab, pCsr, bDesc);
235196 }else if( pCsr->pExpr ){
235197 rc = fts5CursorParseRank(pConfig, pCsr, pRank);
235198 if( rc==SQLITE_OK0 ){
235199 if( bOrderByRank ){
235200 pCsr->ePlan = FTS5_PLAN_SORTED_MATCH4;
235201 rc = fts5CursorFirstSorted(pTab, pCsr, bDesc);
235202 }else{
235203 pCsr->ePlan = FTS5_PLAN_MATCH1;
235204 rc = fts5CursorFirst(pTab, pCsr, bDesc);
235205 }
235206 }
235207 }else if( pConfig->zContent==0 ){
235208 *pConfig->pzErrmsg = sqlite3_mprintf(
235209 "%s: table does not support scanning", pConfig->zName
235210 );
235211 rc = SQLITE_ERROR1;
235212 }else{
235213 /* This is either a full-table scan (ePlan==FTS5_PLAN_SCAN) or a lookup
235214 ** by rowid (ePlan==FTS5_PLAN_ROWID). */
235215 pCsr->ePlan = (pRowidEq ? FTS5_PLAN_ROWID6 : FTS5_PLAN_SCAN5);
235216 rc = sqlite3Fts5StorageStmt(
235217 pTab->pStorage, fts5StmtType(pCsr), &pCsr->pStmt, &pTab->p.base.zErrMsg
235218 );
235219 if( rc==SQLITE_OK0 ){
235220 if( pRowidEq!=0 ){
235221 assert( pCsr->ePlan==FTS5_PLAN_ROWID )((void) (0));
235222 sqlite3_bind_value(pCsr->pStmt, 1, pRowidEq);
235223 }else{
235224 sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iFirstRowid);
235225 sqlite3_bind_int64(pCsr->pStmt, 2, pCsr->iLastRowid);
235226 }
235227 rc = fts5NextMethod(pCursor);
235228 }
235229 }
235230
235231 filter_out:
235232 sqlite3Fts5ExprFree(pExpr);
235233 pConfig->pzErrmsg = pzErrmsg;
235234 return rc;
235235}
235236
235237/*
235238** This is the xEof method of the virtual table. SQLite calls this
235239** routine to find out if it has reached the end of a result set.
235240*/
235241static int fts5EofMethod(sqlite3_vtab_cursor *pCursor){
235242 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
235243 return (CsrFlagTest(pCsr, FTS5CSR_EOF)((pCsr)->csrflags & (0x01)) ? 1 : 0);
235244}
235245
235246/*
235247** Return the rowid that the cursor currently points to.
235248*/
235249static i64 fts5CursorRowid(Fts5Cursor *pCsr){
235250 assert( pCsr->ePlan==FTS5_PLAN_MATCH((void) (0))
235251 || pCsr->ePlan==FTS5_PLAN_SORTED_MATCH((void) (0))
235252 || pCsr->ePlan==FTS5_PLAN_SOURCE((void) (0))
235253 )((void) (0));
235254 if( pCsr->pSorter ){
235255 return pCsr->pSorter->iRowid;
235256 }else{
235257 return sqlite3Fts5ExprRowid(pCsr->pExpr);
235258 }
235259}
235260
235261/*
235262** This is the xRowid method. The SQLite core calls this routine to
235263** retrieve the rowid for the current row of the result set. fts5
235264** exposes %_content.rowid as the rowid for the virtual table. The
235265** rowid should be written to *pRowid.
235266*/
235267static int fts5RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
235268 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
235269 int ePlan = pCsr->ePlan;
235270
235271 assert( CsrFlagTest(pCsr, FTS5CSR_EOF)==0 )((void) (0));
235272 switch( ePlan ){
235273 case FTS5_PLAN_SPECIAL3:
235274 *pRowid = 0;
235275 break;
235276
235277 case FTS5_PLAN_SOURCE2:
235278 case FTS5_PLAN_MATCH1:
235279 case FTS5_PLAN_SORTED_MATCH4:
235280 *pRowid = fts5CursorRowid(pCsr);
235281 break;
235282
235283 default:
235284 *pRowid = sqlite3_column_int64(pCsr->pStmt, 0);
235285 break;
235286 }
235287
235288 return SQLITE_OK0;
235289}
235290
235291/*
235292** If the cursor requires seeking (bSeekRequired flag is set), seek it.
235293** Return SQLITE_OK if no error occurs, or an SQLite error code otherwise.
235294**
235295** If argument bErrormsg is true and an error occurs, an error message may
235296** be left in sqlite3_vtab.zErrMsg.
235297*/
235298static int fts5SeekCursor(Fts5Cursor *pCsr, int bErrormsg){
235299 int rc = SQLITE_OK0;
235300
235301 /* If the cursor does not yet have a statement handle, obtain one now. */
235302 if( pCsr->pStmt==0 ){
235303 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
235304 int eStmt = fts5StmtType(pCsr);
235305 rc = sqlite3Fts5StorageStmt(
235306 pTab->pStorage, eStmt, &pCsr->pStmt, (bErrormsg?&pTab->p.base.zErrMsg:0)
235307 );
235308 assert( rc!=SQLITE_OK || pTab->p.base.zErrMsg==0 )((void) (0));
235309 assert( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_CONTENT) )((void) (0));
235310 }
235311
235312 if( rc==SQLITE_OK0 && CsrFlagTest(pCsr, FTS5CSR_REQUIRE_CONTENT)((pCsr)->csrflags & (0x02)) ){
235313 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
235314 assert( pCsr->pExpr )((void) (0));
235315 sqlite3_reset(pCsr->pStmt);
235316 sqlite3_bind_int64(pCsr->pStmt, 1, fts5CursorRowid(pCsr));
235317 pTab->pConfig->bLock++;
235318 rc = sqlite3_step(pCsr->pStmt);
235319 pTab->pConfig->bLock--;
235320 if( rc==SQLITE_ROW100 ){
235321 rc = SQLITE_OK0;
235322 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_CONTENT)((pCsr)->csrflags &= ~(0x02));
235323 }else{
235324 rc = sqlite3_reset(pCsr->pStmt);
235325 if( rc==SQLITE_OK0 ){
235326 rc = FTS5_CORRUPT(11 | (1<<8));
235327 }else if( pTab->pConfig->pzErrmsg ){
235328 *pTab->pConfig->pzErrmsg = sqlite3_mprintf(
235329 "%s", sqlite3_errmsg(pTab->pConfig->db)
235330 );
235331 }
235332 }
235333 }
235334 return rc;
235335}
235336
235337static void fts5SetVtabError(Fts5FullTable *p, const char *zFormat, ...){
235338 va_list ap; /* ... printf arguments */
235339 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
235340 assert( p->p.base.zErrMsg==0 )((void) (0));
235341 p->p.base.zErrMsg = sqlite3_vmprintf(zFormat, ap);
235342 va_end(ap)__builtin_va_end(ap);
235343}
235344
235345/*
235346** This function is called to handle an FTS INSERT command. In other words,
235347** an INSERT statement of the form:
235348**
235349** INSERT INTO fts(fts) VALUES($pCmd)
235350** INSERT INTO fts(fts, rank) VALUES($pCmd, $pVal)
235351**
235352** Argument pVal is the value assigned to column "fts" by the INSERT
235353** statement. This function returns SQLITE_OK if successful, or an SQLite
235354** error code if an error occurs.
235355**
235356** The commands implemented by this function are documented in the "Special
235357** INSERT Directives" section of the documentation. It should be updated if
235358** more commands are added to this function.
235359*/
235360static int fts5SpecialInsert(
235361 Fts5FullTable *pTab, /* Fts5 table object */
235362 const char *zCmd, /* Text inserted into table-name column */
235363 sqlite3_value *pVal /* Value inserted into rank column */
235364){
235365 Fts5Config *pConfig = pTab->p.pConfig;
235366 int rc = SQLITE_OK0;
235367 int bError = 0;
235368
235369 if( 0==sqlite3_stricmp("delete-all", zCmd) ){
235370 if( pConfig->eContent==FTS5_CONTENT_NORMAL0 ){
235371 fts5SetVtabError(pTab,
235372 "'delete-all' may only be used with a "
235373 "contentless or external content fts5 table"
235374 );
235375 rc = SQLITE_ERROR1;
235376 }else{
235377 rc = sqlite3Fts5StorageDeleteAll(pTab->pStorage);
235378 }
235379 }else if( 0==sqlite3_stricmp("rebuild", zCmd) ){
235380 if( pConfig->eContent==FTS5_CONTENT_NONE1 ){
235381 fts5SetVtabError(pTab,
235382 "'rebuild' may not be used with a contentless fts5 table"
235383 );
235384 rc = SQLITE_ERROR1;
235385 }else{
235386 rc = sqlite3Fts5StorageRebuild(pTab->pStorage);
235387 }
235388 }else if( 0==sqlite3_stricmp("optimize", zCmd) ){
235389 rc = sqlite3Fts5StorageOptimize(pTab->pStorage);
235390 }else if( 0==sqlite3_stricmp("merge", zCmd) ){
235391 int nMerge = sqlite3_value_int(pVal);
235392 rc = sqlite3Fts5StorageMerge(pTab->pStorage, nMerge);
235393 }else if( 0==sqlite3_stricmp("integrity-check", zCmd) ){
235394 int iArg = sqlite3_value_int(pVal);
235395 rc = sqlite3Fts5StorageIntegrity(pTab->pStorage, iArg);
235396#ifdef SQLITE_DEBUG
235397 }else if( 0==sqlite3_stricmp("prefix-index", zCmd) ){
235398 pConfig->bPrefixIndex = sqlite3_value_int(pVal);
235399#endif
235400 }else{
235401 rc = sqlite3Fts5IndexLoadConfig(pTab->p.pIndex);
235402 if( rc==SQLITE_OK0 ){
235403 rc = sqlite3Fts5ConfigSetValue(pTab->p.pConfig, zCmd, pVal, &bError);
235404 }
235405 if( rc==SQLITE_OK0 ){
235406 if( bError ){
235407 rc = SQLITE_ERROR1;
235408 }else{
235409 rc = sqlite3Fts5StorageConfigValue(pTab->pStorage, zCmd, pVal, 0);
235410 }
235411 }
235412 }
235413 return rc;
235414}
235415
235416static int fts5SpecialDelete(
235417 Fts5FullTable *pTab,
235418 sqlite3_value **apVal
235419){
235420 int rc = SQLITE_OK0;
235421 int eType1 = sqlite3_value_type(apVal[1]);
235422 if( eType1==SQLITE_INTEGER1 ){
235423 sqlite3_int64 iDel = sqlite3_value_int64(apVal[1]);
235424 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iDel, &apVal[2]);
235425 }
235426 return rc;
235427}
235428
235429static void fts5StorageInsert(
235430 int *pRc,
235431 Fts5FullTable *pTab,
235432 sqlite3_value **apVal,
235433 i64 *piRowid
235434){
235435 int rc = *pRc;
235436 if( rc==SQLITE_OK0 ){
235437 rc = sqlite3Fts5StorageContentInsert(pTab->pStorage, apVal, piRowid);
235438 }
235439 if( rc==SQLITE_OK0 ){
235440 rc = sqlite3Fts5StorageIndexInsert(pTab->pStorage, apVal, *piRowid);
235441 }
235442 *pRc = rc;
235443}
235444
235445/*
235446** This function is the implementation of the xUpdate callback used by
235447** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
235448** inserted, updated or deleted.
235449**
235450** A delete specifies a single argument - the rowid of the row to remove.
235451**
235452** Update and insert operations pass:
235453**
235454** 1. The "old" rowid, or NULL.
235455** 2. The "new" rowid.
235456** 3. Values for each of the nCol matchable columns.
235457** 4. Values for the two hidden columns (<tablename> and "rank").
235458*/
235459static int fts5UpdateMethod(
235460 sqlite3_vtab *pVtab, /* Virtual table handle */
235461 int nArg, /* Size of argument array */
235462 sqlite3_value **apVal, /* Array of arguments */
235463 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
235464){
235465 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
235466 Fts5Config *pConfig = pTab->p.pConfig;
235467 int eType0; /* value_type() of apVal[0] */
235468 int rc = SQLITE_OK0; /* Return code */
235469
235470 /* A transaction must be open when this is called. */
235471 assert( pTab->ts.eState==1 )((void) (0));
235472
235473 assert( pVtab->zErrMsg==0 )((void) (0));
235474 assert( nArg==1 || nArg==(2+pConfig->nCol+2) )((void) (0));
235475 assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER((void) (0))
235476 || sqlite3_value_type(apVal[0])==SQLITE_NULL((void) (0))
235477 )((void) (0));
235478 assert( pTab->p.pConfig->pzErrmsg==0 )((void) (0));
235479 pTab->p.pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
235480
235481 /* Put any active cursors into REQUIRE_SEEK state. */
235482 fts5TripCursors(pTab);
235483
235484 eType0 = sqlite3_value_type(apVal[0]);
235485 if( eType0==SQLITE_NULL5
235486 && sqlite3_value_type(apVal[2+pConfig->nCol])!=SQLITE_NULL5
235487 ){
235488 /* A "special" INSERT op. These are handled separately. */
235489 const char *z = (const char*)sqlite3_value_text(apVal[2+pConfig->nCol]);
235490 if( pConfig->eContent!=FTS5_CONTENT_NORMAL0
235491 && 0==sqlite3_stricmp("delete", z)
235492 ){
235493 rc = fts5SpecialDelete(pTab, apVal);
235494 }else{
235495 rc = fts5SpecialInsert(pTab, z, apVal[2 + pConfig->nCol + 1]);
235496 }
235497 }else{
235498 /* A regular INSERT, UPDATE or DELETE statement. The trick here is that
235499 ** any conflict on the rowid value must be detected before any
235500 ** modifications are made to the database file. There are 4 cases:
235501 **
235502 ** 1) DELETE
235503 ** 2) UPDATE (rowid not modified)
235504 ** 3) UPDATE (rowid modified)
235505 ** 4) INSERT
235506 **
235507 ** Cases 3 and 4 may violate the rowid constraint.
235508 */
235509 int eConflict = SQLITE_ABORT4;
235510 if( pConfig->eContent==FTS5_CONTENT_NORMAL0 ){
235511 eConflict = sqlite3_vtab_on_conflict(pConfig->db);
235512 }
235513
235514 assert( eType0==SQLITE_INTEGER || eType0==SQLITE_NULL )((void) (0));
235515 assert( nArg!=1 || eType0==SQLITE_INTEGER )((void) (0));
235516
235517 /* Filter out attempts to run UPDATE or DELETE on contentless tables.
235518 ** This is not suported. */
235519 if( eType0==SQLITE_INTEGER1 && fts5IsContentless(pTab) ){
235520 pTab->p.base.zErrMsg = sqlite3_mprintf(
235521 "cannot %s contentless fts5 table: %s",
235522 (nArg>1 ? "UPDATE" : "DELETE from"), pConfig->zName
235523 );
235524 rc = SQLITE_ERROR1;
235525 }
235526
235527 /* DELETE */
235528 else if( nArg==1 ){
235529 i64 iDel = sqlite3_value_int64(apVal[0]); /* Rowid to delete */
235530 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iDel, 0);
235531 }
235532
235533 /* INSERT or UPDATE */
235534 else{
235535 int eType1 = sqlite3_value_numeric_type(apVal[1]);
235536
235537 if( eType1!=SQLITE_INTEGER1 && eType1!=SQLITE_NULL5 ){
235538 rc = SQLITE_MISMATCH20;
235539 }
235540
235541 else if( eType0!=SQLITE_INTEGER1 ){
235542 /* If this is a REPLACE, first remove the current entry (if any) */
235543 if( eConflict==SQLITE_REPLACE5 && eType1==SQLITE_INTEGER1 ){
235544 i64 iNew = sqlite3_value_int64(apVal[1]); /* Rowid to delete */
235545 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iNew, 0);
235546 }
235547 fts5StorageInsert(&rc, pTab, apVal, pRowid);
235548 }
235549
235550 /* UPDATE */
235551 else{
235552 i64 iOld = sqlite3_value_int64(apVal[0]); /* Old rowid */
235553 i64 iNew = sqlite3_value_int64(apVal[1]); /* New rowid */
235554 if( eType1==SQLITE_INTEGER1 && iOld!=iNew ){
235555 if( eConflict==SQLITE_REPLACE5 ){
235556 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0);
235557 if( rc==SQLITE_OK0 ){
235558 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iNew, 0);
235559 }
235560 fts5StorageInsert(&rc, pTab, apVal, pRowid);
235561 }else{
235562 rc = sqlite3Fts5StorageContentInsert(pTab->pStorage, apVal, pRowid);
235563 if( rc==SQLITE_OK0 ){
235564 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0);
235565 }
235566 if( rc==SQLITE_OK0 ){
235567 rc = sqlite3Fts5StorageIndexInsert(pTab->pStorage, apVal,*pRowid);
235568 }
235569 }
235570 }else{
235571 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0);
235572 fts5StorageInsert(&rc, pTab, apVal, pRowid);
235573 }
235574 }
235575 }
235576 }
235577
235578 pTab->p.pConfig->pzErrmsg = 0;
235579 return rc;
235580}
235581
235582/*
235583** Implementation of xSync() method.
235584*/
235585static int fts5SyncMethod(sqlite3_vtab *pVtab){
235586 int rc;
235587 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
235588 fts5CheckTransactionState(pTab, FTS5_SYNC, 0);
235589 pTab->p.pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
235590 fts5TripCursors(pTab);
235591 rc = sqlite3Fts5StorageSync(pTab->pStorage);
235592 pTab->p.pConfig->pzErrmsg = 0;
235593 return rc;
235594}
235595
235596/*
235597** Implementation of xBegin() method.
235598*/
235599static int fts5BeginMethod(sqlite3_vtab *pVtab){
235600 fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_BEGIN, 0);
235601 fts5NewTransaction((Fts5FullTable*)pVtab);
235602 return SQLITE_OK0;
235603}
235604
235605/*
235606** Implementation of xCommit() method. This is a no-op. The contents of
235607** the pending-terms hash-table have already been flushed into the database
235608** by fts5SyncMethod().
235609*/
235610static int fts5CommitMethod(sqlite3_vtab *pVtab){
235611 UNUSED_PARAM(pVtab)(void)(pVtab); /* Call below is a no-op for NDEBUG builds */
235612 fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_COMMIT, 0);
235613 return SQLITE_OK0;
235614}
235615
235616/*
235617** Implementation of xRollback(). Discard the contents of the pending-terms
235618** hash-table. Any changes made to the database are reverted by SQLite.
235619*/
235620static int fts5RollbackMethod(sqlite3_vtab *pVtab){
235621 int rc;
235622 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
235623 fts5CheckTransactionState(pTab, FTS5_ROLLBACK, 0);
235624 rc = sqlite3Fts5StorageRollback(pTab->pStorage);
235625 return rc;
235626}
235627
235628static int fts5CsrPoslist(Fts5Cursor*, int, const u8**, int*);
235629
235630static void *fts5ApiUserData(Fts5Context *pCtx){
235631 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
235632 return pCsr->pAux->pUserData;
235633}
235634
235635static int fts5ApiColumnCount(Fts5Context *pCtx){
235636 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
235637 return ((Fts5Table*)(pCsr->base.pVtab))->pConfig->nCol;
235638}
235639
235640static int fts5ApiColumnTotalSize(
235641 Fts5Context *pCtx,
235642 int iCol,
235643 sqlite3_int64 *pnToken
235644){
235645 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
235646 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
235647 return sqlite3Fts5StorageSize(pTab->pStorage, iCol, pnToken);
235648}
235649
235650static int fts5ApiRowCount(Fts5Context *pCtx, i64 *pnRow){
235651 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
235652 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
235653 return sqlite3Fts5StorageRowCount(pTab->pStorage, pnRow);
235654}
235655
235656static int fts5ApiTokenize(
235657 Fts5Context *pCtx,
235658 const char *pText, int nText,
235659 void *pUserData,
235660 int (*xToken)(void*, int, const char*, int, int, int)
235661){
235662 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
235663 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
235664 return sqlite3Fts5Tokenize(
235665 pTab->pConfig, FTS5_TOKENIZE_AUX0x0008, pText, nText, pUserData, xToken
235666 );
235667}
235668
235669static int fts5ApiPhraseCount(Fts5Context *pCtx){
235670 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
235671 return sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
235672}
235673
235674static int fts5ApiPhraseSize(Fts5Context *pCtx, int iPhrase){
235675 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
235676 return sqlite3Fts5ExprPhraseSize(pCsr->pExpr, iPhrase);
235677}
235678
235679static int fts5ApiColumnText(
235680 Fts5Context *pCtx,
235681 int iCol,
235682 const char **pz,
235683 int *pn
235684){
235685 int rc = SQLITE_OK0;
235686 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
235687 if( fts5IsContentless((Fts5FullTable*)(pCsr->base.pVtab))
235688 || pCsr->ePlan==FTS5_PLAN_SPECIAL3
235689 ){
235690 *pz = 0;
235691 *pn = 0;
235692 }else{
235693 rc = fts5SeekCursor(pCsr, 0);
235694 if( rc==SQLITE_OK0 ){
235695 *pz = (const char*)sqlite3_column_text(pCsr->pStmt, iCol+1);
235696 *pn = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
235697 }
235698 }
235699 return rc;
235700}
235701
235702static int fts5CsrPoslist(
235703 Fts5Cursor *pCsr,
235704 int iPhrase,
235705 const u8 **pa,
235706 int *pn
235707){
235708 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
235709 int rc = SQLITE_OK0;
235710 int bLive = (pCsr->pSorter==0);
235711
235712 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_POSLIST)((pCsr)->csrflags & (0x40)) ){
235713
235714 if( pConfig->eDetail!=FTS5_DETAIL_FULL0 ){
235715 Fts5PoslistPopulator *aPopulator;
235716 int i;
235717 aPopulator = sqlite3Fts5ExprClearPoslists(pCsr->pExpr, bLive);
235718 if( aPopulator==0 ) rc = SQLITE_NOMEM7;
235719 for(i=0; i<pConfig->nCol && rc==SQLITE_OK0; i++){
235720 int n; const char *z;
235721 rc = fts5ApiColumnText((Fts5Context*)pCsr, i, &z, &n);
235722 if( rc==SQLITE_OK0 ){
235723 rc = sqlite3Fts5ExprPopulatePoslists(
235724 pConfig, pCsr->pExpr, aPopulator, i, z, n
235725 );
235726 }
235727 }
235728 sqlite3_free(aPopulator);
235729
235730 if( pCsr->pSorter ){
235731 sqlite3Fts5ExprCheckPoslists(pCsr->pExpr, pCsr->pSorter->iRowid);
235732 }
235733 }
235734 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_POSLIST)((pCsr)->csrflags &= ~(0x40));
235735 }
235736
235737 if( pCsr->pSorter && pConfig->eDetail==FTS5_DETAIL_FULL0 ){
235738 Fts5Sorter *pSorter = pCsr->pSorter;
235739 int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
235740 *pn = pSorter->aIdx[iPhrase] - i1;
235741 *pa = &pSorter->aPoslist[i1];
235742 }else{
235743 *pn = sqlite3Fts5ExprPoslist(pCsr->pExpr, iPhrase, pa);
235744 }
235745
235746 return rc;
235747}
235748
235749/*
235750** Ensure that the Fts5Cursor.nInstCount and aInst[] variables are populated
235751** correctly for the current view. Return SQLITE_OK if successful, or an
235752** SQLite error code otherwise.
235753*/
235754static int fts5CacheInstArray(Fts5Cursor *pCsr){
235755 int rc = SQLITE_OK0;
235756 Fts5PoslistReader *aIter; /* One iterator for each phrase */
235757 int nIter; /* Number of iterators/phrases */
235758 int nCol = ((Fts5Table*)pCsr->base.pVtab)->pConfig->nCol;
235759
235760 nIter = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
235761 if( pCsr->aInstIter==0 ){
235762 sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * nIter;
235763 pCsr->aInstIter = (Fts5PoslistReader*)sqlite3Fts5MallocZero(&rc, nByte);
235764 }
235765 aIter = pCsr->aInstIter;
235766
235767 if( aIter ){
235768 int nInst = 0; /* Number instances seen so far */
235769 int i;
235770
235771 /* Initialize all iterators */
235772 for(i=0; i<nIter && rc==SQLITE_OK0; i++){
235773 const u8 *a;
235774 int n;
235775 rc = fts5CsrPoslist(pCsr, i, &a, &n);
235776 if( rc==SQLITE_OK0 ){
235777 sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]);
235778 }
235779 }
235780
235781 if( rc==SQLITE_OK0 ){
235782 while( 1 ){
235783 int *aInst;
235784 int iBest = -1;
235785 for(i=0; i<nIter; i++){
235786 if( (aIter[i].bEof==0)
235787 && (iBest<0 || aIter[i].iPos<aIter[iBest].iPos)
235788 ){
235789 iBest = i;
235790 }
235791 }
235792 if( iBest<0 ) break;
235793
235794 nInst++;
235795 if( nInst>=pCsr->nInstAlloc ){
235796 int nNewSize = pCsr->nInstAlloc ? pCsr->nInstAlloc*2 : 32;
235797 aInst = (int*)sqlite3_realloc64(
235798 pCsr->aInst, nNewSize*sizeof(int)*3
235799 );
235800 if( aInst ){
235801 pCsr->aInst = aInst;
235802 pCsr->nInstAlloc = nNewSize;
235803 }else{
235804 nInst--;
235805 rc = SQLITE_NOMEM7;
235806 break;
235807 }
235808 }
235809
235810 aInst = &pCsr->aInst[3 * (nInst-1)];
235811 aInst[0] = iBest;
235812 aInst[1] = FTS5_POS2COLUMN(aIter[iBest].iPos)(int)(aIter[iBest].iPos >> 32);
235813 aInst[2] = FTS5_POS2OFFSET(aIter[iBest].iPos)(int)(aIter[iBest].iPos & 0x7FFFFFFF);
235814 if( aInst[1]<0 || aInst[1]>=nCol ){
235815 rc = FTS5_CORRUPT(11 | (1<<8));
235816 break;
235817 }
235818 sqlite3Fts5PoslistReaderNext(&aIter[iBest]);
235819 }
235820 }
235821
235822 pCsr->nInstCount = nInst;
235823 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_INST)((pCsr)->csrflags &= ~(0x08));
235824 }
235825 return rc;
235826}
235827
235828static int fts5ApiInstCount(Fts5Context *pCtx, int *pnInst){
235829 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
235830 int rc = SQLITE_OK0;
235831 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_INST)((pCsr)->csrflags & (0x08))==0
235832 || SQLITE_OK0==(rc = fts5CacheInstArray(pCsr)) ){
235833 *pnInst = pCsr->nInstCount;
235834 }
235835 return rc;
235836}
235837
235838static int fts5ApiInst(
235839 Fts5Context *pCtx,
235840 int iIdx,
235841 int *piPhrase,
235842 int *piCol,
235843 int *piOff
235844){
235845 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
235846 int rc = SQLITE_OK0;
235847 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_INST)((pCsr)->csrflags & (0x08))==0
235848 || SQLITE_OK0==(rc = fts5CacheInstArray(pCsr))
235849 ){
235850 if( iIdx<0 || iIdx>=pCsr->nInstCount ){
235851 rc = SQLITE_RANGE25;
235852#if 0
235853 }else if( fts5IsOffsetless((Fts5Table*)pCsr->base.pVtab) ){
235854 *piPhrase = pCsr->aInst[iIdx*3];
235855 *piCol = pCsr->aInst[iIdx*3 + 2];
235856 *piOff = -1;
235857#endif
235858 }else{
235859 *piPhrase = pCsr->aInst[iIdx*3];
235860 *piCol = pCsr->aInst[iIdx*3 + 1];
235861 *piOff = pCsr->aInst[iIdx*3 + 2];
235862 }
235863 }
235864 return rc;
235865}
235866
235867static sqlite3_int64 fts5ApiRowid(Fts5Context *pCtx){
235868 return fts5CursorRowid((Fts5Cursor*)pCtx);
235869}
235870
235871static int fts5ColumnSizeCb(
235872 void *pContext, /* Pointer to int */
235873 int tflags,
235874 const char *pUnused, /* Buffer containing token */
235875 int nUnused, /* Size of token in bytes */
235876 int iUnused1, /* Start offset of token */
235877 int iUnused2 /* End offset of token */
235878){
235879 int *pCnt = (int*)pContext;
235880 UNUSED_PARAM2(pUnused, nUnused)(void)(pUnused), (void)(nUnused);
235881 UNUSED_PARAM2(iUnused1, iUnused2)(void)(iUnused1), (void)(iUnused2);
235882 if( (tflags & FTS5_TOKEN_COLOCATED0x0001)==0 ){
235883 (*pCnt)++;
235884 }
235885 return SQLITE_OK0;
235886}
235887
235888static int fts5ApiColumnSize(Fts5Context *pCtx, int iCol, int *pnToken){
235889 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
235890 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
235891 Fts5Config *pConfig = pTab->p.pConfig;
235892 int rc = SQLITE_OK0;
235893
235894 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_DOCSIZE)((pCsr)->csrflags & (0x04)) ){
235895 if( pConfig->bColumnsize ){
235896 i64 iRowid = fts5CursorRowid(pCsr);
235897 rc = sqlite3Fts5StorageDocsize(pTab->pStorage, iRowid, pCsr->aColumnSize);
235898 }else if( pConfig->zContent==0 ){
235899 int i;
235900 for(i=0; i<pConfig->nCol; i++){
235901 if( pConfig->abUnindexed[i]==0 ){
235902 pCsr->aColumnSize[i] = -1;
235903 }
235904 }
235905 }else{
235906 int i;
235907 for(i=0; rc==SQLITE_OK0 && i<pConfig->nCol; i++){
235908 if( pConfig->abUnindexed[i]==0 ){
235909 const char *z; int n;
235910 void *p = (void*)(&pCsr->aColumnSize[i]);
235911 pCsr->aColumnSize[i] = 0;
235912 rc = fts5ApiColumnText(pCtx, i, &z, &n);
235913 if( rc==SQLITE_OK0 ){
235914 rc = sqlite3Fts5Tokenize(
235915 pConfig, FTS5_TOKENIZE_AUX0x0008, z, n, p, fts5ColumnSizeCb
235916 );
235917 }
235918 }
235919 }
235920 }
235921 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_DOCSIZE)((pCsr)->csrflags &= ~(0x04));
235922 }
235923 if( iCol<0 ){
235924 int i;
235925 *pnToken = 0;
235926 for(i=0; i<pConfig->nCol; i++){
235927 *pnToken += pCsr->aColumnSize[i];
235928 }
235929 }else if( iCol<pConfig->nCol ){
235930 *pnToken = pCsr->aColumnSize[iCol];
235931 }else{
235932 *pnToken = 0;
235933 rc = SQLITE_RANGE25;
235934 }
235935 return rc;
235936}
235937
235938/*
235939** Implementation of the xSetAuxdata() method.
235940*/
235941static int fts5ApiSetAuxdata(
235942 Fts5Context *pCtx, /* Fts5 context */
235943 void *pPtr, /* Pointer to save as auxdata */
235944 void(*xDelete)(void*) /* Destructor for pPtr (or NULL) */
235945){
235946 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
235947 Fts5Auxdata *pData;
235948
235949 /* Search through the cursors list of Fts5Auxdata objects for one that
235950 ** corresponds to the currently executing auxiliary function. */
235951 for(pData=pCsr->pAuxdata; pData; pData=pData->pNext){
235952 if( pData->pAux==pCsr->pAux ) break;
235953 }
235954
235955 if( pData ){
235956 if( pData->xDelete ){
235957 pData->xDelete(pData->pPtr);
235958 }
235959 }else{
235960 int rc = SQLITE_OK0;
235961 pData = (Fts5Auxdata*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Auxdata));
235962 if( pData==0 ){
235963 if( xDelete ) xDelete(pPtr);
235964 return rc;
235965 }
235966 pData->pAux = pCsr->pAux;
235967 pData->pNext = pCsr->pAuxdata;
235968 pCsr->pAuxdata = pData;
235969 }
235970
235971 pData->xDelete = xDelete;
235972 pData->pPtr = pPtr;
235973 return SQLITE_OK0;
235974}
235975
235976static void *fts5ApiGetAuxdata(Fts5Context *pCtx, int bClear){
235977 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
235978 Fts5Auxdata *pData;
235979 void *pRet = 0;
235980
235981 for(pData=pCsr->pAuxdata; pData; pData=pData->pNext){
235982 if( pData->pAux==pCsr->pAux ) break;
235983 }
235984
235985 if( pData ){
235986 pRet = pData->pPtr;
235987 if( bClear ){
235988 pData->pPtr = 0;
235989 pData->xDelete = 0;
235990 }
235991 }
235992
235993 return pRet;
235994}
235995
235996static void fts5ApiPhraseNext(
235997 Fts5Context *pUnused,
235998 Fts5PhraseIter *pIter,
235999 int *piCol, int *piOff
236000){
236001 UNUSED_PARAM(pUnused)(void)(pUnused);
236002 if( pIter->a>=pIter->b ){
236003 *piCol = -1;
236004 *piOff = -1;
236005 }else{
236006 int iVal;
236007 pIter->a += fts5GetVarint32(pIter->a, iVal)sqlite3Fts5GetVarint32(pIter->a,(u32*)&iVal);
236008 if( iVal==1 ){
236009 pIter->a += fts5GetVarint32(pIter->a, iVal)sqlite3Fts5GetVarint32(pIter->a,(u32*)&iVal);
236010 *piCol = iVal;
236011 *piOff = 0;
236012 pIter->a += fts5GetVarint32(pIter->a, iVal)sqlite3Fts5GetVarint32(pIter->a,(u32*)&iVal);
236013 }
236014 *piOff += (iVal-2);
236015 }
236016}
236017
236018static int fts5ApiPhraseFirst(
236019 Fts5Context *pCtx,
236020 int iPhrase,
236021 Fts5PhraseIter *pIter,
236022 int *piCol, int *piOff
236023){
236024 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
236025 int n;
236026 int rc = fts5CsrPoslist(pCsr, iPhrase, &pIter->a, &n);
236027 if( rc==SQLITE_OK0 ){
236028 assert( pIter->a || n==0 )((void) (0));
236029 pIter->b = (pIter->a ? &pIter->a[n] : 0);
236030 *piCol = 0;
236031 *piOff = 0;
236032 fts5ApiPhraseNext(pCtx, pIter, piCol, piOff);
236033 }
236034 return rc;
236035}
236036
236037static void fts5ApiPhraseNextColumn(
236038 Fts5Context *pCtx,
236039 Fts5PhraseIter *pIter,
236040 int *piCol
236041){
236042 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
236043 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
236044
236045 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS2 ){
236046 if( pIter->a>=pIter->b ){
236047 *piCol = -1;
236048 }else{
236049 int iIncr;
236050 pIter->a += fts5GetVarint32(&pIter->a[0], iIncr)sqlite3Fts5GetVarint32(&pIter->a[0],(u32*)&iIncr);
236051 *piCol += (iIncr-2);
236052 }
236053 }else{
236054 while( 1 ){
236055 int dummy;
236056 if( pIter->a>=pIter->b ){
236057 *piCol = -1;
236058 return;
236059 }
236060 if( pIter->a[0]==0x01 ) break;
236061 pIter->a += fts5GetVarint32(pIter->a, dummy)sqlite3Fts5GetVarint32(pIter->a,(u32*)&dummy);
236062 }
236063 pIter->a += 1 + fts5GetVarint32(&pIter->a[1], *piCol)sqlite3Fts5GetVarint32(&pIter->a[1],(u32*)&*piCol);
236064 }
236065}
236066
236067static int fts5ApiPhraseFirstColumn(
236068 Fts5Context *pCtx,
236069 int iPhrase,
236070 Fts5PhraseIter *pIter,
236071 int *piCol
236072){
236073 int rc = SQLITE_OK0;
236074 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
236075 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
236076
236077 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS2 ){
236078 Fts5Sorter *pSorter = pCsr->pSorter;
236079 int n;
236080 if( pSorter ){
236081 int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
236082 n = pSorter->aIdx[iPhrase] - i1;
236083 pIter->a = &pSorter->aPoslist[i1];
236084 }else{
236085 rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, iPhrase, &pIter->a, &n);
236086 }
236087 if( rc==SQLITE_OK0 ){
236088 assert( pIter->a || n==0 )((void) (0));
236089 pIter->b = (pIter->a ? &pIter->a[n] : 0);
236090 *piCol = 0;
236091 fts5ApiPhraseNextColumn(pCtx, pIter, piCol);
236092 }
236093 }else{
236094 int n;
236095 rc = fts5CsrPoslist(pCsr, iPhrase, &pIter->a, &n);
236096 if( rc==SQLITE_OK0 ){
236097 assert( pIter->a || n==0 )((void) (0));
236098 pIter->b = (pIter->a ? &pIter->a[n] : 0);
236099 if( n<=0 ){
236100 *piCol = -1;
236101 }else if( pIter->a[0]==0x01 ){
236102 pIter->a += 1 + fts5GetVarint32(&pIter->a[1], *piCol)sqlite3Fts5GetVarint32(&pIter->a[1],(u32*)&*piCol);
236103 }else{
236104 *piCol = 0;
236105 }
236106 }
236107 }
236108
236109 return rc;
236110}
236111
236112
236113static int fts5ApiQueryPhrase(Fts5Context*, int, void*,
236114 int(*)(const Fts5ExtensionApi*, Fts5Context*, void*)
236115);
236116
236117static const Fts5ExtensionApi sFts5Api = {
236118 2, /* iVersion */
236119 fts5ApiUserData,
236120 fts5ApiColumnCount,
236121 fts5ApiRowCount,
236122 fts5ApiColumnTotalSize,
236123 fts5ApiTokenize,
236124 fts5ApiPhraseCount,
236125 fts5ApiPhraseSize,
236126 fts5ApiInstCount,
236127 fts5ApiInst,
236128 fts5ApiRowid,
236129 fts5ApiColumnText,
236130 fts5ApiColumnSize,
236131 fts5ApiQueryPhrase,
236132 fts5ApiSetAuxdata,
236133 fts5ApiGetAuxdata,
236134 fts5ApiPhraseFirst,
236135 fts5ApiPhraseNext,
236136 fts5ApiPhraseFirstColumn,
236137 fts5ApiPhraseNextColumn,
236138};
236139
236140/*
236141** Implementation of API function xQueryPhrase().
236142*/
236143static int fts5ApiQueryPhrase(
236144 Fts5Context *pCtx,
236145 int iPhrase,
236146 void *pUserData,
236147 int(*xCallback)(const Fts5ExtensionApi*, Fts5Context*, void*)
236148){
236149 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
236150 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
236151 int rc;
236152 Fts5Cursor *pNew = 0;
236153
236154 rc = fts5OpenMethod(pCsr->base.pVtab, (sqlite3_vtab_cursor**)&pNew);
236155 if( rc==SQLITE_OK0 ){
236156 pNew->ePlan = FTS5_PLAN_MATCH1;
236157 pNew->iFirstRowid = SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32)));
236158 pNew->iLastRowid = LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
236159 pNew->base.pVtab = (sqlite3_vtab*)pTab;
236160 rc = sqlite3Fts5ExprClonePhrase(pCsr->pExpr, iPhrase, &pNew->pExpr);
236161 }
236162
236163 if( rc==SQLITE_OK0 ){
236164 for(rc = fts5CursorFirst(pTab, pNew, 0);
236165 rc==SQLITE_OK0 && CsrFlagTest(pNew, FTS5CSR_EOF)((pNew)->csrflags & (0x01))==0;
236166 rc = fts5NextMethod((sqlite3_vtab_cursor*)pNew)
236167 ){
236168 rc = xCallback(&sFts5Api, (Fts5Context*)pNew, pUserData);
236169 if( rc!=SQLITE_OK0 ){
236170 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
236171 break;
236172 }
236173 }
236174 }
236175
236176 fts5CloseMethod((sqlite3_vtab_cursor*)pNew);
236177 return rc;
236178}
236179
236180static void fts5ApiInvoke(
236181 Fts5Auxiliary *pAux,
236182 Fts5Cursor *pCsr,
236183 sqlite3_context *context,
236184 int argc,
236185 sqlite3_value **argv
236186){
236187 assert( pCsr->pAux==0 )((void) (0));
236188 pCsr->pAux = pAux;
236189 pAux->xFunc(&sFts5Api, (Fts5Context*)pCsr, context, argc, argv);
236190 pCsr->pAux = 0;
236191}
236192
236193static Fts5Cursor *fts5CursorFromCsrid(Fts5Global *pGlobal, i64 iCsrId){
236194 Fts5Cursor *pCsr;
236195 for(pCsr=pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
236196 if( pCsr->iCsrId==iCsrId ) break;
236197 }
236198 return pCsr;
236199}
236200
236201static void fts5ApiCallback(
236202 sqlite3_context *context,
236203 int argc,
236204 sqlite3_value **argv
236205){
236206
236207 Fts5Auxiliary *pAux;
236208 Fts5Cursor *pCsr;
236209 i64 iCsrId;
236210
236211 assert( argc>=1 )((void) (0));
236212 pAux = (Fts5Auxiliary*)sqlite3_user_data(context);
236213 iCsrId = sqlite3_value_int64(argv[0]);
236214
236215 pCsr = fts5CursorFromCsrid(pAux->pGlobal, iCsrId);
236216 if( pCsr==0 || pCsr->ePlan==0 ){
236217 char *zErr = sqlite3_mprintf("no such cursor: %lld", iCsrId);
236218 sqlite3_result_error(context, zErr, -1);
236219 sqlite3_free(zErr);
236220 }else{
236221 fts5ApiInvoke(pAux, pCsr, context, argc-1, &argv[1]);
236222 }
236223}
236224
236225
236226/*
236227** Given cursor id iId, return a pointer to the corresponding Fts5Table
236228** object. Or NULL If the cursor id does not exist.
236229*/
236230static Fts5Table *sqlite3Fts5TableFromCsrid(
236231 Fts5Global *pGlobal, /* FTS5 global context for db handle */
236232 i64 iCsrId /* Id of cursor to find */
236233){
236234 Fts5Cursor *pCsr;
236235 pCsr = fts5CursorFromCsrid(pGlobal, iCsrId);
236236 if( pCsr ){
236237 return (Fts5Table*)pCsr->base.pVtab;
236238 }
236239 return 0;
236240}
236241
236242/*
236243** Return a "position-list blob" corresponding to the current position of
236244** cursor pCsr via sqlite3_result_blob(). A position-list blob contains
236245** the current position-list for each phrase in the query associated with
236246** cursor pCsr.
236247**
236248** A position-list blob begins with (nPhrase-1) varints, where nPhrase is
236249** the number of phrases in the query. Following the varints are the
236250** concatenated position lists for each phrase, in order.
236251**
236252** The first varint (if it exists) contains the size of the position list
236253** for phrase 0. The second (same disclaimer) contains the size of position
236254** list 1. And so on. There is no size field for the final position list,
236255** as it can be derived from the total size of the blob.
236256*/
236257static int fts5PoslistBlob(sqlite3_context *pCtx, Fts5Cursor *pCsr){
236258 int i;
236259 int rc = SQLITE_OK0;
236260 int nPhrase = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
236261 Fts5Buffer val;
236262
236263 memset(&val, 0, sizeof(Fts5Buffer));
236264 switch( ((Fts5Table*)(pCsr->base.pVtab))->pConfig->eDetail ){
236265 case FTS5_DETAIL_FULL0:
236266
236267 /* Append the varints */
236268 for(i=0; i<(nPhrase-1); i++){
236269 const u8 *dummy;
236270 int nByte = sqlite3Fts5ExprPoslist(pCsr->pExpr, i, &dummy);
236271 sqlite3Fts5BufferAppendVarint(&rc, &val, nByte);
236272 }
236273
236274 /* Append the position lists */
236275 for(i=0; i<nPhrase; i++){
236276 const u8 *pPoslist;
236277 int nPoslist;
236278 nPoslist = sqlite3Fts5ExprPoslist(pCsr->pExpr, i, &pPoslist);
236279 sqlite3Fts5BufferAppendBlob(&rc, &val, nPoslist, pPoslist);
236280 }
236281 break;
236282
236283 case FTS5_DETAIL_COLUMNS2:
236284
236285 /* Append the varints */
236286 for(i=0; rc==SQLITE_OK0 && i<(nPhrase-1); i++){
236287 const u8 *dummy;
236288 int nByte;
236289 rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, i, &dummy, &nByte);
236290 sqlite3Fts5BufferAppendVarint(&rc, &val, nByte);
236291 }
236292
236293 /* Append the position lists */
236294 for(i=0; rc==SQLITE_OK0 && i<nPhrase; i++){
236295 const u8 *pPoslist;
236296 int nPoslist;
236297 rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, i, &pPoslist, &nPoslist);
236298 sqlite3Fts5BufferAppendBlob(&rc, &val, nPoslist, pPoslist);
236299 }
236300 break;
236301
236302 default:
236303 break;
236304 }
236305
236306 sqlite3_result_blob(pCtx, val.p, val.n, sqlite3_free);
236307 return rc;
236308}
236309
236310/*
236311** This is the xColumn method, called by SQLite to request a value from
236312** the row that the supplied cursor currently points to.
236313*/
236314static int fts5ColumnMethod(
236315 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
236316 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
236317 int iCol /* Index of column to read value from */
236318){
236319 Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
236320 Fts5Config *pConfig = pTab->p.pConfig;
236321 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
236322 int rc = SQLITE_OK0;
236323
236324 assert( CsrFlagTest(pCsr, FTS5CSR_EOF)==0 )((void) (0));
236325
236326 if( pCsr->ePlan==FTS5_PLAN_SPECIAL3 ){
236327 if( iCol==pConfig->nCol ){
236328 sqlite3_result_int64(pCtx, pCsr->iSpecial);
236329 }
236330 }else
236331
236332 if( iCol==pConfig->nCol ){
236333 /* User is requesting the value of the special column with the same name
236334 ** as the table. Return the cursor integer id number. This value is only
236335 ** useful in that it may be passed as the first argument to an FTS5
236336 ** auxiliary function. */
236337 sqlite3_result_int64(pCtx, pCsr->iCsrId);
236338 }else if( iCol==pConfig->nCol+1 ){
236339
236340 /* The value of the "rank" column. */
236341 if( pCsr->ePlan==FTS5_PLAN_SOURCE2 ){
236342 fts5PoslistBlob(pCtx, pCsr);
236343 }else if(
236344 pCsr->ePlan==FTS5_PLAN_MATCH1
236345 || pCsr->ePlan==FTS5_PLAN_SORTED_MATCH4
236346 ){
236347 if( pCsr->pRank || SQLITE_OK0==(rc = fts5FindRankFunction(pCsr)) ){
236348 fts5ApiInvoke(pCsr->pRank, pCsr, pCtx, pCsr->nRankArg, pCsr->apRankArg);
236349 }
236350 }
236351 }else if( !fts5IsContentless(pTab) ){
236352 pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
236353 rc = fts5SeekCursor(pCsr, 1);
236354 if( rc==SQLITE_OK0 ){
236355 sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1));
236356 }
236357 pConfig->pzErrmsg = 0;
236358 }
236359 return rc;
236360}
236361
236362
236363/*
236364** This routine implements the xFindFunction method for the FTS3
236365** virtual table.
236366*/
236367static int fts5FindFunctionMethod(
236368 sqlite3_vtab *pVtab, /* Virtual table handle */
236369 int nUnused, /* Number of SQL function arguments */
236370 const char *zName, /* Name of SQL function */
236371 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
236372 void **ppArg /* OUT: User data for *pxFunc */
236373){
236374 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
236375 Fts5Auxiliary *pAux;
236376
236377 UNUSED_PARAM(nUnused)(void)(nUnused);
236378 pAux = fts5FindAuxiliary(pTab, zName);
236379 if( pAux ){
236380 *pxFunc = fts5ApiCallback;
236381 *ppArg = (void*)pAux;
236382 return 1;
236383 }
236384
236385 /* No function of the specified name was found. Return 0. */
236386 return 0;
236387}
236388
236389/*
236390** Implementation of FTS5 xRename method. Rename an fts5 table.
236391*/
236392static int fts5RenameMethod(
236393 sqlite3_vtab *pVtab, /* Virtual table handle */
236394 const char *zName /* New name of table */
236395){
236396 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
236397 return sqlite3Fts5StorageRename(pTab->pStorage, zName);
236398}
236399
236400static int sqlite3Fts5FlushToDisk(Fts5Table *pTab){
236401 fts5TripCursors((Fts5FullTable*)pTab);
236402 return sqlite3Fts5StorageSync(((Fts5FullTable*)pTab)->pStorage);
236403}
236404
236405/*
236406** The xSavepoint() method.
236407**
236408** Flush the contents of the pending-terms table to disk.
236409*/
236410static int fts5SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
236411 UNUSED_PARAM(iSavepoint)(void)(iSavepoint); /* Call below is a no-op for NDEBUG builds */
236412 fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_SAVEPOINT, iSavepoint);
236413 return sqlite3Fts5FlushToDisk((Fts5Table*)pVtab);
236414}
236415
236416/*
236417** The xRelease() method.
236418**
236419** This is a no-op.
236420*/
236421static int fts5ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
236422 UNUSED_PARAM(iSavepoint)(void)(iSavepoint); /* Call below is a no-op for NDEBUG builds */
236423 fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_RELEASE, iSavepoint);
236424 return sqlite3Fts5FlushToDisk((Fts5Table*)pVtab);
236425}
236426
236427/*
236428** The xRollbackTo() method.
236429**
236430** Discard the contents of the pending terms table.
236431*/
236432static int fts5RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
236433 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
236434 UNUSED_PARAM(iSavepoint)(void)(iSavepoint); /* Call below is a no-op for NDEBUG builds */
236435 fts5CheckTransactionState(pTab, FTS5_ROLLBACKTO, iSavepoint);
236436 fts5TripCursors(pTab);
236437 return sqlite3Fts5StorageRollback(pTab->pStorage);
236438}
236439
236440/*
236441** Register a new auxiliary function with global context pGlobal.
236442*/
236443static int fts5CreateAux(
236444 fts5_api *pApi, /* Global context (one per db handle) */
236445 const char *zName, /* Name of new function */
236446 void *pUserData, /* User data for aux. function */
236447 fts5_extension_function xFunc, /* Aux. function implementation */
236448 void(*xDestroy)(void*) /* Destructor for pUserData */
236449){
236450 Fts5Global *pGlobal = (Fts5Global*)pApi;
236451 int rc = sqlite3_overload_function(pGlobal->db, zName, -1);
236452 if( rc==SQLITE_OK0 ){
236453 Fts5Auxiliary *pAux;
236454 sqlite3_int64 nName; /* Size of zName in bytes, including \0 */
236455 sqlite3_int64 nByte; /* Bytes of space to allocate */
236456
236457 nName = strlen(zName) + 1;
236458 nByte = sizeof(Fts5Auxiliary) + nName;
236459 pAux = (Fts5Auxiliary*)sqlite3_malloc64(nByte);
236460 if( pAux ){
236461 memset(pAux, 0, (size_t)nByte);
236462 pAux->zFunc = (char*)&pAux[1];
236463 memcpy(pAux->zFunc, zName, nName);
236464 pAux->pGlobal = pGlobal;
236465 pAux->pUserData = pUserData;
236466 pAux->xFunc = xFunc;
236467 pAux->xDestroy = xDestroy;
236468 pAux->pNext = pGlobal->pAux;
236469 pGlobal->pAux = pAux;
236470 }else{
236471 rc = SQLITE_NOMEM7;
236472 }
236473 }
236474
236475 return rc;
236476}
236477
236478/*
236479** Register a new tokenizer. This is the implementation of the
236480** fts5_api.xCreateTokenizer() method.
236481*/
236482static int fts5CreateTokenizer(
236483 fts5_api *pApi, /* Global context (one per db handle) */
236484 const char *zName, /* Name of new function */
236485 void *pUserData, /* User data for aux. function */
236486 fts5_tokenizer *pTokenizer, /* Tokenizer implementation */
236487 void(*xDestroy)(void*) /* Destructor for pUserData */
236488){
236489 Fts5Global *pGlobal = (Fts5Global*)pApi;
236490 Fts5TokenizerModule *pNew;
236491 sqlite3_int64 nName; /* Size of zName and its \0 terminator */
236492 sqlite3_int64 nByte; /* Bytes of space to allocate */
236493 int rc = SQLITE_OK0;
236494
236495 nName = strlen(zName) + 1;
236496 nByte = sizeof(Fts5TokenizerModule) + nName;
236497 pNew = (Fts5TokenizerModule*)sqlite3_malloc64(nByte);
236498 if( pNew ){
236499 memset(pNew, 0, (size_t)nByte);
236500 pNew->zName = (char*)&pNew[1];
236501 memcpy(pNew->zName, zName, nName);
236502 pNew->pUserData = pUserData;
236503 pNew->x = *pTokenizer;
236504 pNew->xDestroy = xDestroy;
236505 pNew->pNext = pGlobal->pTok;
236506 pGlobal->pTok = pNew;
236507 if( pNew->pNext==0 ){
236508 pGlobal->pDfltTok = pNew;
236509 }
236510 }else{
236511 rc = SQLITE_NOMEM7;
236512 }
236513
236514 return rc;
236515}
236516
236517static Fts5TokenizerModule *fts5LocateTokenizer(
236518 Fts5Global *pGlobal,
236519 const char *zName
236520){
236521 Fts5TokenizerModule *pMod = 0;
236522
236523 if( zName==0 ){
236524 pMod = pGlobal->pDfltTok;
236525 }else{
236526 for(pMod=pGlobal->pTok; pMod; pMod=pMod->pNext){
236527 if( sqlite3_stricmp(zName, pMod->zName)==0 ) break;
236528 }
236529 }
236530
236531 return pMod;
236532}
236533
236534/*
236535** Find a tokenizer. This is the implementation of the
236536** fts5_api.xFindTokenizer() method.
236537*/
236538static int fts5FindTokenizer(
236539 fts5_api *pApi, /* Global context (one per db handle) */
236540 const char *zName, /* Name of new function */
236541 void **ppUserData,
236542 fts5_tokenizer *pTokenizer /* Populate this object */
236543){
236544 int rc = SQLITE_OK0;
236545 Fts5TokenizerModule *pMod;
236546
236547 pMod = fts5LocateTokenizer((Fts5Global*)pApi, zName);
236548 if( pMod ){
236549 *pTokenizer = pMod->x;
236550 *ppUserData = pMod->pUserData;
236551 }else{
236552 memset(pTokenizer, 0, sizeof(fts5_tokenizer));
236553 rc = SQLITE_ERROR1;
236554 }
236555
236556 return rc;
236557}
236558
236559static int sqlite3Fts5GetTokenizer(
236560 Fts5Global *pGlobal,
236561 const char **azArg,
236562 int nArg,
236563 Fts5Config *pConfig,
236564 char **pzErr
236565){
236566 Fts5TokenizerModule *pMod;
236567 int rc = SQLITE_OK0;
236568
236569 pMod = fts5LocateTokenizer(pGlobal, nArg==0 ? 0 : azArg[0]);
236570 if( pMod==0 ){
236571 assert( nArg>0 )((void) (0));
236572 rc = SQLITE_ERROR1;
236573 *pzErr = sqlite3_mprintf("no such tokenizer: %s", azArg[0]);
236574 }else{
236575 rc = pMod->x.xCreate(
236576 pMod->pUserData, (azArg?&azArg[1]:0), (nArg?nArg-1:0), &pConfig->pTok
236577 );
236578 pConfig->pTokApi = &pMod->x;
236579 if( rc!=SQLITE_OK0 ){
236580 if( pzErr ) *pzErr = sqlite3_mprintf("error in tokenizer constructor");
236581 }else{
236582 pConfig->ePattern = sqlite3Fts5TokenizerPattern(
236583 pMod->x.xCreate, pConfig->pTok
236584 );
236585 }
236586 }
236587
236588 if( rc!=SQLITE_OK0 ){
236589 pConfig->pTokApi = 0;
236590 pConfig->pTok = 0;
236591 }
236592
236593 return rc;
236594}
236595
236596static void fts5ModuleDestroy(void *pCtx){
236597 Fts5TokenizerModule *pTok, *pNextTok;
236598 Fts5Auxiliary *pAux, *pNextAux;
236599 Fts5Global *pGlobal = (Fts5Global*)pCtx;
236600
236601 for(pAux=pGlobal->pAux; pAux; pAux=pNextAux){
236602 pNextAux = pAux->pNext;
236603 if( pAux->xDestroy ) pAux->xDestroy(pAux->pUserData);
236604 sqlite3_free(pAux);
236605 }
236606
236607 for(pTok=pGlobal->pTok; pTok; pTok=pNextTok){
236608 pNextTok = pTok->pNext;
236609 if( pTok->xDestroy ) pTok->xDestroy(pTok->pUserData);
236610 sqlite3_free(pTok);
236611 }
236612
236613 sqlite3_free(pGlobal);
236614}
236615
236616static void fts5Fts5Func(
236617 sqlite3_context *pCtx, /* Function call context */
236618 int nArg, /* Number of args */
236619 sqlite3_value **apArg /* Function arguments */
236620){
236621 Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx);
236622 fts5_api **ppApi;
236623 UNUSED_PARAM(nArg)(void)(nArg);
236624 assert( nArg==1 )((void) (0));
236625 ppApi = (fts5_api**)sqlite3_value_pointer(apArg[0], "fts5_api_ptr");
236626 if( ppApi ) *ppApi = &pGlobal->api;
236627}
236628
236629/*
236630** Implementation of fts5_source_id() function.
236631*/
236632static void fts5SourceIdFunc(
236633 sqlite3_context *pCtx, /* Function call context */
236634 int nArg, /* Number of args */
236635 sqlite3_value **apUnused /* Function arguments */
236636){
236637 assert( nArg==0 )((void) (0));
236638 UNUSED_PARAM2(nArg, apUnused)(void)(nArg), (void)(apUnused);
236639 sqlite3_result_text(pCtx, "fts5: 2022-07-21 15:24:47 698edb77537b67c41adc68f9b892db56bcf9a55e00371a61420f3ddd668e6603", -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
236640}
236641
236642/*
236643** Return true if zName is the extension on one of the shadow tables used
236644** by this module.
236645*/
236646static int fts5ShadowName(const char *zName){
236647 static const char *azName[] = {
236648 "config", "content", "data", "docsize", "idx"
236649 };
236650 unsigned int i;
236651 for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
236652 if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
236653 }
236654 return 0;
236655}
236656
236657static int fts5Init(sqlite3 *db){
236658 static const sqlite3_module fts5Mod = {
236659 /* iVersion */ 3,
236660 /* xCreate */ fts5CreateMethod,
236661 /* xConnect */ fts5ConnectMethod,
236662 /* xBestIndex */ fts5BestIndexMethod,
236663 /* xDisconnect */ fts5DisconnectMethod,
236664 /* xDestroy */ fts5DestroyMethod,
236665 /* xOpen */ fts5OpenMethod,
236666 /* xClose */ fts5CloseMethod,
236667 /* xFilter */ fts5FilterMethod,
236668 /* xNext */ fts5NextMethod,
236669 /* xEof */ fts5EofMethod,
236670 /* xColumn */ fts5ColumnMethod,
236671 /* xRowid */ fts5RowidMethod,
236672 /* xUpdate */ fts5UpdateMethod,
236673 /* xBegin */ fts5BeginMethod,
236674 /* xSync */ fts5SyncMethod,
236675 /* xCommit */ fts5CommitMethod,
236676 /* xRollback */ fts5RollbackMethod,
236677 /* xFindFunction */ fts5FindFunctionMethod,
236678 /* xRename */ fts5RenameMethod,
236679 /* xSavepoint */ fts5SavepointMethod,
236680 /* xRelease */ fts5ReleaseMethod,
236681 /* xRollbackTo */ fts5RollbackToMethod,
236682 /* xShadowName */ fts5ShadowName
236683 };
236684
236685 int rc;
236686 Fts5Global *pGlobal = 0;
236687
236688 pGlobal = (Fts5Global*)sqlite3_malloc(sizeof(Fts5Global));
236689 if( pGlobal==0 ){
236690 rc = SQLITE_NOMEM7;
236691 }else{
236692 void *p = (void*)pGlobal;
236693 memset(pGlobal, 0, sizeof(Fts5Global));
236694 pGlobal->db = db;
236695 pGlobal->api.iVersion = 2;
236696 pGlobal->api.xCreateFunction = fts5CreateAux;
236697 pGlobal->api.xCreateTokenizer = fts5CreateTokenizer;
236698 pGlobal->api.xFindTokenizer = fts5FindTokenizer;
236699 rc = sqlite3_create_module_v2(db, "fts5", &fts5Mod, p, fts5ModuleDestroy);
236700 if( rc==SQLITE_OK0 ) rc = sqlite3Fts5IndexInit(db);
236701 if( rc==SQLITE_OK0 ) rc = sqlite3Fts5ExprInit(pGlobal, db);
236702 if( rc==SQLITE_OK0 ) rc = sqlite3Fts5AuxInit(&pGlobal->api);
236703 if( rc==SQLITE_OK0 ) rc = sqlite3Fts5TokenizerInit(&pGlobal->api);
236704 if( rc==SQLITE_OK0 ) rc = sqlite3Fts5VocabInit(pGlobal, db);
236705 if( rc==SQLITE_OK0 ){
236706 rc = sqlite3_create_function(
236707 db, "fts5", 1, SQLITE_UTF81, p, fts5Fts5Func, 0, 0
236708 );
236709 }
236710 if( rc==SQLITE_OK0 ){
236711 rc = sqlite3_create_function(
236712 db, "fts5_source_id", 0, SQLITE_UTF81, p, fts5SourceIdFunc, 0, 0
236713 );
236714 }
236715 }
236716
236717 /* If SQLITE_FTS5_ENABLE_TEST_MI is defined, assume that the file
236718 ** fts5_test_mi.c is compiled and linked into the executable. And call
236719 ** its entry point to enable the matchinfo() demo. */
236720#ifdef SQLITE_FTS5_ENABLE_TEST_MI
236721 if( rc==SQLITE_OK0 ){
236722 extern int sqlite3Fts5TestRegisterMatchinfo(sqlite3*);
236723 rc = sqlite3Fts5TestRegisterMatchinfo(db);
236724 }
236725#endif
236726
236727 return rc;
236728}
236729
236730/*
236731** The following functions are used to register the module with SQLite. If
236732** this module is being built as part of the SQLite core (SQLITE_CORE is
236733** defined), then sqlite3_open() will call sqlite3Fts5Init() directly.
236734**
236735** Or, if this module is being built as a loadable extension,
236736** sqlite3Fts5Init() is omitted and the two standard entry points
236737** sqlite3_fts_init() and sqlite3_fts5_init() defined instead.
236738*/
236739#ifndef SQLITE_CORE1
236740#ifdef _WIN32
236741__declspec(dllexport)
236742#endif
236743SQLITE_API int sqlite3_fts_init(
236744 sqlite3 *db,
236745 char **pzErrMsg,
236746 const sqlite3_api_routines *pApi
236747){
236748 SQLITE_EXTENSION_INIT2(pApi)(void)pApi;;
236749 (void)pzErrMsg; /* Unused parameter */
236750 return fts5Init(db);
236751}
236752
236753#ifdef _WIN32
236754__declspec(dllexport)
236755#endif
236756SQLITE_API int sqlite3_fts5_init(
236757 sqlite3 *db,
236758 char **pzErrMsg,
236759 const sqlite3_api_routines *pApi
236760){
236761 SQLITE_EXTENSION_INIT2(pApi)(void)pApi;;
236762 (void)pzErrMsg; /* Unused parameter */
236763 return fts5Init(db);
236764}
236765#else
236766SQLITE_PRIVATEstatic int sqlite3Fts5Init(sqlite3 *db){
236767 return fts5Init(db);
236768}
236769#endif
236770
236771/*
236772** 2014 May 31
236773**
236774** The author disclaims copyright to this source code. In place of
236775** a legal notice, here is a blessing:
236776**
236777** May you do good and not evil.
236778** May you find forgiveness for yourself and forgive others.
236779** May you share freely, never taking more than you give.
236780**
236781******************************************************************************
236782**
236783*/
236784
236785
236786
236787/* #include "fts5Int.h" */
236788
236789struct Fts5Storage {
236790 Fts5Config *pConfig;
236791 Fts5Index *pIndex;
236792 int bTotalsValid; /* True if nTotalRow/aTotalSize[] are valid */
236793 i64 nTotalRow; /* Total number of rows in FTS table */
236794 i64 *aTotalSize; /* Total sizes of each column */
236795 sqlite3_stmt *aStmt[11];
236796};
236797
236798
236799#if FTS5_STMT_SCAN_ASC0!=0
236800# error "FTS5_STMT_SCAN_ASC mismatch"
236801#endif
236802#if FTS5_STMT_SCAN_DESC1!=1
236803# error "FTS5_STMT_SCAN_DESC mismatch"
236804#endif
236805#if FTS5_STMT_LOOKUP2!=2
236806# error "FTS5_STMT_LOOKUP mismatch"
236807#endif
236808
236809#define FTS5_STMT_INSERT_CONTENT3 3
236810#define FTS5_STMT_REPLACE_CONTENT4 4
236811#define FTS5_STMT_DELETE_CONTENT5 5
236812#define FTS5_STMT_REPLACE_DOCSIZE6 6
236813#define FTS5_STMT_DELETE_DOCSIZE7 7
236814#define FTS5_STMT_LOOKUP_DOCSIZE8 8
236815#define FTS5_STMT_REPLACE_CONFIG9 9
236816#define FTS5_STMT_SCAN10 10
236817
236818/*
236819** Prepare the two insert statements - Fts5Storage.pInsertContent and
236820** Fts5Storage.pInsertDocsize - if they have not already been prepared.
236821** Return SQLITE_OK if successful, or an SQLite error code if an error
236822** occurs.
236823*/
236824static int fts5StorageGetStmt(
236825 Fts5Storage *p, /* Storage handle */
236826 int eStmt, /* FTS5_STMT_XXX constant */
236827 sqlite3_stmt **ppStmt, /* OUT: Prepared statement handle */
236828 char **pzErrMsg /* OUT: Error message (if any) */
236829){
236830 int rc = SQLITE_OK0;
236831
236832 /* If there is no %_docsize table, there should be no requests for
236833 ** statements to operate on it. */
236834 assert( p->pConfig->bColumnsize || (((void) (0))
236835 eStmt!=FTS5_STMT_REPLACE_DOCSIZE((void) (0))
236836 && eStmt!=FTS5_STMT_DELETE_DOCSIZE((void) (0))
236837 && eStmt!=FTS5_STMT_LOOKUP_DOCSIZE((void) (0))
236838 ))((void) (0));
236839
236840 assert( eStmt>=0 && eStmt<ArraySize(p->aStmt) )((void) (0));
236841 if( p->aStmt[eStmt]==0 ){
236842 const char *azStmt[] = {
236843 "SELECT %s FROM %s T WHERE T.%Q >= ? AND T.%Q <= ? ORDER BY T.%Q ASC",
236844 "SELECT %s FROM %s T WHERE T.%Q <= ? AND T.%Q >= ? ORDER BY T.%Q DESC",
236845 "SELECT %s FROM %s T WHERE T.%Q=?", /* LOOKUP */
236846
236847 "INSERT INTO %Q.'%q_content' VALUES(%s)", /* INSERT_CONTENT */
236848 "REPLACE INTO %Q.'%q_content' VALUES(%s)", /* REPLACE_CONTENT */
236849 "DELETE FROM %Q.'%q_content' WHERE id=?", /* DELETE_CONTENT */
236850 "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)", /* REPLACE_DOCSIZE */
236851 "DELETE FROM %Q.'%q_docsize' WHERE id=?", /* DELETE_DOCSIZE */
236852
236853 "SELECT sz FROM %Q.'%q_docsize' WHERE id=?", /* LOOKUP_DOCSIZE */
236854
236855 "REPLACE INTO %Q.'%q_config' VALUES(?,?)", /* REPLACE_CONFIG */
236856 "SELECT %s FROM %s AS T", /* SCAN */
236857 };
236858 Fts5Config *pC = p->pConfig;
236859 char *zSql = 0;
236860
236861 switch( eStmt ){
236862 case FTS5_STMT_SCAN10:
236863 zSql = sqlite3_mprintf(azStmt[eStmt],
236864 pC->zContentExprlist, pC->zContent
236865 );
236866 break;
236867
236868 case FTS5_STMT_SCAN_ASC0:
236869 case FTS5_STMT_SCAN_DESC1:
236870 zSql = sqlite3_mprintf(azStmt[eStmt], pC->zContentExprlist,
236871 pC->zContent, pC->zContentRowid, pC->zContentRowid,
236872 pC->zContentRowid
236873 );
236874 break;
236875
236876 case FTS5_STMT_LOOKUP2:
236877 zSql = sqlite3_mprintf(azStmt[eStmt],
236878 pC->zContentExprlist, pC->zContent, pC->zContentRowid
236879 );
236880 break;
236881
236882 case FTS5_STMT_INSERT_CONTENT3:
236883 case FTS5_STMT_REPLACE_CONTENT4: {
236884 int nCol = pC->nCol + 1;
236885 char *zBind;
236886 int i;
236887
236888 zBind = sqlite3_malloc64(1 + nCol*2);
236889 if( zBind ){
236890 for(i=0; i<nCol; i++){
236891 zBind[i*2] = '?';
236892 zBind[i*2 + 1] = ',';
236893 }
236894 zBind[i*2-1] = '\0';
236895 zSql = sqlite3_mprintf(azStmt[eStmt], pC->zDb, pC->zName, zBind);
236896 sqlite3_free(zBind);
236897 }
236898 break;
236899 }
236900
236901 default:
236902 zSql = sqlite3_mprintf(azStmt[eStmt], pC->zDb, pC->zName);
236903 break;
236904 }
236905
236906 if( zSql==0 ){
236907 rc = SQLITE_NOMEM7;
236908 }else{
236909 int f = SQLITE_PREPARE_PERSISTENT0x01;
236910 if( eStmt>FTS5_STMT_LOOKUP2 ) f |= SQLITE_PREPARE_NO_VTAB0x04;
236911 p->pConfig->bLock++;
236912 rc = sqlite3_prepare_v3(pC->db, zSql, -1, f, &p->aStmt[eStmt], 0);
236913 p->pConfig->bLock--;
236914 sqlite3_free(zSql);
236915 if( rc!=SQLITE_OK0 && pzErrMsg ){
236916 *pzErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pC->db));
236917 }
236918 }
236919 }
236920
236921 *ppStmt = p->aStmt[eStmt];
236922 sqlite3_reset(*ppStmt);
236923 return rc;
236924}
236925
236926
236927static int fts5ExecPrintf(
236928 sqlite3 *db,
236929 char **pzErr,
236930 const char *zFormat,
236931 ...
236932){
236933 int rc;
236934 va_list ap; /* ... printf arguments */
236935 char *zSql;
236936
236937 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
236938 zSql = sqlite3_vmprintf(zFormat, ap);
236939
236940 if( zSql==0 ){
236941 rc = SQLITE_NOMEM7;
236942 }else{
236943 rc = sqlite3_exec(db, zSql, 0, 0, pzErr);
236944 sqlite3_free(zSql);
236945 }
236946
236947 va_end(ap)__builtin_va_end(ap);
236948 return rc;
236949}
236950
236951/*
236952** Drop all shadow tables. Return SQLITE_OK if successful or an SQLite error
236953** code otherwise.
236954*/
236955static int sqlite3Fts5DropAll(Fts5Config *pConfig){
236956 int rc = fts5ExecPrintf(pConfig->db, 0,
236957 "DROP TABLE IF EXISTS %Q.'%q_data';"
236958 "DROP TABLE IF EXISTS %Q.'%q_idx';"
236959 "DROP TABLE IF EXISTS %Q.'%q_config';",
236960 pConfig->zDb, pConfig->zName,
236961 pConfig->zDb, pConfig->zName,
236962 pConfig->zDb, pConfig->zName
236963 );
236964 if( rc==SQLITE_OK0 && pConfig->bColumnsize ){
236965 rc = fts5ExecPrintf(pConfig->db, 0,
236966 "DROP TABLE IF EXISTS %Q.'%q_docsize';",
236967 pConfig->zDb, pConfig->zName
236968 );
236969 }
236970 if( rc==SQLITE_OK0 && pConfig->eContent==FTS5_CONTENT_NORMAL0 ){
236971 rc = fts5ExecPrintf(pConfig->db, 0,
236972 "DROP TABLE IF EXISTS %Q.'%q_content';",
236973 pConfig->zDb, pConfig->zName
236974 );
236975 }
236976 return rc;
236977}
236978
236979static void fts5StorageRenameOne(
236980 Fts5Config *pConfig, /* Current FTS5 configuration */
236981 int *pRc, /* IN/OUT: Error code */
236982 const char *zTail, /* Tail of table name e.g. "data", "config" */
236983 const char *zName /* New name of FTS5 table */
236984){
236985 if( *pRc==SQLITE_OK0 ){
236986 *pRc = fts5ExecPrintf(pConfig->db, 0,
236987 "ALTER TABLE %Q.'%q_%s' RENAME TO '%q_%s';",
236988 pConfig->zDb, pConfig->zName, zTail, zName, zTail
236989 );
236990 }
236991}
236992
236993static int sqlite3Fts5StorageRename(Fts5Storage *pStorage, const char *zName){
236994 Fts5Config *pConfig = pStorage->pConfig;
236995 int rc = sqlite3Fts5StorageSync(pStorage);
236996
236997 fts5StorageRenameOne(pConfig, &rc, "data", zName);
236998 fts5StorageRenameOne(pConfig, &rc, "idx", zName);
236999 fts5StorageRenameOne(pConfig, &rc, "config", zName);
237000 if( pConfig->bColumnsize ){
237001 fts5StorageRenameOne(pConfig, &rc, "docsize", zName);
237002 }
237003 if( pConfig->eContent==FTS5_CONTENT_NORMAL0 ){
237004 fts5StorageRenameOne(pConfig, &rc, "content", zName);
237005 }
237006 return rc;
237007}
237008
237009/*
237010** Create the shadow table named zPost, with definition zDefn. Return
237011** SQLITE_OK if successful, or an SQLite error code otherwise.
237012*/
237013static int sqlite3Fts5CreateTable(
237014 Fts5Config *pConfig, /* FTS5 configuration */
237015 const char *zPost, /* Shadow table to create (e.g. "content") */
237016 const char *zDefn, /* Columns etc. for shadow table */
237017 int bWithout, /* True for without rowid */
237018 char **pzErr /* OUT: Error message */
237019){
237020 int rc;
237021 char *zErr = 0;
237022
237023 rc = fts5ExecPrintf(pConfig->db, &zErr, "CREATE TABLE %Q.'%q_%q'(%s)%s",
237024 pConfig->zDb, pConfig->zName, zPost, zDefn,
237025#ifndef SQLITE_FTS5_NO_WITHOUT_ROWID
237026 bWithout?" WITHOUT ROWID":
237027#endif
237028 ""
237029 );
237030 if( zErr ){
237031 *pzErr = sqlite3_mprintf(
237032 "fts5: error creating shadow table %q_%s: %s",
237033 pConfig->zName, zPost, zErr
237034 );
237035 sqlite3_free(zErr);
237036 }
237037
237038 return rc;
237039}
237040
237041/*
237042** Open a new Fts5Index handle. If the bCreate argument is true, create
237043** and initialize the underlying tables
237044**
237045** If successful, set *pp to point to the new object and return SQLITE_OK.
237046** Otherwise, set *pp to NULL and return an SQLite error code.
237047*/
237048static int sqlite3Fts5StorageOpen(
237049 Fts5Config *pConfig,
237050 Fts5Index *pIndex,
237051 int bCreate,
237052 Fts5Storage **pp,
237053 char **pzErr /* OUT: Error message */
237054){
237055 int rc = SQLITE_OK0;
237056 Fts5Storage *p; /* New object */
237057 sqlite3_int64 nByte; /* Bytes of space to allocate */
237058
237059 nByte = sizeof(Fts5Storage) /* Fts5Storage object */
237060 + pConfig->nCol * sizeof(i64); /* Fts5Storage.aTotalSize[] */
237061 *pp = p = (Fts5Storage*)sqlite3_malloc64(nByte);
237062 if( !p ) return SQLITE_NOMEM7;
237063
237064 memset(p, 0, (size_t)nByte);
237065 p->aTotalSize = (i64*)&p[1];
237066 p->pConfig = pConfig;
237067 p->pIndex = pIndex;
237068
237069 if( bCreate ){
237070 if( pConfig->eContent==FTS5_CONTENT_NORMAL0 ){
237071 int nDefn = 32 + pConfig->nCol*10;
237072 char *zDefn = sqlite3_malloc64(32 + (sqlite3_int64)pConfig->nCol * 10);
237073 if( zDefn==0 ){
237074 rc = SQLITE_NOMEM7;
237075 }else{
237076 int i;
237077 int iOff;
237078 sqlite3_snprintf(nDefn, zDefn, "id INTEGER PRIMARY KEY");
237079 iOff = (int)strlen(zDefn);
237080 for(i=0; i<pConfig->nCol; i++){
237081 sqlite3_snprintf(nDefn-iOff, &zDefn[iOff], ", c%d", i);
237082 iOff += (int)strlen(&zDefn[iOff]);
237083 }
237084 rc = sqlite3Fts5CreateTable(pConfig, "content", zDefn, 0, pzErr);
237085 }
237086 sqlite3_free(zDefn);
237087 }
237088
237089 if( rc==SQLITE_OK0 && pConfig->bColumnsize ){
237090 rc = sqlite3Fts5CreateTable(
237091 pConfig, "docsize", "id INTEGER PRIMARY KEY, sz BLOB", 0, pzErr
237092 );
237093 }
237094 if( rc==SQLITE_OK0 ){
237095 rc = sqlite3Fts5CreateTable(
237096 pConfig, "config", "k PRIMARY KEY, v", 1, pzErr
237097 );
237098 }
237099 if( rc==SQLITE_OK0 ){
237100 rc = sqlite3Fts5StorageConfigValue(p, "version", 0, FTS5_CURRENT_VERSION4);
237101 }
237102 }
237103
237104 if( rc ){
237105 sqlite3Fts5StorageClose(p);
237106 *pp = 0;
237107 }
237108 return rc;
237109}
237110
237111/*
237112** Close a handle opened by an earlier call to sqlite3Fts5StorageOpen().
237113*/
237114static int sqlite3Fts5StorageClose(Fts5Storage *p){
237115 int rc = SQLITE_OK0;
237116 if( p ){
237117 int i;
237118
237119 /* Finalize all SQL statements */
237120 for(i=0; i<ArraySize(p->aStmt)((int)(sizeof(p->aStmt)/sizeof(p->aStmt[0]))); i++){
237121 sqlite3_finalize(p->aStmt[i]);
237122 }
237123
237124 sqlite3_free(p);
237125 }
237126 return rc;
237127}
237128
237129typedef struct Fts5InsertCtx Fts5InsertCtx;
237130struct Fts5InsertCtx {
237131 Fts5Storage *pStorage;
237132 int iCol;
237133 int szCol; /* Size of column value in tokens */
237134};
237135
237136/*
237137** Tokenization callback used when inserting tokens into the FTS index.
237138*/
237139static int fts5StorageInsertCallback(
237140 void *pContext, /* Pointer to Fts5InsertCtx object */
237141 int tflags,
237142 const char *pToken, /* Buffer containing token */
237143 int nToken, /* Size of token in bytes */
237144 int iUnused1, /* Start offset of token */
237145 int iUnused2 /* End offset of token */
237146){
237147 Fts5InsertCtx *pCtx = (Fts5InsertCtx*)pContext;
237148 Fts5Index *pIdx = pCtx->pStorage->pIndex;
237149 UNUSED_PARAM2(iUnused1, iUnused2)(void)(iUnused1), (void)(iUnused2);
237150 if( nToken>FTS5_MAX_TOKEN_SIZE32768 ) nToken = FTS5_MAX_TOKEN_SIZE32768;
237151 if( (tflags & FTS5_TOKEN_COLOCATED0x0001)==0 || pCtx->szCol==0 ){
237152 pCtx->szCol++;
237153 }
237154 return sqlite3Fts5IndexWrite(pIdx, pCtx->iCol, pCtx->szCol-1, pToken, nToken);
237155}
237156
237157/*
237158** If a row with rowid iDel is present in the %_content table, add the
237159** delete-markers to the FTS index necessary to delete it. Do not actually
237160** remove the %_content row at this time though.
237161*/
237162static int fts5StorageDeleteFromIndex(
237163 Fts5Storage *p,
237164 i64 iDel,
237165 sqlite3_value **apVal
237166){
237167 Fts5Config *pConfig = p->pConfig;
237168 sqlite3_stmt *pSeek = 0; /* SELECT to read row iDel from %_data */
237169 int rc; /* Return code */
237170 int rc2; /* sqlite3_reset() return code */
237171 int iCol;
237172 Fts5InsertCtx ctx;
237173
237174 if( apVal==0 ){
237175 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP2, &pSeek, 0);
237176 if( rc!=SQLITE_OK0 ) return rc;
237177 sqlite3_bind_int64(pSeek, 1, iDel);
237178 if( sqlite3_step(pSeek)!=SQLITE_ROW100 ){
237179 return sqlite3_reset(pSeek);
237180 }
237181 }
237182
237183 ctx.pStorage = p;
237184 ctx.iCol = -1;
237185 rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 1, iDel);
237186 for(iCol=1; rc==SQLITE_OK0 && iCol<=pConfig->nCol; iCol++){
237187 if( pConfig->abUnindexed[iCol-1]==0 ){
237188 const char *zText;
237189 int nText;
237190 assert( pSeek==0 || apVal==0 )((void) (0));
237191 assert( pSeek!=0 || apVal!=0 )((void) (0));
237192 if( pSeek ){
237193 zText = (const char*)sqlite3_column_text(pSeek, iCol);
237194 nText = sqlite3_column_bytes(pSeek, iCol);
237195 }else if( ALWAYS(apVal)(apVal) ){
237196 zText = (const char*)sqlite3_value_text(apVal[iCol-1]);
237197 nText = sqlite3_value_bytes(apVal[iCol-1]);
237198 }else{
237199 continue;
237200 }
237201 ctx.szCol = 0;
237202 rc = sqlite3Fts5Tokenize(pConfig, FTS5_TOKENIZE_DOCUMENT0x0004,
237203 zText, nText, (void*)&ctx, fts5StorageInsertCallback
237204 );
237205 p->aTotalSize[iCol-1] -= (i64)ctx.szCol;
237206 if( p->aTotalSize[iCol-1]<0 ){
237207 rc = FTS5_CORRUPT(11 | (1<<8));
237208 }
237209 }
237210 }
237211 if( rc==SQLITE_OK0 && p->nTotalRow<1 ){
237212 rc = FTS5_CORRUPT(11 | (1<<8));
237213 }else{
237214 p->nTotalRow--;
237215 }
237216
237217 rc2 = sqlite3_reset(pSeek);
237218 if( rc==SQLITE_OK0 ) rc = rc2;
237219 return rc;
237220}
237221
237222
237223/*
237224** Insert a record into the %_docsize table. Specifically, do:
237225**
237226** INSERT OR REPLACE INTO %_docsize(id, sz) VALUES(iRowid, pBuf);
237227**
237228** If there is no %_docsize table (as happens if the columnsize=0 option
237229** is specified when the FTS5 table is created), this function is a no-op.
237230*/
237231static int fts5StorageInsertDocsize(
237232 Fts5Storage *p, /* Storage module to write to */
237233 i64 iRowid, /* id value */
237234 Fts5Buffer *pBuf /* sz value */
237235){
237236 int rc = SQLITE_OK0;
237237 if( p->pConfig->bColumnsize ){
237238 sqlite3_stmt *pReplace = 0;
237239 rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE6, &pReplace, 0);
237240 if( rc==SQLITE_OK0 ){
237241 sqlite3_bind_int64(pReplace, 1, iRowid);
237242 sqlite3_bind_blob(pReplace, 2, pBuf->p, pBuf->n, SQLITE_STATIC((sqlite3_destructor_type)0));
237243 sqlite3_step(pReplace);
237244 rc = sqlite3_reset(pReplace);
237245 sqlite3_bind_null(pReplace, 2);
237246 }
237247 }
237248 return rc;
237249}
237250
237251/*
237252** Load the contents of the "averages" record from disk into the
237253** p->nTotalRow and p->aTotalSize[] variables. If successful, and if
237254** argument bCache is true, set the p->bTotalsValid flag to indicate
237255** that the contents of aTotalSize[] and nTotalRow are valid until
237256** further notice.
237257**
237258** Return SQLITE_OK if successful, or an SQLite error code if an error
237259** occurs.
237260*/
237261static int fts5StorageLoadTotals(Fts5Storage *p, int bCache){
237262 int rc = SQLITE_OK0;
237263 if( p->bTotalsValid==0 ){
237264 rc = sqlite3Fts5IndexGetAverages(p->pIndex, &p->nTotalRow, p->aTotalSize);
237265 p->bTotalsValid = bCache;
237266 }
237267 return rc;
237268}
237269
237270/*
237271** Store the current contents of the p->nTotalRow and p->aTotalSize[]
237272** variables in the "averages" record on disk.
237273**
237274** Return SQLITE_OK if successful, or an SQLite error code if an error
237275** occurs.
237276*/
237277static int fts5StorageSaveTotals(Fts5Storage *p){
237278 int nCol = p->pConfig->nCol;
237279 int i;
237280 Fts5Buffer buf;
237281 int rc = SQLITE_OK0;
237282 memset(&buf, 0, sizeof(buf));
237283
237284 sqlite3Fts5BufferAppendVarint(&rc, &buf, p->nTotalRow);
237285 for(i=0; i<nCol; i++){
237286 sqlite3Fts5BufferAppendVarint(&rc, &buf, p->aTotalSize[i]);
237287 }
237288 if( rc==SQLITE_OK0 ){
237289 rc = sqlite3Fts5IndexSetAverages(p->pIndex, buf.p, buf.n);
237290 }
237291 sqlite3_free(buf.p);
237292
237293 return rc;
237294}
237295
237296/*
237297** Remove a row from the FTS table.
237298*/
237299static int sqlite3Fts5StorageDelete(Fts5Storage *p, i64 iDel, sqlite3_value **apVal){
237300 Fts5Config *pConfig = p->pConfig;
237301 int rc;
237302 sqlite3_stmt *pDel = 0;
237303
237304 assert( pConfig->eContent!=FTS5_CONTENT_NORMAL || apVal==0 )((void) (0));
237305 rc = fts5StorageLoadTotals(p, 1);
237306
237307 /* Delete the index records */
237308 if( rc==SQLITE_OK0 ){
237309 rc = fts5StorageDeleteFromIndex(p, iDel, apVal);
237310 }
237311
237312 /* Delete the %_docsize record */
237313 if( rc==SQLITE_OK0 && pConfig->bColumnsize ){
237314 rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_DOCSIZE7, &pDel, 0);
237315 if( rc==SQLITE_OK0 ){
237316 sqlite3_bind_int64(pDel, 1, iDel);
237317 sqlite3_step(pDel);
237318 rc = sqlite3_reset(pDel);
237319 }
237320 }
237321
237322 /* Delete the %_content record */
237323 if( pConfig->eContent==FTS5_CONTENT_NORMAL0 ){
237324 if( rc==SQLITE_OK0 ){
237325 rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_CONTENT5, &pDel, 0);
237326 }
237327 if( rc==SQLITE_OK0 ){
237328 sqlite3_bind_int64(pDel, 1, iDel);
237329 sqlite3_step(pDel);
237330 rc = sqlite3_reset(pDel);
237331 }
237332 }
237333
237334 return rc;
237335}
237336
237337/*
237338** Delete all entries in the FTS5 index.
237339*/
237340static int sqlite3Fts5StorageDeleteAll(Fts5Storage *p){
237341 Fts5Config *pConfig = p->pConfig;
237342 int rc;
237343
237344 p->bTotalsValid = 0;
237345
237346 /* Delete the contents of the %_data and %_docsize tables. */
237347 rc = fts5ExecPrintf(pConfig->db, 0,
237348 "DELETE FROM %Q.'%q_data';"
237349 "DELETE FROM %Q.'%q_idx';",
237350 pConfig->zDb, pConfig->zName,
237351 pConfig->zDb, pConfig->zName
237352 );
237353 if( rc==SQLITE_OK0 && pConfig->bColumnsize ){
237354 rc = fts5ExecPrintf(pConfig->db, 0,
237355 "DELETE FROM %Q.'%q_docsize';",
237356 pConfig->zDb, pConfig->zName
237357 );
237358 }
237359
237360 /* Reinitialize the %_data table. This call creates the initial structure
237361 ** and averages records. */
237362 if( rc==SQLITE_OK0 ){
237363 rc = sqlite3Fts5IndexReinit(p->pIndex);
237364 }
237365 if( rc==SQLITE_OK0 ){
237366 rc = sqlite3Fts5StorageConfigValue(p, "version", 0, FTS5_CURRENT_VERSION4);
237367 }
237368 return rc;
237369}
237370
237371static int sqlite3Fts5StorageRebuild(Fts5Storage *p){
237372 Fts5Buffer buf = {0,0,0};
237373 Fts5Config *pConfig = p->pConfig;
237374 sqlite3_stmt *pScan = 0;
237375 Fts5InsertCtx ctx;
237376 int rc, rc2;
237377
237378 memset(&ctx, 0, sizeof(Fts5InsertCtx));
237379 ctx.pStorage = p;
237380 rc = sqlite3Fts5StorageDeleteAll(p);
237381 if( rc==SQLITE_OK0 ){
237382 rc = fts5StorageLoadTotals(p, 1);
237383 }
237384
237385 if( rc==SQLITE_OK0 ){
237386 rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN10, &pScan, 0);
237387 }
237388
237389 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pScan) ){
237390 i64 iRowid = sqlite3_column_int64(pScan, 0);
237391
237392 sqlite3Fts5BufferZero(&buf);
237393 rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 0, iRowid);
237394 for(ctx.iCol=0; rc==SQLITE_OK0 && ctx.iCol<pConfig->nCol; ctx.iCol++){
237395 ctx.szCol = 0;
237396 if( pConfig->abUnindexed[ctx.iCol]==0 ){
237397 const char *zText = (const char*)sqlite3_column_text(pScan, ctx.iCol+1);
237398 int nText = sqlite3_column_bytes(pScan, ctx.iCol+1);
237399 rc = sqlite3Fts5Tokenize(pConfig,
237400 FTS5_TOKENIZE_DOCUMENT0x0004,
237401 zText, nText,
237402 (void*)&ctx,
237403 fts5StorageInsertCallback
237404 );
237405 }
237406 sqlite3Fts5BufferAppendVarint(&rc, &buf, ctx.szCol);
237407 p->aTotalSize[ctx.iCol] += (i64)ctx.szCol;
237408 }
237409 p->nTotalRow++;
237410
237411 if( rc==SQLITE_OK0 ){
237412 rc = fts5StorageInsertDocsize(p, iRowid, &buf);
237413 }
237414 }
237415 sqlite3_free(buf.p);
237416 rc2 = sqlite3_reset(pScan);
237417 if( rc==SQLITE_OK0 ) rc = rc2;
237418
237419 /* Write the averages record */
237420 if( rc==SQLITE_OK0 ){
237421 rc = fts5StorageSaveTotals(p);
237422 }
237423 return rc;
237424}
237425
237426static int sqlite3Fts5StorageOptimize(Fts5Storage *p){
237427 return sqlite3Fts5IndexOptimize(p->pIndex);
237428}
237429
237430static int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge){
237431 return sqlite3Fts5IndexMerge(p->pIndex, nMerge);
237432}
237433
237434static int sqlite3Fts5StorageReset(Fts5Storage *p){
237435 return sqlite3Fts5IndexReset(p->pIndex);
237436}
237437
237438/*
237439** Allocate a new rowid. This is used for "external content" tables when
237440** a NULL value is inserted into the rowid column. The new rowid is allocated
237441** by inserting a dummy row into the %_docsize table. The dummy will be
237442** overwritten later.
237443**
237444** If the %_docsize table does not exist, SQLITE_MISMATCH is returned. In
237445** this case the user is required to provide a rowid explicitly.
237446*/
237447static int fts5StorageNewRowid(Fts5Storage *p, i64 *piRowid){
237448 int rc = SQLITE_MISMATCH20;
237449 if( p->pConfig->bColumnsize ){
237450 sqlite3_stmt *pReplace = 0;
237451 rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE6, &pReplace, 0);
237452 if( rc==SQLITE_OK0 ){
237453 sqlite3_bind_null(pReplace, 1);
237454 sqlite3_bind_null(pReplace, 2);
237455 sqlite3_step(pReplace);
237456 rc = sqlite3_reset(pReplace);
237457 }
237458 if( rc==SQLITE_OK0 ){
237459 *piRowid = sqlite3_last_insert_rowid(p->pConfig->db);
237460 }
237461 }
237462 return rc;
237463}
237464
237465/*
237466** Insert a new row into the FTS content table.
237467*/
237468static int sqlite3Fts5StorageContentInsert(
237469 Fts5Storage *p,
237470 sqlite3_value **apVal,
237471 i64 *piRowid
237472){
237473 Fts5Config *pConfig = p->pConfig;
237474 int rc = SQLITE_OK0;
237475
237476 /* Insert the new row into the %_content table. */
237477 if( pConfig->eContent!=FTS5_CONTENT_NORMAL0 ){
237478 if( sqlite3_value_type(apVal[1])==SQLITE_INTEGER1 ){
237479 *piRowid = sqlite3_value_int64(apVal[1]);
237480 }else{
237481 rc = fts5StorageNewRowid(p, piRowid);
237482 }
237483 }else{
237484 sqlite3_stmt *pInsert = 0; /* Statement to write %_content table */
237485 int i; /* Counter variable */
237486 rc = fts5StorageGetStmt(p, FTS5_STMT_INSERT_CONTENT3, &pInsert, 0);
237487 for(i=1; rc==SQLITE_OK0 && i<=pConfig->nCol+1; i++){
237488 rc = sqlite3_bind_value(pInsert, i, apVal[i]);
237489 }
237490 if( rc==SQLITE_OK0 ){
237491 sqlite3_step(pInsert);
237492 rc = sqlite3_reset(pInsert);
237493 }
237494 *piRowid = sqlite3_last_insert_rowid(pConfig->db);
237495 }
237496
237497 return rc;
237498}
237499
237500/*
237501** Insert new entries into the FTS index and %_docsize table.
237502*/
237503static int sqlite3Fts5StorageIndexInsert(
237504 Fts5Storage *p,
237505 sqlite3_value **apVal,
237506 i64 iRowid
237507){
237508 Fts5Config *pConfig = p->pConfig;
237509 int rc = SQLITE_OK0; /* Return code */
237510 Fts5InsertCtx ctx; /* Tokenization callback context object */
237511 Fts5Buffer buf; /* Buffer used to build up %_docsize blob */
237512
237513 memset(&buf, 0, sizeof(Fts5Buffer));
237514 ctx.pStorage = p;
237515 rc = fts5StorageLoadTotals(p, 1);
237516
237517 if( rc==SQLITE_OK0 ){
237518 rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 0, iRowid);
237519 }
237520 for(ctx.iCol=0; rc==SQLITE_OK0 && ctx.iCol<pConfig->nCol; ctx.iCol++){
237521 ctx.szCol = 0;
237522 if( pConfig->abUnindexed[ctx.iCol]==0 ){
237523 const char *zText = (const char*)sqlite3_value_text(apVal[ctx.iCol+2]);
237524 int nText = sqlite3_value_bytes(apVal[ctx.iCol+2]);
237525 rc = sqlite3Fts5Tokenize(pConfig,
237526 FTS5_TOKENIZE_DOCUMENT0x0004,
237527 zText, nText,
237528 (void*)&ctx,
237529 fts5StorageInsertCallback
237530 );
237531 }
237532 sqlite3Fts5BufferAppendVarint(&rc, &buf, ctx.szCol);
237533 p->aTotalSize[ctx.iCol] += (i64)ctx.szCol;
237534 }
237535 p->nTotalRow++;
237536
237537 /* Write the %_docsize record */
237538 if( rc==SQLITE_OK0 ){
237539 rc = fts5StorageInsertDocsize(p, iRowid, &buf);
237540 }
237541 sqlite3_free(buf.p);
237542
237543 return rc;
237544}
237545
237546static int fts5StorageCount(Fts5Storage *p, const char *zSuffix, i64 *pnRow){
237547 Fts5Config *pConfig = p->pConfig;
237548 char *zSql;
237549 int rc;
237550
237551 zSql = sqlite3_mprintf("SELECT count(*) FROM %Q.'%q_%s'",
237552 pConfig->zDb, pConfig->zName, zSuffix
237553 );
237554 if( zSql==0 ){
237555 rc = SQLITE_NOMEM7;
237556 }else{
237557 sqlite3_stmt *pCnt = 0;
237558 rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &pCnt, 0);
237559 if( rc==SQLITE_OK0 ){
237560 if( SQLITE_ROW100==sqlite3_step(pCnt) ){
237561 *pnRow = sqlite3_column_int64(pCnt, 0);
237562 }
237563 rc = sqlite3_finalize(pCnt);
237564 }
237565 }
237566
237567 sqlite3_free(zSql);
237568 return rc;
237569}
237570
237571/*
237572** Context object used by sqlite3Fts5StorageIntegrity().
237573*/
237574typedef struct Fts5IntegrityCtx Fts5IntegrityCtx;
237575struct Fts5IntegrityCtx {
237576 i64 iRowid;
237577 int iCol;
237578 int szCol;
237579 u64 cksum;
237580 Fts5Termset *pTermset;
237581 Fts5Config *pConfig;
237582};
237583
237584
237585/*
237586** Tokenization callback used by integrity check.
237587*/
237588static int fts5StorageIntegrityCallback(
237589 void *pContext, /* Pointer to Fts5IntegrityCtx object */
237590 int tflags,
237591 const char *pToken, /* Buffer containing token */
237592 int nToken, /* Size of token in bytes */
237593 int iUnused1, /* Start offset of token */
237594 int iUnused2 /* End offset of token */
237595){
237596 Fts5IntegrityCtx *pCtx = (Fts5IntegrityCtx*)pContext;
237597 Fts5Termset *pTermset = pCtx->pTermset;
237598 int bPresent;
237599 int ii;
237600 int rc = SQLITE_OK0;
237601 int iPos;
237602 int iCol;
237603
237604 UNUSED_PARAM2(iUnused1, iUnused2)(void)(iUnused1), (void)(iUnused2);
237605 if( nToken>FTS5_MAX_TOKEN_SIZE32768 ) nToken = FTS5_MAX_TOKEN_SIZE32768;
237606
237607 if( (tflags & FTS5_TOKEN_COLOCATED0x0001)==0 || pCtx->szCol==0 ){
237608 pCtx->szCol++;
237609 }
237610
237611 switch( pCtx->pConfig->eDetail ){
237612 case FTS5_DETAIL_FULL0:
237613 iPos = pCtx->szCol-1;
237614 iCol = pCtx->iCol;
237615 break;
237616
237617 case FTS5_DETAIL_COLUMNS2:
237618 iPos = pCtx->iCol;
237619 iCol = 0;
237620 break;
237621
237622 default:
237623 assert( pCtx->pConfig->eDetail==FTS5_DETAIL_NONE )((void) (0));
237624 iPos = 0;
237625 iCol = 0;
237626 break;
237627 }
237628
237629 rc = sqlite3Fts5TermsetAdd(pTermset, 0, pToken, nToken, &bPresent);
237630 if( rc==SQLITE_OK0 && bPresent==0 ){
237631 pCtx->cksum ^= sqlite3Fts5IndexEntryCksum(
237632 pCtx->iRowid, iCol, iPos, 0, pToken, nToken
237633 );
237634 }
237635
237636 for(ii=0; rc==SQLITE_OK0 && ii<pCtx->pConfig->nPrefix; ii++){
237637 const int nChar = pCtx->pConfig->aPrefix[ii];
237638 int nByte = sqlite3Fts5IndexCharlenToBytelen(pToken, nToken, nChar);
237639 if( nByte ){
237640 rc = sqlite3Fts5TermsetAdd(pTermset, ii+1, pToken, nByte, &bPresent);
237641 if( bPresent==0 ){
237642 pCtx->cksum ^= sqlite3Fts5IndexEntryCksum(
237643 pCtx->iRowid, iCol, iPos, ii+1, pToken, nByte
237644 );
237645 }
237646 }
237647 }
237648
237649 return rc;
237650}
237651
237652/*
237653** Check that the contents of the FTS index match that of the %_content
237654** table. Return SQLITE_OK if they do, or SQLITE_CORRUPT if not. Return
237655** some other SQLite error code if an error occurs while attempting to
237656** determine this.
237657*/
237658static int sqlite3Fts5StorageIntegrity(Fts5Storage *p, int iArg){
237659 Fts5Config *pConfig = p->pConfig;
237660 int rc = SQLITE_OK0; /* Return code */
237661 int *aColSize; /* Array of size pConfig->nCol */
237662 i64 *aTotalSize; /* Array of size pConfig->nCol */
237663 Fts5IntegrityCtx ctx;
237664 sqlite3_stmt *pScan;
237665 int bUseCksum;
237666
237667 memset(&ctx, 0, sizeof(Fts5IntegrityCtx));
237668 ctx.pConfig = p->pConfig;
237669 aTotalSize = (i64*)sqlite3_malloc64(pConfig->nCol*(sizeof(int)+sizeof(i64)));
237670 if( !aTotalSize ) return SQLITE_NOMEM7;
237671 aColSize = (int*)&aTotalSize[pConfig->nCol];
237672 memset(aTotalSize, 0, sizeof(i64) * pConfig->nCol);
237673
237674 bUseCksum = (pConfig->eContent==FTS5_CONTENT_NORMAL0
237675 || (pConfig->eContent==FTS5_CONTENT_EXTERNAL2 && iArg)
237676 );
237677 if( bUseCksum ){
237678 /* Generate the expected index checksum based on the contents of the
237679 ** %_content table. This block stores the checksum in ctx.cksum. */
237680 rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN10, &pScan, 0);
237681 if( rc==SQLITE_OK0 ){
237682 int rc2;
237683 while( SQLITE_ROW100==sqlite3_step(pScan) ){
237684 int i;
237685 ctx.iRowid = sqlite3_column_int64(pScan, 0);
237686 ctx.szCol = 0;
237687 if( pConfig->bColumnsize ){
237688 rc = sqlite3Fts5StorageDocsize(p, ctx.iRowid, aColSize);
237689 }
237690 if( rc==SQLITE_OK0 && pConfig->eDetail==FTS5_DETAIL_NONE1 ){
237691 rc = sqlite3Fts5TermsetNew(&ctx.pTermset);
237692 }
237693 for(i=0; rc==SQLITE_OK0 && i<pConfig->nCol; i++){
237694 if( pConfig->abUnindexed[i] ) continue;
237695 ctx.iCol = i;
237696 ctx.szCol = 0;
237697 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS2 ){
237698 rc = sqlite3Fts5TermsetNew(&ctx.pTermset);
237699 }
237700 if( rc==SQLITE_OK0 ){
237701 const char *zText = (const char*)sqlite3_column_text(pScan, i+1);
237702 int nText = sqlite3_column_bytes(pScan, i+1);
237703 rc = sqlite3Fts5Tokenize(pConfig,
237704 FTS5_TOKENIZE_DOCUMENT0x0004,
237705 zText, nText,
237706 (void*)&ctx,
237707 fts5StorageIntegrityCallback
237708 );
237709 }
237710 if( rc==SQLITE_OK0 && pConfig->bColumnsize && ctx.szCol!=aColSize[i] ){
237711 rc = FTS5_CORRUPT(11 | (1<<8));
237712 }
237713 aTotalSize[i] += ctx.szCol;
237714 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS2 ){
237715 sqlite3Fts5TermsetFree(ctx.pTermset);
237716 ctx.pTermset = 0;
237717 }
237718 }
237719 sqlite3Fts5TermsetFree(ctx.pTermset);
237720 ctx.pTermset = 0;
237721
237722 if( rc!=SQLITE_OK0 ) break;
237723 }
237724 rc2 = sqlite3_reset(pScan);
237725 if( rc==SQLITE_OK0 ) rc = rc2;
237726 }
237727
237728 /* Test that the "totals" (sometimes called "averages") record looks Ok */
237729 if( rc==SQLITE_OK0 ){
237730 int i;
237731 rc = fts5StorageLoadTotals(p, 0);
237732 for(i=0; rc==SQLITE_OK0 && i<pConfig->nCol; i++){
237733 if( p->aTotalSize[i]!=aTotalSize[i] ) rc = FTS5_CORRUPT(11 | (1<<8));
237734 }
237735 }
237736
237737 /* Check that the %_docsize and %_content tables contain the expected
237738 ** number of rows. */
237739 if( rc==SQLITE_OK0 && pConfig->eContent==FTS5_CONTENT_NORMAL0 ){
237740 i64 nRow = 0;
237741 rc = fts5StorageCount(p, "content", &nRow);
237742 if( rc==SQLITE_OK0 && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT(11 | (1<<8));
237743 }
237744 if( rc==SQLITE_OK0 && pConfig->bColumnsize ){
237745 i64 nRow = 0;
237746 rc = fts5StorageCount(p, "docsize", &nRow);
237747 if( rc==SQLITE_OK0 && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT(11 | (1<<8));
237748 }
237749 }
237750
237751 /* Pass the expected checksum down to the FTS index module. It will
237752 ** verify, amongst other things, that it matches the checksum generated by
237753 ** inspecting the index itself. */
237754 if( rc==SQLITE_OK0 ){
237755 rc = sqlite3Fts5IndexIntegrityCheck(p->pIndex, ctx.cksum, bUseCksum);
237756 }
237757
237758 sqlite3_free(aTotalSize);
237759 return rc;
237760}
237761
237762/*
237763** Obtain an SQLite statement handle that may be used to read data from the
237764** %_content table.
237765*/
237766static int sqlite3Fts5StorageStmt(
237767 Fts5Storage *p,
237768 int eStmt,
237769 sqlite3_stmt **pp,
237770 char **pzErrMsg
237771){
237772 int rc;
237773 assert( eStmt==FTS5_STMT_SCAN_ASC((void) (0))
237774 || eStmt==FTS5_STMT_SCAN_DESC((void) (0))
237775 || eStmt==FTS5_STMT_LOOKUP((void) (0))
237776 )((void) (0));
237777 rc = fts5StorageGetStmt(p, eStmt, pp, pzErrMsg);
237778 if( rc==SQLITE_OK0 ){
237779 assert( p->aStmt[eStmt]==*pp )((void) (0));
237780 p->aStmt[eStmt] = 0;
237781 }
237782 return rc;
237783}
237784
237785/*
237786** Release an SQLite statement handle obtained via an earlier call to
237787** sqlite3Fts5StorageStmt(). The eStmt parameter passed to this function
237788** must match that passed to the sqlite3Fts5StorageStmt() call.
237789*/
237790static void sqlite3Fts5StorageStmtRelease(
237791 Fts5Storage *p,
237792 int eStmt,
237793 sqlite3_stmt *pStmt
237794){
237795 assert( eStmt==FTS5_STMT_SCAN_ASC((void) (0))
237796 || eStmt==FTS5_STMT_SCAN_DESC((void) (0))
237797 || eStmt==FTS5_STMT_LOOKUP((void) (0))
237798 )((void) (0));
237799 if( p->aStmt[eStmt]==0 ){
237800 sqlite3_reset(pStmt);
237801 p->aStmt[eStmt] = pStmt;
237802 }else{
237803 sqlite3_finalize(pStmt);
237804 }
237805}
237806
237807static int fts5StorageDecodeSizeArray(
237808 int *aCol, int nCol, /* Array to populate */
237809 const u8 *aBlob, int nBlob /* Record to read varints from */
237810){
237811 int i;
237812 int iOff = 0;
237813 for(i=0; i<nCol; i++){
237814 if( iOff>=nBlob ) return 1;
237815 iOff += fts5GetVarint32(&aBlob[iOff], aCol[i])sqlite3Fts5GetVarint32(&aBlob[iOff],(u32*)&aCol[i]);
237816 }
237817 return (iOff!=nBlob);
237818}
237819
237820/*
237821** Argument aCol points to an array of integers containing one entry for
237822** each table column. This function reads the %_docsize record for the
237823** specified rowid and populates aCol[] with the results.
237824**
237825** An SQLite error code is returned if an error occurs, or SQLITE_OK
237826** otherwise.
237827*/
237828static int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol){
237829 int nCol = p->pConfig->nCol; /* Number of user columns in table */
237830 sqlite3_stmt *pLookup = 0; /* Statement to query %_docsize */
237831 int rc; /* Return Code */
237832
237833 assert( p->pConfig->bColumnsize )((void) (0));
237834 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP_DOCSIZE8, &pLookup, 0);
237835 if( pLookup ){
237836 int bCorrupt = 1;
237837 assert( rc==SQLITE_OK )((void) (0));
237838 sqlite3_bind_int64(pLookup, 1, iRowid);
237839 if( SQLITE_ROW100==sqlite3_step(pLookup) ){
237840 const u8 *aBlob = sqlite3_column_blob(pLookup, 0);
237841 int nBlob = sqlite3_column_bytes(pLookup, 0);
237842 if( 0==fts5StorageDecodeSizeArray(aCol, nCol, aBlob, nBlob) ){
237843 bCorrupt = 0;
237844 }
237845 }
237846 rc = sqlite3_reset(pLookup);
237847 if( bCorrupt && rc==SQLITE_OK0 ){
237848 rc = FTS5_CORRUPT(11 | (1<<8));
237849 }
237850 }else{
237851 assert( rc!=SQLITE_OK )((void) (0));
237852 }
237853
237854 return rc;
237855}
237856
237857static int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnToken){
237858 int rc = fts5StorageLoadTotals(p, 0);
237859 if( rc==SQLITE_OK0 ){
237860 *pnToken = 0;
237861 if( iCol<0 ){
237862 int i;
237863 for(i=0; i<p->pConfig->nCol; i++){
237864 *pnToken += p->aTotalSize[i];
237865 }
237866 }else if( iCol<p->pConfig->nCol ){
237867 *pnToken = p->aTotalSize[iCol];
237868 }else{
237869 rc = SQLITE_RANGE25;
237870 }
237871 }
237872 return rc;
237873}
237874
237875static int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow){
237876 int rc = fts5StorageLoadTotals(p, 0);
237877 if( rc==SQLITE_OK0 ){
237878 /* nTotalRow being zero does not necessarily indicate a corrupt
237879 ** database - it might be that the FTS5 table really does contain zero
237880 ** rows. However this function is only called from the xRowCount() API,
237881 ** and there is no way for that API to be invoked if the table contains
237882 ** no rows. Hence the FTS5_CORRUPT return. */
237883 *pnRow = p->nTotalRow;
237884 if( p->nTotalRow<=0 ) rc = FTS5_CORRUPT(11 | (1<<8));
237885 }
237886 return rc;
237887}
237888
237889/*
237890** Flush any data currently held in-memory to disk.
237891*/
237892static int sqlite3Fts5StorageSync(Fts5Storage *p){
237893 int rc = SQLITE_OK0;
237894 i64 iLastRowid = sqlite3_last_insert_rowid(p->pConfig->db);
237895 if( p->bTotalsValid ){
237896 rc = fts5StorageSaveTotals(p);
237897 p->bTotalsValid = 0;
237898 }
237899 if( rc==SQLITE_OK0 ){
237900 rc = sqlite3Fts5IndexSync(p->pIndex);
237901 }
237902 sqlite3_set_last_insert_rowid(p->pConfig->db, iLastRowid);
237903 return rc;
237904}
237905
237906static int sqlite3Fts5StorageRollback(Fts5Storage *p){
237907 p->bTotalsValid = 0;
237908 return sqlite3Fts5IndexRollback(p->pIndex);
237909}
237910
237911static int sqlite3Fts5StorageConfigValue(
237912 Fts5Storage *p,
237913 const char *z,
237914 sqlite3_value *pVal,
237915 int iVal
237916){
237917 sqlite3_stmt *pReplace = 0;
237918 int rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_CONFIG9, &pReplace, 0);
237919 if( rc==SQLITE_OK0 ){
237920 sqlite3_bind_text(pReplace, 1, z, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
237921 if( pVal ){
237922 sqlite3_bind_value(pReplace, 2, pVal);
237923 }else{
237924 sqlite3_bind_int(pReplace, 2, iVal);
237925 }
237926 sqlite3_step(pReplace);
237927 rc = sqlite3_reset(pReplace);
237928 sqlite3_bind_null(pReplace, 1);
237929 }
237930 if( rc==SQLITE_OK0 && pVal ){
237931 int iNew = p->pConfig->iCookie + 1;
237932 rc = sqlite3Fts5IndexSetCookie(p->pIndex, iNew);
237933 if( rc==SQLITE_OK0 ){
237934 p->pConfig->iCookie = iNew;
237935 }
237936 }
237937 return rc;
237938}
237939
237940/*
237941** 2014 May 31
237942**
237943** The author disclaims copyright to this source code. In place of
237944** a legal notice, here is a blessing:
237945**
237946** May you do good and not evil.
237947** May you find forgiveness for yourself and forgive others.
237948** May you share freely, never taking more than you give.
237949**
237950******************************************************************************
237951*/
237952
237953
237954/* #include "fts5Int.h" */
237955
237956/**************************************************************************
237957** Start of ascii tokenizer implementation.
237958*/
237959
237960/*
237961** For tokenizers with no "unicode" modifier, the set of token characters
237962** is the same as the set of ASCII range alphanumeric characters.
237963*/
237964static unsigned char aAsciiTokenChar[128] = {
237965 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00..0x0F */
237966 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10..0x1F */
237967 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20..0x2F */
237968 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 0x30..0x3F */
237969 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40..0x4F */
237970 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x50..0x5F */
237971 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60..0x6F */
237972 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x70..0x7F */
237973};
237974
237975typedef struct AsciiTokenizer AsciiTokenizer;
237976struct AsciiTokenizer {
237977 unsigned char aTokenChar[128];
237978};
237979
237980static void fts5AsciiAddExceptions(
237981 AsciiTokenizer *p,
237982 const char *zArg,
237983 int bTokenChars
237984){
237985 int i;
237986 for(i=0; zArg[i]; i++){
237987 if( (zArg[i] & 0x80)==0 ){
237988 p->aTokenChar[(int)zArg[i]] = (unsigned char)bTokenChars;
237989 }
237990 }
237991}
237992
237993/*
237994** Delete a "ascii" tokenizer.
237995*/
237996static void fts5AsciiDelete(Fts5Tokenizer *p){
237997 sqlite3_free(p);
237998}
237999
238000/*
238001** Create an "ascii" tokenizer.
238002*/
238003static int fts5AsciiCreate(
238004 void *pUnused,
238005 const char **azArg, int nArg,
238006 Fts5Tokenizer **ppOut
238007){
238008 int rc = SQLITE_OK0;
238009 AsciiTokenizer *p = 0;
238010 UNUSED_PARAM(pUnused)(void)(pUnused);
238011 if( nArg%2 ){
238012 rc = SQLITE_ERROR1;
238013 }else{
238014 p = sqlite3_malloc(sizeof(AsciiTokenizer));
238015 if( p==0 ){
238016 rc = SQLITE_NOMEM7;
238017 }else{
238018 int i;
238019 memset(p, 0, sizeof(AsciiTokenizer));
238020 memcpy(p->aTokenChar, aAsciiTokenChar, sizeof(aAsciiTokenChar));
238021 for(i=0; rc==SQLITE_OK0 && i<nArg; i+=2){
238022 const char *zArg = azArg[i+1];
238023 if( 0==sqlite3_stricmp(azArg[i], "tokenchars") ){
238024 fts5AsciiAddExceptions(p, zArg, 1);
238025 }else
238026 if( 0==sqlite3_stricmp(azArg[i], "separators") ){
238027 fts5AsciiAddExceptions(p, zArg, 0);
238028 }else{
238029 rc = SQLITE_ERROR1;
238030 }
238031 }
238032 if( rc!=SQLITE_OK0 ){
238033 fts5AsciiDelete((Fts5Tokenizer*)p);
238034 p = 0;
238035 }
238036 }
238037 }
238038
238039 *ppOut = (Fts5Tokenizer*)p;
238040 return rc;
238041}
238042
238043
238044static void asciiFold(char *aOut, const char *aIn, int nByte){
238045 int i;
238046 for(i=0; i<nByte; i++){
238047 char c = aIn[i];
238048 if( c>='A' && c<='Z' ) c += 32;
238049 aOut[i] = c;
238050 }
238051}
238052
238053/*
238054** Tokenize some text using the ascii tokenizer.
238055*/
238056static int fts5AsciiTokenize(
238057 Fts5Tokenizer *pTokenizer,
238058 void *pCtx,
238059 int iUnused,
238060 const char *pText, int nText,
238061 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
238062){
238063 AsciiTokenizer *p = (AsciiTokenizer*)pTokenizer;
238064 int rc = SQLITE_OK0;
238065 int ie;
238066 int is = 0;
238067
238068 char aFold[64];
238069 int nFold = sizeof(aFold);
238070 char *pFold = aFold;
238071 unsigned char *a = p->aTokenChar;
238072
238073 UNUSED_PARAM(iUnused)(void)(iUnused);
238074
238075 while( is<nText && rc==SQLITE_OK0 ){
238076 int nByte;
238077
238078 /* Skip any leading divider characters. */
238079 while( is<nText && ((pText[is]&0x80)==0 && a[(int)pText[is]]==0) ){
238080 is++;
238081 }
238082 if( is==nText ) break;
238083
238084 /* Count the token characters */
238085 ie = is+1;
238086 while( ie<nText && ((pText[ie]&0x80) || a[(int)pText[ie]] ) ){
238087 ie++;
238088 }
238089
238090 /* Fold to lower case */
238091 nByte = ie-is;
238092 if( nByte>nFold ){
238093 if( pFold!=aFold ) sqlite3_free(pFold);
238094 pFold = sqlite3_malloc64((sqlite3_int64)nByte*2);
238095 if( pFold==0 ){
238096 rc = SQLITE_NOMEM7;
238097 break;
238098 }
238099 nFold = nByte*2;
238100 }
238101 asciiFold(pFold, &pText[is], nByte);
238102
238103 /* Invoke the token callback */
238104 rc = xToken(pCtx, 0, pFold, nByte, is, ie);
238105 is = ie+1;
238106 }
238107
238108 if( pFold!=aFold ) sqlite3_free(pFold);
238109 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
238110 return rc;
238111}
238112
238113/**************************************************************************
238114** Start of unicode61 tokenizer implementation.
238115*/
238116
238117
238118/*
238119** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
238120** from the sqlite3 source file utf.c. If this file is compiled as part
238121** of the amalgamation, they are not required.
238122*/
238123#ifndef SQLITE_AMALGAMATION1
238124
238125static const unsigned char sqlite3Utf8Trans1[] = {
238126 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
238127 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
238128 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
238129 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
238130 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
238131 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
238132 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
238133 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
238134};
238135
238136#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
; } }
\
238137 c = *(zIn++); \
238138 if( c>=0xc0 ){ \
238139 c = sqlite3Utf8Trans1[c-0xc0]; \
238140 while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \
238141 c = (c<<6) + (0x3f & *(zIn++)); \
238142 } \
238143 if( c<0x80 \
238144 || (c&0xFFFFF800)==0xD800 \
238145 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
238146 }
238147
238148
238149#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); } }
{ \
238150 if( c<0x00080 ){ \
238151 *zOut++ = (unsigned char)(c&0xFF); \
238152 } \
238153 else if( c<0x00800 ){ \
238154 *zOut++ = 0xC0 + (unsigned char)((c>>6)&0x1F); \
238155 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
238156 } \
238157 else if( c<0x10000 ){ \
238158 *zOut++ = 0xE0 + (unsigned char)((c>>12)&0x0F); \
238159 *zOut++ = 0x80 + (unsigned char)((c>>6) & 0x3F); \
238160 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
238161 }else{ \
238162 *zOut++ = 0xF0 + (unsigned char)((c>>18) & 0x07); \
238163 *zOut++ = 0x80 + (unsigned char)((c>>12) & 0x3F); \
238164 *zOut++ = 0x80 + (unsigned char)((c>>6) & 0x3F); \
238165 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
238166 } \
238167}
238168
238169#endif /* ifndef SQLITE_AMALGAMATION */
238170
238171typedef struct Unicode61Tokenizer Unicode61Tokenizer;
238172struct Unicode61Tokenizer {
238173 unsigned char aTokenChar[128]; /* ASCII range token characters */
238174 char *aFold; /* Buffer to fold text into */
238175 int nFold; /* Size of aFold[] in bytes */
238176 int eRemoveDiacritic; /* True if remove_diacritics=1 is set */
238177 int nException;
238178 int *aiException;
238179
238180 unsigned char aCategory[32]; /* True for token char categories */
238181};
238182
238183/* Values for eRemoveDiacritic (must match internals of fts5_unicode2.c) */
238184#define FTS5_REMOVE_DIACRITICS_NONE0 0
238185#define FTS5_REMOVE_DIACRITICS_SIMPLE1 1
238186#define FTS5_REMOVE_DIACRITICS_COMPLEX2 2
238187
238188static int fts5UnicodeAddExceptions(
238189 Unicode61Tokenizer *p, /* Tokenizer object */
238190 const char *z, /* Characters to treat as exceptions */
238191 int bTokenChars /* 1 for 'tokenchars', 0 for 'separators' */
238192){
238193 int rc = SQLITE_OK0;
238194 int n = (int)strlen(z);
238195 int *aNew;
238196
238197 if( n>0 ){
238198 aNew = (int*)sqlite3_realloc64(p->aiException,
238199 (n+p->nException)*sizeof(int));
238200 if( aNew ){
238201 int nNew = p->nException;
238202 const unsigned char *zCsr = (const unsigned char*)z;
238203 const unsigned char *zTerm = (const unsigned char*)&z[n];
238204 while( zCsr<zTerm ){
238205 u32 iCode;
238206 int bToken;
238207 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; } }
;
238208 if( iCode<128 ){
238209 p->aTokenChar[iCode] = (unsigned char)bTokenChars;
238210 }else{
238211 bToken = p->aCategory[sqlite3Fts5UnicodeCategory(iCode)];
238212 assert( (bToken==0 || bToken==1) )((void) (0));
238213 assert( (bTokenChars==0 || bTokenChars==1) )((void) (0));
238214 if( bToken!=bTokenChars && sqlite3Fts5UnicodeIsdiacritic(iCode)==0 ){
238215 int i;
238216 for(i=0; i<nNew; i++){
238217 if( (u32)aNew[i]>iCode ) break;
238218 }
238219 memmove(&aNew[i+1], &aNew[i], (nNew-i)*sizeof(int));
238220 aNew[i] = iCode;
238221 nNew++;
238222 }
238223 }
238224 }
238225 p->aiException = aNew;
238226 p->nException = nNew;
238227 }else{
238228 rc = SQLITE_NOMEM7;
238229 }
238230 }
238231
238232 return rc;
238233}
238234
238235/*
238236** Return true if the p->aiException[] array contains the value iCode.
238237*/
238238static int fts5UnicodeIsException(Unicode61Tokenizer *p, int iCode){
238239 if( p->nException>0 ){
238240 int *a = p->aiException;
238241 int iLo = 0;
238242 int iHi = p->nException-1;
238243
238244 while( iHi>=iLo ){
238245 int iTest = (iHi + iLo) / 2;
238246 if( iCode==a[iTest] ){
238247 return 1;
238248 }else if( iCode>a[iTest] ){
238249 iLo = iTest+1;
238250 }else{
238251 iHi = iTest-1;
238252 }
238253 }
238254 }
238255
238256 return 0;
238257}
238258
238259/*
238260** Delete a "unicode61" tokenizer.
238261*/
238262static void fts5UnicodeDelete(Fts5Tokenizer *pTok){
238263 if( pTok ){
238264 Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTok;
238265 sqlite3_free(p->aiException);
238266 sqlite3_free(p->aFold);
238267 sqlite3_free(p);
238268 }
238269 return;
238270}
238271
238272static int unicodeSetCategories(Unicode61Tokenizer *p, const char *zCat){
238273 const char *z = zCat;
238274
238275 while( *z ){
238276 while( *z==' ' || *z=='\t' ) z++;
238277 if( *z && sqlite3Fts5UnicodeCatParse(z, p->aCategory) ){
238278 return SQLITE_ERROR1;
238279 }
238280 while( *z!=' ' && *z!='\t' && *z!='\0' ) z++;
238281 }
238282
238283 sqlite3Fts5UnicodeAscii(p->aCategory, p->aTokenChar);
238284 return SQLITE_OK0;
238285}
238286
238287/*
238288** Create a "unicode61" tokenizer.
238289*/
238290static int fts5UnicodeCreate(
238291 void *pUnused,
238292 const char **azArg, int nArg,
238293 Fts5Tokenizer **ppOut
238294){
238295 int rc = SQLITE_OK0; /* Return code */
238296 Unicode61Tokenizer *p = 0; /* New tokenizer object */
238297
238298 UNUSED_PARAM(pUnused)(void)(pUnused);
238299
238300 if( nArg%2 ){
238301 rc = SQLITE_ERROR1;
238302 }else{
238303 p = (Unicode61Tokenizer*)sqlite3_malloc(sizeof(Unicode61Tokenizer));
238304 if( p ){
238305 const char *zCat = "L* N* Co";
238306 int i;
238307 memset(p, 0, sizeof(Unicode61Tokenizer));
238308
238309 p->eRemoveDiacritic = FTS5_REMOVE_DIACRITICS_SIMPLE1;
238310 p->nFold = 64;
238311 p->aFold = sqlite3_malloc64(p->nFold * sizeof(char));
238312 if( p->aFold==0 ){
238313 rc = SQLITE_NOMEM7;
238314 }
238315
238316 /* Search for a "categories" argument */
238317 for(i=0; rc==SQLITE_OK0 && i<nArg; i+=2){
238318 if( 0==sqlite3_stricmp(azArg[i], "categories") ){
238319 zCat = azArg[i+1];
238320 }
238321 }
238322
238323 if( rc==SQLITE_OK0 ){
238324 rc = unicodeSetCategories(p, zCat);
238325 }
238326
238327 for(i=0; rc==SQLITE_OK0 && i<nArg; i+=2){
238328 const char *zArg = azArg[i+1];
238329 if( 0==sqlite3_stricmp(azArg[i], "remove_diacritics") ){
238330 if( (zArg[0]!='0' && zArg[0]!='1' && zArg[0]!='2') || zArg[1] ){
238331 rc = SQLITE_ERROR1;
238332 }else{
238333 p->eRemoveDiacritic = (zArg[0] - '0');
238334 assert( p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_NONE((void) (0))
238335 || p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_SIMPLE((void) (0))
238336 || p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_COMPLEX((void) (0))
238337 )((void) (0));
238338 }
238339 }else
238340 if( 0==sqlite3_stricmp(azArg[i], "tokenchars") ){
238341 rc = fts5UnicodeAddExceptions(p, zArg, 1);
238342 }else
238343 if( 0==sqlite3_stricmp(azArg[i], "separators") ){
238344 rc = fts5UnicodeAddExceptions(p, zArg, 0);
238345 }else
238346 if( 0==sqlite3_stricmp(azArg[i], "categories") ){
238347 /* no-op */
238348 }else{
238349 rc = SQLITE_ERROR1;
238350 }
238351 }
238352
238353 }else{
238354 rc = SQLITE_NOMEM7;
238355 }
238356 if( rc!=SQLITE_OK0 ){
238357 fts5UnicodeDelete((Fts5Tokenizer*)p);
238358 p = 0;
238359 }
238360 *ppOut = (Fts5Tokenizer*)p;
238361 }
238362 return rc;
238363}
238364
238365/*
238366** Return true if, for the purposes of tokenizing with the tokenizer
238367** passed as the first argument, codepoint iCode is considered a token
238368** character (not a separator).
238369*/
238370static int fts5UnicodeIsAlnum(Unicode61Tokenizer *p, int iCode){
238371 return (
238372 p->aCategory[sqlite3Fts5UnicodeCategory((u32)iCode)]
238373 ^ fts5UnicodeIsException(p, iCode)
238374 );
238375}
238376
238377static int fts5UnicodeTokenize(
238378 Fts5Tokenizer *pTokenizer,
238379 void *pCtx,
238380 int iUnused,
238381 const char *pText, int nText,
238382 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
238383){
238384 Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTokenizer;
238385 int rc = SQLITE_OK0;
238386 unsigned char *a = p->aTokenChar;
238387
238388 unsigned char *zTerm = (unsigned char*)&pText[nText];
238389 unsigned char *zCsr = (unsigned char *)pText;
238390
238391 /* Output buffer */
238392 char *aFold = p->aFold;
238393 int nFold = p->nFold;
238394 const char *pEnd = &aFold[nFold-6];
238395
238396 UNUSED_PARAM(iUnused)(void)(iUnused);
238397
238398 /* Each iteration of this loop gobbles up a contiguous run of separators,
238399 ** then the next token. */
238400 while( rc==SQLITE_OK0 ){
238401 u32 iCode; /* non-ASCII codepoint read from input */
238402 char *zOut = aFold;
238403 int is;
238404 int ie;
238405
238406 /* Skip any separator characters. */
238407 while( 1 ){
238408 if( zCsr>=zTerm ) goto tokenize_done;
238409 if( *zCsr & 0x80 ) {
238410 /* A character outside of the ascii range. Skip past it if it is
238411 ** a separator character. Or break out of the loop if it is not. */
238412 is = zCsr - (unsigned char*)pText;
238413 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; } }
;
238414 if( fts5UnicodeIsAlnum(p, iCode) ){
238415 goto non_ascii_tokenchar;
238416 }
238417 }else{
238418 if( a[*zCsr] ){
238419 is = zCsr - (unsigned char*)pText;
238420 goto ascii_tokenchar;
238421 }
238422 zCsr++;
238423 }
238424 }
238425
238426 /* Run through the tokenchars. Fold them into the output buffer along
238427 ** the way. */
238428 while( zCsr<zTerm ){
238429
238430 /* Grow the output buffer so that there is sufficient space to fit the
238431 ** largest possible utf-8 character. */
238432 if( zOut>pEnd ){
238433 aFold = sqlite3_malloc64((sqlite3_int64)nFold*2);
238434 if( aFold==0 ){
238435 rc = SQLITE_NOMEM7;
238436 goto tokenize_done;
238437 }
238438 zOut = &aFold[zOut - p->aFold];
238439 memcpy(aFold, p->aFold, nFold);
238440 sqlite3_free(p->aFold);
238441 p->aFold = aFold;
238442 p->nFold = nFold = nFold*2;
238443 pEnd = &aFold[nFold-6];
238444 }
238445
238446 if( *zCsr & 0x80 ){
238447 /* An non-ascii-range character. Fold it into the output buffer if
238448 ** it is a token character, or break out of the loop if it is not. */
238449 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; } }
;
238450 if( fts5UnicodeIsAlnum(p,iCode)||sqlite3Fts5UnicodeIsdiacritic(iCode) ){
238451 non_ascii_tokenchar:
238452 iCode = sqlite3Fts5UnicodeFold(iCode, p->eRemoveDiacritic);
238453 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); } }
;
238454 }else{
238455 break;
238456 }
238457 }else if( a[*zCsr]==0 ){
238458 /* An ascii-range separator character. End of token. */
238459 break;
238460 }else{
238461 ascii_tokenchar:
238462 if( *zCsr>='A' && *zCsr<='Z' ){
238463 *zOut++ = *zCsr + 32;
238464 }else{
238465 *zOut++ = *zCsr;
238466 }
238467 zCsr++;
238468 }
238469 ie = zCsr - (unsigned char*)pText;
238470 }
238471
238472 /* Invoke the token callback */
238473 rc = xToken(pCtx, 0, aFold, zOut-aFold, is, ie);
238474 }
238475
238476 tokenize_done:
238477 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
238478 return rc;
238479}
238480
238481/**************************************************************************
238482** Start of porter stemmer implementation.
238483*/
238484
238485/* Any tokens larger than this (in bytes) are passed through without
238486** stemming. */
238487#define FTS5_PORTER_MAX_TOKEN64 64
238488
238489typedef struct PorterTokenizer PorterTokenizer;
238490struct PorterTokenizer {
238491 fts5_tokenizer tokenizer; /* Parent tokenizer module */
238492 Fts5Tokenizer *pTokenizer; /* Parent tokenizer instance */
238493 char aBuf[FTS5_PORTER_MAX_TOKEN64 + 64];
238494};
238495
238496/*
238497** Delete a "porter" tokenizer.
238498*/
238499static void fts5PorterDelete(Fts5Tokenizer *pTok){
238500 if( pTok ){
238501 PorterTokenizer *p = (PorterTokenizer*)pTok;
238502 if( p->pTokenizer ){
238503 p->tokenizer.xDelete(p->pTokenizer);
238504 }
238505 sqlite3_free(p);
238506 }
238507}
238508
238509/*
238510** Create a "porter" tokenizer.
238511*/
238512static int fts5PorterCreate(
238513 void *pCtx,
238514 const char **azArg, int nArg,
238515 Fts5Tokenizer **ppOut
238516){
238517 fts5_api *pApi = (fts5_api*)pCtx;
238518 int rc = SQLITE_OK0;
238519 PorterTokenizer *pRet;
238520 void *pUserdata = 0;
238521 const char *zBase = "unicode61";
238522
238523 if( nArg>0 ){
238524 zBase = azArg[0];
238525 }
238526
238527 pRet = (PorterTokenizer*)sqlite3_malloc(sizeof(PorterTokenizer));
238528 if( pRet ){
238529 memset(pRet, 0, sizeof(PorterTokenizer));
238530 rc = pApi->xFindTokenizer(pApi, zBase, &pUserdata, &pRet->tokenizer);
238531 }else{
238532 rc = SQLITE_NOMEM7;
238533 }
238534 if( rc==SQLITE_OK0 ){
238535 int nArg2 = (nArg>0 ? nArg-1 : 0);
238536 const char **azArg2 = (nArg2 ? &azArg[1] : 0);
238537 rc = pRet->tokenizer.xCreate(pUserdata, azArg2, nArg2, &pRet->pTokenizer);
238538 }
238539
238540 if( rc!=SQLITE_OK0 ){
238541 fts5PorterDelete((Fts5Tokenizer*)pRet);
238542 pRet = 0;
238543 }
238544 *ppOut = (Fts5Tokenizer*)pRet;
238545 return rc;
238546}
238547
238548typedef struct PorterContext PorterContext;
238549struct PorterContext {
238550 void *pCtx;
238551 int (*xToken)(void*, int, const char*, int, int, int);
238552 char *aBuf;
238553};
238554
238555typedef struct PorterRule PorterRule;
238556struct PorterRule {
238557 const char *zSuffix;
238558 int nSuffix;
238559 int (*xCond)(char *zStem, int nStem);
238560 const char *zOutput;
238561 int nOutput;
238562};
238563
238564#if 0
238565static int fts5PorterApply(char *aBuf, int *pnBuf, PorterRule *aRule){
238566 int ret = -1;
238567 int nBuf = *pnBuf;
238568 PorterRule *p;
238569
238570 for(p=aRule; p->zSuffix; p++){
238571 assert( strlen(p->zSuffix)==p->nSuffix )((void) (0));
238572 assert( strlen(p->zOutput)==p->nOutput )((void) (0));
238573 if( nBuf<p->nSuffix ) continue;
238574 if( 0==memcmp(&aBuf[nBuf - p->nSuffix], p->zSuffix, p->nSuffix) ) break;
238575 }
238576
238577 if( p->zSuffix ){
238578 int nStem = nBuf - p->nSuffix;
238579 if( p->xCond==0 || p->xCond(aBuf, nStem) ){
238580 memcpy(&aBuf[nStem], p->zOutput, p->nOutput);
238581 *pnBuf = nStem + p->nOutput;
238582 ret = p - aRule;
238583 }
238584 }
238585
238586 return ret;
238587}
238588#endif
238589
238590static int fts5PorterIsVowel(char c, int bYIsVowel){
238591 return (
238592 c=='a' || c=='e' || c=='i' || c=='o' || c=='u' || (bYIsVowel && c=='y')
238593 );
238594}
238595
238596static int fts5PorterGobbleVC(char *zStem, int nStem, int bPrevCons){
238597 int i;
238598 int bCons = bPrevCons;
238599
238600 /* Scan for a vowel */
238601 for(i=0; i<nStem; i++){
238602 if( 0==(bCons = !fts5PorterIsVowel(zStem[i], bCons)) ) break;
238603 }
238604
238605 /* Scan for a consonent */
238606 for(i++; i<nStem; i++){
238607 if( (bCons = !fts5PorterIsVowel(zStem[i], bCons)) ) return i+1;
238608 }
238609 return 0;
238610}
238611
238612/* porter rule condition: (m > 0) */
238613static int fts5Porter_MGt0(char *zStem, int nStem){
238614 return !!fts5PorterGobbleVC(zStem, nStem, 0);
238615}
238616
238617/* porter rule condition: (m > 1) */
238618static int fts5Porter_MGt1(char *zStem, int nStem){
238619 int n;
238620 n = fts5PorterGobbleVC(zStem, nStem, 0);
238621 if( n && fts5PorterGobbleVC(&zStem[n], nStem-n, 1) ){
238622 return 1;
238623 }
238624 return 0;
238625}
238626
238627/* porter rule condition: (m = 1) */
238628static int fts5Porter_MEq1(char *zStem, int nStem){
238629 int n;
238630 n = fts5PorterGobbleVC(zStem, nStem, 0);
238631 if( n && 0==fts5PorterGobbleVC(&zStem[n], nStem-n, 1) ){
238632 return 1;
238633 }
238634 return 0;
238635}
238636
238637/* porter rule condition: (*o) */
238638static int fts5Porter_Ostar(char *zStem, int nStem){
238639 if( zStem[nStem-1]=='w' || zStem[nStem-1]=='x' || zStem[nStem-1]=='y' ){
238640 return 0;
238641 }else{
238642 int i;
238643 int mask = 0;
238644 int bCons = 0;
238645 for(i=0; i<nStem; i++){
238646 bCons = !fts5PorterIsVowel(zStem[i], bCons);
238647 assert( bCons==0 || bCons==1 )((void) (0));
238648 mask = (mask << 1) + bCons;
238649 }
238650 return ((mask & 0x0007)==0x0005);
238651 }
238652}
238653
238654/* porter rule condition: (m > 1 and (*S or *T)) */
238655static int fts5Porter_MGt1_and_S_or_T(char *zStem, int nStem){
238656 assert( nStem>0 )((void) (0));
238657 return (zStem[nStem-1]=='s' || zStem[nStem-1]=='t')
238658 && fts5Porter_MGt1(zStem, nStem);
238659}
238660
238661/* porter rule condition: (*v*) */
238662static int fts5Porter_Vowel(char *zStem, int nStem){
238663 int i;
238664 for(i=0; i<nStem; i++){
238665 if( fts5PorterIsVowel(zStem[i], i>0) ){
238666 return 1;
238667 }
238668 }
238669 return 0;
238670}
238671
238672
238673/**************************************************************************
238674***************************************************************************
238675** GENERATED CODE STARTS HERE (mkportersteps.tcl)
238676*/
238677
238678static int fts5PorterStep4(char *aBuf, int *pnBuf){
238679 int ret = 0;
238680 int nBuf = *pnBuf;
238681 switch( aBuf[nBuf-2] ){
238682
238683 case 'a':
238684 if( nBuf>2 && 0==memcmp("al", &aBuf[nBuf-2], 2) ){
238685 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
238686 *pnBuf = nBuf - 2;
238687 }
238688 }
238689 break;
238690
238691 case 'c':
238692 if( nBuf>4 && 0==memcmp("ance", &aBuf[nBuf-4], 4) ){
238693 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
238694 *pnBuf = nBuf - 4;
238695 }
238696 }else if( nBuf>4 && 0==memcmp("ence", &aBuf[nBuf-4], 4) ){
238697 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
238698 *pnBuf = nBuf - 4;
238699 }
238700 }
238701 break;
238702
238703 case 'e':
238704 if( nBuf>2 && 0==memcmp("er", &aBuf[nBuf-2], 2) ){
238705 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
238706 *pnBuf = nBuf - 2;
238707 }
238708 }
238709 break;
238710
238711 case 'i':
238712 if( nBuf>2 && 0==memcmp("ic", &aBuf[nBuf-2], 2) ){
238713 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
238714 *pnBuf = nBuf - 2;
238715 }
238716 }
238717 break;
238718
238719 case 'l':
238720 if( nBuf>4 && 0==memcmp("able", &aBuf[nBuf-4], 4) ){
238721 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
238722 *pnBuf = nBuf - 4;
238723 }
238724 }else if( nBuf>4 && 0==memcmp("ible", &aBuf[nBuf-4], 4) ){
238725 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
238726 *pnBuf = nBuf - 4;
238727 }
238728 }
238729 break;
238730
238731 case 'n':
238732 if( nBuf>3 && 0==memcmp("ant", &aBuf[nBuf-3], 3) ){
238733 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
238734 *pnBuf = nBuf - 3;
238735 }
238736 }else if( nBuf>5 && 0==memcmp("ement", &aBuf[nBuf-5], 5) ){
238737 if( fts5Porter_MGt1(aBuf, nBuf-5) ){
238738 *pnBuf = nBuf - 5;
238739 }
238740 }else if( nBuf>4 && 0==memcmp("ment", &aBuf[nBuf-4], 4) ){
238741 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
238742 *pnBuf = nBuf - 4;
238743 }
238744 }else if( nBuf>3 && 0==memcmp("ent", &aBuf[nBuf-3], 3) ){
238745 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
238746 *pnBuf = nBuf - 3;
238747 }
238748 }
238749 break;
238750
238751 case 'o':
238752 if( nBuf>3 && 0==memcmp("ion", &aBuf[nBuf-3], 3) ){
238753 if( fts5Porter_MGt1_and_S_or_T(aBuf, nBuf-3) ){
238754 *pnBuf = nBuf - 3;
238755 }
238756 }else if( nBuf>2 && 0==memcmp("ou", &aBuf[nBuf-2], 2) ){
238757 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
238758 *pnBuf = nBuf - 2;
238759 }
238760 }
238761 break;
238762
238763 case 's':
238764 if( nBuf>3 && 0==memcmp("ism", &aBuf[nBuf-3], 3) ){
238765 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
238766 *pnBuf = nBuf - 3;
238767 }
238768 }
238769 break;
238770
238771 case 't':
238772 if( nBuf>3 && 0==memcmp("ate", &aBuf[nBuf-3], 3) ){
238773 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
238774 *pnBuf = nBuf - 3;
238775 }
238776 }else if( nBuf>3 && 0==memcmp("iti", &aBuf[nBuf-3], 3) ){
238777 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
238778 *pnBuf = nBuf - 3;
238779 }
238780 }
238781 break;
238782
238783 case 'u':
238784 if( nBuf>3 && 0==memcmp("ous", &aBuf[nBuf-3], 3) ){
238785 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
238786 *pnBuf = nBuf - 3;
238787 }
238788 }
238789 break;
238790
238791 case 'v':
238792 if( nBuf>3 && 0==memcmp("ive", &aBuf[nBuf-3], 3) ){
238793 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
238794 *pnBuf = nBuf - 3;
238795 }
238796 }
238797 break;
238798
238799 case 'z':
238800 if( nBuf>3 && 0==memcmp("ize", &aBuf[nBuf-3], 3) ){
238801 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
238802 *pnBuf = nBuf - 3;
238803 }
238804 }
238805 break;
238806
238807 }
238808 return ret;
238809}
238810
238811
238812static int fts5PorterStep1B2(char *aBuf, int *pnBuf){
238813 int ret = 0;
238814 int nBuf = *pnBuf;
238815 switch( aBuf[nBuf-2] ){
238816
238817 case 'a':
238818 if( nBuf>2 && 0==memcmp("at", &aBuf[nBuf-2], 2) ){
238819 memcpy(&aBuf[nBuf-2], "ate", 3);
238820 *pnBuf = nBuf - 2 + 3;
238821 ret = 1;
238822 }
238823 break;
238824
238825 case 'b':
238826 if( nBuf>2 && 0==memcmp("bl", &aBuf[nBuf-2], 2) ){
238827 memcpy(&aBuf[nBuf-2], "ble", 3);
238828 *pnBuf = nBuf - 2 + 3;
238829 ret = 1;
238830 }
238831 break;
238832
238833 case 'i':
238834 if( nBuf>2 && 0==memcmp("iz", &aBuf[nBuf-2], 2) ){
238835 memcpy(&aBuf[nBuf-2], "ize", 3);
238836 *pnBuf = nBuf - 2 + 3;
238837 ret = 1;
238838 }
238839 break;
238840
238841 }
238842 return ret;
238843}
238844
238845
238846static int fts5PorterStep2(char *aBuf, int *pnBuf){
238847 int ret = 0;
238848 int nBuf = *pnBuf;
238849 switch( aBuf[nBuf-2] ){
238850
238851 case 'a':
238852 if( nBuf>7 && 0==memcmp("ational", &aBuf[nBuf-7], 7) ){
238853 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
238854 memcpy(&aBuf[nBuf-7], "ate", 3);
238855 *pnBuf = nBuf - 7 + 3;
238856 }
238857 }else if( nBuf>6 && 0==memcmp("tional", &aBuf[nBuf-6], 6) ){
238858 if( fts5Porter_MGt0(aBuf, nBuf-6) ){
238859 memcpy(&aBuf[nBuf-6], "tion", 4);
238860 *pnBuf = nBuf - 6 + 4;
238861 }
238862 }
238863 break;
238864
238865 case 'c':
238866 if( nBuf>4 && 0==memcmp("enci", &aBuf[nBuf-4], 4) ){
238867 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
238868 memcpy(&aBuf[nBuf-4], "ence", 4);
238869 *pnBuf = nBuf - 4 + 4;
238870 }
238871 }else if( nBuf>4 && 0==memcmp("anci", &aBuf[nBuf-4], 4) ){
238872 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
238873 memcpy(&aBuf[nBuf-4], "ance", 4);
238874 *pnBuf = nBuf - 4 + 4;
238875 }
238876 }
238877 break;
238878
238879 case 'e':
238880 if( nBuf>4 && 0==memcmp("izer", &aBuf[nBuf-4], 4) ){
238881 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
238882 memcpy(&aBuf[nBuf-4], "ize", 3);
238883 *pnBuf = nBuf - 4 + 3;
238884 }
238885 }
238886 break;
238887
238888 case 'g':
238889 if( nBuf>4 && 0==memcmp("logi", &aBuf[nBuf-4], 4) ){
238890 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
238891 memcpy(&aBuf[nBuf-4], "log", 3);
238892 *pnBuf = nBuf - 4 + 3;
238893 }
238894 }
238895 break;
238896
238897 case 'l':
238898 if( nBuf>3 && 0==memcmp("bli", &aBuf[nBuf-3], 3) ){
238899 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
238900 memcpy(&aBuf[nBuf-3], "ble", 3);
238901 *pnBuf = nBuf - 3 + 3;
238902 }
238903 }else if( nBuf>4 && 0==memcmp("alli", &aBuf[nBuf-4], 4) ){
238904 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
238905 memcpy(&aBuf[nBuf-4], "al", 2);
238906 *pnBuf = nBuf - 4 + 2;
238907 }
238908 }else if( nBuf>5 && 0==memcmp("entli", &aBuf[nBuf-5], 5) ){
238909 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
238910 memcpy(&aBuf[nBuf-5], "ent", 3);
238911 *pnBuf = nBuf - 5 + 3;
238912 }
238913 }else if( nBuf>3 && 0==memcmp("eli", &aBuf[nBuf-3], 3) ){
238914 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
238915 memcpy(&aBuf[nBuf-3], "e", 1);
238916 *pnBuf = nBuf - 3 + 1;
238917 }
238918 }else if( nBuf>5 && 0==memcmp("ousli", &aBuf[nBuf-5], 5) ){
238919 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
238920 memcpy(&aBuf[nBuf-5], "ous", 3);
238921 *pnBuf = nBuf - 5 + 3;
238922 }
238923 }
238924 break;
238925
238926 case 'o':
238927 if( nBuf>7 && 0==memcmp("ization", &aBuf[nBuf-7], 7) ){
238928 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
238929 memcpy(&aBuf[nBuf-7], "ize", 3);
238930 *pnBuf = nBuf - 7 + 3;
238931 }
238932 }else if( nBuf>5 && 0==memcmp("ation", &aBuf[nBuf-5], 5) ){
238933 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
238934 memcpy(&aBuf[nBuf-5], "ate", 3);
238935 *pnBuf = nBuf - 5 + 3;
238936 }
238937 }else if( nBuf>4 && 0==memcmp("ator", &aBuf[nBuf-4], 4) ){
238938 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
238939 memcpy(&aBuf[nBuf-4], "ate", 3);
238940 *pnBuf = nBuf - 4 + 3;
238941 }
238942 }
238943 break;
238944
238945 case 's':
238946 if( nBuf>5 && 0==memcmp("alism", &aBuf[nBuf-5], 5) ){
238947 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
238948 memcpy(&aBuf[nBuf-5], "al", 2);
238949 *pnBuf = nBuf - 5 + 2;
238950 }
238951 }else if( nBuf>7 && 0==memcmp("iveness", &aBuf[nBuf-7], 7) ){
238952 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
238953 memcpy(&aBuf[nBuf-7], "ive", 3);
238954 *pnBuf = nBuf - 7 + 3;
238955 }
238956 }else if( nBuf>7 && 0==memcmp("fulness", &aBuf[nBuf-7], 7) ){
238957 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
238958 memcpy(&aBuf[nBuf-7], "ful", 3);
238959 *pnBuf = nBuf - 7 + 3;
238960 }
238961 }else if( nBuf>7 && 0==memcmp("ousness", &aBuf[nBuf-7], 7) ){
238962 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
238963 memcpy(&aBuf[nBuf-7], "ous", 3);
238964 *pnBuf = nBuf - 7 + 3;
238965 }
238966 }
238967 break;
238968
238969 case 't':
238970 if( nBuf>5 && 0==memcmp("aliti", &aBuf[nBuf-5], 5) ){
238971 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
238972 memcpy(&aBuf[nBuf-5], "al", 2);
238973 *pnBuf = nBuf - 5 + 2;
238974 }
238975 }else if( nBuf>5 && 0==memcmp("iviti", &aBuf[nBuf-5], 5) ){
238976 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
238977 memcpy(&aBuf[nBuf-5], "ive", 3);
238978 *pnBuf = nBuf - 5 + 3;
238979 }
238980 }else if( nBuf>6 && 0==memcmp("biliti", &aBuf[nBuf-6], 6) ){
238981 if( fts5Porter_MGt0(aBuf, nBuf-6) ){
238982 memcpy(&aBuf[nBuf-6], "ble", 3);
238983 *pnBuf = nBuf - 6 + 3;
238984 }
238985 }
238986 break;
238987
238988 }
238989 return ret;
238990}
238991
238992
238993static int fts5PorterStep3(char *aBuf, int *pnBuf){
238994 int ret = 0;
238995 int nBuf = *pnBuf;
238996 switch( aBuf[nBuf-2] ){
238997
238998 case 'a':
238999 if( nBuf>4 && 0==memcmp("ical", &aBuf[nBuf-4], 4) ){
239000 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
239001 memcpy(&aBuf[nBuf-4], "ic", 2);
239002 *pnBuf = nBuf - 4 + 2;
239003 }
239004 }
239005 break;
239006
239007 case 's':
239008 if( nBuf>4 && 0==memcmp("ness", &aBuf[nBuf-4], 4) ){
239009 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
239010 *pnBuf = nBuf - 4;
239011 }
239012 }
239013 break;
239014
239015 case 't':
239016 if( nBuf>5 && 0==memcmp("icate", &aBuf[nBuf-5], 5) ){
239017 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
239018 memcpy(&aBuf[nBuf-5], "ic", 2);
239019 *pnBuf = nBuf - 5 + 2;
239020 }
239021 }else if( nBuf>5 && 0==memcmp("iciti", &aBuf[nBuf-5], 5) ){
239022 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
239023 memcpy(&aBuf[nBuf-5], "ic", 2);
239024 *pnBuf = nBuf - 5 + 2;
239025 }
239026 }
239027 break;
239028
239029 case 'u':
239030 if( nBuf>3 && 0==memcmp("ful", &aBuf[nBuf-3], 3) ){
239031 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
239032 *pnBuf = nBuf - 3;
239033 }
239034 }
239035 break;
239036
239037 case 'v':
239038 if( nBuf>5 && 0==memcmp("ative", &aBuf[nBuf-5], 5) ){
239039 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
239040 *pnBuf = nBuf - 5;
239041 }
239042 }
239043 break;
239044
239045 case 'z':
239046 if( nBuf>5 && 0==memcmp("alize", &aBuf[nBuf-5], 5) ){
239047 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
239048 memcpy(&aBuf[nBuf-5], "al", 2);
239049 *pnBuf = nBuf - 5 + 2;
239050 }
239051 }
239052 break;
239053
239054 }
239055 return ret;
239056}
239057
239058
239059static int fts5PorterStep1B(char *aBuf, int *pnBuf){
239060 int ret = 0;
239061 int nBuf = *pnBuf;
239062 switch( aBuf[nBuf-2] ){
239063
239064 case 'e':
239065 if( nBuf>3 && 0==memcmp("eed", &aBuf[nBuf-3], 3) ){
239066 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
239067 memcpy(&aBuf[nBuf-3], "ee", 2);
239068 *pnBuf = nBuf - 3 + 2;
239069 }
239070 }else if( nBuf>2 && 0==memcmp("ed", &aBuf[nBuf-2], 2) ){
239071 if( fts5Porter_Vowel(aBuf, nBuf-2) ){
239072 *pnBuf = nBuf - 2;
239073 ret = 1;
239074 }
239075 }
239076 break;
239077
239078 case 'n':
239079 if( nBuf>3 && 0==memcmp("ing", &aBuf[nBuf-3], 3) ){
239080 if( fts5Porter_Vowel(aBuf, nBuf-3) ){
239081 *pnBuf = nBuf - 3;
239082 ret = 1;
239083 }
239084 }
239085 break;
239086
239087 }
239088 return ret;
239089}
239090
239091/*
239092** GENERATED CODE ENDS HERE (mkportersteps.tcl)
239093***************************************************************************
239094**************************************************************************/
239095
239096static void fts5PorterStep1A(char *aBuf, int *pnBuf){
239097 int nBuf = *pnBuf;
239098 if( aBuf[nBuf-1]=='s' ){
239099 if( aBuf[nBuf-2]=='e' ){
239100 if( (nBuf>4 && aBuf[nBuf-4]=='s' && aBuf[nBuf-3]=='s')
239101 || (nBuf>3 && aBuf[nBuf-3]=='i' )
239102 ){
239103 *pnBuf = nBuf-2;
239104 }else{
239105 *pnBuf = nBuf-1;
239106 }
239107 }
239108 else if( aBuf[nBuf-2]!='s' ){
239109 *pnBuf = nBuf-1;
239110 }
239111 }
239112}
239113
239114static int fts5PorterCb(
239115 void *pCtx,
239116 int tflags,
239117 const char *pToken,
239118 int nToken,
239119 int iStart,
239120 int iEnd
239121){
239122 PorterContext *p = (PorterContext*)pCtx;
239123
239124 char *aBuf;
239125 int nBuf;
239126
239127 if( nToken>FTS5_PORTER_MAX_TOKEN64 || nToken<3 ) goto pass_through;
239128 aBuf = p->aBuf;
239129 nBuf = nToken;
239130 memcpy(aBuf, pToken, nBuf);
239131
239132 /* Step 1. */
239133 fts5PorterStep1A(aBuf, &nBuf);
239134 if( fts5PorterStep1B(aBuf, &nBuf) ){
239135 if( fts5PorterStep1B2(aBuf, &nBuf)==0 ){
239136 char c = aBuf[nBuf-1];
239137 if( fts5PorterIsVowel(c, 0)==0
239138 && c!='l' && c!='s' && c!='z' && c==aBuf[nBuf-2]
239139 ){
239140 nBuf--;
239141 }else if( fts5Porter_MEq1(aBuf, nBuf) && fts5Porter_Ostar(aBuf, nBuf) ){
239142 aBuf[nBuf++] = 'e';
239143 }
239144 }
239145 }
239146
239147 /* Step 1C. */
239148 if( aBuf[nBuf-1]=='y' && fts5Porter_Vowel(aBuf, nBuf-1) ){
239149 aBuf[nBuf-1] = 'i';
239150 }
239151
239152 /* Steps 2 through 4. */
239153 fts5PorterStep2(aBuf, &nBuf);
239154 fts5PorterStep3(aBuf, &nBuf);
239155 fts5PorterStep4(aBuf, &nBuf);
239156
239157 /* Step 5a. */
239158 assert( nBuf>0 )((void) (0));
239159 if( aBuf[nBuf-1]=='e' ){
239160 if( fts5Porter_MGt1(aBuf, nBuf-1)
239161 || (fts5Porter_MEq1(aBuf, nBuf-1) && !fts5Porter_Ostar(aBuf, nBuf-1))
239162 ){
239163 nBuf--;
239164 }
239165 }
239166
239167 /* Step 5b. */
239168 if( nBuf>1 && aBuf[nBuf-1]=='l'
239169 && aBuf[nBuf-2]=='l' && fts5Porter_MGt1(aBuf, nBuf-1)
239170 ){
239171 nBuf--;
239172 }
239173
239174 return p->xToken(p->pCtx, tflags, aBuf, nBuf, iStart, iEnd);
239175
239176 pass_through:
239177 return p->xToken(p->pCtx, tflags, pToken, nToken, iStart, iEnd);
239178}
239179
239180/*
239181** Tokenize using the porter tokenizer.
239182*/
239183static int fts5PorterTokenize(
239184 Fts5Tokenizer *pTokenizer,
239185 void *pCtx,
239186 int flags,
239187 const char *pText, int nText,
239188 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
239189){
239190 PorterTokenizer *p = (PorterTokenizer*)pTokenizer;
239191 PorterContext sCtx;
239192 sCtx.xToken = xToken;
239193 sCtx.pCtx = pCtx;
239194 sCtx.aBuf = p->aBuf;
239195 return p->tokenizer.xTokenize(
239196 p->pTokenizer, (void*)&sCtx, flags, pText, nText, fts5PorterCb
239197 );
239198}
239199
239200/**************************************************************************
239201** Start of trigram implementation.
239202*/
239203typedef struct TrigramTokenizer TrigramTokenizer;
239204struct TrigramTokenizer {
239205 int bFold; /* True to fold to lower-case */
239206};
239207
239208/*
239209** Free a trigram tokenizer.
239210*/
239211static void fts5TriDelete(Fts5Tokenizer *p){
239212 sqlite3_free(p);
239213}
239214
239215/*
239216** Allocate a trigram tokenizer.
239217*/
239218static int fts5TriCreate(
239219 void *pUnused,
239220 const char **azArg,
239221 int nArg,
239222 Fts5Tokenizer **ppOut
239223){
239224 int rc = SQLITE_OK0;
239225 TrigramTokenizer *pNew = (TrigramTokenizer*)sqlite3_malloc(sizeof(*pNew));
239226 UNUSED_PARAM(pUnused)(void)(pUnused);
239227 if( pNew==0 ){
239228 rc = SQLITE_NOMEM7;
239229 }else{
239230 int i;
239231 pNew->bFold = 1;
239232 for(i=0; rc==SQLITE_OK0 && i<nArg; i+=2){
239233 const char *zArg = azArg[i+1];
239234 if( 0==sqlite3_stricmp(azArg[i], "case_sensitive") ){
239235 if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1] ){
239236 rc = SQLITE_ERROR1;
239237 }else{
239238 pNew->bFold = (zArg[0]=='0');
239239 }
239240 }else{
239241 rc = SQLITE_ERROR1;
239242 }
239243 }
239244 if( rc!=SQLITE_OK0 ){
239245 fts5TriDelete((Fts5Tokenizer*)pNew);
239246 pNew = 0;
239247 }
239248 }
239249 *ppOut = (Fts5Tokenizer*)pNew;
239250 return rc;
239251}
239252
239253/*
239254** Trigram tokenizer tokenize routine.
239255*/
239256static int fts5TriTokenize(
239257 Fts5Tokenizer *pTok,
239258 void *pCtx,
239259 int unusedFlags,
239260 const char *pText, int nText,
239261 int (*xToken)(void*, int, const char*, int, int, int)
239262){
239263 TrigramTokenizer *p = (TrigramTokenizer*)pTok;
239264 int rc = SQLITE_OK0;
239265 char aBuf[32];
239266 const unsigned char *zIn = (const unsigned char*)pText;
239267 const unsigned char *zEof = &zIn[nText];
239268 u32 iCode;
239269
239270 UNUSED_PARAM(unusedFlags)(void)(unusedFlags);
239271 while( 1 ){
239272 char *zOut = aBuf;
239273 int iStart = zIn - (const unsigned char*)pText;
239274 const unsigned char *zNext;
239275
239276 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; } }
;
239277 if( iCode==0 ) break;
239278 zNext = zIn;
239279 if( zIn<zEof ){
239280 if( p->bFold ) iCode = sqlite3Fts5UnicodeFold(iCode, 0);
239281 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); } }
;
239282 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; } }
;
239283 if( iCode==0 ) break;
239284 }else{
239285 break;
239286 }
239287 if( zIn<zEof ){
239288 if( p->bFold ) iCode = sqlite3Fts5UnicodeFold(iCode, 0);
239289 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); } }
;
239290 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; } }
;
239291 if( iCode==0 ) break;
239292 if( p->bFold ) iCode = sqlite3Fts5UnicodeFold(iCode, 0);
239293 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); } }
;
239294 }else{
239295 break;
239296 }
239297 rc = xToken(pCtx, 0, aBuf, zOut-aBuf, iStart, iStart + zOut-aBuf);
239298 if( rc!=SQLITE_OK0 ) break;
239299 zIn = zNext;
239300 }
239301
239302 return rc;
239303}
239304
239305/*
239306** Argument xCreate is a pointer to a constructor function for a tokenizer.
239307** pTok is a tokenizer previously created using the same method. This function
239308** returns one of FTS5_PATTERN_NONE, FTS5_PATTERN_LIKE or FTS5_PATTERN_GLOB
239309** indicating the style of pattern matching that the tokenizer can support.
239310** In practice, this is:
239311**
239312** "trigram" tokenizer, case_sensitive=1 - FTS5_PATTERN_GLOB
239313** "trigram" tokenizer, case_sensitive=0 (the default) - FTS5_PATTERN_LIKE
239314** all other tokenizers - FTS5_PATTERN_NONE
239315*/
239316static int sqlite3Fts5TokenizerPattern(
239317 int (*xCreate)(void*, const char**, int, Fts5Tokenizer**),
239318 Fts5Tokenizer *pTok
239319){
239320 if( xCreate==fts5TriCreate ){
239321 TrigramTokenizer *p = (TrigramTokenizer*)pTok;
239322 return p->bFold ? FTS5_PATTERN_LIKE65 : FTS5_PATTERN_GLOB66;
239323 }
239324 return FTS5_PATTERN_NONE0;
239325}
239326
239327/*
239328** Register all built-in tokenizers with FTS5.
239329*/
239330static int sqlite3Fts5TokenizerInit(fts5_api *pApi){
239331 struct BuiltinTokenizer {
239332 const char *zName;
239333 fts5_tokenizer x;
239334 } aBuiltin[] = {
239335 { "unicode61", {fts5UnicodeCreate, fts5UnicodeDelete, fts5UnicodeTokenize}},
239336 { "ascii", {fts5AsciiCreate, fts5AsciiDelete, fts5AsciiTokenize }},
239337 { "porter", {fts5PorterCreate, fts5PorterDelete, fts5PorterTokenize }},
239338 { "trigram", {fts5TriCreate, fts5TriDelete, fts5TriTokenize}},
239339 };
239340
239341 int rc = SQLITE_OK0; /* Return code */
239342 int i; /* To iterate through builtin functions */
239343
239344 for(i=0; rc==SQLITE_OK0 && i<ArraySize(aBuiltin)((int)(sizeof(aBuiltin)/sizeof(aBuiltin[0]))); i++){
239345 rc = pApi->xCreateTokenizer(pApi,
239346 aBuiltin[i].zName,
239347 (void*)pApi,
239348 &aBuiltin[i].x,
239349 0
239350 );
239351 }
239352
239353 return rc;
239354}
239355
239356/*
239357** 2012-05-25
239358**
239359** The author disclaims copyright to this source code. In place of
239360** a legal notice, here is a blessing:
239361**
239362** May you do good and not evil.
239363** May you find forgiveness for yourself and forgive others.
239364** May you share freely, never taking more than you give.
239365**
239366******************************************************************************
239367*/
239368
239369/*
239370** DO NOT EDIT THIS MACHINE GENERATED FILE.
239371*/
239372
239373
239374/* #include <assert.h> */
239375
239376
239377
239378/*
239379** If the argument is a codepoint corresponding to a lowercase letter
239380** in the ASCII range with a diacritic added, return the codepoint
239381** of the ASCII letter only. For example, if passed 235 - "LATIN
239382** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
239383** E"). The resuls of passing a codepoint that corresponds to an
239384** uppercase letter are undefined.
239385*/
239386static int fts5_remove_diacritic(int c, int bComplex){
239387 unsigned short aDia[] = {
239388 0, 1797, 1848, 1859, 1891, 1928, 1940, 1995,
239389 2024, 2040, 2060, 2110, 2168, 2206, 2264, 2286,
239390 2344, 2383, 2472, 2488, 2516, 2596, 2668, 2732,
239391 2782, 2842, 2894, 2954, 2984, 3000, 3028, 3336,
239392 3456, 3696, 3712, 3728, 3744, 3766, 3832, 3896,
239393 3912, 3928, 3944, 3968, 4008, 4040, 4056, 4106,
239394 4138, 4170, 4202, 4234, 4266, 4296, 4312, 4344,
239395 4408, 4424, 4442, 4472, 4488, 4504, 6148, 6198,
239396 6264, 6280, 6360, 6429, 6505, 6529, 61448, 61468,
239397 61512, 61534, 61592, 61610, 61642, 61672, 61688, 61704,
239398 61726, 61784, 61800, 61816, 61836, 61880, 61896, 61914,
239399 61948, 61998, 62062, 62122, 62154, 62184, 62200, 62218,
239400 62252, 62302, 62364, 62410, 62442, 62478, 62536, 62554,
239401 62584, 62604, 62640, 62648, 62656, 62664, 62730, 62766,
239402 62830, 62890, 62924, 62974, 63032, 63050, 63082, 63118,
239403 63182, 63242, 63274, 63310, 63368, 63390,
239404 };
239405#define HIBIT((unsigned char)0x80) ((unsigned char)0x80)
239406 unsigned char aChar[] = {
239407 '\0', 'a', 'c', 'e', 'i', 'n',
239408 'o', 'u', 'y', 'y', 'a', 'c',
239409 'd', 'e', 'e', 'g', 'h', 'i',
239410 'j', 'k', 'l', 'n', 'o', 'r',
239411 's', 't', 'u', 'u', 'w', 'y',
239412 'z', 'o', 'u', 'a', 'i', 'o',
239413 'u', 'u'|HIBIT((unsigned char)0x80), 'a'|HIBIT((unsigned char)0x80), 'g', 'k', 'o',
239414 'o'|HIBIT((unsigned char)0x80), 'j', 'g', 'n', 'a'|HIBIT((unsigned char)0x80), 'a',
239415 'e', 'i', 'o', 'r', 'u', 's',
239416 't', 'h', 'a', 'e', 'o'|HIBIT((unsigned char)0x80), 'o',
239417 'o'|HIBIT((unsigned char)0x80), 'y', '\0', '\0', '\0', '\0',
239418 '\0', '\0', '\0', '\0', 'a', 'b',
239419 'c'|HIBIT((unsigned char)0x80), 'd', 'd', 'e'|HIBIT((unsigned char)0x80), 'e', 'e'|HIBIT((unsigned char)0x80),
239420 'f', 'g', 'h', 'h', 'i', 'i'|HIBIT((unsigned char)0x80),
239421 'k', 'l', 'l'|HIBIT((unsigned char)0x80), 'l', 'm', 'n',
239422 'o'|HIBIT((unsigned char)0x80), 'p', 'r', 'r'|HIBIT((unsigned char)0x80), 'r', 's',
239423 's'|HIBIT((unsigned char)0x80), 't', 'u', 'u'|HIBIT((unsigned char)0x80), 'v', 'w',
239424 'w', 'x', 'y', 'z', 'h', 't',
239425 'w', 'y', 'a', 'a'|HIBIT((unsigned char)0x80), 'a'|HIBIT((unsigned char)0x80), 'a'|HIBIT((unsigned char)0x80),
239426 'e', 'e'|HIBIT((unsigned char)0x80), 'e'|HIBIT((unsigned char)0x80), 'i', 'o', 'o'|HIBIT((unsigned char)0x80),
239427 'o'|HIBIT((unsigned char)0x80), 'o'|HIBIT((unsigned char)0x80), 'u', 'u'|HIBIT((unsigned char)0x80), 'u'|HIBIT((unsigned char)0x80), 'y',
239428 };
239429
239430 unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
239431 int iRes = 0;
239432 int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
239433 int iLo = 0;
239434 while( iHi>=iLo ){
239435 int iTest = (iHi + iLo) / 2;
239436 if( key >= aDia[iTest] ){
239437 iRes = iTest;
239438 iLo = iTest+1;
239439 }else{
239440 iHi = iTest-1;
239441 }
239442 }
239443 assert( key>=aDia[iRes] )((void) (0));
239444 if( bComplex==0 && (aChar[iRes] & 0x80) ) return c;
239445 return (c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : ((int)aChar[iRes] & 0x7F);
239446}
239447
239448
239449/*
239450** Return true if the argument interpreted as a unicode codepoint
239451** is a diacritical modifier character.
239452*/
239453static int sqlite3Fts5UnicodeIsdiacritic(int c){
239454 unsigned int mask0 = 0x08029FDF;
239455 unsigned int mask1 = 0x000361F8;
239456 if( c<768 || c>817 ) return 0;
239457 return (c < 768+32) ?
239458 (mask0 & ((unsigned int)1 << (c-768))) :
239459 (mask1 & ((unsigned int)1 << (c-768-32)));
239460}
239461
239462
239463/*
239464** Interpret the argument as a unicode codepoint. If the codepoint
239465** is an upper case character that has a lower case equivalent,
239466** return the codepoint corresponding to the lower case version.
239467** Otherwise, return a copy of the argument.
239468**
239469** The results are undefined if the value passed to this function
239470** is less than zero.
239471*/
239472static int sqlite3Fts5UnicodeFold(int c, int eRemoveDiacritic){
239473 /* Each entry in the following array defines a rule for folding a range
239474 ** of codepoints to lower case. The rule applies to a range of nRange
239475 ** codepoints starting at codepoint iCode.
239476 **
239477 ** If the least significant bit in flags is clear, then the rule applies
239478 ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
239479 ** need to be folded). Or, if it is set, then the rule only applies to
239480 ** every second codepoint in the range, starting with codepoint C.
239481 **
239482 ** The 7 most significant bits in flags are an index into the aiOff[]
239483 ** array. If a specific codepoint C does require folding, then its lower
239484 ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
239485 **
239486 ** The contents of this array are generated by parsing the CaseFolding.txt
239487 ** file distributed as part of the "Unicode Character Database". See
239488 ** http://www.unicode.org for details.
239489 */
239490 static const struct TableEntry {
239491 unsigned short iCode;
239492 unsigned char flags;
239493 unsigned char nRange;
239494 } aEntry[] = {
239495 {65, 14, 26}, {181, 64, 1}, {192, 14, 23},
239496 {216, 14, 7}, {256, 1, 48}, {306, 1, 6},
239497 {313, 1, 16}, {330, 1, 46}, {376, 116, 1},
239498 {377, 1, 6}, {383, 104, 1}, {385, 50, 1},
239499 {386, 1, 4}, {390, 44, 1}, {391, 0, 1},
239500 {393, 42, 2}, {395, 0, 1}, {398, 32, 1},
239501 {399, 38, 1}, {400, 40, 1}, {401, 0, 1},
239502 {403, 42, 1}, {404, 46, 1}, {406, 52, 1},
239503 {407, 48, 1}, {408, 0, 1}, {412, 52, 1},
239504 {413, 54, 1}, {415, 56, 1}, {416, 1, 6},
239505 {422, 60, 1}, {423, 0, 1}, {425, 60, 1},
239506 {428, 0, 1}, {430, 60, 1}, {431, 0, 1},
239507 {433, 58, 2}, {435, 1, 4}, {439, 62, 1},
239508 {440, 0, 1}, {444, 0, 1}, {452, 2, 1},
239509 {453, 0, 1}, {455, 2, 1}, {456, 0, 1},
239510 {458, 2, 1}, {459, 1, 18}, {478, 1, 18},
239511 {497, 2, 1}, {498, 1, 4}, {502, 122, 1},
239512 {503, 134, 1}, {504, 1, 40}, {544, 110, 1},
239513 {546, 1, 18}, {570, 70, 1}, {571, 0, 1},
239514 {573, 108, 1}, {574, 68, 1}, {577, 0, 1},
239515 {579, 106, 1}, {580, 28, 1}, {581, 30, 1},
239516 {582, 1, 10}, {837, 36, 1}, {880, 1, 4},
239517 {886, 0, 1}, {902, 18, 1}, {904, 16, 3},
239518 {908, 26, 1}, {910, 24, 2}, {913, 14, 17},
239519 {931, 14, 9}, {962, 0, 1}, {975, 4, 1},
239520 {976, 140, 1}, {977, 142, 1}, {981, 146, 1},
239521 {982, 144, 1}, {984, 1, 24}, {1008, 136, 1},
239522 {1009, 138, 1}, {1012, 130, 1}, {1013, 128, 1},
239523 {1015, 0, 1}, {1017, 152, 1}, {1018, 0, 1},
239524 {1021, 110, 3}, {1024, 34, 16}, {1040, 14, 32},
239525 {1120, 1, 34}, {1162, 1, 54}, {1216, 6, 1},
239526 {1217, 1, 14}, {1232, 1, 88}, {1329, 22, 38},
239527 {4256, 66, 38}, {4295, 66, 1}, {4301, 66, 1},
239528 {7680, 1, 150}, {7835, 132, 1}, {7838, 96, 1},
239529 {7840, 1, 96}, {7944, 150, 8}, {7960, 150, 6},
239530 {7976, 150, 8}, {7992, 150, 8}, {8008, 150, 6},
239531 {8025, 151, 8}, {8040, 150, 8}, {8072, 150, 8},
239532 {8088, 150, 8}, {8104, 150, 8}, {8120, 150, 2},
239533 {8122, 126, 2}, {8124, 148, 1}, {8126, 100, 1},
239534 {8136, 124, 4}, {8140, 148, 1}, {8152, 150, 2},
239535 {8154, 120, 2}, {8168, 150, 2}, {8170, 118, 2},
239536 {8172, 152, 1}, {8184, 112, 2}, {8186, 114, 2},
239537 {8188, 148, 1}, {8486, 98, 1}, {8490, 92, 1},
239538 {8491, 94, 1}, {8498, 12, 1}, {8544, 8, 16},
239539 {8579, 0, 1}, {9398, 10, 26}, {11264, 22, 47},
239540 {11360, 0, 1}, {11362, 88, 1}, {11363, 102, 1},
239541 {11364, 90, 1}, {11367, 1, 6}, {11373, 84, 1},
239542 {11374, 86, 1}, {11375, 80, 1}, {11376, 82, 1},
239543 {11378, 0, 1}, {11381, 0, 1}, {11390, 78, 2},
239544 {11392, 1, 100}, {11499, 1, 4}, {11506, 0, 1},
239545 {42560, 1, 46}, {42624, 1, 24}, {42786, 1, 14},
239546 {42802, 1, 62}, {42873, 1, 4}, {42877, 76, 1},
239547 {42878, 1, 10}, {42891, 0, 1}, {42893, 74, 1},
239548 {42896, 1, 4}, {42912, 1, 10}, {42922, 72, 1},
239549 {65313, 14, 26},
239550 };
239551 static const unsigned short aiOff[] = {
239552 1, 2, 8, 15, 16, 26, 28, 32,
239553 37, 38, 40, 48, 63, 64, 69, 71,
239554 79, 80, 116, 202, 203, 205, 206, 207,
239555 209, 210, 211, 213, 214, 217, 218, 219,
239556 775, 7264, 10792, 10795, 23228, 23256, 30204, 54721,
239557 54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274,
239558 57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406,
239559 65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462,
239560 65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511,
239561 65514, 65521, 65527, 65528, 65529,
239562 };
239563
239564 int ret = c;
239565
239566 assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 )((void) (0));
239567
239568 if( c<128 ){
239569 if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
239570 }else if( c<65536 ){
239571 const struct TableEntry *p;
239572 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
239573 int iLo = 0;
239574 int iRes = -1;
239575
239576 assert( c>aEntry[0].iCode )((void) (0));
239577 while( iHi>=iLo ){
239578 int iTest = (iHi + iLo) / 2;
239579 int cmp = (c - aEntry[iTest].iCode);
239580 if( cmp>=0 ){
239581 iRes = iTest;
239582 iLo = iTest+1;
239583 }else{
239584 iHi = iTest-1;
239585 }
239586 }
239587
239588 assert( iRes>=0 && c>=aEntry[iRes].iCode )((void) (0));
239589 p = &aEntry[iRes];
239590 if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
239591 ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
239592 assert( ret>0 )((void) (0));
239593 }
239594
239595 if( eRemoveDiacritic ){
239596 ret = fts5_remove_diacritic(ret, eRemoveDiacritic==2);
239597 }
239598 }
239599
239600 else if( c>=66560 && c<66600 ){
239601 ret = c + 40;
239602 }
239603
239604 return ret;
239605}
239606
239607
239608static int sqlite3Fts5UnicodeCatParse(const char *zCat, u8 *aArray){
239609 aArray[0] = 1;
239610 switch( zCat[0] ){
239611 case 'C':
239612 switch( zCat[1] ){
239613 case 'c': aArray[1] = 1; break;
239614 case 'f': aArray[2] = 1; break;
239615 case 'n': aArray[3] = 1; break;
239616 case 's': aArray[4] = 1; break;
239617 case 'o': aArray[31] = 1; break;
239618 case '*':
239619 aArray[1] = 1;
239620 aArray[2] = 1;
239621 aArray[3] = 1;
239622 aArray[4] = 1;
239623 aArray[31] = 1;
239624 break;
239625 default: return 1; }
239626 break;
239627
239628 case 'L':
239629 switch( zCat[1] ){
239630 case 'l': aArray[5] = 1; break;
239631 case 'm': aArray[6] = 1; break;
239632 case 'o': aArray[7] = 1; break;
239633 case 't': aArray[8] = 1; break;
239634 case 'u': aArray[9] = 1; break;
239635 case 'C': aArray[30] = 1; break;
239636 case '*':
239637 aArray[5] = 1;
239638 aArray[6] = 1;
239639 aArray[7] = 1;
239640 aArray[8] = 1;
239641 aArray[9] = 1;
239642 aArray[30] = 1;
239643 break;
239644 default: return 1; }
239645 break;
239646
239647 case 'M':
239648 switch( zCat[1] ){
239649 case 'c': aArray[10] = 1; break;
239650 case 'e': aArray[11] = 1; break;
239651 case 'n': aArray[12] = 1; break;
239652 case '*':
239653 aArray[10] = 1;
239654 aArray[11] = 1;
239655 aArray[12] = 1;
239656 break;
239657 default: return 1; }
239658 break;
239659
239660 case 'N':
239661 switch( zCat[1] ){
239662 case 'd': aArray[13] = 1; break;
239663 case 'l': aArray[14] = 1; break;
239664 case 'o': aArray[15] = 1; break;
239665 case '*':
239666 aArray[13] = 1;
239667 aArray[14] = 1;
239668 aArray[15] = 1;
239669 break;
239670 default: return 1; }
239671 break;
239672
239673 case 'P':
239674 switch( zCat[1] ){
239675 case 'c': aArray[16] = 1; break;
239676 case 'd': aArray[17] = 1; break;
239677 case 'e': aArray[18] = 1; break;
239678 case 'f': aArray[19] = 1; break;
239679 case 'i': aArray[20] = 1; break;
239680 case 'o': aArray[21] = 1; break;
239681 case 's': aArray[22] = 1; break;
239682 case '*':
239683 aArray[16] = 1;
239684 aArray[17] = 1;
239685 aArray[18] = 1;
239686 aArray[19] = 1;
239687 aArray[20] = 1;
239688 aArray[21] = 1;
239689 aArray[22] = 1;
239690 break;
239691 default: return 1; }
239692 break;
239693
239694 case 'S':
239695 switch( zCat[1] ){
239696 case 'c': aArray[23] = 1; break;
239697 case 'k': aArray[24] = 1; break;
239698 case 'm': aArray[25] = 1; break;
239699 case 'o': aArray[26] = 1; break;
239700 case '*':
239701 aArray[23] = 1;
239702 aArray[24] = 1;
239703 aArray[25] = 1;
239704 aArray[26] = 1;
239705 break;
239706 default: return 1; }
239707 break;
239708
239709 case 'Z':
239710 switch( zCat[1] ){
239711 case 'l': aArray[27] = 1; break;
239712 case 'p': aArray[28] = 1; break;
239713 case 's': aArray[29] = 1; break;
239714 case '*':
239715 aArray[27] = 1;
239716 aArray[28] = 1;
239717 aArray[29] = 1;
239718 break;
239719 default: return 1; }
239720 break;
239721
239722 }
239723 return 0;
239724}
239725
239726static u16 aFts5UnicodeBlock[] = {
239727 0, 1471, 1753, 1760, 1760, 1760, 1760, 1760, 1760, 1760,
239728 1760, 1760, 1760, 1760, 1760, 1763, 1765,
239729 };
239730static u16 aFts5UnicodeMap[] = {
239731 0, 32, 33, 36, 37, 40, 41, 42, 43, 44,
239732 45, 46, 48, 58, 60, 63, 65, 91, 92, 93,
239733 94, 95, 96, 97, 123, 124, 125, 126, 127, 160,
239734 161, 162, 166, 167, 168, 169, 170, 171, 172, 173,
239735 174, 175, 176, 177, 178, 180, 181, 182, 184, 185,
239736 186, 187, 188, 191, 192, 215, 216, 223, 247, 248,
239737 256, 312, 313, 329, 330, 377, 383, 385, 387, 388,
239738 391, 394, 396, 398, 402, 403, 405, 406, 409, 412,
239739 414, 415, 417, 418, 423, 427, 428, 431, 434, 436,
239740 437, 440, 442, 443, 444, 446, 448, 452, 453, 454,
239741 455, 456, 457, 458, 459, 460, 461, 477, 478, 496,
239742 497, 498, 499, 500, 503, 505, 506, 564, 570, 572,
239743 573, 575, 577, 580, 583, 584, 592, 660, 661, 688,
239744 706, 710, 722, 736, 741, 748, 749, 750, 751, 768,
239745 880, 884, 885, 886, 890, 891, 894, 900, 902, 903,
239746 904, 908, 910, 912, 913, 931, 940, 975, 977, 978,
239747 981, 984, 1008, 1012, 1014, 1015, 1018, 1020, 1021, 1072,
239748 1120, 1154, 1155, 1160, 1162, 1217, 1231, 1232, 1329, 1369,
239749 1370, 1377, 1417, 1418, 1423, 1425, 1470, 1471, 1472, 1473,
239750 1475, 1476, 1478, 1479, 1488, 1520, 1523, 1536, 1542, 1545,
239751 1547, 1548, 1550, 1552, 1563, 1566, 1568, 1600, 1601, 1611,
239752 1632, 1642, 1646, 1648, 1649, 1748, 1749, 1750, 1757, 1758,
239753 1759, 1765, 1767, 1769, 1770, 1774, 1776, 1786, 1789, 1791,
239754 1792, 1807, 1808, 1809, 1810, 1840, 1869, 1958, 1969, 1984,
239755 1994, 2027, 2036, 2038, 2039, 2042, 2048, 2070, 2074, 2075,
239756 2084, 2085, 2088, 2089, 2096, 2112, 2137, 2142, 2208, 2210,
239757 2276, 2304, 2307, 2308, 2362, 2363, 2364, 2365, 2366, 2369,
239758 2377, 2381, 2382, 2384, 2385, 2392, 2402, 2404, 2406, 2416,
239759 2417, 2418, 2425, 2433, 2434, 2437, 2447, 2451, 2474, 2482,
239760 2486, 2492, 2493, 2494, 2497, 2503, 2507, 2509, 2510, 2519,
239761 2524, 2527, 2530, 2534, 2544, 2546, 2548, 2554, 2555, 2561,
239762 2563, 2565, 2575, 2579, 2602, 2610, 2613, 2616, 2620, 2622,
239763 2625, 2631, 2635, 2641, 2649, 2654, 2662, 2672, 2674, 2677,
239764 2689, 2691, 2693, 2703, 2707, 2730, 2738, 2741, 2748, 2749,
239765 2750, 2753, 2759, 2761, 2763, 2765, 2768, 2784, 2786, 2790,
239766 2800, 2801, 2817, 2818, 2821, 2831, 2835, 2858, 2866, 2869,
239767 2876, 2877, 2878, 2879, 2880, 2881, 2887, 2891, 2893, 2902,
239768 2903, 2908, 2911, 2914, 2918, 2928, 2929, 2930, 2946, 2947,
239769 2949, 2958, 2962, 2969, 2972, 2974, 2979, 2984, 2990, 3006,
239770 3008, 3009, 3014, 3018, 3021, 3024, 3031, 3046, 3056, 3059,
239771 3065, 3066, 3073, 3077, 3086, 3090, 3114, 3125, 3133, 3134,
239772 3137, 3142, 3146, 3157, 3160, 3168, 3170, 3174, 3192, 3199,
239773 3202, 3205, 3214, 3218, 3242, 3253, 3260, 3261, 3262, 3263,
239774 3264, 3270, 3271, 3274, 3276, 3285, 3294, 3296, 3298, 3302,
239775 3313, 3330, 3333, 3342, 3346, 3389, 3390, 3393, 3398, 3402,
239776 3405, 3406, 3415, 3424, 3426, 3430, 3440, 3449, 3450, 3458,
239777 3461, 3482, 3507, 3517, 3520, 3530, 3535, 3538, 3542, 3544,
239778 3570, 3572, 3585, 3633, 3634, 3636, 3647, 3648, 3654, 3655,
239779 3663, 3664, 3674, 3713, 3716, 3719, 3722, 3725, 3732, 3737,
239780 3745, 3749, 3751, 3754, 3757, 3761, 3762, 3764, 3771, 3773,
239781 3776, 3782, 3784, 3792, 3804, 3840, 3841, 3844, 3859, 3860,
239782 3861, 3864, 3866, 3872, 3882, 3892, 3893, 3894, 3895, 3896,
239783 3897, 3898, 3899, 3900, 3901, 3902, 3904, 3913, 3953, 3967,
239784 3968, 3973, 3974, 3976, 3981, 3993, 4030, 4038, 4039, 4046,
239785 4048, 4053, 4057, 4096, 4139, 4141, 4145, 4146, 4152, 4153,
239786 4155, 4157, 4159, 4160, 4170, 4176, 4182, 4184, 4186, 4190,
239787 4193, 4194, 4197, 4199, 4206, 4209, 4213, 4226, 4227, 4229,
239788 4231, 4237, 4238, 4239, 4240, 4250, 4253, 4254, 4256, 4295,
239789 4301, 4304, 4347, 4348, 4349, 4682, 4688, 4696, 4698, 4704,
239790 4746, 4752, 4786, 4792, 4800, 4802, 4808, 4824, 4882, 4888,
239791 4957, 4960, 4969, 4992, 5008, 5024, 5120, 5121, 5741, 5743,
239792 5760, 5761, 5787, 5788, 5792, 5867, 5870, 5888, 5902, 5906,
239793 5920, 5938, 5941, 5952, 5970, 5984, 5998, 6002, 6016, 6068,
239794 6070, 6071, 6078, 6086, 6087, 6089, 6100, 6103, 6104, 6107,
239795 6108, 6109, 6112, 6128, 6144, 6150, 6151, 6155, 6158, 6160,
239796 6176, 6211, 6212, 6272, 6313, 6314, 6320, 6400, 6432, 6435,
239797 6439, 6441, 6448, 6450, 6451, 6457, 6464, 6468, 6470, 6480,
239798 6512, 6528, 6576, 6593, 6600, 6608, 6618, 6622, 6656, 6679,
239799 6681, 6686, 6688, 6741, 6742, 6743, 6744, 6752, 6753, 6754,
239800 6755, 6757, 6765, 6771, 6783, 6784, 6800, 6816, 6823, 6824,
239801 6912, 6916, 6917, 6964, 6965, 6966, 6971, 6972, 6973, 6978,
239802 6979, 6981, 6992, 7002, 7009, 7019, 7028, 7040, 7042, 7043,
239803 7073, 7074, 7078, 7080, 7082, 7083, 7084, 7086, 7088, 7098,
239804 7142, 7143, 7144, 7146, 7149, 7150, 7151, 7154, 7164, 7168,
239805 7204, 7212, 7220, 7222, 7227, 7232, 7245, 7248, 7258, 7288,
239806 7294, 7360, 7376, 7379, 7380, 7393, 7394, 7401, 7405, 7406,
239807 7410, 7412, 7413, 7424, 7468, 7531, 7544, 7545, 7579, 7616,
239808 7676, 7680, 7830, 7838, 7936, 7944, 7952, 7960, 7968, 7976,
239809 7984, 7992, 8000, 8008, 8016, 8025, 8027, 8029, 8031, 8033,
239810 8040, 8048, 8064, 8072, 8080, 8088, 8096, 8104, 8112, 8118,
239811 8120, 8124, 8125, 8126, 8127, 8130, 8134, 8136, 8140, 8141,
239812 8144, 8150, 8152, 8157, 8160, 8168, 8173, 8178, 8182, 8184,
239813 8188, 8189, 8192, 8203, 8208, 8214, 8216, 8217, 8218, 8219,
239814 8221, 8222, 8223, 8224, 8232, 8233, 8234, 8239, 8240, 8249,
239815 8250, 8251, 8255, 8257, 8260, 8261, 8262, 8263, 8274, 8275,
239816 8276, 8277, 8287, 8288, 8298, 8304, 8305, 8308, 8314, 8317,
239817 8318, 8319, 8320, 8330, 8333, 8334, 8336, 8352, 8400, 8413,
239818 8417, 8418, 8421, 8448, 8450, 8451, 8455, 8456, 8458, 8459,
239819 8462, 8464, 8467, 8468, 8469, 8470, 8472, 8473, 8478, 8484,
239820 8485, 8486, 8487, 8488, 8489, 8490, 8494, 8495, 8496, 8500,
239821 8501, 8505, 8506, 8508, 8510, 8512, 8517, 8519, 8522, 8523,
239822 8524, 8526, 8527, 8528, 8544, 8579, 8581, 8585, 8592, 8597,
239823 8602, 8604, 8608, 8609, 8611, 8612, 8614, 8615, 8622, 8623,
239824 8654, 8656, 8658, 8659, 8660, 8661, 8692, 8960, 8968, 8972,
239825 8992, 8994, 9001, 9002, 9003, 9084, 9085, 9115, 9140, 9180,
239826 9186, 9216, 9280, 9312, 9372, 9450, 9472, 9655, 9656, 9665,
239827 9666, 9720, 9728, 9839, 9840, 9985, 10088, 10089, 10090, 10091,
239828 10092, 10093, 10094, 10095, 10096, 10097, 10098, 10099, 10100, 10101,
239829 10102, 10132, 10176, 10181, 10182, 10183, 10214, 10215, 10216, 10217,
239830 10218, 10219, 10220, 10221, 10222, 10223, 10224, 10240, 10496, 10627,
239831 10628, 10629, 10630, 10631, 10632, 10633, 10634, 10635, 10636, 10637,
239832 10638, 10639, 10640, 10641, 10642, 10643, 10644, 10645, 10646, 10647,
239833 10648, 10649, 10712, 10713, 10714, 10715, 10716, 10748, 10749, 10750,
239834 11008, 11056, 11077, 11079, 11088, 11264, 11312, 11360, 11363, 11365,
239835 11367, 11374, 11377, 11378, 11380, 11381, 11383, 11388, 11390, 11393,
239836 11394, 11492, 11493, 11499, 11503, 11506, 11513, 11517, 11518, 11520,
239837 11559, 11565, 11568, 11631, 11632, 11647, 11648, 11680, 11688, 11696,
239838 11704, 11712, 11720, 11728, 11736, 11744, 11776, 11778, 11779, 11780,
239839 11781, 11782, 11785, 11786, 11787, 11788, 11789, 11790, 11799, 11800,
239840 11802, 11803, 11804, 11805, 11806, 11808, 11809, 11810, 11811, 11812,
239841 11813, 11814, 11815, 11816, 11817, 11818, 11823, 11824, 11834, 11904,
239842 11931, 12032, 12272, 12288, 12289, 12292, 12293, 12294, 12295, 12296,
239843 12297, 12298, 12299, 12300, 12301, 12302, 12303, 12304, 12305, 12306,
239844 12308, 12309, 12310, 12311, 12312, 12313, 12314, 12315, 12316, 12317,
239845 12318, 12320, 12321, 12330, 12334, 12336, 12337, 12342, 12344, 12347,
239846 12348, 12349, 12350, 12353, 12441, 12443, 12445, 12447, 12448, 12449,
239847 12539, 12540, 12543, 12549, 12593, 12688, 12690, 12694, 12704, 12736,
239848 12784, 12800, 12832, 12842, 12872, 12880, 12881, 12896, 12928, 12938,
239849 12977, 12992, 13056, 13312, 19893, 19904, 19968, 40908, 40960, 40981,
239850 40982, 42128, 42192, 42232, 42238, 42240, 42508, 42509, 42512, 42528,
239851 42538, 42560, 42606, 42607, 42608, 42611, 42612, 42622, 42623, 42624,
239852 42655, 42656, 42726, 42736, 42738, 42752, 42775, 42784, 42786, 42800,
239853 42802, 42864, 42865, 42873, 42878, 42888, 42889, 42891, 42896, 42912,
239854 43000, 43002, 43003, 43010, 43011, 43014, 43015, 43019, 43020, 43043,
239855 43045, 43047, 43048, 43056, 43062, 43064, 43065, 43072, 43124, 43136,
239856 43138, 43188, 43204, 43214, 43216, 43232, 43250, 43256, 43259, 43264,
239857 43274, 43302, 43310, 43312, 43335, 43346, 43359, 43360, 43392, 43395,
239858 43396, 43443, 43444, 43446, 43450, 43452, 43453, 43457, 43471, 43472,
239859 43486, 43520, 43561, 43567, 43569, 43571, 43573, 43584, 43587, 43588,
239860 43596, 43597, 43600, 43612, 43616, 43632, 43633, 43639, 43642, 43643,
239861 43648, 43696, 43697, 43698, 43701, 43703, 43705, 43710, 43712, 43713,
239862 43714, 43739, 43741, 43742, 43744, 43755, 43756, 43758, 43760, 43762,
239863 43763, 43765, 43766, 43777, 43785, 43793, 43808, 43816, 43968, 44003,
239864 44005, 44006, 44008, 44009, 44011, 44012, 44013, 44016, 44032, 55203,
239865 55216, 55243, 55296, 56191, 56319, 57343, 57344, 63743, 63744, 64112,
239866 64256, 64275, 64285, 64286, 64287, 64297, 64298, 64312, 64318, 64320,
239867 64323, 64326, 64434, 64467, 64830, 64831, 64848, 64914, 65008, 65020,
239868 65021, 65024, 65040, 65047, 65048, 65049, 65056, 65072, 65073, 65075,
239869 65077, 65078, 65079, 65080, 65081, 65082, 65083, 65084, 65085, 65086,
239870 65087, 65088, 65089, 65090, 65091, 65092, 65093, 65095, 65096, 65097,
239871 65101, 65104, 65108, 65112, 65113, 65114, 65115, 65116, 65117, 65118,
239872 65119, 65122, 65123, 65124, 65128, 65129, 65130, 65136, 65142, 65279,
239873 65281, 65284, 65285, 65288, 65289, 65290, 65291, 65292, 65293, 65294,
239874 65296, 65306, 65308, 65311, 65313, 65339, 65340, 65341, 65342, 65343,
239875 65344, 65345, 65371, 65372, 65373, 65374, 65375, 65376, 65377, 65378,
239876 65379, 65380, 65382, 65392, 65393, 65438, 65440, 65474, 65482, 65490,
239877 65498, 65504, 65506, 65507, 65508, 65509, 65512, 65513, 65517, 65529,
239878 65532, 0, 13, 40, 60, 63, 80, 128, 256, 263,
239879 311, 320, 373, 377, 394, 400, 464, 509, 640, 672,
239880 768, 800, 816, 833, 834, 842, 896, 927, 928, 968,
239881 976, 977, 1024, 1064, 1104, 1184, 2048, 2056, 2058, 2103,
239882 2108, 2111, 2135, 2136, 2304, 2326, 2335, 2336, 2367, 2432,
239883 2494, 2560, 2561, 2565, 2572, 2576, 2581, 2585, 2616, 2623,
239884 2624, 2640, 2656, 2685, 2687, 2816, 2873, 2880, 2904, 2912,
239885 2936, 3072, 3680, 4096, 4097, 4098, 4099, 4152, 4167, 4178,
239886 4198, 4224, 4226, 4227, 4272, 4275, 4279, 4281, 4283, 4285,
239887 4286, 4304, 4336, 4352, 4355, 4391, 4396, 4397, 4406, 4416,
239888 4480, 4482, 4483, 4531, 4534, 4543, 4545, 4549, 4560, 5760,
239889 5803, 5804, 5805, 5806, 5808, 5814, 5815, 5824, 8192, 9216,
239890 9328, 12288, 26624, 28416, 28496, 28497, 28559, 28563, 45056, 53248,
239891 53504, 53545, 53605, 53607, 53610, 53613, 53619, 53627, 53635, 53637,
239892 53644, 53674, 53678, 53760, 53826, 53829, 54016, 54112, 54272, 54298,
239893 54324, 54350, 54358, 54376, 54402, 54428, 54430, 54434, 54437, 54441,
239894 54446, 54454, 54459, 54461, 54469, 54480, 54506, 54532, 54535, 54541,
239895 54550, 54558, 54584, 54587, 54592, 54598, 54602, 54610, 54636, 54662,
239896 54688, 54714, 54740, 54766, 54792, 54818, 54844, 54870, 54896, 54922,
239897 54952, 54977, 54978, 55003, 55004, 55010, 55035, 55036, 55061, 55062,
239898 55068, 55093, 55094, 55119, 55120, 55126, 55151, 55152, 55177, 55178,
239899 55184, 55209, 55210, 55235, 55236, 55242, 55246, 60928, 60933, 60961,
239900 60964, 60967, 60969, 60980, 60985, 60987, 60994, 60999, 61001, 61003,
239901 61005, 61009, 61012, 61015, 61017, 61019, 61021, 61023, 61025, 61028,
239902 61031, 61036, 61044, 61049, 61054, 61056, 61067, 61089, 61093, 61099,
239903 61168, 61440, 61488, 61600, 61617, 61633, 61649, 61696, 61712, 61744,
239904 61808, 61926, 61968, 62016, 62032, 62208, 62256, 62263, 62336, 62368,
239905 62406, 62432, 62464, 62528, 62530, 62713, 62720, 62784, 62800, 62971,
239906 63045, 63104, 63232, 0, 42710, 42752, 46900, 46912, 47133, 63488,
239907 1, 32, 256, 0, 65533,
239908 };
239909static u16 aFts5UnicodeData[] = {
239910 1025, 61, 117, 55, 117, 54, 50, 53, 57, 53,
239911 49, 85, 333, 85, 121, 85, 841, 54, 53, 50,
239912 56, 48, 56, 837, 54, 57, 50, 57, 1057, 61,
239913 53, 151, 58, 53, 56, 58, 39, 52, 57, 34,
239914 58, 56, 58, 57, 79, 56, 37, 85, 56, 47,
239915 39, 51, 111, 53, 745, 57, 233, 773, 57, 261,
239916 1822, 37, 542, 37, 1534, 222, 69, 73, 37, 126,
239917 126, 73, 69, 137, 37, 73, 37, 105, 101, 73,
239918 37, 73, 37, 190, 158, 37, 126, 126, 73, 37,
239919 126, 94, 37, 39, 94, 69, 135, 41, 40, 37,
239920 41, 40, 37, 41, 40, 37, 542, 37, 606, 37,
239921 41, 40, 37, 126, 73, 37, 1886, 197, 73, 37,
239922 73, 69, 126, 105, 37, 286, 2181, 39, 869, 582,
239923 152, 390, 472, 166, 248, 38, 56, 38, 568, 3596,
239924 158, 38, 56, 94, 38, 101, 53, 88, 41, 53,
239925 105, 41, 73, 37, 553, 297, 1125, 94, 37, 105,
239926 101, 798, 133, 94, 57, 126, 94, 37, 1641, 1541,
239927 1118, 58, 172, 75, 1790, 478, 37, 2846, 1225, 38,
239928 213, 1253, 53, 49, 55, 1452, 49, 44, 53, 76,
239929 53, 76, 53, 44, 871, 103, 85, 162, 121, 85,
239930 55, 85, 90, 364, 53, 85, 1031, 38, 327, 684,
239931 333, 149, 71, 44, 3175, 53, 39, 236, 34, 58,
239932 204, 70, 76, 58, 140, 71, 333, 103, 90, 39,
239933 469, 34, 39, 44, 967, 876, 2855, 364, 39, 333,
239934 1063, 300, 70, 58, 117, 38, 711, 140, 38, 300,
239935 38, 108, 38, 172, 501, 807, 108, 53, 39, 359,
239936 876, 108, 42, 1735, 44, 42, 44, 39, 106, 268,
239937 138, 44, 74, 39, 236, 327, 76, 85, 333, 53,
239938 38, 199, 231, 44, 74, 263, 71, 711, 231, 39,
239939 135, 44, 39, 106, 140, 74, 74, 44, 39, 42,
239940 71, 103, 76, 333, 71, 87, 207, 58, 55, 76,
239941 42, 199, 71, 711, 231, 71, 71, 71, 44, 106,
239942 76, 76, 108, 44, 135, 39, 333, 76, 103, 44,
239943 76, 42, 295, 103, 711, 231, 71, 167, 44, 39,
239944 106, 172, 76, 42, 74, 44, 39, 71, 76, 333,
239945 53, 55, 44, 74, 263, 71, 711, 231, 71, 167,
239946 44, 39, 42, 44, 42, 140, 74, 74, 44, 44,
239947 42, 71, 103, 76, 333, 58, 39, 207, 44, 39,
239948 199, 103, 135, 71, 39, 71, 71, 103, 391, 74,
239949 44, 74, 106, 106, 44, 39, 42, 333, 111, 218,
239950 55, 58, 106, 263, 103, 743, 327, 167, 39, 108,
239951 138, 108, 140, 76, 71, 71, 76, 333, 239, 58,
239952 74, 263, 103, 743, 327, 167, 44, 39, 42, 44,
239953 170, 44, 74, 74, 76, 74, 39, 71, 76, 333,
239954 71, 74, 263, 103, 1319, 39, 106, 140, 106, 106,
239955 44, 39, 42, 71, 76, 333, 207, 58, 199, 74,
239956 583, 775, 295, 39, 231, 44, 106, 108, 44, 266,
239957 74, 53, 1543, 44, 71, 236, 55, 199, 38, 268,
239958 53, 333, 85, 71, 39, 71, 39, 39, 135, 231,
239959 103, 39, 39, 71, 135, 44, 71, 204, 76, 39,
239960 167, 38, 204, 333, 135, 39, 122, 501, 58, 53,
239961 122, 76, 218, 333, 335, 58, 44, 58, 44, 58,
239962 44, 54, 50, 54, 50, 74, 263, 1159, 460, 42,
239963 172, 53, 76, 167, 364, 1164, 282, 44, 218, 90,
239964 181, 154, 85, 1383, 74, 140, 42, 204, 42, 76,
239965 74, 76, 39, 333, 213, 199, 74, 76, 135, 108,
239966 39, 106, 71, 234, 103, 140, 423, 44, 74, 76,
239967 202, 44, 39, 42, 333, 106, 44, 90, 1225, 41,
239968 41, 1383, 53, 38, 10631, 135, 231, 39, 135, 1319,
239969 135, 1063, 135, 231, 39, 135, 487, 1831, 135, 2151,
239970 108, 309, 655, 519, 346, 2727, 49, 19847, 85, 551,
239971 61, 839, 54, 50, 2407, 117, 110, 423, 135, 108,
239972 583, 108, 85, 583, 76, 423, 103, 76, 1671, 76,
239973 42, 236, 266, 44, 74, 364, 117, 38, 117, 55,
239974 39, 44, 333, 335, 213, 49, 149, 108, 61, 333,
239975 1127, 38, 1671, 1319, 44, 39, 2247, 935, 108, 138,
239976 76, 106, 74, 44, 202, 108, 58, 85, 333, 967,
239977 167, 1415, 554, 231, 74, 333, 47, 1114, 743, 76,
239978 106, 85, 1703, 42, 44, 42, 236, 44, 42, 44,
239979 74, 268, 202, 332, 44, 333, 333, 245, 38, 213,
239980 140, 42, 1511, 44, 42, 172, 42, 44, 170, 44,
239981 74, 231, 333, 245, 346, 300, 314, 76, 42, 967,
239982 42, 140, 74, 76, 42, 44, 74, 71, 333, 1415,
239983 44, 42, 76, 106, 44, 42, 108, 74, 149, 1159,
239984 266, 268, 74, 76, 181, 333, 103, 333, 967, 198,
239985 85, 277, 108, 53, 428, 42, 236, 135, 44, 135,
239986 74, 44, 71, 1413, 2022, 421, 38, 1093, 1190, 1260,
239987 140, 4830, 261, 3166, 261, 265, 197, 201, 261, 265,
239988 261, 265, 197, 201, 261, 41, 41, 41, 94, 229,
239989 265, 453, 261, 264, 261, 264, 261, 264, 165, 69,
239990 137, 40, 56, 37, 120, 101, 69, 137, 40, 120,
239991 133, 69, 137, 120, 261, 169, 120, 101, 69, 137,
239992 40, 88, 381, 162, 209, 85, 52, 51, 54, 84,
239993 51, 54, 52, 277, 59, 60, 162, 61, 309, 52,
239994 51, 149, 80, 117, 57, 54, 50, 373, 57, 53,
239995 48, 341, 61, 162, 194, 47, 38, 207, 121, 54,
239996 50, 38, 335, 121, 54, 50, 422, 855, 428, 139,
239997 44, 107, 396, 90, 41, 154, 41, 90, 37, 105,
239998 69, 105, 37, 58, 41, 90, 57, 169, 218, 41,
239999 58, 41, 58, 41, 58, 137, 58, 37, 137, 37,
240000 135, 37, 90, 69, 73, 185, 94, 101, 58, 57,
240001 90, 37, 58, 527, 1134, 94, 142, 47, 185, 186,
240002 89, 154, 57, 90, 57, 90, 57, 250, 57, 1018,
240003 89, 90, 57, 58, 57, 1018, 8601, 282, 153, 666,
240004 89, 250, 54, 50, 2618, 57, 986, 825, 1306, 217,
240005 602, 1274, 378, 1935, 2522, 719, 5882, 57, 314, 57,
240006 1754, 281, 3578, 57, 4634, 3322, 54, 50, 54, 50,
240007 54, 50, 54, 50, 54, 50, 54, 50, 54, 50,
240008 975, 1434, 185, 54, 50, 1017, 54, 50, 54, 50,
240009 54, 50, 54, 50, 54, 50, 537, 8218, 4217, 54,
240010 50, 54, 50, 54, 50, 54, 50, 54, 50, 54,
240011 50, 54, 50, 54, 50, 54, 50, 54, 50, 54,
240012 50, 2041, 54, 50, 54, 50, 1049, 54, 50, 8281,
240013 1562, 697, 90, 217, 346, 1513, 1509, 126, 73, 69,
240014 254, 105, 37, 94, 37, 94, 165, 70, 105, 37,
240015 3166, 37, 218, 158, 108, 94, 149, 47, 85, 1221,
240016 37, 37, 1799, 38, 53, 44, 743, 231, 231, 231,
240017 231, 231, 231, 231, 231, 1036, 85, 52, 51, 52,
240018 51, 117, 52, 51, 53, 52, 51, 309, 49, 85,
240019 49, 53, 52, 51, 85, 52, 51, 54, 50, 54,
240020 50, 54, 50, 54, 50, 181, 38, 341, 81, 858,
240021 2874, 6874, 410, 61, 117, 58, 38, 39, 46, 54,
240022 50, 54, 50, 54, 50, 54, 50, 54, 50, 90,
240023 54, 50, 54, 50, 54, 50, 54, 50, 49, 54,
240024 82, 58, 302, 140, 74, 49, 166, 90, 110, 38,
240025 39, 53, 90, 2759, 76, 88, 70, 39, 49, 2887,
240026 53, 102, 39, 1319, 3015, 90, 143, 346, 871, 1178,
240027 519, 1018, 335, 986, 271, 58, 495, 1050, 335, 1274,
240028 495, 2042, 8218, 39, 39, 2074, 39, 39, 679, 38,
240029 36583, 1786, 1287, 198, 85, 8583, 38, 117, 519, 333,
240030 71, 1502, 39, 44, 107, 53, 332, 53, 38, 798,
240031 44, 2247, 334, 76, 213, 760, 294, 88, 478, 69,
240032 2014, 38, 261, 190, 350, 38, 88, 158, 158, 382,
240033 70, 37, 231, 44, 103, 44, 135, 44, 743, 74,
240034 76, 42, 154, 207, 90, 55, 58, 1671, 149, 74,
240035 1607, 522, 44, 85, 333, 588, 199, 117, 39, 333,
240036 903, 268, 85, 743, 364, 74, 53, 935, 108, 42,
240037 1511, 44, 74, 140, 74, 44, 138, 437, 38, 333,
240038 85, 1319, 204, 74, 76, 74, 76, 103, 44, 263,
240039 44, 42, 333, 149, 519, 38, 199, 122, 39, 42,
240040 1543, 44, 39, 108, 71, 76, 167, 76, 39, 44,
240041 39, 71, 38, 85, 359, 42, 76, 74, 85, 39,
240042 70, 42, 44, 199, 199, 199, 231, 231, 1127, 74,
240043 44, 74, 44, 74, 53, 42, 44, 333, 39, 39,
240044 743, 1575, 36, 68, 68, 36, 63, 63, 11719, 3399,
240045 229, 165, 39, 44, 327, 57, 423, 167, 39, 71,
240046 71, 3463, 536, 11623, 54, 50, 2055, 1735, 391, 55,
240047 58, 524, 245, 54, 50, 53, 236, 53, 81, 80,
240048 54, 50, 54, 50, 54, 50, 54, 50, 54, 50,
240049 54, 50, 54, 50, 54, 50, 85, 54, 50, 149,
240050 112, 117, 149, 49, 54, 50, 54, 50, 54, 50,
240051 117, 57, 49, 121, 53, 55, 85, 167, 4327, 34,
240052 117, 55, 117, 54, 50, 53, 57, 53, 49, 85,
240053 333, 85, 121, 85, 841, 54, 53, 50, 56, 48,
240054 56, 837, 54, 57, 50, 57, 54, 50, 53, 54,
240055 50, 85, 327, 38, 1447, 70, 999, 199, 199, 199,
240056 103, 87, 57, 56, 58, 87, 58, 153, 90, 98,
240057 90, 391, 839, 615, 71, 487, 455, 3943, 117, 1455,
240058 314, 1710, 143, 570, 47, 410, 1466, 44, 935, 1575,
240059 999, 143, 551, 46, 263, 46, 967, 53, 1159, 263,
240060 53, 174, 1289, 1285, 2503, 333, 199, 39, 1415, 71,
240061 39, 743, 53, 271, 711, 207, 53, 839, 53, 1799,
240062 71, 39, 108, 76, 140, 135, 103, 871, 108, 44,
240063 271, 309, 935, 79, 53, 1735, 245, 711, 271, 615,
240064 271, 2343, 1007, 42, 44, 42, 1703, 492, 245, 655,
240065 333, 76, 42, 1447, 106, 140, 74, 76, 85, 34,
240066 149, 807, 333, 108, 1159, 172, 42, 268, 333, 149,
240067 76, 42, 1543, 106, 300, 74, 135, 149, 333, 1383,
240068 44, 42, 44, 74, 204, 42, 44, 333, 28135, 3182,
240069 149, 34279, 18215, 2215, 39, 1482, 140, 422, 71, 7898,
240070 1274, 1946, 74, 108, 122, 202, 258, 268, 90, 236,
240071 986, 140, 1562, 2138, 108, 58, 2810, 591, 841, 837,
240072 841, 229, 581, 841, 837, 41, 73, 41, 73, 137,
240073 265, 133, 37, 229, 357, 841, 837, 73, 137, 265,
240074 233, 837, 73, 137, 169, 41, 233, 837, 841, 837,
240075 841, 837, 841, 837, 841, 837, 841, 837, 841, 901,
240076 809, 57, 805, 57, 197, 809, 57, 805, 57, 197,
240077 809, 57, 805, 57, 197, 809, 57, 805, 57, 197,
240078 809, 57, 805, 57, 197, 94, 1613, 135, 871, 71,
240079 39, 39, 327, 135, 39, 39, 39, 39, 39, 39,
240080 103, 71, 39, 39, 39, 39, 39, 39, 71, 39,
240081 135, 231, 135, 135, 39, 327, 551, 103, 167, 551,
240082 89, 1434, 3226, 506, 474, 506, 506, 367, 1018, 1946,
240083 1402, 954, 1402, 314, 90, 1082, 218, 2266, 666, 1210,
240084 186, 570, 2042, 58, 5850, 154, 2010, 154, 794, 2266,
240085 378, 2266, 3738, 39, 39, 39, 39, 39, 39, 17351,
240086 34, 3074, 7692, 63, 63,
240087 };
240088
240089static int sqlite3Fts5UnicodeCategory(u32 iCode) {
240090 int iRes = -1;
240091 int iHi;
240092 int iLo;
240093 int ret;
240094 u16 iKey;
240095
240096 if( iCode>=(1<<20) ){
240097 return 0;
240098 }
240099 iLo = aFts5UnicodeBlock[(iCode>>16)];
240100 iHi = aFts5UnicodeBlock[1+(iCode>>16)];
240101 iKey = (iCode & 0xFFFF);
240102 while( iHi>iLo ){
240103 int iTest = (iHi + iLo) / 2;
240104 assert( iTest>=iLo && iTest<iHi )((void) (0));
240105 if( iKey>=aFts5UnicodeMap[iTest] ){
240106 iRes = iTest;
240107 iLo = iTest+1;
240108 }else{
240109 iHi = iTest;
240110 }
240111 }
240112
240113 if( iRes<0 ) return 0;
240114 if( iKey>=(aFts5UnicodeMap[iRes]+(aFts5UnicodeData[iRes]>>5)) ) return 0;
240115 ret = aFts5UnicodeData[iRes] & 0x1F;
240116 if( ret!=30 ) return ret;
240117 return ((iKey - aFts5UnicodeMap[iRes]) & 0x01) ? 5 : 9;
240118}
240119
240120static void sqlite3Fts5UnicodeAscii(u8 *aArray, u8 *aAscii){
240121 int i = 0;
240122 int iTbl = 0;
240123 while( i<128 ){
240124 int bToken = aArray[ aFts5UnicodeData[iTbl] & 0x1F ];
240125 int n = (aFts5UnicodeData[iTbl] >> 5) + i;
240126 for(; i<128 && i<n; i++){
240127 aAscii[i] = (u8)bToken;
240128 }
240129 iTbl++;
240130 }
240131 aAscii[0] = 0; /* 0x00 is never a token character */
240132}
240133
240134
240135/*
240136** 2015 May 30
240137**
240138** The author disclaims copyright to this source code. In place of
240139** a legal notice, here is a blessing:
240140**
240141** May you do good and not evil.
240142** May you find forgiveness for yourself and forgive others.
240143** May you share freely, never taking more than you give.
240144**
240145******************************************************************************
240146**
240147** Routines for varint serialization and deserialization.
240148*/
240149
240150
240151/* #include "fts5Int.h" */
240152
240153/*
240154** This is a copy of the sqlite3GetVarint32() routine from the SQLite core.
240155** Except, this version does handle the single byte case that the core
240156** version depends on being handled before its function is called.
240157*/
240158static int sqlite3Fts5GetVarint32(const unsigned char *p, u32 *v){
240159 u32 a,b;
240160
240161 /* The 1-byte case. Overwhelmingly the most common. */
240162 a = *p;
240163 /* a: p0 (unmasked) */
240164 if (!(a&0x80))
240165 {
240166 /* Values between 0 and 127 */
240167 *v = a;
240168 return 1;
240169 }
240170
240171 /* The 2-byte case */
240172 p++;
240173 b = *p;
240174 /* b: p1 (unmasked) */
240175 if (!(b&0x80))
240176 {
240177 /* Values between 128 and 16383 */
240178 a &= 0x7f;
240179 a = a<<7;
240180 *v = a | b;
240181 return 2;
240182 }
240183
240184 /* The 3-byte case */
240185 p++;
240186 a = a<<14;
240187 a |= *p;
240188 /* a: p0<<14 | p2 (unmasked) */
240189 if (!(a&0x80))
240190 {
240191 /* Values between 16384 and 2097151 */
240192 a &= (0x7f<<14)|(0x7f);
240193 b &= 0x7f;
240194 b = b<<7;
240195 *v = a | b;
240196 return 3;
240197 }
240198
240199 /* A 32-bit varint is used to store size information in btrees.
240200 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
240201 ** A 3-byte varint is sufficient, for example, to record the size
240202 ** of a 1048569-byte BLOB or string.
240203 **
240204 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
240205 ** rare larger cases can be handled by the slower 64-bit varint
240206 ** routine.
240207 */
240208 {
240209 u64 v64;
240210 u8 n;
240211 p -= 2;
240212 n = sqlite3Fts5GetVarint(p, &v64);
240213 *v = ((u32)v64) & 0x7FFFFFFF;
240214 assert( n>3 && n<=9 )((void) (0));
240215 return n;
240216 }
240217}
240218
240219
240220/*
240221** Bitmasks used by sqlite3GetVarint(). These precomputed constants
240222** are defined here rather than simply putting the constant expressions
240223** inline in order to work around bugs in the RVT compiler.
240224**
240225** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
240226**
240227** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
240228*/
240229#define SLOT_2_00x001fc07f 0x001fc07f
240230#define SLOT_4_2_00xf01fc07f 0xf01fc07f
240231
240232/*
240233** Read a 64-bit variable-length integer from memory starting at p[0].
240234** Return the number of bytes read. The value is stored in *v.
240235*/
240236static u8 sqlite3Fts5GetVarint(const unsigned char *p, u64 *v){
240237 u32 a,b,s;
240238
240239 a = *p;
240240 /* a: p0 (unmasked) */
240241 if (!(a&0x80))
240242 {
240243 *v = a;
240244 return 1;
240245 }
240246
240247 p++;
240248 b = *p;
240249 /* b: p1 (unmasked) */
240250 if (!(b&0x80))
240251 {
240252 a &= 0x7f;
240253 a = a<<7;
240254 a |= b;
240255 *v = a;
240256 return 2;
240257 }
240258
240259 /* Verify that constants are precomputed correctly */
240260 assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) )((void) (0));
240261 assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) )((void) (0));
240262
240263 p++;
240264 a = a<<14;
240265 a |= *p;
240266 /* a: p0<<14 | p2 (unmasked) */
240267 if (!(a&0x80))
240268 {
240269 a &= SLOT_2_00x001fc07f;
240270 b &= 0x7f;
240271 b = b<<7;
240272 a |= b;
240273 *v = a;
240274 return 3;
240275 }
240276
240277 /* CSE1 from below */
240278 a &= SLOT_2_00x001fc07f;
240279 p++;
240280 b = b<<14;
240281 b |= *p;
240282 /* b: p1<<14 | p3 (unmasked) */
240283 if (!(b&0x80))
240284 {
240285 b &= SLOT_2_00x001fc07f;
240286 /* moved CSE1 up */
240287 /* a &= (0x7f<<14)|(0x7f); */
240288 a = a<<7;
240289 a |= b;
240290 *v = a;
240291 return 4;
240292 }
240293
240294 /* a: p0<<14 | p2 (masked) */
240295 /* b: p1<<14 | p3 (unmasked) */
240296 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
240297 /* moved CSE1 up */
240298 /* a &= (0x7f<<14)|(0x7f); */
240299 b &= SLOT_2_00x001fc07f;
240300 s = a;
240301 /* s: p0<<14 | p2 (masked) */
240302
240303 p++;
240304 a = a<<14;
240305 a |= *p;
240306 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
240307 if (!(a&0x80))
240308 {
240309 /* we can skip these cause they were (effectively) done above in calc'ing s */
240310 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
240311 /* b &= (0x7f<<14)|(0x7f); */
240312 b = b<<7;
240313 a |= b;
240314 s = s>>18;
240315 *v = ((u64)s)<<32 | a;
240316 return 5;
240317 }
240318
240319 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
240320 s = s<<7;
240321 s |= b;
240322 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
240323
240324 p++;
240325 b = b<<14;
240326 b |= *p;
240327 /* b: p1<<28 | p3<<14 | p5 (unmasked) */
240328 if (!(b&0x80))
240329 {
240330 /* we can skip this cause it was (effectively) done above in calc'ing s */
240331 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
240332 a &= SLOT_2_00x001fc07f;
240333 a = a<<7;
240334 a |= b;
240335 s = s>>18;
240336 *v = ((u64)s)<<32 | a;
240337 return 6;
240338 }
240339
240340 p++;
240341 a = a<<14;
240342 a |= *p;
240343 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
240344 if (!(a&0x80))
240345 {
240346 a &= SLOT_4_2_00xf01fc07f;
240347 b &= SLOT_2_00x001fc07f;
240348 b = b<<7;
240349 a |= b;
240350 s = s>>11;
240351 *v = ((u64)s)<<32 | a;
240352 return 7;
240353 }
240354
240355 /* CSE2 from below */
240356 a &= SLOT_2_00x001fc07f;
240357 p++;
240358 b = b<<14;
240359 b |= *p;
240360 /* b: p3<<28 | p5<<14 | p7 (unmasked) */
240361 if (!(b&0x80))
240362 {
240363 b &= SLOT_4_2_00xf01fc07f;
240364 /* moved CSE2 up */
240365 /* a &= (0x7f<<14)|(0x7f); */
240366 a = a<<7;
240367 a |= b;
240368 s = s>>4;
240369 *v = ((u64)s)<<32 | a;
240370 return 8;
240371 }
240372
240373 p++;
240374 a = a<<15;
240375 a |= *p;
240376 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
240377
240378 /* moved CSE2 up */
240379 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
240380 b &= SLOT_2_00x001fc07f;
240381 b = b<<8;
240382 a |= b;
240383
240384 s = s<<4;
240385 b = p[-4];
240386 b &= 0x7f;
240387 b = b>>3;
240388 s |= b;
240389
240390 *v = ((u64)s)<<32 | a;
240391
240392 return 9;
240393}
240394
240395/*
240396** The variable-length integer encoding is as follows:
240397**
240398** KEY:
240399** A = 0xxxxxxx 7 bits of data and one flag bit
240400** B = 1xxxxxxx 7 bits of data and one flag bit
240401** C = xxxxxxxx 8 bits of data
240402**
240403** 7 bits - A
240404** 14 bits - BA
240405** 21 bits - BBA
240406** 28 bits - BBBA
240407** 35 bits - BBBBA
240408** 42 bits - BBBBBA
240409** 49 bits - BBBBBBA
240410** 56 bits - BBBBBBBA
240411** 64 bits - BBBBBBBBC
240412*/
240413
240414#ifdef SQLITE_NOINLINE__attribute__((noinline))
240415# define FTS5_NOINLINE__attribute__((noinline)) SQLITE_NOINLINE__attribute__((noinline))
240416#else
240417# define FTS5_NOINLINE__attribute__((noinline))
240418#endif
240419
240420/*
240421** Write a 64-bit variable-length integer to memory starting at p[0].
240422** The length of data write will be between 1 and 9 bytes. The number
240423** of bytes written is returned.
240424**
240425** A variable-length integer consists of the lower 7 bits of each byte
240426** for all bytes that have the 8th bit set and one byte with the 8th
240427** bit clear. Except, if we get to the 9th byte, it stores the full
240428** 8 bits and is the last byte.
240429*/
240430static int FTS5_NOINLINE__attribute__((noinline)) fts5PutVarint64(unsigned char *p, u64 v){
240431 int i, j, n;
240432 u8 buf[10];
240433 if( v & (((u64)0xff000000)<<32) ){
240434 p[8] = (u8)v;
240435 v >>= 8;
240436 for(i=7; i>=0; i--){
240437 p[i] = (u8)((v & 0x7f) | 0x80);
240438 v >>= 7;
240439 }
240440 return 9;
240441 }
240442 n = 0;
240443 do{
240444 buf[n++] = (u8)((v & 0x7f) | 0x80);
240445 v >>= 7;
240446 }while( v!=0 );
240447 buf[0] &= 0x7f;
240448 assert( n<=9 )((void) (0));
240449 for(i=0, j=n-1; j>=0; j--, i++){
240450 p[i] = buf[j];
240451 }
240452 return n;
240453}
240454
240455static int sqlite3Fts5PutVarint(unsigned char *p, u64 v){
240456 if( v<=0x7f ){
240457 p[0] = v&0x7f;
240458 return 1;
240459 }
240460 if( v<=0x3fff ){
240461 p[0] = ((v>>7)&0x7f)|0x80;
240462 p[1] = v&0x7f;
240463 return 2;
240464 }
240465 return fts5PutVarint64(p,v);
240466}
240467
240468
240469static int sqlite3Fts5GetVarintLen(u32 iVal){
240470#if 0
240471 if( iVal<(1 << 7 ) ) return 1;
240472#endif
240473 assert( iVal>=(1 << 7) )((void) (0));
240474 if( iVal<(1 << 14) ) return 2;
240475 if( iVal<(1 << 21) ) return 3;
240476 if( iVal<(1 << 28) ) return 4;
240477 return 5;
240478}
240479
240480/*
240481** 2015 May 08
240482**
240483** The author disclaims copyright to this source code. In place of
240484** a legal notice, here is a blessing:
240485**
240486** May you do good and not evil.
240487** May you find forgiveness for yourself and forgive others.
240488** May you share freely, never taking more than you give.
240489**
240490******************************************************************************
240491**
240492** This is an SQLite virtual table module implementing direct access to an
240493** existing FTS5 index. The module may create several different types of
240494** tables:
240495**
240496** col:
240497** CREATE TABLE vocab(term, col, doc, cnt, PRIMARY KEY(term, col));
240498**
240499** One row for each term/column combination. The value of $doc is set to
240500** the number of fts5 rows that contain at least one instance of term
240501** $term within column $col. Field $cnt is set to the total number of
240502** instances of term $term in column $col (in any row of the fts5 table).
240503**
240504** row:
240505** CREATE TABLE vocab(term, doc, cnt, PRIMARY KEY(term));
240506**
240507** One row for each term in the database. The value of $doc is set to
240508** the number of fts5 rows that contain at least one instance of term
240509** $term. Field $cnt is set to the total number of instances of term
240510** $term in the database.
240511**
240512** instance:
240513** CREATE TABLE vocab(term, doc, col, offset, PRIMARY KEY(<all-fields>));
240514**
240515** One row for each term instance in the database.
240516*/
240517
240518
240519/* #include "fts5Int.h" */
240520
240521
240522typedef struct Fts5VocabTable Fts5VocabTable;
240523typedef struct Fts5VocabCursor Fts5VocabCursor;
240524
240525struct Fts5VocabTable {
240526 sqlite3_vtab base;
240527 char *zFts5Tbl; /* Name of fts5 table */
240528 char *zFts5Db; /* Db containing fts5 table */
240529 sqlite3 *db; /* Database handle */
240530 Fts5Global *pGlobal; /* FTS5 global object for this database */
240531 int eType; /* FTS5_VOCAB_COL, ROW or INSTANCE */
240532 unsigned bBusy; /* True if busy */
240533};
240534
240535struct Fts5VocabCursor {
240536 sqlite3_vtab_cursor base;
240537 sqlite3_stmt *pStmt; /* Statement holding lock on pIndex */
240538 Fts5Table *pFts5; /* Associated FTS5 table */
240539
240540 int bEof; /* True if this cursor is at EOF */
240541 Fts5IndexIter *pIter; /* Term/rowid iterator object */
240542 void *pStruct; /* From sqlite3Fts5StructureRef() */
240543
240544 int nLeTerm; /* Size of zLeTerm in bytes */
240545 char *zLeTerm; /* (term <= $zLeTerm) paramater, or NULL */
240546
240547 /* These are used by 'col' tables only */
240548 int iCol;
240549 i64 *aCnt;
240550 i64 *aDoc;
240551
240552 /* Output values used by all tables. */
240553 i64 rowid; /* This table's current rowid value */
240554 Fts5Buffer term; /* Current value of 'term' column */
240555
240556 /* Output values Used by 'instance' tables only */
240557 i64 iInstPos;
240558 int iInstOff;
240559};
240560
240561#define FTS5_VOCAB_COL0 0
240562#define FTS5_VOCAB_ROW1 1
240563#define FTS5_VOCAB_INSTANCE2 2
240564
240565#define FTS5_VOCAB_COL_SCHEMA"term, col, doc, cnt" "term, col, doc, cnt"
240566#define FTS5_VOCAB_ROW_SCHEMA"term, doc, cnt" "term, doc, cnt"
240567#define FTS5_VOCAB_INST_SCHEMA"term, doc, col, offset" "term, doc, col, offset"
240568
240569/*
240570** Bits for the mask used as the idxNum value by xBestIndex/xFilter.
240571*/
240572#define FTS5_VOCAB_TERM_EQ0x01 0x01
240573#define FTS5_VOCAB_TERM_GE0x02 0x02
240574#define FTS5_VOCAB_TERM_LE0x04 0x04
240575
240576
240577/*
240578** Translate a string containing an fts5vocab table type to an
240579** FTS5_VOCAB_XXX constant. If successful, set *peType to the output
240580** value and return SQLITE_OK. Otherwise, set *pzErr to an error message
240581** and return SQLITE_ERROR.
240582*/
240583static int fts5VocabTableType(const char *zType, char **pzErr, int *peType){
240584 int rc = SQLITE_OK0;
240585 char *zCopy = sqlite3Fts5Strndup(&rc, zType, -1);
240586 if( rc==SQLITE_OK0 ){
240587 sqlite3Fts5Dequote(zCopy);
240588 if( sqlite3_stricmp(zCopy, "col")==0 ){
240589 *peType = FTS5_VOCAB_COL0;
240590 }else
240591
240592 if( sqlite3_stricmp(zCopy, "row")==0 ){
240593 *peType = FTS5_VOCAB_ROW1;
240594 }else
240595 if( sqlite3_stricmp(zCopy, "instance")==0 ){
240596 *peType = FTS5_VOCAB_INSTANCE2;
240597 }else
240598 {
240599 *pzErr = sqlite3_mprintf("fts5vocab: unknown table type: %Q", zCopy);
240600 rc = SQLITE_ERROR1;
240601 }
240602 sqlite3_free(zCopy);
240603 }
240604
240605 return rc;
240606}
240607
240608
240609/*
240610** The xDisconnect() virtual table method.
240611*/
240612static int fts5VocabDisconnectMethod(sqlite3_vtab *pVtab){
240613 Fts5VocabTable *pTab = (Fts5VocabTable*)pVtab;
240614 sqlite3_free(pTab);
240615 return SQLITE_OK0;
240616}
240617
240618/*
240619** The xDestroy() virtual table method.
240620*/
240621static int fts5VocabDestroyMethod(sqlite3_vtab *pVtab){
240622 Fts5VocabTable *pTab = (Fts5VocabTable*)pVtab;
240623 sqlite3_free(pTab);
240624 return SQLITE_OK0;
240625}
240626
240627/*
240628** This function is the implementation of both the xConnect and xCreate
240629** methods of the FTS3 virtual table.
240630**
240631** The argv[] array contains the following:
240632**
240633** argv[0] -> module name ("fts5vocab")
240634** argv[1] -> database name
240635** argv[2] -> table name
240636**
240637** then:
240638**
240639** argv[3] -> name of fts5 table
240640** argv[4] -> type of fts5vocab table
240641**
240642** or, for tables in the TEMP schema only.
240643**
240644** argv[3] -> name of fts5 tables database
240645** argv[4] -> name of fts5 table
240646** argv[5] -> type of fts5vocab table
240647*/
240648static int fts5VocabInitVtab(
240649 sqlite3 *db, /* The SQLite database connection */
240650 void *pAux, /* Pointer to Fts5Global object */
240651 int argc, /* Number of elements in argv array */
240652 const char * const *argv, /* xCreate/xConnect argument array */
240653 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
240654 char **pzErr /* Write any error message here */
240655){
240656 const char *azSchema[] = {
240657 "CREATE TABlE vocab(" FTS5_VOCAB_COL_SCHEMA"term, col, doc, cnt" ")",
240658 "CREATE TABlE vocab(" FTS5_VOCAB_ROW_SCHEMA"term, doc, cnt" ")",
240659 "CREATE TABlE vocab(" FTS5_VOCAB_INST_SCHEMA"term, doc, col, offset" ")"
240660 };
240661
240662 Fts5VocabTable *pRet = 0;
240663 int rc = SQLITE_OK0; /* Return code */
240664 int bDb;
240665
240666 bDb = (argc==6 && strlen(argv[1])==4 && memcmp("temp", argv[1], 4)==0);
240667
240668 if( argc!=5 && bDb==0 ){
240669 *pzErr = sqlite3_mprintf("wrong number of vtable arguments");
240670 rc = SQLITE_ERROR1;
240671 }else{
240672 int nByte; /* Bytes of space to allocate */
240673 const char *zDb = bDb ? argv[3] : argv[1];
240674 const char *zTab = bDb ? argv[4] : argv[3];
240675 const char *zType = bDb ? argv[5] : argv[4];
240676 int nDb = (int)strlen(zDb)+1;
240677 int nTab = (int)strlen(zTab)+1;
240678 int eType = 0;
240679
240680 rc = fts5VocabTableType(zType, pzErr, &eType);
240681 if( rc==SQLITE_OK0 ){
240682 assert( eType>=0 && eType<ArraySize(azSchema) )((void) (0));
240683 rc = sqlite3_declare_vtab(db, azSchema[eType]);
240684 }
240685
240686 nByte = sizeof(Fts5VocabTable) + nDb + nTab;
240687 pRet = sqlite3Fts5MallocZero(&rc, nByte);
240688 if( pRet ){
240689 pRet->pGlobal = (Fts5Global*)pAux;
240690 pRet->eType = eType;
240691 pRet->db = db;
240692 pRet->zFts5Tbl = (char*)&pRet[1];
240693 pRet->zFts5Db = &pRet->zFts5Tbl[nTab];
240694 memcpy(pRet->zFts5Tbl, zTab, nTab);
240695 memcpy(pRet->zFts5Db, zDb, nDb);
240696 sqlite3Fts5Dequote(pRet->zFts5Tbl);
240697 sqlite3Fts5Dequote(pRet->zFts5Db);
240698 }
240699 }
240700
240701 *ppVTab = (sqlite3_vtab*)pRet;
240702 return rc;
240703}
240704
240705
240706/*
240707** The xConnect() and xCreate() methods for the virtual table. All the
240708** work is done in function fts5VocabInitVtab().
240709*/
240710static int fts5VocabConnectMethod(
240711 sqlite3 *db, /* Database connection */
240712 void *pAux, /* Pointer to tokenizer hash table */
240713 int argc, /* Number of elements in argv array */
240714 const char * const *argv, /* xCreate/xConnect argument array */
240715 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
240716 char **pzErr /* OUT: sqlite3_malloc'd error message */
240717){
240718 return fts5VocabInitVtab(db, pAux, argc, argv, ppVtab, pzErr);
240719}
240720static int fts5VocabCreateMethod(
240721 sqlite3 *db, /* Database connection */
240722 void *pAux, /* Pointer to tokenizer hash table */
240723 int argc, /* Number of elements in argv array */
240724 const char * const *argv, /* xCreate/xConnect argument array */
240725 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
240726 char **pzErr /* OUT: sqlite3_malloc'd error message */
240727){
240728 return fts5VocabInitVtab(db, pAux, argc, argv, ppVtab, pzErr);
240729}
240730
240731/*
240732** Implementation of the xBestIndex method.
240733**
240734** Only constraints of the form:
240735**
240736** term <= ?
240737** term == ?
240738** term >= ?
240739**
240740** are interpreted. Less-than and less-than-or-equal are treated
240741** identically, as are greater-than and greater-than-or-equal.
240742*/
240743static int fts5VocabBestIndexMethod(
240744 sqlite3_vtab *pUnused,
240745 sqlite3_index_info *pInfo
240746){
240747 int i;
240748 int iTermEq = -1;
240749 int iTermGe = -1;
240750 int iTermLe = -1;
240751 int idxNum = 0;
240752 int nArg = 0;
240753
240754 UNUSED_PARAM(pUnused)(void)(pUnused);
240755
240756 for(i=0; i<pInfo->nConstraint; i++){
240757 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
240758 if( p->usable==0 ) continue;
240759 if( p->iColumn==0 ){ /* term column */
240760 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ2 ) iTermEq = i;
240761 if( p->op==SQLITE_INDEX_CONSTRAINT_LE8 ) iTermLe = i;
240762 if( p->op==SQLITE_INDEX_CONSTRAINT_LT16 ) iTermLe = i;
240763 if( p->op==SQLITE_INDEX_CONSTRAINT_GE32 ) iTermGe = i;
240764 if( p->op==SQLITE_INDEX_CONSTRAINT_GT4 ) iTermGe = i;
240765 }
240766 }
240767
240768 if( iTermEq>=0 ){
240769 idxNum |= FTS5_VOCAB_TERM_EQ0x01;
240770 pInfo->aConstraintUsage[iTermEq].argvIndex = ++nArg;
240771 pInfo->estimatedCost = 100;
240772 }else{
240773 pInfo->estimatedCost = 1000000;
240774 if( iTermGe>=0 ){
240775 idxNum |= FTS5_VOCAB_TERM_GE0x02;
240776 pInfo->aConstraintUsage[iTermGe].argvIndex = ++nArg;
240777 pInfo->estimatedCost = pInfo->estimatedCost / 2;
240778 }
240779 if( iTermLe>=0 ){
240780 idxNum |= FTS5_VOCAB_TERM_LE0x04;
240781 pInfo->aConstraintUsage[iTermLe].argvIndex = ++nArg;
240782 pInfo->estimatedCost = pInfo->estimatedCost / 2;
240783 }
240784 }
240785
240786 /* This virtual table always delivers results in ascending order of
240787 ** the "term" column (column 0). So if the user has requested this
240788 ** specifically - "ORDER BY term" or "ORDER BY term ASC" - set the
240789 ** sqlite3_index_info.orderByConsumed flag to tell the core the results
240790 ** are already in sorted order. */
240791 if( pInfo->nOrderBy==1
240792 && pInfo->aOrderBy[0].iColumn==0
240793 && pInfo->aOrderBy[0].desc==0
240794 ){
240795 pInfo->orderByConsumed = 1;
240796 }
240797
240798 pInfo->idxNum = idxNum;
240799 return SQLITE_OK0;
240800}
240801
240802/*
240803** Implementation of xOpen method.
240804*/
240805static int fts5VocabOpenMethod(
240806 sqlite3_vtab *pVTab,
240807 sqlite3_vtab_cursor **ppCsr
240808){
240809 Fts5VocabTable *pTab = (Fts5VocabTable*)pVTab;
240810 Fts5Table *pFts5 = 0;
240811 Fts5VocabCursor *pCsr = 0;
240812 int rc = SQLITE_OK0;
240813 sqlite3_stmt *pStmt = 0;
240814 char *zSql = 0;
240815
240816 if( pTab->bBusy ){
240817 pVTab->zErrMsg = sqlite3_mprintf(
240818 "recursive definition for %s.%s", pTab->zFts5Db, pTab->zFts5Tbl
240819 );
240820 return SQLITE_ERROR1;
240821 }
240822 zSql = sqlite3Fts5Mprintf(&rc,
240823 "SELECT t.%Q FROM %Q.%Q AS t WHERE t.%Q MATCH '*id'",
240824 pTab->zFts5Tbl, pTab->zFts5Db, pTab->zFts5Tbl, pTab->zFts5Tbl
240825 );
240826 if( zSql ){
240827 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
240828 }
240829 sqlite3_free(zSql);
240830 assert( rc==SQLITE_OK || pStmt==0 )((void) (0));
240831 if( rc==SQLITE_ERROR1 ) rc = SQLITE_OK0;
240832
240833 pTab->bBusy = 1;
240834 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW100 ){
240835 i64 iId = sqlite3_column_int64(pStmt, 0);
240836 pFts5 = sqlite3Fts5TableFromCsrid(pTab->pGlobal, iId);
240837 }
240838 pTab->bBusy = 0;
240839
240840 if( rc==SQLITE_OK0 ){
240841 if( pFts5==0 ){
240842 rc = sqlite3_finalize(pStmt);
240843 pStmt = 0;
240844 if( rc==SQLITE_OK0 ){
240845 pVTab->zErrMsg = sqlite3_mprintf(
240846 "no such fts5 table: %s.%s", pTab->zFts5Db, pTab->zFts5Tbl
240847 );
240848 rc = SQLITE_ERROR1;
240849 }
240850 }else{
240851 rc = sqlite3Fts5FlushToDisk(pFts5);
240852 }
240853 }
240854
240855 if( rc==SQLITE_OK0 ){
240856 i64 nByte = pFts5->pConfig->nCol * sizeof(i64)*2 + sizeof(Fts5VocabCursor);
240857 pCsr = (Fts5VocabCursor*)sqlite3Fts5MallocZero(&rc, nByte);
240858 }
240859
240860 if( pCsr ){
240861 pCsr->pFts5 = pFts5;
240862 pCsr->pStmt = pStmt;
240863 pCsr->aCnt = (i64*)&pCsr[1];
240864 pCsr->aDoc = &pCsr->aCnt[pFts5->pConfig->nCol];
240865 }else{
240866 sqlite3_finalize(pStmt);
240867 }
240868
240869 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
240870 return rc;
240871}
240872
240873static void fts5VocabResetCursor(Fts5VocabCursor *pCsr){
240874 pCsr->rowid = 0;
240875 sqlite3Fts5IterClose(pCsr->pIter);
240876 sqlite3Fts5StructureRelease(pCsr->pStruct);
240877 pCsr->pStruct = 0;
240878 pCsr->pIter = 0;
240879 sqlite3_free(pCsr->zLeTerm);
240880 pCsr->nLeTerm = -1;
240881 pCsr->zLeTerm = 0;
240882 pCsr->bEof = 0;
240883}
240884
240885/*
240886** Close the cursor. For additional information see the documentation
240887** on the xClose method of the virtual table interface.
240888*/
240889static int fts5VocabCloseMethod(sqlite3_vtab_cursor *pCursor){
240890 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
240891 fts5VocabResetCursor(pCsr);
240892 sqlite3Fts5BufferFree(&pCsr->term);
240893 sqlite3_finalize(pCsr->pStmt);
240894 sqlite3_free(pCsr);
240895 return SQLITE_OK0;
240896}
240897
240898static int fts5VocabInstanceNewTerm(Fts5VocabCursor *pCsr){
240899 int rc = SQLITE_OK0;
240900
240901 if( sqlite3Fts5IterEof(pCsr->pIter)((pCsr->pIter)->bEof) ){
240902 pCsr->bEof = 1;
240903 }else{
240904 const char *zTerm;
240905 int nTerm;
240906 zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
240907 if( pCsr->nLeTerm>=0 ){
240908 int nCmp = MIN(nTerm, pCsr->nLeTerm)((nTerm)<(pCsr->nLeTerm)?(nTerm):(pCsr->nLeTerm));
240909 int bCmp = memcmp(pCsr->zLeTerm, zTerm, nCmp);
240910 if( bCmp<0 || (bCmp==0 && pCsr->nLeTerm<nTerm) ){
240911 pCsr->bEof = 1;
240912 }
240913 }
240914
240915 sqlite3Fts5BufferSet(&rc, &pCsr->term, nTerm, (const u8*)zTerm);
240916 }
240917 return rc;
240918}
240919
240920static int fts5VocabInstanceNext(Fts5VocabCursor *pCsr){
240921 int eDetail = pCsr->pFts5->pConfig->eDetail;
240922 int rc = SQLITE_OK0;
240923 Fts5IndexIter *pIter = pCsr->pIter;
240924 i64 *pp = &pCsr->iInstPos;
240925 int *po = &pCsr->iInstOff;
240926
240927 assert( sqlite3Fts5IterEof(pIter)==0 )((void) (0));
240928 assert( pCsr->bEof==0 )((void) (0));
240929 while( eDetail==FTS5_DETAIL_NONE1
240930 || sqlite3Fts5PoslistNext64(pIter->pData, pIter->nData, po, pp)
240931 ){
240932 pCsr->iInstPos = 0;
240933 pCsr->iInstOff = 0;
240934
240935 rc = sqlite3Fts5IterNextScan(pCsr->pIter);
240936 if( rc==SQLITE_OK0 ){
240937 rc = fts5VocabInstanceNewTerm(pCsr);
240938 if( pCsr->bEof || eDetail==FTS5_DETAIL_NONE1 ) break;
240939 }
240940 if( rc ){
240941 pCsr->bEof = 1;
240942 break;
240943 }
240944 }
240945
240946 return rc;
240947}
240948
240949/*
240950** Advance the cursor to the next row in the table.
240951*/
240952static int fts5VocabNextMethod(sqlite3_vtab_cursor *pCursor){
240953 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
240954 Fts5VocabTable *pTab = (Fts5VocabTable*)pCursor->pVtab;
240955 int nCol = pCsr->pFts5->pConfig->nCol;
240956 int rc;
240957
240958 rc = sqlite3Fts5StructureTest(pCsr->pFts5->pIndex, pCsr->pStruct);
240959 if( rc!=SQLITE_OK0 ) return rc;
240960 pCsr->rowid++;
240961
240962 if( pTab->eType==FTS5_VOCAB_INSTANCE2 ){
240963 return fts5VocabInstanceNext(pCsr);
240964 }
240965
240966 if( pTab->eType==FTS5_VOCAB_COL0 ){
240967 for(pCsr->iCol++; pCsr->iCol<nCol; pCsr->iCol++){
240968 if( pCsr->aDoc[pCsr->iCol] ) break;
240969 }
240970 }
240971
240972 if( pTab->eType!=FTS5_VOCAB_COL0 || pCsr->iCol>=nCol ){
240973 if( sqlite3Fts5IterEof(pCsr->pIter)((pCsr->pIter)->bEof) ){
240974 pCsr->bEof = 1;
240975 }else{
240976 const char *zTerm;
240977 int nTerm;
240978
240979 zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
240980 assert( nTerm>=0 )((void) (0));
240981 if( pCsr->nLeTerm>=0 ){
240982 int nCmp = MIN(nTerm, pCsr->nLeTerm)((nTerm)<(pCsr->nLeTerm)?(nTerm):(pCsr->nLeTerm));
240983 int bCmp = memcmp(pCsr->zLeTerm, zTerm, nCmp);
240984 if( bCmp<0 || (bCmp==0 && pCsr->nLeTerm<nTerm) ){
240985 pCsr->bEof = 1;
240986 return SQLITE_OK0;
240987 }
240988 }
240989
240990 sqlite3Fts5BufferSet(&rc, &pCsr->term, nTerm, (const u8*)zTerm);
240991 memset(pCsr->aCnt, 0, nCol * sizeof(i64));
240992 memset(pCsr->aDoc, 0, nCol * sizeof(i64));
240993 pCsr->iCol = 0;
240994
240995 assert( pTab->eType==FTS5_VOCAB_COL || pTab->eType==FTS5_VOCAB_ROW )((void) (0));
240996 while( rc==SQLITE_OK0 ){
240997 int eDetail = pCsr->pFts5->pConfig->eDetail;
240998 const u8 *pPos; int nPos; /* Position list */
240999 i64 iPos = 0; /* 64-bit position read from poslist */
241000 int iOff = 0; /* Current offset within position list */
241001
241002 pPos = pCsr->pIter->pData;
241003 nPos = pCsr->pIter->nData;
241004
241005 switch( pTab->eType ){
241006 case FTS5_VOCAB_ROW1:
241007 if( eDetail==FTS5_DETAIL_FULL0 ){
241008 while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){
241009 pCsr->aCnt[0]++;
241010 }
241011 }
241012 pCsr->aDoc[0]++;
241013 break;
241014
241015 case FTS5_VOCAB_COL0:
241016 if( eDetail==FTS5_DETAIL_FULL0 ){
241017 int iCol = -1;
241018 while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){
241019 int ii = FTS5_POS2COLUMN(iPos)(int)(iPos >> 32);
241020 if( iCol!=ii ){
241021 if( ii>=nCol ){
241022 rc = FTS5_CORRUPT(11 | (1<<8));
241023 break;
241024 }
241025 pCsr->aDoc[ii]++;
241026 iCol = ii;
241027 }
241028 pCsr->aCnt[ii]++;
241029 }
241030 }else if( eDetail==FTS5_DETAIL_COLUMNS2 ){
241031 while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff,&iPos) ){
241032 assert_nc( iPos>=0 && iPos<nCol )((void) (0));
241033 if( iPos>=nCol ){
241034 rc = FTS5_CORRUPT(11 | (1<<8));
241035 break;
241036 }
241037 pCsr->aDoc[iPos]++;
241038 }
241039 }else{
241040 assert( eDetail==FTS5_DETAIL_NONE )((void) (0));
241041 pCsr->aDoc[0]++;
241042 }
241043 break;
241044
241045 default:
241046 assert( pTab->eType==FTS5_VOCAB_INSTANCE )((void) (0));
241047 break;
241048 }
241049
241050 if( rc==SQLITE_OK0 ){
241051 rc = sqlite3Fts5IterNextScan(pCsr->pIter);
241052 }
241053 if( pTab->eType==FTS5_VOCAB_INSTANCE2 ) break;
241054
241055 if( rc==SQLITE_OK0 ){
241056 zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
241057 if( nTerm!=pCsr->term.n
241058 || (nTerm>0 && memcmp(zTerm, pCsr->term.p, nTerm))
241059 ){
241060 break;
241061 }
241062 if( sqlite3Fts5IterEof(pCsr->pIter)((pCsr->pIter)->bEof) ) break;
241063 }
241064 }
241065 }
241066 }
241067
241068 if( rc==SQLITE_OK0 && pCsr->bEof==0 && pTab->eType==FTS5_VOCAB_COL0 ){
241069 for(/* noop */; pCsr->iCol<nCol && pCsr->aDoc[pCsr->iCol]==0; pCsr->iCol++);
241070 if( pCsr->iCol==nCol ){
241071 rc = FTS5_CORRUPT(11 | (1<<8));
241072 }
241073 }
241074 return rc;
241075}
241076
241077/*
241078** This is the xFilter implementation for the virtual table.
241079*/
241080static int fts5VocabFilterMethod(
241081 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
241082 int idxNum, /* Strategy index */
241083 const char *zUnused, /* Unused */
241084 int nUnused, /* Number of elements in apVal */
241085 sqlite3_value **apVal /* Arguments for the indexing scheme */
241086){
241087 Fts5VocabTable *pTab = (Fts5VocabTable*)pCursor->pVtab;
241088 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
241089 int eType = pTab->eType;
241090 int rc = SQLITE_OK0;
241091
241092 int iVal = 0;
241093 int f = FTS5INDEX_QUERY_SCAN0x0008;
241094 const char *zTerm = 0;
241095 int nTerm = 0;
241096
241097 sqlite3_value *pEq = 0;
241098 sqlite3_value *pGe = 0;
241099 sqlite3_value *pLe = 0;
241100
241101 UNUSED_PARAM2(zUnused, nUnused)(void)(zUnused), (void)(nUnused);
241102
241103 fts5VocabResetCursor(pCsr);
241104 if( idxNum & FTS5_VOCAB_TERM_EQ0x01 ) pEq = apVal[iVal++];
241105 if( idxNum & FTS5_VOCAB_TERM_GE0x02 ) pGe = apVal[iVal++];
241106 if( idxNum & FTS5_VOCAB_TERM_LE0x04 ) pLe = apVal[iVal++];
241107
241108 if( pEq ){
241109 zTerm = (const char *)sqlite3_value_text(pEq);
241110 nTerm = sqlite3_value_bytes(pEq);
241111 f = 0;
241112 }else{
241113 if( pGe ){
241114 zTerm = (const char *)sqlite3_value_text(pGe);
241115 nTerm = sqlite3_value_bytes(pGe);
241116 }
241117 if( pLe ){
241118 const char *zCopy = (const char *)sqlite3_value_text(pLe);
241119 if( zCopy==0 ) zCopy = "";
241120 pCsr->nLeTerm = sqlite3_value_bytes(pLe);
241121 pCsr->zLeTerm = sqlite3_malloc(pCsr->nLeTerm+1);
241122 if( pCsr->zLeTerm==0 ){
241123 rc = SQLITE_NOMEM7;
241124 }else{
241125 memcpy(pCsr->zLeTerm, zCopy, pCsr->nLeTerm+1);
241126 }
241127 }
241128 }
241129
241130 if( rc==SQLITE_OK0 ){
241131 Fts5Index *pIndex = pCsr->pFts5->pIndex;
241132 rc = sqlite3Fts5IndexQuery(pIndex, zTerm, nTerm, f, 0, &pCsr->pIter);
241133 if( rc==SQLITE_OK0 ){
241134 pCsr->pStruct = sqlite3Fts5StructureRef(pIndex);
241135 }
241136 }
241137 if( rc==SQLITE_OK0 && eType==FTS5_VOCAB_INSTANCE2 ){
241138 rc = fts5VocabInstanceNewTerm(pCsr);
241139 }
241140 if( rc==SQLITE_OK0 && !pCsr->bEof
241141 && (eType!=FTS5_VOCAB_INSTANCE2
241142 || pCsr->pFts5->pConfig->eDetail!=FTS5_DETAIL_NONE1)
241143 ){
241144 rc = fts5VocabNextMethod(pCursor);
241145 }
241146
241147 return rc;
241148}
241149
241150/*
241151** This is the xEof method of the virtual table. SQLite calls this
241152** routine to find out if it has reached the end of a result set.
241153*/
241154static int fts5VocabEofMethod(sqlite3_vtab_cursor *pCursor){
241155 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
241156 return pCsr->bEof;
241157}
241158
241159static int fts5VocabColumnMethod(
241160 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
241161 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
241162 int iCol /* Index of column to read value from */
241163){
241164 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
241165 int eDetail = pCsr->pFts5->pConfig->eDetail;
241166 int eType = ((Fts5VocabTable*)(pCursor->pVtab))->eType;
241167 i64 iVal = 0;
241168
241169 if( iCol==0 ){
241170 sqlite3_result_text(
241171 pCtx, (const char*)pCsr->term.p, pCsr->term.n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1)
241172 );
241173 }else if( eType==FTS5_VOCAB_COL0 ){
241174 assert( iCol==1 || iCol==2 || iCol==3 )((void) (0));
241175 if( iCol==1 ){
241176 if( eDetail!=FTS5_DETAIL_NONE1 ){
241177 const char *z = pCsr->pFts5->pConfig->azCol[pCsr->iCol];
241178 sqlite3_result_text(pCtx, z, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
241179 }
241180 }else if( iCol==2 ){
241181 iVal = pCsr->aDoc[pCsr->iCol];
241182 }else{
241183 iVal = pCsr->aCnt[pCsr->iCol];
241184 }
241185 }else if( eType==FTS5_VOCAB_ROW1 ){
241186 assert( iCol==1 || iCol==2 )((void) (0));
241187 if( iCol==1 ){
241188 iVal = pCsr->aDoc[0];
241189 }else{
241190 iVal = pCsr->aCnt[0];
241191 }
241192 }else{
241193 assert( eType==FTS5_VOCAB_INSTANCE )((void) (0));
241194 switch( iCol ){
241195 case 1:
241196 sqlite3_result_int64(pCtx, pCsr->pIter->iRowid);
241197 break;
241198 case 2: {
241199 int ii = -1;
241200 if( eDetail==FTS5_DETAIL_FULL0 ){
241201 ii = FTS5_POS2COLUMN(pCsr->iInstPos)(int)(pCsr->iInstPos >> 32);
241202 }else if( eDetail==FTS5_DETAIL_COLUMNS2 ){
241203 ii = (int)pCsr->iInstPos;
241204 }
241205 if( ii>=0 && ii<pCsr->pFts5->pConfig->nCol ){
241206 const char *z = pCsr->pFts5->pConfig->azCol[ii];
241207 sqlite3_result_text(pCtx, z, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
241208 }
241209 break;
241210 }
241211 default: {
241212 assert( iCol==3 )((void) (0));
241213 if( eDetail==FTS5_DETAIL_FULL0 ){
241214 int ii = FTS5_POS2OFFSET(pCsr->iInstPos)(int)(pCsr->iInstPos & 0x7FFFFFFF);
241215 sqlite3_result_int(pCtx, ii);
241216 }
241217 break;
241218 }
241219 }
241220 }
241221
241222 if( iVal>0 ) sqlite3_result_int64(pCtx, iVal);
241223 return SQLITE_OK0;
241224}
241225
241226/*
241227** This is the xRowid method. The SQLite core calls this routine to
241228** retrieve the rowid for the current row of the result set. The
241229** rowid should be written to *pRowid.
241230*/
241231static int fts5VocabRowidMethod(
241232 sqlite3_vtab_cursor *pCursor,
241233 sqlite_int64 *pRowid
241234){
241235 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
241236 *pRowid = pCsr->rowid;
241237 return SQLITE_OK0;
241238}
241239
241240static int sqlite3Fts5VocabInit(Fts5Global *pGlobal, sqlite3 *db){
241241 static const sqlite3_module fts5Vocab = {
241242 /* iVersion */ 2,
241243 /* xCreate */ fts5VocabCreateMethod,
241244 /* xConnect */ fts5VocabConnectMethod,
241245 /* xBestIndex */ fts5VocabBestIndexMethod,
241246 /* xDisconnect */ fts5VocabDisconnectMethod,
241247 /* xDestroy */ fts5VocabDestroyMethod,
241248 /* xOpen */ fts5VocabOpenMethod,
241249 /* xClose */ fts5VocabCloseMethod,
241250 /* xFilter */ fts5VocabFilterMethod,
241251 /* xNext */ fts5VocabNextMethod,
241252 /* xEof */ fts5VocabEofMethod,
241253 /* xColumn */ fts5VocabColumnMethod,
241254 /* xRowid */ fts5VocabRowidMethod,
241255 /* xUpdate */ 0,
241256 /* xBegin */ 0,
241257 /* xSync */ 0,
241258 /* xCommit */ 0,
241259 /* xRollback */ 0,
241260 /* xFindFunction */ 0,
241261 /* xRename */ 0,
241262 /* xSavepoint */ 0,
241263 /* xRelease */ 0,
241264 /* xRollbackTo */ 0,
241265 /* xShadowName */ 0
241266 };
241267 void *p = (void*)pGlobal;
241268
241269 return sqlite3_create_module_v2(db, "fts5vocab", &fts5Vocab, p, 0);
241270}
241271
241272
241273
241274#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5) */
241275
241276/************** End of fts5.c ************************************************/
241277/************** Begin file stmt.c ********************************************/
241278/*
241279** 2017-05-31
241280**
241281** The author disclaims copyright to this source code. In place of
241282** a legal notice, here is a blessing:
241283**
241284** May you do good and not evil.
241285** May you find forgiveness for yourself and forgive others.
241286** May you share freely, never taking more than you give.
241287**
241288*************************************************************************
241289**
241290** This file demonstrates an eponymous virtual table that returns information
241291** about all prepared statements for the database connection.
241292**
241293** Usage example:
241294**
241295** .load ./stmt
241296** .mode line
241297** .header on
241298** SELECT * FROM stmt;
241299*/
241300#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_STMTVTAB)
241301#if !defined(SQLITEINT_H)
241302/* #include "sqlite3ext.h" */
241303#endif
241304SQLITE_EXTENSION_INIT1
241305/* #include <assert.h> */
241306/* #include <string.h> */
241307
241308#ifndef SQLITE_OMIT_VIRTUALTABLE
241309
241310
241311#define STMT_NUM_INTEGER_COLUMN 10
241312typedef struct StmtRow StmtRow;
241313struct StmtRow {
241314 sqlite3_int64 iRowid; /* Rowid value */
241315 char *zSql; /* column "sql" */
241316 int aCol[STMT_NUM_INTEGER_COLUMN+1]; /* all other column values */
241317 StmtRow *pNext; /* Next row to return */
241318};
241319
241320/* stmt_vtab is a subclass of sqlite3_vtab which will
241321** serve as the underlying representation of a stmt virtual table
241322*/
241323typedef struct stmt_vtab stmt_vtab;
241324struct stmt_vtab {
241325 sqlite3_vtab base; /* Base class - must be first */
241326 sqlite3 *db; /* Database connection for this stmt vtab */
241327};
241328
241329/* stmt_cursor is a subclass of sqlite3_vtab_cursor which will
241330** serve as the underlying representation of a cursor that scans
241331** over rows of the result
241332*/
241333typedef struct stmt_cursor stmt_cursor;
241334struct stmt_cursor {
241335 sqlite3_vtab_cursor base; /* Base class - must be first */
241336 sqlite3 *db; /* Database connection for this cursor */
241337 StmtRow *pRow; /* Current row */
241338};
241339
241340/*
241341** The stmtConnect() method is invoked to create a new
241342** stmt_vtab that describes the stmt virtual table.
241343**
241344** Think of this routine as the constructor for stmt_vtab objects.
241345**
241346** All this routine needs to do is:
241347**
241348** (1) Allocate the stmt_vtab object and initialize all fields.
241349**
241350** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
241351** result set of queries against stmt will look like.
241352*/
241353static int stmtConnect(
241354 sqlite3 *db,
241355 void *pAux,
241356 int argc, const char *const*argv,
241357 sqlite3_vtab **ppVtab,
241358 char **pzErr
241359){
241360 stmt_vtab *pNew;
241361 int rc;
241362
241363/* Column numbers */
241364#define STMT_COLUMN_SQL 0 /* SQL for the statement */
241365#define STMT_COLUMN_NCOL 1 /* Number of result columns */
241366#define STMT_COLUMN_RO 2 /* True if read-only */
241367#define STMT_COLUMN_BUSY 3 /* True if currently busy */
241368#define STMT_COLUMN_NSCAN 4 /* SQLITE_STMTSTATUS_FULLSCAN_STEP */
241369#define STMT_COLUMN_NSORT 5 /* SQLITE_STMTSTATUS_SORT */
241370#define STMT_COLUMN_NAIDX 6 /* SQLITE_STMTSTATUS_AUTOINDEX */
241371#define STMT_COLUMN_NSTEP 7 /* SQLITE_STMTSTATUS_VM_STEP */
241372#define STMT_COLUMN_REPREP 8 /* SQLITE_STMTSTATUS_REPREPARE */
241373#define STMT_COLUMN_RUN 9 /* SQLITE_STMTSTATUS_RUN */
241374#define STMT_COLUMN_MEM 10 /* SQLITE_STMTSTATUS_MEMUSED */
241375
241376
241377 rc = sqlite3_declare_vtab(db,
241378 "CREATE TABLE x(sql,ncol,ro,busy,nscan,nsort,naidx,nstep,"
241379 "reprep,run,mem)");
241380 if( rc==SQLITE_OK0 ){
241381 pNew = sqlite3_malloc64( sizeof(*pNew) );
241382 *ppVtab = (sqlite3_vtab*)pNew;
241383 if( pNew==0 ) return SQLITE_NOMEM7;
241384 memset(pNew, 0, sizeof(*pNew));
241385 pNew->db = db;
241386 }
241387 return rc;
241388}
241389
241390/*
241391** This method is the destructor for stmt_cursor objects.
241392*/
241393static int stmtDisconnect(sqlite3_vtab *pVtab){
241394 sqlite3_free(pVtab);
241395 return SQLITE_OK0;
241396}
241397
241398/*
241399** Constructor for a new stmt_cursor object.
241400*/
241401static int stmtOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
241402 stmt_cursor *pCur;
241403 pCur = sqlite3_malloc64( sizeof(*pCur) );
241404 if( pCur==0 ) return SQLITE_NOMEM7;
241405 memset(pCur, 0, sizeof(*pCur));
241406 pCur->db = ((stmt_vtab*)p)->db;
241407 *ppCursor = &pCur->base;
241408 return SQLITE_OK0;
241409}
241410
241411static void stmtCsrReset(stmt_cursor *pCur){
241412 StmtRow *pRow = 0;
241413 StmtRow *pNext = 0;
241414 for(pRow=pCur->pRow; pRow; pRow=pNext){
241415 pNext = pRow->pNext;
241416 sqlite3_free(pRow);
241417 }
241418 pCur->pRow = 0;
241419}
241420
241421/*
241422** Destructor for a stmt_cursor.
241423*/
241424static int stmtClose(sqlite3_vtab_cursor *cur){
241425 stmtCsrReset((stmt_cursor*)cur);
241426 sqlite3_free(cur);
241427 return SQLITE_OK0;
241428}
241429
241430
241431/*
241432** Advance a stmt_cursor to its next row of output.
241433*/
241434static int stmtNext(sqlite3_vtab_cursor *cur){
241435 stmt_cursor *pCur = (stmt_cursor*)cur;
241436 StmtRow *pNext = pCur->pRow->pNext;
241437 sqlite3_free(pCur->pRow);
241438 pCur->pRow = pNext;
241439 return SQLITE_OK0;
241440}
241441
241442/*
241443** Return values of columns for the row at which the stmt_cursor
241444** is currently pointing.
241445*/
241446static int stmtColumn(
241447 sqlite3_vtab_cursor *cur, /* The cursor */
241448 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
241449 int i /* Which column to return */
241450){
241451 stmt_cursor *pCur = (stmt_cursor*)cur;
241452 StmtRow *pRow = pCur->pRow;
241453 if( i==STMT_COLUMN_SQL ){
241454 sqlite3_result_text(ctx, pRow->zSql, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
241455 }else{
241456 sqlite3_result_int(ctx, pRow->aCol[i]);
241457 }
241458 return SQLITE_OK0;
241459}
241460
241461/*
241462** Return the rowid for the current row. In this implementation, the
241463** rowid is the same as the output value.
241464*/
241465static int stmtRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
241466 stmt_cursor *pCur = (stmt_cursor*)cur;
241467 *pRowid = pCur->pRow->iRowid;
241468 return SQLITE_OK0;
241469}
241470
241471/*
241472** Return TRUE if the cursor has been moved off of the last
241473** row of output.
241474*/
241475static int stmtEof(sqlite3_vtab_cursor *cur){
241476 stmt_cursor *pCur = (stmt_cursor*)cur;
241477 return pCur->pRow==0;
241478}
241479
241480/*
241481** This method is called to "rewind" the stmt_cursor object back
241482** to the first row of output. This method is always called at least
241483** once prior to any call to stmtColumn() or stmtRowid() or
241484** stmtEof().
241485*/
241486static int stmtFilter(
241487 sqlite3_vtab_cursor *pVtabCursor,
241488 int idxNum, const char *idxStr,
241489 int argc, sqlite3_value **argv
241490){
241491 stmt_cursor *pCur = (stmt_cursor *)pVtabCursor;
241492 sqlite3_stmt *p = 0;
241493 sqlite3_int64 iRowid = 1;
241494 StmtRow **ppRow = 0;
241495
241496 stmtCsrReset(pCur);
241497 ppRow = &pCur->pRow;
241498 for(p=sqlite3_next_stmt(pCur->db, 0); p; p=sqlite3_next_stmt(pCur->db, p)){
241499 const char *zSql = sqlite3_sql(p);
241500 sqlite3_int64 nSql = zSql ? strlen(zSql)+1 : 0;
241501 StmtRow *pNew = (StmtRow*)sqlite3_malloc64(sizeof(StmtRow) + nSql);
241502
241503 if( pNew==0 ) return SQLITE_NOMEM7;
241504 memset(pNew, 0, sizeof(StmtRow));
241505 if( zSql ){
241506 pNew->zSql = (char*)&pNew[1];
241507 memcpy(pNew->zSql, zSql, nSql);
241508 }
241509 pNew->aCol[STMT_COLUMN_NCOL] = sqlite3_column_count(p);
241510 pNew->aCol[STMT_COLUMN_RO] = sqlite3_stmt_readonly(p);
241511 pNew->aCol[STMT_COLUMN_BUSY] = sqlite3_stmt_busy(p);
241512 pNew->aCol[STMT_COLUMN_NSCAN] = sqlite3_stmt_status(
241513 p, SQLITE_STMTSTATUS_FULLSCAN_STEP1, 0
241514 );
241515 pNew->aCol[STMT_COLUMN_NSORT] = sqlite3_stmt_status(
241516 p, SQLITE_STMTSTATUS_SORT2, 0
241517 );
241518 pNew->aCol[STMT_COLUMN_NAIDX] = sqlite3_stmt_status(
241519 p, SQLITE_STMTSTATUS_AUTOINDEX3, 0
241520 );
241521 pNew->aCol[STMT_COLUMN_NSTEP] = sqlite3_stmt_status(
241522 p, SQLITE_STMTSTATUS_VM_STEP4, 0
241523 );
241524 pNew->aCol[STMT_COLUMN_REPREP] = sqlite3_stmt_status(
241525 p, SQLITE_STMTSTATUS_REPREPARE5, 0
241526 );
241527 pNew->aCol[STMT_COLUMN_RUN] = sqlite3_stmt_status(
241528 p, SQLITE_STMTSTATUS_RUN6, 0
241529 );
241530 pNew->aCol[STMT_COLUMN_MEM] = sqlite3_stmt_status(
241531 p, SQLITE_STMTSTATUS_MEMUSED99, 0
241532 );
241533 pNew->iRowid = iRowid++;
241534 *ppRow = pNew;
241535 ppRow = &pNew->pNext;
241536 }
241537
241538 return SQLITE_OK0;
241539}
241540
241541/*
241542** SQLite will invoke this method one or more times while planning a query
241543** that uses the stmt virtual table. This routine needs to create
241544** a query plan for each invocation and compute an estimated cost for that
241545** plan.
241546*/
241547static int stmtBestIndex(
241548 sqlite3_vtab *tab,
241549 sqlite3_index_info *pIdxInfo
241550){
241551 pIdxInfo->estimatedCost = (double)500;
241552 pIdxInfo->estimatedRows = 500;
241553 return SQLITE_OK0;
241554}
241555
241556/*
241557** This following structure defines all the methods for the
241558** stmt virtual table.
241559*/
241560static sqlite3_module stmtModule = {
241561 0, /* iVersion */
241562 0, /* xCreate */
241563 stmtConnect, /* xConnect */
241564 stmtBestIndex, /* xBestIndex */
241565 stmtDisconnect, /* xDisconnect */
241566 0, /* xDestroy */
241567 stmtOpen, /* xOpen - open a cursor */
241568 stmtClose, /* xClose - close a cursor */
241569 stmtFilter, /* xFilter - configure scan constraints */
241570 stmtNext, /* xNext - advance a cursor */
241571 stmtEof, /* xEof - check for end of scan */
241572 stmtColumn, /* xColumn - read data */
241573 stmtRowid, /* xRowid - read data */
241574 0, /* xUpdate */
241575 0, /* xBegin */
241576 0, /* xSync */
241577 0, /* xCommit */
241578 0, /* xRollback */
241579 0, /* xFindMethod */
241580 0, /* xRename */
241581 0, /* xSavepoint */
241582 0, /* xRelease */
241583 0, /* xRollbackTo */
241584 0, /* xShadowName */
241585};
241586
241587#endif /* SQLITE_OMIT_VIRTUALTABLE */
241588
241589SQLITE_PRIVATEstatic int sqlite3StmtVtabInit(sqlite3 *db){
241590 int rc = SQLITE_OK0;
241591#ifndef SQLITE_OMIT_VIRTUALTABLE
241592 rc = sqlite3_create_module(db, "sqlite_stmt", &stmtModule, 0);
241593#endif
241594 return rc;
241595}
241596
241597#ifndef SQLITE_CORE1
241598#ifdef _WIN32
241599__declspec(dllexport)
241600#endif
241601SQLITE_API int sqlite3_stmt_init(
241602 sqlite3 *db,
241603 char **pzErrMsg,
241604 const sqlite3_api_routines *pApi
241605){
241606 int rc = SQLITE_OK0;
241607 SQLITE_EXTENSION_INIT2(pApi)(void)pApi;;
241608#ifndef SQLITE_OMIT_VIRTUALTABLE
241609 rc = sqlite3StmtVtabInit(db);
241610#endif
241611 return rc;
241612}
241613#endif /* SQLITE_CORE */
241614#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
241615
241616/************** End of stmt.c ************************************************/
241617/* Return the source-id for this library */
241618SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID"2022-07-21 15:24:47 698edb77537b67c41adc68f9b892db56bcf9a55e00371a61420f3ddd668e6603"; }
241619/************************** End of sqlite3.c ******************************/